Move out GlobalInstance.

This commit is contained in:
Sergey Pepyakin
2017-12-13 18:19:42 +01:00
parent 5eaf2ca183
commit 075ed7d369
5 changed files with 39 additions and 32 deletions

30
src/interpreter/global.rs Normal file
View 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()
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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<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)]
pub struct ModuleInstance {
types: RefCell<Vec<Rc<FunctionType>>>,