Exemple #1
0
        protected virtual void EmitMethodsGroup(List <MethodDeclaration> group)
        {
            if (group.Count == 1)
            {
                if ((!group[0].Body.IsNull || Emitter.GetScript(group[0]) != null) && (!StaticBlock || !Helpers.IsEntryPointMethod(Emitter, group[0])))
                {
                    Emitter.VisitMethodDeclaration(group[0]);
                }
            }
            else
            {
                var typeDef    = Emitter.GetTypeDefinition();
                var name       = group[0].Name;
                var methodsDef = typeDef.Methods.Where(m => m.Name == name);
                Emitter.MethodsGroup        = methodsDef;
                Emitter.MethodsGroupBuilder = new Dictionary <int, StringBuilder>();

                foreach (var method in group)
                {
                    if (!method.Body.IsNull && (!StaticBlock || !Helpers.IsEntryPointMethod(Emitter, group[0])))
                    {
                        Emitter.VisitMethodDeclaration(method);
                    }
                }

                Emitter.MethodsGroup        = null;
                Emitter.MethodsGroupBuilder = null;
            }
        }
        protected virtual void EmitPropertyMethod(CustomEventDeclaration customEventDeclaration, Accessor accessor, bool remover)
        {
            if (!accessor.IsNull && Emitter.GetInline(accessor) == null)
            {
                EnsureComma();

                ResetLocals();

                var prevMap      = BuildLocalsMap();
                var prevNamesMap = BuildLocalsNamesMap();

                AddLocals(new ParameterDeclaration[] { new ParameterDeclaration {
                                                           Name = "value"
                                                       } }, accessor.Body);
                XmlToJsDoc.EmitComment(this, CustomEventDeclaration);
                var member_rr = (MemberResolveResult)Emitter.Resolver.ResolveNode(customEventDeclaration);

                Write(Helpers.GetEventRef(customEventDeclaration, Emitter, remover, false, false, OverloadsCollection.ExcludeTypeParameterForDefinition(member_rr)));
                WriteColon();
                WriteFunction();
                var m_rr = (MemberResolveResult)Emitter.Resolver.ResolveNode(customEventDeclaration);
                var nm   = Helpers.GetFunctionName(Emitter.AssemblyInfo.NamedFunctions, m_rr.Member, Emitter, remover);
                if (nm != null)
                {
                    Write(nm);
                }
                WriteOpenParentheses();
                Write("value");
                WriteCloseParentheses();
                WriteSpace();

                var script = Emitter.GetScript(accessor);

                if (script == null)
                {
                    accessor.Body.AcceptVisitor(Emitter);
                }
                else
                {
                    BeginBlock();

                    WriteLines(script);

                    EndBlock();
                }

                ClearLocalsMap(prevMap);
                ClearLocalsNamesMap(prevNamesMap);
                Emitter.Comma = true;
            }
        }
