Skip to content

Commit

Permalink
feat: Implement Bun.inspect.custom (oven-sh#4243)
Browse files Browse the repository at this point in the history
* add Bun.inspect.custom

* test

* Add Types
  • Loading branch information
paperclover authored Aug 21, 2023
1 parent 752e59f commit 397182b
Showing 6 changed files with 26 additions and 8 deletions.
6 changes: 6 additions & 0 deletions packages/bun-types/bun.d.ts
Original file line number Diff line number Diff line change
@@ -2309,6 +2309,12 @@ declare module "bun" {
* @param args
*/
export function inspect(arg: any, options: BunInspectOptions): string;
export namespace inspect {
/**
* That can be used to declare custom inspect functions.
*/
const custom: typeof import("util").inspect.custom;
}

interface MMapOptions {
/**
13 changes: 10 additions & 3 deletions src/bun.js/api/bun.zig
Original file line number Diff line number Diff line change
@@ -3,9 +3,9 @@
/// - Add a callback or property to the below struct
/// - @export it in the appropriate place
/// - Update "@begin bunObjectTable" in BunObject.cpp
/// - Getters use a generated wrapper function `BunObject_getter_wrap_<name>`
/// - Update "BunObject+exports.h"
/// - Run "make dev"
///
pub const BunObject = struct {
// --- Callbacks ---
pub const DO_NOT_USE_OR_YOU_WILL_BE_FIRED_mimalloc_dump = dump_mimalloc;
@@ -25,7 +25,6 @@ pub const BunObject = struct {
pub const gzipSync = JSC.wrapStaticMethod(JSZlib, "gzipSync", true);
pub const indexOfLine = Bun.indexOfLine;
pub const inflateSync = JSC.wrapStaticMethod(JSZlib, "inflateSync", true);
pub const inspect = Bun.inspect;
pub const jest = @import("../test/jest.zig").Jest.call;
pub const listen = JSC.wrapStaticMethod(JSC.API.Listener, "listen", false);
pub const mmap = Bun.mmapFile;
@@ -63,6 +62,7 @@ pub const BunObject = struct {
pub const cwd = Bun.getCWD;
pub const enableANSIColors = Bun.enableANSIColors;
pub const hash = Bun.getHashObject;
pub const inspect = Bun.getInspect;
pub const main = Bun.getMain;
pub const origin = Bun.getOrigin;
pub const stderr = Bun.getStderr;
@@ -107,6 +107,7 @@ pub const BunObject = struct {
@export(BunObject.cwd, .{ .name = getterName("cwd") });
@export(BunObject.enableANSIColors, .{ .name = getterName("enableANSIColors") });
@export(BunObject.hash, .{ .name = getterName("hash") });
@export(BunObject.inspect, .{ .name = getterName("inspect") });
@export(BunObject.main, .{ .name = getterName("main") });
@export(BunObject.origin, .{ .name = getterName("origin") });
@export(BunObject.stderr, .{ .name = getterName("stderr") });
@@ -132,7 +133,6 @@ pub const BunObject = struct {
@export(BunObject.gzipSync, .{ .name = callbackName("gzipSync") });
@export(BunObject.indexOfLine, .{ .name = callbackName("indexOfLine") });
@export(BunObject.inflateSync, .{ .name = callbackName("inflateSync") });
@export(BunObject.inspect, .{ .name = callbackName("inspect") });
@export(BunObject.jest, .{ .name = callbackName("jest") });
@export(BunObject.listen, .{ .name = callbackName("listen") });
@export(BunObject.mmap, .{ .name = callbackName("mmap") });
@@ -465,6 +465,13 @@ pub fn inspect(
return ret;
}

pub fn getInspect(globalObject: *JSC.JSGlobalObject, _: *JSC.JSObject) callconv(.C) JSC.JSValue {
const fun = JSC.createCallback(globalObject, ZigString.static("inspect"), 2, &inspect);
var str = ZigString.init("nodejs.util.inspect.custom");
fun.put(globalObject, ZigString.static("custom"), JSC.JSValue.symbolFor(globalObject, &str));
return fun;
}

pub fn registerMacro(
globalObject: *JSC.JSGlobalObject,
callframe: *JSC.CallFrame,
6 changes: 3 additions & 3 deletions src/bun.js/bindings/BunObject+exports.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// clang-format off

// --- Getters ---
#define FOR_EACH_GETTER(macro) \
macro(CryptoHasher) \
macro(FFI) \
@@ -19,14 +20,15 @@
macro(cwd) \
macro(enableANSIColors) \
macro(hash) \
macro(inspect) \
macro(main) \
macro(origin) \
macro(stderr) \
macro(stdin) \
macro(stdout) \
macro(unsafe) \
// --- Getters ---

// --- Callbacks ---
#define FOR_EACH_CALLBACK(macro) \
macro(DO_NOT_USE_OR_YOU_WILL_BE_FIRED_mimalloc_dump) \
macro(_Os) \
@@ -44,7 +46,6 @@
macro(gzipSync) \
macro(indexOfLine) \
macro(inflateSync) \
macro(inspect) \
macro(jest) \
macro(listen) \
macro(mmap) \
@@ -62,7 +63,6 @@
macro(which) \
macro(write) \


#define DECLARE_ZIG_BUN_OBJECT_CALLBACK(name) extern "C" JSC::EncodedJSValue BunObject_callback_##name(JSC::JSGlobalObject*, JSC::CallFrame*);
FOR_EACH_CALLBACK(DECLARE_ZIG_BUN_OBJECT_CALLBACK);
#undef DECLARE_ZIG_BUN_OBJECT_CALLBACK
2 changes: 1 addition & 1 deletion src/bun.js/bindings/BunObject.cpp
Original file line number Diff line number Diff line change
@@ -599,7 +599,7 @@ JSC_DEFINE_HOST_FUNCTION(functionHashCode,
hash BunObject_getter_wrap_hash DontDelete|PropertyCallback
indexOfLine BunObject_callback_indexOfLine DontDelete|Function 1
inflateSync BunObject_callback_inflateSync DontDelete|Function 1
inspect BunObject_callback_inspect DontDelete|Function 1
inspect BunObject_getter_wrap_inspect DontDelete|PropertyCallback
isMainThread constructIsMainThread ReadOnly|DontDelete|PropertyCallback
jest BunObject_callback_jest DontEnum|DontDelete|Function 1
listen BunObject_callback_listen DontDelete|Function 1
2 changes: 1 addition & 1 deletion src/bun.js/bindings/BunObject.lut.h
Original file line number Diff line number Diff line change
@@ -315,7 +315,7 @@ static const struct HashTableValue bunObjectTableValues[81] = {
{ "hash"_s, static_cast<unsigned>(PropertyAttribute::DontDelete|PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, BunObject_getter_wrap_hash } },
{ "indexOfLine"_s, static_cast<unsigned>(PropertyAttribute::DontDelete|PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, BunObject_callback_indexOfLine, 1 } },
{ "inflateSync"_s, static_cast<unsigned>(PropertyAttribute::DontDelete|PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, BunObject_callback_inflateSync, 1 } },
{ "inspect"_s, static_cast<unsigned>(PropertyAttribute::DontDelete|PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, BunObject_callback_inspect, 1 } },
{ "inspect"_s, static_cast<unsigned>(PropertyAttribute::DontDelete|PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, BunObject_getter_wrap_inspect } },
{ "isMainThread"_s, static_cast<unsigned>(PropertyAttribute::ReadOnly|PropertyAttribute::DontDelete|PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructIsMainThread } },
{ "jest"_s, static_cast<unsigned>(PropertyAttribute::DontEnum|PropertyAttribute::DontDelete|PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, BunObject_callback_jest, 1 } },
{ "listen"_s, static_cast<unsigned>(PropertyAttribute::DontDelete|PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, BunObject_callback_listen, 1 } },
5 changes: 5 additions & 0 deletions test/js/bun/util/inspect.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { it, expect, describe } from "bun:test";
import util from "util";

it("getters", () => {
const obj = {
@@ -357,3 +358,7 @@ it("new Date(..)", () => {
expect(Bun.inspect(new Date("hello world"))).toBe("Invalid Date");
expect(Bun.inspect(new Date("Invalid Date"))).toBe("Invalid Date");
});

it("Bun.inspect.custom exists", () => {
expect(Bun.inspect.custom).toBe(util.inspect.custom);
});

0 comments on commit 397182b

Please sign in to comment.