-
Notifications
You must be signed in to change notification settings - Fork 542
[RGen] Provide factory method with the invoker call. #23020
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
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.
Create the needed factory method that will generate the call for the invoker native delegate.
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 support for generating the native invoker call in trampolines by introducing a factory method and wiring up tests to validate the invocation syntax.
- Introduce
GetNativeInvokerVariableName
inNomenclator
to standardize the invoker variable name. - Implement
GetTrampolineNativeInvokeArgument
,GetTrampolineNativeInvokeArguments
, andCallNativeInvokerDelegate
in the binding syntax factory. - Add data-driven tests under
BindingSyntaxFactoryTrampolineTests.cs
to verify void and non-void invocations for various parameter types.
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
File | Description |
---|---|
tests/rgen/Microsoft.Macios.Generator.Tests/Emitters/BindingSyntaxFactoryTrampolineTests.cs | Add TestDataGetTrampolineNativeInvokeArguments and CallNativeInvokerDelegateTests to validate generated invoker calls |
src/rgen/Microsoft.Macios.Generator/Nomenclator.cs | Add GetNativeInvokerVariableName factory method |
src/rgen/Microsoft.Macios.Generator/Emitters/BindingSyntaxFactory.Trampoline.cs | Implement argument and invocation generation for native delegate calls |
Comments suppressed due to low confidence (1)
tests/rgen/Microsoft.Macios.Generator.Tests/Emitters/BindingSyntaxFactoryTrampolineTests.cs:4657
- Test coverage currently focuses on primitive and bindable types; consider adding cases for delegate parameters (both C-callback and block callback) to ensure those code paths in
GetTrampolineNativeInvokeArgument
are exercised.
class TestDataGetTrampolineNativeInvokeArguments : IEnumerable<object []> {
var expression = (Type: parameterType, Parameter: parameter) switch { | ||
// delegate parameter, c callback | ||
// System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer<ParameterType> (ParameterName) | ||
// TODO |
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.
The TODO placeholders for C-callback and block-callback delegate handling indicate missing logic paths. Implement the Marshal.GetDelegateForFunctionPointer
and CreateTrampolineNativeInvocationClass
invocations to fully support delegate parameters.
Copilot uses AI. Check for mistakes.
var expression = (Type: parameterType, Parameter: parameter) switch { | ||
// delegate parameter, c callback | ||
// System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer<ParameterType> (ParameterName) | ||
// TODO | ||
{ Type.IsDelegate: true, Parameter.IsCCallback: true } => | ||
GetDelegateForFunctionPointer (parameterType.GetIdentifierSyntax (), [Argument (parameterIdentifier)]), | ||
|
||
// delegate parameter, block callback | ||
// TrampolineNativeInvocationClass.Create (ParameterName)! | ||
// TODO | ||
{ Type.IsDelegate: true, Parameter.IsBlockCallback: true } | ||
=> CreateTrampolineNativeInvocationClass (trampolineName, [Argument (parameterIdentifier)]), | ||
|
||
// native enum, return the conversion expression to the native type | ||
{ Type.IsNativeEnum: true} | ||
=> CastNativeToEnum (parameter)!, | ||
|
||
// boolean, convert it to byte | ||
{ Type.SpecialType: SpecialType.System_Boolean } | ||
=> CastToByte (parameter.Name, parameterType)!, | ||
|
||
// array types | ||
|
||
// use the native handle of the array | ||
{ Type.IsArray: true, Type.ArrayElementTypeIsWrapped: true } => | ||
IdentifierName (Nomenclator.GetNameForVariableType (parameter.Name, Nomenclator.VariableType.NSArray)!), | ||
|
||
// NSArray.ArrayFromHandle<{0}> ({1})! | ||
{ Type.IsArray: true, Type.ArrayElementIsINativeObject: true } => | ||
IdentifierName (Nomenclator.GetNameForVariableType (parameter.Name, Nomenclator.VariableType.NSArray)!), | ||
|
||
// string[] |
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] This large switch expression covers many type cases and can be hard to follow. Consider refactoring individual type-handling branches into smaller helper methods or separate functions for clarity and easier maintenance.
var expression = (Type: parameterType, Parameter: parameter) switch { | |
// delegate parameter, c callback | |
// System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer<ParameterType> (ParameterName) | |
// TODO | |
{ Type.IsDelegate: true, Parameter.IsCCallback: true } => | |
GetDelegateForFunctionPointer (parameterType.GetIdentifierSyntax (), [Argument (parameterIdentifier)]), | |
// delegate parameter, block callback | |
// TrampolineNativeInvocationClass.Create (ParameterName)! | |
// TODO | |
{ Type.IsDelegate: true, Parameter.IsBlockCallback: true } | |
=> CreateTrampolineNativeInvocationClass (trampolineName, [Argument (parameterIdentifier)]), | |
// native enum, return the conversion expression to the native type | |
{ Type.IsNativeEnum: true} | |
=> CastNativeToEnum (parameter)!, | |
// boolean, convert it to byte | |
{ Type.SpecialType: SpecialType.System_Boolean } | |
=> CastToByte (parameter.Name, parameterType)!, | |
// array types | |
// use the native handle of the array | |
{ Type.IsArray: true, Type.ArrayElementTypeIsWrapped: true } => | |
IdentifierName (Nomenclator.GetNameForVariableType (parameter.Name, Nomenclator.VariableType.NSArray)!), | |
// NSArray.ArrayFromHandle<{0}> ({1})! | |
{ Type.IsArray: true, Type.ArrayElementIsINativeObject: true } => | |
IdentifierName (Nomenclator.GetNameForVariableType (parameter.Name, Nomenclator.VariableType.NSArray)!), | |
// string[] | |
var expression = GetExpressionForParameterType(parameterType, parameter, trampolineName, parameterIdentifier); |
Copilot uses AI. Check for mistakes.
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.
No, it is not that hard to follow and the switch expression ensures that there are not collisions which would happen with a refactor.
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.
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.
This comment has been minimized.
This comment has been minimized.
Failing tests are unrelated to code changes |
✅ [CI Build #7732211] Build passed (Build packages) ✅Pipeline on Agent |
✅ [PR Build #7732211] 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 #7732211] Build passed (Build macOS tests) ✅Pipeline on Agent |
💻 [CI Build #7732211] Tests on macOS X64 - Mac Sonoma (14) passed 💻✅ All tests on macOS X64 - Mac Sonoma (14) passed. Pipeline on Agent |
💻 [CI Build #7732211] Tests on macOS M1 - Mac Monterey (12) passed 💻✅ All tests on macOS M1 - Mac Monterey (12) passed. Pipeline on Agent |
💻 [CI Build #7732211] Tests on macOS arm64 - Mac Sequoia (15) passed 💻✅ All tests on macOS arm64 - Mac Sequoia (15) passed. Pipeline on Agent |
💻 [CI Build #7732211] Tests on macOS M1 - Mac Ventura (13) passed 💻✅ All tests on macOS M1 - Mac Ventura (13) passed. Pipeline on Agent |
🚀 [CI Build #7732211] Test results 🚀Test results✅ All tests passed on VSTS: test results. 🎉 All 115 tests passed 🎉 Tests counts✅ cecil: All 1 tests passed. Html Report (VSDrops) Download Pipeline on Agent |
Create the needed factory method that will generate the call for the invoker native delegate.