mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
add tests for state variables accessors. normal and constant
fixed the issue with accessors for constant state variables
This commit is contained in:
parent
289b814670
commit
1af8ff0121
@ -554,8 +554,11 @@ void VariableDeclaration::checkTypeRequirements()
|
||||
{
|
||||
if (!dynamic_cast<ContractDefinition const*>(getScope()))
|
||||
BOOST_THROW_EXCEPTION(createTypeError("Illegal use of \"constant\" specifier."));
|
||||
if ((m_type && !m_type->isValueType()) || !m_value)
|
||||
BOOST_THROW_EXCEPTION(createTypeError("Unitialized \"constant\" variable."));
|
||||
if (!m_value)
|
||||
BOOST_THROW_EXCEPTION(createTypeError("Uninitialized \"constant\" variable."));
|
||||
else if (!m_type->isValueType())
|
||||
// TODO: const is implemented only for uint, bytesXX and enums types.
|
||||
BOOST_THROW_EXCEPTION(createTypeError("Illegal use of \"constant\" specifier. \"constant\" is not implemented for this type yet."));
|
||||
}
|
||||
if (m_type)
|
||||
{
|
||||
|
@ -540,7 +540,7 @@ public:
|
||||
void setType(std::shared_ptr<Type const> const& _type) { m_type = _type; }
|
||||
|
||||
virtual bool isLValue() const override;
|
||||
virtual bool isPartOfExternalInterface() const override { return isPublic() && !m_isConstant; }
|
||||
virtual bool isPartOfExternalInterface() const override { return isPublic(); }
|
||||
|
||||
void checkTypeRequirements();
|
||||
bool isLocalVariable() const { return !!dynamic_cast<FunctionDefinition const*>(getScope()); }
|
||||
|
@ -390,7 +390,10 @@ bool Compiler::visit(VariableDeclaration const& _variableDeclaration)
|
||||
m_breakTags.clear();
|
||||
m_continueTags.clear();
|
||||
|
||||
ExpressionCompiler(m_context, m_optimize).appendStateVariableAccessor(_variableDeclaration);
|
||||
if (_variableDeclaration.isConstant())
|
||||
ExpressionCompiler(m_context, m_optimize).appendConstStateVariableAccessor(_variableDeclaration);
|
||||
else
|
||||
ExpressionCompiler(m_context, m_optimize).appendStateVariableAccessor(_variableDeclaration);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
@ -67,8 +67,19 @@ void ExpressionCompiler::appendStateVariableInitialization(VariableDeclaration c
|
||||
StorageItem(m_context, _varDecl).storeValue(*type, _varDecl.getLocation(), true);
|
||||
}
|
||||
|
||||
void ExpressionCompiler::appendConstStateVariableAccessor(VariableDeclaration const& _varDecl)
|
||||
{
|
||||
solAssert(_varDecl.isConstant(), "");
|
||||
_varDecl.getValue()->accept(*this);
|
||||
|
||||
// append return
|
||||
m_context << eth::dupInstruction(_varDecl.getType()->getSizeOnStack() + 1);
|
||||
m_context.appendJump(eth::AssemblyItem::JumpType::OutOfFunction);
|
||||
}
|
||||
|
||||
void ExpressionCompiler::appendStateVariableAccessor(VariableDeclaration const& _varDecl)
|
||||
{
|
||||
solAssert(!_varDecl.isConstant(), "");
|
||||
CompilerContext::LocationSetter locationSetter(m_context, _varDecl);
|
||||
FunctionType accessorType(_varDecl);
|
||||
|
||||
|
@ -67,6 +67,9 @@ public:
|
||||
/// Appends code for a State Variable accessor function
|
||||
void appendStateVariableAccessor(VariableDeclaration const& _varDecl);
|
||||
|
||||
/// Appends code for a Constant State Variable accessor function
|
||||
void appendConstStateVariableAccessor(const VariableDeclaration& _varDecl);
|
||||
|
||||
private:
|
||||
virtual bool visit(Assignment const& _assignment) override;
|
||||
virtual bool visit(UnaryOperation const& _unaryOperation) override;
|
||||
|
@ -5161,6 +5161,27 @@ BOOST_AUTO_TEST_CASE(string_as_mapping_key)
|
||||
) == encodeArgs(u256(7 + i)));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(accessor_for_state_variable)
|
||||
{
|
||||
char const* sourceCode = R"(
|
||||
contract Lotto{
|
||||
uint public ticketPrice = 500;
|
||||
})";
|
||||
|
||||
compileAndRun(sourceCode);
|
||||
BOOST_CHECK(callContractFunction("ticketPrice()") == encodeArgs(u256(500)));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(accessor_for_const_state_variable)
|
||||
{
|
||||
char const* sourceCode = R"(
|
||||
contract Lotto{
|
||||
uint constant public ticketPrice = 555;
|
||||
})";
|
||||
|
||||
compileAndRun(sourceCode);
|
||||
BOOST_CHECK(callContractFunction("ticketPrice()") == encodeArgs(u256(555)));
|
||||
}
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user