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

reduce the import weight of internal/primordials #16209

Merged
merged 5 commits into from
Jan 11, 2025
Merged
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
61 changes: 28 additions & 33 deletions src/js/internal/assert/assertion_error.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
"use strict";

const {
ArrayPrototypeJoin,
ArrayPrototypePop,
ArrayPrototypeSlice,
Error,
ErrorCaptureStackTrace,
ObjectAssign,
ObjectDefineProperty,
ObjectGetPrototypeOf,
ObjectPrototypeHasOwnProperty,
String,
StringPrototypeRepeat,
StringPrototypeSlice,
StringPrototypeSplit,
} = require("internal/primordials");


const { inspect } = require("internal/util/inspect");
const colors = require("internal/util/colors");
const { validateObject } = require("internal/validators");

const ErrorCaptureStackTrace = Error.captureStackTrace;
const ObjectAssign = Object.assign;
const ObjectDefineProperty = Object.defineProperty;
const ObjectGetPrototypeOf = Object.getPrototypeOf;
const ObjectPrototypeHasOwnProperty = Object.prototype.hasOwnProperty;
const ArrayPrototypeJoin = Array.prototype.join;
const ArrayPrototypePop = Array.prototype.pop;
const ArrayPrototypeSlice = Array.prototype.slice;
const StringPrototypeRepeat = String.prototype.repeat;
const StringPrototypeSlice = String.prototype.slice;
const StringPrototypeSplit = String.prototype.split;

