Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/src/postamble_minimal.js
4128 views
1
/**
2
* @license
3
* Copyright 2019 The Emscripten Authors
4
* SPDX-License-Identifier: MIT
5
*/
6
7
// === Auto-generated postamble setup entry stuff ===
8
#if HAS_MAIN // Only if user is exporting a C main(), we will generate a run() function that can be used to launch main.
9
10
#if EXIT_RUNTIME
11
function exitRuntime(ret) {
12
<<< ATEXITS >>>
13
#if PTHREADS
14
PThread.terminateAllThreads();
15
#endif
16
17
#if ASSERTIONS
18
runtimeExited = true;
19
#endif
20
21
_proc_exit(ret);
22
23
#if STACK_OVERFLOW_CHECK
24
checkStackCookie();
25
#endif
26
}
27
#endif
28
29
function run() {
30
#if MEMORYPROFILER
31
emscriptenMemoryProfiler.onPreloadComplete();
32
#endif
33
34
<<< ATMAINS >>>
35
36
#if PROXY_TO_PTHREAD
37
// User requested the PROXY_TO_PTHREAD option, so call a stub main which
38
// pthread_create()s a new thread that will call the user's real main() for
39
// the application.
40
__emscripten_proxy_main();
41
#elif ASYNCIFY == 2 && EXIT_RUNTIME
42
// In JSPI-enabled build mode, the main() function will return a Promise,
43
// which resolves to the process exit code.
44
_main().then(exitRuntime);
45
#elif EXIT_RUNTIME
46
// In regular exitRuntime mode, exit with the given return code from main().
47
exitRuntime(_main());
48
#else
49
// Run a persistent (never-exiting) application starting at main().
50
_main();
51
#endif
52
53
#if STACK_OVERFLOW_CHECK
54
checkStackCookie();
55
#endif
56
<<< ATPOSTRUNS >>>
57
}
58
#endif
59
60
function initRuntime(wasmExports) {
61
#if ASSERTIONS || SAFE_HEAP || USE_ASAN || MODULARIZE
62
runtimeInitialized = true;
63
#endif
64
65
#if PTHREADS
66
PThread.tlsInitFunctions.push(wasmExports['_emscripten_tls_init']);
67
if (ENVIRONMENT_IS_PTHREAD) return;
68
#endif
69
70
#if WASM_WORKERS
71
if (ENVIRONMENT_IS_WASM_WORKER) return _wasmWorkerInitializeRuntime();
72
#endif
73
74
#if STACK_OVERFLOW_CHECK
75
_emscripten_stack_init();
76
#if STACK_OVERFLOW_CHECK >= 2
77
setStackLimits();
78
#endif
79
writeStackCookie();
80
#endif
81
82
<<< ATINITS >>>
83
84
#if hasExportedSymbol('__wasm_call_ctors')
85
wasmExports['__wasm_call_ctors']();
86
#endif
87
88
<<< ATPOSTCTORS >>>
89
}
90
91
// Initialize wasm (asynchronous)
92
93
#if SINGLE_FILE && WASM == 1 && !WASM2JS
94
Module['wasm'] = base64Decode('<<< WASM_BINARY_DATA >>>');
95
#endif
96
97
#if LibraryManager.has('libexports.js')
98
// emscripten_get_exported_function() requires wasmExports to be defined in the
99
// outer scope.
100
var wasmExports;
101
#endif
102
103
#if PTHREADS || WASM_WORKERS
104
var wasmModule;
105
106
function loadModule() {
107
assignWasmImports();
108
#endif
109
110
#if ASYNCIFY
111
Asyncify.instrumentWasmImports(wasmImports);
112
#endif
113
114
var imports = {
115
#if MINIFY_WASM_IMPORTED_MODULES
116
'a': wasmImports,
117
#else // MINIFY_WASM_IMPORTED_MODULES
118
'env': wasmImports,
119
'{{{ WASI_MODULE_NAME }}}': wasmImports,
120
#endif // MINIFY_WASM_IMPORTED_MODULES
121
};
122
123
#if MINIMAL_RUNTIME_STREAMING_WASM_INSTANTIATION
124
// https://caniuse.com/#feat=wasm and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/instantiateStreaming
125
#if MIN_FIREFOX_VERSION < 58 || MIN_CHROME_VERSION < 61 || MIN_SAFARI_VERSION < 150000 || ENVIRONMENT_MAY_BE_NODE
126
#if ASSERTIONS && !WASM2JS
127
// Module['wasm'] should contain a typed array of the Wasm object data, or a
128
// precompiled WebAssembly Module.
129
assert(WebAssembly.instantiateStreaming || Module['wasm'], 'Must load WebAssembly Module in to variable Module.wasm before adding compiled output .js script to the DOM');
130
#endif
131
#if AUDIO_WORKLET
132
instantiatePromise =
133
#endif
134
(WebAssembly.instantiateStreaming
135
#if ENVIRONMENT_MAY_BE_NODE
136
// Node's fetch API cannot be used for local files, so we cannot use instantiateStreaming
137
&& !ENVIRONMENT_IS_NODE
138
#endif
139
? WebAssembly.instantiateStreaming(fetch('{{{ TARGET_BASENAME }}}.wasm'), imports)
140
: WebAssembly.instantiate(Module['wasm'], imports)).then((output) => {
141
#else
142
#if AUDIO_WORKLET
143
instantiatePromise =
144
#endif
145
WebAssembly.instantiateStreaming(fetch('{{{ TARGET_BASENAME }}}.wasm'), imports).then((output) => {
146
#endif
147
148
#else // Non-streaming instantiation
149
#if ASSERTIONS && !WASM2JS
150
// Module['wasm'] should contain a typed array of the Wasm object data, or a
151
// precompiled WebAssembly Module.
152
assert(Module['wasm'], 'Must load WebAssembly Module in to variable Module.wasm before adding compiled output .js script to the DOM');
153
#endif
154
155
<<< ATMODULES >>>
156
157
{{{ exportJSSymbols() }}}
158
159
// Add missingProperties supression here because closure compiler doesn't know that
160
// WebAssembly.instantiate is polymorphic in its return value.
161
#if AUDIO_WORKLET
162
instantiatePromise =
163
#endif
164
WebAssembly.instantiate(Module['wasm'], imports).then(/** @suppress {missingProperties} */ (output) => {
165
#endif
166
167
#if !LibraryManager.has('libexports.js') && ASYNCIFY != 1
168
// If not using the emscripten_get_exported_function() API, keep the
169
// `wasmExports` variable in local scope to this instantiate function to save
170
// code size. (otherwise access it without to export it to outer scope)
171
var
172
#endif
173
// WebAssembly instantiation API gotcha: if Module['wasm'] above was a typed
174
// array, then the output object will have an output.instance and
175
// output.module objects. But if Module['wasm'] is an already compiled
176
// WebAssembly module, then output is the WebAssembly instance itself.
177
// Depending on the build mode, Module['wasm'] can mean a different thing.
178
#if PTHREADS || WASM_WORKERS
179
// In pthreads and wasm workers, Module['wasm'] is a compiled
180
// WebAssembly.Module. In that case, 'output' is a WebAssembly.Instance.
181
// In main thread, Module['wasm'] is either a typed array or a fetch stream.
182
// In that case, 'output.instance' is the WebAssembly.Instance.
183
wasmExports = (output.instance || output).exports;
184
// Stash the Wasm module for future worker creation.
185
wasmModule = output.module || Module['wasm'];
186
#elif MINIMAL_RUNTIME_STREAMING_WASM_COMPILATION
187
// In MINIMAL_RUNTIME_STREAMING_WASM_COMPILATION mode, Module['wasm'] is the
188
// compiled module so we just get the instance back.
189
wasmExports = output.exports;
190
#else
191
wasmExports = output.instance.exports;
192
#endif
193
194
#if ASYNCIFY
195
wasmExports = Asyncify.instrumentWasmExports(wasmExports);
196
#endif
197
198
#if MEMORY64 || CAN_ADDRESS_2GB
199
wasmExports = applySignatureConversions(wasmExports);
200
#endif
201
202
#if !DECLARE_ASM_MODULE_EXPORTS
203
exportWasmSymbols(wasmExports);
204
#else
205
assignWasmExports(wasmExports);
206
#endif
207
#if '$wasmTable' in addedLibraryItems
208
wasmTable = wasmExports['__indirect_function_table'];
209
#if ASSERTIONS
210
assert(wasmTable);
211
#endif
212
#endif
213
214
#if AUDIO_WORKLET
215
// If we are in the audio worklet environment, we can only access the Module object
216
// and not the global scope of the main JS script. Therefore we need to export
217
// all symbols that the audio worklet scope needs onto the Module object.
218
#if ASSERTIONS
219
// In ASSERTIONS-enabled builds, the needed symbols have gotten read-only getters
220
// saved to the Module. Remove the getters so we can manually export them here.
221
delete Module['stackSave'];
222
delete Module['stackAlloc'];
223
delete Module['stackRestore'];
224
delete Module['wasmTable'];
225
#endif
226
Module['stackSave'] = stackSave;
227
Module['stackAlloc'] = stackAlloc;
228
Module['stackRestore'] = stackRestore;
229
Module['wasmTable'] = wasmTable;
230
#endif
231
232
#if !IMPORTED_MEMORY
233
wasmMemory = wasmExports['memory'];
234
#if ASSERTIONS
235
assert(wasmMemory);
236
#endif
237
updateMemoryViews();
238
#endif
239
<<< ATPRERUNS >>>
240
241
initRuntime(wasmExports);
242
243
#if PTHREADS && PTHREAD_POOL_SIZE
244
var workersReady = PThread.loadWasmModuleToAllWorkers();
245
#if PTHREAD_POOL_DELAY_LOAD
246
ready();
247
#else
248
workersReady.then(ready);
249
#endif
250
#else
251
ready();
252
#endif
253
}
254
255
#if WASM == 2
256
, (error) => {
257
#if ASSERTIONS
258
console.error(error);
259
#endif
260
261
#if ENVIRONMENT_MAY_BE_NODE || ENVIRONMENT_MAY_BE_SHELL
262
if (typeof location != 'undefined') {
263
#endif
264
// WebAssembly compilation failed, try running the JS fallback instead.
265
var search = location.search;
266
if (search.indexOf('_rwasm=0') < 0) {
267
location.href += (search ? search + '&' : '?') + '_rwasm=0';
268
}
269
#if ENVIRONMENT_MAY_BE_NODE || ENVIRONMENT_MAY_BE_SHELL
270
}
271
#endif
272
}
273
#endif // WASM == 2
274
);
275
276
#if PTHREADS || WASM_WORKERS
277
}
278
279
// When running in a background thread we delay module loading until we have
280
{{{ runIfMainThread('loadModule();') }}}
281
#endif
282
283