void IScriptGlobalsHelper.PrintLine(ScriptGlobals globals, object color, Exception ex) { if (!IsCurrentScript(globals)) { return; } ReplEditor.OutputPrintLine(Format(ex), color); }
void IScriptGlobalsHelper.PrintLine(ScriptGlobals globals, object color, string text) { if (!IsCurrentScript(globals)) { return; } ReplEditor.OutputPrintLine(text, color); }
public void OnVisible() { if (hasInitialized) { return; } hasInitialized = true; ReplEditor.OutputPrintLine(Logo, BoxedTextColor.ReplOutputText); InitializeExecutionEngine(true, true); }
void InitializeExecutionEngine(bool loadConfig, bool showHelp) { Debug.Assert(execState == null); if (execState != null) { throw new InvalidOperationException(); } execState = new ExecState(this, dispatcher, new CancellationTokenSource()); var execStateCache = execState; Task.Run(() => { execStateCache.CancellationToken.ThrowIfCancellationRequested(); var userOpts = new UserScriptOptions(); if (loadConfig) { userOpts.LibPaths.AddRange(GetDefaultLibPaths()); userOpts.LoadPaths.AddRange(GetDefaultLoadPaths()); InitializeUserScriptOptions(userOpts); } var opts = ScriptOptions.Default; opts = opts.WithMetadataResolver(ScriptMetadataResolver.Default .WithBaseDirectory(AppDirectories.BinDirectory) .WithSearchPaths(userOpts.LibPaths.Distinct(StringComparer.OrdinalIgnoreCase))); opts = opts.WithSourceResolver(ScriptSourceResolver.Default .WithBaseDirectory(AppDirectories.BinDirectory) .WithSearchPaths(userOpts.LoadPaths.Distinct(StringComparer.OrdinalIgnoreCase))); opts = opts.WithImports(userOpts.Imports); opts = opts.WithReferences(userOpts.References); execStateCache.ScriptOptions = opts; var script = Create <object>(string.Empty, execStateCache.ScriptOptions, typeof(IScriptGlobals), null); execStateCache.CancellationToken.ThrowIfCancellationRequested(); execStateCache.ScriptState = script.RunAsync(execStateCache.Globals, execStateCache.CancellationToken).Result; if (showHelp) { ReplEditor.OutputPrintLine(Help, BoxedTextColor.ReplOutputText); } }, execStateCache.CancellationToken) .ContinueWith(t => { execStateCache.IsInitializing = false; var ex = t.Exception; if (!t.IsCanceled && !t.IsFaulted) { CommandExecuted(); } else { ReplEditor.OutputPrintLine($"Could not create the script:\n\n{ex}", BoxedTextColor.Error, true); } }, CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext()); }
protected override void InitializeUserScriptOptions(UserScriptOptions options) { var rspFile = GetResponseFile("CSharpInteractive.rsp"); if (rspFile == null) { return; } ReplEditor.OutputPrintLine(string.Format(dnSpy_Scripting_Roslyn_Resources.LoadingContextFromFile, Path.GetFileName(rspFile)), BoxedTextColor.ReplOutputText); foreach (var t in ResponseFileReader.Read(rspFile)) { switch (t.Item1.ToLowerInvariant()) { case "r": case "reference": options.References.AddRange(RespFileUtils.GetReferences(t.Item2)); break; case "u": case "using": case "usings": case "import": case "imports": options.Imports.AddRange(t.Item2.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)); break; case "lib": case "libpath": case "libpaths": options.LibPaths.AddRange(RespFileUtils.GetReferencePaths(t.Item2)); break; case "loadpath": case "loadpaths": options.LoadPaths.AddRange(RespFileUtils.GetReferencePaths(t.Item2)); break; default: Debug.Fail($"Unknown option: '{t.Item1}'"); break; } } }
void PrintDiagnostics(ImmutableArray <Diagnostic> diagnostics) { const int MAX_DIAGS = 5; for (int i = 0; i < diagnostics.Length && i < MAX_DIAGS; i++) { ReplEditor.OutputPrintLine(DiagnosticFormatter.Format(diagnostics[i], Thread.CurrentThread.CurrentUICulture), BoxedTextColor.Error, true); } int extraErrors = diagnostics.Length - MAX_DIAGS; if (extraErrors > 0) { if (extraErrors == 1) { ReplEditor.OutputPrintLine(string.Format(dnSpy_Scripting_Roslyn_Resources.CompilationAdditionalError, extraErrors), BoxedTextColor.Error, true); } else { ReplEditor.OutputPrintLine(string.Format(dnSpy_Scripting_Roslyn_Resources.CompilationAdditionalErrors, extraErrors), BoxedTextColor.Error, true); } } }
public void Reset(bool loadConfig = true) { if (!CanReset) { return; } if (execState != null) { execState.CancellationTokenSource.Cancel(); try { execState.Globals.RaiseScriptReset(); } catch { // Ignore buggy script exceptions } execState.CancellationTokenSource.Dispose(); } isResetting = true; execState = null; ReplEditor.Reset(); isResetting = false; ReplEditor.OutputPrintLine(dnSpy_Scripting_Roslyn_Resources.ResettingExecutionEngine, BoxedTextColor.ReplOutputText); InitializeExecutionEngine(loadConfig, false); }
void ObjectOutputLine(object color, PrintOptionsImpl printOptions, object value, bool startOnNewLine = false) { ObjectOutput(color, printOptions, value, startOnNewLine); ReplEditor.OutputPrintLine(string.Empty, color); }
bool ExecuteCommandInternal(string input) { Debug.Assert(execState != null && !execState.IsInitializing); if (execState == null || execState.IsInitializing) { return(true); } lock (lockObj) { Debug.Assert(execState.ExecTask == null && !execState.Executing); if (execState.ExecTask != null || execState.Executing) { return(true); } execState.Executing = true; } try { var scState = ParseScriptCommand(input); if (scState != null) { if (execState != null) { lock (lockObj) execState.Executing = false; } scState.Command.Execute(this, scState.Arguments); bool isReset = scState.Command is ResetCommand; if (!isReset) { CommandExecuted(); } return(true); } var oldState = execState; var taskSched = TaskScheduler.FromCurrentSynchronizationContext(); Task.Run(() => { oldState.CancellationToken.ThrowIfCancellationRequested(); var opts = oldState.ScriptOptions.WithReferences(Array.Empty <MetadataReference>()).WithImports(Array.Empty <string>()); var execTask = oldState.ScriptState.ContinueWithAsync(input, opts, oldState.CancellationToken); oldState.CancellationToken.ThrowIfCancellationRequested(); lock (lockObj) { if (oldState == execState) { oldState.ExecTask = execTask; } } execTask.ContinueWith(t => { var ex = t.Exception; bool isActive; lock (lockObj) { isActive = oldState == execState; if (isActive) { oldState.ExecTask = null; } } if (isActive) { try { if (ex != null) { this.ReplEditor.OutputPrint(Format(ex.InnerException), BoxedTextColor.Error, true); } if (!t.IsCanceled && !t.IsFaulted) { oldState.ScriptState = t.Result; var val = t.Result.ReturnValue; if (val != null) { ObjectOutputLine(BoxedTextColor.ReplOutputText, oldState.Globals.PrintOptionsImpl, val, true); } } } finally { CommandExecuted(); } } }, CancellationToken.None, TaskContinuationOptions.None, taskSched); }) .ContinueWith(t => { if (execState != null) { lock (lockObj) execState.Executing = false; } var innerEx = t.Exception?.InnerException; if (innerEx is CompilationErrorException) { var cee = (CompilationErrorException)innerEx; PrintDiagnostics(cee.Diagnostics); CommandExecuted(); } else if (innerEx is OperationCanceledException) { CommandExecuted(); } else { var ex = t.Exception; if (ex != null) { this.ReplEditor.OutputPrint(ex.ToString(), BoxedTextColor.Error, true); CommandExecuted(); } } }, CancellationToken.None, TaskContinuationOptions.None, taskSched); return(true); } catch (Exception ex) { if (execState != null) { lock (lockObj) execState.Executing = false; } ReplEditor.OutputPrintLine($"Error executing script:\n\n{ex}", BoxedTextColor.Error, true); return(false); } }