Skip to content
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

[jnimarshalmethod-gen] Create delegate types for marshaling methods with 15+ parameters #711

Conversation

radekdoulik
Copy link
Member

@radekdoulik radekdoulik commented Sep 8, 2020

Context: #709

Generate delegate types for marshaling methods. We generate them
in cases where we cannot use Func<> or Action<> delegates
because of too many parameters (more than 14) and when we don't
have special custom delegate type available - like
_JniMarshal_PPZBCSIJFDLLLLLDFJ_Z delegate type in our tests.

Let the TypeMover move these delegate types to the original
assembly as well.

Added test to check marshaling with auto-generated delegate types.

Example of generated delegate type:

.class private auto ansi sealed '__<$>_jni_marshal_LLZBCSIJFDLLLLLDFJII_I'
       extends [mscorlib]System.MulticastDelegate
{
  .method public hidebysig specialname rtspecialname 
          instance void  .ctor(object A_1,
                               native int A_2) runtime managed
  {
  } // end of method '__<$>_jni_marshal_LLZBCSIJFDLLLLLDFJII_I'::.ctor

  .method public hidebysig newslot virtual 
          instance bool  Invoke(native int A_1,
                                native int A_2,
                                bool A_3,
                                int8 A_4,
                                char A_5,
                                int16 A_6,
                                int32 A_7,
                                int64 A_8,
                                float32 A_9,
                                float64 A_10,
                                native int A_11,
                                native int A_12,
                                native int A_13,
                                native int A_14,
                                native int A_15,
                                float64 A_16,
                                float32 A_17,
                                int64 A_18,
                                int32 A_19,
                                int32 A_20) runtime managed
  {
  } // end of method '__<$>_jni_marshal_LLZBCSIJFDLLLLLDFJII_I'::Invoke

  .method public hidebysig newslot virtual 
          instance [mscorlib]System.IAsyncResult 
          BeginInvoke(native int A_1,
                      native int A_2,
                      bool A_3,
                      int8 A_4,
                      char A_5,
                      int16 A_6,
                      int32 A_7,
                      int64 A_8,
                      float32 A_9,
                      float64 A_10,
                      native int A_11,
                      native int A_12,
                      native int A_13,
                      native int A_14,
                      native int A_15,
                      float64 A_16,
                      float32 A_17,
                      int64 A_18,
                      int32 A_19,
                      int32 A_20,
                      [mscorlib]System.AsyncCallback A_21,
                      object A_22) runtime managed
  {
  } // end of method '__<$>_jni_marshal_LLZBCSIJFDLLLLLDFJII_I'::BeginInvoke

  .method public hidebysig newslot virtual 
          instance bool  EndInvoke([mscorlib]System.IAsyncResult A_1) runtime managed
  {
  } // end of method '__<$>_jni_marshal_LLZBCSIJFDLLLLLDFJII_I'::EndInvoke

} // end of class '__<$>_jni_marshal_LLZBCSIJFDLLLLLDFJII_I'

An example of the optimized code in the registration __<$>_jni_marshal_methods'::__RegisterNativeMembers method:

...
  IL_0181:  dup
  IL_0182:  ldc.i4.s   11
  IL_0184:  ldstr      "staticManyParametersWithAutogeneratedDelegateType"
  IL_0189:  ldstr      "(ZBCSIJFDLjava/lang/Object;Ljava/lang/String;Ljava"
  + "/util/ArrayList;Ljava/lang/String;Ljava/lang/Object;DFJII)I"
  IL_018e:  ldnull
  IL_018f:  ldftn      int32 Java.InteropTests.ExportTest/'__<$>_jni_marshal_methods'::staticManyParametersWithAutogeneratedDelegateType(native int,
                                                                                                                                         native int,
                                                                                                                                         bool,
                                                                                                                                         int8,
                                                                                                                                         char,
                                                                                                                                         int16,
                                                                                                                                         int32,
                                                                                                                                         int64,
                                                                                                                                         float32,
                                                                                                                                         float64,
                                                                                                                                         native int,
                                                                                                                                         native int,
                                                                                                                                         native int,
                                                                                                                                         native int,
                                                                                                                                         native int,
                                                                                                                                         float64,
                                                                                                                                         float32,
                                                                                                                                         int64,
                                                                                                                                         int32,
                                                                                                                                         int32)
  IL_0195:  newobj     instance void '__<$>_jni_marshal_LLZBCSIJFDLLLLLDFJII_I'::.ctor(object,
                                                                                       native int)
  IL_019a:  newobj     instance void [Java.Interop]Java.Interop.JniNativeMethodRegistration::.ctor(string,
                                                                                                   string,
                                                                                                   [mscorlib]System.Delegate)
  IL_019f:  stelem     [Java.Interop]Java.Interop.JniNativeMethodRegistration
