wasmparser/readers/component/
instances.rs1use crate::limits::{MAX_WASM_INSTANTIATION_ARGS, MAX_WASM_INSTANTIATION_EXPORTS};
2use crate::{
3 BinaryReader, ComponentExport, ComponentExternalKind, Export, FromReader, Result,
4 SectionLimited,
5};
6
7#[derive(Debug, Copy, Clone, PartialEq, Eq)]
9pub enum InstantiationArgKind {
10 Instance,
12}
13
14#[derive(Debug, Clone)]
16pub struct InstantiationArg<'a> {
17 pub name: &'a str,
19 pub kind: InstantiationArgKind,
21 pub index: u32,
23}
24
25#[derive(Debug, Clone)]
27pub enum Instance<'a> {
28 Instantiate {
30 module_index: u32,
32 args: Box<[InstantiationArg<'a>]>,
34 },
35 FromExports(Box<[Export<'a>]>),
37}
38
39pub type InstanceSectionReader<'a> = SectionLimited<'a, Instance<'a>>;
52
53impl<'a> FromReader<'a> for Instance<'a> {
54 fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
55 Ok(match reader.read_u8()? {
56 0x00 => Instance::Instantiate {
57 module_index: reader.read_var_u32()?,
58 args: reader
59 .read_iter(MAX_WASM_INSTANTIATION_ARGS, "core instantiation arguments")?
60 .collect::<Result<_>>()?,
61 },
62 0x01 => Instance::FromExports(
63 reader
64 .read_iter(MAX_WASM_INSTANTIATION_ARGS, "core instantiation arguments")?
65 .collect::<Result<_>>()?,
66 ),
67 x => return reader.invalid_leading_byte(x, "core instance"),
68 })
69 }
70}
71
72impl<'a> FromReader<'a> for InstantiationArg<'a> {
73 fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
74 Ok(InstantiationArg {
75 name: reader.read()?,
76 kind: reader.read()?,
77 index: reader.read()?,
78 })
79 }
80}
81
82impl<'a> FromReader<'a> for InstantiationArgKind {
83 fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
84 Ok(match reader.read_u8()? {
85 0x12 => InstantiationArgKind::Instance,
86 x => return reader.invalid_leading_byte(x, "instantiation arg kind"),
87 })
88 }
89}
90
91#[derive(Debug, Clone)]
93pub struct ComponentInstantiationArg<'a> {
94 pub name: &'a str,
96 pub kind: ComponentExternalKind,
98 pub index: u32,
100}
101
102#[derive(Debug, Clone)]
104pub enum ComponentInstance<'a> {
105 Instantiate {
107 component_index: u32,
109 args: Box<[ComponentInstantiationArg<'a>]>,
111 },
112 FromExports(Box<[ComponentExport<'a>]>),
114}
115
116pub type ComponentInstanceSectionReader<'a> = SectionLimited<'a, ComponentInstance<'a>>;
129
130impl<'a> FromReader<'a> for ComponentInstance<'a> {
131 fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
132 Ok(match reader.read_u8()? {
133 0x00 => ComponentInstance::Instantiate {
134 component_index: reader.read_var_u32()?,
135 args: reader
136 .read_iter(MAX_WASM_INSTANTIATION_ARGS, "instantiation arguments")?
137 .collect::<Result<_>>()?,
138 },
139 0x01 => ComponentInstance::FromExports(
140 (0..reader.read_size(MAX_WASM_INSTANTIATION_EXPORTS, "instantiation exports")?)
141 .map(|_| {
142 Ok(ComponentExport {
143 name: reader.read()?,
144 url: "",
145 kind: reader.read()?,
146 index: reader.read()?,
147 ty: None,
148 })
149 })
150 .collect::<Result<_>>()?,
151 ),
152 x => return reader.invalid_leading_byte(x, "instance"),
153 })
154 }
155}
156impl<'a> FromReader<'a> for ComponentInstantiationArg<'a> {
157 fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
158 Ok(ComponentInstantiationArg {
159 name: reader.read()?,
160 kind: reader.read()?,
161 index: reader.read()?,
162 })
163 }
164}