More review fixes

This commit is contained in:
Lachlan Sneff 2019-01-29 13:04:42 -08:00
parent 767fdbd950
commit 581363119b
15 changed files with 76 additions and 80 deletions

View File

@ -11,8 +11,8 @@ use wasmer_runtime_core::{
module::{DataInitializer, ExportIndex, ImportName, TableInitializer},
structures::{Map, TypedIndex},
types::{
ElementType, FuncSig, GlobalDesc, GlobalIndex, GlobalInit, Initializer, LocalFuncIndex,
LocalOrImport, MemoryDesc, SigIndex, TableDesc, Value,
ElementType, FuncSig, GlobalDescriptor, GlobalIndex, GlobalInit, Initializer,
LocalFuncIndex, LocalOrImport, MemoryDescriptor, SigIndex, TableDescriptor, Value,
},
};
@ -106,7 +106,7 @@ impl<'module, 'isa, 'data> ModuleEnvironment<'data> for ModuleEnv<'module, 'isa>
/// Declares a global to the environment.
fn declare_global(&mut self, global: cranelift_wasm::Global) {
let desc = GlobalDesc {
let desc = GlobalDescriptor {
mutable: global.mutability,
ty: Converter(global.ty).into(),
};
@ -155,7 +155,7 @@ impl<'module, 'isa, 'data> ModuleEnvironment<'data> for ModuleEnv<'module, 'isa>
name: name.to_string(),
};
let desc = GlobalDesc {
let desc = GlobalDescriptor {
mutable: global.mutability,
ty: Converter(global.ty).into(),
};
@ -175,8 +175,8 @@ impl<'module, 'isa, 'data> ModuleEnvironment<'data> for ModuleEnv<'module, 'isa>
fn declare_table(&mut self, table: cranelift_wasm::Table) {
use cranelift_wasm::TableElementType;
// Add table ir to the list of tables
self.module.tables.push(TableDesc {
ty: match table.ty {
self.module.tables.push(TableDescriptor {
element: match table.ty {
TableElementType::Func => ElementType::Anyfunc,
_ => unimplemented!(),
},
@ -199,8 +199,8 @@ impl<'module, 'isa, 'data> ModuleEnvironment<'data> for ModuleEnv<'module, 'isa>
name: name.to_string(),
};
let imported_table = TableDesc {
ty: match table.ty {
let imported_table = TableDescriptor {
element: match table.ty {
TableElementType::Func => ElementType::Anyfunc,
_ => unimplemented!(),
},
@ -250,7 +250,7 @@ impl<'module, 'isa, 'data> ModuleEnvironment<'data> for ModuleEnv<'module, 'isa>
/// Declares a memory to the environment
fn declare_memory(&mut self, memory: cranelift_wasm::Memory) {
self.module.memories.push(MemoryDesc {
self.module.memories.push(MemoryDescriptor {
min: memory.minimum,
max: memory.maximum,
shared: memory.shared,
@ -269,7 +269,7 @@ impl<'module, 'isa, 'data> ModuleEnvironment<'data> for ModuleEnv<'module, 'isa>
name: name.to_string(),
};
let memory = MemoryDesc {
let memory = MemoryDescriptor {
min: memory.minimum,
max: memory.maximum,
shared: memory.shared,

View File

@ -10,7 +10,7 @@ use wasmer_runtime_core::{
import::{ImportObject, Namespace},
memory::Memory,
types::{
FuncSig, GlobalDesc,
FuncSig, GlobalDescriptor,
Type::{self, *},
},
vm::LocalGlobal,

View File

@ -6,7 +6,7 @@ use wasmer_runtime_core::{
memory::Memory,
prelude::*,
table::Table,
types::{ElementType, MemoryDesc, TableDesc, Value},
types::{ElementType, MemoryDescriptor, TableDescriptor, Value},
};
static EXAMPLE_WASM: &'static [u8] = include_bytes!("simple.wasm");
@ -15,7 +15,7 @@ fn main() -> Result<()> {
let wasm_binary = wat2wasm(IMPORT_MODULE.as_bytes()).expect("WAST not valid or malformed");
let inner_module = wasmer_runtime_core::compile_with(&wasm_binary, &CraneliftCompiler::new())?;
let memory = Memory::new(MemoryDesc {
let memory = Memory::new(MemoryDescriptor {
min: 1,
max: Some(1),
shared: false,
@ -24,8 +24,8 @@ fn main() -> Result<()> {
let global = Global::new(Value::I32(42));
let table = Table::new(TableDesc {
ty: ElementType::Anyfunc,
let table = Table::new(TableDescriptor {
element: ElementType::Anyfunc,
min: 10,
max: None,
})

View File

@ -434,15 +434,15 @@ fn import_memories(
.and_then(|namespace| namespace.get_export(&name));
match memory_import {
Some(Export::Memory(mut memory)) => {
if expected_memory_desc.fits_in_imported(memory.description()) {
if expected_memory_desc.fits_in_imported(memory.descriptor()) {
memories.push(memory.clone());
vm_memories.push(memory.vm_local_memory());
} else {
link_errors.push(LinkError::IncorrectMemoryDescription {
link_errors.push(LinkError::IncorrectMemoryDescriptor {
namespace: namespace.clone(),
name: name.clone(),
expected: *expected_memory_desc,
found: memory.description(),
found: memory.descriptor(),
});
}
}
@ -493,15 +493,15 @@ fn import_tables(
.and_then(|namespace| namespace.get_export(&name));
match table_import {
Some(Export::Table(mut table)) => {
if expected_table_desc.fits_in_imported(table.description()) {
if expected_table_desc.fits_in_imported(table.descriptor()) {
vm_tables.push(table.vm_local_table());
tables.push(table);
} else {
link_errors.push(LinkError::IncorrectTableDescription {
link_errors.push(LinkError::IncorrectTableDescriptor {
namespace: namespace.clone(),
name: name.clone(),
expected: *expected_table_desc,
found: table.description(),
found: table.descriptor(),
});
}
}
@ -552,15 +552,15 @@ fn import_globals(
.and_then(|namespace| namespace.get_export(name));
match import {
Some(Export::Global(mut global)) => {
if global.description() == *imported_global_desc {
if global.descriptor() == *imported_global_desc {
vm_globals.push(global.vm_local_global());
globals.push(global);
} else {
link_errors.push(LinkError::IncorrectGlobalDescription {
link_errors.push(LinkError::IncorrectGlobalDescriptor {
namespace: namespace.clone(),
name: name.clone(),
expected: *imported_global_desc,
found: global.description(),
found: global.descriptor(),
});
}
}

View File

@ -1,4 +1,6 @@
use crate::types::{FuncSig, GlobalDesc, MemoryDesc, MemoryIndex, TableDesc, TableIndex, Type};
use crate::types::{
FuncSig, GlobalDescriptor, MemoryDescriptor, MemoryIndex, TableDescriptor, TableIndex, Type,
};
use std::sync::Arc;
pub type Result<T> = std::result::Result<T, Box<Error>>;
@ -47,23 +49,23 @@ pub enum LinkError {
namespace: String,
name: String,
},
IncorrectMemoryDescription {
IncorrectMemoryDescriptor {
namespace: String,
name: String,
expected: MemoryDesc,
found: MemoryDesc,
expected: MemoryDescriptor,
found: MemoryDescriptor,
},
IncorrectTableDescription {
IncorrectTableDescriptor {
namespace: String,
name: String,
expected: TableDesc,
found: TableDesc,
expected: TableDescriptor,
found: TableDescriptor,
},
IncorrectGlobalDescription {
IncorrectGlobalDescriptor {
namespace: String,
name: String,
expected: GlobalDesc,
found: GlobalDesc,
expected: GlobalDescriptor,
found: GlobalDescriptor,
},
}

View File

@ -1,13 +1,13 @@
use crate::{
export::Export,
import::IsExport,
types::{GlobalDesc, Type, Value},
types::{GlobalDescriptor, Type, Value},
vm,
};
use std::{cell::RefCell, fmt, rc::Rc};
pub struct Global {
desc: GlobalDesc,
desc: GlobalDescriptor,
storage: Rc<RefCell<vm::LocalGlobal>>,
}
@ -21,7 +21,7 @@ impl Global {
}
fn new_internal(value: Value, mutable: bool) -> Self {
let desc = GlobalDesc {
let desc = GlobalDescriptor {
mutable,
ty: value.ty(),
};
@ -41,7 +41,7 @@ impl Global {
}
}
pub fn description(&self) -> GlobalDesc {
pub fn descriptor(&self) -> GlobalDescriptor {
self.desc
}

View File

@ -1,7 +1,7 @@
use crate::{
memory::{WASM_MAX_PAGES, WASM_PAGE_SIZE},
sys,
types::MemoryDesc,
types::MemoryDescriptor,
vm,
};
@ -27,7 +27,7 @@ pub struct DynamicMemory {
}
impl DynamicMemory {
pub(super) fn new(desc: MemoryDesc, local: &mut vm::LocalMemory) -> Option<Box<Self>> {
pub(super) fn new(desc: MemoryDescriptor, local: &mut vm::LocalMemory) -> Option<Box<Self>> {
let memory = {
let mut memory =
sys::Memory::with_size((desc.min as usize * WASM_PAGE_SIZE) + DYNAMIC_GUARD_SIZE)

View File

@ -3,7 +3,7 @@ use crate::{
import::IsExport,
memory::dynamic::DYNAMIC_GUARD_SIZE,
memory::static_::{SAFE_STATIC_GUARD_SIZE, SAFE_STATIC_HEAP_SIZE},
types::{MemoryDesc, ValueType},
types::{MemoryDescriptor, ValueType},
vm,
};
use std::{cell::RefCell, fmt, mem, ptr, rc::Rc, slice};
@ -18,12 +18,12 @@ pub const WASM_PAGE_SIZE: usize = 65_536;
pub const WASM_MAX_PAGES: usize = 65_536;
pub struct Memory {
desc: MemoryDesc,
desc: MemoryDescriptor,
storage: Rc<RefCell<(MemoryStorage, Box<vm::LocalMemory>)>>,
}
impl Memory {
pub fn new(desc: MemoryDesc) -> Option<Self> {
pub fn new(desc: MemoryDescriptor) -> Option<Self> {
let mut vm_local_memory = Box::new(vm::LocalMemory {
base: ptr::null_mut(),
bound: 0,
@ -46,7 +46,7 @@ impl Memory {
})
}
pub fn description(&self) -> MemoryDesc {
pub fn descriptor(&self) -> MemoryDescriptor {
self.desc
}

View File

@ -4,7 +4,7 @@ use crate::{
WASM_MAX_PAGES, WASM_PAGE_SIZE,
},
sys,
types::MemoryDesc,
types::MemoryDescriptor,
vm,
};
@ -27,7 +27,7 @@ pub struct StaticMemory {
impl StaticMemory {
pub(in crate::memory) fn new(
desc: MemoryDesc,
desc: MemoryDescriptor,
local: &mut vm::LocalMemory,
) -> Option<Box<Self>> {
let memory = {

View File

@ -5,9 +5,10 @@ use crate::{
sig_registry::SigRegistry,
structures::Map,
types::{
FuncIndex, GlobalDesc, GlobalIndex, GlobalInit, ImportedFuncIndex, ImportedGlobalIndex,
ImportedMemoryIndex, ImportedTableIndex, Initializer, LocalGlobalIndex, LocalMemoryIndex,
LocalTableIndex, MemoryDesc, MemoryIndex, SigIndex, TableDesc, TableIndex,
FuncIndex, GlobalDescriptor, GlobalIndex, GlobalInit, ImportedFuncIndex,
ImportedGlobalIndex, ImportedMemoryIndex, ImportedTableIndex, Initializer,
LocalGlobalIndex, LocalMemoryIndex, LocalTableIndex, MemoryDescriptor, MemoryIndex,
SigIndex, TableDescriptor, TableIndex,
},
Instance,
};
@ -21,15 +22,15 @@ pub struct ModuleInner {
pub protected_caller: Box<dyn ProtectedCaller>,
// This are strictly local and the typsystem ensures that.
pub memories: Map<LocalMemoryIndex, MemoryDesc>,
pub memories: Map<LocalMemoryIndex, MemoryDescriptor>,
pub globals: Map<LocalGlobalIndex, GlobalInit>,
pub tables: Map<LocalTableIndex, TableDesc>,
pub tables: Map<LocalTableIndex, TableDescriptor>,
// These are strictly imported and the typesystem ensures that.
pub imported_functions: Map<ImportedFuncIndex, ImportName>,
pub imported_memories: Map<ImportedMemoryIndex, (ImportName, MemoryDesc)>,
pub imported_tables: Map<ImportedTableIndex, (ImportName, TableDesc)>,
pub imported_globals: Map<ImportedGlobalIndex, (ImportName, GlobalDesc)>,
pub imported_memories: Map<ImportedMemoryIndex, (ImportName, MemoryDescriptor)>,
pub imported_tables: Map<ImportedTableIndex, (ImportName, TableDescriptor)>,
pub imported_globals: Map<ImportedGlobalIndex, (ImportName, GlobalDescriptor)>,
pub exports: HashMap<String, ExportIndex>,

View File

@ -2,7 +2,7 @@ use crate::{
instance::Function,
sig_registry::SigRegistry,
structures::TypedIndex,
types::{FuncSig, TableDesc},
types::{FuncSig, TableDescriptor},
vm,
};
@ -32,13 +32,6 @@ impl<'a> Anyfunc<'a> {
},
}
}
pub(crate) fn raw(&self) -> *const vm::Func {
match self.inner {
AnyfuncInner::Host { ptr, .. } => ptr,
AnyfuncInner::Managed(ref func) => func.raw(),
}
}
}
impl<'a> From<Function<'a>> for Anyfunc<'a> {
@ -55,7 +48,7 @@ pub struct AnyfuncTable {
}
impl AnyfuncTable {
pub fn new(desc: TableDesc, local: &mut vm::LocalTable) -> Result<Box<Self>, ()> {
pub fn new(desc: TableDescriptor, local: &mut vm::LocalTable) -> Result<Box<Self>, ()> {
let initial_table_backing_len = match desc.max {
Some(max) => max,
None => desc.min,

View File

@ -1,7 +1,7 @@
use crate::{
export::Export,
import::IsExport,
types::{ElementType, TableDesc},
types::{ElementType, TableDescriptor},
vm,
};
use std::{cell::RefCell, fmt, ptr, rc::Rc};
@ -22,19 +22,19 @@ pub enum TableStorage {
}
pub struct Table {
desc: TableDesc,
desc: TableDescriptor,
storage: Rc<RefCell<(TableStorage, vm::LocalTable)>>,
}
impl Table {
pub fn new(desc: TableDesc) -> Result<Self, ()> {
pub fn new(desc: TableDescriptor) -> Result<Self, ()> {
let mut local = vm::LocalTable {
base: ptr::null_mut(),
count: 0,
table: ptr::null_mut(),
};
let storage = match desc.ty {
let storage = match desc.element {
ElementType::Anyfunc => TableStorage::Anyfunc(AnyfuncTable::new(desc, &mut local)?),
};
@ -44,7 +44,7 @@ impl Table {
})
}
pub fn description(&self) -> TableDesc {
pub fn descriptor(&self) -> TableDescriptor {
self.desc
}

View File

@ -128,21 +128,21 @@ pub enum ElementType {
}
#[derive(Debug, Clone, Copy)]
pub struct TableDesc {
pub struct TableDescriptor {
/// Type of data stored in this table.
pub ty: ElementType,
pub element: ElementType,
/// The minimum number of elements that must be stored in this table.
pub min: u32,
/// The maximum number of elements in this table.
pub max: Option<u32>,
}
impl TableDesc {
pub(crate) fn fits_in_imported(&self, imported: TableDesc) -> bool {
impl TableDescriptor {
pub(crate) fn fits_in_imported(&self, imported: TableDescriptor) -> bool {
// TODO: We should define implementation limits.
let imported_max = imported.max.unwrap_or(u32::max_value());
let self_max = self.max.unwrap_or(u32::max_value());
self.ty == imported.ty && imported_max <= self_max && self.min <= imported.min
self.element == imported.element && imported_max <= self_max && self.min <= imported.min
}
}
@ -158,7 +158,7 @@ pub enum Initializer {
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GlobalDesc {
pub struct GlobalDescriptor {
pub mutable: bool,
pub ty: Type,
}
@ -166,13 +166,13 @@ pub struct GlobalDesc {
/// A wasm global.
#[derive(Debug, Clone)]
pub struct GlobalInit {
pub desc: GlobalDesc,
pub desc: GlobalDescriptor,
pub init: Initializer,
}
/// A wasm memory.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MemoryDesc {
pub struct MemoryDescriptor {
/// The minimum number of allowed pages.
pub min: u32,
/// The maximum number of allowed pages.
@ -181,7 +181,7 @@ pub struct MemoryDesc {
pub shared: bool,
}
impl MemoryDesc {
impl MemoryDescriptor {
pub fn memory_type(self) -> MemoryType {
match (self.max.is_some(), self.shared) {
(true, true) => MemoryType::SharedStatic,
@ -191,7 +191,7 @@ impl MemoryDesc {
}
}
pub(crate) fn fits_in_imported(&self, imported: MemoryDesc) -> bool {
pub(crate) fn fits_in_imported(&self, imported: MemoryDescriptor) -> bool {
let imported_max = imported.max.unwrap_or(65_536);
let self_max = self.max.unwrap_or(65_536);

View File

@ -110,7 +110,7 @@ impl Ctx {
/// fn read_memory(ctx: &Ctx) -> u8 {
/// let first_memory = ctx.memory(0);
/// // Read the first byte of that linear memory.
/// first_memory.get(0)
/// first_memory.read(0).unwrap()
/// }
/// ```
pub fn memory<'a>(&'a self, mem_index: u32) -> &'a Memory {

View File

@ -92,7 +92,7 @@ pub mod wasm {
pub use wasmer_runtime_core::instance::Function;
pub use wasmer_runtime_core::memory::Memory;
pub use wasmer_runtime_core::table::Table;
pub use wasmer_runtime_core::types::{FuncSig, MemoryDesc, TableDesc, Type, Value};
pub use wasmer_runtime_core::types::{FuncSig, MemoryDescriptor, TableDescriptor, Type, Value};
}
/// Compile WebAssembly binary code into a [`Module`].