From a4d85cd522e22750ff6449d21f3fbb9c9f86c7c1 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Tue, 13 Dec 2022 20:49:50 +0000 Subject: [PATCH] LibJS: Convert Promise::create() to NonnullGCPtr --- .../LibWeb/BindingsGenerator/IDLGenerators.cpp | 2 +- Userland/Libraries/LibJS/Module.cpp | 2 +- .../Libraries/LibJS/Runtime/AsyncFunctionDriverWrapper.cpp | 2 +- Userland/Libraries/LibJS/Runtime/Completion.cpp | 2 +- Userland/Libraries/LibJS/Runtime/Promise.cpp | 4 ++-- Userland/Libraries/LibJS/Runtime/Promise.h | 2 +- Userland/Libraries/LibJS/Runtime/PromiseCapability.cpp | 2 +- Userland/Libraries/LibJS/Runtime/PromiseConstructor.cpp | 2 +- Userland/Libraries/LibJS/Runtime/PromisePrototype.cpp | 6 +++--- Userland/Libraries/LibJS/Runtime/VM.cpp | 4 ++-- Userland/Libraries/LibJS/SyntheticModule.cpp | 4 ++-- Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp | 6 +++--- Userland/Libraries/LibWeb/FileAPI/Blob.cpp | 4 ++-- Userland/Libraries/LibWeb/HTML/Scripting/ModuleScript.cpp | 4 ++-- Userland/Libraries/LibWeb/WebDriver/ExecuteScript.cpp | 2 +- Userland/Libraries/LibWeb/WebIDL/Promise.cpp | 4 ++-- 16 files changed, 26 insertions(+), 26 deletions(-) diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp index 18dc29d56ca..2def7e86f4a 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp @@ -520,7 +520,7 @@ static void generate_to_cpp(SourceGenerator& generator, ParameterType& parameter // anything of this sort. Both Gecko and Blink do it, however, so I'm sure it's correct. scoped_generator.append(R"~~~( if (!@js_name@@js_suffix@.is_object() || !is(@js_name@@js_suffix@.as_object())) { - auto* new_promise = JS::Promise::create(realm); + auto new_promise = JS::Promise::create(realm); new_promise->fulfill(@js_name@@js_suffix@); @js_name@@js_suffix@ = new_promise; } diff --git a/Userland/Libraries/LibJS/Module.cpp b/Userland/Libraries/LibJS/Module.cpp index e0315a8b0da..986c070d790 100644 --- a/Userland/Libraries/LibJS/Module.cpp +++ b/Userland/Libraries/LibJS/Module.cpp @@ -47,7 +47,7 @@ ThrowCompletionOr Module::inner_module_evaluation(VM& vm, Vector&, { // 1. If module is not a Cyclic Module Record, then // a. Let promise be ! module.Evaluate(). - auto* promise = TRY(evaluate(vm)); + auto promise = TRY(evaluate(vm)); // b. Assert: promise.[[PromiseState]] is not pending. VERIFY(promise->state() != Promise::State::Pending); diff --git a/Userland/Libraries/LibJS/Runtime/AsyncFunctionDriverWrapper.cpp b/Userland/Libraries/LibJS/Runtime/AsyncFunctionDriverWrapper.cpp index 36c93b2c41d..e3afe6ec5c9 100644 --- a/Userland/Libraries/LibJS/Runtime/AsyncFunctionDriverWrapper.cpp +++ b/Userland/Libraries/LibJS/Runtime/AsyncFunctionDriverWrapper.cpp @@ -56,7 +56,7 @@ ThrowCompletionOr AsyncFunctionDriverWrapper::react_to_async_task_complet return promise; } - auto* promise = static_cast(&promise_value.as_object()); + auto promise = static_cast(&promise_value.as_object()); if (TRY(result.get(vm, vm.names.done)).to_boolean()) return promise; diff --git a/Userland/Libraries/LibJS/Runtime/Completion.cpp b/Userland/Libraries/LibJS/Runtime/Completion.cpp index 3b1db9a70b2..f5f5b94df47 100644 --- a/Userland/Libraries/LibJS/Runtime/Completion.cpp +++ b/Userland/Libraries/LibJS/Runtime/Completion.cpp @@ -89,7 +89,7 @@ ThrowCompletionOr await(VM& vm, Value value) auto on_rejected = NativeFunction::create(realm, move(rejected_closure), 1, ""); // 7. Perform PerformPromiseThen(promise, onFulfilled, onRejected). - auto* promise = verify_cast(promise_object); + auto promise = verify_cast(promise_object); promise->perform_then(on_fulfilled, on_rejected, {}); // FIXME: Since we don't support context suspension, we attempt to "wait" for the promise to resolve diff --git a/Userland/Libraries/LibJS/Runtime/Promise.cpp b/Userland/Libraries/LibJS/Runtime/Promise.cpp index 3d62d1fa42a..8fff28c0480 100644 --- a/Userland/Libraries/LibJS/Runtime/Promise.cpp +++ b/Userland/Libraries/LibJS/Runtime/Promise.cpp @@ -42,9 +42,9 @@ ThrowCompletionOr promise_resolve(VM& vm, Object& constructor, Value va return promise_capability->promise().ptr(); } -Promise* Promise::create(Realm& realm) +NonnullGCPtr Promise::create(Realm& realm) { - return realm.heap().allocate(realm, *realm.intrinsics().promise_prototype()); + return *realm.heap().allocate(realm, *realm.intrinsics().promise_prototype()); } // 27.2 Promise Objects, https://tc39.es/ecma262/#sec-promise-objects diff --git a/Userland/Libraries/LibJS/Runtime/Promise.h b/Userland/Libraries/LibJS/Runtime/Promise.h index 790e3482e46..8ec2a870007 100644 --- a/Userland/Libraries/LibJS/Runtime/Promise.h +++ b/Userland/Libraries/LibJS/Runtime/Promise.h @@ -27,7 +27,7 @@ public: Handle, }; - static Promise* create(Realm&); + static NonnullGCPtr create(Realm&); virtual ~Promise() = default; diff --git a/Userland/Libraries/LibJS/Runtime/PromiseCapability.cpp b/Userland/Libraries/LibJS/Runtime/PromiseCapability.cpp index 0d57ea1f089..7a4f1565c54 100644 --- a/Userland/Libraries/LibJS/Runtime/PromiseCapability.cpp +++ b/Userland/Libraries/LibJS/Runtime/PromiseCapability.cpp @@ -78,7 +78,7 @@ ThrowCompletionOr> new_promise_capability(VM& vm auto executor = NativeFunction::create(realm, move(executor_closure), 2, ""); // 6. Let promise be ? Construct(C, « executor »). - auto* promise = TRY(construct(vm, constructor.as_function(), executor)); + auto promise = TRY(construct(vm, constructor.as_function(), executor)); // 7. If IsCallable(promiseCapability.[[Resolve]]) is false, throw a TypeError exception. // NOTE: We only assign a value in the executor closure if it is a function. diff --git a/Userland/Libraries/LibJS/Runtime/PromiseConstructor.cpp b/Userland/Libraries/LibJS/Runtime/PromiseConstructor.cpp index 7aab263eb6b..ad87333c1d6 100644 --- a/Userland/Libraries/LibJS/Runtime/PromiseConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/PromiseConstructor.cpp @@ -290,7 +290,7 @@ ThrowCompletionOr PromiseConstructor::construct(FunctionObject& new_tar // 5. Set promise.[[PromiseFulfillReactions]] to a new empty List. // 6. Set promise.[[PromiseRejectReactions]] to a new empty List. // 7. Set promise.[[PromiseIsHandled]] to false. - auto* promise = TRY(ordinary_create_from_constructor(vm, new_target, &Intrinsics::promise_prototype)); + auto promise = TRY(ordinary_create_from_constructor(vm, new_target, &Intrinsics::promise_prototype)); // 8. Let resolvingFunctions be CreateResolvingFunctions(promise). auto [resolve_function, reject_function] = promise->create_resolving_functions(); diff --git a/Userland/Libraries/LibJS/Runtime/PromisePrototype.cpp b/Userland/Libraries/LibJS/Runtime/PromisePrototype.cpp index fd0b05f5432..721969639dc 100644 --- a/Userland/Libraries/LibJS/Runtime/PromisePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/PromisePrototype.cpp @@ -45,7 +45,7 @@ JS_DEFINE_NATIVE_FUNCTION(PromisePrototype::then) // 1. Let promise be the this value. // 2. If IsPromise(promise) is false, throw a TypeError exception. - auto* promise = TRY(typed_this_object(vm)); + auto promise = TRY(typed_this_object(vm)); // 3. Let C be ? SpeciesConstructor(promise, %Promise%). auto* constructor = TRY(species_constructor(vm, *promise, *realm.intrinsics().promise_constructor())); @@ -113,7 +113,7 @@ JS_DEFINE_NATIVE_FUNCTION(PromisePrototype::finally) auto result = TRY(call(vm, on_finally, js_undefined())); // ii. Let promise be ? PromiseResolve(C, result). - auto* promise = TRY(promise_resolve(vm, constructor, result)); + auto promise = TRY(promise_resolve(vm, constructor, result)); // iii. Let returnValue be a new Abstract Closure with no parameters that captures value and performs the following steps when called: auto return_value = [value_handle = make_handle(value)](auto&) -> ThrowCompletionOr { @@ -142,7 +142,7 @@ JS_DEFINE_NATIVE_FUNCTION(PromisePrototype::finally) auto result = TRY(call(vm, on_finally, js_undefined())); // ii. Let promise be ? PromiseResolve(C, result). - auto* promise = TRY(promise_resolve(vm, constructor, result)); + auto promise = TRY(promise_resolve(vm, constructor, result)); // iii. Let throwReason be a new Abstract Closure with no parameters that captures reason and performs the following steps when called: auto throw_reason = [reason_handle = make_handle(reason)](auto&) -> ThrowCompletionOr { diff --git a/Userland/Libraries/LibJS/Runtime/VM.cpp b/Userland/Libraries/LibJS/Runtime/VM.cpp index f60e2f07a69..a65f8cbb06c 100644 --- a/Userland/Libraries/LibJS/Runtime/VM.cpp +++ b/Userland/Libraries/LibJS/Runtime/VM.cpp @@ -75,7 +75,7 @@ VM::VM(OwnPtr custom_data) // By default, we throw on dynamic imports this is to prevent arbitrary file access by scripts. VERIFY(current_realm()); auto& realm = *current_realm(); - auto* promise = Promise::create(realm); + auto promise = Promise::create(realm); // If you are here because you want to enable dynamic module importing make sure it won't be a security problem // by checking the default implementation of HostImportModuleDynamically and creating your own hook or calling @@ -965,7 +965,7 @@ void VM::import_module_dynamically(ScriptOrModule referencing_script_or_module, // FinishDynamicImport(referencingScriptOrModule, moduleRequest, promiseCapability, promise), // where promise is a Promise rejected with an error representing the cause of failure. - auto* promise = Promise::create(realm); + auto promise = Promise::create(realm); ScopeGuard finish_dynamic_import = [&] { host_finish_dynamic_import(referencing_script_or_module, module_request, promise_capability, promise); diff --git a/Userland/Libraries/LibJS/SyntheticModule.cpp b/Userland/Libraries/LibJS/SyntheticModule.cpp index c446f93ca5e..c9802a25a6a 100644 --- a/Userland/Libraries/LibJS/SyntheticModule.cpp +++ b/Userland/Libraries/LibJS/SyntheticModule.cpp @@ -107,7 +107,7 @@ ThrowCompletionOr SyntheticModule::evaluate(VM& vm) // 12. Return Completion(result). // Note: Because we expect it to return a promise we convert this here. - auto* promise = Promise::create(realm()); + auto promise = Promise::create(realm()); if (result.is_error()) { VERIFY(result.throw_completion().value().has_value()); promise->reject(*result.throw_completion().value()); @@ -115,7 +115,7 @@ ThrowCompletionOr SyntheticModule::evaluate(VM& vm) // Note: This value probably isn't visible to JS code? But undefined is fine anyway. promise->fulfill(js_undefined()); } - return promise; + return promise.ptr(); } // 1.2.2 SetSyntheticModuleExport ( module, exportName, exportValue ), https://tc39.es/proposal-json-modules/#sec-setsyntheticmoduleexport diff --git a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp index 37edb0b28ce..ef0f18e829a 100644 --- a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp +++ b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp @@ -38,7 +38,7 @@ JS::Promise* SubtleCrypto::digest(DeprecatedString const& algorithm, JS::Handle< auto data_buffer_or_error = WebIDL::get_buffer_source_copy(*data.cell()); if (data_buffer_or_error.is_error()) { auto error = WebIDL::OperationError::create(realm, "Failed to copy bytes from ArrayBuffer"); - auto* promise = JS::Promise::create(realm); + auto promise = JS::Promise::create(realm); promise->reject(error.ptr()); return promise; } @@ -59,13 +59,13 @@ JS::Promise* SubtleCrypto::digest(DeprecatedString const& algorithm, JS::Handle< // 4. If an error occurred, return a Promise rejected with normalizedAlgorithm. else { auto error = WebIDL::NotSupportedError::create(realm, DeprecatedString::formatted("Invalid hash function '{}'", algorithm)); - auto* promise = JS::Promise::create(realm); + auto promise = JS::Promise::create(realm); promise->reject(error.ptr()); return promise; } // 5. Let promise be a new Promise. - auto* promise = JS::Promise::create(realm); + auto promise = JS::Promise::create(realm); // 6. Return promise and perform the remaining steps in parallel. // FIXME: We don't have a good abstraction for this yet, so we do it in sync. diff --git a/Userland/Libraries/LibWeb/FileAPI/Blob.cpp b/Userland/Libraries/LibWeb/FileAPI/Blob.cpp index 485ec4d3641..fbff0374661 100644 --- a/Userland/Libraries/LibWeb/FileAPI/Blob.cpp +++ b/Userland/Libraries/LibWeb/FileAPI/Blob.cpp @@ -244,7 +244,7 @@ JS::Promise* Blob::text() // FIXME: We still need to implement ReadableStream for this step to be fully valid. // 3. Let promise be the result of reading all bytes from stream with reader - auto* promise = JS::Promise::create(realm()); + auto promise = JS::Promise::create(realm()); auto result = JS::PrimitiveString::create(vm(), DeprecatedString { m_byte_buffer.bytes() }); // 4. Return the result of transforming promise by a fulfillment handler that returns the result of running UTF-8 decode on its first argument. @@ -260,7 +260,7 @@ JS::Promise* Blob::array_buffer() // FIXME: We still need to implement ReadableStream for this step to be fully valid. // 3. Let promise be the result of reading all bytes from stream with reader. - auto* promise = JS::Promise::create(realm()); + auto promise = JS::Promise::create(realm()); auto buffer_result = JS::ArrayBuffer::create(realm(), m_byte_buffer.size()); if (buffer_result.is_error()) { promise->reject(buffer_result.release_error().value().release_value()); diff --git a/Userland/Libraries/LibWeb/HTML/Scripting/ModuleScript.cpp b/Userland/Libraries/LibWeb/HTML/Scripting/ModuleScript.cpp index b0536be3b70..dfc380302a2 100644 --- a/Userland/Libraries/LibWeb/HTML/Scripting/ModuleScript.cpp +++ b/Userland/Libraries/LibWeb/HTML/Scripting/ModuleScript.cpp @@ -112,7 +112,7 @@ JS::Promise* JavaScriptModuleScript::run(PreventErrorReporting) // 2. Check if we can run script with settings. If this returns "do not run", then return a promise resolved with undefined. if (settings.can_run_script() == RunScriptDecision::DoNotRun) { - auto* promise = JS::Promise::create(settings.realm()); + auto promise = JS::Promise::create(settings.realm()); promise->fulfill(JS::js_undefined()); return promise; } @@ -140,7 +140,7 @@ JS::Promise* JavaScriptModuleScript::run(PreventErrorReporting) // If Evaluate fails to complete as a result of the user agent aborting the running script, // then set evaluationPromise to a promise rejected with a new "QuotaExceededError" DOMException. if (elevation_promise_or_error.is_error()) { - auto* promise = JS::Promise::create(settings_object().realm()); + auto promise = JS::Promise::create(settings_object().realm()); promise->reject(WebIDL::QuotaExceededError::create(settings_object().realm(), "Failed to evaluate module script").ptr()); evaluation_promise = promise; diff --git a/Userland/Libraries/LibWeb/WebDriver/ExecuteScript.cpp b/Userland/Libraries/LibWeb/WebDriver/ExecuteScript.cpp index 350a4d415a3..1f4744f35d4 100644 --- a/Userland/Libraries/LibWeb/WebDriver/ExecuteScript.cpp +++ b/Userland/Libraries/LibWeb/WebDriver/ExecuteScript.cpp @@ -315,7 +315,7 @@ ExecuteScriptResultSerialized execute_async_script(Web::Page& page, DeprecatedSt auto start = Time::now_monotonic(); // 4. Let promise be a new Promise. - auto* promise = JS::Promise::create(realm); + auto promise = JS::Promise::create(realm); // FIXME: 5 Run the following substeps in parallel: auto result = [&] { diff --git a/Userland/Libraries/LibWeb/WebIDL/Promise.cpp b/Userland/Libraries/LibWeb/WebIDL/Promise.cpp index bf98fcda24a..4a8c74a2baf 100644 --- a/Userland/Libraries/LibWeb/WebIDL/Promise.cpp +++ b/Userland/Libraries/LibWeb/WebIDL/Promise.cpp @@ -135,7 +135,7 @@ JS::NonnullGCPtr react_to_promise(JS::PromiseCapability const& prom auto new_capability = MUST(JS::new_promise_capability(vm, constructor)); // 7. Return PerformPromiseThen(promise.[[Promise]], onFulfilled, onRejected, newCapability). - auto* promise = verify_cast(promise_capability.promise().ptr()); + auto promise = verify_cast(promise_capability.promise().ptr()); auto value = promise->perform_then(on_fulfilled, on_rejected, new_capability); return verify_cast(value.as_object()); } @@ -173,7 +173,7 @@ JS::NonnullGCPtr upon_rejection(JS::PromiseCapability const& promis void mark_promise_as_handled(JS::PromiseCapability const& promise_capability) { // To mark as handled a Promise promise, set promise.[[Promise]].[[PromiseIsHandled]] to true. - auto* promise = verify_cast(promise_capability.promise().ptr()); + auto promise = verify_cast(promise_capability.promise().ptr()); promise->set_is_handled(); }