Skip to content

Commit

Permalink
[Java.Interop.Tools.Expressions] Add Java.Interop.Tools.Expressions
Browse files Browse the repository at this point in the history
Write an `Expression<T>` compiler which uses Cecil for codegen
and assembly writing!

Then updated jnimarshalmethod-gen to use it!

Testing this puppy:

	% export PATH=$HOME/Developer/src/xamarin/xamarin-android/bin/Debug/dotnet:$PATH
	% mkdir _x
	% dotnet bin/Debug-net7.0/jnimarshalmethod-gen.dll \
	  bin/TestDebug-net7.0/Java.Interop.Export-Tests.dll \
	  -v --keeptemp \
	  --jvm /Library/Java/JavaVirtualMachines/microsoft-11.jdk/Contents/Home/lib/jli/libjli.dylib \
	  -o _x \
	  -L bin/TestDebug-net7.0 \
	  -L /usr/local/share/dotnet/shared/Microsoft.NETCore.App/7.0.0-rc.2.22472.3

First param is assembly to process; `Java.Interop.Export-Tests.dll`
is handy because that's what the `run-test-jnimarshal` target in
`Makefile` processes.

`-v` is verbose, `--keeptemp` is keep temporary files.

`--jvm PATH` is the path to the JVM library to load+use.

`-o DIR` is where to place output files; this will create
`_x/Java.Interop.Export-Tests.dll`.

`-L DIR` adds `DIR` to library resolution paths; this adds
`bin/TestDebug/net7.0` (dependencies of `Java.Interop.Export-Tests.dll`)
and `Microsoft.NETCore.App/7.0.0-rc.1.22422.12` (net7 libs).

…plus we have, like, *real* unit tests.  Which are differently odd.

"one-off" non-unit test test: make sure that the unit test assembly
can be decompiled:

	ikdasm  bin/TestDebug-net7.0/Java.Interop.Tools.Expressions-Tests-ExpressionCompilerTests.dll

This currently fails. :-/
  • Loading branch information
