Skip to content

Commit d61497b

Browse files
committed
fix lowering results for async exports
Previously, we were either dropping the result too early (e.g. `wit_bindgen_rt::async_support::ErrorContext`) or not at all (e.g. `String` and `Vec`) due to the lowering code expecting to pass ownership to the caller, which would later free memory using a post-return function. But async exports don't have post-return functions; they use `task.return` instead, and they should free any memory (and/or drop handles) after `task.return` returns. So now we do that! Signed-off-by: Joel Dice <joel.dice@fermyon.com>
1 parent 5905093 commit d61497b

2 files changed

Lines changed: 5 additions & 4 deletions

File tree

crates/core/src/abi.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,8 +1477,9 @@ impl<'a, B: Bindgen> Generator<'a, B> {
14771477
// Lowering parameters calling a wasm import means
14781478
// we don't need to pass ownership, but we pass
14791479
// ownership in all other cases.
1480-
match (self.variant, self.lift_lower) {
1481-
(AbiVariant::GuestImport, LiftLower::LowerArgsLiftResults) => None,
1480+
match (self.variant, self.lift_lower, self.async_) {
1481+
(AbiVariant::GuestImport, LiftLower::LowerArgsLiftResults, _)
1482+
| (AbiVariant::GuestExport, LiftLower::LiftArgsLowerResults, true) => None,
14821483
_ => Some("cabi_realloc"),
14831484
}
14841485
}

crates/guest-rust/rt/src/async_support.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,12 @@ unsafe fn poll(state: *mut FutureState) -> Poll<()> {
122122
#[doc(hidden)]
123123
pub fn first_poll<T: 'static>(
124124
future: impl Future<Output = T> + 'static,
125-
fun: impl FnOnce(T) + 'static,
125+
fun: impl FnOnce(&T) + 'static,
126126
) -> *mut u8 {
127127
let state = Box::into_raw(Box::new(FutureState {
128128
todo: 0,
129129
tasks: Some(
130-
[Box::pin(future.map(fun)) as BoxFuture]
130+
[Box::pin(future.map(|v| fun(&v))) as BoxFuture]
131131
.into_iter()
132132
.collect(),
133133
),

0 commit comments

Comments
 (0)