Example #1
0
 void _Visit(JsInvocationExpression node)
 {
     Visit(node.Member);
     if (!node.OmitParanthesis)
     {
         Control("(");
     }
     else if (node.Arguments.IsNotNullOrEmpty() && node.ArgumentsPrefix == null)
     {
         Space();
     }
     Write(node.ArgumentsPrefix, JsTokenType.Unknown);
     if (node.Arguments != null)
     {
         if (node.OmitCommas)
         {
             VisitEach(node.Arguments);
         }
         else
         {
             VisitEachJoin(node.Arguments, Comma);
         }
     }
     Write(node.ArgumentsSuffix, JsTokenType.Unknown);
     if (!node.OmitParanthesis)
     {
         Control(")");
     }
 }
Example #2
0
 /// <summary>
 /// MyFunction(prm1, prm2) -> MyFunction.call(context, prm1, prm2)
 /// </summary>
 /// <param name="node"></param>
 /// <param name="context"></param>
 /// <returns></returns>
 public static void ToCallWithContext(JsInvocationExpression node, JsExpression context)
 {
     node.Member = node.Member.Member("call");
     if (node.Arguments == null)
     {
         node.Arguments = new List <JsExpression>();
     }
     node.Arguments.Insert(0, Js.This());
 }
Example #3
0
 public static JsInvocationExpression InsertArgument(this JsInvocationExpression exp, int index, JsExpression arg)
 {
     if (exp.Arguments == null)
     {
         exp.Arguments = new List <JsExpression> {
             arg
         }
     }
     ;
     else
     {
         exp.Arguments.Insert(index, arg);
     }
     return(exp);
 }
