/** * 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 */ /** * Unit tests for eval error locations (eval-location.ts): a stack frame in * the compiled eval script maps to the caller's code line and column, past * the prelude and the top-level-return wrapper, and a query names its own * line. * * npm run build-pkg && npm run test-units * * Runs against the BUILT dist/. */ import './test-isolation.mjs'; import assert from 'assert/strict'; import fs from 'fs'; import path from 'path'; import vm from 'vm'; import {test} from 'node:test'; import {fileURLToPath, pathToFileURL} from 'url'; const here = path.dirname(fileURLToPath(import.meta.url)); const dist = path.join(here, '..', 'dist'); if (!fs.existsSync(path.join(dist, 'eval-location.js'))) { console.error( 'check-eval-error-location: dist/eval-location.js is missing; build first.', ); process.exit(1); } const {formatEvalLocation, joinPrelude, locateEvalError} = await import( pathToFileURL(path.join(dist, 'eval-location.js')).href ); const {compileEvalScript, composeQueries} = await import( pathToFileURL(path.join(dist, 'tools', 'eval.js')).href ); const THROWS_ON_3 = [ 'const o = Object.create(null);', 'let n = 1;', 'result = `${o}`;', ].join('\n'); /** Compile and run as runEval does; the thrown error and its layout. */ function fail(code, prelude) { const source = joinPrelude(prelude, code); let wrapperLines = 0; try { const compiled = compileEvalScript(source, 'memlab_eval'); wrapperLines = compiled.wrapperLines; compiled.script.runInContext(vm.createContext({result: undefined})); } catch (err) { const tagged = err?.memlabWrapperLines; return { err, layout: { code, prelude, wrapperLines: typeof tagged === 'number' ? tagged : wrapperLines, }, }; } throw new Error('expected the code to throw'); } test('a runtime error names the code line and column', () => { const {err, layout} = fail(THROWS_ON_3); const loc = locateEvalError(err, layout); assert.equal(loc.part, 'code'); assert.equal(loc.line, 3); assert.equal(loc.text, 'result = `${o}`;'); const shown = formatEvalLocation(loc); assert.match( shown, /^at code line 3:\d+:\n {4}result = `\$\{o\}`;\n {4} *\^$/, ); }); test('the prelude lines are not counted against the code', () => { const {err, layout} = fail(THROWS_ON_3, 'const a = 1;\nconst b = 2;'); assert.deepEqual( {...locateEvalError(err, layout), col: null}, {part: 'code', line: 3, col: null, text: 'result = `${o}`;'}, ); }); test('an error in the prelude says so', () => { const {err, layout} = fail('result = 1;', 'const a = 1;\nnull.x;'); const loc = locateEvalError(err, layout); assert.equal(loc.part, 'prelude'); assert.equal(loc.line, 2); }); test('code with a top-level return is mapped past its wrapper', () => { const {err, layout} = fail(`${THROWS_ON_3}\nreturn result;`); assert.equal(layout.wrapperLines, 1); const loc = locateEvalError(err, layout); assert.equal(loc.line, 3); }); test('a syntax error names its line', () => { const {err, layout} = fail('const a = 1;\nconst b = ;\nresult = a;'); const loc = locateEvalError(err, layout); assert.equal(loc.line, 2); assert.equal(loc.col, null); assert.match(formatEvalLocation(loc), /^at code line 2:\n {4}const b = ;$/); }); test('a syntax error inside the return wrapper is still on its line', () => { const {err, layout} = fail('return 1;\nconst b = ;'); assert.equal(layout.wrapperLines, 1); assert.equal(locateEvalError(err, layout).line, 2); }); test('a host helper that threw maps to the call in the code', () => { const ctx = vm.createContext({ result: undefined, helper: () => { throw new TypeError('bad id'); }, }); const code = 'let x = 1;\n\nresult = helper(x);'; const {script, wrapperLines} = compileEvalScript(code, 'memlab_eval'); let err; try { script.runInContext(ctx); } catch (e) { err = e; } assert.equal(locateEvalError(err, {code, wrapperLines}).line, 3); }); test('no script frame, no location', () => { assert.equal( locateEvalError(new Error('host only'), {code: 'x', wrapperLines: 0}), null, ); assert.equal(locateEvalError('a string', {code: 'x', wrapperLines: 0}), null); }); test('a failing query names its own line', () => { const source = joinPrelude( 'const p = 1;', composeQueries([ {name: 'ok', code: 'return 1;'}, {name: 'bad', code: 'const o = Object.create(null);\nreturn `${o}`;'}, ]), ); const ctx = vm.createContext({result: undefined}); compileEvalScript(source, 'memlab_eval').script.runInContext(ctx); assert.equal(ctx.result.ok, 1); assert.match( ctx.result.bad.error, /primitive value \(at query line 2:\d+\)$/, ); });