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

WIP attempt to make an Export wrapper type #1842

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 4 additions & 4 deletions lib/api/src/exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::fmt;
use std::iter::{ExactSizeIterator, FromIterator};
use std::sync::Arc;
use thiserror::Error;
use wasmer_vm::Export;
use wasmer_vm::EngineExport;

/// The `ExportError` can happen when trying to get a specific
/// export [`Extern`] from the [`Instance`] exports.
Expand Down Expand Up @@ -264,11 +264,11 @@ impl FromIterator<(String, Extern)> for Exports {
}

impl LikeNamespace for Exports {
fn get_namespace_export(&self, name: &str) -> Option<Export> {
fn get_namespace_export(&self, name: &str) -> Option<EngineExport> {
self.map.get(name).map(|is_export| is_export.to_export())
}

fn get_namespace_exports(&self) -> Vec<(String, Export)> {
fn get_namespace_exports(&self) -> Vec<(String, EngineExport)> {
self.map
.iter()
.map(|(k, v)| (k.clone(), v.to_export()))
Expand All @@ -284,7 +284,7 @@ pub trait Exportable<'a>: Sized {
/// can be used while instantiating the [`Module`].
///
/// [`Module`]: crate::Module
fn to_export(&self) -> Export;
fn to_export(&self) -> EngineExport;

/// Implementation of how to get the export corresponding to the implementing type
/// from an [`Instance`] by name.
Expand Down
109 changes: 60 additions & 49 deletions lib/api/src/externals/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ pub use inner::{UnsafeMutableEnv, WithUnsafeMutableEnv};
use std::cmp::max;
use std::fmt;
use wasmer_vm::{
raise_user_trap, resume_panic, wasmer_call_trampoline, Export, ExportFunction,
VMCallerCheckedAnyfunc, VMDynamicFunctionContext, VMFunctionBody, VMFunctionEnvironment,
VMFunctionKind, VMTrampoline,
raise_user_trap, resume_panic, wasmer_call_trampoline, EngineExport, EngineExportFunction,
ExportFunction, VMCallerCheckedAnyfunc, VMDynamicFunctionContext, VMFunctionBody,
VMFunctionEnvironment, VMFunctionKind, VMTrampoline,
};

/// A function defined in the Wasm module
Expand Down Expand Up @@ -56,7 +56,7 @@ pub enum FunctionDefinition {
pub struct Function {
pub(crate) store: Store,
pub(crate) definition: FunctionDefinition,
pub(crate) exported: ExportFunction,
pub(crate) exported: EngineExportFunction,
}

impl Function {
Expand Down Expand Up @@ -95,13 +95,15 @@ impl Function {
Self {
store: store.clone(),
definition: FunctionDefinition::Host(HostFunctionDefinition { has_env: false }),
exported: ExportFunction {
address,
kind: VMFunctionKind::Dynamic,
vmctx,
exported: EngineExportFunction {
function_ptr: None,
signature: ty.clone(),
call_trampoline: None,
function: ExportFunction {
address,
kind: VMFunctionKind::Dynamic,
vmctx,
signature: ty.clone(),
call_trampoline: None,
},
},
}
}
Expand Down Expand Up @@ -155,13 +157,15 @@ impl Function {
Self {
store: store.clone(),
definition: FunctionDefinition::Host(HostFunctionDefinition { has_env: true }),
exported: ExportFunction {
address,
kind: VMFunctionKind::Dynamic,
vmctx,
exported: EngineExportFunction {
function_ptr,
signature: ty.clone(),
call_trampoline: None,
function: ExportFunction {
address,
kind: VMFunctionKind::Dynamic,
vmctx,
signature: ty.clone(),
call_trampoline: None,
},
},
}
}
Expand Down Expand Up @@ -200,15 +204,18 @@ impl Function {
Self {
store: store.clone(),
definition: FunctionDefinition::Host(HostFunctionDefinition { has_env: false }),
exported: ExportFunction {
address,
vmctx,
signature,

exported: EngineExportFunction {
// TODO: figure out what's going on in this function: it takes an `Env`
// param but also marks itself as not having an env
function_ptr: None,
kind: VMFunctionKind::Static,
call_trampoline: None,
function: ExportFunction {
address,
vmctx,
signature,
kind: VMFunctionKind::Static,
call_trampoline: None,
},
},
}
}
Expand Down Expand Up @@ -266,13 +273,15 @@ impl Function {
Self {
store: store.clone(),
definition: FunctionDefinition::Host(HostFunctionDefinition { has_env: true }),
exported: ExportFunction {
address,
kind: VMFunctionKind::Static,
vmctx,
exported: EngineExportFunction {
function_ptr,
signature,
call_trampoline: None,
function: ExportFunction {
address,
kind: VMFunctionKind::Static,
vmctx,
signature,
call_trampoline: None,
},
},
}
}
Expand Down Expand Up @@ -313,13 +322,15 @@ impl Function {
Self {
store: store.clone(),
definition: FunctionDefinition::Host(HostFunctionDefinition { has_env: true }),
exported: ExportFunction {
address,
kind: VMFunctionKind::Static,
vmctx,
exported: EngineExportFunction {
function_ptr,
signature,
call_trampoline: None,
function: ExportFunction {
address,
kind: VMFunctionKind::Static,
vmctx,
signature,
call_trampoline: None,
},
},
}
}
Expand All @@ -342,7 +353,7 @@ impl Function {
/// assert_eq!(f.ty().results(), vec![Type::I32]);
/// ```
pub fn ty(&self) -> &FunctionType {
&self.exported.signature
&self.exported.function.signature
}

/// Returns the [`Store`] where the `Function` belongs.
Expand Down Expand Up @@ -399,9 +410,9 @@ impl Function {
// Call the trampoline.
if let Err(error) = unsafe {
wasmer_call_trampoline(
self.exported.vmctx,
self.exported.function.vmctx,
func.trampoline,
self.exported.address,
self.exported.function.address,
values_vec.as_mut_ptr() as *mut u8,
)
} {
Expand Down Expand Up @@ -501,8 +512,8 @@ impl Function {
Ok(results.into_boxed_slice())
}

pub(crate) fn from_export(store: &Store, wasmer_export: ExportFunction) -> Self {
if let Some(trampoline) = wasmer_export.call_trampoline {
pub(crate) fn from_export(store: &Store, wasmer_export: EngineExportFunction) -> Self {
if let Some(trampoline) = wasmer_export.function.call_trampoline {
Self {
store: store.clone(),
definition: FunctionDefinition::Wasm(WasmFunctionDefinition { trampoline }),
Expand All @@ -512,7 +523,7 @@ impl Function {
Self {
store: store.clone(),
definition: FunctionDefinition::Host(HostFunctionDefinition {
has_env: !wasmer_export.vmctx.is_null(),
has_env: !wasmer_export.function.vmctx.is_null(),
}),
exported: wasmer_export,
}
Expand All @@ -523,11 +534,11 @@ impl Function {
let vmsignature = self
.store
.engine()
.register_signature(&self.exported.signature);
.register_signature(&self.exported.function.signature);
VMCallerCheckedAnyfunc {
func_ptr: self.exported.address,
func_ptr: self.exported.function.address,
type_index: vmsignature,
vmctx: self.exported.vmctx,
vmctx: self.exported.function.vmctx,
}
}

Expand Down Expand Up @@ -613,7 +624,7 @@ impl Function {
{
// type check
{
let expected = self.exported.signature.params();
let expected = self.exported.function.signature.params();
let given = Args::wasm_types();

if expected != given {
Expand All @@ -626,7 +637,7 @@ impl Function {
}

{
let expected = self.exported.signature.results();
let expected = self.exported.function.signature.results();
let given = Rets::wasm_types();

if expected != given {
Expand All @@ -641,16 +652,16 @@ impl Function {

Ok(NativeFunc::new(
self.store.clone(),
self.exported.address,
self.exported.vmctx,
self.exported.kind,
self.exported.function.address,
self.exported.function.vmctx,
self.exported.function.kind,
self.definition.clone(),
))
}
}

impl<'a> Exportable<'a> for Function {
fn to_export(&self) -> Export {
fn to_export(&self) -> EngineExport {
self.exported.clone().into()
}

Expand Down
4 changes: 2 additions & 2 deletions lib/api/src/externals/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::Mutability;
use crate::RuntimeError;
use std::fmt;
use std::sync::Arc;
use wasmer_vm::{Export, ExportGlobal, Global as RuntimeGlobal};
use wasmer_vm::{EngineExport, ExportGlobal, Global as RuntimeGlobal};

/// A WebAssembly `global` instance.
///
Expand Down Expand Up @@ -215,7 +215,7 @@ impl fmt::Debug for Global {
}

impl<'a> Exportable<'a> for Global {
fn to_export(&self) -> Export {
fn to_export(&self) -> EngineExport {
ExportGlobal {
from: self.global.clone(),
}
Expand Down
4 changes: 2 additions & 2 deletions lib/api/src/externals/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::convert::TryInto;
use std::slice;
use std::sync::Arc;
use wasmer_types::{Pages, ValueType};
use wasmer_vm::{Export, ExportMemory, Memory as RuntimeMemory, MemoryError};
use wasmer_vm::{EngineExport, ExportMemory, Memory as RuntimeMemory, MemoryError};

/// A WebAssembly `memory` instance.
///
Expand Down Expand Up @@ -245,7 +245,7 @@ impl Memory {
}

impl<'a> Exportable<'a> for Memory {
fn to_export(&self) -> Export {
fn to_export(&self) -> EngineExport {
ExportMemory {
from: self.memory.clone(),
}
Expand Down
4 changes: 2 additions & 2 deletions lib/api/src/externals/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::exports::{ExportError, Exportable};
use crate::store::{Store, StoreObject};
use crate::ExternType;
use std::fmt;
use wasmer_vm::Export;
use wasmer_vm::{EngineExport, Export};

/// An `Extern` is the runtime representation of an entity that
/// can be imported or exported.
Expand Down Expand Up @@ -58,7 +58,7 @@ impl Extern {
}

impl<'a> Exportable<'a> for Extern {
fn to_export(&self) -> Export {
fn to_export(&self) -> EngineExport {
match self {
Self::Function(f) => f.to_export(),
Self::Global(g) => g.to_export(),
Expand Down
4 changes: 2 additions & 2 deletions lib/api/src/externals/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::types::{Val, ValFuncRef};
use crate::RuntimeError;
use crate::TableType;
use std::sync::Arc;
use wasmer_vm::{Export, ExportTable, Table as RuntimeTable, VMCallerCheckedAnyfunc};
use wasmer_vm::{EngineExport, ExportTable, Table as RuntimeTable, VMCallerCheckedAnyfunc};

/// A WebAssembly `table` instance.
///
Expand Down Expand Up @@ -153,7 +153,7 @@ impl Table {
}

impl<'a> Exportable<'a> for Table {
fn to_export(&self) -> Export {
fn to_export(&self) -> EngineExport {
ExportTable {
from: self.table.clone(),
}
Expand Down
Loading