Bug 1941128 - Turn off network.dns.native_https_query on Mac again
[gecko.git] / dom / bindings / test / TestInterfaceAsyncIterableSingleWithArgs.cpp
blob6c02829186102b7c0faf56cfdeb5c7a076c05888
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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/TestInterfaceAsyncIterableSingleWithArgs.h"
8 #include "ScriptSettings.h"
9 #include "js/Value.h"
10 #include "mozilla/dom/TestInterfaceJSMaplikeSetlikeIterableBinding.h"
11 #include "nsPIDOMWindow.h"
12 #include "mozilla/dom/BindingUtils.h"
13 #include "mozilla/dom/IterableIterator.h"
14 #include "mozilla/dom/Promise-inl.h"
16 namespace mozilla::dom {
18 NS_IMPL_CYCLE_COLLECTION_CLASS(TestInterfaceAsyncIterableSingleWithArgs)
20 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(
21 TestInterfaceAsyncIterableSingleWithArgs, TestInterfaceAsyncIterableSingle)
22 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
24 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(
25 TestInterfaceAsyncIterableSingleWithArgs, TestInterfaceAsyncIterableSingle)
26 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
28 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(
29 TestInterfaceAsyncIterableSingleWithArgs, TestInterfaceAsyncIterableSingle)
30 NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mReturnLastCalledWith)
31 NS_IMPL_CYCLE_COLLECTION_TRACE_END
33 NS_IMPL_ADDREF_INHERITED(TestInterfaceAsyncIterableSingleWithArgs,
34 TestInterfaceAsyncIterableSingle)
35 NS_IMPL_RELEASE_INHERITED(TestInterfaceAsyncIterableSingleWithArgs,
36 TestInterfaceAsyncIterableSingle)
38 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(
39 TestInterfaceAsyncIterableSingleWithArgs)
40 NS_INTERFACE_MAP_END_INHERITING(TestInterfaceAsyncIterableSingle)
42 // static
43 already_AddRefed<TestInterfaceAsyncIterableSingleWithArgs>
44 TestInterfaceAsyncIterableSingleWithArgs::Constructor(
45 const GlobalObject& aGlobal, ErrorResult& aRv) {
46 nsCOMPtr<nsPIDOMWindowInner> window =
47 do_QueryInterface(aGlobal.GetAsSupports());
48 if (!window) {
49 aRv.Throw(NS_ERROR_FAILURE);
50 return nullptr;
53 RefPtr<TestInterfaceAsyncIterableSingleWithArgs> r =
54 new TestInterfaceAsyncIterableSingleWithArgs(window);
55 return r.forget();
58 JSObject* TestInterfaceAsyncIterableSingleWithArgs::WrapObject(
59 JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
60 return TestInterfaceAsyncIterableSingleWithArgs_Binding::Wrap(aCx, this,
61 aGivenProto);
64 void TestInterfaceAsyncIterableSingleWithArgs::InitAsyncIteratorData(
65 IteratorData& aData, Iterator::IteratorType aType,
66 const TestInterfaceAsyncIteratorOptions& aOptions, ErrorResult& aError) {
67 aData.mMultiplier = aOptions.mMultiplier;
68 aData.mBlockingPromises = aOptions.mBlockingPromises;
69 aData.mFailNextAfter = aOptions.mFailNextAfter;
70 aData.mThrowFromNext = aOptions.mThrowFromNext;
71 if (aOptions.mThrowFromReturn.WasPassed()) {
72 aData.mThrowFromReturn = &aOptions.mThrowFromReturn.Value();
76 already_AddRefed<Promise>
77 TestInterfaceAsyncIterableSingleWithArgs::GetNextIterationResult(
78 Iterator* aIterator, ErrorResult& aRv) {
79 if (aIterator->Data().mThrowFromNext) {
80 AutoJSAPI jsapi;
81 MOZ_RELEASE_ASSERT(jsapi.Init(GetParentObject()));
82 JS_ReportErrorASCII(jsapi.cx(), "Throwing from next().");
83 aRv.MightThrowJSException();
84 aRv.StealExceptionFromJSContext(jsapi.cx());
85 return nullptr;
88 if (aIterator->Data().mIndex == aIterator->Data().mFailNextAfter) {
89 aRv.ThrowTypeError("Failed because we of 'failNextAfter'.");
90 return nullptr;
92 return TestInterfaceAsyncIterableSingle::GetNextIterationResult(
93 aIterator, aIterator->Data(), aRv);
96 already_AddRefed<Promise>
97 TestInterfaceAsyncIterableSingleWithArgs::IteratorReturn(
98 JSContext* aCx, Iterator* aIterator, JS::Handle<JS::Value> aValue,
99 ErrorResult& aRv) {
100 ++mReturnCallCount;
102 if (RefPtr<TestThrowingCallback> throwFromReturn =
103 aIterator->Data().mThrowFromReturn) {
104 throwFromReturn->Call(aRv, nullptr, CallbackFunction::eRethrowExceptions);
105 return nullptr;
108 RefPtr<Promise> promise = Promise::Create(GetParentObject()->AsGlobal(), aRv);
109 if (NS_WARN_IF(aRv.Failed())) {
110 return nullptr;
113 mReturnLastCalledWith = aValue;
114 promise->MaybeResolve(JS::UndefinedHandleValue);
115 return promise.forget();
118 } // namespace mozilla::dom