Exemple #3
0
        protected virtual void EmitCtorForInstantiableClass()
        {
            var baseType        = Emitter.GetBaseTypeDefinition();
            var typeDef         = Emitter.GetTypeDefinition();
            var isObjectLiteral = Emitter.Validator.IsObjectLiteral(typeDef);
            var isPlainMode     = Emitter.Validator.GetObjectCreateMode(typeDef) == 0;

            var ctorWrappers = isObjectLiteral ? new string[0] : EmitInitMembers().ToArray();

            if (!TypeInfo.HasRealInstantiable(Emitter) && ctorWrappers.Length == 0 || isObjectLiteral && isPlainMode)
            {
                if (ctorHeader)
                {
                    WriteNewLine();
                    EndBlock();
                }
                return;
            }

            bool forceDefCtor = isObjectLiteral && Emitter.Validator.GetObjectCreateMode(typeDef) == 1 && TypeInfo.Ctors.Count == 0;

            if (typeDef.IsValueType || forceDefCtor || (TypeInfo.Ctors.Count == 0 && ctorWrappers.Length > 0))
            {
                TypeInfo.Ctors.Add(new ConstructorDeclaration
                {
                    Modifiers = Modifiers.Public,
                    Body      = new BlockStatement()
                });
            }

            if (!ctorHeader && TypeInfo.Ctors.Count > 0)
            {
                EnsureComma();
                ctorHeader = true;
                Write(JS.Fields.CTORS);
                WriteColon();
                BeginBlock();
            }

            Emitter.InConstructor = true;
            foreach (var ctor in TypeInfo.Ctors)
            {
                var oldRules = Emitter.Rules;

                if (ctor.Body.HasChildren)
                {
                    if (Emitter.Resolver.ResolveNode(ctor) is MemberResolveResult rr)
                    {
                        Emitter.Rules = Rules.Get(Emitter, rr.Member);
                    }
                }

                EnsureComma();
                ResetLocals();
                var prevMap      = BuildLocalsMap();
                var prevNamesMap = BuildLocalsNamesMap();
                AddLocals(ctor.Parameters, ctor.Body);

                var ctorName = JS.Funcs.CONSTRUCTOR;

                if (TypeInfo.Ctors.Count > 1 && ctor.Parameters.Count > 0)
                {
                    var overloads = OverloadsCollection.Create(Emitter, ctor);
                    ctorName = overloads.GetOverloadName();
                }

                XmlToJsDoc.EmitComment(this, ctor);
                Write(ctorName);

                WriteColon();
                WriteFunction();

                int pos = Emitter.Output.Length;
                EmitMethodParameters(ctor.Parameters, null, ctor);
                var ctorParams = Emitter.Output.ToString().Substring(pos);

                WriteSpace();
                BeginBlock();
                var len            = Emitter.Output.Length;
                var requireNewLine = false;

                var         noThisInvocation = ctor.Initializer == null || ctor.Initializer.IsNull || ctor.Initializer.ConstructorInitializerType == ConstructorInitializerType.Base;
                IWriterInfo oldWriter        = null;
                if (ctorWrappers.Length > 0 && noThisInvocation)
                {
                    oldWriter = SaveWriter();
                    NewWriter();
                }

                ConvertParamsToReferences(ctor.Parameters);

                if (len != Emitter.Output.Length)
                {
                    requireNewLine = true;
                }

                if (isObjectLiteral)
                {
                    if (requireNewLine)
                    {
                        WriteNewLine();
                    }

                    Write("var " + JS.Vars.D_THIS + " = ");

                    var isBaseObjectLiteral = baseType != null && Emitter.Validator.IsObjectLiteral(baseType);
                    if (isBaseObjectLiteral && baseType != null && (!Emitter.Validator.IsExternalType(baseType) || Emitter.Validator.IsH5Class(baseType)) ||
                        (ctor.Initializer != null && ctor.Initializer.ConstructorInitializerType == ConstructorInitializerType.This))
                    {
                        EmitBaseConstructor(ctor, ctorName, true);
                    }
                    else if (isBaseObjectLiteral && baseType != null && ctor.Initializer != null &&
                             ctor.Initializer.ConstructorInitializerType == ConstructorInitializerType.Base)
                    {
                        EmitExternalBaseCtor(ctor, ref requireNewLine);
                    }
                    else
                    {
                        Write("{ };");
                    }

                    WriteNewLine();

                    string name = Emitter.Validator.GetCustomTypeName(typeDef, Emitter, false);
                    if (name.IsEmpty())
                    {
                        name = H5Types.ToJsName(TypeInfo.Type, Emitter);
                    }

                    Write(JS.Vars.D_THIS + "." + JS.Funcs.GET_TYPE + " = function () { return " + name + "; };");

                    WriteNewLine();
                    Write("(function ()");
                    BeginBlock();
                    requireNewLine = false;
                }

                var beginPosition = Emitter.Output.Length;

                if (noThisInvocation)
                {
                    if (requireNewLine)
                    {
                        WriteNewLine();
                    }

                    if (isObjectLiteral)
                    {
                        var fieldBlock = new FieldBlock(Emitter, TypeInfo, false, false, true);
                        fieldBlock.Emit();

                        var properties = TypeInfo.InstanceProperties;

                        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 p)
                                {
                                    if (p.Getter.Body.IsNull && p.Setter.Body.IsNull)
                                    {
                                        continue;
                                    }

                                    Write(JS.Types.Object.DEFINEPROPERTY);
                                    WriteOpenParentheses();
                                    Write("this, ");
                                    WriteScript(OverloadsCollection.Create(Emitter, p).GetOverloadName());
                                    WriteComma();
                                    Emitter.Comma = false;
                                    BeginBlock();
                                    var memberResult = Emitter.Resolver.ResolveNode(p) as MemberResolveResult;
                                    var block        = new VisitorPropertyBlock(Emitter, p);
                                    block.EmitPropertyMethod(p, p.Getter, ((IProperty)memberResult.Member).Getter, false, true);
                                    block.EmitPropertyMethod(p, p.Setter, ((IProperty)memberResult.Member).Setter, true, true);
                                    EnsureComma(true);
                                    Write(JS.Fields.ENUMERABLE + ": true");
                                    WriteNewLine();
                                    EndBlock();
                                    WriteCloseParentheses();
                                    Write(";");
                                    WriteNewLine();
                                }
                            }
                        }
                    }
                    else
                    {
                        Write("this." + JS.Funcs.INITIALIZE + "();");
                        requireNewLine = true;
                    }
                }

                if (!isObjectLiteral)
                {
                    if (baseType != null && (!Emitter.Validator.IsExternalType(baseType) || Emitter.Validator.IsH5Class(baseType)) ||
                        (ctor.Initializer != null && ctor.Initializer.ConstructorInitializerType == ConstructorInitializerType.This))
                    {
                        if (requireNewLine)
                        {
                            WriteNewLine();
                            requireNewLine = false;
                        }
                        EmitBaseConstructor(ctor, ctorName, false);
                    }
                    else if (baseType != null && (ctor.Initializer == null || ctor.Initializer.IsNull || ctor.Initializer.ConstructorInitializerType == ConstructorInitializerType.Base))
                    {
                        EmitExternalBaseCtor(ctor, ref requireNewLine);
                    }
                }

                var script = Emitter.GetScript(ctor);
                var hasAdditionalIndent = false;

                if (script == null)
                {
                    if (ctor.Body.HasChildren)
                    {
                        if (requireNewLine)
                        {
                            WriteNewLine();
                        }

                        ctor.Body.AcceptChildren(Emitter);

                        if (!Emitter.IsAsync)
                        {
                            hasAdditionalIndent = Emitter.TempVariables.Count > 0;
                            EmitTempVars(beginPosition, true);
                        }
                    }
                    else if (requireNewLine)
                    {
                        WriteNewLine();
                    }
                }
                else
                {
                    if (requireNewLine)
                    {
                        WriteNewLine();
                    }

                    WriteLines(script);
                }

                if (oldWriter != null)
                {
                    WrapBody(oldWriter, ctorWrappers, ctorParams);
                }

                if (isObjectLiteral)
                {
                    if (requireNewLine)
                    {
                        WriteNewLine();
                    }
                    EndBlock();
                    Write(")." + JS.Funcs.CALL + "(" + JS.Vars.D_THIS + ");");
                    WriteNewLine();
                    Write("return " + JS.Vars.D_THIS + ";");
                    WriteNewLine();
                }

                if (hasAdditionalIndent)
                {
                    Indent();
                }

                EndBlock();
                Emitter.Comma = true;
                ClearLocalsMap(prevMap);
                ClearLocalsNamesMap(prevNamesMap);

                Emitter.Rules = oldRules;
            }

            Emitter.InConstructor = false;

            if (ctorHeader)
            {
                WriteNewLine();
                EndBlock();
            }
        }
