Ejemplo n.º 1
0
        void IProtoSerializer.EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
        {
            Type expected = ExpectedType;

            Helpers.DebugAssert(valueFrom != null);

            using (Compiler.Local loc = ctx.GetLocalWithValue(expected, valueFrom))
                using (Compiler.Local fieldNumber = new Compiler.Local(ctx, ctx.MapType(typeof(int))))
                {
                    // pre-callbacks
                    if (HasCallbacks(TypeModel.CallbackType.BeforeDeserialize))
                    {
                        if (ExpectedType.IsValueType)
                        {
                            EmitCallbackIfNeeded(ctx, loc, TypeModel.CallbackType.BeforeDeserialize);
                        }
                        else
                        { // could be null
                            Compiler.CodeLabel callbacksDone = ctx.DefineLabel();
                            ctx.LoadValue(loc);
                            ctx.BranchIfFalse(callbacksDone, false);
                            EmitCallbackIfNeeded(ctx, loc, TypeModel.CallbackType.BeforeDeserialize);
                            ctx.MarkLabel(callbacksDone);
                        }
                    }

                    Compiler.CodeLabel @continue = ctx.DefineLabel(), processField = ctx.DefineLabel();
                    ctx.Branch(@continue, false);

                    ctx.MarkLabel(processField);
                    foreach (BasicList.Group group in BasicList.GetContiguousGroups(fieldNumbers, serializers))
                    {
                        Compiler.CodeLabel tryNextField = ctx.DefineLabel();
                        int groupItemCount = group.Items.Count;
                        if (groupItemCount == 1)
                        {
                            // discreet group; use an equality test
                            ctx.LoadValue(fieldNumber);
                            ctx.LoadValue(group.First);
                            Compiler.CodeLabel processThisField = ctx.DefineLabel();
                            ctx.BranchIfEqual(processThisField, true);
                            ctx.Branch(tryNextField, false);
                            WriteFieldHandler(ctx, expected, loc, processThisField, @continue, (IProtoSerializer)group.Items[0]);
                        }
                        else
                        { // implement as a jump-table-based switch
                            ctx.LoadValue(fieldNumber);
                            ctx.LoadValue(group.First);
                            ctx.Subtract(); // jump-tables are zero-based
                            Compiler.CodeLabel[] jmp = new Compiler.CodeLabel[groupItemCount];
                            for (int i = 0; i < groupItemCount; i++)
                            {
                                jmp[i] = ctx.DefineLabel();
                            }
                            ctx.Switch(jmp);
                            // write the default...
                            ctx.Branch(tryNextField, false);
                            for (int i = 0; i < groupItemCount; i++)
                            {
                                WriteFieldHandler(ctx, expected, loc, jmp[i], @continue, (IProtoSerializer)group.Items[i]);
                            }
                        }
                        ctx.MarkLabel(tryNextField);
                    }

                    EmitCreateIfNull(ctx, loc);
                    ctx.LoadReaderWriter();
                    if (isExtensible)
                    {
                        ctx.LoadValue(loc);
                        ctx.EmitCall(ctx.MapType(typeof(ProtoReader)).GetMethod("AppendExtensionData"));
                    }
                    else
                    {
                        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);

                    EmitCreateIfNull(ctx, loc);
                    // post-callbacks
                    EmitCallbackIfNeeded(ctx, loc, TypeModel.CallbackType.AfterDeserialize);

                    if (valueFrom != null && !loc.IsSame(valueFrom))
                    {
                        ctx.LoadValue(loc);
                        ctx.Cast(valueFrom.Type);
                        ctx.StoreValue(valueFrom);
                    }
                }
        }
