2021-01-10 21:59:07 -05:00
|
|
|
// Copyright 2018-2021 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) => {
|
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
|
|
|
// Some of the code here is adapted directly from V8 and licensed under a BSD
|
|
|
|
// style license available here: https://github.com/v8/v8/blob/24886f2d1c565287d33d71e4109a53bf0b54b75c/LICENSE.v8
|
|
|
|
function patchCallSite(callSite, location) {
|
|
|
|
return {
|
|
|
|
getThis() {
|
|
|
|
return callSite.getThis();
|
|
|
|
},
|
|
|
|
getTypeName() {
|
|
|
|
return callSite.getTypeName();
|
|
|
|
},
|
|
|
|
getFunction() {
|
|
|
|
return callSite.getFunction();
|
|
|
|
},
|
|
|
|
getFunctionName() {
|
|
|
|
return callSite.getFunctionName();
|
|
|
|
},
|
|
|
|
getMethodName() {
|
|
|
|
return callSite.getMethodName();
|
|
|
|
},
|
|
|
|
getFileName() {
|
|
|
|
return location.fileName;
|
|
|
|
},
|
|
|
|
getLineNumber() {
|
|
|
|
return location.lineNumber;
|
|
|
|
},
|
|
|
|
getColumnNumber() {
|
|
|
|
return location.columnNumber;
|
|
|
|
},
|
|
|
|
getEvalOrigin() {
|
|
|
|
return callSite.getEvalOrigin();
|
|
|
|
},
|
|
|
|
isToplevel() {
|
|
|
|
return callSite.isToplevel();
|
|
|
|
},
|
|
|
|
isEval() {
|
|
|
|
return callSite.isEval();
|
|
|
|
},
|
|
|
|
isNative() {
|
|
|
|
return callSite.isNative();
|
|
|
|
},
|
|
|
|
isConstructor() {
|
|
|
|
return callSite.isConstructor();
|
|
|
|
},
|
|
|
|
isAsync() {
|
|
|
|
return callSite.isAsync();
|
|
|
|
},
|
|
|
|
isPromiseAll() {
|
|
|
|
return callSite.isPromiseAll();
|
|
|
|
},
|
|
|
|
getPromiseIndex() {
|
|
|
|
return callSite.getPromiseIndex();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep in sync with `cli/fmt_errors.rs`.
|
2021-09-18 09:40:04 -04:00
|
|
|
function formatLocation(callSite, formatFileNameFn) {
|
2020-12-10 08:45:41 -05:00
|
|
|
if (callSite.isNative()) {
|
|
|
|
return "native";
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = "";
|
|
|
|
|
|
|
|
const fileName = callSite.getFileName();
|
|
|
|
|
|
|
|
if (fileName) {
|
2021-09-18 09:40:04 -04:00
|
|
|
result += formatFileNameFn(fileName);
|
2020-12-10 08:45:41 -05:00
|
|
|
} else {
|
|
|
|
if (callSite.isEval()) {
|
|
|
|
const evalOrigin = callSite.getEvalOrigin();
|
|
|
|
if (evalOrigin == null) {
|
|
|
|
throw new Error("assert evalOrigin");
|
|
|
|
}
|
|
|
|
result += `${evalOrigin}, `;
|
|
|
|
}
|
|
|
|
result += "<anonymous>";
|
|
|
|
}
|
|
|
|
|
|
|
|
const lineNumber = callSite.getLineNumber();
|
|
|
|
if (lineNumber != null) {
|
|
|
|
result += `:${lineNumber}`;
|
|
|
|
|
|
|
|
const columnNumber = callSite.getColumnNumber();
|
|
|
|
if (columnNumber != null) {
|
|
|
|
result += `:${columnNumber}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep in sync with `cli/fmt_errors.rs`.
|
2021-09-18 09:40:04 -04:00
|
|
|
function formatCallSite(callSite, formatFileNameFn) {
|
2020-12-10 08:45:41 -05:00
|
|
|
let result = "";
|
|
|
|
const functionName = callSite.getFunctionName();
|
|
|
|
|
|
|
|
const isTopLevel = callSite.isToplevel();
|
|
|
|
const isAsync = callSite.isAsync();
|
|
|
|
const isPromiseAll = callSite.isPromiseAll();
|
|
|
|
const isConstructor = callSite.isConstructor();
|
|
|
|
const isMethodCall = !(isTopLevel || isConstructor);
|
|
|
|
|
|
|
|
if (isAsync) {
|
|
|
|
result += "async ";
|
|
|
|
}
|
|
|
|
if (isPromiseAll) {
|
|
|
|
result += `Promise.all (index ${callSite.getPromiseIndex()})`;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
if (isMethodCall) {
|
|
|
|
const typeName = callSite.getTypeName();
|
|
|
|
const methodName = callSite.getMethodName();
|
|
|
|
|
|
|
|
if (functionName) {
|
|
|
|
if (typeName) {
|
2021-07-02 06:18:30 -04:00
|
|
|
if (!StringPrototypeStartsWith(functionName, typeName)) {
|
2020-12-10 08:45:41 -05:00
|
|
|
result += `${typeName}.`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result += functionName;
|
|
|
|
if (methodName) {
|
2021-07-02 06:18:30 -04:00
|
|
|
if (!StringPrototypeEndsWith(functionName, methodName)) {
|
2020-12-10 08:45:41 -05:00
|
|
|
result += ` [as ${methodName}]`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (typeName) {
|
|
|
|
result += `${typeName}.`;
|
|
|
|
}
|
|
|
|
if (methodName) {
|
|
|
|
result += methodName;
|
|
|
|
} else {
|
|
|
|
result += "<anonymous>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (isConstructor) {
|
|
|
|
result += "new ";
|
|
|
|
if (functionName) {
|
|
|
|
result += functionName;
|
|
|
|
} else {
|
|
|
|
result += "<anonymous>";
|
|
|
|
}
|
|
|
|
} else if (functionName) {
|
|
|
|
result += functionName;
|
|
|
|
} else {
|
2021-09-18 09:40:04 -04:00
|
|
|
result += formatLocation(callSite, formatFileNameFn);
|
2020-12-10 08:45:41 -05:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-09-18 09:40:04 -04:00
|
|
|
result += ` (${formatLocation(callSite, formatFileNameFn)})`;
|
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(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a function that can be used as `Error.prepareStackTrace`.
|
2021-04-28 10:08:51 -04:00
|
|
|
*
|
2020-12-10 08:45:41 -05:00
|
|
|
* This function accepts an optional argument, a function that performs
|
|
|
|
* source mapping. It is not required to pass this argument, but
|
|
|
|
* in such case only JavaScript sources will have proper position in
|
|
|
|
* stack frames.
|
|
|
|
* @param {(
|
|
|
|
* fileName: string,
|
|
|
|
* lineNumber: number,
|
|
|
|
* columnNumber: number
|
|
|
|
* ) => {
|
|
|
|
* fileName: string,
|
|
|
|
* lineNumber: number,
|
|
|
|
* columnNumber: number
|
2021-04-28 10:08:51 -04:00
|
|
|
* }} sourceMappingFn
|
2020-12-10 08:45:41 -05:00
|
|
|
*/
|
2021-09-18 09:40:04 -04:00
|
|
|
function createPrepareStackTrace(sourceMappingFn, formatFileNameFn) {
|
2020-12-10 08:45:41 -05:00
|
|
|
return function prepareStackTrace(
|
|
|
|
error,
|
|
|
|
callSites,
|
|
|
|
) {
|
2021-07-02 06:18:30 -04:00
|
|
|
const mappedCallSites = ArrayPrototypeMap(callSites, (callSite) => {
|
|
|
|
const fileName = callSite.getFileName();
|
|
|
|
const lineNumber = callSite.getLineNumber();
|
|
|
|
const columnNumber = callSite.getColumnNumber();
|
|
|
|
if (
|
|
|
|
sourceMappingFn && fileName && lineNumber != null &&
|
|
|
|
columnNumber != null
|
|
|
|
) {
|
|
|
|
return patchCallSite(
|
|
|
|
callSite,
|
|
|
|
sourceMappingFn({
|
|
|
|
fileName,
|
|
|
|
lineNumber,
|
|
|
|
columnNumber,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return callSite;
|
|
|
|
});
|
|
|
|
ObjectDefineProperties(error, {
|
2020-12-10 08:45:41 -05:00
|
|
|
__callSiteEvals: { value: [], configurable: true },
|
|
|
|
});
|
|
|
|
const formattedCallSites = [];
|
|
|
|
for (const callSite of mappedCallSites) {
|
2021-07-02 06:18:30 -04:00
|
|
|
ArrayPrototypePush(error.__callSiteEvals, evaluateCallSite(callSite));
|
2021-09-18 09:40:04 -04:00
|
|
|
ArrayPrototypePush(
|
|
|
|
formattedCallSites,
|
|
|
|
formatCallSite(callSite, formatFileNameFn),
|
|
|
|
);
|
2020-12-10 08:45:41 -05: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 +
|
2021-07-02 06:18:30 -04:00
|
|
|
ArrayPrototypeJoin(
|
|
|
|
ArrayPrototypeMap(formattedCallSites, (s) => `\n at ${s}`),
|
|
|
|
"",
|
|
|
|
);
|
2020-12-10 08:45:41 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-07-02 06:18:30 -04:00
|
|
|
ObjectAssign(globalThis.__bootstrap.core, { createPrepareStackTrace });
|
|
|
|
ObjectFreeze(globalThis.__bootstrap.core);
|
2020-12-10 08:45:41 -05:00
|
|
|
})(this);
|