forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwasm2js.js
96 lines (88 loc) · 2.9 KB
/
wasm2js.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
/**
* @license
* Copyright 2019 The Emscripten Authors
* SPDX-License-Identifier: MIT
*/
// wasm2js.js - enough of a polyfill for the WebAssembly object so that we can load
// wasm2js code that way.
// Emit "var WebAssembly" if definitely using wasm2js. Otherwise, in MAYBE_WASM2JS
// mode, we can't use a "var" since it would prevent normal wasm from working.
/** @suppress{duplicate, const} */
#if WASM2JS || WASM == 2
var
#endif
WebAssembly = {
// Note that we do not use closure quoting (this['buffer'], etc.) on these
// functions, as they are just meant for internal use. In other words, this is
// not a fully general polyfill.
/** @constructor */
Memory: function(opts) {
#if SHARED_MEMORY
this.buffer = new SharedArrayBuffer(opts['initial'] * {{{ WASM_PAGE_SIZE }}});
#else
this.buffer = new ArrayBuffer(opts['initial'] * {{{ WASM_PAGE_SIZE }}});
#endif
},
#if RELOCATABLE
// Only needed in RELOCATABLE builds since normal builds export the table
// from the wasm module.
// Table is not a normal constructor and instead returns the array object.
// That lets us use the length property automatically, which is simpler and
// smaller (but instanceof will not report that an instance of Table is an
// instance of this function).
Table: /** @constructor */ function(opts) {
var ret = new Array(opts['initial']);
#if ALLOW_TABLE_GROWTH
ret.grow = function(by) {
ret.push(null);
};
#else
#if ASSERTIONS // without assertions we'll throw on calling the missing function
ret.grow = function(by) {
abort('Unable to grow wasm table. Build with ALLOW_TABLE_GROWTH.')
};
#endif // ASSERTIONS
#endif // ALLOW_TABLE_GROWTH
ret.set = function(i, func) {
ret[i] = func;
};
ret.get = function(i) {
return ret[i];
};
return ret;
},
#endif
Module: function(binary) {
// TODO: use the binary and info somehow - right now the wasm2js output is embedded in
// the main JS
},
/** @constructor */
Instance: function(module, info) {
// TODO: use the module and info somehow - right now the wasm2js output is embedded in
// the main JS
// This will be replaced by the actual wasm2js code.
this.exports = Module['__wasm2jsInstantiate__'](asmLibraryArg);
},
instantiate: /** @suppress{checkTypes} */ function(binary, info) {
return {
then: function(ok) {
var module = new WebAssembly.Module(binary);
ok({
#if SHARED_MEMORY
'module': module,
#endif
'instance': new WebAssembly.Instance(module)
});
#if ASSERTIONS || WASM == 2 // see postamble_minimal.js which uses .catch
// Emulate a simple WebAssembly.instantiate(..).then(()=>{}).catch(()=>{}) syntax.
return { catch: function() {} };
#endif
}
};
},
RuntimeError: Error
};
#if !MINIMAL_RUNTIME
// We don't need to actually download a wasm binary, mark it as present but empty.
wasmBinary = [];
#endif