Differences between Rhino and Nashorn when using Bindings.remove(x)

A. Sundararajan sundararajan.athijegannathan at oracle.com
Tue May 13 04:11:36 UTC 2014


Yes, nashorn is different from rhino. Please check out 
https://wiki.openjdk.java.net/display/Nashorn/Nashorn+jsr223+engine+notes

Bindings on top of Nashorn's Global tries to follow language rules 
rather than treating it as "Map". Per language rules, you can't delete a 
global variable - in strict mode, you'd get a TypeError for attempting 
to do so.  Assigning undefined is better way to go. That way "surprise" 
of missing variable is minimized.

Also, user provided Bindings only "variables" (i.e., not the ones 
created by "foo = 33" in evaluated code) are treated as "read-only". 
i.e., not found in Nashorn  global scope -- so check out if found in 
Bindings of ScriptContext (via __noSuchProperty__)

Hope this helps,
-Sundar


On Monday 12 May 2014 10:14 AM, Eric Woudenberg wrote:
> It appears that in Rhino, doing "myvar = undefined" is the same as 
> doing EngineBindings.remove(myvar), however in Nashorn the 
> EngineBindings.remove() operation behaves quite differently.
>
> I've attached a Jython program that I've run under jdk7 and jdk8 that 
> point up the differences (in /RED italics/):
>
>>      JAVA 7 (RHINO) JAVA 8 (NASHORN)
>>
> Case 1: No longer able to store a value after Bindings.remove().
>> +----Expr "myvar = 33" +----Expr "myvar = 33"
>>
>>   myvar = 33                              myvar = 33
>>   Bindings.remove(myvar)                  Bindings.remove(myvar)
>>     Engine.eval('myvar'): FAILED Engine.eval('myvar'): FAILED
>>     typeof(myvar)=undefined typeof(myvar)=undefined
>>     myvar in bindings: False                myvar in bindings: False
>>   myvar = 33                              myvar = 33
>> /Engine.eval('myvar'): 33                Engine.eval('myvar'): FAILED//
>> //    typeof(myvar)=number typeof(myvar)=undefined//
>> //    myvar in bindings: True                 myvar in bindings: False/
>>
> Case 2: Bindings.remove() does not remove the value.
>> +----Expr "var myvar=33" +----Expr "var myvar=33"
>>
>>   var myvar=33                            var myvar=33
>> Bindings.remove(myvar) Bindings.remove(myvar)
>> /Engine.eval('myvar'): FAILED Engine.eval('myvar'): 33//
>> //    typeof(myvar)=undefined typeof(myvar)=number//
>> //    myvar in bindings: False                myvar in bindings: True/
>>   var myvar=33                            var myvar=33
>>     Engine.eval('myvar'): 33 Engine.eval('myvar'): 33
>>     typeof(myvar)=number                    typeof(myvar)=number
>>     myvar in bindings: True                 myvar in bindings: True
>
> We previously used Bindings.remove('myvar') and could not understand 
> Nashorn's behavior. We've switched to using "myvar = undefined" and 
> everything seems fine now.
>
> Nonetheless, can someone explain why doing Bindings.remove("var") 
> shows this behavior?
>
> Thank you,
> Eric Woudenberg
>



More information about the nashorn-dev mailing list