| 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 |
//! FFI info for callback interfaces |
| 6 |
|
| 7 |
use super::*; |
| 8 |
use std::collections::HashSet; |
| 9 |
|
| 10 |
pub fn pass(module: &mut Module) -> Result<()> { |
| 11 |
let crate_name = module.crate_name.clone(); |
| 12 |
module.try_visit_mut(|cbi: &mut CallbackInterface| { |
| 13 |
cbi.vtable = vtable(&crate_name, &cbi.name, cbi.methods.clone())?; |
| 14 |
Ok(()) |
| 15 |
})?; |
| 16 |
module.try_visit_mut(|int: &mut Interface| { |
| 17 |
if int.imp.has_callback_interface() { |
| 18 |
int.vtable = Some(vtable(&crate_name, &int.name, int.methods.clone())?); |
| 19 |
} |
| 20 |
Ok(()) |
| 21 |
})?; |
| 22 |
// Convert CallableKind:Method to CallableKind:VTableMethod |
| 23 |
module.try_visit_mut(|cbi: &mut CallbackInterface| { |
| 24 |
for meth in cbi.methods.iter_mut() { |
| 25 |
meth.callable.kind = match meth.callable.kind.take() { |
| 26 |
CallableKind::Method { interface_name } => CallableKind::VTableMethod { |
| 27 |
trait_name: interface_name, |
| 28 |
}, |
| 29 |
kind => bail!("Unexpected callable kind: {kind:?}"), |
| 30 |
}; |
| 31 |
} |
| 32 |
Ok(()) |
| 33 |
})?; |
| 34 |
module.try_visit_mut(|vtable: &mut VTable| { |
| 35 |
for meth in vtable.methods.iter_mut() { |
| 36 |
meth.callable.kind = match meth.callable.kind.take() { |
| 37 |
CallableKind::Method { interface_name } => CallableKind::VTableMethod { |
| 38 |
trait_name: interface_name, |
| 39 |
}, |
| 40 |
kind => bail!("Unexpected callable kind: {kind:?}"), |
| 41 |
}; |
| 42 |
} |
| 43 |
Ok(()) |
| 44 |
})?; |
| 45 |
|
| 46 |
add_vtable_ffi_definitions(module)?; |
| 47 |
Ok(()) |
| 48 |
} |
| 49 |
|
| 50 |
fn vtable(crate_name: &str, interface_name: &str, methods: Vec<Method>) -> Result<VTable> { |
| 51 |
Ok(VTable { |
| 52 |
struct_type: FfiType::Struct(FfiStructName(format!( |
| 53 |
"VTableCallbackInterface{}", |
| 54 |
interface_name |
| 55 |
))), |
| 56 |
interface_name: interface_name.to_string(), |
| 57 |
init_fn: RustFfiFunctionName(uniffi_meta::init_callback_vtable_fn_symbol_name( |
| 58 |
crate_name, |
| 59 |
interface_name, |
| 60 |
)), |
| 61 |
methods: methods |
| 62 |
.into_iter() |
| 63 |
.enumerate() |
| 64 |
.map(|(i, meth)| { |
| 65 |
Ok(VTableMethod { |
| 66 |
callable: meth.callable, |
| 67 |
ffi_type: FfiType::Function(FfiFunctionTypeName(format!( |
| 68 |
"CallbackInterface{}Method{i}", |
| 69 |
interface_name |
| 70 |
))), |
| 71 |
}) |
| 72 |
}) |
| 73 |
.collect::<Result<Vec<_>>>()?, |
| 74 |
}) |
| 75 |
} |
| 76 |
|
| 77 |
fn add_vtable_ffi_definitions(module: &mut Module) -> Result<()> { |
| 78 |
let mut ffi_definitions = vec![]; |
| 79 |
let mut seen_return_types = HashSet::new(); |
| 80 |
let module_name = module.name.clone(); |
| 81 |
module.try_visit(|vtable: &VTable| { |
| 82 |
let interface_name = &vtable.interface_name; |
| 83 |
// FFI Function Type for each method in the VTable |
| 84 |
for (i, meth) in vtable.methods.iter().enumerate() { |
| 85 |
let method_name = format!("CallbackInterface{interface_name}Method{i}"); |
| 86 |
match &meth.callable.async_data { |
| 87 |
Some(async_data) => { |
| 88 |
ffi_definitions.push(vtable_method_async( |
| 89 |
&module_name, |
| 90 |
interface_name, |
| 91 |
method_name, |
| 92 |
async_data, |
| 93 |
&meth.callable, |
| 94 |
)?); |
| 95 |
} |
| 96 |
None => { |
| 97 |
ffi_definitions.push(vtable_method( |
| 98 |
&module_name, |
| 99 |
interface_name, |
| 100 |
method_name, |
| 101 |
&meth.callable, |
| 102 |
)); |
| 103 |
} |
| 104 |
} |
| 105 |
// Async-related FFI definitions |
| 106 |
let Some(async_info) = &meth.callable.async_data else { |
| 107 |
continue; |
| 108 |
}; |
| 109 |
let ffi_return_type = meth |
| 110 |
.callable |
| 111 |
.return_type |
| 112 |
.ty |
| 113 |
.clone() |
| 114 |
.map(|return_type| return_type.ffi_type); |
| 115 |
|
| 116 |
if seen_return_types.insert(ffi_return_type.clone()) { |
| 117 |
ffi_definitions.extend([ |
| 118 |
FfiStruct { |
| 119 |
name: async_info.ffi_foreign_future_result.clone(), |
| 120 |
fields: match ffi_return_type { |
| 121 |
Some(return_ffi_type) => vec![ |
| 122 |
FfiField::new("return_value", return_ffi_type), |
| 123 |
FfiField::new("call_status", FfiType::RustCallStatus), |
| 124 |
], |
| 125 |
None => vec![ |
| 126 |
// In Rust, `return_value` is `()` -- a ZST. |
| 127 |
// ZSTs are not valid in `C`, but they also take up 0 space. |
| 128 |
// Skip the `return_value` field to make the layout correct. |
| 129 |
FfiField::new("call_status", FfiType::RustCallStatus), |
| 130 |
], |
| 131 |
}, |
| 132 |
} |
| 133 |
.into(), |
| 134 |
FfiFunctionType { |
| 135 |
name: async_info.ffi_foreign_future_complete.clone(), |
| 136 |
arguments: vec![ |
| 137 |
FfiArgument::new( |
| 138 |
"callback_data", |
| 139 |
FfiType::Handle(HandleKind::ForeignFuture), |
| 140 |
), |
| 141 |
FfiArgument::new( |
| 142 |
"result", |
| 143 |
FfiType::Struct(async_info.ffi_foreign_future_result.clone()), |
| 144 |
), |
| 145 |
], |
| 146 |
return_type: FfiReturnType { ty: None }, |
| 147 |
has_rust_call_status_arg: false, |
| 148 |
} |
| 149 |
.into(), |
| 150 |
]); |
| 151 |
} |
| 152 |
} |
| 153 |
// FFIStruct for the VTable |
| 154 |
ffi_definitions.extend([ |
| 155 |
FfiFunctionType { |
| 156 |
name: FfiFunctionTypeName(format!( |
| 157 |
"CallbackInterfaceFree{module_name}_{interface_name}" |
| 158 |
)), |
| 159 |
arguments: vec![FfiArgument::new( |
| 160 |
"handle", |
| 161 |
FfiType::Handle(HandleKind::CallbackInterface { |
| 162 |
module_name: module_name.to_string(), |
| 163 |
interface_name: interface_name.to_string(), |
| 164 |
}), |
| 165 |
)], |
| 166 |
return_type: FfiReturnType { ty: None }, |
| 167 |
has_rust_call_status_arg: false, |
| 168 |
} |
| 169 |
.into(), |
| 170 |
FfiStruct { |
| 171 |
name: FfiStructName(format!("VTableCallbackInterface{}", interface_name)), |
| 172 |
fields: vtable |
| 173 |
.methods |
| 174 |
.iter() |
| 175 |
.map(|vtable_meth| { |
| 176 |
FfiField::new(&vtable_meth.callable.name, vtable_meth.ffi_type.clone()) |
| 177 |
}) |
| 178 |
.chain([FfiField::new( |
| 179 |
"uniffi_free", |
| 180 |
FfiType::Function(FfiFunctionTypeName(format!( |
| 181 |
"CallbackInterfaceFree{module_name}_{interface_name}" |
| 182 |
))), |
| 183 |
)]) |
| 184 |
.collect(), |
| 185 |
} |
| 186 |
.into(), |
| 187 |
]); |
| 188 |
// FFI Function to initialize the VTable |
| 189 |
ffi_definitions.push( |
| 190 |
FfiFunction { |
| 191 |
name: vtable.init_fn.clone(), |
| 192 |
arguments: vec![FfiArgument { |
| 193 |
name: "vtable".into(), |
| 194 |
ty: FfiType::Reference(Box::new(vtable.struct_type.clone())), |
| 195 |
}], |
| 196 |
return_type: FfiReturnType { ty: None }, |
| 197 |
async_data: None, |
| 198 |
has_rust_call_status_arg: false, |
| 199 |
kind: FfiFunctionKind::RustVtableInit, |
| 200 |
..FfiFunction::default() |
| 201 |
} |
| 202 |
.into(), |
| 203 |
); |
| 204 |
Ok(()) |
| 205 |
})?; |
| 206 |
module.ffi_definitions.extend(ffi_definitions); |
| 207 |
Ok(()) |
| 208 |
} |
| 209 |
|
| 210 |
fn vtable_method( |
| 211 |
module_name: &str, |
| 212 |
interface_name: &str, |
| 213 |
method_name: String, |
| 214 |
callable: &Callable, |
| 215 |
) -> FfiDefinition { |
| 216 |
FfiFunctionType { |
| 217 |
name: FfiFunctionTypeName(method_name), |
| 218 |
arguments: std::iter::once(FfiArgument { |
| 219 |
name: "uniffi_handle".into(), |
| 220 |
ty: FfiType::Handle(HandleKind::CallbackInterface { |
| 221 |
module_name: module_name.to_string(), |
| 222 |
interface_name: interface_name.to_string(), |
| 223 |
}), |
| 224 |
}) |
| 225 |
.chain(callable.arguments.iter().map(|arg| FfiArgument { |
| 226 |
name: arg.name.clone(), |
| 227 |
ty: arg.ty.ffi_type.clone(), |
| 228 |
})) |
| 229 |
.chain(std::iter::once(match &callable.return_type.ty { |
| 230 |
Some(ty) => FfiArgument { |
| 231 |
name: "uniffi_out_return".into(), |
| 232 |
ty: FfiType::MutReference(Box::new(ty.ffi_type.clone())), |
| 233 |
}, |
| 234 |
None => FfiArgument { |
| 235 |
name: "uniffi_out_return".into(), |
| 236 |
ty: FfiType::VoidPointer, |
| 237 |
}, |
| 238 |
})) |
| 239 |
.collect(), |
| 240 |
has_rust_call_status_arg: true, |
| 241 |
return_type: FfiReturnType { ty: None }, |
| 242 |
} |
| 243 |
.into() |
| 244 |
} |
| 245 |
|
| 246 |
fn vtable_method_async( |
| 247 |
module_name: &str, |
| 248 |
interface_name: &str, |
| 249 |
method_name: String, |
| 250 |
async_data: &AsyncData, |
| 251 |
callable: &Callable, |
| 252 |
) -> Result<FfiDefinition> { |
| 253 |
Ok(FfiFunctionType { |
| 254 |
name: FfiFunctionTypeName(method_name), |
| 255 |
arguments: std::iter::once(FfiArgument { |
| 256 |
name: "uniffi_handle".into(), |
| 257 |
ty: FfiType::Handle(HandleKind::CallbackInterface { |
| 258 |
module_name: module_name.to_string(), |
| 259 |
interface_name: interface_name.to_string(), |
| 260 |
}), |
| 261 |
}) |
| 262 |
.chain(callable.arguments.iter().map(|arg| FfiArgument { |
| 263 |
name: arg.name.clone(), |
| 264 |
ty: arg.ty.ffi_type.clone(), |
| 265 |
})) |
| 266 |
.chain([ |
| 267 |
FfiArgument { |
| 268 |
name: "uniffi_future_callback".into(), |
| 269 |
ty: FfiType::Function(async_data.ffi_foreign_future_complete.clone()), |
| 270 |
}, |
| 271 |
FfiArgument { |
| 272 |
name: "uniffi_callback_data".into(), |
| 273 |
ty: FfiType::Handle(HandleKind::ForeignFutureCallbackData), |
| 274 |
}, |
| 275 |
FfiArgument { |
| 276 |
name: "uniffi_out_return".into(), |
| 277 |
ty: FfiType::MutReference(Box::new(FfiType::Struct(FfiStructName( |
| 278 |
"ForeignFuture".to_owned(), |
| 279 |
)))), |
| 280 |
}, |
| 281 |
]) |
| 282 |
.collect(), |
| 283 |
has_rust_call_status_arg: false, |
| 284 |
return_type: FfiReturnType { ty: None }, |
| 285 |
} |
| 286 |
.into()) |
| 287 |
} |