[[Extensible]]and Proxies (Was: Proxy.isProxy )

David Bruant david.bruant at labri.fr
Thu Jul 14 10:38:20 PDT 2011

Le 14/07/2011 18:57, Mark S. Miller a écrit :
> On Thu, Jul 14, 2011 at 6:01 AM, David Bruant <david.bruant at labri.fr
> <mailto:david.bruant at labri.fr>> wrote:
>         If that's the question:
>         -----
>         var a = Array(1,2,3,4); // a.length===4
>         Object.preventExtensions(a);
>         console.log(a[3]); // 4, of course
>         a.length = 3; // should delete a[3] under the hood.
>         console.log(a[3]);
>         -----
>         Logs "undefined" with a conformant ECMAScript Array.
>         This cannot be emulated with current proxies. Current choices are:
>         * Throw a TypeError on Object.preventExtensions
>         * Accept to be transformed into a normal object fully
>         respecting ES5.1 8.12 algorithms and log "4"
>         * Turn the "length" property into a getter/setter pair which
>         violates ES5.1 which ask the length property to be a
>         non-configurable data property.
>     So having the length be an accessor means that such an object is
>     not a perfectly transparent emulation of an array. What is a use
>     case this non-transparency actually matters.
I guess I can turn the question into: What motivated ES5.1 to
define the length property as a data property and keep the step 3 of rather than a defining a getter/setter pair and remove step 3?

Let assume arrays length property are non-configurable getter/setter for
a minute.
If you hand an array to some untruted code, this code can extract the
length setter and set length as it wishes. You have no way to change the
setter yourself as the property is non-configurable. So, in pure ES5.1 +
(assumption of length being a non-configurable getter/setter pair), as
soon as you hand an array, you'd grant the right to change the length at
any time to the untrusted code.
(this may be the answer to my question above)
Still under the same assumption, in the Harmony era (with proxies), a
workaround could be to wrap your array in a proxy preventing access to
the length property descriptor. But it sounds much more work than "just"
exposing a data property.

> In all the cases I can think of, where we want precisely the behavior
> of an array, why not just use an array?
Of course an array should be used. I think that the current question is
rather "can proxies as currently defined can fully emulate arrays?".
Being able to fully emulate arrays sounds like an "easy" use case.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110714/989f9614/attachment.html>

More information about the es-discuss mailing list