1 /* Copyright (C) 1995,1996,1998,2000,2001,2003,2004, 2006, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
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.
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.
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
25 #include "libguile/_scm.h"
27 #include "libguile/alist.h"
28 #include "libguile/eval.h"
29 #include "libguile/procs.h"
30 #include "libguile/gsubr.h"
31 #include "libguile/smob.h"
32 #include "libguile/root.h"
33 #include "libguile/vectors.h"
34 #include "libguile/hashtab.h"
35 #include "libguile/programs.h"
37 #include "libguile/validate.h"
38 #include "libguile/procprop.h"
41 SCM_GLOBAL_SYMBOL (scm_sym_system_procedure
, "system-procedure");
42 SCM_GLOBAL_SYMBOL (scm_sym_name
, "name");
45 static scm_i_pthread_mutex_t overrides_lock
= SCM_I_PTHREAD_MUTEX_INITIALIZER
;
48 scm_i_procedure_arity (SCM proc
, int *req
, int *opt
, int *rest
)
50 while (!SCM_PROGRAM_P (proc
))
54 switch (SCM_TYP7 (proc
))
57 if (!SCM_SMOB_APPLICABLE_P (proc
))
59 proc
= scm_i_smob_apply_trampoline (proc
);
62 if (!SCM_STRUCT_APPLICABLE_P (proc
))
64 proc
= SCM_STRUCT_PROCEDURE (proc
);
70 return scm_i_program_arity (proc
, req
, opt
, rest
);
73 SCM_DEFINE (scm_procedure_minimum_arity
, "procedure-minimum-arity", 1, 0, 0,
75 "Return the \"minimum arity\" of a procedure.\n\n"
76 "If the procedure has only one arity, that arity is returned\n"
77 "as a list of three values: the number of required arguments,\n"
78 "the number of optional arguments, and a boolean indicating\n"
79 "whether or not the procedure takes rest arguments.\n\n"
80 "For a case-lambda procedure, the arity returned is the one\n"
81 "with the lowest minimum number of arguments, and the highest\n"
82 "maximum number of arguments.\n\n"
83 "If it was not possible to determine the arity of the procedure,\n"
84 "@code{#f} is returned.")
85 #define FUNC_NAME s_scm_procedure_minimum_arity
89 if (scm_i_procedure_arity (proc
, &req
, &opt
, &rest
))
90 return scm_list_3 (scm_from_int (req
),
92 scm_from_bool (rest
));
98 SCM_DEFINE (scm_procedure_properties
, "procedure-properties", 1, 0, 0,
100 "Return @var{obj}'s property list.")
101 #define FUNC_NAME s_scm_procedure_properties
105 SCM_VALIDATE_PROC (1, proc
);
107 scm_i_pthread_mutex_lock (&overrides_lock
);
108 ret
= scm_hashq_ref (overrides
, proc
, SCM_BOOL_F
);
109 scm_i_pthread_mutex_unlock (&overrides_lock
);
111 if (scm_is_false (ret
))
113 if (SCM_PROGRAM_P (proc
))
114 ret
= scm_i_program_properties (proc
);
123 SCM_DEFINE (scm_set_procedure_properties_x
, "set-procedure-properties!", 2, 0, 0,
124 (SCM proc
, SCM alist
),
125 "Set @var{proc}'s property list to @var{alist}.")
126 #define FUNC_NAME s_scm_set_procedure_properties_x
128 SCM_VALIDATE_PROC (1, proc
);
130 scm_i_pthread_mutex_lock (&overrides_lock
);
131 scm_hashq_set_x (overrides
, proc
, alist
);
132 scm_i_pthread_mutex_unlock (&overrides_lock
);
134 return SCM_UNSPECIFIED
;
138 SCM_DEFINE (scm_procedure_property
, "procedure-property", 2, 0, 0,
140 "Return the property of @var{proc} with name @var{key}.")
141 #define FUNC_NAME s_scm_procedure_property
143 SCM_VALIDATE_PROC (1, proc
);
145 return scm_assq_ref (scm_procedure_properties (proc
), key
);
149 SCM_DEFINE (scm_set_procedure_property_x
, "set-procedure-property!", 3, 0, 0,
150 (SCM proc
, SCM key
, SCM val
),
151 "In @var{proc}'s property list, set the property named @var{key} to\n"
153 #define FUNC_NAME s_scm_set_procedure_property_x
157 SCM_VALIDATE_PROC (1, proc
);
159 props
= scm_procedure_properties (proc
);
161 scm_i_pthread_mutex_lock (&overrides_lock
);
162 scm_hashq_set_x (overrides
, proc
, scm_assq_set_x (props
, key
, val
));
163 scm_i_pthread_mutex_unlock (&overrides_lock
);
165 return SCM_UNSPECIFIED
;
175 overrides
= scm_make_weak_key_hash_table (SCM_UNDEFINED
);
176 #include "libguile/procprop.x"