LibWeb: Always return a rejected Promise for functions which throw
Some checks are pending
CI / Lagom (false, FUZZ, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (false, NO_FUZZ, macos-15, macOS, Clang) (push) Waiting to run
CI / Lagom (false, NO_FUZZ, ubuntu-24.04, Linux, GNU) (push) Waiting to run
CI / Lagom (true, NO_FUZZ, ubuntu-24.04, Linux, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (macos-14, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (ubuntu-24.04, Linux, Linux-x86_64) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Push notes / build (push) Waiting to run

We were previously throwing an exception if the generated code was
throwing an exception before it hit the implementation of the interface.
Instead, we are meant to catch any exception, and wrap that in a
rejected promise.

For example, this was impacting the fixed test in this commit as an
exception was being thrown when invoking WebIDL::convert_to_int<T>
as the given number was out of range, and the [EnforceRange]
extended attribute decorates that attribute.

This same type of case is seen for a few tests in WPT.
This commit is contained in:
Shannon Booth 2024-11-17 04:52:45 +13:00 committed by Andreas Kling
parent 8d93cac983
commit 98dadb0ce6
Notes: github-actions[bot] 2024-11-16 17:34:49 +00:00
2 changed files with 30 additions and 3 deletions

View file

@ -1992,6 +1992,14 @@ JS_DEFINE_NATIVE_FUNCTION(@class_name@::@function.name:snakecase@@overload_suffi
[[maybe_unused]] auto& realm = *vm.current_realm(); [[maybe_unused]] auto& realm = *vm.current_realm();
)~~~"); )~~~");
// NOTE: Create a wrapper lambda so that if the function steps return an exception, we can return that in a rejected promise.
if (function.return_type->name() == "Promise"sv) {
function_generator.append(R"~~~(
auto steps = [&realm, &vm]() -> JS::ThrowCompletionOr<GC::Ref<WebIDL::Promise>> {
(void)realm;
)~~~");
}
if (is_static_function == StaticFunction::No) { if (is_static_function == StaticFunction::No) {
function_generator.append(R"~~~( function_generator.append(R"~~~(
auto* impl = TRY(impl_from(vm)); auto* impl = TRY(impl_from(vm));
@ -2053,6 +2061,26 @@ JS_DEFINE_NATIVE_FUNCTION(@class_name@::@function.name:snakecase@@overload_suffi
)~~~"); )~~~");
} }
if (function.return_type->name() == "Promise"sv) {
// https://webidl.spec.whatwg.org/#dfn-create-operation-function
// If we had an exception running the steps and are meant to return a Promise, wrap that exception in a rejected promise.
function_generator.append(R"~~~(
return retval;
};
auto maybe_retval = steps();
// And then, if an exception E was thrown:
// 1. If op has a return type that is a promise type, then return ! Call(%Promise.reject%, %Promise%, «E»).
// 2. Otherwise, end these steps and allow the exception to propagate.
// NOTE: We know that this is a Promise return type statically by the IDL.
if (maybe_retval.is_throw_completion())
return WebIDL::create_rejected_promise(realm, maybe_retval.error_value())->promise();
auto retval = maybe_retval.release_value();
)~~~");
}
generate_return_statement(generator, *function.return_type, interface); generate_return_statement(generator, *function.return_type, interface);
function_generator.append(R"~~~( function_generator.append(R"~~~(

View file

@ -6,11 +6,10 @@ Rerun
Found 24 tests Found 24 tests
23 Pass 24 Pass
1 Fail
Details Details
Result Test Name MessagePass ReadableStream with byte source: read({ min }) rejects if min is 0 Result Test Name MessagePass ReadableStream with byte source: read({ min }) rejects if min is 0
Fail ReadableStream with byte source: read({ min }) rejects if min is negative Pass ReadableStream with byte source: read({ min }) rejects if min is negative
Pass ReadableStream with byte source: read({ min }) rejects if min is larger than view's length (Uint8Array) Pass ReadableStream with byte source: read({ min }) rejects if min is larger than view's length (Uint8Array)
Pass ReadableStream with byte source: read({ min }) rejects if min is larger than view's length (Uint16Array) Pass ReadableStream with byte source: read({ min }) rejects if min is larger than view's length (Uint16Array)
Pass ReadableStream with byte source: read({ min }) rejects if min is larger than view's length (DataView) Pass ReadableStream with byte source: read({ min }) rejects if min is larger than view's length (DataView)