Merge remote-tracking branch 'origin/stable-2.0'
[bpt/guile.git] / libguile / modules.c
index 6c3f262..d87ec7a 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1998,2000,2001,2002,2003,2004,2006,2007,2008,2009,2010,2011 Free Software Foundation, Inc.
+/* Copyright (C) 1998,2000,2001,2002,2003,2004,2006,2007,2008,2009,2010,2011,2012 Free Software Foundation, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public License
@@ -80,9 +80,10 @@ SCM_DEFINE (scm_current_module, "current-module", 0, 0, 0,
            "Return the current module.")
 #define FUNC_NAME s_scm_current_module
 {
-  SCM curr = scm_fluid_ref (the_module);
-
-  return scm_is_true (curr) ? curr : scm_the_root_module ();
+  if (scm_module_system_booted_p)
+    return scm_fluid_ref (the_module);
+  else
+    return SCM_BOOL_F;
 }
 #undef FUNC_NAME
 
@@ -153,7 +154,7 @@ convert_module_name (const char *name)
        ptr++;
       if (ptr > name)
        {
-         SCM sym = scm_from_locale_symboln (name, ptr-name);
+         SCM sym = scm_from_utf8_symboln (name, ptr-name);
          *tail = scm_cons (sym, SCM_EOL);
          tail = SCM_CDRLOC (*tail);
        }
@@ -218,7 +219,7 @@ scm_c_export (const char *name, ...)
   if (name)
     {
       va_list ap;
-      SCM names = scm_cons (scm_from_locale_symbol (name), SCM_EOL);
+      SCM names = scm_cons (scm_from_utf8_symbol (name), SCM_EOL);
       SCM *tail = SCM_CDRLOC (names);
       va_start (ap, name);
       while (1)
@@ -226,7 +227,7 @@ scm_c_export (const char *name, ...)
          const char *n = va_arg (ap, const char *);
          if (n == NULL)
            break;
-         *tail = scm_cons (scm_from_locale_symbol (n), SCM_EOL);
+         *tail = scm_cons (scm_from_utf8_symbol (n), SCM_EOL);
          tail = SCM_CDRLOC (*tail);
        }
       va_end (ap);
@@ -235,38 +236,6 @@ scm_c_export (const char *name, ...)
 }
 
 
-/* Environments */
-
-SCM_SYMBOL (sym_module, "module");
-
-SCM
-scm_lookup_closure_module (SCM proc)
-{
-  if (scm_is_false (proc))
-    return scm_the_root_module ();
-  else if (SCM_EVAL_CLOSURE_P (proc))
-    return SCM_PACK (SCM_SMOB_DATA (proc));
-  else
-    {
-      SCM mod;
-
-      /* FIXME: The `module' property is no longer set on eval closures, as it
-        introduced a circular reference that precludes garbage collection of
-        modules with the current weak hash table semantics (see
-        http://lists.gnu.org/archive/html/guile-devel/2009-01/msg00102.html and
-        http://thread.gmane.org/gmane.comp.programming.garbage-collection.boehmgc/2465
-        for details). Since it doesn't appear to be used (only in this
-        function, which has 1 caller), we no longer extend
-        `set-module-eval-closure!' to set the `module' property. */
-      abort ();
-
-      mod = scm_procedure_property (proc, sym_module);
-      if (scm_is_false (mod))
-       mod = scm_the_root_module ();
-      return mod;
-    }
-}
-
 /*
  * C level implementation of the standard eval closure
  *
@@ -519,84 +488,37 @@ SCM_DEFINE (scm_module_variable, "module-variable", 2, 0, 0,
 }
 #undef FUNC_NAME
 
-scm_t_bits scm_tc16_eval_closure;
-
-#define SCM_F_EVAL_CLOSURE_INTERFACE (1<<0)
-#define SCM_EVAL_CLOSURE_INTERFACE_P(e) \
-  (SCM_SMOB_FLAGS (e) & SCM_F_EVAL_CLOSURE_INTERFACE)
-
-/* NOTE: This function may be called by a smob application
-   or from another C function directly. */
 SCM
