Skip to content

Commit ecfbdca

Browse files
Driegermmarchini
authored andcommitted
src: move JSDate "ToString" logic to JSDate class
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 <joyeec9h3@gmail.com> Reviewed-By: Matheus Marchini <mat@mmarchini.me>
1 parent c29d5af commit ecfbdca

File tree

5 files changed

+45
-28
lines changed

5 files changed

+45
-28
lines changed

src/llv8.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,31 @@ std::string HeapNumber::ToString(bool whole, Error& err) {
679679
return buf;
680680
}
681681

682+
std::string JSDate::ToString(Error& err) {
683+
v8::Value val(GetValue(err));
684+
685+
// Check if it is SMI
686+
// e.g: date = new Date(1)
687+
v8::Smi smi(val);
688+
if (smi.Check()) {
689+
std::string s = smi.ToString(err);
690+
if (err.Fail()) return "";
691+
return s;
692+
}
693+
694+
// Check if it is HeapNumber
695+
// e.g: date = new Date()
696+
v8::HeapNumber hn(val);
697+
if (hn.Check()) {
698+
std::string s = hn.ToString(true, err);
699+
if (err.Fail()) return "";
700+
return s;
701+
}
702+
703+
Error::PrintInDebugMode("JSDate is not a Smi neither a HeapNumber");
704+
return "";
705+
}
706+
682707

683708
std::string Symbol::ToString(Error& err) {
684709
if (!String::IsString(v8(), Name(err), err)) {

src/llv8.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ class JSDate : public JSObject {
373373
V8_VALUE_DEFAULT_METHODS(JSDate, JSObject);
374374

375375
inline Value GetValue(Error& err);
376+
std::string ToString(Error& err);
376377
};
377378

378379
class FixedArrayBase : public HeapObject {

src/printer.cc

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <cinttypes>
22
#include <sstream>
3+
#include <iostream>
34

45
#include "deps/rang/include/rang.hpp"
56
#include "src/llv8-inl.h"
@@ -136,34 +137,10 @@ std::string Printer::Stringify(v8::JSFunction js_function, Error& err) {
136137

137138
template <>
138139
std::string Printer::Stringify(v8::JSDate js_date, Error& err) {
139-
std::string pre = "<JSDate: ";
140-
141-
v8::Value val = js_date.GetValue(err);
142-
143-
v8::Smi smi(val);
144-
if (smi.Check()) {
145-
std::string s = smi.ToString(err);
146-
if (err.Fail()) {
147-
return pre + ">";
148-
}
149-
150-
return pre + s + ">";
151-
}
152-
153-
v8::HeapNumber hn(val);
154-
if (hn.Check()) {
155-
std::string s = hn.ToString(true, err);
156-
if (err.Fail()) {
157-
return pre + ">";
158-
}
159-
return pre + s + ">";
160-
}
161-
162-
double d = static_cast<double>(val.raw());
163-
char buf[128];
164-
snprintf(buf, sizeof(buf), "%f", d);
165-
166-
return pre + ">";
140+
std::stringstream ss;
141+
ss << rang::fg::yellow << "<JSDate: " + js_date.ToString(err) + ">"
142+
<< rang::fg::reset;
143+
return ss.str();
167144
}
168145

169146
template <>

test/fixtures/inspect-scenario.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ function closure() {
7474
let scopedAPI = zlib.createDeflate()._handle;
7575
let scopedArray = [ 0, scopedAPI ];
7676

77+
c.hashmap['date_1'] = new Date('2000-01-01');
78+
c.hashmap['date_2'] = new Date(1);
79+
7780
exports.holder = scopedAPI;
7881

7982
c.hashmap.scoped = function name() {

test/plugin/inspect-test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,18 @@ const hashMapTests = {
345345
cb(null);
346346
});
347347
}
348+
},
349+
// .date_1=<JSDate: >
350+
'date_1': {
351+
re: /\.date_1=0x[0-9a-f]+:<JSDate: 946684800000\.000000>/,
352+
desc: ".date_2 JSDate element"
353+
},
354+
// .date_2=<JSDate: 1>
355+
'date_2' : {
356+
re: /\.date_2=0x[0-9a-f]+:<JSDate: 1>/,
357+
desc: ".date_2 JSDate element",
348358
}
359+
349360
};
350361

351362
const contextTests = {

0 commit comments

Comments
 (0)