Using === across different contexts

Sundararajan Athijegannathan sundararajan.athijegannathan at oracle.com
Tue Dec 22 03:48:13 UTC 2015


Unless we create mirrors as weak refs internally (i.e., maintain 1:1 
with underlying foreign object reference), there is no easy solution. 
And maintaining such weak refs is unnecessarily complex. "Foreign 
object" call/access is mean to be just a "lightweight wrapper" based 
access. That said, you can do the === on the foreign context itself. You 
can call ScriptObjectMirror's eval to evaluate === test in that foreign 
context. That would get right object identity.

-Sundar

On 12/22/2015 4:26 AM, Vivin Suresh Paliath wrote:
> One more thing I noticed is that apparently a new ScriptObjectMirror
> instance is probably being created each time x is dereferenced, so "e.x ===
> e.x" also returns "false".
>
> On Mon, Dec 21, 2015 at 3:49 PM, Vivin Suresh Paliath <
> vivin.paliath at gmail.com> wrote:
>
>> 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