[Sol->Yul] Implementing dynamic array push for arrays of structs.

This commit is contained in:
Djordje Mijovic
2021-01-11 17:49:42 +01:00
committed by chriseth
parent b06936b11c
commit 85b8325f0b
7 changed files with 104 additions and 50 deletions
@@ -0,0 +1,16 @@
contract C {
uint8 b = 23;
uint120[][] s;
uint8 a = 17;
function f(uint120[] calldata c) public returns(uint120) {
s.push(c);
assert(s.length == 1);
assert(s[0].length == c.length);
assert(s[0].length > 0);
return s[0][0];
}
}
// ====
// compileViaYul: also
// ----
// f(uint120[]): 0x20, 3, 1, 2, 3 -> 1
@@ -0,0 +1,19 @@
contract C {
uint8 b = 23;
uint120[][] s;
uint8 a = 17;
function f() public returns(uint120) {
delete s;
uint120[] memory m = new uint120[](3);
m[0] = 1;
s.push(m);
assert(s.length == 1);
assert(s[0].length == m.length);
assert(s[0].length > 0);
return s[0][0];
}
}
// ====
// compileViaYul: also
// ----
// f() -> 1
@@ -18,6 +18,7 @@ contract c {
return (data[0].a, data[0].b, data[0].c[2], data[0].d[2]);
}
}
// ====
// compileViaYul: also
// ----
// test() -> 2, 3, 4, 5
@@ -0,0 +1,20 @@
pragma abicoder v2;
contract c {
struct S {
uint16 a;
uint16 b;
uint16[3] c;
uint16[] d;
}
S[] data;
function test(S calldata c) public returns (uint16, uint16, uint16, uint16) {
data.push(c);
return (data[0].a, data[0].b, data[0].c[2], data[0].d[2]);
}
}
// ====
// compileViaYul: also
// ----
// test((uint16, uint16, uint16[3], uint16[])): 0x20, 2, 3, 0, 0, 4, 0xC0, 4, 0, 0, 5, 0, 0 -> 2, 3, 4, 5