diff options
| author | Henry Gressmann <mail@henrygressmann.de> | 2024-01-23 13:56:31 +0100 |
|---|---|---|
| committer | Henry Gressmann <mail@henrygressmann.de> | 2024-01-23 13:56:31 +0100 |
| commit | 1617d7a24fc454130215336b986c0607bec374de (patch) | |
| tree | ef083f17c0dae8b9e8c50381783e736506e835cf /crates/parser/src/lib.rs | |
| parent | 5d7f3bca95afb465b12b699df031b3f6139982da (diff) | |
core: attach types to functions
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
Diffstat (limited to 'crates/parser/src/lib.rs')
| -rw-r--r-- | crates/parser/src/lib.rs | 35 |
1 files changed, 23 insertions, 12 deletions
diff --git a/crates/parser/src/lib.rs b/crates/parser/src/lib.rs index 0618683..ae6cc4d 100644 --- a/crates/parser/src/lib.rs +++ b/crates/parser/src/lib.rs @@ -20,7 +20,7 @@ mod log { mod conversion; mod error; mod module; -use alloc::vec::Vec; +use alloc::{string::ToString, vec::Vec}; pub use error::*; use module::ModuleReader; use tinywasm_types::WasmFunction; @@ -103,15 +103,26 @@ impl TryFrom<ModuleReader> for TinyWasmModule { return Err(ParseError::EndNotReached); } - let func_types = reader.func_addrs; + let code_type_addrs = reader.code_type_addrs; + let local_function_count = reader.code.len(); + + if code_type_addrs.len() != local_function_count { + return Err(ParseError::Other("Code and code type address count mismatch".to_string())); + } + let funcs = reader .code .into_iter() - .zip(func_types) - .map(|(f, ty)| WasmFunction { - instructions: f.body, - locals: f.locals, - ty_addr: ty, + .zip(code_type_addrs) + .map(|(f, ty_idx)| { + ( + ty_idx, + WasmFunction { + instructions: f.body, + locals: f.locals, + ty: reader.func_types.get(ty_idx as usize).unwrap().clone(), + }, + ) }) .collect::<Vec<_>>(); @@ -119,17 +130,17 @@ impl TryFrom<ModuleReader> for TinyWasmModule { let table_types = reader.table_types; Ok(TinyWasmModule { - version: reader.version, - start_func: reader.start_func, - func_types: reader.func_types.into_boxed_slice(), funcs: funcs.into_boxed_slice(), - exports: reader.exports.into_boxed_slice(), + func_types: reader.func_types.into_boxed_slice(), globals: globals.into_boxed_slice(), table_types: table_types.into_boxed_slice(), - memory_types: reader.memory_types.into_boxed_slice(), imports: reader.imports.into_boxed_slice(), + version: reader.version, + start_func: reader.start_func, data: reader.data.into_boxed_slice(), + exports: reader.exports.into_boxed_slice(), elements: reader.elements.into_boxed_slice(), + memory_types: reader.memory_types.into_boxed_slice(), }) } } |
