Repository navigation
Conversation
…tter 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().
|
Preview build of 88d4fd5: |
…h 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.
…ts 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#35527) and say what a sync has to keep. The test is the fork's own, so its name begins with bun-.
|
No actionable comments were generated in the recent review. WalkthroughIndexed stores on global objects now use the global proxy as the receiver when it exists. The change applies this receiver handling to indexed-store paths and accessor setters. A new stress test checks behavior across store forms, realms, and property configurations. ChangesGlobal Indexed Store Receivers
Merge Risk:
|
Problem
globalThis[0] = vcalls a setter with the wrongthis:undefinedin a strict setter, the setter's own global object in a sloppy one. A Proxy'ssettrap getsundefined. A name key is right.mainan index store to a jsdom window fails:TypeError: Proxy object's 'set' trap returned falsy value for property '0'. No release has that regression.JSObject::putByIndex()takes no receiver. The code behind it hands theJSGlobalObjectto script (runtime/SparseArrayValueMap.cpp:263,runtime/JSObject.cpp:3730,:988).Fix
receiverForPutByIndex()returnsglobalThis()for aJSGlobalObject. The three places above call it.this.JSTests/stress/bun-global-object-put-by-index-receiver.js. 17 of 17run-jsc-stress-testsmodes fail on the parent and pass here.Background
globalThisis aJSGlobalProxyin front of itsJSGlobalObject, which is also the global scope.JSValue::toThis()reads a scope as nothis.putByIndex. Both still need this swap.Downsides
lto-aot)..textofjscgrows by 192 bytes on x64 and 256 on arm64.x = 1as a bare identifier, andObject.create(globalThis)[0] = v.Notes
Repro, in the
jscshell.Node.js v26.3.0 prints
truefor the same program withconsole.log.Why it matters now. Bun's
node:vmswaps the receiver for a name key itself: "Setters and Proxy traps must see globalThis" (src/jsc/bindings/NodeVM.cpp:1162and:1275, oven-sh/bun#44833). The same pull request put the prototype chain of anode:vmglobal on the global object, where the engine's index store walks it. jsdom 27.0.0 and later, withrunScriptsset, makes its window fromvm.constants.DONT_CONTEXTIFYand puts a Proxy on that chain. Itssettrap refuses a receiver that is no object. jsdom 31.0.1 withrunScripts: "dangerously":window[0] = vfrom strict codewindow[2] = vin a sloppy page scriptReflect.set(window, 7, v)truetrueTypeError: Proxy object's 'set' trap returned falsy value for property '0'falsetrueoven-sh/bun#44833 is in no release. jsdom 27.0.0 gives the same table. jsdom 26.1.0 makes its window with
vm.createContext(window), and all four store with it. Nobody reported this.In a CommonJS file Bun drops the
"use strict"of a function (oven-sh/bun#14251), and a sloppy setter of the same realm then seesglobalThisby accident. An ES module,eval,new Functionand anode:vmscript show theundefined.How the receiver is lost.
JSGlobalProxy::putByIndexforwards to the hook of its target with the target as the cell. A store by name with an index key (Reflect.set(globalThis, 0, v),globalThis["0"] = v,Object.assign) reaches the same hook fromJSObject::putInlineForJSObject, which dropsslot.thisValue()when the receiver is the global proxy of the object.JSObject::notifyPresenceOfIndexedAccessorsalready maps aJSGlobalObjectto itsglobalThis()the same way (WebKit/WebKit@363afc5630).Where a store starts. A native can hand the
JSGlobalObjectitself to script.$vm.createGlobalObject()does, and so does Bun for thetargetof an event on the global object (oven-sh/bun#39102). An index store through that reference gets the proxy as well: the two are one object to script (isThisValueAltered()inruntime/JSGlobalProxy.h), and a callee cannot take theJSGlobalObjectasthis. A store by name through that reference still hands theJSGlobalObjecton. This change does not touchJSObject::put.Upstream. 284931@main (WebKit/WebKit@ee167b8fe4fd, bug 225397, "Don't use JSScope instances as
thisvalue when calling functions") made this swap inSparseArrayEntry::putaswrapGlobalObject(), with 102 other files. 284962@main (WebKit/WebKit@9bfbbb1fe8f0, bug 281214) reverted all of it a day later for a "sub-1% JS2 perf regression". WebKit#35527 lands it again and is open since 2024-10. That change alters the bytecode of every strict function and of every call from a scope. This one changes no bytecode and no JIT code. Upstream does not swap at the two places inJSObject.cpp: itsProxyObject::performPutturns the scope intoundefinedtoday, and WebKit#35527 removes that. After a sync that brings WebKit#35527,SparseArrayEntry::putneeds one swap, and both calls inJSObject.cppstay. The comment ofreceiverForPutByIndex()andJSTests/BUN-TEST-DIFFERENCES.mdsay so.What the test holds. Nine ways to store (
global[key] = vin sloppy and strict code, a string key,Reflect.setwith and without a receiver,Object.assign, a destructuring target, afor-oftarget,this[key] = vin global code), keys 0, 7, 1000000000 and 4294967294, a strict and a sloppy setter of the same realm and of another realm, onObject.prototypeand on the global object itself, with exactly one call of the setter for each store. The same stores through theJSGlobalObjectthat$vm.createGlobalObject()returns, with setters and with a Proxy. Also six functions ofArray.prototypecalled on the global object, a JSONP program, loops for the tiers, a Proxy with and without asettrap on the prototype chain of a ShadowRealm's global object, and a global object that is not extensible. It also holds what does not change: a store that no setter takes makes a data property with all attributes, an own data property is in front of an inherited setter, a deleted setter takes no store, a getter without a setter and a read-only value refuse the store, and a plain object, an array and a number are still their own receivers.Test runs.
run-jsc-stress-testson the one file, 17 modes. Thejscof this repository's CI for the parent commit de04322 (linux-amd64-lto-aot) fails all 17. Thejscof this pull request (linux-amd64-lto-aot) passes all 17, and itslinux-amd64-debug-asan-aotbuild passes the 15 modes that--debug --asanruns. The seven test lanes of CI pass.Counts of the wrong call. gdb breakpoints at the entry of
GetterSetter::callSetterandProxyObject::putByIndexCommon, with the condition thatthisValueis a cell of typeGlobalObjectType, over the test with its assertions collected, on thedebug-asan-aotjscof CI. Parent: 30908 of 30911 setter calls and 42 Proxy hand-offs get theJSGlobalObject. This pull request: 0 of 30909 and 0. In a debug Bun (index stores innode:vmcontexts, a window shaped like jsdom's, the main realm, a Worker, a ShadowRealm, N-API): 40073 setter calls and 18 Proxy hand-offs before, 0 and 0 after.Instructions per store.
jsc --useJIT=0of CI, parent against this pull request, user instructions counted withperf_event_open, one loop of N stores, (I(2N) - I(N)) / N at N = 1000000, the lowest of three runs each. Three runs differ by at most 0.05 instructions per store.lto-aotltoa[i] = ion a new arrayo[1e9 + (i & 1023)] = iglobalThis[i & 7] = iglobalThis[1e9 + (i & 7)] = iArray.prototypeo[keys[i & 7]] = iwith the keys"0"to"7"settrap on the prototype chainglobalThis[0] = iwith a setter onObject.prototypesettrap on its prototype chainIn the
ltolane three stores that reach no setter take one instruction less and two take one more.JSObject::putByIndexspillsshouldThrowat another place there. The check of the type is in the arm for index 4294967295 in both lanes.Code shape.
jscof CI, parent against this pull request, instructions in the bytes of a function's symbol.lto-aotlto-aotSparseArrayValueMap::putEntrySparseArrayEntry::putJSObject::attemptToInterceptPutByIndexOnHoleJSObject::attemptToInterceptPutByIndexOnHoleForPrototypeJSObject::putByIndexBeyondVectorLengthWithArrayStorageJSGlobalProxy::putByIndexJSObject::putByIndexcallSetterOfIndex(new)putEntryhasSparseArrayEntry::putinlined before and after, in both. WithreceiverForPutByIndex()called inSparseArrayEntry::putitself, clang 23 at-O3does not inline it intoputEntry: cost 255, threshold 250 (-Rpass-missed=inline, the file compiled alone on x64). The Proxy hand-off is in place: out of line it cost a store that reaches a Proxy 5 more instructions and saved none elsewhere.Size.
.textofjsc: 40838755 to 40838947 bytes (linux-amd64-lto-aot), 34093635 to 34093859 (linux-amd64-lto), 41009216 to 41009472 (linux-arm64-lto-aot)..rodatais 8704 bytes larger in each. The compressed ICU data accounts for it (icudt79_dat, 12123152 to 12131872 bytes in all three). This change adds no data.Not changed.
x = 1andxpass the scope as the receiver fromput_to_scopeandget_from_scope, so a strict accessor ofxseesundefined. Bun corrects this fornode:vmcontexts in its own global object class, and not for the main realm.Object.create(globalThis)[0] = vdoes not find an index setter or a read-only index that is the global object's own:attemptToInterceptPutByIndexOnHoleForPrototypedoes not look into the target of aJSGlobalProxy. For aDONT_CONTEXTIFYglobal ofnode:vmBun 1.4.3 called that setter and Bun 1.4.4 does not. The remedy is another rule (where the walk looks, and not whom it hands on), so it is not in this change.DeferTerminationForAWhile(JSObject::putByIndexBeyondVectorLengthWithArrayStorage), before and after. Anode:vmtimeout orworker.terminate()does not stop a script that loops in such a setter. Node.js stops it. This is so for every object.JSGlobalProxyobjects for oneJSGlobalObject($vm.createGlobalProxy,JSGlobalObject::resetPrototype): an index store through the second proxy gets the first one. A store by name gets the proxy it was made through.Base of the branch. de04322, the commit that Bun's
mainpinned when the branch was cut, so that a build of Bun could use the preview build of this pull request. oven-sh/bun#48892 pins that build and has Bun's tests.mainhere has one more commit since (#1047). It touches none of the files of this change.