Move WeakMap.prototype.getOrInsertComputed tests from staging to built-ins

This commit is contained in:
Daniel Minor 2025-06-09 09:45:43 -04:00 committed by Jordan Harband
parent 609614febe
commit 6a73fa8f11
No known key found for this signature in database
GPG Key ID: 9F6A681E35EF8B56
24 changed files with 153 additions and 126 deletions

View File

@ -2,7 +2,7 @@
// Copyright (C) 2025 Jonas Haukenes. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: proposal-upsert
esid: sec-weakmap.prototype.getorinsertcomputed
description: |
Adds a value with an Object key if key is not already in the map.
info: |
@ -13,7 +13,6 @@ info: |
9. Append p to M.[[WeakMapData]].
...
features: [WeakMap, upsert]
flags: [noStrict]
---*/
var map = new WeakMap();
var foo = {};

View File

@ -2,7 +2,7 @@
// Copyright (C) 2025 Jonas Haukenes. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: proposal-upsert
esid: sec-weakmap.prototype.getorinsertcomputed
description: |
Adds a value with a Symbol key if key is not already in the map.
info: |
@ -13,7 +13,6 @@ info: |
9. Append p to M.[[WeakMapData]].
...
features: [Symbol, WeakMap, symbols-as-weakmap-keys, upsert]
flags: [noStrict]
---*/
var map = new WeakMap();
var foo = Symbol('a description');

View File

@ -1,7 +1,7 @@
// Copyright (C) 2025 Jonas Haukenes, Mathias Ness. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: proposal-upsert
esid: sec-weakmap.prototype.getorinsertcomputed
description: |
Does not throw if `callbackfn` is callable.
info: |
@ -10,8 +10,7 @@ info: |
...
3. If IsCallable(callbackfn) is false, throw a TypeError exception.
...
features: [arrow-function, upsert]
flags: [noStrict]
features: [arrow-function, upsert, WeakMap]
---*/
var bar = {};
var baz = {};

View File

@ -1,7 +1,7 @@
// Copyright (C) 2024 Jonas Haukenes, Mathias Ness. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: proposal-upsert
esid: sec-weakmap.prototype.getorinsertcomputed
description: |
WeakMap.getOrInsertComputed throws when callbackfn throws return if abrubt completion Call(callbackfn, key)
info: |
@ -10,8 +10,7 @@ info: |
...
6. Let value be ? Call(callbackfn, undefined, key).
...
features: [upsert]
flags: [noStrict]
features: [upsert, WeakMap]
---*/
var map = new WeakMap();

View File

@ -0,0 +1,24 @@
// Copyright (C) 2025 Daniel Minor. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-weakmap.prototype.getorinsertcomputed
description: |
Check that callbackfn receives undefined for this and exactly one argument.
info: |
WeakMap.prototype.getOrInsertComputed ( key , callbackfn )
...
6. Let value be ? Call(callbackfn, key).
...
features: [upsert, Symbol, WeakMap]
flags: [onlyStrict]
---*/
var map = new WeakMap();
var symbol = Symbol('a description');
map.getOrInsertComputed(symbol, function () {
assert.sameValue(this, undefined);
assert.sameValue(arguments.length, 1);
assert.sameValue(arguments[0], symbol);
});

View File

