Debian Codegraph @

js/src/tests/non262/WeakMap/getOrInsert/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.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 Adds a value with a Symbol key if key is not already in the map.
9 info: |
10 WeakMap.prototype.getOrInsert ( key, value )
11
12 ...
13 5. Let p be the Record {[[Key]]: key, [[Value]]: value}.
14 6. 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.getOrInsert(foo, 1);
24 map.getOrInsert(bar, 2);
25 map.getOrInsert(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);