-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tweak logic for better constant expression evaluation (respect file-l…
…evel constants, support more node types and cast operations)
- Loading branch information
1 parent
f467859
commit ac2dfc8
Showing
6 changed files
with
319 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import expect from "expect"; | ||
import { | ||
assert, | ||
ASTReader, | ||
compileSol, | ||
detectCompileErrors, | ||
evalConstantExpr, | ||
Expression, | ||
InferType, | ||
LatestCompilerVersion, | ||
SourceUnit, | ||
Value, | ||
VariableDeclaration, | ||
XPath | ||
} from "../../src"; | ||
|
||
const cases: Array<[string, Array<[string, Value]>]> = [ | ||
[ | ||
"test/samples/solidity/consts/consts.sol", | ||
[ | ||
["//VariableDeclaration[@id=5]", 100n], | ||
["//VariableDeclaration[@id=8]", 15n], | ||
["//VariableDeclaration[@id=13]", 115n], | ||
["//VariableDeclaration[@id=18]", 158n], | ||
["//VariableDeclaration[@id=24]", 158n], | ||
["//VariableDeclaration[@id=31]", false], | ||
["//VariableDeclaration[@id=37]", 158n], | ||
["//VariableDeclaration[@id=44]", 85n], | ||
["//VariableDeclaration[@id=47]", "abcd"], | ||
["//VariableDeclaration[@id=53]", Buffer.from("abcd", "utf-8")], | ||
["//VariableDeclaration[@id=58]", 97n], | ||
["//VariableDeclaration[@id=64]", "abcd"], | ||
["//VariableDeclaration[@id=73]", 30841n], | ||
["//VariableDeclaration[@id=82]", 30841n], | ||
["//VariableDeclaration[@id=88]", 258n] | ||
] | ||
] | ||
]; | ||
|
||
describe("Constant expression evaluator integration test", () => { | ||
for (const [sample, mapping] of cases) { | ||
describe(sample, () => { | ||
let units: SourceUnit[]; | ||
let inference: InferType; | ||
|
||
before(async () => { | ||
const result = await compileSol(sample, "auto"); | ||
|
||
const data = result.data; | ||
const compilerVersion = result.compilerVersion || LatestCompilerVersion; | ||
|
||
const errors = detectCompileErrors(data); | ||
|
||
expect(errors).toHaveLength(0); | ||
|
||
const reader = new ASTReader(); | ||
|
||
units = reader.read(data); | ||
|
||
expect(units.length).toBeGreaterThanOrEqual(1); | ||
|
||
inference = new InferType(compilerVersion); | ||
}); | ||
|
||
for (const [selector, expectation] of mapping) { | ||
let found = false; | ||
|
||
it(`${selector} -> ${expectation}`, () => { | ||
for (const unit of units) { | ||
const results = new XPath(unit).query(selector); | ||
|
||
if (results.length > 0) { | ||
const [expr] = results; | ||
|
||
assert( | ||
expr instanceof Expression || expr instanceof VariableDeclaration, | ||
`Expected selector result to be an {0} or {1} descendant, got {2} instead`, | ||
Expression.name, | ||
VariableDeclaration.name, | ||
expr | ||
); | ||
|
||
found = true; | ||
|
||
expect(evalConstantExpr(expr, inference)).toEqual(expectation); | ||
|
||
break; | ||
} | ||
} | ||
|
||
assert( | ||
found, | ||
`Selector "{0}" not found in source units of sample "{1}"`, | ||
selector, | ||
sample | ||
); | ||
}); | ||
} | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import "./lib_a.sol"; | ||
import "./lib_a.sol" as LibA; | ||
|
||
uint constant SOME_CONST = 100; | ||
uint constant SOME_OTHER = 15; | ||
uint constant SOME_ELSE = SOME_CONST + SOME_OTHER; | ||
uint constant C2 = SOME_ELSE + ANOTHER_CONST; | ||
uint constant C3 = SOME_ELSE + LibA.ANOTHER_CONST; | ||
uint constant C4 = -SOME_CONST; | ||
bool constant C5 = false; | ||
uint constant C6 = C5 ? SOME_ELSE : C3; | ||
uint constant C7 = LibA.ANOTHER_CONST + LibB.AND_ANOTHER_CONST; | ||
// uint constant C8 = LibA.ANOTHER_CONST + LibA.LibB.AND_ANOTHER_CONST; | ||
|
||
string constant FOO = "abcd"; | ||
bytes constant BOO = bytes("abcd"); | ||
bytes1 constant MOO = BOO[0]; | ||
string constant WOO = string(BOO); | ||
|
||
uint16 constant U16S = uint16(bytes2("xy")); | ||
uint16 constant U16B = uint16(bytes2(hex"7879")); | ||
bytes2 constant B2U = bytes2(0x0102); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pragma solidity ^0.7.5; | ||
|
||
import "./lib_b.sol" as LibB; | ||
|
||
uint constant ANOTHER_CONST = LibB.AND_ANOTHER_CONST + 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pragma solidity ^0.7.5; | ||
|
||
uint constant AND_ANOTHER_CONST = 42; |
Oops, something went wrong.