diff --git a/interpreter-lib/src/execution/air/match_.rs b/interpreter-lib/src/execution/air/match_.rs index f7efaffd..7c951455 100644 --- a/interpreter-lib/src/execution/air/match_.rs +++ b/interpreter-lib/src/execution/air/match_.rs @@ -46,9 +46,9 @@ mod tests { use crate::contexts::execution_trace::ExecutionTrace; use crate::JValue; - use aqua_test_utils::call_vm; use aqua_test_utils::create_aqua_vm; use aqua_test_utils::echo_string_call_service; + use aqua_test_utils::{call_vm, set_variable_call_service}; use std::rc::Rc; @@ -194,4 +194,47 @@ mod tests { assert_eq!(res.ret_code, 1015); } + + #[test] + fn match_with_two_xors() { + use crate::contexts::execution_trace::CallResult::*; + use crate::contexts::execution_trace::ExecutedState::*; + + let local_peer_id = "local_peer_id"; + let mut vm = create_aqua_vm( + set_variable_call_service(serde_json::json!(false).to_string()), + local_peer_id, + ); + + let local_peer_id_2 = "local_peer_id_2"; + + let script = format!( + r#" + (xor + (seq + (seq + (call "{0}" ("getDataSrv" "condition") [] condition) + (call "{0}" ("getDataSrv" "relay") [] relay) + ) + (xor + (match condition true + (call "{0}" ("println" "print") ["it is true"]) + ) + (call "{1}" ("println" "print") ["it is false"]) + ) + ) + (call "{0}" ("errorHandlingSrv" "error") [%last_error%]) + ) + "#, + local_peer_id, local_peer_id_2 + ); + + let res = call_vm!(vm, "", script, "", ""); + let mut trace: ExecutionTrace = + serde_json::from_slice(&res.data).expect("the interpreter should provide correct trace"); + + let expected_executed_call_result = Call(RequestSentBy(local_peer_id.to_string())); + assert_eq!(res.ret_code, 0); + assert_eq!(trace.pop_back().unwrap(), expected_executed_call_result); + } } diff --git a/interpreter-lib/src/execution/air/mismatch.rs b/interpreter-lib/src/execution/air/mismatch.rs index ceac681d..2445d53f 100644 --- a/interpreter-lib/src/execution/air/mismatch.rs +++ b/interpreter-lib/src/execution/air/mismatch.rs @@ -49,6 +49,7 @@ mod tests { use aqua_test_utils::call_vm; use aqua_test_utils::create_aqua_vm; use aqua_test_utils::echo_string_call_service; + use aqua_test_utils::set_variable_call_service; use std::rc::Rc; @@ -194,4 +195,47 @@ mod tests { assert_eq!(res.ret_code, 1016); } + + #[test] + fn mismatch_with_two_xors() { + use crate::contexts::execution_trace::CallResult::*; + use crate::contexts::execution_trace::ExecutedState::*; + + let local_peer_id = "local_peer_id"; + let mut vm = create_aqua_vm( + set_variable_call_service(serde_json::json!(false).to_string()), + local_peer_id, + ); + + let local_peer_id_2 = "local_peer_id_2"; + + let script = format!( + r#" + (xor + (seq + (seq + (call "{0}" ("getDataSrv" "condition") [] condition) + (call "{0}" ("getDataSrv" "relay") [] relay) + ) + (xor + (mismatch condition true + (call "{1}" ("println" "print") ["it is false"]) + ) + (call "{0}" ("println" "print") ["it is true"]) + ) + ) + (call "{0}" ("errorHandlingSrv" "error") [%last_error%]) + ) + "#, + local_peer_id, local_peer_id_2 + ); + + let res = call_vm!(vm, "", script, "", ""); + let mut trace: ExecutionTrace = + serde_json::from_slice(&res.data).expect("the interpreter should provide correct trace"); + + let expected_executed_call_result = Call(RequestSentBy(local_peer_id.to_string())); + assert_eq!(res.ret_code, 0); + assert_eq!(trace.pop_back().unwrap(), expected_executed_call_result); + } } diff --git a/interpreter-lib/src/execution/air/xor.rs b/interpreter-lib/src/execution/air/xor.rs index 663639a1..e8571a2f 100644 --- a/interpreter-lib/src/execution/air/xor.rs +++ b/interpreter-lib/src/execution/air/xor.rs @@ -31,7 +31,7 @@ impl<'i> super::ExecutableInstruction<'i> for Xor<'i> { Err(e) if is_catchable_by_xor(&e) => { exec_ctx.subtree_complete = true; exec_ctx.last_error_could_be_set = true; - log::warn!("xor caught an error: {}", e); + print_xor_log(&e); self.1.execute(exec_ctx, trace_ctx) } @@ -46,6 +46,17 @@ fn is_catchable_by_xor(exec_error: &ExecutionError) -> bool { !matches!(exec_error, ExecutionError::InvalidExecutedState(..)) } +fn print_xor_log(e: &ExecutionError) { + match e { + // These errors actually aren't real errors, but a way to bubble execution up from match + // to a corresponding xor. They'll become errors iff there is no such xor and execution is + // bubble up until the very beginning of current subtree. So the error message shouldn't + // be print out in order not to confuse users. + ExecutionError::MatchWithoutXorError | ExecutionError::MismatchWithoutXorError => {} + e => log::warn!("xor caught an error: {}", e), + } +} + #[cfg(test)] mod tests { use crate::contexts::execution_trace::ExecutionTrace;