Skip to content

Commit

Permalink
src: move JSDate "ToString" logic to JSDate class
Browse files Browse the repository at this point in the history
Move logic that was representing a JSDate object
to a string from the Printer::Stringfy method to
the JSDate class, implementing "ToString" method.

PR-URL: #257
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Matheus Marchini <[email protected]>
  • Loading branch information
Drieger authored and mmarchini committed Mar 5, 2019
1 parent c29d5af commit ecfbdca
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 28 deletions.
25 changes: 25 additions & 0 deletions src/llv8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
1 change: 1 addition & 0 deletions src/llv8.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
33 changes: 5 additions & 28 deletions src/printer.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <cinttypes>
#include <sstream>
#include <iostream>

#include "deps/rang/include/rang.hpp"
#include "src/llv8-inl.h"
Expand Down Expand Up @@ -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 = "<JSDate: ";

v8::Value val = js_date.GetValue(err);

v8::Smi smi(val);
if (smi.Check()) {
std::string s = smi.ToString(err);
if (err.Fail()) {
return 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<double>(val.raw());
char buf[128];
snprintf(buf, sizeof(buf), "%f", d);

return pre + ">";
std::stringstream ss;
ss << rang::fg::yellow << "<JSDate: " + js_date.ToString(err) + ">"
<< rang::fg::reset;
return ss.str();
}

template <>
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/inspect-scenario.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
11 changes: 11 additions & 0 deletions test/plugin/inspect-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,18 @@ const hashMapTests = {
cb(null);
});
}
},
// .date_1=<JSDate: >
'date_1': {
re: /\.date_1=0x[0-9a-f]+:<JSDate: 946684800000\.000000>/,
desc: ".date_2 JSDate element"
},
// .date_2=<JSDate: 1>
'date_2' : {
re: /\.date_2=0x[0-9a-f]+:<JSDate: 1>/,
desc: ".date_2 JSDate element",
}

};

const contextTests = {
Expand Down

0 comments on commit ecfbdca

Please sign in to comment.