lib: add uncurried accessor properties to primordials

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 <duhamelantoine1995@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
ExE Boss 2020-11-30 20:00:00 +01:00 committed by James M Snell
parent b5bb09448a
commit c83e599420
No known key found for this signature in database
GPG Key ID: 7341B15C070877AC
4 changed files with 58 additions and 60 deletions

View File

@ -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();

View File

@ -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);
}
}
}

View File

@ -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)

View File

@ -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 = {