11cb0ef41Sopenharmony_ci// Flags: --expose-gc --expose-internals 21cb0ef41Sopenharmony_ci'use strict'; 31cb0ef41Sopenharmony_ci 41cb0ef41Sopenharmony_ciconst common = require('../common'); 51cb0ef41Sopenharmony_ciconst { deepStrictEqual, strictEqual } = require('assert'); 61cb0ef41Sopenharmony_ciconst { IterableWeakMap } = require('internal/util/iterable_weak_map'); 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ci// Ensures iterating over the map does not rely on methods which can be 91cb0ef41Sopenharmony_ci// mutated by users. 101cb0ef41Sopenharmony_ciReflect.getPrototypeOf(function*() {}).prototype.next = common.mustNotCall(); 111cb0ef41Sopenharmony_ciReflect.getPrototypeOf(new Set()[Symbol.iterator]()).next = 121cb0ef41Sopenharmony_ci common.mustNotCall(); 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ci// It drops entry if a reference is no longer held. 151cb0ef41Sopenharmony_ci{ 161cb0ef41Sopenharmony_ci const wm = new IterableWeakMap(); 171cb0ef41Sopenharmony_ci const _cache = { 181cb0ef41Sopenharmony_ci moduleA: {}, 191cb0ef41Sopenharmony_ci moduleB: {}, 201cb0ef41Sopenharmony_ci moduleC: {}, 211cb0ef41Sopenharmony_ci }; 221cb0ef41Sopenharmony_ci wm.set(_cache.moduleA, 'hello'); 231cb0ef41Sopenharmony_ci wm.set(_cache.moduleB, 'discard'); 241cb0ef41Sopenharmony_ci wm.set(_cache.moduleC, 'goodbye'); 251cb0ef41Sopenharmony_ci delete _cache.moduleB; 261cb0ef41Sopenharmony_ci setImmediate(() => { 271cb0ef41Sopenharmony_ci _cache; // eslint-disable-line no-unused-expressions 281cb0ef41Sopenharmony_ci globalThis.gc(); 291cb0ef41Sopenharmony_ci const values = [...wm]; 301cb0ef41Sopenharmony_ci deepStrictEqual(values, ['hello', 'goodbye']); 311cb0ef41Sopenharmony_ci }); 321cb0ef41Sopenharmony_ci} 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ci// It updates an existing entry, if the same key is provided twice. 351cb0ef41Sopenharmony_ci{ 361cb0ef41Sopenharmony_ci const wm = new IterableWeakMap(); 371cb0ef41Sopenharmony_ci const _cache = { 381cb0ef41Sopenharmony_ci moduleA: {}, 391cb0ef41Sopenharmony_ci moduleB: {}, 401cb0ef41Sopenharmony_ci }; 411cb0ef41Sopenharmony_ci wm.set(_cache.moduleA, 'hello'); 421cb0ef41Sopenharmony_ci wm.set(_cache.moduleB, 'goodbye'); 431cb0ef41Sopenharmony_ci wm.set(_cache.moduleB, 'goodnight'); 441cb0ef41Sopenharmony_ci const values = [...wm]; 451cb0ef41Sopenharmony_ci deepStrictEqual(values, ['hello', 'goodnight']); 461cb0ef41Sopenharmony_ci} 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ci// It allows entry to be deleted by key. 491cb0ef41Sopenharmony_ci{ 501cb0ef41Sopenharmony_ci const wm = new IterableWeakMap(); 511cb0ef41Sopenharmony_ci const _cache = { 521cb0ef41Sopenharmony_ci moduleA: {}, 531cb0ef41Sopenharmony_ci moduleB: {}, 541cb0ef41Sopenharmony_ci moduleC: {}, 551cb0ef41Sopenharmony_ci }; 561cb0ef41Sopenharmony_ci wm.set(_cache.moduleA, 'hello'); 571cb0ef41Sopenharmony_ci wm.set(_cache.moduleB, 'discard'); 581cb0ef41Sopenharmony_ci wm.set(_cache.moduleC, 'goodbye'); 591cb0ef41Sopenharmony_ci wm.delete(_cache.moduleB); 601cb0ef41Sopenharmony_ci const values = [...wm]; 611cb0ef41Sopenharmony_ci deepStrictEqual(values, ['hello', 'goodbye']); 621cb0ef41Sopenharmony_ci} 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ci// It handles delete for key that does not exist. 651cb0ef41Sopenharmony_ci{ 661cb0ef41Sopenharmony_ci const wm = new IterableWeakMap(); 671cb0ef41Sopenharmony_ci const _cache = { 681cb0ef41Sopenharmony_ci moduleA: {}, 691cb0ef41Sopenharmony_ci moduleB: {}, 701cb0ef41Sopenharmony_ci moduleC: {}, 711cb0ef41Sopenharmony_ci }; 721cb0ef41Sopenharmony_ci wm.set(_cache.moduleA, 'hello'); 731cb0ef41Sopenharmony_ci wm.set(_cache.moduleC, 'goodbye'); 741cb0ef41Sopenharmony_ci wm.delete(_cache.moduleB); 751cb0ef41Sopenharmony_ci const values = [...wm]; 761cb0ef41Sopenharmony_ci deepStrictEqual(values, ['hello', 'goodbye']); 771cb0ef41Sopenharmony_ci} 781cb0ef41Sopenharmony_ci 791cb0ef41Sopenharmony_ci// It allows an entry to be fetched by key. 801cb0ef41Sopenharmony_ci{ 811cb0ef41Sopenharmony_ci const wm = new IterableWeakMap(); 821cb0ef41Sopenharmony_ci const _cache = { 831cb0ef41Sopenharmony_ci moduleA: {}, 841cb0ef41Sopenharmony_ci moduleB: {}, 851cb0ef41Sopenharmony_ci moduleC: {}, 861cb0ef41Sopenharmony_ci }; 871cb0ef41Sopenharmony_ci wm.set(_cache.moduleA, 'hello'); 881cb0ef41Sopenharmony_ci wm.set(_cache.moduleB, 'discard'); 891cb0ef41Sopenharmony_ci wm.set(_cache.moduleC, 'goodbye'); 901cb0ef41Sopenharmony_ci strictEqual(wm.get(_cache.moduleB), 'discard'); 911cb0ef41Sopenharmony_ci} 921cb0ef41Sopenharmony_ci 931cb0ef41Sopenharmony_ci// It returns true for has() if key exists. 941cb0ef41Sopenharmony_ci{ 951cb0ef41Sopenharmony_ci const wm = new IterableWeakMap(); 961cb0ef41Sopenharmony_ci const _cache = { 971cb0ef41Sopenharmony_ci moduleA: {}, 981cb0ef41Sopenharmony_ci }; 991cb0ef41Sopenharmony_ci wm.set(_cache.moduleA, 'hello'); 1001cb0ef41Sopenharmony_ci strictEqual(wm.has(_cache.moduleA), true); 1011cb0ef41Sopenharmony_ci} 102