summaryrefslogtreecommitdiff
path: root/ext/node/polyfills/internal/util.mjs
diff options
context:
space:
mode:
Diffstat (limited to 'ext/node/polyfills/internal/util.mjs')
-rw-r--r--ext/node/polyfills/internal/util.mjs36
1 files changed, 36 insertions, 0 deletions
diff --git a/ext/node/polyfills/internal/util.mjs b/ext/node/polyfills/internal/util.mjs
index 596599859..e6b32d17d 100644
--- a/ext/node/polyfills/internal/util.mjs
+++ b/ext/node/polyfills/internal/util.mjs
@@ -15,6 +15,10 @@ import {
} from "ext:deno_node/internal/primordials.mjs";
import { ERR_UNKNOWN_SIGNAL } from "ext:deno_node/internal/errors.ts";
import { os } from "ext:deno_node/internal_binding/constants.ts";
+import { primordials } from "ext:core/mod.js";
+const {
+ SafeWeakRef,
+} = primordials;
export const customInspectSymbol = Symbol.for("nodejs.util.inspect.custom");
export const kEnumerableProperty = Object.create(null);
@@ -135,6 +139,38 @@ export function convertToValidSignal(signal) {
throw new ERR_UNKNOWN_SIGNAL(signal);
}
+export class WeakReference {
+ #weak = null;
+ #strong = null;
+ #refCount = 0;
+ constructor(object) {
+ this.#weak = new SafeWeakRef(object);
+ }
+
+ incRef() {
+ this.#refCount++;
+ if (this.#refCount === 1) {
+ const derefed = this.#weak.deref();
+ if (derefed !== undefined) {
+ this.#strong = derefed;
+ }
+ }
+ return this.#refCount;
+ }
+
+ decRef() {
+ this.#refCount--;
+ if (this.#refCount === 0) {
+ this.#strong = null;
+ }
+ return this.#refCount;
+ }
+
+ get() {
+ return this.#weak.deref();
+ }
+}
+
promisify.custom = kCustomPromisifiedSymbol;
export default {