iterable-with-symbol-keys.js (1765B)
1 // |reftest| shell-option(--enable-symbols-as-weakmap-keys) skip-if(!xulRuntime.shell) -- requires shell-options 2 // Copyright (C) 2022 Igalia, S.L. All rights reserved. 3 // This code is governed by the BSD license found in the LICENSE file. 4 /*--- 5 esid: sec-weakmap-iterable 6 description: > 7 Returns the new WeakMap adding entries from the iterable parameter, with 8 Symbol keys. 9 info: | 10 WeakMap ( [ _iterable_ ] ) 11 5. Let _adder_ be ? Get(_map_, *"set"*). 12 6. Return ? AddEntriesFromIterable(_map_, _iterable_, _adder_). 13 14 AddEntriesFromIterable: 15 3. Repeat, 16 i. Let _status_ be Completion(Call(_adder_, _target_, « _k_, _v_ »)). 17 18 WeakMap.prototype.set( _key_, _value_ ): 19 6. Let _p_ be the Record {[[Key]]: _key_, [[Value]]: _value_}. 20 7. Append _p_ as the last element of _entries_. 21 features: [Symbol, WeakMap, symbols-as-weakmap-keys] 22 ---*/ 23 24 var sym = Symbol('a description'); 25 var results = []; 26 var set = WeakMap.prototype.set; 27 WeakMap.prototype.set = function(key, value) { 28 results.push({ 29 _this: this, 30 key: key, 31 value: value 32 }); 33 return set.call(this, key, value); 34 }; 35 var map = new WeakMap([ 36 [sym, 42], 37 [Symbol.hasInstance, 43], 38 ]); 39 40 assert.sameValue(results.length, 2, 'Called set() for each entry'); 41 assert.sameValue(results[0].key, sym, 'Adds object in order - first key, regular symbol'); 42 assert.sameValue(results[0].value, 42, 'Adds object in order - first value'); 43 assert.sameValue(results[0]._this, map, 'Adds object in order - this'); 44 assert.sameValue(results[1].key, Symbol.hasInstance, 'Adds object in order - second key, well-known symbol'); 45 assert.sameValue(results[1].value, 43, 'Adds object in order - second value'); 46 assert.sameValue(results[1]._this, map, 'Adds object in order - this'); 47 48 reportCompare(0, 0);