Example #1
0
 public virtual void PopIfReturnValue(MethodInstance method)
 {
     if (mv != null)
     {
         mv.PopIfReturnValue(method);
     }
 }
Example #2
0
        protected PropertyInstance ImplementNotifyPropertyChangedSource(PropertyInstance p_propertyChangeTemplate,
                                                                        FieldInstance f_propertyChangeSupport)
        {
            MethodInstance m_onPropertyChanged_Values = MethodInstance.FindByTemplate(template_m_onPropertyChanged_Values, true);

            if (m_onPropertyChanged_Values == null)
            {
                IMethodVisitor mv = VisitMethod(template_m_onPropertyChanged_Values);
                mv.CallThisGetter(p_propertyChangeTemplate);
                mv.LoadThis();
                mv.GetThisField(f_propertyChangeSupport);

                // getMethodHandle(sender, propertyName)
                mv.CallThisGetter(p_propertyChangeTemplate);
                mv.LoadThis();
                mv.LoadArg(0);
                mv.InvokeVirtual(m_getMethodHandle);

                mv.LoadArg(1);
                mv.LoadArg(2);
                // firePropertyChange(sender, propertyChangeSupport, property, oldValue, newValue)
                mv.InvokeVirtual(m_firePropertyChange);
                mv.PopIfReturnValue(m_firePropertyChange);
                mv.ReturnVoidOrThis();
                mv.EndMethod();
                m_onPropertyChanged_Values = mv.Method;
            }
            MethodInstance m_onPropertyChanged = MethodInstance.FindByTemplate(template_m_onPropertyChanged, true);

            if (m_onPropertyChanged == null)
            {
                IMethodVisitor mv = VisitMethod(template_m_onPropertyChanged);
                mv.LoadThis();
                mv.LoadArg(0);
                mv.PushNull();
                mv.PushNull();
                mv.InvokeVirtual(m_onPropertyChanged_Values);
                mv.PopIfReturnValue(m_onPropertyChanged_Values);
                mv.ReturnVoidOrThis();
                mv.EndMethod();
                m_onPropertyChanged = mv.Method;
            }
            PropertyInstance p_pceHandlers = PropertyInstance.FindByTemplate(p_propertyChangeSupport, true);

            if (p_pceHandlers == null)
            {
                HideFromDebug(ImplementGetter(p_propertyChangeSupport.Getter, f_propertyChangeSupport));
                p_pceHandlers = PropertyInstance.FindByTemplate(p_propertyChangeSupport, false);
            }
            if (EmbeddedEnhancementHint.HasMemberPath(State.Context))
            {
                PropertyInstance p_parentEntity = EmbeddedTypeVisitor.GetParentObjectProperty(this);
                if (MethodInstance.FindByTemplate(p_parentChildEventHandler.Getter, true) == null)
                {
                    IMethodVisitor mv = VisitMethod(p_parentChildEventHandler.Getter);
                    mv.CallThisGetter(p_parentEntity);
                    mv.InvokeInterface(p_parentChildEventHandler.Getter);
                    mv.ReturnValue();
                    mv.EndMethod();
                    HideFromDebug(mv.Method);
                }
                if (MethodInstance.FindByTemplate(p_collectionEventHandler.Getter, true) == null)
                {
                    IMethodVisitor mv = VisitMethod(p_collectionEventHandler.Getter);
                    mv.CallThisGetter(p_parentEntity);
                    mv.InvokeInterface(p_collectionEventHandler.Getter);
                    mv.ReturnValue();
                    mv.EndMethod();
                    HideFromDebug(mv.Method);
                }
            }
            else
            {
                if (MethodInstance.FindByTemplate(p_parentChildEventHandler.Getter, true) == null)
                {
                    HideFromDebug(ImplementLazyInitProperty(p_parentChildEventHandler, delegate(IMethodVisitor mv)
                    {
                        MethodInstance method = new MethodInstance(null, typeof(NotifyPropertyChangedClassVisitor), typeof(PropertyChangedEventHandler), "CreateParentChildEventHandler", typeof(Object));
                        mv.LoadThis();
                        mv.InvokeStatic(method);
                    }));
                }
                if (MethodInstance.FindByTemplate(p_collectionEventHandler.Getter, true) == null)
                {
                    HideFromDebug(ImplementLazyInitProperty(p_collectionEventHandler, delegate(IMethodVisitor mv)
                    {
                        MethodInstance method = new MethodInstance(null, typeof(NotifyPropertyChangedClassVisitor), typeof(NotifyCollectionChangedEventHandler), "CreateCollectionEventHandler", typeof(Object));
                        mv.LoadThis();
                        mv.InvokeStatic(method);
                    }));
                }
            }

            //MethodAttributes ma = MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig;
            //{
            //    ConstructorInfo pceaCI = typeof(PropertyChangedEventArgs).GetConstructor(new Type[] { typeof(String) });

            //    MethodBuilder mb = VisitorUtil.DefineMethod(vs, onPropertyChangedMI_string, ma);
            //    ILGenerator gen = mb.GetILGenerator();
            //    gen.Emit(OpCodes.Ldarg_0);
            //    gen.Emit(OpCodes.Ldarg_1);
            //    gen.Emit(OpCodes.Newobj, pceaCI);
            //    gen.Emit(OpCodes.Call, onPropertyChangedMI_pceArg);
            //    gen.Emit(OpCodes.Ret);
            //}
            //{
            //    MethodBuilder mb = VisitorUtil.DefineMethod(vs, onPropertyChangedMI_pceArg, ma);
            //    ILGenerator gen = mb.GetILGenerator();
            //    gen.Emit(OpCodes.Ldarg_0);
            //    gen.Emit(OpCodes.Call, pctPI.GetGetMethod());
            //    gen.Emit(OpCodes.Ldarg_0);
            //    gen.Emit(OpCodes.Ldarg_1);
            //    gen.Emit(OpCodes.Call, FirePropertyChangedMI);
            //    gen.Emit(OpCodes.Ret);
            //}
            //    List<PropertyChangedEventHandler> PropertyChangedEventHandlers { get; }

            //void OnPropertyChanged(String propertyName);

            //void OnPropertyChanged(PropertyChangedEventArgs args);
            return(p_pceHandlers);
        }