Ejemplo n.º 2
0
        }                                                            // 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 != forType)
                        {
                            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);
                            ser.EmitWrite(ctx, null);
                            ctx.Branch(startFields, false);
                            ctx.MarkLabel(nextTest);
                        }
                    }


                    if (constructType != null && constructType != forType)
                    {
                        using (Compiler.Local actualType = new Compiler.Local(ctx, ctx.MapType(typeof(System.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(ctx.MapType(typeof(object)).GetMethod("GetType"));
                            ctx.CopyValue();
                            ctx.StoreValue(actualType);
                            ctx.LoadValue(forType);
                            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(ctx.MapType(typeof(object)).GetMethod("GetType"));
                        ctx.LoadValue(forType);
                        ctx.BranchIfEqual(startFields, true);
                    }
                    // unexpected, then... note that this *might* be a proxy, which
                    // is handled by ThrowUnexpectedSubtype
                    ctx.LoadValue(forType);
                    ctx.LoadValue(loc);
                    ctx.EmitCall(ctx.MapType(typeof(object)).GetMethod("GetType"));
                    ctx.EmitCall(ctx.MapType(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 == forType)
                    {
                        ser.EmitWrite(ctx, loc);
                    }
                }

                // extension data
                if (isExtensible)
                {
                    ctx.LoadValue(loc);
                    ctx.LoadReaderWriter();
                    ctx.EmitCall(ctx.MapType(typeof(ProtoWriter)).GetMethod("AppendExtensionData"));
                }
                // post-callbacks
                EmitCallbackIfNeeded(ctx, loc, TypeModel.CallbackType.AfterSerialize);
            }
        }
Ejemplo n.º 3
0
        static void EmitInvokeCallback(Compiler.CompilerContext ctx, MethodInfo method, bool copyValue, Type constructType, Type type)
        {
            if (method != null)
            {
                if (copyValue)
                {
                    ctx.CopyValue();           // assumes the target is on the stack, and that we want to *retain* it on the stack
                }
                ParameterInfo[] parameters = method.GetParameters();
                bool            handled    = 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(System.Type)))
                    {
                        Type tmp = constructType;
                        if (tmp == null)
                        {
                            tmp = type;              // no ?? in some C# profiles
                        }
                        ctx.LoadValue(tmp);
                    }
#if PLAT_BINARYFORMATTER
                    else if (parameterType == ctx.MapType(typeof(System.Runtime.Serialization.StreamingContext)))
                    {
                        ctx.LoadSerializationContext();
                        MethodInfo op = ctx.MapType(typeof(SerializationContext)).GetMethod("op_Implicit", new Type[] { ctx.MapType(typeof(SerializationContext)) });
                        if (op != null)
                        { // it isn't always! (framework versions, etc)
                            ctx.EmitCall(op);
                            handled = true;
                        }
                    }
#endif
                    else
                    {
                        handled = false;
                    }
                }
                if (handled)
                {
                    ctx.EmitCall(method);
                    if (constructType != null)
                    {
                        if (method.ReturnType == ctx.MapType(typeof(object)))
                        {
                            ctx.CastFromObject(type);
                        }
                    }
                }
                else
                {
                    throw Meta.CallbackSet.CreateInvalidCallbackSignature(method);
                }
            }
        }
Ejemplo n.º 4
0
        protected override void EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
        {
            bool writeValue;

            SanityCheck(ctx.Model, property, Tail, out writeValue, ctx.NonPublic, ctx.AllowInternal(property));
            if (ExpectedType.IsValueType && valueFrom == null)
            {
                throw new InvalidOperationException("Attempt to mutate struct on the head of the stack; changes would be lost");
            }

            ctx.LoadAddress(valueFrom, ExpectedType); // stack is: old-addr
            if (writeValue && Tail.RequiresOldValue)
            {                                         // need to read and write
                ctx.CopyValue();
            }
            // stack is: [old-addr]|old-addr
            if (Tail.RequiresOldValue)
            {
                ctx.LoadValue(property);                                // stack is: [old-addr]|old-value
            }
            ctx.ReadNullCheckedTail(property.PropertyType, Tail, null); // stack is [old-addr]|[new-value]

            if (writeValue)
            {
                // stack is old-addr|new-value
                Compiler.CodeLabel @skip = new Compiler.CodeLabel(), allDone = new Compiler.CodeLabel(); // <=== default structs
                if (!property.PropertyType.IsValueType)
                {                                                                                        // if the tail returns a null, intepret that as *no assign*
                    ctx.CopyValue();                                                                     // old-addr|new-value|new-value
                    @skip   = ctx.DefineLabel();
                    allDone = ctx.DefineLabel();
                    ctx.BranchIfFalse(@skip, true); // old-addr|new-value
                }

                if (shadowSetter == null)
                {
                    ctx.StoreValue(property);
                }
                else
                {
                    ctx.EmitCall(shadowSetter);
                }
                if (!property.PropertyType.IsValueType)
                {
                    ctx.Branch(allDone, true);

                    ctx.MarkLabel(@skip); // old-addr|new-value
                    ctx.DiscardValue();
                    ctx.DiscardValue();

                    ctx.MarkLabel(allDone);
                }
            }
            else
            { // don't want return value; drop it if anything there
                // stack is [new-value]
                if (Tail.ReturnsValue)
                {
                    ctx.DiscardValue();
                }
            }
        }
Ejemplo n.º 5
0
        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, 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.LoadReader(true);
                        ctx.EmitCall(typeof(ProtoReader).GetMethod("SkipField", ProtoReader.State.StateTypeArray));

                        ctx.MarkLabel(@continue);
                        ctx.EmitBasicRead("ReadFieldHeader", 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
                        }
                    }
                }
            }
        }
