1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-28 19:15:09 +00:00

LibJS: Return Optional<T> from Completion::{value,target}(), not T

In the end this is a nicer API than having separate has_{value,target}()
and having to check those first, and then making another Optional from
the unwrapped value:

    completion.has_value() ? completion.value() : Optional<Value> {}
    //                       ^^^^^^^^^^^^^^^^^^
    //         Implicit creation of non-empty Optional<Value>

This way we need to unwrap the optional ourselves, but can easily pass
it to something else as well.

This is in anticipation of the AST using completions :^)
This commit is contained in:
Linus Groh 2021-12-28 17:42:14 +01:00
parent b39aede8fe
commit 85f0fc2b83
15 changed files with 55 additions and 57 deletions

View file

@ -246,23 +246,23 @@ ThrowCompletionOr<Object*> ECMAScriptFunctionObject::internal_construct(MarkedVa
if (result.type() == Completion::Type::Return) {
// FIXME: This is leftover from untangling the call/construct mess - doesn't belong here in any way, but removing it breaks derived classes.
// Likely fixed by making ClassDefinitionEvaluation fully spec compliant.
if (kind == ConstructorKind::Derived && result.value().is_object()) {
if (kind == ConstructorKind::Derived && result.value()->is_object()) {
auto prototype = TRY(new_target.get(vm.names.prototype));
if (prototype.is_object())
TRY(result.value().as_object().internal_set_prototype_of(&prototype.as_object()));
TRY(result.value()->as_object().internal_set_prototype_of(&prototype.as_object()));
}
// EOF (End of FIXME)
// a. If Type(result.[[Value]]) is Object, return NormalCompletion(result.[[Value]]).
if (result.value().is_object())
return &result.value().as_object();
if (result.value()->is_object())
return &result.value()->as_object();
// b. If kind is base, return NormalCompletion(thisArgument).
if (kind == ConstructorKind::Base)
return this_argument;
// c. If result.[[Value]] is not undefined, throw a TypeError exception.
if (!result.value().is_undefined())
if (!result.value()->is_undefined())
return vm.throw_completion<TypeError>(global_object, ErrorType::DerivedConstructorReturningInvalidValue);
}
// 11. Else, ReturnIfAbrupt(result).
@ -788,7 +788,7 @@ Completion ECMAScriptFunctionObject::ordinary_call_evaluate_body()
VERIFY(result_and_frame.frame != nullptr);
if (result_and_frame.value.is_error()) {
vm.throw_exception(bytecode_interpreter->global_object(), result_and_frame.value.release_error().value());
vm.throw_exception(bytecode_interpreter->global_object(), *result_and_frame.value.release_error().value());
return throw_completion(vm.exception()->value());
}
auto result = result_and_frame.value.release_value();
@ -844,7 +844,7 @@ Completion ECMAScriptFunctionObject::ordinary_call_evaluate_body()
// 4. Else,
else {
// a. Perform ! Call(promiseCapability.[[Reject]], undefined, « declResult.[[Value]] »).
MUST(call(global_object(), promise_capability.reject, js_undefined(), declaration_result.throw_completion().value()));
MUST(call(global_object(), promise_capability.reject, js_undefined(), *declaration_result.throw_completion().value()));
}
// 5. Return Completion { [[Type]]: return, [[Value]]: promiseCapability.[[Promise]], [[Target]]: empty }.