blob: 74e3e4bbea0451f02a9abc734c19ea2c26f5c5bd (
plain)
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
|
// Extended API
// Makes the internal buffer iterable so we can use Uint8Array.from
if (typeof Symbol !== 'undefined' && Symbol.iterator) {
Module['BufferWithRandomAccess'].prototype[Symbol.iterator] = function() {
var b = this, i = 0, L = b.size();
return {
next: function() {
if (i < L) {
return { done: false, value: b.at(i++) }
} else {
return { done: true }
}
}
}
};
}
// Converts (copies) a BufferWithRandomAccess to native ArrayBuffer
if (typeof Uint8Array !== 'undefined') {
Module['BufferWithRandomAccess'].prototype['toArrayBuffer'] = function() {
return Uint8Array['from'](this).buffer; // :ArrayBuffer
};
}
// Parse and compile S-expression text syntax into WASM code.
// When running in NodeJS, this functions returns a Buffer instead of ArrayBuffer.
Module['compileWast'] = function compileWast(sourceText /*:string*/) /*:ArrayBuffer*/ {
var parser = new Module['SExpressionParser'](sourceText);
// console.log('s-expr dump:');
// parser.get_root().dump();
var s_module = parser.get_root().getChild(0);
// console.log('s_module', s_module)
// strange API for creating WASM `module` from S-expression AST
var module = new Module['Module']();
new Module['SExpressionWasmBuilder'](module, s_module);
// console.log('module:', module);
// this.WasmPrinter.prototype.printModule(module);
// emit WASM
var debug = false;
var buf0 = new Module['BufferWithRandomAccess'](debug);
var wasmWriter = new Module['WasmBinaryWriter'](module, buf0, debug);
wasmWriter.write();
if (ENVIRONMENT_IS_NODE) {
return Buffer['from'](Uint8Array['from'](buf0));
} else {
return buf0['toArrayBuffer']();
}
};
|