mirror of
https://github.com/nodejs/node.git
synced 2026-11-09 15:11:11 -05:00
lib: add WeakRef and FinalizationRegistry to primordials
Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com> PR-URL: https://github.com/nodejs/node/pull/37263 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
1 parent
9498e97015
commit
78343bbdc5
4 files changed
+30
-15
No files matched your search
@@ -48,6 +48,8 @@ rules:
|
||||
- prepareStackTrace
|
||||
- stackTraceLimit
|
||||
- name: EvalError
|
||||
- name: FinalizationRegistry
|
||||
into: Safe
|
||||
- name: Float32Array
|
||||
- name: Float64Array
|
||||
- name: Function
|
||||
@@ -86,6 +88,8 @@ rules:
|
||||
- name: URIError
|
||||
- name: WeakMap
|
||||
into: Safe
|
||||
- name: WeakRef
|
||||
into: Safe
|
||||
- name: WeakSet
|
||||
into: Safe
|
||||
globals:
|
||||
|
||||
@@ -14,8 +14,10 @@ const {
|
||||
ObjectGetOwnPropertyDescriptors,
|
||||
ReflectApply,
|
||||
SafeArrayIterator,
|
||||
SafeFinalizationRegistry,
|
||||
SafeMap,
|
||||
SafeWeakMap,
|
||||
SafeWeakRef,
|
||||
SafeWeakSet,
|
||||
String,
|
||||
Symbol,
|
||||
@@ -201,7 +203,7 @@ let weakListenersState = null;
|
||||
// get garbage collected now that it's weak.
|
||||
let objectToWeakListenerMap = null;
|
||||
function weakListeners() {
|
||||
weakListenersState ??= new globalThis.FinalizationRegistry(
|
||||
weakListenersState ??= new SafeFinalizationRegistry(
|
||||
(listener) => listener.remove()
|
||||
);
|
||||
objectToWeakListenerMap ??= new SafeWeakMap();
|
||||
@@ -232,7 +234,7 @@ class Listener {
|
||||
this.weak = Boolean(weak); // Don't retain the object
|
||||
|
||||
if (this.weak) {
|
||||
this.callback = new globalThis.WeakRef(listener);
|
||||
this.callback = new SafeWeakRef(listener);
|
||||
weakListeners().registry.register(listener, this, this);
|
||||
// Make the retainer retain the listener in a WeakMap
|
||||
weakListeners().map.set(weak, listener);
|
||||
|
||||
@@ -163,6 +163,7 @@ function copyPrototype(src, dest, prefix) {
|
||||
'Date',
|
||||
'Error',
|
||||
'EvalError',
|
||||
'FinalizationRegistry',
|
||||
'Float32Array',
|
||||
'Float64Array',
|
||||
'Function',
|
||||
@@ -186,6 +187,7 @@ function copyPrototype(src, dest, prefix) {
|
||||
'Uint8Array',
|
||||
'Uint8ClampedArray',
|
||||
'WeakMap',
|
||||
'WeakRef',
|
||||
'WeakSet',
|
||||
].forEach((name) => {
|
||||
const original = global[name];
|
||||
@@ -229,6 +231,7 @@ function copyPrototype(src, dest, prefix) {
|
||||
|
||||
const {
|
||||
ArrayPrototypeForEach,
|
||||
FinalizationRegistry,
|
||||
FunctionPrototypeCall,
|
||||
Map,
|
||||
ObjectFreeze,
|
||||
@@ -236,6 +239,7 @@ const {
|
||||
Set,
|
||||
SymbolIterator,
|
||||
WeakMap,
|
||||
WeakRef,
|
||||
WeakSet,
|
||||
} = primordials;
|
||||
|
||||
@@ -334,6 +338,7 @@ primordials.SafeWeakMap = makeSafe(
|
||||
constructor(i) { super(i); } // eslint-disable-line no-useless-constructor
|
||||
}
|
||||
);
|
||||
|
||||
primordials.SafeSet = makeSafe(
|
||||
Set,
|
||||
class SafeSet extends Set {
|
||||
@@ -347,5 +352,20 @@ primordials.SafeWeakSet = makeSafe(
|
||||
}
|
||||
);
|
||||
|
||||
primordials.SafeFinalizationRegistry = makeSafe(
|
||||
FinalizationRegistry,
|
||||
class SafeFinalizationRegistry extends FinalizationRegistry {
|
||||
// eslint-disable-next-line no-useless-constructor
|
||||
constructor(cleanupCallback) { super(cleanupCallback); }
|
||||
}
|
||||
);
|
||||
primordials.SafeWeakRef = makeSafe(
|
||||
WeakRef,
|
||||
class SafeWeakRef extends WeakRef {
|
||||
// eslint-disable-next-line no-useless-constructor
|
||||
constructor(target) { super(target); }
|
||||
}
|
||||
);
|
||||
|
||||
ObjectSetPrototypeOf(primordials, null);
|
||||
ObjectFreeze(primordials);
|
||||
@@ -1,25 +1,14 @@
|
||||
'use strict';
|
||||
|
||||
const {
|
||||
makeSafe,
|
||||
ObjectFreeze,
|
||||
SafeFinalizationRegistry,
|
||||
SafeSet,
|
||||
SafeWeakMap,
|
||||
SafeWeakRef,
|
||||
SymbolIterator,
|
||||
} = primordials;
|
||||
|
||||
// TODO(aduh95): Add FinalizationRegistry to primordials
|
||||
const SafeFinalizationRegistry = makeSafe(
|
||||
globalThis.FinalizationRegistry,
|
||||
class SafeFinalizationRegistry extends globalThis.FinalizationRegistry {}
|
||||
);
|
||||
|
||||
// TODO(aduh95): Add WeakRef to primordials
|
||||
const SafeWeakRef = makeSafe(
|
||||
globalThis.WeakRef,
|
||||
class SafeWeakRef extends globalThis.WeakRef {}
|
||||
);
|
||||
|
||||
// This class is modified from the example code in the WeakRefs specification:
|
||||
// https://github.com/tc39/proposal-weakrefs
|
||||
// Licensed under ECMA's MIT-style license, see:
|
||||
|
||||
Reference in new issue
Block a user