diff --git a/src/bun.js/ConsoleObject.zig b/src/bun.js/ConsoleObject.zig index 42705bbad3..166a0f027f 100644 --- a/src/bun.js/ConsoleObject.zig +++ b/src/bun.js/ConsoleObject.zig @@ -2717,7 +2717,7 @@ pub const Formatter = struct { }, .Map => { const length_value = try value.get(this.globalThis, "size") orelse jsc.JSValue.jsNumberFromInt32(0); - const length = length_value.toInt32(); + const length = try length_value.coerce(i32, this.globalThis); const prev_quote_strings = this.quote_strings; this.quote_strings = true; @@ -2824,7 +2824,7 @@ pub const Formatter = struct { }, .Set => { const length_value = try value.get(this.globalThis, "size") orelse jsc.JSValue.jsNumberFromInt32(0); - const length = length_value.toInt32(); + const length = try length_value.coerce(i32, this.globalThis); const prev_quote_strings = this.quote_strings; this.quote_strings = true; diff --git a/test/js/bun/util/inspect.test.js b/test/js/bun/util/inspect.test.js index a28e2c6313..02b3be838c 100644 --- a/test/js/bun/util/inspect.test.js +++ b/test/js/bun/util/inspect.test.js @@ -347,6 +347,23 @@ it("inspect", () => { expect(Bun.inspect(new Map())).toBe("Map {}"); expect(Bun.inspect(new Map([["foo", "bar"]]))).toBe('Map(1) {\n "foo": "bar",\n}'); expect(Bun.inspect(new Set(["bar"]))).toBe('Set(1) {\n "bar",\n}'); + + // Regression test: Set/Map with overridden size property should not panic + const setWithOverriddenSize = new Set(); + Object.defineProperty(setWithOverriddenSize, "size", { + writable: true, + enumerable: true, + value: Set, + }); + expect(Bun.inspect(setWithOverriddenSize)).toBe("Set {}"); + + const mapWithOverriddenSize = new Map(); + Object.defineProperty(mapWithOverriddenSize, "size", { + writable: true, + enumerable: true, + value: "not a number", + }); + expect(Bun.inspect(mapWithOverriddenSize)).toBe("Map {}"); expect(Bun.inspect(
foo
)).toBe("
foo
"); expect(Bun.inspect(
foo
)).toBe("
foo
"); expect(Bun.inspect(
foo
)).toBe("
foo
");