Debian Codegraph @

js/src/tests/non262/WeakMap/getOrInsertComputed/throw-if-key-cannot-be-held-weakly.js

raw
1 // |reftest| shell-option(--enable-upsert) skip-if(!WeakMap.prototype.getOrInsertComputed)
2 // Copyright (C) 2015 the V8 project authors. 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: Throws TypeError if key cannot be held weakly.
8 info: |
9 WeakMap.prototype.getOrInsertComputed ( key, callbackfn )
10
11 ...
12 4. If CanBeHeldWeakly(_key_) is *false*, throw a *TypeError* exception.
13 ...
14 features: [Symbol, WeakMap]
15 ---*/
16
17 var s = new WeakMap();
18
19 assertThrowsInstanceOf(function() {
20 s.getOrInsertComputed(1, () => 1);
21 }, TypeError);
22
23 assertThrowsInstanceOf(function() {
24 s.getOrInsertComputed(false, () => 1);
25 }, TypeError);
26
27 assertThrowsInstanceOf(function() {
28 s.getOrInsertComputed(undefined, () => 1);
29 }, TypeError);
30
31 assertThrowsInstanceOf(function() {
32 s.getOrInsertComputed('string', () => 1);
33 }, TypeError);
34
35 assertThrowsInstanceOf(function() {
36 s.getOrInsertComputed(null, () => 1);
37 }, TypeError);
38
39 assertThrowsInstanceOf(function() {
40 s.getOrInsertComputed(Symbol.for('registered symbol'), () => 1);
41 }, TypeError, 'Registered symbol not allowed as WeakMap key');
42
43 reportCompare(0, 0);