aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar dave caruso <me@paperdave.net> 2023-08-21 16:25:37 -0700
committerGravatar GitHub <noreply@github.com> 2023-08-21 16:25:37 -0700
commit397182b400067be7b5293899fb3855524d07d8bf (patch)
tree23cf7beb7dcb9757d16ff6a267d0e1592c0649fb
parent752e59f23c78937558618f30c27abf3eafd8d5d5 (diff)
downloadbun-397182b400067be7b5293899fb3855524d07d8bf.tar.gz
bun-397182b400067be7b5293899fb3855524d07d8bf.tar.zst
bun-397182b400067be7b5293899fb3855524d07d8bf.zip
feat: Implement Bun.inspect.custom (#4243)
* add Bun.inspect.custom * test * Add Types
-rw-r--r--packages/bun-types/bun.d.ts6
-rw-r--r--src/bun.js/api/bun.zig13
-rw-r--r--src/bun.js/bindings/BunObject+exports.h6
-rw-r--r--src/bun.js/bindings/BunObject.cpp2
-rw-r--r--src/bun.js/bindings/BunObject.lut.h2
-rw-r--r--test/js/bun/util/inspect.test.js5
6 files changed, 26 insertions, 8 deletions
diff --git a/packages/bun-types/bun.d.ts b/packages/bun-types/bun.d.ts
index a89e943b3..e5eaf1674 100644
--- a/packages/bun-types/bun.d.ts
+++ b/packages/bun-types/bun.d.ts
@@ -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 {
/**
diff --git a/src/bun.js/api/bun.zig b/src/bun.js/api/bun.zig
index 8d1244195..a9d7ed970 100644
--- a/src/bun.js/api/bun.zig
+++ b/src/bun.js/api/bun.zig
@@ -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,
diff --git a/src/bun.js/bindings/BunObject+exports.h b/src/bun.js/bindings/BunObject+exports.h
index c081a7c1c..b805b90d8 100644
--- a/src/bun.js/bindings/BunObject+exports.h
+++ b/src/bun.js/bindings/BunObject+exports.h
@@ -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
diff --git a/src/bun.js/bindings/BunObject.cpp b/src/bun.js/bindings/BunObject.cpp
index 5bbc5a191..aaf16beb8 100644
--- a/src/bun.js/bindings/BunObject.cpp
+++ b/src/bun.js/bindings/BunObject.cpp
@@ -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
diff --git a/src/bun.js/bindings/BunObject.lut.h b/src/bun.js/bindings/BunObject.lut.h
index 0596146e6..1971cb8da 100644
--- a/src/bun.js/bindings/BunObject.lut.h
+++ b/src/bun.js/bindings/BunObject.lut.h
@@ -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 } },
diff --git a/test/js/bun/util/inspect.test.js b/test/js/bun/util/inspect.test.js
index 6ae9e9816..32b3f2bfa 100644
--- a/test/js/bun/util/inspect.test.js
+++ b/test/js/bun/util/inspect.test.js
@@ -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);
+});