From 7b12fd26f3d0ec2fe655cd537fda21f760e92d11 Mon Sep 17 00:00:00 2001 From: Kevin Ballard Date: Sun, 13 Jul 2014 13:21:06 -0700 Subject: [PATCH] Change how we separate toplevel and global scopes Instead of introducing a new local scope at the point of `set`, merely push a new local scope at the end of env_init(). This means we have a single toplevel local scope across the lifetime of the fish process, which means that set -l foo bar echo $foo behaves as expected, without modifying the global environment. --- builtin_set.cpp | 11 ----------- env.cpp | 18 ++++++++---------- env.h | 6 ------ 3 files changed, 8 insertions(+), 27 deletions(-) diff --git a/builtin_set.cpp b/builtin_set.cpp index 043bd522e..af257d160 100644 --- a/builtin_set.cpp +++ b/builtin_set.cpp @@ -566,17 +566,6 @@ static int builtin_set(parser_t &parser, wchar_t **argv) */ scope = (local ? ENV_LOCAL : 0) | (global ? ENV_GLOBAL : 0) | (exportv ? ENV_EXPORT : 0) | (unexport ? ENV_UNEXPORT : 0) | (universal ? ENV_UNIVERSAL:0) | ENV_USER; - /* - If we're interacting with the local scope at all, ensure we actually have - one that's distinct from the global scope. If we don't have one yet, - create one and modify the current block to pop it. - */ - if ((scope & ENV_LOCAL) && env_ensure_local_scope()) - { - block_t *block = parser.current_block(); - block->wants_pop_env = true; - } - if (query) { /* diff --git a/env.cpp b/env.cpp index 278e2af4e..88f24b911 100644 --- a/env.cpp +++ b/env.cpp @@ -593,6 +593,14 @@ void env_init(const struct config_paths_t *paths /* or NULL */) /* Set fish_bind_mode to "default" */ env_set(FISH_BIND_MODE_VAR, DEFAULT_BIND_MODE, ENV_GLOBAL); + + /* + Now that the global scope is fully initialized, add a toplevel local + scope. This same local scope will persist throughout the lifetime of the + fish process, and it will ensure that `set -l` commands run at the + command-line don't affect the global scope. + */ + env_push(false); } /** @@ -1195,16 +1203,6 @@ void env_pop() } } -bool env_ensure_local_scope() -{ - if (top == global_env) - { - env_push(false); - return true; - } - return false; -} - /** Function used with to insert keys of one table into a set::set */ diff --git a/env.h b/env.h index 605fc453a..3fa64e914 100644 --- a/env.h +++ b/env.h @@ -206,12 +206,6 @@ void env_push(bool new_scope); */ void env_pop(); -/** - Push the variable stack if the current scope is the global scope. Returns - whether it pushed a scope. -*/ -bool env_ensure_local_scope(); - /** Synchronizes all universal variable changes: writes everything out, reads stuff in */ void env_universal_barrier();