2017-06-03 17:45:03 +03:00
|
|
|
#![cfg(test)]
|
|
|
|
|
2017-06-03 21:39:45 +03:00
|
|
|
use std::env;
|
2017-06-03 17:45:03 +03:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::process::Command;
|
|
|
|
use std::fs::File;
|
2017-06-03 21:20:52 +03:00
|
|
|
use std::sync::Arc;
|
2017-06-03 17:45:03 +03:00
|
|
|
|
|
|
|
use serde_json;
|
|
|
|
use test;
|
2017-06-06 12:07:58 +03:00
|
|
|
use parity_wasm::{self, elements, builder};
|
2017-06-03 21:20:52 +03:00
|
|
|
use parity_wasm::interpreter::{
|
2017-06-05 18:48:08 +03:00
|
|
|
RuntimeValue,
|
2017-06-03 21:20:52 +03:00
|
|
|
ProgramInstance, ModuleInstance, ModuleInstanceInterface,
|
|
|
|
Error as InterpreterError,
|
|
|
|
};
|
|
|
|
|
2017-06-06 12:07:58 +03:00
|
|
|
fn spec_test_module() -> elements::Module {
|
|
|
|
builder::module()
|
|
|
|
.function()
|
|
|
|
.signature().with_param(elements::ValueType::I32).build()
|
|
|
|
.body().build()
|
|
|
|
.build()
|
|
|
|
.export().field("print").internal().func(0).build()
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
|
2017-06-03 21:20:52 +03:00
|
|
|
fn setup_program(base_dir: &str, test_module_path: &str) -> (ProgramInstance, Arc<ModuleInstance>) {
|
|
|
|
let mut wasm_path = PathBuf::from(base_dir.clone());
|
|
|
|
wasm_path.push(test_module_path);
|
|
|
|
let module = parity_wasm::deserialize_file(&wasm_path)
|
|
|
|
.expect(&format!("Wasm file {} failed to load", wasm_path.to_string_lossy()));
|
2017-06-06 12:07:58 +03:00
|
|
|
|
2017-06-03 21:20:52 +03:00
|
|
|
let program = ProgramInstance::new().expect("Failed creating program");
|
2017-06-06 12:07:58 +03:00
|
|
|
program.add_module("spectest", spec_test_module()).expect("Failed adding 'spectest' module");
|
|
|
|
|
2017-06-03 21:20:52 +03:00
|
|
|
let module_instance = program.add_module("test", module).expect("Failed adding module");
|
|
|
|
(program, module_instance)
|
|
|
|
}
|
|
|
|
|
2017-06-05 18:13:17 +03:00
|
|
|
fn try_load(base_dir: &str, module_path: &str) -> Result<parity_wasm::elements::Module, parity_wasm::elements::Error> {
|
|
|
|
let mut wasm_path = PathBuf::from(base_dir.clone());
|
|
|
|
wasm_path.push(module_path);
|
|
|
|
parity_wasm::deserialize_file(&wasm_path)
|
|
|
|
}
|
|
|
|
|
2017-06-03 21:20:52 +03:00
|
|
|
fn runtime_value(test_val: &test::RuntimeValue) -> parity_wasm::RuntimeValue {
|
|
|
|
match test_val.value_type.as_ref() {
|
|
|
|
"i32" => {
|
|
|
|
let unsigned: u32 = test_val.value.parse().expect("Literal parse error");
|
|
|
|
parity_wasm::RuntimeValue::I32(unsigned as i32)
|
|
|
|
},
|
2017-06-05 16:33:12 +03:00
|
|
|
"i64" => {
|
|
|
|
let unsigned: u64 = test_val.value.parse().expect("Literal parse error");
|
|
|
|
parity_wasm::RuntimeValue::I64(unsigned as i64)
|
|
|
|
},
|
|
|
|
"f32" => {
|
|
|
|
let unsigned: u32 = test_val.value.parse().expect("Literal parse error");
|
2017-06-05 18:48:08 +03:00
|
|
|
parity_wasm::RuntimeValue::decode_f32(unsigned)
|
2017-06-05 16:33:12 +03:00
|
|
|
},
|
|
|
|
"f64" => {
|
|
|
|
let unsigned: u64 = test_val.value.parse().expect("Literal parse error");
|
2017-06-05 18:48:08 +03:00
|
|
|
parity_wasm::RuntimeValue::decode_f64(unsigned)
|
2017-06-05 16:33:12 +03:00
|
|
|
},
|
2017-06-03 21:20:52 +03:00
|
|
|
_ => panic!("Unknwon runtime value type"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn runtime_values(test_vals: &[test::RuntimeValue]) -> Vec<parity_wasm::RuntimeValue> {
|
|
|
|
test_vals.iter().map(runtime_value).collect::<Vec<parity_wasm::RuntimeValue>>()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run_action(module: &ModuleInstance, action: &test::Action)
|
|
|
|
-> Result<Option<parity_wasm::RuntimeValue>, InterpreterError>
|
|
|
|
{
|
|
|
|
match *action {
|
|
|
|
test::Action::Invoke { ref field, ref args} => {
|
|
|
|
module.execute_export(field, runtime_values(args).into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-03 17:45:03 +03:00
|
|
|
|
2017-06-03 22:14:38 +03:00
|
|
|
pub fn spec(name: &str) {
|
2017-06-03 17:45:03 +03:00
|
|
|
let outdir = env::var("OUT_DIR").unwrap();
|
|
|
|
|
|
|
|
let mut wast2wasm_path = PathBuf::from(outdir.clone());
|
|
|
|
wast2wasm_path.push("bin");
|
|
|
|
wast2wasm_path.push("wast2wasm");
|
|
|
|
|
|
|
|
let mut json_spec_path = PathBuf::from(outdir.clone());
|
2017-06-03 22:14:38 +03:00
|
|
|
json_spec_path.push(&format!("{}.json", name));
|
2017-06-03 17:45:03 +03:00
|
|
|
|
2017-06-03 22:14:38 +03:00
|
|
|
let wast2wasm_output = Command::new(wast2wasm_path)
|
2017-06-03 17:45:03 +03:00
|
|
|
.arg("--spec")
|
|
|
|
.arg("-o")
|
|
|
|
.arg(&json_spec_path)
|
2017-06-06 11:50:40 +03:00
|
|
|
.arg(&format!("./wabt/third_party/testsuite/{}.wast", name))
|
2017-06-03 17:45:03 +03:00
|
|
|
.output()
|
|
|
|
.expect("Failed to execute process");
|
|
|
|
|
2017-06-05 17:46:12 +03:00
|
|
|
if !wast2wasm_output.status.success() {
|
|
|
|
println!("wasm2wast error code: {}", wast2wasm_output.status);
|
|
|
|
println!("wasm2wast stdout: {}", String::from_utf8_lossy(&wast2wasm_output.stdout));
|
|
|
|
println!("wasm2wast stderr: {}", String::from_utf8_lossy(&wast2wasm_output.stderr));
|
|
|
|
panic!("wasm2wast exited with status {}", wast2wasm_output.status);
|
|
|
|
}
|
2017-06-03 22:14:38 +03:00
|
|
|
|
|
|
|
let mut f = File::open(&json_spec_path)
|
|
|
|
.expect(&format!("Failed to load json file {}", &json_spec_path.to_string_lossy()));
|
|
|
|
let spec: test::Spec = serde_json::from_reader(&mut f).expect("Failed to deserialize JSON file");
|
2017-06-03 21:20:52 +03:00
|
|
|
|
|
|
|
let first_command = &spec.commands[0];
|
2017-06-03 21:39:45 +03:00
|
|
|
let (mut _program, mut module) = match first_command {
|
|
|
|
&test::Command::Module { ref filename, .. } => {
|
2017-06-03 21:20:52 +03:00
|
|
|
setup_program(&outdir, filename)
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
panic!("First command supposed to specify module");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
for command in spec.commands.iter().skip(1) {
|
2017-06-05 15:30:03 +03:00
|
|
|
println!("command {:?}", command);
|
2017-06-03 21:20:52 +03:00
|
|
|
match command {
|
2017-06-03 21:39:45 +03:00
|
|
|
&test::Command::Module { ref filename, .. } => {
|
|
|
|
let (_new_program, new_module) = setup_program(&outdir, &filename);
|
2017-06-03 21:20:52 +03:00
|
|
|
module = new_module;
|
|
|
|
},
|
|
|
|
&test::Command::AssertReturn { line, ref action, ref expected } => {
|
|
|
|
let result = run_action(&*module, action);
|
|
|
|
match result {
|
|
|
|
Ok(result) => {
|
|
|
|
let spec_expected = runtime_values(expected);
|
|
|
|
let actual_result = result.into_iter().collect::<Vec<parity_wasm::RuntimeValue>>();
|
2017-06-06 10:45:05 +03:00
|
|
|
for (actual_result, spec_expected) in actual_result.iter().zip(spec_expected.iter()) {
|
|
|
|
assert_eq!(actual_result.variable_type(), spec_expected.variable_type());
|
|
|
|
// f32::NAN != f32::NAN
|
|
|
|
match spec_expected {
|
|
|
|
&RuntimeValue::F32(val) if val.is_nan() => match actual_result {
|
|
|
|
&RuntimeValue::F32(val) => assert!(val.is_nan()),
|
|
|
|
_ => unreachable!(), // checked above that types are same
|
|
|
|
},
|
|
|
|
&RuntimeValue::F64(val) if val.is_nan() => match actual_result {
|
|
|
|
&RuntimeValue::F64(val) => assert!(val.is_nan()),
|
|
|
|
_ => unreachable!(), // checked above that types are same
|
|
|
|
},
|
|
|
|
spec_expected @ _ => assert_eq!(actual_result, spec_expected),
|
|
|
|
}
|
|
|
|
}
|
2017-06-03 21:33:58 +03:00
|
|
|
println!("assert_return at line {} - success", line);
|
2017-06-03 21:20:52 +03:00
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
panic!("Expected action to return value, got error: {:?}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2017-06-05 18:48:08 +03:00
|
|
|
&test::Command::AssertReturnCanonicalNan { line, ref action } | &test::Command::AssertReturnArithmeticNan { line, ref action } => {
|
|
|
|
let result = run_action(&*module, action);
|
|
|
|
match result {
|
|
|
|
Ok(result) => {
|
|
|
|
for actual_result in result.into_iter().collect::<Vec<parity_wasm::RuntimeValue>>() {
|
|
|
|
match actual_result {
|
|
|
|
RuntimeValue::F32(val) => if !val.is_nan() { panic!("Expected nan value, got {:?}", val) },
|
|
|
|
RuntimeValue::F64(val) => if !val.is_nan() { panic!("Expected nan value, got {:?}", val) },
|
|
|
|
val @ _ => panic!("Expected action to return float value, got {:?}", val),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
println!("assert_return_nan at line {} - success", line);
|
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
panic!("Expected action to return value, got error: {:?}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2017-06-03 21:33:58 +03:00
|
|
|
&test::Command::AssertTrap { line, ref action, .. } => {
|
|
|
|
let result = run_action(&*module, action);
|
|
|
|
match result {
|
|
|
|
Ok(result) => {
|
|
|
|
panic!("Expected action to result in a trap, got result: {:?}", result);
|
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
println!("assert_trap at line {} - success ({:?})", line, e);
|
|
|
|
}
|
|
|
|
}
|
2017-06-05 18:13:17 +03:00
|
|
|
},
|
|
|
|
&test::Command::AssertInvalid { line, ref filename, .. } => {
|
|
|
|
let module_load = try_load(&outdir, filename);
|
|
|
|
match module_load {
|
2017-06-05 19:23:49 +03:00
|
|
|
Ok(_) => {
|
2017-06-05 18:13:17 +03:00
|
|
|
panic!("Expected invalid module definition, got some module!")
|
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
println!("assert_invalid at line {} - success ({:?})", line, e)
|
|
|
|
}
|
|
|
|
}
|
2017-06-05 19:23:49 +03:00
|
|
|
},
|
|
|
|
&test::Command::Action { line, ref action } => {
|
|
|
|
match run_action(&*module, action) {
|
|
|
|
Ok(_) => { },
|
|
|
|
Err(e) => {
|
|
|
|
panic!("Failed to invoke action at line {}: {:?}", line, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2017-06-03 21:20:52 +03:00
|
|
|
}
|
|
|
|
}
|
2017-06-03 21:33:58 +03:00
|
|
|
}
|