From 9176a0de993637d2e6836b669c6eb40216bdb025 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 4 Sep 2022 21:48:54 +0200 Subject: [PATCH] LibWeb: Stop using Bindings::wrap() in a bunch of places wrap() is now basically a no-op so we should stop using it everywhere and eventually remove it. This patch removes uses of wrap() in non-generated code. --- .../Libraries/LibWeb/Crypto/SubtleCrypto.cpp | 12 +++---- Userland/Libraries/LibWeb/DOM/AbortSignal.cpp | 2 +- Userland/Libraries/LibWeb/DOM/EventTarget.cpp | 32 +++++++------------ .../Libraries/LibWeb/DOM/NodeIterator.cpp | 4 +-- Userland/Libraries/LibWeb/DOM/TreeWalker.cpp | 4 +-- Userland/Libraries/LibWeb/HTML/Window.cpp | 31 ++++++------------ .../WebContent/ConsoleGlobalObject.cpp | 3 +- 7 files changed, 32 insertions(+), 56 deletions(-) diff --git a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp index 575339a254..59cfb04c98 100644 --- a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp +++ b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp @@ -37,9 +37,9 @@ JS::Promise* SubtleCrypto::digest(String const& algorithm, JS::Handlereject(error); + promise->reject(error.ptr()); return promise; } auto& data_buffer = data_buffer_or_error.value(); @@ -58,9 +58,9 @@ JS::Promise* SubtleCrypto::digest(String const& algorithm, JS::Handlereject(error); + promise->reject(error.ptr()); return promise; } @@ -79,8 +79,8 @@ JS::Promise* SubtleCrypto::digest(String const& algorithm, JS::Handlereject(error); + auto error = DOM::OperationError::create(global_object(), "Failed to create result buffer"); + promise->reject(error.ptr()); return promise; } diff --git a/Userland/Libraries/LibWeb/DOM/AbortSignal.cpp b/Userland/Libraries/LibWeb/DOM/AbortSignal.cpp index f65291129d..d76e5b989d 100644 --- a/Userland/Libraries/LibWeb/DOM/AbortSignal.cpp +++ b/Userland/Libraries/LibWeb/DOM/AbortSignal.cpp @@ -43,7 +43,7 @@ void AbortSignal::signal_abort(JS::Value reason) if (!reason.is_undefined()) m_abort_reason = reason; else - m_abort_reason = wrap(realm(), AbortError::create(global_object(), "Aborted without reason")); + m_abort_reason = AbortError::create(global_object(), "Aborted without reason").ptr(); // 3. For each algorithm in signal’s abort algorithms: run algorithm. for (auto& algorithm : m_abort_algorithms) diff --git a/Userland/Libraries/LibWeb/DOM/EventTarget.cpp b/Userland/Libraries/LibWeb/DOM/EventTarget.cpp index f94161eb5f..37d1c38893 100644 --- a/Userland/Libraries/LibWeb/DOM/EventTarget.cpp +++ b/Userland/Libraries/LibWeb/DOM/EventTarget.cpp @@ -441,22 +441,16 @@ Bindings::CallbackType* EventTarget::get_current_value_of_event_handler(FlyStrin // 3. If eventHandler is an element's event handler, then set scope to NewObjectEnvironment(document, true, scope). // (Otherwise, eventHandler is a Window object's event handler.) - if (is(this)) { - auto* wrapped_document = Bindings::wrap(realm, *document); - scope = JS::new_object_environment(*wrapped_document, true, scope); - } + if (is(this)) + scope = JS::new_object_environment(*document, true, scope); // 4. If form owner is not null, then set scope to NewObjectEnvironment(form owner, true, scope). - if (form_owner) { - auto* wrapped_form_owner = Bindings::wrap(realm, *form_owner); - scope = JS::new_object_environment(*wrapped_form_owner, true, scope); - } + if (form_owner) + scope = JS::new_object_environment(*form_owner, true, scope); // 5. If element is not null, then set scope to NewObjectEnvironment(element, true, scope). - if (element) { - auto* wrapped_element = Bindings::wrap(realm, *element); - scope = JS::new_object_environment(*wrapped_element, true, scope); - } + if (element) + scope = JS::new_object_environment(*element, true, scope); // 6. Return scope. (NOTE: Not necessary) @@ -625,18 +619,14 @@ JS::ThrowCompletionOr EventTarget::process_event_handler_for_event(FlyStri // 4. Process the Event object event as follows: JS::Completion return_value_or_error; - // Needed for wrapping. - auto* callback_object = &callback->callback; - auto& realm = callback_object->shape().realm(); - if (special_error_event_handling) { // -> If special error event handling is true // Invoke callback with five arguments, the first one having the value of event's message attribute, the second having the value of event's filename attribute, the third having the value of event's lineno attribute, // the fourth having the value of event's colno attribute, the fifth having the value of event's error attribute, and with the callback this value set to event's currentTarget. // Let return value be the callback's return value. [WEBIDL] auto& error_event = verify_cast(event); - auto* wrapped_message = JS::js_string(callback_object->heap(), error_event.message()); - auto* wrapped_filename = JS::js_string(callback_object->heap(), error_event.filename()); + auto* wrapped_message = JS::js_string(vm(), error_event.message()); + auto* wrapped_filename = JS::js_string(vm(), error_event.filename()); auto wrapped_lineno = JS::Value(error_event.lineno()); auto wrapped_colno = JS::Value(error_event.colno()); @@ -645,7 +635,7 @@ JS::ThrowCompletionOr EventTarget::process_event_handler_for_event(FlyStri // NOTE: current_target is always non-null here, as the event dispatcher takes care to make sure it's non-null (and uses it as the this value for the callback!) // FIXME: This is rewrapping the this value of the callback defined in activate_event_handler. While I don't think this is observable as the event dispatcher // calls directly into the callback without considering things such as proxies, it is a waste. However, if it observable, then we must reuse the this_value that was given to the callback. - auto* this_value = Bindings::wrap(realm, *error_event.current_target()); + auto* this_value = error_event.current_target().ptr(); return_value_or_error = Bindings::IDL::invoke_callback(*callback, this_value, wrapped_message, wrapped_filename, wrapped_lineno, wrapped_colno, error_event.error()); } else { @@ -653,10 +643,10 @@ JS::ThrowCompletionOr EventTarget::process_event_handler_for_event(FlyStri // Invoke callback with one argument, the value of which is the Event object event, with the callback this value set to event's currentTarget. Let return value be the callback's return value. [WEBIDL] // FIXME: This has the same rewrapping issue as this_value. - auto* wrapped_event = Bindings::wrap(realm, event); + auto* wrapped_event = &event; // FIXME: The comments about this in the special_error_event_handling path also apply here. - auto* this_value = Bindings::wrap(realm, *event.current_target()); + auto* this_value = event.current_target().ptr(); return_value_or_error = Bindings::IDL::invoke_callback(*callback, this_value, wrapped_event); } diff --git a/Userland/Libraries/LibWeb/DOM/NodeIterator.cpp b/Userland/Libraries/LibWeb/DOM/NodeIterator.cpp index 6c9d3bacc2..648fcf1405 100644 --- a/Userland/Libraries/LibWeb/DOM/NodeIterator.cpp +++ b/Userland/Libraries/LibWeb/DOM/NodeIterator.cpp @@ -130,7 +130,7 @@ JS::ThrowCompletionOr NodeIterator::filter(Node& node) { // 1. If traverser’s active flag is set, then throw an "InvalidStateError" DOMException. if (m_active) - return JS::throw_completion(wrap(shape().realm(), InvalidStateError::create(global_object(), "NodeIterator is already active"))); + return throw_completion(InvalidStateError::create(global_object(), "NodeIterator is already active")); // 2. Let n be node’s nodeType attribute value − 1. auto n = node.node_type() - 1; @@ -148,7 +148,7 @@ JS::ThrowCompletionOr NodeIterator::filter(Node& node) // 6. Let result be the return value of call a user object’s operation with traverser’s filter, "acceptNode", and « node ». // If this throws an exception, then unset traverser’s active flag and rethrow the exception. - auto result = Bindings::IDL::call_user_object_operation(m_filter->callback(), "acceptNode", {}, wrap(shape().realm(), node)); + auto result = Bindings::IDL::call_user_object_operation(m_filter->callback(), "acceptNode", {}, &node); if (result.is_abrupt()) { m_active = false; return result; diff --git a/Userland/Libraries/LibWeb/DOM/TreeWalker.cpp b/Userland/Libraries/LibWeb/DOM/TreeWalker.cpp index 2a496c27e9..0244542556 100644 --- a/Userland/Libraries/LibWeb/DOM/TreeWalker.cpp +++ b/Userland/Libraries/LibWeb/DOM/TreeWalker.cpp @@ -231,7 +231,7 @@ JS::ThrowCompletionOr TreeWalker::filter(Node& node) { // 1. If traverser’s active flag is set, then throw an "InvalidStateError" DOMException. if (m_active) - return JS::throw_completion(wrap(shape().realm(), InvalidStateError::create(global_object(), "NodeIterator is already active"))); + return throw_completion(InvalidStateError::create(global_object(), "NodeIterator is already active")); // 2. Let n be node’s nodeType attribute value − 1. auto n = node.node_type() - 1; @@ -249,7 +249,7 @@ JS::ThrowCompletionOr TreeWalker::filter(Node& node) // 6. Let result be the return value of call a user object’s operation with traverser’s filter, "acceptNode", and « node ». // If this throws an exception, then unset traverser’s active flag and rethrow the exception. - auto result = Bindings::IDL::call_user_object_operation(m_filter->callback(), "acceptNode", {}, wrap(shape().realm(), node)); + auto result = Bindings::IDL::call_user_object_operation(m_filter->callback(), "acceptNode", {}, &node); if (result.is_abrupt()) { m_active = false; return result; diff --git a/Userland/Libraries/LibWeb/HTML/Window.cpp b/Userland/Libraries/LibWeb/HTML/Window.cpp index 8a50160ee6..db68182a71 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.cpp +++ b/Userland/Libraries/LibWeb/HTML/Window.cpp @@ -689,9 +689,7 @@ u32 Window::request_idle_callback_impl(Bindings::CallbackType& callback) auto handle = window.m_idle_callback_identifier; // 4. Push callback to the end of window's list of idle request callbacks, associated with handle. auto handler = [callback = JS::make_handle(callback)](JS::NonnullGCPtr deadline) -> JS::Completion { - auto& realm = callback->callback.shape().realm(); - auto* wrapped_deadline = Bindings::wrap(realm, *deadline); - return Bindings::IDL::invoke_callback(const_cast(*callback), {}, JS::Value(wrapped_deadline)); + return Bindings::IDL::invoke_callback(const_cast(*callback), {}, deadline.ptr()); }; window.m_idle_request_callbacks.append(adopt_ref(*new IdleCallback(move(handler), handle))); // 5. Return handle and then continue running this algorithm asynchronously. @@ -1101,16 +1099,14 @@ JS_DEFINE_NATIVE_FUNCTION(Window::parent_getter) JS_DEFINE_NATIVE_FUNCTION(Window::document_getter) { - auto& realm = *vm.current_realm(); auto* impl = TRY(impl_from(vm)); - return wrap(realm, impl->associated_document()); + return &impl->associated_document(); } JS_DEFINE_NATIVE_FUNCTION(Window::performance_getter) { - auto& realm = *vm.current_realm(); auto* impl = TRY(impl_from(vm)); - return wrap(realm, impl->performance()); + return &impl->performance(); } JS_DEFINE_NATIVE_FUNCTION(Window::performance_setter) @@ -1132,18 +1128,16 @@ JS_DEFINE_NATIVE_FUNCTION(Window::performance_setter) JS_DEFINE_NATIVE_FUNCTION(Window::screen_getter) { - auto& realm = *vm.current_realm(); auto* impl = TRY(impl_from(vm)); - return wrap(realm, impl->screen()); + return &impl->screen(); } JS_DEFINE_NATIVE_FUNCTION(Window::event_getter) { - auto& realm = *vm.current_realm(); auto* impl = TRY(impl_from(vm)); if (!impl->current_event()) return JS::js_undefined(); - return wrap(realm, const_cast(*impl->current_event())); + return impl->current_event(); } JS_DEFINE_NATIVE_FUNCTION(Window::event_setter) @@ -1166,9 +1160,8 @@ JS_DEFINE_NATIVE_FUNCTION(Window::location_setter) JS_DEFINE_NATIVE_FUNCTION(Window::crypto_getter) { - auto& realm = *vm.current_realm(); auto* impl = TRY(impl_from(vm)); - return wrap(realm, impl->crypto()); + return &impl->crypto(); } JS_DEFINE_NATIVE_FUNCTION(Window::inner_width_getter) @@ -1191,23 +1184,18 @@ JS_DEFINE_NATIVE_FUNCTION(Window::device_pixel_ratio_getter) JS_DEFINE_NATIVE_FUNCTION(Window::get_computed_style) { - auto& realm = *vm.current_realm(); auto* impl = TRY(impl_from(vm)); auto* object = TRY(vm.argument(0).to_object(vm)); if (!is(object)) return vm.throw_completion(JS::ErrorType::NotAnObjectOfType, "DOM element"); - return wrap(realm, *impl->get_computed_style_impl(*static_cast(object))); + return impl->get_computed_style_impl(*static_cast(object)); } JS_DEFINE_NATIVE_FUNCTION(Window::get_selection) { - auto& realm = *vm.current_realm(); auto* impl = TRY(impl_from(vm)); - auto* selection = impl->get_selection_impl(); - if (!selection) - return JS::js_null(); - return wrap(realm, *selection); + return impl->get_selection_impl(); } JS_DEFINE_NATIVE_FUNCTION(Window::match_media) @@ -1346,9 +1334,8 @@ JS_DEFINE_NATIVE_FUNCTION(Window::scroll_by) JS_DEFINE_NATIVE_FUNCTION(Window::history_getter) { - auto& realm = *vm.current_realm(); auto* impl = TRY(impl_from(vm)); - return wrap(realm, impl->associated_document().history()); + return impl->associated_document().history(); } JS_DEFINE_NATIVE_FUNCTION(Window::screen_left_getter) diff --git a/Userland/Services/WebContent/ConsoleGlobalObject.cpp b/Userland/Services/WebContent/ConsoleGlobalObject.cpp index 1402f4a12f..caec34e43a 100644 --- a/Userland/Services/WebContent/ConsoleGlobalObject.cpp +++ b/Userland/Services/WebContent/ConsoleGlobalObject.cpp @@ -94,7 +94,6 @@ JS::ThrowCompletionOr> ConsoleGlobalObject::internal JS_DEFINE_NATIVE_FUNCTION(ConsoleGlobalObject::inspected_node_getter) { - auto& realm = *vm.current_realm(); auto* this_object = TRY(vm.this_value().to_object(vm)); if (!is(this_object)) @@ -106,7 +105,7 @@ JS_DEFINE_NATIVE_FUNCTION(ConsoleGlobalObject::inspected_node_getter) if (!inspected_node) return JS::js_undefined(); - return Web::Bindings::wrap(realm, *inspected_node); + return inspected_node; } }