-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathpostamble_minimal.js
More file actions
336 lines (287 loc) · 9.83 KB
/
postamble_minimal.js
File metadata and controls
336 lines (287 loc) · 9.83 KB
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
/**
* @license
* Copyright 2019 The Emscripten Authors
* SPDX-License-Identifier: MIT
*/
#if LOAD_SOURCE_MAP
#include "source_map_support.js"
#endif
// === 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.
#if EXIT_RUNTIME
function exitRuntime(ret) {
<<< ATEXITS >>>
#if PTHREADS
PThread.terminateAllThreads();
#endif
#if ASSERTIONS
runtimeExited = true;
#endif
_proc_exit(ret);
#if STACK_OVERFLOW_CHECK
checkStackCookie();
#endif
}
#endif
{{{ globalThis.argc_argv = function(condition) {
if (!MAIN_READS_PARAMS) return '';
return `argc, ${to64('argv')}`;
}
globalThis.HEAPptr = MEMORY64 ? 'HEAPU64' : 'HEAPU32';
null;
}}}
function run() {
#if MEMORYPROFILER
emscriptenMemoryProfiler.onPreloadComplete();
#endif
<<< ATMAINS >>>
#if MAIN_READS_PARAMS
var args =
#if ENVIRONMENT_MAY_BE_NODE
// Remove Node.js executable name from argc/argv to emulate C/C++ standards.
ENVIRONMENT_IS_NODE ? process.argv.slice(1) :
#endif
[location.href.split('?')[0], ...location.search.slice(1).split('&').map(decodeURIComponent)];
// C standard (C17 §5.1.2.2.1/5): "The parameters argc and argv and the
// strings pointed to by the argv array shall be modifiable by the program,
// and retain their last-stored values between program startup and program
// termination."
// -> in particular this means that the stackAlloc() that we do below shall
// never be undone, and ideally should no longer be considered to be part of
// the stack. Though currently it will be. (TODO: figure if this will ever be
// a problem)
var arg,
argc = args.length,
argv = stackAlloc(argc * {{{ POINTER_SIZE }}} + {{{ POINTER_SIZE }}}),
argvIndex = argv / {{{ POINTER_SIZE }}};
for (arg of args) {{{ HEAPptr }}}[argvIndex++] = {{{ to64('stringToUTF8OnStack(arg)') }}};
// C standard (C17 §5.1.2.2.1/2): "argv[argc] shall be a null pointer."
{{{ HEAPptr }}}[argvIndex] = {{{ to64(0) }}};
#endif
#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.
__emscripten_proxy_main({{{ argc_argv() }}});
#elif ASYNCIFY == 2 && EXIT_RUNTIME
// In JSPI-enabled build mode, the main() function will return a Promise,
// which resolves to the process exit code.
_main({{{ argc_argv() }}}).then(exitRuntime);
#elif EXIT_RUNTIME
// In regular exitRuntime mode, exit with the given return code from main().
try {
exitRuntime(_main({{{ argc_argv() }}}));
} catch(e) {
var exitCode = e.match(/^exit\((\d+)\)$/);
if (exitCode) {
#if RUNTIME_DEBUG
dbg(`main() called ${e}.`); // e.g. "main() called exit(0)."
#endif
#if expectToReceiveOnModule('onExit')
// Report to Module that the program exited.
Module['onExit']?.(exitCode[1]|0);
#endif
} else {
#if RUNTIME_DEBUG
dbg(`main() threw an exception: ${e}.`);
#endif
// Some other exception occurred - re-throw it.
throw e;
}
}
#else
// Run a persistent (never-exiting) application starting at main().
_main({{{ argc_argv() }}});
#endif
#if STACK_OVERFLOW_CHECK
checkStackCookie();
#endif
<<< ATPOSTRUNS >>>
}
#endif
function initRuntime(wasmExports) {
#if ASSERTIONS || SAFE_HEAP || USE_ASAN || MODULARIZE
runtimeInitialized = true;
#endif
#if PTHREADS
PThread.tlsInitFunctions.push(wasmExports['_emscripten_tls_init']);
if (ENVIRONMENT_IS_PTHREAD) return;
#endif
#if WASM_WORKERS
if (ENVIRONMENT_IS_WASM_WORKER) return _wasmWorkerInitializeRuntime();
#endif
#if STACK_OVERFLOW_CHECK
_emscripten_stack_init();
#if STACK_OVERFLOW_CHECK >= 2
setStackLimits();
#endif
writeStackCookie();
#endif
<<< ATINITS >>>
#if hasExportedSymbol('__wasm_call_ctors')
wasmExports['__wasm_call_ctors']();
#endif
<<< ATPOSTCTORS >>>
}
// Initialize wasm (asynchronous)
#if SINGLE_FILE && SINGLE_FILE_BINARY_ENCODE && !WASM2JS
Module['wasm'] = binaryDecode("<<< WASM_BINARY_DATA >>>");
#elif 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 || WASM_WORKERS
var wasmModule;
function loadModule() {
assignWasmImports();
#endif
#if ASYNCIFY
Asyncify.instrumentWasmImports(wasmImports);
#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
{{{
#if EXPORT_ES6 && !ENVIRONMENT_MAY_BE_AUDIO_WORKLET
const moduleUrl = `new URL('${TARGET_BASENAME}.wasm', import.meta.url)`;
#elif !EXPORT_ES6 || AUDIO_WORKLET
const moduleUrl = `'${TARGET_BASENAME}.wasm'`;
#else
const moduleUrl = `ENVIRONMENT_IS_AUDIO_WORKLET ? '${TARGET_BASENAME}.wasm' : new URL('${TARGET_BASENAME}.wasm', import.meta.url)`;
#endif
}}}
// https://caniuse.com/#feat=wasm and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/instantiateStreaming
#if MIN_SAFARI_VERSION < 150000 || ENVIRONMENT_MAY_BE_NODE
#if ASSERTIONS && !WASM2JS
// Module['wasm'] should contain a typed array of the Wasm object data, or a
// precompiled WebAssembly Module.
assert(WebAssembly.instantiateStreaming || Module['wasm'], 'Must load WebAssembly Module in to variable Module.wasm before adding compiled output .js script to the DOM');
#endif
#if AUDIO_WORKLET
instantiatePromise =
#endif
(WebAssembly.instantiateStreaming
#if ENVIRONMENT_MAY_BE_NODE
// Node's fetch API cannot be used for local files, so we cannot use instantiateStreaming
&& !ENVIRONMENT_IS_NODE
#endif
? WebAssembly.instantiateStreaming(fetch({{{ moduleUrl }}}), imports)
: WebAssembly.instantiate(Module['wasm'], imports)).then((output) => {
#else
#if AUDIO_WORKLET
instantiatePromise =
#endif
WebAssembly.instantiateStreaming(fetch({{{ moduleUrl }}}), 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.
assert(Module['wasm'], 'Must load WebAssembly Module in to variable Module.wasm before adding compiled output .js script to the DOM');
#endif
<<< ATMODULES >>>
{{{ exportJSSymbols() }}}
// Add missingProperties supression here because closure compiler doesn't know that
// WebAssembly.instantiate is polymorphic in its return value.
#if AUDIO_WORKLET
instantiatePromise =
#endif
WebAssembly.instantiate(Module['wasm'], imports).then(/** @suppress {missingProperties} */ (output) => {
#endif
#if !LibraryManager.has('libexports.js') && ASYNCIFY != 1
// 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 PTHREADS || WASM_WORKERS
// In pthreads and wasm workers, Module['wasm'] is a 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;
// Stash the Wasm module for future worker creation.
wasmModule = output.module || Module['wasm'];
#elif MINIMAL_RUNTIME_STREAMING_WASM_COMPILATION
// In MINIMAL_RUNTIME_STREAMING_WASM_COMPILATION mode, Module['wasm'] is the
// compiled module so we just get the instance back.
wasmExports = output.exports;
#else
wasmExports = output.instance.exports;
#endif
#if ASYNCIFY
wasmExports = Asyncify.instrumentWasmExports(wasmExports);
#endif
#if MEMORY64 || CAN_ADDRESS_2GB
wasmExports = applySignatureConversions(wasmExports);
#endif
assignWasmExports(wasmExports);
#if !IMPORTED_MEMORY
updateMemoryViews();
#endif
<<< ATPRERUNS >>>
initRuntime(wasmExports);
{{{ function waitOnStartupPromisesAndEmitReady() {
var promises = [];
if (PTHREADS && PTHREAD_POOL_SIZE) {
promises.push('PThread.loadWasmModuleToAllWorkers()');
}
if (LOAD_SOURCE_MAP) {
promises.push('getSourceMapAsync().then(json=>{receiveSourceMapJSON(json)})');
}
if (promises.length == 0) {
return 'ready();'
} else if (promises.length == 1) {
return `${promises[0]}.then(ready);`;
} else {
return `Promise.all(${', '.join(promises)}).then(ready);`
}
}
null;
}}}
#if PTHREADS && PTHREAD_POOL_SIZE && PTHREAD_POOL_DELAY_LOAD
// In PTHREAD_POOL_DELAY_LOAD mode, we kick off loading Wasm Module to all
// PThread Workers, but do not wait on it.
PThread.loadWasmModuleToAllWorkers();
#endif
{{{ waitOnStartupPromisesAndEmitReady(); }}}
}
#if WASM == 2
, (error) => {
#if ASSERTIONS
console.error(error);
#endif
#if ENVIRONMENT_MAY_BE_NODE || ENVIRONMENT_MAY_BE_SHELL
if (globalThis.location) {
#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
);
#if PTHREADS || WASM_WORKERS
}
// When running in a background thread we delay module loading until we have
{{{ runIfMainThread('loadModule();') }}}
#endif