-
Notifications
You must be signed in to change notification settings - Fork 549
[RGen] Generate the post conversion calls for the native invoker. #23019
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RGen] Generate the post conversion calls for the native invoker. #23019
Conversation
The changes will get the native object representations (string -> NSString) so that we can call the native invoker.
Creates any call that has to be executed after the call to the native invoker, in most of the cases we need to add a GC.KeepAlive to ensure that the handle is not removed by the GC while it is in used by the native code.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds post-invoke GC.KeepAlive generation for trampoline parameters and updates existing property emitters to use the new helper.
- Introduces
GetTrampolinePostNativeInvokeArgumentConversionsto emitGC.KeepAlivecalls for managed-to-native parameters. - Adds a
KeepAlivehelper inBindingSyntaxFactory.Runtimeand wires it throughKnownTypesandClassEmitter. - Updates all expected-property-test files to fully qualify
global::System.GC.KeepAlive.
Reviewed Changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/rgen/.../BindingSyntaxFactoryTrampolineTests.cs | New data-driven tests for post-native-invoke GC.KeepAlive logic |
| tests/rgen/.../tvOSExpectedPropertyTests.cs (and other platform variants) | Updated expected outputs to use global::System.GC.KeepAlive |
| src/rgen/.../Emitters/ClassEmitter.cs | Replaced inline GC.KeepAlive(this) with the KeepAlive helper |
| src/rgen/.../Emitters/BindingSyntaxFactory.Trampoline.cs | Added GetTrampolinePostNativeInvokeArgumentConversions switch |
| src/rgen/.../Emitters/BindingSyntaxFactory.Runtime.cs | Added KeepAlive(string) invocation helper |
| src/rgen/.../Emitters/BindingSyntaxFactory.KnownTypes.cs | Introduced a GC TypeSyntax reference for code generation |
Comments suppressed due to low confidence (1)
tests/rgen/Microsoft.Macios.Generator.Tests/Emitters/BindingSyntaxFactoryTrampolineTests.cs:4627
- Add a test case for
IsProtocolparameters to ensure thatGetTrampolinePostNativeInvokeArgumentConversionscorrectly emits aGC.KeepAlivecall for protocol-bound objects.
}
| {ExpressionStatement (invocations.Getter.SendSuper)} | ||
| }} | ||
| GC.KeepAlive (this); | ||
| {KeepAlive ("this")}; |
Copilot
AI
Jun 10, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Consider wrapping the KeepAlive invocation in an ExpressionStatement (e.g., ExpressionStatement(KeepAlive("this"))) instead of inserting a raw invocation with a semicolon. This will align with how other generated statements are formatted and reuse the existing helper.
| {KeepAlive ("this")}; | |
| {ExpressionStatement(KeepAlive("this"))}; |
| { | ||
| // decide the type of conversion we need to do based on the type of the parameter | ||
| #pragma warning disable format | ||
| return parameter.Type switch { |
Copilot
AI
Jun 10, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Several cases emit identical KeepAlive calls for different types (e.g., string arrays, NSObject/INativeObject arrays). You might consolidate these patterns—group array element handling or merge the NSObject/INativeObject paths—to reduce duplication and simplify the switch.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
| Nomenclator.GetNameForVariableType (parameter.Name, Nomenclator.VariableType.BindFrom)! | ||
| ))], | ||
|
|
||
| // boolean, convert it to byte |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment seems wrong
✅ [CI Build #9946845] Build passed (Build packages) ✅Pipeline on Agent |
✅ [PR Build #9946845] Build passed (Detect API changes) ✅Pipeline on Agent |
✅ API diff for current PR / commit.NET ( No breaking changes )✅ API diff vs stable.NET ( No breaking changes )ℹ️ Generator diffGenerator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes) Pipeline on Agent |
✅ [CI Build #9946845] Build passed (Build macOS tests) ✅Pipeline on Agent |
💻 [CI Build #9946845] Tests on macOS X64 - Mac Sonoma (14) passed 💻✅ All tests on macOS X64 - Mac Sonoma (14) passed. Pipeline on Agent |
💻 [CI Build #9946845] Tests on macOS M1 - Mac Monterey (12) passed 💻✅ All tests on macOS M1 - Mac Monterey (12) passed. Pipeline on Agent |
💻 [CI Build #9946845] Tests on macOS arm64 - Mac Sequoia (15) passed 💻✅ All tests on macOS arm64 - Mac Sequoia (15) passed. Pipeline on Agent |
💻 [CI Build #9946845] Tests on macOS M1 - Mac Ventura (13) passed 💻✅ All tests on macOS M1 - Mac Ventura (13) passed. Pipeline on Agent |
🚀 [CI Build #9946845] Test results 🚀Test results✅ All tests passed on VSTS: test results. 🎉 All 119 tests passed 🎉 Tests counts✅ cecil: All 1 tests passed. Html Report (VSDrops) Download Pipeline on Agent |
Creates any call that has to be executed after the call to the native invoker, in most of the cases we need to add a GC.KeepAlive to ensure that the handle is not removed by the GC while it is in used by the native code.