Hide GlobalType

This commit is contained in:
Sergey Pepyakin 2018-01-22 16:23:07 +03:00
parent aae9a3c129
commit b323d004fe
8 changed files with 39 additions and 30 deletions

View File

@ -24,15 +24,10 @@ pub struct GlobalInstance {
impl GlobalInstance {
pub fn alloc(val: RuntimeValue, mutable: bool) -> GlobalRef {
let global = GlobalInstance::new(val, mutable);
GlobalRef(Rc::new(global))
}
fn new(val: RuntimeValue, mutable: bool) -> GlobalInstance {
GlobalInstance {
GlobalRef(Rc::new(GlobalInstance {
val: Cell::new(val),
mutable,
}
}))
}
pub fn set(&self, val: RuntimeValue) -> Result<(), Error> {

View File

@ -1,10 +1,11 @@
use std::collections::HashMap;
use parity_wasm::elements::{GlobalType, MemoryType, TableType};
use parity_wasm::elements::{MemoryType, TableType};
use global::GlobalRef;
use memory::MemoryRef;
use func::FuncRef;
use table::TableRef;
use module::ModuleRef;
use types::GlobalDescriptor;
use {Error, Signature};
pub trait ImportResolver {
@ -19,7 +20,7 @@ pub trait ImportResolver {
&self,
module_name: &str,
field_name: &str,
global_type: &GlobalType,
global_type: &GlobalDescriptor,
) -> Result<GlobalRef, Error>;
fn resolve_memory(
@ -86,7 +87,7 @@ impl<'a> ImportResolver for ImportsBuilder<'a> {
&self,
module_name: &str,
field_name: &str,
global_type: &GlobalType,
global_type: &GlobalDescriptor,
) -> Result<GlobalRef, Error> {
self.resolver(module_name).ok_or_else(||
Error::Instantiation(format!("Module {} not found", module_name))
@ -130,7 +131,7 @@ pub trait ModuleImportResolver {
fn resolve_global(
&self,
field_name: &str,
_global_type: &GlobalType,
_global_type: &GlobalDescriptor,
) -> Result<GlobalRef, Error> {
Err(Error::Instantiation(
format!("Export {} not found", field_name),
@ -177,7 +178,7 @@ impl ModuleImportResolver for ModuleRef {
fn resolve_global(
&self,
field_name: &str,
_global_type: &GlobalType,
_global_type: &GlobalDescriptor,
) -> Result<GlobalRef, Error> {
Ok(self.export_by_name(field_name)
.ok_or_else(|| {

View File

@ -10,7 +10,6 @@ extern crate byteorder;
use std::fmt;
use std::error;
use std::path::Path;
use std::collections::HashMap;
use parity_wasm::elements::Module;
@ -136,7 +135,7 @@ pub use self::imports::{ModuleImportResolver, ImportResolver, ImportsBuilder};
pub use self::module::{ModuleInstance, ModuleRef, ExternVal, NotStartedModuleRef};
pub use self::global::{GlobalInstance, GlobalRef};
pub use self::func::{FuncInstance, FuncRef};
pub use self::types::{Signature, ValueType};
pub use self::types::{Signature, ValueType, GlobalDescriptor};
pub struct LoadedModule {
labels: HashMap<usize, HashMap<usize, usize>>,

View File

@ -12,6 +12,7 @@ use table::TableRef;
use memory::MemoryRef;
use host::Externals;
use common::{DEFAULT_MEMORY_INDEX, DEFAULT_TABLE_INDEX};
use types::GlobalDescriptor;
#[derive(Clone, Debug)]
pub struct ModuleRef(Rc<ModuleInstance>);
@ -398,7 +399,8 @@ impl ModuleInstance {
ExternVal::Memory(memory)
}
External::Global(ref global_type) => {
let global = imports.resolve_global(module_name, field_name, global_type)?;
let global_descriptor = GlobalDescriptor::from_global_type(global_type);
let global = imports.resolve_global(module_name, field_name, &global_descriptor)?;
ExternVal::Global(global)
}
};

View File

@ -1,5 +1,5 @@
use parity_wasm::elements::{deserialize_buffer, MemoryType, TableType};
use parity_wasm::elements::{MemoryType, TableType};
use {
Error, Signature, Externals, FuncInstance, FuncRef, HostError, ImportsBuilder,
MemoryInstance, MemoryRef, TableInstance, TableRef, ModuleImportResolver, ModuleInstance, ModuleRef,

View File

@ -1,8 +1,8 @@
use parity_wasm::elements::{GlobalType, MemoryType, Module, TableType};
use parity_wasm::elements::{MemoryType, TableType};
use {
Error, Signature, FuncRef, GlobalInstance, GlobalRef, ImportsBuilder, MemoryInstance,
MemoryRef, ModuleImportResolver, ModuleInstance, NopExternals, RuntimeValue,
TableInstance, TableRef, LoadedModule, load_from_buffer,
TableInstance, TableRef, LoadedModule, load_from_buffer, GlobalDescriptor,
};
use std::fs::File;
@ -34,7 +34,7 @@ impl ModuleImportResolver for Env {
fn resolve_global(
&self,
field_name: &str,
_global_type: &GlobalType,
_global_type: &GlobalDescriptor,
) -> Result<GlobalRef, Error> {
match field_name {
"tableBase" => Ok(self.table_base.clone()),

View File

@ -1,4 +1,4 @@
use parity_wasm::elements::{FunctionType, ValueType as EValueType};
use parity_wasm::elements::{FunctionType, ValueType as EValueType, GlobalType};
#[derive(Debug, Clone, PartialEq)]
pub struct Signature {
@ -57,3 +57,25 @@ impl ValueType {
}
}
}
pub struct GlobalDescriptor {
value_type: ValueType,
mutable: bool,
}
impl GlobalDescriptor {
pub(crate) fn from_global_type(global_type: &GlobalType) -> GlobalDescriptor {
GlobalDescriptor {
value_type: ValueType::from_elements(global_type.content_type()),
mutable: global_type.is_mutable(),
}
}
pub fn value_type(&self) -> ValueType {
self.value_type
}
pub fn is_mutable(&self) -> bool {
self.mutable
}
}

View File

@ -42,16 +42,6 @@ pub struct ValidatedModule {
pub module: Module,
}
impl ValidatedModule {
pub fn module(&self) -> &Module {
&self.module
}
pub fn into_module(self) -> Module {
self.module
}
}
impl ::std::ops::Deref for ValidatedModule {
type Target = Module;
fn deref(&self) -> &Module {