aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/jsruntime/qv4engine.cpp
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2022-11-14 16:27:50 +0100
committerUlf Hermann <ulf.hermann@qt.io>2022-11-15 08:33:19 +0100
commit1fc939a870ce4520688fcf92948851fa06c6926a (patch)
tree276a95a5f7430df06e2f28fa6c45d5015df0922d /src/qml/jsruntime/qv4engine.cpp
parent1166ebf3e9493b0be7e72aaeb72362ec666bc8d6 (diff)
QML: Faithfully convert undefined and null to string
If you do that in JS you get "undefined" and "null", respectively. Our C++-based conversion methods should do the same. The documentation for QJSValue also suggests that QJSValue::toFoo() should behave like qjsvalue_cast<Foo>(x). So far QJSValue::toString() produced "undefined" and "null" while qjsvalue_cast<String>(x) produced an empty string. [ChangeLog][QtQml][Important Behavior Changes] qjsvalue_cast<QString>(x) now returns "undefined" for undefined JS values, and "null" for null JS values. This is in line with what QJSValue::toString() does, and also what JavaScript itself would produce when stringifying such values. Previously, qjsvalue_cast would return an empty string for both, undefined and null JS values. Change-Id: Ib93f4157f092ed769dca946541ffbcfbd7317d4c Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/qml/jsruntime/qv4engine.cpp')
-rw-r--r--src/qml/jsruntime/qv4engine.cpp6
1 files changed, 4 insertions, 2 deletions
diff --git a/src/qml/jsruntime/qv4engine.cpp b/src/qml/jsruntime/qv4engine.cpp
index ad718e32cb..49afc77705 100644
--- a/src/qml/jsruntime/qv4engine.cpp
+++ b/src/qml/jsruntime/qv4engine.cpp
@@ -2401,8 +2401,10 @@ bool ExecutionEngine::metaTypeFromJS(const Value &value, QMetaType metaType, voi
*reinterpret_cast<double*>(data) = value.toNumber();
return true;
case QMetaType::QString:
- if (value.isUndefined() || value.isNull())
- *reinterpret_cast<QString*>(data) = QString();
+ if (value.isUndefined())
+ *reinterpret_cast<QString*>(data) = QStringLiteral("undefined");
+ else if (value.isNull())
+ *reinterpret_cast<QString*>(data) = QStringLiteral("null");
else
*reinterpret_cast<QString*>(data) = value.toQString();
return true;