diff --git a/Userland/Libraries/LibJS/Console.cpp b/Userland/Libraries/LibJS/Console.cpp index 6dea85170e..69a4fbde6c 100644 --- a/Userland/Libraries/LibJS/Console.cpp +++ b/Userland/Libraries/LibJS/Console.cpp @@ -22,6 +22,67 @@ Console::Console(Realm& realm) { } +// 1.1.1. assert(condition, ...data), https://console.spec.whatwg.org/#assert +ThrowCompletionOr Console::assert_() +{ + auto& vm = realm().vm(); + + // 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 = MUST_OR_THROW_OOM(PrimitiveString::create(vm, "Assertion failed"sv)); + + // NOTE: Assemble `data` from the function arguments. + MarkedVector data { vm.heap() }; + 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 = TRY_OR_THROW_OOM(vm, String::formatted("{}: {}", TRY(message->utf8_string()), MUST(first.to_string(vm)))); + // 2. Set data[0] to concat. + data[0] = PrimitiveString::create(vm, move(concat)); + } + } + + // 5. Perform Logger("assert", data). + if (m_client) + TRY(m_client->logger(LogLevel::Assert, data)); + return js_undefined(); +} + +// 1.1.2. clear(), https://console.spec.whatwg.org/#clear +Value Console::clear() +{ + // 1. Empty the appropriate group stack. + m_group_stack.clear(); + + // 2. If possible for the environment, clear the console. (Otherwise, do nothing.) + if (m_client) + m_client->clear(); + return js_undefined(); +} + // 1.1.3. debug(...data), https://console.spec.whatwg.org/#debug ThrowCompletionOr Console::debug() { @@ -66,29 +127,6 @@ ThrowCompletionOr Console::log() return js_undefined(); } -// 1.1.9. warn(...data), https://console.spec.whatwg.org/#warn -ThrowCompletionOr Console::warn() -{ - // 1. Perform Logger("warn", data). - if (m_client) { - auto data = vm_arguments(); - return m_client->logger(LogLevel::Warn, data); - } - return js_undefined(); -} - -// 1.1.2. clear(), https://console.spec.whatwg.org/#clear -Value Console::clear() -{ - // 1. Empty the appropriate group stack. - m_group_stack.clear(); - - // 2. If possible for the environment, clear the console. (Otherwise, do nothing.) - if (m_client) - m_client->clear(); - return js_undefined(); -} - // 1.1.8. trace(...data), https://console.spec.whatwg.org/#trace ThrowCompletionOr Console::trace() { @@ -119,6 +157,17 @@ ThrowCompletionOr Console::trace() return m_client->printer(Console::LogLevel::Trace, trace); } +// 1.1.9. warn(...data), https://console.spec.whatwg.org/#warn +ThrowCompletionOr Console::warn() +{ + // 1. Perform Logger("warn", data). + if (m_client) { + auto data = vm_arguments(); + return m_client->logger(LogLevel::Warn, data); + } + return js_undefined(); +} + static ThrowCompletionOr label_or_fallback(VM& vm, StringView fallback) { return vm.argument_count() > 0 @@ -187,55 +236,6 @@ ThrowCompletionOr Console::count_reset() return js_undefined(); } -// 1.1.1. assert(condition, ...data), https://console.spec.whatwg.org/#assert -ThrowCompletionOr Console::assert_() -{ - auto& vm = realm().vm(); - - // 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 = MUST_OR_THROW_OOM(PrimitiveString::create(vm, "Assertion failed"sv)); - - // NOTE: Assemble `data` from the function arguments. - MarkedVector data { vm.heap() }; - 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 = TRY_OR_THROW_OOM(vm, String::formatted("{}: {}", TRY(message->utf8_string()), MUST(first.to_string(vm)))); - // 2. Set data[0] to concat. - data[0] = PrimitiveString::create(vm, move(concat)); - } - } - - // 5. Perform Logger("assert", data). - if (m_client) - TRY(m_client->logger(LogLevel::Assert, data)); - return js_undefined(); -} - // 1.3.1. group(...data), https://console.spec.whatwg.org/#group ThrowCompletionOr Console::group() { diff --git a/Userland/Libraries/LibJS/Console.h b/Userland/Libraries/LibJS/Console.h index 1ac8469061..e0d4a14523 100644 --- a/Userland/Libraries/LibJS/Console.h +++ b/Userland/Libraries/LibJS/Console.h @@ -65,16 +65,16 @@ public: HashMap& counters() { return m_counters; } HashMap const& counters() const { return m_counters; } + ThrowCompletionOr assert_(); + Value clear(); ThrowCompletionOr debug(); ThrowCompletionOr error(); ThrowCompletionOr info(); ThrowCompletionOr log(); - ThrowCompletionOr warn(); - Value clear(); ThrowCompletionOr trace(); + ThrowCompletionOr warn(); ThrowCompletionOr count(); ThrowCompletionOr count_reset(); - ThrowCompletionOr assert_(); ThrowCompletionOr group(); ThrowCompletionOr group_collapsed(); ThrowCompletionOr group_end(); diff --git a/Userland/Libraries/LibJS/Runtime/ConsoleObject.cpp b/Userland/Libraries/LibJS/Runtime/ConsoleObject.cpp index ac7a0f2b94..cee80300ac 100644 --- a/Userland/Libraries/LibJS/Runtime/ConsoleObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/ConsoleObject.cpp @@ -23,16 +23,16 @@ ThrowCompletionOr ConsoleObject::initialize(Realm& realm) auto& vm = this->vm(); MUST_OR_THROW_OOM(Base::initialize(realm)); u8 attr = Attribute::Writable | Attribute::Enumerable | Attribute::Configurable; - define_native_function(realm, vm.names.log, log, 0, attr); + define_native_function(realm, vm.names.assert, assert_, 0, attr); + define_native_function(realm, vm.names.clear, clear, 0, attr); define_native_function(realm, vm.names.debug, debug, 0, attr); - define_native_function(realm, vm.names.info, info, 0, attr); - define_native_function(realm, vm.names.warn, warn, 0, attr); define_native_function(realm, vm.names.error, error, 0, attr); + define_native_function(realm, vm.names.info, info, 0, attr); + define_native_function(realm, vm.names.log, log, 0, attr); define_native_function(realm, vm.names.trace, trace, 0, attr); + define_native_function(realm, vm.names.warn, warn, 0, attr); define_native_function(realm, vm.names.count, count, 0, attr); define_native_function(realm, vm.names.countReset, count_reset, 0, attr); - define_native_function(realm, vm.names.clear, clear, 0, attr); - define_native_function(realm, vm.names.assert, assert_, 0, attr); define_native_function(realm, vm.names.group, group, 0, attr); define_native_function(realm, vm.names.groupCollapsed, group_collapsed, 0, attr); define_native_function(realm, vm.names.groupEnd, group_end, 0, attr); @@ -43,11 +43,18 @@ ThrowCompletionOr ConsoleObject::initialize(Realm& realm) return {}; } -// 1.1.6. log(...data), https://console.spec.whatwg.org/#log -JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::log) +// 1.1.1. assert(condition, ...data), https://console.spec.whatwg.org/#assert +JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::assert_) { auto& console_object = *vm.current_realm()->intrinsics().console_object(); - return console_object.console().log(); + return console_object.console().assert_(); +} + +// 1.1.2. clear(), https://console.spec.whatwg.org/#clear +JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::clear) +{ + auto& console_object = *vm.current_realm()->intrinsics().console_object(); + return console_object.console().clear(); } // 1.1.3. debug(...data), https://console.spec.whatwg.org/#debug @@ -57,20 +64,6 @@ JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::debug) return console_object.console().debug(); } -// 1.1.5. info(...data), https://console.spec.whatwg.org/#info -JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::info) -{ - auto& console_object = *vm.current_realm()->intrinsics().console_object(); - return console_object.console().info(); -} - -// 1.1.9. warn(...data), https://console.spec.whatwg.org/#warn -JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::warn) -{ - auto& console_object = *vm.current_realm()->intrinsics().console_object(); - return console_object.console().warn(); -} - // 1.1.4. error(...data), https://console.spec.whatwg.org/#error JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::error) { @@ -78,6 +71,20 @@ JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::error) return console_object.console().error(); } +// 1.1.5. info(...data), https://console.spec.whatwg.org/#info +JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::info) +{ + auto& console_object = *vm.current_realm()->intrinsics().console_object(); + return console_object.console().info(); +} + +// 1.1.6. log(...data), https://console.spec.whatwg.org/#log +JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::log) +{ + auto& console_object = *vm.current_realm()->intrinsics().console_object(); + return console_object.console().log(); +} + // 1.1.8. trace(...data), https://console.spec.whatwg.org/#trace JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::trace) { @@ -85,6 +92,13 @@ JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::trace) return console_object.console().trace(); } +// 1.1.9. warn(...data), https://console.spec.whatwg.org/#warn +JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::warn) +{ + auto& console_object = *vm.current_realm()->intrinsics().console_object(); + return console_object.console().warn(); +} + // 1.2.1. count(label), https://console.spec.whatwg.org/#count JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::count) { @@ -99,20 +113,6 @@ JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::count_reset) return console_object.console().count_reset(); } -// 1.1.2. clear(), https://console.spec.whatwg.org/#clear -JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::clear) -{ - auto& console_object = *vm.current_realm()->intrinsics().console_object(); - return console_object.console().clear(); -} - -// 1.1.1. assert(condition, ...data), https://console.spec.whatwg.org/#assert -JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::assert_) -{ - auto& console_object = *vm.current_realm()->intrinsics().console_object(); - return console_object.console().assert_(); -} - // 1.3.1. group(...data), https://console.spec.whatwg.org/#group JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::group) { diff --git a/Userland/Libraries/LibJS/Runtime/ConsoleObject.h b/Userland/Libraries/LibJS/Runtime/ConsoleObject.h index 6e0833390b..0e0cd7b2de 100644 --- a/Userland/Libraries/LibJS/Runtime/ConsoleObject.h +++ b/Userland/Libraries/LibJS/Runtime/ConsoleObject.h @@ -22,16 +22,16 @@ public: private: explicit ConsoleObject(Realm&); - JS_DECLARE_NATIVE_FUNCTION(log); + JS_DECLARE_NATIVE_FUNCTION(assert_); + JS_DECLARE_NATIVE_FUNCTION(clear); JS_DECLARE_NATIVE_FUNCTION(debug); - JS_DECLARE_NATIVE_FUNCTION(info); - JS_DECLARE_NATIVE_FUNCTION(warn); JS_DECLARE_NATIVE_FUNCTION(error); + JS_DECLARE_NATIVE_FUNCTION(info); + JS_DECLARE_NATIVE_FUNCTION(log); JS_DECLARE_NATIVE_FUNCTION(trace); + JS_DECLARE_NATIVE_FUNCTION(warn); JS_DECLARE_NATIVE_FUNCTION(count); JS_DECLARE_NATIVE_FUNCTION(count_reset); - JS_DECLARE_NATIVE_FUNCTION(clear); - JS_DECLARE_NATIVE_FUNCTION(assert_); JS_DECLARE_NATIVE_FUNCTION(group); JS_DECLARE_NATIVE_FUNCTION(group_collapsed); JS_DECLARE_NATIVE_FUNCTION(group_end);