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

CommonJS support broken #5

Open
santo74 opened this issue Oct 2, 2014 · 0 comments
Open

CommonJS support broken #5

santo74 opened this issue Oct 2, 2014 · 0 comments

Comments

@santo74
Copy link

santo74 commented Oct 2, 2014

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:

  1. the Vertx module loader should explicitly call the wrapper function with the global object as "this" object, see fix above
  2. modules/libraries included in commonjs libraries are loaded from the wrong location.

sANTo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant