Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/qtdeclarative/src/qtdeclarative/src/qml/jsruntime/qv4arraybuffer.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 | #include "qv4arraybuffer_p.h" | - | ||||||||||||
40 | #include "qv4typedarray_p.h" | - | ||||||||||||
41 | #include "qv4dataview_p.h" | - | ||||||||||||
42 | #include "qv4string_p.h" | - | ||||||||||||
43 | #include "qv4jscall_p.h" | - | ||||||||||||
44 | #include "qv4symbol_p.h" | - | ||||||||||||
45 | - | |||||||||||||
46 | using namespace QV4; | - | ||||||||||||
47 | - | |||||||||||||
48 | DEFINE_OBJECT_VTABLE(ArrayBufferCtor); | - | ||||||||||||
49 | DEFINE_OBJECT_VTABLE(ArrayBuffer); | - | ||||||||||||
50 | - | |||||||||||||
51 | void Heap::ArrayBufferCtor::init(QV4::ExecutionContext *scope) | - | ||||||||||||
52 | { | - | ||||||||||||
53 | Heap::FunctionObject::init(scope, QStringLiteral("ArrayBuffer")); executed 98116 times by 153 tests: return qstring_literal_temp; Executed by:
| 98116 | ||||||||||||
54 | } executed 98418 times by 153 tests: end of block Executed by:
| 98418 | ||||||||||||
55 | - | |||||||||||||
56 | ReturnedValue ArrayBufferCtor::virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *) | - | ||||||||||||
57 | { | - | ||||||||||||
58 | ExecutionEngine *v4 = f->engine(); | - | ||||||||||||
59 | Scope scope(v4); | - | ||||||||||||
60 | - | |||||||||||||
61 | ScopedValue l(scope, argc ? argv[0] : Primitive::undefinedValue()); | - | ||||||||||||
62 | double dl = l->toInteger(); | - | ||||||||||||
63 | if (v4->hasException)
| 8-2308 | ||||||||||||
64 | return Encode::undefined(); executed 8 times by 1 test: return Encode::undefined(); Executed by:
| 8 | ||||||||||||
65 | uint len = (uint)qBound(0., dl, (double)UINT_MAX); | - | ||||||||||||
66 | if (len != dl)
| 28-2290 | ||||||||||||
67 | return v4->throwRangeError(QLatin1String("ArrayBuffer constructor: invalid length")); executed 28 times by 1 test: return v4->throwRangeError(QLatin1String("ArrayBuffer constructor: invalid length")); Executed by:
| 28 | ||||||||||||
68 | - | |||||||||||||
69 | Scoped<ArrayBuffer> a(scope, v4->newArrayBuffer(len)); | - | ||||||||||||
70 | if (scope.engine->hasException)
| 0-2287 | ||||||||||||
71 | return Encode::undefined(); never executed: return Encode::undefined(); | 0 | ||||||||||||
72 | - | |||||||||||||
73 | return a->asReturnedValue(); executed 2280 times by 1 test: return a->asReturnedValue(); Executed by:
| 2280 | ||||||||||||
74 | } | - | ||||||||||||
75 | - | |||||||||||||
76 | - | |||||||||||||
77 | ReturnedValue ArrayBufferCtor::virtualCall(const FunctionObject *f, const Value *, const Value *argv, int argc) | - | ||||||||||||
78 | { | - | ||||||||||||
79 | return virtualCallAsConstructor(f, argv, argc, f); executed 4 times by 1 test: return virtualCallAsConstructor(f, argv, argc, f); Executed by:
| 4 | ||||||||||||
80 | } | - | ||||||||||||
81 | - | |||||||||||||
82 | ReturnedValue ArrayBufferCtor::method_isView(const FunctionObject *, const Value *, const Value *argv, int argc) | - | ||||||||||||
83 | { | - | ||||||||||||
84 | if (argc < 1)
| 8-232 | ||||||||||||
85 | return Encode(false); executed 8 times by 1 test: return Encode(false); Executed by:
| 8 | ||||||||||||
86 | - | |||||||||||||
87 | if (argv[0].as<TypedArray>() ||
| 108-124 | ||||||||||||
88 | argv[0].as<DataView>())
| 12-112 | ||||||||||||
89 | return Encode(true); executed 120 times by 1 test: return Encode(true); Executed by:
| 120 | ||||||||||||
90 | - | |||||||||||||
91 | return Encode(false); executed 112 times by 1 test: return Encode(false); Executed by:
| 112 | ||||||||||||
92 | } | - | ||||||||||||
93 | - | |||||||||||||
94 | - | |||||||||||||
95 | void Heap::ArrayBuffer::init(size_t length) | - | ||||||||||||
96 | { | - | ||||||||||||
97 | Object::init(); | - | ||||||||||||
98 | if (length < UINT_MAX)
| 36-21446 | ||||||||||||
99 | data = QTypedArrayData<char>::allocate(length + 1); executed 21432 times by 2 tests: data = QTypedArrayData<char>::allocate(length + 1); Executed by:
| 21432 | ||||||||||||
100 | if (!data) {
| 36-21444 | ||||||||||||
101 | internalClass->engine->throwRangeError(QStringLiteral("ArrayBuffer: out of memory")); executed 36 times by 1 test: return qstring_literal_temp; Executed by:
| 36 | ||||||||||||
102 | return; executed 36 times by 1 test: return; Executed by:
| 36 | ||||||||||||
103 | } | - | ||||||||||||
104 | data->size = int(length); | - | ||||||||||||
105 | memset(data->data(), 0, length + 1); | - | ||||||||||||
106 | } executed 21443 times by 2 tests: end of block Executed by:
| 21443 | ||||||||||||
107 | - | |||||||||||||
108 | void Heap::ArrayBuffer::init(const QByteArray& array) | - | ||||||||||||
109 | { | - | ||||||||||||
110 | Object::init(); | - | ||||||||||||
111 | data = const_cast<QByteArray&>(array).data_ptr(); | - | ||||||||||||
112 | data->ref.ref(); | - | ||||||||||||
113 | } executed 6 times by 3 tests: end of block Executed by:
| 6 | ||||||||||||
114 | - | |||||||||||||
115 | void Heap::ArrayBuffer::destroy() | - | ||||||||||||
116 | { | - | ||||||||||||
117 | if (data && !data->ref.deref())
| 2-21489 | ||||||||||||
118 | QTypedArrayData<char>::deallocate(data); executed 21487 times by 3 tests: QTypedArrayData<char>::deallocate(data); Executed by:
| 21487 | ||||||||||||
119 | Object::destroy(); | - | ||||||||||||
120 | } executed 21526 times by 4 tests: end of block Executed by:
| 21526 | ||||||||||||
121 | - | |||||||||||||
122 | QByteArray ArrayBuffer::asByteArray() const | - | ||||||||||||
123 | { | - | ||||||||||||
124 | QByteArrayDataPtr ba = { d()->data }; | - | ||||||||||||
125 | ba.ptr->ref.ref(); | - | ||||||||||||
126 | return QByteArray(ba); executed 6 times by 3 tests: return QByteArray(ba); Executed by:
| 6 | ||||||||||||
127 | } | - | ||||||||||||
128 | - | |||||||||||||
129 | void ArrayBuffer::detach() { | - | ||||||||||||
130 | if (!d()->data->ref.isShared())
| 0 | ||||||||||||
131 | return; never executed: return; | 0 | ||||||||||||
132 | - | |||||||||||||
133 | QTypedArrayData<char> *oldData = d()->data; | - | ||||||||||||
134 | - | |||||||||||||
135 | d()->data = QTypedArrayData<char>::allocate(oldData->size + 1); | - | ||||||||||||
136 | if (!d()->data) {
| 0 | ||||||||||||
137 | engine()->throwRangeError(QStringLiteral("ArrayBuffer: out of memory")); never executed: return qstring_literal_temp; | 0 | ||||||||||||
138 | return; never executed: return; | 0 | ||||||||||||
139 | } | - | ||||||||||||
140 | - | |||||||||||||
141 | memcpy(d()->data->data(), oldData->data(), oldData->size + 1); | - | ||||||||||||
142 | - | |||||||||||||
143 | if (!oldData->ref.deref())
| 0 | ||||||||||||
144 | QTypedArrayData<char>::deallocate(oldData); never executed: QTypedArrayData<char>::deallocate(oldData); | 0 | ||||||||||||
145 | } never executed: end of block | 0 | ||||||||||||
146 | - | |||||||||||||
147 | - | |||||||||||||
148 | void ArrayBufferPrototype::init(ExecutionEngine *engine, Object *ctor) | - | ||||||||||||
149 | { | - | ||||||||||||
150 | Scope scope(engine); | - | ||||||||||||
151 | ScopedObject o(scope); | - | ||||||||||||
152 | ctor->defineReadonlyConfigurableProperty(engine->id_length(), Primitive::fromInt32(1)); | - | ||||||||||||
153 | ctor->defineReadonlyProperty(engine->id_prototype(), (o = this)); | - | ||||||||||||
154 | ctor->defineDefaultProperty(QStringLiteral("isView"), ArrayBufferCtor::method_isView, 1); executed 98432 times by 153 tests: return qstring_literal_temp; Executed by:
| 98432 | ||||||||||||
155 | ctor->addSymbolSpecies(); | - | ||||||||||||
156 | - | |||||||||||||
157 | defineDefaultProperty(engine->id_constructor(), (o = ctor)); | - | ||||||||||||
158 | defineAccessorProperty(QStringLiteral("byteLength"), method_get_byteLength, nullptr); executed 99005 times by 153 tests: return qstring_literal_temp; Executed by:
| 99005 | ||||||||||||
159 | defineDefaultProperty(QStringLiteral("slice"), method_slice, 2); executed 98764 times by 153 tests: return qstring_literal_temp; Executed by:
| 98764 | ||||||||||||
160 | defineDefaultProperty(QStringLiteral("toString"), method_toString, 0); executed 98980 times by 153 tests: return qstring_literal_temp; Executed by:
| 98980 | ||||||||||||
161 | ScopedString name(scope, engine->newString(QStringLiteral("ArrayBuffer"))); executed 98785 times by 153 tests: return qstring_literal_temp; Executed by:
| 98785 | ||||||||||||
162 | defineReadonlyConfigurableProperty(scope.engine->symbol_toStringTag(), name); | - | ||||||||||||
163 | } executed 99039 times by 153 tests: end of block Executed by:
| 99039 | ||||||||||||
164 | - | |||||||||||||
165 | ReturnedValue ArrayBufferPrototype::method_get_byteLength(const FunctionObject *b, const Value *thisObject, const Value *, int) | - | ||||||||||||
166 | { | - | ||||||||||||
167 | const ArrayBuffer *a = thisObject->as<ArrayBuffer>(); | - | ||||||||||||
168 | if (!a)
| 52-86 | ||||||||||||
169 | return b->engine()->throwTypeError(); executed 52 times by 1 test: return b->engine()->throwTypeError(); Executed by:
| 52 | ||||||||||||
170 | - | |||||||||||||
171 | return Encode(a->d()->data->size); executed 86 times by 2 tests: return Encode(a->d()->data->size); Executed by:
| 86 | ||||||||||||
172 | } | - | ||||||||||||
173 | - | |||||||||||||
174 | ReturnedValue ArrayBufferPrototype::method_slice(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc) | - | ||||||||||||
175 | { | - | ||||||||||||
176 | ExecutionEngine *v4 = b->engine(); | - | ||||||||||||
177 | const ArrayBuffer *a = thisObject->as<ArrayBuffer>(); | - | ||||||||||||
178 | if (!a)
| 32-121 | ||||||||||||
179 | return v4->throwTypeError(); executed 32 times by 1 test: return v4->throwTypeError(); Executed by:
| 32 | ||||||||||||
180 | - | |||||||||||||
181 | double start = argc > 0 ? argv[0].toInteger() : 0;
| 45-77 | ||||||||||||
182 | double end = (argc < 2 || argv[1].isUndefined()) ?
| 4-79 | ||||||||||||
183 | a->d()->data->size : argv[1].toInteger(); | - | ||||||||||||
184 | if (v4->hasException)
| 0-124 | ||||||||||||
185 | return QV4::Encode::undefined(); never executed: return QV4::Encode::undefined(); | 0 | ||||||||||||
186 | - | |||||||||||||
187 | double first = (start < 0) ? qMax(a->d()->data->size + start, 0.) : qMin(start, (double)a->d()->data->size);
| 4-121 | ||||||||||||
188 | double final = (end < 0) ? qMax(a->d()->data->size + end, 0.) : qMin(end, (double)a->d()->data->size);
| 4-123 | ||||||||||||
189 | - | |||||||||||||
190 | Scope scope(v4); | - | ||||||||||||
191 | ScopedFunctionObject constructor(scope, a->get(scope.engine->id_constructor())); | - | ||||||||||||
192 | if (!constructor)
| 52-74 | ||||||||||||
193 | return v4->throwTypeError(); executed 73 times by 1 test: return v4->throwTypeError(); Executed by:
| 73 | ||||||||||||
194 | - | |||||||||||||
195 | double newLen = qMax(final - first, 0.); | - | ||||||||||||
196 | ScopedValue argument(scope, QV4::Encode(newLen)); | - | ||||||||||||
197 | QV4::Scoped<ArrayBuffer> newBuffer(scope, constructor->callAsConstructor(argument, 1)); | - | ||||||||||||
198 | if (!newBuffer || newBuffer->d()->data->size < (int)newLen)
| 0-51 | ||||||||||||
199 | return v4->throwTypeError(); never executed: return v4->throwTypeError(); | 0 | ||||||||||||
200 | - | |||||||||||||
201 | memcpy(newBuffer->d()->data->data(), a->d()->data->data() + (uint)first, newLen); | - | ||||||||||||
202 | return Encode::undefined(); executed 52 times by 1 test: return Encode::undefined(); Executed by:
| 52 | ||||||||||||
203 | } | - | ||||||||||||
204 | - | |||||||||||||
205 | ReturnedValue ArrayBufferPrototype::method_toString(const FunctionObject *b, const Value *thisObject, const Value *, int) | - | ||||||||||||
206 | { | - | ||||||||||||
207 | ExecutionEngine *v4 = b->engine(); | - | ||||||||||||
208 | const ArrayBuffer *a = thisObject->as<ArrayBuffer>(); | - | ||||||||||||
209 | if (!a)
| 2-12 | ||||||||||||
210 | RETURN_UNDEFINED(); executed 12 times by 1 test: return QV4::Encode::undefined(); Executed by:
| 12 | ||||||||||||
211 | return Encode(v4->newString(QString::fromUtf8(a->asByteArray()))); executed 2 times by 1 test: return Encode(v4->newString(QString::fromUtf8(a->asByteArray()))); Executed by:
| 2 | ||||||||||||
212 | } | - | ||||||||||||
Source code | Switch to Preprocessed file |