Skip to content

Commit

Permalink
fix: proivde localIds to match args to builder fn
Browse files Browse the repository at this point in the history
Signed-off-by: Victor Adossi <[email protected]>
  • Loading branch information
vados-cosmonic committed Oct 18, 2023
1 parent fcac3ea commit 4549c65
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/module/functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::parse::IndicesToIds;
use crate::tombstone_arena::{Id, Tombstone, TombstoneArena};
use crate::ty::TypeId;
use crate::ty::ValType;
use crate::{ExportItem, FunctionBuilder, InstrSeqBuilder, Memory, MemoryId};
use crate::{ExportItem, FunctionBuilder, InstrSeqBuilder, LocalId, Memory, MemoryId};

pub use self::local_function::LocalFunction;

Expand Down Expand Up @@ -466,7 +466,7 @@ impl Module {
pub fn replace_exported_func(
&mut self,
fid: FunctionId,
builder_fn: impl FnOnce(&mut InstrSeqBuilder),
builder_fn: impl FnOnce((&mut InstrSeqBuilder, &Vec<LocalId>)),
) -> Result<FunctionId> {
let original_export_id = self
.exports
Expand All @@ -486,7 +486,7 @@ impl Module {
// Add the function produced by `fn_builder` as a local function
let mut builder = FunctionBuilder::new(&mut self.types, &params, &results);
let mut new_fn_body = builder.func_body();
builder_fn(&mut new_fn_body);
builder_fn((&mut new_fn_body, &lf.args));
let func = builder.local_func(lf.args.clone());
let new_fn_id = self.funcs.add_local(func);

Expand Down Expand Up @@ -520,7 +520,7 @@ impl Module {
pub fn replace_imported_func(
&mut self,
fid: FunctionId,
builder_fn: impl FnOnce(&mut InstrSeqBuilder),
builder_fn: impl FnOnce((&mut InstrSeqBuilder, &Vec<LocalId>)),
) -> Result<FunctionId> {
let original_import_id = self
.imports
Expand All @@ -537,11 +537,17 @@ impl Module {
let ty = self.types.get(*tid);
let (params, results) = (ty.params().to_vec(), ty.results().to_vec());

// Build the list LocalIds used by args to match the original function
let args = params
.iter()
.map(|ty| self.locals.add(*ty))
.collect::<Vec<_>>();

// Build the new function
let mut builder = FunctionBuilder::new(&mut self.types, &params, &results);
let mut new_fn_body = builder.func_body();
builder_fn(&mut new_fn_body);
let new_func_kind = FunctionKind::Local(builder.local_func(vec![]));
builder_fn((&mut new_fn_body, &args));
let new_func_kind = FunctionKind::Local(builder.local_func(args));

// Mutate the existing function, changing it from a FunctionKind::ImportedFunction
// to the local function produced by running the provided `fn_builder`
Expand Down

0 comments on commit 4549c65

Please sign in to comment.