[libevmasm] loadFromAssemblyJSON: optionally load from sourceList.

This commit is contained in:
Alexander Arlt 2021-11-09 18:54:53 -05:00
parent c822263646
commit dcb697f6d4
3 changed files with 17 additions and 12 deletions

View File

@ -518,11 +518,21 @@ bool Assembly::addAssemblyItemsFromJSON(Json::Value const& _code)
return true;
}
bool Assembly::loadFromAssemblyJSON(Json::Value const& _json)
bool Assembly::loadFromAssemblyJSON(Json::Value const& _json, bool _loadSources /* = true */)
{
if (!_json[".code"].isArray())
return false;
bool result{true};
if (_loadSources)
{
vector<string> sourceList;
if (_json.isMember("sourceList"))
for (auto const& it: _json["sourceList"])
sourceList.emplace_back(it.asString());
setSources(sourceList);
}
addAssemblyItemsFromJSON(_json[".code"]);
if (_json[".auxdata"].isString())
this->m_auxiliaryData = fromHex(_json[".auxdata"].asString());
@ -538,7 +548,7 @@ bool Assembly::loadFromAssemblyJSON(Json::Value const& _json)
{
shared_ptr<Assembly> subassembly = make_shared<Assembly>();
subassembly->setSources(this->sources());
result &= subassembly->loadFromAssemblyJSON(code);
result &= subassembly->loadFromAssemblyJSON(code, false);
this->m_subs.emplace_back(subassembly);
}
}

View File

@ -39,6 +39,7 @@
#include <sstream>
#include <memory>
#include <map>
#include <utility>
namespace solidity::evmasm
{
@ -159,7 +160,7 @@ public:
bool _includeSourceList = true
) const;
bool loadFromAssemblyJSON(Json::Value const& _json);
bool loadFromAssemblyJSON(Json::Value const& _json, bool _loadSources = true);
/// Mark this assembly as invalid. Calling ``assemble`` on it will throw.
void markAsInvalid() { m_invalid = true; }
@ -168,7 +169,7 @@ public:
size_t encodeSubPath(std::vector<size_t> const& _subPath);
void setSources(std::vector<std::shared_ptr<std::string const>> _sources) {
m_sources = _sources;
m_sources = std::move(_sources);
}
void setSources(std::vector<std::string> const& _sources) {

View File

@ -685,20 +685,14 @@ bool CompilerStack::compile(State _stopAfter)
optimiserSettings.runJumpdestRemover = m_optimiserSettings.runJumpdestRemover;
optimiserSettings.runPeephole = m_optimiserSettings.runPeephole;
vector<string> sourceList;
if (m_evmAssemblyJson[evmAssemblyJsonSource].isMember("sourceList"))
for (auto const& it: m_evmAssemblyJson[evmAssemblyJsonSource]["sourceList"])
sourceList.emplace_back(it.asString());
m_contracts[evmAssemblyJsonSource].evmAssembly = make_shared<evmasm::Assembly>(evmAssemblyJsonSource);
m_contracts[evmAssemblyJsonSource].evmAssembly->setSources(sourceList);
m_contracts[evmAssemblyJsonSource].evmAssembly->loadFromAssemblyJSON(m_evmAssemblyJson[evmAssemblyJsonSource]);
m_contracts[evmAssemblyJsonSource].evmAssembly->optimise(optimiserSettings);
m_contracts[evmAssemblyJsonSource].object = m_contracts[evmAssemblyJsonSource].evmAssembly->assemble();
m_contracts[evmAssemblyJsonSource].evmRuntimeAssembly = make_shared<evmasm::Assembly>(evmAssemblyJsonSource);
m_contracts[evmAssemblyJsonSource].evmRuntimeAssembly->setSources(sourceList);
m_contracts[evmAssemblyJsonSource].evmRuntimeAssembly->loadFromAssemblyJSON(m_evmAssemblyJson[evmAssemblyJsonSource][".data"]["0"]);
m_contracts[evmAssemblyJsonSource].evmRuntimeAssembly->setSources(m_contracts[evmAssemblyJsonSource].evmAssembly->sources());
m_contracts[evmAssemblyJsonSource].evmRuntimeAssembly->loadFromAssemblyJSON(m_evmAssemblyJson[evmAssemblyJsonSource][".data"]["0"], false);
m_contracts[evmAssemblyJsonSource].evmRuntimeAssembly->optimise(optimiserSettings);
m_contracts[evmAssemblyJsonSource].runtimeObject = m_contracts[evmAssemblyJsonSource].evmRuntimeAssembly->assemble();
}