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

Clean up templates #29

Merged
merged 3 commits into from
Mar 5, 2024
Merged
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
130 changes: 9 additions & 121 deletions bindgen/src/bindings/cpp/templates/cpp_scaffolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,55 +50,7 @@ struct RustCallStatus {

typedef int ForeignCallback(uint64_t handle, uint32_t method, uint8_t *args_data, int32_t args_len, RustBuffer *buf_ptr);

struct RustStreamBuffer: std::basic_streambuf<uint8_t> {
RustStreamBuffer(RustBuffer *buf) {
this->setg(buf->data, buf->data, buf->data + buf->len);
this->setp(buf->data, buf->data + buf->capacity);
}
~RustStreamBuffer() = default;

private:
RustStreamBuffer() = delete;
RustStreamBuffer(const RustStreamBuffer &) = delete;
RustStreamBuffer(RustStreamBuffer &&) = delete;

RustStreamBuffer &operator=(const RustStreamBuffer &) = delete;
RustStreamBuffer &operator=(RustStreamBuffer &&) = delete;
};

struct RustStream: std::basic_iostream<uint8_t> {
RustStream(RustBuffer *buf):
streambuf(RustStreamBuffer(buf)), std::basic_iostream<uint8_t>(&streambuf) { }

template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
RustStream &operator>>(T &val) {
read(reinterpret_cast<uint8_t *>(&val), sizeof(T));

if (std::endian::native != std::endian::big) {
auto bytes = reinterpret_cast<char *>(&val);

std::reverse(bytes, bytes + sizeof(T));
}

return *this;
}

template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
RustStream &operator<<(T val) {
if (std::endian::native != std::endian::big) {
auto bytes = reinterpret_cast<char *>(&val);

std::reverse(bytes, bytes + sizeof(T));
}

write(reinterpret_cast<uint8_t *>(&val), sizeof(T));

return *this;
}
private:
RustStreamBuffer streambuf;
};

{%- include "rust_buf_stream.cpp" %}
{%- include "scaffolding/object_map.cpp" %}

{%- for typ in ci.iter_types() %}
Expand Down Expand Up @@ -209,28 +161,9 @@ UNIFFI_EXPORT RustBuffer {{ ci.ffi_rustbuffer_reserve().name() }}(RustBuffer buf

{% for func in ci.function_definitions() %}
{% let ffi_func = func.ffi_func() %}
UNIFFI_EXPORT
{% match ffi_func.return_type() -%}
{% when Some with (return_type) %}{{ return_type|ffi_type_name }} {% when None %}void {% endmatch %}{{ ffi_func.name() }}(
{%- for arg in ffi_func.arguments() %}
{{- arg.type_().borrow()|ffi_type_name }} {{ arg.name() }}{% if !loop.last || ffi_func.has_rust_call_status_arg() %}, {% endif -%}
{% endfor %}
{%- if ffi_func.has_rust_call_status_arg() %}RustCallStatus *out_status{% endif -%}
) {
UNIFFI_EXPORT {%- call macros::fn_definition(ffi_func) %} {
{%- call macros::fn_prologue(ci, func, ffi_func) %}
{%- match func.return_type() %}
{%- when Some with (return_type) %}
auto ret = {{ namespace }}::{{ func.name() }}(
{%- for arg in func.arguments() %}
{{- arg|lift_fn }}({{ arg.name()|var_name }}){% if !loop.last %}, {% endif -%}
{% endfor %});
return {{ return_type|lower_fn }}(ret);
{% when None %}
{{ namespace }}::{{ func.name() }}(
{%- for arg in func.arguments() %}
{{- arg|lift_fn }}({{ arg.name()|var_name }}){% if !loop.last %}, {% endif -%}
{%- endfor %});
{%- endmatch %}
{%- call macros::invoke_native_fn(func, namespace) %}
{%- call macros::fn_epilogue(ci, func, ffi_func) %}
}
{% endfor %}
Expand All @@ -248,14 +181,7 @@ UNIFFI_EXPORT void {{ ffi_func.name() }}(ForeignCallback callback_stub, RustCall

{% for ctor in obj.constructors() %}
{% let ffi_ctor = ctor.ffi_func() %}
UNIFFI_EXPORT
{% match ffi_ctor.return_type() -%}
{% when Some with (return_type) %}{{ return_type|ffi_type_name }} {% when None %}void {% endmatch %}{{ ffi_ctor.name() }}(
{%- for arg in ffi_ctor.arguments() %}
{{- arg.type_().borrow()|ffi_type_name }} {{ arg.name() }}{% if !loop.last || ffi_ctor.has_rust_call_status_arg() %}, {% endif -%}
{% endfor %}
{%- if ffi_ctor.has_rust_call_status_arg() %}RustCallStatus *out_status{% endif -%}
) {
UNIFFI_EXPORT {%- call macros::fn_definition(ffi_ctor) %} {
{%- if ffi_ctor.has_rust_call_status_arg() %}
out_status->code = UNIFFI_CALL_STATUS_OK;
{% endif -%}
Expand All @@ -269,14 +195,7 @@ UNIFFI_EXPORT
{% endfor %}

{% let ffi_dtor = obj.ffi_object_free() %}
UNIFFI_EXPORT
{% match ffi_dtor.return_type() -%}
{% when Some with (return_type) %}{{ return_type|ffi_type_name }} {% when None %}void {% endmatch %}{{ ffi_dtor.name() }}(
{%- for arg in ffi_dtor.arguments() %}
{{- arg.type_().borrow()|ffi_type_name }} {{ arg.name() }}{% if !loop.last || ffi_dtor.has_rust_call_status_arg() %}, {% endif -%}
{% endfor %}
{%- if ffi_dtor.has_rust_call_status_arg() %}RustCallStatus *out_status{% endif -%}
) {
UNIFFI_EXPORT {%- call macros::fn_definition(ffi_dtor) %} {
{%- if ffi_dtor.has_rust_call_status_arg() %}
out_status->code = UNIFFI_CALL_STATUS_OK;
{% endif -%}
Expand All @@ -286,48 +205,17 @@ UNIFFI_EXPORT

{% for func in obj.methods() %}
{% let ffi_func = func.ffi_func() %}
UNIFFI_EXPORT
{% match ffi_func.return_type() -%}
{% when Some with (return_type) %}{{ return_type|ffi_type_name }} {% when None %}void {% endmatch %}{{ ffi_func.name() }}(
{%- for arg in ffi_func.arguments() %}
{{- arg.type_().borrow()|ffi_type_name }} {{ arg.name() }}{% if !loop.last || ffi_func.has_rust_call_status_arg() %}, {% endif -%}
{% endfor %}
{%- if ffi_func.has_rust_call_status_arg() %}RustCallStatus *out_status{% endif -%}
) {
{%- if ffi_func.has_rust_call_status_arg() %}
out_status->code = UNIFFI_CALL_STATUS_OK;
{% endif -%}

auto obj = {{ obj.name() }}_map.at((uint64_t)ptr);

UNIFFI_EXPORT {%- call macros::fn_definition(ffi_func) %} {
{%- call macros::fn_prologue(ci, func, ffi_func) %}
{% match func.return_type() %}
{% when Some with (return_type) %}
auto ret = obj->{{ func.name() }}({% if func.takes_self_by_arc() %}obj{% if !func.arguments().is_empty() %},{% endif %}{% endif %}
{%- for arg in func.arguments() %}
{{- arg|lift_fn }}({{ arg.name()|var_name }}){% if !loop.last %}, {% endif -%}
{% endfor %});
return {{ return_type|lower_fn }}(ret);
{% when None %}
obj->{{ func.name() }}(
{%- for arg in func.arguments() %}
{{- arg|lift_fn }}({{ arg.name()|var_name }}){% if !loop.last %}, {% endif -%}
{% endfor %});
{% endmatch %}
auto obj = {{ obj.name() }}_map.at((uint64_t)ptr);
{%- call macros::invoke_native_fn_obj(func) %}
{%- call macros::fn_epilogue(ci, func, ffi_func) %}
}
{% endfor %}
{% endfor %}

{% for func in ci.iter_futures_ffi_function_definitons() %}
UNIFFI_EXPORT
{% match func.return_type() -%}
{% when Some with (return_type) %}{{ return_type|ffi_type_name }} {% when None %}void {% endmatch %}{{ func.name() }}(
{%- for arg in func.arguments() %}
{{- arg.type_().borrow()|ffi_type_name }} {{ arg.name() }}{% if !loop.last || func.has_rust_call_status_arg() %}, {% endif -%}
{% endfor %}
{%- if func.has_rust_call_status_arg() %}RustCallStatus *out_status{% endif -%}
) {
UNIFFI_EXPORT {%- call macros::fn_definition(func) %} {
{%- match func.return_type() %}{% when Some with (return_type) %}
{% match return_type %}
{% when FfiType::RustArcPtr(_) %}
Expand Down
49 changes: 49 additions & 0 deletions bindgen/src/bindings/cpp/templates/rust_buf_stream.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
struct RustStreamBuffer: std::basic_streambuf<uint8_t> {
RustStreamBuffer(RustBuffer *buf) {
this->setg(buf->data, buf->data, buf->data + buf->len);
this->setp(buf->data, buf->data + buf->capacity);
}
~RustStreamBuffer() = default;

private:
RustStreamBuffer() = delete;
RustStreamBuffer(const RustStreamBuffer &) = delete;
RustStreamBuffer(RustStreamBuffer &&) = delete;

RustStreamBuffer &operator=(const RustStreamBuffer &) = delete;
RustStreamBuffer &operator=(RustStreamBuffer &&) = delete;
};

struct RustStream: std::basic_iostream<uint8_t> {
RustStream(RustBuffer *buf):
streambuf(RustStreamBuffer(buf)), std::basic_iostream<uint8_t>(&streambuf) { }

template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
RustStream &operator>>(T &val) {
read(reinterpret_cast<uint8_t *>(&val), sizeof(T));

if (std::endian::native != std::endian::big) {
auto bytes = reinterpret_cast<char *>(&val);

std::reverse(bytes, bytes + sizeof(T));
}

return *this;
}

template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
RustStream &operator<<(T val) {
if (std::endian::native != std::endian::big) {
auto bytes = reinterpret_cast<char *>(&val);

std::reverse(bytes, bytes + sizeof(T));
}

write(reinterpret_cast<uint8_t *>(&val), sizeof(T));

return *this;
}
private:
RustStreamBuffer streambuf;
};

43 changes: 43 additions & 0 deletions bindgen/src/bindings/cpp/templates/scaffolding/macros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,46 @@
{% endmatch %}
{%- endif %}
{% endmacro %}

{% macro invoke_native_fn(scaffolding_fn, namespace) %}
{% match func.return_type() %}
{% when Some with (return_type) %}
auto ret = {{ namespace }}::{{ scaffolding_fn.name() }}(
{%- for arg in scaffolding_fn.arguments() %}
{{- arg|lift_fn }}({{ arg.name()|var_name }}){% if !loop.last %}, {% endif -%}
{% endfor %});
return {{ return_type|lower_fn }}(ret);
{% when None %}
{{ namespace }}::{{ scaffolding_fn.name() }}(
{%- for arg in scaffolding_fn.arguments() %}
{{- arg|lift_fn }}({{ arg.name()|var_name }}){% if !loop.last %}, {% endif -%}
{% endfor %});
{% endmatch %}
{% endmacro %}

{% macro invoke_native_fn_obj(scaffolding_fn) %}
{% match func.return_type() %}
{% when Some with (return_type) %}
auto ret = obj->{{ scaffolding_fn.name() }}(
{% if scaffolding_fn.takes_self_by_arc() %}obj{% if !scaffolding_fn.arguments().is_empty() %},{% endif %}{% endif %}
{%- for arg in scaffolding_fn.arguments() %}
{{- arg|lift_fn }}({{ arg.name()|var_name }}){% if !loop.last %}, {% endif -%}
{% endfor %});
return {{ return_type|lower_fn }}(ret);
{% when None %}
obj->{{ scaffolding_fn.name() }}(
{% if scaffolding_fn.takes_self_by_arc() %}obj{% if !scaffolding_fn.arguments().is_empty() %},{% endif %}{% endif %}
{%- for arg in scaffolding_fn.arguments() %}
{{- arg|lift_fn }}({{ arg.name()|var_name }}){% if !loop.last %}, {% endif -%}
{% endfor %});
{% endmatch %}
{% endmacro %}

{% macro fn_definition(ffi_func) %}
{% match ffi_func.return_type() -%}
{% when Some with (return_type) %}{{ return_type|ffi_type_name }} {% when None %}void {% endmatch %}{{ ffi_func.name() }}(
{%- for arg in ffi_func.arguments() %}
{{- arg.type_().borrow()|ffi_type_name }} {{ arg.name() }}{% if !loop.last || ffi_func.has_rust_call_status_arg() %}, {% endif -%}
{% endfor %}
{%- if ffi_func.has_rust_call_status_arg() %}RustCallStatus *out_status{% endif -%})
{% endmacro %}
49 changes: 1 addition & 48 deletions bindgen/src/bindings/cpp/templates/wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,54 +79,7 @@ typedef {{ type_name }} {{ name }};
{%- endfor %}

namespace uniffi {
struct RustStreamBuffer: std::basic_streambuf<uint8_t> {
RustStreamBuffer(RustBuffer *buf) {
this->setg(buf->data, buf->data, buf->data + buf->len);
this->setp(buf->data, buf->data + buf->capacity);
}
~RustStreamBuffer() = default;

private:
RustStreamBuffer() = delete;
RustStreamBuffer(const RustStreamBuffer &) = delete;
RustStreamBuffer(RustStreamBuffer &&) = delete;

RustStreamBuffer &operator=(const RustStreamBuffer &) = delete;
RustStreamBuffer &operator=(RustStreamBuffer &&) = delete;
};

struct RustStream: std::basic_iostream<uint8_t> {
RustStream(RustBuffer *buf):
streambuf(RustStreamBuffer(buf)), std::basic_iostream<uint8_t>(&streambuf) { }

template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
RustStream &operator>>(T &val) {
read(reinterpret_cast<uint8_t *>(&val), sizeof(T));

if (std::endian::native != std::endian::big) {
auto bytes = reinterpret_cast<char *>(&val);

std::reverse(bytes, bytes + sizeof(T));
}

return *this;
}

template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
RustStream &operator<<(T val) {
if (std::endian::native != std::endian::big) {
auto bytes = reinterpret_cast<char *>(&val);

std::reverse(bytes, bytes + sizeof(T));
}

write(reinterpret_cast<uint8_t *>(&val), sizeof(T));

return *this;
}
private:
RustStreamBuffer streambuf;
};
{%- include "rust_buf_stream.cpp" %}

RustBuffer rustbuffer_alloc(int32_t);
RustBuffer rustbuffer_from_bytes(const ForeignBytes &);
Expand Down
4 changes: 2 additions & 2 deletions cpp-tests/scaffolding_tests/coverall/lib_coverall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ std::shared_ptr<coverall::Coveralls> coverall::Coveralls::get_other() {
return this->other;
}

void coverall::Coveralls::take_other_fallible() {
void coverall::Coveralls::take_other_fallible(const std::shared_ptr<Coveralls> &self) {
throw coverall::coverall_error::TooManyHoles();
}

void coverall::Coveralls::take_other_panic(std::string message) {
void coverall::Coveralls::take_other_panic(const std::shared_ptr<Coveralls> &self, std::string message) {
throw std::runtime_error(message);
}

Expand Down
4 changes: 2 additions & 2 deletions cpp-tests/scaffolding_tests/coverall/lib_coverall.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ namespace {

void take_other(const std::shared_ptr<Coveralls> &other);
std::shared_ptr<Coveralls> get_other();
void take_other_fallible();
void take_other_panic(std::string message);
void take_other_fallible(const std::shared_ptr<Coveralls> &self);
void take_other_panic(const std::shared_ptr<Coveralls> &self, std::string message);

std::shared_ptr<Coveralls> clone_me();

Expand Down
4 changes: 2 additions & 2 deletions cpp-tests/scaffolding_tests/todolist/lib_todolist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ void todolist::TodoList::clear_item(const std::string &todo) {
this->items.erase(it);
}

void todolist::TodoList::make_default() {
todolist::set_default_list(this->shared_from_this());
void todolist::TodoList::make_default(const std::shared_ptr<TodoList> &self) {
todolist::set_default_list(self);
}

#include <todolist_cpp_scaffolding.cpp>
4 changes: 2 additions & 2 deletions cpp-tests/scaffolding_tests/todolist/lib_todolist.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ namespace {
std::string text;
};

class TodoList: public std::enable_shared_from_this<TodoList>{
class TodoList {
public:
TodoList() = default;

Expand All @@ -62,7 +62,7 @@ namespace {
std::string get_last();
std::string get_first();
void clear_item(const std::string &item);
void make_default();
void make_default(const std::shared_ptr<TodoList> &self);
private:
std::vector<std::string> items;
std::mutex items_mutex;
Expand Down
Loading