Emit unowned-this closures for synchronous @JS bindings#20
Merged
Conversation
Synchronous `@JS` functions and property accessors never read the JS `this` (the receiver is the module's real `self`), yet the synthesized `setProperty` closure bound `this` as an owning `JavaScriptValue`, allocating it and forming/destroying its `weak` runtime reference on every call. They now bind through the unowned-`this` `setProperty` overload, typing the closure's first parameter `borrowing JavaScriptUnownedValue` to select it. The parameter list is parenthesized and fully typed because Swift rejects a type annotation on a shorthand closure parameter. Async functions keep the owning-`this` overload: there is no unowned-`this` async variant and the buffer escapes into the task.
The macro now emits sync `@JS` bindings with a fully-typed closure parameter list (`(this: borrowing JavaScriptUnownedValue, arguments: consuming JavaScriptValuesBuffer) in`) to select the unowned-`this` `setProperty` overload. The fixture expectations still carried the old `this, arguments in` shorthand, so every sync-binding expansion test was failing. Update them to match. Async fixtures keep the shorthand and the owning-`this` overload, so they are left untouched.
58619e8 to
0edc92a
Compare
Kudo
approved these changes
Jun 17, 2026
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.
Why
Synchronous
@JSfunctions and property accessors never read the JSthis— the receiver is the module's realself, and the synthesized body callsself.<name>(...)directly. Yet the generatedsetPropertyclosure boundthisas an owningJavaScriptValue, which allocates the value and forms/destroys itsweakruntime reference on every call. Profiling the no-op@JScall floor showed this per-call churn was a meaningful slice of the Swift-side cost.How
Sync
@JSfunctions and propertyget/setaccessors now bind through the unowned-thissetPropertyoverload added in expo-modules-jsi, which handsthisto the closure as a borrowedJavaScriptUnownedValueinstead of an owningJavaScriptValue. The closure's first parameter is typedborrowing JavaScriptUnownedValueso overload resolution selects that (otherwise@_disfavoredOverload) overload. Because Swift rejects a type annotation on a shorthand{ [capture] name, name in }parameter, the synthesized parameter list is now parenthesized and fully typed.Async functions are unchanged: they keep the untyped shorthand and the owning-
thisoverload, since there is no unowned-thisasync variant and the arguments buffer escapes into the task anyway.Requires the unowned-
thiscreateFunction/setPropertyoverloads in expo-modules-jsi: expo/expo#46949.Test Plan
Existing macro expansion tests pass. End-to-end, built locally and verified against the Modules Core benchmarks: the synthesized
@JShost-function path drops the per-call owning-thisallocation andweak-runtime traffic, improving the no-op call floor ~28% and the argument cases ~15–18% on the iPhone simulator, and making@JSthe fastest host-function tier (overtaking@OptimizedFunction).