From 24d4d633089669dfab75b871b238051ef14aab7f 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 PR-URL: https://github.com/nodejs/node/pull/36016 Reviewed-By: Antoine du Hamel Reviewed-By: Benjamin Gruenbaum Reviewed-By: Shingo Inoue --- 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 5ba9f504441cc2..0ff52255feff41 100644 --- a/lib/internal/per_context/primordials.js +++ b/lib/internal/per_context/primordials.js @@ -171,5 +171,18 @@ primordials.SafeWeakSet = 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 fd3bef5d63effc..a82018da796bc8 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -58,10 +58,10 @@ const { SymbolPrototypeValueOf, SymbolIterator, SymbolToStringTag, + TypedArrayPrototype, Uint16Array, Uint32Array, Uint8Array, - Uint8ArrayPrototype, Uint8ClampedArray, uncurryThis, } = primordials; @@ -142,8 +142,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,