80 releases (breaking)

new 0.220.0 Nov 12, 2024
0.218.0 Sep 30, 2024
0.215.0 Jul 31, 2024
0.202.0 Mar 26, 2024
0.1.0 Nov 23, 2020

#50 in WebAssembly

Download history 270068/week @ 2024-07-27 264181/week @ 2024-08-03 255477/week @ 2024-08-10 272355/week @ 2024-08-17 236620/week @ 2024-08-24 250944/week @ 2024-08-31 247042/week @ 2024-09-07 244283/week @ 2024-09-14 239590/week @ 2024-09-21 247113/week @ 2024-09-28 270559/week @ 2024-10-05 261926/week @ 2024-10-12 277065/week @ 2024-10-19 263477/week @ 2024-10-26 275203/week @ 2024-11-02 259743/week @ 2024-11-09

1,118,668 downloads per month
Used in 737 crates (63 directly)

Apache-2.0…

755KB
17K SLoC

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.ty().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.

Dependencies