Coverage Report

Created: 2025-01-09 07:53

/src/wasm-tools/crates/wasm-encoder/src/component/builder.rs
Line
Count
Source (jump to first uncovered line)
1
use crate::component::*;
2
use crate::{ExportKind, Module, RawSection, ValType};
3
use alloc::vec::Vec;
4
use core::mem;
5
6
/// Convenience type to build a component incrementally and automatically keep
7
/// track of index spaces.
8
///
9
/// This type is intended to be a wrapper around the [`Component`] encoding type
10
/// which is useful for building it up incrementally over time. This type will
11
/// automatically collect definitions into sections and reports the index of all
12
/// items added by keeping track of indices internally.
13
#[derive(Debug, Default)]
14
pub struct ComponentBuilder {
15
    /// The binary component that's being built.
16
    component: Component,
17
18
    /// The last section which was appended to during encoding. This type is
19
    /// generated by the `section_accessors` macro below.
20
    ///
21
    /// When something is encoded this is used if it matches the kind of item
22
    /// being encoded, otherwise it's "flushed" to the output component and a
23
    /// new section is started.
24
    last_section: LastSection,
25
26
    // Core index spaces
27
    core_modules: u32,
28
    core_funcs: u32,
29
    core_types: u32,
30
    core_memories: u32,
31
    core_tables: u32,
32
    core_instances: u32,
33
    core_tags: u32,
34
    core_globals: u32,
35
36
    // Component index spaces
37
    funcs: u32,
38
    instances: u32,
39
    types: u32,
40
    components: u32,
41
    values: u32,
42
}
43
44
impl ComponentBuilder {
45
    /// Returns the current number of core modules.
46
0
    pub fn core_module_count(&self) -> u32 {
47
0
        self.core_modules
48
0
    }
49
50
    /// Returns the current number of core funcs.
51
0
    pub fn core_func_count(&self) -> u32 {
52
0
        self.core_funcs
53
0
    }
54
55
    /// Returns the current number of core types.
56
0
    pub fn core_type_count(&self) -> u32 {
57
0
        self.core_types
58
0
    }
59
60
    /// Returns the current number of core memories.
61
0
    pub fn core_memory_count(&self) -> u32 {
62
0
        self.core_memories
63
0
    }
64
65
    /// Returns the current number of core tables.
66
0
    pub fn core_table_count(&self) -> u32 {
67
0
        self.core_tables
68
0
    }
69
70
    /// Returns the current number of core instances.
71
0
    pub fn core_instance_count(&self) -> u32 {
72
0
        self.core_instances
73
0
    }
74
75
    /// Returns the current number of core tags.
76
0
    pub fn core_tag_count(&self) -> u32 {
77
0
        self.core_tags
78
0
    }
79
80
    /// Returns the current number of core globals.
81
0
    pub fn core_global_count(&self) -> u32 {
82
0
        self.core_globals
83
0
    }
84
85
    /// Returns the current number of component funcs.
86
0
    pub fn func_count(&self) -> u32 {
87
0
        self.funcs
88
0
    }
89
90
    /// Returns the current number of component instances.
91
0
    pub fn instance_count(&self) -> u32 {
92
0
        self.instances
93
0
    }
94
95
    /// Returns the current number of component values.
96
0
    pub fn value_count(&self) -> u32 {
97
0
        self.values
98
0
    }
99
100
    /// Returns the current number of components.
101
0
    pub fn component_count(&self) -> u32 {
102
0
        self.components
103
0
    }
104
105
    /// Returns the current number of component types.
106
0
    pub fn type_count(&self) -> u32 {
107
0
        self.types
108
0
    }
109
110
    /// Completes this component and returns the binary encoding of the entire
111
    /// component.
112
24.6k
    pub fn finish(mut self) -> Vec<u8> {
113
24.6k
        self.flush();
114
24.6k
        self.component.finish()
115
24.6k
    }
116
117
    /// Encodes a core wasm `Module` into this component, returning its index.
118
9.94k
    pub fn core_module(&mut self, module: &Module) -> u32 {
119
9.94k
        self.flush();
120
9.94k
        self.component.section(&ModuleSection(module));
121
9.94k
        inc(&mut self.core_modules)
122
9.94k
    }
123
124
    /// Encodes a core wasm `module` into this component, returning its index.
125
6.23k
    pub fn core_module_raw(&mut self, module: &[u8]) -> u32 {
126
6.23k
        self.flush();
127
6.23k
        self.component.section(&RawSection {
128
6.23k
            id: ComponentSectionId::CoreModule.into(),
129
6.23k
            data: module,
130
6.23k
        });
131
6.23k
        inc(&mut self.core_modules)
132
6.23k
    }
133
134
    /// Instantiates a core wasm module at `module_index` with the `args`
135
    /// provided.
136
    ///
137
    /// Returns the index of the core wasm instance crated.
138
16.1k
    pub fn core_instantiate<'a, A>(&mut self, module_index: u32, args: A) -> u32
139
16.1k
    where
140
16.1k
        A: IntoIterator<Item = (&'a str, ModuleArg)>,
141
16.1k
        A::IntoIter: ExactSizeIterator,
142
16.1k
    {
143
16.1k
        self.instances().instantiate(module_index, args);
144
16.1k
        inc(&mut self.core_instances)
145
16.1k
    }
<wasm_encoder::component::builder::ComponentBuilder>::core_instantiate::<[(&str, wasm_encoder::component::instances::ModuleArg); 0]>
Line
Count
Source
138
1.85k
    pub fn core_instantiate<'a, A>(&mut self, module_index: u32, args: A) -> u32
139
1.85k
    where
140
1.85k
        A: IntoIterator<Item = (&'a str, ModuleArg)>,
141
1.85k
        A::IntoIter: ExactSizeIterator,
142
1.85k
    {
143
1.85k
        self.instances().instantiate(module_index, args);
144
1.85k
        inc(&mut self.core_instances)
145
1.85k
    }
<wasm_encoder::component::builder::ComponentBuilder>::core_instantiate::<[(&str, wasm_encoder::component::instances::ModuleArg); 1]>
Line
Count
Source
138
8.09k
    pub fn core_instantiate<'a, A>(&mut self, module_index: u32, args: A) -> u32
139
8.09k
    where
140
8.09k
        A: IntoIterator<Item = (&'a str, ModuleArg)>,
141
8.09k
        A::IntoIter: ExactSizeIterator,
142
8.09k
    {
143
8.09k
        self.instances().instantiate(module_index, args);
144
8.09k
        inc(&mut self.core_instances)
145
8.09k
    }
<wasm_encoder::component::builder::ComponentBuilder>::core_instantiate::<alloc::vec::Vec<(&str, wasm_encoder::component::instances::ModuleArg)>>
Line
Count
Source
138
6.23k
    pub fn core_instantiate<'a, A>(&mut self, module_index: u32, args: A) -> u32
139
6.23k
    where
140
6.23k
        A: IntoIterator<Item = (&'a str, ModuleArg)>,
141
6.23k
        A::IntoIter: ExactSizeIterator,
142
6.23k
    {
143
6.23k
        self.instances().instantiate(module_index, args);
144
6.23k
        inc(&mut self.core_instances)
145
6.23k
    }
Unexecuted instantiation: <wasm_encoder::component::builder::ComponentBuilder>::core_instantiate::<_>
146
147
    /// Creates a new core wasm instance from the `exports` provided.
148
    ///
149
    /// Returns the index of the core wasm instance crated.
150
13.5k
    pub fn core_instantiate_exports<'a, E>(&mut self, exports: E) -> u32
151
13.5k
    where
152
13.5k
        E: IntoIterator<Item = (&'a str, ExportKind, u32)>,
153
13.5k
        E::IntoIter: ExactSizeIterator,
154
13.5k
    {
155
13.5k
        self.instances().export_items(exports);
156
13.5k
        inc(&mut self.core_instances)
157
13.5k
    }
<wasm_encoder::component::builder::ComponentBuilder>::core_instantiate_exports::<[(&str, wasm_encoder::core::exports::ExportKind, u32); 1]>
Line
Count
Source
150
6.23k
    pub fn core_instantiate_exports<'a, E>(&mut self, exports: E) -> u32
151
6.23k
    where
152
6.23k
        E: IntoIterator<Item = (&'a str, ExportKind, u32)>,
153
6.23k
        E::IntoIter: ExactSizeIterator,
154
6.23k
    {
155
6.23k
        self.instances().export_items(exports);
156
6.23k
        inc(&mut self.core_instances)
157
6.23k
    }
<wasm_encoder::component::builder::ComponentBuilder>::core_instantiate_exports::<alloc::vec::Vec<(&str, wasm_encoder::core::exports::ExportKind, u32)>>
Line
Count
Source
150
7.27k
    pub fn core_instantiate_exports<'a, E>(&mut self, exports: E) -> u32
151
7.27k
    where
152
7.27k
        E: IntoIterator<Item = (&'a str, ExportKind, u32)>,
153
7.27k
        E::IntoIter: ExactSizeIterator,
154
7.27k
    {
155
7.27k
        self.instances().export_items(exports);
156
7.27k
        inc(&mut self.core_instances)
157
7.27k
    }
Unexecuted instantiation: <wasm_encoder::component::builder::ComponentBuilder>::core_instantiate_exports::<_>
158
159
    /// Creates a new aliased item where the core `instance` specified has its
160
    /// export `name` aliased out with the `kind` specified.
161
    ///
162
    /// Returns the index of the item crated.
163
69.6k
    pub fn core_alias_export(&mut self, instance: u32, name: &str, kind: ExportKind) -> u32 {
164
69.6k
        self.alias(Alias::CoreInstanceExport {
165
69.6k
            instance,
166
69.6k
            kind,
167
69.6k
            name,
168
69.6k
        })
169
69.6k
    }
170
171
    /// Adds a new alias to this component
172
86.4k
    pub fn alias(&mut self, alias: Alias<'_>) -> u32 {
173
86.4k
        self.aliases().alias(alias);
174
0
        match alias {
175
16.8k
            Alias::InstanceExport { kind, .. } => self.inc_kind(kind),
176
69.6k
            Alias::CoreInstanceExport { kind, .. } => self.inc_core_kind(kind),
177
            Alias::Outer {
178
                kind: ComponentOuterAliasKind::Type,
179
                ..
180
0
            } => inc(&mut self.types),
181
            Alias::Outer {
182
                kind: ComponentOuterAliasKind::CoreModule,
183
                ..
184
0
            } => inc(&mut self.core_modules),
185
            Alias::Outer {
186
                kind: ComponentOuterAliasKind::Component,
187
                ..
188
0
            } => inc(&mut self.components),
189
            Alias::Outer {
190
                kind: ComponentOuterAliasKind::CoreType,
191
                ..
192
0
            } => inc(&mut self.core_types),
193
        }
194
86.4k
    }
195
196
    /// Creates an alias to a previous component instance's exported item.
197
    ///
198
    /// The `instance` provided is the instance to access and the `name` is the
199
    /// item to access.
200
    ///
201
    /// Returns the index of the new item defined.
202
16.8k
    pub fn alias_export(&mut self, instance: u32, name: &str, kind: ComponentExportKind) -> u32 {
203
16.8k
        self.alias(Alias::InstanceExport {
204
16.8k
            instance,
205
16.8k
            kind,
206
16.8k
            name,
207
16.8k
        })
208
16.8k
    }
209
210
178k
    fn inc_kind(&mut self, kind: ComponentExportKind) -> u32 {
211
178k
        match kind {
212
33.8k
            ComponentExportKind::Func => inc(&mut self.funcs),
213
0
            ComponentExportKind::Module => inc(&mut self.core_modules),
214
134k
            ComponentExportKind::Type => inc(&mut self.types),
215
0
            ComponentExportKind::Component => inc(&mut self.components),
216
9.65k
            ComponentExportKind::Instance => inc(&mut self.instances),
217
0
            ComponentExportKind::Value => inc(&mut self.values),
218
        }
219
178k
    }
220
221
69.6k
    fn inc_core_kind(&mut self, kind: ExportKind) -> u32 {
222
69.6k
        match kind {
223
61.5k
            ExportKind::Func => inc(&mut self.core_funcs),
224
1.85k
            ExportKind::Table => inc(&mut self.core_tables),
225
6.23k
            ExportKind::Memory => inc(&mut self.core_memories),
226
0
            ExportKind::Global => inc(&mut self.core_globals),
227
0
            ExportKind::Tag => inc(&mut self.core_tags),
228
        }
229
69.6k
    }
230
231
    /// Lowers the `func_index` component function into a core wasm function
232
    /// using the `options` provided.
233
    ///
234
    /// Returns the index of the core wasm function created.
235
17.6k
    pub fn lower_func<O>(&mut self, func_index: u32, options: O) -> u32
236
17.6k
    where
237
17.6k
        O: IntoIterator<Item = CanonicalOption>,
238
17.6k
        O::IntoIter: ExactSizeIterator,
239
17.6k
    {
240
17.6k
        self.canonical_functions().lower(func_index, options);
241
17.6k
        inc(&mut self.core_funcs)
242
17.6k
    }
<wasm_encoder::component::builder::ComponentBuilder>::lower_func::<alloc::vec::Vec<wasm_encoder::component::canonicals::CanonicalOption>>
Line
Count
Source
235
12.0k
    pub fn lower_func<O>(&mut self, func_index: u32, options: O) -> u32
236
12.0k
    where
237
12.0k
        O: IntoIterator<Item = CanonicalOption>,
238
12.0k
        O::IntoIter: ExactSizeIterator,
239
12.0k
    {
240
12.0k
        self.canonical_functions().lower(func_index, options);
241
12.0k
        inc(&mut self.core_funcs)
242
12.0k
    }
<wasm_encoder::component::builder::ComponentBuilder>::lower_func::<<wit_component::encoding::RequiredOptions>::into_iter::Iter>
Line
Count
Source
235
5.60k
    pub fn lower_func<O>(&mut self, func_index: u32, options: O) -> u32
236
5.60k
    where
237
5.60k
        O: IntoIterator<Item = CanonicalOption>,
238
5.60k
        O::IntoIter: ExactSizeIterator,
239
5.60k
    {
240
5.60k
        self.canonical_functions().lower(func_index, options);
241
5.60k
        inc(&mut self.core_funcs)
242
5.60k
    }
Unexecuted instantiation: <wasm_encoder::component::builder::ComponentBuilder>::lower_func::<_>
243
244
    /// Lifts the core wasm `core_func_index` function with the component
245
    /// function type `type_index` and `options`.
246
    ///
247
    /// Returns the index of the component function created.
248
21.1k
    pub fn lift_func<O>(&mut self, core_func_index: u32, type_index: u32, options: O) -> u32
249
21.1k
    where
250
21.1k
        O: IntoIterator<Item = CanonicalOption>,
251
21.1k
        O::IntoIter: ExactSizeIterator,
252
21.1k
    {
253
21.1k
        self.canonical_functions()
254
21.1k
            .lift(core_func_index, type_index, options);
255
21.1k
        inc(&mut self.funcs)
256
21.1k
    }
<wasm_encoder::component::builder::ComponentBuilder>::lift_func::<alloc::vec::Vec<wasm_encoder::component::canonicals::CanonicalOption>>
Line
Count
Source
248
21.1k
    pub fn lift_func<O>(&mut self, core_func_index: u32, type_index: u32, options: O) -> u32
249
21.1k
    where
250
21.1k
        O: IntoIterator<Item = CanonicalOption>,
251
21.1k
        O::IntoIter: ExactSizeIterator,
252
21.1k
    {
253
21.1k
        self.canonical_functions()
254
21.1k
            .lift(core_func_index, type_index, options);
255
21.1k
        inc(&mut self.funcs)
256
21.1k
    }
Unexecuted instantiation: <wasm_encoder::component::builder::ComponentBuilder>::lift_func::<_>
257
258
    /// Imports a new item into this component with the `name` and `ty` specified.
259
40.9k
    pub fn import(&mut self, name: &str, ty: ComponentTypeRef) -> u32 {
260
40.9k
        let ret = match &ty {
261
3.11k
            ComponentTypeRef::Instance(_) => inc(&mut self.instances),
262
25.2k
            ComponentTypeRef::Func(_) => inc(&mut self.funcs),
263
12.5k
            ComponentTypeRef::Type(..) => inc(&mut self.types),
264
0
            ComponentTypeRef::Component(_) => inc(&mut self.components),
265
0
            ComponentTypeRef::Module(_) => inc(&mut self.core_modules),
266
0
            ComponentTypeRef::Value(_) => inc(&mut self.values),
267
        };
268
40.9k
        self.imports().import(name, ty);
269
40.9k
        ret
270
40.9k
    }
271
272
    /// Exports a new item from this component with the `name` and `kind`
273
    /// specified.
274
    ///
275
    /// The `idx` is the item to export and the `ty` is an optional type to
276
    /// ascribe to the export.
277
161k
    pub fn export(
278
161k
        &mut self,
279
161k
        name: &str,
280
161k
        kind: ComponentExportKind,
281
161k
        idx: u32,
282
161k
        ty: Option<ComponentTypeRef>,
283
161k
    ) -> u32 {
284
161k
        self.exports().export(name, kind, idx, ty);
285
161k
        self.inc_kind(kind)
286
161k
    }
287
288
    /// Creates a new encoder for the next core type in this component.
289
0
    pub fn core_type(&mut self) -> (u32, ComponentCoreTypeEncoder<'_>) {
290
0
        (inc(&mut self.core_types), self.core_types().ty())
291
0
    }
292
293
    /// Creates a new encoder for the next type in this component.
294
0
    pub fn ty(&mut self) -> (u32, ComponentTypeEncoder<'_>) {
295
0
        (inc(&mut self.types), self.types().ty())
296
0
    }
297
298
    /// Creates a new instance type within this component.
299
3.11k
    pub fn type_instance(&mut self, ty: &InstanceType) -> u32 {
300
3.11k
        self.types().instance(ty);
301
3.11k
        inc(&mut self.types)
302
3.11k
    }
303
304
    /// Creates a new component type within this component.
305
82.6k
    pub fn type_component(&mut self, ty: &ComponentType) -> u32 {
306
82.6k
        self.types().component(ty);
307
82.6k
        inc(&mut self.types)
308
82.6k
    }
309
310
    /// Creates a new defined component type within this component.
311
116k
    pub fn type_defined(&mut self) -> (u32, ComponentDefinedTypeEncoder<'_>) {
312
116k
        (inc(&mut self.types), self.types().defined_type())
313
116k
    }
314
315
    /// Creates a new component function type within this component.
316
37.4k
    pub fn type_function(&mut self) -> (u32, ComponentFuncTypeEncoder<'_>) {
317
37.4k
        (inc(&mut self.types), self.types().function())
318
37.4k
    }
319
320
    /// Declares a new resource type within this component.
321
2.64k
    pub fn type_resource(&mut self, rep: ValType, dtor: Option<u32>) -> u32 {
322
2.64k
        self.types().resource(rep, dtor);
323
2.64k
        inc(&mut self.types)
324
2.64k
    }
325
326
    /// Defines a new subcomponent of this component.
327
9.65k
    pub fn component(&mut self, mut builder: ComponentBuilder) -> u32 {
328
9.65k
        builder.flush();
329
9.65k
        self.flush();
330
9.65k
        self.component
331
9.65k
            .section(&NestedComponentSection(&builder.component));
332
9.65k
        inc(&mut self.components)
333
9.65k
    }
334
335
    /// Defines a new subcomponent of this component.
336
0
    pub fn component_raw(&mut self, data: &[u8]) -> u32 {
337
0
        let raw_section = RawSection {
338
0
            id: ComponentSectionId::Component.into(),
339
0
            data,
340
0
        };
341
0
        self.flush();
342
0
        self.component.section(&raw_section);
343
0
        inc(&mut self.components)
344
0
    }
345
346
    /// Instantiates the `component_index` specified with the `args` specified.
347
9.65k
    pub fn instantiate<A, S>(&mut self, component_index: u32, args: A) -> u32
348
9.65k
    where
349
9.65k
        A: IntoIterator<Item = (S, ComponentExportKind, u32)>,
350
9.65k
        A::IntoIter: ExactSizeIterator,
351
9.65k
        S: AsRef<str>,
352
9.65k
    {
353
9.65k
        self.component_instances()
354
9.65k
            .instantiate(component_index, args);
355
9.65k
        inc(&mut self.instances)
356
9.65k
    }
<wasm_encoder::component::builder::ComponentBuilder>::instantiate::<alloc::vec::Vec<(alloc::string::String, wasm_encoder::component::exports::ComponentExportKind, u32)>, alloc::string::String>
Line
Count
Source
347
9.65k
    pub fn instantiate<A, S>(&mut self, component_index: u32, args: A) -> u32
348
9.65k
    where
349
9.65k
        A: IntoIterator<Item = (S, ComponentExportKind, u32)>,
350
9.65k
        A::IntoIter: ExactSizeIterator,
351
9.65k
        S: AsRef<str>,
352
9.65k
    {
353
9.65k
        self.component_instances()
354
9.65k
            .instantiate(component_index, args);
355
9.65k
        inc(&mut self.instances)
356
9.65k
    }
Unexecuted instantiation: <wasm_encoder::component::builder::ComponentBuilder>::instantiate::<_, _>
357
358
    /// Declares a new `resource.drop` intrinsic.
359
4.72k
    pub fn resource_drop(&mut self, ty: u32) -> u32 {
360
4.72k
        self.canonical_functions().resource_drop(ty);
361
4.72k
        inc(&mut self.core_funcs)
362
4.72k
    }
363
364
    /// Declares a new `resource.new` intrinsic.
365
2.64k
    pub fn resource_new(&mut self, ty: u32) -> u32 {
366
2.64k
        self.canonical_functions().resource_new(ty);
367
2.64k
        inc(&mut self.core_funcs)
368
2.64k
    }
369
370
    /// Declares a new `resource.rep` intrinsic.
371
2.64k
    pub fn resource_rep(&mut self, ty: u32) -> u32 {
372
2.64k
        self.canonical_functions().resource_rep(ty);
373
2.64k
        inc(&mut self.core_funcs)
374
2.64k
    }
375
376
    /// Declares a new `thread.spawn` intrinsic.
377
0
    pub fn thread_spawn(&mut self, ty: u32) -> u32 {
378
0
        self.canonical_functions().thread_spawn(ty);
379
0
        inc(&mut self.core_funcs)
380
0
    }
381
382
    /// Declares a new `thread.hw_concurrency` intrinsic.
383
0
    pub fn thread_hw_concurrency(&mut self) -> u32 {
384
0
        self.canonical_functions().thread_hw_concurrency();
385
0
        inc(&mut self.core_funcs)
386
0
    }
387
388
    /// Declares a new `task.backpressure` intrinsic.
389
0
    pub fn task_backpressure(&mut self) -> u32 {
390
0
        self.canonical_functions().task_backpressure();
391
0
        inc(&mut self.core_funcs)
392
0
    }
393
394
    /// Declares a new `task.return` intrinsic.
395
0
    pub fn task_return(&mut self, ty: u32) -> u32 {
396
0
        self.canonical_functions().task_return(ty);
397
0
        inc(&mut self.core_funcs)
398
0
    }
399
400
    /// Declares a new `task.wait` intrinsic.
401
0
    pub fn task_wait(&mut self, async_: bool, memory: u32) -> u32 {
402
0
        self.canonical_functions().task_wait(async_, memory);
403
0
        inc(&mut self.core_funcs)
404
0
    }
405
406
    /// Declares a new `task.poll` intrinsic.
407
0
    pub fn task_poll(&mut self, async_: bool, memory: u32) -> u32 {
408
0
        self.canonical_functions().task_poll(async_, memory);
409
0
        inc(&mut self.core_funcs)
410
0
    }
411
412
    /// Declares a new `task.yield` intrinsic.
413
0
    pub fn task_yield(&mut self, async_: bool) -> u32 {
414
0
        self.canonical_functions().task_yield(async_);
415
0
        inc(&mut self.core_funcs)
416
0
    }
417
418
    /// Declares a new `subtask.drop` intrinsic.
419
0
    pub fn subtask_drop(&mut self) -> u32 {
420
0
        self.canonical_functions().subtask_drop();
421
0
        inc(&mut self.core_funcs)
422
0
    }
423
424
    /// Declares a new `stream.new` intrinsic.
425
0
    pub fn stream_new(&mut self, ty: u32) -> u32 {
426
0
        self.canonical_functions().stream_new(ty);
427
0
        inc(&mut self.core_funcs)
428
0
    }
429
430
    /// Declares a new `stream.read` intrinsic.
431
0
    pub fn stream_read<O>(&mut self, ty: u32, options: O) -> u32
432
0
    where
433
0
        O: IntoIterator<Item = CanonicalOption>,
434
0
        O::IntoIter: ExactSizeIterator,
435
0
    {
436
0
        self.canonical_functions().stream_read(ty, options);
437
0
        inc(&mut self.core_funcs)
438
0
    }
Unexecuted instantiation: <wasm_encoder::component::builder::ComponentBuilder>::stream_read::<alloc::vec::Vec<wasm_encoder::component::canonicals::CanonicalOption>>
Unexecuted instantiation: <wasm_encoder::component::builder::ComponentBuilder>::stream_read::<_>
439
440
    /// Declares a new `stream.write` intrinsic.
441
0
    pub fn stream_write<O>(&mut self, ty: u32, options: O) -> u32
442
0
    where
443
0
        O: IntoIterator<Item = CanonicalOption>,
444
0
        O::IntoIter: ExactSizeIterator,
445
0
    {
446
0
        self.canonical_functions().stream_write(ty, options);
447
0
        inc(&mut self.core_funcs)
448
0
    }
Unexecuted instantiation: <wasm_encoder::component::builder::ComponentBuilder>::stream_write::<alloc::vec::Vec<wasm_encoder::component::canonicals::CanonicalOption>>
Unexecuted instantiation: <wasm_encoder::component::builder::ComponentBuilder>::stream_write::<_>
449
450
    /// Declares a new `stream.cancel-read` intrinsic.
451
0
    pub fn stream_cancel_read(&mut self, ty: u32, async_: bool) -> u32 {
452
0
        self.canonical_functions().stream_cancel_read(ty, async_);
453
0
        inc(&mut self.core_funcs)
454
0
    }
455
456
    /// Declares a new `stream.cancel-write` intrinsic.
457
0
    pub fn stream_cancel_write(&mut self, ty: u32, async_: bool) -> u32 {
458
0
        self.canonical_functions().stream_cancel_write(ty, async_);
459
0
        inc(&mut self.core_funcs)
460
0
    }
461
462
    /// Declares a new `stream.close-readable` intrinsic.
463
0
    pub fn stream_close_readable(&mut self, ty: u32) -> u32 {
464
0
        self.canonical_functions().stream_close_readable(ty);
465
0
        inc(&mut self.core_funcs)
466
0
    }
467
468
    /// Declares a new `stream.close-writable` intrinsic.
469
0
    pub fn stream_close_writable(&mut self, ty: u32) -> u32 {
470
0
        self.canonical_functions().stream_close_writable(ty);
471
0
        inc(&mut self.core_funcs)
472
0
    }
473
474
    /// Declares a new `future.new` intrinsic.
475
0
    pub fn future_new(&mut self, ty: u32) -> u32 {
476
0
        self.canonical_functions().future_new(ty);
477
0
        inc(&mut self.core_funcs)
478
0
    }
479
480
    /// Declares a new `future.read` intrinsic.
481
0
    pub fn future_read<O>(&mut self, ty: u32, options: O) -> u32
482
0
    where
483
0
        O: IntoIterator<Item = CanonicalOption>,
484
0
        O::IntoIter: ExactSizeIterator,
485
0
    {
486
0
        self.canonical_functions().future_read(ty, options);
487
0
        inc(&mut self.core_funcs)
488
0
    }
Unexecuted instantiation: <wasm_encoder::component::builder::ComponentBuilder>::future_read::<alloc::vec::Vec<wasm_encoder::component::canonicals::CanonicalOption>>
Unexecuted instantiation: <wasm_encoder::component::builder::ComponentBuilder>::future_read::<_>
489
490
    /// Declares a new `future.write` intrinsic.
491
0
    pub fn future_write<O>(&mut self, ty: u32, options: O) -> u32
492
0
    where
493
0
        O: IntoIterator<Item = CanonicalOption>,
494
0
        O::IntoIter: ExactSizeIterator,
495
0
    {
496
0
        self.canonical_functions().future_write(ty, options);
497
0
        inc(&mut self.core_funcs)
498
0
    }
Unexecuted instantiation: <wasm_encoder::component::builder::ComponentBuilder>::future_write::<alloc::vec::Vec<wasm_encoder::component::canonicals::CanonicalOption>>
Unexecuted instantiation: <wasm_encoder::component::builder::ComponentBuilder>::future_write::<_>
499
500
    /// Declares a new `future.cancel-read` intrinsic.
501
0
    pub fn future_cancel_read(&mut self, ty: u32, async_: bool) -> u32 {
502
0
        self.canonical_functions().future_cancel_read(ty, async_);
503
0
        inc(&mut self.core_funcs)
504
0
    }
505
506
    /// Declares a new `future.cancel-write` intrinsic.
507
0
    pub fn future_cancel_write(&mut self, ty: u32, async_: bool) -> u32 {
508
0
        self.canonical_functions().future_cancel_write(ty, async_);
509
0
        inc(&mut self.core_funcs)
510
0
    }
511
512
    /// Declares a new `future.close-readable` intrinsic.
513
0
    pub fn future_close_readable(&mut self, ty: u32) -> u32 {
514
0
        self.canonical_functions().future_close_readable(ty);
515
0
        inc(&mut self.core_funcs)
516
0
    }
517
518
    /// Declares a new `future.close-writable` intrinsic.
519
0
    pub fn future_close_writable(&mut self, ty: u32) -> u32 {
520
0
        self.canonical_functions().future_close_writable(ty);
521
0
        inc(&mut self.core_funcs)
522
0
    }
523
524
    /// Declares a new `error-context.new` intrinsic.
525
0
    pub fn error_context_new<O>(&mut self, options: O) -> u32
526
0
    where
527
0
        O: IntoIterator<Item = CanonicalOption>,
528
0
        O::IntoIter: ExactSizeIterator,
529
0
    {
530
0
        self.canonical_functions().error_context_new(options);
531
0
        inc(&mut self.core_funcs)
532
0
    }
Unexecuted instantiation: <wasm_encoder::component::builder::ComponentBuilder>::error_context_new::<alloc::vec::Vec<wasm_encoder::component::canonicals::CanonicalOption>>
Unexecuted instantiation: <wasm_encoder::component::builder::ComponentBuilder>::error_context_new::<_>
533
534
    /// Declares a new `error-context.debug-message` intrinsic.
535
0
    pub fn error_context_debug_message<O>(&mut self, options: O) -> u32
536
0
    where
537
0
        O: IntoIterator<Item = CanonicalOption>,
538
0
        O::IntoIter: ExactSizeIterator,
539
0
    {
540
0
        self.canonical_functions()
541
0
            .error_context_debug_message(options);
542
0
        inc(&mut self.core_funcs)
543
0
    }
Unexecuted instantiation: <wasm_encoder::component::builder::ComponentBuilder>::error_context_debug_message::<alloc::vec::Vec<wasm_encoder::component::canonicals::CanonicalOption>>
Unexecuted instantiation: <wasm_encoder::component::builder::ComponentBuilder>::error_context_debug_message::<_>
544
545
    /// Declares a new `error-context.drop` intrinsic.
546
0
    pub fn error_context_drop(&mut self) -> u32 {
547
0
        self.canonical_functions().error_context_drop();
548
0
        inc(&mut self.core_funcs)
549
0
    }
550
551
    /// Adds a new custom section to this component.
552
18.3k
    pub fn custom_section(&mut self, section: &CustomSection<'_>) {
553
18.3k
        self.flush();
554
18.3k
        self.component.section(section);
555
18.3k
    }
556
557
    /// Adds a new custom section to this component.
558
24.6k
    pub fn raw_custom_section(&mut self, section: &[u8]) {
559
24.6k
        self.flush();
560
24.6k
        self.component.section(&RawCustomSection(section));
561
24.6k
    }
562
}
563
564
// Helper macro to generate methods on `ComponentBuilder` to get specific
565
// section encoders that automatically flush and write out prior sections as
566
// necessary.
567
macro_rules! section_accessors {
568
    ($($method:ident => $section:ident)*) => (
569
        #[derive(Debug, Default)]
570
        enum LastSection {
571
            #[default]
572
            None,
573
            $($section($section),)*
574
        }
575
576
        impl ComponentBuilder {
577
            $(
578
619k
                fn $method(&mut self) -> &mut $section {
579
619k
                    match &self.last_section {
580
                        // The last encoded section matches the section that's
581
                        // being requested, so no change is necessary.
582
151k
                        LastSection::$section(_) => {}
583
584
                        // Otherwise the last section didn't match this section,
585
                        // so flush any prior section if needed and start
586
                        // encoding the desired section of this method.
587
467k
                        _ => {
588
467k
                            self.flush();
589
467k
                            self.last_section = LastSection::$section($section::new());
590
467k
                        }
591
                    }
592
619k
                    match &mut self.last_section {
593
619k
                        LastSection::$section(ret) => ret,
594
0
                        _ => unreachable!()
595
                    }
596
619k
                }
<wasm_encoder::component::builder::ComponentBuilder>::component_instances
Line
Count
Source
578
9.65k
                fn $method(&mut self) -> &mut $section {
579
9.65k
                    match &self.last_section {
580
                        // The last encoded section matches the section that's
581
                        // being requested, so no change is necessary.
582
0
                        LastSection::$section(_) => {}
583
584
                        // Otherwise the last section didn't match this section,
585
                        // so flush any prior section if needed and start
586
                        // encoding the desired section of this method.
587
9.65k
                        _ => {
588
9.65k
                            self.flush();
589
9.65k
                            self.last_section = LastSection::$section($section::new());
590
9.65k
                        }
591
                    }
592
9.65k
                    match &mut self.last_section {
593
9.65k
                        LastSection::$section(ret) => ret,
594
0
                        _ => unreachable!()
595
                    }
596
9.65k
                }
<wasm_encoder::component::builder::ComponentBuilder>::instances
Line
Count
Source
578
29.7k
                fn $method(&mut self) -> &mut $section {
579
29.7k
                    match &self.last_section {
580
                        // The last encoded section matches the section that's
581
                        // being requested, so no change is necessary.
582
11.1k
                        LastSection::$section(_) => {}
583
584
                        // Otherwise the last section didn't match this section,
585
                        // so flush any prior section if needed and start
586
                        // encoding the desired section of this method.
587
18.5k
                        _ => {
588
18.5k
                            self.flush();
589
18.5k
                            self.last_section = LastSection::$section($section::new());
590
18.5k
                        }
591
                    }
592
29.7k
                    match &mut self.last_section {
593
29.7k
                        LastSection::$section(ret) => ret,
594
0
                        _ => unreachable!()
595
                    }
596
29.7k
                }
<wasm_encoder::component::builder::ComponentBuilder>::canonical_functions
Line
Count
Source
578
48.8k
                fn $method(&mut self) -> &mut $section {
579
48.8k
                    match &self.last_section {
580
                        // The last encoded section matches the section that's
581
                        // being requested, so no change is necessary.
582
10.3k
                        LastSection::$section(_) => {}
583
584
                        // Otherwise the last section didn't match this section,
585
                        // so flush any prior section if needed and start
586
                        // encoding the desired section of this method.
587
38.4k
                        _ => {
588
38.4k
                            self.flush();
589
38.4k
                            self.last_section = LastSection::$section($section::new());
590
38.4k
                        }
591
                    }
592
48.8k
                    match &mut self.last_section {
593
48.8k
                        LastSection::$section(ret) => ret,
594
0
                        _ => unreachable!()
595
                    }
596
48.8k
                }
<wasm_encoder::component::builder::ComponentBuilder>::aliases
Line
Count
Source
578
86.4k
                fn $method(&mut self) -> &mut $section {
579
86.4k
                    match &self.last_section {
580
                        // The last encoded section matches the section that's
581
                        // being requested, so no change is necessary.
582
37.8k
                        LastSection::$section(_) => {}
583
584
                        // Otherwise the last section didn't match this section,
585
                        // so flush any prior section if needed and start
586
                        // encoding the desired section of this method.
587
48.5k
                        _ => {
588
48.5k
                            self.flush();
589
48.5k
                            self.last_section = LastSection::$section($section::new());
590
48.5k
                        }
591
                    }
592
86.4k
                    match &mut self.last_section {
593
86.4k
                        LastSection::$section(ret) => ret,
594
0
                        _ => unreachable!()
595
                    }
596
86.4k
                }
<wasm_encoder::component::builder::ComponentBuilder>::exports
Line
Count
Source
578
161k
                fn $method(&mut self) -> &mut $section {
579
161k
                    match &self.last_section {
580
                        // The last encoded section matches the section that's
581
                        // being requested, so no change is necessary.
582
11.7k
                        LastSection::$section(_) => {}
583
584
                        // Otherwise the last section didn't match this section,
585
                        // so flush any prior section if needed and start
586
                        // encoding the desired section of this method.
587
149k
                        _ => {
588
149k
                            self.flush();
589
149k
                            self.last_section = LastSection::$section($section::new());
590
149k
                        }
591
                    }
592
161k
                    match &mut self.last_section {
593
161k
                        LastSection::$section(ret) => ret,
594
0
                        _ => unreachable!()
595
                    }
596
161k
                }
<wasm_encoder::component::builder::ComponentBuilder>::imports
Line
Count
Source
578
40.9k
                fn $method(&mut self) -> &mut $section {
579
40.9k
                    match &self.last_section {
580
                        // The last encoded section matches the section that's
581
                        // being requested, so no change is necessary.
582
13.5k
                        LastSection::$section(_) => {}
583
584
                        // Otherwise the last section didn't match this section,
585
                        // so flush any prior section if needed and start
586
                        // encoding the desired section of this method.
587
27.3k
                        _ => {
588
27.3k
                            self.flush();
589
27.3k
                            self.last_section = LastSection::$section($section::new());
590
27.3k
                        }
591
                    }
592
40.9k
                    match &mut self.last_section {
593
40.9k
                        LastSection::$section(ret) => ret,
594
0
                        _ => unreachable!()
595
                    }
596
40.9k
                }
<wasm_encoder::component::builder::ComponentBuilder>::types
Line
Count
Source
578
242k
                fn $method(&mut self) -> &mut $section {
579
242k
                    match &self.last_section {
580
                        // The last encoded section matches the section that's
581
                        // being requested, so no change is necessary.
582
67.1k
                        LastSection::$section(_) => {}
583
584
                        // Otherwise the last section didn't match this section,
585
                        // so flush any prior section if needed and start
586
                        // encoding the desired section of this method.
587
174k
                        _ => {
588
174k
                            self.flush();
589
174k
                            self.last_section = LastSection::$section($section::new());
590
174k
                        }
591
                    }
592
242k
                    match &mut self.last_section {
593
242k
                        LastSection::$section(ret) => ret,
594
0
                        _ => unreachable!()
595
                    }
596
242k
                }
Unexecuted instantiation: <wasm_encoder::component::builder::ComponentBuilder>::core_types
597
            )*
598
599
            /// Writes out the last section into the final component binary if
600
            /// there is a section specified, otherwise does nothing.
601
570k
            fn flush(&mut self) {
602
570k
                match mem::take(&mut self.last_section) {
603
103k
                    LastSection::None => {}
604
                    $(
605
9.65k
                        LastSection::$section(section) => {
606
9.65k
                            self.component.section(&section);
607
9.65k
                        }
608
                    )*
609
                }
610
570k
            }
611
612
        }
613
    )
614
}
615
616
section_accessors! {
617
    component_instances => ComponentInstanceSection
618
    instances => InstanceSection
619
    canonical_functions => CanonicalFunctionSection
620
    aliases => ComponentAliasSection
621
    exports => ComponentExportSection
622
    imports => ComponentImportSection
623
    types => ComponentTypeSection
624
    core_types => CoreTypeSection
625
}
626
627
645k
fn inc(idx: &mut u32) -> u32 {
628
645k
    let ret = *idx;
629
645k
    *idx += 1;
630
645k
    ret
631
645k
}