Skip to content

Commit

Permalink
lib: add uncurried accessor properties to primordials
Browse files Browse the repository at this point in the history
Closes: #32127

PR-URL: #36329
Fixes: #32127
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
ExE-Boss authored and danielleadams committed Dec 7, 2020
1 parent cedf51f commit c2938bd
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 60 deletions.
10 changes: 2 additions & 8 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ const {
ObjectCreate,
ObjectDefineProperties,
ObjectDefineProperty,
ObjectGetOwnPropertyDescriptor,
ObjectSetPrototypeOf,
StringPrototypeCharCodeAt,
StringPrototypeReplace,
Expand All @@ -44,12 +43,11 @@ const {
StringPrototypeTrim,
SymbolSpecies,
SymbolToPrimitive,
TypedArrayPrototype,
TypedArrayPrototypeGetByteLength,
TypedArrayPrototypeFill,
TypedArrayPrototypeSet,
Uint8Array,
Uint8ArrayPrototype,
uncurryThis,
} = primordials;

const {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
55 changes: 37 additions & 18 deletions lib/internal/per_context/primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
19 changes: 6 additions & 13 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const {
Int32Array,
JSONStringify,
Map,
MapPrototype,
MapPrototypeGetSize,
MapPrototypeEntries,
MathFloor,
MathMax,
Expand Down Expand Up @@ -50,15 +50,15 @@ const {
RegExp,
RegExpPrototypeToString,
Set,
SetPrototype,
SetPrototypeGetSize,
SetPrototypeValues,
String,
StringPrototypeValueOf,
SymbolPrototypeToString,
SymbolPrototypeValueOf,
SymbolIterator,
SymbolToStringTag,
TypedArrayPrototype,
TypedArrayPrototypeGetLength,
Uint16Array,
Uint32Array,
Uint8Array,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 ?
Expand All @@ -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 ?
Expand All @@ -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)
Expand Down
34 changes: 13 additions & 21 deletions lib/internal/util/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit c2938bd

Please sign in to comment.