mirror of
https://github.com/fluencelabs/aquavm
synced 2025-06-27 21:51:34 +00:00
make match work with init_peer_id (#96)
This commit is contained in:
4
Cargo.lock
generated
4
Cargo.lock
generated
@ -747,9 +747,9 @@ checksum = "37ab347416e802de484e4d03c7316c48f1ecb56574dfd4a46a80f173ce1de04d"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "fluence"
|
name = "fluence"
|
||||||
version = "0.6.1"
|
version = "0.6.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "54b2f78a2432dcd10e7113f9f08aac004373dee610a3b4ca692d5b12a3e7c867"
|
checksum = "5b731bd4a69a3945186f2ff96ff753908939c8a2debd60e1f0e8edb6a28757f8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"fce-timestamp-macro",
|
"fce-timestamp-macro",
|
||||||
"fluence-sdk-macro",
|
"fluence-sdk-macro",
|
||||||
|
@ -128,6 +128,7 @@ Iterable: IterableValue<'input> = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Matchable: MatchableValue<'input> = {
|
Matchable: MatchableValue<'input> = {
|
||||||
|
InitPeerId => MatchableValue::InitPeerId,
|
||||||
<v:Alphanumeric> => MatchableValue::Variable(Variable::Scalar(v)),
|
<v:Alphanumeric> => MatchableValue::Variable(Variable::Scalar(v)),
|
||||||
<v:Stream> => MatchableValue::Variable(Variable::Stream(v)),
|
<v:Stream> => MatchableValue::Variable(Variable::Stream(v)),
|
||||||
<s:Literal> => MatchableValue::Literal(s),
|
<s:Literal> => MatchableValue::Literal(s),
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -98,6 +98,7 @@ pub enum IterableValue<'i> {
|
|||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
|
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
|
||||||
pub enum MatchableValue<'i> {
|
pub enum MatchableValue<'i> {
|
||||||
|
InitPeerId,
|
||||||
Literal(&'i str),
|
Literal(&'i str),
|
||||||
Number(Number),
|
Number(Number),
|
||||||
Boolean(bool),
|
Boolean(bool),
|
||||||
|
@ -86,6 +86,7 @@ impl fmt::Display for MatchableValue<'_> {
|
|||||||
use MatchableValue::*;
|
use MatchableValue::*;
|
||||||
|
|
||||||
match self {
|
match self {
|
||||||
|
InitPeerId => write!(f, "%init_peer_id%"),
|
||||||
Literal(str) => write!(f, r#""{}""#, str),
|
Literal(str) => write!(f, r#""{}""#, str),
|
||||||
Number(number) => write!(f, "{}", number),
|
Number(number) => write!(f, "{}", number),
|
||||||
Boolean(bool) => write!(f, "{}", bool),
|
Boolean(bool) => write!(f, "{}", bool),
|
||||||
|
@ -358,6 +358,21 @@ fn parse_match() {
|
|||||||
assert_eq!(instruction, expected);
|
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]
|
#[test]
|
||||||
fn parse_mismatch() {
|
fn parse_mismatch() {
|
||||||
use ast::MatchableValue::Variable;
|
use ast::MatchableValue::Variable;
|
||||||
|
@ -192,8 +192,10 @@ impl<'i> VariableValidator<'i> {
|
|||||||
|
|
||||||
fn met_matchable(&mut self, matchable: &MatchableValue<'i>, span: Span) {
|
fn met_matchable(&mut self, matchable: &MatchableValue<'i>, span: Span) {
|
||||||
match matchable {
|
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::Variable(variable) => self.met_variable(variable, span),
|
||||||
MatchableValue::JsonPath { variable, .. } => self.met_variable(variable, span),
|
MatchableValue::JsonPath { variable, .. } => self.met_variable(variable, span),
|
||||||
}
|
}
|
||||||
|
@ -31,8 +31,19 @@ pub(crate) fn are_matchable_eq<'ctx>(
|
|||||||
use MatchableValue::*;
|
use MatchableValue::*;
|
||||||
|
|
||||||
match (left, right) {
|
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)),
|
(Literal(value), matchable) => compare_matchable(matchable, exec_ctx, make_string_comparator(value)),
|
||||||
(matchable, Literal(value)) => 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::*;
|
use MatchableValue::*;
|
||||||
|
|
||||||
match matchable {
|
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) => {
|
Literal(str) => {
|
||||||
let jvalue = str.to_string().into();
|
let jvalue = str.to_string().into();
|
||||||
Ok(comparator(Cow::Owned(jvalue)))
|
Ok(comparator(Cow::Owned(jvalue)))
|
||||||
|
@ -163,6 +163,41 @@ mod tests {
|
|||||||
assert_eq!(actual_trace[1], expected_executed_call_result);
|
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]
|
#[test]
|
||||||
fn match_with_equal_numbers() {
|
fn match_with_equal_numbers() {
|
||||||
let local_peer_id = "local_peer_id";
|
let local_peer_id = "local_peer_id";
|
||||||
|
@ -16,7 +16,7 @@ path = "src/fce.rs"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
interpreter-lib = { path = "../interpreter-lib" }
|
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
|
# Keep 0.2.65 until this is resolved https://github.com/rustwasm/wasm-pack/issues/886
|
||||||
wasm-bindgen = "=0.2.65"
|
wasm-bindgen = "=0.2.65"
|
||||||
|
Reference in New Issue
Block a user