mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-09 14:18:20 +00:00
126 lines
3.4 KiB
Rust
126 lines
3.4 KiB
Rust
pub(crate) mod function;
|
|
mod global;
|
|
mod memory;
|
|
mod table;
|
|
|
|
pub use self::function::{
|
|
FromToNativeWasmType, Function, HostFunction, WasmTypeList, WithEnv, WithoutEnv,
|
|
};
|
|
|
|
#[cfg(feature = "deprecated")]
|
|
pub use self::function::{UnsafeMutableEnv, WithUnsafeMutableEnv};
|
|
pub use self::global::Global;
|
|
pub use self::memory::Memory;
|
|
pub use self::table::Table;
|
|
|
|
use crate::exports::{ExportError, Exportable};
|
|
use crate::store::{Store, StoreObject};
|
|
use crate::ExternType;
|
|
use std::fmt;
|
|
use wasmer_engine::EngineExport;
|
|
|
|
/// An `Extern` is the runtime representation of an entity that
|
|
/// can be imported or exported.
|
|
///
|
|
/// Spec: https://webassembly.github.io/spec/core/exec/runtime.html#external-values
|
|
#[derive(Clone)]
|
|
pub enum Extern {
|
|
/// A external [`Function`].
|
|
Function(Function),
|
|
/// A external [`Global`].
|
|
Global(Global),
|
|
/// A external [`Table`].
|
|
Table(Table),
|
|
/// A external [`Memory`].
|
|
Memory(Memory),
|
|
}
|
|
|
|
impl Extern {
|
|
/// Return the undelying type of the inner `Extern`.
|
|
pub fn ty(&self) -> ExternType {
|
|
match self {
|
|
Self::Function(ft) => ExternType::Function(ft.ty().clone()),
|
|
Self::Memory(ft) => ExternType::Memory(*ft.ty()),
|
|
Self::Table(tt) => ExternType::Table(*tt.ty()),
|
|
Self::Global(gt) => ExternType::Global(*gt.ty()),
|
|
}
|
|
}
|
|
|
|
/// Create an `Extern` from an `Export`.
|
|
pub fn from_export(store: &Store, export: EngineExport) -> Self {
|
|
match export {
|
|
EngineExport::Function(f) => Self::Function(Function::from_export(store, f)),
|
|
EngineExport::Memory(m) => Self::Memory(Memory::from_export(store, m)),
|
|
EngineExport::Global(g) => Self::Global(Global::from_export(store, g)),
|
|
EngineExport::Table(t) => Self::Table(Table::from_export(store, t)),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<'a> Exportable<'a> for Extern {
|
|
fn to_export(&self) -> EngineExport {
|
|
match self {
|
|
Self::Function(f) => f.to_export(),
|
|
Self::Global(g) => g.to_export(),
|
|
Self::Memory(m) => m.to_export(),
|
|
Self::Table(t) => t.to_export(),
|
|
}
|
|
}
|
|
|
|
fn get_self_from_extern(_extern: &'a Self) -> Result<&'a Self, ExportError> {
|
|
// Since this is already an extern, we can just return it.
|
|
Ok(_extern)
|
|
}
|
|
}
|
|
|
|
impl StoreObject for Extern {
|
|
fn comes_from_same_store(&self, store: &Store) -> bool {
|
|
let my_store = match self {
|
|
Self::Function(f) => f.store(),
|
|
Self::Global(g) => g.store(),
|
|
Self::Memory(m) => m.store(),
|
|
Self::Table(t) => t.store(),
|
|
};
|
|
Store::same(my_store, store)
|
|
}
|
|
}
|
|
|
|
impl fmt::Debug for Extern {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
write!(
|
|
f,
|
|
"{}",
|
|
match self {
|
|
Self::Function(_) => "Function(...)",
|
|
Self::Global(_) => "Global(...)",
|
|
Self::Memory(_) => "Memory(...)",
|
|
Self::Table(_) => "Table(...)",
|
|
}
|
|
)
|
|
}
|
|
}
|
|
|
|
impl From<Function> for Extern {
|
|
fn from(r: Function) -> Self {
|
|
Self::Function(r)
|
|
}
|
|
}
|
|
|
|
impl From<Global> for Extern {
|
|
fn from(r: Global) -> Self {
|
|
Self::Global(r)
|
|
}
|
|
}
|
|
|
|
impl From<Memory> for Extern {
|
|
fn from(r: Memory) -> Self {
|
|
Self::Memory(r)
|
|
}
|
|
}
|
|
|
|
impl From<Table> for Extern {
|
|
fn from(r: Table) -> Self {
|
|
Self::Table(r)
|
|
}
|
|
}
|