Ejemplo n.º 6
0
        public void EmitRead(Compiler.CompilerContext ctx, Compiler.Local incoming)
        {
            using (ctx.StartDebugBlockAuto(this))
            {
                var g = ctx.G;

                using (Compiler.Local objValue = ctx.GetLocalWithValueForEmitRead(this, incoming))
                    using (Compiler.Local reservedTrap = new Local(ctx, ctx.MapType(typeof(int))))
                        using (Compiler.Local token = new Local(ctx, ctx.MapType(typeof(SubItemToken))))
                            using (Compiler.Local refLocalToNoteObject = new Local(ctx, ctx.MapType(typeof(object))))
                            {
                                ctx.EmitCallReserveNoteObject();
                                ctx.StoreValue(reservedTrap);

                                Compiler.Local[] locals = new Compiler.Local[_members.Length];
                                try
                                {
                                    g.Assign(token, g.ReaderFunc.StartSubItem());

                                    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].Member.MemberType)
                                        {
                                        case MemberTypes.Field:
                                            ctx.LoadValue((FieldInfo)_members[i].Member);
                                            break;

                                        case MemberTypes.Property:
                                            ctx.LoadValue((PropertyInfo)_members[i].Member);
                                            break;
                                        }
                                        ctx.StoreValue(locals[i]);
                                    }

                                    if (!ExpectedType.IsValueType)
                                    {
                                        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.EmitReadReturnsValue)
                                            {
                                                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(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);
                                    }

                                    g.Reader.EndSubItem(token);

                                    for (int i = 0; i < locals.Length; i++)
                                    {
                                        ctx.LoadValue(locals[i]);
                                    }
                                    ctx.EmitCtor(_ctor);
                                    ctx.StoreValue(objValue);

                                    ctx.LoadValue(objValue);
                                    ctx.CastToObject(ctx.MapType(_ctor.DeclaringType));
                                    ctx.StoreValue(refLocalToNoteObject);

                                    ctx.LoadValue(reservedTrap);
                                    ctx.LoadAddress(refLocalToNoteObject, refLocalToNoteObject.Type);
                                    ctx.EmitCallNoteReservedTrappedObject();

                                    if (EmitReadReturnsValue)
                                    {
                                        ctx.LoadValue(objValue);
                                    }
                                }
                                finally
                                {
                                    for (int i = 0; i < locals.Length; i++)
                                    {
                                        if (!locals[i].IsNullRef())
                                        {
                                            locals[i].Dispose(); // release for re-use
                                        }
                                    }
                                }
                            }
            }
        }
Ejemplo n.º 7
0
        protected override void EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
        {
            using (Compiler.Local oldValue = ctx.GetLocalWithValue(ExpectedType, valueFrom))
                using (Compiler.Local token = new Compiler.Local(ctx, typeof(SubItemToken)))
                    using (Compiler.Local field = new Compiler.Local(ctx, typeof(int)))
                    {
                        ctx.LoadReader(true);
                        ctx.EmitCall(typeof(ProtoReader).GetMethod("StartSubItem",
                                                                   Compiler.ReaderUtil.ReaderStateTypeArray));
                        ctx.StoreValue(token);

                        Compiler.CodeLabel next = ctx.DefineLabel(), processField = ctx.DefineLabel(), end = ctx.DefineLabel();

                        ctx.MarkLabel(next);

                        ctx.EmitBasicRead("ReadFieldHeader", typeof(int));
                        ctx.CopyValue();
                        ctx.StoreValue(field);
                        ctx.LoadValue(Tag); // = 1 - process
                        ctx.BranchIfEqual(processField, true);
                        ctx.LoadValue(field);
                        ctx.LoadValue(1); // < 1 - exit
                        ctx.BranchIfLess(end, false);

                        // default: skip
                        ctx.LoadReader(true);
                        ctx.EmitCall(typeof(ProtoReader).GetMethod("SkipField", Compiler.ReaderUtil.StateTypeArray));
                        ctx.Branch(next, true);

                        // process
                        ctx.MarkLabel(processField);
                        if (Tail.RequiresOldValue)
                        {
                            if (Helpers.IsValueType(ExpectedType))
                            {
                                ctx.LoadAddress(oldValue, ExpectedType);
                                ctx.EmitCall(ExpectedType.GetMethod("GetValueOrDefault", Helpers.EmptyTypes));
                            }
                            else
                            {
                                ctx.LoadValue(oldValue);
                            }
                        }
                        Tail.EmitRead(ctx, null);
                        // note we demanded always returns a value
                        if (Helpers.IsValueType(ExpectedType))
                        {
                            ctx.EmitCtor(ExpectedType, Tail.ExpectedType); // re-nullable<T> it
                        }
                        ctx.StoreValue(oldValue);
                        ctx.Branch(next, false);

                        // outro
                        ctx.MarkLabel(end);

                        ctx.LoadValue(token);
                        ctx.LoadReader(true);
                        ctx.EmitCall(typeof(ProtoReader).GetMethod("EndSubItem",
                                                                   new[] { typeof(SubItemToken), typeof(ProtoReader), Compiler.ReaderUtil.ByRefStateType }));
                        ctx.LoadValue(oldValue); // load the old value
                    }
        }
