Skip to content

[Rgen] Add the needed code to make the objc_msgSend signatures. - #22029

Merged
mandel-macaque merged 8 commits into
mainfrom
dev/mandel/objc_msgSend_factory
Jan 23, 2025
Merged

[Rgen] Add the needed code to make the objc_msgSend signatures.#22029
mandel-macaque merged 8 commits into
mainfrom
dev/mandel/objc_msgSend_factory

Conversation

@mandel-macaque

@mandel-macaque mandel-macaque commented Jan 23, 2025

Copy link
Copy Markdown
Contributor

Add the code that will calculate the signature needed in the objc_msgSend which will be the method that calls the native objc message.

Extra context

When we create a binding for a native type we need to be able to send 'messages' to the objc objects. From a C# developer perspective a message can be considered a method. So for example, the following objc code:

[object foo:bar];

Can be translated to:

objc_msgSend(object, sel_getUid("foo:"), bar);

We can say this process is similar to reflection and the Invoke method. Our bindings provide methods around objc_msgsend which we use to send the messaged with the arguments needed by the message. For example, a property with a NSString backing fields defined as:

[Export ("text")]
public string? Text { get; set; }

Results in the following generated code:

[BindingImpl (BindingImplOptions.GeneratedCode | BindingImplOptions.Optimizable)]
public virtual string? Text {
	[Export ("text")]
	get {
		global::UIKit.UIApplication.EnsureUIThread ();
		if (IsDirectBinding) {
			return CFString.FromHandle (global::ObjCRuntime.Messaging.NativeHandle_objc_msgSend (this.Handle, Selector.GetHandle ("text")), false)!;
		} else {
			return CFString.FromHandle (global::ObjCRuntime.Messaging.NativeHandle_objc_msgSendSuper (this.SuperHandle, Selector.GetHandle ("text")), false)!;
		}
	}
	[Export ("setText:")]
	set {
		global::UIKit.UIApplication.EnsureUIThread ();
		var nsvalue = CFString.CreateNative (value);
		if (IsDirectBinding) {
			global::ObjCRuntime.Messaging.void_objc_msgSend_NativeHandle (this.Handle, Selector.GetHandle ("setText:"), nsvalue);
		} else {
			global::ObjCRuntime.Messaging.void_objc_msgSendSuper_NativeHandle (this.SuperHandle, Selector.GetHandle ("setText:"), nsvalue);
		}
		CFString.ReleaseNative (nsvalue);
	}
}

Our objc_msgSend method signatures are calculated based on the method parameters and live in the runtime. These objc_msgSend calls are generated by https://github.com/xamarin/xamarin-macios/blob/main/runtime/bindings-generator.cs#L20 For example a objc_msgSend signature for a method that takes a string and returns a string would be:

IntPtr_objc_msgSend_IntPtr

But this is a simplifications because there is a diff between sending a message to a super class (prefix super) or if it deals with a structure (stret).

More info can be found in the Apple documentation.

mandel-macaque and others added 2 commits January 22, 2025 19:26
Add the code that will calculate the signature needed in the
objc_msgSend which will be the method that calls the native objc
message.
@github-actions

Copy link
Copy Markdown
Contributor

⚠️ Your code has been reformatted. ⚠️

If this is not desired, add the actions-disable-autoformat label, and revert the reformatting commit.

If files unrelated to your change were modified, try reverting the reformatting commit + merging with the target branch (and push those changes).

@mandel-macaque mandel-macaque added the actions-disable-autoformat Disable autoformatting C# code according to our style guidelines label Jan 23, 2025
@github-actions

Copy link
Copy Markdown
Contributor

⚠️ Your code has been reformatted. ⚠️

If this is not desired, add the actions-disable-autoformat label, and revert the reformatting commit.

If files unrelated to your change were modified, try reverting the reformatting commit + merging with the target branch (and push those changes).

@mandel-macaque
mandel-macaque force-pushed the dev/mandel/objc_msgSend_factory branch from 2a80edc to 6ad32da Compare January 23, 2025 01:11

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot reviewed 5 out of 18 changed files in this pull request and generated 6 comments.

Files not reviewed (13)
  • src/rgen/Microsoft.Macios.Transformer/Microsoft.Macios.Transformer.csproj: Language not supported
  • src/rgen/Microsoft.Macios.Generator/Emitters/BindingSyntaxFactory.cs: Evaluated as low risk
  • src/rgen/Microsoft.Macios.Generator/DataModel/TypeInfoComparer.cs: Evaluated as low risk
  • src/rgen/Microsoft.Macios.Generator/Formatters/TypeInfoFormatter.cs: Evaluated as low risk
  • src/rgen/Microsoft.Macios.Generator/DataModel/TypeInfo.Generator.cs: Evaluated as low risk
  • tests/rgen/Microsoft.Macios.Generator.Tests/TestDataFactory.cs: Evaluated as low risk
  • src/rgen/Microsoft.Macios.Generator/Emitters/BindingSyntaxFactory.Dlfcn.cs: Evaluated as low risk
  • tests/rgen/Microsoft.Macios.Generator.Tests/DataModel/TypeInfoComparerTests.cs: Evaluated as low risk
  • src/rgen/Microsoft.Macios.Generator/Formatters/ParameterFormatter.cs: Evaluated as low risk
  • src/rgen/Microsoft.Macios.Generator/DataModel/Property.cs: Evaluated as low risk
  • src/rgen/Microsoft.Macios.Generator/DataModel/TypeInfo.cs: Evaluated as low risk
  • src/rgen/Microsoft.Macios.Generator/Extensions/SpecialTypeExtensions.cs: Evaluated as low risk
  • tests/rgen/Microsoft.Macios.Generator.Tests/Emitters/GetObjCMessageSendMethodNameTests.cs: Evaluated as low risk

