Repository navigation
[release/11.0.1xx] [TrimmableTypeMap] Avoid peer lookup reflection #13586
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2352,7 +2352,9 @@ Java.Interop.JavaPeerAliasesAttribute | |
| Java.Interop.JavaPeerAliasesAttribute.Aliases.get -> string![]! | ||
| Java.Interop.JavaPeerAliasesAttribute.JavaPeerAliasesAttribute(params string![]! aliases) -> void | ||
| Java.Interop.JavaPeerProxy | ||
| Java.Interop.JavaPeerProxy.IsSealed.get -> bool | ||
| Java.Interop.JavaPeerProxy.JavaPeerProxy(string! jniName, System.Type! targetType) -> void | ||
| Java.Interop.JavaPeerProxy.JavaPeerProxy(string! jniName, System.Type! targetType, bool isSealed) -> void | ||
|
Comment on lines
+2355
to
+2357
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @simonrozsival is it a problem to add new APIs in a service release? What would happen is someone with the new workload could create a NuGet package that wouldn't work for folks with the old one?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The typemap code, which uses this, is always generated at app build time. No libraries (incl. Mono.Android) use this ctor. Also, this currently only affects Native AOT apps by default. I will think this through tomorrow and I'll try to think about the risks. At the same time, this is just performance improvement that we do not need to backport and we can keep this .NET 12 only. |
||
| Java.Interop.JavaPeerProxy.JniName.get -> string! | ||
| Java.Interop.JavaPeerProxy.TargetType.get -> System.Type! | ||
| Java.Lang.ICharSequence.GetChars(int srcBegin, int srcEnd, char[]! dst, int dstBegin) -> void | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -97,6 +97,79 @@ public void Generate_HasRequiredAssemblyReferences () | |
| Assert.Contains ("System.Runtime.InteropServices", asmRefs); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData ("Mono.Android", false)] | ||
| [InlineData ("Mono.Android", true)] | ||
| [InlineData ("Other", false)] | ||
| [InlineData ("Other", true)] | ||
| public void Generate_ProxyConstructor_PassesSealedFrameworkEligibility (string assemblyName, bool isSealed) | ||
| { | ||
| foreach (bool alias in new [] { false, true }) { | ||
| foreach (bool generic in new [] { false, true }) { | ||
| var peer = MakePeerWithActivation ( | ||
| "example/Peer", generic ? "Example.Peer`1" : "Example.Peer", assemblyName) with { | ||
| IsFrameworkAssembly = true, | ||
| IsSealed = isSealed, | ||
| IsGenericDefinition = generic, | ||
| DoNotGenerateAcw = true, | ||
| }; | ||
| var peers = new List<JavaPeerInfo> { peer }; | ||
| if (alias) { | ||
| peers.Add (MakePeerWithActivation ("example/Peer", "Example.OtherPeer", assemblyName) with { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤖 💡 Testing — Consider adding a mixed-assembly alias case: a sealed {Rule: Test edge cases; verify interop boundary behavior} |
||
| IsFrameworkAssembly = true, | ||
| DoNotGenerateAcw = true, | ||
| }); | ||
| } | ||
|
|
||
| var generator = new TypeMapAssemblyGenerator (new Version (11, 0)); | ||
| var model = generator.CreateModel (peers, "_Example.TypeMap"); | ||
| using var stream = new MemoryStream (); | ||
| generator.Generate (model, stream, useSharedTypemapUniverse: true); | ||
| stream.Position = 0; | ||
| using var pe = new PEReader (stream); | ||
| var reader = pe.GetMetadataReader (); | ||
| var baseConstructors = FindCtorMemberRefs ( | ||
| reader, "Java.Interop", "JavaPeerProxy", "System.String", "System.Type", "System.Boolean"); | ||
| Assert.NotEmpty (baseConstructors); | ||
| Assert.Equal (alias ? 2 : 1, model.ProxyTypes.Count); | ||
|
|
||
| foreach (var proxy in model.ProxyTypes) { | ||
| bool eligible = assemblyName == "Mono.Android" && isSealed && | ||
| proxy.TargetType.ManagedTypeName == peer.ManagedTypeName; | ||
| Assert.Equal (eligible, proxy.CanResolveFromSealedTargetType); | ||
| var type = reader.TypeDefinitions | ||
| .Select (reader.GetTypeDefinition) | ||
| .Single (type => reader.GetString (type.Namespace) == proxy.Namespace && | ||
| reader.GetString (type.Name) == proxy.TypeName); | ||
| Assert.Equal (HandleKind.TypeReference, type.BaseType.Kind); | ||
| var baseType = reader.GetTypeReference ((TypeReferenceHandle) type.BaseType); | ||
| Assert.Equal ("Java.Interop", reader.GetString (baseType.Namespace)); | ||
| Assert.Equal ("JavaPeerProxy", reader.GetString (baseType.Name)); | ||
| Assert.DoesNotContain (type.GetProperties (), | ||
| handle => reader.GetString (reader.GetPropertyDefinition (handle).Name) == "IsSealed"); | ||
|
|
||
| var constructor = type.GetMethods () | ||
| .Select (reader.GetMethodDefinition) | ||
| .Single (method => reader.GetString (method.Name) == ".ctor"); | ||
| var il = pe.GetMethodBody (constructor.RelativeVirtualAddress).GetILBytes (); | ||
| Assert.NotNull (il); | ||
| bool callsBaseWithExpectedValue = false; | ||
| foreach (var handle in baseConstructors) { | ||
| int token = MetadataTokens.GetToken (handle); | ||
| byte [] call = [ | ||
| (byte) (eligible ? ILOpCode.Ldc_i4_1 : ILOpCode.Ldc_i4_0), | ||
| (byte) ILOpCode.Call, | ||
| (byte) token, (byte) (token >> 8), (byte) (token >> 16), (byte) (token >> 24), | ||
| ]; | ||
| callsBaseWithExpectedValue |= il.AsSpan ().IndexOf (call) >= 0; | ||
| } | ||
| Assert.True (callsBaseWithExpectedValue, | ||
| $"{proxy.TypeName}: assembly={assemblyName}, sealed={isSealed}, alias={alias}, generic={generic}"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Generate_InheritedGenericBaseCallback_UsesValueTypeGenericArgument () | ||
| { | ||
|
|
||
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.
What if we made this
EditorBrowsable.Hidden? We'd also want to do that in main.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.
I don't see how that would be very helpful. This property is in no way different from the other properties. If anything, we should add it to the whole JavaPeerProxy class