Browse Source

wrap const char* in jsstring

cl-refactor
Marek Kotewicz 10 years ago
parent
commit
5c2be28a2d
  1. 4
      eth/main.cpp
  2. 2
      libjsconsole/JSConsole.cpp
  3. 13
      libjsengine/JSEngine.cpp
  4. 12
      libjsengine/JSEngine.h
  5. 6
      libjsengine/JSPrinter.h
  6. 2
      libjsengine/JSV8Engine.cpp
  7. 2
      libjsengine/JSV8Engine.h
  8. 2
      libjsengine/JSV8Printer.cpp
  9. 2
      libjsengine/JSV8Printer.h
  10. 14
      test/libjsengine/JSV8Engine.cpp

4
eth/main.cpp

@ -1645,12 +1645,8 @@ int main(int argc, char** argv)
} }
} }
else else
{
while (!g_exit) while (!g_exit)
{
this_thread::sleep_for(chrono::milliseconds(1000)); this_thread::sleep_for(chrono::milliseconds(1000));
}
}
StructuredLogger::stopping(clientImplString, dev::Version); StructuredLogger::stopping(clientImplString, dev::Version);
auto netData = web3.saveNetwork(); auto netData = web3.saveNetwork();

2
libjsconsole/JSConsole.cpp

@ -69,7 +69,7 @@ void JSConsole::repl() const
{ {
add_history(cmd.c_str()); add_history(cmd.c_str());
auto value = m_engine.eval(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; cout << result << endl;
} }
} }

13
libjsengine/JSEngine.cpp

@ -20,4 +20,17 @@
* Ethereum client. * Ethereum client.
*/ */
#include <string.h>
#include <stdlib.h>
#include "JSEngine.h" #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);
}

12
libjsengine/JSEngine.h

