Skip to content

Commit

Permalink
Merge #865
Browse files Browse the repository at this point in the history
865: adding tests for `state_creator` of `import_object` r=MarkMcCaskey a=YaronWittenstein

Part of the PR #807 changes was adding support for shared import objects between threads.

https://github.com/wasmerio/wasmer/pull/807/files#diff-d20cb4c5a883566b85be4cc046f45aa9R49

I've added tests/examples on how to create an `import object` with a state_creator
(function or closure)

Co-authored-by: Yaron Wittenstein <[email protected]>
  • Loading branch information
bors[bot] and YaronWittenstein authored Nov 25, 2019
2 parents a69c029 + 4adfbc4 commit 7cd2965
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions lib/runtime-core/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,54 @@ mod test {
use crate::export::Export;
use crate::global::Global;
use crate::types::Value;
use std::ffi::c_void;
use std::sync::Arc;

struct Data {
inner: *const u32,
}

unsafe impl Send for Data {}
unsafe impl Sync for Data {}

fn dummy_state_creator(data: Arc<Data>) -> (*mut c_void, fn(*mut c_void)) {
let data: *mut Data = Arc::into_raw(data) as _;

(data as _, |_| {})
}

#[test]
fn state_creator_fn() {
let ptr = &0xAABBCCDDu32 as *const u32;
let data = Arc::new(Data { inner: ptr });

let imports = imports! {
move || dummy_state_creator(Arc::clone(&data)),
};

let (state, _dtor) = imports.call_state_creator().unwrap();
let data: &mut Data = unsafe { &mut *(state as *mut Data) };

assert_eq!(ptr, data.inner);
}

#[test]
fn state_creator_closure() {
let ptr = &0xAABBCCDDu32 as *const u32;
let data = Arc::new(Data { inner: ptr });

let imports = imports! {
move || {
let data: *mut Data = Arc::into_raw(Arc::clone(&data)) as _;
(data as _, |_| {})
},
};

let (state, _dtor) = imports.call_state_creator().unwrap();
let data: &mut Data = unsafe { &mut *(state as *mut Data) };

assert_eq!(ptr, data.inner);
}

#[test]
fn extending_works() {
Expand Down

0 comments on commit 7cd2965

Please sign in to comment.