Skip to content

Commit

Permalink
Merge #2569
Browse files Browse the repository at this point in the history
2569: refactor(api): add Send + Sync trait when creating an instance r=Amanieu a=bnjjj

# Description

I'm currently implementing wasmer inside Krustlet (krustlet/krustlet#677). Then I had to force the thread safety of trait object used to create a wasmer instance. I know it could be a breaking change and maybe I miss some backgrounds about this. So feel free to let me know what do you think and how we could have a better implementation if needed. I'm open to any suggestions.


Co-authored-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com>
  • Loading branch information
bors[bot] and bnjjj committed Oct 6, 2021
2 parents 68bd649 + 064154f commit 823c38c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 21 deletions.
4 changes: 2 additions & 2 deletions lib/api/src/sys/import_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub trait LikeNamespace {
/// ```
#[derive(Clone, Default)]
pub struct ImportObject {
map: Arc<Mutex<HashMap<String, Box<dyn LikeNamespace>>>>,
map: Arc<Mutex<HashMap<String, Box<dyn LikeNamespace + Send + Sync>>>>,
}

impl ImportObject {
Expand Down Expand Up @@ -87,7 +87,7 @@ impl ImportObject {
pub fn register<S, N>(&mut self, name: S, namespace: N) -> Option<Box<dyn LikeNamespace>>
where
S: Into<String>,
N: LikeNamespace + 'static,
N: LikeNamespace + Send + Sync + 'static,
{
let mut guard = self.map.lock().unwrap();
let map = guard.borrow_mut();
Expand Down
5 changes: 4 additions & 1 deletion lib/api/src/sys/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ impl Instance {
/// Those are, as defined by the spec:
/// * Link errors that happen when plugging the imports into the instance
/// * Runtime errors that happen when running the module `start` function.
pub fn new(module: &Module, resolver: &dyn Resolver) -> Result<Self, InstantiationError> {
pub fn new(
module: &Module,
resolver: &(dyn Resolver + Send + Sync),
) -> Result<Self, InstantiationError> {
let store = module.store();
let handle = module.instantiate(resolver)?;
let exports = module
Expand Down
32 changes: 16 additions & 16 deletions lib/engine/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl<T: NamedResolver> NamedResolver for &T {
}
}

impl NamedResolver for Box<dyn NamedResolver> {
impl NamedResolver for Box<dyn NamedResolver + Send + Sync> {
fn resolve_by_name(&self, module: &str, field: &str) -> Option<Export> {
(**self).resolve_by_name(module, field)
}
Expand Down Expand Up @@ -293,7 +293,7 @@ pub fn resolve_imports(
}

/// A [`Resolver`] that links two resolvers together in a chain.
pub struct NamedResolverChain<A: NamedResolver, B: NamedResolver> {
pub struct NamedResolverChain<A: NamedResolver + Send + Sync, B: NamedResolver + Send + Sync> {
a: A,
b: B,
}
Expand All @@ -303,31 +303,31 @@ pub struct NamedResolverChain<A: NamedResolver, B: NamedResolver> {
/// ```
/// # use wasmer_engine::{ChainableNamedResolver, NamedResolver};
/// # fn chainable_test<A, B>(imports1: A, imports2: B)
/// # where A: NamedResolver + Sized,
/// # B: NamedResolver + Sized,
/// # where A: NamedResolver + Sized + Send + Sync,
/// # B: NamedResolver + Sized + Send + Sync,
/// # {
/// // override duplicates with imports from `imports2`
/// imports1.chain_front(imports2);
/// # }
/// ```
pub trait ChainableNamedResolver: NamedResolver + Sized {
pub trait ChainableNamedResolver: NamedResolver + Sized + Send + Sync {
/// Chain a resolver in front of the current resolver.
///
/// This will cause the second resolver to override the first.
///
/// ```
/// # use wasmer_engine::{ChainableNamedResolver, NamedResolver};
/// # fn chainable_test<A, B>(imports1: A, imports2: B)
/// # where A: NamedResolver + Sized,
/// # B: NamedResolver + Sized,
/// # where A: NamedResolver + Sized + Send + Sync,
/// # B: NamedResolver + Sized + Send + Sync,
/// # {
/// // override duplicates with imports from `imports2`
/// imports1.chain_front(imports2);
/// # }
/// ```
fn chain_front<U>(self, other: U) -> NamedResolverChain<U, Self>
where
U: NamedResolver,
U: NamedResolver + Send + Sync,
{
NamedResolverChain { a: other, b: self }
}
Expand All @@ -339,28 +339,28 @@ pub trait ChainableNamedResolver: NamedResolver + Sized {
/// ```
/// # use wasmer_engine::{ChainableNamedResolver, NamedResolver};
/// # fn chainable_test<A, B>(imports1: A, imports2: B)
/// # where A: NamedResolver + Sized,
/// # B: NamedResolver + Sized,
/// # where A: NamedResolver + Sized + Send + Sync,
/// # B: NamedResolver + Sized + Send + Sync,
/// # {
/// // override duplicates with imports from `imports1`
/// imports1.chain_back(imports2);
/// # }
/// ```
fn chain_back<U>(self, other: U) -> NamedResolverChain<Self, U>
where
U: NamedResolver,
U: NamedResolver + Send + Sync,
{
NamedResolverChain { a: self, b: other }
}
}

// We give these chain methods to all types implementing NamedResolver
impl<T: NamedResolver> ChainableNamedResolver for T {}
impl<T: NamedResolver + Send + Sync> ChainableNamedResolver for T {}

impl<A, B> NamedResolver for NamedResolverChain<A, B>
where
A: NamedResolver,
B: NamedResolver,
A: NamedResolver + Send + Sync,
B: NamedResolver + Send + Sync,
{
fn resolve_by_name(&self, module: &str, field: &str) -> Option<Export> {
self.a
Expand All @@ -371,8 +371,8 @@ where

impl<A, B> Clone for NamedResolverChain<A, B>
where
A: NamedResolver + Clone,
B: NamedResolver + Clone,
A: NamedResolver + Clone + Send + Sync,
B: NamedResolver + Clone + Send + Sync,
{
fn clone(&self) -> Self {
Self {
Expand Down
5 changes: 3 additions & 2 deletions lib/wasi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,12 @@ impl WasiEnv {
pub fn import_object_for_all_wasi_versions(
&mut self,
module: &Module,
) -> Result<Box<dyn NamedResolver>, WasiError> {
) -> Result<Box<dyn NamedResolver + Send + Sync>, WasiError> {
let wasi_versions =
get_wasi_versions(module, false).ok_or(WasiError::UnknownWasiVersion)?;

let mut resolver: Box<dyn NamedResolver> = { Box::new(()) };
let mut resolver: Box<dyn NamedResolver + Send + Sync> =
{ Box::new(()) as Box<dyn NamedResolver + Send + Sync> };
for version in wasi_versions.iter() {
let new_import_object =
generate_import_object_from_env(module.store(), self.clone(), *version);
Expand Down
Binary file modified tests/wasi-wast/wasi/snapshot1/fd_rename_path.wasm
Binary file not shown.

0 comments on commit 823c38c

Please sign in to comment.