diff options
author | 2023-03-15 19:18:56 -0700 | |
---|---|---|
committer | 2023-03-15 19:18:56 -0700 | |
commit | 9a5f78fa3bdc7d841067332480a37b9faa7ff70e (patch) | |
tree | ed6b22b632e66bbc346d76bb3bcf05cfccdda875 | |
parent | 47865fe82a1495da910f6f7fa5bc515d1dd3bc96 (diff) | |
download | bun-9a5f78fa3bdc7d841067332480a37b9faa7ff70e.tar.gz bun-9a5f78fa3bdc7d841067332480a37b9faa7ff70e.tar.zst bun-9a5f78fa3bdc7d841067332480a37b9faa7ff70e.zip |
Fixes #2399
-rw-r--r-- | src/bun.js/bindings/bindings.cpp | 16 | ||||
-rw-r--r-- | src/bun.js/bindings/exports.zig | 10 | ||||
-rw-r--r-- | test/js/bun/util/inspect.test.js | 24 |
3 files changed, 38 insertions, 12 deletions
diff --git a/src/bun.js/bindings/bindings.cpp b/src/bun.js/bindings/bindings.cpp index 63e17c189..9e0857578 100644 --- a/src/bun.js/bindings/bindings.cpp +++ b/src/bun.js/bindings/bindings.cpp @@ -3605,10 +3605,6 @@ restart: bool anyHits = false; JSC::JSObject* objectToUse = prototypeObject.getObject(); structure->forEachProperty(vm, [&](const PropertyTableEntry& entry) -> bool { - if ((entry.attributes() & PropertyAttribute::Accessor) != 0) { - return true; - } - if ((entry.attributes() & (PropertyAttribute::Function)) == 0 && (entry.attributes() & (PropertyAttribute::Builtin)) != 0) { return true; } @@ -3634,7 +3630,7 @@ restart: return true; JSC::JSValue propertyValue = objectToUse == object ? objectToUse->getDirect(entry.offset()) : JSValue(); - if (!propertyValue || propertyValue.isGetterSetter()) { + if (!propertyValue || propertyValue.isGetterSetter() && !((entry.attributes() & PropertyAttribute::Accessor) != 0)) { propertyValue = objectToUse->get(globalObject, prop); } @@ -3693,10 +3689,6 @@ restart: if (!object->getPropertySlot(globalObject, property, slot)) continue; - if ((slot.attributes() & PropertyAttribute::Accessor) != 0) { - continue; - } - if ((slot.attributes() & PropertyAttribute::DontEnum) != 0) { if (property == vm.propertyNames->length || property == vm.propertyNames->underscoreProto @@ -3716,7 +3708,9 @@ restart: JSC::JSValue propertyValue = jsUndefined(); if ((slot.attributes() & PropertyAttribute::DontEnum) != 0) { - if (slot.attributes() & PropertyAttribute::BuiltinOrFunction) { + if ((slot.attributes() & PropertyAttribute::Accessor) != 0) { + propertyValue = slot.getPureResult(); + } else if (slot.attributes() & PropertyAttribute::BuiltinOrFunction) { propertyValue = slot.getValue(globalObject, property); } else if (slot.isCustom()) { propertyValue = slot.getValue(globalObject, property); @@ -3725,6 +3719,8 @@ restart: } else if (object->getOwnPropertySlot(object, globalObject, property, slot)) { propertyValue = slot.getValue(globalObject, property); } + } else if ((slot.attributes() & PropertyAttribute::Accessor) != 0) { + propertyValue = slot.getPureResult(); } else { propertyValue = slot.getValue(globalObject, property); } diff --git a/src/bun.js/bindings/exports.zig b/src/bun.js/bindings/exports.zig index 704dd6ae0..d4401865d 100644 --- a/src/bun.js/bindings/exports.zig +++ b/src/bun.js/bindings/exports.zig @@ -1268,6 +1268,8 @@ pub const ZigConsoleClient = struct { JSX, Event, + Getter, + pub fn isPrimitive(this: Tag) bool { return switch (this) { .String, @@ -1433,8 +1435,6 @@ pub const ZigConsoleClient = struct { // None of these should ever exist here // But we're going to check anyway - .GetterSetter, - .CustomGetterSetter, .APIValueWrapper, .NativeExecutable, .ProgramExecutable, @@ -1461,6 +1461,8 @@ pub const ZigConsoleClient = struct { .Event => .Event, + .GetterSetter, .CustomGetterSetter => .Getter, + else => .JSON, }, .cell = js_type, @@ -2032,6 +2034,9 @@ pub const ZigConsoleClient = struct { writer.print(comptime Output.prettyFmt("<cyan>[Function<d>:<r> <cyan>{}]<r>", enable_ansi_colors), .{printable}); } }, + .Getter => { + writer.print(comptime Output.prettyFmt("<cyan>[Getter]<r>", enable_ansi_colors), .{}); + }, .Array => { const len = @truncate(u32, value.getLengthOfArray(this.globalThis)); if (len == 0) { @@ -2800,6 +2805,7 @@ pub const ZigConsoleClient = struct { .NativeCode => this.printAs(.NativeCode, Writer, writer, value, result.cell, enable_ansi_colors), .JSX => this.printAs(.JSX, Writer, writer, value, result.cell, enable_ansi_colors), .Event => this.printAs(.Event, Writer, writer, value, result.cell, enable_ansi_colors), + .Getter => this.printAs(.Getter, Writer, writer, value, result.cell, enable_ansi_colors), }; } }; diff --git a/test/js/bun/util/inspect.test.js b/test/js/bun/util/inspect.test.js index fe8f62a1c..de1adbcce 100644 --- a/test/js/bun/util/inspect.test.js +++ b/test/js/bun/util/inspect.test.js @@ -1,5 +1,29 @@ import { it, expect, describe } from "bun:test"; +it("getters", () => { + const obj = { + get foo() { + return 42; + }, + }; + + expect(Bun.inspect(obj)).toBe("{\n" + ' "foo": [Getter]' + "\n" + "}"); + var called = false; + const objWithThrowingGetter = { + get foo() { + called = true; + throw new Error("Test failed!"); + }, + set foo(v) { + called = true; + throw new Error("Test failed!"); + }, + }; + + expect(Bun.inspect(objWithThrowingGetter)).toBe("{\n" + ' "foo": [Getter]' + "\n" + "}"); + expect(called).toBe(false); +}); + it("Timeout", () => { const id = setTimeout(() => {}, 0); expect(Bun.inspect(id)).toBe(`Timeout (#${+id})`); |