diff options
Diffstat (limited to 'src/qml/jsapi')
-rw-r--r-- | src/qml/jsapi/qjsengine.cpp | 49 | ||||
-rw-r--r-- | src/qml/jsapi/qjsengine.h | 8 | ||||
-rw-r--r-- | src/qml/jsapi/qjsvalue.cpp | 67 | ||||
-rw-r--r-- | src/qml/jsapi/qjsvalue.h | 2 | ||||
-rw-r--r-- | src/qml/jsapi/qjsvalue_p.h | 1 | ||||
-rw-r--r-- | src/qml/jsapi/qjsvalueiterator.cpp | 10 |
6 files changed, 113 insertions, 24 deletions
diff --git a/src/qml/jsapi/qjsengine.cpp b/src/qml/jsapi/qjsengine.cpp index 9c952f0d42..4404a5d79f 100644 --- a/src/qml/jsapi/qjsengine.cpp +++ b/src/qml/jsapi/qjsengine.cpp @@ -166,6 +166,17 @@ Q_DECLARE_METATYPE(QList<int>) properties of the proxy object. No binding code is needed because it is done dynamically using the Qt meta object system. + Use newQMetaObject() to wrap a QMetaObject; this gives you a + "script representation" of a QObject-based class. newQMetaObject() + returns a proxy script object; enum values of the class are available + as properties of the proxy object. + + Constructors exposed to the meta-object system ( using Q_INVOKABLE ) can be + called from the script to create a new QObject instance with + JavaScriptOwnership. + + + \snippet code/src_script_qjsengine.cpp 5 \section1 Extensions @@ -261,12 +272,8 @@ static void checkForApplicationInstance() \l{ECMA-262}, Section 15.1. */ QJSEngine::QJSEngine() - : QObject(*new QJSEnginePrivate, 0) - , d(new QV8Engine(this)) + : QJSEngine(nullptr) { - checkForApplicationInstance(); - - QJSEnginePrivate::addToDebugServer(this); } /*! @@ -515,6 +522,38 @@ QJSValue QJSEngine::newQObject(QObject *object) } /*! + \since 5.8 + + Creates a JavaScript object that wraps the given QMetaObject + The metaObject must outlive the script engine. It is recommended to only + use this method with static metaobjects. + + + When called as a constructor, a new instance of the class will be created. + Only constructors exposed by Q_INVOKABLE will be visible from the script engine. + + \sa newQObject() +*/ + +QJSValue QJSEngine::newQMetaObject(const QMetaObject* metaObject) { + Q_D(QJSEngine); + QV4::ExecutionEngine *v4 = QV8Engine::getV4(d); + QV4::Scope scope(v4); + QV4::ScopedValue v(scope, QV4::QMetaObjectWrapper::create(v4, metaObject)); + return QJSValue(v4, v->asReturnedValue()); +} + +/*! \fn QJSValue QJSEngine::newQMetaObject<T>() + + \since 5.8 + Creates a JavaScript object that wraps the static QMetaObject associated + with class \c{T}. + + \sa newQObject() +*/ + + +/*! Returns this engine's Global Object. By default, the Global Object contains the built-in objects that are diff --git a/src/qml/jsapi/qjsengine.h b/src/qml/jsapi/qjsengine.h index 6ecd0c7ec0..41c4b81270 100644 --- a/src/qml/jsapi/qjsengine.h +++ b/src/qml/jsapi/qjsengine.h @@ -74,6 +74,14 @@ public: QJSValue newQObject(QObject *object); + QJSValue newQMetaObject(const QMetaObject* metaObject); + + template <typename T> + QJSValue newQMetaObject() + { + return newQMetaObject(&T::staticMetaObject); + } + template <typename T> inline QJSValue toScriptValue(const T &value) { diff --git a/src/qml/jsapi/qjsvalue.cpp b/src/qml/jsapi/qjsvalue.cpp index ec7848aba2..a4a96a96a7 100644 --- a/src/qml/jsapi/qjsvalue.cpp +++ b/src/qml/jsapi/qjsvalue.cpp @@ -178,7 +178,7 @@ QJSValue::QJSValue(SpecialValue value) : d(0) { if (value == NullValue) - QJSValuePrivate::setVariant(this, QVariant(QMetaType::VoidStar, (void *)0)); + QJSValuePrivate::setVariant(this, QVariant::fromValue(nullptr)); } /*! @@ -293,7 +293,10 @@ bool QJSValue::isNull() const if (val) return val->isNull(); QVariant *variant = QJSValuePrivate::getVariant(this); - return variant && variant->userType() == QMetaType::VoidStar; + if (!variant) + return false; + const int type = variant->userType(); + return type == QMetaType::Nullptr || type == QMetaType::VoidStar; } /*! @@ -582,7 +585,7 @@ quint32 QJSValue::toUInt() const \table \header \li Input Type \li Result \row \li Undefined \li An invalid QVariant. - \row \li Null \li A QVariant containing a null pointer (QMetaType::VoidStar). + \row \li Null \li A QVariant containing a null pointer (QMetaType::Nullptr). \row \li Boolean \li A QVariant containing the value of the boolean. \row \li Number \li A QVariant containing the value of the number. \row \li String \li A QVariant containing the value of the string. @@ -619,7 +622,7 @@ QVariant QJSValue::toVariant() const return QVariant(val->asDouble()); } if (val->isNull()) - return QVariant(QMetaType::VoidStar, 0); + return QVariant(QMetaType::Nullptr, 0); Q_ASSERT(val->isUndefined()); return QVariant(); } @@ -663,11 +666,11 @@ QJSValue QJSValue::call(const QJSValueList &args) callData->args[i] = QJSValuePrivate::convertedToValue(engine, args.at(i)); } - ScopedValue result(scope, f->call(callData)); + f->call(scope, callData); if (engine->hasException) - result = engine->catchException(); + scope.result = engine->catchException(); - return QJSValue(engine, result->asReturnedValue()); + return QJSValue(engine, scope.result.asReturnedValue()); } /*! @@ -719,11 +722,11 @@ QJSValue QJSValue::callWithInstance(const QJSValue &instance, const QJSValueList callData->args[i] = QJSValuePrivate::convertedToValue(engine, args.at(i)); } - ScopedValue result(scope, f->call(callData)); + f->call(scope, callData); if (engine->hasException) - result = engine->catchException(); + scope.result = engine->catchException(); - return QJSValue(engine, result->asReturnedValue()); + return QJSValue(engine, scope.result.asReturnedValue()); } /*! @@ -767,11 +770,11 @@ QJSValue QJSValue::callAsConstructor(const QJSValueList &args) callData->args[i] = QJSValuePrivate::convertedToValue(engine, args.at(i)); } - ScopedValue result(scope, f->construct(callData)); + f->construct(scope, callData); if (engine->hasException) - result = engine->catchException(); + scope.result = engine->catchException(); - return QJSValue(engine, result->asReturnedValue()); + return QJSValue(engine, scope.result.asReturnedValue()); } #ifdef QT_DEPRECATED @@ -938,7 +941,7 @@ bool QJSValue::equals(const QJSValue& other) const if (!ov) return other.equals(*this); - return Runtime::compareEqual(*v, *ov); + return Runtime::method_compareEqual(*v, *ov); } /*! @@ -1234,6 +1237,28 @@ QObject *QJSValue::toQObject() const } /*! + \since 5.8 + + * If this QJSValue is a QMetaObject, returns the QMetaObject pointer + * that the QJSValue represents; otherwise, returns 0. + * + * \sa isQMetaObject() + */ +const QMetaObject *QJSValue::toQMetaObject() const +{ + QV4::ExecutionEngine *engine = QJSValuePrivate::engine(this); + if (!engine) + return 0; + QV4::Scope scope(engine); + QV4::Scoped<QV4::QMetaObjectWrapper> wrapper(scope, QJSValuePrivate::getValue(this)); + if (!wrapper) + return 0; + + return wrapper->metaObject(); +} + + +/*! Returns a QDateTime representation of this value, in local time. If this QJSValue is not a date, or the value of the date is NaN (Not-a-Number), an invalid QDateTime is returned. @@ -1286,4 +1311,18 @@ bool QJSValue::isQObject() const return val && val->as<QV4::QObjectWrapper>() != 0; } +/*! + \since 5.8 + + Returns true if this QJSValue is a QMetaObject; otherwise returns + false. + + \sa toQMetaObject(), QJSEngine::newQMetaObject() +*/ +bool QJSValue::isQMetaObject() const +{ + QV4::Value *val = QJSValuePrivate::getValue(this); + return val && val->as<QV4::QMetaObjectWrapper>() != 0; +} + QT_END_NAMESPACE diff --git a/src/qml/jsapi/qjsvalue.h b/src/qml/jsapi/qjsvalue.h index e207e1b099..ab20a2607d 100644 --- a/src/qml/jsapi/qjsvalue.h +++ b/src/qml/jsapi/qjsvalue.h @@ -98,6 +98,7 @@ public: bool isUndefined() const; bool isVariant() const; bool isQObject() const; + bool isQMetaObject() const; bool isObject() const; bool isDate() const; bool isRegExp() const; @@ -111,6 +112,7 @@ public: bool toBool() const; QVariant toVariant() const; QObject *toQObject() const; + const QMetaObject *toQMetaObject() const; QDateTime toDateTime() const; bool equals(const QJSValue &other) const; diff --git a/src/qml/jsapi/qjsvalue_p.h b/src/qml/jsapi/qjsvalue_p.h index 25afd9275c..c4761ad6ea 100644 --- a/src/qml/jsapi/qjsvalue_p.h +++ b/src/qml/jsapi/qjsvalue_p.h @@ -132,6 +132,7 @@ public: case QMetaType::Void: *v = QV4::Encode::undefined(); break; + case QMetaType::Nullptr: case QMetaType::VoidStar: *v = QV4::Encode::null(); break; diff --git a/src/qml/jsapi/qjsvalueiterator.cpp b/src/qml/jsapi/qjsvalueiterator.cpp index 86c7554924..ce472ce7e5 100644 --- a/src/qml/jsapi/qjsvalueiterator.cpp +++ b/src/qml/jsapi/qjsvalueiterator.cpp @@ -103,11 +103,11 @@ QJSValueIterator::QJSValueIterator(const QJSValue& object) return; QV4::Scope scope(v4); QV4::Scoped<QV4::ForEachIteratorObject> it(scope, d_ptr->iterator.value()); - it->d()->it.flags = QV4::ObjectIterator::NoFlags; + it->d()->it().flags = QV4::ObjectIterator::NoFlags; QV4::ScopedString nm(scope); QV4::Property nextProperty; QV4::PropertyAttributes nextAttributes; - it->d()->it.next(nm.getRef(), &d_ptr->nextIndex, &nextProperty, &nextAttributes); + it->d()->it().next(nm.getRef(), &d_ptr->nextIndex, &nextProperty, &nextAttributes); d_ptr->nextName.set(v4, nm.asReturnedValue()); } @@ -157,7 +157,7 @@ bool QJSValueIterator::next() QV4::ScopedString nm(scope); QV4::Property nextProperty; QV4::PropertyAttributes nextAttributes; - it->d()->it.next(nm.getRef(), &d_ptr->nextIndex, &nextProperty, &nextAttributes); + it->d()->it().next(nm.getRef(), &d_ptr->nextIndex, &nextProperty, &nextAttributes); d_ptr->nextName.set(v4, nm.asReturnedValue()); return d_ptr->currentName.as<QV4::String>() || d_ptr->currentIndex != UINT_MAX; } @@ -231,11 +231,11 @@ QJSValueIterator& QJSValueIterator::operator=(QJSValue& object) QV4::ScopedObject o(scope, QJSValuePrivate::getValue(&object)); d_ptr->iterator.set(v4, v4->newForEachIteratorObject(o)); QV4::Scoped<QV4::ForEachIteratorObject> it(scope, d_ptr->iterator.value()); - it->d()->it.flags = QV4::ObjectIterator::NoFlags; + it->d()->it().flags = QV4::ObjectIterator::NoFlags; QV4::ScopedString nm(scope); QV4::Property nextProperty; QV4::PropertyAttributes nextAttributes; - it->d()->it.next(nm.getRef(), &d_ptr->nextIndex, &nextProperty, &nextAttributes); + it->d()->it().next(nm.getRef(), &d_ptr->nextIndex, &nextProperty, &nextAttributes); d_ptr->nextName.set(v4, nm.asReturnedValue()); return *this; } |