24 releases

0.9.2 Sep 10, 2019
0.9.0 Jul 24, 2019
0.7.4 Jan 14, 2019
0.7.3 Dec 18, 2018
0.2.2 Mar 11, 2018

#8 in WebAssembly

Download history 4072/week @ 2019-12-02 4033/week @ 2019-12-09 4555/week @ 2019-12-16 1648/week @ 2019-12-23 1427/week @ 2019-12-30 3464/week @ 2020-01-06 4097/week @ 2020-01-13 2737/week @ 2020-01-20 2672/week @ 2020-01-27 2469/week @ 2020-02-03 3020/week @ 2020-02-10 3054/week @ 2020-02-17 2972/week @ 2020-02-24 2462/week @ 2020-03-02 2816/week @ 2020-03-09 2459/week @ 2020-03-16

18,758 downloads per month
Used in 38 crates (29 directly)

Apache-2.0

3.5MB
46K SLoC

Coq 32K SLoC // 0.1% comments Intel HEX 9K SLoC // 0.1% comments Processing 2.5K SLoC // 0.1% comments Razor 1.5K SLoC // 0.2% comments BrightScript 634 SLoC // 0.1% comments Vim Script 7 SLoC BASH 6 SLoC // 0.7% comments

WABT bindings for Rust

crates.io docs.rs

Rust bindings for WABT. Work in progress.

Usage

Add this to your Cargo.toml:

[dependencies]
wabt = "0.9.0"

Example

wat2wasm (previously known as wast2wasm):

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
        ]
    );
}

wasm2wat:

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::from_str(wast)?;
while let Some(Command { kind, .. }) = parser.next()? { 
    match kind {
        CommandKind::Module { module, name } => {
            // The module is declared as annonymous.
            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"),
    }
}

Dependencies