| 1 |
// Copyright 2007 Alp Toker <[email protected]> |
| 2 |
// This software is made available under the MIT License |
| 3 |
// See COPYING for details |
| 4 |
|
| 5 |
using System; |
| 6 |
using System.Reflection; |
| 7 |
using System.Reflection.Emit; |
| 8 |
using System.Collections.Generic; |
| 9 |
|
| 10 |
namespace NDesk.DBus |
| 11 |
{ |
| 12 |
static class TypeImplementer |
| 13 |
{ |
| 14 |
static AssemblyBuilder asmB; |
| 15 |
static ModuleBuilder modB; |
| 16 |
|
| 17 |
static void InitHack () |
| 18 |
{ |
| 19 |
if (asmB != null) |
| 20 |
return; |
| 21 |
|
| 22 |
asmB = AppDomain.CurrentDomain.DefineDynamicAssembly (new AssemblyName ("NDesk.DBus.Proxies"), AssemblyBuilderAccess.Run); |
| 23 |
modB = asmB.DefineDynamicModule ("ProxyModule"); |
| 24 |
} |
| 25 |
|
| 26 |
static Dictionary<Type,Type> map = new Dictionary<Type,Type> (); |
| 27 |
|
| 28 |
public static Type GetImplementation (Type declType) |
| 29 |
{ |
| 30 |
Type retT; |
| 31 |
|
| 32 |
if (map.TryGetValue (declType, out retT)) |
| 33 |
return retT; |
| 34 |
|
| 35 |
InitHack (); |
| 36 |
|
| 37 |
TypeBuilder typeB = modB.DefineType (declType.Name + "Proxy", TypeAttributes.Class | TypeAttributes.Public, typeof (BusObject)); |
| 38 |
|
| 39 |
Implement (typeB, declType); |
| 40 |
|
| 41 |
foreach (Type iface in declType.GetInterfaces ()) |
| 42 |
Implement (typeB, iface); |
| 43 |
|
| 44 |
retT = typeB.CreateType (); |
| 45 |
map[declType] = retT; |
| 46 |
|
| 47 |
return retT; |
| 48 |
} |
| 49 |
|
| 50 |
public static void Implement (TypeBuilder typeB, Type iface) |
| 51 |
{ |
| 52 |
typeB.AddInterfaceImplementation (iface); |
| 53 |
|
| 54 |
foreach (MethodInfo declMethod in iface.GetMethods ()) { |
| 55 |
ParameterInfo[] parms = declMethod.GetParameters (); |
| 56 |
|
| 57 |
Type[] parmTypes = new Type[parms.Length]; |
| 58 |
for (int i = 0 ; i < parms.Length ; i++) |
| 59 |
parmTypes[i] = parms[i].ParameterType; |
| 60 |
|
| 61 |
MethodAttributes attrs = declMethod.Attributes ^ MethodAttributes.Abstract; |
| 62 |
MethodBuilder method_builder = typeB.DefineMethod (declMethod.Name, attrs, declMethod.ReturnType, parmTypes); |
| 63 |
typeB.DefineMethodOverride (method_builder, declMethod); |
| 64 |
|
| 65 |
//define in/out/ref/name for each of the parameters |
| 66 |
for (int i = 0; i < parms.Length ; i++) |
| 67 |
method_builder.DefineParameter (i, parms[i].Attributes, parms[i].Name); |
| 68 |
|
| 69 |
ILGenerator ilg = method_builder.GetILGenerator (); |
| 70 |
GenHookupMethod (ilg, declMethod, sendMethodCallMethod, Mapper.GetInterfaceName (iface), declMethod.Name); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
static MethodInfo sendMethodCallMethod = typeof (BusObject).GetMethod ("SendMethodCall"); |
| 75 |
static MethodInfo sendSignalMethod = typeof (BusObject).GetMethod ("SendSignal"); |
| 76 |
static MethodInfo toggleSignalMethod = typeof (BusObject).GetMethod ("ToggleSignal"); |
| 77 |
|
| 78 |
static Dictionary<EventInfo,DynamicMethod> hookup_methods = new Dictionary<EventInfo,DynamicMethod> (); |
| 79 |
public static DynamicMethod GetHookupMethod (EventInfo ei) |
| 80 |
{ |
| 81 |
DynamicMethod hookupMethod; |
| 82 |
if (hookup_methods.TryGetValue (ei, out hookupMethod)) |
| 83 |
return hookupMethod; |
| 84 |
|
| 85 |
if (ei.EventHandlerType.IsAssignableFrom (typeof (System.EventHandler))) |
| 86 |
Console.Error.WriteLine ("Warning: Cannot yet fully expose EventHandler and its subclasses: " + ei.EventHandlerType); |
| 87 |
|
| 88 |
MethodInfo declMethod = ei.EventHandlerType.GetMethod ("Invoke"); |
| 89 |
|
| 90 |
hookupMethod = GetHookupMethod (declMethod, sendSignalMethod, Mapper.GetInterfaceName (ei), ei.Name); |
| 91 |
|
| 92 |
hookup_methods[ei] = hookupMethod; |
| 93 |
|
| 94 |
return hookupMethod; |
| 95 |
} |
| 96 |
|
| 97 |
public static DynamicMethod GetHookupMethod (MethodInfo declMethod, MethodInfo invokeMethod, string @interface, string member) |
| 98 |
{ |
| 99 |
ParameterInfo[] delegateParms = declMethod.GetParameters (); |
| 100 |
Type[] hookupParms = new Type[delegateParms.Length+1]; |
| 101 |
hookupParms[0] = typeof (BusObject); |
| 102 |
for (int i = 0; i < delegateParms.Length ; i++) |
| 103 |
hookupParms[i+1] = delegateParms[i].ParameterType; |
| 104 |
|
| 105 |
DynamicMethod hookupMethod = new DynamicMethod ("Handle" + member, declMethod.ReturnType, hookupParms, typeof (MessageWriter)); |
| 106 |
|
| 107 |
ILGenerator ilg = hookupMethod.GetILGenerator (); |
| 108 |
|
| 109 |
GenHookupMethod (ilg, declMethod, invokeMethod, @interface, member); |
| 110 |
|
| 111 |
return hookupMethod; |
| 112 |
} |
| 113 |
|
| 114 |
//static MethodInfo getMethodFromHandleMethod = typeof (MethodBase).GetMethod ("GetMethodFromHandle", new Type[] {typeof (RuntimeMethodHandle)}); |
| 115 |
static MethodInfo getTypeFromHandleMethod = typeof (Type).GetMethod ("GetTypeFromHandle", new Type[] {typeof (RuntimeTypeHandle)}); |
| 116 |
static ConstructorInfo argumentNullExceptionConstructor = typeof (ArgumentNullException).GetConstructor (new Type[] {typeof (string)}); |
| 117 |
static ConstructorInfo messageWriterConstructor = typeof (MessageWriter).GetConstructor (Type.EmptyTypes); |
| 118 |
static MethodInfo messageWriterWriteMethod = typeof (MessageWriter).GetMethod ("WriteComplex", new Type[] {typeof (object), typeof (Type)}); |
| 119 |
static MethodInfo messageWriterWritePad = typeof (MessageWriter).GetMethod ("WritePad", new Type[] {typeof (int)}); |
| 120 |
|
| 121 |
static Dictionary<Type,MethodInfo> writeMethods = new Dictionary<Type,MethodInfo> (); |
| 122 |
|
| 123 |
public static MethodInfo GetWriteMethod (Type t) |
| 124 |
{ |
| 125 |
MethodInfo meth; |
| 126 |
|
| 127 |
if (writeMethods.TryGetValue (t, out meth)) |
| 128 |
return meth; |
| 129 |
|
| 130 |
/* |
| 131 |
Type tUnder = t; |
| 132 |
if (t.IsEnum) |
| 133 |
tUnder = Enum.GetUnderlyingType (t); |
| 134 |
|
| 135 |
meth = typeof (MessageWriter).GetMethod ("Write", BindingFlags.ExactBinding | BindingFlags.Instance | BindingFlags.Public, null, new Type[] {tUnder}, null); |
| 136 |
if (meth != null) { |
| 137 |
writeMethods[t] = meth; |
| 138 |
return meth; |
| 139 |
} |
| 140 |
*/ |
| 141 |
|
| 142 |
DynamicMethod method_builder = new DynamicMethod ("Write" + t.Name, typeof (void), new Type[] {typeof (MessageWriter), t}, typeof (MessageWriter)); |
| 143 |
ILGenerator ilg = method_builder.GetILGenerator (); |
| 144 |
|
| 145 |
ilg.Emit (OpCodes.Ldarg_0); |
| 146 |
ilg.Emit (OpCodes.Ldarg_1); |
| 147 |
|
| 148 |
GenMarshalWrite (ilg, t); |
| 149 |
|
| 150 |
ilg.Emit (OpCodes.Ret); |
| 151 |
|
| 152 |
meth = method_builder; |
| 153 |
|
| 154 |
writeMethods[t] = meth; |
| 155 |
return meth; |
| 156 |
} |
| 157 |
|
| 158 |
//takes the Writer instance and the value of Type t off the stack, writes it |
| 159 |
public static void GenWriter (ILGenerator ilg, Type t) |
| 160 |
{ |
| 161 |
Type tUnder = t; |
| 162 |
//bool imprecise = false; |
| 163 |
|
| 164 |
if (t.IsEnum) { |
| 165 |
tUnder = Enum.GetUnderlyingType (t); |
| 166 |
//imprecise = true; |
| 167 |
} |
| 168 |
|
| 169 |
//MethodInfo exactWriteMethod = typeof (MessageWriter).GetMethod ("Write", new Type[] {tUnder}); |
| 170 |
MethodInfo exactWriteMethod = typeof (MessageWriter).GetMethod ("Write", BindingFlags.ExactBinding | BindingFlags.Instance | BindingFlags.Public, null, new Type[] {tUnder}, null); |
| 171 |
//ExactBinding InvokeMethod |
| 172 |
|
| 173 |
if (exactWriteMethod != null) { |
| 174 |
//if (imprecise) |
| 175 |
// ilg.Emit (OpCodes.Castclass, tUnder); |
| 176 |
|
| 177 |
ilg.Emit (exactWriteMethod.IsFinal ? OpCodes.Call : OpCodes.Callvirt, exactWriteMethod); |
| 178 |
} else { |
| 179 |
//..boxed if necessary |
| 180 |
if (t.IsValueType) |
| 181 |
ilg.Emit (OpCodes.Box, t); |
| 182 |
|
| 183 |
//the Type parameter |
| 184 |
ilg.Emit (OpCodes.Ldtoken, t); |
| 185 |
ilg.Emit (OpCodes.Call, getTypeFromHandleMethod); |
| 186 |
|
| 187 |
ilg.Emit (messageWriterWriteMethod.IsFinal ? OpCodes.Call : OpCodes.Callvirt, messageWriterWriteMethod); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
//takes a writer and a reference to an object off the stack |
| 192 |
public static void GenMarshalWrite (ILGenerator ilg, Type type) |
| 193 |
{ |
| 194 |
LocalBuilder val = ilg.DeclareLocal (type); |
| 195 |
ilg.Emit (OpCodes.Stloc, val); |
| 196 |
|
| 197 |
LocalBuilder writer = ilg.DeclareLocal (typeof (MessageWriter)); |
| 198 |
ilg.Emit (OpCodes.Stloc, writer); |
| 199 |
|
| 200 |
FieldInfo[] fis = type.GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); |
| 201 |
|
| 202 |
//align to 8 for structs |
| 203 |
ilg.Emit (OpCodes.Ldloc, writer); |
| 204 |
ilg.Emit (OpCodes.Ldc_I4, 8); |
| 205 |
ilg.Emit (messageWriterWritePad.IsFinal ? OpCodes.Call : OpCodes.Callvirt, messageWriterWritePad); |
| 206 |
|
| 207 |
foreach (FieldInfo fi in fis) { |
| 208 |
Type t = fi.FieldType; |
| 209 |
|
| 210 |
//the Writer to write to |
| 211 |
ilg.Emit (OpCodes.Ldloc, writer); |
| 212 |
|
| 213 |
//the object parameter |
| 214 |
ilg.Emit (OpCodes.Ldloc, val); |
| 215 |
ilg.Emit (OpCodes.Ldfld, fi); |
| 216 |
|
| 217 |
GenWriter (ilg, t); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
public static void GenHookupMethod (ILGenerator ilg, MethodInfo declMethod, MethodInfo invokeMethod, string @interface, string member) |
| 222 |
{ |
| 223 |
ParameterInfo[] parms = declMethod.GetParameters (); |
| 224 |
Type retType = declMethod.ReturnType; |
| 225 |
|
| 226 |
//the BusObject instance |
| 227 |
ilg.Emit (OpCodes.Ldarg_0); |
| 228 |
|
| 229 |
//MethodInfo |
| 230 |
/* |
| 231 |
ilg.Emit (OpCodes.Ldtoken, declMethod); |
| 232 |
ilg.Emit (OpCodes.Call, getMethodFromHandleMethod); |
| 233 |
*/ |
| 234 |
|
| 235 |
//interface |
| 236 |
ilg.Emit (OpCodes.Ldstr, @interface); |
| 237 |
|
| 238 |
//special case event add/remove methods |
| 239 |
if (declMethod.IsSpecialName && (declMethod.Name.StartsWith ("add_") || declMethod.Name.StartsWith ("remove_"))) { |
| 240 |
string[] parts = declMethod.Name.Split (new char[]{'_'}, 2); |
| 241 |
string ename = parts[1]; |
| 242 |
//Delegate dlg = (Delegate)inArgs[0]; |
| 243 |
bool adding = parts[0] == "add"; |
| 244 |
|
| 245 |
ilg.Emit (OpCodes.Ldstr, ename); |
| 246 |
|
| 247 |
ilg.Emit (OpCodes.Ldarg_1); |
| 248 |
|
| 249 |
ilg.Emit (OpCodes.Ldc_I4, adding ? 1 : 0); |
| 250 |
|
| 251 |
ilg.Emit (OpCodes.Tailcall); |
| 252 |
ilg.Emit (toggleSignalMethod.IsFinal ? OpCodes.Call : OpCodes.Callvirt, toggleSignalMethod); |
| 253 |
ilg.Emit (OpCodes.Ret); |
| 254 |
return; |
| 255 |
} |
| 256 |
|
| 257 |
//property accessor mapping |
| 258 |
if (declMethod.IsSpecialName) { |
| 259 |
if (member.StartsWith ("get_")) |
| 260 |
member = "Get" + member.Substring (4); |
| 261 |
else if (member.StartsWith ("set_")) |
| 262 |
member = "Set" + member.Substring (4); |
| 263 |
} |
| 264 |
|
| 265 |
//member |
| 266 |
ilg.Emit (OpCodes.Ldstr, member); |
| 267 |
|
| 268 |
//signature |
| 269 |
Signature inSig = Signature.Empty; |
| 270 |
Signature outSig = Signature.Empty; |
| 271 |
|
| 272 |
if (!declMethod.IsSpecialName) |
| 273 |
foreach (ParameterInfo parm in parms) |
| 274 |
{ |
| 275 |
if (parm.IsOut) |
| 276 |
outSig += Signature.GetSig (parm.ParameterType.GetElementType ()); |
| 277 |
else |
| 278 |
inSig += Signature.GetSig (parm.ParameterType); |
| 279 |
} |
| 280 |
|
| 281 |
ilg.Emit (OpCodes.Ldstr, inSig.Value); |
| 282 |
|
| 283 |
LocalBuilder writer = ilg.DeclareLocal (typeof (MessageWriter)); |
| 284 |
ilg.Emit (OpCodes.Newobj, messageWriterConstructor); |
| 285 |
ilg.Emit (OpCodes.Stloc, writer); |
| 286 |
|
| 287 |
foreach (ParameterInfo parm in parms) |
| 288 |
{ |
| 289 |
if (parm.IsOut) |
| 290 |
continue; |
| 291 |
|
| 292 |
Type t = parm.ParameterType; |
| 293 |
//offset by one to account for "this" |
| 294 |
int i = parm.Position + 1; |
| 295 |
|
| 296 |
//null checking of parameters (but not their recursive contents) |
| 297 |
if (!t.IsValueType) { |
| 298 |
Label notNull = ilg.DefineLabel (); |
| 299 |
|
| 300 |
//if the value is null... |
| 301 |
ilg.Emit (OpCodes.Ldarg, i); |
| 302 |
ilg.Emit (OpCodes.Brtrue_S, notNull); |
| 303 |
|
| 304 |
//...throw Exception |
| 305 |
string paramName = parm.Name; |
| 306 |
ilg.Emit (OpCodes.Ldstr, paramName); |
| 307 |
ilg.Emit (OpCodes.Newobj, argumentNullExceptionConstructor); |
| 308 |
ilg.Emit (OpCodes.Throw); |
| 309 |
|
| 310 |
//was not null, so all is well |
| 311 |
ilg.MarkLabel (notNull); |
| 312 |
} |
| 313 |
|
| 314 |
ilg.Emit (OpCodes.Ldloc, writer); |
| 315 |
|
| 316 |
//the parameter |
| 317 |
ilg.Emit (OpCodes.Ldarg, i); |
| 318 |
|
| 319 |
GenWriter (ilg, t); |
| 320 |
} |
| 321 |
|
| 322 |
ilg.Emit (OpCodes.Ldloc, writer); |
| 323 |
|
| 324 |
//the expected return Type |
| 325 |
ilg.Emit (OpCodes.Ldtoken, retType); |
| 326 |
ilg.Emit (OpCodes.Call, getTypeFromHandleMethod); |
| 327 |
|
| 328 |
LocalBuilder exc = ilg.DeclareLocal (typeof (Exception)); |
| 329 |
ilg.Emit (OpCodes.Ldloca_S, exc); |
| 330 |
|
| 331 |
//make the call |
| 332 |
ilg.Emit (invokeMethod.IsFinal ? OpCodes.Call : OpCodes.Callvirt, invokeMethod); |
| 333 |
|
| 334 |
//define a label we'll use to deal with a non-null Exception |
| 335 |
Label noErr = ilg.DefineLabel (); |
| 336 |
|
| 337 |
//if the out Exception is not null... |
| 338 |
ilg.Emit (OpCodes.Ldloc, exc); |
| 339 |
ilg.Emit (OpCodes.Brfalse_S, noErr); |
| 340 |
|
| 341 |
//...throw it. |
| 342 |
ilg.Emit (OpCodes.Ldloc, exc); |
| 343 |
ilg.Emit (OpCodes.Throw); |
| 344 |
|
| 345 |
//Exception was null, so all is well |
| 346 |
ilg.MarkLabel (noErr); |
| 347 |
|
| 348 |
if (retType == typeof (void)) { |
| 349 |
//we aren't expecting a return value, so throw away the (hopefully) null return |
| 350 |
if (invokeMethod.ReturnType != typeof (void)) |
| 351 |
ilg.Emit (OpCodes.Pop); |
| 352 |
} else { |
| 353 |
if (retType.IsValueType) |
| 354 |
ilg.Emit (OpCodes.Unbox_Any, retType); |
| 355 |
else |
| 356 |
ilg.Emit (OpCodes.Castclass, retType); |
| 357 |
} |
| 358 |
|
| 359 |
ilg.Emit (OpCodes.Ret); |
| 360 |
} |
| 361 |
} |
| 362 |
} |