11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst {
41cb0ef41Sopenharmony_ci  ObjectFreeze,
51cb0ef41Sopenharmony_ci  SafeFinalizationRegistry,
61cb0ef41Sopenharmony_ci  SafeSet,
71cb0ef41Sopenharmony_ci  SafeWeakMap,
81cb0ef41Sopenharmony_ci  SafeWeakRef,
91cb0ef41Sopenharmony_ci  SymbolIterator,
101cb0ef41Sopenharmony_ci} = primordials;
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci// This class is modified from the example code in the WeakRefs specification:
131cb0ef41Sopenharmony_ci// https://github.com/tc39/proposal-weakrefs
141cb0ef41Sopenharmony_ci// Licensed under ECMA's MIT-style license, see:
151cb0ef41Sopenharmony_ci// https://github.com/tc39/ecma262/blob/HEAD/LICENSE.md
161cb0ef41Sopenharmony_ciclass IterableWeakMap {
171cb0ef41Sopenharmony_ci  #weakMap = new SafeWeakMap();
181cb0ef41Sopenharmony_ci  #refSet = new SafeSet();
191cb0ef41Sopenharmony_ci  #finalizationGroup = new SafeFinalizationRegistry(cleanup);
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci  set(key, value) {
221cb0ef41Sopenharmony_ci    const entry = this.#weakMap.get(key);
231cb0ef41Sopenharmony_ci    if (entry) {
241cb0ef41Sopenharmony_ci      // If there's already an entry for the object represented by "key",
251cb0ef41Sopenharmony_ci      // the value can be updated without creating a new WeakRef:
261cb0ef41Sopenharmony_ci      this.#weakMap.set(key, { value, ref: entry.ref });
271cb0ef41Sopenharmony_ci    } else {
281cb0ef41Sopenharmony_ci      const ref = new SafeWeakRef(key);
291cb0ef41Sopenharmony_ci      this.#weakMap.set(key, { value, ref });
301cb0ef41Sopenharmony_ci      this.#refSet.add(ref);
311cb0ef41Sopenharmony_ci      this.#finalizationGroup.register(key, {
321cb0ef41Sopenharmony_ci        set: this.#refSet,
331cb0ef41Sopenharmony_ci        ref,
341cb0ef41Sopenharmony_ci      }, ref);
351cb0ef41Sopenharmony_ci    }
361cb0ef41Sopenharmony_ci  }
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  get(key) {
391cb0ef41Sopenharmony_ci    return this.#weakMap.get(key)?.value;
401cb0ef41Sopenharmony_ci  }
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  has(key) {
431cb0ef41Sopenharmony_ci    return this.#weakMap.has(key);
441cb0ef41Sopenharmony_ci  }
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  delete(key) {
471cb0ef41Sopenharmony_ci    const entry = this.#weakMap.get(key);
481cb0ef41Sopenharmony_ci    if (!entry) {
491cb0ef41Sopenharmony_ci      return false;
501cb0ef41Sopenharmony_ci    }
511cb0ef41Sopenharmony_ci    this.#weakMap.delete(key);
521cb0ef41Sopenharmony_ci    this.#refSet.delete(entry.ref);
531cb0ef41Sopenharmony_ci    this.#finalizationGroup.unregister(entry.ref);
541cb0ef41Sopenharmony_ci    return true;
551cb0ef41Sopenharmony_ci  }
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  [SymbolIterator]() {
581cb0ef41Sopenharmony_ci    const iterator = this.#refSet[SymbolIterator]();
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci    const next = () => {
611cb0ef41Sopenharmony_ci      const result = iterator.next();
621cb0ef41Sopenharmony_ci      if (result.done) return result;
631cb0ef41Sopenharmony_ci      const key = result.value.deref();
641cb0ef41Sopenharmony_ci      if (key == null) return next();
651cb0ef41Sopenharmony_ci      const { value } = this.#weakMap.get(key);
661cb0ef41Sopenharmony_ci      return { done: false, value };
671cb0ef41Sopenharmony_ci    };
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci    return {
701cb0ef41Sopenharmony_ci      [SymbolIterator]() { return this; },
711cb0ef41Sopenharmony_ci      next,
721cb0ef41Sopenharmony_ci    };
731cb0ef41Sopenharmony_ci  }
741cb0ef41Sopenharmony_ci}
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_cifunction cleanup({ set, ref }) {
771cb0ef41Sopenharmony_ci  set.delete(ref);
781cb0ef41Sopenharmony_ci}
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ciObjectFreeze(IterableWeakMap.prototype);
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_cimodule.exports = {
831cb0ef41Sopenharmony_ci  IterableWeakMap,
841cb0ef41Sopenharmony_ci};
85