Skip to content

Commit

Permalink
Try #2281:
Browse files Browse the repository at this point in the history
  • Loading branch information
bors[bot] authored May 1, 2021
2 parents 8012f3f + 1b3fa61 commit 18c439f
Show file tree
Hide file tree
Showing 14 changed files with 282 additions and 323 deletions.
84 changes: 17 additions & 67 deletions lib/api/src/externals/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,10 @@ use std::sync::Arc;
use wasmer_engine::{Export, ExportFunction, ExportFunctionMetadata};
use wasmer_vm::{
raise_user_trap, resume_panic, wasmer_call_trampoline, ImportInitializerFuncPtr,
VMCallerCheckedAnyfunc, VMDynamicFunctionContext, VMExportFunction, VMFuncRef, VMFunctionBody,
VMCallerCheckedAnyfunc, VMDynamicFunctionContext, VMFuncRef, VMFunction, VMFunctionBody,
VMFunctionEnvironment, VMFunctionKind, VMTrampoline,
};

/// A function defined in the Wasm module
#[derive(Clone, PartialEq, MemoryUsage)]
pub struct WasmFunctionDefinition {
// Address of the trampoline to do the call.
#[loupe(skip)]
pub(crate) trampoline: VMTrampoline,
}

/// A function defined in the Host
#[derive(Clone, PartialEq, MemoryUsage)]
pub struct HostFunctionDefinition {
/// If the host function has a custom environment attached
pub(crate) has_env: bool,
}

/// The inner helper
#[derive(Clone, PartialEq, MemoryUsage)]
pub enum FunctionDefinition {
/// A function defined in the Wasm side
Wasm(WasmFunctionDefinition),
/// A function defined in the Host side
Host(HostFunctionDefinition),
}

