Title: [196836] trunk/Source/_javascript_Core
Revision
196836
Author
[email protected]
Date
2016-02-19 14:56:31 -0800 (Fri, 19 Feb 2016)

Log Message

[ES6] Implement Proxy.[[Call]]
https://bugs.webkit.org/show_bug.cgi?id=154425

Reviewed by Mark Lam.

This patch is a straight forward implementation of
Proxy.[[Call]] with respect to section 9.5.12
of the ECMAScript spec.
https://tc39.github.io/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-call-thisargument-argumentslist

* runtime/ProxyObject.cpp:
(JSC::ProxyObject::finishCreation):
(JSC::performProxyGet):
(JSC::ProxyObject::performInternalMethodGetOwnProperty):
(JSC::ProxyObject::performHasProperty):
(JSC::ProxyObject::getOwnPropertySlotByIndex):
(JSC::performProxyCall):
(JSC::ProxyObject::getCallData):
(JSC::ProxyObject::visitChildren):
* runtime/ProxyObject.h:
(JSC::ProxyObject::create):
* tests/es6.yaml:
* tests/stress/proxy-call.js: Added.
(assert):
(throw.new.Error.let.target):
(throw.new.Error.let.handler.apply):
(throw.new.Error):
(assert.let.target):
(assert.let.handler.get apply):
(let.target):
(let.handler.apply):
(i.catch):
(assert.let.handler.apply):

Modified Paths

Added Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (196835 => 196836)


--- trunk/Source/_javascript_Core/ChangeLog	2016-02-19 22:28:24 UTC (rev 196835)
+++ trunk/Source/_javascript_Core/ChangeLog	2016-02-19 22:56:31 UTC (rev 196836)
@@ -1,3 +1,39 @@
+2016-02-19  Saam Barati  <[email protected]>
+
+        [ES6] Implement Proxy.[[Call]]
+        https://bugs.webkit.org/show_bug.cgi?id=154425
+
+        Reviewed by Mark Lam.
+
+        This patch is a straight forward implementation of
+        Proxy.[[Call]] with respect to section 9.5.12
+        of the ECMAScript spec.
+        https://tc39.github.io/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-call-thisargument-argumentslist
+
+        * runtime/ProxyObject.cpp:
+        (JSC::ProxyObject::finishCreation):
+        (JSC::performProxyGet):
+        (JSC::ProxyObject::performInternalMethodGetOwnProperty):
+        (JSC::ProxyObject::performHasProperty):
+        (JSC::ProxyObject::getOwnPropertySlotByIndex):
+        (JSC::performProxyCall):
+        (JSC::ProxyObject::getCallData):
+        (JSC::ProxyObject::visitChildren):
+        * runtime/ProxyObject.h:
+        (JSC::ProxyObject::create):
+        * tests/es6.yaml:
+        * tests/stress/proxy-call.js: Added.
+        (assert):
+        (throw.new.Error.let.target):
+        (throw.new.Error.let.handler.apply):
+        (throw.new.Error):
+        (assert.let.target):
+        (assert.let.handler.get apply):
+        (let.target):
+        (let.handler.apply):
+        (i.catch):
+        (assert.let.handler.apply):
+
 2016-02-19  Csaba Osztrogonác  <[email protected]>
 
         Remove more LLVM related dead code after r196729

Modified: trunk/Source/_javascript_Core/runtime/ProxyObject.cpp (196835 => 196836)


--- trunk/Source/_javascript_Core/runtime/ProxyObject.cpp	2016-02-19 22:28:24 UTC (rev 196835)
+++ trunk/Source/_javascript_Core/runtime/ProxyObject.cpp	2016-02-19 22:56:31 UTC (rev 196836)
@@ -65,7 +65,11 @@
         return;
     }
 
