1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3#ifndef QV4ARRAYBUFFER_H
4#define QV4ARRAYBUFFER_H
5
6//
7// W A R N I N G
8// -------------
9//
10// This file is not part of the Qt API. It exists purely as an
11// implementation detail. This header file may change from version to
12// version without notice, or even be removed.
13//
14// We mean it.
15//
16
17#include "qv4object_p.h"
18#include "qv4functionobject_p.h"
19#include <QtCore/qarraydatapointer.h>
20
21QT_BEGIN_NAMESPACE
22
23namespace QV4 {
24
25namespace Heap {
26
27struct SharedArrayBufferCtor : FunctionObject {
28 void init(QV4::ExecutionContext *scope);
29};
30
31struct ArrayBufferCtor : SharedArrayBufferCtor {
32 void init(QV4::ExecutionContext *scope);
33};
34
35struct Q_QML_PRIVATE_EXPORT SharedArrayBuffer : Object {
36 void init(size_t length);
37 void init(const QByteArray& array);
38 void destroy();
39
40 void setSharedArrayBuffer(bool shared) noexcept { isShared = shared; }
41 bool isSharedArrayBuffer() const noexcept { return isShared; }
42
43 char *arrayData() noexcept { return arrayDataPointer()->data(); }
44 const char *constArrayData() const noexcept { return constArrayDataPointer()->data(); }
45 uint arrayDataLength() const noexcept { return constArrayDataPointer().size; }
46
47 bool hasSharedArrayData() const noexcept { return constArrayDataPointer().isShared(); }
48 bool hasDetachedArrayData() const noexcept { return constArrayDataPointer().isNull(); }
49 void detachArrayData() noexcept { arrayDataPointer().clear(); }
50
51 bool arrayDataNeedsDetach() const noexcept { return constArrayDataPointer().needsDetach(); }
52
53private:
54 const QArrayDataPointer<const char> &constArrayDataPointer() const noexcept
55 {
56 return *reinterpret_cast<const QArrayDataPointer<const char> *>(&arrayDataPointerStorage);
57 }
58 QArrayDataPointer<char> &arrayDataPointer() noexcept
59 {
60 return *reinterpret_cast<QArrayDataPointer<char> *>(&arrayDataPointerStorage);
61 }
62
63 template <typename T>
64 struct storage_t { alignas(T) unsigned char data[sizeof(T)]; };
65
66 storage_t<QArrayDataPointer<char>>
67 arrayDataPointerStorage;
68 bool isShared;
69};
70
71struct Q_QML_PRIVATE_EXPORT ArrayBuffer : SharedArrayBuffer {
72 void init(size_t length) {
73 SharedArrayBuffer::init(length);
74 setSharedArrayBuffer(false);
75 }
76 void init(const QByteArray& array) {
77 SharedArrayBuffer::init(array);
78 setSharedArrayBuffer(false);
79 }
80};
81
82}
83
84struct SharedArrayBufferCtor : FunctionObject
85{
86 V4_OBJECT2(SharedArrayBufferCtor, FunctionObject)
87
88 static ReturnedValue virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *);
89 static ReturnedValue virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc);
90};
91
92struct ArrayBufferCtor : SharedArrayBufferCtor
93{
94 V4_OBJECT2(ArrayBufferCtor, SharedArrayBufferCtor)
95
96 static ReturnedValue virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *);
97
98 static ReturnedValue method_isView(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
99};
100
101struct Q_QML_PRIVATE_EXPORT SharedArrayBuffer : Object
102{
103 V4_OBJECT2(SharedArrayBuffer, Object)
104 V4_NEEDS_DESTROY
105 V4_PROTOTYPE(sharedArrayBufferPrototype)
106
107 QByteArray asByteArray() const;
108
109 uint arrayDataLength() const { return d()->arrayDataLength(); }
110 char *arrayData() { return d()->arrayData(); }
111 const char *constArrayData() const { return d()->constArrayData(); }
112
113 bool hasSharedArrayData() { return d()->hasSharedArrayData(); }
114 bool hasDetachedArrayData() const { return d()->hasDetachedArrayData(); }
115 bool isSharedArrayBuffer() const { return d()->isSharedArrayBuffer(); }
116};
117
118struct Q_QML_PRIVATE_EXPORT ArrayBuffer : SharedArrayBuffer
119{
120 V4_OBJECT2(ArrayBuffer, SharedArrayBuffer)
121 V4_NEEDS_DESTROY
122 V4_PROTOTYPE(arrayBufferPrototype)
123
124 QByteArray asByteArray() const;
125 uint arrayDataLength() const { return d()->arrayDataLength(); }
126 char *dataData() { if (d()->arrayDataNeedsDetach()) detach(); return d()->arrayData(); }
127 // ### is that detach needed?
128 const char *constArrayData() const { return d()->constArrayData(); }
129 bool hasSharedArrayData() { return d()->hasSharedArrayData(); }
130 void detachArrayData() { d()->detachArrayData(); }
131
132 void detach();
133};
134
135struct SharedArrayBufferPrototype : Object
136{
137 void init(ExecutionEngine *engine, Object *ctor);
138
139 static ReturnedValue method_get_byteLength(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
140 static ReturnedValue method_slice(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
141
142 static ReturnedValue slice(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc, bool shared);
143};
144
145struct ArrayBufferPrototype : SharedArrayBufferPrototype
146{
147 void init(ExecutionEngine *engine, Object *ctor);
148
149 static ReturnedValue method_get_byteLength(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
150 static ReturnedValue method_slice(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
151 static ReturnedValue method_toString(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
152};
153
154
155} // namespace QV4
156
157QT_END_NAMESPACE
158
159#endif
160

source code of qtdeclarative/src/qml/jsruntime/qv4arraybuffer_p.h