From 401d8c06fa92d31a3bede1117d16c196e6896857 Mon Sep 17 00:00:00 2001 From: Mike Voronov Date: Wed, 29 Dec 2021 20:01:49 +0300 Subject: [PATCH] Fix bug with empty arrays in match (#207) --- .../air/compare_matchable/comparator.rs | 6 ++- air/tests/test_module/issues/issue_206.rs | 50 +++++++++++++++++++ air/tests/test_module/issues/mod.rs | 1 + 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 air/tests/test_module/issues/issue_206.rs diff --git a/air/src/execution_step/air/compare_matchable/comparator.rs b/air/src/execution_step/air/compare_matchable/comparator.rs index 80a35e19..5cc12b00 100644 --- a/air/src/execution_step/air/compare_matchable/comparator.rs +++ b/air/src/execution_step/air/compare_matchable/comparator.rs @@ -47,6 +47,11 @@ pub(crate) fn are_matchable_eq<'ctx>( compare_matchable(matchable, exec_ctx, make_string_comparator(value)) } + (EmptyArray, EmptyArray) => Ok(true), + (EmptyArray, matchable) | (matchable, EmptyArray) => { + compare_matchable(matchable, exec_ctx, make_object_comparator(JValue::Array(vec![]))) + } + (Boolean(left_boolean), Boolean(right_boolean)) => Ok(left_boolean == right_boolean), (Boolean(value), matchable) | (matchable, Boolean(value)) => { compare_matchable(matchable, exec_ctx, make_object_comparator((*value).into())) @@ -63,7 +68,6 @@ pub(crate) fn are_matchable_eq<'ctx>( Ok(left_value == right_value) } - _ => Ok(false), } } diff --git a/air/tests/test_module/issues/issue_206.rs b/air/tests/test_module/issues/issue_206.rs new file mode 100644 index 00000000..eab13763 --- /dev/null +++ b/air/tests/test_module/issues/issue_206.rs @@ -0,0 +1,50 @@ +/* + * Copyright 2021 Fluence Labs Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +use air_test_utils::prelude::*; + +use fstrings::f; +use fstrings::format_args_f; + +#[test] +// test for github.com/fluencelabs/aquavm/issues/206 +fn issue_206() { + let peer_1_id = "peer_1_id"; + let mut peer_1 = create_avm(echo_call_service(), peer_1_id); + + let script = f!(r#" + (new $result + (seq + (xor + (match $result [] + (xor + (ap "is nil" $result) + (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1]) + ) + ) + (ap "not" $result) + ) + (call %init_peer_id% ("op" "identity") [$result] result-fix) + ) + ) + "#); + + let result = checked_call_vm!(peer_1, peer_1_id, &script, "", ""); + + let actual_trace = trace_from_result(&result); + let expected_trace = vec![executed_state::ap(Some(0)), executed_state::scalar(json!(["is nil"]))]; + assert_eq!(actual_trace, expected_trace); +} diff --git a/air/tests/test_module/issues/mod.rs b/air/tests/test_module/issues/mod.rs index db591577..9fe3d294 100644 --- a/air/tests/test_module/issues/mod.rs +++ b/air/tests/test_module/issues/mod.rs @@ -19,3 +19,4 @@ mod issue_173; mod issue_177; mod issue_178; mod issue_180; +mod issue_206;