...

@radekdoulik radekdoulik changed the title Create delegate types for marshaling methods with 14+ parameters Create delegate types for marshaling methods with 15+ parameters Sep 8, 2020
@radekdoulik radekdoulik force-pushed the pr-finish-general-marshal-delegate-types-2 branch from 06985ed to 8f101c8 Compare September 9, 2020 10:42
@radekdoulik radekdoulik changed the title Create delegate types for marshaling methods with 15+ parameters [jnimarshalmethod-gen] Create delegate types for marshaling methods with 15+ parameters Sep 9, 2020
@radekdoulik radekdoulik added this to In Progress in jnimarshalmethod-gen via automation Sep 9, 2020
@radekdoulik radekdoulik marked this pull request as ready for review September 9, 2020 18:58
@jonpryor
Copy link
Member

Is there a reason to generate these new __<$>_jni_marshal_-prefixed delegate types instead of using the same types -- or same naming convention? -- as the _JniMarshal_-prefixed types?

return marshalDelegateType;
}

public static void AddMarshalerTypeNameSuffix (StringBuilder sb, Type returnType, List<Type> funcTypeParams)
Copy link
Member

Choose a reason for hiding this comment

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

I ask because I'm wondering if there's some way to not add this public export, or at least find a "more suitable" location for it?

@@ -159,6 +159,66 @@ public static void StaticActionFloat (float f)
return false;
return true;
}

[JavaCallable ("staticManyParametersWithAutogeneratedDelegateType", Signature="(ZBCSIJFDLjava/lang/Object;Ljava/lang/String;Ljava/util/ArrayList;Ljava/lang/String;Ljava/lang/Object;DFJII)I")]
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand how this method differs from the existing "lots of parameters" test:

https://github.com/xamarin/java.interop/blob/03c227228955622511bafabe61a12e30e45dc6f7/tests/Java.Interop.Export-Tests/Java.Interop/ExportTest.cs#L109-L162

How does this (pair of) method(s) differ from the current ExportType.staticFuncThisMethodTakesLotsOfParameters() method? They both have lots of parameters, and they both appear to have "similar" [JavaCallable] custom attribute values. How does this test differ?

parameterTypes.Add (typeof (AsyncCallback));
parameterTypes.Add (typeof (object));

CreateDelegateRuntimeManagedMethod (dtb, "BeginInvoke", typeof (IAsyncResult), parameterTypes.ToArray ());
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to declare BeginInvoke() and EndInvoke() if these are generated types, with type names that are "inaccessible", and jnimarshalmethod-gen isn't emitting references to these methods?

Base automatically changed from master to main January 22, 2021 20:46
@jpobst jpobst removed their request for review August 30, 2021 15:43
@jpobst jpobst marked this pull request as draft December 2, 2021 22:29
@jonpryor
Copy link
Member

jonpryor commented Mar 2, 2023

Closing as this is largely moot as of #1046 + dependencies.

@jonpryor jonpryor closed this Mar 2, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Apr 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
No open projects
jnimarshalmethod-gen
  
In Progress
Development

Successfully merging this pull request may close these issues.

None yet

2 participants