Title: [254704] trunk
Revision
254704
Author
[email protected]
Date
2020-01-16 13:30:39 -0800 (Thu, 16 Jan 2020)

Log Message

Make the callAsyncJavaScriptFunction function actually be async (so await works).
<rdar://problem/58571682> and https://bugs.webkit.org/show_bug.cgi?id=206364

Reviewed by Geoffrey Garen.

Source/WebCore:

Covered by API tests.

* bindings/js/ScriptController.cpp:
(WebCore::ScriptController::callInWorld):

Source/WebKit:

* UIProcess/API/Cocoa/WKWebViewPrivate.h: Update callAsyncJavaScriptFunction: header docs.

Tools:

* TestWebKitAPI/Tests/WebKitCocoa/AsyncFunction.mm:
(TestWebKitAPI::TEST):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (254703 => 254704)


--- trunk/Source/WebCore/ChangeLog	2020-01-16 21:02:11 UTC (rev 254703)
+++ trunk/Source/WebCore/ChangeLog	2020-01-16 21:30:39 UTC (rev 254704)
@@ -1,3 +1,15 @@
+2020-01-16  Brady Eidson  <[email protected]>
+
+        Make the callAsyncJavaScriptFunction function actually be async (so await works).
+        <rdar://problem/58571682> and https://bugs.webkit.org/show_bug.cgi?id=206364
+
+        Reviewed by Geoffrey Garen.
+
+        Covered by API tests.
+
+        * bindings/js/ScriptController.cpp:
+        (WebCore::ScriptController::callInWorld):
+
 2020-01-16  Don Olmstead  <[email protected]>
 
         Non-unified build fixes mid January 2020 edition

Modified: trunk/Source/WebCore/bindings/js/ScriptController.cpp (254703 => 254704)


--- trunk/Source/WebCore/bindings/js/ScriptController.cpp	2020-01-16 21:02:11 UTC (rev 254703)
+++ trunk/Source/WebCore/bindings/js/ScriptController.cpp	2020-01-16 21:30:39 UTC (rev 254704)
@@ -604,7 +604,7 @@
     String errorMessage;
 
     // Build up a new script string that is an async function with arguments, and deserialize those arguments.
-    functionStringBuilder.append("(function(");
+    functionStringBuilder.append("(async function(");
     for (auto argument = parameters.arguments->begin(); argument != parameters.arguments->end();) {
         functionStringBuilder.append(argument->key);
         auto serializedArgument = SerializedScriptValue::createFromWireBytes(WTFMove(argument->value));

Modified: trunk/Source/WebKit/ChangeLog (254703 => 254704)


--- trunk/Source/WebKit/ChangeLog	2020-01-16 21:02:11 UTC (rev 254703)
+++ trunk/Source/WebKit/ChangeLog	2020-01-16 21:30:39 UTC (rev 254704)
@@ -1,3 +1,12 @@
+2020-01-16  Brady Eidson  <[email protected]>
+
+        Make the callAsyncJavaScriptFunction function actually be async (so await works).
+        <rdar://problem/58571682> and https://bugs.webkit.org/show_bug.cgi?id=206364
+
+        Reviewed by Geoffrey Garen.
+
+        * UIProcess/API/Cocoa/WKWebViewPrivate.h: Update callAsyncJavaScriptFunction: header docs.
+
 2020-01-16  Don Olmstead  <[email protected]>
 
         Non-unified build fixes mid January 2020 edition

Modified: trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebViewPrivate.h (254703 => 254704)


--- trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebViewPrivate.h	2020-01-16 21:02:11 UTC (rev 254703)
+++ trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebViewPrivate.h	2020-01-16 21:30:39 UTC (rev 254704)
@@ -325,8 +325,11 @@
  @param contentWorld The WKContentWorld in which to call the _javascript_ function.
  @param completionHandler A block to invoke with the return value of the function call, or with the asynchronous resolution of the function's return value.
  @discussion The _javascript_ string is treated as an anonymous _javascript_ function that can be called with named arguments.
- Pass in _javascript_ string formatted for evaluation, not as one of the variants of function call available in _javascript_.
- For example:
+ Do not format your string as one of the variants of function call available in _javascript_.
+ Instead pass in a _javascript_ string representing the function text, formatted for evaluation.
+ For example do not pass in the string:
+     function(x, y, z) { return x ? y : z; }
+ Instead pass in the string:
      return x ? y : z;
 
  The arguments dictionary supplies the values for those arguments which are serialized into _javascript_ equivalents.
@@ -355,6 +358,16 @@
  If the object calls "fulfill", your completion handler will be called with the result.
  If the object calls "reject", your completion handler will be called with a WKErrorJavaScriptAsyncFunctionResultRejected error containing the reject reason in the userInfo dictionary.
  If the object is garbage collected before it is resolved, your completion handler will be called with an error indicating that it will never be resolved.
+
+ Since the function is a _javascript_ "async" function you can use _javascript_ "await" on those objects inside your function text.
+ For example:
+     var p = new Promise(function (r) {
+         r(42);
+     });
+     await p;
+     return p;
+
+ The above function text will create a promise that will fulfull with the value 42, wait for it to resolve, then return the fulfillment value of 42.
 */
 - (void)_callAsyncJavaScriptFunction:(NSString *)_javascript_String withArguments:(NSDictionary<NSString *, id> *)arguments inWorld:(_WKContentWorld *)contentWorld completionHandler:(void (^)(id, NSError *error))completionHandler;
 

Modified: trunk/Tools/ChangeLog (254703 => 254704)


--- trunk/Tools/ChangeLog	2020-01-16 21:02:11 UTC (rev 254703)
+++ trunk/Tools/ChangeLog	2020-01-16 21:30:39 UTC (rev 254704)
@@ -1,3 +1,13 @@
+2020-01-16  Brady Eidson  <[email protected]>
+
+        Make the callAsyncJavaScriptFunction function actually be async (so await works).
+        <rdar://problem/58571682> and https://bugs.webkit.org/show_bug.cgi?id=206364
+
+        Reviewed by Geoffrey Garen.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/AsyncFunction.mm:
+        (TestWebKitAPI::TEST):
+
 2020-01-16  Simon Fraser  <[email protected]>
 
         fast/forms/ios/zoom-after-input-tap-wide-input.html is timing out

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/AsyncFunction.mm (254703 => 254704)


--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/AsyncFunction.mm	2020-01-16 21:02:11 UTC (rev 254703)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/AsyncFunction.mm	2020-01-16 21:30:39 UTC (rev 254704)
@@ -243,7 +243,43 @@
     }];
     TestWebKitAPI::Util::run(&done);
 