-    m_target.set(vm, this, jsCast<JSObject*>(target));
+    CallData ignored;
+    JSObject* targetAsObject = jsCast<JSObject*>(target);
+    m_isCallable = targetAsObject->methodTable(vm)->getCallData(targetAsObject, ignored) != CallTypeNone;
+
+    m_target.set(vm, this, targetAsObject);
     m_handler.set(vm, this, handler);
 }
 
@@ -105,8 +109,6 @@
     MarkedArgumentBuffer arguments;
     arguments.append(target);
     arguments.append(identifierToSafePublicJSValue(vm, Identifier::fromUid(&vm, propertyName.uid())));
-    if (exec->hadException())
-        return JSValue::encode(jsUndefined());
     arguments.append(thisObject);
     JSValue trapResult = call(exec, getHandler, callType, callData, handler, arguments);
     if (exec->hadException())
@@ -152,8 +154,6 @@
     MarkedArgumentBuffer arguments;
     arguments.append(target);
     arguments.append(identifierToSafePublicJSValue(vm, Identifier::fromUid(&vm, propertyName.uid())));
-    if (exec->hadException())
-        return false;
     JSValue trapResult = call(exec, getOwnPropertyDescriptorMethod, callType, callData, handler, arguments);
     if (exec->hadException())
         return false;
@@ -234,8 +234,6 @@
     MarkedArgumentBuffer arguments;
     arguments.append(target);
     arguments.append(identifierToSafePublicJSValue(vm, Identifier::fromUid(&vm, propertyName.uid())));
-    if (exec->hadException())
-        return false;
     JSValue trapResult = call(exec, hasMethod, callType, callData, handler, arguments);
     if (exec->hadException())
         return false;
@@ -298,6 +296,51 @@
     return thisObject->getOwnPropertySlotCommon(exec, ident.impl(), slot);
 }
 
+static EncodedJSValue JSC_HOST_CALL performProxyCall(ExecState* exec)
+{
+    VM& vm = exec->vm();
+    ProxyObject* proxy = jsCast<ProxyObject*>(exec->callee());
+    JSValue handlerValue = proxy->handler();
+    if (handlerValue.isNull())
+        return throwVMTypeError(exec, ASCIILiteral("Proxy 'handler' is null. It should be an Object."));
+
+    JSObject* handler = jsCast<JSObject*>(handlerValue);
+    CallData callData;
+    CallType callType;
+    JSValue applyMethod = handler->getMethod(exec, callData, callType, makeIdentifier(vm, "apply"), ASCIILiteral("'apply' property of a Proxy's handler should be callable."));
+    if (exec->hadException())
+        return JSValue::encode(jsUndefined());
+    JSObject* target = proxy->target();
+    if (applyMethod.isUndefined()) {
+        CallData callData;
+        CallType callType = target->methodTable(vm)->getCallData(target, callData);
+        RELEASE_ASSERT(callType != CallTypeNone);
+        return JSValue::encode(call(exec, target, callType, callData, exec->thisValue(), ArgList(exec)));
+    }
+
+    JSArray* argArray = constructArray(exec, static_cast<ArrayAllocationProfile*>(nullptr), ArgList(exec));
+    if (exec->hadException())
+        return JSValue::encode(jsUndefined());
+    MarkedArgumentBuffer arguments;
+    arguments.append(target);
+    arguments.append(exec->thisValue());
+    arguments.append(argArray);
+    return JSValue::encode(call(exec, applyMethod, callType, callData, handler, arguments));
+}
+
+CallType ProxyObject::getCallData(JSCell* cell, CallData& callData)
+{
+    ProxyObject* proxy = jsCast<ProxyObject*>(cell);
+    if (!proxy->m_isCallable) {
+        callData.js.functionExecutable = nullptr;
+        callData.js.scope = nullptr;
+        return CallTypeNone;
+    }
+
+    callData.native.function = performProxyCall;
+    return CallTypeHost;
+}
+
 void ProxyObject::visitChildren(JSCell* cell, SlotVisitor& visitor)
 {
     ProxyObject* thisObject = jsCast<ProxyObject*>(cell);

Modified: trunk/Source/_javascript_Core/runtime/ProxyObject.h (196835 => 196836)


--- trunk/Source/_javascript_Core/runtime/ProxyObject.h	2016-02-19 22:28:24 UTC (rev 196835)
+++ trunk/Source/_javascript_Core/runtime/ProxyObject.h	2016-02-19 22:56:31 UTC (rev 196836)
@@ -35,7 +35,7 @@
 public:
     typedef JSNonFinalObject Base;
 
-    const static unsigned StructureFlags = Base::StructureFlags | OverridesGetOwnPropertySlot;
+    const static unsigned StructureFlags = Base::StructureFlags | OverridesGetOwnPropertySlot | TypeOfShouldCallGetCallData;
 
     static ProxyObject* create(ExecState* exec, Structure* structure, JSValue target, JSValue handler)
     {
@@ -61,6 +61,7 @@
 
     static bool getOwnPropertySlot(JSObject*, ExecState*, PropertyName, PropertySlot&);
     static bool getOwnPropertySlotByIndex(JSObject*, ExecState*, unsigned propertyName, PropertySlot&);
+    static CallType getCallData(JSCell*, CallData&);
     static void visitChildren(JSCell*, SlotVisitor&);
 
     bool getOwnPropertySlotCommon(ExecState*, PropertyName, PropertySlot&);
@@ -69,6 +70,7 @@
 
     WriteBarrier<JSObject> m_target;
     WriteBarrier<Unknown> m_handler;
+    bool m_isCallable : 1;
 };
 
 } // namespace JSC

