Coverage Report

Created: 2026-06-21 07:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wasm-tools/crates/wasm-encoder/src/component/canonicals.rs
Line
Count
Source
1
use crate::{
2
    ComponentSection, ComponentSectionId, ComponentValType, Encode, ValType, encode_section,
3
};
4
use alloc::vec::Vec;
5
6
/// Represents options for canonical function definitions.
7
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8
pub enum CanonicalOption {
9
    /// The string types in the function signature are UTF-8 encoded.
10
    UTF8,
11
    /// The string types in the function signature are UTF-16 encoded.
12
    UTF16,
13
    /// The string types in the function signature are compact UTF-16 encoded.
14
    CompactUTF16,
15
    /// The memory to use if the lifting or lowering of a function requires memory access.
16
    ///
17
    /// The value is an index to a core memory.
18
    Memory(u32),
19
    /// The realloc function to use if the lifting or lowering of a function requires memory
20
    /// allocation.
21
    ///
22
    /// The value is an index to a core function of type `(func (param i32 i32 i32 i32) (result i32))`.
23
    Realloc(u32),
24
    /// The post-return function to use if the lifting of a function requires
25
    /// cleanup after the function returns.
26
    PostReturn(u32),
27
    /// Indicates that specified function should be lifted or lowered using the `async` ABI.
28
    Async,
29
    /// The function to use if the async lifting of a function should receive task/stream/future progress events
30
    /// using a callback.
31
    Callback(u32),
32
    /// The core function type to lower a component function into.
33
    CoreType(u32),
34
    /// Use the GC variant of the canonical ABI.
35
    Gc,
36
}
37
38
impl Encode for CanonicalOption {
39
12.7k
    fn encode(&self, sink: &mut Vec<u8>) {
40
12.7k
        match self {
41
698
            Self::UTF8 => sink.push(0x00),
42
0
            Self::UTF16 => sink.push(0x01),
43
0
            Self::CompactUTF16 => sink.push(0x02),
44
1.93k
            Self::Memory(idx) => {
45
1.93k
                sink.push(0x03);
46
1.93k
                idx.encode(sink);
47
1.93k
            }
48
530
            Self::Realloc(idx) => {
49
530
                sink.push(0x04);
50
530
                idx.encode(sink);
51
530
            }
52
6.35k
            Self::PostReturn(idx) => {
53
6.35k
                sink.push(0x05);
54
6.35k
                idx.encode(sink);
55
6.35k
            }
56
2.12k
            Self::Async => {
57
2.12k
                sink.push(0x06);
58
2.12k
            }
59
1.10k
            Self::Callback(idx) => {
60
1.10k
                sink.push(0x07);
61
1.10k
                idx.encode(sink);
62
1.10k
            }
63
0
            Self::CoreType(idx) => {
64
0
                sink.push(0x08);
65
0
                idx.encode(sink);
66
0
            }
67
0
            Self::Gc => {
68
0
                sink.push(0x09);
69
0
            }
70
        }
71
12.7k
    }
72
}
73
74
/// An encoder for the canonical function section of WebAssembly components.
75
///
76
/// # Example
77
///
78
/// ```
79
/// use wasm_encoder::{Component, CanonicalFunctionSection, CanonicalOption};
80
///
81
/// let mut functions = CanonicalFunctionSection::new();
82
/// functions.lift(0, 0, [CanonicalOption::UTF8]);
83
///
84
/// let mut component = Component::new();
85
/// component.section(&functions);
86
///
87
/// let bytes = component.finish();
88
/// ```
89
#[derive(Clone, Debug, Default)]
90
pub struct CanonicalFunctionSection {
91
    bytes: Vec<u8>,
92
    num_added: u32,
93
}
94
95
impl CanonicalFunctionSection {
96
    /// Construct a new component function section encoder.
97
13.7k
    pub fn new() -> Self {
98
13.7k
        Self::default()
99
13.7k
    }
100
101
    /// The number of functions in the section.
102
0
    pub fn len(&self) -> u32 {
103
0
        self.num_added
104
0
    }
105
106
    /// Determines if the section is empty.
107
0
    pub fn is_empty(&self) -> bool {
108
0
        self.num_added == 0
109
0
    }
110
111
    /// Define a function that will lift a core WebAssembly function to the canonical ABI.
112
7.58k
    pub fn lift<O>(&mut self, core_func_index: u32, type_index: u32, options: O) -> &mut Self
113
7.58k
    where
114
7.58k
        O: IntoIterator<Item = CanonicalOption>,
115
7.58k
        O::IntoIter: ExactSizeIterator,
116
    {
117
7.58k
        self.bytes.push(0x00);
118
7.58k
        self.bytes.push(0x00);
119
7.58k
        core_func_index.encode(&mut self.bytes);
120
7.58k
        self.encode_options(options);
121
7.58k
        type_index.encode(&mut self.bytes);
122
7.58k
        self.num_added += 1;
123
7.58k
        self
124
7.58k
    }
<wasm_encoder::component::canonicals::CanonicalFunctionSection>::lift::<alloc::vec::Vec<wasm_encoder::component::canonicals::CanonicalOption>>
Line
Count
Source
112
7.58k
    pub fn lift<O>(&mut self, core_func_index: u32, type_index: u32, options: O) -> &mut Self
113
7.58k
    where
114
7.58k
        O: IntoIterator<Item = CanonicalOption>,
115
7.58k
        O::IntoIter: ExactSizeIterator,
116
    {
117
7.58k
        self.bytes.push(0x00);
118
7.58k
        self.bytes.push(0x00);
119
7.58k
        core_func_index.encode(&mut self.bytes);
120
7.58k
        self.encode_options(options);
121
7.58k
        type_index.encode(&mut self.bytes);
122
7.58k
        self.num_added += 1;
123
7.58k
        self
124
7.58k
    }
Unexecuted instantiation: <wasm_encoder::component::canonicals::CanonicalFunctionSection>::lift::<alloc::vec::Vec<wasm_encoder::component::canonicals::CanonicalOption>>
Unexecuted instantiation: <wasm_encoder::component::canonicals::CanonicalFunctionSection>::lift::<_>
125
126
    /// Define a function that will lower a canonical ABI function to a core WebAssembly function.
127
2.68k
    pub fn lower<O>(&mut self, func_index: u32, options: O) -> &mut Self
128
2.68k
    where
129
2.68k
        O: IntoIterator<Item = CanonicalOption>,
130
2.68k
        O::IntoIter: ExactSizeIterator,
131
    {
132
2.68k
        self.bytes.push(0x01);
133
2.68k
        self.bytes.push(0x00);
134
2.68k
        func_index.encode(&mut self.bytes);
135
2.68k
        self.encode_options(options);
136
2.68k
        self.num_added += 1;
137
2.68k
        self
138
2.68k
    }
<wasm_encoder::component::canonicals::CanonicalFunctionSection>::lower::<alloc::vec::Vec<wasm_encoder::component::canonicals::CanonicalOption>>
Line
Count
Source
127
2.12k
    pub fn lower<O>(&mut self, func_index: u32, options: O) -> &mut Self
128
2.12k
    where
129
2.12k
        O: IntoIterator<Item = CanonicalOption>,
130
2.12k
        O::IntoIter: ExactSizeIterator,
131
    {
132
2.12k
        self.bytes.push(0x01);
133
2.12k
        self.bytes.push(0x00);
134
2.12k
        func_index.encode(&mut self.bytes);
135
2.12k
        self.encode_options(options);
136
2.12k
        self.num_added += 1;
137
2.12k
        self
138
2.12k
    }
<wasm_encoder::component::canonicals::CanonicalFunctionSection>::lower::<<wit_component::encoding::RequiredOptions>::into_iter::Iter>
Line
Count
Source
127
564
    pub fn lower<O>(&mut self, func_index: u32, options: O) -> &mut Self
128
564
    where
129
564
        O: IntoIterator<Item = CanonicalOption>,
130
564
        O::IntoIter: ExactSizeIterator,
131
    {
132
564
        self.bytes.push(0x01);
133
564
        self.bytes.push(0x00);
134
564
        func_index.encode(&mut self.bytes);
135
564
        self.encode_options(options);
136
564
        self.num_added += 1;
137
564
        self
138
564
    }
Unexecuted instantiation: <wasm_encoder::component::canonicals::CanonicalFunctionSection>::lower::<alloc::vec::Vec<wasm_encoder::component::canonicals::CanonicalOption>>
Unexecuted instantiation: <wasm_encoder::component::canonicals::CanonicalFunctionSection>::lower::<_>
139
140
    /// Defines a function which will create an owned handle to the resource
141
    /// specified by `ty_index`.
142
508
    pub fn resource_new(&mut self, ty_index: u32) -> &mut Self {
143
508
        self.bytes.push(0x02);
144
508
        ty_index.encode(&mut self.bytes);
145
508
        self.num_added += 1;
146
508
        self
147
508
    }
148
149
    /// Defines a function which will drop the specified type of handle.
150
833
    pub fn resource_drop(&mut self, ty_index: u32) -> &mut Self {
151
833
        self.bytes.push(0x03);
152
833
        ty_index.encode(&mut self.bytes);
153
833
        self.num_added += 1;
154
833
        self
155
833
    }
156
157
    /// Defines a function which will drop the specified type of handle.
158
0
    pub fn resource_drop_async(&mut self, ty_index: u32) -> &mut Self {
159
0
        self.bytes.push(0x07);
160
0
        ty_index.encode(&mut self.bytes);
161
0
        self.num_added += 1;
162
0
        self
163
0
    }
164
165
    /// Defines a function which will return the representation of the specified
166
    /// resource type.
167
508
    pub fn resource_rep(&mut self, ty_index: u32) -> &mut Self {
168
508
        self.bytes.push(0x04);
169
508
        ty_index.encode(&mut self.bytes);
170
508
        self.num_added += 1;
171
508
        self
172
508
    }
173
174
    /// Defines a function which will spawn a new thread by invoking a shared
175
    /// function of type `ty_index`.
176
0
    pub fn thread_spawn_ref(&mut self, ty_index: u32) -> &mut Self {
177
0
        self.bytes.push(0x40);
178
0
        ty_index.encode(&mut self.bytes);
179
0
        self.num_added += 1;
180
0
        self
181
0
    }
182
183
    /// Defines a function which will spawn a new thread by invoking a shared
184
    /// function indirectly through a `funcref` table.
185
0
    pub fn thread_spawn_indirect(&mut self, ty_index: u32, table_index: u32) -> &mut Self {
186
0
        self.bytes.push(0x41);
187
0
        ty_index.encode(&mut self.bytes);
188
0
        table_index.encode(&mut self.bytes);
189
0
        self.num_added += 1;
190
0
        self
191
0
    }
192
193
    /// Defines a function which will return the number of threads that can be
194
    /// expected to execute concurrently.
195
0
    pub fn thread_available_parallelism(&mut self) -> &mut Self {
196
0
        self.bytes.push(0x42);
197
0
        self.num_added += 1;
198
0
        self
199
0
    }
200
201
    /// Defines a function which tells the host to increment the backpressure
202
    /// counter.
203
633
    pub fn backpressure_inc(&mut self) -> &mut Self {
204
633
        self.bytes.push(0x24);
205
633
        self.num_added += 1;
206
633
        self
207
633
    }
208
209
    /// Defines a function which tells the host to decrement the backpressure
210
    /// counter.
211
633
    pub fn backpressure_dec(&mut self) -> &mut Self {
212
633
        self.bytes.push(0x25);
213
633
        self.num_added += 1;
214
633
        self
215
633
    }
216
217
    /// Defines a function which returns a result to the caller of a lifted
218
    /// export function.  This allows the callee to continue executing after
219
    /// returning a result.
220
1.66k
    pub fn task_return<O>(&mut self, ty: Option<ComponentValType>, options: O) -> &mut Self
221
1.66k
    where
222
1.66k
        O: IntoIterator<Item = CanonicalOption>,
223
1.66k
        O::IntoIter: ExactSizeIterator,
224
    {
225
1.66k
        self.bytes.push(0x09);
226
1.66k
        crate::encode_resultlist(&mut self.bytes, ty);
227
1.66k
        self.encode_options(options);
228
1.66k
        self.num_added += 1;
229
1.66k
        self
230
1.66k
    }
<wasm_encoder::component::canonicals::CanonicalFunctionSection>::task_return::<[wasm_encoder::component::canonicals::CanonicalOption; 0]>
Line
Count
Source
220
1.64k
    pub fn task_return<O>(&mut self, ty: Option<ComponentValType>, options: O) -> &mut Self
221
1.64k
    where
222
1.64k
        O: IntoIterator<Item = CanonicalOption>,
223
1.64k
        O::IntoIter: ExactSizeIterator,
224
    {
225
1.64k
        self.bytes.push(0x09);
226
1.64k
        crate::encode_resultlist(&mut self.bytes, ty);
227
1.64k
        self.encode_options(options);
228
1.64k
        self.num_added += 1;
229
1.64k
        self
230
1.64k
    }
<wasm_encoder::component::canonicals::CanonicalFunctionSection>::task_return::<<wit_component::encoding::RequiredOptions>::into_iter::Iter>
Line
Count
Source
220
23
    pub fn task_return<O>(&mut self, ty: Option<ComponentValType>, options: O) -> &mut Self
221
23
    where
222
23
        O: IntoIterator<Item = CanonicalOption>,
223
23
        O::IntoIter: ExactSizeIterator,
224
    {
225
23
        self.bytes.push(0x09);
226
23
        crate::encode_resultlist(&mut self.bytes, ty);
227
23
        self.encode_options(options);
228
23
        self.num_added += 1;
229
23
        self
230
23
    }
Unexecuted instantiation: <wasm_encoder::component::canonicals::CanonicalFunctionSection>::task_return::<_>
231
232
    /// Defines a function to acknowledge cancellation of the current task.
233
633
    pub fn task_cancel(&mut self) -> &mut Self {
234
633
        self.bytes.push(0x05);
235
633
        self.num_added += 1;
236
633
        self
237
633
    }
238
239
    /// Defines a new `context.get` intrinsic of the ith slot with the given
240
    /// value type.
241
633
    pub fn context_get(&mut self, ty: ValType, i: u32) -> &mut Self {
242
633
        self.bytes.push(0x0a);
243
633
        ty.encode(&mut self.bytes);
244
633
        i.encode(&mut self.bytes);
245
633
        self.num_added += 1;
246
633
        self
247
633
    }
248
249
    /// Defines a new `context.set` intrinsic of the ith slot with the given
250
    /// value type.
251
633
    pub fn context_set(&mut self, ty: ValType, i: u32) -> &mut Self {
252
633
        self.bytes.push(0x0b);
253
633
        ty.encode(&mut self.bytes);
254
633
        i.encode(&mut self.bytes);
255
633
        self.num_added += 1;
256
633
        self
257
633
    }
258
259
    /// Defines a function which yields control to the host so that other tasks
260
    /// are able to make progress, if any.
261
    ///
262
    /// If `cancellable` is true, the caller instance may be reentered.
263
633
    pub fn thread_yield(&mut self, cancellable: bool) -> &mut Self {
264
633
        self.bytes.push(0x0c);
265
633
        self.bytes.push(if cancellable { 1 } else { 0 });
266
633
        self.num_added += 1;
267
633
        self
268
633
    }
269
270
    /// Defines a function to drop a specified task which has completed.
271
633
    pub fn subtask_drop(&mut self) -> &mut Self {
272
633
        self.bytes.push(0x0d);
273
633
        self.num_added += 1;
274
633
        self
275
633
    }
276
277
    /// Defines a function to cancel an in-progress task.
278
633
    pub fn subtask_cancel(&mut self, async_: bool) -> &mut Self {
279
633
        self.bytes.push(0x06);
280
633
        self.bytes.push(if async_ { 1 } else { 0 });
281
633
        self.num_added += 1;
282
633
        self
283
633
    }
284
285
    /// Defines a function to create a new `stream` handle of the specified
286
    /// type.
287
223
    pub fn stream_new(&mut self, ty: u32) -> &mut Self {
288
223
        self.bytes.push(0x0e);
289
223
        ty.encode(&mut self.bytes);
290
223
        self.num_added += 1;
291
223
        self
292
223
    }
293
294
    /// Defines a function to read from a `stream` of the specified type.
295
220
    pub fn stream_read<O>(&mut self, ty: u32, options: O) -> &mut Self
296
220
    where
297
220
        O: IntoIterator<Item = CanonicalOption>,
298
220
        O::IntoIter: ExactSizeIterator,
299
    {
300
220
        self.bytes.push(0x0f);
301
220
        ty.encode(&mut self.bytes);
302
220
        self.encode_options(options);
303
220
        self.num_added += 1;
304
220
        self
305
220
    }
<wasm_encoder::component::canonicals::CanonicalFunctionSection>::stream_read::<<wit_component::encoding::RequiredOptions>::into_iter::Iter>
Line
Count
Source
295
220
    pub fn stream_read<O>(&mut self, ty: u32, options: O) -> &mut Self
296
220
    where
297
220
        O: IntoIterator<Item = CanonicalOption>,
298
220
        O::IntoIter: ExactSizeIterator,
299
    {
300
220
        self.bytes.push(0x0f);
301
220
        ty.encode(&mut self.bytes);
302
220
        self.encode_options(options);
303
220
        self.num_added += 1;
304
220
        self
305
220
    }
Unexecuted instantiation: <wasm_encoder::component::canonicals::CanonicalFunctionSection>::stream_read::<_>
306
307
    /// Defines a function to write to a `stream` of the specified type.
308
220
    pub fn stream_write<O>(&mut self, ty: u32, options: O) -> &mut Self
309
220
    where
310
220
        O: IntoIterator<Item = CanonicalOption>,
311
220
        O::IntoIter: ExactSizeIterator,
312
    {
313
220
        self.bytes.push(0x10);
314
220
        ty.encode(&mut self.bytes);
315
220
        self.encode_options(options);
316
220
        self.num_added += 1;
317
220
        self
318
220
    }
<wasm_encoder::component::canonicals::CanonicalFunctionSection>::stream_write::<<wit_component::encoding::RequiredOptions>::into_iter::Iter>
Line
Count
Source
308
220
    pub fn stream_write<O>(&mut self, ty: u32, options: O) -> &mut Self
309
220
    where
310
220
        O: IntoIterator<Item = CanonicalOption>,
311
220
        O::IntoIter: ExactSizeIterator,
312
    {
313
220
        self.bytes.push(0x10);
314
220
        ty.encode(&mut self.bytes);
315
220
        self.encode_options(options);
316
220
        self.num_added += 1;
317
220
        self
318
220
    }
Unexecuted instantiation: <wasm_encoder::component::canonicals::CanonicalFunctionSection>::stream_write::<_>
319
320
    /// Defines a function to cancel an in-progress read from a `stream` of the
321
    /// specified type.
322
223
    pub fn stream_cancel_read(&mut self, ty: u32, async_: bool) -> &mut Self {
323
223
        self.bytes.push(0x11);
324
223
        ty.encode(&mut self.bytes);
325
223
        self.bytes.push(if async_ { 1 } else { 0 });
326
223
        self.num_added += 1;
327
223
        self
328
223
    }
329
330
    /// Defines a function to cancel an in-progress write to a `stream` of the
331
    /// specified type.
332
223
    pub fn stream_cancel_write(&mut self, ty: u32, async_: bool) -> &mut Self {
333
223
        self.bytes.push(0x12);
334
223
        ty.encode(&mut self.bytes);
335
223
        self.bytes.push(if async_ { 1 } else { 0 });
336
223
        self.num_added += 1;
337
223
        self
338
223
    }
339
340
    /// Defines a function to drop the readable end of a `stream` of the
341
    /// specified type.
342
223
    pub fn stream_drop_readable(&mut self, ty: u32) -> &mut Self {
343
223
        self.bytes.push(0x13);
344
223
        ty.encode(&mut self.bytes);
345
223
        self.num_added += 1;
346
223
        self
347
223
    }
348
349
    /// Defines a function to drop the writable end of a `stream` of the
350
    /// specified type.
351
223
    pub fn stream_drop_writable(&mut self, ty: u32) -> &mut Self {
352
223
        self.bytes.push(0x14);
353
223
        ty.encode(&mut self.bytes);
354
223
        self.num_added += 1;
355
223
        self
356
223
    }
357
358
    /// Defines a function to create a new `future` handle of the specified
359
    /// type.
360
88
    pub fn future_new(&mut self, ty: u32) -> &mut Self {
361
88
        self.bytes.push(0x15);
362
88
        ty.encode(&mut self.bytes);
363
88
        self.num_added += 1;
364
88
        self
365
88
    }
366
367
    /// Defines a function to read from a `future` of the specified type.
368
87
    pub fn future_read<O>(&mut self, ty: u32, options: O) -> &mut Self
369
87
    where
370
87
        O: IntoIterator<Item = CanonicalOption>,
371
87
        O::IntoIter: ExactSizeIterator,
372
    {
373
87
        self.bytes.push(0x16);
374
87
        ty.encode(&mut self.bytes);
375
87
        self.encode_options(options);
376
87
        self.num_added += 1;
377
87
        self
378
87
    }
<wasm_encoder::component::canonicals::CanonicalFunctionSection>::future_read::<<wit_component::encoding::RequiredOptions>::into_iter::Iter>
Line
Count
Source
368
87
    pub fn future_read<O>(&mut self, ty: u32, options: O) -> &mut Self
369
87
    where
370
87
        O: IntoIterator<Item = CanonicalOption>,
371
87
        O::IntoIter: ExactSizeIterator,
372
    {
373
87
        self.bytes.push(0x16);
374
87
        ty.encode(&mut self.bytes);
375
87
        self.encode_options(options);
376
87
        self.num_added += 1;
377
87
        self
378
87
    }
Unexecuted instantiation: <wasm_encoder::component::canonicals::CanonicalFunctionSection>::future_read::<_>
379
380
    /// Defines a function to write to a `future` of the specified type.
381
87
    pub fn future_write<O>(&mut self, ty: u32, options: O) -> &mut Self
382
87
    where
383
87
        O: IntoIterator<Item = CanonicalOption>,
384
87
        O::IntoIter: ExactSizeIterator,
385
    {
386
87
        self.bytes.push(0x17);
387
87
        ty.encode(&mut self.bytes);
388
87
        self.encode_options(options);
389
87
        self.num_added += 1;
390
87
        self
391
87
    }
<wasm_encoder::component::canonicals::CanonicalFunctionSection>::future_write::<<wit_component::encoding::RequiredOptions>::into_iter::Iter>
Line
Count
Source
381
87
    pub fn future_write<O>(&mut self, ty: u32, options: O) -> &mut Self
382
87
    where
383
87
        O: IntoIterator<Item = CanonicalOption>,
384
87
        O::IntoIter: ExactSizeIterator,
385
    {
386
87
        self.bytes.push(0x17);
387
87
        ty.encode(&mut self.bytes);
388
87
        self.encode_options(options);
389
87
        self.num_added += 1;
390
87
        self
391
87
    }
Unexecuted instantiation: <wasm_encoder::component::canonicals::CanonicalFunctionSection>::future_write::<_>
392
393
    /// Defines a function to cancel an in-progress read from a `future` of the
394
    /// specified type.
395
88
    pub fn future_cancel_read(&mut self, ty: u32, async_: bool) -> &mut Self {
396
88
        self.bytes.push(0x18);
397
88
        ty.encode(&mut self.bytes);
398
88
        self.bytes.push(if async_ { 1 } else { 0 });
399
88
        self.num_added += 1;
400
88
        self
401
88
    }
402
403
    /// Defines a function to cancel an in-progress write to a `future` of the
404
    /// specified type.
405
88
    pub fn future_cancel_write(&mut self, ty: u32, async_: bool) -> &mut Self {
406
88
        self.bytes.push(0x19);
407
88
        ty.encode(&mut self.bytes);
408
88
        self.bytes.push(if async_ { 1 } else { 0 });
409
88
        self.num_added += 1;
410
88
        self
411
88
    }
412
413
    /// Defines a function to drop the readable end of a `future` of the
414
    /// specified type.
415
88
    pub fn future_drop_readable(&mut self, ty: u32) -> &mut Self {
416
88
        self.bytes.push(0x1a);
417
88
        ty.encode(&mut self.bytes);
418
88
        self.num_added += 1;
419
88
        self
420
88
    }
421
422
    /// Defines a function to drop the writable end of a `future` of the
423
    /// specified type.
424
88
    pub fn future_drop_writable(&mut self, ty: u32) -> &mut Self {
425
88
        self.bytes.push(0x1b);
426
88
        ty.encode(&mut self.bytes);
427
88
        self.num_added += 1;
428
88
        self
429
88
    }
430
431
    /// Defines a function to create a new `error-context` with a specified
432
    /// debug message.
433
0
    pub fn error_context_new<O>(&mut self, options: O) -> &mut Self
434
0
    where
435
0
        O: IntoIterator<Item = CanonicalOption>,
436
0
        O::IntoIter: ExactSizeIterator,
437
    {
438
0
        self.bytes.push(0x1c);
439
0
        self.encode_options(options);
440
0
        self.num_added += 1;
441
0
        self
442
0
    }
Unexecuted instantiation: <wasm_encoder::component::canonicals::CanonicalFunctionSection>::error_context_new::<<wit_component::encoding::RequiredOptions>::into_iter::Iter>
Unexecuted instantiation: <wasm_encoder::component::canonicals::CanonicalFunctionSection>::error_context_new::<_>
443
444
    /// Defines a function to get the debug message for a specified
445
    /// `error-context`.
446
    ///
447
    /// Note that the debug message might not necessarily match what was passed
448
    /// to `error-context.new`.
449
0
    pub fn error_context_debug_message<O>(&mut self, options: O) -> &mut Self
450
0
    where
451
0
        O: IntoIterator<Item = CanonicalOption>,
452
0
        O::IntoIter: ExactSizeIterator,
453
    {
454
0
        self.bytes.push(0x1d);
455
0
        self.encode_options(options);
456
0
        self.num_added += 1;
457
0
        self
458
0
    }
Unexecuted instantiation: <wasm_encoder::component::canonicals::CanonicalFunctionSection>::error_context_debug_message::<<wit_component::encoding::RequiredOptions>::into_iter::Iter>
Unexecuted instantiation: <wasm_encoder::component::canonicals::CanonicalFunctionSection>::error_context_debug_message::<_>
459
460
    /// Defines a function to drop a specified `error-context`.
461
0
    pub fn error_context_drop(&mut self) -> &mut Self {
462
0
        self.bytes.push(0x1e);
463
0
        self.num_added += 1;
464
0
        self
465
0
    }
466
467
    /// Declare a new `waitable-set.new` intrinsic, used to create a
468
    /// `waitable-set` pseudo-resource.
469
633
    pub fn waitable_set_new(&mut self) -> &mut Self {
470
633
        self.bytes.push(0x1f);
471
633
        self.num_added += 1;
472
633
        self
473
633
    }
474
475
    /// Declare a new `waitable-set.wait` intrinsic, used to block on a
476
    /// `waitable-set`.
477
633
    pub fn waitable_set_wait(&mut self, async_: bool, memory: u32) -> &mut Self {
478
633
        self.bytes.push(0x20);
479
633
        self.bytes.push(if async_ { 1 } else { 0 });
480
633
        memory.encode(&mut self.bytes);
481
633
        self.num_added += 1;
482
633
        self
483
633
    }
484
485
    /// Declare a new `waitable-set.wait` intrinsic, used to check, without
486
    /// blocking, if anything in a `waitable-set` is ready.
487
633
    pub fn waitable_set_poll(&mut self, async_: bool, memory: u32) -> &mut Self {
488
633
        self.bytes.push(0x21);
489
633
        self.bytes.push(if async_ { 1 } else { 0 });
490
633
        memory.encode(&mut self.bytes);
491
633
        self.num_added += 1;
492
633
        self
493
633
    }
494
495
    /// Declare a new `waitable-set.drop` intrinsic, used to dispose a
496
    /// `waitable-set` pseudo-resource.
497
633
    pub fn waitable_set_drop(&mut self) -> &mut Self {
498
633
        self.bytes.push(0x22);
499
633
        self.num_added += 1;
500
633
        self
501
633
    }
502
503
    /// Declare a new `waitable.join` intrinsic, used to add an item to a
504
    /// `waitable-set`.
505
633
    pub fn waitable_join(&mut self) -> &mut Self {
506
633
        self.bytes.push(0x23);
507
633
        self.num_added += 1;
508
633
        self
509
633
    }
510
511
    /// Declare a new `thread.index` intrinsic, used to get the index of the
512
    /// current thread.
513
0
    pub fn thread_index(&mut self) -> &mut Self {
514
0
        self.bytes.push(0x26);
515
0
        self.num_added += 1;
516
0
        self
517
0
    }
518
519
    /// Declare a new `thread.new-indirect` intrinsic, used to create a new
520
    /// thread by invoking a function indirectly through a `funcref` table.
521
0
    pub fn thread_new_indirect(&mut self, ty_index: u32, table_index: u32) -> &mut Self {
522
0
        self.bytes.push(0x27);
523
0
        ty_index.encode(&mut self.bytes);
524
0
        table_index.encode(&mut self.bytes);
525
0
        self.num_added += 1;
526
0
        self
527
0
    }
528
529
    /// Declare a new `thread.suspend-to-suspended` intrinsic, used to switch execution to
530
    /// another suspended thread.
531
0
    pub fn thread_suspend_to_suspended(&mut self, cancellable: bool) -> &mut Self {
532
0
        self.bytes.push(0x28);
533
0
        self.bytes.push(if cancellable { 1 } else { 0 });
534
0
        self.num_added += 1;
535
0
        self
536
0
    }
537
538
    /// Declare a new `thread.suspend` intrinsic, used to suspend execution of
539
    /// the current thread.
540
0
    pub fn thread_suspend(&mut self, cancellable: bool) -> &mut Self {
541
0
        self.bytes.push(0x29);
542
0
        self.bytes.push(if cancellable { 1 } else { 0 });
543
0
        self.num_added += 1;
544
0
        self
545
0
    }
546
547
    /// Declare a new `thread.suspend-to` intrinsic, used to suspend the current
548
    /// thread and switch to another thread that might not be suspended.
549
0
    pub fn thread_suspend_to(&mut self, cancellable: bool) -> &mut Self {
550
0
        self.bytes.push(0x2c);
551
0
        self.bytes.push(if cancellable { 1 } else { 0 });
552
0
        self.num_added += 1;
553
0
        self
554
0
    }
555
556
    /// Declare a new `thread.unsuspend` intrinsic, used to resume execution
557
    /// of the given thread.
558
0
    pub fn thread_unsuspend(&mut self) -> &mut Self {
559
0
        self.bytes.push(0x2a);
560
0
        self.num_added += 1;
561
0
        self
562
0
    }
563
564
    /// Declare a new `thread.yield-to-suspended` intrinsic, used to yield execution to
565
    /// a given suspended thread.
566
0
    pub fn thread_yield_to_suspended(&mut self, cancellable: bool) -> &mut Self {
567
0
        self.bytes.push(0x2b);
568
0
        self.bytes.push(if cancellable { 1 } else { 0 });
569
0
        self.num_added += 1;
570
0
        self
571
0
    }
572
573
12.5k
    fn encode_options<O>(&mut self, options: O) -> &mut Self
574
12.5k
    where
575
12.5k
        O: IntoIterator<Item = CanonicalOption>,
576
12.5k
        O::IntoIter: ExactSizeIterator,
577
    {
578
12.5k
        let options = options.into_iter();
579
12.5k
        options.len().encode(&mut self.bytes);
580
12.7k
        for option in options {
581
12.7k
            option.encode(&mut self.bytes);
582
12.7k
        }
583
12.5k
        self
584
12.5k
    }
<wasm_encoder::component::canonicals::CanonicalFunctionSection>::encode_options::<[wasm_encoder::component::canonicals::CanonicalOption; 0]>
Line
Count
Source
573
1.64k
    fn encode_options<O>(&mut self, options: O) -> &mut Self
574
1.64k
    where
575
1.64k
        O: IntoIterator<Item = CanonicalOption>,
576
1.64k
        O::IntoIter: ExactSizeIterator,
577
    {
578
1.64k
        let options = options.into_iter();
579
1.64k
        options.len().encode(&mut self.bytes);
580
1.64k
        for option in options {
581
0
            option.encode(&mut self.bytes);
582
0
        }
583
1.64k
        self
584
1.64k
    }
<wasm_encoder::component::canonicals::CanonicalFunctionSection>::encode_options::<alloc::vec::Vec<wasm_encoder::component::canonicals::CanonicalOption>>
Line
Count
Source
573
9.70k
    fn encode_options<O>(&mut self, options: O) -> &mut Self
574
9.70k
    where
575
9.70k
        O: IntoIterator<Item = CanonicalOption>,
576
9.70k
        O::IntoIter: ExactSizeIterator,
577
    {
578
9.70k
        let options = options.into_iter();
579
9.70k
        options.len().encode(&mut self.bytes);
580
10.4k
        for option in options {
581
10.4k
            option.encode(&mut self.bytes);
582
10.4k
        }
583
9.70k
        self
584
9.70k
    }
<wasm_encoder::component::canonicals::CanonicalFunctionSection>::encode_options::<<wit_component::encoding::RequiredOptions>::into_iter::Iter>
Line
Count
Source
573
1.20k
    fn encode_options<O>(&mut self, options: O) -> &mut Self
574
1.20k
    where
575
1.20k
        O: IntoIterator<Item = CanonicalOption>,
576
1.20k
        O::IntoIter: ExactSizeIterator,
577
    {
578
1.20k
        let options = options.into_iter();
579
1.20k
        options.len().encode(&mut self.bytes);
580
2.25k
        for option in options {
581
2.25k
            option.encode(&mut self.bytes);
582
2.25k
        }
583
1.20k
        self
584
1.20k
    }
Unexecuted instantiation: <wasm_encoder::component::canonicals::CanonicalFunctionSection>::encode_options::<alloc::vec::Vec<wasm_encoder::component::canonicals::CanonicalOption>>
Unexecuted instantiation: <wasm_encoder::component::canonicals::CanonicalFunctionSection>::encode_options::<_>
585
}
586
587
impl Encode for CanonicalFunctionSection {
588
13.7k
    fn encode(&self, sink: &mut Vec<u8>) {
589
13.7k
        encode_section(sink, self.num_added, &self.bytes);
590
13.7k
    }
591
}
592
593
impl ComponentSection for CanonicalFunctionSection {
594
13.7k
    fn id(&self) -> u8 {
595
13.7k
        ComponentSectionId::CanonicalFunction.into()
596
13.7k
    }
597
}