mirror of
https://github.com/roc-lang/roc.git
synced 2024-11-11 05:34:11 +03:00
wasm_interp: move test_utils into tests/mod.rs
This commit is contained in:
parent
ffd35f5884
commit
4714b4599a
@ -1,6 +1,5 @@
|
||||
mod call_stack;
|
||||
mod instance;
|
||||
pub mod test_utils;
|
||||
mod tests;
|
||||
mod value_stack;
|
||||
pub mod wasi;
|
||||
|
@ -1,111 +0,0 @@
|
||||
use crate::{DefaultImportDispatcher, Instance, DEFAULT_IMPORTS};
|
||||
use bumpalo::{collections::Vec, Bump};
|
||||
use roc_wasm_module::{
|
||||
opcodes::OpCode, Export, ExportType, SerialBuffer, Signature, Value, ValueType, WasmModule,
|
||||
};
|
||||
|
||||
pub fn default_state(arena: &Bump) -> Instance<DefaultImportDispatcher> {
|
||||
let pages = 1;
|
||||
let program_counter = 0;
|
||||
let globals = [];
|
||||
Instance::new(arena, pages, program_counter, globals, DEFAULT_IMPORTS)
|
||||
}
|
||||
|
||||
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;
|
||||
let func_len_index = buf.encode_padded_u32(0);
|
||||
let start = buf.len();
|
||||
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
|
||||
|
||||
buf.overwrite_padded_u32(func_len_index, (buf.len() - start) as u32);
|
||||
|
||||
module.code.function_count = 1;
|
||||
module.code.function_offsets.push(0);
|
||||
module.add_function_signature(Signature {
|
||||
param_types: Vec::new_in(&arena),
|
||||
ret_type: Some(ValueType::from(expected)),
|
||||
});
|
||||
module.export.append(Export {
|
||||
name: "test",
|
||||
ty: ExportType::Func,
|
||||
index: 0,
|
||||
});
|
||||
}
|
||||
|
||||
// Dump the generated module to a file (this is mainly for debugging the test itself)
|
||||
if std::env::var("DEBUG_WASM_INTERP_TEST").is_ok() {
|
||||
let filename = format!("/tmp/{:?}.wasm", op);
|
||||
println!("\nDumping test module to {}\n", &filename);
|
||||
let mut outfile_buf = Vec::new_in(&arena);
|
||||
module.serialize(&mut outfile_buf);
|
||||
std::fs::write(&filename, outfile_buf).unwrap();
|
||||
}
|
||||
|
||||
let mut inst = Instance::for_module(&arena, &module, DEFAULT_IMPORTS, true).unwrap();
|
||||
|
||||
let return_val = inst.call_export(&module, "test", []).unwrap().unwrap();
|
||||
|
||||
assert_eq!(return_val, expected);
|
||||
}
|
||||
|
||||
pub fn create_exported_function_no_locals<'a, F>(
|
||||
module: &mut WasmModule<'a>,
|
||||
name: &'a str,
|
||||
signature: Signature<'a>,
|
||||
write_instructions: F,
|
||||
) where
|
||||
F: FnOnce(&mut Vec<'a, u8>),
|
||||
{
|
||||
let internal_fn_index = module.code.function_offsets.len();
|
||||
let fn_index = module.import.function_count() + internal_fn_index;
|
||||
module.export.exports.push(Export {
|
||||
name,
|
||||
ty: ExportType::Func,
|
||||
index: fn_index as u32,
|
||||
});
|
||||
module.add_function_signature(signature);
|
||||
|
||||
let offset = module.code.bytes.encode_padded_u32(0);
|
||||
let start = module.code.bytes.len();
|
||||
module.code.bytes.push(0); // no locals
|
||||
write_instructions(&mut module.code.bytes);
|
||||
let len = module.code.bytes.len() - start;
|
||||
module.code.bytes.overwrite_padded_u32(offset, len as u32);
|
||||
|
||||
module.code.function_count += 1;
|
||||
module.code.function_offsets.push(offset as u32);
|
||||
}
|
@ -7,3 +7,115 @@ mod test_f64;
|
||||
mod test_i32;
|
||||
mod test_i64;
|
||||
mod test_mem;
|
||||
|
||||
use crate::{DefaultImportDispatcher, Instance, DEFAULT_IMPORTS};
|
||||
use bumpalo::{collections::Vec, Bump};
|
||||
use roc_wasm_module::{
|
||||
opcodes::OpCode, Export, ExportType, SerialBuffer, Signature, Value, ValueType, WasmModule,
|
||||
};
|
||||
|
||||
pub fn default_state(arena: &Bump) -> Instance<DefaultImportDispatcher> {
|
||||
let pages = 1;
|
||||
let program_counter = 0;
|
||||
let globals = [];
|
||||
Instance::new(arena, pages, program_counter, globals, DEFAULT_IMPORTS)
|
||||
}
|
||||
|
||||
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;
|
||||
let func_len_index = buf.encode_padded_u32(0);
|
||||
let start = buf.len();
|
||||
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
|
||||
|
||||
buf.overwrite_padded_u32(func_len_index, (buf.len() - start) as u32);
|
||||
|
||||
module.code.function_count = 1;
|
||||
module.code.function_offsets.push(0);
|
||||
module.add_function_signature(Signature {
|
||||
param_types: Vec::new_in(&arena),
|
||||
ret_type: Some(ValueType::from(expected)),
|
||||
});
|
||||
module.export.append(Export {
|
||||
name: "test",
|
||||
ty: ExportType::Func,
|
||||
index: 0,
|
||||
});
|
||||
}
|
||||
|
||||
// Dump the generated module to a file (this is mainly for debugging the test itself)
|
||||
if std::env::var("DEBUG_WASM_INTERP_TEST").is_ok() {
|
||||
let filename = format!("/tmp/{:?}.wasm", op);
|
||||
println!("\nDumping test module to {}\n", &filename);
|
||||
let mut outfile_buf = Vec::new_in(&arena);
|
||||
module.serialize(&mut outfile_buf);
|
||||
std::fs::write(&filename, outfile_buf).unwrap();
|
||||
}
|
||||
|
||||
let mut inst = Instance::for_module(&arena, &module, DEFAULT_IMPORTS, true).unwrap();
|
||||
|
||||
let return_val = inst.call_export(&module, "test", []).unwrap().unwrap();
|
||||
|
||||
assert_eq!(return_val, expected);
|
||||
}
|
||||
|
||||
pub fn create_exported_function_no_locals<'a, F>(
|
||||
module: &mut WasmModule<'a>,
|
||||
name: &'a str,
|
||||
signature: Signature<'a>,
|
||||
write_instructions: F,
|
||||
) where
|
||||
F: FnOnce(&mut Vec<'a, u8>),
|
||||
{
|
||||
let internal_fn_index = module.code.function_offsets.len();
|
||||
let fn_index = module.import.function_count() + internal_fn_index;
|
||||
module.export.exports.push(Export {
|
||||
name,
|
||||
ty: ExportType::Func,
|
||||
index: fn_index as u32,
|
||||
});
|
||||
module.add_function_signature(signature);
|
||||
|
||||
let offset = module.code.bytes.encode_padded_u32(0);
|
||||
let start = module.code.bytes.len();
|
||||
module.code.bytes.push(0); // no locals
|
||||
write_instructions(&mut module.code.bytes);
|
||||
let len = module.code.bytes.len() - start;
|
||||
module.code.bytes.overwrite_padded_u32(offset, len as u32);
|
||||
|
||||
module.code.function_count += 1;
|
||||
module.code.function_offsets.push(offset as u32);
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
#![cfg(test)]
|
||||
|
||||
use crate::test_utils::{const_value, create_exported_function_no_locals, default_state};
|
||||
use crate::{Action, ImportDispatcher, Instance, ValueStack, DEFAULT_IMPORTS};
|
||||
use super::{const_value, create_exported_function_no_locals, default_state};
|
||||
use crate::{instance::Action, ImportDispatcher, Instance, ValueStack, DEFAULT_IMPORTS};
|
||||
use bumpalo::{collections::Vec, Bump};
|
||||
use roc_wasm_module::sections::{Import, ImportDesc};
|
||||
use roc_wasm_module::{
|
||||
|
@ -1,6 +1,6 @@
|
||||
#![cfg(test)]
|
||||
|
||||
use crate::test_utils::test_op_example;
|
||||
use super::test_op_example;
|
||||
use roc_wasm_module::{opcodes::OpCode::*, Value};
|
||||
|
||||
#[test]
|
||||
|
@ -1,6 +1,6 @@
|
||||
#![cfg(test)]
|
||||
|
||||
use crate::test_utils::test_op_example;
|
||||
use super::test_op_example;
|
||||
use roc_wasm_module::{opcodes::OpCode, opcodes::OpCode::*, Value};
|
||||
|
||||
fn test_f32_comparison(op: OpCode, arg1: f32, arg2: f32, expected: bool) {
|
||||
|
@ -1,6 +1,6 @@
|
||||
#![cfg(test)]
|
||||
|
||||
use crate::test_utils::test_op_example;
|
||||
use super::test_op_example;
|
||||
use roc_wasm_module::{opcodes::OpCode, opcodes::OpCode::*, Value};
|
||||
|
||||
fn test_f64_comparison(op: OpCode, arg1: f64, arg2: f64, expected: bool) {
|
||||
|
@ -1,6 +1,6 @@
|
||||
#![cfg(test)]
|
||||
|
||||
use crate::test_utils::test_op_example;
|
||||
use super::test_op_example;
|
||||
use roc_wasm_module::{opcodes::OpCode, opcodes::OpCode::*, Value};
|
||||
|
||||
fn test_i32_binop(op: OpCode, arg1: i32, arg2: i32, expected: i32) {
|
||||
|
@ -1,6 +1,6 @@
|
||||
#![cfg(test)]
|
||||
|
||||
use crate::test_utils::test_op_example;
|
||||
use super::test_op_example;
|
||||
use roc_wasm_module::{opcodes::OpCode, opcodes::OpCode::*, Value};
|
||||
|
||||
fn test_i64_comparison(op: OpCode, arg1: i64, arg2: i64, expected: bool) {
|
||||
|
@ -1,4 +1,5 @@
|
||||
use crate::{test_utils::create_exported_function_no_locals, Instance, DEFAULT_IMPORTS};
|
||||
use super::create_exported_function_no_locals;
|
||||
use crate::{Instance, DEFAULT_IMPORTS};
|
||||
use bumpalo::{collections::Vec, Bump};
|
||||
use roc_wasm_module::{
|
||||
opcodes::OpCode,
|
||||
|
Loading…
Reference in New Issue
Block a user