Browse Source

rpc provider in progress

cl-refactor
Marek Kotewicz 10 years ago
parent
commit
7d8d30c551
  1. 12
      eth/main.cpp
  2. 1
      libjsconsole/CMakeLists.txt
  3. 11
      libjsconsole/JSConsole.cpp
  4. 2
      libjsconsole/JSConsole.h
  5. 2
      libjsconsole/JSResources.cmake
  6. 11
      libjsengine/Common.js
  7. 4
      libjsengine/JSResources.cmake
  8. 66
      libjsengine/JSV8Engine.cpp
  9. 1
      libjsengine/JSV8Printer.cpp
  10. 79
      libjsengine/JSV8RPC.cpp
  11. 33
      libjsengine/JSV8RPC.h
  12. 2
      libjsengine/PrettyPrint.js
  13. 2
      test/libjsengine/JSV8Engine.cpp

12
eth/main.cpp

@ -412,6 +412,18 @@ void doFarm(MinerType _m, string const& _remote, unsigned _recheckPeriod)
int main(int argc, char** argv)
{
JSConsole console;
console.repl();
console.repl();
console.repl();
console.repl();
console.repl();
console.repl();
console.repl();
console.repl();
console.repl();
console.repl();
console.repl();
cout << "\x1b[30mEthBlack\x1b[0m" << endl;
cout << "\x1b[90mEthCoal\x1b[0m" << endl;
cout << "\x1b[37mEthGray\x1b[0m" << endl;

1
libjsconsole/CMakeLists.txt

@ -29,3 +29,4 @@ target_link_libraries(${EXECUTABLE} ${READLINE_LIBRARIES})
install( TARGETS ${EXECUTABLE} RUNTIME DESTINATION bin ARCHIVE DESTINATION lib LIBRARY DESTINATION lib )
install( FILES ${HEADERS} DESTINATION include/${EXECUTABLE} )

11
libjsconsole/JSConsole.cpp

@ -16,12 +16,13 @@ using namespace std;
using namespace dev;
using namespace dev::eth;
JSConsole::JSConsole(): m_engine(), m_printer(m_engine)
JSConsole::JSConsole(): m_engine(), m_printer(m_engine), m_rpc(m_engine)
{
JSConsoleResources resources;
string web3 = resources.loadResourceAsString("web3");
m_engine.eval(web3.c_str());
m_engine.eval("web3 = require('web3');");
// JSConsoleResources resources;
// string web3 = resources.loadResourceAsString("web3");
// m_engine.eval(web3.c_str());
// m_engine.eval("web3 = require('web3');");
m_rpc.StartListening();
}
void JSConsole::repl() const

2
libjsconsole/JSConsole.h

@ -6,6 +6,7 @@
#include <libjsengine/JSV8Engine.h>
#include <libjsengine/JSV8Printer.h>
#include <libjsengine/JSV8RPC.h>
namespace dev
{
@ -23,6 +24,7 @@ private:
JSV8Engine m_engine;
JSV8Printer m_printer;
JSV8RPC m_rpc;
};
}

2
libjsconsole/JSResources.cmake

@ -1,5 +1,5 @@
set(web3 "${CMAKE_CURRENT_LIST_DIR}/../libjsqrc/ethereumjs/dist/web3.min.js")
set(web3 "${CMAKE_CURRENT_LIST_DIR}/../libjsqrc/ethereumjs/dist/web3.js")
set(ETH_RESOURCE_NAME "JSConsoleResources")
set(ETH_RESOURCE_LOCATION "${CMAKE_CURRENT_BINARY_DIR}")

11
libjsengine/Common.js

@ -0,0 +1,11 @@
console = {};
console.log = function () {
};
console.warn = function () {
};
console.error = function () {
};
setTimeout = function () {
};

4
libjsengine/JSResources.cmake

