Bläddra i källkod

JsProxy should return proxied target's ToObject via ToObject (#1675)

Marko Lahma 1 år sedan
förälder
incheckning
cbe3b6f6c8
2 ändrade filer med 41 tillägg och 0 borttagningar
  1. 39 0
      Jint.Tests/Runtime/ProxyTests.cs
  2. 2 0
      Jint/Native/Proxy/JsProxy.cs

+ 39 - 0
Jint.Tests/Runtime/ProxyTests.cs

@@ -236,6 +236,17 @@ public class ProxyTests
 
         private int x = 1;
         public int PropertySideEffect => x++;
+
+        public string Name => "My Name is Test";
+
+        public void SayHello()
+        {
+        }
+
+        public int Add(int a, int b)
+        {
+            return a + b;
+        }
     }
 
     [Fact]
@@ -443,4 +454,32 @@ public class ProxyTests
         Assert.Equal(2, _engine.Evaluate("p.PropertySideEffect").AsInteger()); // no call to PropertySideEffect
         Assert.Equal(2, TestClass.Instance.PropertySideEffect); // second call to PropertySideEffect
     }
+
+   [Fact]
+    public void ToObjectReturnsProxiedToObject()
+    {
+        _engine
+            .SetValue("T", new TestClass())
+            .Execute("""
+                 const handler = {
+                     get(target, property, receiver) {
+
+                         if (!target[property]) {
+                             return (...args) => "Not available";
+                         }
+
+                         // return Reflect.get(target, property, receiver);
+                         return Reflect.get(...arguments);
+                     }
+                 };
+
+                 const p = new Proxy(T, handler);
+                 const name = p.Name;  // works
+                 const s = p.GetX();   // works because method does NOT exist on clr object
+
+                 p.SayHello();          // throws System.Reflection.TargetException: 'Object does not match target type.'
+                 const t = p.Add(5,3);  // throws System.Reflection.TargetException: 'Object does not match target type.'
+             """);
+
+    }
 }

+ 2 - 0
Jint/Native/Proxy/JsProxy.cs

@@ -100,6 +100,8 @@ namespace Jint.Native.Proxy
             return _target.IsArray();
         }
 
+        public override object ToObject() => _target.ToObject();
+
         internal override bool IsConstructor
         {
             get