Skip to content

Commit 10949db

Browse files
authored
wasm-encoder: Recursion groups (#1276)
1 parent e808006 commit 10949db

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

crates/wasm-encoder/src/core/types.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ pub struct SubType {
1111
pub composite_type: CompositeType,
1212
}
1313

14+
impl Encode for SubType {
15+
fn encode(&self, sink: &mut Vec<u8>) {
16+
// We only need to emit a prefix byte before the actual composite type
17+
// when either the type is not final or it has a declared super type.
18+
if self.supertype_idx.is_some() || !self.is_final {
19+
sink.push(if self.is_final { 0x4f } else { 0x50 });
20+
self.supertype_idx.encode(sink);
21+
}
22+
self.composite_type.encode(sink);
23+
}
24+
}
25+
1426
#[cfg(feature = "wasmparser")]
1527
impl From<wasmparser::SubType> for SubType {
1628
fn from(sub_ty: wasmparser::SubType) -> Self {
@@ -510,14 +522,21 @@ impl TypeSection {
510522

511523
/// Define an explicit subtype in this type section.
512524
pub fn subtype(&mut self, ty: &SubType) -> &mut Self {
513-
// We only need to emit a prefix byte before the actual composite type
514-
// when either the type is not final or it has a declared super type.
515-
if ty.supertype_idx.is_some() || !ty.is_final {
516-
self.bytes.push(if ty.is_final { 0x4f } else { 0x50 });
517-
ty.supertype_idx.encode(&mut self.bytes);
518-
}
525+
ty.encode(&mut self.bytes);
526+
self.num_added += 1;
527+
self
528+
}
519529

520-
ty.composite_type.encode(&mut self.bytes);
530+
/// Define an explicit recursion group in this type section.
531+
pub fn rec<T>(&mut self, types: T) -> &mut Self
532+
where
533+
T: IntoIterator<Item = SubType>,
534+
T::IntoIter: ExactSizeIterator,
535+
{
536+
let types = types.into_iter();
537+
self.bytes.push(0x4e);
538+
types.len().encode(&mut self.bytes);
539+
types.for_each(|t| t.encode(&mut self.bytes));
521540
self.num_added += 1;
522541
self
523542
}

0 commit comments

Comments
 (0)