mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-09 06:08:29 +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)]
|
#[derive(Clone)]
|
||||||
pub struct Global {
|
pub struct Global {
|
||||||
store: Store,
|
store: Store,
|
||||||
exported: ExportGlobal,
|
global: Arc<RuntimeGlobal>,
|
||||||
// This should not be here;
|
|
||||||
// it should be accessed through `exported`
|
|
||||||
// vm_global_definition: Arc<UnsafeCell<VMGlobalDefinition>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Global {
|
impl Global {
|
||||||
@@ -56,19 +53,15 @@ impl Global {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let definition = global.vmglobal();
|
let definition = global.vmglobal();
|
||||||
let exported = ExportGlobal {
|
|
||||||
definition,
|
|
||||||
from: Arc::new(global),
|
|
||||||
};
|
|
||||||
Ok(Global {
|
Ok(Global {
|
||||||
store: store.clone(),
|
store: store.clone(),
|
||||||
exported,
|
global: Arc::new(global),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the [`GlobalType`] of the `Global`.
|
/// Returns the [`GlobalType`] of the `Global`.
|
||||||
pub fn ty(&self) -> &GlobalType {
|
pub fn ty(&self) -> &GlobalType {
|
||||||
self.exported.from.ty()
|
self.global.ty()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the [`Store`] where the `Global` belongs.
|
/// Returns the [`Store`] where the `Global` belongs.
|
||||||
@@ -79,7 +72,7 @@ impl Global {
|
|||||||
/// Retrieves the current value [`Val`] that the Global has.
|
/// Retrieves the current value [`Val`] that the Global has.
|
||||||
pub fn get(&self) -> Val {
|
pub fn get(&self) -> Val {
|
||||||
unsafe {
|
unsafe {
|
||||||
let definition = self.exported.from.get();
|
let definition = self.global.get();
|
||||||
match self.ty().ty {
|
match self.ty().ty {
|
||||||
ValType::I32 => Val::from(*definition.as_i32()),
|
ValType::I32 => Val::from(*definition.as_i32()),
|
||||||
ValType::I64 => Val::from(*definition.as_i64()),
|
ValType::I64 => Val::from(*definition.as_i64()),
|
||||||
@@ -114,7 +107,7 @@ impl Global {
|
|||||||
return Err(RuntimeError::new("cross-`Store` values are not supported"));
|
return Err(RuntimeError::new("cross-`Store` values are not supported"));
|
||||||
}
|
}
|
||||||
unsafe {
|
unsafe {
|
||||||
let definition = self.exported.from.get_mut();
|
let definition = self.global.get_mut();
|
||||||
match val {
|
match val {
|
||||||
Val::I32(i) => *definition.as_i32_mut() = i,
|
Val::I32(i) => *definition.as_i32_mut() = i,
|
||||||
Val::I64(i) => *definition.as_i64_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 {
|
pub(crate) fn from_export(store: &Store, wasmer_export: ExportGlobal) -> Global {
|
||||||
Global {
|
Global {
|
||||||
store: store.clone(),
|
store: store.clone(),
|
||||||
exported: wasmer_export,
|
global: wasmer_export.from.clone(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns whether or not these two globals refer to the same data.
|
/// Returns whether or not these two globals refer to the same data.
|
||||||
pub fn same(&self, other: &Global) -> bool {
|
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 {
|
impl<'a> Exportable<'a> for Global {
|
||||||
fn to_export(&self) -> Export {
|
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> {
|
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
|
/// This function allows to match all imports of a `ModuleInfo` with concrete definitions provided by
|
||||||
/// concrete definitions provided by a `Resolver`.
|
/// a `Resolver`.
|
||||||
///
|
///
|
||||||
/// If all imports are satisfied, it returns an `Imports` instance
|
/// If all imports are satisfied returns an `Imports` instance required for a module instantiation.
|
||||||
/// required for a module instantiation.
|
|
||||||
pub fn resolve_imports(
|
pub fn resolve_imports(
|
||||||
module: &ModuleInfo,
|
module: &ModuleInfo,
|
||||||
resolver: &dyn Resolver,
|
resolver: &dyn Resolver,
|
||||||
@@ -214,7 +213,7 @@ pub fn resolve_imports(
|
|||||||
|
|
||||||
Export::Global(ref g) => {
|
Export::Global(ref g) => {
|
||||||
global_imports.push(VMGlobalImport {
|
global_imports.push(VMGlobalImport {
|
||||||
definition: g.definition,
|
definition: g.from.vmglobal(),
|
||||||
from: g.from.clone(),
|
from: g.from.clone(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,8 +4,7 @@
|
|||||||
use crate::global::Global;
|
use crate::global::Global;
|
||||||
use crate::memory::{Memory, MemoryStyle};
|
use crate::memory::{Memory, MemoryStyle};
|
||||||
use crate::table::{Table, TableStyle};
|
use crate::table::{Table, TableStyle};
|
||||||
use crate::vmcontext::{VMContext, VMFunctionBody, VMFunctionKind, VMGlobalDefinition};
|
use crate::vmcontext::{VMContext, VMFunctionBody, VMFunctionKind};
|
||||||
use std::ptr::NonNull;
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use wasm_common::{FunctionType, MemoryType, TableType};
|
use wasm_common::{FunctionType, MemoryType, TableType};
|
||||||
|
|
||||||
@@ -136,8 +135,6 @@ impl From<ExportMemory> for Export {
|
|||||||
/// A global export value.
|
/// A global export value.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct ExportGlobal {
|
pub struct ExportGlobal {
|
||||||
/// The address of the global storage.
|
|
||||||
pub definition: NonNull<VMGlobalDefinition>,
|
|
||||||
/// The global declaration, used for compatibility checking.
|
/// The global declaration, used for compatibility checking.
|
||||||
pub from: Arc<Global>,
|
pub from: Arc<Global>,
|
||||||
}
|
}
|
||||||
@@ -156,7 +153,7 @@ unsafe impl Sync for ExportGlobal {}
|
|||||||
impl ExportGlobal {
|
impl ExportGlobal {
|
||||||
/// Returns whether or not the two `ExportGlobal`s refer to the same Global.
|
/// Returns whether or not the two `ExportGlobal`s refer to the same Global.
|
||||||
pub fn same(&self, other: &Self) -> bool {
|
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()
|
ExportMemory { from }.into()
|
||||||
}
|
}
|
||||||
ExportIndex::Global(index) => {
|
ExportIndex::Global(index) => {
|
||||||
let (definition, from) = {
|
let from = {
|
||||||
if let Some(def_index) = self.module.local_global_index(*index) {
|
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 {
|
} else {
|
||||||
let import = self.imported_global(*index);
|
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