-
Notifications
You must be signed in to change notification settings - Fork 221
Expand file tree
/
Copy pathProgram.cs
More file actions
72 lines (63 loc) · 3.04 KB
/
Program.cs
File metadata and controls
72 lines (63 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using Microsoft.Diagnostics.Runtime;
using ProtoBuf;
using SharpLab.Container.Execution;
using SharpLab.Container.Protocol;
using SharpLab.Container.Protocol.Stdin;
using SharpLab.Container.Runtime;
using SharpLab.Runtime.Internal;
namespace SharpLab.Container {
public static class Program {
public static string ExeFileName { get; } = Path.ChangeExtension(Path.GetFileName(typeof(Program).Assembly.Location), "exe");
public static void Main() {
try {
SafeMain();
}
catch (Exception ex) {
Console.WriteLine(ex.ToString());
}
}
private static void SafeMain() {
Console.OutputEncoding = Encoding.UTF8;
var consoleIn = new UserCodeConsoleReader();
Console.SetIn(consoleIn);
using var stdin = Console.OpenStandardInput(1024);
using var stdout = Console.OpenStandardOutput(1024);
Run(stdin, stdout, () => consoleIn.Reset());
}
// TODO: Change test structure so that this can be inlined
internal static void Run(Stream stdin, Stream stdout, Action beforeCommand) {
var stdoutWriter = new StdoutWriter(stdout, new Utf8JsonWriter(stdout, new() {
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
}));
var flowWriter = new FlowWriter(stdoutWriter, new Utf8ValuePresenter());
SetupRuntimeServices(flowWriter, stdoutWriter);
var executeCommandHandler = new ExecuteCommandHandler(flowWriter, stdoutWriter);
var shouldExit = false;
while (!shouldExit) {
var command = Serializer.DeserializeWithLengthPrefix<ExecuteCommand?>(stdin, PrefixStyle.Base128);
if (command == null)
break; // end-of-input
beforeCommand();
executeCommandHandler.Execute(command);
}
}
private static void SetupRuntimeServices(FlowWriter flowWriter, StdoutWriter stdoutWriter) {
#pragma warning disable CS0618 // Type or member is obsolete
RuntimeServices.ValuePresenter = new LegacyValuePresenter();
#pragma warning restore CS0618 // Type or member is obsolete
RuntimeServices.InspectionWriter = new InspectionWriter(stdoutWriter);
RuntimeServices.FlowWriter = flowWriter;
RuntimeServices.MemoryBytesInspector = new MemoryBytesInspector(new Pool<ClrRuntime>(() => {
var dataTarget = DataTarget.AttachToProcess(Current.ProcessId, suspend: false);
return dataTarget.ClrVersions.Single(c => c.Flavor == ClrFlavor.Core).CreateRuntime();
}));
RuntimeServices.MemoryGraphBuilderFactory = argumentNames => new MemoryGraphBuilder(argumentNames, RuntimeServices.ValuePresenter);
}
}
}