2020-02-17 12:54:12 +00:00
|
|
|
use wasmer_interface_types::{
|
2020-04-09 08:48:50 +00:00
|
|
|
ast::*, decoders::binary::parse, encoders::binary::ToBytes, interpreter::Instruction, types::*,
|
2020-04-09 10:10:48 +00:00
|
|
|
vec1,
|
2020-02-17 12:54:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Tests an AST to binary, then binary to AST roundtrip.
|
|
|
|
#[test]
|
|
|
|
fn test_binary_encoding_decoding_roundtrip() {
|
|
|
|
let original_ast = Interfaces {
|
2020-02-26 14:32:14 +00:00
|
|
|
types: vec![
|
2020-03-26 10:18:24 +00:00
|
|
|
Type::Function {
|
2020-02-26 14:32:14 +00:00
|
|
|
inputs: vec![],
|
|
|
|
outputs: vec![],
|
|
|
|
},
|
2020-03-26 10:18:24 +00:00
|
|
|
Type::Function {
|
2020-12-28 19:33:58 +00:00
|
|
|
inputs: vec![IType::I32, IType::I32],
|
|
|
|
outputs: vec![IType::S32],
|
2020-02-26 14:32:14 +00:00
|
|
|
},
|
2020-03-26 12:17:36 +00:00
|
|
|
Type::Record(RecordType {
|
2020-12-28 19:33:58 +00:00
|
|
|
fields: vec1![IType::String, IType::I32],
|
2020-03-26 12:17:36 +00:00
|
|
|
}),
|
2020-02-26 14:32:14 +00:00
|
|
|
],
|
2020-02-17 12:54:12 +00:00
|
|
|
imports: vec![Import {
|
|
|
|
namespace: "a",
|
|
|
|
name: "b",
|
2020-05-28 13:05:58 +00:00
|
|
|
function_type: 0,
|
2020-02-17 12:54:12 +00:00
|
|
|
}],
|
2020-02-26 14:32:14 +00:00
|
|
|
adapters: vec![Adapter {
|
|
|
|
function_type: 0,
|
2020-02-17 12:54:12 +00:00
|
|
|
instructions: vec![Instruction::ArgumentGet { index: 1 }],
|
|
|
|
}],
|
2020-02-26 14:32:14 +00:00
|
|
|
exports: vec![Export {
|
|
|
|
name: "ab",
|
|
|
|
function_type: 1,
|
|
|
|
}],
|
|
|
|
implementations: vec![Implementation {
|
|
|
|
core_function_type: 0,
|
|
|
|
adapter_function_type: 0,
|
|
|
|
}],
|
2020-02-17 12:54:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut binary = vec![];
|
|
|
|
|
|
|
|
original_ast
|
|
|
|
.to_bytes(&mut binary)
|
|
|
|
.expect("Failed to encode the AST.");
|
|
|
|
|
|
|
|
let (remainder, ast) = parse::<()>(binary.as_slice()).expect("Failed to decode the AST.");
|
|
|
|
|
|
|
|
assert!(remainder.is_empty());
|
|
|
|
|
|
|
|
assert_eq!(original_ast, ast);
|
|
|
|
}
|