コード例 #1
0
        /// <summary>
        /// Starts the interpreter in release mode. The program will be compiled (emitted) and run quickly. Breakpoint statements will be ignored.
        /// </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 StartRelease(MarshaledResultSetter callback, bool verbose, params object[] args)
        {
            Requires.NotNull(_middleware, nameof(_middleware));
            _releaseModeForced = true;
            _forceStop         = false;
            DebugMode          = false;

            var action = new Action(() =>
            {
                if (_releaseModeRuntime == null || _compilerResult == null || _compilerResult.BuildErrors != null)
                {
                    if (State == BaZicInterpreterState.Preparing)
                    {
                        _compilerResult = Build(BaZicCompilerOutputType.DynamicallyLinkedLibrary, string.Empty, string.Empty, string.Empty, false);

                        if (_compilerResult.BuildErrors != null)
                        {
                            ChangeState(this, new UnexpectedException(_compilerResult.BuildErrors));
                        }
                        else
                        {
                            _assemblySandbox.LoadAssembly(_compilerResult.Assembly, false);
                            _compilerResult.Dispose();
                        }
                    }
                }

                if (State == BaZicInterpreterState.Preparing)
                {
                    RunningStateManager.SetIsRunningMainFunction(true);
                    _releaseModeRuntime.Run(_programArguments);

                    if (State == BaZicInterpreterState.Running || State == BaZicInterpreterState.Idle)
                    {
                        ProgramResult = _releaseModeRuntime.ProgramResult;
                        RunningStateManager.UpdateState();
                    }
                }
            });

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

            Start(callback, action, verbose, args);
        }
コード例 #2
0
        /// <summary>
        /// Compiles the program in memory and load it (to use it later) or returns the build errors.
        /// </summary>
        /// <param name="callback">The cross-AppDomain task proxy.</param>
        /// <returns>Returns the build errors, or null if it succeed.</returns>
        internal void Build(MarshaledResultSetter <AggregateException> callback)
        {
            Requires.NotNull(_middleware, nameof(_middleware));
            if (DebugMode)
            {
                throw new UnexpectedException(new Exception(L.BaZic.Runtime.BaZicInterpreter.CannotBuildAfterStartDebug));
            }
            CheckState(BaZicInterpreterState.Ready, BaZicInterpreterState.Stopped, BaZicInterpreterState.StoppedWithError);

            if (ProgramIsOptimized)
            {
                throw new InvalidOperationException(L.BaZic.Runtime.BaZicInterpreter.CannotRunOptimizedProgramInRelease);
            }

            Error = null;
            ChangeState(this, new BaZicInterpreterStateChangeEventArgs(BaZicInterpreterState.Running));

            AggregateException buildErrors = null;
            var currentCulture             = LocalizationHelper.GetCurrentCulture();

            _mainInterpreterTask = Task.Run(() =>
            {
                LocalizationHelper.SetCurrentCulture(currentCulture, false, false);

                _compilerResult = Build(BaZicCompilerOutputType.DynamicallyLinkedLibrary, string.Empty, string.Empty, string.Empty, false);

                if (_compilerResult.BuildErrors != null)
                {
                    buildErrors = _compilerResult.BuildErrors;
                    ChangeState(this, new UnexpectedException(_compilerResult.BuildErrors));
                }
                else
                {
                    _assemblySandbox.LoadAssembly(_compilerResult.Assembly, false);
                    _compilerResult.Dispose();
                    _releaseModeForced = true;
                    ChangeState(this, new BaZicInterpreterStateChangeEventArgs(BaZicInterpreterState.Stopped));
                }
            });
            _mainInterpreterTask.ContinueWith((task) =>
            {
                callback.SetResult(buildErrors);
            });
        }