jonpryor committed Nov 2, 2022
1 parent 5318261 commit 75b5ce6
Show file tree
Hide file tree
Showing 12 changed files with 1,562 additions and 81 deletions.
14 changes: 14 additions & 0 deletions Java.Interop.sln
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Java.Base", "src\Java.Base\
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Java.Base-Tests", "tests\Java.Base-Tests\Java.Base-Tests.csproj", "{CB05E11B-B96F-4179-A4E9-5D6BDE29A8FC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Java.Interop.Tools.Expressions", "src\Java.Interop.Tools.Expressions\Java.Interop.Tools.Expressions.csproj", "{1A0262FE-3CDB-4AF2-AAD8-65C59524FE8A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Java.Interop.Tools.Expressions-Tests", "tests\Java.Interop.Tools.Expressions-Tests\Java.Interop.Tools.Expressions-Tests.csproj", "{211BAA88-66B1-41B2-88B2-530DBD8DF702}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
src\Java.Interop.NamingCustomAttributes\Java.Interop.NamingCustomAttributes.projitems*{58b564a1-570d-4da2-b02d-25bddb1a9f4f}*SharedItemsImports = 5
Expand Down Expand Up @@ -308,6 +312,14 @@ Global
{CB05E11B-B96F-4179-A4E9-5D6BDE29A8FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CB05E11B-B96F-4179-A4E9-5D6BDE29A8FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CB05E11B-B96F-4179-A4E9-5D6BDE29A8FC}.Release|Any CPU.Build.0 = Release|Any CPU
{1A0262FE-3CDB-4AF2-AAD8-65C59524FE8A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1A0262FE-3CDB-4AF2-AAD8-65C59524FE8A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1A0262FE-3CDB-4AF2-AAD8-65C59524FE8A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1A0262FE-3CDB-4AF2-AAD8-65C59524FE8A}.Release|Any CPU.Build.0 = Release|Any CPU
{211BAA88-66B1-41B2-88B2-530DBD8DF702}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{211BAA88-66B1-41B2-88B2-530DBD8DF702}.Debug|Any CPU.Build.0 = Debug|Any CPU
{211BAA88-66B1-41B2-88B2-530DBD8DF702}.Release|Any CPU.ActiveCfg = Release|Any CPU
{211BAA88-66B1-41B2-88B2-530DBD8DF702}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -360,6 +372,8 @@ Global
{11942DE9-AEC2-4B95-87AB-CA707C37643D} = {271C9F30-F679-4793-942B-0D9527CB3E2F}
{30DCECA5-16FD-4FD0-883C-E5E83B11565D} = {0998E45F-8BCE-4791-A944-962CD54E2D80}
{CB05E11B-B96F-4179-A4E9-5D6BDE29A8FC} = {271C9F30-F679-4793-942B-0D9527CB3E2F}
{1A0262FE-3CDB-4AF2-AAD8-65C59524FE8A} = {0998E45F-8BCE-4791-A944-962CD54E2D80}
{211BAA88-66B1-41B2-88B2-530DBD8DF702} = {271C9F30-F679-4793-942B-0D9527CB3E2F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {29204E0C-382A-49A0-A814-AD7FBF9774A5}
Expand Down
4 changes: 2 additions & 2 deletions src/Java.Interop.Export/Java.Interop.Export.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;$(DotNetTargetFramework)</TargetFrameworks>
<LangVersion>8.0</LangVersion>
<LangVersion>9.0</LangVersion>
<ProjectGuid>{B501D075-6183-4E1D-92C9-F7B5002475B1}</ProjectGuid>
<Nullable>enable</Nullable>
<SignAssembly>true</SignAssembly>
Expand All @@ -23,4 +23,4 @@
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" PrivateAssets="All" />
</ItemGroup>
</Project>
</Project>
23 changes: 8 additions & 15 deletions src/Java.Interop.Export/Java.Interop/MarshalMemberBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,6 @@ public string GetJniMethodSignature (JavaCallableAttribute export, MethodInfo me
return export.Signature = GetJniMethodSignature (method);
}

string GetTypeSignature (ParameterInfo p)
{
var info = Runtime.TypeManager.GetTypeSignature (p.ParameterType);
if (info.IsValid)
return info.QualifiedReference;

var marshaler = GetParameterMarshaler (p);
info = Runtime.TypeManager.GetTypeSignature (marshaler.MarshalType);
if (info.IsValid)
return info.QualifiedReference;

throw new NotSupportedException ("Don't know how to determine JNI signature for parameter type: " + p.ParameterType.FullName + ".");
}

Delegate CreateJniMethodMarshaler (MethodInfo method, JavaCallableAttribute? export, Type? type)
{
var e = CreateMarshalToManagedExpression (method, export, type);
Expand Down Expand Up @@ -277,6 +263,7 @@ public LambdaExpression CreateMarshalToManagedExpression (MethodInfo method, Jav
static AssemblyBuilder? assemblyBuilder;
static ModuleBuilder? moduleBuilder;
static Type[]? DelegateCtorSignature;
static Dictionary<string, Type> marshalDelegateTypes;

static Type? CreateMarshalDelegateType (string name, Type? returnType, List<Type> funcTypeParams)
{
Expand All @@ -290,6 +277,10 @@ public LambdaExpression CreateMarshalToManagedExpression (MethodInfo method, Jav
typeof (object),
typeof (IntPtr)
};
marshalDelegateTypes = new ();
}
if (marshalDelegateTypes.TryGetValue (name, out var type)) {
return type;
}
funcTypeParams.Insert (0, typeof (IntPtr));
funcTypeParams.Insert (0, typeof (IntPtr));
Expand All @@ -307,7 +298,9 @@ public LambdaExpression CreateMarshalToManagedExpression (MethodInfo method, Jav
.SetImplementationFlags (ImplAttributes);
typeBuilder.DefineMethod ("Invoke", InvokeAttributes, returnType, funcTypeParams.ToArray ())
.SetImplementationFlags (ImplAttributes);
return typeBuilder.CreateTypeInfo ();
var marshalDelType = typeBuilder.CreateTypeInfo ();
marshalDelegateTypes.Add (name, marshalDelType);
return marshalDelType;
}
}
#endif // NET
Expand Down
5 changes: 5 additions & 0 deletions src/Java.Interop.Tools.Expressions/Class1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Java.Interop.Tools.ExpressionCompiler;

public class Class1
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(DotNetTargetFramework)</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<Import Project="..\..\TargetFrameworkDependentValues.props" />

<PropertyGroup>
<OutputPath>$(UtilityOutputFullPath)</OutputPath>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" PrivateAssets="All" />
</ItemGroup>

<Import Project="..\..\build-tools\scripts\cecil.projitems" />

<ItemGroup>
<ProjectReference Include="..\..\src\Java.Interop.Tools.Cecil\Java.Interop.Tools.Cecil.csproj" />
<ProjectReference Include="..\..\src\Java.Interop.Tools.Diagnostics\Java.Interop.Tools.Diagnostics.csproj" />
</ItemGroup>

</Project>
Loading

0 comments on commit 75b5ce6

Please sign in to comment.