-scm_eval_closure_lookup (SCM eclo, SCM sym, SCM definep)
+scm_module_ensure_local_variable (SCM module, SCM sym)
+#define FUNC_NAME "module-ensure-local-variable"
 {
-  SCM module = SCM_PACK (SCM_SMOB_DATA (eclo));
-  if (scm_is_true (definep))
+  if (SCM_LIKELY (scm_module_system_booted_p))
     {
-      if (SCM_EVAL_CLOSURE_INTERFACE_P (eclo))
-       return SCM_BOOL_F;
+      SCM_VALIDATE_MODULE (1, module);
+      SCM_VALIDATE_SYMBOL (2, sym);
+
       return scm_call_2 (SCM_VARIABLE_REF (module_make_local_var_x_var),
-                        module, sym);
+                         module, sym);
     }
-  else
-    return scm_module_variable (module, sym);
-}
 
-SCM_DEFINE (scm_standard_eval_closure, "standard-eval-closure", 1, 0, 0,
-           (SCM module),
-           "Return an eval closure for the module @var{module}.")
-#define FUNC_NAME s_scm_standard_eval_closure
-{
-  SCM_RETURN_NEWSMOB (scm_tc16_eval_closure, SCM_UNPACK (module));
-}
-#undef FUNC_NAME
+  {
+    SCM handle, var;
 
+    handle = scm_hashq_create_handle_x (scm_pre_modules_obarray,
+                                        sym, SCM_BOOL_F);
+    var = SCM_CDR (handle);
 
-SCM_DEFINE (scm_standard_interface_eval_closure,
-           "standard-interface-eval-closure", 1, 0, 0,
-           (SCM module),
-           "Return a interface eval closure for the module @var{module}. "
-           "Such a closure does not allow new bindings to be added.")
-#define FUNC_NAME s_scm_standard_interface_eval_closure
-{
-  SCM_RETURN_NEWSMOB (scm_tc16_eval_closure | (SCM_F_EVAL_CLOSURE_INTERFACE<<16),
-                     SCM_UNPACK (module));
-}
-#undef FUNC_NAME
+    if (scm_is_false (var))
+      {
+        var = scm_make_variable (SCM_UNDEFINED);
+        SCM_SETCDR (handle, var);
+      }
 
-SCM_DEFINE (scm_eval_closure_module,
-           "eval-closure-module", 1, 0, 0,
-           (SCM eval_closure),
-           "Return the module associated with this eval closure.")
-/* the idea is that eval closures are really not the way to do things, they're
-   superfluous given our module system. this function lets mmacros migrate away
-   from eval closures. */
-#define FUNC_NAME s_scm_eval_closure_module
-{
-  SCM_MAKE_VALIDATE_MSG (SCM_ARG1, eval_closure, EVAL_CLOSURE_P,
-                         "eval-closure");
-  return SCM_SMOB_OBJECT (eval_closure);
+    return var;
+  }
 }
 #undef FUNC_NAME
 
-SCM
-scm_module_lookup_closure (SCM module)
-{
-  if (scm_is_false (module))
-    return SCM_BOOL_F;
-  else
-    return SCM_MODULE_EVAL_CLOSURE (module);
-}
-
-SCM
-scm_current_module_lookup_closure ()
-{
-  if (scm_module_system_booted_p)
-    return scm_module_lookup_closure (scm_current_module ());
-  else
-    return SCM_BOOL_F;
-}
-
 SCM_SYMBOL (sym_macroexpand, "macroexpand");
 
 SCM_DEFINE (scm_module_transformer, "module-transformer", 1, 0, 0,
@@ -676,65 +598,10 @@ scm_module_public_interface (SCM module)
   return scm_call_1 (SCM_VARIABLE_REF (module_public_interface_var), module);
 }
 
