63 releases (breaking)

0.207.0 May 7, 2024
0.205.0 Apr 18, 2024
0.202.0 Mar 26, 2024
0.38.1 Nov 29, 2023
0.1.0 Nov 23, 2020

#62 in WebAssembly

Download history 202912/week @ 2024-01-24 229743/week @ 2024-01-31 213626/week @ 2024-02-07 228904/week @ 2024-02-14 228004/week @ 2024-02-21 238182/week @ 2024-02-28 202405/week @ 2024-03-06 209165/week @ 2024-03-13 214125/week @ 2024-03-20 198224/week @ 2024-03-27 228102/week @ 2024-04-03 229783/week @ 2024-04-10 221769/week @ 2024-04-17 220523/week @ 2024-04-24 199596/week @ 2024-05-01 178843/week @ 2024-05-08

862,750 downloads per month
Used in 622 crates (55 directly)

Apache-2.0 WITH LLVM-exception

550KB
12K 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.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