From 72afc0c7d1602d6b24a574ec117631816553a798 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Mon, 27 Apr 2015 22:20:57 +0200 Subject: [PATCH] cleanup, libjsengine --- CMakeLists.txt | 2 +- libdevcore/CommonIO.cpp | 2 +- libethconsole/JSV8ScopeBase.h | 69 ----------------- {libethconsole => libjsengine}/CMakeLists.txt | 6 +- .../JSScope.cpp => libjsengine/JSEngine.cpp | 2 +- .../JSScope.h => libjsengine/JSEngine.h | 11 +-- .../JSV8Engine.cpp | 76 ++++++++++++++++--- libjsengine/JSV8Engine.h | 37 +++++++++ test/CMakeLists.txt | 4 +- .../CMakeLists.txt | 0 .../JSV8ScopeBase.cpp | 4 +- 11 files changed, 116 insertions(+), 97 deletions(-) delete mode 100644 libethconsole/JSV8ScopeBase.h rename {libethconsole => libjsengine}/CMakeLists.txt (79%) rename libethconsole/JSScope.cpp => libjsengine/JSEngine.cpp (69%) rename libethconsole/JSScope.h => libjsengine/JSEngine.h (61%) rename libethconsole/JSV8ScopeBase.cpp => libjsengine/JSV8Engine.cpp (52%) create mode 100644 libjsengine/JSV8Engine.h rename test/{libethconsole => libjsengine}/CMakeLists.txt (100%) rename test/{libethconsole => libjsengine}/JSV8ScopeBase.cpp (82%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7e9ec4742..75aa0d80b 100644 --- a/CMakeLists.txt +++ b/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) diff --git a/libdevcore/CommonIO.cpp b/libdevcore/CommonIO.cpp index cf98254c4..5b90a9079 100644 --- a/libdevcore/CommonIO.cpp +++ b/libdevcore/CommonIO.cpp @@ -30,7 +30,7 @@ string dev::memDump(bytes const& _bytes, unsigned _width, bool _html) { stringstream ret; if (_html) - ret << "
";
+		ret << "
";
 	for (unsigned i = 0; i < _bytes.size(); i += _width)
 	{
 		ret << hex << setw(4) << setfill('0') << i << " ";
diff --git a/libethconsole/JSV8ScopeBase.h b/libethconsole/JSV8ScopeBase.h
deleted file mode 100644
index 2f7d46c79..000000000
--- a/libethconsole/JSV8ScopeBase.h
+++ /dev/null
@@ -1,69 +0,0 @@
-//
-// Created by Marek Kotewicz on 27/04/15.
-//
-
-#pragma once
-
-#include 
-#include "JSScope.h"
-
-namespace dev
-{
-namespace eth
-{
-
-class JSV8Env
-{
-public:
-	JSV8Env();
-	~JSV8Env();
-
-private:
-	v8::Platform* m_platform;
-};
-
-v8::Handle CreateShellContext(v8::Isolate* isolate)
-{
-	v8::Handle 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  const& context() const { return m_context; }
-
-private:
-	v8::Isolate::Scope m_isolateScope;
-	v8::HandleScope m_handleScope;
-	v8::Handle  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  const &_value) const;
-};
-
-}
-}
-
diff --git a/libethconsole/CMakeLists.txt b/libjsengine/CMakeLists.txt
similarity index 79%
rename from libethconsole/CMakeLists.txt
rename to libjsengine/CMakeLists.txt
index d62da84ff..aaf933eb6 100644
--- a/libethconsole/CMakeLists.txt
+++ b/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})
 
diff --git a/libethconsole/JSScope.cpp b/libjsengine/JSEngine.cpp
similarity index 69%
rename from libethconsole/JSScope.cpp
rename to libjsengine/JSEngine.cpp
index 10795e9b0..45898cacd 100644
--- a/libethconsole/JSScope.cpp
+++ b/libjsengine/JSEngine.cpp
@@ -2,4 +2,4 @@
 // Created by Marek Kotewicz on 27/04/15.
 //
 
