Debian Codegraph @

js/src/tests/non262/WeakMap/getOrInsert/returns-value-if-key-is-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 from the specified Object key
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 ...
16 features: [WeakMap]
17 ---*/
18
19 var foo = {};
20 var bar = {};
21 var baz = [];
22 var map = new WeakMap([
23 [foo, 0]
24 ]);
25
26 assertEq(map.getOrInsert(foo, 3), 0);
27
28 map.set(bar, 1);
29 assertEq(map.getOrInsert(bar, 4), 1);
30
31 map.set(baz, 2);
32 assertEq(map.getOrInsert(baz, 5), 2);
33
34 reportCompare(0, 0);