diff options
author | Lars Knoll <lars.knoll@qt.io> | 2018-08-24 15:57:31 +0200 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2018-08-26 16:59:33 +0000 |
commit | e4e220fb568e22a4da7e6bd3d28ac34038041759 (patch) | |
tree | 2327058e1997f3b3d73a0313ce96fe5fe7e7c777 /src/qml/jsruntime/qv4atomics.cpp | |
parent | 7bdb46c5aae3da345653fe91fbe9281b39015497 (diff) |
Partial Atomics support
Implement support for basic atomic operations on
SharedArrayBuffers.
Half of the methods still need a proper implementation.
Change-Id: Ia88da358e0c2ae6d28e9bcc1feb8980b6f51a812
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'src/qml/jsruntime/qv4atomics.cpp')
-rw-r--r-- | src/qml/jsruntime/qv4atomics.cpp | 190 |
1 files changed, 190 insertions, 0 deletions
diff --git a/src/qml/jsruntime/qv4atomics.cpp b/src/qml/jsruntime/qv4atomics.cpp new file mode 100644 index 0000000000..21813c0667 --- /dev/null +++ b/src/qml/jsruntime/qv4atomics.cpp @@ -0,0 +1,190 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtQml module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** 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 The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/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 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "qv4arraybuffer_p.h" +#include "qv4typedarray_p.h" +#include "qv4atomics_p.h" +#include "qv4symbol_p.h" + +using namespace QV4; + +DEFINE_OBJECT_VTABLE(Atomics); + +void Heap::Atomics::init() +{ + Object::init(); + Scope scope(internalClass->engine); + ScopedObject m(scope, this); + + m->defineDefaultProperty(QStringLiteral("add"), QV4::Atomics::method_add, 3); + m->defineDefaultProperty(QStringLiteral("and"), QV4::Atomics::method_and, 3); + m->defineDefaultProperty(QStringLiteral("compareExchange"), QV4::Atomics::method_compareExchange, 4); + m->defineDefaultProperty(QStringLiteral("exchange"), QV4::Atomics::method_exchange, 3); + m->defineDefaultProperty(QStringLiteral("isLockFree"), QV4::Atomics::method_isLockFree, 1); + m->defineDefaultProperty(QStringLiteral("load"), QV4::Atomics::method_load, 2); + m->defineDefaultProperty(QStringLiteral("or"), QV4::Atomics::method_or, 3); + m->defineDefaultProperty(QStringLiteral("store"), QV4::Atomics::method_store, 3); + m->defineDefaultProperty(QStringLiteral("sub"), QV4::Atomics::method_sub, 3); + m->defineDefaultProperty(QStringLiteral("wait"), QV4::Atomics::method_wait, 4); + m->defineDefaultProperty(QStringLiteral("wake"), QV4::Atomics::method_wake, 3); + m->defineDefaultProperty(QStringLiteral("xor"), QV4::Atomics::method_xor, 3); + + ScopedString name(scope, scope.engine->newString(QStringLiteral("Atomics"))); + m->defineReadonlyConfigurableProperty(scope.engine->symbol_toStringTag(), name); +} + +static SharedArrayBuffer *validateSharedIntegerTypedArray(Scope &scope, const Value &typedArray, bool onlyInt32 = false) +{ + const TypedArray *a = typedArray.as<TypedArray>(); + if (!a) { + scope.engine->throwTypeError(); + return nullptr; + } + + TypedArrayType t(a->arrayType()); + if (!a->d()->type->atomicLoad || (onlyInt32 && t != TypedArrayType::Int32Array)) { + scope.engine->throwTypeError(); + return nullptr; + } + + Scoped<SharedArrayBuffer> buffer(scope, a->d()->buffer); + if (!buffer->isSharedArrayBuffer()) { + scope.engine->throwTypeError(); + return nullptr; + } + Q_ASSERT(!buffer->isDetachedBuffer()); + return buffer; +} + +static int validateAtomicAccess(Scope &scope, const TypedArray &typedArray, const Value &index) +{ + const TypedArray &a = static_cast<const TypedArray &>(typedArray); + qint64 idx = index.toIndex(); + if (scope.hasException()) + return -1; + if (idx < 0 || idx >= a.length()) { + scope.engine->throwRangeError(QStringLiteral("index out of range.")); + return -1; + } + return static_cast<int>(idx); +} + +ReturnedValue atomicReadModifyWrite(const FunctionObject *f, const Value *argv, int argc, AtomicModifyOps modify) +{ + Scope scope(f); + if (!argc) + return scope.engine->throwTypeError(); + + SharedArrayBuffer *buffer = validateSharedIntegerTypedArray(scope, argv[0]); + if (!buffer) + return Encode::undefined(); + const TypedArray &a = static_cast<const TypedArray &>(argv[0]); + int index = validateAtomicAccess(scope, a, argc > 1 ? argv[1] : Primitive::undefinedValue()); + if (index < 0) + return Encode::undefined(); + + Value v = Primitive::fromReturnedValue((argc > 2 ? argv[2] : Primitive::undefinedValue()).convertedToNumber()); + if (scope.hasException()) + return Encode::undefined(); + + int bytesPerElement = a.d()->type->bytesPerElement; + int byteOffset = a.d()->byteOffset + index * bytesPerElement; + + return a.d()->type->atomicModifyOps[modify](buffer->data() + byteOffset, v); +} + +ReturnedValue Atomics::method_add(const FunctionObject *f, const Value *, const Value *argv, int argc) +{ + return atomicReadModifyWrite(f, argv, argc, AtomicAdd); +} + +ReturnedValue Atomics::method_and(const FunctionObject *f, const Value *, const Value *argv, int argc) +{ + return atomicReadModifyWrite(f, argv, argc, AtomicAnd); +} + +ReturnedValue Atomics::method_compareExchange(const FunctionObject *f, const Value *, const Value *, int) +{ + return f->engine()->throwTypeError(); +} + +ReturnedValue Atomics::method_exchange(const FunctionObject *f, const Value *, const Value *argv, int argc) +{ + return atomicReadModifyWrite(f, argv, argc, AtomicExchange); +} + +ReturnedValue Atomics::method_isLockFree(const FunctionObject *f, const Value *, const Value *, int) +{ + return f->engine()->throwTypeError(); +} + +ReturnedValue Atomics::method_load(const FunctionObject *f, const Value *, const Value *, int) +{ + return f->engine()->throwTypeError(); +} + +ReturnedValue Atomics::method_or(const FunctionObject *f, const Value *, const Value *argv, int argc) +{ + return atomicReadModifyWrite(f, argv, argc, AtomicOr); +} + +ReturnedValue Atomics::method_store(const FunctionObject *f, const Value *, const Value *, int) +{ + return f->engine()->throwTypeError(); +} + +ReturnedValue Atomics::method_sub(const FunctionObject *f, const Value *, const Value *argv, int argc) +{ + return atomicReadModifyWrite(f, argv, argc, AtomicSub); +} + +ReturnedValue Atomics::method_wait(const FunctionObject *f, const Value *, const Value *, int) +{ + return f->engine()->throwTypeError(); +} + +ReturnedValue Atomics::method_wake(const FunctionObject *f, const Value *, const Value *, int) +{ + return f->engine()->throwTypeError(); +} + +ReturnedValue Atomics::method_xor(const FunctionObject *f, const Value *, const Value *argv, int argc) +{ + return atomicReadModifyWrite(f, argv, argc, AtomicXor); + +} |