-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
Copy pathpostamble_minimal.js
266 lines (228 loc) · 7.77 KB
/
postamble_minimal.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
/**
* @license
* Copyright 2019 The Emscripten Authors
* SPDX-License-Identifier: MIT
*/
// === Auto-generated postamble setup entry stuff ===
#if HAS_MAIN // Only if user is exporting a C main(), we will generate a run() function that can be used to launch main.
function run() {
#if MEMORYPROFILER
emscriptenMemoryProfiler.onPreloadComplete();
#endif
<<< ATMAINS >>>
#if PROXY_TO_PTHREAD
// User requested the PROXY_TO_PTHREAD option, so call a stub main which
// pthread_create()s a new thread that will call the user's real main() for
// the application.
var ret = __emscripten_proxy_main();
#else
var ret = _main();
#if EXIT_RUNTIME
<<< ATEXITS >>>
#if PTHREADS
PThread.terminateAllThreads();
#endif
#endif
#if EXIT_RUNTIME
#if ASSERTIONS
runtimeExited = true;
#endif
_proc_exit(ret);
#endif
#endif // PROXY_TO_PTHREAD
#if STACK_OVERFLOW_CHECK
checkStackCookie();
#endif
<<< ATPOSTRUNS >>>
}
#endif
function initRuntime(wasmExports) {
#if ASSERTIONS || SAFE_HEAP || USE_ASAN
runtimeInitialized = true;
#endif
#if PTHREADS
if (ENVIRONMENT_IS_PTHREAD) return
#endif
#if WASM_WORKERS
if (ENVIRONMENT_IS_WASM_WORKER) return _wasmWorkerInitializeRuntime();
#endif
#if STACK_OVERFLOW_CHECK
_emscripten_stack_init();
writeStackCookie();
#if STACK_OVERFLOW_CHECK >= 2
setStackLimits();
#endif
#endif
#if PTHREADS
PThread.tlsInitFunctions.push(wasmExports['_emscripten_tls_init']);
#endif
<<< ATINITS >>>
#if hasExportedSymbol('__wasm_call_ctors')
wasmExports['__wasm_call_ctors']();
#endif
<<< ATPOSTCTORS >>>
}
// Initialize wasm (asynchronous)
#if SINGLE_FILE && WASM == 1 && !WASM2JS
Module['wasm'] = base64Decode('<<< WASM_BINARY_DATA >>>');
#endif
#if LibraryManager.has('libexports.js')
// emscripten_get_exported_function() requires wasmExports to be defined in the
// outer scope.
var wasmExports;
#endif
#if PTHREADS
var wasmModule;
#endif
#if PTHREADS
function loadModule() {
assignWasmImports();
#endif
var imports = {
#if MINIFY_WASM_IMPORTED_MODULES
'a': wasmImports,
#else // MINIFY_WASM_IMPORTED_MODULES
'env': wasmImports,
'{{{ WASI_MODULE_NAME }}}': wasmImports,
#endif // MINIFY_WASM_IMPORTED_MODULES
};
#if MINIMAL_RUNTIME_STREAMING_WASM_INSTANTIATION
// https://caniuse.com/#feat=wasm and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/instantiateStreaming
// Firefox 52 added Wasm support, but only Firefox 58 added instantiateStreaming.
// Chrome 57 added Wasm support, but only Chrome 61 added instantiateStreaming.
// Node.js and Safari do not support instantiateStreaming.
#if MIN_FIREFOX_VERSION < 58 || MIN_CHROME_VERSION < 61 || ENVIRONMENT_MAY_BE_NODE || MIN_SAFARI_VERSION != TARGET_NOT_SUPPORTED
#if ASSERTIONS && !WASM2JS
// Module['wasm'] should contain a typed array of the Wasm object data, or a
// precompiled WebAssembly Module.
if (!WebAssembly.instantiateStreaming && !Module['wasm']) throw 'Must load WebAssembly Module in to variable Module.wasm before adding compiled output .js script to the DOM';
#endif
(WebAssembly.instantiateStreaming
? WebAssembly.instantiateStreaming(fetch('{{{ TARGET_BASENAME }}}.wasm'), imports)
: WebAssembly.instantiate(Module['wasm'], imports)).then((output) => {
#else
WebAssembly.instantiateStreaming(fetch('{{{ TARGET_BASENAME }}}.wasm'), imports).then((output) => {
#endif
#else // Non-streaming instantiation
#if ASSERTIONS && !WASM2JS
// Module['wasm'] should contain a typed array of the Wasm object data, or a
// precompiled WebAssembly Module.
if (!Module['wasm']) throw 'Must load WebAssembly Module in to variable Module.wasm before adding compiled output .js script to the DOM';
#endif
<<< ATMODULES >>>
{{{ exportJSSymbols() }}}
WebAssembly.instantiate(Module['wasm'], imports).then((output) => {
#endif
#if !LibraryManager.has('libexports.js')
// If not using the emscripten_get_exported_function() API, keep the
// `wasmExports` variable in local scope to this instantiate function to save
// code size. (otherwise access it without to export it to outer scope)
var
#endif
// WebAssembly instantiation API gotcha: if Module['wasm'] above was a typed
// array, then the output object will have an output.instance and
// output.module objects. But if Module['wasm'] is an already compiled
// WebAssembly module, then output is the WebAssembly instance itself.
// Depending on the build mode, Module['wasm'] can mean a different thing.
#if MINIMAL_RUNTIME_STREAMING_WASM_COMPILATION || MINIMAL_RUNTIME_STREAMING_WASM_INSTANTIATION || PTHREADS
// https://caniuse.com/#feat=wasm and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/instantiateStreaming
// Firefox 52 added Wasm support, but only Firefox 58 added compileStreaming &
// instantiateStreaming.
// Chrome 57 added Wasm support, but only Chrome 61 added compileStreaming &
// instantiateStreaming.
// Node.js and Safari do not support compileStreaming or instantiateStreaming.
#if MIN_FIREFOX_VERSION < 58 || MIN_CHROME_VERSION < 61 || ENVIRONMENT_MAY_BE_NODE || MIN_SAFARI_VERSION != TARGET_NOT_SUPPORTED || PTHREADS
// In pthreads, Module['wasm'] is an already compiled WebAssembly.Module. In
// that case, 'output' is a WebAssembly.Instance.
// In main thread, Module['wasm'] is either a typed array or a fetch stream.
// In that case, 'output.instance' is the WebAssembly.Instance.
wasmExports = (output.instance || output).exports;
#else
wasmExports = output.exports;
#endif
#else
wasmExports = output.instance.exports;
#endif
#if MEMORY64 || CAN_ADDRESS_2GB
wasmExports = applySignatureConversions(wasmExports);
#endif
#if USE_OFFSET_CONVERTER
#if PTHREADS
if (!ENVIRONMENT_IS_PTHREAD)
#endif
wasmOffsetConverter = new WasmOffsetConverter(Module['wasm'], output.module);
#endif
#if !DECLARE_ASM_MODULE_EXPORTS
exportWasmSymbols(wasmExports);
#else
assignWasmExports(wasmExports);
#endif
#if '$wasmTable' in addedLibraryItems
wasmTable = wasmExports['__indirect_function_table'];
#if ASSERTIONS
assert(wasmTable);
#endif
#endif
#if AUDIO_WORKLET
// If we are in the audio worklet environment, we can only access the Module object
// and not the global scope of the main JS script. Therefore we need to export
// all symbols that the audio worklet scope needs onto the Module object.
#if ASSERTIONS
// In ASSERTIONS-enabled builds, the needed symbols have gotten read-only getters
// saved to the Module. Remove the getters so we can manually export them here.
delete Module['stackSave'];
delete Module['stackAlloc'];
delete Module['stackRestore'];
delete Module['wasmTable'];
#endif
Module['stackSave'] = stackSave;
Module['stackAlloc'] = stackAlloc;
Module['stackRestore'] = stackRestore;
Module['wasmTable'] = wasmTable;
#endif
#if !IMPORTED_MEMORY
wasmMemory = wasmExports['memory'];
#if ASSERTIONS
assert(wasmMemory);
#endif
updateMemoryViews();
#endif
<<< ATPRERUNS >>>
initRuntime(wasmExports);
#if PTHREADS
// Export Wasm module for pthread creation to access.
wasmModule = output.module || Module['wasm'];
PThread.loadWasmModuleToAllWorkers(ready);
#else
ready();
#endif
}
#if ASSERTIONS || WASM == 2
, (error) => {
#if ASSERTIONS
console.error(error);
#endif
#if WASM == 2
#if ENVIRONMENT_MAY_BE_NODE || ENVIRONMENT_MAY_BE_SHELL
if (typeof location != 'undefined') {
#endif
// WebAssembly compilation failed, try running the JS fallback instead.
var search = location.search;
if (search.indexOf('_rwasm=0') < 0) {
location.href += (search ? search + '&' : '?') + '_rwasm=0';
}
#if ENVIRONMENT_MAY_BE_NODE || ENVIRONMENT_MAY_BE_SHELL
}
#endif
#endif // WASM == 2
}
#endif // ASSERTIONS || WASM == 2
);
#if PTHREADS
}
if (!ENVIRONMENT_IS_PTHREAD) {
// When running in a pthread we delay module loading untill we have
// received the module via postMessage
loadModule();
}
#endif