mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 22:38:13 +00:00
LibJS+WebContent+js: Bring console.count[Reset]() to spec
The `CountReset` log level is displayed as a warning, since the message is always to warn that the counter doesn't exist. This is also in line with the table at https://console.spec.whatwg.org/#loglevel-severity
This commit is contained in:
parent
260836135a
commit
834ced82d4
5 changed files with 50 additions and 73 deletions
|
@ -90,17 +90,58 @@ Value Console::trace()
|
|||
return js_undefined();
|
||||
}
|
||||
|
||||
Value Console::count()
|
||||
// 1.2.1. count(label), https://console.spec.whatwg.org/#count
|
||||
ThrowCompletionOr<Value> Console::count()
|
||||
{
|
||||
// NOTE: "default" is the default value in the IDL. https://console.spec.whatwg.org/#ref-for-count
|
||||
auto label = vm().argument_count() ? TRY(vm().argument(0).to_string(global_object())) : "default";
|
||||
|
||||
// 1. Let map be the associated count map.
|
||||
auto& map = m_counters;
|
||||
|
||||
// 2. If map[label] exists, set map[label] to map[label] + 1.
|
||||
if (auto found = map.find(label); found != map.end()) {
|
||||
map.set(label, found->value + 1);
|
||||
}
|
||||
// 3. Otherwise, set map[label] to 1.
|
||||
else {
|
||||
map.set(label, 1);
|
||||
}
|
||||
|
||||
// 4. Let concat be the concatenation of label, U+003A (:), U+0020 SPACE, and ToString(map[label]).
|
||||
String concat = String::formatted("{}: {}", label, map.get(label).value());
|
||||
|
||||
// 5. Perform Logger("count", « concat »).
|
||||
Vector<Value> concat_as_vector { js_string(vm(), concat) };
|
||||
if (m_client)
|
||||
return m_client->count();
|
||||
TRY(m_client->logger(LogLevel::Count, concat_as_vector));
|
||||
return js_undefined();
|
||||
}
|
||||
|
||||
Value Console::count_reset()
|
||||
// 1.2.2. countReset(label), https://console.spec.whatwg.org/#countreset
|
||||
ThrowCompletionOr<Value> Console::count_reset()
|
||||
{
|
||||
if (m_client)
|
||||
return m_client->count_reset();
|
||||
// NOTE: "default" is the default value in the IDL. https://console.spec.whatwg.org/#ref-for-countreset
|
||||
auto label = vm().argument_count() ? TRY(vm().argument(0).to_string(global_object())) : "default";
|
||||
|
||||
// 1. Let map be the associated count map.
|
||||
auto& map = m_counters;
|
||||
|
||||
// 2. If map[label] exists, set map[label] to 0.
|
||||
if (auto found = map.find(label); found != map.end()) {
|
||||
map.set(label, 0);
|
||||
}
|
||||
// 3. Otherwise:
|
||||
else {
|
||||
// 1. Let message be a string without any formatting specifiers indicating generically
|
||||
// that the given label does not have an associated count.
|
||||
auto message = String::formatted("\"{}\" doesn't have a count", label);
|
||||
// 2. Perform Logger("countReset", « message »);
|
||||
Vector<Value> message_as_vector { js_string(vm(), message) };
|
||||
if (m_client)
|
||||
TRY(m_client->logger(LogLevel::CountReset, message_as_vector));
|
||||
}
|
||||
|
||||
return js_undefined();
|
||||
}
|
||||
|
||||
|
@ -111,28 +152,6 @@ Value Console::assert_()
|
|||
return js_undefined();
|
||||
}
|
||||
|
||||
unsigned Console::counter_increment(String label)
|
||||
{
|
||||
auto value = m_counters.get(label);
|
||||
if (!value.has_value()) {
|
||||
m_counters.set(label, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
auto new_value = value.value() + 1;
|
||||
m_counters.set(label, new_value);
|
||||
return new_value;
|
||||
}
|
||||
|
||||
bool Console::counter_reset(String label)
|
||||
{
|
||||
if (!m_counters.contains(label))
|
||||
return false;
|
||||
|
||||
m_counters.remove(label);
|
||||
return true;
|
||||
}
|
||||
|
||||
Vector<Value> Console::vm_arguments()
|
||||
{
|
||||
Vector<Value> arguments;
|
||||
|
|
|
@ -63,13 +63,10 @@ public:
|
|||
ThrowCompletionOr<Value> warn();
|
||||
Value clear();
|
||||
Value trace();
|
||||
Value count();
|
||||
Value count_reset();
|
||||
ThrowCompletionOr<Value> count();
|
||||
ThrowCompletionOr<Value> count_reset();
|
||||
Value assert_();
|
||||
|
||||
unsigned counter_increment(String label);
|
||||
bool counter_reset(String label);
|
||||
|
||||
void output_debug_message(LogLevel log_level, String output) const;
|
||||
|
||||
private:
|
||||
|
@ -92,8 +89,6 @@ public:
|
|||
|
||||
virtual Value clear() = 0;
|
||||
virtual Value trace() = 0;
|
||||
virtual Value count() = 0;
|
||||
virtual Value count_reset() = 0;
|
||||
virtual Value assert_() = 0;
|
||||
|
||||
protected:
|
||||
|
|
|
@ -135,25 +135,6 @@ JS::Value WebContentConsoleClient::trace()
|
|||
return JS::js_undefined();
|
||||
}
|
||||
|
||||
JS::Value WebContentConsoleClient::count()
|
||||
{
|
||||
auto label = vm().argument_count() ? vm().argument(0).to_string_without_side_effects() : "default";
|
||||
auto counter_value = m_console.counter_increment(label);
|
||||
print_html(String::formatted("{}: {}", label, counter_value));
|
||||
return JS::js_undefined();
|
||||
}
|
||||
|
||||
JS::Value WebContentConsoleClient::count_reset()
|
||||
{
|
||||
auto label = vm().argument_count() ? vm().argument(0).to_string_without_side_effects() : "default";
|
||||
if (m_console.counter_reset(label)) {
|
||||
print_html(String::formatted("{}: 0", label));
|
||||
} else {
|
||||
print_html(String::formatted("\"{}\" doesn't have a count", label));
|
||||
}
|
||||
return JS::js_undefined();
|
||||
}
|
||||
|
||||
JS::Value WebContentConsoleClient::assert_()
|
||||
{
|
||||
auto& vm = this->vm();
|
||||
|
@ -196,6 +177,7 @@ JS::ThrowCompletionOr<JS::Value> WebContentConsoleClient::printer(JS::Console::L
|
|||
html.append("<span class=\"log\"> ");
|
||||
break;
|
||||
case JS::Console::LogLevel::Warn:
|
||||
case JS::Console::LogLevel::CountReset:
|
||||
html.append("<span class=\"warn\">(w) ");
|
||||
break;
|
||||
default:
|
||||
|
|
|
@ -26,8 +26,6 @@ public:
|
|||
private:
|
||||
virtual JS::Value clear() override;
|
||||
virtual JS::Value trace() override;
|
||||
virtual JS::Value count() override;
|
||||
virtual JS::Value count_reset() override;
|
||||
virtual JS::Value assert_() override;
|
||||
virtual JS::ThrowCompletionOr<JS::Value> printer(JS::Console::LogLevel log_level, Vector<JS::Value>&) override;
|
||||
|
||||
|
|
|
@ -1141,24 +1141,6 @@ public:
|
|||
return JS::js_undefined();
|
||||
}
|
||||
|
||||
virtual JS::Value count() override
|
||||
{
|
||||
auto label = vm().argument_count() ? vm().argument(0).to_string_without_side_effects() : "default";
|
||||
auto counter_value = m_console.counter_increment(label);
|
||||
js_outln("{}: {}", label, counter_value);
|
||||
return JS::js_undefined();
|
||||
}
|
||||
|
||||
virtual JS::Value count_reset() override
|
||||
{
|
||||
auto label = vm().argument_count() ? vm().argument(0).to_string_without_side_effects() : "default";
|
||||
if (m_console.counter_reset(label))
|
||||
js_outln("{}: 0", label);
|
||||
else
|
||||
js_outln("\033[33;1m\"{}\" doesn't have a count\033[0m", label);
|
||||
return JS::js_undefined();
|
||||
}
|
||||
|
||||
virtual JS::Value assert_() override
|
||||
{
|
||||
auto& vm = this->vm();
|
||||
|
@ -1192,6 +1174,7 @@ public:
|
|||
js_outln("{}", output);
|
||||
break;
|
||||
case JS::Console::LogLevel::Warn:
|
||||
case JS::Console::LogLevel::CountReset:
|
||||
js_outln("\033[33;1m{}\033[0m", output);
|
||||
break;
|
||||
default:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue