Repository navigation
Conversation
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.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When a class extends a constructor interceptor, its
super(...)call ends up increateCtorInterceptor, which always rannew ctorFunc(...). The function interceptor reaches that code viaapply, sonew.targetis lost. The derived instance was therefore replaced by a plain base-class instance:instanceofwas false and the subclass methods were missing.This matters in practice because
hyperion-core/src/IGlobalThisinterceptsglobalThis.Promise(used by hyperion-flowlet and hyperion-autologging), andhyperion-dominstalls the same interceptor onXMLHttpRequest(that path was not tested separately). Page code likeclass MyPromise extends Promise {}breaks once hyperion is loaded. For example, withp-lazy(class PLazy extends Promise),await new PLazy(fn)resolves toundefinedandfnnever runs.The fix: when the interceptor is called with a
thisthat is an instance of the original constructor but has a different prototype, the call is a subclasssuper(...). In that case the object is built withReflect.construct(ctorFunc, arguments, this.constructor), so it keeps the derived prototype andnew.target, as it would without interception. The common case, constructing the interceptor directly, still goes through the existing code. Calls withoutnewkeep their current behavior.Test plan
test subclass of intercepted constructorinConstructorInterceptor.test.ts. It checksclass D extends interceptor,Reflect.construct(interceptor, args, D), that hooks still see the args and theDinstance, and that direct construction is unchanged.test subclass of intercepted PromiseinIGlobalThis.test.ts, which subclasses the intercepted globalPromise.B/Promiseinstead ofD/MyPromise. They pass with it.npx jestinpackages/hyperion-core: 9 suites, 44 tests pass.packages/hyperion-dom(which includes the XMLHttpRequest constructor interception): 9 suites, 14 tests pass.tsc --noEmitin 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).new Interceptor(x): 23.3 (23-24) → 23.3 (22-24).new Promise43.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.FunctionInterceptor, which this change doesn't touch: 6.6 → 5.7.Reflect.constructwhen used as a base class takes about 300-380 ns in the same Node version.I also tried doing the prototype comparison first and
instanceofonly 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.