Example #1
0
        public override object Execute(Func <object>[] args)
        {
            var func = (CustomFunction)args[0]();

            try
            {
                var callArgsObj = args[1]();
                if (!(callArgsObj is object[] callArgs))
                {
                    throw new InvalidOperationException(
                              $"Malformed called to '{func.Name}'.  Expected a list of arguments but received '{callArgsObj}'");
                }

                if (callArgs.Length != func.Parameters.Length)
                {
                    throw new InvalidOperationException(
                              $"'{func.Name}' expects {func.Parameters.Length} arguments but is being called with {callArgs.Length}");
                }

                InitialiseLocals(func.Parameters, callArgs);

                func.Execute();

                object result = null;
                if (ScopeManager.Contains(ReturnFunction.ReturnVarName))
                {
                    result = ScopeManager.Get(ReturnFunction.ReturnVarName);
                }

                ScopeManager.LeaveScope();

                return(result);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException($"Error executing function '{func.Name}': {ex.Message}");
            }
        }
 public static Task <ScopeDescription> GetAsync(this ScopeManager scopeManager)
 {
     return(Task.Factory.StartNew(() => scopeManager.Get()));
 }