public Writer(int fileDescriptor, CapturedOutputWriter monitor)
 {
     this.fileDescriptor = fileDescriptor;
     this.monitor        = monitor;
     this.codeCellId     = monitor.codeCellId;
 }
Ejemplo n.º 2
0
        public async Task RunAsync(
            Compilation compilation,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            foreach (var assembly in new AssemblyDefinition [] { compilation.ExecutableAssembly }
                     .Concat(compilation.References ?? new AssemblyDefinition [] { }))
            {
                agent.LoadExternalDependencies(null, assembly?.ExternalDependencies);
            }

            Exception evaluationException = null;
            var       result = new Evaluation {
                RequestId  = compilation.MessageId,
                CodeCellId = compilation.CodeCellId,
                Phase      = EvaluationPhase.Compiled
            };

            if (compilation?.ExecutableAssembly?.Content.PEImage != null)
            {
                result.Compilation = compilation;
            }

            loadedAssemblies = new List <AssemblyDefinition> ();

            var savedStdout = Console.Out;
            var savedStderr = Console.Error;

            var capturedOutputWriter = new CapturedOutputWriter(compilation.CodeCellId);

            Console.SetOut(capturedOutputWriter.StandardOutput);
            Console.SetError(capturedOutputWriter.StandardError);
            capturedOutputWriter.SegmentCaptured += CapturedOutputWriter_SegmentCaptured;

            var stopwatch = new Stopwatch();

            currentRunThread = Thread.CurrentThread;
            currentRunThread.CurrentCulture   = InteractiveCulture.CurrentCulture;
            currentRunThread.CurrentUICulture = InteractiveCulture.CurrentUICulture;

            initializedAgentIntegration = false;

            try {
                // We _only_ want to capture exceptions from user-code here but
                // allow our own bugs to propagate out to the normal exception
                // handling. The only reason we capture these exceptions is in
                // case anything has been written to stdout/stderr by user-code
                // before the exception was raised... we still want to send that
                // captured output back to the client for rendering.
                stopwatch.Start();
                result.Result = await CoreRunAsync(compilation, cancellationToken);
            } catch (AggregateException e) when(e.InnerExceptions?.Count == 1)
            {
                evaluationException = e;
                // the Roslyn-emitted script delegates are async, so all exceptions
                // raised within a delegate should be AggregateException; if there's
                // just one inner exception, unwind it since the async nature of the
                // script delegates is a REPL implementation detail.
                result.Exception = ExceptionNode.Create(e.InnerExceptions [0]);
            } catch (ThreadAbortException e) {
                evaluationException = e;
                result.Interrupted  = true;
                Thread.ResetAbort();
            } catch (ThreadInterruptedException e) {
                evaluationException = e;
                result.Interrupted  = true;
            } catch (Exception e) {
                evaluationException = e;
                result.Exception    = ExceptionNode.Create(e);
            } finally {
                stopwatch.Stop();
                currentRunThread = null;

                result.Phase = EvaluationPhase.Evaluated;
                evaluations.Observers.OnNext(result);

                capturedOutputWriter.SegmentCaptured -= CapturedOutputWriter_SegmentCaptured;
                Console.SetOut(savedStdout);
                Console.SetError(savedStderr);
            }

            result.EvaluationDuration = stopwatch.Elapsed;

            // an exception in PrepareResult should not be explicitly caught
            // here (see above) since it'll be handled at a higher level and can be
            // flagged as being a bug in our code since this method should never throw.
            result.Result = agent.RepresentationManager.Prepare(result.Result);

            result.InitializedAgentIntegration = initializedAgentIntegration;
            result.LoadedAssemblies            = loadedAssemblies.ToArray();

            result.CultureLCID   = InteractiveCulture.CurrentCulture.LCID;
            result.UICultureLCID = InteractiveCulture.CurrentUICulture.LCID;

            result.Phase = EvaluationPhase.Represented;
            evaluations.Observers.OnNext(result);

            if (evaluationException != null)
            {
                evaluations.Observers.OnError(evaluationException);
            }

            agent.PublishEvaluation(result);

            result.Phase = EvaluationPhase.Completed;
            evaluations.Observers.OnNext(result);
        }
Ejemplo n.º 3
0
 public Writer(int fileDescriptor, CapturedOutputWriter monitor)
 {
     this.fileDescriptor = fileDescriptor;
     this.monitor        = monitor;
     this.context        = monitor.context;
 }