Skip to content

Commit

Permalink
Add support for array concatenation
Browse files Browse the repository at this point in the history
  • Loading branch information
danhper committed Aug 7, 2024
1 parent 4999ae1 commit ed14187
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Features

* Decode logs returned by `Transaction.getReceipt()` when available in ABI
* Add support for array concatenation

### Other changes

Expand Down
28 changes: 28 additions & 0 deletions src/interpreter/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit ed14187

Please sign in to comment.