ladybird/Userland/Libraries/LibJS/Runtime/PromiseReaction.cpp
Linus Groh 7327a28ccc LibJS: Add ECMA-262 section/title/URL comments almost everywhere
As mentioned on Discord earlier, we'll add these to all new functions
going forward - this is the backfill. Reasons:

- It makes you look at the spec, implementing based on MDN or V8
  behavior is a no-go
- It makes finding the various functions that are non-compliant easier,
  in the future everything should either have such a comment or, if it's
  not from the spec at all, a comment explaining why that is the case
- It makes it easier to check whether a certain abstract operation is
  implemented in LibJS, not all of them use the same name as the spec.
  E.g. RejectPromise() is Promise::reject()
- It makes it easier to reason about vm.arguments(), e.g. when the
  function has a rest parameter
- It makes it easier to see whether a certain function is from a
  proposal or Annex B

Also:

- Add arguments to all functions and abstract operations that already
  had a comment
- Fix some outdated section numbers
- Replace some ecma-international.org URLs with tc39.es
2021-06-13 00:33:28 +01:00

96 lines
3.5 KiB
C++

/*
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/MarkedValueList.h>
#include <LibJS/Runtime/NativeFunction.h>
#include <LibJS/Runtime/PromiseReaction.h>
namespace JS {
// 27.2.1.5 NewPromiseCapability ( C ), https://tc39.es/ecma262/#sec-newpromisecapability
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 {};
}
struct {
Value resolve { js_undefined() };
Value reject { js_undefined() };
} promise_capability_functions;
auto* executor = NativeFunction::create(global_object, "", [&promise_capability_functions](auto& vm, auto& global_object) -> Value {
auto resolve = vm.argument(0);
auto reject = vm.argument(1);
// No idea what other engines say here.
if (!promise_capability_functions.resolve.is_undefined()) {
vm.template throw_exception<TypeError>(global_object, ErrorType::GetCapabilitiesExecutorCalledMultipleTimes);
return {};
}
if (!promise_capability_functions.resolve.is_undefined()) {
vm.template throw_exception<TypeError>(global_object, ErrorType::GetCapabilitiesExecutorCalledMultipleTimes);
return {};
}
promise_capability_functions.resolve = resolve;
promise_capability_functions.reject = reject;
return js_undefined();
});
executor->define_property(vm.names.length, Value(2));
MarkedValueList arguments(vm.heap());
arguments.append(executor);
auto promise = vm.construct(constructor.as_function(), constructor.as_function(), move(arguments));
if (vm.exception())
return {};
// I'm not sure if we could VERIFY(promise.is_object()) instead - the spec doesn't have this check...
if (!promise.is_object()) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, promise.to_string_without_side_effects());
return {};
}
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 {};
}
return {
&promise.as_object(),
&promise_capability_functions.resolve.as_function(),
&promise_capability_functions.reject.as_function(),
};
}
PromiseReaction::PromiseReaction(Type type, Optional<PromiseCapability> capability, Optional<JobCallback> handler)
: m_type(type)
, m_capability(move(capability))
, m_handler(move(handler))
{
}
void PromiseReaction::visit_edges(Cell::Visitor& visitor)
{
Cell::visit_edges(visitor);
if (m_capability.has_value()) {
auto& capability = m_capability.value();
visitor.visit(capability.promise);
visitor.visit(capability.resolve);
visitor.visit(capability.reject);
}
if (m_handler.has_value()) {
auto& handler = m_handler.value();
visitor.visit(handler.callback);
}
}
}