// runs programs on the webassembly build (build/web/zc.wasm, from make web) and compares with the expected output. // usage: node tests/drivers/wasm_test.js [path/to/zc.wasm] const fs = require('fs'); const ZC = require('../../src/web/zc.js'); (async () => { const zc = await ZC.load(fs.readFileSync(process.argv[2] || __dirname + '/../../build/web/zc.wasm')); const cases = [ ['examples/hello.zc', 'hello, world!'], ['examples/fact.zc', '1101110101111100000000\n3628800\n'], ['examples/selfpatch.zc', 'ABC'], ['examples/lists.zc', '[0 1 4 9 16 25]\nlive: 7\nafter the block: 0\n'], ['examples/wiring.zc', '16 1234 25 4/9'], ['examples/struct_closure_opt.zc', '299\n'], ['examples/opt_test.zc', '1303\n'], ['tests/programs/basics.zc', '285 610 42 512\nhey 65 -3 -2 1024 -8\n12456\n757\n9 100-101\n5\nok\n'], ['tests/programs/replace_owner.zc', '98 7 9 0\n'], ]; let ok = 0, bad = 0; for (const [file, want] of cases) { const r = await zc.run(fs.readFileSync(__dirname + '/../../' + file, 'utf8')); if (r.output.replace(/\n+$/, '') === want.replace(/\n+$/, '')) { ok++; console.log('ok wasm ' + file); } else { bad++; console.log('FAIL wasm ' + file + ': got ' + JSON.stringify(r.output) + ' ' + r.messages); } } // an error is reported the way the interpreter reports it const r = await zc.run('fn main() { return undefined_name; }'); if (/undefined_name/.test(r.output)) { ok++; console.log('ok wasm compiler errors'); } else { bad++; console.log('FAIL wasm compiler errors: ' + JSON.stringify(r.output)); } console.log(ok + ' wasm checks passed, ' + bad + ' failed'); process.exitCode = bad ? 1 : 0; })();