Coverage Report

Created: 2026-08-28 08:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wasm-tools/crates/wasm-encoder/src/component/exports.rs
Line
Count
Source
1
use super::{
2
    COMPONENT_SORT, CORE_MODULE_SORT, CORE_SORT, FUNCTION_SORT, INSTANCE_SORT, TYPE_SORT,
3
    VALUE_SORT,
4
};
5
use crate::{
6
    ComponentExternName, ComponentSection, ComponentSectionId, ComponentTypeRef, Encode,
7
    encode_section,
8
};
9
use alloc::vec::Vec;
10
11
/// Represents the kind of an export from a WebAssembly component.
12
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
13
pub enum ComponentExportKind {
14
    /// The export is a core module.
15
    Module,
16
    /// The export is a function.
17
    Func,
18
    /// The export is a value.
19
    Value,
20
    /// The export is a type.
21
    Type,
22
    /// The export is an instance.
23
    Instance,
24
    /// The export is a component.
25
    Component,
26
}
27
28
impl Encode for ComponentExportKind {
29
145k
    fn encode(&self, sink: &mut Vec<u8>) {
30
145k
        match self {
31
0
            Self::Module => {
32
0
                sink.push(CORE_SORT);
33
0
                sink.push(CORE_MODULE_SORT);
34
0
            }
35
28.4k
            Self::Func => {
36
28.4k
                sink.push(FUNCTION_SORT);
37
28.4k
            }
38
0
            Self::Value => {
39
0
                sink.push(VALUE_SORT);
40
0
            }
41
94.7k
            Self::Type => {
42
94.7k
                sink.push(TYPE_SORT);
43
94.7k
            }
44
19.0k
            Self::Instance => {
45
19.0k
                sink.push(INSTANCE_SORT);
46
19.0k
            }
47
3.14k
            Self::Component => {
48
3.14k
                sink.push(COMPONENT_SORT);
49
3.14k
            }
50
        }
51
145k
    }
52
}
53
54
/// An encoder for the export section of WebAssembly component.
55
///
56
/// # Example
57
///
58
/// ```rust
59
/// use wasm_encoder::{Component, ComponentExportSection, ComponentExportKind};
60
///
61
/// // This exports a function named "foo"
62
/// let mut exports = ComponentExportSection::new();
63
/// exports.export("foo", ComponentExportKind::Func, 0, None);
64
///
65
/// let mut component = Component::new();
66
/// component.section(&exports);
67
///
68
/// let bytes = component.finish();
69
/// ```
70
#[derive(Clone, Debug, Default)]
71
pub struct ComponentExportSection {
72
    bytes: Vec<u8>,
73
    num_added: u32,
74
}
75
76
impl ComponentExportSection {
77
    /// Create a new component export section encoder.
78
17.4k
    pub fn new() -> Self {
79
17.4k
        Self::default()
80
17.4k
    }
81
82
    /// The number of exports in the section.
83
0
    pub fn len(&self) -> u32 {
84
0
        self.num_added
85
0
    }
86
87
    /// Determines if the section is empty.
88
0
    pub fn is_empty(&self) -> bool {
89
0
        self.num_added == 0
90
0
    }
91
92
    /// Define an export in the export section.
93
18.3k
    pub fn export<'a>(
94
18.3k
        &mut self,
95
18.3k
        name: impl Into<ComponentExternName<'a>>,
96
18.3k
        kind: ComponentExportKind,
97
18.3k
        index: u32,
98
18.3k
        ty: Option<ComponentTypeRef>,
99
18.3k
    ) -> &mut Self {
100
18.3k
        name.into().encode(&mut self.bytes);
101
18.3k
        kind.encode(&mut self.bytes);
102
18.3k
        index.encode(&mut self.bytes);
103
18.3k
        match ty {
104
2.03k
            Some(ty) => {
105
2.03k
                self.bytes.push(0x01);
106
2.03k
                ty.encode(&mut self.bytes);
107
2.03k
            }
108
16.2k
            None => {
109
16.2k
                self.bytes.push(0x00);
110
16.2k
            }
111
        }
112
18.3k
        self.num_added += 1;
113
18.3k
        self
114
18.3k
    }
<wasm_encoder::component::exports::ComponentExportSection>::export::<wasm_encoder::component::imports::ComponentExternName>
Line
Count
Source
93
18.3k
    pub fn export<'a>(
94
18.3k
        &mut self,
95
18.3k
        name: impl Into<ComponentExternName<'a>>,
96
18.3k
        kind: ComponentExportKind,
97
18.3k
        index: u32,
98
18.3k
        ty: Option<ComponentTypeRef>,
99
18.3k
    ) -> &mut Self {
100
18.3k
        name.into().encode(&mut self.bytes);
101
18.3k
        kind.encode(&mut self.bytes);
102
18.3k
        index.encode(&mut self.bytes);
103
18.3k
        match ty {
104
2.03k
            Some(ty) => {
105
2.03k
                self.bytes.push(0x01);
106
2.03k
                ty.encode(&mut self.bytes);
107
2.03k
            }
108
16.2k
            None => {
109
16.2k
                self.bytes.push(0x00);
110
16.2k
            }
111
        }
112
18.3k
        self.num_added += 1;
113
18.3k
        self
114
18.3k
    }
Unexecuted instantiation: <wasm_encoder::component::exports::ComponentExportSection>::export::<_>
115
}
116
117
impl Encode for ComponentExportSection {
118
17.4k
    fn encode(&self, sink: &mut Vec<u8>) {
119
17.4k
        encode_section(sink, self.num_added, &self.bytes);
120
17.4k
    }
121
}
122
123
impl ComponentSection for ComponentExportSection {
124
17.4k
    fn id(&self) -> u8 {
125
17.4k
        ComponentSectionId::Export.into()
126
17.4k
    }
127
}