wasmparser_nostd/readers/component/
canonicals.rs

1use crate::limits::MAX_WASM_CANONICAL_OPTIONS;
2use crate::{BinaryReader, FromReader, Result, SectionLimited};
3use ::alloc::boxed::Box;
4
5/// Represents options for component functions.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum CanonicalOption {
8    /// The string types in the function signature are UTF-8 encoded.
9    UTF8,
10    /// The string types in the function signature are UTF-16 encoded.
11    UTF16,
12    /// The string types in the function signature are compact UTF-16 encoded.
13    CompactUTF16,
14    /// The memory to use if the lifting or lowering of a function requires memory access.
15    ///
16    /// The value is an index to a core memory.
17    Memory(u32),
18    /// The realloc function to use if the lifting or lowering of a function requires memory
19    /// allocation.
20    ///
21    /// The value is an index to a core function of type `(func (param i32 i32 i32 i32) (result i32))`.
22    Realloc(u32),
23    /// The post-return function to use if the lifting of a function requires
24    /// cleanup after the function returns.
25    PostReturn(u32),
26}
27
28/// Represents a canonical function in a WebAssembly component.
29#[derive(Debug, Clone)]
30pub enum CanonicalFunction {
31    /// The function lifts a core WebAssembly function to the canonical ABI.
32    Lift {
33        /// The index of the core WebAssembly function to lift.
34        core_func_index: u32,
35        /// The index of the lifted function's type.
36        type_index: u32,
37        /// The canonical options for the function.
38        options: Box<[CanonicalOption]>,
39    },
40    /// The function lowers a canonical ABI function to a core WebAssembly function.
41    Lower {
42        /// The index of the function to lower.
43        func_index: u32,
44        /// The canonical options for the function.
45        options: Box<[CanonicalOption]>,
46    },
47}
48
49/// A reader for the canonical section of a WebAssembly component.
50pub type ComponentCanonicalSectionReader<'a> = SectionLimited<'a, CanonicalFunction>;
51
52impl<'a> FromReader<'a> for CanonicalFunction {
53    fn from_reader(reader: &mut BinaryReader<'a>) -> Result<CanonicalFunction> {
54        Ok(match reader.read_u8()? {
55            0x00 => match reader.read_u8()? {
56                0x00 => {
57                    let core_func_index = reader.read_var_u32()?;
58                    let options = reader
59                        .read_iter(MAX_WASM_CANONICAL_OPTIONS, "canonical options")?
60                        .collect::<Result<_>>()?;
61                    let type_index = reader.read_var_u32()?;
62                    CanonicalFunction::Lift {
63                        core_func_index,
64                        options,
65                        type_index,
66                    }
67                }
68                x => return reader.invalid_leading_byte(x, "canonical function lift"),
69            },
70            0x01 => match reader.read_u8()? {
71                0x00 => CanonicalFunction::Lower {
72                    func_index: reader.read_var_u32()?,
73                    options: reader
74                        .read_iter(MAX_WASM_CANONICAL_OPTIONS, "canonical options")?
75                        .collect::<Result<_>>()?,
76                },
77                x => return reader.invalid_leading_byte(x, "canonical function lower"),
78            },
79            x => return reader.invalid_leading_byte(x, "canonical function"),
80        })
81    }
82}
83
84impl<'a> FromReader<'a> for CanonicalOption {
85    fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
86        Ok(match reader.read_u8()? {
87            0x00 => CanonicalOption::UTF8,
88            0x01 => CanonicalOption::UTF16,
89            0x02 => CanonicalOption::CompactUTF16,
90            0x03 => CanonicalOption::Memory(reader.read_var_u32()?),
91            0x04 => CanonicalOption::Realloc(reader.read_var_u32()?),
92            0x05 => CanonicalOption::PostReturn(reader.read_var_u32()?),
93            x => return reader.invalid_leading_byte(x, "canonical option"),
94        })
95    }
96}