@@ -2389,6 +2389,15 @@ def modularize():
23892389 if async_emit != '' and settings .EXPORT_NAME == 'config' :
23902390 diagnostics .warning ('emcc' , 'EXPORT_NAME should not be named "config" when targeting Safari' )
23912391
2392+ # Both pthreads and wasm workers rely on _scriptName in the absence of `import.meta`
2393+ add_script_name_wrapper = settings .SHARED_MEMORY and not (settings .EXPORT_ES6 and settings .USE_ES6_IMPORT_META )
2394+ add_assertion_wrapper = settings .ASSERTIONS and not settings .MODULARIZE == 'instance'
2395+
2396+ if add_script_name_wrapper or add_assertion_wrapper :
2397+ wrapper_name = settings .EXPORT_NAME + '_inner' ;
2398+ else :
2399+ wrapper_name = settings .EXPORT_NAME ;
2400+
23922401 if settings .MODULARIZE == 'instance' :
23932402 wrapper_function = '''
23942403export default async function init(moduleArg = {}) {
@@ -2402,8 +2411,8 @@ def modularize():
24022411 'generated_js' : generated_js
24032412 }
24042413 else :
2405- wrapper_function = '''
2406- %(maybe_async)sfunction(moduleArg = {}) {
2414+ wrapper_function = '''\
2415+ %(maybe_async)sfunction %(wrapper_name)s (moduleArg = {}) {
24072416 var moduleRtn;
24082417
24092418%(generated_js)s
@@ -2412,26 +2421,18 @@ def modularize():
24122421}
24132422''' % {
24142423 'maybe_async' : async_emit ,
2415- 'generated_js' : generated_js
2424+ 'generated_js' : generated_js ,
2425+ 'wrapper_name' : wrapper_name
24162426 }
24172427
2418- if settings .MINIMAL_RUNTIME and not settings .PTHREADS :
2419- # Single threaded MINIMAL_RUNTIME programs do not need access to
2420- # document.currentScript, so a simple export declaration is enough.
2421- src = f'/** @nocollapse */ var { settings .EXPORT_NAME } = { wrapper_function } ;'
2422- else :
2428+ if add_script_name_wrapper :
24232429 script_url_node = ''
2424- # When MODULARIZE this JS may be executed later,
2425- # after document.currentScript is gone, so we save it.
2426- # In EXPORT_ES6 + PTHREADS the 'thread' is actually an ES6 module
2427- # webworker running in strict mode, so doesn't have access to 'document'.
2428- # In this case use 'import.meta' instead.
2429- if settings .EXPORT_ES6 and settings .USE_ES6_IMPORT_META :
2430- script_url = 'import.meta.url'
2431- else :
2432- script_url = "typeof document != 'undefined' ? document.currentScript?.src : undefined"
2433- if settings .ENVIRONMENT_MAY_BE_NODE :
2434- script_url_node = "if (typeof __filename != 'undefined') _scriptName = _scriptName || __filename;"
2430+ # When MODULARIZE this JS may be executed later, after
2431+ # document.currentScript is gone, so we save it. This is only
2432+ # needed if the program actually requires `_scriptName`
2433+ script_url = "typeof document != 'undefined' ? document.currentScript?.src : undefined"
2434+ if settings .ENVIRONMENT_MAY_BE_NODE :
2435+ script_url_node = "if (typeof __filename != 'undefined') _scriptName = _scriptName || __filename;"
24352436 if settings .MODULARIZE == 'instance' :
24362437 src = '''\
24372438 var _scriptName = %(script_url)s;
@@ -2447,14 +2448,33 @@ def modularize():
24472448 var %(EXPORT_NAME)s = (() => {
24482449 var _scriptName = %(script_url)s;
24492450 %(script_url_node)s
2450- return ( %(wrapper_function)s);
2451+ return %(wrapper_function)s
24512452})();
24522453''' % {
24532454 'EXPORT_NAME' : settings .EXPORT_NAME ,
24542455 'script_url' : script_url ,
24552456 'script_url_node' : script_url_node ,
24562457 'wrapper_function' : wrapper_function ,
24572458 }
2459+ elif add_assertion_wrapper :
2460+ src = '''\
2461+ var %(EXPORT_NAME)s = (() => {
2462+ %(wrapper_function)s
2463+ // Return a small, never-async wrapper around %(wrapper_name)s which
2464+ // checks for callers using `new`.
2465+ return function(arg) {
2466+ if (new.target) throw new Error("%(EXPORT_NAME)s() must not be called with new");
2467+ return %(wrapper_name)s(arg);
2468+ }
2469+ })();
2470+ ''' % {
2471+ 'EXPORT_NAME' : settings .EXPORT_NAME ,
2472+ 'wrapper_function' : wrapper_function ,
2473+ 'wrapper_name' : wrapper_name ,
2474+ }
2475+ else :
2476+ # No wrapper required. A simple export declaration is enough.
2477+ src = f'/** @nocollapse */ { wrapper_function } ;'
24582478
24592479 # Given the async nature of how the Module function and Module object
24602480 # come into existence in AudioWorkletGlobalScope, store the Module
0 commit comments