24 个版本

使用旧的 Rust 2015

0.10.0 2020年7月27日
0.9.2 2019年9月10日
0.9.1 2019年8月12日
0.9.0 2019年7月24日
0.2.2 2018年3月11日

#220 in WebAssembly

Download history · Rust 包仓库 2725/week @ 2024-03-14 · Rust 包仓库 2682/week @ 2024-03-21 · Rust 包仓库 2019/week @ 2024-03-28 · Rust 包仓库 2442/week @ 2024-04-04 · Rust 包仓库 2342/week @ 2024-04-11 · Rust 包仓库 3261/week @ 2024-04-18 · Rust 包仓库 2851/week @ 2024-04-25 · Rust 包仓库 2714/week @ 2024-05-02 · Rust 包仓库 2013/week @ 2024-05-09 · Rust 包仓库 2225/week @ 2024-05-16 · Rust 包仓库 1896/week @ 2024-05-23 · Rust 包仓库 1715/week @ 2024-05-30 · Rust 包仓库 2060/week @ 2024-06-06 · Rust 包仓库 1807/week @ 2024-06-13 · Rust 包仓库 1979/week @ 2024-06-20 · Rust 包仓库 1972/week @ 2024-06-27 · Rust 包仓库

8,103 每月下载量
65 个包中使用 (55 直接)

Apache-2.0 协议

15MB
252K SLoC

WebAssembly 139K SLoC // 0.0% comments · Rust 包仓库 C++ 74K SLoC // 0.1% comments · Rust 包仓库 Python 21K SLoC // 0.3% comments · Rust 包仓库 JavaScript 9K SLoC // 0.1% comments · Rust 包仓库 Visual Studio Project 3K SLoC · Rust 包仓库 C 2.5K SLoC // 0.1% comments · Rust 包仓库 Rust 1.5K SLoC // 0.0% comments · Rust 包仓库 Bazel 512 SLoC // 0.2% comments · Rust 包仓库 Automake 422 SLoC // 0.2% comments · Rust 包仓库 Shell 399 SLoC // 0.6% comments · Rust 包仓库 M4 254 SLoC // 0.4% comments · Rust 包仓库 Visual Studio Solution 233 SLoC · Rust 包仓库 VB6 149 SLoC · Rust 包仓库 Xcode Config 32 SLoC // 0.7% comments · Rust 包仓库 Batch 6 SLoC // 0.7% comments · Rust 包仓库 Forge Config 1 SLoC · Rust 包仓库

WABT 的 Rust 绑定

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 包很有用。使用它们的优点是它们完全用 Rust 实现。此外,wast 等其他功能允许您向 WebAssembly 文本格式添加自己的扩展。

要打印 wasm 二进制文件的文本表示,wasmprinter 可能更适合您,因为它完全用 Rust 实现。

依赖关系