Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/src/wasm2js.js
6165 views
1
/**
2
* @license
3
* Copyright 2019 The Emscripten Authors
4
* SPDX-License-Identifier: MIT
5
*/
6
7
// wasm2js.js - enough of a polyfill for the WebAssembly object so that we can load
8
// wasm2js code that way.
9
10
/** @suppress{duplicate, const, checkTypes} */
11
var WebAssembly = {
12
// Note that we do not use closure quoting (this['buffer'], etc.) on these
13
// functions, as they are just meant for internal use. In other words, this is
14
// not a fully general polyfill.
15
/** @constructor */
16
Memory: function(opts) {
17
#if SHARED_MEMORY
18
this.buffer = new SharedArrayBuffer(opts['initial'] * {{{ WASM_PAGE_SIZE }}});
19
#else
20
this.buffer = new ArrayBuffer(opts['initial'] * {{{ WASM_PAGE_SIZE }}});
21
#endif
22
},
23
24
#if RELOCATABLE
25
// Only needed in RELOCATABLE builds since normal builds export the table
26
// from the wasm module.
27
// Table is not a normal constructor and instead returns the array object.
28
// That lets us use the length property automatically, which is simpler and
29
// smaller (but instanceof will not report that an instance of Table is an
30
// instance of this function).
31
Table: /** @constructor */ function(opts) {
32
var ret = new Array(opts['initial']);
33
#if ALLOW_TABLE_GROWTH
34
ret.grow = function(by) {
35
ret.push(null);
36
};
37
#else
38
#if ASSERTIONS // without assertions we'll throw on calling the missing function
39
ret.grow = function(by) {
40
abort('Unable to grow wasm table. Build with ALLOW_TABLE_GROWTH.')
41
};
42
#endif // ASSERTIONS
43
#endif // ALLOW_TABLE_GROWTH
44
ret.set = function(i, func) {
45
ret[i] = func;
46
};
47
ret.get = function(i) {
48
return ret[i];
49
};
50
return ret;
51
},
52
#endif
53
54
Module: function(binary) {
55
// TODO: use the binary and info somehow - right now the wasm2js output is embedded in
56
// the main JS
57
},
58
59
/** @constructor */
60
Instance: function(module, info) {
61
// TODO: use the module somehow - right now the wasm2js output is embedded in
62
// the main JS
63
// This will be replaced by the actual wasm2js code.
64
this.exports = Module['__wasm2jsInstantiate__'](info);
65
},
66
67
instantiate: /** @suppress{checkTypes} */ function(binary, info) {
68
return {
69
then: function(ok) {
70
var module = new WebAssembly.Module(binary);
71
ok({
72
#if SHARED_MEMORY
73
'module': module,
74
#endif
75
'instance': new WebAssembly.Instance(module, info)
76
});
77
#if ASSERTIONS || WASM == 2 // see postamble_minimal.js which uses .catch
78
// Emulate a simple WebAssembly.instantiate(..).then(()=>{}).catch(()=>{}) syntax.
79
return { catch: function() {} };
80
#endif
81
}
82
};
83
},
84
85
RuntimeError: Error,
86
87
#if !MINIMAL_RUNTIME
88
isWasm2js: true,
89
#endif
90
};
91
92