Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

* vm.rs - calling Ctx data_finalizer upon destruction #561

Merged
merged 3 commits into from
Jul 24, 2019
Merged
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
46 changes: 38 additions & 8 deletions lib/runtime-core/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ pub struct Ctx {
pub data_finalizer: Option<fn(data: *mut c_void)>,
}

/// When an instance context is destructed, we're calling its `data_finalizer`
/// In order avoid leaking resources.
///
/// Implementing the `data_finalizer` function is the responsibility of the `wasmer` end-user.
///
/// See test: `test_data_finalizer` as an example
impl Drop for Ctx {
fn drop(&mut self) {
if let Some(ref finalizer) = self.data_finalizer {
finalizer(self.data);
}
}
}

/// The internal context of the currently running WebAssembly instance.
///
///
Expand Down Expand Up @@ -749,14 +763,24 @@ mod vm_ctx_tests {
x: u32,
y: bool,
str: String,
finalizer: Box<FnMut()>,
}

impl Drop for TestData {
fn drop(&mut self) {
(*self.finalizer)();
}
}

fn test_data_finalizer(data: *mut c_void) {
let test_data: &mut TestData = unsafe { &mut *(data as *mut TestData) };
assert_eq!(test_data.x, 10);
assert_eq!(test_data.y, true);
assert_eq!(test_data.str, "Test".to_string());

assert_eq!(10, test_data.x);
assert_eq!(true, test_data.y);
assert_eq!("Test".to_string(), test_data.str,);

println!("hello from finalizer");

drop(test_data);
}

Expand All @@ -766,7 +790,9 @@ mod vm_ctx_tests {
x: 10,
y: true,
str: "Test".to_string(),
finalizer: Box::new(move || {}),
};

let mut local_backing = LocalBacking {
memories: Map::new().into_boxed_map(),
tables: Map::new().into_boxed_map(),
Expand All @@ -781,6 +807,7 @@ mod vm_ctx_tests {

internals: crate::backing::Internals([0; crate::backing::INTERNALS_SIZE]),
};

let mut import_backing = ImportBacking {
memories: Map::new().into_boxed_map(),
tables: Map::new().into_boxed_map(),
Expand All @@ -791,21 +818,24 @@ mod vm_ctx_tests {
vm_tables: Map::new().into_boxed_map(),
vm_globals: Map::new().into_boxed_map(),
};

let module = generate_module();
let data = &mut data as *mut _ as *mut c_void;
let data_ptr = &mut data as *mut _ as *mut c_void;
let ctx = unsafe {
Ctx::new_with_data(
&mut local_backing,
&mut import_backing,
&module,
data,
data_ptr,
test_data_finalizer,
)
};

let ctx_test_data = cast_test_data(ctx.data);
assert_eq!(ctx_test_data.x, 10);
assert_eq!(ctx_test_data.y, true);
assert_eq!(ctx_test_data.str, "Test".to_string());
assert_eq!(10, ctx_test_data.x);
assert_eq!(true, ctx_test_data.y);
assert_eq!("Test".to_string(), ctx_test_data.str);

drop(ctx);
}

Expand Down