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