Refactor the C API to eliminate memory leaks

This commit is contained in:
Amanieu d'Antras
2021-11-15 16:32:17 +00:00
parent f2633d927c
commit 75cb5ab788
19 changed files with 403 additions and 723 deletions

View File

@@ -28,7 +28,7 @@ pub unsafe extern "C" fn wasm_trap_new(
_store: &mut wasm_store_t,
message: &wasm_message_t,
) -> Option<Box<wasm_trap_t>> {
let message_bytes = message.into_slice()?;
let message_bytes = message.as_slice();
// The trap message is typed with `wasm_message_t` which is a
// typeref to `wasm_name_t` with the exception that it's a
@@ -117,10 +117,7 @@ pub unsafe extern "C" fn wasm_trap_message(
let mut byte_vec = message.into_bytes();
byte_vec.push(0);
let byte_vec: wasm_byte_vec_t = byte_vec.into();
out.size = byte_vec.size;
out.data = byte_vec.data;
out.set_buffer(byte_vec);
}
/// Gets the origin frame attached to the trap.
@@ -137,10 +134,12 @@ pub unsafe extern "C" fn wasm_trap_trace(
out: &mut wasm_frame_vec_t,
) {
let frames = trap.inner.trace();
let frame_vec: wasm_frame_vec_t = frames.into();
out.size = frame_vec.size;
out.data = frame_vec.data;
out.set_buffer(
frames
.iter()
.map(|frame| Some(Box::new(frame.into())))
.collect(),
);
}
#[cfg(test)]