コード例 #1
0
        private async Task HandleScriptErrors(Func <Task> doWork)
        {
            try
            {
                await doWork();

                if (_scriptState?.Exception != null)
                {
                    Console.WritePrettyError(CSharpObjectFormatter.Instance.FormatException(_scriptState.Exception));
                }

                if (_scriptState?.ReturnValue != null)
                {
                    _globals.Print(_scriptState.ReturnValue);
                }
            }
            catch (CompilationErrorException e)
            {
                foreach (var diagnostic in e.Diagnostics)
                {
                    Console.WritePrettyError(diagnostic.ToString());
                }
            }
            catch (Exception e)
            {
                Console.WritePrettyError(CSharpObjectFormatter.Instance.FormatException(e));
            }
        }
コード例 #2
0
 private void DisplaySubmissionResult(ScriptState <object> state)
 {
     if (state.Script.HasReturnValue())
     {
         _globals.Print(state.ReturnValue);
     }
 }
コード例 #3
0
        protected virtual async Task Execute(string input, bool debugMode)
        {
            try
            {
                if (_scriptState == null)
                {
                    var sourceText = SourceText.From(input);
                    var context    = new ScriptContext(sourceText, CurrentDirectory, Enumerable.Empty <string>(), debugMode: debugMode);
                    await RunFirstScript(context);
                }
                else
                {
                    var lineRuntimeDependencies =
                        ScriptCompiler.RuntimeDependencyResolver.GetDependenciesFromCode(CurrentDirectory, input).ToArray();
                    var lineDependencies = lineRuntimeDependencies.SelectMany(rtd => rtd.Assemblies).Distinct();

                    var scriptMap = lineRuntimeDependencies.ToDictionary(rdt => rdt.Name, rdt => rdt.Scripts);
                    if (scriptMap.Count > 0)
                    {
                        _scriptOptions =
                            _scriptOptions.WithSourceResolver(
                                new NuGetSourceReferenceResolver(
                                    new SourceFileResolver(ImmutableArray <string> .Empty, CurrentDirectory), scriptMap));
                    }
                    foreach (var runtimeDependency in lineDependencies)
                    {
                        Logger.Verbose("Adding reference to a runtime dependency => " + runtimeDependency);
                        _scriptOptions = _scriptOptions.AddReferences(MetadataReference.CreateFromFile(runtimeDependency.Path));
                    }

                    _scriptState = await _scriptState.ContinueWithAsync(input, _scriptOptions, ex => true);
                }

                if (_scriptState?.Exception != null)
                {
                    Console.WritePrettyError(CSharpObjectFormatter.Instance.FormatException(_scriptState.Exception));
                }

                if (_scriptState?.ReturnValue != null)
                {
                    _globals.Print(_scriptState.ReturnValue);
                }
            }
            catch (Exception e)
            {
                Console.WritePrettyError(CSharpObjectFormatter.Instance.FormatException(e));
            }
        }
コード例 #4
0
            private void DisplaySubmissionResult(ScriptState <object> state)
            {
                bool hasValue;
                var  resultType = state.Script.GetCompilation().GetSubmissionResultType(out hasValue);

                if (hasValue)
                {
                    if (resultType != null && resultType.SpecialType == SpecialType.System_Void)
                    {
                        Console.Out.WriteLine(_replServiceProvider.ObjectFormatter.VoidDisplayString);
                    }
                    else
                    {
                        _globals.Print(state.ReturnValue);
                    }
                }
            }
コード例 #5
0
        protected virtual async Task Execute(string input, bool debugMode)
        {
            try
            {
                if (_scriptState == null)
                {
                    var sourceText = SourceText.From(input);
                    var context    = new ScriptContext(sourceText, CurrentDirectory, Enumerable.Empty <string>(), debugMode: debugMode);
                    await RunFirstScript(context);
                }
                else
                {
                    var lineDependencies = ScriptCompiler.RuntimeDependencyResolver.GetDependenciesFromCode(CurrentDirectory, input);

                    foreach (var runtimeDependency in lineDependencies)
                    {
                        Logger.Verbose("Adding reference to a runtime dependency => " + runtimeDependency);
                        _scriptOptions = _scriptOptions.AddReferences(MetadataReference.CreateFromFile(runtimeDependency.Path));
                    }

                    _scriptState = await _scriptState.ContinueWithAsync(input, _scriptOptions, ex => true);
                }

                if (_scriptState?.Exception != null)
                {
                    Console.WritePrettyError(CSharpObjectFormatter.Instance.FormatException(_scriptState.Exception));
                }

                if (_scriptState?.ReturnValue != null)
                {
                    _globals.Print(_scriptState.ReturnValue);
                }
            }
            catch (Exception e)
            {
                Console.WritePrettyError(CSharpObjectFormatter.Instance.FormatException(e));
            }
        }
コード例 #6
0
            private async Task <ScriptState <object> > ExecuteOnUIThread(Script <object> script, ScriptState <object> stateOpt, bool displayResult)
            {
                return(await((Task <ScriptState <object> >)s_control.Invoke(
                                 (Func <Task <ScriptState <object> > >)(async() =>
                {
                    var task = (stateOpt == null) ?
                               script.RunAsync(_globals, catchException: e => true, cancellationToken: CancellationToken.None) :
                               script.RunFromAsync(stateOpt, catchException: e => true, cancellationToken: CancellationToken.None);

                    var newState = await task.ConfigureAwait(false);

                    if (newState.Exception != null)
                    {
                        DisplayException(newState.Exception);
                    }
                    else if (displayResult && newState.Script.HasReturnValue())
                    {
                        _globals.Print(newState.ReturnValue);
                    }

                    return newState;
                }))).ConfigureAwait(false));
            }
コード例 #7
0
        public async Task <object> ExecuteAsync(string statement)
        {
            statement = PrepareStatement(statement);

            if (_scriptState == null)
            {
                var usingStatements = new []
                {
                    "using static ICSharpCore.Script.Extensions;",
                    "using static ICSharpCore.Primitives.DisplayDataEmitter;"
                };


                var references = _references;

                if (references != null && references.Any())
                {
                    foreach (var line in references)
                    {
                        if (line.StartsWith("#r ") || line.StartsWith("#load "))
                        {
                            TryLoadReferenceFromScript(line);
                        }
                    }

                    usingStatements = references.Union(usingStatements).ToArray();
                }

                _scriptState = await CSharpScript.RunAsync(string.Join("\r\n", usingStatements), _scriptOptions, globals : _globals);

                _scriptState = await _scriptState.ContinueWithAsync(statement, _scriptOptions);
            }
            else
            {
                _scriptState = await _scriptState.ContinueWithAsync(statement, _scriptOptions);
            }

            if (_scriptState.ReturnValue == null)
            {
                return(string.Empty);
            }

            var jdisplayData = _scriptState.ReturnValue as JObject;

            if (jdisplayData != null)
            {
                return(jdisplayData);
            }

            var displayData = _scriptState.ReturnValue as DisplayData;

            if (displayData != null)
            {
                return(displayData);
            }

            _globals.Print(_scriptState.ReturnValue);

            var output = _interactiveOutput.ToString();

            _interactiveOutput.Clear();

            return(new DisplayData(output));
        }