1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-11 10:18:15 +09:00

LibWebView+WebContent: Begin supporting the DevTools JavaScript console

This supports evaluating the script and replying with the result. We
currently serialize JS objects to a string, but we will need to support
dynamic interaction with the objects over IPC. This does not yet support
sending console messages to DevTools.
This commit is contained in:
Timothy Flynn 2025-02-24 11:57:33 -05:00 committed by Andreas Kling
parent 6d33b70e61
commit 32bc2dc7b6
Notes: github-actions[bot] 2025-02-28 12:09:35 +00:00
10 changed files with 101 additions and 2 deletions

View file

@ -4,7 +4,13 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/JsonObject.h>
#include <AK/JsonValue.h>
#include <AK/MemoryStream.h>
#include <LibJS/Print.h>
#include <LibJS/Runtime/BigInt.h>
#include <LibJS/Runtime/Realm.h>
#include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
#include <LibWeb/HTML/Window.h>
#include <WebContent/ConsoleGlobalEnvironmentExtensions.h>
#include <WebContent/DevToolsConsoleClient.h>
@ -29,9 +35,67 @@ DevToolsConsoleClient::DevToolsConsoleClient(JS::Realm& realm, JS::Console& cons
DevToolsConsoleClient::~DevToolsConsoleClient() = default;
// https://firefox-source-docs.mozilla.org/devtools/backend/protocol.html#grips
static JsonValue serialize_js_value(JS::Realm& realm, JS::Value value)
{
auto& vm = realm.vm();
auto serialize_type = [](StringView type) {
JsonObject serialized;
serialized.set("type"sv, type);
return serialized;
};
if (value.is_undefined())
return serialize_type("undefined"sv);
if (value.is_null())
return serialize_type("null"sv);
if (value.is_boolean())
return value.as_bool();
if (value.is_string())
return value.as_string().utf8_string();
if (value.is_number()) {
if (value.is_nan())
return serialize_type("NaN"sv);
if (value.is_positive_infinity())
return serialize_type("Infinity"sv);
if (value.is_negative_infinity())
return serialize_type("-Infinity"sv);
if (value.is_negative_zero())
return serialize_type("-0"sv);
return value.as_double();
}
if (value.is_bigint()) {
auto serialized = serialize_type("BigInt"sv);
serialized.set("text"sv, MUST(value.as_bigint().big_integer().to_base(10)));
return serialized;
}
if (value.is_symbol())
return MUST(value.as_symbol().descriptive_string());
// FIXME: Handle serialization of object grips. For now, we stringify the object.
if (value.is_object()) {
Web::HTML::TemporaryExecutionContext execution_context { realm };
AllocatingMemoryStream stream;
JS::PrintContext context { vm, stream, true };
MUST(JS::print(value, context));
return MUST(String::from_stream(stream, stream.used_buffer_size()));
}
return {};
}
void DevToolsConsoleClient::handle_result(JS::Value result)
{
(void)result;
m_client->did_execute_js_console_input(serialize_js_value(m_realm, result));
}
void DevToolsConsoleClient::report_exception(JS::Error const& exception, bool in_promise)