Coverage Report

Created: 2026-11-23 07:59

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
12.6k
    fn encode(&self, sink: &mut Vec<u8>) {
17
12.6k
        let (sort, idx) = match self {
18
12.6k
            Self::Instance(idx) => (CORE_INSTANCE_SORT, *idx),
19
12.6k
        };
20
12.6k
        sink.push(sort);
21
12.6k
        idx.encode(sink);
22
12.6k
    }
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
13.8k
    pub fn new() -> Self {
50
13.8k
        Self::default()
51
13.8k
    }
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
11.0k
    pub fn instantiate<A, S>(&mut self, module_index: u32, args: A) -> &mut Self
65
11.0k
    where
66
11.0k
        A: IntoIterator<Item = (S, ModuleArg)>,
67
11.0k
        A::IntoIter: ExactSizeIterator,
68
11.0k
        S: AsRef<str>,
69
    {
70
11.0k
        let args = args.into_iter();
71
11.0k
        self.bytes.push(0x00);
72
11.0k
        module_index.encode(&mut self.bytes);
73
11.0k
        args.len().encode(&mut self.bytes);
74
12.6k
        for (name, arg) in args {
75
12.6k
            name.as_ref().encode(&mut self.bytes);
76
12.6k
            arg.encode(&mut self.bytes);
77
12.6k
        }
78
11.0k
        self.num_added += 1;
79
11.0k
        self
80
11.0k
    }
<wasm_encoder::component::instances::InstanceSection>::instantiate::<[(&str, wasm_encoder::component::instances::ModuleArg); 0], &str>
Line
Count
Source
64
1.52k
    pub fn instantiate<A, S>(&mut self, module_index: u32, args: A) -> &mut Self
65
1.52k
    where
66
1.52k
        A: IntoIterator<Item = (S, ModuleArg)>,
67
1.52k
        A::IntoIter: ExactSizeIterator,
68
1.52k
        S: AsRef<str>,
69
    {
70
1.52k
        let args = args.into_iter();
71
1.52k
        self.bytes.push(0x00);
72
1.52k
        module_index.encode(&mut self.bytes);
73
1.52k
        args.len().encode(&mut self.bytes);
74
1.52k
        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.52k
        self.num_added += 1;
79
1.52k
        self
80
1.52k
    }
<wasm_encoder::component::instances::InstanceSection>::instantiate::<alloc::vec::Vec<(&str, wasm_encoder::component::instances::ModuleArg)>, &str>
Line
Count
Source
64
9.52k
    pub fn instantiate<A, S>(&mut self, module_index: u32, args: A) -> &mut Self
65
9.52k
    where
66
9.52k
        A: IntoIterator<Item = (S, ModuleArg)>,
67
9.52k
        A::IntoIter: ExactSizeIterator,
68
9.52k
        S: AsRef<str>,
69
    {
70
9.52k
        let args = args.into_iter();
71
9.52k
        self.bytes.push(0x00);
72
9.52k
        module_index.encode(&mut self.bytes);
73
9.52k
        args.len().encode(&mut self.bytes);
74
12.6k
        for (name, arg) in args {
75
12.6k
            name.as_ref().encode(&mut self.bytes);
76
12.6k
            arg.encode(&mut self.bytes);
77
12.6k
        }
78
9.52k
        self.num_added += 1;
79
9.52k
        self
80
9.52k
    }
Unexecuted instantiation: <wasm_encoder::component::instances::InstanceSection>::instantiate::<_, _>
81
82
    /// Define an instance by exporting core WebAssembly items.
83
6.39k
    pub fn export_items<E, S>(&mut self, exports: E) -> &mut Self
84
6.39k
    where
85
6.39k
        E: IntoIterator<Item = (S, ExportKind, u32)>,
86
6.39k
        E::IntoIter: ExactSizeIterator,
87
6.39k
        S: AsRef<str>,
88
    {
89
6.39k
        let exports = exports.into_iter();
90
6.39k
        self.bytes.push(0x01);
91
6.39k
        exports.len().encode(&mut self.bytes);
92
52.2k
        for (name, kind, index) in exports {
93
52.2k
            name.as_ref().encode(&mut self.bytes);
94
52.2k
            kind.encode(&mut self.bytes);
95
52.2k
            index.encode(&mut self.bytes);
96
52.2k
        }
97
6.39k
        self.num_added += 1;
98
6.39k
        self
99
6.39k
    }
