Debian Codegraph @

js/src/tests/non262/WeakMap/getOrInsert/returns-value-if-key-is-not-present-object-key.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 Returns the value given as parameter when key is not present.
9 info: |
10 WeakMap.prototype.getOrInsert ( key, value )
11
12 ...
13 4. 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, return p.[[Value]].
15 5. Let p be the Record { [[Key]]: key, [[Value]]: value }.
16 6. Append p to M.[[WeakMapData]].
17 7. Return value.
18 features: [WeakMap]
19 ---*/
20
21 var foo = {};
22 var bar = {};
23 var baz = [];
24 var map = new WeakMap();
25
26 assertEq(map.getOrInsert(foo, 0), 0);
27
28 assertEq(map.getOrInsert(bar, 1), 1);
29
30 assertEq(map.getOrInsert(baz, 2), 2);
31
32 reportCompare(0, 0);