forked from AngleSharp/AngleSharp.Js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDomPrototypeInstance.cs
More file actions
261 lines (229 loc) · 9.83 KB
/
Copy pathDomPrototypeInstance.cs
File metadata and controls
261 lines (229 loc) · 9.83 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
namespace AngleSharp.Js
{
using AngleSharp.Attributes;
using AngleSharp.Text;
using Jint.Native.Object;
using Jint.Native.Symbol;
using Jint.Runtime.Descriptors;
using Jint.Runtime.Interop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
sealed class DomPrototypeInstance : ObjectInstance
{
private readonly String _name;
private readonly EngineInstance _instance;
private PropertyInfo _numericIndexer;
private PropertyInfo _stringIndexer;
public DomPrototypeInstance(EngineInstance engine, Type type)
: base(engine.Jint)
{
var baseType = type.GetTypeInfo().BaseType ?? typeof(Object);
_name = type.GetOfficialName(baseType);
_instance = engine;
Set(GlobalSymbolRegistry.ToStringTag, _name);
SetAllMembers(type);
SetExtensionMembers();
// DOM objects can have properties added dynamically
Prototype = engine.GetDomPrototype(baseType);
}
public Boolean TryGetFromIndex(Object value, String index, out PropertyDescriptor result)
{
// If we have a numeric indexer and the property is numeric
result = default;
if (_numericIndexer != null && Int32.TryParse(index, out var numericIndex))
{
try
{
var args = new Object[] { numericIndex };
var orig = _numericIndexer.GetMethod.Invoke(value, args);
result = new PropertyDescriptor(orig.ToJsValue(_instance), false, false, false);
return true;
}
catch (TargetInvocationException ex)
{
if (ex.InnerException is ArgumentOutOfRangeException)
{
result = PropertyDescriptor.Undefined;
return true;
}
throw;
}
}
// Else a string property
// If we have a string indexer and no property exists for this name then use the string indexer
// Jint possibly has a limitation here - if an object has a string indexer. How do we know whether to use the defined indexer or a property?
// Eg. object.callMethod1() vs object['callMethod1'] is not necessarily the same if the object has a string indexer?? (I'm not an ECMA expert!)
// node.attributes is one such object - has both a string and numeric indexer
// This GetOwnProperty override might need an additional parameter to let us know this was called via an indexer
if (_stringIndexer != null && !HasProperty(index))
{
var args = new Object[] { index };
var valueAtIndex = _stringIndexer.GetMethod.Invoke(value, args);
if (valueAtIndex == null)
{
result = PropertyDescriptor.Undefined;
return false;
}
var prop = valueAtIndex.ToJsValue(_instance);
result = new PropertyDescriptor(prop, false, false, false);
return true;
}
return false;
}
private void SetExtensionMembers()
{
foreach (var type in _instance.Libs.GetExtensionTypes(_name))
{
var typeInfo = type.GetTypeInfo();
SetExtensionMethods(typeInfo.DeclaredMethods);
}
}
private void SetAllMembers(Type parentType)
{
foreach (var type in parentType.GetTypeTree())
{
var typeInfo = type.GetTypeInfo();
SetNormalProperties(typeInfo.DeclaredProperties);
SetNormalMethods(typeInfo.DeclaredMethods);
SetNormalEvents(typeInfo.DeclaredEvents);
}
}
private void SetNormalEvents(IEnumerable<EventInfo> eventInfos)
{
foreach (var eventInfo in eventInfos)
{
foreach (var m in eventInfo.GetCustomAttributes<DomNameAttribute>())
{
SetEvent(m.OfficialName, eventInfo.AddMethod, eventInfo.RemoveMethod);
}
}
}
private void SetExtensionMethods(IEnumerable<MethodInfo> methods)
{
foreach (var entry in methods.GetExtensions())
{
var name = entry.Key;
var value = entry.Value;
if (HasProperty(name))
{
// skip
}
else if (value.Adder != null && value.Remover != null)
{
SetEvent(name, value.Adder, value.Remover);
}
else if (value.Getter != null || value.Setter != null)
{
SetProperty(name, value.Getter, value.Setter, value.Forward);
}
else if (value.Other != null)
{
SetMethod(name, value.Other);
}
}
}
private void SetNormalProperties(IEnumerable<PropertyInfo> properties)
{
foreach (var property in properties)
{
var indexParameters = property.GetIndexParameters();
var accessor = property.GetCustomAttribute<DomAccessorAttribute>()?.Type;
var putsForward = property.GetCustomAttribute<DomPutForwardsAttribute>();
var names = property
.GetCustomAttributes<DomNameAttribute>()
.Select(m => m.OfficialName)
.ToArray();
if (accessor == Accessors.Method)
{
// property decorated with Method accessor, so we need to treat it as a method, not a property
if (property.GetMethod == null)
{
throw new InvalidOperationException("Getter not found.");
}
foreach (var name in names)
{
SetMethod(name, property.GetMethod);
}
// methods were set, so finish processing
return;
}
if (accessor == Accessors.Getter || accessor == Accessors.Setter || Array.Exists(names, m => m.Is("item")))
{
SetIndexer(property, indexParameters);
}
foreach (var name in names)
{
SetProperty(name, property.GetMethod, property.SetMethod, putsForward);
}
}
}
private void SetNormalMethods(IEnumerable<MethodInfo> methods)
{
foreach (var method in methods)
{
foreach (var m in method.GetCustomAttributes<DomNameAttribute>())
{
SetMethod(m.OfficialName, method);
}
}
}
private void SetEvent(String name, MethodInfo adder, MethodInfo remover)
{
var eventInstance = new DomEventInstance(_instance, adder, remover);
FastSetProperty(name, new GetSetPropertyDescriptor(eventInstance.Getter, eventInstance.Setter, false, false));
}
private void SetProperty(String name, MethodInfo getter, MethodInfo setter, DomPutForwardsAttribute putsForward)
{
FastSetProperty(name, new GetSetPropertyDescriptor(
new ClrFunction(_instance.Jint, name, (obj, values) =>
_instance.Call(getter, obj, values)),
new ClrFunction(_instance.Jint, name, (obj, values) =>
{
if (putsForward != null)
{
var ep = Array.Empty<Object>();
var that = obj as DomNodeInstance;
var target = getter.Invoke(that.Value, ep);
var propName = putsForward.PropertyName;
var prop = getter.ReturnType
.GetInheritedProperties()
.FirstOrDefault(m => m.GetCustomAttributes<DomNameAttribute>().Any(n => n.OfficialName.Is(propName)));
var args = _instance.BuildArgs(prop.SetMethod, values);
prop.SetMethod.Invoke(target, args);
return prop.GetMethod.Invoke(target, ep).ToJsValue(_instance);
}
return _instance.Call(setter, obj, values);
}), false, false));
}
private void SetIndexer(PropertyInfo property, ParameterInfo[] indexParameters)
{
if (indexParameters.Length == 1)
{
if (indexParameters[0].ParameterType == typeof(Int32))
{
_numericIndexer = property;
}
else if (indexParameters[0].ParameterType == typeof(String))
{
_stringIndexer = property;
}
}
}
private void SetMethod(String name, MethodInfo method)
{
//TODO Jint
// If it already has a property with the given name (usually another method),
// then convert that method to a two-layer method, which decides which one
// to pick depending on the number (and probably types) of arguments.
if (!HasProperty(name))
{
FastSetProperty(name, new PropertyDescriptor(
new ClrFunction(_instance.Jint, name, (obj, values) =>
_instance.Call(method, obj, values)
), false, false, false));
}
}
}
}