Bug 206364

Summary: Make the callAsyncJavaScriptFunction function actually be async (so await works)
Product: WebKit Reporter: Brady Eidson <beidson>
Component: WebKit WebsiteAssignee: Nobody <webkit-unassigned>
Status: RESOLVED FIXED    
Severity: Normal CC: commit-queue, ggaren, hi, jond, keith_miller, saam
Priority: P2 Keywords: InRadar
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
Attachments:
Description Flags
Patch none

Description Brady Eidson 2020-01-16 11:22:16 PST
Make the callAsyncJavaScriptFunction function actually be async (so await works)

<rdar://problem/58571682>
Comment 1 Brady Eidson 2020-01-16 11:39:11 PST
Created attachment 387938 [details]
Patch
Comment 2 Geoffrey Garen 2020-01-16 11:41:31 PST
Comment on attachment 387938 [details]
Patch

r=me
Comment 3 Devin Rousso 2020-01-16 11:47:02 PST
Comment on attachment 387938 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=387938&action=review

> Source/WebCore/bindings/js/ScriptController.cpp:607
> +    functionStringBuilder.append("(async function(");

One issue with making a function `async` is that they _always_ return a `Promise`.  I think it would be better to have `ScriptController:callInWorld` take an `enum class IsAsync { Yes, No };` so that callers can decide whether or not they want this.
Comment 4 Brady Eidson 2020-01-16 11:57:38 PST
(In reply to Devin Rousso from comment #3)
> Comment on attachment 387938 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=387938&action=review
> 
> > Source/WebCore/bindings/js/ScriptController.cpp:607
> > +    functionStringBuilder.append("(async function(");
> 
> One issue with making a function `async` is that they _always_ return a
> `Promise`.  

Which is fine, because literally the only client of this is "callAsyncJavaScriptFunction" which expects promise results.

> I think it would be better to have
> `ScriptController:callInWorld` take an `enum class IsAsync { Yes, No };` so
> that callers can decide whether or not they want this.

There's no reason to do now that because there would be no client of "No"
Comment 5 WebKit Commit Bot 2020-01-16 13:30:43 PST
Comment on attachment 387938 [details]
Patch

Clearing flags on attachment: 387938

Committed r254704: <https://trac.webkit.org/changeset/254704>
Comment 6 WebKit Commit Bot 2020-01-16 13:30:45 PST
All reviewed patches have been landed.  Closing bug.
Comment 7 Saam Barati 2020-01-16 23:45:11 PST
(In reply to Devin Rousso from comment #3)
> Comment on attachment 387938 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=387938&action=review
> 
> > Source/WebCore/bindings/js/ScriptController.cpp:607
> > +    functionStringBuilder.append("(async function(");
> 
> One issue with making a function `async` is that they _always_ return a
> `Promise`.  I think it would be better to have
> `ScriptController:callInWorld` take an `enum class IsAsync { Yes, No };` so
> that callers can decide whether or not they want this.

Why would the API change here? When the Promise that’s returned is resolved, we IPC the result back to the UI process, so I think it should not be (very) observable to users. (Like if you treat it like a non async function, and do something like “return 42;”, everything Just Works and the UI process is called back with “42”.)

I think the only concern here is that async functions have slightly different semantics than non async functions. I think(?) things like reserved identifiers and such. But I think that’s ok here, especially because this will be documented.
Comment 8 Devin Rousso 2020-01-17 10:47:05 PST
Comment on attachment 387938 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=387938&action=review

>>>> Source/WebCore/bindings/js/ScriptController.cpp:607
>>>> +    functionStringBuilder.append("(async function(");
>>> 
>>> One issue with making a function `async` is that they _always_ return a `Promise`.  I think it would be better to have `ScriptController:callInWorld` take an `enum class IsAsync { Yes, No };` so that callers can decide whether or not they want this.
>> 
>> Which is fine, because literally the only client of this is "callAsyncJavaScriptFunction" which expects promise results.
> 
> Why would the API change here? When the Promise that’s returned is resolved, we IPC the result back to the UI process, so I think it should not be (very) observable to users. (Like if you treat it like a non async function, and do something like “return 42;”, everything Just Works and the UI process is called back with “42”.)
> 
> I think the only concern here is that async functions have slightly different semantics than non async functions. I think(?) things like reserved identifiers and such. But I think that’s ok here, especially because this will be documented.

I misread how `ScriptController::callInWorld` was being used.  My apologies for the confusion.