Skip to content

Upgrade WebKit: an index store to globalThis has globalThis as its receiver - #48892

Draft
robobun wants to merge 2 commits into
mainfrom
robobun/451f540b/index-stores-to-globalthis
Draft

robobun wants to merge 2 commits into
mainfrom
robobun/451f540b/index-stores-to-globalthis

Conversation

@robobun

@robobun robobun commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator

Draft: WEBKIT_VERSION is the preview build of oven-sh/WebKit#1049, for CI only. It goes when main's WebKit has the change.

Problem

  • globalThis[0] = v calls a setter with the wrong this: undefined in a strict setter. A Proxy's set trap gets undefined too. Node.js passes globalThis, as Bun does for a name key.
  • On main, an index store to a jsdom 27 window fails: TypeError: Proxy object's 'set' trap returned falsy value for property '0'. Bun 1.4.4 stores it. The cause, bun test: Vitest compatibility #44833, is in no release.
  • JavaScriptCore's JSObject::putByIndex takes no receiver and hands the JSGlobalObject to script.

Fix

Background

  • globalThis is a proxy in front of the global object, which is also the global scope. A callee reads a scope as no this.
  • bun test: Vitest compatibility #44833 gave name keys this rule in node:vm: "Setters and Proxy traps must see globalThis" (src/jsc/bindings/NodeVM.cpp:1162).
  • The engine PR lists the designs weighed.

Downsides

  • None in Bun's code. In the engine a store that calls an index setter runs 10 more instructions, and .text grows by 192 bytes.
  • Still different from Node.js (Notes): a bare identifier in the main realm, and Object.create(globalThis)[0] = v.
Notes

Repro.

import vm from "node:vm";
for (const key of ["name", 0]) {
  const sandbox = {};
  vm.createContext(sandbox);
  const g = vm.runInContext("this", sandbox);
  vm.runInContext(
    `var seen = "not called"; Object.defineProperty(Object.prototype, ${JSON.stringify(key)}, { set(v) { "use strict"; seen = this === globalThis ? "globalThis of the context" : typeof this; }, configurable: true })`,
    sandbox,
  );
  g[key] = 1;
  console.log(`[${key}] this in the setter: ${vm.runInContext("seen", sandbox)}`);
}

Node.js v26.3.0 prints globalThis of the context twice. Bun on main prints it for name and undefined for 0. With this pin Bun prints it twice.

jsdom. jsdom 27.0.0 and later, with runScripts set, makes its window from vm.constants.DONT_CONTEXTIFY and puts a Proxy on the window's prototype chain. The set trap of that Proxy refuses a receiver that is no object. #44833 put the prototype chain of a node:vm global on the global object, where the engine's index store walks it, and corrected the receiver for name keys. jsdom 27.0.0 and 31.0.1 with runScripts: "dangerously":

window[0] = v from a module 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
this branch stored stored true

With jsdom 26.1.0, which makes its window with vm.createContext(window), all four store. The repository's tests use jsdom 25.0.1, so the test here has a window shaped like that of jsdom 27.

The pin. autobuild-preview-pr-1049-88d4fd5b is WebKit de043228c2 plus oven-sh/WebKit#1049. main pinned de043228c2 when this branch was cut. Each time main moves its pin, this branch conflicts on that one line until the pin here is replaced. After oven-sh/WebKit#1049 merges, WEBKIT_VERSION has to name the build of the merge commit. If another PR pins a WebKit with the change first, the line in scripts/build/deps/webkit.ts drops out of this PR and the tests stay. The fix is in the pinned engine, outside src/: with this branch's pin and the src/ of main the new tests pass too. They fail on main as it is.

What the tests hold.

  • vm.test.ts, for a DONT_CONTEXTIFY context and for a contextified object: a strict setter and a sloppy setter made in the outer realm and a strict setter made in the context, on the context's Object.prototype, reached from inside the context (globalThis[0] = v, a string key, Reflect.set, Object.assign, strict code) and from outside. A Proxy on the prototype chain with a set trap, and one without a trap in front of a setter. An index setter that is the global's own (DONT_CONTEXTIFY), and a loop through the JIT tiers.
  • vm.test.ts with vm-global-index-stores-fixture.cjs: 35 ways to store to an index key of globalThis (assignment and compound assignment, destructuring, for-of and for-in targets, Reflect.set with four receivers, Object.assign, super[key], eight functions of Array.prototype, with, global code, a class field, Array.from), each with 4 things in the way (an accessor on Object.prototype, an accessor of globalThis, a Proxy with traps, a Proxy without traps in front of an accessor), in the main realm and in a DONT_CONTEXTIFY context. The test runs the file with Node.js and with Bun and compares the output. Node.js v22.8.0, v26.3.0 and v26.12.1 print the same 140 lines. Bun 1.4.5-canary.1 differs in 111 of them in each realm.
  • vm-proxy-in-global-prototype.test.ts: index stores on a window shaped like jsdom's land as own properties.
  • index-stores-to-globalthis.test.ts: the main realm, a Worker and a ShadowRealm, also through the target of an event on globalThis.
  • napi.test.ts and v8.test.ts: napi_set_element, napi_set_property, napi_set_named_property and v8::Object::Set on the global object, for index 0 and for 4294967295. 4294967295 is no array index: script stores it by name, and only napi_set_element and Object::Set(context, index, value) reach the arm of the engine's index store for it. The output is compared with Node.js.
  • The two tests named "in every JIT tier" (this one and its neighbour for name keys) now set BUN_JSC_thresholdForFTLOptimizeAfterWarmUp=1000. Without it their loops end in the DFG.
  • The expected values are what Node.js v26.3.0 prints for the same programs (ShadowRealm excepted, which Node.js does not have).

