-
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.
Improve constant expression evaluator (#225)
* Tweak logic for better constant expression evaluation (respect file-level constants, support more node types and cast operations) * Tweak integration test to refer to variable names for clarity * Handle string literals with byte sequence in constant expression evaluator (#227) * Address review remarks: check for OOB when index accessing into a fixed bytes, clamp int to type on cast to bytes.
- Loading branch information
1 parent
f467859
commit 883a401
Showing
6 changed files
with
403 additions
and
40 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,108 @@ | ||
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[@name='SOME_CONST']", 100n], | ||
["//VariableDeclaration[@name='SOME_OTHER']", 15n], | ||
["//VariableDeclaration[@name='SOME_ELSE']", 115n], | ||
["//VariableDeclaration[@name='C2']", 158n], | ||
["//VariableDeclaration[@name='C3']", 158n], | ||
[ | ||
"//VariableDeclaration[@name='C4']", | ||
115792089237316195423570985008687907853269984665640564039457584007913129639836n | ||
], | ||
["//VariableDeclaration[@name='C5']", false], | ||
["//VariableDeclaration[@name='C6']", 158n], | ||
["//VariableDeclaration[@name='C7']", 85n], | ||
|
||
["//VariableDeclaration[@name='FOO']", "abcd"], | ||
["//VariableDeclaration[@name='BOO']", Buffer.from("abcd", "utf-8")], | ||
["//VariableDeclaration[@name='MOO']", 97n], | ||
["//VariableDeclaration[@name='WOO']", "abcd"], | ||
|
||
["//VariableDeclaration[@name='U16S']", 30841n], | ||
["//VariableDeclaration[@name='U16B']", 30841n], | ||
["//VariableDeclaration[@name='B2U']", 258n], | ||
["//VariableDeclaration[@name='NON_UTF8_SEQ']", Buffer.from("7532eaac", "hex")] | ||
] | ||
] | ||
]; | ||
|
||
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 | ||
); | ||
}); | ||
} | ||
}); | ||
} | ||
}); |
Oops, something went wrong.