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 4 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
64 changes: 18 additions & 46 deletions lib/internal/per_context/primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@
FinalizationRegistry,
FunctionPrototypeCall,
Map,
ObjectEntries,
ObjectDefineProperties,
ObjectDefineProperty,
ObjectFreeze,
Expand Down Expand Up @@ -402,60 +403,31 @@
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
},
);
const safeClasses = {
__proto__: null,
SafeMap: Map,
SafeWeakMap: WeakMap,
SafeSet: Set,
SafeWeakSet: WeakSet,
SafeFinalizationRegistry: FinalizationRegistry,
SafeWeakRef: WeakRef,
SafePromise: Promise

Check failure on line 421 in lib/internal/per_context/primordials.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing trailing comma
};

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); }
},
);
for (const [safeName, unsafeClass] of ObjectEntries(safeClasses)) {

Check failure on line 424 in lib/internal/per_context/primordials.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Use object destructuring instead of array destructuring
primordials[safeName] = makeSafe(unsafeClass, class extends unsafeClass {});
}

const SafePromise = makeSafe(
Promise,
class SafePromise extends Promise {
// eslint-disable-next-line no-useless-constructor
constructor(executor) { super(executor); }
},
);
const {
SafePromise,
} = primordials;
Comment on lines +420 to +422
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that SafePromise used to not be on the primordials object, it was only used locally on that file; it probably makes sense to keep it that way.


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