void tst_QJSValueIterator::assignObjectToIterator()
{
    QJSEngine eng;
    QJSValue obj1 = eng.newObject();
    obj1.setProperty("foo", 123);
    QJSValue obj2 = eng.newObject();
    obj2.setProperty("bar", 456);

    QJSValueIterator it(obj1);
    QVERIFY(it.hasNext());
    it.next();
    it = obj2;
    QVERIFY(it.hasNext());
    it.next();
    QCOMPARE(it.name(), QString::fromLatin1("bar"));

    it = obj1;
    QVERIFY(it.hasNext());
    it.next();
    QCOMPARE(it.name(), QString::fromLatin1("foo"));

    it = obj2;
    QVERIFY(it.hasNext());
    it.next();
    QCOMPARE(it.name(), QString::fromLatin1("bar"));

    it = obj2;
    QVERIFY(it.hasNext());
    it.next();
    QCOMPARE(it.name(), QString::fromLatin1("bar"));
}
Ejemplo n.º 2
0
QJSValue JSKitXMLHttpRequest::response() const
{
    QJSEngine *engine = _mgr->engine();
    if (_responseType.isEmpty() || _responseType == "text") {
        return engine->toScriptValue(QString::fromUtf8(_response));
    } else if (_responseType == "arraybuffer") {
        QJSValue arrayBufferProto = engine->globalObject().property("ArrayBuffer").property("prototype");
        QJSValue arrayBuf = engine->newObject();
        if (!arrayBufferProto.isUndefined()) {
            arrayBuf.setPrototype(arrayBufferProto);
            arrayBuf.setProperty("byteLength", engine->toScriptValue<uint>(_response.size()));
            QJSValue array = engine->newArray(_response.size());
            for (int i = 0; i < _response.size(); i++) {
                array.setProperty(i, engine->toScriptValue<int>(_response[i]));
            }
            arrayBuf.setProperty("_bytes", array);
            qCDebug(l) << "returning ArrayBuffer of" << _response.size() << "bytes";
        } else {
            qCWarning(l) << "Cannot find proto of ArrayBuffer";
        }
        return arrayBuf;
    } else {
        qCWarning(l) << "unsupported responseType:" << _responseType;
        return engine->toScriptValue<void*>(0);
    }
}
QJSValue QDeclarativeGalleryQueryModel::get(const QJSValue &index) const
{
    QJSEngine *scriptEngine = index.engine();

    if (!scriptEngine)
       return QJSValue();

    const int i = index.toInt();

    if (i < 0 || i >= m_rowCount || (i != m_resultSet->currentIndex() && !m_resultSet->fetch(i)))
       return QJSValue();

    QJSValue object = scriptEngine->newObject();

    object.setProperty(
            QLatin1String("itemId"), scriptEngine->toScriptValue(m_resultSet->itemId()));
    object.setProperty(
            QLatin1String("itemUrl"), scriptEngine->toScriptValue(m_resultSet->itemUrl()));

    typedef QVector<QPair<int, QString> >::const_iterator iterator;
    for (iterator it = m_propertyNames.constBegin(), end = m_propertyNames.constEnd();
            it != end;
            ++it) {
        QVariant value = m_resultSet->metaData(it->first);

        if (value.isNull())
            value = QVariant(m_resultSet->propertyType(it->first));

        object.setProperty(it->second, scriptEngine->toScriptValue(value));
    }

    return object;
}
void tst_QJSValueIterator::iterateOverObjectFromDeletedEngine()
{
    QJSEngine *engine = new QJSEngine;
    QJSValue objet = engine->newObject();

    // populate object with properties
    QHash<QString, int> properties;
    properties.insert("foo",1235);
    properties.insert("oof",5321);
    properties.insert("ofo",3521);
    QHash<QString, int>::const_iterator i = properties.constBegin();
    for (; i != properties.constEnd(); ++i) {
        objet.setProperty(i.key(), i.value());
    }

    // start iterating
    QJSValueIterator it(objet);
    it.next();
    QVERIFY(properties.contains(it.name()));

    delete engine;

    QVERIFY(objet.isUndefined());
    QVERIFY(it.name().isEmpty());
    QVERIFY(it.value().isUndefined());

    QVERIFY(!it.hasNext());
    it.next();

    QVERIFY(it.name().isEmpty());
    QVERIFY(it.value().isUndefined());

}
Ejemplo n.º 5
0
QJSValue JSKitPebble::buildAckEventObject(uint transaction, const QString &message) const
{
    QJSEngine *engine = _mgr->engine();
    QJSValue eventObj = engine->newObject();
    QJSValue dataObj = engine->newObject();

    dataObj.setProperty("transactionId", engine->toScriptValue(transaction));
    eventObj.setProperty("data", dataObj);

    if (!message.isEmpty()) {
        QJSValue errorObj = engine->newObject();
        errorObj.setProperty("message", engine->toScriptValue(message));
        eventObj.setProperty("error", errorObj);
    }

    return eventObj;
}
void tst_QJSValueIterator::iterateForward()
{
    QFETCH(QStringList, propertyNames);
    QFETCH(QStringList, propertyValues);
    QMap<QString, QString> pmap;
    QVERIFY(propertyNames.size() == propertyValues.size());

    QJSEngine engine;
    QJSValue object = engine.newObject();
    for (int i = 0; i < propertyNames.size(); ++i) {
        QString name = propertyNames.at(i);
        QString value = propertyValues.at(i);
        pmap.insert(name, value);
        object.setProperty(name, engine.toScriptValue(value));
    }
    QJSValue otherObject = engine.newObject();
    otherObject.setProperty("foo", engine.toScriptValue(123456));
    otherObject.setProperty("protoProperty", engine.toScriptValue(654321));
    object.setPrototype(otherObject); // should not affect iterator

    QStringList lst;
    QJSValueIterator it(object);
    while (!pmap.isEmpty()) {
        QCOMPARE(it.hasNext(), true);
        QCOMPARE(it.hasNext(), true);
        it.next();
        QString name = it.name();
        QCOMPARE(pmap.contains(name), true);
        QCOMPARE(it.name(), name);
        QCOMPARE(it.value().strictlyEquals(engine.toScriptValue(pmap.value(name))), true);
        pmap.remove(name);
        lst.append(name);
    }

    QCOMPARE(it.hasNext(), false);
    QCOMPARE(it.hasNext(), false);

    it = object;
    for (int i = 0; i < lst.count(); ++i) {
        QCOMPARE(it.hasNext(), true);
        it.next();
        QCOMPARE(it.name(), lst.at(i));
    }
}
Ejemplo n.º 7
0
void tst_QJSValueIterator::flags()
{
    QJSEngine engine;
    QJSValue object = engine.newObject();
    QJSValue::PropertyFlags flags = flags;
    object.setProperty("foo", 123, QJSValue::SkipInEnumeration | QJSValue::ReadOnly | QJSValue::Undeletable);
    QJSValueIterator it(object);
    it.next();
    QBENCHMARK {
        for (int i = 0; i < 50000; ++i)
            it.flags();
    }
}
QJSValue SortFilterProxyModel::get(int idx) const
{
    QJSEngine *engine = qmlEngine(this);
    QJSValue value = engine->newObject();
    if (idx >= 0 && idx < count()) {
        QHash<int, QByteArray> roles = roleNames();
        QHashIterator<int, QByteArray> it(roles);
        while (it.hasNext()) {
            it.next();
            value.setProperty(QString::fromUtf8(it.value()), data(index(idx, 0), it.key()).toString());
        }
    }
    return value;
}
Ejemplo n.º 9
0
void tst_QJSValueIterator::hasNextAndNext()
{
    QJSEngine engine;
    QJSValue object = engine.newObject();
    for (int i = 0; i < 2000; ++i)
        object.setProperty(i, i);
    QBENCHMARK {
        for (int i = 0; i < 1000; ++i) {
            QJSValueIterator it(object);
            while (it.hasNext())
                it.next();
        }
    }
}
Ejemplo n.º 10
0
void JsonDbPartition::call(QMap<QJsonDbWriteRequest*, QJSValue> &callbacks, QJsonDbWriteRequest *request)
{
    QJSValue callback = callbacks[request];
    QJSEngine *engine = callback.engine();
    if (!engine) {
        callbacks.remove(request);
        return;
    }
    QList<QJsonObject> objects = request->takeResults();
    QJSValueList args;
    // object : id  , statenumber , items
    QJSValue response= engine->newObject();
    response.setProperty(JsonDbStrings::Protocol::stateNumber(), request->stateNumber());
    response.setProperty(JsonDbStrings::Protocol::requestId(), request->property("requestId").toInt());
    response.setProperty(QLatin1String("items"), qjsonobject_list_to_qjsvalue(objects));
    args << QJSValue(QJSValue::UndefinedValue) << response;
    callback.call(args);
    callbacks.remove(request);
}
Ejemplo n.º 11
0
void QQuickDialog::updateStandardButtons()
{
    if (m_standardButtonsRightModel.isUndefined()) {
        QJSEngine *engine = qmlEngine(this);
        // Managed objects so no need to destroy any existing ones
        m_standardButtonsLeftModel = engine->newArray();
        m_standardButtonsRightModel = engine->newArray();
        int i = 0;

        QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme();
        QPlatformDialogHelper::ButtonLayout layoutPolicy =
            static_cast<QPlatformDialogHelper::ButtonLayout>(theme->themeHint(QPlatformTheme::DialogButtonBoxLayout).toInt());
        const int *buttonLayout = QPlatformDialogHelper::buttonLayout(Qt::Horizontal, layoutPolicy);
        QJSValue *model = &m_standardButtonsLeftModel;
        for (int r = 0; buttonLayout[r] != QPlatformDialogHelper::EOL; ++r) {
            quint32 role = (buttonLayout[r] & ~QPlatformDialogHelper::Reverse);
            // Keep implementation in sync with that in QDialogButtonBoxPrivate::layoutButtons()
            // to the extent that QtQuick supports the same features
            switch (role) {
            case QPlatformDialogHelper::Stretch:
                model = &m_standardButtonsRightModel;
                i = 0;
                break;
            // TODO maybe: case QPlatformDialogHelper::AlternateRole:
            default: {
                    for (int e = QPlatformDialogHelper::LowestBit; e <= QPlatformDialogHelper::HighestBit; ++e) {
                        quint32 standardButton = 1 << e;
                        quint32 standardButtonRole = QPlatformDialogHelper::buttonRole(
                            static_cast<QPlatformDialogHelper::StandardButton>(standardButton));
                        if ((m_enabledButtons & standardButton) && standardButtonRole == role) {
                            QJSValue o = engine->newObject();
                            o.setProperty("text", theme->standardButtonText(standardButton));
                            o.setProperty("standardButton", standardButton);
                            o.setProperty("role", role);
                            model->setProperty(i++, o);
                        }
                   }
                } break;
            }
        }
    }
}
Ejemplo n.º 12
0
void JsonDbPartition::queryFinished()
{
    JsonDbQueryObject *object = qobject_cast<JsonDbQueryObject*>(sender());
    if (object) {
        int id = findIds.value(object);
        QJSValue callback = findCallbacks.value(object);
        QJSEngine *engine = callback.engine();
        if (engine && callback.isCallable()) {
            QJSValueList args;
            // object : id  , statenumber , items
            QJSValue response= engine->newObject();
            response.setProperty(JsonDbStrings::Protocol::stateNumber(), object->stateNumber());
            response.setProperty(JsonDbStrings::Protocol::requestId(),  id);
            response.setProperty(QLatin1String("items"), object->takeResults());
            args << QJSValue(QJSValue::UndefinedValue) << response;
            callback.call(args);
        }
        findIds.remove(object);
        findCallbacks.remove(object);
        object->deleteLater();
    }
}
Ejemplo n.º 13
0
void JsonDbPartition::queryStatusChanged()
{
    JsonDbQueryObject *object = qobject_cast<JsonDbQueryObject*>(sender());
    if (object && object->status() == JsonDbQueryObject::Error) {
        int id = findIds.value(object);
        QJSValue callback = findCallbacks.value(object);
        QJSEngine *engine = callback.engine();
        if (engine && callback.isCallable()) {
            QJSValueList args;

            QJSValue response = engine->newObject();
            response.setProperty(JsonDbStrings::Protocol::stateNumber(), -1);
            response.setProperty(JsonDbStrings::Protocol::requestId(),  id);
            response.setProperty(QLatin1String("items"), engine->newArray());

            args << engine->toScriptValue(object->error())<< response;
            callback.call(args);
        }
        findIds.remove(object);
        findCallbacks.remove(object);
        object->deleteLater();
    }

}
Ejemplo n.º 14
0
void JsonDbPartition::callErrorCallback(QMap<QJsonDbWriteRequest*, QJSValue> &callbacks, QJsonDbWriteRequest *request,
                                        QtJsonDb::QJsonDbRequest::ErrorCode code, const QString &message)
{
    QJSValue callback = callbacks[request];
    QJSEngine *engine = callback.engine();
    if (!engine) {
        callbacks.remove(request);
        return;
    }
    QJSValueList args;
    QVariantMap error;
    error.insert(QLatin1String("code"), code);
    error.insert(QLatin1String("message"), message);

    // object : id
    QJSValue response = engine->newObject();
    response.setProperty(JsonDbStrings::Protocol::stateNumber(), -1);
    response.setProperty(JsonDbStrings::Protocol::requestId(), request->property("requestId").toInt());
    response.setProperty(QLatin1String("items"), engine->newArray());

    args << engine->toScriptValue(QVariant(error))<< response;
    callback.call(args);
    callbacks.remove(request);
}
void tst_QJSValueIterator::iterateGetterSetter()
{
    // unified getter/setter function
    {
        QJSEngine eng;
        QJSValue obj = eng.newObject();
        obj.setProperty("foo", eng.newFunction(myGetterSetter),
                        QScriptValue::PropertyGetter | QScriptValue::PropertySetter);
        QJSValue val(&eng, 123);
        obj.setProperty("foo", val);
        QVERIFY(obj.property("bar").strictlyEquals(val));
        QVERIFY(obj.property("foo").strictlyEquals(val));

        QJSValueIterator it(obj);
        QVERIFY(it.hasNext());
        it.next();
        QCOMPARE(it.name(), QString::fromLatin1("foo"));
        QCOMPARE(it.flags(), QScriptValue::PropertyFlags(QScriptValue::PropertyGetter | QScriptValue::PropertySetter));
        QVERIFY(it.value().strictlyEquals(val));
        QJSValue val2(&eng, 456);
        it.setValue(val2);
        QVERIFY(obj.property("bar").strictlyEquals(val2));
        QVERIFY(obj.property("foo").strictlyEquals(val2));

        QVERIFY(it.hasNext());
        it.next();
        QCOMPARE(it.name(), QString::fromLatin1("bar"));
        QVERIFY(!it.hasNext());

        QVERIFY(it.hasPrevious());
        it.previous();
        QCOMPARE(it.name(), QString::fromLatin1("bar"));
        QVERIFY(it.hasPrevious());
        it.previous();
        QCOMPARE(it.name(), QString::fromLatin1("foo"));
        QCOMPARE(it.flags(), QScriptValue::PropertyFlags(QScriptValue::PropertyGetter | QScriptValue::PropertySetter));
        QVERIFY(it.value().strictlyEquals(val2));
        it.setValue(val);
        QVERIFY(obj.property("bar").strictlyEquals(val));
        QVERIFY(obj.property("foo").strictlyEquals(val));
    }
    // separate getter/setter function
    for (int x = 0; x < 2; ++x) {
        QJSEngine eng;
        QJSValue obj = eng.newObject();
        if (x == 0) {
            obj.setProperty("foo", eng.newFunction(myGetter), QScriptValue::PropertyGetter);
            obj.setProperty("foo", eng.newFunction(mySetter), QScriptValue::PropertySetter);
        } else {
            obj.setProperty("foo", eng.newFunction(mySetter), QScriptValue::PropertySetter);
            obj.setProperty("foo", eng.newFunction(myGetter), QScriptValue::PropertyGetter);
        }
        QJSValue val(&eng, 123);
        obj.setProperty("foo", val);
        QVERIFY(obj.property("bar").strictlyEquals(val));
        QVERIFY(obj.property("foo").strictlyEquals(val));

        QJSValueIterator it(obj);
        QVERIFY(it.hasNext());
        it.next();
        QCOMPARE(it.name(), QString::fromLatin1("foo"));
        QVERIFY(it.value().strictlyEquals(val));
        QJSValue val2(&eng, 456);
        it.setValue(val2);
        QVERIFY(obj.property("bar").strictlyEquals(val2));
        QVERIFY(obj.property("foo").strictlyEquals(val2));

        QVERIFY(it.hasNext());
        it.next();
        QCOMPARE(it.name(), QString::fromLatin1("bar"));
        QVERIFY(!it.hasNext());

        QVERIFY(it.hasPrevious());
        it.previous();
        QCOMPARE(it.name(), QString::fromLatin1("bar"));
        QVERIFY(it.hasPrevious());
        it.previous();
        QCOMPARE(it.name(), QString::fromLatin1("foo"));
        QVERIFY(it.value().strictlyEquals(val2));
        it.setValue(val);
        QVERIFY(obj.property("bar").strictlyEquals(val));
        QVERIFY(obj.property("foo").strictlyEquals(val));
    }
}