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
use wasmi_core::TypedRawVal;
use crate::{
AsContext,
AsContextMut,
GlobalType,
Mutability,
Val,
core::CoreGlobal,
errors::GlobalError,
store::Stored,
};
define_handle! {
/// A Wasm global variable reference.
struct Global(u32, Stored) => CoreGlobal;
}
impl Global {
/// Creates a new global variable to the store.
///
/// # Panics
///
/// If `value` does not originate from `ctx`.
pub fn new(mut ctx: impl AsContextMut, value: Val, mutability: Mutability) -> Self {
let ty = GlobalType::new(value.ty(), mutability);
let Some(value) = Val::unwrap_raw_val(&value, ctx.as_context()) else {
panic!("value does not originate from `ctx`: {value:?}")
};
ctx.as_context_mut()
.store
.inner
.alloc_global(CoreGlobal::new(value, ty))
}
/// Returns the [`GlobalType`] of the global variable.
///
/// # Panics
///
/// Panics if `ctx` does not own this [`Global`].
pub fn ty(&self, ctx: impl AsContext) -> GlobalType {
ctx.as_context().store.inner.resolve_global(self).ty()
}
/// Sets a new value to the global variable.
///
/// # Errors
///
/// - If the global variable is immutable.
/// - If there is a type mismatch between the global variable and the new value.
///
/// # Panics
///
/// Panics if `ctx` does not own this `self` or `new_value`.
pub fn set(&self, mut ctx: impl AsContextMut, new_value: Val) -> Result<(), GlobalError> {
let ty = new_value.ty();
let Some(new_value) = new_value.unwrap_raw_val(ctx.as_context()) else {
panic!("new_value does not originate from `ctx`: {new_value:?}")
};
let new_value = TypedRawVal::new(ty, new_value);
ctx.as_context_mut()
.store
.inner
.resolve_global_mut(self)
.set(new_value)
}
/// Returns the current value of the global variable.
///
/// # Panics
///
/// Panics if `ctx` does not own this [`Global`].
pub fn get(&self, ctx: impl AsContext) -> Val {
let store = &ctx.as_context().store.inner;
let value = store.resolve_global(self).get();
Val::from_raw_parts(value.raw(), value.ty(), store)
}
}