forked from Taritsyn/JavaScriptEngineSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsEvaluationService.cs
More file actions
139 lines (118 loc) · 3.06 KB
/
JsEvaluationService.cs
File metadata and controls
139 lines (118 loc) · 3.06 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
139
using System.Collections.Generic;
using System.Linq;
#if NETSTANDARD1_6 || NET451
using Microsoft.AspNetCore.Mvc.Rendering;
#elif NET40
using System.Web.Mvc;
#else
#error No implementation for this target
#endif
using JavaScriptEngineSwitcher.Core;
using JavaScriptEngineSwitcher.Sample.Logic.Models;
namespace JavaScriptEngineSwitcher.Sample.Logic.Services
{
public class JsEvaluationService
{
private static readonly Dictionary<string, string> _engineDisplayNameMappings;
private readonly JsEngineSwitcher _engineSwitcher;
static JsEvaluationService()
{
_engineDisplayNameMappings = new Dictionary<string, string>
{
{ "ChakraCoreJsEngine", "ChakraCore" },
{ "JintJsEngine", "Jint" },
{ "JurassicJsEngine", "Jurassic" },
{ "MsieJsEngine", "MSIE" },
{ "V8JsEngine", "V8" }
};
}
#if NET40
public JsEvaluationService()
: this(JsEngineSwitcher.Instance)
{ }
#endif
public JsEvaluationService(JsEngineSwitcher engineSwitcher)
{
_engineSwitcher = engineSwitcher;
}
public JsEvaluationViewModel GetInitializationData()
{
var model = new JsEvaluationViewModel
{
EngineName = _engineSwitcher.DefaultEngineName,
AvailableEngineList = _engineSwitcher.EngineFactories
.Select(e => new SelectListItem
{
Value = e.EngineName,
Text = GetEngineDisplayName(e.EngineName)
}),
Expression = string.Empty,
Result = null
};
return model;
}
public JsEvaluationViewModel Evaluate(JsEvaluationViewModel model)
{
IJsEngine engine = null;
var result = new JsEvaluationResultViewModel();
try
{
engine = _engineSwitcher.CreateEngine(model.EngineName);
result.Value = engine.Evaluate<string>(model.Expression);
}
catch (JsEngineLoadException e)
{
var error = GetJsEvaluationErrorFromException(e);
result.Errors.Add(error);
}
catch (JsRuntimeException e)
{
var error = GetJsEvaluationErrorFromException(e);
error.LineNumber = e.LineNumber;
error.ColumnNumber = e.ColumnNumber;
error.SourceFragment = e.SourceFragment;
result.Errors.Add(error);
}
finally
{
if (engine != null)
{
engine.Dispose();
}
}
model.Result = result;
return model;
}
private static JsEvaluationErrorViewModel GetJsEvaluationErrorFromException(JsException jsException)
{
var jsError = new JsEvaluationErrorViewModel
{
EngineName = GetEngineDisplayName(jsException.EngineName),
EngineVersion = jsException.EngineVersion,
Message = jsException.Message
};
return jsError;
}
private static string GetEngineDisplayName(string engineName)
{
string displayName = engineName;
const string postfix = "JsEngine";
if (_engineDisplayNameMappings.ContainsKey(engineName))
{
displayName = _engineDisplayNameMappings[engineName];
}
else
{
if (engineName.EndsWith(postfix))
{
int displayNameLength = engineName.Length - postfix.Length;
if (displayNameLength > 0)
{
displayName = engineName.Substring(0, displayNameLength);
}
}
}
return displayName;
}
}
}