Skip to content

Commit 105e207

Browse files
committed
Finished for 0.14
1 parent 196db0d commit 105e207

File tree

3 files changed

+90
-39
lines changed

3 files changed

+90
-39
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using AngleSharp.Attributes;
2+
using Jint.Native.Object;
3+
using Jint.Runtime.Descriptors;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Reflection;
8+
9+
namespace AngleSharp.Js.Cache
10+
{
11+
static class CreatorCache
12+
{
13+
private static readonly Dictionary<Type, Action<EngineInstance, ObjectInstance>> _constructorActions = new Dictionary<Type, Action<EngineInstance, ObjectInstance>>();
14+
15+
public static Action<EngineInstance, ObjectInstance> GetConstructorAction(this Type type)
16+
{
17+
if (!_constructorActions.TryGetValue(type, out var action))
18+
{
19+
var ti = type.GetTypeInfo();
20+
var names = ti.GetCustomAttributes<DomNameAttribute>();
21+
var name = names.FirstOrDefault();
22+
23+
if (name != null && !ti.IsEnum)
24+
{
25+
var info = ti.DeclaredConstructors.FirstOrDefault(m => m.GetCustomAttributes<DomConstructorAttribute>().Any());
26+
action = (engine, obj) =>
27+
{
28+
var constructor = info != null ? new DomConstructorInstance(engine, info) : new DomConstructorInstance(engine, type);
29+
obj.FastSetProperty(name.OfficialName, new PropertyDescriptor(constructor, false, true, false));
30+
};
31+
}
32+
else
33+
{
34+
action = (e, o) => { };
35+
}
36+
37+
_constructorActions.Add(type, action);
38+
}
39+
40+
return action;
41+
}
42+
43+
private static readonly Dictionary<Type, Action<EngineInstance, ObjectInstance>> _instanceActions = new Dictionary<Type, Action<EngineInstance, ObjectInstance>>();
44+
45+
public static Action<EngineInstance, ObjectInstance> GetInstanceAction(this Type type)
46+
{
47+
if (!_instanceActions.TryGetValue(type, out var action))
48+
{
49+
var info = type.GetTypeInfo().DeclaredConstructors.FirstOrDefault(m => m.GetParameters().Length == 0);
50+
51+
if (info != null)
52+
{
53+
var attributes = type.GetTypeInfo().GetCustomAttributes<DomInstanceAttribute>();
54+
action = (engine, obj) =>
55+
{
56+
foreach (var attribute in attributes)
57+
{
58+
var instance = info.Invoke(null);
59+
60+
if (instance != null)
61+
{
62+
var node = engine.GetDomNode(instance);
63+
obj.FastSetProperty(attribute.Name, new PropertyDescriptor(node, false, true, false));
64+
}
65+
}
66+
};
67+
}
68+
else
69+
{
70+
action = (e, o) => { };
71+
}
72+
73+
_instanceActions.Add(type, action);
74+
}
75+
76+
return action;
77+
}
78+
}
79+
}

src/AngleSharp.Js/Extensions/EngineExtensions.cs

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace AngleSharp.Js
22
{
33
using AngleSharp.Attributes;
44
using AngleSharp.Dom;
5+
using AngleSharp.Js.Cache;
56
using Jint;
67
using Jint.Native;
78
using Jint.Native.Object;
@@ -10,7 +11,6 @@ namespace AngleSharp.Js
1011
using Jint.Runtime.Interop;
1112
using System;
1213
using System.Collections.Generic;
13-
using System.Linq;
1414
using System.Reflection;
1515

1616
static class EngineExtensions
@@ -117,7 +117,6 @@ public static Object[] BuildArgs(this EngineInstance context, MethodBase method,
117117
args[i] = parameters[i].ParameterType.GetDefaultValue();
118118
}
119119
}
120-
121120

