FieldBindingGenerator.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 FieldBindingGenerator
  10. {
  11. internal static string GenerateFieldRegisterCode(this Type type, FieldInfo[] fields, HashSet<FieldInfo> excludes)
  12. {
  13. StringBuilder sb = new StringBuilder();
  14. int idx = 0;
  15. foreach (var i in fields)
  16. {
  17. if (excludes != null && excludes.Contains(i))
  18. continue;
  19. if (type.ShouldSkipField(i))
  20. continue;
  21. if (i.IsSpecialName)
  22. continue;
  23. sb.AppendLine(string.Format(" field = type.GetField(\"{0}\", flag);", i.Name));
  24. sb.AppendLine(string.Format(" app.RegisterCLRFieldGetter(field, get_{0}_{1});", i.Name, idx));
  25. if (!i.IsInitOnly && !i.IsLiteral)
  26. {
  27. sb.AppendLine(string.Format(" app.RegisterCLRFieldSetter(field, set_{0}_{1});", i.Name, idx));
  28. sb.AppendLine(string.Format(" app.RegisterCLRFieldBinding(field, CopyToStack_{0}_{1}, AssignFromStack_{0}_{1});", i.Name, idx));
  29. }
  30. else
  31. {
  32. sb.AppendLine(string.Format(" app.RegisterCLRFieldBinding(field, CopyToStack_{0}_{1}, null);", i.Name, idx));
  33. }
  34. idx++;
  35. }
  36. return sb.ToString();
  37. }
  38. internal static string GenerateFieldWraperCode(this Type type, FieldInfo[] fields, string typeClsName, HashSet<FieldInfo> excludes, List<Type> valueTypeBinders, Enviorment.AppDomain domain)
  39. {
  40. StringBuilder sb = new StringBuilder();
  41. int idx = 0;
  42. foreach (var i in fields)
  43. {
  44. if (excludes != null && excludes.Contains(i))
  45. continue;
  46. if (type.ShouldSkipField(i))
  47. continue;
  48. sb.AppendLine(string.Format(" static object get_{0}_{1}(ref object o)", i.Name, idx));
  49. sb.AppendLine(" {");
  50. if (i.IsStatic)
  51. {
  52. sb.AppendLine(string.Format(" return {0}.{1};", typeClsName, i.Name));
  53. }
  54. else
  55. {
  56. sb.AppendLine(string.Format(" return (({0})o).{1};", typeClsName, i.Name));
  57. }
  58. sb.AppendLine(" }");
  59. sb.AppendLine();
  60. sb.AppendLine(string.Format(" static StackObject* CopyToStack_{0}_{1}(ref object o, ILIntepreter __intp, StackObject* __ret, IList<object> __mStack)", i.Name, idx));
  61. sb.AppendLine(" {");
  62. if (i.IsStatic)
  63. {
  64. sb.AppendLine(string.Format(" var result_of_this_method = {0}.{1};", typeClsName, i.Name));
  65. }
  66. else
  67. {
  68. sb.AppendLine(string.Format(" var result_of_this_method = (({0})o).{1};", typeClsName, i.Name));
  69. }
  70. string clsName, realClsName;
  71. bool isByRef;
  72. i.FieldType.GetClassName(out clsName, out realClsName, out isByRef);
  73. if (i.FieldType.IsValueType && !i.FieldType.IsPrimitive && valueTypeBinders != null && valueTypeBinders.Contains(i.FieldType))
  74. {
  75. sb.AppendLine(string.Format(" if (ILRuntime.Runtime.Generated.CLRBindings.s_{0}_Binder != null) {{", clsName));
  76. sb.AppendLine(string.Format(" ILRuntime.Runtime.Generated.CLRBindings.s_{0}_Binder.PushValue(ref result_of_this_method, __intp, __ret, __mStack);", clsName));
  77. sb.AppendLine(" return __ret + 1;");
  78. sb.AppendLine(" } else {");
  79. i.FieldType.GetReturnValueCode(sb, domain);
  80. sb.AppendLine(" }");
  81. }
  82. else
  83. {
  84. i.FieldType.GetReturnValueCode(sb, domain);
  85. }
  86. sb.AppendLine(" }");
  87. sb.AppendLine();
  88. if (!i.IsInitOnly && !i.IsLiteral)
  89. {
  90. sb.AppendLine(string.Format(" static void set_{0}_{1}(ref object o, object v)", i.Name, idx));
  91. sb.AppendLine(" {");
  92. if (i.IsStatic)
  93. {
  94. sb.AppendLine(string.Format(" {0}.{1} = ({2})v;", typeClsName, i.Name, realClsName));
  95. }
  96. else
  97. {
  98. if (type.IsValueType)
  99. {
  100. sb.AppendLine(string.Format(" {0} ins =({0})o;", typeClsName));
  101. sb.AppendLine(string.Format(" ins.{0} = ({1})v;", i.Name, realClsName));
  102. sb.AppendLine(" o = ins;");
  103. }
  104. else
  105. sb.AppendLine(string.Format(" (({0})o).{1} = ({2})v;", typeClsName, i.Name, realClsName));
  106. }
  107. sb.AppendLine(" }");
  108. sb.AppendLine();
  109. sb.AppendLine(string.Format(" static StackObject* AssignFromStack_{0}_{1}(ref object o, ILIntepreter __intp, StackObject* ptr_of_this_method, IList<object> __mStack)", i.Name, idx));
  110. sb.AppendLine(" {");
  111. sb.AppendLine(" ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain;");
  112. i.FieldType.AppendArgumentCode(sb, 0, i.Name, valueTypeBinders, false, false, false);
  113. if (i.IsStatic)
  114. {
  115. sb.AppendLine(string.Format(" {0}.{1} = @{1};", typeClsName, i.Name));
  116. }
  117. else
  118. {
  119. if (type.IsValueType)
  120. {
  121. sb.AppendLine(string.Format(" {0} ins =({0})o;", typeClsName));
  122. sb.AppendLine(string.Format(" ins.{0} = @{0};", i.Name));
  123. sb.AppendLine(" o = ins;");
  124. }
  125. else
  126. {
  127. sb.AppendLine(string.Format(" (({0})o).{1} = @{1};", typeClsName, i.Name));
  128. }
  129. }
  130. sb.AppendLine(" return ptr_of_this_method;");
  131. sb.AppendLine(" }");
  132. sb.AppendLine();
  133. }
  134. idx++;
  135. }
  136. return sb.ToString();
  137. }
  138. internal static bool CheckCanPinn(this Type type)
  139. {
  140. if (type.IsValueType)
  141. {
  142. FieldInfo[] fi = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
  143. bool res = true;
  144. foreach(var i in fi)
  145. {
  146. if(!i.FieldType.IsPrimitive)
  147. {
  148. res = false;
  149. break;
  150. }
  151. }
  152. return res;
  153. }
  154. else
  155. return false;
  156. }
  157. }
  158. }