1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 #ifndef QV4TYPEDARRAY_H
40 #define QV4TYPEDARRAY_H
41 
42 //
43 //  W A R N I N G
44 //  -------------
45 //
46 // This file is not part of the Qt API.  It exists purely as an
47 // implementation detail.  This header file may change from version to
48 // version without notice, or even be removed.
49 //
50 // We mean it.
51 //
52 
53 #include "qv4object_p.h"
54 #include "qv4functionobject_p.h"
55 #include "qv4arraybuffer_p.h"
56 
57 QT_BEGIN_NAMESPACE
58 
59 namespace QV4 {
60 
61 struct ArrayBuffer;
62 
63 enum TypedArrayType {
64     Int8Array,
65     UInt8Array,
66     Int16Array,
67     UInt16Array,
68     Int32Array,
69     UInt32Array,
70     UInt8ClampedArray,
71     Float32Array,
72     Float64Array,
73     NTypedArrayTypes
74 };
75 
76 enum AtomicModifyOps {
77     AtomicAdd,
78     AtomicAnd,
79     AtomicExchange,
80     AtomicOr,
81     AtomicSub,
82     AtomicXor,
83     NAtomicModifyOps
84 };
85 
86 struct TypedArrayOperations {
87     typedef ReturnedValue (*Read)(const char *data);
88     typedef void (*Write)(char *data, Value value);
89     typedef ReturnedValue (*AtomicModify)(char *data, Value value);
90     typedef ReturnedValue (*AtomicCompareExchange)(char *data, Value expected, Value v);
91     typedef ReturnedValue (*AtomicLoad)(char *data);
92     typedef ReturnedValue (*AtomicStore)(char *data, Value value);
93 
94     template<typename T>
95     static constexpr TypedArrayOperations create(const char *name);
96     template<typename T>
97     static constexpr TypedArrayOperations createWithAtomics(const char *name);
98 
99     int bytesPerElement;
100     const char *name;
101     Read read;
102     Write write;
103     AtomicModify atomicModifyOps[AtomicModifyOps::NAtomicModifyOps];
104     AtomicCompareExchange atomicCompareExchange;
105     AtomicLoad atomicLoad;
106     AtomicStore atomicStore;
107 };
108 
109 namespace Heap {
110 
111 #define TypedArrayMembers(class, Member) \
112     Member(class, Pointer, ArrayBuffer *, buffer) \
113     Member(class, NoMark, const TypedArrayOperations *, type) \
114     Member(class, NoMark, uint, byteLength) \
115     Member(class, NoMark, uint, byteOffset) \
116     Member(class, NoMark, uint, arrayType)
117 
DECLARE_HEAP_OBJECT(TypedArray,Object)118 DECLARE_HEAP_OBJECT(TypedArray, Object) {
119     DECLARE_MARKOBJECTS(TypedArray);
120     using Type = TypedArrayType;
121 
122     void init(Type t);
123 };
124 
125 struct IntrinsicTypedArrayCtor : FunctionObject {
126 };
127 
128 struct TypedArrayCtor : FunctionObject {
129     void init(QV4::ExecutionContext *scope, TypedArray::Type t);
130 
131     TypedArray::Type type;
132 };
133 
134 struct IntrinsicTypedArrayPrototype : Object {
135 };
136 
137 struct TypedArrayPrototype : Object {
138     inline void init(TypedArray::Type t);
139     TypedArray::Type type;
140 };
141 
142 
143 }
144 
145 struct Q_QML_PRIVATE_EXPORT TypedArray : Object
146 {
147     V4_OBJECT2(TypedArray, Object)
148 
149     static Heap::TypedArray *create(QV4::ExecutionEngine *e, Heap::TypedArray::Type t);
150 
byteLengthTypedArray151     uint byteLength() const {
152         return d()->byteLength;
153     }
154 
lengthTypedArray155     uint length() const {
156         return d()->byteLength/d()->type->bytesPerElement;
157     }
158 
arrayDataTypedArray159     QTypedArrayData<char> *arrayData() {
160         return d()->buffer->data;
161     }
162 
arrayTypeTypedArray163     Heap::TypedArray::Type arrayType() const {
164         return static_cast<Heap::TypedArray::Type>(d()->arrayType);
165     }
166     using Object::get;
167 
168     static ReturnedValue virtualGet(const Managed *m, PropertyKey id, const Value *receiver, bool *hasProperty);
169     static bool virtualHasProperty(const Managed *m, PropertyKey id);
170     static PropertyAttributes virtualGetOwnProperty(const Managed *m, PropertyKey id, Property *p);
171     static bool virtualPut(Managed *m, PropertyKey id, const Value &value, Value *receiver);
172     static bool virtualDefineOwnProperty(Managed *m, PropertyKey id, const Property *p, PropertyAttributes attrs);
173     static OwnPropertyKeyIterator *virtualOwnPropertyKeys(const Object *m, Value *target);
174 
175 };
176 
177 struct IntrinsicTypedArrayCtor: FunctionObject
178 {
179     V4_OBJECT2(IntrinsicTypedArrayCtor, FunctionObject)
180 
181     static constexpr VTable::Call virtualCall = nullptr;
182 
183     static ReturnedValue method_of(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
184     static ReturnedValue method_from(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
185 };
186 
187 struct TypedArrayCtor: FunctionObject
188 {
189     V4_OBJECT2(TypedArrayCtor, FunctionObject)
190 
191     static ReturnedValue virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *);
192     static ReturnedValue virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc);
193 };
194 
195 struct IntrinsicTypedArrayPrototype : Object
196 {
197     V4_OBJECT2(IntrinsicTypedArrayPrototype, Object)
198     V4_PROTOTYPE(objectPrototype)
199 
200     void init(ExecutionEngine *engine, IntrinsicTypedArrayCtor *ctor);
201 
202     static ReturnedValue method_get_buffer(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
203     static ReturnedValue method_get_byteLength(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
204     static ReturnedValue method_get_byteOffset(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
205     static ReturnedValue method_get_length(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
206 
207     static ReturnedValue method_copyWithin(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
208     static ReturnedValue method_entries(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
209     static ReturnedValue method_every(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
210     static ReturnedValue method_fill(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
211     static ReturnedValue method_filter(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
212     static ReturnedValue method_find(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
213     static ReturnedValue method_findIndex(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
214     static ReturnedValue method_forEach(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
215     static ReturnedValue method_includes(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
216     static ReturnedValue method_indexOf(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
217     static ReturnedValue method_join(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
218     static ReturnedValue method_keys(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
219     static ReturnedValue method_lastIndexOf(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
220     static ReturnedValue method_map(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
221     static ReturnedValue method_reduce(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
222     static ReturnedValue method_reduceRight(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
223     static ReturnedValue method_reverse(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
224     static ReturnedValue method_some(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
225     static ReturnedValue method_values(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
226     static ReturnedValue method_set(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
227     static ReturnedValue method_slice(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
228     static ReturnedValue method_subarray(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
229     static ReturnedValue method_toLocaleString(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
230 
231     static ReturnedValue method_get_toStringTag(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
232 
233 };
234 
235 struct TypedArrayPrototype : Object
236 {
237     V4_OBJECT2(TypedArrayPrototype, Object)
238     V4_PROTOTYPE(objectPrototype)
239 
240     void init(ExecutionEngine *engine, TypedArrayCtor *ctor);
241 };
242 
243 inline void
init(TypedArray::Type t)244 Heap::TypedArrayPrototype::init(TypedArray::Type t)
245 {
246     Object::init();
247     type = t;
248 }
249 
250 } // namespace QV4
251 
252 QT_END_NAMESPACE
253 
254 #endif
255