2021-02-27 21:44:49 -06:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Brandon Scott <xeon.productions@gmail.com>
|
|
|
|
* Copyright (c) 2020, Hunter Salyer <thefalsehonesty@gmail.com>
|
2022-11-19 16:12:19 +00:00
|
|
|
* Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
|
2024-08-09 23:24:26 +02:00
|
|
|
* Copyright (c) 2024, Gasim Gasimzada <gasim@gasimzada.net>
|
2021-02-27 21:44:49 -06:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-02-27 21:44:49 -06:00
|
|
|
*/
|
|
|
|
|
2024-10-31 08:03:32 -04:00
|
|
|
#include <LibJS/Runtime/GlobalEnvironment.h>
|
|
|
|
#include <LibJS/Runtime/ObjectEnvironment.h>
|
2023-02-10 11:40:52 -05:00
|
|
|
#include <LibJS/Runtime/Realm.h>
|
2024-10-31 08:03:32 -04:00
|
|
|
#include <LibWeb/HTML/Scripting/ClassicScript.h>
|
|
|
|
#include <LibWeb/HTML/Scripting/Environments.h>
|
2022-12-09 18:48:25 +01:00
|
|
|
#include <WebContent/ConsoleGlobalEnvironmentExtensions.h>
|
2024-02-02 18:00:48 -07:00
|
|
|
#include <WebContent/PageClient.h>
|
|
|
|
#include <WebContent/WebContentConsoleClient.h>
|
2021-02-27 21:44:49 -06:00
|
|
|
|
|
|
|
namespace WebContent {
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DEFINE_ALLOCATOR(WebContentConsoleClient);
|
2024-04-20 21:19:51 +02:00
|
|
|
|
2025-02-24 09:48:13 -05:00
|
|
|
WebContentConsoleClient::WebContentConsoleClient(JS::Realm& realm, JS::Console& console, PageClient& client, ConsoleGlobalEnvironmentExtensions& console_global_environment_extensions)
|
2021-09-03 10:16:36 +01:00
|
|
|
: ConsoleClient(console)
|
2025-02-24 09:48:13 -05:00
|
|
|
, m_realm(realm)
|
2021-09-03 10:16:36 +01:00
|
|
|
, m_client(client)
|
2025-02-24 09:48:13 -05:00
|
|
|
, m_console_global_environment_extensions(console_global_environment_extensions)
|
2021-09-03 10:16:36 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-04-20 21:19:51 +02:00
|
|
|
WebContentConsoleClient::~WebContentConsoleClient() = default;
|
|
|
|
|
|
|
|
void WebContentConsoleClient::visit_edges(JS::Cell::Visitor& visitor)
|
|
|
|
{
|
|
|
|
Base::visit_edges(visitor);
|
2025-02-24 09:48:13 -05:00
|
|
|
visitor.visit(m_realm);
|
2024-04-20 21:19:51 +02:00
|
|
|
visitor.visit(m_client);
|
|
|
|
visitor.visit(m_console_global_environment_extensions);
|
|
|
|
}
|
|
|
|
|
2025-02-23 08:57:40 -05:00
|
|
|
void WebContentConsoleClient::handle_input(StringView js_source)
|
2021-02-27 21:44:49 -06:00
|
|
|
{
|
2024-10-31 08:03:32 -04:00
|
|
|
auto& settings = Web::HTML::relevant_settings_object(*m_console_global_environment_extensions);
|
|
|
|
auto script = Web::HTML::ClassicScript::create("(console)", js_source, settings.realm(), settings.api_base_url());
|
2021-10-03 13:58:26 +02:00
|
|
|
|
2024-10-31 08:03:32 -04:00
|
|
|
auto with_scope = JS::new_object_environment(*m_console_global_environment_extensions, true, &settings.realm().global_environment());
|
2021-10-03 13:58:26 +02:00
|
|
|
|
2024-10-31 08:03:32 -04:00
|
|
|
// FIXME: Add parse error printouts back once ClassicScript can report parse errors.
|
|
|
|
auto result = script->run(Web::HTML::ClassicScript::RethrowErrors::No, with_scope);
|
2021-10-14 16:12:53 +01:00
|
|
|
|
2025-04-04 18:11:45 +02:00
|
|
|
if (!result.is_error()) {
|
|
|
|
m_console_global_environment_extensions->set_most_recent_result(result.value());
|
|
|
|
handle_result(result.value());
|
2022-10-03 14:41:41 +01:00
|
|
|
}
|
2021-02-27 21:44:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|