Example #1
0
 protected override void EmitWrite(CompilerContext ctx, Local valueFrom)
 {
     using (Local local = ctx.GetLocalWithValue(this.ExpectedType, valueFrom))
     {
         MethodInfo info;
         MethodInfo info2;
         MethodInfo method      = this.GetEnumeratorInfo(ctx.Model, out info, out info2);
         Type       returnType  = method.ReturnType;
         bool       writePacked = this.WritePacked;
         using (Local local2 = new Local(ctx, returnType))
         {
             using (Local local3 = writePacked ? new Local(ctx, ctx.MapType(typeof(SubItemToken))) : null)
             {
                 if (writePacked)
                 {
                     ctx.LoadValue(this.fieldNumber);
                     ctx.LoadValue(2);
                     ctx.LoadReaderWriter();
                     ctx.EmitCall(ctx.MapType(typeof(ProtoWriter)).GetMethod("WriteFieldHeader"));
                     ctx.LoadValue(local);
                     ctx.LoadReaderWriter();
                     ctx.EmitCall(ctx.MapType(typeof(ProtoWriter)).GetMethod("StartSubItem"));
                     ctx.StoreValue(local3);
                     ctx.LoadValue(this.fieldNumber);
                     ctx.LoadReaderWriter();
                     ctx.EmitCall(ctx.MapType(typeof(ProtoWriter)).GetMethod("SetPackedField"));
                 }
                 ctx.LoadAddress(local, this.ExpectedType);
                 ctx.EmitCall(method);
                 ctx.StoreValue(local2);
                 using (ctx.Using(local2))
                 {
                     CodeLabel label  = ctx.DefineLabel();
                     CodeLabel label2 = ctx.DefineLabel();
                     ctx.Branch(label2, false);
                     ctx.MarkLabel(label);
                     ctx.LoadAddress(local2, returnType);
                     ctx.EmitCall(info2);
                     Type expectedType = base.Tail.ExpectedType;
                     if ((expectedType != ctx.MapType(typeof(object))) && (info2.ReturnType == ctx.MapType(typeof(object))))
                     {
                         ctx.CastFromObject(expectedType);
                     }
                     base.Tail.EmitWrite(ctx, null);
                     ctx.MarkLabel(label2);
                     ctx.LoadAddress(local2, returnType);
                     ctx.EmitCall(info);
                     ctx.BranchIfTrue(label, false);
                 }
                 if (writePacked)
                 {
                     ctx.LoadValue(local3);
                     ctx.LoadReaderWriter();
                     ctx.EmitCall(ctx.MapType(typeof(ProtoWriter)).GetMethod("EndSubItem"));
                 }
             }
         }
     }
 }
 private static void WriteSerializePair(ILGenerator il, CompilerContext ctx, SerializerPair pair)
 {
     il.Emit(OpCodes.Ldarg_2);
     ctx.CastFromObject(pair.Type.Type);
     il.Emit(OpCodes.Ldarg_3);
     il.EmitCall(OpCodes.Call, pair.Serialize, null);
     ctx.Return();
 }
Example #3
0
 private static void EmitInvokeCallback(CompilerContext ctx, MethodInfo method, bool copyValue, Type constructType, Type type)
 {
     if (method != null)
     {
         if (copyValue)
         {
             ctx.CopyValue();
         }
         ParameterInfo[] parameters = method.GetParameters();
         bool            flag       = true;
         for (int i = 0; i < parameters.Length; i++)
         {
             Type parameterType = parameters[0].ParameterType;
             if (parameterType == ctx.MapType(typeof(SerializationContext)))
             {
                 ctx.LoadSerializationContext();
             }
             else if (parameterType == ctx.MapType(typeof(Type)))
             {
                 Type type3 = constructType;
                 if (type3 == null)
                 {
                     type3 = type;
                 }
                 ctx.LoadValue(type3);
             }
             else if (parameterType == ctx.MapType(typeof(StreamingContext)))
             {
                 ctx.LoadSerializationContext();
                 MethodInfo info = ctx.MapType(typeof(SerializationContext)).GetMethod("op_Implicit", new Type[] { ctx.MapType(typeof(SerializationContext)) });
                 if (info != null)
                 {
                     ctx.EmitCall(info);
                     flag = true;
                 }
             }
             else
             {
                 flag = false;
             }
         }
         if (!flag)
         {
             throw CallbackSet.CreateInvalidCallbackSignature(method);
         }
         ctx.EmitCall(method);
         if ((constructType != null) && (method.ReturnType == ctx.MapType(typeof(object))))
         {
             ctx.CastFromObject(type);
         }
     }
 }
