Coverage Report

Created: 2024-10-16 07:58

/rust/registry/src/index.crates.io-6f17d22bba15001f/wasm-encoder-0.32.0/src/component/exports.rs
Line
Count
Source (jump to first uncovered line)
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
    encode_section, AsComponentExternName, ComponentSection, ComponentSectionId, ComponentTypeRef,
7
    Encode,
8
};
9
10
/// Represents the kind of an export from a WebAssembly component.
11
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
12
pub enum ComponentExportKind {
13
    /// The export is a core module.
14
    Module,
15
    /// The export is a function.
16
    Func,
17
    /// The export is a value.
18
    Value,
19
    /// The export is a type.
20
    Type,
21
    /// The export is an instance.
22
    Instance,
23
    /// The export is a component.
24
    Component,
25
}
26
27
impl Encode for ComponentExportKind {
28
0
    fn encode(&self, sink: &mut Vec<u8>) {
29
0
        match self {
30
0
            Self::Module => {
31
0
                sink.push(CORE_SORT);
32
0
                sink.push(CORE_MODULE_SORT);
33
0
            }
34
0
            Self::Func => {
35
0
                sink.push(FUNCTION_SORT);
36
0
            }
37
0
            Self::Value => {
38
0
                sink.push(VALUE_SORT);
39
0
            }
40
0
            Self::Type => {
41
0
                sink.push(TYPE_SORT);
42
0
            }
43
0
            Self::Instance => {
44
0
                sink.push(INSTANCE_SORT);
45
0
            }
46
0
            Self::Component => {
47
0
                sink.push(COMPONENT_SORT);
48
0
            }
49
        }
50
0
    }
51
}
52
53
/// An encoder for the export section of WebAssembly component.
54
///
55
/// # Example
56
///
57
/// ```rust
58
/// use wasm_encoder::{Component, ComponentExportSection, ComponentExportKind, ComponentExternName};
59
///
60
/// // This exports a function named "foo"
61
/// let mut exports = ComponentExportSection::new();
62
/// let name = ComponentExternName::Kebab("foo");
63
/// exports.export(name, 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
0
    pub fn new() -> Self {
79
0
        Self::default()
80
0
    }
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
0
    pub fn export(
94
0
        &mut self,
95
0
        name: impl AsComponentExternName,
96
0
        kind: ComponentExportKind,
97
0
        index: u32,
98
0
        ty: Option<ComponentTypeRef>,
99
0
    ) -> &mut Self {
100
0
        name.as_component_extern_name().encode(&mut self.bytes);
101
0
        kind.encode(&mut self.bytes);
102
0
        index.encode(&mut self.bytes);
103
0
        match ty {
104
0
            Some(ty) => {
105
0
                self.bytes.push(0x01);
106
0
                ty.encode(&mut self.bytes);
107
0
            }
108
0
            None => {
109
0
                self.bytes.push(0x00);
110
0
            }
111
        }
112
0
        self.num_added += 1;
113
0
        self
114
0
    }
Unexecuted instantiation: <wasm_encoder::component::exports::ComponentExportSection>::export::<wasm_encoder::component::imports::ComponentExternName>
Unexecuted instantiation: <wasm_encoder::component::exports::ComponentExportSection>::export::<&str>
115
}
116
117
impl Encode for ComponentExportSection {
118
0
    fn encode(&self, sink: &mut Vec<u8>) {
119
0
        encode_section(sink, self.num_added, &self.bytes);
120
0
    }
121
}
122
123
impl ComponentSection for ComponentExportSection {
124
0
    fn id(&self) -> u8 {
125
0
        ComponentSectionId::Export.into()
126
0
    }
Unexecuted instantiation: <wasm_encoder::component::exports::ComponentExportSection as wasm_encoder::component::ComponentSection>::id
Unexecuted instantiation: <wasm_encoder::component::exports::ComponentExportSection as wasm_encoder::component::ComponentSection>::id
127
}