From b7a8fb86b277ffe06830662b68638096c599512e Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Fri, 13 Nov 2026 15:55:02 +0000 Subject: [PATCH 1/3] [JSC] A store to an index of a global object hands globalThis to a setter and to a Proxy JSObject::putByIndex() takes no receiver. The code behind it hands the object it stores to, to a setter and to a Proxy on the prototype chain. For a global object that is the JSGlobalObject, which script does not hold. It is a JSScope, so a strict setter sees `undefined` and a sloppy setter sees the global object of its own realm. receiverForPutByIndex() returns the proxy of a global object and every other value as it is. The three places that hand a receiver to script call it: the setter call of a sparse map entry, the hand-off to a Proxy, and the store by name of index 4294967295. The setter call is out of line. With the check inline, SparseArrayEntry::put() is no longer inlined into SparseArrayValueMap::putEntry(). --- JSTests/BUN-TEST-DIFFERENCES.md | 1 + .../global-object-put-by-index-receiver.js | 285 ++++++++++++++++++ .../JavaScriptCore/runtime/JSGlobalObject.h | 13 + Source/JavaScriptCore/runtime/JSObject.cpp | 4 +- Source/JavaScriptCore/runtime/JSObject.h | 2 +- .../runtime/SparseArrayValueMap.cpp | 9 +- 6 files changed, 310 insertions(+), 4 deletions(-) create mode 100644 JSTests/stress/global-object-put-by-index-receiver.js diff --git a/JSTests/BUN-TEST-DIFFERENCES.md b/JSTests/BUN-TEST-DIFFERENCES.md index 9c13ee1da08d2..8d828e0f4ec4b 100644 --- a/JSTests/BUN-TEST-DIFFERENCES.md +++ b/JSTests/BUN-TEST-DIFFERENCES.md @@ -264,6 +264,7 @@ Not upstream's: a sync neither brings nor changes them. Most are named `stress/b | `stress/bun-return-address-of-function-is-signed.js` | with `ENABLE(RETURN_ADDRESS_SIGNING)` a function signs the return address that it saves in LLInt, Baseline, the DFG and the FTL alike: `$vm.returnAddressOfCallerIsSigned()` signs the address again and compares. Elsewhere, and on a processor without pointer authentication (`$vm.functionsSignReturnAddress()`), the test ends at once | | `stress/bun-return-address-with-wrong-signature-is-not-returned-to.js` | with `ENABLE(RETURN_ADDRESS_SIGNING)` a function does not return once a bit of the signature of its saved return address was flipped, in each of the four tiers: the run must crash. Where nothing is signed the test crashes by itself | | `stress/bun-frozen-array-push.js` | `push()` throws for an array that was frozen in place, with copy-on-write storage (`JSObject::tryFreezeArrayInPlace`), in every tier and with no arguments too: the DFG makes no `GetArrayLength` of that push (`ArrayPushIntrinsic` in `dfg/DFGByteCodeParser.cpp`) | +| `stress/global-object-put-by-index-receiver.js` | a store to an array index of a global object has the global object's proxy as its receiver: a setter on the global object or on its prototype chain gets it as `this`, and a Proxy there gets it in its `set` trap, for every way to store and in every tier. Which stores a setter takes does not change. Upstream hands out the `JSGlobalObject`, which a strict setter sees as `undefined` and a sloppy one as the global object of its own realm | | the fr-CA block of `stress/intl-date-pattern-includes-literal-text.js` (the file is upstream's) | keeps the file's coverage on CLDR 49, where fr's pattern no longer has literal text: an `h` inside literal text is not mistaken for the hour field | | the last block of `stress/regexp-test-sticky-first-char-filter.js` (the file is upstream's; nothing of upstream's text changes) | the first-character filter of a sticky `test()` comes from the first sets of the alternatives (`yarr/YarrFirstFollowSets.*`): behind a leading lookbehind, forward reference or lookahead, and in groups nested more than eight deep, a byte that passes the filter still reaches the matcher | diff --git a/JSTests/stress/global-object-put-by-index-receiver.js b/JSTests/stress/global-object-put-by-index-receiver.js new file mode 100644 index 0000000000000..f59e07cad8173 --- /dev/null +++ b/JSTests/stress/global-object-put-by-index-receiver.js @@ -0,0 +1,285 @@ +//@ requireOptions("--useShadowRealm=1") + +// A store to an array index of a global object ends in JSObject::putByIndex() on the JSGlobalObject, and putByIndex() takes +// no receiver. Script holds a global object only as its proxy, globalThis, so that is the receiver of the store: a setter +// gets it as `this`, and the set trap of a Proxy gets it as its receiver argument. + +function shouldBe(actual, expected, message) { + if (actual !== expected) + throw new Error(message + ": expected " + String(expected) + " but got " + String(actual)); +} + +// The TypeError of a store is the one of the realm whose code stores. +function shouldThrowTypeError(global, run, message) { + let thrown = null; + try { + run(); + } catch (error) { + thrown = error; + } + if (!(thrown instanceof global.TypeError)) + throw new Error(message + ": expected a TypeError but got " + String(thrown)); +} + +const otherRealm = createGlobalObject(); +const realms = [["the main realm", globalThis], ["another realm", otherRealm]]; + +// 1000000000 is beyond any vector, and 4294967294 is the largest array index. +const keys = [0, 7, 1000000000, 4294967294]; + +const stores = [ + ["global[key] = value", (global, key) => { global[key] = "value"; }], + ["'use strict'; global[key] = value", (global, key) => { "use strict"; global[key] = "value"; }], + ["global[String(key)] = value", (global, key) => { global[String(key)] = "value"; }], + ["Reflect.set(global, key, value)", (global, key) => { shouldBe(Reflect.set(global, key, "value"), true, "Reflect.set"); }], + ["Reflect.set(global, key, value, global)", (global, key) => { shouldBe(Reflect.set(global, key, "value", global), true, "Reflect.set"); }], + ["Object.assign(global, { [key]: value })", (global, key) => { Object.assign(global, { [key]: "value" }); }], + ["[global[key]] = [value]", (global, key) => { [global[key]] = ["value"]; }], + ["for (global[key] of [value]);", (global, key) => { for (global[key] of ["value"]); }], + ["this[key] = value in global code", (global, key) => { global.eval(`this[${key}] = "value";`); }], +]; + +// A sloppy function replaces a `this` that is no object with the global object of its own realm. So a sloppy setter +// of one realm on the global object of the other tells "no receiver" from the right receiver, as a strict one does. +const setterMakers = [ + ["a strict setter", realm => realm.Function("report", "return function (value) { 'use strict'; report(this, value); }")], + ["a sloppy setter", realm => realm.Function("report", "return function (value) { report(this, value); }")], +]; + +// Defines a setter for `key` on `holder`, runs `store`, and says what the setter was called with. +function storeWithSetter(holder, key, makeSetter, store) { + const seen = { calls: 0, receiver: undefined, value: undefined }; + Object.defineProperty(holder, key, { + set: makeSetter((thisValue, value) => { + seen.calls++; + seen.receiver = thisValue; + seen.value = value; + }), + configurable: true, + }); + try { + store(); + } finally { + delete holder[key]; + } + return seen; +} + +function holdersOf(global) { + return [["Object.prototype", global.Object.prototype], ["the global object itself", global]]; +} + +for (const [realmName, global] of realms) { + for (const [holderName, holder] of holdersOf(global)) { + for (const [setterName, makeSetter] of setterMakers) { + for (const [setterRealmName, setterRealm] of realms) { + for (const [storeName, store] of stores) { + for (const key of keys) { + const message = `${storeName}, key ${key}, global object of ${realmName}, ${setterName} of ${setterRealmName} on ${holderName}`; + const seen = storeWithSetter(holder, key, makeSetter(setterRealm), () => store(global, key)); + shouldBe(seen.calls, 1, message + ": calls of the setter"); + shouldBe(seen.receiver, global, message + ": this"); + shouldBe(seen.value, "value", message + ": value"); + } + } + } + } + } +} + +// Functions of Array.prototype store to their `this`. Each of these stores "value" at index 0. +const mutators = [ + ["push", 0, global => { Array.prototype.push.call(global, "value"); }], + ["unshift", 0, global => { Array.prototype.unshift.call(global, "value"); }], + ["splice", 0, global => { Array.prototype.splice.call(global, 0, 0, "value"); }], + ["fill", 1, global => { Array.prototype.fill.call(global, "value"); }], + ["copyWithin", 2, global => { Array.prototype.copyWithin.call(global, 0, 1); }], + ["reverse", 2, global => { Array.prototype.reverse.call(global); }], +]; +const strictSetterOfMainRealm = setterMakers[0][1](globalThis); +for (const [realmName, global] of realms) { + for (const [holderName, holder] of holdersOf(global)) { + for (const [mutatorName, length, mutate] of mutators) { + const message = `Array.prototype.${mutatorName} on the global object of ${realmName}, setter on ${holderName}`; + global.length = length; + if (length === 2) + global[1] = "value"; + const seen = storeWithSetter(holder, 0, strictSetterOfMainRealm, () => mutate(global)); + delete global[1]; + shouldBe(delete global.length, true, message + ": delete of length"); + shouldBe(seen.calls, 1, message + ": calls of the setter"); + shouldBe(seen.receiver, global, message + ": this"); + shouldBe(seen.value, "value", message + ": value"); + } + } +} + +// A program that is a JSONP assignment is not compiled: the interpreter does the store itself. +for (const [realmName, global] of realms) { + for (const [holderName, holder] of holdersOf(global)) { + const message = `a JSONP program in ${realmName}, setter on ${holderName}`; + const seen = storeWithSetter(holder, 5, strictSetterOfMainRealm, () => global.loadString(`globalThis[5] = {"jsonp": true}`)); + shouldBe(seen.calls, 1, message + ": calls of the setter"); + shouldBe(seen.receiver, global, message + ": this"); + shouldBe(seen.value.jsonp, true, message + ": value"); + } +} + +// The same store from every tier. +{ + let receiver; + Object.defineProperty(Object.prototype, 3, { + set(value) { + "use strict"; + receiver = this; + }, + configurable: true, + }); + function sloppyStore(global, value) { + global[3] = value; + } + noInline(sloppyStore); + function strictStore(global, value) { + "use strict"; + global[3] = value; + } + noInline(strictStore); + for (let i = 0; i < testLoopCount; i++) { + receiver = null; + sloppyStore(globalThis, i); + shouldBe(receiver, globalThis, "sloppy store, iteration " + i); + receiver = null; + strictStore(globalThis, i); + shouldBe(receiver, globalThis, "strict store, iteration " + i); + } + + // Other receivers stay what they are: an object that is no global object, and a primitive. + const object = {}; + receiver = null; + object[3] = 1; + shouldBe(receiver, object, "a store to a plain object"); + const array = []; + receiver = null; + array[3] = 1; + shouldBe(receiver, array, "a store to an array"); + receiver = null; + (function () { + "use strict"; + try { + (5)[3] = 1; + } catch { } + })(); + shouldBe(receiver, 5, "a store to a number"); + + delete Object.prototype[3]; +} + +// What decides whether a setter is called does not change. +for (const [realmName, global] of realms) { + const hasOwn = key => global.Object.prototype.hasOwnProperty.call(global, key); + const strictStore = global.Function("key", "value", "'use strict'; globalThis[key] = value;"); + + // No accessor: the store makes a data property of the global object. + for (const key of keys) { + const message = `key ${key} of the global object of ${realmName}`; + global[key] = "data"; + const descriptor = Object.getOwnPropertyDescriptor(global, key); + shouldBe(descriptor.value, "data", message + ": value"); + shouldBe(descriptor.writable && descriptor.enumerable && descriptor.configurable, true, message + ": attributes"); + shouldBe(global[key], "data", message + ": read"); + shouldBe(delete global[key], true, message + ": delete"); + } + + for (const [holderName, holder] of holdersOf(global)) { + const message = `the global object of ${realmName}, ${holderName}`; + + // A setter that was deleted takes no store. + let calls = 0; + Object.defineProperty(holder, 9, { set(value) { calls++; }, configurable: true }); + shouldBe(delete holder[9], true, message + ": delete of the setter"); + global[9] = "data"; + shouldBe(calls, 0, message + ": calls of a deleted setter"); + shouldBe(hasOwn(9) && global[9], "data", message + ": store after a deleted setter"); + shouldBe(delete global[9], true, message + ": delete"); + + // A getter without a setter, and a value that is read-only: sloppy code gives up, strict code throws. + for (const [propertyName, property] of [["a getter without a setter", { get() { return "fixed"; }, configurable: true }], ["a read-only value", { value: "fixed", writable: false, configurable: true }]]) { + Object.defineProperty(holder, 9, property); + global[9] = "other"; + shouldBe(global[9], "fixed", `${message}, ${propertyName}: sloppy store`); + shouldBe(Reflect.set(global, 9, "other"), false, `${message}, ${propertyName}: Reflect.set`); + shouldThrowTypeError(global, () => strictStore(9, "other"), `${message}, ${propertyName}: strict store`); + shouldBe(global[9], "fixed", `${message}, ${propertyName}: strict store`); + shouldBe(delete holder[9], true, `${message}, ${propertyName}: delete`); + } + } + + // A data property of the global object is in front of a setter on its prototype chain. + { + const message = `an own data property of the global object of ${realmName} in front of a setter`; + let calls = 0; + global[9] = "own"; + Object.defineProperty(global.Object.prototype, 9, { set(value) { calls++; }, configurable: true }); + global[9] = "again"; + strictStore(9, "again"); + shouldBe(calls, 0, message + ": calls of the setter"); + shouldBe(global[9], "again", message + ": value"); + shouldBe(delete global.Object.prototype[9] && delete global[9], true, message + ": delete"); + } +} + +// A global object that is not extensible takes no new index, and a setter on its prototype chain is still called. +{ + const global = createGlobalObject(); + const strictStore = global.Function("key", "value", "'use strict'; globalThis[key] = value;"); + shouldBe(Reflect.preventExtensions(global), true, "preventExtensions of a global object"); + global[9] = "data"; + shouldBe(9 in global, false, "a sloppy store to a global object that is not extensible"); + shouldThrowTypeError(global, () => strictStore(9, "data"), "a strict store to a global object that is not extensible"); + const seen = storeWithSetter(global.Object.prototype, 9, strictSetterOfMainRealm, () => strictStore(9, "value")); + shouldBe(seen.calls, 1, "a global object that is not extensible: calls of the setter"); + shouldBe(seen.receiver, global, "a global object that is not extensible: this"); +} + +// A Proxy on the prototype chain of a global object. The global object of the shell has an immutable prototype, the +// one of a ShadowRealm has not. +{ + const realm = new ShadowRealm; + const result = realm.evaluate(` + (function () { + const log = []; + const name = value => value === globalThis ? "globalThis" : typeof value; + const target = {}; + const withTrap = new Proxy(target, { + set(target, key, value, receiver) { + log.push("set trap " + key + " " + name(receiver)); + return true; + }, + }); + Object.setPrototypeOf(globalThis, Object.create(withTrap)); + globalThis[0] = 1; + globalThis[1000000000] = 1; + Reflect.set(globalThis, 1, 1); + (function () { + "use strict"; + globalThis[2] = 1; + })(); + log.push("own " + Object.getOwnPropertyNames(globalThis).filter(key => key === "0" || key === "1" || key === "2" || key === "1000000000").length); + + // Without a set trap the store goes on at the target, with the same receiver. + Object.defineProperty(target, 3, { + set(value) { + "use strict"; + log.push("setter behind the proxy " + name(this)); + }, + configurable: true, + }); + Object.setPrototypeOf(globalThis, new Proxy(target, {})); + globalThis[3] = 1; + globalThis[4] = "four"; + log.push("own 4 " + Object.hasOwn(globalThis, 4) + " " + Object.hasOwn(target, 4) + " " + globalThis[4]); + return log.join(", "); + })() + `); + shouldBe(result, "set trap 0 globalThis, set trap 1000000000 globalThis, set trap 1 globalThis, set trap 2 globalThis, own 0, setter behind the proxy globalThis, own 4 true false four", "a Proxy on the prototype chain of the global object of a ShadowRealm"); +} diff --git a/Source/JavaScriptCore/runtime/JSGlobalObject.h b/Source/JavaScriptCore/runtime/JSGlobalObject.h index 72b4bf59581f8..cf092b28b16de 100644 --- a/Source/JavaScriptCore/runtime/JSGlobalObject.h +++ b/Source/JavaScriptCore/runtime/JSGlobalObject.h @@ -1490,6 +1490,19 @@ inline JSObject* JSGlobalObject::globalThis() const return m_globalThis.get(); } +// putByIndex() takes no receiver: the code behind it takes the object it stores to for one. Script stores to a global object +// through its proxy, and the JSGlobalObject itself is a JSScope, which a callee reads as no `this` (JSValue::toThis()). This +// is what to hand to a setter or to a Proxy in place of that object. Upstream hands out the JSGlobalObject. +ALWAYS_INLINE JSValue receiverForPutByIndex(JSValue thisValue) +{ + if (thisValue.isCell() && thisValue.asCell()->type() == GlobalObjectType) [[unlikely]] { + // Null until JSGlobalObject::finishCreation() has made the proxy. + if (JSObject* globalThis = uncheckedDowncast(thisValue.asCell())->globalThis()) + return globalThis; + } + return thisValue; +} + ALWAYS_INLINE VM& getVM(JSGlobalObject* globalObject) { return globalObject->vm(); diff --git a/Source/JavaScriptCore/runtime/JSObject.cpp b/Source/JavaScriptCore/runtime/JSObject.cpp index ef9b1da638dc2..1fa8c737288ef 100644 --- a/Source/JavaScriptCore/runtime/JSObject.cpp +++ b/Source/JavaScriptCore/runtime/JSObject.cpp @@ -985,7 +985,7 @@ bool JSObject::putByIndex(JSCell* cell, JSGlobalObject* globalObject, unsigned p } if (propertyName > MAX_ARRAY_INDEX) { - PutPropertySlot slot(cell, shouldThrow); + PutPropertySlot slot(receiverForPutByIndex(cell), shouldThrow); return thisObject->methodTable()->put(thisObject, globalObject, Identifier::from(vm, propertyName), value, slot); } @@ -3727,7 +3727,7 @@ bool JSObject::attemptToInterceptPutByIndexOnHoleForPrototype(JSGlobalObject* gl if (current->type() == ProxyObjectType) { scope.release(); auto* proxy = uncheckedDowncast(current); - putResult = proxy->putByIndexCommon(globalObject, thisValue, i, value, shouldThrow); + putResult = proxy->putByIndexCommon(globalObject, receiverForPutByIndex(thisValue), i, value, shouldThrow); return true; } diff --git a/Source/JavaScriptCore/runtime/JSObject.h b/Source/JavaScriptCore/runtime/JSObject.h index 48c18dd887d3e..9330c9dca80f4 100644 --- a/Source/JavaScriptCore/runtime/JSObject.h +++ b/Source/JavaScriptCore/runtime/JSObject.h @@ -234,7 +234,7 @@ class JSObject : public JSCell { JS_EXPORT_PRIVATE static bool put(JSCell*, JSGlobalObject*, PropertyName, JSValue, PutPropertySlot&); NODELETE static bool mightBeSpecialProperty(VM&, JSType, UniquedStringImpl*); JS_EXPORT_PRIVATE NEVER_INLINE static bool definePropertyOnReceiver(JSGlobalObject*, PropertyName, JSValue, PutPropertySlot&); - // putByIndex assumes that the receiver is this JSCell object. + // putByIndex assumes that the receiver is this JSCell object. For a JSGlobalObject that is its proxy: receiverForPutByIndex(). JS_EXPORT_PRIVATE static bool putByIndex(JSCell*, JSGlobalObject*, unsigned propertyName, JSValue, bool shouldThrow); // This performs the ECMAScript Set() operation. diff --git a/Source/JavaScriptCore/runtime/SparseArrayValueMap.cpp b/Source/JavaScriptCore/runtime/SparseArrayValueMap.cpp index 3390f8d17cc76..c11811fa9c4a3 100644 --- a/Source/JavaScriptCore/runtime/SparseArrayValueMap.cpp +++ b/Source/JavaScriptCore/runtime/SparseArrayValueMap.cpp @@ -247,6 +247,13 @@ JSValue SparseArrayEntry::getConcurrently() const return attributesDependency.consume(this)->m_value.get(); } +// Not inline: with the receiver check in it, SparseArrayEntry::put() is too large to be inlined into +// SparseArrayValueMap::putEntry(), and every store to a sparse map pays for the call. +static NEVER_INLINE bool callSetterOfIndex(JSGlobalObject* globalObject, GetterSetter* getterSetter, JSValue thisValue, JSValue value, bool shouldThrow) +{ + return getterSetter->callSetter(globalObject, receiverForPutByIndex(thisValue), value, shouldThrow); +} + bool SparseArrayEntry::put(JSGlobalObject* globalObject, JSValue thisValue, SparseArrayValueMap* map, JSValue value, bool shouldThrow) { VM& vm = globalObject->vm(); @@ -260,7 +267,7 @@ bool SparseArrayEntry::put(JSGlobalObject* globalObject, JSValue thisValue, Spar return true; } - RELEASE_AND_RETURN(scope, uncheckedDowncast(m_value.get())->callSetter(globalObject, thisValue, value, shouldThrow)); + RELEASE_AND_RETURN(scope, callSetterOfIndex(globalObject, uncheckedDowncast(m_value.get()), thisValue, value, shouldThrow)); } JSValue SparseArrayEntry::getNonSparseMode() const From 094f4f0d1c1a2fe3e3af5791dedbf05bdfba6120 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Fri, 13 Nov 2026 17:42:49 +0000 Subject: [PATCH 2/3] Hand a receiver to a Proxy out of line, and keep the arguments of both calls in their registers callSetterOfIndex() takes its parameters in the order of GetterSetter::callSetter()'s. The hand-off to a Proxy goes through putByIndexThroughProxy(), which is not inline and takes its parameters in the order of ProxyObject::putByIndexCommon()'s. SparseArrayValueMap::putEntry(), SparseArrayEntry::put() and JSObject::attemptToInterceptPutByIndexOnHoleForPrototype() then compile to the code they were, but for the function each of them calls. --- Source/JavaScriptCore/runtime/JSObject.cpp | 9 ++++++++- Source/JavaScriptCore/runtime/SparseArrayValueMap.cpp | 7 ++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Source/JavaScriptCore/runtime/JSObject.cpp b/Source/JavaScriptCore/runtime/JSObject.cpp index 1fa8c737288ef..96083b6eb2759 100644 --- a/Source/JavaScriptCore/runtime/JSObject.cpp +++ b/Source/JavaScriptCore/runtime/JSObject.cpp @@ -3692,6 +3692,13 @@ void JSObject::deallocateSparseIndexMap() arrayStorage->m_sparseMap.clear(); } +// Not inline, and with the parameters in the order of ProxyObject::putByIndexCommon()'s: the walk below then compiles +// to what it was, but for the function it calls. +static NEVER_INLINE bool putByIndexThroughProxy(ProxyObject* proxy, JSGlobalObject* globalObject, JSValue thisValue, unsigned i, JSValue value, bool shouldThrow) +{ + return proxy->putByIndexCommon(globalObject, receiverForPutByIndex(thisValue), i, value, shouldThrow); +} + bool JSObject::attemptToInterceptPutByIndexOnHoleForPrototype(JSGlobalObject* globalObject, JSValue thisValue, unsigned i, JSValue value, bool shouldThrow, bool& putResult) { VM& vm = globalObject->vm(); @@ -3727,7 +3734,7 @@ bool JSObject::attemptToInterceptPutByIndexOnHoleForPrototype(JSGlobalObject* gl if (current->type() == ProxyObjectType) { scope.release(); auto* proxy = uncheckedDowncast(current); - putResult = proxy->putByIndexCommon(globalObject, receiverForPutByIndex(thisValue), i, value, shouldThrow); + putResult = putByIndexThroughProxy(proxy, globalObject, thisValue, i, value, shouldThrow); return true; } diff --git a/Source/JavaScriptCore/runtime/SparseArrayValueMap.cpp b/Source/JavaScriptCore/runtime/SparseArrayValueMap.cpp index c11811fa9c4a3..da5dd775234cd 100644 --- a/Source/JavaScriptCore/runtime/SparseArrayValueMap.cpp +++ b/Source/JavaScriptCore/runtime/SparseArrayValueMap.cpp @@ -248,8 +248,9 @@ JSValue SparseArrayEntry::getConcurrently() const } // Not inline: with the receiver check in it, SparseArrayEntry::put() is too large to be inlined into -// SparseArrayValueMap::putEntry(), and every store to a sparse map pays for the call. -static NEVER_INLINE bool callSetterOfIndex(JSGlobalObject* globalObject, GetterSetter* getterSetter, JSValue thisValue, JSValue value, bool shouldThrow) +// SparseArrayValueMap::putEntry(), and every store to a sparse map pays for the call. The parameters are in the order +// of GetterSetter::callSetter()'s, so that they stay in their registers. +static NEVER_INLINE bool callSetterOfIndex(GetterSetter* getterSetter, JSGlobalObject* globalObject, JSValue thisValue, JSValue value, bool shouldThrow) { return getterSetter->callSetter(globalObject, receiverForPutByIndex(thisValue), value, shouldThrow); } @@ -267,7 +268,7 @@ bool SparseArrayEntry::put(JSGlobalObject* globalObject, JSValue thisValue, Spar return true; } - RELEASE_AND_RETURN(scope, callSetterOfIndex(globalObject, uncheckedDowncast(m_value.get()), thisValue, value, shouldThrow)); + RELEASE_AND_RETURN(scope, callSetterOfIndex(uncheckedDowncast(m_value.get()), globalObject, thisValue, value, shouldThrow)); } JSValue SparseArrayEntry::getNonSparseMode() const From 88d4fd5b563ce218227a3eadb68957ede6615a5d Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sat, 14 Nov 2026 00:46:33 +0000 Subject: [PATCH 3/3] Hand the receiver to a Proxy inline again, and test a store that starts on the JSGlobalObject The Proxy hand-off in attemptToInterceptPutByIndexOnHoleForPrototype() calls receiverForPutByIndex() in place, as it did in the first commit. The function that took it out of line cost a store that reaches a Proxy 5 instructions and saved none for any other store. The comment of receiverForPutByIndex() gives the reason that holds wherever a store starts: a JSGlobalObject and its proxy are one object to script, and only the proxy can be a `this`. A native can hand the JSGlobalObject itself to script. The test has a block for that, with $vm.createGlobalObject(). The comment and JSTests/BUN-TEST-DIFFERENCES.md name upstream's swap for the setter (284931@main, bug 225397, reverted by 284962@main, WebKit/WebKit#35527) and say what a sync has to keep. The test is the fork's own, so its name begins with bun-. --- JSTests/BUN-TEST-DIFFERENCES.md | 2 +- ...un-global-object-put-by-index-receiver.js} | 65 ++++++++++++++++++- .../JavaScriptCore/runtime/JSGlobalObject.h | 10 ++- Source/JavaScriptCore/runtime/JSObject.cpp | 9 +-- 4 files changed, 72 insertions(+), 14 deletions(-) rename JSTests/stress/{global-object-put-by-index-receiver.js => bun-global-object-put-by-index-receiver.js} (83%) diff --git a/JSTests/BUN-TEST-DIFFERENCES.md b/JSTests/BUN-TEST-DIFFERENCES.md index 8d828e0f4ec4b..a1c9b3cfbe17f 100644 --- a/JSTests/BUN-TEST-DIFFERENCES.md +++ b/JSTests/BUN-TEST-DIFFERENCES.md @@ -264,7 +264,7 @@ Not upstream's: a sync neither brings nor changes them. Most are named `stress/b | `stress/bun-return-address-of-function-is-signed.js` | with `ENABLE(RETURN_ADDRESS_SIGNING)` a function signs the return address that it saves in LLInt, Baseline, the DFG and the FTL alike: `$vm.returnAddressOfCallerIsSigned()` signs the address again and compares. Elsewhere, and on a processor without pointer authentication (`$vm.functionsSignReturnAddress()`), the test ends at once | | `stress/bun-return-address-with-wrong-signature-is-not-returned-to.js` | with `ENABLE(RETURN_ADDRESS_SIGNING)` a function does not return once a bit of the signature of its saved return address was flipped, in each of the four tiers: the run must crash. Where nothing is signed the test crashes by itself | | `stress/bun-frozen-array-push.js` | `push()` throws for an array that was frozen in place, with copy-on-write storage (`JSObject::tryFreezeArrayInPlace`), in every tier and with no arguments too: the DFG makes no `GetArrayLength` of that push (`ArrayPushIntrinsic` in `dfg/DFGByteCodeParser.cpp`) | -| `stress/global-object-put-by-index-receiver.js` | a store to an array index of a global object has the global object's proxy as its receiver: a setter on the global object or on its prototype chain gets it as `this`, and a Proxy there gets it in its `set` trap, for every way to store and in every tier. Which stores a setter takes does not change. Upstream hands out the `JSGlobalObject`, which a strict setter sees as `undefined` and a sloppy one as the global object of its own realm | +| `stress/bun-global-object-put-by-index-receiver.js` | a store to an array index of a global object has the global object's proxy as its receiver: a setter on the global object or on its prototype chain gets it as `this`, and a Proxy there gets it in its `set` trap, for every way to store, in every tier, and also where the store starts on the `JSGlobalObject` itself. Which stores a setter takes does not change. Upstream hands out the `JSGlobalObject`, which a strict setter sees as `undefined` and a sloppy one as the global object of its own realm. Upstream's `wrapGlobalObject()` (284931@main, bug 225397, reverted by 284962@main; WebKit/WebKit#35527 lands it again) is the same swap for the setter alone. **A sync that brings it keeps `receiverForPutByIndex()` at both calls in `runtime/JSObject.cpp`: this test holds the one for a Proxy, and Bun's `test/napi/napi.test.ts` the one for index 4294967295** | | the fr-CA block of `stress/intl-date-pattern-includes-literal-text.js` (the file is upstream's) | keeps the file's coverage on CLDR 49, where fr's pattern no longer has literal text: an `h` inside literal text is not mistaken for the hour field | | the last block of `stress/regexp-test-sticky-first-char-filter.js` (the file is upstream's; nothing of upstream's text changes) | the first-character filter of a sticky `test()` comes from the first sets of the alternatives (`yarr/YarrFirstFollowSets.*`): behind a leading lookbehind, forward reference or lookahead, and in groups nested more than eight deep, a byte that passes the filter still reaches the matcher | diff --git a/JSTests/stress/global-object-put-by-index-receiver.js b/JSTests/stress/bun-global-object-put-by-index-receiver.js similarity index 83% rename from JSTests/stress/global-object-put-by-index-receiver.js rename to JSTests/stress/bun-global-object-put-by-index-receiver.js index f59e07cad8173..c5937cecfbaac 100644 --- a/JSTests/stress/global-object-put-by-index-receiver.js +++ b/JSTests/stress/bun-global-object-put-by-index-receiver.js @@ -1,8 +1,9 @@ //@ requireOptions("--useShadowRealm=1") // A store to an array index of a global object ends in JSObject::putByIndex() on the JSGlobalObject, and putByIndex() takes -// no receiver. Script holds a global object only as its proxy, globalThis, so that is the receiver of the store: a setter -// gets it as `this`, and the set trap of a Proxy gets it as its receiver argument. +// no receiver. The JSGlobalObject and its proxy, globalThis, are one object to script, and only the proxy can be a `this`. +// So the proxy is the receiver of the store: a setter gets it as `this`, and the set trap of a Proxy gets it as its +// receiver argument. function shouldBe(actual, expected, message) { if (actual !== expected) @@ -228,6 +229,66 @@ for (const [realmName, global] of realms) { } } +// A native can hand the JSGlobalObject itself to script, as $vm.createGlobalObject() does. A store to an index of that one +// has the proxy as its receiver too. +{ + const bare = $vm.createGlobalObject(); + const global = bare.globalThis; + shouldBe(bare === global, false, "$vm.createGlobalObject() returns the JSGlobalObject"); + + for (const [holderName, holder] of holdersOf(global)) { + for (const [setterName, makeSetter] of setterMakers) { + for (const [setterRealmName, setterRealm] of realms) { + for (const [storeName, store] of stores) { + for (const key of keys) { + const message = `${storeName}, key ${key}, global is a JSGlobalObject, ${setterName} of ${setterRealmName} on ${holderName}`; + const seen = storeWithSetter(holder, key, makeSetter(setterRealm), () => store(bare, key)); + shouldBe(seen.calls, 1, message + ": calls of the setter"); + shouldBe(seen.receiver, global, message + ": this"); + shouldBe(seen.value, "value", message + ": value"); + } + } + } + } + } + + let receiver; + Object.defineProperty(global.Object.prototype, 3, { + set(value) { + "use strict"; + receiver = this; + }, + configurable: true, + }); + function storeInEveryTier(object, value) { + object[3] = value; + } + noInline(storeInEveryTier); + for (let i = 0; i < testLoopCount; i++) { + receiver = null; + storeInEveryTier(bare, i); + shouldBe(receiver, global, "a store to a JSGlobalObject, iteration " + i); + } + + // The same with a Proxy on the prototype chain. + const receivers = []; + const withTrap = new Proxy({}, { + set(target, key, value, receiver) { + receivers.push(receiver); + return true; + }, + }); + const bareBelowProxy = $vm.createGlobalObject(Object.create(withTrap)); + for (const [storeName, store] of stores) { + for (const key of keys) { + receivers.length = 0; + store(bareBelowProxy, key); + shouldBe(receivers.length, 1, `${storeName}, key ${key}, global is a JSGlobalObject: calls of the set trap`); + shouldBe(receivers[0], bareBelowProxy.globalThis, `${storeName}, key ${key}, global is a JSGlobalObject: receiver of the set trap`); + } + } +} + // A global object that is not extensible takes no new index, and a setter on its prototype chain is still called. { const global = createGlobalObject(); diff --git a/Source/JavaScriptCore/runtime/JSGlobalObject.h b/Source/JavaScriptCore/runtime/JSGlobalObject.h index cf092b28b16de..bb71ed6e889dc 100644 --- a/Source/JavaScriptCore/runtime/JSGlobalObject.h +++ b/Source/JavaScriptCore/runtime/JSGlobalObject.h @@ -1490,9 +1490,13 @@ inline JSObject* JSGlobalObject::globalThis() const return m_globalThis.get(); } -// putByIndex() takes no receiver: the code behind it takes the object it stores to for one. Script stores to a global object -// through its proxy, and the JSGlobalObject itself is a JSScope, which a callee reads as no `this` (JSValue::toThis()). This -// is what to hand to a setter or to a Proxy in place of that object. Upstream hands out the JSGlobalObject. +// putByIndex() takes no receiver: the code behind it takes the object it stores to for one. A JSGlobalObject and its proxy are +// one object to script (isThisValueAltered()), and only the proxy can be a `this`: the JSGlobalObject is a JSScope, which a +// callee reads as no `this` (JSValue::toThis()). So a setter and a Proxy get the proxy in place of the JSGlobalObject. +// +// Upstream has this for the setter alone, as wrapGlobalObject() in SparseArrayEntry::put(): 284931@main (bug 225397), which +// 284962@main reverted and WebKit/WebKit#35527 lands again. A sync that brings it leaves one swap there, and keeps the two +// calls in JSObject.cpp: upstream has neither. ALWAYS_INLINE JSValue receiverForPutByIndex(JSValue thisValue) { if (thisValue.isCell() && thisValue.asCell()->type() == GlobalObjectType) [[unlikely]] { diff --git a/Source/JavaScriptCore/runtime/JSObject.cpp b/Source/JavaScriptCore/runtime/JSObject.cpp index 96083b6eb2759..1fa8c737288ef 100644 --- a/Source/JavaScriptCore/runtime/JSObject.cpp +++ b/Source/JavaScriptCore/runtime/JSObject.cpp @@ -3692,13 +3692,6 @@ void JSObject::deallocateSparseIndexMap() arrayStorage->m_sparseMap.clear(); } -// Not inline, and with the parameters in the order of ProxyObject::putByIndexCommon()'s: the walk below then compiles -// to what it was, but for the function it calls. -static NEVER_INLINE bool putByIndexThroughProxy(ProxyObject* proxy, JSGlobalObject* globalObject, JSValue thisValue, unsigned i, JSValue value, bool shouldThrow) -{ - return proxy->putByIndexCommon(globalObject, receiverForPutByIndex(thisValue), i, value, shouldThrow); -} - bool JSObject::attemptToInterceptPutByIndexOnHoleForPrototype(JSGlobalObject* globalObject, JSValue thisValue, unsigned i, JSValue value, bool shouldThrow, bool& putResult) { VM& vm = globalObject->vm(); @@ -3734,7 +3727,7 @@ bool JSObject::attemptToInterceptPutByIndexOnHoleForPrototype(JSGlobalObject* gl if (current->type() == ProxyObjectType) { scope.release(); auto* proxy = uncheckedDowncast(current); - putResult = putByIndexThroughProxy(proxy, globalObject, thisValue, i, value, shouldThrow); + putResult = proxy->putByIndexCommon(globalObject, receiverForPutByIndex(thisValue), i, value, shouldThrow); return true; }