Truly independent web browser
LibWeb: Implement WebGLContextEvent in Rust
Shannon Booth2e265b68fae7
diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_gl/exports.rs b/Libraries/LibWeb/Bindings/Rust/src/web_gl/exports.rsindex 0153c18174c..7da31075e20 100644--- a/Libraries/LibWeb/Bindings/Rust/src/web_gl/exports.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/web_gl/exports.rs@@ -11,6 +11,8 @@ use core::ffi::c_void;use core::marker::PhantomData;use core::ptr::NonNull;+use ak::Utf16String;+use super::canvas::Canvas;use super::fingerprint::verify_that_rust_and_cpp_agree_on_layouts_and_constants;use super::host::CppTransport;@@ -19,9 +21,11 @@ use super::state::{ContextParameters, ContextRef, ContextState, RenderingContext, WebGL1, WebGL2, WebGLVersion,drawing_buffer_dimension,};+use super::web_gl_context_event::{WebGLContextEvent, WebGLContextEventInit};use super::web_gl_rendering_context::WebGLPowerPreference;use super::web_gl_rendering_context_base::WebGLContextAttributes;use crate::bindings::interface::{Interface, allocate, cell_into_abi, realm_arguments};+use crate::dom::event::EventInit;/// The bytes of the names of the extensions C++ lends.fn extension_names(names: ak::Span<'_, ak::Span<'_, u8>>) -> Vec<Vec<u8>> {@@ -278,3 +282,29 @@ pub unsafe extern "C" fn web_webgl_context_restore(context.restore(transport, supported_opengl_extensions);});}++/// # Safety+///+/// `vm` must be the live VM, `realm` a live realm, and `type` a raw AK::Utf16String that the call takes over.+#[unsafe(no_mangle)]+pub unsafe extern "C" fn web_webgl_context_event_create(+ vm: *mut c_void,+ realm: *mut c_void,+ r#type: usize,+) -> *mut c_void {+ // SAFETY: The caller passes the live VM and a live realm.+ let (vm, realm) = unsafe { realm_arguments(vm, realm) };+ // SAFETY: The caller hands over the type.+ let r#type = unsafe { Utf16String::from_raw_owned(r#type) };+ let event_init = WebGLContextEventInit {+ parent: EventInit {+ bubbles: false,+ cancelable: true,+ composed: false,+ },+ status_message: Utf16String::from_utf8(""),+ };+ let event = WebGLContextEvent::create(vm, realm, &r#type, &event_init);+ event.event().set_is_trusted(true);+ event.event().as_ptr()+}diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_gl/mod.rs b/Libraries/LibWeb/Bindings/Rust/src/web_gl/mod.rsindex 4a659b30116..178e608aa1d 100644--- a/Libraries/LibWeb/Bindings/Rust/src/web_gl/mod.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/web_gl/mod.rs@@ -51,6 +51,8 @@ pub(crate) mod web_gl_active_info;#[cfg(not(test))]pub(crate) mod web_gl_buffer;#[cfg(not(test))]+pub(crate) mod web_gl_context_event;+#[cfg(not(test))]pub(crate) mod web_gl_draw_buffers;#[cfg(not(test))]pub(crate) mod web_gl_framebuffer;diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_gl/web_gl_context_event.rs b/Libraries/LibWeb/Bindings/Rust/src/web_gl/web_gl_context_event.rsnew file mode 100644index 00000000000..fe6d711376b--- /dev/null+++ b/Libraries/LibWeb/Bindings/Rust/src/web_gl/web_gl_context_event.rs@@ -0,0 +1,77 @@+/*+ * Copyright (c) 2022, Luke Wilde <[email protected]>+ * Copyright (c) 2022, Andreas Kling <[email protected]>+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++//! WebGLContextEvent, https://registry.khronos.org/webgl/specs/latest/1.0/#5.15++use core::ffi::c_void;+use core::ptr::NonNull;++use ak::Utf16String;+use libjs_rust::api::{Gc, Realm, Trace, Vm};++use crate::bindings::event_target::CppEvent;+use crate::bindings::interface::{Ref, Wrappable, allocate};+use crate::bindings::{CallContext, WebIdlResult};+use crate::dom::event::EventInit;++pub(crate) struct WebGLContextEventInit {+ pub(crate) parent: EventInit,+ pub(crate) status_message: Utf16String,+}++#[derive(Trace)]+#[gc(crate = libjs_rust)]+pub(crate) struct WebGLContextEvent {+ event: CppEvent,+ status_message: Utf16String,+}++impl Wrappable for WebGLContextEvent {+ fn cpp_parent(&self) -> Option<NonNull<c_void>> {+ self.event.as_cpp_parent()+ }+}++impl WebGLContextEvent {+ pub(crate) fn create(+ vm: &Vm,+ realm: Gc<Realm>,+ event_name: &Utf16String,+ event_init: &WebGLContextEventInit,+ ) -> Ref<WebGLContextEvent> {+ let event = CppEvent::create_for_owner(+ realm,+ event_name,+ event_init.parent.bubbles,+ event_init.parent.cancelable,+ event_init.parent.composed,+ );+ let object = allocate(vm, |_| WebGLContextEvent {+ event,+ status_message: event_init.status_message.clone(),+ });+ object.event.set_owner(object);+ object+ }++ pub(crate) fn construct(+ cx: &mut CallContext,+ r#type: Utf16String,+ event_init: WebGLContextEventInit,+ ) -> WebIdlResult<Ref<WebGLContextEvent>> {+ Ok(Self::create(cx.vm(), cx.realm(), &r#type, &event_init))+ }++ pub(crate) fn event(&self) -> &CppEvent {+ &self.event+ }++ pub(crate) fn status_message(&self, _: &mut CallContext) -> WebIdlResult<Utf16String> {+ Ok(self.status_message.clone())+ }+}diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txtindex 2a27f632280..1f57801dded 100644--- a/Libraries/LibWeb/CMakeLists.txt+++ b/Libraries/LibWeb/CMakeLists.txt@@ -998,7 +998,6 @@ set(SOURCESWebGL/EventNames.cppWebGL/RemoteWebGLTransportFFI.cppWebGL/WebGLContextAttributes.cpp- WebGL/WebGLContextEvent.cppWebGL/WebGLRenderingContextBase.cppWebIDL/AbstractOperations.cppWebIDL/AsyncIterator.cppdiff --git a/Libraries/LibWeb/Forward.h b/Libraries/LibWeb/Forward.hindex ebc9d27c788..baee4975648 100644--- a/Libraries/LibWeb/Forward.h+++ b/Libraries/LibWeb/Forward.h@@ -1217,7 +1217,6 @@ namespace Web::WebGL {class RemoteWebGLTransport;class WebGL2RenderingContext;-class WebGLContextEvent;class WebGLRenderingContext;class WebGLRenderingContextBase;diff --git a/Libraries/LibWeb/WebGL/WebGLContextEvent.cpp b/Libraries/LibWeb/WebGL/WebGLContextEvent.cppdeleted file mode 100644index 170d0d2028c..00000000000--- a/Libraries/LibWeb/WebGL/WebGLContextEvent.cpp+++ /dev/null@@ -1,33 +0,0 @@-/*- * Copyright (c) 2022, Andreas Kling <[email protected]>- *- * SPDX-License-Identifier: BSD-2-Clause- */--#include <LibGC/Heap.h>-#include <LibWeb/Bindings/WebGLContextEvent.h>-#include <LibWeb/WebGL/WebGLContextEvent.h>--namespace Web::WebGL {--GC_DEFINE_ALLOCATOR(WebGLContextEvent);--GC::Ref<WebGLContextEvent> WebGLContextEvent::create(Utf16FlyString const& event_name, Bindings::WebGLContextEventInit const& event_init, HighResolutionTime::DOMHighResTimeStamp time_stamp)-{- return GC::Heap::the().allocate<WebGLContextEvent>(event_name, event_init, time_stamp);-}--WebIDL::ExceptionOr<GC::Ref<WebGLContextEvent>> WebGLContextEvent::create_for_constructor(Utf16FlyString const& event_name, Bindings::WebGLContextEventInit const& event_init, HighResolutionTime::DOMHighResTimeStamp time_stamp)-{- return create(event_name, event_init, time_stamp);-}--WebGLContextEvent::WebGLContextEvent(Utf16FlyString const& type, Bindings::WebGLContextEventInit const& event_init, HighResolutionTime::DOMHighResTimeStamp time_stamp)- : DOM::Event(type, event_init, time_stamp)- , m_status_message(event_init.status_message)-{-}--WebGLContextEvent::~WebGLContextEvent() = default;--}diff --git a/Libraries/LibWeb/WebGL/WebGLContextEvent.h b/Libraries/LibWeb/WebGL/WebGLContextEvent.hdeleted file mode 100644index 33e767c9233..00000000000--- a/Libraries/LibWeb/WebGL/WebGLContextEvent.h+++ /dev/null@@ -1,35 +0,0 @@-/*- * Copyright (c) 2022, Luke Wilde <[email protected]>- * Copyright (c) 2022, Andreas Kling <[email protected]>- *- * SPDX-License-Identifier: BSD-2-Clause- */--#pragma once--#include <AK/Utf16FlyString.h>-#include <AK/Utf16String.h>-#include <LibWeb/DOM/Event.h>-#include <LibWebCommon/HighResolutionTime/DOMHighResTimeStamp.h>--namespace Web::WebGL {--class WebGLContextEvent final : public DOM::Event {- WEB_WRAPPABLE(WebGLContextEvent, DOM::Event);- GC_DECLARE_ALLOCATOR(WebGLContextEvent);--public:- [[nodiscard]] static GC::Ref<WebGLContextEvent> create(Utf16FlyString const& type, Bindings::WebGLContextEventInit const&, HighResolutionTime::DOMHighResTimeStamp);- static WebIDL::ExceptionOr<GC::Ref<WebGLContextEvent>> create_for_constructor(Utf16FlyString const& type, Bindings::WebGLContextEventInit const&, HighResolutionTime::DOMHighResTimeStamp);-- virtual ~WebGLContextEvent() override;-- Utf16String const& status_message() const { return m_status_message; }--private:- WebGLContextEvent(Utf16FlyString const& type, Bindings::WebGLContextEventInit const&, HighResolutionTime::DOMHighResTimeStamp);-- Utf16String m_status_message;-};--}diff --git a/Libraries/LibWeb/WebGL/WebGLContextEvent.idl b/Libraries/LibWeb/WebGL/WebGLContextEvent.idlindex 38739ae32a1..172e861ddff 100644--- a/Libraries/LibWeb/WebGL/WebGLContextEvent.idl+++ b/Libraries/LibWeb/WebGL/WebGLContextEvent.idl@@ -1,7 +1,7 @@// https://registry.khronos.org/webgl/specs/latest/1.0/#5.15-[Exposed=(Window,Worker)]+[Exposed=(Window,Worker), ImplementedIn=Rust]interface WebGLContextEvent : Event {- constructor([FlyString] DOMString type, optional WebGLContextEventInit eventInit = {});+ constructor(DOMString type, optional WebGLContextEventInit eventInit = {});readonly attribute DOMString statusMessage;};diff --git a/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.cpp b/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.cppindex 5881981da03..2399ffd193d 100644--- a/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.cpp+++ b/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.cpp@@ -15,9 +15,10 @@#include <LibJS/HostClassBuilder.h>#include <LibJS/Runtime/Object.h>#include <LibJS/Runtime/Realm.h>-#include <LibWeb/Bindings/WebGLContextEvent.h>+#include <LibJS/Runtime/VM.h>#include <LibWeb/Bindings/WrapperWorld.h>#include <LibWeb/Compositor/CompositorHost.h>+#include <LibWeb/DOM/Event.h>#include <LibWeb/DOM/EventTarget.h>#include <LibWeb/HTML/Canvas/CanvasHost.h>#include <LibWeb/HTML/EventLoop/Task.h>@@ -26,13 +27,11 @@#include <LibWeb/HTML/HTMLVideoElement.h>#include <LibWeb/HTML/OffscreenCanvas.h>#include <LibWeb/HTML/WindowOrWorkerGlobalScope.h>-#include <LibWeb/HighResolutionTime/TimeOrigin.h>#include <LibWeb/Page/Page.h>#include <LibWeb/WebGL/EventNames.h>#include <LibWeb/WebGL/RemoteWebGLTransport.h>#include <LibWeb/WebGL/WebGL2RenderingContext.h>#include <LibWeb/WebGL/WebGLContextAttributes.h>-#include <LibWeb/WebGL/WebGLContextEvent.h>#include <LibWeb/WebGL/WebGLRenderingContext.h>#include <LibWeb/WebGL/WebGLRenderingContextBase.h>@@ -111,6 +110,7 @@ void web_webgl_context_set_restore_requested(void* context, bool requested);bool web_webgl_context_wants_restoring(void* context);void web_webgl_context_actual_context_parameters(void* context, RustContextAttributes* out_context_attributes);void web_webgl_context_restore(void* context, void* transport, ReadonlySpan<ReadonlyBytes> supported_extensions);+void* web_webgl_context_event_create(JSVM*, void* realm, FlatPtr type);// What the Rust implementation asks of LibWeb, declared in Bindings/Rust/src/web_gl/canvas.rs.void web_webgl_canvas_did_change_content(void* canvas_host);@@ -169,10 +169,8 @@ static bool fire_webgl_context_event(HTML::CanvasHost& canvas, Utf16FlyString co{// To fire a WebGL context event named e means that an event using the WebGLContextEvent interface, with its type attribute [DOM4] initialized to e, its cancelable attribute initialized to true, and its isTrusted attribute [DOM4] initialized to true, is to be dispatched at the given object.// FIXME: Consider setting a status message.- auto event = WebGLContextEvent::create(type, Bindings::WebGLContextEventInit {}, HighResolutionTime::current_high_resolution_time(canvas.canvas_relevant_global_object()));- event->set_is_trusted(true);- event->set_cancelable(true);- return canvas.canvas_event_target().dispatch_event(*event);+ auto& event = *static_cast<DOM::Event*>(web_webgl_context_event_create(JS::HostABI::vm_to_abi(JS::VM::the()), web_webgl_canvas_relevant_realm(&canvas), type.to_utf16_string().into_raw()));+ return canvas.canvas_event_target().dispatch_event(event);}// https://www.khronos.org/registry/webgl/specs/latest/1.0/#fire-a-webgl-context-creation-errordiff --git a/Tests/LibWeb/Text/expected/DOM/Event-has-unforgeable-properties.txt b/Tests/LibWeb/Text/expected/DOM/Event-has-unforgeable-properties.txtindex cd23b047f4d..e78a152bdb4 100644--- a/Tests/LibWeb/Text/expected/DOM/Event-has-unforgeable-properties.txt+++ b/Tests/LibWeb/Text/expected/DOM/Event-has-unforgeable-properties.txt@@ -16,3 +16,9 @@ isTrusted descriptor is not undefined: trueisTrusted enumerable: trueisTrusted configurable: falseisTrusted writable: undefined+isTrusted exists on WebGLContextEvent instance itself: true+isTrusted does not exist on LocationPrototype: true+isTrusted descriptor is not undefined: true+isTrusted enumerable: true+isTrusted configurable: false+isTrusted writable: undefineddiff --git a/Tests/LibWeb/Text/input/DOM/Event-has-unforgeable-properties.html b/Tests/LibWeb/Text/input/DOM/Event-has-unforgeable-properties.htmlindex b7a462ca017..55a86c6ac4f 100644--- a/Tests/LibWeb/Text/input/DOM/Event-has-unforgeable-properties.html+++ b/Tests/LibWeb/Text/input/DOM/Event-has-unforgeable-properties.html@@ -6,7 +6,7 @@"isTrusted",];- for (const eventType of [Event, UIEvent, CustomEvent]) {+ for (const eventType of [Event, UIEvent, CustomEvent, WebGLContextEvent]) {const event = new eventType("test");const eventPrototype = Object.getPrototypeOf(event);