mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-07 05:08:19 +00:00
Simplified globals a bit further
This commit is contained in:
26
lib/api/src/externals/global.rs
vendored
26
lib/api/src/externals/global.rs
vendored
@@ -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 {
|
||||
@@ -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.
|
||||
@@ -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()),
|
||||
@@ -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,
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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> {
|
||||
|
||||
@@ -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,
|
||||
@@ -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(),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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};
|
||||
|
||||
@@ -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>,
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user