Bug 1769952 - Fix running raptor on a Win10-64 VM r=sparky
[gecko.git] / dom / streams / TransformerCallbackHelpers.cpp
blob6903759199b5d1dc7b754c8a3751448cd0b8644a
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
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 "TransformerCallbackHelpers.h"
9 #include "mozilla/dom/Promise.h"
10 #include "mozilla/dom/TransformStreamDefaultController.h"
12 using namespace mozilla::dom;
14 NS_IMPL_CYCLE_COLLECTION_WITH_JS_MEMBERS(TransformerAlgorithms,
15 (mGlobal, mTransformCallback,
16 mFlushCallback),
17 (mTransformer))
18 NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(TransformerAlgorithms, AddRef)
19 NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(TransformerAlgorithms, Release)
21 // https://streams.spec.whatwg.org/#set-up-transform-stream-default-controller-from-transformer
22 already_AddRefed<Promise> TransformerAlgorithms::TransformCallback(
23 JSContext* aCx, JS::Handle<JS::Value> aChunk,
24 TransformStreamDefaultController& aController, ErrorResult& aRv) {
25 if (!mTransformCallback) {
26 // Step 2.1. Let result be
27 // TransformStreamDefaultControllerEnqueue(controller, chunk).
28 aController.Enqueue(aCx, aChunk, aRv);
30 // Step 2.2. If result is an abrupt completion, return a promise rejected
31 // with result.[[Value]].
32 if (aRv.MaybeSetPendingException(aCx)) {
33 JS::Rooted<JS::Value> error(aCx);
34 if (!JS_GetPendingException(aCx, &error)) {
35 // Uncatchable exception; we should mark aRv and return.
36 aRv.StealExceptionFromJSContext(aCx);
37 return nullptr;
39 JS_ClearPendingException(aCx);
41 RefPtr<Promise> promise =
42 Promise::Create(aController.GetParentObject(), aRv);
43 if (aRv.Failed()) {
44 return nullptr;
46 promise->MaybeReject(error);
47 return promise.forget();
50 // Step 2.3. Otherwise, return a promise resolved with undefined.
51 return Promise::CreateResolvedWithUndefined(aController.GetParentObject(),
52 aRv);
54 // Step 4. If transformerDict["transform"] exists, set transformAlgorithm to
55 // an algorithm which takes an argument chunk and returns the result of
56 // invoking transformerDict["transform"] with argument list « chunk,
57 // controller » and callback this value transformer.
58 JS::Rooted<JSObject*> thisObj(aCx, mTransformer);
59 return MOZ_KnownLive(mTransformCallback)
60 ->Call(thisObj, aChunk, aController, aRv,
61 "TransformStreamDefaultController.[[transformAlgorithm]]",
62 CallbackObject::eRethrowExceptions);
65 // https://streams.spec.whatwg.org/#set-up-transform-stream-default-controller-from-transformer
66 already_AddRefed<Promise> TransformerAlgorithms::FlushCallback(
67 JSContext* aCx, TransformStreamDefaultController& aController,
68 ErrorResult& aRv) {
69 if (!mFlushCallback) {
70 // Step 3. Let flushAlgorithm be an algorithm which returns a promise
71 // resolved with undefined.
72 return Promise::CreateResolvedWithUndefined(aController.GetParentObject(),
73 aRv);
75 // Step 5. If transformerDict["flush"] exists, set flushAlgorithm to an
76 // algorithm which returns the result of invoking transformerDict["flush"]
77 // with argument list « controller » and callback this value transformer.
78 JS::Rooted<JSObject*> thisObj(aCx, mTransformer);
79 return MOZ_KnownLive(mFlushCallback)
80 ->Call(thisObj, aController, aRv,
81 "TransformStreamDefaultController.[[flushAlgorithm]]",
82 CallbackObject::eRethrowExceptions);