ConsoleGlobalObject.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "ConsoleGlobalObject.h"
  7. #include <LibWeb/Bindings/NodeWrapper.h>
  8. #include <LibWeb/Bindings/NodeWrapperFactory.h>
  9. #include <LibWeb/Bindings/WindowObject.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/DOM/Window.h>
  12. namespace WebContent {
  13. ConsoleGlobalObject::ConsoleGlobalObject(Web::Bindings::WindowObject& parent_object)
  14. : m_window_object(&parent_object)
  15. {
  16. }
  17. ConsoleGlobalObject::~ConsoleGlobalObject()
  18. {
  19. }
  20. void ConsoleGlobalObject::initialize_global_object()
  21. {
  22. Base::initialize_global_object();
  23. // $0 magic variable
  24. define_native_accessor("$0", inspected_node_getter, nullptr, 0);
  25. }
  26. void ConsoleGlobalObject::visit_edges(Visitor& visitor)
  27. {
  28. Base::visit_edges(visitor);
  29. visitor.visit(m_window_object);
  30. }
  31. JS::Object* ConsoleGlobalObject::internal_get_prototype_of() const
  32. {
  33. return m_window_object->internal_get_prototype_of();
  34. }
  35. bool ConsoleGlobalObject::internal_set_prototype_of(JS::Object* prototype)
  36. {
  37. return m_window_object->internal_set_prototype_of(prototype);
  38. }
  39. bool ConsoleGlobalObject::internal_is_extensible() const
  40. {
  41. return m_window_object->internal_is_extensible();
  42. }
  43. bool ConsoleGlobalObject::internal_prevent_extensions()
  44. {
  45. return m_window_object->internal_prevent_extensions();
  46. }
  47. Optional<JS::PropertyDescriptor> ConsoleGlobalObject::internal_get_own_property(JS::PropertyName const& property_name) const
  48. {
  49. if (auto result = m_window_object->internal_get_own_property(property_name); result.has_value())
  50. return result;
  51. return Base::internal_get_own_property(property_name);
  52. }
  53. bool ConsoleGlobalObject::internal_define_own_property(JS::PropertyName const& property_name, JS::PropertyDescriptor const& descriptor)
  54. {
  55. return m_window_object->internal_define_own_property(property_name, descriptor);
  56. }
  57. bool ConsoleGlobalObject::internal_has_property(JS::PropertyName const& property_name) const
  58. {
  59. return Object::internal_has_property(property_name) || m_window_object->internal_has_property(property_name);
  60. }
  61. JS::Value ConsoleGlobalObject::internal_get(JS::PropertyName const& property_name, JS::Value receiver) const
  62. {
  63. if (m_window_object->has_own_property(property_name))
  64. return m_window_object->internal_get(property_name, (receiver == this) ? m_window_object : receiver);
  65. return Base::internal_get(property_name, receiver);
  66. }
  67. bool ConsoleGlobalObject::internal_set(JS::PropertyName const& property_name, JS::Value value, JS::Value receiver)
  68. {
  69. return m_window_object->internal_set(property_name, value, (receiver == this) ? m_window_object : receiver);
  70. }
  71. bool ConsoleGlobalObject::internal_delete(JS::PropertyName const& property_name)
  72. {
  73. return m_window_object->internal_delete(property_name);
  74. }
  75. JS::MarkedValueList ConsoleGlobalObject::internal_own_property_keys() const
  76. {
  77. return m_window_object->internal_own_property_keys();
  78. }
  79. JS_DEFINE_NATIVE_GETTER(ConsoleGlobalObject::inspected_node_getter)
  80. {
  81. auto* this_object = vm.this_value(global_object).to_object(global_object);
  82. if (!this_object)
  83. return JS::js_null();
  84. if (!is<ConsoleGlobalObject>(this_object)) {
  85. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotA, "ConsoleGlobalObject");
  86. return {};
  87. }
  88. auto console_global_object = static_cast<ConsoleGlobalObject*>(this_object);
  89. auto& window = console_global_object->m_window_object->impl();
  90. auto* inspected_node = window.document().inspected_node();
  91. if (!inspected_node)
  92. return JS::js_undefined();
  93. return Web::Bindings::wrap(global_object, *inspected_node);
  94. }
  95. }