-
Notifications
You must be signed in to change notification settings - Fork 0
/
workFunction.js
executable file
·895 lines (783 loc) · 24.8 KB
/
workFunction.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
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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
#!/bin/usr/env node
/*
* Copyright 2020 WebAssembly Community Group participants
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const { promises: fs } = require('fs');
const fetch = async function fetchWrapper(filename) {
return fs.readFile(`./${filename}`);
}
///////////////////////////
exports.workFunction = async function workFunction(DATUM_ARG, CLANG_ARG, MEMFS_ARG, SYSROOT_ARG) {
progress();
const isWorkFun = true;
/*
const CLANG_WINJ_BYTES = Buffer.from(CLANG_ARG, 'base64');
const MEMFS_WINJ_BYTES = Buffer.from(MEMFS_ARG, 'base64');
const SYSROOT_WINJ_BYTES = Buffer.from(SYSROOT_ARG, 'base64');
*/
const CLANG_WINJ_BYTES = CLANG_ARG
const MEMFS_WINJ_BYTES = MEMFS_ARG
const SYSROOT_WINJ_BYTES = SYSROOT_ARG
function sleep(ms) {
return new Promise((resolve, _) => setTimeout(resolve, ms));
}
function readStr(u8, o, len = -1) {
let str = '';
let end = u8.length;
if (len != -1)
end = o + len;
for (let i = o; i < end && u8[i] != 0; ++i)
str += String.fromCharCode(u8[i]);
return str;
}
function debounceLazy(f, ms) {
let waiting = 0;
let running = false;
const wait = async () => {
++waiting;
await sleep(ms);
return --waiting === 0;
};
const wrapped = async (...args) => {
if (await wait()) {
while (running) await wait();
running = true;
try {
await f(...args);
} finally {
running = false;
}
}
};
return wrapped;
}
const API = (function() {
class ProcExit extends Error {
constructor(code) {
super(`process exited with code ${code}.`);
this.code = code;
}
};
class NotImplemented extends Error {
constructor(modname, fieldname) {
super(`${modname}.${fieldname} not implemented.`);
}
}
class AbortError extends Error {
constructor(msg = 'abort') { super(msg); }
}
class AssertError extends Error {
constructor(msg) { super(msg); }
}
function assert(cond) {
if (!cond) {
throw new AssertError('assertion failed.');
}
}
function getInstance(module, imports) {
return WebAssembly.instantiate(module, imports);
}
function getImportObject(obj, names) {
const result = {};
for (let name of names) {
result[name] = obj[name].bind(obj);
}
return result;
}
function msToSec(start, end) {
return ((end - start) / 1000).toFixed(2);
}
const ESUCCESS = 0;
class Memory {
constructor(memory) {
this.memory = memory;
this.buffer = this.memory.buffer;
this.u8 = new Uint8Array(this.buffer);
this.u32 = new Uint32Array(this.buffer);
}
check() {
if (this.buffer.byteLength === 0) {
this.buffer = this.memory.buffer;
this.u8 = new Uint8Array(this.buffer);
this.u32 = new Uint32Array(this.buffer);
}
}
read8(o) { return this.u8[o]; }
read32(o) { return this.u32[o >> 2]; }
write8(o, v) { this.u8[o] = v; }
write32(o, v) { this.u32[o >> 2] = v; }
write64(o, vlo, vhi = 0) { this.write32(o, vlo); this.write32(o + 4, vhi); }
readStr(o, len) {
return readStr(this.u8, o, len);
}
// Null-terminated string.
writeStr(o, str) {
o += this.write(o, str);
this.write8(o, 0);
return str.length + 1;
}
write(o, buf) {
if (buf instanceof ArrayBuffer) {
return this.write(o, new Uint8Array(buf));
} else if (typeof buf === 'string') {
return this.write(o, buf.split('').map(x => x.charCodeAt(0)));
} else {
const dst = new Uint8Array(this.buffer, o, buf.length);
dst.set(buf);
return buf.length;
}
}
};
class MemFS {
constructor(options) {
const compileStreaming = options.compileStreaming;
this.hostWrite = options.hostWrite;
this.stdinStr = options.stdinStr || "";
this.stdinStrPos = 0;
this.memfsFilename = options.memfsFilename;
this.hostMem_ = null; // Set later when wired up to application.
// Imports for memfs module.
const env = getImportObject(
this, [ 'abort', 'host_write', 'host_read', 'memfs_log', 'copy_in', 'copy_out' ]);
this.ready = compileStreaming(this.memfsFilename)
.then(module => WebAssembly.instantiate(module, {env}))
.then(instance => {
this.instance = instance;
this.exports = instance.exports;
this.mem = new Memory(this.exports.memory);
this.exports.init();
})
}
set hostMem(mem) {
this.hostMem_ = mem;
}
setStdinStr(str) {
this.stdinStr = str;
this.stdinStrPos = 0;
}
addDirectory(path) {
this.mem.check();
this.mem.write(this.exports.GetPathBuf(), path);
this.exports.AddDirectoryNode(path.length);
}
addFile(path, contents) {
const length =
contents instanceof ArrayBuffer ? contents.byteLength : contents.length;
this.mem.check();
this.mem.write(this.exports.GetPathBuf(), path);
const inode = this.exports.AddFileNode(path.length, length);
const addr = this.exports.GetFileNodeAddress(inode);
this.mem.check();
this.mem.write(addr, contents);
}
getFileContents(path) {
this.mem.check();
this.mem.write(this.exports.GetPathBuf(), path);
const inode = this.exports.FindNode(path.length);
const addr = this.exports.GetFileNodeAddress(inode);
const size = this.exports.GetFileNodeSize(inode);
return new Uint8Array(this.mem.buffer, addr, size);
}
abort() { throw new AbortError(); }
host_write(fd, iovs, iovs_len, nwritten_out) {
this.hostMem_.check();
assert(fd <= 2);
let size = 0;
let str = '';
for (let i = 0; i < iovs_len; ++i) {
const buf = this.hostMem_.read32(iovs);
iovs += 4;
const len = this.hostMem_.read32(iovs);
iovs += 4;
str += this.hostMem_.readStr(buf, len);
size += len;
}
this.hostMem_.write32(nwritten_out, size);
this.hostWrite(str);
return ESUCCESS;
}
host_read(fd, iovs, iovs_len, nread) {
this.hostMem_.check();
assert(fd === 0);
let size = 0;
for (let i = 0; i < iovs_len; ++i) {
const buf = this.hostMem_.read32(iovs);
iovs += 4;
const len = this.hostMem_.read32(iovs);
iovs += 4;
const lenToWrite = Math.min(len, (this.stdinStr.length - this.stdinStrPos));
if(lenToWrite === 0){
break;
}
this.hostMem_.write(buf, this.stdinStr.substr(this.stdinStrPos, lenToWrite));
size += lenToWrite;
this.stdinStrPos += lenToWrite;
if(lenToWrite !== len){
break;
}
}
// For logging
// this.hostWrite("Read "+ size + "bytes, pos: "+ this.stdinStrPos + "\n");
this.hostMem_.write32(nread, size);
return ESUCCESS;
}
memfs_log(buf, len) {
this.mem.check();
console.log(this.mem.readStr(buf, len));
}
copy_out(clang_dst, memfs_src, size) {
this.hostMem_.check();
const dst = new Uint8Array(this.hostMem_.buffer, clang_dst, size);
this.mem.check();
const src = new Uint8Array(this.mem.buffer, memfs_src, size);
// console.log(`copy_out(${clang_dst.toString(16)}, ${memfs_src.toString(16)}, ${size})`);
dst.set(src);
}
copy_in(memfs_dst, clang_src, size) {
this.mem.check();
const dst = new Uint8Array(this.mem.buffer, memfs_dst, size);
this.hostMem_.check();
const src = new Uint8Array(this.hostMem_.buffer, clang_src, size);
// console.log(`copy_in(${memfs_dst.toString(16)}, ${clang_src.toString(16)}, ${size})`);
dst.set(src);
}
}
const RAF_PROC_EXIT_CODE = 0xC0C0A;
class App {
constructor(module, memfs, name, ...args) {
this.argv = [name, ...args];
this.environ = {USER : 'alice'};
this.memfs = memfs;
this.allowRequestAnimationFrame = true;
this.handles = new Map();
this.nextHandle = 0;
const env = getImportObject(this, [
'canvas_arc',
'canvas_arcTo',
'canvas_beginPath',
'canvas_bezierCurveTo',
'canvas_clearRect',
'canvas_clip',
'canvas_closePath',
'canvas_createImageData',
'canvas_destroyHandle',
'canvas_ellipse',
'canvas_fill',
'canvas_fillRect',
'canvas_fillText',
'canvas_imageDataSetData',
'canvas_lineTo',
'canvas_measureText',
'canvas_moveTo',
'canvas_putImageData',
'canvas_quadraticCurveTo',
'canvas_rect',
'canvas_requestAnimationFrame',
'canvas_restore',
'canvas_rotate',
'canvas_save',
'canvas_scale',
'canvas_setFillStyle',
'canvas_setFont',
'canvas_setGlobalAlpha',
'canvas_setHeight',
'canvas_setLineCap',
'canvas_setLineDashOffset',
'canvas_setLineJoin',
'canvas_setLineWidth',
'canvas_setMiterLimit',
'canvas_setShadowBlur',
'canvas_setShadowColor',
'canvas_setShadowOffsetX',
'canvas_setShadowOffsetY',
'canvas_setStrokeStyle',
'canvas_setTextAlign',
'canvas_setTextBaseline',
'canvas_setTransform',
'canvas_setWidth',
'canvas_stroke',
'canvas_strokeRect',
'canvas_strokeText',
'canvas_transform',
'canvas_translate',
]);
const wasi_unstable = getImportObject(this, [
'proc_exit', 'environ_sizes_get', 'environ_get', 'args_sizes_get',
'args_get', 'random_get', 'clock_time_get', 'poll_oneoff'
]);
// Fill in some WASI implementations from memfs.
Object.assign(wasi_unstable, this.memfs.exports);
this.ready = getInstance(module, {wasi_unstable, env}).then(instance => {
this.instance = instance;
this.exports = this.instance.exports;
this.mem = new Memory(this.exports.memory);
this.memfs.hostMem = this.mem;
});
}
async run() {
progress();
await this.ready;
try {
this.exports._start();
} catch (exn) {
let writeStack = true;
if (exn instanceof ProcExit) {
if (exn.code === RAF_PROC_EXIT_CODE) {
console.log('Allowing rAF after exit.');
return true;
}
// Don't allow rAF unless you return the right code.
console.log(`Disallowing rAF since exit code is ${exn.code}.`);
this.allowRequestAnimationFrame = false;
if (exn.code == 0) {
return false;
}
writeStack = false;
}
// Write error message.
let msg = `\x1b[91mError: ${exn.message}`;
if (writeStack) {
msg = msg + `\n${exn.stack}`;
}
msg += '\x1b[0m\n';
this.memfs.hostWrite(msg);
// Propagate error.
throw exn;
}
progress();
}
proc_exit(code) {
throw new ProcExit(code);
}
environ_sizes_get(environ_count_out, environ_buf_size_out) {
this.mem.check();
let size = 0;
const names = Object.getOwnPropertyNames(this.environ);
for (const name of names) {
const value = this.environ[name];
// +2 to account for = and \0 in "name=value\0".
size += name.length + value.length + 2;
}
this.mem.write64(environ_count_out, names.length);
this.mem.write64(environ_buf_size_out, size);
return ESUCCESS;
}
environ_get(environ_ptrs, environ_buf) {
this.mem.check();
const names = Object.getOwnPropertyNames(this.environ);
for (const name of names) {
this.mem.write32(environ_ptrs, environ_buf);
environ_ptrs += 4;
environ_buf +=
this.mem.writeStr(environ_buf, `${name}=${this.environ[name]}`);
}
this.mem.write32(environ_ptrs, 0);
return ESUCCESS;
}
args_sizes_get(argc_out, argv_buf_size_out) {
this.mem.check();
let size = 0;
for (let arg of this.argv) {
size += arg.length + 1; // "arg\0".
}
this.mem.write64(argc_out, this.argv.length);
this.mem.write64(argv_buf_size_out, size);
return ESUCCESS;
}
args_get(argv_ptrs, argv_buf) {
this.mem.check();
for (let arg of this.argv) {
this.mem.write32(argv_ptrs, argv_buf);
argv_ptrs += 4;
argv_buf += this.mem.writeStr(argv_buf, arg);
}
this.mem.write32(argv_ptrs, 0);
return ESUCCESS;
}
random_get(buf, buf_len) {
const data = new Uint8Array(this.mem.buffer, buf, buf_len);
for (let i = 0; i < buf_len; ++i) {
data[i] = (Math.random() * 256) | 0;
}
}
clock_time_get(clock_id, precision, time_out) {
throw new NotImplemented('wasi_unstable', 'clock_time_get');
}
poll_oneoff(in_ptr, out_ptr, nsubscriptions, nevents_out) {
throw new NotImplemented('wasi_unstable', 'poll_oneoff');
}
canvas_destroyHandle(handle) {
this.handles.delete(handle);
}
// Canvas API
canvas_setWidth(width) { if (canvas) canvas.width = width; }
canvas_setHeight(height) { if (canvas) canvas.height = height; }
canvas_requestAnimationFrame() {
if (this.allowRequestAnimationFrame) {
requestAnimationFrame(ms => {
if (this.allowRequestAnimationFrame) {
this.exports.canvas_loop(ms);
}
});
}
}
// ImageData stuff
canvas_createImageData(w, h) {
if (ctx2d) {
const imageData = ctx2d.createImageData(w, h);
const handle = this.nextHandle++;
this.handles.set(handle, imageData);
return handle;
}
return -1;
}
canvas_putImageData(handle, x, y) {
if (ctx2d) {
const imageData = this.handles.get(handle);
if (imageData) {
ctx2d.putImageData(imageData, x, y);
}
}
}
canvas_imageDataSetData(handle, buffer, offset, size) {
const imageData = this.handles.get(handle);
if (imageData) {
this.mem.check();
const src = new Uint8Array(this.mem.buffer, buffer, size);
imageData.data.set(src, offset);
}
}
// Other Canvas methods.
canvas_arc(...args) { if (ctx2d) ctx2d.arc(...args); }
canvas_arcTo(...args) { if (ctx2d) ctx2d.arcTo(...args); }
canvas_beginPath(...args) { if (ctx2d) ctx2d.beginPath(...args); }
canvas_bezierCurveTo(...args) { if (ctx2d) ctx2d.bezierCurveTo(...args); }
canvas_clearRect(...args) { if (ctx2d) ctx2d.clearRect(...args); }
canvas_clip(value) { if (ctx2d) ctx2d.clip(['nonzero', 'evenodd'][value]); }
canvas_closePath(...args) { if (ctx2d) ctx2d.closePath(...args); }
canvas_ellipse(...args) { if (ctx2d) ctx2d.ellipse(...args); }
canvas_fill(value) { if (ctx2d) ctx2d.fill(['nonzero', 'evenodd'][value]); }
canvas_fillRect(...args) { if (ctx2d) ctx2d.fillRect(...args); }
canvas_fillText(text, text_len, x, y) { // TODO: maxwidth
this.mem.check();
if (ctx2d) ctx2d.fillText(this.mem.readStr(text, text_len), x, y);
}
canvas_lineTo(...args) { if (ctx2d) ctx2d.lineTo(...args); }
canvas_measureText(text, text_len) {
this.mem.check();
if (ctx2d) return ctx2d.measureText(this.mem.readStr(text, text_len)).width;
return 0;
}
canvas_moveTo(...args) { if (ctx2d) ctx2d.moveTo(...args); }
canvas_quadraticCurveTo(...args) { if (ctx2d) ctx2d.quadraticCurveTo(...args); }
canvas_rect(...args) { if (ctx2d) ctx2d.rect(...args); }
canvas_restore(...args) { if (ctx2d) ctx2d.restore(...args); }
canvas_rotate(...args) { if (ctx2d) ctx2d.rotate(...args); }
canvas_save(...args) { if (ctx2d) ctx2d.save(...args); }
canvas_scale(...args) { if (ctx2d) ctx2d.scale(...args); }
canvas_setTransform(...args) { if (ctx2d) ctx2d.setTransform(...args); }
canvas_stroke(...args) { if (ctx2d) ctx2d.stroke(...args); }
canvas_strokeRect(...args) { if (ctx2d) ctx2d.strokeRect(...args); }
canvas_strokeText(text, text_len, x, y) { // TODO: maxwidth
this.mem.check();
if (ctx2d) ctx2d.strokeText(this.mem.readStr(text, text_len), x, y);
}
canvas_transform(...args) { if (ctx2d) ctx2d.transform(...args); }
canvas_translate(...args) { if (ctx2d) ctx2d.translate(...args); }
// Canvas properties.
canvas_setFillStyle(buf, len) {
this.mem.check();
if (ctx2d) ctx2d.fillStyle = this.mem.readStr(buf, len);
}
canvas_setFont(buf, len) {
this.mem.check();
if (ctx2d) ctx2d.font = this.mem.readStr(buf, len);
}
canvas_setGlobalAlpha(value) { if (ctx2d) ctx2d.globalAlpha = value; }
canvas_setLineCap(value) {
if (ctx2d) ctx2d.lineCap = ['butt', 'round', 'square'][value];
}
canvas_setLineDashOffset(value) { if (ctx2d) ctx2d.lineDashOffset = value; }
canvas_setLineJoin(value) {
if (ctx2d) ctx2d.lineJoin = ['bevel', 'round', 'miter'][value];
}
canvas_setLineWidth(value) { if (ctx2d) ctx2d.lineWidth = value; }
canvas_setMiterLimit(value) { if (ctx2d) ctx2d.miterLimit = value; }
canvas_setShadowBlur(value) { if (ctx2d) ctx2d.shadowBlur = value; }
canvas_setShadowColor(buf, len) {
this.mem.check();
if (ctx2d) ctx2d.shadowColor = this.mem.readStr(buf, len);
}
canvas_setShadowOffsetX(value) { if (ctx2d) ctx2d.setShadowOffsetX = value; }
canvas_setShadowOffsetY(value) { if (ctx2d) ctx2d.setShadowOffsetY = value; }
canvas_setStrokeStyle(buf, len) {
this.mem.check();
if (ctx2d) ctx2d.strokeStyle = this.mem.readStr(buf, len);
}
canvas_setTextAlign(value) {
if (ctx2d)
ctx2d.textAlign = ['left', 'right', 'center', 'start', 'end'][value];
}
canvas_setTextBaseline(value) {
if (ctx2d)
ctx2d.textBaseline = [
'top', 'hanging', 'middle', 'alphabetic', 'ideographic', 'bottom'
][value];
}
}
class Tar {
constructor(buffer) {
this.u8 = new Uint8Array(buffer);
this.offset = 0;
}
readStr(len) {
const result = readStr(this.u8, this.offset, len);
this.offset += len;
return result;
}
readOctal(len) {
return parseInt(this.readStr(len), 8);
}
alignUp() {
this.offset = (this.offset + 511) & ~511;
}
readEntry() {
if (this.offset + 512 > this.u8.length) {
return null;
}
const entry = {
filename : this.readStr(100),
mode : this.readOctal(8),
owner : this.readOctal(8),
group : this.readOctal(8),
size : this.readOctal(12),
mtim : this.readOctal(12),
checksum : this.readOctal(8),
type : this.readStr(1),
linkname : this.readStr(100),
};
if (this.readStr(8) !== 'ustar ') {
return null;
}
entry.ownerName = this.readStr(32);
entry.groupName = this.readStr(32);
entry.devMajor = this.readStr(8);
entry.devMinor = this.readStr(8);
entry.filenamePrefix = this.readStr(155);
this.alignUp();
if (entry.type === '0') { // Regular file.
entry.contents = this.u8.subarray(this.offset, this.offset + entry.size);
this.offset += entry.size;
this.alignUp();
} else if (entry.type !== '5') { // Directory.
console.log('type', entry.type);
assert(false);
}
return entry;
}
untar(memfs) {
progress();
let entry;
while (entry = this.readEntry()) {
switch (entry.type) {
case '0': // Regular file.
memfs.addFile(entry.filename, entry.contents);
break;
case '5':
memfs.addDirectory(entry.filename);
break;
}
}
}
}
class API {
constructor(options) {
this.moduleCache = {};
this.readBuffer = options.readBuffer;
this.compileStreaming = options.compileStreaming;
this.hostWrite = options.hostWrite;
this.clangFilename = options.clang || 'clang';
this.lldFilename = options.lld || 'lld';
this.sysrootFilename = options.sysroot || 'sysroot.tar';
this.showTiming = options.showTiming || false;
this.clangCommonArgs = [
'-disable-free',
'-isysroot', '/',
'-internal-isystem', '/include/c++/v1',
'-internal-isystem', '/include',
'-internal-isystem', '/lib/clang/8.0.1/include',
'-ferror-limit', '19',
'-fmessage-length', '80',
'-fcolor-diagnostics',
];
this.memfs = new MemFS({
compileStreaming : this.compileStreaming,
hostWrite : this.hostWrite,
memfsFilename : options.memfs || 'memfs',
});
this.ready = this.memfs.ready.then(
() => { return this.untar(this.memfs, this.sysrootFilename); });
}
hostLog(message) {
const yellowArrow = '\x1b[1;93m>\x1b[0m ';
this.hostWrite(`${yellowArrow}${message}`);
}
async getModule(name) {
assert(name === 'clang'); // for my sake, this name will always equal clang
if (this.moduleCache[name]) return this.moduleCache[name];
console.log('> Fetching and compiling clang...');
// TODO XXX wp - use winj for clang
const module = await this.compileStreaming(name);
this.moduleCache[name] = module;
return module;
}
async untar(memfs, filename) {
await this.memfs.ready;
const promise = (async () => {
// TODO XXX wp - use winj for tar
const tar = new Tar(await this.readBuffer(filename));
tar.untar(this.memfs);
})();
console.log(`> Untarring ${filename}`);
await promise;
}
async compile(options) {
const input = options.input;
const contents = options.contents;
const obj = options.obj;
const opt = options.opt || '2';
await this.ready;
this.memfs.addFile(input, contents);
progress();
const clang = await this.getModule(this.clangFilename);
return await this.run(clang, 'clang', '-cc1', '-emit-obj',
...this.clangCommonArgs, '-O2', '-o', obj, '-x',
'c++', input);
}
async compileToAssembly(options) {
console.log('compileToAssembly called -- ');
const input = options.input;
const output = options.output;
const contents = options.contents;
//const obj = options.obj;
const triple = options.triple || 'x86_64';
const opt = options.opt || '2';
await this.ready;
this.memfs.addFile(input, contents);
const clang = await this.getModule(this.clangFilename);
await this.run(clang, 'clang', '-cc1', '-S', ...this.clangCommonArgs,
`-triple=${triple}`, '-mllvm',
'--x86-asm-syntax=intel', `-O${opt}`,
'-o', output, '-x', 'c', input);
return this.memfs.getFileContents(output);
}
async run(module, ...args) {
this.hostLog(`${args.join(' ')}\n`);
const start = +new Date();
const app = new App(module, this.memfs, ...args);
const instantiate = +new Date();
const stillRunning = await app.run();
const end = +new Date();
this.hostWrite('\n');
if (this.showTiming) {
const green = '\x1b[92m';
const normal = '\x1b[0m';
let msg = `${green}(${msToSec(start, instantiate)}s`;
msg += `/${msToSec(instantiate, end)}s)${normal}\n`;
this.hostWrite(msg);
}
return stillRunning ? app : null;
}
}
return API;
})();
// helper function convert nodejs Buffer to JS ArrayBuffer
function toArrayBuffer(buf) {
var ab = new ArrayBuffer(buf.length);
var view = new Uint8Array(ab);
for (var i = 0; i < buf.length; ++i) {
view[i] = buf[i];
}
return ab;
}
function _base64ToArrayBuffer(base64) {
progress();
var binary_string = atob(base64);
var len = binary_string.length;
var bytes = new Uint8Array(len);
for (var i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
progress();
}
return bytes.buffer;
}
// actually calls the compile stuff
async function compile () {
async function readBuffer(filename) {
//const response = await fetch('sysroot.tar'); // this is where sysroot.tar is loaded
let response;
if (isWorkFun) {
response = SYSROOT_WINJ_BYTES;
} else {
// response = await fetch('sysroot.tar');
}
const ab = _base64ToArrayBuffer(response);
return ab;
}
async function compileStreaming(filename) {
//const response = await fetch(filename); // this is where memfs and clang are loaded
let response;
if (isWorkFun) {
if (filename === 'clang') {
response = CLANG_WINJ_BYTES;
} else if (filename === 'memfs') {
response = MEMFS_WINJ_BYTES;
} else {
throw new Error('expected to load clang or memfs');
}
}
else {
// response = await fetch(filename);
}
const ab = _base64ToArrayBuffer(response);
return WebAssembly.compile(ab);
}
function hostWrite(s) { console.log(s); }
const api = new API({
readBuffer,
compileStreaming,
hostWrite,
});
const input = 'test.cc';
const output = 'test.S';
const contents = DATUM_ARG;
const triple = 'x86_64';
const opt = '2';
const outputBuf = await api.compileToAssembly({input, output, contents, triple, opt});
console.log('=-=-=-=- x86 compiled bytes:');
console.log(outputBuf);
return outputBuf;
}
progress();
return await compile();
} ////////////////////// end of big winj wrapper
async function main() {
console.log('done');
console.log('about to run the work function in an eval');
eval(
`(${workFunction})().then((res) => {console.log("DONE EVAL")})`
);
}
//main()