Debian Codegraph @

js/src/tests/non262/WeakMap/getOrInsert/returns-value-if-key-is-not-present-symbol-key.js

raw
1 // |reftest| shell-option(--enable-symbols-as-weakmap-keys) skip-if(release_or_beta||!xulRuntime.shell) shell-option(--enable-upsert) skip-if(!WeakMap.prototype.getOrInsert) -- symbols-as-weakmap-keys is not released yet, requires shell-options
2 // Copyright (C) 2022 Igalia, S.L. 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: [Symbol, WeakMap, symbols-as-weakmap-keys]
19 ---*/
20
21 var foo = Symbol('a description');
22 var bar = Symbol('a description');
23 var baz = Symbol('different description');
24 var map = new WeakMap();
25
26 assertEq(map.getOrInsert(foo, 0), 0, 'Regular symbol as key, added in constructor');
27
28 assertEq(map.getOrInsert(baz, 2), 2, 'Regular symbol as key, added with set()');
29 assertEq(map.getOrInsert(bar, 1), 1, "Symbols with the same description don't overwrite each other");
30
31 assertEq(map.getOrInsert(Symbol.hasInstance, 3), 3, 'Well-known symbol as key');
32
33 reportCompare(0, 0);