From 8fee9b7271339379628f41ba469e9633ae410a45 Mon Sep 17 00:00:00 2001 From: ExE Boss <3889017+ExE-Boss@users.noreply.github.com> Date: Sat, 7 Nov 2020 08:40:00 +0100 Subject: [PATCH] =?UTF-8?q?lib:=20add=C2=A0%TypedArray%=20abstract=C2=A0co?= =?UTF-8?q?nstructor=20to=C2=A0primordials?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refs: https://github.com/nodejs/node/pull/35448 Refs: https://github.com/nodejs/node/pull/36003 Refs: https://tc39.es/ecma262/#sec-%typedarray%-intrinsic-object Co-authored-by: Antoine du Hamel --- lib/internal/freeze_intrinsics.js | 6 ++++-- lib/internal/per_context/primordials.js | 13 +++++++++++++ lib/internal/util/inspect.js | 5 ++--- lib/internal/util/types.js | 5 +---- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/lib/internal/freeze_intrinsics.js b/lib/internal/freeze_intrinsics.js index 21612790ce749c..5e99af2d9ad0bb 100644 --- a/lib/internal/freeze_intrinsics.js +++ b/lib/internal/freeze_intrinsics.js @@ -65,6 +65,8 @@ const { SymbolIterator, SyntaxError, TypeError, + TypedArray, + TypedArrayPrototype, Uint16Array, Uint32Array, Uint8Array, @@ -105,7 +107,7 @@ module.exports = function() { // AsyncGeneratorFunction ObjectGetPrototypeOf(async function* () {}), // TypedArray - ObjectGetPrototypeOf(Uint8Array), + TypedArrayPrototype, // 19 Fundamental Objects Object.prototype, // 19.1 @@ -189,7 +191,7 @@ module.exports = function() { // AsyncGeneratorFunction ObjectGetPrototypeOf(async function* () {}), // TypedArray - ObjectGetPrototypeOf(Uint8Array), + TypedArray, // 18 The Global Object eval, diff --git a/lib/internal/per_context/primordials.js b/lib/internal/per_context/primordials.js index ddecb18861c258..e8e64baf0c6429 100644 --- a/lib/internal/per_context/primordials.js +++ b/lib/internal/per_context/primordials.js @@ -175,5 +175,18 @@ primordials.SafePromise = makeSafe( copyPrototype(original.prototype, primordials, `${name}Prototype`); }); +// Create copies of abstract intrinsic objects that are not directly exposed +// on the global object. +// Refs: https://tc39.es/ecma262/#sec-%typedarray%-intrinsic-object +[ + { name: 'TypedArray', original: Reflect.getPrototypeOf(Uint8Array) }, +].forEach(({ name, original }) => { + primordials[name] = original; + // The static %TypedArray% methods require a valid `this`, but can't be bound, + // as they need a subclass constructor as the receiver: + copyPrototype(original, primordials, name); + copyPrototype(original.prototype, primordials, `${name}Prototype`); +}); + Object.setPrototypeOf(primordials, null); Object.freeze(primordials); diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index e6787760fea392..4e1ed55c81cd98 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -57,10 +57,10 @@ const { SymbolPrototypeValueOf, SymbolIterator, SymbolToStringTag, + TypedArrayPrototype, Uint16Array, Uint32Array, Uint8Array, - Uint8ArrayPrototype, Uint8ClampedArray, uncurryThis, } = primordials; @@ -141,8 +141,7 @@ const setSizeGetter = uncurryThis( const mapSizeGetter = uncurryThis( ObjectGetOwnPropertyDescriptor(MapPrototype, 'size').get); const typedArraySizeGetter = uncurryThis( - ObjectGetOwnPropertyDescriptor( - ObjectGetPrototypeOf(Uint8ArrayPrototype), 'length').get); + ObjectGetOwnPropertyDescriptor(TypedArrayPrototype, 'length').get); let hexSlice; diff --git a/lib/internal/util/types.js b/lib/internal/util/types.js index 4b9eeb469b2d19..115c473ae221d4 100644 --- a/lib/internal/util/types.js +++ b/lib/internal/util/types.js @@ -3,14 +3,11 @@ const { ArrayBufferIsView, ObjectGetOwnPropertyDescriptor, - ObjectGetPrototypeOf, SymbolToStringTag, - Uint8ArrayPrototype, + TypedArrayPrototype, uncurryThis, } = primordials; -const TypedArrayPrototype = ObjectGetPrototypeOf(Uint8ArrayPrototype); - const TypedArrayProto_toStringTag = uncurryThis( ObjectGetOwnPropertyDescriptor(TypedArrayPrototype,