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

add new_with_user_state #2282

Closed
Closed
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion crates/runtime/src/externref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
24 changes: 22 additions & 2 deletions crates/wasmtime/src/instance.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -102,6 +104,11 @@ pub struct Instance {
module: Module,
}

pub struct HostState {
frame_info_registration: Box<Option<Arc<GlobalFrameInfoRegistration>>>,
pub user_state: Option<dyn Any>,
}

impl Instance {
/// Creates a new [`Instance`] from the previously compiled [`Module`] and
/// list of `imports` specified.
Expand Down Expand Up @@ -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<Instance, Error> {
fn new_with_user_state(
store: &Store,
module: &Module,
imports: &[Extern],
user_state: Option<dyn Any>,
) -> Result<Instance, Error> {
if !Engine::same(store.engine(), module.engine()) {
bail!("cross-`Engine` instantiation is not currently supported");
}
Expand All @@ -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| {
Expand All @@ -179,6 +195,10 @@ impl Instance {
})
}

pub fn new(store: &Store, module: &Module, imports: &[Extern]) -> Result<Instance, Error> {
return new_with_user_state(store, module, imports, None);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return new_with_user_state(store, module, imports, None);
Instance::new_with_user_state(store, module, imports, None)
  1. You can't use new_with_user_state directly as it isn't imported in the current module.
  2. Please prefer implicit return at the end of a block/function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

}

/// Returns the associated [`Store`] that this `Instance` is compiled into.
///
/// This is the [`Store`] that generally serves as a sort of global cache
Expand Down