122121
if (max != parameters.Length)
123122
{
@@ -185,36 +184,14 @@ public static void AddInstances(this EngineInstance engine, ObjectInstance obj,
185184

186185
public static void AddConstructor(this EngineInstance engine, ObjectInstance obj, Type type)
187186
{
188-
var ti = type.GetTypeInfo();
189-
var names = ti.GetCustomAttributes<DomNameAttribute>();
190-
var name = names.FirstOrDefault();
191-
192-
if (name != null && !ti.IsEnum)
193-
{
194-
var info = ti.DeclaredConstructors.FirstOrDefault(m => m.GetCustomAttributes<DomConstructorAttribute>().Any());
195-
var constructor = info != null ? new DomConstructorInstance(engine, info) : new DomConstructorInstance(engine, type);
196-
obj.FastSetProperty(name.OfficialName, new PropertyDescriptor(constructor, false, true, false));
197-
}
187+
var apply = type.GetConstructorAction();
188+
apply.Invoke(engine, obj);
198189
}
199190

200191
public static void AddInstance(this EngineInstance engine, ObjectInstance obj, Type type)
201192
{
202-
var attributes = type.GetTypeInfo().GetCustomAttributes<DomInstanceAttribute>();
203-
var info = type.GetTypeInfo().DeclaredConstructors.FirstOrDefault(m => m.GetParameters().Length == 0);
204-
205-
if (info != null)
206-
{
207-
foreach (var attribute in attributes)
208-
{
209-
var instance = info.Invoke(null);
210-
211-
if (instance != null)
212-
{
213-
var node = engine.GetDomNode(instance);
214-
obj.FastSetProperty(attribute.Name, new PropertyDescriptor(node, false, true, false));
215-
}
216-
}
217-
}
193+
var apply = type.GetInstanceAction();
194+
apply.Invoke(engine, obj);
218195
}
219196

220197
public static JsValue RunScript(this EngineInstance engine, String source) =>

src/AngleSharp.Js/Proxies/DomPrototypeInstance.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ namespace AngleSharp.Js
1313

1414
sealed class DomPrototypeInstance : ObjectInstance
1515
{
16-
private readonly Type _type;
1716
private readonly String _name;
1817
private readonly EngineInstance _instance;
1918

@@ -24,11 +23,10 @@ public DomPrototypeInstance(EngineInstance engine, Type type)
2423
: base(engine.Jint)
2524
{
2625
var baseType = type.GetTypeInfo().BaseType ?? typeof(Object);
27-
_type = type;
2826
_name = type.GetOfficialName(baseType);
2927
_instance = engine;
3028

31-
SetAllMembers();
29+
SetAllMembers(type);
3230
SetExtensionMembers();
3331

3432
// DOM objects can have properties added dynamically
@@ -45,21 +43,18 @@ public Boolean TryGetFromIndex(Object value, String index, out PropertyDescripto
4543

4644
if (_numericIndexer != null && Int32.TryParse(index, out var numericIndex))
4745
{
48-
var args = new Object[] { numericIndex };
49-
5046
try
5147
{
48+
var args = new Object[] { numericIndex };
5249
var orig = _numericIndexer.GetMethod.Invoke(value, args);
53-
var prop = orig.ToJsValue(_instance);
54-
result = new PropertyDescriptor(prop, false, false, false);
50+
result = new PropertyDescriptor(orig.ToJsValue(_instance), false, false, false);
5551
return true;
5652
}
5753
catch (TargetInvocationException ex)
5854
{
5955
if (ex.InnerException is ArgumentOutOfRangeException)
6056
{
61-
var prop = JsValue.Undefined;
62-
result = new PropertyDescriptor(prop, false, false, false);
57+
result = new PropertyDescriptor(JsValue.Undefined, false, false, false);
6358
return true;
6459
}
6560

@@ -93,9 +88,9 @@ private void SetExtensionMembers()
9388
}
9489
}
9590

96-
private void SetAllMembers()
91+
private void SetAllMembers(Type parentType)
9792
{
98-
foreach (var type in _type.GetTypeTree())
93+
foreach (var type in parentType.GetTypeTree())
9994
{
10095
var typeInfo = type.GetTypeInfo();
10196
SetNormalProperties(typeInfo.DeclaredProperties);

0 commit comments

Comments
 (0)