From 175772b3de48f7b24d6ab28255eed28e7cb53eb1 Mon Sep 17 00:00:00 2001 From: Valery Kotov Date: Wed, 25 Mar 2015 20:17:08 +0200 Subject: QML Engine: Share data for ArrayBuffer created from QByteArray. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ExecutionEngine performs shallow copy of internal data for ArrayBuffer created from QByteArray. Change-Id: I514cd9708a7fbe9a989937fac62d00b464d7362d Reviewed-by: Simon Hausmann Reviewed-by: Valery Kotov Reviewed-by: Pasi Keränen --- src/qml/jsruntime/qv4arraybuffer.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'src/qml/jsruntime/qv4arraybuffer.cpp') diff --git a/src/qml/jsruntime/qv4arraybuffer.cpp b/src/qml/jsruntime/qv4arraybuffer.cpp index e42fcdc4fd..11664f1194 100644 --- a/src/qml/jsruntime/qv4arraybuffer.cpp +++ b/src/qml/jsruntime/qv4arraybuffer.cpp @@ -95,6 +95,13 @@ Heap::ArrayBuffer::ArrayBuffer(ExecutionEngine *e, size_t length) memset(data->data(), 0, length + 1); } +Heap::ArrayBuffer::ArrayBuffer(ExecutionEngine *e, const QByteArray& array) + : Heap::Object(e->emptyClass, e->arrayBufferPrototype.asObject()) + , data(const_cast(array).data_ptr()) +{ + data->ref.ref(); +} + Heap::ArrayBuffer::~ArrayBuffer() { if (!data->ref.deref()) @@ -108,6 +115,25 @@ QByteArray ArrayBuffer::asByteArray() const return QByteArray(ba); } +void ArrayBuffer::detach() { + if (!d()->data->ref.isShared()) + return; + + QTypedArrayData *oldData = d()->data; + + d()->data = QTypedArrayData::allocate(oldData->size + 1); + if (!d()->data) { + engine()->throwRangeError(QStringLiteral("ArrayBuffer: out of memory")); + return; + } + + memcpy(d()->data->data(), oldData->data(), oldData->size + 1); + + if (!oldData->ref.deref()) + QTypedArrayData::deallocate(oldData); +} + + void ArrayBufferPrototype::init(ExecutionEngine *engine, Object *ctor) { Scope scope(engine); -- cgit v1.2.1