annevk/html-cross-origin-objects

map might not work well

domenic opened this issue · 4 comments

Since the key is todo, it's unclear. But it's important to realize weak maps are keyed by reference, not value. So if the key is a tuple, weak maps will probably not work.

In general, weak maps can often be replaced by adding properties directly on the object. So e.g. instead of wm.set(a, b) and wm.get(a), you can do a._privateVariable = b and a._privateVariable. Maybe a similar approach would be useful here.

Based on discussion on IRC the new idea is to define this as a "map" that supports "has", "get", and "set" waves hands. Then we say: "The UA should allow the property descriptors their accessor functions stored in [[crossOriginPropertyDescriptorMap]] to be garbage collected if no references to them exist and it is not possible for them to be accessed."

And then, to be extra generous, we add some examples. One where you cannot GC (because someone got a reference) and one where you likely can (document.domain goodness).

I may be way off base but I don't think the IRC solution is good. At least currently, there is too much hand waving. It could work, but you would have to precisely define the semantics of has, get, and set operations on your new "Map" specification type in a similar way to operations on Records and Lists. Note that you can't hand-wave away how to compare ECMAScript values as keys - there are many possible ways to do it depending on the types of values you're dealing with.

I don't think there need be any normative mention of memory collection behaviors. Implementations are free to do non-observable things, and if outside users can't observe that an entry is collected, implementations are of course free to do so. Given that users can't directly look up keys and can't enumerate the map contents, it can be implemented with good GC semantics with or without a normative statement telling me to do so.

For this reason I'd say it's ok to use a normal map and have a note that says that implementations should choose to remove items from the map when they are no longer in use elsewhere.

@domenic opinions on this?

I don't think it's a big deal. If this were the ES spec, sure, there's no precedent for a "map" data type, and instead they cobble together an awkward List of Records. But in HTML we use such maps all the time.

I agree that GC behavior can be confined to a note. I might still call it a "weak map" in the spec, like we do "rejected promises weak set"? Or maybe not because of the key being a record.