Ejemplo n.º 1
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;
        }
Ejemplo n.º 2
0
        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;
            }
        }
Ejemplo n.º 3
0
        protected virtual void EmitLambda(IEnumerable <ParameterDeclaration> parameters, AstNode body, AstNode context)
        {
            var rr         = Emitter.Resolver.ResolveNode(context);
            var oldLifting = Emitter.ForbidLifting;

            Emitter.ForbidLifting = false;
            var             noLiftingRule = Emitter.Rules.Lambda == LambdaRule.Plain;
            CaptureAnalyzer analyzer      = null;

            if (!noLiftingRule)
            {
                analyzer = new CaptureAnalyzer(Emitter);
                analyzer.Analyze(Body, Parameters.Select(p => p.Name));
            }

            var oldLevel = Emitter.Level;

            if (!noLiftingRule && analyzer.UsedVariables.Count == 0)
            {
                Emitter.ResetLevel();
                Indent();
            }

            IAsyncBlock asyncBlock = null;

            PushLocals();

            if (IsAsync)
            {
                if (context is LambdaExpression)
                {
                    asyncBlock = new AsyncBlock(Emitter, (LambdaExpression)context);
                }
                else
                {
                    asyncBlock = new AsyncBlock(Emitter, (AnonymousMethodExpression)context);
                }

                asyncBlock.InitAsyncBlock();
            }
            else if (YieldBlock.HasYield(body))
            {
                IsAsync = true;
                if (context is LambdaExpression)
                {
                    asyncBlock = new GeneratorBlock(Emitter, (LambdaExpression)context);
                }
                else
                {
                    asyncBlock = new GeneratorBlock(Emitter, (AnonymousMethodExpression)context);
                }

                asyncBlock.InitAsyncBlock();
            }

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

            AddLocals(parameters, body);

            bool block = body is BlockStatement;

            Write("");

            var savedThisCount       = Emitter.ThisRefCounter;
            var capturedVariables    = GetCapturedLoopVariablesNames();
            var hasCapturedVariables = capturedVariables != null && capturedVariables.Length > 0;

            if (hasCapturedVariables)
            {
                Write("(function ($me, ");
                Write(string.Join(", ", capturedVariables) + ") ");
                BeginBlock();
                Write("return ");
            }

            var savedPos = Emitter.Output.Length;

            WriteFunction();
            EmitMethodParameters(parameters, null, context);
            WriteSpace();

            int pos = 0;

            if (!block && !IsAsync)
            {
                BeginBlock();
                pos = Emitter.Output.Length;
            }

            bool isSimpleLambda = body.Parent is LambdaExpression && !block && !IsAsync;

            if (isSimpleLambda)
            {
                ConvertParamsToReferences(parameters);

                if (!(rr is LambdaResolveResult lrr) || lrr.ReturnType.Kind != TypeKind.Void)
                {
                    WriteReturn(true);
                }
            }

            if (IsAsync)
            {
                asyncBlock.Emit(true);
            }
            else
            {
                body.AcceptVisitor(Emitter);
            }

            if (isSimpleLambda)
            {
                WriteSemiColon();
            }

            if (!block && !IsAsync)
            {
                WriteNewLine();
                EndBlock();
            }

            if (!block && !IsAsync)
            {
                EmitTempVars(pos);
            }

            if (!noLiftingRule && analyzer.UsedVariables.Count == 0)
            {
                if (!Emitter.ForbidLifting)
                {
                    var name          = "f" + (Emitter.NamedFunctions.Count + 1);
                    var code          = Emitter.Output.ToString().Substring(savedPos);
                    var codeForComare = RemoveTokens(code);

                    var pair = Emitter.NamedFunctions.FirstOrDefault(p =>
                    {
                        if (Emitter.AssemblyInfo.SourceMap.Enabled)
                        {
                            return(RemoveTokens(p.Value) == codeForComare);
                        }

                        return(p.Value == code);
                    });

                    if (pair.Key != null && pair.Value != null)
                    {
                        name = pair.Key;
                    }
                    else
                    {
                        Emitter.NamedFunctions.Add(name, code);
                    }

                    Emitter.Output.Remove(savedPos, Emitter.Output.Length - savedPos);
                    Emitter.Output.Insert(savedPos, JS.Vars.D_ + "." + H5Types.ToJsName(Emitter.TypeInfo.Type, Emitter, true) + "." + name);
                }

                Emitter.ResetLevel(oldLevel);
            }

            Emitter.ForbidLifting = oldLifting;


            var methodDeclaration = Body.GetParent <MethodDeclaration>();
            var thisCaptured      = Emitter.ThisRefCounter > savedThisCount ||
                                    IsAsync && methodDeclaration != null &&
                                    !methodDeclaration.HasModifier(Modifiers.Static);

            if (thisCaptured)
            {
                Emitter.Output.Insert(savedPos, JS.Funcs.H5_BIND + (hasCapturedVariables ? "($me, " : "(this, "));
                WriteCloseParentheses();
            }

            if (hasCapturedVariables)
            {
                WriteSemiColon(true);
                EndBlock();
                Write(")(");

                Write("this, ");

                Write(string.Join(", ", capturedVariables));
                Write(")");
            }

            PopLocals();
            ClearLocalsMap(prevMap);
            ClearLocalsNamesMap(prevNamesMap);
        }
