diff --git a/src/generics/lib.generics.ts b/src/generics/lib.generics.ts index 0f3e47b..649c71d 100644 --- a/src/generics/lib.generics.ts +++ b/src/generics/lib.generics.ts @@ -3428,7 +3428,9 @@ class Map { if (this._entries[i].next >= -1) { const key = this._entries[i].key; const value = this._entries[i].value; - yield <[key: K, value: V]>[key, value]; + // `[key, value]`, as in JavaScript: a tuple without labels, which JSON.stringify writes as an array + // (a labelled one was `{"key": ..., "value": ...}`) + yield <[K, V]>[key, value]; } } } @@ -3556,9 +3558,10 @@ class Set { return this.removeValue(v); } + // `[value, value]` for each value, as in JavaScript (a set's key is its value): it gave the values alone @borrows("this") entries() { - return this.iter(); + return this.iterEntries(); } @borrows("this") @@ -3866,6 +3869,18 @@ class Set { } } } + + @borrows("this") + private *iterEntries() { + for (let i = 0; i < this.count; i++) { + if (this._entries[i].next >= -1) { + // (two reads of the field, as Map's two: one value put into the tuple twice crashed under -mm=own) + const key = this._entries[i].value; + const value = this._entries[i].value; + yield <[V, V]>[key, value]; + } + } + } } // The value of a string index signature, `{ [key: string]: T }`, and of an object literal with a key known diff --git a/tests/map_entries.ts b/tests/map_entries.ts index 874e9d4..76f05a3 100644 --- a/tests/map_entries.ts +++ b/tests/map_entries.ts @@ -3,6 +3,6 @@ const map1 = new Map(); map1.set('0', 'foo'); map1.set(1, 'bar'); -for (const entry of map1.entries()) console.log(`entry: ${entry.key} = ${entry.value}`); +for (const entry of map1.entries()) console.log(`entry: ${entry[0]} = ${entry[1]}`); console.log("ALL DONE"); diff --git a/tests/set_entries.ts b/tests/set_entries.ts new file mode 100644 index 0000000..242c60a --- /dev/null +++ b/tests/set_entries.ts @@ -0,0 +1,14 @@ +// Set.entries() yields `[value, value]` for each value, as in JavaScript, and Map.entries() `[key, value]` tuples +// that JSON.stringify writes as arrays +const set1 = new Set(); +set1.add("x" + 1); +set1.add("y" + 2); +for (const entry of set1.entries()) console.log(`entry: ${entry[0]} = ${entry[1]}`); +for (const [a, b] of set1.entries()) console.log(a == b ? "same" : "different"); + +const map1 = new Map(); +map1.set("a", 1); +map1.set("b", 2); +console.log(JSON.stringify([...map1.entries()])); + +console.log("ALL DONE");