Debian Codegraph @

js/src/builtin/DataViewObject.h

raw
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 #ifndef vm_DataViewObject_h
8 #define vm_DataViewObject_h
9
10 #include "mozilla/CheckedInt.h"
11 #include "mozilla/Maybe.h"
12
13 #include "js/Class.h"
14 #include "vm/ArrayBufferViewObject.h"
15 #include "vm/JSObject.h"
16
17 namespace js {
18
19 class ArrayBufferObjectMaybeShared;
20
21 // In the DataViewObject, the private slot contains a raw pointer into
22 // the buffer. The buffer may be shared memory and the raw pointer
23 // should not be exposed without sharedness information accompanying
24 // it.
25 //
26 // DataViewObject is an abstract base class and has exactly two concrete
27 // subclasses, FixedLengthDataViewObject and ResizableDataViewObject.
28
29 class DataViewObject : public ArrayBufferViewObject {
30 protected:
31 static const ClassSpec classSpec_;
32
33 private:
34 template <typename NativeType>
35 SharedMem<uint8_t*> getDataPointer(uint64_t offset, size_t length,
36 bool* isSharedMemory);
37
38 static bool bufferGetterImpl(JSContext* cx, const CallArgs& args);
39 static bool bufferGetter(JSContext* cx, unsigned argc, Value* vp);
40
41 static bool byteLengthGetterImpl(JSContext* cx, const CallArgs& args);
42 static bool byteLengthGetter(JSContext* cx, unsigned argc, Value* vp);
43
44 static bool byteOffsetGetterImpl(JSContext* cx, const CallArgs& args);
45 static bool byteOffsetGetter(JSContext* cx, unsigned argc, Value* vp);
46
47 static bool getAndCheckConstructorArgs(JSContext* cx, HandleObject bufobj,
48 const CallArgs& args,
49 size_t* byteOffsetPtr,
50 size_t* byteLengthPtr,
51 AutoLength* autoLengthPtr);
52 static bool constructSameCompartment(JSContext* cx, HandleObject bufobj,
53 const CallArgs& args);
54 static bool constructWrapped(JSContext* cx, HandleObject bufobj,
55 const CallArgs& args);
56
57 static DataViewObject* create(
58 JSContext* cx, size_t byteOffset, size_t byteLength,
59 Handle<ArrayBufferObjectMaybeShared*> arrayBuffer, HandleObject proto);
60
61 public:
62 static const JSClass protoClass_;
63
64 /**
65 * Return the current byteLength, or |Nothing| if the DataView is detached or
66 * out-of-bounds.
67 */
68 mozilla::Maybe<size_t> byteLength() {
69 return ArrayBufferViewObject::length();
70 }
71
72 template <typename NativeType>
73 static bool offsetIsInBounds(uint64_t offset, size_t byteLength) {
74 return offsetIsInBounds(sizeof(NativeType), offset, byteLength);
75 }
76 static bool offsetIsInBounds(uint32_t byteSize, uint64_t offset,
77 size_t byteLength) {
78 MOZ_ASSERT(byteSize <= 8);
79 mozilla::CheckedInt<uint64_t> endOffset(offset);
80 endOffset += byteSize;
81 return endOffset.isValid() && endOffset.value() <= byteLength;
82 }
83
84 static bool isOriginalByteOffsetGetter(Native native) {
85 return native == byteOffsetGetter;
86 }
87
88 static bool isOriginalByteLengthGetter(Native native) {
89 return native == byteLengthGetter;
90 }
91
92 static bool construct(JSContext* cx, unsigned argc, Value* vp);
93
94 static bool getInt8Impl(JSContext* cx, const CallArgs& args);
95 static bool fun_getInt8(JSContext* cx, unsigned argc, Value* vp);
96
97 static bool getUint8Impl(JSContext* cx, const CallArgs& args);
98 static bool fun_getUint8(JSContext* cx, unsigned argc, Value* vp);
99
100 static bool getInt16Impl(JSContext* cx, const CallArgs& args);
101 static bool fun_getInt16(JSContext* cx, unsigned argc, Value* vp);
102
103 static bool getUint16Impl(JSContext* cx, const CallArgs& args);
104 static bool fun_getUint16(JSContext* cx, unsigned argc, Value* vp);
105
106 static bool getInt32Impl(JSContext* cx, const CallArgs& args);
107 static bool fun_getInt32(JSContext* cx, unsigned argc, Value* vp);
108
109 static bool getUint32Impl(JSContext* cx, const CallArgs& args);
110 static bool fun_getUint32(JSContext* cx, unsigned argc, Value* vp);
111
112 static bool getBigInt64Impl(JSContext* cx, const CallArgs& args);
113 static bool fun_getBigInt64(JSContext* cx, unsigned argc, Value* vp);
114
115 static bool getBigUint64Impl(JSContext* cx, const CallArgs& args);
116 static bool fun_getBigUint64(JSContext* cx, unsigned argc, Value* vp);
117
118 static bool getFloat16Impl(JSContext* cx, const CallArgs& args);
119 static bool fun_getFloat16(JSContext* cx, unsigned argc, Value* vp);
120
121 static bool getFloat32Impl(JSContext* cx, const CallArgs& args);
122 static bool fun_getFloat32(JSContext* cx, unsigned argc, Value* vp);
123
124 static bool getFloat64Impl(JSContext* cx, const CallArgs& args);
125 static bool fun_getFloat64(JSContext* cx, unsigned argc, Value* vp);
126
127 static bool setInt8Impl(JSContext* cx, const CallArgs& args);
128 static bool fun_setInt8(JSContext* cx, unsigned argc, Value* vp);
129
130 static bool setUint8Impl(JSContext* cx, const CallArgs& args);
131 static bool fun_setUint8(JSContext* cx, unsigned argc, Value* vp);
132
133 static bool setInt16Impl(JSContext* cx, const CallArgs& args);
134 static bool fun_setInt16(JSContext* cx, unsigned argc, Value* vp);
135
136 static bool setUint16Impl(JSContext* cx, const CallArgs& args);
137 static bool fun_setUint16(JSContext* cx, unsigned argc, Value* vp);
138
139 static bool setInt32Impl(JSContext* cx, const CallArgs& args);
140 static bool fun_setInt32(JSContext* cx, unsigned argc, Value* vp);
141
142 static bool setUint32Impl(JSContext* cx, const CallArgs& args);
143 static bool fun_setUint32(JSContext* cx, unsigned argc, Value* vp);
144
145 static bool setBigInt64Impl(JSContext* cx, const CallArgs& args);
146 static bool fun_setBigInt64(JSContext* cx, unsigned argc, Value* vp);
147
148 static bool setBigUint64Impl(JSContext* cx, const CallArgs& args);
149 static bool fun_setBigUint64(JSContext* cx, unsigned argc, Value* vp);
150
151 static bool setFloat16Impl(JSContext* cx, const CallArgs& args);
152 static bool fun_setFloat16(JSContext* cx, unsigned argc, Value* vp);
153
154 static bool setFloat32Impl(JSContext* cx, const CallArgs& args);
155 static bool fun_setFloat32(JSContext* cx, unsigned argc, Value* vp);
156
157 static bool setFloat64Impl(JSContext* cx, const CallArgs& args);
158 static bool fun_setFloat64(JSContext* cx, unsigned argc, Value* vp);
159
160 template <typename NativeType>
161 NativeType read(uint64_t offset, size_t length, bool isLittleEndian);
162
163 template <typename NativeType>
164 static bool read(JSContext* cx, Handle<DataViewObject*> obj,
165 const CallArgs& args, NativeType* val);
166 template <typename NativeType>
167 static bool write(JSContext* cx, Handle<DataViewObject*> obj,
168 const CallArgs& args);
169
170 private:
171 static const JSFunctionSpec methods[];
172 static const JSPropertySpec properties[];
173 };
174
175 /**
176 * DataView whose buffer is a fixed-length (Shared)ArrayBuffer object.
177 */
178 class FixedLengthDataViewObject : public DataViewObject {
179 public:
180 static const JSClass class_;
181
182 size_t byteOffset() const {
183 return ArrayBufferViewObject::byteOffsetSlotValue();
184 }
185
186 size_t byteLength() const { return ArrayBufferViewObject::lengthSlotValue(); }
187 };
188
189 /**
190 * DataView whose buffer is a resizable (Shared)ArrayBuffer object.
191 */
192 class ResizableDataViewObject : public DataViewObject {
193 friend class DataViewObject;
194
195 static ResizableDataViewObject* create(
196 JSContext* cx, size_t byteOffset, size_t byteLength,
197 AutoLength autoLength, Handle<ArrayBufferObjectMaybeShared*> arrayBuffer,
198 HandleObject proto);
199
200 public:
201 static const uint8_t RESERVED_SLOTS = RESIZABLE_RESERVED_SLOTS;
202
203 static const JSClass class_;
204 };
205
206 // For structured cloning.
207 JSObject* NewDataView(JSContext* cx, HandleObject buffer, size_t byteOffset);
208
209 } // namespace js
210
211 template <>
212 inline bool JSObject::is<js::DataViewObject>() const {
213 return is<js::FixedLengthDataViewObject>() ||
214 is<js::ResizableDataViewObject>();
215 }
216
217 #endif /* vm_DataViewObject_h */