1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
use crate::{
    error::ExportError,
    get_global_store, new,
    types::{GlobalDescriptor, Value},
};
use std::fmt;

/// A handle to a Wasm Global
#[derive(Clone)]
pub struct Global {
    new_global: new::wasmer::Global,
}

impl Global {
    /// Create a new `Global` value.
    ///
    /// # Example
    ///
    /// ```
    /// # use wasmer_runtime_core::{global::Global, types::Value};
    /// let global = Global::new(Value::I32(42));
    /// ```
    pub fn new(value: Value) -> Self {
        Self {
            new_global: new::wasmer::Global::new(&get_global_store(), value),
        }
    }

    /// Create a new, mutable `Global` value.
    ///
    /// # Example
    ///
    /// ```
    /// # use wasmer_runtime_core::{global::Global, types::Value};
    /// let global = Global::new_mutable(Value::I32(42));
    /// ```
    pub fn new_mutable(value: Value) -> Self {
        Self {
            new_global: new::wasmer::Global::new_mut(&get_global_store(), value),
        }
    }

    /// Get the [`GlobalDescriptor`] generated for this global.
    ///
    /// [`GlobalDescriptor`]: struct.GlobalDescriptor.html
    pub fn descriptor(&self) -> GlobalDescriptor {
        self.new_global.ty().into()
    }

    /// Set the value help by this global.
    ///
    /// This method will panic if the value is
    /// the wrong type.
    pub fn set(&self, value: Value) {
        self.new_global.set(value).unwrap()
    }

    /// Get the value held by this global.
    ///
    /// # Example
    ///
    /// ```
    /// # use wasmer_runtime_core::{global::Global, types::Value};
    /// let global = Global::new_mutable(Value::I32(42));
    /// assert_eq!(global.get(), Value::I32(42));
    ///
    /// global.set(Value::I32(7));
    /// assert_eq!(global.get(), Value::I32(7));
    /// ```
    pub fn get(&self) -> Value {
        self.new_global.get()
    }
}

impl fmt::Debug for Global {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        write!(formatter, "{:?}", &self.new_global)
    }
}

impl From<&new::wasmer::Global> for Global {
    fn from(new_global: &new::wasmer::Global) -> Self {
        Self {
            new_global: new_global.clone(),
        }
    }
}

impl<'a> new::wasmer::Exportable<'a> for Global {
    fn to_export(&self) -> new::wasmer_vm::Export {
        self.new_global.to_export()
    }

    fn get_self_from_extern(r#extern: &'a new::wasmer::Extern) -> Result<&'a Self, ExportError> {
        match r#extern {
            new::wasmer::Extern::Global(global) => Ok(
                // It's not ideal to call `Box::leak` here, but it
                // would introduce too much changes in the
                // `new::wasmer` API to support `Cow` or similar.
                Box::leak(Box::<Global>::new(global.into())),
            ),
            _ => Err(ExportError::IncompatibleType),
        }
    }
}