Bug 188212 - Add self.queueMicrotask(f) on DOMWindow
Summary: Add self.queueMicrotask(f) on DOMWindow
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: DOM (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Yusuke Suzuki
URL:
Keywords: InRadar
Depends on:
Blocks: 188247
  Show dependency treegraph
 
Reported: 2018-07-31 20:32 PDT by Ryosuke Niwa
Modified: 2018-09-18 02:25 PDT (History)
11 users (show)

See Also:


Attachments
Patch (16.42 KB, patch)
2018-08-01 07:22 PDT, Yusuke Suzuki
no flags Details | Formatted Diff | Diff
Patch (10.79 KB, patch)
2018-08-01 20:09 PDT, Yusuke Suzuki
no flags Details | Formatted Diff | Diff
Patch (11.78 KB, patch)
2018-08-01 20:19 PDT, Yusuke Suzuki
no flags Details | Formatted Diff | Diff
Patch (11.79 KB, patch)
2018-08-01 20:21 PDT, Yusuke Suzuki
rniwa: review+
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Ryosuke Niwa 2018-07-31 20:32:26 PDT
See https://github.com/whatwg/html/issues/512
Comment 1 Yusuke Suzuki 2018-08-01 07:22:16 PDT
Created attachment 346275 [details]
Patch
Comment 2 Ryosuke Niwa 2018-08-01 14:43:33 PDT
Comment on attachment 346275 [details]
Patch

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

> Source/WebCore/workers/WorkerGlobalScope.cpp:256
> +    postTask([callback = WTFMove(callback)](ScriptExecutionContext&) {

How can this be correct? Microtasks & tasks are completely different concepts & timing.
Comment 4 Ryosuke Niwa 2018-08-01 14:53:56 PDT
Comment on attachment 346275 [details]
Patch

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

>> Source/WebCore/workers/WorkerGlobalScope.cpp:256
>> +    postTask([callback = WTFMove(callback)](ScriptExecutionContext&) {
> 
> How can this be correct? Microtasks & tasks are completely different concepts & timing.

I think we need to hook into JSC's microtask queue instead here.
Comment 5 Yusuke Suzuki 2018-08-01 19:44:28 PDT
Comment on attachment 346275 [details]
Patch

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

>>> Source/WebCore/workers/WorkerGlobalScope.cpp:256
>>> +    postTask([callback = WTFMove(callback)](ScriptExecutionContext&) {
>> 
>> How can this be correct? Microtasks & tasks are completely different concepts & timing.
> 
> I think we need to hook into JSC's microtask queue instead here.

"How can this be correct? Microtasks & tasks are completely different concepts & timing."

Yes, this is correct in terms of our current implementation.

Unfortunately, our Worker does not support Microtasks as our main thread does. So this code is the same to the current Worker's microtask queuing mechanism.
We can find that JSWorkerGlobalScopeBase::queueTaskToEventLoop does the same thing, while JSDOMWindowBase::queueTaskToEventLoop posts microtasks correctly.

"I think we need to hook into JSC's microtask queue instead here."

The current approach against using JSC's microtask queue has Pros and Cons.

Pros:
- It skips one level indirection (JSC -> WebCore through function pointer)
- It leverages binding VoidCallback type checking instead of manually typechecking in custom binding code
Cons:
- It does not use JSC's microtask stack, consistency

Given that this `self.queueMicrotask` is performance-sensitive (If it is not performance-sensitive, we can just use `Promise.resolve().then(callback)`), and I prefer non-custom binding code, I took the current approach in this patch.
But if the consistency is preferable, we can use JSC's stack in binding custom code. I'll upload this version.
Comment 6 Ryosuke Niwa 2018-08-01 19:58:26 PDT
Here's a problem though.

If the worker ever supports a feature which schedules a task, then all microtasks are supposed to be drained before such a task is executed. The proposed patch doesn't implement this semantics.

I don't think we want to be in the business of auditing everything worker supports and making sure they never schedules a task.
Comment 7 Yusuke Suzuki 2018-08-01 20:03:59 PDT
(In reply to Ryosuke Niwa from comment #6)
> Here's a problem though.
> 
> If the worker ever supports a feature which schedules a task, then all
> microtasks are supposed to be drained before such a task is executed. The
> proposed patch doesn't implement this semantics.
> 
> I don't think we want to be in the business of auditing everything worker
> supports and making sure they never schedules a task.

OK, then, I'll implement only DOM window version for now.
Comment 8 Yusuke Suzuki 2018-08-01 20:09:44 PDT
Created attachment 346353 [details]
Patch
Comment 9 Yusuke Suzuki 2018-08-01 20:19:31 PDT
Created attachment 346354 [details]
Patch
Comment 10 Yusuke Suzuki 2018-08-01 20:21:27 PDT
Created attachment 346355 [details]
Patch
Comment 11 Ryosuke Niwa 2018-08-01 20:26:17 PDT
Comment on attachment 346355 [details]
Patch

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

> Source/JavaScriptCore/runtime/JSJob.cpp:-48
> -    virtual ~JSJobMicrotask()

Where did the destructor go?

> Source/WebCore/page/DOMWindow.idl:92
> +    [Custom] void queueMicrotask(VoidCallback callback);

So we're not gonna implement the one in worker??
Comment 12 Ryosuke Niwa 2018-08-01 20:28:17 PDT
Comment on attachment 346355 [details]
Patch

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

> Source/JavaScriptCore/runtime/JSJob.cpp:60
> +Ref<Microtask> createJSJob(VM& vm, JSValue job)

I think we should call this createJSTask instead to use the consistent terminology.
Comment 13 Yusuke Suzuki 2018-08-01 20:28:30 PDT
Comment on attachment 346355 [details]
Patch

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

>> Source/JavaScriptCore/runtime/JSJob.cpp:-48
>> -    virtual ~JSJobMicrotask()
> 
> Where did the destructor go?

Microtask has `virtual ~Microtask()`, so this is not necessary.

>> Source/WebCore/page/DOMWindow.idl:92
>> +    [Custom] void queueMicrotask(VoidCallback callback);
> 
> So we're not gonna implement the one in worker??

Yes, we will implement it after MicrotaskQueue is adopted in worker environments.
Comment 14 Ryosuke Niwa 2018-08-01 20:29:24 PDT
Comment on attachment 346355 [details]
Patch

Sounds good to me.
Comment 15 Ryosuke Niwa 2018-08-01 20:29:43 PDT
Let's file a bug for the worker one so that we don't forget though.
Comment 16 Yusuke Suzuki 2018-08-01 20:33:14 PDT
Comment on attachment 346355 [details]
Patch

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

>> Source/JavaScriptCore/runtime/JSJob.cpp:60
>> +Ref<Microtask> createJSJob(VM& vm, JSValue job)
> 
> I think we should call this createJSTask instead to use the consistent terminology.

The interesting thing is that this "Job" is a word used in ECMA262 :P https://tc39.github.io/ecma262/#sec-enqueuejob
Using a word "microtask" in WebKit tree consistently sounds fine. Fixed.

>>> Source/WebCore/page/DOMWindow.idl:92
>>> +    [Custom] void queueMicrotask(VoidCallback callback);
>> 
>> So we're not gonna implement the one in worker??
> 
> Yes, we will implement it after MicrotaskQueue is adopted in worker environments.

The interesting thing is that this "Job" is a word used in ECMA262 :P https://tc39.github.io/ecma262/#sec-enqueuejob
Using a word "microtask" in WebKit tree consistently sounds fine. Fixed.
Comment 17 Yusuke Suzuki 2018-08-01 20:37:10 PDT
(In reply to Ryosuke Niwa from comment #15)
> Let's file a bug for the worker one so that we don't forget though.

OK, filed in bug 188246 and bug 188247
Comment 18 Ryosuke Niwa 2018-08-01 20:38:12 PDT
Oh, you're right:
https://html.spec.whatwg.org/#integration-with-the-javascript-job-queue

But I think the definition of job there is slightly different from what we're doing here.
Comment 19 Yusuke Suzuki 2018-08-01 20:55:52 PDT
Committed r234491: <https://trac.webkit.org/changeset/234491>
Comment 20 Radar WebKit Bug Importer 2018-08-01 21:07:52 PDT
<rdar://problem/42841210>