Debian Codegraph @

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

raw
1 // |reftest| shell-option(--enable-upsert) skip-if(!WeakMap.prototype.getOrInsert)
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.getOrInsert ( key, value )
11
12 ...
13 5. Let p be the Record {[[Key]]: key, [[Value]]: value}.
14 6. 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.getOrInsert(foo, 1);
25 map.getOrInsert(bar, 2);
26 map.getOrInsert(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);