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
use crate::{BinaryReader, ComponentExternalKind, ExternalKind, FromReader, Result};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ComponentOuterAliasKind {
CoreModule,
CoreType,
Type,
Component,
}
#[derive(Debug, Clone)]
pub enum ComponentAlias<'a> {
InstanceExport {
kind: ComponentExternalKind,
instance_index: u32,
name: &'a str,
},
CoreInstanceExport {
kind: ExternalKind,
instance_index: u32,
name: &'a str,
},
Outer {
kind: ComponentOuterAliasKind,
count: u32,
index: u32,
},
}
pub type ComponentAliasSectionReader<'a> = crate::SectionLimited<'a, ComponentAlias<'a>>;
impl<'a> FromReader<'a> for ComponentAlias<'a> {
fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
let offset = reader.original_position();
let byte1 = reader.read_u8()?;
let byte2 = if byte1 == 0x00 {
Some(reader.read_u8()?)
} else {
None
};
Ok(match reader.read_u8()? {
0x00 => ComponentAlias::InstanceExport {
kind: ComponentExternalKind::from_bytes(byte1, byte2, offset)?,
instance_index: reader.read_var_u32()?,
name: reader.read_string()?,
},
0x01 => ComponentAlias::CoreInstanceExport {
kind: BinaryReader::external_kind_from_byte(
byte2.ok_or_else(|| {
BinaryReader::invalid_leading_byte_error(
byte1,
"core instance export kind",
offset,
)
})?,
offset,
)?,
instance_index: reader.read_var_u32()?,
name: reader.read_string()?,
},
0x02 => ComponentAlias::Outer {
kind: component_outer_alias_kind_from_bytes(byte1, byte2, offset)?,
count: reader.read_var_u32()?,
index: reader.read_var_u32()?,
},
x => reader.invalid_leading_byte(x, "alias")?,
})
}
}
fn component_outer_alias_kind_from_bytes(
byte1: u8,
byte2: Option<u8>,
offset: usize,
) -> Result<ComponentOuterAliasKind> {
Ok(match byte1 {
0x00 => match byte2.unwrap() {
0x10 => ComponentOuterAliasKind::CoreType,
0x11 => ComponentOuterAliasKind::CoreModule,
x => {
return Err(BinaryReader::invalid_leading_byte_error(
x,
"component outer alias kind",
offset + 1,
))
}
},
0x03 => ComponentOuterAliasKind::Type,
0x04 => ComponentOuterAliasKind::Component,
x => {
return Err(BinaryReader::invalid_leading_byte_error(
x,
"component outer alias kind",
offset,
))
}
})
}