Commit | Line | Data |
---|---|---|
cc95e00a | 1 | /* Copyright (C) 1998,2000,2001,2002, 2003, 2004 Free Software Foundation, Inc. |
1ffa265b | 2 | * |
73be1d9e MV |
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. | |
1ffa265b | 7 | * |
73be1d9e MV |
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. | |
1ffa265b | 12 | * |
73be1d9e MV |
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 | |
92205699 | 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
73be1d9e | 16 | */ |
6e8d25a6 | 17 | |
6e8d25a6 | 18 | |
1ffa265b MD |
19 | \f |
20 | ||
d02b98e9 MV |
21 | #include <stdarg.h> |
22 | ||
a0599745 | 23 | #include "libguile/_scm.h" |
1ffa265b | 24 | |
a0599745 | 25 | #include "libguile/eval.h" |
fb43bf74 | 26 | #include "libguile/smob.h" |
a0599745 | 27 | #include "libguile/procprop.h" |
152abe96 MD |
28 | #include "libguile/vectors.h" |
29 | #include "libguile/hashtab.h" | |
30 | #include "libguile/struct.h" | |
31 | #include "libguile/variable.h" | |
7f763132 | 32 | #include "libguile/fluids.h" |
d02b98e9 | 33 | #include "libguile/deprecation.h" |
1ffa265b | 34 | |
a0599745 | 35 | #include "libguile/modules.h" |
1ffa265b | 36 | |
86d31dfe | 37 | int scm_module_system_booted_p = 0; |
e3365c07 | 38 | |
92c2555f | 39 | scm_t_bits scm_module_tag; |
e3365c07 | 40 | |
1ffa265b MD |
41 | static SCM the_module; |
42 | ||
55000e5f MV |
43 | SCM_DEFINE (scm_current_module, "current-module", 0, 0, 0, |
44 | (), | |
45 | "Return the current module.") | |
46 | #define FUNC_NAME s_scm_current_module | |
1ffa265b | 47 | { |
55000e5f | 48 | return scm_fluid_ref (the_module); |
1ffa265b | 49 | } |
55000e5f | 50 | #undef FUNC_NAME |
1ffa265b | 51 | |
86d31dfe | 52 | static void scm_post_boot_init_modules (void); |
1ffa265b | 53 | |
55000e5f MV |
54 | SCM_DEFINE (scm_set_current_module, "set-current-module", 1, 0, 0, |
55 | (SCM module), | |
9401323e | 56 | "Set the current module to @var{module} and return\n" |
55000e5f MV |
57 | "the previous current module.") |
58 | #define FUNC_NAME s_scm_set_current_module | |
1ffa265b | 59 | { |
55000e5f MV |
60 | SCM old; |
61 | ||
86d31dfe MV |
62 | if (!scm_module_system_booted_p) |
63 | scm_post_boot_init_modules (); | |
64 | ||
65 | SCM_VALIDATE_MODULE (SCM_ARG1, module); | |
55000e5f MV |
66 | |
67 | old = scm_current_module (); | |
68 | scm_fluid_set_x (the_module, module); | |
69 | ||
1ffa265b MD |
70 | return old; |
71 | } | |
55000e5f | 72 | #undef FUNC_NAME |
1ffa265b | 73 | |
e3365c07 MD |
74 | SCM_DEFINE (scm_interaction_environment, "interaction-environment", 0, 0, 0, |
75 | (), | |
1e6808ea MG |
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.") | |
e3365c07 MD |
81 | #define FUNC_NAME s_scm_interaction_environment |
82 | { | |
aa767bc5 | 83 | return scm_current_module (); |
e3365c07 MD |
84 | } |
85 | #undef FUNC_NAME | |
86 | ||
1ffa265b | 87 | SCM |
d02b98e9 MV |
88 | scm_c_call_with_current_module (SCM module, |
89 | SCM (*func)(void *), void *data) | |
1ffa265b | 90 | { |
d02b98e9 | 91 | return scm_c_with_fluid (the_module, module, func, data); |
1ffa265b MD |
92 | } |
93 | ||
cb1cfc42 MV |
94 | void |
95 | scm_frame_current_module (SCM module) | |
96 | { | |
97 | scm_frame_fluid (the_module, module); | |
98 | } | |
06e80f59 HWN |
99 | |
100 | /* | |
101 | convert "A B C" to scheme list (A B C) | |
102 | */ | |
d02b98e9 MV |
103 | static SCM |
104 | convert_module_name (const char *name) | |
281004cc | 105 | { |
d02b98e9 MV |
106 | SCM list = SCM_EOL; |
107 | SCM *tail = &list; | |
281004cc | 108 | |
d02b98e9 MV |
109 | const char *ptr; |
110 | while (*name) | |
111 | { | |
112 | while (*name == ' ') | |
113 | name++; | |
114 | ptr = name; | |
115 | while (*ptr && *ptr != ' ') | |
116 | ptr++; | |
117 | if (ptr > name) | |
118 | { | |
cc95e00a MV |
119 | SCM sym = scm_from_locale_symboln (name, ptr-name); |
120 | *tail = scm_cons (sym, SCM_EOL); | |
d02b98e9 MV |
121 | tail = SCM_CDRLOC (*tail); |
122 | } | |
123 | name = ptr; | |
124 | } | |
125 | ||
126 | return list; | |
1ffa265b MD |
127 | } |
128 | ||
d02b98e9 MV |
129 | static SCM process_define_module_var; |
130 | static SCM process_use_modules_var; | |
131 | static SCM resolve_module_var; | |
132 | ||
9e57344b | 133 | SCM |
d02b98e9 | 134 | scm_c_resolve_module (const char *name) |
9e57344b | 135 | { |
d02b98e9 | 136 | return scm_resolve_module (convert_module_name (name)); |
9e57344b MV |
137 | } |
138 | ||
55000e5f | 139 | SCM |
d02b98e9 | 140 | scm_resolve_module (SCM name) |
55000e5f | 141 | { |
fdc28395 | 142 | return scm_call_1 (SCM_VARIABLE_REF (resolve_module_var), name); |
55000e5f MV |
143 | } |
144 | ||
145 | SCM | |
d02b98e9 MV |
146 | scm_c_define_module (const char *name, |
147 | void (*init)(void *), void *data) | |
55000e5f | 148 | { |
fdc28395 | 149 | SCM module = scm_call_1 (SCM_VARIABLE_REF (process_define_module_var), |
1afff620 | 150 | scm_list_1 (convert_module_name (name))); |
d02b98e9 MV |
151 | if (init) |
152 | scm_c_call_with_current_module (module, (SCM (*)(void*))init, data); | |
153 | return module; | |
55000e5f MV |
154 | } |
155 | ||
d02b98e9 MV |
156 | void |
157 | scm_c_use_module (const char *name) | |
90184345 | 158 | { |
fdc28395 | 159 | scm_call_1 (SCM_VARIABLE_REF (process_use_modules_var), |
b64f4200 | 160 | scm_list_1 (scm_list_1 (convert_module_name (name)))); |
90184345 MD |
161 | } |
162 | ||
d02b98e9 | 163 | static SCM module_export_x_var; |
281004cc | 164 | |
eb880cef | 165 | |
06e80f59 HWN |
166 | /* |
167 | TODO: should export this function? --hwn. | |
168 | */ | |
169 | static SCM | |
170 | scm_export (SCM module, SCM namelist) | |
171 | { | |
8c330007 MD |
172 | return scm_call_2 (SCM_VARIABLE_REF (module_export_x_var), |
173 | module, namelist); | |
06e80f59 HWN |
174 | } |
175 | ||
eb880cef MV |
176 | |
177 | /* | |
178 | @code{scm_c_export}(@var{name-list}) | |
179 | ||
180 | @code{scm_c_export} exports the named bindings from the current | |
181 | module, making them visible to users of the module. This function | |
182 | takes a list of string arguments, terminated by NULL, e.g. | |
183 | ||
184 | @example | |
185 | scm_c_export ("add-double-record", "bamboozle-money", NULL); | |
186 | @end example | |
187 | */ | |
d02b98e9 MV |
188 | void |
189 | scm_c_export (const char *name, ...) | |
281004cc | 190 | { |
eb880cef | 191 | if (name) |
d02b98e9 | 192 | { |
eb880cef | 193 | va_list ap; |
cc95e00a | 194 | SCM names = scm_cons (scm_from_locale_symbol (name), SCM_EOL); |
eb880cef MV |
195 | SCM *tail = SCM_CDRLOC (names); |
196 | va_start (ap, name); | |
197 | while (1) | |
198 | { | |
199 | const char *n = va_arg (ap, const char *); | |
200 | if (n == NULL) | |
201 | break; | |
cc95e00a | 202 | *tail = scm_cons (scm_from_locale_symbol (n), SCM_EOL); |
eb880cef MV |
203 | tail = SCM_CDRLOC (*tail); |
204 | } | |
205 | va_end (ap); | |
06e80f59 | 206 | scm_export (scm_current_module(), names); |
d02b98e9 | 207 | } |
281004cc MD |
208 | } |
209 | ||
06e80f59 | 210 | |
e3365c07 | 211 | /* Environments */ |
d164a5af MD |
212 | |
213 | SCM | |
6e8d25a6 | 214 | scm_top_level_env (SCM thunk) |
d164a5af MD |
215 | { |
216 | if (SCM_IMP (thunk)) | |
217 | return SCM_EOL; | |
218 | else | |
219 | return scm_cons (thunk, SCM_EOL); | |
220 | } | |
221 | ||
222 | SCM | |
223 | scm_env_top_level (SCM env) | |
224 | { | |
d2e53ed6 | 225 | while (scm_is_pair (env)) |
d164a5af | 226 | { |
c88b1456 | 227 | SCM car_env = SCM_CAR (env); |
d2e53ed6 | 228 | if (!scm_is_pair (car_env) && scm_is_true (scm_procedure_p (car_env))) |
c88b1456 | 229 | return car_env; |
d164a5af MD |
230 | env = SCM_CDR (env); |
231 | } | |
232 | return SCM_BOOL_F; | |
233 | } | |
234 | ||
86d31dfe MV |
235 | SCM_SYMBOL (sym_module, "module"); |
236 | ||
d02b98e9 MV |
237 | static SCM the_root_module_var; |
238 | ||
239 | static SCM | |
240 | the_root_module () | |
241 | { | |
242 | if (scm_module_system_booted_p) | |
243 | return SCM_VARIABLE_REF (the_root_module_var); | |
244 | else | |
245 | return SCM_BOOL_F; | |
246 | } | |
247 | ||
86d31dfe MV |
248 | SCM |
249 | scm_lookup_closure_module (SCM proc) | |
250 | { | |
7888309b | 251 | if (scm_is_false (proc)) |
d02b98e9 | 252 | return the_root_module (); |
86d31dfe MV |
253 | else if (SCM_EVAL_CLOSURE_P (proc)) |
254 | return SCM_PACK (SCM_SMOB_DATA (proc)); | |
255 | else | |
256 | { | |
257 | SCM mod = scm_procedure_property (proc, sym_module); | |
7888309b | 258 | if (scm_is_false (mod)) |
d02b98e9 | 259 | mod = the_root_module (); |
86d31dfe MV |
260 | return mod; |
261 | } | |
262 | } | |
263 | ||
e24ca538 MV |
264 | SCM_DEFINE (scm_env_module, "env-module", 1, 0, 0, |
265 | (SCM env), | |
266 | "Return the module of @var{ENV}, a lexical environment.") | |
267 | #define FUNC_NAME s_scm_env_module | |
86d31dfe MV |
268 | { |
269 | return scm_lookup_closure_module (scm_env_top_level (env)); | |
270 | } | |
e24ca538 | 271 | #undef FUNC_NAME |
86d31dfe | 272 | |
152abe96 MD |
273 | /* |
274 | * C level implementation of the standard eval closure | |
275 | * | |
276 | * This increases loading speed substantially. | |
277 | * The code will be replaced by the low-level environments in next release. | |
278 | */ | |
279 | ||
86d31dfe | 280 | static SCM module_make_local_var_x_var; |
152abe96 MD |
281 | |
282 | static SCM | |
283 | module_variable (SCM module, SCM sym) | |
284 | { | |
dc187f33 | 285 | #define SCM_BOUND_THING_P(b) \ |
7888309b | 286 | (scm_is_true (b)) |
dc187f33 | 287 | |
152abe96 | 288 | /* 1. Check module obarray */ |
e3365c07 | 289 | SCM b = scm_hashq_ref (SCM_MODULE_OBARRAY (module), sym, SCM_UNDEFINED); |
dc187f33 | 290 | if (SCM_BOUND_THING_P (b)) |
152abe96 MD |
291 | return b; |
292 | { | |
e3365c07 | 293 | SCM binder = SCM_MODULE_BINDER (module); |
7888309b | 294 | if (scm_is_true (binder)) |
152abe96 MD |
295 | /* 2. Custom binder */ |
296 | { | |
fdc28395 | 297 | b = scm_call_3 (binder, module, sym, SCM_BOOL_F); |
dc187f33 | 298 | if (SCM_BOUND_THING_P (b)) |
152abe96 MD |
299 | return b; |
300 | } | |
301 | } | |
302 | { | |
303 | /* 3. Search the use list */ | |
e3365c07 | 304 | SCM uses = SCM_MODULE_USES (module); |
d2e53ed6 | 305 | while (scm_is_pair (uses)) |
152abe96 MD |
306 | { |
307 | b = module_variable (SCM_CAR (uses), sym); | |
dc187f33 | 308 | if (SCM_BOUND_THING_P (b)) |
152abe96 MD |
309 | return b; |
310 | uses = SCM_CDR (uses); | |
311 | } | |
312 | return SCM_BOOL_F; | |
313 | } | |
dc187f33 | 314 | #undef SCM_BOUND_THING_P |
152abe96 MD |
315 | } |
316 | ||
92c2555f | 317 | scm_t_bits scm_tc16_eval_closure; |
152abe96 | 318 | |
86d31dfe MV |
319 | #define SCM_F_EVAL_CLOSURE_INTERFACE (1<<16) |
320 | #define SCM_EVAL_CLOSURE_INTERFACE_P(e) \ | |
321 | (SCM_CELL_WORD_0 (e) & SCM_F_EVAL_CLOSURE_INTERFACE) | |
322 | ||
fb43bf74 KN |
323 | /* NOTE: This function may be called by a smob application |
324 | or from another C function directly. */ | |
325 | SCM | |
326 | scm_eval_closure_lookup (SCM eclo, SCM sym, SCM definep) | |
152abe96 | 327 | { |
fb43bf74 | 328 | SCM module = SCM_PACK (SCM_SMOB_DATA (eclo)); |
7888309b | 329 | if (scm_is_true (definep)) |
86d31dfe MV |
330 | { |
331 | if (SCM_EVAL_CLOSURE_INTERFACE_P (eclo)) | |
332 | return SCM_BOOL_F; | |
fdc28395 KN |
333 | return scm_call_2 (SCM_VARIABLE_REF (module_make_local_var_x_var), |
334 | module, sym); | |
86d31dfe | 335 | } |
152abe96 MD |
336 | else |
337 | return module_variable (module, sym); | |
338 | } | |
339 | ||
340 | SCM_DEFINE (scm_standard_eval_closure, "standard-eval-closure", 1, 0, 0, | |
341 | (SCM module), | |
84526793 | 342 | "Return an eval closure for the module @var{module}.") |
152abe96 MD |
343 | #define FUNC_NAME s_scm_standard_eval_closure |
344 | { | |
e841c3e0 | 345 | SCM_RETURN_NEWSMOB (scm_tc16_eval_closure, SCM_UNPACK (module)); |
152abe96 MD |
346 | } |
347 | #undef FUNC_NAME | |
348 | ||
86d31dfe MV |
349 | SCM_DEFINE (scm_standard_interface_eval_closure, |
350 | "standard-interface-eval-closure", 1, 0, 0, | |
351 | (SCM module), | |
352 | "Return a interface eval closure for the module @var{module}. " | |
353 | "Such a closure does not allow new bindings to be added.") | |
354 | #define FUNC_NAME s_scm_standard_interface_eval_closure | |
355 | { | |
356 | SCM_RETURN_NEWSMOB (scm_tc16_eval_closure | SCM_F_EVAL_CLOSURE_INTERFACE, | |
357 | SCM_UNPACK (module)); | |
358 | } | |
359 | #undef FUNC_NAME | |
360 | ||
d02b98e9 MV |
361 | SCM |
362 | scm_module_lookup_closure (SCM module) | |
363 | { | |
7888309b | 364 | if (scm_is_false (module)) |
d02b98e9 MV |
365 | return SCM_BOOL_F; |
366 | else | |
367 | return SCM_MODULE_EVAL_CLOSURE (module); | |
368 | } | |
369 | ||
370 | SCM | |
371 | scm_current_module_lookup_closure () | |
372 | { | |
373 | if (scm_module_system_booted_p) | |
374 | return scm_module_lookup_closure (scm_current_module ()); | |
375 | else | |
376 | return SCM_BOOL_F; | |
377 | } | |
378 | ||
379 | SCM | |
380 | scm_module_transformer (SCM module) | |
381 | { | |
7888309b | 382 | if (scm_is_false (module)) |
d02b98e9 MV |
383 | return SCM_BOOL_F; |
384 | else | |
385 | return SCM_MODULE_TRANSFORMER (module); | |
386 | } | |
387 | ||
388 | SCM | |
389 | scm_current_module_transformer () | |
390 | { | |
391 | if (scm_module_system_booted_p) | |
392 | return scm_module_transformer (scm_current_module ()); | |
393 | else | |
394 | return SCM_BOOL_F; | |
395 | } | |
396 | ||
109c2c9f MD |
397 | SCM_DEFINE (scm_module_import_interface, "module-import-interface", 2, 0, 0, |
398 | (SCM module, SCM sym), | |
399 | "") | |
400 | #define FUNC_NAME s_scm_module_import_interface | |
401 | { | |
7888309b | 402 | #define SCM_BOUND_THING_P(b) (scm_is_true (b)) |
d583ce1a | 403 | SCM uses; |
109c2c9f MD |
404 | SCM_VALIDATE_MODULE (SCM_ARG1, module); |
405 | /* Search the use list */ | |
d583ce1a | 406 | uses = SCM_MODULE_USES (module); |
d2e53ed6 | 407 | while (scm_is_pair (uses)) |
109c2c9f | 408 | { |
2e945bcc | 409 | SCM _interface = SCM_CAR (uses); |
109c2c9f | 410 | /* 1. Check module obarray */ |
2e945bcc | 411 | SCM b = scm_hashq_ref (SCM_MODULE_OBARRAY (_interface), sym, SCM_BOOL_F); |
109c2c9f | 412 | if (SCM_BOUND_THING_P (b)) |
2e945bcc | 413 | return _interface; |
109c2c9f | 414 | { |
2e945bcc | 415 | SCM binder = SCM_MODULE_BINDER (_interface); |
7888309b | 416 | if (scm_is_true (binder)) |
109c2c9f MD |
417 | /* 2. Custom binder */ |
418 | { | |
2e945bcc | 419 | b = scm_call_3 (binder, _interface, sym, SCM_BOOL_F); |
109c2c9f | 420 | if (SCM_BOUND_THING_P (b)) |
2e945bcc | 421 | return _interface; |
109c2c9f MD |
422 | } |
423 | } | |
424 | /* 3. Search use list recursively. */ | |
2e945bcc | 425 | _interface = scm_module_import_interface (_interface, sym); |
7888309b | 426 | if (scm_is_true (_interface)) |
2e945bcc | 427 | return _interface; |
109c2c9f MD |
428 | uses = SCM_CDR (uses); |
429 | } | |
430 | return SCM_BOOL_F; | |
431 | } | |
432 | #undef FUNC_NAME | |
433 | ||
86d31dfe MV |
434 | /* scm_sym2var |
435 | * | |
436 | * looks up the variable bound to SYM according to PROC. PROC should be | |
437 | * a `eval closure' of some module. | |
438 | * | |
439 | * When no binding exists, and DEFINEP is true, create a new binding | |
440 | * with a initial value of SCM_UNDEFINED. Return `#f' when DEFINEP as | |
441 | * false and no binding exists. | |
442 | * | |
443 | * When PROC is `#f', it is ignored and the binding is searched for in | |
444 | * the scm_pre_modules_obarray (a `eq' hash table). | |
445 | */ | |
446 | ||
447 | SCM scm_pre_modules_obarray; | |
448 | ||
449 | SCM | |
450 | scm_sym2var (SCM sym, SCM proc, SCM definep) | |
451 | #define FUNC_NAME "scm_sym2var" | |
452 | { | |
453 | SCM var; | |
454 | ||
455 | if (SCM_NIMP (proc)) | |
456 | { | |
457 | if (SCM_EVAL_CLOSURE_P (proc)) | |
458 | { | |
459 | /* Bypass evaluator in the standard case. */ | |
460 | var = scm_eval_closure_lookup (proc, sym, definep); | |
461 | } | |
462 | else | |
fdc28395 | 463 | var = scm_call_2 (proc, sym, definep); |
86d31dfe MV |
464 | } |
465 | else | |
466 | { | |
467 | SCM handle; | |
468 | ||
7888309b | 469 | if (scm_is_false (definep)) |
86d31dfe MV |
470 | var = scm_hashq_ref (scm_pre_modules_obarray, sym, SCM_BOOL_F); |
471 | else | |
472 | { | |
473 | handle = scm_hashq_create_handle_x (scm_pre_modules_obarray, | |
474 | sym, SCM_BOOL_F); | |
475 | var = SCM_CDR (handle); | |
7888309b | 476 | if (scm_is_false (var)) |
86d31dfe MV |
477 | { |
478 | var = scm_make_variable (SCM_UNDEFINED); | |
86d31dfe MV |
479 | SCM_SETCDR (handle, var); |
480 | } | |
481 | } | |
482 | } | |
483 | ||
7888309b | 484 | if (scm_is_true (var) && !SCM_VARIABLEP (var)) |
1afff620 | 485 | SCM_MISC_ERROR ("~S is not bound to a variable", scm_list_1 (sym)); |
86d31dfe MV |
486 | |
487 | return var; | |
488 | } | |
489 | #undef FUNC_NAME | |
490 | ||
491 | SCM | |
492 | scm_c_module_lookup (SCM module, const char *name) | |
493 | { | |
cc95e00a | 494 | return scm_module_lookup (module, scm_from_locale_symbol (name)); |
86d31dfe MV |
495 | } |
496 | ||
497 | SCM | |
498 | scm_module_lookup (SCM module, SCM sym) | |
499 | #define FUNC_NAME "module-lookup" | |
500 | { | |
501 | SCM var; | |
502 | SCM_VALIDATE_MODULE (1, module); | |
503 | ||
504 | var = scm_sym2var (sym, scm_module_lookup_closure (module), SCM_BOOL_F); | |
7888309b | 505 | if (scm_is_false (var)) |
1afff620 | 506 | SCM_MISC_ERROR ("unbound variable: ~S", scm_list_1 (sym)); |
86d31dfe MV |
507 | return var; |
508 | } | |
509 | #undef FUNC_NAME | |
510 | ||
511 | SCM | |
512 | scm_c_lookup (const char *name) | |
513 | { | |
cc95e00a | 514 | return scm_lookup (scm_from_locale_symbol (name)); |
86d31dfe MV |
515 | } |
516 | ||
517 | SCM | |
518 | scm_lookup (SCM sym) | |
519 | { | |
520 | SCM var = | |
521 | scm_sym2var (sym, scm_current_module_lookup_closure (), SCM_BOOL_F); | |
7888309b | 522 | if (scm_is_false (var)) |
1afff620 | 523 | scm_misc_error ("scm_lookup", "unbound variable: ~S", scm_list_1 (sym)); |
86d31dfe MV |
524 | return var; |
525 | } | |
526 | ||
527 | SCM | |
528 | scm_c_module_define (SCM module, const char *name, SCM value) | |
529 | { | |
cc95e00a | 530 | return scm_module_define (module, scm_from_locale_symbol (name), value); |
86d31dfe MV |
531 | } |
532 | ||
533 | SCM | |
534 | scm_module_define (SCM module, SCM sym, SCM value) | |
535 | #define FUNC_NAME "module-define" | |
536 | { | |
537 | SCM var; | |
538 | SCM_VALIDATE_MODULE (1, module); | |
539 | ||
540 | var = scm_sym2var (sym, scm_module_lookup_closure (module), SCM_BOOL_T); | |
541 | SCM_VARIABLE_SET (var, value); | |
542 | return var; | |
543 | } | |
544 | #undef FUNC_NAME | |
545 | ||
546 | SCM | |
547 | scm_c_define (const char *name, SCM value) | |
548 | { | |
cc95e00a | 549 | return scm_define (scm_from_locale_symbol (name), value); |
86d31dfe MV |
550 | } |
551 | ||
552 | SCM | |
553 | scm_define (SCM sym, SCM value) | |
554 | { | |
555 | SCM var = | |
556 | scm_sym2var (sym, scm_current_module_lookup_closure (), SCM_BOOL_T); | |
557 | SCM_VARIABLE_SET (var, value); | |
558 | return var; | |
559 | } | |
560 | ||
561 | SCM | |
562 | scm_module_reverse_lookup (SCM module, SCM variable) | |
563 | #define FUNC_NAME "module-reverse-lookup" | |
564 | { | |
565 | SCM obarray; | |
c014a02e | 566 | long i, n; |
86d31dfe | 567 | |
7888309b | 568 | if (scm_is_false (module)) |
86d31dfe MV |
569 | obarray = scm_pre_modules_obarray; |
570 | else | |
571 | { | |
572 | SCM_VALIDATE_MODULE (1, module); | |
573 | obarray = SCM_MODULE_OBARRAY (module); | |
574 | } | |
575 | ||
6dc1cd1e MV |
576 | if (!SCM_HASHTABLE_P (obarray)) |
577 | return SCM_BOOL_F; | |
578 | ||
86d31dfe MV |
579 | /* XXX - We do not use scm_hash_fold here to avoid searching the |
580 | whole obarray. We should have a scm_hash_find procedure. */ | |
581 | ||
c35738c1 | 582 | n = SCM_HASHTABLE_N_BUCKETS (obarray); |
86d31dfe MV |
583 | for (i = 0; i < n; ++i) |
584 | { | |
4057a3e0 | 585 | SCM ls = SCM_HASHTABLE_BUCKET (obarray, i), handle; |
d2e53ed6 | 586 | while (!scm_is_null (ls)) |
86d31dfe MV |
587 | { |
588 | handle = SCM_CAR (ls); | |
589 | if (SCM_CDR (handle) == variable) | |
590 | return SCM_CAR (handle); | |
591 | ls = SCM_CDR (ls); | |
592 | } | |
593 | } | |
594 | ||
595 | /* Try the `uses' list. | |
596 | */ | |
597 | { | |
598 | SCM uses = SCM_MODULE_USES (module); | |
d2e53ed6 | 599 | while (scm_is_pair (uses)) |
86d31dfe MV |
600 | { |
601 | SCM sym = scm_module_reverse_lookup (SCM_CAR (uses), variable); | |
7888309b | 602 | if (scm_is_true (sym)) |
86d31dfe MV |
603 | return sym; |
604 | uses = SCM_CDR (uses); | |
605 | } | |
606 | } | |
607 | ||
608 | return SCM_BOOL_F; | |
609 | } | |
610 | #undef FUNC_NAME | |
611 | ||
612 | SCM_DEFINE (scm_get_pre_modules_obarray, "%get-pre-modules-obarray", 0, 0, 0, | |
613 | (), | |
614 | "Return the obarray that is used for all new bindings before " | |
615 | "the module system is booted. The first call to " | |
616 | "@code{set-current-module} will boot the module system.") | |
617 | #define FUNC_NAME s_scm_get_pre_modules_obarray | |
618 | { | |
619 | return scm_pre_modules_obarray; | |
620 | } | |
621 | #undef FUNC_NAME | |
622 | ||
d02b98e9 MV |
623 | SCM_SYMBOL (scm_sym_system_module, "system-module"); |
624 | ||
625 | SCM | |
626 | scm_system_module_env_p (SCM env) | |
627 | { | |
628 | SCM proc = scm_env_top_level (env); | |
7888309b | 629 | if (scm_is_false (proc)) |
d02b98e9 | 630 | return SCM_BOOL_T; |
7888309b | 631 | return ((scm_is_true (scm_procedure_property (proc, |
d02b98e9 MV |
632 | scm_sym_system_module))) |
633 | ? SCM_BOOL_T | |
634 | : SCM_BOOL_F); | |
635 | } | |
636 | ||
86d31dfe MV |
637 | void |
638 | scm_modules_prehistory () | |
639 | { | |
640 | scm_pre_modules_obarray | |
231a4ea8 | 641 | = scm_permanent_object (scm_c_make_hash_table (1533)); |
86d31dfe MV |
642 | } |
643 | ||
1ffa265b MD |
644 | void |
645 | scm_init_modules () | |
646 | { | |
a0599745 | 647 | #include "libguile/modules.x" |
86d31dfe MV |
648 | module_make_local_var_x_var = scm_c_define ("module-make-local-var!", |
649 | SCM_UNDEFINED); | |
e841c3e0 KN |
650 | scm_tc16_eval_closure = scm_make_smob_type ("eval-closure", 0); |
651 | scm_set_smob_mark (scm_tc16_eval_closure, scm_markcdr); | |
652 | scm_set_smob_apply (scm_tc16_eval_closure, scm_eval_closure_lookup, 2, 0, 0); | |
55000e5f MV |
653 | |
654 | the_module = scm_permanent_object (scm_make_fluid ()); | |
1ffa265b MD |
655 | } |
656 | ||
86d31dfe | 657 | static void |
1ffa265b MD |
658 | scm_post_boot_init_modules () |
659 | { | |
86d31dfe MV |
660 | #define PERM(x) scm_permanent_object(x) |
661 | ||
662 | SCM module_type = SCM_VARIABLE_REF (scm_c_lookup ("module-type")); | |
904a077d | 663 | scm_module_tag = (SCM_CELL_WORD_1 (module_type) + scm_tc3_struct); |
d02b98e9 MV |
664 | |
665 | resolve_module_var = PERM (scm_c_lookup ("resolve-module")); | |
666 | process_define_module_var = PERM (scm_c_lookup ("process-define-module")); | |
667 | process_use_modules_var = PERM (scm_c_lookup ("process-use-modules")); | |
668 | module_export_x_var = PERM (scm_c_lookup ("module-export!")); | |
669 | the_root_module_var = PERM (scm_c_lookup ("the-root-module")); | |
670 | ||
e3365c07 | 671 | scm_module_system_booted_p = 1; |
1ffa265b | 672 | } |
89e00824 ML |
673 | |
674 | /* | |
675 | Local Variables: | |
676 | c-file-style: "gnu" | |
677 | End: | |
678 | */ |