예제 #1
0
        protected override void DoEmit()
        {
            if (this.Emitter.Output.Length > 0)
            {
                this.WriteNewLine();
            }

            XmlToJsDoc.EmitComment(this, this.Emitter.Translator.EmitNode);
            string globalTarget = BridgeTypes.GetGlobalTarget(this.TypeInfo.Type.GetDefinition(), this.TypeInfo.TypeDeclaration);

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

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

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

                this.WriteNewLine();
                this.EndBlock();
                this.WriteCloseParentheses();
                this.WriteSemiColon();

                this.EmitAnonymousTypes();
                this.EmitNamedFunctions();

                this.WriteAfterInitMethods();

                this.WriteNewLine();
            }
            else
            {
                this.EmitClassHeader();

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

                if (this.TypeInfo.TypeDeclaration.ClassType != ClassType.Interface)
                {
                    this.EmitStaticBlock();
                    this.EmitInstantiableBlock();
                }

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

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

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

            int checkPos = this.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)
                    {
                        this.Emitter.VisitPropertyDeclaration((PropertyDeclaration)prop);
                    }
                    else if (prop is CustomEventDeclaration)
                    {
                        this.Emitter.VisitCustomEventDeclaration((CustomEventDeclaration)prop);
                    }
                    else if (prop is IndexerDeclaration)
                    {
                        this.Emitter.VisitIndexerDeclaration((IndexerDeclaration)prop);
                    }
                }
            }

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

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

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

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

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

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

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

            if (globalTarget == null)
            {
                if (checkPos == this.Emitter.Output.Length)
                {
                    this.Emitter.IsNewLine = writerInfo.IsNewLine;
                    this.Emitter.ResetLevel(writerInfo.Level);
                    this.Emitter.Comma         = writerInfo.Comma;
                    this.Emitter.Output.Length = pos;
                }
                else
                {
                    this.WriteNewLine();
                    this.EndBlock();
                }
            }
        }
예제 #3
0
        protected override void DoEmit()
        {
            XmlToJsDoc.EmitComment(this, this.Emitter.Translator.EmitNode);
            string globalTarget = BridgeTypes.GetGlobalTarget(this.TypeInfo.Type.GetDefinition(), this.TypeInfo.TypeDeclaration);

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

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

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

                this.WriteNewLine();
                this.EndBlock();
                this.WriteCloseParentheses();
                this.WriteSemiColon();

                this.EmitAnonymousTypes();
                this.EmitNamedFunctions();

                this.WriteAfterInitMethods();

                this.WriteNewLine();
            }
            else
            {
                this.EmitClassHeader();

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

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

                    this.EmitStaticBlock();
                    this.EmitInstantiableBlock();
                }

                this.EmitClassEnd();
            }
        }