diff --git a/lib/clif-backend/src/module_env.rs b/lib/clif-backend/src/module_env.rs index c8b0f7576..85e32e87e 100644 --- a/lib/clif-backend/src/module_env.rs +++ b/lib/clif-backend/src/module_env.rs @@ -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, diff --git a/lib/emscripten/src/lib.rs b/lib/emscripten/src/lib.rs index 9ae0ae469..0ea2ad583 100644 --- a/lib/emscripten/src/lib.rs +++ b/lib/emscripten/src/lib.rs @@ -10,7 +10,7 @@ use wasmer_runtime_core::{ import::{ImportObject, Namespace}, memory::Memory, types::{ - FuncSig, GlobalDesc, + FuncSig, GlobalDescriptor, Type::{self, *}, }, vm::LocalGlobal, diff --git a/lib/runtime-core/examples/simple/main.rs b/lib/runtime-core/examples/simple/main.rs index 37e9d5e13..ad3992ba2 100644 --- a/lib/runtime-core/examples/simple/main.rs +++ b/lib/runtime-core/examples/simple/main.rs @@ -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, }) diff --git a/lib/runtime-core/src/backing.rs b/lib/runtime-core/src/backing.rs index 1f3847c8c..7187bfb9e 100644 --- a/lib/runtime-core/src/backing.rs +++ b/lib/runtime-core/src/backing.rs @@ -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(), }); } } diff --git a/lib/runtime-core/src/error.rs b/lib/runtime-core/src/error.rs index 3fe09e86b..f518140d3 100644 --- a/lib/runtime-core/src/error.rs +++ b/lib/runtime-core/src/error.rs @@ -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 = std::result::Result>; @@ -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, }, } diff --git a/lib/runtime-core/src/global.rs b/lib/runtime-core/src/global.rs index 1a578d193..4d4eb57e0 100644 --- a/lib/runtime-core/src/global.rs +++ b/lib/runtime-core/src/global.rs @@ -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>, } @@ -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 } diff --git a/lib/runtime-core/src/memory/dynamic.rs b/lib/runtime-core/src/memory/dynamic.rs index 478e737ac..8f0d5059f 100644 --- a/lib/runtime-core/src/memory/dynamic.rs +++ b/lib/runtime-core/src/memory/dynamic.rs @@ -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> { + pub(super) fn new(desc: MemoryDescriptor, local: &mut vm::LocalMemory) -> Option> { let memory = { let mut memory = sys::Memory::with_size((desc.min as usize * WASM_PAGE_SIZE) + DYNAMIC_GUARD_SIZE) diff --git a/lib/runtime-core/src/memory/mod.rs b/lib/runtime-core/src/memory/mod.rs index 4e25c359e..e4e0c9342 100644 --- a/lib/runtime-core/src/memory/mod.rs +++ b/lib/runtime-core/src/memory/mod.rs @@ -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)>>, } impl Memory { - pub fn new(desc: MemoryDesc) -> Option { + pub fn new(desc: MemoryDescriptor) -> Option { 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 } diff --git a/lib/runtime-core/src/memory/static_/unshared.rs b/lib/runtime-core/src/memory/static_/unshared.rs index 1a1ef0f65..65727b777 100644 --- a/lib/runtime-core/src/memory/static_/unshared.rs +++ b/lib/runtime-core/src/memory/static_/unshared.rs @@ -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> { let memory = { diff --git a/lib/runtime-core/src/module.rs b/lib/runtime-core/src/module.rs index e3c6021f8..9db69e08a 100644 --- a/lib/runtime-core/src/module.rs +++ b/lib/runtime-core/src/module.rs @@ -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, // This are strictly local and the typsystem ensures that. - pub memories: Map, + pub memories: Map, pub globals: Map, - pub tables: Map, + pub tables: Map, // These are strictly imported and the typesystem ensures that. pub imported_functions: Map, - pub imported_memories: Map, - pub imported_tables: Map, - pub imported_globals: Map, + pub imported_memories: Map, + pub imported_tables: Map, + pub imported_globals: Map, pub exports: HashMap, diff --git a/lib/runtime-core/src/table/anyfunc.rs b/lib/runtime-core/src/table/anyfunc.rs index c16bc3e65..e4994f337 100644 --- a/lib/runtime-core/src/table/anyfunc.rs +++ b/lib/runtime-core/src/table/anyfunc.rs @@ -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> for Anyfunc<'a> { @@ -55,7 +48,7 @@ pub struct AnyfuncTable { } impl AnyfuncTable { - pub fn new(desc: TableDesc, local: &mut vm::LocalTable) -> Result, ()> { + pub fn new(desc: TableDescriptor, local: &mut vm::LocalTable) -> Result, ()> { let initial_table_backing_len = match desc.max { Some(max) => max, None => desc.min, diff --git a/lib/runtime-core/src/table/mod.rs b/lib/runtime-core/src/table/mod.rs index cd3e0d2da..20cd873c6 100644 --- a/lib/runtime-core/src/table/mod.rs +++ b/lib/runtime-core/src/table/mod.rs @@ -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>, } impl Table { - pub fn new(desc: TableDesc) -> Result { + pub fn new(desc: TableDescriptor) -> Result { 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 } diff --git a/lib/runtime-core/src/types.rs b/lib/runtime-core/src/types.rs index 6d8c92f84..f9ce4f78b 100644 --- a/lib/runtime-core/src/types.rs +++ b/lib/runtime-core/src/types.rs @@ -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, } -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); diff --git a/lib/runtime-core/src/vm.rs b/lib/runtime-core/src/vm.rs index d1fc2d3f4..19113ca04 100644 --- a/lib/runtime-core/src/vm.rs +++ b/lib/runtime-core/src/vm.rs @@ -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 { diff --git a/lib/runtime/src/lib.rs b/lib/runtime/src/lib.rs index 2bdf27f9d..9fd78822b 100644 --- a/lib/runtime/src/lib.rs +++ b/lib/runtime/src/lib.rs @@ -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`].