Plug a memory leak (noticed by Yves)
[p5sagit/p5-mst-13.2.git] / xsutils.c
CommitLineData
d6376244 1/* xsutils.c
2 *
acde74e1 3 * Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
371fce9b 4 * by Larry Wall and others
d6376244 5 *
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
8 *
9 */
10
d31a8517 11/*
12 * "Perilous to us all are the devices of an art deeper than we possess
13 * ourselves." --Gandalf
14 */
15
16
09bef843 17#include "EXTERN.h"
18#define PERL_IN_XSUTILS_C
19#include "perl.h"
20
21/*
22 * Contributed by Spider Boardman (spider.boardman@orb.nashua.nh.us).
23 */
24
349fd7b7 25/* package attributes; */
27da23d5 26PERL_XS_EXPORT_C void XS_attributes_reftype(pTHX_ CV *cv);
27PERL_XS_EXPORT_C void XS_attributes__modify_attrs(pTHX_ CV *cv);
28PERL_XS_EXPORT_C void XS_attributes__guess_stash(pTHX_ CV *cv);
29PERL_XS_EXPORT_C void XS_attributes__fetch_attrs(pTHX_ CV *cv);
30PERL_XS_EXPORT_C void XS_attributes_bootstrap(pTHX_ CV *cv);
349fd7b7 31
32
33/*
34 * Note that only ${pkg}::bootstrap definitions should go here.
35 * This helps keep down the start-up time, which is especially
36 * relevant for users who don't invoke any features which are
37 * (partially) implemented here.
38 *
39 * The various bootstrap definitions can take care of doing
40 * package-specific newXS() calls. Since the layout of the
6a34af38 41 * bundled *.pm files is in a version-specific directory,
349fd7b7 42 * version checks in these bootstrap calls are optional.
43 */
44
a4c98449 45static const char file[] = __FILE__;
46
349fd7b7 47void
48Perl_boot_core_xsutils(pTHX)
49{
349fd7b7 50 newXS("attributes::bootstrap", XS_attributes_bootstrap, file);
51}
52
349fd7b7 53#include "XSUB.h"
54
55static int
acfe0abc 56modify_SV_attributes(pTHX_ SV *sv, SV **retlist, SV **attrlist, int numattrs)
09bef843 57{
97aff369 58 dVAR;
09bef843 59 SV *attr;
09bef843 60 int nret;
61
62 for (nret = 0 ; numattrs && (attr = *attrlist++); numattrs--) {
4373e329 63 STRLEN len;
cfd0369c 64 const char *name = SvPV_const(attr, len);
4373e329 65 const bool negated = (*name == '-');
66
67 if (negated) {
09bef843 68 name++;
69 len--;
70 }
71 switch (SvTYPE(sv)) {
72 case SVt_PVCV:
73 switch ((int)len) {
74 case 6:
8cad210e 75 switch (name[3]) {
09bef843 76#ifdef CVf_LVALUE
d5adc3a1 77 case 'l':
8cad210e 78 if (memEQ(name, "lvalue", 6)) {
09bef843 79 if (negated)
80 CvFLAGS((CV*)sv) &= ~CVf_LVALUE;
81 else
82 CvFLAGS((CV*)sv) |= CVf_LVALUE;
83 continue;
84 }
8cad210e 85 break;
d5adc3a1 86#endif
8cad210e 87 case 'k':
8cad210e 88 if (memEQ(name, "locked", 6)) {
09bef843 89 if (negated)
90 CvFLAGS((CV*)sv) &= ~CVf_LOCKED;
91 else
92 CvFLAGS((CV*)sv) |= CVf_LOCKED;
93 continue;
94 }
95 break;
8cad210e 96 case 'h':
97 if (memEQ(name, "method", 6)) {
09bef843 98 if (negated)
99 CvFLAGS((CV*)sv) &= ~CVf_METHOD;
100 else
101 CvFLAGS((CV*)sv) |= CVf_METHOD;
102 continue;
103 }
104 break;
105 }
106 break;
107 }
108 break;
109 default:
0256094b 110 switch ((int)len) {
95f0a2f1 111 case 6:
8cad210e 112 switch (name[5]) {
113 case 'd':
114 if (memEQ(name, "share", 5)) {
13c1b207 115 if (negated)
116 Perl_croak(aTHX_ "A variable may not be unshared");
117 SvSHARE(sv);
118 continue;
119 }
120 break;
8cad210e 121 case 'e':
122 if (memEQ(name, "uniqu", 5)) {
95f0a2f1 123 if (SvTYPE(sv) == SVt_PVGV) {
44f8325f 124 if (negated) {
95f0a2f1 125 GvUNIQUE_off(sv);
44f8325f 126 } else {
95f0a2f1 127 GvUNIQUE_on(sv);
44f8325f 128 }
95f0a2f1 129 }
130 /* Hope this came from toke.c if not a GV. */
0256094b 131 continue;
132 }
133 }
134 }
09bef843 135 break;
136 }
137 /* anything recognized had a 'continue' above */
138 *retlist++ = attr;
139 nret++;
140 }
141
142 return nret;
143}
144
145
09bef843 146
147/* package attributes; */
148
149XS(XS_attributes_bootstrap)
150{
97aff369 151 dVAR;
09bef843 152 dXSARGS;
58c0efa5 153 PERL_UNUSED_ARG(cv);
09bef843 154
592f5969 155 if( items > 1 )
156 Perl_croak(aTHX_ "Usage: attributes::bootstrap $module");
b7953727 157
09bef843 158 newXS("attributes::_modify_attrs", XS_attributes__modify_attrs, file);
159 newXSproto("attributes::_guess_stash", XS_attributes__guess_stash, file, "$");
160 newXSproto("attributes::_fetch_attrs", XS_attributes__fetch_attrs, file, "$");
161 newXSproto("attributes::reftype", XS_attributes_reftype, file, "$");
162
163 XSRETURN(0);
164}
165
166XS(XS_attributes__modify_attrs)
167{
97aff369 168 dVAR;
09bef843 169 dXSARGS;
170 SV *rv, *sv;
58c0efa5 171 PERL_UNUSED_ARG(cv);
09bef843 172
173 if (items < 1) {
174usage:
175 Perl_croak(aTHX_
176 "Usage: attributes::_modify_attrs $reference, @attributes");
177 }
178
179 rv = ST(0);
180 if (!(SvOK(rv) && SvROK(rv)))
181 goto usage;
182 sv = SvRV(rv);
183 if (items > 1)
acfe0abc 184 XSRETURN(modify_SV_attributes(aTHX_ sv, &ST(0), &ST(1), items-1));
09bef843 185
186 XSRETURN(0);
187}
188
189XS(XS_attributes__fetch_attrs)
190{
97aff369 191 dVAR;
09bef843 192 dXSARGS;
193 SV *rv, *sv;
194 cv_flags_t cvflags;
58c0efa5 195 PERL_UNUSED_ARG(cv);
09bef843 196
197 if (items != 1) {
198usage:
199 Perl_croak(aTHX_
200 "Usage: attributes::_fetch_attrs $reference");
201 }
202
203 rv = ST(0);
204 SP -= items;
205 if (!(SvOK(rv) && SvROK(rv)))
206 goto usage;
207 sv = SvRV(rv);
208
209 switch (SvTYPE(sv)) {
210 case SVt_PVCV:
211 cvflags = CvFLAGS((CV*)sv);
212 if (cvflags & CVf_LOCKED)
396482e1 213 XPUSHs(sv_2mortal(newSVpvs("locked")));
09bef843 214#ifdef CVf_LVALUE
215 if (cvflags & CVf_LVALUE)
396482e1 216 XPUSHs(sv_2mortal(newSVpvs("lvalue")));
09bef843 217#endif
218 if (cvflags & CVf_METHOD)
396482e1 219 XPUSHs(sv_2mortal(newSVpvs("method")));
7fb37951 220 if (GvUNIQUE(CvGV((CV*)sv)))
396482e1 221 XPUSHs(sv_2mortal(newSVpvs("unique")));
95f0a2f1 222 break;
223 case SVt_PVGV:
224 if (GvUNIQUE(sv))
396482e1 225 XPUSHs(sv_2mortal(newSVpvs("unique")));
09bef843 226 break;
227 default:
228 break;
229 }
230
231 PUTBACK;
232}
233
234XS(XS_attributes__guess_stash)
235{
97aff369 236 dVAR;
09bef843 237 dXSARGS;
238 SV *rv, *sv;
d277572a 239 dXSTARG;
58c0efa5 240 PERL_UNUSED_ARG(cv);
09bef843 241
242 if (items != 1) {
243usage:
244 Perl_croak(aTHX_
245 "Usage: attributes::_guess_stash $reference");
246 }
247
248 rv = ST(0);
249 ST(0) = TARG;
250 if (!(SvOK(rv) && SvROK(rv)))
251 goto usage;
252 sv = SvRV(rv);
253
254 if (SvOBJECT(sv))
7423f6db 255 sv_setpvn(TARG, HvNAME_get(SvSTASH(sv)), HvNAMELEN_get(SvSTASH(sv)));
09bef843 256#if 0 /* this was probably a bad idea */
257 else if (SvPADMY(sv))
258 sv_setsv(TARG, &PL_sv_no); /* unblessed lexical */
259#endif
260 else {
5c284bb0 261 const HV *stash = NULL;
09bef843 262 switch (SvTYPE(sv)) {
263 case SVt_PVCV:
6676db26 264 if (CvGV(sv) && isGV(CvGV(sv)) && GvSTASH(CvGV(sv)))
09bef843 265 stash = GvSTASH(CvGV(sv));
6676db26 266 else if (/* !CvANON(sv) && */ CvSTASH(sv))
09bef843 267 stash = CvSTASH(sv);
268 break;
09bef843 269 case SVt_PVGV:
6676db26 270 if (GvGP(sv) && GvESTASH((GV*)sv))
09bef843 271 stash = GvESTASH((GV*)sv);
272 break;
273 default:
274 break;
275 }
276 if (stash)
7423f6db 277 sv_setpvn(TARG, HvNAME_get(stash), HvNAMELEN_get(stash));
09bef843 278 }
279
09bef843 280 SvSETMAGIC(TARG);
09bef843 281 XSRETURN(1);
282}
283
284XS(XS_attributes_reftype)
285{
97aff369 286 dVAR;
09bef843 287 dXSARGS;
288 SV *rv, *sv;
d277572a 289 dXSTARG;
58c0efa5 290 PERL_UNUSED_ARG(cv);
09bef843 291
292 if (items != 1) {
293usage:
294 Perl_croak(aTHX_
295 "Usage: attributes::reftype $reference");
296 }
297
298 rv = ST(0);
299 ST(0) = TARG;
5b295bef 300 SvGETMAGIC(rv);
121e869f 301 if (!(SvOK(rv) && SvROK(rv)))
09bef843 302 goto usage;
303 sv = SvRV(rv);
304 sv_setpv(TARG, sv_reftype(sv, 0));
09bef843 305 SvSETMAGIC(TARG);
09bef843 306
307 XSRETURN(1);
308}
309
66610fdd 310/*
311 * Local variables:
312 * c-indentation-style: bsd
313 * c-basic-offset: 4
314 * indent-tabs-mode: t
315 * End:
316 *
37442d52 317 * ex: set ts=8 sts=4 sw=4 noet:
318 */