make match work with init_peer_id (#96)

This commit is contained in:
vms 2021-04-29 18:26:13 +03:00 committed by GitHub
parent 255feedb60
commit 2a408d4ee4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 471 additions and 351 deletions

4
Cargo.lock generated
View File

@ -747,9 +747,9 @@ checksum = "37ab347416e802de484e4d03c7316c48f1ecb56574dfd4a46a80f173ce1de04d"
[[package]]
name = "fluence"
version = "0.6.1"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54b2f78a2432dcd10e7113f9f08aac004373dee610a3b4ca692d5b12a3e7c867"
checksum = "5b731bd4a69a3945186f2ff96ff753908939c8a2debd60e1f0e8edb6a28757f8"
dependencies = [
"fce-timestamp-macro",
"fluence-sdk-macro",

View File

@ -128,6 +128,7 @@ Iterable: IterableValue<'input> = {
}
Matchable: MatchableValue<'input> = {
InitPeerId => MatchableValue::InitPeerId,
<v:Alphanumeric> => MatchableValue::Variable(Variable::Scalar(v)),
<v:Stream> => MatchableValue::Variable(Variable::Stream(v)),
<s:Literal> => MatchableValue::Literal(s),

File diff suppressed because it is too large Load Diff

View File

@ -98,6 +98,7 @@ pub enum IterableValue<'i> {
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub enum MatchableValue<'i> {
InitPeerId,
Literal(&'i str),
Number(Number),
Boolean(bool),

View File

@ -86,6 +86,7 @@ impl fmt::Display for MatchableValue<'_> {
use MatchableValue::*;
match self {
InitPeerId => write!(f, "%init_peer_id%"),
Literal(str) => write!(f, r#""{}""#, str),
Number(number) => write!(f, "{}", number),
Boolean(bool) => write!(f, "{}", bool),

View File

@ -358,6 +358,21 @@ fn parse_match() {
assert_eq!(instruction, expected);
}
#[test]
fn parse_match_with_init_peer_id() {
use ast::MatchableValue::InitPeerId;
use ast::MatchableValue::Variable;
let source_code = r#"
(match v1 %init_peer_id%
(null)
)
"#;
let instruction = parse(&source_code);
let expected = match_(Variable(Scalar("v1")), InitPeerId, null());
assert_eq!(instruction, expected);
}
#[test]
fn parse_mismatch() {
use ast::MatchableValue::Variable;

View File

@ -192,8 +192,10 @@ impl<'i> VariableValidator<'i> {
fn met_matchable(&mut self, matchable: &MatchableValue<'i>, span: Span) {
match matchable {
MatchableValue::Number(_) | MatchableValue::Boolean(_) | MatchableValue::Literal(_) => {
}
MatchableValue::InitPeerId
| MatchableValue::Number(_)
| MatchableValue::Boolean(_)
| MatchableValue::Literal(_) => {}
MatchableValue::Variable(variable) => self.met_variable(variable, span),
MatchableValue::JsonPath { variable, .. } => self.met_variable(variable, span),
}

View File

@ -31,8 +31,19 @@ pub(crate) fn are_matchable_eq<'ctx>(
use MatchableValue::*;
match (left, right) {
(Literal(left_name), Literal(right_name)) => Ok(left_name == right_name),
(InitPeerId, InitPeerId) => Ok(true),
(InitPeerId, matchable) => compare_matchable(
matchable,
exec_ctx,
make_string_comparator(exec_ctx.init_peer_id.as_str()),
),
(matchable, InitPeerId) => compare_matchable(
matchable,
exec_ctx,
make_string_comparator(exec_ctx.init_peer_id.as_str()),
),
(Literal(left_name), Literal(right_name)) => Ok(left_name == right_name),
(Literal(value), matchable) => compare_matchable(matchable, exec_ctx, make_string_comparator(value)),
(matchable, Literal(value)) => compare_matchable(matchable, exec_ctx, make_string_comparator(value)),
@ -95,6 +106,11 @@ fn compare_matchable<'ctx>(
use MatchableValue::*;
match matchable {
InitPeerId => {
let init_peer_id = exec_ctx.init_peer_id.clone();
let jvalue = init_peer_id.into();
Ok(comparator(Cow::Owned(jvalue)))
}
Literal(str) => {
let jvalue = str.to_string().into();
Ok(comparator(Cow::Owned(jvalue)))

View File

@ -163,6 +163,41 @@ mod tests {
assert_eq!(actual_trace[1], expected_executed_call_result);
}
#[test]
fn match_with_init_peer_id() {
use crate::contexts::execution_trace::CallResult::*;
use crate::contexts::execution_trace::ExecutedState::*;
let set_variable_peer_id = "set_variable_peer_id";
let mut set_variable_vm = create_aqua_vm(echo_string_call_service(), set_variable_peer_id);
let local_peer_id = "local_peer_id";
let mut vm = create_aqua_vm(echo_string_call_service(), local_peer_id);
let script = format!(
r#"
(seq
(call "{0}" ("" "") ["{1}"] value_1)
(xor
(match value_1 %init_peer_id%
(call "{1}" ("service_id_2" "local_fn_name") ["result_1"] result_1)
)
(call "{1}" ("service_id_2" "local_fn_name") ["result_2"] result_2)
)
)"#,
set_variable_peer_id, local_peer_id
);
let res = call_vm!(set_variable_vm, local_peer_id, script.clone(), "", "");
let res = call_vm!(vm, local_peer_id, script, "", res.data);
let actual_trace: ExecutionTrace = serde_json::from_slice(&res.data).expect("should be valid json");
let expected_executed_call_result = Call(Executed(Rc::new(JValue::String(String::from("result_1")))));
assert_eq!(actual_trace.len(), 2);
assert_eq!(actual_trace[1], expected_executed_call_result);
}
#[test]
fn match_with_equal_numbers() {
let local_peer_id = "local_peer_id";

View File

@ -16,7 +16,7 @@ path = "src/fce.rs"
[dependencies]
interpreter-lib = { path = "../interpreter-lib" }
fluence = { version = "0.6.1", features = ["logger"] }
fluence = { version = "0.6.2", features = ["logger"] }
# Keep 0.2.65 until this is resolved https://github.com/rustwasm/wasm-pack/issues/886
wasm-bindgen = "=0.2.65"