declare namespace Internal {
const enum Operation {
Insert = 0,
Expand Down Expand Up @@ -63,7 +58,7 @@ function copyError(source) {
__proto__: null,
value: source.message,
});
if (ObjectPrototypeHasOwnProperty(source, "cause")) {
if (ObjectPrototypeHasOwnProperty.$call(source, "cause")) {
let { cause } = source;

if (Error.isError(cause)) {
Expand Down Expand Up @@ -115,7 +110,7 @@ function getColoredMyersDiff(actual, expected) {
const header = `${colors.green}actual${colors.white} ${colors.red}expected${colors.white}`;
const skipped = false;

// const diff = myersDiff(StringPrototypeSplit(actual, ""), StringPrototypeSplit(expected, ""));
// const diff = myersDiff(StringPrototypeSplit.$call(actual, ""), StringPrototypeSplit.$call(expected, ""));
const diff = myersDiff(actual, expected, false, false);
let message = printSimpleMyersDiff(diff);

Expand Down Expand Up @@ -149,7 +144,7 @@ function getStackedDiff(actual, expected) {
}

if (indicatorIdx !== -1) {
message += `\n${StringPrototypeRepeat(" ", indicatorIdx + 2)}^`;
message += `\n${StringPrototypeRepeat.$call(" ", indicatorIdx + 2)}^`;
}
}

Expand Down Expand Up @@ -193,8 +188,8 @@ function createErrDiff(actual, expected, operator, customMessage) {
let message = "";
const inspectedActual = inspectValue(actual);
const inspectedExpected = inspectValue(expected);
const inspectedSplitActual = StringPrototypeSplit(inspectedActual, "\n");
const inspectedSplitExpected = StringPrototypeSplit(inspectedExpected, "\n");
const inspectedSplitActual = StringPrototypeSplit.$call(inspectedActual, "\n");
const inspectedSplitExpected = StringPrototypeSplit.$call(inspectedExpected, "\n");
const showSimpleDiff = isSimpleDiff(actual, inspectedSplitActual, expected, inspectedSplitExpected);
let header = `${colors.green}+ actual${colors.white} ${colors.red}- expected${colors.white}`;

Expand All @@ -211,10 +206,10 @@ function createErrDiff(actual, expected, operator, customMessage) {
// Handles the case where the objects are structurally the same but different references
operator = "notIdentical";
if (inspectedSplitActual.length > 50) {
message = `${ArrayPrototypeJoin(ArrayPrototypeSlice(inspectedSplitActual, 0, 50), "\n")}\n...}`;
message = `${ArrayPrototypeJoin.$call(ArrayPrototypeSlice.$call(inspectedSplitActual, 0, 50), "\n")}\n...}`;
skipped = true;
} else {
message = ArrayPrototypeJoin(inspectedSplitActual, "\n");
message = ArrayPrototypeJoin.$call(inspectedSplitActual, "\n");
}
header = "";
} else {
Expand All @@ -236,12 +231,12 @@ function createErrDiff(actual, expected, operator, customMessage) {
}

function addEllipsis(string) {
const lines = StringPrototypeSplit(string, "\n", 11);
const lines = StringPrototypeSplit.$call(string, "\n", 11);
if (lines.length > 10) {
lines.length = 10;
return `${ArrayPrototypeJoin(lines, "\n")}\n...`;
return `${ArrayPrototypeJoin.$call(lines, "\n")}\n...`;
} else if (string.length > kMaxLongStringLength) {
return `${StringPrototypeSlice(string, kMaxLongStringLength)}...`;
return `${StringPrototypeSlice.$call(string, kMaxLongStringLength)}...`;
}
return string;
}
Expand Down Expand Up @@ -296,7 +291,7 @@ class AssertionError extends Error {
// In case the objects are equal but the operator requires unequal, show
// the first object and say A equals B
let base = kReadableOperator[operator];
const res = StringPrototypeSplit(inspectValue(actual), "\n");
const res = StringPrototypeSplit.$call(inspectValue(actual), "\n");

// In case "actual" is an object or a function, it should not be
// reference equal.
Expand All @@ -312,15 +307,15 @@ class AssertionError extends Error {
if (res.length > 50) {
res[46] = `${colors.blue}...${colors.white}`;
while (res.length > 47) {
ArrayPrototypePop(res);
ArrayPrototypePop.$call(res);
}
}

// Only print a single input.
if (res.length === 1) {
super(`${base}${res[0].length > 5 ? "\n\n" : " "}${res[0]}`);
} else {
super(`${base}\n\n${ArrayPrototypeJoin(res, "\n")}\n`);
super(`${base}\n\n${ArrayPrototypeJoin.$call(res, "\n")}\n`);
}
} else {
let res = inspectValue(actual);
Expand All @@ -329,15 +324,15 @@ class AssertionError extends Error {
if (operator === "notDeepEqual" && res === other) {
res = `${knownOperator}\n\n${res}`;
if (res.length > 1024) {
res = `${StringPrototypeSlice(res, 0, 1021)}...`;
res = `${StringPrototypeSlice.$call(res, 0, 1021)}...`;
}
super(res);
} else {
if (res.length > kMaxLongStringLength) {
res = `${StringPrototypeSlice(res, 0, 509)}...`;
res = `${StringPrototypeSlice.$call(res, 0, 509)}...`;
}
if (other.length > kMaxLongStringLength) {
other = `${StringPrototypeSlice(other, 0, 509)}...`;
other = `${StringPrototypeSlice.$call(other, 0, 509)}...`;
}
if (operator === "deepEqual") {
res = `${knownOperator}\n\n${res}\n\nshould loosely deep-equal\n\n`;
Expand Down
25 changes: 10 additions & 15 deletions src/js/internal/assert/calltracker.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
"use strict";

const {
ArrayPrototypePush,
ArrayPrototypeSlice,
Error,
FunctionPrototype,
ObjectFreeze,
Proxy,
SafeSet,
SafeWeakMap,
} = require("internal/primordials");
const { SafeSet, SafeWeakMap } = require("internal/primordials");

const AssertionError = require("internal/assert/assertion_error");
const { validateUint32 } = require("internal/validators");

const noop = FunctionPrototype;
const ObjectFreeze = Object.freeze;
const ArrayPrototypePush = Array.prototype.push;
const ArrayPrototypeSlice = Array.prototype.slice;

const noop = () => {};

class CallTrackerContext {
#expected;
Expand All @@ -29,8 +24,8 @@ class CallTrackerContext {
}

track(thisArg, args) {
const argsClone = ObjectFreeze(ArrayPrototypeSlice(args));
ArrayPrototypePush(this.#calls, ObjectFreeze({ thisArg, arguments: argsClone }));
const argsClone = ObjectFreeze(ArrayPrototypeSlice.$call(args));
ArrayPrototypePush.$call(this.#calls, ObjectFreeze({ thisArg, arguments: argsClone }));
}

get delta() {
Expand All @@ -41,7 +36,7 @@ class CallTrackerContext {
this.#calls = [];
}
getCalls() {
return ObjectFreeze(ArrayPrototypeSlice(this.#calls));
return ObjectFreeze(ArrayPrototypeSlice.$call(this.#calls));
}

report() {
Expand Down Expand Up @@ -119,7 +114,7 @@ class CallTracker {
for (const context of this.#callChecks) {
const message = context.report();
if (message !== undefined) {
ArrayPrototypePush(errors, message);
ArrayPrototypePush.$call(errors, message);
}
}
return errors;
Expand Down
16 changes: 0 additions & 16 deletions src/js/internal/assert/utils.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
/* prettier-ignore */
'use strict';

// const {
// ArrayPrototypeShift,
// Error,
// ErrorCaptureStackTrace,
// FunctionPrototypeBind,
// RegExpPrototypeSymbolReplace,
// SafeMap,
// StringPrototypeCharCodeAt,
// StringPrototypeIncludes,
// StringPrototypeIndexOf,
// StringPrototypeReplace,
// StringPrototypeSlice,
// StringPrototypeSplit,
// StringPrototypeStartsWith,
// } = require("internal/primordials");

var AssertionError;
function loadAssertionError() {
if (AssertionError === undefined) {
Expand Down
125 changes: 7 additions & 118 deletions src/js/internal/primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// Do not use this file for new code, many things here will be slow especailly when intrinsics for these operations is available.
// It is primarily used for `internal/util`

const ObjectSetPrototypeOf = Object.setPrototypeOf;
const ObjectFreeze = Object.freeze;

const createSafeIterator = (factory, next) => {
class SafeIterator {
constructor(iterable) {
Expand All @@ -14,9 +17,9 @@ const createSafeIterator = (factory, next) => {
return this;
}
}
Object.setPrototypeOf(SafeIterator.prototype, null);
Object.freeze(SafeIterator.prototype);
Object.freeze(SafeIterator);
ObjectSetPrototypeOf(SafeIterator.prototype, null);
ObjectFreeze(SafeIterator.prototype);
ObjectFreeze(SafeIterator);
return SafeIterator;
};

Expand Down Expand Up @@ -76,97 +79,16 @@ const StringIterator = uncurryThis(String.prototype[Symbol.iterator]);
const StringIteratorPrototype = Reflect.getPrototypeOf(StringIterator(""));
const ArrayPrototypeForEach = uncurryThis(Array.prototype.forEach);

function ErrorCaptureStackTrace(targetObject, maybeStartStackFn) {
Error.captureStackTrace(targetObject, maybeStartStackFn);

if (maybeStartStackFn === undefined) {
// Remove the second line, which is this function
targetObject.stack = targetObject.stack.replace(/.*\n.*/, "$1");
}
}

const arrayProtoPush = Array.prototype.push;
const ArrayPrototypeSymbolIterator = uncurryThis(Array.prototype[Symbol.iterator]);
const ArrayIteratorPrototypeNext = uncurryThis(ArrayPrototypeSymbolIterator.next);

export default {
makeSafe, // exported for testing
Array,
ArrayFrom: Array.from,
ArrayIsArray: Array.isArray,
SafeArrayIterator: createSafeIterator(ArrayPrototypeSymbolIterator, ArrayIteratorPrototypeNext),
ArrayPrototypeFlat: uncurryThis(Array.prototype.flat),
ArrayPrototypeFilter: uncurryThis(Array.prototype.filter),
ArrayPrototypeForEach,
ArrayPrototypeFill: uncurryThis(Array.prototype.fill),
ArrayPrototypeIncludes: uncurryThis(Array.prototype.includes),
ArrayPrototypeIndexOf: uncurryThis(Array.prototype.indexOf),
ArrayPrototypeJoin: uncurryThis(Array.prototype.join),
ArrayPrototypeMap: uncurryThis(Array.prototype.map),
ArrayPrototypePop: uncurryThis(Array.prototype.pop),
ArrayPrototypePush: uncurryThis(arrayProtoPush),
ArrayPrototypePushApply: (a, b) => arrayProtoPush.$apply(a, b),
ArrayPrototypeSlice: uncurryThis(Array.prototype.slice),
ArrayPrototypeSort: uncurryThis(Array.prototype.sort),
ArrayPrototypeSplice: uncurryThis(Array.prototype.splice),
ArrayPrototypeUnshift: uncurryThis(Array.prototype.unshift),
BigIntPrototypeValueOf: uncurryThis(BigInt.prototype.valueOf),
BooleanPrototypeValueOf: uncurryThis(Boolean.prototype.valueOf),
DatePrototypeGetTime: uncurryThis(Date.prototype.getTime),
DatePrototypeToISOString: uncurryThis(Date.prototype.toISOString),
DatePrototypeToString: uncurryThis(Date.prototype.toString),
Error,
ErrorCaptureStackTrace,
ErrorPrototypeToString: uncurryThis(Error.prototype.toString),
FunctionPrototypeBind: uncurryThis(Function.prototype.bind),
FunctionPrototypeCall: uncurryThis(Function.prototype["call"]),
FunctionPrototypeToString: uncurryThis(Function.prototype.toString),
JSONStringify: JSON.stringify,
MapPrototypeDelete: uncurryThis(Map.prototype.delete),
MapPrototypeSet: uncurryThis(Map.prototype.set),
MapPrototypeGetSize: getGetter(Map, "size"),
MapPrototypeEntries: uncurryThis(Map.prototype.entries),
MapPrototypeValues: uncurryThis(Map.prototype.values),
MapPrototypeKeys: uncurryThis(Map.prototype.keys),
MathFloor: Math.floor,
MathMax: Math.max,
MathMin: Math.min,
MathRound: Math.round,
MathSqrt: Math.sqrt,
MathTrunc: Math.trunc,
MathAbs: Math.abs,
Number,
NumberIsFinite: Number.isFinite,
NumberIsNaN: Number.isNaN,
NumberParseFloat: Number.parseFloat,
NumberParseInt: Number.parseInt,
NumberPrototypeToString: uncurryThis(Number.prototype.toString),
NumberPrototypeValueOf: uncurryThis(Number.prototype.valueOf),
Object,
ObjectAssign: Object.assign,
ObjectCreate: Object.create,
ObjectDefineProperty: Object.defineProperty,
ObjectEntries: Object.entries,
ObjectGetOwnPropertyDescriptor: Object.getOwnPropertyDescriptor,
ObjectGetOwnPropertyDescriptors: Object.getOwnPropertyDescriptors,
ObjectGetOwnPropertyNames: Object.getOwnPropertyNames,
ObjectGetOwnPropertySymbols: Object.getOwnPropertySymbols,
ObjectGetPrototypeOf: Object.getPrototypeOf,
ObjectIs: Object.is,
ObjectKeys: Object.keys,
ObjectPrototypeHasOwnProperty: uncurryThis(Object.prototype.hasOwnProperty),
ObjectPrototypeIsPrototypeOf: uncurryThis(Object.prototype.isPrototypeOf),
ObjectPrototypePropertyIsEnumerable: uncurryThis(Object.prototype.propertyIsEnumerable),
ObjectPrototypeToString: uncurryThis(Object.prototype.toString),
ObjectSeal: Object.seal,
ObjectSetPrototypeOf: Object.setPrototypeOf,
ReflectHas: Reflect.has,
ReflectOwnKeys: Reflect.ownKeys,
RegExp,
RegExpPrototypeExec: uncurryThis(RegExp.prototype.exec),
RegExpPrototypeSymbolReplace: uncurryThis(RegExp.prototype[Symbol.replace]),
RegExpPrototypeSymbolSplit: uncurryThis(RegExp.prototype[Symbol.split]),
RegExpPrototypeTest: uncurryThis(RegExp.prototype.test),
RegExpPrototypeToString: uncurryThis(RegExp.prototype.toString),
SafeStringIterator: createSafeIterator(StringIterator, uncurryThis(StringIteratorPrototype.next)),
SafeMap: makeSafe(
Map,
Expand All @@ -192,41 +114,8 @@ export default {
}
},
),
DatePrototypeGetMilliseconds: uncurryThis(Date.prototype.getMilliseconds),
DatePrototypeToUTCString: uncurryThis(Date.prototype.toUTCString),
SetPrototypeGetSize: getGetter(Set, "size"),
SetPrototypeEntries: uncurryThis(Set.prototype.entries),
SetPrototypeValues: uncurryThis(Set.prototype.values),
String,
StringPrototypeAt: uncurryThis(String.prototype.at),
StringPrototypeCharCodeAt: uncurryThis(String.prototype.charCodeAt),
StringPrototypeCodePointAt: uncurryThis(String.prototype.codePointAt),
StringPrototypeEndsWith: uncurryThis(String.prototype.endsWith),
StringPrototypeIncludes: uncurryThis(String.prototype.includes),
StringPrototypeIndexOf: uncurryThis(String.prototype.indexOf),
StringPrototypeLastIndexOf: uncurryThis(String.prototype.lastIndexOf),
StringPrototypeMatch: uncurryThis(String.prototype.match),
StringPrototypeNormalize: uncurryThis(String.prototype.normalize),
StringPrototypePadEnd: uncurryThis(String.prototype.padEnd),
StringPrototypePadStart: uncurryThis(String.prototype.padStart),
StringPrototypeRepeat: uncurryThis(String.prototype.repeat),
StringPrototypeReplace: uncurryThis(String.prototype.replace),
StringPrototypeReplaceAll: uncurryThis(String.prototype.replaceAll),
StringPrototypeSlice: uncurryThis(String.prototype.slice),
StringPrototypeSplit: uncurryThis(String.prototype.split),
StringPrototypeStartsWith: uncurryThis(String.prototype.startsWith),
StringPrototypeToLowerCase: uncurryThis(String.prototype.toLowerCase),
StringPrototypeToUpperCase: uncurryThis(String.prototype.toUpperCase),
StringPrototypeTrim: uncurryThis(String.prototype.trim),
StringPrototypeValueOf: uncurryThis(String.prototype.valueOf),
SymbolPrototypeToString: uncurryThis(Symbol.prototype.toString),
SymbolPrototypeValueOf: uncurryThis(Symbol.prototype.valueOf),
SymbolDispose: Symbol.dispose,
SymbolAsyncDispose: Symbol.asyncDispose,
SymbolIterator: Symbol.iterator,
SymbolAsyncIterator: Symbol.asyncIterator,
SymbolFor: Symbol.for,
SymbolToStringTag: Symbol.toStringTag,
TypedArrayPrototypeGetLength: getGetter(Uint8Array, "length"),
TypedArrayPrototypeGetSymbolToStringTag: getGetter(Uint8Array, Symbol.toStringTag),
Uint8ClampedArray,
Expand Down
Loading
Loading