Type of property names, as seen by proxy traps

David Bruant david.bruant at labri.fr
Thu Jul 7 09:24:06 PDT 2011


Le 07/07/2011 18:05, Andreas Rossberg a écrit :
> On 7 July 2011 17:58, Brendan Eich <brendan at mozilla.com> wrote:
>> On Jul 7, 2011, at 8:32 AM, Andreas Rossberg wrote:
>>
>>> On 7 July 2011 16:12, David Bruant <david.bruant at labri.fr> wrote:
>>>>>> Derived traps as showed are written in JS for expository purposes.
>>>>>> Engines
>>>>>> will be free to optimize as they wish internally as long as the observed
>>>>>> behavior is the same.
>>>>> True, but optimizing that actually is more tricky than you might
>>>>> think, since in general it would change the semantics if an engine
>>>>> decided to call toString only once. It has to make sure that none of
>>>>> the names are objects, or at least none of their toString methods was
>>>>> modified and they are all free of side effects.
>>>> Interesting.
>>>> However, I'm not sure side-effects are a problem.
>>>> -----
>>>> var o = {a:1, toString:function(){o.b = 12; return 'a'; }};
>>>> console.log(o[o], o.b); // 1, 12 on Firefox 5
>>>> -----
>>>> Here, o[o] triggers a side effect and that sound like the normal behavior.
>>> I'm not sure I understand what your example is intended to show. But
>>> consider this:
>>>
>>> var i = 0
>>> var o = {toString: function() { ++i; return "a" }
>>> var p = Proxy.create({getOwnPropertyNames: function() { return [o] }, ...})
>>> var k = Object.keys(p)
>>> // What's the value of i now?
>> Fresh tracemonkey tip js shell:
>>
>> js> var i = 0
>> js> var o = {toString: function() { ++i; return "a" }}
>> js> var p = Proxy.create({getOwnPropertyNames: function() { return [o] },
>>                      getOwnPropertyDescriptor: function() { return {value:42} }})
>> js> var k = Object.keys(p)
>> js> i
>> 1
>>
>> Where would there be a double-conversion?
> Well, with the canonical fix to the spec we discussed further up the
> thread (adding a conversion to string in the default trap for `keys')
> there would (have to) be. So my concern was that that is perhaps not
> the best fix, despite its simplicity.
No, with the current keys default trap (calling
this.getOwnPropertyNames()) there is no double conversion. Only one at
the exit of the keys trap. There would be 2 conversions if the "keys"
trap had the proxy argument (based on
http://wiki.ecmascript.org/doku.php?id=strawman:handler_access_to_proxy)
and if internally, the default keys trap was calling
"Object.getOwnPropertyNames(proxy)" (which would call the trap and do
type coercion).

But the current implementation and a type coercion only when going out
of traps would do double-conversion.

David


More information about the es-discuss mailing list