/**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'.20var _scriptName = typeof document != 'undefined' ? document.currentScript?.src : undefined;21return async function(moduleArg = {}) {22var moduleRtn;2324"<<< INNER_JS_CODE >>>"2526return moduleRtn;27};28})();29#else30// When targetting node and ES6 we use `await import ..` in the generated code31// so the outer function needs to be marked as async.32async function {{{ EXPORT_NAME }}}(moduleArg = {}) {33var moduleRtn;3435"<<< INNER_JS_CODE >>>"3637return moduleRtn;38}39#endif4041// Export using a UMD style export, or ES6 exports if selected42#if EXPORT_ES643export default {{{ EXPORT_NAME }}};44#elif !MINIMAL_RUNTIME45if (typeof exports === 'object' && typeof module === 'object') {46module.exports = {{{ EXPORT_NAME }}};47// This default export looks redundant, but it allows TS to import this48// commonjs style module.49module.exports.default = {{{ EXPORT_NAME }}};50} else if (typeof define === 'function' && define['amd'])51define([], () => {{{ EXPORT_NAME }}});52#endif5354#if PTHREADS5556// Create code for detecting if we are running in a pthread.57// Normally this detection is done when the module is itself run but58// when running in MODULARIZE mode we need use this to know if we should59// run the module constructor on startup (true only for pthreads).60#if ENVIRONMENT_MAY_BE_WEB || ENVIRONMENT_MAY_BE_WORKER61var isPthread = globalThis.self?.name?.startsWith('em-pthread');62#if ENVIRONMENT_MAY_BE_NODE63// In order to support both web and node we also need to detect node here.64var isNode = {{{ nodeDetectionCode() }}};65if (isNode) isPthread = {{{ nodePthreadDetection() }}}66#endif67#else ENVIRONMENT_MAY_BE_NODE68var isPthread = {{{ nodePthreadDetection() }}}69// When running as a pthread, construct a new instance on startup70#endif7172#if MODULARIZE == 'instance'73isPthread && init();74#else75isPthread && {{{ EXPORT_NAME }}}();76#endif7778#endif // PTHREADS7980#if WASM_WORKERS8182// Same as above for for WASM_WORKERS83// Normally this detection is done when the module is itself run but84// when running in MODULARIZE mode we need use this to know if we should85// run the module constructor on startup (true only for pthreads).86#if ENVIRONMENT_MAY_BE_WEB || ENVIRONMENT_MAY_BE_WORKER87var isWW = globalThis.self?.name == 'em-ww';88// In order to support both web and node we also need to detect node here.89#if ENVIRONMENT_MAY_BE_NODE90#if !PTHREADS91var isNode = {{{ nodeDetectionCode() }}};92#endif93if (isNode) isWW = {{{ nodeWWDetection() }}};94#endif95#elif ENVIRONMENT_MAY_BE_NODE96var isWW = {{{ nodeWWDetection() }}};97#endif9899#if AUDIO_WORKLET100isWW ||= typeof AudioWorkletGlobalScope !== 'undefined';101// When running as a wasm worker, construct a new instance on startup102#endif103104#if MODULARIZE == 'instance'105isWW && init();106#else107isWW && {{{ EXPORT_NAME }}}();108#endif109110#endif // WASM_WORKERS111112113