Debian Codegraph @

third_party/rust/uniffi_bindgen/src/bindings/python/templates/NamespaceLibraryTemplate.py

raw
1 # Define some ctypes FFI types that we use in the library
2
3 """
4 Function pointer for a Rust task, which a callback function that takes a opaque pointer
5 """
6 _UNIFFI_RUST_TASK = ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.c_int8)
7
8 def _uniffi_future_callback_t(return_type):
9 """
10 Factory function to create callback function types for async functions
11 """
12 return ctypes.CFUNCTYPE(None, ctypes.c_uint64, return_type, _UniffiRustCallStatus)
13
14 def _uniffi_load_indirect():
15 """
16 This is how we find and load the dynamic library provided by the component.
17 For now we just look it up by name.
18 """
19 if sys.platform == "darwin":
20 libname = "lib{}.dylib"
21 elif sys.platform.startswith("win"):
22 # As of python3.8, ctypes does not seem to search $PATH when loading DLLs.
23 # We could use `os.add_dll_directory` to configure the search path, but
24 # it doesn't feel right to mess with application-wide settings. Let's
25 # assume that the `.dll` is next to the `.py` file and load by full path.
26 libname = os.path.join(
27 os.path.dirname(__file__),
28 "{}.dll",
29 )
30 else:
31 # Anything else must be an ELF platform - Linux, *BSD, Solaris/illumos
32 libname = "lib{}.so"
33
34 libname = libname.format("{{ config.cdylib_name() }}")
35 path = os.path.join(os.path.dirname(__file__), libname)
36 lib = ctypes.cdll.LoadLibrary(path)
37 return lib
38
39 def _uniffi_check_contract_api_version(lib):
40 # Get the bindings contract version from our ComponentInterface
41 bindings_contract_version = {{ ci.uniffi_contract_version() }}
42 # Get the scaffolding contract version by calling the into the dylib
43 scaffolding_contract_version = lib.{{ ci.ffi_uniffi_contract_version().name() }}()
44 if bindings_contract_version != scaffolding_contract_version:
45 raise InternalError("UniFFI contract version mismatch: try cleaning and rebuilding your project")
46
47 def _uniffi_check_api_checksums(lib):
48 {%- for (name, expected_checksum) in ci.iter_checksums() %}
49 if lib.{{ name }}() != {{ expected_checksum }}:
50 raise InternalError("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
51 {%- else %}
52 pass
53 {%- endfor %}
54
55 # A ctypes library to expose the extern-C FFI definitions.
56 # This is an implementation detail which will be called internally by the public API.
57
58 _UniffiLib = _uniffi_load_indirect()
59
60 {%- for def in ci.ffi_definitions() %}
61 {%- match def %}
62 {%- when FfiDefinition::CallbackFunction(callback) %}
63 {{ callback.name()|ffi_callback_name }} = ctypes.CFUNCTYPE(
64 {%- match callback.return_type() %}
65 {%- when Some(return_type) %}{{ return_type|ffi_type_name(ci) }},
66 {%- when None %}None,
67 {%- endmatch %}
68 {%- for arg in callback.arguments() -%}
69 {{ arg.type_().borrow()|ffi_type_name(ci) }},
70 {%- endfor -%}
71 {%- if callback.has_rust_call_status_arg() %}
72 ctypes.POINTER(_UniffiRustCallStatus),
73 {%- endif %}
74 )
75 {%- when FfiDefinition::Struct(ffi_struct) %}
76 class {{ ffi_struct.name()|ffi_struct_name }}(ctypes.Structure):
77 _fields_ = [
78 {%- for field in ffi_struct.fields() %}
79 ("{{ field.name() }}", {{ field.type_().borrow()|ffi_type_name(ci) }}),
80 {%- endfor %}
81 ]
82 {%- when FfiDefinition::Function(func) %}
83 _UniffiLib.{{ func.name() }}.argtypes = (
84 {%- call py::arg_list_ffi_decl(func) -%}
85 )
86 _UniffiLib.{{ func.name() }}.restype = {% match func.return_type() %}{% when Some(type_) %}{{ type_|ffi_type_name(ci) }}{% when None %}None{% endmatch %}
87 {%- endmatch %}
88 {%- endfor %}
89
90 {# Ensure to call the contract verification only after we defined all functions. -#}
91 _uniffi_check_contract_api_version(_UniffiLib)
92 # _uniffi_check_api_checksums(_UniffiLib)