diff --git a/SolidityEndToEndTest.cpp b/SolidityEndToEndTest.cpp index 8bce1788a..c25b8ca5d 100644 --- a/SolidityEndToEndTest.cpp +++ b/SolidityEndToEndTest.cpp @@ -1653,22 +1653,72 @@ BOOST_AUTO_TEST_CASE(constructor_argument_overriding) BOOST_AUTO_TEST_CASE(function_modifier) { char const* sourceCode = R"( - contract BaseBase { - uint m_a; - function BaseBase(uint a) { - m_a = a; - } - } - contract Base is BaseBase(2) { } - contract Derived is Base, BaseBase(3) { - function getA() returns (uint r) { return m_a; } + contract C { + function getOne() nonFree returns (uint r) { return 1; } + modifier nonFree { if (msg.value > 0) _ } } )"; - compileAndRun(sourceCode, 0, "Derived"); - BOOST_CHECK(callContractFunction("getA()") == encodeArgs(3)); + compileAndRun(sourceCode); + 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 +// functions called by modifiers used by constructor need to be pulled into the creation context BOOST_AUTO_TEST_SUITE_END() diff --git a/SolidityExpressionCompiler.cpp b/SolidityExpressionCompiler.cpp index d50dc253e..06c252db3 100644 --- a/SolidityExpressionCompiler.cpp +++ b/SolidityExpressionCompiler.cpp @@ -118,8 +118,11 @@ bytes compileFirstExpression(const string& _sourceCode, vector> _ CompilerContext context; for (vector const& function: _functions) context.addFunction(dynamic_cast(resolveDeclaration(function, resolver))); + unsigned parametersSize = _localVariables.size(); // assume they are all one slot on the stack + context.adjustStackOffset(parametersSize); for (vector const& variable: _localVariables) - context.addVariable(dynamic_cast(resolveDeclaration(variable, resolver))); + context.addVariable(dynamic_cast(resolveDeclaration(variable, resolver)), + parametersSize--); ExpressionCompiler::compileExpression(context, *extractor.getExpression()); diff --git a/SolidityNameAndTypeResolution.cpp b/SolidityNameAndTypeResolution.cpp index c195539f0..69559b4a1 100644 --- a/SolidityNameAndTypeResolution.cpp +++ b/SolidityNameAndTypeResolution.cpp @@ -573,6 +573,17 @@ BOOST_AUTO_TEST_CASE(function_overrides_modifier) 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() }