예제 #1
0
        /// <summary>
        ///		Emits the serializing value instructions.
        /// </summary>
        /// <param name="emitter">The emitter.</param>
        /// <param name="il">The il generator.</param>
        /// <param name="packerArgumentIndex">Index of the packer argument.</param>
        /// <param name="valueType">Type of the current member value.</param>
        /// <param name="memberName">Name of the current member.</param>
        /// <param name="nilImplication">The nil implication of the current member.</param>
        /// <param name="loadValueEmitter">The delegate which emits case specific value loading instructions.</param>
        /// <param name="localHolder">The <see cref="LocalVariableHolder"/> which holds shared local variable information.</param>
        public static void EmitSerializeValue(SerializerEmitter emitter, TracingILGenerator il, int packerArgumentIndex, Type valueType, string memberName, NilImplication nilImplication, Action <TracingILGenerator> loadValueEmitter, LocalVariableHolder localHolder)
        {
            Contract.Requires(emitter != null);
            Contract.Requires(il != null);
            Contract.Requires(packerArgumentIndex >= 0);
            Contract.Requires(valueType != null);
            Contract.Requires(loadValueEmitter != null);

            /*
             * var serializingValue = LOAD_VALUE;
             * NULL_PROHIBIT_HANDLING
             * GET_SERIALIZER.PackTo( packer, serializingValue );
             */
            var value = localHolder.GetSerializingValue(valueType);

            loadValueEmitter(il);
            il.EmitAnyStloc(value);

            if (memberName != null && nilImplication == NilImplication.Prohibit)
            {
                /*
                 *	if( serializingValue == null )(
                 *	{
                 *		throw SerializationExceptions.NewNullIsProhibited();
                 *	}
                 */

                if (!valueType.IsValueType)
                {
                    il.EmitAnyLdloc(value);
                    var endIf = il.DefineLabel("END_IF");
                    il.EmitBrtrue_S(endIf);
                    il.EmitLdstr(memberName);
                    il.EmitAnyCall(SerializationExceptions.NewNullIsProhibitedMethod);
                    il.EmitThrow();
                    il.MarkLabel(endIf);
                }
                else if (Nullable.GetUnderlyingType(valueType) != null)
                {
                    il.EmitAnyLdloca(value);
                    il.EmitGetProperty(typeof(Nullable <>).MakeGenericType(Nullable.GetUnderlyingType(valueType)).GetProperty("HasValue"));
                    var endIf = il.DefineLabel("END_IF");
                    il.EmitBrtrue_S(endIf);
                    il.EmitLdstr(memberName);
                    il.EmitAnyCall(SerializationExceptions.NewNullIsProhibitedMethod);
                    il.EmitThrow();
                    il.MarkLabel(endIf);
                }
            }

            var serializerGetter = emitter.RegisterSerializer(valueType);

            serializerGetter(il, 0);
            il.EmitAnyLdarg(packerArgumentIndex);
            il.EmitAnyLdloc(value);
            il.EmitAnyCall(typeof(MessagePackSerializer <>).MakeGenericType(valueType).GetMethod("PackTo"));
        }
예제 #2
0
        public static void EmitSerializeValue(SerializerEmitter emitter, TracingILGenerator il, int packerArgumentIndex, Type valueType, string memberName, NilImplication nilImplication, Action <TracingILGenerator> loadValueEmitter, LocalVariableHolder localHolder)
        {
            Contract.Requires(emitter != null);
            Contract.Requires(il != null);
            Contract.Requires(packerArgumentIndex >= 0);
            Contract.Requires(valueType != null);
            Contract.Requires(loadValueEmitter != null);
            LocalBuilder serializingValue = localHolder.GetSerializingValue(valueType);

            loadValueEmitter(il);
            il.EmitAnyStloc(serializingValue);
            if ((memberName != null) && (nilImplication == NilImplication.Prohibit))
            {
                Label label;
                if (!valueType.IsValueType)
                {
                    il.EmitAnyLdloc(serializingValue);
                    label = il.DefineLabel("END_IF");
                    il.EmitBrtrue_S(label);
                    il.EmitLdstr(memberName);
                    il.EmitAnyCall(SerializationExceptions.NewNullIsProhibitedMethod);
                    il.EmitThrow();
                    il.MarkLabel(label);
                }
                else if (Nullable.GetUnderlyingType(valueType) != null)
                {
                    il.EmitAnyLdloca(serializingValue);
                    il.EmitGetProperty(typeof(Nullable <>).MakeGenericType(new Type[] { Nullable.GetUnderlyingType(valueType) }).GetProperty("HasValue"));
                    label = il.DefineLabel("END_IF");
                    il.EmitBrtrue_S(label);
                    il.EmitLdstr(memberName);
                    il.EmitAnyCall(SerializationExceptions.NewNullIsProhibitedMethod);
                    il.EmitThrow();
                    il.MarkLabel(label);
                }
            }
            emitter.RegisterSerializer(valueType)(il, 0);
            il.EmitAnyLdarg(packerArgumentIndex);
            il.EmitAnyLdloc(serializingValue);
            il.EmitAnyCall(typeof(MessagePackSerializer <>).MakeGenericType(new Type[] { valueType }).GetMethod("PackTo"));
        }
