Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FrozenArray: Is T mutable? #348

Open
henbos opened this issue Apr 24, 2017 · 5 comments
Open

FrozenArray: Is T mutable? #348

henbos opened this issue Apr 24, 2017 · 5 comments

Comments

@henbos
Copy link

henbos commented Apr 24, 2017

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:

  • Clarify the FrozenArray description to say that the objects are mutable.
  • Update the conversion algorithm to include SetIntegrityLevel(array[i], "frozen").
@henbos
Copy link
Author

henbos commented Apr 24, 2017

(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
Copy link
Member

annevk commented Apr 24, 2017

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

@henbos
Copy link
Author

henbos commented Apr 24, 2017

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
Copy link
Author

henbos commented Apr 24, 2017

"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
Copy link
Collaborator

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>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants