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/bindings/bindings.cpp | |
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/bindings/bindings.cpp')
-rw-r--r-- | src/bun.js/bindings/bindings.cpp | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/bun.js/bindings/bindings.cpp b/src/bun.js/bindings/bindings.cpp index e1d6ba526..5e5f43c99 100644 --- a/src/bun.js/bindings/bindings.cpp +++ b/src/bun.js/bindings/bindings.cpp @@ -3957,6 +3957,7 @@ enum class BuiltinNamesMap : uint8_t { data, toString, redirect, + inspectCustom, }; static JSC::Identifier builtinNameMap(JSC::JSGlobalObject* globalObject, unsigned char name) @@ -3988,7 +3989,46 @@ static JSC::Identifier builtinNameMap(JSC::JSGlobalObject* globalObject, unsigne case BuiltinNamesMap::redirect: { return clientData->builtinNames().redirectPublicName(); } + case BuiltinNamesMap::inspectCustom: { + return Identifier::fromUid(vm.symbolRegistry().symbolForKey("nodejs.util.inspect.custom"_s)); } + } +} + +extern "C" EncodedJSValue JSC__JSValue__callCustomInspectFunction( + JSC::JSGlobalObject* lexicalGlobalObject, + JSC__JSValue encodedFunctionValue, + JSC__JSValue encodedThisValue, + unsigned depth, + unsigned max_depth, + bool colors) +{ + auto* globalObject = jsCast<Zig::GlobalObject*>(lexicalGlobalObject); + JSValue functionToCall = JSValue::decode(encodedFunctionValue); + JSValue thisValue = JSValue::decode(encodedThisValue); + JSC::VM& vm = globalObject->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + + JSFunction* inspectFn = globalObject->utilInspectFunction(); + JSFunction* stylizeFn = colors ? globalObject->utilInspectStylizeColorFunction() : globalObject->utilInspectStylizeNoColorFunction(); + + JSObject* options = JSC::constructEmptyObject(globalObject); + options->putDirect(vm, Identifier::fromString(vm, "stylize"_s), stylizeFn); + options->putDirect(vm, Identifier::fromString(vm, "depth"_s), jsNumber(max_depth)); + options->putDirect(vm, Identifier::fromString(vm, "colors"_s), jsBoolean(colors)); + + auto callData = JSC::getCallData(functionToCall); + MarkedArgumentBuffer arguments; + arguments.append(jsNumber(depth)); + arguments.append(options); + arguments.append(inspectFn); + + auto inspectRet = JSC::call(globalObject, functionToCall, callData, thisValue, arguments); + if (auto exe = scope.exception()) { + scope.clearException(); + return JSValue::encode(exe); + } + RELEASE_AND_RETURN(scope, JSValue::encode(inspectRet)); } JSC__JSValue JSC__JSValue__fastGetDirect_(JSC__JSValue JSValue0, JSC__JSGlobalObject* globalObject, unsigned char arg2) |