wasm_interp: create a test_utils module

This commit is contained in:
Brian Carroll 2022-11-27 22:08:37 +00:00
parent e3df393a22
commit eeca876421
No known key found for this signature in database
GPG Key ID: 5C7B2EC4101703C0
3 changed files with 66 additions and 29 deletions

View File

@ -1,5 +1,6 @@
mod call_stack;
mod execute;
pub mod test_utils;
mod value_stack;
// Exposed for testing only. Should eventually become private.

View File

@ -0,0 +1,64 @@
use crate::{Action, ExecutionState};
use bumpalo::{collections::Vec, Bump};
use roc_wasm_module::{opcodes::OpCode, SerialBuffer, Value, WasmModule};
pub fn default_state(arena: &Bump) -> ExecutionState {
let pages = 1;
let program_counter = 0;
let globals = [];
ExecutionState::new(arena, pages, program_counter, globals)
}
pub fn const_value(buf: &mut Vec<'_, u8>, value: Value) {
use Value::*;
match value {
I32(x) => {
buf.push(OpCode::I32CONST as u8);
buf.encode_i32(x);
}
I64(x) => {
buf.push(OpCode::I64CONST as u8);
buf.encode_i64(x);
}
F32(x) => {
buf.push(OpCode::F32CONST as u8);
buf.encode_f32(x);
}
F64(x) => {
buf.push(OpCode::F64CONST as u8);
buf.encode_f64(x);
}
}
}
pub fn test_op_example<A>(op: OpCode, args: A, expected: Value)
where
A: IntoIterator<Item = Value>,
{
let arena = Bump::new();
let mut module = WasmModule::new(&arena);
{
let buf = &mut module.code.bytes;
buf.push(0); // no locals
for arg in args {
const_value(buf, arg);
}
buf.push(op as u8);
buf.push(OpCode::END as u8); // end function
}
let mut state = default_state(&arena);
state.call_stack.push_frame(
0,
0,
&[],
&mut state.value_stack,
&module.code.bytes,
&mut state.program_counter,
);
while let Action::Continue = state.execute_next_instruction(&module) {}
assert_eq!(state.value_stack.pop(), expected);
}

View File

@ -1,6 +1,7 @@
#![cfg(test)]
use bumpalo::{collections::Vec, Bump};
use roc_wasm_interp::test_utils::{const_value, default_state};
use roc_wasm_interp::{Action, ExecutionState, ValueStack};
use roc_wasm_module::{
opcodes::OpCode,
@ -9,35 +10,6 @@ use roc_wasm_module::{
WasmModule,
};
fn default_state(arena: &Bump) -> ExecutionState {
let pages = 1;
let program_counter = 0;
let globals = [];
ExecutionState::new(arena, pages, program_counter, globals)
}
fn const_value(buf: &mut Vec<'_, u8>, value: Value) {
use Value::*;
match value {
I32(x) => {
buf.push(OpCode::I32CONST as u8);
buf.encode_i32(x);
}
I64(x) => {
buf.push(OpCode::I64CONST as u8);
buf.encode_i64(x);
}
F32(x) => {
buf.push(OpCode::F32CONST as u8);
buf.encode_f32(x);
}
F64(x) => {
buf.push(OpCode::F64CONST as u8);
buf.encode_f64(x);
}
}
}
#[test]
fn test_loop() {
test_loop_help(10, 55);