/** * @internal * @brief QBsonConverter::mapValue converts alls simple values and redirects to * mapObject and mapArray methods. If a value can not converted an * empty bsonvalue is returned. * * @param jsValue jsValue to convert * * @return QBsonValue */ QBsonValue QBsonConverter::mapValue(const QJSValue &jsValue) { if (jsValue.isBool()) { return QBsonValue(jsValue.toBool()); } if (jsValue.isDate()) { return QBsonValue(jsValue.toDateTime()); } if (jsValue.isString()) { return QBsonValue(jsValue.toString()); } if (jsValue.isNumber()) { int vType = jsValue.toVariant().type(); switch (vType) { case QVariant::Double: return QBsonValue(jsValue.toVariant().toDouble()); break; case QVariant::Int: return QBsonValue(jsValue.toVariant().toInt()); break; } } if (jsValue.isArray()) { return mapArray(jsValue); } if (jsValue.isObject()) { return mapObject(jsValue); } return QBsonValue(); }
QJsonValue QJsonConvert::jsonFromScriptValue(const QJSValue& jsval){ if ( jsval.isArray() ){ QJSValueIterator it(jsval); QJsonArray arr; while( it.hasNext() ){ it.next(); QJsonValue itJsonValue = jsonFromScriptValue(it.value()); if( !itJsonValue.isNull() ) arr.append(itJsonValue); } return QJsonValue(arr); } else if ( jsval.isObject() ){ QJSValueIterator it(jsval); QJsonObject obj; while( it.hasNext() ){ it.next(); QJsonValue itJsonValue = jsonFromScriptValue(it.value()); if( !itJsonValue.isNull() ) obj[it.name()] = itJsonValue; } return QJsonValue(obj); } else if ( jsval.isString() ){ return QJsonValue(jsval.toString()); } else if ( jsval.isBool() ){ return QJsonValue(jsval.toString()); } else if ( jsval.isNumber() ){ return QJsonValue(jsval.toNumber() ); } return QJsonValue(); }
static QString qjsValueToString(const QJSValue &v) { if (v.isArray()) { return QStringLiteral("<array>"); } else if (v.isBool()) { return v.toBool() ? QStringLiteral("true") : QStringLiteral("false"); } else if (v.isCallable()) { return QStringLiteral("<callable>"); } else if (v.isDate()) { return v.toDateTime().toString(); } else if (v.isError()) { return QStringLiteral("<error>"); } else if (v.isNull()) { return QStringLiteral("<null>"); } else if (v.isNumber()) { return QString::number(v.toNumber()); } else if (v.isObject()) { return QStringLiteral("<object>"); } else if (v.isQObject()) { return Util::displayString(v.toQObject()); } else if (v.isRegExp()) { return QStringLiteral("<regexp>"); } else if (v.isString()) { return v.toString(); } else if (v.isUndefined()) { return QStringLiteral("<undefined>"); } else if (v.isVariant()) { return VariantHandler::displayString(v.toVariant()); } return QStringLiteral("<unknown QJSValue>"); }
void JSKitXMLHttpRequest::send(const QJSValue &data) { QByteArray byteData; if (data.isUndefined() || data.isNull()) { // Do nothing, byteData is empty. } else if (data.isString()) { byteData == data.toString().toUtf8(); } else if (data.isObject()) { if (data.hasProperty("byteLength")) { // Looks like an ArrayView or an ArrayBufferView! QJSValue buffer = data.property("buffer"); if (buffer.isUndefined()) { // We must assume we've been passed an ArrayBuffer directly buffer = data; } QJSValue array = data.property("_bytes"); int byteLength = data.property("byteLength").toInt(); if (array.isArray()) { byteData.reserve(byteLength); for (int i = 0; i < byteLength; i++) { byteData.append(array.property(i).toInt()); } qCDebug(l) << "passed an ArrayBufferView of" << byteData.length() << "bytes"; } else { qCWarning(l) << "passed an unknown/invalid ArrayBuffer" << data.toString(); } } else { qCWarning(l) << "passed an unknown object" << data.toString(); } } QBuffer *buffer; if (!byteData.isEmpty()) { buffer = new QBuffer; buffer->setData(byteData); } else { buffer = 0; } qCDebug(l) << "sending" << _verb << "to" << _request.url() << "with" << QString::fromUtf8(byteData); _reply = _net->sendCustomRequest(_request, _verb.toLatin1(), buffer); connect(_reply, &QNetworkReply::finished, this, &JSKitXMLHttpRequest::handleReplyFinished); connect(_reply, static_cast<void (QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error), this, &JSKitXMLHttpRequest::handleReplyError); if (buffer) { // So that it gets deleted alongside the reply object. buffer->setParent(_reply); } }
Option<Error> jsToJson(QJSValue value, JsonValue::Builder builder) { if (value.isArray()) { std::vector<QJSValue> arr; QJSValueIterator iter(value); while (iter.hasNext()) { iter.next(); // dafuq!? why is this required if (!iter.hasNext()) { break; } arr.push_back(iter.value()); } auto lst = builder.initArray(static_cast<unsigned int>(arr.size())); for (auto k = 0; k < arr.size(); ++k) { auto subvalue = lst[k]; auto opt_err = jsToJson(arr[k], subvalue); if (opt_err.has()) { return opt_err; } } return Option<Error>(); } if (value.isObject()) { std::vector<std::tuple<QString, QJSValue>> arr; QJSValueIterator iter(value); while (iter.hasNext()) { iter.next(); arr.push_back(std::make_tuple(iter.name(), iter.value())); } auto lst = builder.initObject(static_cast<unsigned int>(arr.size())); for (auto k = 0; k < arr.size(); ++k) { auto value_builder = lst[k].initValue(); const char *name = std::get<0>(arr[k]).toUtf8().constData(); lst[k].setName(name); auto opt_err = jsToJson(std::get<1>(arr[k]), value_builder); if (opt_err.has()) { return opt_err; } } return Option<Error>(); } if (value.isBool()) { builder.setBoolean(value.toBool()); return Option<Error>(); } if (value.isNumber()) { builder.setNumber(value.toNumber()); return Option<Error>(); } if (value.isString()) { const char *str = value.toString().toUtf8().constData(); builder.setString(str); return Option<Error>(); } if (value.isNull()) { builder.setNone(); return Option<Error>(); } auto tpname = toStdString(value.toVariant().typeName()); Error err(Error::Kind::ClientSideError, "Invalid type: " + tpname); return err; }
QJSValue RequestPrototype::query(const QJSValue &query) { if (query.isObject()) { JSValueIterator it(query); while (it.next()) { m_query.addQueryItem( it.name(), it.value().toString()); } } else if (query.isString()) { QUrlQuery parsed(query.toString()); QList<QPair<QString, QString> > items = parsed.queryItems(QUrl::FullyDecoded); QPair<QString, QString> pair; foreach(pair, items) { m_query.addQueryItem(pair.first, pair.second); }
void QFAppScriptRunnable::setCondition(QJSValue condition) { m_condition = condition; if (condition.isString()) { setType(condition.toString()); m_isSignalCondition = false; } else if (condition.isObject() && condition.hasProperty("connect")) { Q_ASSERT(!m_engine.isNull()); QString type = QString("QuickFlux.AppScript.%1").arg(QUuid::createUuid().toString()); setType(type); QString generator = "(function(dispatcher) { return function() {dispatcher.dispatch(arguments)}})"; QFAppDispatcher* dispatcher = QFAppDispatcher::instance(m_engine); QFAppScriptDispatcherWrapper * wrapper = new QFAppScriptDispatcherWrapper(); wrapper->setType(type); wrapper->setDispatcher(dispatcher); QJSValue generatorFunc = m_engine->evaluate(generator); QJSValueList args; args << m_engine->newQObject(wrapper); QJSValue callback = generatorFunc.call(args); args.clear(); args << callback; QJSValue connect = condition.property("connect"); connect.callWithInstance(condition,args); m_callback = callback; m_isSignalCondition = true; } else { qWarning() << "AppScript: Invalid condition type"; } }
bool TemplateEngine::evaluateBooleanJavaScriptExpression(QJSEngine &engine, const QString &expression, bool *result, QString *errorMessage) { if (errorMessage) errorMessage->clear(); if (result) *result = false; const QJSValue value = engine.evaluate(expression); if (value.isError()) { if (errorMessage) *errorMessage = QString::fromLatin1("Error in \"%1\": %2") .arg(expression, value.toString()); return false; } // Try to convert to bool, be that an int or whatever. if (value.isBool()) { if (result) *result = value.toBool(); return true; } if (value.isNumber()) { if (result) *result = !qFuzzyCompare(value.toNumber(), 0); return true; } if (value.isString()) { if (result) *result = !value.toString().isEmpty(); return true; } if (errorMessage) *errorMessage = QString::fromLatin1("Cannot convert result of \"%1\" (\"%2\"to bool.") .arg(expression, value.toString()); return false; }
extern "C" int hsqml_is_jval_string(HsQMLJValHandle* hndl) { QJSValue* value = reinterpret_cast<QJSValue*>(hndl); return value->isString(); }