summaryrefslogtreecommitdiffstats
path: root/dom/bindings/IterableIterator.cpp
blob: d0f2e5d2271a0047c9546f575e932b577374e27d (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/* -*- 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/IterableIterator.h"
#include "mozilla/dom/Promise-inl.h"

namespace mozilla::dom {

// Due to IterableIterator being a templated class, we implement the necessary
// CC bits in a superclass that IterableIterator then inherits from. This allows
// us to put the macros outside of the header. The base class has pure virtual
// functions for Traverse/Unlink that the templated subclasses will override.

NS_IMPL_CYCLE_COLLECTION_CLASS(IterableIteratorBase)

NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(IterableIteratorBase)
  tmp->TraverseHelper(cb);
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END

NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(IterableIteratorBase)
  tmp->UnlinkHelper();
NS_IMPL_CYCLE_COLLECTION_UNLINK_END

namespace iterator_utils {

void DictReturn(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
                bool aDone, JS::Handle<JS::Value> aValue, ErrorResult& aRv) {
  RootedDictionary<IterableKeyOrValueResult> dict(aCx);
  dict.mDone = aDone;
  dict.mValue = aValue;
  JS::Rooted<JS::Value> dictValue(aCx);
  if (!ToJSValue(aCx, dict, &dictValue)) {
    aRv.Throw(NS_ERROR_FAILURE);
    return;
  }
  aResult.set(dictValue);
}

void DictReturn(JSContext* aCx, JS::MutableHandle<JSObject*> aResult,
                bool aDone, JS::Handle<JS::Value> aValue, ErrorResult& aRv) {
  JS::Rooted<JS::Value> dictValue(aCx);
  DictReturn(aCx, &dictValue, aDone, aValue, aRv);
  if (aRv.Failed()) {
    return;
  }
  aResult.set(&dictValue.toObject());
}

void KeyAndValueReturn(JSContext* aCx, JS::Handle<JS::Value> aKey,
                       JS::Handle<JS::Value> aValue,
                       JS::MutableHandle<JSObject*> aResult, ErrorResult& aRv) {
  RootedDictionary<IterableKeyAndValueResult> dict(aCx);
  dict.mDone = false;
  // Dictionary values are a Sequence, which is a FallibleTArray, so we need
  // to check returns when appending.
  if (!dict.mValue.AppendElement(aKey, mozilla::fallible)) {
    aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
    return;
  }
  if (!dict.mValue.AppendElement(aValue, mozilla::fallible)) {
    aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
    return;
  }
  JS::Rooted<JS::Value> dictValue(aCx);
  if (!ToJSValue(aCx, dict, &dictValue)) {
    aRv.Throw(NS_ERROR_FAILURE);
    return;
  }
  aResult.set(&dictValue.toObject());
}

}  // namespace iterator_utils

namespace binding_detail {

static already_AddRefed<Promise> PromiseOrErr(
    Result<RefPtr<Promise>, nsresult>&& aResult, ErrorResult& aError) {
  if (aResult.isErr()) {
    aError.Throw(aResult.unwrapErr());
    return nullptr;
  }

  return aResult.unwrap().forget();
}

already_AddRefed<Promise> AsyncIterableNextImpl::NextSteps(
    JSContext* aCx, AsyncIterableIteratorBase* aObject,
    nsIGlobalObject* aGlobalObject, ErrorResult& aRv) {
  // 2. If object’s is finished is true, then:
  if (aObject->mIsFinished) {
    // 1. Let result be CreateIterResultObject(undefined, true).
    JS::Rooted<JS::Value> dict(aCx);
    iterator_utils::DictReturn(aCx, &dict, true, JS::UndefinedHandleValue, aRv);
    if (aRv.Failed()) {
      return Promise::CreateRejectedWithErrorResult(aGlobalObject, aRv);
    }

    // 2. Perform ! Call(nextPromiseCapability.[[Resolve]], undefined,
    //    «result»).
    // 3. Return nextPromiseCapability.[[Promise]].
    return Promise::Resolve(aGlobalObject, aCx, dict, aRv);
  }

  // 4. Let nextPromise be the result of getting the next iteration result with
  //    object’s target and object.
  RefPtr<Promise> nextPromise;
  {
    ErrorResult error;
    nextPromise = GetNextResult(error);

    error.WouldReportJSException();
    if (error.Failed()) {
      nextPromise = Promise::Reject(aGlobalObject, std::move(error), aRv);
    }
  }

  // 5. Let fulfillSteps be the following steps, given next:
  auto fulfillSteps = [](JSContext* aCx, JS::Handle<JS::Value> aNext,
                         ErrorResult& aRv,
                         const RefPtr<AsyncIterableIteratorBase>& aObject,
                         const nsCOMPtr<nsIGlobalObject>& aGlobalObject)
      -> already_AddRefed<Promise> {
    // 1. Set object’s ongoing promise to null.
    aObject->mOngoingPromise = nullptr;

    // 2. If next is end of iteration, then:
    JS::Rooted<JS::Value> dict(aCx);
    if (aNext.isMagic(binding_details::END_OF_ITERATION)) {
      // 1. Set object’s is finished to true.
      aObject->mIsFinished = true;
      // 2. Return CreateIterResultObject(undefined, true).
      iterator_utils::DictReturn(aCx, &dict, true, JS::UndefinedHandleValue,
                                 aRv);
      if (aRv.Failed()) {
        return nullptr;
      }
    } else {
      // 3. Otherwise, if interface has a pair asynchronously iterable
      // declaration:
      //   1. Assert: next is a value pair.
      //   2. Return the iterator result for next and kind.
      // 4. Otherwise:
      //   1. Assert: interface has a value asynchronously iterable declaration.
      //   2. Assert: next is a value of the type that appears in the
      //   declaration.
      //   3. Let value be next, converted to an ECMAScript value.
      //   4. Return CreateIterResultObject(value, false).
      iterator_utils::DictReturn(aCx, &dict, false, aNext, aRv);
      if (aRv.Failed()) {
        return nullptr;
      }
    }
    // Note that ThenCatchWithCycleCollectedArgs expects a Promise, so
    // we use Promise::Resolve here. The specs do convert this to a
    // promise too at another point, but the end result should be the
    // same.
    return Promise::Resolve(aGlobalObject, aCx, dict, aRv);
  };
  // 7. Let rejectSteps be the following steps, given reason:
  auto rejectSteps = [](JSContext* aCx, JS::Handle<JS::Value> aReason,
                        ErrorResult& aRv,
                        const RefPtr<AsyncIterableIteratorBase>& aObject,
                        const nsCOMPtr<nsIGlobalObject>& aGlobalObject) {
    // 1. Set object’s ongoing promise to null.
    aObject->mOngoingPromise = nullptr;
    // 2. Set object’s is finished to true.
    aObject->mIsFinished = true;
    // 3. Throw reason.
    return Promise::Reject(aGlobalObject, aCx, aReason, aRv);
  };
  // 9. Perform PerformPromiseThen(nextPromise, onFulfilled, onRejected,
  //    nextPromiseCapability).
  Result<RefPtr<Promise>, nsresult> result =
      nextPromise->ThenCatchWithCycleCollectedArgs(
          std::move(fulfillSteps), std::move(rejectSteps), RefPtr{aObject},
          nsCOMPtr{aGlobalObject});

  // 10. Return nextPromiseCapability.[[Promise]].
  return PromiseOrErr(std::move(result), aRv);
}

already_AddRefed<Promise> AsyncIterableNextImpl::Next(
    JSContext* aCx, AsyncIterableIteratorBase* aObject,
    nsISupports* aGlobalObject, ErrorResult& aRv) {
  nsCOMPtr<nsIGlobalObject> globalObject = do_QueryInterface(aGlobalObject);

  // 3.7.10.2. Asynchronous iterator prototype object
  // …
  // 10. If ongoingPromise is not null, then:
  if (aObject->mOngoingPromise) {
    // 1. Let afterOngoingPromiseCapability be
    //    ! NewPromiseCapability(%Promise%).
    // 2. Let onSettled be CreateBuiltinFunction(nextSteps, « »).

    // aObject is the same object as 'this', so it's fine to capture 'this'
    // without taking a strong reference, because we already take a strong
    // reference to it through aObject.
    auto onSettled = [this](JSContext* aCx, JS::Handle<JS::Value> aValue,
                            ErrorResult& aRv,
                            const RefPtr<AsyncIterableIteratorBase>& aObject,
                            const nsCOMPtr<nsIGlobalObject>& aGlobalObject)
                         MOZ_CAN_RUN_SCRIPT_FOR_DEFINITION {
                           return NextSteps(aCx, aObject, aGlobalObject, aRv);
                         };

    // 3. Perform PerformPromiseThen(ongoingPromise, onSettled, onSettled,
    //    afterOngoingPromiseCapability).
    Result<RefPtr<Promise>, nsresult> afterOngoingPromise =
        aObject->mOngoingPromise->ThenCatchWithCycleCollectedArgs(
            onSettled, onSettled, RefPtr{aObject}, std::move(globalObject));
    if (afterOngoingPromise.isErr()) {
      aRv.Throw(afterOngoingPromise.unwrapErr());
      return nullptr;
    }

    // 4. Set object’s ongoing promise to
    //    afterOngoingPromiseCapability.[[Promise]].
    aObject->mOngoingPromise = afterOngoingPromise.unwrap().forget();
  } else {
    // 11. Otherwise:
    //   1. Set object’s ongoing promise to the result of running nextSteps.
    aObject->mOngoingPromise = NextSteps(aCx, aObject, globalObject, aRv);
  }

  // 12. Return object’s ongoing promise.
  return do_AddRef(aObject->mOngoingPromise);
}

already_AddRefed<Promise> AsyncIterableReturnImpl::ReturnSteps(
    JSContext* aCx, AsyncIterableIteratorBase* aObject,
    nsIGlobalObject* aGlobalObject, JS::Handle<JS::Value> aValue,
    ErrorResult& aRv) {
  // 2. If object’s is finished is true, then:
  if (aObject->mIsFinished) {
    // 1. Let result be CreateIterResultObject(value, true).
    JS::Rooted<JS::Value> dict(aCx);
    iterator_utils::DictReturn(aCx, &dict, true, aValue, aRv);
    if (aRv.Failed()) {
      return Promise::CreateRejectedWithErrorResult(aGlobalObject, aRv);
    }

    // 2. Perform ! Call(returnPromiseCapability.[[Resolve]], undefined,
    //    «result»).
    // 3. Return returnPromiseCapability.[[Promise]].
    return Promise::Resolve(aGlobalObject, aCx, dict, aRv);
  }

  // 3. Set object’s is finished to true.
  aObject->mIsFinished = true;

  // 4. Return the result of running the asynchronous iterator return algorithm
  // for interface, given object’s target, object, and value.
  ErrorResult error;
  RefPtr<Promise> returnPromise = GetReturnPromise(aCx, aValue, error);

  error.WouldReportJSException();
  if (error.Failed()) {
    return Promise::Reject(aGlobalObject, std::move(error), aRv);
  }

  return returnPromise.forget();
}

already_AddRefed<Promise> AsyncIterableReturnImpl::Return(
    JSContext* aCx, AsyncIterableIteratorBase* aObject,
    nsISupports* aGlobalObject, JS::Handle<JS::Value> aValue,
    ErrorResult& aRv) {
  nsCOMPtr<nsIGlobalObject> globalObject = do_QueryInterface(aGlobalObject);

  // 3.7.10.2. Asynchronous iterator prototype object
  // …
  RefPtr<Promise> returnStepsPromise;
  // 11. If ongoingPromise is not null, then:
  if (aObject->mOngoingPromise) {
    // 1. Let afterOngoingPromiseCapability be
    //    ! NewPromiseCapability(%Promise%).
    // 2. Let onSettled be CreateBuiltinFunction(returnSteps, « »).

    // aObject is the same object as 'this', so it's fine to capture 'this'
    // without taking a strong reference, because we already take a strong
    // reference to it through aObject.
    auto onSettled =
        [this](JSContext* aCx, JS::Handle<JS::Value> aValue, ErrorResult& aRv,
               const RefPtr<AsyncIterableIteratorBase>& aObject,
               const nsCOMPtr<nsIGlobalObject>& aGlobalObject,
               JS::Handle<JS::Value> aVal) MOZ_CAN_RUN_SCRIPT_FOR_DEFINITION {
          return ReturnSteps(aCx, aObject, aGlobalObject, aVal, aRv);
        };

    // 3. Perform PerformPromiseThen(ongoingPromise, onSettled, onSettled,
    //    afterOngoingPromiseCapability).
    Result<RefPtr<Promise>, nsresult> afterOngoingPromise =
        aObject->mOngoingPromise->ThenCatchWithCycleCollectedArgsJS(
            onSettled, onSettled,
            std::make_tuple(RefPtr{aObject}, nsCOMPtr{globalObject}),
            std::make_tuple(aValue));
    if (afterOngoingPromise.isErr()) {
      aRv.Throw(afterOngoingPromise.unwrapErr());
      return nullptr;
    }

    // 4. Set returnStepsPromise to afterOngoingPromiseCapability.[[Promise]].
    returnStepsPromise = afterOngoingPromise.unwrap().forget();
  } else {
    // 12. Otherwise:
    //   1. Set returnStepsPromise to the result of running returnSteps.
    returnStepsPromise = ReturnSteps(aCx, aObject, globalObject, aValue, aRv);
  }

  // 13. Let fulfillSteps be the following steps:
  auto onFullFilled = [](JSContext* aCx, JS::Handle<JS::Value>,
                         ErrorResult& aRv,
                         const nsCOMPtr<nsIGlobalObject>& aGlobalObject,
                         JS::Handle<JS::Value> aVal) {
    // 1. Return CreateIterResultObject(value, true).
    JS::Rooted<JS::Value> dict(aCx);
    iterator_utils::DictReturn(aCx, &dict, true, aVal, aRv);
    return Promise::Resolve(aGlobalObject, aCx, dict, aRv);
  };

  // 14. Let onFulfilled be CreateBuiltinFunction(fulfillSteps, « »).
  // 15. Perform PerformPromiseThen(returnStepsPromise, onFulfilled, undefined,
  // returnPromiseCapability).
  Result<RefPtr<Promise>, nsresult> returnPromise =
      returnStepsPromise->ThenWithCycleCollectedArgsJS(
          onFullFilled, std::make_tuple(std::move(globalObject)),
          std::make_tuple(aValue));

  // 16. Return returnPromiseCapability.[[Promise]].
  return PromiseOrErr(std::move(returnPromise), aRv);
}

}  // namespace binding_detail

}  // namespace mozilla::dom