1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 16:47:44 +00:00

LibJS: Sort Console methods in spec order

Easier to compare to the spec side-by-side when everything is in the
same order.
This commit is contained in:
Timothy Flynn 2023-06-21 21:27:14 -04:00 committed by Andreas Kling
parent caa24d0805
commit 9f8e5f0b1c
4 changed files with 116 additions and 116 deletions

View file

@ -22,6 +22,67 @@ Console::Console(Realm& realm)
{
}
// 1.1.1. assert(condition, ...data), https://console.spec.whatwg.org/#assert
ThrowCompletionOr<Value> 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<Value> 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<Value> Console::debug()
{
@ -66,29 +127,6 @@ ThrowCompletionOr<Value> Console::log()
return js_undefined();
}
// 1.1.9. warn(...data), https://console.spec.whatwg.org/#warn
ThrowCompletionOr<Value> 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<Value> Console::trace()
{
@ -119,6 +157,17 @@ ThrowCompletionOr<Value> Console::trace()
return m_client->printer(Console::LogLevel::Trace, trace);
}
// 1.1.9. warn(...data), https://console.spec.whatwg.org/#warn
ThrowCompletionOr<Value> 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<String> label_or_fallback(VM& vm, StringView fallback)
{
return vm.argument_count() > 0
@ -187,55 +236,6 @@ ThrowCompletionOr<Value> Console::count_reset()
return js_undefined();
}
// 1.1.1. assert(condition, ...data), https://console.spec.whatwg.org/#assert
ThrowCompletionOr<Value> 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<Value> 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<Value> Console::group()
{

View file

@ -65,16 +65,16 @@ public:
HashMap<String, unsigned>& counters() { return m_counters; }
HashMap<String, unsigned> const& counters() const { return m_counters; }
ThrowCompletionOr<Value> assert_();
Value clear();
ThrowCompletionOr<Value> debug();
ThrowCompletionOr<Value> error();
ThrowCompletionOr<Value> info();
ThrowCompletionOr<Value> log();
ThrowCompletionOr<Value> warn();
Value clear();
ThrowCompletionOr<Value> trace();
ThrowCompletionOr<Value> warn();
ThrowCompletionOr<Value> count();
ThrowCompletionOr<Value> count_reset();
ThrowCompletionOr<Value> assert_();
ThrowCompletionOr<Value> group();
ThrowCompletionOr<Value> group_collapsed();
ThrowCompletionOr<Value> group_end();

View file

@ -23,16 +23,16 @@ ThrowCompletionOr<void> 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<void> 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)
{

View file

@ -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);