Example #4
0
 void ProtoBuf.Serializers.IProtoSerializer.EmitRead(CompilerContext ctx, Local valueFrom)
 {
     if (!this.EmitDedicatedMethod(ctx, valueFrom, true))
     {
         ctx.LoadValue(valueFrom);
         if (this.type.IsValueType)
         {
             ctx.CastToObject(this.type);
         }
         ctx.LoadValue(ctx.MapMetaKeyToCompiledKey(this.key));
         ctx.LoadReaderWriter();
         ctx.EmitCall(ctx.MapType(typeof(ProtoReader)).GetMethod("ReadObject"));
         ctx.CastFromObject(this.type);
     }
 }
Example #5
0
 public void EmitRead(CompilerContext ctx, Local valueFrom)
 {
     ctx.LoadValue(valueFrom);
     ctx.CastToObject(this.type);
     ctx.LoadReaderWriter();
     ctx.LoadValue(ctx.MapMetaKeyToCompiledKey(this.key));
     if (this.type != ctx.MapType(typeof(object)))
     {
         ctx.LoadValue(this.type);
     }
     else
     {
         ctx.LoadNullRef();
     }
     ctx.LoadValue((int)this.options);
     ctx.EmitCall(ctx.MapType(typeof(BclHelpers)).GetMethod("ReadNetObject"));
     ctx.CastFromObject(this.type);
 }
        private void WriteDeserializePair(string assemblyName, TypeBuilder type, SerializerPair[] methodPairs, CompilerContext.ILVersion ilVersion, ILGenerator il, SerializerPair pair, int i,
                                          CompilerContext ctx)
        {
            Type keyType = pair.Type.Type;

            if (keyType.IsValueType)
            {
                il.Emit(OpCodes.Ldarg_2);
                il.Emit(OpCodes.Ldarg_3);
                il.EmitCall(OpCodes.Call, EmitBoxedSerializer(type, i, keyType, methodPairs, this, ilVersion, assemblyName).Member, null);
                ctx.Return();
            }
            else
            {
                il.Emit(OpCodes.Ldarg_2);
                ctx.CastFromObject(keyType);
                il.Emit(OpCodes.Ldarg_3);
                il.EmitCall(OpCodes.Call, pair.Deserialize, null);
                ctx.Return();
            }
        }
Example #7
0
        protected override void EmitWrite(CompilerContext ctx, Local valueFrom)
        {
            Type       itemType = typeof(KeyValuePair <TKey, TValue>);
            MethodInfo moveNext, current, getEnumerator = ListDecorator.GetEnumeratorInfo(
                ExpectedType, itemType, out moveNext, out current);
            Type enumeratorType = getEnumerator.ReturnType;

            MethodInfo key    = itemType.GetProperty(nameof(KeyValuePair <TKey, TValue> .Key)).GetGetMethod(),
                       @value = itemType.GetProperty(nameof(KeyValuePair <TKey, TValue> .Value)).GetGetMethod();

            using (Compiler.Local list = ctx.GetLocalWithValue(ExpectedType, valueFrom))
                using (Compiler.Local iter = new Compiler.Local(ctx, enumeratorType))
                    using (Compiler.Local token = new Compiler.Local(ctx, typeof(SubItemToken)))
                        using (Compiler.Local kvp = new Compiler.Local(ctx, itemType))
                        {
                            ctx.LoadAddress(list, ExpectedType);
                            ctx.EmitCall(getEnumerator, ExpectedType);
                            ctx.StoreValue(iter);
                            using (ctx.Using(iter))
                            {
                                Compiler.CodeLabel body = ctx.DefineLabel(), next = ctx.DefineLabel();
                                ctx.Branch(next, false);

                                ctx.MarkLabel(body);

                                ctx.LoadAddress(iter, enumeratorType);
                                ctx.EmitCall(current, enumeratorType);

                                if (itemType != typeof(object) && current.ReturnType == typeof(object))
                                {
                                    ctx.CastFromObject(itemType);
                                }
                                ctx.StoreValue(kvp);

                                ctx.LoadValue(fieldNumber);
                                ctx.LoadValue((int)wireType);
                                ctx.LoadWriter(true);
                                ctx.EmitCall(ProtoWriter.GetStaticMethod("WriteFieldHeader"));

                                ctx.LoadNullRef();
                                ctx.LoadWriter(true);
                                ctx.EmitCall(ProtoWriter.GetStaticMethod("StartSubItem"));
                                ctx.StoreValue(token);

                                ctx.LoadAddress(kvp, itemType);
                                ctx.EmitCall(key, itemType);
                                ctx.WriteNullCheckedTail(typeof(TKey), keyTail, null);

                                ctx.LoadAddress(kvp, itemType);
                                ctx.EmitCall(value, itemType);
                                ctx.WriteNullCheckedTail(typeof(TValue), Tail, null);

                                ctx.LoadValue(token);
                                ctx.LoadWriter(true);
                                ctx.EmitCall(ProtoWriter.GetStaticMethod("EndSubItem"));

                                ctx.MarkLabel(@next);
                                ctx.LoadAddress(iter, enumeratorType);
                                ctx.EmitCall(moveNext, enumeratorType);
                                ctx.BranchIfTrue(body, false);
                            }
                        }
        }
