Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/src/modularize.js
6162 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
#if MIN_FIREFOX_VERSION < 74 || LEGACY_VM_SUPPORT
22
// This modularize.js script is not Babeled, so manually adapt for old browsers.
23
var _scriptName = typeof document !== 'undefined' && document.currentScript ? document.currentScript.src : undefined;
24
#else
25
var _scriptName = globalThis.document?.currentScript?.src;
26
#endif
27
return async function(moduleArg = {}) {
28
var moduleRtn;
29
30
"<<< INNER_JS_CODE >>>"
31
32
return moduleRtn;
33
};
34
})();
35
#else
36
// When targeting node and ES6 we use `await import ..` in the generated code
37
// so the outer function needs to be marked as async.
38
async function {{{ EXPORT_NAME }}}(moduleArg = {}) {
39
var moduleRtn;
40
41
"<<< INNER_JS_CODE >>>"
42
43
return moduleRtn;
44
}
45
#endif
46
47
// Export using a UMD style export, or ES6 exports if selected
48
#if EXPORT_ES6
49
export default {{{ EXPORT_NAME }}};
50
#elif !MINIMAL_RUNTIME
51
if (typeof exports === 'object' && typeof module === 'object') {
52
module.exports = {{{ EXPORT_NAME }}};
53
// This default export looks redundant, but it allows TS to import this
54
// commonjs style module.
55
module.exports.default = {{{ EXPORT_NAME }}};
56
} else if (typeof define === 'function' && define['amd'])
57
define([], () => {{{ EXPORT_NAME }}});
58
#endif
59
60
#if PTHREADS
61
62
// Create code for detecting if we are running in a pthread.
63
// Normally this detection is done when the module is itself run but
64
// when running in MODULARIZE mode we need use this to know if we should
65
// run the module constructor on startup (true only for pthreads).
66
#if ENVIRONMENT_MAY_BE_WEB || ENVIRONMENT_MAY_BE_WORKER
67
var isPthread = globalThis.self?.name?.startsWith('em-pthread');
68
#if ENVIRONMENT_MAY_BE_NODE
69
// In order to support both web and node we also need to detect node here.
70
var isNode = {{{ nodeDetectionCode() }}};
71
if (isNode) isPthread = {{{ nodePthreadDetection() }}}
72
#endif
73
#else ENVIRONMENT_MAY_BE_NODE
74
var isPthread = {{{ nodePthreadDetection() }}}
75
// When running as a pthread, construct a new instance on startup
76
#endif
77
78
#if MODULARIZE == 'instance'
79
isPthread && init();
80
#else
81
isPthread && {{{ EXPORT_NAME }}}();
82
#endif
83
84
#endif // PTHREADS
85
86
#if WASM_WORKERS
87
88
// Same as above for for WASM_WORKERS
89
// Normally this detection is done when the module is itself run but
90
// when running in MODULARIZE mode we need use this to know if we should
91
// run the module constructor on startup (true only for pthreads).
92
#if ENVIRONMENT_MAY_BE_WEB || ENVIRONMENT_MAY_BE_WORKER
93
var isWW = globalThis.self?.name == 'em-ww';
94
// In order to support both web and node we also need to detect node here.
95
#if ENVIRONMENT_MAY_BE_NODE
96
#if !PTHREADS
97
var isNode = {{{ nodeDetectionCode() }}};
98
#endif
99
if (isNode) isWW = {{{ nodeWWDetection() }}};
100
#endif
101
#elif ENVIRONMENT_MAY_BE_NODE
102
var isWW = {{{ nodeWWDetection() }}};
103
#endif
104
105
#if AUDIO_WORKLET
106
isWW ||= !!globalThis.AudioWorkletGlobalScope;
107
// When running as a wasm worker, construct a new instance on startup
108
#endif
109
110
#if MODULARIZE == 'instance'
111
isWW && init();
112
#else
113
isWW && {{{ EXPORT_NAME }}}();
114
#endif
115
116
#endif // WASM_WORKERS
117
118