From ed14187aad1dae8e509b5691babebe1e391397e2 Mon Sep 17 00:00:00 2001 From: Daniel Perez Date: Wed, 7 Aug 2024 12:01:53 +0100 Subject: [PATCH] Add support for array concatenation --- CHANGELOG.md | 1 + src/interpreter/value.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b34cca..245cf35 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Features * Decode logs returned by `Transaction.getReceipt()` when available in ABI +* Add support for array concatenation ### Other changes diff --git a/src/interpreter/value.rs b/src/interpreter/value.rs index d41972d..4e819d4 100644 --- a/src/interpreter/value.rs +++ b/src/interpreter/value.rs @@ -601,6 +601,11 @@ impl Add for Value { Ok(Value::Int(I256::from_raw(a) + b, s1.max(s2))) } (Value::Str(a), Value::Str(b)) => return Ok(Value::Str(a + &b)), + (Value::Array(a, t1), Value::Array(b, t2)) if t1 == t2 => { + let mut new_arr = a.clone(); + new_arr.extend(b); + return Ok(Value::Array(new_arr, t1)); + } _ => bail!(error_msg), } .and_then(Value::validate_int) @@ -691,6 +696,29 @@ impl Rem for Value { mod tests { use super::*; + #[test] + fn test_add() { + assert_eq!( + (Value::from(1u64) + Value::from(2u64)).unwrap(), + Value::from(3u64) + ); + + assert_eq!( + (Value::from("foo") + Value::from("bar")).unwrap(), + Value::from("foobar") + ); + + assert_eq!( + (Value::Array(vec![Value::from(1u64)], Box::new(Type::Uint(256))) + + Value::Array(vec![Value::from(2u64)], Box::new(Type::Uint(256)))) + .unwrap(), + Value::Array( + vec![Value::from(1u64), Value::from(2u64)], + Box::new(Type::Uint(256)) + ) + ); + } + #[test] fn test_value_from_hex() { let addr = Address::from_hex("0x7a250d5630b4cf539739df2c5dacb4c659f2488d").unwrap();