Browse Source

Global variable "now" (alias for block.timestamp).

cl-refactor
chriseth 10 years ago
parent
commit
4f852f473e
  1. 12
      libsolidity/ExpressionCompiler.cpp
  2. 1
      libsolidity/GlobalContext.cpp
  3. 11
      test/SolidityEndToEndTest.cpp

12
libsolidity/ExpressionCompiler.cpp

@ -825,10 +825,20 @@ void ExpressionCompiler::endVisit(Identifier const& _identifier)
Declaration const* declaration = _identifier.getReferencedDeclaration();
if (MagicVariableDeclaration const* magicVar = dynamic_cast<MagicVariableDeclaration const*>(declaration))
{
if (magicVar->getType()->getCategory() == Type::Category::Contract)
switch (magicVar->getType()->getCategory())
{
case Type::Category::Contract:
// "this" or "super"
if (!dynamic_cast<ContractType const&>(*magicVar->getType()).isSuper())
m_context << eth::Instruction::ADDRESS;
break;
case Type::Category::Integer:
// "now"
m_context << eth::Instruction::TIMESTAMP;
break;
default:
break;
}
}
else if (FunctionDefinition const* functionDef = dynamic_cast<FunctionDefinition const*>(declaration))
m_context << m_context.getVirtualFunctionEntryLabel(*functionDef).pushTag();

1
libsolidity/GlobalContext.cpp

@ -37,6 +37,7 @@ GlobalContext::GlobalContext():
m_magicVariables(vector<shared_ptr<MagicVariableDeclaration const>>{make_shared<MagicVariableDeclaration>("block", make_shared<MagicType>(MagicType::Kind::Block)),
make_shared<MagicVariableDeclaration>("msg", make_shared<MagicType>(MagicType::Kind::Message)),
make_shared<MagicVariableDeclaration>("tx", make_shared<MagicType>(MagicType::Kind::Transaction)),
make_shared<MagicVariableDeclaration>("now", make_shared<IntegerType>(256)),
make_shared<MagicVariableDeclaration>("suicide",
make_shared<FunctionType>(strings{"address"}, strings{}, FunctionType::Location::Suicide)),
make_shared<MagicVariableDeclaration>("sha3",

11
test/SolidityEndToEndTest.cpp

@ -1031,6 +1031,17 @@ BOOST_AUTO_TEST_CASE(blockchain)
BOOST_CHECK(callContractFunctionWithValue("someInfo()", 28) == encodeArgs(28, 0, 1));
}
BOOST_AUTO_TEST_CASE(now)
{
char const* sourceCode = "contract test {\n"
" function someInfo() returns (bool success) {\n"
" return block.timestamp == now && now > 0;\n"
" }\n"
"}\n";
compileAndRun(sourceCode);
BOOST_CHECK(callContractFunction("someInfo()") == encodeArgs(true));
}
BOOST_AUTO_TEST_CASE(function_types)
{
char const* sourceCode = "contract test {\n"

Loading…
Cancel
Save