Don't require Store in Instance constructor (#810)
* Don't require `Store` in `Instance` constructor This can be inferred from the `Module` argument. Additionally add a `store` accessor to an `Instance` in case it's needed to instantiate another `Module`. cc #708 * Update more constructors * Fix a doctest * Don't ignore store in `wasm_instance_new` * Run rustfmt
This commit is contained in:
@@ -59,7 +59,6 @@ use wasmtime_runtime::Export;
|
||||
///
|
||||
/// // Create module instance that imports our function
|
||||
/// let instance = wasmtime::Instance::new(
|
||||
/// &store,
|
||||
/// &module,
|
||||
/// &[times_two_function.into()]
|
||||
/// )?;
|
||||
|
||||
@@ -68,7 +68,8 @@ pub struct Instance {
|
||||
}
|
||||
|
||||
impl Instance {
|
||||
pub fn new(store: &Store, module: &Module, externs: &[Extern]) -> Result<Instance, Error> {
|
||||
pub fn new(module: &Module, externs: &[Extern]) -> Result<Instance, Error> {
|
||||
let store = module.store();
|
||||
let context = store.context().clone();
|
||||
let exports = store.global_exports().clone();
|
||||
let (mut instance_handle, contexts) = instantiate_in_context(
|
||||
@@ -100,14 +101,27 @@ impl Instance {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn exports(&self) -> &[Extern] {
|
||||
&self.exports
|
||||
/// Returns the associated [`Store`] that this `Instance` is compiled into.
|
||||
///
|
||||
/// This is the [`Store`] that generally serves as a sort of global cache
|
||||
/// for various instance-related things.
|
||||
pub fn store(&self) -> &Store {
|
||||
self.module.store()
|
||||
}
|
||||
|
||||
/// Returns the associated [`Module`] that this `Instance` instantiated.
|
||||
///
|
||||
/// The corresponding [`Module`] here is a static version of this `Instance`
|
||||
/// which can be used to learn information such as naming information about
|
||||
/// various functions.
|
||||
pub fn module(&self) -> &Module {
|
||||
&self.module
|
||||
}
|
||||
|
||||
pub fn exports(&self) -> &[Extern] {
|
||||
&self.exports
|
||||
}
|
||||
|
||||
pub fn find_export_by_name(&self, name: &str) -> Option<&Extern> {
|
||||
let (i, _) = self
|
||||
.module
|
||||
|
||||
@@ -397,6 +397,10 @@ impl Store {
|
||||
.get(&type_index)
|
||||
.cloned()
|
||||
}
|
||||
|
||||
pub(crate) fn ptr_eq(a: &Store, b: &Store) -> bool {
|
||||
Rc::ptr_eq(&a.inner, &b.inner)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Store {
|
||||
|
||||
@@ -686,7 +686,6 @@ pub unsafe extern "C" fn wasm_instance_new(
|
||||
imports: *const *const wasm_extern_t,
|
||||
result: *mut *mut wasm_trap_t,
|
||||
) -> *mut wasm_instance_t {
|
||||
let store = &(*store).store.borrow();
|
||||
let mut externs: Vec<Extern> = Vec::with_capacity((*module).imports.len());
|
||||
for i in 0..(*module).imports.len() {
|
||||
let import = *imports.add(i);
|
||||
@@ -697,8 +696,21 @@ pub unsafe extern "C" fn wasm_instance_new(
|
||||
ExternHost::Memory(e) => Extern::Memory(e.borrow().clone()),
|
||||
});
|
||||
}
|
||||
let store = &(*store).store.borrow();
|
||||
let module = &(*module).module.borrow();
|
||||
match Instance::new(store, module, &externs) {
|
||||
// FIXME(WebAssembly/wasm-c-api#126) what else can we do with the `store`
|
||||
// argument?
|
||||
if !Store::ptr_eq(&store, module.store()) {
|
||||
if !result.is_null() {
|
||||
let trap = Trap::new("wasm_store_t must match store in wasm_module_t");
|
||||
let trap = Box::new(wasm_trap_t {
|
||||
trap: HostRef::new(trap),
|
||||
});
|
||||
(*result) = Box::into_raw(trap);
|
||||
}
|
||||
return ptr::null_mut();
|
||||
}
|
||||
match Instance::new(module, &externs) {
|
||||
Ok(instance) => {
|
||||
let instance = Box::new(wasm_instance_t {
|
||||
instance: HostRef::new(instance),
|
||||
|
||||
Reference in New Issue
Block a user