Change Rcs to type aliases

This commit is contained in:
Sergey Pepyakin 2018-01-05 13:10:01 +03:00
parent e0ddc56fec
commit 9b11d1c96c
10 changed files with 96 additions and 70 deletions

View File

@ -21,16 +21,17 @@ use parity_wasm::interpreter::{
TableInstance,
ModuleInstance,
HostFunc,
MemoryRef,
};
struct SpecModule {
default_host_callback: Rc<HostFunc<()>>,
table: Rc<TableInstance<()>>,
memory: Rc<MemoryInstance>,
global_i32: Rc<GlobalInstance>,
global_i64: Rc<GlobalInstance>,
global_f32: Rc<GlobalInstance>,
global_f64: Rc<GlobalInstance>,
memory: MemoryRef,
global_i32: GlobalRef,
global_i64: GlobalRef,
global_f32: GlobalRef,
global_f64: GlobalRef,
}
impl SpecModule {
@ -73,7 +74,7 @@ impl ImportResolver<()> for SpecModule {
&self,
field_name: &str,
global_type: &GlobalType,
) -> Result<Rc<GlobalInstance>, InterpreterError> {
) -> Result<GlobalRef, InterpreterError> {
if field_name == "global" {
return match global_type.content_type() {
ValueType::I32 => {
@ -98,7 +99,7 @@ impl ImportResolver<()> for SpecModule {
&self,
field_name: &str,
_memory_type: &MemoryType,
) -> Result<Rc<MemoryInstance>, InterpreterError> {
) -> Result<MemoryRef, InterpreterError> {
if field_name == "memory" {
return Ok(Rc::clone(&self.memory));
}

View File

@ -3,6 +3,7 @@ use std::fmt;
use std::collections::HashMap;
use std::borrow::Cow;
use elements::{FunctionType, Local, Opcodes};
use interpreter::module::FuncRef;
use interpreter::{Error, ModuleInstance};
use interpreter::runner::{prepare_function_args, FunctionContext, Interpreter};
use interpreter::host::HostFunc;
@ -83,7 +84,7 @@ impl FuncInstance {
}
pub fn invoke<'a, 'b: 'a>(
func: Rc<FuncInstance>,
func: FuncRef,
args: Cow<[RuntimeValue]>,
state: &'a mut HostState<'b>,
) -> Result<Option<RuntimeValue>, Error> {

View File

@ -1,8 +1,11 @@
use interpreter::module::GlobalRef;
use std::rc::Rc;
use std::collections::HashMap;
use std::collections::hash_map::Entry;
use elements::{FunctionType, GlobalType, MemoryType, TableType, ValueType};
use interpreter::module::{ExternVal, ModuleInstance};
use interpreter::module::{ExternVal, ModuleInstance, FuncRef};
use interpreter::module::MemoryRef;
use interpreter::module::TableRef;
use interpreter::func::FuncInstance;
use interpreter::global::GlobalInstance;
use interpreter::memory::MemoryInstance;
@ -222,33 +225,33 @@ impl HostModuleBuilder {
self
}
pub fn insert_func<N: Into<String>>(&mut self, name: N, func: Rc<FuncInstance>) {
pub fn insert_func<N: Into<String>>(&mut self, name: N, func: FuncRef) {
self.insert(name, ExternVal::Func(func));
}
pub fn insert_global<N: Into<String>>(&mut self, name: N, global: Rc<GlobalInstance>) {
pub fn insert_global<N: Into<String>>(&mut self, name: N, global: GlobalRef) {
self.insert(name, ExternVal::Global(global));
}
pub fn insert_memory<N: Into<String>>(&mut self, name: N, memory: Rc<MemoryInstance>) {
pub fn insert_memory<N: Into<String>>(&mut self, name: N, memory: MemoryRef) {
self.insert(name, ExternVal::Memory(memory));
}
pub fn insert_table<N: Into<String>>(&mut self, name: N, table: Rc<TableInstance>) {
pub fn insert_table<N: Into<String>>(&mut self, name: N, table: TableRef) {
self.insert(name, ExternVal::Table(table));
}
pub fn with_global<N: Into<String>>(mut self, name: N, global: Rc<GlobalInstance>) -> Self {
pub fn with_global<N: Into<String>>(mut self, name: N, global: GlobalRef) -> Self {
self.insert_global(name, global);
self
}
pub fn with_memory<N: Into<String>>(mut self, name: N, memory: Rc<MemoryInstance>) -> Self {
pub fn with_memory<N: Into<String>>(mut self, name: N, memory: MemoryRef) -> Self {
self.insert_memory(name, memory);
self
}
pub fn with_table<N: Into<String>>(mut self, name: N, table: Rc<TableInstance>) -> Self {
pub fn with_table<N: Into<String>>(mut self, name: N, table: TableRef) -> Self {
self.insert_table(name, table);
self
}
@ -281,7 +284,7 @@ impl ImportResolver for HostModule {
&self,
field_name: &str,
func_type: &FunctionType,
) -> Result<Rc<FuncInstance>, Error> {
) -> Result<FuncRef, Error> {
self.internal_instance.resolve_func(field_name, func_type)
}
@ -289,7 +292,7 @@ impl ImportResolver for HostModule {
&self,
field_name: &str,
global_type: &GlobalType,
) -> Result<Rc<GlobalInstance>, Error> {
) -> Result<GlobalRef, Error> {
self.internal_instance
.resolve_global(field_name, global_type)
}
@ -298,7 +301,7 @@ impl ImportResolver for HostModule {
&self,
field_name: &str,
memory_type: &MemoryType,
) -> Result<Rc<MemoryInstance>, Error> {
) -> Result<MemoryRef, Error> {
self.internal_instance
.resolve_memory(field_name, memory_type)
}
@ -307,7 +310,7 @@ impl ImportResolver for HostModule {
&self,
field_name: &str,
table_type: &TableType,
) -> Result<Rc<TableInstance>, Error> {
) -> Result<TableRef, Error> {
self.internal_instance.resolve_table(field_name, table_type)
}
}
@ -382,3 +385,20 @@ impl IntoReturnVal for () {
None
}
}
trait Externals {
fn invoke_index(
&mut self,
index: u32,
args: &[RuntimeValue],
) -> Result<Option<RuntimeValue>, Error>;
// TODO: or check signature?
fn signature(&self, index: usize) -> &FunctionType;
fn memory_by_index(&self, index: usize) -> &MemoryInstance;
fn table_by_index(&self, index: usize) -> &TableInstance;
fn global_by_index(&self, index: usize) -> &GlobalInstance;
}

View File

@ -1,10 +1,9 @@
use std::rc::Rc;
use interpreter::module::GlobalRef;
use std::collections::HashMap;
use elements::{FunctionType, GlobalType, MemoryType, TableType};
use interpreter::func::FuncInstance;
use interpreter::global::GlobalInstance;
use interpreter::memory::MemoryInstance;
use interpreter::table::TableInstance;
use interpreter::module::MemoryRef;
use interpreter::module::FuncRef;
use interpreter::module::TableRef;
use interpreter::Error;
pub struct Imports<'a> {
@ -45,23 +44,23 @@ pub trait ImportResolver {
&self,
field_name: &str,
func_type: &FunctionType,
) -> Result<Rc<FuncInstance>, Error>;
) -> Result<FuncRef, Error>;
fn resolve_global(
&self,
field_name: &str,
global_type: &GlobalType,
) -> Result<Rc<GlobalInstance>, Error>;
) -> Result<GlobalRef, Error>;
fn resolve_memory(
&self,
field_name: &str,
memory_type: &MemoryType,
) -> Result<Rc<MemoryInstance>, Error>;
) -> Result<MemoryRef, Error>;
fn resolve_table(
&self,
field_name: &str,
table_type: &TableType,
) -> Result<Rc<TableInstance>, Error>;
) -> Result<TableRef, Error>;
}

View File

@ -157,7 +157,7 @@ pub use self::program::ProgramInstance;
pub use self::value::RuntimeValue;
pub use self::host::{HostModule, HostModuleBuilder, HostFunc, IntoReturnVal, FromArg};
pub use self::imports::{ImportResolver, Imports};
pub use self::module::ModuleInstance;
pub use self::module::{ModuleInstance, FuncRef, MemoryRef, GlobalRef, TableRef};
pub use self::global::GlobalInstance;
pub use self::func::FuncInstance;
pub use self::state::{HostState, StateKey};

View File

@ -13,11 +13,16 @@ use interpreter::state::HostState;
use validation::validate_module;
use common::{DEFAULT_MEMORY_INDEX, DEFAULT_TABLE_INDEX};
pub type FuncRef = Rc<FuncInstance>;
pub type TableRef = Rc<TableInstance>;
pub type MemoryRef = Rc<MemoryInstance>;
pub type GlobalRef = Rc<GlobalInstance>;
pub enum ExternVal {
Func(Rc<FuncInstance>),
Table(Rc<TableInstance>),
Memory(Rc<MemoryInstance>),
Global(Rc<GlobalInstance>),
Func(FuncRef),
Table(TableRef),
Memory(MemoryRef),
Global(GlobalRef),
}
impl Clone for ExternVal {
@ -47,28 +52,28 @@ impl fmt::Debug for ExternVal {
}
impl ExternVal {
pub fn as_func(&self) -> Option<Rc<FuncInstance>> {
pub fn as_func(&self) -> Option<FuncRef> {
match *self {
ExternVal::Func(ref func) => Some(Rc::clone(func)),
_ => None,
}
}
pub fn as_table(&self) -> Option<Rc<TableInstance>> {
pub fn as_table(&self) -> Option<TableRef> {
match *self {
ExternVal::Table(ref table) => Some(Rc::clone(table)),
_ => None,
}
}
pub fn as_memory(&self) -> Option<Rc<MemoryInstance>> {
pub fn as_memory(&self) -> Option<MemoryRef> {
match *self {
ExternVal::Memory(ref memory) => Some(Rc::clone(memory)),
_ => None,
}
}
pub fn as_global(&self) -> Option<Rc<GlobalInstance>> {
pub fn as_global(&self) -> Option<GlobalRef> {
match *self {
ExternVal::Global(ref global) => Some(Rc::clone(global)),
_ => None,
@ -79,10 +84,10 @@ impl ExternVal {
#[derive(Debug)]
pub struct ModuleInstance {
types: RefCell<Vec<Rc<FunctionType>>>,
tables: RefCell<Vec<Rc<TableInstance>>>,
funcs: RefCell<Vec<Rc<FuncInstance>>>,
memories: RefCell<Vec<Rc<MemoryInstance>>>,
globals: RefCell<Vec<Rc<GlobalInstance>>>,
tables: RefCell<Vec<TableRef>>,
funcs: RefCell<Vec<FuncRef>>,
memories: RefCell<Vec<MemoryRef>>,
globals: RefCell<Vec<GlobalRef>>,
exports: RefCell<HashMap<String, ExternVal>>,
}
@ -106,19 +111,19 @@ impl ModuleInstance {
instance
}
pub fn memory_by_index(&self, idx: u32) -> Option<Rc<MemoryInstance>> {
pub fn memory_by_index(&self, idx: u32) -> Option<MemoryRef> {
self.memories.borrow().get(idx as usize).cloned()
}
pub fn table_by_index(&self, idx: u32) -> Option<Rc<TableInstance>> {
pub fn table_by_index(&self, idx: u32) -> Option<TableRef> {
self.tables.borrow().get(idx as usize).cloned()
}
pub fn global_by_index(&self, idx: u32) -> Option<Rc<GlobalInstance>> {
pub fn global_by_index(&self, idx: u32) -> Option<GlobalRef> {
self.globals.borrow().get(idx as usize).cloned()
}
pub fn func_by_index(&self, idx: u32) -> Option<Rc<FuncInstance>> {
pub fn func_by_index(&self, idx: u32) -> Option<FuncRef> {
self.funcs.borrow().get(idx as usize).cloned()
}
@ -130,7 +135,7 @@ impl ModuleInstance {
self.exports.borrow().get(name).cloned()
}
fn push_func(&self, func: Rc<FuncInstance>) {
fn push_func(&self, func: FuncRef) {
self.funcs.borrow_mut().push(func);
}
@ -138,15 +143,15 @@ impl ModuleInstance {
self.types.borrow_mut().push(func_type)
}
fn push_memory(&self, memory: Rc<MemoryInstance>) {
fn push_memory(&self, memory: MemoryRef) {
self.memories.borrow_mut().push(memory)
}
fn push_table(&self, table: Rc<TableInstance>) {
fn push_table(&self, table: TableRef) {
self.tables.borrow_mut().push(table)
}
fn push_global(&self, global: Rc<GlobalInstance>) {
fn push_global(&self, global: GlobalRef) {
self.globals.borrow_mut().push(global)
}
@ -498,7 +503,7 @@ impl ImportResolver for ModuleInstance {
&self,
field_name: &str,
_func_type: &FunctionType,
) -> Result<Rc<FuncInstance>, Error> {
) -> Result<FuncRef, Error> {
Ok(self.export_by_name(field_name)
.ok_or_else(|| {
Error::Validation(format!("Export {} not found", field_name))
@ -513,7 +518,7 @@ impl ImportResolver for ModuleInstance {
&self,
field_name: &str,
_global_type: &GlobalType,
) -> Result<Rc<GlobalInstance>, Error> {
) -> Result<GlobalRef, Error> {
Ok(self.export_by_name(field_name)
.ok_or_else(|| {
Error::Validation(format!("Export {} not found", field_name))
@ -528,7 +533,7 @@ impl ImportResolver for ModuleInstance {
&self,
field_name: &str,
_memory_type: &MemoryType,
) -> Result<Rc<MemoryInstance>, Error> {
) -> Result<MemoryRef, Error> {
Ok(self.export_by_name(field_name)
.ok_or_else(|| {
Error::Validation(format!("Export {} not found", field_name))
@ -543,7 +548,7 @@ impl ImportResolver for ModuleInstance {
&self,
field_name: &str,
_table_type: &TableType,
) -> Result<Rc<TableInstance>, Error> {
) -> Result<TableRef, Error> {
Ok(self.export_by_name(field_name)
.ok_or_else(|| {
Error::Validation(format!("Export {} not found", field_name))
@ -559,17 +564,17 @@ fn alloc_func_type(func_type: FunctionType) -> Rc<FunctionType> {
Rc::new(func_type)
}
fn alloc_table(table_type: &TableType) -> Result<Rc<TableInstance>, Error> {
fn alloc_table(table_type: &TableType) -> Result<TableRef, Error> {
let table = TableInstance::new(table_type)?;
Ok(Rc::new(table))
}
fn alloc_memory(mem_type: &MemoryType) -> Result<Rc<MemoryInstance>, Error> {
fn alloc_memory(mem_type: &MemoryType) -> Result<MemoryRef, Error> {
let memory = MemoryInstance::new(&mem_type)?;
Ok(Rc::new(memory))
}
fn alloc_global(global_type: GlobalType, val: RuntimeValue) -> Rc<GlobalInstance> {
fn alloc_global(global_type: GlobalType, val: RuntimeValue) -> GlobalRef {
let global = GlobalInstance::new(val, global_type.is_mutable());
Rc::new(global)
}

View File

@ -3,7 +3,7 @@ use std::collections::HashMap;
use std::borrow::Cow;
use elements::Module;
use interpreter::Error;
use interpreter::module::ModuleInstance;
use interpreter::module::{ModuleInstance, FuncRef};
use interpreter::func::FuncInstance;
use interpreter::host::HostModule;
use interpreter::value::RuntimeValue;
@ -98,7 +98,7 @@ impl ProgramInstance {
pub fn invoke_func<'a>(
&mut self,
func_instance: Rc<FuncInstance>,
func_instance: FuncRef,
args: &[RuntimeValue],
state: &'a mut HostState<'a>,
) -> Result<Option<RuntimeValue>, Error> {

View File

@ -8,6 +8,7 @@ use std::collections::{HashMap, VecDeque};
use elements::{Opcode, BlockType, Local, FunctionType};
use interpreter::Error;
use interpreter::module::ModuleInstance;
use interpreter::module::FuncRef;
use interpreter::func::FuncInstance;
use interpreter::value::{
RuntimeValue, TryInto, WrapInto, TryTruncateInto, ExtendInto,
@ -27,7 +28,7 @@ pub struct FunctionContext {
/// Is context initialized.
pub is_initialized: bool,
/// Internal function reference.
pub function: Rc<FuncInstance>,
pub function: FuncRef,
pub module: Rc<ModuleInstance>,
/// Function return type.
pub return_type: BlockType,
@ -48,7 +49,7 @@ pub enum InstructionOutcome {
/// Branch to given frame.
Branch(usize),
/// Execute function call.
ExecuteCall(Rc<FuncInstance>),
ExecuteCall(FuncRef),
/// End current frame.
End,
/// Return from current function block.
@ -60,7 +61,7 @@ enum RunResult {
/// Function has returned (optional) value.
Return(Option<RuntimeValue>),
/// Function is calling other function.
NestedCall(Rc<FuncInstance>),
NestedCall(FuncRef),
}
impl<'a, 'b: 'a> Interpreter<'a, 'b> {
@ -998,7 +999,7 @@ impl<'a, 'b: 'a> Interpreter<'a, 'b> {
}
impl FunctionContext {
pub fn new<'store>(function: Rc<FuncInstance>, value_stack_limit: usize, frame_stack_limit: usize, function_type: &FunctionType, args: Vec<RuntimeValue>) -> Self {
pub fn new<'store>(function: FuncRef, value_stack_limit: usize, frame_stack_limit: usize, function_type: &FunctionType, args: Vec<RuntimeValue>) -> Self {
let module = match *function {
FuncInstance::Internal { ref module, .. } => Rc::clone(module),
FuncInstance::Host { .. } => panic!("Host functions can't be called as internally defined functions; Thus FunctionContext can be created only with internally defined functions; qed"),
@ -1015,7 +1016,7 @@ impl FunctionContext {
}
}
pub fn nested(&mut self, function: Rc<FuncInstance>) -> Result<Self, Error> {
pub fn nested(&mut self, function: FuncRef) -> Result<Self, Error> {
let (function_locals, module, function_return_type) = {
let module = match *function {
FuncInstance::Internal { ref module, .. } => Rc::clone(module),

View File

@ -1,18 +1,17 @@
use std::u32;
use std::fmt;
use std::rc::Rc;
use std::cell::RefCell;
use elements::{ResizableLimits, TableType};
use interpreter::Error;
use interpreter::module::FuncRef;
use interpreter::module::check_limits;
use interpreter::func::FuncInstance;
/// Table instance.
pub struct TableInstance {
/// Table limits.
limits: ResizableLimits,
/// Table memory buffer.
buffer: RefCell<Vec<Option<Rc<FuncInstance>>>>,
buffer: RefCell<Vec<Option<FuncRef>>>,
}
impl fmt::Debug for TableInstance {
@ -40,7 +39,7 @@ impl TableInstance {
}
/// Get the specific value in the table
pub fn get(&self, offset: u32) -> Result<Rc<FuncInstance>, Error> {
pub fn get(&self, offset: u32) -> Result<FuncRef, Error> {
let buffer = self.buffer.borrow();
let buffer_len = buffer.len();
let table_elem = buffer.get(offset as usize).cloned().ok_or(
@ -57,7 +56,7 @@ impl TableInstance {
}
/// Set the table element to the specified function.
pub fn set(&self, offset: u32, value: Rc<FuncInstance>) -> Result<(), Error> {
pub fn set(&self, offset: u32, value: FuncRef) -> Result<(), Error> {
let mut buffer = self.buffer.borrow_mut();
let buffer_len = buffer.len();
let table_elem = buffer.get_mut(offset as usize).ok_or(Error::Table(format!(

View File

@ -118,7 +118,7 @@ impl ::std::fmt::Display for UserErrorWithCode {
impl UserError for UserErrorWithCode {}
struct TestState {
pub memory: Rc<MemoryInstance>,
pub memory: MemoryRef,
pub values: RefCell<Vec<i32>>,
}