This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Stop VC++ from warning "all references to 'SHELL32.dll' discarded
[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
59e7186f
RGS
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
YO
179regexp *
180Perl_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
JH
199PERL_XS_EXPORT_C void XS_UNIVERSAL_isa(pTHX_ CV *cv);
200PERL_XS_EXPORT_C void XS_UNIVERSAL_can(pTHX_ CV *cv);
cbc021f9 201PERL_XS_EXPORT_C void XS_UNIVERSAL_DOES(pTHX_ CV *cv);
27da23d5 202PERL_XS_EXPORT_C void XS_UNIVERSAL_VERSION(pTHX_ CV *cv);
439cb1c4
JP
203XS(XS_version_new);
204XS(XS_version_stringify);
205XS(XS_version_numify);
9137345a 206XS(XS_version_normal);
439cb1c4
JP
207XS(XS_version_vcmp);
208XS(XS_version_boolean);
2dfd8427
AL
209#ifdef HASATTRIBUTE_NORETURN
210XS(XS_version_noop) __attribute__noreturn__;
211#else
439cb1c4 212XS(XS_version_noop);
2dfd8427 213#endif
c8d69e4a 214XS(XS_version_is_alpha);
137d6fc0 215XS(XS_version_qv);
8800c35a 216XS(XS_utf8_is_utf8);
1b026014
NIS
217XS(XS_utf8_valid);
218XS(XS_utf8_encode);
219XS(XS_utf8_decode);
220XS(XS_utf8_upgrade);
221XS(XS_utf8_downgrade);
222XS(XS_utf8_unicode_to_native);
223XS(XS_utf8_native_to_unicode);
29569577
JH
224XS(XS_Internals_SvREADONLY);
225XS(XS_Internals_SvREFCNT);
f044d0d1 226XS(XS_Internals_hv_clear_placehold);
39f7a870 227XS(XS_PerlIO_get_layers);
39cff0d9 228XS(XS_Regexp_DESTROY);
9a7034eb 229XS(XS_Internals_hash_seed);
008fb0c0 230XS(XS_Internals_rehash_seed);
05619474 231XS(XS_Internals_HvREHASH);
e1234d8e 232XS(XS_Internals_inc_sub_generation);
80305961 233XS(XS_re_is_regexp);
192b9cd1
AB
234XS(XS_re_regname);
235XS(XS_re_regnames);
80305961 236XS(XS_re_regnames_count);
192b9cd1
AB
237XS(XS_Tie_Hash_NamedCapture_FETCH);
238XS(XS_Tie_Hash_NamedCapture_STORE);
239XS(XS_Tie_Hash_NamedCapture_DELETE);
240XS(XS_Tie_Hash_NamedCapture_CLEAR);
241XS(XS_Tie_Hash_NamedCapture_EXISTS);
242XS(XS_Tie_Hash_NamedCapture_FIRSTKEY);
243XS(XS_Tie_Hash_NamedCapture_NEXTKEY);
244XS(XS_Tie_Hash_NamedCapture_SCALAR);
245XS(XS_Tie_Hash_NamedCapture_flags);
0cb96387
GS
246
247void
248Perl_boot_core_UNIVERSAL(pTHX)
249{
97aff369 250 dVAR;
157e3fc8 251 static const char file[] = __FILE__;
0cb96387
GS
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
JP
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
JP
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
JP
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
NIS
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
JH
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
JH
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
YO
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
AB
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
GS
309}
310
55497cff 311
6d4a7be2
PP
312XS(XS_UNIVERSAL_isa)
313{
97aff369 314 dVAR;
6d4a7be2 315 dXSARGS;
58c0efa5 316 PERL_UNUSED_ARG(cv);
6d4a7be2
PP
317
318 if (items != 2)
cea2e8a9 319 Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)");
c4420975
AL
320 else {
321 SV * const sv = ST(0);
322 const char *name;
6d4a7be2 323
c4420975 324 SvGETMAGIC(sv);
d3f7f2b2 325
c4420975
AL
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
AL
332 ST(0) = boolSV(sv_derived_from(sv, name));
333 XSRETURN(1);
334 }
6d4a7be2
PP
335}
336
6d4a7be2
PP
337XS(XS_UNIVERSAL_can)
338{
97aff369 339 dVAR;
6d4a7be2
PP
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
PP
346
347 if (items != 2)
cea2e8a9 348 Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)");
6d4a7be2
PP
349
350 sv = ST(0);
f8f70380 351
5b295bef 352 SvGETMAGIC(sv);
d3f7f2b2 353
253ecd6d
RGS
354 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
355 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
f8f70380
GS
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
NIS
364 pkg = SvSTASH(sv);
365 }
366 else {
da51bb9b 367 pkg = gv_stashsv(sv, 0);
6f08146e
NIS
368 }
369
370 if (pkg) {
c4420975 371 GV * const gv = gv_fetchmethod_autoload(pkg, name, FALSE);
dc848c6f
PP
372 if (gv && isGV(gv))
373 rv = sv_2mortal(newRV((SV*)GvCV(gv)));
6d4a7be2
PP
374 }
375
376 ST(0) = rv;
377 XSRETURN(1);
378}
379
cbc021f9 380XS(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
PP
400XS(XS_UNIVERSAL_VERSION)
401{
97aff369 402 dVAR;
6d4a7be2
PP
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
PP
415 pkg = SvSTASH(sv);
416 }
417 else {
da51bb9b 418 pkg = gv_stashsv(ST(0), 0);
6d4a7be2
PP
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
PP
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
PP
430 }
431 else {
3280af22 432 sv = (SV*)&PL_sv_undef;
6d4a7be2
PP
433 undef = "(undef)";
434 }
435
1571675a 436 if (items > 1) {
1571675a
GS
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
NC
443 "%s does not define $%s::VERSION--version check failed",
444 name, name);
445 } else {
a3b680e6
AL
446 Perl_croak(aTHX_
447 "%s defines neither package nor VERSION--version check failed",
0510663f 448 SvPVx_nolen_const(ST(0)) );
62658f4d
PM
449 }
450 }
ad63d80f 451
137d6fc0
JP
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
RGS
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
RGS
463 } else {
464 Perl_croak(aTHX_ "%s version %"SVf" required--"
465 "this is only version %"SVf"", HvNAME_get(pkg),
8cb289bd
RGS
466 SVfARG(vstringify(req)),
467 SVfARG(vstringify(sv)));
ac0e6a2f
RGS
468 }
469 }
470
2d8e6c8d 471 }
6d4a7be2 472
2b140d5b 473 if ( SvOK(sv) && sv_derived_from(sv, "version") ) {
8cb289bd 474 ST(0) = vstringify(sv);
13f8f398
JP
475 } else {
476 ST(0) = sv;
b38a9dc5 477 }
6d4a7be2
PP
478
479 XSRETURN(1);
480}
481
439cb1c4
JP
482XS(XS_version_new)
483{
97aff369 484 dVAR;
439cb1c4 485 dXSARGS;
58c0efa5 486 PERL_UNUSED_ARG(cv);
129318bd 487 if (items > 3)
439cb1c4
JP
488 Perl_croak(aTHX_ "Usage: version::new(class, version)");
489 SP -= items;
490 {
137d6fc0
JP
491 SV *vs = ST(1);
492 SV *rv;
c4420975
AL
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
JP
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
JP
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
JP
511
512 PUSHs(sv_2mortal(rv));
439cb1c4
JP
513 PUTBACK;
514 return;
515 }
516}
517
518XS(XS_version_stringify)
519{
97aff369 520 dVAR;
41be1fbd 521 dXSARGS;
58c0efa5 522 PERL_UNUSED_ARG(cv);
41be1fbd
JH
523 if (items < 1)
524 Perl_croak(aTHX_ "Usage: version::stringify(lobj, ...)");
525 SP -= items;
526 {
7452cf6a 527 SV * lobj;
41be1fbd
JH
528
529 if (sv_derived_from(ST(0), "version")) {
9137345a 530 lobj = SvRV(ST(0));
41be1fbd
JH
531 }
532 else
533 Perl_croak(aTHX_ "lobj is not of type version");
534
137d6fc0 535 PUSHs(sv_2mortal(vstringify(lobj)));
41be1fbd
JH
536
537 PUTBACK;
538 return;
539 }
439cb1c4
JP
540}
541
542XS(XS_version_numify)
543{
97aff369 544 dVAR;
41be1fbd 545 dXSARGS;
58c0efa5 546 PERL_UNUSED_ARG(cv);
41be1fbd
JH
547 if (items < 1)
548 Perl_croak(aTHX_ "Usage: version::numify(lobj, ...)");
549 SP -= items;
550 {
7452cf6a 551 SV * lobj;
41be1fbd
JH
552
553 if (sv_derived_from(ST(0), "version")) {
9137345a 554 lobj = SvRV(ST(0));
41be1fbd
JH
555 }
556 else
557 Perl_croak(aTHX_ "lobj is not of type version");
558
137d6fc0 559 PUSHs(sv_2mortal(vnumify(lobj)));
41be1fbd
JH
560
561 PUTBACK;
562 return;
563 }
439cb1c4
JP
564}
565
9137345a
JP
566XS(XS_version_normal)
567{
97aff369 568 dVAR;
9137345a 569 dXSARGS;
58c0efa5 570 PERL_UNUSED_ARG(cv);
9137345a
JP
571 if (items < 1)
572 Perl_croak(aTHX_ "Usage: version::normal(lobj, ...)");
573 SP -= items;
574 {
7452cf6a 575 SV * lobj;
9137345a
JP
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
JP
590XS(XS_version_vcmp)
591{
97aff369 592 dVAR;
41be1fbd 593 dXSARGS;
58c0efa5 594 PERL_UNUSED_ARG(cv);
41be1fbd
JH
595 if (items < 1)
596 Perl_croak(aTHX_ "Usage: version::vcmp(lobj, ...)");
597 SP -= items;
598 {
7452cf6a 599 SV * lobj;
41be1fbd
JH
600
601 if (sv_derived_from(ST(0), "version")) {
9137345a 602 lobj = SvRV(ST(0));
41be1fbd
JH
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
JH
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
JH
629 }
630
631 PUTBACK;
632 return;
633 }
439cb1c4
JP
634}
635
636XS(XS_version_boolean)
637{
97aff369
JH
638 dVAR;
639 dXSARGS;
58c0efa5 640 PERL_UNUSED_ARG(cv);
97aff369
JH
641 if (items < 1)
642 Perl_croak(aTHX_ "Usage: version::boolean(lobj, ...)");
643 SP -= items;
c4420975
AL
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
AL
647 PUSHs(sv_2mortal(rs));
648 PUTBACK;
649 return;
650 }
651 else
652 Perl_croak(aTHX_ "lobj is not of type version");
439cb1c4
JP
653}
654
655XS(XS_version_noop)
656{
97aff369 657 dVAR;
2dfd8427 658 dXSARGS;
58c0efa5 659 PERL_UNUSED_ARG(cv);
2dfd8427
AL
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
JP
669}
670
c8d69e4a
JP
671XS(XS_version_is_alpha)
672{
97aff369 673 dVAR;
c8d69e4a 674 dXSARGS;
58c0efa5 675 PERL_UNUSED_ARG(cv);
c8d69e4a
JP
676 if (items != 1)
677 Perl_croak(aTHX_ "Usage: version::is_alpha(lobj)");
678 SP -= items;
c4420975
AL
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
JP
685 PUTBACK;
686 return;
687 }
c4420975
AL
688 else
689 Perl_croak(aTHX_ "lobj is not of type version");
c8d69e4a
JP
690}
691
137d6fc0
JP
692XS(XS_version_qv)
693{
97aff369 694 dVAR;
137d6fc0 695 dXSARGS;
58c0efa5 696 PERL_UNUSED_ARG(cv);
137d6fc0
JP
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
RGS
703 SV * const rv = sv_newmortal();
704 sv_setsv(rv,ver); /* make a duplicate */
705 upg_version(rv, TRUE);
706 PUSHs(rv);
137d6fc0
JP
707 }
708 else
709 {
710 PUSHs(sv_2mortal(new_version(ver)));
711 }
712
713 PUTBACK;
714 return;
715 }
716}
717
8800c35a
JH
718XS(XS_utf8_is_utf8)
719{
97aff369 720 dVAR;
41be1fbd 721 dXSARGS;
58c0efa5 722 PERL_UNUSED_ARG(cv);
41be1fbd
JH
723 if (items != 1)
724 Perl_croak(aTHX_ "Usage: utf8::is_utf8(sv)");
c4420975
AL
725 else {
726 const SV * const sv = ST(0);
727 if (SvUTF8(sv))
728 XSRETURN_YES;
729 else
730 XSRETURN_NO;
41be1fbd
JH
731 }
732 XSRETURN_EMPTY;
8800c35a
JH
733}
734
1b026014
NIS
735XS(XS_utf8_valid)
736{
97aff369 737 dVAR;
41be1fbd 738 dXSARGS;
58c0efa5 739 PERL_UNUSED_ARG(cv);
41be1fbd
JH
740 if (items != 1)
741 Perl_croak(aTHX_ "Usage: utf8::valid(sv)");
c4420975
AL
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
NIS
752}
753
754XS(XS_utf8_encode)
755{
97aff369 756 dVAR;
1b026014 757 dXSARGS;
58c0efa5 758 PERL_UNUSED_ARG(cv);
1b026014
NIS
759 if (items != 1)
760 Perl_croak(aTHX_ "Usage: utf8::encode(sv)");
c4420975 761 sv_utf8_encode(ST(0));
1b026014
NIS
762 XSRETURN_EMPTY;
763}
764
765XS(XS_utf8_decode)
766{
97aff369 767 dVAR;
1b026014 768 dXSARGS;
58c0efa5 769 PERL_UNUSED_ARG(cv);
1b026014
NIS
770 if (items != 1)
771 Perl_croak(aTHX_ "Usage: utf8::decode(sv)");
c4420975
AL
772 else {
773 SV * const sv = ST(0);
6867be6d 774 const bool RETVAL = sv_utf8_decode(sv);
1b026014
NIS
775 ST(0) = boolSV(RETVAL);
776 sv_2mortal(ST(0));
777 }
778 XSRETURN(1);
779}
780
781XS(XS_utf8_upgrade)
782{
97aff369 783 dVAR;
1b026014 784 dXSARGS;
58c0efa5 785 PERL_UNUSED_ARG(cv);
1b026014
NIS
786 if (items != 1)
787 Perl_croak(aTHX_ "Usage: utf8::upgrade(sv)");
c4420975
AL
788 else {
789 SV * const sv = ST(0);
1b026014
NIS
790 STRLEN RETVAL;
791 dXSTARG;
792
793 RETVAL = sv_utf8_upgrade(sv);
794 XSprePUSH; PUSHi((IV)RETVAL);
795 }
796 XSRETURN(1);
797}
798
799XS(XS_utf8_downgrade)
800{
97aff369 801 dVAR;
1b026014 802 dXSARGS;
58c0efa5 803 PERL_UNUSED_ARG(cv);
1b026014
NIS
804 if (items < 1 || items > 2)
805 Perl_croak(aTHX_ "Usage: utf8::downgrade(sv, failok=0)");
c4420975
AL
806 else {
807 SV * const sv = ST(0);
6867be6d
AL
808 const bool failok = (items < 2) ? 0 : (int)SvIV(ST(1));
809 const bool RETVAL = sv_utf8_downgrade(sv, failok);
1b026014 810
1b026014
NIS
811 ST(0) = boolSV(RETVAL);
812 sv_2mortal(ST(0));
813 }
814 XSRETURN(1);
815}
816
817XS(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
JH
823
824 if (items > 1)
825 Perl_croak(aTHX_ "Usage: utf8::native_to_unicode(sv)");
826
1b026014
NIS
827 ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv)));
828 XSRETURN(1);
829}
830
831XS(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
JH
837
838 if (items > 1)
839 Perl_croak(aTHX_ "Usage: utf8::unicode_to_native(sv)");
840
1b026014
NIS
841 ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv)));
842 XSRETURN(1);
843}
844
14a976d6 845XS(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
JH
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
JH
865 SvREADONLY_off(sv);
866 XSRETURN_NO;
867 }
868 }
14a976d6 869 XSRETURN_UNDEF; /* Can't happen. */
29569577
JH
870}
871
14a976d6 872XS(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
JH
883 SvREFCNT(sv) = SvIV(ST(1));
884 XSRETURN_IV(SvREFCNT(sv));
885 }
14a976d6 886 XSRETURN_UNDEF; /* Can't happen. */
29569577
JH
887}
888
f044d0d1 889XS(XS_Internals_hv_clear_placehold)
dfd4ef2f 890{
97aff369 891 dVAR;
dfd4ef2f 892 dXSARGS;
58c0efa5 893 PERL_UNUSED_ARG(cv);
6867be6d 894
3540d4ce
AB
895 if (items != 1)
896 Perl_croak(aTHX_ "Usage: UNIVERSAL::hv_clear_placeholders(hv)");
c4420975
AL
897 else {
898 HV * const hv = (HV *) SvRV(ST(0));
899 hv_clear_placeholders(hv);
900 XSRETURN(0);
901 }
dfd4ef2f 902}
39f7a870 903
39cff0d9
AE
904XS(XS_Regexp_DESTROY)
905{
96a5add6 906 PERL_UNUSED_CONTEXT;
53c1dcc0 907 PERL_UNUSED_ARG(cv);
39cff0d9
AE
908}
909
39f7a870
JH
910XS(XS_PerlIO_get_layers)
911{
97aff369 912 dVAR;
39f7a870 913 dXSARGS;
58c0efa5 914 PERL_UNUSED_ARG(cv);
39f7a870
JH
915 if (items < 1 || items % 2 == 0)
916 Perl_croak(aTHX_ "Usage: PerlIO_get_layers(filehandle[,args])");
5fef3b4a 917#ifdef USE_PERLIO
39f7a870
JH
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
AL
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
JH
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
JH
971 }
972
973 if (gv && (io = GvIO(gv))) {
974 dTARGET;
c4420975 975 AV* const av = PerlIO_get_layers(aTHX_ input ?
39f7a870
JH
976 IoIFP(io) : IoOFP(io));
977 I32 i;
c4420975 978 const I32 last = av_len(av);
39f7a870
JH
979 I32 nitem = 0;
980
981 for (i = last; i >= 0; i -= 3) {
c4420975
AL
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
AL
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
JH
989
990 if (details) {
ec3bab8e
NC
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
JH
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
MHM
1006 SVfARG(*namsvp),
1007 SVfARG(*argsvp)));
39f7a870 1008 else if (namok)
95b63a38 1009 XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf,
be2597df 1010 SVfARG(*namsvp)));
39f7a870
JH
1011 else
1012 XPUSHs(&PL_sv_undef);
1013 nitem++;
1014 if (flgok) {
c4420975 1015 const IV flags = SvIVX(*flgsvp);
39f7a870
JH
1016
1017 if (flags & PERLIO_F_UTF8) {
396482e1 1018 XPUSHs(newSVpvs("utf8"));
39f7a870
JH
1019 nitem++;
1020 }
1021 }
1022 }
1023 }
1024
1025 SvREFCNT_dec(av);
1026
1027 XSRETURN(nitem);
1028 }
1029 }
5fef3b4a 1030#endif
39f7a870
JH
1031
1032 XSRETURN(0);
1033}
1034
9a7034eb 1035XS(XS_Internals_hash_seed)
c910b28a 1036{
97aff369 1037 dVAR;
c85d3f85
NC
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
JH
1044}
1045
008fb0c0 1046XS(XS_Internals_rehash_seed)
8e90d776 1047{
97aff369 1048 dVAR;
8e90d776
NC
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
NC
1055}
1056
05619474
NC
1057XS(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
NC
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
YO
1074XS(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 1097XS(XS_re_regnames_count)
80305961 1098{
192b9cd1
AB
1099 REGEXP *rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1100 SV * ret;
80305961
YO
1101 dVAR;
1102 dXSARGS;
192b9cd1
AB
1103
1104 if (items != 0)
1105 Perl_croak(aTHX_ "Usage: %s(%s)", "re::regnames_count", "");
1106
1107 SP -= items;
1108
1109 if (!rx)
1110 XSRETURN_UNDEF;
1111
1112 ret = CALLREG_NAMED_BUFF_COUNT(rx);
1113
1114 SPAGAIN;
1115
1116 if (ret) {
1117 XPUSHs(ret);
1118 PUTBACK;
1119 return;
1120 } else {
1121 XSRETURN_UNDEF;
1122 }
1123}
1124
1125XS(XS_re_regname)
1126{
1127 dVAR;
1128 dXSARGS;
1129 REGEXP * rx;
1130 U32 flags;
1131 SV * ret;
1132
28d8d7f4 1133 if (items < 1 || items > 2)
192b9cd1
AB
1134 Perl_croak(aTHX_ "Usage: %s(%s)", "re::regname", "name[, all ]");
1135
80305961 1136 SP -= items;
80305961 1137
192b9cd1
AB
1138 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1139
1140 if (!rx)
1141 XSRETURN_UNDEF;
1142
1143 if (items == 2 && SvTRUE(ST(1))) {
1144 flags = RXf_HASH_ALL;
1145 } else {
1146 flags = RXf_HASH_ONE;
80305961 1147 }
192b9cd1
AB
1148 ret = CALLREG_NAMED_BUFF_FETCH(rx, ST(0), (flags | RXf_HASH_REGNAME));
1149
1150 if (ret) {
1151 if (SvROK(ret))
1152 XPUSHs(ret);
1153 else
1154 XPUSHs(SvREFCNT_inc(ret));
1155 XSRETURN(1);
1156 }
1157 XSRETURN_UNDEF;
80305961
YO
1158}
1159
192b9cd1 1160
80305961
YO
1161XS(XS_re_regnames)
1162{
192b9cd1 1163 dVAR;
80305961 1164 dXSARGS;
192b9cd1
AB
1165 REGEXP * rx;
1166 U32 flags;
1167 SV *ret;
1168 AV *av;
1169 I32 length;
1170 I32 i;
1171 SV **entry;
1172
1173 if (items > 1)
1174 Perl_croak(aTHX_ "Usage: %s(%s)", "re::regnames", "[all]");
1175
1176 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1177
1178 if (!rx)
1179 XSRETURN_UNDEF;
1180
1181 if (items == 1 && SvTRUE(ST(0))) {
1182 flags = RXf_HASH_ALL;
1183 } else {
1184 flags = RXf_HASH_ONE;
1185 }
1186
80305961 1187 SP -= items;
80305961 1188
192b9cd1
AB
1189 ret = CALLREG_NAMED_BUFF_ALL(rx, (flags | RXf_HASH_REGNAMES));
1190
1191 SPAGAIN;
1192
1193 SP -= items;
1194
1195 if (!ret)
1196 XSRETURN_UNDEF;
1197
1198 av = (AV*)SvRV(ret);
1199 length = av_len(av);
1200
1201 for (i = 0; i <= length; i++) {
1202 entry = av_fetch(av, i, FALSE);
1203
1204 if (!entry)
1205 Perl_croak(aTHX_ "NULL array element in re::regnames()");
1206
1207 XPUSHs(*entry);
80305961 1208 }
192b9cd1
AB
1209 PUTBACK;
1210 return;
80305961
YO
1211}
1212
192b9cd1 1213XS(XS_Tie_Hash_NamedCapture_FETCH)
80305961 1214{
192b9cd1 1215 dVAR;
80305961 1216 dXSARGS;
192b9cd1
AB
1217 REGEXP * rx;
1218 U32 flags;
1219 SV * ret;
1220
1221 if (items != 2)
1222 Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::STORE($key, $flags)");
1223
1224 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1225
1226 if (!rx)
1227 XSRETURN_UNDEF;
1228
80305961 1229 SP -= items;
192b9cd1
AB
1230
1231 flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0))));
1232 ret = CALLREG_NAMED_BUFF_FETCH(rx, ST(1), flags);
1233
1234 SPAGAIN;
1235
1236 if (ret) {
1237 if (SvROK(ret))
1238 XPUSHs(ret);
1239 else
1240 XPUSHs(SvREFCNT_inc(ret));
1241 PUTBACK;
1242 return;
1243 }
1244 XSRETURN_UNDEF;
1245}
1246
1247XS(XS_Tie_Hash_NamedCapture_STORE)
1248{
1249 dVAR;
1250 dXSARGS;
1251 REGEXP * rx;
1252 U32 flags;
1253
1254 if (items != 3)
1255 Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::STORE($key, $value, $flags)");
1256
1257 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1258
1259 if (!rx) {
1260 if (!PL_localizing)
1261 Perl_croak(aTHX_ PL_no_modify);
1262 else
28d8d7f4 1263 XSRETURN_UNDEF;
80305961 1264 }
192b9cd1
AB
1265
1266 SP -= items;
1267
1268 flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0))));
1269 CALLREG_NAMED_BUFF_STORE(rx,ST(1), ST(2), flags);
80305961
YO
1270}
1271
192b9cd1
AB
1272XS(XS_Tie_Hash_NamedCapture_DELETE)
1273{
1274 dVAR;
1275 dXSARGS;
1276 REGEXP * rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1277 U32 flags;
80305961 1278
192b9cd1
AB
1279 if (items != 2)
1280 Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::DELETE($key, $flags)");
1281
1282 if (!rx)
1283 Perl_croak(aTHX_ PL_no_modify);
1284
1285 SP -= items;
1286
1287 flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0))));
1288 CALLREG_NAMED_BUFF_DELETE(rx, ST(1), flags);
1289}
1290
1291XS(XS_Tie_Hash_NamedCapture_CLEAR)
80305961 1292{
192b9cd1 1293 dVAR;
80305961 1294 dXSARGS;
192b9cd1
AB
1295 REGEXP * rx;
1296 U32 flags;
1297
1298 if (items != 1)
1299 Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::CLEAR($flags)");
1300
1301 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1302
1303 if (!rx)
1304 Perl_croak(aTHX_ PL_no_modify);
1305
80305961 1306 SP -= items;
80305961 1307
192b9cd1
AB
1308 flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0))));
1309 CALLREG_NAMED_BUFF_CLEAR(rx, flags);
1310}
1311
1312XS(XS_Tie_Hash_NamedCapture_EXISTS)
1313{
1314 dVAR;
1315 dXSARGS;
1316 REGEXP * rx;
1317 U32 flags;
1318 SV * ret;
1319
1320 if (items != 2)
1321 Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::EXISTS($key, $flags)");
1322
1323 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1324
1325 if (!rx)
28d8d7f4 1326 XSRETURN_UNDEF;
192b9cd1
AB
1327
1328 SP -= items;
1329
1330 flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0))));
1331 ret = CALLREG_NAMED_BUFF_EXISTS(rx, ST(1), flags);
1332
1333 SPAGAIN;
1334
1335 XPUSHs(ret);
80305961
YO
1336 PUTBACK;
1337 return;
80305961
YO
1338}
1339
192b9cd1
AB
1340XS(XS_Tie_Hash_NamedCapture_FIRSTKEY)
1341{
1342 dVAR;
1343 dXSARGS;
1344 REGEXP * rx;
1345 U32 flags;
1346 SV * ret;
80305961 1347
192b9cd1
AB
1348 if (items != 1)
1349 Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::FIRSTKEY()");
1350
1351 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1352
1353 if (!rx)
1354 XSRETURN_UNDEF;
1355
1356 SP -= items;
1357
1358 flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0))));
1359 ret = CALLREG_NAMED_BUFF_FIRSTKEY(rx, flags);
1360
1361 SPAGAIN;
1362
1363 if (ret) {
1364 XPUSHs(SvREFCNT_inc(ret));
1365 PUTBACK;
1366 } else {
1367 XSRETURN_UNDEF;
1368 }
1369
1370}
1371
1372XS(XS_Tie_Hash_NamedCapture_NEXTKEY)
80305961 1373{
192b9cd1 1374 dVAR;
80305961 1375 dXSARGS;
192b9cd1
AB
1376 REGEXP * rx;
1377 U32 flags;
1378 SV * ret;
1379
1380 if (items != 2)
1381 Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::NEXTKEY($lastkey)");
1382
1383 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1384
1385 if (!rx)
1386 XSRETURN_UNDEF;
80305961 1387
80305961 1388 SP -= items;
192b9cd1
AB
1389
1390 flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0))));
1391 ret = CALLREG_NAMED_BUFF_NEXTKEY(rx, ST(1), flags);
1392
1393 SPAGAIN;
1394
1395 if (ret) {
1396 XPUSHs(ret);
80305961
YO
1397 } else {
1398 XSRETURN_UNDEF;
1399 }
1400 PUTBACK;
192b9cd1
AB
1401}
1402
1403XS(XS_Tie_Hash_NamedCapture_SCALAR)
1404{
1405 dVAR;
1406 dXSARGS;
1407 REGEXP * rx;
1408 U32 flags;
1409 SV * ret;
1410
1411 if (items != 1)
1412 Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::SCALAR()");
1413
1414 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1415
1416 if (!rx)
1417 XSRETURN_UNDEF;
1418
1419 SP -= items;
1420
1421 flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0))));
1422 ret = CALLREG_NAMED_BUFF_SCALAR(rx, flags);
1423
1424 SPAGAIN;
1425
1426 if (ret) {
1427 XPUSHs(ret);
1428 PUTBACK;
1429 return;
1430 } else {
1431 XSRETURN_UNDEF;
1432 }
1433}
1434
1435XS(XS_Tie_Hash_NamedCapture_flags)
1436{
1437 dVAR;
1438 dXSARGS;
1439
1440 if (items != 0)
1441 Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::flags()");
1442
1443 XPUSHs(sv_2mortal(newSVuv(RXf_HASH_ONE)));
1444 XPUSHs(sv_2mortal(newSVuv(RXf_HASH_ALL)));
1445 PUTBACK;
1446 return;
80305961
YO
1447}
1448
1449
241d1a3b
NC
1450/*
1451 * Local variables:
1452 * c-indentation-style: bsd
1453 * c-basic-offset: 4
1454 * indent-tabs-mode: t
1455 * End:
1456 *
37442d52
RGS
1457 * ex: set ts=8 sts=4 sw=4 noet:
1458 */