LibJS: Convert the NewPromiseCapability AO to ThrowCompletionOr

This commit is contained in:
Idan Horowitz 2021-10-23 03:34:48 +03:00 committed by Andreas Kling
parent 2b87f77578
commit 0d602c5ec5
Notes: sideshowbarker 2024-07-18 01:59:59 +09:00
6 changed files with 19 additions and 41 deletions

View file

@ -27,9 +27,7 @@ Object* promise_resolve(GlobalObject& global_object, Object& constructor, Value
if (same_value(value_constructor, &constructor))
return &static_cast<Promise&>(value.as_object());
}
auto promise_capability = new_promise_capability(global_object, &constructor);
if (vm.exception())
return nullptr;
auto promise_capability = TRY_OR_DISCARD(new_promise_capability(global_object, &constructor));
[[maybe_unused]] auto result = TRY_OR_DISCARD(vm.call(*promise_capability.resolve, js_undefined(), value));
return promise_capability.promise;
}

View file

@ -263,9 +263,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(PromiseConstructor::all)
{
auto* constructor = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object));
auto promise_capability = new_promise_capability(global_object, constructor);
if (vm.exception())
return {};
auto promise_capability = TRY_OR_DISCARD(new_promise_capability(global_object, constructor));
auto promise_resolve = TRY_OR_REJECT(vm, promise_capability, get_promise_resolve(global_object, constructor));
auto* iterator_record = TRY_OR_REJECT(vm, promise_capability, get_iterator(global_object, vm.argument(0)));
@ -286,9 +284,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(PromiseConstructor::all_settled)
{
auto* constructor = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object));
auto promise_capability = new_promise_capability(global_object, constructor);
if (vm.exception())
return {};
auto promise_capability = TRY_OR_DISCARD(new_promise_capability(global_object, constructor));
auto promise_resolve = TRY_OR_REJECT(vm, promise_capability, get_promise_resolve(global_object, constructor));
auto* iterator_record = TRY_OR_REJECT(vm, promise_capability, get_iterator(global_object, vm.argument(0)));
@ -309,9 +305,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(PromiseConstructor::any)
{
auto* constructor = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object));
auto promise_capability = new_promise_capability(global_object, constructor);
if (vm.exception())
return {};
auto promise_capability = TRY_OR_DISCARD(new_promise_capability(global_object, constructor));
auto promise_resolve = TRY_OR_REJECT(vm, promise_capability, get_promise_resolve(global_object, constructor));
auto* iterator_record = TRY_OR_REJECT(vm, promise_capability, get_iterator(global_object, vm.argument(0)));
@ -332,9 +326,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(PromiseConstructor::race)
{
auto* constructor = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object));
auto promise_capability = new_promise_capability(global_object, constructor);
if (vm.exception())
return {};
auto promise_capability = TRY_OR_DISCARD(new_promise_capability(global_object, constructor));
auto promise_resolve = TRY_OR_REJECT(vm, promise_capability, get_promise_resolve(global_object, constructor));
auto* iterator_record = TRY_OR_REJECT(vm, promise_capability, get_iterator(global_object, vm.argument(0)));
@ -354,9 +346,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(PromiseConstructor::race)
JS_DEFINE_OLD_NATIVE_FUNCTION(PromiseConstructor::reject)
{
auto* constructor = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object));
auto promise_capability = new_promise_capability(global_object, constructor);
if (vm.exception())
return {};
auto promise_capability = TRY_OR_DISCARD(new_promise_capability(global_object, constructor));
auto reason = vm.argument(0);
[[maybe_unused]] auto result = TRY_OR_DISCARD(vm.call(*promise_capability.reject, js_undefined(), reason));
return promise_capability.promise;

View file

@ -42,9 +42,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(PromisePrototype::then)
auto on_fulfilled = vm.argument(0);
auto on_rejected = vm.argument(1);
auto* constructor = TRY_OR_DISCARD(species_constructor(global_object, *promise, *global_object.promise_constructor()));
auto result_capability = new_promise_capability(global_object, constructor);
if (vm.exception())
return {};
auto result_capability = TRY_OR_DISCARD(new_promise_capability(global_object, constructor));
return promise->perform_then(on_fulfilled, on_rejected, result_capability);
}