-/* scm_sym2var
- *
- * looks up the variable bound to SYM according to PROC.  PROC should be
- * a `eval closure' of some module.
- *
- * When no binding exists, and DEFINEP is true, create a new binding
- * with a initial value of SCM_UNDEFINED.  Return `#f' when DEFINEP as
- * false and no binding exists.
- *
- * When PROC is `#f', it is ignored and the binding is searched for in
- * the scm_pre_modules_obarray (a `eq' hash table).
- */
-
-SCM 
-scm_sym2var (SCM sym, SCM proc, SCM definep)
-#define FUNC_NAME "scm_sym2var"
-{
-  SCM var;
-
-  if (SCM_NIMP (proc))
-    {
-      if (SCM_EVAL_CLOSURE_P (proc))
-       {
-         /* Bypass evaluator in the standard case. */
-         var = scm_eval_closure_lookup (proc, sym, definep);
-       }
-      else
-       var = scm_call_2 (proc, sym, definep);
-    }
-  else
-    {
-      SCM handle;
-
-      if (scm_is_false (definep))
-       var = scm_hashq_ref (scm_pre_modules_obarray, sym, SCM_BOOL_F);
-      else
-       {
-         handle = scm_hashq_create_handle_x (scm_pre_modules_obarray,
-                                             sym, SCM_BOOL_F);
-         var = SCM_CDR (handle);
-         if (scm_is_false (var))
-           {
-             var = scm_make_variable (SCM_UNDEFINED);
-             SCM_SETCDR (handle, var);
-           }
-       }
-    }
-
-  if (scm_is_true (var) && !SCM_VARIABLEP (var))
-    SCM_MISC_ERROR ("~S is not bound to a variable", scm_list_1 (sym));
-
-  return var;
-}
-#undef FUNC_NAME
-
 SCM
 scm_c_module_lookup (SCM module, const char *name)
 {
-  return scm_module_lookup (module, scm_from_locale_symbol (name));
+  return scm_module_lookup (module, scm_from_utf8_symbol (name));
 }
 
 SCM
@@ -742,9 +609,7 @@ scm_module_lookup (SCM module, SCM sym)
 #define FUNC_NAME "module-lookup"
 {
   SCM var;
-  SCM_VALIDATE_MODULE (1, module);
-
-  var = scm_sym2var (sym, scm_module_lookup_closure (module), SCM_BOOL_F);
+  var = scm_module_variable (module, sym);
   if (scm_is_false (var))
     unbound_variable (FUNC_NAME, sym);
   return var;
@@ -754,17 +619,13 @@ scm_module_lookup (SCM module, SCM sym)
 SCM
 scm_c_lookup (const char *name)
 {
-  return scm_lookup (scm_from_locale_symbol (name));
+  return scm_lookup (scm_from_utf8_symbol (name));
 }
 
 SCM
 scm_lookup (SCM sym)
 {
-  SCM var = 
-    scm_sym2var (sym, scm_current_module_lookup_closure (), SCM_BOOL_F);
-  if (scm_is_false (var))
-    unbound_variable (NULL, sym);
-  return var;
+  return scm_module_lookup (scm_current_module (), sym);
 }
 
 SCM
@@ -807,14 +668,14 @@ SCM
 scm_c_public_variable (const char *module_name, const char *name)
 {
   return scm_public_variable (convert_module_name (module_name),
-                              scm_from_locale_symbol (name));
+                              scm_from_utf8_symbol (name));
 }
 
 SCM
 scm_c_private_variable (const char *module_name, const char *name)
 {
   return scm_private_variable (convert_module_name (module_name),
-                               scm_from_locale_symbol (name));
+                               scm_from_utf8_symbol (name));
 }
 
 SCM
@@ -849,14 +710,14 @@ SCM
 scm_c_public_lookup (const char *module_name, const char *name)
 {
   return scm_public_lookup (convert_module_name (module_name),
-                            scm_from_locale_symbol (name));
+                            scm_from_utf8_symbol (name));
 }
 
 SCM
 scm_c_private_lookup (const char *module_name, const char *name)
 {
   return scm_private_lookup (convert_module_name (module_name),
-                             scm_from_locale_symbol (name));
+                             scm_from_utf8_symbol (name));
 }
 
 SCM
