Browse Source

downgrade v8 to 3.15

cl-refactor
Marek Kotewicz 10 years ago
parent
commit
e546787890
  1. 3
      cmake/EthDependencies.cmake
  2. 20
      cmake/Findv8.cmake
  3. 174
      libjsengine/JSV8Engine.cpp
  4. 1
      libjsengine/JSV8Engine.h
  5. 8
      libjsengine/JSV8Printer.cpp
  6. 34
      libjsengine/JSV8RPC.cpp

3
cmake/EthDependencies.cmake

@ -31,7 +31,8 @@ endif()
# homebrew installs qts in opt # homebrew installs qts in opt
if (APPLE) if (APPLE)
set (CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} "/usr/local/opt/qt5") set (CMAKE_PREFIX_PATH "/usr/local/opt/qt5" ${CMAKE_PREFIX_PATH})
set (CMAKE_PREFIX_PATH "/usr/local/opt/v8-315" ${CMAKE_PREFIX_PATH})
endif() endif()
find_program(CTEST_COMMAND ctest) find_program(CTEST_COMMAND ctest)

20
cmake/Findv8.cmake

@ -25,25 +25,11 @@ find_library(
find_library( find_library(
V8_BASE_LIBRARY V8_BASE_LIBRARY
NAMES v8_base NAMES v8_base
DOC "v8 library" DOC "v8 base library"
)
find_library(
V8_LIBBASE_LIBRARY
NAMES v8_libbase
DOC "v8 library"
) )
find_library( set(V8_INCLUDE_DIRS ${V8_INCLUDE_DIR})
V8_LIBPLATFORM_LIBRARY set(V8_LIBRARIES ${V8_LIBRARY} ${V8_BASE_LIBRARY})
NAMES v8_libplatform
DOC "v8 library"
)
string(REPLACE "/include" "" V8_INCLUDE_DIR_LOCATION ${V8_INCLUDE_DIR})
set(V8_INCLUDE_DIRS ${V8_INCLUDE_DIR} ${V8_INCLUDE_DIR_LOCATION})
set(V8_LIBRARIES ${V8_LIBRARY} ${V8_BASE_LIBRARY} ${V8_LIBBASE_LIBRARY} ${V8_LIBPLATFORM_LIBRARY})
# debug library on windows # debug library on windows
# same naming convention as in qt (appending debug library with d) # same naming convention as in qt (appending debug library with d)

174
libjsengine/JSV8Engine.cpp

@ -21,7 +21,7 @@
*/ */
#include <memory> #include <memory>
#include <libplatform/libplatform.h> //#include <libplatform/libplatform.h>
#include "JSV8Engine.h" #include "JSV8Engine.h"
#include "libjsengine/JSEngineResources.hpp" #include "libjsengine/JSEngineResources.hpp"
@ -42,59 +42,59 @@ static const char* toCString(v8::String::Utf8Value const& value)
// from https://github.com/v8/v8-git-mirror/blob/master/samples/shell.cc // from https://github.com/v8/v8-git-mirror/blob/master/samples/shell.cc
static void reportException(v8::Isolate* isolate, v8::TryCatch* try_catch) static void reportException(v8::Isolate* isolate, v8::TryCatch* try_catch)
{ {
v8::HandleScope handle_scope(isolate); // v8::HandleScope handle_scope;
v8::String::Utf8Value exception(try_catch->Exception()); // v8::String::Utf8Value exception(try_catch->Exception());
const char* exception_string = toCString(exception); // const char* exception_string = toCString(exception);
v8::Handle<v8::Message> message = try_catch->Message(); // v8::Handle<v8::Message> message = try_catch->Message();
//
// V8 didn't provide any extra information about this error; just // V8 didn't provide any extra information about this error; just
// print the exception. // print the exception.
if (message.IsEmpty()) // if (message.IsEmpty())
fprintf(stderr, "%s\n", exception_string); // fprintf(stderr, "%s\n", exception_string);
else // else
{ // {
// Print (filename):(line number): (message). // Print (filename):(line number): (message).
v8::String::Utf8Value filename(message->GetScriptOrigin().ResourceName()); // v8::String::Utf8Value filename(message->GetScriptOrigin().ResourceName());
const char* filename_string = toCString(filename); // const char* filename_string = toCString(filename);
int linenum = message->GetLineNumber(); // int linenum = message->GetLineNumber();
fprintf(stderr, "%s:%i: %s\n", filename_string, linenum, exception_string); // fprintf(stderr, "%s:%i: %s\n", filename_string, linenum, exception_string);
//
// Print line of source code. // Print line of source code.
v8::String::Utf8Value sourceline(message->GetSourceLine()); // v8::String::Utf8Value sourceline(message->GetSourceLine());
const char* sourceline_string = toCString(sourceline); // const char* sourceline_string = toCString(sourceline);
fprintf(stderr, "%s\n", sourceline_string); // fprintf(stderr, "%s\n", sourceline_string);
//
// Print wavy underline (GetUnderline is deprecated). // Print wavy underline (GetUnderline is deprecated).
int start = message->GetStartColumn(); // int start = message->GetStartColumn();
//
for (int i = 0; i < start; i++) // for (int i = 0; i < start; i++)
fprintf(stderr, " "); // fprintf(stderr, " ");
//
int end = message->GetEndColumn(); // int end = message->GetEndColumn();
//
for (int i = start; i < end; i++) // for (int i = start; i < end; i++)
fprintf(stderr, "^"); // fprintf(stderr, "^");
//
fprintf(stderr, "\n"); // fprintf(stderr, "\n");
v8::String::Utf8Value stack_trace(try_catch->StackTrace()); // v8::String::Utf8Value stack_trace(try_catch->StackTrace());
//
if (stack_trace.length() > 0) // if (stack_trace.length() > 0)
{ // {
const char* stack_trace_string = toCString(stack_trace); // const char* stack_trace_string = toCString(stack_trace);
fprintf(stderr, "%s\n", stack_trace_string); // fprintf(stderr, "%s\n", stack_trace_string);
} // }
} // }
} }
class ShellArrayBufferAllocator : public v8::ArrayBuffer::Allocator { //class ShellArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
public: //public:
virtual void* Allocate(size_t length) { // virtual void* Allocate(size_t length) {
void* data = AllocateUninitialized(length); // void* data = AllocateUninitialized(length);
return data == NULL ? data : memset(data, 0, length); // return data == NULL ? data : memset(data, 0, length);
} // }
virtual void* AllocateUninitialized(size_t length) { return malloc(length); } // virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
virtual void Free(void* data, size_t) { free(data); } // virtual void Free(void* data, size_t) { free(data); }
}; //};
class JSV8Env class JSV8Env
{ {
@ -104,32 +104,37 @@ public:
~JSV8Env(); ~JSV8Env();
private: private:
v8::Platform *m_platform; // v8::Platform *m_platform;
}; };
v8::Handle<v8::Context> createShellContext(v8::Isolate* isolate) v8::Handle<v8::Context> createShellContext()
{ {
v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate); v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
v8::Handle<v8::Context> context = v8::Context::New(isolate, NULL, global); return v8::Context::New(NULL, global);
return context;
} }
class JSV8Scope class JSV8Scope
{ {
public: public:
JSV8Scope(v8::Isolate* _isolate): JSV8Scope():
m_isolateScope(_isolate), m_handleScope(),
m_handleScope(_isolate), m_context(createShellContext()),
m_context(createShellContext(_isolate)),
m_contextScope(m_context) m_contextScope(m_context)
{} {
m_context->Enter();
}
~JSV8Scope()
{
m_context->Exit();
m_context.Dispose();
}
v8::Handle <v8::Context> const& context() const { return m_context; } v8::Persistent <v8::Context> const& context() const { return m_context; }
private: private:
v8::Isolate::Scope m_isolateScope;
v8::HandleScope m_handleScope; v8::HandleScope m_handleScope;
v8::Handle <v8::Context> m_context; v8::Persistent <v8::Context> m_context;
v8::Context::Scope m_contextScope; v8::Context::Scope m_contextScope;
}; };
@ -152,28 +157,22 @@ const char* JSV8Value::asCString() const
JSV8Env::JSV8Env() JSV8Env::JSV8Env()
{ {
static bool initialized = false; // v8::V8::InitializeICU();
if (initialized) // m_platform = v8::platform::CreateDefaultPlatform();
return; // v8::V8::InitializePlatform(m_platform);
initialized = true; // v8::V8::Initialize();
v8::V8::InitializeICU(); // ShellArrayBufferAllocator array_buffer_allocator;
m_platform = v8::platform::CreateDefaultPlatform(); // v8::V8::SetArrayBufferAllocator(&array_buffer_allocator);
v8::V8::InitializePlatform(m_platform);
v8::V8::Initialize();
ShellArrayBufferAllocator array_buffer_allocator;
v8::V8::SetArrayBufferAllocator(&array_buffer_allocator);
} }
JSV8Env::~JSV8Env() JSV8Env::~JSV8Env()
{ {
v8::V8::Dispose(); v8::V8::Dispose();
v8::V8::ShutdownPlatform(); // v8::V8::ShutdownPlatform();
delete m_platform; // delete m_platform;
} }
JSV8Engine::JSV8Engine(): JSV8Engine::JSV8Engine(): m_scope(new JSV8Scope())
m_isolate(v8::Isolate::New()),
m_scope(new JSV8Scope(m_isolate))
{ {
JSEngineResources resources; JSEngineResources resources;
string common = resources.loadResourceAsString("common"); string common = resources.loadResourceAsString("common");
@ -186,15 +185,14 @@ JSV8Engine::JSV8Engine():
JSV8Engine::~JSV8Engine() JSV8Engine::~JSV8Engine()
{ {
delete m_scope; delete m_scope;
m_isolate->Dispose();
} }
JSV8Value JSV8Engine::eval(const char* _cstr) const JSV8Value JSV8Engine::eval(const char* _cstr) const
{ {
v8::HandleScope handleScope(m_isolate); v8::HandleScope handleScope;
v8::TryCatch tryCatch; v8::TryCatch tryCatch;
v8::Local<v8::String> source = v8::String::NewFromUtf8(context()->GetIsolate(), _cstr); v8::Local<v8::String> source = v8::String::New(_cstr);
v8::Local<v8::String> name(v8::String::NewFromUtf8(context()->GetIsolate(), "(shell)")); v8::Local<v8::String> name(v8::String::New("(shell)"));
v8::ScriptOrigin origin(name); v8::ScriptOrigin origin(name);
v8::Handle<v8::Script> script = v8::Script::Compile(source, &origin); v8::Handle<v8::Script> script = v8::Script::Compile(source, &origin);
@ -202,16 +200,16 @@ JSV8Value JSV8Engine::eval(const char* _cstr) const
// the handle returned from the TryCatch is destroyed // the handle returned from the TryCatch is destroyed
if (script.IsEmpty()) if (script.IsEmpty())
{ {
reportException(context()->GetIsolate(), &tryCatch); // reportException(&tryCatch);
return v8::Exception::Error(v8::Local<v8::String>::New(context()->GetIsolate(), tryCatch.Message()->Get())); return v8::Exception::Error(v8::Local<v8::String>::New(tryCatch.Message()->Get()));
} }
auto result = script->Run(); auto result = script->Run();
if (result.IsEmpty()) if (result.IsEmpty())
{ {
reportException(context()->GetIsolate(), &tryCatch); // reportException(&tryCatch);
return v8::Exception::Error(v8::Local<v8::String>::New(context()->GetIsolate(), tryCatch.Message()->Get())); return v8::Exception::Error(v8::Local<v8::String>::New(tryCatch.Message()->Get()));
} }
return result; return result;

1
libjsengine/JSV8Engine.h

@ -54,7 +54,6 @@ public:
private: private:
static JSV8Env s_env; static JSV8Env s_env;
v8::Isolate* m_isolate;
JSV8Scope* m_scope; JSV8Scope* m_scope;
}; };

8
libjsengine/JSV8Printer.cpp

@ -37,11 +37,11 @@ JSV8Printer::JSV8Printer(JSV8Engine const& _engine): m_engine(_engine)
const char* JSV8Printer::prettyPrint(JSV8Value const& _value) const const char* JSV8Printer::prettyPrint(JSV8Value const& _value) const
{ {
v8::HandleScope handleScope(m_engine.context()->GetIsolate()); v8::HandleScope handleScope;
v8::Local<v8::String> pp = v8::String::NewFromUtf8(m_engine.context()->GetIsolate(), "prettyPrint"); 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::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(m_engine.context()->Global()->Get(pp));
v8::Local<v8::Value> values[1] = {_value.value()}; v8::Local<v8::Value> values[1] = {v8::Local<v8::Value>::New(_value.value())};
v8::Local<v8::Value> res = v8::Local<v8::Value>::Cast(func->Call(func, 1, values)); v8::Local<v8::Value> res = func->Call(func, 1, values);
v8::String::Utf8Value str(res); v8::String::Utf8Value str(res);
return *str ? *str : "<pretty print conversion failed>"; return *str ? *str : "<pretty print conversion failed>";
} }

34
libjsengine/JSV8RPC.cpp

@ -20,10 +20,8 @@
* Ethereum client. * Ethereum client.
*/ */
#include "libjsconsole/JSConsoleResources.hpp"
#include "JSV8RPC.h" #include "JSV8RPC.h"
using namespace std;
using namespace dev; using namespace dev;
using namespace dev::eth; using namespace dev::eth;
@ -32,12 +30,12 @@ namespace dev
namespace eth namespace eth
{ {
void JSV8RPCSend(v8::FunctionCallbackInfo<v8::Value> const& args) v8::Handle<v8::Value> JSV8RPCSend(v8::Arguments const& args)
{ {
v8::Local<v8::String> JSON = v8::String::NewFromUtf8(args.GetIsolate(), "JSON"); v8::Local<v8::String> JSON = v8::String::New("JSON");
v8::Local<v8::String> parse = v8::String::NewFromUtf8(args.GetIsolate(), "parse"); v8::Local<v8::String> parse = v8::String::New("parse");
v8::Local<v8::String> stringify = v8::String::NewFromUtf8(args.GetIsolate(), "stringify"); v8::Local<v8::String> stringify = v8::String::New("stringify");
v8::Handle<v8::Object> jsonObject = v8::Handle<v8::Object>::Cast(args.GetIsolate()->GetCurrentContext()->Global()->Get(JSON)); 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> parseFunc = v8::Handle<v8::Function>::Cast(jsonObject->Get(parse));
v8::Handle<v8::Function> stringifyFunc = v8::Handle<v8::Function>::Cast(jsonObject->Get(stringify)); v8::Handle<v8::Function> stringifyFunc = v8::Handle<v8::Function>::Cast(jsonObject->Get(stringify));
@ -49,8 +47,8 @@ void JSV8RPCSend(v8::FunctionCallbackInfo<v8::Value> const& args)
v8::String::Utf8Value str(stringifiedArg); v8::String::Utf8Value str(stringifiedArg);
that->onSend(*str); that->onSend(*str);
v8::Local<v8::Value> values[1] = {v8::String::NewFromUtf8(args.GetIsolate(), that->lastResponse())}; v8::Local<v8::Value> values[1] = {v8::String::New(that->lastResponse())};
args.GetReturnValue().Set(parseFunc->Call(parseFunc, 1, values)); return parseFunc->Call(parseFunc, 1, values);
} }
} }
@ -58,19 +56,19 @@ void JSV8RPCSend(v8::FunctionCallbackInfo<v8::Value> const& args)
JSV8RPC::JSV8RPC(JSV8Engine const &_engine): m_engine(_engine) JSV8RPC::JSV8RPC(JSV8Engine const &_engine): m_engine(_engine)
{ {
v8::HandleScope scope(m_engine.context()->GetIsolate()); v8::HandleScope scope;
v8::Local<v8::ObjectTemplate> rpcTemplate = v8::ObjectTemplate::New(m_engine.context()->GetIsolate()); v8::Local<v8::ObjectTemplate> rpcTemplate = v8::ObjectTemplate::New();
rpcTemplate->SetInternalFieldCount(1); rpcTemplate->SetInternalFieldCount(1);
rpcTemplate->Set(v8::String::NewFromUtf8(m_engine.context()->GetIsolate(), "send"), rpcTemplate->Set(v8::String::New("send"),
v8::FunctionTemplate::New(m_engine.context()->GetIsolate(), JSV8RPCSend)); v8::FunctionTemplate::New(JSV8RPCSend));
rpcTemplate->Set(v8::String::NewFromUtf8(m_engine.context()->GetIsolate(), "sendAsync"), rpcTemplate->Set(v8::String::New("sendAsync"),
v8::FunctionTemplate::New(m_engine.context()->GetIsolate(), JSV8RPCSend)); v8::FunctionTemplate::New(JSV8RPCSend));
v8::Local<v8::Object> obj = rpcTemplate->NewInstance(); v8::Local<v8::Object> obj = rpcTemplate->NewInstance();
obj->SetInternalField(0, v8::External::New(m_engine.context()->GetIsolate(), this)); obj->SetInternalField(0, v8::External::New(this));
v8::Local<v8::String> web3 = v8::String::NewFromUtf8(m_engine.context()->GetIsolate(), "web3"); v8::Local<v8::String> web3 = v8::String::New("web3");
v8::Local<v8::String> setProvider = v8::String::NewFromUtf8(m_engine.context()->GetIsolate(), "setProvider"); 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::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::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(web3object->Get(setProvider));
v8::Local<v8::Value> values[1] = {obj}; v8::Local<v8::Value> values[1] = {obj};

Loading…
Cancel
Save