Browse Source

Merge pull request #2389 from debris/optional_readline

jsconsole on windows
cl-refactor
Gav Wood 10 years ago
parent
commit
6c8803c3a6
  1. 26
      cmake/Findv8.cmake
  2. 2
      ethconsole/CMakeLists.txt
  3. 1
      extdep/getstuff.bat
  4. 8
      libjsconsole/CMakeLists.txt
  5. 31
      libjsconsole/JSConsole.h
  6. 2
      libjsengine/CMakeLists.txt
  7. 4
      libjsengine/JSEngine.h
  8. 17
      libjsengine/JSV8Engine.cpp
  9. 1
      libjsengine/JSV8Engine.h
  10. 1
      libjsengine/JSV8Printer.cpp
  11. 26
      libjsengine/PrettyPrint.js

26
cmake/Findv8.cmake

@ -30,13 +30,33 @@ set(V8_LIBRARIES ${V8_LIBRARY})
# boost is using the same "hack" as us with "optimized" and "debug"
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
find_library(
V8_LIBRARY
NAMES v8_base
DOC "v8 base library"
)
find_library(
V8_NO_SNAPSHOT_LIBRARY
NAMES v8_nosnapshot
DOC "v8 nosnapshot library"
)
set(V8_LIBRARIES ${V8_LIBRARY} ${V8_NO_SNAPSHOT_LIBRARY})
find_library(
V8_LIBRARY_DEBUG
NAMES v8d
DOC "v8 debug library"
NAMES v8_based
DOC "v8 base library"
)
find_library(
V8_NO_SNAPSHOT_LIBRARY_DEBUG
NAMES v8_nosnapshotd
DOC "v8 nosnapshot library"
)
set(V8_LIBRARIES optimized ${V8_LIBRARIES} debug ${V8_LIBRARY_DEBUG})
set(V8_LIBRARIES "ws2_32" "winmm" optimized ${V8_LIBRARIES} debug ${V8_LIBRARY_DEBUG} ${V8_NO_SNAPSHOT_LIBRARY_DEBUG})
endif()

2
ethconsole/CMakeLists.txt

@ -13,9 +13,7 @@ set(EXECUTABLE ethconsole)
file(GLOB HEADERS "*.h")
add_executable(${EXECUTABLE} ${SRC_LIST} ${HEADERS})
target_link_libraries(${EXECUTABLE} ${Boost_REGEX_LIBRARIES})
target_link_libraries(${EXECUTABLE} ${READLINE_LIBRARIES})
target_link_libraries(${EXECUTABLE} ${CURL_LIBRARIES})
if (DEFINED WIN32 AND NOT DEFINED CMAKE_COMPILER_IS_MINGW)

1
extdep/getstuff.bat

@ -14,6 +14,7 @@ call :download leveldb 1.2
call :download microhttpd 0.9.2
call :download qt 5.4.1
call :download miniupnpc 1.9
call :download v8 3.15.9
goto :EOF

8
libjsconsole/CMakeLists.txt

@ -12,7 +12,9 @@ aux_source_directory(. SRC_LIST)
include_directories(BEFORE ${V8_INCLUDE_DIRS})
include_directories(BEFORE ..)
include_directories(${READLINE_INCLUDE_DIRS})
if (READLINE_FOUND)
include_directories(${READLINE_INCLUDE_DIRS})
endif()
include_directories(${JSON_RPC_CPP_INCLUDE_DIRS})
include_directories(${CURL_INCLUDE_DIRS})
@ -24,7 +26,9 @@ add_library(${EXECUTABLE} ${SRC_LIST} ${HEADERS})
target_link_libraries(${EXECUTABLE} jsengine)
target_link_libraries(${EXECUTABLE} devcore)
target_link_libraries(${EXECUTABLE} ${READLINE_LIBRARIES})
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})

31
libjsconsole/JSConsole.h