Example #8
0
        protected override void EmitWrite(CompilerContext ctx, Local valueFrom)
        {
            MethodInfo methodInfo;
            MethodInfo methodInfo1;
            Local      local;

            using (Local localWithValue = ctx.GetLocalWithValue(this.ExpectedType, valueFrom))
            {
                MethodInfo enumeratorInfo = this.GetEnumeratorInfo(ctx.Model, out methodInfo, out methodInfo1);
                Type       returnType     = enumeratorInfo.ReturnType;
                bool       writePacked    = this.WritePacked;
                using (Local local1 = new Local(ctx, returnType))
                {
                    if (writePacked)
                    {
                        local = new Local(ctx, ctx.MapType(typeof(SubItemToken)));
                    }
                    else
                    {
                        local = null;
                    }
                    using (Local local2 = local)
                    {
                        if (writePacked)
                        {
                            ctx.LoadValue(this.fieldNumber);
                            ctx.LoadValue(2);
                            ctx.LoadReaderWriter();
                            ctx.EmitCall(ctx.MapType(typeof(ProtoWriter)).GetMethod("WriteFieldHeader"));
                            ctx.LoadValue(localWithValue);
                            ctx.LoadReaderWriter();
                            ctx.EmitCall(ctx.MapType(typeof(ProtoWriter)).GetMethod("StartSubItem"));
                            ctx.StoreValue(local2);
                            ctx.LoadValue(this.fieldNumber);
                            ctx.LoadReaderWriter();
                            ctx.EmitCall(ctx.MapType(typeof(ProtoWriter)).GetMethod("SetPackedField"));
                        }
                        ctx.LoadAddress(localWithValue, this.ExpectedType);
                        ctx.EmitCall(enumeratorInfo);
                        ctx.StoreValue(local1);
                        using (IDisposable disposable = ctx.Using(local1))
                        {
                            CodeLabel codeLabel  = ctx.DefineLabel();
                            CodeLabel codeLabel1 = ctx.DefineLabel();
                            ctx.Branch(codeLabel1, false);
                            ctx.MarkLabel(codeLabel);
                            ctx.LoadAddress(local1, returnType);
                            ctx.EmitCall(methodInfo1);
                            Type expectedType = this.Tail.ExpectedType;
                            if (expectedType != ctx.MapType(typeof(object)) && methodInfo1.ReturnType == ctx.MapType(typeof(object)))
                            {
                                ctx.CastFromObject(expectedType);
                            }
                            this.Tail.EmitWrite(ctx, null);
                            ctx.MarkLabel(codeLabel1);
                            ctx.LoadAddress(local1, returnType);
                            ctx.EmitCall(methodInfo);
                            ctx.BranchIfTrue(codeLabel, false);
                        }
                        if (writePacked)
                        {
                            ctx.LoadValue(local2);
                            ctx.LoadReaderWriter();
                            ctx.EmitCall(ctx.MapType(typeof(ProtoWriter)).GetMethod("EndSubItem"));
                        }
                    }
                }
            }
        }