[libevmasm] EVM Assembly JSON Import: add support for sub-object index.

This commit is contained in:
Alexander Arlt 2023-06-21 11:03:09 +02:00
parent 2ffa8aa57a
commit c5de882bbb
2 changed files with 9 additions and 5 deletions

View File

@ -508,7 +508,7 @@ Json::Value Assembly::assemblyJSON(std::vector<std::string> const& _sources, boo
std::pair<std::shared_ptr<Assembly>, std::vector<std::string>> Assembly::fromJSON(Json::Value const& _json, vector<string> const& _sourceList, int _level)
{
solRequire(_json.isObject(), AssemblyImportException, "Supplied JSON is not an object.");
static set<string> const validMembers{".code", ".data", ".auxdata", "sourceList"};
static set<string> const validMembers{".code", ".data", ".auxdata", "sourceList", "index"};
for (auto const& attribute: _json.getMemberNames())
solRequire(validMembers.count(attribute), AssemblyImportException, "Unknown attribute '" + attribute + "'.");
solRequire(_json.isMember(".code"), AssemblyImportException, "Member '.code' does not exist.");
@ -580,9 +580,13 @@ std::pair<std::shared_ptr<Assembly>, std::vector<std::string>> Assembly::fromJSO
}
else if (code.isObject())
{
solAssert(code.isMember("index"));
size_t index = static_cast<size_t>(code["index"].asInt());
if (result->m_subs.size() <= index)
result->m_subs.resize(index + 1);
shared_ptr<Assembly> subassembly(Assembly::fromJSON(code, sourceList, _level + 1).first);
solAssert(subassembly);
result->m_subs.emplace_back(subassembly);
result->m_subs[index] = subassembly;
}
else
solThrow(AssemblyImportException, "Key inside '.data' '" + dataItemID + "' can only be a valid hex-string or an object.");

View File

@ -198,12 +198,12 @@ BOOST_AUTO_TEST_CASE(all_assembly_items)
"{\"begin\":6,\"end\":8,\"name\":\"PUSHIMMUTABLE\",\"source\":1,\"value\":\"someImmutable\"},"
"{\"begin\":6,\"end\":8,\"name\":\"PUSH [ErrorTag]\",\"source\":1},"
"{\"begin\":6,\"end\":8,\"name\":\"INVALID\",\"source\":1}"
"]},"
"],\"index\":0},"
"\"1\":{\".code\":["
"{\"begin\":8,\"end\":18,\"name\":\"VERBATIM\",\"source\":2,\"value\":\"ffff\"},"
"{\"begin\":8,\"end\":18,\"name\":\"VERBATIM\",\"source\":2,\"value\":\"74657374\"},"
"{\"begin\":8,\"end\":18,\"name\":\"MSTORE\",\"source\":2}"
"]},\"A6885B3731702DA62E8E4A8F584AC46A7F6822F4E2BA50FBA902F67B1588D23B\":\"01020304\"},\"sourceList\":[\"root.asm\",\"sub.asm\",\"verbatim.asm\"]}"
"],\"index\":1},\"A6885B3731702DA62E8E4A8F584AC46A7F6822F4E2BA50FBA902F67B1588D23B\":\"01020304\"},\"sourceList\":[\"root.asm\",\"sub.asm\",\"verbatim.asm\"]}"
};
Json::Value jsonValue;
BOOST_CHECK(util::jsonParseStrict(json, jsonValue));
@ -388,7 +388,7 @@ BOOST_AUTO_TEST_CASE(immutable)
"{\"begin\":6,\"end\":8,\"name\":\"PUSHIMMUTABLE\",\"source\":1,\"value\":\"someImmutable\"},"
"{\"begin\":6,\"end\":8,\"name\":\"PUSHIMMUTABLE\",\"source\":1,\"value\":\"someOtherImmutable\"},"
"{\"begin\":6,\"end\":8,\"name\":\"PUSHIMMUTABLE\",\"source\":1,\"value\":\"someImmutable\"}"
"]}},\"sourceList\":[\"root.asm\",\"sub.asm\"]}"
"],\"index\":0}},\"sourceList\":[\"root.asm\",\"sub.asm\"]}"
);
}