Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factor catch implementation to static function #76

Merged
merged 1 commit into from
Mar 18, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions gen/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,13 @@ fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
let mut needs_unsafe_bitcopy = false;
let mut needs_manually_drop = false;
let mut needs_maybe_uninit = false;
let mut needs_trycatch = false;
for api in apis {
match api {
Api::CxxFunction(efn) if !out.header => {
if efn.throws {
needs_trycatch = true;
}
for arg in &efn.args {
if arg.ty == RustString {
needs_unsafe_bitcopy = true;
Expand Down Expand Up @@ -166,6 +170,7 @@ fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
|| needs_unsafe_bitcopy
|| needs_manually_drop
|| needs_maybe_uninit
|| needs_trycatch
{
writeln!(out, "// #include \"rust/cxx.h\"");
}
Expand Down Expand Up @@ -200,6 +205,20 @@ fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
writeln!(out, "}};");
}

if needs_trycatch {
out.next_section();
out.include.exception = true;
writeln!(out, "template <typename Try, typename Fail>");
writeln!(
out,
"static void trycatch(Try &&func, Fail &&fail) noexcept try {{",
);
writeln!(out, " func();");
writeln!(out, "}} catch (const ::std::exception &e) {{");
writeln!(out, " fail(e.what());");
writeln!(out, "}}");
}

out.end_block("namespace cxxbridge02");
out.end_block("namespace rust");
}
Expand Down Expand Up @@ -297,8 +316,9 @@ fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
write!(out, " ");
if efn.throws {
writeln!(out, "::rust::Str::Repr throw$;");
writeln!(out, " try {{");
write!(out, " ");
writeln!(out, " ::rust::trycatch(");
writeln!(out, " [&] {{");
write!(out, " ");
}
if indirect_return {
write!(out, "new (return$) ");
Expand Down Expand Up @@ -349,16 +369,15 @@ fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
writeln!(out, ";");
if efn.throws {
out.include.cstring = true;
out.include.exception = true;
writeln!(out, " throw$.ptr = nullptr;");
writeln!(out, " }} catch (const ::std::exception &catch$) {{");
writeln!(out, " const char *return$ = catch$.what();");
writeln!(out, " throw$.len = ::std::strlen(return$);");
writeln!(out, " throw$.ptr = nullptr;");
writeln!(out, " }},");
writeln!(out, " [&](const char *catch$) {{");
writeln!(out, " throw$.len = ::std::strlen(catch$);");
writeln!(
out,
" throw$.ptr = cxxbridge02$exception(return$, throw$.len);",
" throw$.ptr = cxxbridge02$exception(catch$, throw$.len);",
);
writeln!(out, " }}");
writeln!(out, " }});");
writeln!(out, " return throw$;");
}
writeln!(out, "}}");
Expand Down