test262/test/built-ins/WeakMap/iterator-items-keys-cannot-be-held-weakly.js
Philip Chimento 6291e42a72 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
2022-10-12 09:58:45 +02:00

51 lines
1.3 KiB
JavaScript

// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-weakmap-iterable
description: >
Throws a TypeError if keys in iterable items cannot be held weakly.
info: |
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]
---*/
assert.throws(TypeError, function() {
new WeakMap([1, 1]);
});
assert.throws(TypeError, function() {
new WeakMap(['', 1]);
});
assert.throws(TypeError, function() {
new WeakMap([true, 1]);
});
assert.throws(TypeError, function() {
new WeakMap([null, 1]);
});
assert.throws(TypeError, function() {
new WeakMap([Symbol.for('registered symbol'), 1]);
}, 'Registered symbol not allowed as a WeakMap key');
assert.throws(TypeError, function() {
new WeakMap([undefined, 1]);
});
assert.throws(TypeError, function() {
new WeakMap([
['a', 1], 2
]);
});