From e26ddb6d12e990762d9530f8e8b6919a9019994d Mon Sep 17 00:00:00 2001 From: Sergey Pepyakin Date: Mon, 22 Jan 2018 16:34:32 +0300 Subject: [PATCH] Hide MemoryType. --- src/imports.rs | 12 +++++------- src/lib.rs | 2 +- src/module.rs | 5 +++-- src/tests/host.rs | 8 +++----- src/tests/wasm.rs | 5 ++--- src/types.rs | 25 ++++++++++++++++++++++++- 6 files changed, 38 insertions(+), 19 deletions(-) diff --git a/src/imports.rs b/src/imports.rs index 4f0cb75e45..ef6a0e97b8 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -1,12 +1,10 @@ use std::collections::HashMap; -use parity_wasm::elements::MemoryType; use global::GlobalRef; use memory::MemoryRef; use func::FuncRef; use table::TableRef; use module::ModuleRef; -use types::GlobalDescriptor; -use types::TableDescriptor; +use types::{GlobalDescriptor, TableDescriptor, MemoryDescriptor}; use {Error, Signature}; pub trait ImportResolver { @@ -28,7 +26,7 @@ pub trait ImportResolver { &self, module_name: &str, field_name: &str, - memory_type: &MemoryType, + memory_type: &MemoryDescriptor, ) -> Result; fn resolve_table( @@ -99,7 +97,7 @@ impl<'a> ImportResolver for ImportsBuilder<'a> { &self, module_name: &str, field_name: &str, - memory_type: &MemoryType, + memory_type: &MemoryDescriptor, ) -> Result { self.resolver(module_name).ok_or_else(|| Error::Instantiation(format!("Module {} not found", module_name)) @@ -142,7 +140,7 @@ pub trait ModuleImportResolver { fn resolve_memory( &self, field_name: &str, - _memory_type: &MemoryType, + _memory_type: &MemoryDescriptor, ) -> Result { Err(Error::Instantiation( format!("Export {} not found", field_name), @@ -194,7 +192,7 @@ impl ModuleImportResolver for ModuleRef { fn resolve_memory( &self, field_name: &str, - _memory_type: &MemoryType, + _memory_type: &MemoryDescriptor, ) -> Result { Ok(self.export_by_name(field_name) .ok_or_else(|| { diff --git a/src/lib.rs b/src/lib.rs index e31e9393e6..da44a23b7f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -135,7 +135,7 @@ pub use self::imports::{ModuleImportResolver, ImportResolver, ImportsBuilder}; pub use self::module::{ModuleInstance, ModuleRef, ExternVal, NotStartedModuleRef}; pub use self::global::{GlobalInstance, GlobalRef}; pub use self::func::{FuncInstance, FuncRef}; -pub use self::types::{Signature, ValueType, GlobalDescriptor, TableDescriptor}; +pub use self::types::{Signature, ValueType, GlobalDescriptor, TableDescriptor, MemoryDescriptor}; pub struct LoadedModule { labels: HashMap>, diff --git a/src/module.rs b/src/module.rs index 37e05bc249..fb0b5c5d4f 100644 --- a/src/module.rs +++ b/src/module.rs @@ -12,7 +12,7 @@ use table::TableRef; use memory::MemoryRef; use host::Externals; use common::{DEFAULT_MEMORY_INDEX, DEFAULT_TABLE_INDEX}; -use types::{GlobalDescriptor, TableDescriptor}; +use types::{GlobalDescriptor, TableDescriptor, MemoryDescriptor}; #[derive(Clone, Debug)] pub struct ModuleRef(Rc); @@ -396,7 +396,8 @@ impl ModuleInstance { ExternVal::Table(table) } External::Memory(ref memory_type) => { - let memory = imports.resolve_memory(module_name, field_name, memory_type)?; + let memory_descriptor = MemoryDescriptor::from_elements(memory_type); + let memory = imports.resolve_memory(module_name, field_name, &memory_descriptor)?; ExternVal::Memory(memory) } External::Global(ref global_type) => { diff --git a/src/tests/host.rs b/src/tests/host.rs index 318532d50b..c96a2d9251 100644 --- a/src/tests/host.rs +++ b/src/tests/host.rs @@ -1,9 +1,7 @@ - -use parity_wasm::elements::MemoryType; use { Error, Signature, Externals, FuncInstance, FuncRef, HostError, ImportsBuilder, MemoryInstance, MemoryRef, TableInstance, TableRef, ModuleImportResolver, ModuleInstance, ModuleRef, - RuntimeValue, TryInto, LoadedModule, load_from_buffer, TableDescriptor, + RuntimeValue, TryInto, LoadedModule, load_from_buffer, TableDescriptor, MemoryDescriptor, }; use types::ValueType; use wabt::wat2wasm; @@ -195,7 +193,7 @@ impl ModuleImportResolver for TestHost { fn resolve_memory( &self, field_name: &str, - _memory_type: &MemoryType, + _memory_type: &MemoryDescriptor, ) -> Result { Err(Error::Instantiation( format!("Export {} not found", field_name), @@ -443,7 +441,7 @@ fn defer_providing_externals() { fn resolve_memory( &self, field_name: &str, - _memory_type: &MemoryType, + _memory_type: &MemoryDescriptor, ) -> Result { if field_name == "mem" { Ok(self.mem.clone()) diff --git a/src/tests/wasm.rs b/src/tests/wasm.rs index 05144b51ec..f86594fea4 100644 --- a/src/tests/wasm.rs +++ b/src/tests/wasm.rs @@ -1,8 +1,7 @@ -use parity_wasm::elements::MemoryType; use { Error, Signature, FuncRef, GlobalInstance, GlobalRef, ImportsBuilder, MemoryInstance, MemoryRef, ModuleImportResolver, ModuleInstance, NopExternals, RuntimeValue, - TableInstance, TableRef, LoadedModule, load_from_buffer, GlobalDescriptor, TableDescriptor, + TableInstance, TableRef, LoadedModule, load_from_buffer, GlobalDescriptor, TableDescriptor, MemoryDescriptor, }; use std::fs::File; @@ -49,7 +48,7 @@ impl ModuleImportResolver for Env { fn resolve_memory( &self, field_name: &str, - _memory_type: &MemoryType, + _memory_type: &MemoryDescriptor, ) -> Result { match field_name { "memory" => Ok(self.memory.clone()), diff --git a/src/types.rs b/src/types.rs index 3d65fa0e02..01c7c25d5d 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,4 +1,5 @@ -use parity_wasm::elements::{FunctionType, ValueType as EValueType, GlobalType, TableType}; +use parity_wasm::elements::{ + FunctionType, ValueType as EValueType, GlobalType, TableType, MemoryType}; #[derive(Debug, Clone, PartialEq)] pub struct Signature { @@ -101,3 +102,25 @@ impl TableDescriptor { self.maximum } } + +pub struct MemoryDescriptor { + initial: u32, + maximum: Option, +} + +impl MemoryDescriptor { + pub(crate) fn from_elements(memory_type: &MemoryType) -> MemoryDescriptor { + MemoryDescriptor { + initial: memory_type.limits().initial(), + maximum: memory_type.limits().maximum(), + } + } + + pub fn initial(&self) -> u32 { + self.initial + } + + pub fn maximum(&self) -> Option { + self.maximum + } +}