Marek Kotewicz
10 years ago
12 changed files with 321 additions and 1 deletions
@ -0,0 +1,69 @@ |
|||
# Find v8 |
|||
# |
|||
# Find the v8 includes and library |
|||
# |
|||
# if you nee to add a custom library search path, do it via via CMAKE_PREFIX_PATH |
|||
# |
|||
# This module defines |
|||
# V8_INCLUDE_DIRS, where to find header, etc. |
|||
# V8_LIBRARIES, the libraries needed to use v8. |
|||
# V8_FOUND, If false, do not try to use v8. |
|||
|
|||
# only look in default directories |
|||
find_path( |
|||
V8_INCLUDE_DIR |
|||
NAMES v8.h |
|||
DOC "v8 include dir" |
|||
) |
|||
|
|||
find_library( |
|||
V8_LIBRARY |
|||
NAMES v8 |
|||
DOC "v8 library" |
|||
) |
|||
|
|||
find_library( |
|||
V8_BASE_LIBRARY |
|||
NAMES v8_base |
|||
DOC "v8 library" |
|||
) |
|||
|
|||
find_library( |
|||
V8_LIBBASE_LIBRARY |
|||
NAMES v8_libbase |
|||
DOC "v8 library" |
|||
) |
|||
|
|||
find_library( |
|||
V8_LIBPLATFORM_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 |
|||
# same naming convention as in qt (appending debug library with d) |
|||
# boost is using the same "hack" as us with "optimized" and "debug" |
|||
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") |
|||
|
|||
find_library( |
|||
V8_LIBRARY_DEBUG |
|||
NAMES v8d |
|||
DOC "v8 debug library" |
|||
) |
|||
|
|||
set(V8_LIBRARIES optimized ${V8_LIBRARIES} debug ${V8_LIBRARY_DEBUG}) |
|||
|
|||
endif() |
|||
|
|||
# handle the QUIETLY and REQUIRED arguments and set V8_FOUND to TRUE |
|||
# if all listed variables are TRUE, hide their existence from configuration view |
|||
include(FindPackageHandleStandardArgs) |
|||
find_package_handle_standard_args(v8 DEFAULT_MSG |
|||
V8_INCLUDE_DIR V8_LIBRARY) |
|||
mark_as_advanced (V8_INCLUDE_DIR V8_LIBRARY) |
|||
|
@ -0,0 +1,28 @@ |
|||
cmake_policy(SET CMP0015 NEW) |
|||
# this policy was introduced in cmake 3.0 |
|||
# remove if, once 3.0 will be used on unix |
|||
if (${CMAKE_MAJOR_VERSION} GREATER 2) |
|||
# old policy do not use MACOSX_RPATH |
|||
cmake_policy(SET CMP0042 OLD) |
|||
endif() |
|||
|
|||
set(CMAKE_AUTOMOC OFF) |
|||
|
|||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libstdc++") |
|||
|
|||
aux_source_directory(. SRC_LIST) |
|||
|
|||
include_directories(BEFORE ..) |
|||
include_directories(${V8_INCLUDE_DIRS}) |
|||
|
|||
set(EXECUTABLE ethconsole) |
|||
|
|||
file(GLOB HEADERS "*.h") |
|||
|
|||
add_library(${EXECUTABLE} SHARED ${SRC_LIST} ${HEADERS}) |
|||
|
|||
target_link_libraries(${EXECUTABLE} ${V8_LIBRARIES}) |
|||
|
|||
install( TARGETS ${EXECUTABLE} RUNTIME DESTINATION bin ARCHIVE DESTINATION lib LIBRARY DESTINATION lib ) |
|||
install( FILES ${HEADERS} DESTINATION include/${EXECUTABLE} ) |
|||
|
@ -0,0 +1,5 @@ |
|||
//
|
|||
// Created by Marek Kotewicz on 27/04/15.
|
|||
//
|
|||
|
|||
#include "JSScope.h" |
@ -0,0 +1,25 @@ |
|||
//
|
|||
// Created by Marek Kotewicz on 27/04/15.
|
|||
//
|
|||
|
|||
#pragma once |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
|
|||
class JSScope |
|||
{ |
|||
public: |
|||
JSScope() |
|||
{ }; |
|||
|
|||
virtual ~JSScope() |
|||
{ }; |
|||
|
|||
virtual const char* evaluate(const char* _cstr) const = 0; |
|||
}; |
|||
|
|||
} |
|||
} |
@ -0,0 +1,87 @@ |
|||
//
|
|||
// Created by Marek Kotewicz on 27/04/15.
|
|||
//
|
|||
|
|||
#include <libplatform/libplatform.h> |
|||
#include <memory> |
|||
#include "JSV8ScopeBase.h" |
|||
|
|||
using namespace dev; |
|||
using namespace dev::eth; |
|||
|
|||
JSV8Env JSV8ScopeBase::s_env = JSV8Env(); |
|||
|
|||
class ShellArrayBufferAllocator : public v8::ArrayBuffer::Allocator { |
|||
public: |
|||
virtual void* Allocate(size_t length) { |
|||
void* data = AllocateUninitialized(length); |
|||
return data == NULL ? data : memset(data, 0, length); |
|||
} |
|||
virtual void* AllocateUninitialized(size_t length) { return malloc(length); } |
|||
virtual void Free(void* data, size_t) { free(data); } |
|||
}; |
|||
|
|||
JSV8Env::JSV8Env() |
|||
{ |
|||
static bool initialized = false; |
|||
if (initialized) |
|||
return; |
|||
initialized = true; |
|||
v8::V8::InitializeICU(); |
|||
m_platform = v8::platform::CreateDefaultPlatform(); |
|||
v8::V8::InitializePlatform(m_platform); |
|||
v8::V8::Initialize(); |
|||
ShellArrayBufferAllocator array_buffer_allocator; |
|||
v8::V8::SetArrayBufferAllocator(&array_buffer_allocator); |
|||
} |
|||
|
|||
JSV8Env::~JSV8Env() |
|||
{ |
|||
v8::V8::Dispose(); |
|||
v8::V8::ShutdownPlatform(); |
|||
delete m_platform; |
|||
} |
|||
|
|||
JSV8ScopeBase::JSV8ScopeBase(): |
|||
m_isolate(v8::Isolate::New()), |
|||
m_scope(new JSV8DumbScope(m_isolate)) |
|||
{ } |
|||
|
|||
JSV8ScopeBase::~JSV8ScopeBase() |
|||
{ |
|||
delete m_scope; |
|||
m_isolate->Dispose(); |
|||
} |
|||
|
|||
const char* JSV8ScopeBase::evaluate(const char* _cstr) const |
|||
{ |
|||
v8::HandleScope handleScope(m_isolate); |
|||
// v8::TryCatch tryCatch;
|
|||
v8::Local<v8::String> source = v8::String::NewFromUtf8(m_scope->context()->GetIsolate(), _cstr); |
|||
v8::Local<v8::String> name(v8::String::NewFromUtf8(m_scope->context()->GetIsolate(), "(shell)")); |
|||
v8::ScriptOrigin origin(name); |
|||
v8::Handle<v8::Script> script = v8::Script::Compile(source, &origin); |
|||
if (script.IsEmpty()) |
|||
{ |
|||
// TODO: handle exceptions
|
|||
return ""; |
|||
} |
|||
|
|||
v8::Handle<v8::Value> result = script->Run(); |
|||
return formatValue(result); |
|||
} |
|||
|
|||
const char* JSV8ScopeBase::formatValue(v8::Handle<v8::Value> const &_value) const |
|||
{ |
|||
if (_value.IsEmpty()) |
|||
{ |
|||
// TODO: handle exceptions
|
|||
return ""; |
|||
} |
|||
else if (_value->IsUndefined()) |
|||
{ |
|||
return "undefined"; |
|||
} |
|||
v8::String::Utf8Value str(_value); |
|||
return *str ? *str : "<string conversion failed>"; |
|||
} |
@ -0,0 +1,69 @@ |
|||
//
|
|||
// Created by Marek Kotewicz on 27/04/15.
|
|||
//
|
|||
|
|||
#pragma once |
|||
|
|||
#include <v8.h> |
|||
#include "JSScope.h" |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
|
|||
class JSV8Env |
|||
{ |
|||
public: |
|||
JSV8Env(); |
|||
~JSV8Env(); |
|||
|
|||
private: |
|||
v8::Platform* m_platform; |
|||
}; |
|||
|
|||
v8::Handle<v8::Context> CreateShellContext(v8::Isolate* isolate) |
|||
{ |
|||
v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate); |
|||
return v8::Context::New(isolate, NULL, global); |
|||
} |
|||
|
|||
class JSV8DumbScope |
|||
{ |
|||
public: |
|||
JSV8DumbScope(v8::Isolate* _isolate): |
|||
m_isolateScope(_isolate), |
|||
m_handleScope(_isolate), |
|||
m_context(CreateShellContext(_isolate)), |
|||
m_contextScope(m_context) |
|||
{} |
|||
|
|||
v8::Handle <v8::Context> const& context() const { return m_context; } |
|||
|
|||
private: |
|||
v8::Isolate::Scope m_isolateScope; |
|||
v8::HandleScope m_handleScope; |
|||
v8::Handle <v8::Context> m_context; |
|||
v8::Context::Scope m_contextScope; |
|||
}; |
|||
|
|||
class JSV8ScopeBase : public JSScope |
|||
{ |
|||
public: |
|||
JSV8ScopeBase(); |
|||
|
|||
virtual ~JSV8ScopeBase(); |
|||
|
|||
const char* evaluate(const char* _cstr) const; |
|||
|
|||
private: |
|||
static JSV8Env s_env; |
|||
v8::Isolate* m_isolate; |
|||
JSV8DumbScope* m_scope; |
|||
|
|||
virtual const char* formatValue(v8::Handle <v8::Value> const &_value) const; |
|||
}; |
|||
|
|||
} |
|||
} |
|||
|
@ -0,0 +1,7 @@ |
|||
cmake_policy(SET CMP0015 NEW) |
|||
|
|||
aux_source_directory(. SRCS) |
|||
|
|||
add_sources(${SRCS}) |
|||
|
|||
|
@ -0,0 +1,21 @@ |
|||
//
|
|||
// Created by Marek Kotewicz on 27/04/15.
|
|||
//
|
|||
|
|||
#include <boost/test/unit_test.hpp> |
|||
#include "../../libethconsole/JSV8ScopeBase.h" |
|||
|
|||
using namespace std; |
|||
using namespace dev; |
|||
using namespace dev::eth; |
|||
|
|||
BOOST_AUTO_TEST_SUITE(jsscope) |
|||
|
|||
BOOST_AUTO_TEST_CASE(common) |
|||
{ |
|||
JSV8ScopeBase scope; |
|||
string result = scope.evaluate("1 + 1"); |
|||
BOOST_CHECK_EQUAL(result, "2"); |
|||
} |
|||
|
|||
BOOST_AUTO_TEST_SUITE_END() |
Loading…
Reference in new issue