0

So, my problem is the following:

Lets say, i have a base of js modules which need to be preloaded. I have now several scripts which need to be able to be run without interfereing into others.

The scripts look like this:

function A(){
}

function B(){
}

What i want to do is call Function A from Script 1 and Function A from Script 2. But only the last Script is executed correctly since it is the same ScriptEngine I am calling.

I tried an attempt like this:

   public static ScriptEngine eval(BufferedReader file) throws ScriptException {
    SimpleScriptContext context = new SimpleScriptContext();
    context.getBindings(ScriptContext.ENGINE_SCOPE).putAll(engine.getBindings(ScriptContext.ENGINE_SCOPE));
    ScriptEngine dengine = new NashornScriptEngineFactory().getScriptEngine("-strict");
    dengine.eval(file,context);
    ClassLoader cl = MyClass.getInstance().getClass().getClassLoader();
    Thread.currentThread().setContextClassLoader(cl);
    return dengine;
}

to create multiple ScriptEngines. But that failed. So what is the right attempt, to "clone" already loaded Scripts without rerunning them?

Sebastian G.
  • 616
  • 1
  • 7
  • 25
  • Maybe this thread can help you: [Reusing compiled JS code keeping speed and thread-safety](http://mail.openjdk.java.net/pipermail/nashorn-dev/2014-October/003758.html) – Aris2World Aug 09 '17 at 06:33
  • Give a look also to this question: https://stackoverflow.com/questions/11116120/clone-entire-javascript-scriptengine?rq=1 – Aris2World Aug 09 '17 at 06:37

0 Answers0