diff --git a/src/parser.rs b/src/parser.rs index 1ac91eb..de9b082 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -741,6 +741,11 @@ generate! { function_name -> (FuncIdx, Name) = { let name = run!(name); (idx, name) }} +generate! { global_name -> (GlobalIdx, Name) = { + let idx = run!(globalidx); + let name = run!(name); + (idx, name) +}} generate! { names -> Names = { // Reference: https://github.com/WebAssembly/wabt/blob/713bece/src/binary-reader.cc#L1634 // This is a custom section, that appears to show up in WASI modules. @@ -749,19 +754,30 @@ generate! { names -> Names = { module: None, functions: HashMap::new(), locals: HashMap::new(), + globals: HashMap::new(), }; - let name_type = run!(u32); // module = 0, function = 1, local = 2 - if name_type != 1 { - // We don't support non-function names just yet. Might add it - // in the future. - unimplemented!() - } - let subsection_size = run!(u32); - if subsection_size > 0 { - let mut inp = inp![..subsection_size as usize]; - names.functions = run_manual!(vec(inp, function_name)).into_iter().collect(); - if inp.len() != 0 { - err!("Unused bytes in custom name section") + while peek_inp!(@@@length@@@) != 0 { + let name_type = run!(byte); + let subsection_size = run!(u32); + if subsection_size > 0 { + let mut inp = inp![..subsection_size as usize]; + match name_type { + 0 => { + names.module = Some(run_manual!(name(inp))); + }, + 1 => { + names.functions = run_manual!(vec(inp, function_name)).into_iter().collect(); + }, + 7 => { + names.globals = run_manual!(vec(inp, global_name)).into_iter().collect(); + }, + _ => { + err!("Unimplemented name type {}", name_type) + }, + }; + if inp.len() != 0 { + err!("Unused bytes in custom name section") + } } } names @@ -833,6 +849,7 @@ generate! { module -> Module = { module: None, functions: HashMap::new(), locals: HashMap::new(), + globals: HashMap::new(), } } }; diff --git a/src/wasm.rs b/src/wasm.rs index b03c314..86b85df 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -81,7 +81,7 @@ pub mod syntax { pub struct TableIdx(pub u32); #[derive(Copy, Clone, Debug)] pub struct MemIdx(pub u32); - #[derive(Copy, Clone, Debug)] + #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] pub struct GlobalIdx(pub u32); #[derive(Copy, Clone, Debug)] pub struct LocalIdx(pub u32); @@ -315,7 +315,8 @@ pub mod syntax { pub struct Names { pub module: Option, pub functions: std::collections::HashMap, - pub locals: std::collections::HashMap>, + pub locals: std::collections::HashMap>, + pub globals: std::collections::HashMap, } pub enum FuncInternals {