Ejemplo n.º 1
0
        private static object CreateDelegate(Type delegateType, Type targetType, ConstructorInfo constructor, Type[] parameterTypes)
        {
            var dynamicMethod =
#if !SILVERLIGHT
                new DynamicMethod("Create" + targetType.Name, targetType, parameterTypes, restrictedSkipVisibility: true);
#else
                new DynamicMethod("Create" + targetType.Name, targetType, parameterTypes);
#endif // !SILVERLIGHT
            var il = new TracingILGenerator(dynamicMethod, NullTextWriter.Instance, isDebuggable: false);
            if (constructor == null)
            {
                // Value type's init.
                il.DeclareLocal(targetType);
                il.EmitAnyLdloca(0);
                il.EmitInitobj(targetType);
                il.EmitAnyLdloc(0);
            }
            else
            {
                for (var i = 0; i < parameterTypes.Length; i++)
                {
                    il.EmitAnyLdarg(i);
                }

                il.EmitNewobj(constructor);
            }

            il.EmitRet();
            return(dynamicMethod.CreateDelegate(delegateType));
        }
Ejemplo n.º 2
0
        public static void EmitGetUnpackerItemsCountAsInt32(TracingILGenerator il, int unpackerArgumentIndex, LocalVariableHolder localHolder)
        {
            Contract.Requires(il != null);
            Contract.Requires(unpackerArgumentIndex >= 0);
            il.EmitAnyLdloca(localHolder.RawItemsCount);
            il.EmitInitobj(typeof(long));
            il.BeginExceptionBlock();
            il.EmitAnyLdarg(unpackerArgumentIndex);
            il.EmitGetProperty(_Unpacker.ItemsCount);
            il.EmitAnyStloc(localHolder.RawItemsCount);
            il.BeginCatchBlock(typeof(InvalidOperationException));
            il.EmitAnyCall(SerializationExceptions.NewIsIncorrectStreamMethod);
            il.EmitThrow();
            il.EndExceptionBlock();
            il.EmitAnyLdloc(localHolder.RawItemsCount);
            il.EmitLdc_I8(0x7fffffffL);
            Label target = il.DefineLabel();

            il.EmitBle_S(target);
            il.EmitAnyCall(SerializationExceptions.NewIsTooLargeCollectionMethod);
            il.EmitThrow();
            il.MarkLabel(target);
            il.EmitAnyLdloc(localHolder.RawItemsCount);
            il.EmitConv_I4();
        }
