changeset 9144:c6463412aebb

eliminate symbol_table::scope_stack; fix scoping issue with evalin
author John W. Eaton <jwe@octave.org>
date Tue, 21 Apr 2009 15:39:57 -0400
parents 74d5c1a4ca96
children 53364bb317d4
files src/ChangeLog src/ls-mat5.cc src/mex.cc src/ov-fcn-handle.cc src/ov-usr-fcn.cc src/parse.y src/symtab.cc src/symtab.h src/toplev.cc src/toplev.h src/variables.cc
diffstat 11 files changed, 118 insertions(+), 86 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog	Tue Apr 21 10:00:11 2009 -0700
+++ b/src/ChangeLog	Tue Apr 21 15:39:57 2009 -0400
@@ -1,3 +1,30 @@
+2009-04-21  John W. Eaton  <jwe@octave.org>
+
+	* parse.y (Fassignin): Add missing unwind_protect frame.
+
+	* toplev.h (push (symbol_table::scope_id, symbol_table::context_id)):
+	New function.
+
+	* toplev.cc (main_loop): Don't call symbol_table::reset_scope.
+
+	* mex.cc (mexGetVariable, mexPutVariable): Use unwind_protect to
+	restore call stack and scope.
+
+	* ov-usr-fcn.cc (octave_user_function::do_multi_index_op):
+	Don't use symbol_table::push_scope.
+	* variablees.cc (do_who): Likewise.  Use octave_call_stack and
+	unwind_protect to manage change in scope.
+	* ov-fcn-handle.cc (octave_fcn_handle::load_ascii,
+	octave_fcn_handle::load_binary, octave_fcn_handle::load_hdf5):
+	Likewise.
+	* ls-mat5.cc (read_mat5_binary_element): Likewise.
+
+	* symtab.h (erase_scope (void*)): New function, for unwind_protect.
+	(symbol_table::push_scope, symbol_table::pop_scope,
+	symbol_table::reset_scope): Delete.
+	(symbol_table::scope_stack): Delete static member.
+	* symtab.cc (symbol_table::scope_stack): Delete definition.
+
 2009-04-17  Jaroslav Hajek  <highegg@gmail.com>
 
 	* oct-map.h (Octave_map::contents (const_iterator) const,
--- a/src/ls-mat5.cc	Tue Apr 21 10:00:11 2009 -0700
+++ b/src/ls-mat5.cc	Tue Apr 21 15:39:57 2009 -0400
@@ -65,6 +65,7 @@
 #include "pager.h"
 #include "pt-exp.h"
 #include "sysdep.h"
+#include "toplev.h"
 #include "unwind-prot.h"
 #include "utils.h"
 #include "variables.h"
@@ -868,7 +869,18 @@
 	    tc2 = m2.contents("MCOS")(0).cell_value()(1 + off).cell_value()(1);
 	    m2 = tc2.map_value();
 
+	    unwind_protect::begin_frame ("anon_mat5_load");
+
+	    // Set up temporary scope to use for evaluating the text
+	    // that defines the anonymous function.
+
 	    symbol_table::scope_id local_scope = symbol_table::alloc_scope ();
+	    unwind_protect::add (symbol_table::erase_scope, &local_scope);
+
+	    symbol_table::set_scope (local_scope);
+
+	    octave_call_stack::push (local_scope, 0);
+	    unwind_protect::add (octave_call_stack::unwind_pop, 0);
 
 	    if (m2.nfields() > 0)
 	      {
@@ -884,12 +896,6 @@
                   }
 	      }
 	    
-	    unwind_protect::begin_frame ("anon_mat5_load");
-	    
-	    symbol_table::push_scope (local_scope);
-
-	    unwind_protect::add (symbol_table::pop_scope);
-
 	    int parse_status;
 	    octave_value anon_fcn_handle = 
 	      eval_string (fname.substr (4), true, parse_status);
@@ -914,8 +920,6 @@
 	      }
 
 	    unwind_protect::run_frame ("anon_mat5_load");
