Title: [185493] trunk/Source/WebCore
Revision
185493
Author
[email protected]
Date
2015-06-12 02:25:46 -0700 (Fri, 12 Jun 2015)

Log Message

Bindings generator should generate code for Promise-based APIs
https://bugs.webkit.org/show_bug.cgi?id=145833

Reviewed by Darin Adler.

Covered by existing tests.

* Modules/webaudio/AudioContext.idl: Removing custom binding for resume, suspend and close.
* bindings/js/JSAudioContextCustom.cpp: Ditto.
* bindings/scripts/CodeGeneratorJS.pm:
(GenerateHeader): Refactoring to use IsReturningPromise.
(GenerateImplementation): Disabling include for return type if it is a promise.
(GenerateParametersCheck): Adding DeferredWrapper() as argument to the DOM method if JS method returns a promise.
(GenerateImplementationFunctionCall): Added support for promise-returning API.
(IsReturningPromise): Checking whether function is returning a promise.
* bindings/scripts/test/GObject/WebKitDOMTestObj.cpp:
(webkit_dom_test_obj_test_promise_function):
* bindings/scripts/test/GObject/WebKitDOMTestObj.h:
* bindings/scripts/test/JS/JSTestObj.cpp:
(WebCore::jsTestObjPrototypeFunctionTestPromiseFunction):
* bindings/scripts/test/ObjC/DOMTestObj.h:
* bindings/scripts/test/ObjC/DOMTestObj.mm:
(-[DOMTestObj testPromiseFunction]):
* bindings/scripts/test/TestObj.idl: Adding promise returning function.

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (185492 => 185493)


--- trunk/Source/WebCore/ChangeLog	2015-06-12 08:19:36 UTC (rev 185492)
+++ trunk/Source/WebCore/ChangeLog	2015-06-12 09:25:46 UTC (rev 185493)
@@ -1,3 +1,31 @@
+2015-06-12  Youenn Fablet  <[email protected]>
+
+        Bindings generator should generate code for Promise-based APIs
+        https://bugs.webkit.org/show_bug.cgi?id=145833
+
+        Reviewed by Darin Adler.
+
+        Covered by existing tests.
+
+        * Modules/webaudio/AudioContext.idl: Removing custom binding for resume, suspend and close.
+        * bindings/js/JSAudioContextCustom.cpp: Ditto.
+        * bindings/scripts/CodeGeneratorJS.pm:
+        (GenerateHeader): Refactoring to use IsReturningPromise.
+        (GenerateImplementation): Disabling include for return type if it is a promise.
+        (GenerateParametersCheck): Adding DeferredWrapper() as argument to the DOM method if JS method returns a promise.
+        (GenerateImplementationFunctionCall): Added support for promise-returning API.
+        (IsReturningPromise): Checking whether function is returning a promise.
+        * bindings/scripts/test/GObject/WebKitDOMTestObj.cpp:
+        (webkit_dom_test_obj_test_promise_function):
+        * bindings/scripts/test/GObject/WebKitDOMTestObj.h:
+        * bindings/scripts/test/JS/JSTestObj.cpp:
+        (WebCore::jsTestObjPrototypeFunctionTestPromiseFunction):
+        * bindings/scripts/test/ObjC/DOMTestObj.h:
+        * bindings/scripts/test/ObjC/DOMTestObj.mm:
+        (-[DOMTestObj testPromiseFunction]):
+        * bindings/scripts/test/TestObj.idl: Adding promise returning function.
+
+
 2015-06-12  Manuel Rego Casasnovas  <[email protected]>
 
         [CSS Grid Layout] Fix grid-template-areas parsing to avoid spaces

Modified: trunk/Source/WebCore/Modules/webaudio/AudioContext.idl (185492 => 185493)


--- trunk/Source/WebCore/Modules/webaudio/AudioContext.idl	2015-06-12 08:19:36 UTC (rev 185492)
+++ trunk/Source/WebCore/Modules/webaudio/AudioContext.idl	2015-06-12 09:25:46 UTC (rev 185493)
@@ -50,9 +50,9 @@
     // All panning is relative to this listener.
     readonly attribute AudioListener listener;
 
-    [Custom] Promise suspend();
-    [Custom] Promise resume();
-    [Custom] Promise close();
+    Promise suspend();
+    Promise resume();
+    Promise close();
 
     readonly attribute AudioContextState state;
     attribute EventHandler onstatechange;

Modified: trunk/Source/WebCore/bindings/js/JSAudioContextCustom.cpp (185492 => 185493)


