| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
use proc_macro2::TokenStream; |
| 6 |
use quote::{format_ident, quote}; |
| 7 |
use syn::Result; |
| 8 |
|
| 9 |
use crate::util::mod_path; |
| 10 |
use uniffi_meta::UNIFFI_CONTRACT_VERSION; |
| 11 |
|
| 12 |
pub fn setup_scaffolding(namespace: String) -> Result<TokenStream> { |
| 13 |
let module_path = mod_path()?; |
| 14 |
let normalized_module_path = module_path.replace("::", "__"); |
| 15 |
let ffi_contract_version_ident = |
| 16 |
format_ident!("ffi_{normalized_module_path}_uniffi_contract_version"); |
| 17 |
let namespace_upper = namespace.to_ascii_uppercase(); |
| 18 |
let namespace_const_ident = format_ident!("UNIFFI_META_CONST_NAMESPACE_{namespace_upper}"); |
| 19 |
let namespace_static_ident = format_ident!("UNIFFI_META_NAMESPACE_{namespace_upper}"); |
| 20 |
let ffi_rustbuffer_alloc_ident = format_ident!("ffi_{normalized_module_path}_rustbuffer_alloc"); |
| 21 |
let ffi_rustbuffer_from_bytes_ident = |
| 22 |
format_ident!("ffi_{normalized_module_path}_rustbuffer_from_bytes"); |
| 23 |
let ffi_rustbuffer_free_ident = format_ident!("ffi_{normalized_module_path}_rustbuffer_free"); |
| 24 |
let ffi_rustbuffer_reserve_ident = |
| 25 |
format_ident!("ffi_{normalized_module_path}_rustbuffer_reserve"); |
| 26 |
let reexport_hack_ident = format_ident!("{normalized_module_path}_uniffi_reexport_hack"); |
| 27 |
let ffi_rust_future_scaffolding_fns = rust_future_scaffolding_fns(&normalized_module_path); |
| 28 |
|
| 29 |
Ok(quote! { |
| 30 |
// Unit struct to parameterize the FfiConverter trait. |
| 31 |
// |
| 32 |
// We use FfiConverter<UniFfiTag> to handle lowering/lifting/serializing types for this crate. See |
| 33 |
// https://mozilla.github.io/uniffi-rs/internals/lifting_and_lowering.html#code-generation-and-the-fficonverter-trait |
| 34 |
// for details. |
| 35 |
// |
| 36 |
// This is pub, since we need to access it to support external types |
| 37 |
#[doc(hidden)] |
| 38 |
pub struct UniFfiTag; |
| 39 |
|
| 40 |
#[allow(clippy::missing_safety_doc, missing_docs)] |
| 41 |
#[doc(hidden)] |
| 42 |
#[unsafe(no_mangle)] |
| 43 |
pub extern "C" fn #ffi_contract_version_ident() -> ::std::primitive::u32 { |
| 44 |
#UNIFFI_CONTRACT_VERSION |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
/// Export namespace metadata. |
| 49 |
/// |
| 50 |
/// See `uniffi_bindgen::macro_metadata` for how this is used. |
| 51 |
|
| 52 |
const #namespace_const_ident: ::uniffi::MetadataBuffer = |
| 53 |
::uniffi::MetadataBuffer::from_code(::uniffi::metadata::codes::NAMESPACE) |
| 54 |
.concat_str(#module_path) |
| 55 |
.concat_str(#namespace); |
| 56 |
|
| 57 |
#[doc(hidden)] |
| 58 |
#[unsafe(no_mangle)] |
| 59 |
pub static #namespace_static_ident: [::std::primitive::u8; #namespace_const_ident.size] = |
| 60 |
#namespace_const_ident.into_array(); |
| 61 |
|
| 62 |
// Everybody gets basic buffer support, since it's needed for passing complex types over the FFI. |
| 63 |
// |
| 64 |
// See `uniffi/src/ffi/rustbuffer.rs` for documentation on these functions |
| 65 |
|
| 66 |
#[allow(clippy::missing_safety_doc, missing_docs)] |
| 67 |
#[doc(hidden)] |
| 68 |
#[unsafe(no_mangle)] |
| 69 |
pub extern "C" fn #ffi_rustbuffer_alloc_ident( |
| 70 |
size: ::std::primitive::u64, |
| 71 |
call_status: &mut ::uniffi::RustCallStatus, |
| 72 |
) -> ::uniffi::RustBuffer { |
| 73 |
::uniffi::ffi::uniffi_rustbuffer_alloc(size, call_status) |
| 74 |
} |
| 75 |
|
| 76 |
#[allow(clippy::missing_safety_doc, missing_docs)] |
| 77 |
#[doc(hidden)] |
| 78 |
#[unsafe(no_mangle)] |
| 79 |
pub unsafe extern "C" fn #ffi_rustbuffer_from_bytes_ident( |
| 80 |
bytes: ::uniffi::ForeignBytes, |
| 81 |
call_status: &mut ::uniffi::RustCallStatus, |
| 82 |
) -> ::uniffi::RustBuffer { |
| 83 |
::uniffi::ffi::uniffi_rustbuffer_from_bytes(bytes, call_status) |
| 84 |
} |
| 85 |
|
| 86 |
#[allow(clippy::missing_safety_doc, missing_docs)] |
| 87 |
#[doc(hidden)] |
| 88 |
#[unsafe(no_mangle)] |
| 89 |
pub unsafe extern "C" fn #ffi_rustbuffer_free_ident( |
| 90 |
buf: ::uniffi::RustBuffer, |
| 91 |
call_status: &mut ::uniffi::RustCallStatus, |
| 92 |
) { |
| 93 |
::uniffi::ffi::uniffi_rustbuffer_free(buf, call_status); |
| 94 |
} |
| 95 |
|
| 96 |
#[allow(clippy::missing_safety_doc, missing_docs)] |
| 97 |
#[doc(hidden)] |
| 98 |
#[unsafe(no_mangle)] |
| 99 |
pub unsafe extern "C" fn #ffi_rustbuffer_reserve_ident( |
| 100 |
buf: ::uniffi::RustBuffer, |
| 101 |
additional: ::std::primitive::u64, |
| 102 |
call_status: &mut ::uniffi::RustCallStatus, |
| 103 |
) -> ::uniffi::RustBuffer { |
| 104 |
::uniffi::ffi::uniffi_rustbuffer_reserve(buf, additional, call_status) |
| 105 |
} |
| 106 |
|
| 107 |
#ffi_rust_future_scaffolding_fns |
| 108 |
|
| 109 |
// Code to re-export the UniFFI scaffolding functions. |
| 110 |
// |
| 111 |
// Rust won't always re-export the functions from dependencies |
| 112 |
// ([rust-lang#50007](https://github.com/rust-lang/rust/issues/50007)) |
| 113 |
// |
| 114 |
// A workaround for this is to have the dependent crate reference a function from its dependency in |
| 115 |
// an extern "C" function. This is clearly hacky and brittle, but at least we have some unittests |
| 116 |
// that check if this works (fixtures/reexport-scaffolding-macro). |
| 117 |
// |
| 118 |
// The main way we use this macro is for that contain multiple UniFFI components (libxul, |
| 119 |
// megazord). The combined library has a cargo dependency for each component and calls |
| 120 |
// uniffi_reexport_scaffolding!() for each one. |
| 121 |
|
| 122 |
#[allow(missing_docs)] |
| 123 |
#[doc(hidden)] |
| 124 |
pub const fn uniffi_reexport_hack() {} |
| 125 |
|
| 126 |
#[doc(hidden)] |
| 127 |
#[macro_export] |
| 128 |
macro_rules! uniffi_reexport_scaffolding { |
| 129 |
() => { |
| 130 |
#[doc(hidden)] |
| 131 |
#[unsafe(no_mangle)] |
| 132 |
pub extern "C" fn #reexport_hack_ident() { |
| 133 |
$crate::uniffi_reexport_hack() |
| 134 |
} |
| 135 |
}; |
| 136 |
} |
| 137 |
}) |
| 138 |
} |
| 139 |
|
| 140 |
/// Generates the rust_future_* functions |
| 141 |
/// |
| 142 |
/// The foreign side uses a type-erased `Handle` to interact with futures, which presents |
| 143 |
/// a problem when creating scaffolding functions. What is the `ReturnType` parameter of `RustFutureFfi`? |
| 144 |
/// |
| 145 |
/// Handle this by using some brute-force monomorphization. For each possible ffi type, we |
| 146 |
/// generate a set of scaffolding functions. The bindings code is responsible for calling the one |
| 147 |
/// corresponds the scaffolding function that created the `Handle`. |
| 148 |
/// |
| 149 |
/// This introduces safety issues, but we do get some type checking. If the bindings code calls |
| 150 |
/// the wrong rust_future_complete function, they should get an unexpected return type, which |
| 151 |
/// hopefully will result in a compile-time error. |
| 152 |
fn rust_future_scaffolding_fns(module_path: &str) -> TokenStream { |
| 153 |
let fn_info = [ |
| 154 |
(quote! { u8 }, "u8"), |
| 155 |
(quote! { i8 }, "i8"), |
| 156 |
(quote! { u16 }, "u16"), |
| 157 |
(quote! { i16 }, "i16"), |
| 158 |
(quote! { u32 }, "u32"), |
| 159 |
(quote! { i32 }, "i32"), |
| 160 |
(quote! { u64 }, "u64"), |
| 161 |
(quote! { i64 }, "i64"), |
| 162 |
(quote! { f32 }, "f32"), |
| 163 |
(quote! { f64 }, "f64"), |
| 164 |
(quote! { *const ::std::ffi::c_void }, "pointer"), |
| 165 |
(quote! { ::uniffi::RustBuffer }, "rust_buffer"), |
| 166 |
(quote! { () }, "void"), |
| 167 |
]; |
| 168 |
fn_info.iter() |
| 169 |
.map(|(return_type, fn_suffix)| { |
| 170 |
let ffi_rust_future_poll = format_ident!("ffi_{module_path}_rust_future_poll_{fn_suffix}"); |
| 171 |
let ffi_rust_future_cancel = format_ident!("ffi_{module_path}_rust_future_cancel_{fn_suffix}"); |
| 172 |
let ffi_rust_future_complete = format_ident!("ffi_{module_path}_rust_future_complete_{fn_suffix}"); |
| 173 |
let ffi_rust_future_free = format_ident!("ffi_{module_path}_rust_future_free_{fn_suffix}"); |
| 174 |
|
| 175 |
quote! { |
| 176 |
#[allow(clippy::missing_safety_doc, missing_docs)] |
| 177 |
#[doc(hidden)] |
| 178 |
#[unsafe(no_mangle)] |
| 179 |
pub unsafe extern "C" fn #ffi_rust_future_poll(handle: ::uniffi::Handle, callback: ::uniffi::RustFutureContinuationCallback, data: u64) { |
| 180 |
::uniffi::ffi::rust_future_poll::<#return_type, crate::UniFfiTag>(handle, callback, data); |
| 181 |
} |
| 182 |
|
| 183 |
#[allow(clippy::missing_safety_doc, missing_docs)] |
| 184 |
#[doc(hidden)] |
| 185 |
#[unsafe(no_mangle)] |
| 186 |
pub unsafe extern "C" fn #ffi_rust_future_cancel(handle: ::uniffi::Handle) { |
| 187 |
::uniffi::ffi::rust_future_cancel::<#return_type, crate::UniFfiTag>(handle) |
| 188 |
} |
| 189 |
|
| 190 |
#[allow(clippy::missing_safety_doc, missing_docs)] |
| 191 |
#[doc(hidden)] |
| 192 |
#[unsafe(no_mangle)] |
| 193 |
pub unsafe extern "C" fn #ffi_rust_future_complete( |
| 194 |
handle: ::uniffi::Handle, |
| 195 |
out_status: &mut ::uniffi::RustCallStatus |
| 196 |
) -> #return_type { |
| 197 |
::uniffi::ffi::rust_future_complete::<#return_type, crate::UniFfiTag>(handle, out_status) |
| 198 |
} |
| 199 |
|
| 200 |
#[allow(clippy::missing_safety_doc, missing_docs)] |
| 201 |
#[doc(hidden)] |
| 202 |
#[unsafe(no_mangle)] |
| 203 |
pub unsafe extern "C" fn #ffi_rust_future_free(handle: ::uniffi::Handle) { |
| 204 |
::uniffi::ffi::rust_future_free::<#return_type, crate::UniFfiTag>(handle) |
| 205 |
} |
| 206 |
} |
| 207 |
}) |
| 208 |
.collect() |
| 209 |
} |