History

rework ScriptValue to act like a stack variable rather than a pointer

Heather Anderson committed 5 years ago

Commit 85810221 parent f209c51170 files · +2837 −2594Browse files

Showing the changes to libraries/script-engine/src/AssetScriptingInterface.cpp (1 of 170 files). Show all

Unified · Split

libraries/script-engine/src/AssetScriptingInterface.cpp+65−61
@@ -63,7 +63,7 @@ bool AssetScriptingInterface::initializeCache() { · 62 unmodified lines
6363 }
6464 }
6565
66−void AssetScriptingInterface::uploadData(QString data, ScriptValuePointer callback) {
66+void AssetScriptingInterface::uploadData(QString data, const ScriptValue& callback) {
6767 auto handler = jsBindCallback(thisObject(), callback);
6868 QByteArray dataByteArray = data.toUtf8();
6969 auto upload = DependencyManager::get<AssetClient>()->createUpload(dataByteArray);
@@ -88,7 +88,7 @@ void AssetScriptingInterface::uploadData(QString data, ScriptValuePointer callba · 18 unmodified lines
8888 upload->start();
8989 }
9090
91−void AssetScriptingInterface::setMapping(QString path, QString hash, ScriptValuePointer callback) {
91+void AssetScriptingInterface::setMapping(QString path, QString hash, const ScriptValue& callback) {
9292 auto handler = jsBindCallback(thisObject(), callback);
9393 auto setMappingRequest = assetClient()->createSetMappingRequest(path, hash);
9494 Promise deferred = makePromise(__FUNCTION__);
@@ -113,7 +113,7 @@ void AssetScriptingInterface::setMapping(QString path, QString hash, ScriptValue · 18 unmodified lines
113113 * @typedef {object} Assets.DownloadDataError
114114 * @property {string} errorMessage - <code>""</code> if the download was successful, otherwise a description of the error.
115115 */
116−void AssetScriptingInterface::downloadData(QString urlString, ScriptValuePointer callback) {
116+void AssetScriptingInterface::downloadData(QString urlString, const ScriptValue& callback) {
117117 // FIXME: historically this API method failed silently when given a non-atp prefixed
118118 // urlString (or if the AssetRequest failed).
119119 // .. is that by design or could we update without breaking things to provide better feedback to scripts?
@@ -156,7 +156,7 @@ void AssetScriptingInterface::downloadData(QString urlString, ScriptValuePointer · 36 unmodified lines
156156 assetRequest->start();
157157 }
158158
159−void AssetScriptingInterface::setBakingEnabled(QString path, bool enabled, ScriptValuePointer callback) {
159+void AssetScriptingInterface::setBakingEnabled(QString path, bool enabled, const ScriptValue& callback) {
160160 auto setBakingEnabledRequest = DependencyManager::get<AssetClient>()->createSetBakingEnabledRequest({ path }, enabled);
161161
162162 Promise deferred = jsPromiseReady(makePromise(__FUNCTION__), thisObject(), callback);
@@ -186,11 +186,11 @@ void AssetScriptingInterface::sendFakedHandshake() { · 23 unmodified lines
186186
187187 #endif
188188
189−void AssetScriptingInterface::getMapping(QString asset, ScriptValuePointer callback) {
189+void AssetScriptingInterface::getMapping(QString asset, const ScriptValue& callback) {
190190 auto path = AssetUtils::getATPUrl(asset).path();
191191 auto handler = jsBindCallback(thisObject(), callback);
192192 JS_VERIFY(AssetUtils::isValidFilePath(path), "invalid ATP file path: " + asset + "(path:"+path+")");
193− JS_VERIFY(callback->isFunction(), "expected second parameter to be a callback function");
193+ JS_VERIFY(callback.isFunction(), "expected second parameter to be a callback function");
194194 Promise promise = getAssetInfo(path);
195195 auto scriptEngine = engine();
196196 promise->ready([=](QString error, QVariantMap result) {
@@ -210,19 +210,19 @@ bool AssetScriptingInterface::jsVerify(bool condition, const QString& error) { · 13 unmodified lines
210210 return false;
211211 }
212212
213−ScriptValuePointer AssetScriptingInterface::jsBindCallback(ScriptValuePointer scope, ScriptValuePointer callback) {
214− ScriptValuePointer handler = ::makeScopedHandlerObject(scope, callback);
215− ScriptValuePointer value = handler->property("callback");
216− if (!jsVerify(handler->isObject() && value->isFunction(),
217− QString("jsBindCallback -- .callback is not a function (%1)").arg(value->toVariant().typeName()))) {
218− return ScriptValuePointer();
213+ScriptValue AssetScriptingInterface::jsBindCallback(const ScriptValue& scope, const ScriptValue& callback) {
214+ ScriptValue handler = ::makeScopedHandlerObject(scope, callback);
215+ ScriptValue value = handler.property("callback");
216+ if (!jsVerify(handler.isObject() && value.isFunction(),
217+ QString("jsBindCallback -- .callback is not a function (%1)").arg(value.toVariant().typeName()))) {
218+ return ScriptValue();
219219 }
220220 return handler;
221221 }
222222
223−Promise AssetScriptingInterface::jsPromiseReady(Promise promise, ScriptValuePointer scope, ScriptValuePointer callback) {
223+Promise AssetScriptingInterface::jsPromiseReady(Promise promise, const ScriptValue& scope, const ScriptValue& callback) {
224224 auto handler = jsBindCallback(scope, callback);
225− if (!jsVerify(handler->isValid(), "jsPromiseReady -- invalid callback handler")) {
225+ if (!jsVerify(handler.isValid(), "jsPromiseReady -- invalid callback handler")) {
226226 return nullptr;
227227 }
228228 auto scriptEngine = engine();
@@ -231,25 +231,25 @@ Promise AssetScriptingInterface::jsPromiseReady(Promise promise, ScriptValuePoin · 2 unmodified lines
231231 });
232232 }
233233
234−void AssetScriptingInterface::jsCallback(const ScriptValuePointer& handler,
235− const ScriptValuePointer& error, const ScriptValuePointer& result) {
234+void AssetScriptingInterface::jsCallback(const ScriptValue& handler,
235+ const ScriptValue& error, const ScriptValue& result) {
236236 Q_ASSERT(thread() == QThread::currentThread());
237− auto errorValue = !error->toBool() ? engine()->nullValue() : error;
238− JS_VERIFY(handler->isObject() && handler->property("callback")->isFunction(),
237+ auto errorValue = !error.toBool() ? engine()->nullValue() : error;
238+ JS_VERIFY(handler.isObject() && handler.property("callback").isFunction(),
239239 QString("jsCallback -- .callback is not a function (%1)")
240− .arg(handler->property("callback")->toVariant().typeName()));
240+ .arg(handler.property("callback").toVariant().typeName()));
241241 ::callScopedHandlerObject(handler, errorValue, result);
242242 }
243243
244−void AssetScriptingInterface::jsCallback(const ScriptValuePointer& handler,
245− const ScriptValuePointer& error, const QVariantMap& result) {
244+void AssetScriptingInterface::jsCallback(const ScriptValue& handler,
245+ const ScriptValue& error, const QVariantMap& result) {
246246 Q_ASSERT(thread() == QThread::currentThread());
247− Q_ASSERT(handler->engine());
248− auto engine = handler->engine();
247+ Q_ASSERT(handler.engine());
248+ auto engine = handler.engine();
249249 jsCallback(handler, error, engine->toScriptValue(result));
250250 }
251251
252−void AssetScriptingInterface::deleteAsset(ScriptValuePointer options, ScriptValuePointer scope, ScriptValuePointer callback) {
252+void AssetScriptingInterface::deleteAsset(const ScriptValue& options, const ScriptValue& scope, const ScriptValue& callback) {
253253 jsVerify(false, "TODO: deleteAsset API");
254254 }
255255
@@ -279,14 +279,14 @@ void AssetScriptingInterface::deleteAsset(ScriptValuePointer options, ScriptValu · 23 unmodified lines
279279 * @property {boolean} [wasRedirected] - <code>true</code> if the downloaded data is the baked version of the asset,
280280 * <code>false</code> if it isn't baked.
281281 */
282−void AssetScriptingInterface::getAsset(ScriptValuePointer options, ScriptValuePointer scope, ScriptValuePointer callback) {
283− JS_VERIFY(options->isObject() || options->isString(), "expected request options Object or URL as first parameter");
284−
285− auto decompress = options->property("decompress")->toBool() || options->property("compressed")->toBool();
286− auto responseType = options->property("responseType")->toString().toLower();
287− auto url = options->property("url")->toString();
288− if (options->isString()) {
289− url = options->toString();
282+void AssetScriptingInterface::getAsset(const ScriptValue& options, const ScriptValue& scope, const ScriptValue& callback) {
283+ JS_VERIFY(options.isObject() || options.isString(), "expected request options Object or URL as first parameter");
284+
285+ auto decompress = options.property("decompress").toBool() || options.property("compressed").toBool();
286+ auto responseType = options.property("responseType").toString().toLower();
287+ auto url = options.property("url").toString();
288+ if (options.isString()) {
289+ url = options.toString();
290290 }
291291 if (responseType.isEmpty()) {
292292 responseType = "text";
@@ -345,10 +345,10 @@ void AssetScriptingInterface::getAsset(ScriptValuePointer options, ScriptValuePo · 52 unmodified lines
345345 * @property {boolean} [wasRedirected] - <code>true</code> if the resolved data is for the baked version of the asset,
346346 * <code>false</code> if it isn't.
347347 */
348−void AssetScriptingInterface::resolveAsset(ScriptValuePointer options, ScriptValuePointer scope, ScriptValuePointer callback) {
348+void AssetScriptingInterface::resolveAsset(const ScriptValue& options, const ScriptValue& scope, const ScriptValue& callback) {
349349 const QString& URL{ "url" };
350350
351− auto url = (options->isString() ? options : options->property(URL))->toString();
351+ auto url = (options.isString() ? options : options.property(URL)).toString();
352352 auto asset = AssetUtils::getATPUrl(url).path();
353353
354354 JS_VERIFY(AssetUtils::isValidFilePath(asset) || AssetUtils::isValidHash(asset),
@@ -372,10 +372,10 @@ void AssetScriptingInterface::resolveAsset(ScriptValuePointer options, ScriptVal · 17 unmodified lines
372372 * @property {string|object|ArrayBuffer} [response] - The decompressed data.
373373 * @property {Assets.ResponseType} [responseType] - The type of the decompressed data in <code>response</code>.
374374 */
375−void AssetScriptingInterface::decompressData(ScriptValuePointer options, ScriptValuePointer scope, ScriptValuePointer callback) {
376− auto data = options->property("data");
375+void AssetScriptingInterface::decompressData(const ScriptValue& options, const ScriptValue& scope, const ScriptValue& callback) {
376+ auto data = options.property("data");
377377 QByteArray dataByteArray = scriptvalue_cast<QByteArray>(data);
378− auto responseType = options->property("responseType")->toString().toLower();
378+ auto responseType = options.property("responseType").toString().toLower();
379379 if (responseType.isEmpty()) {
380380 responseType = "text";
381381 }
@@ -413,10 +413,10 @@ namespace { · 31 unmodified lines
413413 * @property {string} [contentType] - The MIME type of the compressed data, i.e., <code>"application/gzip"</code>.
414414 * @property {ArrayBuffer} [data] - The compressed data.
415415 */
416−void AssetScriptingInterface::compressData(ScriptValuePointer options, ScriptValuePointer scope, ScriptValuePointer callback) {
417− auto data = options->property("data")->isValid() ? options->property("data") : options;
418− QByteArray dataByteArray = data->isString() ? data->toString().toUtf8() : scriptvalue_cast<QByteArray>(data);
419− int level = options->property("level")->isNumber() ? options->property("level")->toInt32() : DEFAULT_GZIP_COMPRESSION_LEVEL;
416+void AssetScriptingInterface::compressData(const ScriptValue& options, const ScriptValue& scope, const ScriptValue& callback) {
417+ auto data = options.property("data").isValid() ? options.property("data") : options;
418+ QByteArray dataByteArray = data.isString() ? data.toString().toUtf8() : scriptvalue_cast<QByteArray>(data);
419+ int level = options.property("level").isNumber() ? options.property("level").toInt32() : DEFAULT_GZIP_COMPRESSION_LEVEL;
420420 JS_VERIFY(level >= DEFAULT_GZIP_COMPRESSION_LEVEL || level <= MAX_GZIP_COMPRESSION_LEVEL, QString("invalid .level %1").arg(level));
421421 jsPromiseReady(compressBytes(dataByteArray, level), scope, callback);
422422 }
@@ -442,18 +442,18 @@ void AssetScriptingInterface::compressData(ScriptValuePointer options, ScriptVal · 19 unmodified lines
442442 * @property {string} [url] - The <code>atp:</code> URL of the content: using the path if specified, otherwise the hash.
443443 * @property {string} [path] - The uploaded content's mapped path, if specified.
444444 */
445−void AssetScriptingInterface::putAsset(ScriptValuePointer options, ScriptValuePointer scope, ScriptValuePointer callback) {
446− auto compress = options->property("compress")->toBool() || options->property("compressed")->toBool();
447− auto data = options->isObject() ? options->property("data") : options;
448− auto rawPath = options->property("path")->toString();
445+void AssetScriptingInterface::putAsset(const ScriptValue& options, const ScriptValue& scope, const ScriptValue& callback) {
446+ auto compress = options.property("compress").toBool() || options.property("compressed").toBool();
447+ auto data = options.isObject() ? options.property("data") : options;
448+ auto rawPath = options.property("path").toString();
449449 auto path = AssetUtils::getATPUrl(rawPath).path();
450450
451− QByteArray dataByteArray = data->isString() ? data->toString().toUtf8() : scriptvalue_cast<QByteArray>(data);
451+ QByteArray dataByteArray = data.isString() ? data.toString().toUtf8() : scriptvalue_cast<QByteArray>(data);
452452
453453 JS_VERIFY(path.isEmpty() || AssetUtils::isValidFilePath(path),
454454 QString("expected valid ATP file path '%1' ('%2')").arg(rawPath).arg(path));
455455 JS_VERIFY(dataByteArray.size() > 0,
456− QString("expected non-zero .data (got %1 / #%2 bytes)").arg(data->toVariant().typeName()).arg(dataByteArray.size()));
456+ QString("expected non-zero .data (got %1 / #%2 bytes)").arg(data.toVariant().typeName()).arg(dataByteArray.size()));
457457
458458 // [compressed] => uploaded to server => [mapped to path]
459459 Promise prepared = makePromise("putAsset::prepared");
@@ -498,8 +498,8 @@ void AssetScriptingInterface::putAsset(ScriptValuePointer options, ScriptValuePo · 38 unmodified lines
498498 * @property {string} url - The URL of the cached asset to get information on. Must start with <code>"atp:"</code> or
499499 * <code>"cache:"</code>.
500500 */
501−void AssetScriptingInterface::queryCacheMeta(ScriptValuePointer options, ScriptValuePointer scope, ScriptValuePointer callback) {
502− QString url = options->isString() ? options->toString() : options->property("url")->toString();
501+void AssetScriptingInterface::queryCacheMeta(const ScriptValue& options, const ScriptValue& scope, const ScriptValue& callback) {
502+ QString url = options.isString() ? options.toString() : options.property("url").toString();
503503 JS_VERIFY(QUrl(url).isValid(), QString("Invalid URL '%1'").arg(url));
504504 jsPromiseReady(Parent::queryCacheMeta(url), scope, callback);
505505 }
@@ -513,16 +513,16 @@ void AssetScriptingInterface::queryCacheMeta(ScriptValuePointer options, ScriptV · 7 unmodified lines
513513 * @property {string} url - The URL of the asset to load from cache. Must start with <code>"atp:"</code> or
514514 * <code>"cache:"</code>.
515515 */
516−void AssetScriptingInterface::loadFromCache(ScriptValuePointer options, ScriptValuePointer scope, ScriptValuePointer callback) {
516+void AssetScriptingInterface::loadFromCache(const ScriptValue& options, const ScriptValue& scope, const ScriptValue& callback) {
517517 QString url, responseType;
518518 bool decompress = false;
519− if (options->isString()) {
520− url = options->toString();
519+ if (options.isString()) {
520+ url = options.toString();
521521 responseType = "text";
522522 } else {
523− url = options->property("url")->toString();
524− responseType = options->property("responseType")->isValid() ? options->property("responseType")->toString() : "text";
525− decompress = options->property("decompress")->toBool() || options->property("compressed")->toBool();
523+ url = options.property("url").toString();
524+ responseType = options.property("responseType").isValid() ? options.property("responseType").toString() : "text";
525+ decompress = options.property("decompress").toBool() || options.property("compressed").toBool();
526526 }
527527 JS_VERIFY(QUrl(url).isValid(), QString("Invalid URL '%1'").arg(url));
528528 JS_VERIFY(RESPONSE_TYPES.contains(responseType),
@@ -555,17 +555,21 @@ bool AssetScriptingInterface::canWriteCacheValue(const QUrl& url) { · 26 unmodified lines
555555 * @property {string} [url] - The URL to associate with the cache item. Must start with <code>"atp:"</code> or
556556 * <code>"cache:"</code>. If not specified, the URL is <code>"atp:"</code> followed by the SHA256 hash of the content.
557557 */
558−void AssetScriptingInterface::saveToCache(ScriptValuePointer options, ScriptValuePointer scope, ScriptValuePointer callback) {
559− JS_VERIFY(options->isObject(), QString("expected options object as first parameter not: %1").arg(options->toVariant().typeName()));
558+void AssetScriptingInterface::saveToCache(const ScriptValue& options, const ScriptValue& scope, const ScriptValue& callback) {
559+ JS_VERIFY(options.isObject(), QString("expected options object as first parameter not: %1").arg(options.toVariant().typeName()));
560560
561− QString url = options->property("url")->toString();
562− QByteArray data = scriptvalue_cast<QByteArray>(options->property("data"));
563− QVariantMap headers = scriptvalue_cast<QVariantMap>(options->property("headers"));
561+ QString url = options.property("url").toString();
562+ QByteArray data = scriptvalue_cast<QByteArray>(options.property("data"));
563+ QVariantMap headers = scriptvalue_cast<QVariantMap>(options.property("headers"));
564564
565565 saveToCache(url, data, headers, scope, callback);
566566 }
567567
568−void AssetScriptingInterface::saveToCache(const QUrl& rawURL, const QByteArray& data, const QVariantMap& metadata, ScriptValuePointer scope, ScriptValuePointer callback) {
568+void AssetScriptingInterface::saveToCache(const QUrl& rawURL,
569+ const QByteArray& data,
570+ const QVariantMap& metadata,
571+ const ScriptValue& scope,
572+ const ScriptValue& callback) {
569573 QUrl url = rawURL;
570574 if (url.path().isEmpty() && !data.isEmpty()) {
571575 // generate a valid ATP URL from the data -- appending any existing fragment or querystring values