18 releases

Uses old Rust 2015

0.8.0 Jul 27, 2020
0.7.1 Mar 18, 2020
0.7.0 Sep 10, 2019
0.6.1 Jul 24, 2019
0.1.5 Feb 13, 2018

#1359 in WebAssembly

Download history 2372/week @ 2024-07-21 2260/week @ 2024-07-28 2390/week @ 2024-08-04 2806/week @ 2024-08-11 2608/week @ 2024-08-18 2832/week @ 2024-08-25 3040/week @ 2024-09-01 2711/week @ 2024-09-08 2157/week @ 2024-09-15 2946/week @ 2024-09-22 3015/week @ 2024-09-29 2819/week @ 2024-10-06 2560/week @ 2024-10-13 3341/week @ 2024-10-20 3791/week @ 2024-10-27 1946/week @ 2024-11-03

11,786 downloads per month
Used in wabt

Apache-2.0

15MB
251K SLoC

WebAssembly 139K SLoC // 0.0% comments C++ 74K SLoC // 0.1% comments Python 21K SLoC // 0.3% comments JavaScript 9K SLoC // 0.1% comments Visual Studio Project 3K SLoC C 2.5K SLoC // 0.1% comments Bazel 513 SLoC // 0.2% comments Automake 423 SLoC // 0.2% comments Shell 400 SLoC // 0.6% comments Rust 256 SLoC // 0.0% comments M4 255 SLoC // 0.4% comments Visual Studio Solution 234 SLoC VB6 150 SLoC Templ 148 SLoC // 0.0% comments Xcode Config 33 SLoC // 0.7% comments Batch 7 SLoC // 0.7% comments Forge Config 2 SLoC

Contains (obscure autoconf code, 7KB) configure.ac, (obscure autoconf code, 1KB) wabt/third_party/gtest/configure.ac, (obscure autoconf code, 3KB) configure.ac

WABT bindings for Rust

crates.io docs.rs

Rust bindings for WABT.

Usage

Add this to your Cargo.toml:

[dependencies]
wabt = "0.9.0"

Use cases

Assemble a given program in WebAssembly text format (aka wat) and translate it into binary.

extern crate wabt;
use wabt::wat2wasm;

fn main() {
    assert_eq!(
        wat2wasm("(module)").unwrap(),
        &[
            0, 97, 115, 109, // \0ASM - magic
            1, 0, 0, 0       //  0x01 - version
        ]
    );
}

or disassemble a wasm binary into the text format.

extern crate wabt;
use wabt::wasm2wat;
fn main() {
    assert_eq!(
        wasm2wat(&[
            0, 97, 115, 109, // \0ASM - magic
            1, 0, 0, 0       //    01 - version
        ]),
        Ok("(module)\n".to_owned()),
    );
}

wabt can be also used for parsing the official testsuite scripts.

use wabt::script::{ScriptParser, Command, CommandKind, Action, Value};

let wast = r#"
;; Define anonymous module with function export named `sub`.
(module
  (func (export "sub") (param $x i32) (param $y i32) (result i32)
    ;; return x - y;
    (i32.sub
      (get_local $x) (get_local $y)
    )
  )
)

;; Assert that invoking export `sub` with parameters (8, 3)
;; should return 5.
(assert_return
  (invoke "sub"
    (i32.const 8) (i32.const 3)
  )
  (i32.const 5)
)
"#;

let mut parser = ScriptParser::<f32, f64>::from_str(wast)?;
while let Some(Command { kind, .. }) = parser.next()? {
    match kind {
        CommandKind::Module { module, name } => {
            // The module is declared as anonymous.
            assert_eq!(name, None);

            // Convert the module into the binary representation and check the magic number.
            let module_binary = module.into_vec();
            assert_eq!(&module_binary[0..4], &[0, 97, 115, 109]);
        }
        CommandKind::AssertReturn { action, expected } => {
            assert_eq!(action, Action::Invoke {
                module: None,
                field: "sub".to_string(),
                args: vec![
                    Value::I32(8),
                    Value::I32(3)
                ],
            });
            assert_eq!(expected, vec![Value::I32(5)]);
        },
        _ => panic!("there are no other commands apart from that defined above"),
    }
}

Alternatives

You might find wat or wast crate useful if you only want to parse .wat or .wast source. The advantage of using them is that they are implemented completely in Rust. Moreover, wast among other things allows you to add your own extensions to WebAssembly text format.

For print the text representation of a wasm binary, wasmprinter can work better for you, since it is implemented completely in Rust.

No runtime deps