-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDynamicConfig.cs
252 lines (214 loc) · 7.52 KB
/
DynamicConfig.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
using Jint;
using Jint.Native;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
#if !__IOS__
namespace Jint.Ex
{
/// <summary>
/// Implement configuration file in JavaScript
/// Use the Jint v 2.x JavaScript runtime
/// </summary>
public class DynamicConfig : DynamicObject
{
Engine _engine;
public static dynamic LoadFile(string fileName = "config.js")
{
return Load(System.IO.File.ReadAllText(fileName));
}
public static dynamic Load(string source)
{
return new DynamicConfig(source);
}
private DynamicConfig(string source)
{
this._engine = this.GetJint();
this._engine.Execute(source);
}
private static void Print(object s)
{
if (s == null)
s = "null";
Console.WriteLine(s.ToString());
}
private Engine GetJint()
{
_engine = new Engine();
_engine.SetValue("print", new Action<object>(Print));
return _engine;
}
private string GetFullPathConfigJsFilename(string fileName = "config.js")
{
var currentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
return Path.Combine(currentPath, fileName);
}
private Jint.Native.JsValue __GetJsValue(string property)
{
return this._engine.Execute(property).GetCompletionValue();
}
internal static object ConvertJsValueToNetObject(Jint.Native.JsValue v)
{
if (v.IsBoolean())
return v.AsBoolean();
if (v.IsNumber())
return v.AsNumber();
if (v.IsNull())
return null;
if (v.IsUndefined())
return "undefined";
if (v.IsString())
return v.AsString();
if (v.IsObject())
return v.ToObject();
throw new ArgumentException(string.Format("Cannot get display value for JsValue"));
}
internal static JsValue CallFunction(Jint.Native.Function.FunctionInstance f, object[] args, Jint.Engine engine)
{
var jsValue = f.Call(JsValue.Undefined, HelperClass.MakeJsValues(args.ToList(), engine).ToArray());
return jsValue;
}
internal static bool IsFunction(JsValue jsValue)
{
return jsValue.IsObject() && jsValue.AsObject() is Jint.Native.Function.FunctionInstance;
}
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
var pname = binder.Name;
var jsValue = __GetJsValue(pname);
result = null;
if (DynamicConfig.IsFunction(jsValue))
{
var f = jsValue.AsObject() as Jint.Native.Function.FunctionInstance;
jsValue = DynamicConfig.CallFunction(f, args, this._engine);
if (jsValue.IsObject())
result = new DynamicJsValueForDotNet(jsValue.AsObject(), this._engine);
else
result = DynamicConfig.ConvertJsValueToNetObject(jsValue);
return true;
}
return false;
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
var pname = binder.Name;
var jsValue = __GetJsValue(pname);
if (jsValue.IsObject())
{
result = new DynamicJsValueForDotNet(jsValue.AsObject(), this._engine);
}
else
result = ConvertJsValueToNetObject(jsValue);
return true;
}
}
public class DynamicJsValueForDotNet : DynamicObject
{
JsValue _jsValue;
Jint.Engine _engine;
public DynamicJsValueForDotNet(JsValue jsValue, Jint.Engine engine)
{
this._jsValue = jsValue;
this._engine = engine;
}
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
var pname = binder.Name;
var jsValue = __GetJsValue(pname);
result = null;
if (DynamicConfig.IsFunction(jsValue))
{
var f = jsValue.AsObject() as Jint.Native.Function.FunctionInstance;
jsValue = DynamicConfig.CallFunction(f, args, this._engine);
if (jsValue.IsObject())
result = new DynamicJsValueForDotNet(jsValue.AsObject(), this._engine);
else
result = DynamicConfig.ConvertJsValueToNetObject(jsValue);
return true;
}
return false;
}
public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)
{
result = null;
var o = this._jsValue.AsObject();
if (o is Jint.Native.Array.ArrayInstance)
{
var ai = o as Jint.Native.Array.ArrayInstance;
var v = ai.Get(indexes[0].ToString());
var vo = DynamicConfig.ConvertJsValueToNetObject(v);
result = vo;
}
else
{
}
return true;
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
return true;
}
public Jint.Native.JsValue __GetJsValue(string property)
{
Jint.Native.Object.ObjectInstance o = this._jsValue.AsObject();
var jsValue = o.Get(property);
return jsValue;
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
var pname = binder.Name;
var jsValue = __GetJsValue(pname);
if (jsValue.IsObject())
result = new DynamicJsValueForDotNet(jsValue.AsObject(), this._engine);
else
result = DynamicConfig.ConvertJsValueToNetObject(jsValue);
return true;
}
}
}
#endif
/*
public bool Tracing(string property, bool defaultValue = false)
{
var jsVal = __Tracing(property);
if (jsVal.IsBoolean())
return jsVal.AsBoolean();
else
return defaultValue;
}
public int Tracing(string property, int defaultValue = 0)
{
var jsVal = __Tracing(property);
if (jsVal.IsNumber())
return (int)jsVal.AsNumber();
else
return defaultValue;
}
public double Tracing(string property, double defaultValue = 0)
{
var jsVal = __Tracing(property);
if (jsVal.IsNumber())
return jsVal.AsNumber();
else
return defaultValue;
}
public string Tracing(string property, string defaultValue = "")
{
var jsVal = __Tracing(property);
if (jsVal.IsNumber())
return jsVal.AsString();
else
return defaultValue;
}
public Jint.Native.JsValue __Tracing(string property)
{
var code = String.Format("Tracing.{0};", property);
var jsValue = this._engine.Execute(code);
return jsValue;
}
*/