summaryrefslogtreecommitdiff
path: root/Userland/Services/WebContent/WebContentConsoleClient.cpp
diff options
context:
space:
mode:
authorSam Atkins <atkinssj@gmail.com>2021-09-03 10:16:36 +0100
committerAndreas Kling <kling@serenityos.org>2021-09-06 18:20:26 +0200
commit7838eab34158bcb317fad5d00ae50691b0e9e02f (patch)
treefef1513c3b1346f47d8a58b38e80a6f1e57fafba /Userland/Services/WebContent/WebContentConsoleClient.cpp
parentca2c1299233678cb4fe7fcdb9ded21e8bdde5d0e (diff)
downloadserenity-7838eab34158bcb317fad5d00ae50691b0e9e02f.zip
WebContent: Implement ConsoleGlobalObject which proxies to WindowObject
ConsoleGlobalObject is used as the global object when running javascript from the Browser console. This lets us implement console-only functions and variables (like `$0`) without exposing them to webpage content. It passes other calls over to the usual WindowObject so any code that would have worked in the webpage will still work in the console. :^)
Diffstat (limited to 'Userland/Services/WebContent/WebContentConsoleClient.cpp')
-rw-r--r--Userland/Services/WebContent/WebContentConsoleClient.cpp19
1 files changed, 16 insertions, 3 deletions
diff --git a/Userland/Services/WebContent/WebContentConsoleClient.cpp b/Userland/Services/WebContent/WebContentConsoleClient.cpp
index 7a3fbe6bb2..b3a9c0ef9d 100644
--- a/Userland/Services/WebContent/WebContentConsoleClient.cpp
+++ b/Userland/Services/WebContent/WebContentConsoleClient.cpp
@@ -1,6 +1,7 @@
/*
* Copyright (c) 2021, Brandon Scott <xeon.productions@gmail.com>
* Copyright (c) 2020, Hunter Salyer <thefalsehonesty@gmail.com>
+ * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@@ -9,10 +10,22 @@
#include <LibJS/Interpreter.h>
#include <LibJS/MarkupGenerator.h>
#include <LibJS/Parser.h>
-#include <LibWeb/Bindings/DOMExceptionWrapper.h>
+#include <LibWeb/Bindings/WindowObject.h>
+#include <WebContent/ConsoleGlobalObject.h>
namespace WebContent {
+WebContentConsoleClient::WebContentConsoleClient(JS::Console& console, WeakPtr<JS::Interpreter> interpreter, ClientConnection& client)
+ : ConsoleClient(console)
+ , m_client(client)
+ , m_interpreter(interpreter)
+{
+ JS::DeferGC defer_gc(m_interpreter->heap());
+ auto console_global_object = m_interpreter->heap().allocate_without_global_object<ConsoleGlobalObject>(static_cast<Web::Bindings::WindowObject&>(m_interpreter->global_object()));
+ console_global_object->initialize_global_object();
+ m_console_global_object = JS::make_handle(console_global_object);
+}
+
void WebContentConsoleClient::handle_input(const String& js_source)
{
auto parser = JS::Parser(JS::Lexer(js_source));
@@ -24,9 +37,9 @@ void WebContentConsoleClient::handle_input(const String& js_source)
auto hint = error.source_location_hint(js_source);
if (!hint.is_empty())
output_html.append(String::formatted("<pre>{}</pre>", escape_html_entities(hint)));
- m_interpreter->vm().throw_exception<JS::SyntaxError>(m_interpreter->global_object(), error.to_string());
+ m_interpreter->vm().throw_exception<JS::SyntaxError>(*m_console_global_object.cell(), error.to_string());
} else {
- m_interpreter->run(m_interpreter->global_object(), *program);
+ m_interpreter->run(*m_console_global_object.cell(), *program);
}
if (m_interpreter->exception()) {