ScriptContext.ENGINE_SCOPE is set to be wrapper over Nashorn's Global object. So, from current ScriptContext instance, ENGINE_SCOPE bindings is fetched and set to be current Nashorn Global instance - (Context.setGlobal call). And reset global after eval as you mentioned.

ScriptContext.GLOBAL_SCOPE can be any Bindings object. That is used if a global variable is referred in script - but not defined/assigned in script. Nashorn Global instance's __noSuchProperty__ hook method will look for mapping in GLOBAL_SCOPE, if available and use the same.

Simple sample below (lazy to write Java for a small example ;-) )

var m = new javax.script.ScriptEngineManager();
var e = m.getEngineByName("nashorn");

var globals = e.getContext().getBindings(javax.script.ScriptContext.GLOBAL_SCOPE);
globals.put("x", "hello");
e.eval("print(x)");

PS. The word 'global' for Nashorn should not be confused with ENGINE vs GLOBAL scope terms jsr223 API. jsr223 ENGINE_SCOPE is associated with nashorn's Global scope object.

Hope this helps,
-Sundar

On Thursday 18 July 2013 03:42 PM, Benjamin Sieffert wrote:
Hi, I'm still working on this with Tobias.

Our current understanding of what happens is as follows:
When invocation or evaluation is requested on a NashornScriptEngine, it
will:
1. Fetch its current context (protected AbstractScriptEngine member field)
scope: ScriptContext.ENGINE_SCOPE
2. Set this context as the global nashorn context in the current thread
(internal.runtime.Context.setGlobal() -> ThreadLocal<ScriptObject>
currentGlobal)
3. Do the evaluation
4. Reset the global nashorn context in the current thread (most likely to
null, since ThreadLocal<ScriptObject> currentGlobal gets initialized to
nothing but an empty ThreadLocal<>())

Is this rougly correct?
If yes, question is, why does the context that is explicitly engine-scoped
become the global scope during execution?
(On a sidenote, we tried to do some things with using GLOBAL_SCOPE bindings
with Nashorn in the past and it didn't work. Seems like you aren't
using/supporting this at all?)

ps: Here's the profiler output missing in Tobias' Mail: evernote
dotcom/shard/s4/sh/1eaec11d-9732-43c3-b33d-c7e4d6e07d5f/264ff0ba4e4721525a411b5cbcb9fe6a

Best regards,
Benjamin

Reply via email to