Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/src/shell_minimal.js
4128 views
1
/**
2
* @license
3
* Copyright 2010 The Emscripten Authors
4
* SPDX-License-Identifier: MIT
5
*/
6
7
#if MODULARIZE
8
var Module = moduleArg;
9
#elif USE_CLOSURE_COMPILER
10
/** @type{Object} */
11
var Module;
12
// if (!Module)` is crucial for Closure Compiler here as it will
13
// otherwise replace every `Module` occurrence with the object below
14
if (!Module) /** @suppress{checkTypes}*/Module =
15
#if AUDIO_WORKLET
16
globalThis.{{{ EXPORT_NAME }}} ||
17
#endif
18
{"__EMSCRIPTEN_PRIVATE_MODULE_EXPORT_NAME_SUBSTITUTION__":1};
19
20
#elif ENVIRONMENT_MAY_BE_NODE || ENVIRONMENT_MAY_BE_SHELL
21
22
// When running on the web we expect Module to be defined externally, in the
23
// HTML. Otherwise we must define it here before its first use
24
var Module =
25
#if SUPPORTS_GLOBALTHIS
26
// As a small code size optimization, we can use 'globalThis' to refer to the global scope Module variable.
27
globalThis.{{{ EXPORT_NAME }}} || {};
28
#else
29
// Otherwise do a good old typeof check.
30
typeof {{{ EXPORT_NAME }}} != 'undefined' ? {{{ EXPORT_NAME }}} : {};
31
#endif
32
33
#else
34
var Module = {{{ EXPORT_NAME }}};
35
#endif
36
37
#if ENVIRONMENT_MAY_BE_NODE
38
var ENVIRONMENT_IS_NODE = {{{ nodeDetectionCode() }}};
39
#endif
40
41
#if ENVIRONMENT_MAY_BE_SHELL
42
var ENVIRONMENT_IS_SHELL = typeof read == 'function';
43
#endif
44
45
#if ASSERTIONS || PTHREADS
46
#if !ENVIRONMENT_MAY_BE_NODE && !ENVIRONMENT_MAY_BE_SHELL
47
var ENVIRONMENT_IS_WEB = true
48
#elif ENVIRONMENT.length == 1
49
var ENVIRONMENT_IS_WEB = {{{ ENVIRONMENT[0] === 'web' }}};
50
#elif ENVIRONMENT_MAY_BE_SHELL && ENVIRONMENT_MAY_BE_NODE
51
var ENVIRONMENT_IS_WEB = !ENVIRONMENT_IS_NODE && !ENVIRONMENT_IS_SHELL;
52
#elif ENVIRONMENT_MAY_BE_SHELL
53
var ENVIRONMENT_IS_WEB = !ENVIRONMENT_IS_SHELL;
54
#else
55
var ENVIRONMENT_IS_WEB = !ENVIRONMENT_IS_NODE;
56
#endif
57
#endif // ASSERTIONS || PTHREADS
58
59
#if ENVIRONMENT_MAY_BE_NODE && (PTHREADS || WASM_WORKERS)
60
if (ENVIRONMENT_IS_NODE) {
61
var worker_threads = require('worker_threads');
62
global.Worker = worker_threads.Worker;
63
}
64
#endif
65
66
#if WASM_WORKERS
67
var ENVIRONMENT_IS_WASM_WORKER = globalThis.name == 'em-ww';
68
69
#if ENVIRONMENT_MAY_BE_NODE
70
if (ENVIRONMENT_IS_NODE) {
71
// The way we signal to a worker that it is hosting a pthread is to construct
72
// it with a specific name.
73
ENVIRONMENT_IS_WASM_WORKER = worker_threads['workerData'] == 'em-ww'
74
}
75
#endif
76
#endif
77
78
#if AUDIO_WORKLET
79
var ENVIRONMENT_IS_AUDIO_WORKLET = typeof AudioWorkletGlobalScope !== 'undefined';
80
if (ENVIRONMENT_IS_AUDIO_WORKLET) ENVIRONMENT_IS_WASM_WORKER = true;
81
#endif
82
83
#if ASSERTIONS && ENVIRONMENT_MAY_BE_NODE && ENVIRONMENT_MAY_BE_SHELL
84
if (ENVIRONMENT_IS_NODE && ENVIRONMENT_IS_SHELL) {
85
throw 'unclear environment';
86
}
87
#endif
88
89
// Redefine these in a --pre-js to override behavior. If you would like to
90
// remove out() or err() altogether, you can no-op it out to function() {},
91
// and build with --closure 1 to get Closure optimize out all the uses
92
// altogether.
93
94
#if ENVIRONMENT_MAY_BE_NODE && PTHREADS
95
// Set up the out() and err() hooks, which are how we can print to stdout or
96
// stderr, respectively.
97
// Normally just binding console.log/console.error here works fine, but
98
// under node (with workers) we see missing/out-of-order messages so route
99
// directly to stdout and stderr.
100
// See https://github.com/emscripten-core/emscripten/issues/14804
101
var defaultPrint = console.log.bind(console);
102
var defaultPrintErr = console.error.bind(console);
103
if (ENVIRONMENT_IS_NODE) {
104
var fs = require('fs');
105
defaultPrint = (...args) => fs.writeSync(1, args.join(' ') + '\n');
106
defaultPrintErr = (...args) => fs.writeSync(2, args.join(' ') + '\n');
107
}
108
var out = defaultPrint;
109
var err = defaultPrintErr;
110
#else
111
var out = (...args) => console.log(...args);
112
var err = (...args) => console.error(...args);
113
#endif
114
115
// Override this function in a --pre-js file to get a signal for when
116
// compilation is ready. In that callback, call the function run() to start
117
// the program.
118
function ready() {
119
#if MODULARIZE
120
readyPromiseResolve?.(Module);
121
#endif // MODULARIZE
122
#if INVOKE_RUN && HAS_MAIN
123
{{{ runIfMainThread("run();") }}}
124
#elif ASSERTIONS
125
out('ready() called, and INVOKE_RUN=0. The runtime is now ready for you to call run() to invoke application _main(). You can also override ready() in a --pre-js file to get this signal as a callback')
126
#endif
127
#if PTHREADS
128
// This Worker is now ready to host pthreads, tell the main thread we can proceed.
129
if (ENVIRONMENT_IS_PTHREAD) {
130
startWorker();
131
}
132
#endif
133
}
134
135
#if PTHREADS
136
// MINIMAL_RUNTIME does not support --proxy-to-worker option, so Worker and Pthread environments
137
// coincide.
138
var ENVIRONMENT_IS_WORKER = typeof WorkerGlobalScope != 'undefined';
139
var ENVIRONMENT_IS_PTHREAD = ENVIRONMENT_IS_WORKER && self.name?.startsWith('em-pthread');
140
141
#if !MODULARIZE
142
// In MODULARIZE mode _scriptName needs to be captured already at the very top of the page immediately when the page is parsed, so it is generated there
143
// before the page load. In non-MODULARIZE modes generate it here.
144
var _scriptName = typeof document != 'undefined' ? document.currentScript?.src : undefined;
145
#endif
146
147
#if ENVIRONMENT_MAY_BE_NODE
148
if (ENVIRONMENT_IS_NODE) {
149
ENVIRONMENT_IS_WORKER = !worker_threads.isMainThread;
150
// Under node we set `workerData` to `em-pthread` to signal that the worker
151
// is hosting a pthread.
152
ENVIRONMENT_IS_PTHREAD = ENVIRONMENT_IS_WORKER && worker_threads['workerData'] == 'em-pthread'
153
#if !EXPORT_ES6
154
_scriptName = __filename;
155
#endif
156
} else
157
#endif // ENVIRONMENT_MAY_BE_NODE
158
if (ENVIRONMENT_IS_WORKER) {
159
_scriptName = self.location.href;
160
}
161
#endif // PTHREADS
162
163
// --pre-jses are emitted after the Module integration code, so that they can
164
// refer to Module (if they choose; they can also define Module)
165
{{{ preJS() }}}
166
167
#if !SINGLE_FILE
168
169
#if PTHREADS
170
if (!ENVIRONMENT_IS_PTHREAD) {
171
#endif
172
173
#if ENVIRONMENT_MAY_BE_NODE && ((WASM == 1 && !WASM2JS) || WASM == 2)
174
// Wasm or Wasm2JS loading:
175
176
if (ENVIRONMENT_IS_NODE) {
177
var fs = require('fs');
178
#if WASM == 2
179
if (typeof WebAssembly != 'undefined') Module['wasm'] = fs.readFileSync(__dirname + '/{{{ TARGET_BASENAME }}}.wasm');
180
else eval(fs.readFileSync(__dirname + '/{{{ TARGET_BASENAME }}}.wasm.js')+'');
181
#else
182
#if !WASM2JS
183
Module['wasm'] = fs.readFileSync(__dirname + '/{{{ TARGET_BASENAME }}}.wasm');
184
#endif
185
#endif
186
}
187
#endif
188
189
#if ENVIRONMENT_MAY_BE_SHELL && ((WASM == 1 && !WASM2JS) || WASM == 2)
190
if (ENVIRONMENT_IS_SHELL) {
191
#if WASM == 2
192
if (typeof WebAssembly != 'undefined') Module['wasm'] = read('{{{ TARGET_BASENAME }}}.wasm', 'binary');
193
else eval(read('{{{ TARGET_BASENAME }}}.wasm.js')+'');
194
#else
195
#if !WASM2JS
196
Module['wasm'] = read('{{{ TARGET_BASENAME }}}.wasm', 'binary');
197
#endif
198
#endif
199
}
200
#endif
201
202
#if PTHREADS
203
}
204
#endif
205
206
#endif // !SINGLE_FILE
207
208
209