| 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 |
|
| 7 |
pub fn pass(module: &mut Module) -> Result<()> { |
| 8 |
let namespace = module.crate_name.clone(); |
| 9 |
|
| 10 |
// Change `is_async` to `async_data` |
| 11 |
module.visit_mut(|callable: &mut Callable| { |
| 12 |
callable.async_data = callable |
| 13 |
.is_async |
| 14 |
.then(|| generate_async_data(&namespace, callable.ffi_return_type())); |
| 15 |
}); |
| 16 |
module.visit_mut(|ffi_func: &mut FfiFunction| { |
| 17 |
ffi_func.async_data = ffi_func |
| 18 |
.is_async |
| 19 |
.then(|| generate_async_data(&namespace, ffi_func.return_type.ty.as_ref())); |
| 20 |
}); |
| 21 |
Ok(()) |
| 22 |
} |
| 23 |
|
| 24 |
fn generate_async_data(namespace: &str, ffi_return_type: Option<&FfiType>) -> AsyncData { |
| 25 |
let return_type_name = match ffi_return_type { |
| 26 |
Some(FfiType::UInt8) => "u8", |
| 27 |
Some(FfiType::Int8) => "i8", |
| 28 |
Some(FfiType::UInt16) => "u16", |
| 29 |
Some(FfiType::Int16) => "i16", |
| 30 |
Some(FfiType::UInt32) => "u32", |
| 31 |
Some(FfiType::Int32) => "i32", |
| 32 |
Some(FfiType::UInt64) => "u64", |
| 33 |
Some(FfiType::Int64) => "i64", |
| 34 |
Some(FfiType::Float32) => "f32", |
| 35 |
Some(FfiType::Float64) => "f64", |
| 36 |
Some(FfiType::RustArcPtr { .. }) => "pointer", |
| 37 |
Some(FfiType::RustBuffer(_)) => "rust_buffer", |
| 38 |
None => "void", |
| 39 |
ty => panic!("Invalid future return type: {ty:?}"), |
| 40 |
}; |
| 41 |
AsyncData { |
| 42 |
ffi_rust_future_poll: RustFfiFunctionName(format!( |
| 43 |
"ffi_{namespace}_rust_future_poll_{return_type_name}" |
| 44 |
)), |
| 45 |
ffi_rust_future_cancel: RustFfiFunctionName(format!( |
| 46 |
"ffi_{namespace}_rust_future_cancel_{return_type_name}" |
| 47 |
)), |
| 48 |
ffi_rust_future_complete: RustFfiFunctionName(format!( |
| 49 |
"ffi_{namespace}_rust_future_complete_{return_type_name}" |
| 50 |
)), |
| 51 |
ffi_rust_future_free: RustFfiFunctionName(format!( |
| 52 |
"ffi_{namespace}_rust_future_free_{return_type_name}" |
| 53 |
)), |
| 54 |
ffi_foreign_future_result: FfiStructName(format!("ForeignFutureResult{return_type_name}")), |
| 55 |
ffi_foreign_future_complete: FfiFunctionTypeName(format!( |
| 56 |
"ForeignFutureComplete{return_type_name}" |
| 57 |
)), |
| 58 |
} |
| 59 |
} |