mirror of
https://github.com/oven-sh/bun
synced 2026-02-02 15:08:46 +00:00
feat: Implement Bun.inspect.custom (#4243)
* add Bun.inspect.custom * test * Add Types
This commit is contained in:
6
packages/bun-types/bun.d.ts
vendored
6
packages/bun-types/bun.d.ts
vendored
@@ -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 {
|
||||
/**
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 } },
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user