Skip to content

Commit 14ef098

Browse files
committed
Keep track of number of functions, args and ouputs
1 parent 795cdf1 commit 14ef098

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

packages/vm/src/parsed_wasm.rs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use wasmer::wasmparser::{
2-
Export, Import, MemoryType, Parser, TableType, ValidPayload, Validator, WasmFeatures,
2+
Export, Import, MemoryType, Parser, Payload, TableType, Type, ValidPayload, Validator,
3+
WasmFeatures,
34
};
45

56
use crate::VmResult;
@@ -13,6 +14,10 @@ pub struct ParsedWasm<'a> {
1314
pub imports: Vec<Import<'a>>,
1415
pub tables: Vec<TableType>,
1516
pub memories: Vec<MemoryType>,
17+
pub function_count: usize,
18+
pub type_count: u32,
19+
pub max_func_params: usize,
20+
pub max_func_results: usize,
1621
}
1722

1823
impl<'a> ParsedWasm<'a> {
@@ -34,6 +39,10 @@ impl<'a> ParsedWasm<'a> {
3439
imports: vec![],
3540
tables: vec![],
3641
memories: vec![],
42+
function_count: 0,
43+
type_count: 0,
44+
max_func_params: 0,
45+
max_func_results: 0,
3746
};
3847

3948
let mut fun_allocations = Default::default();
@@ -45,20 +54,36 @@ impl<'a> ParsedWasm<'a> {
4554
let mut fun_validator = fv.into_validator(fun_allocations);
4655
fun_validator.validate(&body)?;
4756
fun_allocations = fun_validator.into_allocations();
57+
58+
this.function_count += 1;
4859
}
4960

5061
match p {
51-
wasmer::wasmparser::Payload::Version { num, .. } => this.version = num,
52-
wasmer::wasmparser::Payload::ImportSection(i) => {
62+
Payload::TypeSection(t) => {
63+
this.type_count = t.get_count();
64+
for t_res in t {
65+
let ty: Type = t_res?;
66+
match ty {
67+
Type::Func(ft) => {
68+
this.max_func_params =
69+
core::cmp::max(ft.params().len(), this.max_func_params);
70+
this.max_func_results =
71+
core::cmp::max(ft.results().len(), this.max_func_results);
72+
}
73+
}
74+
}
75+
}
76+
Payload::Version { num, .. } => this.version = num,
77+
Payload::ImportSection(i) => {
5378
this.imports = i.into_iter().collect::<Result<Vec<_>, _>>()?;
5479
}
55-
wasmer::wasmparser::Payload::TableSection(t) => {
80+
Payload::TableSection(t) => {
5681
this.tables = t.into_iter().collect::<Result<Vec<_>, _>>()?;
5782
}
58-
wasmer::wasmparser::Payload::MemorySection(m) => {
83+
Payload::MemorySection(m) => {
5984
this.memories = m.into_iter().collect::<Result<Vec<_>, _>>()?;
6085
}
61-
wasmer::wasmparser::Payload::ExportSection(e) => {
86+
Payload::ExportSection(e) => {
6287
this.exports = e.into_iter().collect::<Result<Vec<_>, _>>()?;
6388
}
6489
_ => {} // ignore everything else

0 commit comments

Comments
 (0)