LibJS: Convert the PromiseResolve AO to ThrowCompletionOr

This commit is contained in:
Idan Horowitz 2021-10-23 03:44:07 +03:00 committed by Andreas Kling
parent 0d602c5ec5
commit 81bdb20c61
Notes: sideshowbarker 2024-07-18 01:59:55 +09:00
4 changed files with 8 additions and 12 deletions

View file

@ -19,16 +19,16 @@
namespace JS { namespace JS {
// 27.2.4.7.1 PromiseResolve ( C, x ), https://tc39.es/ecma262/#sec-promise-resolve // 27.2.4.7.1 PromiseResolve ( C, x ), https://tc39.es/ecma262/#sec-promise-resolve
Object* promise_resolve(GlobalObject& global_object, Object& constructor, Value value) ThrowCompletionOr<Object*> promise_resolve(GlobalObject& global_object, Object& constructor, Value value)
{ {
auto& vm = global_object.vm(); auto& vm = global_object.vm();
if (value.is_object() && is<Promise>(value.as_object())) { if (value.is_object() && is<Promise>(value.as_object())) {
auto value_constructor = TRY_OR_DISCARD(value.as_object().get(vm.names.constructor)); auto value_constructor = TRY(value.as_object().get(vm.names.constructor));
if (same_value(value_constructor, &constructor)) if (same_value(value_constructor, &constructor))
return &static_cast<Promise&>(value.as_object()); return &static_cast<Promise&>(value.as_object());
} }
auto promise_capability = TRY_OR_DISCARD(new_promise_capability(global_object, &constructor)); auto promise_capability = TRY(new_promise_capability(global_object, &constructor));
[[maybe_unused]] auto result = TRY_OR_DISCARD(vm.call(*promise_capability.resolve, js_undefined(), value)); (void)TRY(vm.call(*promise_capability.resolve, js_undefined(), value));
return promise_capability.promise; return promise_capability.promise;
} }

View file

@ -11,7 +11,7 @@
namespace JS { namespace JS {
Object* promise_resolve(GlobalObject&, Object& constructor, Value); ThrowCompletionOr<Object*> promise_resolve(GlobalObject&, Object& constructor, Value);
class Promise final : public Object { class Promise final : public Object {
JS_OBJECT(Promise, Object); JS_OBJECT(Promise, Object);

View file

@ -357,7 +357,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(PromiseConstructor::resolve)
{ {
auto* constructor = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object)); auto* constructor = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object));
auto value = vm.argument(0); auto value = vm.argument(0);
return promise_resolve(global_object, *constructor, value); return TRY_OR_DISCARD(promise_resolve(global_object, *constructor, value));
} }
// 27.2.4.8 get Promise [ @@species ], https://tc39.es/ecma262/#sec-get-promise-@@species // 27.2.4.8 get Promise [ @@species ], https://tc39.es/ecma262/#sec-get-promise-@@species

View file

@ -72,9 +72,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(PromisePrototype::finally)
auto& on_finally = const_cast<FunctionObject&>(*on_finally_handle.cell()); auto& on_finally = const_cast<FunctionObject&>(*on_finally_handle.cell());
auto value = vm.argument(0); auto value = vm.argument(0);
auto result = TRY(vm.call(on_finally, js_undefined())); auto result = TRY(vm.call(on_finally, js_undefined()));
auto* promise = promise_resolve(global_object, constructor, result); auto* promise = TRY(promise_resolve(global_object, constructor, result));
if (auto* exception = vm.exception())
return throw_completion(exception->value());
auto* value_thunk = NativeFunction::create(global_object, "", [value](auto&, auto&) -> ThrowCompletionOr<Value> { auto* value_thunk = NativeFunction::create(global_object, "", [value](auto&, auto&) -> ThrowCompletionOr<Value> {
return value; return value;
}); });
@ -88,9 +86,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(PromisePrototype::finally)
auto& on_finally = const_cast<FunctionObject&>(*on_finally_handle.cell()); auto& on_finally = const_cast<FunctionObject&>(*on_finally_handle.cell());
auto reason = vm.argument(0); auto reason = vm.argument(0);
auto result = TRY(vm.call(on_finally, js_undefined())); auto result = TRY(vm.call(on_finally, js_undefined()));
auto* promise = promise_resolve(global_object, constructor, result); auto* promise = TRY(promise_resolve(global_object, constructor, result));
if (auto* exception = vm.exception())
return throw_completion(exception->value());
auto* thrower = NativeFunction::create(global_object, "", [reason](auto& vm, auto& global_object) -> ThrowCompletionOr<Value> { auto* thrower = NativeFunction::create(global_object, "", [reason](auto& vm, auto& global_object) -> ThrowCompletionOr<Value> {
vm.throw_exception(global_object, reason); vm.throw_exception(global_object, reason);
return throw_completion(reason); return throw_completion(reason);