-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathworker.js
More file actions
652 lines (630 loc) · 21.7 KB
/
worker.js
File metadata and controls
652 lines (630 loc) · 21.7 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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
self.tsh = {};
// echo "jsdom = require('jsdom')" > jsdom-export.js; browserify jsdom-export.js -o ../libraries/jsdom.js
importScripts("libraries/jsdom.js");
importScripts("../modules.js");
self.tsh.symbols = {};
self.tsh.AbstractView = class {};
self.tsh.View = class extends self.tsh.AbstractView {
constructor(render, value) {
super();
this.render = render;
this.value = value;
}
toHtml() {
return this.render(this.value).toHtml();
}
};
self.tsh.Tag = class extends self.tsh.AbstractView {
constructor(tag) {
super();
this.tag = tag;
}
toHtml() {
return this.tag;
}
};
self.tsh.Task = class extends self.tsh.AbstractView {
constructor(run) {
super();
this.run = run;
}
then(body) {
return new self.tsh.Task(async world => {
let v1 = (await this.run(world)).result;
let v2 = (await body(v1).run(world)).result;
return {result: v2};
});
}
map(body) {
return new self.tsh.Task(async world => {
let v = (await this.run(world)).result;
return {result: body(v)};
});
}
catch(body) {
return new self.tsh.Task(async world => {
try {
let v1 = (await this.run(world)).result;
return {result: v1};
} catch(e) {
if(e === self.tsh.Task.abortedError) throw e;
let v2 = (await body(e).run(world)).result;
return {result: v2};
}
});
}
toHtml() {
return {_tag: "span", children: ["task"]};
}
};
self.tsh.Task.abortedError = new class {};
self.tsh.Task.simple = function(body) {
return new self.tsh.Task(world => new Promise((resolvePromise, rejectPromise) =>
body(x => resolvePromise({result: x}), rejectPromise, world)
));
};
self.tsh.Stream = class extends self.tsh.AbstractView {
constructor(open) {
super();
this.open = open;
}
then(body) {
let open = this.open;
return new self.tsh.Stream(async function*(world) {
let outer = open(world);
let o = await outer.next();
while(!o.done) {
let inner = body(o.value.result).open(world);
let i = await inner.next();
while(!i.done) {
yield {result: i.value.result};
i = await inner.next();
}
o = await outer.next();
}
});
}
filter(body) {
let open = this.open;
return new self.tsh.Stream(async function*(world) {
let outer = open(world);
let o = await outer.next();
while(!o.done) {
let v = o.value;
if(body(v.result)) yield v;
o = await outer.next();
}
});
}
map(body) {
let open = this.open;
return new self.tsh.Stream(async function*(world) {
let outer = open(world);
let o = await outer.next();
while(!o.done) {
yield {result: body(o.value.result)};
o = await outer.next();
}
});
}
zip(f, stream) {
let open = this.open;
return new self.tsh.Stream(async function*(world) {
let s1 = open(world);
let s2 = stream.open(world);
let n1 = await s1.next();
let n2 = await s2.next();
while(!n1.done && !n2.done) {
yield {result: f(n1.value.result)(n2.value.result)};
n1 = await s1.next();
n2 = await s2.next();
}
});
}
latest(f, stream) {
let open = this.open;
return new self.tsh.Stream(async function*(world) {
let s1 = open(world);
let s2 = stream.open(world);
let n1 = null;
let n2 = null;
let p1 = s1.next().then(n => { p1 = null; n1 = n });
let p2 = s2.next().then(n => { p2 = null; n2 = n });
await p1;
await p2;
while(!n1.done && !n2.done) {
yield {result: f(n1.value.result)(n2.value.result)};
if(p1 === null) p1 = s1.next().then(n => { p1 = null; n1 = n });
if(p2 === null) p2 = s2.next().then(n => { p2 = null; n2 = n });
await Promise.race([p1, p2]);
}
});
}
scan(x, f) {
let open = this.open;
return new self.tsh.Stream(async function*(world) {
let outer = open(world);
var r = {result: x};
let o = await outer.next();
while(!o.done) {
r = {result: f(r.result)(o.value.result)};
yield r;
o = await outer.next();
}
});
}
fold(x, f) {
let open = this.open;
return new self.tsh.Task(async world => {
let outer = open(world);
var r = x;
let o = await outer.next();
while(!o.done) {
r = f(r)(o.value.result);
o = await outer.next();
}
return {result: r};
});
}
switchMap(body) {
let open = this.open;
return new self.tsh.Stream(async function*(world) {
let s1 = open(world);
let s2 = null;
let p1 = null;
let p2 = null;
let done = false;
let newController = null;
let newWorld = null;
function propagateAbort() { newController.abort() }
function stopInner() {
if(newController && newWorld) {
if(world.abortSignal) world.abortSignal.removeEventListener("abort", propagateAbort);
newController.abort();
newController = null;
newWorld = null;
}
}
try {
while(!done) {
if(p1 == null) {
p1 = s1.next().then(n => {
p1 = null;
p2 = null;
stopInner();
if(n.done) {
done = true;
} else {
let stream2 = body(n.value.result);
newController = new AbortController();
if(world.abortSignal) world.abortSignal.addEventListener("abort", propagateAbort);
newWorld = Object.assign({}, world, {abortSignal: newController.signal});
s2 = stream2.open(newWorld);
}
return {outer: true};
});
}
if(p2 == null && s2 != null) {
p2 = s2.next();
}
let n = await Promise.race([p1, p2].filter(p => p != null));
if(n.done) {
s2 = null;
p2 = null;
} else if(!n.outer) {
p2 = null;
yield n.value;
}
}
} finally {
stopInner();
}
});
}
merge(stream) {
let open = this.open;
return new self.tsh.Stream(function(world) {
let s1 = open(world);
let s2 = stream.open(world);
let p1 = null;
let p2 = null;
return {next() {
if(p1 === null && s1 !== null) p1 = s1.next().then(n => { if(n.done) s1 = null; return {p: true, n: n}; });
if(p2 === null && s2 !== null) p2 = s2.next().then(n => { if(n.done) s2 = null; return {p: false, n: n}; });
let promises = [p1, p2].filter(p => p !== null);
return promises.length === 0 ? {done: true} : Promise.race(promises).then(r => {
if(r.p) p1 = null;
else p2 = null;
return r.n.done ? this.next() : r.n;
});
}};
});
}
take(count) {
let open = this.open;
return new self.tsh.Stream(async function*(world) {
let outer = open(world);
for(let i = 0; i < count; i++) {
let o = await outer.next();
if(o.done) return;
yield o.value;
}
});
}
drop(count) {
let open = this.open;
return new self.tsh.Stream(async function*(world) {
let outer = open(world);
for(let i = 0; i < count; i++) {
let o = await outer.next();
if(o.done) return;
}
while(true) {
let o = await outer.next();
if(o.done) return;
yield o.value;
}
});
}
takeWhile(condition) {
let open = this.open;
return new self.tsh.Stream(async function*(world) {
let outer = open(world);
while(true) {
let o = await outer.next();
if(o.done || !condition(o.value.result)) return;
yield o.value;
}
});
}
dropWhile(condition) {
let open = this.open;
return new self.tsh.Stream(async function*(world) {
let outer = open(world);
let triggered = false;
while(true) {
let o = await outer.next();
if(o.done) return;
if(triggered || !condition(o.value.result)) {
triggered = true;
yield o.value;
}
}
});
}
batch(size) {
let open = this.open;
return new self.tsh.Stream(async function*(world) {
let outer = open(world);
let list = [];
while(true) {
let o = await outer.next();
if(o.done) {
yield {result: list};
return;
}
list.push(o.value.result);
if(list.length >= size) {
yield {result: list};
list = [];
}
}
});
}
buffer(initial, aggregate, condition) {
let open = this.open;
return new self.tsh.Stream(world => {
let outer = open(world);
let isInitial = true;
let state = initial;
let done = false;
let going = false;
let promise = null;
function go() {
going = true;
if(!done && (isInitial || !condition(state))) {
promise = outer.next().then(x => {
if(x.done) done = true; else {
isInitial = false;
state = aggregate(state)(x.value.result);
go();
}
return x;
});
} else {
going = false;
}
}
go();
function next() {
if(!isInitial) {
let result = state;
isInitial = true;
state = initial;
if(!going) go();
return Promise.resolve({done: false, value: {result: result}})
}
if(done) return Promise.resolve({done: true});
if(!going) go();
return promise.then(next);
}
return {next: next};
});
}
catch(f) {
let open = this.open;
return new self.tsh.Stream(async function*(world) {
try {
let outer = open(world);
while(true) {
let o = await outer.next();
if(o.done) return;
yield o.value;
}
} catch(e) {
if(e === self.tsh.Task.abortedError) throw e;
let outer = f(e).open(world);
while(true) {
let o = await outer.next();
if(o.done) return;
yield o.value;
}
}
});
}
toHtml() {
return {_tag: "span", children: ["stream"]};
}
};
self.tsh.Stream.forever = (x, f) => new self.tsh.Stream(async function*(world) {
var r = {result: x};
while(true) {
r = await f(r.result).run(world);
yield r
}
});
self.tsh.Stream.once = task => new self.tsh.Stream(async function*(world) {
yield await task.run(world);
});
self.tsh.Stream.ofList = a => new self.tsh.Stream(async function*(world) {
for(var i = 0; i < a.length; i++) yield {result: a[i]};
});
self.tsh.Stream.empty = new self.tsh.Stream(async function*(world) {});
self.tsh.Lazy = class extends self.tsh.AbstractView {
constructor(compute) {
super();
this.compute = compute;
}
toHtml() {
return {_tag: "span", children: ["lazy"]};
}
};
self.tsh.Dom = class extends self.tsh.AbstractView {
constructor(list) {
super();
if(!Array.isArray(list)) throw "Expected a List Dom, but got " + list;
this.list = list;
}
toHtml() {
return {_tag: "span", children: ["dom"]};
}
};
self.tsh.Regex = class extends self.tsh.AbstractView {
constructor(pattern, flags) {
super();
this.pattern = pattern;
this.flags = flags;
this.global = null;
this.nonGlobal = null;
}
cacheNonGlobal() {
if(this.nonGlobal === null) this.nonGlobal = new RegExp(this.pattern, this.flags);
return this.nonGlobal;
}
cacheGlobal(offset) {
if(this.global === null || this.global.lastIndex !== offset) {
this.global = new RegExp(this.pattern, this.flags + "g");
this.global.lastIndex = offset;
}
return this.global;
}
toHtml() {
return {_tag: "span", children: [
"[regex: " + JSON.stringify(this.pattern) + ", flags: " + this.flags + "]"
]};
}
};
self.tsh.toHtml = value => {
if(value instanceof self.tsh.AbstractView) return value.toHtml();
if(value instanceof Function) return {_tag: "span", children: ["function"]};
if(value instanceof Uint8Array) return {_tag: "span", children: ["bytes"]};
if(value instanceof Response) return {_tag: "span", children: ["response"]};
if(typeof value === 'string') return {_tag: "span", children: [JSON.stringify(value)]};
if(typeof value === 'number') return {_tag: "span", children: [JSON.stringify(value)]};
if(value === void 0) return {_tag: "span", children: ["undefined"]};
if(value === null) return {_tag: "span", children: ["null"]};
if(value === true) return {_tag: "span", children: ["true"]};
if(value === false) return {_tag: "span", children: ["false"]};
var result = [];
if(Array.isArray(value)) {
result.push("[");
for(var i = 0; i < value.length; i++) {
if(result.length > 1) result.push(", ");
result.push(self.tsh.toHtml(value[i]));
}
result.push("]");
} else if(XMap.isInstance(value)) {
result.push("[map: ");
var pairs = XMap.toList(value);
for(var i = 0; i < pairs.length; i++) {
if(result.length > 1) result.push(", ");
result.push(self.tsh.toHtml(pairs[i].key));
result.push(" ~> ");
result.push(self.tsh.toHtml(pairs[i].value));
}
result.push("]");
} else if(XSet.isInstance(value)) {
result.push("[set: ");
var members = XSet.toList(value);
for(var i = 0; i < members.length; i++) {
if(result.length > 1) result.push(", ");
result.push(self.tsh.toHtml(members[i]));
}
result.push("]");
} else {
var isConstructor =
Object.prototype.hasOwnProperty.call(value, "_") &&
typeof value._ === 'string' &&
value._.match(/^[A-Z][a-zA-Z0-9]*$/g);
if(isConstructor) {
result.push(value._);
for(var j = 1; Object.prototype.hasOwnProperty.call(value, "_" + j); j++) {
var v = value["_" + j];
var simple = !v || !Object.prototype.hasOwnProperty.call(v, "_1") || !v._;
result.push(" ");
if(!simple) result.push("(");
result.push(self.tsh.toHtml(v));
if(!simple) result.push(")");
}
} else {
var isPair = false;
if(Object.prototype.hasOwnProperty.call(value, "key") && Object.prototype.hasOwnProperty.call(value, "value")) {
isPair = true;
for(var p in value) if(Object.prototype.hasOwnProperty.call(value, p)) {
if(p !== "key" && p !== "value") isPair = false;
}
}
if(isPair) {
result.push(self.tsh.toHtml(value.key));
result.push(" ~> ");
if(value.value instanceof Object) result.push("(");
result.push(self.tsh.toHtml(value.value));
if(value.value instanceof Object) result.push(")");
} else {
result.push("{");
for(var k in value) if(Object.prototype.hasOwnProperty.call(value, k)) {
if(result.length > 1) result.push(", ");
var l = k.match(/^[a-z][a-zA-Z0-9]*$/g) ? k : JSON.stringify(k);
result.push(l + ": ");
result.push(self.tsh.toHtml(value[k]));
}
result.push("}");
}
}
}
return {_tag: "span", children: result};
};
self.tsh.record = (m, r) => {
for(var k in r) {
if(Object.prototype.hasOwnProperty.call(r, k) && !Object.prototype.hasOwnProperty.call(m, k)) m[k] = r[k];
}
return m;
};
self.tsh.lookup = (r, f) => {
return r != null && Object.prototype.hasOwnProperty.call(r, f) ? self.tsh.some(r[f]) : self.tsh.none;
};
self.tsh.none = {_: "None"};
self.tsh.some = v => ({_: "Some", _1: v});
self.tsh.maybe = v => v == null ? self.tsh.none : self.tsh.some(v);
self.tsh.isNone = v => v._ === "None";
self.tsh.isSome = v => v._ === "Some";
self.tsh.hexForBytes = (function() {
var result = new Array(256);
for(var i = 0; i < 256; i++) {
result[i] = (i < 16 ? "0" : "") + i.toString(16);
}
return result;
})();
self.tsh.bytesForHex = (function() {
var result = {};
for(var i = 0; i < 256; i++) {
result[self.tsh.hexForBytes[i]] = i;
result[self.tsh.hexForBytes[i].toUpperCase()] = i;
}
return result;
})();
self.tsh.toHex = a => a.reduce((s, b) => s + self.tsh.hexForBytes[b], "");
self.tsh.ofHex = s => {
var l = s.length / 2;
var result = new Uint8Array(l);
for(var i = 0; i < l; i += 1) {
result[i] = self.tsh.bytesForHex[s[i * 2] + s[i * 2 + 1]];
}
return result;
};
self.tsh.then = (m, f) => {
if(Array.isArray(m)) {
var result = [];
for(var i = 0; i < m.length; i++) {
var a = f(m[i]);
for(var j = 0; j < a.length; j++) {
result.push(a[j]);
}
}
return result;
} else if(m instanceof self.tsh.Task) {
return m.then(f);
} else if(m instanceof self.tsh.Stream) {
return m.then(f);
} else {
console.error("Operator <- not supported for: " + m);
throw "Operator <- not supported for: " + m;
}
};
self.tsh.action = actionName => parameter => new self.tsh.Task(async world => {
var action = {action: actionName, data: parameter, context: world};
var options = {method: "POST", body: JSON.stringify(action)};
function checkAborted() {
if(world.abortSignal && world.abortSignal.aborted) throw self.tsh.Task.abortedError;
}
checkAborted();
if(world.abortSignal) options.signal = world.abortSignal;
console.log(actionName +
(parameter.from != null ? " " + parameter.from : "") +
(parameter.size != null ? " " + parameter.size : "") +
(parameter.path != null ? " " + parameter.path : "") +
(parameter.target != null ? " " + parameter.target : "") +
(parameter.command != null ? " " + parameter.command : "") +
(world.ssh != null ? " (" + world.ssh.user + "@" + world.ssh.host + ")" : "")
);
let result = await fetch("/execute", options);
checkAborted();
if(!result.ok) {
let problem;
try {
problem = await result.text();
} catch (_) {
problem = "Action error " + result.status + ": " + options.body;
}
checkAborted();
throw new Error(problem);
} else if(actionName === "File.streamBytes") {
checkAborted();
return {result: result.body.getReader()};
} else if(result.headers.get('Content-Type') === "application/octet-stream") {
let bytes = new Uint8Array(await result.arrayBuffer());
checkAborted();
return {result: bytes};
} else {
let json = await result.json();
checkAborted();
return {result: json.data};
}
});
self.tsh.loadImport = url => new self.tsh.Task.simple((t, e, w) => {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onload = function() {
if(xhr.status === 200) {
var f = new Function('exports', xhr.responseText);
var exported = {};
f(exported);
t(exported);
} else {
e('Could not load module');
}
};
xhr.send();
});
importScripts("./tokenizer.js");
importScripts("./target/scala-2.12/topshell-opt.js");