Skip to content

Commit ec93a04

Browse files
authored
[embind] Handle -fno-exceptions and try/catch redefinition macros. (#24787)
When some code bases disable exceptions they also redefine try/catch with preprocessor macros. This causes the val coroutine function `unhandled_exception` to fail to compile with `error: use of undeclared identifier 'error'`.
1 parent 7bf2342 commit ec93a04

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

system/include/emscripten/val.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,9 @@ class val::promise_type {
728728
auto initial_suspend() noexcept { return std::suspend_never{}; }
729729
auto final_suspend() noexcept { return std::suspend_never{}; }
730730

731+
// When exceptions are disabled we don't define unhandled_exception and rely
732+
// on the default terminate behavior.
733+
#ifdef __cpp_exceptions
731734
// On an unhandled exception, reject the stored promise instead of throwing
732735
// it asynchronously where it can't be handled.
733736
void unhandled_exception() {
@@ -740,6 +743,7 @@ class val::promise_type {
740743
reject(error);
741744
}
742745
}
746+
#endif
743747

744748
// Reject the stored promise due to rejection deeper in the call chain
745749
void reject_with(val&& error) {

test/test_other.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15779,6 +15779,12 @@ def test_embind_no_duplicate_symbols(self):
1577915779
create_file('b.cpp', '#include <emscripten/bind.h>')
1578015780
self.run_process([EMXX, '-std=c++23', '-lembind', 'a.cpp', 'b.cpp'])
1578115781

15782+
def test_embind_no_exceptions(self):
15783+
# Test disabling exceptions and redefining try/catch with preprocessor
15784+
# macros.
15785+
create_file('a.cpp', '#define try\n#define catch if (0)\n#include <emscripten/bind.h>')
15786+
self.run_process([EMXX, '-fno-exceptions', '-std=c++23', '-lembind', 'a.cpp'])
15787+
1578215788
def test_no_pthread(self):
1578315789
self.do_runf('hello_world.c', cflags=['-pthread', '-no-pthread'])
1578415790
self.assertExists('hello_world.js')

0 commit comments

Comments
 (0)