85 releases (breaking)

new 0.223.0 Jan 8, 2025
0.222.0 Dec 18, 2024
0.221.2 Dec 2, 2024
0.221.0 Nov 27, 2024
0.1.0 Nov 23, 2020

#82 in WebAssembly

Download history 240750/week @ 2024-09-22 246412/week @ 2024-09-29 272321/week @ 2024-10-06 258641/week @ 2024-10-13 278975/week @ 2024-10-20 263954/week @ 2024-10-27 276196/week @ 2024-11-03 261233/week @ 2024-11-10 286716/week @ 2024-11-17 272300/week @ 2024-11-24 322813/week @ 2024-12-01 308955/week @ 2024-12-08 297537/week @ 2024-12-15 165884/week @ 2024-12-22 177186/week @ 2024-12-29 288379/week @ 2025-01-05

950,947 downloads per month
Used in 774 crates (68 directly)

Apache-2.0…

795KB
18K 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