Showing the changes to libraries/script-engine/src/AssetScriptingInterface.cpp (1 of 2 files). Show all
Unified · Split
| @@ -28,12 +28,9 @@ · 27 unmodified lines | |||
| 28 | #include "ScriptEngine.h" | 28 | #include "ScriptEngine.h" |
| 29 | #include "ScriptEngineLogging.h" | 29 | #include "ScriptEngineLogging.h" |
| 30 | 30 | ||
| 31 | AssetScriptingInterface::AssetScriptingInterface(QObject* parent) : BaseAssetScriptingInterface(parent) { | 31 | AssetScriptingInterface::AssetScriptingInterface(QObject* parent) : BaseAssetScriptingInterface(parent) {} |
| 32 | if (auto engine = qobject_cast<QScriptEngine*>(parent)) { | 32 | |
| 33 | registerMetaTypes(engine); | 33 | #define JS_VERIFY(cond, error) { if (!this->jsVerify(cond, error)) { return; } } |
| 34 | } | ||
| 35 | } | ||
| 36 | #define JS_ASSERT(cond, error) { if (!this->jsAssert(cond, error)) { return; } } | ||
| 37 | 34 | ||
| 38 | void AssetScriptingInterface::uploadData(QString data, QScriptValue callback) { | 35 | void AssetScriptingInterface::uploadData(QString data, QScriptValue callback) { |
| 39 | auto handler = makeScopedHandlerObject(thisObject(), callback); | 36 | auto handler = makeScopedHandlerObject(thisObject(), callback); |
| @@ -176,8 +173,8 @@ void AssetScriptingInterface::sendFakedHandshake() { · 136 unmodified lines | |||
| 176 | void AssetScriptingInterface::getMapping(QString asset, QScriptValue callback) { | 173 | void AssetScriptingInterface::getMapping(QString asset, QScriptValue callback) { |
| 177 | auto path = AssetUtils::getATPUrl(asset).path(); | 174 | auto path = AssetUtils::getATPUrl(asset).path(); |
| 178 | auto handler = makeScopedHandlerObject(thisObject(), callback); | 175 | auto handler = makeScopedHandlerObject(thisObject(), callback); |
| 179 | JS_ASSERT(AssetUtils::isValidFilePath(path), "invalid ATP file path: " + asset + "(path:"+path+")"); | 176 | JS_VERIFY(AssetUtils::isValidFilePath(path), "invalid ATP file path: " + asset + "(path:"+path+")"); |
| 180 | JS_ASSERT(callback.isFunction(), "expected second parameter to be a callback function"); | 177 | JS_VERIFY(callback.isFunction(), "expected second parameter to be a callback function"); |
| 181 | qDebug() << ">>getMapping//getAssetInfo" << path; | 178 | qDebug() << ">>getMapping//getAssetInfo" << path; |
| 182 | Promise promise = getAssetInfo(path); | 179 | Promise promise = getAssetInfo(path); |
| 183 | promise->ready([this, handler](QString error, QVariantMap result) { | 180 | promise->ready([this, handler](QString error, QVariantMap result) { |
| @@ -186,14 +183,14 @@ void AssetScriptingInterface::getMapping(QString asset, QScriptValue callback) { · 2 unmodified lines | |||
| 186 | }); | 183 | }); |
| 187 | } | 184 | } |
| 188 | 185 | ||
| 189 | bool AssetScriptingInterface::jsAssert(bool condition, const QString& error) { | 186 | bool AssetScriptingInterface::jsVerify(bool condition, const QString& error) { |
| 190 | if (condition) { | 187 | if (condition) { |
| 191 | return true; | 188 | return true; |
| 192 | } | 189 | } |
| 193 | if (context()) { | 190 | if (context()) { |
| 194 | context()->throwError(error); | 191 | context()->throwError(error); |
| 195 | } else { | 192 | } else { |
| 196 | qDebug() << "WARNING -- jsAssert failed outside of a valid JS context: " + error; | 193 | qDebug() << "WARNING -- jsVerify failed outside of a valid JS context: " + error; |
| 197 | } | 194 | } |
| 198 | return false; | 195 | return false; |
| 199 | } | 196 | } |
| @@ -202,7 +199,7 @@ void AssetScriptingInterface::jsCallback(const QScriptValue& handler, · 2 unmodified lines | |||
| 202 | const QScriptValue& error, const QScriptValue& result) { | 199 | const QScriptValue& error, const QScriptValue& result) { |
| 203 | Q_ASSERT(thread() == QThread::currentThread()); | 200 | Q_ASSERT(thread() == QThread::currentThread()); |
| 204 | auto errorValue = !error.toBool() ? QScriptValue::NullValue : error; | 201 | auto errorValue = !error.toBool() ? QScriptValue::NullValue : error; |
| 205 | JS_ASSERT(handler.isObject() && handler.property("callback").isFunction(), | 202 | JS_VERIFY(handler.isObject() && handler.property("callback").isFunction(), |
| 206 | QString("jsCallback -- .callback is not a function (%1)") | 203 | QString("jsCallback -- .callback is not a function (%1)") |
| 207 | .arg(handler.property("callback").toVariant().typeName())); | 204 | .arg(handler.property("callback").toVariant().typeName())); |
| 208 | ::callScopedHandlerObject(handler, errorValue, result); | 205 | ::callScopedHandlerObject(handler, errorValue, result); |
| @@ -217,11 +214,11 @@ void AssetScriptingInterface::jsCallback(const QScriptValue& handler, · 8 unmodified lines | |||
| 217 | } | 214 | } |
| 218 | 215 | ||
| 219 | void AssetScriptingInterface::deleteAsset(QScriptValue options, QScriptValue scope, QScriptValue callback) { | 216 | void AssetScriptingInterface::deleteAsset(QScriptValue options, QScriptValue scope, QScriptValue callback) { |
| 220 | jsAssert(false, "TODO: deleteAsset API"); | 217 | jsVerify(false, "TODO: deleteAsset API"); |
| 221 | } | 218 | } |
| 222 | 219 | ||
| 223 | void AssetScriptingInterface::getAsset(QScriptValue options, QScriptValue scope, QScriptValue callback) { | 220 | void AssetScriptingInterface::getAsset(QScriptValue options, QScriptValue scope, QScriptValue callback) { |
| 224 | JS_ASSERT(options.isObject() || options.isString(), "expected request options Object or URL as first parameter"); | 221 | JS_VERIFY(options.isObject() || options.isString(), "expected request options Object or URL as first parameter"); |
| 225 | 222 | ||
| 226 | auto decompress = options.property("decompress").toBool() || options.property("compressed").toBool(); | 223 | auto decompress = options.property("decompress").toBool() || options.property("compressed").toBool(); |
| 227 | auto responseType = options.property("responseType").toString().toLower(); | 224 | auto responseType = options.property("responseType").toString().toLower(); |
| @@ -235,11 +232,11 @@ void AssetScriptingInterface::getAsset(QScriptValue options, QScriptValue scope, · 7 unmodified lines | |||
| 235 | auto asset = AssetUtils::getATPUrl(url).path(); | 232 | auto asset = AssetUtils::getATPUrl(url).path(); |
| 236 | auto handler = makeScopedHandlerObject(scope, callback); | 233 | auto handler = makeScopedHandlerObject(scope, callback); |
| 237 | 234 | ||
| 238 | JS_ASSERT(handler.property("callback").isFunction(), | 235 | JS_VERIFY(handler.property("callback").isFunction(), |
| 239 | QString("Invalid callback function (%1)").arg(handler.property("callback").toVariant().typeName())); | 236 | QString("Invalid callback function (%1)").arg(handler.property("callback").toVariant().typeName())); |
| 240 | JS_ASSERT(AssetUtils::isValidHash(asset) || AssetUtils::isValidFilePath(asset), | 237 | JS_VERIFY(AssetUtils::isValidHash(asset) || AssetUtils::isValidFilePath(asset), |
| 241 | QString("Invalid ATP url '%1'").arg(url)); | 238 | QString("Invalid ATP url '%1'").arg(url)); |
| 242 | JS_ASSERT(RESPONSE_TYPES.contains(responseType), | 239 | JS_VERIFY(RESPONSE_TYPES.contains(responseType), |
| 243 | QString("Invalid responseType: '%1' (expected: %2)").arg(responseType).arg(RESPONSE_TYPES.join(" | "))); | 240 | QString("Invalid responseType: '%1' (expected: %2)").arg(responseType).arg(RESPONSE_TYPES.join(" | "))); |
| 244 | 241 | ||
| 245 | Promise resolved = makePromise("resolved"); | 242 | Promise resolved = makePromise("resolved"); |
| @@ -283,9 +280,9 @@ void AssetScriptingInterface::resolveAsset(QScriptValue options, QScriptValue sc · 37 unmodified lines | |||
| 283 | auto asset = AssetUtils::getATPUrl(url).path(); | 280 | auto asset = AssetUtils::getATPUrl(url).path(); |
| 284 | auto handler = makeScopedHandlerObject(scope, callback); | 281 | auto handler = makeScopedHandlerObject(scope, callback); |
| 285 | 282 | ||
| 286 | JS_ASSERT(AssetUtils::isValidFilePath(asset) || AssetUtils::isValidHash(asset), | 283 | JS_VERIFY(AssetUtils::isValidFilePath(asset) || AssetUtils::isValidHash(asset), |
| 287 | "expected options to be an asset URL or request options containing .url property"); | 284 | "expected options to be an asset URL or request options containing .url property"); |
| 288 | JS_ASSERT(handler.property("callback").isFunction(), "invalid callback function"); | 285 | JS_VERIFY(handler.property("callback").isFunction(), "invalid callback function"); |
| 289 | getAssetInfo(asset)->ready([=](QString error, QVariantMap result) { | 286 | getAssetInfo(asset)->ready([=](QString error, QVariantMap result) { |
| 290 | qDebug() << "//resolveAsset/getAssetInfo" << error << result.value("hash"); | 287 | qDebug() << "//resolveAsset/getAssetInfo" << error << result.value("hash"); |
| 291 | jsCallback(handler, error, result); | 288 | jsCallback(handler, error, result); |
| @@ -348,11 +345,11 @@ void AssetScriptingInterface::putAsset(QScriptValue options, QScriptValue scope, · 56 unmodified lines | |||
| 348 | data.toString().toUtf8() : | 345 | data.toString().toUtf8() : |
| 349 | qscriptvalue_cast<QByteArray>(data); | 346 | qscriptvalue_cast<QByteArray>(data); |
| 350 | 347 | ||
| 351 | JS_ASSERT(path.isEmpty() || AssetUtils::isValidFilePath(path), | 348 | JS_VERIFY(path.isEmpty() || AssetUtils::isValidFilePath(path), |
| 352 | QString("expected valid ATP file path '%1' ('%2')").arg(rawPath).arg(path)); | 349 | QString("expected valid ATP file path '%1' ('%2')").arg(rawPath).arg(path)); |
| 353 | JS_ASSERT(handler.property("callback").isFunction(), | 350 | JS_VERIFY(handler.property("callback").isFunction(), |
| 354 | "invalid callback function"); | 351 | "invalid callback function"); |
| 355 | JS_ASSERT(dataByteArray.size() > 0, | 352 | JS_VERIFY(dataByteArray.size() > 0, |
| 356 | QString("expected non-zero .data (got %1 / #%2 bytes)") | 353 | QString("expected non-zero .data (got %1 / #%2 bytes)") |
| 357 | .arg(data.toVariant().typeName()) | 354 | .arg(data.toVariant().typeName()) |
| 358 | .arg(dataByteArray.size())); | 355 | .arg(dataByteArray.size())); |