diff --git a/rhino/src/main/java/org/mozilla/javascript/optimizer/Codegen.java b/rhino/src/main/java/org/mozilla/javascript/optimizer/Codegen.java index 08bd386616..96a9e15ca8 100644 --- a/rhino/src/main/java/org/mozilla/javascript/optimizer/Codegen.java +++ b/rhino/src/main/java/org/mozilla/javascript/optimizer/Codegen.java @@ -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); diff --git a/tests/src/test/java/org/mozilla/javascript/tests/commonjs/module/RequireTest.java b/tests/src/test/java/org/mozilla/javascript/tests/commonjs/module/RequireTest.java index 8f9785ac2c..6d68d9f481 100644 --- a/tests/src/test/java/org/mozilla/javascript/tests/commonjs/module/RequireTest.java +++ b/tests/src/test/java/org/mozilla/javascript/tests/commonjs/module/RequireTest.java @@ -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; @@ -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 @@ -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)); } diff --git a/tests/src/test/resources/org/mozilla/javascript/tests/commonjs/module/throw-one.js b/tests/src/test/resources/org/mozilla/javascript/tests/commonjs/module/throw-one.js new file mode 100644 index 0000000000..b3523d280d --- /dev/null +++ b/tests/src/test/resources/org/mozilla/javascript/tests/commonjs/module/throw-one.js @@ -0,0 +1,2 @@ +const two = require('throw-two'); +two(); \ No newline at end of file diff --git a/tests/src/test/resources/org/mozilla/javascript/tests/commonjs/module/throw-two.js b/tests/src/test/resources/org/mozilla/javascript/tests/commonjs/module/throw-two.js new file mode 100644 index 0000000000..86ad37b601 --- /dev/null +++ b/tests/src/test/resources/org/mozilla/javascript/tests/commonjs/module/throw-two.js @@ -0,0 +1,3 @@ +module.exports = function() { + throw new Error('hey') +} \ No newline at end of file