mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Implement Dynamic array push and fix test
Still a work in progress. There is a disturbance in the stack at the moment and that's why there are some cout statements left for debugging.
This commit is contained in:
parent
3287cd464f
commit
a521843f6b
@ -623,6 +623,43 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
|
|||||||
appendExternalFunctionCall(function, arguments);
|
appendExternalFunctionCall(function, arguments);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case Location::ArrayPush:
|
||||||
|
{
|
||||||
|
cout << "Beginning " << m_context.stackHeight() << endl;
|
||||||
|
solAssert(function.parameterTypes().size() == 1, "");
|
||||||
|
solAssert(!!function.parameterTypes()[0], "");
|
||||||
|
TypePointer const& paramType = function.parameterTypes()[0];
|
||||||
|
ArrayType arrayType(DataLocation::Storage, paramType);
|
||||||
|
// get the current length
|
||||||
|
ArrayUtils(m_context).retrieveLength(arrayType);
|
||||||
|
m_context << eth::Instruction::DUP1;
|
||||||
|
cout << "After DUP1 " << m_context.stackHeight() << endl;
|
||||||
|
// stack: ArrayReference currentLength currentLength
|
||||||
|
m_context << u256(1) << eth::Instruction::ADD;
|
||||||
|
// stack: ArrayReference currentLength newLength
|
||||||
|
m_context << eth::Instruction::DUP3 << eth::Instruction::DUP2;
|
||||||
|
ArrayUtils(m_context).resizeDynamicArray(arrayType);
|
||||||
|
cout << "After Resize Dynamic Array " << m_context.stackHeight() << endl;
|
||||||
|
m_context << eth::Instruction::SWAP2 << eth::Instruction::SWAP1;
|
||||||
|
// stack: newLength ArrayReference oldLength
|
||||||
|
ArrayUtils(m_context).accessIndex(arrayType, false);
|
||||||
|
cout << "After Access Index " << m_context.stackHeight() << endl;
|
||||||
|
|
||||||
|
// stack: newLength storageSlot slotOffset
|
||||||
|
arguments[0]->accept(*this);
|
||||||
|
// stack: newLength storageSlot slotOffset argValue
|
||||||
|
TypePointer type = arguments[0]->annotation().type;
|
||||||
|
utils().convertType(*type, *type->mobileType());
|
||||||
|
type = type->mobileType();
|
||||||
|
utils().moveToStackTop(1 + type->sizeOnStack());
|
||||||
|
utils().moveToStackTop(1 + type->sizeOnStack());
|
||||||
|
// stack: newLength argValue storageSlot slotOffset
|
||||||
|
StorageItem(m_context, *paramType).storeValue(*type, _functionCall.location(), true);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Location::ByteArrayPush:
|
||||||
|
// TODO
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Invalid function type."));
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Invalid function type."));
|
||||||
}
|
}
|
||||||
@ -806,16 +843,12 @@ void ExpressionCompiler::endVisit(MemberAccess const& _memberAccess)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (member == "push" && type.isDynamicallySized() && type.location() == DataLocation::Storage)
|
else if (member == "push")
|
||||||
{
|
{
|
||||||
if (type.isByteArray())
|
solAssert(
|
||||||
{
|
type.isDynamicallySized() && type.location() == DataLocation::Storage,
|
||||||
solAssert(!type.isString(), "Index access to string is not allowed.");
|
"Tried to use .push() on a non-dynamically sized array"
|
||||||
setLValue<StorageByteArrayElement>(_indexAccess);
|
);
|
||||||
}
|
|
||||||
else
|
|
||||||
setLValueToStorageItem(_indexAccess);
|
|
||||||
setLValue<StorageArrayLength>(_memberAccess, type);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
solAssert(false, "Illegal array member.");
|
solAssert(false, "Illegal array member.");
|
||||||
|
@ -858,6 +858,28 @@ string ArrayType::canonicalName(bool _addDataLocation) const
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MemberList const& ArrayType::members() const
|
||||||
|
{
|
||||||
|
if (!m_members)
|
||||||
|
{
|
||||||
|
MemberList::MemberMap members;
|
||||||
|
if (!isString())
|
||||||
|
{
|
||||||
|
members.push_back({"length", make_shared<IntegerType>(256)});
|
||||||
|
if (isDynamicallySized() && location() == DataLocation::Storage)
|
||||||
|
members.push_back({"push", make_shared<FunctionType>(
|
||||||
|
TypePointers{baseType()},
|
||||||
|
TypePointers{make_shared<IntegerType>(256)},
|
||||||
|
strings{string()},
|
||||||
|
strings{string()},
|
||||||
|
isByteArray() ? FunctionType::Location::ByteArrayPush : FunctionType::Location::ArrayPush
|
||||||
|
)});
|
||||||
|
}
|
||||||
|
m_members.reset(new MemberList(members));
|
||||||
|
}
|
||||||
|
return *m_members;
|
||||||
|
}
|
||||||
|
|
||||||
TypePointer ArrayType::encodingType() const
|
TypePointer ArrayType::encodingType() const
|
||||||
{
|
{
|
||||||
if (location() == DataLocation::Storage)
|
if (location() == DataLocation::Storage)
|
||||||
@ -913,8 +935,6 @@ TypePointer ArrayType::copyForLocation(DataLocation _location, bool _isPointer)
|
|||||||
return copy;
|
return copy;
|
||||||
}
|
}
|
||||||
|
|
||||||
const MemberList ArrayType::s_arrayTypeMemberList({{"length", make_shared<IntegerType>(256)}});
|
|
||||||
|
|
||||||
bool ContractType::operator==(Type const& _other) const
|
bool ContractType::operator==(Type const& _other) const
|
||||||
{
|
{
|
||||||
if (_other.category() != category())
|
if (_other.category() != category())
|
||||||
@ -1422,6 +1442,8 @@ unsigned FunctionType::sizeOnStack() const
|
|||||||
size = 1;
|
size = 1;
|
||||||
else if (location == Location::Internal)
|
else if (location == Location::Internal)
|
||||||
size = 1;
|
size = 1;
|
||||||
|
else if (location == Location::ArrayPush || location == Location::ByteArrayPush)
|
||||||
|
size = 1;
|
||||||
if (m_gasSet)
|
if (m_gasSet)
|
||||||
size++;
|
size++;
|
||||||
if (m_valueSet)
|
if (m_valueSet)
|
||||||
|
@ -505,10 +505,7 @@ public:
|
|||||||
virtual unsigned sizeOnStack() const override;
|
virtual unsigned sizeOnStack() const override;
|
||||||
virtual std::string toString(bool _short) const override;
|
virtual std::string toString(bool _short) const override;
|
||||||
virtual std::string canonicalName(bool _addDataLocation) const override;
|
virtual std::string canonicalName(bool _addDataLocation) const override;
|
||||||
virtual MemberList const& members() const override
|
virtual MemberList const& members() const override;
|
||||||
{
|
|
||||||
return isString() ? EmptyMemberList : s_arrayTypeMemberList;
|
|
||||||
}
|
|
||||||
virtual TypePointer encodingType() const override;
|
virtual TypePointer encodingType() const override;
|
||||||
virtual TypePointer decodingType() const override;
|
virtual TypePointer decodingType() const override;
|
||||||
virtual TypePointer interfaceType(bool _inLibrary) const override;
|
virtual TypePointer interfaceType(bool _inLibrary) const override;
|
||||||
@ -532,7 +529,8 @@ private:
|
|||||||
TypePointer m_baseType;
|
TypePointer m_baseType;
|
||||||
bool m_hasDynamicLength = true;
|
bool m_hasDynamicLength = true;
|
||||||
u256 m_length;
|
u256 m_length;
|
||||||
static const MemberList s_arrayTypeMemberList;
|
/// List of member types, will be lazy-initialized because of recursive references.
|
||||||
|
mutable std::unique_ptr<MemberList> m_members;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -736,7 +734,9 @@ public:
|
|||||||
Event, ///< syntactic sugar for LOG*
|
Event, ///< syntactic sugar for LOG*
|
||||||
SetGas, ///< modify the default gas value for the function call
|
SetGas, ///< modify the default gas value for the function call
|
||||||
SetValue, ///< modify the default value transfer for the function call
|
SetValue, ///< modify the default value transfer for the function call
|
||||||
BlockHash ///< BLOCKHASH
|
BlockHash, ///< BLOCKHASH
|
||||||
|
ArrayPush, ///< .push() to a dynamically sized array in storage
|
||||||
|
ByteArrayPush ///< .push() to a dynamically sized byte array in storage
|
||||||
};
|
};
|
||||||
|
|
||||||
virtual Category category() const override { return Category::Function; }
|
virtual Category category() const override { return Category::Function; }
|
||||||
|
@ -3481,16 +3481,19 @@ BOOST_AUTO_TEST_CASE(array_push)
|
|||||||
{
|
{
|
||||||
char const* sourceCode = R"(
|
char const* sourceCode = R"(
|
||||||
contract c {
|
contract c {
|
||||||
int[] data;
|
uint[] data;
|
||||||
function test() returns (uint) {
|
function test() returns (uint x, uint y, uint z, uint l) {
|
||||||
data.push(5);
|
data.push(5);
|
||||||
|
x = data[0];
|
||||||
data.push(4);
|
data.push(4);
|
||||||
return data.push(3);
|
y = data[1];
|
||||||
|
l = data.push(3);
|
||||||
|
z = data[2];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)";
|
)";
|
||||||
compileAndRun(sourceCode);
|
compileAndRun(sourceCode);
|
||||||
BOOST_CHECK(callContractFunction("test()") == encodeArgs(3));
|
BOOST_CHECK(callContractFunction("test()") == encodeArgs(5, 4, 3, 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(external_array_args)
|
BOOST_AUTO_TEST_CASE(external_array_args)
|
||||||
|
Loading…
Reference in New Issue
Block a user