From 9ffc533ffbba156ff69dc1bd3a9a4a2cb53b68af Mon Sep 17 00:00:00 2001 From: Kent Hansen Date: Thu, 26 Mar 2009 15:02:20 +0100 Subject: Fix regression in qscriptvalue_cast (primitive-->complex type cast) qscriptvalue_cast implementation was changed from 4.4 to 4.5 because of the introduction of QScriptValue constructors that don't take an engine pointer. However, when the old constructors are used, the behavior of qscriptvalue_cast should be as before, which this patch ensures. In short: If we have an engine pointer, use it. Task-number: 248802 Reviewed-by: Ariya Hidayat --- src/script/qscriptengine.cpp | 3 ++- tests/auto/qscriptengine/tst_qscriptengine.cpp | 28 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/script/qscriptengine.cpp b/src/script/qscriptengine.cpp index d4e1923..d8908ed 100644 --- a/src/script/qscriptengine.cpp +++ b/src/script/qscriptengine.cpp @@ -1169,7 +1169,8 @@ bool QScriptEngine::convert(const QScriptValue &value, int type, void *ptr) bool QScriptEngine::convertV2(const QScriptValue &value, int type, void *ptr) { QScriptValueImpl impl = QScriptValuePrivate::valueOf(value); - return QScriptEnginePrivate::convert(impl, type, ptr, /*engine=*/0); + QScriptEnginePrivate *eng_p = QScriptEnginePrivate::get(value.engine()); + return QScriptEnginePrivate::convert(impl, type, ptr, eng_p); } /*! diff --git a/tests/auto/qscriptengine/tst_qscriptengine.cpp b/tests/auto/qscriptengine/tst_qscriptengine.cpp index 9995b46..5339fb4 100644 --- a/tests/auto/qscriptengine/tst_qscriptengine.cpp +++ b/tests/auto/qscriptengine/tst_qscriptengine.cpp @@ -1187,6 +1187,16 @@ static void fooFromScriptValue(const QScriptValue &value, Foo &foo) foo.y = value.property("y").toInt32(); } +static QScriptValue fooToScriptValueV2(QScriptEngine *eng, const Foo &foo) +{ + return QScriptValue(eng, foo.x); +} + +static void fooFromScriptValueV2(const QScriptValue &value, Foo &foo) +{ + foo.x = value.toInt32(); +} + Q_DECLARE_METATYPE(QLinkedList) Q_DECLARE_METATYPE(QList) Q_DECLARE_METATYPE(QVector) @@ -1427,6 +1437,24 @@ void tst_QScriptEngine::valueConversion() QVERIFY(val.isVariant()); QCOMPARE(val.toVariant(), var); } + + // task 248802 + qScriptRegisterMetaType(&eng, fooToScriptValueV2, fooFromScriptValueV2); + { + QScriptValue num(&eng, 123); + Foo foo = qScriptValueToValue(num); + QCOMPARE(foo.x, 123); + } + { + QScriptValue num(123); + Foo foo = qScriptValueToValue(num); + QCOMPARE(foo.x, -1); + } + { + QScriptValue str(&eng, "123"); + Foo foo = qScriptValueToValue(str); + QCOMPARE(foo.x, 123); + } } static QScriptValue __import__(QScriptContext *ctx, QScriptEngine *eng) -- cgit v0.12