// runs zc in a browser or in node: the microcore compiled to webassembly, // with the compiler genome inside. ZC.load(bytes) gives a machine, and // machine.run(source) compiles and runs a program the way `zy < file.zc` does, // returning what it printed. every run starts from a fresh instance. (function (root) { async function load(wasm) { if (typeof wasm === 'string') wasm = await (await fetch(wasm)).arrayBuffer(); const mod = await WebAssembly.compile(wasm); return { async run(source) { let out = [], msg = [], mem; const inst = await WebAssembly.instantiate(mod, { env: { js_putc: c => { out.push(c); }, js_msg: c => { msg.push(c); } } }); const x = inst.exports; const bytes = new TextEncoder().encode(source); if (bytes.length > x.wasm_input_cap()) throw new Error('program too long for the input buffer'); new Uint8Array(x.memory.buffer).set(bytes, x.wasm_input()); x.wasm_set_input(bytes.length); const status = x.wasm_run(); const dec = new TextDecoder(); return { output: dec.decode(new Uint8Array(out)), messages: dec.decode(new Uint8Array(msg)), status }; } }; } const ZC = { load }; if (typeof module !== 'undefined' && module.exports) module.exports = ZC; else root.ZC = ZC; })(typeof self !== 'undefined' ? self : this);