wasmparser/readers/component/
aliases.rs1use crate::{BinaryReader, ComponentExternalKind, ExternalKind, FromReader, Result};
2
3#[derive(Clone, Copy, Debug, Eq, PartialEq)]
5pub enum ComponentOuterAliasKind {
6 CoreModule,
8 CoreType,
10 Type,
12 Component,
14}
15
16#[derive(Debug, Clone, Eq, PartialEq)]
18pub enum ComponentAlias<'a> {
19 InstanceExport {
21 kind: ComponentExternalKind,
23 instance_index: u32,
25 name: &'a str,
27 },
28 CoreInstanceExport {
30 kind: ExternalKind,
32 instance_index: u32,
34 name: &'a str,
36 },
37 Outer {
39 kind: ComponentOuterAliasKind,
41 count: u32,
43 index: u32,
45 },
46}
47
48pub type ComponentAliasSectionReader<'a> = crate::SectionLimited<'a, ComponentAlias<'a>>;
50
51impl<'a> FromReader<'a> for ComponentAlias<'a> {
52 fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
53 let offset = reader.original_position();
55 let byte1 = reader.read_u8()?;
56 let byte2 = if byte1 == 0x00 {
57 Some(reader.read_u8()?)
58 } else {
59 None
60 };
61
62 Ok(match reader.read_u8()? {
63 0x00 => ComponentAlias::InstanceExport {
64 kind: ComponentExternalKind::from_bytes(byte1, byte2, offset)?,
65 instance_index: reader.read_var_u32()?,
66 name: reader.read_string()?,
67 },
68 0x01 => ComponentAlias::CoreInstanceExport {
69 kind: BinaryReader::external_kind_from_byte(
70 byte2.ok_or_else(|| {
71 BinaryReader::invalid_leading_byte_error(
72 byte1,
73 "core instance export kind",
74 offset,
75 )
76 })?,
77 offset,
78 )?,
79 instance_index: reader.read_var_u32()?,
80 name: reader.read_string()?,
81 },
82 0x02 => ComponentAlias::Outer {
83 kind: component_outer_alias_kind_from_bytes(byte1, byte2, offset)?,
84 count: reader.read_var_u32()?,
85 index: reader.read_var_u32()?,
86 },
87 x => reader.invalid_leading_byte(x, "alias")?,
88 })
89 }
90}
91
92fn component_outer_alias_kind_from_bytes(
93 byte1: u8,
94 byte2: Option<u8>,
95 offset: usize,
96) -> Result<ComponentOuterAliasKind> {
97 Ok(match byte1 {
98 0x00 => match byte2.unwrap() {
99 0x10 => ComponentOuterAliasKind::CoreType,
100 0x11 => ComponentOuterAliasKind::CoreModule,
101 x => {
102 return Err(BinaryReader::invalid_leading_byte_error(
103 x,
104 "component outer alias kind",
105 offset + 1,
106 ))
107 }
108 },
109 0x03 => ComponentOuterAliasKind::Type,
110 0x04 => ComponentOuterAliasKind::Component,
111 x => {
112 return Err(BinaryReader::invalid_leading_byte_error(
113 x,
114 "component outer alias kind",
115 offset,
116 ))
117 }
118 })
119}