@@ -2389,6 +2389,14 @@ 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+ requires_script_name = settings .SHARED_MEMORY and not (settings .EXPORT_ES6 and settings .USE_ES6_IMPORT_META )
2394+
2395+ if requires_script_name or settings .ASSERTIONS :
2396+ wrapper_name = settings .EXPORT_NAME + '_inner' ;
2397+ else :
2398+ wrapper_name = settings .EXPORT_NAME ;
2399+
23922400 if settings .MODULARIZE == 'instance' :
23932401 wrapper_function = '''
23942402export default async function init(moduleArg = {}) {
@@ -2402,8 +2410,8 @@ def modularize():
24022410 'generated_js' : generated_js
24032411 }
24042412 else :
2405- wrapper_function = '''
2406- %(maybe_async)sfunction(moduleArg = {}) {
2413+ wrapper_function = '''\
2414+ %(maybe_async)sfunction %(wrapper_name)s (moduleArg = {}) {
24072415 var moduleRtn;
24082416
24092417%(generated_js)s
@@ -2412,26 +2420,18 @@ def modularize():
24122420}
24132421''' % {
24142422 'maybe_async' : async_emit ,
2415- 'generated_js' : generated_js
2423+ 'generated_js' : generated_js ,
2424+ 'wrapper_name' : wrapper_name
24162425 }
24172426
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 :
2427+ if requires_script_name :
24232428 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;"
2429+ # When MODULARIZE this JS may be executed later, after
2430+ # document.currentScript is gone, so we save it. This is only
2431+ # needed if the program actually requires `_scriptName`
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;"
24352435 if settings .MODULARIZE == 'instance' :
24362436 src = '''\
24372437 var _scriptName = %(script_url)s;
@@ -2447,14 +2447,34 @@ def modularize():
24472447 var %(EXPORT_NAME)s = (() => {
24482448 var _scriptName = %(script_url)s;
24492449 %(script_url_node)s
2450- return ( %(wrapper_function)s);
2450+ return %(wrapper_function)s
24512451})();
24522452''' % {
24532453 'EXPORT_NAME' : settings .EXPORT_NAME ,
24542454 'script_url' : script_url ,
24552455 'script_url_node' : script_url_node ,
24562456 'wrapper_function' : wrapper_function ,
24572457 }
2458+ elif settings .ASSERTIONS and settings .MODULARIZE != 'instance' :
2459+ src = '''\
2460+ var %(EXPORT_NAME)s = (() => {
2461+ %(wrapper_function)s
2462+ // Return a small, never-async wrapper around %(wrapper_name)s which
2463+ // checks for callers using `new`.
2464+ return function(arg) {
2465+ if (new.target) throw new Error("%(EXPORT_NAME)s() must not be called with new");
2466+ return %(wrapper_name)s(arg);
2467+ }
2468+ })();
2469+ ''' % {
2470+ 'EXPORT_NAME' : settings .EXPORT_NAME ,
2471+ 'wrapper_function' : wrapper_function ,
2472+ 'wrapper_name' : wrapper_name ,
2473+ }
2474+ else :
2475+ # Single threaded programs do not need access to document.currentScript,
2476+ # so 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