whatwg / webidl

Web IDL Standard
https://webidl.spec.whatwg.org/
Other
405 stars 162 forks source link

FrozenArray: Is T mutable? #348

Open henbos opened 7 years ago

henbos commented 7 years ago

A FrozenArray\<T> is said to be:

A frozen array type is a parameterized type whose values are references to objects that hold a fixed length array of unmodifiable values. The values in the array are of type T.

Since FrozenArray\<T> values are references, they are unlike sequence types, which are lists of values that are passed by value.

http://heycam.github.io/webidl/#idl-frozen-array

A fixed length value of unmodifiable values sounds like the values themselves are immutable. But the values are references, and the conversion from sequence does not freeze the elements of the array, only the array object:

  1. Let array be the result of converting the sequence of values of type T to an ECMAScript value.
  2. Perform SetIntegrityLevel(array, "frozen").
  3. Return array.

http://heycam.github.io/webidl/#es-frozen-array

It is clear that frozenArray.push(foo) and frozenArray[0] = 42 should have no effect, but what about frozenArrayOfObjects[0].foo = "bar"?

We should either:

henbos commented 7 years ago

(I suppose frozenArray[0] = 42 was a bad example and wouldn't work because then T is not an Object type, but frozenArray[0] = fooObject wouldn't work either.)

annevk commented 7 years ago

Being able to freeze the arguments would help with https://github.com/whatwg/notifications/issues/74 although it's all a little hacky. I'm still not a big fan of the FrozenArray pattern.

henbos commented 7 years ago

Another confusion: If the values of FrozenArray\<T> have values that are references ("unlike sequence types, which are lists of values that are passed by value"), does FrozenArray\<DictionaryType> make sense? Can dictionaries be passed by reference?

I'm trying to implement a spec that has a FrozenArray\<DictionaryType> attribute: https://w3c.github.io/webrtc-pc/#dom-rtccertificate-fingerprints

henbos commented 7 years ago

"Dictionaries are always passed by value. ... any dictionary returned from a platform object will be a copy and modifications made to it will not be visible to the platform object." https://www.w3.org/TR/WebIDL-1/#idl-dictionaries

Well, FrozenArray\<DictionaryType> is still handy to have (and used in several places in the webrtc spec), the implementation just has to make sure that the elements are copied.

bzbarsky commented 7 years ago

If the values of FrozenArray have values that are references

The quoted bit is saying the FrozenArray<T> itself is a reference, always, unlike sequence<T> which is a pass-by-value thing.

When it says "unmodifiable values" what it means is IDL values, in the sense of "things that have an IDL type". These could be references or value types, depending on T. This might be worth making clearer somehow...

the implementation just has to make sure that the elements are copied

Yes, exactly, just like it would for FrozenArray<long>.