Skip to content

Commit feb9b11

Browse files
committed
Implemented prototype chain
1 parent 39f3f59 commit feb9b11

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

src/AngleSharp.Scripting.JavaScript/DomNodeInstance.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ public DomNodeInstance(EngineInstance engine, Object value)
1919
Prototype = engine.GetDomPrototype(value.GetType());
2020
}
2121

22-
public Object Value
22+
public override String Class
2323
{
24-
get { return _value; }
24+
get { return Prototype.Class; }
2525
}
2626

27-
public override String ToString()
27+
public Object Value
2828
{
29-
return Prototype.ToString();
29+
get { return _value; }
3030
}
3131

3232
public override PropertyDescriptor GetOwnProperty(String propertyName)

src/AngleSharp.Scripting.JavaScript/DomPrototypeInstance.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,22 @@ sealed class DomPrototypeInstance : ObjectInstance
2222
public DomPrototypeInstance(EngineInstance engine, Type type)
2323
: base(engine.Jint)
2424
{
25+
var baseType = type.GetTypeInfo().BaseType;
2526
_type = type;
26-
_name = String.Format("[object {0}]", type.Name);
27+
_name = type.GetOfficialName(baseType);
2728
_engine = engine;
2829

2930
SetAllMembers();
3031
SetPseudoProperties();
3132

3233
// DOM objects can have properties added dynamically
3334
Extensible = true;
34-
Prototype = engine.GetDomPrototype(type.GetTypeInfo().BaseType);
35+
Prototype = engine.GetDomPrototype(baseType);
3536
}
36-
37-
public override String ToString()
37+
38+
public override String Class
3839
{
39-
return _name;
40+
get { return _name; }
4041
}
4142

4243
public Boolean TryGetFromIndex(Object value, String index, out PropertyDescriptor result)

src/AngleSharp.Scripting.JavaScript/Extensions.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,5 +320,24 @@ public static String GetOfficialName(this MemberInfo member)
320320
var officalNameAttribute = names.FirstOrDefault();
321321
return officalNameAttribute != null ? officalNameAttribute.OfficialName : member.Name;
322322
}
323+
324+
public static String GetOfficialName(this Type currentType, Type baseType)
325+
{
326+
var ti = currentType.GetTypeInfo();
327+
var name = ti.GetCustomAttribute<DomNameAttribute>(true)?.OfficialName;
328+
329+
if (name == null)
330+
{
331+
foreach (var impl in ti.ImplementedInterfaces.Except(baseType.GetTypeInfo().ImplementedInterfaces))
332+
{
333+
name = impl.GetTypeInfo().GetCustomAttribute<DomNameAttribute>(false)?.OfficialName;
334+
335+
if (name != null)
336+
break;
337+
}
338+
}
339+
340+
return name ?? currentType.Name;
341+
}
323342
}
324343
}

src/AngleSharp.Scripting.JavaScript/PrototypeCache.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
sealed class PrototypeCache
99
{
1010
private readonly Dictionary<Type, ObjectInstance> _prototypes;
11+
private readonly Engine _engine;
1112

1213
public PrototypeCache(Engine engine)
1314
{
1415
_prototypes = new Dictionary<Type, ObjectInstance>();
15-
_prototypes.Add(typeof(Object), engine.Object);
16+
_prototypes.Add(typeof(Object), engine.Object.PrototypeObject);
17+
_engine = engine;
1618
}
1719

1820
public ObjectInstance GetOrCreate(Type type, Func<Type, ObjectInstance> creator)

0 commit comments

Comments
 (0)