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 + 7e7b930 commit 565618d
Show file tree
Hide file tree
Showing 16 changed files with 182 additions and 179 deletions.
12 changes: 6 additions & 6 deletions lib/api/src/externals/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ 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,
};

Expand Down Expand Up @@ -218,7 +218,7 @@ impl Function {
)
},
)),
vm_function: VMExportFunction {
vm_function: VMFunction {
address,
kind: VMFunctionKind::Dynamic,
vmctx,
Expand Down Expand Up @@ -311,7 +311,7 @@ impl Function {
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 @@ -365,7 +365,7 @@ impl Function {
// 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 @@ -424,7 +424,7 @@ impl Function {
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 @@ -472,7 +472,7 @@ impl Function {
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
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
39 changes: 19 additions & 20 deletions lib/api/src/externals/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use loupe::MemoryUsage;
use std::convert::TryInto;
use std::slice;
use std::sync::Arc;
use wasmer_engine::{Export, ExportMemory};
use wasmer_engine::Export;
use wasmer_types::{Pages, ValueType};
use wasmer_vm::{Memory as RuntimeMemory, MemoryError, VMExportMemory};
use wasmer_vm::{MemoryError, VMMemory};

/// A WebAssembly `memory` instance.
///
Expand All @@ -27,7 +27,7 @@ use wasmer_vm::{Memory as RuntimeMemory, MemoryError, VMExportMemory};
#[derive(Debug, Clone, MemoryUsage)]
pub struct Memory {
store: Store,
memory: Arc<dyn RuntimeMemory>,
vm_memory: VMMemory,
}

impl Memory {
Expand All @@ -51,7 +51,12 @@ impl Memory {

Ok(Self {
store: store.clone(),
memory,
vm_memory: VMMemory {
from: memory,
// We are creating it from the host, and therefore there is no
// associated instance with this memory
instance_ref: None,
},
})
}

Expand All @@ -69,7 +74,7 @@ impl Memory {
/// assert_eq!(m.ty(), &mt);
/// ```
pub fn ty(&self) -> &MemoryType {
self.memory.ty()
self.vm_memory.from.ty()
}

/// Returns the [`Store`] where the `Memory` belongs.
Expand Down Expand Up @@ -110,21 +115,21 @@ impl Memory {
/// by resizing this Memory.
#[allow(clippy::mut_from_ref)]
pub unsafe fn data_unchecked_mut(&self) -> &mut [u8] {
let definition = self.memory.vmmemory();
let definition = self.vm_memory.from.vmmemory();
let def = definition.as_ref();
slice::from_raw_parts_mut(def.base, def.current_length.try_into().unwrap())
}

/// Returns the pointer to the raw bytes of the `Memory`.
pub fn data_ptr(&self) -> *mut u8 {
let definition = self.memory.vmmemory();
let definition = self.vm_memory.from.vmmemory();
let def = unsafe { definition.as_ref() };
def.base
}

/// Returns the size (in bytes) of the `Memory`.
pub fn data_size(&self) -> u64 {
let definition = self.memory.vmmemory();
let definition = self.vm_memory.from.vmmemory();
let def = unsafe { definition.as_ref() };
def.current_length.into()
}
Expand All @@ -142,7 +147,7 @@ impl Memory {
/// assert_eq!(m.size(), Pages(1));
/// ```
pub fn size(&self) -> Pages {
self.memory.size()
self.vm_memory.from.size()
}

/// Grow memory by the specified amount of WebAssembly [`Pages`] and return
Expand Down Expand Up @@ -179,7 +184,7 @@ impl Memory {
where
IntoPages: Into<Pages>,
{
self.memory.grow(delta.into())
self.vm_memory.from.grow(delta.into())
}

/// Return a "view" of the currently accessible memory. By
Expand Down Expand Up @@ -221,10 +226,10 @@ impl Memory {
unsafe { MemoryView::new(base as _, length as u32) }
}

pub(crate) fn from_vm_export(store: &Store, wasmer_export: ExportMemory) -> Self {
pub(crate) fn from_vm_export(store: &Store, vm_memory: VMMemory) -> Self {
Self {
store: store.clone(),
memory: wasmer_export.vm_memory.from,
vm_memory,
}
}

Expand All @@ -241,19 +246,13 @@ impl Memory {
/// assert!(m.same(&m));
/// ```
pub fn same(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.memory, &other.memory)
Arc::ptr_eq(&self.vm_memory.from, &other.vm_memory.from)
}
}

impl<'a> Exportable<'a> for Memory {
fn to_export(&self) -> Export {
ExportMemory {
vm_memory: VMExportMemory {
from: self.memory.clone(),
instance_ref: None,
},
}
.into()
self.vm_memory.clone().into()
}

fn get_self_from_extern(_extern: &'a Extern) -> Result<&'a Self, ExportError> {
Expand Down
40 changes: 19 additions & 21 deletions lib/api/src/externals/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use crate::RuntimeError;
use crate::TableType;
use loupe::MemoryUsage;
use std::sync::Arc;
use wasmer_engine::{Export, ExportTable};
use wasmer_vm::{Table as RuntimeTable, TableElement, VMExportTable};
use wasmer_engine::Export;
use wasmer_vm::{Table as RuntimeTable, TableElement, VMTable};

/// A WebAssembly `table` instance.
///
Expand All @@ -21,7 +21,7 @@ use wasmer_vm::{Table as RuntimeTable, TableElement, VMExportTable};
#[derive(Clone, MemoryUsage)]
pub struct Table {
store: Store,
table: Arc<dyn RuntimeTable>,
vm_table: VMTable,
}

fn set_table_item(
Expand Down Expand Up @@ -54,13 +54,16 @@ impl Table {

Ok(Self {
store: store.clone(),
table,
vm_table: VMTable {
from: table,
instance_ref: None,
},
})
}

/// Returns the [`TableType`] of the `Table`.
pub fn ty(&self) -> &TableType {
self.table.ty()
self.vm_table.from.ty()
}

/// Returns the [`Store`] where the `Table` belongs.
Expand All @@ -70,19 +73,19 @@ impl Table {

/// Retrieves an element of the table at the provided `index`.
pub fn get(&self, index: u32) -> Option<Val> {
let item = self.table.get(index)?;
let item = self.vm_table.from.get(index)?;
Some(ValFuncRef::from_table_reference(item, &self.store))
}

/// Sets an element `val` in the Table at the provided `index`.
pub fn set(&self, index: u32, val: Val) -> Result<(), RuntimeError> {
let item = val.into_table_reference(&self.store)?;
set_table_item(self.table.as_ref(), index, item)
set_table_item(self.vm_table.from.as_ref(), index, item)
}

/// Retrieves the size of the `Table` (in elements)
pub fn size(&self) -> u32 {
self.table.size()
self.vm_table.from.size()
}

/// Grows the size of the `Table` by `delta`, initializating
Expand All @@ -96,7 +99,8 @@ impl Table {
/// Returns an error if the `delta` is out of bounds for the table.
pub fn grow(&self, delta: u32, init: Val) -> Result<u32, RuntimeError> {
let item = init.into_table_reference(&self.store)?;
self.table
self.vm_table
.from
.grow(delta, item)
.ok_or_else(|| RuntimeError::new(format!("failed to grow table by `{}`", delta)))
}
Expand All @@ -121,8 +125,8 @@ impl Table {
));
}
RuntimeTable::copy(
dst_table.table.as_ref(),
src_table.table.as_ref(),
dst_table.vm_table.from.as_ref(),
src_table.vm_table.from.as_ref(),
dst_index,
src_index,
len,
Expand All @@ -131,28 +135,22 @@ impl Table {
Ok(())
}

pub(crate) fn from_vm_export(store: &Store, wasmer_export: ExportTable) -> Self {
pub(crate) fn from_vm_export(store: &Store, vm_table: VMTable) -> Self {
Self {
store: store.clone(),
table: wasmer_export.vm_table.from,
vm_table,
}
}

/// Returns whether or not these two tables refer to the same data.
pub fn same(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.table, &other.table)
Arc::ptr_eq(&self.vm_table.from, &other.vm_table.from)
}
}

impl<'a> Exportable<'a> for Table {
fn to_export(&self) -> Export {
ExportTable {
vm_table: VMExportTable {
from: self.table.clone(),
instance_ref: None,
},
}
.into()
self.vm_table.clone().into()
}

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

0 comments on commit 565618d

Please sign in to comment.