diff options
-rw-r--r-- | src/qml/jsruntime/jsruntime.pri | 10 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4arraybuffer.cpp | 166 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4arraybuffer_p.h | 93 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4dataview.cpp | 316 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4dataview_p.h | 96 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4engine.cpp | 39 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4engine_p.h | 11 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4managed_p.h | 10 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4typedarray.cpp | 571 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4typedarray_p.h | 133 | ||||
-rw-r--r-- | src/quick/items/qquickitem.cpp | 39 | ||||
-rw-r--r-- | src/quick/items/qquickitem_p.h | 8 | ||||
-rw-r--r-- | src/quick/items/qquickwindow.cpp | 139 | ||||
-rw-r--r-- | tests/auto/qml/qjsengine/tst_qjsengine.cpp | 12 | ||||
-rw-r--r-- | tests/manual/v4/typedarray.js | 13 | ||||
-rw-r--r-- | tests/manual/v4/typedarrays.js | 738 |
16 files changed, 2341 insertions, 53 deletions
diff --git a/src/qml/jsruntime/jsruntime.pri b/src/qml/jsruntime/jsruntime.pri index c27aaa90d8..ef44ca6f4d 100644 --- a/src/qml/jsruntime/jsruntime.pri +++ b/src/qml/jsruntime/jsruntime.pri @@ -42,7 +42,10 @@ SOURCES += \ $$PWD/qv4qobjectwrapper.cpp \ $$PWD/qv4qmlextensions.cpp \ $$PWD/qv4vme_moth.cpp \ - $$PWD/qv4profiling.cpp + $$PWD/qv4profiling.cpp \ + $$PWD/qv4arraybuffer.cpp \ + $$PWD/qv4typedarray.cpp \ + $$PWD/qv4dataview.cpp HEADERS += \ $$PWD/qv4global_p.h \ @@ -89,7 +92,10 @@ HEADERS += \ $$PWD/qv4qobjectwrapper_p.h \ $$PWD/qv4qmlextensions_p.h \ $$PWD/qv4vme_moth_p.h \ - $$PWD/qv4profiling_p.h + $$PWD/qv4profiling_p.h \ + $$PWD/qv4arraybuffer_p.h \ + $$PWD/qv4typedarray_p.h \ + $$PWD/qv4dataview_p.h } diff --git a/src/qml/jsruntime/qv4arraybuffer.cpp b/src/qml/jsruntime/qv4arraybuffer.cpp new file mode 100644 index 0000000000..c97b16a255 --- /dev/null +++ b/src/qml/jsruntime/qv4arraybuffer.cpp @@ -0,0 +1,166 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtQml module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL21$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "qv4arraybuffer_p.h" +#include "qv4typedarray_p.h" +#include "qv4dataview_p.h" + +using namespace QV4; + +DEFINE_OBJECT_VTABLE(ArrayBufferCtor); +DEFINE_OBJECT_VTABLE(ArrayBuffer); + +ArrayBufferCtor::Data::Data(ExecutionContext *scope) + : FunctionObject::Data(scope, QStringLiteral("ArrayBuffer")) +{ + setVTable(staticVTable()); +} + +ReturnedValue ArrayBufferCtor::construct(Managed *m, CallData *callData) +{ + ExecutionEngine *v4 = m->engine(); + + Scope scope(v4); + ScopedValue l(scope, callData->argument(0)); + double dl = l->toInteger(); + if (v4->hasException) + return Encode::undefined(); + uint len = (uint)qBound(0., dl, (double)UINT_MAX); + if (len != dl) + return v4->currentContext()->throwRangeError(QLatin1String("ArrayBuffer constructor: invalid length")); + + Scoped<ArrayBuffer> a(scope, v4->memoryManager->alloc<ArrayBuffer>(v4, len)); + if (scope.engine->hasException) + return Encode::undefined(); + return a.asReturnedValue(); +} + + +ReturnedValue ArrayBufferCtor::call(Managed *that, CallData *callData) +{ + return construct(that, callData); +} + +ReturnedValue ArrayBufferCtor::method_isView(CallContext *ctx) +{ + QV4::Scope scope(ctx); + QV4::Scoped<TypedArray> a(scope, ctx->argument(0)); + if (!!a) + return Encode(true); + QV4::Scoped<DataView> v(scope, ctx->argument(0)); + if (!!v) + return Encode(true); + return Encode(true); +} + + +ArrayBuffer::Data::Data(ExecutionEngine *e, int length) + : Object::Data(e->arrayBufferClass) +{ + data = QTypedArrayData<char>::allocate(length + 1); + if (!data) { + data = 0; + e->currentContext()->throwRangeError(QStringLiteral("ArrayBuffer: out of memory")); + return; + } + data->size = length; + memset(data->data(), 0, length + 1); +} + +QByteArray ArrayBuffer::asByteArray() const +{ + QByteArrayDataPtr ba = { d()->data }; + ba.ptr->ref.ref(); + return QByteArray(ba); +} + +void ArrayBuffer::destroy(Managed *m) +{ + ArrayBuffer *b = static_cast<ArrayBuffer *>(m); + if (!b->d()->data->ref.deref()) + QTypedArrayData<char>::deallocate(b->d()->data); +} + + +void ArrayBufferPrototype::init(ExecutionEngine *engine, Object *ctor) +{ + Scope scope(engine); + ScopedObject o(scope); + ctor->defineReadonlyProperty(engine->id_length, Primitive::fromInt32(1)); + ctor->defineReadonlyProperty(engine->id_prototype, (o = this)); + ctor->defineDefaultProperty(QStringLiteral("isView"), ArrayBufferCtor::method_isView, 1); + defineDefaultProperty(engine->id_constructor, (o = ctor)); + defineAccessorProperty(QStringLiteral("byteLength"), method_get_byteLength, 0); + defineDefaultProperty(QStringLiteral("slice"), method_slice, 2); +} + +ReturnedValue ArrayBufferPrototype::method_get_byteLength(CallContext *ctx) +{ + Scope scope(ctx); + Scoped<ArrayBuffer> v(scope, ctx->d()->callData->thisObject); + if (!v) + return ctx->throwTypeError(); + + return Encode(v->d()->data->size); +} + +ReturnedValue ArrayBufferPrototype::method_slice(CallContext *ctx) +{ + Scope scope(ctx); + Scoped<ArrayBuffer> a(scope, ctx->d()->callData->thisObject); + if (!a) + return ctx->throwTypeError(); + + double start = ctx->d()->callData->argc > 0 ? ctx->d()->callData->args[0].toInteger() : 0; + double end = (ctx->d()->callData->argc < 2 || ctx->d()->callData->args[1].isUndefined()) ? + a->d()->data->size : ctx->d()->callData->args[1].toInteger(); + if (scope.engine->hasException) + return Encode::undefined(); + + double first = (start < 0) ? qMax(a->d()->data->size + start, 0.) : qMin(start, (double)a->d()->data->size); + double final = (end < 0) ? qMax(a->d()->data->size + end, 0.) : qMin(end, (double)a->d()->data->size); + + Scoped<FunctionObject> constructor(scope, a->get(scope.engine->id_constructor)); + if (!constructor) + return ctx->throwTypeError(); + + ScopedCallData callData(scope, 1); + double newLen = qMax(final - first, 0.); + callData->args[0] = QV4::Encode(newLen); + QV4::Scoped<ArrayBuffer> newBuffer(scope, constructor->construct(callData)); + if (!newBuffer || newBuffer->d()->data->size < (int)newLen) + return scope.engine->currentContext()->throwTypeError(); + + memcpy(newBuffer->d()->data->data(), a->d()->data->data() + (uint)first, newLen); + + return newBuffer.asReturnedValue(); +} diff --git a/src/qml/jsruntime/qv4arraybuffer_p.h b/src/qml/jsruntime/qv4arraybuffer_p.h new file mode 100644 index 0000000000..57ee34e570 --- /dev/null +++ b/src/qml/jsruntime/qv4arraybuffer_p.h @@ -0,0 +1,93 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtQml module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL21$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef QV4ARRAYBUFFER_H +#define QV4ARRAYBUFFER_H + +#include "qv4object_p.h" +#include "qv4functionobject_p.h" + +QT_BEGIN_NAMESPACE + +namespace QV4 { + +struct ArrayBufferCtor: FunctionObject +{ + struct Data : FunctionObject::Data { + Data(ExecutionContext *scope); + }; + + V4_OBJECT(FunctionObject) + + static ReturnedValue construct(Managed *m, CallData *callData); + static ReturnedValue call(Managed *that, CallData *callData); + + static ReturnedValue method_isView(CallContext *ctx); + +}; + +struct ArrayBuffer : Object +{ + struct Data : Object::Data { + Data(ExecutionEngine *e, int length); + QTypedArrayData<char> *data; + }; + V4_OBJECT(Object) + + QByteArray asByteArray() const; + uint byteLength() const { return d()->data->size; } + char *data() { + // ### detach if refcount > 1 + return d()->data->data(); + } + const char *constData() { + // ### detach if refcount > 1 + return d()->data->data(); + } + + static void destroy(Managed *m); +}; + +struct ArrayBufferPrototype: Object +{ + void init(ExecutionEngine *engine, Object *ctor); + + static ReturnedValue method_get_byteLength(CallContext *ctx); + static ReturnedValue method_slice(CallContext *ctx); +}; + + +} // namespace QV4 + +QT_END_NAMESPACE + +#endif diff --git a/src/qml/jsruntime/qv4dataview.cpp b/src/qml/jsruntime/qv4dataview.cpp new file mode 100644 index 0000000000..2750b2ceff --- /dev/null +++ b/src/qml/jsruntime/qv4dataview.cpp @@ -0,0 +1,316 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtQml module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL21$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qv4dataview_p.h" +#include "qv4arraybuffer_p.h" + +#include "qendian.h" + +using namespace QV4; + +DEFINE_OBJECT_VTABLE(DataViewCtor); +DEFINE_OBJECT_VTABLE(DataView); + +DataViewCtor::Data::Data(ExecutionContext *scope) + : FunctionObject::Data(scope, QStringLiteral("DataView")) +{ + setVTable(staticVTable()); +} + +ReturnedValue DataViewCtor::construct(Managed *m, CallData *callData) +{ + Scope scope(m->engine()); + Scoped<ArrayBuffer> buffer(scope, callData->argument(0)); + if (!buffer) + return scope.engine->currentContext()->throwTypeError(); + + double bo = callData->argc > 1 ? callData->args[1].toNumber() : 0; + uint byteOffset = (uint)bo; + uint bufferLength = buffer->d()->data->size; + double bl = callData->argc < 3 || callData->args[2].isUndefined() ? (bufferLength - bo) : callData->args[2].toNumber(); + uint byteLength = (uint)bl; + if (bo != byteOffset || bl != byteLength || byteOffset + byteLength > bufferLength) + return scope.engine->currentContext()->throwRangeError(QStringLiteral("DataView: constructor arguments out of range")); + + Scoped<DataView> a(scope, scope.engine->memoryManager->alloc<DataView>(scope.engine)); + a->d()->buffer = buffer; + a->d()->byteLength = byteLength; + a->d()->byteOffset = byteOffset; + return a.asReturnedValue(); + +} + +ReturnedValue DataViewCtor::call(Managed *that, CallData *callData) +{ + return construct(that, callData); +} + + +DataView::Data::Data(ExecutionEngine *e) + : Object::Data(e->dataViewClass), + buffer(0), + byteLength(0), + byteOffset(0) +{ +} + + +void DataView::markObjects(Managed *that, ExecutionEngine *e) +{ + DataView *v = static_cast<DataView *>(that); + v->d()->buffer->mark(e); +} + +void DataViewPrototype::init(ExecutionEngine *engine, Object *ctor) +{ + Scope scope(engine); + ScopedObject o(scope); + ctor->defineReadonlyProperty(engine->id_length, Primitive::fromInt32(3)); + ctor->defineReadonlyProperty(engine->id_prototype, (o = this)); + defineDefaultProperty(engine->id_constructor, (o = ctor)); + defineAccessorProperty(QStringLiteral("buffer"), method_get_buffer, 0); + defineAccessorProperty(QStringLiteral("byteLength"), method_get_byteLength, 0); + defineAccessorProperty(QStringLiteral("byteOffset"), method_get_byteOffset, 0); + + defineDefaultProperty(QStringLiteral("getInt8"), method_getChar<signed char>, 0); + defineDefaultProperty(QStringLiteral("getUInt8"), method_getChar<unsigned char>, 0); + defineDefaultProperty(QStringLiteral("getInt16"), method_get<short>, 0); + defineDefaultProperty(QStringLiteral("getUInt16"), method_get<unsigned short>, 0); + defineDefaultProperty(QStringLiteral("getInt32"), method_get<int>, 0); + defineDefaultProperty(QStringLiteral("getUInt32"), method_get<unsigned int>, 0); + defineDefaultProperty(QStringLiteral("getFloat32"), method_getFloat<float>, 0); + defineDefaultProperty(QStringLiteral("getFloat64"), method_getFloat<double>, 0); + + defineDefaultProperty(QStringLiteral("setInt8"), method_setChar<signed char>, 0); + defineDefaultProperty(QStringLiteral("setUInt8"), method_setChar<unsigned char>, 0); + defineDefaultProperty(QStringLiteral("setInt16"), method_set<short>, 0); + defineDefaultProperty(QStringLiteral("setUInt16"), method_set<unsigned short>, 0); + defineDefaultProperty(QStringLiteral("setInt32"), method_set<int>, 0); + defineDefaultProperty(QStringLiteral("setUInt32"), method_set<unsigned int>, 0); + defineDefaultProperty(QStringLiteral("setFloat32"), method_setFloat<float>, 0); + defineDefaultProperty(QStringLiteral("setFloat64"), method_setFloat<double>, 0); +} + +ReturnedValue DataViewPrototype::method_get_buffer(CallContext *ctx) +{ + Scope scope(ctx); + Scoped<DataView> v(scope, ctx->d()->callData->thisObject); + if (!v) + return ctx->throwTypeError(); + + return Encode(v->d()->buffer->asReturnedValue()); +} + +ReturnedValue DataViewPrototype::method_get_byteLength(CallContext *ctx) +{ + Scope scope(ctx); + Scoped<DataView> v(scope, ctx->d()->callData->thisObject); + if (!v) + return ctx->throwTypeError(); + + return Encode(v->d()->byteLength); +} + +ReturnedValue DataViewPrototype::method_get_byteOffset(CallContext *ctx) +{ + Scope scope(ctx); + Scoped<DataView> v(scope, ctx->d()->callData->thisObject); + if (!v) + return ctx->throwTypeError(); + + return Encode(v->d()->byteOffset); +} + +template <typename T> +ReturnedValue DataViewPrototype::method_getChar(CallContext *ctx) +{ + Scope scope(ctx); + Scoped<DataView> v(scope, ctx->d()->callData->thisObject); + if (!v || ctx->d()->callData->argc < 1) + return ctx->throwTypeError(); + double l = ctx->d()->callData->args[0].toNumber(); + uint idx = (uint)l; + if (l != idx || idx + sizeof(T) > v->d()->byteLength) + return ctx->throwTypeError(); + idx += v->d()->byteOffset; + + T t = T(v->d()->buffer->d()->data->data()[idx]); + + return Encode((int)t); +} + +template <typename T> +ReturnedValue DataViewPrototype::method_get(CallContext *ctx) +{ + Scope scope(ctx); + Scoped<DataView> v(scope, ctx->d()->callData->thisObject); + if (!v || ctx->d()->callData->argc < 1) + return ctx->throwTypeError(); + double l = ctx->d()->callData->args[0].toNumber(); + uint idx = (uint)l; + if (l != idx || idx + sizeof(T) > v->d()->byteLength) + return ctx->throwTypeError(); + idx += v->d()->byteOffset; + + bool littleEndian = ctx->d()->callData->argc < 2 ? false : ctx->d()->callData->args[1].toBoolean(); + + T t = littleEndian + ? qFromLittleEndian<T>((uchar *)v->d()->buffer->d()->data->data() + idx) + : qFromBigEndian<T>((uchar *)v->d()->buffer->d()->data->data() + idx); + + return Encode(t); +} + +template <typename T> +ReturnedValue DataViewPrototype::method_getFloat(CallContext *ctx) +{ + Scope scope(ctx); + Scoped<DataView> v(scope, ctx->d()->callData->thisObject); + if (!v || ctx->d()->callData->argc < 1) + return ctx->throwTypeError(); + double l = ctx->d()->callData->args[0].toNumber(); + uint idx = (uint)l; + if (l != idx || idx + sizeof(T) > v->d()->byteLength) + return ctx->throwTypeError(); + idx += v->d()->byteOffset; + + bool littleEndian = ctx->d()->callData->argc < 2 ? false : ctx->d()->callData->args[1].toBoolean(); + + if (sizeof(T) == 4) { + // float + union { + uint i; + float f; + } u; + u.i = littleEndian + ? qFromLittleEndian<uint>((uchar *)v->d()->buffer->d()->data->data() + idx) + : qFromBigEndian<uint>((uchar *)v->d()->buffer->d()->data->data() + idx); + return Encode(u.f); + } else { + Q_ASSERT(sizeof(T) == 8); + union { + quint64 i; + double d; + } u; + u.i = littleEndian + ? qFromLittleEndian<quint64>((uchar *)v->d()->buffer->d()->data->data() + idx) + : qFromBigEndian<quint64>((uchar *)v->d()->buffer->d()->data->data() + idx); + return Encode(u.d); + } +} + +template <typename T> +ReturnedValue DataViewPrototype::method_setChar(CallContext *ctx) +{ + Scope scope(ctx); + Scoped<DataView> v(scope, ctx->d()->callData->thisObject); + if (!v || ctx->d()->callData->argc < 1) + return ctx->throwTypeError(); + double l = ctx->d()->callData->args[0].toNumber(); + uint idx = (uint)l; + if (l != idx || idx + sizeof(T) > v->d()->byteLength) + return ctx->throwTypeError(); + idx += v->d()->byteOffset; + + int val = ctx->d()->callData->argc >= 2 ? ctx->d()->callData->args[1].toInt32() : 0; + v->d()->buffer->d()->data->data()[idx] = (char)val; + + return Encode::undefined(); +} + +template <typename T> +ReturnedValue DataViewPrototype::method_set(CallContext *ctx) +{ + Scope scope(ctx); + Scoped<DataView> v(scope, ctx->d()->callData->thisObject); + if (!v || ctx->d()->callData->argc < 1) + return ctx->throwTypeError(); + double l = ctx->d()->callData->args[0].toNumber(); + uint idx = (uint)l; + if (l != idx || idx + sizeof(T) > v->d()->byteLength) + return ctx->throwTypeError(); + idx += v->d()->byteOffset; + + int val = ctx->d()->callData->argc >= 2 ? ctx->d()->callData->args[1].toInt32() : 0; + + bool littleEndian = ctx->d()->callData->argc < 3 ? false : ctx->d()->callData->args[2].toBoolean(); + + if (littleEndian) + qToLittleEndian<T>(val, (uchar *)v->d()->buffer->d()->data->data() + idx); + else + qToBigEndian<T>(val, (uchar *)v->d()->buffer->d()->data->data() + idx); + + return Encode::undefined(); +} + +template <typename T> +ReturnedValue DataViewPrototype::method_setFloat(CallContext *ctx) +{ + Scope scope(ctx); + Scoped<DataView> v(scope, ctx->d()->callData->thisObject); + if (!v || ctx->d()->callData->argc < 1) + return ctx->throwTypeError(); + double l = ctx->d()->callData->args[0].toNumber(); + uint idx = (uint)l; + if (l != idx || idx + sizeof(T) > v->d()->byteLength) + return ctx->throwTypeError(); + idx += v->d()->byteOffset; + + double val = ctx->d()->callData->argc >= 2 ? ctx->d()->callData->args[1].toNumber() : qSNaN(); + bool littleEndian = ctx->d()->callData->argc < 3 ? false : ctx->d()->callData->args[2].toBoolean(); + + if (sizeof(T) == 4) { + // float + union { + uint i; + float f; + } u; + u.f = val; + if (littleEndian) + qToLittleEndian(u.i, (uchar *)v->d()->buffer->d()->data->data() + idx); + else + qToBigEndian(u.i, (uchar *)v->d()->buffer->d()->data->data() + idx); + } else { + Q_ASSERT(sizeof(T) == 8); + union { + quint64 i; + double d; + } u; + u.d = val; + if (littleEndian) + qToLittleEndian(u.i, (uchar *)v->d()->buffer->d()->data->data() + idx); + else + qToBigEndian(u.i, (uchar *)v->d()->buffer->d()->data->data() + idx); + } + return Encode::undefined(); +} diff --git a/src/qml/jsruntime/qv4dataview_p.h b/src/qml/jsruntime/qv4dataview_p.h new file mode 100644 index 0000000000..aa8a0201d9 --- /dev/null +++ b/src/qml/jsruntime/qv4dataview_p.h @@ -0,0 +1,96 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtQml module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL21$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef QV4DATAVIEW_H +#define QV4DATAVIEW_H + +#include "qv4object_p.h" +#include "qv4functionobject_p.h" + +QT_BEGIN_NAMESPACE + +namespace QV4 { + +struct ArrayBuffer; + +struct DataViewCtor: FunctionObject +{ + struct Data : FunctionObject::Data { + Data(ExecutionContext *scope); + }; + + V4_OBJECT(FunctionObject) + + static ReturnedValue construct(Managed *m, CallData *callData); + static ReturnedValue call(Managed *that, CallData *callData); +}; + +struct DataView : Object +{ + struct Data : Object::Data { + Data(ExecutionEngine *e); + ArrayBuffer *buffer; + uint byteLength; + uint byteOffset; + }; + V4_OBJECT(Object) + + static void markObjects(Managed *that, ExecutionEngine *e); +}; + +struct DataViewPrototype: Object +{ + void init(ExecutionEngine *engine, Object *ctor); + + static ReturnedValue method_get_buffer(CallContext *ctx); + static ReturnedValue method_get_byteLength(CallContext *ctx); + static ReturnedValue method_get_byteOffset(CallContext *ctx); + template <typename T> + static ReturnedValue method_getChar(CallContext *ctx); + template <typename T> + static ReturnedValue method_get(CallContext *ctx); + template <typename T> + static ReturnedValue method_getFloat(CallContext *ctx); + template <typename T> + static ReturnedValue method_setChar(CallContext *ctx); + template <typename T> + static ReturnedValue method_set(CallContext *ctx); + template <typename T> + static ReturnedValue method_setFloat(CallContext *ctx); +}; + + +} // namespace QV4 + +QT_END_NAMESPACE + +#endif diff --git a/src/qml/jsruntime/qv4engine.cpp b/src/qml/jsruntime/qv4engine.cpp index ea075f9cbd..057f692bf9 100644 --- a/src/qml/jsruntime/qv4engine.cpp +++ b/src/qml/jsruntime/qv4engine.cpp @@ -61,6 +61,9 @@ #include "qv4qobjectwrapper_p.h" #include "qv4qmlextensions_p.h" #include "qv4memberdata_p.h" +#include "qv4arraybuffer_p.h" +#include "qv4dataview_p.h" +#include "qv4typedarray_p.h" #include <QtCore/QTextStream> #include <QDateTime> @@ -252,6 +255,9 @@ ExecutionEngine::ExecutionEngine(EvalISelFactory *factory) id_toString = newIdentifier(QStringLiteral("toString")); id_destroy = newIdentifier(QStringLiteral("destroy")); id_valueOf = newIdentifier(QStringLiteral("valueOf")); + id_byteLength = newIdentifier(QStringLiteral("byteLength")); + id_byteOffset = newIdentifier(QStringLiteral("byteOffset")); + id_buffer = newIdentifier(QStringLiteral("buffer")); memberDataClass = InternalClass::create(this, MemberData::staticVTable(), 0); @@ -363,6 +369,26 @@ ExecutionEngine::ExecutionEngine(EvalISelFactory *factory) static_cast<VariantPrototype *>(variantPrototype.getPointer())->init(); static_cast<SequencePrototype *>(sequencePrototype.managed())->init(); + + // typed arrays + + arrayBufferCtor = memoryManager->alloc<ArrayBufferCtor>(rootContext); + Scoped<ArrayBufferPrototype> arrayBufferPrototype(scope, memoryManager->alloc<ArrayBufferPrototype>(objectClass)); + arrayBufferPrototype->init(this, arrayBufferCtor.asObject()); + arrayBufferClass = InternalClass::create(this, ArrayBuffer::staticVTable(), arrayBufferPrototype); + + dataViewCtor = memoryManager->alloc<DataViewCtor>(rootContext); + Scoped<DataViewPrototype> dataViewPrototype(scope, memoryManager->alloc<DataViewPrototype>(objectClass)); + dataViewPrototype->init(this, dataViewCtor.asObject()); + dataViewClass = InternalClass::create(this, DataView::staticVTable(), dataViewPrototype); + + for (int i = 0; i < TypedArray::NTypes; ++i) { + typedArrayCtors[i] = memoryManager->alloc<TypedArrayCtor>(rootContext, TypedArray::Type(i)); + Scoped<TypedArrayPrototype> typedArrayPrototype(scope, memoryManager->alloc<TypedArrayPrototype>(this, TypedArray::Type(i))); + typedArrayPrototype->init(this, static_cast<TypedArrayCtor *>(typedArrayCtors[i].asObject())); + typedArrayClasses[i] = InternalClass::create(this, TypedArray::staticVTable(), typedArrayPrototype); + } + // // set up the global object // @@ -386,6 +412,12 @@ ExecutionEngine::ExecutionEngine(EvalISelFactory *factory) globalObject->defineDefaultProperty(QStringLiteral("SyntaxError"), syntaxErrorCtor); globalObject->defineDefaultProperty(QStringLiteral("TypeError"), typeErrorCtor); globalObject->defineDefaultProperty(QStringLiteral("URIError"), uRIErrorCtor); + + globalObject->defineDefaultProperty(QStringLiteral("ArrayBuffer"), arrayBufferCtor); + globalObject->defineDefaultProperty(QStringLiteral("DataView"), dataViewCtor); + ScopedString str(scope); + for (int i = 0; i < TypedArray::NTypes; ++i) + globalObject->defineDefaultProperty((str = typedArrayCtors[i].asFunctionObject()->name())->toQString(), typedArrayCtors[i]); ScopedObject o(scope); globalObject->defineDefaultProperty(QStringLiteral("Math"), (o = memoryManager->alloc<MathObject>(QV4::InternalClass::create(this, MathObject::staticVTable(), objectPrototype)))); globalObject->defineDefaultProperty(QStringLiteral("JSON"), (o = memoryManager->alloc<JsonObject>(QV4::InternalClass::create(this, JsonObject::staticVTable(), objectPrototype)))); @@ -887,6 +919,9 @@ void ExecutionEngine::markObjects() id_toString->mark(this); id_destroy->mark(this); id_valueOf->mark(this); + id_byteLength->mark(this); + id_byteOffset->mark(this); + id_buffer->mark(this); objectCtor.mark(this); stringCtor.mark(this); @@ -903,6 +938,10 @@ void ExecutionEngine::markObjects() syntaxErrorCtor.mark(this); typeErrorCtor.mark(this); uRIErrorCtor.mark(this); + arrayBufferCtor.mark(this); + dataViewCtor.mark(this); + for (int i = 0; i < TypedArray::NTypes; ++i) + typedArrayCtors[i].mark(this); sequencePrototype.mark(this); exceptionValue.mark(this); diff --git a/src/qml/jsruntime/qv4engine_p.h b/src/qml/jsruntime/qv4engine_p.h index a4a40c2f41..c77c5b1ce1 100644 --- a/src/qml/jsruntime/qv4engine_p.h +++ b/src/qml/jsruntime/qv4engine_p.h @@ -189,6 +189,10 @@ public: Value typeErrorCtor; Value uRIErrorCtor; Value sequencePrototype; + Value arrayBufferCtor; + Value dataViewCtor; + enum { NTypedArrayTypes = 9 }; // avoid header dependency + Value typedArrayCtors[NTypedArrayTypes]; InternalClassPool *classPool; InternalClass *emptyClass; @@ -224,6 +228,10 @@ public: InternalClass *variantClass; InternalClass *memberDataClass; + InternalClass *arrayBufferClass; + InternalClass *dataViewClass; + InternalClass *typedArrayClasses[NTypedArrayTypes]; // TypedArray::NValues, avoid including the header here + EvalFunction *evalFunction; FunctionObject *thrower; @@ -262,6 +270,9 @@ public: StringValue id_toString; StringValue id_destroy; StringValue id_valueOf; + StringValue id_byteLength; + StringValue id_byteOffset; + StringValue id_buffer; QSet<CompiledData::CompilationUnit*> compilationUnits; diff --git a/src/qml/jsruntime/qv4managed_p.h b/src/qml/jsruntime/qv4managed_p.h index ce0ee973e8..636c89c5e4 100644 --- a/src/qml/jsruntime/qv4managed_p.h +++ b/src/qml/jsruntime/qv4managed_p.h @@ -45,7 +45,7 @@ QT_BEGIN_NAMESPACE namespace QV4 { #define Q_MANAGED_CHECK \ - template <typename T> inline void qt_check_for_QMANAGED_macro(const T *_q_argument) const \ + template <typename _T> inline void qt_check_for_QMANAGED_macro(const _T *_q_argument) const \ { int i = qYouForgotTheQ_MANAGED_Macro(this, _q_argument); i = i + 1; } template <typename T> @@ -66,8 +66,8 @@ inline void qYouForgotTheQ_MANAGED_Macro(T1, T2) {} typedef superClass SuperClass; \ static const QV4::ManagedVTable static_vtbl; \ static inline const QV4::ManagedVTable *staticVTable() { return &static_vtbl; } \ - template <typename T> \ - QV4::Returned<T> *asReturned() { return QV4::Returned<T>::create(this); } \ + template <typename _T> \ + QV4::Returned<_T> *asReturned() { return QV4::Returned<_T>::create(this); } \ V4_MANAGED_SIZE_TEST \ const Data *d() const { return &static_cast<const Data &>(Managed::data); } \ Data *d() { return &static_cast<Data &>(Managed::data); } @@ -78,8 +78,8 @@ inline void qYouForgotTheQ_MANAGED_Macro(T1, T2) {} typedef superClass SuperClass; \ static const QV4::ObjectVTable static_vtbl; \ static inline const QV4::ManagedVTable *staticVTable() { return &static_vtbl.managedVTable; } \ - template <typename T> \ - QV4::Returned<T> *asReturned() { return QV4::Returned<T>::create(this); } \ + template <typename _T> \ + QV4::Returned<_T> *asReturned() { return QV4::Returned<_T>::create(this); } \ V4_MANAGED_SIZE_TEST \ const Data *d() const { return &static_cast<const Data &>(Managed::data); } \ Data *d() { return &static_cast<Data &>(Managed::data); } diff --git a/src/qml/jsruntime/qv4typedarray.cpp b/src/qml/jsruntime/qv4typedarray.cpp new file mode 100644 index 0000000000..08d272862b --- /dev/null +++ b/src/qml/jsruntime/qv4typedarray.cpp @@ -0,0 +1,571 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtQml module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL21$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "qv4typedarray_p.h" +#include "qv4arraybuffer_p.h" + +using namespace QV4; + +DEFINE_OBJECT_VTABLE(TypedArrayCtor); +DEFINE_OBJECT_VTABLE(TypedArrayPrototype); +DEFINE_OBJECT_VTABLE(TypedArray); + +Q_STATIC_ASSERT((int)ExecutionEngine::NTypedArrayTypes == (int)TypedArray::NTypes); + +ReturnedValue Int8ArrayRead(const char *data, int index) +{ + return Encode((int)(signed char)data[index]); +} + +void Int8ArrayWrite(ExecutionEngine *e, char *data, int index, ValueRef value) +{ + signed char v = (signed char)value->toUInt32(); + if (e->hasException) + return; + data[index] = v; +} + +ReturnedValue UInt8ArrayRead(const char *data, int index) +{ + return Encode((int)(unsigned char)data[index]); +} + +void UInt8ArrayWrite(ExecutionEngine *e, char *data, int index, ValueRef value) +{ + unsigned char v = (unsigned char)value->toUInt32(); + if (e->hasException) + return; + data[index] = v; +} + +void UInt8ClampedArrayWrite(ExecutionEngine *e, char *data, int index, ValueRef value) +{ + if (value->isInteger()) { + data[index] = (char)(unsigned char)qBound(0, value->integerValue(), 255); + return; + } + double d = value->toNumber(); + if (e->hasException) + return; + // ### is there a way to optimise this? + if (d <= 0 || std::isnan(d)) { + data[index] = 0; + return; + } + if (d >= 255) { + data[index] = 255; + return; + } + double f = floor(d); + if (f + 0.5 < d) { + data[index] = (unsigned char)(f + 1); + return; + } + if (d < f + 0.5) { + data[index] = (unsigned char)(f); + return; + } + if (int(f) % 2) { + // odd number + data[index] = (unsigned char)(f + 1); + return; + } + data[index] = (unsigned char)(f); +} + +ReturnedValue Int16ArrayRead(const char *data, int index) +{ + return Encode((int)*(short *)(data + index)); +} + +void Int16ArrayWrite(ExecutionEngine *e, char *data, int index, ValueRef value) +{ + short v = (short)value->toInt32(); + if (e->hasException) + return; + *(short *)(data + index) = v; +} + +ReturnedValue UInt16ArrayRead(const char *data, int index) +{ + return Encode((int)*(unsigned short *)(data + index)); +} + +void UInt16ArrayWrite(ExecutionEngine *e, char *data, int index, ValueRef value) +{ + unsigned short v = (unsigned short)value->toInt32(); + if (e->hasException) + return; + *(unsigned short *)(data + index) = v; +} + +ReturnedValue Int32ArrayRead(const char *data, int index) +{ + return Encode(*(int *)(data + index)); +} + +void Int32ArrayWrite(ExecutionEngine *e, char *data, int index, ValueRef value) +{ + int v = (int)value->toInt32(); + if (e->hasException) + return; + *(int *)(data + index) = v; +} + +ReturnedValue UInt32ArrayRead(const char *data, int index) +{ + return Encode(*(unsigned int *)(data + index)); +} + +void UInt32ArrayWrite(ExecutionEngine *e, char *data, int index, ValueRef value) +{ + unsigned int v = (unsigned int)value->toUInt32(); + if (e->hasException) + return; + *(unsigned int *)(data + index) = v; +} + +ReturnedValue Float32ArrayRead(const char *data, int index) +{ + return Encode(*(float *)(data + index)); +} + +void Float32ArrayWrite(ExecutionEngine *e, char *data, int index, ValueRef value) +{ + float v = value->toNumber(); + if (e->hasException) + return; + *(float *)(data + index) = v; +} + +ReturnedValue Float64ArrayRead(const char *data, int index) +{ + return Encode(*(double *)(data + index)); +} + +void Float64ArrayWrite(ExecutionEngine *e, char *data, int index, ValueRef value) +{ + double v = value->toNumber(); + if (e->hasException) + return; + *(double *)(data + index) = v; +} + +const TypedArrayOperations operations[TypedArray::NTypes] = { + { 1, "Int8Array", Int8ArrayRead, Int8ArrayWrite }, + { 1, "Uint8Array", UInt8ArrayRead, UInt8ArrayWrite }, + { 1, "Uint8ClampedArray", UInt8ArrayRead, UInt8ClampedArrayWrite }, + { 2, "Int16Array", Int16ArrayRead, Int16ArrayWrite }, + { 2, "Uint16Array", UInt16ArrayRead, UInt16ArrayWrite }, + { 4, "Int32Array", Int32ArrayRead, Int32ArrayWrite }, + { 4, "Uint32Array", UInt32ArrayRead, UInt32ArrayWrite }, + { 4, "Float32Array", Float32ArrayRead, Float32ArrayWrite }, + { 8, "Float64Array", Float64ArrayRead, Float64ArrayWrite }, +}; + + +TypedArrayCtor::Data::Data(ExecutionContext *scope, TypedArray::Type t) + : FunctionObject::Data(scope, QLatin1String(operations[t].name)) + , type(t) +{ + setVTable(staticVTable()); +} + +ReturnedValue TypedArrayCtor::construct(Managed *m, CallData *callData) +{ + Scope scope(m->engine()); + Scoped<TypedArrayCtor> that(scope, static_cast<TypedArrayCtor *>(m)); + + if (!callData->argc || !callData->args[0].isObject()) { + // ECMA 6 22.2.1.1 + double l = callData->argc ? callData->args[0].toNumber() : 0; + if (scope.engine->hasException) + return Encode::undefined(); + uint len = (uint)l; + if (l != len) + scope.engine->currentContext()->throwRangeError(QStringLiteral("Non integer length for typed array.")); + uint byteLength = len * operations[that->d()->type].bytesPerElement; + Scoped<ArrayBuffer> buffer(scope, scope.engine->memoryManager->alloc<ArrayBuffer>(scope.engine, byteLength)); + if (scope.engine->hasException) + return Encode::undefined(); + + Scoped<TypedArray > array(scope, scope.engine->memoryManager->alloc<TypedArray>(scope.engine, that->d()->type)); + array->d()->buffer = buffer; + array->d()->byteLength = byteLength; + array->d()->byteOffset = 0; + + return array.asReturnedValue(); + } + Scoped<TypedArray> typedArray(scope, callData->argument(0)); + if (!!typedArray) { + // ECMA 6 22.2.1.2 + Scoped<ArrayBuffer> buffer(scope, typedArray->d()->buffer); + uint srcElementSize = typedArray->d()->type->bytesPerElement; + uint destElementSize = operations[that->d()->type].bytesPerElement; + uint byteLength = typedArray->d()->byteLength; + uint destByteLength = byteLength*destElementSize/srcElementSize; + + Scoped<ArrayBuffer> newBuffer(scope, scope.engine->memoryManager->alloc<ArrayBuffer>(scope.engine, destByteLength)); + if (scope.engine->hasException) + return Encode::undefined(); + + Scoped<TypedArray > array(scope, scope.engine->memoryManager->alloc<TypedArray>(scope.engine, that->d()->type)); + array->d()->buffer = newBuffer; + array->d()->byteLength = destByteLength; + array->d()->byteOffset = 0; + + const char *src = buffer->d()->data->data() + typedArray->d()->byteOffset; + char *dest = newBuffer->d()->data->data(); + + // check if src and new type have the same size. In that case we can simply memcpy the data + if (srcElementSize == destElementSize) { + memcpy(dest, src, byteLength); + } else { + // not same size, we need to loop + uint l = typedArray->length(); + TypedArrayRead read = typedArray->d()->type->read; + TypedArrayWrite write =array->d()->type->write; + for (uint i = 0; i < l; ++i) { + Primitive val; + val.val = read(src, i*srcElementSize); + write(scope.engine, dest, i*destElementSize, val); + } + } + + return array.asReturnedValue(); + } + Scoped<ArrayBuffer> buffer(scope, callData->argument(0)); + if (!!buffer) { + // ECMA 6 22.2.1.4 + + double dbyteOffset = callData->argc > 1 ? callData->args[1].toInteger() : 0; + uint byteOffset = (uint)dbyteOffset; + uint elementSize = operations[that->d()->type].bytesPerElement; + if (dbyteOffset < 0 || (byteOffset % elementSize) || dbyteOffset > buffer->byteLength()) + return scope.engine->currentContext()->throwRangeError(QStringLiteral("new TypedArray: invalid byteOffset")); + + uint byteLength; + if (callData->argc < 3 || callData->args[2].isUndefined()) { + byteLength = buffer->byteLength() - byteOffset; + if (buffer->byteLength() < byteOffset || byteLength % elementSize) + return scope.engine->currentContext()->throwRangeError(QStringLiteral("new TypedArray: invalid length")); + } else { + double l = qBound(0., callData->args[2].toInteger(), (double)UINT_MAX); + if (scope.engine->hasException) + return Encode::undefined(); + l *= elementSize; + if (buffer->byteLength() - byteOffset < l) + return scope.engine->currentContext()->throwRangeError(QStringLiteral("new TypedArray: invalid length")); + byteLength = (uint)l; + } + + Scoped<TypedArray > array(scope, scope.engine->memoryManager->alloc<TypedArray>(scope.engine, that->d()->type)); + array->d()->buffer = buffer; + array->d()->byteLength = byteLength; + array->d()->byteOffset = byteOffset; + return array.asReturnedValue(); + } + + // ECMA 6 22.2.1.3 + + ScopedObject o(scope, callData->argument(0)); + uint l = (uint) qBound(0., ScopedValue(scope, o->get(scope.engine->id_length))->toInteger(), (double)UINT_MAX); + if (scope.engine->hasException) + return scope.engine->currentContext()->throwTypeError(); + + uint elementSize = operations[that->d()->type].bytesPerElement; + Scoped<ArrayBuffer> newBuffer(scope, scope.engine->memoryManager->alloc<ArrayBuffer>(scope.engine, l * elementSize)); + if (scope.engine->hasException) + return Encode::undefined(); + + Scoped<TypedArray > array(scope, scope.engine->memoryManager->alloc<TypedArray>(scope.engine, that->d()->type)); + array->d()->buffer = newBuffer; + array->d()->byteLength = l * elementSize; + array->d()->byteOffset = 0; + + uint idx = 0; + char *b = newBuffer->d()->data->data(); + ScopedValue val(scope); + while (idx < l) { + val = o->getIndexed(idx); + array->d()->type->write(scope.engine, b, 0, val); + if (scope.engine->hasException) + return Encode::undefined(); + ++idx; + b += elementSize; + } + + + return array.asReturnedValue(); +} + +ReturnedValue TypedArrayCtor::call(Managed *that, CallData *callData) +{ + return construct(that, callData); +} + +TypedArray::Data::Data(ExecutionEngine *e, Type t) + : Object::Data(e->typedArrayClasses[t]), + type(operations + t) +{ +} + +void TypedArray::markObjects(Managed *that, ExecutionEngine *e) +{ + static_cast<TypedArray *>(that)->d()->buffer->mark(e); +} + +ReturnedValue TypedArray::getIndexed(Managed *m, uint index, bool *hasProperty) +{ + Scope scope(m->engine()); + Scoped<TypedArray> a(scope, static_cast<TypedArray *>(m)); + + uint bytesPerElement = a->d()->type->bytesPerElement; + uint byteOffset = a->d()->byteOffset + index * bytesPerElement; + if (byteOffset + bytesPerElement > (uint)a->d()->buffer->byteLength()) { + if (hasProperty) + *hasProperty = false; + return Encode::undefined(); + } + if (hasProperty) + *hasProperty = true; + return a->d()->type->read(a->d()->buffer->d()->data->data(), byteOffset); +} + +void TypedArray::putIndexed(Managed *m, uint index, const ValueRef value) +{ + if (m->engine()->hasException) + return; + + Scope scope(m->engine()); + Scoped<TypedArray> a(scope, static_cast<TypedArray *>(m)); + + uint bytesPerElement = a->d()->type->bytesPerElement; + uint byteOffset = a->d()->byteOffset + index * bytesPerElement; + if (byteOffset + bytesPerElement > (uint)a->d()->buffer->byteLength()) + goto reject; + + a->d()->type->write(scope.engine, a->d()->buffer->d()->data->data(), byteOffset, value); + return; + +reject: + if (scope.engine->currentContext()->d()->strictMode) + scope.engine->currentContext()->throwTypeError(); +} + +void TypedArrayPrototype::init(ExecutionEngine *engine, TypedArrayCtor *ctor) +{ + Scope scope(engine); + ScopedObject o(scope); + ctor->defineReadonlyProperty(engine->id_length, Primitive::fromInt32(3)); + ctor->defineReadonlyProperty(engine->id_prototype, (o = this)); + ctor->defineReadonlyProperty(QStringLiteral("BYTES_PER_ELEMENT"), Primitive::fromInt32(operations[ctor->d()->type].bytesPerElement)); + defineDefaultProperty(engine->id_constructor, (o = ctor)); + defineAccessorProperty(QStringLiteral("buffer"), method_get_buffer, 0); + defineAccessorProperty(QStringLiteral("byteLength"), method_get_byteLength, 0); + defineAccessorProperty(QStringLiteral("byteOffset"), method_get_byteOffset, 0); + defineAccessorProperty(QStringLiteral("length"), method_get_length, 0); + defineReadonlyProperty(QStringLiteral("BYTES_PER_ELEMENT"), Primitive::fromInt32(operations[ctor->d()->type].bytesPerElement)); + + defineDefaultProperty(QStringLiteral("set"), method_set, 1); + defineDefaultProperty(QStringLiteral("subarray"), method_subarray, 0); +} + +ReturnedValue TypedArrayPrototype::method_get_buffer(CallContext *ctx) +{ + Scope scope(ctx); + Scoped<TypedArray> v(scope, ctx->d()->callData->thisObject); + if (!v) + return ctx->throwTypeError(); + + return Encode(v->d()->buffer->asReturnedValue()); +} + +ReturnedValue TypedArrayPrototype::method_get_byteLength(CallContext *ctx) +{ + Scope scope(ctx); + Scoped<TypedArray> v(scope, ctx->d()->callData->thisObject); + if (!v) + return ctx->throwTypeError(); + + return Encode(v->d()->byteLength); +} + +ReturnedValue TypedArrayPrototype::method_get_byteOffset(CallContext *ctx) +{ + Scope scope(ctx); + Scoped<TypedArray> v(scope, ctx->d()->callData->thisObject); + if (!v) + return ctx->throwTypeError(); + + return Encode(v->d()->byteOffset); +} + +ReturnedValue TypedArrayPrototype::method_get_length(CallContext *ctx) +{ + Scope scope(ctx); + Scoped<TypedArray> v(scope, ctx->d()->callData->thisObject); + if (!v) + return ctx->throwTypeError(); + + return Encode(v->d()->byteLength/v->d()->type->bytesPerElement); +} + +ReturnedValue TypedArrayPrototype::method_set(CallContext *ctx) +{ + Scope scope(ctx); + Scoped<TypedArray> a(scope, ctx->d()->callData->thisObject); + if (!a) + return ctx->throwTypeError(); + Scoped<ArrayBuffer> buffer(scope, a->d()->buffer); + if (!buffer) + ctx->throwTypeError(); + + double doffset = ctx->d()->callData->argc >= 2 ? ctx->d()->callData->args[1].toInteger() : 0; + if (scope.engine->hasException) + return Encode::undefined(); + + if (doffset < 0 || doffset >= UINT_MAX) + return ctx->throwRangeError(QStringLiteral("TypedArray.set: out of range")); + uint offset = (uint)doffset; + uint elementSize = a->d()->type->bytesPerElement; + + Scoped<TypedArray> srcTypedArray(scope, ctx->d()->callData->args[0]); + if (!srcTypedArray) { + // src is a regular object + ScopedObject o(scope, ctx->d()->callData->args[0].toObject(ctx)); + if (scope.engine->hasException || !o) + return ctx->throwTypeError(); + + double len = ScopedValue(scope, o->get(scope.engine->id_length))->toNumber(); + uint l = (uint)len; + if (scope.engine->hasException || l != len) + return ctx->throwTypeError(); + + if (offset + l > a->length()) + return ctx->throwRangeError(QStringLiteral("TypedArray.set: out of range")); + + uint idx = 0; + char *b = buffer->d()->data->data() + a->d()->byteOffset + offset*elementSize; + ScopedValue val(scope); + while (idx < l) { + val = o->getIndexed(idx); + a->d()->type->write(scope.engine, b, 0, val); + if (scope.engine->hasException) + return Encode::undefined(); + ++idx; + b += elementSize; + } + return Encode::undefined(); + } + + // src is a typed array + Scoped<ArrayBuffer> srcBuffer(scope, srcTypedArray->d()->buffer); + if (!srcBuffer) + return ctx->throwTypeError(); + + uint l = srcTypedArray->length(); + if (offset + l > a->length()) + return ctx->throwRangeError(QStringLiteral("TypedArray.set: out of range")); + + char *dest = buffer->d()->data->data() + a->d()->byteOffset + offset*elementSize; + const char *src = srcBuffer->d()->data->data() + srcTypedArray->d()->byteOffset; + if (srcTypedArray->d()->type == a->d()->type) { + // same type of typed arrays, use memmove (as srcbuffer and buffer could be the same) + memmove(dest, src, srcTypedArray->d()->byteLength); + return Encode::undefined(); + } + + char *srcCopy = 0; + if (buffer->d() == srcBuffer->d()) { + // same buffer, need to take a temporary copy, to not run into problems + srcCopy = new char[srcTypedArray->d()->byteLength]; + memcpy(srcCopy, src, srcTypedArray->d()->byteLength); + src = srcCopy; + } + + // typed arrays of different kind, need to manually loop + uint srcElementSize = srcTypedArray->d()->type->bytesPerElement; + TypedArrayRead read = srcTypedArray->d()->type->read; + TypedArrayWrite write = a->d()->type->write; + for (uint i = 0; i < l; ++i) { + Primitive val; + val.val = read(src, i*srcElementSize); + write(scope.engine, dest, i*elementSize, val); + } + + if (srcCopy) + delete [] srcCopy; + + return Encode::undefined(); +} + +ReturnedValue TypedArrayPrototype::method_subarray(CallContext *ctx) +{ + Scope scope(ctx); + Scoped<TypedArray> a(scope, ctx->d()->callData->thisObject); + + if (!a) + return ctx->throwTypeError(); + + Scoped<ArrayBuffer> buffer(scope, a->d()->buffer); + if (!buffer) + return ctx->throwTypeError(); + + int len = a->length(); + double b = ctx->d()->callData->argc > 0 ? ctx->d()->callData->args[0].toInteger() : 0; + if (b < 0) + b = len + b; + uint begin = (uint)qBound(0., b, (double)len); + + double e = ctx->d()->callData->argc < 2 || ctx->d()->callData->args[1].isUndefined() ? len : ctx->d()->callData->args[1].toInteger(); + if (e < 0) + e = len + e; + uint end = (uint)qBound(0., e, (double)len); + if (end < begin) + end = begin; + + if (scope.engine->hasException) + return Encode::undefined(); + + int newLen = end - begin; + + Scoped<FunctionObject> constructor(scope, a->get(scope.engine->id_constructor)); + if (!constructor) + return ctx->throwTypeError(); + + ScopedCallData callData(scope, 3); + callData->args[0] = buffer; + callData->args[1] = Encode(a->d()->byteOffset + begin*a->d()->type->bytesPerElement); + callData->args[2] = Encode(newLen); + return constructor->construct(callData); +} diff --git a/src/qml/jsruntime/qv4typedarray_p.h b/src/qml/jsruntime/qv4typedarray_p.h new file mode 100644 index 0000000000..deafb0eed8 --- /dev/null +++ b/src/qml/jsruntime/qv4typedarray_p.h @@ -0,0 +1,133 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtQml module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL21$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef QV4TYPEDARRAY_H +#define QV4TYPEDARRAY_H + +#include "qv4object_p.h" +#include "qv4functionobject_p.h" + +QT_BEGIN_NAMESPACE + +namespace QV4 { + +struct ArrayBuffer; + +typedef ReturnedValue (*TypedArrayRead)(const char *data, int index); +typedef void (*TypedArrayWrite)(ExecutionEngine *engine, char *data, int index, ValueRef value); + +struct TypedArrayOperations { + int bytesPerElement; + const char *name; + TypedArrayRead read; + TypedArrayWrite write; +}; + +struct TypedArray : Object +{ + enum Type { + Int8Array, + UInt8Array, + UInt8ClampedArray, + Int16Array, + UInt16Array, + Int32Array, + UInt32Array, + Float32Array, + Float64Array, + NTypes + }; + + struct Data : Object::Data { + Data(ExecutionEngine *e, Type t); + + const TypedArrayOperations *type; + ArrayBuffer *buffer; + uint byteLength; + uint byteOffset; + }; + V4_OBJECT(Object) + + uint length() const { + return d()->byteLength/d()->type->bytesPerElement; + } + + + static void markObjects(Managed *that, ExecutionEngine *e); + static ReturnedValue getIndexed(Managed *m, uint index, bool *hasProperty); + static void putIndexed(Managed *m, uint index, const ValueRef value); +}; + +struct TypedArrayCtor: FunctionObject +{ + struct Data : FunctionObject::Data { + Data(ExecutionContext *scope, TypedArray::Type t); + + TypedArray::Type type; + }; + + V4_OBJECT(FunctionObject) + + static ReturnedValue construct(Managed *m, CallData *callData); + static ReturnedValue call(Managed *that, CallData *callData); +}; + + +struct TypedArrayPrototype : Object +{ + struct Data : Object::Data { + Data(ExecutionEngine *e, TypedArray::Type t) + : Object::Data(e) + , type(t) + { + setVTable(staticVTable()); + } + TypedArray::Type type; + }; + V4_OBJECT(Object) + + void init(ExecutionEngine *engine, TypedArrayCtor *ctor); + + static ReturnedValue method_get_buffer(CallContext *ctx); + static ReturnedValue method_get_byteLength(CallContext *ctx); + static ReturnedValue method_get_byteOffset(CallContext *ctx); + static ReturnedValue method_get_length(CallContext *ctx); + + static ReturnedValue method_set(CallContext *ctx); + static ReturnedValue method_subarray(CallContext *ctx); +}; + +} // namespace QV4 + +QT_END_NAMESPACE + +#endif diff --git a/src/quick/items/qquickitem.cpp b/src/quick/items/qquickitem.cpp index b2ee5af6ff..41e9e0e57a 100644 --- a/src/quick/items/qquickitem.cpp +++ b/src/quick/items/qquickitem.cpp @@ -2724,8 +2724,11 @@ void QQuickItemPrivate::addChild(QQuickItem *child) #ifndef QT_NO_CURSOR QQuickItemPrivate *childPrivate = QQuickItemPrivate::get(child); - if (childPrivate->extra.isAllocated()) - incrementCursorCount(childPrivate->extra.value().numItemsWithCursor); + + // if the added child has a cursor and we do not currently have any children + // with cursors, bubble the notification up + if (childPrivate->hasCursorInChild && !hasCursorInChild) + setHasCursorInChild(true); #endif markSortedChildrenDirty(child); @@ -2747,8 +2750,10 @@ void QQuickItemPrivate::removeChild(QQuickItem *child) #ifndef QT_NO_CURSOR QQuickItemPrivate *childPrivate = QQuickItemPrivate::get(child); - if (childPrivate->extra.isAllocated()) - incrementCursorCount(-childPrivate->extra.value().numItemsWithCursor); + + // turn it off, if nothing else is using it + if (childPrivate->hasCursorInChild && hasCursorInChild) + setHasCursorInChild(false); #endif markSortedChildrenDirty(child); @@ -2950,6 +2955,7 @@ QQuickItemPrivate::QQuickItemPrivate() , isAccessible(false) , culled(false) , hasCursor(false) + , hasCursorInChild(false) , activeFocusOnTab(false) , implicitAntialiasing(false) , antialiasingValid(false) @@ -6733,15 +6739,27 @@ void QQuickItem::setAcceptHoverEvents(bool enabled) d->hoverEnabled = enabled; } -void QQuickItemPrivate::incrementCursorCount(int delta) +void QQuickItemPrivate::setHasCursorInChild(bool hasCursor) { #ifndef QT_NO_CURSOR Q_Q(QQuickItem); - extra.value().numItemsWithCursor += delta; + + // if we're asked to turn it off (because of an unsetcursor call, or a node + // removal) then we should check our children and make sure it's really ok + // to turn it off. + if (!hasCursor && hasCursorInChild) { + foreach (QQuickItem *otherChild, childItems) { + QQuickItemPrivate *otherChildPrivate = QQuickItemPrivate::get(otherChild); + if (otherChildPrivate->hasCursorInChild) + return; // nope! sorry, something else wants it kept on. + } + } + + hasCursorInChild = hasCursor; QQuickItem *parent = q->parentItem(); if (parent) { QQuickItemPrivate *parentPrivate = QQuickItemPrivate::get(parent); - parentPrivate->incrementCursorCount(delta); + parentPrivate->setHasCursorInChild(hasCursor); } #endif } @@ -6804,7 +6822,7 @@ void QQuickItem::setCursor(const QCursor &cursor) } if (!d->hasCursor) { - d->incrementCursorCount(+1); + d->setHasCursorInChild(true); d->hasCursor = true; if (d->window) { QWindow *renderWindow = QQuickRenderControl::renderWindowFor(d->window); @@ -6827,7 +6845,7 @@ void QQuickItem::unsetCursor() Q_D(QQuickItem); if (!d->hasCursor) return; - d->incrementCursorCount(-1); + d->setHasCursorInChild(false); d->hasCursor = false; if (d->extra.isAllocated()) d->extra->cursor = QCursor(); @@ -7809,9 +7827,6 @@ QQuickItemPrivate::ExtraData::ExtraData() : z(0), scale(1), rotation(0), opacity(1), contents(0), screenAttached(0), layoutDirectionAttached(0), keyHandler(0), layer(0), -#ifndef QT_NO_CURSOR - numItemsWithCursor(0), -#endif effectRefCount(0), hideRefCount(0), opacityNode(0), clipNode(0), rootNode(0), acceptedMouseButtons(0), origin(QQuickItem::Center), diff --git a/src/quick/items/qquickitem_p.h b/src/quick/items/qquickitem_p.h index e416facc6b..02d3b820a0 100644 --- a/src/quick/items/qquickitem_p.h +++ b/src/quick/items/qquickitem_p.h @@ -342,7 +342,6 @@ public: mutable QQuickItemLayer *layer; #ifndef QT_NO_CURSOR QCursor cursor; - int numItemsWithCursor; #endif QPointF userTransformOriginPoint; @@ -353,6 +352,8 @@ public: QQuickDefaultClipNode *clipNode; QSGRootNode *rootNode; + QObjectList resourcesList; + // Although acceptedMouseButtons is inside ExtraData, we actually store // the LeftButton flag in the extra.flag() bit. This is because it is // extremely common to set acceptedMouseButtons to LeftButton, but very @@ -362,7 +363,7 @@ public: QQuickItem::TransformOrigin origin:5; uint transparentForPositioner : 1; - QObjectList resourcesList; + // 26 bits padding }; QLazilyAllocated<ExtraData> extra; @@ -414,6 +415,7 @@ public: bool culled:1; bool hasCursor:1; // Bit 32 + bool hasCursorInChild:1; bool activeFocusOnTab:1; bool implicitAntialiasing:1; bool antialiasingValid:1; @@ -576,7 +578,7 @@ public: virtual void mirrorChange() {} - void incrementCursorCount(int delta); + void setHasCursorInChild(bool hasCursor); // recursive helper to let a visual parent mark its visual children void markObjects(QV4::ExecutionEngine *e); diff --git a/src/quick/items/qquickwindow.cpp b/src/quick/items/qquickwindow.cpp index 6e82be383b..c0d2dc3f33 100644 --- a/src/quick/items/qquickwindow.cpp +++ b/src/quick/items/qquickwindow.cpp @@ -2020,11 +2020,14 @@ bool QQuickWindowPrivate::deliverMatchingPointsToItem(QQuickItem *item, QTouchEv touchEvent.data()->setTarget(item); bool touchEventAccepted = false; + qCDebug(DBG_TOUCH) << " - considering delivering " << touchEvent << " to " << item; + // First check whether the parent wants to be a filter, // and if the parent accepts the event we are done. if (sendFilteredTouchEvent(item->parentItem(), item, event, hasFiltered)) { // If the touch was accepted (regardless by whom or in what form), // update acceptedNewPoints + qCDebug(DBG_TOUCH) << " - can't. intercepted " << touchEvent.data() << " to " << item->parentItem() << " instead of " << item; foreach (int id, matchingNewPoints) acceptedNewPoints->insert(id); return true; @@ -2035,6 +2038,7 @@ bool QQuickWindowPrivate::deliverMatchingPointsToItem(QQuickItem *item, QTouchEv itemForTouchPointId[id] = item; // Deliver the touch event to the given item + qCDebug(DBG_TOUCH) << " - actually delivering " << touchEvent << " to " << item; QCoreApplication::sendEvent(item, touchEvent.data()); touchEventAccepted = touchEvent->isAccepted(); @@ -2271,10 +2275,7 @@ QQuickItem *QQuickWindowPrivate::findCursorItem(QQuickItem *item, const QPointF return 0; } - const int numCursorsInHierarchy = itemPrivate->extra.isAllocated() ? itemPrivate->extra.value().numItemsWithCursor : 0; - const int numChildrenWithCursor = itemPrivate->hasCursor ? numCursorsInHierarchy-1 : numCursorsInHierarchy; - - if (numChildrenWithCursor > 0) { + if (itemPrivate->hasCursorInChild) { QList<QQuickItem *> children = itemPrivate->paintOrderChildItems(); for (int ii = children.count() - 1; ii >= 0; --ii) { QQuickItem *child = children.at(ii); @@ -2307,6 +2308,7 @@ bool QQuickWindowPrivate::sendFilteredTouchEvent(QQuickItem *target, QQuickItem QScopedPointer<QTouchEvent> targetEvent(touchEventForItemBounds(target, *event)); if (!targetEvent->touchPoints().isEmpty()) { if (target->childMouseEventFilter(item, targetEvent.data())) { + qCDebug(DBG_TOUCH) << " - first chance intercepted on childMouseEventFilter by " << target; QVector<int> touchIds; for (int i = 0; i < targetEvent->touchPoints().size(); ++i) touchIds.append(targetEvent->touchPoints().at(i).id()); @@ -2347,6 +2349,7 @@ bool QQuickWindowPrivate::sendFilteredTouchEvent(QQuickItem *target, QQuickItem // targetEvent is already transformed wrt local position, velocity, etc. QScopedPointer<QMouseEvent> mouseEvent(touchToMouseEvent(t, tp, event, item, false)); if (target->childMouseEventFilter(item, mouseEvent.data())) { + qCDebug(DBG_TOUCH) << " - second chance intercepted on childMouseEventFilter by " << target; if (t != QEvent::MouseButtonRelease) { itemForTouchPointId[tp.id()] = target; touchMouseId = tp.id(); @@ -2559,7 +2562,9 @@ bool QQuickWindow::sendEvent(QQuickItem *item, QEvent *e) case QEvent::TouchUpdate: case QEvent::TouchEnd: { QSet<QQuickItem*> hasFiltered; - d->sendFilteredTouchEvent(item->parentItem(), item, static_cast<QTouchEvent *>(e), &hasFiltered); + QTouchEvent *ev = static_cast<QTouchEvent *>(e); + qCDebug(DBG_TOUCH) << " - sendEvent for " << ev << " to " << item->parentItem() << " and " << item; + d->sendFilteredTouchEvent(item->parentItem(), item, ev, &hasFiltered); } break; default: @@ -2658,6 +2663,38 @@ static inline QSGNode *qquickitem_before_paintNode(QQuickItemPrivate *d) return Q_UNLIKELY(before) ? QQuickItemPrivate::get(before)->itemNode() : 0; } +static QVector<QSGNode *> buildOrderedNodeList(QQuickItemPrivate *itemPriv) +{ + QList<QQuickItem *> orderedChildren = itemPriv->paintOrderChildItems(); + QVector<QSGNode *> desiredNodes; + desiredNodes.reserve(orderedChildren.size() + 1); // + 1 for the paintNode + + int ii = 0; + + for (; ii < orderedChildren.count() && orderedChildren.at(ii)->z() < 0; ++ii) { + QQuickItemPrivate *childPrivate = QQuickItemPrivate::get(orderedChildren.at(ii)); + if (!childPrivate->explicitVisible && + (!childPrivate->extra.isAllocated() || !childPrivate->extra->effectRefCount)) + continue; + + desiredNodes.append(childPrivate->itemNode()); + } + + if (itemPriv->paintNode) + desiredNodes.append(itemPriv->paintNode); + + for (; ii < orderedChildren.count(); ++ii) { + QQuickItemPrivate *childPrivate = QQuickItemPrivate::get(orderedChildren.at(ii)); + if (!childPrivate->explicitVisible && + (!childPrivate->extra.isAllocated() || !childPrivate->extra->effectRefCount)) + continue; + + desiredNodes.append(childPrivate->itemNode()); + } + + return desiredNodes; +} + void QQuickWindowPrivate::updateDirtyNode(QQuickItem *item) { QQuickItemPrivate *itemPriv = QQuickItemPrivate::get(item); @@ -2758,36 +2795,78 @@ void QQuickWindowPrivate::updateDirtyNode(QQuickItem *item) } if (dirty & QQuickItemPrivate::ChildrenUpdateMask) { - QSGNode *parent = itemPriv->childContainerNode(); - parent->removeAllChildNodes(); - - QList<QQuickItem *> orderedChildren = itemPriv->paintOrderChildItems(); - int ii = 0; + QVector<QSGNode *> desiredNodes = buildOrderedNodeList(itemPriv); + + // now start making current state match the promised land of + // desiredNodes. in the case of our current state matching desiredNodes + // (though why would we get ChildrenUpdateMask with no changes?) then we + // should make no changes at all. + + // how many nodes did we process, when examining changes + int desiredNodesProcessed = 0; + + // currentNode is how far, in our present tree, we have processed. we + // make use of this later on to trim the current child list if the + // desired list is shorter. + QSGNode *groupNode = itemPriv->childContainerNode(); + QSGNode *currentNode = groupNode->firstChild(); + int added = 0; + int removed = 0; + int replaced = 0; +#if defined(CHILDRENUPDATE_DEBUG) + // This is slow! Do not do this in a normal/profiling build! + int initialCount = groupNode->childCount(); +#endif - for (; ii < orderedChildren.count() && orderedChildren.at(ii)->z() < 0; ++ii) { - QQuickItemPrivate *childPrivate = QQuickItemPrivate::get(orderedChildren.at(ii)); - if (!childPrivate->explicitVisible && - (!childPrivate->extra.isAllocated() || !childPrivate->extra->effectRefCount)) - continue; - if (childPrivate->itemNode()->parent()) - childPrivate->itemNode()->parent()->removeChildNode(childPrivate->itemNode()); + while (currentNode && desiredNodesProcessed < desiredNodes.size()) { + QSGNode *desiredNode = desiredNodes.at(desiredNodesProcessed); + + // uh oh... reality and our utopic paradise are diverging! + // we need to reconcile this... + if (currentNode != desiredNode) { + // for now, we're just removing the node from the children - + // and replacing it with the new node. + if (desiredNode->parent()) + desiredNode->parent()->removeChildNode(desiredNode); + groupNode->insertChildNodeAfter(desiredNode, currentNode); + groupNode->removeChildNode(currentNode); + replaced++; + + // since we just replaced currentNode, we also need to reset + // the pointer. + currentNode = desiredNode; + } - itemPriv->childContainerNode()->appendChildNode(childPrivate->itemNode()); + currentNode = currentNode->nextSibling(); + desiredNodesProcessed++; } - if (itemPriv->paintNode) - itemPriv->childContainerNode()->appendChildNode(itemPriv->paintNode); - - for (; ii < orderedChildren.count(); ++ii) { - QQuickItemPrivate *childPrivate = QQuickItemPrivate::get(orderedChildren.at(ii)); - if (!childPrivate->explicitVisible && - (!childPrivate->extra.isAllocated() || !childPrivate->extra->effectRefCount)) - continue; - if (childPrivate->itemNode()->parent()) - childPrivate->itemNode()->parent()->removeChildNode(childPrivate->itemNode()); - - itemPriv->childContainerNode()->appendChildNode(childPrivate->itemNode()); + // if we didn't process as many nodes as in the new list, then we have + // more nodes at the end of desiredNodes to append to our list. + // this will be the case when adding new nodes, for instance. + if (desiredNodesProcessed < desiredNodes.size()) { + for (int i = desiredNodesProcessed; i < desiredNodes.size(); ++i) { + QSGNode *desiredNode = desiredNodes.at(i); + if (desiredNode->parent()) + desiredNode->parent()->removeChildNode(desiredNode); + groupNode->appendChildNode(desiredNode); + added++; + } + } else if (currentNode) { + // on the other hand, if we processed less than our current node + // tree, then nodes have been _removed_ from the scene, and we need + // to take care of that here. + while (currentNode) { + QSGNode *node = currentNode->nextSibling(); + groupNode->removeChildNode(currentNode); + currentNode = node; + removed++; + } } + +#if defined(CHILDRENUPDATE_DEBUG) + qDebug() << "Done children update for " << itemPriv << "- before:" << initialCount << "after:" << groupNode->childCount() << "added:" << added << "removed:" << removed << "replaced:" << replaced; +#endif } if ((dirty & QQuickItemPrivate::Size) && itemPriv->clipNode()) { diff --git a/tests/auto/qml/qjsengine/tst_qjsengine.cpp b/tests/auto/qml/qjsengine/tst_qjsengine.cpp index 741fa9f04d..2e7fe34796 100644 --- a/tests/auto/qml/qjsengine/tst_qjsengine.cpp +++ b/tests/auto/qml/qjsengine/tst_qjsengine.cpp @@ -802,8 +802,18 @@ void tst_QJSEngine::globalObjectProperties_enumerate() << "unescape" << "SyntaxError" << "undefined" - // JavaScriptCore << "JSON" + << "ArrayBuffer" + << "DataView" + << "Int8Array" + << "Uint8Array" + << "Uint8ClampedArray" + << "Int16Array" + << "Uint16Array" + << "Int32Array" + << "Uint32Array" + << "Float32Array" + << "Float64Array" ; QSet<QString> actualNames; { diff --git a/tests/manual/v4/typedarray.js b/tests/manual/v4/typedarray.js new file mode 100644 index 0000000000..3a18531af8 --- /dev/null +++ b/tests/manual/v4/typedarray.js @@ -0,0 +1,13 @@ +var y = new Float32Array(3); +y.set([1.3, 2.1, 3.5]); +x = new Uint8ClampedArray(256); +x.set([-3, 2.5, 3.5, 256]); +x.set(y, 5); +print(x.length, x.byteLength, x[0], x[1], x[2], x[3], x[5], x[6], x[7]) +x = new Int16Array(x); +print(x.length, x.byteLength, x[0], x[1], x[2], x[3], x[5], x[6], x[7]) +x = x.subarray(1, 2); +print(x) +print(x.length, x.byteLength, x.byteOffset, x[0], x[1])//, x[2], x[3], x[5], x[6], x[7]) +x = new Int8Array([-1, 0, 3, 127, 255]); +print(x.length, x.byteLength, x.byteOffset, x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7]) diff --git a/tests/manual/v4/typedarrays.js b/tests/manual/v4/typedarrays.js new file mode 100644 index 0000000000..d7a863e573 --- /dev/null +++ b/tests/manual/v4/typedarrays.js @@ -0,0 +1,738 @@ +// Copyright 2013 the V8 project authors. All rights reserved. +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following +// disclaimer in the documentation and/or other materials provided +// with the distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// ArrayBuffer + +function assertThrows(func, type) { + var ok = false; + try { + func() + } catch (e) { + if (type !== undefined) + ok = (typeof(e) == typeof(new type())) + else + ok = true + } + if (!ok) + throw "assertThrows failed" +} + +function assertSame(a, b) { + if (a !== b) + throw "assertSame failed:" + a + "!==" + b +} + +function assertTrue(b) { + if (!b) + throw "assertTrue failed:" + b +} + +function assertEquals(a, b) { + if (isNaN(a) && isNaN(b)) + return + if (a !== b) + throw "assertEquals failed:" + a + "!==" + b +} + +function assertArrayEquals(a, b) +{ + if (a.length != b.length) + throw "assertArrayEquals failed:" + a + "!=" + b + var i; + for (i = 0; i < a.length; ++i) + assertEquals(a[i], b[i]) +} + +function TestByteLength(param, expectedByteLength) { + var ab = new ArrayBuffer(param); + assertSame(expectedByteLength, ab.byteLength); +} + +function TestArrayBufferCreation() { + TestByteLength(1, 1); + TestByteLength(256, 256); + TestByteLength(2.567, 2); + + TestByteLength("abc", 0); + + TestByteLength(0, 0); + + assertThrows(function() { new ArrayBuffer(-10); }, RangeError); + assertThrows(function() { new ArrayBuffer(-2.567); }, RangeError); + +/* TODO[dslomov]: Reenable the test + assertThrows(function() { + var ab1 = new ArrayBuffer(0xFFFFFFFFFFFF) + }, RangeError); +*/ + + var ab = new ArrayBuffer(); + assertSame(0, ab.byteLength); +} + +TestArrayBufferCreation(); + +function TestByteLengthNotWritable() { + var ab = new ArrayBuffer(1024); + assertSame(1024, ab.byteLength); + + assertThrows(function() { "use strict"; ab.byteLength = 42; }, TypeError); +} + +TestByteLengthNotWritable(); + +function TestSlice(expectedResultLen, initialLen, start, end) { + var ab = new ArrayBuffer(initialLen); + var a1 = new Uint8Array(ab); + for (var i = 0; i < a1.length; i++) { + a1[i] = 0xCA; + } + var slice = ab.slice(start, end); + assertSame(expectedResultLen, slice.byteLength); + var a2 = new Uint8Array(slice); + for (var i = 0; i < a2.length; i++) { + assertSame(0xCA, a2[i]); + } +} + +function TestArrayBufferSlice() { + var ab = new ArrayBuffer(1024); + var ab1 = ab.slice(512, 1024); + assertSame(512, ab1.byteLength); + + TestSlice(512, 1024, 512, 1024); + TestSlice(512, 1024, 512); + + TestSlice(0, 0, 1, 20); + TestSlice(100, 100, 0, 100); + TestSlice(100, 100, 0, 1000); + + TestSlice(0, 100, 5, 1); + + TestSlice(1, 100, -11, -10); + TestSlice(9, 100, -10, 99); + TestSlice(0, 100, -10, 80); + TestSlice(10, 100, 80, -10); + + TestSlice(10, 100, 90, "100"); + TestSlice(10, 100, "90", "100"); + + TestSlice(0, 100, 90, "abc"); + TestSlice(10, 100, "abc", 10); + + TestSlice(10, 100, 0.96, 10.96); + TestSlice(10, 100, 0.96, 10.01); + TestSlice(10, 100, 0.01, 10.01); + TestSlice(10, 100, 0.01, 10.96); + + TestSlice(10, 100, 90); + TestSlice(10, 100, -10); +} + +TestArrayBufferSlice(); + +// Typed arrays + +function TestTypedArray(constr, elementSize, typicalElement) { + assertSame(elementSize, constr.BYTES_PER_ELEMENT); + + var ab = new ArrayBuffer(256*elementSize); + + var a0 = new constr(30); + assertTrue(ArrayBuffer.isView(a0)); + assertSame(elementSize, a0.BYTES_PER_ELEMENT); + assertSame(30, a0.length); + assertSame(30*elementSize, a0.byteLength); + assertSame(0, a0.byteOffset); + assertSame(30*elementSize, a0.buffer.byteLength); + + var aLen0 = new constr(0); + assertSame(elementSize, aLen0.BYTES_PER_ELEMENT); + assertSame(0, aLen0.length); + assertSame(0, aLen0.byteLength); + assertSame(0, aLen0.byteOffset); + assertSame(0, aLen0.buffer.byteLength); + + var aOverBufferLen0 = new constr(ab, 128*elementSize, 0); + assertSame(ab, aOverBufferLen0.buffer); + assertSame(elementSize, aOverBufferLen0.BYTES_PER_ELEMENT); + assertSame(0, aOverBufferLen0.length); + assertSame(0, aOverBufferLen0.byteLength); + assertSame(128*elementSize, aOverBufferLen0.byteOffset); + + var a1 = new constr(ab, 128*elementSize, 128); + assertSame(ab, a1.buffer); + assertSame(elementSize, a1.BYTES_PER_ELEMENT); + assertSame(128, a1.length); + assertSame(128*elementSize, a1.byteLength); + assertSame(128*elementSize, a1.byteOffset); + + + var a2 = new constr(ab, 64*elementSize, 128); + assertSame(ab, a2.buffer); + assertSame(elementSize, a2.BYTES_PER_ELEMENT); + assertSame(128, a2.length); + assertSame(128*elementSize, a2.byteLength); + assertSame(64*elementSize, a2.byteOffset); + + var a3 = new constr(ab, 192*elementSize); + assertSame(ab, a3.buffer); + assertSame(64, a3.length); + assertSame(64*elementSize, a3.byteLength); + assertSame(192*elementSize, a3.byteOffset); + + var a4 = new constr(ab); + assertSame(ab, a4.buffer); + assertSame(256, a4.length); + assertSame(256*elementSize, a4.byteLength); + assertSame(0, a4.byteOffset); + + + var i; + for (i = 0; i < 128; i++) { + a1[i] = typicalElement; + } + + for (i = 0; i < 128; i++) { + assertSame(typicalElement, a1[i]); + } + + for (i = 0; i < 64; i++) { + assertSame(0, a2[i]); + } + + for (i = 64; i < 128; i++) { + assertSame(typicalElement, a2[i]); + } + + for (i = 0; i < 64; i++) { + assertSame(typicalElement, a3[i]); + } + + for (i = 0; i < 128; i++) { + assertSame(0, a4[i]); + } + + for (i = 128; i < 256; i++) { + assertSame(typicalElement, a4[i]); + } + + var aAtTheEnd = new constr(ab, 256*elementSize); + assertSame(elementSize, aAtTheEnd.BYTES_PER_ELEMENT); + assertSame(0, aAtTheEnd.length); + assertSame(0, aAtTheEnd.byteLength); + assertSame(256*elementSize, aAtTheEnd.byteOffset); + + assertThrows(function () { new constr(ab, 257*elementSize); }, RangeError); + assertThrows( + function () { new constr(ab, 128*elementSize, 192); }, + RangeError); + + if (elementSize !== 1) { + assertThrows(function() { new constr(ab, 128*elementSize - 1, 10); }, + RangeError); + var unalignedArrayBuffer = new ArrayBuffer(10*elementSize + 1); + var goodArray = new constr(unalignedArrayBuffer, 0, 10); + assertSame(10, goodArray.length); + assertSame(10*elementSize, goodArray.byteLength); + assertThrows(function() { new constr(unalignedArrayBuffer)}, RangeError); + assertThrows(function() { new constr(unalignedArrayBuffer, 5*elementSize)}, + RangeError); + } + + var aFromString = new constr("30"); + assertSame(elementSize, aFromString.BYTES_PER_ELEMENT); + assertSame(30, aFromString.length); + assertSame(30*elementSize, aFromString.byteLength); + assertSame(0, aFromString.byteOffset); + assertSame(30*elementSize, aFromString.buffer.byteLength); + + var jsArray = []; + for (i = 0; i < 30; i++) { + jsArray.push(typicalElement); + } + var aFromArray = new constr(jsArray); + assertSame(elementSize, aFromArray.BYTES_PER_ELEMENT); + assertSame(30, aFromArray.length); + assertSame(30*elementSize, aFromArray.byteLength); + assertSame(0, aFromArray.byteOffset); + assertSame(30*elementSize, aFromArray.buffer.byteLength); + for (i = 0; i < 30; i++) { + assertSame(typicalElement, aFromArray[i]); + } + + var abLen0 = new ArrayBuffer(0); + var aOverAbLen0 = new constr(abLen0); + assertSame(abLen0, aOverAbLen0.buffer); + assertSame(elementSize, aOverAbLen0.BYTES_PER_ELEMENT); + assertSame(0, aOverAbLen0.length); + assertSame(0, aOverAbLen0.byteLength); + assertSame(0, aOverAbLen0.byteOffset); + + var aNoParam = new constr(); + assertSame(elementSize, aNoParam.BYTES_PER_ELEMENT); + assertSame(0, aNoParam.length); + assertSame(0, aNoParam.byteLength); + assertSame(0, aNoParam.byteOffset); +} + +TestTypedArray(Uint8Array, 1, 0xFF); +TestTypedArray(Int8Array, 1, -0x7F); +TestTypedArray(Uint16Array, 2, 0xFFFF); +TestTypedArray(Int16Array, 2, -0x7FFF); +TestTypedArray(Uint32Array, 4, 0xFFFFFFFF); +TestTypedArray(Int32Array, 4, -0x7FFFFFFF); +TestTypedArray(Float32Array, 4, 0.5); +TestTypedArray(Float64Array, 8, 0.5); +TestTypedArray(Uint8ClampedArray, 1, 0xFF); + +function SubarrayTestCase(constructor, item, expectedResultLen, expectedStartIndex, + initialLen, start, end) { + var a = new constructor(initialLen); + var s = a.subarray(start, end); + assertSame(constructor, s.constructor); + assertSame(expectedResultLen, s.length); + if (s.length > 0) { + s[0] = item; + assertSame(item, a[expectedStartIndex]); + } +} + +function TestSubArray(constructor, item) { + SubarrayTestCase(constructor, item, 512, 512, 1024, 512, 1024); + SubarrayTestCase(constructor, item, 512, 512, 1024, 512); + + SubarrayTestCase(constructor, item, 0, undefined, 0, 1, 20); + SubarrayTestCase(constructor, item, 100, 0, 100, 0, 100); + SubarrayTestCase(constructor, item, 100, 0, 100, 0, 1000); + SubarrayTestCase(constructor, item, 0, undefined, 100, 5, 1); + + SubarrayTestCase(constructor, item, 1, 89, 100, -11, -10); + SubarrayTestCase(constructor, item, 9, 90, 100, -10, 99); + SubarrayTestCase(constructor, item, 0, undefined, 100, -10, 80); + SubarrayTestCase(constructor, item, 10,80, 100, 80, -10); + + SubarrayTestCase(constructor, item, 10,90, 100, 90, "100"); + SubarrayTestCase(constructor, item, 10,90, 100, "90", "100"); + + SubarrayTestCase(constructor, item, 0, undefined, 100, 90, "abc"); + SubarrayTestCase(constructor, item, 10,0, 100, "abc", 10); + + SubarrayTestCase(constructor, item, 10,0, 100, 0.96, 10.96); + SubarrayTestCase(constructor, item, 10,0, 100, 0.96, 10.01); + SubarrayTestCase(constructor, item, 10,0, 100, 0.01, 10.01); + SubarrayTestCase(constructor, item, 10,0, 100, 0.01, 10.96); + + + SubarrayTestCase(constructor, item, 10,90, 100, 90); + SubarrayTestCase(constructor, item, 10,90, 100, -10); + + var method = constructor.prototype.subarray; + method.call(new constructor(100), 0, 100); + var o = {}; + assertThrows(function() { method.call(o, 0, 100); }, TypeError); +} + +TestSubArray(Uint8Array, 0xFF); +TestSubArray(Int8Array, -0x7F); +TestSubArray(Uint16Array, 0xFFFF); +TestSubArray(Int16Array, -0x7FFF); +TestSubArray(Uint32Array, 0xFFFFFFFF); +TestSubArray(Int32Array, -0x7FFFFFFF); +TestSubArray(Float32Array, 0.5); +TestSubArray(Float64Array, 0.5); +TestSubArray(Uint8ClampedArray, 0xFF); + +function TestTypedArrayOutOfRange(constructor, value, result) { + var a = new constructor(1); + a[0] = value; + assertSame(result, a[0]); +} + +TestTypedArrayOutOfRange(Uint8Array, 0x1FA, 0xFA); +TestTypedArrayOutOfRange(Uint8Array, -1, 0xFF); + +TestTypedArrayOutOfRange(Int8Array, 0x1FA, 0x7A - 0x80); + +TestTypedArrayOutOfRange(Uint16Array, 0x1FFFA, 0xFFFA); +TestTypedArrayOutOfRange(Uint16Array, -1, 0xFFFF); +TestTypedArrayOutOfRange(Int16Array, 0x1FFFA, 0x7FFA - 0x8000); + +TestTypedArrayOutOfRange(Uint32Array, 0x1FFFFFFFA, 0xFFFFFFFA); +TestTypedArrayOutOfRange(Uint32Array, -1, 0xFFFFFFFF); +TestTypedArrayOutOfRange(Int32Array, 0x1FFFFFFFA, 0x7FFFFFFA - 0x80000000); + +TestTypedArrayOutOfRange(Uint8ClampedArray, 0x1FA, 0xFF); +TestTypedArrayOutOfRange(Uint8ClampedArray, -1, 0); + +var typedArrayConstructors = [ + Uint8Array, + Int8Array, + Uint16Array, + Int16Array, + Uint32Array, + Int32Array, + Uint8ClampedArray, + Float32Array, + Float64Array]; + +function TestPropertyTypeChecks(constructor) { + function CheckProperty(name) { + var d = Object.getOwnPropertyDescriptor(constructor.prototype, name); + var o = {}; + assertThrows(function() {d.get.call(o);}, TypeError); + for (var i = 0; i < typedArrayConstructors.length; i++) { + var ctor = typedArrayConstructors[i]; + var a = new ctor(10); + /* Lars: According to my interpretation of ecma6 the test below is wrong. The accessors for the buffer etc. properties + do not check for type matching, are the same for all typed arrays. According to the spec they only check that the + object has a [[ViewedArrayBuffer]] internal property which they all have. + + if (ctor === constructor) { + d.get.call(a); // shouldn't throw + } else { + assertThrows(function() {d.get.call(a);}, TypeError); + } + + the below is IMO correct: + */ + d.get.call(a); // shouldn't throw + assertThrows(function() {d.get.call({});}, TypeError); // throw on objects that aren't Typed Arrays + } + } + + CheckProperty("buffer"); + CheckProperty("byteOffset"); + CheckProperty("byteLength"); + CheckProperty("length"); +} + +for(i = 0; i < typedArrayConstructors.length; i++) { + TestPropertyTypeChecks(typedArrayConstructors[i]); +} + + +function TestTypedArraySet() { + // Test array.set in different combinations. + + function assertArrayPrefix(expected, array) { + for (var i = 0; i < expected.length; ++i) { + assertEquals(expected[i], array[i]); + } + } + + var a11 = new Int16Array([1, 2, 3, 4, 0, -1]) + var a12 = new Uint16Array(15) + a12.set(a11, 3) + assertArrayPrefix([0, 0, 0, 1, 2, 3, 4, 0, 0xffff, 0, 0], a12) + assertThrows(function(){ a11.set(a12) }) + + var a21 = [1, undefined, 10, NaN, 0, -1, {valueOf: function() {return 3}}] + var a22 = new Int32Array(12) + a22.set(a21, 2) + assertArrayPrefix([0, 0, 1, 0, 10, 0, 0, -1, 3, 0], a22) + + var a31 = new Float32Array([2, 4, 6, 8, 11, NaN, 1/0, -3]) + var a32 = a31.subarray(2, 6) + a31.set(a32, 4) + assertArrayPrefix([2, 4, 6, 8, 6, 8, 11, NaN], a31) + assertArrayPrefix([6, 8, 6, 8], a32) + + var a4 = new Uint8ClampedArray([3,2,5,6]) + a4.set(a4) + assertArrayPrefix([3, 2, 5, 6], a4) + + // Cases with overlapping backing store but different element sizes. + var b = new ArrayBuffer(4) + var a5 = new Int16Array(b) + var a50 = new Int8Array(b) + var a51 = new Int8Array(b, 0, 2) + var a52 = new Int8Array(b, 1, 2) + var a53 = new Int8Array(b, 2, 2) + + a5.set([0x5050, 0x0a0a]) + assertArrayPrefix([0x50, 0x50, 0x0a, 0x0a], a50) + assertArrayPrefix([0x50, 0x50], a51) + assertArrayPrefix([0x50, 0x0a], a52) + assertArrayPrefix([0x0a, 0x0a], a53) + + a50.set([0x50, 0x50, 0x0a, 0x0a]) + a51.set(a5) + assertArrayPrefix([0x50, 0x0a, 0x0a, 0x0a], a50) + + a50.set([0x50, 0x50, 0x0a, 0x0a]) + a52.set(a5) + assertArrayPrefix([0x50, 0x50, 0x0a, 0x0a], a50) + + a50.set([0x50, 0x50, 0x0a, 0x0a]) + a53.set(a5) + assertArrayPrefix([0x50, 0x50, 0x50, 0x0a], a50) + + a50.set([0x50, 0x51, 0x0a, 0x0b]) + a5.set(a51) + assertArrayPrefix([0x0050, 0x0051], a5) + + a50.set([0x50, 0x51, 0x0a, 0x0b]) + a5.set(a52) + assertArrayPrefix([0x0051, 0x000a], a5) + + a50.set([0x50, 0x51, 0x0a, 0x0b]) + a5.set(a53) + assertArrayPrefix([0x000a, 0x000b], a5) + + // Mixed types of same size. + var a61 = new Float32Array([1.2, 12.3]) + var a62 = new Int32Array(2) + a62.set(a61) + assertArrayPrefix([1, 12], a62) + a61.set(a62) + assertArrayPrefix([1, 12], a61) + + // Invalid source + var a = new Uint16Array(50); + var expected = []; + for (i = 0; i < 50; i++) { + a[i] = i; + expected.push(i); + } + /* Lars: Also here the test is wrong. According to ecma 6 this should throw: + a.set({}); + + but a.set([]) shouldn't + */ + assertThrows(function() { a.set({}) }, TypeError) + a.set([]) + assertArrayPrefix(expected, a); + assertThrows(function() { a.set.call({}) }, TypeError); + assertThrows(function() { a.set.call([]) }, TypeError); + + assertThrows(function() { a.set(0); }, TypeError); + assertThrows(function() { a.set(0, 1); }, TypeError); +} + +TestTypedArraySet(); + +function TestTypedArraysWithIllegalIndices() { + var a = new Int32Array(100); + + a[-10] = 10; + assertEquals(undefined, a[-10]); + a["-10"] = 10; + assertEquals(undefined, a["-10"]); + + var s = " -10"; + a[s] = 10; + assertEquals(10, a[s]); + var s1 = " -10 "; + a[s] = 10; + assertEquals(10, a[s]); + + a["-1e2"] = 10; + assertEquals(10, a["-1e2"]); + assertEquals(undefined, a[-1e2]); + + /* Chromium bug: 424619 + * a[-Infinity] = 50; + * assertEquals(undefined, a[-Infinity]); + */ + a[1.5] = 10; + assertEquals(undefined, a[1.5]); + var nan = Math.sqrt(-1); + a[nan] = 5; + assertEquals(5, a[nan]); + + var x = 0; + var y = -0; + assertEquals(Infinity, 1/x); + assertEquals(-Infinity, 1/y); + a[x] = 5; + a[y] = 27; + assertEquals(27, a[x]); + assertEquals(27, a[y]); +} + +// Lars: I can't find anything in the spec that should prevent indices that are not array indices from working as usual. +// So I don't see why the test is as it is. +//TestTypedArraysWithIllegalIndices(); + +function TestTypedArraysWithIllegalIndicesStrict() { + 'use strict'; + var a = new Int32Array(100); + + a[-10] = 10; + assertEquals(undefined, a[-10]); + a["-10"] = 10; + assertEquals(undefined, a["-10"]); + + var s = " -10"; + a[s] = 10; + assertEquals(10, a[s]); + var s1 = " -10 "; + a[s] = 10; + assertEquals(10, a[s]); + + a["-1e2"] = 10; + assertEquals(10, a["-1e2"]); + assertEquals(undefined, a[-1e2]); + + /* Chromium bug: 424619 + * a[-Infinity] = 50; + * assertEquals(undefined, a[-Infinity]); + */ + a[1.5] = 10; + assertEquals(undefined, a[1.5]); + var nan = Math.sqrt(-1); + a[nan] = 5; + assertEquals(5, a[nan]); + + var x = 0; + var y = -0; + assertEquals(Infinity, 1/x); + assertEquals(-Infinity, 1/y); + a[x] = 5; + a[y] = 27; + assertEquals(27, a[x]); + assertEquals(27, a[y]); +} + +// Lars: See above +//TestTypedArraysWithIllegalIndicesStrict(); + +// DataView +function TestDataViewConstructor() { + var ab = new ArrayBuffer(256); + + var d1 = new DataView(ab, 1, 255); + assertTrue(ArrayBuffer.isView(d1)); + assertSame(ab, d1.buffer); + assertSame(1, d1.byteOffset); + assertSame(255, d1.byteLength); + + var d2 = new DataView(ab, 2); + assertSame(ab, d2.buffer); + assertSame(2, d2.byteOffset); + assertSame(254, d2.byteLength); + + var d3 = new DataView(ab); + assertSame(ab, d3.buffer); + assertSame(0, d3.byteOffset); + assertSame(256, d3.byteLength); + + var d3a = new DataView(ab, 1, 0); + assertSame(ab, d3a.buffer); + assertSame(1, d3a.byteOffset); + assertSame(0, d3a.byteLength); + + var d3b = new DataView(ab, 256, 0); + assertSame(ab, d3b.buffer); + assertSame(256, d3b.byteOffset); + assertSame(0, d3b.byteLength); + + var d3c = new DataView(ab, 256); + assertSame(ab, d3c.buffer); + assertSame(256, d3c.byteOffset); + assertSame(0, d3c.byteLength); + + /* This is wrong according to ecma 6 and should throw: + + var d4 = new DataView(ab, 1, 3.1415926); + assertSame(ab, d4.buffer); + assertSame(1, d4.byteOffset); + assertSame(3, d4.byteLength); + */ + assertThrows(function() { new DataView(ab, 3.1415926); }, RangeError); + + // error cases + assertThrows(function() { new DataView(ab, -1); }, RangeError); + assertThrows(function() { new DataView(ab, 1, -1); }, RangeError); + assertThrows(function() { new DataView(); }, TypeError); + assertThrows(function() { new DataView([]); }, TypeError); + assertThrows(function() { new DataView(ab, 257); }, RangeError); + assertThrows(function() { new DataView(ab, 1, 1024); }, RangeError); +} + +TestDataViewConstructor(); + +function TestDataViewPropertyTypeChecks() { + var a = new DataView(new ArrayBuffer(10)); + function CheckProperty(name) { + var d = Object.getOwnPropertyDescriptor(DataView.prototype, name); + var o = {} + assertThrows(function() {d.get.call(o);}, TypeError); + d.get.call(a); // shouldn't throw + } + + CheckProperty("buffer"); + CheckProperty("byteOffset"); + CheckProperty("byteLength"); +} + + +TestDataViewPropertyTypeChecks(); + +// General tests for properties + +// Test property attribute [[Enumerable]] +function TestEnumerable(func, obj) { + function props(x) { + var array = []; + for (var p in x) array.push(p); + return array.sort(); + } + assertArrayEquals([], props(func)); + assertArrayEquals([], props(func.prototype)); + if (obj) + assertArrayEquals([], props(obj)); +} +TestEnumerable(ArrayBuffer, new ArrayBuffer()); +for(i = 0; i < typedArrayConstructors.length; i++) { + TestEnumerable(typedArrayConstructors[i]); +} +TestEnumerable(DataView, new DataView(new ArrayBuffer())); + +// Test arbitrary properties on ArrayBuffer +function TestArbitrary(m) { + function TestProperty(map, property, value) { + map[property] = value; + assertEquals(value, map[property]); + } + for (var i = 0; i < 20; i++) { + TestProperty(m, 'key' + i, 'val' + i); + TestProperty(m, 'foo' + i, 'bar' + i); + } +} +TestArbitrary(new ArrayBuffer(256)); +for(i = 0; i < typedArrayConstructors.length; i++) { + TestArbitrary(new typedArrayConstructors[i](10)); +} +TestArbitrary(new DataView(new ArrayBuffer(256))); + +/* Lars: Not sure this is correct, as ECMA6 seems to allow calling the constructors as functions. + */ +// Test direct constructor call +//assertThrows(function() { ArrayBuffer(); }, TypeError); +//assertThrows(function() { DataView(new ArrayBuffer()); }, TypeError); |