From 63bf6ac4c483cc64b48c410c6e1afb404f2bcbd1 Mon Sep 17 00:00:00 2001 From: Maximilian Goldstein Date: Wed, 1 Apr 2020 08:06:30 +0200 Subject: [PATCH] Throw an error if an incompatible parameter is passed to a C++ function [ChangeLog][Important Behavior Changes][QML] Throw an error if an incompatible parameter is passed to a C++ function Change-Id: I088e362869f7dc00ca639a0fbc4ba20cb9e82f7d Reviewed-by: Fabian Kosmale Reviewed-by: Ulf Hermann --- src/qml/jsruntime/qv4qobjectwrapper.cpp | 13 ++++-- .../qml/qqmlecmascript/tst_qqmlecmascript.cpp | 42 ++++++++----------- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/src/qml/jsruntime/qv4qobjectwrapper.cpp b/src/qml/jsruntime/qv4qobjectwrapper.cpp index ea809064a3..720324ea42 100644 --- a/src/qml/jsruntime/qv4qobjectwrapper.cpp +++ b/src/qml/jsruntime/qv4qobjectwrapper.cpp @@ -1298,11 +1298,16 @@ static QV4::ReturnedValue CallMethod(const QQmlObjectOrGadget &object, int index : QString()); } - qWarning() << QLatin1String("Passing incompatible arguments to C++ functions from " - "JavaScript is dangerous and deprecated."); - qWarning() << QLatin1String("This will throw a JavaScript TypeError in future " - "releases of Qt!"); + const bool is_signal = + object.metaObject()->method(index).methodType() == QMetaMethod::Signal; + if (is_signal) { + qWarning() << "Passing incomatible arguments to signals is not supported."; + } else { + return engine->throwTypeError( + "Passing incompatible arguments to C++ functions from " + "JavaScript is not allowed."); + } } } QVarLengthArray argData(args.count()); diff --git a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp index 5dfe76c2f2..2a23d7ec60 100644 --- a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp +++ b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp @@ -2906,32 +2906,28 @@ void tst_qqmlecmascript::callQtInvokables() QCOMPARE(o->actuals().at(0), QVariant(QString())); o->reset(); - QVERIFY(EVALUATE_VALUE("object.method_QPointF(0)", QV4::Primitive::undefinedValue())); + QVERIFY(EVALUATE_ERROR("object.method_QPointF(0)")); QCOMPARE(o->error(), false); - QCOMPARE(o->invoked(), 12); - QCOMPARE(o->actuals().count(), 1); - QCOMPARE(o->actuals().at(0), QVariant(QPointF())); + QCOMPARE(o->invoked(), -1); + QCOMPARE(o->actuals().count(), 0); o->reset(); - QVERIFY(EVALUATE_VALUE("object.method_QPointF(null)", QV4::Primitive::undefinedValue())); + QVERIFY(EVALUATE_ERROR("object.method_QPointF(null)")); QCOMPARE(o->error(), false); - QCOMPARE(o->invoked(), 12); - QCOMPARE(o->actuals().count(), 1); - QCOMPARE(o->actuals().at(0), QVariant(QPointF())); + QCOMPARE(o->invoked(), -1); + QCOMPARE(o->actuals().count(), 0); o->reset(); - QVERIFY(EVALUATE_VALUE("object.method_QPointF(undefined)", QV4::Primitive::undefinedValue())); + QVERIFY(EVALUATE_ERROR("object.method_QPointF(undefined)")); QCOMPARE(o->error(), false); - QCOMPARE(o->invoked(), 12); - QCOMPARE(o->actuals().count(), 1); - QCOMPARE(o->actuals().at(0), QVariant(QPointF())); + QCOMPARE(o->invoked(), -1); + QCOMPARE(o->actuals().count(), 0); o->reset(); - QVERIFY(EVALUATE_VALUE("object.method_QPointF(object)", QV4::Primitive::undefinedValue())); + QVERIFY(EVALUATE_ERROR("object.method_QPointF(object)")); QCOMPARE(o->error(), false); - QCOMPARE(o->invoked(), 12); - QCOMPARE(o->actuals().count(), 1); - QCOMPARE(o->actuals().at(0), QVariant(QPointF())); + QCOMPARE(o->invoked(), -1); + QCOMPARE(o->actuals().count(), 0); o->reset(); QVERIFY(EVALUATE_VALUE("object.method_QPointF(object.method_get_QPointF())", QV4::Primitive::undefinedValue())); @@ -2948,18 +2944,16 @@ void tst_qqmlecmascript::callQtInvokables() QCOMPARE(o->actuals().at(0), QVariant(QPointF(9, 12))); o->reset(); - QVERIFY(EVALUATE_VALUE("object.method_QObject(0)", QV4::Primitive::undefinedValue())); + QVERIFY(EVALUATE_ERROR("object.method_QObject(0)")); QCOMPARE(o->error(), false); - QCOMPARE(o->invoked(), 13); - QCOMPARE(o->actuals().count(), 1); - QCOMPARE(o->actuals().at(0), QVariant::fromValue((QObject *)nullptr)); + QCOMPARE(o->invoked(), -1); + QCOMPARE(o->actuals().count(), 0); o->reset(); - QVERIFY(EVALUATE_VALUE("object.method_QObject(\"Hello world\")", QV4::Primitive::undefinedValue())); + QVERIFY(EVALUATE_ERROR("object.method_QObject(\"Hello world\")")); QCOMPARE(o->error(), false); - QCOMPARE(o->invoked(), 13); - QCOMPARE(o->actuals().count(), 1); - QCOMPARE(o->actuals().at(0), QVariant::fromValue((QObject *)nullptr)); + QCOMPARE(o->invoked(), -1); + QCOMPARE(o->actuals().count(), 0); o->reset(); QVERIFY(EVALUATE_VALUE("object.method_QObject(null)", QV4::Primitive::undefinedValue()));