Mangle the expected output correctly for both 5.9 and 5.8
[p5sagit/p5-mst-13.2.git] / xsutils.c
CommitLineData
d6376244 1/* xsutils.c
2 *
cbdf9ef8 3 * Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005
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; */
acfe0abc 26void XS_attributes__warn_reserved(pTHX_ CV *cv);
27void XS_attributes_reftype(pTHX_ CV *cv);
28void XS_attributes__modify_attrs(pTHX_ CV *cv);
29void XS_attributes__guess_stash(pTHX_ CV *cv);
30void XS_attributes__fetch_attrs(pTHX_ CV *cv);
31void XS_attributes_bootstrap(pTHX_ CV *cv);
349fd7b7 32
33
34/*
35 * Note that only ${pkg}::bootstrap definitions should go here.
36 * This helps keep down the start-up time, which is especially
37 * relevant for users who don't invoke any features which are
38 * (partially) implemented here.
39 *
40 * The various bootstrap definitions can take care of doing
41 * package-specific newXS() calls. Since the layout of the
6a34af38 42 * bundled *.pm files is in a version-specific directory,
349fd7b7 43 * version checks in these bootstrap calls are optional.
44 */
45
46void
47Perl_boot_core_xsutils(pTHX)
48{
e1ec3a88 49 const char file[] = __FILE__;
349fd7b7 50
51 newXS("attributes::bootstrap", XS_attributes_bootstrap, file);
52}
53
349fd7b7 54#include "XSUB.h"
55
56static int
acfe0abc 57modify_SV_attributes(pTHX_ SV *sv, SV **retlist, SV **attrlist, int numattrs)
09bef843 58{
59 SV *attr;
60 char *name;
61 STRLEN len;
62 bool negated;
63 int nret;
64
65 for (nret = 0 ; numattrs && (attr = *attrlist++); numattrs--) {
66 name = SvPV(attr, len);
155aba94 67 if ((negated = (*name == '-'))) {
09bef843 68 name++;
69 len--;
70 }
71 switch (SvTYPE(sv)) {
72 case SVt_PVCV:
73 switch ((int)len) {
77c9267e 74#ifdef CVf_ASSERTION
42262798 75 case 9:
8cad210e 76 if (memEQ(name, "assertion", 9)) {
42262798 77 if (negated)
78 CvFLAGS((CV*)sv) &= ~CVf_ASSERTION;
79 else
80 CvFLAGS((CV*)sv) |= CVf_ASSERTION;
81 continue;
82 }
83 break;
77c9267e 84#endif
09bef843 85 case 6:
8cad210e 86 switch (name[3]) {
09bef843 87 case 'l':
88#ifdef CVf_LVALUE
8cad210e 89 if (memEQ(name, "lvalue", 6)) {
09bef843 90 if (negated)
91 CvFLAGS((CV*)sv) &= ~CVf_LVALUE;
92 else
93 CvFLAGS((CV*)sv) |= CVf_LVALUE;
94 continue;
95 }
8cad210e 96 break;
97 case 'k':
09bef843 98#endif /* defined CVf_LVALUE */
8cad210e 99 if (memEQ(name, "locked", 6)) {
09bef843 100 if (negated)
101 CvFLAGS((CV*)sv) &= ~CVf_LOCKED;
102 else
103 CvFLAGS((CV*)sv) |= CVf_LOCKED;
104 continue;
105 }
106 break;
8cad210e 107 case 'h':
108 if (memEQ(name, "method", 6)) {
09bef843 109 if (negated)
110 CvFLAGS((CV*)sv) &= ~CVf_METHOD;
111 else
112 CvFLAGS((CV*)sv) |= CVf_METHOD;
113 continue;
114 }
115 break;
116 }
117 break;
118 }
119 break;
120 default:
0256094b 121 switch ((int)len) {
95f0a2f1 122 case 6:
8cad210e 123 switch (name[5]) {
124 case 'd':
125 if (memEQ(name, "share", 5)) {
13c1b207 126 if (negated)
127 Perl_croak(aTHX_ "A variable may not be unshared");
128 SvSHARE(sv);
129 continue;
130 }
131 break;
8cad210e 132 case 'e':
133 if (memEQ(name, "uniqu", 5)) {
95f0a2f1 134 if (SvTYPE(sv) == SVt_PVGV) {
135 if (negated)
136 GvUNIQUE_off(sv);
137 else
138 GvUNIQUE_on(sv);
139 }
140 /* Hope this came from toke.c if not a GV. */
0256094b 141 continue;
142 }
143 }
144 }
09bef843 145 break;
146 }
147 /* anything recognized had a 'continue' above */
148 *retlist++ = attr;
149 nret++;
150 }
151
152 return nret;
153}
154
155
09bef843 156
157/* package attributes; */
158
159XS(XS_attributes_bootstrap)
160{
161 dXSARGS;
e1ec3a88 162 const char file[] = __FILE__;
6867be6d 163 (void)cv;
09bef843 164
592f5969 165 if( items > 1 )
166 Perl_croak(aTHX_ "Usage: attributes::bootstrap $module");
b7953727 167
09bef843 168 newXSproto("attributes::_warn_reserved", XS_attributes__warn_reserved, file, "");
169 newXS("attributes::_modify_attrs", XS_attributes__modify_attrs, file);
170 newXSproto("attributes::_guess_stash", XS_attributes__guess_stash, file, "$");
171 newXSproto("attributes::_fetch_attrs", XS_attributes__fetch_attrs, file, "$");
172 newXSproto("attributes::reftype", XS_attributes_reftype, file, "$");
173
174 XSRETURN(0);
175}
176
177XS(XS_attributes__modify_attrs)
178{
179 dXSARGS;
180 SV *rv, *sv;
6867be6d 181 (void)cv;
09bef843 182
183 if (items < 1) {
184usage:
185 Perl_croak(aTHX_
186 "Usage: attributes::_modify_attrs $reference, @attributes");
187 }
188
189 rv = ST(0);
190 if (!(SvOK(rv) && SvROK(rv)))
191 goto usage;
192 sv = SvRV(rv);
193 if (items > 1)
acfe0abc 194 XSRETURN(modify_SV_attributes(aTHX_ sv, &ST(0), &ST(1), items-1));
09bef843 195
196 XSRETURN(0);
197}
198
199XS(XS_attributes__fetch_attrs)
200{
201 dXSARGS;
202 SV *rv, *sv;
203 cv_flags_t cvflags;
6867be6d 204 (void)cv;
09bef843 205
206 if (items != 1) {
207usage:
208 Perl_croak(aTHX_
209 "Usage: attributes::_fetch_attrs $reference");
210 }
211
212 rv = ST(0);
213 SP -= items;
214 if (!(SvOK(rv) && SvROK(rv)))
215 goto usage;
216 sv = SvRV(rv);
217
218 switch (SvTYPE(sv)) {
219 case SVt_PVCV:
220 cvflags = CvFLAGS((CV*)sv);
221 if (cvflags & CVf_LOCKED)
222 XPUSHs(sv_2mortal(newSVpvn("locked", 6)));
223#ifdef CVf_LVALUE
224 if (cvflags & CVf_LVALUE)
225 XPUSHs(sv_2mortal(newSVpvn("lvalue", 6)));
226#endif
227 if (cvflags & CVf_METHOD)
228 XPUSHs(sv_2mortal(newSVpvn("method", 6)));
7fb37951 229 if (GvUNIQUE(CvGV((CV*)sv)))
95f0a2f1 230 XPUSHs(sv_2mortal(newSVpvn("unique", 6)));
06492da6 231 if (cvflags & CVf_ASSERTION)
232 XPUSHs(sv_2mortal(newSVpvn("assertion", 9)));
95f0a2f1 233 break;
234 case SVt_PVGV:
235 if (GvUNIQUE(sv))
236 XPUSHs(sv_2mortal(newSVpvn("unique", 6)));
09bef843 237 break;
238 default:
239 break;
240 }
241
242 PUTBACK;
243}
244
245XS(XS_attributes__guess_stash)
246{
247 dXSARGS;
248 SV *rv, *sv;
d277572a 249 dXSTARG;
6867be6d 250 (void)cv;
09bef843 251
252 if (items != 1) {
253usage:
254 Perl_croak(aTHX_
255 "Usage: attributes::_guess_stash $reference");
256 }
257
258 rv = ST(0);
259 ST(0) = TARG;
260 if (!(SvOK(rv) && SvROK(rv)))
261 goto usage;
262 sv = SvRV(rv);
263
264 if (SvOBJECT(sv))
265 sv_setpv(TARG, HvNAME(SvSTASH(sv)));
266#if 0 /* this was probably a bad idea */
267 else if (SvPADMY(sv))
268 sv_setsv(TARG, &PL_sv_no); /* unblessed lexical */
269#endif
270 else {
6867be6d 271 const HV *stash = Nullhv;
09bef843 272 switch (SvTYPE(sv)) {
273 case SVt_PVCV:
6676db26 274 if (CvGV(sv) && isGV(CvGV(sv)) && GvSTASH(CvGV(sv)))
09bef843 275 stash = GvSTASH(CvGV(sv));
6676db26 276 else if (/* !CvANON(sv) && */ CvSTASH(sv))
09bef843 277 stash = CvSTASH(sv);
278 break;
279 case SVt_PVMG:
14befaf4 280 if (!(SvFAKE(sv) && SvTIED_mg(sv, PERL_MAGIC_glob)))
09bef843 281 break;
282 /*FALLTHROUGH*/
283 case SVt_PVGV:
6676db26 284 if (GvGP(sv) && GvESTASH((GV*)sv))
09bef843 285 stash = GvESTASH((GV*)sv);
286 break;
287 default:
288 break;
289 }
290 if (stash)
291 sv_setpv(TARG, HvNAME(stash));
292 }
293
09bef843 294 SvSETMAGIC(TARG);
09bef843 295 XSRETURN(1);
296}
297
298XS(XS_attributes_reftype)
299{
300 dXSARGS;
301 SV *rv, *sv;
d277572a 302 dXSTARG;
6867be6d 303 (void)cv;
09bef843 304
305 if (items != 1) {
306usage:
307 Perl_croak(aTHX_
308 "Usage: attributes::reftype $reference");
309 }
310
311 rv = ST(0);
312 ST(0) = TARG;
4694d0ea 313 if (SvGMAGICAL(rv))
314 mg_get(rv);
121e869f 315 if (!(SvOK(rv) && SvROK(rv)))
09bef843 316 goto usage;
317 sv = SvRV(rv);
318 sv_setpv(TARG, sv_reftype(sv, 0));
09bef843 319 SvSETMAGIC(TARG);
09bef843 320
321 XSRETURN(1);
322}
323
324XS(XS_attributes__warn_reserved)
325{
326 dXSARGS;
6867be6d 327 (void)cv;
09bef843 328
329 if (items != 0) {
330 Perl_croak(aTHX_
331 "Usage: attributes::_warn_reserved ()");
332 }
333
334 EXTEND(SP,1);
0006dff7 335 ST(0) = boolSV(ckWARN(WARN_RESERVED));
09bef843 336
337 XSRETURN(1);
338}
339