Skip to content

Commit

Permalink
Instance now pulls state from the ImportObject
Browse files Browse the repository at this point in the history
  • Loading branch information
lachlansneff committed Mar 28, 2019
1 parent e3a6b7c commit 7b0992e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
8 changes: 6 additions & 2 deletions lib/runtime-core/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use hashbrown::{hash_map::Entry, HashMap};
use std::collections::VecDeque;
use std::{
cell::{Ref, RefCell},
rc::Rc,
ffi::c_void,
rc::Rc,
};

pub trait LikeNamespace {
Expand Down Expand Up @@ -58,7 +58,7 @@ impl ImportObject {
}
}

pub fn new_with_data<F>(state_creator: F) -> Self
pub fn new_with_data<F>(state_creator: F) -> Self
where
F: Fn() -> (*mut c_void, fn(*mut c_void)) + 'static,
{
Expand All @@ -68,6 +68,10 @@ impl ImportObject {
}
}

pub(crate) fn call_state_creator(&self) -> Option<(*mut c_void, fn(*mut c_void))> {
self.state_creator.as_ref().map(|state_gen| state_gen())
}

/// Register anything that implements `LikeNamespace` as a namespace.
///
/// # Usage:
Expand Down
11 changes: 10 additions & 1 deletion lib/runtime-core/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,16 @@ impl Instance {
// Initialize the vm::Ctx in-place after the backing
// has been boxed.
unsafe {
*inner.vmctx = vm::Ctx::new(&mut inner.backing, &mut inner.import_backing, &module)
*inner.vmctx = match imports.call_state_creator() {
Some((data, dtor)) => vm::Ctx::new_with_data(
&mut inner.backing,
&mut inner.import_backing,
&module,
data,
dtor,
),
None => vm::Ctx::new(&mut inner.backing, &mut inner.import_backing, &module),
};
};

let instance = Instance {
Expand Down
2 changes: 1 addition & 1 deletion lib/runtime-core/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ macro_rules! func {
/// "foo" => func!(foo),
/// },
/// };
///
///
/// let imports_with_state = imports! {
/// || (0 as _, |_a| {}),
/// "env" => {
Expand Down
4 changes: 2 additions & 2 deletions lib/runtime-core/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct Ctx {
module: *const ModuleInner,

pub data: *mut c_void,
pub data_finalizer: Option<extern "C" fn(data: *mut c_void)>,
pub data_finalizer: Option<fn(data: *mut c_void)>,
}

/// The internal context of the currently running WebAssembly instance.
Expand Down Expand Up @@ -100,7 +100,7 @@ impl Ctx {
import_backing: &mut ImportBacking,
module: &ModuleInner,
data: *mut c_void,
data_finalizer: extern "C" fn(*mut c_void),
data_finalizer: fn(*mut c_void),
) -> Self {
Self {
internal: InternalCtx {
Expand Down

0 comments on commit 7b0992e

Please sign in to comment.