+    // Verify we can await for a promise to be resolved before returning.
+    functionBody = @"var r = 0; var p = new Promise(function(fulfill, reject) { setTimeout(function(){ r = 42; fulfill(); }, 5);}); await p; return r;";
+
     done = false;
+    [webView _callAsyncJavaScriptFunction:functionBody withArguments:nil inWorld:_WKContentWorld.pageContentWorld completionHandler:[&] (id result, NSError *error) {
+        EXPECT_NULL(error);
+        EXPECT_TRUE([result isKindOfClass:[NSNumber class]]);
+        EXPECT_TRUE([result isEqualToNumber:@42]);
+        done = true;
+    }];
+    TestWebKitAPI::Util::run(&done);
+
+    // Returning an already resolved promise gives the value it was resolved with.
+    functionBody = @"var p = new Promise(function(fulfill, reject) { setTimeout(function(){ fulfill('Fulfilled!') }, 5);}); await p; return p;";
+
+    done = false;
+    [webView _callAsyncJavaScriptFunction:functionBody withArguments:nil inWorld:_WKContentWorld.pageContentWorld completionHandler:[&] (id result, NSError *error) {
+        EXPECT_NULL(error);
+        EXPECT_TRUE([result isKindOfClass:[NSString class]]);
+        EXPECT_TRUE([result isEqualToString:@"Fulfilled!"]);
+        done = true;
+    }];
+    TestWebKitAPI::Util::run(&done);
+
+    // Chaining thenables should work.
+    functionBody = @"var p = new Promise(function (r) { r(new Promise(function (r) { r(42); })); }); await p; return 'Done';";
+
+    done = false;
+    [webView _callAsyncJavaScriptFunction:functionBody withArguments:nil inWorld:_WKContentWorld.pageContentWorld completionHandler:[&] (id result, NSError *error) {
+        EXPECT_NULL(error);
+        EXPECT_TRUE([result isKindOfClass:[NSString class]]);
+        EXPECT_TRUE([result isEqualToString:@"Done"]);
+        done = true;
+    }];
+    TestWebKitAPI::Util::run(&done);
+
+    done = false;
     tryGCPromise(webView.get(), done);
     TestWebKitAPI::Util::run(&done);
 }
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to