/**1* @license2* Copyright 2025 The Emscripten Authors3* SPDX-License-Identifier: MIT4*/56// This code implements the `-sMODULARIZE` settings by taking the generated7// JS program code (INNER_JS_CODE) and wrapping it in a factory function.89#if SOURCE_PHASE_IMPORTS10import source wasmModule from './{{{ WASM_BINARY_FILE }}}';11#endif1213#if ENVIRONMENT_MAY_BE_WEB && !EXPORT_ES6 && !(MINIMAL_RUNTIME && !PTHREADS)14// Single threaded MINIMAL_RUNTIME programs do not need access to15// document.currentScript, so a simple export declaration is enough.16var {{{ EXPORT_NAME }}} = (() => {17// When MODULARIZE this JS may be executed later,18// after document.currentScript is gone, so we save it.19// In EXPORT_ES6 mode we can just use 'import.meta.url'.20#if MIN_FIREFOX_VERSION < 74 || LEGACY_VM_SUPPORT21// This modularize.js script is not Babeled, so manually adapt for old browsers.22var _scriptName = typeof document !== 'undefined' && document.currentScript ? document.currentScript.src : undefined;23#else24var _scriptName = globalThis.document?.currentScript?.src;25#endif26return async function(moduleArg = {}) {27var moduleRtn;2829"<<< INNER_JS_CODE >>>"3031return moduleRtn;32};33})();34#else35// When targeting node and ES6 we use `await import ..` in the generated code36// so the outer function needs to be marked as async.37async function {{{ EXPORT_NAME }}}(moduleArg = {}) {38var moduleRtn;3940"<<< INNER_JS_CODE >>>"4142return moduleRtn;43}44#endif4546// Export using a UMD style export, or ES6 exports if selected47#if EXPORT_ES648export default {{{ EXPORT_NAME }}};49#elif !MINIMAL_RUNTIME50if (typeof exports === 'object' && typeof module === 'object') {51module.exports = {{{ EXPORT_NAME }}};52// This default export looks redundant, but it allows TS to import this53// commonjs style module.54module.exports.default = {{{ EXPORT_NAME }}};55} else if (typeof define === 'function' && define['amd'])56define([], () => {{{ EXPORT_NAME }}});57#endif5859#if PTHREADS6061// Create code for detecting if we are running in a pthread.62// Normally this detection is done when the module is itself run but63// when running in MODULARIZE mode we need use this to know if we should64// run the module constructor on startup (true only for pthreads).65#if ENVIRONMENT_MAY_BE_WEB || ENVIRONMENT_MAY_BE_WORKER66var isPthread = globalThis.self?.name?.startsWith('em-pthread');67#if ENVIRONMENT_MAY_BE_NODE68// In order to support both web and node we also need to detect node here.69var isNode = {{{ nodeDetectionCode() }}};70if (isNode) isPthread = {{{ nodePthreadDetection() }}}71#endif72#else ENVIRONMENT_MAY_BE_NODE73var isPthread = {{{ nodePthreadDetection() }}}74// When running as a pthread, construct a new instance on startup75#endif7677#if MODULARIZE == 'instance'78isPthread && init();79#else80isPthread && {{{ EXPORT_NAME }}}();81#endif8283#endif // PTHREADS8485#if WASM_WORKERS8687// Same as above for for WASM_WORKERS88// Normally this detection is done when the module is itself run but89// when running in MODULARIZE mode we need use this to know if we should90// run the module constructor on startup (true only for pthreads).91#if ENVIRONMENT_MAY_BE_WEB || ENVIRONMENT_MAY_BE_WORKER92var isWW = globalThis.self?.name == 'em-ww';93// In order to support both web and node we also need to detect node here.94#if ENVIRONMENT_MAY_BE_NODE95#if !PTHREADS96var isNode = {{{ nodeDetectionCode() }}};97#endif98if (isNode) isWW = {{{ nodeWWDetection() }}};99#endif100#elif ENVIRONMENT_MAY_BE_NODE101var isWW = {{{ nodeWWDetection() }}};102#endif103104#if AUDIO_WORKLET105isWW ||= !!globalThis.AudioWorkletGlobalScope;106// When running as a wasm worker, construct a new instance on startup107#endif108109#if MODULARIZE == 'instance'110isWW && init();111#else112isWW && {{{ EXPORT_NAME }}}();113#endif114115#endif // WASM_WORKERS116117118