diff --git a/crates/runtime/src/externref.rs b/crates/runtime/src/externref.rs index 0903f397e250..01c2b89872ab 100644 --- a/crates/runtime/src/externref.rs +++ b/crates/runtime/src/externref.rs @@ -669,7 +669,7 @@ impl VMExternRefActivationsTable { // The current `precise_stack_roots` becomes our new over-appoximated // set for the next GC cycle. let mut over_approximated = self.over_approximated_stack_roots.borrow_mut(); - mem::swap(&mut *precise_stack_roots, &mut *over_approximated); + mem::swap(precise_stack_roots, &mut *over_approximated); // And finally, the new `precise_stack_roots` should be cleared and // remain empty until the next GC cycle. diff --git a/crates/wasmtime/src/instance.rs b/crates/wasmtime/src/instance.rs index 2ca3aa421c9d..58acf6b41e31 100644 --- a/crates/wasmtime/src/instance.rs +++ b/crates/wasmtime/src/instance.rs @@ -1,8 +1,10 @@ +use crate::frame_info::GlobalFrameInfoRegistration; use crate::trampoline::StoreInstanceHandle; use crate::{Engine, Export, Extern, Func, Global, Memory, Module, Store, Table, Trap}; use anyhow::{anyhow, bail, Context, Error, Result}; use std::any::Any; use std::mem; +use std::sync::Arc; use wasmtime_environ::EntityIndex; use wasmtime_jit::CompiledModule; use wasmtime_runtime::{ @@ -102,7 +104,47 @@ pub struct Instance { module: Module, } +pub struct HostState { + #[allow(dead_code)] + frame_info_registration: Option>, + pub user_state: Option>, +} + impl Instance { + /// allows the caller to specify customimzed user_state, which can be accessed by + /// instance.host_state().downcast_ref::.user_state + pub fn new_with_user_state( + store: &Store, + module: &Module, + imports: &[Extern], + user_state: Option>, + ) -> Result { + if !Engine::same(store.engine(), module.engine()) { + bail!("cross-`Engine` instantiation is not currently supported"); + } + + let host_info = Box::new({ + let frame_info_registration = module.register_frame_info(); + store.register_jit_code(&module); + store.register_stack_maps(&module); + + HostState { + frame_info_registration, + user_state, + } + }); + + let handle = with_imports(store, module.compiled_module(), imports, |imports| { + instantiate(store, module.compiled_module(), imports, host_info) + })?; + + Ok(Instance { + handle, + store: store.clone(), + module: module.clone(), + }) + } + /// Creates a new [`Instance`] from the previously compiled [`Module`] and /// list of `imports` specified. /// @@ -157,26 +199,7 @@ impl Instance { /// [issue]: https://github.com/bytecodealliance/wasmtime/issues/727 /// [`ExternType`]: crate::ExternType pub fn new(store: &Store, module: &Module, imports: &[Extern]) -> Result { - if !Engine::same(store.engine(), module.engine()) { - bail!("cross-`Engine` instantiation is not currently supported"); - } - - let host_info = Box::new({ - let frame_info_registration = module.register_frame_info(); - store.register_jit_code(&module); - store.register_stack_maps(&module); - frame_info_registration - }); - - let handle = with_imports(store, module.compiled_module(), imports, |imports| { - instantiate(store, module.compiled_module(), imports, host_info) - })?; - - Ok(Instance { - handle, - store: store.clone(), - module: module.clone(), - }) + Instance::new_with_user_state(store, module, imports, None) } /// Returns the associated [`Store`] that this `Instance` is compiled into.