1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:18:11 +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

@ -160,7 +160,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyObject::compile)
auto buffer_or_error = vm.argument(0).to_object(global_object);
JS::Value rejection_value;
if (buffer_or_error.is_error()) {
rejection_value = buffer_or_error.throw_completion().value();
rejection_value = *buffer_or_error.throw_completion().value();
vm.clear_exception();
vm.stop_unwind();
}
@ -172,7 +172,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyObject::compile)
auto* buffer = buffer_or_error.release_value();
auto result = parse_module(global_object, buffer);
if (result.is_error())
promise->reject(result.release_error().value());
promise->reject(*result.release_error().value());
else
promise->fulfill(vm.heap().allocate<WebAssemblyModuleObject>(global_object, global_object, result.release_value()));
return promise;
@ -325,7 +325,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyObject::instantiate)
auto promise = JS::Promise::create(global_object);
bool should_return_module = false;
if (buffer_or_error.is_error()) {
auto rejection_value = buffer_or_error.throw_completion().value();
auto rejection_value = *buffer_or_error.throw_completion().value();
vm.clear_exception();
vm.stop_unwind();
promise->reject(rejection_value);
@ -337,7 +337,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyObject::instantiate)
if (is<JS::ArrayBuffer>(buffer) || is<JS::TypedArrayBase>(buffer)) {
auto result = parse_module(global_object, buffer);
if (result.is_error()) {
promise->reject(result.release_error().value());
promise->reject(*result.release_error().value());
return promise;
}
module = &WebAssemblyObject::s_compiled_modules.at(result.release_value()).module;
@ -353,7 +353,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyObject::instantiate)
auto result = instantiate_module(*module, vm, global_object);
if (result.is_error()) {
promise->reject(result.release_error().value());
promise->reject(*result.release_error().value());
} else {
auto instance_object = vm.heap().allocate<WebAssemblyInstanceObject>(global_object, global_object, result.release_value());
if (should_return_module) {