forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsMemberExpression.cs
More file actions
203 lines (182 loc) · 7.14 KB
/
JsMemberExpression.cs
File metadata and controls
203 lines (182 loc) · 7.14 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using ServiceStack.Text;
namespace ServiceStack.Script
{
public class JsMemberExpression : JsExpression
{
public JsToken Object { get; }
public JsToken Property { get; }
public bool Computed { get; } //indexer
public JsMemberExpression(JsToken @object, JsToken property) : this(@object, property, false) { }
public JsMemberExpression(JsToken @object, JsToken property, bool computed)
{
Object = @object;
Property = property;
Computed = computed;
}
public override string ToRawString()
{
var sb = StringBuilderCache.Allocate();
sb.Append(Object.ToRawString());
if (Computed)
{
sb.Append("[");
sb.Append(Property.ToRawString());
sb.Append("]");
}
else
{
sb.Append(".");
sb.Append(Property.ToRawString());
}
return StringBuilderCache.ReturnAndFree(sb);
}
public override Dictionary<string, object> ToJsAst()
{
var to = new Dictionary<string, object>
{
["type"] = ToJsAstType(),
["computed"] = Computed,
["object"] = Object.ToJsAst(),
["property"] = Property.ToJsAst(),
};
return to;
}
public override object Evaluate(ScriptScopeContext scope)
{
var targetValue = Object.Evaluate(scope);
var ret = GetValue(targetValue, scope);
return Equals(ret, JsNull.Value)
? null
: ret;
}
private static object PropValue(object targetValue, Type targetType, string name)
{
var memberFn = TypeProperties.Get(targetType).GetPublicGetter(name)
?? TypeFields.Get(targetType).GetPublicGetter(name);
if (memberFn != null)
{
return memberFn(targetValue);
}
var methods = targetType.GetInstanceMethods();
var indexerMethod =
methods.FirstOrDefault(x => x.Name == "get_Item" && x.GetParameters().Any(p => p.ParameterType == typeof(string))) ??
methods.FirstOrDefault(x => x.Name == "get_Item" && x.GetParameters().Any(p => p.ParameterType != typeof(string)));
if (indexerMethod != null)
{
var fn = indexerMethod.GetInvoker();
var ret = fn(targetValue, name);
return ret;
}
throw new ArgumentException($"'{targetType.Name}' does not have a '{name}' property or field");
}
private object GetValue(object targetValue, ScriptScopeContext scope)
{
if (targetValue == null || targetValue == JsNull.Value)
return JsNull.Value;
var targetType = targetValue.GetType();
try
{
if (!Computed)
{
if (Property is JsIdentifier identifier)
{
var ret = PropValue(targetValue, targetType, identifier.Name);
// Don't emit member expression on null KeyValuePair
if (ret == null && targetType.Name == "KeyValuePair`2")
return JsNull.Value;
return ret;
}
}
else
{
var indexValue = Property.Evaluate(scope);
if (indexValue == null)
return JsNull.Value;
if (targetType.IsArray)
{
var array = (Array) targetValue;
if (indexValue is long l)
return array.GetValue(l);
var intValue = indexValue.ConvertTo<int>();
return array.GetValue(intValue);
}
if (targetValue is IDictionary dict)
{
var ret = dict[indexValue];
return ret ?? JsNull.Value;
}
if (indexValue is string propName)
{
return PropValue(targetValue, targetType, propName);
}
if (targetValue is IList list)
{
var intValue = indexValue.ConvertTo<int>();
return list[intValue];
}
if (targetValue is IEnumerable e)
{
var intValue = indexValue.ConvertTo<int>();
var i = 0;
foreach (var item in e)
{
if (i++ == intValue)
return item;
}
return null;
}
if (DynamicNumber.IsNumber(indexValue.GetType()))
{
var indexerMethod = targetType.GetInstanceMethod("get_Item");
if (indexerMethod != null)
{
var fn = indexerMethod.GetInvoker();
var ret = fn(targetValue, indexValue);
return ret ?? JsNull.Value;
}
}
}
}
catch (KeyNotFoundException)
{
return JsNull.Value;
}
catch (Exception ex)
{
var exResult = scope.PageResult.Format.OnExpressionException(scope.PageResult, ex);
if (exResult != null)
return exResult;
var expr = ToRawString();
throw new BindingExpressionException($"Could not evaluate expression '{expr}'", null, expr, ex);
}
throw new NotSupportedException($"'{targetValue.GetType()}' does not support access by '{Property}'");
}
protected bool Equals(JsMemberExpression other)
{
return Equals(Object, other.Object) &&
Equals(Property, other.Property) &&
Computed == other.Computed;
}
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((JsMemberExpression) obj);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = (Object != null ? Object.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (Property != null ? Property.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ Computed.GetHashCode();
return hashCode;
}
}
}
}