Browse Source

Decoding for constructor.

cl-refactor
chriseth 9 years ago
parent
commit
9e36e8c4e1
  1. 15
      libsolidity/Compiler.cpp
  2. 2
      libsolidity/CompilerUtils.cpp
  3. 28
      libsolidity/Types.cpp
  4. 9
      libsolidity/Types.h
  5. 40
      test/libsolidity/SolidityEndToEndTest.cpp
  6. 13
      test/libsolidity/SolidityNameAndTypeResolution.cpp

15
libsolidity/Compiler.cpp

@ -163,14 +163,14 @@ void Compiler::appendConstructor(FunctionDefinition const& _constructor)
{
CompilerContext::LocationSetter locationSetter(m_context, _constructor);
// copy constructor arguments from code to memory and then to stack, they are supplied after the actual program
unsigned argumentSize = 0;
for (ASTPointer<VariableDeclaration> const& var: _constructor.getParameters())
argumentSize += var->getType()->getCalldataEncodedSize();
if (argumentSize > 0)
if (!_constructor.getParameters().empty())
{
CompilerUtils(m_context).fetchFreeMemoryPointer();
m_context << u256(argumentSize) << eth::Instruction::DUP1;
m_context.appendProgramSize(); // program itself
// CODESIZE is program plus manually added arguments
m_context << eth::Instruction::CODESIZE << eth::Instruction::SUB;
// stack: <memptr> <argument size>
m_context << eth::Instruction::DUP1;
m_context.appendProgramSize();
m_context << eth::Instruction::DUP4 << eth::Instruction::CODECOPY;
m_context << eth::Instruction::ADD;
@ -265,8 +265,9 @@ void Compiler::appendCalldataUnpacker(
{
solAssert(arrayType.location() == DataLocation::Memory, "");
// compute data pointer
m_context << eth::Instruction::DUP1 << eth::Instruction::MLOAD;
//@todo once we support nested arrays, this offset needs to be dynamic.
m_context << eth::Instruction::DUP1 << _startOffset << eth::Instruction::ADD;
m_context << _startOffset << eth::Instruction::ADD;
m_context << eth::Instruction::SWAP1 << u256(0x20) << eth::Instruction::ADD;
}
else

2
libsolidity/CompilerUtils.cpp

@ -135,7 +135,7 @@ void CompilerUtils::storeInMemoryDynamic(Type const& _type, bool _padToWordBound
m_context << u256(0) << u256(identityContractAddress);
//@TODO do not use ::CALL if less than 32 bytes?
//@todo in production, we should not have to pair c_callNewAccountGas.
m_context << u256(eth::c_callGas + 10 + eth::c_callNewAccountGas) << eth::Instruction::GAS;
m_context << u256(eth::c_callGas + 15 + eth::c_callNewAccountGas) << eth::Instruction::GAS;
m_context << eth::Instruction::SUB << eth::Instruction::CALL;
m_context << eth::Instruction::POP; // ignore return value

28
libsolidity/Types.cpp

@ -822,16 +822,16 @@ string ArrayType::toString(bool _short) const
TypePointer ArrayType::externalType() const
{
if (m_arrayKind != ArrayKind::Ordinary)
return this->copyForLocation(DataLocation::Memory, true);
return this->copyForLocation(DataLocation::CallData, true);
if (!m_baseType->externalType())
return TypePointer();
if (m_baseType->getCategory() == Category::Array && m_baseType->isDynamicallySized())
return TypePointer();
if (isDynamicallySized())
return std::make_shared<ArrayType>(DataLocation::Memory, m_baseType->externalType());
return std::make_shared<ArrayType>(DataLocation::CallData, m_baseType->externalType());
else
return std::make_shared<ArrayType>(DataLocation::Memory, m_baseType->externalType(), m_length);
return std::make_shared<ArrayType>(DataLocation::CallData, m_baseType->externalType(), m_length);
}
TypePointer ArrayType::copyForLocation(DataLocation _location, bool _isPointer) const
@ -903,7 +903,7 @@ MemberList const& ContractType::getMembers() const
for (auto const& it: m_contract.getInterfaceFunctions())
members.push_back(MemberList::Member(
it.second->getDeclaration().getName(),
it.second->removeDynamicReturnTypes(),
it.second->asMemberFunction(),
&it.second->getDeclaration()
));
m_members.reset(new MemberList(members));
@ -1084,11 +1084,7 @@ FunctionType::FunctionType(FunctionDefinition const& _function, bool _isInternal
for (ASTPointer<VariableDeclaration> const& var: _function.getParameters())
{
paramNames.push_back(var->getName());
if (_isInternal)
params.push_back(var->getType());
else
params.push_back(var->getType()->externalType());
solAssert(!!params.back(), "Function argument type not valid in this context.");
params.push_back(var->getType());
}
retParams.reserve(_function.getReturnParameters().size());
retParamNames.reserve(_function.getReturnParameters().size());
@ -1416,8 +1412,18 @@ TypePointer FunctionType::copyAndSetGasOrValue(bool _setGas, bool _setValue) con
);
}
FunctionTypePointer FunctionType::removeDynamicReturnTypes() const
FunctionTypePointer FunctionType::asMemberFunction() const
{
TypePointers parameterTypes;
for (auto const& t: m_parameterTypes)
{
auto refType = dynamic_cast<ReferenceType const*>(t.get());
if (refType && refType->location() == DataLocation::CallData)
parameterTypes.push_back(refType->copyForLocation(DataLocation::Memory, false));
else
parameterTypes.push_back(t);
}
//@todo make this more intelligent once we support destructuring assignments
TypePointers returnParameterTypes;
vector<string> returnParameterNames;
@ -1427,7 +1433,7 @@ FunctionTypePointer FunctionType::removeDynamicReturnTypes() const
returnParameterNames.push_back(m_returnParameterNames.front());
}
return make_shared<FunctionType>(
m_parameterTypes,
parameterTypes,
returnParameterTypes,
m_parameterNames,
returnParameterNames,

9
libsolidity/Types.h

@ -735,10 +735,11 @@ public:
/// of the parameters to fals.
TypePointer copyAndSetGasOrValue(bool _setGas, bool _setValue) const;
/// @returns a copy of this function type where all return parameters of dynamic size are removed.
/// This is needed if external functions are called internally, as they cannot return dynamic
/// values.
FunctionTypePointer removeDynamicReturnTypes() const;
/// @returns a copy of this function type where all return parameters of dynamic size are
/// removed and the location of reference types is changed from CallData to Memory.
/// This is needed if external functions are called on other contracts, as they cannot return
/// dynamic values.
FunctionTypePointer asMemberFunction() const;
private:
static TypePointers parseElementaryTypeVector(strings const& _types);

40
test/libsolidity/SolidityEndToEndTest.cpp

@ -4457,26 +4457,52 @@ BOOST_AUTO_TEST_CASE(bytes_index_access_memory)
);
}
BOOST_AUTO_TEST_CASE(bytes_in_constructors)
BOOST_AUTO_TEST_CASE(bytes_in_constructors_unpacker)
{
char const* sourceCode = R"(
contract Base {
contract Test {
uint public m_x;
bytes public m_s;
function Test(uint x, bytes s) {
m_x = x;
m_s = s;
}
}
)";
string s1("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
bytes dyn1 = encodeArgs(u256(s1.length()), s1);
u256 x = 7;
bytes args1 = encodeArgs(x, u256(0x40)) + dyn1;
compileAndRun(sourceCode, 0, "Test", args1);
BOOST_REQUIRE(callContractFunction("m_x()") == encodeArgs(x));
BOOST_REQUIRE(callContractFunction("m_s()") == encodeArgs(u256(0x20)) + dyn1);
}
BOOST_AUTO_TEST_CASE(bytes_in_constructors_packer)
{
char const* sourceCode = R"(
contract Base {
uint public m_x;
bytes m_s;
function Base(uint x, bytes s) {
m_x = x;
m_s = s;
}
function part(uint i) returns (byte) {
return m_s[i];
}
}
contract Main is Base {
function Main(bytes s, uint x) Base(x, f(s)) {}
function f(bytes s) returns (bytes) { return s; }
function Main(bytes s, uint x) Base(x, s){}//f(s)) {}
function f(bytes s) returns (bytes) {
return s;
}
}
contract Creator {
function f(uint x, bytes s) returns (uint r, bytes b) {
function f(uint x, bytes s) returns (uint r, byte ch) {
var c = new Main(s, x);
r = c.m_x();
b = c.m_s();
ch = c.part(x);
}
}
)";
@ -4487,7 +4513,7 @@ BOOST_AUTO_TEST_CASE(bytes_in_constructors)
bytes args1 = encodeArgs(x, u256(0x40)) + dyn1;
BOOST_REQUIRE(
callContractFunction("f(uint256,bytes)", asString(args1)) ==
encodeArgs(x, u256(0x40), dyn1)
encodeArgs(x, string{s1[unsigned(x)]})
);
}

13
test/libsolidity/SolidityNameAndTypeResolution.cpp

@ -1987,6 +1987,19 @@ BOOST_AUTO_TEST_CASE(mem_array_assignment_changes_base_type)
BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
}
BOOST_AUTO_TEST_CASE(dynamic_return_types_not_possible)
{
char const* sourceCode = R"(
contract C {
function f(uint) returns (string);
function g() {
var x = this.f(2);
}
}
)";
BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
}
BOOST_AUTO_TEST_SUITE_END()
}

Loading…
Cancel
Save