Example #1
0
        /// <summary>
        /// Calls the custom function.
        /// </summary>
        /// <param name="name">Name of the function</param>
        /// <param name="exp"></param>
        /// <returns></returns>
        public static object CallFunctionExternal(Context ctx, IAstVisitor visitor, string name, FunctionCallExpr exp)
        {
            var externalFuncs = ctx.ExternalFunctions;
            var objectName    = name;
            var method        = string.Empty;
            Func <string, string, FunctionCallExpr, object> callback = null;

            // Contains callback for full function name ? e.g. CreateUser
            if (externalFuncs.Contains(name))
            {
                callback = externalFuncs.GetByName(name);
            }

            // Contains callback that handles multiple methods on a "object".
            // e.g. Blog.Create, Blog.Delete etc.
            if (name.Contains("."))
            {
                var ndxDot = name.IndexOf(".");
                objectName = name.Substring(0, ndxDot);
                method     = name.Substring(ndxDot + 1);
                if (externalFuncs.Contains(objectName + ".*"))
                {
                    callback = externalFuncs.GetByName(objectName + ".*");
                }
            }

            if (callback == null)
            {
                return(LObjects.Null);
            }

            // 1. Resolve parameter froms expressions into Lang values.
            ParamHelper.ResolveParametersToHostLangValues(exp.ParamListExpressions, exp.ParamList, visitor);
            var result = callback(objectName, method, exp);

            return(result);
        }
Example #2
0
        /// <summary>
        /// Dynamically invokes a method call.
        /// </summary>
        /// <param name="ctx">Context of the script</param>
        /// <param name="obj">Instance of the object for which the method call is being applied.</param>
        /// <param name="datatype">The datatype of the object.</param>
        /// <param name="methodInfo">The method to call.</param>
        /// <param name="paramListExpressions">List of expressions representing parameters for the method call</param>
        /// <param name="paramList">The list of values(evaluated from expressions) to call.</param>
        /// <param name="resolveParams">Whether or not to resolve the parameters from expressions to values.</param>
        /// <returns></returns>
        private static object MethodCall(Context ctx, object obj, Type datatype, MethodInfo methodInfo, List <Expr> paramListExpressions, List <object> paramList, bool resolveParams, IAstVisitor visitor)
        {
            // 1. Convert language expressions to values.
            if (resolveParams)
            {
                ParamHelper.ResolveParametersForMethodCall(methodInfo, paramListExpressions, paramList, visitor);
            }

            // 2. Convert internal language types to c# code method types.
            var args = LangTypeHelper.ConvertArgs(paramList, methodInfo);

            // 3. Handle  params object[];
            if (methodInfo.GetParameters().Length == 1)
            {
                if (methodInfo.GetParameters()[0].ParameterType == typeof(object[]))
                {
                    args = new object[] { args }
                }
                ;
            }
            var result = methodInfo.Invoke(obj, args);

            return(result);
        }