malaporte / nashorn-commonjs-modules

CommonJS modules support for Nashorn
MIT License
108 stars 31 forks source link

Javascript files with comments on last line break module loading #22

Closed apottere closed 7 years ago

apottere commented 7 years ago

This line seems to be the culprit:

engine.eval("(function (exports, require, module, __filename, __dirname) {" + code + "})");

When code contains a JS file string where the last line ends in a comment, it comments out the end brackets, }), and parsing fails. Adding a newline would most likely fix this:

engine.eval("(function (exports, require, module, __filename, __dirname) {" + code + "\n})");
malaporte commented 7 years ago

Thanks for the report! I must made a fix for this.

iUwej commented 7 years ago

@malaporte the maven repo doesn't seem to have this fix. Tried executing some program and got this

java.lang.RuntimeException: javax.script.ScriptException: /Users/suiluj/projects/design-portal-frontend/node_modules/reflect-metadata/Reflect.js:1125:37 Expected } but found eof
//# sourceMappingURL=Reflect.js.map})
                                     ^ in /Users/suiluj/projects/design-portal-frontend/node_modules/reflect-metadata/Reflect.js at line number 1125 at column number 37
    at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:397)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:449)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:406)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:402)
    at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:150)
    at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:249)
    at example.Hello$.delayedEndpoint$example$Hello$1(Hello.scala:15)
    at example.Hello$delayedInit$body.apply(Hello.scala:8)
    at scala.Function0.apply$mcV$sp(Function0.scala:34)
    at scala.Function0.apply$mcV$sp$(Function0.scala:34)
    at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
    at scala.App.$anonfun$main$1$adapted(App.scala:76)
    at scala.collection.immutable.List.foreach(List.scala:378)
    at scala.App.main(App.scala:76)
    at scala.App.main$(App.scala:74)
    at example.Hello$.main(Hello.scala:8)
    at example.Hello.main(Hello.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
Caused by: javax.script.ScriptException: /Users/suiluj/projects/design-portal-frontend/node_modules/reflect-metadata/Reflect.js:1125:37 Expected } but found eof
//# sourceMappingURL=Reflect.js.map})
                                     ^ in /Users/suiluj/projects/design-portal-frontend/node_modules/reflect-metadata/Reflect.js at line number 1125 at column number 37
    at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:470)
    at jdk.nashorn.api.scripting.NashornScriptEngine.compileImpl(NashornScriptEngine.java:537)
    at jdk.nashorn.api.scripting.NashornScriptEngine.compileImpl(NashornScriptEngine.java:524)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:402)
    at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:155)
    at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
    at com.coveo.nashorn_modules.Module.compileJavaScriptModule(Module.java:332)
    at com.coveo.nashorn_modules.Module.compileModuleAndPutInCache(Module.java:288)
    at com.coveo.nashorn_modules.Module.loadModuleAsFile(Module.java:202)
    at com.coveo.nashorn_modules.Module.loadModuleThroughPackageJson(Module.java:247)
    at com.coveo.nashorn_modules.Module.loadModuleAsFolder(Module.java:215)
    at com.coveo.nashorn_modules.Module.attemptToLoadFromThisFolder(Module.java:181)
    at com.coveo.nashorn_modules.Module.searchForModuleInNodeModules(Module.java:150)
    at com.coveo.nashorn_modules.Module.require(Module.java:122)
    at jdk.nashorn.internal.scripts.Script$Recompilation$7$143854AAA$\^eval\_.L:2294(<eval>:2296)
    at jdk.nashorn.internal.scripts.Script$Recompilation$5$209I$\^eval\_.L:1#__webpack_require__(<eval>:20)
    at jdk.nashorn.internal.scripts.Script$Recompilation$6$142267AAA$\^eval\_.L:2248(<eval>:2253)
    at jdk.nashorn.internal.scripts.Script$Recompilation$5$209I$\^eval\_.L:1#__webpack_require__(<eval>:20)
    at jdk.nashorn.internal.scripts.Script$Recompilation$4$28A$\^eval\_.:scopeCall(<eval>)
    at jdk.nashorn.internal.scripts.Script$Recompilation$4$28A$\^eval\_.L:1(<eval>:63)
    at jdk.nashorn.internal.scripts.Script$3$\^eval\_.:program(<eval>:1)
    at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:637)
    at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:494)
    at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:449)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:406)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:402)
    at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:150)
    at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:249)
    at example.Hello$.delayedEndpoint$example$Hello$1(Hello.scala:15)
    at example.Hello$delayedInit$body.apply(Hello.scala:8)
    at scala.Function0.apply$mcV$sp(Function0.scala:34)
    at scala.Function0.apply$mcV$sp$(Function0.scala:34)
    at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
    at scala.App.$anonfun$main$1$adapted(App.scala:76)
    at scala.collection.immutable.List.foreach(List.scala:378)
    at scala.App.main(App.scala:76)
    at scala.App.main$(App.scala:74)
    at example.Hello$.main(Hello.scala:8)
    at example.Hello.main(Hello.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
Caused by: jdk.nashorn.internal.runtime.ParserException: /Users/suiluj/projects/design-portal-frontend/node_modules/reflect-metadata/Reflect.js:1125:37 Expected } but found eof
//# sourceMappingURL=Reflect.js.map})
                                     ^
    at jdk.nashorn.internal.parser.AbstractParser.error(AbstractParser.java:294)
    at jdk.nashorn.internal.parser.AbstractParser.error(AbstractParser.java:279)
    at jdk.nashorn.internal.parser.AbstractParser.expectDontAdvance(AbstractParser.java:350)
    at jdk.nashorn.internal.parser.AbstractParser.expect(AbstractParser.java:337)
    at jdk.nashorn.internal.parser.Parser.functionBody(Parser.java:2926)
    at jdk.nashorn.internal.parser.Parser.functionExpression(Parser.java:2663)
    at jdk.nashorn.internal.parser.Parser.memberExpression(Parser.java:2506)
    at jdk.nashorn.internal.parser.Parser.leftHandSideExpression(Parser.java:2372)
    at jdk.nashorn.internal.parser.Parser.unaryExpression(Parser.java:3147)
    at jdk.nashorn.internal.parser.Parser.expression(Parser.java:3282)
    at jdk.nashorn.internal.parser.Parser.primaryExpression(Parser.java:1992)
    at jdk.nashorn.internal.parser.Parser.memberExpression(Parser.java:2511)
    at jdk.nashorn.internal.parser.Parser.leftHandSideExpression(Parser.java:2372)
    at jdk.nashorn.internal.parser.Parser.unaryExpression(Parser.java:3147)
    at jdk.nashorn.internal.parser.Parser.expression(Parser.java:3282)
    at jdk.nashorn.internal.parser.Parser.expressionStatement(Parser.java:1150)
    at jdk.nashorn.internal.parser.Parser.statement(Parser.java:967)
    at jdk.nashorn.internal.parser.Parser.sourceElements(Parser.java:773)
    at jdk.nashorn.internal.parser.Parser.program(Parser.java:709)
    at jdk.nashorn.internal.parser.Parser.parse(Parser.java:283)
    at jdk.nashorn.internal.parser.Parser.parse(Parser.java:249)
    at jdk.nashorn.internal.runtime.Context.compile(Context.java:1284)
    at jdk.nashorn.internal.runtime.Context.compileScript(Context.java:1251)
    at jdk.nashorn.internal.runtime.Context.compileScript(Context.java:627)
    at jdk.nashorn.api.scripting.NashornScriptEngine.compileImpl(NashornScriptEngine.java:535)
    at jdk.nashorn.api.scripting.NashornScriptEngine.compileImpl(NashornScriptEngine.java:524)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:402)
    at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:155)
    at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
    at com.coveo.nashorn_modules.Module.compileJavaScriptModule(Module.java:332)
    at com.coveo.nashorn_modules.Module.compileModuleAndPutInCache(Module.java:288)
    at com.coveo.nashorn_modules.Module.loadModuleAsFile(Module.java:202)
    at com.coveo.nashorn_modules.Module.loadModuleThroughPackageJson(Module.java:247)
    at com.coveo.nashorn_modules.Module.loadModuleAsFolder(Module.java:215)
    at com.coveo.nashorn_modules.Module.attemptToLoadFromThisFolder(Module.java:181)
    at com.coveo.nashorn_modules.Module.searchForModuleInNodeModules(Module.java:150)
    at com.coveo.nashorn_modules.Module.require(Module.java:122)
    at jdk.nashorn.internal.scripts.Script$Recompilation$7$143854AAA$\^eval\_.L:2294(<eval>:2296)
    at jdk.nashorn.internal.scripts.Script$Recompilation$5$209I$\^eval\_.L:1#__webpack_require__(<eval>:20)
    at jdk.nashorn.internal.scripts.Script$Recompilation$6$142267AAA$\^eval\_.L:2248(<eval>:2253)
    at jdk.nashorn.internal.scripts.Script$Recompilation$5$209I$\^eval\_.L:1#__webpack_require__(<eval>:20)
    at jdk.nashorn.internal.scripts.Script$Recompilation$4$28A$\^eval\_.:scopeCall(<eval>)
    at jdk.nashorn.internal.scripts.Script$Recompilation$4$28A$\^eval\_.L:1(<eval>:63)
    at jdk.nashorn.internal.scripts.Script$3$\^eval\_.:program(<eval>:1)
    at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:637)
    at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:494)
    at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:449)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:406)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:402)
    at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:150)
    at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:249)
    at example.Hello$.delayedEndpoint$example$Hello$1(Hello.scala:15)
    at example.Hello$delayedInit$body.apply(Hello.scala:8)
    at scala.Function0.apply$mcV$sp(Function0.scala:34)
    at scala.Function0.apply$mcV$sp$(Function0.scala:34)
    at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
    at scala.App.$anonfun$main$1$adapted(App.scala:76)
    at scala.collection.immutable.List.foreach(List.scala:378)
    at scala.App.main(App.scala:76)
    at scala.App.main$(App.scala:74)
    at example.Hello$.main(Hello.scala:8)
    at example.Hello.main(Hello.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
[trace] Stack trace suppressed: run last compile:run for the full output.
java.lang.RuntimeException: Nonzero exit code: 1
    at scala.sys.package$.error(package.scala:27)
[trace] Stack trace suppressed: run last compile:run for the full output.
[error] (compile:run) Nonzero exit code: 1
[error] Total time: 8 s, completed Sep 19, 2017 12:11:01 AM
malaporte commented 7 years ago

Indeed, it seems I haven't thought of making a release afterwards. I just published 1.0.9 to Maven Central.