-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworker.js
234 lines (222 loc) · 5.9 KB
/
worker.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
var output = null;
var stdin = "", stdout = "", yielded = false;
var mod, inst;
var loop = null;
var MoreData = {};
function crankNL() {
if(stdin.indexOf("\n") != -1) crank();
}
function crank(once) {
if(inst == null || loop == null) return;
try {
while(stdin.length > 0 || once) {
once = false;
if(yielded) {
if(stdin.length == 0) break;
var val = inst.exports.tl_new_int(interp, stdin.charCodeAt(0));
stdin = stdin.substr(1);
inst.exports.tl_wasm_values_push(interp, val);
yielded = false;
}
var res = loop.next();
if(res.done) {
sysprint("Main loop exited.\n");
loop = null;
return;
}
if(res.value === MoreData) {
yielded = true;
continue;
}
throw new Error("Not sure how to handle suspension of value " + res.value);
}
} catch(e) {
sysprint("A fatal error occurred: " + e + "\n");
throw e;
}
}
function eprint(x) {
flush();
postMessage({type: "stderr", text: x});
}
function sysprint(x) {
flush();
postMessage({type: "system", text: x});
}
function print(x) {
stdout += x;
if(stdout.indexOf("\n") != -1) {
flush();
}
}
function flush() {
if(stdout.length > 0) {
postMessage({type: "stdout", text: stdout});
stdout = "";
}
}
onmessage = function(e) {
switch(e.data.type) {
case "keydown":
var c = null;
if(e.data.key.length == 1) {
c = e.data.key;
}
if(e.data.key == "Enter") {
c = "\n";
}
if(e.data.key == "Backspace" && stdin.length > 0) {
stdin = stdin.substr(0, stdin.length - 1);
postMessage({type: "stdin", text: "\b"});
}
if(c != null) {
stdin += c;
postMessage({type: "stdin", text: c});
crankNL();
}
break;
case "paste":
if(e.data.text.length > 0) {
stdin += e.data.text;
postMessage({type: "stdin", text: e.data.text});
crankNL();
}
break;
default:
console.log("Unknown message:", e.data);
break;
}
}
var STDIN = 0, STDOUT = 1, STDERR = 2, EOF = -1, PAGE = 65536, PTR = 4;
var watermark = null, interp = null, expr = null, pm_name = null, _main_k_ref = null, main_then = null;
var running = true;
function PGOF(x) { return Math.floor(x / PAGE); }
var PM_NAME = "PRIME_MOVER";
var ENC = new TextEncoder(), DEC = new TextDecoder();
function init() {
// Set aside a tl_interp on the heap
watermark = inst.exports['__heap_base'].value
// As of this writing, sizeof(tl_interp) == 168
// TODO: find a way to export this information
interp = watermark; watermark += 256;
expr = watermark; watermark += PTR;
pm_name = watermark;
var mem = inst.exports['memory'];
var u8a = new Uint8Array(mem, pm_name);
var res = ENC.encodeInto(PM_NAME, u8a);
u8a[res.written] = 0;
watermark += res.written + 1;
sysprint("Interp at " + interp + ", expr at " + expr + ", pm_name at " + pm_name + " len " + res.written + " value " + DEC.decode(new Uint8Array(mem, pm_name, res.written)) + "\n");
// Align the watermark
if(watermark&7 != 0) watermark += (8 - 7&watermark);
var pw = PGOF(watermark), pgs = PGOF(mem.buffer.byteLength);
if(pw > pgs) { mem.grow(pw - pgs); }
// Find the prime mover
var _main_k = inst.exports._main_read_k;
var tbl = inst.exports.__indirect_function_table;
for(var i = 0; i < tbl.length; i++) {
if(tbl.get(i) === _main_k) {
_main_k_ref = i;
break;
}
}
if(_main_k_ref == null) {
print("Couldn't find prime mover! Refusing to start.");
return;
}
sysprint("Prime mover: " + _main_k_ref + "\n");
// The rest of this code mirrors tl's ordinary main()
inst.exports.tl_interp_init(interp);
loop = mainloop();
crank(true);
}
function* mainloop() {
var error;
while(running) {
inst.exports.tl_wasm_clear_state(interp);
inst.exports.tl_gc(interp); // Take advantage of the cleaner roots
eprint("> ");
flush();
if(main_then == null) {
main_then = inst.exports.tl_new_then(interp, _main_k_ref, 0, pm_name);
inst.exports.tl_wasm_make_permanent(main_then);
}
inst.exports.tl_push_apply(interp, 1, main_then, inst.exports.tl_wasm_get_env(interp));
inst.exports.tl_read(interp);
while(true) {
var res = inst.exports.tl_apply_next(interp)
if(res == 0) break;
switch(res) {
case 1: break;
case 2:
yield MoreData;
break;
default:
console.log("Unkown result:", res);
break;
}
}
error = inst.exports.tl_wasm_get_error(interp);
if(error != 0) {
eprint("Error: ");
inst.exports.tl_print(interp, error);
eprint("\n");
}
}
sysprint("Program exited.\n");
}
var imports = {
tl: {
fflush: function(fd) {
flush();
},
fputc: function(fd, c) {
print(String.fromCharCode(c));
},
fgetc: function(fd) {
throw new Error("Should never be called");
},
halt: function(code) {
sysprint("\nProcess exited with code " + code);
running = false;
// This isn't supposed to return, but the best we can
// do is promise not to call any other APIs
},
new_heap: function(min, whereptr, szptr) {
sysprint("new_heap @" + whereptr + " sz@" + szptr + "\n");
var amt = Math.max(min, 1024*PAGE);
if(watermark == null) {
watermark = inst.exports['__heap_base'].value;
}
var where = watermark;
watermark += amt;
var mem = inst.exports['memory'];
var pgs = PGOF(mem.buffer.byteLength);
var maxpg = PGOF(watermark + PAGE - 1);
if(maxpg > pgs) {
mem.grow(maxpg - pgs);
sysprint("memory grown to " + mem.buffer.byteLength + " bytes\n");
}
var dv = new DataView(mem.buffer);
dv.setInt32(whereptr, where, true);
dv.setInt32(szptr, amt, true);
sysprint("alloc'd " + dv.getInt32(szptr, true) + " bytes at " + dv.getInt32(whereptr, true) + "\n");
},
release_heap: function(where, sz) {}, // Don't care
},
};
// Work around issues with the MIME type provided by simple servers
fetch("../tl.wasm?" + Math.random()).then(resp =>
resp.arrayBuffer()
).then(buf =>
WebAssembly.instantiate(
buf,
imports,
)
).then(res => {
mod = res.module;
inst = res.instance;
setTimeout(init, 0);
}).catch(err => {
sysprint("LOADING ERROR: " + err + "\n");
});