Browse Source

cleanup, libjsengine

cl-refactor
Marek Kotewicz 10 years ago
parent
commit
72afc0c7d1
  1. 2
      CMakeLists.txt
  2. 2
      libdevcore/CommonIO.cpp
  3. 69
      libethconsole/JSV8ScopeBase.h
  4. 6
      libjsengine/CMakeLists.txt
  5. 2
      libjsengine/JSEngine.cpp
  6. 11
      libjsengine/JSEngine.h
  7. 76
      libjsengine/JSV8Engine.cpp
  8. 37
      libjsengine/JSV8Engine.h
  9. 4
      test/CMakeLists.txt
  10. 0
      test/libjsengine/CMakeLists.txt
  11. 4
      test/libjsengine/JSV8ScopeBase.cpp

2
CMakeLists.txt

@ -327,7 +327,7 @@ if (JSONRPC)
add_subdirectory(libweb3jsonrpc)
endif()
add_subdirectory(libethconsole)
add_subdirectory(libjsengine)
add_subdirectory(secp256k1)
add_subdirectory(libp2p)
add_subdirectory(libdevcrypto)

2
libdevcore/CommonIO.cpp

@ -30,7 +30,7 @@ string dev::memDump(bytes const& _bytes, unsigned _width, bool _html)
{
stringstream ret;
if (_html)
ret << "<pre style=\"font-family: Monospace,Lucida JSV8ScopeBase,Courier,Courier New,sans-serif; font-size: small\">";
ret << "<pre style=\"font-family: Monospace,Lucida JSV8Engine,Courier,Courier New,sans-serif; font-size: small\">";
for (unsigned i = 0; i < _bytes.size(); i += _width)
{
ret << hex << setw(4) << setfill('0') << i << " ";

69
libethconsole/JSV8ScopeBase.h

@ -1,69 +0,0 @@
//
// 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;
};
}
}

6
libethconsole/CMakeLists.txt → libjsengine/CMakeLists.txt

@ -8,6 +8,8 @@ endif()
set(CMAKE_AUTOMOC OFF)
# macos brew version of v8 needs to be compiled with libstdc++
# it also needs to be dynamic library
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libstdc++")
aux_source_directory(. SRC_LIST)
@ -15,11 +17,11 @@ aux_source_directory(. SRC_LIST)
include_directories(BEFORE ..)
include_directories(${V8_INCLUDE_DIRS})
set(EXECUTABLE ethconsole)
set(EXECUTABLE jsengine)
file(GLOB HEADERS "*.h")
add_library(${EXECUTABLE} SHARED ${SRC_LIST} ${HEADERS})
add_library(${EXECUTABLE} ${SRC_LIST} ${HEADERS})
target_link_libraries(${EXECUTABLE} ${V8_LIBRARIES})

2
libethconsole/JSScope.cpp → libjsengine/JSEngine.cpp

@ -2,4 +2,4 @@
// Created by Marek Kotewicz on 27/04/15.
//
#include "JSScope.h"
#include "JSEngine.h"

11
libethconsole/JSScope.h → libjsengine/JSEngine.h

