Beispiel #1
0
 protected override void EmitWrite(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
 {
     ctx.LoadValue((int)fieldNumber);
     ctx.LoadValue((int)wireType);
     ctx.LoadWriter(true);
     ctx.EmitCall(ProtoWriter.GetStaticMethod("WriteFieldHeader"));
     Tail.EmitWrite(ctx, valueFrom);
 }
Beispiel #2
0
        protected override void EmitWrite(ProtoBuf.Compiler.CompilerContext ctx, ProtoBuf.Compiler.Local valueFrom)
        {
            // int i and T[] arr
            using (Compiler.Local arr = ctx.GetLocalWithValue(ExpectedType, valueFrom))
                using (Compiler.Local i = new ProtoBuf.Compiler.Local(ctx, typeof(int)))
                {
                    bool writePacked       = (options & OPTIONS_WritePacked) != 0;
                    bool fixedLengthPacked = writePacked && CanUsePackedPrefix();

                    using (Compiler.Local token = (writePacked && !fixedLengthPacked) ? new Compiler.Local(ctx, typeof(SubItemToken)) : null)
                    {
                        Type mappedWriter = typeof(ProtoWriter);
                        if (writePacked)
                        {
                            ctx.LoadValue(fieldNumber);
                            ctx.LoadValue((int)WireType.String);
                            ctx.LoadWriter(true);
                            ctx.EmitCall(ProtoWriter.GetStaticMethod("WriteFieldHeader"));

                            if (fixedLengthPacked)
                            {
                                // write directly - no need for buffering
                                ctx.LoadLength(arr, false);
                                ctx.LoadValue((int)packedWireType);
                                ctx.LoadWriter(true);
                                ctx.EmitCall(ProtoWriter.GetStaticMethod("WritePackedPrefix"));
                            }
                            else
                            {
                                ctx.LoadValue(arr);
                                ctx.LoadWriter(true);
                                ctx.EmitCall(ProtoWriter.GetStaticMethod("StartSubItem"));
                                ctx.StoreValue(token);
                            }
                            ctx.LoadValue(fieldNumber);
                            ctx.LoadWriter(false);
                            ctx.EmitCall(mappedWriter.GetMethod("SetPackedField"));
                        }
                        EmitWriteArrayLoop(ctx, i, arr);

                        if (writePacked)
                        {
                            if (fixedLengthPacked)
                            {
                                ctx.LoadValue(fieldNumber);
                                ctx.LoadWriter(false);
                                ctx.EmitCall(mappedWriter.GetMethod("ClearPackedField"));
                            }
                            else
                            {
                                ctx.LoadValue(token);
                                ctx.LoadWriter(true);
                                ctx.EmitCall(ProtoWriter.GetStaticMethod("EndSubItem"));
                            }
                        }
                    }
                }
        }
 public void EmitWrite(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
 {
     ctx.LoadValue(valueFrom);
     ctx.CastToObject(ExpectedType);
     ctx.LoadWriter(true);
     ctx.LoadValue(ctx.MapMetaKeyToCompiledKey(key));
     ctx.LoadValue((int)options);
     ctx.EmitCall(ProtoWriter.GetStaticMethod <BclHelpers>("WriteNetObject"));
 }
        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, typeof(SubItemToken)))
                {
                    ctx.LoadNullRef();
                    ctx.LoadWriter(true);
                    ctx.EmitCall(ProtoWriter.GetStaticMethod("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.LoadWriter(true);
                    ctx.EmitCall(ProtoWriter.GetStaticMethod("EndSubItem"));
                }
        }
        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);
                            }
                        }
        }
        }                                                            // updates field directly
