aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/qml/v8/qv8sequencewrapper_p_p.h
diff options
context:
space:
mode:
authorGlenn Watson <glenn.watson@nokia.com>2012-07-18 13:20:00 +1000
committerQt by Nokia <qt-info@nokia.com>2012-08-08 00:34:22 +0200
commitec519529087cc3005d55242569dcbca3dcee91bf (patch)
tree9fb83a2396c62e5231b11aaff013e94ed7f239dc /src/qml/qml/v8/qv8sequencewrapper_p_p.h
parent8bb5677f45935a03f1ed439e8a01ca71e9f1152c (diff)
Support JS Array.sort() function for sequence wrappers.
The V8 natve sort implementation calls some functions that are incompatible with the way sequence wrappers work. In particular, it calls an internal length() function which does not pass through the length accessor provided by sequence wrappers, so the sort function always thinks the array is zero length. Instead, clone the array prototype and override the sort function with one that is specific to sequence wrappers. Task-number: QTBUG-25269 Change-Id: Ic83b9ee0bd3a0707e512f28057f0f99b432fded4 Reviewed-by: Matthew Vogt <matthew.vogt@nokia.com>
Diffstat (limited to 'src/qml/qml/v8/qv8sequencewrapper_p_p.h')
-rw-r--r--src/qml/qml/v8/qv8sequencewrapper_p_p.h20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/qml/qml/v8/qv8sequencewrapper_p_p.h b/src/qml/qml/v8/qv8sequencewrapper_p_p.h
index cf20aa39fd..e74a5849cc 100644
--- a/src/qml/qml/v8/qv8sequencewrapper_p_p.h
+++ b/src/qml/qml/v8/qv8sequencewrapper_p_p.h
@@ -88,6 +88,7 @@ public:
virtual v8::Handle<v8::Boolean> indexedDeleter(quint32 index) = 0;
virtual v8::Handle<v8::Array> indexedEnumerator() = 0;
virtual v8::Handle<v8::Value> toString() = 0;
+ virtual void sort(v8::Handle<v8::Function> comparer) = 0;
ObjectType objectType;
QByteArray typeName;
@@ -474,6 +475,25 @@ static QString convertUrlToString(QV8Engine *, const QUrl &v)
void *a[] = { &c, 0, &status, &flags }; \
QMetaObject::metacall(object, QMetaObject::WriteProperty, propertyIndex, a); \
} \
+ class CompareFunctor \
+ { \
+ public: \
+ CompareFunctor(QV8Engine *engine, v8::Handle<v8::Function> f) : jsFn(f), eng(engine) {} \
+ bool operator()(SequenceElementType e0, SequenceElementType e1) \
+ { \
+ v8::Handle<v8::Value> argv[2] = { eng->fromVariant(e0), eng->fromVariant(e1) }; \
+ v8::Handle<v8::Value> compareValue = jsFn->Call(eng->global(), 2, argv); \
+ return compareValue->NumberValue() < 0; \
+ } \
+ private: \
+ v8::Handle<v8::Function> jsFn; \
+ QV8Engine *eng; \
+ }; \
+ void sort(v8::Handle<v8::Function> jsCompareFunction) \
+ { \
+ CompareFunctor cf(engine, jsCompareFunction); \
+ qSort(c.begin(), c.end(), cf); \
+ } \
private: \
QQmlGuard<QObject> object; \
int propertyIndex; \