Skip to content

Commit

Permalink
Clean up templates
Browse files Browse the repository at this point in the history
Signed-off-by: Martynas Gurskas <[email protected]>
  • Loading branch information
Lipt0nas committed Dec 6, 2023
1 parent 760c0a5 commit a3cb297
Show file tree
Hide file tree
Showing 43 changed files with 930 additions and 1,015 deletions.
8 changes: 8 additions & 0 deletions bindgen/src/bindings/cpp/gen_cpp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ impl BindingsConfig for Config {
fn update_from_dependency_configs(&mut self, _config_map: HashMap<&str, &Self>) {}
}

#[derive(Template)]
#[template(syntax = "cpp", escape = "none", path = "internal_types.cpp")]
struct InternalTypeRenderer<'a> {
ci: &'a ComponentInterface,
}

#[derive(Template)]
#[template(syntax = "cpp", escape = "none", path = "types.cpp")]
struct TypeRenderer<'a> {
Expand Down Expand Up @@ -198,6 +204,7 @@ impl<'a> CppWrapperHeader<'a> {
struct CppWrapper<'a> {
ci: &'a ComponentInterface,
config: &'a Config,
internal_type_helper_code: String,
type_helper_code: String,
}

Expand All @@ -206,6 +213,7 @@ impl<'a> CppWrapper<'a> {
Self {
ci,
config,
internal_type_helper_code: InternalTypeRenderer { ci }.render().unwrap(),
type_helper_code: TypeRenderer { ci }.render().unwrap(),
}
}
Expand Down
13 changes: 6 additions & 7 deletions bindgen/src/bindings/cpp/templates/arith_conv.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
{{ type_name }} {{ namespace }}::uniffi::{{ ffi_converter_name }}::lift({{ type_name }} val) {
{{ type_name }} {{ ffi_converter_name }}::lift({{ type_name }} val) {
return val;
}

{{ type_name }} {{ namespace }}::uniffi::{{ ffi_converter_name }}::lower({{ type_name }} val) {
{{ type_name }} {{ ffi_converter_name }}::lower({{ type_name }} val) {
return val;
}

{{ type_name }} {{ namespace }}::uniffi::{{ ffi_converter_name }}::read({{ namespace }}::uniffi::RustStream &stream) {
{{ type_name }} {{ ffi_converter_name }}::read(RustStream &stream) {
{{ type_name }} ret;

stream >> ret;

return ret;
}

void {{ namespace }}::uniffi::{{ ffi_converter_name }}::write({{ namespace }}::uniffi::RustStream &stream, {{ type_name }} val) {
void {{ ffi_converter_name }}::write(RustStream &stream, {{ type_name }} val) {
stream << val;
}

int32_t {{ namespace }}::uniffi::{{ ffi_converter_name }}::allocation_size({{ type_name }}) {
int32_t {{ ffi_converter_name }}::allocation_size({{ type_name }}) {
return sizeof({{ type_name }});
}
}
5 changes: 2 additions & 3 deletions bindgen/src/bindings/cpp/templates/arith_conv.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{% let type_name = typ|type_name %}

{%- let type_name = typ|type_name %}
struct {{ typ|ffi_converter_name }} {
static {{ type_name }} lift({{ type_name }});
static {{ type_name }} lower({{ type_name }});
static {{ type_name }} read(RustStream &);
static void write(RustStream &, {{ type_name }});
static int32_t allocation_size({{ type_name }});
};
};
38 changes: 17 additions & 21 deletions bindgen/src/bindings/cpp/templates/bool_conv.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
{%- let class_name = ffi_converter_name|class_name %}
bool {{ class_name }}::lift(uint8_t val) {
return !!val;
}

namespace {{ namespace }} {
bool uniffi::{{ class_name }}::lift(uint8_t val) {
return !!val;
}

uint8_t uniffi::{{ class_name }}::lower(bool val) {
return val;
}

{{ type_name }} uniffi::{{ class_name }}::read(uniffi::RustStream &stream) {
uint8_t val;

stream.get(val);
uint8_t {{ class_name }}::lower(bool val) {
return val;
}

return val;
}
{{ type_name }} {{ class_name }}::read(RustStream &stream) {
uint8_t val;
stream >> val;

void uniffi::{{ class_name }}::write(uniffi::RustStream &stream, bool val) {
stream.put(static_cast<uint8_t>(val));
}
return val;
}

int32_t uniffi::{{ class_name }}::allocation_size(bool) {
return 1;
}
void {{ class_name }}::write(RustStream &stream, bool val) {
stream << val;
}

int32_t {{ class_name }}::allocation_size(bool) {
return 1;
}
7 changes: 3 additions & 4 deletions bindgen/src/bindings/cpp/templates/bool_conv.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{%- let type_name = typ|type_name %}

struct {{ typ|ffi_converter_name }} {
static {{ type_name }} lift(uint8_t);
static uint8_t lower({{ type_name }});
static {{ type_name }} read(::{{ namespace }}::uniffi::RustStream &);
static void write(::{{ namespace }}::uniffi::RustStream &, {{ type_name }});
static {{ type_name }} read(RustStream &);
static void write(RustStream &, {{ type_name }});
static int32_t allocation_size({{ type_name }});
};
};
68 changes: 31 additions & 37 deletions bindgen/src/bindings/cpp/templates/bytes_conv.cpp
Original file line number Diff line number Diff line change
@@ -1,52 +1,46 @@
{%- let ffi_converter_name = typ|ffi_converter_name %}
{%- let class_name = ffi_converter_name|class_name %}
{%- let type_name = typ|type_name %}
{{ type_name }} {{ ffi_converter_name }}::lift(RustBuffer buf) {
auto stream = RustStream(&buf);
auto ret = read(stream);

namespace {{ namespace }} {
{{ type_name }} uniffi::{{ class_name }}::lift(RustBuffer buf) {
auto stream = RustStream(&buf);
auto ret = read(stream);
rustbuffer_free(buf);

rustbuffer_free(buf);

return ret;
}

RustBuffer uniffi::{{ class_name }}::lower(const {{ type_name }} &val) {
auto buf = uniffi::rustbuffer_alloc(allocation_size(val));
auto stream = RustStream(&buf);

write(stream, val);
return ret;
}

return buf;
}
RustBuffer {{ ffi_converter_name }}::lower(const {{ type_name }} &val) {
auto buf = uniffi::rustbuffer_alloc(allocation_size(val));
auto stream = RustStream(&buf);

{{ type_name }} uniffi::{{ class_name }}::read(uniffi::RustStream &stream) {
{{ type_name }} ret;
int32_t count;
write(stream, val);

stream >> count;
return buf;
}

ret.reserve(count);
{{ type_name }} {{ ffi_converter_name }}::read(RustStream &stream) {
{{ type_name }} ret;
int32_t count;
stream >> count;

for (decltype(count) i = 0; i < count; i++) {
uint8_t elem;
stream >> elem;
ret.push_back(elem);
}
ret.reserve(count);

return ret;
for (decltype(count) i = 0; i < count; i++) {
uint8_t elem;
stream >> elem;
ret.push_back(elem);
}

void uniffi::{{ class_name }}::write(uniffi::RustStream &stream, const {{ type_name }} &val) {
stream << static_cast<int32_t>(val.size());
return ret;
}

for (auto &elem : val) {
stream << elem;
}
}
void {{ ffi_converter_name }}::write(RustStream &stream, const {{ type_name }} &val) {
stream << static_cast<int32_t>(val.size());

int32_t uniffi::{{ class_name }}::allocation_size(const {{ type_name }} &val) {
return sizeof(int32_t) + sizeof(uint8_t) * val.size();
for (auto &elem : val) {
stream << elem;
}
}

int32_t {{ ffi_converter_name }}::allocation_size(const {{ type_name }} &val) {
return sizeof(int32_t) + sizeof(uint8_t) * val.size();
}
6 changes: 2 additions & 4 deletions bindgen/src/bindings/cpp/templates/bytes_conv.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
{%- let ffi_converter_name = typ|ffi_converter_name %}
{%- let type_name = typ|type_name %}

struct {{ ffi_converter_name|class_name }} {
static {{ type_name }} lift(RustBuffer);
static RustBuffer lower(const {{ type_name }} &);
static {{ type_name }} read({{ namespace }}::uniffi::RustStream &);
static void write({{ namespace }}::uniffi::RustStream &, const {{ type_name }} &);

static {{ type_name }} read(RustStream &);
static void write(RustStream &, const {{ type_name }} &);
static int32_t allocation_size(const {{ type_name }} &);
};
10 changes: 4 additions & 6 deletions bindgen/src/bindings/cpp/templates/callback.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
{%- let iface = ci|get_callback_interface_definition(name) %}
{%- let type_name = typ|type_name %}
{%- let class_name = type_name|class_name %}
{%- let ffi_converter_name = typ|ffi_converter_name %}

{%- call macros::docstring(iface, 0) %}
{% call macros::docstring(iface, 0) %}
struct {{ class_name }} {
{% for method in iface.methods() -%}
{%- call macros::docstring(method, 4) %}
{%- for method in iface.methods() %}
{% call macros::docstring(method, 4) %}
virtual
{%- match method.return_type() -%}
{%- match method.return_type() %}
{% when Some with (return_type) %} {{ return_type|type_name }} {% else %} void {% endmatch -%}
{{ method.name()|fn_name }}({% call macros::param_list(method) %}) = 0;
{%- endfor %}
Expand Down
6 changes: 2 additions & 4 deletions bindgen/src/bindings/cpp/templates/callback_conv.hpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
{%- let ffi_converter_name = typ|ffi_converter_name %}
{%- let type_name = typ|type_name %}
{%- let class_name = type_name|class_name %}

struct {{ ffi_converter_name|class_name }} {
static std::shared_ptr<{{ class_name }}> lift(uint64_t);
static uint64_t lower(std::shared_ptr<{{ class_name }}>);
static std::shared_ptr<{{ class_name }}> read(uniffi::RustStream &);
static std::shared_ptr<{{ class_name }}> read(RustStream &);
static void write(RustStream &, uint64_t);

static int32_t allocation_size(const {{ class_name }} &);

private:
static int callback_stub(uint64_t, uint32_t, uint8_t *, int32_t, RustBuffer *);

static std::once_flag once;
static HandleMap<{{ class_name }}> callbacks;
};
};
109 changes: 52 additions & 57 deletions bindgen/src/bindings/cpp/templates/callback_iface_tmpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,66 @@
{%- let ffi_converter_name = typ|ffi_converter_name %}
{%- let class_name = ffi_converter_name|class_name %}
{%- let iface = ci|get_callback_interface_definition(name) %}
std::once_flag uniffi::{{ class_name }}::once = std::once_flag();
uniffi::HandleMap<{{ type_name|class_name }}> uniffi::{{ class_name }}::callbacks = {};

namespace {{ namespace }} {
std::once_flag uniffi::{{ class_name }}::once = std::once_flag();
uniffi::HandleMap<{{ type_name|class_name }}> uniffi::{{ class_name }}::callbacks = {};
std::shared_ptr<{{ type_name|class_name }}> uniffi::{{ class_name }}::lift(uint64_t handle) {
std::call_once({{ class_name }}::once, []() {
rust_call({{ iface.ffi_init_callback().name() }}, nullptr, callback_stub);
});

std::shared_ptr<{{ type_name|class_name }}> uniffi::{{ class_name }}::lift(uint64_t handle) {
std::call_once(uniffi::{{ class_name }}::once, []() {
uniffi::rust_call({{ iface.ffi_init_callback().name() }}, nullptr, callback_stub);
});

return callbacks.at(handle);
}
return callbacks.at(handle);
}

uint64_t uniffi::{{ class_name }}::lower(std::shared_ptr<{{ type_name|class_name }}> impl) {
std::call_once(uniffi::{{ class_name }}::once, []() {
uniffi::rust_call({{ iface.ffi_init_callback().name() }}, nullptr, callback_stub);
});
uint64_t uniffi::{{ class_name }}::lower(std::shared_ptr<{{ type_name|class_name }}> impl) {
std::call_once({{ class_name }}::once, []() {
rust_call({{ iface.ffi_init_callback().name() }}, nullptr, callback_stub);
});

return callbacks.insert(impl);
}
return callbacks.insert(impl);
}

int uniffi::{{ class_name }}::callback_stub(uint64_t handle, uint32_t method, uint8_t *args_data, int32_t args_len, RustBuffer *buf_ptr) {
ForeignBytes bytes = { args_len, args_data };
auto buf = uniffi::rustbuffer_from_bytes(bytes);
auto stream = uniffi::RustStream(&buf);
int uniffi::{{ class_name }}::callback_stub(uint64_t handle, uint32_t method, uint8_t *args_data, int32_t args_len, RustBuffer *buf_ptr) {
ForeignBytes bytes = { args_len, args_data };
auto buf = rustbuffer_from_bytes(bytes);
auto stream = RustStream(&buf);

switch (method) {
case 0:
callbacks.erase(handle);
break;
{% for method in iface.methods() -%}
case {{ loop.index }}:
{% if method.throws_type().is_some() %}{{ "try {" }}{% endif %}
switch (method) {
case 0:
callbacks.erase(handle);
break;
{%- for method in iface.methods() %}
case {{ loop.index }}:
{% if method.throws_type().is_some() %}{{ "try {" }}{% endif %}
auto impl = lift(handle);
{% if method.return_type().is_some() %}auto ret = {% endif -%}
{%- if method.return_type().is_some() %}
auto ret = {% endif -%}
impl->{{ method.name() }}(
{%- for arg in method.arguments() %}
{{- arg|read_fn }}(stream){%- if !loop.last %}, {% else %}{% endif %}
{% endfor -%}
);
{%- match method.return_type() %}
{% when Some with (return_type) %}

*buf_ptr = uniffi::rustbuffer_alloc({{ return_type|allocation_size_fn }}(ret));

auto out_stream = uniffi::RustStream(buf_ptr);

{%- for arg in method.arguments() %}
{{- arg|read_fn }}(stream){%- if !loop.last %}, {% else %}{% endif %}
{%- endfor -%}
);
{%- match method.return_type() %}
{% when Some with (return_type) %}
*buf_ptr = rustbuffer_alloc({{ return_type|allocation_size_fn }}(ret));
auto out_stream = RustStream(buf_ptr);
{{ return_type|write_fn }}(out_stream, ret);
{% else %}
{% endmatch %}
{%- match method.throws_type() %}
{% when Some with (e) -%}
} catch (const {{ e|type_name }} &ex) {
*buf_ptr = uniffi::{{ e|ffi_converter_name }}::lower(ex);
return 1;
} catch (std::exception &ex) {
*buf_ptr = {{ Type::String.borrow()|lower_fn }}(ex.what());
return 1;
}
{%- else %}
{%- endmatch %}
break;
{%- endfor %}
{%- else %}
{%- endmatch %}
{%- match method.throws_type() %}
{% when Some with (e) %}
} catch (const {{ e|type_name }} &ex) {
*buf_ptr = {{ e|ffi_converter_name }}::lower(ex);
return 1;
} catch (std::exception &ex) {
*buf_ptr = {{ Type::String.borrow()|lower_fn }}(ex.what());
return 1;
}

return 0;
{% else %}
{%- endmatch %}
break;
{%- endfor %}
}
}

return 0;
}
Loading

0 comments on commit a3cb297

Please sign in to comment.