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
use crate::IRecordType;
use crate::IType;
use crate::IValue;
pub fn ser_type_size(ty: &IType) -> u32 {
const WASM_POINTER_SIZE: u32 = 4;
match ty {
IType::Boolean | IType::S8 | IType::U8 => 1,
IType::S16 | IType::U16 => 2,
IType::S32 | IType::U32 | IType::I32 | IType::F32 => 4,
IType::Record(_) => 4,
IType::String | IType::ByteArray | IType::Array(_) => 2 * WASM_POINTER_SIZE,
IType::S64 | IType::U64 | IType::I64 | IType::F64 => 8,
}
}
pub fn ser_value_size(value: &IValue) -> u32 {
match value {
IValue::Boolean(_) | IValue::S8(_) | IValue::U8(_) => 1,
IValue::S16(_) | IValue::U16(_) => 2,
IValue::S32(_) | IValue::U32(_) | IValue::F32(_) | IValue::I32(_) => 4,
IValue::S64(_) | IValue::U64(_) | IValue::F64(_) | IValue::I64(_) => 8,
IValue::String(_) | IValue::ByteArray(_) | IValue::Array(_) => 2 * 4,
IValue::Record(_) => 4,
}
}
pub fn record_size(record_type: &IRecordType) -> u32 {
record_type
.fields
.iter()
.map(|f| ser_type_size(&f.ty))
.sum()
}
pub fn type_tag_form_itype(itype: &IType) -> u32 {
const POINTER_CODE: u32 = 3; match itype {
IType::Boolean => 0, IType::U8 => 1, IType::U16 => 2, IType::U32 => 3, IType::U64 => 4, IType::S8 => 5, IType::S16 => 6, IType::S32 | IType::I32 => 7, IType::S64 | IType::I64 => 8, IType::F32 => 9, IType::F64 => 10, IType::ByteArray | IType::Array(_) | IType::Record(_) | IType::String => POINTER_CODE,
}
}
pub fn type_tag_form_ivalue(itype: &IValue) -> u32 {
const POINTER_CODE: u32 = 3; match itype {
IValue::Boolean(_) => 0, IValue::U8(_) => 1, IValue::U16(_) => 2, IValue::U32(_) => 3, IValue::U64(_) => 4, IValue::S8(_) => 5, IValue::S16(_) => 6, IValue::S32(_) | IValue::I32(_) => 7, IValue::S64(_) | IValue::I64(_) => 8, IValue::F32(_) => 9, IValue::F64(_) => 10, IValue::ByteArray(_) | IValue::Array(_) | IValue::Record(_) | IValue::String(_) => {
POINTER_CODE
}
}
}