Skip to content

Commit

Permalink
Try #561:
Browse files Browse the repository at this point in the history
  • Loading branch information
bors[bot] committed Jul 15, 2019
2 parents 8a471cc + ad10152 commit 6487b41
Showing 1 changed file with 38 additions and 8 deletions.
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

0 comments on commit 6487b41

Please sign in to comment.