feat(console): add printer for mapiterator (#6778)

* feat(console): add printer for mapiterator

MapIterator was not supported in printing, libraries like discordjs make big use of maps and
so i think supporting them would be a good idea.

* fix: snake case var

* fix: add tests for log and add setiterator printer
This commit is contained in:
Liz
2023-10-30 09:29:25 +01:00
committed by GitHub
parent 08fdbb3c7d
commit e5ccce1e89
3 changed files with 139 additions and 25 deletions

View File

@@ -1314,6 +1314,8 @@ pub const ZigConsoleClient = struct {
Error,
TypedArray,
Map,
MapIterator,
SetIterator,
Set,
BigInt,
Symbol,
@@ -1370,6 +1372,8 @@ pub const ZigConsoleClient = struct {
Error: void,
TypedArray: void,
Map: void,
MapIterator: void,
SetIterator: void,
Set: void,
BigInt: void,
Symbol: void,
@@ -1532,6 +1536,8 @@ pub const ZigConsoleClient = struct {
JSValue.JSType.BooleanObject => .Boolean,
JSValue.JSType.JSFunction => .Function,
JSValue.JSType.JSWeakMap, JSValue.JSType.JSMap => .Map,
JSValue.JSType.JSMapIterator => .MapIterator,
JSValue.JSType.JSSetIterator => .SetIterator,
JSValue.JSType.JSWeakSet, JSValue.JSType.JSSet => .Set,
JSValue.JSType.JSDate => .JSON,
JSValue.JSType.JSPromise => .Promise,
@@ -1746,37 +1752,54 @@ pub const ZigConsoleClient = struct {
this.estimated_line_length += 1;
}
pub fn MapIterator(comptime Writer: type, comptime enable_ansi_colors: bool) type {
pub fn MapIterator(comptime Writer: type, comptime enable_ansi_colors: bool, comptime is_iterator: bool) type {
return struct {
formatter: *ZigConsoleClient.Formatter,
writer: Writer,
count: usize = 0,
pub fn forEach(_: [*c]JSC.VM, globalObject: [*c]JSGlobalObject, ctx: ?*anyopaque, nextValue: JSValue) callconv(.C) void {
var this: *@This() = bun.cast(*@This(), ctx orelse return);
const key = JSC.JSObject.getIndex(nextValue, globalObject, 0);
const value = JSC.JSObject.getIndex(nextValue, globalObject, 1);
this.formatter.writeIndent(Writer, this.writer) catch unreachable;
const key_tag = Tag.getAdvanced(key, globalObject, .{ .hide_global = true });
if (!is_iterator) {
const key = JSC.JSObject.getIndex(nextValue, globalObject, 0);
const value = JSC.JSObject.getIndex(nextValue, globalObject, 1);
this.formatter.writeIndent(Writer, this.writer) catch unreachable;
const key_tag = Tag.getAdvanced(key, globalObject, .{ .hide_global = true });
this.formatter.format(
key_tag,
Writer,
this.writer,
key,
this.formatter.globalThis,
enable_ansi_colors,
);
this.writer.writeAll(": ") catch unreachable;
const value_tag = Tag.getAdvanced(value, globalObject, .{ .hide_global = true });
this.formatter.format(
value_tag,
Writer,
this.writer,
value,
this.formatter.globalThis,
enable_ansi_colors,
);
this.formatter.format(
key_tag,
Writer,
this.writer,
key,
this.formatter.globalThis,
enable_ansi_colors,
);
this.writer.writeAll(": ") catch unreachable;
const value_tag = Tag.getAdvanced(value, globalObject, .{ .hide_global = true });
this.formatter.format(
value_tag,
Writer,
this.writer,
value,
this.formatter.globalThis,
enable_ansi_colors,
);
} else {
this.writer.writeAll("\n") catch unreachable;
this.formatter.writeIndent(Writer, this.writer) catch unreachable;
const tag = Tag.getAdvanced(nextValue, globalObject, .{ .hide_global = true });
this.count += 1;
this.formatter.format(
tag,
Writer,
this.writer,
nextValue,
this.formatter.globalThis,
enable_ansi_colors,
);
}
this.formatter.printComma(Writer, this.writer, enable_ansi_colors) catch unreachable;
this.writer.writeAll("\n") catch unreachable;
if (!is_iterator)
this.writer.writeAll("\n") catch unreachable;
}
};
}
@@ -2473,7 +2496,7 @@ pub const ZigConsoleClient = struct {
this.depth +|= 1;
defer this.indent -|= 1;
defer this.depth -|= 1;
var iter = MapIterator(Writer, enable_ansi_colors){
var iter = MapIterator(Writer, enable_ansi_colors, false){
.formatter = this,
.writer = writer_,
};
@@ -2482,6 +2505,50 @@ pub const ZigConsoleClient = struct {
this.writeIndent(Writer, writer_) catch {};
writer.writeAll("}");
},
.MapIterator => {
const prev_quote_strings = this.quote_strings;
this.quote_strings = true;
defer this.quote_strings = prev_quote_strings;
writer.print("MapIterator {{ ", .{});
{
this.indent += 1;
this.depth +|= 1;
defer this.indent -|= 1;
defer this.depth -|= 1;
var iter = MapIterator(Writer, enable_ansi_colors, true){
.formatter = this,
.writer = writer_,
};
value.forEach(this.globalThis, &iter, @TypeOf(iter).forEach);
if (iter.count > 0)
writer.writeAll("\n");
}
this.writeIndent(Writer, writer_) catch {};
writer.writeAll("}");
},
.SetIterator => {
const prev_quote_strings = this.quote_strings;
this.quote_strings = true;
defer this.quote_strings = prev_quote_strings;
writer.print("SetIterator {{ ", .{});
{
this.indent += 1;
this.depth +|= 1;
defer this.indent -|= 1;
defer this.depth -|= 1;
var iter = MapIterator(Writer, enable_ansi_colors, true){
.formatter = this,
.writer = writer_,
};
value.forEach(this.globalThis, &iter, @TypeOf(iter).forEach);
if (iter.count > 0)
writer.writeAll("\n");
}
this.writeIndent(Writer, writer_) catch {};
writer.writeAll("}");
},
.Set => {
const length_value = value.get(this.globalThis, "size") orelse JSC.JSValue.jsNumberFromInt32(0);
const length = length_value.toInt32();
@@ -3072,6 +3139,8 @@ pub const ZigConsoleClient = struct {
.Error => this.printAs(.Error, Writer, writer, value, result.cell, enable_ansi_colors),
.ArrayBuffer, .TypedArray => this.printAs(.TypedArray, Writer, writer, value, result.cell, enable_ansi_colors),
.Map => this.printAs(.Map, Writer, writer, value, result.cell, enable_ansi_colors),
.MapIterator => this.printAs(.MapIterator, Writer, writer, value, result.cell, enable_ansi_colors),
.SetIterator => this.printAs(.SetIterator, Writer, writer, value, result.cell, enable_ansi_colors),
.Set => this.printAs(.Set, Writer, writer, value, result.cell, enable_ansi_colors),
.Symbol => this.printAs(.Symbol, Writer, writer, value, result.cell, enable_ansi_colors),
.BigInt => this.printAs(.BigInt, Writer, writer, value, result.cell, enable_ansi_colors),

View File

@@ -75,3 +75,37 @@ String 123 should be 2nd word, 456 == 456 and percent s %s == What okay
}
}
}
SetIterator {
1,
"123",
{
a: [],
str: "123123132",
nr: 3453
},
}
SetIterator {
1,
"123",
{
a: [],
str: "123123132",
nr: 3453
},
}
SetIterator { } SetIterator { }
MapIterator {
"key",
"key_2",
}
MapIterator {
{
a: [],
str: "123123132",
nr: 3453
},
{
b: "test"
},
}
MapIterator { } MapIterator { }

View File

@@ -76,3 +76,14 @@ console.dir({ 1: { 2: { 3: 3 } } }, { depth: 0, colors: false }, "Some ignored a
console.dir({ 1: { 2: { 3: 3 } } }, { depth: -1, colors: false }, "Some ignored arg");
console.dir({ 1: { 2: { 3: 3 } } }, { depth: 1.2, colors: false }, "Some ignored arg");
console.dir({ 1: { 2: { 3: 3 } } }, { depth: Infinity, colors: false }, "Some ignored arg");
const set = new Set([1, "123", { a: [], str: "123123132", nr: 3453 }]);
console.log(set.keys());
console.log(set.values());
console.log(new Set().keys(), new Set().values());
const m = new Map([
["key", { a: [], str: "123123132", nr: 3453 }],
["key_2", { b: "test" }],
]);
console.log(m.keys());
console.log(m.values());
console.log(new Map().keys(), new Map().values());