Skip to content

rust error callback #4

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
17 changes: 13 additions & 4 deletions gen/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,11 +352,11 @@ pub(super) fn write(out: &mut OutFile) {
writeln!(out, "template <>");
writeln!(out, "class impl<Error> final {{");
writeln!(out, "public:");
writeln!(out, " static Error error(repr::PtrLen repr) noexcept {{");
writeln!(out, " static void error(const char* msg, size_t len) {{");
writeln!(out, " Error error;");
writeln!(out, " error.msg = static_cast<char const *>(repr.ptr);");
writeln!(out, " error.len = repr.len;");
writeln!(out, " return error;");
writeln!(out, " error.msg = msg;");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the lifetime/ownership of msg? Is it pointing to Rust-owned memory?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added a comment to cxx.h (not sure what's the best place for it): it points to c++ memory and will belong to c++.

https://github.com/cloudflare/workerd-cxx/blob/master/src/result.rs#L60

writeln!(out, " error.len = len;");
writeln!(out, " throw error;");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to throw a value of type Error... but don't we want to convert to a kj::Exception and throw it with kj::throwFatalException() (which adds stack traces, etc.)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we will set our own throw_rust_error callback that will do kj::throw. This function is used only if callback is not overriden

writeln!(out, " }}");
writeln!(out, "}};");
}
Expand Down Expand Up @@ -395,6 +395,15 @@ pub(super) fn write(out: &mut OutFile) {
}

out.end_block(Block::AnonymousNamespace);

if builtin.rust_error {
out.next_section();
writeln!(
out,
"inline void (*throw_rust_error)(const char*, size_t) = impl<Error>::error;"
);
}

out.end_block(Block::InlineNamespace("cxxbridge1"));

if builtin.trycatch {
Expand Down
5 changes: 4 additions & 1 deletion gen/src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,10 @@ fn write_rust_function_shim_impl(
writeln!(out, ";");
out.builtin.rust_error = true;
writeln!(out, " if (error$.ptr) {{");
writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);");
writeln!(
out,
" ::rust::throw_rust_error(static_cast<char const *>(error$.ptr), error$.len);"
);
writeln!(out, " }}");

if indirect_return {
Expand Down
4 changes: 4 additions & 0 deletions include/cxx.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ template <typename T>
class impl;
}

// msg is 0-terminated, size counts the 0.
// msg was new char[] allocated, ownership is transferred to throw_rust_error.
extern void (*throw_rust_error)(const char* msg, size_t size);

#ifndef CXXBRIDGE1_RUST_STRING
#define CXXBRIDGE1_RUST_STRING
// https://cxx.rs/binding/string.html
Expand Down
26 changes: 26 additions & 0 deletions tests/ffi/tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <array>
#include <cstdlib>
#include <cstring>
#include <exception>
#include <iterator>
#include <memory>
#include <numeric>
Expand Down Expand Up @@ -761,6 +762,20 @@ std::unique_ptr<::F::F> c_return_ns_opaque_ptr() {
return f;
}

struct TestException : public std::exception {
TestException(std::string&& msg) : message(std::move(msg)) {}

static void do_throw(const char* msg, size_t len) {
throw TestException(std::string(msg, len));
}

const char* what() const noexcept override {
return message.c_str();
}

std::string message;
};

extern "C" const char *cxx_run_test() noexcept {
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
Expand Down Expand Up @@ -987,6 +1002,17 @@ extern "C" const char *cxx_run_test() noexcept {
ASSERT(std::strcmp(e.what(), "panic in cxx_test_suite::ffi::r_panic: foobar") == 0);
}

// Test custom exception handler
auto prev_handler = ::rust::throw_rust_error;
::rust::throw_rust_error = TestException::do_throw;
try {
r_fail_return_primitive();
ASSERT(false);
} catch (const TestException &e) {
ASSERT(std::strcmp(e.what(), "rust error") == 0);
}
::rust::throw_rust_error = prev_handler;

cxx_test_suite_set_correct();
return nullptr;
}
Expand Down
Loading