private void CheckStaticCtorFunction(LuaBlockSyntax body)
        {
            var statements = new List <LuaStatementSyntax>();

            statements.AddRange(staticInitStatements_);
            if (staticCtor_ != null)
            {
                statements.AddRange(staticCtor_.Function.Body.Statements);
            }
            if (statements.Count > 0 || IsForceStaticCtor)
            {
                var staticCtor = new LuaFunctionExpressionSyntax();
                staticCtor.AddParameter(LuaIdentifierNameSyntax.This);
                staticCtor.Body.Statements.AddRange(statements);
                AddStaticAssignmentNames(staticCtor.Body);
                if (staticCtor_ != null && staticCtor_.Document != null)
                {
                    body.AddStatement(staticCtor_.Document);
                }
                AddInitFunction(body, LuaIdentifierNameSyntax.StaticCtor, staticCtor);
            }
        }
Example #2
0
        private void CheckCtorsFunction(LuaBlockSyntax body)
        {
            bool hasInit  = initStatements_.Count > 0;
            bool hasCtors = ctors_.Count > 0;

            if (hasCtors)
            {
                if (hasInit)
                {
                    if (ctors_.Count == 1)
                    {
                        ctors_.First().Function.Body.Statements.InsertRange(0, initStatements_);
                    }
                    else
                    {
                        var init = LuaIdentifierNameSyntax.Init;
                        AddInitFunction(body, init, GetInitFunction(), false);
                        foreach (var ctor in ctors_)
                        {
                            if (!ctor.Function.IsInvokeThisCtor)
                            {
                                var invocationInit = new LuaInvocationExpressionSyntax(init, LuaIdentifierNameSyntax.This);
                                ctor.Function.Body.Statements.Insert(0, invocationInit);
                            }
                        }
                    }
                }

                if (ctors_.Count == 1)
                {
                    var ctor = ctors_.First();
                    if (ctor.Function.Body.Statements.Count > 0)
                    {
                        if (ctor.Document != null)
                        {
                            body.AddStatement(ctor.Document);
                        }
                        AddInitFunction(body, LuaIdentifierNameSyntax.Ctor, ctor.Function);
                    }
                }
                else
                {
                    LuaTableExpression ctrosTable = new LuaTableExpression();
                    int index = 1;
                    foreach (var ctor in ctors_)
                    {
                        if (ctor.Document != null)
                        {
                            body.AddStatement(ctor.Document);
                        }
                        LuaIdentifierNameSyntax nameIdentifier = GetCtorNameString(index);
                        AddInitFunction(body, nameIdentifier, ctor.Function, false);
                        ctrosTable.Add(nameIdentifier);
                        ++index;
                    }
                    AddResultTable(LuaIdentifierNameSyntax.Ctor, ctrosTable);
                }
            }
            else
            {
                if (hasInit)
                {
                    AddInitFunction(body, LuaIdentifierNameSyntax.Ctor, GetInitFunction());
                }
            }
        }