bool EmitDedicatedMethod(Compiler.CompilerContext ctx, Compiler.Local valueFrom, bool read) { System.Reflection.Emit.MethodBuilder method = ctx.GetDedicatedMethod(key, read); if (method == null) return false; using (Compiler.Local token = new ProtoBuf.Compiler.Local(ctx, typeof(SubItemToken))) { Type rwType = read ? typeof(ProtoReader) : typeof(ProtoWriter); ctx.LoadValue(valueFrom); if (!read) // write requires the object for StartSubItem; read doesn't { if (type.IsValueType) { ctx.LoadNullRef(); } else { ctx.CopyValue(); } } ctx.LoadReaderWriter(); ctx.EmitCall(rwType.GetMethod("StartSubItem")); ctx.StoreValue(token); // note: value already on the stack ctx.LoadReaderWriter(); ctx.EmitCall(method); // handle inheritance (we will be calling the *base* version of things, // but we expect Read to return the "type" type) if (read && type != method.ReturnType) ctx.Cast(this.type); ctx.LoadValue(token); ctx.LoadReaderWriter(); ctx.EmitCall(rwType.GetMethod("EndSubItem")); } return true; }
public void EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom) { ctx.LoadValue(valueFrom); ctx.CastToObject(type); ctx.LoadReaderWriter(); ctx.LoadValue(ctx.MapMetaKeyToCompiledKey(key)); if (type == ctx.MapType(typeof(object))) ctx.LoadNullRef(); else ctx.LoadValue(type); ctx.LoadValue((int)options); ctx.EmitCall(ctx.MapType(typeof(BclHelpers)).GetMethod("ReadNetObject")); ctx.CastFromObject(type); }
void IProtoSerializer.EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom) { if (overwriteList) { ctx.LoadNullRef(); } else { ctx.LoadValue(valueFrom); } ctx.LoadReaderWriter(); ctx.EmitCall(typeof(ProtoReader).GetMethod("AppendBytes")); }
protected override void EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom) { Tail.EmitRead(ctx, valueFrom); ctx.CopyValue(); Compiler.CodeLabel @nonEmpty = ctx.DefineLabel(), @end = ctx.DefineLabel(); ctx.LoadValue(typeof(string).GetProperty("Length")); ctx.BranchIfTrue(@nonEmpty, true); ctx.DiscardValue(); ctx.LoadNullRef(); ctx.Branch(@end, true); ctx.MarkLabel(@nonEmpty); ctx.EmitCtor(typeof(Uri), typeof(string)); ctx.MarkLabel(@end); }
public void EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom) { ctx.LoadValue(valueFrom); ctx.CastToObject(type); ctx.LoadReaderWriter(); ctx.LoadValue(key); if (type == typeof(object)) { ctx.LoadNullRef(); } else { ctx.LoadValue(type); } ctx.LoadValue((int)options); ctx.EmitCall(typeof(BclHelpers).GetMethod("ReadNetObject")); ctx.CastFromObject(type); }
public void EmitRead(Compiler.CompilerContext ctx, Compiler.Local incoming) { using (Compiler.Local objValue = ctx.GetLocalWithValue(ExpectedType, incoming)) { Compiler.Local[] locals = new Compiler.Local[members.Length]; try { for (int i = 0; i < locals.Length; i++) { Type type = GetMemberType(i); bool store = true; locals[i] = new Compiler.Local(ctx, type); if (!Helpers.IsValueType(ExpectedType)) { // value-types always read the old value if (Helpers.IsValueType(type)) { switch (Helpers.GetTypeCode(type)) { case ProtoTypeCode.Boolean: case ProtoTypeCode.Byte: case ProtoTypeCode.Int16: case ProtoTypeCode.Int32: case ProtoTypeCode.SByte: case ProtoTypeCode.UInt16: case ProtoTypeCode.UInt32: ctx.LoadValue(0); break; case ProtoTypeCode.Int64: case ProtoTypeCode.UInt64: ctx.LoadValue(0L); break; case ProtoTypeCode.Single: ctx.LoadValue(0.0F); break; case ProtoTypeCode.Double: ctx.LoadValue(0.0D); break; case ProtoTypeCode.Decimal: ctx.LoadValue(0M); break; case ProtoTypeCode.Guid: ctx.LoadValue(Guid.Empty); break; default: ctx.LoadAddress(locals[i], type); ctx.EmitCtor(type); store = false; break; } } else { ctx.LoadNullRef(); } if (store) { ctx.StoreValue(locals[i]); } } } Compiler.CodeLabel skipOld = Helpers.IsValueType(ExpectedType) ? new Compiler.CodeLabel() : ctx.DefineLabel(); if (!Helpers.IsValueType(ExpectedType)) { ctx.LoadAddress(objValue, ExpectedType); ctx.BranchIfFalse(skipOld, false); } for (int i = 0; i < members.Length; i++) { ctx.LoadAddress(objValue, ExpectedType); if (members[i] is FieldInfo) { ctx.LoadValue((FieldInfo)members[i]); } else if (members[i] is PropertyInfo) { ctx.LoadValue((PropertyInfo)members[i]); } ctx.StoreValue(locals[i]); } if (!Helpers.IsValueType(ExpectedType)) ctx.MarkLabel(skipOld); using (Compiler.Local fieldNumber = new Compiler.Local(ctx, ctx.MapType(typeof(int)))) { Compiler.CodeLabel @continue = ctx.DefineLabel(), processField = ctx.DefineLabel(), notRecognised = ctx.DefineLabel(); ctx.Branch(@continue, false); Compiler.CodeLabel[] handlers = new Compiler.CodeLabel[members.Length]; for (int i = 0; i < members.Length; i++) { handlers[i] = ctx.DefineLabel(); } ctx.MarkLabel(processField); ctx.LoadValue(fieldNumber); ctx.LoadValue(1); ctx.Subtract(); // jump-table is zero-based ctx.Switch(handlers); // and the default: ctx.Branch(notRecognised, false); for (int i = 0; i < handlers.Length; i++) { ctx.MarkLabel(handlers[i]); IProtoSerializer tail = tails[i]; Compiler.Local oldValIfNeeded = tail.RequiresOldValue ? locals[i] : null; ctx.ReadNullCheckedTail(locals[i].Type, tail, oldValIfNeeded); if (tail.ReturnsValue) { if (Helpers.IsValueType(locals[i].Type)) { ctx.StoreValue(locals[i]); } else { Compiler.CodeLabel hasValue = ctx.DefineLabel(), allDone = ctx.DefineLabel(); ctx.CopyValue(); ctx.BranchIfTrue(hasValue, true); // interpret null as "don't assign" ctx.DiscardValue(); ctx.Branch(allDone, true); ctx.MarkLabel(hasValue); ctx.StoreValue(locals[i]); ctx.MarkLabel(allDone); } } ctx.Branch(@continue, false); } ctx.MarkLabel(notRecognised); ctx.LoadReaderWriter(); ctx.EmitCall(ctx.MapType(typeof(ProtoReader)).GetMethod("SkipField")); ctx.MarkLabel(@continue); ctx.EmitBasicRead("ReadFieldHeader", ctx.MapType(typeof(int))); ctx.CopyValue(); ctx.StoreValue(fieldNumber); ctx.LoadValue(0); ctx.BranchIfGreater(processField, false); } for (int i = 0; i < locals.Length; i++) { ctx.LoadValue(locals[i]); } ctx.EmitCtor(ctor); ctx.StoreValue(objValue); } finally { for (int i = 0; i < locals.Length; i++) { if (locals[i] != null) locals[i].Dispose(); // release for re-use } } } }
protected override void EmitWrite(Compiler.CompilerContext ctx, Compiler.Local valueFrom) { using(Compiler.Local valOrNull = ctx.GetLocalWithValue(expectedType, valueFrom)) using(Compiler.Local token = new Compiler.Local(ctx, ctx.MapType(typeof(SubItemToken)))) { ctx.LoadNullRef(); ctx.LoadReaderWriter(); ctx.EmitCall(ctx.MapType(typeof(ProtoWriter)).GetMethod("StartSubItem")); ctx.StoreValue(token); if (Helpers.IsValueType(expectedType)) { ctx.LoadAddress(valOrNull, expectedType); ctx.LoadValue(expectedType.GetProperty("HasValue")); } else { ctx.LoadValue(valOrNull); } Compiler.CodeLabel @end = ctx.DefineLabel(); ctx.BranchIfFalse(@end, false); if (Helpers.IsValueType(expectedType)) { ctx.LoadAddress(valOrNull, expectedType); ctx.EmitCall(expectedType.GetMethod("GetValueOrDefault", Helpers.EmptyTypes)); } else { ctx.LoadValue(valOrNull); } Tail.EmitWrite(ctx, null); ctx.MarkLabel(@end); ctx.LoadValue(token); ctx.LoadReaderWriter(); ctx.EmitCall(ctx.MapType(typeof(ProtoWriter)).GetMethod("EndSubItem")); } }
private static void EmitReadAndAddItem(Compiler.CompilerContext ctx, Compiler.Local list, IProtoSerializer tail, MethodInfo add) { ctx.LoadValue(list); Type itemType = tail.ExpectedType; if (tail.RequiresOldValue) { if (itemType.IsValueType || !tail.ReturnsValue) { // going to need a variable using (Compiler.Local item = new Compiler.Local(ctx, itemType)) { if (itemType.IsValueType) { // initialise the struct ctx.LoadAddress(item, itemType); ctx.EmitCtor(itemType); } else { // assign null ctx.LoadNullRef(); ctx.StoreValue(item); } tail.EmitRead(ctx, item); if (!tail.ReturnsValue) { ctx.LoadValue(item); } } } else { // no variable; pass the null on the stack and take the value *off* the stack ctx.LoadNullRef(); tail.EmitRead(ctx, null); } } else { if (tail.ReturnsValue) { // out only (on the stack); just emit it tail.EmitRead(ctx, null); } else { // doesn't take anything in nor return anything! WTF? throw new InvalidOperationException(); } } // our "Add" is chosen either to take the correct type, or to take "object"; // we may need to box the value Type addParamType = add.GetParameters()[0].ParameterType; if(addParamType != itemType) { if (addParamType == typeof(object)) { ctx.CastToObject(itemType); } else { throw new InvalidOperationException("Conflicting item/add type"); } } ctx.EmitCall(add); if (add.ReturnType != typeof(void)) { ctx.DiscardValue(); } }
private void EmitReadArray(Compiler.CompilerContext ctx, Compiler.Local list, Compiler.Local oldArr, Compiler.Local newArr, IProtoSerializer tail, MethodInfo add, Type listType) { Compiler.CodeLabel readPacked = packedWireType == WireType.None ? new Compiler.CodeLabel() : ctx.DefineLabel(); Compiler.CodeLabel @end = ctx.DefineLabel(); if (packedWireType != WireType.None) { ctx.LoadReaderWriter(); ctx.LoadValue(typeof(ProtoReader).GetProperty("WireType")); ctx.LoadValue((int)WireType.String); ctx.BranchIfEqual(readPacked, false); } using (Compiler.Local fieldNumber = new Compiler.Local(ctx, typeof(int))) { // int fieldNumber = source.FieldNumber; ctx.LoadReaderWriter(); ctx.LoadValue(typeof (ProtoReader).GetProperty("FieldNumber")); ctx.StoreValue(fieldNumber); using (Compiler.Local readObject = new Compiler.Local(ctx, typeof (bool))) using (Compiler.Local arrayKey = new Compiler.Local(ctx, typeof (int))) { // bool readObject = true; ctx.LoadValue(true); ctx.StoreValue(readObject); // value = value ?? Array.CreateInstance(itemType, 0); Compiler.CodeLabel @endCreateInstance = ctx.DefineLabel(); ctx.LoadValue(oldArr); ctx.LoadNullRef(); ctx.TestEqual(); ctx.BranchIfFalse(@endCreateInstance, true); ctx.LoadValue(itemType); ctx.LoadValue(0); ctx.EmitCall(typeof(Array).GetMethod("CreateInstance", new Type[] { typeof(Type), typeof(int) })); ctx.StoreValue(oldArr); ctx.MarkLabel(@endCreateInstance); if (AsReference) { using (Compiler.Local objectKey = new Compiler.Local(ctx, typeof(int))) { //int objectKey = source.ReadInt32(); ctx.LoadReaderWriter(); ctx.EmitCall(typeof(ProtoReader).GetMethod("ReadInt32")); ctx.StoreValue(objectKey); Compiler.CodeLabel @ifNotThere = ctx.DefineLabel(); Compiler.CodeLabel @endIf = ctx.DefineLabel(); //if (objectKey > 0) ctx.LoadValue(objectKey); ctx.LoadValue(0); ctx.BranchIfLessOrEqual(@ifNotThere, true); // value = source.NetCache.GetKeyedObject(objectKey); ctx.LoadReaderWriter(); ctx.LoadValue(typeof(ProtoReader).GetProperty("NetCache")); ctx.LoadValue(objectKey); ctx.EmitCall(typeof(NetObjectCache).GetMethod("GetKeyedObject")); ctx.StoreValue(oldArr); //readObject = false; ctx.LoadValue(false); ctx.StoreValue(readObject); //} ctx.Branch(@endIf, true); //else ctx.MarkLabel(@ifNotThere); //bool dummy; //arrayKey = source.NetCache.AddObjectKey(value, out dummy); using (Compiler.Local dummy = new Compiler.Local(ctx, typeof(bool))) { ctx.LoadReaderWriter(); ctx.LoadValue(typeof(ProtoReader).GetProperty("NetCache")); ctx.LoadValue(oldArr); ctx.LoadAddress(dummy, typeof(bool)); ctx.EmitCall(typeof(NetObjectCache).GetMethod("AddObjectKey", new Type[] { typeof(object), typeof(bool).MakeByRefType() })); ctx.StoreValue(arrayKey); } ctx.MarkLabel(@endIf); } } else { using (Compiler.Local readValue = new Compiler.Local(ctx, typeof(int))) { ctx.LoadReaderWriter(); ctx.EmitCall(typeof(ProtoReader).GetMethod("ReadInt32")); ctx.StoreValue(readValue); Compiler.CodeLabel @codeContinues = ctx.DefineLabel(); ctx.LoadValue(readValue); ctx.LoadValue(0); ctx.BranchIfEqual(@codeContinues, true); ctx.LoadValue(oldArr); ctx.Branch(@end, false); ctx.MarkLabel(@codeContinues); } } /* if (readObject) { while (source.TryReadFieldHeader(field)) { list.Add(Tail.Read(null, source)); } */ Compiler.CodeLabel @finishRead = ctx.DefineLabel(); ctx.LoadValue(readObject); ctx.BranchIfFalse(@finishRead, false); Compiler.CodeLabel @beginWhileLoop = ctx.DefineLabel(); Compiler.CodeLabel @endWhileLoop = ctx.DefineLabel(); ctx.MarkLabel(@beginWhileLoop); ctx.LoadReaderWriter(); ctx.LoadValue(fieldNumber); ctx.EmitCall(typeof (ProtoReader).GetMethod("TryReadFieldHeader")); ctx.BranchIfFalse(@endWhileLoop, false); ListDecorator.EmitReadAndAddItem(ctx, list, tail, add, null); ctx.Branch(@beginWhileLoop, false); ctx.MarkLabel(@endWhileLoop); // Array newArray = Array.CreateInstance(itemType, list.Count); ctx.LoadValue(itemType); ctx.LoadValue(list); ctx.LoadValue(listType.GetProperty("Count")); ctx.EmitCall(typeof(Array).GetMethod("CreateInstance", new Type[] { typeof(Type), typeof(int) })); ctx.StoreValue(newArr); // list.CopyTo(newArray, 0); ctx.LoadValue(list); ctx.LoadValue(newArr); ctx.LoadValue(0); ctx.EmitCall(listType.GetMethod("CopyTo", new Type[] { ExpectedType, typeof(int) })); if (AsReference) { //source.NetCache.UpdateKeyedObject(arrayKey, value, newArray); ctx.LoadReaderWriter(); ctx.LoadValue(typeof(ProtoReader).GetProperty("NetCache")); ctx.LoadValue(arrayKey); ctx.LoadValue(oldArr); ctx.LoadValue(newArr); ctx.EmitCall(typeof(NetObjectCache).GetMethod("UpdateKeyedObject")); } //value = newArray; ctx.LoadValue(newArr); ctx.StoreValue(oldArr); ctx.MarkLabel(@finishRead); ctx.LoadValue(oldArr); ctx.MarkLabel(@end); } } if (packedWireType != WireType.None) { Compiler.CodeLabel allDone = ctx.DefineLabel(); ctx.Branch(allDone, false); ctx.MarkLabel(readPacked); ctx.LoadReaderWriter(); ctx.EmitCall(typeof (ProtoReader).GetMethod("StartSubItem")); Compiler.CodeLabel testForData = ctx.DefineLabel(), noMoreData = ctx.DefineLabel(); ctx.MarkLabel(testForData); ctx.LoadValue((int) packedWireType); ctx.LoadReaderWriter(); ctx.EmitCall(typeof (ProtoReader).GetMethod("HasSubValue")); ctx.BranchIfFalse(noMoreData, false); ListDecorator.EmitReadAndAddItem(ctx, list, tail, add, null); ctx.Branch(testForData, false); ctx.MarkLabel(noMoreData); ctx.LoadReaderWriter(); ctx.EmitCall(typeof (ProtoReader).GetMethod("EndSubItem")); EmitReadEndPacked(ctx, list, oldArr, newArr, listType); ctx.MarkLabel(allDone); } }
bool EmitDedicatedMethod(Compiler.CompilerContext ctx, Compiler.Local valueFrom, bool read) { #if SILVERLIGHT return false; #else MethodBuilder method = ctx.GetDedicatedMethod(key, read); if (method == null) return false; using (Compiler.Local token = new ProtoBuf.Compiler.Local(ctx, ctx.MapType(typeof(SubItemToken)))) { Type rwType = ctx.MapType(read ? typeof(ProtoReader) : typeof(ProtoWriter)); ctx.LoadValue(valueFrom); if (!read) // write requires the object for StartSubItem; read doesn't { // (if recursion-check is disabled [subtypes] then null is fine too) if (type.IsValueType || !recursionCheck) { ctx.LoadNullRef(); } else { ctx.CopyValue(); } } ctx.LoadReaderWriter(); ctx.EmitCall(rwType.GetMethod("StartSubItem")); ctx.StoreValue(token); // note: value already on the stack ctx.LoadReaderWriter(); ctx.EmitCall(method); // handle inheritance (we will be calling the *base* version of things, // but we expect Read to return the "type" type) if (read && type != method.ReturnType) ctx.Cast(this.type); ctx.LoadValue(token); ctx.LoadReaderWriter(); ctx.EmitCall(rwType.GetMethod("EndSubItem")); } return true; #endif }
private void EmitCreateIfNull(Compiler.CompilerContext ctx, Type type, Compiler.Local storage) { Helpers.DebugAssert(storage != null); if (!type.IsValueType) { Compiler.CodeLabel afterNullCheck = ctx.DefineLabel(); ctx.LoadValue(storage); ctx.BranchIfTrue(afterNullCheck, true); // different ways of creating a new instance bool callNoteObject = true; if (!useConstructor) { // DataContractSerializer style ctx.LoadValue(constructType); ctx.EmitCall(typeof(BclHelpers).GetMethod("GetUninitializedObject")); ctx.Cast(forType); } else if (constructType.IsClass && hasConstructor) { // XmlSerializer style ctx.EmitCtor(constructType); } else { ctx.LoadValue(type); ctx.EmitCall(typeof(TypeModel).GetMethod("ThrowCannotCreateInstance", BindingFlags.Static | BindingFlags.Public)); ctx.LoadNullRef(); callNoteObject = false; } if (callNoteObject) { // track root object creation ctx.CopyValue(); ctx.LoadReaderWriter(); ctx.EmitCall(typeof(ProtoReader).GetMethod("NoteObject", BindingFlags.Static | BindingFlags.Public)); } if (baseCtorCallbacks != null) { for (int i = 0; i < baseCtorCallbacks.Length; i++) { EmitInvokeCallback(ctx, baseCtorCallbacks[i]); } } if (callbacks != null) EmitInvokeCallback(ctx, callbacks.BeforeDeserialize); ctx.StoreValue(storage); ctx.MarkLabel(afterNullCheck); } }
public void EmitRead(Compiler.CompilerContext ctx, Compiler.Local incoming) { bool issueReferenceDirectives = !baseTupleAsReference && asReference; Compiler.CodeLabel @end = ctx.DefineLabel(); using (Compiler.Local tupleKey = new Compiler.Local(ctx, typeof(int))) using (Compiler.Local oldTuple = new Compiler.Local(ctx, typeof(object))) using (Compiler.Local objValue = ctx.GetLocalWithValue(ExpectedType, incoming)) { Compiler.Local[] locals = new Compiler.Local[members.Length]; try { if (issueReferenceDirectives) { //int tupleKey = 0; ctx.LoadValue(0); ctx.StoreValue(tupleKey); //object oldTuple = null; ctx.LoadNullRef(); ctx.StoreValue(oldTuple); //tupleKey = (int)source.ReadUInt32(); ctx.LoadReaderWriter(); ctx.EmitCall(typeof (ProtoReader).GetMethod("ReadUInt32")); //ctx.CastToObject(typeof (int)); ctx.StoreValue(tupleKey); Compiler.CodeLabel @objectNotFound = ctx.DefineLabel(); ctx.LoadValue(tupleKey); ctx.LoadValue(0); ctx.BranchIfEqual(@objectNotFound, true); //// return source.NetCache.GetKeyedObject(tupleKey); ctx.LoadReaderWriter(); ctx.LoadValue(typeof (ProtoReader).GetProperty("NetCache")); ctx.LoadValue(tupleKey); ctx.EmitCall(typeof (NetObjectCache).GetMethod("GetKeyedObject")); ctx.CastFromObject(ExpectedType); ctx.StoreValue(objValue); ctx.Branch(@end, false); ctx.MarkLabel(@objectNotFound); ctx.EmitCtor(typeof (object)); ctx.StoreValue(oldTuple); // tupleKey = source.NetCache.AddObjectKey(oldTuple, out dummy); using (Compiler.Local dummy = new Compiler.Local(ctx, typeof (bool))) { ctx.LoadReaderWriter(); ctx.LoadValue(typeof (ProtoReader).GetProperty("NetCache")); ctx.LoadValue(oldTuple); ctx.LoadAddress(dummy, typeof (bool)); ctx.EmitCall(typeof (NetObjectCache).GetMethod("AddObjectKey", new Type[] { typeof(object), typeof(bool).MakeByRefType() })); ctx.StoreValue(tupleKey); } } for (int i = 0; i < locals.Length; i++) { Type type = GetMemberType(i); bool store = true; locals[i] = new Compiler.Local(ctx, type); if (!ExpectedType.IsValueType) { // value-types always read the old value if (type.IsValueType) { switch (Helpers.GetTypeCode(type)) { case ProtoTypeCode.Boolean: case ProtoTypeCode.Byte: case ProtoTypeCode.Int16: case ProtoTypeCode.Int32: case ProtoTypeCode.SByte: case ProtoTypeCode.UInt16: case ProtoTypeCode.UInt32: ctx.LoadValue(0); break; case ProtoTypeCode.Int64: case ProtoTypeCode.UInt64: ctx.LoadValue(0L); break; case ProtoTypeCode.Single: ctx.LoadValue(0.0F); break; case ProtoTypeCode.Double: ctx.LoadValue(0.0D); break; case ProtoTypeCode.Decimal: ctx.LoadValue(0M); break; case ProtoTypeCode.Guid: ctx.LoadValue(Guid.Empty); break; default: ctx.LoadAddress(locals[i], type); ctx.EmitCtor(type); store = false; break; } } else { ctx.LoadNullRef(); } if (store) { ctx.StoreValue(locals[i]); } } } Compiler.CodeLabel skipOld = ExpectedType.IsValueType ? new Compiler.CodeLabel() : ctx.DefineLabel(); if (!ExpectedType.IsValueType) { ctx.LoadAddress(objValue, ExpectedType); ctx.BranchIfFalse(skipOld, false); } for (int i = 0; i < members.Length; i++) { ctx.LoadAddress(objValue, ExpectedType); switch (members[i].MemberType) { case MemberTypes.Field: ctx.LoadValue((FieldInfo) members[i]); break; case MemberTypes.Property: ctx.LoadValue((PropertyInfo) members[i]); break; } ctx.StoreValue(locals[i]); } if (!ExpectedType.IsValueType) ctx.MarkLabel(skipOld); using (Compiler.Local fieldNumber = new Compiler.Local(ctx, typeof (int))) using (Compiler.Local j = new Compiler.Local(ctx, typeof (int))) { // j = 0 ctx.LoadValue(0); ctx.StoreValue(j); Compiler.CodeLabel @continue = ctx.DefineLabel(), processField = ctx.DefineLabel(), notRecognised = ctx.DefineLabel(), @endWhileLoop = ctx.DefineLabel(); ctx.Branch(@continue, false); Compiler.CodeLabel[] handlers = new Compiler.CodeLabel[members.Length]; for (int i = 0; i < members.Length; i++) { handlers[i] = ctx.DefineLabel(); } ctx.MarkLabel(processField); ctx.LoadValue(fieldNumber); ctx.LoadValue(1); ctx.Subtract(); // jump-table is zero-based ctx.Switch(handlers); // and the default: ctx.Branch(notRecognised, false); for (int i = 0; i < handlers.Length; i++) { ctx.MarkLabel(handlers[i]); IProtoSerializer tail = tails[i]; Compiler.Local oldValIfNeeded = tail.RequiresOldValue ? locals[i] : null; ctx.ReadNullCheckedTail(locals[i].Type, tail, oldValIfNeeded); if (tail.ReturnsValue) { if (locals[i].Type.IsValueType) { ctx.StoreValue(locals[i]); } else { Compiler.CodeLabel hasValue = ctx.DefineLabel(), allDone = ctx.DefineLabel(); ctx.CopyValue(); ctx.BranchIfTrue(hasValue, true); // interpret null as "don't assign" ctx.DiscardValue(); ctx.Branch(allDone, true); ctx.MarkLabel(hasValue); ctx.StoreValue(locals[i]); ctx.MarkLabel(allDone); } } ctx.Branch(@continue, false); } ctx.MarkLabel(notRecognised); ctx.LoadReaderWriter(); ctx.EmitCall(typeof (ProtoReader).GetMethod("SkipField")); ctx.MarkLabel(@continue); // j < values.Length ctx.LoadValue(j); ctx.LoadValue(members.Length); ctx.BranchIfGreaterOrEqual(@endWhileLoop, false); //j++ ctx.LoadValue(j); ctx.LoadValue(1); ctx.Add(); ctx.StoreValue(j); // source.ReadNextFieldHack() > 0 ctx.LoadReaderWriter(); ctx.EmitCall(typeof (ProtoReader).GetMethod("ReadNextFieldHack")); ctx.LoadValue(0); ctx.BranchIfLessOrEqual(@endWhileLoop, true); // field = source.ReadFieldHeader(); ctx.LoadReaderWriter(); ctx.EmitCall(typeof (ProtoReader).GetMethod("ReadFieldHeader")); ctx.StoreValue(fieldNumber); ctx.Branch(processField, false); ctx.MarkLabel(@endWhileLoop); } for (int i = 0; i < locals.Length; i++) { ctx.LoadValue(locals[i]); } ctx.EmitCtor(ctor); ctx.StoreValue(objValue); if (issueReferenceDirectives) { //source.NetCache.UpdateKeyedObject(tupleKey, oldTuple, result); ctx.LoadReaderWriter(); ctx.LoadValue(typeof (ProtoReader).GetProperty("NetCache")); ctx.LoadValue(tupleKey); ctx.LoadValue(oldTuple); ctx.LoadValue(objValue); ctx.CastToObject(ExpectedType); ctx.EmitCall(typeof (NetObjectCache).GetMethod("UpdateKeyedObject")); } if (forceIssueFakeHeader) { //source.ReadEndGroupFieldHeaderHack(); ctx.LoadReaderWriter(); ctx.EmitCall(typeof (ProtoReader).GetMethod("ReadEndGroupFieldHeaderHack")); } ctx.MarkLabel(@end); ctx.LoadValue(objValue); } finally { for (int i = 0; i < locals.Length; i++) { if (locals[i] != null) locals[i].Dispose(); // release for re-use } } } }
private void EmitCreateIfNull(Compiler.CompilerContext ctx, Type type, Compiler.Local storage) { Helpers.DebugAssert(storage != null); if (!type.IsValueType) { Compiler.CodeLabel afterNullCheck = ctx.DefineLabel(); ctx.LoadValue(storage); ctx.BranchIfTrue(afterNullCheck, true); // different ways of creating a new instance if (!useConstructor) { // DataContractSerializer style ctx.LoadValue(forType); ctx.EmitCall(typeof(BclHelpers).GetMethod("GetUninitializedObject")); ctx.Cast(forType); } else if (type.IsClass && !type.IsAbstract && ( (type.GetConstructor( BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, null, Helpers.EmptyTypes, null)) != null)) { // XmlSerializer style ctx.EmitCtor(type); } else { ctx.LoadValue(type); ctx.EmitCall(typeof(TypeModel).GetMethod("ThrowCannotCreateInstance", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)); ctx.LoadNullRef(); } if (baseCtorCallbacks != null) { for (int i = 0; i < baseCtorCallbacks.Length; i++) { EmitInvokeCallback(ctx, baseCtorCallbacks[i]); } } if (callbacks != null) EmitInvokeCallback(ctx, callbacks.BeforeDeserialize); ctx.StoreValue(storage); ctx.MarkLabel(afterNullCheck); } }