Skip to content

Commit

Permalink
Simplified globals a bit further
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Jul 23, 2020
1 parent 59715c1 commit 04f2250
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 29 deletions.
26 changes: 11 additions & 15 deletions lib/api/src/externals/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ use wasmer_vm::{Export, ExportGlobal, Global as RuntimeGlobal};
#[derive(Clone)]
pub struct Global {
store: Store,
exported: ExportGlobal,
// This should not be here;
// it should be accessed through `exported`
// vm_global_definition: Arc<UnsafeCell<VMGlobalDefinition>>,
global: Arc<RuntimeGlobal>,
}

impl Global {
Expand Down Expand Up @@ -56,19 +53,15 @@ impl Global {
};

let definition = global.vmglobal();
let exported = ExportGlobal {
definition,
from: Arc::new(global),
};
Ok(Global {
store: store.clone(),
exported,
global: Arc::new(global),
})
}

/// Returns the [`GlobalType`] of the `Global`.
pub fn ty(&self) -> &GlobalType {
self.exported.from.ty()
self.global.ty()
}

/// Returns the [`Store`] where the `Global` belongs.
Expand All @@ -79,7 +72,7 @@ impl Global {
/// Retrieves the current value [`Val`] that the Global has.
pub fn get(&self) -> Val {
unsafe {
let definition = self.exported.from.get();
let definition = self.global.get();
match self.ty().ty {
ValType::I32 => Val::from(*definition.as_i32()),
ValType::I64 => Val::from(*definition.as_i64()),
Expand Down Expand Up @@ -114,7 +107,7 @@ impl Global {
return Err(RuntimeError::new("cross-`Store` values are not supported"));
}
unsafe {
let definition = self.exported.from.get_mut();
let definition = self.global.get_mut();
match val {
Val::I32(i) => *definition.as_i32_mut() = i,
Val::I64(i) => *definition.as_i64_mut() = i,
Expand All @@ -129,13 +122,13 @@ impl Global {
pub(crate) fn from_export(store: &Store, wasmer_export: ExportGlobal) -> Global {
Global {
store: store.clone(),
exported: wasmer_export,
global: wasmer_export.from.clone(),
}
}

/// Returns whether or not these two globals refer to the same data.
pub fn same(&self, other: &Global) -> bool {
self.exported.same(&other.exported)
Arc::ptr_eq(&self.global, &other.global)
}
}

Expand All @@ -151,7 +144,10 @@ impl fmt::Debug for Global {

impl<'a> Exportable<'a> for Global {
fn to_export(&self) -> Export {
self.exported.clone().into()
ExportGlobal {
from: self.global.clone(),
}
.into()
}

fn get_self_from_extern(_extern: &'a Extern) -> Result<&'a Self, ExportError> {
Expand Down
9 changes: 4 additions & 5 deletions lib/engine/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,10 @@ fn get_extern_from_export(_module: &ModuleInfo, export: &Export) -> ExternType {
}
}

/// This function allows to match all imports of a `ModuleInfo` with
/// concrete definitions provided by a `Resolver`.
/// This function allows to match all imports of a `ModuleInfo` with concrete definitions provided by
/// a `Resolver`.
///
/// If all imports are satisfied, it returns an `Imports` instance
/// required for a module instantiation.
/// If all imports are satisfied returns an `Imports` instance required for a module instantiation.
pub fn resolve_imports(
module: &ModuleInfo,
resolver: &dyn Resolver,
Expand Down Expand Up @@ -214,7 +213,7 @@ pub fn resolve_imports(

Export::Global(ref g) => {
global_imports.push(VMGlobalImport {
definition: g.definition,
definition: g.from.vmglobal(),
from: g.from.clone(),
});
}
Expand Down
7 changes: 2 additions & 5 deletions lib/vm/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
use crate::global::Global;
use crate::memory::{Memory, MemoryStyle};
use crate::table::{Table, TableStyle};
use crate::vmcontext::{VMContext, VMFunctionBody, VMFunctionKind, VMGlobalDefinition};
use std::ptr::NonNull;
use crate::vmcontext::{VMContext, VMFunctionBody, VMFunctionKind};
use std::sync::Arc;
use wasm_common::{FunctionType, MemoryType, TableType};

Expand Down Expand Up @@ -136,8 +135,6 @@ impl From<ExportMemory> for Export {
/// A global export value.
#[derive(Debug, Clone)]
pub struct ExportGlobal {
/// The address of the global storage.
pub definition: NonNull<VMGlobalDefinition>,
/// The global declaration, used for compatibility checking.
pub from: Arc<Global>,
}
Expand All @@ -156,7 +153,7 @@ unsafe impl Sync for ExportGlobal {}
impl ExportGlobal {
/// Returns whether or not the two `ExportGlobal`s refer to the same Global.
pub fn same(&self, other: &Self) -> bool {
self.definition == other.definition && Arc::ptr_eq(&self.from, &other.from)
Arc::ptr_eq(&self.from, &other.from)
}
}

Expand Down
8 changes: 4 additions & 4 deletions lib/vm/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,15 +333,15 @@ impl Instance {
ExportMemory { from }.into()
}
ExportIndex::Global(index) => {
let (definition, from) = {
let from = {
if let Some(def_index) = self.module.local_global_index(*index) {
(self.global_ptr(def_index), self.globals[def_index].clone())
self.globals[def_index].clone()
} else {
let import = self.imported_global(*index);
(import.definition, import.from.clone())
import.from.clone()
}
};
ExportGlobal { definition, from }.into()
ExportGlobal { from }.into()
}
}
}
Expand Down

0 comments on commit 04f2250

Please sign in to comment.