/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * @format * @oncall memory_lab */ /** * Where in the caller's code an eval error happened. * * The eval script is compiled as `memlab_eval`, so every stack frame in it * reads `memlab_eval::`. Those lines count the compiled source: * the prelude and its separator come first, and code with a top-level * `return` sits inside a one-line wrapper. An error such as "Cannot convert * object to primitive value" said nothing else, so the caller could not tell * which of 30 lines failed. */ export interface EvalSourceLayout { /** The code the caller passed. */ code: string; /** The prelude compiled ahead of it, if any. */ prelude?: string | null; /** Lines the compiler's wrapper added before the source (0 or 1). */ wrapperLines: number; } export interface EvalErrorLocation { part: 'code' | 'prelude' | 'fn'; line: number; /** 1-based; null for a syntax error, whose stack names only the line. */ col: number | null; /** The failing source line, when the part's text is known. */ text: string | null; /** The `helpers.fn` name, for `part: "fn"`. */ fn?: string; } /** `${prelude}\n;\n${code}` when there is a prelude, as runEval compiles it. */ export function joinPrelude( prelude: string | null | undefined, code: string, ): string { return prelude != null && prelude.trim() !== '' ? `${prelude}\n;\n${code}` : code; } function stackOf(err: unknown): string { // Cross-realm: an error raised by the script's own realm is not a host // `Error`, so read the property rather than test `instanceof`. if (err == null || typeof err !== 'object') return ''; const stack = (err as {stack?: unknown}).stack; return typeof stack === 'string' ? stack : ''; } /** * The first `memlab_eval` frame of `err`, mapped to the caller's code. A host * helper that threw has its own frames first; the first script frame is then * the call in the caller's code. Null when no frame is in the script. */ export function locateEvalError( err: unknown, layout: EvalSourceLayout, ): EvalErrorLocation | null { const stack = stackOf(err); if (stack === '') return null; let compiledLine: number; let col: number | null; const frame = /(?:^|[\s(])memlab_eval:(?:fn:([^\s:]+):)?(\d+):(\d+)\)?$/m.exec(stack); // Node puts `memlab_eval:` and the source line on top of the stack; // a compile error has only that, no frame in the script. const header = /^memlab_eval:(\d+)\n/.exec(stack); if (frame != null) { if (frame[1] != null) { return { part: 'fn', fn: frame[1], line: Number(frame[2]), col: Number(frame[3]), text: null, }; } compiledLine = Number(frame[2]); col = Number(frame[3]); } else if (header != null) { compiledLine = Number(header[1]); col = null; } else { return null; } const sourceLine = compiledLine - layout.wrapperLines; if (sourceLine < 1) return null; const hasPrelude = layout.prelude != null && layout.prelude.trim() !== ''; const preludeLines = hasPrelude ? (layout.prelude as string).split('\n') : []; // The prelude, then the `;` separator line, then the code. const codeStart = hasPrelude ? preludeLines.length + 2 : 1; const codeLines = layout.code.split('\n'); if (sourceLine >= codeStart) { const line = sourceLine - codeStart + 1; if (line > codeLines.length) return null; return {part: 'code', line, col, text: codeLines[line - 1]}; } if (sourceLine <= preludeLines.length) { return { part: 'prelude', line: sourceLine, col, text: preludeLines[sourceLine - 1], }; } return null; } /** * `at code line 3:15:` with the line and a caret under the column, for the * top of an eval error. */ export function formatEvalLocation(loc: EvalErrorLocation): string { const where = loc.part === 'fn' ? `helpers.fn("${loc.fn}") line ${loc.line}` : `${loc.part} line ${loc.line}`; const head = `at ${where}${loc.col != null ? `:${loc.col}` : ''}`; if (loc.text == null) return head; // Long minified-looking lines are cut around the column. const MAX = 160; let text = loc.text; let caretAt = loc.col != null ? loc.col - 1 : null; if (text.length > MAX) { const from = Math.max(0, Math.min(text.length - MAX, (caretAt ?? 0) - 60)); text = `${from > 0 ? '…' : ''}${text.slice(from, from + MAX)}…`; if (caretAt != null) caretAt = caretAt - from + (from > 0 ? 1 : 0); } const lines = [`${head}:`, ` ${text}`]; if (caretAt != null && caretAt >= 0 && caretAt <= text.length) { lines.push(` ${' '.repeat(caretAt)}^`); } return lines.join('\n'); }