wasmer_cli/commands/
inspect.rs

1use 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)]
11/// The options for the `wasmer validate` subcommand
12pub struct Inspect {
13    /// File to validate as WebAssembly
14    #[clap(name = "FILE")]
15    path: PathBuf,
16
17    #[clap(flatten)]
18    rt: RuntimeOptions,
19}
20
21impl Inspect {
22    /// Runs logic for the `validate` subcommand
23    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 module_contents = std::fs::read(&self.path)?;
30        let engine = self
31            .rt
32            .get_engine_for_module(&module_contents, &Target::default())?;
33
34        let iswasm = is_wasm(&module_contents);
35        let module_len = module_contents.len();
36        let module = Module::new(&engine, module_contents)?;
37        println!(
38            "Backend used to parse the module: {}",
39            engine.deterministic_id()
40        );
41
42        println!("Type: {}", if !iswasm { "wat" } else { "wasm" });
43        println!("Size: {}", ByteSize(module_len as _));
44        println!("Imports:");
45        println!("  Functions:");
46        for f in module.imports().functions() {
47            println!("    \"{}\".\"{}\": {}", f.module(), f.name(), f.ty());
48        }
49        println!("  Memories:");
50        for f in module.imports().memories() {
51            println!("    \"{}\".\"{}\": {}", f.module(), f.name(), f.ty());
52        }
53        println!("  Tables:");
54        for f in module.imports().tables() {
55            println!("    \"{}\".\"{}\": {}", f.module(), f.name(), f.ty());
56        }
57        println!("  Globals:");
58        for f in module.imports().globals() {
59            println!("    \"{}\".\"{}\": {}", f.module(), f.name(), f.ty());
60        }
61        println!("Exports:");
62        println!("  Functions:");
63        for f in module.exports().functions() {
64            println!("    \"{}\": {}", f.name(), f.ty());
65        }
66        println!("  Memories:");
67        for f in module.exports().memories() {
68            println!("    \"{}\": {}", f.name(), f.ty());
69        }
70        println!("  Tables:");
71        for f in module.exports().tables() {
72            println!("    \"{}\": {}", f.name(), f.ty());
73        }
74        println!("  Globals:");
75        for f in module.exports().globals() {
76            println!("    \"{}\": {}", f.name(), f.ty());
77        }
78        Ok(())
79    }
80}