| 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 |
//! Add FFI scaffolding function info |
| 6 |
|
| 7 |
use super::*; |
| 8 |
|
| 9 |
pub fn pass(module: &mut Module) -> Result<()> { |
| 10 |
let crate_name = module.crate_name.clone(); |
| 11 |
let module_name = module.name.clone(); |
| 12 |
let mut ffi_definitions = vec![]; |
| 13 |
|
| 14 |
module.visit_mut(|callable: &mut Callable| { |
| 15 |
let name = &callable.name; |
| 16 |
let ffi_func_name = match &callable.kind { |
| 17 |
CallableKind::Function => uniffi_meta::fn_symbol_name(&crate_name, name), |
| 18 |
CallableKind::Method { interface_name } => { |
| 19 |
uniffi_meta::method_symbol_name(&crate_name, interface_name, name) |
| 20 |
} |
| 21 |
CallableKind::Constructor { interface_name, .. } => { |
| 22 |
uniffi_meta::constructor_symbol_name(&crate_name, interface_name, name) |
| 23 |
} |
| 24 |
// VTable methods for callback interfaces don't have FFI functions for them |
| 25 |
CallableKind::VTableMethod { .. } => return, |
| 26 |
}; |
| 27 |
callable.ffi_func = RustFfiFunctionName(ffi_func_name.clone()); |
| 28 |
|
| 29 |
let receiver_argument = match &callable.kind { |
| 30 |
CallableKind::Method { interface_name } => Some(FfiArgument { |
| 31 |
name: "uniffi_ptr".to_string(), |
| 32 |
ty: FfiType::RustArcPtr { |
| 33 |
module_name: module_name.clone(), |
| 34 |
object_name: interface_name.clone(), |
| 35 |
}, |
| 36 |
}), |
| 37 |
_ => None, |
| 38 |
}; |
| 39 |
let base_arguments = callable.arguments.iter().map(|arg| FfiArgument { |
| 40 |
name: arg.name.clone(), |
| 41 |
ty: arg.ty.ffi_type.clone(), |
| 42 |
}); |
| 43 |
|
| 44 |
ffi_definitions.push(if callable.async_data.is_none() { |
| 45 |
FfiFunction { |
| 46 |
name: RustFfiFunctionName(ffi_func_name), |
| 47 |
async_data: None, |
| 48 |
arguments: receiver_argument |
| 49 |
.into_iter() |
| 50 |
.chain(base_arguments) |
| 51 |
.collect(), |
| 52 |
return_type: FfiReturnType { |
| 53 |
ty: callable |
| 54 |
.return_type |
| 55 |
.ty |
| 56 |
.as_ref() |
| 57 |
.map(|ty| ty.ffi_type.clone()), |
| 58 |
}, |
| 59 |
has_rust_call_status_arg: true, |
| 60 |
kind: FfiFunctionKind::Scaffolding, |
| 61 |
..FfiFunction::default() |
| 62 |
} |
| 63 |
.into() |
| 64 |
} else { |
| 65 |
FfiFunction { |
| 66 |
name: RustFfiFunctionName(ffi_func_name), |
| 67 |
async_data: callable.async_data.clone(), |
| 68 |
arguments: receiver_argument |
| 69 |
.into_iter() |
| 70 |
.chain(base_arguments) |
| 71 |
.collect(), |
| 72 |
return_type: FfiReturnType { |
| 73 |
ty: Some(FfiType::Handle(HandleKind::RustFuture)), |
| 74 |
}, |
| 75 |
has_rust_call_status_arg: false, |
| 76 |
kind: FfiFunctionKind::Scaffolding, |
| 77 |
..FfiFunction::default() |
| 78 |
} |
| 79 |
.into() |
| 80 |
}); |
| 81 |
}); |
| 82 |
|
| 83 |
module.ffi_definitions.extend(ffi_definitions); |
| 84 |
Ok(()) |
| 85 |
} |