Comment thread src/rgen/Microsoft.Macios.Generator/Emitters/BindingSyntaxFactory.ObjCRuntime.cs Outdated
Comment thread src/rgen/Microsoft.Macios.Generator/Emitters/BindingSyntaxFactory.ObjCRuntime.cs Outdated
Comment thread src/rgen/Microsoft.Macios.Generator/Emitters/BindingSyntaxFactory.ObjCRuntime.cs Outdated
Comment thread src/rgen/Microsoft.Macios.Generator/Emitters/BindingSyntaxFactory.ObjCRuntime.cs Outdated
Comment thread src/rgen/Microsoft.Macios.Generator/Extensions/EnumExtensions.cs Outdated
Comment thread src/rgen/Microsoft.Macios.Generator/Extensions/EnumExtensions.cs Outdated
@github-actions

Copy link
Copy Markdown
Contributor

⚠️ Your code has been reformatted. ⚠️

If this is not desired, add the actions-disable-autoformat label, and revert the reformatting commit.

If files unrelated to your change were modified, try reverting the reformatting commit + merging with the target branch (and push those changes).

mandel-macaque and others added 2 commits January 22, 2025 20:20
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@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.

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

@jonathanpeppers jonathanpeppers left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just a minor issue with a comment, I think. Not a blocker. 👍

Comment on lines +24 to +27
ExportData<ObjCBindings.Property> property => property.Flags.HasFlag (ObjCBindings.Property
.CustomMarshalDirective),
ExportData<ObjCBindings.Method> method => method.Flags.HasFlag (
ObjCBindings.Property.CustomMarshalDirective),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I remembered HasFlag had a performance issue, but it's ok to use now in .NET 5+:

Comment on lines 21 to +24
/// <summary>
/// Type of the parameter.
/// </summary>
public string Name { get; }
public string FullyQualifiedName {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nitpick: is the summary comment here wrong now?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It is, fixing

Comment thread src/rgen/Microsoft.Macios.Generator/DataModel/TypeInfo.cs Outdated
@mandel-macaque
mandel-macaque merged commit d52838b into main Jan 23, 2025
@mandel-macaque
mandel-macaque deleted the dev/mandel/objc_msgSend_factory branch January 23, 2025 17:15
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

✅ [PR Build] Build passed (Build packages) ✅

Pipeline on Agent
Hash: 4427d37cc06bbf433825f7a6467615d9e5d74bdd [PR build]

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

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

Pipeline on Agent
Hash: 4427d37cc06bbf433825f7a6467615d9e5d74bdd [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

✅ [PR Build] Build passed (Build macOS tests) ✅

Pipeline on Agent
Hash: 4427d37cc06bbf433825f7a6467615d9e5d74bdd [PR build]

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

✅ API diff for current PR / commit

.NET ( No breaking changes )

❗ API diff vs stable (Breaking changes)

.NET ( ❗ Breaking changes ❗ )

ℹ️ Generator diff

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

Pipeline on Agent
Hash: d52838baf4e7b660a1f4fa1ae29c07dc0b9ae767 [PR build]

@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

Copy link
Copy Markdown
Collaborator

💻 [CI Build] Windows Integration Tests passed 💻

All Windows Integration Tests passed.

Pipeline on Agent
Hash: 4427d37cc06bbf433825f7a6467615d9e5d74bdd [PR build]

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

💻 [PR Build] Tests on macOS M1 - Mac Monterey (12) passed 💻

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

Pipeline on Agent
Hash: 4427d37cc06bbf433825f7a6467615d9e5d74bdd [PR build]

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

💻 [PR Build] Tests on macOS arm64 - Mac Sequoia (15) passed 💻

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

Pipeline on Agent
Hash: 4427d37cc06bbf433825f7a6467615d9e5d74bdd [PR build]

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

💻 [PR Build] Tests on macOS X64 - Mac Sonoma (14) passed 💻

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

Pipeline on Agent
Hash: 4427d37cc06bbf433825f7a6467615d9e5d74bdd [PR build]

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

💻 [PR Build] Tests on macOS M1 - Mac Ventura (13) passed 💻

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

Pipeline on Agent
Hash: 4427d37cc06bbf433825f7a6467615d9e5d74bdd [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

🚀 [CI Build] Test results 🚀

Test results

✅ All tests passed on VSTS: test results.

🎉 All 120 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 6 tests passed. Html Report (VSDrops) Download
✅ linker: All 44 tests passed. Html Report (VSDrops) Download
✅ monotouch (iOS): All 10 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
✅ xcframework: All 4 tests passed. Html Report (VSDrops) Download
✅ xtro: All 1 tests passed. Html Report (VSDrops) Download

Pipeline on Agent
Hash: d52838baf4e7b660a1f4fa1ae29c07dc0b9ae767 [PR build]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

actions-disable-autoformat Disable autoformatting C# code according to our style guidelines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants