That's all just standard JSR223 stuff - all Nashorn agnostic:
//Pass in contextScriptEngineManager mgr = new
ScriptEngineManager();ScriptEngine engine =
mgr.getEngineByName("JavaScript");Map<String, String> cx = new
HashMap<>();engine.put("console", new
MyConsoleObject());engine.eval("console.log('Nashorn')"); //Compile a
scriptCompilable compiler = (Compilable) engine; //Nashorn implements
Compilable CompiledScript script = compiler.compile("function add(a, b) {
return a+b }");...Object result = script.eval();
> Date: Thu, 24 Oct 2013 19:49:08 -0700
> Subject: poking objects into context?
> From: [email protected]
> To: [email protected]
>
> I had another couple of basic questions.
>
> 1) If my script is "pure" javascript provided by someone else, and I don't
> want to interfere with the script by pre-pending some java objects into it,
> what's the prescribed way for getting said objects into the execution
> context. I'm thinking about substitutes some commonly used browser objects
> like console in the first case, but also potentially objects which enable
> communication back into the wider environment I would be using the scripts
> in. Of course, I can declare references to said objects in the script, but
> external insertion would I think be more elegant.
>
> 2) If I have a lot of instances of the same script running in different
> places in my java environment, is there a way to (a) get the script from
> one ScriptEngine and place the compiled form into another (thus avoiding
> the overhead of a second compilation) or (b) create a secondary instance of
> the same (I assume) ScriptObject which does not share state with the
> original instance.
>
> thanks in advance
>
> M