Ejemplo n.º 8
0
        void IProtoTypeSerializer.EmitCallback(Compiler.CompilerContext ctx, Compiler.Local valueFrom, TypeModel.CallbackType callbackType)

        {
            bool actuallyHasInheritance = false;

            if (CanHaveInheritance)

            {
                for (int i = 0; i < serializers.Length; i++)

                {
                    IProtoSerializer ser = serializers[i];

                    if (ser.ExpectedType != forType && ((IProtoTypeSerializer)ser).HasCallbacks(callbackType))

                    {
                        actuallyHasInheritance = true;
                    }
                }
            }



            Helpers.DebugAssert(((IProtoTypeSerializer)this).HasCallbacks(callbackType), "Shouldn't be calling this if there is nothing to do");

            MethodInfo method = callbacks == null ? null : callbacks[callbackType];

            if (method == null && !actuallyHasInheritance)

            {
                return;
            }

            ctx.LoadAddress(valueFrom, ExpectedType);

            EmitInvokeCallback(ctx, method, actuallyHasInheritance, null, forType);



            if (actuallyHasInheritance)

            {
                Compiler.CodeLabel @break = ctx.DefineLabel();

                for (int i = 0; i < serializers.Length; i++)

                {
                    IProtoSerializer ser = serializers[i];

                    IProtoTypeSerializer typeser;

                    Type serType = ser.ExpectedType;

                    if (serType != forType &&

                        (typeser = (IProtoTypeSerializer)ser).HasCallbacks(callbackType))

                    {
                        Compiler.CodeLabel ifMatch = ctx.DefineLabel(), nextTest = ctx.DefineLabel();

                        ctx.CopyValue();

                        ctx.TryCast(serType);

                        ctx.CopyValue();

                        ctx.BranchIfTrue(ifMatch, true);

                        ctx.DiscardValue();

                        ctx.Branch(nextTest, false);

                        ctx.MarkLabel(ifMatch);

                        typeser.EmitCallback(ctx, null, callbackType);

                        ctx.Branch(@break, false);

                        ctx.MarkLabel(nextTest);
                    }
                }

                ctx.MarkLabel(@break);

                ctx.DiscardValue();
            }
        }
Ejemplo n.º 9
0
        void IProtoTypeSerializer.EmitCreateInstance(Compiler.CompilerContext ctx)

        {
            // different ways of creating a new instance

            bool callNoteObject = true;

            if (factory != null)

            {
                EmitInvokeCallback(ctx, factory, false, constructType, forType);
            }

            else if (!useConstructor)

            {   // DataContractSerializer style
                ctx.LoadValue(constructType);

                ctx.EmitCall(ctx.MapType(typeof(BclHelpers)).GetMethod("GetUninitializedObject"));

                ctx.Cast(forType);
            }

            else if (constructType.IsClass && hasConstructor)

            {   // XmlSerializer style
                ctx.EmitCtor(constructType);
            }

            else

            {
                ctx.LoadValue(ExpectedType);

                ctx.EmitCall(ctx.MapType(typeof(TypeModel)).GetMethod("ThrowCannotCreateInstance",

                                                                      BindingFlags.Static | BindingFlags.Public));

                ctx.LoadNullRef();

                callNoteObject = false;
            }

            if (callNoteObject)

            {
                // track root object creation

                ctx.CopyValue();

                ctx.LoadReaderWriter();

                ctx.EmitCall(ctx.MapType(typeof(ProtoReader)).GetMethod("NoteObject",

                                                                        BindingFlags.Static | BindingFlags.Public));
            }

            if (baseCtorCallbacks != null)

            {
                for (int i = 0; i < baseCtorCallbacks.Length; i++)

                {
                    EmitInvokeCallback(ctx, baseCtorCallbacks[i], true, null, forType);
                }
            }
        }
Ejemplo n.º 10
0
        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
        }