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;
#[derive(Clone)]
pub struct Global {
new_global: new::wasmer::Global,
}
impl Global {
pub fn new(value: Value) -> Self {
Self {
new_global: new::wasmer::Global::new(&get_global_store(), value),
}
}
pub fn new_mutable(value: Value) -> Self {
Self {
new_global: new::wasmer::Global::new_mut(&get_global_store(), value),
}
}
pub fn descriptor(&self) -> GlobalDescriptor {
self.new_global.ty().into()
}
pub fn set(&self, value: Value) {
self.new_global.set(value).unwrap()
}
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(
Box::leak(Box::<Global>::new(global.into())),
),
_ => Err(ExportError::IncompatibleType),
}
}
}