CommonBindingGenerator.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.Linq;
  5. using System.Text;
  6. using ILRuntime.Runtime.Enviorment;
  7. namespace ILRuntime.Runtime.CLRBinding
  8. {
  9. static class CommonBindingGenerator
  10. {
  11. internal static string GenerateMiscRegisterCode(this Type type, string typeClsName, bool defaultCtor, bool newArr)
  12. {
  13. StringBuilder sb = new StringBuilder();
  14. if (defaultCtor && !type.IsPrimitive && !type.IsAbstract)
  15. {
  16. var constructorFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly;
  17. var hasDefaultConstructor = type.GetConstructor(constructorFlags, null, new Type[0], null) != null;
  18. if (hasDefaultConstructor || type.IsValueType)
  19. {
  20. sb.AppendLine(string.Format(" app.RegisterCLRCreateDefaultInstance(type, () => new {0}());", typeClsName));
  21. }
  22. }
  23. if (newArr)
  24. {
  25. if (!type.IsAbstract || !type.IsSealed)
  26. {
  27. if (type.IsArray)
  28. {
  29. Type elementType = type;
  30. int arrCnt = 0;
  31. while (elementType.IsArray)
  32. {
  33. elementType = elementType.GetElementType();
  34. arrCnt++;
  35. }
  36. string elem, clsName;
  37. bool isByRef;
  38. elementType.GetClassName(out clsName, out elem, out isByRef);
  39. string trail = "";
  40. for (int i = 0; i < arrCnt; i++)
  41. trail += "[]";
  42. sb.AppendLine(string.Format(" app.RegisterCLRCreateArrayInstance(type, s => new {0}[s]{1});", elem, trail));
  43. }
  44. else
  45. sb.AppendLine(string.Format(" app.RegisterCLRCreateArrayInstance(type, s => new {0}[s]);", typeClsName));
  46. }
  47. }
  48. return sb.ToString();
  49. }
  50. internal static string GenerateCommonCode(this Type type, string typeClsName)
  51. {
  52. if (!type.IsValueType)
  53. return "";
  54. StringBuilder sb = new StringBuilder();
  55. if (type.IsPrimitive)
  56. {
  57. sb.AppendLine(string.Format(" static {0} GetInstance(ILRuntime.Runtime.Enviorment.AppDomain __domain, StackObject* ptr_of_this_method, IList<object> __mStack)", typeClsName));
  58. sb.AppendLine(" {");
  59. if (type.IsPrimitive || type.IsValueType)
  60. sb.AppendLine(" ptr_of_this_method = ILIntepreter.GetObjectAndResolveReference(ptr_of_this_method);");
  61. sb.AppendLine(string.Format(" {0} instance_of_this_method;", typeClsName));
  62. sb.Append(@" switch(ptr_of_this_method->ObjectType)
  63. {
  64. case ObjectTypes.FieldReference:
  65. {
  66. var instance_of_fieldReference = __mStack[ptr_of_this_method->Value];
  67. if(instance_of_fieldReference is ILTypeInstance)
  68. {
  69. instance_of_this_method = (");
  70. sb.Append(typeClsName);
  71. sb.Append(")typeof(");
  72. sb.Append(typeClsName);
  73. sb.Append(").CheckCLRTypes(((ILTypeInstance)instance_of_fieldReference)[ptr_of_this_method->ValueLow])");
  74. sb.Append(";");
  75. sb.Append(@"
  76. }
  77. else
  78. {
  79. var t = __domain.GetType(instance_of_fieldReference.GetType()) as CLRType;
  80. instance_of_this_method = (");
  81. sb.Append(typeClsName);
  82. sb.Append(")t.GetFieldValue(ptr_of_this_method->ValueLow, instance_of_fieldReference);");
  83. sb.Append(@"
  84. }
  85. }
  86. break;
  87. case ObjectTypes.StaticFieldReference:
  88. {
  89. var t = __domain.GetType(ptr_of_this_method->Value);
  90. if(t is ILType)
  91. {
  92. instance_of_this_method = (");
  93. sb.Append(typeClsName);
  94. sb.Append(")typeof(");
  95. sb.Append(typeClsName);
  96. sb.Append(").CheckCLRTypes(((ILType)t).StaticInstance[ptr_of_this_method->ValueLow])");
  97. sb.Append(";");
  98. sb.Append(@"
  99. }
  100. else
  101. {
  102. instance_of_this_method = (");
  103. sb.Append(typeClsName);
  104. sb.Append(@")((CLRType)t).GetFieldValue(ptr_of_this_method->ValueLow, null);
  105. }
  106. }
  107. break;
  108. case ObjectTypes.ArrayReference:
  109. {
  110. var instance_of_arrayReference = __mStack[ptr_of_this_method->Value] as ");
  111. sb.Append(typeClsName);
  112. sb.AppendLine(@"[];
  113. instance_of_this_method = instance_of_arrayReference[ptr_of_this_method->ValueLow];
  114. }
  115. break;
  116. default:");
  117. sb.AppendLine(string.Format(" instance_of_this_method = {0};", type.GetRetrieveValueCode(typeClsName)));
  118. sb.AppendLine(@" break;
  119. }
  120. return instance_of_this_method;");
  121. sb.AppendLine(" }");
  122. }
  123. if (!type.IsPrimitive && !type.IsAbstract)
  124. {
  125. sb.AppendLine(string.Format(" static void WriteBackInstance(ILRuntime.Runtime.Enviorment.AppDomain __domain, StackObject* ptr_of_this_method, IList<object> __mStack, ref {0} instance_of_this_method)", typeClsName));
  126. sb.AppendLine(" {");
  127. sb.AppendLine(@" ptr_of_this_method = ILIntepreter.GetObjectAndResolveReference(ptr_of_this_method);
  128. switch(ptr_of_this_method->ObjectType)
  129. {
  130. case ObjectTypes.Object:
  131. {
  132. __mStack[ptr_of_this_method->Value] = instance_of_this_method;");
  133. sb.Append(@" }
  134. break;
  135. case ObjectTypes.FieldReference:
  136. {
  137. var ___obj = __mStack[ptr_of_this_method->Value];
  138. if(___obj is ILTypeInstance)
  139. {
  140. ((ILTypeInstance)___obj)[ptr_of_this_method->ValueLow] = instance_of_this_method");
  141. sb.Append(@";
  142. }
  143. else
  144. {
  145. var t = __domain.GetType(___obj.GetType()) as CLRType;
  146. t.SetFieldValue(ptr_of_this_method->ValueLow, ref ___obj, instance_of_this_method");
  147. sb.Append(@");
  148. }
  149. }
  150. break;
  151. case ObjectTypes.StaticFieldReference:
  152. {
  153. var t = __domain.GetType(ptr_of_this_method->Value);
  154. if(t is ILType)
  155. {
  156. ((ILType)t).StaticInstance[ptr_of_this_method->ValueLow] = instance_of_this_method");
  157. sb.Append(@";
  158. }
  159. else
  160. {
  161. ((CLRType)t).SetStaticFieldValue(ptr_of_this_method->ValueLow, instance_of_this_method");
  162. sb.Append(@");
  163. }
  164. }
  165. break;
  166. case ObjectTypes.ArrayReference:
  167. {
  168. var instance_of_arrayReference = __mStack[ptr_of_this_method->Value] as ");
  169. sb.Append(typeClsName);
  170. sb.AppendLine(@"[];
  171. instance_of_arrayReference[ptr_of_this_method->ValueLow] = instance_of_this_method;
  172. }
  173. break;
  174. }");
  175. sb.AppendLine(@" }");
  176. }
  177. return sb.ToString();
  178. }
  179. }
  180. }