Ejemplo n.º 4
0
        public void DoEmitBlock()
        {
            if (BlockStatement.Parent is MethodDeclaration)
            {
                var methodDeclaration = (MethodDeclaration)BlockStatement.Parent;
                if (!methodDeclaration.ReturnType.IsNull)
                {
                    var rr = Emitter.Resolver.ResolveNode(methodDeclaration.ReturnType);
                    ReturnType = rr.Type;
                }
                ConvertParamsToReferences(methodDeclaration.Parameters);
            }
            else if (BlockStatement.Parent is AnonymousMethodExpression)
            {
                var methodDeclaration = (AnonymousMethodExpression)BlockStatement.Parent;
                var rr = Emitter.Resolver.ResolveNode(methodDeclaration);
                ReturnType = rr.Type;
                ConvertParamsToReferences(methodDeclaration.Parameters);
            }
            else if (BlockStatement.Parent is LambdaExpression)
            {
                var methodDeclaration = (LambdaExpression)BlockStatement.Parent;
                var rr = Emitter.Resolver.ResolveNode(methodDeclaration);
                ReturnType = rr.Type;
                ConvertParamsToReferences(methodDeclaration.Parameters);
            }
            else if (BlockStatement.Parent is ConstructorDeclaration)
            {
                ConvertParamsToReferences(((ConstructorDeclaration)BlockStatement.Parent).Parameters);
            }
            else if (BlockStatement.Parent is OperatorDeclaration)
            {
                ConvertParamsToReferences(((OperatorDeclaration)BlockStatement.Parent).Parameters);
            }
            else if (BlockStatement.Parent is Accessor)
            {
                var role = BlockStatement.Parent.Role.ToString();

                if (role == "Setter")
                {
                    ConvertParamsToReferences(new ParameterDeclaration[] { new ParameterDeclaration {
                                                                               Name = "value"
                                                                           } });
                }
                else if (role == "Getter")
                {
                    var methodDeclaration = (Accessor)BlockStatement.Parent;
                    if (!methodDeclaration.ReturnType.IsNull)
                    {
                        var rr = Emitter.Resolver.ResolveNode(methodDeclaration.ReturnType);
                        ReturnType = rr.Type;
                    }
                }
            }

            if (IsMethodBlock && YieldBlock.HasYield(BlockStatement))
            {
                IsYield = true;
            }

            if (IsMethodBlock)
            {
                OldReturnType      = Emitter.ReturnType;
                Emitter.ReturnType = ReturnType;
            }

            if (Emitter.BeforeBlock != null)
            {
                Emitter.BeforeBlock();
                Emitter.BeforeBlock = null;
            }

            var ra = ReachabilityAnalysis.Create(BlockStatement, Emitter.Resolver.Resolver);

            BlockStatement.Children.ToList().ForEach(child =>
            {
                if (child is Statement statement && !ra.IsReachable(statement))
                {
                    return;
                }

                child.AcceptVisitor(Emitter);
            });
        }
Ejemplo n.º 5
0
        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;
            }
        }