Counts of the wrong call. gdb breakpoints at the entry of GetterSetter::callSetter and ProxyObject::putByIndexCommon, with the condition that thisValue is the global object itself, on debug builds running the programs behind these tests (node:vm contexts, a window shaped like jsdom's, the main realm with a Worker and a ShadowRealm, N-API): 40073 setter calls and 18 Proxy hand-offs on main, 0 of 40079 setter calls and 0 hand-offs on this branch.

Cost. Measured on the jsc of the engine's CI, parent against oven-sh/WebKit#1049, --useJIT=0, user instructions per store of a loop: six kinds of index store that reach no setter (arrays, sparse objects, globalThis, string keys) run the same number of instructions in the lto-aot build. A store that calls an index setter runs 10 more (1007 to 1017). One that reaches a Proxy's set trap runs 5 more (1180 to 1185). On a global object these are 12 and 9 more. .text of jsc grows by 192 bytes on x64 and 256 on arm64. A release Bun was not built for this.

Leftovers. This change causes none of them.

Self-review. Two of its 16 points are not taken.

Local runs. Bun 1.4.5-canary.1+b6e7b9cdc, a release build of main, fails all 15 new tests. bun bd test of the changed files with this pin: index-stores-to-globalthis.test.ts 2 pass. vm-proxy-in-global-prototype.test.ts 16 pass, 1 skip. vm.test.ts 377 pass and 5 fail: the 5 are time limits of 5 s that a debug build of main exceeds too (the four "reads the same in every tier" tests and the test with a 2^27-character string). napi.test.ts 225 pass and 1 fail ("should crash on duplicate hooks", a time limit, also on main). The Object tests of v8.test.ts: 8 pass. The debug profile of main compiles only with two more includes in BunPlugin.cpp (#48722). The build had them. This branch does not.


no test proof · iteration 0 · platform-specific test(s) that do not run on this machine, deferring to CI, which covers all platforms: test/v8/v8.test.ts, test/napi/napi.test.ts, test/js/node/vm/vm.test.ts

… receiver

A setter for the index on the prototype chain of the global object, or on the
global object itself, gets globalThis as `this`. A Proxy there gets it as the
receiver of its `set` trap. The tests cover node:vm contexts, the main realm,
a Worker, a ShadowRealm, napi_set_element and v8::Object::Set, and a matrix of
35 ways to store whose output is compared with Node.js.

The two tests named "in every JIT tier" lower the FTL threshold, so that their
loops reach the FTL.
@robobun

robobun commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator Author

Status: draft. It waits for oven-sh/WebKit#1049, which CI has built and tested. After that merges, WEBKIT_VERSION moves to the build of the merge commit. main moved its WebKit pin after this branch was cut, so the branch conflicts on that one line. The pin here stays as it is until the merge: it is the build that the CI run below tested.

How it was reproduced. The program under "Repro" in the PR body prints undefined for index 0 on Bun 1.4.5-canary.1+b6e7b9cdc and globalThis of the context on Node.js v26.3.0. With jsdom 27.0.0 and runScripts: "dangerously", window[0] = "zero" throws TypeError: Proxy object's 'set' trap returned falsy value for property '0' on the same canary. Node.js and Bun 1.4.4 store the value. The 15 new tests fail on that canary and pass on this branch.

CI, build 141327. Every job that ran passed. The changed test files pass on each lane that runs them: Debian and Ubuntu (x64, aarch64, ASAN), Alpine, Windows (x64, aarch64) and macOS aarch64. v8.test.ts does not run on Alpine. Three tests that this PR does not touch failed once and passed on the retry: test/aot/test-runs-in-folders-8.test.ts, test/aot/test-runs-in-folders-18.test.ts and test/js/web/fetch/fetch-http3-client.test.ts. The four macOS x64 test jobs have not found an agent yet.

PR: #48892

@robobun

robobun commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 11:10 PM PT - Nov 13th, 2026

✅ @robobun, your commit 4930eb5bbf3db5feb529a7c97844ea63c8e7afde passed in Build #141327! 🎉


🧪   To try this PR locally:

bunx bun-pr 48892

That installs a local version of the PR into your bun-48892 executable, so you can run:

bun-48892 --bun

This branch has not been deployed

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant