Skip to content

Commit bb982a4

Browse files
authored
Allows the user to insert new function into the module. (#68)
1 parent b2227ae commit bb982a4

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/loader/mod.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,30 @@ pub struct Module<'a> {
308308
}
309309

310310
impl<'a> Module<'a> {
311+
/// Appends a function type to the module, returning the new function index.
312+
pub fn append_function(&mut self, inputs: &[ValType], outputs: &[ValType]) -> u32 {
313+
// We either find an existing identical type, or we create a new one.
314+
let type_idx = self
315+
.types
316+
.iter()
317+
.position(|t| t.ty.params() == inputs && t.ty.results() == outputs)
318+
.unwrap_or_else(|| {
319+
// Create a new type.
320+
let type_idx = self.types.len();
321+
self.types.push(Rc::new(FuncType {
322+
unique_id: type_idx as u32,
323+
ty: wasmparser::FuncType::new(inputs.iter().cloned(), outputs.iter().cloned()),
324+
}));
325+
326+
type_idx
327+
}) as u32;
328+
329+
let func_idx = self.func_types.len() as u32;
330+
self.func_types.push(type_idx);
331+
332+
func_idx
333+
}
334+
311335
fn get_type(&self, type_idx: u32) -> &FuncType {
312336
&self.types[type_idx as usize]
313337
}

0 commit comments

Comments
 (0)