Skip to content

Commit

Permalink
LibJS: Have AsyncFunctionDriverWrapper unwrap promises before returning
Browse files Browse the repository at this point in the history
24 new passes on test262. :^)
  • Loading branch information
awesomekling committed Jun 27, 2023
1 parent 9430bbc commit 191e0a7
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions Userland/Libraries/LibJS/Runtime/AsyncFunctionDriverWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,26 @@ void AsyncFunctionDriverWrapper::continue_async_execution(VM& vm, Value value, b
auto promise_value = TRY(result.get(vm, vm.names.value));

if (TRY(result.get(vm, vm.names.done)).to_boolean()) {
// We hit a `return value;`
m_top_level_promise->fulfill(promise_value);

// We should not execute anymore, so we are safe to allow our selfs to be GC'd
// We should not execute anymore, so we are safe to allow ourselves to be GC'd.
m_self_handle = {};

// When returning a promise, we need to unwrap it.
if (promise_value.is_object() && is<Promise>(promise_value.as_object())) {
auto& returned_promise = static_cast<Promise&>(promise_value.as_object());
if (returned_promise.state() == Promise::State::Fulfilled) {
m_top_level_promise->fulfill(returned_promise.result());
return {};
}
if (returned_promise.state() == Promise::State::Rejected)
return throw_completion(returned_promise.result());

// The promise is still pending but there's nothing more to do here.
return {};
}

// We hit a `return value;`
m_top_level_promise->fulfill(promise_value);
return {};
}

Expand Down

0 comments on commit 191e0a7

Please sign in to comment.