mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Allow structs as part of function interfaces.
This commit is contained in:
parent
a0d171722a
commit
2e72bd163a
@ -1770,7 +1770,7 @@ TypePointer StructType::interfaceType(bool _inLibrary) const
|
||||
if (_inLibrary && location() == DataLocation::Storage)
|
||||
return shared_from_this();
|
||||
else
|
||||
return TypePointer();
|
||||
return copyForLocation(DataLocation::Memory, true);
|
||||
}
|
||||
|
||||
TypePointer StructType::copyForLocation(DataLocation _location, bool _isPointer) const
|
||||
|
@ -86,12 +86,12 @@ Json::Value ABI::generate(ContractDefinition const& _contractDef)
|
||||
Json::Value params(Json::arrayValue);
|
||||
for (auto const& p: it->parameters())
|
||||
{
|
||||
solAssert(!!p->annotation().type->interfaceType(false), "");
|
||||
auto type = p->annotation().type->interfaceType(false);
|
||||
solAssert(type, "");
|
||||
Json::Value input;
|
||||
input["name"] = p->name();
|
||||
input["type"] = p->annotation().type->interfaceType(false)->canonicalName(false);
|
||||
input["indexed"] = p->isIndexed();
|
||||
params.append(input);
|
||||
auto param = formatType(p->name(), *type, false);
|
||||
param["indexed"] = p->isIndexed();
|
||||
params.append(param);
|
||||
}
|
||||
event["inputs"] = params;
|
||||
abi.append(event);
|
||||
@ -111,10 +111,50 @@ Json::Value ABI::formatTypeList(
|
||||
for (unsigned i = 0; i < _names.size(); ++i)
|
||||
{
|
||||
solAssert(_types[i], "");
|
||||
Json::Value param;
|
||||
param["name"] = _names[i];
|
||||
param["type"] = _types[i]->canonicalName(_forLibrary);
|
||||
params.append(param);
|
||||
params.append(formatType(_names[i], *_types[i], _forLibrary));
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
||||
Json::Value ABI::formatType(string const& _name, Type const& _type, bool _forLibrary)
|
||||
{
|
||||
Json::Value ret;
|
||||
ret["name"] = _name;
|
||||
if (_type.isValueType() || (_forLibrary && _type.dataStoredIn(DataLocation::Storage)))
|
||||
ret["type"] = _type.canonicalName(_forLibrary);
|
||||
else if (ArrayType const* arrayType = dynamic_cast<ArrayType const*>(&_type))
|
||||
{
|
||||
if (arrayType->isByteArray())
|
||||
ret["type"] = _type.canonicalName(_forLibrary);
|
||||
else
|
||||
{
|
||||
string suffix;
|
||||
if (arrayType->isDynamicallySized())
|
||||
suffix = "[]";
|
||||
else
|
||||
suffix = string("[") + arrayType->length().str() + "]";
|
||||
solAssert(arrayType->baseType(), "");
|
||||
Json::Value subtype = formatType("", *arrayType->baseType(), _forLibrary);
|
||||
if (subtype["type"].isString() && !subtype.isMember("subtype"))
|
||||
ret["type"] = subtype["type"].asString() + suffix;
|
||||
else
|
||||
{
|
||||
ret["type"] = suffix;
|
||||
solAssert(!subtype.isMember("subtype"), "");
|
||||
ret["subtype"] = subtype["type"];
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (StructType const* structType = dynamic_cast<StructType const*>(&_type))
|
||||
{
|
||||
ret["type"] = Json::arrayValue;
|
||||
for (auto const& member: structType->members(nullptr))
|
||||
{
|
||||
solAssert(member.type, "");
|
||||
ret["type"].append(formatType(member.name, *member.type, _forLibrary));
|
||||
}
|
||||
}
|
||||
else
|
||||
solAssert(false, "Invalid type.");
|
||||
return ret;
|
||||
}
|
||||
|
@ -50,6 +50,10 @@ private:
|
||||
std::vector<TypePointer> const& _types,
|
||||
bool _forLibrary
|
||||
);
|
||||
/// @returns a Json object with "name", "type" and potentially "subtype" keys, according
|
||||
/// to the ABI specification.
|
||||
/// If it is possible to express the type as a single string, it is allowed to return a single string.
|
||||
static Json::Value formatType(std::string const& _name, Type const& _type, bool _forLibrary);
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -48,7 +48,7 @@ public:
|
||||
|
||||
Json::Value generatedInterface = m_compilerStack.contractABI("");
|
||||
Json::Value expectedInterface;
|
||||
m_reader.parse(_expectedInterfaceString, expectedInterface);
|
||||
BOOST_REQUIRE(m_reader.parse(_expectedInterfaceString, expectedInterface));
|
||||
BOOST_CHECK_MESSAGE(
|
||||
expectedInterface == generatedInterface,
|
||||
"Expected:\n" << expectedInterface.toStyledString() <<
|
||||
@ -939,6 +939,140 @@ BOOST_AUTO_TEST_CASE(function_type)
|
||||
checkInterface(sourceCode, interface);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(return_structs)
|
||||
{
|
||||
char const* text = R"(
|
||||
contract C {
|
||||
struct S { uint a; T[] sub; }
|
||||
struct T { uint[2] x; }
|
||||
function f() returns (uint x, S s) {
|
||||
}
|
||||
}
|
||||
)";
|
||||
char const* interface = R"(
|
||||
[
|
||||
{
|
||||
"constant" : false,
|
||||
"payable": false,
|
||||
"inputs": [],
|
||||
"name": "f",
|
||||
"outputs": [{
|
||||
"name": "x",
|
||||
"type": "uint256"
|
||||
}, {
|
||||
"name": "s",
|
||||
"type": [{
|
||||
"name": "a",
|
||||
"type": "uint256"
|
||||
}, {
|
||||
"name": "sub",
|
||||
"subtype": [{
|
||||
"name": "x",
|
||||
"type": "uint256[2]"
|
||||
}],
|
||||
"type": "[]"
|
||||
}]
|
||||
}],
|
||||
"type" : "function"
|
||||
}
|
||||
]
|
||||
)";
|
||||
checkInterface(text, interface);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(event_structs)
|
||||
{
|
||||
char const* text = R"(
|
||||
contract C {
|
||||
struct S { uint a; T[] sub; bytes b; }
|
||||
struct T { uint[2] x; }
|
||||
event E(T t, S s);
|
||||
}
|
||||
)";
|
||||
char const* interface = R"(
|
||||
[{
|
||||
"anonymous" : false,
|
||||
"inputs" : [{
|
||||
"indexed" : false,
|
||||
"name" : "t",
|
||||
"type" : [{
|
||||
"name" : "x",
|
||||
"type" : "uint256[2]"
|
||||
}]
|
||||
}, {
|
||||
"indexed" : false,
|
||||
"name" : "s",
|
||||
"type" : [{
|
||||
"name" : "a",
|
||||
"type" : "uint256"
|
||||
}, {
|
||||
"name" : "sub",
|
||||
"subtype" : [{
|
||||
"name" : "x",
|
||||
"type" : "uint256[2]"
|
||||
}],
|
||||
"type" : "[]"
|
||||
}, {
|
||||
"name" : "b",
|
||||
"type" : "bytes"
|
||||
}]
|
||||
}],
|
||||
"name" : "E",
|
||||
"type" : "event"
|
||||
}]
|
||||
)";
|
||||
checkInterface(text, interface);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(structs_in_libraries)
|
||||
{
|
||||
char const* text = R"(
|
||||
library L {
|
||||
struct S { uint a; T[] sub; bytes b; }
|
||||
struct T { uint[2] x; }
|
||||
function f(L.S storage s) {}
|
||||
function g(L.S s) {}
|
||||
}
|
||||
)";
|
||||
char const* interface = R"(
|
||||
[{
|
||||
"constant" : false,
|
||||
"inputs" : [{
|
||||
"name" : "s",
|
||||
"type" : [{
|
||||
"name" : "a",
|
||||
"type" : "uint256"
|
||||
}, {
|
||||
"name" : "sub",
|
||||
"subtype" : [{
|
||||
"name" : "x",
|
||||
"type" : "uint256[2]"
|
||||
}],
|
||||
"type" : "[]"
|
||||
}, {
|
||||
"name" : "b",
|
||||
"type" : "bytes"
|
||||
}]
|
||||
}],
|
||||
"name" : "g",
|
||||
"outputs" : [],
|
||||
"payable" : false,
|
||||
"type" : "function"
|
||||
}, {
|
||||
"constant" : false,
|
||||
"inputs" : [{
|
||||
"name" : "s",
|
||||
"type" : "L.S storage"
|
||||
}],
|
||||
"name" : "f",
|
||||
"outputs" : [],
|
||||
"payable" : false,
|
||||
"type" : "function"
|
||||
}]
|
||||
)";
|
||||
checkInterface(text, interface);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
}
|
||||
|
@ -9682,6 +9682,47 @@ BOOST_AUTO_TEST_CASE(contracts_separated_with_comment)
|
||||
compileAndRun(sourceCode, 0, "C2");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(return_structs)
|
||||
{
|
||||
char const* sourceCode = R"(
|
||||
contract C {
|
||||
struct S { uint a; T[] sub; }
|
||||
struct T { uint[2] x; }
|
||||
function f() returns (uint x, S s) {
|
||||
x = 7;
|
||||
s.a = 8;
|
||||
s.sub = new S[](3);
|
||||
s.sub[0][0] = 9;
|
||||
s.sub[1][0] = 10;
|
||||
s.sub[2][1] = 11;
|
||||
}
|
||||
}
|
||||
)";
|
||||
compileAndRun(sourceCode, 0, "C");
|
||||
|
||||
// Will calculate the exact encoding later.
|
||||
// BOOST_CHECK(callContractFunction("f()") == encodeArgs(
|
||||
// u256(7), u256(0x40),
|
||||
// u256(8), u256(0x40),
|
||||
// u256(3),
|
||||
// // s.sub[0]
|
||||
// u256(9), u256(0xc0),
|
||||
// // s.sub[1]
|
||||
// u256(10), u256(0xe0),
|
||||
// // s.sub[2]
|
||||
// u256(11), u256(0x100),
|
||||
// // s.sub[0].sub
|
||||
// u256(2)
|
||||
// // s.sub[0].sub[0].a
|
||||
// u256(8),
|
||||
// u256()
|
||||
// // s.sub[1].sub
|
||||
// u256(0)
|
||||
// // s.sub[2].sub
|
||||
// u256(2)
|
||||
// ));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(include_creation_bytecode_only_once)
|
||||
{
|
||||
char const* sourceCode = R"(
|
||||
|
@ -5396,6 +5396,56 @@ BOOST_AUTO_TEST_CASE(constructible_internal_constructor)
|
||||
success(text);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(return_structs)
|
||||
{
|
||||
char const* text = R"(
|
||||
contract C {
|
||||
struct S { uint a; T[] sub; }
|
||||
struct T { uint[] x; }
|
||||
function f() returns (uint x, S s) {
|
||||
}
|
||||
}
|
||||
)";
|
||||
success(text);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(return_recursive_structs)
|
||||
{
|
||||
char const* text = R"(
|
||||
contract C {
|
||||
struct S { uint a; S[] sub; }
|
||||
function f() returns (uint x, S s) {
|
||||
}
|
||||
}
|
||||
)";
|
||||
success(text);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(return_recursive_structs2)
|
||||
{
|
||||
char const* text = R"(
|
||||
contract C {
|
||||
struct S { uint a; S[2] sub; }
|
||||
function f() returns (uint x, S s) {
|
||||
}
|
||||
}
|
||||
)";
|
||||
CHECK_ERROR(text, TypeError, "recursive data types in external functions.");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(return_recursive_structs3)
|
||||
{
|
||||
char const* text = R"(
|
||||
contract C {
|
||||
struct S { uint a; S sub; }
|
||||
struct T { S s; }
|
||||
function f() returns (uint x, T t) {
|
||||
}
|
||||
}
|
||||
)";
|
||||
CHECK_ERROR(text, TypeError, "recursive data types in external functions.");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(address_checksum_type_deduction)
|
||||
{
|
||||
char const* text = R"(
|
||||
|
Loading…
Reference in New Issue
Block a user