Debian Codegraph @

js/src/tests/non262/WeakMap/getOrInsertComputed/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.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 8. Let p be the Record { [[Key]]: key, [[Value]]: value }.
14 9. Append p to M.[[WeakMapData]].
15 10. Return value.
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
24 assertEq(map.getOrInsertComputed(foo, () => 0), 0, 'Regular symbol as key, added in constructor');
25 assertEq(map.getOrInsertComputed(baz, () => 2), 2, 'Regular symbol as key, added with set()');
26 assertEq(map.getOrInsertComputed(bar, () => 1), 1, "Symbols with the same description don't overwrite each other");
27 assertEq(map.getOrInsertComputed(Symbol.hasInstance, () => 3), 3, 'Well-known symbol as key');
28
29 reportCompare(0, 0);