View file

@ -13,13 +13,11 @@
namespace JS {
// 27.2.1.5 NewPromiseCapability ( C ), https://tc39.es/ecma262/#sec-newpromisecapability
PromiseCapability new_promise_capability(GlobalObject& global_object, Value constructor)
ThrowCompletionOr<PromiseCapability> new_promise_capability(GlobalObject& global_object, Value constructor)
{
auto& vm = global_object.vm();
if (!constructor.is_constructor()) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotAConstructor, constructor.to_string_without_side_effects());
return {};
}
if (!constructor.is_constructor())
return vm.throw_completion<TypeError>(global_object, ErrorType::NotAConstructor, constructor.to_string_without_side_effects());
struct {
Value resolve { js_undefined() };
@ -46,18 +44,14 @@ PromiseCapability new_promise_capability(GlobalObject& global_object, Value cons
MarkedValueList arguments(vm.heap());
arguments.append(executor);
auto* promise = TRY_OR_DISCARD(construct(global_object, constructor.as_function(), move(arguments)));
auto* promise = TRY(construct(global_object, constructor.as_function(), move(arguments)));
if (!promise_capability_functions.resolve.is_function()) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, promise_capability_functions.resolve.to_string_without_side_effects());
return {};
}
if (!promise_capability_functions.reject.is_function()) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, promise_capability_functions.reject.to_string_without_side_effects());
return {};
}
if (!promise_capability_functions.resolve.is_function())
return vm.throw_completion<TypeError>(global_object, ErrorType::NotAFunction, promise_capability_functions.resolve.to_string_without_side_effects());
if (!promise_capability_functions.reject.is_function())
return vm.throw_completion<TypeError>(global_object, ErrorType::NotAFunction, promise_capability_functions.reject.to_string_without_side_effects());
return {
return PromiseCapability {
promise,
&promise_capability_functions.resolve.as_function(),
&promise_capability_functions.reject.as_function(),

View file

@ -20,7 +20,7 @@ struct PromiseCapability {
};
// 27.2.1.5 NewPromiseCapability ( C ), https://tc39.es/ecma262/#sec-newpromisecapability
PromiseCapability new_promise_capability(GlobalObject& global_object, Value constructor);
ThrowCompletionOr<PromiseCapability> new_promise_capability(GlobalObject& global_object, Value constructor);
// 27.2.1.2 PromiseReaction Records, https://tc39.es/ecma262/#sec-promisereaction-records
class PromiseReaction final : public Cell {

View file

@ -161,8 +161,7 @@ ThrowCompletionOr<Value> shadow_realm_import_value(GlobalObject& global_object,
// 5. Assert: evalContext is an execution context associated to a ShadowRealm instance's [[ExecutionContext]].
// 6. Let innerCapability be ! NewPromiseCapability(%Promise%).
auto inner_capability = new_promise_capability(global_object, global_object.promise_constructor());
VERIFY(!vm.exception());
auto inner_capability = MUST(new_promise_capability(global_object, global_object.promise_constructor()));
// 7. Let runningContext be the running execution context.
// 8. If runningContext is not already suspended, suspend runningContext.
@ -245,8 +244,7 @@ ThrowCompletionOr<Value> shadow_realm_import_value(GlobalObject& global_object,
on_fulfilled->define_direct_property(vm.names.name, js_string(vm, String::empty()), Attribute::Configurable);
// 16. Let promiseCapability be ! NewPromiseCapability(%Promise%).
auto promise_capability = new_promise_capability(global_object, global_object.promise_constructor());
VERIFY(!vm.exception());
auto promise_capability = MUST(new_promise_capability(global_object, global_object.promise_constructor()));
// NOTE: Even though the spec tells us to use %ThrowTypeError%, it's not observable if we actually do.
// Throw a nicer TypeError forwarding the import error message instead (we know the argument is an Error object).