Adapt existing tests regarding Symbols as weak values

WeakMap, WeakSet, WeakRef, and FinalizationRegistry all had tests
verifying what would happen if they were called with a value that wasn't
allowed as a weak value: before this proposal, that was a non-Object.
Now, allowed weak values are Objects, well-known Symbols, and unregistered
Symbols. That leaves registered Symbols that are still not allowed as weak
values.

This commit updates those tests to use a registered Symbol instead of an
unregistered Symbol; they should still pass, regardless of whether the
implementation has implemented symbols-as-weakmap-keys yet.

The tests are renamed as appropriate.

Also updates the frontmatter to the most current spec text, including the
CanBeHeldWeakly abstract operation.

See: #2850
This commit is contained in:
Philip Chimento 2022-09-22 17:58:07 -07:00 committed by Ms2ger
parent 735f95adf5
commit 6291e42a72
12 changed files with 67 additions and 98 deletions

View File

@ -3,18 +3,10 @@
/*--- /*---
esid: sec-finalization-registry.prototype.register esid: sec-finalization-registry.prototype.register
description: Throws a TypeError if target is not an Object description: Throws a TypeError if target cannot be held weakly
info: | info: |
FinalizationRegistry.prototype.register ( target , holdings [, unregisterToken ] ) FinalizationRegistry.prototype.register ( _target_ , _heldValue_ [, _unregisterToken_ ] )
3. If CanBeHeldWeakly(_target_) is *false*, throw a *TypeError* exception.
1. Let finalizationRegistry be the this value.
2. If Type(finalizationRegistry) is not Object, throw a TypeError exception.
3. If Type(target) is not Object, throw a TypeError exception.
4. If finalizationRegistry does not have a [[Cells]] internal slot, throw a TypeError exception.
5. If Type(unregisterToken) is not Object,
a. If unregisterToken is not undefined, throw a TypeError exception.
b. Set unregisterToken to empty.
...
features: [FinalizationRegistry] features: [FinalizationRegistry]
---*/ ---*/
@ -46,7 +38,7 @@ assert.throws(TypeError, function() {
finalizationRegistry.register('object'); finalizationRegistry.register('object');
}, 'string'); }, 'string');
var s = Symbol(); var s = Symbol.for('registered symbol');
assert.throws(TypeError, function() { assert.throws(TypeError, function() {
finalizationRegistry.register(s); finalizationRegistry.register(s);
}, 'symbol'); }, 'registered symbol');

View File

@ -3,17 +3,13 @@
/*--- /*---
esid: sec-finalization-registry.prototype.register esid: sec-finalization-registry.prototype.register
description: Throws a TypeError if unregisterToken is not an Object or undefined description: >
Throws a TypeError if unregisterToken is not undefined and cannot be held
weakly
info: | info: |
FinalizationRegistry.prototype.register ( target , holdings [, unregisterToken ] ) FinalizationRegistry.prototype.register ( _target_ , _heldValue_ [, _unregisterToken_ ] )
5. If CanBeHeldWeakly(_unregisterToken_) is *false*, then
1. Let finalizationRegistry be the this value. a. If _unregisterToken_ is not *undefined*, throw a *TypeError* exception.
2. If Type(finalizationRegistry) is not Object, throw a TypeError exception.
3. If Type(target) is not Object, throw a TypeError exception.
4. If finalizationRegistry does not have a [[Cells]] internal slot, throw a TypeError exception.
5. If Type(unregisterToken) is not Object,
a. If unregisterToken is not undefined, throw a TypeError exception.
...
features: [FinalizationRegistry] features: [FinalizationRegistry]
---*/ ---*/
@ -42,7 +38,7 @@ assert.throws(TypeError, function() {
finalizationRegistry.register(target, undefined, 'object'); finalizationRegistry.register(target, undefined, 'object');
}, 'string'); }, 'string');
var s = Symbol(); var s = Symbol.for('registered symbol');
assert.throws(TypeError, function() { assert.throws(TypeError, function() {
finalizationRegistry.register(target, undefined, s); finalizationRegistry.register(target, undefined, s);
}, 'symbol'); }, 'registered symbol');