Modified: trunk/Source/_javascript_Core/tests/es6.yaml (196835 => 196836)


--- trunk/Source/_javascript_Core/tests/es6.yaml	2016-02-19 22:28:24 UTC (rev 196835)
+++ trunk/Source/_javascript_Core/tests/es6.yaml	2016-02-19 22:56:31 UTC (rev 196836)
@@ -907,7 +907,7 @@
 - path: es6/prototype_of_bound_functions_subclasses.js
   cmd: runES6 :fail
 - path: es6/Proxy_apply_handler.js
-  cmd: runES6 :fail
+  cmd: runES6 :normal
 - path: es6/Proxy_Array.isArray_support.js
   cmd: runES6 :fail
 - path: es6/Proxy_construct_handler.js
@@ -975,7 +975,7 @@
 - path: es6/Proxy_internal_get_calls_Error.prototype.toString.js
   cmd: runES6 :normal
 - path: es6/Proxy_internal_get_calls_Function.prototype.bind.js
-  cmd: runES6 :fail
+  cmd: runES6 :normal
 - path: es6/Proxy_internal_get_calls_HasBinding.js
   cmd: runES6 :normal
 - path: es6/Proxy_internal_get_calls_instanceof_operator.js
@@ -1021,7 +1021,7 @@
 - path: es6/Proxy_internal_getOwnPropertyDescriptor_calls_[[Set]].js
   cmd: runES6 :fail
 - path: es6/Proxy_internal_getOwnPropertyDescriptor_calls_Function.prototype.bind.js
-  cmd: runES6 :fail
+  cmd: runES6 :normal
 - path: es6/Proxy_internal_getOwnPropertyDescriptor_calls_Object.assign.js
   cmd: runES6 :fail
 - path: es6/Proxy_internal_getOwnPropertyDescriptor_calls_Object.prototype.hasOwnProperty.js

Added: trunk/Source/_javascript_Core/tests/stress/proxy-call.js (0 => 196836)


