2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2021-02-04 17:18:32 -05:00
|
|
|
"use strict";
|
2020-12-10 08:45:41 -05:00
|
|
|
|
|
|
|
((window) => {
|
2023-06-13 11:45:06 -04:00
|
|
|
if (globalThis.__bootstrap.core.prepareStackTrace) {
|
|
|
|
return;
|
|
|
|
}
|
2022-04-15 10:08:09 -04:00
|
|
|
const core = Deno.core;
|
2022-08-11 09:56:56 -04:00
|
|
|
const ops = core.ops;
|
2021-07-02 06:18:30 -04:00
|
|
|
const {
|
|
|
|
Error,
|
|
|
|
ObjectFreeze,
|
|
|
|
ObjectAssign,
|
|
|
|
StringPrototypeStartsWith,
|
|
|
|
StringPrototypeEndsWith,
|
|
|
|
ObjectDefineProperties,
|
|
|
|
ArrayPrototypePush,
|
|
|
|
ArrayPrototypeMap,
|
|
|
|
ArrayPrototypeJoin,
|
|
|
|
} = window.__bootstrap.primordials;
|
|
|
|
|
2020-12-10 08:45:41 -05:00
|
|
|
// Keep in sync with `cli/fmt_errors.rs`.
|
2022-08-01 11:17:55 -04:00
|
|
|
function formatLocation(cse) {
|
|
|
|
if (cse.isNative) {
|
2020-12-10 08:45:41 -05:00
|
|
|
return "native";
|
|
|
|
}
|
|
|
|
let result = "";
|
2022-08-01 11:17:55 -04:00
|
|
|
if (cse.fileName) {
|
2022-08-11 09:56:56 -04:00
|
|
|
result += ops.op_format_file_name(cse.fileName);
|
2020-12-10 08:45:41 -05:00
|
|
|
} else {
|
2022-08-01 11:17:55 -04:00
|
|
|
if (cse.isEval) {
|
|
|
|
if (cse.evalOrigin == null) {
|
2020-12-10 08:45:41 -05:00
|
|
|
throw new Error("assert evalOrigin");
|
|
|
|
}
|
2022-08-01 11:17:55 -04:00
|
|
|
result += `${cse.evalOrigin}, `;
|
2020-12-10 08:45:41 -05:00
|
|
|
}
|
|
|
|
result += "<anonymous>";
|
|
|
|
}
|
2022-08-01 11:17:55 -04:00
|
|
|
if (cse.lineNumber != null) {
|
|
|
|
result += `:${cse.lineNumber}`;
|
|
|
|
if (cse.columnNumber != null) {
|
|
|
|
result += `:${cse.columnNumber}`;
|
2020-12-10 08:45:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep in sync with `cli/fmt_errors.rs`.
|
2022-08-01 11:17:55 -04:00
|
|
|
function formatCallSiteEval(cse) {
|
2020-12-10 08:45:41 -05:00
|
|
|
let result = "";
|
2022-08-01 11:17:55 -04:00
|
|
|
if (cse.isAsync) {
|
2020-12-10 08:45:41 -05:00
|
|
|
result += "async ";
|
|
|
|
}
|
2022-08-01 11:17:55 -04:00
|
|
|
if (cse.isPromiseAll) {
|
|
|
|
result += `Promise.all (index ${cse.promiseIndex})`;
|
2020-12-10 08:45:41 -05:00
|
|
|
return result;
|
|
|
|
}
|
2022-08-01 11:17:55 -04:00
|
|
|
const isMethodCall = !(cse.isToplevel || cse.isConstructor);
|
2020-12-10 08:45:41 -05:00
|
|
|
if (isMethodCall) {
|
2022-08-01 11:17:55 -04:00
|
|
|
if (cse.functionName) {
|
|
|
|
if (cse.typeName) {
|
|
|
|
if (!StringPrototypeStartsWith(cse.functionName, cse.typeName)) {
|
|
|
|
result += `${cse.typeName}.`;
|
2020-12-10 08:45:41 -05:00
|
|
|
}
|
|
|
|
}
|
2022-08-01 11:17:55 -04:00
|
|
|
result += cse.functionName;
|
|
|
|
if (cse.methodName) {
|
|
|
|
if (!StringPrototypeEndsWith(cse.functionName, cse.methodName)) {
|
|
|
|
result += ` [as ${cse.methodName}]`;
|
2020-12-10 08:45:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-08-01 11:17:55 -04:00
|
|
|
if (cse.typeName) {
|
|
|
|
result += `${cse.typeName}.`;
|
2020-12-10 08:45:41 -05:00
|
|
|
}
|
2022-08-01 11:17:55 -04:00
|
|
|
if (cse.methodName) {
|
|
|
|
result += cse.methodName;
|
2020-12-10 08:45:41 -05:00
|
|
|
} else {
|
|
|
|
result += "<anonymous>";
|
|
|
|
}
|
|
|
|
}
|
2022-08-01 11:17:55 -04:00
|
|
|
} else if (cse.isConstructor) {
|
2020-12-10 08:45:41 -05:00
|
|
|
result += "new ";
|
2022-08-01 11:17:55 -04:00
|
|
|
if (cse.functionName) {
|
|
|
|
result += cse.functionName;
|
2020-12-10 08:45:41 -05:00
|
|
|
} else {
|
|
|
|
result += "<anonymous>";
|
|
|
|
}
|
2022-08-01 11:17:55 -04:00
|
|
|
} else if (cse.functionName) {
|
|
|
|
result += cse.functionName;
|
2020-12-10 08:45:41 -05:00
|
|
|
} else {
|
2022-08-01 11:17:55 -04:00
|
|
|
result += formatLocation(cse);
|
2020-12-10 08:45:41 -05:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-08-01 11:17:55 -04:00
|
|
|
result += ` (${formatLocation(cse)})`;
|
2020-12-10 08:45:41 -05:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function evaluateCallSite(callSite) {
|
|
|
|
return {
|
|
|
|
this: callSite.getThis(),
|
|
|
|
typeName: callSite.getTypeName(),
|
|
|
|
function: callSite.getFunction(),
|
|
|
|
functionName: callSite.getFunctionName(),
|
|
|
|
methodName: callSite.getMethodName(),
|
|
|
|
fileName: callSite.getFileName(),
|
|
|
|
lineNumber: callSite.getLineNumber(),
|
|
|
|
columnNumber: callSite.getColumnNumber(),
|
|
|
|
evalOrigin: callSite.getEvalOrigin(),
|
|
|
|
isToplevel: callSite.isToplevel(),
|
|
|
|
isEval: callSite.isEval(),
|
|
|
|
isNative: callSite.isNative(),
|
|
|
|
isConstructor: callSite.isConstructor(),
|
|
|
|
isAsync: callSite.isAsync(),
|
|
|
|
isPromiseAll: callSite.isPromiseAll(),
|
|
|
|
promiseIndex: callSite.getPromiseIndex(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-08-01 11:17:55 -04:00
|
|
|
function sourceMapCallSiteEval(cse) {
|
|
|
|
if (cse.fileName && cse.lineNumber != null && cse.columnNumber != null) {
|
2022-08-11 09:56:56 -04:00
|
|
|
return { ...cse, ...ops.op_apply_source_map(cse) };
|
2022-08-01 11:17:55 -04:00
|
|
|
}
|
|
|
|
return cse;
|
|
|
|
}
|
|
|
|
|
2022-05-03 13:45:57 -04:00
|
|
|
/** A function that can be used as `Error.prepareStackTrace`. */
|
2022-08-01 11:17:55 -04:00
|
|
|
function prepareStackTrace(error, callSites) {
|
|
|
|
let callSiteEvals = ArrayPrototypeMap(callSites, evaluateCallSite);
|
|
|
|
callSiteEvals = ArrayPrototypeMap(callSiteEvals, sourceMapCallSiteEval);
|
2022-05-03 13:45:57 -04:00
|
|
|
ObjectDefineProperties(error, {
|
2022-09-07 09:11:16 -04:00
|
|
|
__callSiteEvals: { __proto__: null, value: [], configurable: true },
|
2022-05-03 13:45:57 -04:00
|
|
|
});
|
|
|
|
const formattedCallSites = [];
|
2023-01-26 06:26:42 -05:00
|
|
|
for (let i = 0; i < callSiteEvals.length; ++i) {
|
|
|
|
const cse = callSiteEvals[i];
|
2022-08-01 11:17:55 -04:00
|
|
|
ArrayPrototypePush(error.__callSiteEvals, cse);
|
|
|
|
ArrayPrototypePush(formattedCallSites, formatCallSiteEval(cse));
|
2022-05-03 13:45:57 -04:00
|
|
|
}
|
|
|
|
const message = error.message !== undefined ? error.message : "";
|
|
|
|
const name = error.name !== undefined ? error.name : "Error";
|
|
|
|
let messageLine;
|
|
|
|
if (name != "" && message != "") {
|
|
|
|
messageLine = `${name}: ${message}`;
|
|
|
|
} else if ((name || message) != "") {
|
|
|
|
messageLine = name || message;
|
|
|
|
} else {
|
|
|
|
messageLine = "";
|
|
|
|
}
|
|
|
|
return messageLine +
|
|
|
|
ArrayPrototypeJoin(
|
|
|
|
ArrayPrototypeMap(formattedCallSites, (s) => `\n at ${s}`),
|
|
|
|
"",
|
|
|
|
);
|
2020-12-10 08:45:41 -05:00
|
|
|
}
|
|
|
|
|
2022-05-03 13:45:57 -04:00
|
|
|
ObjectAssign(globalThis.__bootstrap.core, { prepareStackTrace });
|
2021-07-02 06:18:30 -04:00
|
|
|
ObjectFreeze(globalThis.__bootstrap.core);
|
2020-12-10 08:45:41 -05:00
|
|
|
})(this);
|