aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorValery Kotov <kotov.valery@gmail.com>2015-03-25 20:17:08 +0200
committerSimon Hausmann <simon.hausmann@theqtcompany.com>2015-04-15 09:27:59 +0000
commit175772b3de48f7b24d6ab28255eed28e7cb53eb1 (patch)
treef07ff961a34f1c0db9d32a406aa70d65c493d96b /src
parent171c318a2b6593479cdd78023804c90fb291708a (diff)
QML Engine: Share data for ArrayBuffer created from QByteArray.
ExecutionEngine performs shallow copy of internal data for ArrayBuffer created from QByteArray. Change-Id: I514cd9708a7fbe9a989937fac62d00b464d7362d Reviewed-by: Simon Hausmann <simon.hausmann@theqtcompany.com> Reviewed-by: Valery Kotov <kotov.valery@gmail.com> Reviewed-by: Pasi Keränen <pasi.keranen@digia.com>
Diffstat (limited to 'src')
-rw-r--r--src/qml/jsruntime/qv4arraybuffer.cpp26
-rw-r--r--src/qml/jsruntime/qv4arraybuffer_p.h13
-rw-r--r--src/qml/jsruntime/qv4engine.cpp5
3 files changed, 32 insertions, 12 deletions
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<QByteArray&>(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<char> *oldData = d()->data;
+
+ d()->data = QTypedArrayData<char>::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<char>::deallocate(oldData);
+}
+
+
void ArrayBufferPrototype::init(ExecutionEngine *engine, Object *ctor)
{
Scope scope(engine);
diff --git a/src/qml/jsruntime/qv4arraybuffer_p.h b/src/qml/jsruntime/qv4arraybuffer_p.h
index 9e19a83f1e..fe3150618d 100644
--- a/src/qml/jsruntime/qv4arraybuffer_p.h
+++ b/src/qml/jsruntime/qv4arraybuffer_p.h
@@ -48,6 +48,7 @@ struct ArrayBufferCtor : FunctionObject {
struct Q_QML_PRIVATE_EXPORT ArrayBuffer : Object {
ArrayBuffer(ExecutionEngine *e, size_t length);
+ ArrayBuffer(ExecutionEngine *e, const QByteArray& array);
~ArrayBuffer();
QTypedArrayData<char> *data;
@@ -74,15 +75,11 @@ struct Q_QML_PRIVATE_EXPORT ArrayBuffer : Object
QByteArray asByteArray() const;
uint byteLength() const { return d()->byteLength(); }
- char *data() {
- // ### detach if refcount > 1
- return d()->data->data();
- }
- const char *constData() {
- // ### detach if refcount > 1
- return d()->data->data();
- }
+ char *data() { detach(); return d()->data ? d()->data->data() : 0; }
+ const char *constData() { detach(); return d()->data ? d()->data->data() : 0; }
+private:
+ void detach();
};
struct ArrayBufferPrototype: Object
diff --git a/src/qml/jsruntime/qv4engine.cpp b/src/qml/jsruntime/qv4engine.cpp
index eecf874e19..8c0df3ef04 100644
--- a/src/qml/jsruntime/qv4engine.cpp
+++ b/src/qml/jsruntime/qv4engine.cpp
@@ -585,10 +585,7 @@ Heap::ArrayObject *ExecutionEngine::newArrayObject(InternalClass *ic, Object *pr
Heap::ArrayBuffer *ExecutionEngine::newArrayBuffer(const QByteArray &array)
{
Scope scope(this);
- Scoped<ArrayBuffer> object(scope, memoryManager->alloc<ArrayBuffer>(this, array.size()));
- if (!hasException) {
- memcpy(object->d()->data->data(), array.data(), array.size());
- }
+ Scoped<ArrayBuffer> object(scope, memoryManager->alloc<ArrayBuffer>(this, array));
return object->d();
}