Skip to content

Commit

Permalink
Add UNIFFI prefix to call status variables, include invalid values …
Browse files Browse the repository at this point in the history
…in case of unexpected errors in callbacks

Signed-off-by: Martynas Gurskas <[email protected]>
  • Loading branch information
Lipt0nas committed Feb 29, 2024
1 parent f9e1227 commit a892588
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
28 changes: 14 additions & 14 deletions bindgen/src/bindings/cpp/templates/cpp_scaffolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@

using namespace {{ namespace }};

constexpr int8_t CALL_STATUS_OK = 0;
constexpr int8_t CALL_STATUS_ERROR = 1;
constexpr int8_t CALL_STATUS_PANIC = 2;
constexpr int8_t UNIFFI_CALL_STATUS_OK = 0;
constexpr int8_t UNIFFI_CALL_STATUS_ERROR = 1;
constexpr int8_t UNIFFI_CALL_STATUS_PANIC = 2;

struct ForeignBytes {
int32_t len;
Expand Down Expand Up @@ -164,7 +164,7 @@ extern "C" {
#endif

UNIFFI_EXPORT RustBuffer {{ ci.ffi_rustbuffer_alloc().name() }}(int32_t size, RustCallStatus *out_status) {
out_status->code = CALL_STATUS_OK;
out_status->code = UNIFFI_CALL_STATUS_OK;

RustBuffer buf = {
.capacity = size,
Expand All @@ -176,7 +176,7 @@ UNIFFI_EXPORT RustBuffer {{ ci.ffi_rustbuffer_alloc().name() }}(int32_t size, Ru
}

UNIFFI_EXPORT RustBuffer {{ ci.ffi_rustbuffer_from_bytes().name() }}(ForeignBytes bytes, RustCallStatus *out_status) {
out_status->code = CALL_STATUS_OK;
out_status->code = UNIFFI_CALL_STATUS_OK;

RustBuffer buf = {
.capacity = bytes.len,
Expand All @@ -190,13 +190,13 @@ UNIFFI_EXPORT RustBuffer {{ ci.ffi_rustbuffer_from_bytes().name() }}(ForeignByte
}

UNIFFI_EXPORT void {{ ci.ffi_rustbuffer_free().name() }}(RustBuffer buf, RustCallStatus *out_status) {
out_status->code = CALL_STATUS_OK;
out_status->code = UNIFFI_CALL_STATUS_OK;

delete[] buf.data;
}

UNIFFI_EXPORT RustBuffer {{ ci.ffi_rustbuffer_reserve().name() }}(RustBuffer buffer, int32_t additional, RustCallStatus *out_status) {
out_status->code = CALL_STATUS_OK;
out_status->code = UNIFFI_CALL_STATUS_OK;

RustBuffer buf = {
.capacity = buffer.capacity + additional,
Expand Down Expand Up @@ -238,7 +238,7 @@ UNIFFI_EXPORT
{% for func in ci.callback_interface_definitions() %}
{% let ffi_func = func.ffi_init_callback() %}
UNIFFI_EXPORT void {{ ffi_func.name() }}(ForeignCallback callback_stub, RustCallStatus *out_status) {
out_status->code = CALL_STATUS_OK;
out_status->code = UNIFFI_CALL_STATUS_OK;

{{ func|ffi_converter_name }}::fn_handle.store(reinterpret_cast<uint64_t>(callback_stub));
}
Expand All @@ -257,7 +257,7 @@ UNIFFI_EXPORT
{%- if ffi_ctor.has_rust_call_status_arg() %}RustCallStatus *out_status{% endif -%}
) {
{%- if ffi_ctor.has_rust_call_status_arg() %}
out_status->code = CALL_STATUS_OK;
out_status->code = UNIFFI_CALL_STATUS_OK;
{% endif -%}

std::shared_ptr<{{ obj.name() }}> obj = std::make_shared<{{ obj.name() }}>(
Expand All @@ -278,7 +278,7 @@ UNIFFI_EXPORT
{%- if ffi_dtor.has_rust_call_status_arg() %}RustCallStatus *out_status{% endif -%}
) {
{%- if ffi_dtor.has_rust_call_status_arg() %}
out_status->code = CALL_STATUS_OK;
out_status->code = UNIFFI_CALL_STATUS_OK;
{% endif -%}

{{ obj.name() }}_map.erase((uint64_t)ptr);
Expand All @@ -295,7 +295,7 @@ UNIFFI_EXPORT
{%- if ffi_func.has_rust_call_status_arg() %}RustCallStatus *out_status{% endif -%}
) {
{%- if ffi_func.has_rust_call_status_arg() %}
out_status->code = CALL_STATUS_OK;
out_status->code = UNIFFI_CALL_STATUS_OK;
{% endif -%}

auto obj = {{ obj.name() }}_map.at((uint64_t)ptr);
Expand Down Expand Up @@ -351,7 +351,7 @@ UNIFFI_EXPORT uint32_t {{ contract_fn.name() }}() { return {{ ci.uniffi_contract
#endif

RustBuffer rustbuffer_alloc(int32_t size) {
RustCallStatus status = { CALL_STATUS_OK };
RustCallStatus status = { UNIFFI_CALL_STATUS_OK };

return {{ ci.ffi_rustbuffer_alloc().name() }}(
size,
Expand All @@ -360,7 +360,7 @@ RustBuffer rustbuffer_alloc(int32_t size) {
}

RustBuffer rustbuffer_from_bytes(const ForeignBytes& bytes) {
RustCallStatus status = { CALL_STATUS_OK };
RustCallStatus status = { UNIFFI_CALL_STATUS_OK };

return {{ ci.ffi_rustbuffer_from_bytes().name() }}(
bytes,
Expand All @@ -369,7 +369,7 @@ RustBuffer rustbuffer_from_bytes(const ForeignBytes& bytes) {
}

void rustbuffer_free(RustBuffer& buf) {
RustCallStatus status = { CALL_STATUS_OK };
RustCallStatus status = { UNIFFI_CALL_STATUS_OK };

{{ ci.ffi_rustbuffer_free().name() }}(
buf,
Expand Down
12 changes: 6 additions & 6 deletions bindgen/src/bindings/cpp/templates/scaffolding/callback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class {{ iface.name() }}Proxy: public {{ iface.name() }} {
auto ret = callback_stub(this->handle, {{ loop.index }}, in_buf.data, size, &out_buf);
rustbuffer_free(in_buf);

if (ret == CALL_STATUS_OK) {
if (ret == UNIFFI_CALL_STATUS_OK) {
{% match m.return_type() %}
{% when Some with (return_type) %}
RustStream out_stream(&out_buf);
Expand All @@ -57,7 +57,7 @@ class {{ iface.name() }}Proxy: public {{ iface.name() }} {
rustbuffer_free(out_buf);
{% endmatch %}
}
else if (ret == CALL_STATUS_ERROR) {
else if (ret == UNIFFI_CALL_STATUS_ERROR) {
RustStream out_stream(&out_buf);
int32_t v;
out_stream >> v;
Expand All @@ -77,20 +77,20 @@ class {{ iface.name() }}Proxy: public {{ iface.name() }} {
{%- endfor %}
default:
rustbuffer_free(out_buf);
throw std::runtime_error("Unexpected error variant");
throw std::runtime_error("Unexpected error variant: " + std::to_string(v));
}
{%- endif %}
rustbuffer_free(out_buf);
throw std::runtime_error("Callback returned unexpected error code");
throw std::runtime_error("Callback returned an error");
}
else if (ret == CALL_STATUS_PANIC) {
else if (ret == UNIFFI_CALL_STATUS_PANIC) {
auto result = FfiConverterString::lift(out_buf);

throw std::runtime_error(result);
}
else {
rustbuffer_free(out_buf);
throw std::runtime_error("Unknown error code");
throw std::runtime_error("Unknown error code: " + std::to_string(ret));
}
}
{% endfor %}
Expand Down
8 changes: 4 additions & 4 deletions bindgen/src/bindings/cpp/templates/scaffolding/macros.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% macro fn_prologue(ci, func, ffi_func) -%}
{%- if ffi_func.has_rust_call_status_arg() %}
out_status->code = CALL_STATUS_OK;
out_status->code = UNIFFI_CALL_STATUS_OK;
{%- endif %}
try {
{% endmacro %}
Expand All @@ -13,15 +13,15 @@
{%- for variant in err_enum.variants() %}
{%- let converter_name = err_enum|ffi_converter_name %}
} catch (const {{ err_type|to_lower_snake_case }}::{{ variant.name() }} &e) {
out_status->code = CALL_STATUS_ERROR;
out_status->code = UNIFFI_CALL_STATUS_ERROR;
out_status->error_buf = {{ converter_name }}{{ variant.name() }}::lower(e);
{%- endfor %}
{%- endif %}
} catch (const std::exception &e) {
out_status->code = CALL_STATUS_PANIC;
out_status->code = UNIFFI_CALL_STATUS_PANIC;
out_status->error_buf = {{ Type::String.borrow()|ffi_converter_name }}::lower(e.what());
} catch (...) {
out_status->code = CALL_STATUS_PANIC;
out_status->code = UNIFFI_CALL_STATUS_PANIC;
}
{% match func.return_type() %}
{% when Some with (return_type) %}
Expand Down

0 comments on commit a892588

Please sign in to comment.