18个版本

使用旧的Rust 2015

0.8.0 2020年7月27日
0.7.1 2020年3月18日
0.7.0 2019年9月10日
0.6.1 2019年7月24日
0.1.5 2018年2月13日

WebAssembly中排名1266

Download history 2718/week @ 2024-03-14 2661/week @ 2024-03-21 2006/week @ 2024-03-28 2461/week @ 2024-04-04 2333/week @ 2024-04-11 3268/week @ 2024-04-18 2850/week @ 2024-04-25 2712/week @ 2024-05-02 2017/week @ 2024-05-09 2227/week @ 2024-05-16 1890/week @ 2024-05-23 1728/week @ 2024-05-30 2057/week @ 2024-06-06 1806/week @ 2024-06-13 1988/week @ 2024-06-20 1977/week @ 2024-06-27

每月下载8,113
用于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 Xcode Config 33 SLoC // 0.7% comments Batch 7 SLoC // 0.7% comments Forge Config 2 SLoC

包含 (神秘的autoconf代码,7KB) configure.ac,(神秘的autoconf代码,1KB) wabt/third_party/gtest/configure.ac,(神秘的autoconf代码,3KB) configure.ac

为Rust的WABT绑定

crates.io docs.rs

Rust对WABT的绑定。

用法

将此添加到您的 Cargo.toml

[dependencies]
wabt = "0.9.0"

用例

将给定程序在WebAssembly文本格式(即wat)中汇编并转换为二进制。

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

或将wasm二进制文件反汇编为文本格式。

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 还可以用于解析官方 测试套件 脚本。

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"),
    }
}

替代方案

如果您只想解析 .wat.wast 源,您可能会发现 watwast crate很有用。使用它们的优点是它们完全用Rust实现。此外,wast 等等还允许您向WebAssembly文本格式添加自己的扩展。

为了打印wasm二进制的文本表示,wasmprinter 可以更好地为您服务,因为它完全用Rust实现。

无运行时依赖项