forked from dotnet/try
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeRunner.cs
More file actions
138 lines (114 loc) · 4.8 KB
/
Copy pathCodeRunner.cs
File metadata and controls
138 lines (114 loc) · 4.8 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Builder;
using System.CommandLine.Invocation;
using System.CommandLine.Parsing;
using System.IO;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json;
namespace MLS.WasmCodeRunner
{
public class CodeRunner
{
int sequence = -1;
public InteropMessage<WasmCodeRunnerResponse> ProcessRunRequest(string message)
{
var messageObject = JsonConvert.DeserializeObject<InteropMessage<WasmCodeRunnerRequest>>(message);
if (messageObject.Sequence > this.sequence)
{
this.sequence = messageObject.Sequence;
}
else
{
return null;
}
if (messageObject.Data.Base64Assembly == null && messageObject.Data.Diagnostics == null)
{
// Something was posted that wasn't meant for us
return null;
}
var sequence = messageObject.Sequence;
var runRequest = messageObject.Data;
if (runRequest.Succeeded)
{
return ExecuteRunRequest(runRequest, sequence);
}
var diagnostics = runRequest.Diagnostics.Select(d => d.Message);
return new InteropMessage<WasmCodeRunnerResponse>(sequence, new WasmCodeRunnerResponse(false, null, diagnostics.ToArray(), null, null));
}
public InteropMessage<WasmCodeRunnerResponse> ExecuteRunRequest(WasmCodeRunnerRequest runRequest, int sequence)
{
var output = new List<string>();
string runnerException = null;
var bytes = Convert.FromBase64String(runRequest.Base64Assembly);
var stdOut = new StringWriter();
var stdError = new StringWriter();
using (var consoleState = new PreserveConsoleState())
{
MethodInfo main;
try
{
var assembly = Assembly.Load(bytes);
Console.SetOut(stdOut);
Console.SetError(stdError);
main = EntryPointDiscoverer.FindStaticEntryMethod(assembly);
}
catch (InvalidProgramException)
{
var result = new WasmCodeRunnerResponse(succeeded: false, exception: null,
output: new[] { "error CS5001: Program does not contain a static 'Main' method suitable for an entry point" },
diagnostics: Array.Empty<SerializableDiagnostic>(),
runnerException: null);
return new InteropMessage<WasmCodeRunnerResponse>(sequence, result);
}
try
{
var args = runRequest.RunArgs;
var builder = new CommandLineBuilder()
.ConfigureRootCommandFromMethod(main);
var parser = builder.Build();
parser.InvokeAsync(args).GetAwaiter().GetResult();
}
catch (Exception e)
{
if ((e.InnerException ?? e) is TypeLoadException t)
{
runnerException = $"Missing type `{t.TypeName}`";
}
if ((e.InnerException ?? e) is MissingMethodException m)
{
runnerException = $"Missing method `{m.Message}`";
}
if ((e.InnerException ?? e) is FileNotFoundException f)
{
runnerException = $"Missing file: `{f.FileName}`";
}
output.AddRange(SplitOnNewlines(e.ToString()));
}
var errors = stdError.ToString();
if (!string.IsNullOrWhiteSpace(errors))
{
runnerException = errors;
output.AddRange(SplitOnNewlines(errors));
}
output.AddRange(SplitOnNewlines(stdOut.ToString()));
var rb = new WasmCodeRunnerResponse(
succeeded: true,
exception: null,
output: output.ToArray(),
diagnostics: null,
runnerException: runnerException);
return new InteropMessage<WasmCodeRunnerResponse>(sequence, rb);
}
}
private IEnumerable<string> SplitOnNewlines(string str)
{
str = str.Replace("\r\n", "\n");
return str.Split('\n');
}
}
}