/// <summary> /// Emits the deserialize collection 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="target">The target collection variable.</param> /// <param name="member">The deserializing member metadata which holds the collection.</param> /// <param name="memberType">Type of the deserializing member.</param> /// <param name="nilImplication">The nil implication.</param> /// <param name="localHolder">The <see cref="LocalVariableHolder"/> which holds shared local variable information.</param> public static void EmitDeserializeCollectionValue( SerializerEmitter emitter, TracingILGenerator il, int unpackerArgumentIndex, LocalBuilder target, MemberInfo member, Type memberType, NilImplication nilImplication, LocalVariableHolder localHolder ) { Contract.Requires( emitter != null ); Contract.Requires( il != null ); Contract.Requires( unpackerArgumentIndex >= 0 ); Contract.Requires( target != null ); Contract.Requires( member != null ); Contract.Requires( memberType != null ); Contract.Requires( localHolder != null ); var endOfDeserialization = il.DefineLabel( "END_OF_DESERIALIZATION" ); EmitGeneralRead( il, unpackerArgumentIndex ); switch ( nilImplication ) { case NilImplication.MemberDefault: { /* * if( unpacker.Data.Value.IsNil ) * { * // Skip current. * goto END_OF_DESERIALIZATION; * } */ il.EmitAnyLdarg( unpackerArgumentIndex ); il.EmitGetProperty( Metadata._Unpacker.Data ); var data = localHolder.UnpackedData; il.EmitAnyStloc( data ); il.EmitAnyLdloca( data ); il.EmitGetProperty( Metadata._Nullable<MessagePackObject>.Value ); var dataValue = localHolder.UnpackedDataValue; il.EmitAnyStloc( dataValue ); il.EmitAnyLdloca( dataValue ); il.EmitGetProperty( Metadata._MessagePackObject.IsNil ); il.EmitBrtrue( endOfDeserialization ); break; } case NilImplication.Null: case NilImplication.Prohibit: { /* * // for Prohibit * if( unpacker.Data.Value.IsNil ) * { * throw SerializationEceptions.NewProhibitNullException( "..." ); * } * * // for Null, and * if( unpacker.Data.Value.IsNil ) * { * throw SerializationEceptions.NewReadOnlyMemberItemsMustNotBeNullMethod( "..." ); * } */ il.EmitAnyLdarg( unpackerArgumentIndex ); il.EmitGetProperty( Metadata._Unpacker.Data ); var data = localHolder.UnpackedData; il.EmitAnyStloc( data ); il.EmitAnyLdloca( data ); il.EmitGetProperty( Metadata._Nullable<MessagePackObject>.Value ); var dataValue = localHolder.UnpackedDataValue; il.EmitAnyStloc( dataValue ); il.EmitAnyLdloca( dataValue ); il.EmitGetProperty( Metadata._MessagePackObject.IsNil ); var endIf0 = il.DefineLabel( "END_IF0" ); il.EmitBrfalse_S( endIf0 ); il.EmitLdstr( member.Name ); if ( nilImplication == NilImplication.Prohibit ) { il.EmitAnyCall( SerializationExceptions.NewNullIsProhibitedMethod ); } else { // Because result member is readonly collection, so the member will not be null if packed value was nil. il.EmitAnyCall( SerializationExceptions.NewReadOnlyMemberItemsMustNotBeNullMethod ); } il.EmitThrow(); il.MarkLabel( endIf0 ); break; } } /* * if( !unpacker.IsArrayHeader && !unpacker.IsMapHeader ) * { * throw new SerializatonException( "Cannot deserialize..." ); * } * * using( var subtreeUnpacker = unpacker.ReadSubtree() ) * { * GET_SERIALIZER.UnpackTo( unpacker, result ) ) * } * * END_OF_DESERIALIZATION: */ var endIf = il.DefineLabel( "THEN" ); var serializerGetter = emitter.RegisterSerializer( memberType ); il.EmitAnyLdarg( unpackerArgumentIndex ); il.EmitGetProperty( Metadata._Unpacker.IsArrayHeader ); il.EmitBrtrue_S( endIf ); il.EmitAnyLdarg( unpackerArgumentIndex ); il.EmitGetProperty( Metadata._Unpacker.IsMapHeader ); il.EmitBrtrue_S( endIf ); // else il.EmitLdstr( member.Name ); il.EmitAnyCall( SerializationExceptions.NewStreamDoesNotContainCollectionForMemberMethod ); il.EmitThrow(); // then var subtreeUnpacker = localHolder.SubtreeUnpacker; il.MarkLabel( endIf ); EmitUnpackerBeginReadSubtree( il, unpackerArgumentIndex, subtreeUnpacker ); serializerGetter( il, 0 ); il.EmitAnyLdloc( subtreeUnpacker ); il.EmitAnyLdloc( target ); Emittion.EmitLoadValue( il, member ); il.EmitAnyCall( typeof( MessagePackSerializer<> ).MakeGenericType( memberType ).GetMethod( "UnpackTo", new[] { typeof( Unpacker ), memberType } ) ); EmitUnpackerEndReadSubtree( il, subtreeUnpacker ); il.MarkLabel( endOfDeserialization ); }
/// <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="value">The value local variable which stores unpacked value.</param> /// <param name="targetType">The type of deserialzing type.</param> /// <param name="member">The metadata for nil implication. Specify <c>null</c> if nil implication is not needed.</param> /// <param name="memberName">The name of the member.</param> /// <param name="endOfDeserialization">The end of deserialization label for nil implication.</param> /// <param name="localHolder">The <see cref="LocalVariableHolder"/> which holds shared local variable information.</param> private static void EmitDeserializeValueCore( SerializerEmitter emitter, TracingILGenerator il, int unpackerArgumentIndex, LocalBuilder value, Type targetType, SerializingMember? member, string memberName, Label endOfDeserialization, LocalVariableHolder localHolder ) { var directUnpacking = Metadata._Unpacker.GetDirectReadMethod( value.LocalType ); if ( directUnpacking != null && ( member == null || !UnpackHelpers.IsReadOnlyAppendableCollectionMember( member.Value.Member ) ) ) { var isSuccess = localHolder.IsDeserializationSucceeded; il.EmitLdc_I4_0(); il.EmitAnyStloc( isSuccess ); il.BeginExceptionBlock(); il.EmitAnyLdarg( unpackerArgumentIndex ); il.EmitAnyLdloca( value ); il.EmitAnyCall( directUnpacking ); il.EmitAnyStloc( isSuccess ); il.BeginCatchBlock( typeof( MessageTypeException ) ); var ex = localHolder.GetCatchedException( typeof( MessageTypeException ) ); il.EmitAnyStloc( ex ); il.EmitTypeOf( targetType ); il.EmitLdstr( memberName ); il.EmitAnyLdloc( ex ); il.EmitAnyCall( SerializationExceptions.NewFailedToDeserializeMemberMethod ); il.EmitThrow(); il.EndExceptionBlock(); var endIf0 = il.DefineLabel( "END_IF" ); il.EmitAnyLdloc( isSuccess ); il.EmitBrtrue_S( endIf0 ); il.EmitAnyCall( SerializationExceptions.NewUnexpectedEndOfStreamMethod ); il.EmitThrow(); il.MarkLabel( endIf0 ); if ( member != null ) { // If null, nil implication is NOT needed. EmitNilImplicationForPrimitive( il, member.Value, value, endOfDeserialization ); } } else { EmitGeneralRead( il, unpackerArgumentIndex ); if ( member != null ) { // If null, nil implication is NOT needed. EmitNilImplication( il, unpackerArgumentIndex, member.Value.Contract.Name, member.Value.Contract.NilImplication, endOfDeserialization, localHolder ); } var thenIffCollection = il.DefineLabel( "THEN_IF_COLLECTION" ); var endIfCollection = il.DefineLabel( "END_IF_COLLECTION" ); /* * if( !unpacker.IsArrayHeader && !unpacker.IsMapHeader ) * { * value = GET_SERIALIZER().UnpackFrom( unpacker ); * } * else * { * using( var subtreeUnpacker = unpacker.ReadSubtree() ) * { * value = GET_SERIALIZER().UnpackFrom( subtreeUnpacker ); * } * } */ il.EmitAnyLdarg( unpackerArgumentIndex ); il.EmitGetProperty( Metadata._Unpacker.IsArrayHeader ); il.EmitAnyLdarg( unpackerArgumentIndex ); il.EmitGetProperty( Metadata._Unpacker.IsMapHeader ); il.EmitOr(); il.EmitBrtrue_S( thenIffCollection ); EmitUnpackFrom( emitter, il, value, unpackerArgumentIndex ); il.EmitBr_S( endIfCollection ); var subtreeUnpacker = localHolder.SubtreeUnpacker; il.MarkLabel( thenIffCollection ); EmitUnpackerBeginReadSubtree( il, unpackerArgumentIndex, subtreeUnpacker ); EmitUnpackFrom( emitter, il, value, subtreeUnpacker ); EmitUnpackerEndReadSubtree( il, subtreeUnpacker ); il.MarkLabel( endIfCollection ); } }
/// <summary> /// Emits the nil implication. /// </summary> /// <param name="il">The il generator.</param> /// <param name="unpackerArgumentIndex">Index of the unpacker argument.</param> /// <param name="memberName">Name of the deserializing member.</param> /// <param name="nilImplication">The nil implication.</param> /// <param name="endOfDeserialization">The label to the end of deserialization.</param> /// <param name="localHolder">The <see cref="LocalVariableHolder"/> which holds shared local variable information.</param> public static void EmitNilImplication( TracingILGenerator il, int unpackerArgumentIndex, string memberName, NilImplication nilImplication, Label endOfDeserialization, LocalVariableHolder localHolder ) { switch ( nilImplication ) { case NilImplication.MemberDefault: { // TODO: This should be empty for extra items. /* * if( unpacker.Data.Value.IsNil ) * { * // Skip current. * goto END_OF_DESERIALIZATION; * } */ il.EmitAnyLdarg( unpackerArgumentIndex ); il.EmitGetProperty( Metadata._Unpacker.Data ); var data = localHolder.UnpackedData; il.EmitAnyStloc( data ); il.EmitAnyLdloca( data ); il.EmitGetProperty( Metadata._Nullable<MessagePackObject>.Value ); var dataValue = localHolder.UnpackedDataValue; il.EmitAnyStloc( dataValue ); il.EmitAnyLdloca( dataValue ); il.EmitGetProperty( Metadata._MessagePackObject.IsNil ); il.EmitBrtrue( endOfDeserialization ); break; } case NilImplication.Prohibit: { /* * if( unpacker.Data.Value.IsNil ) * { * throw SerializationEceptions.NewProhibitNullException( "..." ); * } */ il.EmitAnyLdarg( unpackerArgumentIndex ); il.EmitGetProperty( Metadata._Unpacker.Data ); var data = localHolder.UnpackedData; il.EmitAnyStloc( data ); il.EmitAnyLdloca( data ); il.EmitGetProperty( Metadata._Nullable<MessagePackObject>.Value ); var dataValue = localHolder.UnpackedDataValue; il.EmitAnyStloc( dataValue ); il.EmitAnyLdloca( dataValue ); il.EmitGetProperty( Metadata._MessagePackObject.IsNil ); var endIf0 = il.DefineLabel( "END_IF0" ); il.EmitBrfalse_S( endIf0 ); il.EmitLdstr( memberName ); il.EmitAnyCall( SerializationExceptions.NewNullIsProhibitedMethod ); il.EmitThrow(); il.MarkLabel( endIf0 ); break; } } }
/// <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 ); }
/// <summary> /// Emits deserializing value instructions. /// </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="value">The value local variable which stores unpacked value.</param> /// <param name="targetType">The type of deserialzing type.</param> /// <param name="memberName">The name of the member.</param> /// <param name="localHolder">The <see cref="LocalVariableHolder"/> which holds shared local variable information.</param> public static void EmitDeserializeValueWithoutNilImplication( SerializerEmitter emitter, TracingILGenerator il, int unpackerArgumentIndex, LocalBuilder value, Type targetType, string memberName, LocalVariableHolder localHolder ) { Contract.Requires( emitter != null ); Contract.Requires( il != null ); Contract.Requires( unpackerArgumentIndex >= 0 ); Contract.Requires( value != 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: */ // Nil implication is not needed. EmitDeserializeValueCore( emitter, il, unpackerArgumentIndex, value, targetType, null, memberName, endOfDeserialization, localHolder ); il.MarkLabel( endOfDeserialization ); }
/// <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(); }
/// <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" ) ); }
private static void CreateTuplePack( SerializerEmitter emiter, Type tupleType, IList<Type> itemTypes, Action<TracingILGenerator, LocalBuilder> loadTupleEmitter ) { var il = emiter.GetPackToMethodILGenerator(); var localHolder = new LocalVariableHolder( il ); try { /* * packer.PackArrayHeader( cardinarity ); * _serializer0.PackTo( packer, tuple.Item1 ); * : * _serializer6.PackTo( packer, tuple.item7 ); * _serializer7.PackTo( packer, tuple.Rest.Item1 ); */ var tuple = il.DeclareLocal( tupleType, "tuple" ); loadTupleEmitter( il, tuple ); il.EmitAnyLdarg( 1 ); il.EmitAnyLdc_I4( itemTypes.Count ); il.EmitAnyCall( Metadata._Packer.PackArrayHeader ); il.EmitPop(); var tupleTypeList = TupleItems.CreateTupleTypeList( itemTypes ); int depth = -1; for ( int i = 0; i < itemTypes.Count; i++ ) { if ( i % 7 == 0 ) { depth++; } Emittion.EmitSerializeValue( emiter, il, 1, itemTypes[ i ], null, NilImplication.MemberDefault, il0 => { il0.EmitAnyLdloc( tuple ); for ( int j = 0; j < depth; j++ ) { // .TRest.TRest ... var rest = tupleTypeList[ j ].GetProperty( "Rest" ); il0.EmitGetProperty( rest ); } var itemn = tupleTypeList[ depth ].GetProperty( "Item" + ( ( i % 7 ) + 1 ) ); #if DEBUG Contract.Assert( itemn != null, tupleTypeList[ depth ].GetFullName() + "::Item" + ( ( i % 7 ) + 1 ) + " [ " + depth + " ] @ " + i ); #endif il0.EmitGetProperty( itemn ); }, localHolder ); } il.EmitRet(); } finally { il.FlushTrace(); } }
private static void CreateTupleUnpackFrom( SerializerEmitter emitter, IList<Type> itemTypes ) { var il = emitter.GetUnpackFromMethodILGenerator(); var localHolder = new LocalVariableHolder( il ); try { /* * checked * { * if (!unpacker.IsArrayHeader) * { * throw SerializationExceptions.NewIsNotArrayHeader(); * } * * if ((int)unpacker.ItemsCount != n) * { * throw SerializationExceptions.NewTupleCardinarityIsNotMatch(n, (int)unpacker.ItemsCount); * } * * if (!unpacker.Read()) * { * throw SerializationExceptions.NewMissingItem(0); * } * * DESERIALIZE_VALUE * * : * * return new Tuple<...>( item1, ... , new Tuple<...>(...)...); * } */ il.EmitAnyLdarg( 1 ); il.EmitGetProperty( Metadata._Unpacker.IsArrayHeader ); var endIf = il.DefineLabel( "END_IF" ); il.EmitBrtrue_S( endIf ); il.EmitAnyCall( SerializationExceptions.NewIsNotArrayHeaderMethod ); il.EmitThrow(); il.MarkLabel( endIf ); var itemsCount = localHolder.ItemsCount; Emittion.EmitGetUnpackerItemsCountAsInt32( il, 1, localHolder ); il.EmitAnyLdc_I4( itemTypes.Count ); il.EmitAnyStloc( itemsCount ); il.EmitAnyLdloc( itemsCount ); var endIf1 = il.DefineLabel( "END_IF1" ); il.EmitBeq_S( endIf1 ); il.EmitAnyLdc_I4( itemTypes.Count ); il.EmitAnyLdloc( itemsCount ); il.EmitAnyCall( SerializationExceptions.NewTupleCardinarityIsNotMatchMethod ); il.EmitThrow(); il.MarkLabel( endIf1 ); var itemLocals = new LocalBuilder[ itemTypes.Count ]; var useDummyNullables = new bool[ itemTypes.Count ]; for ( int i = 0; i < itemTypes.Count; i++ ) { if ( itemTypes[ i ] != typeof( MessagePackObject ) && itemTypes[ i ].GetIsValueType() && Nullable.GetUnderlyingType( itemTypes[ i ] ) == null ) { // Use temporary nullable value for nil implication. itemLocals[ i ] = il.DeclareLocal( typeof( Nullable<> ).MakeGenericType( itemTypes[ i ] ), "item" + i.ToString( CultureInfo.InvariantCulture ) ); useDummyNullables[ i ] = true; } else { itemLocals[ i ] = il.DeclareLocal( itemTypes[ i ], "item" + i.ToString( CultureInfo.InvariantCulture ) ); } // Tuple member should be NilImplication.MemberDefault. Emittion.EmitDeserializeValueWithoutNilImplication( emitter, il, 1, itemLocals[ i ], typeof( Tuple ), "Item" + ( i ).ToString( CultureInfo.InvariantCulture ), localHolder ); } for ( int i = 0; i < itemLocals.Length; i++ ) { if ( useDummyNullables[ i ] ) { il.EmitAnyLdloca( itemLocals[ i ] ); il.EmitGetProperty( typeof( Nullable<> ).MakeGenericType( itemTypes[ i ] ).GetProperty( "Value" ) ); } else { il.EmitAnyLdloc( itemLocals[ i ] ); } } var tupleTypeList = TupleItems.CreateTupleTypeList( itemTypes ); for ( int depth = tupleTypeList.Count - 1; 0 <= depth; depth-- ) { il.EmitNewobj( tupleTypeList[ depth ].GetConstructors().Single() ); } il.EmitRet(); } finally { il.FlushTrace(); } }
private static void CreateMapUnpackFrom( SerializationContext context, Type targetType, SerializerEmitter emitter, CollectionTraits traits ) { var il = emitter.GetUnpackFromMethodILGenerator(); var localHolder = new LocalVariableHolder( il ); var instanceType = targetType; try { /* * if (!unpacker.IsMapHeader) * { * throw SerializationExceptions.NewIsNotMapHeader(); * } * * TDictionary<TKey, TValue> dictionary = new ...; * this.UnpackToCore(unpacker, dictionary); * return dictionary; */ if ( targetType.IsInterface || targetType.IsAbstract ) { instanceType = context.DefaultCollectionTypes.GetConcreteType( targetType ); if( instanceType == null ) { il.EmitTypeOf( targetType ); il.EmitAnyCall( SerializationExceptions.NewNotSupportedBecauseCannotInstanciateAbstractTypeMethod ); il.EmitThrow(); return; } } il.EmitAnyLdarg( 1 ); il.EmitGetProperty( Metadata._Unpacker.IsMapHeader ); var endIf = il.DefineLabel( "END_IF" ); il.EmitBrtrue_S( endIf ); il.EmitAnyCall( SerializationExceptions.NewIsNotMapHeaderMethod ); il.EmitThrow(); il.MarkLabel( endIf ); var collection = localHolder.GetDeserializingCollection( instanceType ); Emittion.EmitConstruction( il, collection, il0 => Emittion.EmitGetUnpackerItemsCountAsInt32( il0, 1, localHolder ) ); EmitInvokeMapUnpackToHelper( targetType, emitter, traits, il, 1, il0 => il0.EmitAnyLdloc( collection ) ); il.EmitAnyLdloc( collection ); il.EmitRet(); } finally { il.FlushTrace(); } }
private static void CreatePackArrayProceduresCore( Type targetType, SerializerEmitter emitter, CollectionTraits traits ) { var il = emitter.GetPackToMethodILGenerator(); var localHolder = new LocalVariableHolder( il ); try { // Array if ( targetType.IsArray ) { /* * // array * packer.PackArrayHeader( length ); * for( int i = 0; i < length; i++ ) * { * this._serializer.PackTo( packer, collection[ i ] ); * } */ var length = localHolder.PackingCollectionCount; il.EmitAnyLdarg( 2 ); il.EmitLdlen(); il.EmitAnyStloc( length ); il.EmitAnyLdarg( 1 ); il.EmitAnyLdloc( length ); il.EmitAnyCall( Metadata._Packer.PackArrayHeader ); il.EmitPop(); Emittion.EmitFor( il, length, ( il0, i ) => Emittion.EmitSerializeValue( emitter, il0, 1, traits.ElementType, null, NilImplication.MemberDefault, il1 => { il1.EmitAnyLdarg( 2 ); il1.EmitAnyLdloc( i ); il1.EmitLdelem( traits.ElementType ); }, localHolder ) ); } else if ( traits.CountProperty == null ) { /* * array = collection.ToArray(); * packer.PackArrayHeader( length ); * for( int i = 0; i < length; i++ ) * { * this._serializer.PackTo( packer, array[ i ] ); * } */ var array = localHolder.GetSerializingCollection( traits.ElementType.MakeArrayType() ); EmitLoadTarget( targetType, il, 2 ); il.EmitAnyCall( Metadata._Enumerable.ToArray1Method.MakeGenericMethod( traits.ElementType ) ); il.EmitAnyStloc( array ); var length = localHolder.PackingCollectionCount; il.EmitAnyLdloc( array ); il.EmitLdlen(); il.EmitAnyStloc( length ); il.EmitAnyLdarg( 1 ); il.EmitAnyLdloc( length ); il.EmitAnyCall( Metadata._Packer.PackArrayHeader ); il.EmitPop(); Emittion.EmitFor( il, length, ( il0, i ) => Emittion.EmitSerializeValue( emitter, il0, 1, traits.ElementType, null, NilImplication.MemberDefault, il1 => { il1.EmitAnyLdloc( array ); il1.EmitAnyLdloc( i ); il1.EmitLdelem( traits.ElementType ); }, localHolder ) ); } else { /* * // Enumerable * packer.PackArrayHeader( collection.Count ); * foreach( var item in list ) * { * this._serializer.PackTo( packer, array[ i ] ); * } */ var collection = localHolder.GetSerializingCollection( targetType ); // This instruction always ldarg, not to be ldarga il.EmitAnyLdarg( 2 ); il.EmitAnyStloc( collection ); var count = localHolder.PackingCollectionCount; EmitLoadTarget( targetType, il, 2 ); il.EmitGetProperty( traits.CountProperty ); il.EmitAnyStloc( count ); il.EmitAnyLdarg( 1 ); il.EmitAnyLdloc( count ); il.EmitAnyCall( Metadata._Packer.PackArrayHeader ); il.EmitPop(); Emittion.EmitForEach( il, traits, collection, ( il0, getCurrentEmitter ) => Emittion.EmitSerializeValue( emitter, il0, 1, traits.ElementType, null, NilImplication.MemberDefault, _ => getCurrentEmitter(), localHolder ) ); } il.EmitRet(); } finally { il.FlushTrace(); } }
private static void CreateMapPack( Type targetType, SerializerEmitter emiter, CollectionTraits traits ) { var il = emiter.GetPackToMethodILGenerator(); var localHolder = new LocalVariableHolder( il ); try { /* * int count = ((ICollection<KeyValuePair<string, DateTime>>)dictionary).Count; * packer.PackMapHeader(count); * foreach (KeyValuePair<string, DateTime> current in dictionary) * { * this._serializer0.PackTo(packer, current.Key); * this._serializer1.PackTo(packer, current.Value); * } */ var collection = localHolder.GetSerializingCollection( targetType ); var item = localHolder.GetSerializingCollectionItem( traits.ElementType ); var keyProperty = traits.ElementType.GetProperty( "Key" ); var valueProperty = traits.ElementType.GetProperty( "Value" ); // This instruction is always ldarg, not to be ldarga. il.EmitAnyLdarg( 2 ); il.EmitAnyStloc( collection ); var count = localHolder.PackingCollectionCount; EmitLoadTarget( targetType, il, collection ); il.EmitGetProperty( traits.CountProperty ); il.EmitAnyStloc( count ); il.EmitAnyLdarg( 1 ); il.EmitAnyLdloc( count ); il.EmitAnyCall( Metadata._Packer.PackMapHeader ); il.EmitPop(); Emittion.EmitForEach( il, traits, collection, ( il0, getCurrentEmitter ) => { if ( traits.ElementType.IsGenericType ) { Contract.Assert( traits.ElementType.GetGenericTypeDefinition() == typeof( KeyValuePair<,> ) ); getCurrentEmitter(); il0.EmitAnyStloc( item ); Emittion.EmitSerializeValue( emiter, il0, 1, traits.ElementType.GetGenericArguments()[ 0 ], null, NilImplication.MemberDefault, il1 => { il1.EmitAnyLdloca( item ); il1.EmitGetProperty( keyProperty ); }, localHolder ); Emittion.EmitSerializeValue( emiter, il0, 1, traits.ElementType.GetGenericArguments()[ 1 ], null, NilImplication.MemberDefault, il1 => { il1.EmitAnyLdloca( item ); il1.EmitGetProperty( valueProperty ); }, localHolder ); } else { Contract.Assert( traits.ElementType == typeof( DictionaryEntry ) ); getCurrentEmitter(); il0.EmitAnyStloc( item ); Emittion.EmitSerializeValue( emiter, il0, 1, typeof( MessagePackObject ), null, NilImplication.MemberDefault, il1 => { il0.EmitAnyLdloca( item ); il0.EmitGetProperty( Metadata._DictionaryEntry.Key ); il0.EmitUnbox_Any( typeof( MessagePackObject ) ); }, localHolder ); Emittion.EmitSerializeValue( emiter, il0, 1, typeof( MessagePackObject ), null, NilImplication.MemberDefault, il1 => { il0.EmitAnyLdloca( item ); il0.EmitGetProperty( Metadata._DictionaryEntry.Value ); il0.EmitUnbox_Any( typeof( MessagePackObject ) ); }, localHolder ); } } ); il.EmitRet(); } finally { il.FlushTrace(); } }
private static void CreateArrayUnpackFrom( Type targetType, SerializerEmitter emitter, CollectionTraits traits ) { var il = emitter.GetUnpackFromMethodILGenerator(); var localHolder = new LocalVariableHolder( il ); try { if ( targetType.IsInterface || targetType.IsAbstract ) { il.EmitTypeOf( targetType ); il.EmitAnyCall( SerializationExceptions.NewNotSupportedBecauseCannotInstanciateAbstractTypeMethod ); il.EmitThrow(); return; } /* * if (!unpacker.IsArrayHeader) * { * throw SerializationExceptions.NewIsNotArrayHeader(); * } * * TCollection collection = new ...; * this.UnpackToCore(unpacker, array); * return collection; */ il.EmitAnyLdarg( 1 ); il.EmitGetProperty( Metadata._Unpacker.IsArrayHeader ); var endIf = il.DefineLabel( "END_IF" ); il.EmitBrtrue_S( endIf ); il.EmitAnyCall( SerializationExceptions.NewIsNotArrayHeaderMethod ); il.EmitThrow(); il.MarkLabel( endIf ); var collection = localHolder.GetDeserializingCollection( targetType ); // Emit newobj, newarr, or call ValueType..ctor() Emittion.EmitConstruction( il, collection, il0 => Emittion.EmitGetUnpackerItemsCountAsInt32( il0, 1, localHolder ) ); EmitInvokeArrayUnpackToHelper( targetType, emitter, traits, il, 1, il0 => il0.EmitAnyLdloc( collection ) ); il.EmitAnyLdloc( collection ); il.EmitRet(); } finally { il.FlushTrace(); } }
/// <summary> /// Emits the nil implication. /// </summary> /// <param name="il">The il generator.</param> /// <param name="unpackerArgumentIndex">Index of the unpacker argument.</param> /// <param name="member">Metadata of the serializing member.</param> /// <param name="endOfDeserialization">The label to the end of deserialization.</param> /// <param name="localHolder">The <see cref="LocalVariableHolder"/> which holds shared local variable information.</param> public static void EmitNilImplication( TracingILGenerator il, int unpackerArgumentIndex, SerializingMember member, Label endOfDeserialization, LocalVariableHolder localHolder ) { switch ( member.Contract.NilImplication ) { case NilImplication.MemberDefault: { // TODO: This should be empty for extra items. /* * if( unpacker.Data.Value.IsNil ) * { * // Skip current. * goto END_OF_DESERIALIZATION; * } */ il.EmitAnyLdarg( unpackerArgumentIndex ); il.EmitGetProperty( Metadata._Unpacker.LastReadData ); var data = localHolder.UnpackedData; il.EmitAnyStloc( data ); il.EmitAnyLdloca( data ); il.EmitGetProperty( Metadata._MessagePackObject.IsNil ); il.EmitBrtrue( endOfDeserialization ); break; } case NilImplication.Null: { if ( member.Member.GetMemberValueType().GetIsValueType() && Nullable.GetUnderlyingType( member.Member.GetMemberValueType() ) == null ) { throw SerializationExceptions.NewValueTypeCannotBeNull( member.Contract.Name, member.Member.GetMemberValueType(), member.Member.DeclaringType ); } if ( !member.Member.CanSetValue() ) { throw SerializationExceptions.NewReadOnlyMemberItemsMustNotBeNull( member.Contract.Name ); } break; } case NilImplication.Prohibit: { if ( !member.Member.CanSetValue() ) { throw SerializationExceptions.NewReadOnlyMemberItemsMustNotBeNull( member.Contract.Name ); } /* * if( unpacker.Data.Value.IsNil ) * { * throw SerializationEceptions.NewProhibitNullException( "..." ); * } */ il.EmitAnyLdarg( unpackerArgumentIndex ); il.EmitGetProperty( Metadata._Unpacker.LastReadData ); var data = localHolder.UnpackedData; il.EmitAnyStloc( data ); il.EmitAnyLdloca( data ); il.EmitGetProperty( Metadata._MessagePackObject.IsNil ); var endIf0 = il.DefineLabel( "END_IF0" ); il.EmitBrfalse_S( endIf0 ); il.EmitLdstr( member.Contract.Name ); il.EmitAnyCall( SerializationExceptions.NewNullIsProhibitedMethod ); il.EmitThrow(); il.MarkLabel( endIf0 ); break; } } }
private static void EmitUnpackMembersFromArray(SerializerEmitter emitter, TracingILGenerator unpackerIL, SerializingMember[] entries, LocalBuilder result, LocalVariableHolder localHolder) { /* * 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.EmitBlt(else0); // Tail missing member handling. if (entries[i].Member != null) { // Respect nil implication. Emittion.EmitNilImplication(unpackerIL, 1, entries[i].Contract.Name, entries[i].Contract.NilImplication, endIf0, localHolder); } unpackerIL.EmitBr(endIf0); unpackerIL.MarkLabel(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.MarkLabel(endIf0); } }
private static void CreateMapPack(Type targetType, SerializerEmitter emiter, CollectionTraits traits) { TracingILGenerator packToMethodILGenerator = emiter.GetPackToMethodILGenerator(); LocalVariableHolder localHolder = new LocalVariableHolder(packToMethodILGenerator); try { < > c__DisplayClass20 class3; LocalBuilder serializingCollection = localHolder.GetSerializingCollection(targetType); LocalBuilder item = localHolder.GetSerializingCollectionItem(traits.ElementType); PropertyInfo keyProperty = traits.ElementType.GetProperty("Key"); PropertyInfo valueProperty = traits.ElementType.GetProperty("Value"); packToMethodILGenerator.EmitAnyLdarg(2); packToMethodILGenerator.EmitAnyStloc(serializingCollection); LocalBuilder packingCollectionCount = localHolder.PackingCollectionCount; EmitLoadTarget(targetType, packToMethodILGenerator, serializingCollection); packToMethodILGenerator.EmitGetProperty(traits.CountProperty); packToMethodILGenerator.EmitAnyStloc(packingCollectionCount); packToMethodILGenerator.EmitAnyLdarg(1); packToMethodILGenerator.EmitAnyLdloc(packingCollectionCount); packToMethodILGenerator.EmitAnyCall(_Packer.PackMapHeader); packToMethodILGenerator.EmitPop(); Emittion.EmitForEach(packToMethodILGenerator, traits, serializingCollection, delegate(TracingILGenerator il0, Action getCurrentEmitter) { Action <TracingILGenerator> loadValueEmitter = null; Action <TracingILGenerator> action2 = null; Action <TracingILGenerator> action3 = null; Action <TracingILGenerator> action4 = null; < > c__DisplayClass20 class1 = class3; if (traits.ElementType.IsGenericType) { Contract.Assert(traits.ElementType.GetGenericTypeDefinition() == typeof(KeyValuePair <,>)); getCurrentEmitter(); il0.EmitAnyStloc(item); if (loadValueEmitter == null) { loadValueEmitter = delegate(TracingILGenerator il1) { il1.EmitAnyLdloca(item); il1.EmitGetProperty(keyProperty); }; } Emittion.EmitSerializeValue(emiter, il0, 1, traits.ElementType.GetGenericArguments()[0], null, NilImplication.MemberDefault, loadValueEmitter, localHolder); if (action2 == null) { action2 = delegate(TracingILGenerator il1) { il1.EmitAnyLdloca(item); il1.EmitGetProperty(valueProperty); }; } Emittion.EmitSerializeValue(emiter, il0, 1, traits.ElementType.GetGenericArguments()[1], null, NilImplication.MemberDefault, action2, localHolder); } else { Contract.Assert(traits.ElementType == typeof(DictionaryEntry)); getCurrentEmitter(); il0.EmitAnyStloc(item); if (action3 == null) { action3 = delegate(TracingILGenerator il1) { il0.EmitAnyLdloca(item); il0.EmitGetProperty(_DictionaryEntry.Key); il0.EmitUnbox_Any(typeof(MessagePackObject)); }; } Emittion.EmitSerializeValue(emiter, il0, 1, typeof(MessagePackObject), null, NilImplication.MemberDefault, action3, localHolder); if (action4 == null) { action4 = delegate(TracingILGenerator il1) { il0.EmitAnyLdloca(item); il0.EmitGetProperty(_DictionaryEntry.Value); il0.EmitUnbox_Any(typeof(MessagePackObject)); }; } Emittion.EmitSerializeValue(emiter, il0, 1, typeof(MessagePackObject), null, NilImplication.MemberDefault, action4, localHolder); } });