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    fn new_inherited() -> Self {
39        PromiseRejectionEvent {
40            event: Event::new_inherited(),
41            promise: Heap::default(),
42            reason: Heap::default(),
43        }
44    }
45
46    pub(crate) fn new(
47        global: &GlobalScope,
48        type_: Atom,
49        bubbles: EventBubbles,
50        cancelable: EventCancelable,
51        promise: Rc<Promise>,
52        reason: HandleValue,
53        can_gc: CanGc,
54    ) -> DomRoot<Self> {
55        Self::new_with_proto(
56            global,
57            None,
58            type_,
59            bubbles,
60            cancelable,
61            promise.promise_obj(),
62            reason,
63            can_gc,
64        )
65    }
66
67    #[allow(clippy::too_many_arguments)]
68    fn new_with_proto(
69        global: &GlobalScope,
70        proto: Option<HandleObject>,
71        type_: Atom,
72        bubbles: EventBubbles,
73        cancelable: EventCancelable,
74        promise: HandleObject,
75        reason: HandleValue,
76        can_gc: CanGc,
77    ) -> DomRoot<Self> {
78        let ev = reflect_dom_object_with_proto(
79            Box::new(PromiseRejectionEvent::new_inherited()),
80            global,
81            proto,
82            can_gc,
83        );
84        ev.promise.set(promise.get());
85
86        {
87            let event = ev.upcast::<Event>();
88            event.init_event(type_, bool::from(bubbles), bool::from(cancelable));
89
90            ev.reason.set(reason.get());
91        }
92        ev
93    }
94}
95
96impl PromiseRejectionEventMethods<crate::DomTypeHolder> for PromiseRejectionEvent {
97    /// <https://html.spec.whatwg.org/multipage/#promiserejectionevent>
98    fn Constructor(
99        global: &GlobalScope,
100        proto: Option<HandleObject>,
101        can_gc: CanGc,
102        type_: DOMString,
103        init: RootedTraceableBox<PromiseRejectionEventBinding::PromiseRejectionEventInit>,
104    ) -> Fallible<DomRoot<Self>> {
105        let reason = init.reason.handle();
106        let bubbles = EventBubbles::from(init.parent.bubbles);
107        let cancelable = EventCancelable::from(init.parent.cancelable);
108
109        let event = PromiseRejectionEvent::new_with_proto(
110            global,
111            proto,
112            Atom::from(type_),
113            bubbles,
114            cancelable,
115            init.promise.handle(),
116            reason,
117            can_gc,
118        );
119        Ok(event)
120    }
121
122    /// <https://html.spec.whatwg.org/multipage/#dom-promiserejectionevent-promise>
123    fn Promise(&self, _cx: JSContext) -> NonNull<JSObject> {
124        NonNull::new(self.promise.get()).unwrap()
125    }
126
127    /// <https://html.spec.whatwg.org/multipage/#dom-promiserejectionevent-reason>
128    fn Reason(&self, _cx: JSContext, mut retval: MutableHandleValue) {
129        retval.set(self.reason.get())
130    }
131
132    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
133    fn IsTrusted(&self) -> bool {
134        self.event.IsTrusted()
135    }
136}