Example #4
0
 public static JsInvocationExpression AddArgument(this JsInvocationExpression exp, JsExpression arg)
 {
     if (exp.Arguments == null)
     {
         exp.Arguments = new List <JsExpression> {
             arg
         }
     }
     ;
     else
     {
         exp.Arguments.Add(arg);
     }
     return(exp);
 }
        public static JsExpressionStatement getArrayInsertStatement(string arrayName, string itemValue)
        {
            JsInvocationExpression constInvoke = new JsInvocationExpression();

            /********************************************************/
            // build add statement to array
            /********************************************************/
            JsMemberExpression arrayMember = AstUtils.getNewMemberExpression(arrayName);
            JsMemberExpression addMember = AstUtils.getNewMemberExpression("push", arrayMember);
            constInvoke.Member = addMember;
            constInvoke.Arguments = new List<JsExpression>();

            constInvoke.Arguments.Add(AstUtils.getJsCodeExpression(itemValue));

            JsExpressionStatement constructorStatement = AstUtils.getJsExpressionStatement(constInvoke);
            return constructorStatement;
        }
        JsSwitchSection getPropertiesSwitchSection(string caseIndex, IEnumerable<IProperty> properties, JsInvocationExpression initalizerOverride)
        {
            // if fields come in, regardless, build it.
            // if no fields, and a override, build it. (i.e., we must create the call to super even if this class does not define any fields
            if (initalizerOverride == null && properties == null)
            {
                return null;
            }

            JsSwitchSection results = AstUtils.getJsSwitchSection(caseIndex);
            results.Statements = new List<JsStatement>();

            if (initalizerOverride == null)
            {
                initalizerOverride = AstUtils.getEmptyArrayInvocationExpression();
            }

            JsBinaryExpression pExpression = AstUtils.getJsBinaryExpression(
                                                                            AstUtils.getNewMemberExpression( InjectionPointVariableConstants.SWITCH_RETURN_VARIABLE_NAME ),
                                                                            "=",
                                                                            initalizerOverride
                                                                            );

            JsExpressionStatement pDeclaration = AstUtils.getJsExpressionStatement(pExpression);
            results.Statements.Add(pDeclaration);

            if (properties != null)
            {
                foreach (IProperty property in properties)
                {
                    if( property.CanSet )
                    {
                        JsExpressionStatement propertyStatement = getMethodInjectStatement(property.Setter);
                        results.Statements.Add(propertyStatement);
                    }
                }
            }

            // add the break last...
            results.Statements.Add(AstUtils.getJsBreakStatement());

            return results;
        }
        JsSwitchSection getFieldsSwitchSection(string caseIndex, IEnumerable<IField> fields, JsInvocationExpression initalizerOverride)
        {
            // if fields come in, regardless, build it.
            // if no fields, and a override, build it. (i.e., we must create the call to super even if this class does not define any fields
            if (initalizerOverride == null && fields == null)
            {
                return null;
            }

            JsSwitchSection results = AstUtils.getJsSwitchSection(caseIndex);
            results.Statements = new List<JsStatement>();

            if (initalizerOverride == null)
            {
                initalizerOverride = AstUtils.getEmptyArrayInvocationExpression();
            }

            JsBinaryExpression pExpression = AstUtils.getJsBinaryExpression(AstUtils.getNewMemberExpression( InjectionPointVariableConstants.SWITCH_RETURN_VARIABLE_NAME ),
                                                                            "=",
                                                                            initalizerOverride);

            JsExpressionStatement pDeclaration = AstUtils.getJsExpressionStatement(pExpression);
            results.Statements.Add(pDeclaration);

            if (fields != null)
            {
                foreach (IField field in fields)
                {
                    bool fRequired = IEntityUtils.isFieldRequired(field);
                    string fAnnotation = IEntityUtils.getInjectAnnotation(field);
                    string fDefaultValue = null;

                    if (field.ConstantValue != null)
                    {
                        // if a default value is defined, set it.
                        fDefaultValue = field.ConstantValue.ToString();
                    }

                    string codeStr = GuiceUtils.getInjectonPointString(field.Name, field.Type.FullName, field.Type.Namespace, fDefaultValue, fRequired, fAnnotation);
                    results.Statements.Add( AstUtils.getArrayInsertStatement( InjectionPointVariableConstants.SWITCH_RETURN_VARIABLE_NAME, codeStr ) );
                }
            }

            // add the break last...
            results.Statements.Add(AstUtils.getJsBreakStatement());

            return results;
        }
        protected void _visit( JsInvocationExpression node, bool include )
        {
            if ( node != null )
            {
                visit( node.Member, include );

                foreach ( JsExpression jsExp in node.Arguments )
                {
                    visit( node );
                }
            }
        }
        protected override void _visit(JsInvocationExpression node)
        {
            if (node != null)
            {
                bool found = false;
                JsExpression mExpression = node.Member;
                if ( mExpression != null && mExpression.NodeType == JsNodeType.MemberExpression )
                {
                    JsMemberExpression member = ( JsMemberExpression ) mExpression;
                    if ( member.Name == "Typeof" )
                    {
                        found = true;
                    }
                }

                // If we find a Typeof, then we need to include the args as a dependency
                if (!found)
                {
                    visit( node.Member );
                    foreach ( JsExpression arg in node.Arguments )
                    {
                        visit(arg);
                    }
                }
                else
                {
                    foreach ( JsExpression arg in node.Arguments )
                    {
                        dependencyList.Add( arg.ToJs() );
                    }
                }
            }
        }
        public JsNode VisitInvocationResolveResult(CSharpInvocationResolveResult res)
        {
            Res = res;

            ProcessMember();

            if (MethodAtt != null && MethodAtt.InlineCode != null)
                return Js.CodeExpression(MethodAtt.InlineCode);

            var conditional = ProcessConditional();
            if (conditional)
                return null;

            bool omittedCalls;
            var omittedCallsNode = ProcessOmitCalls(out omittedCalls);
            if (omittedCalls)
                return omittedCallsNode;

            JsMember = SkJs.EntityToMember(Member);
            ProcessTarget();

            ProcessPrmBindings();

            ProcessNativeParams();

            ProcessByRefs1();

            PrmBindings.ForEach(t => t.JsCallResult = VisitExpression(t.Binding.CallResult));
            Node2 = new JsInvocationExpression
            {
                Member = JsMember,
                Arguments = PrmBindings.Select(t => t.JsCallResult).ToList(),
            };

            ProcessByRefs2();

            ProcessExtensionImplementedInInstance();

            TransformIntoBaseMethodCallIfNeeded(Res, Node2);

            ProcessArgsCustomization();

            ProcessGenericMethodArgs();

            var inlineCodeExpression = ProcessInlineCodeExpression();
            if (inlineCodeExpression != null)
                return inlineCodeExpression;



            var omittedDotOperator = ProcessOmitDotOperator();
            if (omittedDotOperator != null)
                return omittedDotOperator;

            ProcessRemoveEmptyPreviousMemberName();

            var indexerAccess = ProcessIndexer();
            if (indexerAccess != null)
                return indexerAccess;

            ProcessByRefs3();

            return Node2;

        }
        void TransformIntoBaseMethodCallIfNeeded(CSharpInvocationResolveResult res, JsInvocationExpression node2)
        {
            var target = res.TargetResult as ThisResolveResult;
            if (target != null && target.CausesNonVirtualInvocation) //base.
            {
                //var info = res.GetInfo();
                //var node = info.Nodes.FirstOrDefault();
                var ce = target.Type;// node.FindThisEntity();
                if (ce != null && Sk.IsExtJsType(ce.GetDefinitionOrArrayType()))
                {
                    node2.Member = Js.This().Member("callParent");
                    if (node2.Arguments.IsNotNullOrEmpty())
                        node2.Arguments = new List<JsExpression> { Js.NewJsonArray(node2.Arguments.ToArray()) };
                    //var me2 = (node2.Member as JsMemberExpression);
                    //me2.Name = "callParent";
                    return;

                }
                IMethod me2;
                var me = res.Member;
                if (me is IProperty)
                    me2 = ((IProperty)me).Getter;
                else if (me is IMethod)
                    me2 = (IMethod)res.Member;
                else
                    throw new Exception("Can't resolve method from member: " + res.Member);
                var member = SkJs.EntityMethodToJsFunctionRef(me2);
                member = member.Member("call");
                node2.Member = member;
                if (node2.Arguments == null)
                    node2.Arguments = new List<JsExpression>();
                node2.Arguments.Insert(0, Js.This());

            }
        }
        // need a better name...
        // example: randori.apples.SuperClass.injectionPoints(t);
        public static JsInvocationExpression getStaticMethodCallInvocationExpression(string methodName, string classPath, List<JsExpression> args)
        {
            //randori.apples.SuperClass
            JsMemberExpression classPathExpr = AstUtils.getNewMemberExpression(classPath);

            //methodName
            JsInvocationExpression initer = new JsInvocationExpression();
            initer.Member =  AstUtils.getNewMemberExpression(methodName, classPathExpr);

            // Method Argumenets
            if (args != null)
            {
                initer.Arguments = args;
            }

            return initer;
        }
        public static JsVariableDeclarator getJsVariableDeclarator(string variableName, JsInvocationExpression initer = null)
        {
            JsVariableDeclarator result = new JsVariableDeclarator();
            result.Name = variableName;

            // should the variable be initialized?
            if (initer != null)
                result.Initializer = initer;

            return result;
        }
        public static JsVariableDeclarationStatement getJsVariableDeclarationStatement(string variableName, JsInvocationExpression initer = null)
        {
            // declare function return object
            JsVariableDeclarationStatement pResult = new JsVariableDeclarationStatement();
            JsVariableDeclarationExpression pExpression = new JsVariableDeclarationExpression();
            pResult.Declaration = pExpression;

            // add variable declarations
            pExpression.Declarators = new List<JsVariableDeclarator>();
            pExpression.Declarators.Add(getJsVariableDeclarator(variableName, initer));
            return pResult;
        }
        /***************************************************************************************************************************/
        /**************************************** Array Statement Utilities *******************************************************/
        /***************************************************************************************************************************/
        // example: []
        public static JsInvocationExpression getEmptyArrayInvocationExpression()
        {
            JsInvocationExpression initer = new JsInvocationExpression();
            initer.Arguments = new List<JsExpression>();
            initer.Member = new JsExpression();
            initer.OmitParanthesis = true;

            JsJsonArrayExpression args = new JsJsonArrayExpression();
            args.Items = new List<JsExpression>();
            initer.Arguments.Add(args);

            return initer;
        }
        void ProcessByRefs3()
        {
            if (ByRefs.IsNotNullOrEmpty())
            {
                var func = Js.Function();

                //It must assigned to a temporary variable, because typed arrays do not acceppt json.
                for (var i = 0; i < ByRefs.Count; i++)
                {
                    var byRef = ByRefs[i];
                    func.Add(Js.Var(RefIndexToName(i), Js.Json().Add("Value", VisitExpression(byRef))).Statement());
                }

                func.Add(Js.Var("$res", Node2).Statement());

                for (var i = 0; i < ByRefs.Count; i++)
                {
                    var byRef = ByRefs[i];
                    func.Add(Js.Assign(VisitExpression(byRef), Js.Member(Js.Member(RefIndexToName(i)), "Value")).Statement());
                }

                func.Add(Js.Return(Js.Member("$res")));
                Node2 = Importer.WrapFunctionAndInvoke(Res, func);
            }
        }
 protected virtual void _visit( JsInvocationExpression node )
 {
     throw new NotImplementedException( "JsInvocationExpression" );
 }
Example #18
0
 /// <summary>
 /// MyFunction(prm1, prm2) -> MyFunction.call(context, prm1, prm2)
 /// </summary>
 /// <param name="node"></param>
 /// <param name="context"></param>
 /// <returns></returns>
 public static void ToCallWithContext(JsInvocationExpression node, JsExpression context)
 {
     node.Member = node.Member.Member("call");
     if (node.Arguments == null)
         node.Arguments = new List<JsExpression>();
     node.Arguments.Insert(0, Js.This());
 }