@ -23,9 +23,11 @@
#pragma once
#include <libdevcore/Log.h>
// TODO! make readline optional!
#if ETH_READLINE
#include <readline/readline.h>
#include <readline/history.h>
#endif
namespace dev
{
@ -42,18 +44,35 @@ public:
void readExpression() const
{
std::string cmd = "";
g_logPost = [](std::string const& a, char const*) { std::cout << "\r \r" << a << std::endl << std::flush; rl_forced_update_display(); };
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());
isEmpty = !(buff && *buff);
isEmpty = !buff;
if (!isEmpty)
{
cmd += std::string(buff);
cmd += " ";
rl = std::string(buff);
free(buff);
}
#else
std::cout << promptForIndentionLevel(openBrackets) << std::flush;
std::getline(std::cin, rl);
isEmpty = rl.length() == 0;
#endif
if (!isEmpty)
{
cmd += rl;
cmd += " ";
int open = std::count(cmd.begin(), cmd.end(), '{');
open += std::count(cmd.begin(), cmd.end(), '(');
int closed = std::count(cmd.begin(), cmd.end(), '}');
@ -64,7 +83,9 @@ public:
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;

2
libjsengine/CMakeLists.txt

@ -21,6 +21,8 @@ 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

4
libjsengine/JSEngine.h

@ -29,7 +29,11 @@ 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
{

17
libjsengine/JSV8Engine.cpp

@ -23,6 +23,10 @@
#include <memory>
#include "JSV8Engine.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;
@ -87,15 +91,6 @@ void reportException(v8::TryCatch* _tryCatch)
}
}
class JSV8Env
{
public:
~JSV8Env()
{
v8::V8::Dispose();
}
};
class JSV8Scope
{
public:
@ -124,8 +119,6 @@ private:
}
}
JSV8Env JSV8Engine::s_env = JSV8Env();
JSString JSV8Value::toString() const
{
if (m_value.IsEmpty())
@ -141,6 +134,7 @@ JSString JSV8Value::toString() const
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");
@ -157,7 +151,6 @@ JSV8Engine::~JSV8Engine()
JSV8Value JSV8Engine::eval(char const* _cstr) const
{
v8::HandleScope handleScope;
v8::TryCatch tryCatch;
v8::Local<v8::String> source = v8::String::New(_cstr);
v8::Local<v8::String> name(v8::String::New("(shell)"));

1
libjsengine/JSV8Engine.h

@ -56,7 +56,6 @@ public:
v8::Handle<v8::Context> const& context() const;
private:
static JSV8Env s_env;
JSV8Scope* m_scope;
};

1
libjsengine/JSV8Printer.cpp

@ -37,7 +37,6 @@ JSV8Printer::JSV8Printer(JSV8Engine const& _engine): m_engine(_engine)
JSString JSV8Printer::prettyPrint(JSV8Value const& _value) const
{
v8::HandleScope handleScope;
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())};

26
libjsengine/PrettyPrint.js

@ -1,4 +1,14 @@
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)
@ -16,13 +26,13 @@ var prettyPrint = (function () {
}
str += " ]";
} else if (object instanceof Error) {
str += "\033[31m" + "Error:\033[0m " + object.message;
str += color_red + "Error: " + color_white + object.message;
} else if (object === null) {
str += "\033[1m\033[30m" + "null";
str += color_blue + "null";
} else if(typeof(object) === "undefined") {
str += "\033[1m\033[30m" + object;
str += color_blue + object;
} else if (isBigNumber(object)) {
str += "\033[32m'" + object.toString(10) + "'";
str += color_green + object.toString(10) + "'";
} else if(typeof(object) === "object") {
str += "{\n";
indent += " ";
@ -41,15 +51,15 @@ var prettyPrint = (function () {
});
str += indent.substr(2, indent.length) + "}";
} else if(typeof(object) === "string") {
str += "\033[32m'" + object + "'";
str += color_green + object + "'";
} else if(typeof(object) === "number") {
str += "\033[31m" + object;
str += color_red + object;
} else if(typeof(object) === "function") {
str += "\033[35m[Function]";
str += color_pink + "[Function]";
} else {
str += object;
}
str += "\033[0m";
str += color_white;
return str;
}
var redundantFields = [

Loading…
Cancel
Save