diff --git a/src/llv8.cc b/src/llv8.cc index 0dbe6ac4..e07d2811 100644 --- a/src/llv8.cc +++ b/src/llv8.cc @@ -679,6 +679,31 @@ std::string HeapNumber::ToString(bool whole, Error& err) { return buf; } +std::string JSDate::ToString(Error& err) { + v8::Value val(GetValue(err)); + + // Check if it is SMI + // e.g: date = new Date(1) + v8::Smi smi(val); + if (smi.Check()) { + std::string s = smi.ToString(err); + if (err.Fail()) return ""; + return s; + } + + // Check if it is HeapNumber + // e.g: date = new Date() + v8::HeapNumber hn(val); + if (hn.Check()) { + std::string s = hn.ToString(true, err); + if (err.Fail()) return ""; + return s; + } + + Error::PrintInDebugMode("JSDate is not a Smi neither a HeapNumber"); + return ""; +} + std::string Symbol::ToString(Error& err) { if (!String::IsString(v8(), Name(err), err)) { diff --git a/src/llv8.h b/src/llv8.h index c0c80896..d54fce72 100644 --- a/src/llv8.h +++ b/src/llv8.h @@ -373,6 +373,7 @@ class JSDate : public JSObject { V8_VALUE_DEFAULT_METHODS(JSDate, JSObject); inline Value GetValue(Error& err); + std::string ToString(Error& err); }; class FixedArrayBase : public HeapObject { diff --git a/src/printer.cc b/src/printer.cc index 56fd1a53..c6b0b6c9 100644 --- a/src/printer.cc +++ b/src/printer.cc @@ -1,5 +1,6 @@ #include #include +#include #include "deps/rang/include/rang.hpp" #include "src/llv8-inl.h" @@ -136,34 +137,10 @@ std::string Printer::Stringify(v8::JSFunction js_function, Error& err) { template <> std::string Printer::Stringify(v8::JSDate js_date, Error& err) { - std::string pre = ""; - } - - return pre + s + ">"; - } - - v8::HeapNumber hn(val); - if (hn.Check()) { - std::string s = hn.ToString(true, err); - if (err.Fail()) { - return pre + ">"; - } - return pre + s + ">"; - } - - double d = static_cast(val.raw()); - char buf[128]; - snprintf(buf, sizeof(buf), "%f", d); - - return pre + ">"; + std::stringstream ss; + ss << rang::fg::yellow << "" + << rang::fg::reset; + return ss.str(); } template <> diff --git a/test/fixtures/inspect-scenario.js b/test/fixtures/inspect-scenario.js index 0e926aad..fb6883e3 100644 --- a/test/fixtures/inspect-scenario.js +++ b/test/fixtures/inspect-scenario.js @@ -74,6 +74,9 @@ function closure() { let scopedAPI = zlib.createDeflate()._handle; let scopedArray = [ 0, scopedAPI ]; + c.hashmap['date_1'] = new Date('2000-01-01'); + c.hashmap['date_2'] = new Date(1); + exports.holder = scopedAPI; c.hashmap.scoped = function name() { diff --git a/test/plugin/inspect-test.js b/test/plugin/inspect-test.js index be81a780..1d06b45c 100644 --- a/test/plugin/inspect-test.js +++ b/test/plugin/inspect-test.js @@ -345,7 +345,18 @@ const hashMapTests = { cb(null); }); } + }, + // .date_1= + 'date_1': { + re: /\.date_1=0x[0-9a-f]+:/, + desc: ".date_2 JSDate element" + }, + // .date_2= + 'date_2' : { + re: /\.date_2=0x[0-9a-f]+:/, + desc: ".date_2 JSDate element", } + }; const contextTests = {