From 7655bcab4b8c49f707e96cdd3eb95e31cbc4b6c5 Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 30 Jul 2015 15:42:18 +0200 Subject: [PATCH] Fix pretty printer and implement console.log/debug/error. --- libjsengine/Common.js | 9 +-------- libjsengine/JSV8Engine.cpp | 22 ++++++++++++++++++++++ libjsengine/PrettyPrint.js | 4 ++-- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/libjsengine/Common.js b/libjsengine/Common.js index 3911409a7..7270d8676 100644 --- a/libjsengine/Common.js +++ b/libjsengine/Common.js @@ -1,11 +1,4 @@ -console = {}; -console.log = function () { -}; -console.warn = function () { -}; -console.error = function () { -}; - setTimeout = function () { + console.error("setTimeout not available in this environment."); }; diff --git a/libjsengine/JSV8Engine.cpp b/libjsengine/JSV8Engine.cpp index a97120a67..77976a724 100644 --- a/libjsengine/JSV8Engine.cpp +++ b/libjsengine/JSV8Engine.cpp @@ -22,6 +22,7 @@ #include #include "JSV8Engine.h" +#include "JSV8Printer.h" #include "libjsengine/JSEngineResources.hpp" #include "BuildInfo.h" @@ -91,6 +92,16 @@ void reportException(v8::TryCatch* _tryCatch) } } +v8::Handle ConsoleLog(v8::Arguments const& _args) +{ + auto engine = reinterpret_cast(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 { public: @@ -142,6 +153,17 @@ JSV8Engine::JSV8Engine(): m_scope(new JSV8Scope()) eval(web3.c_str()); eval("web3 = require('web3');"); 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() diff --git a/libjsengine/PrettyPrint.js b/libjsengine/PrettyPrint.js index 080c1a485..ad25764e6 100644 --- a/libjsengine/PrettyPrint.js +++ b/libjsengine/PrettyPrint.js @@ -51,7 +51,7 @@ var prettyPrint = (function () { }); str += indent.substr(2, indent.length) + "}"; } else if(typeof(object) === "string") { - str += color_green + object + "'"; + str += color_green + "'" + object + "'"; } else if(typeof(object) === "number") { str += color_red + object; } else if(typeof(object) === "function") { @@ -93,7 +93,7 @@ var prettyPrint = (function () { var args = arguments; var ret = ""; for(var i = 0, l = args.length; i < l; i++) { - ret += pp(args[i], "") + "\n"; + ret += pp(args[i], ""); } return ret; };