mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-25 22:08:59 +00:00
LibJS+WebContent+js: Bring console.trace() to spec
The spec very kindly defines `Printer` as accepting "Implementation-specific representations of printable things such as a stack trace or group." for the `args`. We make use of that here by passing the `Trace` itself to `Printer`, instead of having to produce a representation of the stack trace in advance and then pass that to `Printer`. That both avoids the hassle of tracking whether the data has been html-encoded or not, and means clients don't have to implement the whole `trace()` algorithm, but only the code needed to output the trace.
This commit is contained in:
parent
ce694490f3
commit
ff5e07d718
Notes:
sideshowbarker
2024-07-17 22:06:49 +09:00
Author: https://github.com/AtkinsSJ
Commit: ff5e07d718
Pull-request: https://github.com/SerenityOS/serenity/pull/11382
Reviewed-by: https://github.com/davidot ✅
5 changed files with 68 additions and 50 deletions
|
@ -87,11 +87,36 @@ Value Console::clear()
|
||||||
return js_undefined();
|
return js_undefined();
|
||||||
}
|
}
|
||||||
|
|
||||||
Value Console::trace()
|
// 1.1.8. trace(...data), https://console.spec.whatwg.org/#trace
|
||||||
|
ThrowCompletionOr<Value> Console::trace()
|
||||||
{
|
{
|
||||||
if (m_client)
|
if (!m_client)
|
||||||
return m_client->trace();
|
return js_undefined();
|
||||||
return js_undefined();
|
|
||||||
|
// 1. Let trace be some implementation-specific, potentially-interactive representation of the callstack from where this function was called.
|
||||||
|
Console::Trace trace;
|
||||||
|
auto& execution_context_stack = vm().execution_context_stack();
|
||||||
|
// NOTE: -2 to skip the console.trace() execution context
|
||||||
|
for (ssize_t i = execution_context_stack.size() - 2; i >= 0; --i) {
|
||||||
|
auto& function_name = execution_context_stack[i]->function_name;
|
||||||
|
trace.stack.append(function_name.is_empty() ? "<anonymous>" : function_name);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Optionally, let formattedData be the result of Formatter(data), and incorporate formattedData as a label for trace.
|
||||||
|
if (vm().argument_count() > 0) {
|
||||||
|
StringBuilder builder;
|
||||||
|
auto data = vm_arguments();
|
||||||
|
auto formatted_data = TRY(m_client->formatter(data));
|
||||||
|
for (auto const& item : formatted_data) {
|
||||||
|
if (!builder.is_empty())
|
||||||
|
builder.append(' ');
|
||||||
|
builder.append(TRY(item.to_string(global_object())));
|
||||||
|
}
|
||||||
|
trace.label = builder.to_string();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Perform Printer("trace", « trace »).
|
||||||
|
return m_client->printer(JS::Console::LogLevel::Trace, trace);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1.2.1. count(label), https://console.spec.whatwg.org/#count
|
// 1.2.1. count(label), https://console.spec.whatwg.org/#count
|
||||||
|
@ -237,16 +262,6 @@ VM& ConsoleClient::vm()
|
||||||
return global_object().vm();
|
return global_object().vm();
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector<String> ConsoleClient::get_trace() const
|
|
||||||
{
|
|
||||||
Vector<String> trace;
|
|
||||||
auto& execution_context_stack = m_console.global_object().vm().execution_context_stack();
|
|
||||||
// NOTE: -2 to skip the console.trace() execution context
|
|
||||||
for (ssize_t i = execution_context_stack.size() - 2; i >= 0; --i)
|
|
||||||
trace.append(execution_context_stack[i]->function_name);
|
|
||||||
return trace;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2.1. Logger(logLevel, args), https://console.spec.whatwg.org/#logger
|
// 2.1. Logger(logLevel, args), https://console.spec.whatwg.org/#logger
|
||||||
ThrowCompletionOr<Value> ConsoleClient::logger(Console::LogLevel log_level, Vector<Value>& args)
|
ThrowCompletionOr<Value> ConsoleClient::logger(Console::LogLevel log_level, Vector<Value>& args)
|
||||||
{
|
{
|
||||||
|
|
|
@ -43,6 +43,11 @@ public:
|
||||||
Warn,
|
Warn,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct Trace {
|
||||||
|
String label;
|
||||||
|
Vector<String> stack;
|
||||||
|
};
|
||||||
|
|
||||||
explicit Console(GlobalObject&);
|
explicit Console(GlobalObject&);
|
||||||
|
|
||||||
void set_client(ConsoleClient& client) { m_client = &client; }
|
void set_client(ConsoleClient& client) { m_client = &client; }
|
||||||
|
@ -62,7 +67,7 @@ public:
|
||||||
ThrowCompletionOr<Value> log();
|
ThrowCompletionOr<Value> log();
|
||||||
ThrowCompletionOr<Value> warn();
|
ThrowCompletionOr<Value> warn();
|
||||||
Value clear();
|
Value clear();
|
||||||
Value trace();
|
ThrowCompletionOr<Value> trace();
|
||||||
ThrowCompletionOr<Value> count();
|
ThrowCompletionOr<Value> count();
|
||||||
ThrowCompletionOr<Value> count_reset();
|
ThrowCompletionOr<Value> count_reset();
|
||||||
ThrowCompletionOr<Value> assert_();
|
ThrowCompletionOr<Value> assert_();
|
||||||
|
@ -85,10 +90,9 @@ public:
|
||||||
|
|
||||||
ThrowCompletionOr<Value> logger(Console::LogLevel log_level, Vector<Value>& args);
|
ThrowCompletionOr<Value> logger(Console::LogLevel log_level, Vector<Value>& args);
|
||||||
ThrowCompletionOr<Vector<Value>> formatter(Vector<Value>& args);
|
ThrowCompletionOr<Vector<Value>> formatter(Vector<Value>& args);
|
||||||
virtual ThrowCompletionOr<Value> printer(Console::LogLevel log_level, Vector<Value>&) = 0;
|
virtual ThrowCompletionOr<Value> printer(Console::LogLevel log_level, Variant<Vector<Value>, Console::Trace>) = 0;
|
||||||
|
|
||||||
virtual void clear() = 0;
|
virtual void clear() = 0;
|
||||||
virtual Value trace() = 0;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual ~ConsoleClient() = default;
|
virtual ~ConsoleClient() = default;
|
||||||
|
@ -98,8 +102,6 @@ protected:
|
||||||
GlobalObject& global_object() { return m_console.global_object(); }
|
GlobalObject& global_object() { return m_console.global_object(); }
|
||||||
const GlobalObject& global_object() const { return m_console.global_object(); }
|
const GlobalObject& global_object() const { return m_console.global_object(); }
|
||||||
|
|
||||||
Vector<String> get_trace() const;
|
|
||||||
|
|
||||||
Console& m_console;
|
Console& m_console;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -120,24 +120,25 @@ void WebContentConsoleClient::clear()
|
||||||
clear_output();
|
clear_output();
|
||||||
}
|
}
|
||||||
|
|
||||||
JS::Value WebContentConsoleClient::trace()
|
|
||||||
{
|
|
||||||
StringBuilder html;
|
|
||||||
html.append(escape_html_entities(vm().join_arguments()));
|
|
||||||
auto trace = get_trace();
|
|
||||||
for (auto& function_name : trace) {
|
|
||||||
if (function_name.is_empty())
|
|
||||||
function_name = "<anonymous>";
|
|
||||||
html.appendff(" -> {}<br>", function_name);
|
|
||||||
}
|
|
||||||
print_html(html.string_view());
|
|
||||||
return JS::js_undefined();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2.3. Printer(logLevel, args[, options]), https://console.spec.whatwg.org/#printer
|
// 2.3. Printer(logLevel, args[, options]), https://console.spec.whatwg.org/#printer
|
||||||
JS::ThrowCompletionOr<JS::Value> WebContentConsoleClient::printer(JS::Console::LogLevel log_level, Vector<JS::Value>& arguments)
|
JS::ThrowCompletionOr<JS::Value> WebContentConsoleClient::printer(JS::Console::LogLevel log_level, Variant<Vector<JS::Value>, JS::Console::Trace> arguments)
|
||||||
{
|
{
|
||||||
auto output = String::join(" ", arguments);
|
if (log_level == JS::Console::LogLevel::Trace) {
|
||||||
|
auto trace = arguments.get<JS::Console::Trace>();
|
||||||
|
StringBuilder html;
|
||||||
|
if (!trace.label.is_empty())
|
||||||
|
html.appendff("<span class='title'>{}</span><br>", escape_html_entities(trace.label));
|
||||||
|
|
||||||
|
html.append("<span class='trace'>");
|
||||||
|
for (auto& function_name : trace.stack)
|
||||||
|
html.appendff("-> {}<br>", escape_html_entities(function_name));
|
||||||
|
html.append("</span>");
|
||||||
|
|
||||||
|
print_html(html.string_view());
|
||||||
|
return JS::js_undefined();
|
||||||
|
}
|
||||||
|
|
||||||
|
auto output = String::join(" ", arguments.get<Vector<JS::Value>>());
|
||||||
m_console.output_debug_message(log_level, output);
|
m_console.output_debug_message(log_level, output);
|
||||||
|
|
||||||
StringBuilder html;
|
StringBuilder html;
|
||||||
|
|
|
@ -25,8 +25,7 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual void clear() override;
|
virtual void clear() override;
|
||||||
virtual JS::Value trace() override;
|
virtual JS::ThrowCompletionOr<JS::Value> printer(JS::Console::LogLevel log_level, Variant<Vector<JS::Value>, JS::Console::Trace>) override;
|
||||||
virtual JS::ThrowCompletionOr<JS::Value> printer(JS::Console::LogLevel log_level, Vector<JS::Value>&) override;
|
|
||||||
|
|
||||||
ClientConnection& m_client;
|
ClientConnection& m_client;
|
||||||
WeakPtr<JS::Interpreter> m_interpreter;
|
WeakPtr<JS::Interpreter> m_interpreter;
|
||||||
|
|
|
@ -1128,21 +1128,22 @@ public:
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual JS::Value trace() override
|
virtual JS::ThrowCompletionOr<JS::Value> printer(JS::Console::LogLevel log_level, Variant<Vector<JS::Value>, JS::Console::Trace> arguments) override
|
||||||
{
|
{
|
||||||
js_outln("{}", vm().join_arguments());
|
if (log_level == JS::Console::LogLevel::Trace) {
|
||||||
auto trace = get_trace();
|
auto trace = arguments.get<JS::Console::Trace>();
|
||||||
for (auto& function_name : trace) {
|
StringBuilder builder;
|
||||||
if (function_name.is_empty())
|
if (!trace.label.is_empty())
|
||||||
function_name = "<anonymous>";
|
builder.appendff("\033[36;1m{}\033[0m\n", trace.label);
|
||||||
js_outln(" -> {}", function_name);
|
|
||||||
}
|
|
||||||
return JS::js_undefined();
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual JS::ThrowCompletionOr<JS::Value> printer(JS::Console::LogLevel log_level, Vector<JS::Value>& arguments) override
|
for (auto& function_name : trace.stack)
|
||||||
{
|
builder.appendff("-> {}\n", function_name);
|
||||||
auto output = String::join(" ", arguments);
|
|
||||||
|
js_outln("{}", builder.string_view());
|
||||||
|
return JS::js_undefined();
|
||||||
|
}
|
||||||
|
|
||||||
|
auto output = String::join(" ", arguments.get<Vector<JS::Value>>());
|
||||||
m_console.output_debug_message(log_level, output);
|
m_console.output_debug_message(log_level, output);
|
||||||
|
|
||||||
switch (log_level) {
|
switch (log_level) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue