Skip to content

Commit a1c1f51

Browse files
fix(core): bind native window methods in the scoped sub-composition proxy (#3378)
The scoped window proxy handed native methods back unbound, so `this` at call time was the Proxy and Chrome rejected it with "Illegal invocation". That broke window.addEventListener, setTimeout, matchMedia and getComputedStyle inside every sub-composition, including the window.addEventListener("hf-seek", ...) form the Three.js and TypeGPU adapters document. The sibling document and gsap proxies in this file already bound; this one was the outlier. Bind only non-constructors: Function.prototype.bind drops static members, so binding a class exposed on window would silently strip its statics. Built-in methods have no .prototype, classes and constructors do. Closes #3376
1 parent 8b67bb6 commit a1c1f51

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

‎packages/core/src/compiler/compositionScoping.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,40 @@ body { margin: 0; }
169169
expect(fakeWindow.__captured).toEqual({ title: "Pro", price: "$29" });
170170
});
171171

172+
it("hands native window methods back bound to the real window", () => {
173+
// Regression: the scoped `window` proxy returned natives UNBOUND, so `this`
174+
// at call time was the Proxy itself and Chrome rejected it with
175+
// "Illegal invocation" — breaking window.addEventListener, setTimeout,
176+
// matchMedia, getComputedStyle and requestAnimationFrame in every
177+
// sub-composition, including the window.addEventListener("hf-seek", ...)
178+
// form the Three.js and TypeGPU adapters document. The sibling document
179+
// and gsap proxies in this file always bound; this one did not.
180+
const { document } = parseHTML(`<div data-composition-id="scene"></div>`);
181+
const fakeWindow: Record<string, unknown> = { document, __timelines: {} };
182+
let boundToWindow = false;
183+
// Method shorthand, not `function () {}`: like a real native method it has
184+
// no `.prototype`, which is the property the proxy uses to tell a method
185+
// apart from a class. Stands in for the native brand check, which throws
186+
// when the method is invoked with anything else as `this`.
187+
const natives = {
188+
addEventListener(this: unknown) {
189+
if (this !== fakeWindow) throw new TypeError("Illegal invocation");
190+
boundToWindow = true;
191+
},
192+
};
193+
fakeWindow.addEventListener = natives.addEventListener;
194+
195+
const wrapped = wrapScopedCompositionScript(
196+
`window.addEventListener("hf-seek", function () {});`,
197+
"scene",
198+
);
199+
new Function("window", wrapped)(fakeWindow);
200+
201+
// Asserted on the call landing, not on throwing: the wrapper's error
202+
// boundary swallows the TypeError, which is why this shipped unnoticed.
203+
expect(boundToWindow).toBe(true);
204+
});
205+
172206
it("preserves non-getVariables members on window.__hyperframes (only getVariables is rescoped)", () => {
173207
const { document } = parseHTML(`<div data-composition-id="card-1"></div>`);
174208
let fitCalled = false;

‎packages/core/src/compiler/compositionScoping.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,22 @@ export function wrapScopedCompositionScript(
485485
// (__hfScopedHyperframes is a hoisted var assigned below, before any
486486
// sub-comp script -- the only code that reads this -- runs.)
487487
if (prop === "__hyperframes") return __hfScopedHyperframes;
488-
return Reflect.get(target, prop, target);
488+
// Native window methods must stay bound to the real window. Handed
489+
// back unbound, "this" at call time is this Proxy and Chrome rejects
490+
// it with "Illegal invocation", which broke window.addEventListener,
491+
// setTimeout, matchMedia and getComputedStyle inside every
492+
// sub-composition -- including the window.addEventListener("hf-seek",
493+
// ...) form the Three.js and TypeGPU adapters document. The sibling
494+
// document and gsap proxies here already bind.
495+
//
496+
// Only bind non-constructors. Function.prototype.bind drops static
497+
// members, so binding a class exposed on window (window.Texts and
498+
// friends) would silently strip its statics. Built-in methods have
499+
// no .prototype; classes and constructor functions do.
500+
var value = Reflect.get(target, prop, target);
501+
return typeof value === "function" && value.prototype === undefined
502+
? value.bind(target)
503+
: value;
489504
},
490505
set: function(target, prop, value, receiver) {
491506
if (prop === "__timelines") {

0 commit comments

Comments
 (0)