<wasm_encoder::component::instances::InstanceSection>::export_items::<alloc::vec::Vec<(&str, wasm_encoder::core::exports::ExportKind, u32)>, &str>
Line
Count
Source
83
6.39k
    pub fn export_items<E, S>(&mut self, exports: E) -> &mut Self
84
6.39k
    where
85
6.39k
        E: IntoIterator<Item = (S, ExportKind, u32)>,
86
6.39k
        E::IntoIter: ExactSizeIterator,
87
6.39k
        S: AsRef<str>,
88
    {
89
6.39k
        let exports = exports.into_iter();
90
6.39k
        self.bytes.push(0x01);
91
6.39k
        exports.len().encode(&mut self.bytes);
92
52.2k
        for (name, kind, index) in exports {
93
52.2k
            name.as_ref().encode(&mut self.bytes);
94
52.2k
            kind.encode(&mut self.bytes);
95
52.2k
            index.encode(&mut self.bytes);
96
52.2k
        }
97
6.39k
        self.num_added += 1;
98
6.39k
        self
99
6.39k
    }
Unexecuted instantiation: <wasm_encoder::component::instances::InstanceSection>::export_items::<_, _>
100
}
101
102
impl Encode for InstanceSection {
103
13.8k
    fn encode(&self, sink: &mut Vec<u8>) {
104
13.8k
        encode_section(sink, self.num_added, &self.bytes);
105
13.8k
    }
106
}
107
108
impl ComponentSection for InstanceSection {
109
13.8k
    fn id(&self) -> u8 {
110
13.8k
        ComponentSectionId::CoreInstance.into()
111
13.8k
    }
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
8.74k
    pub fn new() -> Self {
139
8.74k
        Self::default()
140
8.74k
    }
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
8.74k
    pub fn instantiate<A, S>(&mut self, component_index: u32, args: A) -> &mut Self
154
8.74k
    where
155
8.74k
        A: IntoIterator<Item = (S, ComponentExportKind, u32)>,
156
8.74k
        A::IntoIter: ExactSizeIterator,
157
8.74k
        S: AsRef<str>,
158
    {
159
8.74k
        let args = args.into_iter();
160
8.74k
        self.bytes.push(0x00);
161
8.74k
        component_index.encode(&mut self.bytes);
162
8.74k
        args.len().encode(&mut self.bytes);
163
17.3k
        for (name, kind, index) in args {
164
17.3k
            name.as_ref().encode(&mut self.bytes);
165
17.3k
            kind.encode(&mut self.bytes);
166
17.3k
            index.encode(&mut self.bytes);
167
17.3k
        }
168
8.74k
        self.num_added += 1;
169
8.74k
        self
170
8.74k
    }
<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
8.74k
    pub fn instantiate<A, S>(&mut self, component_index: u32, args: A) -> &mut Self
154
8.74k
    where
155
8.74k
        A: IntoIterator<Item = (S, ComponentExportKind, u32)>,
156
8.74k
        A::IntoIter: ExactSizeIterator,
157
8.74k
        S: AsRef<str>,
158
    {
159
8.74k
        let args = args.into_iter();
160
8.74k
        self.bytes.push(0x00);
161
8.74k
        component_index.encode(&mut self.bytes);
162
8.74k
        args.len().encode(&mut self.bytes);
163
17.3k
        for (name, kind, index) in args {
164
17.3k
            name.as_ref().encode(&mut self.bytes);
165
17.3k
            kind.encode(&mut self.bytes);
166
17.3k
            index.encode(&mut self.bytes);
167
17.3k
        }
168
8.74k
        self.num_added += 1;
169
8.74k
        self
170
8.74k
    }
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
8.74k
    fn encode(&self, sink: &mut Vec<u8>) {
194
8.74k
        encode_section(sink, self.num_added, &self.bytes);
195
8.74k
    }
196
}
197
198
impl ComponentSection for ComponentInstanceSection {
199
8.74k
    fn id(&self) -> u8 {
200
8.74k
        ComponentSectionId::Instance.into()
201
8.74k
    }
202
}