Selaa lähdekoodia

Add intermediary prototype for TypeReference (#1231)

Marko Lahma 3 vuotta sitten
vanhempi
commit
c5cd6251a2
2 muutettua tiedostoa jossa 24 lisäystä ja 2 poistoa
  1. 20 0
      Jint.Tests/Runtime/InteropTests.cs
  2. 4 2
      Jint/Runtime/Interop/TypeReference.cs

+ 20 - 0
Jint.Tests/Runtime/InteropTests.cs

@@ -3066,5 +3066,25 @@ namespace Jint.Tests.Runtime
             engine.Realm.GlobalObject.RemoveOwnProperty("o");
             return reference;
         }
+
+        [Fact]
+        public void TypeReferenceShouldGetIntermediaryPrototype()
+        {
+            var engine = new Engine();
+            engine.SetValue("Person", TypeReference.CreateTypeReference<Person>(engine));
+
+            var calls = new List<string>();
+            engine.SetValue("log", new Action<string>(calls.Add));
+
+            engine.Execute("Person.prototype.__defineGetter__('bar', function() { log('called'); return 5 });");
+            engine.Execute("var instance = new Person();");
+            engine.Execute("log(instance.bar)");
+
+            engine.Execute("var z = {};");
+            engine.Execute("z['bar'] = 20;");
+            engine.Execute("log(z['bar']);");
+
+            Assert.Equal("called#5#20", string.Join("#", calls));
+        }
     }
 }

+ 4 - 2
Jint/Runtime/Interop/TypeReference.cs

@@ -13,7 +13,7 @@ namespace Jint.Runtime.Interop
 {
     public sealed class TypeReference : FunctionInstance, IConstructor, IObjectWrapper
     {
-        private static readonly JsString _name = new JsString("typereference");
+        private static readonly JsString _name = new("typereference");
         private static readonly ConcurrentDictionary<Type, MethodDescriptor[]> _constructorCache = new();
         private static readonly ConcurrentDictionary<MemberAccessorKey, ReflectionAccessor> _memberAccessors = new();
 
@@ -26,7 +26,9 @@ namespace Jint.Runtime.Interop
 
             _prototype = engine.Realm.Intrinsics.Function.PrototypeObject;
             _length = PropertyDescriptor.AllForbiddenDescriptor.NumberZero;
-            _prototypeDescriptor = new PropertyDescriptor(engine.Realm.Intrinsics.Object.PrototypeObject, PropertyFlag.AllForbidden);
+
+            var proto = new ObjectInstance(engine);
+            _prototypeDescriptor = new PropertyDescriptor(proto, PropertyFlag.AllForbidden);
 
             PreventExtensions();
         }