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