-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathreply.js
More file actions
57 lines (55 loc) · 1.96 KB
/
reply.js
File metadata and controls
57 lines (55 loc) · 1.96 KB
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
49
50
51
52
53
54
55
56
57
"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.postRun = () => {};
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 = [];
repl.postRun();
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 : () => {
const a = eval(tedea(repl.eval_in));
repl.eval_out = a instanceof Uint8Array ? a : teen(a);
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.fetch_module = async function(url) {
const response = await fetch(url);
if (!response.ok) throw new Error(`Response status: ${response.status}`);
const bl = new Uint8Array(await (await response.blob()).arrayBuffer());
repl.runBlob("chat_module", [], bl);
}
repl.reset = async function() {
repl.instance = await WebAssembly.instantiate(repl.module, importObj);
repl.run("chat_new", ["Main"], "");
}
repl.run("chat_new", ["Main"], "");
return repl;
}