Coverage Report

Created: 2026-09-14 07:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wasm-tools/crates/wasm-encoder/src/component/instances.rs
Line
Count
Source
1
use super::CORE_INSTANCE_SORT;
2
use crate::{
3
    ComponentExportKind, ComponentExternName, ComponentSection, ComponentSectionId, Encode,
4
    ExportKind, encode_section,
5
};
6
use alloc::vec::Vec;
7
8
/// Represents an argument to a module instantiation.
9
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
10
pub enum ModuleArg {
11
    /// The argument is an instance.
12
    Instance(u32),
13
}
14
15
impl Encode for ModuleArg {
16
9.51k
    fn encode(&self, sink: &mut Vec<u8>) {
17
9.51k
        let (sort, idx) = match self {
18
9.51k
            Self::Instance(idx) => (CORE_INSTANCE_SORT, *idx),
19
9.51k
        };
20
9.51k
        sink.push(sort);
21
9.51k
        idx.encode(sink);
22
9.51k
    }
23
}
24
25
/// An encoder for the core instance section of WebAssembly components.
26
///
27
/// # Example
28
///
29
/// ```rust
30
/// use wasm_encoder::{Component, InstanceSection, ExportKind, ModuleArg};
31
///
32
/// let mut instances = InstanceSection::new();
33
/// instances.export_items([("foo", ExportKind::Func, 0)]);
34
/// instances.instantiate(1, [("foo", ModuleArg::Instance(0))]);
35
///
36
/// let mut component = Component::new();
37
/// component.section(&instances);
38
///
39
/// let bytes = component.finish();
40
/// ```
41
#[derive(Clone, Debug, Default)]
42
pub struct InstanceSection {
43
    bytes: Vec<u8>,
44
    num_added: u32,
45
}
46
47
impl InstanceSection {
48
    /// Create a new core instance section encoder.
49
11.2k
    pub fn new() -> Self {
50
11.2k
        Self::default()
51
11.2k
    }
52
53
    /// The number of instances in the section.
54
0
    pub fn len(&self) -> u32 {
55
0
        self.num_added
56
0
    }
57
58
    /// Determines if the section is empty.
59
0
    pub fn is_empty(&self) -> bool {
60
0
        self.num_added == 0
61
0
    }
62
63
    /// Define an instance by instantiating a core module.
64
9.78k
    pub fn instantiate<A, S>(&mut self, module_index: u32, args: A) -> &mut Self
65
9.78k
    where
66
9.78k
        A: IntoIterator<Item = (S, ModuleArg)>,
67
9.78k
        A::IntoIter: ExactSizeIterator,
68
9.78k
        S: AsRef<str>,
69
    {
70
9.78k
        let args = args.into_iter();
71
9.78k
        self.bytes.push(0x00);
72
9.78k
        module_index.encode(&mut self.bytes);
73
9.78k
        args.len().encode(&mut self.bytes);
74
9.78k
        for (name, arg) in args {
75
9.51k
            name.as_ref().encode(&mut self.bytes);
76
9.51k
            arg.encode(&mut self.bytes);
77
9.51k
        }
78
9.78k
        self.num_added += 1;
79
9.78k
        self
80
9.78k
    }
<wasm_encoder::component::instances::InstanceSection>::instantiate::<[(&str, wasm_encoder::component::instances::ModuleArg); 0], &str>
Line
Count
Source
64
1.01k
    pub fn instantiate<A, S>(&mut self, module_index: u32, args: A) -> &mut Self
65
1.01k
    where
66
1.01k
        A: IntoIterator<Item = (S, ModuleArg)>,
67
1.01k
        A::IntoIter: ExactSizeIterator,
68
1.01k
        S: AsRef<str>,
69
    {
70
1.01k
        let args = args.into_iter();
71
1.01k
        self.bytes.push(0x00);
72
1.01k
        module_index.encode(&mut self.bytes);
73
1.01k
        args.len().encode(&mut self.bytes);
74
1.01k
        for (name, arg) in args {
75
0
            name.as_ref().encode(&mut self.bytes);
76
0
            arg.encode(&mut self.bytes);
77
0
        }
78
1.01k
        self.num_added += 1;
79
1.01k
        self
80
1.01k
    }
<wasm_encoder::component::instances::InstanceSection>::instantiate::<alloc::vec::Vec<(&str, wasm_encoder::component::instances::ModuleArg)>, &str>
Line
Count
Source
64
8.77k
    pub fn instantiate<A, S>(&mut self, module_index: u32, args: A) -> &mut Self
65
8.77k
    where
66
8.77k
        A: IntoIterator<Item = (S, ModuleArg)>,
67
8.77k
        A::IntoIter: ExactSizeIterator,
68
8.77k
        S: AsRef<str>,
69
    {
70
8.77k
        let args = args.into_iter();
71
8.77k
        self.bytes.push(0x00);
72
8.77k
        module_index.encode(&mut self.bytes);
73
8.77k
        args.len().encode(&mut self.bytes);
74
9.51k
        for (name, arg) in args {
75
9.51k
            name.as_ref().encode(&mut self.bytes);
76
9.51k
            arg.encode(&mut self.bytes);
77
9.51k
        }
78
8.77k
        self.num_added += 1;
79
8.77k
        self
80
8.77k
    }
Unexecuted instantiation: <wasm_encoder::component::instances::InstanceSection>::instantiate::<_, _>
81
82
    /// Define an instance by exporting core WebAssembly items.
83
4.11k
    pub fn export_items<E, S>(&mut self, exports: E) -> &mut Self
84
4.11k
    where
85
4.11k
        E: IntoIterator<Item = (S, ExportKind, u32)>,
86
4.11k
        E::IntoIter: ExactSizeIterator,
87
4.11k
        S: AsRef<str>,
88
    {
89
4.11k
        let exports = exports.into_iter();
90
4.11k
        self.bytes.push(0x01);
91
4.11k
        exports.len().encode(&mut self.bytes);
92
22.3k
        for (name, kind, index) in exports {
93
22.3k
            name.as_ref().encode(&mut self.bytes);
94
22.3k
            kind.encode(&mut self.bytes);
95
22.3k
            index.encode(&mut self.bytes);
96
22.3k
        }
97
4.11k
        self.num_added += 1;
98
4.11k
        self
99
4.11k
    }
<wasm_encoder::component::instances::InstanceSection>::export_items::<alloc::vec::Vec<(&str, wasm_encoder::core::exports::ExportKind, u32)>, &str>
Line
Count
Source
83
4.11k
    pub fn export_items<E, S>(&mut self, exports: E) -> &mut Self
84
4.11k
    where
85
4.11k
        E: IntoIterator<Item = (S, ExportKind, u32)>,
86
4.11k
        E::IntoIter: ExactSizeIterator,
87
4.11k
        S: AsRef<str>,
88
    {
89
4.11k
        let exports = exports.into_iter();
90
4.11k
        self.bytes.push(0x01);
91
4.11k
        exports.len().encode(&mut self.bytes);
92
22.3k
        for (name, kind, index) in exports {
93
22.3k
            name.as_ref().encode(&mut self.bytes);
94
22.3k
            kind.encode(&mut self.bytes);
95
22.3k
            index.encode(&mut self.bytes);
96
22.3k
        }
97
4.11k
        self.num_added += 1;
98
4.11k
        self
99
4.11k
    }
Unexecuted instantiation: <wasm_encoder::component::instances::InstanceSection>::export_items::<_, _>
100
}
101
102
impl Encode for InstanceSection {
103
11.2k
    fn encode(&self, sink: &mut Vec<u8>) {
104
11.2k
        encode_section(sink, self.num_added, &self.bytes);
105
11.2k
    }
106
}
107
108
impl ComponentSection for InstanceSection {
109
11.2k
    fn id(&self) -> u8 {
110
11.2k
        ComponentSectionId::CoreInstance.into()
111
11.2k
    }
112
}
113
114
/// An encoder for the instance section of WebAssembly components.
115
///
116
/// # Example
117
///
118
/// ```rust
119
/// use wasm_encoder::{Component, ComponentInstanceSection, ComponentExportKind};
120
///
121
/// let mut instances = ComponentInstanceSection::new();
122
/// instances.export_items([("foo", ComponentExportKind::Func, 0)]);
123
/// instances.instantiate(1, [("foo", ComponentExportKind::Instance, 0)]);
124
///
125
/// let mut component = Component::new();
126
/// component.section(&instances);
127
///
128
/// let bytes = component.finish();
129
/// ```
130
#[derive(Clone, Debug, Default)]
131
pub struct ComponentInstanceSection {
132
    bytes: Vec<u8>,
133
    num_added: u32,
134
}
135
136
impl ComponentInstanceSection {
137
    /// Create a new instance section encoder.
138
3.78k
    pub fn new() -> Self {
139
3.78k
        Self::default()
140
3.78k
    }
141
142
    /// The number of instances in the section.
143
0
    pub fn len(&self) -> u32 {
144
0
        self.num_added
145
0
    }
146
147
    /// Determines if the section is empty.
148
0
    pub fn is_empty(&self) -> bool {
149
0
        self.num_added == 0
150
0
    }
151
152
    /// Define an instance by instantiating a component.
153
3.78k
    pub fn instantiate<A, S>(&mut self, component_index: u32, args: A) -> &mut Self
154
3.78k
    where
155
3.78k
        A: IntoIterator<Item = (S, ComponentExportKind, u32)>,
156
3.78k
        A::IntoIter: ExactSizeIterator,
157
3.78k
        S: AsRef<str>,
158
    {
159
3.78k
        let args = args.into_iter();
160
3.78k
        self.bytes.push(0x00);
161
3.78k
        component_index.encode(&mut self.bytes);
162
3.78k
        args.len().encode(&mut self.bytes);
163
6.33k
        for (name, kind, index) in args {
164
6.33k
            name.as_ref().encode(&mut self.bytes);
165
6.33k
            kind.encode(&mut self.bytes);
166
6.33k
            index.encode(&mut self.bytes);
167
6.33k
        }
168
3.78k
        self.num_added += 1;
169
3.78k
        self
170
3.78k
    }
<wasm_encoder::component::instances::ComponentInstanceSection>::instantiate::<alloc::vec::Vec<(alloc::string::String, wasm_encoder::component::exports::ComponentExportKind, u32)>, alloc::string::String>
Line
Count
Source
153
3.78k
    pub fn instantiate<A, S>(&mut self, component_index: u32, args: A) -> &mut Self
154
3.78k
    where
155
3.78k
        A: IntoIterator<Item = (S, ComponentExportKind, u32)>,
156
3.78k
        A::IntoIter: ExactSizeIterator,
157
3.78k
        S: AsRef<str>,
158
    {
159
3.78k
        let args = args.into_iter();
160
3.78k
        self.bytes.push(0x00);
161
3.78k
        component_index.encode(&mut self.bytes);
162
3.78k
        args.len().encode(&mut self.bytes);
163
6.33k
        for (name, kind, index) in args {
164
6.33k
            name.as_ref().encode(&mut self.bytes);
165
6.33k
            kind.encode(&mut self.bytes);
166
6.33k
            index.encode(&mut self.bytes);
167
6.33k
        }
168
3.78k
        self.num_added += 1;
169
3.78k
        self
170
3.78k
    }
Unexecuted instantiation: <wasm_encoder::component::instances::ComponentInstanceSection>::instantiate::<_, _>
171
172
    /// Define an instance by exporting items.
173
0
    pub fn export_items<'a, N, E>(&mut self, exports: E) -> &mut Self
174
0
    where
175
0
        E: IntoIterator<Item = (N, ComponentExportKind, u32)>,
176
0
        E::IntoIter: ExactSizeIterator,
177
0
        N: Into<ComponentExternName<'a>>,
178
    {
179
0
        let exports = exports.into_iter();
180
0
        self.bytes.push(0x01);
181
0
        exports.len().encode(&mut self.bytes);
182
0
        for (name, kind, index) in exports {
183
0
            name.into().encode(&mut self.bytes);
184
0
            kind.encode(&mut self.bytes);
185
0
            index.encode(&mut self.bytes);
186
0
        }
187
0
        self.num_added += 1;
188
0
        self
189
0
    }
190
}
191
192
impl Encode for ComponentInstanceSection {
193
3.78k
    fn encode(&self, sink: &mut Vec<u8>) {
194
3.78k
        encode_section(sink, self.num_added, &self.bytes);
195
3.78k
    }
196
}
197
198
impl ComponentSection for ComponentInstanceSection {
199
3.78k
    fn id(&self) -> u8 {
200
3.78k
        ComponentSectionId::Instance.into()
201
3.78k
    }
202
}