about summary refs log tree commit diff
path: root/nix-repl.cc
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2013-09-09T15·06+0200
committerEelco Dolstra <eelco.dolstra@logicblox.com>2013-09-09T15·06+0200
commite91160021f992169228bc59cfa509cfb66335e8c (patch)
tree886a096f5e10c6c1b52ea8f00ac7f550fac3c959 /nix-repl.cc
parent498f8b048513bf3eee810c80f6795ab1ef32793f (diff)
On reload, wipe the environment
Diffstat (limited to 'nix-repl.cc')
-rw-r--r--nix-repl.cc26
1 files changed, 18 insertions, 8 deletions
diff --git a/nix-repl.cc b/nix-repl.cc
index c0ba3a588313..012fdfe1e6c3 100644
--- a/nix-repl.cc
+++ b/nix-repl.cc
@@ -29,6 +29,7 @@ struct NixRepl
 
     Strings loadedFiles;
 
+    const static int envSize = 32768;
     StaticEnv staticEnv;
     Env * env;
     int displ;
@@ -43,6 +44,7 @@ struct NixRepl
     bool getLine(string & line);
     bool processLine(string line);
     void loadFile(const Path & path);
+    void initEnv();
     void reloadFiles();
     void addAttrsToScope(Value & attrs);
     void addVarToScope(const Symbol & name, Value & v);
@@ -75,10 +77,6 @@ NixRepl::NixRepl()
 {
     curDir = absPath(".");
 
-    env = &state.allocEnv(32768);
-    env->up = &state.baseEnv;
-    displ = 0;
-
     store = openStore();
 }
 
@@ -90,10 +88,8 @@ void NixRepl::mainLoop(const Strings & args)
     foreach (Strings::const_iterator, i, args)
         loadedFiles.push_back(*i);
 
-    if (!loadedFiles.empty()) {
-        reloadFiles();
-        std::cout << std::endl;
-    }
+    reloadFiles();
+    if (!loadedFiles.empty()) std::cout << std::endl;
 
     using_history();
     read_history(0);
@@ -383,8 +379,20 @@ void NixRepl::loadFile(const Path & path)
 }
 
 
+void NixRepl::initEnv()
+{
+    env = &state.allocEnv(envSize);
+    env->up = &state.baseEnv;
+    displ = 0;
+    varNames.clear();
+    staticEnv.vars.clear();
+}
+
+
 void NixRepl::reloadFiles()
 {
+    initEnv();
+
     Strings old = loadedFiles;
     loadedFiles.clear();
 
@@ -407,6 +415,8 @@ void NixRepl::addAttrsToScope(Value & attrs)
 
 void NixRepl::addVarToScope(const Symbol & name, Value & v)
 {
+    if (displ >= envSize)
+        throw Error("environment full; cannot add more variables");
     staticEnv.vars[name] = displ;
     env->values[displ++] = &v;
     varNames.insert((string) name);