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"
|
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"
|
2016-01-19 23:48:22 +03:00
|
|
|
#include "mozilla/dom/ProfileTimelineMarkerBinding.h"
|
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 "mozilla/dom/Promise.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"
|
2013-06-18 23:02:15 +04:00
|
|
|
#include "jsprf.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"
|
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
|
|
|
|
2013-09-09 07:28:50 +04:00
|
|
|
#include "nsIException.h"
|
2016-01-01 08:19:20 +03:00
|
|
|
#include "nsIPlatformInfo.h"
|
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)
|
2016-09-05 18:54:04 +03:00
|
|
|
, mDisableMicroTaskCheckpoint(false)
|
2017-10-05 18:34:12 +03:00
|
|
|
, mMicroTaskLevel(0)
|
2017-10-11 15:31:38 +03:00
|
|
|
, mMicroTaskRecursionDepth(0)
|
2013-06-18 23:02:07 +04:00
|
|
|
{
|
2017-05-09 14:59:00 +03:00
|
|
|
MOZ_COUNT_CTOR(CycleCollectedJSContext);
|
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()
|
2016-02-14 16:30:25 +03:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
ProcessMetastableStateQueue(mBaseRecursionDepth);
|
|
|
|
MOZ_ASSERT(mMetastableStateEvents.IsEmpty());
|
|
|
|
|
|
|
|
ProcessStableStateQueue();
|
|
|
|
MOZ_ASSERT(mStableStateEvents.IsEmpty());
|
|
|
|
|
|
|
|
// Clear mPendingException first, since it might be cycle collected.
|
|
|
|
mPendingException = nullptr;
|
|
|
|
|
2016-05-02 21:21:43 +03:00
|
|
|
MOZ_ASSERT(mDebuggerPromiseMicroTaskQueue.empty());
|
|
|
|
MOZ_ASSERT(mPromiseMicroTaskQueue.empty());
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
JS::SetGetIncumbentGlobalCallback(mJSContext, GetIncumbentGlobalCallback);
|
|
|
|
|
|
|
|
JS::SetEnqueuePromiseJobCallback(mJSContext, EnqueuePromiseJobCallback, this);
|
|
|
|
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()));
|
|
|
|
}
|
|
|
|
|
2016-02-14 16:30:25 +03:00
|
|
|
nsresult
|
2017-02-11 02:47:50 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
size_t
|
|
|
|
CycleCollectedJSContext::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
|
2013-06-18 23:02:16 +04:00
|
|
|
{
|
2017-02-24 00:23:45 +03:00
|
|
|
return 0;
|
2013-06-18 23:02:16 +04:00
|
|
|
}
|
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
class PromiseJobRunnable final : public Runnable
|
2013-06-18 23:02:15 +04:00
|
|
|
{
|
2017-02-24 00:23:45 +03:00
|
|
|
public:
|
2017-06-12 22:34:10 +03:00
|
|
|
PromiseJobRunnable(JS::HandleObject aCallback,
|
|
|
|
JS::HandleObject aAllocationSite,
|
2017-02-24 00:23:45 +03:00
|
|
|
nsIGlobalObject* aIncumbentGlobal)
|
2017-06-12 22:34:10 +03:00
|
|
|
: Runnable("PromiseJobRunnable")
|
|
|
|
, mCallback(
|
|
|
|
new PromiseJobCallback(aCallback, aAllocationSite, aIncumbentGlobal))
|
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:
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() override
|
|
|
|
{
|
|
|
|
JSObject* callback = mCallback->CallbackPreserveColor();
|
|
|
|
nsIGlobalObject* global = callback ? xpc::NativeGlobal(callback) : nullptr;
|
|
|
|
if (global && !global->IsDying()) {
|
|
|
|
mCallback->Call("promise callback");
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2017-03-17 01:41:36 +03:00
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
private:
|
|
|
|
RefPtr<PromiseJobCallback> mCallback;
|
|
|
|
};
|
2017-03-05 12:23:33 +03:00
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
/* static */
|
|
|
|
JSObject*
|
|
|
|
CycleCollectedJSContext::GetIncumbentGlobalCallback(JSContext* aCx)
|
2013-06-18 23:02:15 +04:00
|
|
|
{
|
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
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
/* static */
|
|
|
|
bool
|
|
|
|
CycleCollectedJSContext::EnqueuePromiseJobCallback(JSContext* aCx,
|
|
|
|
JS::HandleObject aJob,
|
|
|
|
JS::HandleObject aAllocationSite,
|
|
|
|
JS::HandleObject aIncumbentGlobal,
|
|
|
|
void* aData)
|
2013-06-18 23:02:16 +04:00
|
|
|
{
|
2017-02-24 00:23:45 +03:00
|
|
|
CycleCollectedJSContext* self = static_cast<CycleCollectedJSContext*>(aData);
|
|
|
|
MOZ_ASSERT(aCx == self->Context());
|
|
|
|
MOZ_ASSERT(Get() == self);
|
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);
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIRunnable> runnable = new PromiseJobRunnable(aJob, aAllocationSite, global);
|
|
|
|
self->DispatchToMicroTask(runnable.forget());
|
|
|
|
return true;
|
2013-06-18 23:02:16 +04:00
|
|
|
}
|
2013-07-09 18:28:15 +04:00
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
/* static */
|
2015-12-31 16:21:49 +03:00
|
|
|
void
|
2017-02-24 00:23:45 +03:00
|
|
|
CycleCollectedJSContext::PromiseRejectionTrackerCallback(JSContext* aCx,
|
|
|
|
JS::HandleObject aPromise,
|
2018-01-08 06:08:33 +03:00
|
|
|
JS::PromiseRejectionHandlingState state,
|
2017-02-24 00:23:45 +03:00
|
|
|
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<nsIException>
|
|
|
|
CycleCollectedJSContext::GetPendingException() const
|
2015-12-31 16:21:49 +03:00
|
|
|
{
|
2016-08-11 15:39:22 +03:00
|
|
|
MOZ_ASSERT(mJSContext);
|
2016-02-14 16:30:25 +03:00
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
nsCOMPtr<nsIException> out = mPendingException;
|
|
|
|
return out.forget();
|
2015-12-31 16:21:49 +03:00
|
|
|
}
|
|
|
|
|
2013-07-09 18:28:15 +04:00
|
|
|
void
|
2017-02-24 00:23:45 +03:00
|
|
|
CycleCollectedJSContext::SetPendingException(nsIException* aException)
|
2013-07-09 18:28:15 +04:00
|
|
|
{
|
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-02-24 00:23:45 +03:00
|
|
|
std::queue<nsCOMPtr<nsIRunnable>>&
|
|
|
|
CycleCollectedJSContext::GetPromiseMicroTaskQueue()
|
2013-07-09 18:28:15 +04:00
|
|
|
{
|
2016-08-11 15:39:22 +03:00
|
|
|
MOZ_ASSERT(mJSContext);
|
2017-02-24 00:23:45 +03:00
|
|
|
return mPromiseMicroTaskQueue;
|
2013-07-09 18:28:15 +04:00
|
|
|
}
|
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
std::queue<nsCOMPtr<nsIRunnable>>&
|
|
|
|
CycleCollectedJSContext::GetDebuggerPromiseMicroTaskQueue()
|
2013-08-04 03:55:39 +04:00
|
|
|
{
|
2017-02-24 00:23:45 +03:00
|
|
|
MOZ_ASSERT(mJSContext);
|
|
|
|
return mDebuggerPromiseMicroTaskQueue;
|
2013-08-04 03:55:39 +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
|
|
|
void
|
2016-09-14 16:47:32 +03:00
|
|
|
CycleCollectedJSContext::ProcessStableStateQueue()
|
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
|
|
|
{
|
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;
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mStableStateEvents.Length(); ++i) {
|
|
|
|
nsCOMPtr<nsIRunnable> event = mStableStateEvents[i].forget();
|
|
|
|
event->Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
mStableStateEvents.Clear();
|
|
|
|
mDoingStableStates = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-09-14 16:47:32 +03:00
|
|
|
CycleCollectedJSContext::ProcessMetastableStateQueue(uint32_t aRecursionDepth)
|
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
|
|
|
{
|
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;
|
|
|
|
|
|
|
|
nsTArray<RunInMetastableStateData> localQueue = Move(mMetastableStateEvents);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < localQueue.Length(); ++i)
|
|
|
|
{
|
|
|
|
RunInMetastableStateData& data = localQueue[i];
|
|
|
|
if (data.mRecursionDepth != aRecursionDepth) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIRunnable> runnable = data.mRunnable.forget();
|
|
|
|
runnable->Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
localQueue.AppendElements(mMetastableStateEvents);
|
|
|
|
localQueue.SwapElements(mMetastableStateEvents);
|
|
|
|
mDoingStableStates = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-09-14 16:47:32 +03:00
|
|
|
CycleCollectedJSContext::AfterProcessTask(uint32_t aRecursionDepth)
|
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
|
|
|
{
|
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
|
|
|
|
|
|
|
|
// Execute any events that were waiting for a microtask to complete.
|
|
|
|
// This is not (yet) in the spec.
|
|
|
|
ProcessMetastableStateQueue(aRecursionDepth);
|
|
|
|
|
|
|
|
// Step 4.1: Execute microtasks.
|
2016-09-05 18:54:04 +03:00
|
|
|
if (!mDisableMicroTaskCheckpoint) {
|
2017-10-11 15:31:38 +03:00
|
|
|
PerformMicroTaskCheckPoint();
|
2016-09-05 18:54:04 +03:00
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
Promise::PerformMicroTaskCheckpoint();
|
|
|
|
} else {
|
|
|
|
Promise::PerformWorkerMicroTaskCheckpoint();
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-09-14 16:47:32 +03:00
|
|
|
CycleCollectedJSContext::AfterProcessMicrotask()
|
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
|
|
|
{
|
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
|
|
|
AfterProcessMicrotask(RecursionDepth());
|
|
|
|
}
|
|
|
|
|
2017-10-30 13:07:42 +03:00
|
|
|
void CycleCollectedJSContext::IsIdleGCTaskNeeded()
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (Runtime()->IsIdleGCTaskNeeded()) {
|
|
|
|
nsCOMPtr<nsIRunnable> gc_task = new IdleTimeGCTaskRunnable();
|
|
|
|
NS_IdleDispatchToCurrentThread(gc_task.forget());
|
|
|
|
Runtime()->SetPendingIdleGCTask();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
void
|
2016-09-14 16:47:32 +03:00
|
|
|
CycleCollectedJSContext::AfterProcessMicrotask(uint32_t aRecursionDepth)
|
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
|
|
|
{
|
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
|
|
|
// Between microtasks, execute any events that were waiting for a microtask
|
|
|
|
// to complete.
|
|
|
|
ProcessMetastableStateQueue(aRecursionDepth);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
2016-09-14 16:47:32 +03:00
|
|
|
CycleCollectedJSContext::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
|
|
|
{
|
|
|
|
return mOwningThread->RecursionDepth();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-09-14 16:47:32 +03:00
|
|
|
CycleCollectedJSContext::RunInStableState(already_AddRefed<nsIRunnable>&& 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
|
|
|
{
|
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
|
|
|
mStableStateEvents.AppendElement(Move(aRunnable));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-09-14 16:47:32 +03:00
|
|
|
CycleCollectedJSContext::RunInMetastableState(already_AddRefed<nsIRunnable>&& 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
|
|
|
{
|
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
|
|
|
RunInMetastableStateData data;
|
|
|
|
data.mRunnable = aRunnable;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
mMetastableStateEvents.AppendElement(Move(data));
|
|
|
|
}
|
2013-08-04 03:55:39 +04:00
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
void
|
|
|
|
CycleCollectedJSContext::DispatchToMicroTask(already_AddRefed<nsIRunnable> aRunnable)
|
2013-07-09 18:28:15 +04:00
|
|
|
{
|
2017-02-24 00:23:45 +03:00
|
|
|
RefPtr<nsIRunnable> 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
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
mPromiseMicroTaskQueue.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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-05 18:34:12 +03:00
|
|
|
void
|
2017-10-11 15:31:38 +03:00
|
|
|
CycleCollectedJSContext::PerformMicroTaskCheckPoint()
|
2017-10-05 18:34:12 +03:00
|
|
|
{
|
2017-10-11 15:31:38 +03:00
|
|
|
if (mPendingMicroTaskRunnables.empty()) {
|
|
|
|
// Nothing to do, return early.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t currentDepth = RecursionDepth();
|
|
|
|
if (mMicroTaskRecursionDepth >= currentDepth) {
|
|
|
|
// We are already executing microtasks for the current recursion depth.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::AutoRestore<uint32_t> restore(mMicroTaskRecursionDepth);
|
|
|
|
MOZ_ASSERT(currentDepth > 0);
|
|
|
|
mMicroTaskRecursionDepth = currentDepth;
|
|
|
|
|
|
|
|
AutoSlowOperation aso;
|
|
|
|
|
|
|
|
std::queue<RefPtr<MicroTaskRunnable>> suppressed;
|
|
|
|
while (!mPendingMicroTaskRunnables.empty()) {
|
|
|
|
RefPtr<MicroTaskRunnable> runnable =
|
|
|
|
mPendingMicroTaskRunnables.front().forget();
|
|
|
|
mPendingMicroTaskRunnables.pop();
|
|
|
|
if (runnable->Suppressed()) {
|
|
|
|
suppressed.push(runnable);
|
|
|
|
} else {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CycleCollectedJSContext::DispatchMicroTaskRunnable(
|
|
|
|
already_AddRefed<MicroTaskRunnable> aRunnable)
|
|
|
|
{
|
|
|
|
mPendingMicroTaskRunnables.push(aRunnable);
|
2017-10-05 18:34:12 +03:00
|
|
|
}
|
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
} // namespace mozilla
|