summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Atkins <atkinssj@serenityos.org>2022-10-03 14:41:41 +0100
committerLinus Groh <mail@linusgroh.de>2022-11-19 16:56:31 +0000
commitc793beb0cc1e967aa7fb1b3ab388d61a012f5001 (patch)
treeccf0f04b1ed1c7729484b8594abba4b9f9c7a817
parent3ec13fdb860fceb17d8e0ac8ecbe3d9cb734ddf4 (diff)
downloadserenity-c793beb0cc1e967aa7fb1b3ab388d61a012f5001.zip
WebContent: Add `$_` variable to browser console
This holds the return value of the expression that was last entered into the browser console. If that last expression returned an error of some kind, `$_` will be `undefined`. This matches the behaviour in Firefox.
-rw-r--r--Userland/Services/WebContent/ConsoleGlobalObject.cpp7
-rw-r--r--Userland/Services/WebContent/ConsoleGlobalObject.h5
-rw-r--r--Userland/Services/WebContent/WebContentConsoleClient.cpp4
3 files changed, 15 insertions, 1 deletions
diff --git a/Userland/Services/WebContent/ConsoleGlobalObject.cpp b/Userland/Services/WebContent/ConsoleGlobalObject.cpp
index 9b76e42d44..45e9dd8e5d 100644
--- a/Userland/Services/WebContent/ConsoleGlobalObject.cpp
+++ b/Userland/Services/WebContent/ConsoleGlobalObject.cpp
@@ -22,6 +22,7 @@ void ConsoleGlobalObject::initialize(JS::Realm& realm)
Base::initialize(realm);
define_native_accessor(realm, "$0", $0_getter, nullptr, 0);
+ define_native_accessor(realm, "$_", $__getter, nullptr, 0);
}
void ConsoleGlobalObject::visit_edges(Visitor& visitor)
@@ -110,4 +111,10 @@ JS_DEFINE_NATIVE_FUNCTION(ConsoleGlobalObject::$0_getter)
return inspected_node;
}
+JS_DEFINE_NATIVE_FUNCTION(ConsoleGlobalObject::$__getter)
+{
+ auto* console_global_object = TRY(get_console(vm));
+ return console_global_object->m_most_recent_result;
+}
+
}
diff --git a/Userland/Services/WebContent/ConsoleGlobalObject.h b/Userland/Services/WebContent/ConsoleGlobalObject.h
index 1cb4758127..e4abdfc287 100644
--- a/Userland/Services/WebContent/ConsoleGlobalObject.h
+++ b/Userland/Services/WebContent/ConsoleGlobalObject.h
@@ -33,13 +33,18 @@ public:
virtual JS::ThrowCompletionOr<bool> internal_delete(JS::PropertyKey const& name) override;
virtual JS::ThrowCompletionOr<JS::MarkedVector<JS::Value>> internal_own_property_keys() const override;
+ void set_most_recent_result(JS::Value result) { m_most_recent_result = move(result); }
+
private:
virtual void visit_edges(Visitor&) override;
// $0, the DOM node currently selected in the inspector
JS_DECLARE_NATIVE_FUNCTION($0_getter);
+ // $_, the value of the most recent expression entered into the console
+ JS_DECLARE_NATIVE_FUNCTION($__getter);
Web::HTML::Window* m_window_object;
+ JS::Value m_most_recent_result { JS::js_undefined() };
};
}
diff --git a/Userland/Services/WebContent/WebContentConsoleClient.cpp b/Userland/Services/WebContent/WebContentConsoleClient.cpp
index e7767f3060..f72da8df8d 100644
--- a/Userland/Services/WebContent/WebContentConsoleClient.cpp
+++ b/Userland/Services/WebContent/WebContentConsoleClient.cpp
@@ -77,8 +77,10 @@ void WebContentConsoleClient::handle_input(String const& js_source)
auto result = script->run();
- if (result.value().has_value())
+ if (result.value().has_value()) {
+ m_console_global_object->set_most_recent_result(result.value().value());
print_html(JS::MarkupGenerator::html_from_value(*result.value()));
+ }
}
void WebContentConsoleClient::report_exception(JS::Error const& exception, bool in_promise)