procprop.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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/weak-table.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. int
  37. scm_i_procedure_arity (SCM proc, int *req, int *opt, int *rest)
  38. {
  39. while (!SCM_PROGRAM_P (proc))
  40. {
  41. if (SCM_STRUCTP (proc))
  42. {
  43. if (!SCM_STRUCT_APPLICABLE_P (proc))
  44. return 0;
  45. proc = SCM_STRUCT_PROCEDURE (proc);
  46. }
  47. else if (SCM_HAS_TYP7 (proc, scm_tc7_smob))
  48. {
  49. if (!SCM_SMOB_APPLICABLE_P (proc))
  50. return 0;
  51. proc = scm_i_smob_apply_trampoline (proc);
  52. }
  53. else
  54. return 0;
  55. }
  56. return scm_i_program_arity (proc, req, opt, rest);
  57. }
  58. SCM_DEFINE (scm_procedure_minimum_arity, "procedure-minimum-arity", 1, 0, 0,
  59. (SCM proc),
  60. "Return the \"minimum arity\" of a procedure.\n\n"
  61. "If the procedure has only one arity, that arity is returned\n"
  62. "as a list of three values: the number of required arguments,\n"
  63. "the number of optional arguments, and a boolean indicating\n"
  64. "whether or not the procedure takes rest arguments.\n\n"
  65. "For a case-lambda procedure, the arity returned is the one\n"
  66. "with the lowest minimum number of arguments, and the highest\n"
  67. "maximum number of arguments.\n\n"
  68. "If it was not possible to determine the arity of the procedure,\n"
  69. "@code{#f} is returned.")
  70. #define FUNC_NAME s_scm_procedure_minimum_arity
  71. {
  72. int req, opt, rest;
  73. if (scm_i_procedure_arity (proc, &req, &opt, &rest))
  74. return scm_list_3 (scm_from_int (req),
  75. scm_from_int (opt),
  76. scm_from_bool (rest));
  77. else
  78. return SCM_BOOL_F;
  79. }
  80. #undef FUNC_NAME
  81. SCM_DEFINE (scm_procedure_properties, "procedure-properties", 1, 0, 0,
  82. (SCM proc),
  83. "Return @var{obj}'s property list.")
  84. #define FUNC_NAME s_scm_procedure_properties
  85. {
  86. SCM ret;
  87. SCM_VALIDATE_PROC (1, proc);
  88. ret = scm_weak_table_refq (overrides, proc, SCM_BOOL_F);
  89. if (scm_is_false (ret))
  90. {
  91. if (SCM_PROGRAM_P (proc))
  92. ret = scm_i_program_properties (proc);
  93. else
  94. ret = SCM_EOL;
  95. }
  96. return ret;
  97. }
  98. #undef FUNC_NAME
  99. SCM_DEFINE (scm_set_procedure_properties_x, "set-procedure-properties!", 2, 0, 0,
  100. (SCM proc, SCM alist),
  101. "Set @var{proc}'s property list to @var{alist}.")
  102. #define FUNC_NAME s_scm_set_procedure_properties_x
  103. {
  104. SCM_VALIDATE_PROC (1, proc);
  105. scm_weak_table_putq_x (overrides, proc, alist);
  106. return SCM_UNSPECIFIED;
  107. }
  108. #undef FUNC_NAME
  109. SCM_DEFINE (scm_procedure_property, "procedure-property", 2, 0, 0,
  110. (SCM proc, SCM key),
  111. "Return the property of @var{proc} with name @var{key}.")
  112. #define FUNC_NAME s_scm_procedure_property
  113. {
  114. SCM_VALIDATE_PROC (1, proc);
  115. return scm_assq_ref (scm_procedure_properties (proc), key);
  116. }
  117. #undef FUNC_NAME
  118. SCM_DEFINE (scm_set_procedure_property_x, "set-procedure-property!", 3, 0, 0,
  119. (SCM proc, SCM key, SCM val),
  120. "In @var{proc}'s property list, set the property named @var{key} to\n"
  121. "@var{val}.")
  122. #define FUNC_NAME s_scm_set_procedure_property_x
  123. {
  124. SCM props;
  125. SCM_VALIDATE_PROC (1, proc);
  126. scm_i_pthread_mutex_lock (&scm_i_misc_mutex);
  127. props = scm_weak_table_refq (overrides, proc, SCM_BOOL_F);
  128. if (scm_is_false (props))
  129. {
  130. if (SCM_PROGRAM_P (proc))
  131. props = scm_i_program_properties (proc);
  132. else
  133. props = SCM_EOL;
  134. }
  135. scm_weak_table_putq_x (overrides, proc, scm_assq_set_x (props, key, val));
  136. scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
  137. return SCM_UNSPECIFIED;
  138. }
  139. #undef FUNC_NAME
  140. void
  141. scm_init_procprop ()
  142. {
  143. overrides = scm_c_make_weak_table (0, SCM_WEAK_TABLE_KIND_KEY);
  144. #include "libguile/procprop.x"
  145. }
  146. /*
  147. Local Variables:
  148. c-file-style: "gnu"
  149. End:
  150. */