diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2022-11-15 10:13:09 +0100 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2022-11-15 14:26:17 +0100 |
commit | 7b29ed6f64a300d4d714371c2872fcba344beebd (patch) | |
tree | d2e83316030d03a05317958e22770bb784ffcbe0 /src/qml/jsapi/qjsengine.cpp | |
parent | d9d247746423f6a9f1f36aa9be14fad1edcd6616 (diff) |
QJSEngine: Provide a method to coerce values in JS fashion
JavaScript has its own type coercion rules. We already have a methods
that coerce QVariants, QJSValues and QJSManagedValues to specific types.
The new method is a generalization of all of those and can coerce
everything to everything (as far as JavaScript can).
Change-Id: I9b6877fb40f67b6f2354781bbd4cf18cf996c7b0
Reviewed-by: Sami Shalayel <sami.shalayel@qt.io>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/qml/jsapi/qjsengine.cpp')
-rw-r--r-- | src/qml/jsapi/qjsengine.cpp | 31 |
1 files changed, 24 insertions, 7 deletions
diff --git a/src/qml/jsapi/qjsengine.cpp b/src/qml/jsapi/qjsengine.cpp index 61042607c9..98df83fd99 100644 --- a/src/qml/jsapi/qjsengine.cpp +++ b/src/qml/jsapi/qjsengine.cpp @@ -833,7 +833,7 @@ bool QJSEngine::convertV2(const QJSValue &value, int type, void *ptr) return convertV2(value, QMetaType(type), ptr); } -static bool convertString(const QString &string, QMetaType metaType, void *ptr) +bool QJSEngine::convertString(const QString &string, QMetaType metaType, void *ptr) { // have a string based value without engine. Do conversion manually if (metaType == QMetaType::fromType<bool>()) { @@ -906,20 +906,24 @@ bool QJSEngine::convertV2(const QJSValue &value, QMetaType metaType, void *ptr) bool QJSEngine::convertVariant(const QVariant &value, QMetaType metaType, void *ptr) { - if (value.metaType() == QMetaType::fromType<QString>()) - return convertString(value.toString(), metaType, ptr); - // TODO: We could probably avoid creating a QV4::Value in many cases, but we'd have to // duplicate much of metaTypeFromJS and some methods of QV4::Value itself here. return QV4::ExecutionEngine::metaTypeFromJS(handle()->fromVariant(value), metaType, ptr); } +bool QJSEngine::convertMetaType(QMetaType fromType, const void *from, QMetaType toType, void *to) +{ + // TODO: We could probably avoid creating a QV4::Value in many cases, but we'd have to + // duplicate much of metaTypeFromJS and some methods of QV4::Value itself here. + return QV4::ExecutionEngine::metaTypeFromJS(handle()->fromData(fromType, from), toType, to); +} + /*! \fn template <typename T> QJSValue QJSEngine::toScriptValue(const T &value) Creates a QJSValue with the given \a value. This works with any type \c{T} that has a \c{QMetaType}. - \sa fromScriptValue() + \sa fromScriptValue(), coerceValue() */ /*! \fn template <typename T> T QJSEngine::fromScriptValue(const QJSValue &value) @@ -927,7 +931,7 @@ bool QJSEngine::convertVariant(const QVariant &value, QMetaType metaType, void * Returns the given \a value converted to the template type \c{T}. This works with any type \c{T} that has a \c{QMetaType}. - \sa toScriptValue() + \sa toScriptValue(), coerceValue() */ /*! \fn template <typename T> T QJSEngine::fromVariant(const QVariant &value) @@ -939,7 +943,20 @@ bool QJSEngine::convertVariant(const QVariant &value, QMetaType metaType, void * conversions between JavaScript-equivalent types that are not performed by qvariant_cast by default. - \sa fromScriptValue() qvariant_cast() + \sa coerceValue(), fromScriptValue(), qvariant_cast() +*/ + +/*! \fn template <typename From, typename To> T QJSEngine::coerceValue(const From &from) + + Returns the given \a from converted to the template type \c{To}. + This works with any type \c{T} that has a \c{QMetaType}. The + conversion is done in JavaScript semantics. Those differ from + qvariant_cast's semantics. There are a number of implicit + conversions between JavaScript-equivalent types that are not + performed by qvariant_cast by default. This method is a generalization of + all the other conversion methods in this class. + + \sa fromVariant(), qvariant_cast(), fromScriptValue(), toScriptValue() */ /*! |