Commit | Line | Data |
d6376244 |
1 | /* universal.c |
2 | * |
b5f8cc5c |
3 | * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, |
fdf8c088 |
4 | * 2005, 2006, 2007 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 | * "The roots of those mountains must be roots indeed; there must be |
13 | * great secrets buried there which have not been discovered since the |
14 | * beginning." --Gandalf, relating Gollum's story |
15 | */ |
16 | |
166f8a29 |
17 | /* This file contains the code that implements the functions in Perl's |
18 | * UNIVERSAL package, such as UNIVERSAL->can(). |
192b9cd1 |
19 | * |
20 | * It is also used to store XS functions that need to be present in |
21 | * miniperl for a lack of a better place to put them. It might be |
22 | * clever to move them to seperate XS files which would then be pulled |
23 | * in by some to-be-written build process. |
166f8a29 |
24 | */ |
25 | |
6d4a7be2 |
26 | #include "EXTERN.h" |
864dbfa3 |
27 | #define PERL_IN_UNIVERSAL_C |
6d4a7be2 |
28 | #include "perl.h" |
6d4a7be2 |
29 | |
39f7a870 |
30 | #ifdef USE_PERLIO |
31 | #include "perliol.h" /* For the PERLIO_F_XXX */ |
32 | #endif |
33 | |
6d4a7be2 |
34 | /* |
35 | * Contributed by Graham Barr <Graham.Barr@tiuk.ti.com> |
36 | * The main guts of traverse_isa was actually copied from gv_fetchmeth |
37 | */ |
38 | |
a9ec700e |
39 | STATIC bool |
810e6113 |
40 | S_isa_lookup(pTHX_ HV *stash, const char * const name, const HV* const name_stash) |
6d4a7be2 |
41 | { |
97aff369 |
42 | dVAR; |
e1a479c5 |
43 | AV* stash_linear_isa; |
44 | SV** svp; |
bfcb3514 |
45 | const char *hvname; |
e1a479c5 |
46 | I32 items; |
6d4a7be2 |
47 | |
301daebc |
48 | /* A stash/class can go by many names (ie. User == main::User), so |
49 | we compare the stash itself just in case */ |
34692224 |
50 | if (name_stash && ((const HV *)stash == name_stash)) |
a9ec700e |
51 | return TRUE; |
6d4a7be2 |
52 | |
bfcb3514 |
53 | hvname = HvNAME_get(stash); |
54 | |
55 | if (strEQ(hvname, name)) |
a9ec700e |
56 | return TRUE; |
6d4a7be2 |
57 | |
a1d407e8 |
58 | if (strEQ(name, "UNIVERSAL")) |
a9ec700e |
59 | return TRUE; |
a1d407e8 |
60 | |
e1a479c5 |
61 | stash_linear_isa = mro_get_linear_isa(stash); |
62 | svp = AvARRAY(stash_linear_isa) + 1; |
63 | items = AvFILLp(stash_linear_isa); |
64 | while (items--) { |
65 | SV* const basename_sv = *svp++; |
810e6113 |
66 | HV* const basestash = gv_stashsv(basename_sv, 0); |
dd69841b |
67 | if (!basestash) { |
b0c482e3 |
68 | if (ckWARN(WARN_SYNTAX)) |
e1a479c5 |
69 | Perl_warner(aTHX_ packWARN(WARN_SYNTAX), |
70 | "Can't locate package %"SVf" for the parents of %s", |
71 | SVfARG(basename_sv), hvname); |
72 | continue; |
46e4b22b |
73 | } |
e1a479c5 |
74 | if(name_stash == basestash || strEQ(name, SvPVX(basename_sv))) |
75 | return TRUE; |
6d4a7be2 |
76 | } |
77 | |
a9ec700e |
78 | return FALSE; |
6d4a7be2 |
79 | } |
80 | |
954c1994 |
81 | /* |
ccfc67b7 |
82 | =head1 SV Manipulation Functions |
83 | |
954c1994 |
84 | =for apidoc sv_derived_from |
85 | |
6885da0e |
86 | Returns a boolean indicating whether the SV is derived from the specified class |
87 | I<at the C level>. To check derivation at the Perl level, call C<isa()> as a |
88 | normal Perl method. |
954c1994 |
89 | |
90 | =cut |
91 | */ |
92 | |
55497cff |
93 | bool |
864dbfa3 |
94 | Perl_sv_derived_from(pTHX_ SV *sv, const char *name) |
55497cff |
95 | { |
97aff369 |
96 | dVAR; |
0b6f4f5c |
97 | HV *stash; |
46e4b22b |
98 | |
5b295bef |
99 | SvGETMAGIC(sv); |
55497cff |
100 | |
101 | if (SvROK(sv)) { |
0b6f4f5c |
102 | const char *type; |
55497cff |
103 | sv = SvRV(sv); |
104 | type = sv_reftype(sv,0); |
0b6f4f5c |
105 | if (type && strEQ(type,name)) |
106 | return TRUE; |
107 | stash = SvOBJECT(sv) ? SvSTASH(sv) : NULL; |
55497cff |
108 | } |
109 | else { |
da51bb9b |
110 | stash = gv_stashsv(sv, 0); |
55497cff |
111 | } |
46e4b22b |
112 | |
0b6f4f5c |
113 | if (stash) { |
da51bb9b |
114 | HV * const name_stash = gv_stashpv(name, 0); |
810e6113 |
115 | return isa_lookup(stash, name, name_stash); |
0b6f4f5c |
116 | } |
117 | else |
118 | return FALSE; |
301daebc |
119 | |
55497cff |
120 | } |
121 | |
cbc021f9 |
122 | /* |
123 | =for apidoc sv_does |
124 | |
125 | Returns a boolean indicating whether the SV performs a specific, named role. |
126 | The SV can be a Perl object or the name of a Perl class. |
127 | |
128 | =cut |
129 | */ |
130 | |
1b026014 |
131 | #include "XSUB.h" |
132 | |
cbc021f9 |
133 | bool |
134 | Perl_sv_does(pTHX_ SV *sv, const char *name) |
135 | { |
136 | const char *classname; |
137 | bool does_it; |
59e7186f |
138 | SV *methodname; |
cbc021f9 |
139 | |
140 | dSP; |
141 | ENTER; |
142 | SAVETMPS; |
143 | |
144 | SvGETMAGIC(sv); |
145 | |
146 | if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv)) |
147 | || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv)))) |
148 | return FALSE; |
149 | |
150 | if (sv_isobject(sv)) { |
151 | classname = sv_reftype(SvRV(sv),TRUE); |
152 | } else { |
94707740 |
153 | classname = SvPV_nolen(sv); |
cbc021f9 |
154 | } |
155 | |
156 | if (strEQ(name,classname)) |
157 | return TRUE; |
158 | |
159 | PUSHMARK(SP); |
160 | XPUSHs(sv); |
161 | XPUSHs(sv_2mortal(newSVpv(name, 0))); |
162 | PUTBACK; |
163 | |
59e7186f |
164 | methodname = sv_2mortal(newSVpv("isa", 0)); |
165 | /* ugly hack: use the SvSCREAM flag so S_method_common |
166 | * can figure out we're calling DOES() and not isa(), |
167 | * and report eventual errors correctly. --rgs */ |
168 | SvSCREAM_on(methodname); |
169 | call_sv(methodname, G_SCALAR | G_METHOD); |
cbc021f9 |
170 | SPAGAIN; |
171 | |
172 | does_it = SvTRUE( TOPs ); |
173 | FREETMPS; |
174 | LEAVE; |
175 | |
176 | return does_it; |
177 | } |
178 | |
80305961 |
179 | regexp * |
180 | Perl_get_re_arg( pTHX_ SV *sv, U32 flags, MAGIC **mgp) { |
181 | MAGIC *mg; |
182 | if (sv) { |
183 | if (SvMAGICAL(sv)) |
184 | mg_get(sv); |
185 | if (SvROK(sv) && |
186 | (sv = (SV*)SvRV(sv)) && /* assign deliberate */ |
187 | SvTYPE(sv) == SVt_PVMG && |
188 | (mg = mg_find(sv, PERL_MAGIC_qr))) /* assign deliberate */ |
189 | { |
190 | if (mgp) *mgp = mg; |
191 | return (regexp *)mg->mg_obj; |
192 | } |
193 | } |
194 | if (mgp) *mgp = NULL; |
195 | return ((flags && PL_curpm) ? PM_GETRE(PL_curpm) : NULL); |
196 | } |
197 | |
198 | |
27da23d5 |
199 | PERL_XS_EXPORT_C void XS_UNIVERSAL_isa(pTHX_ CV *cv); |
200 | PERL_XS_EXPORT_C void XS_UNIVERSAL_can(pTHX_ CV *cv); |
cbc021f9 |
201 | PERL_XS_EXPORT_C void XS_UNIVERSAL_DOES(pTHX_ CV *cv); |
27da23d5 |
202 | PERL_XS_EXPORT_C void XS_UNIVERSAL_VERSION(pTHX_ CV *cv); |
439cb1c4 |
203 | XS(XS_version_new); |
204 | XS(XS_version_stringify); |
205 | XS(XS_version_numify); |
9137345a |
206 | XS(XS_version_normal); |
439cb1c4 |
207 | XS(XS_version_vcmp); |
208 | XS(XS_version_boolean); |
2dfd8427 |
209 | #ifdef HASATTRIBUTE_NORETURN |
210 | XS(XS_version_noop) __attribute__noreturn__; |
211 | #else |
439cb1c4 |
212 | XS(XS_version_noop); |
2dfd8427 |
213 | #endif |
c8d69e4a |
214 | XS(XS_version_is_alpha); |
137d6fc0 |
215 | XS(XS_version_qv); |
8800c35a |
216 | XS(XS_utf8_is_utf8); |
1b026014 |
217 | XS(XS_utf8_valid); |
218 | XS(XS_utf8_encode); |
219 | XS(XS_utf8_decode); |
220 | XS(XS_utf8_upgrade); |
221 | XS(XS_utf8_downgrade); |
222 | XS(XS_utf8_unicode_to_native); |
223 | XS(XS_utf8_native_to_unicode); |
29569577 |
224 | XS(XS_Internals_SvREADONLY); |
225 | XS(XS_Internals_SvREFCNT); |
f044d0d1 |
226 | XS(XS_Internals_hv_clear_placehold); |
39f7a870 |
227 | XS(XS_PerlIO_get_layers); |
39cff0d9 |
228 | XS(XS_Regexp_DESTROY); |
9a7034eb |
229 | XS(XS_Internals_hash_seed); |
008fb0c0 |
230 | XS(XS_Internals_rehash_seed); |
05619474 |
231 | XS(XS_Internals_HvREHASH); |
e1234d8e |
232 | XS(XS_Internals_inc_sub_generation); |
80305961 |
233 | XS(XS_re_is_regexp); |
192b9cd1 |
234 | XS(XS_re_regname); |
235 | XS(XS_re_regnames); |
80305961 |
236 | XS(XS_re_regnames_count); |
192b9cd1 |
237 | XS(XS_Tie_Hash_NamedCapture_FETCH); |
238 | XS(XS_Tie_Hash_NamedCapture_STORE); |
239 | XS(XS_Tie_Hash_NamedCapture_DELETE); |
240 | XS(XS_Tie_Hash_NamedCapture_CLEAR); |
241 | XS(XS_Tie_Hash_NamedCapture_EXISTS); |
242 | XS(XS_Tie_Hash_NamedCapture_FIRSTKEY); |
243 | XS(XS_Tie_Hash_NamedCapture_NEXTKEY); |
244 | XS(XS_Tie_Hash_NamedCapture_SCALAR); |
245 | XS(XS_Tie_Hash_NamedCapture_flags); |
0cb96387 |
246 | |
247 | void |
248 | Perl_boot_core_UNIVERSAL(pTHX) |
249 | { |
97aff369 |
250 | dVAR; |
157e3fc8 |
251 | static const char file[] = __FILE__; |
0cb96387 |
252 | |
253 | newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file); |
254 | newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file); |
cbc021f9 |
255 | newXS("UNIVERSAL::DOES", XS_UNIVERSAL_DOES, file); |
0cb96387 |
256 | newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file); |
439cb1c4 |
257 | { |
ad63d80f |
258 | /* register the overloading (type 'A') magic */ |
259 | PL_amagic_generation++; |
439cb1c4 |
260 | /* Make it findable via fetchmethod */ |
be2ebcad |
261 | newXS("version::()", XS_version_noop, file); |
439cb1c4 |
262 | newXS("version::new", XS_version_new, file); |
263 | newXS("version::(\"\"", XS_version_stringify, file); |
264 | newXS("version::stringify", XS_version_stringify, file); |
265 | newXS("version::(0+", XS_version_numify, file); |
266 | newXS("version::numify", XS_version_numify, file); |
9137345a |
267 | newXS("version::normal", XS_version_normal, file); |
439cb1c4 |
268 | newXS("version::(cmp", XS_version_vcmp, file); |
269 | newXS("version::(<=>", XS_version_vcmp, file); |
270 | newXS("version::vcmp", XS_version_vcmp, file); |
271 | newXS("version::(bool", XS_version_boolean, file); |
272 | newXS("version::boolean", XS_version_boolean, file); |
273 | newXS("version::(nomethod", XS_version_noop, file); |
274 | newXS("version::noop", XS_version_noop, file); |
c8d69e4a |
275 | newXS("version::is_alpha", XS_version_is_alpha, file); |
137d6fc0 |
276 | newXS("version::qv", XS_version_qv, file); |
439cb1c4 |
277 | } |
8800c35a |
278 | newXS("utf8::is_utf8", XS_utf8_is_utf8, file); |
1b026014 |
279 | newXS("utf8::valid", XS_utf8_valid, file); |
280 | newXS("utf8::encode", XS_utf8_encode, file); |
281 | newXS("utf8::decode", XS_utf8_decode, file); |
282 | newXS("utf8::upgrade", XS_utf8_upgrade, file); |
283 | newXS("utf8::downgrade", XS_utf8_downgrade, file); |
284 | newXS("utf8::native_to_unicode", XS_utf8_native_to_unicode, file); |
285 | newXS("utf8::unicode_to_native", XS_utf8_unicode_to_native, file); |
29569577 |
286 | newXSproto("Internals::SvREADONLY",XS_Internals_SvREADONLY, file, "\\[$%@];$"); |
287 | newXSproto("Internals::SvREFCNT",XS_Internals_SvREFCNT, file, "\\[$%@];$"); |
dfd4ef2f |
288 | newXSproto("Internals::hv_clear_placeholders", |
f044d0d1 |
289 | XS_Internals_hv_clear_placehold, file, "\\%"); |
9d569fce |
290 | newXSproto("PerlIO::get_layers", |
291 | XS_PerlIO_get_layers, file, "*;@"); |
39cff0d9 |
292 | newXS("Regexp::DESTROY", XS_Regexp_DESTROY, file); |
9a7034eb |
293 | newXSproto("Internals::hash_seed",XS_Internals_hash_seed, file, ""); |
008fb0c0 |
294 | newXSproto("Internals::rehash_seed",XS_Internals_rehash_seed, file, ""); |
05619474 |
295 | newXSproto("Internals::HvREHASH", XS_Internals_HvREHASH, file, "\\%"); |
80305961 |
296 | newXSproto("re::is_regexp", XS_re_is_regexp, file, "$"); |
28d8d7f4 |
297 | newXSproto("re::regname", XS_re_regname, file, ";$$"); |
298 | newXSproto("re::regnames", XS_re_regnames, file, ";$"); |
28d8d7f4 |
299 | newXSproto("re::regnames_count", XS_re_regnames_count, file, ""); |
192b9cd1 |
300 | newXS("Tie::Hash::NamedCapture::FETCH", XS_Tie_Hash_NamedCapture_FETCH, file); |
301 | newXS("Tie::Hash::NamedCapture::STORE", XS_Tie_Hash_NamedCapture_STORE, file); |
302 | newXS("Tie::Hash::NamedCapture::DELETE", XS_Tie_Hash_NamedCapture_DELETE, file); |
303 | newXS("Tie::Hash::NamedCapture::CLEAR", XS_Tie_Hash_NamedCapture_CLEAR, file); |
304 | newXS("Tie::Hash::NamedCapture::EXISTS", XS_Tie_Hash_NamedCapture_EXISTS, file); |
305 | newXS("Tie::Hash::NamedCapture::FIRSTKEY", XS_Tie_Hash_NamedCapture_FIRSTKEY, file); |
306 | newXS("Tie::Hash::NamedCapture::NEXTKEY", XS_Tie_Hash_NamedCapture_NEXTKEY, file); |
307 | newXS("Tie::Hash::NamedCapture::SCALAR", XS_Tie_Hash_NamedCapture_SCALAR, file); |
308 | newXS("Tie::Hash::NamedCapture::flags", XS_Tie_Hash_NamedCapture_flags, file); |
0cb96387 |
309 | } |
310 | |
55497cff |
311 | |
6d4a7be2 |
312 | XS(XS_UNIVERSAL_isa) |
313 | { |
97aff369 |
314 | dVAR; |
6d4a7be2 |
315 | dXSARGS; |
58c0efa5 |
316 | PERL_UNUSED_ARG(cv); |
6d4a7be2 |
317 | |
318 | if (items != 2) |
cea2e8a9 |
319 | Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)"); |
c4420975 |
320 | else { |
321 | SV * const sv = ST(0); |
322 | const char *name; |
6d4a7be2 |
323 | |
c4420975 |
324 | SvGETMAGIC(sv); |
d3f7f2b2 |
325 | |
c4420975 |
326 | if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv)) |
327 | || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv)))) |
328 | XSRETURN_UNDEF; |
f8f70380 |
329 | |
c4420975 |
330 | name = SvPV_nolen_const(ST(1)); |
6d4a7be2 |
331 | |
c4420975 |
332 | ST(0) = boolSV(sv_derived_from(sv, name)); |
333 | XSRETURN(1); |
334 | } |
6d4a7be2 |
335 | } |
336 | |
6d4a7be2 |
337 | XS(XS_UNIVERSAL_can) |
338 | { |
97aff369 |
339 | dVAR; |
6d4a7be2 |
340 | dXSARGS; |
341 | SV *sv; |
6867be6d |
342 | const char *name; |
6d4a7be2 |
343 | SV *rv; |
6f08146e |
344 | HV *pkg = NULL; |
58c0efa5 |
345 | PERL_UNUSED_ARG(cv); |
6d4a7be2 |
346 | |
347 | if (items != 2) |
cea2e8a9 |
348 | Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)"); |
6d4a7be2 |
349 | |
350 | sv = ST(0); |
f8f70380 |
351 | |
5b295bef |
352 | SvGETMAGIC(sv); |
d3f7f2b2 |
353 | |
253ecd6d |
354 | if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv)) |
355 | || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv)))) |
f8f70380 |
356 | XSRETURN_UNDEF; |
357 | |
0510663f |
358 | name = SvPV_nolen_const(ST(1)); |
3280af22 |
359 | rv = &PL_sv_undef; |
6d4a7be2 |
360 | |
46e4b22b |
361 | if (SvROK(sv)) { |
6f08146e |
362 | sv = (SV*)SvRV(sv); |
46e4b22b |
363 | if (SvOBJECT(sv)) |
6f08146e |
364 | pkg = SvSTASH(sv); |
365 | } |
366 | else { |
da51bb9b |
367 | pkg = gv_stashsv(sv, 0); |
6f08146e |
368 | } |
369 | |
370 | if (pkg) { |
c4420975 |
371 | GV * const gv = gv_fetchmethod_autoload(pkg, name, FALSE); |
dc848c6f |
372 | if (gv && isGV(gv)) |
373 | rv = sv_2mortal(newRV((SV*)GvCV(gv))); |
6d4a7be2 |
374 | } |
375 | |
376 | ST(0) = rv; |
377 | XSRETURN(1); |
378 | } |
379 | |
cbc021f9 |
380 | XS(XS_UNIVERSAL_DOES) |
381 | { |
382 | dVAR; |
383 | dXSARGS; |
58c0efa5 |
384 | PERL_UNUSED_ARG(cv); |
cbc021f9 |
385 | |
386 | if (items != 2) |
26be3db7 |
387 | Perl_croak(aTHX_ "Usage: invocant->DOES(kind)"); |
cbc021f9 |
388 | else { |
389 | SV * const sv = ST(0); |
390 | const char *name; |
391 | |
392 | name = SvPV_nolen_const(ST(1)); |
393 | if (sv_does( sv, name )) |
394 | XSRETURN_YES; |
395 | |
396 | XSRETURN_NO; |
397 | } |
398 | } |
399 | |
6d4a7be2 |
400 | XS(XS_UNIVERSAL_VERSION) |
401 | { |
97aff369 |
402 | dVAR; |
6d4a7be2 |
403 | dXSARGS; |
404 | HV *pkg; |
405 | GV **gvp; |
406 | GV *gv; |
407 | SV *sv; |
e1ec3a88 |
408 | const char *undef; |
58c0efa5 |
409 | PERL_UNUSED_ARG(cv); |
6d4a7be2 |
410 | |
1571675a |
411 | if (SvROK(ST(0))) { |
6d4a7be2 |
412 | sv = (SV*)SvRV(ST(0)); |
1571675a |
413 | if (!SvOBJECT(sv)) |
cea2e8a9 |
414 | Perl_croak(aTHX_ "Cannot find version of an unblessed reference"); |
6d4a7be2 |
415 | pkg = SvSTASH(sv); |
416 | } |
417 | else { |
da51bb9b |
418 | pkg = gv_stashsv(ST(0), 0); |
6d4a7be2 |
419 | } |
420 | |
4608196e |
421 | gvp = pkg ? (GV**)hv_fetchs(pkg, "VERSION", FALSE) : NULL; |
6d4a7be2 |
422 | |
0008872a |
423 | if (gvp && isGV(gv = *gvp) && (sv = GvSV(gv)) && SvOK(sv)) { |
c4420975 |
424 | SV * const nsv = sv_newmortal(); |
6d4a7be2 |
425 | sv_setsv(nsv, sv); |
426 | sv = nsv; |
137d6fc0 |
427 | if ( !sv_derived_from(sv, "version")) |
ac0e6a2f |
428 | upg_version(sv, FALSE); |
c445ea15 |
429 | undef = NULL; |
6d4a7be2 |
430 | } |
431 | else { |
3280af22 |
432 | sv = (SV*)&PL_sv_undef; |
6d4a7be2 |
433 | undef = "(undef)"; |
434 | } |
435 | |
1571675a |
436 | if (items > 1) { |
1571675a |
437 | SV *req = ST(1); |
438 | |
62658f4d |
439 | if (undef) { |
bfcb3514 |
440 | if (pkg) { |
c4420975 |
441 | const char * const name = HvNAME_get(pkg); |
a3b680e6 |
442 | Perl_croak(aTHX_ |
bfcb3514 |
443 | "%s does not define $%s::VERSION--version check failed", |
444 | name, name); |
445 | } else { |
a3b680e6 |
446 | Perl_croak(aTHX_ |
447 | "%s defines neither package nor VERSION--version check failed", |
0510663f |
448 | SvPVx_nolen_const(ST(0)) ); |
62658f4d |
449 | } |
450 | } |
ad63d80f |
451 | |
137d6fc0 |
452 | if ( !sv_derived_from(req, "version")) { |
453 | /* req may very well be R/O, so create a new object */ |
ac0e6a2f |
454 | req = sv_2mortal( new_version(req) ); |
137d6fc0 |
455 | } |
1571675a |
456 | |
ac0e6a2f |
457 | if ( vcmp( req, sv ) > 0 ) { |
458 | if ( hv_exists((HV*)SvRV(req), "qv", 2 ) ) { |
459 | Perl_croak(aTHX_ "%s version %"SVf" required--" |
460 | "this is only version %"SVf"", HvNAME_get(pkg), |
be2597df |
461 | SVfARG(vnormal(req)), |
be2597df |
462 | SVfARG(vnormal(sv))); |
ac0e6a2f |
463 | } else { |
464 | Perl_croak(aTHX_ "%s version %"SVf" required--" |
465 | "this is only version %"SVf"", HvNAME_get(pkg), |
8cb289bd |
466 | SVfARG(vstringify(req)), |
467 | SVfARG(vstringify(sv))); |
ac0e6a2f |
468 | } |
469 | } |
470 | |
2d8e6c8d |
471 | } |
6d4a7be2 |
472 | |
2b140d5b |
473 | if ( SvOK(sv) && sv_derived_from(sv, "version") ) { |
8cb289bd |
474 | ST(0) = vstringify(sv); |
13f8f398 |
475 | } else { |
476 | ST(0) = sv; |
b38a9dc5 |
477 | } |
6d4a7be2 |
478 | |
479 | XSRETURN(1); |
480 | } |
481 | |
439cb1c4 |
482 | XS(XS_version_new) |
483 | { |
97aff369 |
484 | dVAR; |
439cb1c4 |
485 | dXSARGS; |
58c0efa5 |
486 | PERL_UNUSED_ARG(cv); |
129318bd |
487 | if (items > 3) |
439cb1c4 |
488 | Perl_croak(aTHX_ "Usage: version::new(class, version)"); |
489 | SP -= items; |
490 | { |
137d6fc0 |
491 | SV *vs = ST(1); |
492 | SV *rv; |
c4420975 |
493 | const char * const classname = |
494 | sv_isobject(ST(0)) /* get the class if called as an object method */ |
495 | ? HvNAME(SvSTASH(SvRV(ST(0)))) |
496 | : (char *)SvPV_nolen(ST(0)); |
9137345a |
497 | |
92dcf8ce |
498 | if ( items == 1 || vs == &PL_sv_undef ) { /* no param or explicit undef */ |
499 | /* create empty object */ |
500 | vs = sv_newmortal(); |
501 | sv_setpvn(vs,"",0); |
9137345a |
502 | } |
503 | else if ( items == 3 ) { |
504 | vs = sv_newmortal(); |
cfd0369c |
505 | Perl_sv_setpvf(aTHX_ vs,"v%s",SvPV_nolen_const(ST(2))); |
129318bd |
506 | } |
439cb1c4 |
507 | |
137d6fc0 |
508 | rv = new_version(vs); |
0723351e |
509 | if ( strcmp(classname,"version") != 0 ) /* inherited new() */ |
da51bb9b |
510 | sv_bless(rv, gv_stashpv(classname, GV_ADD)); |
137d6fc0 |
511 | |
512 | PUSHs(sv_2mortal(rv)); |
439cb1c4 |
513 | PUTBACK; |
514 | return; |
515 | } |
516 | } |
517 | |
518 | XS(XS_version_stringify) |
519 | { |
97aff369 |
520 | dVAR; |
41be1fbd |
521 | dXSARGS; |
58c0efa5 |
522 | PERL_UNUSED_ARG(cv); |
41be1fbd |
523 | if (items < 1) |
524 | Perl_croak(aTHX_ "Usage: version::stringify(lobj, ...)"); |
525 | SP -= items; |
526 | { |
7452cf6a |
527 | SV * lobj; |
41be1fbd |
528 | |
529 | if (sv_derived_from(ST(0), "version")) { |
9137345a |
530 | lobj = SvRV(ST(0)); |
41be1fbd |
531 | } |
532 | else |
533 | Perl_croak(aTHX_ "lobj is not of type version"); |
534 | |
137d6fc0 |
535 | PUSHs(sv_2mortal(vstringify(lobj))); |
41be1fbd |
536 | |
537 | PUTBACK; |
538 | return; |
539 | } |
439cb1c4 |
540 | } |
541 | |
542 | XS(XS_version_numify) |
543 | { |
97aff369 |
544 | dVAR; |
41be1fbd |
545 | dXSARGS; |
58c0efa5 |
546 | PERL_UNUSED_ARG(cv); |
41be1fbd |
547 | if (items < 1) |
548 | Perl_croak(aTHX_ "Usage: version::numify(lobj, ...)"); |
549 | SP -= items; |
550 | { |
7452cf6a |
551 | SV * lobj; |
41be1fbd |
552 | |
553 | if (sv_derived_from(ST(0), "version")) { |
9137345a |
554 | lobj = SvRV(ST(0)); |
41be1fbd |
555 | } |
556 | else |
557 | Perl_croak(aTHX_ "lobj is not of type version"); |
558 | |
137d6fc0 |
559 | PUSHs(sv_2mortal(vnumify(lobj))); |
41be1fbd |
560 | |
561 | PUTBACK; |
562 | return; |
563 | } |
439cb1c4 |
564 | } |
565 | |
9137345a |
566 | XS(XS_version_normal) |
567 | { |
97aff369 |
568 | dVAR; |
9137345a |
569 | dXSARGS; |
58c0efa5 |
570 | PERL_UNUSED_ARG(cv); |
9137345a |
571 | if (items < 1) |
572 | Perl_croak(aTHX_ "Usage: version::normal(lobj, ...)"); |
573 | SP -= items; |
574 | { |
7452cf6a |
575 | SV * lobj; |
9137345a |
576 | |
577 | if (sv_derived_from(ST(0), "version")) { |
578 | lobj = SvRV(ST(0)); |
579 | } |
580 | else |
581 | Perl_croak(aTHX_ "lobj is not of type version"); |
582 | |
583 | PUSHs(sv_2mortal(vnormal(lobj))); |
584 | |
585 | PUTBACK; |
586 | return; |
587 | } |
588 | } |
589 | |
439cb1c4 |
590 | XS(XS_version_vcmp) |
591 | { |
97aff369 |
592 | dVAR; |
41be1fbd |
593 | dXSARGS; |
58c0efa5 |
594 | PERL_UNUSED_ARG(cv); |
41be1fbd |
595 | if (items < 1) |
596 | Perl_croak(aTHX_ "Usage: version::vcmp(lobj, ...)"); |
597 | SP -= items; |
598 | { |
7452cf6a |
599 | SV * lobj; |
41be1fbd |
600 | |
601 | if (sv_derived_from(ST(0), "version")) { |
9137345a |
602 | lobj = SvRV(ST(0)); |
41be1fbd |
603 | } |
604 | else |
605 | Perl_croak(aTHX_ "lobj is not of type version"); |
606 | |
607 | { |
608 | SV *rs; |
609 | SV *rvs; |
610 | SV * robj = ST(1); |
7452cf6a |
611 | const IV swap = (IV)SvIV(ST(2)); |
41be1fbd |
612 | |
613 | if ( ! sv_derived_from(robj, "version") ) |
614 | { |
615 | robj = new_version(robj); |
616 | } |
617 | rvs = SvRV(robj); |
618 | |
619 | if ( swap ) |
620 | { |
621 | rs = newSViv(vcmp(rvs,lobj)); |
622 | } |
623 | else |
624 | { |
625 | rs = newSViv(vcmp(lobj,rvs)); |
626 | } |
627 | |
137d6fc0 |
628 | PUSHs(sv_2mortal(rs)); |
41be1fbd |
629 | } |
630 | |
631 | PUTBACK; |
632 | return; |
633 | } |
439cb1c4 |
634 | } |
635 | |
636 | XS(XS_version_boolean) |
637 | { |
97aff369 |
638 | dVAR; |
639 | dXSARGS; |
58c0efa5 |
640 | PERL_UNUSED_ARG(cv); |
97aff369 |
641 | if (items < 1) |
642 | Perl_croak(aTHX_ "Usage: version::boolean(lobj, ...)"); |
643 | SP -= items; |
c4420975 |
644 | if (sv_derived_from(ST(0), "version")) { |
645 | SV * const lobj = SvRV(ST(0)); |
396482e1 |
646 | SV * const rs = newSViv( vcmp(lobj,new_version(newSVpvs("0"))) ); |
c4420975 |
647 | PUSHs(sv_2mortal(rs)); |
648 | PUTBACK; |
649 | return; |
650 | } |
651 | else |
652 | Perl_croak(aTHX_ "lobj is not of type version"); |
439cb1c4 |
653 | } |
654 | |
655 | XS(XS_version_noop) |
656 | { |
97aff369 |
657 | dVAR; |
2dfd8427 |
658 | dXSARGS; |
58c0efa5 |
659 | PERL_UNUSED_ARG(cv); |
2dfd8427 |
660 | if (items < 1) |
661 | Perl_croak(aTHX_ "Usage: version::noop(lobj, ...)"); |
662 | if (sv_derived_from(ST(0), "version")) |
663 | Perl_croak(aTHX_ "operation not supported with version object"); |
664 | else |
665 | Perl_croak(aTHX_ "lobj is not of type version"); |
666 | #ifndef HASATTRIBUTE_NORETURN |
667 | XSRETURN_EMPTY; |
668 | #endif |
439cb1c4 |
669 | } |
670 | |
c8d69e4a |
671 | XS(XS_version_is_alpha) |
672 | { |
97aff369 |
673 | dVAR; |
c8d69e4a |
674 | dXSARGS; |
58c0efa5 |
675 | PERL_UNUSED_ARG(cv); |
c8d69e4a |
676 | if (items != 1) |
677 | Perl_croak(aTHX_ "Usage: version::is_alpha(lobj)"); |
678 | SP -= items; |
c4420975 |
679 | if (sv_derived_from(ST(0), "version")) { |
680 | SV * const lobj = ST(0); |
681 | if ( hv_exists((HV*)SvRV(lobj), "alpha", 5 ) ) |
682 | XSRETURN_YES; |
683 | else |
684 | XSRETURN_NO; |
c8d69e4a |
685 | PUTBACK; |
686 | return; |
687 | } |
c4420975 |
688 | else |
689 | Perl_croak(aTHX_ "lobj is not of type version"); |
c8d69e4a |
690 | } |
691 | |
137d6fc0 |
692 | XS(XS_version_qv) |
693 | { |
97aff369 |
694 | dVAR; |
137d6fc0 |
695 | dXSARGS; |
58c0efa5 |
696 | PERL_UNUSED_ARG(cv); |
137d6fc0 |
697 | if (items != 1) |
698 | Perl_croak(aTHX_ "Usage: version::qv(ver)"); |
699 | SP -= items; |
700 | { |
701 | SV * ver = ST(0); |
c4420975 |
702 | if ( !SvVOK(ver) ) { /* only need to do with if not already v-string */ |
ac0e6a2f |
703 | SV * const rv = sv_newmortal(); |
704 | sv_setsv(rv,ver); /* make a duplicate */ |
705 | upg_version(rv, TRUE); |
706 | PUSHs(rv); |
137d6fc0 |
707 | } |
708 | else |
709 | { |
710 | PUSHs(sv_2mortal(new_version(ver))); |
711 | } |
712 | |
713 | PUTBACK; |
714 | return; |
715 | } |
716 | } |
717 | |
8800c35a |
718 | XS(XS_utf8_is_utf8) |
719 | { |
97aff369 |
720 | dVAR; |
41be1fbd |
721 | dXSARGS; |
58c0efa5 |
722 | PERL_UNUSED_ARG(cv); |
41be1fbd |
723 | if (items != 1) |
724 | Perl_croak(aTHX_ "Usage: utf8::is_utf8(sv)"); |
c4420975 |
725 | else { |
726 | const SV * const sv = ST(0); |
727 | if (SvUTF8(sv)) |
728 | XSRETURN_YES; |
729 | else |
730 | XSRETURN_NO; |
41be1fbd |
731 | } |
732 | XSRETURN_EMPTY; |
8800c35a |
733 | } |
734 | |
1b026014 |
735 | XS(XS_utf8_valid) |
736 | { |
97aff369 |
737 | dVAR; |
41be1fbd |
738 | dXSARGS; |
58c0efa5 |
739 | PERL_UNUSED_ARG(cv); |
41be1fbd |
740 | if (items != 1) |
741 | Perl_croak(aTHX_ "Usage: utf8::valid(sv)"); |
c4420975 |
742 | else { |
743 | SV * const sv = ST(0); |
744 | STRLEN len; |
745 | const char * const s = SvPV_const(sv,len); |
746 | if (!SvUTF8(sv) || is_utf8_string((const U8*)s,len)) |
747 | XSRETURN_YES; |
748 | else |
749 | XSRETURN_NO; |
750 | } |
41be1fbd |
751 | XSRETURN_EMPTY; |
1b026014 |
752 | } |
753 | |
754 | XS(XS_utf8_encode) |
755 | { |
97aff369 |
756 | dVAR; |
1b026014 |
757 | dXSARGS; |
58c0efa5 |
758 | PERL_UNUSED_ARG(cv); |
1b026014 |
759 | if (items != 1) |
760 | Perl_croak(aTHX_ "Usage: utf8::encode(sv)"); |
c4420975 |
761 | sv_utf8_encode(ST(0)); |
1b026014 |
762 | XSRETURN_EMPTY; |
763 | } |
764 | |
765 | XS(XS_utf8_decode) |
766 | { |
97aff369 |
767 | dVAR; |
1b026014 |
768 | dXSARGS; |
58c0efa5 |
769 | PERL_UNUSED_ARG(cv); |
1b026014 |
770 | if (items != 1) |
771 | Perl_croak(aTHX_ "Usage: utf8::decode(sv)"); |
c4420975 |
772 | else { |
773 | SV * const sv = ST(0); |
6867be6d |
774 | const bool RETVAL = sv_utf8_decode(sv); |
1b026014 |
775 | ST(0) = boolSV(RETVAL); |
776 | sv_2mortal(ST(0)); |
777 | } |
778 | XSRETURN(1); |
779 | } |
780 | |
781 | XS(XS_utf8_upgrade) |
782 | { |
97aff369 |
783 | dVAR; |
1b026014 |
784 | dXSARGS; |
58c0efa5 |
785 | PERL_UNUSED_ARG(cv); |
1b026014 |
786 | if (items != 1) |
787 | Perl_croak(aTHX_ "Usage: utf8::upgrade(sv)"); |
c4420975 |
788 | else { |
789 | SV * const sv = ST(0); |
1b026014 |
790 | STRLEN RETVAL; |
791 | dXSTARG; |
792 | |
793 | RETVAL = sv_utf8_upgrade(sv); |
794 | XSprePUSH; PUSHi((IV)RETVAL); |
795 | } |
796 | XSRETURN(1); |
797 | } |
798 | |
799 | XS(XS_utf8_downgrade) |
800 | { |
97aff369 |
801 | dVAR; |
1b026014 |
802 | dXSARGS; |
58c0efa5 |
803 | PERL_UNUSED_ARG(cv); |
1b026014 |
804 | if (items < 1 || items > 2) |
805 | Perl_croak(aTHX_ "Usage: utf8::downgrade(sv, failok=0)"); |
c4420975 |
806 | else { |
807 | SV * const sv = ST(0); |
6867be6d |
808 | const bool failok = (items < 2) ? 0 : (int)SvIV(ST(1)); |
809 | const bool RETVAL = sv_utf8_downgrade(sv, failok); |
1b026014 |
810 | |
1b026014 |
811 | ST(0) = boolSV(RETVAL); |
812 | sv_2mortal(ST(0)); |
813 | } |
814 | XSRETURN(1); |
815 | } |
816 | |
817 | XS(XS_utf8_native_to_unicode) |
818 | { |
97aff369 |
819 | dVAR; |
1b026014 |
820 | dXSARGS; |
6867be6d |
821 | const UV uv = SvUV(ST(0)); |
58c0efa5 |
822 | PERL_UNUSED_ARG(cv); |
b7953727 |
823 | |
824 | if (items > 1) |
825 | Perl_croak(aTHX_ "Usage: utf8::native_to_unicode(sv)"); |
826 | |
1b026014 |
827 | ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv))); |
828 | XSRETURN(1); |
829 | } |
830 | |
831 | XS(XS_utf8_unicode_to_native) |
832 | { |
97aff369 |
833 | dVAR; |
1b026014 |
834 | dXSARGS; |
6867be6d |
835 | const UV uv = SvUV(ST(0)); |
58c0efa5 |
836 | PERL_UNUSED_ARG(cv); |
b7953727 |
837 | |
838 | if (items > 1) |
839 | Perl_croak(aTHX_ "Usage: utf8::unicode_to_native(sv)"); |
840 | |
1b026014 |
841 | ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv))); |
842 | XSRETURN(1); |
843 | } |
844 | |
14a976d6 |
845 | XS(XS_Internals_SvREADONLY) /* This is dangerous stuff. */ |
29569577 |
846 | { |
97aff369 |
847 | dVAR; |
29569577 |
848 | dXSARGS; |
c4420975 |
849 | SV * const sv = SvRV(ST(0)); |
58c0efa5 |
850 | PERL_UNUSED_ARG(cv); |
6867be6d |
851 | |
29569577 |
852 | if (items == 1) { |
853 | if (SvREADONLY(sv)) |
854 | XSRETURN_YES; |
855 | else |
856 | XSRETURN_NO; |
857 | } |
858 | else if (items == 2) { |
859 | if (SvTRUE(ST(1))) { |
860 | SvREADONLY_on(sv); |
861 | XSRETURN_YES; |
862 | } |
863 | else { |
14a976d6 |
864 | /* I hope you really know what you are doing. */ |
29569577 |
865 | SvREADONLY_off(sv); |
866 | XSRETURN_NO; |
867 | } |
868 | } |
14a976d6 |
869 | XSRETURN_UNDEF; /* Can't happen. */ |
29569577 |
870 | } |
871 | |
14a976d6 |
872 | XS(XS_Internals_SvREFCNT) /* This is dangerous stuff. */ |
29569577 |
873 | { |
97aff369 |
874 | dVAR; |
29569577 |
875 | dXSARGS; |
c4420975 |
876 | SV * const sv = SvRV(ST(0)); |
58c0efa5 |
877 | PERL_UNUSED_ARG(cv); |
6867be6d |
878 | |
29569577 |
879 | if (items == 1) |
14a976d6 |
880 | XSRETURN_IV(SvREFCNT(sv) - 1); /* Minus the ref created for us. */ |
29569577 |
881 | else if (items == 2) { |
14a976d6 |
882 | /* I hope you really know what you are doing. */ |
29569577 |
883 | SvREFCNT(sv) = SvIV(ST(1)); |
884 | XSRETURN_IV(SvREFCNT(sv)); |
885 | } |
14a976d6 |
886 | XSRETURN_UNDEF; /* Can't happen. */ |
29569577 |
887 | } |
888 | |
f044d0d1 |
889 | XS(XS_Internals_hv_clear_placehold) |
dfd4ef2f |
890 | { |
97aff369 |
891 | dVAR; |
dfd4ef2f |
892 | dXSARGS; |
58c0efa5 |
893 | PERL_UNUSED_ARG(cv); |
6867be6d |
894 | |
3540d4ce |
895 | if (items != 1) |
896 | Perl_croak(aTHX_ "Usage: UNIVERSAL::hv_clear_placeholders(hv)"); |
c4420975 |
897 | else { |
898 | HV * const hv = (HV *) SvRV(ST(0)); |
899 | hv_clear_placeholders(hv); |
900 | XSRETURN(0); |
901 | } |
dfd4ef2f |
902 | } |
39f7a870 |
903 | |
39cff0d9 |
904 | XS(XS_Regexp_DESTROY) |
905 | { |
96a5add6 |
906 | PERL_UNUSED_CONTEXT; |
53c1dcc0 |
907 | PERL_UNUSED_ARG(cv); |
39cff0d9 |
908 | } |
909 | |
39f7a870 |
910 | XS(XS_PerlIO_get_layers) |
911 | { |
97aff369 |
912 | dVAR; |
39f7a870 |
913 | dXSARGS; |
58c0efa5 |
914 | PERL_UNUSED_ARG(cv); |
39f7a870 |
915 | if (items < 1 || items % 2 == 0) |
916 | Perl_croak(aTHX_ "Usage: PerlIO_get_layers(filehandle[,args])"); |
5fef3b4a |
917 | #ifdef USE_PERLIO |
39f7a870 |
918 | { |
919 | SV * sv; |
920 | GV * gv; |
921 | IO * io; |
922 | bool input = TRUE; |
923 | bool details = FALSE; |
924 | |
925 | if (items > 1) { |
c4420975 |
926 | SV * const *svp; |
39f7a870 |
927 | for (svp = MARK + 2; svp <= SP; svp += 2) { |
c4420975 |
928 | SV * const * const varp = svp; |
929 | SV * const * const valp = svp + 1; |
39f7a870 |
930 | STRLEN klen; |
c4420975 |
931 | const char * const key = SvPV_const(*varp, klen); |
39f7a870 |
932 | |
933 | switch (*key) { |
934 | case 'i': |
935 | if (klen == 5 && memEQ(key, "input", 5)) { |
936 | input = SvTRUE(*valp); |
937 | break; |
938 | } |
939 | goto fail; |
940 | case 'o': |
941 | if (klen == 6 && memEQ(key, "output", 6)) { |
942 | input = !SvTRUE(*valp); |
943 | break; |
944 | } |
945 | goto fail; |
946 | case 'd': |
947 | if (klen == 7 && memEQ(key, "details", 7)) { |
948 | details = SvTRUE(*valp); |
949 | break; |
950 | } |
951 | goto fail; |
952 | default: |
953 | fail: |
954 | Perl_croak(aTHX_ |
955 | "get_layers: unknown argument '%s'", |
956 | key); |
957 | } |
958 | } |
959 | |
960 | SP -= (items - 1); |
961 | } |
962 | |
963 | sv = POPs; |
964 | gv = (GV*)sv; |
965 | |
966 | if (!isGV(sv)) { |
967 | if (SvROK(sv) && isGV(SvRV(sv))) |
968 | gv = (GV*)SvRV(sv); |
671d49be |
969 | else if (SvPOKp(sv)) |
f776e3cd |
970 | gv = gv_fetchsv(sv, 0, SVt_PVIO); |
39f7a870 |
971 | } |
972 | |
973 | if (gv && (io = GvIO(gv))) { |
974 | dTARGET; |
c4420975 |
975 | AV* const av = PerlIO_get_layers(aTHX_ input ? |
39f7a870 |
976 | IoIFP(io) : IoOFP(io)); |
977 | I32 i; |
c4420975 |
978 | const I32 last = av_len(av); |
39f7a870 |
979 | I32 nitem = 0; |
980 | |
981 | for (i = last; i >= 0; i -= 3) { |
c4420975 |
982 | SV * const * const namsvp = av_fetch(av, i - 2, FALSE); |
983 | SV * const * const argsvp = av_fetch(av, i - 1, FALSE); |
984 | SV * const * const flgsvp = av_fetch(av, i, FALSE); |
39f7a870 |
985 | |
c4420975 |
986 | const bool namok = namsvp && *namsvp && SvPOK(*namsvp); |
987 | const bool argok = argsvp && *argsvp && SvPOK(*argsvp); |
988 | const bool flgok = flgsvp && *flgsvp && SvIOK(*flgsvp); |
39f7a870 |
989 | |
990 | if (details) { |
ec3bab8e |
991 | XPUSHs(namok |
992 | ? newSVpvn(SvPVX_const(*namsvp), SvCUR(*namsvp)) |
993 | : &PL_sv_undef); |
994 | XPUSHs(argok |
995 | ? newSVpvn(SvPVX_const(*argsvp), SvCUR(*argsvp)) |
996 | : &PL_sv_undef); |
39f7a870 |
997 | if (flgok) |
998 | XPUSHi(SvIVX(*flgsvp)); |
999 | else |
1000 | XPUSHs(&PL_sv_undef); |
1001 | nitem += 3; |
1002 | } |
1003 | else { |
1004 | if (namok && argok) |
1005 | XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf"(%"SVf")", |
be2597df |
1006 | SVfARG(*namsvp), |
1007 | SVfARG(*argsvp))); |
39f7a870 |
1008 | else if (namok) |
95b63a38 |
1009 | XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf, |
be2597df |
1010 | SVfARG(*namsvp))); |
39f7a870 |
1011 | else |
1012 | XPUSHs(&PL_sv_undef); |
1013 | nitem++; |
1014 | if (flgok) { |
c4420975 |
1015 | const IV flags = SvIVX(*flgsvp); |
39f7a870 |
1016 | |
1017 | if (flags & PERLIO_F_UTF8) { |
396482e1 |
1018 | XPUSHs(newSVpvs("utf8")); |
39f7a870 |
1019 | nitem++; |
1020 | } |
1021 | } |
1022 | } |
1023 | } |
1024 | |
1025 | SvREFCNT_dec(av); |
1026 | |
1027 | XSRETURN(nitem); |
1028 | } |
1029 | } |
5fef3b4a |
1030 | #endif |
39f7a870 |
1031 | |
1032 | XSRETURN(0); |
1033 | } |
1034 | |
9a7034eb |
1035 | XS(XS_Internals_hash_seed) |
c910b28a |
1036 | { |
97aff369 |
1037 | dVAR; |
c85d3f85 |
1038 | /* Using dXSARGS would also have dITEM and dSP, |
1039 | * which define 2 unused local variables. */ |
557b887a |
1040 | dAXMARK; |
53c1dcc0 |
1041 | PERL_UNUSED_ARG(cv); |
ad73156c |
1042 | PERL_UNUSED_VAR(mark); |
81eaca17 |
1043 | XSRETURN_UV(PERL_HASH_SEED); |
c910b28a |
1044 | } |
1045 | |
008fb0c0 |
1046 | XS(XS_Internals_rehash_seed) |
8e90d776 |
1047 | { |
97aff369 |
1048 | dVAR; |
8e90d776 |
1049 | /* Using dXSARGS would also have dITEM and dSP, |
1050 | * which define 2 unused local variables. */ |
557b887a |
1051 | dAXMARK; |
53c1dcc0 |
1052 | PERL_UNUSED_ARG(cv); |
ad73156c |
1053 | PERL_UNUSED_VAR(mark); |
008fb0c0 |
1054 | XSRETURN_UV(PL_rehash_seed); |
8e90d776 |
1055 | } |
1056 | |
05619474 |
1057 | XS(XS_Internals_HvREHASH) /* Subject to change */ |
1058 | { |
97aff369 |
1059 | dVAR; |
05619474 |
1060 | dXSARGS; |
58c0efa5 |
1061 | PERL_UNUSED_ARG(cv); |
05619474 |
1062 | if (SvROK(ST(0))) { |
c4420975 |
1063 | const HV * const hv = (HV *) SvRV(ST(0)); |
05619474 |
1064 | if (items == 1 && SvTYPE(hv) == SVt_PVHV) { |
1065 | if (HvREHASH(hv)) |
1066 | XSRETURN_YES; |
1067 | else |
1068 | XSRETURN_NO; |
1069 | } |
1070 | } |
1071 | Perl_croak(aTHX_ "Internals::HvREHASH $hashref"); |
1072 | } |
241d1a3b |
1073 | |
80305961 |
1074 | XS(XS_re_is_regexp) |
1075 | { |
1076 | dVAR; |
1077 | dXSARGS; |
1078 | if (items != 1) |
1079 | Perl_croak(aTHX_ "Usage: %s(%s)", "re::is_regexp", "sv"); |
1080 | PERL_UNUSED_VAR(cv); /* -W */ |
1081 | PERL_UNUSED_VAR(ax); /* -Wall */ |
1082 | SP -= items; |
1083 | { |
1084 | SV * sv = ST(0); |
1085 | if ( Perl_get_re_arg( aTHX_ sv, 0, NULL ) ) |
1086 | { |
1087 | XSRETURN_YES; |
1088 | } else { |
1089 | XSRETURN_NO; |
1090 | } |
1091 | /* NOTREACHED */ |
1092 | PUTBACK; |
1093 | return; |
1094 | } |
1095 | } |
1096 | |
192b9cd1 |
1097 | XS(XS_re_regnames_count) |
80305961 |
1098 | { |
192b9cd1 |
1099 | REGEXP *rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL; |
1100 | SV * ret; |
80305961 |
1101 | dVAR; |
1102 | dXSARGS; |
48fc4736 |
1103 | PERL_UNUSED_ARG(cv); |
192b9cd1 |
1104 | |
1105 | if (items != 0) |
1106 | Perl_croak(aTHX_ "Usage: %s(%s)", "re::regnames_count", ""); |
1107 | |
1108 | SP -= items; |
1109 | |
1110 | if (!rx) |
1111 | XSRETURN_UNDEF; |
1112 | |
1113 | ret = CALLREG_NAMED_BUFF_COUNT(rx); |
1114 | |
1115 | SPAGAIN; |
1116 | |
1117 | if (ret) { |
1118 | XPUSHs(ret); |
1119 | PUTBACK; |
1120 | return; |
1121 | } else { |
1122 | XSRETURN_UNDEF; |
1123 | } |
1124 | } |
1125 | |
1126 | XS(XS_re_regname) |
1127 | { |
1128 | dVAR; |
1129 | dXSARGS; |
1130 | REGEXP * rx; |
1131 | U32 flags; |
1132 | SV * ret; |
48fc4736 |
1133 | PERL_UNUSED_ARG(cv); |
192b9cd1 |
1134 | |
28d8d7f4 |
1135 | if (items < 1 || items > 2) |
192b9cd1 |
1136 | Perl_croak(aTHX_ "Usage: %s(%s)", "re::regname", "name[, all ]"); |
1137 | |
80305961 |
1138 | SP -= items; |
80305961 |
1139 | |
192b9cd1 |
1140 | rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL; |
1141 | |
1142 | if (!rx) |
1143 | XSRETURN_UNDEF; |
1144 | |
1145 | if (items == 2 && SvTRUE(ST(1))) { |
1146 | flags = RXf_HASH_ALL; |
1147 | } else { |
1148 | flags = RXf_HASH_ONE; |
80305961 |
1149 | } |
192b9cd1 |
1150 | ret = CALLREG_NAMED_BUFF_FETCH(rx, ST(0), (flags | RXf_HASH_REGNAME)); |
1151 | |
1152 | if (ret) { |
1153 | if (SvROK(ret)) |
1154 | XPUSHs(ret); |
1155 | else |
1156 | XPUSHs(SvREFCNT_inc(ret)); |
1157 | XSRETURN(1); |
1158 | } |
1159 | XSRETURN_UNDEF; |
80305961 |
1160 | } |
1161 | |
192b9cd1 |
1162 | |
80305961 |
1163 | XS(XS_re_regnames) |
1164 | { |
192b9cd1 |
1165 | dVAR; |
80305961 |
1166 | dXSARGS; |
192b9cd1 |
1167 | REGEXP * rx; |
1168 | U32 flags; |
1169 | SV *ret; |
1170 | AV *av; |
1171 | I32 length; |
1172 | I32 i; |
1173 | SV **entry; |
48fc4736 |
1174 | PERL_UNUSED_ARG(cv); |
192b9cd1 |
1175 | |
1176 | if (items > 1) |
1177 | Perl_croak(aTHX_ "Usage: %s(%s)", "re::regnames", "[all]"); |
1178 | |
1179 | rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL; |
1180 | |
1181 | if (!rx) |
1182 | XSRETURN_UNDEF; |
1183 | |
1184 | if (items == 1 && SvTRUE(ST(0))) { |
1185 | flags = RXf_HASH_ALL; |
1186 | } else { |
1187 | flags = RXf_HASH_ONE; |
1188 | } |
1189 | |
80305961 |
1190 | SP -= items; |
80305961 |
1191 | |
192b9cd1 |
1192 | ret = CALLREG_NAMED_BUFF_ALL(rx, (flags | RXf_HASH_REGNAMES)); |
1193 | |
1194 | SPAGAIN; |
1195 | |
1196 | SP -= items; |
1197 | |
1198 | if (!ret) |
1199 | XSRETURN_UNDEF; |
1200 | |
1201 | av = (AV*)SvRV(ret); |
1202 | length = av_len(av); |
1203 | |
1204 | for (i = 0; i <= length; i++) { |
1205 | entry = av_fetch(av, i, FALSE); |
1206 | |
1207 | if (!entry) |
1208 | Perl_croak(aTHX_ "NULL array element in re::regnames()"); |
1209 | |
1210 | XPUSHs(*entry); |
80305961 |
1211 | } |
192b9cd1 |
1212 | PUTBACK; |
1213 | return; |
80305961 |
1214 | } |
1215 | |
192b9cd1 |
1216 | XS(XS_Tie_Hash_NamedCapture_FETCH) |
80305961 |
1217 | { |
192b9cd1 |
1218 | dVAR; |
80305961 |
1219 | dXSARGS; |
192b9cd1 |
1220 | REGEXP * rx; |
1221 | U32 flags; |
1222 | SV * ret; |
48fc4736 |
1223 | PERL_UNUSED_ARG(cv); |
192b9cd1 |
1224 | |
1225 | if (items != 2) |
1226 | Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::STORE($key, $flags)"); |
1227 | |
1228 | rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL; |
1229 | |
1230 | if (!rx) |
1231 | XSRETURN_UNDEF; |
1232 | |
80305961 |
1233 | SP -= items; |
192b9cd1 |
1234 | |
1235 | flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0)))); |
1236 | ret = CALLREG_NAMED_BUFF_FETCH(rx, ST(1), flags); |
1237 | |
1238 | SPAGAIN; |
1239 | |
1240 | if (ret) { |
1241 | if (SvROK(ret)) |
1242 | XPUSHs(ret); |
1243 | else |
1244 | XPUSHs(SvREFCNT_inc(ret)); |
1245 | PUTBACK; |
1246 | return; |
1247 | } |
1248 | XSRETURN_UNDEF; |
1249 | } |
1250 | |
1251 | XS(XS_Tie_Hash_NamedCapture_STORE) |
1252 | { |
1253 | dVAR; |
1254 | dXSARGS; |
1255 | REGEXP * rx; |
1256 | U32 flags; |
48fc4736 |
1257 | PERL_UNUSED_ARG(cv); |
192b9cd1 |
1258 | |
1259 | if (items != 3) |
1260 | Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::STORE($key, $value, $flags)"); |
1261 | |
1262 | rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL; |
1263 | |
1264 | if (!rx) { |
1265 | if (!PL_localizing) |
1266 | Perl_croak(aTHX_ PL_no_modify); |
1267 | else |
28d8d7f4 |
1268 | XSRETURN_UNDEF; |
80305961 |
1269 | } |
192b9cd1 |
1270 | |
1271 | SP -= items; |
1272 | |
1273 | flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0)))); |
1274 | CALLREG_NAMED_BUFF_STORE(rx,ST(1), ST(2), flags); |
80305961 |
1275 | } |
1276 | |
192b9cd1 |
1277 | XS(XS_Tie_Hash_NamedCapture_DELETE) |
1278 | { |
1279 | dVAR; |
1280 | dXSARGS; |
1281 | REGEXP * rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL; |
1282 | U32 flags; |
48fc4736 |
1283 | PERL_UNUSED_ARG(cv); |
80305961 |
1284 | |
192b9cd1 |
1285 | if (items != 2) |
1286 | Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::DELETE($key, $flags)"); |
1287 | |
1288 | if (!rx) |
1289 | Perl_croak(aTHX_ PL_no_modify); |
1290 | |
1291 | SP -= items; |
1292 | |
1293 | flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0)))); |
1294 | CALLREG_NAMED_BUFF_DELETE(rx, ST(1), flags); |
1295 | } |
1296 | |
1297 | XS(XS_Tie_Hash_NamedCapture_CLEAR) |
80305961 |
1298 | { |
192b9cd1 |
1299 | dVAR; |
80305961 |
1300 | dXSARGS; |
192b9cd1 |
1301 | REGEXP * rx; |
1302 | U32 flags; |
48fc4736 |
1303 | PERL_UNUSED_ARG(cv); |
192b9cd1 |
1304 | |
1305 | if (items != 1) |
1306 | Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::CLEAR($flags)"); |
1307 | |
1308 | rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL; |
1309 | |
1310 | if (!rx) |
1311 | Perl_croak(aTHX_ PL_no_modify); |
1312 | |
80305961 |
1313 | SP -= items; |
80305961 |
1314 | |
192b9cd1 |
1315 | flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0)))); |
1316 | CALLREG_NAMED_BUFF_CLEAR(rx, flags); |
1317 | } |
1318 | |
1319 | XS(XS_Tie_Hash_NamedCapture_EXISTS) |
1320 | { |
1321 | dVAR; |
1322 | dXSARGS; |
1323 | REGEXP * rx; |
1324 | U32 flags; |
1325 | SV * ret; |
48fc4736 |
1326 | PERL_UNUSED_ARG(cv); |
192b9cd1 |
1327 | |
1328 | if (items != 2) |
1329 | Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::EXISTS($key, $flags)"); |
1330 | |
1331 | rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL; |
1332 | |
1333 | if (!rx) |
28d8d7f4 |
1334 | XSRETURN_UNDEF; |
192b9cd1 |
1335 | |
1336 | SP -= items; |
1337 | |
1338 | flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0)))); |
1339 | ret = CALLREG_NAMED_BUFF_EXISTS(rx, ST(1), flags); |
1340 | |
1341 | SPAGAIN; |
1342 | |
1343 | XPUSHs(ret); |
80305961 |
1344 | PUTBACK; |
1345 | return; |
80305961 |
1346 | } |
1347 | |
192b9cd1 |
1348 | XS(XS_Tie_Hash_NamedCapture_FIRSTKEY) |
1349 | { |
1350 | dVAR; |
1351 | dXSARGS; |
1352 | REGEXP * rx; |
1353 | U32 flags; |
1354 | SV * ret; |
48fc4736 |
1355 | PERL_UNUSED_ARG(cv); |
80305961 |
1356 | |
192b9cd1 |
1357 | if (items != 1) |
1358 | Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::FIRSTKEY()"); |
1359 | |
1360 | rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL; |
1361 | |
1362 | if (!rx) |
1363 | XSRETURN_UNDEF; |
1364 | |
1365 | SP -= items; |
1366 | |
1367 | flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0)))); |
1368 | ret = CALLREG_NAMED_BUFF_FIRSTKEY(rx, flags); |
1369 | |
1370 | SPAGAIN; |
1371 | |
1372 | if (ret) { |
1373 | XPUSHs(SvREFCNT_inc(ret)); |
1374 | PUTBACK; |
1375 | } else { |
1376 | XSRETURN_UNDEF; |
1377 | } |
1378 | |
1379 | } |
1380 | |
1381 | XS(XS_Tie_Hash_NamedCapture_NEXTKEY) |
80305961 |
1382 | { |
192b9cd1 |
1383 | dVAR; |
80305961 |
1384 | dXSARGS; |
192b9cd1 |
1385 | REGEXP * rx; |
1386 | U32 flags; |
1387 | SV * ret; |
48fc4736 |
1388 | PERL_UNUSED_ARG(cv); |
192b9cd1 |
1389 | |
1390 | if (items != 2) |
1391 | Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::NEXTKEY($lastkey)"); |
1392 | |
1393 | rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL; |
1394 | |
1395 | if (!rx) |
1396 | XSRETURN_UNDEF; |
80305961 |
1397 | |
80305961 |
1398 | SP -= items; |
192b9cd1 |
1399 | |
1400 | flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0)))); |
1401 | ret = CALLREG_NAMED_BUFF_NEXTKEY(rx, ST(1), flags); |
1402 | |
1403 | SPAGAIN; |
1404 | |
1405 | if (ret) { |
1406 | XPUSHs(ret); |
80305961 |
1407 | } else { |
1408 | XSRETURN_UNDEF; |
1409 | } |
1410 | PUTBACK; |
192b9cd1 |
1411 | } |
1412 | |
1413 | XS(XS_Tie_Hash_NamedCapture_SCALAR) |
1414 | { |
1415 | dVAR; |
1416 | dXSARGS; |
1417 | REGEXP * rx; |
1418 | U32 flags; |
1419 | SV * ret; |
48fc4736 |
1420 | PERL_UNUSED_ARG(cv); |
192b9cd1 |
1421 | |
1422 | if (items != 1) |
1423 | Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::SCALAR()"); |
1424 | |
1425 | rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL; |
1426 | |
1427 | if (!rx) |
1428 | XSRETURN_UNDEF; |
1429 | |
1430 | SP -= items; |
1431 | |
1432 | flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0)))); |
1433 | ret = CALLREG_NAMED_BUFF_SCALAR(rx, flags); |
1434 | |
1435 | SPAGAIN; |
1436 | |
1437 | if (ret) { |
1438 | XPUSHs(ret); |
1439 | PUTBACK; |
1440 | return; |
1441 | } else { |
1442 | XSRETURN_UNDEF; |
1443 | } |
1444 | } |
1445 | |
1446 | XS(XS_Tie_Hash_NamedCapture_flags) |
1447 | { |
1448 | dVAR; |
1449 | dXSARGS; |
48fc4736 |
1450 | PERL_UNUSED_ARG(cv); |
192b9cd1 |
1451 | |
1452 | if (items != 0) |
1453 | Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::flags()"); |
1454 | |
1455 | XPUSHs(sv_2mortal(newSVuv(RXf_HASH_ONE))); |
1456 | XPUSHs(sv_2mortal(newSVuv(RXf_HASH_ALL))); |
1457 | PUTBACK; |
1458 | return; |
80305961 |
1459 | } |
1460 | |
1461 | |
241d1a3b |
1462 | /* |
1463 | * Local variables: |
1464 | * c-indentation-style: bsd |
1465 | * c-basic-offset: 4 |
1466 | * indent-tabs-mode: t |
1467 | * End: |
1468 | * |
37442d52 |
1469 | * ex: set ts=8 sts=4 sw=4 noet: |
1470 | */ |