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); } }
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; } } } }
void tst_QJSValueIterator::iterateArrayAndRemoveAllElements() { QJSEngine engine; QJSValue array = engine.newArray(); for (int i = 0; i < 20000; ++i) array.setProperty(i, i); QBENCHMARK { QJSValueIterator it(array); while (it.hasNext()) { it.next(); it.remove(); } } }
void tst_QJSValueIterator::iterateArrayAndDoubleElements() { QJSEngine engine; QJSValue array = engine.newArray(); for (int i = 0; i < 20000; ++i) array.setProperty(i, i); QBENCHMARK { QJSValueIterator it(array); while (it.hasNext()) { it.next(); it.setValue(QJSValue(&engine, it.value().toNumber() * 2)); } } }
void tst_QJSValueIterator::iterateArrayAndConvertNameToIndex() { QJSEngine engine; QJSValue array = engine.newArray(); for (int i = 0; i < 20000; ++i) array.setProperty(i, i); QBENCHMARK { QJSValueIterator it(array); while (it.hasNext()) { it.next(); it.scriptName().toArrayIndex(); } } }
void JsonDatabase::onQueryFinished() { QJsonDbReadRequest *request = qobject_cast<QJsonDbReadRequest *>(sender()); if (listCallbacks.contains(request)) { QJSValue callback = listCallbacks[request]; QJSEngine *engine = callback.engine(); QJSValueList args; args << QJSValue(QJSValue::UndefinedValue); QList<QJsonObject> objects = request->takeResults(); int count = objects.count(); if (count) { QJSValue response = engine->newArray(count); for (int i = 0; i < count; ++i) { QString partitionName = objects[i].value(QStringLiteral("name")).toString(); response.setProperty(i, engine->newQObject(partition(partitionName))); } args << response; } else { args << engine->newArray(); } callback.call(args); listCallbacks.remove(request); } }
void JsonDatabase::onQueryError(QtJsonDb::QJsonDbRequest::ErrorCode code, const QString &message) { QJsonDbReadRequest *request = qobject_cast<QJsonDbReadRequest *>(sender()); if (listCallbacks.contains(request)) { QJSValue callback = listCallbacks[request]; QJSEngine *engine = callback.engine(); QJSValueList args; QVariantMap error; error.insert(QStringLiteral("code"), code); error.insert(QStringLiteral("message"), message); args << engine->toScriptValue(QVariant(error)) << engine->newArray(); callback.call(args); listCallbacks.remove(request); } }
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(); } }
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::iterateArray() { QFETCH(QStringList, propertyNames); QFETCH(QStringList, propertyValues); QJSEngine engine; QJSValue array = engine.newArray(); // Fill the array for (int i = 0; i < propertyNames.size(); ++i) { array.setProperty(propertyNames.at(i), propertyValues.at(i)); } // Iterate thru array properties. Note that the QJSValueIterator doesn't guarantee // any order on the iteration! int length = array.property("length").toInt(); QCOMPARE(length, propertyNames.size()); bool iteratedThruLength = false; QHash<QString, QJSValue> arrayProperties; QJSValueIterator it(array); // Iterate forward while (it.hasNext()) { it.next(); const QString name = it.name(); if (name == QString::fromLatin1("length")) { QVERIFY(it.value().isNumber()); QCOMPARE(it.value().toInt(), length); QVERIFY2(!iteratedThruLength, "'length' appeared more than once during iteration."); iteratedThruLength = true; continue; } // Storing the properties we iterate in a hash to compare with test data. QVERIFY2(!arrayProperties.contains(name), "property appeared more than once during iteration."); arrayProperties.insert(name, it.value()); QVERIFY(it.value().strictlyEquals(array.property(name))); } // Verify properties QVERIFY(iteratedThruLength); QCOMPARE(arrayProperties.size(), propertyNames.size()); for (int i = 0; i < propertyNames.size(); ++i) { QVERIFY(arrayProperties.contains(propertyNames.at(i))); QCOMPARE(arrayProperties.value(propertyNames.at(i)).toString(), propertyValues.at(i)); } #if 0 // Iterate backwards arrayProperties.clear(); iteratedThruLength = false; it.toBack(); while (it.hasPrevious()) { it.previous(); const QString name = it.name(); if (name == QString::fromLatin1("length")) { QVERIFY(it.value().isNumber()); QCOMPARE(it.value().toInt(), length); QCOMPARE(it.flags(), QScriptValue::SkipInEnumeration | QScriptValue::Undeletable); QVERIFY2(!iteratedThruLength, "'length' appeared more than once during iteration."); iteratedThruLength = true; continue; } // Storing the properties we iterate in a hash to compare with test data. QVERIFY2(!arrayProperties.contains(name), "property appeared more than once during iteration."); arrayProperties.insert(name, it.value()); QCOMPARE(it.flags(), array.propertyFlags(name)); QVERIFY(it.value().strictlyEquals(array.property(name))); } // Verify properties QVERIFY(iteratedThruLength); QCOMPARE(arrayProperties.size(), propertyNames.size()); for (int i = 0; i < propertyNames.size(); ++i) { QVERIFY(arrayProperties.contains(propertyNames.at(i))); QCOMPARE(arrayProperties.value(propertyNames.at(i)).toString(), propertyValues.at(i)); } // ### Do we still need this test? // Forward test again but as object arrayProperties.clear(); iteratedThruLength = false; QJSValue arrayObject = engine.toObject(array); QJSValueIterator it2(arrayObject); while (it2.hasNext()) { it2.next(); const QString name = it2.name(); if (name == QString::fromLatin1("length")) { QVERIFY(it2.value().isNumber()); QCOMPARE(it2.value().toInt(), length); QCOMPARE(it2.flags(), QScriptValue::SkipInEnumeration | QScriptValue::Undeletable); QVERIFY2(!iteratedThruLength, "'length' appeared more than once during iteration."); iteratedThruLength = true; continue; } // Storing the properties we iterate in a hash to compare with test data. QVERIFY2(!arrayProperties.contains(name), "property appeared more than once during iteration."); arrayProperties.insert(name, it2.value()); QCOMPARE(it2.flags(), arrayObject.propertyFlags(name)); QVERIFY(it2.value().strictlyEquals(arrayObject.property(name))); } // Verify properties QVERIFY(iteratedThruLength); QCOMPARE(arrayProperties.size(), propertyNames.size()); for (int i = 0; i < propertyNames.size(); ++i) { QVERIFY(arrayProperties.contains(propertyNames.at(i))); QCOMPARE(arrayProperties.value(propertyNames.at(i)).toString(), propertyValues.at(i)); } #endif }