Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/src/modularize.js
4128 views
1
/**
2
* @license
3
* Copyright 2025 The Emscripten Authors
4
* SPDX-License-Identifier: MIT
5
*/
6
7
// This code implements the `-sMODULARIZE` settings by taking the generated
8
// JS program code (INNER_JS_CODE) and wrapping it in a factory function.
9
10
#if SOURCE_PHASE_IMPORTS
11
import source wasmModule from './{{{ WASM_BINARY_FILE }}}';
12
#endif
13
14
#if ENVIRONMENT_MAY_BE_WEB && !EXPORT_ES6 && !(MINIMAL_RUNTIME && !PTHREADS)
15
// Single threaded MINIMAL_RUNTIME programs do not need access to
16
// document.currentScript, so a simple export declaration is enough.
17
var {{{ EXPORT_NAME }}} = (() => {
18
// When MODULARIZE this JS may be executed later,
19
// after document.currentScript is gone, so we save it.
20
// In EXPORT_ES6 mode we can just use 'import.meta.url'.
21
var _scriptName = typeof document != 'undefined' ? document.currentScript?.src : undefined;
22
return async function(moduleArg = {}) {
23
var moduleRtn;
24
25
"<<< INNER_JS_CODE >>>"
26
27
return moduleRtn;
28
};
29
})();
30
#else
31
// When targetting node and ES6 we use `await import ..` in the generated code
32
// so the outer function needs to be marked as async.
33
async function {{{ EXPORT_NAME }}}(moduleArg = {}) {
34
var moduleRtn;
35
36
"<<< INNER_JS_CODE >>>"
37
38
return moduleRtn;
39
}
40
#endif
41
42
// Export using a UMD style export, or ES6 exports if selected
43
#if EXPORT_ES6
44
export default {{{ EXPORT_NAME }}};
45
#elif !MINIMAL_RUNTIME
46
if (typeof exports === 'object' && typeof module === 'object') {
47
module.exports = {{{ EXPORT_NAME }}};
48
// This default export looks redundant, but it allows TS to import this
49
// commonjs style module.
50
module.exports.default = {{{ EXPORT_NAME }}};
51
} else if (typeof define === 'function' && define['amd'])
52
define([], () => {{{ EXPORT_NAME }}});
53
#endif
54
55
#if PTHREADS
56
57
// Create code for detecting if we are running in a pthread.
58
// Normally this detection is done when the module is itself run but
59
// when running in MODULARIZE mode we need use this to know if we should
60
// run the module constructor on startup (true only for pthreads).
61
#if ENVIRONMENT_MAY_BE_WEB || ENVIRONMENT_MAY_BE_WORKER
62
var isPthread = globalThis.self?.name?.startsWith('em-pthread');
63
#if ENVIRONMENT_MAY_BE_NODE
64
// In order to support both web and node we also need to detect node here.
65
var isNode = {{{ nodeDetectionCode() }}};
66
if (isNode) isPthread = {{{ nodePthreadDetection() }}}
67
#endif
68
#else ENVIRONMENT_MAY_BE_NODE
69
var isPthread = {{{ nodePthreadDetection() }}}
70
// When running as a pthread, construct a new instance on startup
71
#endif
72
73
#if MODULARIZE == 'instance'
74
isPthread && init();
75
#else
76
isPthread && {{{ EXPORT_NAME }}}();
77
#endif
78
79
#endif // PTHREADS
80
81
#if WASM_WORKERS
82
83
// Same as above for for WASM_WORKERS
84
// Normally this detection is done when the module is itself run but
85
// when running in MODULARIZE mode we need use this to know if we should
86
// run the module constructor on startup (true only for pthreads).
87
#if ENVIRONMENT_MAY_BE_WEB || ENVIRONMENT_MAY_BE_WORKER
88
var isWW = globalThis.self?.name == 'em-ww';
89
// In order to support both web and node we also need to detect node here.
90
#if ENVIRONMENT_MAY_BE_NODE
91
#if !PTHREADS
92
var isNode = {{{ nodeDetectionCode() }}};
93
#endif
94
if (isNode) isWW = {{{ nodeWWDetection() }}};
95
#endif
96
#elif ENVIRONMENT_MAY_BE_NODE
97
var isWW = {{{ nodeWWDetection() }}};
98
#endif
99
100
#if AUDIO_WORKLET
101
isWW ||= typeof AudioWorkletGlobalScope !== 'undefined';
102
// When running as a wasm worker, construct a new instance on startup
103
#endif
104
105
#if MODULARIZE == 'instance'
106
isWW && init();
107
#else
108
isWW && {{{ EXPORT_NAME }}}();
109
#endif
110
111
#endif // WASM_WORKERS
112
113