summaryrefslogtreecommitdiffstats
path: root/dom/bindings/test/TestInterfaceAsyncIterableSingleWithArgs.cpp
blob: 6c02829186102b7c0faf56cfdeb5c7a076c05888 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/dom/TestInterfaceAsyncIterableSingleWithArgs.h"
#include "ScriptSettings.h"
#include "js/Value.h"
#include "mozilla/dom/TestInterfaceJSMaplikeSetlikeIterableBinding.h"
#include "nsPIDOMWindow.h"
#include "mozilla/dom/BindingUtils.h"
#include "mozilla/dom/IterableIterator.h"
#include "mozilla/dom/Promise-inl.h"

namespace mozilla::dom {

NS_IMPL_CYCLE_COLLECTION_CLASS(TestInterfaceAsyncIterableSingleWithArgs)

NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(
    TestInterfaceAsyncIterableSingleWithArgs, TestInterfaceAsyncIterableSingle)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END

NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(
    TestInterfaceAsyncIterableSingleWithArgs, TestInterfaceAsyncIterableSingle)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END

NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(
    TestInterfaceAsyncIterableSingleWithArgs, TestInterfaceAsyncIterableSingle)
  NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mReturnLastCalledWith)
NS_IMPL_CYCLE_COLLECTION_TRACE_END

NS_IMPL_ADDREF_INHERITED(TestInterfaceAsyncIterableSingleWithArgs,
                         TestInterfaceAsyncIterableSingle)
NS_IMPL_RELEASE_INHERITED(TestInterfaceAsyncIterableSingleWithArgs,
                          TestInterfaceAsyncIterableSingle)

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(
    TestInterfaceAsyncIterableSingleWithArgs)
NS_INTERFACE_MAP_END_INHERITING(TestInterfaceAsyncIterableSingle)

// static
already_AddRefed<TestInterfaceAsyncIterableSingleWithArgs>
TestInterfaceAsyncIterableSingleWithArgs::Constructor(
    const GlobalObject& aGlobal, ErrorResult& aRv) {
  nsCOMPtr<nsPIDOMWindowInner> window =
      do_QueryInterface(aGlobal.GetAsSupports());
  if (!window) {
    aRv.Throw(NS_ERROR_FAILURE);
    return nullptr;
  }

  RefPtr<TestInterfaceAsyncIterableSingleWithArgs> r =
      new TestInterfaceAsyncIterableSingleWithArgs(window);
  return r.forget();
}

JSObject* TestInterfaceAsyncIterableSingleWithArgs::WrapObject(
    JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
  return TestInterfaceAsyncIterableSingleWithArgs_Binding::Wrap(aCx, this,
                                                                aGivenProto);
}

void TestInterfaceAsyncIterableSingleWithArgs::InitAsyncIteratorData(
    IteratorData& aData, Iterator::IteratorType aType,
    const TestInterfaceAsyncIteratorOptions& aOptions, ErrorResult& aError) {
  aData.mMultiplier = aOptions.mMultiplier;
  aData.mBlockingPromises = aOptions.mBlockingPromises;
  aData.mFailNextAfter = aOptions.mFailNextAfter;
  aData.mThrowFromNext = aOptions.mThrowFromNext;
  if (aOptions.mThrowFromReturn.WasPassed()) {
    aData.mThrowFromReturn = &aOptions.mThrowFromReturn.Value();
  }
}

already_AddRefed<Promise>
TestInterfaceAsyncIterableSingleWithArgs::GetNextIterationResult(
    Iterator* aIterator, ErrorResult& aRv) {
  if (aIterator->Data().mThrowFromNext) {
    AutoJSAPI jsapi;
    MOZ_RELEASE_ASSERT(jsapi.Init(GetParentObject()));
    JS_ReportErrorASCII(jsapi.cx(), "Throwing from next().");
    aRv.MightThrowJSException();
    aRv.StealExceptionFromJSContext(jsapi.cx());
    return nullptr;
  }

  if (aIterator->Data().mIndex == aIterator->Data().mFailNextAfter) {
    aRv.ThrowTypeError("Failed because we of 'failNextAfter'.");
    return nullptr;
  }
  return TestInterfaceAsyncIterableSingle::GetNextIterationResult(
      aIterator, aIterator->Data(), aRv);
}

already_AddRefed<Promise>
TestInterfaceAsyncIterableSingleWithArgs::IteratorReturn(
    JSContext* aCx, Iterator* aIterator, JS::Handle<JS::Value> aValue,
    ErrorResult& aRv) {
  ++mReturnCallCount;

  if (RefPtr<TestThrowingCallback> throwFromReturn =
          aIterator->Data().mThrowFromReturn) {
    throwFromReturn->Call(aRv, nullptr, CallbackFunction::eRethrowExceptions);
    return nullptr;
  }

  RefPtr<Promise> promise = Promise::Create(GetParentObject()->AsGlobal(), aRv);
  if (NS_WARN_IF(aRv.Failed())) {
    return nullptr;
  }

  mReturnLastCalledWith = aValue;
  promise->MaybeResolve(JS::UndefinedHandleValue);
  return promise.forget();
}

}  // namespace mozilla::dom