Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic nlohmann-json integration. #14872

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ include(EthCcache)
# Let's find our dependencies
include(EthDependencies)
include(fmtlib)
include(jsoncpp)
include(range-v3)
include_directories(SYSTEM ${JSONCPP_INCLUDE_DIR})

find_package(Threads)

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

This file was deleted.

75 changes: 23 additions & 52 deletions cmake/templates/license.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -34,62 +34,33 @@ picosha2:
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

jsoncpp:
The JsonCpp library's source code, including accompanying documentation,
tests and demonstration applications, are licensed under the following
conditions...

The JsonCpp Authors explicitly disclaim copyright in all
jurisdictions which recognize such a disclaimer. In such jurisdictions,
this software is released into the Public Domain.

In jurisdictions which do not recognize Public Domain property (e.g. Germany as of
2010), this software is Copyright (c) 2007-2010 by The JsonCpp Authors, and is
released under the terms of the MIT License (see below).

In jurisdictions which recognize Public Domain property, the user of this
software may choose to accept it either as 1) Public Domain, 2) under the
conditions of the MIT License (see below), or 3) under the terms of dual
Public Domain/MIT License conditions described here, as they choose.

The MIT License is about as close to Public Domain as a license can get, and is
described in clear, concise terms at:

http://en.wikipedia.org/wiki/MIT_License

The full text of the MIT License follows:

========================================================================
Copyright (c) 2007-2010 The JsonCpp Authors

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is
nlohmann-json:
__ _____ _____ _____
__| | __| | | | JSON for Modern C++
| | |__ | | | | | | version 3.11.2
|_____|_____|_____|_|___| https://github.com/nlohmann/json

Licensed under the MIT License <http://opensource.org/licenses/MIT>.
SPDX-License-Identifier: MIT
Copyright (c) 2013-2022 Niels Lohmann <http://nlohmann.me>.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
========================================================================
(END LICENSE TEXT)

The MIT license is compatible with both the GPL and commercial
software, affording one all of the rights of Public Domain with the
minor nuisance of being required to keep the above copyright notice
and license text in the source code. Note also that by accepting the
Public Domain "license" you can re-license your copy using whatever
license you like.

scanner/token:
The liblangutil/{CharStream,Scanner,Token}.{h,cpp} files are derived from
Expand Down
2 changes: 1 addition & 1 deletion libevmasm/AbstractAssemblyStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class AbstractAssemblyStack
virtual std::string const* sourceMapping(std::string const& _contractName) const = 0;
virtual std::string const* runtimeSourceMapping(std::string const& _contractName) const = 0;

virtual Json::Value assemblyJSON(std::string const& _contractName) const = 0;
virtual Json assemblyJSON(std::string const& _contractName) const = 0;
virtual std::string assemblyString(std::string const& _contractName, StringMap const& _sourceCodes) const = 0;

virtual std::string const filesystemFriendlyName(std::string const& _contractName) const = 0;
Expand Down
94 changes: 47 additions & 47 deletions libevmasm/Assembly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ unsigned Assembly::codeSize(unsigned subTagSize) const
}
}

