How to share a common bindings for different ScriptContext with Java ScriptEngine API? -


i playing nashorn , scriptengine api of java 8. have unique instance of scriptengine execute several different scripts. remove functions (e.g. print) scripts execute, specify specific binding binding each script.

my problem not know if possible make kind of hierarchy scriptcontext.

here exemple of :

public static void main(string[] args) throws exception {     scriptenginemanager manager = new scriptenginemanager();     scriptengine engine = manager.getenginebyname("nashorn");     bindings bindings = engine.getbindings(scriptcontext.engine_scope);     bindings.remove("print");      // script run     string script1 = "print('hello world')";     string script2 = "print(person)";      // define different script context     scriptcontext newcontext = new simplescriptcontext();     bindings enginescope = newcontext.getbindings(scriptcontext.engine_scope);      // set variable different value in scope     enginescope.put("person", "toto");      // evaluate script in global context     // should throw message because print not declared in context     try {         engine.eval(script1);     } catch (scriptexception se) {         system.out.println(se.getmessage()); // out : referenceerror: "print" not defined in <eval> @ line number 1     }      // should throw message because print not declared in context     engine.eval(script2, newcontext); // out : toto } 

here, probeme when execute script2 print has been removed context.

is there clean way ?


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -