Share context/JS bindings

Axel Dörfler axeld at pinc-software.de
Tue Jun 14 07:09:31 UTC 2016


Hi there,

I'm trying to reduce the overhead of running JavaScript code. It looks 
like the best thing you can do is a) share a single engine, and b) reuse 
the script context.

I'm using a per-thread context like this:

     public static ScriptContext createJavaScriptContext() {
         ScriptContext context = new SimpleScriptContext();
         Bindings bindings = perThreadGlobalScope.get();
         if (bindings == null) {
             bindings = getJavaScriptEngine().createBindings();
             perThreadGlobalScope.set(bindings);
         } else
             bindings.clear();

         context.setBindings(bindings, ScriptContext.ENGINE_SCOPE);

         return context;
     }

Now I'm facing the problem that JavaScript variables are shared between 
different uses.
Ie. a:

     var found;
     print found;
     found = 1;

Would print "1" on the second run.

Is there a way to avoid this?

Bye,
    Axel.


More information about the nashorn-dev mailing list