LibWeb: Implement the timeout step of execute_async_script

This commit is contained in:
stelar7 2023-06-17 18:07:27 +02:00 committed by Andrew Kaster
parent 23b378822b
commit b0adf96eff
Notes: sideshowbarker 2024-07-17 06:45:52 +09:00

View file

@ -331,6 +331,10 @@ ExecuteScriptResultSerialized execute_async_script(Web::Page& page, DeprecatedSt
auto& vm = window->vm();
auto start = MonotonicTime::now();
auto has_timed_out = [&] {
return timeout.has_value() && (MonotonicTime::now() - start) > Duration::from_seconds(static_cast<i64>(*timeout));
};
// AD-HOC: An execution context is required for Promise creation hooks.
HTML::TemporaryExecutionContext execution_context { document->relevant_settings_object() };
@ -381,7 +385,7 @@ ExecuteScriptResultSerialized execute_async_script(Web::Page& page, DeprecatedSt
vm.custom_data()->spin_event_loop_until([&] {
if (script_promise.state() != JS::Promise::State::Pending)
return true;
if (timeout.has_value() && (MonotonicTime::now() - start) > Duration::from_seconds(static_cast<i64>(*timeout)))
if (has_timed_out())
return true;
return false;
});
@ -395,12 +399,22 @@ ExecuteScriptResultSerialized execute_async_script(Web::Page& page, DeprecatedSt
WebIDL::reject_promise(realm, promise_capability, script_promise.result());
}();
// FIXME: 6. If promise is still pending and session script timeout milliseconds is reached, return error with error code script timeout.
// 6. If promise is still pending and session script timeout milliseconds is reached, return error with error code script timeout.
vm.custom_data()->spin_event_loop_until([&] {
if (has_timed_out()) {
return true;
}
return promise->state() != JS::Promise::State::Pending;
});
if (has_timed_out()) {
auto error_object = JsonObject {};
error_object.set("name", "Error");
error_object.set("message", "script timeout");
return { ExecuteScriptResultType::Timeout, move(error_object) };
}
auto json_value_or_error = json_clone(realm, promise->result());
if (json_value_or_error.is_error()) {
auto error_object = JsonObject {};