use crate::js::export::VMGlobal;
use crate::js::exports::{ExportError, Exportable};
use crate::js::externals::Extern;
use crate::js::store::{AsStoreMut, AsStoreRef, InternalStoreHandle, StoreHandle};
use crate::js::value::Value;
use crate::js::wasm_bindgen_polyfill::Global as JSGlobal;
use crate::js::GlobalType;
use crate::js::Mutability;
use crate::js::RuntimeError;
use wasm_bindgen::JsValue;
#[derive(Debug, Clone, PartialEq)]
pub struct Global {
pub(crate) handle: StoreHandle<VMGlobal>,
}
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> {
if !val.is_from_store(store) {
return Err(RuntimeError::new(
"cross-`WasmerEnv` values are not supported",
));
}
let global_ty = GlobalType {
mutability,
ty: val.ty(),
};
let descriptor = js_sys::Object::new();
let (type_str, value) = match val {
Value::I32(i) => ("i32", JsValue::from_f64(i as _)),
Value::I64(i) => ("i64", JsValue::from_f64(i as _)),
Value::F32(f) => ("f32", JsValue::from_f64(f as _)),
Value::F64(f) => ("f64", JsValue::from_f64(f)),
_ => unimplemented!("The type is not yet supported in the JS Global API"),
};
js_sys::Reflect::set(&descriptor, &"value".into(), &type_str.into())?;
js_sys::Reflect::set(
&descriptor,
&"mutable".into(),
&mutability.is_mutable().into(),
)?;
let js_global = JSGlobal::new(&descriptor, &value).unwrap();
let vm_global = VMGlobal::new(js_global, global_ty);
Ok(Self::from_vm_export(store, vm_global))
}
pub fn ty(&self, store: &impl AsStoreRef) -> GlobalType {
self.handle.get(store.as_store_ref().objects()).ty
}
pub fn get(&self, store: &impl AsStoreRef) -> Value {
unsafe {
let raw = self
.handle
.get(store.as_store_ref().objects())
.global
.value()
.as_f64()
.unwrap();
let ty = self.handle.get(store.as_store_ref().objects()).ty;
Value::from_raw(store, ty.ty, raw)
}
}
pub fn set(&self, store: &mut impl AsStoreMut, val: Value) -> Result<(), RuntimeError> {
if !val.is_from_store(store) {
return Err(RuntimeError::new(
"cross-`WasmerEnv` values are not supported",
));
}
let global_ty = self.ty(&store);
if global_ty.mutability == Mutability::Const {
return Err(RuntimeError::new("The global is immutable".to_owned()));
}
if val.ty() != global_ty.ty {
return Err(RuntimeError::new("The types don't match".to_owned()));
}
let new_value = match val {
Value::I32(i) => JsValue::from_f64(i as _),
Value::I64(i) => JsValue::from_f64(i as _),
Value::F32(f) => JsValue::from_f64(f as _),
Value::F64(f) => JsValue::from_f64(f),
_ => {
return Err(RuntimeError::new(
"The type is not yet supported in the JS Global API".to_owned(),
))
}
};
self.handle
.get_mut(store.objects_mut())
.global
.set_value(&new_value);
Ok(())
}
pub(crate) fn from_vm_export(store: &mut impl AsStoreMut, vm_global: VMGlobal) -> Self {
Self {
handle: StoreHandle::new(store.objects_mut(), vm_global),
}
}
pub(crate) fn from_vm_extern(
store: &mut impl AsStoreMut,
internal: InternalStoreHandle<VMGlobal>,
) -> Self {
Self {
handle: unsafe {
StoreHandle::from_internal(store.as_store_ref().objects().id(), internal)
},
}
}
pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool {
self.handle.store_id() == store.as_store_ref().objects().id()
}
}
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),
}
}
}