-
Notifications
You must be signed in to change notification settings - Fork 7
Description
I'm trying to run some existing code on Nashorn instead of Rhino by using mod-lang-nashorn.
The code uses CommonJS to "require" some external libraries, such as URI.js
With the Rhino module everything works, but with the Nashorn module I get the following error:
javax.script.ScriptException: TypeError: Cannot read property "punycode" from undefined in <eval> at line number 27
FYI: punycode is another library, that is being "required" by the URI.js library.
First I thought it was an issue with the URI.js library and created a ticket for it at the URI.js github site.
But they explained (and convinced) me that the issues are caused by some bugs in the Vert.x Nashorn module and not in the URI.js library.
The error above can easily be solved by changing line 60 in VertxRequire from this:
"})({ id: __jscriptcontext.getModule().getId(),\n" +
to this:
"})(this, { id: __jscriptcontext.getModule().getId(),\n" +
This simple change fixes the error mentioned above.
Unfortunately there is one other bug, namely that the modules "required" by a CommonJS module are loaded from a wrong location.
E.g. URI.js loads several other libraries, like this:
module.exports = factory(require('./punycode'), require('./IPv6'), require('./SecondLevelDomains'));
When I try to load URI.js (and therefore also those other libraries) with following code:
var URI = require("js/libs/uri/URI");
I get the following error:
Cannot find script: ./punycode.js
In other words, the Nashorn module is trying to load those modules from the wrong location.
So to summarize, there are 2 issues with the CommonJS support in mod-lang-nashorn:
- the Vertx module loader should explicitly call the wrapper function with the global object as "this" object, see fix above
- modules/libraries included in commonjs libraries are loaded from the wrong location.
sANTo