--- trunk/Source/WebCore/bindings/js/JSAudioContextCustom.cpp	2015-06-12 08:19:36 UTC (rev 185492)
+++ trunk/Source/WebCore/bindings/js/JSAudioContextCustom.cpp	2015-06-12 09:25:46 UTC (rev 185493)
@@ -111,33 +111,6 @@
     return JSValue::encode(CREATE_DOM_WRAPPER(jsConstructor->globalObject(), AudioContext, audioContext.get()));
 }
 
-JSValue JSAudioContext::suspend(ExecState* state)
-{
-    JSPromiseDeferred* promiseDeferred = JSPromiseDeferred::create(state, globalObject());
-
-    impl().suspend(DeferredWrapper(state, globalObject(), promiseDeferred));
-
-    return promiseDeferred->promise();
-}
-
-JSValue JSAudioContext::resume(ExecState* state)
-{
-    JSPromiseDeferred* promiseDeferred = JSPromiseDeferred::create(state, globalObject());
-
-    impl().resume(DeferredWrapper(state, globalObject(), promiseDeferred));
-
-    return promiseDeferred->promise();
-}
-
-JSValue JSAudioContext::close(ExecState* state)
-{
-    JSPromiseDeferred* promiseDeferred = JSPromiseDeferred::create(state, globalObject());
-
-    impl().close(DeferredWrapper(state, globalObject(), promiseDeferred));
-
-    return promiseDeferred->promise();
-}
-
 } // namespace WebCore
 
 #endif // ENABLE(WEB_AUDIO)

Modified: trunk/Source/WebCore/bindings/scripts/CodeGeneratorJS.pm (185492 => 185493)


--- trunk/Source/WebCore/bindings/scripts/CodeGeneratorJS.pm	2015-06-12 08:19:36 UTC (rev 185492)
+++ trunk/Source/WebCore/bindings/scripts/CodeGeneratorJS.pm	2015-06-12 09:25:46 UTC (rev 185493)
@@ -1047,7 +1047,7 @@
                 $needsVisitChildren = 1;
                 push(@headerContent, "#endif\n") if $conditionalString;
             }
-            elsif ($attribute->signature->type eq "Promise") {
+            elsif (IsReturningPromise($attribute)) {
                 $headerIncludes{"JSDOMPromise.h"} = 1;
 
                 my $conditionalString = $codeGenerator->GenerateConditionalString($attribute->signature);
@@ -2759,7 +2759,7 @@
 
             next if $isCustom && $isOverloaded && $function->{overloadIndex} > 1;
 
-            AddIncludesForTypeInImpl($function->signature->type) unless $isCustom;
+            AddIncludesForTypeInImpl($function->signature->type) unless $isCustom or IsReturningPromise($function);
 
             my $functionName = GetFunctionName($className, $function);
 
@@ -3386,6 +3386,8 @@
         $argsIndex++;
     }
 
+    push(@arguments, "DeferredWrapper(exec, castedThis->globalObject(), promiseDeferred)") if IsReturningPromise($function);
+
     push(@arguments, "ec") if $raisesException;
 
     return ("$functionName(" . join(", ", @arguments) . ")", scalar @arguments);
@@ -3654,9 +3656,17 @@
             push(@implContent, "#else\n");
             push(@implContent, $indent . "result = " . NativeToJSValue($function->signature, 1, $interfaceName, $functionString, $thisObject) . ";\n");
             push(@implContent, "#endif\n");
+        } elsif (IsReturningPromise($function)) {
+            AddToImplIncludes("JSDOMPromise.h");
+
+            push(@implContent, $indent . "JSPromiseDeferred* promiseDeferred = JSPromiseDeferred::create(exec, castedThis->globalObject());\n");
+            push(@implContent, $indent . $functionString . ";\n");
+            push(@implContent, $indent . "JSValue result = promiseDeferred->promise();\n");
+
         } else {
             push(@implContent, $indent . "JSValue result = " . NativeToJSValue($function->signature, 1, $interfaceName, $functionString, $thisObject) . ";\n");
         }
+        # FIXME: In case of IsReturningPromise($function), the function should not throw. Exception should be used to reject the promise callback.
         push(@implContent, "\n" . $indent . "setDOMException(exec, ec);\n") if $raisesException;
 
         if ($codeGenerator->ExtendedAttributeContains($function->signature->extendedAttributes->{"CallWith"}, "ScriptState")) {
@@ -4774,6 +4784,13 @@
     return !$interface->extendedAttributes->{"NoInterfaceObject"} || $interface->extendedAttributes->{"CustomConstructor"};
 }
 