#if FEAT_COMPILER
        void IProtoSerializer.EmitWrite(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
        {
            Type expected = ExpectedType;

            using (Compiler.Local loc = ctx.GetLocalWithValue(expected, valueFrom))
            {
                // pre-callbacks
                EmitCallbackIfNeeded(ctx, loc, TypeModel.CallbackType.BeforeSerialize);

                Compiler.CodeLabel startFields = ctx.DefineLabel();
                // inheritance
                if (CanHaveInheritance)
                {
                    for (int i = 0; i < serializers.Length; i++)
                    {
                        IProtoSerializer ser     = serializers[i];
                        Type             serType = ser.ExpectedType;
                        if (serType != ExpectedType)
                        {
                            Compiler.CodeLabel ifMatch = ctx.DefineLabel(), nextTest = ctx.DefineLabel();
                            ctx.LoadValue(loc);
                            ctx.TryCast(serType);
                            ctx.CopyValue();
                            ctx.BranchIfTrue(ifMatch, true);
                            ctx.DiscardValue();
                            ctx.Branch(nextTest, true);
                            ctx.MarkLabel(ifMatch);
                            if (Helpers.IsValueType(serType))
                            {
                                ctx.DiscardValue();
                                ctx.LoadValue(loc);
                                ctx.CastFromObject(serType);
                            }
                            ser.EmitWrite(ctx, null);
                            ctx.Branch(startFields, false);
                            ctx.MarkLabel(nextTest);
                        }
                    }

                    if (constructType != null && constructType != ExpectedType)
                    {
                        using (Compiler.Local actualType = new Compiler.Local(ctx, typeof(Type)))
                        {
                            // would have jumped to "fields" if an expected sub-type, so two options:
                            // a: *exactly* that type, b: an *unexpected* type
                            ctx.LoadValue(loc);
                            ctx.EmitCall(typeof(object).GetMethod("GetType"));
                            ctx.CopyValue();
                            ctx.StoreValue(actualType);
                            ctx.LoadValue(ExpectedType);
                            ctx.BranchIfEqual(startFields, true);

                            ctx.LoadValue(actualType);
                            ctx.LoadValue(constructType);
                            ctx.BranchIfEqual(startFields, true);
                        }
                    }
                    else
                    {
                        // would have jumped to "fields" if an expected sub-type, so two options:
                        // a: *exactly* that type, b: an *unexpected* type
                        ctx.LoadValue(loc);
                        ctx.EmitCall(typeof(object).GetMethod("GetType"));
                        ctx.LoadValue(ExpectedType);
                        ctx.BranchIfEqual(startFields, true);
                    }
                    // unexpected, then... note that this *might* be a proxy, which
                    // is handled by ThrowUnexpectedSubtype
                    ctx.LoadValue(ExpectedType);
                    ctx.LoadValue(loc);
                    ctx.EmitCall(typeof(object).GetMethod("GetType"));
                    ctx.EmitCall(typeof(TypeModel).GetMethod("ThrowUnexpectedSubtype",
                                                             BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static));
                }
                // fields

                ctx.MarkLabel(startFields);
                for (int i = 0; i < serializers.Length; i++)
                {
                    IProtoSerializer ser = serializers[i];
                    if (ser.ExpectedType == ExpectedType)
                    {
                        ser.EmitWrite(ctx, loc);
                    }
                }

                // extension data
                if (isExtensible)
                {
                    ctx.LoadValue(loc);
                    ctx.LoadWriter(true);
                    ctx.EmitCall(ProtoWriter.GetStaticMethod("AppendExtensionData"));
                }
                // post-callbacks
                EmitCallbackIfNeeded(ctx, loc, TypeModel.CallbackType.AfterSerialize);
            }
        }
Beispiel #7
0
        protected override void EmitWrite(ProtoBuf.Compiler.CompilerContext ctx, ProtoBuf.Compiler.Local valueFrom)
        {
            using (Compiler.Local list = ctx.GetLocalWithValue(ExpectedType, valueFrom))
            {
                MethodInfo getEnumerator = GetEnumeratorInfo(out MethodInfo moveNext, out MethodInfo current);
                Helpers.DebugAssert(moveNext != null);
                Helpers.DebugAssert(current != null);
                Helpers.DebugAssert(getEnumerator != null);
                Type enumeratorType = getEnumerator.ReturnType;
                bool writePacked    = WritePacked;
                using (Compiler.Local iter = new Compiler.Local(ctx, enumeratorType))
                           using (Compiler.Local token = writePacked ? new Compiler.Local(ctx, typeof(SubItemToken)) : null)
                           {
                               if (writePacked)
                               {
                                   ctx.LoadValue(fieldNumber);
                                   ctx.LoadValue((int)WireType.String);
                                   ctx.LoadWriter(true);
                                   ctx.EmitCall(ProtoWriter.GetStaticMethod("WriteFieldHeader"));

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

                                   ctx.LoadValue(fieldNumber);
                                   ctx.LoadWriter(false);
                                   ctx.EmitCall(typeof(ProtoWriter).GetMethod("SetPackedField"));
                               }

                               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);
                                   Type itemType = Tail.ExpectedType;
                                   if (itemType != typeof(object) && current.ReturnType == typeof(object))
                                   {
                                       ctx.CastFromObject(itemType);
                                   }
                                   Tail.EmitWrite(ctx, null);

                                   ctx.MarkLabel(@next);
                                   ctx.LoadAddress(iter, enumeratorType);
                                   ctx.EmitCall(moveNext, enumeratorType);
                                   ctx.BranchIfTrue(body, false);
                               }

                               if (writePacked)
                               {
                                   ctx.LoadValue(token);
                                   ctx.LoadWriter(true);
                                   ctx.EmitCall(ProtoWriter.GetStaticMethod("EndSubItem"));
                               }
                           }
            }
        }
Beispiel #8
0
        private bool EmitDedicatedMethod(Compiler.CompilerContext ctx, Compiler.Local valueFrom, bool read)
        {
            MethodBuilder method = ctx.GetDedicatedMethod(key, read);

            if (method == null)
            {
                return(false);
            }

            using (Compiler.Local val = ctx.GetLocalWithValue(type, valueFrom))
                using (Compiler.Local token = new ProtoBuf.Compiler.Local(ctx, typeof(SubItemToken)))
                {
                    Type rwType = read ? typeof(ProtoReader) : typeof(ProtoWriter);

                    if (read)
                    {
                        ctx.LoadReader(true);
                    }
                    else
                    {
                        // write requires the object for StartSubItem; read doesn't
                        // (if recursion-check is disabled [subtypes] then null is fine too)
                        if (Helpers.IsValueType(type) || !recursionCheck)
                        {
                            ctx.LoadNullRef();
                        }
                        else
                        {
                            ctx.LoadValue(val);
                        }
                        ctx.LoadWriter(true);
                    }
                    ctx.EmitCall(Helpers.GetStaticMethod(rwType, "StartSubItem",
                                                         read ? ProtoReader.State.ReaderStateTypeArray : new Type[] { typeof(object), rwType, ProtoWriter.ByRefStateType }));
                    ctx.StoreValue(token);

                    if (read)
                    {
                        ctx.LoadReader(true);
                        ctx.LoadValue(val);
                    }
                    else
                    {
                        ctx.LoadWriter(true);
                        ctx.LoadValue(val);
                    }
                    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(type);
                    }
                    ctx.LoadValue(token);
                    if (read)
                    {
                        ctx.LoadReader(true);
                        ctx.EmitCall(Helpers.GetStaticMethod(rwType, "EndSubItem",
                                                             new Type[] { typeof(SubItemToken), rwType, ProtoReader.State.ByRefStateType }));
                    }
                    else
                    {
                        ctx.LoadWriter(true);
                        ctx.EmitCall(ProtoWriter.GetStaticMethod("EndSubItem"));
                    }
                }
            return(true);
        }