1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * vim: set ts=8 sts=2 et sw=2 tw=80:
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/. */
6 
7 /* Pull descriptor objects for tracking byte stream pull-into requests. */
8 
9 #ifndef builtin_streams_PullIntoDescriptor_h
10 #define builtin_streams_PullIntoDescriptor_h
11 
12 #include <stdint.h>  // int32_t, uint32_t
13 
14 #include "js/Class.h"              // JSClass
15 #include "vm/ArrayBufferObject.h"  // js::ArrayBufferObject;
16 #include "vm/NativeObject.h"       // js::NativeObject
17 
18 namespace js {
19 
20 enum class ReaderType : int32_t { Default = 0, BYOB = 1 };
21 
22 class PullIntoDescriptor : public NativeObject {
23  private:
24   enum Slots {
25     Slot_buffer,
26     Slot_ByteOffset,
27     Slot_ByteLength,
28     Slot_BytesFilled,
29     Slot_ElementSize,
30     Slot_Ctor,
31     Slot_ReaderType,
32     SlotCount
33   };
34 
35  public:
36   static const JSClass class_;
37 
buffer()38   ArrayBufferObject* buffer() {
39     return &getFixedSlot(Slot_buffer).toObject().as<ArrayBufferObject>();
40   }
setBuffer(ArrayBufferObject * buffer)41   void setBuffer(ArrayBufferObject* buffer) {
42     setFixedSlot(Slot_buffer, ObjectValue(*buffer));
43   }
ctor()44   JSObject* ctor() { return getFixedSlot(Slot_Ctor).toObjectOrNull(); }
byteOffset()45   uint32_t byteOffset() const {
46     return getFixedSlot(Slot_ByteOffset).toInt32();
47   }
byteLength()48   uint32_t byteLength() const {
49     return getFixedSlot(Slot_ByteLength).toInt32();
50   }
bytesFilled()51   uint32_t bytesFilled() const {
52     return getFixedSlot(Slot_BytesFilled).toInt32();
53   }
setBytesFilled(int32_t bytes)54   void setBytesFilled(int32_t bytes) {
55     setFixedSlot(Slot_BytesFilled, Int32Value(bytes));
56   }
elementSize()57   uint32_t elementSize() const {
58     return getFixedSlot(Slot_ElementSize).toInt32();
59   }
readerType()60   ReaderType readerType() const {
61     int32_t n = getFixedSlot(Slot_ReaderType).toInt32();
62     MOZ_ASSERT(n == int32_t(ReaderType::Default) ||
63                n == int32_t(ReaderType::BYOB));
64     return ReaderType(n);
65   }
66 
67   static PullIntoDescriptor* create(JSContext* cx,
68                                     JS::Handle<ArrayBufferObject*> buffer,
69                                     uint32_t byteOffset, uint32_t byteLength,
70                                     uint32_t bytesFilled, uint32_t elementSize,
71                                     JS::Handle<JSObject*> ctor,
72                                     ReaderType readerType);
73 };
74 
75 }  // namespace js
76 
77 #endif  // builtin_streams_PullIntoDescriptor_h
78