Skip to content

[JSC] A store to an index of a global object hands globalThis to a setter and to a Proxy - #1049

Open
robobun wants to merge 3 commits into
mainfrom
robobun/451f540b/global-object-put-by-index-receiver
Open

robobun wants to merge 3 commits into
mainfrom
robobun/451f540b/global-object-put-by-index-receiver

Conversation

@robobun

@robobun robobun commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

  • globalThis[0] = v calls a setter with the wrong this: undefined in a strict setter, the setter's own global object in a sloppy one. A Proxy's set trap gets undefined. A name key is right.
  • On Bun's main an 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 the JSGlobalObject to script (runtime/SparseArrayValueMap.cpp:263, runtime/JSObject.cpp:3730, :988).

Fix

  • receiverForPutByIndex() returns globalThis() for a JSGlobalObject. The three places above call it.
  • The two are one object to script, and only the proxy can be a this.
  • Verified: JSTests/stress/bun-global-object-put-by-index-receiver.js. 17 of 17 run-jsc-stress-tests modes fail on the parent and pass here.

Background

  • globalThis is a JSGlobalProxy in front of its JSGlobalObject, which is also the global scope. JSValue::toThis() reads a scope as no this.
  • Upstream made this swap for the setter in 284931@main and reverted that commit (Notes).
  • Considered: a receiver parameter on the hook (about 50 files) and the global object's own putByIndex. Both still need this swap.

Downsides

  • A store that calls an index setter runs 10 more instructions, one that reaches a Proxy 5 more. Other stores run the same number (lto-aot).
  • .text of jsc grows by 192 bytes on x64 and 256 on arm64.
  • Still wrong (Notes): x = 1 as a bare identifier, and Object.create(globalThis)[0] = v.
Notes

Repro, in the jsc shell.

let seen = "not called";
Object.defineProperty(Object.prototype, 0, { set(v) { "use strict"; seen = this; }, configurable: true });
globalThis[0] = 1;
print(seen === globalThis); // false: seen is undefined

Node.js v26.3.0 prints true for the same program with console.log.

Why it matters now. Bun's node:vm swaps the receiver for a name key itself: "Setters and Proxy traps must see globalThis" (src/jsc/bindings/NodeVM.cpp:1162 and :1275, oven-sh/bun#44833). The same pull request put the prototype chain of a node:vm global on the global object, where the engine's index store walks it. jsdom 27.0.0 and later, with runScripts set, makes its window from vm.constants.DONT_CONTEXTIFY and puts a Proxy on that chain. Its set trap refuses a receiver that is no object. jsdom 31.0.1 with runScripts: "dangerously":

window[0] = v from strict code window[2] = v in a sloppy page script Reflect.set(window, 7, v)
Node.js v26.3.0 stored stored true
Bun 1.4.4 stored stored true
Bun 1.4.5-canary.1+b6e7b9cdc TypeError: Proxy object's 'set' trap returned falsy value for property '0' not stored, no error false
Bun with this change stored stored true

oven-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 sees globalThis by accident. An ES module, eval, new Function and a node:vm script show the undefined.

How the receiver is lost. JSGlobalProxy::putByIndex forwards 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 from JSObject::putInlineForJSObject, which drops slot.thisValue() when the receiver is the global proxy of the object. JSObject::notifyPresenceOfIndexedAccessors already maps a JSGlobalObject to its globalThis() the same way (WebKit/WebKit@363afc5630).

Where a store starts. A native can hand the JSGlobalObject itself to script. $vm.createGlobalObject() does, and so does Bun for the target of 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() in runtime/JSGlobalProxy.h), and a callee cannot take the JSGlobalObject as this. A store by name through that reference still hands the JSGlobalObject on. This change does not touch JSObject::put.

Upstream. 284931@main (WebKit/WebKit@ee167b8fe4fd, bug 225397, "Don't use JSScope instances as this value when calling functions") made this swap in SparseArrayEntry::put as wrapGlobalObject(), 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 in JSObject.cpp: its ProxyObject::performPut turns the scope into undefined today, and WebKit#35527 removes that. After a sync that brings WebKit#35527, SparseArrayEntry::put needs one swap, and both calls in JSObject.cpp stay. The comment of receiverForPutByIndex() and JSTests/BUN-TEST-DIFFERENCES.md say so.

