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 all commits
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
2 changes: 1 addition & 1 deletion 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_engine::Export;

/// The `ExportError` can happen when trying to get a specific
/// export [`Extern`] from the [`Instance`] exports.
Expand Down
108 changes: 60 additions & 48 deletions lib/api/src/externals/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ pub use inner::{UnsafeMutableEnv, WithUnsafeMutableEnv};

use std::cmp::max;
use std::fmt;
use wasmer_engine::{Export, ExportFunction};
use wasmer_vm::{
raise_user_trap, resume_panic, wasmer_call_trampoline, Export, ExportFunction,
VMCallerCheckedAnyfunc, VMDynamicFunctionContext, VMFunctionBody, VMFunctionEnvironment,
raise_user_trap, resume_panic, wasmer_call_trampoline, VMCallerCheckedAnyfunc,
VMDynamicFunctionContext, VMExportFunction, VMFunctionBody, VMFunctionEnvironment,
VMFunctionKind, VMTrampoline,
};

Expand Down Expand Up @@ -96,12 +97,14 @@ impl Function {
store: store.clone(),
definition: FunctionDefinition::Host(HostFunctionDefinition { has_env: false }),
exported: ExportFunction {
address,
kind: VMFunctionKind::Dynamic,
vmctx,
function_ptr: None,
signature: ty.clone(),
call_trampoline: None,
import_init_function_ptr: None,
vm_function: VMExportFunction {
address,
kind: VMFunctionKind::Dynamic,
vmctx,
signature: ty.clone(),
call_trampoline: None,
},
},
}
}
Expand Down Expand Up @@ -146,7 +149,7 @@ impl Function {
host_env: Box::into_raw(Box::new(dynamic_ctx)) as *mut _,
};
// TODO: look into removing transmute by changing API type signatures
let function_ptr = Some(unsafe {
let import_init_function_ptr = Some(unsafe {
std::mem::transmute::<fn(_, _) -> Result<(), _>, fn(_, _) -> Result<(), _>>(
Env::init_with_instance,
)
Expand All @@ -156,12 +159,14 @@ impl Function {
store: store.clone(),
definition: FunctionDefinition::Host(HostFunctionDefinition { has_env: true }),
exported: ExportFunction {
address,
kind: VMFunctionKind::Dynamic,
vmctx,
function_ptr,
signature: ty.clone(),
call_trampoline: None,
import_init_function_ptr,
vm_function: VMExportFunction {
address,
kind: VMFunctionKind::Dynamic,
vmctx,
signature: ty.clone(),
call_trampoline: None,
},
},
}
}
Expand Down Expand Up @@ -200,15 +205,18 @@ impl Function {
Self {
store: store.clone(),
definition: FunctionDefinition::Host(HostFunctionDefinition { has_env: false }),

exported: ExportFunction {
address,
vmctx,
signature,
// 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,
import_init_function_ptr: None,
vm_function: VMExportFunction {
address,
vmctx,
signature,
kind: VMFunctionKind::Static,
call_trampoline: None,
},
},
}
}
Expand Down Expand Up @@ -256,7 +264,7 @@ impl Function {
host_env: Box::into_raw(box_env) as *mut _,
};
// TODO: look into removing transmute by changing API type signatures
let function_ptr = Some(unsafe {
let import_init_function_ptr = Some(unsafe {
std::mem::transmute::<fn(_, _) -> Result<(), _>, fn(_, _) -> Result<(), _>>(
Env::init_with_instance,
)
Expand All @@ -267,12 +275,14 @@ impl Function {
store: store.clone(),
definition: FunctionDefinition::Host(HostFunctionDefinition { has_env: true }),
exported: ExportFunction {
address,
kind: VMFunctionKind::Static,
vmctx,
function_ptr,
signature,
call_trampoline: None,
import_init_function_ptr,
vm_function: VMExportFunction {
address,
kind: VMFunctionKind::Static,
vmctx,
signature,
call_trampoline: None,
},
},
}
}
Expand Down Expand Up @@ -305,7 +315,7 @@ impl Function {
};
let signature = function.ty();
// TODO: look into removing transmute by changing API type signatures
let function_ptr = Some(std::mem::transmute::<
let import_init_function_ptr = Some(std::mem::transmute::<
fn(_, _) -> Result<(), _>,
fn(_, _) -> Result<(), _>,
>(Env::init_with_instance));
Expand All @@ -314,12 +324,14 @@ impl Function {
store: store.clone(),
definition: FunctionDefinition::Host(HostFunctionDefinition { has_env: true }),
exported: ExportFunction {
address,
kind: VMFunctionKind::Static,
vmctx,
function_ptr,
signature,
call_trampoline: None,
import_init_function_ptr,
vm_function: VMExportFunction {
address,
kind: VMFunctionKind::Static,
vmctx,
signature,
call_trampoline: None,
},
},
}
}
Expand All @@ -342,7 +354,7 @@ impl Function {
/// assert_eq!(f.ty().results(), vec![Type::I32]);
/// ```
pub fn ty(&self) -> &FunctionType {
&self.exported.signature
&self.exported.vm_function.signature
}

/// Returns the [`Store`] where the `Function` belongs.
Expand Down Expand Up @@ -399,9 +411,9 @@ impl Function {
// Call the trampoline.
if let Err(error) = unsafe {
wasmer_call_trampoline(
self.exported.vmctx,
self.exported.vm_function.vmctx,
func.trampoline,
self.exported.address,
self.exported.vm_function.address,
values_vec.as_mut_ptr() as *mut u8,
)
} {
Expand Down Expand Up @@ -502,7 +514,7 @@ impl Function {
}

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

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

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

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

if expected != given {
Expand All @@ -641,9 +653,9 @@ impl Function {

Ok(NativeFunc::new(
self.store.clone(),
self.exported.address,
self.exported.vmctx,
self.exported.kind,
self.exported.vm_function.address,
self.exported.vm_function.vmctx,
self.exported.vm_function.kind,
self.definition.clone(),
))
}
Expand Down
9 changes: 6 additions & 3 deletions lib/api/src/externals/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use crate::Mutability;
use crate::RuntimeError;
use std::fmt;
use std::sync::Arc;
use wasmer_vm::{Export, ExportGlobal, Global as RuntimeGlobal};
use wasmer_engine::{Export, ExportGlobal};
use wasmer_vm::{Global as RuntimeGlobal, VMExportGlobal};

/// A WebAssembly `global` instance.
///
Expand Down Expand Up @@ -183,7 +184,7 @@ impl Global {
pub(crate) fn from_export(store: &Store, wasmer_export: ExportGlobal) -> Self {
Self {
store: store.clone(),
global: wasmer_export.from,
global: wasmer_export.vm_global.from,
}
}

Expand Down Expand Up @@ -217,7 +218,9 @@ impl fmt::Debug for Global {
impl<'a> Exportable<'a> for Global {
fn to_export(&self) -> Export {
ExportGlobal {
from: self.global.clone(),
vm_global: VMExportGlobal {
from: self.global.clone(),
},
}
.into()
}
Expand Down
9 changes: 6 additions & 3 deletions lib/api/src/externals/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ use crate::{MemoryType, MemoryView};
use std::convert::TryInto;
use std::slice;
use std::sync::Arc;
use wasmer_engine::{Export, ExportMemory};
use wasmer_types::{Pages, ValueType};
use wasmer_vm::{Export, ExportMemory, Memory as RuntimeMemory, MemoryError};
use wasmer_vm::{Memory as RuntimeMemory, MemoryError, VMExportMemory};

/// A WebAssembly `memory` instance.
///
Expand Down Expand Up @@ -223,7 +224,7 @@ impl Memory {
pub(crate) fn from_export(store: &Store, wasmer_export: ExportMemory) -> Self {
Self {
store: store.clone(),
memory: wasmer_export.from,
memory: wasmer_export.vm_memory.from,
}
}

Expand All @@ -247,7 +248,9 @@ impl Memory {
impl<'a> Exportable<'a> for Memory {
fn to_export(&self) -> Export {
ExportMemory {
from: self.memory.clone(),
vm_memory: VMExportMemory {
from: self.memory.clone(),
},
}
.into()
}
Expand Down
2 changes: 1 addition & 1 deletion 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_engine::Export;

/// An `Extern` is the runtime representation of an entity that
/// can be imported or exported.
Expand Down
9 changes: 6 additions & 3 deletions lib/api/src/externals/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ 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_engine::{Export, ExportTable};
use wasmer_vm::{Table as RuntimeTable, VMCallerCheckedAnyfunc, VMExportTable};

/// A WebAssembly `table` instance.
///
Expand Down Expand Up @@ -142,7 +143,7 @@ impl Table {
pub(crate) fn from_export(store: &Store, wasmer_export: ExportTable) -> Self {
Self {
store: store.clone(),
table: wasmer_export.from,
table: wasmer_export.vm_table.from,
}
}

Expand All @@ -155,7 +156,9 @@ impl Table {
impl<'a> Exportable<'a> for Table {
fn to_export(&self) -> Export {
ExportTable {
from: self.table.clone(),
vm_table: VMExportTable {
from: self.table.clone(),
},
}
.into()
}
Expand Down
10 changes: 4 additions & 6 deletions lib/api/src/import_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use std::collections::VecDeque;
use std::collections::{hash_map::Entry, HashMap};
use std::fmt;
use std::sync::{Arc, Mutex};
use wasmer_engine::NamedResolver;
use wasmer_vm::Export;
use wasmer_engine::{Export, NamedResolver};

/// The `LikeNamespace` trait represents objects that act as a namespace for imports.
/// For example, an `Instance` or `Namespace` could be
Expand Down Expand Up @@ -265,7 +264,6 @@ mod test {
use crate::{Global, Store, Val};
use wasmer_engine::ChainableNamedResolver;
use wasmer_types::Type;
use wasmer_vm::Export;

#[test]
fn chaining_works() {
Expand Down Expand Up @@ -320,7 +318,7 @@ mod test {
let happy_dog_entry = resolver.resolve_by_name("dog", "happy").unwrap();

assert!(if let Export::Global(happy_dog_global) = happy_dog_entry {
happy_dog_global.from.ty().ty == Type::I64
happy_dog_global.vm_global.from.ty().ty == Type::I64
} else {
false
});
Expand All @@ -346,7 +344,7 @@ mod test {
let happy_dog_entry = resolver.resolve_by_name("dog", "happy").unwrap();

assert!(if let Export::Global(happy_dog_global) = happy_dog_entry {
happy_dog_global.from.ty().ty == Type::I32
happy_dog_global.vm_global.from.ty().ty == Type::I32
} else {
false
});
Expand All @@ -366,7 +364,7 @@ mod test {
let happy_dog_entry = imports1.resolve_by_name("dog", "happy").unwrap();

assert!(if let Export::Global(happy_dog_global) = happy_dog_entry {
happy_dog_global.from.ty().ty == Type::I32
happy_dog_global.vm_global.from.ty().ty == Type::I32
} else {
false
});
Expand Down
2 changes: 1 addition & 1 deletion lib/api/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl Instance {
.map(|export| {
let name = export.name().to_string();
let export = handle.lookup(&name).expect("export");
let extern_ = Extern::from_export(store, export);
let extern_ = Extern::from_export(store, export.into());
(name, extern_)
})
.collect::<Exports>();
Expand Down
Loading