2013-06-18 23:02:07 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2016-09-14 16:47:32 +03:00
|
|
|
#include "mozilla/CycleCollectedJSContext.h"
|
2013-07-09 18:28:15 +04:00
|
|
|
#include <algorithm>
|
2014-01-30 22:26:54 +04:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2014-12-05 19:53:00 +03:00
|
|
|
#include "mozilla/AutoRestore.h"
|
2017-02-24 00:23:45 +03:00
|
|
|
#include "mozilla/CycleCollectedJSRuntime.h"
|
2018-10-10 00:42:22 +03:00
|
|
|
#include "mozilla/EventStateManager.h"
|
2016-01-19 23:48:22 +03:00
|
|
|
#include "mozilla/Move.h"
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2016-08-15 09:43:21 +03:00
|
|
|
#include "mozilla/Sprintf.h"
|
2015-05-18 18:54:00 +03:00
|
|
|
#include "mozilla/Telemetry.h"
|
2016-01-19 23:48:22 +03:00
|
|
|
#include "mozilla/TimelineConsumers.h"
|
|
|
|
#include "mozilla/TimelineMarker.h"
|
2016-09-02 10:12:24 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2015-04-22 19:43:02 +03:00
|
|
|
#include "mozilla/DebuggerOnGCRunnable.h"
|
2013-06-18 23:02:15 +04:00
|
|
|
#include "mozilla/dom/DOMJSClass.h"
|
2018-02-06 00:34:05 +03:00
|
|
|
#include "mozilla/dom/DOMException.h"
|
2016-01-19 23:48:22 +03:00
|
|
|
#include "mozilla/dom/ProfileTimelineMarkerBinding.h"
|
2016-02-10 01:40:31 +03:00
|
|
|
#include "mozilla/dom/PromiseBinding.h"
|
2016-03-22 18:22:24 +03:00
|
|
|
#include "mozilla/dom/PromiseDebugging.h"
|
2013-12-12 05:51:57 +04:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2018-08-02 10:11:57 +03:00
|
|
|
#include "jsapi.h"
|
2015-04-27 20:58:24 +03:00
|
|
|
#include "js/Debug.h"
|
2016-12-30 01:00:43 +03:00
|
|
|
#include "js/GCAPI.h"
|
2017-05-03 02:13:49 +03:00
|
|
|
#include "js/Utility.h"
|
2015-10-26 19:14:47 +03:00
|
|
|
#include "nsContentUtils.h"
|
2013-06-18 23:02:15 +04:00
|
|
|
#include "nsCycleCollectionNoteRootCallback.h"
|
2013-06-18 23:02:14 +04:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2013-06-18 23:02:16 +04:00
|
|
|
#include "nsCycleCollector.h"
|
2013-06-18 23:02:16 +04:00
|
|
|
#include "nsDOMJSUtils.h"
|
2017-10-05 18:34:12 +03:00
|
|
|
#include "nsDOMMutationObserver.h"
|
2014-07-12 11:43:06 +04:00
|
|
|
#include "nsJSUtils.h"
|
2018-10-10 00:42:22 +03:00
|
|
|
#include "nsPIDOMWindow.h"
|
2015-12-31 16:21:49 +03:00
|
|
|
#include "nsWrapperCache.h"
|
2016-12-30 00:19:27 +03:00
|
|
|
#include "nsStringBuffer.h"
|
2014-05-29 22:41:54 +04:00
|
|
|
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
#include "nsThread.h"
|
2013-08-28 02:39:02 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2013-06-18 23:02:15 +04:00
|
|
|
#include "xpcpublic.h"
|
2013-06-18 23:02:07 +04:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2013-06-18 23:02:15 +04:00
|
|
|
using namespace mozilla::dom;
|
2013-06-18 23:02:07 +04:00
|
|
|
|
2013-07-09 18:28:15 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
2016-09-14 16:47:32 +03:00
|
|
|
CycleCollectedJSContext::CycleCollectedJSContext()
|
2017-04-24 23:54:27 +03:00
|
|
|
: mIsPrimaryContext(true),
|
|
|
|
mRuntime(nullptr),
|
2016-06-24 21:19:51 +03:00
|
|
|
mJSContext(nullptr),
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
mDoingStableStates(false),
|
2017-11-17 06:01:27 +03:00
|
|
|
mTargetedMicroTaskRecursionDepth(0),
|
2017-10-05 18:34:12 +03:00
|
|
|
mMicroTaskLevel(0),
|
2017-10-11 15:31:38 +03:00
|
|
|
mMicroTaskRecursionDepth(0) {
|
2017-05-09 14:59:00 +03:00
|
|
|
MOZ_COUNT_CTOR(CycleCollectedJSContext);
|
2018-05-10 08:04:12 +03:00
|
|
|
|
|
|
|
// Reinitialize PerThreadAtomCache because dom/bindings/Codegen.py compares
|
|
|
|
// against zero rather than JSID_VOID to detect uninitialized jsid members.
|
|
|
|
memset(static_cast<PerThreadAtomCache*>(this), 0, sizeof(PerThreadAtomCache));
|
|
|
|
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
nsCOMPtr<nsIThread> thread = do_GetCurrentThread();
|
|
|
|
mOwningThread = thread.forget().downcast<nsThread>().take();
|
|
|
|
MOZ_RELEASE_ASSERT(mOwningThread);
|
2016-02-14 16:30:25 +03:00
|
|
|
}
|
|
|
|
|
2016-09-14 16:47:32 +03:00
|
|
|
CycleCollectedJSContext::~CycleCollectedJSContext() {
|
2017-05-09 14:59:00 +03:00
|
|
|
MOZ_COUNT_DTOR(CycleCollectedJSContext);
|
2016-02-14 16:30:25 +03:00
|
|
|
// If the allocation failed, here we are.
|
2016-08-11 15:39:22 +03:00
|
|
|
if (!mJSContext) {
|
2016-02-14 16:30:25 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-10 08:04:12 +03:00
|
|
|
JS_SetContextPrivate(mJSContext, nullptr);
|
|
|
|
|
2017-04-25 00:15:46 +03:00
|
|
|
mRuntime->RemoveContext(this);
|
|
|
|
|
2017-04-24 23:54:27 +03:00
|
|
|
if (mIsPrimaryContext) {
|
|
|
|
mRuntime->Shutdown(mJSContext);
|
|
|
|
}
|
2016-02-14 16:30:25 +03:00
|
|
|
|
|
|
|
// Last chance to process any events.
|
2017-11-17 06:01:27 +03:00
|
|
|
CleanupIDBTransactions(mBaseRecursionDepth);
|
|
|
|
MOZ_ASSERT(mPendingIDBTransactions.IsEmpty());
|
2016-02-14 16:30:25 +03:00
|
|
|
|
|
|
|
ProcessStableStateQueue();
|
|
|
|
MOZ_ASSERT(mStableStateEvents.IsEmpty());
|
|
|
|
|
|
|
|
// Clear mPendingException first, since it might be cycle collected.
|
|
|
|
mPendingException = nullptr;
|
|
|
|
|
2017-11-17 06:01:27 +03:00
|
|
|
MOZ_ASSERT(mDebuggerMicroTaskQueue.empty());
|
|
|
|
MOZ_ASSERT(mPendingMicroTaskRunnables.empty());
|
2016-05-02 21:21:43 +03:00
|
|
|
|
2016-03-22 18:22:24 +03:00
|
|
|
mUncaughtRejections.reset();
|
|
|
|
mConsumedRejections.reset();
|
|
|
|
|
2016-07-23 20:54:19 +03:00
|
|
|
JS_DestroyContext(mJSContext);
|
2016-06-24 21:19:51 +03:00
|
|
|
mJSContext = nullptr;
|
2017-04-24 23:54:27 +03:00
|
|
|
|
|
|
|
if (mIsPrimaryContext) {
|
|
|
|
nsCycleCollector_forgetJSContext();
|
|
|
|
} else {
|
|
|
|
nsCycleCollector_forgetNonPrimaryContext();
|
|
|
|
}
|
2016-02-14 16:30:25 +03:00
|
|
|
|
|
|
|
mozilla::dom::DestroyScriptSettings();
|
|
|
|
|
|
|
|
mOwningThread->SetScriptObserver(nullptr);
|
|
|
|
NS_RELEASE(mOwningThread);
|
|
|
|
|
2017-04-24 23:54:27 +03:00
|
|
|
if (mIsPrimaryContext) {
|
|
|
|
delete mRuntime;
|
|
|
|
}
|
2017-02-24 00:23:45 +03:00
|
|
|
mRuntime = nullptr;
|
2016-03-02 00:53:22 +03:00
|
|
|
}
|
|
|
|
|
2017-04-24 23:54:27 +03:00
|
|
|
void CycleCollectedJSContext::InitializeCommon() {
|
2017-04-25 00:15:46 +03:00
|
|
|
mRuntime->AddContext(this);
|
|
|
|
|
2017-04-24 23:54:27 +03:00
|
|
|
mOwningThread->SetScriptObserver(this);
|
|
|
|
// The main thread has a base recursion depth of 0, workers of 1.
|
|
|
|
mBaseRecursionDepth = RecursionDepth();
|
|
|
|
|
|
|
|
NS_GetCurrentThread()->SetCanInvokeJS(true);
|
|
|
|
|
Bug 1145201: Replace EnqueuePromiseJobCallback and GetIncumbentGlobalCallback with new JobQueue abstract base class. r=arai,smaug
While the behavior of ECMAScript Promises and their associated job queue is
covered by the ECMAScript standard, the HTML specification amends that with
additional behavior the web platform requires. To support this, SpiderMonkey
provides hooks the embedding can set to replace SpiderMonkey's queue with its
own implementation.
At present, these hooks are C-style function-pointer-and-void-pointer pairs,
which are awkward to handle and mistake-prone, as passing a function the wrong
void* is not a type error. Later patches in this series must add new hooks,
making a bad situation worse.
A C++ abstract base class is a well-typed alternative. This introduces a new
`JS::JobQueue` abstract class, and adapts SpiderMonkey's internal job queue and
Gecko's customization to use it. `GetIncumbentGlobalCallback` and
`EnqueuePromiseJobCallback` become virtual methods.
Within SpiderMonkey, the patch gathers the various fields of JSContext that
implement the internal queue into their own type, js::InternalJobQueue. Various
jsfriendapi functions become veneers for calls to methods specific to the
derived class. The InternalJobQueue type itself remains private to SpiderMonkey,
as it uses types like TraceableFifo, derived from Fifo, that are not part of
SpiderMonkey's public API.
Within Gecko, CycleCollectedJSContext acquires JS::JobQueue as a private base
class, and a few static methods are cleaned up nicely.
There are a few other hooks defined in js/public/Promise.h that might make sense
to turn into virtual methods on JobQueue. For example,
DispatchToEventLoopCallback, used for resolving promises of results from
off-main-thread tasks, is probably necessarily connected to the JobQueue
implementation in use, so it might not be sensible to set one without the other.
But it was left unchanged to reduce this patch's size.
Differential Revision: https://phabricator.services.mozilla.com/D17544
--HG--
extra : moz-landing-system : lando
2019-02-12 11:16:16 +03:00
|
|
|
JS::SetJobQueue(mJSContext, this);
|
2017-04-24 23:54:27 +03:00
|
|
|
|
|
|
|
JS::SetPromiseRejectionTrackerCallback(mJSContext,
|
|
|
|
PromiseRejectionTrackerCallback, this);
|
|
|
|
mUncaughtRejections.init(mJSContext,
|
|
|
|
JS::GCVector<JSObject*, 0, js::SystemAllocPolicy>(
|
|
|
|
js::SystemAllocPolicy()));
|
|
|
|
mConsumedRejections.init(mJSContext,
|
|
|
|
JS::GCVector<JSObject*, 0, js::SystemAllocPolicy>(
|
|
|
|
js::SystemAllocPolicy()));
|
2018-05-10 08:04:12 +03:00
|
|
|
|
|
|
|
// Cast to PerThreadAtomCache for dom::GetAtomCache(JSContext*).
|
|
|
|
JS_SetContextPrivate(mJSContext, static_cast<PerThreadAtomCache*>(this));
|
2017-04-24 23:54:27 +03:00
|
|
|
}
|
|
|
|
|
2017-02-11 02:47:50 +03:00
|
|
|
nsresult CycleCollectedJSContext::Initialize(JSRuntime* aParentRuntime,
|
2016-02-14 16:30:25 +03:00
|
|
|
uint32_t aMaxBytes,
|
|
|
|
uint32_t aMaxNurseryBytes) {
|
2016-07-23 20:54:19 +03:00
|
|
|
MOZ_ASSERT(!mJSContext);
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
|
2013-12-12 05:51:57 +04:00
|
|
|
mozilla::dom::InitScriptSettings();
|
2017-02-11 02:47:50 +03:00
|
|
|
mJSContext = JS_NewContext(aMaxBytes, aMaxNurseryBytes, aParentRuntime);
|
2016-07-23 20:54:19 +03:00
|
|
|
if (!mJSContext) {
|
2016-02-14 16:30:25 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2013-06-18 23:02:07 +04:00
|
|
|
}
|
2013-06-18 23:02:14 +04:00
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
mRuntime = CreateRuntime(mJSContext);
|
|
|
|
|
2017-04-24 23:54:27 +03:00
|
|
|
InitializeCommon();
|
2017-02-24 00:23:45 +03:00
|
|
|
|
2017-04-24 23:54:27 +03:00
|
|
|
nsCycleCollector_registerJSContext(this);
|
2016-02-14 16:30:25 +03:00
|
|
|
|
2017-04-24 23:54:27 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-06-18 23:02:15 +04:00
|
|
|
|
2017-04-24 23:54:27 +03:00
|
|
|
nsresult CycleCollectedJSContext::InitializeNonPrimary(
|
|
|
|
CycleCollectedJSContext* aPrimaryContext) {
|
|
|
|
MOZ_ASSERT(!mJSContext);
|
|
|
|
|
|
|
|
mIsPrimaryContext = false;
|
|
|
|
|
|
|
|
mozilla::dom::InitScriptSettings();
|
|
|
|
mJSContext = JS_NewCooperativeContext(aPrimaryContext->mJSContext);
|
|
|
|
if (!mJSContext) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
mRuntime = aPrimaryContext->mRuntime;
|
|
|
|
|
|
|
|
InitializeCommon();
|
|
|
|
|
|
|
|
nsCycleCollector_registerNonPrimaryContext(this);
|
2013-06-18 23:02:15 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-05-10 08:04:12 +03:00
|
|
|
/* static */ CycleCollectedJSContext* CycleCollectedJSContext::GetFor(
|
|
|
|
JSContext* aCx) {
|
|
|
|
// Cast from void* matching JS_SetContextPrivate.
|
|
|
|
auto atomCache = static_cast<PerThreadAtomCache*>(JS_GetContextPrivate(aCx));
|
|
|
|
// Down cast.
|
|
|
|
return static_cast<CycleCollectedJSContext*>(atomCache);
|
|
|
|
}
|
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
size_t CycleCollectedJSContext::SizeOfExcludingThis(
|
|
|
|
MallocSizeOf aMallocSizeOf) const {
|
|
|
|
return 0;
|
2013-06-18 23:02:16 +04:00
|
|
|
}
|
|
|
|
|
2017-11-17 06:01:27 +03:00
|
|
|
class PromiseJobRunnable final : public MicroTaskRunnable {
|
2017-02-24 00:23:45 +03:00
|
|
|
public:
|
2018-10-10 00:42:22 +03:00
|
|
|
PromiseJobRunnable(JS::HandleObject aPromise, JS::HandleObject aCallback,
|
2018-08-04 00:11:39 +03:00
|
|
|
JS::HandleObject aCallbackGlobal,
|
2017-06-12 22:34:10 +03:00
|
|
|
JS::HandleObject aAllocationSite,
|
2017-02-24 00:23:45 +03:00
|
|
|
nsIGlobalObject* aIncumbentGlobal)
|
2018-08-04 00:11:39 +03:00
|
|
|
: mCallback(new PromiseJobCallback(aCallback, aCallbackGlobal,
|
|
|
|
aAllocationSite, aIncumbentGlobal)),
|
2018-10-10 00:42:22 +03:00
|
|
|
mPropagateUserInputEventHandling(false) {
|
2018-08-01 12:25:49 +03:00
|
|
|
MOZ_ASSERT(js::IsFunctionObject(aCallback));
|
2018-10-10 00:42:22 +03:00
|
|
|
|
|
|
|
if (aPromise) {
|
|
|
|
JS::PromiseUserInputEventHandlingState state =
|
|
|
|
JS::GetPromiseUserInputEventHandlingState(aPromise);
|
|
|
|
mPropagateUserInputEventHandling =
|
|
|
|
state ==
|
|
|
|
JS::PromiseUserInputEventHandlingState::HadUserInteractionAtCreation;
|
|
|
|
}
|
2017-02-24 00:23:45 +03:00
|
|
|
}
|
2013-06-18 23:02:15 +04:00
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
virtual ~PromiseJobRunnable() {}
|
2017-03-17 01:41:36 +03:00
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
protected:
|
2017-11-17 06:01:27 +03:00
|
|
|
virtual void Run(AutoSlowOperation& aAso) override {
|
2017-02-24 00:23:45 +03:00
|
|
|
JSObject* callback = mCallback->CallbackPreserveColor();
|
|
|
|
nsIGlobalObject* global = callback ? xpc::NativeGlobal(callback) : nullptr;
|
|
|
|
if (global && !global->IsDying()) {
|
2018-10-10 00:42:22 +03:00
|
|
|
// Propagate the user input event handling bit if needed.
|
|
|
|
nsCOMPtr<nsPIDOMWindowInner> win = do_QueryInterface(global);
|
2019-01-02 16:05:23 +03:00
|
|
|
RefPtr<Document> doc;
|
2018-10-10 00:42:22 +03:00
|
|
|
if (win) {
|
|
|
|
doc = win->GetExtantDoc();
|
|
|
|
}
|
|
|
|
AutoHandlingUserInputStatePusher userInpStatePusher(
|
|
|
|
mPropagateUserInputEventHandling, nullptr, doc);
|
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
mCallback->Call("promise callback");
|
2017-11-17 06:01:27 +03:00
|
|
|
aAso.CheckForInterrupt();
|
2017-02-24 00:23:45 +03:00
|
|
|
}
|
2018-09-20 15:28:59 +03:00
|
|
|
// Now that mCallback is no longer needed, clear any pointers it contains to
|
|
|
|
// JS GC things. This removes any storebuffer entries associated with those
|
|
|
|
// pointers, which can cause problems by taking up memory and by triggering
|
|
|
|
// minor GCs. This otherwise would not happen until the next minor GC or
|
|
|
|
// cycle collection.
|
|
|
|
mCallback->Reset();
|
2017-11-17 06:01:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool Suppressed() override {
|
|
|
|
nsIGlobalObject* global =
|
|
|
|
xpc::NativeGlobal(mCallback->CallbackPreserveColor());
|
|
|
|
return global && global->IsInSyncOperation();
|
2017-02-24 00:23:45 +03:00
|
|
|
}
|
2017-03-17 01:41:36 +03:00
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
private:
|
|
|
|
RefPtr<PromiseJobCallback> mCallback;
|
2018-10-10 00:42:22 +03:00
|
|
|
bool mPropagateUserInputEventHandling;
|
2017-02-24 00:23:45 +03:00
|
|
|
};
|
2017-03-05 12:23:33 +03:00
|
|
|
|
Bug 1145201: Replace EnqueuePromiseJobCallback and GetIncumbentGlobalCallback with new JobQueue abstract base class. r=arai,smaug
While the behavior of ECMAScript Promises and their associated job queue is
covered by the ECMAScript standard, the HTML specification amends that with
additional behavior the web platform requires. To support this, SpiderMonkey
provides hooks the embedding can set to replace SpiderMonkey's queue with its
own implementation.
At present, these hooks are C-style function-pointer-and-void-pointer pairs,
which are awkward to handle and mistake-prone, as passing a function the wrong
void* is not a type error. Later patches in this series must add new hooks,
making a bad situation worse.
A C++ abstract base class is a well-typed alternative. This introduces a new
`JS::JobQueue` abstract class, and adapts SpiderMonkey's internal job queue and
Gecko's customization to use it. `GetIncumbentGlobalCallback` and
`EnqueuePromiseJobCallback` become virtual methods.
Within SpiderMonkey, the patch gathers the various fields of JSContext that
implement the internal queue into their own type, js::InternalJobQueue. Various
jsfriendapi functions become veneers for calls to methods specific to the
derived class. The InternalJobQueue type itself remains private to SpiderMonkey,
as it uses types like TraceableFifo, derived from Fifo, that are not part of
SpiderMonkey's public API.
Within Gecko, CycleCollectedJSContext acquires JS::JobQueue as a private base
class, and a few static methods are cleaned up nicely.
There are a few other hooks defined in js/public/Promise.h that might make sense
to turn into virtual methods on JobQueue. For example,
DispatchToEventLoopCallback, used for resolving promises of results from
off-main-thread tasks, is probably necessarily connected to the JobQueue
implementation in use, so it might not be sensible to set one without the other.
But it was left unchanged to reduce this patch's size.
Differential Revision: https://phabricator.services.mozilla.com/D17544
--HG--
extra : moz-landing-system : lando
2019-02-12 11:16:16 +03:00
|
|
|
JSObject* CycleCollectedJSContext::getIncumbentGlobal(JSContext* aCx) {
|
2017-02-24 00:23:45 +03:00
|
|
|
nsIGlobalObject* global = mozilla::dom::GetIncumbentGlobal();
|
|
|
|
if (global) {
|
|
|
|
return global->GetGlobalJSObject();
|
|
|
|
}
|
|
|
|
return nullptr;
|
2013-06-18 23:02:15 +04:00
|
|
|
}
|
2013-06-18 23:02:16 +04:00
|
|
|
|
Bug 1145201: Replace EnqueuePromiseJobCallback and GetIncumbentGlobalCallback with new JobQueue abstract base class. r=arai,smaug
While the behavior of ECMAScript Promises and their associated job queue is
covered by the ECMAScript standard, the HTML specification amends that with
additional behavior the web platform requires. To support this, SpiderMonkey
provides hooks the embedding can set to replace SpiderMonkey's queue with its
own implementation.
At present, these hooks are C-style function-pointer-and-void-pointer pairs,
which are awkward to handle and mistake-prone, as passing a function the wrong
void* is not a type error. Later patches in this series must add new hooks,
making a bad situation worse.
A C++ abstract base class is a well-typed alternative. This introduces a new
`JS::JobQueue` abstract class, and adapts SpiderMonkey's internal job queue and
Gecko's customization to use it. `GetIncumbentGlobalCallback` and
`EnqueuePromiseJobCallback` become virtual methods.
Within SpiderMonkey, the patch gathers the various fields of JSContext that
implement the internal queue into their own type, js::InternalJobQueue. Various
jsfriendapi functions become veneers for calls to methods specific to the
derived class. The InternalJobQueue type itself remains private to SpiderMonkey,
as it uses types like TraceableFifo, derived from Fifo, that are not part of
SpiderMonkey's public API.
Within Gecko, CycleCollectedJSContext acquires JS::JobQueue as a private base
class, and a few static methods are cleaned up nicely.
There are a few other hooks defined in js/public/Promise.h that might make sense
to turn into virtual methods on JobQueue. For example,
DispatchToEventLoopCallback, used for resolving promises of results from
off-main-thread tasks, is probably necessarily connected to the JobQueue
implementation in use, so it might not be sensible to set one without the other.
But it was left unchanged to reduce this patch's size.
Differential Revision: https://phabricator.services.mozilla.com/D17544
--HG--
extra : moz-landing-system : lando
2019-02-12 11:16:16 +03:00
|
|
|
bool CycleCollectedJSContext::enqueuePromiseJob(
|
2018-10-10 00:42:18 +03:00
|
|
|
JSContext* aCx, JS::HandleObject aPromise, JS::HandleObject aJob,
|
Bug 1145201: Replace EnqueuePromiseJobCallback and GetIncumbentGlobalCallback with new JobQueue abstract base class. r=arai,smaug
While the behavior of ECMAScript Promises and their associated job queue is
covered by the ECMAScript standard, the HTML specification amends that with
additional behavior the web platform requires. To support this, SpiderMonkey
provides hooks the embedding can set to replace SpiderMonkey's queue with its
own implementation.
At present, these hooks are C-style function-pointer-and-void-pointer pairs,
which are awkward to handle and mistake-prone, as passing a function the wrong
void* is not a type error. Later patches in this series must add new hooks,
making a bad situation worse.
A C++ abstract base class is a well-typed alternative. This introduces a new
`JS::JobQueue` abstract class, and adapts SpiderMonkey's internal job queue and
Gecko's customization to use it. `GetIncumbentGlobalCallback` and
`EnqueuePromiseJobCallback` become virtual methods.
Within SpiderMonkey, the patch gathers the various fields of JSContext that
implement the internal queue into their own type, js::InternalJobQueue. Various
jsfriendapi functions become veneers for calls to methods specific to the
derived class. The InternalJobQueue type itself remains private to SpiderMonkey,
as it uses types like TraceableFifo, derived from Fifo, that are not part of
SpiderMonkey's public API.
Within Gecko, CycleCollectedJSContext acquires JS::JobQueue as a private base
class, and a few static methods are cleaned up nicely.
There are a few other hooks defined in js/public/Promise.h that might make sense
to turn into virtual methods on JobQueue. For example,
DispatchToEventLoopCallback, used for resolving promises of results from
off-main-thread tasks, is probably necessarily connected to the JobQueue
implementation in use, so it might not be sensible to set one without the other.
But it was left unchanged to reduce this patch's size.
Differential Revision: https://phabricator.services.mozilla.com/D17544
--HG--
extra : moz-landing-system : lando
2019-02-12 11:16:16 +03:00
|
|
|
JS::HandleObject aAllocationSite, JS::HandleObject aIncumbentGlobal) {
|
|
|
|
MOZ_ASSERT(aCx == Context());
|
|
|
|
MOZ_ASSERT(Get() == this);
|
2013-06-18 23:02:16 +04:00
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
nsIGlobalObject* global = nullptr;
|
|
|
|
if (aIncumbentGlobal) {
|
|
|
|
global = xpc::NativeGlobal(aIncumbentGlobal);
|
|
|
|
}
|
2018-08-04 00:11:39 +03:00
|
|
|
JS::RootedObject jobGlobal(aCx, JS::CurrentGlobalOrNull(aCx));
|
2018-10-10 00:42:22 +03:00
|
|
|
RefPtr<PromiseJobRunnable> runnable = new PromiseJobRunnable(
|
|
|
|
aPromise, aJob, jobGlobal, aAllocationSite, global);
|
Bug 1145201: Replace EnqueuePromiseJobCallback and GetIncumbentGlobalCallback with new JobQueue abstract base class. r=arai,smaug
While the behavior of ECMAScript Promises and their associated job queue is
covered by the ECMAScript standard, the HTML specification amends that with
additional behavior the web platform requires. To support this, SpiderMonkey
provides hooks the embedding can set to replace SpiderMonkey's queue with its
own implementation.
At present, these hooks are C-style function-pointer-and-void-pointer pairs,
which are awkward to handle and mistake-prone, as passing a function the wrong
void* is not a type error. Later patches in this series must add new hooks,
making a bad situation worse.
A C++ abstract base class is a well-typed alternative. This introduces a new
`JS::JobQueue` abstract class, and adapts SpiderMonkey's internal job queue and
Gecko's customization to use it. `GetIncumbentGlobalCallback` and
`EnqueuePromiseJobCallback` become virtual methods.
Within SpiderMonkey, the patch gathers the various fields of JSContext that
implement the internal queue into their own type, js::InternalJobQueue. Various
jsfriendapi functions become veneers for calls to methods specific to the
derived class. The InternalJobQueue type itself remains private to SpiderMonkey,
as it uses types like TraceableFifo, derived from Fifo, that are not part of
SpiderMonkey's public API.
Within Gecko, CycleCollectedJSContext acquires JS::JobQueue as a private base
class, and a few static methods are cleaned up nicely.
There are a few other hooks defined in js/public/Promise.h that might make sense
to turn into virtual methods on JobQueue. For example,
DispatchToEventLoopCallback, used for resolving promises of results from
off-main-thread tasks, is probably necessarily connected to the JobQueue
implementation in use, so it might not be sensible to set one without the other.
But it was left unchanged to reduce this patch's size.
Differential Revision: https://phabricator.services.mozilla.com/D17544
--HG--
extra : moz-landing-system : lando
2019-02-12 11:16:16 +03:00
|
|
|
DispatchToMicroTask(runnable.forget());
|
2017-02-24 00:23:45 +03:00
|
|
|
return true;
|
2013-06-18 23:02:16 +04:00
|
|
|
}
|
2013-07-09 18:28:15 +04:00
|
|
|
|
2019-02-12 11:14:34 +03:00
|
|
|
// Used only by the SpiderMonkey Debugger API, and even then only via
|
|
|
|
// JS::AutoDebuggerJobQueueInterruption, to ensure that the debuggee's queue is
|
|
|
|
// not affected; see comments in js/public/Promise.h.
|
Bug 1145201: Replace EnqueuePromiseJobCallback and GetIncumbentGlobalCallback with new JobQueue abstract base class. r=arai,smaug
While the behavior of ECMAScript Promises and their associated job queue is
covered by the ECMAScript standard, the HTML specification amends that with
additional behavior the web platform requires. To support this, SpiderMonkey
provides hooks the embedding can set to replace SpiderMonkey's queue with its
own implementation.
At present, these hooks are C-style function-pointer-and-void-pointer pairs,
which are awkward to handle and mistake-prone, as passing a function the wrong
void* is not a type error. Later patches in this series must add new hooks,
making a bad situation worse.
A C++ abstract base class is a well-typed alternative. This introduces a new
`JS::JobQueue` abstract class, and adapts SpiderMonkey's internal job queue and
Gecko's customization to use it. `GetIncumbentGlobalCallback` and
`EnqueuePromiseJobCallback` become virtual methods.
Within SpiderMonkey, the patch gathers the various fields of JSContext that
implement the internal queue into their own type, js::InternalJobQueue. Various
jsfriendapi functions become veneers for calls to methods specific to the
derived class. The InternalJobQueue type itself remains private to SpiderMonkey,
as it uses types like TraceableFifo, derived from Fifo, that are not part of
SpiderMonkey's public API.
Within Gecko, CycleCollectedJSContext acquires JS::JobQueue as a private base
class, and a few static methods are cleaned up nicely.
There are a few other hooks defined in js/public/Promise.h that might make sense
to turn into virtual methods on JobQueue. For example,
DispatchToEventLoopCallback, used for resolving promises of results from
off-main-thread tasks, is probably necessarily connected to the JobQueue
implementation in use, so it might not be sensible to set one without the other.
But it was left unchanged to reduce this patch's size.
Differential Revision: https://phabricator.services.mozilla.com/D17544
--HG--
extra : moz-landing-system : lando
2019-02-12 11:16:16 +03:00
|
|
|
void CycleCollectedJSContext::runJobs(JSContext* aCx) {
|
|
|
|
MOZ_ASSERT(aCx == Context());
|
|
|
|
MOZ_ASSERT(Get() == this);
|
|
|
|
PerformMicroTaskCheckPoint();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CycleCollectedJSContext::empty() const {
|
|
|
|
// This is our override of JS::JobQueue::empty. Since that interface is only
|
|
|
|
// concerned with the ordinary microtask queue, not the debugger microtask
|
|
|
|
// queue, we only report on the former.
|
|
|
|
return mPendingMicroTaskRunnables.empty();
|
|
|
|
}
|
|
|
|
|
2019-02-12 11:14:34 +03:00
|
|
|
// Preserve a debuggee's microtask queue while it is interrupted by the
|
|
|
|
// debugger. See the comments for JS::AutoDebuggerJobQueueInterruption.
|
|
|
|
class CycleCollectedJSContext::SavedMicroTaskQueue
|
|
|
|
: public JS::JobQueue::SavedJobQueue {
|
|
|
|
public:
|
|
|
|
explicit SavedMicroTaskQueue(CycleCollectedJSContext* ccjs) : ccjs(ccjs) {
|
|
|
|
ccjs->mPendingMicroTaskRunnables.swap(mQueue);
|
|
|
|
}
|
|
|
|
|
|
|
|
~SavedMicroTaskQueue() {
|
|
|
|
MOZ_RELEASE_ASSERT(ccjs->mPendingMicroTaskRunnables.empty());
|
|
|
|
ccjs->mPendingMicroTaskRunnables.swap(mQueue);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
CycleCollectedJSContext* ccjs;
|
|
|
|
std::queue<RefPtr<MicroTaskRunnable>> mQueue;
|
|
|
|
};
|
|
|
|
|
|
|
|
js::UniquePtr<JS::JobQueue::SavedJobQueue>
|
|
|
|
CycleCollectedJSContext::saveJobQueue(JSContext* cx) {
|
|
|
|
auto saved = js::MakeUnique<SavedMicroTaskQueue>(this);
|
|
|
|
if (!saved) {
|
|
|
|
// When MakeUnique's allocation fails, the SavedMicroTaskQueue constructor
|
|
|
|
// is never called, so mPendingMicroTaskRunnables is still initialized.
|
|
|
|
JS_ReportOutOfMemory(cx);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return saved;
|
|
|
|
}
|
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
/* static */
|
|
|
|
void CycleCollectedJSContext::PromiseRejectionTrackerCallback(
|
|
|
|
JSContext* aCx, JS::HandleObject aPromise,
|
2018-01-08 06:08:33 +03:00
|
|
|
JS::PromiseRejectionHandlingState state, void* aData) {
|
2015-12-31 16:21:49 +03:00
|
|
|
#ifdef DEBUG
|
2017-02-24 00:23:45 +03:00
|
|
|
CycleCollectedJSContext* self = static_cast<CycleCollectedJSContext*>(aData);
|
|
|
|
#endif // DEBUG
|
|
|
|
MOZ_ASSERT(aCx == self->Context());
|
|
|
|
MOZ_ASSERT(Get() == self);
|
2015-12-31 16:21:49 +03:00
|
|
|
|
2018-01-08 06:08:33 +03:00
|
|
|
if (state == JS::PromiseRejectionHandlingState::Unhandled) {
|
2017-02-24 00:23:45 +03:00
|
|
|
PromiseDebugging::AddUncaughtRejection(aPromise);
|
|
|
|
} else {
|
|
|
|
PromiseDebugging::AddConsumedRejection(aPromise);
|
|
|
|
}
|
2015-12-31 16:21:49 +03:00
|
|
|
}
|
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
already_AddRefed<Exception> CycleCollectedJSContext::GetPendingException()
|
|
|
|
const {
|
2016-08-11 15:39:22 +03:00
|
|
|
MOZ_ASSERT(mJSContext);
|
2016-02-14 16:30:25 +03:00
|
|
|
|
2018-02-06 00:34:05 +03:00
|
|
|
nsCOMPtr<Exception> out = mPendingException;
|
2017-02-24 00:23:45 +03:00
|
|
|
return out.forget();
|
2015-12-31 16:21:49 +03:00
|
|
|
}
|
|
|
|
|
2018-02-06 00:34:05 +03:00
|
|
|
void CycleCollectedJSContext::SetPendingException(Exception* aException) {
|
2016-08-11 15:39:22 +03:00
|
|
|
MOZ_ASSERT(mJSContext);
|
2017-02-24 00:23:45 +03:00
|
|
|
mPendingException = aException;
|
2013-07-09 18:28:15 +04:00
|
|
|
}
|
|
|
|
|
2017-11-17 06:01:27 +03:00
|
|
|
std::queue<RefPtr<MicroTaskRunnable>>&
|
|
|
|
CycleCollectedJSContext::GetMicroTaskQueue() {
|
2016-08-11 15:39:22 +03:00
|
|
|
MOZ_ASSERT(mJSContext);
|
2017-11-17 06:01:27 +03:00
|
|
|
return mPendingMicroTaskRunnables;
|
2013-07-09 18:28:15 +04:00
|
|
|
}
|
|
|
|
|
2017-11-17 06:01:27 +03:00
|
|
|
std::queue<RefPtr<MicroTaskRunnable>>&
|
|
|
|
CycleCollectedJSContext::GetDebuggerMicroTaskQueue() {
|
2017-02-24 00:23:45 +03:00
|
|
|
MOZ_ASSERT(mJSContext);
|
2017-11-17 06:01:27 +03:00
|
|
|
return mDebuggerMicroTaskQueue;
|
2013-08-04 03:55:39 +04:00
|
|
|
}
|
|
|
|
|
2016-09-14 16:47:32 +03:00
|
|
|
void CycleCollectedJSContext::ProcessStableStateQueue() {
|
2016-08-11 15:39:22 +03:00
|
|
|
MOZ_ASSERT(mJSContext);
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
MOZ_RELEASE_ASSERT(!mDoingStableStates);
|
|
|
|
mDoingStableStates = true;
|
|
|
|
|
2018-12-13 23:05:00 +03:00
|
|
|
// When run, one event can add another event to the mStableStateEvents, as
|
|
|
|
// such you can't use iterators here.
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
for (uint32_t i = 0; i < mStableStateEvents.Length(); ++i) {
|
|
|
|
nsCOMPtr<nsIRunnable> event = mStableStateEvents[i].forget();
|
|
|
|
event->Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
mStableStateEvents.Clear();
|
|
|
|
mDoingStableStates = false;
|
|
|
|
}
|
|
|
|
|
2017-11-17 06:01:27 +03:00
|
|
|
void CycleCollectedJSContext::CleanupIDBTransactions(uint32_t aRecursionDepth) {
|
2016-08-11 15:39:22 +03:00
|
|
|
MOZ_ASSERT(mJSContext);
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
MOZ_RELEASE_ASSERT(!mDoingStableStates);
|
|
|
|
mDoingStableStates = true;
|
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
nsTArray<PendingIDBTransactionData> localQueue =
|
|
|
|
std::move(mPendingIDBTransactions);
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < localQueue.Length(); ++i) {
|
2017-11-17 06:01:27 +03:00
|
|
|
PendingIDBTransactionData& data = localQueue[i];
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
if (data.mRecursionDepth != aRecursionDepth) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-11-17 06:01:27 +03:00
|
|
|
nsCOMPtr<nsIRunnable> transaction = data.mTransaction.forget();
|
|
|
|
transaction->Run();
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
localQueue.RemoveElementAt(i--);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the queue has events in it now, they were added from something we
|
|
|
|
// called, so they belong at the end of the queue.
|
2017-11-17 06:01:27 +03:00
|
|
|
localQueue.AppendElements(mPendingIDBTransactions);
|
|
|
|
localQueue.SwapElements(mPendingIDBTransactions);
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
mDoingStableStates = false;
|
|
|
|
}
|
|
|
|
|
2017-11-17 06:01:27 +03:00
|
|
|
void CycleCollectedJSContext::BeforeProcessTask(bool aMightBlock) {
|
|
|
|
// If ProcessNextEvent was called during a microtask callback, we
|
|
|
|
// must process any pending microtasks before blocking in the event loop,
|
|
|
|
// otherwise we may deadlock until an event enters the queue later.
|
|
|
|
if (aMightBlock && PerformMicroTaskCheckPoint()) {
|
|
|
|
// If any microtask was processed, we post a dummy event in order to
|
|
|
|
// force the ProcessNextEvent call not to block. This is required
|
|
|
|
// to support nested event loops implemented using a pattern like
|
|
|
|
// "while (condition) thread.processNextEvent(true)", in case the
|
|
|
|
// condition is triggered here by a Promise "then" callback.
|
|
|
|
NS_DispatchToMainThread(new Runnable("BeforeProcessTask"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-14 16:47:32 +03:00
|
|
|
void CycleCollectedJSContext::AfterProcessTask(uint32_t aRecursionDepth) {
|
2016-08-11 15:39:22 +03:00
|
|
|
MOZ_ASSERT(mJSContext);
|
2016-02-14 16:30:25 +03:00
|
|
|
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
// See HTML 6.1.4.2 Processing model
|
|
|
|
|
|
|
|
// Step 4.1: Execute microtasks.
|
2017-11-17 06:01:27 +03:00
|
|
|
PerformMicroTaskCheckPoint();
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
|
|
|
|
// Step 4.2 Execute any events that were waiting for a stable state.
|
|
|
|
ProcessStableStateQueue();
|
2017-10-30 13:07:42 +03:00
|
|
|
|
|
|
|
// This should be a fast test so that it won't affect the next task
|
|
|
|
// processing.
|
|
|
|
IsIdleGCTaskNeeded();
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
}
|
|
|
|
|
2017-11-17 06:01:27 +03:00
|
|
|
void CycleCollectedJSContext::AfterProcessMicrotasks() {
|
2016-08-11 15:39:22 +03:00
|
|
|
MOZ_ASSERT(mJSContext);
|
2017-11-17 06:01:27 +03:00
|
|
|
// Cleanup Indexed Database transactions:
|
|
|
|
// https://html.spec.whatwg.org/multipage/webappapis.html#perform-a-microtask-checkpoint
|
|
|
|
CleanupIDBTransactions(RecursionDepth());
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
}
|
|
|
|
|
2018-12-13 23:05:00 +03:00
|
|
|
void CycleCollectedJSContext::IsIdleGCTaskNeeded() const {
|
2017-10-30 13:07:42 +03:00
|
|
|
class IdleTimeGCTaskRunnable : public mozilla::IdleRunnable {
|
|
|
|
public:
|
|
|
|
using mozilla::IdleRunnable::IdleRunnable;
|
|
|
|
|
|
|
|
public:
|
|
|
|
NS_IMETHOD Run() override {
|
|
|
|
CycleCollectedJSRuntime* ccrt = CycleCollectedJSRuntime::Get();
|
|
|
|
if (ccrt) {
|
|
|
|
ccrt->RunIdleTimeGCTask();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult Cancel() override { return NS_OK; }
|
|
|
|
};
|
|
|
|
|
2018-07-31 22:34:50 +03:00
|
|
|
if (Runtime()->IsIdleGCTaskNeeded()) {
|
2017-10-30 13:07:42 +03:00
|
|
|
nsCOMPtr<nsIRunnable> gc_task = new IdleTimeGCTaskRunnable();
|
2019-01-26 20:18:05 +03:00
|
|
|
NS_DispatchToCurrentThreadQueue(gc_task.forget(), EventQueuePriority::Idle);
|
2017-10-30 13:07:42 +03:00
|
|
|
Runtime()->SetPendingIdleGCTask();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 23:05:00 +03:00
|
|
|
uint32_t CycleCollectedJSContext::RecursionDepth() const {
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
return mOwningThread->RecursionDepth();
|
|
|
|
}
|
|
|
|
|
2016-09-14 16:47:32 +03:00
|
|
|
void CycleCollectedJSContext::RunInStableState(
|
|
|
|
already_AddRefed<nsIRunnable>&& aRunnable) {
|
2016-08-11 15:39:22 +03:00
|
|
|
MOZ_ASSERT(mJSContext);
|
2018-05-30 22:15:35 +03:00
|
|
|
mStableStateEvents.AppendElement(std::move(aRunnable));
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
}
|
|
|
|
|
2017-11-17 06:01:27 +03:00
|
|
|
void CycleCollectedJSContext::AddPendingIDBTransaction(
|
|
|
|
already_AddRefed<nsIRunnable>&& aTransaction) {
|
2016-08-11 15:39:22 +03:00
|
|
|
MOZ_ASSERT(mJSContext);
|
2016-02-14 16:30:25 +03:00
|
|
|
|
2017-11-17 06:01:27 +03:00
|
|
|
PendingIDBTransactionData data;
|
|
|
|
data.mTransaction = aTransaction;
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(mOwningThread);
|
|
|
|
data.mRecursionDepth = RecursionDepth();
|
|
|
|
|
|
|
|
// There must be an event running to get here.
|
2015-08-22 05:56:56 +03:00
|
|
|
#ifndef MOZ_WIDGET_COCOA
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
MOZ_ASSERT(data.mRecursionDepth > mBaseRecursionDepth);
|
2016-06-21 00:11:00 +03:00
|
|
|
#else
|
|
|
|
// XXX bug 1261143
|
|
|
|
// Recursion depth should be greater than mBaseRecursionDepth,
|
|
|
|
// or the runnable will stay in the queue forever.
|
|
|
|
if (data.mRecursionDepth <= mBaseRecursionDepth) {
|
|
|
|
data.mRecursionDepth = mBaseRecursionDepth + 1;
|
|
|
|
}
|
2015-08-22 05:56:56 +03:00
|
|
|
#endif
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
mPendingIDBTransactions.AppendElement(std::move(data));
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
}
|
2013-08-04 03:55:39 +04:00
|
|
|
|
2017-11-17 06:01:27 +03:00
|
|
|
void CycleCollectedJSContext::DispatchToMicroTask(
|
|
|
|
already_AddRefed<MicroTaskRunnable> aRunnable) {
|
|
|
|
RefPtr<MicroTaskRunnable> runnable(aRunnable);
|
2015-07-10 02:54:59 +03:00
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(runnable);
|
2015-07-10 02:54:59 +03:00
|
|
|
|
2018-08-02 10:11:57 +03:00
|
|
|
JS::JobQueueMayNotBeEmpty(Context());
|
2017-11-17 06:01:27 +03:00
|
|
|
mPendingMicroTaskRunnables.push(runnable.forget());
|
2013-07-09 18:28:15 +04:00
|
|
|
}
|
|
|
|
|
2017-10-11 15:31:38 +03:00
|
|
|
class AsyncMutationHandler final : public mozilla::Runnable {
|
|
|
|
public:
|
|
|
|
AsyncMutationHandler() : mozilla::Runnable("AsyncMutationHandler") {}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() override {
|
|
|
|
CycleCollectedJSContext* ccjs = CycleCollectedJSContext::Get();
|
|
|
|
if (ccjs) {
|
|
|
|
ccjs->PerformMicroTaskCheckPoint();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-07-19 19:38:33 +03:00
|
|
|
bool CycleCollectedJSContext::PerformMicroTaskCheckPoint(bool aForce) {
|
2017-11-17 06:01:27 +03:00
|
|
|
if (mPendingMicroTaskRunnables.empty() && mDebuggerMicroTaskQueue.empty()) {
|
|
|
|
AfterProcessMicrotasks();
|
2017-10-11 15:31:38 +03:00
|
|
|
// Nothing to do, return early.
|
2017-11-17 06:01:27 +03:00
|
|
|
return false;
|
2017-10-11 15:31:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t currentDepth = RecursionDepth();
|
2018-07-19 19:38:33 +03:00
|
|
|
if (mMicroTaskRecursionDepth >= currentDepth && !aForce) {
|
2017-10-11 15:31:38 +03:00
|
|
|
// We are already executing microtasks for the current recursion depth.
|
2017-11-17 06:01:27 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mTargetedMicroTaskRecursionDepth != 0 &&
|
|
|
|
mTargetedMicroTaskRecursionDepth != currentDepth) {
|
|
|
|
return false;
|
2017-10-11 15:31:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_IsMainThread() && !nsContentUtils::IsSafeToRunScript()) {
|
|
|
|
// Special case for main thread where DOM mutations may happen when
|
|
|
|
// it is not safe to run scripts.
|
|
|
|
nsContentUtils::AddScriptRunner(new AsyncMutationHandler());
|
2017-11-17 06:01:27 +03:00
|
|
|
return false;
|
2017-10-11 15:31:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::AutoRestore<uint32_t> restore(mMicroTaskRecursionDepth);
|
2018-07-19 19:38:33 +03:00
|
|
|
MOZ_ASSERT(aForce ? currentDepth == 0 : currentDepth > 0);
|
2017-10-11 15:31:38 +03:00
|
|
|
mMicroTaskRecursionDepth = currentDepth;
|
|
|
|
|
2017-11-17 06:01:27 +03:00
|
|
|
bool didProcess = false;
|
2017-10-11 15:31:38 +03:00
|
|
|
AutoSlowOperation aso;
|
|
|
|
|
|
|
|
std::queue<RefPtr<MicroTaskRunnable>> suppressed;
|
2017-11-17 06:01:27 +03:00
|
|
|
for (;;) {
|
|
|
|
RefPtr<MicroTaskRunnable> runnable;
|
|
|
|
if (!mDebuggerMicroTaskQueue.empty()) {
|
|
|
|
runnable = mDebuggerMicroTaskQueue.front().forget();
|
|
|
|
mDebuggerMicroTaskQueue.pop();
|
|
|
|
} else if (!mPendingMicroTaskRunnables.empty()) {
|
|
|
|
runnable = mPendingMicroTaskRunnables.front().forget();
|
|
|
|
mPendingMicroTaskRunnables.pop();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-10-11 15:31:38 +03:00
|
|
|
if (runnable->Suppressed()) {
|
2017-11-17 06:01:27 +03:00
|
|
|
// Microtasks in worker shall never be suppressed.
|
|
|
|
// Otherwise, mPendingMicroTaskRunnables will be replaced later with
|
|
|
|
// all suppressed tasks in mDebuggerMicroTaskQueue unexpectedly.
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2018-08-02 10:11:57 +03:00
|
|
|
JS::JobQueueMayNotBeEmpty(Context());
|
2017-10-11 15:31:38 +03:00
|
|
|
suppressed.push(runnable);
|
|
|
|
} else {
|
2018-08-02 10:11:57 +03:00
|
|
|
if (mPendingMicroTaskRunnables.empty() &&
|
|
|
|
mDebuggerMicroTaskQueue.empty() && suppressed.empty()) {
|
|
|
|
JS::JobQueueIsEmpty(Context());
|
|
|
|
}
|
2017-11-17 06:01:27 +03:00
|
|
|
didProcess = true;
|
2017-10-11 15:31:38 +03:00
|
|
|
runnable->Run(aso);
|
|
|
|
}
|
|
|
|
}
|
2017-10-05 18:34:12 +03:00
|
|
|
|
2017-10-11 15:31:38 +03:00
|
|
|
// Put back the suppressed microtasks so that they will be run later.
|
|
|
|
// Note, it is possible that we end up keeping these suppressed tasks around
|
|
|
|
// for some time, but no longer than spinning the event loop nestedly
|
|
|
|
// (sync XHR, alert, etc.)
|
|
|
|
mPendingMicroTaskRunnables.swap(suppressed);
|
2017-11-17 06:01:27 +03:00
|
|
|
|
|
|
|
AfterProcessMicrotasks();
|
|
|
|
|
|
|
|
return didProcess;
|
2017-10-11 15:31:38 +03:00
|
|
|
}
|
|
|
|
|
2017-11-17 06:01:27 +03:00
|
|
|
void CycleCollectedJSContext::PerformDebuggerMicroTaskCheckpoint() {
|
|
|
|
// Don't do normal microtask handling checks here, since whoever is calling
|
|
|
|
// this method is supposed to know what they are doing.
|
|
|
|
|
|
|
|
AutoSlowOperation aso;
|
|
|
|
for (;;) {
|
|
|
|
// For a debugger microtask checkpoint, we always use the debugger microtask
|
|
|
|
// queue.
|
|
|
|
std::queue<RefPtr<MicroTaskRunnable>>* microtaskQueue =
|
|
|
|
&GetDebuggerMicroTaskQueue();
|
|
|
|
|
|
|
|
if (microtaskQueue->empty()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<MicroTaskRunnable> runnable = microtaskQueue->front().forget();
|
|
|
|
MOZ_ASSERT(runnable);
|
2018-03-01 17:29:02 +03:00
|
|
|
|
2017-11-17 06:01:27 +03:00
|
|
|
// This function can re-enter, so we remove the element before calling.
|
|
|
|
microtaskQueue->pop();
|
2018-08-02 10:11:57 +03:00
|
|
|
|
|
|
|
if (mPendingMicroTaskRunnables.empty() && mDebuggerMicroTaskQueue.empty()) {
|
|
|
|
JS::JobQueueIsEmpty(Context());
|
|
|
|
}
|
2017-11-17 06:01:27 +03:00
|
|
|
runnable->Run(aso);
|
|
|
|
}
|
|
|
|
|
|
|
|
AfterProcessMicrotasks();
|
|
|
|
}
|
Bug 1145201: Replace EnqueuePromiseJobCallback and GetIncumbentGlobalCallback with new JobQueue abstract base class. r=arai,smaug
While the behavior of ECMAScript Promises and their associated job queue is
covered by the ECMAScript standard, the HTML specification amends that with
additional behavior the web platform requires. To support this, SpiderMonkey
provides hooks the embedding can set to replace SpiderMonkey's queue with its
own implementation.
At present, these hooks are C-style function-pointer-and-void-pointer pairs,
which are awkward to handle and mistake-prone, as passing a function the wrong
void* is not a type error. Later patches in this series must add new hooks,
making a bad situation worse.
A C++ abstract base class is a well-typed alternative. This introduces a new
`JS::JobQueue` abstract class, and adapts SpiderMonkey's internal job queue and
Gecko's customization to use it. `GetIncumbentGlobalCallback` and
`EnqueuePromiseJobCallback` become virtual methods.
Within SpiderMonkey, the patch gathers the various fields of JSContext that
implement the internal queue into their own type, js::InternalJobQueue. Various
jsfriendapi functions become veneers for calls to methods specific to the
derived class. The InternalJobQueue type itself remains private to SpiderMonkey,
as it uses types like TraceableFifo, derived from Fifo, that are not part of
SpiderMonkey's public API.
Within Gecko, CycleCollectedJSContext acquires JS::JobQueue as a private base
class, and a few static methods are cleaned up nicely.
There are a few other hooks defined in js/public/Promise.h that might make sense
to turn into virtual methods on JobQueue. For example,
DispatchToEventLoopCallback, used for resolving promises of results from
off-main-thread tasks, is probably necessarily connected to the JobQueue
implementation in use, so it might not be sensible to set one without the other.
But it was left unchanged to reduce this patch's size.
Differential Revision: https://phabricator.services.mozilla.com/D17544
--HG--
extra : moz-landing-system : lando
2019-02-12 11:16:16 +03:00
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
} // namespace mozilla
|