dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / html / scripting / tests_environments.lucb
#==============================================================================================
#
# tests_environments - tests of the scripting environments and the agent (region r29)
#
# DESCRIPTION:
# Ladybird has no unit tests for HTML/Scripting (it is tested through
# Tests/LibWeb/Text, which needs scripting), so these pin the C++ behaviour phase 1
# runs: the similar-origin window agent with its window event loop and its roots, the
# environment settings object's data and registration with its responsible event loop,
# the principal, relevant, current, entry and incumbent realms, settings objects and
# global objects, preparing to run script and callbacks (TemporaryExecutionContext),
# scripting enabled, the module types, and parsing URLs relative to a settings object.
#
# The helpers at the top are for every region's tests. web_test_install_agent gives a test
# VM (r14's web_test_create_vm) the agent bind_initialize_main_thread_vm gives the main
# VM; web_test_create_settings_realm makes a principal realm as
# WindowEnvironmentSettingsObject::setup does, with a test global object (a platform
# object with the UniversalGlobalScopeMixin, as a non-Window global such as a worker's)
# and a test environment settings object (the Window ones need Window, region r28). In
# it, events can be created and dispatched to native listeners.
#
#==============================================================================================
# mark: Helpers =================================================================================
## The agent of `vm`, as Bindings::create_agent makes it for a window: a similar-origin window
## agent on the C heap with its window event loop, and the promise host hooks
## initialize_main_thread_vm installs. web_test_destroy_vm destroys the agent.
func web_test_install_agent(vm: Vm*) -> HtmlSimilarOriginWindowAgent*:
bind_install_main_thread_vm_host_hooks(vm)
with memory.heap:
let agent = html_similar_origin_window_agent_create(vm_heap(vm))
vm_set_agent(vm, agent.html_agent())
return agent
## A test global object: an EventTarget with the UniversalGlobalScopeMixin (as a worker's global).
struct TestHtmlGlobalScope: gc.IsCell:
var event_target: DomEventTarget
var universal_global_scope_mixin: HtmlUniversalGlobalScopeMixin
func cell() -> gc.Cell*: return (gc.Cell*)&self
## The mixins of the test global: its UniversalGlobalScopeMixin.
let test_html_global_scope_mixins: MixinOffsets = MixinOffsets(universal_global_scope_mixin = memory.offset_of(TestHtmlGlobalScope, universal_global_scope_mixin))
## The vtable of the test global: EventTarget's.
let test_html_global_scope_vtable: DomEventTargetVTable = DomEventTargetVTable(platform_object_vtable = BindPlatformObjectVTable(cell_vtable = JsCellVTable(cell_vtable = gc.CellVTable(class_info = &test_html_global_scope_class, visit_edges = dom_event_target_visit_edges_impl), initialize = dom_event_target_initialize_impl)))
## The class of the test global: an EventTarget (its class id) with the mixin.
let test_html_global_scope_class: ak.ClassInfo = ak.ClassInfo(name = "TestGlobalScope", first_id = 49, last_id = 49, size = memory.size_of(TestHtmlGlobalScope), vtable = (const void*)&test_html_global_scope_vtable, mixins = (const void*)&test_html_global_scope_mixins)
## UniversalGlobalScopeMixin::this_impl of the test global: the global itself.
func test_html_global_scope_this_impl(this: HtmlUniversalGlobalScopeMixin*) -> DomEventTarget*:
return (DomEventTarget*)((u8*)this - memory.offset_of(TestHtmlGlobalScope, universal_global_scope_mixin))
## The UniversalGlobalScopeMixin's vtable in the test global.
let test_html_global_scope_universal_global_scope_vtable: HtmlUniversalGlobalScopeMixinVTable = HtmlUniversalGlobalScopeMixinVTable(class_info = &test_html_global_scope_class, this_impl = test_html_global_scope_this_impl, this_impl_const = test_html_global_scope_this_impl)
## create_global_object of the test realms: a test global made in the new realm.
func test_html_create_global_scope(context: void*?, realm: Realm*) -> JsObject*?:
let global = (TestHtmlGlobalScope*)web_allocate_cell(realm, &test_html_global_scope_class)
dom_event_target_construct(&global.event_target, realm)
global.universal_global_scope_mixin.vtable = &test_html_global_scope_universal_global_scope_vtable
global.universal_global_scope_mixin.class_id = test_html_global_scope_class.first_id
gc.heap_undefer_gc(realm_heap(realm))
return (JsObject*)global
## A test environment settings object: its algorithms answer from these fields.
struct TestHtmlSettingsObject: gc.IsCell:
var settings: HtmlEnvironmentSettingsObject
var test_time_origin: f64
var test_api_base_url: web_url.Url
func cell() -> gc.Cell*: return (gc.Cell*)&self
## The test settings object's api_base_url.
func test_html_settings_object_api_base_url(base: HtmlEnvironmentSettingsObject*) -> web_url.Url:
return ((TestHtmlSettingsObject*)base).test_api_base_url
## The test settings object's origin: an opaque origin.
func test_html_settings_object_origin(base: HtmlEnvironmentSettingsObject*) -> web_url.Origin:
return web_url.origin_create_opaque()
## The test settings object's responsible document: none (a non-Window global).
func test_html_settings_object_responsible_document(base: HtmlEnvironmentSettingsObject*) -> DomDocument*?:
return none
## The test settings object's has_cross_site_ancestor.
func test_html_settings_object_has_cross_site_ancestor(base: HtmlEnvironmentSettingsObject*) -> bool:
return false
## The test settings object's cross-origin isolated capability.
func test_html_settings_object_cross_origin_isolated_capability(base: HtmlEnvironmentSettingsObject*) -> HtmlCanUseCrossOriginIsolatedApIs:
return HtmlCanUseCrossOriginIsolatedApIs.no
## The test settings object's time origin.
func test_html_settings_object_time_origin(base: HtmlEnvironmentSettingsObject*) -> f64:
return ((TestHtmlSettingsObject*)base).test_time_origin
## The vtable of the test settings object: EnvironmentSettingsObject's, with the test algorithms.
let test_html_settings_object_vtable: HtmlEnvironmentSettingsObjectVTable = HtmlEnvironmentSettingsObjectVTable(environment_vtable = HtmlEnvironmentVTable(cell_vtable = JsCellVTable(cell_vtable = gc.CellVTable(class_info = &test_html_settings_object_class, visit_edges = html_environment_settings_object_visit_edges_impl, finalize = html_environment_settings_object_finalize_impl), initialize = html_environment_settings_object_initialize_impl), discard_environment = html_environment_settings_object_discard_environment_impl), responsible_document = test_html_settings_object_responsible_document, api_base_url = test_html_settings_object_api_base_url, origin = test_html_settings_object_origin, has_cross_site_ancestor = test_html_settings_object_has_cross_site_ancestor, cross_origin_isolated_capability = test_html_settings_object_cross_origin_isolated_capability, time_origin = test_html_settings_object_time_origin)
## The class of the test settings object (EnvironmentSettingsObject's class id).
let test_html_settings_object_class: ak.ClassInfo = ak.ClassInfo(name = "TestEnvironmentSettingsObject", first_id = 259, last_id = 259, size = memory.size_of(TestHtmlSettingsObject), vtable = (const void*)&test_html_settings_object_vtable)
## A principal realm of `vm` (which needs an agent) made as WindowEnvironmentSettingsObject::setup
## makes one: create_a_new_javascript_realm with a test global, a test settings object over the
## realm execution context, then the [[HostDefined]] with the settings object, the intrinsics and
## a test page.
func web_test_create_settings_realm(vm: Vm*, time_origin: f64 = 1000.0) -> Realm*:
let execution_context = bind_create_a_new_javascript_realm(vm, ak.Function1[Realm*, JsObject*?](call = test_html_create_global_scope, context = none), ak.Function1[Realm*, JsObject*?]())
let realm = execution_context.realm else trap("the execution context has no realm")
let settings = (TestHtmlSettingsObject*)web_allocate_cell(realm, &test_html_settings_object_class)
html_environment_settings_object_construct(&settings.settings, execution_context)
settings.test_time_origin = time_origin
settings.test_api_base_url = domurl_parse(ak.sv("https://example.com/dir/")) else trap("the test base URL parses")
web_initialize_cell(settings.cell(), realm)
let environment = settings.settings.html_environment()
environment.id = ak.string_from_str("test")
environment.creation_url = web_url.about_blank()
environment.top_level_creation_url = web_url.about_blank()
let intrinsics = realm_create_bind_intrinsics(realm, realm)
realm_set_host_defined(realm, bind_principal_host_defined_create(&settings.settings, intrinsics, test_document_page()).bind_host_defined())
return realm
## The settings object of a realm of web_test_create_settings_realm.
func web_test_settings_of(realm: Realm*) -> HtmlEnvironmentSettingsObject*:
return html_principal_realm_settings_object(realm)
## A VM with an agent and a principal realm, for one test.
func web_test_create_vm_and_settings_realm() -> (Vm*, Realm*):
let vm = web_test_create_vm()
_ = web_test_install_agent(vm)
return (vm, web_test_create_settings_realm(vm))
## Whether a String holds `text`.
func test_html_string_is(string: ak.String, text: str) -> bool:
let view = ak.string_bytes_as_string_view(&string)
return ak.string_view_equals(&view, text)
# mark: The agent ===============================================================================
test "SimilarOriginWindowAgent::create: a window event loop with its task queue":
let vm = web_test_create_vm()
defer web_test_destroy_vm(vm)
let agent = web_test_install_agent(vm)
assert(is_html_similar_origin_window_agent(agent.html_agent()))
assert(not agent.mutation_observer_microtask_queued)
let event_loop = agent.agent.event_loop else trap("no event loop")
assert(html_event_loop_type(event_loop) == HtmlEventLoopType.window)
assert(html_event_loop_task_queue(event_loop).m_event_loop == event_loop)
assert(html_task_queue_is_empty(html_event_loop_task_queue(event_loop)))
assert(event_loop.m_rendering_task_function != none)
assert(html_event_loop_microtask_queue_empty(event_loop))
assert(html_main_thread_event_loop() == event_loop)
# The current element queue is the top of the custom element reactions stack.
ak.vector_append_t(&agent.custom_element_reactions_stack.element_queue_stack, ak.Vector[gc.Weak[DomElement]]())
assert(html_similar_origin_window_agent_current_element_queue(agent) == ak.vector_at(&agent.custom_element_reactions_stack.element_queue_stack, 0))
let current_element_queue = html_similar_origin_window_agent_current_element_queue_const(agent)
assert(ak.vector_is_empty(¤t_element_queue))
test "Bindings::initialize_main_thread_vm creates the similar-origin window agent":
bind_s_main_thread_vm = none
bind_initialize_main_thread_vm(BindAgentType.similar_origin_window)
let vm = bind_main_thread_vm()
defer web_test_destroy_vm(vm)
let agent = vm_agent(vm) else trap("no agent")
assert(is_html_similar_origin_window_agent(agent))
assert(html_event_loop_type(html_main_thread_event_loop()) == HtmlEventLoopType.window)
test "the agent's GC::Root and GC::RootVector members are roots":
web_test_clobber_stack()
run_agent_roots_are_roots()
## A platform object of `realm` known only through a weak pointer.
noinline func make_test_agent_root_cell(realm: Realm*) -> gc.Weak[BindPlatformObject]:
return gc.make_weak(realm_create_bind_platform_object_realm(realm, realm, JsObjectMayInterfereWithIndexedPropertyAccess.no))
noinline func run_agent_roots_are_roots():
let vm = web_test_create_vm()
let agent = web_test_install_agent(vm)
let realm_context = make_test_realm_context(vm)
let realm = realm_context.realm else trap("no realm")
let event_loop_weak = gc.make_weak(agent.agent.event_loop)
# Kept by the agent's own storage.
let observer_weak = make_test_agent_root_cell(realm)
ak.vector_append_t(&agent.pending_mutation_observers, (DomMutationObserver*)(gc.weak_ptr(&observer_weak) else trap("collected early")))
# Kept by a vector buffer in managed memory (as when the event loop runs under `with heap`).
let slot_weak = make_test_agent_root_cell(realm)
let allocator: memory.Allocator = vm_heap(vm)
with allocator:
ak.vector_append_t(&agent.signal_slots, (HtmlSlotElement*)(gc.weak_ptr(&slot_weak) else trap("collected early")))
web_test_clobber_stack()
gc.heap_collect_garbage(vm_heap(vm))
assert(web_test_alive(&event_loop_weak))
assert(web_test_alive(&observer_weak))
assert(web_test_alive(&slot_weak))
agent.pending_mutation_observers = ak.Vector[DomMutationObserver*]()
agent.signal_slots = ak.Vector[HtmlSlotElement*]()
web_test_clobber_stack()
gc.heap_collect_garbage(vm_heap(vm))
assert(web_test_alive(&event_loop_weak))
assert(not web_test_alive(&observer_weak))
assert(not web_test_alive(&slot_weak))
_ = realm_context
web_test_destroy_vm(vm)
# mark: Environment settings objects ============================================================
test "an environment settings object: realm, global object, event loop and data":
let (vm, realm) = web_test_create_vm_and_settings_realm()
defer web_test_destroy_vm(vm)
let settings = web_test_settings_of(realm)
let global = realm_global_object(realm)
let event_loop = html_main_thread_event_loop()
assert(html_environment_settings_object_realm(settings) == realm)
assert(html_environment_settings_object_realm_execution_context(settings).realm == realm)
assert(html_environment_settings_object_global_object(settings) == global)
assert(html_environment_settings_object_global_object_const(settings) == global)
assert(html_environment_settings_object_universal_global_scope(settings) == &((TestHtmlGlobalScope*)global).universal_global_scope_mixin)
assert(html_environment_settings_object_universal_global_scope_const(settings) == html_environment_settings_object_universal_global_scope(settings))
_ = html_environment_settings_object_module_map(settings)
assert(html_environment_settings_object_responsible_event_loop(settings) == event_loop)
assert(ak.vector_size(&event_loop.m_related_environment_settings_objects) == 1)
assert(gc.weak_ptr(ak.vector_at(&event_loop.m_related_environment_settings_objects, 0)) == settings)
assert(html_environment_settings_object_time_origin(settings) == 1000.0)
assert(test_html_string_is(settings.html_environment().id, "test"))
assert(ak.intrusive_list_is_empty(html_environment_settings_object_fetch_group(settings)))
# Discarding sets the service worker client's discarded flag.
assert(not html_environment_settings_object_discarded(settings))
html_environment_discard_environment(settings.html_environment())
assert(html_environment_settings_object_discarded(settings))
html_environment_settings_object_set_discarded(settings, false)
assert(not html_environment_settings_object_discarded(settings))
# The worker agents kept alive while starting.
let worker_agent = (HtmlWorkerAgentParent*)settings
html_environment_settings_object_keep_worker_agent_alive_while_starting(settings, worker_agent)
assert(ak.vector_size(&settings.m_worker_agents_to_keep_alive_while_starting) == 1)
html_environment_settings_object_release_worker_agent_from_startup_keep_alive(settings, worker_agent)
assert(ak.vector_is_empty(&settings.m_worker_agents_to_keep_alive_while_starting))
# The current wall time is the wall clock coarsened to 100 microseconds.
let before = high_resolution_time_wall_clock_unsafe_current_time()
let wall_time = html_environment_settings_object_current_wall_time(settings)
let after = high_resolution_time_wall_clock_unsafe_current_time()
# floor(t / 0.1) * 0.1 can land an ulp above t at epoch magnitudes (~2e-4 ms), as in Ladybird.
assert(wall_time >= before - 0.1 and wall_time <= after + 0.001)
assert(wall_time == high_resolution_time_coarsen_time(wall_time))
test "an Environment: its constructor's fields; discarding a plain environment does nothing":
let (vm, realm) = web_test_create_vm_and_settings_realm()
defer web_test_destroy_vm(vm)
let environment = realm_create_html_environment(realm, ak.string_from_str("reserved"), web_url.about_blank(), none, none, none)
assert(test_html_string_is(environment.id, "reserved"))
assert(environment.top_level_creation_url == none and environment.top_level_origin == none)
assert(environment.target_browsing_context == none and not environment.execution_ready)
html_environment_discard_environment(environment)
assert(not is_html_environment_settings_object(environment))
assert(is_html_environment_settings_object(web_test_settings_of(realm)))
test "a settings object unregisters from its event loop when it is collected":
web_test_clobber_stack()
run_settings_object_unregisters()
noinline func make_test_settings_realm(vm: Vm*) -> gc.Weak[HtmlEnvironmentSettingsObject]:
return gc.make_weak(web_test_settings_of(web_test_create_settings_realm(vm)))
noinline func run_settings_object_unregisters():
let vm = web_test_create_vm()
_ = web_test_install_agent(vm)
let event_loop = html_main_thread_event_loop()
let weak = make_test_settings_realm(vm)
assert(ak.vector_size(&event_loop.m_related_environment_settings_objects) == 1)
web_test_clobber_stack()
gc.heap_collect_garbage(vm_heap(vm))
assert(not web_test_alive(&weak))
assert(ak.vector_is_empty(&event_loop.m_related_environment_settings_objects))
web_test_destroy_vm(vm)
# mark: Realms and settings objects of objects ==================================================
test "principal, relevant and node settings objects; the relevant agent":
let (vm, realm) = web_test_create_vm_and_settings_realm()
defer web_test_destroy_vm(vm)
let settings = web_test_settings_of(realm)
let global = realm_global_object(realm)
assert(html_principal_realm_settings_object(realm) == settings)
let object = (JsObject*)realm_create_bind_platform_object_realm(realm, realm, JsObjectMayInterfereWithIndexedPropertyAccess.no)
assert(html_relevant_realm(object) == realm)
assert(html_relevant_settings_object_js_object(object) == settings)
assert(html_relevant_global_object(object) == global)
assert(html_relevant_agent(object) == vm_agent(vm))
assert(html_relevant_similar_origin_window_agent(object).html_agent() == vm_agent(vm))
let document = test_document_in_realm(realm)
let element = test_realm_element(realm, document, "div")
assert(html_relevant_settings_object_dom_node(element.dom_node()) == settings)
assert(html_relevant_global_object((JsObject*)element) == global)
test "TemporaryExecutionContext: current and entry realms while it lives; a checkpoint when it ends":
let (vm, realm) = web_test_create_vm_and_settings_realm()
defer web_test_destroy_vm(vm)
let settings = web_test_settings_of(realm)
let global = realm_global_object(realm)
let other_realm = web_test_create_settings_realm(vm)
var log = TestHtmlLog()
if true:
var context: HtmlTemporaryExecutionContext = ---
html_temporary_execution_context_construct_realm(&context, realm)
defer html_temporary_execution_context_destruct(&context)
assert(vm_running_execution_context(vm) == html_environment_settings_object_realm_execution_context(settings))
assert(html_current_settings_object() == settings)
assert(html_current_global_object() == global)
assert(html_entry_realm() == realm)
assert(html_entry_settings_object() == settings)
assert(html_entry_global_object() == global)
assert(html_event_loop_is_backup_incumbent_realm_stack_empty(html_main_thread_event_loop()))
# A microtask queued while script runs waits for the stack to empty.
html_queue_a_microtask(none, test_html_log_steps(vm, &log, 1))
assert(log.count == 0)
# A nested realm execution context is the entry one; a context that is not a realm
# execution context does not count.
var nested: HtmlTemporaryExecutionContext = ---
html_temporary_execution_context_construct_realm(&nested, other_realm)
let function_context = js_execution_context_create()
function_context.realm = realm
vm_push_execution_context(vm, function_context)
assert(html_current_settings_object() == settings)
assert(html_entry_realm() == other_realm)
_ = vm_pop_execution_context(vm)
html_temporary_execution_context_destruct(&nested)
assert(log.count == 0)
assert(html_entry_realm() == realm)
assert(vm_execution_context_stack(vm).length == 0)
assert(log.count == 1 and log.entries[0] == 1)
test "TemporaryExecutionContext with callbacks: the backup incumbent realm stack gives the incumbent":
let (vm, realm) = web_test_create_vm_and_settings_realm()
defer web_test_destroy_vm(vm)
let settings = web_test_settings_of(realm)
let event_loop = html_main_thread_event_loop()
var context: HtmlTemporaryExecutionContext = ---
html_temporary_execution_context_construct_html_environment_settings_obje(&context, settings, HtmlTemporaryExecutionContextCallbacksEnabled.yes)
assert(not html_event_loop_is_backup_incumbent_realm_stack_empty(event_loop))
assert(html_event_loop_top_of_backup_incumbent_realm_stack(event_loop) == settings)
assert(html_incumbent_settings_object() == settings)
assert(html_incumbent_realm() == realm)
assert(html_incumbent_global_object() == realm_global_object(realm))
html_temporary_execution_context_destruct(&context)
assert(html_event_loop_is_backup_incumbent_realm_stack_empty(event_loop))
assert(vm_execution_context_stack(vm).length == 0)
# mark: Running script, modules and URLs ========================================================
test "scripting is enabled for a non-Window global; can_run_script answers run":
let (vm, realm) = web_test_create_vm_and_settings_realm()
defer web_test_destroy_vm(vm)
let settings = web_test_settings_of(realm)
assert(html_is_scripting_enabled(settings))
assert(not html_is_scripting_disabled(settings))
assert(html_can_run_script(settings) == HtmlRunScriptDecision.run)
# add_module_to_resolved_module_set returns for a global that is not a Window.
html_add_module_to_resolved_module_set(settings, ak.string_from_str("https://example.com/"), ak.string_from_str("x"), none)
test "module_type_allowed: javascript-or-wasm, css and json":
let (vm, realm) = web_test_create_vm_and_settings_realm()
defer web_test_destroy_vm(vm)
let settings = web_test_settings_of(realm)
assert(html_module_type_allowed(settings, ak.sv("javascript-or-wasm")))
assert(html_module_type_allowed(settings, ak.sv("css")))
assert(html_module_type_allowed(settings, ak.sv("json")))
assert(not html_module_type_allowed(settings, ak.sv("javascript")))
assert(not html_module_type_allowed(settings, ak.sv("JSON")))
assert(not html_module_type_allowed(settings, ak.sv("")))
test "parsing URLs relative to a settings object's API base URL":
let (vm, realm) = web_test_create_vm_and_settings_realm()
defer web_test_destroy_vm(vm)
let settings = web_test_settings_of(realm)
let parsed = html_environment_settings_object_parse_url(settings, ak.sv("page.html?q=1#top")) else trap("parses")
assert(test_html_string_is(web_url.url_serialize(&parsed), "https://example.com/dir/page.html?q=1#top"))
let encoded = html_environment_settings_object_encoding_parse_url(settings, ak.sv("../a b")) else trap("parses")
assert(test_html_string_is(web_url.url_serialize(&encoded), "https://example.com/a%20b"))
let serialized = html_environment_settings_object_encoding_parse_and_serialize_url(settings, ak.sv("/x/./y")) else trap("parses")
assert(test_html_string_is(serialized, "https://example.com/x/y"))
assert(html_environment_settings_object_encoding_parse_and_serialize_url(settings, ak.sv("http://[::")) == none)
assert(html_environment_settings_object_parse_url(settings, ak.sv("https://exa mple.com/")) == none)