/// A WebAssembly `function` instance.
///
/// A function instance is the runtime representation of a function.
Expand All @@ -66,7 +42,6 @@ pub enum FunctionDefinition {
#[derive(Clone, PartialEq, MemoryUsage)]
pub struct Function {
pub(crate) store: Store,
pub(crate) definition: FunctionDefinition,
pub(crate) exported: ExportFunction,
}

Expand Down Expand Up @@ -203,7 +178,6 @@ impl Function {

Self {
store: store.clone(),
definition: FunctionDefinition::Host(HostFunctionDefinition { has_env: false }),
exported: ExportFunction {
metadata: Some(Arc::new(
// # Safety
Expand All @@ -218,7 +192,7 @@ impl Function {
)
},
)),
vm_function: VMExportFunction {
vm_function: VMFunction {
address,
kind: VMFunctionKind::Dynamic,
vmctx,
Expand Down Expand Up @@ -308,10 +282,9 @@ impl Function {

Self {
store: store.clone(),
definition: FunctionDefinition::Host(HostFunctionDefinition { has_env: true }),
exported: ExportFunction {
metadata: Some(Arc::new(metadata)),
vm_function: VMExportFunction {
vm_function: VMFunction {
address,
kind: VMFunctionKind::Dynamic,
vmctx,
Expand Down Expand Up @@ -359,13 +332,11 @@ impl Function {

Self {
store: store.clone(),
definition: FunctionDefinition::Host(HostFunctionDefinition { has_env: false }),

exported: ExportFunction {
// TODO: figure out what's going on in this function: it takes an `Env`
// param but also marks itself as not having an env
metadata: None,
vm_function: VMExportFunction {
vm_function: VMFunction {
address,
vmctx,
signature,
Expand Down Expand Up @@ -421,10 +392,9 @@ impl Function {

Self {
store: store.clone(),
definition: FunctionDefinition::Host(HostFunctionDefinition { has_env: true }),
exported: ExportFunction {
metadata: Some(Arc::new(metadata)),
vm_function: VMExportFunction {
vm_function: VMFunction {
address,
kind: VMFunctionKind::Static,
vmctx,
Expand Down Expand Up @@ -469,10 +439,9 @@ impl Function {

Self {
store: store.clone(),
definition: FunctionDefinition::Host(HostFunctionDefinition { has_env: true }),
exported: ExportFunction {
metadata: Some(Arc::new(metadata)),
vm_function: VMExportFunction {
vm_function: VMFunction {
address,
kind: VMFunctionKind::Static,
vmctx,
Expand Down Expand Up @@ -512,7 +481,7 @@ impl Function {

fn call_wasm(
&self,
func: &WasmFunctionDefinition,
trampoline: VMTrampoline,
params: &[Val],
results: &mut [Val],
) -> Result<(), RuntimeError> {
Expand Down Expand Up @@ -560,7 +529,7 @@ impl Function {
if let Err(error) = unsafe {
wasmer_call_trampoline(
self.exported.vm_function.vmctx,
func.trampoline,
trampoline,
self.exported.vm_function.address,
values_vec.as_mut_ptr() as *mut u8,
)
Expand Down Expand Up @@ -649,34 +618,19 @@ impl Function {
/// assert_eq!(sum.call(&[Value::I32(1), Value::I32(2)]).unwrap().to_vec(), vec![Value::I32(3)]);
/// ```
pub fn call(&self, params: &[Val]) -> Result<Box<[Val]>, RuntimeError> {
let mut results = vec![Val::null(); self.result_arity()];

match &self.definition {
FunctionDefinition::Wasm(wasm) => {
self.call_wasm(&wasm, params, &mut results)?;
}
// TODO: we can trivially hit this, look into it
_ => unimplemented!("The function definition isn't supported for the moment"),
if let Some(trampoline) = self.exported.vm_function.call_trampoline {
let mut results = vec![Val::null(); self.result_arity()];
self.call_wasm(trampoline, params, &mut results)?;
return Ok(results.into_boxed_slice());
}

Ok(results.into_boxed_slice())
unimplemented!("The function definition isn't supported for the moment");
}

pub(crate) fn from_vm_export(store: &Store, wasmer_export: ExportFunction) -> Self {
if let Some(trampoline) = wasmer_export.vm_function.call_trampoline {
Self {
store: store.clone(),
definition: FunctionDefinition::Wasm(WasmFunctionDefinition { trampoline }),
exported: wasmer_export,
}
} else {
Self {
store: store.clone(),
definition: FunctionDefinition::Host(HostFunctionDefinition {
has_env: !wasmer_export.vm_function.vmctx.is_null(),
}),
exported: wasmer_export,
}
Self {
store: store.clone(),
exported: wasmer_export,
}
}

Expand Down Expand Up @@ -798,11 +752,7 @@ impl Function {
}
}

Ok(NativeFunc::new(
self.store.clone(),
self.exported.clone(),
self.definition.clone(),
))
Ok(NativeFunc::new(self.store.clone(), self.exported.clone()))
}

#[track_caller]
Expand Down
32 changes: 15 additions & 17 deletions lib/api/src/externals/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use crate::RuntimeError;
use loupe::MemoryUsage;
use std::fmt;
use std::sync::Arc;
use wasmer_engine::{Export, ExportGlobal};
use wasmer_vm::{Global as RuntimeGlobal, VMExportGlobal};
use wasmer_engine::Export;
use wasmer_vm::{Global as RuntimeGlobal, VMGlobal};

/// A WebAssembly `global` instance.
///
Expand All @@ -20,7 +20,7 @@ use wasmer_vm::{Global as RuntimeGlobal, VMExportGlobal};
#[derive(Clone, MemoryUsage)]
pub struct Global {
store: Store,
global: Arc<RuntimeGlobal>,
vm_global: VMGlobal,
}

impl Global {
Expand Down Expand Up @@ -75,7 +75,10 @@ impl Global {

Ok(Self {
store: store.clone(),
global: Arc::new(global),
vm_global: VMGlobal {
from: Arc::new(global),
instance_ref: None,
},
})
}

Expand All @@ -94,7 +97,7 @@ impl Global {
/// assert_eq!(v.ty(), &GlobalType::new(Type::I64, Mutability::Var));
/// ```
pub fn ty(&self) -> &GlobalType {
self.global.ty()
self.vm_global.from.ty()
}

/// Returns the [`Store`] where the `Global` belongs.
Expand Down Expand Up @@ -126,7 +129,7 @@ impl Global {
/// assert_eq!(g.get(), Value::I32(1));
/// ```
pub fn get(&self) -> Val {
self.global.get(&self.store)
self.vm_global.from.get(&self.store)
}

/// Sets a custom value [`Val`] to the runtime Global.
Expand Down Expand Up @@ -175,17 +178,18 @@ impl Global {
return Err(RuntimeError::new("cross-`Store` values are not supported"));
}
unsafe {
self.global
self.vm_global
.from
.set(val)
.map_err(|e| RuntimeError::new(format!("{}", e)))?;
}
Ok(())
}

pub(crate) fn from_vm_export(store: &Store, wasmer_export: ExportGlobal) -> Self {
pub(crate) fn from_vm_export(store: &Store, vm_global: VMGlobal) -> Self {
Self {
store: store.clone(),
global: wasmer_export.vm_global.from,
vm_global,
}
}

Expand All @@ -202,7 +206,7 @@ impl Global {
/// assert!(g.same(&g));
/// ```
pub fn same(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.global, &other.global)
Arc::ptr_eq(&self.vm_global.from, &other.vm_global.from)
}
}

Expand All @@ -218,13 +222,7 @@ impl fmt::Debug for Global {

impl<'a> Exportable<'a> for Global {
fn to_export(&self) -> Export {
ExportGlobal {
vm_global: VMExportGlobal {
from: self.global.clone(),
instance_ref: None,
},
}
.into()
self.vm_global.clone().into()
}

fn get_self_from_extern(_extern: &'a Extern) -> Result<&'a Self, ExportError> {
Expand Down
Loading

0 comments on commit 18c439f

Please sign in to comment.