How to call a top level function in a script when there are multiple versions

Tim Fox timvolpe at gmail.com
Thu Dec 5 15:01:00 PST 2013


I have a ScriptEngine instance which I use many times to execute scripts 
in different scopes:

I.e. repeat the following several times with different scripts

ScriptContext ctx = new SimpleScriptContext();
Bindings bindings = engine.createBindings();
ctx.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
engine.eval(someScript, ctx);

Some time later I want to invoke a top level function 'vertxStop' in one 
of those scripts.

Each script has their own version of function vertxStop as they all have 
their own scope.

According to the docs I can invoke a top level function by casting the 
engine to Invocable:

Invocable inv = (Invocable)engine;
inv.invokeFunction("vertxStop");

But this doesn't let me choose which of the many vertxStop functions I 
want to execute.

I looked for a version of invokeFunction which takes a ScriptContext 
too, but couldn't find one.

Does anyone know how I can invoke a top level function from a specific 
scope using the javax.script API?

Cheers




More information about the nashorn-dev mailing list