1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 07:47:37 +00:00

LibJS: Annotate Promise implementation with spec comments

I wanted to do this for a long time. The guts of Promise are pretty
complex, and it's easier to understand with the spec right next to it.
Also found a couple of issues along the way :^)
This commit is contained in:
Linus Groh 2021-11-14 00:33:16 +00:00
parent df06552b48
commit 01c2570678
7 changed files with 523 additions and 41 deletions

View file

@ -29,47 +29,78 @@ PromiseReactionJob::PromiseReactionJob(PromiseReaction& reaction, Value argument
ThrowCompletionOr<Value> PromiseReactionJob::call()
{
auto& vm = this->vm();
// a. Let promiseCapability be reaction.[[Capability]].
auto& promise_capability = m_reaction.capability();
// b. Let type be reaction.[[Type]].
auto type = m_reaction.type();
// c. Let handler be reaction.[[Handler]].
auto handler = m_reaction.handler();
Value handler_result;
// d. If handler is empty, then
if (!handler.has_value()) {
dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Handler is empty", this);
switch (type) {
case PromiseReaction::Type::Fulfill:
// i. If type is Fulfill, let handlerResult be NormalCompletion(argument).
if (type == PromiseReaction::Type::Fulfill) {
dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Reaction type is Type::Fulfill, setting handler result to {}", this, m_argument);
handler_result = m_argument;
break;
case PromiseReaction::Type::Reject:
}
// ii. Else,
else {
// 1. Assert: type is Reject.
VERIFY(type == PromiseReaction::Type::Reject);
// 2. Let handlerResult be ThrowCompletion(argument).
// NOTE: handler_result is set to exception value further below
dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Reaction type is Type::Reject, throwing exception with argument {}", this, m_argument);
vm.throw_exception(global_object(), m_argument);
// handler_result is set to exception value further below
break;
}
} else {
}
// e. Else, let handlerResult be HostCallJobCallback(handler, undefined, « argument »).
else {
dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Calling handler callback {} @ {} with argument {}", this, handler.value().callback->class_name(), handler.value().callback, m_argument);
handler_result = call_job_callback(vm, handler.value(), js_undefined(), m_argument);
}
// f. If promiseCapability is undefined, then
if (!promise_capability.has_value()) {
// i. Assert: handlerResult is not an abrupt completion.
VERIFY(!vm.exception());
// ii. Return NormalCompletion(empty).
dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Reaction has no PromiseCapability, returning empty value", this);
// TODO: This can't return an empty value at the moment, because the implicit conversion to Completion would fail.
// Change it back when this is using completions (`return normal_completion({})`)
return js_undefined();
}
// g. Assert: promiseCapability is a PromiseCapability Record.
// h. If handlerResult is an abrupt completion, then
if (vm.exception()) {
handler_result = vm.exception()->value();
vm.clear_exception();
vm.stop_unwind();
// i. Let status be Call(promiseCapability.[[Reject]], undefined, « handlerResult.[[Value]] »).
auto* reject_function = promise_capability.value().reject;
dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Calling PromiseCapability's reject function @ {}", this, reject_function);
return vm.call(*reject_function, js_undefined(), handler_result);
} else {
}
// i. Else,
else {
// i. Let status be Call(promiseCapability.[[Resolve]], undefined, « handlerResult.[[Value]] »).
auto* resolve_function = promise_capability.value().resolve;
dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Calling PromiseCapability's resolve function @ {}", this, resolve_function);
return vm.call(*resolve_function, js_undefined(), handler_result);
}
// j. Return Completion(status).
}
void PromiseReactionJob::visit_edges(Visitor& visitor)
@ -97,16 +128,29 @@ PromiseResolveThenableJob::PromiseResolveThenableJob(Promise& promise_to_resolve
ThrowCompletionOr<Value> PromiseResolveThenableJob::call()
{
auto& vm = this->vm();
// a. Let resolvingFunctions be CreateResolvingFunctions(promiseToResolve).
auto [resolve_function, reject_function] = m_promise_to_resolve.create_resolving_functions();
// b. Let thenCallResult be HostCallJobCallback(then, thenable, « resolvingFunctions.[[Resolve]], resolvingFunctions.[[Reject]] »).
dbgln_if(PROMISE_DEBUG, "[PromiseResolveThenableJob @ {}]: Calling then job callback for thenable {}", this, &m_thenable);
auto then_call_result = call_job_callback(vm, m_then, m_thenable, &resolve_function, &reject_function);
// c. If thenCallResult is an abrupt completion, then
if (vm.exception()) {
auto error = vm.exception()->value();
vm.clear_exception();
vm.stop_unwind();
// i. Let status be Call(resolvingFunctions.[[Reject]], undefined, « thenCallResult.[[Value]] »).
// FIXME: Actually do this... not sure why we don't? :yakfused:
// ii. Return Completion(status).
dbgln_if(PROMISE_DEBUG, "[PromiseResolveThenableJob @ {}]: An exception was thrown, returning error {}", this, error);
return error;
}
// d. Return Completion(thenCallResult).
dbgln_if(PROMISE_DEBUG, "[PromiseResolveThenableJob @ {}]: Returning then call result {}", this, then_call_result);
return then_call_result;
}