Exemple #1
0
        /// <summary>
        /// Calls an internal function or external function.
        /// </summary>
        /// <param name="ctx">The context of the runtime.</param>
        /// <param name="fexpr">The function call expression</param>
        /// <param name="functionName">The name of the function. if not supplied, gets from the fexpr</param>
        /// <param name="pushCallStack"></param>
        /// <returns></returns>
        public static object CallFunction(Context ctx, FunctionCallExpr fexpr, string functionName, bool pushCallStack, IAstVisitor visitor)
        {
            if (string.IsNullOrEmpty(functionName))
            {
                functionName = fexpr.NameExp.ToQualifiedName();
            }

            // 1. Check if script func or extern func.
            var isScriptFunc = fexpr.SymScope.IsFunction(functionName);
            var isExternFunc = ctx.ExternalFunctions.Contains(functionName);

            // 2. If neither, this is an error scenario.
            if (!isScriptFunc && !isExternFunc)
            {
                throw ExceptionHelper.BuildRunTimeException(fexpr, "Function does not exist : '" + functionName + "'");
            }

            // 3. Push the name of the function on teh call stack
            if (pushCallStack)
            {
                ctx.State.Stack.Push(functionName, fexpr);
            }

            // 4. Call the function.
            object result = null;

            // Case 1: Custom C# function blog.create blog.*
            if (isExternFunc)
            {
                result = FunctionHelper.CallFunctionExternal(ctx, visitor, functionName, fexpr);
            }

            // Case 2: Script functions "createUser('john');"
            else
            {
                var sym           = fexpr.SymScope.GetSymbol(functionName) as SymbolFunction;
                var func          = sym.FuncExpr as FunctionExpr;
                var resolveParams = !fexpr.RetainEvaluatedParams;
                result = FunctionHelper.CallFunctionInScript(ctx, visitor, functionName, func, fexpr.ParamListExpressions,
                                                             fexpr.ParamList, resolveParams);
            }
            // 3. Finnaly pop the call stact.
            if (pushCallStack)
            {
                ctx.State.Stack.Pop();
            }

            result = CheckConvert(result);
            return(result);
        }
        /// <summary>
        /// Calls an internal function or external function.
        /// </summary>
        /// <param name="ctx">The context of the runtime.</param>
        /// <param name="fexpr">The function call expression</param>
        /// <param name="functionName">The name of the function. if not supplied, gets from the fexpr</param>
        /// <param name="pushCallStack"></param>
        /// <returns></returns>
        public static object CallFunction(Context ctx, FunctionCallExpr fexpr, string functionName, bool pushCallStack)
        {
            if (string.IsNullOrEmpty(functionName))
            {
                functionName = fexpr.NameExp.ToQualifiedName();
            }

            if (!FunctionHelper.IsInternalOrExternalFunction(ctx, functionName, null))
            {
                throw ExceptionHelper.BuildRunTimeException(fexpr, "Function does not exist : '" + functionName + "'");
            }

            // 1. Push the name of the function on teh call stack
            if (pushCallStack)
            {
                ctx.State.Stack.Push(functionName, fexpr);
            }

            // 2. Call the function.
            object result = null;

            // Case 1: Custom C# function blog.create blog.*
            if (ctx.ExternalFunctions.Contains(functionName))
            {
                result = ctx.ExternalFunctions.Call(functionName, fexpr);
            }

            // Case 2: Script functions "createUser('john');"
            else
            {
                result = FunctionHelper.CallFunctionInScript(ctx, functionName, fexpr.ParamListExpressions, fexpr.ParamList, true);
            }

            // 3. Finnaly pop the call stact.
            if (pushCallStack)
            {
                ctx.State.Stack.Pop();
            }

            result = CheckConvert(result);
            return(result);
        }
Exemple #3
0
        /// <summary>
        /// Call a fluent script function from c#.
        /// </summary>
        /// <param name="context">The context of the call.</param>
        /// <param name="expr">The lambda function</param>
        /// <param name="convertApplicableTypes">Whether or not to convert applicable c# types to fluentscript types, eg. ints and longs to double, List(object) to LArrayType and Dictionary(string, object) to LMapType</param>
        /// <param name="args"></param>
        public static object CallFunctionViaCSharpUsingLambda(Context context, FunctionExpr expr, bool convertApplicableTypes, params object[] args)
        {
            var argsList = args.ToList <object>();

            if (convertApplicableTypes)
            {
                LangTypeHelper.ConvertToLangTypeValues(argsList);
            }
            var execution = new Execution();

            execution.Ctx = context;
            if (EvalHelper.Ctx == null)
            {
                EvalHelper.Ctx = context;
            }

            var result = FunctionHelper.CallFunctionInScript(context, execution, expr.Meta.Name, expr, null, argsList, false);

            return(result);
        }