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

adding tests for state_creator of import_object #865

Closed
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
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