@@ -875,20 +736,20 @@ SCM
 scm_c_public_ref (const char *module_name, const char *name)
 {
   return scm_public_ref (convert_module_name (module_name),
-                         scm_from_locale_symbol (name));
+                         scm_from_utf8_symbol (name));
 }
 
 SCM
 scm_c_private_ref (const char *module_name, const char *name)
 {
   return scm_private_ref (convert_module_name (module_name),
-                          scm_from_locale_symbol (name));
+                          scm_from_utf8_symbol (name));
 }
 
 SCM
 scm_c_module_define (SCM module, const char *name, SCM value)
 {
-  return scm_module_define (module, scm_from_locale_symbol (name), value);
+  return scm_module_define (module, scm_from_utf8_symbol (name), value);
 }
 
 SCM
@@ -896,10 +757,10 @@ scm_module_define (SCM module, SCM sym, SCM value)
 #define FUNC_NAME "module-define"
 {
   SCM var;
-  SCM_VALIDATE_MODULE (1, module);
 
-  var = scm_sym2var (sym, scm_module_lookup_closure (module), SCM_BOOL_T);
+  var = scm_module_ensure_local_variable (module, sym);
   SCM_VARIABLE_SET (var, value);
+
   return var;
 }
 #undef FUNC_NAME
@@ -907,7 +768,7 @@ scm_module_define (SCM module, SCM sym, SCM value)
 SCM
 scm_c_define (const char *name, SCM value)
 {
-  return scm_define (scm_from_locale_symbol (name), value);
+  return scm_define (scm_from_utf8_symbol (name), value);
 }
 
 SCM_DEFINE (scm_define, "define!", 2, 0, 0,
@@ -917,11 +778,9 @@ SCM_DEFINE (scm_define, "define!", 2, 0, 0,
             "not a macro.")
 #define FUNC_NAME s_scm_define
 {
-  SCM var;
   SCM_VALIDATE_SYMBOL (SCM_ARG1, sym);
-  var = scm_sym2var (sym, scm_current_module_lookup_closure (), SCM_BOOL_T);
-  SCM_VARIABLE_SET (var, value);
-  return var;
+
+  return scm_module_define (scm_current_module (), sym, value);
 }
 #undef FUNC_NAME
 
@@ -960,16 +819,8 @@ SCM_DEFINE (scm_module_reverse_lookup, "module-reverse-lookup", 2, 0, 0,
        {
          handle = SCM_CAR (ls);
 
-         if (SCM_UNPACK (SCM_CAR (handle)) == 0)
-           {
-             /* FIXME: We hit a weak pair whose car has become unreachable.
-                We should remove the pair in question or something.  */
-           }
-         else
-           {
-             if (scm_is_eq (SCM_CDR (handle), variable))
-               return SCM_CAR (handle);
-           }
+          if (scm_is_eq (SCM_CDR (handle), variable))
+            return SCM_CAR (handle);
 
          ls = SCM_CDR (ls);
        }
@@ -1008,7 +859,7 @@ SCM_SYMBOL (scm_sym_system_module, "system-module");
 void
 scm_modules_prehistory ()
 {
-  scm_pre_modules_obarray = scm_c_make_hash_table (1533);
+  scm_pre_modules_obarray = scm_c_make_hash_table (1790);
 }
 
 void
@@ -1017,9 +868,6 @@ scm_init_modules ()
 #include "libguile/modules.x"
   module_make_local_var_x_var = scm_c_define ("module-make-local-var!",
                                            SCM_UNDEFINED);
-  scm_tc16_eval_closure = scm_make_smob_type ("eval-closure", 0);
-  scm_set_smob_apply (scm_tc16_eval_closure, scm_eval_closure_lookup, 2, 0, 0);
-
   the_module = scm_make_fluid ();
 }