Skip to content

Commit

Permalink
Adapt existing tests regarding Symbols as weak values
Browse files Browse the repository at this point in the history
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: tc39#2850
  • Loading branch information
ptomato committed Sep 28, 2022
1 parent 1171cb4 commit 95efcfb
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,10 @@

/*---
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: |
FinalizationRegistry.prototype.register ( target , holdings [, unregisterToken ] )
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.
...
FinalizationRegistry.prototype.register ( _target_ , _holdings_ [, _unregisterToken_ ] )
3. If CanBeHeldWeakly(_target_) is *false*, throw a *TypeError* exception.
features: [FinalizationRegistry]
---*/

Expand Down Expand Up @@ -46,7 +38,7 @@ assert.throws(TypeError, function() {
finalizationRegistry.register('object');
}, 'string');

var s = Symbol();
var s = Symbol.for('registered symbol');
assert.throws(TypeError, function() {
finalizationRegistry.register(s);
}, 'symbol');
}, 'registered symbol');
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@

/*---
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: |
FinalizationRegistry.prototype.register ( target , holdings [, unregisterToken ] )
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.
...
FinalizationRegistry.prototype.register ( _target_ , _holdings_ [, _unregisterToken_ ] )
5. If CanBeHeldWeakly(_unregisterToken_) is *false*, then
a. If _unregisterToken_ is not *undefined*, throw a *TypeError* exception.
features: [FinalizationRegistry]
---*/

Expand Down Expand Up @@ -42,7 +38,7 @@ assert.throws(TypeError, function() {
finalizationRegistry.register(target, undefined, 'object');
}, 'string');

var s = Symbol();
var s = Symbol.for('registered symbol');
assert.throws(TypeError, function() {
finalizationRegistry.register(target, undefined, s);
}, 'symbol');
}, 'registered symbol');
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@

/*---
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: |
FinalizationRegistry.prototype.unregister ( unregisterToken )
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.
...
FinalizationRegistry.prototype.unregister ( _unregisterToken_ )
3. If CanBeHeldWeakly(_unregisterToken_) is *false*, throw a *TypeError* exception.
features: [FinalizationRegistry]
---*/

Expand Down Expand Up @@ -43,7 +38,7 @@ assert.throws(TypeError, function() {
finalizationRegistry.unregister('object');
}, 'string');

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

