summaryrefslogtreecommitdiffstats
path: root/third_party/rust/uniffi_bindgen/src/bindings/kotlin/templates/NamespaceLibraryTemplate.kt
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--third_party/rust/uniffi_bindgen/src/bindings/kotlin/templates/NamespaceLibraryTemplate.kt61
1 files changed, 55 insertions, 6 deletions
diff --git a/third_party/rust/uniffi_bindgen/src/bindings/kotlin/templates/NamespaceLibraryTemplate.kt b/third_party/rust/uniffi_bindgen/src/bindings/kotlin/templates/NamespaceLibraryTemplate.kt
index 6a3aeada35..1bac8a435c 100644
--- a/third_party/rust/uniffi_bindgen/src/bindings/kotlin/templates/NamespaceLibraryTemplate.kt
+++ b/third_party/rust/uniffi_bindgen/src/bindings/kotlin/templates/NamespaceLibraryTemplate.kt
@@ -13,14 +13,57 @@ private inline fun <reified Lib : Library> loadIndirect(
return Native.load<Lib>(findLibraryName(componentName), Lib::class.java)
}
+// Define FFI callback types
+{%- for def in ci.ffi_definitions() %}
+{%- match def %}
+{%- when FfiDefinition::CallbackFunction(callback) %}
+internal interface {{ callback.name()|ffi_callback_name }} : com.sun.jna.Callback {
+ fun callback(
+ {%- for arg in callback.arguments() -%}
+ {{ arg.name().borrow()|var_name }}: {{ arg.type_().borrow()|ffi_type_name_by_value }},
+ {%- endfor -%}
+ {%- if callback.has_rust_call_status_arg() -%}
+ uniffiCallStatus: UniffiRustCallStatus,
+ {%- endif -%}
+ )
+ {%- match callback.return_type() %}
+ {%- when Some(return_type) %}: {{ return_type|ffi_type_name_by_value }}
+ {%- when None %}
+ {%- endmatch %}
+}
+{%- when FfiDefinition::Struct(ffi_struct) %}
+@Structure.FieldOrder({% for field in ffi_struct.fields() %}"{{ field.name()|var_name_raw }}"{% if !loop.last %}, {% endif %}{% endfor %})
+internal open class {{ ffi_struct.name()|ffi_struct_name }}(
+ {%- for field in ffi_struct.fields() %}
+ @JvmField internal var {{ field.name()|var_name }}: {{ field.type_().borrow()|ffi_type_name_for_ffi_struct }} = {{ field.type_()|ffi_default_value }},
+ {%- endfor %}
+) : Structure() {
+ class UniffiByValue(
+ {%- for field in ffi_struct.fields() %}
+ {{ field.name()|var_name }}: {{ field.type_().borrow()|ffi_type_name_for_ffi_struct }} = {{ field.type_()|ffi_default_value }},
+ {%- endfor %}
+ ): {{ ffi_struct.name()|ffi_struct_name }}({%- for field in ffi_struct.fields() %}{{ field.name()|var_name }}, {%- endfor %}), Structure.ByValue
+
+ internal fun uniffiSetValue(other: {{ ffi_struct.name()|ffi_struct_name }}) {
+ {%- for field in ffi_struct.fields() %}
+ {{ field.name()|var_name }} = other.{{ field.name()|var_name }}
+ {%- endfor %}
+ }
+
+}
+{%- when FfiDefinition::Function(_) %}
+{# functions are handled below #}
+{%- endmatch %}
+{%- endfor %}
+
// A JNA Library to expose the extern-C FFI definitions.
// This is an implementation detail which will be called internally by the public API.
-internal interface _UniFFILib : Library {
+internal interface UniffiLib : Library {
companion object {
- internal val INSTANCE: _UniFFILib by lazy {
- loadIndirect<_UniFFILib>(componentName = "{{ ci.namespace() }}")
- .also { lib: _UniFFILib ->
+ internal val INSTANCE: UniffiLib by lazy {
+ loadIndirect<UniffiLib>(componentName = "{{ ci.namespace() }}")
+ .also { lib: UniffiLib ->
uniffiCheckContractApiVersion(lib)
uniffiCheckApiChecksums(lib)
{% for fn in self.initialization_fns() -%}
@@ -28,6 +71,12 @@ internal interface _UniFFILib : Library {
{% endfor -%}
}
}
+ {% if ci.contains_object_types() %}
+ // The Cleaner for the whole library
+ internal val CLEANER: UniffiCleaner by lazy {
+ UniffiCleaner.create()
+ }
+ {%- endif %}
}
{% for func in ci.iter_ffi_function_definitions() -%}
@@ -37,7 +86,7 @@ internal interface _UniFFILib : Library {
{% endfor %}
}
-private fun uniffiCheckContractApiVersion(lib: _UniFFILib) {
+private fun uniffiCheckContractApiVersion(lib: UniffiLib) {
// Get the bindings contract version from our ComponentInterface
val bindings_contract_version = {{ ci.uniffi_contract_version() }}
// Get the scaffolding contract version by calling the into the dylib
@@ -48,7 +97,7 @@ private fun uniffiCheckContractApiVersion(lib: _UniFFILib) {
}
@Suppress("UNUSED_PARAMETER")
-private fun uniffiCheckApiChecksums(lib: _UniFFILib) {
+private fun uniffiCheckApiChecksums(lib: UniffiLib) {
{%- for (name, expected_checksum) in ci.iter_checksums() %}
if (lib.{{ name }}() != {{ expected_checksum }}.toShort()) {
throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project")