summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Atkins <atkinssj@serenityos.org>2021-12-08 19:12:06 +0000
committerAndreas Kling <kling@serenityos.org>2021-12-27 21:44:07 +0100
commit834ced82d41e8fba84eeb85374ddfb393de8b721 (patch)
treecb8c6b54ac5655be65ee8c36e9d17aabdde6f08b
parent260836135a17596e5b481edcf81a79824b44ca51 (diff)
downloadserenity-834ced82d41e8fba84eeb85374ddfb393de8b721.zip
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
-rw-r--r--Userland/Libraries/LibJS/Console.cpp73
-rw-r--r--Userland/Libraries/LibJS/Console.h9
-rw-r--r--Userland/Services/WebContent/WebContentConsoleClient.cpp20
-rw-r--r--Userland/Services/WebContent/WebContentConsoleClient.h2
-rw-r--r--Userland/Utilities/js.cpp19
5 files changed, 50 insertions, 73 deletions
diff --git a/Userland/Libraries/LibJS/Console.cpp b/Userland/Libraries/LibJS/Console.cpp
index 3866897c27..c591baab15 100644
--- a/Userland/Libraries/LibJS/Console.cpp
+++ b/Userland/Libraries/LibJS/Console.cpp
@@ -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;
diff --git a/Userland/Libraries/LibJS/Console.h b/Userland/Libraries/LibJS/Console.h
index 9461301a98..c63fe4c96b 100644
--- a/Userland/Libraries/LibJS/Console.h
+++ b/Userland/Libraries/LibJS/Console.h
@@ -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:
diff --git a/Userland/Services/WebContent/WebContentConsoleClient.cpp b/Userland/Services/WebContent/WebContentConsoleClient.cpp
index 4966248508..179eebee55 100644
--- a/Userland/Services/WebContent/WebContentConsoleClient.cpp
+++ b/Userland/Services/WebContent/WebContentConsoleClient.cpp
@@ -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:
diff --git a/Userland/Services/WebContent/WebContentConsoleClient.h b/Userland/Services/WebContent/WebContentConsoleClient.h
index 0373344a64..18f4275e72 100644
--- a/Userland/Services/WebContent/WebContentConsoleClient.h
+++ b/Userland/Services/WebContent/WebContentConsoleClient.h
@@ -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;
diff --git a/Userland/Utilities/js.cpp b/Userland/Utilities/js.cpp
index 335ea81fe6..69c5405d9a 100644
--- a/Userland/Utilities/js.cpp
+++ b/Userland/Utilities/js.cpp
@@ -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: