private static void EmitInvokeMapUnpackToHelper(Type targetType, SerializerEmitter emitter, CollectionTraits traits, TracingILGenerator il, int unpackerArgumentIndex, Action <TracingILGenerator> loadCollectionEmitting) { il.EmitAnyLdarg(unpackerArgumentIndex); if (traits.ElementType.IsGenericType) { var keyType = traits.ElementType.GetGenericArguments()[0]; var valueType = traits.ElementType.GetGenericArguments()[1]; var keySerializerGetting = emitter.RegisterSerializer(keyType); var valueSerializerGetting = emitter.RegisterSerializer(valueType); keySerializerGetting(il, 0); valueSerializerGetting(il, 0); loadCollectionEmitting(il); if (targetType.IsValueType) { il.EmitBox(targetType); } il.EmitAnyCall(Metadata._UnpackHelpers.UnpackMapTo_2.MakeGenericMethod(keyType, valueType)); } else { loadCollectionEmitting(il); if (targetType.IsValueType) { il.EmitBox(targetType); } il.EmitAnyCall(Metadata._UnpackHelpers.UnpackNonGenericMapTo); } }
private static void EmitNewDelegate(TracingILGenerator il, Type targetType, MethodInfo method, Action <TracingILGenerator> loadTargetEmitting, Type delegateType) { loadTargetEmitting(il); if (targetType.IsValueType) { il.EmitBox(targetType); } if (method.IsStatic || method.IsFinal || !method.IsVirtual) { il.EmitLdftn(method); } else { il.EmitDup(); il.EmitLdvirtftn(method); } il.EmitNewobj(delegateType.GetConstructor(_delegateConstructorParameterTypes)); }
public static void EmitForEach(TracingILGenerator il, CollectionTraits traits, LocalBuilder collection, Action <TracingILGenerator, Action> bodyEmitter) { Contract.Requires(il != null); Contract.Requires(collection != null); Contract.Requires(bodyEmitter != null); LocalBuilder enumerator = il.DeclareLocal(traits.GetEnumeratorMethod.ReturnType, "enumerator"); if (collection.LocalType.IsValueType) { il.EmitAnyLdloca(collection); } else { il.EmitAnyLdloc(collection); } il.EmitAnyCall(traits.GetEnumeratorMethod); il.EmitAnyStloc(enumerator); if (typeof(IDisposable).IsAssignableFrom(traits.GetEnumeratorMethod.ReturnType)) { il.BeginExceptionBlock(); } Label label = il.DefineLabel("START_LOOP"); il.MarkLabel(label); Label target = il.DefineLabel("END_LOOP"); Type returnType = traits.GetEnumeratorMethod.ReturnType; MethodInfo method = returnType.GetMethod("MoveNext", Type.EmptyTypes); PropertyInfo currentProperty = traits.GetEnumeratorMethod.ReturnType.GetProperty("Current"); if (method == null) { method = _IEnumerator.MoveNext; } if (currentProperty == null) { if (returnType == typeof(IDictionaryEnumerator)) { currentProperty = _IDictionaryEnumerator.Current; } else if (returnType.IsInterface) { if (returnType.IsGenericType && (returnType.GetGenericTypeDefinition() == typeof(IEnumerator <>))) { currentProperty = typeof(IEnumerator <>).MakeGenericType(new Type[] { traits.ElementType }).GetProperty("Current"); } else { currentProperty = _IEnumerator.Current; } } } Contract.Assert(currentProperty != null, returnType.ToString()); if (traits.GetEnumeratorMethod.ReturnType.IsValueType) { il.EmitAnyLdloca(enumerator); } else { il.EmitAnyLdloc(enumerator); } il.EmitAnyCall(method); il.EmitBrfalse(target); bodyEmitter(il, delegate { if (traits.GetEnumeratorMethod.ReturnType.IsValueType) { il.EmitAnyLdloca(enumerator); } else { il.EmitAnyLdloc(enumerator); } il.EmitGetProperty(currentProperty); }); il.EmitBr(label); il.MarkLabel(target); if (typeof(IDisposable).IsAssignableFrom(traits.GetEnumeratorMethod.ReturnType)) { il.BeginFinallyBlock(); if (traits.GetEnumeratorMethod.ReturnType.IsValueType) { MethodInfo info2 = traits.GetEnumeratorMethod.ReturnType.GetMethod("Dispose"); if (((info2 != null) && (info2.GetParameters().Length == 0)) && (info2.ReturnType == typeof(void))) { il.EmitAnyLdloca(enumerator); il.EmitAnyCall(info2); } else { il.EmitAnyLdloc(enumerator); il.EmitBox(traits.GetEnumeratorMethod.ReturnType); il.EmitAnyCall(_IDisposable.Dispose); } } else { il.EmitAnyLdloc(enumerator); il.EmitAnyCall(_IDisposable.Dispose); } il.EndExceptionBlock(); } }
/// <summary> /// Emits 'foreach' statement on the IL stream. /// </summary> /// <param name="il">IL generator to be emitted to.</param> /// <param name="traits"><see cref="CollectionTraits"/> which contains traits of the iterating collection.</param> /// <param name="collection">'collection' argument index.</param> /// <param name="bodyEmitter">Delegate to emit body statement.</param> public static void EmitForEach(TracingILGenerator il, CollectionTraits traits, LocalBuilder collection, Action <TracingILGenerator, Action> bodyEmitter) { Contract.Requires(il != null); Contract.Requires(collection != null); Contract.Requires(bodyEmitter != null); var enumerator = il.DeclareLocal(traits.GetEnumeratorMethod.ReturnType, "enumerator"); // gets enumerator if (collection.LocalType.IsValueType) { il.EmitAnyLdloca(collection); } else { il.EmitAnyLdloc(collection); } il.EmitAnyCall(traits.GetEnumeratorMethod); il.EmitAnyStloc(enumerator); if (typeof(IDisposable).IsAssignableFrom(traits.GetEnumeratorMethod.ReturnType)) { il.BeginExceptionBlock(); } var startLoop = il.DefineLabel("START_LOOP"); il.MarkLabel(startLoop); var endLoop = il.DefineLabel("END_LOOP"); var enumeratorType = traits.GetEnumeratorMethod.ReturnType; MethodInfo moveNextMethod = enumeratorType.GetMethod("MoveNext", Type.EmptyTypes); PropertyInfo currentProperty = traits.GetEnumeratorMethod.ReturnType.GetProperty("Current"); if (moveNextMethod == null) { moveNextMethod = Metadata._IEnumerator.MoveNext; } if (currentProperty == null) { if (enumeratorType == typeof(IDictionaryEnumerator)) { currentProperty = Metadata._IDictionaryEnumerator.Current; } else if (enumeratorType.IsInterface) { if (enumeratorType.IsGenericType && enumeratorType.GetGenericTypeDefinition() == typeof(IEnumerator <>)) { currentProperty = typeof(IEnumerator <>).MakeGenericType(traits.ElementType).GetProperty("Current"); } else { currentProperty = Metadata._IEnumerator.Current; } } } Contract.Assert(currentProperty != null, enumeratorType.ToString()); // iterates if (traits.GetEnumeratorMethod.ReturnType.IsValueType) { il.EmitAnyLdloca(enumerator); } else { il.EmitAnyLdloc(enumerator); } il.EmitAnyCall(moveNextMethod); il.EmitBrfalse(endLoop); bodyEmitter( il, () => { if (traits.GetEnumeratorMethod.ReturnType.IsValueType) { il.EmitAnyLdloca(enumerator); } else { il.EmitAnyLdloc(enumerator); } il.EmitGetProperty(currentProperty); } ); il.EmitBr(startLoop); il.MarkLabel(endLoop); // Dispose if (typeof(IDisposable).IsAssignableFrom(traits.GetEnumeratorMethod.ReturnType)) { il.BeginFinallyBlock(); if (traits.GetEnumeratorMethod.ReturnType.IsValueType) { var disposeMethod = traits.GetEnumeratorMethod.ReturnType.GetMethod("Dispose"); if (disposeMethod != null && disposeMethod.GetParameters().Length == 0 && disposeMethod.ReturnType == typeof(void)) { il.EmitAnyLdloca(enumerator); il.EmitAnyCall(disposeMethod); } else { il.EmitAnyLdloc(enumerator); il.EmitBox(traits.GetEnumeratorMethod.ReturnType); il.EmitAnyCall(Metadata._IDisposable.Dispose); } } else { il.EmitAnyLdloc(enumerator); il.EmitAnyCall(Metadata._IDisposable.Dispose); } il.EndExceptionBlock(); } }
private static void EmitInvokeArrayUnpackToHelper(Type targetType, SerializerEmitter emitter, CollectionTraits traits, TracingILGenerator il, int unpackerArgumentIndex, Action <TracingILGenerator> loadCollectionEmitting) { il.EmitAnyLdarg(unpackerArgumentIndex); var serializerGetting = emitter.RegisterSerializer(traits.ElementType); if (targetType.IsArray) { // Array /* * UnpackHelpers.UnpackArrayTo( unpacker, GET_SERIALIZER, collection ); */ serializerGetting(il, 0); loadCollectionEmitting(il); il.EmitAnyCall(Metadata._UnpackHelpers.UnpackArrayTo_1.MakeGenericMethod(traits.ElementType)); } else if (targetType.IsGenericType) { serializerGetting(il, 0); loadCollectionEmitting(il); if (targetType.IsValueType) { il.EmitBox(targetType); } if (traits.AddMethod.ReturnType == null || traits.AddMethod.ReturnType == typeof(void)) { // with void Add( T item ) /* * Action<T> addition = TCollection.Add * UnpackHelpers.UnpackCollectionTo( unpacker, GET_SERIALIZER, collection, addition ); */ var itemType = traits.AddMethod.GetParameters()[0].ParameterType; EmitNewDelegate(il, targetType, traits.AddMethod, loadCollectionEmitting, typeof(Action <>).MakeGenericType(itemType)); il.EmitAnyCall(Metadata._UnpackHelpers.UnpackCollectionTo_1.MakeGenericMethod(itemType)); } else { // with TDiscarded Add( T item ) /* * Func<T, TDiscarded> addition = TCollection.Add * UnpackHelpers.UnpackCollectionTo( unpacker, GET_SERIALIZER, collection, addition ); */ var itemType = traits.AddMethod.GetParameters()[0].ParameterType; var discardingType = traits.AddMethod.ReturnType; EmitNewDelegate(il, targetType, traits.AddMethod, loadCollectionEmitting, typeof(Func <,>).MakeGenericType(itemType, discardingType)); il.EmitAnyCall(Metadata._UnpackHelpers.UnpackCollectionTo_2.MakeGenericMethod(itemType, discardingType)); } } else { loadCollectionEmitting(il); if (targetType.IsValueType) { il.EmitBox(targetType); } if (traits.AddMethod.ReturnType == null || traits.AddMethod.ReturnType == typeof(void)) { // with void Add( object item ) /* * Action<object> addition = TCollection.Add * UnpackHelpers.UnpackCollectionTo( unpacker, collection, addition ); */ EmitNewDelegate(il, targetType, traits.AddMethod, loadCollectionEmitting, typeof(Action <object>)); il.EmitAnyCall(Metadata._UnpackHelpers.UnpackNonGenericCollectionTo); } else { // with TDiscarded Add( object item ) /* * Func<TDiscarded> addition = TCollection.Add * UnpackHelpers.UnpackCollectionTo( unpacker, collection, addition ); */ var discardingType = traits.AddMethod.ReturnType; EmitNewDelegate(il, targetType, traits.AddMethod, loadCollectionEmitting, typeof(Func <,>).MakeGenericType(typeof(object), discardingType)); il.EmitAnyCall(Metadata._UnpackHelpers.UnpackNonGenericCollectionTo_1.MakeGenericMethod(discardingType)); } } }
public override Func <SerializationContext, PolymorphismSchema, MessagePackSerializer <T> > CreateConstructor <T>() { if (!this._typeBuilder.IsCreated()) { /* * .ctor() : this(null) * {} */ /* * .ctor( SerializationContext context ) * : base( ( context ?? SerializationContext.Default ).CompabilityOptions.PackerCompatibilityOptions ) * { * this._serializer0 = context.GetSerializer<T0>(); * this._serializer1 = context.GetSerializer<T1>(); * this._serializer2 = context.GetSerializer<T2>(); * : * } */ // default { var il = this._defaultConstructorBuilder.GetILGenerator(); // : this(null) il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldnull); il.Emit(OpCodes.Call, this._contextConstructorBuilder); il.Emit(OpCodes.Ret); } // context { var il = new TracingILGenerator(this._contextConstructorBuilder, TextWriter.Null, this._isDebuggable); // : base() il.EmitLdarg_0(); il.EmitLdarg_1(); #if DEBUG Contract.Assert(this._typeBuilder.BaseType != null, "this._typeBuilder.BaseType != null"); #endif if (this._traits.CollectionType == CollectionKind.NotCollection) { il.EmitCallConstructor( this._typeBuilder.BaseType.GetConstructor( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, ConstructorParameterTypes, null ) ); } else { il.EmitCall(this._restoreSchemaMethodBuilder); il.EmitCallConstructor( this._typeBuilder.BaseType.GetConstructor( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, CollectionConstructorParameterTypes, null ) ); } // this._serializerN = context.GetSerializer<T>(); foreach (var entry in this._serializers) { var targetType = Type.GetTypeFromHandle(entry.Key.TypeHandle); MethodInfo getMethod = Metadata._SerializationContext.GetSerializer1_Parameter_Method.MakeGenericMethod(targetType); il.EmitLdarg_0(); il.EmitLdarg_1(); if (targetType.GetIsEnum()) { il.EmitLdarg_1(); il.EmitTypeOf(targetType); il.EmitAnyLdc_I4(( int )entry.Key.EnumSerializationMethod); il.EmitCall(Metadata._EnumMessagePackSerializerHelpers.DetermineEnumSerializationMethodMethod); il.EmitBox(typeof(EnumSerializationMethod)); } else if (DateTimeMessagePackSerializerHelpers.IsDateTime(targetType)) { il.EmitLdarg_1(); il.EmitAnyLdc_I4(( int )entry.Key.DateTimeConversionMethod); il.EmitCall(Metadata._DateTimeMessagePackSerializerHelpers.DetermineDateTimeConversionMethodMethod); il.EmitBox(typeof(DateTimeConversionMethod)); } else { if (entry.Key.PolymorphismSchema == null) { il.EmitLdnull(); } else { entry.Value.SchemaProvider(il); } } il.EmitCallvirt(getMethod); il.EmitStfld(entry.Value.Field); } foreach (var entry in this._fieldInfos) { il.EmitLdarg_0(); il.EmitLdtoken(FieldInfo.GetFieldFromHandle(entry.Key)); il.EmitCall(Metadata._FieldInfo.GetFieldFromHandle); il.EmitStfld(entry.Value); } foreach (var entry in this._methodBases) { il.EmitLdarg_0(); il.EmitLdtoken(MethodBase.GetMethodFromHandle(entry.Key)); il.EmitCall(Metadata._MethodBase.GetMethodFromHandle); il.EmitStfld(entry.Value); } il.EmitRet(); } } var ctor = this._typeBuilder.CreateType().GetConstructor(ConstructorParameterTypes); var contextParameter = Expression.Parameter(typeof(SerializationContext), "context"); var schemaParameter = Expression.Parameter(typeof(PolymorphismSchema), "schema"); #if DEBUG Contract.Assert(ctor != null, "ctor != null"); #endif return (Expression.Lambda <Func <SerializationContext, PolymorphismSchema, MessagePackSerializer <T> > >( Expression.New( ctor, contextParameter ), contextParameter, schemaParameter ).Compile()); }
private void CreateContextfulObjectConstructor( AssemblyBuilderEmittingContext context, Type baseType, TracingILGenerator il, Func <ILConstruct> packActionListInitializerProvider, Func <ILConstruct> packActionTableInitializerProvider, Func <ILConstruct> unpackActionListInitializerProvider, Func <ILConstruct> unpackActionTableInitializerProvider, #if FEATURE_TAP Func <ILConstruct> packAsyncActionListInitializerProvider, Func <ILConstruct> packAsyncActionTableInitializerProvider, Func <ILConstruct> unpackAsyncActionListInitializerProvider, Func <ILConstruct> unpackAsyncActionTableInitializerProvider, #endif // FEATURE_TAP Func <ILConstruct> memberNamesInitializerProvider, Func <ILConstruct> unpackToInitializerProvider ) { /* * .ctor( SerializationContext context ) * : base( ( context ?? SerializationContext.Default ).CompabilityOptions.PackerCompatibilityOptions ) * { * this._serializer0 = context.GetSerializer<T0>(); * this._serializer1 = context.GetSerializer<T1>(); * this._serializer2 = context.GetSerializer<T2>(); * : * } */ // : base() il.EmitLdarg_0(); il.EmitLdarg_1(); if (this._specification.TargetCollectionTraits.CollectionType == CollectionKind.NotCollection) { il.EmitCallConstructor( baseType.GetRuntimeConstructor(ConstructorParameterTypes) ); } else { il.EmitCall(this._methodTable[MethodName.RestoreSchema]); il.EmitCallConstructor( baseType.GetRuntimeConstructor(CollectionConstructorParameterTypes) ); } // this._serializerN = context.GetSerializer<T>(); foreach (var entry in this._serializers) { var targetType = Type.GetTypeFromHandle(entry.Key.TypeHandle); MethodInfo getMethod = Metadata._SerializationContext.GetSerializer1_Parameter_Method.MakeGenericMethod(targetType); il.EmitLdarg_0(); il.EmitLdarg_1(); if (targetType.GetIsEnum()) { il.EmitLdarg_1(); il.EmitTypeOf(targetType); il.EmitAnyLdc_I4(( int )entry.Key.EnumSerializationMethod); il.EmitCall(Metadata._EnumMessagePackSerializerHelpers.DetermineEnumSerializationMethodMethod); il.EmitBox(typeof(EnumSerializationMethod)); } else if (DateTimeMessagePackSerializerHelpers.IsDateTime(targetType)) { il.EmitLdarg_1(); il.EmitAnyLdc_I4(( int )entry.Key.DateTimeConversionMethod); il.EmitCall(Metadata._DateTimeMessagePackSerializerHelpers.DetermineDateTimeConversionMethodMethod); il.EmitBox(typeof(DateTimeConversionMethod)); } else { if (entry.Key.PolymorphismSchema == null) { il.EmitLdnull(); } else { entry.Value.SchemaProvider(il); } } il.EmitCallvirt(getMethod); il.EmitStfld(entry.Value.Field); } foreach (var entry in this._cachedFieldInfos) { il.EmitLdarg_0(); il.EmitLdtoken(entry.Value.Target); il.EmitLdtoken(entry.Value.Target.DeclaringType); il.EmitCall(Metadata._FieldInfo.GetFieldFromHandle); il.EmitStfld(entry.Value.StorageFieldBuilder); } foreach (var entry in this._cachedMethodBases) { il.EmitLdarg_0(); il.EmitLdtoken(entry.Value.Target); il.EmitLdtoken(entry.Value.Target.DeclaringType); il.EmitCall(Metadata._MethodBase.GetMethodFromHandle); il.EmitStfld(entry.Value.StorageFieldBuilder); } if (packActionListInitializerProvider != null) { packActionListInitializerProvider().Evaluate(il); } #if FEATURE_TAP if (packAsyncActionListInitializerProvider != null) { packAsyncActionListInitializerProvider().Evaluate(il); } #endif // FEATURE_TAP if (packActionTableInitializerProvider != null) { packActionTableInitializerProvider().Evaluate(il); } #if FEATURE_TAP if (packAsyncActionTableInitializerProvider != null) { packAsyncActionTableInitializerProvider().Evaluate(il); } #endif // FEATURE_TAP if (unpackActionListInitializerProvider != null) { unpackActionListInitializerProvider().Evaluate(il); } #if FEATURE_TAP if (unpackAsyncActionListInitializerProvider != null) { unpackAsyncActionListInitializerProvider().Evaluate(il); } #endif // FEATURE_TAP if (unpackActionTableInitializerProvider != null) { unpackActionTableInitializerProvider().Evaluate(il); } #if FEATURE_TAP if (unpackAsyncActionTableInitializerProvider != null) { unpackAsyncActionTableInitializerProvider().Evaluate(il); } #endif // FEATURE_TAP if (memberNamesInitializerProvider != null) { memberNamesInitializerProvider().Evaluate(il); } if (unpackToInitializerProvider != null) { unpackToInitializerProvider().Evaluate(il); } foreach (var cachedDelegateInfo in context.GetCachedDelegateInfos()) { // this for stfld il.EmitLdargThis(); var delegateType = cachedDelegateInfo.BackingField.FieldType.ResolveRuntimeType(); // Declare backing field now. var field = context.GetDeclaredField(cachedDelegateInfo.BackingField.FieldName).ResolveRuntimeField(); if (cachedDelegateInfo.IsThisInstance) { il.EmitLdargThis(); } else { il.EmitLdnull(); } // OK this should not be ldvirtftn because target is private or static. il.EmitLdftn(cachedDelegateInfo.TargetMethod.ResolveRuntimeMethod()); // call extern .ctor(Object, void*) il.EmitNewobj(delegateType.GetConstructors().Single()); il.EmitStfld(field); } il.EmitRet(); }