| 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 |
// Add Future-related FFI definitions |
| 11 |
module.ffi_definitions.extend([ |
| 12 |
FfiFunctionType { |
| 13 |
name: FfiFunctionTypeName("RustFutureContinuationCallback".to_owned()), |
| 14 |
arguments: vec![ |
| 15 |
FfiArgument::new("data", FfiType::Handle(HandleKind::RustFuture)), |
| 16 |
FfiArgument::new("poll_result", FfiType::Int8), |
| 17 |
], |
| 18 |
return_type: FfiReturnType { ty: None }, |
| 19 |
has_rust_call_status_arg: false, |
| 20 |
} |
| 21 |
.into(), |
| 22 |
FfiFunctionType { |
| 23 |
name: FfiFunctionTypeName("ForeignFutureFree".to_owned()), |
| 24 |
arguments: vec![FfiArgument::new( |
| 25 |
"handle", |
| 26 |
FfiType::Handle(HandleKind::ForeignFuture), |
| 27 |
)], |
| 28 |
return_type: FfiReturnType { ty: None }, |
| 29 |
has_rust_call_status_arg: false, |
| 30 |
} |
| 31 |
.into(), |
| 32 |
FfiStruct { |
| 33 |
name: FfiStructName("ForeignFuture".to_owned()), |
| 34 |
fields: vec![ |
| 35 |
FfiField::new("handle", FfiType::Handle(HandleKind::ForeignFuture)), |
| 36 |
FfiField::new( |
| 37 |
"free", |
| 38 |
FfiType::Function(FfiFunctionTypeName("ForeignFutureFree".to_owned())), |
| 39 |
), |
| 40 |
], |
| 41 |
} |
| 42 |
.into(), |
| 43 |
]); |
| 44 |
|
| 45 |
// Add ffi definitions to support async calls and populate the maps |
| 46 |
let all_async_return_types = [ |
| 47 |
(Some(FfiType::UInt8), "u8"), |
| 48 |
(Some(FfiType::Int8), "i8"), |
| 49 |
(Some(FfiType::UInt16), "u16"), |
| 50 |
(Some(FfiType::Int16), "i16"), |
| 51 |
(Some(FfiType::UInt32), "u32"), |
| 52 |
(Some(FfiType::Int32), "i32"), |
| 53 |
(Some(FfiType::UInt64), "u64"), |
| 54 |
(Some(FfiType::Int64), "i64"), |
| 55 |
(Some(FfiType::Float32), "f32"), |
| 56 |
(Some(FfiType::Float64), "f64"), |
| 57 |
( |
| 58 |
Some(FfiType::RustArcPtr { |
| 59 |
module_name: "".into(), |
| 60 |
object_name: "".into(), |
| 61 |
}), |
| 62 |
"pointer", |
| 63 |
), |
| 64 |
(Some(FfiType::RustBuffer(None)), "rust_buffer"), |
| 65 |
(None, "void"), |
| 66 |
]; |
| 67 |
for (return_type, return_type_name) in all_async_return_types { |
| 68 |
let poll_name = format!("ffi_{namespace}_rust_future_poll_{return_type_name}"); |
| 69 |
module.ffi_definitions.push(ffi_rust_future_poll(poll_name)); |
| 70 |
|
| 71 |
let cancel_name = format!("ffi_{namespace}_rust_future_cancel_{return_type_name}"); |
| 72 |
module |
| 73 |
.ffi_definitions |
| 74 |
.push(ffi_rust_future_cancel(cancel_name)); |
| 75 |
|
| 76 |
let complete_name = format!("ffi_{namespace}_rust_future_complete_{return_type_name}"); |
| 77 |
module |
| 78 |
.ffi_definitions |
| 79 |
.push(ffi_rust_future_complete(return_type.clone(), complete_name)); |
| 80 |
|
| 81 |
let free_name = format!("ffi_{namespace}_rust_future_free_{return_type_name}"); |
| 82 |
module.ffi_definitions.push(ffi_rust_future_free(free_name)); |
| 83 |
} |
| 84 |
Ok(()) |
| 85 |
} |
| 86 |
|
| 87 |
fn ffi_rust_future_poll(symbol_name: String) -> FfiDefinition { |
| 88 |
FfiFunction { |
| 89 |
name: RustFfiFunctionName(symbol_name), |
| 90 |
async_data: None, |
| 91 |
arguments: vec![ |
| 92 |
FfiArgument { |
| 93 |
name: "handle".to_owned(), |
| 94 |
ty: FfiType::Handle(HandleKind::RustFuture), |
| 95 |
}, |
| 96 |
FfiArgument { |
| 97 |
name: "callback".to_owned(), |
| 98 |
ty: FfiType::Function(FfiFunctionTypeName( |
| 99 |
"RustFutureContinuationCallback".to_owned(), |
| 100 |
)), |
| 101 |
}, |
| 102 |
FfiArgument { |
| 103 |
name: "callback_data".to_owned(), |
| 104 |
ty: FfiType::Handle(HandleKind::RustFuture), |
| 105 |
}, |
| 106 |
], |
| 107 |
return_type: FfiReturnType { ty: None }, |
| 108 |
has_rust_call_status_arg: false, |
| 109 |
kind: FfiFunctionKind::RustFuturePoll, |
| 110 |
..FfiFunction::default() |
| 111 |
} |
| 112 |
.into() |
| 113 |
} |
| 114 |
|
| 115 |
fn ffi_rust_future_cancel(symbol_name: String) -> FfiDefinition { |
| 116 |
FfiFunction { |
| 117 |
name: RustFfiFunctionName(symbol_name), |
| 118 |
async_data: None, |
| 119 |
arguments: vec![FfiArgument { |
| 120 |
name: "handle".to_owned(), |
| 121 |
ty: FfiType::Handle(HandleKind::RustFuture), |
| 122 |
}], |
| 123 |
return_type: FfiReturnType { ty: None }, |
| 124 |
has_rust_call_status_arg: false, |
| 125 |
kind: FfiFunctionKind::RustFutureCancel, |
| 126 |
..FfiFunction::default() |
| 127 |
} |
| 128 |
.into() |
| 129 |
} |
| 130 |
|
| 131 |
fn ffi_rust_future_complete(return_type: Option<FfiType>, symbol_name: String) -> FfiDefinition { |
| 132 |
FfiFunction { |
| 133 |
name: RustFfiFunctionName(symbol_name), |
| 134 |
async_data: None, |
| 135 |
arguments: vec![FfiArgument { |
| 136 |
name: "handle".to_owned(), |
| 137 |
ty: FfiType::Handle(HandleKind::RustFuture), |
| 138 |
}], |
| 139 |
return_type: FfiReturnType { ty: return_type }, |
| 140 |
has_rust_call_status_arg: true, |
| 141 |
kind: FfiFunctionKind::RustFutureComplete, |
| 142 |
..FfiFunction::default() |
| 143 |
} |
| 144 |
.into() |
| 145 |
} |
| 146 |
|
| 147 |
fn ffi_rust_future_free(symbol_name: String) -> FfiDefinition { |
| 148 |
FfiFunction { |
| 149 |
name: RustFfiFunctionName(symbol_name), |
| 150 |
async_data: None, |
| 151 |
arguments: vec![FfiArgument { |
| 152 |
name: "handle".to_owned(), |
| 153 |
ty: FfiType::Handle(HandleKind::RustFuture), |
| 154 |
}], |
| 155 |
return_type: FfiReturnType { ty: None }, |
| 156 |
has_rust_call_status_arg: false, |
| 157 |
kind: FfiFunctionKind::RustFutureFree, |
| 158 |
..FfiFunction::default() |
| 159 |
} |
| 160 |
.into() |
| 161 |
} |