protected virtual void _visit( JsBlock node )
 {
     throw new NotImplementedException( "JsBlock" );
 }
        JsFunction getInjectionPointsFunction(DefaultResolvedTypeDefinition arg1)
        {
            IList<IMethod> constructors = IEntityUtils.getClassConstructors(arg1);
            IList<IField> fields = IEntityUtils.getFieldsByAttribute( arg1, RandoriClassNames.metadataInject );
            IList<IProperty> properties = IEntityUtils.getPropertiesByAttribute( arg1, RandoriClassNames.metadataInject );
            IList<IField> views = IEntityUtils.getFieldsByAttribute( arg1, RandoriClassNames.metadataView );
            IList<IMethod> methods = IEntityUtils.getClassMethodsByAttribute( arg1, RandoriClassNames.metadataInject );

            JsFunction result = new JsFunction();
            result.Parameters = new List<string>();
            result.Parameters.Add(InjectionPointVariableConstants.INJECTION_POINT_PARAMETER_NAME);

            // function contents
            JsBlock resultsBlock = new JsBlock();
            resultsBlock.Statements = new List<JsStatement>();

            // add the block
            result.Block = resultsBlock;

            // Determine what the super class is in the event we need to call the super class's injectionPoints method
            string superClassPath = null;
            foreach (IType type in arg1.DirectBaseTypes)
            {
                // Careful, type.FullName could be all sorts of stuff, like interfaces
                if (type.Kind == TypeKind.Class)
                {
                    bool initArray = GuiceUtils.shouldExcludeBasedOnNamespace(type.Namespace);
                    if (!initArray)
                    {
                        superClassPath = type.FullName;
                    }
                }
            }

            JsSwitchStatement injectionPoints = getInjectionSwitchStatement( InjectionPointVariableConstants.INJECTION_POINT_PARAMETER_NAME, constructors, fields, methods, views, properties, superClassPath );
            if (injectionPoints != null)
            {
                resultsBlock.Statements.Add( AstUtils.getJsVariableDeclarationStatement( InjectionPointVariableConstants.SWITCH_RETURN_VARIABLE_NAME ) );
                resultsBlock.Statements.Add(injectionPoints);
                // return the result variable
                resultsBlock.Statements.Add( AstUtils.getJsReturnStatement( InjectionPointVariableConstants.SWITCH_RETURN_VARIABLE_NAME ) );
            }
            else
            {
                JsInvocationExpression initer = AstUtils.getEmptyArrayInvocationExpression();
                resultsBlock.Statements.Add(AstUtils.getJsReturnStatement(initer));
            }

            return result;
        }
