Skip to content

refactor(runtime): canary wrapper templates by kind - #594

Draft
edusperoni wants to merge 1 commit into
feat/native-member-guardfrom
refactor/canary-template-kinds
Draft

edusperoni wants to merge 1 commit into
feat/native-member-guardfrom
refactor/canary-template-kinds

Conversation

@edusperoni

@edusperoni edusperoni commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #593. Groundwork for #595 (plain canaries forward their JS properties); the handlers are the same for every kind at this commit.

What changes

  • Canary handlers are configured per CanaryKind:
    • kPlain: objects outside JS-extended classes (native class constructors under scope "all", and empty instances of such objects);
    • kExtended: instances of JS-extended classes.
  • Each wrapper takes its object's kind (WrapperLifetimeCanary::KindOf), computed once per wrap.
  • Whether a class is JS-extended is cached per class and thread; the superclass walk costs tens of nanoseconds a class, about 80 ns for NSObject and 410 ns for a five-deep class.
  • WrapperLifetime::Adopt checks the kind it's given in debug builds only.
  • ArgConverter keeps one empty-instance and one constructed-instance constructor per kind.

Behaviour delta

ArgConverter::ConstructObject now swaps this whenever the result's kind differs from the constructor template's kind. Before, it swapped only when a canary was built from a template without handlers. The new cases are:

  • a plain template with a JS-extended result;
  • the reverse.

Both are reached by constructing from a pointer (new NativeClass(handle)) or by an initializer that returns another object. new of a JS-extended subclass through a native base doesn't reach it: ClassConstructorCallback takes the class from the native constructor's metadata, not from new.target.

Both kinds install the same masking handlers here, so the replacement object behaves as this would have. The original this is untouched before the swap (no wrapper, registration or Instances entry), so nothing is built twice.

Testing

Full suite, 0 failures in canary/all, canary/extended and resurrecting/extended. The whole stack, with the next PR on top, also passes in rooted/all, with allowShadowingNativeMembers, and under ASan.

@coderabbitai

coderabbitai Bot commented Nov 21, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Canary handlers are now configured per CanaryKind: kPlain for objects
outside JS-extended classes (native class constructors under scope "all",
empty instances of such objects) and kExtended for instances of JS-extended
classes. Wrappers take the kind of their object (WrapperLifetimeCanary::
KindOf), computed once per wrap; whether a class is JS-extended is cached
per class and thread, since the superclass walk costs tens of nanoseconds a
class. Adopt checks the kind it is given in debug builds only.

ArgConverter::ConstructObject now gives a result whose kind differs from
its constructor's template a new object of the result's kind, returned in
place of `this`. Before, it did so only for a canary built from a template
without handlers. The new cases are a plain template with a JS-extended
result and the reverse, which constructing from a pointer
(`new NativeClass(handle)`) or an initializer that returns another object
reaches. `new` of a JS-extended subclass through a native base does not:
ClassConstructorCallback takes the class from the native constructor's
metadata, not from new.target.

Both kinds install the same masking handlers at this commit, so the
replacement object behaves as `this` would have; a later change gives
plain canaries different handlers.
@edusperoni
edusperoni force-pushed the refactor/canary-template-kinds branch from 0cf7d71 to 21a009d Compare November 21, 2026 23:31

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant