Debian Codegraph @

js/src/tests/non262/WeakMap/getOrInsertComputed/adds-object-element.js

raw
1 // |reftest| shell-option(--enable-upsert) skip-if(!WeakMap.prototype.getOrInsertComputed)
2 // Copyright (C) 2015 the V8 project authors. All rights reserved.
3 // Copyright (C) 2025 Jonas Haukenes. All rights reserved.
4 // This code is governed by the BSD license found in the LICENSE file.
5 /*---
6 esid: proposal-upsert
7 description: >
8 Adds a value with an Object key if key is not already in the map.
9 info: |
10 WeakMap.prototype.getOrInsertComputed ( key, callbackfn )
11
12 ...
13 8. Let p be the Record { [[Key]]: key, [[Value]]: value }.
14 9. Append p to M.[[WeakMapData]].
15 ...
16 features: [WeakMap]
17 ---*/
18
19 var map = new WeakMap();
20 var foo = {};
21 var bar = {};
22 var baz = {};
23
24 map.getOrInsertComputed(foo, () => 1);
25 map.getOrInsertComputed(bar, () => 2);
26 map.getOrInsertComputed(baz, () => 3);
27
28 assertEq(map.has(foo), true);
29 assertEq(map.has(bar), true);
30 assertEq(map.has(baz), true);
31
32 assertEq(map.get(foo), 1);
33 assertEq(map.get(bar), 2);
34 assertEq(map.get(baz), 3);
35
36 reportCompare(0, 0);