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
/// WASM value types and operations
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Value {
I32(i32),
I64(i64),
F32(f32),
F64(f64),
/// A `funcref`. `Some(func_idx)` is a reference to a function in the
/// module's function index space; `None` is the null reference.
FuncRef(Option<u32>),
/// An `externref`. The interpreter has no host object table yet, so an
/// external reference is modeled as an opaque handle: `Some(handle)` is a
/// non-null reference, `None` is null. Handles round-trip through locals,
/// globals, tables, and params unchanged.
ExternRef(Option<u32>),
}
impl Value {
pub fn from_i32(v: i32) -> Self {
Value::I32(v)
}
pub fn from_i64(v: i64) -> Self {
Value::I64(v)
}
pub fn from_f32(v: f32) -> Self {
Value::F32(v)
}
pub fn from_f64(v: f64) -> Self {
Value::F64(v)
}
/// The null `funcref`.
pub fn null_funcref() -> Self {
Value::FuncRef(None)
}
/// The null `externref`.
pub fn null_externref() -> Self {
Value::ExternRef(None)
}
/// True for a reference value (`funcref`/`externref`), false for a number.
pub fn is_ref(&self) -> bool {
matches!(self, Value::FuncRef(_) | Value::ExternRef(_))
}
/// True when this is a null reference. Returns `false` for numbers.
pub fn is_null_ref(&self) -> bool {
matches!(self, Value::FuncRef(None) | Value::ExternRef(None))
}
/// The function index of a `funcref`, if this is a non-null `funcref`.
pub fn as_func_idx(&self) -> Option<u32> {
match self {
Value::FuncRef(f) => *f,
_ => None,
}
}
}