diff --git a/crates/wasm-encoder/README.md b/crates/wasm-encoder/README.md index 5f2efbe210..dc41ab66bc 100644 --- a/crates/wasm-encoder/README.md +++ b/crates/wasm-encoder/README.md @@ -36,12 +36,11 @@ let mut module = Module::new(); let mut types = TypeSection::new(); let params = vec![ValType::I32, ValType::I32]; let results = vec![ValType::I32]; -types.function(params, results); +let type_index = types.function(params, results); module.section(&types); // Encode the function section. let mut functions = FunctionSection::new(); -let type_index = 0; functions.function(type_index); module.section(&functions); diff --git a/crates/wasm-encoder/src/core/code.rs b/crates/wasm-encoder/src/core/code.rs index 8fd62c0f72..1b051044ce 100644 --- a/crates/wasm-encoder/src/core/code.rs +++ b/crates/wasm-encoder/src/core/code.rs @@ -14,10 +14,9 @@ use std::borrow::Cow; /// }; /// /// let mut types = TypeSection::new(); -/// types.function(vec![], vec![ValType::I32]); +/// let type_index = types.function(vec![], vec![ValType::I32]); /// /// let mut functions = FunctionSection::new(); -/// let type_index = 0; /// functions.function(type_index); /// /// let locals = vec![]; diff --git a/crates/wasm-encoder/src/core/data.rs b/crates/wasm-encoder/src/core/data.rs index 6439b66e2e..c47074677c 100644 --- a/crates/wasm-encoder/src/core/data.rs +++ b/crates/wasm-encoder/src/core/data.rs @@ -81,7 +81,7 @@ impl DataSection { } /// Define a data segment. - pub fn segment(&mut self, segment: DataSegment) -> &mut Self + pub fn segment(&mut self, segment: DataSegment) -> u32 where D: IntoIterator, D::IntoIter: ExactSizeIterator, @@ -111,12 +111,13 @@ impl DataSection { data.len().encode(&mut self.bytes); self.bytes.extend(data); + let index = self.num_added; self.num_added += 1; - self + index } /// Define an active data segment. - pub fn active(&mut self, memory_index: u32, offset: &ConstExpr, data: D) -> &mut Self + pub fn active(&mut self, memory_index: u32, offset: &ConstExpr, data: D) -> u32 where D: IntoIterator, D::IntoIter: ExactSizeIterator, @@ -133,7 +134,7 @@ impl DataSection { /// Define a passive data segment. /// /// Passive data segments are part of the bulk memory proposal. - pub fn passive(&mut self, data: D) -> &mut Self + pub fn passive(&mut self, data: D) -> u32 where D: IntoIterator, D::IntoIter: ExactSizeIterator, @@ -145,10 +146,11 @@ impl DataSection { } /// Copy an already-encoded data segment into this data section. - pub fn raw(&mut self, already_encoded_data_segment: &[u8]) -> &mut Self { + pub fn raw(&mut self, already_encoded_data_segment: &[u8]) -> u32 { self.bytes.extend_from_slice(already_encoded_data_segment); + let index = self.num_added; self.num_added += 1; - self + index } } diff --git a/crates/wasm-encoder/src/core/elements.rs b/crates/wasm-encoder/src/core/elements.rs index 5eb641247b..b67bd42b2c 100644 --- a/crates/wasm-encoder/src/core/elements.rs +++ b/crates/wasm-encoder/src/core/elements.rs @@ -102,7 +102,7 @@ impl ElementSection { } /// Define an element segment. - pub fn segment<'a>(&mut self, segment: ElementSegment<'a>) -> &mut Self { + pub fn segment<'a>(&mut self, segment: ElementSegment<'a>) -> u32 { let expr_bit = match segment.elements { Elements::Expressions(_) => 0b100u32, Elements::Functions(_) => 0b000u32, @@ -158,8 +158,9 @@ impl ElementSection { } } + let index = self.num_added; self.num_added += 1; - self + index } /// Define an active element segment. @@ -173,7 +174,7 @@ impl ElementSection { offset: &ConstExpr, element_type: ValType, elements: Elements<'_>, - ) -> &mut Self { + ) -> u32 { self.segment(ElementSegment { mode: ElementMode::Active { table: table_index, @@ -187,7 +188,7 @@ impl ElementSection { /// Encode a passive element segment. /// /// Passive segments are part of the bulk memory proposal. - pub fn passive<'a>(&mut self, element_type: ValType, elements: Elements<'a>) -> &mut Self { + pub fn passive<'a>(&mut self, element_type: ValType, elements: Elements<'a>) -> u32 { self.segment(ElementSegment { mode: ElementMode::Passive, element_type, @@ -198,7 +199,7 @@ impl ElementSection { /// Encode a declared element segment. /// /// Declared segments are part of the bulk memory proposal. - pub fn declared<'a>(&mut self, element_type: ValType, elements: Elements<'a>) -> &mut Self { + pub fn declared<'a>(&mut self, element_type: ValType, elements: Elements<'a>) -> u32 { self.segment(ElementSegment { mode: ElementMode::Declared, element_type, @@ -207,10 +208,11 @@ impl ElementSection { } /// Copy a raw, already-encoded element segment into this elements section. - pub fn raw(&mut self, raw_bytes: &[u8]) -> &mut Self { + pub fn raw(&mut self, raw_bytes: &[u8]) -> u32 { self.bytes.extend_from_slice(raw_bytes); + let index = self.num_added; self.num_added += 1; - self + index } } diff --git a/crates/wasm-encoder/src/core/types.rs b/crates/wasm-encoder/src/core/types.rs index d99e22ea94..03f9d829e4 100644 --- a/crates/wasm-encoder/src/core/types.rs +++ b/crates/wasm-encoder/src/core/types.rs @@ -79,7 +79,7 @@ impl TypeSection { } /// Define a function type in this type section. - pub fn function(&mut self, params: P, results: R) -> &mut Self + pub fn function(&mut self, params: P, results: R) -> u32 where P: IntoIterator, P::IntoIter: ExactSizeIterator, @@ -94,8 +94,10 @@ impl TypeSection { self.bytes.extend(params.map(u8::from)); results.len().encode(&mut self.bytes); self.bytes.extend(results.map(u8::from)); + + let index = self.num_added; self.num_added += 1; - self + index } } diff --git a/crates/wasm-encoder/src/lib.rs b/crates/wasm-encoder/src/lib.rs index 48a53cf9c7..5d2d8ef5f9 100644 --- a/crates/wasm-encoder/src/lib.rs +++ b/crates/wasm-encoder/src/lib.rs @@ -36,12 +36,11 @@ //! let mut types = TypeSection::new(); //! let params = vec![ValType::I32, ValType::I32]; //! let results = vec![ValType::I32]; -//! types.function(params, results); +//! let type_index = types.function(params, results); //! module.section(&types); //! //! // Encode the function section. //! let mut functions = FunctionSection::new(); -//! let type_index = 0; //! functions.function(type_index); //! module.section(&functions); //! diff --git a/crates/wasm-smith/src/component/encode.rs b/crates/wasm-smith/src/component/encode.rs index f2bfe734fc..b816404ffd 100644 --- a/crates/wasm-smith/src/component/encode.rs +++ b/crates/wasm-smith/src/component/encode.rs @@ -223,7 +223,9 @@ impl Type { impl DefinedType { fn encode(&self, enc: wasm_encoder::ComponentDefinedTypeEncoder<'_>) { match self { - Self::Primitive(ty) => enc.primitive(*ty), + Self::Primitive(ty) => { + enc.primitive(*ty); + } Self::Record(ty) => { enc.record(ty.fields.iter().map(|(name, ty)| (name.as_str(), *ty))); } diff --git a/crates/wast/src/component/binary.rs b/crates/wast/src/component/binary.rs index 709c613cfc..dce92978cc 100644 --- a/crates/wast/src/component/binary.rs +++ b/crates/wast/src/component/binary.rs @@ -95,7 +95,9 @@ fn encode_type(encoder: ComponentTypeEncoder, ty: &TypeDef) { fn encode_defined_type(encoder: ComponentDefinedTypeEncoder, ty: &ComponentDefinedType) { match ty { - ComponentDefinedType::Primitive(p) => encoder.primitive((*p).into()), + ComponentDefinedType::Primitive(p) => { + encoder.primitive((*p).into()); + }, ComponentDefinedType::Record(r) => { encoder.record(r.fields.iter().map(|f| (f.name, &f.ty))); } @@ -120,7 +122,9 @@ fn encode_defined_type(encoder: ComponentDefinedTypeEncoder, ty: &ComponentDefin ComponentDefinedType::Enum(e) => { encoder.enum_type(e.names.iter().copied()); } - ComponentDefinedType::Union(u) => encoder.union(u.types.iter()), + ComponentDefinedType::Union(u) => { + encoder.union(u.types.iter()); + }, ComponentDefinedType::Option(o) => { encoder.option(o.element.as_ref()); } @@ -820,10 +824,12 @@ impl From<&ModuleType<'_>> for wasm_encoder::ModuleType { for decl in &ty.decls { match decl { ModuleTypeDecl::Type(t) => match &t.def { - core::TypeDef::Func(f) => encoded.ty().function( - f.params.iter().map(|(_, _, ty)| (*ty).into()), - f.results.iter().copied().map(Into::into), - ), + core::TypeDef::Func(f) => { + encoded.ty().function( + f.params.iter().map(|(_, _, ty)| (*ty).into()), + f.results.iter().copied().map(Into::into), + ); + }, core::TypeDef::Struct(_) | core::TypeDef::Array(_) => { todo!("encoding of GC proposal types not yet implemented") }