aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/jsapi
diff options
context:
space:
mode:
authorFabian Kosmale <fabian.kosmale@qt.io>2020-04-24 10:06:19 +0200
committerFabian Kosmale <fabian.kosmale@qt.io>2020-04-24 11:46:49 +0200
commit1f39b8ba2745f2b9ff462d40f6b0ca473c94484e (patch)
treeeee4831d3dd961680edddcef3ac78423fc77d8c1 /src/qml/jsapi
parentb5fa1411a0109c332ffc7c3dbe9145f7ecc2c9a7 (diff)
Move object onwership functions from QQmlEngine to QJSEngine
[ChangeLog][QML] The setObjectOwnership and objectOwnership functions have been moved from QQmlEngine to QJSEngine. This reflects the fact that they have an effect on objects passed not only to a QQmlEngine, but also to a QJSEngine. As QQmlEngine is a subclass of QJSEngine, existing code continues to work. Fixes: QTBUG-83657 Change-Id: Ib29cdb4a9884e1f1b68fcba64d32b1cb2e5a4297 Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'src/qml/jsapi')
-rw-r--r--src/qml/jsapi/qjsengine.cpp66
-rw-r--r--src/qml/jsapi/qjsengine.h4
2 files changed, 70 insertions, 0 deletions
diff --git a/src/qml/jsapi/qjsengine.cpp b/src/qml/jsapi/qjsengine.cpp
index 65b65f1b21..3bcb12a8b6 100644
--- a/src/qml/jsapi/qjsengine.cpp
+++ b/src/qml/jsapi/qjsengine.cpp
@@ -1007,6 +1007,72 @@ QJSEngine *qjsEngine(const QObject *object)
return data->jsWrapper.engine()->jsEngine();
}
+
+/*!
+ \enum QJSEngine::ObjectOwnership
+
+ ObjectOwnership controls whether or not the JavaScript memory manager automatically destroys the
+ QObject when the corresponding JavaScript object is garbage collected by the
+ engine. The two ownership options are:
+
+ \value CppOwnership The object is owned by C++ code and the JavaScript memory manager will never
+ delete it. The JavaScript destroy() method cannot be used on these objects. This
+ option is similar to QScriptEngine::QtOwnership.
+
+ \value JavaScriptOwnership The object is owned by JavaScript. When the object
+ is returned to the JavaScript memory manager as the return value of a method call, the JavaScript
+ memory manager will track it and delete it if there are no remaining JavaScript references to it
+ and it has no QObject::parent(). An object tracked by one QJSEngine will be deleted during that
+ QJSEngine's destructor. Thus, JavaScript references between objects with JavaScriptOwnership from
+ two different engines will not be valid if one of these engines is deleted. This option is similar
+ to QScriptEngine::ScriptOwnership.
+
+ Generally an application doesn't need to set an object's ownership explicitly. the JavaScript
+ memory manager uses a heuristic to set the default ownership. By default, an object that is
+ created by the JavaScript memory manager has JavaScriptOwnership. The exception to this are the
+ root objects created by calling QQmlComponent::create() or QQmlComponent::beginCreate(), which
+ have CppOwnership by default. The ownership of these root-level objects is considered to have been
+ transferred to the C++ caller.
+
+ Objects not-created by the JavaScript memory manager have CppOwnership by default. The exception
+ to this are objects returned from C++ method calls; their ownership will be set to
+ JavaScriptOwnership. This applies only to explicit invocations of Q_INVOKABLE methods or slots,
+ but not to property getter invocations.
+
+ Calling setObjectOwnership() overrides the default ownership.
+*/
+
+/*!
+ Sets the \a ownership of \a object.
+*/
+void QJSEngine::setObjectOwnership(QObject *object, ObjectOwnership ownership)
+{
+ if (!object)
+ return;
+
+ QQmlData *ddata = QQmlData::get(object, true);
+ if (!ddata)
+ return;
+
+ ddata->indestructible = (ownership == CppOwnership)?true:false;
+ ddata->explicitIndestructibleSet = true;
+}
+
+/*!
+ Returns the ownership of \a object.
+*/
+QJSEngine::ObjectOwnership QJSEngine::objectOwnership(QObject *object)
+{
+ if (!object)
+ return CppOwnership;
+
+ QQmlData *ddata = QQmlData::get(object, false);
+ if (!ddata)
+ return CppOwnership;
+ else
+ return ddata->indestructible?CppOwnership:JavaScriptOwnership;
+}
+
QT_END_NAMESPACE
#include "moc_qjsengine.cpp"
diff --git a/src/qml/jsapi/qjsengine.h b/src/qml/jsapi/qjsengine.h
index 16919454e7..643c5d5a38 100644
--- a/src/qml/jsapi/qjsengine.h
+++ b/src/qml/jsapi/qjsengine.h
@@ -100,6 +100,10 @@ public:
void collectGarbage();
+ enum ObjectOwnership { CppOwnership, JavaScriptOwnership };
+ static void setObjectOwnership(QObject *, ObjectOwnership);
+ static ObjectOwnership objectOwnership(QObject *);
+
#if QT_DEPRECATED_SINCE(5, 6)
QT_DEPRECATED void installTranslatorFunctions(const QJSValue &object = QJSValue());
#endif