This is an archived snapshot of W3C's public bugzilla bug tracker, decommissioned in April 2019. Please see the home page for more details.

Bug 26383 - Structure clones: Should cloned key/values be mutable after cloning?
Summary: Structure clones: Should cloned key/values be mutable after cloning?
Status: RESOLVED WORKSFORME
Alias: None
Product: WHATWG
Classification: Unclassified
Component: HTML (show other bugs)
Version: unspecified
Hardware: Other other
: P3 normal
Target Milestone: Unsorted
Assignee: Ian 'Hixie' Hickson
QA Contact: contributor
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-07-18 19:21 UTC by Tom Schuster
Modified: 2016-03-16 18:36 UTC (History)
5 users (show)

See Also:


Attachments

Description Tom Schuster 2014-07-18 19:21:25 UTC
The structured cloning algorithm isn't totally clear on what should happen when an element is modified after the algorithm in e.g. 6.3.1 invoked a getter.

I think the best solution would be to iterate over all key/values first and then invoking the structured cloning algorithm on that list.

Consider this example:

map = new Map()
map.set("b," {get b() { map.remove("a"); });
map.set("a", "b");
Comment 1 Jason Orendorff 2014-07-18 19:38:39 UTC
Just to clarify here:

1. The structured cloning algorithm may call getters and proxy traps and who knows what else, so the order of operations it specifies is observable.

2. The List and Record types in the ES spec are mutable, and in particular operations on a Map can modify its internal [[MapData]] List and assign to the [[key]] and [[value]] fields of the Records in that List.

Is the intended interaction of language like "For each [...] that is an element of source, run the following substeps" with mutation explained somewhere? (Alternatively, is it supposed to be obvious? In that case I apologize for being such a dunce)

Of course objects are mutable too so step 8 also seems vague.
Comment 2 Jason Orendorff 2014-07-18 19:44:41 UTC
How it works in Firefox:

    -   for Objects, we make a copy of the list of own enumerable properties
        at the time step 8 starts executing

        -   then, just before step 8.2, we do a [[Has]] check, and if the
            property is gone, we skip 8.2-8.4 for that property.

That's already implemented.

Here's what we're implementing for Map and Set:

    -   for Map, take a snapshot of the keys and values in the [[MapData]]
        at the time step 6 starts executing. Then we never consult the Map
        object or its internal data again. In step 6.3 we write exactly the
        keys and values in the snapshot, regardless of what has been done
        to the Map.

    -   same goes for Set: shallow copy on entry to step 7.
Comment 3 Ian 'Hixie' Hickson 2014-07-19 16:45:35 UTC
Shouldn't this just be specified in the JS spec?
Comment 4 Anne 2015-09-01 09:23:31 UTC
Adam, Jason, Tom, can I convince any of you to make a PR against https://github.com/whatwg/html?

Map/Set have already been fixed by Adam. See https://github.com/whatwg/html/commit/367707af5cd76864be9224e4b5a769fbf44fed96.
Comment 5 Adam Klein 2015-09-01 15:57:37 UTC
(In reply to Anne from comment #4)
> Adam, Jason, Tom, can I convince any of you to make a PR against
> https://github.com/whatwg/html?
> 
> Map/Set have already been fixed by Adam. See
> https://github.com/whatwg/html/commit/
> 367707af5cd76864be9224e4b5a769fbf44fed96.

Reading Jason's comment above I realize that my fix didn't go far enough (or with enough specificity) so someone should fix that too (basically I forgot that the spec modifies the records in place).
Comment 6 Adam Klein 2015-09-01 19:55:33 UTC
(In reply to Adam Klein from comment #5)
> (In reply to Anne from comment #4)
> > Adam, Jason, Tom, can I convince any of you to make a PR against
> > https://github.com/whatwg/html?
> > 
> > Map/Set have already been fixed by Adam. See
> > https://github.com/whatwg/html/commit/
> > 367707af5cd76864be9224e4b5a769fbf44fed96.
> 
> Reading Jason's comment above I realize that my fix didn't go far enough (or
> with enough specificity) so someone should fix that too (basically I forgot
> that the spec modifies the records in place).

Handled this problem in https://github.com/whatwg/html/pull/82 (though I can't sign up to fixing the rest of this today)
Comment 7 Anne 2016-03-16 18:36:09 UTC
I think this should be all clear now.