Skip to content

Commit 78343bb

Browse files
ExE-Bossaduh95
authored andcommitted
lib: add WeakRef and FinalizationRegistry to primordials
Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com> PR-URL: nodejs#37263 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 9498e97 commit 78343bb

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

lib/.eslintrc.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ rules:
4848
- prepareStackTrace
4949
- stackTraceLimit
5050
- name: EvalError
51+
- name: FinalizationRegistry
52+
into: Safe
5153
- name: Float32Array
5254
- name: Float64Array
5355
- name: Function
@@ -86,6 +88,8 @@ rules:
8688
- name: URIError
8789
- name: WeakMap
8890
into: Safe
91+
- name: WeakRef
92+
into: Safe
8993
- name: WeakSet
9094
into: Safe
9195
globals:

lib/internal/event_target.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ const {
1414
ObjectGetOwnPropertyDescriptors,
1515
ReflectApply,
1616
SafeArrayIterator,
17+
SafeFinalizationRegistry,
1718
SafeMap,
1819
SafeWeakMap,
20+
SafeWeakRef,
1921
SafeWeakSet,
2022
String,
2123
Symbol,
@@ -201,7 +203,7 @@ let weakListenersState = null;
201203
// get garbage collected now that it's weak.
202204
let objectToWeakListenerMap = null;
203205
function weakListeners() {
204-
weakListenersState ??= new globalThis.FinalizationRegistry(
206+
weakListenersState ??= new SafeFinalizationRegistry(
205207
(listener) => listener.remove()
206208
);
207209
objectToWeakListenerMap ??= new SafeWeakMap();
@@ -232,7 +234,7 @@ class Listener {
232234
this.weak = Boolean(weak); // Don't retain the object
233235

234236
if (this.weak) {
235-
this.callback = new globalThis.WeakRef(listener);
237+
this.callback = new SafeWeakRef(listener);
236238
weakListeners().registry.register(listener, this, this);
237239
// Make the retainer retain the listener in a WeakMap
238240
weakListeners().map.set(weak, listener);

lib/internal/per_context/primordials.js

+20
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ function copyPrototype(src, dest, prefix) {
163163
'Date',
164164
'Error',
165165
'EvalError',
166+
'FinalizationRegistry',
166167
'Float32Array',
167168
'Float64Array',
168169
'Function',
@@ -186,6 +187,7 @@ function copyPrototype(src, dest, prefix) {
186187
'Uint8Array',
187188
'Uint8ClampedArray',
188189
'WeakMap',
190+
'WeakRef',
189191
'WeakSet',
190192
].forEach((name) => {
191193
const original = global[name];
@@ -229,13 +231,15 @@ function copyPrototype(src, dest, prefix) {
229231

230232
const {
231233
ArrayPrototypeForEach,
234+
FinalizationRegistry,
232235
FunctionPrototypeCall,
233236
Map,
234237
ObjectFreeze,
235238
ObjectSetPrototypeOf,
236239
Set,
237240
SymbolIterator,
238241
WeakMap,
242+
WeakRef,
239243
WeakSet,
240244
} = primordials;
241245

@@ -334,6 +338,7 @@ primordials.SafeWeakMap = makeSafe(
334338
constructor(i) { super(i); } // eslint-disable-line no-useless-constructor
335339
}
336340
);
341+
337342
primordials.SafeSet = makeSafe(
338343
Set,
339344
class SafeSet extends Set {
@@ -347,5 +352,20 @@ primordials.SafeWeakSet = makeSafe(
347352
}
348353
);
349354

355+
primordials.SafeFinalizationRegistry = makeSafe(
356+
FinalizationRegistry,
357+
class SafeFinalizationRegistry extends FinalizationRegistry {
358+
// eslint-disable-next-line no-useless-constructor
359+
constructor(cleanupCallback) { super(cleanupCallback); }
360+
}
361+
);
362+
primordials.SafeWeakRef = makeSafe(
363+
WeakRef,
364+
class SafeWeakRef extends WeakRef {
365+
// eslint-disable-next-line no-useless-constructor
366+
constructor(target) { super(target); }
367+
}
368+
);
369+
350370
ObjectSetPrototypeOf(primordials, null);
351371
ObjectFreeze(primordials);

lib/internal/util/iterable_weak_map.js

+2-13
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,14 @@
11
'use strict';
22

33
const {
4-
makeSafe,
54
ObjectFreeze,
5+
SafeFinalizationRegistry,
66
SafeSet,
77
SafeWeakMap,
8+
SafeWeakRef,
89
SymbolIterator,
910
} = primordials;
1011

11-
// TODO(aduh95): Add FinalizationRegistry to primordials
12-
const SafeFinalizationRegistry = makeSafe(
13-
globalThis.FinalizationRegistry,
14-
class SafeFinalizationRegistry extends globalThis.FinalizationRegistry {}
15-
);
16-
17-
// TODO(aduh95): Add WeakRef to primordials
18-
const SafeWeakRef = makeSafe(
19-
globalThis.WeakRef,
20-
class SafeWeakRef extends globalThis.WeakRef {}
21-
);
22-
2312
// This class is modified from the example code in the WeakRefs specification:
2413
// https://github.com/tc39/proposal-weakrefs
2514
// Licensed under ECMA's MIT-style license, see:

0 commit comments

Comments
 (0)