Debian Codegraph @

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

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