Coverage Report

Created: 2026-10-12 08:10

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
5.45k
    fn encode(&self, sink: &mut Vec<u8>) {
40
5.45k
        match self {
41
405
            Self::UTF8 => sink.push(0x00),
42
0
            Self::UTF16 => sink.push(0x01),
43
0
            Self::CompactUTF16 => sink.push(0x02),
44
1.62k
            Self::Memory(idx) => {
45
1.62k
                sink.push(0x03);
46
1.62k
                idx.encode(sink);
47
1.62k
            }
48
263
            Self::Realloc(idx) => {
49
263
                sink.push(0x04);
50
263
                idx.encode(sink);
51
263
            }
52
2.18k
            Self::PostReturn(idx) => {
53
2.18k
                sink.push(0x05);
54
2.18k
                idx.encode(sink);
55
2.18k
            }
56
788
            Self::Async => {
57
788
                sink.push(0x06);
58
788
            }
59
188
            Self::Callback(idx) => {
60
188
                sink.push(0x07);
61
188
                idx.encode(sink);
62
188
            }
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
5.45k
    }
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
6.50k
    pub fn new() -> Self {
98
6.50k
        Self::default()
99
6.50k
    }
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
2.41k
    pub fn lift<O>(&mut self, core_func_index: u32, type_index: u32, options: O) -> &mut Self
113
2.41k
    where
114
2.41k
        O: IntoIterator<Item = CanonicalOption>,
115
2.41k
        O::IntoIter: ExactSizeIterator,
116
    {
117
2.41k
        self.bytes.push(0x00);
118
2.41k
        self.bytes.push(0x00);
119
2.41k
        core_func_index.encode(&mut self.bytes);
120
2.41k
        self.encode_options(options);
121
2.41k
        type_index.encode(&mut self.bytes);
122
2.41k
        self.num_added += 1;
123
2.41k
        self
124
2.41k
    }
<wasm_encoder::component::canonicals::CanonicalFunctionSection>::lift::<alloc::vec::Vec<wasm_encoder::component::canonicals::CanonicalOption>>
Line
Count
Source
112
2.41k
    pub fn lift<O>(&mut self, core_func_index: u32, type_index: u32, options: O) -> &mut Self
113
2.41k
    where
114
2.41k
        O: IntoIterator<Item = CanonicalOption>,
115
2.41k
        O::IntoIter: ExactSizeIterator,
116
    {
117
2.41k
        self.bytes.push(0x00);
118
2.41k
        self.bytes.push(0x00);
119
2.41k
        core_func_index.encode(&mut self.bytes);
120
2.41k
        self.encode_options(options);
121
2.41k
        type_index.encode(&mut self.bytes);
122
2.41k
        self.num_added += 1;
123
2.41k
        self
124
2.41k
    }
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
1.41k
    pub fn lower<O>(&mut self, func_index: u32, options: O) -> &mut Self
128
1.41k
    where
129
1.41k
        O: IntoIterator<Item = CanonicalOption>,
130
1.41k
        O::IntoIter: ExactSizeIterator,
131
    {
132
1.41k
        self.bytes.push(0x01);
133
1.41k
        self.bytes.push(0x00);
134
1.41k
        func_index.encode(&mut self.bytes);
135
1.41k
        self.encode_options(options);
136
1.41k
        self.num_added += 1;
137
1.41k
        self
138
1.41k
    }
<wasm_encoder::component::canonicals::CanonicalFunctionSection>::lower::<alloc::vec::Vec<wasm_encoder::component::canonicals::CanonicalOption>>
Line
Count
Source
127
925
    pub fn lower<O>(&mut self, func_index: u32, options: O) -> &mut Self
128
925
    where
129
925
        O: IntoIterator<Item = CanonicalOption>,
130
925
        O::IntoIter: ExactSizeIterator,
131
    {
132
925
        self.bytes.push(0x01);
133
925
        self.bytes.push(0x00);
134
925
        func_index.encode(&mut self.bytes);
135
925
        self.encode_options(options);
136
925
        self.num_added += 1;
137
925
        self
138
925
    }
<wasm_encoder::component::canonicals::CanonicalFunctionSection>::lower::<<wit_component::encoding::RequiredOptions>::into_iter::Iter>
Line
Count
Source
127
491
    pub fn lower<O>(&mut self, func_index: u32, options: O) -> &mut Self
128
491
    where
129
491
        O: IntoIterator<Item = CanonicalOption>,
130
491
        O::IntoIter: ExactSizeIterator,
131
    {
132
491
        self.bytes.push(0x01);
133
491
        self.bytes.push(0x00);
134
491
        func_index.encode(&mut self.bytes);
135
491
        self.encode_options(options);
136
491
        self.num_added += 1;
137
491
        self
138
491
    }
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
234
    pub fn resource_new(&mut self, ty_index: u32) -> &mut Self {
143
234
        self.bytes.push(0x02);
144
234
        ty_index.encode(&mut self.bytes);
145
234
        self.num_added += 1;
146
234
        self
147
234
    }
148
149
    /// Defines a function which will drop the specified type of handle.
150
399
    pub fn resource_drop(&mut self, ty_index: u32) -> &mut Self {
151
399
        self.bytes.push(0x03);
152
399
        ty_index.encode(&mut self.bytes);
153
399
        self.num_added += 1;
154
399
        self
155
399
    }
156
157
    /// Defines a function which will return the representation of the specified
158
    /// resource type.
159
234
    pub fn resource_rep(&mut self, ty_index: u32) -> &mut Self {
160
234
        self.bytes.push(0x04);
161
234
        ty_index.encode(&mut self.bytes);
162
234
        self.num_added += 1;
163
234
        self
164
234
    }
165
166
    /// Defines a function which will spawn a new thread by invoking a shared
167
    /// function of type `ty_index`.
168
0
    pub fn thread_spawn_ref(&mut self, ty_index: u32) -> &mut Self {
169
0
        self.bytes.push(0x40);
170
0
        ty_index.encode(&mut self.bytes);
171
0
        self.num_added += 1;
172
0
        self
173
0
    }
174
175
    /// Defines a function which will spawn a new thread by invoking a shared
176
    /// function indirectly through a `funcref` table.
177
0
    pub fn thread_spawn_indirect(&mut self, ty_index: u32, table_index: u32) -> &mut Self {
178
0
        self.bytes.push(0x41);
179
0
        ty_index.encode(&mut self.bytes);
180
0
        table_index.encode(&mut self.bytes);
181
0
        self.num_added += 1;
182
0
        self
183
0
    }
184
185
    /// Defines a function which will return the number of threads that can be
186
    /// expected to execute concurrently.
187
0
    pub fn thread_available_parallelism(&mut self) -> &mut Self {
188
0
        self.bytes.push(0x42);
189
0
        self.num_added += 1;
190
0
        self
191
0
    }
192
193
    /// Defines a function which tells the host to increment the backpressure
194
    /// counter.
195
557
    pub fn backpressure_inc(&mut self) -> &mut Self {
196
557
        self.bytes.push(0x24);
197
557
        self.num_added += 1;
198
557
        self
199
557
    }
200
201
    /// Defines a function which tells the host to decrement the backpressure
202
    /// counter.
203
557
    pub fn backpressure_dec(&mut self) -> &mut Self {
204
557
        self.bytes.push(0x25);
205
557
        self.num_added += 1;
206
557
        self
207
557
    }
208
209
    /// Defines a function which returns a result to the caller of a lifted
210
    /// export function.  This allows the callee to continue executing after
211
    /// returning a result.
212
287
    pub fn task_return<O>(&mut self, ty: Option<ComponentValType>, options: O) -> &mut Self
213
287
    where
214
287
        O: IntoIterator<Item = CanonicalOption>,
215
287
        O::IntoIter: ExactSizeIterator,
216
    {
217
287
        self.bytes.push(0x09);
218
287
        crate::encode_resultlist(&mut self.bytes, ty);
219
287
        self.encode_options(options);
220
287
        self.num_added += 1;
221
287
        self
222
287
    }
<wasm_encoder::component::canonicals::CanonicalFunctionSection>::task_return::<alloc::vec::Vec<wasm_encoder::component::canonicals::CanonicalOption>>
Line
Count
Source
212
254
    pub fn task_return<O>(&mut self, ty: Option<ComponentValType>, options: O) -> &mut Self
213
254
    where
214
254
        O: IntoIterator<Item = CanonicalOption>,
215
254
        O::IntoIter: ExactSizeIterator,
216
    {
217
254
        self.bytes.push(0x09);
218
254
        crate::encode_resultlist(&mut self.bytes, ty);
219
254
        self.encode_options(options);
220
254
        self.num_added += 1;
221
254
        self
222
254
    }
<wasm_encoder::component::canonicals::CanonicalFunctionSection>::task_return::<<wit_component::encoding::RequiredOptions>::into_iter::Iter>
Line
Count
Source
212
33
    pub fn task_return<O>(&mut self, ty: Option<ComponentValType>, options: O) -> &mut Self
213
33
    where
214
33
        O: IntoIterator<Item = CanonicalOption>,
215
33
        O::IntoIter: ExactSizeIterator,
216
    {
217
33
        self.bytes.push(0x09);
218
33
        crate::encode_resultlist(&mut self.bytes, ty);
219
33
        self.encode_options(options);
220
33
        self.num_added += 1;
221
33
        self
222
33
    }
Unexecuted instantiation: <wasm_encoder::component::canonicals::CanonicalFunctionSection>::task_return::<_>
223
224
    /// Defines a function to acknowledge cancellation of the current task.
225
557
    pub fn task_cancel(&mut self) -> &mut Self {
226
557
        self.bytes.push(0x05);
227
557
        self.num_added += 1;
228
557
        self
229
557
    }
230
231
    /// Defines a new `context.get` intrinsic of the ith slot with the given
232
    /// value type.
233
557
    pub fn context_get(&mut self, ty: ValType, i: u32) -> &mut Self {
234
557
        self.bytes.push(0x0a);
235
557
        ty.encode(&mut self.bytes);
236
557
        i.encode(&mut self.bytes);
237
557
        self.num_added += 1;
238
557
        self
239
557
    }
240
241
    /// Defines a new `context.set` intrinsic of the ith slot with the given
242
    /// value type.
243
557
    pub fn context_set(&mut self, ty: ValType, i: u32) -> &mut Self {
244
557
        self.bytes.push(0x0b);
245
557
        ty.encode(&mut self.bytes);
246
557
        i.encode(&mut self.bytes);
247
557
        self.num_added += 1;
248
557
        self
249
557
    }
250
251
    /// Defines a function to drop a specified task which has completed.
252
557
    pub fn subtask_drop(&mut self) -> &mut Self {
253
557
        self.bytes.push(0x0d);
254
557
        self.num_added += 1;
255
557
        self
256
557
    }
257
258
    /// Defines a function to cancel an in-progress task.
259
557
    pub fn subtask_cancel(&mut self, async_: bool) -> &mut Self {
260
557
        self.bytes.push(0x06);
261
557
        self.bytes.push(if async_ { 1 } else { 0 });
262
557
        self.num_added += 1;
263
557
        self
264
557
    }
265
266
    /// Defines a function to create a new `stream` handle of the specified
267
    /// type.
268
85
    pub fn stream_new(&mut self, ty: u32) -> &mut Self {
269
85
        self.bytes.push(0x0e);
270
85
        ty.encode(&mut self.bytes);
271
85
        self.num_added += 1;
272
85
        self
273
85
    }
274
275
    /// Defines a function to read from a `stream` of the specified type.
276
85
    pub fn stream_read<O>(&mut self, ty: u32, options: O) -> &mut Self
277
85
    where
278
85
        O: IntoIterator<Item = CanonicalOption>,
279
85
        O::IntoIter: ExactSizeIterator,
280
    {
281
85
        self.bytes.push(0x0f);
282
85
        ty.encode(&mut self.bytes);
283
85
        self.encode_options(options);
284
85
        self.num_added += 1;
285
85
        self
286
85
    }
<wasm_encoder::component::canonicals::CanonicalFunctionSection>::stream_read::<<wit_component::encoding::RequiredOptions>::into_iter::Iter>
Line
Count
Source
276
85
    pub fn stream_read<O>(&mut self, ty: u32, options: O) -> &mut Self
277
85
    where
278
85
        O: IntoIterator<Item = CanonicalOption>,
279
85
        O::IntoIter: ExactSizeIterator,
280
    {
281
85
        self.bytes.push(0x0f);
282
85
        ty.encode(&mut self.bytes);
283
85
        self.encode_options(options);
284
85
        self.num_added += 1;
285
85
        self
286
85
    }
Unexecuted instantiation: <wasm_encoder::component::canonicals::CanonicalFunctionSection>::stream_read::<_>
287
288
    /// Defines a function to write to a `stream` of the specified type.
289
85
    pub fn stream_write<O>(&mut self, ty: u32, options: O) -> &mut Self
290
85
    where
291
85
        O: IntoIterator<Item = CanonicalOption>,
292
85
        O::IntoIter: ExactSizeIterator,
293
    {
294
85
        self.bytes.push(0x10);
295
85
        ty.encode(&mut self.bytes);
296
85
        self.encode_options(options);
297
85
        self.num_added += 1;
298
85
        self
299
85
    }
<wasm_encoder::component::canonicals::CanonicalFunctionSection>::stream_write::<<wit_component::encoding::RequiredOptions>::into_iter::Iter>
Line
Count
Source
289
85
    pub fn stream_write<O>(&mut self, ty: u32, options: O) -> &mut Self
290
85
    where
291
85
        O: IntoIterator<Item = CanonicalOption>,
292
85
        O::IntoIter: ExactSizeIterator,
293
    {
294
85
        self.bytes.push(0x10);
295
85
        ty.encode(&mut self.bytes);
296
85
        self.encode_options(options);
297
85
        self.num_added += 1;
298
85
        self
299
85
    }
Unexecuted instantiation: <wasm_encoder::component::canonicals::CanonicalFunctionSection>::stream_write::<_>
300
301
    /// Defines a function to forward all remaining elements from the readable
302
    /// end of one `stream` to the writable end of another `stream` of the
303
    /// specified type, transferring both ends out of the calling instance.
304
85
    pub fn stream_forward(&mut self, ty: u32) -> &mut Self {
305
85
        self.bytes.push(0x2e);
306
85
        ty.encode(&mut self.bytes);
307
85
        self.num_added += 1;
308
85
        self
309
85
    }
310
311
    /// Defines a function to cancel an in-progress read from a `stream` of the
312
    /// specified type.
313
85
    pub fn stream_cancel_read(&mut self, ty: u32, async_: bool) -> &mut Self {
314
85
        self.bytes.push(0x11);
315
85
        ty.encode(&mut self.bytes);
316
85
        self.bytes.push(if async_ { 1 } else { 0 });
317
85
        self.num_added += 1;
318
85
        self
319
85
    }
320
321
    /// Defines a function to cancel an in-progress write to a `stream` of the
322
    /// specified type.
323
85
    pub fn stream_cancel_write(&mut self, ty: u32, async_: bool) -> &mut Self {
324
85
        self.bytes.push(0x12);
325
85
        ty.encode(&mut self.bytes);
326
85
        self.bytes.push(if async_ { 1 } else { 0 });
327
85
        self.num_added += 1;
328
85
        self
329
85
    }
330
331
    /// Defines a function to drop the readable end of a `stream` of the
332
    /// specified type.
333
85
    pub fn stream_drop_readable(&mut self, ty: u32) -> &mut Self {
334
85
        self.bytes.push(0x13);
335
85
        ty.encode(&mut self.bytes);
336
85
        self.num_added += 1;
337
85
        self
338
85
    }
339
340
    /// Defines a function to drop the writable end of a `stream` of the
341
    /// specified type.
342
85
    pub fn stream_drop_writable(&mut self, ty: u32) -> &mut Self {
343
85
        self.bytes.push(0x14);
344
85
        ty.encode(&mut self.bytes);
345
85
        self.num_added += 1;
346
85
        self
347
85
    }
348
349
    /// Defines a function to create a new `future` handle of the specified
350
    /// type.
351
145
    pub fn future_new(&mut self, ty: u32) -> &mut Self {
352
145
        self.bytes.push(0x15);
353
145
        ty.encode(&mut self.bytes);
354
145
        self.num_added += 1;
355
145
        self
356
145
    }
357
358
    /// Defines a function to read from a `future` of the specified type.
359
121
    pub fn future_read<O>(&mut self, ty: u32, options: O) -> &mut Self
360
121
    where
361
121
        O: IntoIterator<Item = CanonicalOption>,
362
121
        O::IntoIter: ExactSizeIterator,
363
    {
364
121
        self.bytes.push(0x16);
365
121
        ty.encode(&mut self.bytes);
366
121
        self.encode_options(options);
367
121
        self.num_added += 1;
368
121
        self
369
121
    }
<wasm_encoder::component::canonicals::CanonicalFunctionSection>::future_read::<<wit_component::encoding::RequiredOptions>::into_iter::Iter>
Line
Count
Source
359
121
    pub fn future_read<O>(&mut self, ty: u32, options: O) -> &mut Self
360
121
    where
361
121
        O: IntoIterator<Item = CanonicalOption>,
362
121
        O::IntoIter: ExactSizeIterator,
363
    {
364
121
        self.bytes.push(0x16);
365
121
        ty.encode(&mut self.bytes);
366
121
        self.encode_options(options);
367
121
        self.num_added += 1;
368
121
        self
369
121
    }
Unexecuted instantiation: <wasm_encoder::component::canonicals::CanonicalFunctionSection>::future_read::<_>
370
371
    /// Defines a function to write to a `future` of the specified type.
372
121
    pub fn future_write<O>(&mut self, ty: u32, options: O) -> &mut Self
373
121
    where
374
121
        O: IntoIterator<Item = CanonicalOption>,
375
121
        O::IntoIter: ExactSizeIterator,
376
    {
377
121
        self.bytes.push(0x17);
378
121
        ty.encode(&mut self.bytes);
379
121
        self.encode_options(options);
380
121
        self.num_added += 1;
381
121
        self
382
121
    }
<wasm_encoder::component::canonicals::CanonicalFunctionSection>::future_write::<<wit_component::encoding::RequiredOptions>::into_iter::Iter>
Line
Count
Source
372
121
    pub fn future_write<O>(&mut self, ty: u32, options: O) -> &mut Self
373
121
    where
374
121
        O: IntoIterator<Item = CanonicalOption>,
375
121
        O::IntoIter: ExactSizeIterator,
376
    {
377
121
        self.bytes.push(0x17);
378
121
        ty.encode(&mut self.bytes);
379
121
        self.encode_options(options);
380
121
        self.num_added += 1;
381
121
        self
382
121
    }
Unexecuted instantiation: <wasm_encoder::component::canonicals::CanonicalFunctionSection>::future_write::<_>
383
384
    /// Defines a function to forward the value of the readable end of one
385
    /// `future` to the writable end of another `future` of the specified
386
    /// type, transferring both ends out of the calling instance.
387
145
    pub fn future_forward(&mut self, ty: u32) -> &mut Self {
388
145
        self.bytes.push(0x2f);
389
145
        ty.encode(&mut self.bytes);
390
145
        self.num_added += 1;
391
145
        self
392
145
    }
393
394
    /// Defines a function to cancel an in-progress read from a `future` of the
395
    /// specified type.
396
145
    pub fn future_cancel_read(&mut self, ty: u32, async_: bool) -> &mut Self {
397
145
        self.bytes.push(0x18);
398
145
        ty.encode(&mut self.bytes);
399
145
        self.bytes.push(if async_ { 1 } else { 0 });
400
145
        self.num_added += 1;
401
145
        self
402
145
    }
403
404
    /// Defines a function to cancel an in-progress write to a `future` of the
405
    /// specified type.
406
145
    pub fn future_cancel_write(&mut self, ty: u32, async_: bool) -> &mut Self {
407
145
        self.bytes.push(0x19);
408
145
        ty.encode(&mut self.bytes);
409
145
        self.bytes.push(if async_ { 1 } else { 0 });
410
145
        self.num_added += 1;
411
145
        self
412
145
    }
413
414
    /// Defines a function to drop the readable end of a `future` of the
415
    /// specified type.
416
145
    pub fn future_drop_readable(&mut self, ty: u32) -> &mut Self {
417
145
        self.bytes.push(0x1a);
418
145
        ty.encode(&mut self.bytes);
419
145
        self.num_added += 1;
420
145
        self
421
145
    }
422
423
    /// Defines a function to drop the writable end of a `future` of the
424
    /// specified type.
425
145
    pub fn future_drop_writable(&mut self, ty: u32) -> &mut Self {
426
145
        self.bytes.push(0x1b);
427
145
        ty.encode(&mut self.bytes);
428
145
        self.num_added += 1;
429
145
        self
430
145
    }
431
432
    /// Defines a function to create a new `error-context` with a specified
433
    /// debug message.
434
0
    pub fn error_context_new<O>(&mut self, options: O) -> &mut Self
435
0
    where
436
0
        O: IntoIterator<Item = CanonicalOption>,
437
0
        O::IntoIter: ExactSizeIterator,
438
    {
439
0
        self.bytes.push(0x1c);
440
0
        self.encode_options(options);
441
0
        self.num_added += 1;
442
0
        self
443
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::<_>
444
445
    /// Defines a function to get the debug message for a specified
446
    /// `error-context`.
447
    ///
448
    /// Note that the debug message might not necessarily match what was passed
449
    /// to `error-context.new`.
450
0
    pub fn error_context_debug_message<O>(&mut self, options: O) -> &mut Self
451
0
    where
452
0
        O: IntoIterator<Item = CanonicalOption>,
453
0
        O::IntoIter: ExactSizeIterator,
454
    {
455
0
        self.bytes.push(0x1d);
456
0
        self.encode_options(options);
457
0
        self.num_added += 1;
458
0
        self
459
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::<_>
460
461
    /// Defines a function to drop a specified `error-context`.
462
0
    pub fn error_context_drop(&mut self) -> &mut Self {
463
0
        self.bytes.push(0x1e);
464
0
        self.num_added += 1;
465
0
        self
466
0
    }
467
468
    /// Declare a new `waitable-set.new` intrinsic, used to create a
469
    /// `waitable-set` pseudo-resource.
470
557
    pub fn waitable_set_new(&mut self) -> &mut Self {
471
557
        self.bytes.push(0x1f);
472
557
        self.num_added += 1;
473
557
        self
474
557
    }
475
476
    /// Declare a new `waitable-set.wait` intrinsic, used to block on a
477
    /// `waitable-set`.
478
557
    pub fn waitable_set_wait(&mut self, memory: u32) -> &mut Self {
479
557
        self.bytes.push(0x20);
480
557
        self.bytes.push(0);
481
557
        memory.encode(&mut self.bytes);
482
557
        self.num_added += 1;
483
557
        self
484
557
    }
485
486
    /// Declare a new `waitable-set.wait` intrinsic, used to check, without
487
    /// blocking, if anything in a `waitable-set` is ready.
488
557
    pub fn waitable_set_poll(&mut self, memory: u32) -> &mut Self {
489
557
        self.bytes.push(0x21);
490
557
        self.bytes.push(0);
491
557
        memory.encode(&mut self.bytes);
492
557
        self.num_added += 1;
493
557
        self
494
557
    }
495
496
    /// Declare a new `waitable-set.drop` intrinsic, used to dispose a
497
    /// `waitable-set` pseudo-resource.
498
557
    pub fn waitable_set_drop(&mut self) -> &mut Self {
499
557
        self.bytes.push(0x22);
500
557
        self.num_added += 1;
501
557
        self
502
557
    }
503
504
    /// Declare a new `waitable.join` intrinsic, used to add an item to a
505
    /// `waitable-set`.
506
557
    pub fn waitable_join(&mut self) -> &mut Self {
507
557
        self.bytes.push(0x23);
508
557
        self.num_added += 1;
509
557
        self
510
557
    }
511
512
    /// Declare a new `thread.index` intrinsic, used to get the index of the
513
    /// current thread.
514
0
    pub fn thread_index(&mut self) -> &mut Self {
515
0
        self.bytes.push(0x26);
516
0
        self.num_added += 1;
517
0
        self
518
0
    }
519
520
    /// Declare a new `thread.new-indirect` intrinsic, used to create a new
521
    /// thread by invoking a function indirectly through a `funcref` table.
522
0
    pub fn thread_new_indirect(&mut self, ty_index: u32, table_index: u32) -> &mut Self {
523
0
        self.bytes.push(0x27);
524
0
        ty_index.encode(&mut self.bytes);
525
0
        table_index.encode(&mut self.bytes);
526
0
        self.num_added += 1;
527
0
        self
528
0
    }
529
530
    /// Declare a new `thread.resume-later` intrinsic.
531
0
    pub fn thread_resume_later(&mut self) -> &mut Self {
532
0
        self.bytes.push(0x28);
533
0
        self.num_added += 1;
534
0
        self
535
0
    }
536
537
    /// Declare a new `thread.suspend` intrinsic.
538
0
    pub fn thread_suspend(&mut self) -> &mut Self {
539
0
        self.bytes.push(0x29);
540
0
        self.bytes.push(0);
541
0
        self.num_added += 1;
542
0
        self
543
0
    }
544
545
    /// Declare a new `thread.yield` intrinsic.
546
557
    pub fn thread_yield(&mut self) -> &mut Self {
547
557
        self.bytes.push(0x0c);
548
557
        self.bytes.push(0);
549
557
        self.num_added += 1;
550
557
        self
551
557
    }
552
553
    /// Declare a new `thread.suspend-then-resume` intrinsic.
554
0
    pub fn thread_suspend_then_resume(&mut self) -> &mut Self {
555
0
        self.bytes.push(0x2a);
556
0
        self.bytes.push(0);
557
0
        self.num_added += 1;
558
0
        self
559
0
    }
560
561
    /// Declare a new `thread.yield-then-resume` intrinsic.
562
0
    pub fn thread_yield_then_resume(&mut self) -> &mut Self {
563
0
        self.bytes.push(0x2b);
564
0
        self.bytes.push(0);
565
0
        self.num_added += 1;
566
0
        self
567
0
    }
568
569
    /// Declare a new `thread.suspend-then-promote` intrinsic.
570
0
    pub fn thread_suspend_then_promote(&mut self) -> &mut Self {
571
0
        self.bytes.push(0x2c);
572
0
        self.bytes.push(0);
573
0
        self.num_added += 1;
574
0
        self
575
0
    }
576
577
    /// Declare a new `thread.yield-then-promote` intrinsic.
578
0
    pub fn thread_yield_then_promote(&mut self) -> &mut Self {
579
0
        self.bytes.push(0x2d);
580
0
        self.bytes.push(0);
581
0
        self.num_added += 1;
582
0
        self
583
0
    }
584
585
4.53k
    fn encode_options<O>(&mut self, options: O) -> &mut Self
586
4.53k
    where
587
4.53k
        O: IntoIterator<Item = CanonicalOption>,
588
4.53k
        O::IntoIter: ExactSizeIterator,
589
    {
590
4.53k
        let options = options.into_iter();
591
4.53k
        options.len().encode(&mut self.bytes);
592
5.45k
        for option in options {
593
5.45k
            option.encode(&mut self.bytes);
594
5.45k
        }
595
4.53k
        self
596
4.53k
    }
<wasm_encoder::component::canonicals::CanonicalFunctionSection>::encode_options::<alloc::vec::Vec<wasm_encoder::component::canonicals::CanonicalOption>>
Line
Count
Source
585
3.59k
    fn encode_options<O>(&mut self, options: O) -> &mut Self
586
3.59k
    where
587
3.59k
        O: IntoIterator<Item = CanonicalOption>,
588
3.59k
        O::IntoIter: ExactSizeIterator,
589
    {
590
3.59k
        let options = options.into_iter();
591
3.59k
        options.len().encode(&mut self.bytes);
592
3.67k
        for option in options {
593
3.67k
            option.encode(&mut self.bytes);
594
3.67k
        }
595
3.59k
        self
596
3.59k
    }
<wasm_encoder::component::canonicals::CanonicalFunctionSection>::encode_options::<<wit_component::encoding::RequiredOptions>::into_iter::Iter>
Line
Count
Source
585
936
    fn encode_options<O>(&mut self, options: O) -> &mut Self
586
936
    where
587
936
        O: IntoIterator<Item = CanonicalOption>,
588
936
        O::IntoIter: ExactSizeIterator,
589
    {
590
936
        let options = options.into_iter();
591
936
        options.len().encode(&mut self.bytes);
592
1.78k
        for option in options {
593
1.78k
            option.encode(&mut self.bytes);
594
1.78k
        }
595
936
        self
596
936
    }
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::<_>
597
}
598
599
impl Encode for CanonicalFunctionSection {
600
6.50k
    fn encode(&self, sink: &mut Vec<u8>) {
601
6.50k
        encode_section(sink, self.num_added, &self.bytes);
602
6.50k
    }
603
}
604
605
impl ComponentSection for CanonicalFunctionSection {
606
6.50k
    fn id(&self) -> u8 {
607
6.50k
        ComponentSectionId::CanonicalFunction.into()
608
6.50k
    }
609
}