From 7c3fde0837281962f052004bb6ed7eb64739d9b7 Mon Sep 17 00:00:00 2001 From: Andrew Hayzen Date: Fri, 15 Nov 2024 15:39:07 +0000 Subject: [PATCH 1/2] trycatch: do not try catch when no exceptions is enabled For WASM targets the `cc` crate changed to always set `-fno-exceptions`, before only `-fignore-exceptions` was being set elsewhere, so now programs fail to build when using `cxx` if any `throw` or `try ... catch` are used. > error: cannot use 'try' with exceptions disabled Instead for trycatch just call `func()`. --- book/src/binding/result.md | 12 +++++++++--- gen/src/builtin.rs | 12 +++++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/book/src/binding/result.md b/book/src/binding/result.md index 733212a63..856d49ae7 100644 --- a/book/src/binding/result.md +++ b/book/src/binding/result.md @@ -137,10 +137,16 @@ you'd like for the Rust error to have. ...namespace behavior { ... template -static void trycatch(Try &&func, Fail &&fail) noexcept try { +static void trycatch(Try &&func, Fail &&fail) noexcept { +#if defined(RUST_CXX_NO_EXCEPTIONS) func(); -} catch (const std::exception &e) { - fail(e.what()); +#else + try { + func(); + } catch (const std::exception &e) { + fail(e.what()); + } +#endif } ... ...} // namespace behavior diff --git a/gen/src/builtin.rs b/gen/src/builtin.rs index d38473afc..37c33709b 100644 --- a/gen/src/builtin.rs +++ b/gen/src/builtin.rs @@ -410,10 +410,16 @@ pub(super) fn write(out: &mut OutFile) { " ::std::is_same(), ::std::declval())),", ); writeln!(out, " missing>::value>::type"); - writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{"); + writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept {{"); + writeln!(out, "#if defined(RUST_CXX_NO_EXCEPTIONS)"); writeln!(out, " func();"); - writeln!(out, "}} catch (::std::exception const &e) {{"); - writeln!(out, " fail(e.what());"); + writeln!(out, "#else"); + writeln!(out, " try {{"); + writeln!(out, " func();"); + writeln!(out, " }} catch (::std::exception const &e) {{"); + writeln!(out, " fail(e.what());"); + writeln!(out, " }}"); + writeln!(out, "#endif"); writeln!(out, "}}"); out.end_block(Block::Namespace("behavior")); } From 7a0f3f2e3c8dc44f9c74b2f6e6165354239b63a8 Mon Sep 17 00:00:00 2001 From: Andrew Hayzen Date: Fri, 15 Nov 2024 15:54:45 +0000 Subject: [PATCH 2/2] result: without exception do not throw when Result Err just abort For WASM targets the `cc` crate changed to always set `-fno-exceptions`, before only `-fignore-exceptions` was being set elsewhere, so now programs fail to build when using `cxx` if any `throw` or `try ... catch` are used. > error: cannot use 'throw' with exceptions disabled Instead of throwing just abort. --- gen/src/write.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/gen/src/write.rs b/gen/src/write.rs index 77e1da0b2..7d3d5380d 100644 --- a/gen/src/write.rs +++ b/gen/src/write.rs @@ -1123,7 +1123,19 @@ fn write_rust_function_shim_impl( if sig.throws { out.builtin.rust_error = true; writeln!(out, " if (error$.ptr) {{"); + writeln!(out, "#if defined(RUST_CXX_NO_EXCEPTIONS)"); + writeln!( + out, + " const auto msg = static_cast(error$.ptr);" + ); + writeln!( + out, + " std::fprintf(stderr, \"Error: %s Aborting.\\n\", msg);" + ); + writeln!(out, " std::abort();"); + writeln!(out, "#else"); writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);"); + writeln!(out, "#endif"); writeln!(out, " }}"); } if indirect_return {