mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +00:00
LibJS+WebContent+js: Bring console.assert() to spec
This commit is contained in:
parent
9b78e287b0
commit
ce694490f3
Notes:
sideshowbarker
2024-07-17 22:06:53 +09:00
Author: https://github.com/AtkinsSJ Commit: https://github.com/SerenityOS/serenity/commit/ce694490f31 Pull-request: https://github.com/SerenityOS/serenity/pull/11382 Reviewed-by: https://github.com/davidot ✅
5 changed files with 44 additions and 41 deletions
|
@ -149,10 +149,50 @@ ThrowCompletionOr<Value> Console::count_reset()
|
||||||
return js_undefined();
|
return js_undefined();
|
||||||
}
|
}
|
||||||
|
|
||||||
Value Console::assert_()
|
// 1.1.1. assert(condition, ...data), https://console.spec.whatwg.org/#assert
|
||||||
|
ThrowCompletionOr<Value> Console::assert_()
|
||||||
{
|
{
|
||||||
|
// 1. If condition is true, return.
|
||||||
|
auto condition = vm().argument(0).to_boolean();
|
||||||
|
if (condition)
|
||||||
|
return js_undefined();
|
||||||
|
|
||||||
|
// 2. Let message be a string without any formatting specifiers indicating generically an assertion failure (such as "Assertion failed").
|
||||||
|
auto message = js_string(vm(), "Assertion failed");
|
||||||
|
|
||||||
|
// NOTE: Assemble `data` from the function arguments.
|
||||||
|
Vector<JS::Value> data;
|
||||||
|
if (vm().argument_count() > 1) {
|
||||||
|
data.ensure_capacity(vm().argument_count() - 1);
|
||||||
|
for (size_t i = 1; i < vm().argument_count(); ++i) {
|
||||||
|
data.append(vm().argument(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. If data is empty, append message to data.
|
||||||
|
if (data.is_empty()) {
|
||||||
|
data.append(message);
|
||||||
|
}
|
||||||
|
// 4. Otherwise:
|
||||||
|
else {
|
||||||
|
// 1. Let first be data[0].
|
||||||
|
auto& first = data[0];
|
||||||
|
// 2. If Type(first) is not String, then prepend message to data.
|
||||||
|
if (!first.is_string()) {
|
||||||
|
data.prepend(message);
|
||||||
|
}
|
||||||
|
// 3. Otherwise:
|
||||||
|
else {
|
||||||
|
// 1. Let concat be the concatenation of message, U+003A (:), U+0020 SPACE, and first.
|
||||||
|
auto concat = js_string(vm(), String::formatted("{}: {}", message->string(), first.to_string(global_object()).value()));
|
||||||
|
// 2. Set data[0] to concat.
|
||||||
|
data[0] = concat;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. Perform Logger("assert", data).
|
||||||
if (m_client)
|
if (m_client)
|
||||||
return m_client->assert_();
|
TRY(m_client->logger(LogLevel::Assert, data));
|
||||||
return js_undefined();
|
return js_undefined();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -65,7 +65,7 @@ public:
|
||||||
Value trace();
|
Value trace();
|
||||||
ThrowCompletionOr<Value> count();
|
ThrowCompletionOr<Value> count();
|
||||||
ThrowCompletionOr<Value> count_reset();
|
ThrowCompletionOr<Value> count_reset();
|
||||||
Value assert_();
|
ThrowCompletionOr<Value> assert_();
|
||||||
|
|
||||||
void output_debug_message(LogLevel log_level, String output) const;
|
void output_debug_message(LogLevel log_level, String output) const;
|
||||||
|
|
||||||
|
@ -89,7 +89,6 @@ public:
|
||||||
|
|
||||||
virtual void clear() = 0;
|
virtual void clear() = 0;
|
||||||
virtual Value trace() = 0;
|
virtual Value trace() = 0;
|
||||||
virtual Value assert_() = 0;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual ~ConsoleClient() = default;
|
virtual ~ConsoleClient() = default;
|
||||||
|
|
|
@ -134,27 +134,6 @@ JS::Value WebContentConsoleClient::trace()
|
||||||
return JS::js_undefined();
|
return JS::js_undefined();
|
||||||
}
|
}
|
||||||
|
|
||||||
JS::Value WebContentConsoleClient::assert_()
|
|
||||||
{
|
|
||||||
auto& vm = this->vm();
|
|
||||||
if (!vm.argument(0).to_boolean()) {
|
|
||||||
StringBuilder html;
|
|
||||||
if (vm.argument_count() > 1) {
|
|
||||||
html.append("<span class=\"error\">");
|
|
||||||
html.append("Assertion failed:");
|
|
||||||
html.append("</span>");
|
|
||||||
html.append(" ");
|
|
||||||
html.append(escape_html_entities(vm.join_arguments(1)));
|
|
||||||
} else {
|
|
||||||
html.append("<span class=\"error\">");
|
|
||||||
html.append("Assertion failed");
|
|
||||||
html.append("</span>");
|
|
||||||
}
|
|
||||||
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, Vector<JS::Value>& arguments)
|
||||||
{
|
{
|
||||||
|
@ -189,5 +168,4 @@ JS::ThrowCompletionOr<JS::Value> WebContentConsoleClient::printer(JS::Console::L
|
||||||
print_html(html.string_view());
|
print_html(html.string_view());
|
||||||
return JS::js_undefined();
|
return JS::js_undefined();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,6 @@ public:
|
||||||
private:
|
private:
|
||||||
virtual void clear() override;
|
virtual void clear() override;
|
||||||
virtual JS::Value trace() override;
|
virtual JS::Value trace() override;
|
||||||
virtual JS::Value assert_() 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, Vector<JS::Value>&) override;
|
||||||
|
|
||||||
ClientConnection& m_client;
|
ClientConnection& m_client;
|
||||||
|
|
|
@ -1140,20 +1140,6 @@ public:
|
||||||
return JS::js_undefined();
|
return JS::js_undefined();
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual JS::Value assert_() override
|
|
||||||
{
|
|
||||||
auto& vm = this->vm();
|
|
||||||
if (!vm.argument(0).to_boolean()) {
|
|
||||||
if (vm.argument_count() > 1) {
|
|
||||||
js_out("\033[31;1mAssertion failed:\033[0m");
|
|
||||||
js_outln(" {}", vm.join_arguments(1));
|
|
||||||
} else {
|
|
||||||
js_outln("\033[31;1mAssertion failed\033[0m");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return JS::js_undefined();
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual JS::ThrowCompletionOr<JS::Value> printer(JS::Console::LogLevel log_level, Vector<JS::Value>& arguments) override
|
virtual JS::ThrowCompletionOr<JS::Value> printer(JS::Console::LogLevel log_level, Vector<JS::Value>& arguments) override
|
||||||
{
|
{
|
||||||
auto output = String::join(" ", arguments);
|
auto output = String::join(" ", arguments);
|
||||||
|
@ -1164,6 +1150,7 @@ public:
|
||||||
js_outln("\033[36;1m{}\033[0m", output);
|
js_outln("\033[36;1m{}\033[0m", output);
|
||||||
break;
|
break;
|
||||||
case JS::Console::LogLevel::Error:
|
case JS::Console::LogLevel::Error:
|
||||||
|
case JS::Console::LogLevel::Assert:
|
||||||
js_outln("\033[31;1m{}\033[0m", output);
|
js_outln("\033[31;1m{}\033[0m", output);
|
||||||
break;
|
break;
|
||||||
case JS::Console::LogLevel::Info:
|
case JS::Console::LogLevel::Info:
|
||||||
|
|
Loading…
Reference in a new issue