|
| 1 | +use wasmer_interface_types::{ |
| 2 | + ast::*, decoders::binary::parse, encoders::binary::ToBytes, interpreter::Instruction, |
| 3 | +}; |
| 4 | + |
| 5 | +/// Tests an AST to binary, then binary to AST roundtrip. |
| 6 | +#[test] |
| 7 | +fn test_binary_encoding_decoding_roundtrip() { |
| 8 | + let original_ast = Interfaces { |
| 9 | + exports: vec![Export { |
| 10 | + name: "ab", |
| 11 | + input_types: vec![InterfaceType::I32], |
| 12 | + output_types: vec![InterfaceType::I32], |
| 13 | + }], |
| 14 | + types: vec![Type::new( |
| 15 | + "ab", |
| 16 | + vec!["cd", "e"], |
| 17 | + vec![InterfaceType::I32, InterfaceType::I32], |
| 18 | + )], |
| 19 | + imports: vec![Import { |
| 20 | + namespace: "a", |
| 21 | + name: "b", |
| 22 | + input_types: vec![InterfaceType::I32], |
| 23 | + output_types: vec![InterfaceType::I64], |
| 24 | + }], |
| 25 | + adapters: vec![Adapter::Import { |
| 26 | + namespace: "a", |
| 27 | + name: "b", |
| 28 | + input_types: vec![InterfaceType::I32], |
| 29 | + output_types: vec![InterfaceType::I32], |
| 30 | + instructions: vec![Instruction::ArgumentGet { index: 1 }], |
| 31 | + }], |
| 32 | + forwards: vec![Forward { name: "a" }], |
| 33 | + }; |
| 34 | + |
| 35 | + let mut binary = vec![]; |
| 36 | + |
| 37 | + original_ast |
| 38 | + .to_bytes(&mut binary) |
| 39 | + .expect("Failed to encode the AST."); |
| 40 | + |
| 41 | + let (remainder, ast) = parse::<()>(binary.as_slice()).expect("Failed to decode the AST."); |
| 42 | + |
| 43 | + assert!(remainder.is_empty()); |
| 44 | + |
| 45 | + assert_eq!(original_ast, ast); |
| 46 | +} |
0 commit comments