Possible scope leak in Nashorn

Tim Fox timvolpe at gmail.com
Thu Dec 5 04:42:04 PST 2013


Hello Nashorn folks,

I've been playing around with Nashorn today, and in particular I've been 
trying to implement commonJS-like require() functionality, but having 
some problems. Most probably I am doing something stupid...

I'm sure you all know, with commonJS modules you can "require" them from 
your JS scripts, e.g.

var someObject = require("modulename");

Then in modulename.js

you do your stuff then export the object that ends up being returned 
from the require, e.g.

var someObject = {
   foo: "bar"
}

module.exports = someObject;

One key thing is that modules have their own scope, so any globals 
defined in modulename.js shouldn't be visible in the script that 
requires them, and vice versa.

While trying to implement this using Nashorn and running scripts in 
different scopes I've found it's possible for Nashorn to get confused 
about what scope it's using, and for scopes to leak from one to another.

I created this simple runnable example to demonstrate the issue.

I wonder if anyone can advise whether this is a bug, or perhaps I am 
using the API incorrectly. If the latter, could you advise on the proper 
way to use the API to implement this kind of functionality.

Many thanks




More information about the nashorn-dev mailing list