Browse Source

libjsconsole init

cl-refactor
Marek Kotewicz 10 years ago
parent
commit
41afe1672e
  1. 1
      CMakeLists.txt
  2. 25
      libjsconsole/CMakeLists.txt
  3. 5
      libjsconsole/JSConsole.cpp
  4. 19
      libjsconsole/JSConsole.h
  5. 20
      libjsconsole/JSV8Console.cpp
  6. 29
      libjsconsole/JSV8Console.h
  7. 29
      libjsengine/JSV8Engine.cpp
  8. 2
      libjsengine/JSV8Engine.h
  9. 18
      libjsengine/JSV8Printer.cpp
  10. 23
      libjsengine/JSV8Printer.h
  11. 2
      test/libjsengine/CMakeLists.txt
  12. 35
      test/libjsengine/JSV8Engine.cpp
  13. 21
      test/libjsengine/JSV8ScopeBase.cpp

1
CMakeLists.txt

@ -328,6 +328,7 @@ if (JSONRPC)
endif()
add_subdirectory(libjsengine)
add_subdirectory(libjsconsole)
add_subdirectory(secp256k1)
add_subdirectory(libp2p)
add_subdirectory(libdevcrypto)

25
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} )

5
libjsconsole/JSConsole.cpp

@ -0,0 +1,5 @@
//
// Created by Marek Kotewicz on 28/04/15.
//
#include "JSConsole.h"

19
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;
};
}
}

20
libjsconsole/JSV8Console.cpp

@ -0,0 +1,20 @@
//
// Created by Marek Kotewicz on 28/04/15.
//
#include <libjsengine/JSV8Engine.h>
#include <libjsengine/JSV8Printer.h>
#include "JSV8Console.h"
using namespace dev;
using namespace dev::eth;
JSV8Console::JSV8Console(): m_engine(), m_printer(m_engine)
{
}
void JSV8Console::repl() const
{
}

29
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;
};
}
}

29
libjsengine/JSV8Engine.cpp

@ -2,8 +2,8 @@
// Created by Marek Kotewicz on 27/04/15.
//
#include <libplatform/libplatform.h>
#include <memory>
#include <libplatform/libplatform.h>
#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<v8::Value> 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::Value>();
}
v8::Handle<v8::Value> result = script->Run();
return formatOutputValue(result);
return script->Run();
}
v8::Handle<v8::Context> const& JSV8Engine::context() const
const char* JSV8Engine::evaluate(const char* _cstr) const
{
return m_scope->context();
}
v8::Handle<v8::Value> value = (eval(_cstr));
const char* JSV8Engine::formatOutputValue(v8::Handle<v8::Value> 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 : "<string conversion failed>";
}
v8::Handle<v8::Context> const& JSV8Engine::context() const
{
return m_scope->context();
}

2
libjsengine/JSV8Engine.h

@ -20,6 +20,7 @@ class JSV8Engine : public JSEngine
public:
JSV8Engine();
virtual ~JSV8Engine();
v8::Handle<v8::Value> eval(const char* _cstr) const;
const char* evaluate(const char* _cstr) const;
private:
@ -29,7 +30,6 @@ private:
protected:
v8::Handle<v8::Context> const& context() const;
virtual const char* formatOutputValue(v8::Handle<v8::Value> const& _value) const;
};
}

18
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<v8::Value> const& _value) const
{
return "";
}

23
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<v8::Value> const& _value) const;
};
}
}

2
test/libjsengine/CMakeLists.txt

@ -3,5 +3,3 @@ cmake_policy(SET CMP0015 NEW)
aux_source_directory(. SRCS)
add_sources(${SRCS})

35
test/libjsengine/JSV8Engine.cpp

@ -0,0 +1,35 @@
//
// Created by Marek Kotewicz on 27/04/15.
//
#include <boost/test/unit_test.hpp>
#include <libjsengine/JSV8Engine.h>
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()

21
test/libjsengine/JSV8ScopeBase.cpp

@ -1,21 +0,0 @@
//
// Created by Marek Kotewicz on 27/04/15.
//
#include <boost/test/unit_test.hpp>
#include <libjsengine/JSV8Engine.h>
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()
Loading…
Cancel
Save