From 10949db43a3d39d1d9e676d8b8f7b5f8dd4e3793 Mon Sep 17 00:00:00 2001 From: xortoast Date: Thu, 2 Nov 2023 23:14:41 +0100 Subject: [PATCH] wasm-encoder: Recursion groups (#1276) --- crates/wasm-encoder/src/core/types.rs | 33 +++++++++++++++++++++------ 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/crates/wasm-encoder/src/core/types.rs b/crates/wasm-encoder/src/core/types.rs index d28d863f70..6fc410e0f1 100644 --- a/crates/wasm-encoder/src/core/types.rs +++ b/crates/wasm-encoder/src/core/types.rs @@ -11,6 +11,18 @@ pub struct SubType { pub composite_type: CompositeType, } +impl Encode for SubType { + fn encode(&self, sink: &mut Vec) { + // We only need to emit a prefix byte before the actual composite type + // when either the type is not final or it has a declared super type. + if self.supertype_idx.is_some() || !self.is_final { + sink.push(if self.is_final { 0x4f } else { 0x50 }); + self.supertype_idx.encode(sink); + } + self.composite_type.encode(sink); + } +} + #[cfg(feature = "wasmparser")] impl From for SubType { fn from(sub_ty: wasmparser::SubType) -> Self { @@ -510,14 +522,21 @@ impl TypeSection { /// Define an explicit subtype in this type section. pub fn subtype(&mut self, ty: &SubType) -> &mut Self { - // We only need to emit a prefix byte before the actual composite type - // when either the type is not final or it has a declared super type. - if ty.supertype_idx.is_some() || !ty.is_final { - self.bytes.push(if ty.is_final { 0x4f } else { 0x50 }); - ty.supertype_idx.encode(&mut self.bytes); - } + ty.encode(&mut self.bytes); + self.num_added += 1; + self + } - ty.composite_type.encode(&mut self.bytes); + /// Define an explicit recursion group in this type section. + pub fn rec(&mut self, types: T) -> &mut Self + where + T: IntoIterator, + T::IntoIter: ExactSizeIterator, + { + let types = types.into_iter(); + self.bytes.push(0x4e); + types.len().encode(&mut self.bytes); + types.for_each(|t| t.encode(&mut self.bytes)); self.num_added += 1; self }