Skip to content

Commit 5b489fc

Browse files
authored
Make exported __cpp_exception into a global. NFC (#24282)
This still is not quite a generic as I would like so as a followup we can probably make this work for all tags, and perhaps unify the updating of the globals when the exports are available.
1 parent 6702908 commit 5b489fc

File tree

5 files changed

+30
-13
lines changed

5 files changed

+30
-13
lines changed

src/lib/libcore.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2202,7 +2202,7 @@ addToLibrary({
22022202
__asyncify_state: "new WebAssembly.Global({'value': 'i32', 'mutable': true}, 0)",
22032203
__asyncify_data: "new WebAssembly.Global({'value': '{{{ POINTER_WASM_TYPE }}}', 'mutable': true}, {{{ to64(0) }}})",
22042204
#endif
2205-
#endif
2205+
#endif // RELOCATABLE
22062206

22072207
_emscripten_fs_load_embedded_files__deps: ['$FS', '$PATH'],
22082208
_emscripten_fs_load_embedded_files: (ptr) => {

src/lib/libexceptions.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -283,16 +283,11 @@ var LibraryExceptions = {
283283
},
284284
#endif
285285
#if WASM_EXCEPTIONS
286-
$getCppExceptionTag: () =>
287-
// In static linking, tags are defined within the wasm module and are
288-
// exported, whereas in dynamic linking, tags are defined in library.js in
289-
// JS code and wasm modules import them.
290-
#if RELOCATABLE
291-
___cpp_exception // defined in library.js
292-
#else
293-
wasmExports['__cpp_exception']
294-
#endif
295-
,
286+
$getCppExceptionTag__deps: ['__cpp_exception'],
287+
// In static linking, tags are defined within the wasm module and are
288+
// exported, whereas in dynamic linking, tags are defined in libcore.js in
289+
// JS code and wasm modules import them.
290+
$getCppExceptionTag: () => ___cpp_exception,
296291

297292
#if EXCEPTION_STACK_TRACES
298293
// Throw a WebAssembly.Exception object with the C++ tag with a stack trace

src/preamble.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,11 @@ function getWasmImports() {
924924
#endif
925925
#endif
926926

927+
#if hasExportedSymbol('__cpp_exception') && !RELOCATABLE
928+
___cpp_exception = wasmExports['__cpp_exception'];
929+
{{{ receivedSymbol('___cpp_exception') }}};
930+
#endif
931+
927932
#if hasExportedSymbol('__wasm_apply_data_relocs')
928933
__RELOC_FUNCS__.push(wasmExports['__wasm_apply_data_relocs']);
929934
#endif

tools/emscripten.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,10 @@ def trim_asm_const_body(body):
280280
return body
281281

282282

283+
def create_other_export_declarations(tag_exports):
284+
return '\n'.join(f'var {asmjs_mangle(name)};' for name in tag_exports)
285+
286+
283287
def create_global_exports(global_exports):
284288
lines = []
285289
for k, v in global_exports.items():
@@ -417,11 +421,13 @@ def emscript(in_wasm, out_wasm, outfile_js, js_syms, finalize=True, base_metadat
417421

418422
if base_metadata:
419423
function_exports = base_metadata.function_exports
424+
tag_exports = base_metadata.tag_exports
420425
# We want the real values from the final metadata but we only want to
421426
# include names from the base_metadata. See phase_link() in link.py.
422427
global_exports = {k: v for k, v in metadata.global_exports.items() if k in base_metadata.global_exports}
423428
else:
424429
function_exports = metadata.function_exports
430+
tag_exports = metadata.tag_exports
425431
global_exports = metadata.global_exports
426432

427433
if settings.ASYNCIFY == 1:
@@ -431,7 +437,7 @@ def emscript(in_wasm, out_wasm, outfile_js, js_syms, finalize=True, base_metadat
431437
function_exports['asyncify_stop_rewind'] = webassembly.FuncType([], [])
432438

433439
parts = [pre]
434-
parts += create_module(metadata, function_exports, global_exports, forwarded_json['librarySymbols'])
440+
parts += create_module(metadata, function_exports, global_exports, tag_exports, forwarded_json['librarySymbols'])
435441
parts.append(post)
436442

437443
full_js_module = ''.join(parts)
@@ -1000,7 +1006,7 @@ def create_receiving(function_exports):
10001006
return '\n'.join(receiving) + '\n'
10011007

10021008

1003-
def create_module(metadata, function_exports, global_exports, library_symbols):
1009+
def create_module(metadata, function_exports, global_exports, tag_exports,library_symbols):
10041010
module = []
10051011

10061012
receiving = create_receiving(function_exports)
@@ -1010,6 +1016,7 @@ def create_module(metadata, function_exports, global_exports, library_symbols):
10101016
module.append(sending)
10111017
else:
10121018
receiving += create_global_exports(global_exports)
1019+
receiving += create_other_export_declarations(tag_exports)
10131020

10141021
if settings.PTHREADS or settings.WASM_WORKERS:
10151022
sending = textwrap.indent(sending, ' ').strip()

tools/extract_metadata.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,14 @@ def get_function_exports(module):
256256
return rtn
257257

258258

259+
def get_tag_exports(module):
260+
rtn = []
261+
for e in module.get_exports():
262+
if e.kind == webassembly.ExternType.TAG:
263+
rtn.append(e.name)
264+
return rtn
265+
266+
259267
def update_metadata(filename, metadata):
260268
imports = []
261269
invoke_funcs = []
@@ -270,6 +278,7 @@ def update_metadata(filename, metadata):
270278
imports.append(i.field)
271279

272280
metadata.function_exports = get_function_exports(module)
281+
metadata.tag_exports = get_tag_exports(module)
273282
metadata.all_exports = [utils.removeprefix(e.name, '__em_js__') for e in module.get_exports()]
274283

275284
metadata.imports = imports
@@ -340,6 +349,7 @@ def extract_metadata(filename):
340349
metadata = Metadata()
341350
metadata.imports = import_names
342351
metadata.function_exports = get_function_exports(module)
352+
metadata.tag_exports = get_tag_exports(module)
343353
metadata.all_exports = [utils.removeprefix(e.name, '__em_js__') for e in exports]
344354
metadata.em_asm_consts = get_section_strings(module, export_map, 'em_asm')
345355
metadata.js_deps = [d for d in get_section_strings(module, export_map, 'em_lib_deps').values() if d]

0 commit comments

Comments
 (0)