From c2938bde6c6b9ed79005fa1f8e57c61f67c6b638 Mon Sep 17 00:00:00 2001 From: ExE Boss <3889017+ExE-Boss@users.noreply.github.com> Date: Mon, 30 Nov 2020 20:00:00 +0100 Subject: [PATCH] =?UTF-8?q?lib:=20add=C2=A0uncurried=20accessor=C2=A0prope?= =?UTF-8?q?rties=20to=C2=A0`primordials`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes: https://github.com/nodejs/node/pull/32127 PR-URL: https://github.com/nodejs/node/pull/36329 Fixes: https://github.com/nodejs/node/issues/32127 Reviewed-By: Antoine du Hamel Reviewed-By: James M Snell --- lib/buffer.js | 10 +---- lib/internal/per_context/primordials.js | 55 +++++++++++++++++-------- lib/internal/util/inspect.js | 19 +++------ lib/internal/util/types.js | 34 ++++++--------- 4 files changed, 58 insertions(+), 60 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index b84078e04b3a57..d3034a46c60b29 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -35,7 +35,6 @@ const { ObjectCreate, ObjectDefineProperties, ObjectDefineProperty, - ObjectGetOwnPropertyDescriptor, ObjectSetPrototypeOf, StringPrototypeCharCodeAt, StringPrototypeReplace, @@ -44,12 +43,11 @@ const { StringPrototypeTrim, SymbolSpecies, SymbolToPrimitive, - TypedArrayPrototype, + TypedArrayPrototypeGetByteLength, TypedArrayPrototypeFill, TypedArrayPrototypeSet, Uint8Array, Uint8ArrayPrototype, - uncurryThis, } = primordials; const { @@ -117,10 +115,6 @@ const { createUnsafeBuffer } = require('internal/buffer'); -const TypedArrayProto_byteLength = uncurryThis( - ObjectGetOwnPropertyDescriptor(TypedArrayPrototype, - 'byteLength').get); - FastBuffer.prototype.constructor = Buffer; Buffer.prototype = FastBuffer.prototype; addBufferPrototypeMethods(Buffer.prototype); @@ -1015,7 +1009,7 @@ function _fill(buf, value, offset, end, encoding) { if (typeof value === 'number') { // OOB check - const byteLen = TypedArrayProto_byteLength(buf); + const byteLen = TypedArrayPrototypeGetByteLength(buf); const fillLength = end - offset; if (offset > end || fillLength + offset > byteLen) throw new ERR_BUFFER_OUT_OF_BOUNDS(); diff --git a/lib/internal/per_context/primordials.js b/lib/internal/per_context/primordials.js index baaca6d273f86c..9f61b9e849dc2b 100644 --- a/lib/internal/per_context/primordials.js +++ b/lib/internal/per_context/primordials.js @@ -30,44 +30,63 @@ function copyProps(src, dest) { } } +function getNewKey(key) { + return typeof key === 'symbol' ? + `Symbol${key.description[7].toUpperCase()}${key.description.slice(8)}` : + `${key[0].toUpperCase()}${key.slice(1)}`; +} + +function copyAccessor(dest, prefix, key, { enumerable, get, set }) { + Reflect.defineProperty(dest, `${prefix}Get${key}`, { + value: uncurryThis(get), + enumerable + }); + if (set !== undefined) { + Reflect.defineProperty(dest, `${prefix}Set${key}`, { + value: uncurryThis(set), + enumerable + }); + } +} + function copyPropsRenamed(src, dest, prefix) { for (const key of Reflect.ownKeys(src)) { - if (typeof key === 'string') { - Reflect.defineProperty( - dest, - `${prefix}${key[0].toUpperCase()}${key.slice(1)}`, - Reflect.getOwnPropertyDescriptor(src, key)); + const newKey = getNewKey(key); + const desc = Reflect.getOwnPropertyDescriptor(src, key); + if ('get' in desc) { + copyAccessor(dest, prefix, newKey, desc); + } else { + Reflect.defineProperty(dest, `${prefix}${newKey}`, desc); } } } function copyPropsRenamedBound(src, dest, prefix) { for (const key of Reflect.ownKeys(src)) { - if (typeof key === 'string') { - const desc = Reflect.getOwnPropertyDescriptor(src, key); + const newKey = getNewKey(key); + const desc = Reflect.getOwnPropertyDescriptor(src, key); + if ('get' in desc) { + copyAccessor(dest, prefix, newKey, desc); + } else { if (typeof desc.value === 'function') { desc.value = desc.value.bind(src); } - Reflect.defineProperty( - dest, - `${prefix}${key[0].toUpperCase()}${key.slice(1)}`, - desc - ); + Reflect.defineProperty(dest, `${prefix}${newKey}`, desc); } } } function copyPrototype(src, dest, prefix) { for (const key of Reflect.ownKeys(src)) { - if (typeof key === 'string') { - const desc = Reflect.getOwnPropertyDescriptor(src, key); + const newKey = getNewKey(key); + const desc = Reflect.getOwnPropertyDescriptor(src, key); + if ('get' in desc) { + copyAccessor(dest, prefix, newKey, desc); + } else { if (typeof desc.value === 'function') { desc.value = uncurryThis(desc.value); } - Reflect.defineProperty( - dest, - `${prefix}${key[0].toUpperCase()}${key.slice(1)}`, - desc); + Reflect.defineProperty(dest, `${prefix}${newKey}`, desc); } } } diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 59c4c8a833e445..f5d93aa290fb39 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -20,7 +20,7 @@ const { Int32Array, JSONStringify, Map, - MapPrototype, + MapPrototypeGetSize, MapPrototypeEntries, MathFloor, MathMax, @@ -50,7 +50,7 @@ const { RegExp, RegExpPrototypeToString, Set, - SetPrototype, + SetPrototypeGetSize, SetPrototypeValues, String, StringPrototypeValueOf, @@ -58,7 +58,7 @@ const { SymbolPrototypeValueOf, SymbolIterator, SymbolToStringTag, - TypedArrayPrototype, + TypedArrayPrototypeGetLength, Uint16Array, Uint32Array, Uint8Array, @@ -137,13 +137,6 @@ const assert = require('internal/assert'); const { NativeModule } = require('internal/bootstrap/loaders'); -const setSizeGetter = uncurryThis( - ObjectGetOwnPropertyDescriptor(SetPrototype, 'size').get); -const mapSizeGetter = uncurryThis( - ObjectGetOwnPropertyDescriptor(MapPrototype, 'size').get); -const typedArraySizeGetter = uncurryThis( - ObjectGetOwnPropertyDescriptor(TypedArrayPrototype, 'length').get); - let hexSlice; const builtInObjects = new Set( @@ -854,7 +847,7 @@ function formatRaw(ctx, value, recurseTimes, typedArray) { extrasType = kArrayExtrasType; formatter = formatArray; } else if (isSet(value)) { - const size = setSizeGetter(value); + const size = SetPrototypeGetSize(value); const prefix = getPrefix(constructor, tag, 'Set', `(${size})`); keys = getKeys(value, ctx.showHidden); formatter = constructor !== null ? @@ -864,7 +857,7 @@ function formatRaw(ctx, value, recurseTimes, typedArray) { return `${prefix}{}`; braces = [`${prefix}{`, '}']; } else if (isMap(value)) { - const size = mapSizeGetter(value); + const size = MapPrototypeGetSize(value); const prefix = getPrefix(constructor, tag, 'Map', `(${size})`); keys = getKeys(value, ctx.showHidden); formatter = constructor !== null ? @@ -883,7 +876,7 @@ function formatRaw(ctx, value, recurseTimes, typedArray) { // Reconstruct the array information. bound = new constr(value); } - const size = typedArraySizeGetter(value); + const size = TypedArrayPrototypeGetLength(value); const prefix = getPrefix(constructor, tag, fallback, `(${size})`); braces = [`${prefix}[`, ']']; if (value.length === 0 && keys.length === 0 && !ctx.showHidden) diff --git a/lib/internal/util/types.js b/lib/internal/util/types.js index 115c473ae221d4..6f5309a41c3d79 100644 --- a/lib/internal/util/types.js +++ b/lib/internal/util/types.js @@ -2,63 +2,55 @@ const { ArrayBufferIsView, - ObjectGetOwnPropertyDescriptor, - SymbolToStringTag, - TypedArrayPrototype, - uncurryThis, + TypedArrayPrototypeGetSymbolToStringTag, } = primordials; -const TypedArrayProto_toStringTag = - uncurryThis( - ObjectGetOwnPropertyDescriptor(TypedArrayPrototype, - SymbolToStringTag).get); - function isTypedArray(value) { - return TypedArrayProto_toStringTag(value) !== undefined; + return TypedArrayPrototypeGetSymbolToStringTag(value) !== undefined; } function isUint8Array(value) { - return TypedArrayProto_toStringTag(value) === 'Uint8Array'; + return TypedArrayPrototypeGetSymbolToStringTag(value) === 'Uint8Array'; } function isUint8ClampedArray(value) { - return TypedArrayProto_toStringTag(value) === 'Uint8ClampedArray'; + return TypedArrayPrototypeGetSymbolToStringTag(value) === 'Uint8ClampedArray'; } function isUint16Array(value) { - return TypedArrayProto_toStringTag(value) === 'Uint16Array'; + return TypedArrayPrototypeGetSymbolToStringTag(value) === 'Uint16Array'; } function isUint32Array(value) { - return TypedArrayProto_toStringTag(value) === 'Uint32Array'; + return TypedArrayPrototypeGetSymbolToStringTag(value) === 'Uint32Array'; } function isInt8Array(value) { - return TypedArrayProto_toStringTag(value) === 'Int8Array'; + return TypedArrayPrototypeGetSymbolToStringTag(value) === 'Int8Array'; } function isInt16Array(value) { - return TypedArrayProto_toStringTag(value) === 'Int16Array'; + return TypedArrayPrototypeGetSymbolToStringTag(value) === 'Int16Array'; } function isInt32Array(value) { - return TypedArrayProto_toStringTag(value) === 'Int32Array'; + return TypedArrayPrototypeGetSymbolToStringTag(value) === 'Int32Array'; } function isFloat32Array(value) { - return TypedArrayProto_toStringTag(value) === 'Float32Array'; + return TypedArrayPrototypeGetSymbolToStringTag(value) === 'Float32Array'; } function isFloat64Array(value) { - return TypedArrayProto_toStringTag(value) === 'Float64Array'; + return TypedArrayPrototypeGetSymbolToStringTag(value) === 'Float64Array'; } function isBigInt64Array(value) { - return TypedArrayProto_toStringTag(value) === 'BigInt64Array'; + return TypedArrayPrototypeGetSymbolToStringTag(value) === 'BigInt64Array'; } function isBigUint64Array(value) { - return TypedArrayProto_toStringTag(value) === 'BigUint64Array'; + return TypedArrayPrototypeGetSymbolToStringTag(value) === 'BigUint64Array'; } module.exports = {