wasmer_cli/commands/
inspect.rs1use std::path::PathBuf;
2
3use crate::backend::RuntimeOptions;
4use anyhow::{Context, Result};
5use bytesize::ByteSize;
6use clap::Parser;
7use wasmer::*;
8use wasmer_types::target::Target;
9
10#[derive(Debug, Parser)]
11pub struct Inspect {
13 #[clap(name = "FILE")]
15 path: PathBuf,
16
17 #[clap(flatten)]
18 rt: RuntimeOptions,
19}
20
21impl Inspect {
22 pub fn execute(&self) -> Result<()> {
24 self.inner_execute()
25 .context(format!("failed to inspect `{}`", self.path.display()))
26 }
27
28 fn inner_execute(&self) -> Result<()> {
29 let mut module_contents = std::fs::read(&self.path)?;
30 let iswasm = is_wasm(&module_contents);
31
32 if !iswasm {
33 if self.path.extension().and_then(|e| e.to_str()) == Some("wat") {
34 module_contents = wat2wasm(&module_contents)
35 .map_err(|e| anyhow::anyhow!("Cannot convert WAT to WASM: {e}"))?
36 .to_vec();
37 } else {
38 anyhow::bail!("The input file is not a valid WebAssembly binary or WAT file");
39 }
40 }
41
42 let engine = self
43 .rt
44 .get_engine_for_module(&module_contents, &Target::default())?;
45
46 let module_len = module_contents.len();
47 let module = Module::new(&engine, module_contents)?;
48 println!(
49 "Backend used to parse the module: {}",
50 engine.deterministic_id()
51 );
52
53 println!("Type: {}", if !iswasm { "wat" } else { "wasm" });
54 println!("Size: {}", ByteSize(module_len as _));
55 println!("Imports:");
56 println!(" Functions:");
57 for f in module.imports().functions() {
58 println!(" \"{}\".\"{}\": {}", f.module(), f.name(), f.ty());
59 }
60 println!(" Memories:");
61 for f in module.imports().memories() {
62 println!(" \"{}\".\"{}\": {}", f.module(), f.name(), f.ty());
63 }
64 println!(" Tables:");
65 for f in module.imports().tables() {
66 println!(" \"{}\".\"{}\": {}", f.module(), f.name(), f.ty());
67 }
68 println!(" Globals:");
69 for f in module.imports().globals() {
70 println!(" \"{}\".\"{}\": {}", f.module(), f.name(), f.ty());
71 }
72 println!("Exports:");
73 println!(" Functions:");
74 for f in module.exports().functions() {
75 println!(" \"{}\": {}", f.name(), f.ty());
76 }
77 println!(" Memories:");
78 for f in module.exports().memories() {
79 println!(" \"{}\": {}", f.name(), f.ty());
80 }
81 println!(" Tables:");
82 for f in module.exports().tables() {
83 println!(" \"{}\": {}", f.name(), f.ty());
84 }
85 println!(" Globals:");
86 for f in module.exports().globals() {
87 println!(" \"{}\": {}", f.name(), f.ty());
88 }
89 Ok(())
90 }
91}