diff options
author | Sam Atkins <atkinssj@serenityos.org> | 2021-12-08 19:43:29 +0000 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-12-27 21:44:07 +0100 |
commit | ce694490f310b6da886ef086ba9c32c407528883 (patch) | |
tree | 2a07dd369d68d1a7d490655dce17efd2244e27e2 /Userland/Libraries | |
parent | 9b78e287b093bde65024f24415a0a36d0c4993c1 (diff) | |
download | serenity-ce694490f310b6da886ef086ba9c32c407528883.zip |
LibJS+WebContent+js: Bring console.assert() to spec
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibJS/Console.cpp | 44 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Console.h | 3 |
2 files changed, 43 insertions, 4 deletions
diff --git a/Userland/Libraries/LibJS/Console.cpp b/Userland/Libraries/LibJS/Console.cpp index c56e2c6fa8..ce8ef04db6 100644 --- a/Userland/Libraries/LibJS/Console.cpp +++ b/Userland/Libraries/LibJS/Console.cpp @@ -149,10 +149,50 @@ ThrowCompletionOr<Value> Console::count_reset() 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) - return m_client->assert_(); + TRY(m_client->logger(LogLevel::Assert, data)); return js_undefined(); } diff --git a/Userland/Libraries/LibJS/Console.h b/Userland/Libraries/LibJS/Console.h index 2930dcc9eb..e6e3591ba1 100644 --- a/Userland/Libraries/LibJS/Console.h +++ b/Userland/Libraries/LibJS/Console.h @@ -65,7 +65,7 @@ public: Value trace(); ThrowCompletionOr<Value> count(); ThrowCompletionOr<Value> count_reset(); - Value assert_(); + ThrowCompletionOr<Value> assert_(); void output_debug_message(LogLevel log_level, String output) const; @@ -89,7 +89,6 @@ public: virtual void clear() = 0; virtual Value trace() = 0; - virtual Value assert_() = 0; protected: virtual ~ConsoleClient() = default; |