swasmi/
global.rs

1use alloc::rc::Rc;
2use core::cell::Cell;
3use swasm::elements::ValueType as EValueType;
4use types::ValueType;
5use value::RuntimeValue;
6use Error;
7
8/// Reference to a global variable (See [`GlobalInstance`] for details).
9///
10/// This reference has a reference-counting semantics.
11///
12/// [`GlobalInstance`]: struct.GlobalInstance.html
13#[derive(Clone, Debug)]
14pub struct GlobalRef(Rc<GlobalInstance>);
15
16impl ::core::ops::Deref for GlobalRef {
17    type Target = GlobalInstance;
18    fn deref(&self) -> &GlobalInstance {
19        &self.0
20    }
21}
22
23/// Runtime representation of a global variable (or `global` for short).
24///
25/// Global contains a value of a specified type and flag which specifies whser this
26/// global are mutable or immutable. Neither type of the value nor immutability can't be changed
27/// after creation.
28///
29/// Attempt to change value of immutable global or to change type of
30/// the value (e.g. assign [`I32`] value to a global that was created with [`I64`] type) will lead to an error.
31///
32/// [`I32`]: enum.RuntimeValue.html#variant.I32
33/// [`I64`]: enum.RuntimeValue.html#variant.I64
34#[derive(Debug)]
35pub struct GlobalInstance {
36    val: Cell<RuntimeValue>,
37    mutable: bool,
38}
39
40impl GlobalInstance {
41    /// Allocate a global variable instance.
42    ///
43    /// Since it is possible to export only immutable globals,
44    /// users likely want to set `mutable` to `false`.
45    pub fn alloc(val: RuntimeValue, mutable: bool) -> GlobalRef {
46        GlobalRef(Rc::new(GlobalInstance {
47            val: Cell::new(val),
48            mutable,
49        }))
50    }
51
52    /// Change the value of this global variable.
53    ///
54    /// # Errors
55    ///
56    /// Returns `Err` if this global isn't mutable or if
57    /// type of `val` doesn't match global's type.
58    pub fn set(&self, val: RuntimeValue) -> Result<(), Error> {
59        if !self.mutable {
60            return Err(Error::Global(
61                "Attempt to change an immutable variable".into(),
62            ));
63        }
64        if self.value_type() != val.value_type() {
65            return Err(Error::Global("Attempt to change variable type".into()));
66        }
67        self.val.set(val);
68        Ok(())
69    }
70
71    /// Get the value of this global variable.
72    pub fn get(&self) -> RuntimeValue {
73        self.val.get()
74    }
75
76    /// Returns if this global variable is mutable.
77    ///
78    /// Note: Imported and/or exported globals are always immutable.
79    pub fn is_mutable(&self) -> bool {
80        self.mutable
81    }
82
83    /// Returns value type of this global variable.
84    pub fn value_type(&self) -> ValueType {
85        self.val.get().value_type()
86    }
87
88    pub(crate) fn elements_value_type(&self) -> EValueType {
89        self.value_type().into_elements()
90    }
91}