|
| 1 | +// Wrapper around the assert macro to have a hand on which exit code we want to give to our failed |
| 2 | +// assertion |
| 3 | +#[macro_export] |
| 4 | +macro_rules! assert { |
| 5 | + ($cond:expr, $(,)?) => {{ |
| 6 | + let res = std::panic::catch_unwind(|| { |
| 7 | + core::assert!($cond); |
| 8 | + }); |
| 9 | + if res.is_err() { |
| 10 | + let panic_msg = match res.err() { |
| 11 | + Some(err) => match err.downcast::<String>() { |
| 12 | + Ok(panic_msg_box) => Some(panic_msg_box.as_str()), |
| 13 | + Err(err) => None, |
| 14 | + }, |
| 15 | + None => unreachable!(), |
| 16 | + }; |
| 17 | + |
| 18 | + $crate::vm::abort( |
| 19 | + fvm_shared::error::ExitCode::USR_ASSERTION_FAILED.value(), |
| 20 | + panic_msg, |
| 21 | + ); |
| 22 | + } |
| 23 | + }}; |
| 24 | +} |
| 25 | + |
| 26 | +// Utility macro to generate macro code for assert_eq and assert_ne |
| 27 | +macro_rules! assert2 { |
| 28 | + ($assert_macro:ident) => { |
| 29 | + with_dollar_sign! { |
| 30 | + ($d:tt) => { |
| 31 | + #[macro_export] |
| 32 | + macro_rules! $assert_macro { |
| 33 | + ($d left:expr, $d right:expr $d(,$d arg:tt)*) => { |
| 34 | + let res = std::panic::catch_unwind(|| { |
| 35 | + core::$assert_macro!($d left, $d right); |
| 36 | + }); |
| 37 | + if res.is_err() { |
| 38 | + let panic_msg = match res.err() { |
| 39 | + Some(err) => match err.downcast::<String>() { |
| 40 | + Ok(panic_msg_box) => Some(panic_msg_box.as_str()), |
| 41 | + Err(err) => None, |
| 42 | + }, |
| 43 | + None => unreachable!(), |
| 44 | + }; |
| 45 | + |
| 46 | + $crate::vm::abort( |
| 47 | + fvm_shared::error::ExitCode::USR_ASSERTION_FAILED.value(), |
| 48 | + panic_msg, |
| 49 | + ); |
| 50 | + } |
| 51 | + }; |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + }; |
| 56 | +} |
| 57 | + |
| 58 | +// Utility macro to allow for nested repetition |
| 59 | +macro_rules! with_dollar_sign { |
| 60 | + ($($body:tt)*) => { |
| 61 | + macro_rules! __with_dollar_sign { $($body)* } |
| 62 | + __with_dollar_sign!($); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +// Wrapper around the assert_eq macro to have a hand on which exit code we want to give to our failed |
| 67 | +// assertion |
| 68 | +assert2!(assert_eq); |
| 69 | + |
| 70 | +// Wrapper around the assert_ne macro to have a hand on which exit code we want to give to our failed |
| 71 | +// assertion |
| 72 | +assert2!(assert_ne); |
0 commit comments