Debian Codegraph @

js/src/tests/non262/WeakMap/getOrInsertComputed/overwrites-mutation-from-callbackfn.js

raw
1 // |reftest| shell-option(--enable-upsert) skip-if(!WeakMap.prototype.getOrInsertComputed)
2 // Copyright (C) 2025 Jonas Haukenes. All rights reserved.
3 // This code is governed by the BSD license found in the LICENSE file.
4 /*---
5 esid: proposal-upsert
6 description: >
7 If the callbackfn inserts a value on the given key, the value is overwritten.
8 info: |
9 WeakMap.prototype.set ( key, value )
10
11 ...
12 6. Let value be ? Call(callbackfn, undefined, « key »).
13 7. For each Record { [[Key]], [[Value]] } p of M.[[WeakMapData]], do
14 a. If p.[[Key]] is not empty and SameValue(p.[[Key]], key) is true, then
15 i. Set p.[[Value]] to value.
16 ii. Return value.
17 8. Let p be the Record { [[Key]]: key, [[Value]]: value }.
18 9. Append p to M.[[WeakMapData]].
19 ...
20 ---*/
21
22 var map = new WeakMap();
23 var foo = {};
24 var bar = {};
25 var baz = {};
26
27 map.getOrInsertComputed(foo, () => {map.set(foo, 0); return 3;});
28 map.getOrInsertComputed(bar, () => {map.set(bar, 1)});
29 map.getOrInsertComputed(baz, () => {map.set(baz, 2); return 'string';});
30
31 assertEq(map.get(foo), 3);
32 assertEq(map.get(bar), undefined);
33 assertEq(map.get(baz), 'string');
34
35 reportCompare(0, 0);