-
-	    symbol_table::erase_scope (local_scope);
 	  }
 	else
 	  {
--- a/src/mex.cc	Tue Apr 21 10:00:11 2009 -0700
+++ b/src/mex.cc	Tue Apr 21 15:39:57 2009 -0400
@@ -3263,14 +3263,16 @@
 {
   mxArray *retval = 0;
 
-  // FIXME -- should this be in variables.cc?
-
   octave_value val;
 
   if (! strcmp (space, "global"))
     val = get_global_value (name);
   else
     {
+      // FIXME -- should this be in variables.cc?
+
+      unwind_protect::begin_frame ("mexGetVariable");
+
       bool caller = ! strcmp (space, "caller");
       bool base = ! strcmp (space, "base");
 
@@ -3281,12 +3283,15 @@
 	  else
 	    octave_call_stack::goto_base_frame ();
 
+	  if (! error_state)
+	    unwind_protect::add (octave_call_stack::unwind_pop);
+
 	  val = symbol_table::varval (name);
-
-	  octave_call_stack::pop ();
 	}
       else
 	mexErrMsgTxt ("mexGetVariable: symbol table does not exist");
+
+      unwind_protect::run_frame ("mexGetVariable");
     }
 
   if (val.is_defined ())
@@ -3326,6 +3331,8 @@
     {
       // FIXME -- should this be in variables.cc?
 
+      unwind_protect::begin_frame ("mexPutVariable");
+
       bool caller = ! strcmp (space, "caller");
       bool base = ! strcmp (space, "base");
 
@@ -3336,12 +3343,15 @@
 	  else
 	    octave_call_stack::goto_base_frame ();
 
+	  if (! error_state)
+	    unwind_protect::add (octave_call_stack::unwind_pop);
+
 	  symbol_table::varref (name) = mxArray::as_octave_value (ptr);
-
-	  octave_call_stack::pop ();
 	}
       else
 	mexErrMsgTxt ("mexPutVariable: symbol table does not exist");
+
+      unwind_protect::run_frame ("mexPutVariable");
     }
 
   return 0;
--- a/src/ov-fcn-handle.cc	Tue Apr 21 10:00:11 2009 -0700
+++ b/src/ov-fcn-handle.cc	Tue Apr 21 15:39:57 2009 -0400
@@ -313,7 +313,18 @@
 
       pos = is.tellg ();
 
+      unwind_protect::begin_frame ("anon_ascii_load");
+
+      // Set up temporary scope to use for evaluating the text that
+      // defines the anonymous function.
+
       symbol_table::scope_id local_scope = symbol_table::alloc_scope ();
+      unwind_protect::add (symbol_table::erase_scope, &local_scope);
+
+      symbol_table::set_scope (local_scope);
+
+      octave_call_stack::push (local_scope, 0);
+      unwind_protect::add (octave_call_stack::unwind_pop, 0);
 
       octave_idx_type len = 0;
 
@@ -347,12 +358,6 @@
 
       if (is && success)
 	{
-	  unwind_protect::begin_frame ("anon_ascii_load");
-
-	  symbol_table::push_scope (local_scope);
-
-	  unwind_protect::add (symbol_table::pop_scope);
-
 	  int parse_status;
 	  octave_value anon_fcn_handle = 
 	    eval_string (buf, true, parse_status);
@@ -376,13 +381,11 @@
 	    }
 	  else
 	    success = false;
-
-	  unwind_protect::run_frame ("anon_ascii_load");
 	}
       else
 	success = false;
 
-      symbol_table::erase_scope (local_scope);
+      unwind_protect::run_frame ("anon_ascii_load");
     }
   else
     success = set_fcn (octaveroot, fpath);
@@ -491,8 +494,19 @@
       OCTAVE_LOCAL_BUFFER (char, ctmp2, tmp+1);
       is.get (ctmp2, tmp+1, 0);
 
+      unwind_protect::begin_frame ("anon_binary_load");
+
+      // Set up temporary scope to use for evaluating the text that
+      // defines the anonymous function.
+
       symbol_table::scope_id local_scope = symbol_table::alloc_scope ();
