1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/TestUtils.h"
8 #include "mozilla/dom/TestUtilsBinding.h"
10 #include "mozilla/ErrorResult.h"
11 #include "mozilla/dom/Promise.h"
12 #include "mozilla/dom/WorkerCommon.h"
13 #include "mozilla/dom/WorkerPrivate.h"
14 #include "nsJSEnvironment.h"
15 #include "xpcpublic.h"
17 namespace mozilla::dom
{
19 already_AddRefed
<Promise
> TestUtils::Gc(const GlobalObject
& aGlobal
,
21 nsCOMPtr
<nsIGlobalObject
> global
= do_QueryInterface(aGlobal
.GetAsSupports());
22 RefPtr
<Promise
> promise
= Promise::Create(global
, aRv
);
23 if (NS_WARN_IF(aRv
.Failed())) {
27 // TODO(krosylight): Ideally we could use nsJSContext::IncrementalGC to make
28 // it actually async, but that's not required right now.
30 NS_DispatchToCurrentThread(
31 NS_NewCancelableRunnableFunction("TestUtils::Gc", [promise
] {
32 if (NS_IsMainThread()) {
33 nsJSContext::GarbageCollectNow(JS::GCReason::DOM_TESTUTILS
,
34 nsJSContext::NonShrinkingGC
);
35 nsJSContext::CycleCollectNow(CCReason::API
);
37 WorkerPrivate
* workerPrivate
= GetCurrentThreadWorkerPrivate();
38 workerPrivate
->GarbageCollectInternal(workerPrivate
->GetJSContext(),
39 false /* shrinking */,
40 false /* collect children */);
41 workerPrivate
->CycleCollectInternal(false);
44 promise
->MaybeResolveWithUndefined();
47 return promise
.forget();
50 } // namespace mozilla::dom