View File

@ -3,15 +3,10 @@
/*--- /*---
esid: sec-finalization-registry.prototype.unregister esid: sec-finalization-registry.prototype.unregister
description: Throws a TypeError if unregisterToken is not an Object description: Throws a TypeError if unregisterToken cannot be held weakly
info: | info: |
FinalizationRegistry.prototype.unregister ( unregisterToken ) FinalizationRegistry.prototype.unregister ( _unregisterToken_ )
3. If CanBeHeldWeakly(_unregisterToken_) is *false*, throw a *TypeError* exception.
1. Let finalizationRegistry be the this value.
2. If Type(finalizationRegistry) is not Object, throw a TypeError exception.
3. If finalizationRegistry does not have a [[Cells]] internal slot, throw a TypeError exception.
4. If Type(unregisterToken) is not Object, throw a TypeError exception.
...
features: [FinalizationRegistry] features: [FinalizationRegistry]
---*/ ---*/
@ -43,7 +38,7 @@ assert.throws(TypeError, function() {
finalizationRegistry.unregister('object'); finalizationRegistry.unregister('object');
}, 'string'); }, 'string');
var s = Symbol(); var s = Symbol.for('registered symbol');
assert.throws(TypeError, function() { assert.throws(TypeError, function() {
finalizationRegistry.unregister(s); finalizationRegistry.unregister(s);
}, 'symbol'); }, 'registered symbol');

View File

