• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

henrythasler / rust-tiny-wasm / 23354992303

20 Mar 2026 05:35PM UTC coverage: 86.175% (-7.6%) from 93.74%
23354992303

push

github

web-flow
Merge pull request #3 from henrythasler/feature/wasmparser

Feature/wasmparser

90 of 136 new or added lines in 6 files covered. (66.18%)

2 existing lines in 1 file now uncovered.

374 of 434 relevant lines covered (86.18%)

6.41 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

18.18
/src/lib.rs
1
use std::fs;
2
use std::path::Path;
3

4
pub mod assembler;
5
pub mod compiler;
6
pub mod runtime;
7

8
pub type Result<T> = std::result::Result<T, TinyWasmError>;
9

10
#[derive(Debug)]
11
pub enum TinyWasmError {
12
    Io(std::io::Error),
13
    Parser(String),
14
    Compiler(String),
15
}
16

17
impl From<std::io::Error> for TinyWasmError {
NEW
18
    fn from(err: std::io::Error) -> Self {
×
NEW
19
        TinyWasmError::Io(err)
×
NEW
20
    }
×
21
}
22

23
impl From<wasmparser::BinaryReaderError> for TinyWasmError {
NEW
24
    fn from(err: wasmparser::BinaryReaderError) -> Self {
×
NEW
25
        TinyWasmError::Parser(err.message().to_string())
×
UNCOV
26
    }
×
27
}
28

29
// pub fn dump_module_info(filename: &Path) {
30
//     println!("Loading '{}'...", filename.display().bright_blue());
31
//     let wasm_module = loader::wasmparser(filename).unwrap();
32

33
//     println!(
34
//         "Found {} section(s)",
35
//         wasm_module.sections().len().bright_green()
36
//     );
37
//     for section in wasm_module.sections() {
38
//         match section {
39
//             loader::Section::Export(export_section) => {
40
//                 println!("  Section ID: {}", export_section.name().bright_blue());
41
//                 println!(
42
//                     "  Number of Exports: {}",
43
//                     export_section.exports.len().bright_green()
44
//                 );
45

46
//                 for export in &export_section.exports {
47
//                     println!(
48
//                         "    - {} ({:#?}, {})",
49
//                         export.name.bright_yellow(),
50
//                         export.r#type.white(),
51
//                         export.index.white()
52
//                     );
53
//                 }
54
//             }
55
//             loader::Section::Code(code_section) => {
56
//                 println!("  Section ID: {}", code_section.name().bright_blue());
57
//                 println!(
58
//                     "  Number of Entries: {}",
59
//                     code_section.entries.len().bright_green()
60
//                 );
61
//                 for entry in &code_section.entries {
62
//                     println!(
63
//                         "    - Locals: {} ({:?})",
64
//                         entry.get_locals().len().bright_green(),
65
//                         entry.locals
66
//                     );
67
//                     println!(
68
//                         "      Content ({:#?} Bytes): {:02X?}\n",
69
//                         entry.get_code().len(),
70
//                         entry.get_code().bright_yellow()
71
//                     )
72
//                 }
73
//             }
74
//             _ => {}
75
//         }
76
//     }
77
// }
78

NEW
79
pub fn print_module(_filename: &Path) -> Result<()> {
×
NEW
80
    Ok(())
×
NEW
81
}
×
82

83
/// Compiles a WebAssembly module from the given byte array, and then instantiates it using the runtime module.
84
///
85
/// This function takes a byte array representing a WebAssembly module, compiles it using
86
/// the `compiler` module, and then instantiates it using the `runtime` module.
87
/// It returns a `Result` containing the instantiated module or an error message if the compilation or instantiation fails.
88
///
89
/// # Arguments
90
/// * `module` - A byte slice containing the WebAssembly module to be compiled and instantiated
91
/// # Returns
92
/// * `Result<runtime::Runtime, String>` - The instantiated module or an error message if the compilation or instantiation fails
93
/// # Errors
94
/// This function will return an error if the module cannot be compiled or instantiated.
95
pub fn get_module_instance(module: &[u8]) -> Result<runtime::Runtime> {
1✔
96
    let linked_module = compiler::compile(module)?;
1✔
97
    Ok(runtime::instantiate_module(&linked_module))
1✔
98
}
1✔
99

100
/// This function load a WebAssembly module, compiles and executes the given function
101
///
102
/// Loads the WebAssembly module from the specified file, compiles it using the compiler
103
/// module, and then instantiates it using the runtime module. Finally, it retrieves the
104
/// specified function and executes it, returning the result as an i32.
105
///
106
/// # Arguments
107
/// * `filename` - Path to the WebAssembly module
108
/// * `function` - Name of the function to execute
109
/// # Returns
110
/// * `Result<i32, Box<dyn std::error::Error>>` - The i32-result of the function execution or an error
111
/// # Errors
112
/// This function will return an error if the module cannot be loaded, compiled, instantiated, or if the specified function cannot be found or executed.
113
/// # Example
114
/// ```
115
/// use std::path::Path;
116
/// use tiny_wasm::execute;
117
/// let result = execute(&Path::new("path/to/module.wasm"), "function_name").unwrap();
118
/// println!("Result: {}", result);
119
/// ```
NEW
120
pub fn execute(filename: &Path, function: &str) -> Result<i32> {
×
NEW
121
    let module = fs::read(filename)?;
×
NEW
122
    let instance = get_module_instance(&module)?;
×
123
    let _start = unsafe { instance.get_function::<fn() -> i32>(function) };
×
NEW
124
    Ok(_start())
×
125
}
×
126

127
/// This function is a convenience wrapper around the `execute` function to run the `_start` function
NEW
128
pub fn execute_start(filename: &Path) -> Result<i32> {
×
NEW
129
    execute(filename, "_start")
×
UNCOV
130
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc