script/dom/
testutils.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::rc::Rc;
6
7use dom_struct::dom_struct;
8use js::jsapi::{GCReason, JS_GC};
9use script_bindings::reflector::Reflector;
10use script_bindings::script_runtime::CanGc;
11
12use super::globalscope::GlobalScope;
13use crate::dom::bindings::codegen::Bindings::TestUtilsBinding::TestUtilsMethods;
14use crate::dom::promise::Promise;
15use crate::test::TrustedPromise;
16
17#[dom_struct]
18pub(crate) struct TestUtils {
19    reflector_: Reflector,
20}
21
22impl TestUtilsMethods<crate::DomTypeHolder> for TestUtils {
23    /// <https://testutils.spec.whatwg.org/#dom-testutils-gc>
24    #[allow(unsafe_code)]
25    fn Gc(global: &GlobalScope) -> Rc<Promise> {
26        // 1. Let p be a new promise.
27        let promise = Promise::new(global, CanGc::note());
28        let trusted = TrustedPromise::new(promise.clone());
29        // 2. Run the following in parallel:
30        // 2.1 Run implementation-defined steps to perform a garbage collection covering at least the entry Realm.
31        // 2.2 Resolve p.
32        // We need to spin the event-loop in order get the GC to actually run.
33        // We do this by queuing a task that calls the GC and then resolves the promise.
34        let task = task!(testutils_gc: move || {
35            unsafe {
36                JS_GC(*GlobalScope::get_cx(), GCReason::DOM_TESTUTILS);
37            }
38            let promise = trusted.root();
39            promise.resolve_native(&(), CanGc::note());
40        });
41
42        global
43            .task_manager()
44            .dom_manipulation_task_source()
45            .queue(task);
46
47        promise
48    }
49}