Skip to content

Commit 75b5ce6

Browse files
committed
[Java.Interop.Tools.Expressions] Add Java.Interop.Tools.Expressions
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. :-/
1 parent 5318261 commit 75b5ce6

File tree

12 files changed

+1562
-81
lines changed

12 files changed

+1562
-81
lines changed

Java.Interop.sln

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Java.Base", "src\Java.Base\
109109
EndProject
110110
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Java.Base-Tests", "tests\Java.Base-Tests\Java.Base-Tests.csproj", "{CB05E11B-B96F-4179-A4E9-5D6BDE29A8FC}"
111111
EndProject
112+
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}"
113+
EndProject
114+
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}"
115+
EndProject
112116
Global
113117
GlobalSection(SharedMSBuildProjectFiles) = preSolution
114118
src\Java.Interop.NamingCustomAttributes\Java.Interop.NamingCustomAttributes.projitems*{58b564a1-570d-4da2-b02d-25bddb1a9f4f}*SharedItemsImports = 5
@@ -308,6 +312,14 @@ Global
308312
{CB05E11B-B96F-4179-A4E9-5D6BDE29A8FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
309313
{CB05E11B-B96F-4179-A4E9-5D6BDE29A8FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
310314
{CB05E11B-B96F-4179-A4E9-5D6BDE29A8FC}.Release|Any CPU.Build.0 = Release|Any CPU
315+
{1A0262FE-3CDB-4AF2-AAD8-65C59524FE8A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
316+
{1A0262FE-3CDB-4AF2-AAD8-65C59524FE8A}.Debug|Any CPU.Build.0 = Debug|Any CPU
317+
{1A0262FE-3CDB-4AF2-AAD8-65C59524FE8A}.Release|Any CPU.ActiveCfg = Release|Any CPU
318+
{1A0262FE-3CDB-4AF2-AAD8-65C59524FE8A}.Release|Any CPU.Build.0 = Release|Any CPU
319+
{211BAA88-66B1-41B2-88B2-530DBD8DF702}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
320+
{211BAA88-66B1-41B2-88B2-530DBD8DF702}.Debug|Any CPU.Build.0 = Debug|Any CPU
321+
{211BAA88-66B1-41B2-88B2-530DBD8DF702}.Release|Any CPU.ActiveCfg = Release|Any CPU
322+
{211BAA88-66B1-41B2-88B2-530DBD8DF702}.Release|Any CPU.Build.0 = Release|Any CPU
311323
EndGlobalSection
312324
GlobalSection(SolutionProperties) = preSolution
313325
HideSolutionNode = FALSE
@@ -360,6 +372,8 @@ Global
360372
{11942DE9-AEC2-4B95-87AB-CA707C37643D} = {271C9F30-F679-4793-942B-0D9527CB3E2F}
361373
{30DCECA5-16FD-4FD0-883C-E5E83B11565D} = {0998E45F-8BCE-4791-A944-962CD54E2D80}
362374
{CB05E11B-B96F-4179-A4E9-5D6BDE29A8FC} = {271C9F30-F679-4793-942B-0D9527CB3E2F}
375+
{1A0262FE-3CDB-4AF2-AAD8-65C59524FE8A} = {0998E45F-8BCE-4791-A944-962CD54E2D80}
376+
{211BAA88-66B1-41B2-88B2-530DBD8DF702} = {271C9F30-F679-4793-942B-0D9527CB3E2F}
363377
EndGlobalSection
364378
GlobalSection(ExtensibilityGlobals) = postSolution
365379
SolutionGuid = {29204E0C-382A-49A0-A814-AD7FBF9774A5}

src/Java.Interop.Export/Java.Interop.Export.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Project Sdk="Microsoft.NET.Sdk">
33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;$(DotNetTargetFramework)</TargetFrameworks>
5-
<LangVersion>8.0</LangVersion>
5+
<LangVersion>9.0</LangVersion>
66
<ProjectGuid>{B501D075-6183-4E1D-92C9-F7B5002475B1}</ProjectGuid>
77
<Nullable>enable</Nullable>
88
<SignAssembly>true</SignAssembly>
@@ -23,4 +23,4 @@
2323
<ItemGroup>
2424
<PackageReference Include="Microsoft.SourceLink.GitHub" PrivateAssets="All" />
2525
</ItemGroup>
26-
</Project>
26+
</Project>

src/Java.Interop.Export/Java.Interop/MarshalMemberBuilder.cs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,6 @@ public string GetJniMethodSignature (JavaCallableAttribute export, MethodInfo me
8484
return export.Signature = GetJniMethodSignature (method);
8585
}
8686

87-
string GetTypeSignature (ParameterInfo p)
88-
{
89-
var info = Runtime.TypeManager.GetTypeSignature (p.ParameterType);
90-
if (info.IsValid)
91-
return info.QualifiedReference;
92-
93-
var marshaler = GetParameterMarshaler (p);
94-
info = Runtime.TypeManager.GetTypeSignature (marshaler.MarshalType);
95-
if (info.IsValid)
96-
return info.QualifiedReference;
97-
98-
throw new NotSupportedException ("Don't know how to determine JNI signature for parameter type: " + p.ParameterType.FullName + ".");
99-
}
100-
10187
Delegate CreateJniMethodMarshaler (MethodInfo method, JavaCallableAttribute? export, Type? type)
10288
{
10389
var e = CreateMarshalToManagedExpression (method, export, type);
@@ -277,6 +263,7 @@ public LambdaExpression CreateMarshalToManagedExpression (MethodInfo method, Jav
277263
static AssemblyBuilder? assemblyBuilder;
278264
static ModuleBuilder? moduleBuilder;
279265
static Type[]? DelegateCtorSignature;
266+
static Dictionary<string, Type> marshalDelegateTypes;
280267

281268
static Type? CreateMarshalDelegateType (string name, Type? returnType, List<Type> funcTypeParams)
282269
{
@@ -290,6 +277,10 @@ public LambdaExpression CreateMarshalToManagedExpression (MethodInfo method, Jav
290277
typeof (object),
291278
typeof (IntPtr)
292279
};
280+
marshalDelegateTypes = new ();
281+
}
282+
if (marshalDelegateTypes.TryGetValue (name, out var type)) {
283+
return type;
293284
}
294285
funcTypeParams.Insert (0, typeof (IntPtr));
295286
funcTypeParams.Insert (0, typeof (IntPtr));
@@ -307,7 +298,9 @@ public LambdaExpression CreateMarshalToManagedExpression (MethodInfo method, Jav
307298
.SetImplementationFlags (ImplAttributes);
308299
typeBuilder.DefineMethod ("Invoke", InvokeAttributes, returnType, funcTypeParams.ToArray ())
309300
.SetImplementationFlags (ImplAttributes);
310-
return typeBuilder.CreateTypeInfo ();
301+
var marshalDelType = typeBuilder.CreateTypeInfo ();
302+
marshalDelegateTypes.Add (name, marshalDelType);
303+
return marshalDelType;
311304
}
312305
}
313306
#endif // NET
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace Java.Interop.Tools.ExpressionCompiler;
2+
3+
public class Class1
4+
{
5+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>$(DotNetTargetFramework)</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<Import Project="..\..\TargetFrameworkDependentValues.props" />
10+
11+
<PropertyGroup>
12+
<OutputPath>$(UtilityOutputFullPath)</OutputPath>
13+
</PropertyGroup>
14+
15+
<ItemGroup>
16+
<PackageReference Include="Microsoft.SourceLink.GitHub" PrivateAssets="All" />
17+
</ItemGroup>
18+
19+
<Import Project="..\..\build-tools\scripts\cecil.projitems" />
20+
21+
<ItemGroup>
22+
<ProjectReference Include="..\..\src\Java.Interop.Tools.Cecil\Java.Interop.Tools.Cecil.csproj" />
23+
<ProjectReference Include="..\..\src\Java.Interop.Tools.Diagnostics\Java.Interop.Tools.Diagnostics.csproj" />
24+
</ItemGroup>
25+
26+
</Project>

0 commit comments

Comments
 (0)