Change raw indexes with Host*Index types

This commit is contained in:
Sergey Pepyakin 2018-01-08 19:07:23 +03:00
parent 97b74ed10e
commit 91cdc461bc
3 changed files with 19 additions and 15 deletions

View File

@ -7,6 +7,7 @@ use parity_wasm::interpreter::{
Error as InterpreterError, ModuleInstance, UserError, ModuleRef,
HostFuncIndex, Externals, RuntimeValue, GlobalInstance, TableInstance, MemoryInstance,
TableRef, MemoryRef, GlobalRef, FuncRef, TryInto, ImportResolver, FuncInstance,
HostGlobalIndex, HostMemoryIndex, HostTableIndex,
};
use parity_wasm::elements::{Error as DeserializationError};
use parity_wasm::ValidationError;
@ -191,15 +192,15 @@ impl<'a> Externals for Runtime<'a> {
}
}
fn memory_by_index(&self, _index: usize) -> &MemoryInstance {
fn memory_by_index(&self, _index: HostMemoryIndex) -> Option<&MemoryInstance> {
panic!("host module doesn't export any memories")
}
fn table_by_index(&self, _index: usize) -> &TableInstance {
fn table_by_index(&self, _index: HostTableIndex) -> Option<&TableInstance> {
panic!("host module doesn't export any tables")
}
fn global_by_index(&self, _index: usize) -> &GlobalInstance {
fn global_by_index(&self, _index: HostGlobalIndex) -> Option<&GlobalInstance> {
panic!("host module doesn't export any globals")
}
}

View File

@ -6,6 +6,9 @@ use interpreter::value::RuntimeValue;
use interpreter::Error;
pub type HostFuncIndex = u32;
pub type HostMemoryIndex = u32;
pub type HostTableIndex = u32;
pub type HostGlobalIndex = u32;
pub trait Externals {
fn invoke_index(
@ -16,9 +19,9 @@ pub trait Externals {
fn check_signature(&self, index: HostFuncIndex, signature: &FunctionType) -> bool;
fn memory_by_index(&self, index: usize) -> &MemoryInstance;
fn table_by_index(&self, index: usize) -> &TableInstance;
fn global_by_index(&self, index: usize) -> &GlobalInstance;
fn memory_by_index(&self, index: HostMemoryIndex) -> Option<&MemoryInstance>;
fn table_by_index(&self, index: HostTableIndex) -> Option<&TableInstance>;
fn global_by_index(&self, index: HostGlobalIndex) -> Option<&GlobalInstance>;
}
pub struct EmptyExternals;
@ -29,22 +32,22 @@ impl Externals for EmptyExternals {
_index: HostFuncIndex,
_args: &[RuntimeValue],
) -> Result<Option<RuntimeValue>, Error> {
panic!("called invoke_index on EmptyExternals")
Err(Error::Trap("invoke index on empty externals".into()))
}
fn check_signature(&self, _index: HostFuncIndex, _signature: &FunctionType) -> bool {
panic!("called check_signature on EmptyExternals")
false
}
fn memory_by_index(&self, _index: usize) -> &MemoryInstance {
panic!("called memory_by_index on EmptyExternals")
fn memory_by_index(&self, _index: HostMemoryIndex) -> Option<&MemoryInstance> {
None
}
fn table_by_index(&self, _index: usize) -> &TableInstance {
panic!("called table_by_index on EmptyExternals")
fn table_by_index(&self, _index: HostTableIndex) -> Option<&TableInstance> {
None
}
fn global_by_index(&self, _index: usize) -> &GlobalInstance {
panic!("called global_by_index on EmptyExternals")
fn global_by_index(&self, _index: HostGlobalIndex) -> Option<&GlobalInstance> {
None
}
}

View File

@ -153,7 +153,7 @@ pub use self::memory::{MemoryInstance, MemoryRef};
pub use self::table::{TableInstance, TableRef};
pub use self::program::ProgramInstance;
pub use self::value::{RuntimeValue, TryInto};
pub use self::host::{Externals, HostFuncIndex, EmptyExternals};
pub use self::host::{Externals, HostFuncIndex, HostGlobalIndex, HostMemoryIndex, HostTableIndex, EmptyExternals};
pub use self::imports::{ImportResolver, Imports};
pub use self::module::{ModuleInstance, ModuleRef};
pub use self::global::{GlobalInstance, GlobalRef};