mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-07 05:08:19 +00:00
Merge branch 'master' into feature/improve-thread-safety-of-core-types
# Conflicts: # lib/api/src/instance.rs # lib/engine-jit/src/artifact.rs # lib/engine-native/src/artifact.rs
This commit is contained in:
23
lib/api/src/externals/global.rs
vendored
23
lib/api/src/externals/global.rs
vendored
@@ -9,6 +9,12 @@ use std::fmt;
|
||||
use std::sync::Arc;
|
||||
use wasmer_vm::{Export, ExportGlobal, Global as RuntimeGlobal};
|
||||
|
||||
/// A WebAssembly `global` instance.
|
||||
///
|
||||
/// A global instance is the runtime representation of a global variable.
|
||||
/// It consists of an individual value and a flag indicating whether it is mutable.
|
||||
///
|
||||
/// Spec: https://webassembly.github.io/spec/core/exec/runtime.html#global-instances
|
||||
#[derive(Clone)]
|
||||
pub struct Global {
|
||||
store: Store,
|
||||
@@ -19,10 +25,12 @@ pub struct Global {
|
||||
}
|
||||
|
||||
impl Global {
|
||||
/// Create a new `Global` with the initial value [`Val`].
|
||||
pub fn new(store: &Store, val: Val) -> Global {
|
||||
Self::from_type(store, Mutability::Const, val).unwrap()
|
||||
}
|
||||
|
||||
/// Create a mutable `Global` with the initial value [`Val`].
|
||||
pub fn new_mut(store: &Store, val: Val) -> Global {
|
||||
Self::from_type(store, Mutability::Var, val).unwrap()
|
||||
}
|
||||
@@ -31,7 +39,10 @@ impl Global {
|
||||
if !val.comes_from_same_store(store) {
|
||||
return Err(RuntimeError::new("cross-`Store` globals are not supported"));
|
||||
}
|
||||
let from = Arc::new(RuntimeGlobal::new(GlobalType { mutability, ty: val.ty() }));
|
||||
let from = Arc::new(RuntimeGlobal::new(GlobalType {
|
||||
mutability,
|
||||
ty: val.ty(),
|
||||
}));
|
||||
unsafe { from.set_unchecked(val).unwrap() };
|
||||
let definition = from.vmglobal();
|
||||
let exported = ExportGlobal { definition, from };
|
||||
@@ -41,18 +52,28 @@ impl Global {
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns the [`GlobalType`] of the `Global`.
|
||||
pub fn ty(&self) -> &GlobalType {
|
||||
self.exported.from.ty()
|
||||
}
|
||||
|
||||
/// Returns the [`Store`] where the `Global` belongs.
|
||||
pub fn store(&self) -> &Store {
|
||||
&self.store
|
||||
}
|
||||
|
||||
/// Retrieves the current value [`Val`] that the Global has.
|
||||
pub fn get(&self) -> Val {
|
||||
self.exported.from.get()
|
||||
}
|
||||
|
||||
/// Sets a custom value [`Val`] to the runtime Global.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// This function will error if:
|
||||
/// * The global is not mutable
|
||||
/// * The type of the `Val` doesn't matches the Global type.
|
||||
pub fn set(&self, val: Val) -> Result<(), RuntimeError> {
|
||||
if !val.comes_from_same_store(&self.store) {
|
||||
return Err(RuntimeError::new("cross-`Store` values are not supported"));
|
||||
|
||||
Reference in New Issue
Block a user