Files
tubestation/third_party/rust/wasm-encoder
Ben Visness 9756124bfc Bug 1926357: Update wasm spec tests to wasm-3.0. r=rhunt,supply-chain-reviewers
Many proposals have been merged into the wasm-3.0 branch of the
WebAssembly spec repo. It currently represents the state of the art of
wasm, which we keep up with - proposals that are officially standardized
but not yet super widely implemented.

Basing our spec tests on this reduces duplication across proposal repos
and should help us catch spec problems earlier.

This patch reworks the spec test generator slightly to handle some new
module definition syntax, and removes proposals which have already been
merged into wasm-3.0.

Differential Revision: https://phabricator.services.mozilla.com/D227573
2024-11-11 15:38:43 +00:00
..

wasm-encoder

A Bytecode Alliance project

A WebAssembly encoder for Rust.

Crates.io version Download docs.rs docs

Usage

Add wasm-encoder to your Cargo.toml

$ cargo add wasm-encoder

And then you can encode WebAssembly binaries via:

use wasm_encoder::{
    CodeSection, ExportKind, ExportSection, Function, FunctionSection, Instruction,
    Module, TypeSection, ValType,
};

let mut module = Module::new();

// Encode the type section.
let mut types = TypeSection::new();
let params = vec![ValType::I32, ValType::I32];
let results = vec![ValType::I32];
types.function(params, results);
module.section(&types);

// Encode the function section.
let mut functions = FunctionSection::new();
let type_index = 0;
functions.function(type_index);
module.section(&functions);

// Encode the export section.
let mut exports = ExportSection::new();
exports.export("f", ExportKind::Func, 0);
module.section(&exports);

// Encode the code section.
let mut codes = CodeSection::new();
let locals = vec![];
let mut f = Function::new(locals);
f.instruction(&Instruction::LocalGet(0));
f.instruction(&Instruction::LocalGet(1));
f.instruction(&Instruction::I32Add);
f.instruction(&Instruction::End);
codes.function(&f);
module.section(&codes);

// Extract the encoded Wasm bytes for this module.
let wasm_bytes = module.finish();

// We generated a valid Wasm module!
assert!(wasmparser::validate(&wasm_bytes).is_ok());

License

This project is licensed under the Apache 2.0 license with the LLVM exception. See LICENSE for more details.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be licensed as above, without any additional terms or conditions.