* ports.c (scm_port_for_each): Make a snapshot of the port table
[bpt/guile.git] / libguile / modules.c
index f8d5b37..1551d28 100644 (file)
@@ -1,4 +1,4 @@
-/*     Copyright (C) 1998 Free Software Foundation, Inc.
+/*     Copyright (C) 1998, 2000 Free Software Foundation, Inc.
  * 
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 
 \f
 
-#include "_scm.h"
+#include "libguile/_scm.h"
 
-#include "eval.h"
-#include "procprop.h"
+#include "libguile/eval.h"
+#include "libguile/smob.h"
+#include "libguile/procprop.h"
+#include "libguile/vectors.h"
+#include "libguile/hashtab.h"
+#include "libguile/struct.h"
+#include "libguile/variable.h"
+#include "libguile/fluids.h"
 
-#include "modules.h"
+#include "libguile/modules.h"
+
+SCM scm_module_system_booted_p = 0;
+
+SCM scm_module_tag;
 
 static SCM the_root_module;
 static SCM root_module_lookup_closure;
@@ -65,11 +75,16 @@ static SCM the_module;
 SCM
 scm_selected_module ()
 {
-  return SCM_CDR (the_module);
+  return scm_fluid_ref (SCM_CDR (the_module));
 }
 
 static SCM set_current_module;
 
+/* This is the module selected during loading of code.  Currently,
+ * this is the same as (interaction-environment), but need not be in
+ * the future.
+ */
+
 SCM
 scm_select_module (SCM module)
 {
@@ -78,6 +93,19 @@ scm_select_module (SCM module)
   return old;
 }
 
+SCM_DEFINE (scm_interaction_environment, "interaction-environment", 0, 0, 0,
+           (),
+           "This procedure returns a specifier for the environment that contains\n"
+           "implementation-defined bindings, typically a superset of those listed in\n"
+           "the report.  The intent is that this procedure will return the\n"
+           "environment in which the implementation would evaluate expressions\n"
+           "dynamically typed by the user.")
+#define FUNC_NAME s_scm_interaction_environment
+{
+  return scm_selected_module ();
+}
+#undef FUNC_NAME
+
 SCM_SYMBOL (scm_sym_app, "app");
 SCM_SYMBOL (scm_sym_modules, "modules");
 static SCM module_prefix;
@@ -85,7 +113,7 @@ static SCM module_prefix;
 static SCM
 scm_module_full_name (SCM name)
 {
-  if (SCM_CAR (name) == scm_sym_app)
+  if (SCM_EQ_P (SCM_CAR (name), scm_sym_app))
     return name;
   else
     return scm_append (SCM_LIST2 (module_prefix, name));
@@ -110,14 +138,10 @@ scm_ensure_user_module (SCM module)
   return SCM_UNSPECIFIED;
 }
 
-static SCM module_eval_closure;
-
 SCM
 scm_module_lookup_closure (SCM module)
 {
-  return scm_apply (SCM_CDR (module_eval_closure),
-                   SCM_LIST1 (module),
-                   SCM_EOL);
+  return SCM_MODULE_EVAL_CLOSURE (module);
 }
 
 static SCM resolve_module;
@@ -136,8 +160,7 @@ scm_load_scheme_module (SCM name)
   return scm_apply (SCM_CDR (try_module_autoload), SCM_LIST1 (name), SCM_EOL);
 }
 
-/* Environments
- */
+/* Environments */
 
 SCM
 scm_top_level_env (SCM thunk)
@@ -155,7 +178,7 @@ scm_env_top_level (SCM env)
     {
       if (!SCM_CONSP (SCM_CAR (env))
          && SCM_NFALSEP (scm_procedure_p (SCM_CAR (env))))
-       return SCM_CAR(env);
+       return SCM_CAR (env);
       env = SCM_CDR (env);
     }
   return SCM_BOOL_F;
@@ -176,15 +199,91 @@ scm_system_module_env_p (SCM env)
          : SCM_BOOL_F);
 }
 
+/*
+ * C level implementation of the standard eval closure
+ *
+ * This increases loading speed substantially.
+ * The code will be replaced by the low-level environments in next release.
+ */
+
+static SCM module_make_local_var_x;
+
+static SCM
+module_variable (SCM module, SCM sym)
+{
+  /* 1. Check module obarray */
+  SCM b = scm_hashq_ref (SCM_MODULE_OBARRAY (module), sym, SCM_UNDEFINED);
+  if (SCM_VARIABLEP (b))
+    return b;
+  {
+    SCM binder = SCM_MODULE_BINDER (module);
+    if (SCM_NFALSEP (binder))
+      /* 2. Custom binder */
+      {
+       b = scm_apply (binder,
+                      SCM_LIST3 (module, sym, SCM_BOOL_F),
+                      SCM_EOL);
+       if (SCM_NFALSEP (b))
+         return b;
+      }
+  }
+  {
+    /* 3. Search the use list */
+    SCM uses = SCM_MODULE_USES (module);
+    while (SCM_CONSP (uses))
+      {
+       b = module_variable (SCM_CAR (uses), sym);
+       if (SCM_NFALSEP (b))
+         return b;
+       uses = SCM_CDR (uses);
+      }
+    return SCM_BOOL_F;
+  }
+}
+
+scm_bits_t scm_tc16_eval_closure;
+
+/* 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 = SCM_PACK (SCM_SMOB_DATA (eclo));
+  if (SCM_NFALSEP (definep))
+    return scm_apply (SCM_CDR (module_make_local_var_x),
+                     SCM_LIST2 (module, sym),
+                     SCM_EOL);
+  else
+    return module_variable (module, sym);
+}
+
+SCM_DEFINE (scm_standard_eval_closure, "standard-eval-closure", 1, 0, 0,
+           (SCM module),
+           "")
+#define FUNC_NAME s_scm_standard_eval_closure
+{
+  SCM_RETURN_NEWSMOB (scm_tc16_eval_closure, SCM_UNPACK (module));
+}
+#undef FUNC_NAME
+
 void
 scm_init_modules ()
 {
-#include "modules.x"
+#ifndef SCM_MAGIC_SNARFER
+#include "libguile/modules.x"
+#endif
+  module_make_local_var_x = scm_sysintern ("module-make-local-var!",
+                                          SCM_UNDEFINED);
+  scm_tc16_eval_closure = scm_make_smob_type ("eval-closure", 0);
+  scm_set_smob_mark (scm_tc16_eval_closure, scm_markcdr);
+  scm_set_smob_apply (scm_tc16_eval_closure, scm_eval_closure_lookup, 2, 0, 0);
 }
 
 void
 scm_post_boot_init_modules ()
 {
+  scm_module_tag = (SCM_CELL_WORD_1 (SCM_CDR (scm_intern0 ("module-type")))
+                   + scm_tc3_cons_gloc);
   the_root_module = scm_intern0 ("the-root-module");
   the_module = scm_intern0 ("the-module");
   set_current_module = scm_intern0 ("set-current-module");
@@ -192,11 +291,11 @@ scm_post_boot_init_modules ()
                                                   scm_sym_modules));
   make_modules_in = scm_intern0 ("make-modules-in");
   beautify_user_module_x = scm_intern0 ("beautify-user-module!");
-  module_eval_closure = scm_intern0 ("module-eval-closure");
   root_module_lookup_closure = scm_permanent_object
     (scm_module_lookup_closure (SCM_CDR (the_root_module)));
   resolve_module = scm_intern0 ("resolve-module");
   try_module_autoload = scm_intern0 ("try-module-autoload");
+  scm_module_system_booted_p = 1;
 }
 
 /*