-
Notifications
You must be signed in to change notification settings - Fork 3
/
Program.cs
138 lines (114 loc) · 4.02 KB
/
Program.cs
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
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Net.Http;
using WebAssembly;
namespace WasmRoslyn
{
public class Program
{
static CompileService service;
public static void Main(JSObject app, JSObject outputLog)
{
CheckHttpClient();
service = new CompileService(httpClient, app);
return;
}
public static int AddNums(int a, int b)
{
return a+b;
}
public static void SayHello(JSObject app)
{
app.Invoke("sayHelloCallback", new object[] { "Hello Maddy" });
}
public static void Process(JSObject App)
{
List<string> assemblies = new List<string>();
var assembliesText = App.GetObjectProperty("assemblies") as WebAssembly.Core.Array;
for(int i = 0;i<assembliesText.Length;i++)
{
assemblies.Add(assembliesText[i].ToString());
}
Task.Run(() => service.SetReferences(assemblies));
}
public static void Run(JSObject app, string code)
{
Task.Run(() => CompileAndRun(app, code));
}
public static void CompileOnly(JSObject app, string code)
{
Task.Run(() => Compile(app, code));
}
public static async Task Compile(JSObject app, string code)
{
try
{
service.CompileLog = new List<string>();
var type = await service.CompileSourceCode(code);
if (type != null)
{
service.CompileLog.Add($"Exported type returned from assembly: {type}");
}
else
{
service.CompileLog.Add("No exported types were found.");
}
}
catch (Exception e)
{
service.CompileLog.Add(e.Message);
service.CompileLog.Add(e.StackTrace);
throw;
}
finally
{
app.Invoke("setCompileLog", new object[] { string.Join("\r\n",service.CompileLog) });
}
}
public static async Task CompileAndRun(JSObject app, string code)
{
try
{
service.CompileLog = new List<string>();
var result = await service.CompileAndRun(code);
app.Invoke("setRunLog", new object[] { string.Join("\r\n",result) });
}
catch (Exception e)
{
service.CompileLog.Add(e.Message);
service.CompileLog.Add(e.StackTrace);
throw;
}
finally
{
app.Invoke("setCompileLog", new object[] { string.Join("\r\n",service.CompileLog) });
}
}
static HttpClient httpClient;
static string BaseApiUrl = string.Empty;
static string PathName = string.Empty;
static void CheckHttpClient()
{
if (httpClient == null)
{
Console.WriteLine("Create HttpClient");
using (var window = (JSObject)WebAssembly.Runtime.GetGlobalObject("window"))
using (var location = (JSObject)window.GetObjectProperty("location"))
{
BaseApiUrl = (string)location.GetObjectProperty("origin");
PathName = (string)location.GetObjectProperty("pathname");
Console.WriteLine($"Base: {BaseApiUrl} ReferencePath: {PathName}");
}
httpClient = new HttpClient() { BaseAddress = new Uri(new Uri(BaseApiUrl), PathName) };
}
}
private static async Task<String> GetSourceCode(string name)
{
Console.WriteLine($"Fetching: {name}");
CheckHttpClient();
var source = await httpClient.GetStringAsync(name).ConfigureAwait(false);
return source;
}
}
}