1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use crate::{CompileError, WasmError};
use wasmparser::BinaryReaderError;
#[macro_export]
macro_rules! wasm_unsupported {
($($arg:tt)*) => { $crate::WasmError::Unsupported(format!($($arg)*)) }
}
impl From<BinaryReaderError> for WasmError {
fn from(original: BinaryReaderError) -> Self {
Self::InvalidWebAssembly {
message: original.message().into(),
offset: original.offset(),
}
}
}
impl From<BinaryReaderError> for CompileError {
fn from(original: BinaryReaderError) -> Self {
Self::from(WasmError::from(original))
}
}
#[cfg(test)]
mod tests {
use super::*;
use wasmparser::BinaryReader;
#[test]
fn can_convert_binary_reader_error_to_wasm_error() {
let mut reader = BinaryReader::new(b"\0\0\0\0");
let binary_reader_error = reader.read_bytes(10).unwrap_err();
match WasmError::from(binary_reader_error) {
WasmError::InvalidWebAssembly { message, offset } => {
assert_eq!(message, "Unexpected EOF");
assert_eq!(offset, 0);
}
err => panic!("Unexpected error: {:?}", err),
}
}
#[test]
fn can_convert_binary_reader_error_to_compile_error() {
let mut reader = BinaryReader::new(b"\0\0\0\0");
let binary_reader_error = reader.read_bytes(10).unwrap_err();
match CompileError::from(binary_reader_error) {
CompileError::Wasm(WasmError::InvalidWebAssembly { message, offset }) => {
assert_eq!(message, "Unexpected EOF");
assert_eq!(offset, 0);
}
err => panic!("Unexpected error: {:?}", err),
}
}
}