Skip to content

Truly independent web browser

LibWeb: Resolve C++ overloads with objects of Rust interfaces

Andreas Kling481653f77f6f
diff --git a/Libraries/LibWeb/Bindings/RustBindingsServices.cpp b/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
index b7a10abbb74..45aaeb11b7d 100644
--- a/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
+++ b/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
@@ -34,6 +34,19 @@ GC::Ref<JS::HostObject> wrap_rust_interface_object(WrapperWorld& wrapper_world,
constexpr JSHostClass web_bindings_rust_interface_wrapper_host_class = JS::make_host_class(JS_HOST_CLASS_OBJECT, "RustInterface"sv,
nullptr, nullptr, nullptr, JS_HOST_CLASS_IS_PLATFORM_OBJECT);
+bool rust_interface_object_implements_interface(JS::Object const& object, StringView interface_name)
+{
+ if (!JS::is_host_instance_of(object, web_bindings_rust_interface_wrapper_host_class))
+ return false;
+ // NB: The class of the wrappers of an interface has the name of the interface, and the class of the wrappers of
+ // the interface that it inherits from as its parent.
+ for (auto const* host_class = &as<JS::HostObject>(object).host_class(); host_class != &web_bindings_rust_interface_wrapper_host_class; host_class = host_class->parent) {
+ if (StringView { host_class->name, host_class->name_length } == interface_name)
+ return true;
+ }
+ return false;
+}
+
extern "C" {
void* web_bindings_wrapper_world(void* realm, bool* is_main_world);
diff --git a/Libraries/LibWeb/Bindings/RustInterface.h b/Libraries/LibWeb/Bindings/RustInterface.h
index dd6c02079d3..e659aa077b7 100644
--- a/Libraries/LibWeb/Bindings/RustInterface.h
+++ b/Libraries/LibWeb/Bindings/RustInterface.h
@@ -7,6 +7,7 @@
#pragma once
#include <AK/Concepts.h>
+#include <AK/StringView.h>
#include <LibGC/Cell.h>
#include <LibGC/Ptr.h>
#include <LibJS/HostObjectABI.h>
@@ -50,6 +51,11 @@ concept RustInterfaceHandle = IsBaseOf<RustInterfaceCell, T> && GC::IsForeignCel
WEB_API GC::Ref<JS::HostObject> wrap_rust_interface_object(WrapperWorld&, JS::Realm& preferred_realm, InterfaceName, RustInterfaceCell&);
+// Whether the object is a wrapper of an object of an interface implemented in Rust that is or inherits from the
+// interface with the name, as Wrappable::implements_interface() says for the objects of the interfaces implemented in
+// C++.
+WEB_API bool rust_interface_object_implements_interface(JS::Object const&, StringView interface_name);
+
template<RustInterfaceHandle T>
[[nodiscard]] GC::Ref<JS::HostObject> wrap(WrapperWorld& wrapper_world, JS::Realm& preferred_realm, GC::Ref<T> object)
{
diff --git a/Libraries/LibWeb/WebIDL/OverloadResolution.cpp b/Libraries/LibWeb/WebIDL/OverloadResolution.cpp
index 0a5391903dd..8d9f1eec153 100644
--- a/Libraries/LibWeb/WebIDL/OverloadResolution.cpp
+++ b/Libraries/LibWeb/WebIDL/OverloadResolution.cpp
@@ -11,6 +11,7 @@
#include <LibJS/Runtime/TypedArray.h>
#include <LibJS/Runtime/Value.h>
#include <LibWeb/Bindings/PlatformObject.h>
+#include <LibWeb/Bindings/RustInterface.h>
#include <LibWeb/WebIDL/OverloadResolution.h>
namespace Web::WebIDL {
@@ -26,7 +27,7 @@ static bool platform_object_implements_interface(JS::Value value, StringView int
{
auto const* wrappable = Bindings::wrappable_impl_from(&value.as_object());
if (!wrappable)
- return false;
+ return Bindings::rust_interface_object_implements_interface(value.as_object(), interface_name);
return wrappable->implements_interface(MUST(String::from_utf8(interface_name)));
}