| 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 super::*; |
| 6 |
use anyhow::bail; |
| 7 |
|
| 8 |
pub fn pass(module: &mut Module) -> Result<()> { |
| 9 |
// UniFFI contract version function. This is used to check that there wasn't a breaking |
| 10 |
// change to `uniffi-bindgen`. |
| 11 |
module.ffi_uniffi_contract_version = RustFfiFunctionName(format!( |
| 12 |
"ffi_{}_uniffi_contract_version", |
| 13 |
&module.crate_name |
| 14 |
)); |
| 15 |
module.ffi_definitions.push( |
| 16 |
FfiFunction { |
| 17 |
name: RustFfiFunctionName(format!( |
| 18 |
"ffi_{}_uniffi_contract_version", |
| 19 |
&module.crate_name |
| 20 |
)), |
| 21 |
async_data: None, |
| 22 |
arguments: vec![], |
| 23 |
return_type: FfiReturnType { |
| 24 |
ty: Some(FfiType::UInt32), |
| 25 |
}, |
| 26 |
has_rust_call_status_arg: false, |
| 27 |
kind: FfiFunctionKind::UniffiContractVersion, |
| 28 |
..FfiFunction::default() |
| 29 |
} |
| 30 |
.into(), |
| 31 |
); |
| 32 |
|
| 33 |
// Checksums, these are used to check that the bindings were built against the same |
| 34 |
// exported interface as the loaded library. |
| 35 |
let mut checksums = vec![]; |
| 36 |
module.try_visit(|callable: &Callable| { |
| 37 |
let Some(checksum) = callable.checksum else { |
| 38 |
if matches!(callable.kind, CallableKind::VTableMethod { .. }) { |
| 39 |
// UDL-based callbacks don't have checksum functions, ignore those rather than |
| 40 |
// returning an error. |
| 41 |
return Ok(()); |
| 42 |
} |
| 43 |
bail!("Checksum not set for {:#?}", callable); |
| 44 |
}; |
| 45 |
let fn_name = match &callable.kind { |
| 46 |
CallableKind::Function => { |
| 47 |
uniffi_meta::fn_checksum_symbol_name(&module.crate_name, &callable.name) |
| 48 |
} |
| 49 |
CallableKind::Method { |
| 50 |
interface_name: name, |
| 51 |
} |
| 52 |
| CallableKind::VTableMethod { trait_name: name } => { |
| 53 |
uniffi_meta::method_checksum_symbol_name(&module.crate_name, name, &callable.name) |
| 54 |
} |
| 55 |
CallableKind::Constructor { interface_name, .. } => { |
| 56 |
uniffi_meta::constructor_checksum_symbol_name( |
| 57 |
&module.crate_name, |
| 58 |
interface_name, |
| 59 |
&callable.name, |
| 60 |
) |
| 61 |
} |
| 62 |
}; |
| 63 |
checksums.push(( |
| 64 |
FfiFunction { |
| 65 |
name: RustFfiFunctionName(fn_name.clone()), |
| 66 |
async_data: None, |
| 67 |
arguments: vec![], |
| 68 |
return_type: FfiReturnType { |
| 69 |
ty: Some(FfiType::UInt16), |
| 70 |
}, |
| 71 |
has_rust_call_status_arg: false, |
| 72 |
kind: FfiFunctionKind::Checksum, |
| 73 |
..FfiFunction::default() |
| 74 |
}, |
| 75 |
checksum, |
| 76 |
)); |
| 77 |
Ok(()) |
| 78 |
})?; |
| 79 |
for (ffi_func, checksum) in checksums { |
| 80 |
module.checksums.push(Checksum { |
| 81 |
fn_name: ffi_func.name.clone(), |
| 82 |
checksum, |
| 83 |
}); |
| 84 |
module |
| 85 |
.ffi_definitions |
| 86 |
.push(FfiDefinition::RustFunction(ffi_func)); |
| 87 |
} |
| 88 |
module.correct_contract_version = uniffi_meta::UNIFFI_CONTRACT_VERSION.to_string(); |
| 89 |
Ok(()) |
| 90 |
} |