diff --git a/src/interpreter/global.rs b/src/interpreter/global.rs new file mode 100644 index 0000000..572dab7 --- /dev/null +++ b/src/interpreter/global.rs @@ -0,0 +1,30 @@ +use std::cell::Cell; +use interpreter::value::RuntimeValue; +use interpreter::Error; + +#[derive(Debug)] +pub struct GlobalInstance { + val: Cell, + 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() + } +} diff --git a/src/interpreter/host.rs b/src/interpreter/host.rs index 939e873..0c1add7 100644 --- a/src/interpreter/host.rs +++ b/src/interpreter/host.rs @@ -3,7 +3,8 @@ use std::rc::Rc; use std::marker::PhantomData; use std::collections::HashMap; 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::table::TableInstance; use interpreter::value::RuntimeValue; diff --git a/src/interpreter/imports.rs b/src/interpreter/imports.rs index d62aaea..70e3c1d 100644 --- a/src/interpreter/imports.rs +++ b/src/interpreter/imports.rs @@ -1,7 +1,8 @@ use std::rc::Rc; use std::collections::HashMap; 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::table::TableInstance; use interpreter::Error; diff --git a/src/interpreter/mod.rs b/src/interpreter/mod.rs index 5b983fe..ffafe4b 100644 --- a/src/interpreter/mod.rs +++ b/src/interpreter/mod.rs @@ -140,6 +140,7 @@ mod value; mod store; mod host; mod imports; +mod global; #[cfg(test)] mod tests; @@ -151,4 +152,5 @@ pub use self::program::ProgramInstance; pub use self::value::RuntimeValue; pub use self::host::{HostModule, HostModuleBuilder, Func1, AnyFunc, AsReturnVal, FromArg}; pub use self::imports::{ImportResolver, Imports}; -pub use self::store::{FuncInstance, ModuleInstance, GlobalInstance}; +pub use self::store::{FuncInstance, ModuleInstance}; +pub use self::global::GlobalInstance; diff --git a/src/interpreter/store.rs b/src/interpreter/store.rs index db99834..931d3e2 100644 --- a/src/interpreter/store.rs +++ b/src/interpreter/store.rs @@ -1,5 +1,5 @@ use std::rc::Rc; -use std::cell::{Cell, RefCell}; +use std::cell::RefCell; use std::any::Any; use std::fmt; use std::collections::HashMap; @@ -9,6 +9,7 @@ use interpreter::{Error, MemoryInstance, RuntimeValue, TableInstance}; use interpreter::runner::{prepare_function_args, FunctionContext, Interpreter}; use interpreter::host::AnyFunc; use interpreter::imports::{ImportResolver, Imports}; +use interpreter::global::GlobalInstance; use validation::validate_module; use common::{DEFAULT_FRAME_STACK_LIMIT, DEFAULT_MEMORY_INDEX, DEFAULT_TABLE_INDEX, DEFAULT_VALUE_STACK_LIMIT}; @@ -147,34 +148,6 @@ pub struct FuncBody { pub labels: HashMap, } -#[derive(Debug)] -pub struct GlobalInstance { - val: Cell, - 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)] pub struct ModuleInstance { types: RefCell>>,