1 /* Copyright (C) 1995, 1996, 1998, 2000, 2001, 2003, 2004, 2006,
2 * 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 3 of
7 * the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 #define SCM_BUILDING_DEPRECATED_CODE
28 #include "libguile/_scm.h"
30 #include "libguile/alist.h"
31 #include "libguile/deprecation.h"
32 #include "libguile/deprecated.h"
33 #include "libguile/eval.h"
34 #include "libguile/procs.h"
35 #include "libguile/gsubr.h"
36 #include "libguile/smob.h"
37 #include "libguile/root.h"
38 #include "libguile/vectors.h"
39 #include "libguile/hashtab.h"
40 #include "libguile/programs.h"
42 #include "libguile/validate.h"
43 #include "libguile/procprop.h"
46 SCM_GLOBAL_SYMBOL (scm_sym_system_procedure
, "system-procedure");
47 #if (SCM_ENABLE_DEPRECATED == 1)
48 SCM_GLOBAL_SYMBOL (scm_sym_arity
, "arity");
50 SCM_GLOBAL_SYMBOL (scm_sym_name
, "name");
53 static scm_i_pthread_mutex_t overrides_lock
= SCM_I_PTHREAD_MUTEX_INITIALIZER
;
55 static SCM arity_overrides
;
58 scm_i_procedure_arity (SCM proc
, int *req
, int *opt
, int *rest
)
62 scm_i_pthread_mutex_lock (&overrides_lock
);
63 o
= scm_hashq_ref (arity_overrides
, proc
, SCM_BOOL_F
);
64 scm_i_pthread_mutex_unlock (&overrides_lock
);
68 *req
= scm_to_int (scm_car (o
));
69 *opt
= scm_to_int (scm_cadr (o
));
70 *rest
= scm_is_true (scm_caddr (o
));
74 while (!SCM_PROGRAM_P (proc
))
78 switch (SCM_TYP7 (proc
))
81 if (!SCM_SMOB_APPLICABLE_P (proc
))
83 if (!scm_i_program_arity
84 (SCM_SMOB_DESCRIPTOR (proc
).apply_trampoline_objcode
,
88 /* The trampoline gets the smob too, which users don't
94 if (!SCM_STRUCT_APPLICABLE_P (proc
))
96 proc
= SCM_STRUCT_PROCEDURE (proc
);
103 return scm_i_program_arity (proc
, req
, opt
, rest
);
106 SCM_DEFINE (scm_set_procedure_minimum_arity_x
, "set-procedure-minimum-arity!",
107 4, 0, 0, (SCM proc
, SCM req
, SCM opt
, SCM rest
),
109 #define FUNC_NAME s_scm_set_procedure_minimum_arity_x
113 SCM_VALIDATE_PROC (1, proc
);
114 SCM_VALIDATE_INT_COPY (2, req
, t
);
115 SCM_VALIDATE_INT_COPY (3, opt
, t
);
116 SCM_VALIDATE_BOOL (4, rest
);
118 scm_i_pthread_mutex_lock (&overrides_lock
);
119 scm_hashq_set_x (arity_overrides
, proc
, scm_list_3 (req
, opt
, rest
));
120 scm_i_pthread_mutex_unlock (&overrides_lock
);
121 return SCM_UNDEFINED
;
125 SCM_DEFINE (scm_procedure_minimum_arity
, "procedure-minimum-arity", 1, 0, 0,
127 "Return the \"minimum arity\" of a procedure.\n\n"
128 "If the procedure has only one arity, that arity is returned\n"
129 "as a list of three values: the number of required arguments,\n"
130 "the number of optional arguments, and a boolean indicating\n"
131 "whether or not the procedure takes rest arguments.\n\n"
132 "For a case-lambda procedure, the arity returned is the one\n"
133 "with the lowest minimum number of arguments, and the highest\n"
134 "maximum number of arguments.\n\n"
135 "If it was not possible to determine the arity of the procedure,\n"
136 "@code{#f} is returned.")
137 #define FUNC_NAME s_scm_procedure_minimum_arity
141 if (scm_i_procedure_arity (proc
, &req
, &opt
, &rest
))
142 return scm_list_3 (scm_from_int (req
),
144 scm_from_bool (rest
));
150 SCM_DEFINE (scm_procedure_properties
, "procedure-properties", 1, 0, 0,
152 "Return @var{proc}'s property list.")
153 #define FUNC_NAME s_scm_procedure_properties
157 SCM_VALIDATE_PROC (1, proc
);
159 scm_i_pthread_mutex_lock (&overrides_lock
);
160 ret
= scm_hashq_ref (overrides
, proc
, SCM_BOOL_F
);
161 scm_i_pthread_mutex_unlock (&overrides_lock
);
163 if (scm_is_false (ret
))
165 if (SCM_PROGRAM_P (proc
))
166 ret
= scm_i_program_properties (proc
);
171 #if (SCM_ENABLE_DEPRECATED == 1)
172 ret
= scm_acons (scm_sym_arity
, scm_procedure_minimum_arity (proc
), ret
);
179 SCM_DEFINE (scm_set_procedure_properties_x
, "set-procedure-properties!", 2, 0, 0,
180 (SCM proc
, SCM alist
),
181 "Set @var{proc}'s property list to @var{alist}.")
182 #define FUNC_NAME s_scm_set_procedure_properties_x
184 SCM_VALIDATE_PROC (1, proc
);
186 #if (SCM_ENABLE_DEPRECATED == 1)
187 if (scm_is_true (scm_assq (scm_sym_arity
, alist
)))
188 SCM_MISC_ERROR ("arity is a read-only property", SCM_EOL
);
191 scm_i_pthread_mutex_lock (&overrides_lock
);
192 scm_hashq_set_x (overrides
, proc
, alist
);
193 scm_i_pthread_mutex_unlock (&overrides_lock
);
195 return SCM_UNSPECIFIED
;
199 SCM_DEFINE (scm_procedure_property
, "procedure-property", 2, 0, 0,
201 "Return the property of @var{proc} with name @var{key}.")
202 #define FUNC_NAME s_scm_procedure_property
204 SCM_VALIDATE_PROC (1, proc
);
206 #if (SCM_ENABLE_DEPRECATED == 1)
207 if (scm_is_eq (key
, scm_sym_arity
))
208 scm_c_issue_deprecation_warning
209 ("Accessing a procedure's arity via `procedure-property' is deprecated.\n"
210 "Use `procedure-minimum-arity instead.");
213 return scm_assq_ref (scm_procedure_properties (proc
), key
);
217 SCM_DEFINE (scm_set_procedure_property_x
, "set-procedure-property!", 3, 0, 0,
218 (SCM proc
, SCM key
, SCM val
),
219 "In @var{proc}'s property list, set the property named @var{key} to\n"
221 #define FUNC_NAME s_scm_set_procedure_property_x
225 SCM_VALIDATE_PROC (1, proc
);
227 #if (SCM_ENABLE_DEPRECATED == 1)
228 if (scm_is_eq (key
, scm_sym_arity
))
229 SCM_MISC_ERROR ("arity is a deprecated read-only property", SCM_EOL
);
232 scm_dynwind_begin (0);
233 /* Here we must block asyncs while overrides_lock is held, to avoid
234 deadlocks which can happen as follows: scm_i_program_properties
235 calls out to the VM, which will run asyncs. Asyncs are permitted
236 to run VM code, which sometimes checks procedure properties, which
237 locks overrides_lock. */
238 scm_i_dynwind_pthread_mutex_lock_block_asyncs (&overrides_lock
);
239 props
= scm_hashq_ref (overrides
, proc
, SCM_BOOL_F
);
240 if (scm_is_false (props
))
242 if (SCM_PROGRAM_P (proc
))
243 props
= scm_i_program_properties (proc
);
247 scm_hashq_set_x (overrides
, proc
, scm_assq_set_x (props
, key
, val
));
250 return SCM_UNSPECIFIED
;
260 overrides
= scm_make_weak_key_hash_table (SCM_UNDEFINED
);
261 arity_overrides
= scm_make_weak_key_hash_table (SCM_UNDEFINED
);
262 #include "libguile/procprop.x"