# HG changeset patch
# User Piotr Held <pjheld@gmail.com>
# Date 1506560586 25200
#      Wed Sep 27 18:03:06 2017 -0700
# Branch meta_class_clear_all
# Node ID 52c256feb0a9d35a76153c418c7fbc3e925156a0
# Parent  e250aeab3c85da520e14670f9559186a0d1c2a10
reinitialize meta class info after 'clear all' (Bug #52111)

* ov-classdef.cc, ov-classdef.h (cdef_manager::initialize): removed tmp_meta_* classes for code clarit. Moved the symbol table installation section to separate funciton. Removed the dynproperties and events classes since they are unimplemented and in their current state cause Octave to exit.
(cdef_manager::install_meta_classes_in_symtab): new private method to avoid code duplication.
(cdef_manager::reinit): new public method, reloads the classes and packages created in cdef_manager::initialize().
* variables.cc (clear, do_matlab_compatible_clear): add cdef_manager::reinit() after all symbol_table::clear_all() calls.

diff -r e250aeab3c85 -r 52c256feb0a9 libinterp/corefcn/variables.cc
--- a/libinterp/corefcn/variables.cc	Tue Sep 26 15:18:31 2017 -0700
+++ b/libinterp/corefcn/variables.cc	Wed Sep 27 18:03:06 2017 -0700
@@ -2227,6 +2227,10 @@
           && ! scope->is_local_variable ("all"))
         {
           symtab.clear_all ();
+          // Reinstall meta.class, meta.property etc.
+          cdef_manager &cdm
+            = octave::__get_cdef_manager__ ("do_matlab_compatible_clear");
+          cdm.reinit ();
         }
       else if (argv[idx] == "functions"
                && ! scope->is_local_variable ("functions"))
@@ -2249,6 +2253,10 @@
           scope->clear_objects ();
           octave_class::clear_exemplar_map ();
           symtab.clear_all ();
