procprop.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* Copyright (C) 1995,1996,1998,2000,2001,2003,2004, 2006, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * 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.
  12. *
  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
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include "libguile/_scm.h"
  22. #include "libguile/alist.h"
  23. #include "libguile/eval.h"
  24. #include "libguile/procs.h"
  25. #include "libguile/gsubr.h"
  26. #include "libguile/smob.h"
  27. #include "libguile/root.h"
  28. #include "libguile/vectors.h"
  29. #include "libguile/hashtab.h"
  30. #include "libguile/programs.h"
  31. #include "libguile/validate.h"
  32. #include "libguile/procprop.h"
  33. SCM_GLOBAL_SYMBOL (scm_sym_system_procedure, "system-procedure");
  34. SCM_GLOBAL_SYMBOL (scm_sym_name, "name");
  35. static SCM overrides;
  36. static scm_i_pthread_mutex_t overrides_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER;
  37. int
  38. scm_i_procedure_arity (SCM proc, int *req, int *opt, int *rest)
  39. {
  40. while (!SCM_PROGRAM_P (proc))
  41. {
  42. if (SCM_IMP (proc))
  43. return 0;
  44. switch (SCM_TYP7 (proc))
  45. {
  46. case scm_tc7_smob:
  47. if (!SCM_SMOB_APPLICABLE_P (proc))
  48. return 0;
  49. proc = scm_i_smob_apply_trampoline (proc);
  50. break;
  51. case scm_tcs_struct:
  52. if (!SCM_STRUCT_APPLICABLE_P (proc))
  53. return 0;
  54. proc = SCM_STRUCT_PROCEDURE (proc);
  55. break;
  56. default:
  57. return 0;
  58. }
  59. }
  60. return scm_i_program_arity (proc, req, opt, rest);
  61. }
  62. SCM_DEFINE (scm_procedure_minimum_arity, "procedure-minimum-arity", 1, 0, 0,
  63. (SCM proc),
  64. "Return the \"minimum arity\" of a procedure.\n\n"
  65. "If the procedure has only one arity, that arity is returned\n"
  66. "as a list of three values: the number of required arguments,\n"
  67. "the number of optional arguments, and a boolean indicating\n"
  68. "whether or not the procedure takes rest arguments.\n\n"
  69. "For a case-lambda procedure, the arity returned is the one\n"
  70. "with the lowest minimum number of arguments, and the highest\n"
  71. "maximum number of arguments.\n\n"
  72. "If it was not possible to determine the arity of the procedure,\n"
  73. "@code{#f} is returned.")
  74. #define FUNC_NAME s_scm_procedure_minimum_arity
  75. {
  76. int req, opt, rest;
  77. if (scm_i_procedure_arity (proc, &req, &opt, &rest))
  78. return scm_list_3 (scm_from_int (req),
  79. scm_from_int (opt),
  80. scm_from_bool (rest));
  81. else
  82. return SCM_BOOL_F;
  83. }
  84. #undef FUNC_NAME
  85. SCM_DEFINE (scm_procedure_properties, "procedure-properties", 1, 0, 0,
  86. (SCM proc),
  87. "Return @var{obj}'s property list.")
  88. #define FUNC_NAME s_scm_procedure_properties
  89. {
  90. SCM ret;
  91. SCM_VALIDATE_PROC (1, proc);
  92. scm_i_pthread_mutex_lock (&overrides_lock);
  93. ret = scm_hashq_ref (overrides, proc, SCM_BOOL_F);
  94. scm_i_pthread_mutex_unlock (&overrides_lock);
  95. if (scm_is_false (ret))
  96. {
  97. if (SCM_PROGRAM_P (proc))
  98. ret = scm_i_program_properties (proc);
  99. else
  100. ret = SCM_EOL;
  101. }
  102. return ret;
  103. }
  104. #undef FUNC_NAME
  105. SCM_DEFINE (scm_set_procedure_properties_x, "set-procedure-properties!", 2, 0, 0,
  106. (SCM proc, SCM alist),
  107. "Set @var{proc}'s property list to @var{alist}.")
  108. #define FUNC_NAME s_scm_set_procedure_properties_x
  109. {
  110. SCM_VALIDATE_PROC (1, proc);
  111. scm_i_pthread_mutex_lock (&overrides_lock);
  112. scm_hashq_set_x (overrides, proc, alist);
  113. scm_i_pthread_mutex_unlock (&overrides_lock);
  114. return SCM_UNSPECIFIED;
  115. }
  116. #undef FUNC_NAME
  117. SCM_DEFINE (scm_procedure_property, "procedure-property", 2, 0, 0,
  118. (SCM proc, SCM key),
  119. "Return the property of @var{proc} with name @var{key}.")
  120. #define FUNC_NAME s_scm_procedure_property
  121. {
  122. SCM_VALIDATE_PROC (1, proc);
  123. return scm_assq_ref (scm_procedure_properties (proc), key);
  124. }
  125. #undef FUNC_NAME
  126. SCM_DEFINE (scm_set_procedure_property_x, "set-procedure-property!", 3, 0, 0,
  127. (SCM proc, SCM key, SCM val),
  128. "In @var{proc}'s property list, set the property named @var{key} to\n"
  129. "@var{val}.")
  130. #define FUNC_NAME s_scm_set_procedure_property_x
  131. {
  132. SCM props;
  133. SCM_VALIDATE_PROC (1, proc);
  134. props = scm_procedure_properties (proc);
  135. scm_i_pthread_mutex_lock (&overrides_lock);
  136. scm_hashq_set_x (overrides, proc, scm_assq_set_x (props, key, val));
  137. scm_i_pthread_mutex_unlock (&overrides_lock);
  138. return SCM_UNSPECIFIED;
  139. }
  140. #undef FUNC_NAME
  141. void
  142. scm_init_procprop ()
  143. {
  144. overrides = scm_make_weak_key_hash_table (SCM_UNDEFINED);
  145. #include "libguile/procprop.x"
  146. }
  147. /*
  148. Local Variables:
  149. c-file-style: "gnu"
  150. End:
  151. */