Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions test/staging/upsert/Map/getOrInsert-this-is-a-cross-compartment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (C) 2025 Mozilla Corporation. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
flags: [noStrict]
description: |
pending
esid: proposal-upsert
---*/

const g = createNewGlobal({ newCompartment: true });

var map = g.eval("new Map()");

Map.prototype.getOrInsert.call(map, 1, 2);
assert.sameValue(map.get(1), 2);

map.getOrInsert(2, 3);
assert.sameValue(map.get(2), 3);

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (C) 2024 Jonas Haukenes. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: proposal-upsert
description: |
Append a new value in the map normalizing +0 and -0.
info: |
Map.prototype.getOrInsert ( key , value )

...
3. Set key to CanonicalizeKeyedCollectionKey(key).
4. For each Record { [[Key]], [[Value]] } p of M.[[MapData]], do
a. If p.[[Key]] is not empty and SameValue(p.[[Key]], key) is true, return p.[[Value]].
5. Let p be the Record { [[Key]]: key, [[Value]]: value }.
6. Append p to M.[[MapData]].
...
features: [Symbol]
flags: [noStrict]
---*/
// Copyright (C) 2015 the V8 project authors. All rights reserved.
var map = new Map();
map.getOrInsert(-0, 42);
assert.sameValue(map.get(0), 42);

map = new Map();
map.getOrInsert(+0, 43);
assert.sameValue(map.get(0), 43);

50 changes: 50 additions & 0 deletions test/staging/upsert/Map/getOrInsert/append-new-values.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (C) 2024 Jonas Haukenes. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: proposal-upsert
description: |
Append a new value as the last element of entries.
info: |
Map.prototype.getOrInsert ( key , value )

...
3. Set key to CanonicalizeKeyedCollectionKey(key).
4. For each Record { [[Key]], [[Value]] } p of M.[[MapData]], do
a. If p.[[Key]] is not empty and SameValue(p.[[Key]], key) is true, return p.[[Value]].
5. Let p be the Record { [[Key]]: key, [[Value]]: value }.
6. Append p to M.[[MapData]].
...
features: [Symbol]
flags: [noStrict]
---*/
// Copyright (C) 2015 the V8 project authors. All rights reserved.
var s = Symbol(2);
var map = new Map([[4, 4], ['foo3', 3], [s, 2]]);

map.getOrInsert(null, 42);
map.getOrInsert(1, 'valid');

assert.sameValue(map.size, 5);
assert.sameValue(map.get(1), 'valid');

var results = [];

map.forEach(function(value, key) {
results.push({
value: value,
key: key
});
});

var result = results.pop();
assert.sameValue(result.value, 'valid');
assert.sameValue(result.key, 1);

result = results.pop();
assert.sameValue(result.value, 42);
assert.sameValue(result.key, null);

result = results.pop();
assert.sameValue(result.value, 2);
assert.sameValue(result.key, s);

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (C) 2024 Sune Eriksson Lianes. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: proposal-upsert
description: |
Inserts the value for the specified key on different types, when key not present.
info: |
Map.prototype.getOrInsert ( key , value )

...
5. Let p be the Record { [[Key]]: key, [[Value]]: value }.
6. Append p to M.[[MapData]].
...
features:
- Symbol
flags: [noStrict]
---*/
// Copyright (C) 2015 the V8 project authors. All rights reserved.
var map = new Map();

map.getOrInsert('bar', 0);
assert.sameValue(map.get('bar'), 0);

map.getOrInsert(1, 42);
assert.sameValue(map.get(1), 42);

map.getOrInsert(NaN, 1);
assert.sameValue(map.get(NaN), 1);

var item = {};
map.getOrInsert(item, 2);
assert.sameValue(map.get(item), 2);

item = [];
map.getOrInsert(item, 3);
assert.sameValue(map.get(item), 3);

item = Symbol('item');
map.getOrInsert(item, 4);
assert.sameValue(map.get(item), 4);

item = null;
map.getOrInsert(item, 5);
assert.sameValue(map.get(item), 5);

item = undefined;
map.getOrInsert(item, 6);
assert.sameValue(map.get(item), 6);

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (C) 2024 Sune Eriksson Lianes. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: proposal-upsert
description: |
Throws a TypeError if `this` is a Set Object
info: |
Map.prototype.getOrInsert ( key , value )

...
1. Let M be the this value.
2. Perform ? RequireInternalSlot(M, [[MapData]])
...
features: [Set]
flags: [noStrict]
---*/
// Copyright (C) 2015 the V8 project authors. All rights reserved.
assertThrowsInstanceOf(function () {
Map.prototype.getOrInsert.call(new Set(), 1, 1);
}, TypeError);

assertThrowsInstanceOf(function () {
var map = new Map();
map.getOrInsert.call(new Set(), 1, 1);
}, TypeError);

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (C) 2024 Sune Eriksson Lianes. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: proposal-upsert
description: |
Throws a TypeError if `this` is a WeakMap object.
info: |
Map.prototype.getOrInsert ( key , value )

...
1. Let M be the this value.
2. Perform ? RequireInternalSlot(M, [[MapData]]).
...
features: [WeakMap]
flags: [noStrict]
---*/
// Copyright (C) 2015 the V8 project authors. All rights reserved.
assertThrowsInstanceOf(function() {
Map.prototype.getOrInsert.call(new WeakMap(), 1, 1);
}, TypeError);

