Skip to content

Commit

Permalink
[jnimarshalmethod-gen] Fix registration on Windows (#721)
Browse files Browse the repository at this point in the history
`jnimarshalmethod-gen` didn't call [`MethodBuilder.SetParameters()`][0]
when creating the marshal method.  While this worked under Mono, the
lack of a `MethodBuilder.SetParameters()` invocation caused
`jnimarshalmethod-gen` to crash when running under .NET Framework:

	error JM4006: jnimarshalmethod-gen: Unable to process assembly '.\bin\TestDebug\Java.Interop.Export-Tests.dll'
	Specified argument was out of the range of valid values.
	Parameter name: The specified parameter index is not in range.
	System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
	Parameter name: The specified parameter index is not in range.
	   at System.Reflection.Emit.MethodBuilder.DefineParameter(Int32 position, ParameterAttributes attributes, String strParamName)
	   at System.Linq.Expressions.Compiler.LambdaCompiler..ctor(AnalyzedTree tree, LambdaExpression lambda, MethodBuilder method)
	   at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda, MethodBuilder method, DebugInfoGenerator debugInfoGenerator)
	   at Xamarin.Android.Tools.JniMarshalMethodGenerator.App.AddRegisterNativeMembers(TypeBuilder dt, ParameterExpression targetType, List`1 registrationElements) in C:\Users\rodo\git\java.interop\tools\jnimarshalmethod-gen\App.cs:line 564
	   at Xamarin.Android.Tools.JniMarshalMethodGenerator.App.CreateMarshalMethodAssembly(String path) in C:\Users\rodo\git\java.interop\tools\jnimarshalmethod-gen\App.cs:line 426
	   at Xamarin.Android.Tools.JniMarshalMethodGenerator.App.ProcessAssemblies(List`1 assemblies) in C:\Users\rodo\git\java.interop\tools\jnimarshalmethod-gen\App.cs:line 201

Add a call to `MethodBuilder.SetParameters()` for Windows support.

Additionally, when `jnimarshalmethod-gen -v` (verbose) is used,
print out the type(s) for which`__RegisterNativeMembers()` is being
emitted.

[0]: https://docs.microsoft.com/en-us/dotnet/api/system.reflection.emit.methodbuilder.setparameters?view=netcore-3.1
  • Loading branch information
radekdoulik authored Sep 18, 2020
1 parent 5a834d4 commit 53d6051
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion tools/jnimarshalmethod-gen/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,12 @@ static Expression CreateRegistration (string method, string signature, LambdaExp

static void AddRegisterNativeMembers (TypeBuilder dt, ParameterExpression targetType, List<Expression> registrationElements)
{
var args = Expression.Parameter (typeof (JniNativeMethodRegistrationArguments), "args");
if (Verbose) {
Console.Write ("Adding registration method for ");
ColorWriteLine ($"{dt.FullName}", ConsoleColor.Green);
}

var args = Expression.Parameter (typeof (JniNativeMethodRegistrationArguments), "args");
var body = Expression.Block (
new[]{targetType},
Expression.Assign (targetType, Expression.Call (Type_GetType, Expression.Constant (dt.FullName))),
Expand All @@ -495,6 +499,7 @@ static void AddRegisterNativeMembers (TypeBuilder dt, ParameterExpression target

var rb = dt.DefineMethod ("__RegisterNativeMembers",
System.Reflection.MethodAttributes.Public | System.Reflection.MethodAttributes.Static);
rb.SetParameters (typeof (JniNativeMethodRegistrationArguments));
rb.SetCustomAttribute (new CustomAttributeBuilder (typeof (JniAddNativeMethodRegistrationAttribute).GetConstructor (Type.EmptyTypes), new object[0]));
#if _DUMP_REGISTER_NATIVE_MEMBERS
Console.WriteLine ($"## Dumping contents of `{dt.FullName}::__RegisterNativeMembers`: ");
Expand Down

0 comments on commit 53d6051

Please sign in to comment.