Using === across different contexts

Vivin Suresh Paliath vivin.paliath at gmail.com
Mon Dec 21 22:49:02 UTC 2015


I ran into an issue where === returns false even when both should be
pointing to the same object. I'm assuming this is because one of the
objects is wrapped by a ScriptObjectMirror, because it was defined in a
different context.

Here's some code that demonstrates this:

        ScriptEngine engine = new
NashornScriptEngineFactory().getScriptEngine(
            new String[] { "-strict" }
        );

        try {
            engine.eval("function Foo(src) { this.src = src }; var e = { x:
new Foo(\"what\") };");

            ScriptContext c = new SimpleScriptContext();
            c.setBindings(engine.createBindings(),
ScriptContext.ENGINE_SCOPE);

c.getBindings(ScriptContext.ENGINE_SCOPE).putAll(engine.getBindings(ScriptContext.ENGINE_SCOPE));

            System.out.println(engine.eval("var z = e.x; z === e.x;", c));
        } catch(Exception e) {
            throw new RuntimeException(e);
        }

This prints out "false". Is there a way around this? I am also explicitly
copying over all the bindings from the parent scope into the new scope so
that I have access to "e". Could this be the source of the problem, and if
so, is there a better way to achieve what I'm trying to do?

-- 
Ruin untold;
And thine own sadness,
Sing in the grass,
When eve has forgot, that no more hear common things that gleam and pass;
But seek alone to lip, sad Rose of love and ruin untold;
And thine own mother
Can know it as I know
More than another
What makes your own sadness,
Set in her eyes.

map{@n=split//;$j.=$n[0]x$n[1]}split/:/,"01:11:02".
":11:01:11:02:13:01:11:01:11:01:13:02:12:01:13:01".
":11:04:11:06:12:04:11:01:12:01:13:02:12:01:14:01".
":13:01:11:03:12:01:11:04:12:02:11:01:11:01:13:02".
":11:03:11:06:11:01:11:05:12:02:11:01:11:01:13:02".
":11:02:12:01:12:04:11:06:12:01:11:04:12:04:11:01".
":12:03:12:01:12:01:11:01:12:01:12:02:11:01:11:01".
":13:02:11:01:02:11:01:12:02";map{print chr unpack"
i",pack"B32",$_}$j=~m/.{8}/g


More information about the nashorn-dev mailing list