webpack/lib/util/WeakTupleMap.js

215 lines
4.0 KiB
JavaScript
Raw Normal View History

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
2024-02-22 22:20:17 +08:00
/**
* @template T
2024-02-22 22:20:17 +08:00
* @template V
2025-04-04 21:38:51 +08:00
* @typedef {Map<EXPECTED_ANY, WeakTupleMap<T[], V>>} M
2024-02-22 22:20:17 +08:00
*/
2025-04-04 21:38:51 +08:00
2024-02-22 22:20:17 +08:00
/**
* @template T
2024-02-22 22:20:17 +08:00
* @template V
2025-04-04 21:38:51 +08:00
* @typedef {WeakMap<EXPECTED_OBJECT, WeakTupleMap<T[], V>>} W
2024-02-22 22:20:17 +08:00
*/
/**
2025-03-07 21:12:22 +08:00
* @param {EXPECTED_ANY} thing thing
2024-02-22 22:20:17 +08:00
* @returns {boolean} true if is weak
*/
const isWeakKey = thing => typeof thing === "object" && thing !== null;
/**
* @template {any[]} T
* @template V
*/
class WeakTupleMap {
constructor() {
2021-09-27 22:31:42 +08:00
/** @private */
this.f = 0;
2024-02-22 22:20:17 +08:00
/**
* @private
* @type {V | undefined}
2024-06-11 20:32:02 +08:00
*/
2021-09-27 22:31:42 +08:00
this.v = undefined;
2024-02-22 22:20:17 +08:00
/**
* @private
* @type {M<T, V> | undefined}
2024-06-11 20:32:02 +08:00
*/
2021-09-27 22:31:42 +08:00
this.m = undefined;
2024-02-22 22:20:17 +08:00
/**
* @private
* @type {W<T, V> | undefined}
2024-06-11 20:32:02 +08:00
*/
2021-09-27 22:31:42 +08:00
this.w = undefined;
}
/**
* @param {[...T, V]} args tuple
* @returns {void}
*/
set(...args) {
2021-09-27 22:31:42 +08:00
/** @type {WeakTupleMap<T, V>} */
let node = this;
for (let i = 0; i < args.length - 1; i++) {
2021-09-27 22:31:42 +08:00
node = node._get(args[i]);
}
2021-09-27 22:31:42 +08:00
node._setValue(args[args.length - 1]);
}
/**
* @param {T} args tuple
* @returns {boolean} true, if the tuple is in the Set
*/
has(...args) {
2024-02-22 22:20:17 +08:00
/** @type {WeakTupleMap<T, V> | undefined} */
2021-09-27 22:31:42 +08:00
let node = this;
for (let i = 0; i < args.length; i++) {
2021-09-27 22:31:42 +08:00
node = node._peek(args[i]);
if (node === undefined) return false;
}
2021-09-27 22:31:42 +08:00
return node._hasValue();
}
/**
* @param {T} args tuple
2024-02-22 22:20:17 +08:00
* @returns {V | undefined} the value
*/
get(...args) {
2024-02-22 22:20:17 +08:00
/** @type {WeakTupleMap<T, V> | undefined} */
2021-09-27 22:31:42 +08:00
let node = this;
for (let i = 0; i < args.length; i++) {
2021-09-27 22:31:42 +08:00
node = node._peek(args[i]);
2024-08-02 02:36:27 +08:00
if (node === undefined) return;
}
2021-09-27 22:31:42 +08:00
return node._getValue();
}
/**
* @param {[...T, () => V]} args tuple
* @returns {V} the value
*/
provide(...args) {
2021-09-27 22:31:42 +08:00
/** @type {WeakTupleMap<T, V>} */
let node = this;
for (let i = 0; i < args.length - 1; i++) {
2021-09-27 22:31:42 +08:00
node = node._get(args[i]);
}
if (node._hasValue()) return /** @type {V} */ (node._getValue());
const fn = args[args.length - 1];
const newValue = fn(...args.slice(0, -1));
2021-09-27 22:31:42 +08:00
node._setValue(newValue);
return newValue;
}
/**
* @param {T} args tuple
* @returns {void}
*/
delete(...args) {
2024-02-22 22:20:17 +08:00
/** @type {WeakTupleMap<T, V> | undefined} */
2021-09-27 22:31:42 +08:00
let node = this;
for (let i = 0; i < args.length; i++) {
2021-09-27 22:31:42 +08:00
node = node._peek(args[i]);
if (node === undefined) return;
}
2021-09-27 22:31:42 +08:00
node._deleteValue();
}
/**
* @returns {void}
*/
clear() {
2021-09-27 22:31:42 +08:00
this.f = 0;
this.v = undefined;
this.w = undefined;
this.m = undefined;
}
_getValue() {
return this.v;
}
_hasValue() {
return (this.f & 1) === 1;
}
2024-02-22 22:20:17 +08:00
/**
* @param {V} v value
2024-02-22 22:20:17 +08:00
* @private
*/
2021-09-27 22:31:42 +08:00
_setValue(v) {
this.f |= 1;
this.v = v;
}
_deleteValue() {
this.f &= 6;
this.v = undefined;
}
2024-02-22 22:20:17 +08:00
/**
* @param {EXPECTED_ANY} thing thing
2024-02-22 22:20:17 +08:00
* @returns {WeakTupleMap<T, V> | undefined} thing
* @private
*/
2021-09-27 22:31:42 +08:00
_peek(thing) {
if (isWeakKey(thing)) {
2024-08-02 02:36:27 +08:00
if ((this.f & 4) !== 4) return;
2024-02-22 22:20:17 +08:00
return /** @type {W<T, V>} */ (this.w).get(thing);
2021-09-27 22:31:42 +08:00
}
2024-08-02 02:36:27 +08:00
if ((this.f & 2) !== 2) return;
2024-07-31 04:21:27 +08:00
return /** @type {M<T, V>} */ (this.m).get(thing);
2021-09-27 22:31:42 +08:00
}
2024-02-22 22:20:17 +08:00
/**
* @private
* @param {EXPECTED_ANY} thing thing
2024-02-22 22:20:17 +08:00
* @returns {WeakTupleMap<T, V>} value
*/
2021-09-27 22:31:42 +08:00
_get(thing) {
if (isWeakKey(thing)) {
if ((this.f & 4) !== 4) {
const newMap = new WeakMap();
this.f |= 4;
const newNode = new WeakTupleMap();
(this.w = newMap).set(thing, newNode);
return newNode;
}
2024-02-22 22:20:17 +08:00
const entry =
/** @type {W<T, V>} */
(this.w).get(thing);
2021-09-27 22:31:42 +08:00
if (entry !== undefined) {
return entry;
}
const newNode = new WeakTupleMap();
2024-02-22 22:20:17 +08:00
/** @type {W<T, V>} */
(this.w).set(thing, newNode);
2021-09-27 22:31:42 +08:00
return newNode;
2024-07-31 04:21:27 +08:00
}
if ((this.f & 2) !== 2) {
const newMap = new Map();
this.f |= 2;
2021-09-27 22:31:42 +08:00
const newNode = new WeakTupleMap();
2024-07-31 04:21:27 +08:00
(this.m = newMap).set(thing, newNode);
2021-09-27 22:31:42 +08:00
return newNode;
}
2024-07-31 04:21:27 +08:00
const entry =
/** @type {M<T, V>} */
(this.m).get(thing);
if (entry !== undefined) {
return entry;
}
const newNode = new WeakTupleMap();
/** @type {M<T, V>} */
(this.m).set(thing, newNode);
return newNode;
}
}
module.exports = WeakTupleMap;