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 2 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
5 changes: 3 additions & 2 deletions crates/wasm-encoder/src/component/aliases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
esoterra marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
12 changes: 8 additions & 4 deletions crates/wasm-encoder/src/component/canonicals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl CanonicalFunctionSection {
}

/// Define a function that will lift a core WebAssembly function to the canonical ABI.
pub fn lift<O>(&mut self, core_func_index: u32, type_index: u32, options: O) -> &mut Self
pub fn lift<O>(&mut self, core_func_index: u32, type_index: u32, options: O) -> u32
where
O: IntoIterator<Item = CanonicalOption>,
O::IntoIter: ExactSizeIterator,
Expand All @@ -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<O>(&mut self, func_index: u32, options: O) -> &mut Self
pub fn lower<O>(&mut self, func_index: u32, options: O) -> u32
where
O: IntoIterator<Item = CanonicalOption>,
O::IntoIter: ExactSizeIterator,
Expand All @@ -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
}
}

Expand Down
6 changes: 4 additions & 2 deletions crates/wasm-encoder/src/component/exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
9 changes: 6 additions & 3 deletions crates/wasm-encoder/src/component/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
}
}

Expand Down
23 changes: 15 additions & 8 deletions crates/wasm-encoder/src/component/instances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item = (&'a str, ModuleArg)>,
A::IntoIter: ExactSizeIterator,
Expand All @@ -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<Item = (&'a str, ExportKind, u32)>,
E::IntoIter: ExactSizeIterator,
Expand All @@ -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
}
}

Expand Down Expand Up @@ -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<Item = (&'a str, ComponentExportKind, u32)>,
A::IntoIter: ExactSizeIterator,
Expand All @@ -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<Item = (&'a str, ComponentExportKind, u32)>,
E::IntoIter: ExactSizeIterator,
Expand All @@ -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
}
}

Expand Down
Loading