From 396655d145c469a20d457249e8f56e503140b132 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Wed, 21 Jun 2023 21:29:24 -0400 Subject: [PATCH] LibJS: Begin implementing console.dir The intent of the spec is that the output of console.dir is interactable within the console. Our Printer implementation currently just prints the provided object as a string, and doesn't check the provided `options` argument. But having console.dir defined prevents exceptions from being thrown on real websites. --- Userland/Libraries/LibJS/Console.cpp | 20 +++++++++++++++++++ Userland/Libraries/LibJS/Console.h | 1 + .../LibJS/Runtime/CommonPropertyNames.h | 1 + .../Libraries/LibJS/Runtime/ConsoleObject.cpp | 8 ++++++++ .../Libraries/LibJS/Runtime/ConsoleObject.h | 1 + 5 files changed, 31 insertions(+) diff --git a/Userland/Libraries/LibJS/Console.cpp b/Userland/Libraries/LibJS/Console.cpp index 69a4fbde6c..8e6769209e 100644 --- a/Userland/Libraries/LibJS/Console.cpp +++ b/Userland/Libraries/LibJS/Console.cpp @@ -168,6 +168,26 @@ ThrowCompletionOr Console::warn() return js_undefined(); } +// 1.1.10. dir(item, options), https://console.spec.whatwg.org/#dir +ThrowCompletionOr Console::dir() +{ + auto& vm = realm().vm(); + + // 1. Let object be item with generic JavaScript object formatting applied. + // NOTE: Generic formatting is performed by ConsoleClient::printer(). + auto object = vm.argument(0); + + // 2. Perform Printer("dir", « object », options). + if (m_client) { + MarkedVector printer_arguments { vm.heap() }; + TRY_OR_THROW_OOM(vm, printer_arguments.try_append(object)); + + return m_client->printer(LogLevel::Dir, move(printer_arguments)); + } + + return js_undefined(); +} + static ThrowCompletionOr label_or_fallback(VM& vm, StringView fallback) { return vm.argument_count() > 0 diff --git a/Userland/Libraries/LibJS/Console.h b/Userland/Libraries/LibJS/Console.h index e0d4a14523..7b291dd1c0 100644 --- a/Userland/Libraries/LibJS/Console.h +++ b/Userland/Libraries/LibJS/Console.h @@ -73,6 +73,7 @@ public: ThrowCompletionOr log(); ThrowCompletionOr trace(); ThrowCompletionOr warn(); + ThrowCompletionOr dir(); ThrowCompletionOr count(); ThrowCompletionOr count_reset(); ThrowCompletionOr group(); diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index 90c2449a98..08765af063 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -151,6 +151,7 @@ namespace JS { P(deref) \ P(description) \ P(difference) \ + P(dir) \ P(direction) \ P(disambiguation) \ P(disposed) \ diff --git a/Userland/Libraries/LibJS/Runtime/ConsoleObject.cpp b/Userland/Libraries/LibJS/Runtime/ConsoleObject.cpp index cee80300ac..1a1bead0f0 100644 --- a/Userland/Libraries/LibJS/Runtime/ConsoleObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/ConsoleObject.cpp @@ -31,6 +31,7 @@ ThrowCompletionOr ConsoleObject::initialize(Realm& realm) 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.dir, dir, 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.group, group, 0, attr); @@ -99,6 +100,13 @@ JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::warn) return console_object.console().warn(); } +// 1.1.10. dir(item, options), https://console.spec.whatwg.org/#warn +JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::dir) +{ + auto& console_object = *vm.current_realm()->intrinsics().console_object(); + return console_object.console().dir(); +} + // 1.2.1. count(label), https://console.spec.whatwg.org/#count JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::count) { diff --git a/Userland/Libraries/LibJS/Runtime/ConsoleObject.h b/Userland/Libraries/LibJS/Runtime/ConsoleObject.h index 0e0cd7b2de..79e0e2928b 100644 --- a/Userland/Libraries/LibJS/Runtime/ConsoleObject.h +++ b/Userland/Libraries/LibJS/Runtime/ConsoleObject.h @@ -30,6 +30,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(log); JS_DECLARE_NATIVE_FUNCTION(trace); JS_DECLARE_NATIVE_FUNCTION(warn); + JS_DECLARE_NATIVE_FUNCTION(dir); JS_DECLARE_NATIVE_FUNCTION(count); JS_DECLARE_NATIVE_FUNCTION(count_reset); JS_DECLARE_NATIVE_FUNCTION(group);