Hi Rod,

Nashorn allows only script objects (i.e., objects created by a JS constructor or JS object literal expression) as scope expression for "with" statement. Arbitrary objects like "context" (which is an instance of javax.script.ScriptContext) can not be used as 'scope' expression for 'with'.

In jdk9, support has been added to support script objects mirror other script engines or other globals (which are instances of ScriptObjectMirror).

What is ScriptObjectMirror?

it is java API for an underlying JS object (which is an instance of ScriptObject).

For eg. you can a ScriptObjectMirror like this:

ScriptObjectMirror sobj = (ScriptObjectMirror) engine.eval("({ foo: 233 })");

More info here: https://wiki.openjdk.java.net/display/Nashorn/Nashorn+jsr223+engine+notes

Hope this helps,
-Sundar

On Friday 11 July 2014 12:49 PM, Rod Nim wrote:
Dear Nashorn devs;

I'm wanting to invoke a JS function that looks like this:

function myFunc(context) {
    with(context) {
       return a + b;
    }
}

Where a and b are values my Java "context" object supplies.

I've tried passing in "context" from Java (via Invocable) as a Map; and am 
getting:

    javax.script.ScriptException: TypeError: Cannot apply "with" to non script object 
in <eval>

Fair enough. So I tried my own instance of JSObject but Nasorn isn't happy with 
that either, same exception.

So myArg needs to be an instance of ScriptObjectMirror. But ScriptObjectMirror 
is final!

Any way to get me out of his corner?

Thank you in advance for your help. I know you're a very busy team and answering 
"user" questions like this is not a core priority, in fact I almost feel 
embarrassed to ask.

Thanks again.

Rod                                     

Reply via email to