aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/jsruntime/qv4arraybuffer.cpp
diff options
context:
space:
mode:
authorSimon Hausmann <simon.hausmann@qt.io>2016-08-29 14:18:31 +0200
committerSimon Hausmann <simon.hausmann@qt.io>2016-08-30 13:48:22 +0000
commite3b277d5ccac5b9a5cdcbb292fb04d84d44c7869 (patch)
treefec7c8ec16399685ea5015edacb19f9552668f0d /src/qml/jsruntime/qv4arraybuffer.cpp
parent12d8004874c3f69ddd5aa7622da309c46930336b (diff)
Fix conversion of QByteArray back to String in JavaScript
Commit 3b7e2a69f7eb8597c807de39b4de39721e9e2bd2 changed behavior so that QByteArray is converted to the JS ArrayBuffer type, which is a better fit than QVariant. However ArrayBuffer does not have a toString method in the spec, and therefore any previous implicit toString conversion such as when passing to JSON.parse() would fail. To restore compatibility this patch adds a non-spec toString() that performs an UTF-8 conversion, as it was done previously through QVariant's toString. Task-number: QTBUG-55562 Change-Id: I096046954f7b29f7258deaa9ef5c8fa9292552ef Reviewed-by: Lars Knoll <lars.knoll@qt.io> Reviewed-by: J-P Nurmi <jpnurmi@qt.io>
Diffstat (limited to 'src/qml/jsruntime/qv4arraybuffer.cpp')
-rw-r--r--src/qml/jsruntime/qv4arraybuffer.cpp10
1 files changed, 10 insertions, 0 deletions
diff --git a/src/qml/jsruntime/qv4arraybuffer.cpp b/src/qml/jsruntime/qv4arraybuffer.cpp
index e006773c9e..34c7746684 100644
--- a/src/qml/jsruntime/qv4arraybuffer.cpp
+++ b/src/qml/jsruntime/qv4arraybuffer.cpp
@@ -154,6 +154,7 @@ void ArrayBufferPrototype::init(ExecutionEngine *engine, Object *ctor)
defineDefaultProperty(engine->id_constructor(), (o = ctor));
defineAccessorProperty(QStringLiteral("byteLength"), method_get_byteLength, 0);
defineDefaultProperty(QStringLiteral("slice"), method_slice, 2);
+ defineDefaultProperty(QStringLiteral("toString"), method_toString, 0);
}
ReturnedValue ArrayBufferPrototype::method_get_byteLength(CallContext *ctx)
@@ -198,3 +199,12 @@ ReturnedValue ArrayBufferPrototype::method_slice(CallContext *ctx)
return newBuffer.asReturnedValue();
}
+
+ReturnedValue ArrayBufferPrototype::method_toString(CallContext *ctx)
+{
+ Scope scope(ctx);
+ Scoped<ArrayBuffer> a(scope, ctx->thisObject());
+ if (!a)
+ return Encode::undefined();
+ return Encode(ctx->engine()->newString(QString::fromUtf8(a->asByteArray())));
+}