script/dom/
promiserejectionevent.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::ptr::NonNull;
6use std::rc::Rc;
7
8use dom_struct::dom_struct;
9use js::jsapi::{Heap, JSObject};
10use js::jsval::JSVal;
11use js::rust::{HandleObject, HandleValue, MutableHandleValue};
12use stylo_atoms::Atom;
13
14use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
15use crate::dom::bindings::codegen::Bindings::PromiseRejectionEventBinding;
16use crate::dom::bindings::codegen::Bindings::PromiseRejectionEventBinding::PromiseRejectionEventMethods;
17use crate::dom::bindings::error::Fallible;
18use crate::dom::bindings::inheritance::Castable;
19use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
20use crate::dom::bindings::root::DomRoot;
21use crate::dom::bindings::str::DOMString;
22use crate::dom::bindings::trace::RootedTraceableBox;
23use crate::dom::event::{Event, EventBubbles, EventCancelable};
24use crate::dom::globalscope::GlobalScope;
25use crate::dom::promise::Promise;
26use crate::script_runtime::{CanGc, JSContext};
27
28#[dom_struct]
29pub(crate) struct PromiseRejectionEvent {
30    event: Event,
31    #[ignore_malloc_size_of = "Defined in mozjs"]
32    promise: Heap<*mut JSObject>,
33    #[ignore_malloc_size_of = "Defined in mozjs"]
34    reason: Heap<JSVal>,
35}
36
37impl PromiseRejectionEvent {
38    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
39    fn new_inherited() -> Self {
40        PromiseRejectionEvent {
41            event: Event::new_inherited(),
42            promise: Heap::default(),
43            reason: Heap::default(),
44        }
45    }
46
47    pub(crate) fn new(
48        global: &GlobalScope,
49        type_: Atom,
50        bubbles: EventBubbles,
51        cancelable: EventCancelable,
52        promise: Rc<Promise>,
53        reason: HandleValue,
54        can_gc: CanGc,
55    ) -> DomRoot<Self> {
56        Self::new_with_proto(
57            global,
58            None,
59            type_,
60            bubbles,
61            cancelable,
62            promise.promise_obj(),
63            reason,
64            can_gc,
65        )
66    }
67
68    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
69    #[allow(clippy::too_many_arguments)]
70    fn new_with_proto(
71        global: &GlobalScope,
72        proto: Option<HandleObject>,
73        type_: Atom,
74        bubbles: EventBubbles,
75        cancelable: EventCancelable,
76        promise: HandleObject,
77        reason: HandleValue,
78        can_gc: CanGc,
79    ) -> DomRoot<Self> {
80        let ev = reflect_dom_object_with_proto(
81            Box::new(PromiseRejectionEvent::new_inherited()),
82            global,
83            proto,
84            can_gc,
85        );
86        ev.promise.set(promise.get());
87
88        {
89            let event = ev.upcast::<Event>();
90            event.init_event(type_, bool::from(bubbles), bool::from(cancelable));
91
92            ev.reason.set(reason.get());
93        }
94        ev
95    }
96}
97
98impl PromiseRejectionEventMethods<crate::DomTypeHolder> for PromiseRejectionEvent {
99    // https://html.spec.whatwg.org/multipage/#promiserejectionevent
100    fn Constructor(
101        global: &GlobalScope,
102        proto: Option<HandleObject>,
103        can_gc: CanGc,
104        type_: DOMString,
105        init: RootedTraceableBox<PromiseRejectionEventBinding::PromiseRejectionEventInit>,
106    ) -> Fallible<DomRoot<Self>> {
107        let reason = init.reason.handle();
108        let bubbles = EventBubbles::from(init.parent.bubbles);
109        let cancelable = EventCancelable::from(init.parent.cancelable);
110
111        let event = PromiseRejectionEvent::new_with_proto(
112            global,
113            proto,
114            Atom::from(type_),
115            bubbles,
116            cancelable,
117            init.promise.handle(),
118            reason,
119            can_gc,
120        );
121        Ok(event)
122    }
123
124    // https://html.spec.whatwg.org/multipage/#dom-promiserejectionevent-promise
125    fn Promise(&self, _cx: JSContext) -> NonNull<JSObject> {
126        NonNull::new(self.promise.get()).unwrap()
127    }
128
129    // https://html.spec.whatwg.org/multipage/#dom-promiserejectionevent-reason
130    fn Reason(&self, _cx: JSContext, mut retval: MutableHandleValue) {
131        retval.set(self.reason.get())
132    }
133
134    // https://dom.spec.whatwg.org/#dom-event-istrusted
135    fn IsTrusted(&self) -> bool {
136        self.event.IsTrusted()
137    }
138}