Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: add WeakRef and FinalizationRegistry to primordials #37263

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/.eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ rules:
- prepareStackTrace
- stackTraceLimit
- name: EvalError
- name: FinalizationRegistry
into: Safe
- name: Float32Array
- name: Float64Array
- name: Function
Expand Down Expand Up @@ -86,6 +88,8 @@ rules:
- name: URIError
- name: WeakMap
into: Safe
- name: WeakRef
into: Safe
- name: WeakSet
into: Safe
globals:
Expand Down
6 changes: 4 additions & 2 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ const {
ObjectGetOwnPropertyDescriptors,
ReflectApply,
SafeArrayIterator,
SafeFinalizationRegistry,
SafeMap,
SafeWeakMap,
SafeWeakRef,
SafeWeakSet,
String,
Symbol,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
20 changes: 20 additions & 0 deletions lib/internal/per_context/primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ function copyPrototype(src, dest, prefix) {
'Date',
'Error',
'EvalError',
'FinalizationRegistry',
'Float32Array',
'Float64Array',
'Function',
Expand All @@ -186,6 +187,7 @@ function copyPrototype(src, dest, prefix) {
'Uint8Array',
'Uint8ClampedArray',
'WeakMap',
'WeakRef',
'WeakSet',
].forEach((name) => {
const original = global[name];
Expand Down Expand Up @@ -229,13 +231,15 @@ function copyPrototype(src, dest, prefix) {

const {
ArrayPrototypeForEach,
FinalizationRegistry,
FunctionPrototypeCall,
Map,
ObjectFreeze,
ObjectSetPrototypeOf,
Set,
SymbolIterator,
WeakMap,
WeakRef,
WeakSet,
} = primordials;

Expand Down Expand Up @@ -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 {
Expand All @@ -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);
15 changes: 2 additions & 13 deletions lib/internal/util/iterable_weak_map.js
Original file line number Diff line number Diff line change
@@ -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:
Expand Down