示例#1
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);
        }
示例#2
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 );
		}
示例#3
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);
        }
示例#4
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);
            }
        }