Lines
100 %
Functions
Branches
use wasm_encoder::{Encode, Instruction};
use super::{store_begin_place, store_end_place};
use crate::ast::{Place, WasmType};
use crate::compiler::context::CompileContext;
use crate::compiler::emit::FunctionEmitter;
fn encoded(instructions: &[Instruction<'_>]) -> Vec<u8> {
let mut bytes = Vec::new();
for instruction in instructions {
instruction.encode(&mut bytes);
}
bytes
fn store(ctx: &CompileContext, place: Place, keep: bool) -> Vec<u8> {
let mut emit = FunctionEmitter::new();
store_begin_place(ctx, &mut emit, place);
emit.i32_const(5);
store_end_place(ctx, &mut emit, place, keep);
emit.bytes_ref().to_vec()
#[test]
fn a_global_store_needs_nothing_below_the_value() {
let ctx = CompileContext::new_with_host_fns(&[]).unwrap();
assert_eq!(
store(&ctx, Place::Global(3), false),
encoded(&[Instruction::I32Const(5), Instruction::GlobalSet(3)])
);
fn a_kept_global_store_rereads_the_global() {
store(&ctx, Place::Global(3), true),
encoded(&[
Instruction::I32Const(5),
Instruction::GlobalSet(3),
Instruction::GlobalGet(3),
])
fn a_plain_local_store_is_local_set_or_tee() {
let mut ctx = CompileContext::new_with_host_fns(&[]).unwrap();
let idx = ctx.alloc_local(WasmType::I32).unwrap();
store(&ctx, Place::Local(idx), false),
encoded(&[Instruction::I32Const(5), Instruction::LocalSet(idx)])
store(&ctx, Place::Local(idx), true),
encoded(&[Instruction::I32Const(5), Instruction::LocalTee(idx)])
fn a_celled_local_store_pushes_the_cell_first() {
let idx = ctx.alloc_cell_local(WasmType::I32).unwrap();
let cell = ctx.cell_of(idx).unwrap();
Instruction::LocalGet(idx),
Instruction::StructSet {
struct_type_index: cell,
field_index: 0
},