Ejemplo n.º 1
0
        public void Process()
        {
            AfterFunction = BeforeFunction;
            SetParents(BeforeFunction.Block);
            foreach (var me in BeforeFunction.Block.Descendants<JsMemberExpression>().ToList())
            {
                if (me.PreviousMember == null && me.NodeType == JsNodeType.MemberExpression)
                    me.PreviousMember = Js.This();
            }

            BeginNewStep();
            ProcessStatement(BeforeFunction.Block);
            BeforeFunction.Block.Statements.Clear();

            var func = new JsFunction { Block = new JsBlock { Statements = new List<JsStatement>() } };
            var i = 0;
            func.Block.Statements.Add(Js.Var("result").Statement());
            var stSwitch = Js.Switch(_state());
            var lastStep = Js.Block().Add(_state().Assign(Js.Value(Steps.Count)).Statement()).Add(new JsBreakStatement());
            Steps.Add(new YieldStep { Statements = { lastStep } });
            foreach (var step in Steps)
            {
                stSwitch.Case(Js.Value(i), step.Statements);
                i++;
            }
            func.Block.Statements.Add(stSwitch);
            func.Block.Statements.Add(Js.Member("result").Assign(Js.Value(false)).Statement());
            func.Block.Statements.Add(Js.Return(Js.Member("result")));

            BeforeFunction.Block.Statements.Add(Js.Return(Js.New(Js.Member("CustomEnumerable"), func)));
            return;
        }
Ejemplo n.º 2
0
 public static JsBlock Block(this JsFunction func)
 {
     if (func.Block == null)
     {
         func.Block = new JsBlock();
     }
     return(func.Block);
 }
Ejemplo n.º 3
0
 public static JsFunction Add(this JsFunction func, JsStatement st)
 {
     if (func.Block == null)
     {
         func.Block = new JsBlock();
     }
     if (func.Block.Statements == null)
     {
         func.Block.Statements = new List <JsStatement>();
     }
     func.Block.Statements.Add(st);
     return(func);
 }
Ejemplo n.º 4
0
 public static JsFunction AddStatements(this JsFunction func, params JsStatement[] sts)
 {
     if (sts.IsNullOrEmpty())
     {
         return(func);
     }
     if (func.Block == null)
     {
         func.Block = new JsBlock();
     }
     if (func.Block.Statements == null)
     {
         func.Block.Statements = new List <JsStatement>();
     }
     func.Block.Statements.AddRange(sts);
     return(func);
 }
Ejemplo n.º 5
0
        public override JsNode _VisitDelegate(ITypeDefinition ce)
        {
            var CurrentType = new JsClrType { Kind = JsClrTypeKind.Delegate };
            CurrentType.fullname = GetJsTypeName(ce);

            //Generate constructor
            var genericParams = new List<ITypeParameter>(ce.GetGenericArguments());
            var func = new JsFunction();
            func.Parameters = genericParams.Select(t => t.Name).ToList();
            func.Parameters.Add("obj");
            func.Parameters.Add("func");
            func.Block = Js.Block();
            foreach (var ga in genericParams)
            {
                func.Block.Add(Js.This().Member(ga.Name).Assign(Js.Member(ga.Name)).Statement());
            }
            func.Block.Add(Js.Members("System.MulticastDelegate.ctor.call").Invoke(Js.This(), Js.Member("obj"), Js.Member("func")).Statement());
            CurrentType.GetDefinition(false)["ctor"] = func;
            return OnAfterExportType(ce, CurrentType);
            //return func;

            //FullName',{ ShortName:function(T1,T2,T3,...,obj,func){this.T1=T1;....;this.construct(obj,func);},   })");
        }
Ejemplo n.º 6
0
        void _Visit(JsFunction node)
        {
            Keyword(node.IsCoroutine ? "function*" : "function");
            if (node.Name.IsNotNullOrEmpty())
            {
                Write(" ", JsTokenType.Whitespace);
                Literal(node.Name);
            }
            Control("(");

            bool first = true;

            foreach (var param in node.Parameters.NotNull())
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    Comma();
                }

                Literal(param);
            }

            Control(")");
            if (node.Block == null)
            {
                BeginBlock();
                EndBlock();
            }
            else
            {
                Visit(node.Block);
            }
        }
Ejemplo n.º 7
0
 protected JsFunction ApplyYield(JsFunction func)
 {
     if (AstNodeConverter.SupportClrYield && func.Block.Descendants().OfType<JsYieldStatement>().FirstOrDefault() != null)
     {
         var yielder = new YieldRefactorer { BeforeFunction = func };
         yielder.Process();
         return yielder.AfterFunction;
     }
     return func;
 }
Ejemplo n.º 8
0
 public virtual JsNode ExportMethod(IMethod me)
 {
     var jma = Sk.GetJsMethodAttribute(me);
     if (jma != null && jma.GlobalCode)
     {
         var block = ExportMethodBody(me);
         return new JsUnit { Statements = block.Statements };
     }
     var func = new JsFunction();
     func.Parameters = ExportMethodParameters(me);
     func.Name = SkJs.GetEntityJsName(me);
     func.Block = ExportMethodBody(me);
     func = ApplyYield(func);
     return func;
 }
Ejemplo n.º 9
0
 void ExportConstructorParameters(IMethod ctor, JsFunction func)
 {
     var ce = ctor.GetDeclaringTypeDefinition();
     var list = new List<string>();
     if (!Sk.IgnoreTypeArguments(ce))
     {
         //danel
         var gprms = ce.TypeParameters.ToList();//.GetGenericArguments().Where(ga => ga.isGenericParam()).ToList();
         if (gprms.IsNotNullOrEmpty())
         {
             var i = 0;
             foreach (var gprm in gprms)
             {
                 func.Parameters.Add(gprm.Name);
                 if (!ctor.IsStatic && func.Block != null)
                 {
                     func.Block.Statements.Insert(i, Js.This().Member(gprm.Name).Assign(Js.Member(gprm.Name)).Statement());
                     i++;
                 }
             }
         }
     }
     var prms = ctor.Parameters;
     if (prms != null)
     {
         func.Parameters.AddRange(prms.Select(t => t.Name));
     }
 }
Ejemplo n.º 10
0
        public virtual JsNode ExportConstructor(IMethod ctor)
        {
            var ctorName = SkJs.GetEntityJsName(ctor);
            var func = new JsFunction { Parameters = new List<string>() };

            func.Block = ExportConstructorBody(ctor);
            ExportConstructorParameters(ctor, func);
            return func;
        }
        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;
        }
        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(JsFunction node)
 {
     if (node != null)
     {
         visit(node.Block);
     }
 }
        /***************************************************************************************************************************/
        /************************************************ 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;
        }
 protected virtual void _visit( JsFunction node )
 {
     throw new NotImplementedException( "JsFunction" );
 }
        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;
        }