forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsConditionalExpression.cs
More file actions
79 lines (69 loc) · 2.71 KB
/
JsConditionalExpression.cs
File metadata and controls
79 lines (69 loc) · 2.71 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
using System.Collections.Generic;
using ServiceStack.Text;
namespace ServiceStack.Script
{
public class JsConditionalExpression : JsExpression
{
public JsToken Test { get; }
public JsToken Consequent { get; }
public JsToken Alternate { get; }
public JsConditionalExpression(JsToken test, JsToken consequent, JsToken alternate)
{
Test = test ?? throw new SyntaxErrorException($"Test Expression missing in Conditional Expression");
Consequent = consequent ?? throw new SyntaxErrorException($"Consequent Expression missing in Conditional Expression");
Alternate = alternate ?? throw new SyntaxErrorException($"Alternate Expression missing in Conditional Expression");
}
public override string ToRawString()
{
var sb = StringBuilderCache.Allocate();
sb.Append(Test.ToRawString());
sb.Append(" ? ");
sb.Append(Consequent.ToRawString());
sb.Append(" : ");
sb.Append(Alternate.ToRawString());
return StringBuilderCache.ReturnAndFree(sb);
}
public override Dictionary<string, object> ToJsAst()
{
var to = new Dictionary<string, object>
{
["type"] = ToJsAstType(),
["test"] = Test.ToJsAst(),
["consequent"] = Consequent.ToJsAst(),
["alternate"] = Alternate.ToJsAst(),
};
return to;
}
public override object Evaluate(ScriptScopeContext scope)
{
var test = Test.EvaluateToBool(scope);
var value = test
? Consequent.Evaluate(scope)
: Alternate.Evaluate(scope);
return value;
}
protected bool Equals(JsConditionalExpression other)
{
return Equals(Test, other.Test) &&
Equals(Consequent, other.Consequent) &&
Equals(Alternate, other.Alternate);
}
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((JsConditionalExpression) obj);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = (Test != null ? Test.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (Consequent != null ? Consequent.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (Alternate != null ? Alternate.GetHashCode() : 0);
return hashCode;
}
}
}
}