-	      
+      unwind_protect::add (symbol_table::erase_scope, &local_scope);	      
+
+      symbol_table::set_scope (local_scope);
+
+      octave_call_stack::push (local_scope, 0);
+      unwind_protect::add (octave_call_stack::unwind_pop, 0);
+
       if (len > 0)
 	{
 	  for (octave_idx_type i = 0; i < len; i++)
@@ -517,12 +531,6 @@
 
       if (is && success)
 	{
-	  unwind_protect::begin_frame ("anon_binary_load");
-
-	  symbol_table::push_scope (local_scope);
-
-	  unwind_protect::add (symbol_table::pop_scope);
-
 	  int parse_status;
 	  octave_value anon_fcn_handle = 
 	    eval_string (ctmp2, true, parse_status);
@@ -545,11 +553,9 @@
 	    }
 	  else
 	    success = false;
-
-	  unwind_protect::run_frame ("anon_binary_load");
 	}
 
-      symbol_table::erase_scope (local_scope);
+      unwind_protect::run_frame ("anon_binary_load");
     }
   else
     {
@@ -933,7 +939,18 @@
       // restore error reporting:
       H5Eset_auto (err_func, err_func_data);
 
+      unwind_protect::begin_frame ("anon_hdf5_load");
+
+      // Set up temporary scope to use for evaluating the text that
+      // defines the anonymous function.
+
       symbol_table::scope_id local_scope = symbol_table::alloc_scope ();
+      unwind_protect::add (symbol_table::erase_scope, &local_scope);
+
+      symbol_table::set_scope (local_scope);
+
+      octave_call_stack::push (local_scope, 0);
+      unwind_protect::add (octave_call_stack::unwind_pop, 0);
 
       if (len > 0 && success)
 	{
@@ -974,12 +991,6 @@
 
       if (success)
 	{
-	  unwind_protect::begin_frame ("anon_hdf5_load");
-
-	  symbol_table::push_scope (local_scope);
-
-	  unwind_protect::add (symbol_table::pop_scope);
-
 	  int parse_status;
 	  octave_value anon_fcn_handle = 
 	    eval_string (fcn_tmp, true, parse_status);
@@ -1002,11 +1013,9 @@
 	    }
 	  else
 	    success = false;
-
-	  unwind_protect::run_frame ("anon_hdf5_load");
 	}
 
-      symbol_table::erase_scope (local_scope);
+      unwind_protect::run_frame ("anon_hdf5_load");
     }
   else
     {
--- a/src/ov-usr-fcn.cc	Tue Apr 21 10:00:11 2009 -0700
+++ b/src/ov-usr-fcn.cc	Tue Apr 21 15:39:57 2009 -0400
@@ -361,9 +361,6 @@
   // Save old and set current symbol table context, for
   // eval_undefined_error().
 
-  symbol_table::push_scope (local_scope);
-  unwind_protect::add (symbol_table::pop_scope);
-
   octave_call_stack::push (this, local_scope, call_depth);
   unwind_protect::add (octave_call_stack::unwind_pop, 0);
 
--- a/src/parse.y	Tue Apr 21 10:00:11 2009 -0700
+++ b/src/parse.y	Tue Apr 21 15:39:57 2009 -0400
@@ -4171,6 +4171,8 @@
 
       if (! error_state)
         {
+	  unwind_protect::begin_frame ("Fassignin");
+
 	  if (context == "caller")
 	    octave_call_stack::goto_caller_frame ();
 	  else if (context == "base")
@@ -4194,6 +4196,8 @@
 	      else
 		error ("assignin: expecting variable name as second argument");
 	    }
+
+	  unwind_protect::run_frame ("Fassignin");
 	}
       else
         error ("assignin: expecting string as first argument");
--- a/src/symtab.cc	Tue Apr 21 10:00:11 2009 -0700
+++ b/src/symtab.cc	Tue Apr 21 15:39:57 2009 -0400
@@ -64,8 +64,6 @@
 
 symbol_table::scope_id symbol_table::xparent_scope = -1;
 
