1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use crate::guest;
use crate::prelude::*;
use wasmer_runtime::Ctx;
pub fn __import_data(ctx: &mut Ctx, guest_ptr: GuestPtr) -> Result<(), WasmError> {
if !ctx.data.is_null() {
let b: Box<SerializedBytes> = unsafe { Box::from_raw(ctx.data as _) };
guest::write_bytes(ctx, guest_ptr, &*b.bytes())?;
}
ctx.data = std::ptr::null::<SerializedBytes>() as _;
Ok(())
}
#[allow(unused_assignments)]
pub fn free_context_data(data: *mut std::ffi::c_void) {
if !data.is_null() {
let _: Box<SerializedBytes> = unsafe { Box::from_raw(data as _) };
}
}
pub fn set_context_data(ctx: &mut Ctx, serialized_bytes: SerializedBytes) -> Len {
free_context_data(ctx.data);
let len = serialized_bytes.bytes().len();
let b = Box::new(serialized_bytes);
ctx.data = Box::into_raw(b) as _;
len as Len
}