Skip to content

Commit

Permalink
Basic nlohmann-json integration.
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Beregszaszi <alex@rtfs.hu>
  • Loading branch information
aarlt and axic committed Sep 15, 2021
1 parent 34e516e commit 8b07ad3
Show file tree
Hide file tree
Showing 44 changed files with 1,398 additions and 1,497 deletions.
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ include(EthCcache)

# Let's find our dependencies
include(EthDependencies)
include(jsoncpp)
include(nlohmann-json)
include(range-v3)
include_directories(SYSTEM ${JSONCPP_INCLUDE_DIR})

find_package(Threads)

Expand Down
70 changes: 0 additions & 70 deletions cmake/jsoncpp.cmake

This file was deleted.

14 changes: 14 additions & 0 deletions cmake/nlohmann-json.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
include(ExternalProject)

ExternalProject_Add(nlohmann-json
DOWNLOAD_DIR "${CMAKE_SOURCE_DIR}/deps/nlohmann/json"
DOWNLOAD_NAME json.hpp
DOWNLOAD_NO_EXTRACT 1
URL https://github.com/nlohmann/json/releases/download/v3.10.2/json.hpp
URL_HASH SHA256=059743e48b37e41579ee3a92e82e984bfa0d2a9a2b20b175d04db8089f46f047
CMAKE_COMMAND true
BUILD_COMMAND true
INSTALL_COMMAND true
)

include_directories(SYSTEM ${CMAKE_SOURCE_DIR}/deps/nlohmann)
46 changes: 23 additions & 23 deletions libevmasm/Assembly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
#include <liblangutil/CharStream.h>
#include <liblangutil/Exceptions.h>

#include <json/json.h>

#include <fstream>
#include <range/v3/algorithm/any_of.hpp>

Expand Down Expand Up @@ -198,9 +196,9 @@ string Assembly::assemblyString(StringMap const& _sourceCodes) const
return tmp.str();
}

Json::Value Assembly::createJsonValue(string _name, int _source, int _begin, int _end, string _value, string _jumpType)
Json Assembly::createJsonValue(string _name, int _source, int _begin, int _end, string _value, string _jumpType)
{
Json::Value value;
Json value = Json::object();
value["name"] = _name;
value["source"] = _source;
value["begin"] = _begin;
Expand All @@ -219,11 +217,12 @@ string Assembly::toStringInHex(u256 _value)
return hexStr.str();
}