--- trunk/Source/_javascript_Core/tests/stress/proxy-call.js	                        (rev 0)
+++ trunk/Source/_javascript_Core/tests/stress/proxy-call.js	2016-02-19 22:56:31 UTC (rev 196836)
@@ -0,0 +1,394 @@
+function assert(b) {
+    if (!b)
+        throw new Error("Bad assertion");
+}
+
+{
+    let target = function foo(...args) {
+        assert(args[0] === 10);
+        assert(args[1] === 20);
+        return "foo";
+    }
+    let handler = {
+        apply: function(theTarget, thisArg, argArray) {
+            assert(theTarget === target);
+            assert(argArray[0] === 10);
+            assert(argArray[1] === 20);
+            return theTarget.apply(thisArg, argArray);
+        }
+    };
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i < 500; i++) {
+        assert(proxy(10, 20) === "foo");
+    }
+}
+
+{
+    let target = function foo() { }
+    let error = null;
+    let handler = {
+        get apply() {
+            error = new Error();
+            throw error;
+        }
+    };
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i < 500; i++) {
+        let threw = false;
+        try {
+            proxy(10, 20);
+        } catch(e) {
+            assert(e === error);
+            threw = true;
+        }
+        assert(threw);
+    }
+}
+
+{
+    let called = false;
+    let globalThis = this;
+    let target = function foo() {
+        assert(this === globalThis);
+        called = true;
+    }
+    let handler = {
+        apply: null
+    };
+
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i < 500; i++) {
+        proxy();
+        assert(called);
+        called = false;
+    }
+}
+
+{
+    let called = false;
+    let globalThis = this;
+    let target = function foo() {
+        assert(this === globalThis);
+        called = true;
+    }
+    let handler = {
+        apply: undefined
+    };
+
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i < 500; i++) {
+        proxy();
+        assert(called);
+        called = false;
+    }
+}
+
+{
+    let called = false;
+    let thisValue = {};
+    let target = function foo(x, y, z) {
+        assert(this === thisValue);
+        assert(x === 20);
+        assert(y === 45);
+        assert(z === "foo");
+        called = true;
+    }
+
+    let handler = {
+        apply: undefined
+    };
+
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i < 500; i++) {
+        proxy.call(thisValue, 20, 45, "foo");
+        assert(called);
+        called = false;
+        proxy.apply(thisValue, [20, 45, "foo"]);
+        assert(called);
+        called = false;
+    }
+}
+
+{
+    let called = false;
+    let thisValue = {};
+    let target = function foo(x, y, z) {
+        assert(this === thisValue);
+        assert(x === 20);
+        assert(y === 45);
+        assert(z === "foo");
+        called = true;
+        return this;
+    }
+
+    let handler = {
+        apply: function(theTarget, thisArg, argArray) {
+            assert(theTarget === target);
+            assert(argArray[0] === 20);
+            assert(argArray[1] === 45);
+            assert(argArray[2] === "foo");
+            assert(thisArg === thisValue);
+            return theTarget.apply(thisArg, argArray);
+        }
+    };
+
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i < 500; i++) {
+        assert(proxy.call(thisValue, 20, 45, "foo") === thisValue);
+        assert(called);
+        called = false;
+        assert(proxy.apply(thisValue, [20, 45, "foo"]) === thisValue);
+        assert(called);
+        called = false;
+    }
+}
+
+{
+    let called = false;
+    let target = Error;
+
+    let handler = {
+        apply: function(theTarget, thisArg, argArray) {
+            called = true;
+            assert(theTarget === Error);
+            return theTarget.apply(thisArg, argArray);
+        }
+    };
+
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i < 500; i++) {
+        let error = proxy();
+        assert(!!error.stack);
+        called = false;
+    }
+}
+
+{
+    let called = false;
+    let self = this;
+    let target = (x) => {
+        called = true;
+        assert(this === self);
+        return x;
+    };
+
+    let handler = { };
+
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i < 500; i++) {
+        let result = proxy(i);
+        assert(result === i);
+        called = false;
+    }
+}
+
+{
+    let called = false;
+    let self = this;
+    let target = (x) => {
+        assert(this === self);
+        return x;
+    };
+
+    let handler = {
+        apply: function(theTarget, thisArg, argArray) {
+            called = true;
+            assert(theTarget === target);
+            return theTarget.apply(thisArg, argArray);
+        }
+    };
+
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i < 500; i++) {
+        let result = proxy(i);
+        assert(result === i);
+        called = false;
+    }
+}
+
+{
+    let called = false;
+    let self = this;
+    let target = (x) => {
+        assert(this === self);
+        return x;
+    };
+
+    let handler = {
+        apply: function(theTarget, thisArg, argArray) {
+            called = true;
+            assert(theTarget === target);
+            return theTarget.apply(null, argArray);
+        }
+    };
+
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i < 500; i++) {
+        let result = proxy(i);
+        assert(called);
+        assert(result === i);
+        called = false;
+    }
+}
+
+{
+    let called = false;
+    let target = (x) => { };
+    let error = null;
+
+    let handler = {
+        apply: function(theTarget, thisArg, argArray) {
+            error = new Error();
+            throw error;
+        }
+    };
+
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i < 500; i++) {
+        let threw = false;
+        try {
+            proxy();
+        } catch(e) {
+            assert(e === error);
+            threw = true;
+        }
+        assert(threw);
+    }
+}
+
+{
+    let called = false;
+    let error = null;
+    let target = (x) => {
+        error = new Error();
+        throw error;
+    };
+
+    let handler = {
+        apply: function(theTarget, thisArg, argArray) {
+            assert(theTarget === target);
+            return theTarget.apply(null, argArray);
+        }
+    };
+
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i < 500; i++) {
+        let threw = false;
+        try {
+            proxy();
+        } catch(e) {
+            assert(e === error);
+            threw = true;
+        }
+        assert(threw);
+    }
+}
+
+{
+    let called = false;
+    let error = null;
+    let target = new Proxy((x) => x, {});
+
+    let handler = {
+        apply: function(theTarget, thisArg, argArray) {
+            assert(theTarget === target);
+            called = true;
+            return theTarget.apply(null, argArray);
+        }
+    };
+
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i < 500; i++) {
+        assert(proxy(i) === i);
+        assert(called);
+        called = false;
+    }
+}
+
+{
+    let target = (x) => x;
+    let handler = {
+        apply: function(theTarget, thisArg, argArray) {
+            return theTarget.apply(thisArg, argArray);
+        }
+    };
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i < 500; i++) {
+        assert(typeof proxy === "function");
+    }
+}
+
+{
+    let target = function() { }
+    let handler = {
+        apply: function(theTarget, thisArg, argArray) {
+            return theTarget.apply(thisArg, argArray);
+        }
+    };
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i < 500; i++) {
+        assert(typeof proxy === "function");
+    }
+}
+
+{
+    let target = Error;
+    let handler = {
+        apply: function(theTarget, thisArg, argArray) {
+            return theTarget.apply(thisArg, argArray);
+        }
+    };
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i < 500; i++) {
+        assert(typeof proxy === "function");
+    }
+}
+
+{
+    let target = (function foo() { }).bind({});
+    let handler = {
+        apply: function(theTarget, thisArg, argArray) {
+            return theTarget.apply(thisArg, argArray);
+        }
+    };
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i < 500; i++) {
+        assert(typeof proxy === "function");
+    }
+}
+
+{
+    let target = function() { };
+    let handler = {};
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i < 500; i++) {
+        assert(typeof proxy === "function");
+    }
+}
+
+{
+    let target = {};
+    let handler = {};
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i < 500; i++) {
+        assert(typeof proxy === "object");
+    }
+}
+
+{
+    let target = [];
+    let handler = {};
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i < 500; i++) {
+        assert(typeof proxy === "object");
+    }
+}
+
+{
+    let target = new String("foo");
+    let handler = {};
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i < 500; i++) {
+        assert(typeof proxy === "object");
+    }
+}
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to