Commit | Line | Data |
---|---|---|
d6376244 JH |
1 | /* xsutils.c |
2 | * | |
2c351e65 | 3 | * Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, |
7a3458b7 | 4 | * by Larry Wall and others |
d6376244 JH |
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 AT |
11 | /* |
12 | * "Perilous to us all are the devices of an art deeper than we possess | |
13 | * ourselves." --Gandalf | |
14 | */ | |
15 | ||
16 | ||
09bef843 SB |
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; */ |
fe20fd30 JH |
26 | PERL_XS_EXPORT_C void XS_attributes__warn_reserved(pTHX_ CV *cv); |
27 | PERL_XS_EXPORT_C void XS_attributes_reftype(pTHX_ CV *cv); | |
28 | PERL_XS_EXPORT_C void XS_attributes__modify_attrs(pTHX_ CV *cv); | |
29 | PERL_XS_EXPORT_C void XS_attributes__guess_stash(pTHX_ CV *cv); | |
30 | PERL_XS_EXPORT_C void XS_attributes__fetch_attrs(pTHX_ CV *cv); | |
31 | PERL_XS_EXPORT_C void XS_attributes_bootstrap(pTHX_ CV *cv); | |
349fd7b7 GS |
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 GS |
43 | * version checks in these bootstrap calls are optional. |
44 | */ | |
45 | ||
46 | void | |
47 | Perl_boot_core_xsutils(pTHX) | |
48 | { | |
c05e0e2f | 49 | const char file[] = __FILE__; |
349fd7b7 | 50 | |
5d1ca38b | 51 | newXS("attributes::bootstrap", XS_attributes_bootstrap, (char *)file); |
349fd7b7 GS |
52 | } |
53 | ||
349fd7b7 GS |
54 | #include "XSUB.h" |
55 | ||
56 | static int | |
acfe0abc | 57 | modify_SV_attributes(pTHX_ SV *sv, SV **retlist, SV **attrlist, int numattrs) |
09bef843 SB |
58 | { |
59 | SV *attr; | |
09bef843 SB |
60 | int nret; |
61 | ||
62 | for (nret = 0 ; numattrs && (attr = *attrlist++); numattrs--) { | |
065cbbe5 | 63 | STRLEN len; |
71a0dd65 | 64 | const char *name = SvPV_const(attr, len); |
065cbbe5 AL |
65 | const bool negated = (*name == '-'); |
66 | ||
67 | if (negated) { | |
09bef843 SB |
68 | name++; |
69 | len--; | |
70 | } | |
71 | switch (SvTYPE(sv)) { | |
72 | case SVt_PVCV: | |
73 | switch ((int)len) { | |
048e79d5 NC |
74 | #ifdef CVf_ASSERTION |
75 | case 9: | |
04851bb3 | 76 | if (memEQ(name, "assertion", 9)) { |
048e79d5 NC |
77 | if (negated) |
78 | CvFLAGS((CV*)sv) &= ~CVf_ASSERTION; | |
79 | else | |
80 | CvFLAGS((CV*)sv) |= CVf_ASSERTION; | |
81 | continue; | |
82 | } | |
83 | break; | |
84 | #endif | |
09bef843 | 85 | case 6: |
04851bb3 | 86 | switch (name[3]) { |
09bef843 SB |
87 | case 'l': |
88 | #ifdef CVf_LVALUE | |
04851bb3 | 89 | if (memEQ(name, "lvalue", 6)) { |
09bef843 SB |
90 | if (negated) |
91 | CvFLAGS((CV*)sv) &= ~CVf_LVALUE; | |
92 | else | |
93 | CvFLAGS((CV*)sv) |= CVf_LVALUE; | |
94 | continue; | |
95 | } | |
04851bb3 NC |
96 | break; |
97 | case 'k': | |
09bef843 | 98 | #endif /* defined CVf_LVALUE */ |
04851bb3 | 99 | if (memEQ(name, "locked", 6)) { |
09bef843 SB |
100 | if (negated) |
101 | CvFLAGS((CV*)sv) &= ~CVf_LOCKED; | |
102 | else | |
103 | CvFLAGS((CV*)sv) |= CVf_LOCKED; | |
104 | continue; | |
105 | } | |
106 | break; | |
04851bb3 NC |
107 | case 'h': |
108 | if (memEQ(name, "method", 6)) { | |
09bef843 SB |
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: |
04851bb3 NC |
123 | switch (name[5]) { |
124 | case 'd': | |
125 | if (memEQ(name, "share", 5)) { | |
13c1b207 DM |
126 | if (negated) |
127 | Perl_croak(aTHX_ "A variable may not be unshared"); | |
128 | SvSHARE(sv); | |
129 | continue; | |
130 | } | |
131 | break; | |
04851bb3 NC |
132 | case 'e': |
133 | if (memEQ(name, "uniqu", 5)) { | |
95f0a2f1 | 134 | if (SvTYPE(sv) == SVt_PVGV) { |
d015a557 | 135 | if (negated) { |
95f0a2f1 | 136 | GvUNIQUE_off(sv); |
d015a557 | 137 | } else { |
95f0a2f1 | 138 | GvUNIQUE_on(sv); |
d015a557 | 139 | } |
95f0a2f1 SB |
140 | } |
141 | /* Hope this came from toke.c if not a GV. */ | |
0256094b DM |
142 | continue; |
143 | } | |
144 | } | |
145 | } | |
09bef843 SB |
146 | break; |
147 | } | |
148 | /* anything recognized had a 'continue' above */ | |
149 | *retlist++ = attr; | |
150 | nret++; | |
151 | } | |
152 | ||
153 | return nret; | |
154 | } | |
155 | ||
156 | ||
09bef843 SB |
157 | |
158 | /* package attributes; */ | |
159 | ||
160 | XS(XS_attributes_bootstrap) | |
161 | { | |
162 | dXSARGS; | |
c05e0e2f | 163 | const char file[] = __FILE__; |
09bef843 | 164 | |
592f5969 MS |
165 | if( items > 1 ) |
166 | Perl_croak(aTHX_ "Usage: attributes::bootstrap $module"); | |
b7953727 | 167 | |
5d1ca38b NC |
168 | newXSproto("attributes::_warn_reserved", XS_attributes__warn_reserved, (char *)file, ""); |
169 | newXS("attributes::_modify_attrs", XS_attributes__modify_attrs, (char *)file); | |
170 | newXSproto("attributes::_guess_stash", XS_attributes__guess_stash, (char *)file, "$"); | |
171 | newXSproto("attributes::_fetch_attrs", XS_attributes__fetch_attrs, (char *)file, "$"); | |
172 | newXSproto("attributes::reftype", XS_attributes_reftype, (char *)file, "$"); | |
09bef843 SB |
173 | |
174 | XSRETURN(0); | |
175 | } | |
176 | ||
177 | XS(XS_attributes__modify_attrs) | |
178 | { | |
179 | dXSARGS; | |
180 | SV *rv, *sv; | |
181 | ||
182 | if (items < 1) { | |
183 | usage: | |
184 | Perl_croak(aTHX_ | |
185 | "Usage: attributes::_modify_attrs $reference, @attributes"); | |
186 | } | |
187 | ||
188 | rv = ST(0); | |
189 | if (!(SvOK(rv) && SvROK(rv))) | |
190 | goto usage; | |
191 | sv = SvRV(rv); | |
192 | if (items > 1) | |
acfe0abc | 193 | XSRETURN(modify_SV_attributes(aTHX_ sv, &ST(0), &ST(1), items-1)); |
09bef843 SB |
194 | |
195 | XSRETURN(0); | |
196 | } | |
197 | ||
198 | XS(XS_attributes__fetch_attrs) | |
199 | { | |
200 | dXSARGS; | |
201 | SV *rv, *sv; | |
202 | cv_flags_t cvflags; | |
203 | ||
204 | if (items != 1) { | |
205 | usage: | |
206 | Perl_croak(aTHX_ | |
207 | "Usage: attributes::_fetch_attrs $reference"); | |
208 | } | |
209 | ||
210 | rv = ST(0); | |
211 | SP -= items; | |
212 | if (!(SvOK(rv) && SvROK(rv))) | |
213 | goto usage; | |
214 | sv = SvRV(rv); | |
215 | ||
216 | switch (SvTYPE(sv)) { | |
217 | case SVt_PVCV: | |
218 | cvflags = CvFLAGS((CV*)sv); | |
219 | if (cvflags & CVf_LOCKED) | |
220 | XPUSHs(sv_2mortal(newSVpvn("locked", 6))); | |
221 | #ifdef CVf_LVALUE | |
222 | if (cvflags & CVf_LVALUE) | |
223 | XPUSHs(sv_2mortal(newSVpvn("lvalue", 6))); | |
224 | #endif | |
225 | if (cvflags & CVf_METHOD) | |
226 | XPUSHs(sv_2mortal(newSVpvn("method", 6))); | |
7fb37951 | 227 | if (GvUNIQUE(CvGV((CV*)sv))) |
95f0a2f1 SB |
228 | XPUSHs(sv_2mortal(newSVpvn("unique", 6))); |
229 | break; | |
230 | case SVt_PVGV: | |
231 | if (GvUNIQUE(sv)) | |
232 | XPUSHs(sv_2mortal(newSVpvn("unique", 6))); | |
09bef843 SB |
233 | break; |
234 | default: | |
235 | break; | |
236 | } | |
237 | ||
238 | PUTBACK; | |
239 | } | |
240 | ||
241 | XS(XS_attributes__guess_stash) | |
242 | { | |
243 | dXSARGS; | |
244 | SV *rv, *sv; | |
c72da347 | 245 | dXSTARG; |
09bef843 SB |
246 | |
247 | if (items != 1) { | |
248 | usage: | |
249 | Perl_croak(aTHX_ | |
250 | "Usage: attributes::_guess_stash $reference"); | |
251 | } | |
252 | ||
253 | rv = ST(0); | |
254 | ST(0) = TARG; | |
255 | if (!(SvOK(rv) && SvROK(rv))) | |
256 | goto usage; | |
257 | sv = SvRV(rv); | |
258 | ||
259 | if (SvOBJECT(sv)) | |
26ab6a78 | 260 | sv_setpv(TARG, HvNAME_get(SvSTASH(sv))); |
09bef843 SB |
261 | #if 0 /* this was probably a bad idea */ |
262 | else if (SvPADMY(sv)) | |
263 | sv_setsv(TARG, &PL_sv_no); /* unblessed lexical */ | |
264 | #endif | |
265 | else { | |
0e2d6244 | 266 | const HV *stash = NULL; |
09bef843 SB |
267 | switch (SvTYPE(sv)) { |
268 | case SVt_PVCV: | |
6676db26 | 269 | if (CvGV(sv) && isGV(CvGV(sv)) && GvSTASH(CvGV(sv))) |
09bef843 | 270 | stash = GvSTASH(CvGV(sv)); |
6676db26 | 271 | else if (/* !CvANON(sv) && */ CvSTASH(sv)) |
09bef843 SB |
272 | stash = CvSTASH(sv); |
273 | break; | |
274 | case SVt_PVMG: | |
14befaf4 | 275 | if (!(SvFAKE(sv) && SvTIED_mg(sv, PERL_MAGIC_glob))) |
09bef843 SB |
276 | break; |
277 | /*FALLTHROUGH*/ | |
278 | case SVt_PVGV: | |
6676db26 | 279 | if (GvGP(sv) && GvESTASH((GV*)sv)) |
09bef843 SB |
280 | stash = GvESTASH((GV*)sv); |
281 | break; | |
282 | default: | |
283 | break; | |
284 | } | |
285 | if (stash) | |
26ab6a78 | 286 | sv_setpv(TARG, HvNAME_get(stash)); |
09bef843 SB |
287 | } |
288 | ||
09bef843 | 289 | SvSETMAGIC(TARG); |
09bef843 SB |
290 | XSRETURN(1); |
291 | } | |
292 | ||
293 | XS(XS_attributes_reftype) | |
294 | { | |
295 | dXSARGS; | |
296 | SV *rv, *sv; | |
c72da347 | 297 | dXSTARG; |
09bef843 SB |
298 | |
299 | if (items != 1) { | |
300 | usage: | |
301 | Perl_croak(aTHX_ | |
302 | "Usage: attributes::reftype $reference"); | |
303 | } | |
304 | ||
305 | rv = ST(0); | |
306 | ST(0) = TARG; | |
255c29c3 | 307 | SvGETMAGIC(rv); |
121e869f | 308 | if (!(SvOK(rv) && SvROK(rv))) |
09bef843 SB |
309 | goto usage; |
310 | sv = SvRV(rv); | |
311 | sv_setpv(TARG, sv_reftype(sv, 0)); | |
09bef843 | 312 | SvSETMAGIC(TARG); |
09bef843 SB |
313 | |
314 | XSRETURN(1); | |
315 | } | |
316 | ||
317 | XS(XS_attributes__warn_reserved) | |
318 | { | |
319 | dXSARGS; | |
09bef843 SB |
320 | |
321 | if (items != 0) { | |
322 | Perl_croak(aTHX_ | |
323 | "Usage: attributes::_warn_reserved ()"); | |
324 | } | |
325 | ||
326 | EXTEND(SP,1); | |
c72da347 | 327 | ST(0) = boolSV(ckWARN(WARN_RESERVED)); |
09bef843 SB |
328 | |
329 | XSRETURN(1); | |
330 | } | |
331 | ||
d8294a4d NC |
332 | /* |
333 | * Local variables: | |
334 | * c-indentation-style: bsd | |
335 | * c-basic-offset: 4 | |
336 | * indent-tabs-mode: t | |
337 | * End: | |
338 | * | |
339 | * ex: set ts=8 sts=4 sw=4 noet: | |
340 | */ |