Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasm-encoder: Return Indices from builder functions where applicable #876

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions crates/wasm-encoder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 1 addition & 2 deletions crates/wasm-encoder/src/core/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![];
Expand Down
14 changes: 8 additions & 6 deletions crates/wasm-encoder/src/core/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl DataSection {
}

/// Define a data segment.
pub fn segment<D>(&mut self, segment: DataSegment<D>) -> &mut Self
pub fn segment<D>(&mut self, segment: DataSegment<D>) -> u32
where
D: IntoIterator<Item = u8>,
D::IntoIter: ExactSizeIterator,
Expand Down Expand Up @@ -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<D>(&mut self, memory_index: u32, offset: &ConstExpr, data: D) -> &mut Self
pub fn active<D>(&mut self, memory_index: u32, offset: &ConstExpr, data: D) -> u32
where
D: IntoIterator<Item = u8>,
D::IntoIter: ExactSizeIterator,
Expand All @@ -133,7 +134,7 @@ impl DataSection {
/// Define a passive data segment.
///
/// Passive data segments are part of the bulk memory proposal.
pub fn passive<D>(&mut self, data: D) -> &mut Self
pub fn passive<D>(&mut self, data: D) -> u32
where
D: IntoIterator<Item = u8>,
D::IntoIter: ExactSizeIterator,
Expand All @@ -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
}
}

Expand Down
16 changes: 9 additions & 7 deletions crates/wasm-encoder/src/core/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -158,8 +158,9 @@ impl ElementSection {
}
}

let index = self.num_added;
self.num_added += 1;
self
index
}

/// Define an active element segment.
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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
}
}

Expand Down
6 changes: 4 additions & 2 deletions crates/wasm-encoder/src/core/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl TypeSection {
}

/// Define a function type in this type section.
pub fn function<P, R>(&mut self, params: P, results: R) -> &mut Self
pub fn function<P, R>(&mut self, params: P, results: R) -> u32
where
P: IntoIterator<Item = ValType>,
P::IntoIter: ExactSizeIterator,
Expand All @@ -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
}
}

Expand Down
3 changes: 1 addition & 2 deletions crates/wasm-encoder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
//!
Expand Down
4 changes: 3 additions & 1 deletion crates/wasm-smith/src/component/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
Expand Down
18 changes: 12 additions & 6 deletions crates/wast/src/component/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
Expand All @@ -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());
}
Expand Down Expand Up @@ -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")
}
Expand Down