From 0823a3c4227ed0b08c9c53b28599d27fec6c63bf Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Wed, 5 Oct 2022 17:09:26 +0100 Subject: [PATCH] BindingsGenerator+LibWeb: Pass a VM to static IDL-based functions This saves us from having to yoink the VM out of thin air. --- .../LibWeb/BindingsGenerator/IDLGenerators.cpp | 6 ++++++ Userland/Libraries/LibWeb/Fetch/Response.cpp | 11 ++++------- Userland/Libraries/LibWeb/Fetch/Response.h | 6 +++--- Userland/Libraries/LibWeb/HTML/HTMLScriptElement.h | 2 +- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp index bd4a0df2e1..45259bb18d 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp +++ b/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@); })); )~~~"); diff --git a/Userland/Libraries/LibWeb/Fetch/Response.cpp b/Userland/Libraries/LibWeb/Fetch/Response.cpp index 8f5f17dafb..990b80aa86 100644 --- a/Userland/Libraries/LibWeb/Fetch/Response.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Response.cpp @@ -152,19 +152,17 @@ WebIDL::ExceptionOr> Response::construct_impl(JS::Rea } // https://fetch.spec.whatwg.org/#dom-response-error -JS::NonnullGCPtr Response::error() +JS::NonnullGCPtr 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> Response::redirect(String const& url, u16 status) +WebIDL::ExceptionOr> 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> Response::redirect(String const& } // https://fetch.spec.whatwg.org/#dom-response-json -WebIDL::ExceptionOr> Response::json(JS::Value data, ResponseInit const& init) +WebIDL::ExceptionOr> 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. diff --git a/Userland/Libraries/LibWeb/Fetch/Response.h b/Userland/Libraries/LibWeb/Fetch/Response.h index f8b60da670..ece9943e53 100644 --- a/Userland/Libraries/LibWeb/Fetch/Response.h +++ b/Userland/Libraries/LibWeb/Fetch/Response.h @@ -44,9 +44,9 @@ public: [[nodiscard]] NonnullRefPtr response() const { return m_response; } // JS API functions - [[nodiscard]] static JS::NonnullGCPtr error(); - [[nodiscard]] static WebIDL::ExceptionOr> redirect(String const& url, u16 status); - [[nodiscard]] static WebIDL::ExceptionOr> json(JS::Value data, ResponseInit const& init = {}); + [[nodiscard]] static JS::NonnullGCPtr error(JS::VM&); + [[nodiscard]] static WebIDL::ExceptionOr> redirect(JS::VM&, String const& url, u16 status); + [[nodiscard]] static WebIDL::ExceptionOr> json(JS::VM&, JS::Value data, ResponseInit const& init = {}); [[nodiscard]] Bindings::ResponseType type() const; [[nodiscard]] String url() const; [[nodiscard]] bool redirected() const; diff --git a/Userland/Libraries/LibWeb/HTML/HTMLScriptElement.h b/Userland/Libraries/LibWeb/HTML/HTMLScriptElement.h index ee9314a5e7..e241bca6a6 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLScriptElement.h +++ b/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"); }