@ -27,10 +27,20 @@ namespace dev
namespace eth namespace eth
{ {
class JSString
{
public:
JSString(char const* _cstr);
~JSString();
char const* cstr() const { return m_cstr; }
private:
char* m_cstr;
};
class JSValue class JSValue
{ {
public: public:
virtual const char* asCString() const = 0; virtual JSString toString() const = 0;
}; };
template <typename T> template <typename T>

6
libjsengine/JSPrinter.h

@ -22,6 +22,8 @@
#pragma once #pragma once
#include "JSEngine.h"
namespace dev namespace dev
{ {
namespace eth namespace eth
@ -31,8 +33,8 @@ template <typename T>
class JSPrinter class JSPrinter
{ {
public: public:
virtual const char* print(T const& _value) const { return _value.asCString(); } virtual JSString print(T const& _value) const { return _value.toString(); }
virtual const char* prettyPrint(T const& _value) const { return print(_value); } virtual JSString prettyPrint(T const& _value) const { return print(_value); }
}; };
} }

2
libjsengine/JSV8Engine.cpp

@ -130,7 +130,7 @@ private:
JSV8Env JSV8Engine::s_env = JSV8Env(); JSV8Env JSV8Engine::s_env = JSV8Env();
const char* JSV8Value::asCString() const JSString JSV8Value::toString() const
{ {
if (m_value.IsEmpty()) if (m_value.IsEmpty())
return ""; return "";

2
libjsengine/JSV8Engine.h

@ -37,7 +37,7 @@ class JSV8Value : public JSValue
{ {
public: public:
JSV8Value(v8::Handle<v8::Value> _value): m_value(_value) {} JSV8Value(v8::Handle<v8::Value> _value): m_value(_value) {}
const char* asCString() const; JSString toString() const;
v8::Handle<v8::Value> const& value() const { return m_value; } v8::Handle<v8::Value> const& value() const { return m_value; }
private: private:

2
libjsengine/JSV8Printer.cpp

@ -35,7 +35,7 @@ JSV8Printer::JSV8Printer(JSV8Engine const& _engine): m_engine(_engine)
m_engine.eval(prettyPrint.c_str()); 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::HandleScope handleScope;
v8::Local<v8::String> pp = v8::String::New("prettyPrint"); v8::Local<v8::String> pp = v8::String::New("prettyPrint");

2
libjsengine/JSV8Printer.h

@ -34,7 +34,7 @@ class JSV8Printer : public JSPrinter<JSV8Value>
{ {
public: public:
JSV8Printer(JSV8Engine const& _engine); JSV8Printer(JSV8Engine const& _engine);
const char* prettyPrint(JSV8Value const& _value) const; JSString prettyPrint(JSV8Value const& _value) const;
private: private:
JSV8Engine const& m_engine; JSV8Engine const& m_engine;
}; };

14
test/libjsengine/JSV8Engine.cpp

@ -17,7 +17,7 @@ BOOST_AUTO_TEST_CASE(evalInteger)
JSV8Engine engine; JSV8Engine engine;
JSV8Printer printer(engine); JSV8Printer printer(engine);
auto value = engine.eval("1 + 1"); auto value = engine.eval("1 + 1");
string result = printer.print(value); string result = printer.print(value).cstr();
BOOST_CHECK_EQUAL(result, "2"); BOOST_CHECK_EQUAL(result, "2");
} }
@ -26,7 +26,7 @@ BOOST_AUTO_TEST_CASE(evalString)
JSV8Engine engine; JSV8Engine engine;
JSV8Printer printer(engine); JSV8Printer printer(engine);
auto value = engine.eval("'hello ' + 'world'"); auto value = engine.eval("'hello ' + 'world'");
string result = printer.print(value); string result = printer.print(value).cstr();
BOOST_CHECK_EQUAL(result, "hello world"); BOOST_CHECK_EQUAL(result, "hello world");
} }
@ -35,7 +35,7 @@ BOOST_AUTO_TEST_CASE(evalEmpty)
JSV8Engine engine; JSV8Engine engine;
JSV8Printer printer(engine); JSV8Printer printer(engine);
auto value = engine.eval(""); auto value = engine.eval("");
string result = printer.print(value); string result = printer.print(value).cstr();
BOOST_CHECK_EQUAL(result, "undefined"); BOOST_CHECK_EQUAL(result, "undefined");
} }
@ -44,7 +44,7 @@ BOOST_AUTO_TEST_CASE(evalAssignment)
JSV8Engine engine; JSV8Engine engine;
JSV8Printer printer(engine); JSV8Printer printer(engine);
auto value = engine.eval("x = 5"); auto value = engine.eval("x = 5");
string result = printer.print(value); string result = printer.print(value).cstr();
BOOST_CHECK_EQUAL(result, "5"); BOOST_CHECK_EQUAL(result, "5");
} }
@ -53,7 +53,7 @@ BOOST_AUTO_TEST_CASE(evalIncorrectExpression)
JSV8Engine engine; JSV8Engine engine;
JSV8Printer printer(engine); JSV8Printer printer(engine);
auto value = engine.eval("["); 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"); BOOST_CHECK_EQUAL(result, "Error: Uncaught SyntaxError: Unexpected end of input");
} }
@ -62,8 +62,8 @@ BOOST_AUTO_TEST_CASE(evalNull)
JSV8Engine engine; JSV8Engine engine;
JSV8Printer printer(engine); JSV8Printer printer(engine);
auto value = engine.eval("null"); auto value = engine.eval("null");
string result = printer.print(value); string result = printer.print(value).cstr();
string prettyResult = printer.prettyPrint(value); string prettyResult = printer.prettyPrint(value).cstr();
BOOST_CHECK_EQUAL(result, "null"); BOOST_CHECK_EQUAL(result, "null");
BOOST_CHECK_EQUAL(prettyResult.find("null") != std::string::npos, true); BOOST_CHECK_EQUAL(prettyResult.find("null") != std::string::npos, true);
} }

Loading…
Cancel
Save