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