assertThrowsInstanceOf(function() {
var map = new Map();
map.getOrInsert.call(new WeakMap(), 1, 1);
}, TypeError);

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (C) 2024 Sune Eriksson Lianes. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: proposal-upsert
description: |
Throws a TypeError if `this` object does not have a [[MapData]] internal slot.
info: |
Map.getOrInsert ( key , value )

...
1. Let M be the this value.
2. Perform ? RequireInternalSLot(M, [[MapData]])
...
flags: [noStrict]
---*/
// Copyright (C) 2015 the V8 project authors. All rights reserved.
var map = new Map();

assertThrowsInstanceOf(function () {
Map.prototype.getOrInsert.call([], 1, 1);
}, TypeError);

assertThrowsInstanceOf(function () {
map.getOrInsert.call([], 1, 1);
}, TypeError);

assertThrowsInstanceOf(function () {
Map.prototype.getOrInsert.call({}, 1, 1);
}, TypeError);

assertThrowsInstanceOf(function () {
map.getOrInsert.call({}, 1, 1);
}, TypeError);

29 changes: 29 additions & 0 deletions test/staging/upsert/Map/getOrInsert/getOrInsert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (C) 2024 Jonas Haukenes. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: proposal-upsert
description: |
Property type and descriptor.
info: |
Map.prototype.getOrInsert ( key , value )

17 ECMAScript Standard Built-in Objects
includes: [deepEqual.js]
flags: [noStrict]
---*/
// Copyright (C) 2015 the V8 project authors. All rights reserved.
assert.sameValue(
typeof Map.prototype.getOrInsert,
'function',
'`typeof Map.prototype.getOrInsert` is `function`'
);


assert.deepEqual(Object.getOwnPropertyDescriptor(Map.prototype, "getOrInsert"), {
value: Map.prototype.getOrInsert,
writable: true,
enumerable: false,
configurable: true
});


21 changes: 21 additions & 0 deletions test/staging/upsert/Map/getOrInsert/length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (C) 2024 Jonas Haukenes. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: proposal-upsert
description: |
Map.prototype.getOrInsert.length value and descriptor.
info: |
Map.prototype.getOrInsert ( key , value )

17 ECMAScript Standard Built-in Objects
includes: [deepEqual.js]
flags: [noStrict]
---*/
// Copyright (C) 2015 the V8 project authors. All rights reserved.
assert.deepEqual(Object.getOwnPropertyDescriptor(Map.prototype.getOrInsert, "length"), {
value: 2,
writable: false,
enumerable: false,
configurable: true
});

21 changes: 21 additions & 0 deletions test/staging/upsert/Map/getOrInsert/name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (C) 2024 Jonas Haukenes. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: proposal-upsert
description: |
Map.prototype.getOrInsert.name value and descriptor.
info: |
Map.prototype.getOrInsert ( key , value )

17 ECMAScript Standard Built-in Objects
includes: [deepEqual.js]
flags: [noStrict]
---*/
// Copyright (C) 2015 the V8 project authors. All rights reserved.
assert.deepEqual(Object.getOwnPropertyDescriptor(Map.prototype.getOrInsert, "name"), {
value: "getOrInsert",
writable: false,
enumerable: false,
configurable: true
});

30 changes: 30 additions & 0 deletions test/staging/upsert/Map/getOrInsert/not-a-constructor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (C) 2024 Jonas Haukenes. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: proposal-upsert
description: |
Map.prototype.getOrInsert does not implement [[Construct]], is not new-able
info: |
ECMAScript Function Objects

Built-in function objects that are not identified as constructors do not
implement the [[Construct]] internal method unless otherwise specified in
the description of a particular function.

sec-evaluatenew

...
7. If IsConstructor(constructor) is false, throw a TypeError exception.
...
includes: [isConstructor.js]
features: [Map, Reflect.construct, arrow-function]
flags: [noStrict]
---*/
// Copyright (C) 2020 Rick Waldron. All rights reserved.
assert.sameValue(isConstructor(Map.prototype.getOrInsert), false, 'isConstructor(Map.prototype.getOrInsert) must return false');

assertThrowsInstanceOf(() => {
let m = new Map(); new m.getOrInsert();
}, TypeError);

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (C) 2024 Jonas Haukenes. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: proposal-upsert
description: |
Returns the value from the specified key on different types, when key not present.
info: |
Map.prototype.getOrInsert ( key , value )

...
5. Let p be the Record { [[Key]]: key, [[Value]]: value }.
6. Append p to M.[[MapData]].
7. Return p.[[Value]].
...
features:
- Symbol
flags: [noStrict]
---*/
// Copyright (C) 2015 the V8 project authors. All rights reserved.
var map = new Map();

assert.sameValue(map.getOrInsert('bar', 0), 0);

assert.sameValue(map.getOrInsert(1, 42), 42);

assert.sameValue(map.getOrInsert(NaN, 1), 1);

var item = {};
assert.sameValue(map.getOrInsert(item, 2), 2);

item = [];
assert.sameValue(map.getOrInsert(item, 3), 3);

item = Symbol('item');
assert.sameValue(map.getOrInsert(item, 4), 4);

item = null;
assert.sameValue(map.getOrInsert(item, 5), 5);

item = undefined;
assert.sameValue(map.getOrInsert(item, 6), 6);

Loading