1 /* Copyright (C) 1998,2000,2001,2002,2003,2004,2006,2007 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libguile/_scm.h"
25 #include "libguile/eval.h"
26 #include "libguile/smob.h"
27 #include "libguile/procprop.h"
28 #include "libguile/vectors.h"
29 #include "libguile/hashtab.h"
30 #include "libguile/struct.h"
31 #include "libguile/variable.h"
32 #include "libguile/fluids.h"
33 #include "libguile/deprecation.h"
35 #include "libguile/modules.h"
37 int scm_module_system_booted_p
= 0;
39 scm_t_bits scm_module_tag
;
41 static SCM the_module
;
43 SCM_DEFINE (scm_current_module
, "current-module", 0, 0, 0,
45 "Return the current module.")
46 #define FUNC_NAME s_scm_current_module
48 return scm_fluid_ref (the_module
);
52 static void scm_post_boot_init_modules (void);
54 SCM_DEFINE (scm_set_current_module
, "set-current-module", 1, 0, 0,
56 "Set the current module to @var{module} and return\n"
57 "the previous current module.")
58 #define FUNC_NAME s_scm_set_current_module
62 if (!scm_module_system_booted_p
)
63 scm_post_boot_init_modules ();
65 SCM_VALIDATE_MODULE (SCM_ARG1
, module
);
67 old
= scm_current_module ();
68 scm_fluid_set_x (the_module
, module
);
74 SCM_DEFINE (scm_interaction_environment
, "interaction-environment", 0, 0, 0,
76 "Return a specifier for the environment that contains\n"
77 "implementation--defined bindings, typically a superset of those\n"
78 "listed in the report. The intent is that this procedure will\n"
79 "return the environment in which the implementation would\n"
80 "evaluate expressions dynamically typed by the user.")
81 #define FUNC_NAME s_scm_interaction_environment
83 return scm_current_module ();
88 scm_c_call_with_current_module (SCM module
,
89 SCM (*func
)(void *), void *data
)
91 return scm_c_with_fluid (the_module
, module
, func
, data
);
95 scm_dynwind_current_module (SCM module
)
97 scm_dynwind_fluid (the_module
, module
);
101 convert "A B C" to scheme list (A B C)
104 convert_module_name (const char *name
)
115 while (*ptr
&& *ptr
!= ' ')
119 SCM sym
= scm_from_locale_symboln (name
, ptr
-name
);
120 *tail
= scm_cons (sym
, SCM_EOL
);
121 tail
= SCM_CDRLOC (*tail
);
129 static SCM process_define_module_var
;
130 static SCM process_use_modules_var
;
131 static SCM resolve_module_var
;
134 scm_c_resolve_module (const char *name
)
136 return scm_resolve_module (convert_module_name (name
));
140 scm_resolve_module (SCM name
)
142 return scm_call_1 (SCM_VARIABLE_REF (resolve_module_var
), name
);
146 scm_c_define_module (const char *name
,
147 void (*init
)(void *), void *data
)
149 SCM module
= scm_call_1 (SCM_VARIABLE_REF (process_define_module_var
),
150 scm_list_1 (convert_module_name (name
)));
152 scm_c_call_with_current_module (module
, (SCM (*)(void*))init
, data
);
157 scm_c_use_module (const char *name
)
159 scm_call_1 (SCM_VARIABLE_REF (process_use_modules_var
),
160 scm_list_1 (scm_list_1 (convert_module_name (name
))));
163 static SCM module_export_x_var
;
166 scm_module_export (SCM module
, SCM namelist
)
168 return scm_call_2 (SCM_VARIABLE_REF (module_export_x_var
),
174 @code{scm_c_export}(@var{name-list})
176 @code{scm_c_export} exports the named bindings from the current
177 module, making them visible to users of the module. This function
178 takes a list of string arguments, terminated by NULL, e.g.
181 scm_c_export ("add-double-record", "bamboozle-money", NULL);
185 scm_c_export (const char *name
, ...)
190 SCM names
= scm_cons (scm_from_locale_symbol (name
), SCM_EOL
);
191 SCM
*tail
= SCM_CDRLOC (names
);
195 const char *n
= va_arg (ap
, const char *);
198 *tail
= scm_cons (scm_from_locale_symbol (n
), SCM_EOL
);
199 tail
= SCM_CDRLOC (*tail
);
202 scm_module_export (scm_current_module (), names
);
210 scm_top_level_env (SCM thunk
)
215 return scm_cons (thunk
, SCM_EOL
);
219 scm_env_top_level (SCM env
)
221 while (scm_is_pair (env
))
223 SCM car_env
= SCM_CAR (env
);
224 if (!scm_is_pair (car_env
) && scm_is_true (scm_procedure_p (car_env
)))
231 SCM_SYMBOL (sym_module
, "module");
233 static SCM the_root_module_var
;
238 if (scm_module_system_booted_p
)
239 return SCM_VARIABLE_REF (the_root_module_var
);
245 scm_lookup_closure_module (SCM proc
)
247 if (scm_is_false (proc
))
248 return the_root_module ();
249 else if (SCM_EVAL_CLOSURE_P (proc
))
250 return SCM_PACK (SCM_SMOB_DATA (proc
));
253 SCM mod
= scm_procedure_property (proc
, sym_module
);
254 if (scm_is_false (mod
))
255 mod
= the_root_module ();
260 SCM_DEFINE (scm_env_module
, "env-module", 1, 0, 0,
262 "Return the module of @var{ENV}, a lexical environment.")
263 #define FUNC_NAME s_scm_env_module
265 return scm_lookup_closure_module (scm_env_top_level (env
));
270 * C level implementation of the standard eval closure
272 * This increases loading speed substantially. The code may be
273 * replaced by something based on environments.[ch], in a future
277 /* The `module-make-local-var!' variable. */
278 static SCM module_make_local_var_x_var
= SCM_UNSPECIFIED
;
280 /* The `default-duplicate-binding-procedures' variable. */
281 static SCM default_duplicate_binding_procedures_var
= SCM_UNSPECIFIED
;
283 /* Return the list of default duplicate binding handlers (procedures). */
285 default_duplicate_binding_handlers (void)
289 get_handlers
= SCM_VARIABLE_REF (default_duplicate_binding_procedures_var
);
291 return (scm_call_0 (get_handlers
));
294 /* Resolve the import of SYM in MODULE, where SYM is currently provided by
295 both IFACE1 as VAR1 and IFACE2 as VAR2. Return the variable chosen by the
296 duplicate binding handlers or `#f'. */
298 resolve_duplicate_binding (SCM module
, SCM sym
,
299 SCM iface1
, SCM var1
,
300 SCM iface2
, SCM var2
)
302 SCM result
= SCM_BOOL_F
;
304 if (!scm_is_eq (var1
, var2
))
307 SCM handlers
, h
, handler_args
;
309 val1
= SCM_VARIABLE_REF (var1
);
310 val2
= SCM_VARIABLE_REF (var2
);
312 val1
= (val1
== SCM_UNSPECIFIED
) ? SCM_BOOL_F
: val1
;
313 val2
= (val2
== SCM_UNSPECIFIED
) ? SCM_BOOL_F
: val2
;
315 handlers
= SCM_MODULE_DUPLICATE_HANDLERS (module
);
316 if (scm_is_false (handlers
))
317 handlers
= default_duplicate_binding_handlers ();
319 handler_args
= scm_list_n (module
, sym
,
320 iface1
, val1
, iface2
, val2
,
325 scm_is_pair (h
) && scm_is_false (result
);
328 result
= scm_apply (SCM_CAR (h
), handler_args
, SCM_EOL
);
337 /* Lookup SYM as an imported variable of MODULE. */
339 module_imported_variable (SCM module
, SCM sym
)
341 #define SCM_BOUND_THING_P scm_is_true
342 register SCM var
, imports
;
344 /* Search cached imported bindings. */
345 imports
= SCM_MODULE_IMPORT_OBARRAY (module
);
346 var
= scm_hashq_ref (imports
, sym
, SCM_UNDEFINED
);
347 if (SCM_BOUND_THING_P (var
))
351 /* Search the use list for yet uncached imported bindings, possibly
352 resolving duplicates as needed and caching the result in the import
355 SCM found_var
= SCM_BOOL_F
, found_iface
= SCM_BOOL_F
;
357 for (uses
= SCM_MODULE_USES (module
);
359 uses
= SCM_CDR (uses
))
363 iface
= SCM_CAR (uses
);
364 var
= scm_module_variable (iface
, sym
);
366 if (SCM_BOUND_THING_P (var
))
368 if (SCM_BOUND_THING_P (found_var
))
370 /* SYM is a duplicate binding (imported more than once) so we
371 need to resolve it. */
372 found_var
= resolve_duplicate_binding (module
, sym
,
373 found_iface
, found_var
,
375 if (scm_is_eq (found_var
, var
))
379 /* Keep track of the variable we found and check for other
380 occurences of SYM in the use list. */
381 found_var
= var
, found_iface
= iface
;
385 if (SCM_BOUND_THING_P (found_var
))
387 /* Save the lookup result for future reference. */
388 (void) scm_hashq_set_x (imports
, sym
, found_var
);
394 #undef SCM_BOUND_THING_P
397 SCM_DEFINE (scm_module_local_variable
, "module-local-variable", 2, 0, 0,
398 (SCM module
, SCM sym
),
399 "Return the variable bound to @var{sym} in @var{module}. Return "
400 "@code{#f} is @var{sym} is not bound locally in @var{module}.")
401 #define FUNC_NAME s_scm_module_local_variable
403 #define SCM_BOUND_THING_P(b) \
408 /* SCM_MODULE_TAG is not initialized yet when `boot-9.scm' is being
410 if (scm_module_system_booted_p
)
411 SCM_VALIDATE_MODULE (1, module
);
413 SCM_VALIDATE_SYMBOL (2, sym
);
416 /* 1. Check module obarray */
417 b
= scm_hashq_ref (SCM_MODULE_OBARRAY (module
), sym
, SCM_UNDEFINED
);
418 if (SCM_BOUND_THING_P (b
))
421 /* 2. Search imported bindings. In order to be consistent with
422 `module-variable', the binder gets called only when no imported binding
424 b
= module_imported_variable (module
, sym
);
425 if (SCM_BOUND_THING_P (b
))
429 /* 3. Query the custom binder. */
430 SCM binder
= SCM_MODULE_BINDER (module
);
432 if (scm_is_true (binder
))
434 b
= scm_call_3 (binder
, module
, sym
, SCM_BOOL_F
);
435 if (SCM_BOUND_THING_P (b
))
442 #undef SCM_BOUND_THING_P
446 SCM_DEFINE (scm_module_variable
, "module-variable", 2, 0, 0,
447 (SCM module
, SCM sym
),
448 "Return the variable bound to @var{sym} in @var{module}. This "
449 "may be both a local variable or an imported variable. Return "
450 "@code{#f} is @var{sym} is not bound in @var{module}.")
451 #define FUNC_NAME s_scm_module_variable
453 #define SCM_BOUND_THING_P(b) \
458 if (scm_module_system_booted_p
)
459 SCM_VALIDATE_MODULE (1, module
);
461 SCM_VALIDATE_SYMBOL (2, sym
);
463 /* 1. Check module obarray */
464 var
= scm_hashq_ref (SCM_MODULE_OBARRAY (module
), sym
, SCM_UNDEFINED
);
465 if (SCM_BOUND_THING_P (var
))
468 /* 2. Search among the imported variables. */
469 var
= module_imported_variable (module
, sym
);
470 if (SCM_BOUND_THING_P (var
))
474 /* 3. Query the custom binder. */
477 binder
= SCM_MODULE_BINDER (module
);
478 if (scm_is_true (binder
))
480 var
= scm_call_3 (binder
, module
, sym
, SCM_BOOL_F
);
481 if (SCM_BOUND_THING_P (var
))
488 #undef SCM_BOUND_THING_P
492 scm_t_bits scm_tc16_eval_closure
;
494 #define SCM_F_EVAL_CLOSURE_INTERFACE (1<<16)
495 #define SCM_EVAL_CLOSURE_INTERFACE_P(e) \
496 (SCM_CELL_WORD_0 (e) & SCM_F_EVAL_CLOSURE_INTERFACE)
498 /* NOTE: This function may be called by a smob application
499 or from another C function directly. */
501 scm_eval_closure_lookup (SCM eclo
, SCM sym
, SCM definep
)
503 SCM module
= SCM_PACK (SCM_SMOB_DATA (eclo
));
504 if (scm_is_true (definep
))
506 if (SCM_EVAL_CLOSURE_INTERFACE_P (eclo
))
508 return scm_call_2 (SCM_VARIABLE_REF (module_make_local_var_x_var
),
512 return scm_module_variable (module
, sym
);
515 SCM_DEFINE (scm_standard_eval_closure
, "standard-eval-closure", 1, 0, 0,
517 "Return an eval closure for the module @var{module}.")
518 #define FUNC_NAME s_scm_standard_eval_closure
520 SCM_RETURN_NEWSMOB (scm_tc16_eval_closure
, SCM_UNPACK (module
));
525 SCM_DEFINE (scm_standard_interface_eval_closure
,
526 "standard-interface-eval-closure", 1, 0, 0,
528 "Return a interface eval closure for the module @var{module}. "
529 "Such a closure does not allow new bindings to be added.")
530 #define FUNC_NAME s_scm_standard_interface_eval_closure
532 SCM_RETURN_NEWSMOB (scm_tc16_eval_closure
| SCM_F_EVAL_CLOSURE_INTERFACE
,
533 SCM_UNPACK (module
));
538 scm_module_lookup_closure (SCM module
)
540 if (scm_is_false (module
))
543 return SCM_MODULE_EVAL_CLOSURE (module
);
547 scm_current_module_lookup_closure ()
549 if (scm_module_system_booted_p
)
550 return scm_module_lookup_closure (scm_current_module ());
556 scm_module_transformer (SCM module
)
558 if (scm_is_false (module
))
561 return SCM_MODULE_TRANSFORMER (module
);
565 scm_current_module_transformer ()
567 if (scm_module_system_booted_p
)
568 return scm_module_transformer (scm_current_module ());
573 SCM_DEFINE (scm_module_import_interface
, "module-import-interface", 2, 0, 0,
574 (SCM module
, SCM sym
),
575 "Return the module or interface from which @var{sym} is imported "
576 "in @var{module}. If @var{sym} is not imported (i.e., it is not "
577 "defined in @var{module} or it is a module-local binding instead "
578 "of an imported one), then @code{#f} is returned.")
579 #define FUNC_NAME s_scm_module_import_interface
581 SCM var
, result
= SCM_BOOL_F
;
583 SCM_VALIDATE_MODULE (1, module
);
584 SCM_VALIDATE_SYMBOL (2, sym
);
586 var
= scm_module_variable (module
, sym
);
587 if (scm_is_true (var
))
589 /* Look for the module that provides VAR. */
592 local_var
= scm_hashq_ref (SCM_MODULE_OBARRAY (module
), sym
,
594 if (scm_is_eq (local_var
, var
))
598 /* Look for VAR among the used modules. */
599 SCM uses
, imported_var
;
601 for (uses
= SCM_MODULE_USES (module
);
602 scm_is_pair (uses
) && scm_is_false (result
);
603 uses
= SCM_CDR (uses
))
605 imported_var
= scm_module_variable (SCM_CAR (uses
), sym
);
606 if (scm_is_eq (imported_var
, var
))
607 result
= SCM_CAR (uses
);
618 * looks up the variable bound to SYM according to PROC. PROC should be
619 * a `eval closure' of some module.
621 * When no binding exists, and DEFINEP is true, create a new binding
622 * with a initial value of SCM_UNDEFINED. Return `#f' when DEFINEP as
623 * false and no binding exists.
625 * When PROC is `#f', it is ignored and the binding is searched for in
626 * the scm_pre_modules_obarray (a `eq' hash table).
629 SCM scm_pre_modules_obarray
;
632 scm_sym2var (SCM sym
, SCM proc
, SCM definep
)
633 #define FUNC_NAME "scm_sym2var"
639 if (SCM_EVAL_CLOSURE_P (proc
))
641 /* Bypass evaluator in the standard case. */
642 var
= scm_eval_closure_lookup (proc
, sym
, definep
);
645 var
= scm_call_2 (proc
, sym
, definep
);
651 if (scm_is_false (definep
))
652 var
= scm_hashq_ref (scm_pre_modules_obarray
, sym
, SCM_BOOL_F
);
655 handle
= scm_hashq_create_handle_x (scm_pre_modules_obarray
,
657 var
= SCM_CDR (handle
);
658 if (scm_is_false (var
))
660 var
= scm_make_variable (SCM_UNDEFINED
);
661 SCM_SETCDR (handle
, var
);
666 if (scm_is_true (var
) && !SCM_VARIABLEP (var
))
667 SCM_MISC_ERROR ("~S is not bound to a variable", scm_list_1 (sym
));
674 scm_c_module_lookup (SCM module
, const char *name
)
676 return scm_module_lookup (module
, scm_from_locale_symbol (name
));
680 scm_module_lookup (SCM module
, SCM sym
)
681 #define FUNC_NAME "module-lookup"
684 SCM_VALIDATE_MODULE (1, module
);
686 var
= scm_sym2var (sym
, scm_module_lookup_closure (module
), SCM_BOOL_F
);
687 if (scm_is_false (var
))
688 SCM_MISC_ERROR ("unbound variable: ~S", scm_list_1 (sym
));
694 scm_c_lookup (const char *name
)
696 return scm_lookup (scm_from_locale_symbol (name
));
703 scm_sym2var (sym
, scm_current_module_lookup_closure (), SCM_BOOL_F
);
704 if (scm_is_false (var
))
705 scm_misc_error ("scm_lookup", "unbound variable: ~S", scm_list_1 (sym
));
710 scm_c_module_define (SCM module
, const char *name
, SCM value
)
712 return scm_module_define (module
, scm_from_locale_symbol (name
), value
);
716 scm_module_define (SCM module
, SCM sym
, SCM value
)
717 #define FUNC_NAME "module-define"
720 SCM_VALIDATE_MODULE (1, module
);
722 var
= scm_sym2var (sym
, scm_module_lookup_closure (module
), SCM_BOOL_T
);
723 SCM_VARIABLE_SET (var
, value
);
729 scm_c_define (const char *name
, SCM value
)
731 return scm_define (scm_from_locale_symbol (name
), value
);
735 scm_define (SCM sym
, SCM value
)
738 scm_sym2var (sym
, scm_current_module_lookup_closure (), SCM_BOOL_T
);
739 SCM_VARIABLE_SET (var
, value
);
743 SCM_DEFINE (scm_module_reverse_lookup
, "module-reverse-lookup", 2, 0, 0,
744 (SCM module
, SCM variable
),
745 "Return the symbol under which @var{variable} is bound in "
746 "@var{module} or @var{#f} if @var{variable} is not visible "
747 "from @var{module}. If @var{module} is @code{#f}, then the "
748 "pre-module obarray is used.")
749 #define FUNC_NAME s_scm_module_reverse_lookup
754 if (scm_is_false (module
))
755 obarray
= scm_pre_modules_obarray
;
758 SCM_VALIDATE_MODULE (1, module
);
759 obarray
= SCM_MODULE_OBARRAY (module
);
762 if (!SCM_HASHTABLE_P (obarray
))
765 /* XXX - We do not use scm_hash_fold here to avoid searching the
766 whole obarray. We should have a scm_hash_find procedure. */
768 n
= SCM_HASHTABLE_N_BUCKETS (obarray
);
769 for (i
= 0; i
< n
; ++i
)
771 SCM ls
= SCM_HASHTABLE_BUCKET (obarray
, i
), handle
;
772 while (!scm_is_null (ls
))
774 handle
= SCM_CAR (ls
);
776 if (SCM_CAR (handle
) == SCM_PACK (NULL
))
778 /* FIXME: We hit a weak pair whose car has become unreachable.
779 We should remove the pair in question or something. */
783 if (SCM_CDR (handle
) == variable
)
784 return SCM_CAR (handle
);
791 /* Try the `uses' list. */
793 SCM uses
= SCM_MODULE_USES (module
);
794 while (scm_is_pair (uses
))
796 SCM sym
= scm_module_reverse_lookup (SCM_CAR (uses
), variable
);
797 if (scm_is_true (sym
))
799 uses
= SCM_CDR (uses
);
807 SCM_DEFINE (scm_get_pre_modules_obarray
, "%get-pre-modules-obarray", 0, 0, 0,
809 "Return the obarray that is used for all new bindings before "
810 "the module system is booted. The first call to "
811 "@code{set-current-module} will boot the module system.")
812 #define FUNC_NAME s_scm_get_pre_modules_obarray
814 return scm_pre_modules_obarray
;
818 SCM_SYMBOL (scm_sym_system_module
, "system-module");
821 scm_system_module_env_p (SCM env
)
823 SCM proc
= scm_env_top_level (env
);
824 if (scm_is_false (proc
))
826 return ((scm_is_true (scm_procedure_property (proc
,
827 scm_sym_system_module
)))
833 scm_modules_prehistory ()
835 scm_pre_modules_obarray
836 = scm_permanent_object (scm_c_make_hash_table (1533));
842 #include "libguile/modules.x"
843 module_make_local_var_x_var
= scm_c_define ("module-make-local-var!",
845 scm_tc16_eval_closure
= scm_make_smob_type ("eval-closure", 0);
846 scm_set_smob_apply (scm_tc16_eval_closure
, scm_eval_closure_lookup
, 2, 0, 0);
848 the_module
= scm_permanent_object (scm_make_fluid ());
852 scm_post_boot_init_modules ()
854 #define PERM(x) scm_permanent_object(x)
856 SCM module_type
= SCM_VARIABLE_REF (scm_c_lookup ("module-type"));
857 scm_module_tag
= (SCM_CELL_WORD_1 (module_type
) + scm_tc3_struct
);
859 resolve_module_var
= PERM (scm_c_lookup ("resolve-module"));
860 process_define_module_var
= PERM (scm_c_lookup ("process-define-module"));
861 process_use_modules_var
= PERM (scm_c_lookup ("process-use-modules"));
862 module_export_x_var
= PERM (scm_c_lookup ("module-export!"));
863 the_root_module_var
= PERM (scm_c_lookup ("the-root-module"));
864 default_duplicate_binding_procedures_var
=
865 PERM (scm_c_lookup ("default-duplicate-binding-procedures"));
867 scm_module_system_booted_p
= 1;