diff options
author | 2023-08-17 20:56:52 -0700 | |
---|---|---|
committer | 2023-08-17 20:56:52 -0700 | |
commit | 6fd0043f6bf766cc488a88339059e8879fa07161 (patch) | |
tree | d5289bcaf0880a3bf1e2f0b1c681aff93188fe51 /src/bun.js/api | |
parent | 0424fd8f6e7549ed779788006acdc97a8467e287 (diff) | |
download | bun-6fd0043f6bf766cc488a88339059e8879fa07161.tar.gz bun-6fd0043f6bf766cc488a88339059e8879fa07161.tar.zst bun-6fd0043f6bf766cc488a88339059e8879fa07161.zip |
Add `util.inspect.custom` support to `util.inspect/Bun.inspect/console.log` (#4194)
* start work on util.inspect.custom
* asdf
* finish util inspect custom inspect
* inspect
* fix tests
* revert
* tidy
* revert
* oops
* test
* fix issues
Diffstat (limited to 'src/bun.js/api')
-rw-r--r-- | src/bun.js/api/bun.zig | 76 |
1 files changed, 68 insertions, 8 deletions
diff --git a/src/bun.js/api/bun.zig b/src/bun.js/api/bun.zig index d230028ba..7685b8f80 100644 --- a/src/bun.js/api/bun.zig +++ b/src/bun.js/api/bun.zig @@ -227,6 +227,71 @@ pub fn inspect( } } + var formatOptions = ZigConsoleClient.FormatOptions{ + .enable_colors = false, + .add_newline = false, + .flush = false, + .max_depth = 8, + .quote_strings = true, + .ordered_properties = false, + }; + var value = JSC.JSValue.fromRef(arguments[0]); + + if (arguments.len > 1) { + var arg1: JSC.JSValue = JSC.JSValue.fromRef(arguments[1]); + + if (arg1.isObject()) { + if (arg1.getTruthy(ctx, "depth")) |opt| { + if (opt.isInt32()) { + const arg = opt.toInt32(); + if (arg < 0) { + ctx.throwInvalidArguments("expected depth to be greater than or equal to 0, got {d}", .{arg}); + return null; + } + formatOptions.max_depth = @as(u16, @truncate(@as(u32, @intCast(@min(arg, std.math.maxInt(u16)))))); + } else if (opt.isNumber()) { + const v = opt.asDouble(); + if (std.math.isInf(v)) { + formatOptions.max_depth = std.math.maxInt(u16); + } else { + ctx.throwInvalidArguments("expected depth to be an integer, got {d}", .{v}); + return null; + } + } + } + if (arg1.getOptional(ctx, "colors", bool) catch return null) |opt| { + formatOptions.enable_colors = opt; + } + if (arg1.getOptional(ctx, "sorted", bool) catch return null) |opt| { + formatOptions.ordered_properties = opt; + } + } else { + // formatOptions.show_hidden = arg1.toBoolean(); + if (arguments.len > 2) { + var depthArg = JSC.JSValue.fromRef(arguments[1]); + if (depthArg.isInt32()) { + const arg = depthArg.toInt32(); + if (arg < 0) { + ctx.throwInvalidArguments("expected depth to be greater than or equal to 0, got {d}", .{arg}); + return null; + } + formatOptions.max_depth = @as(u16, @truncate(@as(u32, @intCast(@min(arg, std.math.maxInt(u16)))))); + } else if (depthArg.isNumber()) { + const v = depthArg.asDouble(); + if (std.math.isInf(v)) { + formatOptions.max_depth = std.math.maxInt(u16); + } else { + ctx.throwInvalidArguments("expected depth to be an integer, got {d}", .{v}); + return null; + } + } + if (arguments.len > 3) { + formatOptions.enable_colors = JSC.JSValue.fromRef(arguments[2]).toBoolean(); + } + } + } + } + // very stable memory address var array = MutableString.init(getAllocator(ctx), 0) catch unreachable; var buffered_writer_ = MutableString.BufferedWriter{ .context = &array }; @@ -239,17 +304,12 @@ pub fn inspect( ZigConsoleClient.format( .Debug, ctx.ptr(), - @as([*]const JSValue, @ptrCast(arguments.ptr)), - arguments.len, + @as([*]const JSValue, @ptrCast(&value)), + 1, Writer, Writer, writer, - .{ - .enable_colors = false, - .add_newline = false, - .flush = false, - .max_depth = 32, - }, + formatOptions, ); buffered_writer.flush() catch { return JSC.C.JSValueMakeUndefined(ctx); |