Paweł Bylica
9 years ago
30 changed files with 0 additions and 1608 deletions
@ -1,30 +0,0 @@ |
|||
cmake_policy(SET CMP0015 NEW) |
|||
set(CMAKE_AUTOMOC OFF) |
|||
|
|||
aux_source_directory(. SRC_LIST) |
|||
|
|||
include_directories(BEFORE ..) |
|||
include_directories(${JSON_RPC_CPP_INCLUDE_DIRS}) |
|||
include_directories(${CURL_INCLUDE_DIRS}) |
|||
include_directories(${V8_INCLUDE_DIRS}) |
|||
|
|||
set(EXECUTABLE ethconsole) |
|||
|
|||
file(GLOB HEADERS "*.h") |
|||
|
|||
add_executable(${EXECUTABLE} ${SRC_LIST} ${HEADERS}) |
|||
target_link_libraries(${EXECUTABLE} ${Boost_REGEX_LIBRARIES}) |
|||
target_link_libraries(${EXECUTABLE} ${CURL_LIBRARIES}) |
|||
|
|||
if (DEFINED WIN32 AND NOT DEFINED CMAKE_COMPILER_IS_MINGW) |
|||
eth_copy_dlls(${EXECUTABLE} CURL_DLLS) |
|||
endif() |
|||
target_link_libraries(${EXECUTABLE} jsconsole) |
|||
target_link_libraries(${EXECUTABLE} devcore) |
|||
|
|||
if (APPLE) |
|||
install(TARGETS ${EXECUTABLE} DESTINATION bin) |
|||
else() |
|||
eth_install_executable(${EXECUTABLE}) |
|||
endif() |
|||
|
@ -1,59 +0,0 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file main.cpp
|
|||
* @author Marek |
|||
* @date 2014 |
|||
*/ |
|||
|
|||
#include <string> |
|||
#include <libdevcore/FileSystem.h> |
|||
#include <libjsconsole/JSRemoteConsole.h> |
|||
using namespace std; |
|||
using namespace dev; |
|||
using namespace dev::eth; |
|||
|
|||
int main(int argc, char** argv) |
|||
{ |
|||
string remote = contentsString(getDataDir("web3") + "/session.url"); |
|||
if (remote.empty()) |
|||
remote = "http://localhost:8545"; |
|||
string sessionKey = contentsString(getDataDir("web3") + "/session.key"); |
|||
|
|||
for (int i = 1; i < argc; ++i) |
|||
{ |
|||
string arg = argv[i]; |
|||
if (arg == "--url" && i + 1 < argc) |
|||
remote = argv[++i]; |
|||
else if (arg == "--session-key" && i + 1 < argc) |
|||
sessionKey = argv[++i]; |
|||
else |
|||
{ |
|||
cerr << "Invalid argument: " << arg << endl; |
|||
exit(-1); |
|||
} |
|||
} |
|||
|
|||
JSRemoteConsole console(remote); |
|||
|
|||
if (!sessionKey.empty()) |
|||
console.eval("web3.admin.setSessionKey('" + sessionKey + "')"); |
|||
|
|||
while (true) |
|||
console.readExpression(); |
|||
|
|||
return 0; |
|||
} |
@ -1,33 +0,0 @@ |
|||
cmake_policy(SET CMP0015 NEW) |
|||
set(CMAKE_AUTOMOC OFF) |
|||
|
|||
aux_source_directory(. SRC_LIST) |
|||
|
|||
include_directories(BEFORE ${V8_INCLUDE_DIRS}) |
|||
include_directories(BEFORE ..) |
|||
if (READLINE_FOUND) |
|||
include_directories(${READLINE_INCLUDE_DIRS}) |
|||
endif() |
|||
include_directories(${JSON_RPC_CPP_INCLUDE_DIRS}) |
|||
include_directories(${CURL_INCLUDE_DIRS}) |
|||
|
|||
set(EXECUTABLE jsconsole) |
|||
|
|||
file(GLOB HEADERS "*.h") |
|||
|
|||
add_library(${EXECUTABLE} ${SRC_LIST} ${HEADERS}) |
|||
|
|||
target_link_libraries(${EXECUTABLE} jsengine) |
|||
target_link_libraries(${EXECUTABLE} devcore) |
|||
if (READLINE_FOUND) |
|||
target_link_libraries(${EXECUTABLE} ${READLINE_LIBRARIES}) |
|||
endif() |
|||
target_link_libraries(${EXECUTABLE} ${JSON_RPC_CPP_SERVER_LIBRARIES}) |
|||
|
|||
target_link_libraries(${EXECUTABLE} ${CURL_LIBRARIES}) |
|||
if (DEFINED WIN32 AND NOT DEFINED CMAKE_COMPILER_IS_MINGW) |
|||
eth_copy_dlls(${EXECUTABLE} CURL_DLLS) |
|||
endif() |
|||
|
|||
install( TARGETS ${EXECUTABLE} RUNTIME DESTINATION bin ARCHIVE DESTINATION lib LIBRARY DESTINATION lib ) |
|||
install( FILES ${HEADERS} DESTINATION include/${EXECUTABLE} ) |
@ -1,66 +0,0 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file CURLRequest.cpp
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Ethereum client. |
|||
*/ |
|||
|
|||
#include "CURLRequest.h" |
|||
|
|||
using namespace std; |
|||
|
|||
static size_t write_data(void *buffer, size_t elementSize, size_t numberOfElements, void *userp) |
|||
{ |
|||
static_cast<stringstream *>(userp)->write((const char *)buffer, elementSize * numberOfElements); |
|||
return elementSize * numberOfElements; |
|||
} |
|||
|
|||
void CURLRequest::commonCURLPreparation() |
|||
{ |
|||
m_resultBuffer.str(""); |
|||
curl_easy_setopt(m_curl, CURLOPT_URL, (m_url + "?").c_str()); |
|||
curl_easy_setopt(m_curl, CURLOPT_FOLLOWLOCATION, 1L); |
|||
curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, write_data); |
|||
curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, &m_resultBuffer); |
|||
} |
|||
|
|||
std::tuple<long, string> CURLRequest::commonCURLPerform() |
|||
{ |
|||
CURLcode res = curl_easy_perform(m_curl); |
|||
if (res != CURLE_OK) { |
|||
throw runtime_error(curl_easy_strerror(res)); |
|||
} |
|||
long httpCode = 0; |
|||
curl_easy_getinfo(m_curl, CURLINFO_RESPONSE_CODE, &httpCode); |
|||
return make_tuple(httpCode, m_resultBuffer.str()); |
|||
} |
|||
|
|||
std::tuple<long, string> CURLRequest::post() |
|||
{ |
|||
commonCURLPreparation(); |
|||
curl_easy_setopt(m_curl, CURLOPT_POSTFIELDS, m_body.c_str()); |
|||
|
|||
struct curl_slist *headerList = NULL; |
|||
headerList = curl_slist_append(headerList, "Content-Type: application/json"); |
|||
curl_easy_setopt(m_curl, CURLOPT_HTTPHEADER, headerList); |
|||
|
|||
auto result = commonCURLPerform(); |
|||
|
|||
curl_slist_free_all(headerList); |
|||
return result; |
|||
} |
@ -1,58 +0,0 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file CURLRequest.h
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Ethereum client. |
|||
*/ |
|||
|
|||
// based on http://stackoverflow.com/questions/1011339/how-do-you-make-a-http-request-with-c/27026683#27026683
|
|||
|
|||
#pragma once |
|||
|
|||
#include <stdio.h> |
|||
#include <sstream> |
|||
#include <unordered_map> |
|||
#include <curl/curl.h> |
|||
|
|||
class CURLRequest |
|||
{ |
|||
public: |
|||
CURLRequest(): m_curl(curl_easy_init()) {} |
|||
~CURLRequest() |
|||
{ |
|||
if (m_curl) |
|||
curl_easy_cleanup(m_curl); |
|||
} |
|||
|
|||
void setUrl(std::string _url) { m_url = _url; } |
|||
void setBody(std::string _body) { m_body = _body; } |
|||
|
|||
std::tuple<long, std::string> post(); |
|||
|
|||
private: |
|||
std::string m_url; |
|||
std::string m_body; |
|||
|
|||
CURL* m_curl; |
|||
std::stringstream m_resultBuffer; |
|||
|
|||
void commonCURLPreparation(); |
|||
std::tuple<long, std::string> commonCURLPerform(); |
|||
}; |
|||
|
|||
|
@ -1,24 +0,0 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file JSConsole.cpp
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Ethereum client. |
|||
*/ |
|||
|
|||
|
|||
#include "JSConsole.h" |
@ -1,113 +0,0 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file JSConsole.h
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Ethereum client. |
|||
*/ |
|||
|
|||
#pragma once |
|||
|
|||
#include <libdevcore/Log.h> |
|||
|
|||
#if ETH_READLINE |
|||
#include <readline/readline.h> |
|||
#include <readline/history.h> |
|||
#endif |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
|
|||
template<typename Engine, typename Printer> |
|||
class JSConsole |
|||
{ |
|||
public: |
|||
JSConsole(): m_engine(Engine()), m_printer(Printer(m_engine)) {} |
|||
~JSConsole() {} |
|||
|
|||
void readExpression() const |
|||
{ |
|||
std::string cmd = ""; |
|||
g_logPost = [](std::string const& a, char const*) |
|||
{ |
|||
std::cout << "\r \r" << a << std::endl << std::flush; |
|||
#if ETH_READLINE |
|||
rl_forced_update_display(); |
|||
#endif |
|||
}; |
|||
|
|||
bool isEmpty = true; |
|||
int openBrackets = 0; |
|||
do { |
|||
std::string rl; |
|||
#if ETH_READLINE |
|||
char* buff = readline(promptForIndentionLevel(openBrackets).c_str()); |
|||
if (buff) |
|||
{ |
|||
rl = std::string(buff); |
|||
free(buff); |
|||
} |
|||
#else |
|||
std::cout << promptForIndentionLevel(openBrackets) << std::flush; |
|||
std::getline(std::cin, rl); |
|||
#endif |
|||
isEmpty = rl.empty(); |
|||
//@todo this should eventually check the structure of the input, since unmatched
|
|||
// brackets in strings will fail to execute the input now.
|
|||
if (!isEmpty) |
|||
{ |
|||
cmd += rl; |
|||
int open = 0; |
|||
for (char c: {'{', '[', '('}) |
|||
open += std::count(cmd.begin(), cmd.end(), c); |
|||
int closed = 0; |
|||
for (char c: {'}', ']', ')'}) |
|||
closed += std::count(cmd.begin(), cmd.end(), c); |
|||
openBrackets = open - closed; |
|||
} |
|||
} while (openBrackets > 0); |
|||
|
|||
if (!isEmpty) |
|||
{ |
|||
#if ETH_READLINE |
|||
add_history(cmd.c_str()); |
|||
#endif |
|||
auto value = m_engine.eval(cmd.c_str()); |
|||
std::string result = m_printer.prettyPrint(value).cstr(); |
|||
std::cout << result << std::endl; |
|||
} |
|||
} |
|||
|
|||
void eval(std::string const& _expression) { m_engine.eval(_expression.c_str()); } |
|||
|
|||
protected: |
|||
Engine m_engine; |
|||
Printer m_printer; |
|||
|
|||
virtual std::string promptForIndentionLevel(int _i) const |
|||
{ |
|||
if (_i == 0) |
|||
return "> "; |
|||
|
|||
return std::string((_i + 1) * 2, ' '); |
|||
} |
|||
}; |
|||
|
|||
} |
|||
} |
@ -1,34 +0,0 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file JSLocalConsole.cpp
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Ethereum client. |
|||
*/ |
|||
|
|||
#include <iostream> |
|||
#include "JSLocalConsole.h" |
|||
#include "JSV8Connector.h" |
|||
|
|||
using namespace std; |
|||
using namespace dev; |
|||
using namespace dev::eth; |
|||
|
|||
JSLocalConsole::JSLocalConsole() |
|||
{ |
|||
m_jsonrpcConnector.reset(new JSV8Connector(m_engine)); |
|||
} |
@ -1,50 +0,0 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file JSLocalConsole.h
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Ethereum client. |
|||
*/ |
|||
|
|||
#pragma once |
|||
|
|||
#include <libjsengine/JSV8Engine.h> |
|||
#include <libjsengine/JSV8Printer.h> |
|||
#include "JSConsole.h" |
|||
|
|||
class WebThreeStubServer; |
|||
namespace jsonrpc { class AbstractServerConnector; } |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
|
|||
class JSLocalConsole: public JSConsole<JSV8Engine, JSV8Printer> |
|||
{ |
|||
public: |
|||
JSLocalConsole(); |
|||
virtual ~JSLocalConsole() {} |
|||
|
|||
jsonrpc::AbstractServerConnector* connector() { return m_jsonrpcConnector.get(); } |
|||
|
|||
private: |
|||
std::unique_ptr<jsonrpc::AbstractServerConnector> m_jsonrpcConnector; |
|||
}; |
|||
|
|||
} |
|||
} |
@ -1,23 +0,0 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file JSRemoteConsole.cpp
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Ethereum client. |
|||
*/ |
|||
|
|||
#include "JSRemoteConsole.h" |
@ -1,48 +0,0 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file JSRemoteConsole.h
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Ethereum client. |
|||
*/ |
|||
|
|||
#pragma once |
|||
|
|||
#include <libjsengine/JSV8Engine.h> |
|||
#include <libjsengine/JSV8Printer.h> |
|||
#include "JSV8RemoteConnector.h" |
|||
#include "JSConsole.h" |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
|
|||
class JSRemoteConsole: public JSConsole<JSV8Engine, JSV8Printer> |
|||
{ |
|||
|
|||
public: |
|||
JSRemoteConsole(std::string _url): m_connector(m_engine, _url) {} |
|||
virtual ~JSRemoteConsole() {} |
|||
|
|||
private: |
|||
JSV8RemoteConnector m_connector; |
|||
|
|||
}; |
|||
|
|||
} |
|||
} |
@ -1,54 +0,0 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file JSV8Connector.cpp
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Ethereum client. |
|||
*/ |
|||
|
|||
#include "JSV8Connector.h" |
|||
|
|||
using namespace std; |
|||
using namespace dev; |
|||
using namespace dev::eth; |
|||
|
|||
bool JSV8Connector::StartListening() |
|||
{ |
|||
return true; |
|||
} |
|||
|
|||
bool JSV8Connector::StopListening() |
|||
{ |
|||
return true; |
|||
} |
|||
|
|||
bool JSV8Connector::SendResponse(std::string const& _response, void* _addInfo) |
|||
{ |
|||
(void)_addInfo; |
|||
m_lastResponse = _response; |
|||
return true; |
|||
} |
|||
|
|||
void JSV8Connector::onSend(char const* payload) |
|||
{ |
|||
OnRequest(payload, NULL); |
|||
} |
|||
|
|||
JSV8Connector::~JSV8Connector() |
|||
{ |
|||
StopListening(); |
|||
} |
@ -1,55 +0,0 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file JSV8Connector.h
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Ethereum client. |
|||
*/ |
|||
|
|||
#pragma once |
|||
|
|||
#include <string> |
|||
#include <jsonrpccpp/server/abstractserverconnector.h> |
|||
#include <libjsengine/JSV8RPC.h> |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
|
|||
class JSV8Connector: public jsonrpc::AbstractServerConnector, private JSV8RPC |
|||
{ |
|||
public: |
|||
JSV8Connector(JSV8Engine const& _engine): JSV8RPC(_engine) {} |
|||
virtual ~JSV8Connector(); |
|||
|
|||
// implement AbstractServerConnector interface
|
|||
bool StartListening() override; |
|||
bool StopListening() override; |
|||
bool SendResponse(std::string const& _response, void* _addInfo = nullptr) override; |
|||
|
|||
private: |
|||
// implement JSV8RPC interface
|
|||
void onSend(char const* _payload) override; |
|||
|
|||
char const* lastResponse() const override { return m_lastResponse.c_str(); } |
|||
|
|||
std::string m_lastResponse = R"({"id": 1, "jsonrpc": "2.0", "error": "Uninitalized JSV8RPC!"})"; |
|||
}; |
|||
|
|||
} |
|||
} |
@ -1,35 +0,0 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file JSV8RemoteConnector.cpp
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Connector from the standalone javascript console to a remote RPC node. |
|||
*/ |
|||
|
|||
#include "JSV8RemoteConnector.h" |
|||
|
|||
using namespace dev; |
|||
using namespace dev::eth; |
|||
|
|||
void JSV8RemoteConnector::onSend(char const* _payload) |
|||
{ |
|||
m_request.setUrl(m_url); |
|||
m_request.setBody(_payload); |
|||
long code; |
|||
tie(code, m_lastResponse) = m_request.post(); |
|||
(void)code; |
|||
} |
@ -1,52 +0,0 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file JSV8RemoteConnector.cpp
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Connector from the standalone javascript console to a remote RPC node. |
|||
*/ |
|||
|
|||
#pragma once |
|||
|
|||
#include <libjsengine/JSV8RPC.h> |
|||
#include "CURLRequest.h" |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
|
|||
class JSV8RemoteConnector: private JSV8RPC |
|||
{ |
|||
|
|||
public: |
|||
JSV8RemoteConnector(JSV8Engine const& _engine, std::string _url): JSV8RPC(_engine), m_url(_url) {} |
|||
virtual ~JSV8RemoteConnector() {} |
|||
|
|||
private: |
|||
// implement JSV8RPC interface
|
|||
void onSend(char const* _payload) override; |
|||
const char* lastResponse() const override { return m_lastResponse.c_str(); } |
|||
|
|||
private: |
|||
std::string m_url; |
|||
std::string m_lastResponse = R"({"id": 1, "jsonrpc": "2.0", "error": "Uninitalized JSV8RPC!"})"; |
|||
CURLRequest m_request; |
|||
}; |
|||
|
|||
} |
|||
} |
@ -1,30 +0,0 @@ |
|||
cmake_policy(SET CMP0015 NEW) |
|||
set(CMAKE_AUTOMOC OFF) |
|||
|
|||
aux_source_directory(. SRC_LIST) |
|||
|
|||
include_directories(BEFORE ${V8_INCLUDE_DIRS}) |
|||
include_directories(BEFORE ..) |
|||
|
|||
set(EXECUTABLE jsengine) |
|||
|
|||
file(GLOB HEADERS "*.h") |
|||
|
|||
include(EthUtils) |
|||
eth_add_resources("${CMAKE_CURRENT_SOURCE_DIR}/JSResources.cmake" "JSRES") |
|||
add_library(${EXECUTABLE} ${SRC_LIST} ${HEADERS} ${JSRES}) |
|||
|
|||
add_dependencies(${EXECUTABLE} BuildInfo.h) |
|||
|
|||
# macos brew version of v8 needs to be compiled with libstdc++ |
|||
# it also needs to be dynamic library |
|||
# xcode needs libstdc++ to be explicitly set as it's attribute |
|||
if (APPLE) |
|||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libstdc++") |
|||
set_property(TARGET ${EXECUTABLE} PROPERTY XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY "libstdc++") |
|||
endif() |
|||
|
|||
target_link_libraries(${EXECUTABLE} ${V8_LIBRARIES}) |
|||
|
|||
install( TARGETS ${EXECUTABLE} RUNTIME DESTINATION bin ARCHIVE DESTINATION lib LIBRARY DESTINATION lib ) |
|||
install( FILES ${HEADERS} DESTINATION include/${EXECUTABLE} ) |
@ -1,4 +0,0 @@ |
|||
setTimeout = function () { |
|||
console.error("setTimeout not available in this environment."); |
|||
}; |
|||
|
@ -1,36 +0,0 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file JSEngine.cpp
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Ethereum client. |
|||
*/ |
|||
|
|||
#include <string.h> |
|||
#include <stdlib.h> |
|||
#include "JSEngine.h" |
|||
|
|||
using namespace dev; |
|||
using namespace dev::eth; |
|||
|
|||
JSString::JSString(char const* _cstr): m_cstr(strdup(_cstr)) {} |
|||
|
|||
JSString::~JSString() |
|||
{ |
|||
if (m_cstr) |
|||
free(m_cstr); |
|||
} |
@ -1,66 +0,0 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file JSEngine.h
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Ethereum client. |
|||
*/ |
|||
|
|||
#pragma once |
|||
#include <exception> |
|||
|
|||
/// Do not use libstd headers here, it will break on MacOS.
|
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
|
|||
class JSException: public std::exception {}; |
|||
#if defined(_MSC_VER) |
|||
class JSPrintException: public JSException { char const* what() const { return "Cannot print expression!"; } }; |
|||
#else |
|||
class JSPrintException: public JSException { char const* what() const noexcept { return "Cannot print expression!"; } }; |
|||
#endif |
|||
|
|||
class JSString |
|||
{ |
|||
public: |
|||
JSString(char const* _cstr); |
|||
~JSString(); |
|||
char const* cstr() const { return m_cstr; } |
|||
|
|||
private: |
|||
char* m_cstr; |
|||
}; |
|||
|
|||
class JSValue |
|||
{ |
|||
public: |
|||
virtual JSString toString() const = 0; |
|||
}; |
|||
|
|||
template <typename T> |
|||
class JSEngine |
|||
{ |
|||
public: |
|||
// should be used to evalute javascript expression
|
|||
virtual T eval(char const* _cstr, char const* _origin = "(shell)") const = 0; |
|||
}; |
|||
|
|||
} |
|||
} |
@ -1,23 +0,0 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file JSPrinter.cpp
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Ethereum client. |
|||
*/ |
|||
|
|||
#include "JSPrinter.h" |
@ -1,43 +0,0 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file JSPrinter.h
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Ethereum client. |
|||
*/ |
|||
|
|||
#pragma once |
|||
|
|||
#include "JSEngine.h" |
|||
|
|||
/// Do not use libstd headers here, it will break on MacOS.
|
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
|
|||
template <typename T> |
|||
class JSPrinter |
|||
{ |
|||
public: |
|||
virtual JSString print(T const& _value) const { return _value.toString(); } |
|||
virtual JSString prettyPrint(T const& _value) const { return print(_value); } |
|||
}; |
|||
|
|||
} |
|||
} |
@ -1,9 +0,0 @@ |
|||
|
|||
set(web3 "${CMAKE_CURRENT_LIST_DIR}/../libjsqrc/ethereumjs/dist/web3.js") |
|||
set(admin "${CMAKE_CURRENT_LIST_DIR}/../libjsqrc/admin.js") |
|||
set(pretty_print "${CMAKE_CURRENT_LIST_DIR}/PrettyPrint.js") |
|||
set(common "${CMAKE_CURRENT_LIST_DIR}/Common.js") |
|||
|
|||
set(ETH_RESOURCE_NAME "JSEngineResources") |
|||
set(ETH_RESOURCE_LOCATION "${CMAKE_CURRENT_BINARY_DIR}") |
|||
set(ETH_RESOURCES "web3" "pretty_print" "common" "admin") |
@ -1,237 +0,0 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file JSV8Engine.cpp
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Ethereum client. |
|||
*/ |
|||
|
|||
#include <memory> |
|||
#include <iostream> |
|||
#include <fstream> |
|||
#include "JSV8Engine.h" |
|||
#include "JSV8Printer.h" |
|||
#include "libjsengine/JSEngineResources.hpp" |
|||
#include "BuildInfo.h" |
|||
|
|||
#define TO_STRING_HELPER(s) #s |
|||
#define TO_STRING(s) TO_STRING_HELPER(s) |
|||
|
|||
using namespace std; |
|||
using namespace dev; |
|||
using namespace dev::eth; |
|||
using namespace v8; |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
|
|||
static char const* toCString(String::Utf8Value const& _value) |
|||
{ |
|||
if (*_value) |
|||
return *_value; |
|||
throw JSPrintException(); |
|||
} |
|||
|
|||
// from: https://github.com/v8/v8-git-mirror/blob/master/samples/shell.cc
|
|||
// v3.15 from: https://chromium.googlesource.com/v8/v8.git/+/3.14.5.9/samples/shell.cc
|
|||
void reportException(TryCatch* _tryCatch) |
|||
{ |
|||
HandleScope handle_scope; |
|||
String::Utf8Value exception(_tryCatch->Exception()); |
|||
char const* exceptionString = toCString(exception); |
|||
Handle<Message> message = _tryCatch->Message(); |
|||
|
|||
// V8 didn't provide any extra information about this error; just
|
|||
// print the exception.
|
|||
if (message.IsEmpty()) |
|||
printf("%s\n", exceptionString); |
|||
else |
|||
{ |
|||
// Print (filename):(line number): (message).
|
|||
String::Utf8Value filename(message->GetScriptResourceName()); |
|||
char const* filenameString = toCString(filename); |
|||
int linenum = message->GetLineNumber(); |
|||
printf("%s:%i: %s\n", filenameString, linenum, exceptionString); |
|||
|
|||
// Print line of source code.
|
|||
String::Utf8Value sourceline(message->GetSourceLine()); |
|||
char const* sourcelineString = toCString(sourceline); |
|||
printf("%s\n", sourcelineString); |
|||
|
|||
// Print wavy underline (GetUnderline is deprecated).
|
|||
int start = message->GetStartColumn(); |
|||
for (int i = 0; i < start; i++) |
|||
printf(" "); |
|||
|
|||
int end = message->GetEndColumn(); |
|||
|
|||
for (int i = start; i < end; i++) |
|||
printf("^"); |
|||
|
|||
printf("\n"); |
|||
|
|||
String::Utf8Value stackTrace(_tryCatch->StackTrace()); |
|||
if (stackTrace.length() > 0) |
|||
{ |
|||
char const* stackTraceString = toCString(stackTrace); |
|||
printf("%s\n", stackTraceString); |
|||
} |
|||
} |
|||
} |
|||
|
|||
Handle<Value> consoleLog(Arguments const& _args) |
|||
{ |
|||
Local<External> wrap = Local<External>::Cast(_args.Data()); |
|||
auto engine = reinterpret_cast<JSV8Engine const*>(wrap->Value()); |
|||
JSV8Printer printer(*engine); |
|||
for (int i = 0; i < _args.Length(); ++i) |
|||
printf("%s\n", printer.prettyPrint(_args[i]).cstr()); |
|||
return Undefined(); |
|||
} |
|||
|
|||
Handle<Value> loadScript(Arguments const& _args) |
|||
{ |
|||
Local<External> wrap = Local<External>::Cast(_args.Data()); |
|||
auto engine = reinterpret_cast<JSV8Engine const*>(wrap->Value()); |
|||
|
|||
if (_args.Length() < 1) |
|||
return v8::ThrowException(v8::String::New("Missing file name.")); |
|||
if (_args[0].IsEmpty() || _args[0]->IsUndefined()) |
|||
return v8::ThrowException(v8::String::New("Invalid file name.")); |
|||
v8::String::Utf8Value fileName(_args[0]); |
|||
if (fileName.length() == 0) |
|||
return v8::ThrowException(v8::String::New("Invalid file name.")); |
|||
|
|||
std::ifstream is(*fileName, std::ifstream::binary); |
|||
if (!is) |
|||
return v8::ThrowException(v8::String::New("Error opening file.")); |
|||
|
|||
string contents; |
|||
is.seekg(0, is.end); |
|||
streamoff length = is.tellg(); |
|||
if (length > 0) |
|||
{ |
|||
is.seekg(0, is.beg); |
|||
contents.resize(length); |
|||
is.read(const_cast<char*>(contents.data()), length); |
|||
} |
|||
|
|||
return engine->eval(contents.data(), *fileName).value(); |
|||
} |
|||
|
|||
class JSV8Scope |
|||
{ |
|||
public: |
|||
JSV8Scope(): |
|||
m_handleScope(), |
|||
m_context(Context::New(NULL, ObjectTemplate::New())), |
|||
m_contextScope(m_context) |
|||
{ |
|||
m_context->Enter(); |
|||
} |
|||
|
|||
~JSV8Scope() |
|||
{ |
|||
m_context->Exit(); |
|||
m_context.Dispose(); |
|||
} |
|||
|
|||
Persistent <Context> const& context() const { return m_context; } |
|||
|
|||
private: |
|||
HandleScope m_handleScope; |
|||
Persistent <Context> m_context; |
|||
Context::Scope m_contextScope; |
|||
}; |
|||
|
|||
} |
|||
} |
|||
|
|||
JSString JSV8Value::toString() const |
|||
{ |
|||
if (m_value.IsEmpty()) |
|||
return ""; |
|||
|
|||
else if (m_value->IsUndefined()) |
|||
return "undefined"; |
|||
|
|||
String::Utf8Value str(m_value); |
|||
return toCString(str); |
|||
} |
|||
|
|||
JSV8Engine::JSV8Engine(): m_scope(new JSV8Scope()) |
|||
{ |
|||
JSEngineResources resources; |
|||
eval("env = typeof(env) === 'undefined' ? {} : env; env.os = '" TO_STRING(ETH_BUILD_PLATFORM) "'"); |
|||
string common = resources.loadResourceAsString("common"); |
|||
string web3 = resources.loadResourceAsString("web3"); |
|||
string admin = resources.loadResourceAsString("admin"); |
|||
eval(common.c_str()); |
|||
eval(web3.c_str()); |
|||
eval("web3 = require('web3');"); |
|||
eval(admin.c_str()); |
|||
|
|||
auto consoleTemplate = ObjectTemplate::New(); |
|||
|
|||
Local<FunctionTemplate> consoleLogFunction = FunctionTemplate::New(consoleLog, External::New(this)); |
|||
consoleTemplate->Set(String::New("debug"), consoleLogFunction); |
|||
consoleTemplate->Set(String::New("log"), consoleLogFunction); |
|||
consoleTemplate->Set(String::New("error"), consoleLogFunction); |
|||
context()->Global()->Set(String::New("console"), consoleTemplate->NewInstance()); |
|||
|
|||
Local<FunctionTemplate> loadScriptFunction = FunctionTemplate::New(loadScript, External::New(this)); |
|||
context()->Global()->Set(String::New("loadScript"), loadScriptFunction->GetFunction()); |
|||
} |
|||
|
|||
JSV8Engine::~JSV8Engine() |
|||
{ |
|||
delete m_scope; |
|||
} |
|||
|
|||
JSV8Value JSV8Engine::eval(char const* _cstr, char const* _origin) const |
|||
{ |
|||
TryCatch tryCatch; |
|||
Local<String> source = String::New(_cstr); |
|||
Local<String> name(String::New(_origin)); |
|||
ScriptOrigin origin(name); |
|||
Handle<Script> script = Script::Compile(source, &origin); |
|||
|
|||
// Make sure to wrap the exception in a new handle because
|
|||
// the handle returned from the TryCatch is destroyed
|
|||
if (script.IsEmpty()) |
|||
{ |
|||
reportException(&tryCatch); |
|||
return Exception::Error(Local<String>::New(tryCatch.Message()->Get())); |
|||
} |
|||
|
|||
auto result = script->Run(); |
|||
|
|||
if (result.IsEmpty()) |
|||
{ |
|||
reportException(&tryCatch); |
|||
return Exception::Error(Local<String>::New(tryCatch.Message()->Get())); |
|||
} |
|||
|
|||
return result; |
|||
} |
|||
|
|||
Handle<Context> const& JSV8Engine::context() const |
|||
{ |
|||
return m_scope->context(); |
|||
} |
@ -1,65 +0,0 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file JSV8Engine.h
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Ethereum client. |
|||
*/ |
|||
|
|||
#pragma once |
|||
|
|||
#pragma clang diagnostic push |
|||
#pragma clang diagnostic ignored "-Wunused-parameter" |
|||
#include <v8.h> |
|||
#pragma clang diagnostic pop |
|||
#include "JSEngine.h" |
|||
|
|||
/// Do not use libstd headers here, it will break on MacOS.
|
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
|
|||
class JSV8Env; |
|||
class JSV8Scope; |
|||
|
|||
class JSV8Value: public JSValue |
|||
{ |
|||
public: |
|||
JSV8Value(v8::Handle<v8::Value> _value): m_value(_value) {} |
|||
JSString toString() const; |
|||
v8::Handle<v8::Value> const& value() const { return m_value; } |
|||
|
|||
private: |
|||
v8::Handle<v8::Value> m_value; |
|||
}; |
|||
|
|||
class JSV8Engine: public JSEngine<JSV8Value> |
|||
{ |
|||
public: |
|||
JSV8Engine(); |
|||
virtual ~JSV8Engine(); |
|||
JSV8Value eval(char const* _cstr, char const* _origin = "(shell)") const; |
|||
v8::Handle<v8::Context> const& context() const; |
|||
|
|||
private: |
|||
JSV8Scope* m_scope; |
|||
}; |
|||
|
|||
} |
|||
} |
@ -1,48 +0,0 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file JSV8Printer.cpp
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Ethereum client. |
|||
*/ |
|||
|
|||
#include <string> |
|||
#include "JSV8Printer.h" |
|||
#include "libjsengine/JSEngineResources.hpp" |
|||
|
|||
using namespace std; |
|||
using namespace dev; |
|||
using namespace eth; |
|||
|
|||
JSV8Printer::JSV8Printer(JSV8Engine const& _engine): m_engine(_engine) |
|||
{ |
|||
JSEngineResources resources; |
|||
string prettyPrint = resources.loadResourceAsString("pretty_print"); |
|||
m_engine.eval(prettyPrint.c_str()); |
|||
} |
|||
|
|||
JSString JSV8Printer::prettyPrint(JSV8Value const& _value) const |
|||
{ |
|||
v8::Local<v8::String> pp = v8::String::New("prettyPrint"); |
|||
v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(m_engine.context()->Global()->Get(pp)); |
|||
v8::Local<v8::Value> values[1] = {v8::Local<v8::Value>::New(_value.value())}; |
|||
v8::Local<v8::Value> res = func->Call(func, 1, values); |
|||
v8::String::Utf8Value str(res); |
|||
if (*str) |
|||
return *str; |
|||
throw JSPrintException(); |
|||
} |
@ -1,45 +0,0 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file JSV8Printer.h
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Ethereum client. |
|||
*/ |
|||
|
|||
#pragma once |
|||
|
|||
#include "JSPrinter.h" |
|||
#include "JSV8Engine.h" |
|||
|
|||
/// Do not use libstd headers here, it will break on MacOS.
|
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
|
|||
class JSV8Printer: public JSPrinter<JSV8Value> |
|||
{ |
|||
public: |
|||
JSV8Printer(JSV8Engine const& _engine); |
|||
JSString prettyPrint(JSV8Value const& _value) const; |
|||
private: |
|||
JSV8Engine const& m_engine; |
|||
}; |
|||
|
|||
} |
|||
} |
@ -1,103 +0,0 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file JSV8RPC.cpp
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Ethereum client. |
|||
*/ |
|||
|
|||
#include "JSV8RPC.h" |
|||
|
|||
using namespace dev; |
|||
using namespace dev::eth; |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
|
|||
v8::Handle<v8::Value> JSV8RPCSend(v8::Arguments const& _args) |
|||
{ |
|||
v8::Local<v8::String> JSON = v8::String::New("JSON"); |
|||
v8::Local<v8::String> parse = v8::String::New("parse"); |
|||
v8::Local<v8::String> stringify = v8::String::New("stringify"); |
|||
v8::Handle<v8::Object> jsonObject = v8::Handle<v8::Object>::Cast(v8::Context::GetCurrent()->Global()->Get(JSON)); |
|||
v8::Handle<v8::Function> parseFunc = v8::Handle<v8::Function>::Cast(jsonObject->Get(parse)); |
|||
v8::Handle<v8::Function> stringifyFunc = v8::Handle<v8::Function>::Cast(jsonObject->Get(stringify)); |
|||
|
|||
v8::Local<v8::Object> self = _args.Holder(); |
|||
v8::Local<v8::External> wrap = v8::Local<v8::External>::Cast(self->GetInternalField(0)); |
|||
JSV8RPC* that = static_cast<JSV8RPC*>(wrap->Value()); |
|||
v8::Local<v8::Value> vals[1] = {_args[0]->ToObject()}; |
|||
v8::Local<v8::Value> stringifiedArg = stringifyFunc->Call(stringifyFunc, 1, vals); |
|||
v8::String::Utf8Value str(stringifiedArg); |
|||
try |
|||
{ |
|||
that->onSend(*str); |
|||
} |
|||
catch (std::exception const& _e) |
|||
{ |
|||
return v8::ThrowException(v8::String::New(_e.what())); |
|||
} |
|||
catch (...) |
|||
{ |
|||
return v8::ThrowException(v8::String::New("Unknown C++ exception.")); |
|||
} |
|||
|
|||
v8::Local<v8::Value> values[1] = {v8::String::New(that->lastResponse())}; |
|||
return parseFunc->Call(parseFunc, 1, values); |
|||
} |
|||
|
|||
v8::Handle<v8::Value> JSV8RPCSendAsync(v8::Arguments const& _args) |
|||
{ |
|||
// This is synchronous, but uses the callback-interface.
|
|||
|
|||
auto parsed = v8::Local<v8::Value>::New(JSV8RPCSend(_args)); |
|||
v8::Handle<v8::Function> callback = v8::Handle<v8::Function>::Cast(_args[1]); |
|||
v8::Local<v8::Value> callbackArgs[2] = {v8::Local<v8::Value>::New(v8::Null()), parsed}; |
|||
callback->Call(callback, 2, callbackArgs); |
|||
|
|||
return v8::Undefined(); |
|||
} |
|||
|
|||
} |
|||
} |
|||
|
|||
JSV8RPC::JSV8RPC(JSV8Engine const& _engine): m_engine(_engine) |
|||
{ |
|||
v8::HandleScope scope; |
|||
v8::Local<v8::ObjectTemplate> rpcTemplate = v8::ObjectTemplate::New(); |
|||
rpcTemplate->SetInternalFieldCount(1); |
|||
rpcTemplate->Set( |
|||
v8::String::New("send"), |
|||
v8::FunctionTemplate::New(JSV8RPCSend) |
|||
); |
|||
rpcTemplate->Set( |
|||
v8::String::New("sendAsync"), |
|||
v8::FunctionTemplate::New(JSV8RPCSendAsync) |
|||
); |
|||
|
|||
v8::Local<v8::Object> obj = rpcTemplate->NewInstance(); |
|||
obj->SetInternalField(0, v8::External::New(this)); |
|||
|
|||
v8::Local<v8::String> web3 = v8::String::New("web3"); |
|||
v8::Local<v8::String> setProvider = v8::String::New("setProvider"); |
|||
v8::Handle<v8::Object> web3object = v8::Handle<v8::Object>::Cast(m_engine.context()->Global()->Get(web3)); |
|||
v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(web3object->Get(setProvider)); |
|||
v8::Local<v8::Value> values[1] = {obj}; |
|||
func->Call(func, 1, values); |
|||
} |
@ -1,47 +0,0 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file JSV8RPC.h
|
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
* Ethereum client. |
|||
*/ |
|||
|
|||
#pragma once |
|||
|
|||
#include <libjsengine/JSV8Engine.h> |
|||
|
|||
/// Do not use libstd headers here, it will break on MacOS.
|
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
|
|||
class JSV8RPC |
|||
{ |
|||
public: |
|||
JSV8RPC(JSV8Engine const& _engine); |
|||
virtual ~JSV8RPC() { } |
|||
virtual void onSend(char const* _payload) = 0; |
|||
virtual char const* lastResponse() const = 0; |
|||
|
|||
private: |
|||
JSV8Engine const& m_engine; |
|||
}; |
|||
|
|||
} |
|||
} |
@ -1,101 +0,0 @@ |
|||
var prettyPrint = (function () { |
|||
var onlyDecentPlatform = function (x) { |
|||
return env.os.indexOf('Windows') === -1 ? x : ''; |
|||
}; |
|||
|
|||
var color_red = onlyDecentPlatform('\033[31m'); |
|||
var color_green = onlyDecentPlatform('\033[32m'); |
|||
var color_pink = onlyDecentPlatform('\033[35m'); |
|||
var color_white = onlyDecentPlatform('\033[0m'); |
|||
var color_blue = onlyDecentPlatform('\033[30m'); |
|||
|
|||
function pp(object, indent) { |
|||
try { |
|||
JSON.stringify(object) |
|||
} catch(e) { |
|||
return pp(e, indent); |
|||
} |
|||
var str = ""; |
|||
if(object instanceof Array) { |
|||
str += "["; |
|||
for(var i = 0, l = object.length; i < l; i++) { |
|||
str += pp(object[i], indent); |
|||
if(i < l-1) { |
|||
str += ", "; |
|||
} |
|||
} |
|||
str += "]"; |
|||
} else if (object instanceof Error) { |
|||
str += color_red + "Error: " + color_white + object.message; |
|||
} else if (object === null) { |
|||
str += color_blue + "null"; |
|||
} else if(typeof(object) === "undefined") { |
|||
str += color_blue + object; |
|||
} else if (isBigNumber(object)) { |
|||
str += color_green + object.toString(10) + "'"; |
|||
} else if(typeof(object) === "object") { |
|||
str += "{\n"; |
|||
indent += " "; |
|||
var last = getFields(object).pop() |
|||
getFields(object).forEach(function (k) { |
|||
str += indent + k + ": "; |
|||
try { |
|||
str += pp(object[k], indent); |
|||
} catch (e) { |
|||
str += pp(e, indent); |
|||
} |
|||
if(k !== last) { |
|||
str += ","; |
|||
} |
|||
str += "\n"; |
|||
}); |
|||
str += indent.substr(2, indent.length) + "}"; |
|||
} else if(typeof(object) === "string") { |
|||
str += color_green + "'" + object + "'"; |
|||
} else if(typeof(object) === "number") { |
|||
str += color_red + object; |
|||
} else if(typeof(object) === "function") { |
|||
str += color_pink + "[Function]"; |
|||
} else { |
|||
str += object; |
|||
} |
|||
str += color_white; |
|||
return str; |
|||
} |
|||
var redundantFields = [ |
|||
'valueOf', |
|||
'toString', |
|||
'toLocaleString', |
|||
'hasOwnProperty', |
|||
'isPrototypeOf', |
|||
'propertyIsEnumerable', |
|||
'constructor', |
|||
'__defineGetter__', |
|||
'__defineSetter__', |
|||
'__lookupGetter__', |
|||
'__lookupSetter__', |
|||
'__proto__' |
|||
]; |
|||
var getFields = function (object) { |
|||
var result = Object.getOwnPropertyNames(object); |
|||
if (object.constructor && object.constructor.prototype) { |
|||
result = result.concat(Object.getOwnPropertyNames(object.constructor.prototype)); |
|||
} |
|||
return result.filter(function (field) { |
|||
return redundantFields.indexOf(field) === -1; |
|||
}); |
|||
}; |
|||
var isBigNumber = function (object) { |
|||
return (!!object.constructor && object.constructor.name === 'BigNumber') || |
|||
(typeof BigNumber !== 'undefined' && object instanceof BigNumber) |
|||
}; |
|||
function prettyPrintInner(/* */) { |
|||
var args = arguments; |
|||
var ret = ""; |
|||
for(var i = 0, l = args.length; i < l; i++) { |
|||
ret += pp(args[i], ""); |
|||
} |
|||
return ret; |
|||
}; |
|||
return prettyPrintInner; |
|||
})(); |
Loading…
Reference in new issue