| 1 | // Copyright (C) 2016 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 QV4TYPEDARRAY_H |
| 4 | #define QV4TYPEDARRAY_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 "qv4arraybuffer_p.h" |
| 20 | |
| 21 | QT_BEGIN_NAMESPACE |
| 22 | |
| 23 | namespace QV4 { |
| 24 | |
| 25 | struct ArrayBuffer; |
| 26 | |
| 27 | enum TypedArrayType { |
| 28 | Int8Array, |
| 29 | UInt8Array, |
| 30 | Int16Array, |
| 31 | UInt16Array, |
| 32 | Int32Array, |
| 33 | UInt32Array, |
| 34 | UInt8ClampedArray, |
| 35 | Float32Array, |
| 36 | Float64Array, |
| 37 | NTypedArrayTypes |
| 38 | }; |
| 39 | |
| 40 | enum AtomicModifyOps { |
| 41 | AtomicAdd, |
| 42 | AtomicAnd, |
| 43 | AtomicExchange, |
| 44 | AtomicOr, |
| 45 | AtomicSub, |
| 46 | AtomicXor, |
| 47 | NAtomicModifyOps |
| 48 | }; |
| 49 | |
| 50 | struct TypedArrayOperations { |
| 51 | typedef ReturnedValue (*Read)(const char *data); |
| 52 | typedef void (*Write)(char *data, Value value); |
| 53 | typedef ReturnedValue (*AtomicModify)(char *data, Value value); |
| 54 | typedef ReturnedValue (*AtomicCompareExchange)(char *data, Value expected, Value v); |
| 55 | typedef ReturnedValue (*AtomicLoad)(char *data); |
| 56 | typedef ReturnedValue (*AtomicStore)(char *data, Value value); |
| 57 | |
| 58 | template<typename T> |
| 59 | static constexpr TypedArrayOperations create(const char *name); |
| 60 | template<typename T> |
| 61 | static constexpr TypedArrayOperations createWithAtomics(const char *name); |
| 62 | |
| 63 | int bytesPerElement; |
| 64 | const char *name; |
| 65 | Read read; |
| 66 | Write write; |
| 67 | AtomicModify atomicModifyOps[AtomicModifyOps::NAtomicModifyOps]; |
| 68 | AtomicCompareExchange atomicCompareExchange; |
| 69 | AtomicLoad atomicLoad; |
| 70 | AtomicStore atomicStore; |
| 71 | }; |
| 72 | |
| 73 | namespace Heap { |
| 74 | |
| 75 | #define TypedArrayMembers(class, Member) \ |
| 76 | Member(class, Pointer, ArrayBuffer *, buffer) \ |
| 77 | Member(class, NoMark, const TypedArrayOperations *, type) \ |
| 78 | Member(class, NoMark, uint, byteLength) \ |
| 79 | Member(class, NoMark, uint, byteOffset) \ |
| 80 | Member(class, NoMark, uint, arrayType) |
| 81 | |
| 82 | DECLARE_HEAP_OBJECT(TypedArray, Object) { |
| 83 | DECLARE_MARKOBJECTS(TypedArray) |
| 84 | using Type = TypedArrayType; |
| 85 | |
| 86 | void init(Type t); |
| 87 | }; |
| 88 | |
| 89 | struct IntrinsicTypedArrayCtor : FunctionObject { |
| 90 | }; |
| 91 | |
| 92 | struct TypedArrayCtor : FunctionObject { |
| 93 | void init(ExecutionEngine *engine, TypedArray::Type t); |
| 94 | |
| 95 | TypedArray::Type type; |
| 96 | }; |
| 97 | |
| 98 | struct IntrinsicTypedArrayPrototype : Object { |
| 99 | }; |
| 100 | |
| 101 | struct TypedArrayPrototype : Object { |
| 102 | inline void init(TypedArray::Type t); |
| 103 | TypedArray::Type type; |
| 104 | }; |
| 105 | |
| 106 | |
| 107 | } |
| 108 | |
| 109 | struct Q_QML_EXPORT TypedArray : Object |
| 110 | { |
| 111 | V4_OBJECT2(TypedArray, Object) |
| 112 | |
| 113 | static Heap::TypedArray *create(QV4::ExecutionEngine *e, Heap::TypedArray::Type t); |
| 114 | |
| 115 | uint byteOffset() const noexcept { return d()->byteOffset; } |
| 116 | uint byteLength() const noexcept { return d()->byteLength; } |
| 117 | int bytesPerElement() const noexcept { return d()->type->bytesPerElement; } |
| 118 | uint length() const noexcept { return d()->byteLength / d()->type->bytesPerElement; } |
| 119 | |
| 120 | char *arrayData() noexcept { return d()->buffer->arrayData(); } |
| 121 | const char *constArrayData() const noexcept { return d()->buffer->constArrayData(); } |
| 122 | bool hasDetachedArrayData() const noexcept { return d()->buffer->hasDetachedArrayData(); } |
| 123 | uint arrayDataLength() const noexcept { return d()->buffer->arrayDataLength(); } |
| 124 | |
| 125 | Heap::TypedArray::Type arrayType() const noexcept |
| 126 | { |
| 127 | return static_cast<Heap::TypedArray::Type>(d()->arrayType); |
| 128 | } |
| 129 | using Object::get; |
| 130 | |
| 131 | static ReturnedValue virtualGet(const Managed *m, PropertyKey id, const Value *receiver, bool *hasProperty); |
| 132 | static bool virtualHasProperty(const Managed *m, PropertyKey id); |
| 133 | static PropertyAttributes virtualGetOwnProperty(const Managed *m, PropertyKey id, Property *p); |
| 134 | static bool virtualPut(Managed *m, PropertyKey id, const Value &value, Value *receiver); |
| 135 | static bool virtualDefineOwnProperty(Managed *m, PropertyKey id, const Property *p, PropertyAttributes attrs); |
| 136 | static OwnPropertyKeyIterator *virtualOwnPropertyKeys(const Object *m, Value *target); |
| 137 | |
| 138 | }; |
| 139 | |
| 140 | struct IntrinsicTypedArrayCtor: FunctionObject |
| 141 | { |
| 142 | V4_OBJECT2(IntrinsicTypedArrayCtor, FunctionObject) |
| 143 | |
| 144 | static ReturnedValue method_of(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
| 145 | static ReturnedValue method_from(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
| 146 | }; |
| 147 | |
| 148 | struct TypedArrayCtor: FunctionObject |
| 149 | { |
| 150 | V4_OBJECT2(TypedArrayCtor, FunctionObject) |
| 151 | |
| 152 | static ReturnedValue virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *); |
| 153 | static ReturnedValue virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc); |
| 154 | }; |
| 155 | |
| 156 | struct IntrinsicTypedArrayPrototype : Object |
| 157 | { |
| 158 | V4_OBJECT2(IntrinsicTypedArrayPrototype, Object) |
| 159 | V4_PROTOTYPE(objectPrototype) |
| 160 | |
| 161 | void init(ExecutionEngine *engine, IntrinsicTypedArrayCtor *ctor); |
| 162 | |
| 163 | static ReturnedValue method_get_buffer(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
| 164 | static ReturnedValue method_get_byteLength(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
| 165 | static ReturnedValue method_get_byteOffset(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
| 166 | static ReturnedValue method_get_length(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
| 167 | |
| 168 | static ReturnedValue method_copyWithin(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
| 169 | static ReturnedValue method_entries(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
| 170 | static ReturnedValue method_every(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
| 171 | static ReturnedValue method_fill(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
| 172 | static ReturnedValue method_filter(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
| 173 | static ReturnedValue method_find(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
| 174 | static ReturnedValue method_findIndex(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
| 175 | static ReturnedValue method_forEach(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
| 176 | static ReturnedValue method_includes(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
| 177 | static ReturnedValue method_indexOf(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
| 178 | static ReturnedValue method_join(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
| 179 | static ReturnedValue method_keys(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
| 180 | static ReturnedValue method_lastIndexOf(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
| 181 | static ReturnedValue method_map(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
| 182 | static ReturnedValue method_reduce(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
| 183 | static ReturnedValue method_reduceRight(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
| 184 | static ReturnedValue method_reverse(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
| 185 | static ReturnedValue method_some(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
| 186 | static ReturnedValue method_values(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
| 187 | static ReturnedValue method_set(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
| 188 | static ReturnedValue method_slice(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
| 189 | static ReturnedValue method_subarray(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
| 190 | static ReturnedValue method_toLocaleString(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
| 191 | |
| 192 | static ReturnedValue method_get_toStringTag(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
| 193 | |
| 194 | }; |
| 195 | |
| 196 | struct TypedArrayPrototype : Object |
| 197 | { |
| 198 | V4_OBJECT2(TypedArrayPrototype, Object) |
| 199 | V4_PROTOTYPE(objectPrototype) |
| 200 | |
| 201 | void init(ExecutionEngine *engine, TypedArrayCtor *ctor); |
| 202 | }; |
| 203 | |
| 204 | inline void |
| 205 | Heap::TypedArrayPrototype::init(TypedArray::Type t) |
| 206 | { |
| 207 | Object::init(); |
| 208 | type = t; |
| 209 | } |
| 210 | |
| 211 | } // namespace QV4 |
| 212 | |
| 213 | QT_END_NAMESPACE |
| 214 | |
| 215 | #endif |
| 216 | |