aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Liz <accs@liz3.net> 2023-10-30 09:29:25 +0100
committerGravatar GitHub <noreply@github.com> 2023-10-30 01:29:25 -0700
commite5ccce1e89fb2c830dbdc984e7904bcebef78d3c (patch)
tree62d0d7a5a868f5f27dc0670a2bd5380f5373e778
parent08fdbb3c7d53dde7eb53feb55fddfbdd63feb4e5 (diff)
downloadbun-e5ccce1e89fb2c830dbdc984e7904bcebef78d3c.tar.gz
bun-e5ccce1e89fb2c830dbdc984e7904bcebef78d3c.tar.zst
bun-e5ccce1e89fb2c830dbdc984e7904bcebef78d3c.zip
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
-rw-r--r--src/bun.js/bindings/exports.zig121
-rw-r--r--test/js/web/console/console-log.expected.txt34
-rw-r--r--test/js/web/console/console-log.js11
3 files changed, 140 insertions, 26 deletions
diff --git a/src/bun.js/bindings/exports.zig b/src/bun.js/bindings/exports.zig
index 3f8578533..3bbe7976d 100644
--- a/src/bun.js/bindings/exports.zig
+++ b/src/bun.js/bindings/exports.zig
@@ -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 });
-
- 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,
- );
+ 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,
+ );
+ } 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),
diff --git a/test/js/web/console/console-log.expected.txt b/test/js/web/console/console-log.expected.txt
index 39db03722..48ad0d923 100644
--- a/test/js/web/console/console-log.expected.txt
+++ b/test/js/web/console/console-log.expected.txt
@@ -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 { }
diff --git a/test/js/web/console/console-log.js b/test/js/web/console/console-log.js
index 95f419781..d25d5ccf9 100644
--- a/test/js/web/console/console-log.js
+++ b/test/js/web/console/console-log.js
@@ -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());