diff --git a/JSTests/BUN-TEST-DIFFERENCES.md b/JSTests/BUN-TEST-DIFFERENCES.md index 9c13ee1da08d2..b7c40ae1b46f1 100644 --- a/JSTests/BUN-TEST-DIFFERENCES.md +++ b/JSTests/BUN-TEST-DIFFERENCES.md @@ -138,6 +138,11 @@ Rules for an edit to an upstream test: - The rule stands in two more places. `canPutDirectFast` (`runtime/CommonSlowPaths.h`) sends a class field on a returned object to the definition where the name is a custom value: `putDirect()` would put the value in its place. The extra properties of a compiled object have it in `tryHandValueToSetterOfExtraProperty` (`aot/object/AOTObjectMethodTable.cpp`). +- One more store ends in the definition: a store whose receiver is no proxy and answers `[[GetOwnProperty]]` with the custom + value of another object (the global object of a node:vm context in Bun, `$vm.createImpureGetter()` here). + `definePropertyOnReceiverSlow` calls the setter only where `slot.slotBase()` is the receiver. Upstream calls it for such + a slot too, with the receiver, which does not hold the property. That operand is no part of this rule and stays when the + rule goes: without it `stress/reflect-set-receiver-custom-value-of-another-object.js` aborts in a test setter. - What replaces the property as upstream has it: a definition that changes an attribute, a definition of an accessor, `delete`. `Object.seal()` and `Object.freeze()` are of the first kind for most objects: `setIntegrityLevel` (`runtime/ObjectConstructor.cpp`) defines each property again with `configurable: false`, so the custom value becomes a @@ -230,6 +235,7 @@ Not upstream's: a sync neither brings nor changes them. Most are named `stress/b | `stress/sort-scratch-double-pivot.js` | double arithmetic in the comparator of an inlined sort stores no double into an Int32 array | | `stress/array-sort-inline-int32-double-comparator.js` | an earlier, smaller test for the sort fix | | `stress/bun-definition-of-a-value-reaches-the-setter-of-a-custom-value.js` | a definition that gives a value and changes no attribute, and every store through a proxy that ends in one, calls the setter of a custom value with the holder and in the holder's realm, and leaves the property a custom value; a definition that changes an attribute, an accessor definition and `delete` replace it (A11) | +| `stress/reflect-set-receiver-custom-value-of-another-object.js` | a store whose receiver is no proxy and answers `[[GetOwnProperty]]` with the custom value of another object calls no setter: it ends in `[[DefineOwnProperty]]` on the receiver, as it does where the other object has a plain value; a receiver that holds the custom value itself reaches its setter | | `stress/bun-json-parse-long-string-has-the-width-of-its-text.js` | a long string value of `JSON.parse` has the width of the text it is parsed from, also where the same value was parsed from text of the other width before (`JSONCache::longStringMatches`) | | `stress/bun-object-assign-calls-indexed-setters-on-the-chain.js` | `Object.assign()` stores with `[[Set]]` on its fast path too: an indexed setter on the target's prototype chain is called, and a read-only element there refuses the store | | `stress/bun-arrays-become-prototypes-below-immutable-prototypes.js` | with `Object.prototype` and `Array.prototype` immutable and a setter under an index on a class prototype, an array or an object with elements that becomes a prototype keeps the storage it has; reads, stores and changes of length on it and through its heirs answer as the language says | diff --git a/JSTests/stress/reflect-set-receiver-custom-value-of-another-object.js b/JSTests/stress/reflect-set-receiver-custom-value-of-another-object.js new file mode 100644 index 0000000000000..e25ddfa0569e7 --- /dev/null +++ b/JSTests/stress/reflect-set-receiver-custom-value-of-another-object.js @@ -0,0 +1,82 @@ +// A store whose receiver answers [[GetOwnProperty]] with the slot of another object, where that slot is a custom value +// with a setter. The setter belongs to the object that holds the property, so the store does not call it with the +// receiver. It ends in [[DefineOwnProperty]] on the receiver, as it does where the other object has a plain value. +// +// $vm.createImpureGetter(delegate) is such a receiver, and no proxy. It has the [[DefineOwnProperty]] of an ordinary +// object, so it takes the property itself. A store asks it for its own slot only if a proxy is part of the store: here +// the object that the store starts at. +// +// The setters of customValue, customValue2 and customValueGlobalObject assert that they get the object that holds them. + +function shouldBe(actual, expected, what) { + if (actual !== expected) + throw new Error(what + ": got " + String(actual) + ", expected " + String(expected)); +} + +function store(key, value, receiver) { + return Reflect.set(new Proxy({}, {}), key, value, receiver); +} +noInline(store); + +function test() { + { + const holder = $vm.createCustomTestGetterSetter(); + const receiver = $vm.createImpureGetter(holder); + const value = {}; + shouldBe(store("customValue2", value, receiver), true, "customValue2"); + shouldBe(store("customValue", value, receiver), true, "customValue"); + shouldBe(store("customValueGlobalObject", value, receiver), true, "customValueGlobalObject"); + shouldBe(Object.hasOwn(holder, "value2"), false, "the setter of customValue2 was not called"); + shouldBe(Object.hasOwn(value, "result"), false, "the other two setters were not called"); + shouldBe(Reflect.ownKeys(receiver).join(), "customValue2,customValue,customValueGlobalObject", "what the receiver took"); + + // The holder's properties are what they were: a store to the holder reaches the setter. + holder.customValue2 = value; + shouldBe(holder.value2, value, "a store to the holder afterwards"); + } + + { + // The same for a custom value of a static table, whose setter puts a flag on the object it gets. + const holder = $vm.createStaticCustomValue(); + const receiver = $vm.createImpureGetter(holder); + shouldBe(store("testStaticValueSetFlag", 1, receiver), true, "testStaticValueSetFlag"); + shouldBe(holder.testStaticValueSetterCalled, undefined, "the flag on the holder"); + shouldBe(Reflect.ownKeys(receiver).join(), "testStaticValueSetFlag", "what the receiver took"); + } + + { + // ... and for an assignment to the receiver, with the proxy on its prototype chain. + const holder = $vm.createCustomTestGetterSetter(); + const receiver = $vm.createImpureGetter(holder); + Object.setPrototypeOf(receiver, new Proxy({}, {})); + receiver.customValue2 = {}; + shouldBe(Object.hasOwn(holder, "value2"), false, "an assignment to the receiver"); + shouldBe(Reflect.ownKeys(receiver).join(), "customValue2", "what the receiver took by an assignment"); + } + + { + // What such a store does where the other object has a plain value. + const holder = { plain: 1 }; + const receiver = $vm.createImpureGetter(holder); + shouldBe(store("plain", 2, receiver), true, "plain"); + shouldBe(holder.plain, 1, "the plain value of the holder"); + shouldBe(Reflect.ownKeys(receiver).join(), "plain", "what the receiver took of a plain value"); + } + + { + // A receiver that holds the custom value itself: the setter stands in for the store, with the receiver. + const holder = $vm.createCustomTestGetterSetter(); + const value = {}; + shouldBe(store("customValue2", value, holder), true, "customValue2 with the holder as the receiver"); + shouldBe(holder.value2, value, "what the setter got"); + shouldBe(store("customValue", value, holder), true, "customValue with the holder as the receiver"); + shouldBe(value.result, holder, "the object that the setter of customValue got"); + + const staticHolder = $vm.createStaticCustomValue(); + shouldBe(store("testStaticValueSetFlag", 1, staticHolder), true, "testStaticValueSetFlag with the holder as the receiver"); + shouldBe(staticHolder.testStaticValueSetterCalled, true, "the flag on the holder"); + } +} + +for (let i = 0; i < testLoopCount / 100; i++) + test(); diff --git a/Source/JavaScriptCore/runtime/JSObject.cpp b/Source/JavaScriptCore/runtime/JSObject.cpp index ef9b1da638dc2..351bac996734e 100644 --- a/Source/JavaScriptCore/runtime/JSObject.cpp +++ b/Source/JavaScriptCore/runtime/JSObject.cpp @@ -873,7 +873,9 @@ static NEVER_INLINE bool definePropertyOnReceiverSlow(JSGlobalObject* globalObje // A custom setter stands in for [[Set]] on the receiver itself. Through a proxy, the property is // reached by [[DefineOwnProperty]] instead, which replaces the custom value. - if (originalReceiver->type() != ProxyObjectType && (slot.attributes() & PropertyAttribute::CustomValue)) { + // Bun: so is a property that another object holds, where the receiver answers [[GetOwnProperty]] with that object's + // slot. The setter belongs to slot.slotBase(). Upstream calls it with the receiver. + if (originalReceiver->type() != ProxyObjectType && (slot.attributes() & PropertyAttribute::CustomValue) && slot.slotBase() == receiver) { // (Not for a receiver with immutable properties: a native setter does not ask, and [[DefineOwnProperty]] below does.) PutValueFunc customSetter = slot.customSetter(); if (customSetter && !receiver->structure()->isImmutableProperty(propertyName.uid()))