diff --git a/CMakeLists.txt b/CMakeLists.txt index 75aa0d80b..97d108e6c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -328,6 +328,7 @@ if (JSONRPC) endif() add_subdirectory(libjsengine) +add_subdirectory(libjsconsole) add_subdirectory(secp256k1) add_subdirectory(libp2p) add_subdirectory(libdevcrypto) diff --git a/libjsconsole/CMakeLists.txt b/libjsconsole/CMakeLists.txt new file mode 100644 index 000000000..cac47fc6c --- /dev/null +++ b/libjsconsole/CMakeLists.txt @@ -0,0 +1,25 @@ +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) + +aux_source_directory(. SRC_LIST) + +include_directories(BEFORE ..) +include_directories(${V8_INCLUDE_DIRS}) + +set(EXECUTABLE jsconsole) + +file(GLOB HEADERS "*.h") + +add_library(${EXECUTABLE} ${SRC_LIST} ${HEADERS}) + +target_link_libraries(${EXECUTABLE} jsengine) + +install( TARGETS ${EXECUTABLE} RUNTIME DESTINATION bin ARCHIVE DESTINATION lib LIBRARY DESTINATION lib ) +install( FILES ${HEADERS} DESTINATION include/${EXECUTABLE} ) diff --git a/libjsconsole/JSConsole.cpp b/libjsconsole/JSConsole.cpp new file mode 100644 index 000000000..5ba1912ff --- /dev/null +++ b/libjsconsole/JSConsole.cpp @@ -0,0 +1,5 @@ +// +// Created by Marek Kotewicz on 28/04/15. +// + +#include "JSConsole.h" diff --git a/libjsconsole/JSConsole.h b/libjsconsole/JSConsole.h new file mode 100644 index 000000000..3e9124114 --- /dev/null +++ b/libjsconsole/JSConsole.h @@ -0,0 +1,19 @@ +// +// Created by Marek Kotewicz on 28/04/15. +// + +#pragma once + +namespace dev +{ +namespace eth +{ + +class JSConsole +{ +public: + virtual void repl() const = 0; +}; + +} +} diff --git a/libjsconsole/JSV8Console.cpp b/libjsconsole/JSV8Console.cpp new file mode 100644 index 000000000..7889b24c5 --- /dev/null +++ b/libjsconsole/JSV8Console.cpp @@ -0,0 +1,20 @@ +// +// Created by Marek Kotewicz on 28/04/15. +// + +#include +#include +#include "JSV8Console.h" + +using namespace dev; +using namespace dev::eth; + +JSV8Console::JSV8Console(): m_engine(), m_printer(m_engine) +{ + +} + +void JSV8Console::repl() const +{ + +} diff --git a/libjsconsole/JSV8Console.h b/libjsconsole/JSV8Console.h new file mode 100644 index 000000000..929a56f0b --- /dev/null +++ b/libjsconsole/JSV8Console.h @@ -0,0 +1,29 @@ +// +// Created by Marek Kotewicz on 28/04/15. +// + +#pragma once + +#include "JSConsole.h" + +namespace dev +{ +namespace eth +{ + +class JSV8Engine; +class JSV8Printer; + +class JSV8Console : public JSConsole +{ +public: + JSV8Console(); + void repl() const; + +private: + JSV8Engine m_engine; + JSV8Printer m_printer; +}; + +} +} diff --git a/libjsengine/JSV8Engine.cpp b/libjsengine/JSV8Engine.cpp index ac0370042..80c28a3dd 100644 --- a/libjsengine/JSV8Engine.cpp +++ b/libjsengine/JSV8Engine.cpp @@ -2,8 +2,8 @@ // Created by Marek Kotewicz on 27/04/15. // -#include #include +#include #include "JSV8Engine.h" using namespace dev; @@ -94,7 +94,7 @@ JSV8Env::~JSV8Env() JSV8Engine::JSV8Engine(): m_isolate(v8::Isolate::New()), m_scope(new JSV8Scope(m_isolate)) -{ } +{} JSV8Engine::~JSV8Engine() { @@ -102,7 +102,7 @@ JSV8Engine::~JSV8Engine() m_isolate->Dispose(); } -const char* JSV8Engine::evaluate(const char* _cstr) const +v8::Handle JSV8Engine::eval(const char* _cstr) const { v8::HandleScope handleScope(m_isolate); // v8::TryCatch tryCatch; @@ -113,27 +113,28 @@ const char* JSV8Engine::evaluate(const char* _cstr) const if (script.IsEmpty()) { // TODO: handle exceptions - return ""; + return v8::Handle(); } - v8::Handle result = script->Run(); - return formatOutputValue(result); + return script->Run(); } -v8::Handle const& JSV8Engine::context() const +const char* JSV8Engine::evaluate(const char* _cstr) const { - return m_scope->context(); -} + v8::Handle value = (eval(_cstr)); -const char* JSV8Engine::formatOutputValue(v8::Handle const& _value) const -{ - if (_value.IsEmpty()) + if (value.IsEmpty()) { // TODO: handle exceptions return ""; } - else if (_value->IsUndefined()) + else if (value->IsUndefined()) return "undefined"; - v8::String::Utf8Value str(_value); + v8::String::Utf8Value str(value); return *str ? *str : ""; } + +v8::Handle const& JSV8Engine::context() const +{ + return m_scope->context(); +} diff --git a/libjsengine/JSV8Engine.h b/libjsengine/JSV8Engine.h index ebcf80402..a8d8d4a6c 100644 --- a/libjsengine/JSV8Engine.h +++ b/libjsengine/JSV8Engine.h @@ -20,6 +20,7 @@ class JSV8Engine : public JSEngine public: JSV8Engine(); virtual ~JSV8Engine(); + v8::Handle eval(const char* _cstr) const; const char* evaluate(const char* _cstr) const; private: @@ -29,7 +30,6 @@ private: protected: v8::Handle const& context() const; - virtual const char* formatOutputValue(v8::Handle const& _value) const; }; } diff --git a/libjsengine/JSV8Printer.cpp b/libjsengine/JSV8Printer.cpp new file mode 100644 index 000000000..d61489d56 --- /dev/null +++ b/libjsengine/JSV8Printer.cpp @@ -0,0 +1,18 @@ +// +// Created by Marek Kotewicz on 28/04/15. +// + +#include "JSV8Printer.h" + +using namespace dev; +using namespace eth; + +JSV8Printer::JSV8Printer(JSV8Engine const& _engine) +{ + +} + +const char* JSV8Printer::print(v8::Handle const& _value) const +{ + return ""; +} diff --git a/libjsengine/JSV8Printer.h b/libjsengine/JSV8Printer.h new file mode 100644 index 000000000..08113960a --- /dev/null +++ b/libjsengine/JSV8Printer.h @@ -0,0 +1,23 @@ +// +// Created by Marek Kotewicz on 28/04/15. +// + +#pragma once + +#include "JSV8Engine.h" + + +namespace dev +{ +namespace eth +{ + +class JSV8Printer +{ +public: + JSV8Printer(JSV8Engine const& _engine); + const char* print(v8::Handle const& _value) const; +}; + +} +} diff --git a/test/libjsengine/CMakeLists.txt b/test/libjsengine/CMakeLists.txt index 610c58889..3ceda13b0 100644 --- a/test/libjsengine/CMakeLists.txt +++ b/test/libjsengine/CMakeLists.txt @@ -3,5 +3,3 @@ cmake_policy(SET CMP0015 NEW) aux_source_directory(. SRCS) add_sources(${SRCS}) - - diff --git a/test/libjsengine/JSV8Engine.cpp b/test/libjsengine/JSV8Engine.cpp new file mode 100644 index 000000000..b65f07d4d --- /dev/null +++ b/test/libjsengine/JSV8Engine.cpp @@ -0,0 +1,35 @@ +// +// Created by Marek Kotewicz on 27/04/15. +// + +#include +#include + +using namespace std; +using namespace dev; +using namespace dev::eth; + +BOOST_AUTO_TEST_SUITE(jsv8engine) + +BOOST_AUTO_TEST_CASE(evalInteger) +{ + JSV8Engine scope; + string result = scope.evaluate("1 + 1"); + BOOST_CHECK_EQUAL(result, "2"); +} + +BOOST_AUTO_TEST_CASE(evalString) +{ + JSV8Engine scope; + string result = scope.evaluate("'hello ' + 'world'"); + BOOST_CHECK_EQUAL(result, "hello world"); +} + +BOOST_AUTO_TEST_CASE(evalEmpty) +{ + JSV8Engine scope; + string result = scope.evaluate(""); + BOOST_CHECK_EQUAL(result, "undefined"); +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/test/libjsengine/JSV8ScopeBase.cpp b/test/libjsengine/JSV8ScopeBase.cpp deleted file mode 100644 index cf848e2c9..000000000 --- a/test/libjsengine/JSV8ScopeBase.cpp +++ /dev/null @@ -1,21 +0,0 @@ -// -// Created by Marek Kotewicz on 27/04/15. -// - -#include -#include - -using namespace std; -using namespace dev; -using namespace dev::eth; - -BOOST_AUTO_TEST_SUITE(jsscope) - -BOOST_AUTO_TEST_CASE(common) -{ - JSV8Engine scope; - string result = scope.evaluate("1 + 1"); - BOOST_CHECK_EQUAL(result, "2"); -} - -BOOST_AUTO_TEST_SUITE_END()