Debian Codegraph @

js/src/tests/non262/Map/getOrInsertComputed/does-not-evaluate-callbackfn-if-key-present.js

raw
1 // |reftest| shell-option(--enable-upsert) skip-if(!Map.prototype.getOrInsertComputed)
2 // Copyright (C) 2025 Jonas Haukenes. All rights reserved.
3 // This code is governed by the BSD license found in the LICENSE file.
4 /*---
5 esid: proposal-upsert
6 description: >
7 Does not evaluate the callback function if the key is already in the map.
8 info: |
9 WeakMap.prototype.getOrInsertComputed ( key, callbackfn )
10
11 ...
12 5. For each Record { [[Key]], [[Value]] } p of M.[[WeakMapData]], do
13 a. If p.[[Key]] is not empty and SameValue(p.[[Key]], key) is true, return p.[[Value]].
14 6. Let value be ? Call(callbackfn, undefined, « key »).
15 ...
16 features: [WeakMap]
17 ---*/
18
19 var map = new Map([
20 [1, 0]
21 ]);
22
23 function callback() {
24 throw new Error('Callbackfn should not be evaluated if key is present');
25 }
26
27 assertEq(map.getOrInsertComputed(1, callback), 0);
28
29 map.set(2, 1);
30 assertEq(map.getOrInsertComputed(2, callback), 1);
31
32 map.set(3, 2);
33 assertEq(map.getOrInsertComputed(3, callback), 2);
34
35 assertThrowsInstanceOf(function() {
36 map.getOrInsertComputed(4, callback)}
37 , Error);
38
39 reportCompare(0, 0);