Skip to content

Keep the derived class when subclassing an intercepted constructor - #312

Open
0z5a wants to merge 1 commit into
meta:mainfrom
0z5a:fix/ctor-interceptor-subclass
Open

0z5a wants to merge 1 commit into
meta:mainfrom
0z5a:fix/ctor-interceptor-subclass

Conversation

@0z5a

@0z5a 0z5a commented Nov 16, 2026

Copy link
Copy Markdown

Summary

When a class extends a constructor interceptor, its super(...) call ends up in createCtorInterceptor, which always ran new ctorFunc(...). The function interceptor reaches that code via apply, so new.target is lost. The derived instance was therefore replaced by a plain base-class instance: instanceof was false and the subclass methods were missing.

This matters in practice because hyperion-core/src/IGlobalThis intercepts globalThis.Promise (used by hyperion-flowlet and hyperion-autologging), and hyperion-dom installs the same interceptor on XMLHttpRequest (that path was not tested separately). Page code like class MyPromise extends Promise {} breaks once hyperion is loaded. For example, with p-lazy (class PLazy extends Promise), await new PLazy(fn) resolves to undefined and fn never runs.

The fix: when the interceptor is called with a this that is an instance of the original constructor but has a different prototype, the call is a subclass super(...). In that case the object is built with Reflect.construct(ctorFunc, arguments, this.constructor), so it keeps the derived prototype and new.target, as it would without interception. The common case, constructing the interceptor directly, still goes through the existing code. Calls without new keep their current behavior.

Test plan

  • New test subclass of intercepted constructor in ConstructorInterceptor.test.ts. It checks class D extends interceptor, Reflect.construct(interceptor, args, D), that hooks still see the args and the D instance, and that direct construction is unchanged.
  • New test subclass of intercepted Promise in IGlobalThis.test.ts, which subclasses the intercepted global Promise.
  • Both new tests fail without the change: the received constructor is B / Promise instead of D / MyPromise. They pass with it.
  • npx jest in packages/hyperion-core: 9 suites, 44 tests pass. packages/hyperion-dom (which includes the XMLHttpRequest constructor interception): 9 suites, 14 tests pass. tsc --noEmit in hyperion-core (src) is clean.

Perf (Node 24, 2M constructions per run, 15 interleaved runs each, medians in ns/op with IQR, main → this change):

  • new Promise(...) through the intercepted global: 21.1 (21-21) → 21.1 (21-22).
  • Direct new Interceptor(x): 23.3 (23-24) → 23.3 (22-24).
  • With one after-call observer attached: new Promise 43.9 (44-44) → 49.1 (48-50), +12%; direct 30.6 (30-31) → 38.9 (38-40), +27%. So when a hook is attached, the extra check costs about 5-8 ns per construction.
  • Method calls through FunctionInterceptor, which this change doesn't touch: 6.6 → 5.7.
  • The subclass path now takes about 490 ns instead of about 23 ns, but before it returned the wrong object. Without hyperion, a plain function wrapper that forwards to Reflect.construct when used as a base class takes about 300-380 ns in the same Node version.

I also tried doing the prototype comparison first and instanceof only when it differs. It was slower in every case (direct 33.7, with a hook 56.2 / 44.4), so I kept the version above.

This change was drafted with Claude Code (claude-opus-5-5); the test results above are from the runs described.

A class that extends a constructor interceptor reaches createCtorInterceptor from its super() call through apply, so new.target is lost and new ctorFunc() replaced the derived instance with a base instance. Since IGlobalThis intercepts globalThis.Promise, class X extends Promise broke once hyperion was loaded (e.g. p-lazy resolved to undefined). Build the object with Reflect.construct(ctorFunc, arguments, this.constructor) for subclass super() calls.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 16, 2026

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

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant