Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: add file name in stack traces for compiled classes #1773

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,7 @@ private byte[] generateCode(String rawSource) {
boolean hasFunctions = (scriptOrFnNodes.length > 1 || !hasScript);
boolean isStrictMode = scriptOrFnNodes[0].isInStrictMode();

String sourceFile = null;
if (compilerEnv.isGenerateDebugInfo()) {
sourceFile = scriptOrFnNodes[0].getSourceName();
}

String sourceFile = scriptOrFnNodes[0].getSourceName();
ClassFileWriter cfw = new ClassFileWriter(mainClassName, SUPER_CLASS_NAME, sourceFile);
cfw.addField(ID_FIELD_NAME, "I", ACC_PRIVATE);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.mozilla.javascript.tests.commonjs.module;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.InputStreamReader;
Expand All @@ -10,11 +12,14 @@
import java.util.Collections;
import org.junit.Test;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.RhinoException;
import org.mozilla.javascript.ScriptStackElement;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.commonjs.module.Require;
import org.mozilla.javascript.commonjs.module.provider.StrongCachingModuleScriptProvider;
import org.mozilla.javascript.commonjs.module.provider.UrlModuleSourceProvider;
import org.mozilla.javascript.tests.Utils;

/**
* @author Attila Szegedi
Expand Down Expand Up @@ -137,6 +142,32 @@ public void setMainForAlreadyLoadedModule() throws Exception {
}
}

@Test
public void stackTracesAlwaysHaveFileName() {
Utils.runWithAllModes(
cx -> {
cx.setGeneratingDebug(false);
final Scriptable scope = cx.initStandardObjects();
try {
final Require require = getSandboxedRequire(cx, scope, false);
require.install(scope);
RhinoException rhinoException =
assertThrows(
RhinoException.class,
() -> require.requireMain(cx, "throw-one"));

ScriptStackElement[] stack = rhinoException.getScriptStack();
assertEquals(2, stack.length);

assertTrue(stack[0].fileName.contains("throw-two.js"));
assertTrue(stack[1].fileName.contains("throw-one.js"));
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
});
}

private Reader getReader(String name) {
return new InputStreamReader(getClass().getResourceAsStream(name));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const two = require('throw-two');
two();
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function() {
throw new Error('hey')
}
Loading