Exemple #4
0
        protected void VisitMethodDeclaration(MethodDeclaration methodDeclaration)
        {
            foreach (var attrSection in methodDeclaration.Attributes)
            {
                foreach (var attr in attrSection.Attributes)
                {
                    var rr = Emitter.Resolver.ResolveNode(attr.Type);
                    if (rr.Type.FullName == "H5.ExternalAttribute")
                    {
                        return;
                    }
                    else if (rr.Type.FullName == "H5.InitAttribute")
                    {
                        InitPosition initPosition = InitPosition.After;

                        if (attr.HasArgumentList)
                        {
                            if (attr.Arguments.Any())
                            {
                                var argExpr = attr.Arguments.First();
                                var argrr   = Emitter.Resolver.ResolveNode(argExpr);
                                if (argrr.ConstantValue is int)
                                {
                                    initPosition = (InitPosition)argrr.ConstantValue;
                                }
                            }
                        }

                        if (initPosition > 0)
                        {
                            return;
                        }
                    }
                }
            }

            EnsureComma();
            ResetLocals();

            var prevMap      = BuildLocalsMap();
            var prevNamesMap = BuildLocalsNamesMap();

            AddLocals(methodDeclaration.Parameters, methodDeclaration.Body);

            var overloads = OverloadsCollection.Create(Emitter, methodDeclaration);

            XmlToJsDoc.EmitComment(this, MethodDeclaration);
            var isEntryPoint = Helpers.IsEntryPointMethod(Emitter, MethodDeclaration);
            var member_rr    = (MemberResolveResult)Emitter.Resolver.ResolveNode(MethodDeclaration);

            string name = overloads.GetOverloadName(false, null, excludeTypeOnly: OverloadsCollection.ExcludeTypeParameterForDefinition(member_rr));

            if (isEntryPoint)
            {
                Write(JS.Funcs.ENTRY_POINT_NAME);
            }
            else
            {
                Write(name);
            }

            WriteColon();

            WriteFunction();

            if (isEntryPoint)
            {
                Write(name);
                WriteSpace();
            }

            EmitMethodParameters(methodDeclaration.Parameters, methodDeclaration.TypeParameters.Count > 0 && Helpers.IsIgnoreGeneric(methodDeclaration, Emitter) ? null : methodDeclaration.TypeParameters, methodDeclaration);

            WriteSpace();

            var script = Emitter.GetScript(methodDeclaration);

            if (script == null)
            {
                if (YieldBlock.HasYield(methodDeclaration.Body))
                {
                    new GeneratorBlock(Emitter, methodDeclaration).Emit();
                }
                else if (methodDeclaration.HasModifier(Modifiers.Async) || AsyncBlock.HasGoto(methodDeclaration.Body))
                {
                    new AsyncBlock(Emitter, methodDeclaration).Emit();
                }
                else
                {
                    methodDeclaration.Body.AcceptVisitor(Emitter);
                }
            }
            else
            {
                BeginBlock();

                WriteLines(script);

                EndBlock();
            }

            ClearLocalsMap(prevMap);
            ClearLocalsNamesMap(prevNamesMap);
            Emitter.Comma = true;
        }
        protected virtual void EmitIndexerMethod(IndexerDeclaration indexerDeclaration, IProperty prop, Accessor accessor, IMethod propAccessor, bool setter)
        {
            var isIgnore = propAccessor != null && Emitter.Validator.IsExternalType(propAccessor);

            if (!accessor.IsNull && Emitter.GetInline(accessor) == null && !isIgnore)
            {
                EnsureComma();

                ResetLocals();

                var prevMap      = BuildLocalsMap();
                var prevNamesMap = BuildLocalsNamesMap();

                if (setter)
                {
                    AddLocals(new ParameterDeclaration[] { new ParameterDeclaration {
                                                               Name = "value"
                                                           } }, accessor.Body);
                }
                else
                {
                    AddLocals(new ParameterDeclaration[0], accessor.Body);
                }

                XmlToJsDoc.EmitComment(this, IndexerDeclaration, !setter);

                string accName = null;

                if (prop != null)
                {
                    accName = Emitter.GetEntityNameFromAttr(prop, setter);

                    if (string.IsNullOrEmpty(accName))
                    {
                        var member_rr = (MemberResolveResult)Emitter.Resolver.ResolveNode(indexerDeclaration);

                        var overloads = OverloadsCollection.Create(Emitter, indexerDeclaration, setter);
                        accName = overloads.GetOverloadName(false, Helpers.GetSetOrGet(setter), OverloadsCollection.ExcludeTypeParameterForDefinition(member_rr));
                    }
                }

                Write(accName);
                WriteColon();
                WriteFunction();
                EmitMethodParameters(indexerDeclaration.Parameters, null, indexerDeclaration, setter);

                if (setter)
                {
                    Write(", value)");
                }
                WriteSpace();

                var script = Emitter.GetScript(accessor);

                if (script == null)
                {
                    if (YieldBlock.HasYield(accessor.Body))
                    {
                        new GeneratorBlock(Emitter, accessor).Emit();
                    }
                    else
                    {
                        accessor.Body.AcceptVisitor(Emitter);
                    }
                }
                else
                {
                    BeginBlock();

                    WriteLines(script);

                    EndBlock();
                }

                ClearLocalsMap(prevMap);
                ClearLocalsNamesMap(prevNamesMap);
                Emitter.Comma = true;
            }
        }
