-
Notifications
You must be signed in to change notification settings - Fork 824
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3556 from wasmerio/std-api
Use standard API for js and sys for Module. Added Engine in js
- Loading branch information
Showing
96 changed files
with
4,960 additions
and
8,264 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
use core::ops::Deref; | ||
|
||
#[cfg(feature = "sys")] | ||
use crate::sys::engine as engine_imp; | ||
#[cfg(feature = "sys")] | ||
pub(crate) use crate::sys::engine::default_engine; | ||
|
||
#[cfg(feature = "js")] | ||
use crate::js::engine as engine_imp; | ||
#[cfg(feature = "js")] | ||
pub(crate) use crate::js::engine::default_engine; | ||
|
||
/// The engine type | ||
#[derive(Clone, Debug)] | ||
pub struct Engine(pub(crate) engine_imp::Engine); | ||
|
||
impl Engine { | ||
#[deprecated( | ||
since = "3.2.0", | ||
note = "engine.cloned() has been deprecated in favor of engine.clone()" | ||
)] | ||
/// Returns the [`Engine`]. | ||
pub fn cloned(&self) -> Self { | ||
self.clone() | ||
} | ||
|
||
/// Returns the deterministic id of this engine | ||
pub fn deterministic_id(&self) -> &str { | ||
self.0.deterministic_id() | ||
} | ||
} | ||
|
||
impl AsEngineRef for Engine { | ||
fn as_engine_ref(&self) -> EngineRef { | ||
EngineRef { inner: self } | ||
} | ||
} | ||
|
||
impl Default for Engine { | ||
fn default() -> Self { | ||
Self(default_engine()) | ||
} | ||
} | ||
|
||
impl<T: Into<engine_imp::Engine>> From<T> for Engine { | ||
fn from(t: T) -> Self { | ||
Self(t.into()) | ||
} | ||
} | ||
|
||
/// A temporary handle to an [`Engine`] | ||
/// EngineRef can be used to build a [`Module`][wasmer::Module] | ||
/// It can be created directly with an [`Engine`] | ||
/// Or from anything implementing [`AsEngineRef`] | ||
/// like from [`Store`][wasmer::Store] typicaly. | ||
pub struct EngineRef<'a> { | ||
/// The inner engine | ||
pub(crate) inner: &'a Engine, | ||
} | ||
|
||
impl<'a> EngineRef<'a> { | ||
/// Get inner [`Engine`] | ||
pub fn engine(&self) -> &Engine { | ||
self.inner | ||
} | ||
/// Create an EngineRef from an Engine | ||
pub fn new(engine: &'a Engine) -> Self { | ||
EngineRef { inner: engine } | ||
} | ||
} | ||
|
||
/// Helper trait for a value that is convertible to a [`EngineRef`]. | ||
pub trait AsEngineRef { | ||
/// Returns a `EngineRef` pointing to the underlying context. | ||
fn as_engine_ref(&self) -> EngineRef<'_>; | ||
} | ||
|
||
impl AsEngineRef for EngineRef<'_> { | ||
fn as_engine_ref(&self) -> EngineRef<'_> { | ||
EngineRef { inner: self.inner } | ||
} | ||
} | ||
|
||
impl<P> AsEngineRef for P | ||
where | ||
P: Deref, | ||
P::Target: AsEngineRef, | ||
{ | ||
fn as_engine_ref(&self) -> EngineRef<'_> { | ||
(**self).as_engine_ref() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#[cfg(feature = "js")] | ||
pub use crate::js::errors::{LinkError, RuntimeError}; | ||
use thiserror::Error; | ||
#[cfg(feature = "sys")] | ||
pub use wasmer_compiler::{LinkError, RuntimeError}; | ||
|
||
/// An error while instantiating a module. | ||
/// | ||
/// This is not a common WebAssembly error, however | ||
/// we need to differentiate from a `LinkError` (an error | ||
/// that happens while linking, on instantiation), a | ||
/// Trap that occurs when calling the WebAssembly module | ||
/// start function, and an error when initializing the user's | ||
/// host environments. | ||
#[derive(Debug)] | ||
#[cfg_attr(feature = "std", derive(Error))] | ||
pub enum InstantiationError { | ||
/// A linking ocurred during instantiation. | ||
#[cfg_attr(feature = "std", error(transparent))] | ||
Link(LinkError), | ||
|
||
/// A runtime error occured while invoking the start function | ||
#[cfg_attr(feature = "std", error(transparent))] | ||
Start(RuntimeError), | ||
|
||
/// The module was compiled with a CPU feature that is not available on | ||
/// the current host. | ||
#[cfg_attr(feature = "std", error("missing required CPU features: {0:?}"))] | ||
CpuFeature(String), | ||
|
||
/// Import from a different [`Store`]. | ||
/// This error occurs when an import from a different store is used. | ||
#[cfg_attr(feature = "std", error("cannot mix imports from different stores"))] | ||
DifferentStores, | ||
|
||
/// Import from a different Store. | ||
/// This error occurs when an import from a different store is used. | ||
#[cfg_attr(feature = "std", error("incorrect OS or architecture"))] | ||
DifferentArchOS, | ||
} | ||
|
||
#[cfg(feature = "core")] | ||
impl std::fmt::Display for InstantiationError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "InstantiationError") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use std::any::Any; | ||
|
||
use crate::store::{AsStoreMut, AsStoreRef}; | ||
|
||
#[cfg(feature = "js")] | ||
use crate::js::extern_ref as extern_ref_imp; | ||
#[cfg(feature = "sys")] | ||
use crate::sys::extern_ref as extern_ref_imp; | ||
use crate::vm::VMExternRef; | ||
|
||
#[derive(Debug, Clone)] | ||
#[repr(transparent)] | ||
/// An opaque reference to some data. This reference can be passed through Wasm. | ||
pub struct ExternRef(pub(crate) extern_ref_imp::ExternRef); | ||
|
||
impl ExternRef { | ||
/// Make a new extern reference | ||
pub fn new<T>(store: &mut impl AsStoreMut, value: T) -> Self | ||
where | ||
T: Any + Send + Sync + 'static + Sized, | ||
{ | ||
Self(extern_ref_imp::ExternRef::new(store, value)) | ||
} | ||
|
||
/// Try to downcast to the given value. | ||
pub fn downcast<'a, T>(&self, store: &'a impl AsStoreRef) -> Option<&'a T> | ||
where | ||
T: Any + Send + Sync + 'static + Sized, | ||
{ | ||
self.0.downcast(store) | ||
} | ||
|
||
pub(crate) fn vm_externref(&self) -> VMExternRef { | ||
self.0.vm_externref() | ||
} | ||
|
||
pub(crate) unsafe fn from_vm_externref( | ||
store: &mut impl AsStoreMut, | ||
vm_externref: VMExternRef, | ||
) -> Self { | ||
Self(extern_ref_imp::ExternRef::from_vm_externref( | ||
store, | ||
vm_externref, | ||
)) | ||
} | ||
|
||
/// Checks whether this `ExternRef` can be used with the given context. | ||
/// | ||
/// Primitive (`i32`, `i64`, etc) and null funcref/externref values are not | ||
/// tied to a context and can be freely shared between contexts. | ||
/// | ||
/// Externref and funcref values are tied to a context and can only be used | ||
/// with that context. | ||
pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool { | ||
self.0.is_from_store(store) | ||
} | ||
} |
Oops, something went wrong.