-
Notifications
You must be signed in to change notification settings - Fork 3
/
Program.cs
106 lines (96 loc) · 3.59 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
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.JSInterop;
using Microsoft.PowerFx;
using Microsoft.PowerFx.Core.Utils;
using Microsoft.PowerFx.LanguageServerProtocol;
using Microsoft.PowerFx.Syntax;
using Microsoft.PowerFx.Types;
using PowerFxWasm.Commons;
using PowerFxWasm.Model;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace PowerFxWasm
{
public static class Program
{
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
////builder.RootComponents.Add<App>("#app");
////builder.RootComponents.Add<HeadOutlet>("head::after");
//builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
await builder.Build().RunAsync();
}
private static readonly JsonSerializerOptions _jsonSerializerOptions = new JsonSerializerOptions
{
WriteIndented = true,
Converters =
{
new JsonStringEnumConverter(JsonNamingPolicy.CamelCase),
new NodeConverter<TexlNode>(),
new NodeConverter<VariadicOpNode>(),
new NodeConverter<ListNode>(),
new NodeConverter<CallNode>(),
new NodeConverter<Identifier>(),
new NodeConverter<DName>()
}
};
[JSInvokable]
public static async Task<string> EvaluateAsync(string context, string expression)
{
IReadOnlyList<Token> tokens = null;
CheckResult check = null;
var cts = new CancellationTokenSource();
var _timeout = TimeSpan.FromSeconds(2);
cts.CancelAfter(_timeout);
try
{
var engine = new PowerFxScopeFactory().GetEngine();
var parameters = (RecordValue)FormulaValue.FromJson(context);
if (parameters == null)
{
parameters = RecordValue.Empty();
}
tokens = engine.Tokenize(expression);
check = engine.Check(expression, parameters.Type, options: null);
check.ThrowOnErrors();
var eval = check.GetEvaluator();
var result = await eval.EvalAsync(cts.Token, parameters);
var resultString = PowerFxHelper.TestToString(result);
return JsonSerializer.Serialize(new
{
result = resultString,
tokens = tokens,
parse = JsonSerializer.Serialize(check.Parse.Root, _jsonSerializerOptions)
}, new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
});
}
catch (Exception ex)
{
throw ex;
}
finally
{
cts.Dispose();
}
}
[JSInvokable]
public static async Task<string> LspAsync(string body)
{
var scopeFactory = new PowerFxScopeFactory();
var sendToClientData = new List<string>();
var languageServer = new LanguageServer((string data) => sendToClientData.Add(data), scopeFactory);
try
{
languageServer.OnDataReceived(body.ToString());
return JsonSerializer.Serialize(sendToClientData.ToArray());
}
catch (Exception ex)
{
throw;
}
}
}
}