*.empty Idea

C. Scott Ananian ecmascript at cscott.net
Thu Apr 30 18:56:29 UTC 2015


Can you make an alternative proposal that still preserves the essential
property of Object.freeze on collections -- that is to say, preserves
object identity while preventing future writes?

Here's another strawman:
> Add "[[Immutable]]" to the arguments of OrdinaryCreateFromConstructor in
step 2 of 23.1.1.1 (Map) and 23.2.1.1 (Set)
> Add "If the [[Immutable]] internal slot of M is true, throw a TypeError
exception" between steps 3 and 4 of 23.1.3.1 (Map.prototype.clear),
23.1.3.3 (Map.prototype.delete), 23.1.3.9 (Map.prototype.set), 23.2.3.1
(Set.prototype.add), 23.2.3.2 (Set.prototype.clear), and 23.2.3.4
(Set.prototype.delete).
> Add `Map.prototype.makeReadOnly()` and `Set.prototype.makeReadOnly()`
methods with the definition:
>  1. Let M be the this value.
>  2. If Type(M) is not Object, throw a TypeError exception
>  3. If M does not have a [[Immutable]] internal slot, throw a TypeError
exception
>  4. Set the [[Immutable]] internal slot of M to true.

This is somewhat awkward and ad-hoc, but it won't break ES6 code.

I am concerned about this issue in part because as written is seems to be
impossible to prevent writes to Map.  If you subclass it, freeze it,
override set/delete/etc it is still possible to mutate the map using
`Map.set.call(m, 'a', 'b');`.  And there is no way for use code to get at
the [[MapData]] slot to protect it.
  --scott


On Thu, Apr 30, 2015 at 2:42 PM, C. Scott Ananian <ecmascript at cscott.net>
wrote:

> On Thu, Apr 30, 2015 at 2:22 PM, Mark S. Miller <erights at google.com>
> wrote:
>
>> It would also not be compatible with ES6 code. SES will be freezing Map,
>> Set, WeakMap, and WeakSet instances in order to tamper proof their API. I
>> expect many others will as well. Having this freeze then cause a
>> non-mutability in ES7 will break all such ES6 code. This is a non-starter
>> all around.
>>
>
> Couldn't SES use
> Object.seal/Object.preventExtensions/Object.defineProperty to perform
> tamper-proofing without flipping the "frozen" bit?
>  --scott
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150430/7a4b6ce9/attachment.html>


More information about the es-discuss mailing list