mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Compilation of function modifiers.
This commit is contained in:
parent
c86a46b84d
commit
67073948af
@ -1653,22 +1653,72 @@ BOOST_AUTO_TEST_CASE(constructor_argument_overriding)
|
|||||||
BOOST_AUTO_TEST_CASE(function_modifier)
|
BOOST_AUTO_TEST_CASE(function_modifier)
|
||||||
{
|
{
|
||||||
char const* sourceCode = R"(
|
char const* sourceCode = R"(
|
||||||
contract BaseBase {
|
contract C {
|
||||||
uint m_a;
|
function getOne() nonFree returns (uint r) { return 1; }
|
||||||
function BaseBase(uint a) {
|
modifier nonFree { if (msg.value > 0) _ }
|
||||||
m_a = a;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
contract Base is BaseBase(2) { }
|
|
||||||
contract Derived is Base, BaseBase(3) {
|
|
||||||
function getA() returns (uint r) { return m_a; }
|
|
||||||
}
|
}
|
||||||
)";
|
)";
|
||||||
compileAndRun(sourceCode, 0, "Derived");
|
compileAndRun(sourceCode);
|
||||||
BOOST_CHECK(callContractFunction("getA()") == encodeArgs(3));
|
BOOST_CHECK(callContractFunction("getOne()") == encodeArgs(0));
|
||||||
|
BOOST_CHECK(callContractFunctionWithValue("getOne()", 1) == encodeArgs(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(function_modifier_local_variables)
|
||||||
|
{
|
||||||
|
char const* sourceCode = R"(
|
||||||
|
contract C {
|
||||||
|
modifier mod1 { var a = 1; var b = 2; _ }
|
||||||
|
modifier mod2(bool a) { if (a) return; else _ }
|
||||||
|
function f(bool a) mod1 mod2(a) returns (uint r) { return 3; }
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
compileAndRun(sourceCode);
|
||||||
|
BOOST_CHECK(callContractFunction("f(bool)", true) == encodeArgs(0));
|
||||||
|
BOOST_CHECK(callContractFunction("f(bool)", false) == encodeArgs(3));
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(function_modifier_loop)
|
||||||
|
{
|
||||||
|
char const* sourceCode = R"(
|
||||||
|
contract C {
|
||||||
|
modifier repeat(uint count) { for (var i = 0; i < count; ++i) _ }
|
||||||
|
function f() repeat(10) returns (uint r) { r += 1; }
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
compileAndRun(sourceCode);
|
||||||
|
BOOST_CHECK(callContractFunction("f()") == encodeArgs(10));
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(function_modifier_multi_invocation)
|
||||||
|
{
|
||||||
|
char const* sourceCode = R"(
|
||||||
|
contract C {
|
||||||
|
modifier repeat(bool twice) { if (twice) _ _ }
|
||||||
|
function f(bool twice) repeat(twice) returns (uint r) { r += 1; }
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
compileAndRun(sourceCode);
|
||||||
|
BOOST_CHECK(callContractFunction("f(bool)", false) == encodeArgs(1));
|
||||||
|
BOOST_CHECK(callContractFunction("f(bool)", true) == encodeArgs(2));
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(function_modifier_multi_with_return)
|
||||||
|
{
|
||||||
|
// Here, the explicit return prevents the second execution
|
||||||
|
char const* sourceCode = R"(
|
||||||
|
contract C {
|
||||||
|
modifier repeat(bool twice) { if (twice) _ _ }
|
||||||
|
function f(bool twice) repeat(twice) returns (uint r) { r += 1; return r; }
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
compileAndRun(sourceCode);
|
||||||
|
BOOST_CHECK(callContractFunction("f(bool)", false) == encodeArgs(1));
|
||||||
|
BOOST_CHECK(callContractFunction("f(bool)", true) == encodeArgs(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// modifier overriding
|
// modifier overriding
|
||||||
|
// functions called by modifiers used by constructor need to be pulled into the creation context
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE_END()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
|
||||||
|
@ -118,8 +118,11 @@ bytes compileFirstExpression(const string& _sourceCode, vector<vector<string>> _
|
|||||||
CompilerContext context;
|
CompilerContext context;
|
||||||
for (vector<string> const& function: _functions)
|
for (vector<string> const& function: _functions)
|
||||||
context.addFunction(dynamic_cast<FunctionDefinition const&>(resolveDeclaration(function, resolver)));
|
context.addFunction(dynamic_cast<FunctionDefinition const&>(resolveDeclaration(function, resolver)));
|
||||||
|
unsigned parametersSize = _localVariables.size(); // assume they are all one slot on the stack
|
||||||
|
context.adjustStackOffset(parametersSize);
|
||||||
for (vector<string> const& variable: _localVariables)
|
for (vector<string> const& variable: _localVariables)
|
||||||
context.addVariable(dynamic_cast<VariableDeclaration const&>(resolveDeclaration(variable, resolver)));
|
context.addVariable(dynamic_cast<VariableDeclaration const&>(resolveDeclaration(variable, resolver)),
|
||||||
|
parametersSize--);
|
||||||
|
|
||||||
ExpressionCompiler::compileExpression(context, *extractor.getExpression());
|
ExpressionCompiler::compileExpression(context, *extractor.getExpression());
|
||||||
|
|
||||||
|
@ -573,6 +573,17 @@ BOOST_AUTO_TEST_CASE(function_overrides_modifier)
|
|||||||
BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
|
BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(modifier_returns_value)
|
||||||
|
{
|
||||||
|
char const* text = R"(
|
||||||
|
contract A {
|
||||||
|
function f(uint a) mod(2) returns (uint r) {}
|
||||||
|
modifier mod(uint a) { return 7; }
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
|
||||||
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE_END()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user