Skip to content

Commit 5fa5862

Browse files
authored
[esm-integration] Refactor emscript/create_module to share more code. NFC (#24281)
NFC, split out from larger change.
1 parent ef5dbde commit 5fa5862

File tree

1 file changed

+38
-32
lines changed

1 file changed

+38
-32
lines changed

tools/emscripten.py

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -429,13 +429,7 @@ def emscript(in_wasm, out_wasm, outfile_js, js_syms, finalize=True, base_metadat
429429
function_exports['asyncify_stop_rewind'] = webassembly.FuncType([], [])
430430

431431
parts = [pre]
432-
receiving = create_receiving(function_exports)
433-
if settings.WASM_ESM_INTEGRATION:
434-
sending = create_sending(metadata, forwarded_json['librarySymbols'])
435-
reexports = create_reexports()
436-
parts += [sending, receiving, reexports]
437-
else:
438-
parts += create_module(receiving, metadata, global_exports, forwarded_json['librarySymbols'])
432+
parts += create_module(metadata, function_exports, global_exports, forwarded_json['librarySymbols'])
439433
parts.append(post)
440434

441435
full_js_module = ''.join(parts)
@@ -1004,44 +998,56 @@ def create_receiving(function_exports):
1004998
return '\n'.join(receiving) + '\n'
1005999

10061000

1007-
def create_module(receiving, metadata, global_exports, library_symbols):
1008-
receiving += create_global_exports(global_exports)
1001+
def create_module(metadata, function_exports, global_exports, library_symbols):
10091002
module = []
10101003

1004+
receiving = create_receiving(function_exports)
10111005
sending = create_sending(metadata, library_symbols)
1012-
if settings.PTHREADS or settings.WASM_WORKERS:
1013-
sending = textwrap.indent(sending, ' ').strip()
1014-
module.append('''\
1015-
var wasmImports;
1016-
function assignWasmImports() {
1017-
wasmImports = %s;
1018-
}
1019-
''' % sending)
1020-
else:
1021-
module.append('var wasmImports = %s;\n' % sending)
10221006

1023-
if not settings.MINIMAL_RUNTIME:
1024-
if settings.MODULARIZE == 'instance':
1025-
module.append("var wasmExports;\n")
1026-
elif settings.WASM_ASYNC_COMPILATION:
1027-
if can_use_await():
1028-
# In modularize mode the generated code is within a factory function.
1029-
# This magic string gets replaced by `await createWasm`. It needed to allow
1030-
# closure and acorn to process the module without seeing this as a top-level
1031-
# await.
1032-
module.append("var wasmExports = EMSCRIPTEN$AWAIT(createWasm());\n")
1033-
else:
1034-
module.append("var wasmExports;\ncreateWasm();\n")
1007+
if settings.WASM_ESM_INTEGRATION:
1008+
module.append(sending)
1009+
else:
1010+
receiving += create_global_exports(global_exports)
1011+
1012+
if settings.PTHREADS or settings.WASM_WORKERS:
1013+
sending = textwrap.indent(sending, ' ').strip()
1014+
module.append('''\
1015+
var wasmImports;
1016+
function assignWasmImports() {
1017+
wasmImports = %s;
1018+
}
1019+
''' % sending)
10351020
else:
1036-
module.append("var wasmExports = createWasm();\n")
1021+
module.append('var wasmImports = %s;\n' % sending)
1022+
1023+
if not settings.MINIMAL_RUNTIME:
1024+
if settings.MODULARIZE == 'instance':
1025+
module.append("var wasmExports;\n")
1026+
elif settings.WASM_ASYNC_COMPILATION:
1027+
if can_use_await():
1028+
# In modularize mode the generated code is within a factory function.
1029+
# This magic string gets replaced by `await createWasm`. It needed to allow
1030+
# closure and acorn to process the module without seeing this as a top-level
1031+
# await.
1032+
module.append("var wasmExports = EMSCRIPTEN$AWAIT(createWasm());\n")
1033+
else:
1034+
module.append("var wasmExports;\ncreateWasm();\n")
1035+
else:
1036+
module.append("var wasmExports = createWasm();\n")
10371037

10381038
module.append(receiving)
1039+
10391040
if settings.SUPPORT_LONGJMP == 'emscripten' or not settings.DISABLE_EXCEPTION_CATCHING:
10401041
module.append(create_invoke_wrappers(metadata))
10411042
else:
10421043
assert not metadata.invoke_funcs, "invoke_ functions exported but exceptions and longjmp are both disabled"
1044+
10431045
if settings.MEMORY64 or settings.CAN_ADDRESS_2GB:
10441046
module.append(create_pointer_conversion_wrappers(metadata))
1047+
1048+
if settings.WASM_ESM_INTEGRATION:
1049+
module.append(create_reexports())
1050+
10451051
return module
10461052

10471053

0 commit comments

Comments
 (0)