Skip to content

Commit

Permalink
util, feat: support deth in inspect.
Browse files Browse the repository at this point in the history
  • Loading branch information
xicilion committed Apr 18, 2022
1 parent 94b7863 commit 30ca6e4
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 26 deletions.
4 changes: 2 additions & 2 deletions fibjs/src/console/console.cpp
Expand Up @@ -111,7 +111,7 @@ class color_initer {
}
} s_color_initer;

exlib::string json_format(v8::Local<v8::Value> obj, bool color);
exlib::string json_format(v8::Local<v8::Value> obj, bool color, int32_t depth);
exlib::string table_format(v8::Local<v8::Value> obj, v8::Local<v8::Array> fields, bool color, bool encode_string);
result_t util_format(exlib::string fmt, OptArgs args, bool color, exlib::string& retVal);

Expand Down Expand Up @@ -255,7 +255,7 @@ result_t console_base::alert(OptArgs args)

result_t console_base::dir(v8::Local<v8::Value> obj)
{
exlib::string strBuffer = json_format(obj, colors(C_INFO));
exlib::string strBuffer = json_format(obj, colors(C_INFO), 2);
asyncLog(C_INFO, strBuffer);
return 0;
}
Expand Down
10 changes: 5 additions & 5 deletions fibjs/src/test/assert.cpp
Expand Up @@ -16,7 +16,7 @@ namespace fibjs {

DECLARE_MODULE(assert);

exlib::string json_format(v8::Local<v8::Value> obj, bool color);
exlib::string json_format(v8::Local<v8::Value> obj, bool color, int32_t depth);

class _msg {
public:
Expand Down Expand Up @@ -108,19 +108,19 @@ class _msg {
str = strs[0];

if (strs[1]) {
str.append(json_format(*vs[0], false));
str.append(json_format(*vs[0], false, 2));
str.append(strs[1]);

if (strs[2]) {
str.append(json_format(*vs[1], false));
str.append(json_format(*vs[1], false, 2));
str.append(strs[2]);

if (strs[3]) {
str.append(json_format(*vs[2], false));
str.append(json_format(*vs[2], false, 2));
str.append(strs[3]);

if (strs[4]) {
str.append(json_format(*vs[3], false));
str.append(json_format(*vs[3], false, 2));
str.append(strs[4]);
}
}
Expand Down
15 changes: 7 additions & 8 deletions fibjs/src/util/util_format.cpp
Expand Up @@ -56,11 +56,10 @@ void string_format(StringBuffer& strBuffer, v8::Local<v8::Value> v, bool color)
strBuffer.append(color_string(COLOR_GREEN, s, color));
}

#define MAX_OBJECT_LEVEL 3
#define MAX_ARRAY_ITEM 100
#define MAX_BUFFER_ITEM 50

exlib::string json_format(v8::Local<v8::Value> obj, bool color)
exlib::string json_format(v8::Local<v8::Value> obj, bool color, int32_t depth)
{
StringBuffer strBuffer;

Expand Down Expand Up @@ -218,7 +217,7 @@ exlib::string json_format(v8::Local<v8::Value> obj, bool color)
if (len == 0)
strBuffer.append("[]");
else {
if (sz >= MAX_OBJECT_LEVEL) {
if (sz >= (depth + 1)) {
strBuffer.append(color_string(COLOR_CYAN, "[Array]", color));
break;
}
Expand All @@ -244,7 +243,7 @@ exlib::string json_format(v8::Local<v8::Value> obj, bool color)
if (len == 0)
strBuffer.append("[]");
else {
if (sz >= MAX_OBJECT_LEVEL) {
if (sz >= (depth + 1)) {
strBuffer.append(color_string(COLOR_CYAN, "[TypedArray]", color));
break;
}
Expand Down Expand Up @@ -273,7 +272,7 @@ exlib::string json_format(v8::Local<v8::Value> obj, bool color)
if (len == 0)
strBuffer.append("{}");
else {
if (sz >= MAX_OBJECT_LEVEL) {
if (sz >= (depth + 1)) {
strBuffer.append(color_string(COLOR_CYAN, "[Object]", color));
break;
}
Expand Down Expand Up @@ -399,7 +398,7 @@ result_t util_format(exlib::string fmt, OptArgs args, bool color, exlib::string&
v8::Local<v8::Value> v = v8::Number::New(isolate->m_isolate, (int32_t)n);

exlib::string s;
s = json_format(v, color);
s = json_format(v, color, 2);
retVal.append(s);
}
} else
Expand All @@ -408,7 +407,7 @@ result_t util_format(exlib::string fmt, OptArgs args, bool color, exlib::string&
case 'j':
if (idx < argc) {
exlib::string s;
s = json_format(args[idx++], color);
s = json_format(args[idx++], color, 2);
retVal.append(s);
} else
retVal.append("%j", 2);
Expand Down Expand Up @@ -436,7 +435,7 @@ result_t util_format(exlib::string fmt, OptArgs args, bool color, exlib::string&
retVal.append(isolate->toString(v));
else {
exlib::string s;
s = json_format(v, color);
s = json_format(v, color, 2);

retVal.append(s);
}
Expand Down
25 changes: 15 additions & 10 deletions fibjs/src/util/util_format_table.cpp
Expand Up @@ -17,7 +17,7 @@
namespace fibjs {

void string_format(StringBuffer& strBuffer, v8::Local<v8::Value> v, bool color);
exlib::string json_format(v8::Local<v8::Value> obj, bool color);
exlib::string json_format(v8::Local<v8::Value> obj, bool color, int32_t depth);

struct CharWidth {
int32_t m_char;
Expand Down Expand Up @@ -246,7 +246,7 @@ exlib::string object_format(v8::Local<v8::Value> v, bool color, bool l2 = false)
{
obj_ptr<Buffer_base> _buf = Buffer_base::getInstance(v);
if (_buf)
return json_format(v, color);
return json_format(v, color, 2);

Isolate* isolate = Isolate::current();
v8::Local<v8::Context> _context = isolate->context();
Expand Down Expand Up @@ -287,7 +287,7 @@ exlib::string object_format(v8::Local<v8::Value> v, bool color, bool l2 = false)

JSValue v = array->Get(_context, i);
if (isSimpleValue(v))
buf.append(json_format(v, color));
buf.append(json_format(v, color, 2));
else
buf.append(object_format(v, color, true));
}
Expand Down Expand Up @@ -323,7 +323,7 @@ exlib::string object_format(v8::Local<v8::Value> v, bool color, bool l2 = false)
buf.append(", ");

JSValue v = array->Get(_context, i);
buf.append(json_format(v, color));
buf.append(json_format(v, color, 2));
}

if (len == len1 + 1)
Expand Down Expand Up @@ -359,7 +359,7 @@ exlib::string object_format(v8::Local<v8::Value> v, bool color, bool l2 = false)

v = obj->Get(_context, v);
if (isSimpleValue(v))
buf.append(json_format(v, color));
buf.append(json_format(v, color, 2));
else
buf.append(object_format(v, color, true));
}
Expand All @@ -371,7 +371,7 @@ exlib::string object_format(v8::Local<v8::Value> v, bool color, bool l2 = false)
exlib::string table_format(v8::Local<v8::Value> obj, v8::Local<v8::Array> fields, bool color, bool encode_string)
{
if (isSimpleValue(obj))
return json_format(obj, color);
return json_format(obj, color, 2);

v8::Local<v8::Object> o = v8::Local<v8::Object>::Cast(obj);

Expand Down Expand Up @@ -420,7 +420,7 @@ exlib::string table_format(v8::Local<v8::Value> obj, v8::Local<v8::Array> fields
GetArgumentValue(isolate->m_isolate, v, val);
value_cols.append(val);
} else
value_cols.append(json_format(v, color));
value_cols.append(json_format(v, color, 2));
}
} else {
v8::Local<v8::Object> ro = v8::Local<v8::Object>::Cast(v);
Expand All @@ -446,7 +446,7 @@ exlib::string table_format(v8::Local<v8::Value> obj, v8::Local<v8::Array> fields
if (!encode_string && (rv->IsString() || rv->IsStringObject()))
GetArgumentValue(isolate->m_isolate, rv, row_value);
else
row_value = json_format(rv, color);
row_value = json_format(rv, color, 2);
} else
row_value = object_format(rv, color);

Expand Down Expand Up @@ -551,8 +551,13 @@ result_t util_base::inspect(v8::Local<v8::Value> obj, v8::Local<v8::Object> opti
GetConfigValue(isolate->m_isolate, options, "encode_string", encode_string, true);

retVal = table_format(obj, fields, colors, encode_string);
} else
retVal = json_format(obj, colors);
} else {
int32_t depth = 2;
GetConfigValue(isolate->m_isolate, options, "depth", depth, true);

retVal = json_format(obj, colors, depth);
}

return 0;
}

Expand Down
8 changes: 7 additions & 1 deletion test/util_test.js
Expand Up @@ -1146,8 +1146,14 @@ describe('util', () => {
it("fix: crash on %d.", () => {
util.format("%d", 2n);
});
});

it('inspect', () => {
assert.equal(util.inspect([[[[[]]]]]), "[\n [\n [\n [Array]\n ]\n ]\n]");
assert.equal(util.inspect([[[[[]]]]], {
depth: 3
}), "[\n [\n [\n [\n []\n ]\n ]\n ]\n]");
});
});

describe("table", () => {
function test_table(data, only, expected) {
Expand Down

0 comments on commit 30ca6e4

Please sign in to comment.