Files
ziran/tests/playground_runtime.cjs
T
waozi cb0a762fd4 Migrate to one ZX format and delete the old route
zi2zx bundle and ziran run always write and run revision-28 executable
ZX. The semantic bundle reader and writer, the portable VM, the
TinyCC-compiled and linked native bundles with the vendored TinyCC,
bundle libraries, the VM profile, --vm, BundleInstantiatePortable,
libziran-vm.a and the ZIR-based Wasm lowering with zi2wasm are removed;
the Wasm tier, the embedding API and every tool run revision 28. Tests
of the removed VM internals and libraries are removed, and the rest run
on revision 28. Engine problems read as the diagnostics users rely on.
2026-11-14 14:03:01 -03:00

64 lines
3.3 KiB
JavaScript

const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const createPlayground = require(process.env.ZIRAN_PLAYGROUND_RUNTIME || '../site/assets/playground-runtime.js');
async function main() {
let output = [], diagnostics = [], stages = [];
global.postMessage = message => stages.push(message.stage);
const runtime = await createPlayground({
print: line => output.push(line),
printErr: line => diagnostics.push(line)
});
function run(source) {
output = []; diagnostics = []; stages = [];
runtime.FS.writeFile('/playground.zi', source);
const status = runtime.ccall('RunSource', 'number', [], []);
return {status, output: output.join('\n'), diagnostics: diagnostics.join('\n'), stages};
}
const examples = [
['hello', 'Hello, World!'],
['score', '42'],
['text_demo', '1'],
['fizzbuzz', null],
['functions', null]
];
for (const [name, expected] of examples) {
const result = run(fs.readFileSync(path.join(__dirname, '../examples', name + '.zi'), 'utf8'));
assert.equal(result.status, 0, name + ': ' + result.diagnostics);
if (expected !== null) assert.equal(result.output, expected, name);
assert.deepEqual(result.stages, ['check', 'build', 'run']);
}
assert.equal(run('Answer :: () -> s64 { return 9007199254740993; }').output, '9007199254740993');
for (const [type, value] of [['s8', '-128'], ['s16', '-32768'], ['s32', '-2147483648'],
['s64', '-9223372036854775808'], ['u8', '255'], ['u16', '65535'],
['u32', '4294967295'], ['u64', '18446744073709551615']]) {
const result = run(`Answer :: () -> ${type} { return ${value}; }`);
assert.equal(result.status, 0, type + ': ' + result.diagnostics);
assert.equal(result.output, value, type);
}
assert.equal(run('Answer :: () -> u64 { return (cast(u64)1 << 63) | cast(u64)123; }').output,
'9223372036854775931');
assert.equal(run('main :: () -> u64 { return 18446744073709551615; }').output,
'18446744073709551615');
assert.equal(run('VALUE :: #run 1099511627776;\nAnswer :: () -> s64 { return VALUE; }').output, '1099511627776');
assert.equal(run('main :: () { print("自然\\n"); }').output, '自然');
assert.equal(run('main :: () { print("%\\n", 3.14159); }').output, '3.14159');
const invalid = run('Answer :: () -> s32 { return missing_value; }');
assert.notEqual(invalid.status, 0);
assert.match(invalid.diagnostics, /missing_value/);
assert.equal(invalid.output, '');
const syntax = run('Tag :: enum s32 { First; Last }\nmain :: () {');
assert.notEqual(syntax.status, 0);
assert.match(syntax.diagnostics, /unterminated/);
const noEntry = run('Other :: () -> s32 { return 1; }');
assert.notEqual(noEntry.status, 0);
assert.match(noEntry.diagnostics, /entry procedure/);
const bounded = run('main :: () { while true { } }');
assert.notEqual(bounded.status, 0);
assert.match(bounded.diagnostics, /portable execution failed: stopped after 1000000 steps/);
assert.equal(run('Answer :: () -> s32 { return 17; }').output, '17', 'A failed run must not poison the next run');
console.log('Playground runtime: examples, imports, 64-bit integers, compile-time values, UTF-8, floats, diagnostics, execution limits, and recovery passed');
}
main().catch(error => { console.error(error); process.exitCode = 1; });