| 1 |
{# |
| 2 |
// Template to call into rust. Used in several places. |
| 3 |
// Variable names in `arg_list` should match up with arg lists |
| 4 |
// passed to rust via `arg_list_lowered` |
| 5 |
#} |
| 6 |
|
| 7 |
{%- macro to_ffi_call(func) -%} |
| 8 |
{%- if func.takes_self() %} |
| 9 |
callWithPointer { |
| 10 |
{%- call to_raw_ffi_call(func) %} |
| 11 |
} |
| 12 |
{% else %} |
| 13 |
{%- call to_raw_ffi_call(func) %} |
| 14 |
{% endif %} |
| 15 |
{%- endmacro %} |
| 16 |
|
| 17 |
{%- macro to_raw_ffi_call(func) -%} |
| 18 |
{%- match func.throws_type() %} |
| 19 |
{%- when Some(e) %} |
| 20 |
uniffiRustCallWithError({{ e|type_name(ci) }}) |
| 21 |
{%- else %} |
| 22 |
uniffiRustCall() |
| 23 |
{%- endmatch %} { _status -> |
| 24 |
UniffiLib.INSTANCE.{{ func.ffi_func().name() }}( |
| 25 |
{% if func.takes_self() %}it, {% endif -%} |
| 26 |
{% call arg_list_lowered(func) -%} |
| 27 |
_status) |
| 28 |
} |
| 29 |
{%- endmacro -%} |
| 30 |
|
| 31 |
{%- macro func_decl(func_decl, callable, indent) %} |
| 32 |
{%- if self::can_render_callable(callable, ci) %} |
| 33 |
{%- call render_func_decl(func_decl, callable, indent) %} |
| 34 |
{%- else %} |
| 35 |
// Sorry, the callable "{{ callable.name() }}" isn't supported. |
| 36 |
{%- endif %} |
| 37 |
{%- endmacro %} |
| 38 |
|
| 39 |
{%- macro render_func_decl(func_decl, callable, indent) %} |
| 40 |
{%- call docstring(callable, indent) %} |
| 41 |
|
| 42 |
{%- match callable.throws_type() -%} |
| 43 |
{%- when Some(throwable) %} |
| 44 |
@Throws({{ throwable|type_name(ci) }}::class) |
| 45 |
{%- else -%} |
| 46 |
{%- endmatch -%} |
| 47 |
{%- if callable.is_async() %} |
| 48 |
@Suppress("ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE") |
| 49 |
{{ func_decl }} suspend fun {{ callable.name()|fn_name }}( |
| 50 |
{%- call arg_list(callable, !callable.takes_self()) -%} |
| 51 |
){% match callable.return_type() %}{% when Some(return_type) %} : {{ return_type|type_name(ci) }}{% when None %}{%- endmatch %} { |
| 52 |
return {% call call_async(callable) %} |
| 53 |
} |
| 54 |
{%- else -%} |
| 55 |
{{ func_decl }} fun {{ callable.name()|fn_name }}( |
| 56 |
{%- call arg_list(callable, !callable.takes_self()) -%} |
| 57 |
){%- match callable.return_type() -%} |
| 58 |
{%- when Some(return_type) -%} |
| 59 |
: {{ return_type|type_name(ci) }} { |
| 60 |
return {{ return_type|lift_fn }}({% call to_ffi_call(callable) %}) |
| 61 |
} |
| 62 |
{%- when None %} |
| 63 |
= {% call to_ffi_call(callable) %} |
| 64 |
{%- endmatch %} |
| 65 |
{% endif %} |
| 66 |
{% endmacro %} |
| 67 |
|
| 68 |
{%- macro call_async(callable) -%} |
| 69 |
uniffiRustCallAsync( |
| 70 |
{%- if callable.takes_self() %} |
| 71 |
callWithPointer { thisPtr -> |
| 72 |
UniffiLib.INSTANCE.{{ callable.ffi_func().name() }}( |
| 73 |
thisPtr, |
| 74 |
{% call arg_list_lowered(callable) %} |
| 75 |
) |
| 76 |
}, |
| 77 |
{%- else %} |
| 78 |
UniffiLib.INSTANCE.{{ callable.ffi_func().name() }}({% call arg_list_lowered(callable) %}), |
| 79 |
{%- endif %} |
| 80 |
{{ callable|async_poll(ci) }}, |
| 81 |
{{ callable|async_complete(ci) }}, |
| 82 |
{{ callable|async_free(ci) }}, |
| 83 |
// lift function |
| 84 |
{%- match callable.return_type() %} |
| 85 |
{%- when Some(return_type) %} |
| 86 |
{ {{ return_type|lift_fn }}(it) }, |
| 87 |
{%- when None %} |
| 88 |
{ Unit }, |
| 89 |
{% endmatch %} |
| 90 |
// Error FFI converter |
| 91 |
{%- match callable.throws_type() %} |
| 92 |
{%- when Some(e) %} |
| 93 |
{{ e|type_name(ci) }}.ErrorHandler, |
| 94 |
{%- when None %} |
| 95 |
UniffiNullRustCallStatusErrorHandler, |
| 96 |
{%- endmatch %} |
| 97 |
) |
| 98 |
{%- endmacro %} |
| 99 |
|
| 100 |
{%- macro arg_list_lowered(func) %} |
| 101 |
{%- for arg in func.arguments() %} |
| 102 |
{{- arg|lower_fn }}({{ arg.name()|var_name }}), |
| 103 |
{%- endfor %} |
| 104 |
{%- endmacro -%} |
| 105 |
|
| 106 |
{#- |
| 107 |
// Arglist as used in kotlin declarations of methods, functions and constructors. |
| 108 |
// If is_decl, then default values be specified. |
| 109 |
// Note the var_name and type_name filters. |
| 110 |
-#} |
| 111 |
|
| 112 |
{% macro arg_list(func, is_decl) %} |
| 113 |
{%- for arg in func.arguments() -%} |
| 114 |
{{ arg.name()|var_name }}: {{ arg|type_name(ci) }} |
| 115 |
{%- if is_decl %} |
| 116 |
{%- match arg.default_value() %} |
| 117 |
{%- when Some(literal) %} = {{ literal|render_literal(arg, ci) }} |
| 118 |
{%- else %} |
| 119 |
{%- endmatch %} |
| 120 |
{%- endif %} |
| 121 |
{%- if !loop.last %}, {% endif -%} |
| 122 |
{%- endfor %} |
| 123 |
{%- endmacro %} |
| 124 |
|
| 125 |
{#- |
| 126 |
// Arglist as used in the UniffiLib function declarations. |
| 127 |
// Note unfiltered name but ffi_type_name filters. |
| 128 |
-#} |
| 129 |
{%- macro arg_list_ffi_decl(func) %} |
| 130 |
{%- for arg in func.arguments() %} |
| 131 |
{{- arg.name()|var_name }}: {{ arg.type_().borrow()|ffi_type_name_by_value(ci) -}}, |
| 132 |
{%- endfor %} |
| 133 |
{%- if func.has_rust_call_status_arg() %}uniffi_out_err: UniffiRustCallStatus, {% endif %} |
| 134 |
{%- endmacro -%} |
| 135 |
|
| 136 |
{% macro field_name(field, field_num) %} |
| 137 |
{%- if field.name().is_empty() -%} |
| 138 |
v{{- field_num -}} |
| 139 |
{%- else -%} |
| 140 |
{{ field.name()|var_name }} |
| 141 |
{%- endif -%} |
| 142 |
{%- endmacro %} |
| 143 |
|
| 144 |
{% macro field_name_unquoted(field, field_num) %} |
| 145 |
{%- if field.name().is_empty() -%} |
| 146 |
v{{- field_num -}} |
| 147 |
{%- else -%} |
| 148 |
{{ field.name()|var_name|unquote }} |
| 149 |
{%- endif -%} |
| 150 |
{%- endmacro %} |
| 151 |
|
| 152 |
// Macro for destroying fields |
| 153 |
{%- macro destroy_fields(member) %} |
| 154 |
Disposable.destroy( |
| 155 |
{%- for field in member.fields() %} |
| 156 |
this.{%- call field_name(field, loop.index) -%}{% if loop.last %}{% else %},{% endif -%} |
| 157 |
{%- endfor %} |
| 158 |
) |
| 159 |
{%- endmacro -%} |
| 160 |
|
| 161 |
{%- macro docstring_value(maybe_docstring, indent_spaces) %} |
| 162 |
{%- match maybe_docstring %} |
| 163 |
{%- when Some(docstring) %} |
| 164 |
{{ docstring|docstring(indent_spaces) }} |
| 165 |
{%- else %} |
| 166 |
{%- endmatch %} |
| 167 |
{%- endmacro %} |
| 168 |
|
| 169 |
{%- macro docstring(defn, indent_spaces) %} |
| 170 |
{%- call docstring_value(defn.docstring(), indent_spaces) %} |
| 171 |
{%- endmacro %} |