コード例 #1
0
        /// <summary>
        /// Call the constructor of the class, if exists.
        /// </summary>
        /// <param name="arguments">the argument values</param>
        internal void CreateNewInstanceCallConstructors(Collection <object> arguments)
        {
            if (arguments == null)
            {
                arguments = new Collection <object>();
            }

            var constructor = FindCorrespondingConstructor(arguments.Count);

            if (constructor == null)
            {
                if (Constructors.Count > 0)
                {
                    ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new MethodNotFoundException("ctor", $"There is no constructor with {arguments.Count} argument(s) in the class '{ClassDeclaration.Name}'.")), GetDebugInfo()));
                }
            }
            else
            {
                if (DebugMode)
                {
                    Log(this, $"Calling a constructor of '{ClassDeclaration.Name}'");
                }
                constructor = new MethodInterpreter(constructor.MethodDeclaration, DebugMode);
                constructor.StateChanged           += ChangeState;
                constructor.OnGetParentInterpreter += new Func <ClassInterpreter>(() => this);
                constructor.OnDone += new Action <MethodInterpreter>((met) =>
                {
                    met.Dispose();
                    met.StateChanged -= ChangeState;
                });
                constructor.Initialize();
                constructor.Run(false, arguments, Guid.Empty);
            }
        }
コード例 #2
0
        /// <summary>
        /// Invoke the specified method
        /// </summary>
        /// <param name="callerInterpreter">The caller interpreter (usually a block)</param>
        /// <param name="invokeExpression">The invoke expression</param>
        /// <param name="argumentValues">The list of argument values</param>
        /// <param name="parentMethodInterpreter">The parent method interpret from where the invocation happened</param>
        /// <param name="callStackService">The user call stack service</param>
        /// <returns>Returns the result of the method</returns>
        internal object InvokeMethod(Interpret callerInterpreter, AlgorithmExpression invokeExpression, Collection <object> argumentValues, MethodInterpreter parentMethodInterpreter, CallStackService callStackService)
        {
            var methodName = invokeExpression._methodName.ToString();
            var method     = Methods.FirstOrDefault(m => m.MethodDeclaration._name.ToString() == methodName);

            if (method == null)
            {
                callerInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new MethodNotFoundException(methodName, $"The method '{methodName}' does not exists in the current class or is not accessible."), ClassDeclaration), callerInterpreter.GetDebugInfo()));
                return(null);
            }

            if (!method.MethodDeclaration._isAsync && invokeExpression._await)
            {
                callerInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new MethodNotAwaitableException(methodName), method.MethodDeclaration), callerInterpreter.GetDebugInfo()));
                return(null);
            }

            Guid stackTraceId;
            var  isAsync = method.MethodDeclaration._isAsync;

            if (parentMethodInterpreter == null)
            {
                stackTraceId = Guid.Empty;
            }
            else
            {
                stackTraceId = parentMethodInterpreter.StacktraceId;
                if (DebugMode)
                {
                    var callStack = callStackService.CallStacks.Single(cs => cs.TaceId == stackTraceId);
                    var call      = callStack.Stack.Pop();
                    if (call != null)
                    {
                        call.Variables = callerInterpreter.GetAllAccessibleVariable().DeepClone();
                        callStack.Stack.Push(call);
                    }
                }
            }

            method = new MethodInterpreter(method.MethodDeclaration, DebugMode);
            method.StateChanged           += ChangeState;
            method.OnGetParentInterpreter += new Func <ClassInterpreter>(() => this);
            method.OnDone += new Action <MethodInterpreter>((met) =>
            {
                met.Dispose();
                met.StateChanged -= ChangeState;
            });
            method.Initialize();
            method.Run(invokeExpression._await, argumentValues, stackTraceId);

            if (isAsync && !invokeExpression._await)
            {
                return(null);
            }
            return(method.ReturnedValue);
        }