diff --git a/src/env.cpp b/src/env.cpp index bc36d4921..e58cbd79e 100644 --- a/src/env.cpp +++ b/src/env.cpp @@ -1283,8 +1283,9 @@ env_stack_t &env_stack_t::globals() { return s_globals; } -env_stack_t &env_stack_t::principal() { - static env_stack_t s_principal(env_stack_impl_t::create()); +const std::shared_ptr &env_stack_t::principal_ref() { + static const std::shared_ptr s_principal{ + new env_stack_t(env_stack_impl_t::create())}; return s_principal; } diff --git a/src/env.h b/src/env.h index e51cee02b..6577badc5 100644 --- a/src/env.h +++ b/src/env.h @@ -291,7 +291,8 @@ class env_stack_t final : public environment_t { void mark_changed_exported(); // Compatibility hack; access the "environment stack" from back when there was just one. - static env_stack_t &principal(); + static const std::shared_ptr &principal_ref(); + static env_stack_t &principal() { return *principal_ref(); } // Access a variable stack that only represents globals. // Do not push or pop from this. diff --git a/src/parser.cpp b/src/parser.cpp index 5b65f64b4..b44b5bac1 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -100,7 +100,11 @@ wcstring parser_t::user_presentable_path(const wcstring &path) const { return replace_home_directory_with_tilde(path, vars()); } -parser_t::parser_t() : variables(env_stack_t::principal()) {} +parser_t::parser_t(std::shared_ptr vars) : variables(std::move(vars)) { + assert(variables.get() && "Null variables in parser initializer"); +} + +parser_t::parser_t() : parser_t(env_stack_t::principal_ref()) {} // Out of line destructor to enable forward declaration of parse_execution_context_t parser_t::~parser_t() = default; diff --git a/src/parser.h b/src/parser.h index d4ed98631..ae27c9467 100644 --- a/src/parser.h +++ b/src/parser.h @@ -174,7 +174,7 @@ class parser_t : public std::enable_shared_from_this { /// The 'depth' of the fish call stack. int eval_level = -1; /// Set of variables for the parser. - env_stack_t &variables; + const std::shared_ptr variables; /// Miscellaneous library data. library_data_t library_data{}; @@ -204,6 +204,7 @@ class parser_t : public std::enable_shared_from_this { /// Create a parser. parser_t(); + parser_t(std::shared_ptr vars); /// The main parser. static std::shared_ptr principal; @@ -271,8 +272,8 @@ class parser_t : public std::enable_shared_from_this { const job_list_t &jobs() const { return job_list; } /// Get the variables. - env_stack_t &vars() { return variables; } - const env_stack_t &vars() const { return variables; } + env_stack_t &vars() { return *variables; } + const env_stack_t &vars() const { return *variables; } /// Get the library data. library_data_t &libdata() { return library_data; }