StackObject.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using ILRuntime.CLR.TypeSystem;
  6. using ILRuntime.Runtime.Enviorment;
  7. using ILRuntime.Runtime.Intepreter;
  8. namespace ILRuntime.Runtime.Stack
  9. {
  10. #pragma warning disable CS0660
  11. #pragma warning disable CS0661
  12. [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
  13. public struct StackObject
  14. {
  15. public static StackObject Null = new StackObject() { ObjectType = ObjectTypes.Null, Value = -1, ValueLow = 0 };
  16. public ObjectTypes ObjectType;
  17. public int Value;
  18. public int ValueLow;
  19. public static bool operator ==(StackObject a, StackObject b)
  20. {
  21. return (a.ObjectType == b.ObjectType) && (a.Value == b.Value) && (a.ValueLow == b.ValueLow);
  22. }
  23. public static bool operator !=(StackObject a, StackObject b)
  24. {
  25. return (a.ObjectType != b.ObjectType) || (a.Value != b.Value) || (a.ValueLow == b.ValueLow);
  26. }
  27. //IL2CPP can't process esp->ToObject() properly, so I can only use static function for this
  28. public static unsafe object ToObject(StackObject* esp, ILRuntime.Runtime.Enviorment.AppDomain appdomain, IList<object> mStack)
  29. {
  30. switch (esp->ObjectType)
  31. {
  32. case ObjectTypes.Integer:
  33. return esp->Value;
  34. case ObjectTypes.Long:
  35. {
  36. return *(long*)&esp->Value;
  37. }
  38. case ObjectTypes.Float:
  39. {
  40. return *(float*)&esp->Value;
  41. }
  42. case ObjectTypes.Double:
  43. {
  44. return *(double*)&esp->Value;
  45. }
  46. case ObjectTypes.Object:
  47. return mStack[esp->Value];
  48. case ObjectTypes.FieldReference:
  49. {
  50. ILTypeInstance instance = mStack[esp->Value] as ILTypeInstance;
  51. if (instance != null)
  52. {
  53. return instance[esp->ValueLow];
  54. }
  55. else
  56. {
  57. var obj = mStack[esp->Value];
  58. IType t = null;
  59. if (obj is CrossBindingAdaptorType)
  60. {
  61. t = appdomain.GetType(((CrossBindingAdaptor)((CrossBindingAdaptorType)obj).ILInstance.Type.FirstCLRBaseType).BaseCLRType);
  62. }
  63. else
  64. t = appdomain.GetType(obj.GetType());
  65. return ((CLRType)t).GetFieldValue(esp->ValueLow, obj);
  66. }
  67. }
  68. case ObjectTypes.ArrayReference:
  69. {
  70. Array instance = mStack[esp->Value] as Array;
  71. return instance.GetValue(esp->ValueLow);
  72. }
  73. case ObjectTypes.StaticFieldReference:
  74. {
  75. var t = appdomain.GetType(esp->Value);
  76. if (t is CLR.TypeSystem.ILType)
  77. {
  78. CLR.TypeSystem.ILType type = (CLR.TypeSystem.ILType)t;
  79. return type.StaticInstance[esp->ValueLow];
  80. }
  81. else
  82. {
  83. CLR.TypeSystem.CLRType type = (CLR.TypeSystem.CLRType)t;
  84. return type.GetFieldValue(esp->ValueLow, null);
  85. }
  86. }
  87. case ObjectTypes.StackObjectReference:
  88. {
  89. return ToObject((ILIntepreter.ResolveReference(esp)), appdomain, mStack);
  90. }
  91. case ObjectTypes.ValueTypeObjectReference:
  92. {
  93. StackObject* dst = ILIntepreter.ResolveReference(esp);
  94. IType type = appdomain.GetTypeByIndex(dst->Value);
  95. if (type is ILType)
  96. {
  97. ILType iltype = (ILType)type;
  98. var ins = iltype.Instantiate(false);
  99. for (int i = 0; i < dst->ValueLow; i++)
  100. {
  101. var addr = ILIntepreter.Minus(dst, i + 1);
  102. ins.AssignFromStack(i, addr, appdomain, mStack);
  103. }
  104. return ins;
  105. }
  106. else
  107. {
  108. return ((CLRType)type).ValueTypeBinder.ToObject(dst, mStack);
  109. }
  110. }
  111. case ObjectTypes.Null:
  112. return null;
  113. default:
  114. throw new NotImplementedException();
  115. }
  116. }
  117. public unsafe static void Initialized(ref StackObject esp, int idx, IType fieldType, IList<object> mStack)
  118. {
  119. if (fieldType.IsPrimitive)
  120. {
  121. esp = fieldType.DefaultObject;
  122. }
  123. else
  124. {
  125. if (fieldType.IsValueType)
  126. {
  127. esp.ObjectType = ObjectTypes.Object;
  128. esp.Value = idx;
  129. if (fieldType is CLRType)
  130. {
  131. if (fieldType.TypeForCLR.IsEnum)
  132. {
  133. esp.ObjectType = ObjectTypes.Integer;
  134. esp.Value = 0;
  135. esp.ValueLow = 0;
  136. mStack[idx] = null;
  137. }
  138. else
  139. mStack[idx] = ((CLRType)fieldType).CreateDefaultInstance();
  140. }
  141. else
  142. {
  143. if (((ILType)fieldType).IsEnum)
  144. {
  145. esp.ObjectType = ObjectTypes.Integer;
  146. esp.Value = 0;
  147. esp.ValueLow = 0;
  148. mStack[idx] = null;
  149. }
  150. else
  151. mStack[idx] = ((ILType)fieldType).Instantiate();
  152. }
  153. }
  154. else
  155. {
  156. esp = Null;
  157. mStack[idx] = null;
  158. }
  159. }
  160. }
  161. //IL2CPP can't process esp->Initialized() properly, so I can only use static function for this
  162. public unsafe static void Initialized(StackObject* esp, IType type)
  163. {
  164. if (type.IsPrimitive)
  165. {
  166. *esp = type.DefaultObject;
  167. }
  168. else if (type.IsEnum)
  169. {
  170. ILType ilType = type as ILType;
  171. if (ilType != null)
  172. {
  173. Initialized(esp, ilType.FieldTypes[0]);
  174. }
  175. else
  176. {
  177. Initialized(esp, ((CLRType)type).OrderedFieldTypes[0]);
  178. }
  179. }
  180. else
  181. {
  182. *esp = Null;
  183. }
  184. }
  185. }
  186. public enum ObjectTypes
  187. {
  188. Null,
  189. Integer,
  190. Long,
  191. Float,
  192. Double,
  193. StackObjectReference,//Value = pointer,
  194. StaticFieldReference,
  195. ValueTypeObjectReference,
  196. ValueTypeDescriptor,
  197. Object,
  198. FieldReference,//Value = objIdx, ValueLow = fieldIdx
  199. ArrayReference,//Value = objIdx, ValueLow = elemIdx
  200. }
  201. }