Debian Codegraph @

js/src/tests/non262/WeakMap/getOrInsertComputed/adds-symbol-element.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 Adds a value with a Symbol key if key is not already in the map.
9 info: |
10 WeakMap.prototype.getOrInsertComputed ( _key_, callbackfn )
11
12 ...
13 8. Let p be the Record { [[Key]]: key, [[Value]]: value }.
14 9. Append p to M.[[WeakMapData]].
15 ...
16 features: [Symbol, WeakMap, symbols-as-weakmap-keys]
17 ---*/
18
19 var map = new WeakMap();
20 var foo = Symbol('a description');
21 var bar = Symbol('a description');
22
23 map.getOrInsertComputed(foo, () => 1);
24 map.getOrInsertComputed(bar, () => 2);
25 map.getOrInsertComputed(Symbol.hasInstance, () => 3);
26
27 assertEq(map.has(bar), true, 'Regular symbol as key');
28 assertEq(map.get(foo), 1, "Symbols with the same description don't overwrite each other");
29 assertEq(map.has(Symbol.hasInstance), true, 'Well-known symbol as key');
30
31 assertEq(map.get(bar), 2);
32 assertEq(map.get(Symbol.hasInstance), 3);
33
34 reportCompare(0, 0);