Skip to content

Commit 2cf619f

Browse files
committed
Assembly::loadFromAssemblyJSON(..) as static factory function.
1 parent 1183530 commit 2cf619f

File tree

3 files changed

+20
-22
lines changed

3 files changed

+20
-22
lines changed

libevmasm/Assembly.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -409,41 +409,41 @@ Json::Value Assembly::assemblyJSON(map<string, unsigned> const& _sourceIndices,
409409
return root;
410410
}
411411

412-
bool Assembly::loadFromAssemblyJSON(Json::Value const& _json, bool _loadSources /* = true */)
412+
std::shared_ptr<Assembly> Assembly::loadFromAssemblyJSON(Json::Value const& _json, std::vector<std::string> const& _sourceList /* = {} */, bool _isCreation /* = true */)
413413
{
414414
if (!_json[".code"].isArray())
415-
return false;
416-
bool success{true};
415+
return {};
417416

418-
if (_loadSources)
417+
std::shared_ptr<Assembly> result = std::make_shared<Assembly>(_isCreation, "");
418+
vector<string> sourceList;
419+
if (_sourceList.empty())
419420
{
420-
vector<string> sourceList;
421421
if (_json.isMember("sourceList"))
422422
for (auto const& it: _json["sourceList"])
423423
sourceList.emplace_back(it.asString());
424-
setSources(sourceList);
425424
}
426-
427-
addAssemblyItemsFromJSON(_json[".code"]);
425+
else
426+
sourceList = _sourceList;
427+
result->setSources(sourceList);
428+
result->addAssemblyItemsFromJSON(_json[".code"]);
428429
if (_json[".auxdata"].isString())
429-
m_auxiliaryData = fromHex(_json[".auxdata"].asString());
430+
result->m_auxiliaryData = fromHex(_json[".auxdata"].asString());
430431
Json::Value const& data = _json[".data"];
431432
for (Json::ValueConstIterator itr = data.begin(); itr != data.end(); itr++)
432433
{
433434
solAssert(itr.key().isString(), "");
434435
std::string key = itr.key().asString();
435436
Json::Value const& code = data[key];
436437
if (code.isString())
437-
m_data[h256(fromHex(key))] = fromHex(code.asString());
438+
result->m_data[h256(fromHex(key))] = fromHex(code.asString());
438439
else
439440
{
440-
shared_ptr<Assembly> subassembly = make_shared<Assembly>(false, "");
441-
subassembly->setSources(sources());
442-
success &= subassembly->loadFromAssemblyJSON(code, false);
443-
m_subs.emplace_back(subassembly);
441+
std::shared_ptr<Assembly> subassembly(Assembly::loadFromAssemblyJSON(code, sourceList, /* isCreation = */ false));
442+
assertThrow(subassembly, AssemblyException, "");
443+
result->m_subs.emplace_back(std::make_shared<Assembly>(*subassembly));
444444
}
445445
}
446-
return success;
446+
return result;
447447
}
448448

449449
AssemblyItem Assembly::namedTag(string const& _name, size_t _params, size_t _returns, optional<uint64_t> _sourceID)

libevmasm/Assembly.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class Assembly
156156
/// @param _json JSON object containing assembly
157157
/// @param _loadSources true, if source list should be included, false otherwise.
158158
/// @returns true on success, false otherwise
159-
bool loadFromAssemblyJSON(Json::Value const& _json, bool _loadSources = true);
159+
static std::shared_ptr<Assembly> loadFromAssemblyJSON(Json::Value const& _json, std::vector<std::string> const& _sourceList = {}, bool _isCreation = true);
160160

161161
/// Mark this assembly as invalid. Calling ``assemble`` on it will throw.
162162
void markAsInvalid() { m_invalid = true; }

libsolidity/interface/CompilerStack.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ void CompilerStack::importEvmAssemblyJson(std::map<std::string, Json::Value> con
423423
if (m_stackState != Empty)
424424
solThrow(CompilerError, "Must call importEvmAssemblyJson only before the SourcesSet state.");
425425

426-
Json::Value jsonValue = _sources.begin()->second;
426+
Json::Value const& jsonValue = _sources.begin()->second;
427427
if (jsonValue.isMember("sourceList"))
428428
for (auto const& item: jsonValue["sourceList"])
429429
{
@@ -696,15 +696,13 @@ bool CompilerStack::compile(State _stopAfter)
696696
optimiserSettings.runJumpdestRemover = m_optimiserSettings.runJumpdestRemover;
697697
optimiserSettings.runPeephole = m_optimiserSettings.runPeephole;
698698

699-
m_contracts[evmSourceName].evmAssembly = make_shared<evmasm::Assembly>(true, evmSourceName);
700-
m_contracts[evmSourceName].evmAssembly->loadFromAssemblyJSON(m_evmAssemblyJson[evmSourceName]);
699+
m_contracts[evmSourceName].evmAssembly = evmasm::Assembly::loadFromAssemblyJSON(m_evmAssemblyJson[evmSourceName]);
701700
if (m_optimiserSettings.enabled)
702701
m_contracts[evmSourceName].evmAssembly->optimise(optimiserSettings);
703702
m_contracts[evmSourceName].object = m_contracts[evmSourceName].evmAssembly->assemble();
704703

705-
m_contracts[evmSourceName].evmRuntimeAssembly = make_shared<evmasm::Assembly>(false, evmSourceName);
706-
m_contracts[evmSourceName].evmRuntimeAssembly->setSources(m_contracts[evmSourceName].evmAssembly->sources());
707-
m_contracts[evmSourceName].evmRuntimeAssembly->loadFromAssemblyJSON(m_evmAssemblyJson[evmSourceName][".data"]["0"], false);
704+
m_contracts[evmSourceName].evmRuntimeAssembly = std::make_shared<evmasm::Assembly>(m_contracts[evmSourceName].evmAssembly->sub(0));
705+
solAssert(m_contracts[evmSourceName].evmRuntimeAssembly->isCreation() == false, "");
708706
if (m_optimiserSettings.enabled)
709707
m_contracts[evmSourceName].evmRuntimeAssembly->optimise(optimiserSettings);
710708
m_contracts[evmSourceName].runtimeObject = m_contracts[evmSourceName].evmRuntimeAssembly->assemble();

0 commit comments

Comments
 (0)