Json::Value Assembly::assemblyJSON(map<string, unsigned> const& _sourceIndices) const
Json Assembly::assemblyJSON(map<string, unsigned> const& _sourceIndices) const
{
Json::Value root;
Json root = Json::object();
root[".code"] = Json::array();

Json::Value& collection = root[".code"] = Json::arrayValue;
Json& collection = root[".code"];
for (AssemblyItem const& i: m_items)
{
int sourceIndex = -1;
Expand All @@ -237,7 +236,7 @@ Json::Value Assembly::assemblyJSON(map<string, unsigned> const& _sourceIndices)
switch (i.type())
{
case Operation:
collection.append(
collection.emplace_back(
createJsonValue(
instructionInfo(i.instruction()).name,
sourceIndex,
Expand All @@ -247,41 +246,41 @@ Json::Value Assembly::assemblyJSON(map<string, unsigned> const& _sourceIndices)
);
break;
case Push:
collection.append(
collection.emplace_back(
createJsonValue("PUSH", sourceIndex, i.location().start, i.location().end, toStringInHex(i.data()), i.getJumpTypeAsString()));
break;
case PushTag:
if (i.data() == 0)
collection.append(
collection.emplace_back(
createJsonValue("PUSH [ErrorTag]", sourceIndex, i.location().start, i.location().end, ""));
else
collection.append(
collection.emplace_back(
createJsonValue("PUSH [tag]", sourceIndex, i.location().start, i.location().end, toString(i.data())));
break;
case PushSub:
collection.append(
collection.emplace_back(
createJsonValue("PUSH [$]", sourceIndex, i.location().start, i.location().end, toString(h256(i.data()))));
break;
case PushSubSize:
collection.append(
collection.emplace_back(
createJsonValue("PUSH #[$]", sourceIndex, i.location().start, i.location().end, toString(h256(i.data()))));
break;
case PushProgramSize:
collection.append(
collection.emplace_back(
createJsonValue("PUSHSIZE", sourceIndex, i.location().start, i.location().end));
break;
case PushLibraryAddress:
collection.append(
collection.emplace_back(
createJsonValue("PUSHLIB", sourceIndex, i.location().start, i.location().end, m_libraries.at(h256(i.data())))
);
break;
case PushDeployTimeAddress:
collection.append(
collection.emplace_back(
createJsonValue("PUSHDEPLOYADDRESS", sourceIndex, i.location().start, i.location().end)
);
break;
case PushImmutable:
collection.append(createJsonValue(
collection.emplace_back(createJsonValue(
"PUSHIMMUTABLE",
sourceIndex,
i.location().start,
Expand All @@ -290,7 +289,7 @@ Json::Value Assembly::assemblyJSON(map<string, unsigned> const& _sourceIndices)
));
break;
case AssignImmutable:
collection.append(createJsonValue(
collection.emplace_back(createJsonValue(
"ASSIGNIMMUTABLE",
sourceIndex,
i.location().start,
Expand All @@ -299,16 +298,16 @@ Json::Value Assembly::assemblyJSON(map<string, unsigned> const& _sourceIndices)
));
break;
case Tag:
collection.append(
collection.emplace_back(
createJsonValue("tag", sourceIndex, i.location().start, i.location().end, toString(i.data())));
collection.append(
collection.emplace_back(
createJsonValue("JUMPDEST", sourceIndex, i.location().start, i.location().end));
break;
case PushData:
collection.append(createJsonValue("PUSH data", sourceIndex, i.location().start, i.location().end, toStringInHex(i.data())));
collection.emplace_back(createJsonValue("PUSH data", sourceIndex, i.location().start, i.location().end, toStringInHex(i.data())));
break;
case VerbatimBytecode:
collection.append(createJsonValue("VERBATIM", sourceIndex, i.location().start, i.location().end, toHex(i.verbatimData())));
collection.emplace_back(createJsonValue("VERBATIM", sourceIndex, i.location().start, i.location().end, toHex(i.verbatimData())));
break;
default:
assertThrow(false, InvalidOpcode, "");
Expand All @@ -317,7 +316,8 @@ Json::Value Assembly::assemblyJSON(map<string, unsigned> const& _sourceIndices)

if (!m_data.empty() || !m_subs.empty())
{
Json::Value& data = root[".data"] = Json::objectValue;
root[".data"] = Json::object();
Json& data = root[".data"];
for (auto const& i: m_data)
if (u256(i.first) >= m_subs.size())
data[toStringInHex((u256)i.first)] = toHex(i.second);
Expand Down
7 changes: 3 additions & 4 deletions libevmasm/Assembly.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@

#include <libsolutil/Common.h>
#include <libsolutil/Assertions.h>
#include <libsolutil/JSON.h>
#include <libsolutil/Keccak256.h>

#include <libsolidity/interface/OptimiserSettings.h>

#include <json/json.h>

#include <iostream>
#include <sstream>
#include <memory>
Expand Down Expand Up @@ -151,7 +150,7 @@ class Assembly
) const;

/// Create a JSON representation of the assembly.
Json::Value assemblyJSON(
Json assemblyJSON(
std::map<std::string, unsigned> const& _sourceIndices = std::map<std::string, unsigned>()
) const;

Expand All @@ -170,7 +169,7 @@ class Assembly
unsigned bytesRequired(unsigned subTagSize) const;

private:
static Json::Value createJsonValue(
static Json createJsonValue(
std::string _name,
int _source,
int _begin,
Expand Down
3 changes: 1 addition & 2 deletions libsolidity/ast/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@
#include <liblangutil/SourceLocation.h>
#include <libevmasm/Instruction.h>
#include <libsolutil/FixedHash.h>
#include <libsolutil/JSON.h>
#include <libsolutil/LazyInit.h>

#include <json/json.h>

#include <range/v3/view/subrange.hpp>
#include <range/v3/view/map.hpp>

Expand Down
Loading

0 comments on commit 8b07ad3

Please sign in to comment.