@ -0,0 +1,61 @@
// Copyright (C) 2025 Daniel Minor. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-weakmap.prototype.getorinsertcomputed
description: |
Check state after callback function throws
info: |
WeakMap.prototype.getOrInsertComputed ( key , callbackfn )
...
6. Let value be ? Call(callbackfn, key).
...
features: [upsert, WeakMap, Symbol]
---*/
var map = new WeakMap();
const obj0 = {};
const obj1 = {};
const obj2 = {};
const obj3 = {};
map.set(obj0, 'zero');
map.set(obj1, 'one');
map.set(obj2, 'two');
assert.throws(Error, function() {
map.getOrInsertComputed(Symbol('3'), function() {
throw new Error('throw in callback');
})
});
// Check the values after throwing in callbackfn.
assert.sameValue(map.get(obj0), 'zero');
assert.sameValue(map.get(obj1), 'one');
assert.sameValue(map.get(obj2), 'two');
assert.sameValue(map.has(obj3), false);
assert.throws(Error, function() {
map.getOrInsertComputed(obj3, function() {
map.set(obj1, 'mutated');
throw new Error('throw in callback');
})
});
// Check the values after throwing in callbackfn, with mutation.
assert.sameValue(map.get(obj0), 'zero');
assert.sameValue(map.get(obj1), 'mutated');
assert.sameValue(map.get(obj2), 'two');
assert.sameValue(map.has(obj3), false);
assert.throws(Error, function() {
map.getOrInsertComputed(obj3, function() {
map.set(obj3, 'mutated');
throw new Error('throw in callback');
})
});
// Check the values after throwing in callbackfn, with mutation.
assert.sameValue(map.get(obj0), 'zero');
assert.sameValue(map.get(obj1), 'mutated');
assert.sameValue(map.get(obj2), 'two');
assert.sameValue(map.get(obj3), 'mutated');

View File

@ -1,7 +1,7 @@
// Copyright (C) 2025 Jonas Haukenes. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: proposal-upsert
esid: sec-weakmap.prototype.getorinsertcomputed
description: |
Does not evaluate the callback function if the key is already in the map.
info: |
@ -13,25 +13,30 @@ info: |
6. Let value be ? Call(callbackfn, undefined, « key »).
...
features: [WeakMap, upsert]
flags: [noStrict]
---*/
var map = new Map([
[1, 0]
]);
const obj1 = {};
const obj2 = {};
const obj3 = {};
const obj4 = {};
const map = new WeakMap();
map.set(obj1, 0);
let callbackCalls = 0;
function callback() {
callbackCalls += 1;
throw new Error('Callbackfn should not be evaluated if key is present');
}
assert.sameValue(map.getOrInsertComputed(1, callback), 0);
assert.sameValue(map.getOrInsertComputed(obj1, callback), 0);
map.set(2, 1);
assert.sameValue(map.getOrInsertComputed(2, callback), 1);
map.set(obj2, 1);
assert.sameValue(map.getOrInsertComputed(obj2, callback), 1);
map.set(3, 2);
assert.sameValue(map.getOrInsertComputed(3, callback), 2);
map.set(obj3, 2);
assert.sameValue(map.getOrInsertComputed(obj3, callback), 2);
assert.throws(Error, function() {
map.getOrInsertComputed(4, callback)
map.getOrInsertComputed(obj4, callback)
}, Error);
assert.sameValue(callbackCalls, 1);

View File

@ -2,7 +2,7 @@
// Copyright (C) 2025 Jonas Haukenes. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: proposal-upsert
esid: sec-weakmap.prototype.getorinsertcomputed
description: |
Throws TypeError if `this` doesn't have a [[WeakMapData]] internal slot.
info: |
@ -11,15 +11,14 @@ info: |
...
2. Perform ? RequireInternalSlot(M, [[WeakMapData]]).
...
features: [upsert]
flags: [noStrict]
features: [upsert, WeakMap]
---*/
assert.throws(TypeError, function() {
WeakMap.prototype.getOrInsertComputed.call([], {}, () => 1);
});
var map = new WeakMap();
assert.throws(TypeError, function() {
var map = new WeakMap();
map.getOrInsertComputed.call([], {}, () => 1);
});

View File

