Lines
92.79 %
Functions
59.42 %
Branches
100 %
use wasm_encoder::{BlockType, Function, Instruction, MemArg, ValType};
pub struct FunctionEmitter {
func: Function,
}
impl FunctionEmitter {
pub fn new() -> Self {
Self {
func: Function::new([]),
pub fn new_with_locals(locals: &[(u32, ValType)]) -> Self {
func: Function::new(locals.iter().copied()),
pub fn memory_init(&mut self, data_idx: u32, dst: u32, len: u32) {
self.func.instruction(&Instruction::I32Const(dst as i32));
self.func.instruction(&Instruction::I32Const(0)); // src offset in data segment
self.func.instruction(&Instruction::I32Const(len as i32));
self.func.instruction(&Instruction::MemoryInit {
mem: 0,
data_index: data_idx,
});
pub fn local_get(&mut self, idx: u32) {
self.func.instruction(&Instruction::LocalGet(idx));
pub fn local_set(&mut self, idx: u32) {
self.func.instruction(&Instruction::LocalSet(idx));
pub fn i32_const(&mut self, value: i32) {
self.func.instruction(&Instruction::I32Const(value));
pub fn i32_add(&mut self) {
self.func.instruction(&Instruction::I32Add);
pub fn i32_ge_u(&mut self) {
self.func.instruction(&Instruction::I32GeU);
pub fn array_new_data(&mut self, type_idx: u32, data_idx: u32) {
self.func.instruction(&Instruction::ArrayNewData {
array_type_index: type_idx,
array_data_index: data_idx,
pub fn array_get_u(&mut self, type_idx: u32) {
self.func.instruction(&Instruction::ArrayGetU(type_idx));
pub fn block_start(&mut self) {
self.func.instruction(&Instruction::Block(BlockType::Empty));
pub fn loop_start(&mut self) {
self.func.instruction(&Instruction::Loop(BlockType::Empty));
pub fn block_end(&mut self) {
self.func.instruction(&Instruction::End);
pub fn br(&mut self, depth: u32) {
self.func.instruction(&Instruction::Br(depth));
pub fn br_if(&mut self, depth: u32) {
self.func.instruction(&Instruction::BrIf(depth));
pub fn call(&mut self, func_idx: u32) {
self.func.instruction(&Instruction::Call(func_idx));
pub fn store_u8_dynamic(&mut self, local_base: u32, offset: u32, value: u8) {
self.func.instruction(&Instruction::LocalGet(local_base));
self.func.instruction(&Instruction::I32Const(offset as i32));
self.func
.instruction(&Instruction::I32Const(i32::from(value)));
self.func.instruction(&Instruction::I32Store8(MemArg {
offset: 0,
align: 0,
memory_index: 0,
}));
pub fn store_i32_dynamic(&mut self, local_base: u32, offset: u32, value: i32) {
self.func.instruction(&Instruction::I32Store(MemArg {
align: 2,
pub fn store_i64_dynamic(&mut self, local_base: u32, offset: u32, value: i64) {
self.func.instruction(&Instruction::I64Const(value));
self.func.instruction(&Instruction::I64Store(MemArg {
align: 3,
pub fn i32_store8_raw(&mut self) {
pub fn end(&mut self) {
pub fn finish(self) -> Function {
impl Default for FunctionEmitter {
fn default() -> Self {
Self::new()