void Assembly::importAssemblyItemsFromJSON(Json::Value const& _code, std::vector<std::string> const& _sourceList)
void Assembly::importAssemblyItemsFromJSON(Json const& _code, std::vector<std::string> const& _sourceList)
{
solAssert(m_items.empty());
solRequire(_code.isArray(), AssemblyImportException, "Supplied JSON is not an array.");
solRequire(_code.is_array(), AssemblyImportException, "Supplied JSON is not an array.");
for (auto jsonItemIter = std::begin(_code); jsonItemIter != std::end(_code); ++jsonItemIter)
{
AssemblyItem const& newItem = m_items.emplace_back(createAssemblyItemFromJSON(*jsonItemIter, _sourceList));
Expand All @@ -98,11 +98,11 @@ void Assembly::importAssemblyItemsFromJSON(Json::Value const& _code, std::vector
}
}

AssemblyItem Assembly::createAssemblyItemFromJSON(Json::Value const& _json, std::vector<std::string> const& _sourceList)
AssemblyItem Assembly::createAssemblyItemFromJSON(Json const& _json, std::vector<std::string> const& _sourceList)
{
solRequire(_json.isObject(), AssemblyImportException, "Supplied JSON is not an object.");
solRequire(_json.is_object(), AssemblyImportException, "Supplied JSON is not an object.");
static std::set<std::string> const validMembers{"name", "begin", "end", "source", "value", "modifierDepth", "jumpType"};
for (std::string const& member: _json.getMemberNames())
for (auto const& [member, _]: _json.items())
solRequire(
validMembers.count(member),
AssemblyImportException,
Expand Down Expand Up @@ -427,11 +427,11 @@ std::string Assembly::assemblyString(
return tmp.str();
}

Json::Value Assembly::assemblyJSON(std::map<std::string, unsigned> const& _sourceIndices, bool _includeSourceList) const
Json Assembly::assemblyJSON(std::map<std::string, unsigned> const& _sourceIndices, bool _includeSourceList) const
{
Json::Value root;
root[".code"] = Json::arrayValue;
Json::Value& code = root[".code"];
Json root;
root[".code"] = Json::array();
Json& code = root[".code"];
for (AssemblyItem const& item: m_items)
{
int sourceIndex = -1;
Expand All @@ -443,7 +443,7 @@ Json::Value Assembly::assemblyJSON(std::map<std::string, unsigned> const& _sourc
}

auto [name, data] = item.nameAndData(m_evmVersion);
Json::Value jsonItem;
Json jsonItem;
jsonItem["name"] = name;
jsonItem["begin"] = item.location().start;
jsonItem["end"] = item.location().end;
Expand All @@ -459,32 +459,32 @@ Json::Value Assembly::assemblyJSON(std::map<std::string, unsigned> const& _sourc
if (!data.empty())
jsonItem["value"] = data;
jsonItem["source"] = sourceIndex;
code.append(std::move(jsonItem));
code.emplace_back(std::move(jsonItem));

if (item.type() == AssemblyItemType::Tag)
{
Json::Value jumpdest;
Json jumpdest;
jumpdest["name"] = "JUMPDEST";
jumpdest["begin"] = item.location().start;
jumpdest["end"] = item.location().end;
jumpdest["source"] = sourceIndex;
if (item.m_modifierDepth != 0)
jumpdest["modifierDepth"] = static_cast<int>(item.m_modifierDepth);
code.append(std::move(jumpdest));
code.emplace_back(std::move(jumpdest));
}
}
if (_includeSourceList)
{
root["sourceList"] = Json::arrayValue;
Json::Value& jsonSourceList = root["sourceList"];
root["sourceList"] = Json::array();
Json& jsonSourceList = root["sourceList"];
for (auto const& [name, index]: _sourceIndices)
jsonSourceList[index] = name;
}

if (!m_data.empty() || !m_subs.empty())
{
root[".data"] = Json::objectValue;
Json::Value& data = root[".data"];
root[".data"] = Json::object();
Json& data = root[".data"];
for (auto const& i: m_data)
if (u256(i.first) >= m_subs.size())
data[util::toHex(toBigEndian((u256)i.first), util::HexPrefix::DontAdd, util::HexCase::Upper)] = util::toHex(i.second);
Expand All @@ -504,83 +504,83 @@ Json::Value Assembly::assemblyJSON(std::map<std::string, unsigned> const& _sourc
}

std::pair<std::shared_ptr<Assembly>, std::vector<std::string>> Assembly::fromJSON(
Json::Value const& _json,
Json const& _json,
std::vector<std::string> const& _sourceList,
size_t _level
)
{
solRequire(_json.isObject(), AssemblyImportException, "Supplied JSON is not an object.");
solRequire(_json.is_object(), AssemblyImportException, "Supplied JSON is not an object.");
static std::set<std::string> const validMembers{".code", ".data", ".auxdata", "sourceList"};
for (std::string const& attribute: _json.getMemberNames())
for (auto const& [attribute, _]: _json.items())
solRequire(validMembers.count(attribute), AssemblyImportException, "Unknown attribute '" + attribute + "'.");

if (_level == 0)
{
if (_json.isMember("sourceList"))
if (_json.contains("sourceList"))
{
solRequire(_json["sourceList"].isArray(), AssemblyImportException, "Optional member 'sourceList' is not an array.");
for (Json::Value const& sourceName: _json["sourceList"])
solRequire(sourceName.isString(), AssemblyImportException, "The 'sourceList' array contains an item that is not a string.");
solRequire(_json["sourceList"].is_array(), AssemblyImportException, "Optional member 'sourceList' is not an array.");
for (Json const& sourceName: _json["sourceList"])
solRequire(sourceName.is_string(), AssemblyImportException, "The 'sourceList' array contains an item that is not a string.");
}
}
else
solRequire(
!_json.isMember("sourceList"),
!_json.contains("sourceList"),
AssemblyImportException,
"Member 'sourceList' may only be present in the root JSON object."
);

auto result = std::make_shared<Assembly>(EVMVersion{}, _level == 0 /* _creation */, "" /* _name */);
std::vector<std::string> parsedSourceList;
if (_json.isMember("sourceList"))
if (_json.contains("sourceList"))
{
solAssert(_level == 0);
solAssert(_sourceList.empty());
for (Json::Value const& sourceName: _json["sourceList"])
for (Json const& sourceName: _json["sourceList"])
{
solRequire(
std::find(parsedSourceList.begin(), parsedSourceList.end(), sourceName.asString()) == parsedSourceList.end(),
std::find(parsedSourceList.begin(), parsedSourceList.end(), sourceName.get<std::string>()) == parsedSourceList.end(),
AssemblyImportException,
"Items in 'sourceList' array are not unique."
);
parsedSourceList.emplace_back(sourceName.asString());
parsedSourceList.emplace_back(sourceName.get<std::string>());
}
}

solRequire(_json.isMember(".code"), AssemblyImportException, "Member '.code' is missing.");
solRequire(_json[".code"].isArray(), AssemblyImportException, "Member '.code' is not an array.");
for (Json::Value const& codeItem: _json[".code"])
solRequire(codeItem.isObject(), AssemblyImportException, "The '.code' array contains an item that is not an object.");
solRequire(_json.contains(".code"), AssemblyImportException, "Member '.code' is missing.");
solRequire(_json[".code"].is_array(), AssemblyImportException, "Member '.code' is not an array.");
for (Json const& codeItem: _json[".code"])
solRequire(codeItem.is_object(), AssemblyImportException, "The '.code' array contains an item that is not an object.");

result->importAssemblyItemsFromJSON(_json[".code"], _level == 0 ? parsedSourceList : _sourceList);

if (_json.isMember(".auxdata"))
if (_json.contains(".auxdata"))
{
solRequire(_json[".auxdata"].isString(), AssemblyImportException, "Optional member '.auxdata' is not a string.");
result->m_auxiliaryData = fromHex(_json[".auxdata"].asString());
solRequire(_json[".auxdata"].is_string(), AssemblyImportException, "Optional member '.auxdata' is not a string.");
result->m_auxiliaryData = fromHex(_json[".auxdata"].get<std::string>());
solRequire(!result->m_auxiliaryData.empty(), AssemblyImportException, "Optional member '.auxdata' is not a valid hexadecimal string.");
}

if (_json.isMember(".data"))
if (_json.contains(".data"))
{
solRequire(_json[".data"].isObject(), AssemblyImportException, "Optional member '.data' is not an object.");
Json::Value const& data = _json[".data"];
solRequire(_json[".data"].is_object(), AssemblyImportException, "Optional member '.data' is not an object.");
Json const& data = _json[".data"];
std::map<size_t, std::shared_ptr<Assembly>> subAssemblies;
for (Json::ValueConstIterator dataIter = data.begin(); dataIter != data.end(); dataIter++)
for (Json::const_iterator dataIter = data.begin(); dataIter != data.end(); dataIter++)
{
solAssert(dataIter.key().isString());
std::string dataItemID = dataIter.key().asString();
Json::Value const& dataItem = data[dataItemID];
if (dataItem.isString())
solAssert(dataIter.value().is_string());
std::string dataItemID = dataIter.value().get<std::string>();
Json const& dataItem = data[dataItemID];
if (dataItem.is_string())
{
solRequire(
dataItem.asString().empty() || !fromHex(dataItem.asString()).empty(),
dataItem.get<std::string>().empty() || !fromHex(dataItem.get<std::string>()).empty(),
AssemblyImportException,
"The value for key '" + dataItemID + "' inside '.data' is not a valid hexadecimal string."
);
result->m_data[h256(fromHex(dataItemID))] = fromHex(dataItem.asString());
result->m_data[h256(fromHex(dataItemID))] = fromHex(dataItem.get<std::string>());
}
else if (dataItem.isObject())
else if (dataItem.is_object())
{
size_t index{};
try
Expand Down
Loading