mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 02:08:11 +00:00
LibWeb: Implement the timeout step of execute_async_script
This commit is contained in:
parent
23b378822b
commit
b0adf96eff
1 changed files with 17 additions and 3 deletions
|
@ -331,6 +331,10 @@ ExecuteScriptResultSerialized execute_async_script(Web::Page& page, DeprecatedSt
|
||||||
auto& vm = window->vm();
|
auto& vm = window->vm();
|
||||||
auto start = MonotonicTime::now();
|
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.
|
// AD-HOC: An execution context is required for Promise creation hooks.
|
||||||
HTML::TemporaryExecutionContext execution_context { document->relevant_settings_object() };
|
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([&] {
|
vm.custom_data()->spin_event_loop_until([&] {
|
||||||
if (script_promise.state() != JS::Promise::State::Pending)
|
if (script_promise.state() != JS::Promise::State::Pending)
|
||||||
return true;
|
return true;
|
||||||
if (timeout.has_value() && (MonotonicTime::now() - start) > Duration::from_seconds(static_cast<i64>(*timeout)))
|
if (has_timed_out())
|
||||||
return true;
|
return true;
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
@ -395,12 +399,22 @@ ExecuteScriptResultSerialized execute_async_script(Web::Page& page, DeprecatedSt
|
||||||
WebIDL::reject_promise(realm, promise_capability, script_promise.result());
|
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([&] {
|
vm.custom_data()->spin_event_loop_until([&] {
|
||||||
|
if (has_timed_out()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
return promise->state() != JS::Promise::State::Pending;
|
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());
|
auto json_value_or_error = json_clone(realm, promise->result());
|
||||||
if (json_value_or_error.is_error()) {
|
if (json_value_or_error.is_error()) {
|
||||||
auto error_object = JsonObject {};
|
auto error_object = JsonObject {};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue