mirror of
https://github.com/fluencelabs/parity-wasm
synced 2025-06-03 01:51:52 +00:00
Change Rcs to type aliases
This commit is contained in:
parent
e0ddc56fec
commit
9b11d1c96c
@ -21,16 +21,17 @@ use parity_wasm::interpreter::{
|
|||||||
TableInstance,
|
TableInstance,
|
||||||
ModuleInstance,
|
ModuleInstance,
|
||||||
HostFunc,
|
HostFunc,
|
||||||
|
MemoryRef,
|
||||||
};
|
};
|
||||||
|
|
||||||
struct SpecModule {
|
struct SpecModule {
|
||||||
default_host_callback: Rc<HostFunc<()>>,
|
default_host_callback: Rc<HostFunc<()>>,
|
||||||
table: Rc<TableInstance<()>>,
|
table: Rc<TableInstance<()>>,
|
||||||
memory: Rc<MemoryInstance>,
|
memory: MemoryRef,
|
||||||
global_i32: Rc<GlobalInstance>,
|
global_i32: GlobalRef,
|
||||||
global_i64: Rc<GlobalInstance>,
|
global_i64: GlobalRef,
|
||||||
global_f32: Rc<GlobalInstance>,
|
global_f32: GlobalRef,
|
||||||
global_f64: Rc<GlobalInstance>,
|
global_f64: GlobalRef,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SpecModule {
|
impl SpecModule {
|
||||||
@ -73,7 +74,7 @@ impl ImportResolver<()> for SpecModule {
|
|||||||
&self,
|
&self,
|
||||||
field_name: &str,
|
field_name: &str,
|
||||||
global_type: &GlobalType,
|
global_type: &GlobalType,
|
||||||
) -> Result<Rc<GlobalInstance>, InterpreterError> {
|
) -> Result<GlobalRef, InterpreterError> {
|
||||||
if field_name == "global" {
|
if field_name == "global" {
|
||||||
return match global_type.content_type() {
|
return match global_type.content_type() {
|
||||||
ValueType::I32 => {
|
ValueType::I32 => {
|
||||||
@ -98,7 +99,7 @@ impl ImportResolver<()> for SpecModule {
|
|||||||
&self,
|
&self,
|
||||||
field_name: &str,
|
field_name: &str,
|
||||||
_memory_type: &MemoryType,
|
_memory_type: &MemoryType,
|
||||||
) -> Result<Rc<MemoryInstance>, InterpreterError> {
|
) -> Result<MemoryRef, InterpreterError> {
|
||||||
if field_name == "memory" {
|
if field_name == "memory" {
|
||||||
return Ok(Rc::clone(&self.memory));
|
return Ok(Rc::clone(&self.memory));
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ use std::fmt;
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use elements::{FunctionType, Local, Opcodes};
|
use elements::{FunctionType, Local, Opcodes};
|
||||||
|
use interpreter::module::FuncRef;
|
||||||
use interpreter::{Error, ModuleInstance};
|
use interpreter::{Error, ModuleInstance};
|
||||||
use interpreter::runner::{prepare_function_args, FunctionContext, Interpreter};
|
use interpreter::runner::{prepare_function_args, FunctionContext, Interpreter};
|
||||||
use interpreter::host::HostFunc;
|
use interpreter::host::HostFunc;
|
||||||
@ -83,7 +84,7 @@ impl FuncInstance {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn invoke<'a, 'b: 'a>(
|
pub fn invoke<'a, 'b: 'a>(
|
||||||
func: Rc<FuncInstance>,
|
func: FuncRef,
|
||||||
args: Cow<[RuntimeValue]>,
|
args: Cow<[RuntimeValue]>,
|
||||||
state: &'a mut HostState<'b>,
|
state: &'a mut HostState<'b>,
|
||||||
) -> Result<Option<RuntimeValue>, Error> {
|
) -> Result<Option<RuntimeValue>, Error> {
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
|
use interpreter::module::GlobalRef;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::collections::hash_map::Entry;
|
use std::collections::hash_map::Entry;
|
||||||
use elements::{FunctionType, GlobalType, MemoryType, TableType, ValueType};
|
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::func::FuncInstance;
|
||||||
use interpreter::global::GlobalInstance;
|
use interpreter::global::GlobalInstance;
|
||||||
use interpreter::memory::MemoryInstance;
|
use interpreter::memory::MemoryInstance;
|
||||||
@ -222,33 +225,33 @@ impl HostModuleBuilder {
|
|||||||
self
|
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));
|
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));
|
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));
|
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));
|
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.insert_global(name, global);
|
||||||
self
|
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.insert_memory(name, memory);
|
||||||
self
|
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.insert_table(name, table);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
@ -281,7 +284,7 @@ impl ImportResolver for HostModule {
|
|||||||
&self,
|
&self,
|
||||||
field_name: &str,
|
field_name: &str,
|
||||||
func_type: &FunctionType,
|
func_type: &FunctionType,
|
||||||
) -> Result<Rc<FuncInstance>, Error> {
|
) -> Result<FuncRef, Error> {
|
||||||
self.internal_instance.resolve_func(field_name, func_type)
|
self.internal_instance.resolve_func(field_name, func_type)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -289,7 +292,7 @@ impl ImportResolver for HostModule {
|
|||||||
&self,
|
&self,
|
||||||
field_name: &str,
|
field_name: &str,
|
||||||
global_type: &GlobalType,
|
global_type: &GlobalType,
|
||||||
) -> Result<Rc<GlobalInstance>, Error> {
|
) -> Result<GlobalRef, Error> {
|
||||||
self.internal_instance
|
self.internal_instance
|
||||||
.resolve_global(field_name, global_type)
|
.resolve_global(field_name, global_type)
|
||||||
}
|
}
|
||||||
@ -298,7 +301,7 @@ impl ImportResolver for HostModule {
|
|||||||
&self,
|
&self,
|
||||||
field_name: &str,
|
field_name: &str,
|
||||||
memory_type: &MemoryType,
|
memory_type: &MemoryType,
|
||||||
) -> Result<Rc<MemoryInstance>, Error> {
|
) -> Result<MemoryRef, Error> {
|
||||||
self.internal_instance
|
self.internal_instance
|
||||||
.resolve_memory(field_name, memory_type)
|
.resolve_memory(field_name, memory_type)
|
||||||
}
|
}
|
||||||
@ -307,7 +310,7 @@ impl ImportResolver for HostModule {
|
|||||||
&self,
|
&self,
|
||||||
field_name: &str,
|
field_name: &str,
|
||||||
table_type: &TableType,
|
table_type: &TableType,
|
||||||
) -> Result<Rc<TableInstance>, Error> {
|
) -> Result<TableRef, Error> {
|
||||||
self.internal_instance.resolve_table(field_name, table_type)
|
self.internal_instance.resolve_table(field_name, table_type)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -382,3 +385,20 @@ impl IntoReturnVal for () {
|
|||||||
None
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
use std::rc::Rc;
|
use interpreter::module::GlobalRef;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use elements::{FunctionType, GlobalType, MemoryType, TableType};
|
use elements::{FunctionType, GlobalType, MemoryType, TableType};
|
||||||
use interpreter::func::FuncInstance;
|
use interpreter::module::MemoryRef;
|
||||||
use interpreter::global::GlobalInstance;
|
use interpreter::module::FuncRef;
|
||||||
use interpreter::memory::MemoryInstance;
|
use interpreter::module::TableRef;
|
||||||
use interpreter::table::TableInstance;
|
|
||||||
use interpreter::Error;
|
use interpreter::Error;
|
||||||
|
|
||||||
pub struct Imports<'a> {
|
pub struct Imports<'a> {
|
||||||
@ -45,23 +44,23 @@ pub trait ImportResolver {
|
|||||||
&self,
|
&self,
|
||||||
field_name: &str,
|
field_name: &str,
|
||||||
func_type: &FunctionType,
|
func_type: &FunctionType,
|
||||||
) -> Result<Rc<FuncInstance>, Error>;
|
) -> Result<FuncRef, Error>;
|
||||||
|
|
||||||
fn resolve_global(
|
fn resolve_global(
|
||||||
&self,
|
&self,
|
||||||
field_name: &str,
|
field_name: &str,
|
||||||
global_type: &GlobalType,
|
global_type: &GlobalType,
|
||||||
) -> Result<Rc<GlobalInstance>, Error>;
|
) -> Result<GlobalRef, Error>;
|
||||||
|
|
||||||
fn resolve_memory(
|
fn resolve_memory(
|
||||||
&self,
|
&self,
|
||||||
field_name: &str,
|
field_name: &str,
|
||||||
memory_type: &MemoryType,
|
memory_type: &MemoryType,
|
||||||
) -> Result<Rc<MemoryInstance>, Error>;
|
) -> Result<MemoryRef, Error>;
|
||||||
|
|
||||||
fn resolve_table(
|
fn resolve_table(
|
||||||
&self,
|
&self,
|
||||||
field_name: &str,
|
field_name: &str,
|
||||||
table_type: &TableType,
|
table_type: &TableType,
|
||||||
) -> Result<Rc<TableInstance>, Error>;
|
) -> Result<TableRef, Error>;
|
||||||
}
|
}
|
||||||
|
@ -157,7 +157,7 @@ pub use self::program::ProgramInstance;
|
|||||||
pub use self::value::RuntimeValue;
|
pub use self::value::RuntimeValue;
|
||||||
pub use self::host::{HostModule, HostModuleBuilder, HostFunc, IntoReturnVal, FromArg};
|
pub use self::host::{HostModule, HostModuleBuilder, HostFunc, IntoReturnVal, FromArg};
|
||||||
pub use self::imports::{ImportResolver, Imports};
|
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::global::GlobalInstance;
|
||||||
pub use self::func::FuncInstance;
|
pub use self::func::FuncInstance;
|
||||||
pub use self::state::{HostState, StateKey};
|
pub use self::state::{HostState, StateKey};
|
||||||
|
@ -13,11 +13,16 @@ use interpreter::state::HostState;
|
|||||||
use validation::validate_module;
|
use validation::validate_module;
|
||||||
use common::{DEFAULT_MEMORY_INDEX, DEFAULT_TABLE_INDEX};
|
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 {
|
pub enum ExternVal {
|
||||||
Func(Rc<FuncInstance>),
|
Func(FuncRef),
|
||||||
Table(Rc<TableInstance>),
|
Table(TableRef),
|
||||||
Memory(Rc<MemoryInstance>),
|
Memory(MemoryRef),
|
||||||
Global(Rc<GlobalInstance>),
|
Global(GlobalRef),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Clone for ExternVal {
|
impl Clone for ExternVal {
|
||||||
@ -47,28 +52,28 @@ impl fmt::Debug for ExternVal {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ExternVal {
|
impl ExternVal {
|
||||||
pub fn as_func(&self) -> Option<Rc<FuncInstance>> {
|
pub fn as_func(&self) -> Option<FuncRef> {
|
||||||
match *self {
|
match *self {
|
||||||
ExternVal::Func(ref func) => Some(Rc::clone(func)),
|
ExternVal::Func(ref func) => Some(Rc::clone(func)),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_table(&self) -> Option<Rc<TableInstance>> {
|
pub fn as_table(&self) -> Option<TableRef> {
|
||||||
match *self {
|
match *self {
|
||||||
ExternVal::Table(ref table) => Some(Rc::clone(table)),
|
ExternVal::Table(ref table) => Some(Rc::clone(table)),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_memory(&self) -> Option<Rc<MemoryInstance>> {
|
pub fn as_memory(&self) -> Option<MemoryRef> {
|
||||||
match *self {
|
match *self {
|
||||||
ExternVal::Memory(ref memory) => Some(Rc::clone(memory)),
|
ExternVal::Memory(ref memory) => Some(Rc::clone(memory)),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_global(&self) -> Option<Rc<GlobalInstance>> {
|
pub fn as_global(&self) -> Option<GlobalRef> {
|
||||||
match *self {
|
match *self {
|
||||||
ExternVal::Global(ref global) => Some(Rc::clone(global)),
|
ExternVal::Global(ref global) => Some(Rc::clone(global)),
|
||||||
_ => None,
|
_ => None,
|
||||||
@ -79,10 +84,10 @@ impl ExternVal {
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct ModuleInstance {
|
pub struct ModuleInstance {
|
||||||
types: RefCell<Vec<Rc<FunctionType>>>,
|
types: RefCell<Vec<Rc<FunctionType>>>,
|
||||||
tables: RefCell<Vec<Rc<TableInstance>>>,
|
tables: RefCell<Vec<TableRef>>,
|
||||||
funcs: RefCell<Vec<Rc<FuncInstance>>>,
|
funcs: RefCell<Vec<FuncRef>>,
|
||||||
memories: RefCell<Vec<Rc<MemoryInstance>>>,
|
memories: RefCell<Vec<MemoryRef>>,
|
||||||
globals: RefCell<Vec<Rc<GlobalInstance>>>,
|
globals: RefCell<Vec<GlobalRef>>,
|
||||||
exports: RefCell<HashMap<String, ExternVal>>,
|
exports: RefCell<HashMap<String, ExternVal>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,19 +111,19 @@ impl ModuleInstance {
|
|||||||
instance
|
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()
|
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()
|
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()
|
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()
|
self.funcs.borrow().get(idx as usize).cloned()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -130,7 +135,7 @@ impl ModuleInstance {
|
|||||||
self.exports.borrow().get(name).cloned()
|
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);
|
self.funcs.borrow_mut().push(func);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,15 +143,15 @@ impl ModuleInstance {
|
|||||||
self.types.borrow_mut().push(func_type)
|
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)
|
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)
|
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)
|
self.globals.borrow_mut().push(global)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -498,7 +503,7 @@ impl ImportResolver for ModuleInstance {
|
|||||||
&self,
|
&self,
|
||||||
field_name: &str,
|
field_name: &str,
|
||||||
_func_type: &FunctionType,
|
_func_type: &FunctionType,
|
||||||
) -> Result<Rc<FuncInstance>, Error> {
|
) -> Result<FuncRef, Error> {
|
||||||
Ok(self.export_by_name(field_name)
|
Ok(self.export_by_name(field_name)
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| {
|
||||||
Error::Validation(format!("Export {} not found", field_name))
|
Error::Validation(format!("Export {} not found", field_name))
|
||||||
@ -513,7 +518,7 @@ impl ImportResolver for ModuleInstance {
|
|||||||
&self,
|
&self,
|
||||||
field_name: &str,
|
field_name: &str,
|
||||||
_global_type: &GlobalType,
|
_global_type: &GlobalType,
|
||||||
) -> Result<Rc<GlobalInstance>, Error> {
|
) -> Result<GlobalRef, Error> {
|
||||||
Ok(self.export_by_name(field_name)
|
Ok(self.export_by_name(field_name)
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| {
|
||||||
Error::Validation(format!("Export {} not found", field_name))
|
Error::Validation(format!("Export {} not found", field_name))
|
||||||
@ -528,7 +533,7 @@ impl ImportResolver for ModuleInstance {
|
|||||||
&self,
|
&self,
|
||||||
field_name: &str,
|
field_name: &str,
|
||||||
_memory_type: &MemoryType,
|
_memory_type: &MemoryType,
|
||||||
) -> Result<Rc<MemoryInstance>, Error> {
|
) -> Result<MemoryRef, Error> {
|
||||||
Ok(self.export_by_name(field_name)
|
Ok(self.export_by_name(field_name)
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| {
|
||||||
Error::Validation(format!("Export {} not found", field_name))
|
Error::Validation(format!("Export {} not found", field_name))
|
||||||
@ -543,7 +548,7 @@ impl ImportResolver for ModuleInstance {
|
|||||||
&self,
|
&self,
|
||||||
field_name: &str,
|
field_name: &str,
|
||||||
_table_type: &TableType,
|
_table_type: &TableType,
|
||||||
) -> Result<Rc<TableInstance>, Error> {
|
) -> Result<TableRef, Error> {
|
||||||
Ok(self.export_by_name(field_name)
|
Ok(self.export_by_name(field_name)
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| {
|
||||||
Error::Validation(format!("Export {} not found", field_name))
|
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)
|
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)?;
|
let table = TableInstance::new(table_type)?;
|
||||||
Ok(Rc::new(table))
|
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)?;
|
let memory = MemoryInstance::new(&mem_type)?;
|
||||||
Ok(Rc::new(memory))
|
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());
|
let global = GlobalInstance::new(val, global_type.is_mutable());
|
||||||
Rc::new(global)
|
Rc::new(global)
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ use std::collections::HashMap;
|
|||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use elements::Module;
|
use elements::Module;
|
||||||
use interpreter::Error;
|
use interpreter::Error;
|
||||||
use interpreter::module::ModuleInstance;
|
use interpreter::module::{ModuleInstance, FuncRef};
|
||||||
use interpreter::func::FuncInstance;
|
use interpreter::func::FuncInstance;
|
||||||
use interpreter::host::HostModule;
|
use interpreter::host::HostModule;
|
||||||
use interpreter::value::RuntimeValue;
|
use interpreter::value::RuntimeValue;
|
||||||
@ -98,7 +98,7 @@ impl ProgramInstance {
|
|||||||
|
|
||||||
pub fn invoke_func<'a>(
|
pub fn invoke_func<'a>(
|
||||||
&mut self,
|
&mut self,
|
||||||
func_instance: Rc<FuncInstance>,
|
func_instance: FuncRef,
|
||||||
args: &[RuntimeValue],
|
args: &[RuntimeValue],
|
||||||
state: &'a mut HostState<'a>,
|
state: &'a mut HostState<'a>,
|
||||||
) -> Result<Option<RuntimeValue>, Error> {
|
) -> Result<Option<RuntimeValue>, Error> {
|
||||||
|
@ -8,6 +8,7 @@ use std::collections::{HashMap, VecDeque};
|
|||||||
use elements::{Opcode, BlockType, Local, FunctionType};
|
use elements::{Opcode, BlockType, Local, FunctionType};
|
||||||
use interpreter::Error;
|
use interpreter::Error;
|
||||||
use interpreter::module::ModuleInstance;
|
use interpreter::module::ModuleInstance;
|
||||||
|
use interpreter::module::FuncRef;
|
||||||
use interpreter::func::FuncInstance;
|
use interpreter::func::FuncInstance;
|
||||||
use interpreter::value::{
|
use interpreter::value::{
|
||||||
RuntimeValue, TryInto, WrapInto, TryTruncateInto, ExtendInto,
|
RuntimeValue, TryInto, WrapInto, TryTruncateInto, ExtendInto,
|
||||||
@ -27,7 +28,7 @@ pub struct FunctionContext {
|
|||||||
/// Is context initialized.
|
/// Is context initialized.
|
||||||
pub is_initialized: bool,
|
pub is_initialized: bool,
|
||||||
/// Internal function reference.
|
/// Internal function reference.
|
||||||
pub function: Rc<FuncInstance>,
|
pub function: FuncRef,
|
||||||
pub module: Rc<ModuleInstance>,
|
pub module: Rc<ModuleInstance>,
|
||||||
/// Function return type.
|
/// Function return type.
|
||||||
pub return_type: BlockType,
|
pub return_type: BlockType,
|
||||||
@ -48,7 +49,7 @@ pub enum InstructionOutcome {
|
|||||||
/// Branch to given frame.
|
/// Branch to given frame.
|
||||||
Branch(usize),
|
Branch(usize),
|
||||||
/// Execute function call.
|
/// Execute function call.
|
||||||
ExecuteCall(Rc<FuncInstance>),
|
ExecuteCall(FuncRef),
|
||||||
/// End current frame.
|
/// End current frame.
|
||||||
End,
|
End,
|
||||||
/// Return from current function block.
|
/// Return from current function block.
|
||||||
@ -60,7 +61,7 @@ enum RunResult {
|
|||||||
/// Function has returned (optional) value.
|
/// Function has returned (optional) value.
|
||||||
Return(Option<RuntimeValue>),
|
Return(Option<RuntimeValue>),
|
||||||
/// Function is calling other function.
|
/// Function is calling other function.
|
||||||
NestedCall(Rc<FuncInstance>),
|
NestedCall(FuncRef),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b: 'a> Interpreter<'a, 'b> {
|
impl<'a, 'b: 'a> Interpreter<'a, 'b> {
|
||||||
@ -998,7 +999,7 @@ impl<'a, 'b: 'a> Interpreter<'a, 'b> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl FunctionContext {
|
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 {
|
let module = match *function {
|
||||||
FuncInstance::Internal { ref module, .. } => Rc::clone(module),
|
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"),
|
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 (function_locals, module, function_return_type) = {
|
||||||
let module = match *function {
|
let module = match *function {
|
||||||
FuncInstance::Internal { ref module, .. } => Rc::clone(module),
|
FuncInstance::Internal { ref module, .. } => Rc::clone(module),
|
||||||
|
@ -1,18 +1,17 @@
|
|||||||
use std::u32;
|
use std::u32;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::rc::Rc;
|
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use elements::{ResizableLimits, TableType};
|
use elements::{ResizableLimits, TableType};
|
||||||
use interpreter::Error;
|
use interpreter::Error;
|
||||||
|
use interpreter::module::FuncRef;
|
||||||
use interpreter::module::check_limits;
|
use interpreter::module::check_limits;
|
||||||
use interpreter::func::FuncInstance;
|
|
||||||
|
|
||||||
/// Table instance.
|
/// Table instance.
|
||||||
pub struct TableInstance {
|
pub struct TableInstance {
|
||||||
/// Table limits.
|
/// Table limits.
|
||||||
limits: ResizableLimits,
|
limits: ResizableLimits,
|
||||||
/// Table memory buffer.
|
/// Table memory buffer.
|
||||||
buffer: RefCell<Vec<Option<Rc<FuncInstance>>>>,
|
buffer: RefCell<Vec<Option<FuncRef>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for TableInstance {
|
impl fmt::Debug for TableInstance {
|
||||||
@ -40,7 +39,7 @@ impl TableInstance {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Get the specific value in the table
|
/// 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 = self.buffer.borrow();
|
||||||
let buffer_len = buffer.len();
|
let buffer_len = buffer.len();
|
||||||
let table_elem = buffer.get(offset as usize).cloned().ok_or(
|
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.
|
/// 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 mut buffer = self.buffer.borrow_mut();
|
||||||
let buffer_len = buffer.len();
|
let buffer_len = buffer.len();
|
||||||
let table_elem = buffer.get_mut(offset as usize).ok_or(Error::Table(format!(
|
let table_elem = buffer.get_mut(offset as usize).ok_or(Error::Table(format!(
|
||||||
|
@ -118,7 +118,7 @@ impl ::std::fmt::Display for UserErrorWithCode {
|
|||||||
impl UserError for UserErrorWithCode {}
|
impl UserError for UserErrorWithCode {}
|
||||||
|
|
||||||
struct TestState {
|
struct TestState {
|
||||||
pub memory: Rc<MemoryInstance>,
|
pub memory: MemoryRef,
|
||||||
pub values: RefCell<Vec<i32>>,
|
pub values: RefCell<Vec<i32>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user