Skip to content

Commit

Permalink
Hide TableType.
Browse files Browse the repository at this point in the history
  • Loading branch information
pepyakin committed Jan 22, 2018
1 parent b323d00 commit 973d58a
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 18 deletions.
11 changes: 6 additions & 5 deletions src/imports.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::collections::HashMap;
use parity_wasm::elements::{MemoryType, TableType};
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 {Error, Signature};

pub trait ImportResolver {
Expand Down Expand Up @@ -34,7 +35,7 @@ pub trait ImportResolver {
&self,
module_name: &str,
field_name: &str,
table_type: &TableType,
table_type: &TableDescriptor,
) -> Result<TableRef, Error>;
}

Expand Down Expand Up @@ -109,7 +110,7 @@ impl<'a> ImportResolver for ImportsBuilder<'a> {
&self,
module_name: &str,
field_name: &str,
table_type: &TableType,
table_type: &TableDescriptor,
) -> Result<TableRef, Error> {
self.resolver(module_name).ok_or_else(||
Error::Instantiation(format!("Module {} not found", module_name))
Expand Down Expand Up @@ -151,7 +152,7 @@ pub trait ModuleImportResolver {
fn resolve_table(
&self,
field_name: &str,
_table_type: &TableType,
_table_type: &TableDescriptor,
) -> Result<TableRef, Error> {
Err(Error::Instantiation(
format!("Export {} not found", field_name),
Expand Down Expand Up @@ -208,7 +209,7 @@ impl ModuleImportResolver for ModuleRef {
fn resolve_table(
&self,
field_name: &str,
_table_type: &TableType,
_table_type: &TableDescriptor,
) -> Result<TableRef, Error> {
Ok(self.export_by_name(field_name)
.ok_or_else(|| {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
pub use self::types::{Signature, ValueType, GlobalDescriptor, TableDescriptor};

pub struct LoadedModule {
labels: HashMap<usize, HashMap<usize, usize>>,
Expand Down
7 changes: 4 additions & 3 deletions src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use table::TableRef;
use memory::MemoryRef;
use host::Externals;
use common::{DEFAULT_MEMORY_INDEX, DEFAULT_TABLE_INDEX};
use types::GlobalDescriptor;
use types::{GlobalDescriptor, TableDescriptor};

#[derive(Clone, Debug)]
pub struct ModuleRef(Rc<ModuleInstance>);
Expand Down Expand Up @@ -391,15 +391,16 @@ impl ModuleInstance {
ExternVal::Func(func)
}
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)
}
External::Memory(ref memory_type) => {
let memory = imports.resolve_memory(module_name, field_name, memory_type)?;
ExternVal::Memory(memory)
}
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)?;
ExternVal::Global(global)
}
Expand Down
6 changes: 3 additions & 3 deletions src/tests/host.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

use parity_wasm::elements::{MemoryType, TableType};
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,
RuntimeValue, TryInto, LoadedModule, load_from_buffer, TableDescriptor,
};
use types::ValueType;
use wabt::wat2wasm;
Expand Down Expand Up @@ -696,7 +696,7 @@ fn dynamically_add_host_func() {
fn resolve_table(
&self,
field_name: &str,
_table_type: &TableType,
_table_type: &TableDescriptor,
) -> Result<TableRef, Error> {
if field_name == "table" {
Ok(self.table.clone())
Expand Down
6 changes: 3 additions & 3 deletions src/tests/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use parity_wasm::elements::{MemoryType, TableType};
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,
TableInstance, TableRef, LoadedModule, load_from_buffer, GlobalDescriptor, TableDescriptor,
};
use std::fs::File;

Expand Down Expand Up @@ -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 {
"table" => Ok(self.table.clone()),
_ => Err(Error::Instantiation(
Expand Down
28 changes: 25 additions & 3 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -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)]
pub struct Signature {
Expand Down Expand Up @@ -64,7 +64,7 @@ pub struct GlobalDescriptor {
}

impl GlobalDescriptor {
pub(crate) fn from_global_type(global_type: &GlobalType) -> GlobalDescriptor {
pub(crate) fn from_elements(global_type: &GlobalType) -> GlobalDescriptor {
GlobalDescriptor {
value_type: ValueType::from_elements(global_type.content_type()),
mutable: global_type.is_mutable(),
Expand All @@ -78,4 +78,26 @@ impl GlobalDescriptor {
pub fn is_mutable(&self) -> bool {
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
}
}

0 comments on commit 973d58a

Please sign in to comment.