aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/jsruntime/qv4typedarray.cpp
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@digia.com>2014-09-11 15:37:31 +0200
committerSimon Hausmann <simon.hausmann@digia.com>2014-10-29 09:07:36 +0100
commita3a44fc16417edfe09fca757f4ab1fb8fdaff00d (patch)
treecce2b9d011b1e70736af2dab901247d9dcb2244e /src/qml/jsruntime/qv4typedarray.cpp
parent868478e92afaa9d0823f3a65ff3d7b44216087ea (diff)
Basic support for typed arrays
This implements most of the spec required for the Khronos typed array specification. It tries to follow ECMAScript 6 as closely as possible, but currently only implements a subset of the ECMAScript 6 specification. Addes a test script in tests/manual/v4 to test our implementation. Change-Id: I8ec63869500358e088b73240e1f37120ae3cf59a Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
Diffstat (limited to 'src/qml/jsruntime/qv4typedarray.cpp')
-rw-r--r--src/qml/jsruntime/qv4typedarray.cpp571
1 files changed, 571 insertions, 0 deletions
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);
+}