modules.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. /* Copyright 1998,2000-2004,2006-2012,2017-2019
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <stdarg.h>
  19. #include "boolean.h"
  20. #include "deprecation.h"
  21. #include "eval.h"
  22. #include "fluids.h"
  23. #include "gsubr.h"
  24. #include "hashtab.h"
  25. #include "keywords.h"
  26. #include "list.h"
  27. #include "pairs.h"
  28. #include "procprop.h"
  29. #include "smob.h"
  30. #include "struct.h"
  31. #include "symbols.h"
  32. #include "threads.h"
  33. #include "variable.h"
  34. #include "vectors.h"
  35. #include "modules.h"
  36. int scm_module_system_booted_p = 0;
  37. scm_t_bits scm_module_tag;
  38. /* The current module, a fluid. */
  39. static SCM the_module;
  40. /* Most of the module system is implemented in Scheme. These bindings from
  41. boot-9 are needed to provide the Scheme interface. */
  42. static SCM the_root_module_var;
  43. static SCM module_make_local_var_x_var;
  44. static SCM define_module_star_var;
  45. static SCM process_use_modules_var;
  46. static SCM resolve_module_var;
  47. static SCM module_public_interface_var;
  48. static SCM module_export_x_var;
  49. static SCM default_duplicate_binding_procedures_var;
  50. /* The #:ensure keyword. */
  51. static SCM k_ensure;
  52. static SCM unbound_variable (const char *func, SCM sym)
  53. {
  54. scm_error (scm_from_latin1_symbol ("unbound-variable"), func,
  55. "Unbound variable: ~S", scm_list_1 (sym), SCM_BOOL_F);
  56. }
  57. SCM
  58. scm_the_root_module (void)
  59. {
  60. if (scm_module_system_booted_p)
  61. return SCM_VARIABLE_REF (the_root_module_var);
  62. else
  63. return SCM_BOOL_F;
  64. }
  65. SCM
  66. scm_i_current_module (scm_thread *thread)
  67. {
  68. if (scm_module_system_booted_p)
  69. return scm_i_fluid_ref (thread, the_module);
  70. else
  71. return SCM_BOOL_F;
  72. }
  73. SCM_DEFINE (scm_current_module, "current-module", 0, 0, 0,
  74. (),
  75. "Return the current module.")
  76. #define FUNC_NAME s_scm_current_module
  77. {
  78. return scm_i_current_module (SCM_I_CURRENT_THREAD);
  79. }
  80. #undef FUNC_NAME
  81. static void scm_post_boot_init_modules (void);
  82. SCM_DEFINE (scm_set_current_module, "set-current-module", 1, 0, 0,
  83. (SCM module),
  84. "Set the current module to @var{module} and return\n"
  85. "the previous current module.")
  86. #define FUNC_NAME s_scm_set_current_module
  87. {
  88. SCM old;
  89. if (!scm_module_system_booted_p)
  90. scm_post_boot_init_modules ();
  91. SCM_VALIDATE_MODULE (SCM_ARG1, module);
  92. old = scm_current_module ();
  93. scm_fluid_set_x (the_module, module);
  94. return old;
  95. }
  96. #undef FUNC_NAME
  97. SCM_DEFINE (scm_interaction_environment, "interaction-environment", 0, 0, 0,
  98. (),
  99. "Return a specifier for the environment that contains\n"
  100. "implementation--defined bindings, typically a superset of those\n"
  101. "listed in the report. The intent is that this procedure will\n"
  102. "return the environment in which the implementation would\n"
  103. "evaluate expressions dynamically typed by the user.")
  104. #define FUNC_NAME s_scm_interaction_environment
  105. {
  106. return scm_current_module ();
  107. }
  108. #undef FUNC_NAME
  109. SCM
  110. scm_c_call_with_current_module (SCM module,
  111. SCM (*func)(void *), void *data)
  112. {
  113. return scm_c_with_fluid (the_module, module, func, data);
  114. }
  115. void
  116. scm_dynwind_current_module (SCM module)
  117. {
  118. scm_dynwind_fluid (the_module, module);
  119. }
  120. /*
  121. convert "A B C" to scheme list (A B C)
  122. */
  123. static SCM
  124. convert_module_name (const char *name)
  125. {
  126. SCM list = SCM_EOL;
  127. SCM *tail = &list;
  128. const char *ptr;
  129. while (*name)
  130. {
  131. while (*name == ' ')
  132. name++;
  133. ptr = name;
  134. while (*ptr && *ptr != ' ')
  135. ptr++;
  136. if (ptr > name)
  137. {
  138. SCM sym = scm_from_utf8_symboln (name, ptr-name);
  139. *tail = scm_cons (sym, SCM_EOL);
  140. tail = SCM_CDRLOC (*tail);
  141. }
  142. name = ptr;
  143. }
  144. return list;
  145. }
  146. SCM
  147. scm_c_resolve_module (const char *name)
  148. {
  149. return scm_resolve_module (convert_module_name (name));
  150. }
  151. SCM
  152. scm_resolve_module (SCM name)
  153. {
  154. return scm_call_1 (SCM_VARIABLE_REF (resolve_module_var), name);
  155. }
  156. SCM
  157. scm_maybe_resolve_module (SCM name)
  158. {
  159. return scm_call_3 (SCM_VARIABLE_REF (resolve_module_var), name,
  160. k_ensure, SCM_BOOL_F);
  161. }
  162. SCM
  163. scm_c_define_module (const char *name,
  164. void (*init)(void *), void *data)
  165. {
  166. SCM module = scm_call_1 (SCM_VARIABLE_REF (define_module_star_var),
  167. convert_module_name (name));
  168. if (init)
  169. scm_c_call_with_current_module (module, (SCM (*)(void*))init, data);
  170. return module;
  171. }
  172. void
  173. scm_c_use_module (const char *name)
  174. {
  175. scm_call_1 (SCM_VARIABLE_REF (process_use_modules_var),
  176. scm_list_1 (scm_list_1 (convert_module_name (name))));
  177. }
  178. SCM
  179. scm_module_export (SCM module, SCM namelist)
  180. {
  181. return scm_call_2 (SCM_VARIABLE_REF (module_export_x_var),
  182. module, namelist);
  183. }
  184. /*
  185. @code{scm_c_export}(@var{name-list})
  186. @code{scm_c_export} exports the named bindings from the current
  187. module, making them visible to users of the module. This function
  188. takes a list of string arguments, terminated by NULL, e.g.
  189. @example
  190. scm_c_export ("add-double-record", "bamboozle-money", NULL);
  191. @end example
  192. */
  193. void
  194. scm_c_export (const char *name, ...)
  195. {
  196. if (name)
  197. {
  198. va_list ap;
  199. SCM names = scm_cons (scm_from_utf8_symbol (name), SCM_EOL);
  200. SCM *tail = SCM_CDRLOC (names);
  201. va_start (ap, name);
  202. while (1)
  203. {
  204. const char *n = va_arg (ap, const char *);
  205. if (n == NULL)
  206. break;
  207. *tail = scm_cons (scm_from_utf8_symbol (n), SCM_EOL);
  208. tail = SCM_CDRLOC (*tail);
  209. }
  210. va_end (ap);
  211. scm_module_export (scm_current_module (), names);
  212. }
  213. }
  214. /*
  215. * C level implementation of the standard eval closure
  216. *
  217. * This increases loading speed substantially. The code may be
  218. * replaced by something based on environments.[ch], in a future
  219. * release.
  220. */
  221. /* Return the list of default duplicate binding handlers (procedures). */
  222. static inline SCM
  223. default_duplicate_binding_handlers (void)
  224. {
  225. SCM get_handlers;
  226. get_handlers = SCM_VARIABLE_REF (default_duplicate_binding_procedures_var);
  227. return (scm_call_0 (get_handlers));
  228. }
  229. /* Each module has an "import obarray" that may be accessed concurrently
  230. by several threads. This mutex protects access to any obarray. This
  231. is coarse-grain but (1) pthread mutexes are quite cheap, and (2)
  232. Scheme "programs" have a cache for free variables anyway. */
  233. static scm_i_pthread_mutex_t import_obarray_mutex =
  234. SCM_I_PTHREAD_MUTEX_INITIALIZER;
  235. /* Resolve the import of SYM in MODULE, where SYM is currently provided by
  236. both IFACE1 as VAR1 and IFACE2 as VAR2. Return the variable chosen by the
  237. duplicate binding handlers or `#f'. */
  238. static inline SCM
  239. resolve_duplicate_binding (SCM module, SCM sym,
  240. SCM iface1, SCM var1,
  241. SCM iface2, SCM var2)
  242. {
  243. SCM args[8];
  244. SCM handlers;
  245. SCM result = SCM_BOOL_F;
  246. if (scm_is_eq (var1, var2))
  247. return var1;
  248. args[0] = module;
  249. args[1] = sym;
  250. args[2] = iface1;
  251. args[3] = SCM_VARIABLE_REF (var1);
  252. if (SCM_UNBNDP (args[3]))
  253. args[3] = SCM_BOOL_F;
  254. args[4] = iface2;
  255. args[5] = SCM_VARIABLE_REF (var2);
  256. if (SCM_UNBNDP (args[5]))
  257. args[5] = SCM_BOOL_F;
  258. scm_i_pthread_mutex_lock (&import_obarray_mutex);
  259. args[6] = scm_hashq_ref (SCM_MODULE_IMPORT_OBARRAY (module), sym, SCM_BOOL_F);
  260. scm_i_pthread_mutex_unlock (&import_obarray_mutex);
  261. args[7] = SCM_BOOL_F;
  262. handlers = SCM_MODULE_DUPLICATE_HANDLERS (module);
  263. if (scm_is_false (handlers))
  264. handlers = default_duplicate_binding_handlers ();
  265. for (; scm_is_pair (handlers); handlers = SCM_CDR (handlers))
  266. {
  267. if (scm_is_true (args[6]))
  268. {
  269. args[7] = SCM_VARIABLE_REF (args[6]);
  270. if (SCM_UNBNDP (args[7]))
  271. args[7] = SCM_BOOL_F;
  272. }
  273. result = scm_call_n (SCM_CAR (handlers), args, 8);
  274. if (scm_is_true (result))
  275. return result;
  276. }
  277. return SCM_BOOL_F;
  278. }
  279. /* No lock is needed for access to this variable, as there are no
  280. threads before modules are booted. */
  281. SCM scm_pre_modules_obarray;
  282. /* Lookup SYM as an imported variable of MODULE. */
  283. static inline SCM
  284. module_imported_variable (SCM module, SCM sym)
  285. {
  286. #define SCM_BOUND_THING_P scm_is_true
  287. register SCM var, imports;
  288. /* Search cached imported bindings. */
  289. imports = SCM_MODULE_IMPORT_OBARRAY (module);
  290. scm_i_pthread_mutex_lock (&import_obarray_mutex);
  291. var = scm_hashq_ref (imports, sym, SCM_UNDEFINED);
  292. scm_i_pthread_mutex_unlock (&import_obarray_mutex);
  293. if (SCM_BOUND_THING_P (var))
  294. return var;
  295. {
  296. /* Search the use list for yet uncached imported bindings, possibly
  297. resolving duplicates as needed and caching the result in the import
  298. obarray. */
  299. SCM uses;
  300. SCM found_var = SCM_BOOL_F, found_iface = SCM_BOOL_F;
  301. for (uses = SCM_MODULE_USES (module);
  302. scm_is_pair (uses);
  303. uses = SCM_CDR (uses))
  304. {
  305. SCM iface;
  306. iface = SCM_CAR (uses);
  307. var = scm_module_variable (iface, sym);
  308. if (SCM_BOUND_THING_P (var))
  309. {
  310. if (SCM_BOUND_THING_P (found_var))
  311. {
  312. /* SYM is a duplicate binding (imported more than once) so we
  313. need to resolve it. */
  314. found_var = resolve_duplicate_binding (module, sym,
  315. found_iface, found_var,
  316. iface, var);
  317. /* Note that it could be that FOUND_VAR doesn't belong
  318. either to FOUND_IFACE or to IFACE, if it was created
  319. by merge-generics. The right thing to do there would
  320. be to treat the import obarray as the iface, but the
  321. import obarray isn't actually a module. Oh well. */
  322. if (scm_is_eq (found_var, var))
  323. found_iface = iface;
  324. }
  325. else
  326. /* Keep track of the variable we found and check for other
  327. occurences of SYM in the use list. */
  328. found_var = var, found_iface = iface;
  329. }
  330. }
  331. if (SCM_BOUND_THING_P (found_var))
  332. {
  333. /* Save the lookup result for future reference. */
  334. scm_i_pthread_mutex_lock (&import_obarray_mutex);
  335. (void) scm_hashq_set_x (imports, sym, found_var);
  336. scm_i_pthread_mutex_unlock (&import_obarray_mutex);
  337. return found_var;
  338. }
  339. }
  340. return SCM_BOOL_F;
  341. #undef SCM_BOUND_THING_P
  342. }
  343. SCM_DEFINE (scm_module_local_variable, "module-local-variable", 2, 0, 0,
  344. (SCM module, SCM sym),
  345. "Return the variable bound to @var{sym} in @var{module}. Return "
  346. "@code{#f} is @var{sym} is not bound locally in @var{module}.")
  347. #define FUNC_NAME s_scm_module_local_variable
  348. {
  349. #define SCM_BOUND_THING_P(b) \
  350. (scm_is_true (b))
  351. register SCM b;
  352. if (scm_module_system_booted_p)
  353. SCM_VALIDATE_MODULE (1, module);
  354. SCM_VALIDATE_SYMBOL (2, sym);
  355. if (scm_is_false (module))
  356. return scm_hashq_ref (scm_pre_modules_obarray, sym, SCM_UNDEFINED);
  357. /* 1. Check module obarray */
  358. b = scm_hashq_ref (SCM_MODULE_OBARRAY (module), sym, SCM_UNDEFINED);
  359. if (SCM_BOUND_THING_P (b))
  360. return b;
  361. /* At this point we should just be able to return #f, but there is the
  362. possibility that a custom binder establishes a mapping for this
  363. variable.
  364. However a custom binder should be called only if there is no
  365. imported binding with the name SYM. So here instead of the order:
  366. 2. Search imported bindings. In order to be consistent with
  367. `module-variable', the binder gets called only when no
  368. imported binding matches SYM.
  369. 3. Query the custom binder.
  370. we first check if there is a binder at all, and if not, just return
  371. #f directly.
  372. */
  373. {
  374. SCM binder = SCM_MODULE_BINDER (module);
  375. if (scm_is_true (binder))
  376. {
  377. /* 2. */
  378. b = module_imported_variable (module, sym);
  379. if (SCM_BOUND_THING_P (b))
  380. return SCM_BOOL_F;
  381. /* 3. */
  382. b = scm_call_3 (binder, module, sym, SCM_BOOL_F);
  383. if (SCM_BOUND_THING_P (b))
  384. return b;
  385. }
  386. }
  387. return SCM_BOOL_F;
  388. #undef SCM_BOUND_THING_P
  389. }
  390. #undef FUNC_NAME
  391. SCM_DEFINE (scm_module_variable, "module-variable", 2, 0, 0,
  392. (SCM module, SCM sym),
  393. "Return the variable bound to @var{sym} in @var{module}. This "
  394. "may be both a local variable or an imported variable. Return "
  395. "@code{#f} is @var{sym} is not bound in @var{module}.")
  396. #define FUNC_NAME s_scm_module_variable
  397. {
  398. #define SCM_BOUND_THING_P(b) \
  399. (scm_is_true (b))
  400. register SCM var;
  401. if (scm_module_system_booted_p)
  402. SCM_VALIDATE_MODULE (1, module);
  403. SCM_VALIDATE_SYMBOL (2, sym);
  404. if (scm_is_false (module))
  405. return scm_hashq_ref (scm_pre_modules_obarray, sym, SCM_UNDEFINED);
  406. /* 1. Check module obarray */
  407. var = scm_hashq_ref (SCM_MODULE_OBARRAY (module), sym, SCM_UNDEFINED);
  408. if (SCM_BOUND_THING_P (var))
  409. return var;
  410. /* 2. Search among the imported variables. */
  411. var = module_imported_variable (module, sym);
  412. if (SCM_BOUND_THING_P (var))
  413. return var;
  414. {
  415. /* 3. Query the custom binder. */
  416. SCM binder;
  417. binder = SCM_MODULE_BINDER (module);
  418. if (scm_is_true (binder))
  419. {
  420. var = scm_call_3 (binder, module, sym, SCM_BOOL_F);
  421. if (SCM_BOUND_THING_P (var))
  422. return var;
  423. }
  424. }
  425. return SCM_BOOL_F;
  426. #undef SCM_BOUND_THING_P
  427. }
  428. #undef FUNC_NAME
  429. SCM
  430. scm_module_ensure_local_variable (SCM module, SCM sym)
  431. #define FUNC_NAME "module-ensure-local-variable"
  432. {
  433. if (SCM_LIKELY (scm_module_system_booted_p))
  434. {
  435. SCM_VALIDATE_MODULE (1, module);
  436. SCM_VALIDATE_SYMBOL (2, sym);
  437. return scm_call_2 (SCM_VARIABLE_REF (module_make_local_var_x_var),
  438. module, sym);
  439. }
  440. {
  441. SCM handle, var;
  442. handle = scm_hashq_create_handle_x (scm_pre_modules_obarray,
  443. sym, SCM_BOOL_F);
  444. var = SCM_CDR (handle);
  445. if (scm_is_false (var))
  446. {
  447. var = scm_make_variable (SCM_UNDEFINED);
  448. SCM_SETCDR (handle, var);
  449. }
  450. return var;
  451. }
  452. }
  453. #undef FUNC_NAME
  454. SCM_SYMBOL (sym_macroexpand, "macroexpand");
  455. SCM_DEFINE (scm_module_transformer, "module-transformer", 1, 0, 0,
  456. (SCM module),
  457. "Returns the syntax expander for the given module.")
  458. #define FUNC_NAME s_scm_module_transformer
  459. {
  460. if (SCM_UNLIKELY (scm_is_false (module)))
  461. {
  462. SCM v = scm_hashq_ref (scm_pre_modules_obarray,
  463. sym_macroexpand,
  464. SCM_BOOL_F);
  465. if (scm_is_false (v))
  466. SCM_MISC_ERROR ("no module, and `macroexpand' unbound", SCM_EOL);
  467. return SCM_VARIABLE_REF (v);
  468. }
  469. else
  470. {
  471. SCM_VALIDATE_MODULE (SCM_ARG1, module);
  472. return SCM_MODULE_TRANSFORMER (module);
  473. }
  474. }
  475. #undef FUNC_NAME
  476. SCM
  477. scm_current_module_transformer ()
  478. {
  479. return scm_module_transformer (scm_current_module ());
  480. }
  481. SCM_DEFINE (scm_module_import_interface, "module-import-interface", 2, 0, 0,
  482. (SCM module, SCM sym),
  483. "Return the module or interface from which @var{sym} is imported "
  484. "in @var{module}. If @var{sym} is not imported (i.e., it is not "
  485. "defined in @var{module} or it is a module-local binding instead "
  486. "of an imported one), then @code{#f} is returned.")
  487. #define FUNC_NAME s_scm_module_import_interface
  488. {
  489. SCM var, result = SCM_BOOL_F;
  490. SCM_VALIDATE_MODULE (1, module);
  491. SCM_VALIDATE_SYMBOL (2, sym);
  492. var = scm_module_variable (module, sym);
  493. if (scm_is_true (var))
  494. {
  495. /* Look for the module that provides VAR. */
  496. SCM local_var;
  497. local_var = scm_hashq_ref (SCM_MODULE_OBARRAY (module), sym,
  498. SCM_UNDEFINED);
  499. if (scm_is_eq (local_var, var))
  500. result = module;
  501. else
  502. {
  503. /* Look for VAR among the used modules. */
  504. SCM uses, imported_var;
  505. for (uses = SCM_MODULE_USES (module);
  506. scm_is_pair (uses) && scm_is_false (result);
  507. uses = SCM_CDR (uses))
  508. {
  509. imported_var = scm_module_variable (SCM_CAR (uses), sym);
  510. if (scm_is_eq (imported_var, var))
  511. result = SCM_CAR (uses);
  512. }
  513. }
  514. }
  515. return result;
  516. }
  517. #undef FUNC_NAME
  518. SCM
  519. scm_module_public_interface (SCM module)
  520. {
  521. return scm_call_1 (SCM_VARIABLE_REF (module_public_interface_var), module);
  522. }
  523. SCM
  524. scm_c_module_lookup (SCM module, const char *name)
  525. {
  526. return scm_module_lookup (module, scm_from_utf8_symbol (name));
  527. }
  528. SCM
  529. scm_module_lookup (SCM module, SCM sym)
  530. #define FUNC_NAME "module-lookup"
  531. {
  532. SCM var;
  533. var = scm_module_variable (module, sym);
  534. if (scm_is_false (var))
  535. unbound_variable (FUNC_NAME, sym);
  536. return var;
  537. }
  538. #undef FUNC_NAME
  539. SCM
  540. scm_c_lookup (const char *name)
  541. {
  542. return scm_lookup (scm_from_utf8_symbol (name));
  543. }
  544. SCM
  545. scm_lookup (SCM sym)
  546. {
  547. return scm_module_lookup (scm_current_module (), sym);
  548. }
  549. SCM
  550. scm_public_variable (SCM module_name, SCM name)
  551. {
  552. SCM mod, iface;
  553. mod = scm_maybe_resolve_module (module_name);
  554. if (scm_is_false (mod))
  555. scm_misc_error ("public-lookup", "Module named ~s does not exist",
  556. scm_list_1 (module_name));
  557. iface = scm_module_public_interface (mod);
  558. if (scm_is_false (iface))
  559. scm_misc_error ("public-lookup", "Module ~s has no public interface",
  560. scm_list_1 (mod));
  561. return scm_module_variable (iface, name);
  562. }
  563. SCM
  564. scm_private_variable (SCM module_name, SCM name)
  565. {
  566. SCM mod;
  567. mod = scm_maybe_resolve_module (module_name);
  568. if (scm_is_false (mod))
  569. scm_misc_error ("private-lookup", "Module named ~s does not exist",
  570. scm_list_1 (module_name));
  571. return scm_module_variable (mod, name);
  572. }
  573. SCM
  574. scm_c_public_variable (const char *module_name, const char *name)
  575. {
  576. return scm_public_variable (convert_module_name (module_name),
  577. scm_from_utf8_symbol (name));
  578. }
  579. SCM
  580. scm_c_private_variable (const char *module_name, const char *name)
  581. {
  582. return scm_private_variable (convert_module_name (module_name),
  583. scm_from_utf8_symbol (name));
  584. }
  585. SCM
  586. scm_public_lookup (SCM module_name, SCM name)
  587. {
  588. SCM var;
  589. var = scm_public_variable (module_name, name);
  590. if (scm_is_false (var))
  591. scm_misc_error ("public-lookup", "No variable bound to ~s in module ~s",
  592. scm_list_2 (name, module_name));
  593. return var;
  594. }
  595. SCM
  596. scm_private_lookup (SCM module_name, SCM name)
  597. {
  598. SCM var;
  599. var = scm_private_variable (module_name, name);
  600. if (scm_is_false (var))
  601. scm_misc_error ("private-lookup", "No variable bound to ~s in module ~s",
  602. scm_list_2 (name, module_name));
  603. return var;
  604. }
  605. SCM
  606. scm_c_public_lookup (const char *module_name, const char *name)
  607. {
  608. return scm_public_lookup (convert_module_name (module_name),
  609. scm_from_utf8_symbol (name));
  610. }
  611. SCM
  612. scm_c_private_lookup (const char *module_name, const char *name)
  613. {
  614. return scm_private_lookup (convert_module_name (module_name),
  615. scm_from_utf8_symbol (name));
  616. }
  617. SCM
  618. scm_public_ref (SCM module_name, SCM name)
  619. {
  620. return scm_variable_ref (scm_public_lookup (module_name, name));
  621. }
  622. SCM
  623. scm_private_ref (SCM module_name, SCM name)
  624. {
  625. return scm_variable_ref (scm_private_lookup (module_name, name));
  626. }
  627. SCM
  628. scm_c_public_ref (const char *module_name, const char *name)
  629. {
  630. return scm_public_ref (convert_module_name (module_name),
  631. scm_from_utf8_symbol (name));
  632. }
  633. SCM
  634. scm_c_private_ref (const char *module_name, const char *name)
  635. {
  636. return scm_private_ref (convert_module_name (module_name),
  637. scm_from_utf8_symbol (name));
  638. }
  639. SCM
  640. scm_c_module_define (SCM module, const char *name, SCM value)
  641. {
  642. return scm_module_define (module, scm_from_utf8_symbol (name), value);
  643. }
  644. SCM
  645. scm_module_define (SCM module, SCM sym, SCM value)
  646. #define FUNC_NAME "module-define"
  647. {
  648. SCM var;
  649. var = scm_module_ensure_local_variable (module, sym);
  650. SCM_VARIABLE_SET (var, value);
  651. return var;
  652. }
  653. #undef FUNC_NAME
  654. SCM
  655. scm_c_define (const char *name, SCM value)
  656. {
  657. return scm_define (scm_from_utf8_symbol (name), value);
  658. }
  659. SCM_DEFINE (scm_define, "define!", 2, 0, 0,
  660. (SCM sym, SCM value),
  661. "Define @var{sym} to be @var{value} in the current module."
  662. "Returns the variable itself. Note that this is a procedure, "
  663. "not a macro.")
  664. #define FUNC_NAME s_scm_define
  665. {
  666. SCM_VALIDATE_SYMBOL (SCM_ARG1, sym);
  667. return scm_module_define (scm_current_module (), sym, value);
  668. }
  669. #undef FUNC_NAME
  670. SCM_DEFINE (scm_module_reverse_lookup, "module-reverse-lookup", 2, 0, 0,
  671. (SCM module, SCM variable),
  672. "Return the symbol under which @var{variable} is bound in "
  673. "@var{module} or @var{#f} if @var{variable} is not visible "
  674. "from @var{module}. If @var{module} is @code{#f}, then the "
  675. "pre-module obarray is used.")
  676. #define FUNC_NAME s_scm_module_reverse_lookup
  677. {
  678. SCM obarray;
  679. long i, n;
  680. if (scm_is_false (module))
  681. obarray = scm_pre_modules_obarray;
  682. else
  683. {
  684. SCM_VALIDATE_MODULE (1, module);
  685. obarray = SCM_MODULE_OBARRAY (module);
  686. }
  687. SCM_VALIDATE_VARIABLE (SCM_ARG2, variable);
  688. if (!SCM_HASHTABLE_P (obarray))
  689. return SCM_BOOL_F;
  690. /* XXX - We do not use scm_hash_fold here to avoid searching the
  691. whole obarray. We should have a scm_hash_find procedure. */
  692. n = SCM_HASHTABLE_N_BUCKETS (obarray);
  693. for (i = 0; i < n; ++i)
  694. {
  695. SCM ls = SCM_HASHTABLE_BUCKET (obarray, i), handle;
  696. while (!scm_is_null (ls))
  697. {
  698. handle = SCM_CAR (ls);
  699. if (scm_is_eq (SCM_CDR (handle), variable))
  700. return SCM_CAR (handle);
  701. ls = SCM_CDR (ls);
  702. }
  703. }
  704. if (!scm_is_false (module))
  705. {
  706. /* Try the `uses' list. */
  707. SCM uses = SCM_MODULE_USES (module);
  708. while (scm_is_pair (uses))
  709. {
  710. SCM sym = scm_module_reverse_lookup (SCM_CAR (uses), variable);
  711. if (scm_is_true (sym))
  712. return sym;
  713. uses = SCM_CDR (uses);
  714. }
  715. }
  716. return SCM_BOOL_F;
  717. }
  718. #undef FUNC_NAME
  719. SCM_DEFINE (scm_get_pre_modules_obarray, "%get-pre-modules-obarray", 0, 0, 0,
  720. (),
  721. "Return the obarray that is used for all new bindings before "
  722. "the module system is booted. The first call to "
  723. "@code{set-current-module} will boot the module system.")
  724. #define FUNC_NAME s_scm_get_pre_modules_obarray
  725. {
  726. return scm_pre_modules_obarray;
  727. }
  728. #undef FUNC_NAME
  729. SCM_SYMBOL (scm_sym_system_module, "system-module");
  730. void
  731. scm_modules_prehistory ()
  732. {
  733. scm_pre_modules_obarray = scm_c_make_hash_table (1790);
  734. }
  735. void
  736. scm_init_modules ()
  737. {
  738. #include "modules.x"
  739. module_make_local_var_x_var = scm_c_define ("module-make-local-var!",
  740. SCM_UNDEFINED);
  741. the_module = scm_make_fluid ();
  742. }
  743. static void
  744. scm_post_boot_init_modules ()
  745. {
  746. SCM module_type = SCM_VARIABLE_REF (scm_c_lookup ("module-type"));
  747. scm_module_tag = SCM_UNPACK (module_type) + scm_tc3_struct;
  748. resolve_module_var = scm_c_lookup ("resolve-module");
  749. define_module_star_var = scm_c_lookup ("define-module*");
  750. process_use_modules_var = scm_c_lookup ("process-use-modules");
  751. module_export_x_var = scm_c_lookup ("module-export!");
  752. the_root_module_var = scm_c_lookup ("the-root-module");
  753. default_duplicate_binding_procedures_var =
  754. scm_c_lookup ("default-duplicate-binding-procedures");
  755. module_public_interface_var = scm_c_lookup ("module-public-interface");
  756. k_ensure = scm_from_utf8_keyword ("ensure");
  757. scm_module_system_booted_p = 1;
  758. }