Revert "with a threaded guile, lock weak sets and tables during a fork"
[bpt/guile.git] / libguile / procprop.c
CommitLineData
fc7bd367 1/* Copyright (C) 1995,1996,1998,2000,2001,2003,2004, 2006, 2008, 2009, 2010, 2011 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"
203a92b6 34#include "libguile/weak-table.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 41SCM_GLOBAL_SYMBOL (scm_sym_system_procedure, "system-procedure");
fd12a19a 42SCM_GLOBAL_SYMBOL (scm_sym_name, "name");
67e60655 43
e1bdf9e2 44static SCM overrides;
56164a5a 45
f3cf9421
AW
46static SCM arity_overrides;
47
314b8716
AW
48int
49scm_i_procedure_arity (SCM proc, int *req, int *opt, int *rest)
67e60655 50{
f3cf9421
AW
51 SCM o;
52
b2208d2e 53 o = scm_weak_table_refq (arity_overrides, proc, SCM_BOOL_F);
f3cf9421
AW
54
55 if (scm_is_true (o))
56 {
57 *req = scm_to_int (scm_car (o));
58 *opt = scm_to_int (scm_cadr (o));
59 *rest = scm_is_true (scm_caddr (o));
60 return 1;
61 }
62
75c3ed28 63 while (!SCM_PROGRAM_P (proc))
67e60655 64 {
8b33752b 65 if (SCM_STRUCTP (proc))
75c3ed28 66 {
75c3ed28
AW
67 if (!SCM_STRUCT_APPLICABLE_P (proc))
68 return 0;
69 proc = SCM_STRUCT_PROCEDURE (proc);
75c3ed28 70 }
8b33752b
AW
71 else if (SCM_HAS_TYP7 (proc, scm_tc7_smob))
72 {
73 if (!SCM_SMOB_APPLICABLE_P (proc))
74 return 0;
75 proc = scm_i_smob_apply_trampoline (proc);
76 }
77 else
78 return 0;
67e60655 79 }
f3cf9421 80
75c3ed28 81 return scm_i_program_arity (proc, req, opt, rest);
67e60655
MD
82}
83
f3cf9421
AW
84SCM_DEFINE (scm_set_procedure_minimum_arity_x, "set-procedure-minimum-arity!",
85 4, 0, 0, (SCM proc, SCM req, SCM opt, SCM rest),
86 "")
87#define FUNC_NAME s_scm_set_procedure_minimum_arity_x
88{
89 int t SCM_UNUSED;
90
91 SCM_VALIDATE_PROC (1, proc);
92 SCM_VALIDATE_INT_COPY (2, req, t);
93 SCM_VALIDATE_INT_COPY (3, opt, t);
94 SCM_VALIDATE_BOOL (4, rest);
95
b2208d2e 96 scm_weak_table_putq_x (arity_overrides, proc, scm_list_3 (req, opt, rest));
f3cf9421
AW
97 return SCM_UNDEFINED;
98}
99#undef FUNC_NAME
100
cb2ce548
AW
101SCM_DEFINE (scm_procedure_minimum_arity, "procedure-minimum-arity", 1, 0, 0,
102 (SCM proc),
103 "Return the \"minimum arity\" of a procedure.\n\n"
104 "If the procedure has only one arity, that arity is returned\n"
105 "as a list of three values: the number of required arguments,\n"
106 "the number of optional arguments, and a boolean indicating\n"
107 "whether or not the procedure takes rest arguments.\n\n"
108 "For a case-lambda procedure, the arity returned is the one\n"
109 "with the lowest minimum number of arguments, and the highest\n"
110 "maximum number of arguments.\n\n"
111 "If it was not possible to determine the arity of the procedure,\n"
112 "@code{#f} is returned.")
113#define FUNC_NAME s_scm_procedure_minimum_arity
114{
115 int req, opt, rest;
116
117 if (scm_i_procedure_arity (proc, &req, &opt, &rest))
118 return scm_list_3 (scm_from_int (req),
119 scm_from_int (opt),
120 scm_from_bool (rest));
121 else
122 return SCM_BOOL_F;
123}
124#undef FUNC_NAME
125
3b3b36dd 126SCM_DEFINE (scm_procedure_properties, "procedure-properties", 1, 0, 0,
1bbd0b84 127 (SCM proc),
b7e64f8b 128 "Return @var{proc}'s property list.")
1bbd0b84 129#define FUNC_NAME s_scm_procedure_properties
0f2d19dd 130{
314b8716 131 SCM ret;
56164a5a 132
34d19ef6 133 SCM_VALIDATE_PROC (1, proc);
314b8716 134
203a92b6 135 ret = scm_weak_table_refq (overrides, proc, SCM_BOOL_F);
314b8716 136
e1bdf9e2
AW
137 if (scm_is_false (ret))
138 {
139 if (SCM_PROGRAM_P (proc))
07e424b7 140 ret = scm_i_program_properties (proc);
e1bdf9e2
AW
141 else
142 ret = SCM_EOL;
143 }
144
3fc7e2c1 145 return ret;
0f2d19dd 146}
1bbd0b84 147#undef FUNC_NAME
0f2d19dd 148
3b3b36dd 149SCM_DEFINE (scm_set_procedure_properties_x, "set-procedure-properties!", 2, 0, 0,
56164a5a
AW
150 (SCM proc, SCM alist),
151 "Set @var{proc}'s property list to @var{alist}.")
1bbd0b84 152#define FUNC_NAME s_scm_set_procedure_properties_x
0f2d19dd 153{
56164a5a
AW
154 SCM_VALIDATE_PROC (1, proc);
155
203a92b6 156 scm_weak_table_putq_x (overrides, proc, alist);
314b8716 157
0f2d19dd
JB
158 return SCM_UNSPECIFIED;
159}
1bbd0b84 160#undef FUNC_NAME
0f2d19dd 161
3b3b36dd 162SCM_DEFINE (scm_procedure_property, "procedure-property", 2, 0, 0,
56164a5a
AW
163 (SCM proc, SCM key),
164 "Return the property of @var{proc} with name @var{key}.")
1bbd0b84 165#define FUNC_NAME s_scm_procedure_property
0f2d19dd 166{
56164a5a
AW
167 SCM_VALIDATE_PROC (1, proc);
168
3fc7e2c1 169 return scm_assq_ref (scm_procedure_properties (proc), key);
0f2d19dd 170}
1bbd0b84 171#undef FUNC_NAME
0f2d19dd 172
3b3b36dd 173SCM_DEFINE (scm_set_procedure_property_x, "set-procedure-property!", 3, 0, 0,
56164a5a
AW
174 (SCM proc, SCM key, SCM val),
175 "In @var{proc}'s property list, set the property named @var{key} to\n"
176 "@var{val}.")
1bbd0b84 177#define FUNC_NAME s_scm_set_procedure_property_x
0f2d19dd 178{
90fa152c 179 SCM props;
56164a5a 180
e1bdf9e2 181 SCM_VALIDATE_PROC (1, proc);
3fc7e2c1 182
203a92b6
AW
183 scm_i_pthread_mutex_lock (&scm_i_misc_mutex);
184 props = scm_weak_table_refq (overrides, proc, SCM_BOOL_F);
f2ed4473
AW
185 if (scm_is_false (props))
186 {
187 if (SCM_PROGRAM_P (proc))
188 props = scm_i_program_properties (proc);
189 else
190 props = SCM_EOL;
191 }
203a92b6
AW
192 scm_weak_table_putq_x (overrides, proc, scm_assq_set_x (props, key, val));
193 scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
c783b082 194
0f2d19dd
JB
195 return SCM_UNSPECIFIED;
196}
1bbd0b84 197#undef FUNC_NAME
0f2d19dd
JB
198
199\f
200
1cc91f1b 201
0f2d19dd
JB
202void
203scm_init_procprop ()
0f2d19dd 204{
203a92b6 205 overrides = scm_c_make_weak_table (0, SCM_WEAK_TABLE_KIND_KEY);
b2208d2e 206 arity_overrides = scm_c_make_weak_table (0, SCM_WEAK_TABLE_KIND_KEY);
a0599745 207#include "libguile/procprop.x"
0f2d19dd
JB
208}
209
89e00824
ML
210
211/*
212 Local Variables:
213 c-file-style: "gnu"
214 End:
215*/