summaryrefslogtreecommitdiffstats
path: root/js/src/builtin/streams/ReadableStreamReader-inl.h
blob: 5d084c3a76ff775187b2a196ca3c5e6fc9df999d (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
/* -*- 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/. */

#ifndef builtin_streams_ReadableStreamReader_inl_h
#define builtin_streams_ReadableStreamReader_inl_h

#include "builtin/streams/ReadableStreamReader.h"

#include "mozilla/Assertions.h"  // MOZ_ASSERT
#include "mozilla/Attributes.h"  // MOZ_MUST_USE

#include "jsfriendapi.h"  // JS_IsDeadWrapper

#include "builtin/streams/ReadableStream.h"  // js::ReadableStream
#include "js/Proxy.h"                        // js::IsProxy
#include "js/RootingAPI.h"                   // JS::Handle
#include "vm/NativeObject.h"                 // js::NativeObject::getFixedSlot

#include "vm/Compartment-inl.h"  // js::UnwrapInternalSlot

namespace js {

/**
 * Returns the stream associated with the given reader.
 */
inline MOZ_MUST_USE ReadableStream* UnwrapStreamFromReader(
    JSContext* cx, JS::Handle<ReadableStreamReader*> reader) {
  MOZ_ASSERT(reader->hasStream());
  return UnwrapInternalSlot<ReadableStream>(cx, reader,
                                            ReadableStreamReader::Slot_Stream);
}

/**
 * Returns the reader associated with the given stream.
 *
 * Must only be called on ReadableStreams that already have a reader
 * associated with them.
 *
 * If the reader is a wrapper, it will be unwrapped, so the result might not be
 * an object from the currently active compartment.
 */
inline MOZ_MUST_USE ReadableStreamReader* UnwrapReaderFromStream(
    JSContext* cx, JS::Handle<ReadableStream*> stream) {
  return UnwrapInternalSlot<ReadableStreamReader>(cx, stream,
                                                  ReadableStream::Slot_Reader);
}

inline MOZ_MUST_USE ReadableStreamReader* UnwrapReaderFromStreamNoThrow(
    ReadableStream* stream) {
  JSObject* readerObj =
      &stream->getFixedSlot(ReadableStream::Slot_Reader).toObject();
  if (IsProxy(readerObj)) {
    if (JS_IsDeadWrapper(readerObj)) {
      return nullptr;
    }

    readerObj = readerObj->maybeUnwrapAs<ReadableStreamReader>();
    if (!readerObj) {
      return nullptr;
    }
  }

  return &readerObj->as<ReadableStreamReader>();
}

}  // namespace js

#endif  // builtin_streams_ReadableStreamReader_inl_h