Skip to content

Commit

Permalink
cmds: Implement info exports
Browse files Browse the repository at this point in the history
  • Loading branch information
benediktwerner committed Jul 26, 2019
1 parent 764acd4 commit 0de215e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
33 changes: 19 additions & 14 deletions src/cmds/info.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use wasmdbg::breakpoints::Breakpoint;
use wasmdbg::vm::Trap;
use wasmdbg::wasm::{External, InitExpr, PAGE_SIZE};
use wasmdbg::wasm::{External, InitExpr, Internal, PAGE_SIZE};
use wasmdbg::Debugger;

use super::{CmdArg, CmdResult, Command, Commands};
Expand Down Expand Up @@ -282,23 +282,28 @@ fn cmd_info_memory(dbg: &mut Debugger, _args: &[CmdArg]) -> CmdResult {

fn cmd_info_globals(dbg: &mut Debugger, _args: &[CmdArg]) -> CmdResult {
for (i, global) in dbg.get_file()?.module().globals().iter().enumerate() {
let const_str = if global.is_mutable() {
"mut "
} else {
"const"
};
let init_str = match global.init_expr() {
InitExpr::Const(val) => format!("{}", val),
InitExpr::Global(index) => format!("global {}", index),
};
println!(" {}: {} {:15} = {}", i, const_str, global.name(), init_str);
println!(" {}: {}", i, global);
}
Ok(())
}

fn cmd_info_exports(_dbg: &mut Debugger, _args: &[CmdArg]) -> CmdResult {
// TODO: Implement
println!("Not implemented");
fn cmd_info_exports(dbg: &mut Debugger, _args: &[CmdArg]) -> CmdResult {
let module = dbg.get_file()?.module();
println!("{} exports", module.exports().len());
for entry in module.exports() {
match entry.internal() {
Internal::Function(index) => println!(
"Function {}: {}",
index,
&module.functions()[*index as usize]
),
Internal::Table(index) => println!("Table {}", index),
Internal::Memory(index) => println!("Memory {}", index),
Internal::Global(index) => {
println!("Global {}: {}", index, &module.globals()[*index as usize])
}
}
}
Ok(())
}

Expand Down
11 changes: 11 additions & 0 deletions src/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,17 @@ impl Global {
}
}

impl fmt::Display for Global {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let const_str = if self.is_mutable() { "mut " } else { "const" };
let init_str = match self.init_expr() {
InitExpr::Const(val) => format!("{}", val),
InitExpr::Global(index) => format!("global {}", index),
};
write!(f, "{} {:15} = {}", const_str, self.name(), init_str)
}
}

pub struct ElementSegment {
index: u32,
offset: InitExpr,
Expand Down

0 comments on commit 0de215e

Please sign in to comment.