Port JS API to new Context API

This commit is contained in:
Manos Pitsidianakis
2022-06-21 16:27:06 +03:00
parent 168e330260
commit 6c089bbc45
34 changed files with 2515 additions and 1621 deletions

View File

@@ -1,19 +1,18 @@
pub(crate) mod function;
mod global;
mod memory;
pub(crate) mod memory;
mod table;
pub use self::function::{
FromToNativeWasmType, Function, HostFunction, WasmTypeList, WithEnv, WithoutEnv,
};
pub use self::function::{FromToNativeWasmType, Function, HostFunction, WasmTypeList};
pub use self::global::Global;
pub use self::memory::{Memory, MemoryError};
pub use self::table::Table;
use crate::js::context::{AsContextMut, AsContextRef};
use crate::js::export::Export;
use crate::js::exports::{ExportError, Exportable};
use crate::js::store::{Store, StoreObject};
use crate::js::store::StoreObject;
use crate::js::types::AsJs;
use crate::js::ExternType;
use std::fmt;
@@ -35,36 +34,58 @@ pub enum Extern {
impl Extern {
/// Return the underlying type of the inner `Extern`.
pub fn ty(&self) -> ExternType {
pub fn ty(&self, ctx: &impl AsContextRef) -> 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()),
Self::Function(ft) => ExternType::Function(ft.ty(ctx).clone()),
Self::Memory(ft) => ExternType::Memory(ft.ty(ctx)),
Self::Table(tt) => ExternType::Table(tt.ty(ctx)),
Self::Global(gt) => ExternType::Global(gt.ty(ctx)),
}
}
/// Create an `Extern` from an `wasmer_compiler::Export`.
pub fn from_vm_export(store: &Store, export: Export) -> Self {
pub fn from_vm_export(ctx: &mut impl AsContextMut, export: Export) -> Self {
match export {
Export::Function(f) => Self::Function(Function::from_vm_export(store, f)),
Export::Memory(m) => Self::Memory(Memory::from_vm_export(store, m)),
Export::Global(g) => Self::Global(Global::from_vm_export(store, g)),
Export::Table(t) => Self::Table(Table::from_vm_export(store, t)),
Export::Function(f) => Self::Function(Function::from_vm_extern(ctx, f)),
Export::Memory(m) => Self::Memory(Memory::from_vm_extern(ctx, m)),
Export::Global(g) => Self::Global(Global::from_vm_extern(ctx, g)),
Export::Table(t) => Self::Table(Table::from_vm_extern(ctx, t)),
}
}
/// Checks whether this `Extern` can be used with the given context.
pub fn is_from_context(&self, ctx: &impl AsContextRef) -> bool {
match self {
Self::Function(val) => val.is_from_context(ctx),
Self::Memory(val) => val.is_from_context(ctx),
Self::Global(val) => val.is_from_context(ctx),
Self::Table(val) => val.is_from_context(ctx),
}
}
fn to_export(&self) -> Export {
match self {
Self::Function(val) => Export::Function(val.handle.internal_handle()),
Self::Memory(val) => Export::Memory(val.handle.internal_handle()),
Self::Global(val) => Export::Global(val.handle.internal_handle()),
Self::Table(val) => Export::Table(val.handle.internal_handle()),
}
}
}
impl<'a> Exportable<'a> for Extern {
fn to_export(&self) -> Export {
impl AsJs for Extern {
fn as_jsvalue(&self, ctx: &impl AsContextRef) -> wasm_bindgen::JsValue {
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(),
Self::Function(_) => self.to_export().as_jsvalue(ctx),
Self::Global(_) => self.to_export().as_jsvalue(ctx),
Self::Table(_) => self.to_export().as_jsvalue(ctx),
Self::Memory(_) => self.to_export().as_jsvalue(ctx),
}
.clone()
}
}
impl<'a> Exportable<'a> for Extern {
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)