Every compile went to godbolt.org from the session directly. The roadmap moves compiles, and later Dafny and Lean checks, to a pinned service of the app's own, and godbolt has no service guarantee, so where a compile goes is now one replaceable piece. - src/services/compile-backend.ts: a CompileBackend (id, host, compiler, compile), explorerBackend() for any service that speaks Compiler Explorer's API at a base URL, GODBOLT as the default, and compileBackend() / setCompileBackend(). - godbolt.ts: compileMojo takes a CompileTarget (base, compiler, host) and names that host in its messages; the request body is unchanged, so tests/mojo.test.mjs still holds it to what the corpus pipeline sends. - Session.compile asks the current backend and records which host answered (CompileRecord.host; older records came from godbolt.org). - The Compiler view names the host it is asking and the host that answered. The "Compiled on godbolt" badge keeps its wording until a second backend is deployed. A test sends a compile through a fake backend and a refused local address. Manual check list: open a Mojo exercise in the Developer area, Compile & run, and see "Asking godbolt.org (mojo_1_1_0)…" then "Mojo 1.1.0 on godbolt.org" in the Compiler view. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FgvbRdoEheAHqsRmr2WuKZ
354 lines
24 KiB
JavaScript
354 lines
24 KiB
JavaScript
// The Mojo corpus as the app sees it: the generated modules agree with mojo/pin.json
|
|
// and the harvested corpus, and the Python pipeline's own tests pass.
|
|
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import fs from 'node:fs';
|
|
import { spawnSync } from 'node:child_process';
|
|
import { MOJO_VERSION, GODBOLT_BASE, GODBOLT_COMPILER, MOJO_SOURCES } from '../dist/mojo/generated/pin.js';
|
|
import { normalize } from '../dist/core/state.js';
|
|
import { MOJO_KEYWORDS, MOJO_SOFT_KEYWORDS, MOJO_REMOVED_WORDS, MOJO_DECORATORS, MOJO_PRELUDE, MOJO_PUNCTUATION } from '../dist/mojo/generated/tokens.js';
|
|
|
|
const pin = JSON.parse(fs.readFileSync(new URL('../mojo/pin.json', import.meta.url), 'utf8'));
|
|
|
|
test('the generated pin is mojo/pin.json', () => {
|
|
assert.equal(MOJO_VERSION, pin.mojo.version);
|
|
assert.equal(GODBOLT_BASE, pin.godbolt.base);
|
|
assert.equal(GODBOLT_COMPILER, pin.godbolt.compiler);
|
|
assert.deepEqual(Object.keys(MOJO_SOURCES).sort(), Object.keys(pin.sources).sort());
|
|
});
|
|
|
|
test('the generated words are the 1.1.0 language', () => {
|
|
for (const w of ['def', 'struct', 'trait', 'var', 'ref', 'comptime', 'raise', 'Self']) assert.ok(MOJO_KEYWORDS.includes(w), w);
|
|
assert.ok(!MOJO_KEYWORDS.includes('fn'), 'fn is removed in 1.1.0, not a keyword to highlight as valid');
|
|
assert.equal(MOJO_REMOVED_WORDS.fn, 'def');
|
|
assert.equal(MOJO_REMOVED_WORDS.read, 'imm');
|
|
for (const w of ['imm', 'mut', 'out', 'deinit']) assert.ok(MOJO_SOFT_KEYWORDS.includes(w), w);
|
|
for (const d of ['fieldwise_init', 'always_inline', 'export']) assert.ok(MOJO_DECORATORS.includes(d), d);
|
|
for (const n of ['Int', 'String', 'List', 'Dict', 'Optional', 'print', 'input', 'range', 'len']) assert.ok(MOJO_PRELUDE.includes(n), n);
|
|
assert.ok(MOJO_PUNCTUATION.indexOf('**=') < MOJO_PUNCTUATION.indexOf('**'), 'longest spellings first');
|
|
});
|
|
|
|
const py = spawnSync('python3', ['--version']);
|
|
test('the corpus pipeline tests pass (python3)', { skip: py.status !== 0 ? 'python3 is not installed' : false }, () => {
|
|
const r = spawnSync('python3', ['-m', 'unittest', 'discover', '-s', 'scripts/mojo/tests'], { encoding: 'utf8', cwd: new URL('..', import.meta.url).pathname });
|
|
assert.equal(r.status, 0, r.stderr.split('\n').slice(-30).join('\n'));
|
|
});
|
|
|
|
/* ---------------------------------------------------------------------------
|
|
* Stage 1: the Mojo pane. Wherever possible these use godbolt's real answers
|
|
* (mojo/golden), so they test against what the compiler said, not what we assume.
|
|
* ------------------------------------------------------------------------- */
|
|
import { tokenizeMojo } from '../dist/mojo/tokenize.js';
|
|
import { removedSyntax } from '../dist/mojo/check.js';
|
|
import { nextIndent } from '../dist/mojo/editing.js';
|
|
import { mojoRequestBody, mojoRequestUrl, normaliseAnswer, compilerDiagnostics, listingFunctions, demangle, sourceOf as lineSource, BUILD_ARGS } from '../dist/services/godbolt.js';
|
|
import { intelLine, labelMap, forSimulator } from '../dist/mojo/att.js';
|
|
import { parseAsm, runAsmProgram } from '../dist/services/asm.js';
|
|
import { Session } from '../dist/core/session.js';
|
|
import { runMojo } from '../dist/mojo/interp.js';
|
|
import { parseModule } from '../dist/mojo/parse.js';
|
|
import { PROBLEMS, stateForProblem } from '../dist/core/catalog.js';
|
|
import { MOJO_PROGRAMS } from '../dist/mojo/generated/programs.js';
|
|
|
|
const ROOT = new URL('..', import.meta.url).pathname;
|
|
const goldens = fs.readdirSync(new URL('../mojo/golden/', import.meta.url)).filter((f) => /^[0-9a-f]{64}\.json$/.test(f))
|
|
.map((f) => JSON.parse(fs.readFileSync(new URL(`../mojo/golden/${f}`, import.meta.url), 'utf8')));
|
|
/** godbolt's answer for one corpus item, rebuilt as the raw JSON the browser receives. */
|
|
function answerFor(item) {
|
|
const g = goldens.find((x) => x.firstItem === item);
|
|
assert.ok(g, `no golden for ${item}; run pixi run mojo-verify`);
|
|
const source = (a) => a.line ? { line: a.line, ...('file' in a ? { file: a.file } : {}), ...('main' in a ? { mainsource: a.main } : {}) } : null;
|
|
return { golden: g, raw: { ...g.raw, asm: (g.result.asm || []).map((a) => ({ text: a.text, source: source(a) })) } };
|
|
}
|
|
const sourceOf = (item) => fs.readFileSync(new URL(`../mojo/${item.replace('probes/', 'probes/')}.mojo`, import.meta.url), 'utf8');
|
|
function applyFix(src, f) {
|
|
const lines = src.split('\n'); const l = lines[f.line - 1];
|
|
lines[f.line - 1] = l.slice(0, f.col - 1) + f.text + l.slice(f.endCol == null ? l.length : f.endCol - 1);
|
|
return lines.join('\n');
|
|
}
|
|
const kinds = (line) => tokenizeMojo(line)[0].filter((t) => t.cls).map((t) => `${t.cls.replace('tok-', '')}:${t.text}`);
|
|
|
|
test('highlighting agrees with the Mojo grammar\'s own tests', () => {
|
|
const { cases } = JSON.parse(fs.readFileSync(new URL('../mojo/corpus/syntax/cases.json', import.meta.url), 'utf8'));
|
|
assert.ok(cases.length >= 20);
|
|
for (const c of cases) {
|
|
const toks = tokenizeMojo(c.text)[0];
|
|
let at = 0; const hit = toks.find((t) => { const ok = c.start >= at && c.start < at + t.text.length; at += t.text.length; return ok; });
|
|
const isKeyword = hit?.cls === 'tok-keyword';
|
|
const wantsKeyword = c.scopes.some((x) => /^(keyword|storage)\./.test(x));
|
|
// syntax 1.1.0 removed (alias) is not a keyword any more, whatever the grammar says
|
|
const expected = c.dialect?.older ? false : wantsKeyword;
|
|
assert.equal(isKeyword, expected, `${c.file}:${c.line} ${JSON.stringify(c.slice)} in ${JSON.stringify(c.text)} wants ${c.scopes.join(' ')}`);
|
|
}
|
|
});
|
|
|
|
test('highlighting: docstrings span lines, ^ transfers or XORs, conventions are ownership words', () => {
|
|
const doc = tokenizeMojo('def f():\n """One\n two."""\n return 1');
|
|
assert.ok(doc[1].some((t) => t.cls === 'tok-string') && doc[2].every((t) => !t.text.trim() || t.cls === 'tok-string'));
|
|
assert.ok(doc[3].some((t) => t.cls === 'tok-keyword' && t.text === 'return'));
|
|
assert.ok(kinds('consume(b^, c)').includes('transfer:^'));
|
|
assert.ok(kinds('var b = a^').includes('transfer:^'));
|
|
assert.ok(kinds('var m = f ^ 0xFF').includes('op:^'));
|
|
assert.deepEqual(kinds('def g(mut self, var x: Int, deinit other: T):').filter((k) => k.startsWith('convention')), ['convention:mut', 'convention:var', 'convention:deinit']);
|
|
assert.ok(kinds(' var name: String').includes('keyword:var'), 'a field declaration is not a convention');
|
|
assert.ok(kinds('def h(read x: Int):').includes('error:read'));
|
|
assert.ok(kinds('var data = file.read()').includes('name:read'), 'read is only an error as a convention');
|
|
assert.ok(kinds('fn old():').includes('error:fn'));
|
|
assert.ok(kinds('@fieldwise_init').includes('label:fieldwise_init'));
|
|
assert.ok(kinds('print(t"n={n * 2}")').includes('name:n'), 't-string interpolations highlight as code');
|
|
});
|
|
|
|
test('removed syntax is caught before compiling, in the compiler\'s words, with its fix', () => {
|
|
const src = 'fn main():\n show(1)\n\ndef show(read x: Int):\n print(x)\n';
|
|
const ds = removedSyntax(src);
|
|
assert.deepEqual(ds.map((d) => [d.line, d.message]), [[1, "'fn' has been removed; use 'def' instead"], [4, "'read' was removed; use 'imm'"]]);
|
|
let fixed = src;
|
|
for (const d of [...ds].reverse()) fixed = applyFix(fixed, d.fix);
|
|
assert.equal(fixed, 'def main():\n show(1)\n\ndef show(imm x: Int):\n print(x)\n');
|
|
assert.deepEqual(removedSyntax(fixed), []);
|
|
// and it is exactly what the real compiler said about the fn probe
|
|
const real = compilerDiagnostics(goldens.find((g) => g.firstItem === 'probes/dialect-fn').result.buildStderr);
|
|
assert.equal(real[0].message, ds[0].message);
|
|
const realRead = compilerDiagnostics(goldens.find((g) => g.firstItem === 'probes/dialect-read').result.buildStderr);
|
|
assert.equal(realRead[0].message, ds[1].message);
|
|
});
|
|
|
|
test('Enter indents after a block opener and dedents after return', () => {
|
|
assert.equal(nextIndent('def f(x: Int) -> Int:', ' '), ' ');
|
|
assert.equal(nextIndent(' comptime for i in range(3):', ' '), ' ');
|
|
assert.equal(nextIndent(' return x # done', ' '), ' ');
|
|
assert.equal(nextIndent(' var y = 1', ' '), ' ');
|
|
});
|
|
|
|
test('the Compile button asks godbolt exactly what the corpus harness asks', () => {
|
|
const src = 'def main():\n print(input())\n';
|
|
const py = spawnSync('python3', ['-c', `import sys, json; sys.path.insert(0, 'scripts/mojo')\nfrom mojocorpus import godbolt\nprint(json.dumps(godbolt.request_body(${JSON.stringify(src)}, 'both', '7\\n', '-O0')))`], { encoding: 'utf8', cwd: ROOT });
|
|
if (py.status !== 0) return; // no python3 here; the pipeline tests cover the Python side
|
|
assert.deepEqual(mojoRequestBody(src, '7\n', '-O0'), JSON.parse(py.stdout));
|
|
assert.equal(mojoRequestUrl(), `${pin.godbolt.base}/api/compiler/${pin.godbolt.compiler}/compile`);
|
|
});
|
|
|
|
test('compiler errors land on their lines, and its fix-its become one-click fixes', () => {
|
|
// The probes were asked in run mode, where errors come in buildResult; the button's mode puts them in stderr. Same text.
|
|
const stderrOf = (item) => goldens.find((g) => g.firstItem === item).result.buildStderr;
|
|
const fnDiag = compilerDiagnostics(stderrOf('probes/dialect-fn'));
|
|
assert.equal(fnDiag[0].line, 3);
|
|
assert.equal(applyFix(sourceOf('probes/dialect-fn'), fnDiag[0].fix).split('\n')[2], 'def main():');
|
|
const copy = compilerDiagnostics(stderrOf('probes/check-implicit-copy'));
|
|
const err = copy.find((d) => d.severity === 'error');
|
|
assert.match(err.message, /cannot be implicitly copied/);
|
|
assert.equal(err.origin, 'the Mojo 1.1.0 compiler');
|
|
const fixes = err.question.options.map((o) => applyFix(sourceOf('probes/check-implicit-copy'), o).split('\n')[err.line - 1].trim());
|
|
assert.deepEqual(fixes, ['var b = a^', 'var b = a.copy()']);
|
|
const alias = compilerDiagnostics(stderrOf('probes/check-aliasing'));
|
|
assert.match(alias[0].message, /aliasing values passed mutably to 's' argument/);
|
|
assert.ok(alias.slice(1).every((d) => d.severity === 'note'));
|
|
assert.deepEqual(compilerDiagnostics(stderrOf('probes/check-use-after-move')).map((d) => [d.severity, d.message]),
|
|
[['error', "use of uninitialized value 'a'"], ['note', "'a' declared here"]]);
|
|
});
|
|
|
|
test('the real listing of add, translated to Intel, runs on the model machine', () => {
|
|
const ans = normaliseAnswer(answerFor('probes/godbolt-opt-O0').raw);
|
|
const fns = listingFunctions(ans.asm);
|
|
assert.deepEqual(fns.filter((f) => f.kind === 'user').map((f) => f.name), ['add(Int, Int)', 'main()']);
|
|
assert.deepEqual(fns.filter((f) => f.kind === 'entry').map((f) => f.name), ['main']);
|
|
const add = fns.find((f) => f.name === 'add(Int, Int)');
|
|
const sim = forSimulator(fns, add);
|
|
assert.deepEqual(sim.source.split('\n').filter((l) => l.trim() && !l.trim().endsWith(':') && !l.includes(';')).map((l) => l.trim()), ['mov rax, rdi', 'add rax, rsi', 'ret']);
|
|
const run = runAsmProgram(parseAsm(sim.source), { setup: 'rdi = 2\nrsi = 3' });
|
|
assert.ok(!run.error, run.error?.message);
|
|
assert.equal(run.output.at(-1), 'rax = 5 (0x5)');
|
|
// main calls print, which the model machine cannot run: it assembles with a stub for print, steps up to the call, and stops there saying so
|
|
const main = forSimulator(fns, fns.find((f) => f.name === 'main()'));
|
|
assert.ok(main.refusedCalls.some((c) => /print/.test(c)));
|
|
const mp = parseAsm(main.source);
|
|
assert.ok(mp.ok, mp.diagnostics.map((d) => d.message).join('; '));
|
|
const mr = runAsmProgram(mp, { setup: main.data.join('\n') });
|
|
assert.ok(mr.count > 5, 'main steps its own instructions before the call');
|
|
assert.match(mr.error.message, /print: standard library, not on the model machine/);
|
|
});
|
|
|
|
test('the optimized listing keeps no function of the user\'s, and one request returned listing and output', () => {
|
|
const ans = normaliseAnswer(answerFor('probes/godbolt-asm-and-run').raw);
|
|
assert.equal(ans.stdout, '5');
|
|
assert.ok(ans.asm.length > 0 && ans.compiled && ans.executed && ans.exitCode === 0);
|
|
assert.deepEqual(listingFunctions(ans.asm).filter((f) => f.kind === 'user'), []);
|
|
});
|
|
|
|
test('AT&T to Intel: operands reverse, sizes become ptr, names become labels', () => {
|
|
const m = labelMap([{ label: '"example::add(::SIMD[DType.int, 1],::SIMD[DType.int, 1])"', name: 'add(Int, Int)', kind: 'user', lines: [] }]);
|
|
const cases = [
|
|
['movq %rdi, 16(%rsp)', 'mov qword ptr [rsp + 16], rdi'],
|
|
['movl $0, -4(%rbp)', 'mov dword ptr [rbp - 4], 0'],
|
|
['movzbl (%rdi,%rcx), %eax', 'movzx eax, byte ptr [rdi + rcx*1]'],
|
|
['movslq 8(%rsi,%rax,4), %rdx', 'movsxd rdx, dword ptr [rsi + rax*4 + 8]'],
|
|
['leaq static_string_ab(%rip), %rax', 'lea rax, [rip + static_string_ab]'],
|
|
['imulq $3, %rsi, %rax', 'imul rax, rsi, 3'],
|
|
['cmovneq %rcx, %rax', 'cmovne rax, rcx'],
|
|
['callq "example::add(::SIMD[DType.int, 1],::SIMD[DType.int, 1])"@PLT', 'call add'],
|
|
['jne .LBB2_2', 'jne .LBB2_2'],
|
|
['retq', 'ret'],
|
|
['cqto', 'cqo'],
|
|
['.cfi_startproc', null],
|
|
];
|
|
for (const [att, intel] of cases) assert.equal(intelLine(att, m), intel, att);
|
|
assert.equal(demangle('"example::add(::SIMD[DType.int, 1],::SIMD[DType.float64, 1])"'), 'add(Int, Float64)');
|
|
});
|
|
|
|
test('the session keeps each answer with the source it answered, and says when it is stale', async () => {
|
|
const s = new Session(normalize({ v: 3, lang: 'mojo', sources: { mojo: 'fn main():\n print(1)\n' } }));
|
|
assert.equal(s.diagnostics('mojo')[0].id, 'removed-syntax');
|
|
const stderr = goldens.find((g) => g.firstItem === 'probes/dialect-fn').result.buildStderr;
|
|
let asked = null;
|
|
const fake = async (source, opts) => { asked = { source, ...opts }; return { answer: { compiled: false, buildStderr: stderr.replace('<source>:3:', '<source>:1:'), executed: false, exitCode: null, stdout: '', stderr: '', timedOut: false, asm: [] }, error: null, seconds: 0.1 }; };
|
|
const rec = await s.compile(fake);
|
|
assert.equal(asked.level, 'O0');
|
|
assert.equal(rec.diagnostics[0].origin, 'the Mojo 1.1.0 compiler');
|
|
assert.equal(s.diagnostics('mojo')[0].id, 'compiler', 'the compiler\'s answer replaces Programming+\'s own finding');
|
|
assert.equal(s.lastCompile().fresh, true);
|
|
s.setSource('mojo', 'def main():\n print(1)\n');
|
|
assert.equal(s.lastCompile().fresh, false);
|
|
assert.deepEqual(s.diagnostics('mojo'), [], 'an answer for older source is not shown on the new lines');
|
|
const failed = await s.compile(async () => ({ answer: null, error: 'Could not reach godbolt.org (offline).', seconds: 0 }));
|
|
assert.equal(failed.outcome.error, 'Could not reach godbolt.org (offline).');
|
|
});
|
|
|
|
test('Compile & run goes through the compile backend, and the record names the service that answered', async () => {
|
|
const { compileBackend, setCompileBackend, explorerBackend, GODBOLT } = await import('../dist/services/compile-backend.js');
|
|
assert.equal(compileBackend(), GODBOLT);
|
|
assert.equal(GODBOLT.host, 'godbolt.org');
|
|
assert.equal(GODBOLT.compiler, 'mojo_1_1_0');
|
|
const asked = [];
|
|
const fake = { id: 'fake', host: 'checker.example', compiler: 'mojo_1_1_0', compile: async (source, opts) => { asked.push({ source, ...opts }); return { answer: null, error: 'checker.example did not answer in time.', seconds: 0 }; } };
|
|
const prev = setCompileBackend(fake);
|
|
try {
|
|
const s = new Session(normalize({ v: 3, lang: 'mojo', sources: { mojo: 'def main():\n print(7)\n' } }));
|
|
const rec = await s.compile();
|
|
assert.equal(asked.length, 1);
|
|
assert.equal(asked[0].source, 'def main():\n print(7)\n');
|
|
assert.equal(asked[0].level, 'O0');
|
|
assert.equal(rec.host, 'checker.example');
|
|
} finally {
|
|
setCompileBackend(prev);
|
|
}
|
|
assert.equal(compileBackend(), GODBOLT);
|
|
// a Compiler Explorer at another address speaks the same API and names itself in its messages
|
|
const elsewhere = explorerBackend('local', { base: 'http://127.0.0.1:9', compiler: 'mojo_1_1_0', host: 'the local checker' });
|
|
const out = await elsewhere.compile('def main():\n pass\n', { timeoutMs: 2000 });
|
|
assert.equal(out.answer, null);
|
|
assert.match(out.error, /the local checker/);
|
|
});
|
|
|
|
test('godbolt\'s answer outlives the session: a new one for the same source finds it without asking again', async () => {
|
|
const { forgetCompiles } = await import('../dist/core/compiles.js');
|
|
forgetCompiles();
|
|
const src = 'def main():\n print(42)\n';
|
|
const answer = { compiled: true, buildStderr: '', executed: true, exitCode: 0, stdout: '42\n', stderr: '', timedOut: false, asm: [{ text: '"example::main()":' }, { text: ' retq' }] };
|
|
let asks = 0;
|
|
const first = new Session(normalize({ v: 3, lang: 'mojo', sources: { mojo: src } }));
|
|
await first.compile(async () => { asks++; return { answer, error: null, seconds: 0.1 }; });
|
|
// a change of problem, a reload: a new session for the same program
|
|
const again = new Session(normalize({ v: 3, lang: 'mojo', sources: { mojo: src } }));
|
|
const last = again.lastCompile();
|
|
assert.ok(last, 'the answer is there before anyone presses Compile');
|
|
assert.equal(last.fresh, true);
|
|
assert.equal(last.record.outcome.answer.stdout, '42\n');
|
|
assert.equal(last.record.functions.length, 1, 'the listing is split into functions again');
|
|
assert.equal(asks, 1);
|
|
// a different program does not see it, and comes back to it once the source is the same again
|
|
const other = new Session(normalize({ v: 3, lang: 'mojo', sources: { mojo: 'def main():\n print(7)\n' } }));
|
|
assert.equal(other.lastCompile(), null);
|
|
other.setSource('mojo', src);
|
|
assert.equal(other.lastCompile()?.fresh, true, 'editing back to a compiled source brings its answer back');
|
|
// a failure to reach godbolt is not kept
|
|
forgetCompiles();
|
|
const offline = new Session(normalize({ v: 3, lang: 'mojo', sources: { mojo: src } }));
|
|
await offline.compile(async () => ({ answer: null, error: 'offline', seconds: 0 }));
|
|
assert.equal(new Session(normalize({ v: 3, lang: 'mojo', sources: { mojo: src } })).lastCompile(), null);
|
|
forgetCompiles();
|
|
});
|
|
|
|
test('an exercise\'s Mojo is a verified program from the corpus', () => {
|
|
assert.equal(MOJO_PROGRAMS['count-base'].verified, true);
|
|
const p = PROBLEMS.find((x) => x.id === 'count-base');
|
|
assert.equal(p.mojo.id, 'count-base');
|
|
assert.equal(p.sources.mojo, MOJO_PROGRAMS['count-base'].source);
|
|
assert.ok(stateForProblem(p).sources.mojo, 'the state carries the exercise\'s Mojo');
|
|
for (const q of PROBLEMS) if (q.mojo) assert.ok(MOJO_PROGRAMS[q.mojo.id], `${q.id} names a Mojo program that does not exist`);
|
|
// every exercise with pseudo has its Mojo too, so the Mojo/pseudo switch always has both sides
|
|
for (const q of PROBLEMS) if (q.sources.pseudo) assert.ok(q.mojo, `${q.id} has pseudo but no Mojo program`);
|
|
});
|
|
|
|
test('each exercise\'s Mojo prints, in the interpreter, what the exercise expects of the pseudo', () => {
|
|
const norm = (s) => s.trim().replace(/^"(.*)"$/, '$1').replace(/^TRUE$/, 'True').replace(/^FALSE$/, 'False');
|
|
for (const q of PROBLEMS) {
|
|
if (!q.mojo || !q.expect) continue;
|
|
const r = runMojo(parseModule(q.mojo.source), q.mojo.source, { maxSteps: 200000, record: false });
|
|
if (r.refused) continue; // the stepper says why; the compiler is the judge
|
|
assert.equal(norm(r.stdout), norm(q.expect), `${q.id}: its Mojo printed ${JSON.stringify(r.stdout)}`);
|
|
}
|
|
});
|
|
|
|
test('the Compile button builds with exactly the flags a passing probe verified', () => {
|
|
const text = fs.readFileSync(new URL('../mojo/probes/godbolt-compile-button.mojo', import.meta.url), 'utf8');
|
|
const args = /^# args: (.*)$/m.exec(text)[1].trim();
|
|
assert.equal(BUILD_ARGS.O0, args, 'change the probe and re-verify before changing the button');
|
|
const g = goldens.find((x) => x.firstItem === 'probes/godbolt-compile-button');
|
|
if (!g) return; // a checkout without that golden; the corpus tests cover it
|
|
assert.equal(g.result.compiled && g.result.executed && g.result.exitCode, 0);
|
|
assert.equal(g.result.stdout.trim(), '5');
|
|
});
|
|
|
|
test('a source line is the program\'s only when godbolt says it comes from the program\'s file', () => {
|
|
assert.deepEqual(lineSource({ line: 10, file: null }), { line: 10 });
|
|
assert.deepEqual(lineSource({ line: 10 }), { line: 10 });
|
|
assert.deepEqual(lineSource({ line: 10, file: '/app/example.mojo' }), { line: 10 });
|
|
assert.deepEqual(lineSource({ line: 10, file: 'other', mainsource: true }), { line: 10 });
|
|
assert.deepEqual(lineSource({ line: 218, file: '/opt/mojo/lib/std/io/io.mojo' }), { line: null, from: { file: 'io.mojo', line: 218 } });
|
|
assert.deepEqual(lineSource({ line: 218, file: null, mainsource: false }), { line: null, from: { file: '?', line: 218 } });
|
|
assert.deepEqual(lineSource(null), { line: null });
|
|
});
|
|
|
|
test('with line tables, the program\'s own instructions point at the program\'s own lines', (t) => {
|
|
const g = goldens.find((x) => x.firstItem === 'probes/godbolt-compile-button');
|
|
if (!g) return t.skip('no golden for probes/godbolt-compile-button');
|
|
const marked = (g.result.asm || []).some((a) => 'file' in a || 'main' in a);
|
|
if (!marked) return t.skip("this golden predates file marking; run: pixi run mojo-verify-item 'probes/godbolt-*'");
|
|
const fns = listingFunctions(normaliseAnswer(answerFor('probes/godbolt-compile-button').raw).asm);
|
|
const lines = (name) => [...new Set(fns.find((f) => f.name === name).lines.map((l) => l.line).filter((x) => x != null))];
|
|
assert.deepEqual(lines('add(Int, Int)'), [5, 6]);
|
|
assert.deepEqual(lines('main()').sort(), [10, 9].sort());
|
|
assert.ok(fns.find((f) => f.name === 'main()').lines.some((l) => l.from && l.from.file.endsWith('.mojo')), 'print\'s inlined defaults are marked as library code');
|
|
});
|
|
|
|
test('Stage 6: a function\'s calls into the standard library are named, from the real listing', async () => {
|
|
const g = goldens.find((x) => x.firstItem === 'probes/godbolt-compile-button');
|
|
if (!g) return;
|
|
const { libraryCalls } = await import('../dist/ui/views/compiler.js').catch(() => ({ libraryCalls: null }));
|
|
if (!libraryCalls) return; // the view needs a DOM to load; covered by the browser check
|
|
const fns = listingFunctions(normaliseAnswer(answerFor('probes/godbolt-compile-button').raw).asm);
|
|
assert.deepEqual(libraryCalls(fns.find((f) => f.name === 'main()')), ['print (std.io.io)']);
|
|
assert.deepEqual(libraryCalls(fns.find((f) => f.name === 'add(Int, Int)')), []);
|
|
});
|
|
|
|
test('Stage 6b: code sent to the x86-64 pane keeps each instruction\'s Mojo line', async () => {
|
|
const g = goldens.find((x) => x.firstItem === 'probes/godbolt-compile-button');
|
|
if (!g) return;
|
|
const { forSimulator } = await import('../dist/mojo/att.js');
|
|
const fns = listingFunctions(normaliseAnswer(answerFor('probes/godbolt-compile-button').raw).asm);
|
|
const { source } = forSimulator(fns, fns.find((f) => f.name === 'add(Int, Int)'));
|
|
const marks = source.split('\n').filter((l) => /; mojo line \d+/.test(l)).map((l) => Number(/; mojo line (\d+)/.exec(l)[1]));
|
|
assert.deepEqual([...new Set(marks)], [5, 6], 'the instructions of add carry lines 5 and 6');
|
|
});
|
|
|
|
test('Stage 6b: comparing builds does not replace the build the student chose', async () => {
|
|
const { Session } = await import('../dist/core/session.js');
|
|
const { normalize } = await import('../dist/core/state.js');
|
|
const s = new Session(normalize({ v: 3, sources: { mojo: 'def main():\n print(1)\n' }, lang: 'mojo' }));
|
|
const fake = (level) => async () => ({ ok: true, answer: { compiled: true, executed: true, exitCode: 0, stdout: level, stderr: '', buildStderr: '', asm: [] } });
|
|
await s.compile(fake('O0'));
|
|
await s.compile(fake('default'), 'default');
|
|
assert.equal(s.lastCompile().record.level, 'O0', 'the view keeps the chosen level');
|
|
assert.ok(s.compileAt(s.source('mojo'), 'default'), 'the other build is there to compare with');
|
|
});
|