Expand All @@ -36,8 +36,8 @@ 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() {
new WeakMap([undefined, 1]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
/*---
esid: sec-weakmap.prototype.delete
description: >
Return false if value is not an Object.
Return false if the key cannot be held weakly.
info: |
WeakMap.prototype.delete ( value )
5. If Type(key) is not Object, return false.
WeakMap.prototype.delete ( _key_ )
5. If CanBeHeldWeakly(_key_) is *false*, return *false*.
features: [Symbol, WeakMap]
---*/

Expand All @@ -19,4 +18,4 @@ assert.sameValue(map.delete(NaN), false);
assert.sameValue(map.delete(null), false);
assert.sameValue(map.delete(undefined), false);
assert.sameValue(map.delete(true), false);
assert.sameValue(map.delete(Symbol()), false);
assert.sameValue(map.delete(Symbol.for('registered symbol')), false, 'registered symbol');
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@
/*---
esid: sec-weakmap.prototype.get
description: >
Returns undefined when key is not an Object.
Returns undefined when key cannot be held weakly.
info: |
WeakMap.prototype.get ( key )
...
4. Let entries be the List that is the value of M’s [[WeakMapData]] internal
slot.
5. If Type(key) is not Object, return undefined.
...
WeakMap.prototype.get ( _key_ )
4. If CanBeHeldWeakly(_key_) is *false*, return *undefined*.
features: [Symbol, WeakMap]
---*/

Expand All @@ -37,6 +32,6 @@ assert.sameValue(
);

assert.sameValue(
map.get(Symbol()), undefined,
'Returns undefined if key is a Symbol'
map.get(Symbol.for('registered symbol')), undefined,
'Returns undefined if key is a registered Symbol'
);
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
/*---
esid: sec-weakmap.prototype.has
description: >
Returns false if value is not an Object.
Returns false if the key cannot be held weakly
info: |
WeakMap.prototype.has ( value )
5. If Type(key) is not Object, return false.
WeakMap.prototype.has ( _key_ )
4. If CanBeHeldWeakly(_key_) is *false*, return *false*.
features: [Symbol, WeakMap]
---*/

Expand All @@ -18,4 +17,4 @@ assert.sameValue(map.has(''), false);
assert.sameValue(map.has(null), false);
assert.sameValue(map.has(undefined), 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');
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-weakmap.prototype.set
description: Throws TypeError if `key` is not Object.
description: Throws TypeError if key cannot be held weakly.
info: |
WeakMap.prototype.set ( key, value )
5. If Type(key) is not Object, throw a TypeError exception.
WeakMap.prototype.set ( _key_, _value_ )
4. If CanBeHeldWeakly(_key_) is *false*, throw a *TypeError* exception.
features: [Symbol, WeakMap]
---*/

Expand All @@ -33,5 +32,5 @@ 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');
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
/*---
esid: sec-weak-ref-target
description: >
Throws a TypeError if target is not Object
Throws a TypeError if target cannot be held weakly
info: |
WeakRef ( target )
1. If NewTarget is undefined, throw a TypeError exception.
2. If Type(target) is not Object, throw a TypeError exception.
...
WeakRef ( _target_ )
2. If CanBeHeldWeakly(_target_) is *false*, throw a *TypeError* exception.
features: [WeakRef]
---*/

Expand Down Expand Up @@ -39,10 +36,10 @@ assert.throws(TypeError, function() {
new WeakRef('Object');
}, 'string');

var s = Symbol();
var s = Symbol.for('registered symbol');
assert.throws(TypeError, function() {
new WeakRef(s);
}, 'symbol');
}, 'registered symbol');

assert.throws(TypeError, function() {
new WeakRef(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-weakset.prototype.add
description: Throws TypeError if `value` is not Object.
description: Throws TypeError if value cannot be held weakly.
info: |
WeakSet.prototype.add ( value )
4. If Type(value) is not Object, throw a TypeError exception.
WeakSet.prototype.add ( _value_ )
3. If CanBeHeldWeakly(_value_) is *false*, throw a *TypeError* exception.
features: [Symbol, WeakSet]
---*/

Expand All @@ -33,5 +32,5 @@ assert.throws(TypeError, function() {
});

assert.throws(TypeError, function() {
s.add(Symbol());
});
s.add(Symbol.for('registered symbol'));
}, 'Registered symbol not allowed as WeakSet value');
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
/*---
esid: sec-weakset.prototype.delete
description: >
Return false if value is not a non-null Object.
Return false if value cannot be held weakly.
info: |
WeakSet.prototype.delete ( value )
4. If Type(value) is not Object, return false.
WeakSet.prototype.delete ( _value_ )
3. If CanBeHeldWeakly(_value_) is *false*, return *false*.
features: [Symbol, WeakSet]
---*/

Expand All @@ -18,4 +17,4 @@ assert.sameValue(s.delete(''), false);
assert.sameValue(s.delete(null), false);
assert.sameValue(s.delete(undefined), 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');
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
/*---
esid: sec-weakset.prototype.has
description: >
Returns false if value is not a non-null Object.
Returns false if value cannot be held weakly.
info: |
WeakSet.prototype.has ( value )
5. If Type(value) is not Object, return false.
WeakSet.prototype.has ( _value_ )
4. If CanBeHeldWeakly(_value_) is *false*, return *false*.
features: [Symbol, WeakSet]
---*/

Expand All @@ -18,4 +17,4 @@ assert.sameValue(s.has(''), false);
assert.sameValue(s.has(null), false);
assert.sameValue(s.has(undefined), 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');

0 comments on commit 95efcfb

Please sign in to comment.