Beispiel #1
0
 /// <summary>
 /// Internal function to hide looking at all function objects.
 /// </summary>
 /// <param name="c"></param>
 /// <param name="arguments"></param>
 /// <param name="functionName"></param>
 /// <returns></returns>
 private Func <object> FindFunction(IScopeContext c, IEnumerable <IExpression> arguments, string functionName)
 {
     return(ExtensibilityControl.Get().FunctionFinders
            .Select(ff => ff.FindFunction(c, arguments, functionName))
            .Where(f => f != null)
            .FirstOrDefault());
 }
        public void MakeSureInitIsCalled()
        {
            MyFunc.WasCalled = false;
            var rootContext = new RootContext();

            ExtensibilityControl.Get().InitializeFunctionObjects(rootContext);
            Assert.IsTrue(MyFunc.WasCalled, "Initialize method was not called");
        }
        /// <summary>
        /// Returns a list of all functions we can find that have the proper name.
        /// </summary>
        /// <param name="FunctionName"></param>
        /// <returns></returns>
        private static IEnumerable <MethodInfo> ListOfFuncsWithName(string fname)
        {
            var funcs = (from fo in ExtensibilityControl.Get().FunctionObjects
                         from m in fo.GetType().GetMethods(BindingFlags.Public | BindingFlags.Static)
                         where m.Name == fname
                         select m).ToArray();

            return(funcs);
        }
        /// <summary>
        /// Find a method in the code, Account for use of C# reserved words by added "Reserved" onto the end of the method
        /// we are trying to find.
        /// </summary>
        /// <param name="methodName"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        /// <remarks>Default arguments make this a little more difficult to deal with</remarks>
        private static MethodInfo[] FindMethodForArgs(string methodName, object[] args)
        {
            // Look for all functions that match the name we want.
            var    argListTypes    = args.Select(a => a.GetType()).ToArray();
            string methodNameFixed = methodName.FixUpReserved();

            var funcs = (from fo in ExtensibilityControl.Get().FunctionObjects
                         from m in fo.GetType().GetMethods()
                         where m.Name == methodNameFixed
                         where m.IsStatic
                         let newm = m.ArgumentListMatches(argListTypes, fo.GetType())
                                    where newm != null
                                    select newm).Distinct().ToArray();

            return(funcs);
        }
        /// <summary>
        /// Evaluate the method call. Use a list of evaluators to try to accomplish the call.
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public object Evaluate(IScopeContext c)
        {
            // All functions and the source object must evaluate correctly. Further, since we want to do the
            // evaluate only once, we do it here, at the top.
            var obj  = ObjectExpression.Evaluate(c);
            var args = FunctionCall.Arguments.Select(a => a.Evaluate(c)).ToArray();

            // Find the first evaluator that can figure out what this is.
            var goodEval = ExtensibilityControl.Get().MethodEvaluators
                           .Select(e => e.Evaluate(c, obj, FunctionCall.FunctionName, args)).Where(r => r.Item1).FirstOrDefault();

            if (goodEval != null)
            {
                return(c.ExecutionContext.ExecutePostCallHook(FunctionCall.FunctionName, obj, goodEval.Item2));
            }
            throw new InvalidOperationException(string.Format("Don't know how to call the function {0} on the object {1} of type {2}.", FunctionCall.ToString(), ObjectExpression.ToString(), obj.GetType().Name));
        }