Skip to content

Commit f820a2e

Browse files
authored
Add wasm-encoder API to directly encode instructions (#1985)
* Delegate `Instruction` encoding to a new type * Refactor to prefer `InstructionSink` * Remove the typed indices * Fix build
1 parent 14629cc commit f820a2e

File tree

19 files changed

+5948
-3080
lines changed

19 files changed

+5948
-3080
lines changed

crates/wasm-encoder/README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ And then you can encode WebAssembly binaries via:
2626

2727
```rust
2828
use wasm_encoder::{
29-
CodeSection, ExportKind, ExportSection, Function, FunctionSection, Instruction,
29+
CodeSection, ExportKind, ExportSection, Function, FunctionSection,
3030
Module, TypeSection, ValType,
3131
};
3232

@@ -54,10 +54,11 @@ module.section(&exports);
5454
let mut codes = CodeSection::new();
5555
let locals = vec![];
5656
let mut f = Function::new(locals);
57-
f.instruction(&Instruction::LocalGet(0));
58-
f.instruction(&Instruction::LocalGet(1));
59-
f.instruction(&Instruction::I32Add);
60-
f.instruction(&Instruction::End);
57+
f.instructions()
58+
.local_get(0)
59+
.local_get(1)
60+
.i32_add()
61+
.end();
6162
codes.function(&f);
6263
module.section(&codes);
6364

crates/wasm-encoder/src/core.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod exports;
88
mod functions;
99
mod globals;
1010
mod imports;
11+
mod instructions;
1112
mod linking;
1213
mod memories;
1314
mod names;
@@ -27,6 +28,7 @@ pub use exports::*;
2728
pub use functions::*;
2829
pub use globals::*;
2930
pub use imports::*;
31+
pub use instructions::*;
3032
pub use linking::*;
3133
pub use memories::*;
3234
pub use names::*;

crates/wasm-encoder/src/core/branch_hints.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ use alloc::vec::Vec;
2525
/// let mut code = CodeSection::new();
2626
/// let mut body = Function::new([]);
2727
///
28-
/// body.instruction(&Instruction::I32Const(1));
28+
/// body.instructions().i32_const(1);
2929
/// let if_offset = body.byte_len();
30-
/// body.instruction(&Instruction::If(BlockType::Empty));
31-
/// body.instruction(&Instruction::End);
32-
/// body.instruction(&Instruction::End);
30+
/// body.instructions()
31+
/// .if_(BlockType::Empty)
32+
/// .end()
33+
/// .end();
3334
/// code.function(&body);
3435
///
3536
/// let mut hints = BranchHints::new();

0 commit comments

Comments
 (0)