From 5c2be28a2d2b009327c108a97b89b8e6e7c046c8 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Tue, 5 May 2015 10:37:14 +0200 Subject: [PATCH] wrap const char* in jsstring --- eth/main.cpp | 4 ---- libjsconsole/JSConsole.cpp | 2 +- libjsengine/JSEngine.cpp | 13 +++++++++++++ libjsengine/JSEngine.h | 12 +++++++++++- libjsengine/JSPrinter.h | 6 ++++-- libjsengine/JSV8Engine.cpp | 2 +- libjsengine/JSV8Engine.h | 2 +- libjsengine/JSV8Printer.cpp | 2 +- libjsengine/JSV8Printer.h | 2 +- test/libjsengine/JSV8Engine.cpp | 14 +++++++------- 10 files changed, 40 insertions(+), 19 deletions(-) diff --git a/eth/main.cpp b/eth/main.cpp index f1b2c0358..bd8f7d516 100644 --- a/eth/main.cpp +++ b/eth/main.cpp @@ -1645,12 +1645,8 @@ int main(int argc, char** argv) } } else - { while (!g_exit) - { this_thread::sleep_for(chrono::milliseconds(1000)); - } - } StructuredLogger::stopping(clientImplString, dev::Version); auto netData = web3.saveNetwork(); diff --git a/libjsconsole/JSConsole.cpp b/libjsconsole/JSConsole.cpp index c59c1e198..e084f010f 100644 --- a/libjsconsole/JSConsole.cpp +++ b/libjsconsole/JSConsole.cpp @@ -69,7 +69,7 @@ void JSConsole::repl() const { add_history(cmd.c_str()); auto value = m_engine.eval(cmd.c_str()); - string result = m_printer.prettyPrint(value); + string result = m_printer.prettyPrint(value).cstr(); cout << result << endl; } } diff --git a/libjsengine/JSEngine.cpp b/libjsengine/JSEngine.cpp index 47afbd34e..f4a5a54c1 100644 --- a/libjsengine/JSEngine.cpp +++ b/libjsengine/JSEngine.cpp @@ -20,4 +20,17 @@ * Ethereum client. */ +#include +#include #include "JSEngine.h" + +using namespace dev; +using namespace dev::eth; + +JSString::JSString(char const *_cstr): m_cstr(strdup(_cstr)) {} + +JSString::~JSString() +{ + if (m_cstr) + free(m_cstr); +} diff --git a/libjsengine/JSEngine.h b/libjsengine/JSEngine.h index 7df55c2f5..275ffd2ec 100644 --- a/libjsengine/JSEngine.h +++ b/libjsengine/JSEngine.h @@ -27,10 +27,20 @@ namespace dev namespace eth { +class JSString +{ +public: + JSString(char const* _cstr); + ~JSString(); + char const* cstr() const { return m_cstr; } +private: + char* m_cstr; +}; + class JSValue { public: - virtual const char* asCString() const = 0; + virtual JSString toString() const = 0; }; template diff --git a/libjsengine/JSPrinter.h b/libjsengine/JSPrinter.h index 173ac2c87..d5f84d2d3 100644 --- a/libjsengine/JSPrinter.h +++ b/libjsengine/JSPrinter.h @@ -22,6 +22,8 @@ #pragma once +#include "JSEngine.h" + namespace dev { namespace eth @@ -31,8 +33,8 @@ template class JSPrinter { public: - virtual const char* print(T const& _value) const { return _value.asCString(); } - virtual const char* prettyPrint(T const& _value) const { return print(_value); } + virtual JSString print(T const& _value) const { return _value.toString(); } + virtual JSString prettyPrint(T const& _value) const { return print(_value); } }; } diff --git a/libjsengine/JSV8Engine.cpp b/libjsengine/JSV8Engine.cpp index 26e4479e8..78710c037 100644 --- a/libjsengine/JSV8Engine.cpp +++ b/libjsengine/JSV8Engine.cpp @@ -130,7 +130,7 @@ private: JSV8Env JSV8Engine::s_env = JSV8Env(); -const char* JSV8Value::asCString() const +JSString JSV8Value::toString() const { if (m_value.IsEmpty()) return ""; diff --git a/libjsengine/JSV8Engine.h b/libjsengine/JSV8Engine.h index 0ec982b8b..085d8ffbe 100644 --- a/libjsengine/JSV8Engine.h +++ b/libjsengine/JSV8Engine.h @@ -37,7 +37,7 @@ class JSV8Value : public JSValue { public: JSV8Value(v8::Handle _value): m_value(_value) {} - const char* asCString() const; + JSString toString() const; v8::Handle const& value() const { return m_value; } private: diff --git a/libjsengine/JSV8Printer.cpp b/libjsengine/JSV8Printer.cpp index 622c8d7dc..d76fdb95c 100644 --- a/libjsengine/JSV8Printer.cpp +++ b/libjsengine/JSV8Printer.cpp @@ -35,7 +35,7 @@ JSV8Printer::JSV8Printer(JSV8Engine const& _engine): m_engine(_engine) m_engine.eval(prettyPrint.c_str()); } -const char* JSV8Printer::prettyPrint(JSV8Value const& _value) const +JSString JSV8Printer::prettyPrint(JSV8Value const& _value) const { v8::HandleScope handleScope; v8::Local pp = v8::String::New("prettyPrint"); diff --git a/libjsengine/JSV8Printer.h b/libjsengine/JSV8Printer.h index b02b025f8..0d9d06e0d 100644 --- a/libjsengine/JSV8Printer.h +++ b/libjsengine/JSV8Printer.h @@ -34,7 +34,7 @@ class JSV8Printer : public JSPrinter { public: JSV8Printer(JSV8Engine const& _engine); - const char* prettyPrint(JSV8Value const& _value) const; + JSString prettyPrint(JSV8Value const& _value) const; private: JSV8Engine const& m_engine; }; diff --git a/test/libjsengine/JSV8Engine.cpp b/test/libjsengine/JSV8Engine.cpp index 2f319ab71..da6a0da0e 100644 --- a/test/libjsengine/JSV8Engine.cpp +++ b/test/libjsengine/JSV8Engine.cpp @@ -17,7 +17,7 @@ BOOST_AUTO_TEST_CASE(evalInteger) JSV8Engine engine; JSV8Printer printer(engine); auto value = engine.eval("1 + 1"); - string result = printer.print(value); + string result = printer.print(value).cstr(); BOOST_CHECK_EQUAL(result, "2"); } @@ -26,7 +26,7 @@ BOOST_AUTO_TEST_CASE(evalString) JSV8Engine engine; JSV8Printer printer(engine); auto value = engine.eval("'hello ' + 'world'"); - string result = printer.print(value); + string result = printer.print(value).cstr(); BOOST_CHECK_EQUAL(result, "hello world"); } @@ -35,7 +35,7 @@ BOOST_AUTO_TEST_CASE(evalEmpty) JSV8Engine engine; JSV8Printer printer(engine); auto value = engine.eval(""); - string result = printer.print(value); + string result = printer.print(value).cstr(); BOOST_CHECK_EQUAL(result, "undefined"); } @@ -44,7 +44,7 @@ BOOST_AUTO_TEST_CASE(evalAssignment) JSV8Engine engine; JSV8Printer printer(engine); auto value = engine.eval("x = 5"); - string result = printer.print(value); + string result = printer.print(value).cstr(); BOOST_CHECK_EQUAL(result, "5"); } @@ -53,7 +53,7 @@ BOOST_AUTO_TEST_CASE(evalIncorrectExpression) JSV8Engine engine; JSV8Printer printer(engine); auto value = engine.eval("["); - string result = printer.print(value); + string result = printer.print(value).cstr(); BOOST_CHECK_EQUAL(result, "Error: Uncaught SyntaxError: Unexpected end of input"); } @@ -62,8 +62,8 @@ BOOST_AUTO_TEST_CASE(evalNull) JSV8Engine engine; JSV8Printer printer(engine); auto value = engine.eval("null"); - string result = printer.print(value); - string prettyResult = printer.prettyPrint(value); + string result = printer.print(value).cstr(); + string prettyResult = printer.prettyPrint(value).cstr(); BOOST_CHECK_EQUAL(result, "null"); BOOST_CHECK_EQUAL(prettyResult.find("null") != std::string::npos, true); }