wasmparser_nostd/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};
6use ::alloc::boxed::Box;
7
8#[derive(Debug, Copy, Clone, PartialEq, Eq)]
10pub enum InstantiationArgKind {
11 Instance,
13}
14
15#[derive(Debug, Clone)]
17pub struct InstantiationArg<'a> {
18 pub name: &'a str,
20 pub kind: InstantiationArgKind,
22 pub index: u32,
24}
25
26#[derive(Debug, Clone)]
28pub enum Instance<'a> {
29 Instantiate {
31 module_index: u32,
33 args: Box<[InstantiationArg<'a>]>,
35 },
36 FromExports(Box<[Export<'a>]>),
38}
39
40pub type InstanceSectionReader<'a> = SectionLimited<'a, Instance<'a>>;
53
54impl<'a> FromReader<'a> for Instance<'a> {
55 fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
56 Ok(match reader.read_u8()? {
57 0x00 => Instance::Instantiate {
58 module_index: reader.read_var_u32()?,
59 args: reader
60 .read_iter(MAX_WASM_INSTANTIATION_ARGS, "core instantiation arguments")?
61 .collect::<Result<_>>()?,
62 },
63 0x01 => Instance::FromExports(
64 reader
65 .read_iter(MAX_WASM_INSTANTIATION_ARGS, "core instantiation arguments")?
66 .collect::<Result<_>>()?,
67 ),
68 x => return reader.invalid_leading_byte(x, "core instance"),
69 })
70 }
71}
72
73impl<'a> FromReader<'a> for InstantiationArg<'a> {
74 fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
75 Ok(InstantiationArg {
76 name: reader.read()?,
77 kind: reader.read()?,
78 index: reader.read()?,
79 })
80 }
81}
82
83impl<'a> FromReader<'a> for InstantiationArgKind {
84 fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
85 Ok(match reader.read_u8()? {
86 0x12 => InstantiationArgKind::Instance,
87 x => return reader.invalid_leading_byte(x, "instantiation arg kind"),
88 })
89 }
90}
91
92#[derive(Debug, Clone)]
94pub struct ComponentInstantiationArg<'a> {
95 pub name: &'a str,
97 pub kind: ComponentExternalKind,
99 pub index: u32,
101}
102
103#[derive(Debug, Clone)]
105pub enum ComponentInstance<'a> {
106 Instantiate {
108 component_index: u32,
110 args: Box<[ComponentInstantiationArg<'a>]>,
112 },
113 FromExports(Box<[ComponentExport<'a>]>),
115}
116
117pub type ComponentInstanceSectionReader<'a> = SectionLimited<'a, ComponentInstance<'a>>;
130
131impl<'a> FromReader<'a> for ComponentInstance<'a> {
132 fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
133 Ok(match reader.read_u8()? {
134 0x00 => ComponentInstance::Instantiate {
135 component_index: reader.read_var_u32()?,
136 args: reader
137 .read_iter(MAX_WASM_INSTANTIATION_ARGS, "instantiation arguments")?
138 .collect::<Result<_>>()?,
139 },
140 0x01 => ComponentInstance::FromExports(
141 (0..reader.read_size(MAX_WASM_INSTANTIATION_EXPORTS, "instantiation exports")?)
142 .map(|_| {
143 Ok(ComponentExport {
144 name: reader.read()?,
145 url: "",
146 kind: reader.read()?,
147 index: reader.read()?,
148 ty: None,
149 })
150 })
151 .collect::<Result<_>>()?,
152 ),
153 x => return reader.invalid_leading_byte(x, "instance"),
154 })
155 }
156}
157impl<'a> FromReader<'a> for ComponentInstantiationArg<'a> {
158 fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
159 Ok(ComponentInstantiationArg {
160 name: reader.read()?,
161 kind: reader.read()?,
162 index: reader.read()?,
163 })
164 }
165}