Skip to content

Commit 8f101c8

Browse files
committed
Move the created delegate types
1 parent c42aa91 commit 8f101c8

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

tools/jnimarshalmethod-gen/App.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ void CreateMarshalMethodAssembly (string path)
292292
var destDir = string.IsNullOrEmpty (outDirectory) ? Path.GetDirectoryName (path) : outDirectory;
293293
var builder = CreateExportedMemberBuilder ();
294294
var matchType = typeNameRegexes.Count > 0;
295+
var newDelegates = new List<Type> ();
295296

296297
if (Verbose)
297298
ColorWriteLine ($"Preparing marshal method assembly '{assemblyName}'", ConsoleColor.Cyan);
@@ -421,7 +422,7 @@ void CreateMarshalMethodAssembly (string path)
421422
if (signature == null)
422423
signature = builder.GetJniMethodSignature (method);
423424

424-
registrationElements.Add (CreateRegistration (name, signature, lambda, targetType, methodName, dm));
425+
registrationElements.Add (CreateRegistration (name, signature, lambda, targetType, methodName, dm, newDelegates));
425426

426427
addedMethods.Add (methodName);
427428
}
@@ -443,7 +444,7 @@ void CreateMarshalMethodAssembly (string path)
443444
if (!string.IsNullOrEmpty (outDirectory))
444445
path = Path.Combine (outDirectory, Path.GetFileName (path));
445446

446-
var mover = new TypeMover (dstAssembly, ad, path, definedTypes, resolver, cache);
447+
var mover = new TypeMover (dstAssembly, ad, path, newDelegates, definedTypes, resolver, cache);
447448
mover.Move ();
448449

449450
if (!keepTemporary)
@@ -478,7 +479,7 @@ static void CreateDelegateRuntimeManagedMethod (TypeBuilder tb, string name, Typ
478479
mb.SetImplementationFlags (System.Reflection.MethodImplAttributes.Runtime | System.Reflection.MethodImplAttributes.Managed);
479480
}
480481

481-
static Expression CreateRegistration (string method, string signature, LambdaExpression lambda, ParameterExpression targetType, string methodName, ModuleBuilder dm)
482+
static Expression CreateRegistration (string method, string signature, LambdaExpression lambda, ParameterExpression targetType, string methodName, ModuleBuilder dm, List<Type> createdDelegateList)
482483
{
483484
Expression registrationDelegateType = null;
484485
if (lambda.Type.Assembly == typeof (object).Assembly ||
@@ -521,6 +522,7 @@ static Expression CreateRegistration (string method, string signature, LambdaExp
521522
CreateDelegateRuntimeManagedMethod (dtb, "EndInvoke", typeof (bool), new Type [] { typeof (IAsyncResult) });
522523

523524
existingType = dtb.CreateType ();
525+
createdDelegateList.Add (existingType);
524526
}
525527

526528
delegateTypeName = existingType.FullName;

tools/jnimarshalmethod-gen/TypeMover.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,19 @@ public class TypeMover
1414
AssemblyDefinition Source { get; }
1515
AssemblyDefinition Destination { get; }
1616
string DestinationPath { get; }
17+
List<Type> DelegateTypes { get; }
1718
Dictionary<string, System.Reflection.Emit.TypeBuilder> Types { get; }
1819
DirectoryAssemblyResolver Resolver { get; }
1920

2021
MethodReference consoleWriteLine;
2122
TypeDefinitionCache cache;
2223

23-
public TypeMover (AssemblyDefinition source, AssemblyDefinition destination, string destinationPath, Dictionary<string, System.Reflection.Emit.TypeBuilder> types, DirectoryAssemblyResolver resolver, TypeDefinitionCache cache)
24+
public TypeMover (AssemblyDefinition source, AssemblyDefinition destination, string destinationPath, List<Type> delegateTypes, Dictionary<string, System.Reflection.Emit.TypeBuilder> types, DirectoryAssemblyResolver resolver, TypeDefinitionCache cache)
2425
{
2526
Source = source;
2627
Destination = destination;
2728
DestinationPath = destinationPath;
29+
DelegateTypes = delegateTypes;
2830
Types = types;
2931
Resolver = resolver;
3032
this.cache = cache;
@@ -45,6 +47,11 @@ public void Move ()
4547
typeMap.Clear ();
4648
resolvedTypeMap.Clear ();
4749

50+
foreach (var type in DelegateTypes) {
51+
MoveDelegate (type);
52+
movedTypesCount++;
53+
}
54+
4855
foreach (var type in Types.Values) {
4956
Move (type);
5057
movedTypesCount++;
@@ -71,6 +78,34 @@ bool TypeIsEmptyOrHasOnlyDefaultConstructor (TypeDefinition type)
7178
return !type.HasMethods || (type.Methods.Count == 1 && type.Methods [0].IsConstructor);
7279
}
7380

81+
void MoveDelegate (Type type)
82+
{
83+
var typeSrc = Source.MainModule.GetType (type.GetCecilName ());
84+
var typeDst = new TypeDefinition ("", typeSrc.Name, typeSrc.Attributes);
85+
var module = Destination.MainModule;
86+
87+
if (App.Verbose) {
88+
Console.Write ($"Moving delegate type ");
89+
App.ColorWrite ($"{typeSrc.FullName},{typeSrc.Module.FileName}", ConsoleColor.Yellow);
90+
Console.Write (" to ");
91+
App.ColorWriteLine ($"{Destination.MainModule.FileName}", ConsoleColor.Yellow);
92+
}
93+
94+
typeDst.BaseType = GetUpdatedType (typeSrc.BaseType, module);
95+
96+
foreach (var m in typeSrc.Methods) {
97+
var md = new MethodDefinition (m.Name, m.Attributes, GetUpdatedType (m.ReturnType, module));
98+
md.ImplAttributes = m.ImplAttributes;
99+
100+
foreach (var p in m.Parameters)
101+
md.Parameters.Add (new ParameterDefinition (p.Name, p.Attributes, GetUpdatedType (p.ParameterType, module)));
102+
103+
typeDst.Methods.Add (md);
104+
}
105+
106+
Destination.MainModule.Types.Add (typeDst);
107+
}
108+
74109
void Move (Type type)
75110
{
76111
var typeSrc = Source.MainModule.GetType (type.GetCecilName ());
@@ -343,6 +378,7 @@ MethodReference GetActionConstructor (TypeReference type, ModuleDefinition modul
343378
var mr = GetUpdatedMethod (m, module);
344379
if (type is GenericInstanceType)
345380
mr.DeclaringType = type;
381+
346382
return mr;
347383
}
348384
}

0 commit comments

Comments
 (0)