Browse Source

Allow calling Proxy revoke method without this context (#822)

* Added test "ProxyCanBeRevokedWithoutContext"
* Allow calling proxy revoke method without this context
* Allow defining properties on arrow function instances
* Added test for arrow function extensibility
Gökhan Kurt 4 years ago
parent
commit
431a012f37

+ 27 - 0
Jint.Tests/Runtime/FunctionTests.cs

@@ -1,3 +1,4 @@
+using System;
 using Xunit;
 
 namespace Jint.Tests.Runtime
@@ -13,5 +14,31 @@ namespace Jint.Tests.Runtime
             Assert.Equal("a, 1, a, {\"0\":\"a\",\"1\":1,\"2\":\"a\"}", e.Execute("testFunc('a', 1, 'a');").GetCompletionValue().AsString());
             Assert.Equal("a, 1, a, {\"0\":\"a\",\"1\":1,\"2\":\"a\"}", e.Execute("testFunc.bind('anything')('a', 1, 'a');").GetCompletionValue().AsString());
         }
+
+        [Fact]
+        public void ProxyCanBeRevokedWithoutContext()
+        {
+            new Engine()
+                .Execute(@"
+                    var revocable = Proxy.revocable({}, {});
+                    var revoke = revocable.revoke;
+                    revoke.call(null);
+                ");
+        }
+
+        [Fact]
+        public void ArrowFunctionShouldBeExtensible()
+        {
+            new Engine()
+                .SetValue("assert", new Action<bool>(Assert.True))
+                .Execute(@"
+                    var a = () => null
+                    Object.defineProperty(a, 'hello', { enumerable: true, get: () => 'world' })
+                    assert(a.hello === 'world')
+
+                    a.foo = 'bar';
+                    assert(a.foo === 'bar');
+                ");
+        }
     }
 }

+ 0 - 1
Jint/Native/Function/ArrowFunctionInstance.cs

@@ -32,7 +32,6 @@ namespace Jint.Native.Function
         {
             _function = function;
 
-            PreventExtensions();
             _prototype = Engine.Function.PrototypeObject;
 
             _length = new LazyPropertyDescriptor(() => JsNumber.Create(function.Initialize(engine, this).Length), PropertyFlag.Configurable);

+ 12 - 12
Jint/Native/Proxy/ProxyConstructor.cs

@@ -75,20 +75,20 @@ namespace Jint.Native.Proxy
 
         private JsValue Revocable(JsValue thisObject, JsValue[] arguments)
         {
-            var p = _engine.Proxy.Construct(arguments, Undefined);
+            var p = Construct(arguments, thisObject);
+
+            System.Func<JsValue, JsValue[], JsValue> revoke = (JsValue thisObject, JsValue[] arguments) =>
+            {
+                var proxy = (ProxyInstance) p;
+                proxy._handler = null;
+                proxy._target = null;
+                return Undefined;
+            };
+
             var result = _engine.Object.Construct(System.Array.Empty<JsValue>());
-            result.DefineOwnProperty(PropertyRevoke, new PropertyDescriptor(new ClrFunctionInstance(_engine, name: null, Revoke, 0, PropertyFlag.Configurable), PropertyFlag.ConfigurableEnumerableWritable));
-            result.DefineOwnProperty(PropertyProxy, new PropertyDescriptor(Construct(arguments, thisObject), PropertyFlag.ConfigurableEnumerableWritable));
+            result.DefineOwnProperty(PropertyRevoke, new PropertyDescriptor(new ClrFunctionInstance(_engine, name: null, revoke, 0, PropertyFlag.Configurable), PropertyFlag.ConfigurableEnumerableWritable));
+            result.DefineOwnProperty(PropertyProxy, new PropertyDescriptor(p, PropertyFlag.ConfigurableEnumerableWritable));
             return result;
         }
-
-        private JsValue Revoke(JsValue thisObject, JsValue[] arguments)
-        {
-            var o = thisObject.AsObject();
-            var proxy = (ProxyInstance) o.Get(PropertyProxy);
-            proxy._handler = null;
-            proxy._target = null;
-            return Undefined;
-        }
     }
 }