What the test holds. Nine ways to store (global[key] = v in sloppy and strict code, a string key, Reflect.set with and without a receiver, Object.assign, a destructuring target, a for-of target, this[key] = v in global code), keys 0, 7, 1000000000 and 4294967294, a strict and a sloppy setter of the same realm and of another realm, on Object.prototype and on the global object itself, with exactly one call of the setter for each store. The same stores through the JSGlobalObject that $vm.createGlobalObject() returns, with setters and with a Proxy. Also six functions of Array.prototype called on the global object, a JSONP program, loops for the tiers, a Proxy with and without a set trap 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-tests on the one file, 17 modes. The jsc of this repository's CI for the parent commit de04322 (linux-amd64-lto-aot) fails all 17. The jsc of this pull request (linux-amd64-lto-aot) passes all 17, and its linux-amd64-debug-asan-aot build passes the 15 modes that --debug --asan runs. The seven test lanes of CI pass.

Counts of the wrong call. gdb breakpoints at the entry of GetterSetter::callSetter and ProxyObject::putByIndexCommon, with the condition that thisValue is a cell of type GlobalObjectType, over the test with its assertions collected, on the debug-asan-aot jsc of CI. Parent: 30908 of 30911 setter calls and 42 Proxy hand-offs get the JSGlobalObject. This pull request: 0 of 30909 and 0. In a debug Bun (index stores in node:vm contexts, 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=0 of CI, parent against this pull request, user instructions counted with perf_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.

store lto-aot lto
a[i] = i on a new array 258.3 to 258.3 258.3 to 258.3
o[1e9 + (i & 1023)] = i 753.6 to 753.6 756.6 to 755.6
globalThis[i & 7] = i 532.0 to 532.0 533.0 to 534.0
globalThis[1e9 + (i & 7)] = i 786.0 to 786.0 789.0 to 788.0
a hole of an array, with a setter for another index on Array.prototype 920.0 to 920.0 915.0 to 914.0
o[keys[i & 7]] = i with the keys "0" to "7" 633.0 to 633.0 629.0 to 630.0
a setter that is the object's own 1007.0 to 1017.0 1007.0 to 1016.0
a setter on the prototype 968.0 to 977.0 962.0 to 971.0
a Proxy with a set trap on the prototype chain 1180.0 to 1185.0 1171.0 to 1176.0
globalThis[0] = i with a setter on Object.prototype 995.0 to 1007.0 989.0 to 1001.0
a global object with a Proxy with a set trap on its prototype chain 1240.0 to 1249.0 1228.0 to 1237.0

In the lto lane three stores that reach no setter take one instruction less and two take one more. JSObject::putByIndex spills shouldThrow at another place there. The check of the type is in the arm for index 4294967295 in both lanes.

Code shape. jsc of CI, parent against this pull request, instructions in the bytes of a function's symbol.

function x64 lto-aot arm64 lto-aot
SparseArrayValueMap::putEntry 83 to 83 143 to 143
SparseArrayEntry::put 53 to 53 91 to 91
JSObject::attemptToInterceptPutByIndexOnHole 69 to 69 64 to 64
JSObject::attemptToInterceptPutByIndexOnHoleForPrototype 319 to 332 311 to 324
JSObject::putByIndexBeyondVectorLengthWithArrayStorage 235 to 235 214 to 214
JSGlobalProxy::putByIndex 9 to 9 12 to 12
JSObject::putByIndex 754 to 762 701 to 712
callSetterOfIndex (new) 18, 65 bytes 19, 76 bytes

putEntry has SparseArrayEntry::put inlined before and after, in both. With receiverForPutByIndex() called in SparseArrayEntry::put itself, clang 23 at -O3 does not inline it into putEntry: 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. .text of jsc: 40838755 to 40838947 bytes (linux-amd64-lto-aot), 34093635 to 34093859 (linux-amd64-lto), 41009216 to 41009472 (linux-arm64-lto-aot). .rodata is 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.

  • A bare identifier: x = 1 and x pass the scope as the receiver from put_to_scope and get_from_scope, so a strict accessor of x sees undefined. Bun corrects this for node:vm contexts in its own global object class, and not for the main realm.
  • Object.create(globalThis)[0] = v does not find an index setter or a read-only index that is the global object's own: attemptToInterceptPutByIndexOnHoleForPrototype does not look into the target of a JSGlobalProxy. For a DONT_CONTEXTIFY global of node:vm Bun 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.
  • A setter of a sparse map entry that is the object's own runs inside DeferTerminationForAWhile (JSObject::putByIndexBeyondVectorLengthWithArrayStorage), before and after. A node:vm timeout or worker.terminate() does not stop a script that loops in such a setter. Node.js stops it. This is so for every object.
  • Two JSGlobalProxy objects for one JSGlobalObject ($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 main pinned 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. main here has one more commit since (#1047). It touches none of the files of this change.

…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().
@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Preview build of 88d4fd5: autobuild-preview-pr-1049-88d4fd5b

…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-.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

@coderabbitai

coderabbitai Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Indexed 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.

Changes

Global Indexed Store Receivers

Layer / File(s) Summary
Receiver helper and contract
Source/JavaScriptCore/runtime/JSGlobalObject.h, Source/JavaScriptCore/runtime/JSObject.h
Adds receiverForPutByIndex() to return the global proxy when available, and documents its use for JSGlobalObject indexed stores.
Indexed-store receiver wiring
Source/JavaScriptCore/runtime/JSObject.cpp, Source/JavaScriptCore/runtime/SparseArrayValueMap.cpp
Uses the helper when initializing put slots for large indices, delegating indexed-hole interception to a proxy, and calling indexed accessor setters.
Receiver behavior tests and documentation
JSTests/stress/bun-global-object-put-by-index-receiver.js, JSTests/BUN-TEST-DIFFERENCES.md
Adds stress tests for receiver behavior across realms, store forms, property configurations, and proxy cases. Adds a test entry describing the behavior and relevant call sites.
Merge Risk: Minimal · up to 88d4f

Indexed stores on global objects now pass the global proxy as the receiver to setters and Proxy traps, instead of undefined or the raw global. Behavior for ordinary objects and for globals without a proxy is unchanged. The change is small, tested across store forms and realms, and I found no merge-blocking risk.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: an indexed store on a global object passes globalThis to a setter and to a Proxy. The raw summary and PR objectives confirm the change adds receiverForPutByIndex() and…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description is thorough and largely follows the WebKit template's spirit. It has a bug-level problem statement, a fix explanation, and a stress test reference with run results. The template requir…
Full details: Title check

Explanation

The title names the main change: an indexed store on a global object passes globalThis to a setter and to a Proxy. The raw summary and PR objectives confirm the change adds receiverForPutByIndex() and uses it at the sparse-map setter call, the Proxy hand-off, and the index-key store path. The title is specific and matches the changeset, and it is 88 characters, which is slightly long but readable.

Full details: Description check

Explanation

The description is thorough and largely follows the WebKit template's spirit. It has a bug-level problem statement, a fix explanation, and a stress test reference with run results. The template requires a bug link on Bugzilla, a 'Reviewed by' line, and a changed-files list in the WebKit ChangeLog-style commit format. The description does not link a Bugzilla bug or list changed files in the template format. It is written for a Bun fork PR, and the template's bug-tracking requirements do not map cleanly onto it. The core explanation is present and detailed, so the description is mostly complete.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

The configuration directory could not be loaded. This review used the available YAML and UI settings; directory instructions and checks were not evaluated.

Git: CodeRabbit could not clone the repository, so clone-backed analysis was skipped and this review may be incomplete. Verify repository clone access, such as SSH credentials, before requesting another full review. If clone access is intentionally unavailable, use path_filters to narrow the review scope.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 5e7a3cef-c3da-4a76-b24e-eedf765c8a1a
Commits

Reviewing files that changed from the base of the PR and between de04322 and 88d4fd5.

Files selected for processing: 6

  • JSTests/BUN-TEST-DIFFERENCES.md
  • JSTests/stress/bun-global-object-put-by-index-receiver.js
  • Source/JavaScriptCore/runtime/JSGlobalObject.h
  • Source/JavaScriptCore/runtime/JSObject.cpp
  • Source/JavaScriptCore/runtime/JSObject.h
  • Source/JavaScriptCore/runtime/SparseArrayValueMap.cpp

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 0 remain after this review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant