Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions crates/core/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1474,11 +1474,12 @@ impl<'a, B: Bindgen> Generator<'a, B> {
}

fn list_realloc(&self) -> Option<&'static str> {
// Lowering parameters calling a wasm import means
// we don't need to pass ownership, but we pass
// ownership in all other cases.
match (self.variant, self.lift_lower) {
(AbiVariant::GuestImport, LiftLower::LowerArgsLiftResults) => None,
// Lowering parameters calling a wasm import _or_ returning a result
// from an async-lifted wasm export means we don't need to pass
// ownership, but we pass ownership in all other cases.
match (self.variant, self.lift_lower, self.async_) {
Comment thread
dicej marked this conversation as resolved.
(AbiVariant::GuestImport, LiftLower::LowerArgsLiftResults, _)
| (AbiVariant::GuestExport, LiftLower::LiftArgsLowerResults, true) => None,
_ => Some("cabi_realloc"),
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/guest-rust/rt/src/async_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ unsafe fn poll(state: *mut FutureState) -> Poll<()> {
#[doc(hidden)]
pub fn first_poll<T: 'static>(
future: impl Future<Output = T> + 'static,
fun: impl FnOnce(T) + 'static,
fun: impl FnOnce(&T) + 'static,
) -> *mut u8 {
let state = Box::into_raw(Box::new(FutureState {
todo: 0,
tasks: Some(
[Box::pin(future.map(fun)) as BoxFuture]
[Box::pin(future.map(|v| fun(&v))) as BoxFuture]
.into_iter()
.collect(),
),
Expand Down
9 changes: 8 additions & 1 deletion crates/rust/src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub(super) struct FunctionBindgen<'a, 'b> {
pub handle_decls: Vec<String>,
always_owned: bool,
pub async_result_name: Option<String>,
emitted_cleanup: bool,
}

impl<'a, 'b> FunctionBindgen<'a, 'b> {
Expand All @@ -47,10 +48,15 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
handle_decls: Vec::new(),
always_owned,
async_result_name: None,
emitted_cleanup: false,
}
}

fn emit_cleanup(&mut self) {
if self.emitted_cleanup {
return;
}
self.emitted_cleanup = true;
for (ptr, layout) in mem::take(&mut self.cleanup) {
let alloc = self.gen.path_to_std_alloc_module();
self.push_str(&format!(
Expand Down Expand Up @@ -1001,10 +1007,11 @@ impl Bindgen for FunctionBindgen<'_, '_> {
self.src,
"\
{func}({});
}});
",
operands.join(", ")
);
self.emit_cleanup();
self.src.push_str("});\n");
}

Instruction::Flush { amt } => {
Expand Down
9 changes: 8 additions & 1 deletion crates/rust/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1088,7 +1088,14 @@ pub mod vtable{ordinal} {{
async_result_name,
..
} = f;
assert!(!needs_cleanup_list);
if async_ {
if needs_cleanup_list {
let vec = self.path_to_vec();
uwriteln!(self.src, "let mut cleanup_list = {vec}::new();");
}
} else {
assert!(!needs_cleanup_list);
}
if let Some(name) = async_result_name {
// When `async_result_name` is `Some(_)`, we wrap the call and any
// `handle_decls` in a block scope to ensure any resource handles
Expand Down