*** empty log message ***
[lilypond.git] / lily / ly-module.cc
blobea0e7c67f899625ee4345baf2b42dfb3c5d38b81
1 /*
2 ly-module.cc -- implement guile module stuff.
4 source file of the GNU LilyPond music typesetter
6 (c) 2002--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
10 #include "string.hh"
11 #include "lily-guile.hh"
12 #include "ly-module.hh"
13 #include "protected-scm.hh"
15 #define FUNC_NAME __FUNCTION__
17 static int module_count;
19 void
20 ly_init_anonymous_module (void * data)
22 (void) data;
23 scm_c_use_module ("lily");
26 Protected_scm anon_modules;
28 SCM
29 ly_make_anonymous_module ()
31 String s = "*anonymous-ly-" + to_string (module_count++) + "*";
32 SCM mod = scm_c_define_module (s.to_str0(), ly_init_anonymous_module, 0);
33 anon_modules = scm_cons (mod, anon_modules);
34 return mod;
37 void
38 ly_clear_anonymous_modules ()
40 SCM s = anon_modules;
41 anon_modules = SCM_EOL;
43 for (; gh_pair_p (s) ; s = gh_cdr (s))
45 SCM tab= scm_c_make_hash_table (2);
46 /* UGH. */
47 SCM_STRUCT_DATA (gh_car (s))[scm_module_index_obarray]
48 = (long unsigned int) tab;
52 #define FUNC_NAME __FUNCTION__
54 static SCM
55 ly_module_define (void *closure, SCM key, SCM val, SCM result)
57 (void) result;
58 SCM module = (SCM) closure;
59 scm_module_define (module, key, scm_variable_ref (val));
60 return SCM_EOL;
63 /* Ugh signature of scm_internal_hash_fold () is inaccurate. */
64 typedef SCM (*Hash_cl_func)();
66 void
67 ly_import_module (SCM dest, SCM src)
69 SCM_VALIDATE_MODULE (1, src);
70 scm_internal_hash_fold ((Hash_cl_func) &ly_module_define, (void*) dest,
71 SCM_EOL, SCM_MODULE_OBARRAY (src));
74 static SCM
75 accumulate_symbol (void *closure, SCM key, SCM val, SCM result)
77 (void) closure;
78 (void) val;
79 return scm_cons (key, result);
82 SCM
83 ly_module_symbols (SCM mod)
85 SCM_VALIDATE_MODULE (1, mod);
87 SCM obarr= SCM_MODULE_OBARRAY (mod);
88 return scm_internal_hash_fold ((Hash_cl_func) &accumulate_symbol,
89 NULL, SCM_EOL, obarr);
92 static SCM
93 entry_to_alist (void *closure, SCM key, SCM val, SCM result)
95 (void) closure;
96 return scm_cons (scm_cons (key, scm_variable_ref (val)), result);
99 SCM
100 ly_module_to_alist (SCM mod)
102 SCM_VALIDATE_MODULE (1, mod);
105 SCM obarr= SCM_MODULE_OBARRAY (mod);
107 return scm_internal_hash_fold ((Hash_cl_func) &entry_to_alist, NULL, SCM_EOL, obarr);
110 /* Lookup SYM, but don't give error when it is not defined. */
112 ly_module_lookup (SCM module, SCM sym)
114 #define FUNC_NAME __FUNCTION__
115 SCM_VALIDATE_MODULE (1, module);
117 return scm_sym2var (sym, scm_module_lookup_closure (module), SCM_BOOL_F);
118 #undef FUNC_NAME
122 ly_modules_lookup (SCM modules, SCM sym)
124 for (SCM s = gh_car (modules); SCM_MODULEP (s); s = ly_cdr (s))
126 SCM v = scm_sym2var (sym, scm_module_lookup_closure (s), SCM_UNDEFINED);
127 if (v != SCM_UNDEFINED)
128 return v;
130 return SCM_UNDEFINED;
134 SCM export_function;
136 void
137 ly_export (SCM module, SCM namelist)
139 if (!export_function)
140 export_function = scm_permanent_object (scm_c_lookup ("module-export!"));
142 scm_call_2 (SCM_VARIABLE_REF (export_function), module, namelist);
145 void
146 ly_reexport_module (SCM mod)
148 ly_export (mod, ly_module_symbols (mod));