Nashorn, javax.script.filename, and load()

David P. Caldwell david at code.davidpcaldwell.com
Fri Jul 10 20:29:39 UTC 2015


According to the documentation for javax.script.ScriptEngine, the key
javax.script.ScriptEngine.FILENAME should point to the current file
being executed. And it does, when executing a local file.

But when executing a file from a URL using load(), it does not work. I
can reliably get the URL using some rigamarole (see below), but I'd
like a method that's portable across JDK 6-8 (or so).

Here's the output of the following snippet when executing locally:

Snippet:
Packages.java.lang.System.out.println("stack = " + new
Packages.java.lang.Throwable().getStackTrace()[0].getFileName());
var global = (function() { return this; })();
Packages.java.lang.System.out.println("javax.script.filename = " +
global[String(Packages.javax.script.ScriptEngine.FILENAME)]);

Output under JDK 8 locally:
stack = rhino/jrunscript/api.js
javax.script.filename = rhino/jrunscript/api.js

Output under JDK 8 executing over HTTP via -e load('url'):
stack = http://bitbucket.org/api/1.0/repositories/davidpcaldwell/jrunscript/raw/local/api.js?test=filename
javax.script.filename = <string>

And further inspection indicates that's a *string* "<string>"

Here's JDK 7 for comparison:

Output under JDK 7 locally:
stack = NativeConstructorAccessorImpl.java
javax.script.filename = rhino/jrunscript/api.js

Output under JDK 7 executing over HTTP via -e load('url'):
stack = NativeConstructorAccessorImpl.java
javax.script.filename =
http://bitbucket.org/api/1.0/repositories/davidpcaldwell/jrunscript/raw/local/api.js?test=filename

Thoughts?

-- David P. Caldwell
http://www.davidpcaldwell.com/


More information about the nashorn-dev mailing list