dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / html / universal_global_scope.lucb
#==============================================================================================
#
# universal_global_scope - the rejected-promise lists of HTML::UniversalGlobalScopeMixin
#
# DESCRIPTION:
# Port of the promise-rejection parts of Libraries/LibWeb/HTML/UniversalGlobalScope.cpp
# (lines 157-243): the about-to-be-notified rejected promises list and the outstanding
# rejected promises weak set that HostPromiseRejectionTracker keeps, and notifying about
# rejected promises at each microtask checkpoint. UniversalGlobalScope is outside the
# phase-1 files; these run because promises do (DESIGN.md ยง3.6, phase-1 stand-ins). Region
# r28 added visit_edges (line 32), which Window::visit_edges calls; region p2r fires the
# unhandledrejection PromiseRejectionEvent (a FontFace that fails to load rejects its
# promise with nothing to handle it).
#
# Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
# Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
# Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: Visiting ================================================================================
## Ported from Libraries/LibWeb/HTML/UniversalGlobalScope.cpp:32 Web::HTML::UniversalGlobalScopeMixin::visit_edges.
## (Added by region r28, for Window::visit_edges.)
pub func html_universal_global_scope_mixin_visit_edges(this: HtmlUniversalGlobalScopeMixin*, visitor: gc.CellVisitor*):
gc.cell_visitor_visit_t(visitor, this.m_count_queuing_strategy_size_function)
gc.cell_visitor_visit_t(visitor, this.m_byte_length_queuing_strategy_size_function)
# visitor.ignore(m_outstanding_rejected_promises_weak_set)
if let list = this.m_about_to_be_notified_rejected_promises_list:
gc.cell_visitor_visit_vector(visitor, list)
# mark: The rejected promise lists ==============================================================
## The predicate of the lists' remove_first_matching: the promise `context`.
func html_is_promise(context: void*?, promise_in_list: const (JsPromise*)*) -> bool:
return (void*?)*promise_in_list == context
## The predicate of the weak set's remove_first_matching: the promise `context`.
func html_is_promise_or_none(context: void*?, promise_in_set: const (JsPromise*?)*) -> bool:
return (void*?)*promise_in_set == context
## Ported from Libraries/LibWeb/HTML/UniversalGlobalScope.cpp:157 Web::HTML::UniversalGlobalScopeMixin::push_onto_outstanding_rejected_promises_weak_set.
pub func html_universal_global_scope_mixin_push_onto_outstanding_rejected_promises_weak_set(this: HtmlUniversalGlobalScopeMixin*, promise: JsPromise*?):
ak.vector_append_t(&this.m_outstanding_rejected_promises_weak_set, promise)
## Ported from Libraries/LibWeb/HTML/UniversalGlobalScope.cpp:162 Web::HTML::UniversalGlobalScopeMixin::remove_from_outstanding_rejected_promises_weak_set.
pub func html_universal_global_scope_mixin_remove_from_outstanding_rejected_promises_weak_set(this: HtmlUniversalGlobalScopeMixin*, promise: JsPromise*?) -> bool:
return ak.vector_remove_first_matching(&this.m_outstanding_rejected_promises_weak_set, ak.Function1[const (JsPromise*?)*, bool](call = html_is_promise_or_none, context = (void*?)promise))
## Ported from Libraries/LibWeb/HTML/UniversalGlobalScope.cpp:169 Web::HTML::UniversalGlobalScopeMixin::push_onto_about_to_be_notified_rejected_promises_list.
pub func html_universal_global_scope_mixin_push_onto_about_to_be_notified_rejected_promises_list(this: HtmlUniversalGlobalScopeMixin*, promise: JsPromise*):
if this.m_about_to_be_notified_rejected_promises_list == none:
# JS::VM::the().heap().allocate<GC::HeapVector<GC::Ref<JS::Promise>>>(): managed memory the
# global's cell keeps (its bytes are scanned).
this.m_about_to_be_notified_rejected_promises_list = new ak.Vector[JsPromise*]() else trap("out of memory")
ak.vector_append_t(this.m_about_to_be_notified_rejected_promises_list else trap("the list"), promise)
## Ported from Libraries/LibWeb/HTML/UniversalGlobalScope.cpp:176 Web::HTML::UniversalGlobalScopeMixin::remove_from_about_to_be_notified_rejected_promises_list.
pub func html_universal_global_scope_mixin_remove_from_about_to_be_notified_rejected_promises_list(this: HtmlUniversalGlobalScopeMixin*, promise: JsPromise*) -> bool:
let list = this.m_about_to_be_notified_rejected_promises_list else return false
return ak.vector_remove_first_matching(list, ak.Function1[const (JsPromise*)*, bool](call = html_is_promise, context = (void*?)promise))
# mark: Notifying about rejected promises =======================================================
## The captures of notify_about_rejected_promises' global task (`[this, &global, list = move(list)]`).
struct HtmlNotifyAboutRejectedPromisesTask:
var this: HtmlUniversalGlobalScopeMixin*
var global: DomEventTarget*
var list: ak.Vector[JsPromise*]*
## notify_about_rejected_promises' global task: fire unhandledrejection for each promise still
## not handled.
func html_notify_about_rejected_promises_task(context: void*?):
let captures = (HtmlNotifyAboutRejectedPromisesTask*)(context else trap("no captures"))
let realm = bind_platform_object_realm(captures.global.bind_platform_object())
# 1. For each promise p in list:
for promise in ak.vector_span(captures.list):
# 1. If p's [[PromiseIsHandled]] internal slot is true, continue to the next iteration of the loop.
if js_promise_is_handled(promise):
continue
# 2. Let notHandled be the result of firing an event named unhandledrejection at global, using PromiseRejectionEvent, with the cancelable attribute initialized to true,
# the promise attribute initialized to p, and the reason attribute initialized to the value of p's [[PromiseResult]] internal slot.
var event_init = html_promise_rejection_event_init_default()
event_init.event_init = DomEventInit(bubbles = false, cancelable = true, composed = false)
event_init.promise = (JsObject*)promise
event_init.reason = js_promise_result(promise)
let promise_rejection_event = html_promise_rejection_event_create(realm, html_event_names_unhandledrejection, event_init)
let not_handled = dom_event_target_dispatch_event(captures.global, promise_rejection_event.dom_event())
# 3. If notHandled is false, then the promise rejection is handled. Otherwise, the promise rejection is not handled.
# 4. If p's [[PromiseIsHandled]] internal slot is false, add p to settings object's outstanding rejected promises weak set.
if not js_promise_is_handled(promise):
html_universal_global_scope_mixin_push_onto_outstanding_rejected_promises_weak_set(captures.this, promise)
# This algorithm results in promise rejections being marked as handled or not handled. These concepts parallel handled and not handled script errors.
# If a rejection is still not handled after this, then the rejection may be reported to a developer console.
if not_handled:
html_report_exception_to_console(js_promise_result(promise), realm, HtmlErrorInPromise.yes)
## Ported from Libraries/LibWeb/HTML/UniversalGlobalScope.cpp:186 Web::HTML::UniversalGlobalScopeMixin::notify_about_rejected_promises.
## https://html.spec.whatwg.org/multipage/webappapis.html#notify-about-rejected-promises
pub func html_universal_global_scope_mixin_notify_about_rejected_promises(this: HtmlUniversalGlobalScopeMixin*):
# 1. Let list be a copy of settings object's about-to-be-notified rejected promises list.
let list = this.m_about_to_be_notified_rejected_promises_list
# 2. If list is empty, return.
let promises = list else return
if ak.vector_is_empty(promises):
return
# 3. Clear settings object's about-to-be-notified rejected promises list.
this.m_about_to_be_notified_rejected_promises_list = none
# 4. Let global be settings object's global object.
let global = html_universal_global_scope_mixin_this_impl(this)
# 5. Queue a global task on the DOM manipulation task source given global to run the following substep:
let captures = new HtmlNotifyAboutRejectedPromisesTask(this = this, global = global, list = promises) else trap("out of memory")
_ = html_queue_global_task(HtmlTaskSource.dom_manipulation, (JsObject*)global, gc.create_function0(gc.cell_heap(global.cell()), ak.Function0[unit](call = html_notify_about_rejected_promises_task, context = captures)))