Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/jsc/bindings/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4899,6 +4899,35 @@ void JSC__VM__releaseWeakRefs(JSC::VM* arg0)
arg0->finalizeSynchronousJSExecution();
}

// A "name" set with Object.defineProperty, which JSC's calculatedDisplayName() does not consult.
static WTF::String explicitFunctionName(JSC::VM& vm, JSC::JSFunction* function)
{
if (!function->displayName(vm).isEmpty()) {
return WTF::String();
}

JSC::JSValue name = function->getDirect(vm, vm.propertyNames->name);
if (name && JSC::isJSString(name)) {
return JSC::asString(name)->tryGetValue();
}

return WTF::String();
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

// The constructor JSObject::calculatedClassName() names an object after.
static JSC::JSFunction* constructorForClassName(JSC::VM& vm, JSC::JSObject* object)
{
JSC::JSValue constructor = object->getDirect(vm, vm.propertyNames->constructor);
if (!constructor && !object->structure()->typeInfo().overridesGetPrototype()) {
JSC::JSValue prototype = object->getPrototypeDirect();
if (prototype.isObject()) {
constructor = JSC::asObject(prototype)->getDirect(vm, vm.propertyNames->constructor);
}
}

return constructor ? dynamicDowncast<JSC::JSFunction>(constructor) : nullptr;
}

void JSC__JSValue__getClassName(JSC::EncodedJSValue JSValue0, JSC::JSGlobalObject* arg1, ZigString* arg2)
{
JSValue value = JSValue::decode(JSValue0);
Expand All @@ -4919,6 +4948,14 @@ void JSC__JSValue__getClassName(JSC::EncodedJSValue JSValue0, JSC::JSGlobalObjec

JSObject* obj = value.toObject(arg1);

if (JSC::JSFunction* constructor = constructorForClassName(arg1->vm(), obj)) {
auto explicitName = explicitFunctionName(arg1->vm(), constructor);
if (!explicitName.isEmpty()) {
*arg2 = Zig::toZigString(explicitName);
return;
}
}

auto calculated = JSObject::calculatedClassName(obj);
if (calculated.length() > 0) {
*arg2 = Zig::toZigString(calculated);
Expand Down Expand Up @@ -4961,6 +4998,11 @@ void JSC__JSValue__getNameProperty(JSC::EncodedJSValue JSValue0, JSC::JSGlobalOb
}

if (JSC::JSFunction* function = dynamicDowncast<JSC::JSFunction>(obj)) {
WTF::String explicitName = explicitFunctionName(vm, function);
if (!explicitName.isEmpty()) {
*arg2 = Zig::toZigString(explicitName);
return;
}

WTF::String actualName = function->name(vm);
if (!actualName.isEmpty() || function->isHostOrBuiltinFunction()) {
Expand Down Expand Up @@ -4992,6 +5034,14 @@ void JSC__JSValue__getNameProperty(JSC::EncodedJSValue JSValue0, JSC::JSGlobalOb
auto& vm = JSC::getVM(globalObject);
auto scope = DECLARE_THROW_SCOPE(globalObject->vm());
JSObject* object = value.getObject();
if (JSC::JSFunction* function = dynamicDowncast<JSC::JSFunction>(object)) {
auto explicitName = explicitFunctionName(vm, function);
if (!explicitName.isEmpty()) {
*arg2 = Bun::toStringRef(explicitName);
return;
}
}

auto displayName = JSC::getCalculatedDisplayName(vm, object);

// JSC doesn't include @@toStringTag in calculated display name
Expand Down
49 changes: 49 additions & 0 deletions test/js/bun/util/inspect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,55 @@ describe("console.logging class displays names and extends", async () => {
}
});

describe("a name defined with Object.defineProperty is displayed", () => {
// tsc (__setFunctionName) and esbuild/Bun (__name) name the anonymous class their decorator
// lowering produces this way; the class binding they assign to is a temporary.
function setName(value, name) {
return Object.defineProperty(value, "name", { value: name, configurable: true });
}
class Base {}
const Item = setName(class {}, "Item");
const Derived = setName(class extends Base {}, "Derived");

it("class and instances", () => {
expect([Bun.inspect(Item), Bun.inspect(new Item()), Bun.inspect({ item: new Item() }, { compact: true })]).toEqual([
"[class Item]",
"Item {}",
"{ item: Item {} }",
]);
});

it("extends clause and subclass instances", () => {
class Child extends Item {}
expect([Bun.inspect(Derived), Bun.inspect(new Derived()), Bun.inspect(Child), Bun.inspect(Item.prototype)]).toEqual(
["[class Derived extends Base]", "Derived {}", "[class Child extends Item]", "Item {}"],
);
});

it("functions", () => {
expect(Bun.inspect(setName(function original() {}, "renamed"))).toBe("[Function: renamed]");
});

it("displayName still takes precedence", () => {
const fn = setName(function original() {}, "renamed");
fn.displayName = "Display";
expect(Bun.inspect(fn)).toBe("[Function: Display]");
});

it("a name accessor is not invoked", () => {
let called = false;
const Cls = Object.defineProperty(class Original {}, "name", {
get() {
called = true;
return "from getter";
},
});
expect(Bun.inspect(Cls)).toBe("[class Original]");
expect(Bun.inspect(new Cls())).toBe("Original {}");
expect(called).toBe(false);
});
});

it("console.log on a Blob shows name", () => {
const blob = new Blob(["foo"], { type: "text/plain" });
expect(Bun.inspect(blob)).toBe('Blob (3 bytes) {\n type: "text/plain;charset=utf-8"\n}');
Expand Down