use crate::exports::{ExportError, Exportable};
use crate::store::{AsStoreMut, AsStoreRef};
use crate::value::Value;
use crate::vm::VMExtern;
use crate::vm::VMExternGlobal;
use crate::Extern;
use crate::GlobalType;
use crate::Mutability;
use crate::RuntimeError;
#[cfg(feature = "js")]
use crate::js::externals::global as global_impl;
#[cfg(feature = "jsc")]
use crate::jsc::externals::global as global_impl;
#[cfg(feature = "sys")]
use crate::sys::externals::global as global_impl;
#[derive(Debug, Clone, PartialEq)]
pub struct Global(pub(crate) global_impl::Global);
impl Global {
pub fn new(store: &mut impl AsStoreMut, val: Value) -> Self {
Self::from_value(store, val, Mutability::Const).unwrap()
}
pub fn new_mut(store: &mut impl AsStoreMut, val: Value) -> Self {
Self::from_value(store, val, Mutability::Var).unwrap()
}
fn from_value(
store: &mut impl AsStoreMut,
val: Value,
mutability: Mutability,
) -> Result<Self, RuntimeError> {
Ok(Self(global_impl::Global::from_value(
store, val, mutability,
)?))
}
pub fn ty(&self, store: &impl AsStoreRef) -> GlobalType {
self.0.ty(store)
}
pub fn get(&self, store: &mut impl AsStoreMut) -> Value {
self.0.get(store)
}
pub fn set(&self, store: &mut impl AsStoreMut, val: Value) -> Result<(), RuntimeError> {
self.0.set(store, val)
}
pub(crate) fn from_vm_extern(store: &mut impl AsStoreMut, vm_extern: VMExternGlobal) -> Self {
Self(global_impl::Global::from_vm_extern(store, vm_extern))
}
pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool {
self.0.is_from_store(store)
}
pub(crate) fn to_vm_extern(&self) -> VMExtern {
self.0.to_vm_extern()
}
}
impl std::cmp::Eq for Global {}
impl<'a> Exportable<'a> for Global {
fn get_self_from_extern(_extern: &'a Extern) -> Result<&'a Self, ExportError> {
match _extern {
Extern::Global(global) => Ok(global),
_ => Err(ExportError::IncompatibleType),
}
}
}