Hide MemoryType.

This commit is contained in:
Sergey Pepyakin 2018-01-22 16:34:32 +03:00
parent 973d58a643
commit e26ddb6d12
6 changed files with 38 additions and 19 deletions

View File

@ -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<MemoryRef, Error>;
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<MemoryRef, Error> {
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<MemoryRef, Error> {
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<MemoryRef, Error> {
Ok(self.export_by_name(field_name)
.ok_or_else(|| {

View File

@ -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<usize, HashMap<usize, usize>>,

View File

@ -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<ModuleInstance>);
@ -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) => {

View File

@ -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<MemoryRef, Error> {
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<MemoryRef, Error> {
if field_name == "mem" {
Ok(self.mem.clone())

View File

@ -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<MemoryRef, Error> {
match field_name {
"memory" => Ok(self.memory.clone()),

View File

@ -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<u32>,
}
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<u32> {
self.maximum
}
}