Skip to content

Commit 77c9eab

Browse files
authored
Allow -fexceptions to work in standalone mode (emscripten-core#19237)
Fixes: emscripten-core#19237
1 parent e3f334c commit 77c9eab

File tree

4 files changed

+13
-12
lines changed

4 files changed

+13
-12
lines changed

embuilder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
'libsockets',
6262
'libstubs',
6363
'libstubs-debug',
64-
'libstandalonewasm',
64+
'libstandalonewasm-nocatch',
6565
'crt1',
6666
'crt1_proxy_main',
6767
'libunwind-except',

system/lib/standalone/standalone.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,19 +157,13 @@ double emscripten_get_now(void) {
157157

158158
// C++ ABI
159159

160-
// Emscripten disables exception catching by default, but not throwing. That
161-
// allows users to see a clear error if a throw happens, and 99% of the
162-
// overhead is in the catching, so this is a reasonable tradeoff.
163-
// For now, in a standalone build just terminate. TODO nice error message
164-
//
165-
// Define these symbols as weak so that when we build with exceptions
166-
// enabled (using wasm-eh) we get the real versions of these functions
167-
// as defined in libc++abi.
168-
169-
__attribute__((__weak__))
160+
#if EMSCRIPTEN_NOCATCH
161+
// When exception catching is disabled, we stub out calls to `__cxa_throw`.
162+
// Otherwise, `__cxa_throw` will be imported from the host.
170163
void __cxa_throw(void* ptr, void* type, void* destructor) {
171164
abort();
172165
}
166+
#endif
173167

174168
// WasmFS integration. We stub out file preloading and such, that are not
175169
// expected to work anyhow.

test/test_other.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10693,6 +10693,7 @@ def test_fignore_exceptions(self):
1069310693
'compile_only': (['-fexceptions'], [], False),
1069410694
# just link isn't enough as codegen didn't emit exceptions support
1069510695
'link_only': ([], ['-fexceptions'], False),
10696+
'standalone': (['-fexceptions'], ['-fexceptions', '-sSTANDALONE_WASM', '-sWASM_BIGINT'], True),
1069610697
})
1069710698
def test_f_exception(self, compile_flags, link_flags, expect_caught):
1069810699
create_file('src.cpp', r'''

tools/system_libs.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1959,10 +1959,13 @@ class libstandalonewasm(MuslInternalLibrary):
19591959

19601960
def __init__(self, **kwargs):
19611961
self.is_mem_grow = kwargs.pop('is_mem_grow')
1962+
self.nocatch = kwargs.pop('nocatch')
19621963
super().__init__(**kwargs)
19631964

19641965
def get_base_name(self):
19651966
name = super().get_base_name()
1967+
if self.nocatch:
1968+
name += '-nocatch'
19661969
if self.is_mem_grow:
19671970
name += '-memgrow'
19681971
return name
@@ -1972,16 +1975,19 @@ def get_cflags(self):
19721975
cflags += ['-DNDEBUG', '-DEMSCRIPTEN_STANDALONE_WASM']
19731976
if self.is_mem_grow:
19741977
cflags += ['-DEMSCRIPTEN_MEMORY_GROWTH']
1978+
if self.nocatch:
1979+
cflags.append('-DEMSCRIPTEN_NOCATCH')
19751980
return cflags
19761981

19771982
@classmethod
19781983
def vary_on(cls):
1979-
return super().vary_on() + ['is_mem_grow']
1984+
return super().vary_on() + ['is_mem_grow', 'nocatch']
19801985

19811986
@classmethod
19821987
def get_default_variation(cls, **kwargs):
19831988
return super().get_default_variation(
19841989
is_mem_grow=settings.ALLOW_MEMORY_GROWTH,
1990+
nocatch=settings.DISABLE_EXCEPTION_CATCHING and not settings.WASM_EXCEPTIONS,
19851991
**kwargs
19861992
)
19871993

0 commit comments

Comments
 (0)