Exemple #6
0
        protected void EmitOperatorDeclaration(OperatorDeclaration operatorDeclaration)
        {
            foreach (var attrSection in operatorDeclaration.Attributes)
            {
                foreach (var attr in attrSection.Attributes)
                {
                    var rr = Emitter.Resolver.ResolveNode(attr.Type);
                    if (rr.Type.FullName == "H5.ExternalAttribute")
                    {
                        return;
                    }
                }
            }

            XmlToJsDoc.EmitComment(this, operatorDeclaration);
            EnsureComma();
            ResetLocals();
            var prevMap      = BuildLocalsMap();
            var prevNamesMap = BuildLocalsNamesMap();

            AddLocals(operatorDeclaration.Parameters, operatorDeclaration.Body);

            var overloads = OverloadsCollection.Create(Emitter, operatorDeclaration);

            if (overloads.HasOverloads)
            {
                string name = overloads.GetOverloadName();
                Write(name);
            }
            else
            {
                Write(Emitter.GetEntityName(operatorDeclaration));
            }

            WriteColon();

            WriteFunction();

            EmitMethodParameters(operatorDeclaration.Parameters, null, operatorDeclaration);

            WriteSpace();

            var script = Emitter.GetScript(operatorDeclaration);

            if (script == null)
            {
                operatorDeclaration.Body.AcceptVisitor(Emitter);
            }
            else
            {
                BeginBlock();

                WriteLines(script);

                EndBlock();
            }

            ClearLocalsMap(prevMap);
            ClearLocalsNamesMap(prevNamesMap);
            Emitter.Comma = true;
        }
        public virtual void EmitPropertyMethod(PropertyDeclaration propertyDeclaration, Accessor accessor, IMethod method, bool setter, bool isObjectLiteral)
        {
            if ((!accessor.IsNull || method != null && Helpers.IsScript(method)) && Emitter.GetInline(accessor) == null)
            {
                EnsureComma();

                ResetLocals();

                var prevMap      = BuildLocalsMap();
                var prevNamesMap = BuildLocalsNamesMap();

                if (setter)
                {
                    AddLocals(new ParameterDeclaration[] { new ParameterDeclaration {
                                                               Name = "value"
                                                           } }, accessor.Body);
                }
                else
                {
                    AddLocals(new ParameterDeclaration[0], accessor.Body);
                }

                //XmlToJsDoc.EmitComment(this, this.PropertyDeclaration);

                Write(setter ? JS.Funcs.Property.SET : JS.Funcs.Property.GET);

                WriteColon();
                WriteFunction();

                var m_rr = (MemberResolveResult)Emitter.Resolver.ResolveNode(propertyDeclaration);

                WriteOpenParentheses();
                Write(setter ? "value" : "");
                WriteCloseParentheses();
                WriteSpace();

                var script = Emitter.GetScript(accessor);

                if (script == null)
                {
                    if (YieldBlock.HasYield(accessor.Body))
                    {
                        new GeneratorBlock(Emitter, accessor).Emit();
                    }
                    else
                    {
                        accessor.Body.AcceptVisitor(Emitter);
                    }
                }
                else
                {
                    BeginBlock();

                    WriteLines(script);

                    EndBlock();
                }

                ClearLocalsMap(prevMap);
                ClearLocalsNamesMap(prevNamesMap);
                Emitter.Comma = true;
            }
        }