@@ -2385,6 +2385,16 @@ def modularize():
23852385 if settings .EXPORT_ES6 and settings .ENVIRONMENT_MAY_BE_NODE :
23862386 async_emit = 'async '
23872387
2388+ # TODO(sbc): Do we really need _scriptName in all cases. Look into reducing this
2389+ # condition.
2390+ add_script_name_wrapper = not settings .MINIMAL_RUNTIME or settings .SHARED_MEMORY
2391+ add_assertion_wrapper = settings .ASSERTIONS and not settings .MODULARIZE == 'instance'
2392+
2393+ if add_script_name_wrapper or add_assertion_wrapper :
2394+ wrapper_name = settings .EXPORT_NAME + '_inner'
2395+ else :
2396+ wrapper_name = settings .EXPORT_NAME
2397+
23882398 if settings .MODULARIZE == 'instance' :
23892399 wrapper_function = '''
23902400export default async function init(moduleArg = {}) {
@@ -2398,8 +2408,8 @@ def modularize():
23982408 'generated_js' : generated_js
23992409 }
24002410 else :
2401- wrapper_function = '''
2402- %(maybe_async)sfunction(moduleArg = {}) {
2411+ wrapper_function = '''\
2412+ %(maybe_async)sfunction %(wrapper_name)s (moduleArg = {}) {
24032413 var moduleRtn;
24042414
24052415%(generated_js)s
@@ -2408,26 +2418,18 @@ def modularize():
24082418}
24092419''' % {
24102420 'maybe_async' : async_emit ,
2411- 'generated_js' : generated_js
2421+ 'generated_js' : generated_js ,
2422+ 'wrapper_name' : wrapper_name
24122423 }
24132424
2414- if settings .MINIMAL_RUNTIME and not settings .PTHREADS :
2415- # Single threaded MINIMAL_RUNTIME programs do not need access to
2416- # document.currentScript, so a simple export declaration is enough.
2417- src = f'/** @nocollapse */ var { settings .EXPORT_NAME } = { wrapper_function } ;'
2418- else :
2425+ if add_script_name_wrapper :
24192426 script_url_node = ''
2420- # When MODULARIZE this JS may be executed later,
2421- # after document.currentScript is gone, so we save it.
2422- # In EXPORT_ES6 + PTHREADS the 'thread' is actually an ES6 module
2423- # webworker running in strict mode, so doesn't have access to 'document'.
2424- # In this case use 'import.meta' instead.
2425- if settings .EXPORT_ES6 and settings .USE_ES6_IMPORT_META :
2426- script_url = 'import.meta.url'
2427- else :
2428- script_url = "typeof document != 'undefined' ? document.currentScript?.src : undefined"
2429- if settings .ENVIRONMENT_MAY_BE_NODE :
2430- script_url_node = "if (typeof __filename != 'undefined') _scriptName = _scriptName || __filename;"
2427+ # When MODULARIZE this JS may be executed later, after
2428+ # document.currentScript is gone, so we save it. This is only
2429+ # needed if the program actually requires `_scriptName`
2430+ script_url = "typeof document != 'undefined' ? document.currentScript?.src : undefined"
2431+ if settings .ENVIRONMENT_MAY_BE_NODE :
2432+ script_url_node = "if (typeof __filename != 'undefined') _scriptName = _scriptName || __filename;"
24312433 if settings .MODULARIZE == 'instance' :
24322434 src = '''\
24332435 var _scriptName = %(script_url)s;
@@ -2443,14 +2445,33 @@ def modularize():
24432445 var %(EXPORT_NAME)s = (() => {
24442446 var _scriptName = %(script_url)s;
24452447 %(script_url_node)s
2446- return ( %(wrapper_function)s);
2448+ return %(wrapper_function)s
24472449})();
24482450''' % {
24492451 'EXPORT_NAME' : settings .EXPORT_NAME ,
24502452 'script_url' : script_url ,
24512453 'script_url_node' : script_url_node ,
24522454 'wrapper_function' : wrapper_function ,
24532455 }
2456+ elif add_assertion_wrapper :
2457+ src = '''\
2458+ var %(EXPORT_NAME)s = (() => {
2459+ %(wrapper_function)s
2460+ // Return a small, never-async wrapper around %(wrapper_name)s which
2461+ // checks for callers using `new`.
2462+ return function(arg) {
2463+ if (new.target) throw new Error("%(EXPORT_NAME)s() must not be called with new");
2464+ return %(wrapper_name)s(arg);
2465+ }
2466+ })();
2467+ ''' % {
2468+ 'EXPORT_NAME' : settings .EXPORT_NAME ,
2469+ 'wrapper_function' : wrapper_function ,
2470+ 'wrapper_name' : wrapper_name ,
2471+ }
2472+ else :
2473+ # No wrapper required. A simple export declaration is enough.
2474+ src = f'/** @nocollapse */ { wrapper_function } ;'
24542475
24552476 # Given the async nature of how the Module function and Module object
24562477 # come into existence in AudioWorkletGlobalScope, store the Module
0 commit comments