Browse Source

Fix pretty printer and implement console.log/debug/error.

cl-refactor
chriseth 10 years ago
parent
commit
7655bcab4b
  1. 9
      libjsengine/Common.js
  2. 22
      libjsengine/JSV8Engine.cpp
  3. 4
      libjsengine/PrettyPrint.js

9
libjsengine/Common.js

@ -1,11 +1,4 @@
console = {};
console.log = function () {
};
console.warn = function () {
};
console.error = function () {
};
setTimeout = function () { setTimeout = function () {
console.error("setTimeout not available in this environment.");
}; };

22
libjsengine/JSV8Engine.cpp

@ -22,6 +22,7 @@
#include <memory> #include <memory>
#include "JSV8Engine.h" #include "JSV8Engine.h"
#include "JSV8Printer.h"
#include "libjsengine/JSEngineResources.hpp" #include "libjsengine/JSEngineResources.hpp"
#include "BuildInfo.h" #include "BuildInfo.h"
@ -91,6 +92,16 @@ void reportException(v8::TryCatch* _tryCatch)
} }
} }
v8::Handle<v8::Value> ConsoleLog(v8::Arguments const& _args)
{
auto engine = reinterpret_cast<JSV8Engine const*>(v8::External::Unwrap(_args.Data()));
JSV8Printer printer(*engine);
for (int i = 0; i < _args.Length(); ++i)
printf("%s\n", printer.prettyPrint(_args[i]).cstr());
return v8::Undefined();
}
class JSV8Scope class JSV8Scope
{ {
public: public:
@ -142,6 +153,17 @@ JSV8Engine::JSV8Engine(): m_scope(new JSV8Scope())
eval(web3.c_str()); eval(web3.c_str());
eval("web3 = require('web3');"); eval("web3 = require('web3');");
eval(admin.c_str()); eval(admin.c_str());
auto consoleTemplate = v8::ObjectTemplate::New();
for (auto property: {"log", "debug", "error"})
consoleTemplate->Set(
v8::String::New(property),
v8::FunctionTemplate::New(ConsoleLog, v8::External::Wrap(this))
);
context()->Global()->Set(
v8::String::New("console"),
consoleTemplate->NewInstance()
);
} }
JSV8Engine::~JSV8Engine() JSV8Engine::~JSV8Engine()

4
libjsengine/PrettyPrint.js

@ -51,7 +51,7 @@ var prettyPrint = (function () {
}); });
str += indent.substr(2, indent.length) + "}"; str += indent.substr(2, indent.length) + "}";
} else if(typeof(object) === "string") { } else if(typeof(object) === "string") {
str += color_green + object + "'"; str += color_green + "'" + object + "'";
} else if(typeof(object) === "number") { } else if(typeof(object) === "number") {
str += color_red + object; str += color_red + object;
} else if(typeof(object) === "function") { } else if(typeof(object) === "function") {
@ -93,7 +93,7 @@ var prettyPrint = (function () {
var args = arguments; var args = arguments;
var ret = ""; var ret = "";
for(var i = 0, l = args.length; i < l; i++) { for(var i = 0, l = args.length; i < l; i++) {
ret += pp(args[i], "") + "\n"; ret += pp(args[i], "");
} }
return ret; return ret;
}; };

Loading…
Cancel
Save