Esempio n. 1
0
        protected override void DoEmit()
        {
            XmlToJsDoc.EmitComment(this, Emitter.Translator.EmitNode);
            string globalTarget = H5Types.GetGlobalTarget(TypeInfo.Type.GetDefinition(), TypeInfo.TypeDeclaration);

            if (globalTarget != null)
            {
                CheckGlobalClass();
                Emitter.NamedFunctions = new Dictionary <string, string>();
                WriteTopInitMethods();

                Write(JS.Types.H5.APPLY);
                WriteOpenParentheses();
                Write(globalTarget);
                Write(", ");
                BeginBlock();

                new MethodBlock(Emitter, TypeInfo, true).Emit();
                EmitMetadata();

                WriteNewLine();
                EndBlock();
                WriteCloseParentheses();
                WriteSemiColon();

                EmitAnonymousTypes();
                EmitNamedFunctions();

                WriteAfterInitMethods();

                WriteNewLine();
            }
            else
            {
                EmitClassHeader();

                Emitter.NamedFunctions = new Dictionary <string, string>();

                if (TypeInfo.TypeDeclaration.ClassType != ClassType.Interface)
                {
                    MethodDeclaration entryPoint = null;
                    if (TypeInfo.StaticMethods.Any(group =>
                    {
                        return(group.Value.Any(method =>
                        {
                            var result = Helpers.IsEntryPointMethod(Emitter, method);
                            if (result)
                            {
                                entryPoint = method;
                            }
                            return result;
                        }));
                    }))
                    {
                        if (!entryPoint.Body.IsNull)
                        {
                            Emitter.VisitMethodDeclaration(entryPoint);
                        }
                    }

                    EmitStaticBlock();
                    EmitInstantiableBlock();
                }

                EmitClassEnd();
            }
        }
Esempio n. 2
0
        protected virtual void EmitMethods(Dictionary <string, List <MethodDeclaration> > methods, Dictionary <string, List <EntityDeclaration> > properties, Dictionary <OperatorType, List <OperatorDeclaration> > operators)
        {
            int pos        = Emitter.Output.Length;
            var writerInfo = SaveWriter();

            string globalTarget = H5Types.GetGlobalTarget(TypeInfo.Type.GetDefinition(), TypeInfo.TypeDeclaration);

            if (globalTarget == null)
            {
                EnsureComma();
                Write(JS.Fields.METHODS);
                WriteColon();
                BeginBlock();
            }

            int checkPos = Emitter.Output.Length;

            var names = new List <string>(properties.Keys);

            foreach (var name in names)
            {
                var props = properties[name];

                foreach (var prop in props)
                {
                    if (prop is PropertyDeclaration)
                    {
                        Emitter.VisitPropertyDeclaration((PropertyDeclaration)prop);
                    }
                    else if (prop is CustomEventDeclaration)
                    {
                        Emitter.VisitCustomEventDeclaration((CustomEventDeclaration)prop);
                    }
                    else if (prop is IndexerDeclaration)
                    {
                        Emitter.VisitIndexerDeclaration((IndexerDeclaration)prop);
                    }
                }
            }

            names = new List <string>(methods.Keys);

            foreach (var name in names)
            {
                EmitMethodsGroup(methods[name]);
            }

            if (operators != null)
            {
                var ops = new List <OperatorType>(operators.Keys);

                foreach (var op in ops)
                {
                    EmitOperatorGroup(operators[op]);
                }
            }

            if (TypeInfo.ClassType == ClassType.Struct)
            {
                if (!StaticBlock)
                {
                    EmitStructMethods();
                }
                else
                {
                    string structName = H5Types.ToJsName(TypeInfo.Type, Emitter);
                    if (TypeInfo.Type.TypeArguments.Count > 0 &&
                        !Helpers.IsIgnoreGeneric(TypeInfo.Type, Emitter))
                    {
                        structName = "(" + structName + ")";
                    }

                    EnsureComma();
                    Write(JS.Funcs.GETDEFAULTVALUE + ": function () { return new " + structName + "(); }");
                    Emitter.Comma = true;
                }
            }
            else if (StaticBlock)
            {
                var ctor = TypeInfo.Type.GetConstructors().FirstOrDefault(c => c.Parameters.Count == 0 && Emitter.GetInline(c) != null);

                if (ctor != null)
                {
                    var code = Emitter.GetInline(ctor);
                    EnsureComma();
                    Write(JS.Funcs.GETDEFAULTVALUE + ": function () ");
                    BeginBlock();
                    Write("return ");
                    var argsInfo = new ArgumentsInfo(Emitter, ctor);
                    new InlineArgumentsBlock(Emitter, argsInfo, code).Emit();
                    Write(";");
                    WriteNewLine();
                    EndBlock();
                    Emitter.Comma = true;
                }
            }

            if (globalTarget == null)
            {
                if (checkPos == Emitter.Output.Length)
                {
                    Emitter.IsNewLine = writerInfo.IsNewLine;
                    Emitter.ResetLevel(writerInfo.Level);
                    Emitter.Comma         = writerInfo.Comma;
                    Emitter.Output.Length = pos;
                }
                else
                {
                    WriteNewLine();
                    EndBlock();
                }
            }
        }