예제 #3
0
		/// <summary>
		///		Emits the serializing value instructions.
		/// </summary>
		/// <param name="emitter">The emitter.</param>
		/// <param name="il">The il generator.</param>
		/// <param name="packerArgumentIndex">Index of the packer argument.</param>
		/// <param name="valueType">Type of the current member value.</param>
		/// <param name="memberName">Name of the current member.</param>
		/// <param name="nilImplication">The nil implication of the current member.</param>
		/// <param name="loadValueEmitter">The delegate which emits case specific value loading instructions.</param>
		/// <param name="localHolder">The <see cref="LocalVariableHolder"/> which holds shared local variable information.</param>
		public static void EmitSerializeValue( SerializerEmitter emitter, TracingILGenerator il, int packerArgumentIndex, Type valueType, string memberName, NilImplication nilImplication, Action<TracingILGenerator> loadValueEmitter, LocalVariableHolder localHolder )
		{
			Contract.Requires( emitter != null );
			Contract.Requires( il != null );
			Contract.Requires( packerArgumentIndex >= 0 );
			Contract.Requires( valueType != null );
			Contract.Requires( loadValueEmitter != null );

			/*
			 * var serializingValue = LOAD_VALUE;
			 * NULL_PROHIBIT_HANDLING
			 * GET_SERIALIZER.PackTo( packer, serializingValue );
			 */
			var value = localHolder.GetSerializingValue( valueType );
			loadValueEmitter( il );
			il.EmitAnyStloc( value );
			if ( memberName != null && nilImplication == NilImplication.Prohibit )
			{
				/*
				 *	if( serializingValue == null )(
				 *	{
				 *		throw SerializationExceptions.NewNullIsProhibited();
				 *	}
				 */

				if ( !valueType.IsValueType )
				{
					il.EmitAnyLdloc( value );
					var endIf = il.DefineLabel( "END_IF" );
					il.EmitBrtrue_S( endIf );
					il.EmitLdstr( memberName );
					il.EmitAnyCall( SerializationExceptions.NewNullIsProhibitedMethod );
					il.EmitThrow();
					il.MarkLabel( endIf );
				}
				else if ( Nullable.GetUnderlyingType( valueType ) != null )
				{
					il.EmitAnyLdloca( value );
					il.EmitGetProperty( typeof( Nullable<> ).MakeGenericType( Nullable.GetUnderlyingType( valueType ) ).GetProperty( "HasValue" ) );
					var endIf = il.DefineLabel( "END_IF" );
					il.EmitBrtrue_S( endIf );
					il.EmitLdstr( memberName );
					il.EmitAnyCall( SerializationExceptions.NewNullIsProhibitedMethod );
					il.EmitThrow();
					il.MarkLabel( endIf );
				}
			}

			var serializerGetter = emitter.RegisterSerializer( valueType );
			serializerGetter( il, 0 );
			il.EmitAnyLdarg( packerArgumentIndex );
			il.EmitAnyLdloc( value );
			il.EmitAnyCall( typeof( MessagePackSerializer<> ).MakeGenericType( valueType ).GetMethod( "PackTo" ) );
		}