From 65c11337f807aaf2948572075315212c8027fc12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=BF=97=E5=BC=BA?= <652732310@qq.com> Date: Thu, 8 Oct 2020 19:57:57 +0800 Subject: [PATCH 1/8] add new_with_user_state --- crates/runtime/src/externref.rs | 2 +- crates/wasmtime/src/instance.rs | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) 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..2816918e4b7a 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,6 +104,11 @@ pub struct Instance { module: Module, } +pub struct HostState { + frame_info_registration: Box>>, + pub user_state: Option, +} + impl Instance { /// Creates a new [`Instance`] from the previously compiled [`Module`] and /// list of `imports` specified. @@ -156,7 +163,12 @@ impl Instance { /// [inst]: https://webassembly.github.io/spec/core/exec/modules.html#exec-instantiation /// [issue]: https://github.com/bytecodealliance/wasmtime/issues/727 /// [`ExternType`]: crate::ExternType - pub fn new(store: &Store, module: &Module, imports: &[Extern]) -> Result { + fn _new( + 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"); } @@ -165,7 +177,11 @@ impl Instance { let frame_info_registration = module.register_frame_info(); store.register_jit_code(&module); store.register_stack_maps(&module); - frame_info_registration + + HostState { + frame_info_registration, + user_state, + } }); let handle = with_imports(store, module.compiled_module(), imports, |imports| { @@ -179,6 +195,19 @@ impl Instance { }) } + pub fn new(store: &Store, module: &Module, imports: &[Extern]) -> Result { + return _new(store, module, imports, None); + } + + pub fn new_with_user_state( + store: &Store, + module: &Module, + imports: &[Extern], + user_state: Option, + ) -> Result { + return _new(store, module, imports, user_state); + } + /// Returns the associated [`Store`] that this `Instance` is compiled into. /// /// This is the [`Store`] that generally serves as a sort of global cache From c1310f9de12ede75fd283a0bb82e5509060ac8ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=BF=97=E5=BC=BA?= <652732310@qq.com> Date: Thu, 8 Oct 2020 20:07:53 +0800 Subject: [PATCH 2/8] fix according to review --- crates/wasmtime/src/instance.rs | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/crates/wasmtime/src/instance.rs b/crates/wasmtime/src/instance.rs index 2816918e4b7a..e9569593b5dc 100644 --- a/crates/wasmtime/src/instance.rs +++ b/crates/wasmtime/src/instance.rs @@ -163,7 +163,7 @@ impl Instance { /// [inst]: https://webassembly.github.io/spec/core/exec/modules.html#exec-instantiation /// [issue]: https://github.com/bytecodealliance/wasmtime/issues/727 /// [`ExternType`]: crate::ExternType - fn _new( + fn new_with_user_state( store: &Store, module: &Module, imports: &[Extern], @@ -196,16 +196,7 @@ impl Instance { } pub fn new(store: &Store, module: &Module, imports: &[Extern]) -> Result { - return _new(store, module, imports, None); - } - - pub fn new_with_user_state( - store: &Store, - module: &Module, - imports: &[Extern], - user_state: Option, - ) -> Result { - return _new(store, module, imports, user_state); + return new_with_user_state(store, module, imports, None); } /// Returns the associated [`Store`] that this `Instance` is compiled into. From 9d744888c603728666858dbe93b2a9e35ed039fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=BF=97=E5=BC=BA?= <652732310@qq.com> Date: Thu, 8 Oct 2020 20:11:48 +0800 Subject: [PATCH 3/8] add missing pub --- crates/wasmtime/src/instance.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/wasmtime/src/instance.rs b/crates/wasmtime/src/instance.rs index e9569593b5dc..bbc5bb044cba 100644 --- a/crates/wasmtime/src/instance.rs +++ b/crates/wasmtime/src/instance.rs @@ -163,7 +163,7 @@ impl Instance { /// [inst]: https://webassembly.github.io/spec/core/exec/modules.html#exec-instantiation /// [issue]: https://github.com/bytecodealliance/wasmtime/issues/727 /// [`ExternType`]: crate::ExternType - fn new_with_user_state( + pub fn new_with_user_state( store: &Store, module: &Module, imports: &[Extern], From c6a1dfc4acd62048ae9dd1140e2369387acc1cdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=BF=97=E5=BC=BA?= <652732310@qq.com> Date: Thu, 8 Oct 2020 20:17:18 +0800 Subject: [PATCH 4/8] prefer implicit return --- crates/wasmtime/src/instance.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/wasmtime/src/instance.rs b/crates/wasmtime/src/instance.rs index bbc5bb044cba..2b5fc6ad0b0b 100644 --- a/crates/wasmtime/src/instance.rs +++ b/crates/wasmtime/src/instance.rs @@ -196,7 +196,7 @@ impl Instance { } pub fn new(store: &Store, module: &Module, imports: &[Extern]) -> Result { - return new_with_user_state(store, module, imports, None); + new_with_user_state(store, module, imports, None) } /// Returns the associated [`Store`] that this `Instance` is compiled into. From 3db1e7ebe4dd07d801519e42557a9fc54853acda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=BF=97=E5=BC=BA?= <652732310@qq.com> Date: Thu, 8 Oct 2020 20:17:54 +0800 Subject: [PATCH 5/8] fix --- crates/wasmtime/src/instance.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/wasmtime/src/instance.rs b/crates/wasmtime/src/instance.rs index 2b5fc6ad0b0b..69f0a5214652 100644 --- a/crates/wasmtime/src/instance.rs +++ b/crates/wasmtime/src/instance.rs @@ -196,7 +196,7 @@ impl Instance { } pub fn new(store: &Store, module: &Module, imports: &[Extern]) -> Result { - new_with_user_state(store, module, imports, None) + Instance::new_with_user_state(store, module, imports, None) } /// Returns the associated [`Store`] that this `Instance` is compiled into. From 372ed924c94bfa57f6e88df0ab809b736db85e09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=BF=97=E5=BC=BA?= <652732310@qq.com> Date: Thu, 8 Oct 2020 20:26:34 +0800 Subject: [PATCH 6/8] fix --- crates/wasmtime/src/instance.rs | 70 +++++++++++++++++---------------- 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/crates/wasmtime/src/instance.rs b/crates/wasmtime/src/instance.rs index 69f0a5214652..78fb17a9fe3d 100644 --- a/crates/wasmtime/src/instance.rs +++ b/crates/wasmtime/src/instance.rs @@ -105,11 +105,45 @@ pub struct Instance { } pub struct HostState { - frame_info_registration: Box>>, - pub user_state: Option, + 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(HostState).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. /// @@ -163,38 +197,6 @@ impl Instance { /// [inst]: https://webassembly.github.io/spec/core/exec/modules.html#exec-instantiation /// [issue]: https://github.com/bytecodealliance/wasmtime/issues/727 /// [`ExternType`]: crate::ExternType - 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(), - }) - } - pub fn new(store: &Store, module: &Module, imports: &[Extern]) -> Result { Instance::new_with_user_state(store, module, imports, None) } From 076c0dbdd595d8be00e6567c4dcd3ff791864a8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=BF=97=E5=BC=BA?= <652732310@qq.com> Date: Thu, 8 Oct 2020 20:28:34 +0800 Subject: [PATCH 7/8] fix comment --- crates/wasmtime/src/instance.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/wasmtime/src/instance.rs b/crates/wasmtime/src/instance.rs index 78fb17a9fe3d..920e118c71ba 100644 --- a/crates/wasmtime/src/instance.rs +++ b/crates/wasmtime/src/instance.rs @@ -104,6 +104,7 @@ pub struct Instance { module: Module, } +#[allow(dead_code)] pub struct HostState { frame_info_registration: Option>, pub user_state: Option>, @@ -111,7 +112,7 @@ pub struct HostState { impl Instance { /// allows the caller to specify customimzed user_state, which can be accessed by - /// instance.host_state().downcast_ref(HostState).user_state + /// instance.host_state().downcast_ref::.user_state pub fn new_with_user_state( store: &Store, module: &Module, From 4b65169cb00c3a949155b134328bb68dbf4f3cf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=BF=97=E5=BC=BA?= <652732310@qq.com> Date: Thu, 8 Oct 2020 21:54:42 +0800 Subject: [PATCH 8/8] add missing commit --- crates/wasmtime/src/instance.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/wasmtime/src/instance.rs b/crates/wasmtime/src/instance.rs index 920e118c71ba..58acf6b41e31 100644 --- a/crates/wasmtime/src/instance.rs +++ b/crates/wasmtime/src/instance.rs @@ -104,8 +104,8 @@ pub struct Instance { module: Module, } -#[allow(dead_code)] pub struct HostState { + #[allow(dead_code)] frame_info_registration: Option>, pub user_state: Option>, }