fix behaviour with compare values after applying lambda (#166)

This commit is contained in:
Mike Voronov 2021-11-04 14:47:18 +03:00 committed by GitHub
parent 18f4c0036f
commit 653f42167a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 3 deletions

View File

@ -115,10 +115,14 @@ fn compare_matchable<'ctx>(
let jvaluable = resolve_ast_variable(&vl.variable, exec_ctx)?; let jvaluable = resolve_ast_variable(&vl.variable, exec_ctx)?;
let jvalues = jvaluable.apply_lambda(&vl.lambda)?; let jvalues = jvaluable.apply_lambda(&vl.lambda)?;
let jvalue = jvalues.into_iter().cloned().collect::<Vec<_>>(); // TODO: it's known that apply_lambda always returns array with one value that is
let jvalue = JValue::Array(jvalue); // intended to support multi-return in the future, this check is needed just in
// case and should be refactored after introducing boxed values
if jvalues.len() != 1 {
return Ok(false);
}
Ok(comparator(Cow::Owned(jvalue))) Ok(comparator(Cow::Borrowed(jvalues[0])))
} }
} }
} }

View File

@ -234,3 +234,40 @@ fn match_with_two_xors() {
assert_eq!(actual_trace.pop().unwrap(), expected_executed_call_result); assert_eq!(actual_trace.pop().unwrap(), expected_executed_call_result);
} }
// https://github.com/fluencelabs/aquavm/issues/165
#[test]
fn issue_165() {
let result_setter_peer_id = "result_setter_peer_id";
let mut result_setter = create_avm(
set_variable_call_service(serde_json::json!({"success": true})),
result_setter_peer_id,
);
let echo_peer_id = "echo_peer_id";
let mut echo_peer = create_avm(echo_call_service(), echo_peer_id);
let script = format!(
r#"
(seq
(call "{0}" ("" "") ["set_result"] result)
(seq
(xor
(match result.$.success! true
(ap 1 $results)
)
(ap 2 $results)
)
(call "{1}" ("callbackSrv" "response") [$results.$.[0]!])
)
)
"#,
result_setter_peer_id, echo_peer_id
);
let setter_result = checked_call_vm!(result_setter, "", &script, "", "");
let echo_result = checked_call_vm!(echo_peer, "", &script, "", setter_result.data);
let trace = trace_from_result(&echo_result);
assert_eq!(trace.last().unwrap(), &executed_state::scalar(json!(1)));
}