diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2020-12-11 13:35:53 +0100 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2020-12-18 17:26:51 +0100 |
commit | 6f181768a3147bbfa9a33cf2c05453365693f5b9 (patch) | |
tree | 3ff80238b5784032c86cfc1a70088e17b62a7127 /src/qml/jsapi/qjsvalue_p.h | |
parent | d5ac54da624dbaebc865c8243a5e1c33d5e1c7ba (diff) |
Add a QJSManagedValue
A QJSManagedValue is a view on a QJSValue which always knows the engine
the value belongs to. This allows us to implement the JavaScript
semantics of the various QJSValue methods in a much more rigorous way.
[ChangeLog][QtQml] The new QJSManagedValue should be used instead of
QJSValue for manipulating properties and prototypes of JavaScript
values, as well as for calling JavaScript functions.
Change-Id: I9d445ffcf68dfa72dba9bae0818e83c80665ad66
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/qml/jsapi/qjsvalue_p.h')
-rw-r--r-- | src/qml/jsapi/qjsvalue_p.h | 29 |
1 files changed, 26 insertions, 3 deletions
diff --git a/src/qml/jsapi/qjsvalue_p.h b/src/qml/jsapi/qjsvalue_p.h index 9e371118cc..cea48fd9e2 100644 --- a/src/qml/jsapi/qjsvalue_p.h +++ b/src/qml/jsapi/qjsvalue_p.h @@ -83,6 +83,12 @@ class Q_AUTOTEST_EXPORT QJSValuePrivate return (m & IsString) ? nullptr : reinterpret_cast<QV4::Value *>(m); } + static QV4::Value *managedValue(QV4::Value &v) + { + quintptr m = quintptr(v.m()); + return (m & IsString) ? nullptr : reinterpret_cast<QV4::Value *>(m); + } + static const QString *qstring(const QV4::Value &v) { const quintptr m = quintptr(v.m()); @@ -108,14 +114,13 @@ class Q_AUTOTEST_EXPORT QJSValuePrivate return QV4::Value::fromHeapObject(reinterpret_cast<QV4::Heap::Base *>(m)).asReturnedValue(); } -protected: - // Only for the test. You're not supposed to subclass QJSValuePrivate otherwise. +public: + static void setRawValue(QJSValue *jsval, QV4::Value *m) { jsval->d = encodeRawValue(quintptr(m)); } -public: static QJSValue fromReturnedValue(QV4::ReturnedValue d) { QJSValue result; @@ -134,6 +139,19 @@ public: return nullptr; } + // This is a move operation and transfers ownership. + static QV4::Value *takeManagedValue(QJSValue *jsval) + { + QV4::Value v = QV4::Value::fromReturnedValue(jsval->d); + if (!v.isManaged()) + return nullptr; + if (QV4::Value *value = managedValue(v)) { + setValue(jsval, QV4::Encode::undefined()); + return value; + } + return nullptr; + } + static QV4::ReturnedValue asPrimitiveType(const QJSValue *jsval) { const QV4::Value v = QV4::Value::fromReturnedValue(jsval->d); @@ -170,6 +188,11 @@ public: jsval->d = v.isManaged() ? encode(v) : v.asReturnedValue(); } + static void adoptValue(QJSValue *jsval, QV4::Value *v) + { + jsval->d = v->isManaged() ? encodeRawValue(quintptr(v)) : v->asReturnedValue(); + } + // Moves any QString onto the V4 heap, changing the value to reflect that. static void manageStringOnV4Heap(QV4::ExecutionEngine *e, QJSValue *jsval) { |