@ -2,7 +2,7 @@
// Copyright (C) 2025 Jonas Haukenes. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: proposal-upsert
esid: sec-weakmap.prototype.getorinsertcomputed
description: |
Throws TypeError if `this` doesn't have a [[WeakMapData]] internal slot.
info: |
@ -11,15 +11,14 @@ info: |
...
2. Perform ? RequireInternalSlot(M, [[WeakMapData]]).
...
features: [Map, upsert]
flags: [noStrict]
features: [WeakMap, upsert]
---*/
assert.throws(TypeError, function() {
WeakMap.prototype.getOrInsertComputed.call(new Map(), {}, () => 1);
});
var map = new WeakMap();
assert.throws(TypeError, function() {
var map = new WeakMap();
map.getOrInsertComputed.call(new Map(), {}, () => 1);
});

View File

@ -2,7 +2,7 @@
// Copyright (C) 2025 Jonas Haukenes. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: proposal-upsert
esid: sec-weakmap.prototype.getorinsertcomputed
description: |
Throws TypeError if `this` doesn't have a [[WeakMapData]] internal slot.
info: |
@ -11,15 +11,14 @@ info: |
...
2. Perform ? RequireInternalSlot(M, [[WeakMapData]]).
...
features: [upsert]
flags: [noStrict]
features: [upsert, WeakMap]
---*/
assert.throws(TypeError, function() {
WeakMap.prototype.getOrInsertComputed.call({}, {}, () => 1);
});
var map = new WeakMap();
assert.throws(TypeError, function() {
var map = new WeakMap();
map.getOrInsertComputed.call({}, {}, () => 1);
});

View File

@ -2,7 +2,7 @@
// Copyright (C) 2025 Jonas Haukenes. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: proposal-upsert
esid: sec-weakmap.prototype.getorinsertcomputed
description: |
Throws TypeError if `this` doesn't have a [[WeakMapData]] internal slot.
info: |
@ -11,15 +11,14 @@ info: |
...
2. Perform ? RequireInternalSlot(M, [[WeakMapData]]).
...
features: [Set, upsert]
flags: [noStrict]
features: [Set, WeakMap, upsert]
---*/
assert.throws(TypeError, function() {
WeakMap.prototype.getOrInsertComputed.call(new Set(), {}, () => 1);
});
var map = new WeakMap();
assert.throws(TypeError, function() {
var map = new WeakMap();
map.getOrInsertComputed.call(new Set(), {}, () => 1);
});

View File

@ -2,7 +2,7 @@
// Copyright (C) 2025 Jonas Haukenes. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: proposal-upsert
esid: sec-weakmap.prototype.getorinsertcomputed
description: |
Throws TypeError if `this` doesn't have a [[WeakMapData]] internal slot.
info: |
@ -11,15 +11,14 @@ info: |
...
2. Perform ? RequireInternalSlot(M, [[WeakMapData]]).
...
features: [upsert]
flags: [noStrict]
features: [WeakMap, upsert]
---*/
assert.throws(TypeError, function() {
WeakMap.prototype.getOrInsertComputed.call(WeakMap.prototype, {}, () => 1);
});
var map = new WeakMap();
assert.throws(TypeError, function() {
var map = new WeakMap();
map.getOrInsertComputed.call(WeakMap.prototype, {}, () => 1);
});

View File

