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 |
|
在WebAssembly中排名1266
每月下载8,113次
用于wabt
15MB
251K SLoC
包含 (神秘的autoconf代码,7KB) configure.ac,(神秘的autoconf代码,1KB) wabt/third_party/gtest/configure.ac,(神秘的autoconf代码,3KB) configure.ac
为Rust的WABT绑定
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
源,您可能会发现 wat
或 wast
crate很有用。使用它们的优点是它们完全用Rust实现。此外,wast
等等还允许您向WebAssembly文本格式添加自己的扩展。
为了打印wasm二进制的文本表示,wasmprinter
可以更好地为您服务,因为它完全用Rust实现。