@@ -8,10 +8,12 @@ namespace AngleSharp.Js
88 using Jint.Native;
99 using Jint.Native.Json;
1010 using Jint.Native.Object;
11+ using Jint.Runtime;
1112 using Jint.Runtime.Interop;
1213 using System;
1314 using System.Collections.Generic;
1415 using System.Reflection;
16+ using System.Runtime.CompilerServices;
1517
1618 sealed class EngineInstance
1719 {
@@ -23,6 +25,7 @@ sealed class EngineInstance
2325 private readonly Engine _engine;
2426 private readonly PrototypeCache _prototypes;
2527 private readonly ReferenceCache _references;
28+ private readonly ConditionalWeakTable<Object, SameObjectCache> _sameObjects;
2629 private readonly LibrarySet _libs;
2730 private readonly DomNodeInstance _window;
2831 private readonly JsImportMap _importMap;
@@ -52,6 +55,7 @@ public EngineInstance(IWindow window, IDictionary<String, Object> assignments, I
5255 _libs = new LibrarySet(libs);
5356 _prototypes = new PrototypeCache(_engine, _libs);
5457 _references = new ReferenceCache();
58+ _sameObjects = new ConditionalWeakTable<Object, SameObjectCache>();
5559
5660 foreach (var assignment in assignments)
5761 {
@@ -98,8 +102,35 @@ public EngineInstance(IWindow window, IDictionary<String, Object> assignments, I
98102
99103 public ObjectInstance GetDomNode(Object obj) => _references.GetOrCreate(obj, CreateInstance);
100104
105+ public ObjectInstance GetDomNode(Object obj, Type type) => CreateInstance(obj, type);
106+
101107 public ObjectInstance GetDomPrototype(Type type) => _prototypes.GetOrCreate(type, CreatePrototype);
102108
109+ public JsValue GetSameObject(Object owner, MethodInfo getter)
110+ {
111+ if (getter == null)
112+ {
113+ return JsValue.Undefined;
114+ }
115+
116+ try
117+ {
118+ var current = getter.Invoke(owner, Array.Empty<Object>());
119+
120+ if (current == null)
121+ {
122+ return JsValue.Null;
123+ }
124+
125+ var cache = _sameObjects.GetValue(owner, _ => new SameObjectCache());
126+ return cache.GetOrUpdate(getter, current, () => CreateInstance(current, getter.ReturnType));
127+ }
128+ catch (TargetInvocationException)
129+ {
130+ throw new JavaScriptException(_engine.Intrinsics.Error);
131+ }
132+ }
133+
103134 /// <summary>
104135 /// Gets the constructor object of the given type, building it on first ask. The
105136 /// prototype keeps it, so that naming the type and reading "constructor" off one of
@@ -218,16 +249,18 @@ private JsValue ImportModule(String specifier, String source)
218249 // A collection is projected by the array-like proxy, which lets the engine read its
219250 // indices and length directly; everything else by the ordinary one. The decision is a
220251 // property of the type, so it is resolved once per type for the whole process.
221- private ObjectInstance CreateInstance(Object obj)
252+ private ObjectInstance CreateInstance(Object obj) => CreateInstance(obj, obj.GetType());
253+
254+ private ObjectInstance CreateInstance(Object obj, Type type)
222255 {
223- var collection = obj.GetType() .GetIndexedCollection();
256+ var collection = type .GetIndexedCollection();
224257
225258 if (collection != null)
226259 {
227- return new DomCollectionInstance(this, obj, collection);
260+ return new DomCollectionInstance(this, obj, type, collection);
228261 }
229262
230- return new DomNodeInstance(this, obj);
263+ return new DomNodeInstance(this, obj, type );
231264 }
232265
233266 /// <summary>
@@ -254,7 +287,7 @@ private ObjectInstance GetParentPrototype(Type type, Type baseType) =>
254287
255288 /// <summary>
256289 /// Converts a value handed over from C#, which reaches Jint through JsValue.FromObject
257- /// rather than through <see cref="EngineExtensions.ToJsValue"/>.
290+ /// rather than through <see cref="EngineExtensions.ToJsValue(Object, EngineInstance) "/>.
258291 /// </summary>
259292 /// <remarks>
260293 /// A DOM object has to arrive as the DOM proxy for the very same reason it does when the
@@ -265,6 +298,29 @@ private ObjectInstance GetParentPrototype(Type type, Type baseType) =>
265298 private ObjectInstance WrapObject(Engine engine, Object target, Type type) =>
266299 target.GetType().IsDomType() ? GetDomNode(target) : ObjectWrapper.Create(engine, target, type);
267300
301+ private sealed class SameObjectCache
302+ {
303+ private readonly Dictionary<MethodInfo, ObjectInstance> _entries = new Dictionary<MethodInfo, ObjectInstance>();
304+
305+ public ObjectInstance GetOrUpdate(MethodInfo getter, Object value, Func<ObjectInstance> create)
306+ {
307+ lock (_entries)
308+ {
309+ if (!_entries.TryGetValue(getter, out var instance))
310+ {
311+ instance = create.Invoke();
312+ _entries.Add(getter, instance);
313+ }
314+ else if (instance is IDomProxy proxy)
315+ {
316+ proxy.Update(value);
317+ }
318+
319+ return instance;
320+ }
321+ }
322+ }
323+
268324 #endregion
269325 }
270326}
0 commit comments