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: simplify primordial safe generation #52479

Closed
wants to merge 6 commits into from
Closed
Changes from 1 commit
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
62 changes: 16 additions & 46 deletions lib/internal/per_context/primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,60 +402,30 @@ const makeSafe = (unsafe, safe) => {
ObjectFreeze(safe);
return safe;
};

primordials.makeSafe = makeSafe;

// Subclass the constructors because we need to use their prototype
// methods later.
// Defining the `constructor` is necessary here to avoid the default
// constructor which uses the user-mutable `%ArrayIteratorPrototype%.next`.
primordials.SafeMap = makeSafe(
Map,
class SafeMap extends Map {
constructor(i) { super(i); } // eslint-disable-line no-useless-constructor
},
);
primordials.SafeWeakMap = makeSafe(
WeakMap,
class SafeWeakMap extends WeakMap {
constructor(i) { super(i); } // eslint-disable-line no-useless-constructor
},
);

primordials.SafeSet = makeSafe(
Set,
class SafeSet extends Set {
constructor(i) { super(i); } // eslint-disable-line no-useless-constructor
},
);
primordials.SafeWeakSet = makeSafe(
WeakSet,
class SafeWeakSet extends WeakSet {
constructor(i) { super(i); } // eslint-disable-line no-useless-constructor
},
);

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); }
},
);
const safeClasses = {
__proto__: null,
SafeMap: Map,
SafeWeakMap: WeakMap,
SafeSet: Set,
SafeWeakSet: WeakSet,
SafeFinalizationRegistry: FinalizationRegistry,
SafeWeakRef: WeakRef,
SafePromise: Promise
};

const SafePromise = makeSafe(
Promise,
class SafePromise extends Promise {
for (const [safeName, unsafeClass] of ObjectEntries(safeClasses)) {
primordials[safeName] = makeSafe(unsafeClass, class extends unsafeClass {
// eslint-disable-next-line no-useless-constructor
constructor(executor) { super(executor); }
},
);
constructor(...args) { super(...args); }
avivkeller marked this conversation as resolved.
Show resolved Hide resolved
});
}

/**
* Attaches a callback that is invoked when the Promise is settled (fulfilled or
Expand Down
Loading