Ejemplo n.º 3
0
        public static void EmitDeserializeValue(SerializerEmitter emitter, TracingILGenerator il, int unpackerArgumentIndex, LocalBuilder result, SerializingMember member, LocalVariableHolder localHolder)
        {
            LocalBuilder deserializedValue;

            Contract.Requires(emitter != null);
            Contract.Requires(il != null);
            Contract.Requires(unpackerArgumentIndex >= 0);
            Contract.Requires(result != null);
            Label endOfDeserialization = il.DefineLabel("END_OF_DESERIALIZATION");
            bool  flag = false;

            if (member.Member.GetMemberValueType().GetIsValueType() && (Nullable.GetUnderlyingType(member.Member.GetMemberValueType()) == null))
            {
                deserializedValue = localHolder.GetDeserializedValue(typeof(Nullable <>).MakeGenericType(new Type[] { member.Member.GetMemberValueType() }));
                flag = true;
            }
            else
            {
                deserializedValue = localHolder.GetDeserializedValue(member.Member.GetMemberValueType());
            }
            if (deserializedValue.LocalType.GetIsValueType())
            {
                il.EmitAnyLdloca(deserializedValue);
                il.EmitInitobj(deserializedValue.LocalType);
            }
            else
            {
                il.EmitLdnull();
                il.EmitAnyStloc(deserializedValue);
            }
            EmitDeserializeValueCore(emitter, il, unpackerArgumentIndex, deserializedValue, result.LocalType, new SerializingMember?(member), member.Contract.Name, endOfDeserialization, localHolder);
            if (result.LocalType.IsValueType)
            {
                il.EmitAnyLdloca(result);
            }
            else
            {
                il.EmitAnyLdloc(result);
            }
            if (flag)
            {
                il.EmitAnyLdloca(deserializedValue);
                il.EmitGetProperty(typeof(Nullable <>).MakeGenericType(new Type[] { member.Member.GetMemberValueType() }).GetProperty("Value"));
            }
            else
            {
                il.EmitAnyLdloc(deserializedValue);
            }
            EmitStoreValue(il, member.Member);
            il.MarkLabel(endOfDeserialization);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Emits gets <see cref="Unpacker.ItemsCount"/> with exception handling.
        /// Note that final state is the value is pushed top of the evaluation stack.
        /// </summary>
        /// <param name="il">IL generator.</param>
        /// <param name="unpackerArgumentIndex">Argument index of the unpacker.</param>
        /// <param name="localHolder">The <see cref="LocalVariableHolder"/> which holds shared local variable information.</param>
        public static void EmitGetUnpackerItemsCountAsInt32(TracingILGenerator il, int unpackerArgumentIndex, LocalVariableHolder localHolder)
        {
            Contract.Requires(il != null);
            Contract.Requires(unpackerArgumentIndex >= 0);

            /*
             *	long rawItemsCount;
             *	try
             *	{
             *		rawItemsCount = unpacker.ItemsCount;
             *	}
             *	catch ( InvalidOperationException ex )
             *	{
             *		throw SerializationExceptions.NewIsIncorrectStream( ex );
             *	}
             *
             *	if( rawItemsCount > Int32.MaxValue )
             *	{
             *		throw SerializationException.NewIsTooLargeCollection();
             *	}
             *
             *	... unchecked( ( int )rawItemsCount );
             */
            il.EmitAnyLdloca(localHolder.RawItemsCount);
            il.EmitInitobj(typeof(long));
            il.BeginExceptionBlock();
            il.EmitAnyLdarg(unpackerArgumentIndex);
            il.EmitGetProperty(Metadata._Unpacker.ItemsCount);
            il.EmitAnyStloc(localHolder.RawItemsCount);
            il.BeginCatchBlock(typeof(InvalidOperationException));
            il.EmitAnyCall(SerializationExceptions.NewIsIncorrectStreamMethod);
            il.EmitThrow();
            il.EndExceptionBlock();

            il.EmitAnyLdloc(localHolder.RawItemsCount);
            il.EmitLdc_I8(Int32.MaxValue);
            var endIf = il.DefineLabel();

            il.EmitBle_S(endIf);
            il.EmitAnyCall(SerializationExceptions.NewIsTooLargeCollectionMethod);
            il.EmitThrow();
            il.MarkLabel(endIf);
            il.EmitAnyLdloc(localHolder.RawItemsCount);
            il.EmitConv_I4();
        }
Ejemplo n.º 5
0
        private static void CreateUnpacking(SerializerEmitter emitter)
        {
            TracingILGenerator unpackFromMethodILGenerator = emitter.GetUnpackFromMethodILGenerator();

            try
            {
                LocalBuilder local    = unpackFromMethodILGenerator.DeclareLocal(typeof(MessagePackObject?), "mayBeNullData");
                LocalBuilder builder2 = unpackFromMethodILGenerator.DeclareLocal(typeof(MessagePackObject), "data");
                LocalBuilder builder3 = unpackFromMethodILGenerator.DeclareLocal(typeof(T), "result");
                LocalBuilder result   = unpackFromMethodILGenerator.DeclareLocal(NullableMessagePackSerializer <T> ._nullableTValueProperty.PropertyType, "value");
                Label        target   = unpackFromMethodILGenerator.DefineLabel("END_IF");
                Label        label2   = unpackFromMethodILGenerator.DefineLabel("END_METHOD");
                unpackFromMethodILGenerator.EmitAnyLdarg(1);
                unpackFromMethodILGenerator.EmitGetProperty(NullableMessagePackSerializer.UnpackerDataProperty);
                unpackFromMethodILGenerator.EmitAnyStloc(local);
                unpackFromMethodILGenerator.EmitAnyLdloca(local);
                unpackFromMethodILGenerator.EmitGetProperty(NullableMessagePackSerializer.Nullable_MessagePackObject_ValueProperty);
                unpackFromMethodILGenerator.EmitAnyStloc(builder2);
                unpackFromMethodILGenerator.EmitAnyLdloca(builder2);
                unpackFromMethodILGenerator.EmitGetProperty(NullableMessagePackSerializer.MessagePackObject_IsNilProperty);
                unpackFromMethodILGenerator.EmitBrfalse_S(target);
                unpackFromMethodILGenerator.EmitAnyLdloca(builder3);
                unpackFromMethodILGenerator.EmitInitobj(builder3.LocalType);
                unpackFromMethodILGenerator.EmitBr_S(label2);
                unpackFromMethodILGenerator.MarkLabel(target);
                Emittion.EmitUnpackFrom(emitter, unpackFromMethodILGenerator, result, 1);
                unpackFromMethodILGenerator.EmitAnyLdloc(result);
                unpackFromMethodILGenerator.EmitAnyCall(NullableMessagePackSerializer <T> ._nullableTImplicitOperator);
                unpackFromMethodILGenerator.EmitAnyStloc(builder3);
                unpackFromMethodILGenerator.MarkLabel(label2);
                unpackFromMethodILGenerator.EmitAnyLdloc(builder3);
                unpackFromMethodILGenerator.EmitRet();
            }
            finally
            {
                unpackFromMethodILGenerator.FlushTrace();
                emitter.FlushTrace();
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Emits the deserialize value.
        /// </summary>
        /// <param name="emitter">The emitter.</param>
        /// <param name="il">The il generator.</param>
        /// <param name="unpackerArgumentIndex">Index of the unpacker argument.</param>
        /// <param name="result">The result local variable which represents the result of deserialization.</param>
        /// <param name="member">The metadata for nil implication. Specify <c>null</c> if nil implication is not needed.</param>
        /// <param name="localHolder">The <see cref="LocalVariableHolder"/> which holds shared local variable information.</param>
        public static void EmitDeserializeValue(SerializerEmitter emitter, TracingILGenerator il, int unpackerArgumentIndex, LocalBuilder result, SerializingMember member, LocalVariableHolder localHolder)
        {
            Contract.Requires(emitter != null);
            Contract.Requires(il != null);
            Contract.Requires(unpackerArgumentIndex >= 0);
            Contract.Requires(result != null);

            var endOfDeserialization = il.DefineLabel("END_OF_DESERIALIZATION");

            /*
             *
             * if( !unpacker.IsArrayHeader && !unpacker.IsMapHeader )
             * {
             *		valueN = GET_SERIALIZER.UnpackFrom( unpacker );
             * }
             * else
             * {
             *		using( var subtreeUnpacker = unpacker.ReadSubtree )
             *		{
             *			valueN = GET_SERIALIZER.UnpackFrom( unpacker );
             *		}
             * }
             *
             * isValueNUnpacked = true;
             * END_OF_DESERIALIZATION:
             */
            LocalBuilder value;
            bool         useDummyNullableValue = false;

            if (member.Member.GetMemberValueType().GetIsValueType() &&
                Nullable.GetUnderlyingType(member.Member.GetMemberValueType()) == null)
            {
                // Use temporary nullable value for nil implication.
                value = localHolder.GetDeserializedValue(
                    typeof(Nullable <>).MakeGenericType(member.Member.GetMemberValueType()));
                useDummyNullableValue = true;
            }
            else
            {
                value = localHolder.GetDeserializedValue(member.Member.GetMemberValueType());
            }

            if (value.LocalType.GetIsValueType())
            {
                il.EmitAnyLdloca(value);
                il.EmitInitobj(value.LocalType);
            }
            else
            {
                il.EmitLdnull();
                il.EmitAnyStloc(value);
            }

            EmitDeserializeValueCore(emitter, il, unpackerArgumentIndex, value, result.LocalType, member, member.Contract.Name, endOfDeserialization, localHolder);

            if (result.LocalType.IsValueType)
            {
                il.EmitAnyLdloca(result);
            }
            else
            {
                il.EmitAnyLdloc(result);
            }

            if (useDummyNullableValue)
            {
                il.EmitAnyLdloca(value);
                il.EmitGetProperty(typeof(Nullable <>).MakeGenericType(member.Member.GetMemberValueType()).GetProperty("Value"));
            }
            else
            {
                il.EmitAnyLdloc(value);
            }

            EmitStoreValue(il, member.Member);

            il.MarkLabel(endOfDeserialization);
        }
Ejemplo n.º 7
0
        private static void EmitUnpackMembersFromArray(SerializerEmitter emitter, TracingILGenerator unpackerIL, SerializingMember[] entries, LocalBuilder result, LocalVariableHolder localHolder, Label endOfDeserialization)
        {
            /*
             *  int unpacked = 0;
             *  int itemsCount = unpacker.ItemsCount;
             *
             *  :
             *  if( unpacked >= itemsCount )
             *  {
             *		HandleNilImplication(...);
             *  }
             *  else
             *  {
             *  #if PRIMITIVE
             *		if( !unpacker.ReadT( out local1 ) )
             *		{
             *			throw SerializationExceptions.NewUnexpectedEndOfStreamMethod();
             *		}
             *  #else
             *		if( !unpacker.Read() )
             *		{
             *			throw SerializationExceptions.NewUnexpectedEndOfStreamMethod();
             *		}
             *
             *		local1 = this._serializer1.Unpack
             *	#endif
             *		unpacked++;
             *	}
             *	:
             */

            // TODO: Supports ExtensionObject like round-tripping.

            var itemsCount = localHolder.ItemsCount;
            var unpacked   = unpackerIL.DeclareLocal(typeof(int), "unpacked");

            Emittion.EmitGetUnpackerItemsCountAsInt32(unpackerIL, 1, localHolder);
            unpackerIL.EmitAnyStloc(itemsCount);

            for (int i = 0; i < entries.Length; i++)
            {
                var endIf0 = unpackerIL.DefineLabel("END_IF");
                var else0  = unpackerIL.DefineLabel("ELSE");

                unpackerIL.EmitAnyLdloc(unpacked);
                unpackerIL.EmitAnyLdloc(itemsCount);
                unpackerIL.EmitBge(else0);

                if (entries[i].Member == null)
                {
                    Emittion.EmitGeneralRead(unpackerIL, 1);
                    // Ignore undefined member -- Nop.
                }
                else if (UnpackHelpers.IsReadOnlyAppendableCollectionMember(entries[i].Member))
                {
                    Emittion.EmitDeserializeCollectionValue(
                        emitter,
                        unpackerIL,
                        1,
                        result,
                        entries[i].Member,
                        entries[i].Member.GetMemberValueType(),
                        entries[i].Contract.NilImplication,
                        localHolder
                        );
                }
                else
                {
                    Emittion.EmitDeserializeValue(
                        emitter,
                        unpackerIL,
                        1,
                        result,
                        entries[i],
                        localHolder
                        );
                }

                unpackerIL.EmitAnyLdloc(unpacked);
                unpackerIL.EmitLdc_I4_1();
                unpackerIL.EmitAdd();
                unpackerIL.EmitAnyStloc(unpacked);

                unpackerIL.EmitBr(endIf0);

                unpackerIL.MarkLabel(else0);

                if (entries[i].Member != null)
                {
                    // Respect nil implication.
                    switch (entries[i].Contract.NilImplication)
                    {
                    case NilImplication.MemberDefault:
                    {
                        unpackerIL.EmitBr(endOfDeserialization);
                        break;
                    }

                    case NilImplication.Null:
                    {
                        if (entries[i].Member.GetMemberValueType().GetIsValueType())
                        {
                            if (Nullable.GetUnderlyingType(entries[i].Member.GetMemberValueType()) == null)
                            {
                                // val type

                                /*
                                 * if( value == null )
                                 * {
                                 *		throw SerializationEceptions.NewValueTypeCannotBeNull( "...", typeof( MEMBER ), typeof( TYPE ) );
                                 * }
                                 */
                                unpackerIL.EmitLdstr(entries[i].Contract.Name);
                                unpackerIL.EmitLdtoken(entries[i].Member.GetMemberValueType());
                                unpackerIL.EmitAnyCall(Metadata._Type.GetTypeFromHandle);
                                unpackerIL.EmitLdtoken(entries[i].Member.DeclaringType);
                                unpackerIL.EmitAnyCall(Metadata._Type.GetTypeFromHandle);
                                unpackerIL.EmitAnyCall(SerializationExceptions.NewValueTypeCannotBeNull3Method);
                                unpackerIL.EmitThrow();
                            }
                            else
                            {
                                // nullable
                                unpackerIL.EmitAnyLdloca(localHolder.GetDeserializedValue(entries[i].Member.GetMemberValueType()));
                                unpackerIL.EmitInitobj(entries[i].Member.GetMemberValueType());
                                unpackerIL.EmitAnyLdloc(result);
                                unpackerIL.EmitAnyLdloc(localHolder.GetDeserializedValue(entries[i].Member.GetMemberValueType()));
                                Emittion.EmitStoreValue(unpackerIL, entries[i].Member);
                            }
                        }
                        else
                        {
                            // ref type
                            unpackerIL.EmitAnyLdloc(result);
                            unpackerIL.EmitLdnull();
                            Emittion.EmitStoreValue(unpackerIL, entries[i].Member);
                        }

                        break;
                    }

                    case NilImplication.Prohibit:
                    {
                        unpackerIL.EmitLdstr(entries[i].Contract.Name);
                        unpackerIL.EmitAnyCall(SerializationExceptions.NewNullIsProhibitedMethod);
                        unpackerIL.EmitThrow();
                        break;
                    }
                    }
                }

                unpackerIL.MarkLabel(endIf0);
            }
        }