Bug 1850713: remove duplicated setting of early hint preloader id in `ScriptLoader...
[gecko.git] / dom / promise / PromiseNativeHandler.cpp
blob5b4b74d41e76c5bb17065d7924a488f912cf76d6
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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 "PromiseNativeHandler.h"
8 #include "mozilla/dom/BindingUtils.h"
9 #include "mozilla/dom/Promise.h"
10 #include "mozilla/dom/DOMException.h"
11 #include "mozilla/dom/DOMExceptionBinding.h"
12 #include "nsISupportsImpl.h"
14 namespace mozilla::dom {
16 NS_IMPL_ISUPPORTS0(MozPromiseRejectOnDestructionBase)
18 NS_IMPL_ISUPPORTS0(DomPromiseListener)
20 DomPromiseListener::DomPromiseListener(CallbackTypeResolved&& aResolve,
21 CallbackTypeRejected&& aReject)
22 : mResolve(std::move(aResolve)), mReject(std::move(aReject)) {}
24 DomPromiseListener::~DomPromiseListener() {
25 if (mReject) {
26 mReject(NS_BINDING_ABORTED);
30 void DomPromiseListener::ResolvedCallback(JSContext* aCx,
31 JS::Handle<JS::Value> aValue,
32 ErrorResult& aRv) {
33 if (mResolve) {
34 mResolve(aCx, aValue);
36 // Let's clear the lambdas in case we have a cycle to ourselves.
37 Clear();
40 void DomPromiseListener::RejectedCallback(JSContext* aCx,
41 JS::Handle<JS::Value> aValue,
42 ErrorResult& aRv) {
43 if (mReject) {
44 nsresult errorCode = NS_ERROR_DOM_NOT_NUMBER_ERR;
45 if (aValue.isInt32()) {
46 errorCode = nsresult(aValue.toInt32());
47 } else if (aValue.isObject()) {
48 RefPtr<::mozilla::dom::DOMException> domException;
49 UNWRAP_OBJECT(DOMException, aValue, domException);
50 if (domException) {
51 errorCode = domException->GetResult();
54 mReject(errorCode);
56 // Let's clear the lambdas in case we have a cycle to ourselves.
57 Clear();
60 void DomPromiseListener::Clear() {
61 mResolve = nullptr;
62 mReject = nullptr;
65 } // namespace mozilla::dom