+          // Reinstall meta.class, meta.property etc.
+          cdef_manager &cdm
+            = octave::__get_cdef_manager__ ("do_matlab_compatible_clear");
+          cdm.reinit ();
         }
       else
         {
@@ -2434,6 +2442,10 @@
                     warning ("clear: ignoring extra arguments after -all");
 
                   symtab.clear_all ();
+                  // Reinstall meta.class, meta.property etc.
+                  cdef_manager &cdm
+                    = octave::__get_cdef_manager__ ("clear");
+                  cdm.reinit ();
                 }
               else if (have_regexp)
                 {
@@ -2457,6 +2469,10 @@
                     scope->clear_objects ();
                   octave_class::clear_exemplar_map ();
                   symtab.clear_all ();
+                  // Reinstall meta.class, meta.property etc.
+                  cdef_manager &cdm
+                    = octave::__get_cdef_manager__ ("clear");
+                  cdm.reinit ();
                 }
               else
                 {
diff -r e250aeab3c85 -r 52c256feb0a9 libinterp/octave-value/ov-classdef.cc
--- a/libinterp/octave-value/ov-classdef.cc	Tue Sep 26 15:18:31 2017 -0700
+++ b/libinterp/octave-value/ov-classdef.cc	Wed Sep 27 18:03:06 2017 -0700
@@ -3359,204 +3359,194 @@
   // bootstrap
   cdef_class tmp_handle = make_class ("handle");
 
-  cdef_class tmp_meta_class
-    = m_meta_class
+  m_meta_class
     = make_meta_class ("meta.class", tmp_handle);
 
-  tmp_handle.set_class (tmp_meta_class);
-  tmp_meta_class.set_class (tmp_meta_class);
+  tmp_handle.set_class (m_meta_class);
+  m_meta_class.set_class (m_meta_class);
 
   // meta classes
-  cdef_class tmp_meta_property
-    = m_meta_property
+  m_meta_property
     = make_meta_class ("meta.property", tmp_handle);
 
-  cdef_class tmp_meta_method
-    = m_meta_method
+  m_meta_method
     = make_meta_class ("meta.method", tmp_handle);
 
-  cdef_class tmp_meta_package
-    = m_meta_package
+  m_meta_package
     = make_meta_class ("meta.package", tmp_handle);
 
-  cdef_class tmp_meta_event
-    = make_meta_class ("meta.event", tmp_handle);
-
-  cdef_class tmp_meta_dynproperty
-    = make_meta_class ("meta.dynamicproperty", tmp_handle);
-
   // meta.class properties
-  tmp_meta_class.install_property
-    (make_attribute (tmp_meta_class, "Abstract"));
-
-  tmp_meta_class.install_property
-    (make_attribute (tmp_meta_class, "ConstructOnLoad"));
-
-  tmp_meta_class.install_property
-    (make_property  (tmp_meta_class, "ContainingPackage"));
-
-  tmp_meta_class.install_property
-    (make_property  (tmp_meta_class, "Description"));
-
-  tmp_meta_class.install_property
-    (make_property  (tmp_meta_class, "DetailedDescription"));
-
-  tmp_meta_class.install_property
-    (make_property  (tmp_meta_class, "Events"));
-
-  tmp_meta_class.install_property
-    (make_attribute (tmp_meta_class, "HandleCompatible"));
-
-  tmp_meta_class.install_property
-    (make_attribute (tmp_meta_class, "Hidden"));
-
-  tmp_meta_class.install_property
-    (make_property (tmp_meta_class, "InferiorClasses",
+  m_meta_class.install_property
+    (make_attribute (m_meta_class, "Abstract"));
+
+  m_meta_class.install_property
+    (make_attribute (m_meta_class, "ConstructOnLoad"));
+
+  m_meta_class.install_property
+    (make_property  (m_meta_class, "ContainingPackage"));
+
+  m_meta_class.install_property
+    (make_property  (m_meta_class, "Description"));
+
+  m_meta_class.install_property
+    (make_property  (m_meta_class, "DetailedDescription"));
+
+  m_meta_class.install_property
+    (make_property  (m_meta_class, "Events"));
+
+  m_meta_class.install_property
+    (make_attribute (m_meta_class, "HandleCompatible"));
+
+  m_meta_class.install_property
+    (make_attribute (m_meta_class, "Hidden"));
+
+  m_meta_class.install_property
+    (make_property (m_meta_class, "InferiorClasses",
                     make_fcn_handle (class_get_inferiorclasses,
                                      "meta.class>get.InferiorClasses"),
                     "public", Matrix (), "private"));
 
-  tmp_meta_class.install_property
-    (make_property (tmp_meta_class, "Methods",
+  m_meta_class.install_property
+    (make_property (m_meta_class, "Methods",
                     make_fcn_handle (class_get_methods,
                                      "meta.class>get.Methods"),
                     "public", Matrix (), "private"));
 
-  tmp_meta_class.install_property
-    (make_property (tmp_meta_class, "MethodList",
+  m_meta_class.install_property
+    (make_property (m_meta_class, "MethodList",
                      make_fcn_handle (class_get_methods,
                                       "meta.class>get.MethodList"),
                     "public", Matrix (), "private"));
 
-  tmp_meta_class.install_property (make_attribute (tmp_meta_class, "Name"));
-
-  tmp_meta_class.install_property
-    (make_property (tmp_meta_class, "Properties",
+  m_meta_class.install_property (make_attribute (m_meta_class, "Name"));
+
+  m_meta_class.install_property
+    (make_property (m_meta_class, "Properties",
                     make_fcn_handle (class_get_properties,
                                      "meta.class>get.Properties"),
                     "public", Matrix (), "private"));
 
-  tmp_meta_class.install_property
-    (make_property (tmp_meta_class, "PropertyList",
+  m_meta_class.install_property
+    (make_property (m_meta_class, "PropertyList",
                     make_fcn_handle (class_get_properties,
                                      "meta.class>get.PropertyList"),
                     "public", Matrix (), "private"));
 
-  tmp_meta_class.install_property (make_attribute (tmp_meta_class, "Sealed"));
-
-  tmp_meta_class.install_property
-    (make_property (tmp_meta_class, "SuperClasses",
+  m_meta_class.install_property (make_attribute (m_meta_class, "Sealed"));
+
+  m_meta_class.install_property
+    (make_property (m_meta_class, "SuperClasses",
                     make_fcn_handle (class_get_superclasses,
                                      "meta.class>get.SuperClasses"),
                     "public", Matrix (), "private"));
 
-  tmp_meta_class.install_property
-    (make_property (tmp_meta_class, "SuperClassList",
+  m_meta_class.install_property
+    (make_property (m_meta_class, "SuperClassList",
                     make_fcn_handle (class_get_superclasses,
                                      "meta.class>get.SuperClassList"),
                     "public", Matrix (), "private"));
 
   // meta.class methods
-  tmp_meta_class.install_method
-    (make_method (tmp_meta_class, "fromName", class_fromName, "public", true));
-
-  tmp_meta_class.install_method
-    (make_method (tmp_meta_class, "fevalStatic", class_fevalStatic, "public",
+  m_meta_class.install_method
+    (make_method (m_meta_class, "fromName", class_fromName, "public", true));
+
+  m_meta_class.install_method
+    (make_method (m_meta_class, "fevalStatic", class_fevalStatic, "public",
                   false));
 
-  tmp_meta_class.install_method
-    (make_method (tmp_meta_class, "getConstant", class_getConstant, "public",
+  m_meta_class.install_method
+    (make_method (m_meta_class, "getConstant", class_getConstant, "public",
                   false));
 
-  tmp_meta_class.install_method (make_method (tmp_meta_class, "eq", class_eq));
-  tmp_meta_class.install_method (make_method (tmp_meta_class, "ne", class_ne));
-  tmp_meta_class.install_method (make_method (tmp_meta_class, "lt", class_lt));
-  tmp_meta_class.install_method (make_method (tmp_meta_class, "le", class_le));
-  tmp_meta_class.install_method (make_method (tmp_meta_class, "gt", class_gt));
-  tmp_meta_class.install_method (make_method (tmp_meta_class, "ge", class_ge));
+  m_meta_class.install_method (make_method (m_meta_class, "eq", class_eq));
+  m_meta_class.install_method (make_method (m_meta_class, "ne", class_ne));
+  m_meta_class.install_method (make_method (m_meta_class, "lt", class_lt));
+  m_meta_class.install_method (make_method (m_meta_class, "le", class_le));
+  m_meta_class.install_method (make_method (m_meta_class, "gt", class_gt));
+  m_meta_class.install_method (make_method (m_meta_class, "ge", class_ge));
 
   // meta.method properties
-  tmp_meta_method.install_property
-    (make_attribute (tmp_meta_method, "Abstract"));
-
-  tmp_meta_method.install_property
-    (make_attribute (tmp_meta_method, "Access"));
-
-  tmp_meta_method.install_property
-    (make_attribute (tmp_meta_method, "DefiningClass"));
-
-  tmp_meta_method.install_property
-    (make_attribute (tmp_meta_method, "Description"));
-
-  tmp_meta_method.install_property
-    (make_attribute (tmp_meta_method, "DetailedDescription"));
-
-  tmp_meta_method.install_property
-    (make_attribute (tmp_meta_method, "Hidden"));
-
-  tmp_meta_method.install_property
-    (make_attribute (tmp_meta_method, "Name"));
-
-  tmp_meta_method.install_property
-    (make_attribute (tmp_meta_method, "Sealed"));
-
-  tmp_meta_method.install_property
-    (make_attribute (tmp_meta_method, "Static"));
+  m_meta_method.install_property
+    (make_attribute (m_meta_method, "Abstract"));
+
+  m_meta_method.install_property
+    (make_attribute (m_meta_method, "Access"));
+
+  m_meta_method.install_property
+    (make_attribute (m_meta_method, "DefiningClass"));
+
+  m_meta_method.install_property
+    (make_attribute (m_meta_method, "Description"));
+
+  m_meta_method.install_property
+    (make_attribute (m_meta_method, "DetailedDescription"));
+
+  m_meta_method.install_property
+    (make_attribute (m_meta_method, "Hidden"));
+
+  m_meta_method.install_property
+    (make_attribute (m_meta_method, "Name"));
+
+  m_meta_method.install_property
+    (make_attribute (m_meta_method, "Sealed"));
+
+  m_meta_method.install_property
+    (make_attribute (m_meta_method, "Static"));
 
   // meta.property properties
-  tmp_meta_property.install_property
-    (make_attribute (tmp_meta_property, "Name"));
-
-  tmp_meta_property.install_property
-    (make_attribute (tmp_meta_property, "Description"));
-
-  tmp_meta_property.install_property
-    (make_attribute (tmp_meta_property, "DetailedDescription"));
-
-  tmp_meta_property.install_property
-    (make_attribute (tmp_meta_property, "Abstract"));
-
-  tmp_meta_property.install_property
-    (make_attribute (tmp_meta_property, "Constant"));
-
-  tmp_meta_property.install_property
-    (make_attribute (tmp_meta_property, "GetAccess"));
-
-  tmp_meta_property.install_property
-    (make_attribute (tmp_meta_property, "SetAccess"));
-
-  tmp_meta_property.install_property
-    (make_attribute (tmp_meta_property, "Dependent"));
-
-  tmp_meta_property.install_property
-    (make_attribute (tmp_meta_property, "Transient"));
-
-  tmp_meta_property.install_property
-    (make_attribute (tmp_meta_property, "Hidden"));
-
-  tmp_meta_property.install_property
-    (make_attribute (tmp_meta_property, "GetObservable"));
-
-  tmp_meta_property.install_property
-    (make_attribute (tmp_meta_property, "SetObservable"));
-
-  tmp_meta_property.install_property
-    (make_attribute (tmp_meta_property, "GetMethod"));
-
-  tmp_meta_property.install_property
-    (make_attribute (tmp_meta_property, "SetMethod"));
-
-  tmp_meta_property.install_property
-    (make_attribute (tmp_meta_property, "DefiningClass"));
-
-  tmp_meta_property.install_property
-    (make_property (tmp_meta_property, "DefaultValue",
+  m_meta_property.install_property
+    (make_attribute (m_meta_property, "Name"));
+
+  m_meta_property.install_property
+    (make_attribute (m_meta_property, "Description"));
+
+  m_meta_property.install_property
+    (make_attribute (m_meta_property, "DetailedDescription"));
+
+  m_meta_property.install_property
+    (make_attribute (m_meta_property, "Abstract"));
+
+  m_meta_property.install_property
+    (make_attribute (m_meta_property, "Constant"));
+
+  m_meta_property.install_property
+    (make_attribute (m_meta_property, "GetAccess"));
+
+  m_meta_property.install_property
+    (make_attribute (m_meta_property, "SetAccess"));
+
+  m_meta_property.install_property
+    (make_attribute (m_meta_property, "Dependent"));
+
+  m_meta_property.install_property
+    (make_attribute (m_meta_property, "Transient"));
+
+  m_meta_property.install_property
+    (make_attribute (m_meta_property, "Hidden"));
+
+  m_meta_property.install_property
+    (make_attribute (m_meta_property, "GetObservable"));
+
+  m_meta_property.install_property
+    (make_attribute (m_meta_property, "SetObservable"));
+
+  m_meta_property.install_property
+    (make_attribute (m_meta_property, "GetMethod"));
+
+  m_meta_property.install_property
+    (make_attribute (m_meta_property, "SetMethod"));
+
+  m_meta_property.install_property
+    (make_attribute (m_meta_property, "DefiningClass"));
+
+  m_meta_property.install_property
+    (make_property (m_meta_property, "DefaultValue",
                     make_fcn_handle (property_get_defaultvalue,
                                      "meta.property>get.DefaultValue"),
                     "public", Matrix (), "private"));
 
-  tmp_meta_property.install_property
-    (make_attribute (tmp_meta_property, "HasDefault"));
+  m_meta_property.install_property
+    (make_attribute (m_meta_property, "HasDefault"));
 
   // meta.property events
   // FIXME: add events
@@ -3568,96 +3558,102 @@
 
   // meta.package properties
 
-  tmp_meta_package.install_property
-    (make_attribute (tmp_meta_package, "Name"));
-
-  tmp_meta_package.install_property
-    (make_property  (tmp_meta_package, "ContainingPackage"));
-
-  tmp_meta_package.install_property
-    (make_property (tmp_meta_package, "ClassList",
+  m_meta_package.install_property
+    (make_attribute (m_meta_package, "Name"));
+
+  m_meta_package.install_property
+    (make_property  (m_meta_package, "ContainingPackage"));
+
+  m_meta_package.install_property
+    (make_property (m_meta_package, "ClassList",
                     make_fcn_handle (package_get_classes,
                                      "meta.package>get.ClassList"),
                     "public", Matrix (), "private"));
 
-  tmp_meta_package.install_property
-    (make_property (tmp_meta_package, "Classes",
+  m_meta_package.install_property
+    (make_property (m_meta_package, "Classes",
                     make_fcn_handle (package_get_classes,
                                      "meta.package>get.Classes"),
                     "public", Matrix (), "private"));
 
-  tmp_meta_package.install_property
-    (make_property (tmp_meta_package, "FunctionList",
+  m_meta_package.install_property
+    (make_property (m_meta_package, "FunctionList",
                     make_fcn_handle (package_get_functions,
                                      "meta.package>get.FunctionList"),
                     "public", Matrix (), "private"));
 
-  tmp_meta_package.install_property
-    (make_property (tmp_meta_package, "Functions",
+  m_meta_package.install_property
+    (make_property (m_meta_package, "Functions",
                     make_fcn_handle (package_get_functions,
                                      "meta.package>get.Functions"),
                     "public", Matrix (), "private"));
 
-  tmp_meta_package.install_property
-    (make_property (tmp_meta_package, "PackageList",
+  m_meta_package.install_property
+    (make_property (m_meta_package, "PackageList",
                       make_fcn_handle (package_get_packages,
                                        "meta.package>get.PackageList"),
                     "public", Matrix (), "private"));
 
-  tmp_meta_package.install_property
-    (make_property (tmp_meta_package, "Packages",
+  m_meta_package.install_property
+    (make_property (m_meta_package, "Packages",
                     make_fcn_handle (package_get_packages,
                                      "meta.package>get.Packages"),
                     "public", Matrix (), "private"));
 
-  tmp_meta_package.install_method
-    (make_method (tmp_meta_package, "fromName", package_fromName,
+  m_meta_package.install_method
+    (make_method (m_meta_package, "fromName", package_fromName,
                   "public", true));
 
-  tmp_meta_package.install_method
-    (make_method (tmp_meta_package, "getAllPackages", package_getAllPackages,
+  m_meta_package.install_method
+    (make_method (m_meta_package, "getAllPackages", package_getAllPackages,
                   "public", true));
 
   // create "meta" package
-  cdef_package package_meta
-    = m_meta
+  m_meta
     = make_package ("meta");
 
-  package_meta.install_class (tmp_meta_class, "class");
-  package_meta.install_class (tmp_meta_property, "property");
-  package_meta.install_class (tmp_meta_method, "method");
-  package_meta.install_class (tmp_meta_package, "package");
-  package_meta.install_class (tmp_meta_event, "event");
-  package_meta.install_class (tmp_meta_dynproperty, "dynproperty");
-
+  m_meta.install_class (m_meta_class, "class");
+  m_meta.install_class (m_meta_property, "property");
+  m_meta.install_class (m_meta_method, "method");
+  m_meta.install_class (m_meta_package, "package");
+
+  install_meta_classes_in_symtab ();
+}
+
+void
+cdef_manager::reinit (void)
+{
+  if (m_meta.ok ())
+    {
+      register_package (m_meta);
+      install_meta_classes_in_symtab ();
+    }
+}
+
+void
+cdef_manager::install_meta_classes_in_symtab (void)
+{
   octave::symbol_table& symtab = m_interpreter.get_symbol_table ();
 
   // install built-in classes into the symbol table
   symtab.install_built_in_function
     ("meta.class",
-     octave_value (tmp_meta_class.get_constructor_function ()));
+     octave_value (m_meta_class.get_constructor_function ()));
 
   symtab.install_built_in_function
     ("meta.method",
-     octave_value (tmp_meta_method.get_constructor_function ()));
+     octave_value (m_meta_method.get_constructor_function ()));
 
   symtab.install_built_in_function
     ("meta.property",
-     octave_value (tmp_meta_property.get_constructor_function ()));
+     octave_value (m_meta_property.get_constructor_function ()));
 
   symtab.install_built_in_function
     ("meta.package",
-     octave_value (tmp_meta_package.get_constructor_function ()));
-
-  symtab.install_built_in_function
-    ("meta.event",
-     octave_value (tmp_meta_event.get_constructor_function ()));
-
-  symtab.install_built_in_function
-    ("meta.dynproperty",
-     octave_value (tmp_meta_dynproperty.get_constructor_function ()));
+     octave_value (m_meta_package.get_constructor_function ()));
 }
 
+
 cdef_class
 cdef_manager::find_class (const std::string& name, bool error_if_not_found,
                           bool load_if_not_found)
diff -r e250aeab3c85 -r 52c256feb0a9 libinterp/octave-value/ov-classdef.h
--- a/libinterp/octave-value/ov-classdef.h	Tue Sep 26 15:18:31 2017 -0700
+++ b/libinterp/octave-value/ov-classdef.h	Wed Sep 27 18:03:06 2017 -0700
@@ -1615,6 +1615,8 @@
 
   void initialize (void);
 
+  void reinit (void);
+
   cdef_class find_class (const std::string& name, bool error_if_not_found = true,
                          bool load_if_not_found = true);
 
@@ -1656,18 +1658,30 @@
 
 private:
 
+  //! Installs the m_meta_class, m_meta_property etc. into symbol table.
+  //!
+  //! Whenever more meta objects (such as meta.event, meta.DynamicProperty)
+  //! are to be added they also need to be installed here into the symbol table.
+  void install_meta_classes_in_symtab (void);
+
   octave::interpreter& m_interpreter;
 
-  // All registered/loaded classes
+  //! All registered/loaded classes
   std::map<std::string, cdef_class> m_all_classes;
 
-  // All registered/loaded packages
+  //! All registered/loaded packages
   std::map<std::string, cdef_package> m_all_packages;
 
+  //! All of the meta.class, meta.property etc. objects.
+  //!
+  //! Plase note that only meta.class, meta.property, meta.method,
+  //! and meta.package are currently supported.
+  //@{
   cdef_class m_meta_class;
   cdef_class m_meta_property;
   cdef_class m_meta_method;
   cdef_class m_meta_package;
+  //@}
 
   cdef_package m_meta;
 };
