diff --git a/lib/buffer.js b/lib/buffer.js index 6185ccb8635b23..da9354e2d1f9e6 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 { @@ -118,10 +116,6 @@ const { addBufferPrototypeMethods } = require('internal/buffer'); -const TypedArrayProto_byteLength = uncurryThis( - ObjectGetOwnPropertyDescriptor(TypedArrayPrototype, - 'byteLength').get); - FastBuffer.prototype.constructor = Buffer; Buffer.prototype = FastBuffer.prototype; addBufferPrototypeMethods(Buffer.prototype); @@ -1031,7 +1025,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 a82018da796bc8..1f7d0db2fdd840 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( @@ -862,7 +855,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 ? @@ -872,7 +865,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 ? @@ -891,7 +884,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 = {