JavaScript Array slot/member == undefined instead of null?

Sundararajan Athijegannathan sundararajan.athijegannathan at oracle.com
Mon Dec 5 05:55:50 UTC 2016


Scripting language independent javax.script APIs like ScriptEngine.eval,
Bindings.get, Invocable.invokeFunction etc. translate undefined to null 
(closest to what you'd expect in java world). But, nashorn specific APIs
like jdk.nashorn.api.scripting.JSObject.getMember/getSlot etc. return
undefined as undefined - without null translation. [as you're calling
nashorn engine specific API].


-Sundar

On 12/1/2016 1:13 AM, Art Fiedler wrote:
> @Sundararajan,
>
> I was previously using .getSlot(index) when extracting items from a
> ScriptObjectMirror when that mirror was an array until I ran into
> a undefined element and that is not equal to null as I had for my
> condition... so I swapped .getSlot(i) with .get(Integer.toString(i))
>
> Are getMember(), get(), and getSlot() suppose to all return the same null
> or undefined value? Or is it expected to have
> getMember() and getSlot() to return Undefined object and get() to return
> null?
>
> As a side note... If this is the intended behavior this might be a better
> way to get the Undefined object instead of making a
> call out of nashorn to a public static method.
>
> Here is an example... (jdk8 1.8.0_102)
>
> Thanks,
> Arthur Fiedler
>
>
>     public static void main(String[] args) throws Exception {
>         NashornScriptEngineFactory factory = new
> NashornScriptEngineFactory();
>         ScriptEngine scriptEngine = factory.getScriptEngine();
>         scriptEngine.eval("globalArray = ['line1',undefined,null];");
>         ScriptObjectMirror array =
> (ScriptObjectMirror)scriptEngine.get("globalArray");
>         System.out.println("size(): " + array.size());
>         for(int i = 0; i < array.size(); i++) {
>             System.out.println("Index: " + i);
>             String iStr = String.valueOf(i);
>             System.out.println("    GetMember('"+iStr+"'): " +
> array.getMember(iStr));
>             System.out.println("    Get('"+iStr+"'): " + array.get(iStr));
>             System.out.println("    GetSlot("+iStr+"): " +
> array.getSlot(i));
>         }
>     }
>     /*
>     size(): 3
>     Index: 0
>         GetMember('0'): line1
>         Get('0'): line1
>         GetSlot(0): line1
>     Index: 1
>         GetMember('1'): undefined
>         Get('1'): null
>         GetSlot(1): undefined
>     Index: 2
>         GetMember('2'): null
>         Get('2'): null
>         GetSlot(2): null
>     */



More information about the nashorn-dev mailing list