From ec519529087cc3005d55242569dcbca3dcee91bf Mon Sep 17 00:00:00 2001 From: Glenn Watson Date: Wed, 18 Jul 2012 13:20:00 +1000 Subject: 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 --- src/qml/qml/v8/qv8sequencewrapper.cpp | 44 +++++++++++++++++++++++++++++++-- src/qml/qml/v8/qv8sequencewrapper_p.h | 5 ++++ src/qml/qml/v8/qv8sequencewrapper_p_p.h | 20 +++++++++++++++ 3 files changed, 67 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/qml/qml/v8/qv8sequencewrapper.cpp b/src/qml/qml/v8/qv8sequencewrapper.cpp index 883ed1b60c..6bd72387b5 100644 --- a/src/qml/qml/v8/qv8sequencewrapper.cpp +++ b/src/qml/qml/v8/qv8sequencewrapper.cpp @@ -64,6 +64,22 @@ void QV8SequenceWrapper::init(QV8Engine *engine) m_engine = engine; m_toString = qPersistentNew(v8::FunctionTemplate::New(ToString)->GetFunction()); m_valueOf = qPersistentNew(v8::FunctionTemplate::New(ValueOf)->GetFunction()); + + QString defaultSortString = QLatin1String( + "(function compare(x,y) {" + " if (x === y) return 0;" + " x = x.toString();" + " y = y.toString();" + " if (x == y) return 0;" + " else return x < y ? -1 : 1;" + "})"); + + m_sort = qPersistentNew(v8::FunctionTemplate::New(Sort)->GetFunction()); + m_arrayPrototype = qPersistentNew(v8::Array::New(1)->GetPrototype()->ToObject()->Clone()); + m_arrayPrototype->Set(v8::String::New("sort"), m_sort); + v8::Local defaultSortCompareScript = v8::Script::Compile(engine->toString(defaultSortString)); + m_defaultSortComparer = qPersistentNew(v8::Handle(v8::Function::Cast(*defaultSortCompareScript->Run()))); + v8::Local ft = v8::FunctionTemplate::New(); ft->InstanceTemplate()->SetFallbackPropertyHandler(Getter, Setter); ft->InstanceTemplate()->SetIndexedPropertyHandler(IndexedGetter, IndexedSetter, 0, IndexedDeleter, IndexedEnumerator); @@ -84,6 +100,9 @@ void QV8SequenceWrapper::init(QV8Engine *engine) void QV8SequenceWrapper::destroy() { + qPersistentDispose(m_defaultSortComparer); + qPersistentDispose(m_sort); + qPersistentDispose(m_arrayPrototype); qPersistentDispose(m_toString); qPersistentDispose(m_valueOf); qPersistentDispose(m_constructor); @@ -122,7 +141,7 @@ v8::Local QV8SequenceWrapper::newSequence(int sequenceType, QObject v8::Local rv = m_constructor->NewInstance(); rv->SetExternalResource(r); - rv->SetPrototype(v8::Array::New(1)->GetPrototype()); + rv->SetPrototype(m_arrayPrototype); return rv; } #undef NEW_REFERENCE_SEQUENCE @@ -145,7 +164,7 @@ v8::Local QV8SequenceWrapper::fromVariant(const QVariant& v, bool *s v8::Local rv = m_constructor->NewInstance(); rv->SetExternalResource(r); - rv->SetPrototype(v8::Array::New(1)->GetPrototype()); + rv->SetPrototype(m_arrayPrototype); return rv; } #undef NEW_COPY_SEQUENCE @@ -227,6 +246,27 @@ v8::Handle QV8SequenceWrapper::ValueOfGetter(v8::Local pr return info.Data(); } +v8::Handle QV8SequenceWrapper::Sort(const v8::Arguments &args) +{ + int argCount = args.Length(); + + if (argCount < 2) { + QV8SequenceResource *sr = v8_resource_cast(args.This()); + Q_ASSERT(sr); + + qint32 length = sr->lengthGetter(); + if (length > 1) { + v8::Handle jsCompareFn = sr->engine->sequenceWrapper()->m_defaultSortComparer; + if (argCount == 1 && args[0]->IsFunction()) + jsCompareFn = v8::Handle(v8::Function::Cast(*args[0])); + + sr->sort(jsCompareFn); + } + } + + return args.This(); +} + v8::Handle QV8SequenceWrapper::ToString(const v8::Arguments &args) { QV8SequenceResource *sr = v8_resource_cast(args.This()); diff --git a/src/qml/qml/v8/qv8sequencewrapper_p.h b/src/qml/qml/v8/qv8sequencewrapper_p.h index 104135ff76..08bc6146f7 100644 --- a/src/qml/qml/v8/qv8sequencewrapper_p.h +++ b/src/qml/qml/v8/qv8sequencewrapper_p.h @@ -61,6 +61,7 @@ QT_BEGIN_NAMESPACE class QV8Engine; class QV8ObjectResource; + class QV8SequenceWrapper { public: @@ -85,6 +86,9 @@ private: v8::Persistent m_constructor; v8::Persistent m_toString; v8::Persistent m_valueOf; + v8::Persistent m_sort; + v8::Persistent m_arrayPrototype; + v8::Persistent m_defaultSortComparer; static v8::Handle IndexedGetter(quint32 index, const v8::AccessorInfo &info); static v8::Handle IndexedSetter(quint32 index, v8::Local value, const v8::AccessorInfo &info); @@ -98,6 +102,7 @@ private: static v8::Handle ValueOf(const v8::Arguments &args); static v8::Handle Getter(v8::Local property, const v8::AccessorInfo &info); static v8::Handle Setter(v8::Local property, v8::Local value, const v8::AccessorInfo &info); + static v8::Handle Sort(const v8::Arguments &args); }; 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 indexedDeleter(quint32 index) = 0; virtual v8::Handle indexedEnumerator() = 0; virtual v8::Handle toString() = 0; + virtual void sort(v8::Handle 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 f) : jsFn(f), eng(engine) {} \ + bool operator()(SequenceElementType e0, SequenceElementType e1) \ + { \ + v8::Handle argv[2] = { eng->fromVariant(e0), eng->fromVariant(e1) }; \ + v8::Handle compareValue = jsFn->Call(eng->global(), 2, argv); \ + return compareValue->NumberValue() < 0; \ + } \ + private: \ + v8::Handle jsFn; \ + QV8Engine *eng; \ + }; \ + void sort(v8::Handle jsCompareFunction) \ + { \ + CompareFunctor cf(engine, jsCompareFunction); \ + qSort(c.begin(), c.end(), cf); \ + } \ private: \ QQmlGuard object; \ int propertyIndex; \ -- cgit v1.2.3