aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSimon Hausmann <simon.hausmann@digia.com>2014-04-09 12:17:40 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2014-04-09 16:49:03 +0200
commit60730cbb5e5475b5db6a15641211aa6958a93197 (patch)
treec2dddee019f4f5367ba357bff358f94cc163367b /src
parentfc5314f96990148a7c32b988caf53c5db92b2b4b (diff)
Add support for dynamic properties for QObjects in JavaScript
In QtScript you could add properties to a JS object that wraps a QObject. Depending on the wrap option the property was either stored on the JavaScript side or as dynamic QObject property. In QJSEngine/QJSValue, neither was supported - properties could not be added. For QObjects wrapped in JavaScript that weren't created by QML, we can restore the behavior of storing dynamically added properties as JavaScript properties. This makes porting from QtScript to QJS* much easier. Task-number: QTBUG-37408 Change-Id: I5ef1f379c08c3d84de9bdcac9b6a9397238064de Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@digia.com> Reviewed-by: Lars Knoll <lars.knoll@digia.com>
Diffstat (limited to 'src')
-rw-r--r--src/qml/jsruntime/qv4qobjectwrapper.cpp13
1 files changed, 10 insertions, 3 deletions
diff --git a/src/qml/jsruntime/qv4qobjectwrapper.cpp b/src/qml/jsruntime/qv4qobjectwrapper.cpp
index f611be2965..de556dc9ed 100644
--- a/src/qml/jsruntime/qv4qobjectwrapper.cpp
+++ b/src/qml/jsruntime/qv4qobjectwrapper.cpp
@@ -682,9 +682,16 @@ void QObjectWrapper::put(Managed *m, const StringRef name, const ValueRef value)
QQmlContextData *qmlContext = QV4::QmlContextWrapper::callingContext(v4);
if (!setQmlProperty(v4->currentContext(), qmlContext, that->m_object, name.getPointer(), QV4::QObjectWrapper::IgnoreRevision, value)) {
- QString error = QLatin1String("Cannot assign to non-existent property \"") +
- name->toQString() + QLatin1Char('\"');
- v4->currentContext()->throwError(error);
+ QQmlData *ddata = QQmlData::get(that->m_object);
+ // Types created by QML are not extensible at run-time, but for other QObjects we can store them
+ // as regular JavaScript properties, like on JavaScript objects.
+ if (ddata && ddata->compiledData) {
+ QString error = QLatin1String("Cannot assign to non-existent property \"") +
+ name->toQString() + QLatin1Char('\"');
+ v4->currentContext()->throwError(error);
+ } else {
+ QV4::Object::put(m, name, value);
+ }
}
}