-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwebIDL.js
638 lines (607 loc) · 17.4 KB
/
webIDL.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
var debugEnabled = false;
// debugEnabled = true;
var debugIndentLevel = 0;
function debug() {
if (debugEnabled) {
console.log.apply(this, ['[|DEBUG|]', ' '.repeat(debugIndentLevel), ...arguments]);
}
}
function debugIndent() {
if (arguments.length > 0) { debug.apply(null, arguments); }
debugIndentLevel += 1;
}
function debugDedent() {
debugIndentLevel -= 1;
}
function debugInstr(name, self, stack, args) {
debugIndent('in instruction', name);
debug('this =', self);
debug('stack =', stack);
if (args !== undefined) {
debug('args =', args);
}
}
function polyfill(module, imports, getExports) {
var u8;
function initMemory() {
if (u8) return;
debug('initializing memory');
var memory = getExports()['memory'] || imports['env'] && imports['env']['memory'];
u8 = new Uint8Array(memory.buffer);
}
// Export declarations, used for call-export arities
const exportDecls = {};
// Original imported functions, accessed by index
const origImports = [];
// Table of references, converts i32 <-> any
const refTable = [];
// Type declarations
const typeMap = {
// Interface types
0x7fff: 'Int',
0x7ffe: 'Float',
0x7ffd: 'Any',
0x7ffc: 'String',
0x7ffb: 'Seq',
// Wasm types
0x7f: 'i32',
0x7e: 'i64',
0x7d: 'f32',
0x7c: 'f64',
0x6f: 'anyref',
};
function pop(stack) {
return stack.splice(stack.length - 1, 1)[0];
}
function popN(stack, n) {
// Return an array of N items, in the order they were pushed
// e.g. stack = [4, 5, 6]; popN(stack, 2) == [5, 6]
return stack.splice(stack.length - n, n);
}
const Instructions = {
argGet(stack, args) {
debugInstr('argGet', this, stack, args);
stack.push(args[this.arg]);
debugDedent();
},
call(stack) {
debugInstr('call', this, stack);
const imp = origImports[this.importIdx];
debug('import decl =', imp);
const args = popN(stack, imp.params.length);
debug('args =', args);
const ret = imp.import.apply(null, args);
if (imp.results.length > 0) {
debug('ret =', ret);
stack.push(ret);
}
debugDedent();
},
callExport(stack) {
debugInstr('callExport', this, stack);
const exp = exportDecls[this.exportName];
debug('export decl =', exp);
const args = popN(stack, exp.params.length);
debug('args =', args);
const fn = getExports()[this.exportName];
const ret = fn.apply(null, args);
if (exp.results.length > 0) {
debug('ret =', ret);
stack.push(ret);
}
debugDedent();
},
readUtf8(stack) {
debugInstr('readUtf8', this, stack);
const len = pop(stack);
const ptr = pop(stack);
debug('ptr, len =', ptr, len);
initMemory();
let str = '';
for (var i = 0; i < len; ++i) {
str += String.fromCharCode(u8[ptr + i]);
}
debug('str =', str);
stack.push(str);
debugDedent();
},
writeUtf8(stack) {
debugInstr('writeUtf8', this, stack);
const str = pop(stack);
debug('str =', str);
const alloc = getExports()[this.alloc];
const len = str.length;
const ptr = alloc(len);
initMemory();
for (var i = 0; i < len; ++i) {
u8[ptr + i] = str.charCodeAt(i);
}
stack.push(ptr);
stack.push(len);
debugDedent();
},
asWasm(stack) {
debugInstr('asWasm', this, stack);
debugDedent();
},
asInterface(stack) {
debugInstr('asInterface', this, stack);
debugDedent();
},
tableRefAdd(stack) {
debugInstr('tableRefAdd', this, stack);
const ref = pop(stack);
debug('ref = ', ref);
const idx = refTable.length;
refTable.push(ref);
debug('refTable =', refTable);
debug('idx = ', idx);
stack.push(idx);
debugDedent();
},
tableRefGet(stack) {
debugInstr('tableRefGet', this, stack);
const idx = pop(stack);
debug('idx = ', idx);
debug('refTable =', refTable);
const ref = refTable[idx];
debug('ref = ', ref);
stack.push(ref);
debugDedent();
},
callMethod(stack) {
debugInstr('callMethod', this, stack);
const imp = origImports[this.importIdx];
debug('import decl =', imp);
const args = popN(stack, imp.params.length - 1);
const self = pop(stack);
debug('self =', self);
debug('args =', args);
const ret = imp.import.apply(self, args);
if (imp.results.length > 0) {
debug('ret =', ret);
stack.push(ret);
}
debugDedent();
},
makeRecord(stack) {
debugInstr('makeRecord', this, stack);
const decl = typeMap[this.ty];
debug('decl =', decl);
const ret = {};
const args = popN(stack, decl.fields.length);
debug('args =', args);
for (var i = 0; i < decl.fields.length; ++i) {
ret[decl.fields[i]] = args[i];
}
debug('ret =', ret);
stack.push(ret);
debugDedent();
},
getField(stack) {
debugInstr('getField', this, stack);
const decl = typeMap[this.ty];
debug('decl =', decl);
const field = decl.fields[this.field];
debug('field =', field);
const obj = pop(stack);
debug('obj =', obj);
const val = obj[field];
debug('val =', val);
stack.push(val);
debugDedent();
},
constVal(stack) {
debugInstr('constVal', this, stack);
const val = this.val;
debug('val =', val);
stack.push(val);
debugDedent();
},
foldSeq(stack) {
debugInstr('foldSeq', this, stack);
const imp = origImports[this.importIdx];
debug('import decl =', imp);
let val = pop(stack);
const list = pop(stack);
debug('list =', list);
debug('val =', val);
for (let i = 0; i < list.length; ++i) {
const elem = list[i];
debugIndent('elem', i, '=', elem);
val = imp.import.apply(null, [val, elem]);
debugDedent();
debug('val =', val);
}
stack.push(val);
debugDedent();
},
}
const interface = {};
function makeAdapter(name, params, results, instrs) {
return function() {
debugIndent('Called function:', name);
const stack = [];
for (var i = 0; i < instrs.length; ++i) {
const instr = instrs[i];
instr.func.apply(instr, [stack, arguments]);
}
debugDedent();
if (results.length > 0) {
return stack[stack.length - 1];
}
};
}
var bindingSections = WebAssembly.Module.customSections(module, 'interface-types');
for (var section = 0; section < bindingSections.length; ++section) {
var bytes = new Uint8Array(bindingSections[section]);
var byteIndex = 0;
function readByte() {
return bytes[byteIndex++];
}
function readLEB() {
const bytes = [];
while (true) {
const byte = readByte();
bytes.push(byte & 0x7f);
if (!(byte & 0x80)) {
break;
}
}
// Bytes are stored little-endian, so read them in significance order
let val = 0;
for (let i = 0; i < bytes.length; ++i) {
const byte = bytes[bytes.length - i - 1];
val <<= 7;
val |= byte & 0x7f;
}
return val;
}
function readStr() {
var len = readByte();
var result = '';
for (var i = 0; i < len; ++i) {
result += String.fromCharCode(readByte());
}
return result;
}
function readType() {
// TODO: interface types may be multi-byte, and depend on a type section
const ty = readLEB();
let name = typeMap[ty];
if (typeof name === 'object') {
name = name.name;
}
debug('ty =', ty, ":", name);
if (ty === 0x7ffb) { // seq
return [ty].concat(readType());
}
return ty;
}
function readList(f, debugMsg) {
if (debugMsg !== undefined) {
debugIndent(debugMsg);
} else {
debugIndent();
}
var len = readByte();
var result = [];
for (var i = 0; i < len; ++i) {
debugIndent(i);
result.push(f());
debugDedent();
}
debugDedent();
return result;
}
function readInstr() {
const opcode = readByte();
let instr;
if (opcode === 0x00) { // arg.get
debugIndent('arg.get');
const arg = readLEB();
debug('arg =', arg);
instr = {
func: Instructions.argGet,
arg,
};
} else if (opcode === 0x01) { // call
debugIndent('call');
const importIdx = readLEB();
debug('importIdx =', importIdx);
instr = {
func: Instructions.call,
importIdx,
};
} else if (opcode === 0x02) { // call-export
debugIndent('call-export');
const exportName = readStr();
debug('exportName =', exportName);
instr = {
func: Instructions.callExport,
exportName,
};
} else if (opcode === 0x03) { // read-utf8
debugIndent('read-utf8');
instr = {
func: Instructions.readUtf8,
};
} else if (opcode === 0x04) { // write-utf8
debugIndent('write-utf8');
const alloc = readStr();
debug('alloc =', alloc);
instr = {
func: Instructions.writeUtf8,
alloc,
};
} else if (opcode === 0x05) { // as-wasm
debugIndent('as-wasm');
const ty = readType();
instr = {
func: Instructions.asWasm,
ty,
}
} else if (opcode === 0x06) { // as-interface
debugIndent('as-interface');
const ty = readType();
instr = {
func: Instructions.asInterface,
ty,
}
} else if (opcode === 0x07) { // table-ref-add
debugIndent('table-ref-add');
instr = {
func: Instructions.tableRefAdd,
};
} else if (opcode === 0x08) { // table-ref-get
debugIndent('table-ref-get');
instr = {
func: Instructions.tableRefGet,
};
} else if (opcode === 0x09) { // call-method
debugIndent('call-method');
const importIdx = readLEB();
debug('importIdx =', importIdx);
instr = {
func: Instructions.callMethod,
importIdx,
};
} else if (opcode === 0x0a) { // make-record
debugIndent('make-record');
const ty = readType();
instr = {
func: Instructions.makeRecord,
ty,
};
} else if (opcode === 0x0c) { // get-field
debugIndent('get-field');
const ty = readLEB();
debug('ty =', ty);
const field = readLEB();
debug('field =', field);
instr = {
func: Instructions.getField,
ty,
field,
};
} else if (opcode === 0x0d) { // const
debugIndent('const');
const ty = readType();
const val = readLEB();
instr = {
func: Instructions.constVal,
ty,
val,
};
} else if (opcode === 0x0e) { // fold-seq
debugIndent('fold-seq');
const importIdx = readLEB();
instr = {
func: Instructions.foldSeq,
importIdx,
};
} else {
throw 'Unknown opcode: ' + opcode;
}
debugDedent();
return instr;
}
const numExports = readLEB();
debug('export count:', numExports);
for (var i = 0; i < numExports; ++i) {
debugIndent('export', i);
const name = readStr();
debug('name =', name);
const params = readList(readType, 'params');
const results = readList(readType, 'results');
debugDedent();
exportDecls[name] = {
params,
results,
};
}
const numTypes = readLEB();
debug('type count:', numTypes);
for (var i = 0; i < numTypes; ++i) {
debugIndent('type', i);
const name = readStr();
debug('name =', name);
const fields = readList(readStr, 'fields');
debug('fields =', fields);
const types = readList(readType, 'types');
debugDedent();
typeMap[i] = {
name,
fields,
types,
};
}
const numImportFuncs = readLEB();
debug('import count:', numImportFuncs);
for (var i = 0; i < numImportFuncs; ++i) {
debugIndent('import', i);
const namespace = readStr();
debug('namespace =', namespace);
const name = readStr();
debug('name =', name);
const params = readList(readType, 'params');
const results = readList(readType, 'results');
debugDedent();
origImports.push({
import: imports[namespace][name],
params,
results,
});
}
const numAdapters = readLEB();
debug('adapter count:', numAdapters);
for (var i = 0; i < numAdapters; ++i) {
debugIndent('adapter', i);
const kind = readByte();
debug('kind =', kind)
let namespace;
if (kind == 0) { // import
namespace = readStr();
debug('namespace =', namespace);
}
const name = readStr();
debug('name =', name);
const params = readList(readType, 'params');
const results = readList(readType, 'results');
const instrs = readList(readInstr, 'instrs');
debugDedent();
const fn = makeAdapter(name, params, results, instrs);
if (kind == 0) { // import
imports[namespace][name] = fn;
} else if (kind == 1) { // export
interface[name] = fn;
} else if (kind == 2) { // helper function
origImports.push({
import: fn,
params,
results,
})
}
}
const numForwards = readLEB();
debug('forward count:', numForwards);
for (var i = 0; i < numForwards; ++i) {
debugIndent('forward', i);
const name = readStr();
debug('name =', name);
Object.defineProperty(interface, name, {
get() {
debug('Getting forwarded export:', name);
return getExports()[name];
}
});
debugDedent();
}
if (bytes.length != byteIndex) {
throw "Invalid read: len=" + bytes.length + ", read=" + byteIndex;
}
}
// Effectively this automates:
// var real_console_log = imports['host']['console_log'];
// imports['host']['console_log'] = function(ptr) {
// var arg = bindingTypes[0](ptr);
// real_console_log(arg);
// };
// var real_document_title = imports['host']['document_title'];
// imports['host']['document_title'] = function(ptr, len) {
// var res = real_document_title();
// bindingTypes[1](res, ptr, len);
// };
return interface;
}
// Taken wholesale from Emscripten, renamed from convertJsFunctionToWasm
function jsToWasmFunc(func, sig) {
// The module is static, with the exception of the type section, which is
// generated based on the signature passed in.
var typeSection = [
0x01, // id: section,
0x00, // length: 0 (placeholder)
0x01, // count: 1
0x60, // form: func
];
var sigRet = sig.slice(0, 1);
var sigParam = sig.slice(1);
var typeCodes = {
'i': 0x7f, // i32
'j': 0x7e, // i64
'f': 0x7d, // f32
'd': 0x7c, // f64
};
// Parameters, length + signatures
typeSection.push(sigParam.length);
for (var i = 0; i < sigParam.length; ++i) {
typeSection.push(typeCodes[sigParam[i]]);
}
// Return values, length + signatures
// With no multi-return in MVP, either 0 (void) or 1 (anything else)
if (sigRet == 'v') {
typeSection.push(0x00);
} else {
typeSection = typeSection.concat([0x01, typeCodes[sigRet]]);
}
// Write the overall length of the type section back into the section header
// (excepting the 2 bytes for the section id and length)
typeSection[1] = typeSection.length - 2;
// Rest of the module is static
var bytes = new Uint8Array([
0x00, 0x61, 0x73, 0x6d, // magic ("\0asm")
0x01, 0x00, 0x00, 0x00, // version: 1
].concat(typeSection, [
0x02, 0x07, // import section
// (import "e" "f" (func 0 (type 0)))
0x01, 0x01, 0x65, 0x01, 0x66, 0x00, 0x00,
0x07, 0x05, // export section
// (export "f" (func 0 (type 0)))
0x01, 0x01, 0x66, 0x00, 0x00,
]));
// We can compile this wasm module synchronously because it is very small.
// This accepts an import (at "e.f"), that it reroutes to an export (at "f")
var module = new WebAssembly.Module(bytes);
var instance = new WebAssembly.Instance(module, {
e: {
f: func
}
});
var wrappedFunc = instance.exports.f;
return wrappedFunc;
}
async function loadWasm(filename, imports) {
imports = imports || {};
var bytes;
if (typeof read === 'function') {
// D8
bytes = read(filename, 'binary');
} else if (typeof require === 'function') {
// NodeJS
var fs = require('fs');
bytes = fs.readFileSync(filename);
} else {
// Browser
var fetched = await fetch(filename);
bytes = await fetched.arrayBuffer();
}
var module = new WebAssembly.Module(bytes);
var instance;
function getExports() {
return instance.exports;
}
var interface = polyfill(module, imports, getExports);
instance = new WebAssembly.Instance(module, imports);
// Actual WebAssembly.Instance exports are Read-Only, so we have to create a
// fake object here
var fakeInstance = {};
for (var name in instance) {
fakeInstance[name] = instance[name];
}
fakeInstance.exports = interface;
return fakeInstance;
}
var isNodeJS = typeof require !== 'undefined';
if (isNodeJS) {
module.exports = { polyfill, loadWasm, jsToWasmFunc }
}