Beispiel #3
0
        protected JsBlock ExportConstructorBody(IMethod ctor)
        {
            var ctorNode = (ConstructorDeclaration)ctor.GetDeclaration();
            BlockStatement ccc = null;
            if (ctorNode != null)
                ccc = ctorNode.Body;
            //var ccc = ctor.GetDefinition();//.decl as CsConstructor;
            //var ccc = ctor.GetDefinition();
            var block2 = (JsBlock)AstNodeConverter.Visit(ccc);
            if (block2 == null)
                block2 = new JsBlock { Statements = new List<JsStatement>() };
            var ce = ctor.GetDeclaringTypeDefinition();
            var isClr = Sk.IsClrType(ce);
            var isPrototype = Sk.IsNativeType(ce);
            var statements = new List<JsStatement>();
            //instance fields initializations
            if (!Sk.InlineFields(ce))
            {
                var isGlobal = Sk.IsGlobalType(ce);
                var fields = GetExportedDeclaredAndGeneratedFields(ce, ctor.IsStatic);
                //var fields = ctor.GetDeclaringTypeDefinition().GetFields(null, GetMemberOptions.IgnoreInheritedMembers).Where(t => t.IsStatic() == ctor.IsStatic).ToList();
                //fields = fields.Where(ShouldExportField).ToList();
                //fields.AddRange(GeneratePropertyFields(ctor.DeclaringTypeDefinition, ctor.IsStatic));

                //var props = ctor.GetDeclaringTypeDefinition().GetProperties(null, GetMemberOptions.IgnoreInheritedMembers).Where(t => t.IsStatic() == ctor.IsStatic).ToList();
                //props = props.Where(t=>Sk.IsNativeField(t)).ToList();
                //props = props.Where(t => Sk.IsJsExported(t)).ToList();
                //var fieldsAndProperties = fields.Cast<IEntity>().Concat(props.Cast<IEntity>());
                var initializers = fields.Select(fe => ExportInitializer(fe, null, isGlobal, false)).Cast<JsStatement>().ToList();
                if (initializers.Contains(null))
                    Log.Warn("Some field initializers were not exported");
                statements.AddRange(initializers.Where(t => t != null));
            }

            if (!ctor.IsStatic)
            {
                //base/this ctor invocation
                var invocation = GetConstructorBaseOrThisInvocation2(ctor);
                if (invocation != null)
                {
                    var baseThisCe = invocation.Member.DeclaringType;
                    var isBaseClr = Sk.IsClrType(baseThisCe.GetDefinition());
                    var isBasePrototype = Sk.IsNativeType(baseThisCe.GetDefinition()) && !Sk.IsJsonMode(baseThisCe.GetDefinition()) && !Sk.IsGlobalType(baseThisCe.GetDefinition());//happens when prototype inherits from json
                    if (isBaseClr == isClr && isBasePrototype == isPrototype) //base and derived are both prototype, or both are clr
                    {
                        var newObjExp2 = AstNodeConverter.VisitExpression(invocation);
                        JsInvocationExpression invocation2;
                        if (newObjExp2 is JsNewObjectExpression)
                        {
                            var newObjExp = (JsNewObjectExpression)newObjExp2;
                            invocation2 = newObjExp.Invocation;
                        }
                        else if (newObjExp2 is JsInvocationExpression)
                        {
                            invocation2 = (JsInvocationExpression)newObjExp2;
                        }
                        else
                        {
                            throw new Exception("Unexpected node: " + newObjExp2);
                        }
                        if (Sk.IsExtJsType(ce))
                        {
                            var invocation3 = Js.This().Member("callParent").Invoke();
                            if (invocation2.Arguments.IsNotNullOrEmpty())
                                invocation3.Arguments = new List<JsExpression> { Js.NewJsonArray(invocation2.Arguments.NotNull().ToArray()) };
                            statements.Add(invocation3.Statement());
                        }
                        else
                        {
                            JsRefactorer.ToCallWithContext(invocation2, new JsThis());
                            statements.Add(invocation2.Statement());
                        }
                    }
                }
            }
            if (block2.Statements == null)
                block2.Statements = new List<JsStatement>();
            block2.Statements.InsertRange(0, statements);

            return block2;
        }
        protected JsFunction getCachedHtmlFileFunction(string arrayName)
        {
            JsFunction result = null;
            JsBlock resultsBlock = null;

            if ( arrayName != null )
            {
                JsIfStatement ifArrayNotNull = new JsIfStatement();
                string arrayKeyStr = arrayName + "[ " + FILE_NAME_PARAMETER + " ]";
                ifArrayNotNull.Condition = AstUtils.getJsBinaryExpression(AstUtils.getNewMemberExpression(arrayKeyStr), "!=", new JsNullExpression());

                JsBlock ifBlock = new JsBlock();
                ifBlock.Statements = new List<JsStatement>();
                ifArrayNotNull.IfStatement = ifBlock;
                ifBlock.Statements.Add(AstUtils.getJsReturnStatement(arrayKeyStr));

                // function contents
                resultsBlock = new JsBlock();
                resultsBlock.Statements = new List<JsStatement>();
                resultsBlock.Statements.Add(ifArrayNotNull);
                resultsBlock.Statements.Add(new JsReturnStatement());
            }

            if (resultsBlock != null)
            {
                result = new JsFunction();
                result.Parameters = new List<string>();
                result.Parameters.Add(FILE_NAME_PARAMETER);
                result.Block = resultsBlock;
            }

            return result;
        }
        protected JsFunction getHtmlFileListFunction(List<string> fileList)
        {
            JsFunction result = null;
            JsBlock resultsBlock = null;

            if (fileList != null && fileList.Count > 0)
            {
                // function contents
                resultsBlock = new JsBlock();
                resultsBlock.Statements = new List<JsStatement>();
                resultsBlock.Statements.Add(AstUtils.getJsVariableDeclarationStatement(InjectionPointVariableConstants.SWITCH_RETURN_VARIABLE_NAME,
                                                                                       AstUtils.getEmptyArrayInvocationExpression()));

                foreach (string fileName in fileList)
                {
                    string fileNameStr = "{n:\'" + fileName + "\'}";
                    resultsBlock.Statements.Add( AstUtils.getArrayInsertStatement( InjectionPointVariableConstants.SWITCH_RETURN_VARIABLE_NAME, fileNameStr ) );
                }

                resultsBlock.Statements.Add( AstUtils.getJsReturnStatement( InjectionPointVariableConstants.SWITCH_RETURN_VARIABLE_NAME) );
            }

            if (resultsBlock != null)
            {
                result = new JsFunction();
                result.Block = resultsBlock;
            }

            return result;
        }
 protected override void _visit(JsBlock node)
 {
     if (node != null)
     {
         foreach (JsStatement statement in node.Statements)
         {
             visit(statement);
         }
     }
 }
        /***************************************************************************************************************************/
        /************************************************ For Getters and Setters **************************************************/
        /***************************************************************************************************************************/
        public static JsExpressionStatement getGetterSetterExpression(string propertyName, string returnName)
        {
            if(propertyName == null)
            {
                return null;
            }

            JsMemberExpression leftExp = getNewMemberExpression(propertyName);

            //
            JsBlock rightBlock = new JsBlock();
            rightBlock.Statements = new List<JsStatement>();
            rightBlock.Statements.Add(getJsReturnStatement(returnName));

            //
            JsFunction rightExp = new JsFunction();
            rightExp.Block = rightBlock;

            JsExpressionStatement results = new JsExpressionStatement();
            results.Expression = getJsAssignmentStatement(leftExp, null, rightExp);

            return results;
        }
        public JsFunction getClassDependencyFunction(JsUnit jsNode)
        {
            // function contents
            JsBlock resultsBlock = new JsBlock();
            resultsBlock.Statements = new List<JsStatement>();

            // Check to see if we need to call super.
            string superClassPath = null;

            // bschmidtke: 12/04/1012 - We do not want to call super at the moment, commenting out to value remains null.
            // Clean up later once this is confirmed after testing.
            // Leaving the code using superClassPath intact until we decide what route to go with this.

            //bool initArray = true;
            //foreach (IType type in cSharpDef.DirectBaseTypes)
            //{
            //    // careful, type.FullName could be all sorts of stuff, like interfaces
            //    if (type.Kind == TypeKind.Class)
            //    {
            //        initArray = GuiceUtils.shouldExcludeBasedOnNamespace(type.Namespace);
            //        if (!initArray)
            //        {
            //            superClassPath = type.FullName;
            //        }
            //    }
            //}

            JsBinaryExpression pExpression = null;
            JsInvocationExpression initializer = null;

            // If we have to call super...do it
            // in the case of properties methods and views, we will call super if a superClassPath is defined.
            if (superClassPath != null)
            {
                // define return variable and call super
                initializer = AstUtils.getStaticMethodCallInvocationExpression(OutputNameConstants.FUNCTION_GET_CLASS_DEPENDENCIES, superClassPath, null);

                // initalized the result array, by calling super
                pExpression = AstUtils.getJsBinaryExpression(
                                                                AstUtils.getNewMemberExpression(InjectionPointVariableConstants.SWITCH_RETURN_VARIABLE_NAME),
                                                                "=",
                                                                initializer
                                                                );
            }
            else
            {
                // initalized empty result array
                pExpression = AstUtils.getJsBinaryExpression( AstUtils.getNewMemberExpression( InjectionPointVariableConstants.SWITCH_RETURN_VARIABLE_NAME ),
                                                                                "=",
                                                                                AstUtils.getEmptyArrayInvocationExpression());
            }

            JsExpressionStatement pStatement = AstUtils.getJsExpressionStatement(pExpression);

            // define return variable.
            resultsBlock.Statements.Add( AstUtils.getJsVariableDeclarationStatement( InjectionPointVariableConstants.SWITCH_RETURN_VARIABLE_NAME ) );

            ClassDependencyVisitor visitor = new ClassDependencyVisitor( cSharpDef, jsEntity );
            List<string> dependencyList = visitor.dependencyList;

            if (dependencyList.Count > 0)
            {
                resultsBlock.Statements.Add( pStatement );

                List<string> dups = new List<string>();
                foreach ( string dependency in dependencyList )
                {
                    if ( !dups.Contains( dependency ) && dependency.Length > 1 )
                    {
                        dups.Add( dependency );

                        string value = "\'" + dependency + "\'";

                        JsExpressionStatement insert = AstUtils.getArrayInsertStatement( InjectionPointVariableConstants.SWITCH_RETURN_VARIABLE_NAME, value );
                        resultsBlock.Statements.Add( insert );
                    }
                }

                resultsBlock.Statements.Add( AstUtils.getJsReturnStatement( InjectionPointVariableConstants.SWITCH_RETURN_VARIABLE_NAME ) );
            }
            else
            {
                if( initializer != null )
                {
                    // if the super class path was defined, we have to no matter what make sure we call super.
                    resultsBlock.Statements.Add(pStatement);
                    resultsBlock.Statements.Add( AstUtils.getJsReturnStatement( InjectionPointVariableConstants.SWITCH_RETURN_VARIABLE_NAME ) );
                }
                else
                {
                    JsInvocationExpression initer = AstUtils.getEmptyArrayInvocationExpression();
                    resultsBlock.Statements.Add(AstUtils.getJsReturnStatement(initer));
                }
            }

            // define function
            JsFunction result = new JsFunction();
            // add the function block
            result.Block = resultsBlock;
            return result;
        }