tc39 / proposal-weakrefs

WeakRefs
https://tc39.github.io/proposal-weakrefs/
409 stars 41 forks source link

Editorial: Fix another `IterableWeakMap` leak #216

Open ExE-Boss opened 3 years ago

ExE-Boss commented 3 years ago

Fixes: https://github.com/tc39/proposal-weakrefs/issues/213

This would cause the #refSet to possibly contain dead references and lead to a key/value pair being yielded multiple times.

const m = new IterableWeakMap();
const a = { [Symbol.toStringTag]: "A" };
const b = { [Symbol.toStringTag]: "B" };
m.set(a, 1);
m.set(b, 2);
m.set(a, 3);

for (const { 0: key, 1: value } of m) {
    // This would previously log:
    // [object A] 3
    // [object B] 2
    // [object A] 3
    console.log(key, value);
}

It also applies some fixes so that new IterableWeakMap() doesn’t throw a TypeError, because undefined is not iterable and makes IterableWeakMap.length === WeakMap.length.