*.empty Idea

Domenic Denicola d at domenic.me
Thu Apr 30 19:07:27 UTC 2015


It's fine to propose something to make maps immutable. Just don't call it Object.freeze.






On Thu, Apr 30, 2015 at 11:56 AM -0700, "C. Scott Ananian" <ecmascript at cscott.net<mailto:ecmascript at cscott.net>> wrote:

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<mailto:ecmascript at cscott.net>> wrote:
On Thu, Apr 30, 2015 at 2:22 PM, Mark S. Miller <erights at google.com<mailto: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/2de8ae90/attachment-0001.html>


More information about the es-discuss mailing list