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
use crate::Error;
use leb128::write;
use std::io::Write;
#[derive(Copy, Clone, PartialEq)]
pub enum GlobalValue {
I32(i32),
I64(i64),
F32(f32),
F64(f64),
}
impl GlobalValue {
pub fn encode(self, writer: &mut impl Write) -> Result<usize, Error> {
let mut written = 0;
written += writer.write(&[self.into()])?;
written += match self {
GlobalValue::I32(v) => write::signed(writer, v as i64)?,
GlobalValue::I64(v) => write::signed(writer, v as i64)?,
GlobalValue::F32(v) => writer.write(&v.to_le_bytes())?,
GlobalValue::F64(v) => writer.write(&v.to_le_bytes())?,
};
Ok(written)
}
}
impl From<GlobalValue> for u8 {
fn from(other: GlobalValue) -> Self {
match other {
GlobalValue::I32(_) => 0x7F,
GlobalValue::I64(_) => 0x7E,
GlobalValue::F32(_) => 0x7D,
GlobalValue::F64(_) => 0x7C,
}
}
}
#[derive(Copy, Clone, PartialEq)]
pub struct GlobalDescriptor {
valtype: GlobalValue,
mutable: bool,
}
impl GlobalDescriptor {
pub fn new(valtype: GlobalValue, mutable: bool) -> Self {
Self { valtype, mutable }
}
pub(crate) fn encode(self, writer: &mut impl Write) -> Result<(), Error> {
self.valtype.encode(writer)?;
writer.write_all(&[self.mutable as u8])?;
Ok(())
}
pub(crate) fn is_mut(&self) -> bool {
self.mutable
}
}