示例#1
0
        /// <summary>
        /// Starts the interpreter in debug mode. The program will be interpreted and support the beakpoint and step by step debugging.
        /// </summary>
        /// <param name="callback">The cross-AppDomain task proxy.</param>
        /// <param name="verbose">Defines if the verbose mode must be enabled or not.</param>
        /// <param name="args">The arguments to pass to the entry point.</param>
        /// <returns>Returns an awaitable task that can wait the end of the program execution</returns>
        internal void StartDebug(MarshaledResultSetter callback, bool verbose, params object[] args)
        {
            Requires.NotNull(_middleware, nameof(_middleware));
            if (_releaseModeForced)
            {
                throw new UnexpectedException(new Exception(L.BaZic.Runtime.BaZicInterpreter.CannotStartDebugAfterBuild));
            }

            _forceStop = false;
            DebugMode  = true;

            var action = new Action(() =>
            {
                LoadAssemblies();

                if (State == BaZicInterpreterState.Preparing)
                {
                    if (ProgramInterpreter == null)
                    {
                        var executionFlowId = RunningStateManager.AddCallStackForUnwaitedMethodInvocation();
                        RunningStateManager.SetIsRunningMainFunction(true);

                        ProgramInterpreter = new ProgramInterpreter(this, Program, executionFlowId);
                    }

                    ProgramInterpreter.Start(_programArguments);

                    RunningStateManager.WaitUnwaitedMethodInvocation(ProgramInterpreter.ExecutionFlowId);

                    if (State == BaZicInterpreterState.Running || State == BaZicInterpreterState.Idle)
                    {
                        ProgramResult = ProgramInterpreter.ProgramResult;
                        FreePauseModeWaiter();
                    }
                }
            });

            callback.ContinueWith(() =>
            {
                RunningStateManager.SetIsRunningMainFunction(false);
                RunningStateManager.UpdateState();
            }, null);

            Start(callback, action, verbose, args);
        }
        /// <summary>
        /// Start the interpreter
        /// </summary>
        /// <param name="debugMode">Defines if the debug mode must be enabled or not</param>
        /// <returns>Returns an awaitable task that can wait the end of the program execution</returns>
        private Task RunAsync(bool debugMode)
        {
            InDebugMode = debugMode;
            return Task.Run(() =>
            {
                RuntimeHelpers.EnsureSufficientExecutionStack();
                GCSettings.LatencyMode = GCLatencyMode.LowLatency;

                ProgramInterpreter = new ProgramInterpreter(Program, debugMode);

                ProgramInterpreter.StateChanged += ChangeState;
                ProgramInterpreter.Start();

                Stop(false);

                ProgramInterpreter.StateChanged -= ChangeState;
                ProgramInterpreter.Dispose();

                GCSettings.LatencyMode = GCLatencyMode.Interactive;
                GC.Collect();
                GC.WaitForPendingFinalizers();
            });
        }