@ -1,6 +1,8 @@
set(web3 "${CMAKE_CURRENT_LIST_DIR}/../libjsqrc/ethereumjs/dist/web3.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 "pretty_print")
set(ETH_RESOURCES "web3" "pretty_print" "common")

66
libjsengine/JSV8Engine.cpp

@ -5,7 +5,9 @@
#include <memory>
#include <libplatform/libplatform.h>
#include "JSV8Engine.h"
#include "libjsengine/JSEngineResources.hpp"
using namespace std;
using namespace dev;
using namespace dev::eth;
@ -14,6 +16,48 @@ namespace dev
namespace eth
{
static const char* toCString(const v8::String::Utf8Value& value) {
return *value ? *value : "<string conversion failed>";
}
// from https://github.com/v8/v8-git-mirror/blob/master/samples/shell.cc
static void reportException(v8::Isolate* isolate, v8::TryCatch* try_catch) {
v8::HandleScope handle_scope(isolate);
v8::String::Utf8Value exception(try_catch->Exception());
const char* exception_string = toCString(exception);
v8::Handle<v8::Message> message = try_catch->Message();
if (message.IsEmpty()) {
// V8 didn't provide any extra information about this error; just
// print the exception.
fprintf(stderr, "%s\n", exception_string);
} else {
// Print (filename):(line number): (message).
v8::String::Utf8Value filename(message->GetScriptOrigin().ResourceName());
const char* filename_string = toCString(filename);
int linenum = message->GetLineNumber();
fprintf(stderr, "%s:%i: %s\n", filename_string, linenum, exception_string);
// Print line of source code.
v8::String::Utf8Value sourceline(message->GetSourceLine());
const char* sourceline_string = toCString(sourceline);
fprintf(stderr, "%s\n", sourceline_string);
// Print wavy underline (GetUnderline is deprecated).
int start = message->GetStartColumn();
for (int i = 0; i < start; i++) {
fprintf(stderr, " ");
}
int end = message->GetEndColumn();
for (int i = start; i < end; i++) {
fprintf(stderr, "^");
}
fprintf(stderr, "\n");
v8::String::Utf8Value stack_trace(try_catch->StackTrace());
if (stack_trace.length() > 0) {
const char* stack_trace_string = toCString(stack_trace);
fprintf(stderr, "%s\n", stack_trace_string);
}
}
}
class ShellArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
public:
virtual void* Allocate(size_t length) {
@ -113,7 +157,14 @@ JSV8Env::~JSV8Env()
JSV8Engine::JSV8Engine():
m_isolate(v8::Isolate::New()),
m_scope(new JSV8Scope(m_isolate))
{}
{
JSEngineResources resources;
string common = resources.loadResourceAsString("common");
string web3 = resources.loadResourceAsString("web3");
eval(common.c_str());
eval(web3.c_str());
eval("web3 = require('web3');");
}
JSV8Engine::~JSV8Engine()
{
@ -133,9 +184,20 @@ JSV8Value JSV8Engine::eval(const char* _cstr) const
// the handle returned from the TryCatch is destroyed
// TODO: improve this cause sometimes incorrect message is being sent!
if (script.IsEmpty())
{
reportException(context()->GetIsolate(), &tryCatch);
return v8::Exception::Error(v8::Local<v8::String>::New(context()->GetIsolate(), tryCatch.Message()->Get()));
}
auto result = script->Run();
if (result.IsEmpty())
{
reportException(context()->GetIsolate(), &tryCatch);
return v8::Exception::Error(v8::Local<v8::String>::New(context()->GetIsolate(), tryCatch.Message()->Get()));
}
return JSV8Value(script->Run());
return result;
}
v8::Handle<v8::Context> const& JSV8Engine::context() const

1
libjsengine/JSV8Printer.cpp

@ -5,7 +5,6 @@
#include <string>
#include "JSV8Printer.h"
#include "libjsengine/JSEngineResources.hpp"
#include "libjsengine/t.h"
using namespace std;
using namespace dev;

79
libjsengine/JSV8RPC.cpp

@ -0,0 +1,79 @@
//
// Created by Marek Kotewicz on 04/05/15.
//
#include "libjsconsole/JSConsoleResources.hpp"
#include "JSV8RPC.h"
#include <assert.h>
using namespace std;
using namespace dev;
using namespace dev::eth;
namespace dev
{
namespace eth
{
void JSV8RPCSend(v8::FunctionCallbackInfo<v8::Value> const& args)
{
const char* tmp = R"(
{
"id": 1,
"jsonrpc": "2.0",
"result": "0x9df33b35fbdd8dff5e557a0cce288614dbf7327c292f1ac5b9c6c0e672005f48"
}
)";
v8::Local<v8::String> JSON = v8::String::NewFromUtf8(args.GetIsolate(), "JSON");
v8::Local<v8::String> parse = v8::String::NewFromUtf8(args.GetIsolate(), "parse");
v8::Handle<v8::Object> jsonObject = v8::Handle<v8::Object>::Cast(args.GetIsolate()->GetCurrentContext()->Global()->Get(JSON));
v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(jsonObject->Get(parse));
v8::Local<v8::Value> values[1] = {v8::String::NewFromUtf8(args.GetIsolate(), tmp)};
args.GetReturnValue().Set(func->Call(func, 1, values));
}
}
}
JSV8RPC::JSV8RPC(JSV8Engine const &_engine): m_engine(_engine) {}
JSV8RPC::~JSV8RPC()
{
StopListening();
}
bool JSV8RPC::StartListening()
{
v8::HandleScope scope(m_engine.context()->GetIsolate());
v8::Local<v8::ObjectTemplate> rpcTemplate = v8::ObjectTemplate::New(m_engine.context()->GetIsolate());
rpcTemplate->SetInternalFieldCount(1);
rpcTemplate->Set(v8::String::NewFromUtf8(m_engine.context()->GetIsolate(), "send"),
v8::FunctionTemplate::New(m_engine.context()->GetIsolate(), JSV8RPCSend));
rpcTemplate->Set(v8::String::NewFromUtf8(m_engine.context()->GetIsolate(), "sendAsync"),
v8::FunctionTemplate::New(m_engine.context()->GetIsolate(), JSV8RPCSend));
v8::Local<v8::Object> obj = rpcTemplate->NewInstance();
obj->SetInternalField(0, v8::External::New(m_engine.context()->GetIsolate(), this));
v8::Local<v8::String> web3 = v8::String::NewFromUtf8(m_engine.context()->GetIsolate(), "web3");
v8::Local<v8::String> setProvider = v8::String::NewFromUtf8(m_engine.context()->GetIsolate(), "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);
return true;
}
bool JSV8RPC::StopListening()
{
return true;
}
bool JSV8RPC::SendResponse(std::string const &_response, void *_addInfo)
{
m_lastResponse = _response;
(void)_addInfo;
return true;
}

33
libjsengine/JSV8RPC.h

@ -0,0 +1,33 @@
//
// Created by Marek Kotewicz on 04/05/15.
//
#pragma once
//#include <jsonrpccpp/server/abstractserverconnector.h>
#include <libjsengine/JSV8Engine.h>
namespace dev
{
namespace eth
{
class JSV8RPC
{
public:
JSV8RPC(JSV8Engine const& _engine);
virtual ~JSV8RPC();
bool StartListening();
bool StopListening();
bool SendResponse(std::string const& _response, void* _addInfo = NULL);
private:
JSV8Engine const& m_engine;
std::string m_lastResponse;
};
}
}

2
libjsengine/PrettyPrint.js

@ -17,6 +17,8 @@ var prettyPrint = (function () {
str += " ]";
} else if (object instanceof Error) {
str += "\033[31m" + "Error:\033[0m " + object.message;
} else if (object === null) {
str += "\033[1m\033[30m" + "null";
} else if (isBigNumber(object)) {
str += "\033[32m'" + object.toString(10) + "'";
} else if(typeof(object) === "object") {

2
test/libjsengine/JSV8Engine.cpp

@ -63,7 +63,9 @@ BOOST_AUTO_TEST_CASE(evalNull)
JSV8Printer printer(engine);
auto value = engine.eval("null");
string result = printer.print(value);
string prettyResult = printer.prettyPrint(value);
BOOST_CHECK_EQUAL(result, "null");
BOOST_CHECK_EQUAL(prettyResult.find("null") != std::string::npos, true);
}
BOOST_AUTO_TEST_SUITE_END()

Loading…
Cancel
Save