diff --git a/examples/tictactoe.rs b/examples/tictactoe.rs index ef8f210..9a68b33 100644 --- a/examples/tictactoe.rs +++ b/examples/tictactoe.rs @@ -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") } } diff --git a/src/interpreter/host.rs b/src/interpreter/host.rs index 20b8c55..a2c33fc 100644 --- a/src/interpreter/host.rs +++ b/src/interpreter/host.rs @@ -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, 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 } } diff --git a/src/interpreter/mod.rs b/src/interpreter/mod.rs index ca3d3ea..ef8523c 100644 --- a/src/interpreter/mod.rs +++ b/src/interpreter/mod.rs @@ -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};