Debian Codegraph @

third_party/rust/uniffi_bindgen/src/bindings/kotlin/templates/NamespaceLibraryTemplate.kt

raw
1 @Synchronized
2 private fun findLibraryName(componentName: String): String {
3 val libOverride = System.getProperty("uniffi.component.$componentName.libraryOverride")
4 if (libOverride != null) {
5 return libOverride
6 }
7 return "{{ config.cdylib_name() }}"
8 }
9
10 private inline fun <reified Lib : Library> loadIndirect(
11 componentName: String
12 ): Lib {
13 return Native.load<Lib>(findLibraryName(componentName), Lib::class.java)
14 }
15
16 // Define FFI callback types
17 {%- for def in ci.ffi_definitions() %}
18 {%- match def %}
19 {%- when FfiDefinition::CallbackFunction(callback) %}
20 internal interface {{ callback.name()|ffi_callback_name }} : com.sun.jna.Callback {
21 fun callback(
22 {%- for arg in callback.arguments() -%}
23 {{ arg.name().borrow()|var_name }}: {{ arg.type_().borrow()|ffi_type_name_by_value(ci) }},
24 {%- endfor -%}
25 {%- if callback.has_rust_call_status_arg() -%}
26 uniffiCallStatus: UniffiRustCallStatus,
27 {%- endif -%}
28 )
29 {%- if let Some(return_type) = callback.return_type() %}
30 : {{ return_type|ffi_type_name_by_value(ci) }}
31 {%- endif %}
32 }
33 {%- when FfiDefinition::Struct(ffi_struct) %}
34 @Structure.FieldOrder({% for field in ffi_struct.fields() %}"{{ field.name()|var_name_raw }}"{% if !loop.last %}, {% endif %}{% endfor %})
35 internal open class {{ ffi_struct.name()|ffi_struct_name }}(
36 {%- for field in ffi_struct.fields() %}
37 @JvmField internal var {{ field.name()|var_name }}: {{ field.type_().borrow()|ffi_type_name_for_ffi_struct(ci) }} = {{ field.type_()|ffi_default_value }},
38 {%- endfor %}
39 ) : Structure() {
40 class UniffiByValue(
41 {%- for field in ffi_struct.fields() %}
42 {{ field.name()|var_name }}: {{ field.type_().borrow()|ffi_type_name_for_ffi_struct(ci) }} = {{ field.type_()|ffi_default_value }},
43 {%- endfor %}
44 ): {{ ffi_struct.name()|ffi_struct_name }}({%- for field in ffi_struct.fields() %}{{ field.name()|var_name }}, {%- endfor %}), Structure.ByValue
45
46 internal fun uniffiSetValue(other: {{ ffi_struct.name()|ffi_struct_name }}) {
47 {%- for field in ffi_struct.fields() %}
48 {{ field.name()|var_name }} = other.{{ field.name()|var_name }}
49 {%- endfor %}
50 }
51
52 }
53 {%- when FfiDefinition::Function(_) %}
54 {# functions are handled below #}
55 {%- endmatch %}
56 {%- endfor %}
57
58
59 {%- macro decl_kotlin_functions(func_list) -%}
60 {% for func in func_list -%}
61 fun {{ func.name() }}(
62 {%- call kt::arg_list_ffi_decl(func) %}
63 ): {% match func.return_type() %}{% when Some with (return_type) %}{{ return_type.borrow()|ffi_type_name_by_value(ci) }}{% when None %}Unit{% endmatch %}
64 {% endfor %}
65 {%- endmacro %}
66
67 // For large crates we prevent `MethodTooLargeException` (see #2340)
68 // N.B. the name of the extension is very misleading, since it is
69 // rather `InterfaceTooLargeException`, caused by too many methods
70 // in the interface for large crates.
71 //
72 // By splitting the otherwise huge interface into two parts
73 // * UniffiLib
74 // * IntegrityCheckingUniffiLib (this)
75 // we allow for ~2x as many methods in the UniffiLib interface.
76 //
77 // The `ffi_uniffi_contract_version` method and all checksum methods are put
78 // into `IntegrityCheckingUniffiLib` and these methods are called only once,
79 // when the library is loaded.
80 internal interface IntegrityCheckingUniffiLib : Library {
81 // Integrity check functions only
82 {# newline below wanted #}
83
84 {%- call decl_kotlin_functions(ci.iter_ffi_function_integrity_checks()) %}
85 }
86
87 // A JNA Library to expose the extern-C FFI definitions.
88 // This is an implementation detail which will be called internally by the public API.
89 internal interface UniffiLib : Library {
90 companion object {
91 internal val INSTANCE: UniffiLib by lazy {
92 val componentName = "{{ ci.namespace() }}"
93 // For large crates we prevent `MethodTooLargeException` (see #2340)
94 // N.B. the name of the extension is very misleading, since it is
95 // rather `InterfaceTooLargeException`, caused by too many methods
96 // in the interface for large crates.
97 //
98 // By splitting the otherwise huge interface into two parts
99 // * UniffiLib (this)
100 // * IntegrityCheckingUniffiLib
101 // And all checksum methods are put into `IntegrityCheckingUniffiLib`
102 // we allow for ~2x as many methods in the UniffiLib interface.
103 //
104 // Thus we first load the library with `loadIndirect` as `IntegrityCheckingUniffiLib`
105 // so that we can (optionally!) call `uniffiCheckApiChecksums`...
106 loadIndirect<IntegrityCheckingUniffiLib>(componentName)
107 .also { lib: IntegrityCheckingUniffiLib ->
108 uniffiCheckContractApiVersion(lib)
109 {%- if !config.omit_checksums %}
110 uniffiCheckApiChecksums(lib)
111 {%- endif %}
112 }
113 // ... and then we load the library as `UniffiLib`
114 // N.B. we cannot use `loadIndirect` once and then try to cast it to `UniffiLib`
115 // => results in `java.lang.ClassCastException: com.sun.proxy.$Proxy cannot be cast to ...`
116 // error. So we must call `loadIndirect` twice. For crates large enough
117 // to trigger this issue, the performance impact is negligible, running on
118 // a macOS M1 machine the `loadIndirect` call takes ~50ms.
119 val lib = loadIndirect<UniffiLib>(componentName)
120 // No need to check the contract version and checksums, since
121 // we already did that with `IntegrityCheckingUniffiLib` above.
122 {% for fn_item in self.initialization_fns(ci) -%}
123 {{ fn_item }}
124 {% endfor -%}
125 // Loading of library with integrity check done.
126 lib
127 }
128 {% if ci.contains_object_types() %}
129 // The Cleaner for the whole library
130 internal val CLEANER: UniffiCleaner by lazy {
131 UniffiCleaner.create()
132 }
133 {%- endif %}
134 }
135
136 // FFI functions
137 {# newline below before call decl_kotlin_functions is needed #}
138
139 {%- call decl_kotlin_functions(ci.iter_ffi_function_definitions_excluding_integrity_checks()) %}
140 }
141
142 private fun uniffiCheckContractApiVersion(lib: IntegrityCheckingUniffiLib) {
143 // Get the bindings contract version from our ComponentInterface
144 val bindings_contract_version = {{ ci.uniffi_contract_version() }}
145 // Get the scaffolding contract version by calling the into the dylib
146 val scaffolding_contract_version = lib.{{ ci.ffi_uniffi_contract_version().name() }}()
147 if (bindings_contract_version != scaffolding_contract_version) {
148 throw RuntimeException("UniFFI contract version mismatch: try cleaning and rebuilding your project")
149 }
150 }
151
152 {%- if !config.omit_checksums %}
153 @Suppress("UNUSED_PARAMETER")
154 private fun uniffiCheckApiChecksums(lib: IntegrityCheckingUniffiLib) {
155 {%- for (name, expected_checksum) in ci.iter_checksums() %}
156 if (lib.{{ name }}() != {{ expected_checksum }}.toShort()) {
157 throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
158 }
159 {%- endfor %}
160 }
161 {%- endif %}
162
163 /**
164 * @suppress
165 */
166 public fun uniffiEnsureInitialized() {
167 UniffiLib.INSTANCE
168 }