Skip to content

Commit

Permalink
util, feat: support symbol key in util.format.
Browse files Browse the repository at this point in the history
  • Loading branch information
xicilion committed Jan 13, 2024
1 parent 26eebc3 commit a760a35
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 53 deletions.
6 changes: 3 additions & 3 deletions fibjs/src/console/console.cpp
Expand Up @@ -135,7 +135,7 @@ void _log(int32_t type, exlib::string fmt, OptArgs args)
if (type <= level) {
exlib::string str;

util_format(fmt, args, colors(type), str);
util_format(Isolate::current(), fmt, args, colors(type), str);
asyncLog(type, str);
}
}
Expand Down Expand Up @@ -246,7 +246,7 @@ result_t console_base::trace(exlib::string fmt, OptArgs args)
if (type <= level) {
exlib::string str;

util_format(fmt, args, colors(type), str);
util_format(Isolate::current(), fmt, args, colors(type), str);
if (str.empty())
str = "Trace";
else
Expand Down Expand Up @@ -281,7 +281,7 @@ result_t console_base::table(v8::Local<v8::Value> obj)

result_t console_base::table(v8::Local<v8::Value> obj, v8::Local<v8::Array> fields)
{
exlib::string strBuffer = table_format(obj, fields, colors(C_INFO), true);
exlib::string strBuffer = table_format(Isolate::current(), obj, fields, colors(C_INFO), true);
asyncLog(C_INFO, strBuffer);
return 0;
}
Expand Down
9 changes: 5 additions & 4 deletions fibjs/src/test/assert.cpp
Expand Up @@ -100,25 +100,26 @@ class _msg {

exlib::string str()
{
Isolate* isolate = Isolate::current();
exlib::string str(msg);

if (str.empty()) {
str = strs[0];

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

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

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

if (strs[4]) {
str.append(json_format(*vs[3]));
str.append(json_format(isolate, *vs[3]));
str.append(strs[4]);
}
}
Expand Down
8 changes: 4 additions & 4 deletions fibjs/src/util/util.h
Expand Up @@ -26,12 +26,12 @@ namespace fibjs {
#define DEFAULT_MAX_ARRAY_LENGTH 100
#define DEFAULT_MAX_STRING_LENGTH 10000

void string_format(StringBuffer& strBuffer, v8::Local<v8::Value> v, bool color = false,
void string_format(Isolate* isolate, StringBuffer& strBuffer, v8::Local<v8::Value> v, bool color = false,
int32_t maxStringLength = DEFAULT_MAX_STRING_LENGTH);
exlib::string json_format(v8::Local<v8::Value> obj, bool color = false, int32_t depth = DEFAULT_DEPTH,
exlib::string json_format(Isolate* isolate, v8::Local<v8::Value> obj, bool color = false, int32_t depth = DEFAULT_DEPTH,
int32_t maxArrayLength = DEFAULT_MAX_ARRAY_LENGTH, int32_t maxStringLength = DEFAULT_MAX_STRING_LENGTH);

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);
exlib::string table_format(Isolate* isolate, v8::Local<v8::Value> obj, v8::Local<v8::Array> fields, bool color, bool encode_string);
result_t util_format(Isolate* isolate, exlib::string fmt, OptArgs args, bool color, exlib::string& retVal);

}
59 changes: 35 additions & 24 deletions fibjs/src/util/util_format.cpp
Expand Up @@ -44,11 +44,10 @@ class _item {
int32_t mode;
};

void string_format(StringBuffer& strBuffer, v8::Local<v8::Value> v, bool color, int32_t maxStringLength)
void string_format(Isolate* isolate, StringBuffer& strBuffer, v8::Local<v8::Value> v, bool color, int32_t maxStringLength)
{
exlib::string ret;

Isolate* isolate = Isolate::current();
exlib::string s = isolate->toString(v);
exlib::wstring32 str32 = utf8to32String(s);

Expand Down Expand Up @@ -76,13 +75,33 @@ void string_format(StringBuffer& strBuffer, v8::Local<v8::Value> v, bool color,
strBuffer.append(color_string(COLOR_GREEN, ret, color));
}

void symbol_format(Isolate* isolate, StringBuffer& strBuffer, v8::Local<v8::Value> v, bool color)
{
exlib::string s("Symbol(");

v8::Local<v8::Symbol> _symbol = v8::Local<v8::Symbol>::Cast(v);
v8::Local<v8::Value> _name = _symbol->Description(isolate->m_isolate);

if (!_name->IsUndefined())
s.append(isolate->toString(_name));

strBuffer.append(color_string(COLOR_GREEN, s + ')', color));
}

void key_format(Isolate* isolate, StringBuffer& strBuffer, v8::Local<v8::Value> v, bool color)
{
if (v->IsSymbol())
symbol_format(isolate, strBuffer, v, color);
else
string_format(isolate, strBuffer, v);
}

#define MAX_BUFFER_ITEM 50

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

Isolate* isolate = Isolate::current();
v8::Local<v8::Context> _context = isolate->context();

QuickArray<_item> stk;
Expand All @@ -106,7 +125,7 @@ exlib::string json_format(v8::Local<v8::Value> obj, bool color, int32_t depth, i
else if (v->IsBigInt() || v->IsBigIntObject())
strBuffer.append(color_string(COLOR_YELLOW, isolate->toString(v) + 'n', color));
else if (v->IsString() || v->IsStringObject())
string_format(strBuffer, v, color, maxStringLength);
string_format(isolate, strBuffer, v, color, maxStringLength);
else if (v->IsRegExp()) {
exlib::string s;
v8::Local<v8::RegExp> re = v8::Local<v8::RegExp>::Cast(v);
Expand All @@ -132,15 +151,7 @@ exlib::string json_format(v8::Local<v8::Value> obj, bool color, int32_t depth, i
exlib::string s(isolate->toString(JSValue(obj->Get(_context, isolate->NewString("stack")))));
strBuffer.append(color_string(COLOR_LIGHTRED, s, color));
} else if (v->IsSymbol()) {
exlib::string s("Symbol(");

v8::Local<v8::Symbol> _symbol = v8::Local<v8::Symbol>::Cast(v);
v8::Local<v8::Value> _name = _symbol->Description(isolate->m_isolate);

if (!_name->IsUndefined())
s.append(isolate->toString(_name));

strBuffer.append(color_string(COLOR_GREEN, s + ')', color));
symbol_format(isolate, strBuffer, v, color);
} else if (v->IsObject()) {
bool isFunction = false;

Expand Down Expand Up @@ -218,7 +229,9 @@ exlib::string json_format(v8::Local<v8::Value> obj, bool color, int32_t depth, i
} else
keys = vs;
} else
keys = obj->GetPropertyNames(_context);
keys = obj->GetPropertyNames(_context, v8::KeyCollectionMode::kOwnOnly,
v8::PropertyFilter::ONLY_ENUMERABLE, v8::IndexFilter::kSkipIndices)
.FromMaybe(v8::Local<v8::Array>());

if (keys.IsEmpty()) {
if (!isFunction)
Expand Down Expand Up @@ -395,7 +408,7 @@ exlib::string json_format(v8::Local<v8::Value> obj, bool color, int32_t depth, i

if (!it->obj.IsEmpty()) {
TryCatch try_catch;
string_format(strBuffer, v);
key_format(isolate, strBuffer, v, color);

if (it->obj->IsMap()) {
strBuffer.append(" => ");
Expand All @@ -413,7 +426,7 @@ exlib::string json_format(v8::Local<v8::Value> obj, bool color, int32_t depth, i
return strBuffer.str();
}

result_t util_format(exlib::string fmt, OptArgs args, bool color, exlib::string& retVal)
result_t util_format(Isolate* isolate, exlib::string fmt, OptArgs args, bool color, exlib::string& retVal)
{
const char* s1;
char ch;
Expand All @@ -425,8 +438,6 @@ result_t util_format(exlib::string fmt, OptArgs args, bool color, exlib::string&
return 0;
}

Isolate* isolate = Isolate::current();

const char* s = fmt.c_str();
const char* s_end = s + fmt.length();

Expand Down Expand Up @@ -456,7 +467,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(isolate, v, color);
retVal.append(s);
}
} else
Expand All @@ -465,7 +476,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(isolate, args[idx++], color);
retVal.append(s);
} else
retVal.append("%j", 2);
Expand Down Expand Up @@ -493,7 +504,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(isolate, v, color);

retVal.append(s);
}
Expand All @@ -504,11 +515,11 @@ result_t util_format(exlib::string fmt, OptArgs args, bool color, exlib::string&

result_t util_base::format(exlib::string fmt, OptArgs args, exlib::string& retVal)
{
return util_format(fmt, args, false, retVal);
return util_format(Isolate::current(), fmt, args, false, retVal);
}

result_t util_base::format(OptArgs args, exlib::string& retVal)
{
return util_format("", args, false, retVal);
return util_format(Isolate::current(), "", args, false, retVal);
}
}
33 changes: 15 additions & 18 deletions fibjs/src/util/util_format_table.cpp
Expand Up @@ -183,14 +183,12 @@ inline void append_value(exlib::string& str, int32_t width, int32_t max_width, S
buf.append(repeat((max_width - width) - (max_width - width) / 2));
}

exlib::string object_format(v8::Local<v8::Value> v, bool color, bool l2 = false)
exlib::string object_format(Isolate* isolate, v8::Local<v8::Value> v, bool color, bool l2 = false)
{
if (IsJSBuffer(v))
return json_format(v, color);
return json_format(isolate, v, color);

Isolate* isolate = Isolate::current();
v8::Local<v8::Context> _context = isolate->context();

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

JSArray keys = obj->GetPropertyNames(_context);
Expand Down Expand Up @@ -227,9 +225,9 @@ 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(isolate, v, color));
else
buf.append(object_format(v, color, true));
buf.append(object_format(isolate, v, color, true));
}

if (len == len1 + 1)
Expand Down Expand Up @@ -263,7 +261,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(isolate, v, color));
}

if (len == len1 + 1)
Expand Down Expand Up @@ -293,29 +291,28 @@ exlib::string object_format(v8::Local<v8::Value> v, bool color, bool l2 = false)
buf.append(", ");

JSValue v = keys->Get(_context, i);
string_format(buf, v);
string_format(isolate, buf, v);

buf.append(": ");

v = obj->Get(_context, v);
if (isSimpleValue(v))
buf.append(json_format(v, color));
buf.append(json_format(isolate, v, color));
else
buf.append(object_format(v, color, true));
buf.append(object_format(isolate, v, color, true));
}
buf.append(" }");

return buf.str();
}

exlib::string table_format(v8::Local<v8::Value> obj, v8::Local<v8::Array> fields, bool color, bool encode_string)
exlib::string table_format(Isolate* isolate, 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(isolate, obj, color);

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

Isolate* isolate = Isolate::current(o);
v8::Local<v8::Context> _context = isolate->context();

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

it->second.resize(i);
it->second.append(row_value);
Expand Down Expand Up @@ -494,7 +491,7 @@ result_t util_base::inspect(v8::Local<v8::Value> obj, v8::Local<v8::Object> opti
if (!options.IsEmpty())
GetConfigValue(isolate, options, "encode_string", encode_string, true);

retVal = table_format(obj, fields, colors, encode_string);
retVal = table_format(isolate, obj, fields, colors, encode_string);
} else {
bool colors = true;
if (!options.IsEmpty())
Expand All @@ -512,7 +509,7 @@ result_t util_base::inspect(v8::Local<v8::Value> obj, v8::Local<v8::Object> opti
if (!options.IsEmpty())
GetConfigValue(isolate, options, "maxStringLength", maxStringLength, true);

retVal = json_format(obj, colors, depth, maxArrayLength, maxStringLength);
retVal = json_format(isolate, obj, colors, depth, maxArrayLength, maxStringLength);
}

return 0;
Expand Down
3 changes: 3 additions & 0 deletions test/util_test.js
Expand Up @@ -849,6 +849,9 @@ describe('util', () => {
it("Symbol", () => {
assert.equal(util.format(Symbol()), 'Symbol()');
assert.equal(util.format(Symbol('debug')), 'Symbol(debug)');
const o = {};
o[Symbol('debug')] = 100;
assert.equal(util.format(o), '{\n Symbol(debug): 100\n}');
});

it("Error", () => {
Expand Down

0 comments on commit a760a35

Please sign in to comment.