diff --git a/lib/internal/per_context/primordials.js b/lib/internal/per_context/primordials.js index 140c80566c9e6c..f6df6faa10e0ce 100644 --- a/lib/internal/per_context/primordials.js +++ b/lib/internal/per_context/primordials.js @@ -275,9 +275,7 @@ const { Array: ArrayConstructor, ArrayPrototypeForEach, ArrayPrototypeMap, - FinalizationRegistry, FunctionPrototypeCall, - Map, ObjectDefineProperties, ObjectDefineProperty, ObjectFreeze, @@ -301,7 +299,6 @@ const { RegExpPrototypeGetSource, RegExpPrototypeGetSticky, RegExpPrototypeGetUnicode, - Set, SymbolIterator, SymbolMatch, SymbolMatchAll, @@ -309,9 +306,6 @@ const { SymbolSearch, SymbolSpecies, SymbolSplit, - WeakMap, - WeakRef, - WeakSet, } = primordials; @@ -402,60 +396,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); } - }, -); +[ + 'Map', + 'WeakMap', + 'Set', + 'WeakSet', + 'FinalizationRegistry', + 'WeakRef', + 'Promise', +].forEach((unsafeClassName) => { + // eslint-disable-next-line no-restricted-globals + const unsafeClass = globalThis[unsafeClassName]; + primordials['Safe' + unsafeClassName] = 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; /** * Attaches a callback that is invoked when the Promise is settled (fulfilled or