TestUtils.cpp (1923B)
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/. */ 6 7 #include "mozilla/dom/TestUtils.h" 8 9 #include "mozilla/ErrorResult.h" 10 #include "mozilla/dom/Promise.h" 11 #include "mozilla/dom/TestUtilsBinding.h" 12 #include "mozilla/dom/WorkerCommon.h" 13 #include "mozilla/dom/WorkerPrivate.h" 14 #include "nsJSEnvironment.h" 15 #include "xpcpublic.h" 16 17 namespace mozilla::dom { 18 19 already_AddRefed<Promise> TestUtils::Gc(const GlobalObject& aGlobal, 20 ErrorResult& aRv) { 21 nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports()); 22 RefPtr<Promise> promise = Promise::Create(global, aRv); 23 if (NS_WARN_IF(aRv.Failed())) { 24 return nullptr; 25 } 26 27 // TODO(krosylight): Ideally we could use nsJSContext::IncrementalGC to make 28 // it actually async, but that's not required right now. 29 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); 36 } else { 37 WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate(); 38 workerPrivate->GarbageCollectInternal(workerPrivate->GetJSContext(), 39 false /* shrinking */, 40 false /* collect children */); 41 workerPrivate->CycleCollectInternal(false); 42 } 43 44 promise->MaybeResolveWithUndefined(); 45 })); 46 47 return promise.forget(); 48 } 49 50 } // namespace mozilla::dom