ConsoleGlobalObject.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "ConsoleGlobalObject.h"
  7. #include <LibJS/Runtime/Array.h>
  8. #include <LibJS/Runtime/Completion.h>
  9. #include <LibWeb/Bindings/ExceptionOrUtils.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/DOM/NodeList.h>
  12. #include <LibWeb/HTML/Window.h>
  13. namespace WebContent {
  14. ConsoleGlobalObject::ConsoleGlobalObject(JS::Realm& realm, Web::HTML::Window& parent_object)
  15. : GlobalObject(realm)
  16. , m_window_object(&parent_object)
  17. {
  18. }
  19. void ConsoleGlobalObject::initialize(JS::Realm& realm)
  20. {
  21. Base::initialize(realm);
  22. define_native_accessor(realm, "$0", $0_getter, nullptr, 0);
  23. define_native_accessor(realm, "$_", $__getter, nullptr, 0);
  24. define_native_function(realm, "$", $_function, 2, JS::default_attributes);
  25. define_native_function(realm, "$$", $$_function, 2, JS::default_attributes);
  26. }
  27. void ConsoleGlobalObject::visit_edges(Visitor& visitor)
  28. {
  29. Base::visit_edges(visitor);
  30. visitor.visit(m_window_object);
  31. }
  32. JS::ThrowCompletionOr<JS::Object*> ConsoleGlobalObject::internal_get_prototype_of() const
  33. {
  34. return m_window_object->internal_get_prototype_of();
  35. }
  36. JS::ThrowCompletionOr<bool> ConsoleGlobalObject::internal_set_prototype_of(JS::Object* prototype)
  37. {
  38. return m_window_object->internal_set_prototype_of(prototype);
  39. }
  40. JS::ThrowCompletionOr<bool> ConsoleGlobalObject::internal_is_extensible() const
  41. {
  42. return m_window_object->internal_is_extensible();
  43. }
  44. JS::ThrowCompletionOr<bool> ConsoleGlobalObject::internal_prevent_extensions()
  45. {
  46. return m_window_object->internal_prevent_extensions();
  47. }
  48. JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> ConsoleGlobalObject::internal_get_own_property(JS::PropertyKey const& property_name) const
  49. {
  50. if (auto result = TRY(m_window_object->internal_get_own_property(property_name)); result.has_value())
  51. return result;
  52. return Base::internal_get_own_property(property_name);
  53. }
  54. JS::ThrowCompletionOr<bool> ConsoleGlobalObject::internal_define_own_property(JS::PropertyKey const& property_name, JS::PropertyDescriptor const& descriptor)
  55. {
  56. return m_window_object->internal_define_own_property(property_name, descriptor);
  57. }
  58. JS::ThrowCompletionOr<bool> ConsoleGlobalObject::internal_has_property(JS::PropertyKey const& property_name) const
  59. {
  60. return TRY(Object::internal_has_property(property_name)) || TRY(m_window_object->internal_has_property(property_name));
  61. }
  62. JS::ThrowCompletionOr<JS::Value> ConsoleGlobalObject::internal_get(JS::PropertyKey const& property_name, JS::Value receiver) const
  63. {
  64. if (TRY(m_window_object->has_own_property(property_name)))
  65. return m_window_object->internal_get(property_name, (receiver == this) ? m_window_object : receiver);
  66. return Base::internal_get(property_name, receiver);
  67. }
  68. JS::ThrowCompletionOr<bool> ConsoleGlobalObject::internal_set(JS::PropertyKey const& property_name, JS::Value value, JS::Value receiver)
  69. {
  70. return m_window_object->internal_set(property_name, value, (receiver == this) ? m_window_object : receiver);
  71. }
  72. JS::ThrowCompletionOr<bool> ConsoleGlobalObject::internal_delete(JS::PropertyKey const& property_name)
  73. {
  74. return m_window_object->internal_delete(property_name);
  75. }
  76. JS::ThrowCompletionOr<JS::MarkedVector<JS::Value>> ConsoleGlobalObject::internal_own_property_keys() const
  77. {
  78. return m_window_object->internal_own_property_keys();
  79. }
  80. static JS::ThrowCompletionOr<ConsoleGlobalObject*> get_console(JS::VM& vm)
  81. {
  82. if (!is<ConsoleGlobalObject>(vm.get_global_object()))
  83. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "ConsoleGlobalObject");
  84. return static_cast<ConsoleGlobalObject*>(&vm.get_global_object());
  85. }
  86. JS_DEFINE_NATIVE_FUNCTION(ConsoleGlobalObject::$0_getter)
  87. {
  88. auto* console_global_object = TRY(get_console(vm));
  89. auto& window = *console_global_object->m_window_object;
  90. auto* inspected_node = window.associated_document().inspected_node();
  91. if (!inspected_node)
  92. return JS::js_undefined();
  93. return inspected_node;
  94. }
  95. JS_DEFINE_NATIVE_FUNCTION(ConsoleGlobalObject::$__getter)
  96. {
  97. auto* console_global_object = TRY(get_console(vm));
  98. return console_global_object->m_most_recent_result;
  99. }
  100. JS_DEFINE_NATIVE_FUNCTION(ConsoleGlobalObject::$_function)
  101. {
  102. auto* console_global_object = TRY(get_console(vm));
  103. auto& window = *console_global_object->m_window_object;
  104. auto selector = TRY(vm.argument(0).to_string(vm));
  105. if (vm.argument_count() > 1) {
  106. auto element_value = vm.argument(1);
  107. if (!(element_value.is_object() && is<Web::DOM::ParentNode>(element_value.as_object()))) {
  108. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "Node");
  109. }
  110. auto& element = static_cast<Web::DOM::ParentNode&>(element_value.as_object());
  111. return TRY(Web::Bindings::throw_dom_exception_if_needed(vm, [&]() {
  112. return element.query_selector(selector);
  113. }));
  114. }
  115. return TRY(Web::Bindings::throw_dom_exception_if_needed(vm, [&]() {
  116. return window.associated_document().query_selector(selector);
  117. }));
  118. }
  119. JS_DEFINE_NATIVE_FUNCTION(ConsoleGlobalObject::$$_function)
  120. {
  121. auto* console_global_object = TRY(get_console(vm));
  122. auto& window = *console_global_object->m_window_object;
  123. auto selector = TRY(vm.argument(0).to_string(vm));
  124. Web::DOM::ParentNode* element = &window.associated_document();
  125. if (vm.argument_count() > 1) {
  126. auto element_value = vm.argument(1);
  127. if (!(element_value.is_object() && is<Web::DOM::ParentNode>(element_value.as_object()))) {
  128. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "Node");
  129. }
  130. element = static_cast<Web::DOM::ParentNode*>(&element_value.as_object());
  131. }
  132. auto node_list = TRY(Web::Bindings::throw_dom_exception_if_needed(vm, [&]() {
  133. return element->query_selector_all(selector);
  134. }));
  135. auto* array = TRY(JS::Array::create(*vm.current_realm(), node_list->length()));
  136. for (auto i = 0u; i < node_list->length(); ++i) {
  137. TRY(array->create_data_property_or_throw(i, node_list->item_value(i)));
  138. }
  139. return array;
  140. }
  141. }