@ -2,7 +2,7 @@
// Copyright (C) 2025 Jonas Haukenes. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: proposal-upsert
esid: sec-weakmap.prototype.getorinsertcomputed
description: |
WeakMap.prototype.getOrInsertComputed property descriptor
info: |
@ -10,16 +10,9 @@ info: |
17 ECMAScript Standard Built-in Objects
includes: [propertyHelper.js]
features: [upsert]
flags: [noStrict]
features: [WeakMap, upsert]
---*/
assert.sameValue(
typeof WeakMap.prototype.getOrInsertComputed,
'function',
'typeof WeakMap.prototype.getOrInsertComputed is "function"'
);
verifyProperty(WeakMap.prototype, "getOrInsertComputed", {
verifyPrimordialCallableProperty(WeakMap.prototype, "getOrInsertComputed", "getOrInsertComputed", 2, {
value: WeakMap.prototype.getOrInsertComputed,
writable: true,
enumerable: false,

View File

@ -2,7 +2,7 @@
// Copyright (C) 2025 Jonas Haukenes. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: proposal-upsert
esid: sec-weakmap.prototype.getorinsertcomputed
description: |
WeakMap.prototype.getOrInsertComputed does not implement [[Construct]], is not new-able
info: |
@ -19,7 +19,6 @@ info: |
...
includes: [isConstructor.js]
features: [Reflect.construct, WeakMap, arrow-function, upsert]
flags: [noStrict]
---*/
assert.sameValue(
isConstructor(WeakMap.prototype.getOrInsertComputed),
@ -27,7 +26,8 @@ assert.sameValue(
'isConstructor(WeakMap.prototype.getOrInsertComputed) must return false'
);
let wm = new WeakMap();
assert.throws(TypeError, () => {
let wm = new WeakMap(); new wm.getOrInsertComputed({}, () => 1);
new wm.getOrInsertComputed({}, () => 1);
});

View File

@ -2,7 +2,7 @@
// Copyright (C) 2025 Jonas Haukenes, Mathias Ness. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: proposal-upsert
esid: sec-weakmap.prototype.getorinsertcomputed
description: |
Throws a TypeError if `callbackfn` is not callable.
info: |
@ -11,41 +11,45 @@ info: |
...
3. If IsCallable(callbackfn) is false, throw a TypeError exception.
...
features: [Symbol, upsert]
flags: [noStrict]
features: [WeakMap, Symbol, upsert]
---*/
var bar = {};
var m = new WeakMap();
assert.throws(TypeError, function () {
m.getOrInsertComputed.call(m, bar, 1);
m.getOrInsertComputed(bar, 1);
});
assert.throws(TypeError, function () {
m.getOrInsertComputed.call(m, bar, "");
m.getOrInsertComputed(bar, "");
});
assert.throws(TypeError, function () {
m.getOrInsertComputed.call(m, bar, true);
m.getOrInsertComputed(bar, true);
});
assert.throws(TypeError, function () {
m.getOrInsertComputed.call(m, bar, undefined);
m.getOrInsertComputed(bar, undefined);
});
assert.throws(TypeError, function () {
m.getOrInsertComputed.call(m, bar, null);
m.getOrInsertComputed(bar, null);
});
assert.throws(TypeError, function () {
m.getOrInsertComputed.call(m, bar, {});
m.getOrInsertComputed(bar, {});
});
assert.throws(TypeError, function () {
m.getOrInsertComputed.call(m, bar, []);
m.getOrInsertComputed(bar, []);
});
assert.throws(TypeError, function () {
m.getOrInsertComputed.call(m, bar, Symbol());
m.getOrInsertComputed(bar, Symbol());
});
// Check that it also throws if the key is already present (thus it does not try to call the callback)
m.set(bar, "foo");
assert.throws(TypeError, function () {
m.getOrInsertComputed(bar, 1);
});

View File

@ -1,7 +1,7 @@
// Copyright (C) 2025 Jonas Haukenes. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: proposal-upsert
esid: sec-weakmap.prototype.getorinsertcomputed
description: |
If the callbackfn inserts a value on the given key, the value is overwritten.
info: |
@ -16,8 +16,7 @@ info: |
8. Let p be the Record { [[Key]]: key, [[Value]]: value }.
9. Append p to M.[[WeakMapData]].
...
features: [upsert]
flags: [noStrict]
features: [WeakMap, upsert]
---*/
var map = new WeakMap();
var foo = {};

View File

@ -2,7 +2,7 @@
// Copyright (C) 2025 Jonas Haukenes. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: proposal-upsert
esid: sec-weakmap.prototype.getorinsertcomputed
description: |
Returns the value from the specified Object key
info: |
@ -13,7 +13,6 @@ info: |
9. Append p to M.[[WeakMapData]].
10. Return value.
features: [WeakMap, upsert]
flags: [noStrict]
---*/
var foo = {};
var bar = {};

View File

@ -2,7 +2,7 @@
// Copyright (C) 2025 Jonas Haukenes. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: proposal-upsert
esid: sec-weakmap.prototype.getorinsertcomputed
description: |
Returns the value from the specified Symbol key
info: |
@ -13,7 +13,6 @@ info: |
9. Append p to M.[[WeakMapData]].
10. Return value.
features: [Symbol, WeakMap, symbols-as-weakmap-keys, upsert]
flags: [noStrict]
---*/
var foo = Symbol('a description');
var bar = Symbol('a description');

View File

@ -2,7 +2,7 @@
// Copyright (C) 2025 Jonas Haukenes. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: proposal-upsert
esid: sec-weakmap.prototype.getorinsertcomputed
description: |
Returns the value from the specified Object key
info: |
@ -13,7 +13,6 @@ info: |
a. If p.[[Key]] is not empty and SameValue(p.[[Key]], key) is true, return p.[[Value]].
...
features: [WeakMap, upsert]
flags: [noStrict]
---*/
var foo = {};
var bar = {};

View File

@ -2,7 +2,7 @@
// Copyright (C) 2025 Jonas Haukenes. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: proposal-upsert
esid: sec-weakmap.prototype.getorinsertcomputed
description: |
Returns the value from the specified Symbol key
info: |
@ -14,7 +14,6 @@ info: |
10. Return value.
features: [Symbol, WeakMap, symbols-as-weakmap-keys, upsert]
flags: [noStrict]
---*/
var foo = Symbol('a description');
var bar = Symbol('a description');

View File

@ -2,7 +2,7 @@
// Copyright (C) 2025 Jonas Haukenes, Sune Eriksson Lianes. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: proposal-upsert
esid: sec-weakmap.prototype.getorinsertcomputed
description: |
Throws a TypeError if `this` is not an Object.
info: |
@ -11,8 +11,7 @@ info: |
1. Let M be the this value
2. Perform ? RequireInternalSlot(M, [[WeakMapData]])
...
features: [Symbol, upsert]
flags: [noStrict]
features: [WeakMap, Symbol, upsert]
---*/
var m = new WeakMap();

View File

@ -2,7 +2,7 @@
// Copyright (C) 2025 Jonas Haukenes. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: proposal-upsert
esid: sec-weakmap.prototype.getorinsertcomputed
description: |
Throws TypeError if key cannot be held weakly.
info: |
@ -12,7 +12,6 @@ info: |
4. If CanBeHeldWeakly(_key_) is *false*, throw a *TypeError* exception.
...
features: [Symbol, WeakMap, upsert]
flags: [noStrict]
---*/
var s = new WeakMap();

View File

@ -1,22 +0,0 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// Copyright (C) 2025 Jonas Haukenes. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: proposal-upsert
description: |
WeakMap.prototype.getOrInsertComputed.length descriptor
info: |
WeakMap.prototype.getOrInsertComputed ( key, callbackfn )
17 ECMAScript Standard Built-in Objects
includes: [propertyHelper.js]
features: [upsert]
flags: [noStrict]
---*/
verifyProperty(WeakMap.prototype.getOrInsertComputed, "length", {
value: 2,
writable: false,
enumerable: false,
configurable: true
});

View File

@ -1,22 +0,0 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// Copyright (C) 2025 Jonas Haukenes. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: proposal-upsert
description: |
WeakMap.prototype.getOrInsertComputed.name descriptor
info: |
WeakMap.prototype.getOrInsertComputed ( key, callbackfn )
17 ECMAScript Standard Built-in Objects
includes: [propertyHelper.js]
features: [upsert]
flags: [noStrict]
---*/
verifyProperty(Map.prototype.getOrInsertComputed, "name", {
value: "getOrInsertComputed",
writable: false,
enumerable: false,
configurable: true
});