Confusing ScriptObjectMirror size() vs mirror.get("length")

Art Fiedler artfiedler at gmail.com
Fri Dec 2 08:56:03 UTC 2016


Thanks for making it more confusing! ;)

The reason I used array.get(iStr) instead of array.getSlot(I) just happened
to be because of my previous post about array.getSlot() returning the
undefined object. I should however have used getSlot in this example to
make it more clear that I knew I was dealing specifically with an array
without properties. You do have a point though and I have a fantastic
solution!! Add to ScriptObjectMirror something like...

    public int getSlotLength() {
        return ScriptUtils.convert(this.get("length"), Integer.class);
    }

This should help reduce any potential bugs if someone happened to use
mirror.size() to loop an accidently on a fragmented array not realizing
.get("length") should be used. Having that method should bring more
visibility for someone getting slots.

Thanks,
Arthur Fiedler

> I think this is not a bug. "length" is not the same as number of
> properties in an object. For example, if you try this:
>
> var a = [3, 5, 66];
> a.foo = "nar";
> for (var i = 0; i < a.length; i++)
>     print(a[i]);
>
> "length" is 3 [and not 4].  keySet size is the number of "keys"
> (properties). You've to get "length" property of the array.
>
> -Sundar
>


More information about the nashorn-dev mailing list