Debian Codegraph @

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

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