aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMitch Curtis <mitch.curtis@qt.io>2018-04-23 14:14:56 +0200
committerMitch Curtis <mitch.curtis@qt.io>2018-05-23 08:28:53 +0000
commit20b376b64a11b4bda18aa1abda543586f04688e8 (patch)
tree21760aed4cd4dde56718b2403d46032eef00105f
parent7c0a9a08f7b078285411269dc740922e06625901 (diff)
Print function identifier when calling toString() on a function
Task-number: QTBUG-50669 Change-Id: Ia0e9491b12ca1db1da2bfa8eb5ca8bbe4658f699 Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
-rw-r--r--src/qml/jsruntime/qv4functionobject.cpp14
-rw-r--r--tests/auto/qml/qjsengine/tst_qjsengine.cpp27
-rw-r--r--tests/auto/qml/qqmlconsole/tst_qqmlconsole.cpp2
3 files changed, 41 insertions, 2 deletions
diff --git a/src/qml/jsruntime/qv4functionobject.cpp b/src/qml/jsruntime/qv4functionobject.cpp
index 8a70715728..1eb56b9c2a 100644
--- a/src/qml/jsruntime/qv4functionobject.cpp
+++ b/src/qml/jsruntime/qv4functionobject.cpp
@@ -311,7 +311,19 @@ ReturnedValue FunctionPrototype::method_toString(const FunctionObject *b, const
if (!fun)
return v4->throwTypeError();
- return Encode(v4->newString(QStringLiteral("function() { [code] }")));
+ const Scope scope(fun->engine());
+ const ScopedString scopedFunctionName(scope, fun->name());
+ const QString functionName(scopedFunctionName ? scopedFunctionName->toQString() : QString());
+ QString functionAsString = QStringLiteral("function");
+
+ // If fun->name() is empty, then there is no function name
+ // to append because the function is anonymous.
+ if (!functionName.isEmpty())
+ functionAsString.append(QLatin1Char(' ') + functionName);
+
+ functionAsString.append(QStringLiteral("() { [code] }"));
+
+ return Encode(v4->newString(functionAsString));
}
ReturnedValue FunctionPrototype::method_apply(const QV4::FunctionObject *b, const Value *thisObject, const Value *argv, int argc)
diff --git a/tests/auto/qml/qjsengine/tst_qjsengine.cpp b/tests/auto/qml/qjsengine/tst_qjsengine.cpp
index 85b4484403..911da1b20c 100644
--- a/tests/auto/qml/qjsengine/tst_qjsengine.cpp
+++ b/tests/auto/qml/qjsengine/tst_qjsengine.cpp
@@ -212,6 +212,9 @@ private slots:
void deleteInsideForIn();
+ void functionToString_data();
+ void functionToString();
+
signals:
void testSignal();
};
@@ -4220,6 +4223,30 @@ void tst_QJSEngine::deleteInsideForIn()
QCOMPARE(iterationCount.toInt(), 4);
}
+void tst_QJSEngine::functionToString_data()
+{
+ QTest::addColumn<QString>("source");
+ QTest::addColumn<QString>("expectedString");
+
+ QTest::newRow("named function") << QString::fromLatin1("function f() {}; f.toString()")
+ << QString::fromLatin1("function f() { [code] }");
+ QTest::newRow("anonymous function") << QString::fromLatin1("(function() {}).toString()")
+ << QString::fromLatin1("function() { [code] }");
+}
+
+// Tests that function.toString() prints the function's name.
+void tst_QJSEngine::functionToString()
+{
+ QFETCH(QString, source);
+ QFETCH(QString, expectedString);
+
+ QJSEngine engine;
+ engine.installExtensions(QJSEngine::AllExtensions);
+ QJSValue evaluationResult = engine.evaluate(source);
+ QVERIFY(!evaluationResult.isError());
+ QCOMPARE(evaluationResult.toString(), expectedString);
+}
+
QTEST_MAIN(tst_QJSEngine)
#include "tst_qjsengine.moc"
diff --git a/tests/auto/qml/qqmlconsole/tst_qqmlconsole.cpp b/tests/auto/qml/qqmlconsole/tst_qqmlconsole.cpp
index 5391e19f50..40a9295e50 100644
--- a/tests/auto/qml/qqmlconsole/tst_qqmlconsole.cpp
+++ b/tests/auto/qml/qqmlconsole/tst_qqmlconsole.cpp
@@ -75,7 +75,7 @@ void tst_qqmlconsole::logging()
QTest::ignoreMessage(QtDebugMsg, "{\"a\":\"hello\",\"d\":1}");
QTest::ignoreMessage(QtDebugMsg, "undefined");
QTest::ignoreMessage(QtDebugMsg, "12");
- QTest::ignoreMessage(QtDebugMsg, "function() { [code] }");
+ QTest::ignoreMessage(QtDebugMsg, "function e() { [code] }");
QTest::ignoreMessage(QtDebugMsg, "true");
// Printing QML object prints out the class/type of QML object with the memory address
// QTest::ignoreMessage(QtDebugMsg, "QtObject_QML_0(0xABCD..)");