Example #3
0
        protected void ImplementPropertyChangeOnProperty(PropertyInstance propertyInfo,
                                                         PropertyInstance p_propertyChangeTemplate, MethodInstance m_firePropertyChange, FieldInstance f_propertyChangeSupport)
        {
            // add property change detection and notification
            if (propertyInfo.Getter == null || propertyInfo.Setter == null)
            {
                return;
            }
            if (InitializeEmbeddedMemberVisitor.IsEmbeddedMember(metaData, propertyInfo.Name))
            {
                return;
            }
            PropertyInstance p_getterMethodHandle = ImplementAssignedReadonlyProperty(propertyInfo.Name + "$MethodHandle",
                                                                                      new MethodHandleValueResolver(PropertyInfoProvider, propertyInfo.Name));
            Type           propertyType         = propertyInfo.PropertyType.Type;
            MethodInstance m_hasPropertyChanged = GetApplicableHasPropertyChangedOverload(propertyType);

            // check value type of last parameter
            bool isBoxingNeededForHasPropertyChanged = IsBoxingNeededForHasPropertyChangedOverload(m_hasPropertyChanged, propertyType);

            IMethodVisitor    mg              = VisitMethod(propertyInfo.Setter);
            Label             l_finish        = mg.NewLabel();
            Label             l_noOldValue    = mg.NewLabel();
            Label             l_noChangeCheck = mg.NewLabel();
            LocalVariableInfo loc_oldValue;

            if (isBoxingNeededForHasPropertyChanged)
            {
                loc_oldValue = mg.NewLocal(typeof(Object));
            }
            else
            {
                loc_oldValue = mg.NewLocal(propertyType);
            }
            LocalVariableInfo loc_valueChanged = mg.NewLocal <bool>();

            MethodInstance m_getSuper       = EnhancerUtil.GetSuperGetter(propertyInfo);
            bool           relationProperty = m_getSuper.Name.EndsWith(ValueHolderIEC.GetNoInitSuffix());

            // initialize flag with false
            mg.Push(false);
            mg.StoreLocal(loc_valueChanged);

            // initialize oldValue with null
            mg.PushNullOrZero(loc_oldValue.LocalType);
            mg.StoreLocal(loc_oldValue);

            if (relationProperty)
            {
                // check if a setter call to an UNINITIALIZED relation occured with value null
                // if it the case there would be no PCE because oldValue & newValue are both null
                // but we need a PCE in this special case
                Label         l_noSpecialHandling = mg.NewLabel();
                FieldInstance f_state             = State.GetAlreadyImplementedField(ValueHolderIEC.GetInitializedFieldName(propertyInfo.Name));
                mg.GetThisField(f_state);
                mg.PushEnum(ValueHolderState.INIT);
                mg.IfCmp(typeof(ValueHolderState), CompareOperator.EQ, l_noSpecialHandling);
                mg.Push(true);
                mg.StoreLocal(loc_valueChanged);
                mg.Mark(l_noSpecialHandling);
            }

            // check if value should be checked to decide for a PCE
            mg.LoadLocal(loc_valueChanged);
            mg.IfZCmp(CompareOperator.NE, l_noOldValue);

            // get old field value calling super property getter
            mg.LoadThis();
            mg.InvokeOnExactOwner(m_getSuper);
            if (isBoxingNeededForHasPropertyChanged)
            {
                mg.Box(propertyType);
            }
            mg.StoreLocal(loc_oldValue);

            mg.Mark(l_noOldValue);

            // set new field value calling super property setter
            mg.LoadThis();
            mg.LoadArg(0);
            mg.InvokeOnExactOwner(EnhancerUtil.GetSuperSetter(propertyInfo));
            mg.PopIfReturnValue(EnhancerUtil.GetSuperSetter(propertyInfo));

            // check if value should be checked to decide for a PCE
            mg.LoadLocal(loc_valueChanged);
            mg.IfZCmp(CompareOperator.NE, l_noChangeCheck);

            LocalVariableInfo loc_newValue = null;

            if (isBoxingNeededForHasPropertyChanged)
            {
                loc_newValue = mg.NewLocal(typeof(Object)); // loc_1  Object newValue
                // Object loc_1 = (Object)value;
                mg.LoadArg(0);
                mg.Box(propertyType);
                mg.StoreLocal(loc_newValue);
            }
            mg.CallThisGetter(p_propertyChangeTemplate);
            // call HasPropertyChanged (static)
            mg.LoadThis();              // "this" as Object obj
            mg.Push(propertyInfo.Name); // String propertyName
            mg.LoadLocal(loc_oldValue);
            if (loc_newValue != null)
            {
                mg.LoadLocal(loc_newValue);
            }
            else
            {
                mg.LoadArg(0);
            }
            mg.InvokeVirtual(m_hasPropertyChanged);
            //// if (!result)
            //// { return; }
            mg.IfZCmp(CompareOperator.EQ, l_finish);

            mg.Mark(l_noChangeCheck);
            // call firePropertyChange on this
            mg.LoadThis();
            // propertyChangeSupport
            mg.GetThisField(f_propertyChangeSupport);
            // property
            mg.CallThisGetter(p_getterMethodHandle);
            // oldValue
            mg.LoadLocal(loc_oldValue);
            if (!isBoxingNeededForHasPropertyChanged && propertyType.IsValueType)
            {
                // old value has not already been boxed but it is now necessary
                mg.ValueOf(propertyType);
            }
            // newValue
            if (loc_newValue != null)
            {
                mg.LoadLocal(loc_newValue);
            }
            else
            {
                mg.LoadArg(0);
                if (propertyType.IsValueType)
                {
                    mg.ValueOf(propertyType);
                }
            }
            // firePropertyChange(propertyChangeSupport, property, oldValue, newValue)
            mg.InvokeVirtual(m_firePropertyChange);

            // return
            mg.Mark(l_finish);
            mg.ReturnVoidOrThis();
            mg.EndMethod();
        }