@ -9,15 +9,12 @@ namespace dev
namespace eth
{
class JSScope
class JSEngine
{
public:
JSScope()
{ };
virtual ~JSScope()
{ };
JSEngine() {};
virtual ~JSEngine() {};
// should be used to evalute javascript expression
virtual const char* evaluate(const char* _cstr) const = 0;
};

76
libethconsole/JSV8ScopeBase.cpp → libjsengine/JSV8Engine.cpp

@ -4,12 +4,15 @@
#include <libplatform/libplatform.h>
#include <memory>
#include "JSV8ScopeBase.h"
#include "JSV8Engine.h"
using namespace dev;
using namespace dev::eth;
JSV8Env JSV8ScopeBase::s_env = JSV8Env();
namespace dev
{
namespace eth
{
class ShellArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
public:
@ -21,6 +24,52 @@ public:
virtual void Free(void* data, size_t) { free(data); }
};
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);
v8::Handle<v8::Context> context = v8::Context::New(isolate, NULL, global);
if (context.IsEmpty())
{
// TODO: throw an exception
}
return context;
}
class JSV8Scope
{
public:
JSV8Scope(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;
};
}
}
JSV8Env JSV8Engine::s_env = JSV8Env();
JSV8Env::JSV8Env()
{
static bool initialized = false;
@ -42,23 +91,23 @@ JSV8Env::~JSV8Env()
delete m_platform;
}
JSV8ScopeBase::JSV8ScopeBase():
JSV8Engine::JSV8Engine():
m_isolate(v8::Isolate::New()),
m_scope(new JSV8DumbScope(m_isolate))
m_scope(new JSV8Scope(m_isolate))
{ }
JSV8ScopeBase::~JSV8ScopeBase()
JSV8Engine::~JSV8Engine()
{
delete m_scope;
m_isolate->Dispose();
}
const char* JSV8ScopeBase::evaluate(const char* _cstr) const
const char* JSV8Engine::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::Local<v8::String> source = v8::String::NewFromUtf8(context()->GetIsolate(), _cstr);
v8::Local<v8::String> name(v8::String::NewFromUtf8(context()->GetIsolate(), "(shell)"));
v8::ScriptOrigin origin(name);
v8::Handle<v8::Script> script = v8::Script::Compile(source, &origin);
if (script.IsEmpty())
@ -68,10 +117,15 @@ const char* JSV8ScopeBase::evaluate(const char* _cstr) const
}
v8::Handle<v8::Value> result = script->Run();
return formatValue(result);
return formatOutputValue(result);
}
const char* JSV8ScopeBase::formatValue(v8::Handle<v8::Value> const &_value) const
v8::Handle<v8::Context> const& JSV8Engine::context() const
{
return m_scope->context();
}
const char* JSV8Engine::formatOutputValue(v8::Handle<v8::Value> const& _value) const
{
if (_value.IsEmpty())
{
@ -79,9 +133,7 @@ const char* JSV8ScopeBase::formatValue(v8::Handle<v8::Value> const &_value) cons
return "";
}
else if (_value->IsUndefined())
{
return "undefined";
}
v8::String::Utf8Value str(_value);
return *str ? *str : "<string conversion failed>";
}

37
libjsengine/JSV8Engine.h

@ -0,0 +1,37 @@
//
// Created by Marek Kotewicz on 27/04/15.
//
#pragma once
#include <v8.h>
#include "JSEngine.h"
namespace dev
{
namespace eth
{
class JSV8Env;
class JSV8Scope;
class JSV8Engine : public JSEngine
{
public:
JSV8Engine();
virtual ~JSV8Engine();
const char* evaluate(const char* _cstr) const;
private:
static JSV8Env s_env;
v8::Isolate* m_isolate;
JSV8Scope* m_scope;
protected:
v8::Handle<v8::Context> const& context() const;
virtual const char* formatOutputValue(v8::Handle<v8::Value> const& _value) const;
};
}
}

4
test/CMakeLists.txt

@ -25,7 +25,7 @@ add_subdirectory(libethereum)
add_subdirectory(libevm)
add_subdirectory(libnatspec)
add_subdirectory(libp2p)
add_subdirectory(libethconsole)
add_subdirectory(libjsengine)
if (SOLIDITY)
add_subdirectory(libsolidity)
@ -68,7 +68,7 @@ target_link_libraries(testeth ${CURL_LIBRARIES})
target_link_libraries(testeth ethereum)
target_link_libraries(testeth ethcore)
target_link_libraries(testeth secp256k1)
target_link_libraries(testeth ethconsole)
target_link_libraries(testeth jsengine)
if (SOLIDITY)
target_link_libraries(testeth solidity)
endif ()

0
test/libethconsole/CMakeLists.txt → test/libjsengine/CMakeLists.txt

4
test/libethconsole/JSV8ScopeBase.cpp → test/libjsengine/JSV8ScopeBase.cpp

@ -3,7 +3,7 @@
//
#include <boost/test/unit_test.hpp>
#include "../../libethconsole/JSV8ScopeBase.h"
#include <libjsengine/JSV8Engine.h>
using namespace std;
using namespace dev;
@ -13,7 +13,7 @@ BOOST_AUTO_TEST_SUITE(jsscope)
BOOST_AUTO_TEST_CASE(common)
{
JSV8ScopeBase scope;
JSV8Engine scope;
string result = scope.evaluate("1 + 1");
BOOST_CHECK_EQUAL(result, "2");
}
Loading…
Cancel
Save