Debian Codegraph @

js/src/tests/non262/WeakMap/getOrInsertComputed/returns-value-if-key-is-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.getOrInsertComputed) -- 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 from the specified Symbol key
9 info: |
10 WeakMap.prototype.getOrInsertComputed ( key, callbackfn )
11
12 ...
13 5. 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: [Symbol, WeakMap, symbols-as-weakmap-keys]
17 ---*/
18
19 var foo = Symbol('a description');
20 var bar = Symbol('a description');
21 var baz = Symbol('different description');
22 var map = new WeakMap([
23 [foo, 0],
24 ]);
25
26 assertEq(map.getOrInsertComputed(foo, () => 3), 0, 'Regular symbol as key, added in constructor');
27
28 map.set(bar, 1);
29 map.set(baz, 2);
30 assertEq(map.getOrInsertComputed(baz, () => 4), 2, 'Regular symbol as key, added with set()');
31 assertEq(map.getOrInsertComputed(bar, () => 5), 1, "Symbols with the same description don't overwrite each other");
32
33 map.set(Symbol.hasInstance, 3);
34 assertEq(map.getOrInsertComputed(Symbol.hasInstance, () => 6), 3, 'Well-known symbol as key');
35
36 reportCompare(0, 0);