Skip to content

[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

Merged
merged 18 commits into from
Jun 14, 2025

Conversation

mandel-macaque
Copy link
Member

Create the needed factory method that will generate the call for the invoker native delegate.

mandel-macaque and others added 5 commits June 10, 2025 12:29
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.
@vs-mobiletools-engineering-service2

This comment has been minimized.

@mandel-macaque mandel-macaque requested a review from Copilot June 10, 2025 21:23
Copy link
Contributor

@Copilot Copilot AI left a 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 in Nomenclator to standardize the invoker variable name.
  • Implement GetTrampolineNativeInvokeArgument, GetTrampolineNativeInvokeArguments, and CallNativeInvokerDelegate 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
Copy link
Preview

Copilot AI Jun 10, 2025

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.

Comment on lines 1175 to 1206
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[]
Copy link
Preview

Copilot AI Jun 10, 2025

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.

Suggested change
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.

Copy link
Member Author

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.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

Base automatically changed from dev/mandel/post-native-invoke-conversions to main June 11, 2025 20:22
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@mandel-macaque
Copy link
Member Author

Failing tests are unrelated to code changes

@vs-mobiletools-engineering-service2
Copy link
Collaborator

✅ [CI Build #7732211] Build passed (Build packages) ✅

Pipeline on Agent
Hash: 7732211f5de753c2cb44bac163d9f533e71a24a7 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

✅ [PR Build #7732211] Build passed (Detect API changes) ✅

Pipeline on Agent
Hash: 7732211f5de753c2cb44bac163d9f533e71a24a7 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

✅ API diff for current PR / commit

.NET ( No breaking changes )

✅ API diff vs stable

.NET ( No breaking changes )

ℹ️ Generator diff

Generator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes)

Pipeline on Agent
Hash: 7732211f5de753c2cb44bac163d9f533e71a24a7 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

✅ [CI Build #7732211] Build passed (Build macOS tests) ✅

Pipeline on Agent
Hash: 7732211f5de753c2cb44bac163d9f533e71a24a7 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build #7732211] Tests on macOS X64 - Mac Sonoma (14) passed 💻

All tests on macOS X64 - Mac Sonoma (14) passed.

Pipeline on Agent
Hash: 7732211f5de753c2cb44bac163d9f533e71a24a7 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build #7732211] Tests on macOS M1 - Mac Monterey (12) passed 💻

All tests on macOS M1 - Mac Monterey (12) passed.

Pipeline on Agent
Hash: 7732211f5de753c2cb44bac163d9f533e71a24a7 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build #7732211] Tests on macOS arm64 - Mac Sequoia (15) passed 💻

All tests on macOS arm64 - Mac Sequoia (15) passed.

Pipeline on Agent
Hash: 7732211f5de753c2cb44bac163d9f533e71a24a7 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build #7732211] Tests on macOS M1 - Mac Ventura (13) passed 💻

All tests on macOS M1 - Mac Ventura (13) passed.

Pipeline on Agent
Hash: 7732211f5de753c2cb44bac163d9f533e71a24a7 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

🚀 [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
✅ dotnettests (iOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (MacCatalyst): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (macOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (Multiple platforms): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (tvOS): All 1 tests passed. Html Report (VSDrops) Download
✅ framework: All 2 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 4 tests passed. Html Report (VSDrops) Download
✅ generator: All 5 tests passed. Html Report (VSDrops) Download
✅ interdependent-binding-projects: All 4 tests passed. Html Report (VSDrops) Download
✅ introspection: All 4 tests passed. Html Report (VSDrops) Download
✅ linker: All 44 tests passed. Html Report (VSDrops) Download
✅ monotouch (iOS): All 8 tests passed. Html Report (VSDrops) Download
✅ monotouch (MacCatalyst): All 11 tests passed. Html Report (VSDrops) Download
✅ monotouch (macOS): All 9 tests passed. Html Report (VSDrops) Download
✅ monotouch (tvOS): All 8 tests passed. Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
✅ windows: All 3 tests passed. Html Report (VSDrops) Download
✅ xcframework: All 4 tests passed. Html Report (VSDrops) Download
✅ xtro: All 1 tests passed. Html Report (VSDrops) Download

Pipeline on Agent
Hash: 7732211f5de753c2cb44bac163d9f533e71a24a7 [PR build]

@mandel-macaque mandel-macaque merged commit 42a623a into main Jun 14, 2025
44 checks passed
@mandel-macaque mandel-macaque deleted the dev/mandel/native-argument-syntax branch June 14, 2025 15:35
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.

4 participants