tinywasm_wasmparser/readers/component/
instances.rs1use alloc::boxed::Box;
2
3use crate::limits::{MAX_WASM_INSTANTIATION_ARGS, MAX_WASM_INSTANTIATION_EXPORTS};
4use crate::{
5 BinaryReader, ComponentExport, ComponentExternalKind, Export, FromReader, Result,
6 SectionLimited,
7};
8
9#[derive(Debug, Copy, Clone, PartialEq, Eq)]
11pub enum InstantiationArgKind {
12 Instance,
14}
15
16#[derive(Debug, Clone)]
18pub struct InstantiationArg<'a> {
19 pub name: &'a str,
21 pub kind: InstantiationArgKind,
23 pub index: u32,
25}
26
27#[derive(Debug, Clone)]
29pub enum Instance<'a> {
30 Instantiate {
32 module_index: u32,
34 args: Box<[InstantiationArg<'a>]>,
36 },
37 FromExports(Box<[Export<'a>]>),
39}
40
41pub type InstanceSectionReader<'a> = SectionLimited<'a, Instance<'a>>;
54
55impl<'a> FromReader<'a> for Instance<'a> {
56 fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
57 Ok(match reader.read_u8()? {
58 0x00 => Instance::Instantiate {
59 module_index: reader.read_var_u32()?,
60 args: reader
61 .read_iter(MAX_WASM_INSTANTIATION_ARGS, "core instantiation arguments")?
62 .collect::<Result<_>>()?,
63 },
64 0x01 => Instance::FromExports(
65 reader
66 .read_iter(MAX_WASM_INSTANTIATION_ARGS, "core instantiation arguments")?
67 .collect::<Result<_>>()?,
68 ),
69 x => return reader.invalid_leading_byte(x, "core instance"),
70 })
71 }
72}
73
74impl<'a> FromReader<'a> for InstantiationArg<'a> {
75 fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
76 Ok(InstantiationArg {
77 name: reader.read()?,
78 kind: reader.read()?,
79 index: reader.read()?,
80 })
81 }
82}
83
84impl<'a> FromReader<'a> for InstantiationArgKind {
85 fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
86 Ok(match reader.read_u8()? {
87 0x12 => InstantiationArgKind::Instance,
88 x => return reader.invalid_leading_byte(x, "instantiation arg kind"),
89 })
90 }
91}
92
93#[derive(Debug, Clone)]
95pub struct ComponentInstantiationArg<'a> {
96 pub name: &'a str,
98 pub kind: ComponentExternalKind,
100 pub index: u32,
102}
103
104#[derive(Debug, Clone)]
106pub enum ComponentInstance<'a> {
107 Instantiate {
109 component_index: u32,
111 args: Box<[ComponentInstantiationArg<'a>]>,
113 },
114 FromExports(Box<[ComponentExport<'a>]>),
116}
117
118pub type ComponentInstanceSectionReader<'a> = SectionLimited<'a, ComponentInstance<'a>>;
131
132impl<'a> FromReader<'a> for ComponentInstance<'a> {
133 fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
134 Ok(match reader.read_u8()? {
135 0x00 => ComponentInstance::Instantiate {
136 component_index: reader.read_var_u32()?,
137 args: reader
138 .read_iter(MAX_WASM_INSTANTIATION_ARGS, "instantiation arguments")?
139 .collect::<Result<_>>()?,
140 },
141 0x01 => ComponentInstance::FromExports(
142 (0..reader.read_size(MAX_WASM_INSTANTIATION_EXPORTS, "instantiation exports")?)
143 .map(|_| {
144 Ok(ComponentExport {
145 name: reader.read()?,
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}