-
Bug
-
Resolution: Fixed
-
P3
-
None
-
None
-
b82
-
generic
-
generic
This issue was reported by Per Bothner. The following is from his email:
===== email =====
The general case of (integrating) a user-provided ScriptContext or Bindings object is painful. However, we can do better when using a ScriptEngine's default ScriptContext and Bindings.
Specifically, I suggest:
When in the NashornScriptEngine constructor), do:
context.setBindings(new NashornBindings(globalObject), ScriptContext.ENGINE_SCOPE);
where:
/** A wrapper (proxy) for a ScriptObject viewed as a Map. */
class NashornBindings implements Bindings {
ScriptObject base;
public NashornBindings(ScriptObject base) { this.base = base; }
public Object get(Object key) { return base.get(key); }
... other methods implementing Bindings using base ...
}
Every program that uses the default ScriptContext and Bindings (which I hope most so, and which we should encourage) now works much better, because the ScriptEngine's ENGINE_SCOPE is now automatically synchronized with the globalObject: The ScriptEngine evaluator automatically sees the properties of the globalObject, and JavaScript using the same Global automatically sees the bindings done by the ScriptEngine.
===== email =====
The general case of (integrating) a user-provided ScriptContext or Bindings object is painful. However, we can do better when using a ScriptEngine's default ScriptContext and Bindings.
Specifically, I suggest:
When in the NashornScriptEngine constructor), do:
context.setBindings(new NashornBindings(globalObject), ScriptContext.ENGINE_SCOPE);
where:
/** A wrapper (proxy) for a ScriptObject viewed as a Map. */
class NashornBindings implements Bindings {
ScriptObject base;
public NashornBindings(ScriptObject base) { this.base = base; }
public Object get(Object key) { return base.get(key); }
... other methods implementing Bindings using base ...
}
Every program that uses the default ScriptContext and Bindings (which I hope most so, and which we should encourage) now works much better, because the ScriptEngine's ENGINE_SCOPE is now automatically synchronized with the globalObject: The ScriptEngine evaluator automatically sees the properties of the globalObject, and JavaScript using the same Global automatically sees the bindings done by the ScriptEngine.