-std::deque<symbol_table::scope_id> symbol_table::scope_stack;
-
 symbol_table::context_id symbol_table::xcurrent_context = 0;
 
 // Should Octave always check to see if function files have changed
--- a/src/symtab.h	Tue Apr 21 10:00:11 2009 -0700
+++ b/src/symtab.h	Tue Apr 21 15:39:57 2009 -0400
@@ -947,34 +947,6 @@
       }
   }
 
-  static void push_scope (scope_id scope)
-  {
-    if (scope_stack.empty ())
-      scope_stack.push_front (xtop_scope);
-
-    set_scope (scope);
-
-    scope_stack.push_front (scope);
-  }
-
-  static void pop_scope (void)
-  {
-    scope_stack.pop_front ();
-
-    set_scope (scope_stack[0]);
-  }
-
-  static void pop_scope (void *) { pop_scope (); }
-
-  static void reset_scope (void)
-  {
-    scope_stack.clear ();
-
-    scope_stack.push_front (xtop_scope);
-
-    set_scope (xtop_scope);
-  }
-
   static void set_parent_scope (scope_id scope)
   {
     xparent_scope = scope;
@@ -985,6 +957,13 @@
     set_parent_scope (-1);
   }
 
+  static void erase_scope (void *ptr)
+  {
+    scope_id *pscope = static_cast<scope_id *> (ptr);
+
+    erase_scope (*pscope);
+  }
+
   static void erase_scope (scope_id scope)
   {
     assert (scope != xglobal_scope);
@@ -1896,8 +1875,6 @@
 
   static context_id xcurrent_context;
 
-  static std::deque<scope_id> scope_stack;
-
   symbol_table (void)
     : table_name (), table () { }
 
--- a/src/toplev.cc	Tue Apr 21 10:00:11 2009 -0700
+++ b/src/toplev.cc	Tue Apr 21 15:39:57 2009 -0400
@@ -543,8 +543,6 @@
     {
       try
 	{
-	  symbol_table::reset_scope ();
-
 	  reset_error_handler ();
 
 	  reset_parser ();
--- a/src/toplev.h	Tue Apr 21 10:00:11 2009 -0700
+++ b/src/toplev.h	Tue Apr 21 15:39:57 2009 -0400
@@ -205,6 +205,14 @@
       instance->do_push (f, scope, context);
   }
 
+  static void
+  push (symbol_table::scope_id scope = symbol_table::current_scope (),
+	symbol_table::context_id context = symbol_table::current_context ())
+  {
+    if (instance_ok ())
+      instance->do_push (0, scope, context);
+  }
+
   static octave_function *top (void)
   {
     return instance_ok () ? instance->do_top (): 0;
--- a/src/variables.cc	Tue Apr 21 10:00:11 2009 -0700
+++ b/src/variables.cc	Tue Apr 21 15:39:57 2009 -0400
@@ -1381,14 +1381,16 @@
 	    {
 	      std::string nm = argv [i + 1];
 
-	      symbol_table::scope_id tmp_scope = symbol_table::alloc_scope ();
-
 	      unwind_protect::begin_frame ("do_who_file");
 
-	      symbol_table::push_scope (tmp_scope);
-	      symbol_table::push_context ();
-	      octave_call_stack::push (0);
+	      // Set up temporary scope.
 
+	      symbol_table::scope_id tmp_scope = symbol_table::alloc_scope ();
+	      unwind_protect::add (symbol_table::erase_scope, &tmp_scope);
+
+	      symbol_table::set_scope (tmp_scope);
+
+	      octave_call_stack::push (tmp_scope, 0);
 	      unwind_protect::add (octave_call_stack::unwind_pop, 0);
 
 	      unwind_protect::add (symbol_table::clear_variables);
@@ -1404,8 +1406,6 @@
 		}
 
 	      unwind_protect::run_frame ("do_who_file");
-
-	      symbol_table::erase_scope (tmp_scope);
 	    }
 
 	  return retval;