TupleSerializer.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. #if !NO_RUNTIME
  2. using System;
  3. using ProtoBuf.Meta;
  4. #if FEAT_IKVM
  5. using Type = IKVM.Reflection.Type;
  6. using IKVM.Reflection;
  7. #else
  8. using System.Reflection;
  9. #endif
  10. namespace ProtoBuf.Serializers
  11. {
  12. sealed class TupleSerializer : IProtoTypeSerializer
  13. {
  14. private readonly MemberInfo[] members;
  15. private readonly ConstructorInfo ctor;
  16. private IProtoSerializer[] tails;
  17. public TupleSerializer(RuntimeTypeModel model, ConstructorInfo ctor, MemberInfo[] members,Type[] kv)
  18. {
  19. if (ctor == null) throw new ArgumentNullException("ctor");
  20. if (members == null) throw new ArgumentNullException("members");
  21. this.ctor = ctor;
  22. this.members = members;
  23. this.tails = new IProtoSerializer[members.Length];
  24. ParameterInfo[] parameters = ctor.GetParameters();
  25. for (int i = 0; i < members.Length; i++)
  26. {
  27. WireType wireType;
  28. Type finalType = parameters[i].ParameterType;
  29. Type itemType = null, defaultType = null;
  30. MetaType.ResolveListTypes(model, finalType, ref itemType, ref defaultType);
  31. Type tmp = itemType == null ? finalType : itemType;
  32. bool asReference = false;
  33. int typeIndex = model.FindOrAddAuto(tmp, false, true, false);
  34. if (typeIndex >= 0)
  35. {
  36. asReference = model[tmp].AsReferenceDefault;
  37. }
  38. if (tmp.FullName == "ILRuntime.Runtime.Intepreter.ILTypeInstance") {
  39. tmp = kv [i];
  40. }
  41. IProtoSerializer tail = ValueMember.TryGetCoreSerializer(model, DataFormat.Default, tmp, out wireType, asReference, false, false, true), serializer;
  42. if (tail == null)
  43. {
  44. throw new InvalidOperationException("No serializer defined for type: " + tmp.FullName);
  45. }
  46. tail = new TagDecorator(i + 1, wireType, false, tail);
  47. if (itemType == null)
  48. {
  49. serializer = tail;
  50. }
  51. else
  52. {
  53. if (finalType.IsArray)
  54. {
  55. serializer = new ArrayDecorator(model, tail, i + 1, false, wireType, finalType, false, false);
  56. }
  57. else
  58. {
  59. serializer = ListDecorator.Create(model, finalType, defaultType, tail, i + 1, false, wireType, true, false, false);
  60. }
  61. }
  62. tails[i] = serializer;
  63. }
  64. }
  65. public bool HasCallbacks(Meta.TypeModel.CallbackType callbackType)
  66. {
  67. return false;
  68. }
  69. #if FEAT_COMPILER
  70. public void EmitCallback(Compiler.CompilerContext ctx, Compiler.Local valueFrom, Meta.TypeModel.CallbackType callbackType) { }
  71. #endif
  72. public Type ExpectedType
  73. {
  74. get { return ctor.DeclaringType; }
  75. }
  76. #if !FEAT_IKVM
  77. void IProtoTypeSerializer.Callback(object value, Meta.TypeModel.CallbackType callbackType, SerializationContext context) { }
  78. object IProtoTypeSerializer.CreateInstance(ProtoReader source) { throw new NotSupportedException(); }
  79. private object GetValue(object obj, int index)
  80. {
  81. PropertyInfo prop;
  82. FieldInfo field;
  83. if ((prop = members[index] as PropertyInfo) != null)
  84. {
  85. if (obj == null)
  86. return Helpers.IsValueType(prop.PropertyType) ? Activator.CreateInstance(prop.PropertyType) : null;
  87. //return prop.GetValue(obj, null);
  88. return prop.GetGetMethod(true).Invoke(obj, null);
  89. }
  90. else if ((field = members[index] as FieldInfo) != null)
  91. {
  92. if (obj == null)
  93. return Helpers.IsValueType(field.FieldType) ? Activator.CreateInstance(field.FieldType) : null;
  94. return field.GetValue(obj);
  95. }
  96. else
  97. {
  98. throw new InvalidOperationException();
  99. }
  100. }
  101. public object Read(object value, ProtoReader source)
  102. {
  103. object[] values = new object[members.Length];
  104. bool invokeCtor = false;
  105. if (value == null)
  106. {
  107. invokeCtor = true;
  108. }
  109. for (int i = 0; i < values.Length; i++)
  110. values[i] = GetValue(value, i);
  111. int field;
  112. while ((field = source.ReadFieldHeader()) > 0)
  113. {
  114. invokeCtor = true;
  115. if (field <= tails.Length)
  116. {
  117. IProtoSerializer tail = tails[field - 1];
  118. values[field - 1] = tails[field - 1].Read(tail.RequiresOldValue ? values[field - 1] : null, source);
  119. }
  120. else
  121. {
  122. source.SkipField();
  123. }
  124. }
  125. return invokeCtor ? ctor.Invoke(values) : value;
  126. }
  127. public void Write(object value, ProtoWriter dest)
  128. {
  129. for (int i = 0; i < tails.Length; i++)
  130. {
  131. object val = GetValue(value, i);
  132. if (val != null) tails[i].Write(val, dest);
  133. }
  134. }
  135. #endif
  136. public bool RequiresOldValue
  137. {
  138. get { return true; }
  139. }
  140. public bool ReturnsValue
  141. {
  142. get { return false; }
  143. }
  144. Type GetMemberType(int index)
  145. {
  146. Type result = Helpers.GetMemberType(members[index]);
  147. if (result == null) throw new InvalidOperationException();
  148. return result;
  149. }
  150. bool IProtoTypeSerializer.CanCreateInstance() { return false; }
  151. #if FEAT_COMPILER
  152. public void EmitWrite(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
  153. {
  154. using (Compiler.Local loc = ctx.GetLocalWithValue(ctor.DeclaringType, valueFrom))
  155. {
  156. for (int i = 0; i < tails.Length; i++)
  157. {
  158. Type type = GetMemberType(i);
  159. ctx.LoadAddress(loc, ExpectedType);
  160. if (members[i] is FieldInfo)
  161. {
  162. ctx.LoadValue((FieldInfo)members[i]);
  163. }
  164. else if (members[i] is PropertyInfo)
  165. {
  166. ctx.LoadValue((PropertyInfo)members[i]);
  167. }
  168. ctx.WriteNullCheckedTail(type, tails[i], null);
  169. }
  170. }
  171. }
  172. void IProtoTypeSerializer.EmitCreateInstance(Compiler.CompilerContext ctx) { throw new NotSupportedException(); }
  173. public void EmitRead(Compiler.CompilerContext ctx, Compiler.Local incoming)
  174. {
  175. using (Compiler.Local objValue = ctx.GetLocalWithValue(ExpectedType, incoming))
  176. {
  177. Compiler.Local[] locals = new Compiler.Local[members.Length];
  178. try
  179. {
  180. for (int i = 0; i < locals.Length; i++)
  181. {
  182. Type type = GetMemberType(i);
  183. bool store = true;
  184. locals[i] = new Compiler.Local(ctx, type);
  185. if (!Helpers.IsValueType(ExpectedType))
  186. {
  187. // value-types always read the old value
  188. if (Helpers.IsValueType(type))
  189. {
  190. switch (Helpers.GetTypeCode(type))
  191. {
  192. case ProtoTypeCode.Boolean:
  193. case ProtoTypeCode.Byte:
  194. case ProtoTypeCode.Int16:
  195. case ProtoTypeCode.Int32:
  196. case ProtoTypeCode.SByte:
  197. case ProtoTypeCode.UInt16:
  198. case ProtoTypeCode.UInt32:
  199. ctx.LoadValue(0);
  200. break;
  201. case ProtoTypeCode.Int64:
  202. case ProtoTypeCode.UInt64:
  203. ctx.LoadValue(0L);
  204. break;
  205. case ProtoTypeCode.Single:
  206. ctx.LoadValue(0.0F);
  207. break;
  208. case ProtoTypeCode.Double:
  209. ctx.LoadValue(0.0D);
  210. break;
  211. case ProtoTypeCode.Decimal:
  212. ctx.LoadValue(0M);
  213. break;
  214. case ProtoTypeCode.Guid:
  215. ctx.LoadValue(Guid.Empty);
  216. break;
  217. default:
  218. ctx.LoadAddress(locals[i], type);
  219. ctx.EmitCtor(type);
  220. store = false;
  221. break;
  222. }
  223. }
  224. else
  225. {
  226. ctx.LoadNullRef();
  227. }
  228. if (store)
  229. {
  230. ctx.StoreValue(locals[i]);
  231. }
  232. }
  233. }
  234. Compiler.CodeLabel skipOld = Helpers.IsValueType(ExpectedType)
  235. ? new Compiler.CodeLabel()
  236. : ctx.DefineLabel();
  237. if (!Helpers.IsValueType(ExpectedType))
  238. {
  239. ctx.LoadAddress(objValue, ExpectedType);
  240. ctx.BranchIfFalse(skipOld, false);
  241. }
  242. for (int i = 0; i < members.Length; i++)
  243. {
  244. ctx.LoadAddress(objValue, ExpectedType);
  245. if (members[i] is FieldInfo)
  246. {
  247. ctx.LoadValue((FieldInfo)members[i]);
  248. }
  249. else if (members[i] is PropertyInfo)
  250. {
  251. ctx.LoadValue((PropertyInfo)members[i]);
  252. }
  253. ctx.StoreValue(locals[i]);
  254. }
  255. if (!Helpers.IsValueType(ExpectedType)) ctx.MarkLabel(skipOld);
  256. using (Compiler.Local fieldNumber = new Compiler.Local(ctx, ctx.MapType(typeof(int))))
  257. {
  258. Compiler.CodeLabel @continue = ctx.DefineLabel(),
  259. processField = ctx.DefineLabel(),
  260. notRecognised = ctx.DefineLabel();
  261. ctx.Branch(@continue, false);
  262. Compiler.CodeLabel[] handlers = new Compiler.CodeLabel[members.Length];
  263. for (int i = 0; i < members.Length; i++)
  264. {
  265. handlers[i] = ctx.DefineLabel();
  266. }
  267. ctx.MarkLabel(processField);
  268. ctx.LoadValue(fieldNumber);
  269. ctx.LoadValue(1);
  270. ctx.Subtract(); // jump-table is zero-based
  271. ctx.Switch(handlers);
  272. // and the default:
  273. ctx.Branch(notRecognised, false);
  274. for (int i = 0; i < handlers.Length; i++)
  275. {
  276. ctx.MarkLabel(handlers[i]);
  277. IProtoSerializer tail = tails[i];
  278. Compiler.Local oldValIfNeeded = tail.RequiresOldValue ? locals[i] : null;
  279. ctx.ReadNullCheckedTail(locals[i].Type, tail, oldValIfNeeded);
  280. if (tail.ReturnsValue)
  281. {
  282. if (Helpers.IsValueType(locals[i].Type))
  283. {
  284. ctx.StoreValue(locals[i]);
  285. }
  286. else
  287. {
  288. Compiler.CodeLabel hasValue = ctx.DefineLabel(), allDone = ctx.DefineLabel();
  289. ctx.CopyValue();
  290. ctx.BranchIfTrue(hasValue, true); // interpret null as "don't assign"
  291. ctx.DiscardValue();
  292. ctx.Branch(allDone, true);
  293. ctx.MarkLabel(hasValue);
  294. ctx.StoreValue(locals[i]);
  295. ctx.MarkLabel(allDone);
  296. }
  297. }
  298. ctx.Branch(@continue, false);
  299. }
  300. ctx.MarkLabel(notRecognised);
  301. ctx.LoadReaderWriter();
  302. ctx.EmitCall(ctx.MapType(typeof(ProtoReader)).GetMethod("SkipField"));
  303. ctx.MarkLabel(@continue);
  304. ctx.EmitBasicRead("ReadFieldHeader", ctx.MapType(typeof(int)));
  305. ctx.CopyValue();
  306. ctx.StoreValue(fieldNumber);
  307. ctx.LoadValue(0);
  308. ctx.BranchIfGreater(processField, false);
  309. }
  310. for (int i = 0; i < locals.Length; i++)
  311. {
  312. ctx.LoadValue(locals[i]);
  313. }
  314. ctx.EmitCtor(ctor);
  315. ctx.StoreValue(objValue);
  316. }
  317. finally
  318. {
  319. for (int i = 0; i < locals.Length; i++)
  320. {
  321. if (locals[i] != null)
  322. locals[i].Dispose(); // release for re-use
  323. }
  324. }
  325. }
  326. }
  327. #endif
  328. }
  329. }
  330. #endif