mirror of
https://github.com/fluencelabs/parity-wasm
synced 2025-06-24 04:01:57 +00:00
Move out GlobalInstance.
This commit is contained in:
30
src/interpreter/global.rs
Normal file
30
src/interpreter/global.rs
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
use std::cell::Cell;
|
||||||
|
use interpreter::value::RuntimeValue;
|
||||||
|
use interpreter::Error;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct GlobalInstance {
|
||||||
|
val: Cell<RuntimeValue>,
|
||||||
|
mutable: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GlobalInstance {
|
||||||
|
pub fn new(val: RuntimeValue, mutable: bool) -> GlobalInstance {
|
||||||
|
GlobalInstance {
|
||||||
|
val: Cell::new(val),
|
||||||
|
mutable,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set(&self, val: RuntimeValue) -> Result<(), Error> {
|
||||||
|
if !self.mutable {
|
||||||
|
return Err(Error::Validation("Can't set immutable global".into()));
|
||||||
|
}
|
||||||
|
self.val.set(val);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get(&self) -> RuntimeValue {
|
||||||
|
self.val.get()
|
||||||
|
}
|
||||||
|
}
|
@ -3,7 +3,8 @@ use std::rc::Rc;
|
|||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use elements::{FunctionType, ValueType, GlobalType, MemoryType, TableType};
|
use elements::{FunctionType, ValueType, GlobalType, MemoryType, TableType};
|
||||||
use interpreter::store::{ExternVal, ModuleInstance, GlobalInstance, FuncInstance};
|
use interpreter::store::{ExternVal, ModuleInstance, FuncInstance};
|
||||||
|
use interpreter::global::GlobalInstance;
|
||||||
use interpreter::memory::MemoryInstance;
|
use interpreter::memory::MemoryInstance;
|
||||||
use interpreter::table::TableInstance;
|
use interpreter::table::TableInstance;
|
||||||
use interpreter::value::RuntimeValue;
|
use interpreter::value::RuntimeValue;
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use elements::{FunctionType, GlobalType, MemoryType, TableType};
|
use elements::{FunctionType, GlobalType, MemoryType, TableType};
|
||||||
use interpreter::store::{FuncInstance, GlobalInstance};
|
use interpreter::store::FuncInstance;
|
||||||
|
use interpreter::global::GlobalInstance;
|
||||||
use interpreter::memory::MemoryInstance;
|
use interpreter::memory::MemoryInstance;
|
||||||
use interpreter::table::TableInstance;
|
use interpreter::table::TableInstance;
|
||||||
use interpreter::Error;
|
use interpreter::Error;
|
||||||
|
@ -140,6 +140,7 @@ mod value;
|
|||||||
mod store;
|
mod store;
|
||||||
mod host;
|
mod host;
|
||||||
mod imports;
|
mod imports;
|
||||||
|
mod global;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
@ -151,4 +152,5 @@ pub use self::program::ProgramInstance;
|
|||||||
pub use self::value::RuntimeValue;
|
pub use self::value::RuntimeValue;
|
||||||
pub use self::host::{HostModule, HostModuleBuilder, Func1, AnyFunc, AsReturnVal, FromArg};
|
pub use self::host::{HostModule, HostModuleBuilder, Func1, AnyFunc, AsReturnVal, FromArg};
|
||||||
pub use self::imports::{ImportResolver, Imports};
|
pub use self::imports::{ImportResolver, Imports};
|
||||||
pub use self::store::{FuncInstance, ModuleInstance, GlobalInstance};
|
pub use self::store::{FuncInstance, ModuleInstance};
|
||||||
|
pub use self::global::GlobalInstance;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::cell::{Cell, RefCell};
|
use std::cell::RefCell;
|
||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
@ -9,6 +9,7 @@ use interpreter::{Error, MemoryInstance, RuntimeValue, TableInstance};
|
|||||||
use interpreter::runner::{prepare_function_args, FunctionContext, Interpreter};
|
use interpreter::runner::{prepare_function_args, FunctionContext, Interpreter};
|
||||||
use interpreter::host::AnyFunc;
|
use interpreter::host::AnyFunc;
|
||||||
use interpreter::imports::{ImportResolver, Imports};
|
use interpreter::imports::{ImportResolver, Imports};
|
||||||
|
use interpreter::global::GlobalInstance;
|
||||||
use validation::validate_module;
|
use validation::validate_module;
|
||||||
use common::{DEFAULT_FRAME_STACK_LIMIT, DEFAULT_MEMORY_INDEX, DEFAULT_TABLE_INDEX,
|
use common::{DEFAULT_FRAME_STACK_LIMIT, DEFAULT_MEMORY_INDEX, DEFAULT_TABLE_INDEX,
|
||||||
DEFAULT_VALUE_STACK_LIMIT};
|
DEFAULT_VALUE_STACK_LIMIT};
|
||||||
@ -147,34 +148,6 @@ pub struct FuncBody {
|
|||||||
pub labels: HashMap<usize, usize>,
|
pub labels: HashMap<usize, usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct GlobalInstance {
|
|
||||||
val: Cell<RuntimeValue>,
|
|
||||||
mutable: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl GlobalInstance {
|
|
||||||
pub fn new(val: RuntimeValue, mutable: bool) -> GlobalInstance {
|
|
||||||
GlobalInstance {
|
|
||||||
val: Cell::new(val),
|
|
||||||
mutable,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set(&self, val: RuntimeValue) -> Result<(), Error> {
|
|
||||||
if !self.mutable {
|
|
||||||
// TODO: better error message
|
|
||||||
return Err(Error::Validation("Can't set immutable global".into()));
|
|
||||||
}
|
|
||||||
self.val.set(val);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get(&self) -> RuntimeValue {
|
|
||||||
self.val.get()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Default, Debug)]
|
#[derive(Default, Debug)]
|
||||||
pub struct ModuleInstance {
|
pub struct ModuleInstance {
|
||||||
types: RefCell<Vec<Rc<FunctionType>>>,
|
types: RefCell<Vec<Rc<FunctionType>>>,
|
||||||
|
Reference in New Issue
Block a user