2021-09-03 10:16:36 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ConsoleGlobalObject.h"
|
2021-09-28 23:30:17 +01:00
|
|
|
#include <LibJS/Runtime/Completion.h>
|
2021-09-03 10:16:36 +01:00
|
|
|
#include <LibWeb/DOM/Document.h>
|
2022-03-07 23:08:26 +01:00
|
|
|
#include <LibWeb/HTML/Window.h>
|
2021-09-03 10:16:36 +01:00
|
|
|
|
|
|
|
namespace WebContent {
|
|
|
|
|
2022-08-28 13:42:07 +02:00
|
|
|
ConsoleGlobalObject::ConsoleGlobalObject(JS::Realm& realm, Web::HTML::Window& parent_object)
|
2022-08-01 20:27:20 +02:00
|
|
|
: GlobalObject(realm)
|
|
|
|
, m_window_object(&parent_object)
|
2021-09-03 10:16:36 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-08-28 14:42:50 +01:00
|
|
|
void ConsoleGlobalObject::initialize(JS::Realm& realm)
|
2021-09-03 10:22:47 +01:00
|
|
|
{
|
2022-08-28 14:42:50 +01:00
|
|
|
Base::initialize(realm);
|
2021-09-03 10:22:47 +01:00
|
|
|
|
|
|
|
// $0 magic variable
|
2022-08-22 21:47:35 +01:00
|
|
|
define_native_accessor(realm, "$0", inspected_node_getter, nullptr, 0);
|
2021-09-03 10:22:47 +01:00
|
|
|
}
|
|
|
|
|
2021-09-03 10:16:36 +01:00
|
|
|
void ConsoleGlobalObject::visit_edges(Visitor& visitor)
|
|
|
|
{
|
|
|
|
Base::visit_edges(visitor);
|
|
|
|
visitor.visit(m_window_object);
|
|
|
|
}
|
|
|
|
|
2021-09-28 23:30:17 +01:00
|
|
|
JS::ThrowCompletionOr<JS::Object*> ConsoleGlobalObject::internal_get_prototype_of() const
|
2021-09-03 10:16:36 +01:00
|
|
|
{
|
|
|
|
return m_window_object->internal_get_prototype_of();
|
|
|
|
}
|
|
|
|
|
2021-09-28 23:54:42 +01:00
|
|
|
JS::ThrowCompletionOr<bool> ConsoleGlobalObject::internal_set_prototype_of(JS::Object* prototype)
|
2021-09-03 10:16:36 +01:00
|
|
|
{
|
|
|
|
return m_window_object->internal_set_prototype_of(prototype);
|
|
|
|
}
|
|
|
|
|
2021-09-29 00:02:05 +01:00
|
|
|
JS::ThrowCompletionOr<bool> ConsoleGlobalObject::internal_is_extensible() const
|
2021-09-03 10:16:36 +01:00
|
|
|
{
|
|
|
|
return m_window_object->internal_is_extensible();
|
|
|
|
}
|
|
|
|
|
2021-09-29 00:13:41 +01:00
|
|
|
JS::ThrowCompletionOr<bool> ConsoleGlobalObject::internal_prevent_extensions()
|
2021-09-03 10:16:36 +01:00
|
|
|
{
|
|
|
|
return m_window_object->internal_prevent_extensions();
|
|
|
|
}
|
|
|
|
|
2021-10-24 16:01:24 +02:00
|
|
|
JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> ConsoleGlobalObject::internal_get_own_property(JS::PropertyKey const& property_name) const
|
2021-09-03 10:16:36 +01:00
|
|
|
{
|
2021-09-29 17:53:57 +01:00
|
|
|
if (auto result = TRY(m_window_object->internal_get_own_property(property_name)); result.has_value())
|
2021-09-03 10:16:36 +01:00
|
|
|
return result;
|
|
|
|
|
|
|
|
return Base::internal_get_own_property(property_name);
|
|
|
|
}
|
|
|
|
|
2021-10-24 16:01:24 +02:00
|
|
|
JS::ThrowCompletionOr<bool> ConsoleGlobalObject::internal_define_own_property(JS::PropertyKey const& property_name, JS::PropertyDescriptor const& descriptor)
|
2021-09-03 10:16:36 +01:00
|
|
|
{
|
|
|
|
return m_window_object->internal_define_own_property(property_name, descriptor);
|
|
|
|
}
|
|
|
|
|
2021-10-24 16:01:24 +02:00
|
|
|
JS::ThrowCompletionOr<bool> ConsoleGlobalObject::internal_has_property(JS::PropertyKey const& property_name) const
|
2021-09-03 10:16:36 +01:00
|
|
|
{
|
2021-09-29 18:08:06 +01:00
|
|
|
return TRY(Object::internal_has_property(property_name)) || TRY(m_window_object->internal_has_property(property_name));
|
2021-09-03 10:16:36 +01:00
|
|
|
}
|
|
|
|
|
2021-10-24 16:01:24 +02:00
|
|
|
JS::ThrowCompletionOr<JS::Value> ConsoleGlobalObject::internal_get(JS::PropertyKey const& property_name, JS::Value receiver) const
|
2021-09-03 10:16:36 +01:00
|
|
|
{
|
2021-10-03 02:17:33 +01:00
|
|
|
if (TRY(m_window_object->has_own_property(property_name)))
|
2021-09-03 10:16:36 +01:00
|
|
|
return m_window_object->internal_get(property_name, (receiver == this) ? m_window_object : receiver);
|
|
|
|
|
|
|
|
return Base::internal_get(property_name, receiver);
|
|
|
|
}
|
|
|
|
|
2021-10-24 16:01:24 +02:00
|
|
|
JS::ThrowCompletionOr<bool> ConsoleGlobalObject::internal_set(JS::PropertyKey const& property_name, JS::Value value, JS::Value receiver)
|
2021-09-03 10:16:36 +01:00
|
|
|
{
|
|
|
|
return m_window_object->internal_set(property_name, value, (receiver == this) ? m_window_object : receiver);
|
|
|
|
}
|
|
|
|
|
2021-10-24 16:01:24 +02:00
|
|
|
JS::ThrowCompletionOr<bool> ConsoleGlobalObject::internal_delete(JS::PropertyKey const& property_name)
|
2021-09-03 10:16:36 +01:00
|
|
|
{
|
|
|
|
return m_window_object->internal_delete(property_name);
|
|
|
|
}
|
|
|
|
|
2022-02-09 10:06:40 +00:00
|
|
|
JS::ThrowCompletionOr<JS::MarkedVector<JS::Value>> ConsoleGlobalObject::internal_own_property_keys() const
|
2021-09-03 10:16:36 +01:00
|
|
|
{
|
|
|
|
return m_window_object->internal_own_property_keys();
|
|
|
|
}
|
|
|
|
|
2021-10-31 17:05:46 +02:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(ConsoleGlobalObject::inspected_node_getter)
|
2021-09-03 10:22:47 +01:00
|
|
|
{
|
2022-08-21 14:00:56 +01:00
|
|
|
auto* this_object = TRY(vm.this_value().to_object(vm));
|
2021-09-03 10:22:47 +01:00
|
|
|
|
2021-10-31 17:05:46 +02:00
|
|
|
if (!is<ConsoleGlobalObject>(this_object))
|
2022-08-16 20:33:17 +01:00
|
|
|
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "ConsoleGlobalObject");
|
2021-09-03 10:22:47 +01:00
|
|
|
|
|
|
|
auto console_global_object = static_cast<ConsoleGlobalObject*>(this_object);
|
2022-09-21 17:45:18 +01:00
|
|
|
auto& window = *console_global_object->m_window_object;
|
2021-09-09 13:55:31 +02:00
|
|
|
auto* inspected_node = window.associated_document().inspected_node();
|
2021-09-03 10:22:47 +01:00
|
|
|
if (!inspected_node)
|
|
|
|
return JS::js_undefined();
|
|
|
|
|
2022-09-04 21:48:54 +02:00
|
|
|
return inspected_node;
|
2021-09-03 10:22:47 +01:00
|
|
|
}
|
|
|
|
|
2021-09-03 10:16:36 +01:00
|
|
|
}
|