wasmparser_nostd/readers/component/
instances.rs

1use 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/// Represents the kind of an instantiation argument for a core instance.
9#[derive(Debug, Copy, Clone, PartialEq, Eq)]
10pub enum InstantiationArgKind {
11    /// The instantiation argument is a core instance.
12    Instance,
13}
14
15/// Represents an argument to instantiating a WebAssembly module.
16#[derive(Debug, Clone)]
17pub struct InstantiationArg<'a> {
18    /// The name of the module argument.
19    pub name: &'a str,
20    /// The kind of the module argument.
21    pub kind: InstantiationArgKind,
22    /// The index of the argument item.
23    pub index: u32,
24}
25
26/// Represents an instance of a WebAssembly module.
27#[derive(Debug, Clone)]
28pub enum Instance<'a> {
29    /// The instance is from instantiating a WebAssembly module.
30    Instantiate {
31        /// The module index.
32        module_index: u32,
33        /// The module's instantiation arguments.
34        args: Box<[InstantiationArg<'a>]>,
35    },
36    /// The instance is a from exporting local items.
37    FromExports(Box<[Export<'a>]>),
38}
39
40/// A reader for the core instance section of a WebAssembly component.
41///
42/// # Examples
43///
44/// ```
45/// use wasmparser_nostd::InstanceSectionReader;
46/// # let data: &[u8] = &[0x01, 0x00, 0x00, 0x01, 0x03, b'f', b'o', b'o', 0x12, 0x00];
47/// let mut reader = InstanceSectionReader::new(data, 0).unwrap();
48/// for inst in reader {
49///     println!("Instance {:?}", inst.expect("instance"));
50/// }
51/// ```
52pub 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/// Represents an argument to instantiating a WebAssembly component.
93#[derive(Debug, Clone)]
94pub struct ComponentInstantiationArg<'a> {
95    /// The name of the component argument.
96    pub name: &'a str,
97    /// The kind of the component argument.
98    pub kind: ComponentExternalKind,
99    /// The index of the argument item.
100    pub index: u32,
101}
102
103/// Represents an instance in a WebAssembly component.
104#[derive(Debug, Clone)]
105pub enum ComponentInstance<'a> {
106    /// The instance is from instantiating a WebAssembly component.
107    Instantiate {
108        /// The component index.
109        component_index: u32,
110        /// The component's instantiation arguments.
111        args: Box<[ComponentInstantiationArg<'a>]>,
112    },
113    /// The instance is a from exporting local items.
114    FromExports(Box<[ComponentExport<'a>]>),
115}
116
117/// A reader for the component instance section of a WebAssembly component.
118///
119/// # Examples
120///
121/// ```
122/// use wasmparser_nostd::ComponentInstanceSectionReader;
123/// # let data: &[u8] = &[0x01, 0x00, 0x00, 0x01, 0x03, b'f', b'o', b'o', 0x01, 0x00];
124/// let mut reader = ComponentInstanceSectionReader::new(data, 0).unwrap();
125/// for inst in reader {
126///     println!("Instance {:?}", inst.expect("instance"));
127/// }
128/// ```
129pub 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}