Commit | Line | Data |
---|---|---|
fd12a19a | 1 | /* Copyright (C) 1995,1996,1998,2000,2001,2003,2004, 2006, 2008, 2009, 2010 Free Software Foundation, Inc. |
0f2d19dd | 2 | * |
73be1d9e | 3 | * This library is free software; you can redistribute it and/or |
53befeb7 NJ |
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. | |
0f2d19dd | 7 | * |
53befeb7 NJ |
8 | * This library is distributed in the hope that it will be useful, but |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
73be1d9e MV |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
11 | * Lesser General Public License for more details. | |
0f2d19dd | 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 | |
53befeb7 NJ |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
16 | * 02110-1301 USA | |
73be1d9e | 17 | */ |
1bbd0b84 | 18 | |
1bbd0b84 | 19 | |
0f2d19dd | 20 | \f |
dbb605f5 LC |
21 | #ifdef HAVE_CONFIG_H |
22 | # include <config.h> | |
23 | #endif | |
0f2d19dd | 24 | |
a0599745 MD |
25 | #include "libguile/_scm.h" |
26 | ||
27 | #include "libguile/alist.h" | |
28 | #include "libguile/eval.h" | |
29 | #include "libguile/procs.h" | |
30 | #include "libguile/gsubr.h" | |
5540e847 | 31 | #include "libguile/smob.h" |
a0599745 MD |
32 | #include "libguile/root.h" |
33 | #include "libguile/vectors.h" | |
82c76fd3 | 34 | #include "libguile/hashtab.h" |
2fb924f6 | 35 | #include "libguile/programs.h" |
a0599745 MD |
36 | |
37 | #include "libguile/validate.h" | |
38 | #include "libguile/procprop.h" | |
0f2d19dd JB |
39 | \f |
40 | ||
c083a529 | 41 | SCM_GLOBAL_SYMBOL (scm_sym_system_procedure, "system-procedure"); |
67e60655 | 42 | SCM_GLOBAL_SYMBOL (scm_sym_arity, "arity"); |
fd12a19a | 43 | SCM_GLOBAL_SYMBOL (scm_sym_name, "name"); |
67e60655 | 44 | |
314b8716 AW |
45 | static SCM props; |
46 | static scm_i_pthread_mutex_t props_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER; | |
56164a5a | 47 | |
314b8716 AW |
48 | int |
49 | scm_i_procedure_arity (SCM proc, int *req, int *opt, int *rest) | |
67e60655 | 50 | { |
75c3ed28 | 51 | while (!SCM_PROGRAM_P (proc)) |
67e60655 | 52 | { |
75c3ed28 | 53 | if (SCM_IMP (proc)) |
314b8716 | 54 | return 0; |
75c3ed28 AW |
55 | switch (SCM_TYP7 (proc)) |
56 | { | |
57 | case scm_tc7_smob: | |
58 | if (!SCM_SMOB_APPLICABLE_P (proc)) | |
59 | return 0; | |
60 | proc = scm_i_smob_apply_trampoline (proc); | |
61 | break; | |
62 | case scm_tcs_struct: | |
63 | if (!SCM_STRUCT_APPLICABLE_P (proc)) | |
64 | return 0; | |
65 | proc = SCM_STRUCT_PROCEDURE (proc); | |
66 | break; | |
67 | default: | |
68 | return 0; | |
69 | } | |
67e60655 | 70 | } |
75c3ed28 | 71 | return scm_i_program_arity (proc, req, opt, rest); |
67e60655 MD |
72 | } |
73 | ||
56164a5a AW |
74 | /* FIXME: instead of the weak hash, perhaps for some kinds of procedures, use |
75 | other means; for example subrs have their own property slot, which is unused | |
76 | at present. */ | |
0f2d19dd | 77 | |
3b3b36dd | 78 | SCM_DEFINE (scm_procedure_properties, "procedure-properties", 1, 0, 0, |
1bbd0b84 | 79 | (SCM proc), |
b380b885 | 80 | "Return @var{obj}'s property list.") |
1bbd0b84 | 81 | #define FUNC_NAME s_scm_procedure_properties |
0f2d19dd | 82 | { |
314b8716 AW |
83 | SCM ret; |
84 | int req, opt, rest; | |
56164a5a | 85 | |
34d19ef6 | 86 | SCM_VALIDATE_PROC (1, proc); |
314b8716 AW |
87 | |
88 | scm_i_pthread_mutex_lock (&props_lock); | |
89 | ret = scm_hashq_ref (props, proc, SCM_EOL); | |
90 | scm_i_pthread_mutex_unlock (&props_lock); | |
91 | ||
92 | scm_i_procedure_arity (proc, &req, &opt, &rest); | |
93 | ||
94 | return scm_acons (scm_sym_arity, | |
95 | scm_list_3 (scm_from_int (req), | |
96 | scm_from_int (opt), | |
97 | scm_from_bool (rest)), | |
98 | ret); | |
0f2d19dd | 99 | } |
1bbd0b84 | 100 | #undef FUNC_NAME |
0f2d19dd | 101 | |
3b3b36dd | 102 | SCM_DEFINE (scm_set_procedure_properties_x, "set-procedure-properties!", 2, 0, 0, |
56164a5a AW |
103 | (SCM proc, SCM alist), |
104 | "Set @var{proc}'s property list to @var{alist}.") | |
1bbd0b84 | 105 | #define FUNC_NAME s_scm_set_procedure_properties_x |
0f2d19dd | 106 | { |
56164a5a AW |
107 | SCM_VALIDATE_PROC (1, proc); |
108 | ||
314b8716 AW |
109 | scm_i_pthread_mutex_lock (&props_lock); |
110 | scm_hashq_set_x (props, proc, alist); | |
111 | scm_i_pthread_mutex_unlock (&props_lock); | |
112 | ||
0f2d19dd JB |
113 | return SCM_UNSPECIFIED; |
114 | } | |
1bbd0b84 | 115 | #undef FUNC_NAME |
0f2d19dd | 116 | |
3b3b36dd | 117 | SCM_DEFINE (scm_procedure_property, "procedure-property", 2, 0, 0, |
56164a5a AW |
118 | (SCM proc, SCM key), |
119 | "Return the property of @var{proc} with name @var{key}.") | |
1bbd0b84 | 120 | #define FUNC_NAME s_scm_procedure_property |
0f2d19dd | 121 | { |
56164a5a AW |
122 | SCM_VALIDATE_PROC (1, proc); |
123 | ||
124 | if (scm_is_eq (key, scm_sym_arity)) | |
125 | /* avoid a cons in this case */ | |
314b8716 AW |
126 | { |
127 | int req, opt, rest; | |
128 | scm_i_procedure_arity (proc, &req, &opt, &rest); | |
129 | return scm_list_3 (scm_from_int (req), | |
130 | scm_from_int (opt), | |
131 | scm_from_bool (rest)); | |
132 | } | |
56164a5a | 133 | else |
c783b082 | 134 | { |
314b8716 AW |
135 | SCM ret; |
136 | ||
137 | scm_i_pthread_mutex_lock (&props_lock); | |
138 | ret = scm_hashq_ref (props, proc, SCM_EOL); | |
139 | scm_i_pthread_mutex_unlock (&props_lock); | |
140 | ||
141 | return scm_assq_ref (ret, key); | |
c783b082 | 142 | } |
0f2d19dd | 143 | } |
1bbd0b84 | 144 | #undef FUNC_NAME |
0f2d19dd | 145 | |
3b3b36dd | 146 | SCM_DEFINE (scm_set_procedure_property_x, "set-procedure-property!", 3, 0, 0, |
56164a5a AW |
147 | (SCM proc, SCM key, SCM val), |
148 | "In @var{proc}'s property list, set the property named @var{key} to\n" | |
149 | "@var{val}.") | |
1bbd0b84 | 150 | #define FUNC_NAME s_scm_set_procedure_property_x |
0f2d19dd | 151 | { |
56164a5a AW |
152 | SCM_VALIDATE_PROC (1, proc); |
153 | ||
154 | if (scm_is_eq (key, scm_sym_arity)) | |
1bbd0b84 | 155 | SCM_MISC_ERROR ("arity is a read-only property", SCM_EOL); |
c783b082 | 156 | |
314b8716 AW |
157 | scm_i_pthread_mutex_lock (&props_lock); |
158 | scm_hashq_set_x (props, proc, | |
159 | scm_assq_set_x (scm_hashq_ref (props, proc, | |
160 | SCM_EOL), | |
161 | key, val)); | |
162 | scm_i_pthread_mutex_unlock (&props_lock); | |
c783b082 | 163 | |
0f2d19dd JB |
164 | return SCM_UNSPECIFIED; |
165 | } | |
1bbd0b84 | 166 | #undef FUNC_NAME |
0f2d19dd JB |
167 | |
168 | \f | |
169 | ||
1cc91f1b | 170 | |
0f2d19dd JB |
171 | void |
172 | scm_init_procprop () | |
0f2d19dd | 173 | { |
314b8716 | 174 | props = scm_make_weak_key_hash_table (SCM_UNDEFINED); |
a0599745 | 175 | #include "libguile/procprop.x" |
0f2d19dd JB |
176 | } |
177 | ||
89e00824 ML |
178 | |
179 | /* | |
180 | Local Variables: | |
181 | c-file-style: "gnu" | |
182 | End: | |
183 | */ |