From 7133285c884afc4f01f1242f7be323c85670f474 Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Wed, 26 Oct 2022 12:12:04 -0500 Subject: [PATCH] Move parser status vars to their own struct Instead of using an enum + array, just use a struct and drop the getter and setter methods from `parser_t`. --- src/builtins/fg.cpp | 4 ++-- src/builtins/status.cpp | 4 ++-- src/parser.h | 23 +++++++++-------------- src/reader.cpp | 12 ++++++------ 4 files changed, 19 insertions(+), 24 deletions(-) diff --git a/src/builtins/fg.cpp b/src/builtins/fg.cpp index 3ca0fe52f..f9a51e67d 100644 --- a/src/builtins/fg.cpp +++ b/src/builtins/fg.cpp @@ -110,11 +110,11 @@ maybe_t builtin_fg(parser_t &parser, io_streams_t &streams, const wchar_t * wcstring ft = tok_command(job->command()); if (!ft.empty()) { // Provide value for `status current-command` - parser.set_status_var(parser_status_var_t::current_command, ft); + parser.libdata().status_vars.command = ft; // Also provide a value for the deprecated fish 2.0 $_ variable parser.set_var_and_fire(L"_", ENV_EXPORT, std::move(ft)); // Provide value for `status current-commandline` - parser.set_status_var(parser_status_var_t::current_commandline, job->command()); + parser.libdata().status_vars.commandline = job->command(); } reader_write_title(job->command(), parser); diff --git a/src/builtins/status.cpp b/src/builtins/status.cpp index 881406878..38f29f606 100644 --- a/src/builtins/status.cpp +++ b/src/builtins/status.cpp @@ -451,7 +451,7 @@ maybe_t builtin_status(parser_t &parser, io_streams_t &streams, const wchar } case STATUS_CURRENT_CMD: { CHECK_FOR_UNEXPECTED_STATUS_ARGS(opts.status_cmd) - const auto &var = parser.get_status_var(parser_status_var_t::current_command); + const auto &var = parser.libdata().status_vars.command; if (!var.empty()) { streams.out.append(var); streams.out.push_back(L'\n'); @@ -463,7 +463,7 @@ maybe_t builtin_status(parser_t &parser, io_streams_t &streams, const wchar } case STATUS_CURRENT_COMMANDLINE: { CHECK_FOR_UNEXPECTED_STATUS_ARGS(opts.status_cmd) - const auto &var = parser.get_status_var(parser_status_var_t::current_commandline); + const auto &var = parser.libdata().status_vars.commandline; streams.out.append(var); streams.out.push_back(L'\n'); break; diff --git a/src/parser.h b/src/parser.h index 51d235bb5..1ad0c7ecf 100644 --- a/src/parser.h +++ b/src/parser.h @@ -221,6 +221,15 @@ struct library_data_t { /// A file descriptor holding the current working directory, for use in openat(). /// This is never null and never invalid. std::shared_ptr cwd_fd{}; + + /// Status variables set by the main thread as jobs are parsed and read by various consumers. + struct { + /// Used to get the head of the current job (not the current command, at least for now) + /// for `status current-command`. + wcstring command; + /// Used to get the full text of the current job for `status current-commandline`. + wcstring commandline; + } status_vars; }; /// The result of parser_t::eval family. @@ -270,9 +279,6 @@ class parser_t : public std::enable_shared_from_this { /// top down using range-based for loops. std::deque block_list; - /// Variables set by the single-threaded parser or reader and queryable by any consumer. - std::array status_vars_; - /// The 'depth' of the fish call stack. int eval_level = -1; @@ -398,17 +404,6 @@ class parser_t : public std::enable_shared_from_this { statuses_t get_last_statuses() const { return vars().get_last_statuses(); } void set_last_statuses(statuses_t s) { vars().set_last_statuses(std::move(s)); } - /// Get a parser status variable - const wcstring& get_status_var(parser_status_var_t var) const { - return status_vars_.at((int) var); - } - - /// Set a parser status variable - void set_status_var(parser_status_var_t var, wcstring val) { - ASSERT_IS_MAIN_THREAD(); - status_vars_[(int) var] = std::move(val); - } - /// Cover of vars().set(), which also fires any returned event handlers. /// \return a value like ENV_OK. int set_var_and_fire(const wcstring &key, env_mode_flags_t mode, wcstring val); diff --git a/src/reader.cpp b/src/reader.cpp index 2238182dc..93011ed50 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -2495,7 +2495,7 @@ static void reader_interactive_init(parser_t &parser) { termsize_container_t::shared().invalidate_tty(); // Provide value for `status current-command` - parser.set_status_var(parser_status_var_t::current_command, L"fish"); + parser.libdata().status_vars.command = L"fish"; // Also provide a value for the deprecated fish 2.0 $_ variable parser.vars().set_one(L"_", ENV_GLOBAL, L"fish"); } @@ -2632,8 +2632,8 @@ static eval_res_t reader_run_command(parser_t &parser, const wcstring &cmd) { // Provide values for `status current-command` and `status current-commandline` if (!ft.empty()) { - parser.set_status_var(parser_status_var_t::current_command, ft); - parser.set_status_var(parser_status_var_t::current_commandline, cmd); + parser.libdata().status_vars.command = ft; + parser.libdata().status_vars.commandline = cmd; // Also provide a value for the deprecated fish 2.0 $_ variable parser.vars().set_one(L"_", ENV_GLOBAL, ft); } @@ -2647,7 +2647,7 @@ static eval_res_t reader_run_command(parser_t &parser, const wcstring &cmd) { auto eval_res = parser.eval(cmd, io_chain_t{}); job_reap(parser, true); - // update the execution duration iff a command is requested for execution + // Update the execution duration iff a command is requested for execution // issue - #4926 if (!ft.empty()) { timepoint_t time_after = timef(); @@ -2659,11 +2659,11 @@ static eval_res_t reader_run_command(parser_t &parser, const wcstring &cmd) { term_steal(); // Provide value for `status current-command` - parser.set_status_var(parser_status_var_t::current_command, program_name); + parser.libdata().status_vars.command = program_name; // Also provide a value for the deprecated fish 2.0 $_ variable parser.vars().set_one(L"_", ENV_GLOBAL, program_name); // Provide value for `status current-commandline` - parser.set_status_var(parser_status_var_t::current_commandline, L""); + parser.libdata().status_vars.commandline = L""; if (have_proc_stat()) { proc_update_jiffies(parser);