Debian Codegraph @

js/src/tests/non262/Map/getOrInsertComputed/append-new-values.js

raw
1 // |reftest| shell-option(--enable-upsert) skip-if(!Map.prototype.getOrInsertComputed)
2 // Copyright (C) 2015 the V8 project authors. All rights reserved.
3 // Copyright (C) 2024 Jonas Haukenes, Mathias Ness. 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 Append a new value as the last element of entries.
9 info: |
10 Map.prototype.getOrInsertComputed ( key , callbackfn )
11
12 ...
13 4. Set key to CanonicalizeKeyedCollectionKey(key).
14 5. For each Record { [[Key]], [[Value]] } p of M.[[MapData]], do
15 a. If p.[[Key]] is not empty and SameValue(p.[[Key]], key) is true, return p.[[Value]].
16 6. Let value be ? Call(callbackfn, key).
17 7. Let p be the Record { [[Key]]: key, [[Value]]: value }.
18 8. Append p to M.[[MapData]].
19 ...
20 features: [Symbol, arrow-function]
21 ---*/
22
23 var s = Symbol(2);
24 var map = new Map([[4, 4], ['foo3', 3], [s, 2]]);
25
26 map.getOrInsertComputed(null, () => 42);
27 map.getOrInsertComputed(1, () => 'valid');
28
29 assertEq(map.size, 5);
30 assertEq(map.get(1), 'valid');
31
32 var results = [];
33
34 map.forEach(function(value, key) {
35 results.push({
36 value: value,
37 key: key
38 });
39 });
40
41 var result = results.pop();
42 assertEq(result.value, 'valid');
43 assertEq(result.key, 1);
44
45 result = results.pop();
46 assertEq(result.value, 42);
47 assertEq(result.key, null);
48
49 result = results.pop();
50 assertEq(result.value, 2);
51 assertEq(result.key, s);
52
53 reportCompare(0, 0);