17 releases (9 breaking)
Uses new Rust 2024
| 0.10.0 | Feb 24, 2026 |
|---|---|
| 0.8.2 | Apr 5, 2020 |
| 0.3.1 | Mar 27, 2020 |
| 0.0.0 | Nov 6, 2019 |
#77 in WebAssembly
Used in 3 crates
(2 directly)
150KB
3.5K
SLoC
webassembly-rs
A Rust library providing complete WebAssembly opcode constants, parsing, and encoding.
100% Spec Test Coverage: This library successfully parses all 4,441+ valid WebAssembly test cases from the official WebAssembly spec test suite.
Features
- ✅ Complete opcode coverage - All 300+ WebAssembly opcodes (Core Spec 3.0)
- ✅ Full parser and encoder - Parse
.wasmfiles and encode back to bytes - ✅ 100% spec test validation - Verified against 5,444 official spec tests
- ✅ Round-trip verified - parse → encode → parse produces valid wasm
- ✅
#![no_std]compatible - Works in embedded environments - ✅ Zero dependencies - Only requires
alloc
Usage
Add to your Cargo.toml:
[dependencies]
webassembly = "0.9"
Quick Start
use webassembly::{parse, encode};
// Parse a WebAssembly binary
let wasm_bytes = std::fs::read("example.wasm").expect("Failed to read wasm");
let program = parse(&wasm_bytes).expect("Failed to parse wasm");
// Access parsed sections
for section in &program.sections {
println!("Found section: {:?}", section);
}
// Encode back to WebAssembly binary
let encoded = encode(&program);
std::fs::write("output.wasm", encoded).expect("Failed to write wasm");
Testing
The library is validated against the complete official WebAssembly spec test suite (5,444 tests):
# Run unit tests (LEB128, section parsing, round-trips)
cargo test
# Run spec test suite (5,444 WebAssembly binaries)
cargo test -- --ignored
# Or use make
make test-all
Test Results
| Test Category | Count | Status |
|---|---|---|
| Unit tests | 15 | ✅ Passing |
| Spec parsing tests | 4,841 | ✅ Passing (100% of valid wasm) |
| Round-trip tests | 4,821 | ✅ Passing (100%) |
| Malformed tests | 608 | ✅ Correctly rejected |
Spec Test Coverage
This library achieves 100% coverage of valid WebAssembly from the official spec:
- All 4,441 valid WebAssembly test binaries parse successfully
- All 608
assert_malformedtests are correctly rejected - Full round-trip validation (parse → encode → re-parse)
API Overview
Core Types
Program- A parsed WebAssembly module containing sectionsSection- WebAssembly sections (Type, Import, Function, Memory, Export, Code, Data, etc.)Instruction- All WebAssembly opcodes with their operandsValType- Value types (I32, I64, F32, F64, etc.)
Opcode Constants
Complete opcode constants matching the WebAssembly spec:
use webassembly::op::*;
// Control flow
assert_eq!(UNREACHABLE, 0x00);
assert_eq!(NOP, 0x01);
assert_eq!(CALL, 0x10);
// Memory
assert_eq!(I32_LOAD, 0x28);
assert_eq!(I32_STORE, 0x36);
// SIMD (300+ SIMD opcodes included)
assert_eq!(I8X16_ADD, 0xFD); // Prefixed opcodes
LEB128 Encoding
Utilities for WebAssembly's variable-length integer encoding:
use webassembly::leb;
let encoded = leb::encode_u32(1234);
let (value, bytes_read) = leb::decode_u32(&encoded).unwrap();
Features
#![no_std] Support
This library works without the standard library:
#![no_std]
extern crate alloc;
use webassembly::{parse, encode};
Spec Test Validation
The library includes comprehensive integration tests that parse the official WebAssembly spec test suite. To run:
# Requires spec tests submodule
git submodule update --init tests/spec-tests
cargo test -- --ignored
Related Projects
- watson - A more featureful WebAssembly library for Rust
- WebAssembly Spec - Official WebAssembly specification
License
This project is licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in webassembly by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.