-
Notifications
You must be signed in to change notification settings - Fork 16
/
reply.js
48 lines (46 loc) · 1.59 KB
/
reply.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"use strict";
function teen(s) { return (new TextEncoder()).encode(s); }
function tede(s) { return (new TextDecoder()).decode(s); }
function tedea(a) { return (new TextDecoder()).decode(Uint8Array.from(a)); }
async function mkRepl() {
const repl = {};
repl.runBlob = function(f, args, blob) {
repl.args = args.map(teen);
repl.buf = [];
repl.out = [];
repl.inp = blob;
repl.cursor = 0;
repl.instance.exports[f]();
repl.eval_in = [], repl.eval_out = [];
return {
buf : repl.buf.map(tedea),
out : tedea(repl.out),
}
};
repl.run = function(f, args, s) { return repl.runBlob(f, args, teen(s)); }
const importObj = {env:
{ putchar: c => repl.out.push(c)
, eof : () => repl.cursor == repl.inp.length
, getchar: () => repl.inp[repl.cursor++]
, nextout: () => { repl.buf.push(repl.out); repl.out = []; }
, argc : () => repl.args.length
, argvlen: i => repl.args[i].length
, argvat : (i, j) => repl.args[i][j]
, eval_put : c => repl.eval_in.push(c)
, eval_run : () => {
repl.eval_out = teen(eval(tedea(repl.eval_in)));
repl.eval_in = [];
}
, eval_size: () => repl.eval_out.length
, eval_at: i => repl.eval_out[i]
}};
const p = await WebAssembly.instantiateStreaming(fetch('../compiler/doh.wasm'), importObj);
repl.instance = p.instance;
repl.module = p.module;
repl.reset = async function() {
repl.instance = await WebAssembly.instantiate(repl.module, importObj);
repl.run("chat_new", ["Main"], "");
}
repl.run("chat_new", ["Main"], "");
return repl;
}