Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: refactor to use more primordials #36265

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 33 additions & 22 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
const {
ArrayFrom,
ArrayIsArray,
ArrayPrototypePop,
ArrayPrototypePush,
ArrayPrototypeSlice,
ArrayPrototypeSort,
Error,
Map,
ObjectCreate,
ObjectDefineProperties,
ObjectDefineProperty,
Expand All @@ -13,8 +16,14 @@ const {
ObjectGetPrototypeOf,
ObjectSetPrototypeOf,
Promise,
ReflectApply,
ReflectConstruct,
Set,
RegExpPrototypeTest,
SafeMap,
SafeSet,
StringPrototypeReplace,
StringPrototypeToLowerCase,
StringPrototypeToUpperCase,
Symbol,
SymbolFor,
} = primordials;
Expand All @@ -40,12 +49,12 @@ const { isNativeError } = internalBinding('types');

const noCrypto = !process.versions.openssl;

const experimentalWarnings = new Set();
const experimentalWarnings = new SafeSet();

const colorRegExp = /\u001b\[\d\d?m/g; // eslint-disable-line no-control-regex

function removeColors(str) {
return str.replace(colorRegExp, '');
return StringPrototypeReplace(str, colorRegExp, '');
}

function isError(e) {
Expand All @@ -57,7 +66,7 @@ function isError(e) {

// Keep a list of deprecation codes that have been warned on so we only warn on
// each one once.
const codesWarned = new Set();
const codesWarned = new SafeSet();

// Mark that a method should not be used.
// Returns a modified function which warns once by default.
Expand Down Expand Up @@ -86,7 +95,7 @@ function deprecate(fn, msg, code) {
if (new.target) {
return ReflectConstruct(fn, args, new.target);
}
return fn.apply(this, args);
return ReflectApply(fn, this, args);
}

// The wrapper will keep the same prototype as fn to maintain prototype chain
Expand Down Expand Up @@ -132,12 +141,13 @@ function slowCases(enc) {
case 4:
if (enc === 'UTF8') return 'utf8';
if (enc === 'ucs2' || enc === 'UCS2') return 'utf16le';
enc = `${enc}`.toLowerCase();
enc = StringPrototypeToLowerCase(`${enc}`);
if (enc === 'utf8') return 'utf8';
if (enc === 'ucs2') return 'utf16le';
break;
case 3:
if (enc === 'hex' || enc === 'HEX' || `${enc}`.toLowerCase() === 'hex')
if (enc === 'hex' || enc === 'HEX' ||
StringPrototypeToLowerCase(`${enc}`) === 'hex')
return 'hex';
break;
case 5:
Expand All @@ -146,7 +156,7 @@ function slowCases(enc) {
if (enc === 'UTF-8') return 'utf8';
if (enc === 'ASCII') return 'ascii';
if (enc === 'UCS-2') return 'utf16le';
enc = `${enc}`.toLowerCase();
enc = StringPrototypeToLowerCase(`${enc}`);
if (enc === 'utf-8') return 'utf8';
if (enc === 'ascii') return 'ascii';
if (enc === 'ucs-2') return 'utf16le';
Expand All @@ -156,18 +166,18 @@ function slowCases(enc) {
if (enc === 'latin1' || enc === 'binary') return 'latin1';
if (enc === 'BASE64') return 'base64';
if (enc === 'LATIN1' || enc === 'BINARY') return 'latin1';
enc = `${enc}`.toLowerCase();
enc = StringPrototypeToLowerCase(`${enc}`);
if (enc === 'base64') return 'base64';
if (enc === 'latin1' || enc === 'binary') return 'latin1';
break;
case 7:
if (enc === 'utf16le' || enc === 'UTF16LE' ||
`${enc}`.toLowerCase() === 'utf16le')
StringPrototypeToLowerCase(`${enc}`) === 'utf16le')
return 'utf16le';
break;
case 8:
if (enc === 'utf-16le' || enc === 'UTF-16LE' ||
`${enc}`.toLowerCase() === 'utf-16le')
StringPrototypeToLowerCase(`${enc}`) === 'utf-16le')
return 'utf16le';
break;
default:
Expand All @@ -184,25 +194,25 @@ function emitExperimentalWarning(feature) {
}

function filterDuplicateStrings(items, low) {
const map = new Map();
const map = new SafeMap();
for (let i = 0; i < items.length; i++) {
const item = items[i];
const key = item.toLowerCase();
const key = StringPrototypeToLowerCase(item);
if (low) {
map.set(key, key);
} else {
map.set(key, item);
}
}
return ArrayFrom(map.values()).sort();
return ArrayPrototypeSort(ArrayFrom(map.values()));
}

function cachedResult(fn) {
let result;
return () => {
if (result === undefined)
result = fn();
return result.slice();
return ArrayPrototypeSlice(result);
};
}

Expand Down Expand Up @@ -244,7 +254,7 @@ function convertToValidSignal(signal) {
return signal;

if (typeof signal === 'string') {
const signalName = signals[signal.toUpperCase()];
const signalName = signals[StringPrototypeToUpperCase(signal)];
if (signalName) return signalName;
}

Expand Down Expand Up @@ -294,7 +304,7 @@ function promisify(original) {

function fn(...args) {
return new Promise((resolve, reject) => {
original.call(this, ...args, (err, ...values) => {
ArrayPrototypePush(args, (err, ...values) => {
if (err) {
return reject(err);
}
Expand All @@ -307,6 +317,7 @@ function promisify(original) {
resolve(values[0]);
}
});
ReflectApply(original, this, args);
});
}

Expand Down Expand Up @@ -343,7 +354,7 @@ function join(output, separator) {
function spliceOne(list, index) {
for (; index + 1 < list.length; index++)
list[index] = list[index + 1];
list.pop();
ArrayPrototypePop(list);
}

const kNodeModulesRE = /^(.*)[\\/]node_modules[\\/]/;
Expand Down Expand Up @@ -376,9 +387,9 @@ function isInsideNodeModules() {
const filename = frame.getFileName();
// If a filename does not start with / or contain \,
// it's likely from Node.js core.
if (!/^\/|\\/.test(filename))
if (!RegExpPrototypeTest(/^\/|\\/, filename))
continue;
return kNodeModulesRE.test(filename);
return RegExpPrototypeTest(kNodeModulesRE, filename);
}
}
return false;
Expand All @@ -389,7 +400,7 @@ function once(callback) {
return function(...args) {
if (called) return;
called = true;
callback.apply(this, args);
ReflectApply(callback, this, args);
};
}

Expand Down
34 changes: 20 additions & 14 deletions lib/internal/util/comparisons.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

const {
ArrayIsArray,
ArrayPrototypeFilter,
ArrayPrototypePush,
BigIntPrototypeValueOf,
BooleanPrototypeValueOf,
DatePrototypeGetTime,
Error,
Map,
NumberIsNaN,
NumberPrototypeValueOf,
ObjectGetOwnPropertySymbols,
Expand All @@ -16,10 +17,11 @@ const {
ObjectPrototypeHasOwnProperty,
ObjectPrototypePropertyIsEnumerable,
ObjectPrototypeToString,
Set,
SafeMap,
SafeSet,
StringPrototypeValueOf,
SymbolPrototypeValueOf,
SymbolToStringTag,
TypedArrayPrototypeGetSymbolToStringTag,
Uint8Array,
} = primordials;

Expand Down Expand Up @@ -126,7 +128,7 @@ function isEqualBoxedPrimitive(val1, val2) {

function isIdenticalTypedArrayType(a, b) {
// Fast path to reduce type checks in the common case.
const check = types[`is${a[SymbolToStringTag]}`];
const check = types[`is${TypedArrayPrototypeGetSymbolToStringTag(a)}`];
if (check !== undefined && check(a)) {
return check(b);
}
Expand All @@ -150,8 +152,9 @@ function isIdenticalTypedArrayType(a, b) {
}
/* c8 ignore next 4 */
assert.fail(
`Unknown TypedArray type checking ${a[SymbolToStringTag]} ${a}\n` +
`and ${b[SymbolToStringTag]} ${b}`
'Unknown TypedArray type checking ' +
`${TypedArrayPrototypeGetSymbolToStringTag(a)} ${a}\n` +
`and ${TypedArrayPrototypeGetSymbolToStringTag(b)} ${b}`
);
}

Expand Down Expand Up @@ -291,7 +294,10 @@ function innerDeepEqual(val1, val2, strict, memos) {
}

function getEnumerables(val, keys) {
return keys.filter((k) => ObjectPrototypePropertyIsEnumerable(val, k));
return ArrayPrototypeFilter(
keys,
(k) => ObjectPrototypePropertyIsEnumerable(val, k)
);
}

function keyCheck(val1, val2, strict, memos, iterationType, aKeys) {
Expand Down Expand Up @@ -330,7 +336,7 @@ function keyCheck(val1, val2, strict, memos, iterationType, aKeys) {
if (!ObjectPrototypePropertyIsEnumerable(val2, key)) {
return false;
}
aKeys.push(key);
ArrayPrototypePush(aKeys, key);
count++;
} else if (ObjectPrototypePropertyIsEnumerable(val2, key)) {
return false;
Expand Down Expand Up @@ -360,8 +366,8 @@ function keyCheck(val1, val2, strict, memos, iterationType, aKeys) {
// Use memos to handle cycles.
if (memos === undefined) {
memos = {
val1: new Map(),
val2: new Map(),
val1: new SafeMap(),
val2: new SafeMap(),
position: 0
};
} else {
Expand Down Expand Up @@ -458,7 +464,7 @@ function setEquiv(a, b, strict, memo) {
// to check this improves the worst case scenario instead.
if (typeof val === 'object' && val !== null) {
if (set === null) {
set = new Set();
set = new SafeSet();
}
// If the specified value doesn't exist in the second set its an not null
// object (or non strict only: a not matching primitive) we'll need to go
Expand All @@ -475,7 +481,7 @@ function setEquiv(a, b, strict, memo) {
}

if (set === null) {
set = new Set();
set = new SafeSet();
}
set.add(val);
}
Expand Down Expand Up @@ -521,7 +527,7 @@ function mapEquiv(a, b, strict, memo) {
for (const [key, item1] of a) {
if (typeof key === 'object' && key !== null) {
if (set === null) {
set = new Set();
set = new SafeSet();
}
set.add(key);
} else {
Expand All @@ -537,7 +543,7 @@ function mapEquiv(a, b, strict, memo) {
if (!mapMightHaveLoosePrim(a, b, key, item1, memo))
return false;
if (set === null) {
set = new Set();
set = new SafeSet();
}
set.add(key);
}
Expand Down
8 changes: 5 additions & 3 deletions lib/internal/util/debuglog.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
'use strict';

const {
FunctionPrototype,
FunctionPrototypeBind,
ObjectCreate,
ObjectDefineProperty,
RegExp,
RegExpPrototypeTest,
SafeArrayIterator,
StringPrototypeToUpperCase
StringPrototypeToLowerCase,
StringPrototypeToUpperCase,
} = primordials;

const { inspect, format, formatWithOptions } = require('internal/util/inspect');
Expand Down Expand Up @@ -37,13 +39,13 @@ function initializeDebugEnv(debugEnv) {
function emitWarningIfNeeded(set) {
if ('HTTP' === set || 'HTTP2' === set) {
process.emitWarning('Setting the NODE_DEBUG environment variable ' +
'to \'' + set.toLowerCase() + '\' can expose sensitive ' +
'to \'' + StringPrototypeToLowerCase(set) + '\' can expose sensitive ' +
'data (such as passwords, tokens and authentication headers) ' +
'in the resulting log.');
}
}

function noop() {}
const noop = FunctionPrototype;

function debuglogImpl(enabled, set) {
if (debugImpls[set] === undefined) {
Expand Down
Loading