]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-modules.cc
Issue 4514/1: Pass setup function call into Scm_module::boot
[lilypond.git] / lily / lily-modules.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2015 by David Kastrup <dak@gnu.org>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "lily-modules.hh"
21 #include "lily-imports.hh"
22
23 struct Scm_module::Variable_record
24 {
25   const char *name_;
26   Scm_variable *var_;
27   Variable_record *next_;
28   Variable_record (const char *name, Scm_variable *var, Variable_record *next_)
29     : name_ (name), var_ (var), next_ (next_)
30   { }
31 };
32
33 void
34 Scm_module::register_variable (const char *name, Scm_variable *var)
35 {
36   variables_ = new Variable_record (name, var, variables_);
37 }
38
39 Scm_module::Scm_module (const char *name)
40   : name_ (name), module_ (SCM_UNDEFINED), variables_ (0)
41 {
42 }
43
44 void
45 Scm_module::boot_init (void *arg)
46 {
47   Scm_module *self = static_cast<Scm_module *> (arg);
48
49   // Establish variables first
50   for (Variable_record *p = self->variables_; p;)
51     {
52       Variable_record *next = p->next_;
53       p->var_->boot (p->name_);
54       delete p;
55       p = next;
56     }
57   self->variables_ = 0;
58 }
59
60 static SCM
61 call_trampoline (void *self)
62 {
63   // One more indirection since void * can only be safely cast to
64   // pointers to data rather than pointers to function.
65   (*static_cast <void (**)()> (self)) ();
66   return SCM_UNDEFINED;
67 }
68
69 void
70 Scm_module::boot (void (*init) ())
71 {
72   assert (SCM_UNBNDP (module_));
73   module_ = scm_c_define_module (name_, boot_init, static_cast <void *> (this));
74   // Can't wrap the following in the scm_c_define_module call since
75   // the init code may need module_ operative.
76   if (init)
77     scm_c_call_with_current_module (module_, call_trampoline, static_cast <void *> (&init));
78 }
79
80 void
81 Scm_module::import ()
82 {
83   assert (SCM_UNBNDP (module_));
84   SCM intrface = scm_c_resolve_module (name_);
85   // Using only the public interface is a voluntary form of access
86   // control in GUILE.  It would be cumbersome to do so until
87   // Guile_user itself is imported.
88   if (SCM_MODULEP (Guile_user::module.module_))
89     intrface = Guile_user::module_public_interface (intrface);
90   for (Variable_record *p = variables_; p;)
91     {
92       Variable_record *next = p->next_;
93       p->var_->import (intrface, p->name_);
94       delete p;
95       p = next;
96     }
97   variables_ = 0;
98   module_ = intrface;
99 }
100
101 void
102 Scm_variable::boot (const char *name)
103 {
104   assert (!SCM_VARIABLEP (var_));
105   var_ = scm_c_define (name, var_);
106 }
107
108 void
109 Scm_variable::import (SCM module, const char *name)
110 {
111   assert (SCM_UNBNDP (var_));
112   var_ = scm_c_module_lookup (module, name);
113 }
114
115
116 Scm_variable::Scm_variable (Scm_module &m, const char *name, SCM value)
117   : var_ (value)
118 {
119   assert (SCM_IMP (value));
120   m.register_variable (name, this);
121 }