From 1a1844adb762bf5c865f721d20fda17b4e9a8fcb Mon Sep 17 00:00:00 2001 From: Kyle Brown Date: Fri, 6 Jan 2023 16:30:22 -0500 Subject: [PATCH 1/7] Return indices instead of &mut Self --- crates/wasm-encoder/src/component/aliases.rs | 5 +- .../wasm-encoder/src/component/canonicals.rs | 12 +- crates/wasm-encoder/src/component/exports.rs | 6 +- crates/wasm-encoder/src/component/imports.rs | 9 +- .../wasm-encoder/src/component/instances.rs | 23 +- crates/wasm-encoder/src/component/types.rs | 261 +++++++++++------- crates/wasm-encoder/src/core/code.rs | 13 +- crates/wasm-encoder/src/core/data.rs | 17 +- crates/wasm-encoder/src/core/elements.rs | 19 +- crates/wasm-encoder/src/core/exports.rs | 5 +- crates/wasm-encoder/src/core/functions.rs | 5 +- crates/wasm-encoder/src/core/globals.rs | 10 +- crates/wasm-encoder/src/core/imports.rs | 5 +- crates/wasm-encoder/src/core/linking.rs | 20 +- crates/wasm-encoder/src/core/memories.rs | 5 +- crates/wasm-encoder/src/core/tables.rs | 5 +- crates/wasm-encoder/src/core/tags.rs | 5 +- crates/wasm-encoder/src/core/types.rs | 6 +- crates/wasm-encoder/src/lib.rs | 3 +- 19 files changed, 261 insertions(+), 173 deletions(-) diff --git a/crates/wasm-encoder/src/component/aliases.rs b/crates/wasm-encoder/src/component/aliases.rs index 1e317fb0e2..9c98c958e1 100644 --- a/crates/wasm-encoder/src/component/aliases.rs +++ b/crates/wasm-encoder/src/component/aliases.rs @@ -106,10 +106,11 @@ impl ComponentAliasSection { } /// Define an alias to a component instance's export. - pub fn alias(&mut self, alias: Alias<'_>) -> &mut Self { + pub fn alias(&mut self, alias: Alias<'_>) -> u32 { alias.encode(&mut self.bytes); + let index = self.num_added; self.num_added += 1; - self + index } } diff --git a/crates/wasm-encoder/src/component/canonicals.rs b/crates/wasm-encoder/src/component/canonicals.rs index e81819c44e..c7425a4432 100644 --- a/crates/wasm-encoder/src/component/canonicals.rs +++ b/crates/wasm-encoder/src/component/canonicals.rs @@ -83,7 +83,7 @@ impl CanonicalFunctionSection { } /// Define a function that will lift a core WebAssembly function to the canonical ABI. - pub fn lift(&mut self, core_func_index: u32, type_index: u32, options: O) -> &mut Self + pub fn lift(&mut self, core_func_index: u32, type_index: u32, options: O) -> u32 where O: IntoIterator, O::IntoIter: ExactSizeIterator, @@ -97,12 +97,14 @@ impl CanonicalFunctionSection { option.encode(&mut self.bytes); } type_index.encode(&mut self.bytes); + + let index = self.num_added; self.num_added += 1; - self + index } /// Define a function that will lower a canonical ABI function to a core WebAssembly function. - pub fn lower(&mut self, func_index: u32, options: O) -> &mut Self + pub fn lower(&mut self, func_index: u32, options: O) -> u32 where O: IntoIterator, O::IntoIter: ExactSizeIterator, @@ -115,8 +117,10 @@ impl CanonicalFunctionSection { for option in options { option.encode(&mut self.bytes); } + + let index = self.num_added; self.num_added += 1; - self + index } } diff --git a/crates/wasm-encoder/src/component/exports.rs b/crates/wasm-encoder/src/component/exports.rs index 100a956c14..da4c9522a1 100644 --- a/crates/wasm-encoder/src/component/exports.rs +++ b/crates/wasm-encoder/src/component/exports.rs @@ -92,13 +92,15 @@ impl ComponentExportSection { url: &str, kind: ComponentExportKind, index: u32, - ) -> &mut Self { + ) -> u32 { name.encode(&mut self.bytes); url.encode(&mut self.bytes); kind.encode(&mut self.bytes); index.encode(&mut self.bytes); + + let index = self.num_added; self.num_added += 1; - self + index } } diff --git a/crates/wasm-encoder/src/component/imports.rs b/crates/wasm-encoder/src/component/imports.rs index 8d843407f3..69733ab66c 100644 --- a/crates/wasm-encoder/src/component/imports.rs +++ b/crates/wasm-encoder/src/component/imports.rs @@ -94,7 +94,8 @@ impl Encode for ComponentTypeRef { /// ("b", PrimitiveValType::String) /// ] /// ) -/// .result(PrimitiveValType::String); +/// .result(PrimitiveValType::String) +/// .finish(); /// /// // This imports a function named `f` with the type defined above /// let mut imports = ComponentImportSection::new(); @@ -129,12 +130,14 @@ impl ComponentImportSection { } /// Define an import in the component import section. - pub fn import(&mut self, name: &str, url: &str, ty: ComponentTypeRef) -> &mut Self { + pub fn import(&mut self, name: &str, url: &str, ty: ComponentTypeRef) -> u32 { name.encode(&mut self.bytes); url.encode(&mut self.bytes); ty.encode(&mut self.bytes); + + let index = self.num_added; self.num_added += 1; - self + index } } diff --git a/crates/wasm-encoder/src/component/instances.rs b/crates/wasm-encoder/src/component/instances.rs index 000a872d6b..c5ff6f63fb 100644 --- a/crates/wasm-encoder/src/component/instances.rs +++ b/crates/wasm-encoder/src/component/instances.rs @@ -59,7 +59,7 @@ impl InstanceSection { } /// Define an instance by instantiating a core module. - pub fn instantiate<'a, A>(&mut self, module_index: u32, args: A) -> &mut Self + pub fn instantiate<'a, A>(&mut self, module_index: u32, args: A) -> u32 where A: IntoIterator, A::IntoIter: ExactSizeIterator, @@ -72,12 +72,14 @@ impl InstanceSection { name.encode(&mut self.bytes); arg.encode(&mut self.bytes); } + + let index = self.num_added; self.num_added += 1; - self + index } /// Define an instance by exporting core WebAssembly items. - pub fn export_items<'a, E>(&mut self, exports: E) -> &mut Self + pub fn export_items<'a, E>(&mut self, exports: E) -> u32 where E: IntoIterator, E::IntoIter: ExactSizeIterator, @@ -90,8 +92,10 @@ impl InstanceSection { kind.encode(&mut self.bytes); index.encode(&mut self.bytes); } + + let index = self.num_added; self.num_added += 1; - self + index } } @@ -146,7 +150,7 @@ impl ComponentInstanceSection { } /// Define an instance by instantiating a component. - pub fn instantiate<'a, A>(&mut self, component_index: u32, args: A) -> &mut Self + pub fn instantiate<'a, A>(&mut self, component_index: u32, args: A) -> u32 where A: IntoIterator, A::IntoIter: ExactSizeIterator, @@ -160,12 +164,13 @@ impl ComponentInstanceSection { kind.encode(&mut self.bytes); index.encode(&mut self.bytes); } + let index = self.num_added; self.num_added += 1; - self + index } /// Define an instance by exporting items. - pub fn export_items<'a, E>(&mut self, exports: E) -> &mut Self + pub fn export_items<'a, E>(&mut self, exports: E) -> u32 where E: IntoIterator, E::IntoIter: ExactSizeIterator, @@ -178,8 +183,10 @@ impl ComponentInstanceSection { kind.encode(&mut self.bytes); index.encode(&mut self.bytes); } + + let index = self.num_added; self.num_added += 1; - self + index } } diff --git a/crates/wasm-encoder/src/component/types.rs b/crates/wasm-encoder/src/component/types.rs index 9407940b32..37a4f3e4fd 100644 --- a/crates/wasm-encoder/src/component/types.rs +++ b/crates/wasm-encoder/src/component/types.rs @@ -19,13 +19,15 @@ impl ModuleType { } /// Defines an import in this module type. - pub fn import(&mut self, module: &str, name: &str, ty: EntityType) -> &mut Self { + pub fn import(&mut self, module: &str, name: &str, ty: EntityType) -> u32 { self.bytes.push(0x00); module.encode(&mut self.bytes); name.encode(&mut self.bytes); ty.encode(&mut self.bytes); + + let index = self.num_added; self.num_added += 1; - self + index } /// Define a type in this module type. @@ -34,30 +36,38 @@ impl ModuleType { #[must_use = "the encoder must be used to encode the type"] pub fn ty(&mut self) -> CoreTypeEncoder { self.bytes.push(0x01); + let index = self.num_added; self.num_added += 1; self.types_added += 1; - CoreTypeEncoder(&mut self.bytes) + CoreTypeEncoder { + index, + bytes: &mut self.bytes, + } } /// Defines an outer core type alias in this module type. - pub fn alias_outer_core_type(&mut self, count: u32, index: u32) -> &mut Self { + pub fn alias_outer_core_type(&mut self, count: u32, index: u32) -> u32 { self.bytes.push(0x02); self.bytes.push(CORE_TYPE_SORT); self.bytes.push(0x01); // outer count.encode(&mut self.bytes); index.encode(&mut self.bytes); + + let index = self.num_added; self.num_added += 1; self.types_added += 1; - self + index } /// Defines an export in this module type. - pub fn export(&mut self, name: &str, ty: EntityType) -> &mut Self { + pub fn export(&mut self, name: &str, ty: EntityType) -> u32 { self.bytes.push(0x03); name.encode(&mut self.bytes); ty.encode(&mut self.bytes); + + let index = self.num_added; self.num_added += 1; - self + index } /// Gets the number of types that have been added to this module type. @@ -76,11 +86,14 @@ impl Encode for ModuleType { /// Used to encode core types. #[derive(Debug)] -pub struct CoreTypeEncoder<'a>(pub(crate) &'a mut Vec); +pub struct CoreTypeEncoder<'a> { + index: u32, + bytes: &'a mut Vec +} impl<'a> CoreTypeEncoder<'a> { /// Define a function type. - pub fn function(self, params: P, results: R) + pub fn function(self, params: P, results: R) -> u32 where P: IntoIterator, P::IntoIter: ExactSizeIterator, @@ -90,16 +103,19 @@ impl<'a> CoreTypeEncoder<'a> { let params = params.into_iter(); let results = results.into_iter(); - self.0.push(0x60); - params.len().encode(self.0); - self.0.extend(params.map(u8::from)); - results.len().encode(self.0); - self.0.extend(results.map(u8::from)); + self.bytes.push(0x60); + params.len().encode(self.bytes); + self.bytes.extend(params.map(u8::from)); + results.len().encode(self.bytes); + self.bytes.extend(results.map(u8::from)); + + self.index } /// Define a module type. - pub fn module(self, ty: &ModuleType) { - ty.encode(self.0); + pub fn module(self, ty: &ModuleType) -> u32 { + ty.encode(self.bytes); + self.index } } @@ -146,28 +162,30 @@ impl CoreTypeSection { /// The returned encoder must be finished before adding another type. #[must_use = "the encoder must be used to encode the type"] pub fn ty(&mut self) -> CoreTypeEncoder<'_> { + let index = self.num_added; self.num_added += 1; - CoreTypeEncoder(&mut self.bytes) + CoreTypeEncoder { + index, + bytes: &mut self.bytes + } } /// 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, R: IntoIterator, R::IntoIter: ExactSizeIterator, { - self.ty().function(params, results); - self + self.ty().function(params, results) } /// Define a module type in this type section. /// /// Currently this is only used for core type sections in components. - pub fn module(&mut self, ty: &ModuleType) -> &mut Self { - self.ty().module(ty); - self + pub fn module(&mut self, ty: &ModuleType) -> u32 { + self.ty().module(ty) } } @@ -204,9 +222,13 @@ impl ComponentType { #[must_use = "the encoder must be used to encode the type"] pub fn core_type(&mut self) -> CoreTypeEncoder { self.bytes.push(0x00); + let index = self.num_added; self.num_added += 1; self.core_types_added += 1; - CoreTypeEncoder(&mut self.bytes) + CoreTypeEncoder { + index, + bytes: &mut self.bytes + } } /// Define a type in this component type. @@ -215,16 +237,18 @@ impl ComponentType { #[must_use = "the encoder must be used to encode the type"] pub fn ty(&mut self) -> ComponentTypeEncoder { self.bytes.push(0x01); + let index = self.num_added; self.num_added += 1; self.types_added += 1; - ComponentTypeEncoder(&mut self.bytes) + ComponentTypeEncoder::new(index, &mut self.bytes) } /// Defines an alias for an exported item of a prior instance or an /// outer type. - pub fn alias(&mut self, alias: Alias<'_>) -> &mut Self { + pub fn alias(&mut self, alias: Alias<'_>) -> u32 { self.bytes.push(0x02); alias.encode(&mut self.bytes); + let index = self.num_added; self.num_added += 1; match &alias { Alias::InstanceExport { @@ -241,35 +265,37 @@ impl ComponentType { } => self.core_types_added += 1, _ => {} } - self + index } /// Defines an import in this component type. - pub fn import(&mut self, name: &str, url: &str, ty: ComponentTypeRef) -> &mut Self { + pub fn import(&mut self, name: &str, url: &str, ty: ComponentTypeRef) -> u32 { self.bytes.push(0x03); name.encode(&mut self.bytes); url.encode(&mut self.bytes); ty.encode(&mut self.bytes); + let index = self.num_added; self.num_added += 1; match ty { ComponentTypeRef::Type(..) => self.types_added += 1, _ => {} } - self + index } /// Defines an export in this component type. - pub fn export(&mut self, name: &str, url: &str, ty: ComponentTypeRef) -> &mut Self { + pub fn export(&mut self, name: &str, url: &str, ty: ComponentTypeRef) -> u32 { self.bytes.push(0x04); name.encode(&mut self.bytes); url.encode(&mut self.bytes); ty.encode(&mut self.bytes); + let index = self.num_added; self.num_added += 1; match ty { ComponentTypeRef::Type(..) => self.types_added += 1, _ => {} } - self + index } /// Gets the number of core types that have been added to this component type. @@ -318,15 +344,13 @@ impl InstanceType { } /// Defines an outer core type alias in this component type. - pub fn alias(&mut self, alias: Alias<'_>) -> &mut Self { - self.0.alias(alias); - self + pub fn alias(&mut self, alias: Alias<'_>) -> u32 { + self.0.alias(alias) } /// Defines an export in this instance type. - pub fn export(&mut self, name: &str, url: &str, ty: ComponentTypeRef) -> &mut Self { - self.0.export(name, url, ty); - self + pub fn export(&mut self, name: &str, url: &str, ty: ComponentTypeRef) -> u32 { + self.0.export(name, url, ty) } /// Gets the number of core types that have been added to this instance type. @@ -360,12 +384,15 @@ impl Encode for InstanceType { /// Used to encode component function types. #[derive(Debug)] -pub struct ComponentFuncTypeEncoder<'a>(&'a mut Vec); +pub struct ComponentFuncTypeEncoder<'a> { + index: u32, + bytes: &'a mut Vec +} impl<'a> ComponentFuncTypeEncoder<'a> { - fn new(sink: &'a mut Vec) -> Self { + fn new(index: u32, sink: &'a mut Vec) -> Self { sink.push(0x40); - Self(sink) + Self { index, bytes: sink } } /// Defines named parameters. @@ -378,10 +405,10 @@ impl<'a> ComponentFuncTypeEncoder<'a> { T: Into, { let params = params.into_iter(); - params.len().encode(self.0); + params.len().encode(self.bytes); for (name, ty) in params { - name.encode(self.0); - ty.into().encode(self.0); + name.encode(self.bytes); + ty.into().encode(self.bytes); } self } @@ -390,8 +417,8 @@ impl<'a> ComponentFuncTypeEncoder<'a> { /// /// This method cannot be used with `results`. pub fn result(&mut self, ty: impl Into) -> &mut Self { - self.0.push(0x00); - ty.into().encode(self.0); + self.bytes.push(0x00); + ty.into().encode(self.bytes); self } @@ -404,35 +431,51 @@ impl<'a> ComponentFuncTypeEncoder<'a> { R::IntoIter: ExactSizeIterator, T: Into, { - self.0.push(0x01); + self.bytes.push(0x01); let results = results.into_iter(); - results.len().encode(self.0); + results.len().encode(self.bytes); for (name, ty) in results { - name.encode(self.0); - ty.into().encode(self.0); + name.encode(self.bytes); + ty.into().encode(self.bytes); } self } + + /// Finalizes the encoder returning its index + pub fn finish(self) -> u32 { + self.index + } } /// Used to encode component and instance types. #[derive(Debug)] -pub struct ComponentTypeEncoder<'a>(&'a mut Vec); +pub struct ComponentTypeEncoder<'a> { + index: u32, + bytes: &'a mut Vec +} impl<'a> ComponentTypeEncoder<'a> { + fn new(index: u32, sink: &'a mut Vec) -> Self { + sink.push(0x40); + Self { index, bytes: sink } + } + /// Define a component type. - pub fn component(self, ty: &ComponentType) { - ty.encode(self.0); + pub fn component(self, ty: &ComponentType) -> u32 { + ty.encode(self.bytes); + self.index } /// Define an instance type. - pub fn instance(self, ty: &InstanceType) { - ty.encode(self.0); + pub fn instance(self, ty: &InstanceType) -> u32 { + ty.encode(self.bytes); + self.index } /// Define a function type. + #[must_use = "the encoder must be used to encode the function type"] pub fn function(self) -> ComponentFuncTypeEncoder<'a> { - ComponentFuncTypeEncoder::new(self.0) + ComponentFuncTypeEncoder::new(self.index, self.bytes) } /// Define a defined component type. @@ -440,7 +483,7 @@ impl<'a> ComponentTypeEncoder<'a> { /// The returned encoder must be used before adding another type. #[must_use = "the encoder must be used to encode the type"] pub fn defined_type(self) -> ComponentDefinedTypeEncoder<'a> { - ComponentDefinedTypeEncoder(self.0) + ComponentDefinedTypeEncoder::new(self.index, self.bytes) } } @@ -523,50 +566,60 @@ impl From for ComponentValType { /// Used for encoding component defined types. #[derive(Debug)] -pub struct ComponentDefinedTypeEncoder<'a>(&'a mut Vec); +pub struct ComponentDefinedTypeEncoder<'a> { + index: u32, + bytes: &'a mut Vec +} + +impl<'a> ComponentDefinedTypeEncoder<'a> { + fn new(index: u32, sink: &'a mut Vec) -> Self { + sink.push(0x40); + Self { index, bytes: sink } + } -impl ComponentDefinedTypeEncoder<'_> { /// Define a primitive value type. - pub fn primitive(self, ty: PrimitiveValType) { - ty.encode(self.0); + pub fn primitive(self, ty: PrimitiveValType) -> u32 { + ty.encode(self.bytes); + self.index } /// Define a record type. - pub fn record<'a, F, T>(self, fields: F) + pub fn record<'b, F, T>(self, fields: F) -> u32 where - F: IntoIterator, + F: IntoIterator, F::IntoIter: ExactSizeIterator, T: Into, { let fields = fields.into_iter(); - self.0.push(0x72); - fields.len().encode(self.0); + self.bytes.push(0x72); + fields.len().encode(self.bytes); for (name, ty) in fields { - name.encode(self.0); - ty.into().encode(self.0); + name.encode(self.bytes); + ty.into().encode(self.bytes); } + self.index } /// Define a variant type. - pub fn variant<'a, C>(self, cases: C) + pub fn variant<'b, C>(self, cases: C) where - C: IntoIterator, Option)>, + C: IntoIterator, Option)>, C::IntoIter: ExactSizeIterator, { let cases = cases.into_iter(); - self.0.push(0x71); - cases.len().encode(self.0); + self.bytes.push(0x71); + cases.len().encode(self.bytes); for (name, ty, refines) in cases { - name.encode(self.0); - ty.encode(self.0); - refines.encode(self.0); + name.encode(self.bytes); + ty.encode(self.bytes); + refines.encode(self.bytes); } } /// Define a list type. pub fn list(self, ty: impl Into) { - self.0.push(0x70); - ty.into().encode(self.0); + self.bytes.push(0x70); + ty.into().encode(self.bytes); } /// Define a tuple type. @@ -577,38 +630,38 @@ impl ComponentDefinedTypeEncoder<'_> { T: Into, { let types = types.into_iter(); - self.0.push(0x6F); - types.len().encode(self.0); + self.bytes.push(0x6F); + types.len().encode(self.bytes); for ty in types { - ty.into().encode(self.0); + ty.into().encode(self.bytes); } } /// Define a flags type. - pub fn flags<'a, I>(self, names: I) + pub fn flags<'b, I>(self, names: I) where - I: IntoIterator, + I: IntoIterator, I::IntoIter: ExactSizeIterator, { let names = names.into_iter(); - self.0.push(0x6E); - names.len().encode(self.0); + self.bytes.push(0x6E); + names.len().encode(self.bytes); for name in names { - name.encode(self.0); + name.encode(self.bytes); } } /// Define an enum type. - pub fn enum_type<'a, I>(self, tags: I) + pub fn enum_type<'b, I>(self, tags: I) where - I: IntoIterator, + I: IntoIterator, I::IntoIter: ExactSizeIterator, { let tags = tags.into_iter(); - self.0.push(0x6D); - tags.len().encode(self.0); + self.bytes.push(0x6D); + tags.len().encode(self.bytes); for tag in tags { - tag.encode(self.0); + tag.encode(self.bytes); } } @@ -620,24 +673,24 @@ impl ComponentDefinedTypeEncoder<'_> { T: Into, { let types = types.into_iter(); - self.0.push(0x6C); - types.len().encode(self.0); + self.bytes.push(0x6C); + types.len().encode(self.bytes); for ty in types { - ty.into().encode(self.0); + ty.into().encode(self.bytes); } } /// Define an option type. pub fn option(self, ty: impl Into) { - self.0.push(0x6B); - ty.into().encode(self.0); + self.bytes.push(0x6B); + ty.into().encode(self.bytes); } /// Define a result type. pub fn result(self, ok: Option, err: Option) { - self.0.push(0x6A); - ok.encode(self.0); - err.encode(self.0); + self.bytes.push(0x6A); + ok.encode(self.bytes); + err.encode(self.bytes); } } @@ -693,20 +746,22 @@ impl ComponentTypeSection { /// The returned encoder must be finished before adding another type. #[must_use = "the encoder must be used to encode the type"] pub fn ty(&mut self) -> ComponentTypeEncoder<'_> { + let index = self.num_added; self.num_added += 1; - ComponentTypeEncoder(&mut self.bytes) + ComponentTypeEncoder { + index, + bytes: &mut self.bytes + } } /// Define a component type in this type section. - pub fn component(&mut self, ty: &ComponentType) -> &mut Self { - self.ty().component(ty); - self + pub fn component(&mut self, ty: &ComponentType) -> u32 { + self.ty().component(ty) } /// Define an instance type in this type section. - pub fn instance(&mut self, ty: &InstanceType) -> &mut Self { - self.ty().instance(ty); - self + pub fn instance(&mut self, ty: &InstanceType) -> u32 { + self.ty().instance(ty) } /// Define a function type in this type section. diff --git a/crates/wasm-encoder/src/core/code.rs b/crates/wasm-encoder/src/core/code.rs index 8fd62c0f72..c9c59476c4 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![]; @@ -66,10 +65,11 @@ impl CodeSection { } /// Write a function body into this code section. - pub fn function(&mut self, func: &Function) -> &mut Self { + pub fn function(&mut self, func: &Function) -> u32 { func.encode(&mut self.bytes); + let index = self.num_added; self.num_added += 1; - self + index } /// Add a raw byte slice into this code section as a function body. @@ -96,10 +96,11 @@ impl CodeSection { /// let mut encoder = wasm_encoder::CodeSection::new(); /// encoder.raw(&code_section[body_range.start..body_range.end]); /// ``` - pub fn raw(&mut self, data: &[u8]) -> &mut Self { + pub fn raw(&mut self, data: &[u8]) -> u32 { data.encode(&mut self.bytes); + let index = self.num_added; self.num_added += 1; - self + index } } diff --git a/crates/wasm-encoder/src/core/data.rs b/crates/wasm-encoder/src/core/data.rs index 6439b66e2e..f6df57d9f1 100644 --- a/crates/wasm-encoder/src/core/data.rs +++ b/crates/wasm-encoder/src/core/data.rs @@ -13,7 +13,7 @@ use crate::{encode_section, encoding_size, ConstExpr, Encode, Section, SectionId /// }; /// /// let mut memory = MemorySection::new(); -/// memory.memory(MemoryType { +/// let memory_index = memory.memory(MemoryType { /// minimum: 1, /// maximum: None, /// memory64: false, @@ -21,7 +21,6 @@ use crate::{encode_section, encoding_size, ConstExpr, Encode, Section, SectionId /// }); /// /// let mut data = DataSection::new(); -/// let memory_index = 0; /// let offset = ConstExpr::i32_const(42); /// let segment_data = b"hello"; /// data.active(memory_index, &offset, segment_data.iter().copied()); @@ -81,7 +80,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 +110,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 +133,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 +145,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..2cda1c48f4 100644 --- a/crates/wasm-encoder/src/core/elements.rs +++ b/crates/wasm-encoder/src/core/elements.rs @@ -13,14 +13,13 @@ use crate::{encode_section, ConstExpr, Encode, Section, SectionId, ValType}; /// }; /// /// let mut tables = TableSection::new(); -/// tables.table(TableType { +/// let table_index = tables.table(TableType { /// element_type: ValType::FuncRef, /// minimum: 128, /// maximum: None, /// }); /// /// let mut elements = ElementSection::new(); -/// let table_index = 0; /// let offset = ConstExpr::i32_const(42); /// let element_type = ValType::FuncRef; /// let functions = Elements::Functions(&[ @@ -102,7 +101,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 +157,9 @@ impl ElementSection { } } + let index = self.num_added; self.num_added += 1; - self + index } /// Define an active element segment. @@ -173,7 +173,7 @@ impl ElementSection { offset: &ConstExpr, element_type: ValType, elements: Elements<'_>, - ) -> &mut Self { + ) -> u32 { self.segment(ElementSegment { mode: ElementMode::Active { table: table_index, @@ -187,7 +187,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 +198,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 +207,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/exports.rs b/crates/wasm-encoder/src/core/exports.rs index 36c7896362..fe79c677c8 100644 --- a/crates/wasm-encoder/src/core/exports.rs +++ b/crates/wasm-encoder/src/core/exports.rs @@ -63,12 +63,13 @@ impl ExportSection { } /// Define an export in the export section. - pub fn export(&mut self, name: &str, kind: ExportKind, index: u32) -> &mut Self { + pub fn export(&mut self, name: &str, kind: ExportKind, index: u32) -> u32 { name.encode(&mut self.bytes); kind.encode(&mut self.bytes); index.encode(&mut self.bytes); + let index = self.num_added; self.num_added += 1; - self + index } } diff --git a/crates/wasm-encoder/src/core/functions.rs b/crates/wasm-encoder/src/core/functions.rs index e21d8c10a7..cefe76b06b 100644 --- a/crates/wasm-encoder/src/core/functions.rs +++ b/crates/wasm-encoder/src/core/functions.rs @@ -43,10 +43,11 @@ impl FunctionSection { } /// Define a function in a module's function section. - pub fn function(&mut self, type_index: u32) -> &mut Self { + pub fn function(&mut self, type_index: u32) -> u32 { type_index.encode(&mut self.bytes); + let index = self.num_added; self.num_added += 1; - self + index } } diff --git a/crates/wasm-encoder/src/core/globals.rs b/crates/wasm-encoder/src/core/globals.rs index 16f23c2891..f5f36a6ebf 100644 --- a/crates/wasm-encoder/src/core/globals.rs +++ b/crates/wasm-encoder/src/core/globals.rs @@ -46,18 +46,20 @@ impl GlobalSection { } /// Define a global. - pub fn global(&mut self, global_type: GlobalType, init_expr: &ConstExpr) -> &mut Self { + pub fn global(&mut self, global_type: GlobalType, init_expr: &ConstExpr) -> u32 { global_type.encode(&mut self.bytes); init_expr.encode(&mut self.bytes); + let index = self.num_added; self.num_added += 1; - self + index } /// Add a raw byte slice into this code section as a global. - pub fn raw(&mut self, data: &[u8]) -> &mut Self { + pub fn raw(&mut self, data: &[u8]) -> u32 { self.bytes.extend(data); + let index = self.num_added; self.num_added += 1; - self + index } } diff --git a/crates/wasm-encoder/src/core/imports.rs b/crates/wasm-encoder/src/core/imports.rs index 455170f227..1f06d6959e 100644 --- a/crates/wasm-encoder/src/core/imports.rs +++ b/crates/wasm-encoder/src/core/imports.rs @@ -120,12 +120,13 @@ impl ImportSection { } /// Define an import in the import section. - pub fn import(&mut self, module: &str, field: &str, ty: impl Into) -> &mut Self { + pub fn import(&mut self, module: &str, field: &str, ty: impl Into) -> u32 { module.encode(&mut self.bytes); field.encode(&mut self.bytes); ty.into().encode(&mut self.bytes); + let index = self.num_added; self.num_added += 1; - self + index } } diff --git a/crates/wasm-encoder/src/core/linking.rs b/crates/wasm-encoder/src/core/linking.rs index 3309277be3..76fee9be7a 100644 --- a/crates/wasm-encoder/src/core/linking.rs +++ b/crates/wasm-encoder/src/core/linking.rs @@ -124,30 +124,32 @@ impl SymbolTable { /// /// The `name` must be omitted if `index` references an imported table and /// the `WASM_SYM_EXPLICIT_NAME` flag is not set. - pub fn function(&mut self, flags: u32, index: u32, name: Option<&str>) -> &mut Self { + pub fn function(&mut self, flags: u32, index: u32, name: Option<&str>) -> u32 { SYMTAB_FUNCTION.encode(&mut self.bytes); flags.encode(&mut self.bytes); index.encode(&mut self.bytes); if let Some(name) = name { name.encode(&mut self.bytes); } + let index = self.num_added; self.num_added += 1; - self + index } /// Define a global symbol in this symbol table. /// /// The `name` must be omitted if `index` references an imported table and /// the `WASM_SYM_EXPLICIT_NAME` flag is not set. - pub fn global(&mut self, flags: u32, index: u32, name: Option<&str>) -> &mut Self { + pub fn global(&mut self, flags: u32, index: u32, name: Option<&str>) -> u32 { SYMTAB_GLOBAL.encode(&mut self.bytes); flags.encode(&mut self.bytes); index.encode(&mut self.bytes); if let Some(name) = name { name.encode(&mut self.bytes); } + let index = self.num_added; self.num_added += 1; - self + index } // TODO: tags @@ -156,15 +158,16 @@ impl SymbolTable { /// /// The `name` must be omitted if `index` references an imported table and /// the `WASM_SYM_EXPLICIT_NAME` flag is not set. - pub fn table(&mut self, flags: u32, index: u32, name: Option<&str>) -> &mut Self { + pub fn table(&mut self, flags: u32, index: u32, name: Option<&str>) -> u32 { SYMTAB_TABLE.encode(&mut self.bytes); flags.encode(&mut self.bytes); index.encode(&mut self.bytes); if let Some(name) = name { name.encode(&mut self.bytes); } + let index = self.num_added; self.num_added += 1; - self + index } /// Add a data symbol to this symbol table. @@ -173,7 +176,7 @@ impl SymbolTable { flags: u32, name: &str, definition: Option, - ) -> &mut Self { + ) -> u32 { SYMTAB_DATA.encode(&mut self.bytes); flags.encode(&mut self.bytes); name.encode(&mut self.bytes); @@ -182,8 +185,9 @@ impl SymbolTable { def.offset.encode(&mut self.bytes); def.size.encode(&mut self.bytes); } + let index = self.num_added; self.num_added += 1; - self + index } // TODO: sections diff --git a/crates/wasm-encoder/src/core/memories.rs b/crates/wasm-encoder/src/core/memories.rs index c651768bdc..449a5799c6 100644 --- a/crates/wasm-encoder/src/core/memories.rs +++ b/crates/wasm-encoder/src/core/memories.rs @@ -45,10 +45,11 @@ impl MemorySection { } /// Define a memory. - pub fn memory(&mut self, memory_type: MemoryType) -> &mut Self { + pub fn memory(&mut self, memory_type: MemoryType) -> u32 { memory_type.encode(&mut self.bytes); + let index = self.num_added; self.num_added += 1; - self + index } } diff --git a/crates/wasm-encoder/src/core/tables.rs b/crates/wasm-encoder/src/core/tables.rs index 57a7628a5d..d184a45a7d 100644 --- a/crates/wasm-encoder/src/core/tables.rs +++ b/crates/wasm-encoder/src/core/tables.rs @@ -44,10 +44,11 @@ impl TableSection { } /// Define a table. - pub fn table(&mut self, table_type: TableType) -> &mut Self { + pub fn table(&mut self, table_type: TableType) -> u32 { table_type.encode(&mut self.bytes); + let index = self.num_added; self.num_added += 1; - self + index } } diff --git a/crates/wasm-encoder/src/core/tags.rs b/crates/wasm-encoder/src/core/tags.rs index 413055f2af..efea5c4ced 100644 --- a/crates/wasm-encoder/src/core/tags.rs +++ b/crates/wasm-encoder/src/core/tags.rs @@ -41,10 +41,11 @@ impl TagSection { } /// Define a tag. - pub fn tag(&mut self, tag_type: TagType) -> &mut Self { + pub fn tag(&mut self, tag_type: TagType) -> u32 { tag_type.encode(&mut self.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); //! From 430ee002dd9a4355c38495205fbc5d49b6e75cce Mon Sep 17 00:00:00 2001 From: Kyle Brown Date: Fri, 6 Jan 2023 16:33:33 -0500 Subject: [PATCH 2/7] Update README --- crates/wasm-encoder/README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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); From 9b405c3ba80edfe33a2313ee65c7ac52b93932a5 Mon Sep 17 00:00:00 2001 From: Kyle Brown Date: Fri, 6 Jan 2023 17:26:16 -0500 Subject: [PATCH 3/7] wasm-encoder: Cargo fmt, improve consistency in types.rs, match arms --- crates/wasm-encoder/src/component/exports.rs | 8 +--- crates/wasm-encoder/src/component/types.rs | 48 ++++++++++++-------- crates/wasm-smith/src/component/encode.rs | 4 +- crates/wast/src/component/binary.rs | 18 +++++--- 4 files changed, 45 insertions(+), 33 deletions(-) diff --git a/crates/wasm-encoder/src/component/exports.rs b/crates/wasm-encoder/src/component/exports.rs index da4c9522a1..2b408fd671 100644 --- a/crates/wasm-encoder/src/component/exports.rs +++ b/crates/wasm-encoder/src/component/exports.rs @@ -86,13 +86,7 @@ impl ComponentExportSection { } /// Define an export in the export section. - pub fn export( - &mut self, - name: &str, - url: &str, - kind: ComponentExportKind, - index: u32, - ) -> u32 { + pub fn export(&mut self, name: &str, url: &str, kind: ComponentExportKind, index: u32) -> u32 { name.encode(&mut self.bytes); url.encode(&mut self.bytes); kind.encode(&mut self.bytes); diff --git a/crates/wasm-encoder/src/component/types.rs b/crates/wasm-encoder/src/component/types.rs index 37a4f3e4fd..547df02599 100644 --- a/crates/wasm-encoder/src/component/types.rs +++ b/crates/wasm-encoder/src/component/types.rs @@ -88,10 +88,15 @@ impl Encode for ModuleType { #[derive(Debug)] pub struct CoreTypeEncoder<'a> { index: u32, - bytes: &'a mut Vec + bytes: &'a mut Vec, } impl<'a> CoreTypeEncoder<'a> { + fn new(index: u32, sink: &'a mut Vec) -> Self { + sink.push(0x40); + Self { index, bytes: sink } + } + /// Define a function type. pub fn function(self, params: P, results: R) -> u32 where @@ -164,10 +169,7 @@ impl CoreTypeSection { pub fn ty(&mut self) -> CoreTypeEncoder<'_> { let index = self.num_added; self.num_added += 1; - CoreTypeEncoder { - index, - bytes: &mut self.bytes - } + CoreTypeEncoder::new(index, &mut self.bytes) } /// Define a function type in this type section. @@ -225,9 +227,9 @@ impl ComponentType { let index = self.num_added; self.num_added += 1; self.core_types_added += 1; - CoreTypeEncoder { + CoreTypeEncoder { index, - bytes: &mut self.bytes + bytes: &mut self.bytes, } } @@ -386,7 +388,7 @@ impl Encode for InstanceType { #[derive(Debug)] pub struct ComponentFuncTypeEncoder<'a> { index: u32, - bytes: &'a mut Vec + bytes: &'a mut Vec, } impl<'a> ComponentFuncTypeEncoder<'a> { @@ -451,7 +453,7 @@ impl<'a> ComponentFuncTypeEncoder<'a> { #[derive(Debug)] pub struct ComponentTypeEncoder<'a> { index: u32, - bytes: &'a mut Vec + bytes: &'a mut Vec, } impl<'a> ComponentTypeEncoder<'a> { @@ -568,7 +570,7 @@ impl From for ComponentValType { #[derive(Debug)] pub struct ComponentDefinedTypeEncoder<'a> { index: u32, - bytes: &'a mut Vec + bytes: &'a mut Vec, } impl<'a> ComponentDefinedTypeEncoder<'a> { @@ -601,7 +603,7 @@ impl<'a> ComponentDefinedTypeEncoder<'a> { } /// Define a variant type. - pub fn variant<'b, C>(self, cases: C) + pub fn variant<'b, C>(self, cases: C) -> u32 where C: IntoIterator, Option)>, C::IntoIter: ExactSizeIterator, @@ -614,16 +616,18 @@ impl<'a> ComponentDefinedTypeEncoder<'a> { ty.encode(self.bytes); refines.encode(self.bytes); } + self.index } /// Define a list type. - pub fn list(self, ty: impl Into) { + pub fn list(self, ty: impl Into) -> u32 { self.bytes.push(0x70); ty.into().encode(self.bytes); + self.index } /// Define a tuple type. - pub fn tuple(self, types: I) + pub fn tuple(self, types: I) -> u32 where I: IntoIterator, I::IntoIter: ExactSizeIterator, @@ -635,10 +639,11 @@ impl<'a> ComponentDefinedTypeEncoder<'a> { for ty in types { ty.into().encode(self.bytes); } + self.index } /// Define a flags type. - pub fn flags<'b, I>(self, names: I) + pub fn flags<'b, I>(self, names: I) -> u32 where I: IntoIterator, I::IntoIter: ExactSizeIterator, @@ -649,10 +654,11 @@ impl<'a> ComponentDefinedTypeEncoder<'a> { for name in names { name.encode(self.bytes); } + self.index } /// Define an enum type. - pub fn enum_type<'b, I>(self, tags: I) + pub fn enum_type<'b, I>(self, tags: I) -> u32 where I: IntoIterator, I::IntoIter: ExactSizeIterator, @@ -663,10 +669,11 @@ impl<'a> ComponentDefinedTypeEncoder<'a> { for tag in tags { tag.encode(self.bytes); } + self.index } /// Define a union type. - pub fn union(self, types: I) + pub fn union(self, types: I) -> u32 where I: IntoIterator, I::IntoIter: ExactSizeIterator, @@ -678,19 +685,22 @@ impl<'a> ComponentDefinedTypeEncoder<'a> { for ty in types { ty.into().encode(self.bytes); } + self.index } /// Define an option type. - pub fn option(self, ty: impl Into) { + pub fn option(self, ty: impl Into) -> u32 { self.bytes.push(0x6B); ty.into().encode(self.bytes); + self.index } /// Define a result type. - pub fn result(self, ok: Option, err: Option) { + pub fn result(self, ok: Option, err: Option) -> u32 { self.bytes.push(0x6A); ok.encode(self.bytes); err.encode(self.bytes); + self.index } } @@ -750,7 +760,7 @@ impl ComponentTypeSection { self.num_added += 1; ComponentTypeEncoder { index, - bytes: &mut self.bytes + bytes: &mut self.bytes, } } diff --git a/crates/wasm-smith/src/component/encode.rs b/crates/wasm-smith/src/component/encode.rs index f2bfe734fc..e2e6471d54 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") } From bb1325a1634d8d8a8ad851683145309158e7784c Mon Sep 17 00:00:00 2001 From: Kyle Brown Date: Fri, 6 Jan 2023 18:01:33 -0500 Subject: [PATCH 4/7] wasm-encoder: cargo fmt and bug fix --- crates/wasm-encoder/src/component/types.rs | 3 --- crates/wasm-smith/src/component/encode.rs | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/crates/wasm-encoder/src/component/types.rs b/crates/wasm-encoder/src/component/types.rs index 547df02599..0c85a4e9c0 100644 --- a/crates/wasm-encoder/src/component/types.rs +++ b/crates/wasm-encoder/src/component/types.rs @@ -93,7 +93,6 @@ pub struct CoreTypeEncoder<'a> { impl<'a> CoreTypeEncoder<'a> { fn new(index: u32, sink: &'a mut Vec) -> Self { - sink.push(0x40); Self { index, bytes: sink } } @@ -458,7 +457,6 @@ pub struct ComponentTypeEncoder<'a> { impl<'a> ComponentTypeEncoder<'a> { fn new(index: u32, sink: &'a mut Vec) -> Self { - sink.push(0x40); Self { index, bytes: sink } } @@ -575,7 +573,6 @@ pub struct ComponentDefinedTypeEncoder<'a> { impl<'a> ComponentDefinedTypeEncoder<'a> { fn new(index: u32, sink: &'a mut Vec) -> Self { - sink.push(0x40); Self { index, bytes: sink } } diff --git a/crates/wasm-smith/src/component/encode.rs b/crates/wasm-smith/src/component/encode.rs index e2e6471d54..b816404ffd 100644 --- a/crates/wasm-smith/src/component/encode.rs +++ b/crates/wasm-smith/src/component/encode.rs @@ -225,7 +225,7 @@ impl DefinedType { match self { Self::Primitive(ty) => { enc.primitive(*ty); - }, + } Self::Record(ty) => { enc.record(ty.fields.iter().map(|(name, ty)| (name.as_str(), *ty))); } From 7383e68c2ba6ffd4e3be904d79cf6e5d23ed79e1 Mon Sep 17 00:00:00 2001 From: Kyle Brown Date: Fri, 6 Jan 2023 18:09:08 -0500 Subject: [PATCH 5/7] wasm-encoder: revert component indexing scheme --- crates/wasm-encoder/src/component/aliases.rs | 5 +- .../wasm-encoder/src/component/canonicals.rs | 12 +- crates/wasm-encoder/src/component/exports.rs | 12 +- crates/wasm-encoder/src/component/imports.rs | 9 +- .../wasm-encoder/src/component/instances.rs | 23 +- crates/wasm-encoder/src/component/types.rs | 278 +++++++----------- 6 files changed, 133 insertions(+), 206 deletions(-) diff --git a/crates/wasm-encoder/src/component/aliases.rs b/crates/wasm-encoder/src/component/aliases.rs index 9c98c958e1..1e317fb0e2 100644 --- a/crates/wasm-encoder/src/component/aliases.rs +++ b/crates/wasm-encoder/src/component/aliases.rs @@ -106,11 +106,10 @@ impl ComponentAliasSection { } /// Define an alias to a component instance's export. - pub fn alias(&mut self, alias: Alias<'_>) -> u32 { + pub fn alias(&mut self, alias: Alias<'_>) -> &mut Self { alias.encode(&mut self.bytes); - let index = self.num_added; self.num_added += 1; - index + self } } diff --git a/crates/wasm-encoder/src/component/canonicals.rs b/crates/wasm-encoder/src/component/canonicals.rs index c7425a4432..e81819c44e 100644 --- a/crates/wasm-encoder/src/component/canonicals.rs +++ b/crates/wasm-encoder/src/component/canonicals.rs @@ -83,7 +83,7 @@ impl CanonicalFunctionSection { } /// Define a function that will lift a core WebAssembly function to the canonical ABI. - pub fn lift(&mut self, core_func_index: u32, type_index: u32, options: O) -> u32 + pub fn lift(&mut self, core_func_index: u32, type_index: u32, options: O) -> &mut Self where O: IntoIterator, O::IntoIter: ExactSizeIterator, @@ -97,14 +97,12 @@ impl CanonicalFunctionSection { option.encode(&mut self.bytes); } type_index.encode(&mut self.bytes); - - let index = self.num_added; self.num_added += 1; - index + self } /// Define a function that will lower a canonical ABI function to a core WebAssembly function. - pub fn lower(&mut self, func_index: u32, options: O) -> u32 + pub fn lower(&mut self, func_index: u32, options: O) -> &mut Self where O: IntoIterator, O::IntoIter: ExactSizeIterator, @@ -117,10 +115,8 @@ impl CanonicalFunctionSection { for option in options { option.encode(&mut self.bytes); } - - let index = self.num_added; self.num_added += 1; - index + self } } diff --git a/crates/wasm-encoder/src/component/exports.rs b/crates/wasm-encoder/src/component/exports.rs index 2b408fd671..100a956c14 100644 --- a/crates/wasm-encoder/src/component/exports.rs +++ b/crates/wasm-encoder/src/component/exports.rs @@ -86,15 +86,19 @@ impl ComponentExportSection { } /// Define an export in the export section. - pub fn export(&mut self, name: &str, url: &str, kind: ComponentExportKind, index: u32) -> u32 { + pub fn export( + &mut self, + name: &str, + url: &str, + kind: ComponentExportKind, + index: u32, + ) -> &mut Self { name.encode(&mut self.bytes); url.encode(&mut self.bytes); kind.encode(&mut self.bytes); index.encode(&mut self.bytes); - - let index = self.num_added; self.num_added += 1; - index + self } } diff --git a/crates/wasm-encoder/src/component/imports.rs b/crates/wasm-encoder/src/component/imports.rs index 69733ab66c..8d843407f3 100644 --- a/crates/wasm-encoder/src/component/imports.rs +++ b/crates/wasm-encoder/src/component/imports.rs @@ -94,8 +94,7 @@ impl Encode for ComponentTypeRef { /// ("b", PrimitiveValType::String) /// ] /// ) -/// .result(PrimitiveValType::String) -/// .finish(); +/// .result(PrimitiveValType::String); /// /// // This imports a function named `f` with the type defined above /// let mut imports = ComponentImportSection::new(); @@ -130,14 +129,12 @@ impl ComponentImportSection { } /// Define an import in the component import section. - pub fn import(&mut self, name: &str, url: &str, ty: ComponentTypeRef) -> u32 { + pub fn import(&mut self, name: &str, url: &str, ty: ComponentTypeRef) -> &mut Self { name.encode(&mut self.bytes); url.encode(&mut self.bytes); ty.encode(&mut self.bytes); - - let index = self.num_added; self.num_added += 1; - index + self } } diff --git a/crates/wasm-encoder/src/component/instances.rs b/crates/wasm-encoder/src/component/instances.rs index c5ff6f63fb..000a872d6b 100644 --- a/crates/wasm-encoder/src/component/instances.rs +++ b/crates/wasm-encoder/src/component/instances.rs @@ -59,7 +59,7 @@ impl InstanceSection { } /// Define an instance by instantiating a core module. - pub fn instantiate<'a, A>(&mut self, module_index: u32, args: A) -> u32 + pub fn instantiate<'a, A>(&mut self, module_index: u32, args: A) -> &mut Self where A: IntoIterator, A::IntoIter: ExactSizeIterator, @@ -72,14 +72,12 @@ impl InstanceSection { name.encode(&mut self.bytes); arg.encode(&mut self.bytes); } - - let index = self.num_added; self.num_added += 1; - index + self } /// Define an instance by exporting core WebAssembly items. - pub fn export_items<'a, E>(&mut self, exports: E) -> u32 + pub fn export_items<'a, E>(&mut self, exports: E) -> &mut Self where E: IntoIterator, E::IntoIter: ExactSizeIterator, @@ -92,10 +90,8 @@ impl InstanceSection { kind.encode(&mut self.bytes); index.encode(&mut self.bytes); } - - let index = self.num_added; self.num_added += 1; - index + self } } @@ -150,7 +146,7 @@ impl ComponentInstanceSection { } /// Define an instance by instantiating a component. - pub fn instantiate<'a, A>(&mut self, component_index: u32, args: A) -> u32 + pub fn instantiate<'a, A>(&mut self, component_index: u32, args: A) -> &mut Self where A: IntoIterator, A::IntoIter: ExactSizeIterator, @@ -164,13 +160,12 @@ impl ComponentInstanceSection { kind.encode(&mut self.bytes); index.encode(&mut self.bytes); } - let index = self.num_added; self.num_added += 1; - index + self } /// Define an instance by exporting items. - pub fn export_items<'a, E>(&mut self, exports: E) -> u32 + pub fn export_items<'a, E>(&mut self, exports: E) -> &mut Self where E: IntoIterator, E::IntoIter: ExactSizeIterator, @@ -183,10 +178,8 @@ impl ComponentInstanceSection { kind.encode(&mut self.bytes); index.encode(&mut self.bytes); } - - let index = self.num_added; self.num_added += 1; - index + self } } diff --git a/crates/wasm-encoder/src/component/types.rs b/crates/wasm-encoder/src/component/types.rs index 0c85a4e9c0..9407940b32 100644 --- a/crates/wasm-encoder/src/component/types.rs +++ b/crates/wasm-encoder/src/component/types.rs @@ -19,15 +19,13 @@ impl ModuleType { } /// Defines an import in this module type. - pub fn import(&mut self, module: &str, name: &str, ty: EntityType) -> u32 { + pub fn import(&mut self, module: &str, name: &str, ty: EntityType) -> &mut Self { self.bytes.push(0x00); module.encode(&mut self.bytes); name.encode(&mut self.bytes); ty.encode(&mut self.bytes); - - let index = self.num_added; self.num_added += 1; - index + self } /// Define a type in this module type. @@ -36,38 +34,30 @@ impl ModuleType { #[must_use = "the encoder must be used to encode the type"] pub fn ty(&mut self) -> CoreTypeEncoder { self.bytes.push(0x01); - let index = self.num_added; self.num_added += 1; self.types_added += 1; - CoreTypeEncoder { - index, - bytes: &mut self.bytes, - } + CoreTypeEncoder(&mut self.bytes) } /// Defines an outer core type alias in this module type. - pub fn alias_outer_core_type(&mut self, count: u32, index: u32) -> u32 { + pub fn alias_outer_core_type(&mut self, count: u32, index: u32) -> &mut Self { self.bytes.push(0x02); self.bytes.push(CORE_TYPE_SORT); self.bytes.push(0x01); // outer count.encode(&mut self.bytes); index.encode(&mut self.bytes); - - let index = self.num_added; self.num_added += 1; self.types_added += 1; - index + self } /// Defines an export in this module type. - pub fn export(&mut self, name: &str, ty: EntityType) -> u32 { + pub fn export(&mut self, name: &str, ty: EntityType) -> &mut Self { self.bytes.push(0x03); name.encode(&mut self.bytes); ty.encode(&mut self.bytes); - - let index = self.num_added; self.num_added += 1; - index + self } /// Gets the number of types that have been added to this module type. @@ -86,18 +76,11 @@ impl Encode for ModuleType { /// Used to encode core types. #[derive(Debug)] -pub struct CoreTypeEncoder<'a> { - index: u32, - bytes: &'a mut Vec, -} +pub struct CoreTypeEncoder<'a>(pub(crate) &'a mut Vec); impl<'a> CoreTypeEncoder<'a> { - fn new(index: u32, sink: &'a mut Vec) -> Self { - Self { index, bytes: sink } - } - /// Define a function type. - pub fn function(self, params: P, results: R) -> u32 + pub fn function(self, params: P, results: R) where P: IntoIterator, P::IntoIter: ExactSizeIterator, @@ -107,19 +90,16 @@ impl<'a> CoreTypeEncoder<'a> { let params = params.into_iter(); let results = results.into_iter(); - self.bytes.push(0x60); - params.len().encode(self.bytes); - self.bytes.extend(params.map(u8::from)); - results.len().encode(self.bytes); - self.bytes.extend(results.map(u8::from)); - - self.index + self.0.push(0x60); + params.len().encode(self.0); + self.0.extend(params.map(u8::from)); + results.len().encode(self.0); + self.0.extend(results.map(u8::from)); } /// Define a module type. - pub fn module(self, ty: &ModuleType) -> u32 { - ty.encode(self.bytes); - self.index + pub fn module(self, ty: &ModuleType) { + ty.encode(self.0); } } @@ -166,27 +146,28 @@ impl CoreTypeSection { /// The returned encoder must be finished before adding another type. #[must_use = "the encoder must be used to encode the type"] pub fn ty(&mut self) -> CoreTypeEncoder<'_> { - let index = self.num_added; self.num_added += 1; - CoreTypeEncoder::new(index, &mut self.bytes) + CoreTypeEncoder(&mut self.bytes) } /// Define a function type in this type section. - pub fn function(&mut self, params: P, results: R) -> u32 + pub fn function(&mut self, params: P, results: R) -> &mut Self where P: IntoIterator, P::IntoIter: ExactSizeIterator, R: IntoIterator, R::IntoIter: ExactSizeIterator, { - self.ty().function(params, results) + self.ty().function(params, results); + self } /// Define a module type in this type section. /// /// Currently this is only used for core type sections in components. - pub fn module(&mut self, ty: &ModuleType) -> u32 { - self.ty().module(ty) + pub fn module(&mut self, ty: &ModuleType) -> &mut Self { + self.ty().module(ty); + self } } @@ -223,13 +204,9 @@ impl ComponentType { #[must_use = "the encoder must be used to encode the type"] pub fn core_type(&mut self) -> CoreTypeEncoder { self.bytes.push(0x00); - let index = self.num_added; self.num_added += 1; self.core_types_added += 1; - CoreTypeEncoder { - index, - bytes: &mut self.bytes, - } + CoreTypeEncoder(&mut self.bytes) } /// Define a type in this component type. @@ -238,18 +215,16 @@ impl ComponentType { #[must_use = "the encoder must be used to encode the type"] pub fn ty(&mut self) -> ComponentTypeEncoder { self.bytes.push(0x01); - let index = self.num_added; self.num_added += 1; self.types_added += 1; - ComponentTypeEncoder::new(index, &mut self.bytes) + ComponentTypeEncoder(&mut self.bytes) } /// Defines an alias for an exported item of a prior instance or an /// outer type. - pub fn alias(&mut self, alias: Alias<'_>) -> u32 { + pub fn alias(&mut self, alias: Alias<'_>) -> &mut Self { self.bytes.push(0x02); alias.encode(&mut self.bytes); - let index = self.num_added; self.num_added += 1; match &alias { Alias::InstanceExport { @@ -266,37 +241,35 @@ impl ComponentType { } => self.core_types_added += 1, _ => {} } - index + self } /// Defines an import in this component type. - pub fn import(&mut self, name: &str, url: &str, ty: ComponentTypeRef) -> u32 { + pub fn import(&mut self, name: &str, url: &str, ty: ComponentTypeRef) -> &mut Self { self.bytes.push(0x03); name.encode(&mut self.bytes); url.encode(&mut self.bytes); ty.encode(&mut self.bytes); - let index = self.num_added; self.num_added += 1; match ty { ComponentTypeRef::Type(..) => self.types_added += 1, _ => {} } - index + self } /// Defines an export in this component type. - pub fn export(&mut self, name: &str, url: &str, ty: ComponentTypeRef) -> u32 { + pub fn export(&mut self, name: &str, url: &str, ty: ComponentTypeRef) -> &mut Self { self.bytes.push(0x04); name.encode(&mut self.bytes); url.encode(&mut self.bytes); ty.encode(&mut self.bytes); - let index = self.num_added; self.num_added += 1; match ty { ComponentTypeRef::Type(..) => self.types_added += 1, _ => {} } - index + self } /// Gets the number of core types that have been added to this component type. @@ -345,13 +318,15 @@ impl InstanceType { } /// Defines an outer core type alias in this component type. - pub fn alias(&mut self, alias: Alias<'_>) -> u32 { - self.0.alias(alias) + pub fn alias(&mut self, alias: Alias<'_>) -> &mut Self { + self.0.alias(alias); + self } /// Defines an export in this instance type. - pub fn export(&mut self, name: &str, url: &str, ty: ComponentTypeRef) -> u32 { - self.0.export(name, url, ty) + pub fn export(&mut self, name: &str, url: &str, ty: ComponentTypeRef) -> &mut Self { + self.0.export(name, url, ty); + self } /// Gets the number of core types that have been added to this instance type. @@ -385,15 +360,12 @@ impl Encode for InstanceType { /// Used to encode component function types. #[derive(Debug)] -pub struct ComponentFuncTypeEncoder<'a> { - index: u32, - bytes: &'a mut Vec, -} +pub struct ComponentFuncTypeEncoder<'a>(&'a mut Vec); impl<'a> ComponentFuncTypeEncoder<'a> { - fn new(index: u32, sink: &'a mut Vec) -> Self { + fn new(sink: &'a mut Vec) -> Self { sink.push(0x40); - Self { index, bytes: sink } + Self(sink) } /// Defines named parameters. @@ -406,10 +378,10 @@ impl<'a> ComponentFuncTypeEncoder<'a> { T: Into, { let params = params.into_iter(); - params.len().encode(self.bytes); + params.len().encode(self.0); for (name, ty) in params { - name.encode(self.bytes); - ty.into().encode(self.bytes); + name.encode(self.0); + ty.into().encode(self.0); } self } @@ -418,8 +390,8 @@ impl<'a> ComponentFuncTypeEncoder<'a> { /// /// This method cannot be used with `results`. pub fn result(&mut self, ty: impl Into) -> &mut Self { - self.bytes.push(0x00); - ty.into().encode(self.bytes); + self.0.push(0x00); + ty.into().encode(self.0); self } @@ -432,50 +404,35 @@ impl<'a> ComponentFuncTypeEncoder<'a> { R::IntoIter: ExactSizeIterator, T: Into, { - self.bytes.push(0x01); + self.0.push(0x01); let results = results.into_iter(); - results.len().encode(self.bytes); + results.len().encode(self.0); for (name, ty) in results { - name.encode(self.bytes); - ty.into().encode(self.bytes); + name.encode(self.0); + ty.into().encode(self.0); } self } - - /// Finalizes the encoder returning its index - pub fn finish(self) -> u32 { - self.index - } } /// Used to encode component and instance types. #[derive(Debug)] -pub struct ComponentTypeEncoder<'a> { - index: u32, - bytes: &'a mut Vec, -} +pub struct ComponentTypeEncoder<'a>(&'a mut Vec); impl<'a> ComponentTypeEncoder<'a> { - fn new(index: u32, sink: &'a mut Vec) -> Self { - Self { index, bytes: sink } - } - /// Define a component type. - pub fn component(self, ty: &ComponentType) -> u32 { - ty.encode(self.bytes); - self.index + pub fn component(self, ty: &ComponentType) { + ty.encode(self.0); } /// Define an instance type. - pub fn instance(self, ty: &InstanceType) -> u32 { - ty.encode(self.bytes); - self.index + pub fn instance(self, ty: &InstanceType) { + ty.encode(self.0); } /// Define a function type. - #[must_use = "the encoder must be used to encode the function type"] pub fn function(self) -> ComponentFuncTypeEncoder<'a> { - ComponentFuncTypeEncoder::new(self.index, self.bytes) + ComponentFuncTypeEncoder::new(self.0) } /// Define a defined component type. @@ -483,7 +440,7 @@ impl<'a> ComponentTypeEncoder<'a> { /// The returned encoder must be used before adding another type. #[must_use = "the encoder must be used to encode the type"] pub fn defined_type(self) -> ComponentDefinedTypeEncoder<'a> { - ComponentDefinedTypeEncoder::new(self.index, self.bytes) + ComponentDefinedTypeEncoder(self.0) } } @@ -566,138 +523,121 @@ impl From for ComponentValType { /// Used for encoding component defined types. #[derive(Debug)] -pub struct ComponentDefinedTypeEncoder<'a> { - index: u32, - bytes: &'a mut Vec, -} - -impl<'a> ComponentDefinedTypeEncoder<'a> { - fn new(index: u32, sink: &'a mut Vec) -> Self { - Self { index, bytes: sink } - } +pub struct ComponentDefinedTypeEncoder<'a>(&'a mut Vec); +impl ComponentDefinedTypeEncoder<'_> { /// Define a primitive value type. - pub fn primitive(self, ty: PrimitiveValType) -> u32 { - ty.encode(self.bytes); - self.index + pub fn primitive(self, ty: PrimitiveValType) { + ty.encode(self.0); } /// Define a record type. - pub fn record<'b, F, T>(self, fields: F) -> u32 + pub fn record<'a, F, T>(self, fields: F) where - F: IntoIterator, + F: IntoIterator, F::IntoIter: ExactSizeIterator, T: Into, { let fields = fields.into_iter(); - self.bytes.push(0x72); - fields.len().encode(self.bytes); + self.0.push(0x72); + fields.len().encode(self.0); for (name, ty) in fields { - name.encode(self.bytes); - ty.into().encode(self.bytes); + name.encode(self.0); + ty.into().encode(self.0); } - self.index } /// Define a variant type. - pub fn variant<'b, C>(self, cases: C) -> u32 + pub fn variant<'a, C>(self, cases: C) where - C: IntoIterator, Option)>, + C: IntoIterator, Option)>, C::IntoIter: ExactSizeIterator, { let cases = cases.into_iter(); - self.bytes.push(0x71); - cases.len().encode(self.bytes); + self.0.push(0x71); + cases.len().encode(self.0); for (name, ty, refines) in cases { - name.encode(self.bytes); - ty.encode(self.bytes); - refines.encode(self.bytes); + name.encode(self.0); + ty.encode(self.0); + refines.encode(self.0); } - self.index } /// Define a list type. - pub fn list(self, ty: impl Into) -> u32 { - self.bytes.push(0x70); - ty.into().encode(self.bytes); - self.index + pub fn list(self, ty: impl Into) { + self.0.push(0x70); + ty.into().encode(self.0); } /// Define a tuple type. - pub fn tuple(self, types: I) -> u32 + pub fn tuple(self, types: I) where I: IntoIterator, I::IntoIter: ExactSizeIterator, T: Into, { let types = types.into_iter(); - self.bytes.push(0x6F); - types.len().encode(self.bytes); + self.0.push(0x6F); + types.len().encode(self.0); for ty in types { - ty.into().encode(self.bytes); + ty.into().encode(self.0); } - self.index } /// Define a flags type. - pub fn flags<'b, I>(self, names: I) -> u32 + pub fn flags<'a, I>(self, names: I) where - I: IntoIterator, + I: IntoIterator, I::IntoIter: ExactSizeIterator, { let names = names.into_iter(); - self.bytes.push(0x6E); - names.len().encode(self.bytes); + self.0.push(0x6E); + names.len().encode(self.0); for name in names { - name.encode(self.bytes); + name.encode(self.0); } - self.index } /// Define an enum type. - pub fn enum_type<'b, I>(self, tags: I) -> u32 + pub fn enum_type<'a, I>(self, tags: I) where - I: IntoIterator, + I: IntoIterator, I::IntoIter: ExactSizeIterator, { let tags = tags.into_iter(); - self.bytes.push(0x6D); - tags.len().encode(self.bytes); + self.0.push(0x6D); + tags.len().encode(self.0); for tag in tags { - tag.encode(self.bytes); + tag.encode(self.0); } - self.index } /// Define a union type. - pub fn union(self, types: I) -> u32 + pub fn union(self, types: I) where I: IntoIterator, I::IntoIter: ExactSizeIterator, T: Into, { let types = types.into_iter(); - self.bytes.push(0x6C); - types.len().encode(self.bytes); + self.0.push(0x6C); + types.len().encode(self.0); for ty in types { - ty.into().encode(self.bytes); + ty.into().encode(self.0); } - self.index } /// Define an option type. - pub fn option(self, ty: impl Into) -> u32 { - self.bytes.push(0x6B); - ty.into().encode(self.bytes); - self.index + pub fn option(self, ty: impl Into) { + self.0.push(0x6B); + ty.into().encode(self.0); } /// Define a result type. - pub fn result(self, ok: Option, err: Option) -> u32 { - self.bytes.push(0x6A); - ok.encode(self.bytes); - err.encode(self.bytes); - self.index + pub fn result(self, ok: Option, err: Option) { + self.0.push(0x6A); + ok.encode(self.0); + err.encode(self.0); } } @@ -753,22 +693,20 @@ impl ComponentTypeSection { /// The returned encoder must be finished before adding another type. #[must_use = "the encoder must be used to encode the type"] pub fn ty(&mut self) -> ComponentTypeEncoder<'_> { - let index = self.num_added; self.num_added += 1; - ComponentTypeEncoder { - index, - bytes: &mut self.bytes, - } + ComponentTypeEncoder(&mut self.bytes) } /// Define a component type in this type section. - pub fn component(&mut self, ty: &ComponentType) -> u32 { - self.ty().component(ty) + pub fn component(&mut self, ty: &ComponentType) -> &mut Self { + self.ty().component(ty); + self } /// Define an instance type in this type section. - pub fn instance(&mut self, ty: &InstanceType) -> u32 { - self.ty().instance(ty) + pub fn instance(&mut self, ty: &InstanceType) -> &mut Self { + self.ty().instance(ty); + self } /// Define a function type in this type section. From f1c6449b2a15b1dae08669942d34f1e4576c827c Mon Sep 17 00:00:00 2001 From: Kyle Brown Date: Fri, 6 Jan 2023 18:34:17 -0500 Subject: [PATCH 6/7] wasm-encoder: remove unsound indexing utilities --- crates/wasm-encoder/src/core/data.rs | 3 ++- crates/wasm-encoder/src/core/elements.rs | 3 ++- crates/wasm-encoder/src/core/exports.rs | 5 ++--- crates/wasm-encoder/src/core/functions.rs | 5 ++--- crates/wasm-encoder/src/core/globals.rs | 10 ++++------ crates/wasm-encoder/src/core/imports.rs | 5 ++--- crates/wasm-encoder/src/core/linking.rs | 20 ++++++++------------ crates/wasm-encoder/src/core/memories.rs | 5 ++--- crates/wasm-encoder/src/core/tables.rs | 5 ++--- crates/wasm-encoder/src/core/tags.rs | 5 ++--- 10 files changed, 28 insertions(+), 38 deletions(-) diff --git a/crates/wasm-encoder/src/core/data.rs b/crates/wasm-encoder/src/core/data.rs index f6df57d9f1..c47074677c 100644 --- a/crates/wasm-encoder/src/core/data.rs +++ b/crates/wasm-encoder/src/core/data.rs @@ -13,7 +13,7 @@ use crate::{encode_section, encoding_size, ConstExpr, Encode, Section, SectionId /// }; /// /// let mut memory = MemorySection::new(); -/// let memory_index = memory.memory(MemoryType { +/// memory.memory(MemoryType { /// minimum: 1, /// maximum: None, /// memory64: false, @@ -21,6 +21,7 @@ use crate::{encode_section, encoding_size, ConstExpr, Encode, Section, SectionId /// }); /// /// let mut data = DataSection::new(); +/// let memory_index = 0; /// let offset = ConstExpr::i32_const(42); /// let segment_data = b"hello"; /// data.active(memory_index, &offset, segment_data.iter().copied()); diff --git a/crates/wasm-encoder/src/core/elements.rs b/crates/wasm-encoder/src/core/elements.rs index 2cda1c48f4..b67bd42b2c 100644 --- a/crates/wasm-encoder/src/core/elements.rs +++ b/crates/wasm-encoder/src/core/elements.rs @@ -13,13 +13,14 @@ use crate::{encode_section, ConstExpr, Encode, Section, SectionId, ValType}; /// }; /// /// let mut tables = TableSection::new(); -/// let table_index = tables.table(TableType { +/// tables.table(TableType { /// element_type: ValType::FuncRef, /// minimum: 128, /// maximum: None, /// }); /// /// let mut elements = ElementSection::new(); +/// let table_index = 0; /// let offset = ConstExpr::i32_const(42); /// let element_type = ValType::FuncRef; /// let functions = Elements::Functions(&[ diff --git a/crates/wasm-encoder/src/core/exports.rs b/crates/wasm-encoder/src/core/exports.rs index fe79c677c8..36c7896362 100644 --- a/crates/wasm-encoder/src/core/exports.rs +++ b/crates/wasm-encoder/src/core/exports.rs @@ -63,13 +63,12 @@ impl ExportSection { } /// Define an export in the export section. - pub fn export(&mut self, name: &str, kind: ExportKind, index: u32) -> u32 { + pub fn export(&mut self, name: &str, kind: ExportKind, index: u32) -> &mut Self { name.encode(&mut self.bytes); kind.encode(&mut self.bytes); index.encode(&mut self.bytes); - let index = self.num_added; self.num_added += 1; - index + self } } diff --git a/crates/wasm-encoder/src/core/functions.rs b/crates/wasm-encoder/src/core/functions.rs index cefe76b06b..e21d8c10a7 100644 --- a/crates/wasm-encoder/src/core/functions.rs +++ b/crates/wasm-encoder/src/core/functions.rs @@ -43,11 +43,10 @@ impl FunctionSection { } /// Define a function in a module's function section. - pub fn function(&mut self, type_index: u32) -> u32 { + pub fn function(&mut self, type_index: u32) -> &mut Self { type_index.encode(&mut self.bytes); - let index = self.num_added; self.num_added += 1; - index + self } } diff --git a/crates/wasm-encoder/src/core/globals.rs b/crates/wasm-encoder/src/core/globals.rs index f5f36a6ebf..16f23c2891 100644 --- a/crates/wasm-encoder/src/core/globals.rs +++ b/crates/wasm-encoder/src/core/globals.rs @@ -46,20 +46,18 @@ impl GlobalSection { } /// Define a global. - pub fn global(&mut self, global_type: GlobalType, init_expr: &ConstExpr) -> u32 { + pub fn global(&mut self, global_type: GlobalType, init_expr: &ConstExpr) -> &mut Self { global_type.encode(&mut self.bytes); init_expr.encode(&mut self.bytes); - let index = self.num_added; self.num_added += 1; - index + self } /// Add a raw byte slice into this code section as a global. - pub fn raw(&mut self, data: &[u8]) -> u32 { + pub fn raw(&mut self, data: &[u8]) -> &mut Self { self.bytes.extend(data); - let index = self.num_added; self.num_added += 1; - index + self } } diff --git a/crates/wasm-encoder/src/core/imports.rs b/crates/wasm-encoder/src/core/imports.rs index 1f06d6959e..455170f227 100644 --- a/crates/wasm-encoder/src/core/imports.rs +++ b/crates/wasm-encoder/src/core/imports.rs @@ -120,13 +120,12 @@ impl ImportSection { } /// Define an import in the import section. - pub fn import(&mut self, module: &str, field: &str, ty: impl Into) -> u32 { + pub fn import(&mut self, module: &str, field: &str, ty: impl Into) -> &mut Self { module.encode(&mut self.bytes); field.encode(&mut self.bytes); ty.into().encode(&mut self.bytes); - let index = self.num_added; self.num_added += 1; - index + self } } diff --git a/crates/wasm-encoder/src/core/linking.rs b/crates/wasm-encoder/src/core/linking.rs index 76fee9be7a..3309277be3 100644 --- a/crates/wasm-encoder/src/core/linking.rs +++ b/crates/wasm-encoder/src/core/linking.rs @@ -124,32 +124,30 @@ impl SymbolTable { /// /// The `name` must be omitted if `index` references an imported table and /// the `WASM_SYM_EXPLICIT_NAME` flag is not set. - pub fn function(&mut self, flags: u32, index: u32, name: Option<&str>) -> u32 { + pub fn function(&mut self, flags: u32, index: u32, name: Option<&str>) -> &mut Self { SYMTAB_FUNCTION.encode(&mut self.bytes); flags.encode(&mut self.bytes); index.encode(&mut self.bytes); if let Some(name) = name { name.encode(&mut self.bytes); } - let index = self.num_added; self.num_added += 1; - index + self } /// Define a global symbol in this symbol table. /// /// The `name` must be omitted if `index` references an imported table and /// the `WASM_SYM_EXPLICIT_NAME` flag is not set. - pub fn global(&mut self, flags: u32, index: u32, name: Option<&str>) -> u32 { + pub fn global(&mut self, flags: u32, index: u32, name: Option<&str>) -> &mut Self { SYMTAB_GLOBAL.encode(&mut self.bytes); flags.encode(&mut self.bytes); index.encode(&mut self.bytes); if let Some(name) = name { name.encode(&mut self.bytes); } - let index = self.num_added; self.num_added += 1; - index + self } // TODO: tags @@ -158,16 +156,15 @@ impl SymbolTable { /// /// The `name` must be omitted if `index` references an imported table and /// the `WASM_SYM_EXPLICIT_NAME` flag is not set. - pub fn table(&mut self, flags: u32, index: u32, name: Option<&str>) -> u32 { + pub fn table(&mut self, flags: u32, index: u32, name: Option<&str>) -> &mut Self { SYMTAB_TABLE.encode(&mut self.bytes); flags.encode(&mut self.bytes); index.encode(&mut self.bytes); if let Some(name) = name { name.encode(&mut self.bytes); } - let index = self.num_added; self.num_added += 1; - index + self } /// Add a data symbol to this symbol table. @@ -176,7 +173,7 @@ impl SymbolTable { flags: u32, name: &str, definition: Option, - ) -> u32 { + ) -> &mut Self { SYMTAB_DATA.encode(&mut self.bytes); flags.encode(&mut self.bytes); name.encode(&mut self.bytes); @@ -185,9 +182,8 @@ impl SymbolTable { def.offset.encode(&mut self.bytes); def.size.encode(&mut self.bytes); } - let index = self.num_added; self.num_added += 1; - index + self } // TODO: sections diff --git a/crates/wasm-encoder/src/core/memories.rs b/crates/wasm-encoder/src/core/memories.rs index 449a5799c6..c651768bdc 100644 --- a/crates/wasm-encoder/src/core/memories.rs +++ b/crates/wasm-encoder/src/core/memories.rs @@ -45,11 +45,10 @@ impl MemorySection { } /// Define a memory. - pub fn memory(&mut self, memory_type: MemoryType) -> u32 { + pub fn memory(&mut self, memory_type: MemoryType) -> &mut Self { memory_type.encode(&mut self.bytes); - let index = self.num_added; self.num_added += 1; - index + self } } diff --git a/crates/wasm-encoder/src/core/tables.rs b/crates/wasm-encoder/src/core/tables.rs index d184a45a7d..57a7628a5d 100644 --- a/crates/wasm-encoder/src/core/tables.rs +++ b/crates/wasm-encoder/src/core/tables.rs @@ -44,11 +44,10 @@ impl TableSection { } /// Define a table. - pub fn table(&mut self, table_type: TableType) -> u32 { + pub fn table(&mut self, table_type: TableType) -> &mut Self { table_type.encode(&mut self.bytes); - let index = self.num_added; self.num_added += 1; - index + self } } diff --git a/crates/wasm-encoder/src/core/tags.rs b/crates/wasm-encoder/src/core/tags.rs index efea5c4ced..413055f2af 100644 --- a/crates/wasm-encoder/src/core/tags.rs +++ b/crates/wasm-encoder/src/core/tags.rs @@ -41,11 +41,10 @@ impl TagSection { } /// Define a tag. - pub fn tag(&mut self, tag_type: TagType) -> u32 { + pub fn tag(&mut self, tag_type: TagType) -> &mut Self { tag_type.encode(&mut self.bytes); - let index = self.num_added; self.num_added += 1; - index + self } } From fe0e95f3ce98026475ae4a6b8276cc3f51773115 Mon Sep 17 00:00:00 2001 From: Kyle Brown Date: Fri, 6 Jan 2023 18:37:08 -0500 Subject: [PATCH 7/7] wasm-encoder: revert non-doc code.rs changes --- crates/wasm-encoder/src/core/code.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/crates/wasm-encoder/src/core/code.rs b/crates/wasm-encoder/src/core/code.rs index c9c59476c4..1b051044ce 100644 --- a/crates/wasm-encoder/src/core/code.rs +++ b/crates/wasm-encoder/src/core/code.rs @@ -65,11 +65,10 @@ impl CodeSection { } /// Write a function body into this code section. - pub fn function(&mut self, func: &Function) -> u32 { + pub fn function(&mut self, func: &Function) -> &mut Self { func.encode(&mut self.bytes); - let index = self.num_added; self.num_added += 1; - index + self } /// Add a raw byte slice into this code section as a function body. @@ -96,11 +95,10 @@ impl CodeSection { /// let mut encoder = wasm_encoder::CodeSection::new(); /// encoder.raw(&code_section[body_range.start..body_range.end]); /// ``` - pub fn raw(&mut self, data: &[u8]) -> u32 { + pub fn raw(&mut self, data: &[u8]) -> &mut Self { data.encode(&mut self.bytes); - let index = self.num_added; self.num_added += 1; - index + self } }