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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
use crate::{types::Val, GlobalType, WasmEdgeResult};
use wasmedge_sys as sys;
#[derive(Debug, Clone)]
pub struct Global {
pub(crate) inner: sys::Global,
pub(crate) name: Option<String>,
pub(crate) mod_name: Option<String>,
pub(crate) ty: GlobalType,
}
impl Global {
pub fn new(ty: GlobalType, init: Val) -> WasmEdgeResult<Self> {
let inner = sys::Global::create(&ty.clone().into(), init.into())?;
Ok(Self {
inner,
name: None,
mod_name: None,
ty,
})
}
pub fn name(&self) -> Option<&str> {
match &self.name {
Some(name) => Some(name.as_ref()),
None => None,
}
}
pub fn mod_name(&self) -> Option<&str> {
match &self.mod_name {
Some(mod_name) => Some(mod_name.as_ref()),
None => None,
}
}
pub fn ty(&self) -> &GlobalType {
&self.ty
}
pub fn get_value(&self) -> Val {
self.inner.get_value().into()
}
pub fn set_value(&mut self, val: Val) -> WasmEdgeResult<()> {
self.inner.set_value(val.into())?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
config::{CommonConfigOptions, ConfigBuilder},
error::{GlobalError, WasmEdgeError},
Executor, ImportObjectBuilder, Mutability, Statistics, Store, ValType,
};
#[test]
fn test_global_type() {
let global_ty = GlobalType::new(ValType::I32, Mutability::Const);
assert_eq!(global_ty.value_ty(), ValType::I32);
assert_eq!(global_ty.mutability(), Mutability::Const);
}
#[test]
#[allow(clippy::assertions_on_result_states)]
fn test_global_basic() {
let result = Global::new(
GlobalType::new(ValType::I32, Mutability::Const),
Val::I32(1314),
);
assert!(result.is_ok());
let global_const = result.unwrap();
let result = Global::new(
GlobalType::new(ValType::F32, Mutability::Var),
Val::F32(13.14),
);
assert!(result.is_ok());
let global_var = result.unwrap();
let result = ImportObjectBuilder::new()
.with_global("const-global", global_const)
.expect("failed to add const-global")
.with_global("var-global", global_var)
.expect("failed to add var-global")
.build("extern");
assert!(result.is_ok());
let import = result.unwrap();
let result = ConfigBuilder::new(CommonConfigOptions::default()).build();
assert!(result.is_ok());
let config = result.unwrap();
let result = Statistics::new();
assert!(result.is_ok());
let mut stat = result.unwrap();
let result = Executor::new(Some(&config), Some(&mut stat));
assert!(result.is_ok());
let mut executor = result.unwrap();
let result = Store::new();
assert!(result.is_ok());
let mut store = result.unwrap();
let result = store.register_import_module(&mut executor, &import);
assert!(result.is_ok());
let result = store.named_instance("extern");
assert!(result.is_ok());
let instance = result.unwrap();
let result = instance.global("const-global");
assert!(result.is_ok());
let mut const_global = result.unwrap();
assert!(const_global.name().is_some());
assert_eq!(const_global.name().unwrap(), "const-global");
assert!(const_global.mod_name().is_some());
assert_eq!(const_global.mod_name().unwrap(), "extern");
let ty = const_global.ty();
assert_eq!(ty.value_ty(), ValType::I32);
assert_eq!(ty.mutability(), Mutability::Const);
if let Val::I32(value) = const_global.get_value() {
assert_eq!(value, 1314);
}
let result = const_global.set_value(Val::I32(314));
assert!(result.is_err());
assert_eq!(
result.unwrap_err(),
Box::new(WasmEdgeError::Global(GlobalError::ModifyConst))
);
let result = store.named_instance("extern");
assert!(result.is_ok());
let instance = result.unwrap();
let result = instance.global("var-global");
assert!(result.is_ok());
let mut var_global = result.unwrap();
assert!(var_global.name().is_some());
assert_eq!(var_global.name().unwrap(), "var-global");
assert!(var_global.mod_name().is_some());
assert_eq!(var_global.mod_name().unwrap(), "extern");
let ty = var_global.ty();
assert_eq!(ty.value_ty(), ValType::F32);
assert_eq!(ty.mutability(), Mutability::Var);
if let Val::F32(value) = var_global.get_value() {
assert_eq!(value, 13.14);
}
let result = var_global.set_value(Val::F32(1.314));
assert!(result.is_ok());
let result = instance.global("var-global");
assert!(result.is_ok());
let var_global = result.unwrap();
if let Val::F32(value) = var_global.get_value() {
assert_eq!(value, 1.314);
}
}
}