From d7b43f8ea17694641a355d5659c00563e60d0acf Mon Sep 17 00:00:00 2001 From: Liz Date: Wed, 27 Sep 2023 04:47:37 +0200 Subject: fix: support console.dir options object correctly (#6059) * fix: support console,dir options object correctly `console.dir` can be passed a second argument which is a object of options. This implements that logic with the currently supported properties: `depth` and `colors`. I used node as a reference for implementation details. Fixes: https://github.com/oven-sh/bun/issues/6039 * style: format zig file * fix: implement changes from review Implements changes requested from review, like adding more test cases and refactoring code style. --- src/bun.js/bindings/exports.zig | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/bun.js/bindings/exports.zig b/src/bun.js/bindings/exports.zig index 10326bc7c..784a6289c 100644 --- a/src/bun.js/bindings/exports.zig +++ b/src/bun.js/bindings/exports.zig @@ -983,22 +983,42 @@ pub const ZigConsoleClient = struct { else console.writer; var writer = buffered_writer.writer(); - const Writer = @TypeOf(writer); - if (len > 0) + + var print_length = len; + var print_options: FormatOptions = .{ + .enable_colors = enable_colors, + .add_newline = true, + .flush = true, + }; + + if (message_type == .Dir and len >= 2) { + print_length = 1; + var opts = vals[1]; + if (opts.isObject()) { + if (opts.get(global, "depth")) |depth_prop| { + if (depth_prop.isNumber()) + print_options.max_depth = depth_prop.toU16(); + if (depth_prop.isNull() or depth_prop.toInt64() == std.math.maxInt(i64)) + print_options.max_depth = std.math.maxInt(u16); + } + if (opts.get(global, "colors")) |colors_prop| { + if (colors_prop.isBoolean()) + print_options.enable_colors = colors_prop.toBoolean(); + } + } + } + + if (print_length > 0) format( level, global, vals, - len, + print_length, @TypeOf(buffered_writer.unbuffered_writer.context), Writer, writer, - .{ - .enable_colors = enable_colors, - .add_newline = true, - .flush = true, - }, + print_options, ) else if (message_type == .Log) { _ = console.writer.write("\n") catch 0; -- cgit v1.2.3