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:
Sam Atkins 2021-12-10 13:48:38 +00:00 committed by Andreas Kling
parent ce694490f3
commit ff5e07d718
Notes: sideshowbarker 2024-07-17 22:06:49 +09:00
5 changed files with 68 additions and 50 deletions

View file

@ -87,11 +87,36 @@ Value Console::clear()
return js_undefined();
}
Value Console::trace()
// 1.1.8. trace(...data), https://console.spec.whatwg.org/#trace
ThrowCompletionOr<Value> Console::trace()
{
if (m_client)
return m_client->trace();
return js_undefined();
if (!m_client)
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
@ -237,16 +262,6 @@ VM& ConsoleClient::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
ThrowCompletionOr<Value> ConsoleClient::logger(Console::LogLevel log_level, Vector<Value>& args)
{

View file

@ -43,6 +43,11 @@ public:
Warn,
};
struct Trace {
String label;
Vector<String> stack;
};
explicit Console(GlobalObject&);
void set_client(ConsoleClient& client) { m_client = &client; }
@ -62,7 +67,7 @@ public:
ThrowCompletionOr<Value> log();
ThrowCompletionOr<Value> warn();
Value clear();
Value trace();
ThrowCompletionOr<Value> trace();
ThrowCompletionOr<Value> count();
ThrowCompletionOr<Value> count_reset();
ThrowCompletionOr<Value> assert_();
@ -85,10 +90,9 @@ public:
ThrowCompletionOr<Value> logger(Console::LogLevel log_level, 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 Value trace() = 0;
protected:
virtual ~ConsoleClient() = default;
@ -98,8 +102,6 @@ protected:
GlobalObject& global_object() { return m_console.global_object(); }
const GlobalObject& global_object() const { return m_console.global_object(); }
Vector<String> get_trace() const;
Console& m_console;
};

View file

@ -120,24 +120,25 @@ void WebContentConsoleClient::clear()
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 = "&lt;anonymous&gt;";
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
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);
StringBuilder html;

View file

@ -25,8 +25,7 @@ public:
private:
virtual void clear() override;
virtual JS::Value trace() override;
virtual JS::ThrowCompletionOr<JS::Value> printer(JS::Console::LogLevel log_level, Vector<JS::Value>&) override;
virtual JS::ThrowCompletionOr<JS::Value> printer(JS::Console::LogLevel log_level, Variant<Vector<JS::Value>, JS::Console::Trace>) override;
ClientConnection& m_client;
WeakPtr<JS::Interpreter> m_interpreter;

View file

@ -1128,21 +1128,22 @@ public:
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());
auto trace = get_trace();
for (auto& function_name : trace) {
if (function_name.is_empty())
function_name = "<anonymous>";
js_outln(" -> {}", function_name);
}
return JS::js_undefined();
}
if (log_level == JS::Console::LogLevel::Trace) {
auto trace = arguments.get<JS::Console::Trace>();
StringBuilder builder;
if (!trace.label.is_empty())
builder.appendff("\033[36;1m{}\033[0m\n", trace.label);
virtual JS::ThrowCompletionOr<JS::Value> printer(JS::Console::LogLevel log_level, Vector<JS::Value>& arguments) override
{
auto output = String::join(" ", arguments);
for (auto& function_name : trace.stack)
builder.appendff("-> {}\n", function_name);
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);
switch (log_level) {