Bladeren bron

BindingsGenerator+LibWeb: Pass a VM to static IDL-based functions

This saves us from having to yoink the VM out of thin air.
Sam Atkins 2 jaren geleden
bovenliggende
commit
0823a3c422

+ 6 - 0
Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp

@@ -1668,6 +1668,12 @@ JS_DEFINE_NATIVE_FUNCTION(@class_name@::@function.name:snakecase@@overload_suffi
     [[maybe_unused]] auto retval = TRY(throw_dom_exception_if_needed(vm, [&] { return impl->@function.cpp_name@(@.arguments@); }));
 )~~~");
     } else {
+        // Make sure first argument for static functions is the Realm.
+        if (arguments_builder.is_empty())
+            function_generator.set(".arguments", "vm");
+        else
+            function_generator.set(".arguments", String::formatted("vm, {}", arguments_builder.string_view()));
+
         function_generator.append(R"~~~(
     [[maybe_unused]] auto retval = TRY(throw_dom_exception_if_needed(vm, [&] { return @interface_fully_qualified_name@::@function.cpp_name@(@.arguments@); }));
 )~~~");

+ 4 - 7
Userland/Libraries/LibWeb/Fetch/Response.cpp

@@ -152,19 +152,17 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> Response::construct_impl(JS::Rea
 }
 
 // https://fetch.spec.whatwg.org/#dom-response-error
-JS::NonnullGCPtr<Response> Response::error()
+JS::NonnullGCPtr<Response> Response::error(JS::VM& vm)
 {
-    auto& vm = Bindings::main_thread_vm();
-
     // The static error() method steps are to return the result of creating a Response object, given a new network error, "immutable", and this’s relevant Realm.
     // FIXME: How can we reliably get 'this', i.e. the object the function was called on, in IDL-defined functions?
     return Response::create(Infrastructure::Response::network_error(), Headers::Guard::Immutable, *vm.current_realm());
 }
 
 // https://fetch.spec.whatwg.org/#dom-response-redirect
-WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> Response::redirect(String const& url, u16 status)
+WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> Response::redirect(JS::VM& vm, String const& url, u16 status)
 {
-    auto& realm = HTML::current_settings_object().realm();
+    auto& realm = *vm.current_realm();
 
     // 1. Let parsedURL be the result of parsing url with current settings object’s API base URL.
     auto api_base_url = HTML::current_settings_object().api_base_url();
@@ -200,9 +198,8 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> Response::redirect(String const&
 }
 
 // https://fetch.spec.whatwg.org/#dom-response-json
-WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> Response::json(JS::Value data, ResponseInit const& init)
+WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> Response::json(JS::VM& vm, JS::Value data, ResponseInit const& init)
 {
-    auto& vm = Bindings::main_thread_vm();
     auto& realm = *vm.current_realm();
 
     // 1. Let bytes the result of running serialize a JavaScript value to JSON bytes on data.

+ 3 - 3
Userland/Libraries/LibWeb/Fetch/Response.h

@@ -44,9 +44,9 @@ public:
     [[nodiscard]] NonnullRefPtr<Infrastructure::Response> response() const { return m_response; }
 
     // JS API functions
-    [[nodiscard]] static JS::NonnullGCPtr<Response> error();
-    [[nodiscard]] static WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> redirect(String const& url, u16 status);
-    [[nodiscard]] static WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> json(JS::Value data, ResponseInit const& init = {});
+    [[nodiscard]] static JS::NonnullGCPtr<Response> error(JS::VM&);
+    [[nodiscard]] static WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> redirect(JS::VM&, String const& url, u16 status);
+    [[nodiscard]] static WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> json(JS::VM&, JS::Value data, ResponseInit const& init = {});
     [[nodiscard]] Bindings::ResponseType type() const;
     [[nodiscard]] String url() const;
     [[nodiscard]] bool redirected() const;

+ 1 - 1
Userland/Libraries/LibWeb/HTML/HTMLScriptElement.h

@@ -44,7 +44,7 @@ public:
     virtual void inserted() override;
 
     // https://html.spec.whatwg.org/multipage/scripting.html#dom-script-supports
-    static bool supports(String const& type)
+    static bool supports(JS::VM&, String const& type)
     {
         return type.is_one_of("classic", "module");
     }