Skip to content

Conversation

@mandel-macaque
Copy link
Contributor

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.

mandel-macaque and others added 4 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.
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

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

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 post-invoke GC.KeepAlive generation for trampoline parameters and updates existing property emitters to use the new helper.

  • Introduces GetTrampolinePostNativeInvokeArgumentConversions to emit GC.KeepAlive calls for managed-to-native parameters.
  • Adds a KeepAlive helper in BindingSyntaxFactory.Runtime and wires it through KnownTypes and ClassEmitter.
  • 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 IsProtocol parameters to ensure that GetTrampolinePostNativeInvokeArgumentConversions correctly emits a GC.KeepAlive call for protocol-bound objects.
        }

{ExpressionStatement (invocations.Getter.SendSuper)}
}}
GC.KeepAlive (this);
{KeepAlive ("this")};
Copy link

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] 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.

Suggested change
{KeepAlive ("this")};
{ExpressionStatement(KeepAlive("this"))};

Copilot uses AI. Check for mistakes.
{
// decide the type of conversion we need to do based on the type of the parameter
#pragma warning disable format
return parameter.Type switch {
Copy link

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] 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.

Copilot uses AI. Check for mistakes.
@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.

Nomenclator.GetNameForVariableType (parameter.Name, Nomenclator.VariableType.BindFrom)!
))],

// boolean, convert it to byte
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment seems wrong

Base automatically changed from dev/mandel/pre-native-invoque-conversions to main June 11, 2025 16:20
@vs-mobiletools-engineering-service2
Copy link
Collaborator

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

Pipeline on Agent
Hash: 9946845e0e29b777da613d3d9a59d53c0faa9d71 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

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

Pipeline on Agent
Hash: 9946845e0e29b777da613d3d9a59d53c0faa9d71 [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: 9946845e0e29b777da613d3d9a59d53c0faa9d71 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

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

Pipeline on Agent
Hash: 9946845e0e29b777da613d3d9a59d53c0faa9d71 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

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

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

Pipeline on Agent
Hash: 9946845e0e29b777da613d3d9a59d53c0faa9d71 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

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

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

Pipeline on Agent
Hash: 9946845e0e29b777da613d3d9a59d53c0faa9d71 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

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

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

Pipeline on Agent
Hash: 9946845e0e29b777da613d3d9a59d53c0faa9d71 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

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

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

Pipeline on Agent
Hash: 9946845e0e29b777da613d3d9a59d53c0faa9d71 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

🚀 [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
✅ 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 15 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: 9946845e0e29b777da613d3d9a59d53c0faa9d71 [PR build]

@mandel-macaque mandel-macaque merged commit a25dccc into main Jun 11, 2025
42 of 44 checks passed
@mandel-macaque mandel-macaque deleted the dev/mandel/post-native-invoke-conversions branch June 11, 2025 20:22
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.

6 participants