@ -3,19 +3,19 @@
/*--- /*---
esid: sec-weakmap-iterable esid: sec-weakmap-iterable
description: > description: >
Throws a TypeError if iterable itens are not Objects. Throws a TypeError if keys in iterable items cannot be held weakly.
info: | info: |
WeakMap ( [ iterable ] ) WeakMap ( [ _iterable_ ] )
5. Let _adder_ be ? Get(_map_, *"set"*).
6. Return ? AddEntriesFromIterable(_map_, _iterable_, _adder_).
... AddEntriesFromIterable:
9. Repeat 3. Repeat,
... i. Let _status_ be Completion(Call(_adder_, _target_, « _k_, _v_ »)).
d. Let nextItem be IteratorValue(next). j. IfAbruptCloseIterator(_status_, _iteratorRecord_).
e. ReturnIfAbrupt(nextItem).
f. If Type(nextItem) is not Object, WeakMap.prototype.set( _key_, _value_ ):
i. Let error be Completion{[[type]]: throw, [[value]]: a newly created 4. If CanBeHeldWeakly(_key_) is *false*, throw a *TypeError* exception.
TypeError object, [[target]]:empty}.
ii. Return IteratorClose(iter, error).
features: [Symbol, WeakMap] features: [Symbol, WeakMap]
---*/ ---*/
@ -36,8 +36,8 @@ assert.throws(TypeError, function() {
}); });
assert.throws(TypeError, function() { assert.throws(TypeError, function() {
new WeakMap([Symbol('a'), 1]); new WeakMap([Symbol.for('registered symbol'), 1]);
}); }, 'Registered symbol not allowed as a WeakMap key');
assert.throws(TypeError, function() { assert.throws(TypeError, function() {
new WeakMap([undefined, 1]); new WeakMap([undefined, 1]);

View File

@ -3,11 +3,10 @@
/*--- /*---
esid: sec-weakmap.prototype.delete esid: sec-weakmap.prototype.delete
description: > description: >
Return false if value is not an Object. Return false if the key cannot be held weakly.
info: | info: |
WeakMap.prototype.delete ( value ) WeakMap.prototype.delete ( _key_ )
5. If CanBeHeldWeakly(_key_) is *false*, return *false*.
5. If Type(key) is not Object, return false.
features: [Symbol, WeakMap] features: [Symbol, WeakMap]
---*/ ---*/
@ -19,4 +18,4 @@ assert.sameValue(map.delete(NaN), false);
assert.sameValue(map.delete(null), false); assert.sameValue(map.delete(null), false);
assert.sameValue(map.delete(undefined), false); assert.sameValue(map.delete(undefined), false);
assert.sameValue(map.delete(true), false); assert.sameValue(map.delete(true), false);
assert.sameValue(map.delete(Symbol()), false); assert.sameValue(map.delete(Symbol.for('registered symbol')), false, 'registered symbol');

View File

@ -3,15 +3,10 @@
/*--- /*---
esid: sec-weakmap.prototype.get esid: sec-weakmap.prototype.get
description: > description: >
Returns undefined when key is not an Object. Returns undefined when key cannot be held weakly.
info: | info: |
WeakMap.prototype.get ( key ) WeakMap.prototype.get ( _key_ )
4. If CanBeHeldWeakly(_key_) is *false*, return *undefined*.
...
4. Let entries be the List that is the value of Ms [[WeakMapData]] internal
slot.
5. If Type(key) is not Object, return undefined.
...
features: [Symbol, WeakMap] features: [Symbol, WeakMap]
---*/ ---*/
@ -37,6 +32,6 @@ assert.sameValue(
); );
assert.sameValue( assert.sameValue(
map.get(Symbol()), undefined, map.get(Symbol.for('registered symbol')), undefined,
'Returns undefined if key is a Symbol' 'Returns undefined if key is a registered Symbol'
); );

View File

@ -3,11 +3,10 @@
/*--- /*---
esid: sec-weakmap.prototype.has esid: sec-weakmap.prototype.has
description: > description: >
Returns false if value is not an Object. Returns false if the key cannot be held weakly
info: | info: |
WeakMap.prototype.has ( value ) WeakMap.prototype.has ( _key_ )
4. If CanBeHeldWeakly(_key_) is *false*, return *false*.
5. If Type(key) is not Object, return false.
features: [Symbol, WeakMap] features: [Symbol, WeakMap]
---*/ ---*/
@ -18,4 +17,4 @@ assert.sameValue(map.has(''), false);
assert.sameValue(map.has(null), false); assert.sameValue(map.has(null), false);
assert.sameValue(map.has(undefined), false); assert.sameValue(map.has(undefined), false);
assert.sameValue(map.has(true), false); assert.sameValue(map.has(true), false);
assert.sameValue(map.has(Symbol()), false); assert.sameValue(map.has(Symbol.for('registered symbol')), false, 'Registered symbol not allowed as key');

View File

@ -2,11 +2,10 @@
// This code is governed by the BSD license found in the LICENSE file. // This code is governed by the BSD license found in the LICENSE file.
/*--- /*---
esid: sec-weakmap.prototype.set esid: sec-weakmap.prototype.set
description: Throws TypeError if `key` is not Object. description: Throws TypeError if key cannot be held weakly.
info: | info: |
WeakMap.prototype.set ( key, value ) WeakMap.prototype.set ( _key_, _value_ )
4. If CanBeHeldWeakly(_key_) is *false*, throw a *TypeError* exception.
5. If Type(key) is not Object, throw a TypeError exception.
features: [Symbol, WeakMap] features: [Symbol, WeakMap]
---*/ ---*/
@ -33,5 +32,5 @@ assert.throws(TypeError, function() {
}); });
assert.throws(TypeError, function() { assert.throws(TypeError, function() {
s.set(Symbol(), 1); s.set(Symbol.for('registered symbol'), 1);
}); }, 'Registered symbol not allowed as WeakMap key');

View File

@ -4,13 +4,10 @@
/*--- /*---
esid: sec-weak-ref-target esid: sec-weak-ref-target
description: > description: >
Throws a TypeError if target is not Object Throws a TypeError if target cannot be held weakly
info: | info: |
WeakRef ( target ) WeakRef ( _target_ )
2. If CanBeHeldWeakly(_target_) is *false*, throw a *TypeError* exception.
1. If NewTarget is undefined, throw a TypeError exception.
2. If Type(target) is not Object, throw a TypeError exception.
...
features: [WeakRef] features: [WeakRef]
---*/ ---*/
@ -39,10 +36,10 @@ assert.throws(TypeError, function() {
new WeakRef('Object'); new WeakRef('Object');
}, 'string'); }, 'string');
var s = Symbol(); var s = Symbol.for('registered symbol');
assert.throws(TypeError, function() { assert.throws(TypeError, function() {
new WeakRef(s); new WeakRef(s);
}, 'symbol'); }, 'registered symbol');
assert.throws(TypeError, function() { assert.throws(TypeError, function() {
new WeakRef(true); new WeakRef(true);

View File

@ -2,11 +2,10 @@
// This code is governed by the BSD license found in the LICENSE file. // This code is governed by the BSD license found in the LICENSE file.
/*--- /*---
esid: sec-weakset.prototype.add esid: sec-weakset.prototype.add
description: Throws TypeError if `value` is not Object. description: Throws TypeError if value cannot be held weakly.
info: | info: |
WeakSet.prototype.add ( value ) WeakSet.prototype.add ( _value_ )
3. If CanBeHeldWeakly(_value_) is *false*, throw a *TypeError* exception.
4. If Type(value) is not Object, throw a TypeError exception.
features: [Symbol, WeakSet] features: [Symbol, WeakSet]
---*/ ---*/
@ -33,5 +32,5 @@ assert.throws(TypeError, function() {
}); });
assert.throws(TypeError, function() { assert.throws(TypeError, function() {
s.add(Symbol()); s.add(Symbol.for('registered symbol'));
}); }, 'Registered symbol not allowed as WeakSet value');

View File

@ -3,11 +3,10 @@
/*--- /*---
esid: sec-weakset.prototype.delete esid: sec-weakset.prototype.delete
description: > description: >
Return false if value is not a non-null Object. Return false if value cannot be held weakly.
info: | info: |
WeakSet.prototype.delete ( value ) WeakSet.prototype.delete ( _value_ )
3. If CanBeHeldWeakly(_value_) is *false*, return *false*.
4. If Type(value) is not Object, return false.
features: [Symbol, WeakSet] features: [Symbol, WeakSet]
---*/ ---*/
@ -18,4 +17,4 @@ assert.sameValue(s.delete(''), false);
assert.sameValue(s.delete(null), false); assert.sameValue(s.delete(null), false);
assert.sameValue(s.delete(undefined), false); assert.sameValue(s.delete(undefined), false);
assert.sameValue(s.delete(true), false); assert.sameValue(s.delete(true), false);
assert.sameValue(s.delete(Symbol()), false); assert.sameValue(s.delete(Symbol.for('registered symbol')), false, 'Registered symbol not allowed as value');

View File

@ -3,11 +3,10 @@
/*--- /*---
esid: sec-weakset.prototype.has esid: sec-weakset.prototype.has
description: > description: >
Returns false if value is not a non-null Object. Returns false if value cannot be held weakly.
info: | info: |
WeakSet.prototype.has ( value ) WeakSet.prototype.has ( _value_ )
4. If CanBeHeldWeakly(_value_) is *false*, return *false*.
5. If Type(value) is not Object, return false.
features: [Symbol, WeakSet] features: [Symbol, WeakSet]
---*/ ---*/
@ -18,4 +17,4 @@ assert.sameValue(s.has(''), false);
assert.sameValue(s.has(null), false); assert.sameValue(s.has(null), false);
assert.sameValue(s.has(undefined), false); assert.sameValue(s.has(undefined), false);
assert.sameValue(s.has(true), false); assert.sameValue(s.has(true), false);
assert.sameValue(s.has(Symbol()), false); assert.sameValue(s.has(Symbol.for('registered symbol')), false, 'Registered symbol not allowed as value');