Skip to content

Commit

Permalink
Implement queueMicrotask()
Browse files Browse the repository at this point in the history
  • Loading branch information
Fedor committed Oct 30, 2023
1 parent d45b7d1 commit 5047fe5
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 105 deletions.
34 changes: 33 additions & 1 deletion dom/base/nsIGlobalObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,30 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsIGlobalObject.h"

#include "mozilla/CycleCollectedJSContext.h"
#include "mozilla/dom/BlobURLProtocolHandler.h"
#include "mozilla/dom/FunctionBinding.h"
#include "mozilla/dom/ServiceWorker.h"
#include "mozilla/dom/ServiceWorkerRegistration.h"
#include "nsContentUtils.h"
#include "nsThreadUtils.h"
#include "nsGlobalWindowInner.h"

using mozilla::AutoSlowOperation;
using mozilla::CycleCollectedJSContext;
using mozilla::DOMEventTargetHelper;
using mozilla::ErrorResult;
using mozilla::IgnoredErrorResult;
using mozilla::MallocSizeOf;
using mozilla::Maybe;
using mozilla::MicroTaskRunnable;
using mozilla::dom::BlobURLProtocolHandler;
using mozilla::dom::ClientInfo;
using mozilla::dom::ServiceWorker;
using mozilla::dom::ServiceWorkerDescriptor;
using mozilla::dom::ServiceWorkerRegistration;
using mozilla::dom::ServiceWorkerRegistrationDescriptor;
using mozilla::dom::VoidFunction;

bool nsIGlobalObject::IsScriptForbidden(JSObject* aCallback,
bool aIsJSImplementedWebIDL) const {
Expand Down Expand Up @@ -232,3 +239,28 @@ size_t nsIGlobalObject::ShallowSizeOfExcludingThis(MallocSizeOf aSizeOf) const {
size_t rtn = mHostObjectURIs.ShallowSizeOfExcludingThis(aSizeOf);
return rtn;
}

class QueuedMicrotask : public MicroTaskRunnable {
public:
QueuedMicrotask(nsIGlobalObject* aGlobal, VoidFunction& aCallback)
: mGlobal(aGlobal), mCallback(&aCallback) {}

MOZ_CAN_RUN_SCRIPT_BOUNDARY void Run(AutoSlowOperation& aAso) final {
IgnoredErrorResult rv;
MOZ_KnownLive(mCallback)->Call(static_cast<ErrorResult&>(rv));
}

bool Suppressed() final { return mGlobal->IsInSyncOperation(); }

private:
nsCOMPtr<nsIGlobalObject> mGlobal;
RefPtr<VoidFunction> mCallback;
};

void nsIGlobalObject::QueueMicrotask(VoidFunction& aCallback) {
CycleCollectedJSContext* context = CycleCollectedJSContext::Get();
if (context) {
RefPtr<MicroTaskRunnable> mt = new QueuedMicrotask(this, aCallback);
context->DispatchToMicroTask(mt.forget());
}
}
3 changes: 3 additions & 0 deletions dom/base/nsIGlobalObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class nsPIDOMWindowInner;
namespace mozilla {
class DOMEventTargetHelper;
namespace dom {
class VoidFunction;
class DebuggerNotificationManager;
class ServiceWorker;
class ServiceWorkerRegistration;
Expand Down Expand Up @@ -176,6 +177,8 @@ class nsIGlobalObject : public nsISupports,
// nullptr otherwise.
nsPIDOMWindowInner* AsInnerWindow();

void QueueMicrotask(mozilla::dom::VoidFunction& aCallback);

protected:
virtual ~nsIGlobalObject();

Expand Down
3 changes: 3 additions & 0 deletions dom/webidl/WindowOrWorkerGlobalScope.webidl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ interface mixin WindowOrWorkerGlobalScope {
long setInterval(DOMString handler, optional long timeout = 0, any... unused);
void clearInterval(optional long handle = 0);

// microtask queuing
void queueMicrotask(VoidFunction callback);

// ImageBitmap
[Throws]
Promise<ImageBitmap> createImageBitmap(ImageBitmapSource aImage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
if (os == "android") and not e10s: FAIL
if (os == "android") and e10s: FAIL

[Window method: queueMicrotask]
expected: FAIL

[Window unforgeable attribute: window]
expected:
if nightly_build: FAIL
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 5047fe5

Please sign in to comment.