Browse Source

pretty printing

cl-refactor
Marek Kotewicz 10 years ago
parent
commit
1055b10b30
  1. 2
      libjsconsole/JSConsole.cpp
  2. 4
      libjsengine/JSV8Engine.h
  3. 12
      libjsengine/JSV8Printer.cpp
  4. 2
      libjsengine/JSV8Printer.h

2
libjsconsole/JSConsole.cpp

@ -51,7 +51,7 @@ void JSConsole::repl() const
{
add_history(cmd.c_str());
auto value = m_engine.eval(cmd.c_str());
string result = m_printer.print(value);
string result = m_printer.prettyPrint(value);
cout << result << endl;
}
}

4
libjsengine/JSV8Engine.h

@ -32,14 +32,12 @@ public:
JSV8Engine();
virtual ~JSV8Engine();
JSV8Value eval(const char* _cstr) const;
v8::Handle<v8::Context> const& context() const;
private:
static JSV8Env s_env;
v8::Isolate* m_isolate;
JSV8Scope* m_scope;
protected:
v8::Handle<v8::Context> const& context() const;
};
}

12
libjsengine/JSV8Printer.cpp

@ -10,14 +10,20 @@ using namespace std;
using namespace dev;
using namespace eth;
JSV8Printer::JSV8Printer(JSV8Engine const& _engine)
JSV8Printer::JSV8Printer(JSV8Engine const& _engine): m_engine(_engine)
{
JSEngineResources resources;
string prettyPrint = resources.loadResourceAsString("pretty_print");
_engine.eval(prettyPrint.c_str());
m_engine.eval(prettyPrint.c_str());
}
const char* JSV8Printer::prettyPrint(JSV8Value const& _value) const
{
return nullptr;
v8::HandleScope handleScope(m_engine.context()->GetIsolate());
v8::Local<v8::String> pp = v8::String::NewFromUtf8(m_engine.context()->GetIsolate(), "prettyPrint");
v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(m_engine.context()->Global()->Get(pp));
v8::Local<v8::Value> values[1] = {_value.value()};
v8::Local<v8::Value> res = v8::Local<v8::Value>::Cast(func->Call(func, 1, values));
v8::String::Utf8Value str(res);
return *str ? *str : "<pretty print conversion failed>";
}

2
libjsengine/JSV8Printer.h

@ -17,6 +17,8 @@ class JSV8Printer : public JSPrinter<JSV8Value>
public:
JSV8Printer(JSV8Engine const& _engine);
const char* prettyPrint(JSV8Value const& _value) const;
private:
JSV8Engine const& m_engine;
};
}

Loading…
Cancel
Save