summaryrefslogtreecommitdiffstats
path: root/js/src/builtin/streams/TeeState.cpp
blob: c8c151fd8a398d9f88ac756336f9ff5da4e0e5e4 (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
/* -*- 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/. */

/* Stream teeing state. */

#include "builtin/streams/TeeState.h"

#include "builtin/streams/ReadableStream.h"  // js::ReadableStream
#include "js/Class.h"          // JSClass, JSCLASS_HAS_RESERVED_SLOTS
#include "js/RootingAPI.h"     // JS::Handle, JS::Rooted
#include "vm/JSContext.h"      // JSContext
#include "vm/PromiseObject.h"  // js::PromiseObject

#include "vm/JSObject-inl.h"  // js::NewBuiltinClassInstance

using js::ReadableStream;
using js::TeeState;

using JS::Handle;
using JS::Int32Value;
using JS::ObjectValue;
using JS::Rooted;

/* static */ TeeState* TeeState::create(
    JSContext* cx, Handle<ReadableStream*> unwrappedStream) {
  Rooted<TeeState*> state(cx, NewBuiltinClassInstance<TeeState>(cx));
  if (!state) {
    return nullptr;
  }

  Rooted<PromiseObject*> cancelPromise(
      cx, PromiseObject::createSkippingExecutor(cx));
  if (!cancelPromise) {
    return nullptr;
  }

  state->setFixedSlot(Slot_Flags, Int32Value(0));
  state->setFixedSlot(Slot_CancelPromise, ObjectValue(*cancelPromise));
  Rooted<JSObject*> wrappedStream(cx, unwrappedStream);
  if (!cx->compartment()->wrap(cx, &wrappedStream)) {
    return nullptr;
  }
  state->setFixedSlot(Slot_Stream, JS::ObjectValue(*wrappedStream));

  return state;
}

const JSClass TeeState::class_ = {"TeeState",
                                  JSCLASS_HAS_RESERVED_SLOTS(SlotCount)};