+sub IsReturningPromise
+{
+    my $function = shift;
+
+    return $function->signature->type eq "Promise" if ($function->signature->type);
+}
+
 sub IsConstructable
 {
     my $interface = shift;

Modified: trunk/Source/WebCore/bindings/scripts/test/GObject/WebKitDOMTestObj.cpp (185492 => 185493)


--- trunk/Source/WebCore/bindings/scripts/test/GObject/WebKitDOMTestObj.cpp	2015-06-12 08:19:36 UTC (rev 185492)
+++ trunk/Source/WebCore/bindings/scripts/test/GObject/WebKitDOMTestObj.cpp	2015-06-12 09:25:46 UTC (rev 185493)
@@ -33,6 +33,7 @@
 #include "WebKitDOMDocumentPrivate.h"
 #include "WebKitDOMNodePrivate.h"
 #include "WebKitDOMPrivate.h"
+#include "WebKitDOMPromisePrivate.h"
 #include "WebKitDOMSVGPointPrivate.h"
 #include "WebKitDOMTestEnumTypePrivate.h"
 #include "WebKitDOMTestNodePrivate.h"
@@ -1551,6 +1552,15 @@
     item->any(a, b);
 }
 
+WebKitDOMPromise* webkit_dom_test_obj_test_promise_function(WebKitDOMTestObj* self)
+{
+    WebCore::JSMainThreadNullState state;
+    g_return_val_if_fail(WEBKIT_DOM_IS_TEST_OBJ(self), 0);
+    WebCore::TestObj* item = WebKit::core(self);
+    RefPtr<WebCore::Promise> gobjectResult = WTF::getPtr(item->testPromiseFunction());
+    return WebKit::kit(gobjectResult.get());
+}
+
 glong webkit_dom_test_obj_get_read_only_long_attr(WebKitDOMTestObj* self)
 {
     WebCore::JSMainThreadNullState state;

Modified: trunk/Source/WebCore/bindings/scripts/test/GObject/WebKitDOMTestObj.h (185492 => 185493)


--- trunk/Source/WebCore/bindings/scripts/test/GObject/WebKitDOMTestObj.h	2015-06-12 08:19:36 UTC (rev 185492)
+++ trunk/Source/WebCore/bindings/scripts/test/GObject/WebKitDOMTestObj.h	2015-06-12 09:25:46 UTC (rev 185493)
@@ -651,6 +651,17 @@
 webkit_dom_test_obj_any(WebKitDOMTestObj* self, gfloat a, glong b);
 
 /**
+ * webkit_dom_test_obj_test_promise_function:
+ * @self: A #WebKitDOMTestObj
+ *
+ * Returns: (transfer none): A #WebKitDOMPromise
+ *
+ * Stability: Unstable
+**/
+WEBKIT_API WebKitDOMPromise*
+webkit_dom_test_obj_test_promise_function(WebKitDOMTestObj* self);
+
+/**
  * webkit_dom_test_obj_get_read_only_long_attr:
  * @self: A #WebKitDOMTestObj
  *

Modified: trunk/Source/WebCore/bindings/scripts/test/JS/JSTestObj.cpp (185492 => 185493)


--- trunk/Source/WebCore/bindings/scripts/test/JS/JSTestObj.cpp	2015-06-12 08:19:36 UTC (rev 185492)
+++ trunk/Source/WebCore/bindings/scripts/test/JS/JSTestObj.cpp	2015-06-12 09:25:46 UTC (rev 185493)
@@ -29,6 +29,7 @@
 #include "Frame.h"
 #include "HTMLNames.h"
 #include "JSDOMBinding.h"
+#include "JSDOMPromise.h"
 #include "JSDOMStringList.h"
 #include "JSDocument.h"
 #include "JSEventListener.h"
@@ -154,6 +155,7 @@
 JSC::EncodedJSValue JSC_HOST_CALL jsTestObjPrototypeFunctionVariadicDoubleMethod(JSC::ExecState*);
 JSC::EncodedJSValue JSC_HOST_CALL jsTestObjPrototypeFunctionVariadicNodeMethod(JSC::ExecState*);
 JSC::EncodedJSValue JSC_HOST_CALL jsTestObjPrototypeFunctionAny(JSC::ExecState*);
+JSC::EncodedJSValue JSC_HOST_CALL jsTestObjPrototypeFunctionTestPromiseFunction(JSC::ExecState*);
 
 // Attributes
 
@@ -648,6 +650,7 @@
     { "variadicDoubleMethod", JSC::Function, NoIntrinsic, (intptr_t)static_cast<NativeFunction>(jsTestObjPrototypeFunctionVariadicDoubleMethod), (intptr_t) (2) },
     { "variadicNodeMethod", JSC::Function, NoIntrinsic, (intptr_t)static_cast<NativeFunction>(jsTestObjPrototypeFunctionVariadicNodeMethod), (intptr_t) (2) },
     { "any", JSC::Function, NoIntrinsic, (intptr_t)static_cast<NativeFunction>(jsTestObjPrototypeFunctionAny), (intptr_t) (2) },
+    { "testPromiseFunction", JSC::Function, NoIntrinsic, (intptr_t)static_cast<NativeFunction>(jsTestObjPrototypeFunctionTestPromiseFunction), (intptr_t) (0) },
 };
 
 const ClassInfo JSTestObjPrototype::s_info = { "TestObjectPrototype", &Base::s_info, 0, CREATE_METHOD_TABLE(JSTestObjPrototype) };
@@ -4423,6 +4426,20 @@
     return JSValue::encode(jsUndefined());
 }
 
+EncodedJSValue JSC_HOST_CALL jsTestObjPrototypeFunctionTestPromiseFunction(ExecState* exec)
+{
+    JSValue thisValue = exec->thisValue();
+    JSTestObj* castedThis = jsDynamicCast<JSTestObj*>(thisValue);
+    if (UNLIKELY(!castedThis))
+        return throwThisTypeError(*exec, "TestObj", "testPromiseFunction");
+    ASSERT_GC_OBJECT_INHERITS(castedThis, JSTestObj::info());
+    auto& impl = castedThis->impl();
+    JSPromiseDeferred* promiseDeferred = JSPromiseDeferred::create(exec, castedThis->globalObject());
+    impl.testPromiseFunction(DeferredWrapper(exec, castedThis->globalObject(), promiseDeferred));
+    JSValue result = promiseDeferred->promise();
+    return JSValue::encode(result);
+}
+
 void JSTestObj::visitChildren(JSCell* cell, SlotVisitor& visitor)
 {
     auto* thisObject = jsCast<JSTestObj*>(cell);

Modified: trunk/Source/WebCore/bindings/scripts/test/ObjC/DOMTestObj.h (185492 => 185493)


--- trunk/Source/WebCore/bindings/scripts/test/ObjC/DOMTestObj.h	2015-06-12 08:19:36 UTC (rev 185492)
+++ trunk/Source/WebCore/bindings/scripts/test/ObjC/DOMTestObj.h	2015-06-12 09:25:46 UTC (rev 185493)
@@ -173,4 +173,5 @@
 - (void)variadicDoubleMethod:(double)head tail:(double)tail;
 - (void)variadicNodeMethod:(DOMNode *)head tail:(DOMNode *)tail;
 - (void)any:(float)a b:(int)b;
+- (DOMPromise *)testPromiseFunction;
 @end

Modified: trunk/Source/WebCore/bindings/scripts/test/ObjC/DOMTestObj.mm (185492 => 185493)


--- trunk/Source/WebCore/bindings/scripts/test/ObjC/DOMTestObj.mm	2015-06-12 08:19:36 UTC (rev 185492)
+++ trunk/Source/WebCore/bindings/scripts/test/ObjC/DOMTestObj.mm	2015-06-12 09:25:46 UTC (rev 185493)
@@ -1183,6 +1183,12 @@
     IMPL->any(a, b);
 }
 
+- (DOMPromise *)testPromiseFunction
+{
+    WebCore::JSMainThreadNullState state;
+    return kit(WTF::getPtr(IMPL->testPromiseFunction()));
+}
+
 @end
 
 WebCore::TestObj* core(DOMTestObj *wrapper)

Modified: trunk/Source/WebCore/bindings/scripts/test/TestObj.idl (185492 => 185493)


--- trunk/Source/WebCore/bindings/scripts/test/TestObj.idl	2015-06-12 08:19:36 UTC (rev 185492)
+++ trunk/Source/WebCore/bindings/scripts/test/TestObj.idl	2015-06-12 09:25:46 UTC (rev 185493)
@@ -274,6 +274,8 @@
     // Promise attributes
     [CustomGetter] readonly attribute Promise testPromiseAttr;
 
+    // Promise function
+    Promise testPromiseFunction();
 };
 
 // The following comment should not generate any code.
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to