Hide TableType.

This commit is contained in:
Sergey Pepyakin 2018-01-22 16:30:13 +03:00
parent b323d004fe
commit 973d58a643
6 changed files with 42 additions and 18 deletions

View File

@ -1,11 +1,12 @@
use std::collections::HashMap; use std::collections::HashMap;
use parity_wasm::elements::{MemoryType, TableType}; use parity_wasm::elements::MemoryType;
use global::GlobalRef; use global::GlobalRef;
use memory::MemoryRef; use memory::MemoryRef;
use func::FuncRef; use func::FuncRef;
use table::TableRef; use table::TableRef;
use module::ModuleRef; use module::ModuleRef;
use types::GlobalDescriptor; use types::GlobalDescriptor;
use types::TableDescriptor;
use {Error, Signature}; use {Error, Signature};
pub trait ImportResolver { pub trait ImportResolver {
@ -34,7 +35,7 @@ pub trait ImportResolver {
&self, &self,
module_name: &str, module_name: &str,
field_name: &str, field_name: &str,
table_type: &TableType, table_type: &TableDescriptor,
) -> Result<TableRef, Error>; ) -> Result<TableRef, Error>;
} }
@ -109,7 +110,7 @@ impl<'a> ImportResolver for ImportsBuilder<'a> {
&self, &self,
module_name: &str, module_name: &str,
field_name: &str, field_name: &str,
table_type: &TableType, table_type: &TableDescriptor,
) -> Result<TableRef, Error> { ) -> Result<TableRef, Error> {
self.resolver(module_name).ok_or_else(|| self.resolver(module_name).ok_or_else(||
Error::Instantiation(format!("Module {} not found", module_name)) Error::Instantiation(format!("Module {} not found", module_name))
@ -151,7 +152,7 @@ pub trait ModuleImportResolver {
fn resolve_table( fn resolve_table(
&self, &self,
field_name: &str, field_name: &str,
_table_type: &TableType, _table_type: &TableDescriptor,
) -> Result<TableRef, Error> { ) -> Result<TableRef, Error> {
Err(Error::Instantiation( Err(Error::Instantiation(
format!("Export {} not found", field_name), format!("Export {} not found", field_name),
@ -208,7 +209,7 @@ impl ModuleImportResolver for ModuleRef {
fn resolve_table( fn resolve_table(
&self, &self,
field_name: &str, field_name: &str,
_table_type: &TableType, _table_type: &TableDescriptor,
) -> Result<TableRef, Error> { ) -> Result<TableRef, Error> {
Ok(self.export_by_name(field_name) Ok(self.export_by_name(field_name)
.ok_or_else(|| { .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::module::{ModuleInstance, ModuleRef, ExternVal, NotStartedModuleRef};
pub use self::global::{GlobalInstance, GlobalRef}; pub use self::global::{GlobalInstance, GlobalRef};
pub use self::func::{FuncInstance, FuncRef}; pub use self::func::{FuncInstance, FuncRef};
pub use self::types::{Signature, ValueType, GlobalDescriptor}; pub use self::types::{Signature, ValueType, GlobalDescriptor, TableDescriptor};
pub struct LoadedModule { pub struct LoadedModule {
labels: HashMap<usize, HashMap<usize, usize>>, labels: HashMap<usize, HashMap<usize, usize>>,

View File

@ -12,7 +12,7 @@ use table::TableRef;
use memory::MemoryRef; use memory::MemoryRef;
use host::Externals; use host::Externals;
use common::{DEFAULT_MEMORY_INDEX, DEFAULT_TABLE_INDEX}; use common::{DEFAULT_MEMORY_INDEX, DEFAULT_TABLE_INDEX};
use types::GlobalDescriptor; use types::{GlobalDescriptor, TableDescriptor};
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct ModuleRef(Rc<ModuleInstance>); pub struct ModuleRef(Rc<ModuleInstance>);
@ -391,7 +391,8 @@ impl ModuleInstance {
ExternVal::Func(func) ExternVal::Func(func)
} }
External::Table(ref table_type) => { External::Table(ref table_type) => {
let table = imports.resolve_table(module_name, field_name, table_type)?; let table_descriptor = TableDescriptor::from_elements(table_type);
let table = imports.resolve_table(module_name, field_name, &table_descriptor)?;
ExternVal::Table(table) ExternVal::Table(table)
} }
External::Memory(ref memory_type) => { External::Memory(ref memory_type) => {
@ -399,7 +400,7 @@ impl ModuleInstance {
ExternVal::Memory(memory) ExternVal::Memory(memory)
} }
External::Global(ref global_type) => { External::Global(ref global_type) => {
let global_descriptor = GlobalDescriptor::from_global_type(global_type); let global_descriptor = GlobalDescriptor::from_elements(global_type);
let global = imports.resolve_global(module_name, field_name, &global_descriptor)?; let global = imports.resolve_global(module_name, field_name, &global_descriptor)?;
ExternVal::Global(global) ExternVal::Global(global)
} }

View File

@ -1,9 +1,9 @@
use parity_wasm::elements::{MemoryType, TableType}; use parity_wasm::elements::MemoryType;
use { use {
Error, Signature, Externals, FuncInstance, FuncRef, HostError, ImportsBuilder, Error, Signature, Externals, FuncInstance, FuncRef, HostError, ImportsBuilder,
MemoryInstance, MemoryRef, TableInstance, TableRef, ModuleImportResolver, ModuleInstance, ModuleRef, MemoryInstance, MemoryRef, TableInstance, TableRef, ModuleImportResolver, ModuleInstance, ModuleRef,
RuntimeValue, TryInto, LoadedModule, load_from_buffer, RuntimeValue, TryInto, LoadedModule, load_from_buffer, TableDescriptor,
}; };
use types::ValueType; use types::ValueType;
use wabt::wat2wasm; use wabt::wat2wasm;
@ -696,7 +696,7 @@ fn dynamically_add_host_func() {
fn resolve_table( fn resolve_table(
&self, &self,
field_name: &str, field_name: &str,
_table_type: &TableType, _table_type: &TableDescriptor,
) -> Result<TableRef, Error> { ) -> Result<TableRef, Error> {
if field_name == "table" { if field_name == "table" {
Ok(self.table.clone()) Ok(self.table.clone())

View File

@ -1,8 +1,8 @@
use parity_wasm::elements::{MemoryType, TableType}; use parity_wasm::elements::MemoryType;
use { use {
Error, Signature, FuncRef, GlobalInstance, GlobalRef, ImportsBuilder, MemoryInstance, Error, Signature, FuncRef, GlobalInstance, GlobalRef, ImportsBuilder, MemoryInstance,
MemoryRef, ModuleImportResolver, ModuleInstance, NopExternals, RuntimeValue, MemoryRef, ModuleImportResolver, ModuleInstance, NopExternals, RuntimeValue,
TableInstance, TableRef, LoadedModule, load_from_buffer, GlobalDescriptor, TableInstance, TableRef, LoadedModule, load_from_buffer, GlobalDescriptor, TableDescriptor,
}; };
use std::fs::File; use std::fs::File;
@ -60,7 +60,7 @@ impl ModuleImportResolver for Env {
} }
} }
fn resolve_table(&self, field_name: &str, _table_type: &TableType) -> Result<TableRef, Error> { fn resolve_table(&self, field_name: &str, _table_type: &TableDescriptor) -> Result<TableRef, Error> {
match field_name { match field_name {
"table" => Ok(self.table.clone()), "table" => Ok(self.table.clone()),
_ => Err(Error::Instantiation( _ => Err(Error::Instantiation(

View File

@ -1,4 +1,4 @@
use parity_wasm::elements::{FunctionType, ValueType as EValueType, GlobalType}; use parity_wasm::elements::{FunctionType, ValueType as EValueType, GlobalType, TableType};
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct Signature { pub struct Signature {
@ -64,7 +64,7 @@ pub struct GlobalDescriptor {
} }
impl GlobalDescriptor { impl GlobalDescriptor {
pub(crate) fn from_global_type(global_type: &GlobalType) -> GlobalDescriptor { pub(crate) fn from_elements(global_type: &GlobalType) -> GlobalDescriptor {
GlobalDescriptor { GlobalDescriptor {
value_type: ValueType::from_elements(global_type.content_type()), value_type: ValueType::from_elements(global_type.content_type()),
mutable: global_type.is_mutable(), mutable: global_type.is_mutable(),
@ -79,3 +79,25 @@ impl GlobalDescriptor {
self.mutable self.mutable
} }
} }
pub struct TableDescriptor {
initial: u32,
maximum: Option<u32>,
}
impl TableDescriptor {
pub(crate) fn from_elements(table_type: &TableType) -> TableDescriptor {
TableDescriptor {
initial: table_type.limits().initial(),
maximum: table_type.limits().maximum(),
}
}
pub fn initial(&self) -> u32 {
self.initial
}
pub fn maximum(&self) -> Option<u32> {
self.maximum
}
}