Critical sections in guardians do not need to block asyncs
[bpt/guile.git] / libguile / procs.c
1 /* Copyright (C) 1995, 1996, 1997, 1999, 2000, 2001, 2006, 2008, 2009,
2 * 2010, 2011, 2012 Free Software Foundation, Inc.
3 *
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.
8 *
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.
13 *
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
17 * 02110-1301 USA
18 */
19
20
21 \f
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include "libguile/_scm.h"
27
28 #include "libguile/strings.h"
29 #include "libguile/vectors.h"
30 #include "libguile/smob.h"
31 #include "libguile/deprecation.h"
32
33 #include "libguile/validate.h"
34 #include "libguile/procs.h"
35 #include "libguile/procprop.h"
36 #include "libguile/objcodes.h"
37 #include "libguile/programs.h"
38 \f
39
40
41 /* {Procedures}
42 */
43
44
45 SCM_DEFINE (scm_procedure_p, "procedure?", 1, 0, 0,
46 (SCM obj),
47 "Return @code{#t} if @var{obj} is a procedure.")
48 #define FUNC_NAME s_scm_procedure_p
49 {
50 if (SCM_NIMP (obj))
51 switch (SCM_TYP7 (obj))
52 {
53 case scm_tcs_struct:
54 if (!((SCM_OBJ_CLASS_FLAGS (obj) & SCM_CLASSF_PURE_GENERIC)
55 || SCM_STRUCT_APPLICABLE_P (obj)))
56 break;
57 case scm_tc7_program:
58 return SCM_BOOL_T;
59 case scm_tc7_smob:
60 return scm_from_bool (SCM_SMOB_DESCRIPTOR (obj).apply);
61 default:
62 return SCM_BOOL_F;
63 }
64 return SCM_BOOL_F;
65 }
66 #undef FUNC_NAME
67
68 SCM_DEFINE (scm_thunk_p, "thunk?", 1, 0, 0,
69 (SCM obj),
70 "Return @code{#t} if @var{obj} is a thunk.")
71 #define FUNC_NAME s_scm_thunk_p
72 {
73 int req, opt, rest;
74 return scm_from_bool (scm_i_procedure_arity (obj, &req, &opt, &rest)
75 && req == 0);
76 }
77 #undef FUNC_NAME
78
79 SCM_GLOBAL_SYMBOL (scm_sym_documentation, "documentation");
80
81 SCM_DEFINE (scm_procedure_documentation, "procedure-documentation", 1, 0, 0,
82 (SCM proc),
83 "Return the documentation string associated with @code{proc}. By\n"
84 "convention, if a procedure contains more than one expression and the\n"
85 "first expression is a string constant, that string is assumed to contain\n"
86 "documentation for that procedure.")
87 #define FUNC_NAME s_scm_procedure_documentation
88 {
89 SCM_VALIDATE_PROC (SCM_ARG1, proc);
90 return scm_procedure_property (proc, scm_sym_documentation);
91 }
92 #undef FUNC_NAME
93
94
95 /* Procedure-with-setter
96 */
97
98 static SCM pws_vtable;
99
100
101 SCM_DEFINE (scm_procedure_with_setter_p, "procedure-with-setter?", 1, 0, 0,
102 (SCM obj),
103 "Return @code{#t} if @var{obj} is a procedure with an\n"
104 "associated setter procedure.")
105 #define FUNC_NAME s_scm_procedure_with_setter_p
106 {
107 return scm_from_bool (SCM_STRUCTP (obj) && SCM_STRUCT_SETTER_P (obj));
108 }
109 #undef FUNC_NAME
110
111 SCM_DEFINE (scm_make_procedure_with_setter, "make-procedure-with-setter", 2, 0, 0,
112 (SCM procedure, SCM setter),
113 "Create a new procedure which behaves like @var{procedure}, but\n"
114 "with the associated setter @var{setter}.")
115 #define FUNC_NAME s_scm_make_procedure_with_setter
116 {
117 SCM name, ret;
118 SCM_VALIDATE_PROC (1, procedure);
119 SCM_VALIDATE_PROC (2, setter);
120 ret = scm_make_struct (pws_vtable, SCM_INUM0,
121 scm_list_2 (procedure, setter));
122
123 /* don't use procedure_name, because don't care enough to do a reverse
124 lookup */
125 name = scm_procedure_property (procedure, scm_sym_name);
126 if (scm_is_true (name))
127 scm_set_procedure_property_x (ret, scm_sym_name, name);
128 return ret;
129 }
130 #undef FUNC_NAME
131
132 SCM_DEFINE (scm_procedure, "procedure", 1, 0, 0,
133 (SCM proc),
134 "Return the procedure of @var{proc}, which must be an\n"
135 "applicable struct.")
136 #define FUNC_NAME s_scm_procedure
137 {
138 SCM_ASSERT (SCM_STRUCTP (proc) && SCM_STRUCT_APPLICABLE_P (proc),
139 proc, SCM_ARG1, FUNC_NAME);
140 return SCM_STRUCT_PROCEDURE (proc);
141 }
142 #undef FUNC_NAME
143
144 SCM_PRIMITIVE_GENERIC (scm_setter, "setter", 1, 0, 0,
145 (SCM proc),
146 "Return the setter of @var{proc}, which must be an\n"
147 "applicable struct with a setter.")
148 #define FUNC_NAME s_scm_setter
149 {
150 SCM_GASSERT1 (SCM_STRUCTP (proc), g_scm_setter, proc, SCM_ARG1, FUNC_NAME);
151 if (SCM_STRUCT_SETTER_P (proc))
152 return SCM_STRUCT_SETTER (proc);
153 if (SCM_PUREGENERICP (proc)
154 && SCM_IS_A_P (proc, scm_class_generic_with_setter))
155 /* FIXME: might not be an accessor */
156 return SCM_GENERIC_SETTER (proc);
157 SCM_WTA_DISPATCH_1 (g_scm_setter, proc, SCM_ARG1, FUNC_NAME);
158 return SCM_BOOL_F; /* not reached */
159 }
160 #undef FUNC_NAME
161
162 \f
163 void
164 scm_init_procs ()
165 {
166 pws_vtable =
167 scm_c_make_struct (scm_applicable_struct_with_setter_vtable_vtable,
168 0,
169 1,
170 SCM_UNPACK (scm_from_latin1_symbol ("pwpw")));
171
172 #include "libguile/procs.x"
173 }
174
175 /*
176 Local Variables:
177 c-file-style: "gnu"
178 End:
179 */