]> git.donarmstrong.com Git - lilypond.git/blob - lily/module-scheme.cc
* lily/include/ly-module.hh: define MODULE_GC_KLUDGE globally.
[lilypond.git] / lily / module-scheme.cc
1 /*
2   lily/module-scheme.cc -- implement module bindings
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
7
8 */
9
10 #include "lily-guile.hh"
11 #include "warn.hh"
12 #include "main.hh"
13 #include "string.hh"
14
15 #include "ly-module.hh"
16
17 /*
18   If a variable in changed in SRC, we DEST doesn't see the
19   definitions.
20 */
21
22 static SCM
23 module_define_closure_func (void *closure, SCM key, SCM val, SCM result)
24 {
25   (void) result;
26   SCM module = (SCM) closure;
27   if (scm_variable_bound_p (val) == SCM_BOOL_T)
28     scm_module_define (module, key, scm_variable_ref (val));
29   return SCM_EOL;
30 }
31
32 LY_DEFINE (ly_module_copy, "ly:module-copy",
33            2, 0, 0, (SCM dest, SCM src),
34            "Copy all bindings from module SRC into DEST.")
35 {
36 #define FUNC_NAME __FUNCTION__
37   SCM_VALIDATE_MODULE (1, src);
38   scm_internal_hash_fold ((Hash_closure_function) & module_define_closure_func,
39                           (void *) dest,
40                           SCM_EOL, SCM_MODULE_OBARRAY (src));
41   return SCM_UNSPECIFIED;
42 }
43
44 LY_DEFINE(ly_clear_anonymous_modules, "ly:clear-anonymous-modules",
45           0, 0, 0, (),
46           "Plug a GUILE 1.6 and 1.7 memory leak by breaking a weak reference "
47           "pointer cycle explicitly."
48           )
49 {
50 #ifdef MODULE_GC_KLUDGE
51   clear_anonymous_modules();
52 #endif
53
54   return SCM_UNSPECIFIED;
55 }
56
57
58 /* Lookup SYM, but don't give error when it is not defined.  */
59 SCM
60 ly_module_lookup (SCM module, SCM sym)
61 {
62 #define FUNC_NAME __FUNCTION__
63   SCM_VALIDATE_MODULE (1, module);
64
65   return scm_sym2var (sym, scm_module_lookup_closure (module), SCM_BOOL_F);
66 #undef FUNC_NAME
67 }
68
69 /* Lookup SYM in a list of modules, which do not have to be related.
70    Return the first instance. */
71 LY_DEFINE (ly_modules_lookup, "ly:modules-lookup",
72            2, 1, 0,
73            (SCM modules, SCM sym, SCM def),
74            "Lookup @var{sym} in the list @var{modules}, "
75            "returning the first occurence.  "
76            "If not found, return @var{default}, or @code{#f}.")
77 {
78   for (SCM s = modules; scm_is_pair (s); s = scm_cdr (s))
79     {
80       SCM mod = scm_car (s);
81       SCM v = ly_module_lookup (mod, sym);
82       if (SCM_VARIABLEP (v) && SCM_VARIABLE_REF (v) != SCM_UNDEFINED)
83         return scm_variable_ref (v);
84     }
85
86   if (def != SCM_UNDEFINED)
87     return def;
88   return SCM_BOOL_F;
89 }