wasmparser/readers/component/
types.rs

1use crate::limits::*;
2use crate::prelude::*;
3use crate::{
4    BinaryReader, ComponentAlias, ComponentExportName, ComponentImport, ComponentTypeRef,
5    FromReader, Import, RecGroup, Result, SectionLimited, TypeRef, ValType,
6};
7use core::fmt;
8
9/// Represents the kind of an outer core alias in a WebAssembly component.
10#[derive(Clone, Copy, Debug, Eq, PartialEq)]
11pub enum OuterAliasKind {
12    /// The alias is to a core type.
13    Type,
14}
15
16/// Represents a core type in a WebAssembly component.
17#[derive(Debug, Clone, Eq, PartialEq)]
18pub enum CoreType<'a> {
19    /// The type is for a core subtype.
20    Rec(RecGroup),
21    /// The type is for a core module.
22    Module(Box<[ModuleTypeDeclaration<'a>]>),
23}
24
25impl<'a> FromReader<'a> for CoreType<'a> {
26    fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
27        // For the time being, this special logic handles an ambiguous encoding
28        // in the component model: the `0x50` opcode represents both a core
29        // module type as well as a GC non-final `sub` type. To avoid this, the
30        // component model specification requires us to prefix a non-final `sub`
31        // type with `0x00` when it is used as a top-level core type of a
32        // component. Eventually (prior to the component model's v1.0 release),
33        // a module type will get a new opcode and this special logic can go
34        // away.
35        Ok(match reader.peek()? {
36            0x00 => {
37                reader.read_u8()?;
38                let x = reader.peek()?;
39                if x != 0x50 {
40                    return reader.invalid_leading_byte(x, "non-final sub type");
41                }
42                CoreType::Rec(reader.read()?)
43            }
44            0x50 => {
45                reader.read_u8()?;
46                CoreType::Module(
47                    reader
48                        .read_iter(MAX_WASM_MODULE_TYPE_DECLS, "module type declaration")?
49                        .collect::<Result<_>>()?,
50                )
51            }
52            _ => CoreType::Rec(reader.read()?),
53        })
54    }
55}
56
57/// Represents a module type declaration in a WebAssembly component.
58#[derive(Debug, Clone, Eq, PartialEq)]
59pub enum ModuleTypeDeclaration<'a> {
60    /// The module type definition is for a type.
61    Type(RecGroup),
62    /// The module type definition is for an export.
63    Export {
64        /// The name of the exported item.
65        name: &'a str,
66        /// The type reference of the export.
67        ty: TypeRef,
68    },
69    /// The module type declaration is for an outer alias.
70    OuterAlias {
71        /// The alias kind.
72        kind: OuterAliasKind,
73        /// The outward count, starting at zero for the current type.
74        count: u32,
75        /// The index of the item within the outer type.
76        index: u32,
77    },
78    /// The module type definition is for an import.
79    Import(Import<'a>),
80}
81
82impl<'a> FromReader<'a> for ModuleTypeDeclaration<'a> {
83    fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
84        Ok(match reader.read_u8()? {
85            0x00 => ModuleTypeDeclaration::Import(reader.read()?),
86            0x01 => ModuleTypeDeclaration::Type(reader.read()?),
87            0x02 => {
88                let kind = match reader.read_u8()? {
89                    0x10 => OuterAliasKind::Type,
90                    x => {
91                        return reader.invalid_leading_byte(x, "outer alias kind");
92                    }
93                };
94                match reader.read_u8()? {
95                    0x01 => ModuleTypeDeclaration::OuterAlias {
96                        kind,
97                        count: reader.read()?,
98                        index: reader.read()?,
99                    },
100                    x => {
101                        return reader.invalid_leading_byte(x, "outer alias target");
102                    }
103                }
104            }
105            0x03 => ModuleTypeDeclaration::Export {
106                name: reader.read()?,
107                ty: reader.read()?,
108            },
109            x => return reader.invalid_leading_byte(x, "type definition"),
110        })
111    }
112}
113
114/// A reader for the core type section of a WebAssembly component.
115///
116/// # Examples
117/// ```
118/// use wasmparser::{CoreTypeSectionReader, BinaryReader};
119/// # let data: &[u8] = &[0x01, 0x60, 0x00, 0x00];
120/// let reader = BinaryReader::new(data, 0);
121/// let mut reader = CoreTypeSectionReader::new(reader).unwrap();
122/// for ty in reader {
123///     println!("Type {:?}", ty.expect("type"));
124/// }
125/// ```
126pub type CoreTypeSectionReader<'a> = SectionLimited<'a, CoreType<'a>>;
127
128/// Represents a value type in a WebAssembly component.
129#[derive(Debug, Clone, Copy, PartialEq, Eq)]
130pub enum ComponentValType {
131    /// The value type is a primitive type.
132    Primitive(PrimitiveValType),
133    /// The value type is a reference to a defined type.
134    Type(u32),
135}
136
137impl<'a> FromReader<'a> for ComponentValType {
138    fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
139        if let Some(ty) = PrimitiveValType::from_byte(reader.peek()?) {
140            reader.read_u8()?;
141            return Ok(ComponentValType::Primitive(ty));
142        }
143
144        Ok(ComponentValType::Type(reader.read_var_s33()? as u32))
145    }
146}
147
148impl<'a> FromReader<'a> for Option<ComponentValType> {
149    fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
150        match reader.read_u8()? {
151            0x0 => Ok(None),
152            0x1 => Ok(Some(reader.read()?)),
153            x => reader.invalid_leading_byte(x, "optional component value type"),
154        }
155    }
156}
157
158/// Represents a primitive value type.
159#[derive(Debug, Clone, Copy, PartialEq, Eq)]
160pub enum PrimitiveValType {
161    /// The type is a boolean.
162    Bool,
163    /// The type is a signed 8-bit integer.
164    S8,
165    /// The type is an unsigned 8-bit integer.
166    U8,
167    /// The type is a signed 16-bit integer.
168    S16,
169    /// The type is an unsigned 16-bit integer.
170    U16,
171    /// The type is a signed 32-bit integer.
172    S32,
173    /// The type is an unsigned 32-bit integer.
174    U32,
175    /// The type is a signed 64-bit integer.
176    S64,
177    /// The type is an unsigned 64-bit integer.
178    U64,
179    /// The type is a 32-bit floating point number with only one NaN.
180    F32,
181    /// The type is a 64-bit floating point number with only one NaN.
182    F64,
183    /// The type is a Unicode character.
184    Char,
185    /// The type is a string.
186    String,
187    /// The error-context type. (added with the async proposal for the component
188    /// model)
189    ErrorContext,
190}
191
192impl PrimitiveValType {
193    fn from_byte(byte: u8) -> Option<PrimitiveValType> {
194        Some(match byte {
195            0x7f => PrimitiveValType::Bool,
196            0x7e => PrimitiveValType::S8,
197            0x7d => PrimitiveValType::U8,
198            0x7c => PrimitiveValType::S16,
199            0x7b => PrimitiveValType::U16,
200            0x7a => PrimitiveValType::S32,
201            0x79 => PrimitiveValType::U32,
202            0x78 => PrimitiveValType::S64,
203            0x77 => PrimitiveValType::U64,
204            0x76 => PrimitiveValType::F32,
205            0x75 => PrimitiveValType::F64,
206            0x74 => PrimitiveValType::Char,
207            0x73 => PrimitiveValType::String,
208            0x64 => PrimitiveValType::ErrorContext,
209            _ => return None,
210        })
211    }
212
213    #[cfg(feature = "validate")]
214    pub(crate) fn contains_ptr(&self) -> bool {
215        matches!(self, Self::String)
216    }
217
218    /// Determines if primitive value type `a` is a subtype of `b`.
219    pub fn is_subtype_of(a: Self, b: Self) -> bool {
220        // Note that this intentionally diverges from the upstream specification
221        // at this time and only considers exact equality for subtyping
222        // relationships.
223        //
224        // More information can be found in the subtyping implementation for
225        // component functions.
226        a == b
227    }
228}
229
230impl fmt::Display for PrimitiveValType {
231    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
232        use PrimitiveValType::*;
233        let s = match self {
234            Bool => "bool",
235            S8 => "s8",
236            U8 => "u8",
237            S16 => "s16",
238            U16 => "u16",
239            S32 => "s32",
240            U32 => "u32",
241            S64 => "s64",
242            U64 => "u64",
243            F32 => "f32",
244            F64 => "f64",
245            Char => "char",
246            String => "string",
247            ErrorContext => "error-context",
248        };
249        s.fmt(f)
250    }
251}
252
253/// Represents a type in a WebAssembly component.
254#[derive(Debug, Clone, Eq, PartialEq)]
255pub enum ComponentType<'a> {
256    /// The type is a component defined type.
257    Defined(ComponentDefinedType<'a>),
258    /// The type is a function type.
259    Func(ComponentFuncType<'a>),
260    /// The type is a component type.
261    Component(Box<[ComponentTypeDeclaration<'a>]>),
262    /// The type is an instance type.
263    Instance(Box<[InstanceTypeDeclaration<'a>]>),
264    /// The type is a fresh new resource type.
265    Resource {
266        /// The representation of this resource type in core WebAssembly.
267        rep: ValType,
268        /// An optionally-specified destructor to use for when this resource is
269        /// no longer needed.
270        dtor: Option<u32>,
271    },
272}
273
274impl<'a> FromReader<'a> for ComponentType<'a> {
275    fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
276        Ok(match reader.read_u8()? {
277            0x3f => ComponentType::Resource {
278                rep: reader.read()?,
279                dtor: match reader.read_u8()? {
280                    0x00 => None,
281                    0x01 => Some(reader.read()?),
282                    b => return reader.invalid_leading_byte(b, "resource destructor"),
283                },
284            },
285            0x40 => {
286                let params = reader
287                    .read_iter(MAX_WASM_FUNCTION_PARAMS, "component function parameters")?
288                    .collect::<Result<_>>()?;
289                let result = read_resultlist(reader)?;
290                ComponentType::Func(ComponentFuncType { params, result })
291            }
292            0x41 => ComponentType::Component(
293                reader
294                    .read_iter(MAX_WASM_COMPONENT_TYPE_DECLS, "component type declaration")?
295                    .collect::<Result<_>>()?,
296            ),
297            0x42 => ComponentType::Instance(
298                reader
299                    .read_iter(MAX_WASM_INSTANCE_TYPE_DECLS, "instance type declaration")?
300                    .collect::<Result<_>>()?,
301            ),
302            x => {
303                if let Some(ty) = PrimitiveValType::from_byte(x) {
304                    ComponentType::Defined(ComponentDefinedType::Primitive(ty))
305                } else {
306                    ComponentType::Defined(ComponentDefinedType::read(reader, x)?)
307                }
308            }
309        })
310    }
311}
312
313/// Represents part of a component type declaration in a WebAssembly component.
314#[derive(Debug, Clone, Eq, PartialEq)]
315pub enum ComponentTypeDeclaration<'a> {
316    /// The component type declaration is for a core type.
317    CoreType(CoreType<'a>),
318    /// The component type declaration is for a type.
319    Type(ComponentType<'a>),
320    /// The component type declaration is for an alias.
321    Alias(ComponentAlias<'a>),
322    /// The component type declaration is for an export.
323    Export {
324        /// The name of the export.
325        name: ComponentExportName<'a>,
326        /// The type reference for the export.
327        ty: ComponentTypeRef,
328    },
329    /// The component type declaration is for an import.
330    Import(ComponentImport<'a>),
331}
332
333impl<'a> FromReader<'a> for ComponentTypeDeclaration<'a> {
334    fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
335        // Component types are effectively instance types with the additional
336        // variant of imports; check for imports here or delegate to
337        // `InstanceTypeDeclaration` with the appropriate conversions.
338        if reader.peek()? == 0x03 {
339            reader.read_u8()?;
340            return Ok(ComponentTypeDeclaration::Import(reader.read()?));
341        }
342
343        Ok(match reader.read()? {
344            InstanceTypeDeclaration::CoreType(t) => ComponentTypeDeclaration::CoreType(t),
345            InstanceTypeDeclaration::Type(t) => ComponentTypeDeclaration::Type(t),
346            InstanceTypeDeclaration::Alias(a) => ComponentTypeDeclaration::Alias(a),
347            InstanceTypeDeclaration::Export { name, ty } => {
348                ComponentTypeDeclaration::Export { name, ty }
349            }
350        })
351    }
352}
353
354/// Represents an instance type declaration in a WebAssembly component.
355#[derive(Debug, Clone, Eq, PartialEq)]
356pub enum InstanceTypeDeclaration<'a> {
357    /// The component type declaration is for a core type.
358    CoreType(CoreType<'a>),
359    /// The instance type declaration is for a type.
360    Type(ComponentType<'a>),
361    /// The instance type declaration is for an alias.
362    Alias(ComponentAlias<'a>),
363    /// The instance type declaration is for an export.
364    Export {
365        /// The name of the export.
366        name: ComponentExportName<'a>,
367        /// The type reference for the export.
368        ty: ComponentTypeRef,
369    },
370}
371
372impl<'a> FromReader<'a> for InstanceTypeDeclaration<'a> {
373    fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
374        Ok(match reader.read_u8()? {
375            0x00 => InstanceTypeDeclaration::CoreType(reader.read()?),
376            0x01 => InstanceTypeDeclaration::Type(reader.read()?),
377            0x02 => InstanceTypeDeclaration::Alias(reader.read()?),
378            0x04 => InstanceTypeDeclaration::Export {
379                name: reader.read()?,
380                ty: reader.read()?,
381            },
382            x => return reader.invalid_leading_byte(x, "component or instance type declaration"),
383        })
384    }
385}
386
387/// Represents a type of a function in a WebAssembly component.
388#[derive(Debug, Clone, Eq, PartialEq)]
389pub struct ComponentFuncType<'a> {
390    /// The function parameters.
391    pub params: Box<[(&'a str, ComponentValType)]>,
392    /// The function result.
393    pub result: Option<ComponentValType>,
394}
395
396pub(crate) fn read_resultlist(reader: &mut BinaryReader<'_>) -> Result<Option<ComponentValType>> {
397    match reader.read_u8()? {
398        0x00 => Ok(Some(reader.read()?)),
399        0x01 => match reader.read_u8()? {
400            0x00 => Ok(None),
401            x => return reader.invalid_leading_byte(x, "number of results"),
402        },
403        x => return reader.invalid_leading_byte(x, "component function results"),
404    }
405}
406
407/// Represents a case in a variant type.
408#[derive(Debug, Clone, PartialEq, Eq)]
409pub struct VariantCase<'a> {
410    /// The name of the variant case.
411    pub name: &'a str,
412    /// The value type of the variant case.
413    pub ty: Option<ComponentValType>,
414    /// The index of the variant case that is refined by this one.
415    pub refines: Option<u32>,
416}
417
418impl<'a> FromReader<'a> for VariantCase<'a> {
419    fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
420        Ok(VariantCase {
421            name: reader.read()?,
422            ty: reader.read()?,
423            refines: match reader.read_u8()? {
424                0x0 => None,
425                0x1 => Some(reader.read_var_u32()?),
426                x => return reader.invalid_leading_byte(x, "variant case refines"),
427            },
428        })
429    }
430}
431
432/// Represents a defined type in a WebAssembly component.
433#[derive(Debug, Clone, PartialEq, Eq)]
434pub enum ComponentDefinedType<'a> {
435    /// The type is one of the primitive value types.
436    Primitive(PrimitiveValType),
437    /// The type is a record with the given fields.
438    Record(Box<[(&'a str, ComponentValType)]>),
439    /// The type is a variant with the given cases.
440    Variant(Box<[VariantCase<'a>]>),
441    /// The type is a list of the given value type.
442    List(ComponentValType),
443    /// The type is a fixed size list of the given value type.
444    FixedSizeList(ComponentValType, u32),
445    /// The type is a tuple of the given value types.
446    Tuple(Box<[ComponentValType]>),
447    /// The type is flags with the given names.
448    Flags(Box<[&'a str]>),
449    /// The type is an enum with the given tags.
450    Enum(Box<[&'a str]>),
451    /// The type is an option of the given value type.
452    Option(ComponentValType),
453    /// The type is a result type.
454    Result {
455        /// The type returned for success.
456        ok: Option<ComponentValType>,
457        /// The type returned for failure.
458        err: Option<ComponentValType>,
459    },
460    /// An owned handle to a resource.
461    Own(u32),
462    /// A borrowed handle to a resource.
463    Borrow(u32),
464    /// A future type with the specified payload type.
465    Future(Option<ComponentValType>),
466    /// A stream type with the specified payload type.
467    Stream(Option<ComponentValType>),
468}
469
470impl<'a> ComponentDefinedType<'a> {
471    fn read(reader: &mut BinaryReader<'a>, byte: u8) -> Result<ComponentDefinedType<'a>> {
472        Ok(match byte {
473            0x72 => ComponentDefinedType::Record(
474                reader
475                    .read_iter(MAX_WASM_RECORD_FIELDS, "record field")?
476                    .collect::<Result<_>>()?,
477            ),
478            0x71 => ComponentDefinedType::Variant(
479                reader
480                    .read_iter(MAX_WASM_VARIANT_CASES, "variant cases")?
481                    .collect::<Result<_>>()?,
482            ),
483            0x70 => ComponentDefinedType::List(reader.read()?),
484            0x6f => ComponentDefinedType::Tuple(
485                reader
486                    .read_iter(MAX_WASM_TUPLE_TYPES, "tuple types")?
487                    .collect::<Result<_>>()?,
488            ),
489            0x6e => ComponentDefinedType::Flags(
490                reader
491                    .read_iter(MAX_WASM_FLAG_NAMES, "flag names")?
492                    .collect::<Result<_>>()?,
493            ),
494            0x6d => ComponentDefinedType::Enum(
495                reader
496                    .read_iter(MAX_WASM_ENUM_CASES, "enum cases")?
497                    .collect::<Result<_>>()?,
498            ),
499            // NOTE: 0x6c (union) removed
500            0x6b => ComponentDefinedType::Option(reader.read()?),
501            0x6a => ComponentDefinedType::Result {
502                ok: reader.read()?,
503                err: reader.read()?,
504            },
505            0x69 => ComponentDefinedType::Own(reader.read()?),
506            0x68 => ComponentDefinedType::Borrow(reader.read()?),
507            0x67 => ComponentDefinedType::FixedSizeList(reader.read()?, reader.read_var_u32()?),
508            0x66 => ComponentDefinedType::Stream(reader.read()?),
509            0x65 => ComponentDefinedType::Future(reader.read()?),
510            x => return reader.invalid_leading_byte(x, "component defined type"),
511        })
512    }
513}
514
515/// A reader for the type section of a WebAssembly component.
516///
517/// # Examples
518///
519/// ```
520/// use wasmparser::{ComponentTypeSectionReader, BinaryReader};
521/// let data: &[u8] = &[0x01, 0x40, 0x01, 0x03, b'f', b'o', b'o', 0x73, 0x00, 0x73];
522/// let reader = BinaryReader::new(data, 0);
523/// let mut reader = ComponentTypeSectionReader::new(reader).unwrap();
524/// for ty in reader {
525///     println!("Type {:?}", ty.expect("type"));
526/// }
527/// ```
528pub type ComponentTypeSectionReader<'a> = SectionLimited<'a, ComponentType<'a>>;