-#include "JSScope.h"
+#include "JSEngine.h"
diff --git a/libethconsole/JSScope.h b/libjsengine/JSEngine.h
similarity index 61%
rename from libethconsole/JSScope.h
rename to libjsengine/JSEngine.h
index 18d6c863c..5f39fe937 100644
--- a/libethconsole/JSScope.h
+++ b/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;
 };
 
diff --git a/libethconsole/JSV8ScopeBase.cpp b/libjsengine/JSV8Engine.cpp
similarity index 52%
rename from libethconsole/JSV8ScopeBase.cpp
rename to libjsengine/JSV8Engine.cpp
index 9e3516812..ac0370042 100644
--- a/libethconsole/JSV8ScopeBase.cpp
+++ b/libjsengine/JSV8Engine.cpp
@@ -4,12 +4,15 @@
 
 #include 
 #include 
-#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 createShellContext(v8::Isolate* isolate)
+{
+	v8::Handle global = v8::ObjectTemplate::New(isolate);
+	v8::Handle 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  const& context() const { return m_context; }
+
+private:
+	v8::Isolate::Scope m_isolateScope;
+	v8::HandleScope m_handleScope;
+	v8::Handle  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 source = v8::String::NewFromUtf8(m_scope->context()->GetIsolate(), _cstr);
-	v8::Local name(v8::String::NewFromUtf8(m_scope->context()->GetIsolate(), "(shell)"));
+	v8::Local source = v8::String::NewFromUtf8(context()->GetIsolate(), _cstr);
+	v8::Local name(v8::String::NewFromUtf8(context()->GetIsolate(), "(shell)"));
 	v8::ScriptOrigin origin(name);
 	v8::Handle script = v8::Script::Compile(source, &origin);
 	if (script.IsEmpty())
@@ -68,10 +117,15 @@ const char* JSV8ScopeBase::evaluate(const char* _cstr) const
 	}
 
 	v8::Handle result = script->Run();
-	return formatValue(result);
+	return formatOutputValue(result);
 }
 
-const char* JSV8ScopeBase::formatValue(v8::Handle const &_value) const
+v8::Handle const& JSV8Engine::context() const
+{
+	return m_scope->context();
+}
+
+const char* JSV8Engine::formatOutputValue(v8::Handle const& _value) const
 {
 	if (_value.IsEmpty())
 	{
@@ -79,9 +133,7 @@ const char* JSV8ScopeBase::formatValue(v8::Handle const &_value) cons
 		return "";
 	}
 	else if (_value->IsUndefined())
-	{
 		return "undefined";
-	}
 	v8::String::Utf8Value str(_value);
 	return *str ? *str : "";
 }
diff --git a/libjsengine/JSV8Engine.h b/libjsengine/JSV8Engine.h
new file mode 100644
index 000000000..ebcf80402
--- /dev/null
+++ b/libjsengine/JSV8Engine.h
@@ -0,0 +1,37 @@
+//
+// Created by Marek Kotewicz on 27/04/15.
+//
+
+#pragma once
+
+#include 
+#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 const& context() const;
+	virtual const char* formatOutputValue(v8::Handle const& _value) const;
+};
+
+}
+}
+
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 2413fc0a9..349ca9800 100644
--- a/test/CMakeLists.txt
+++ b/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 ()
diff --git a/test/libethconsole/CMakeLists.txt b/test/libjsengine/CMakeLists.txt
similarity index 100%
rename from test/libethconsole/CMakeLists.txt
rename to test/libjsengine/CMakeLists.txt
diff --git a/test/libethconsole/JSV8ScopeBase.cpp b/test/libjsengine/JSV8ScopeBase.cpp
similarity index 82%
rename from test/libethconsole/JSV8ScopeBase.cpp
rename to test/libjsengine/JSV8ScopeBase.cpp
index 5bd7bf6a5..cf848e2c9 100644
--- a/test/libethconsole/JSV8ScopeBase.cpp
+++ b/test/libjsengine/JSV8ScopeBase.cpp
@@ -3,7 +3,7 @@
 //
 
 #include 
-#include "../../libethconsole/JSV8ScopeBase.h"
+#include 
 
 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");
 }