forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsExpression.cs
More file actions
464 lines (394 loc) · 14.7 KB
/
JsExpression.cs
File metadata and controls
464 lines (394 loc) · 14.7 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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using ServiceStack.Text;
namespace ServiceStack.Script
{
public abstract class JsExpression : JsToken
{
public abstract Dictionary<string, object> ToJsAst();
public virtual string ToJsAstType() => GetType().ToJsAstType();
}
public class JsIdentifier : JsExpression
{
public string Name { get; }
public JsIdentifier(string name) => Name = name;
public JsIdentifier(ReadOnlySpan<char> name) => Name = name.Value();
public override string ToRawString() => ":" + Name;
public override object Evaluate(ScriptScopeContext scope)
{
var ret = scope.PageResult.GetValue(Name, scope);
return ret;
}
protected bool Equals(JsIdentifier other) => string.Equals(Name, other.Name);
public override Dictionary<string, object> ToJsAst() => new Dictionary<string, object> {
["type"] = ToJsAstType(),
["name"] = Name,
};
public override int GetHashCode() => Name.GetHashCode();
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((JsIdentifier) obj);
}
public override string ToString() => ToRawString();
}
public class JsLiteral : JsExpression
{
public static JsLiteral True = new JsLiteral(true);
public static JsLiteral False = new JsLiteral(false);
public object Value { get; }
public JsLiteral(object value) => Value = value;
public override string ToRawString() => JsonValue(Value);
public override int GetHashCode() => (Value != null ? Value.GetHashCode() : 0);
protected bool Equals(JsLiteral other) => Equals(Value, other.Value);
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
return obj.GetType() == this.GetType() && Equals((JsLiteral) obj);
}
public override string ToString() => ToRawString();
public override object Evaluate(ScriptScopeContext scope) => Value;
public override Dictionary<string, object> ToJsAst() => new Dictionary<string, object> {
["type"] = ToJsAstType(),
["value"] = Value,
["raw"] = JsonValue(Value),
};
}
public class JsArrayExpression : JsExpression
{
public JsToken[] Elements { get; }
public JsArrayExpression(params JsToken[] elements) => Elements = elements.ToArray();
public JsArrayExpression(IEnumerable<JsToken> elements) : this(elements.ToArray()) {}
public override object Evaluate(ScriptScopeContext scope)
{
var to = new List<object>();
foreach (var element in Elements)
{
if (element is JsSpreadElement spread)
{
if (!(spread.Argument.Evaluate(scope) is IEnumerable arr))
continue;
foreach (var value in arr)
{
to.Add(value);
}
}
else
{
var value = element.Evaluate(scope);
to.Add(value);
}
}
return to;
}
public override string ToRawString()
{
var sb = StringBuilderCache.Allocate();
sb.Append("[");
for (var i = 0; i < Elements.Length; i++)
{
if (i > 0)
sb.Append(",");
var element = Elements[i];
sb.Append(element.ToRawString());
}
sb.Append("]");
return StringBuilderCache.ReturnAndFree(sb);
}
public override Dictionary<string, object> ToJsAst()
{
var elements = new List<object>();
var to = new Dictionary<string, object>
{
["type"] = ToJsAstType(),
["elements"] = elements
};
foreach (var element in Elements)
{
elements.Add(element.ToJsAst());
}
return to;
}
protected bool Equals(JsArrayExpression other)
{
return Elements.EquivalentTo(other.Elements);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((JsArrayExpression) obj);
}
public override int GetHashCode()
{
return (Elements != null ? Elements.GetHashCode() : 0);
}
}
public class JsObjectExpression : JsExpression
{
public JsProperty[] Properties { get; }
public JsObjectExpression(params JsProperty[] properties) => Properties = properties;
public JsObjectExpression(IEnumerable<JsProperty> properties) : this(properties.ToArray()) {}
public static string GetKey(JsToken token)
{
if (token is JsLiteral literalKey)
return literalKey.Value.ToString();
if (token is JsIdentifier identifierKey)
return identifierKey.Name;
if (token is JsMemberExpression memberExpr && memberExpr.Property is JsIdentifier prop)
return prop.Name;
throw new SyntaxErrorException($"Invalid Key. Expected a Literal or Identifier but was {token.DebugToken()}");
}
public override object Evaluate(ScriptScopeContext scope)
{
var to = new Dictionary<string, object>();
foreach (var prop in Properties)
{
if (prop.Key == null)
{
if (prop.Value is JsSpreadElement spread)
{
var value = spread.Argument.Evaluate(scope);
var obj = value.ToObjectDictionary();
if (obj != null)
{
foreach (var entry in obj)
{
to[entry.Key] = entry.Value;
}
}
}
else
{
throw new NotSupportedException("Object Expressions does not have a key");
}
}
else
{
var keyString = GetKey(prop.Key);
var value = prop.Value.Evaluate(scope);
to[keyString] = value;
}
}
return to;
}
public override string ToRawString()
{
var sb = StringBuilderCache.Allocate();
sb.Append("{");
for (var i = 0; i < Properties.Length; i++)
{
if (i > 0)
sb.Append(",");
var prop = Properties[i];
if (prop.Key != null)
{
sb.Append(prop.Key.ToRawString());
if (!prop.Shorthand)
{
sb.Append(":");
sb.Append(prop.Value.ToRawString());
}
}
else //.... spread operator
{
sb.Append(prop.Value.ToRawString());
}
}
sb.Append("}");
return StringBuilderCache.ReturnAndFree(sb);
}
public override Dictionary<string, object> ToJsAst()
{
var properties = new List<object>();
var to = new Dictionary<string, object>
{
["type"] = ToJsAstType(),
["properties"] = properties
};
var propType = typeof(JsProperty).ToJsAstType();
foreach (var prop in Properties)
{
properties.Add(new Dictionary<string, object> {
["type"] = propType,
["key"] = prop.Key?.ToJsAst(),
["computed"] = false, //syntax not supported: { ["a" + 1]: 2 }
["value"] = prop.Value.ToJsAst(),
["kind"] = "init",
["method"] = false,
["shorthand"] = prop.Shorthand,
});
}
return to;
}
protected bool Equals(JsObjectExpression other)
{
return Properties.EquivalentTo(other.Properties);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((JsObjectExpression) obj);
}
public override int GetHashCode()
{
return (Properties != null ? Properties.GetHashCode() : 0);
}
}
public class JsProperty
{
public JsToken Key { get; }
public JsToken Value { get; }
public bool Shorthand { get; }
public JsProperty(JsToken key, JsToken value) : this(key, value, shorthand:false){}
public JsProperty(JsToken key, JsToken value, bool shorthand)
{
Key = key;
Value = value;
Shorthand = shorthand;
}
protected bool Equals(JsProperty other)
{
return Equals(Key, other.Key) &&
Equals(Value, other.Value) &&
Shorthand == other.Shorthand;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((JsProperty) obj);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = (Key != null ? Key.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (Value != null ? Value.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ Shorthand.GetHashCode();
return hashCode;
}
}
}
public class JsSpreadElement : JsExpression
{
public JsToken Argument { get; }
public JsSpreadElement(JsToken argument)
{
Argument = argument;
}
public override object Evaluate(ScriptScopeContext scope)
{
return Argument.Evaluate(scope);
}
public override string ToRawString()
{
return "..." + Argument.ToRawString();
}
public override Dictionary<string, object> ToJsAst() => new Dictionary<string, object>
{
["type"] = ToJsAstType(),
["argument"] = Argument.ToJsAst()
};
protected bool Equals(JsSpreadElement other)
{
return Equals(Argument, other.Argument);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((JsSpreadElement) obj);
}
public override int GetHashCode()
{
return (Argument != null ? Argument.GetHashCode() : 0);
}
}
public class JsArrowFunctionExpression : JsExpression
{
public JsIdentifier[] Params { get; }
public JsToken Body { get; }
public JsArrowFunctionExpression(JsIdentifier param, JsToken body) : this(new[] {param}, body) {}
public JsArrowFunctionExpression(JsIdentifier[] @params, JsToken body)
{
Params = @params ?? throw new SyntaxErrorException($"Params missing in Arrow Function Expression");
Body = body ?? throw new SyntaxErrorException($"Body missing in Arrow Function Expression");
}
public override string ToRawString()
{
var sb = StringBuilderCache.Allocate();
sb.Append("(");
for (var i = 0; i < Params.Length; i++)
{
var identifier = Params[i];
if (i > 0)
sb.Append(",");
sb.Append(identifier.Name);
}
sb.Append(") => ");
sb.Append(Body.ToRawString());
return StringBuilderCache.ReturnAndFree(sb);
}
public override object Evaluate(ScriptScopeContext scope) => this;
public object Invoke(params object[] @params) => Invoke(JS.CreateScope(), @params);
public object Invoke(ScriptScopeContext scope, params object[] @params)
{
var args = new Dictionary<string, object>();
for (var i = 0; i < Params.Length; i++)
{
if (@params.Length < i)
break;
var param = Params[i];
args[param.Name] = @params[i];
}
var exprScope = scope.ScopeWithParams(args);
var ret = Body.Evaluate(exprScope);
return ret;
}
public override Dictionary<string, object> ToJsAst()
{
var to = new Dictionary<string, object>
{
["type"] = ToJsAstType(),
["id"] = null,
};
var args = new List<object>();
to["params"] = args;
foreach (var param in Params)
{
args.Add(param.ToJsAst());
}
to["body"] = Body.ToJsAst();
return to;
}
protected bool Equals(JsArrowFunctionExpression other)
{
return Params.EquivalentTo(other.Params) && Equals(Body, other.Body);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((JsArrowFunctionExpression) obj);
}
public override int GetHashCode()
{
unchecked
{
return ((Params != null ? Params.GetHashCode() : 0) * 397) ^ (Body != null ? Body.GetHashCode() : 0);
}
}
}
}