示例#1
0
        public override void VisitEventDeclaration(EventDeclaration eventDeclaration)
        {
            foreach (var singleEvt in eventDeclaration.Variables) {
                var resolveResult = _resolver.Resolve(singleEvt);
                if (!(resolveResult is MemberResolveResult)) {
                    _errorReporter.Region = eventDeclaration.GetRegion();
                    _errorReporter.InternalError("Event declaration " + singleEvt.Name + " does not resolve to a member.");
                    return;
                }

                var evt = ((MemberResolveResult)resolveResult).Member as IEvent;
                if (evt == null) {
                    _errorReporter.Region = eventDeclaration.GetRegion();
                    _errorReporter.InternalError("Event declaration " + singleEvt.Name + " does not resolve to an event (resolves to " + resolveResult.ToString() + ")");
                    return;
                }

                var jsClass = GetJsClass(evt.DeclaringTypeDefinition);
                if (jsClass == null)
                    return;

                var impl = _metadataImporter.GetEventSemantics(evt);
                switch (impl.Type) {
                    case EventScriptSemantics.ImplType.AddAndRemoveMethods: {
                        if ((impl.AddMethod != null && impl.AddMethod.GenerateCode) || (impl.RemoveMethod != null && impl.RemoveMethod.GenerateCode)) {
                            if (evt.IsAbstract) {
                                if (impl.AddMethod.GenerateCode)
                                    AddCompiledMethodToType(jsClass, evt.AddAccessor, impl.AddMethod, new JsMethod(evt.AddAccessor, impl.AddMethod.Name, null, null));
                                if (impl.RemoveMethod.GenerateCode)
                                    AddCompiledMethodToType(jsClass, evt.RemoveAccessor, impl.RemoveMethod, new JsMethod(evt.RemoveAccessor, impl.RemoveMethod.Name, null, null));
                            }
                            else {
                                var fieldName = _metadataImporter.GetAutoEventBackingFieldName(evt);
                                if (singleEvt.Initializer.IsNull) {
                                    AddDefaultFieldInitializerToType(jsClass, fieldName, evt, evt.ReturnType, evt.DeclaringTypeDefinition, evt.IsStatic);
                                }
                                else {
                                    CompileAndAddFieldInitializerToType(jsClass, fieldName, evt.DeclaringTypeDefinition, singleEvt.Initializer, evt.IsStatic);
                                }

                                CompileAndAddAutoEventMethodsToType(jsClass, eventDeclaration, evt, impl, fieldName);
                            }
                        }
                        break;
                    }

                    case EventScriptSemantics.ImplType.NotUsableFromScript: {
                        break;
                    }

                    default: {
                        throw new InvalidOperationException("Invalid event implementation type");
                    }
                }
            }
        }
示例#2
0
        public override void VisitEventDeclaration(EventDeclaration eventDeclaration)
        {
            foreach (var singleEvt in eventDeclaration.Variables)
            {
                var resolveResult = _resolver.Resolve(singleEvt);
                if (!(resolveResult is MemberResolveResult))
                {
                    _errorReporter.Region = eventDeclaration.GetRegion();
                    _errorReporter.InternalError("Event declaration " + singleEvt.Name + " does not resolve to a member.");
                    return;
                }

                var evt = ((MemberResolveResult)resolveResult).Member as IEvent;
                if (evt == null)
                {
                    _errorReporter.Region = eventDeclaration.GetRegion();
                    _errorReporter.InternalError("Event declaration " + singleEvt.Name + " does not resolve to an event (resolves to " + resolveResult.ToString() + ")");
                    return;
                }

                var jsClass = GetJsClass(evt.DeclaringTypeDefinition);
                if (jsClass == null)
                {
                    return;
                }

                var impl = _metadataImporter.GetEventSemantics(evt);
                switch (impl.Type)
                {
                case EventScriptSemantics.ImplType.AddAndRemoveMethods: {
                    if (evt.IsAbstract)
                    {
                        if (impl.AddMethod.GeneratedMethodName != null)
                        {
                            AddCompiledMethodToType(jsClass, evt.AddAccessor, impl.AddMethod, new JsMethod(evt.AddAccessor, impl.AddMethod.GeneratedMethodName, null, null));
                        }
                        if (impl.RemoveMethod.GeneratedMethodName != null)
                        {
                            AddCompiledMethodToType(jsClass, evt.RemoveAccessor, impl.RemoveMethod, new JsMethod(evt.RemoveAccessor, impl.RemoveMethod.GeneratedMethodName, null, null));
                        }
                    }
                    else
                    {
                        var fieldName = _metadataImporter.GetAutoEventBackingFieldName(evt);
                        if (_metadataImporter.ShouldGenerateAutoEventBackingField(evt))
                        {
                            if (singleEvt.Initializer.IsNull)
                            {
                                AddDefaultFieldInitializerToType(jsClass, fieldName, evt, evt.IsStatic);
                            }
                            else
                            {
                                CompileAndAddFieldInitializerToType(jsClass, fieldName, evt, singleEvt.Initializer, evt.IsStatic);
                            }
                        }

                        CompileAndAddAutoEventMethodsToType(jsClass, eventDeclaration, evt, impl, fieldName);
                    }
                    break;
                }

                case EventScriptSemantics.ImplType.NotUsableFromScript: {
                    break;
                }

                default: {
                    throw new InvalidOperationException("Invalid event implementation type");
                }
                }
            }
        }