This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate:
[perl5.git] / universal.c
CommitLineData
d6376244
JH
1/* universal.c
2 *
f27f8384 3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
583439ab 4 * 2005, 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
40d34c0d
SB
17/* This file contains the code that implements the functions in Perl's
18 * UNIVERSAL package, such as UNIVERSAL->can().
19 */
20
6d4a7be2 21#include "EXTERN.h"
864dbfa3 22#define PERL_IN_UNIVERSAL_C
6d4a7be2 23#include "perl.h"
6d4a7be2 24
5095c3e6
JH
25#ifdef USE_PERLIO
26#include "perliol.h" /* For the PERLIO_F_XXX */
27#endif
28
6d4a7be2 29/*
30 * Contributed by Graham Barr <Graham.Barr@tiuk.ti.com>
31 * The main guts of traverse_isa was actually copied from gv_fetchmeth
32 */
33
76e3520e 34STATIC SV *
301daebc
MS
35S_isa_lookup(pTHX_ HV *stash, const char *name, HV* name_stash,
36 int len, int level)
6d4a7be2 37{
38 AV* av;
39 GV* gv;
40 GV** gvp;
41 HV* hv = Nullhv;
46e4b22b 42 SV* subgen = Nullsv;
6d4a7be2 43
301daebc
MS
44 /* A stash/class can go by many names (ie. User == main::User), so
45 we compare the stash itself just in case */
46 if (name_stash && (stash == name_stash))
47 return &PL_sv_yes;
6d4a7be2 48
46e4b22b 49 if (strEQ(HvNAME(stash), name))
3280af22 50 return &PL_sv_yes;
6d4a7be2 51
4e58e0cf
JH
52 if (strEQ(name, "UNIVERSAL"))
53 return &PL_sv_yes;
54
6d4a7be2 55 if (level > 100)
46e4b22b
GS
56 Perl_croak(aTHX_ "Recursive inheritance detected in package '%s'",
57 HvNAME(stash));
6d4a7be2 58
59 gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, FALSE);
60
46e4b22b
GS
61 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (subgen = GvSV(gv))
62 && (hv = GvHV(gv)))
63 {
eb160463 64 if (SvIV(subgen) == (IV)PL_sub_generation) {
46e4b22b
GS
65 SV* sv;
66 SV** svp = (SV**)hv_fetch(hv, name, len, FALSE);
67 if (svp && (sv = *svp) != (SV*)&PL_sv_undef) {
68 DEBUG_o( Perl_deb(aTHX_ "Using cached ISA %s for package %s\n",
69 name, HvNAME(stash)) );
70 return sv;
71 }
72 }
73 else {
74 DEBUG_o( Perl_deb(aTHX_ "ISA Cache in package %s is stale\n",
75 HvNAME(stash)) );
76 hv_clear(hv);
77 sv_setiv(subgen, PL_sub_generation);
78 }
6d4a7be2 79 }
80
81 gvp = (GV**)hv_fetch(stash,"ISA",3,FALSE);
46e4b22b 82
3280af22 83 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (av = GvAV(gv))) {
46e4b22b 84 if (!hv || !subgen) {
6d4a7be2 85 gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, TRUE);
86
87 gv = *gvp;
88
89 if (SvTYPE(gv) != SVt_PVGV)
90 gv_init(gv, stash, "::ISA::CACHE::", 14, TRUE);
91
46e4b22b
GS
92 if (!hv)
93 hv = GvHVn(gv);
94 if (!subgen) {
95 subgen = newSViv(PL_sub_generation);
96 GvSV(gv) = subgen;
97 }
6d4a7be2 98 }
46e4b22b 99 if (hv) {
6d4a7be2 100 SV** svp = AvARRAY(av);
93965878
NIS
101 /* NOTE: No support for tied ISA */
102 I32 items = AvFILLp(av) + 1;
6d4a7be2 103 while (items--) {
104 SV* sv = *svp++;
105 HV* basestash = gv_stashsv(sv, FALSE);
106 if (!basestash) {
599cee73 107 if (ckWARN(WARN_MISC))
9014280d 108 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
c293eb2b
NC
109 "Can't locate package %"SVf" for @%s::ISA",
110 sv, HvNAME(stash));
6d4a7be2 111 continue;
112 }
301daebc
MS
113 if (&PL_sv_yes == isa_lookup(basestash, name, name_stash,
114 len, level + 1)) {
3280af22
NIS
115 (void)hv_store(hv,name,len,&PL_sv_yes,0);
116 return &PL_sv_yes;
6d4a7be2 117 }
118 }
3280af22 119 (void)hv_store(hv,name,len,&PL_sv_no,0);
6d4a7be2 120 }
121 }
4e58e0cf 122 return &PL_sv_no;
6d4a7be2 123}
124
954c1994 125/*
ccfc67b7
JH
126=head1 SV Manipulation Functions
127
954c1994
GS
128=for apidoc sv_derived_from
129
130Returns a boolean indicating whether the SV is derived from the specified
131class. This is the function that implements C<UNIVERSAL::isa>. It works
132for class names as well as for objects.
133
134=cut
135*/
136
55497cff 137bool
864dbfa3 138Perl_sv_derived_from(pTHX_ SV *sv, const char *name)
55497cff 139{
c05e0e2f 140 const char *type;
55497cff 141 HV *stash;
301daebc 142 HV *name_stash;
46e4b22b 143
55497cff 144 stash = Nullhv;
145 type = Nullch;
46e4b22b 146
55497cff 147 if (SvGMAGICAL(sv))
148 mg_get(sv) ;
149
150 if (SvROK(sv)) {
151 sv = SvRV(sv);
152 type = sv_reftype(sv,0);
46e4b22b 153 if (SvOBJECT(sv))
55497cff 154 stash = SvSTASH(sv);
155 }
156 else {
157 stash = gv_stashsv(sv, FALSE);
158 }
46e4b22b 159
301daebc
MS
160 name_stash = gv_stashpv(name, FALSE);
161
55497cff 162 return (type && strEQ(type,name)) ||
301daebc
MS
163 (stash && isa_lookup(stash, name, name_stash, strlen(name), 0)
164 == &PL_sv_yes)
55497cff 165 ? TRUE
166 : FALSE ;
55497cff 167}
168
1b026014
NIS
169#include "XSUB.h"
170
acfe0abc
GS
171void XS_UNIVERSAL_isa(pTHX_ CV *cv);
172void XS_UNIVERSAL_can(pTHX_ CV *cv);
173void XS_UNIVERSAL_VERSION(pTHX_ CV *cv);
18729d3e 174XS(XS_utf8_is_utf8);
1b026014
NIS
175XS(XS_utf8_valid);
176XS(XS_utf8_encode);
177XS(XS_utf8_decode);
178XS(XS_utf8_upgrade);
179XS(XS_utf8_downgrade);
180XS(XS_utf8_unicode_to_native);
181XS(XS_utf8_native_to_unicode);
29569577
JH
182XS(XS_Internals_SvREADONLY);
183XS(XS_Internals_SvREFCNT);
f044d0d1 184XS(XS_Internals_hv_clear_placehold);
5095c3e6 185XS(XS_PerlIO_get_layers);
5e8f8cda 186XS(XS_Regexp_DESTROY);
108e6616 187XS(XS_Internals_hash_seed);
7f047bfa 188XS(XS_Internals_rehash_seed);
cdeba5a7 189XS(XS_Internals_HvREHASH);
0cb96387
GS
190
191void
192Perl_boot_core_UNIVERSAL(pTHX)
193{
c05e0e2f 194 const char file[] = __FILE__;
0cb96387
GS
195
196 newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file);
197 newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file);
198 newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file);
18729d3e 199 newXS("utf8::is_utf8", XS_utf8_is_utf8, file);
1b026014
NIS
200 newXS("utf8::valid", XS_utf8_valid, file);
201 newXS("utf8::encode", XS_utf8_encode, file);
202 newXS("utf8::decode", XS_utf8_decode, file);
203 newXS("utf8::upgrade", XS_utf8_upgrade, file);
204 newXS("utf8::downgrade", XS_utf8_downgrade, file);
205 newXS("utf8::native_to_unicode", XS_utf8_native_to_unicode, file);
206 newXS("utf8::unicode_to_native", XS_utf8_unicode_to_native, file);
29569577
JH
207 newXSproto("Internals::SvREADONLY",XS_Internals_SvREADONLY, file, "\\[$%@];$");
208 newXSproto("Internals::SvREFCNT",XS_Internals_SvREFCNT, file, "\\[$%@];$");
dfd4ef2f 209 newXSproto("Internals::hv_clear_placeholders",
f044d0d1 210 XS_Internals_hv_clear_placehold, file, "\\%");
e0dae7fe
JH
211 newXSproto("PerlIO::get_layers",
212 XS_PerlIO_get_layers, file, "*;@");
5e8f8cda 213 newXS("Regexp::DESTROY", XS_Regexp_DESTROY, file);
108e6616 214 newXSproto("Internals::hash_seed",XS_Internals_hash_seed, file, "");
7f047bfa 215 newXSproto("Internals::rehash_seed",XS_Internals_rehash_seed, file, "");
cdeba5a7 216 newXSproto("Internals::HvREHASH", XS_Internals_HvREHASH, file, "\\%");
0cb96387
GS
217}
218
55497cff 219
6d4a7be2 220XS(XS_UNIVERSAL_isa)
221{
222 dXSARGS;
55497cff 223 SV *sv;
c501bbfe 224 const char *name;
2d8e6c8d 225 STRLEN n_a;
6d4a7be2 226
227 if (items != 2)
cea2e8a9 228 Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)");
6d4a7be2 229
230 sv = ST(0);
f8f70380 231
d3f7f2b2
GS
232 if (SvGMAGICAL(sv))
233 mg_get(sv);
234
6563884d
JH
235 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
236 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
f8f70380
GS
237 XSRETURN_UNDEF;
238
c501bbfe 239 name = (const char *)SvPV(ST(1),n_a);
6d4a7be2 240
54310121 241 ST(0) = boolSV(sv_derived_from(sv, name));
6d4a7be2 242 XSRETURN(1);
243}
244
6d4a7be2 245XS(XS_UNIVERSAL_can)
246{
247 dXSARGS;
248 SV *sv;
c501bbfe 249 const char *name;
6d4a7be2 250 SV *rv;
6f08146e 251 HV *pkg = NULL;
2d8e6c8d 252 STRLEN n_a;
6d4a7be2 253
254 if (items != 2)
cea2e8a9 255 Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)");
6d4a7be2 256
257 sv = ST(0);
f8f70380 258
d3f7f2b2
GS
259 if (SvGMAGICAL(sv))
260 mg_get(sv);
261
6563884d
JH
262 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
263 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
f8f70380
GS
264 XSRETURN_UNDEF;
265
c501bbfe 266 name = (const char *)SvPV(ST(1),n_a);
3280af22 267 rv = &PL_sv_undef;
6d4a7be2 268
46e4b22b 269 if (SvROK(sv)) {
6f08146e 270 sv = (SV*)SvRV(sv);
46e4b22b 271 if (SvOBJECT(sv))
6f08146e
NIS
272 pkg = SvSTASH(sv);
273 }
274 else {
275 pkg = gv_stashsv(sv, FALSE);
276 }
277
278 if (pkg) {
dc848c6f 279 GV *gv = gv_fetchmethod_autoload(pkg, name, FALSE);
280 if (gv && isGV(gv))
281 rv = sv_2mortal(newRV((SV*)GvCV(gv)));
6d4a7be2 282 }
283
284 ST(0) = rv;
285 XSRETURN(1);
286}
287
6d4a7be2 288XS(XS_UNIVERSAL_VERSION)
289{
290 dXSARGS;
291 HV *pkg;
292 GV **gvp;
293 GV *gv;
294 SV *sv;
c05e0e2f 295 const char *undef;
6d4a7be2 296
1571675a 297 if (SvROK(ST(0))) {
6d4a7be2 298 sv = (SV*)SvRV(ST(0));
1571675a 299 if (!SvOBJECT(sv))
cea2e8a9 300 Perl_croak(aTHX_ "Cannot find version of an unblessed reference");
6d4a7be2 301 pkg = SvSTASH(sv);
302 }
303 else {
304 pkg = gv_stashsv(ST(0), FALSE);
305 }
306
307 gvp = pkg ? (GV**)hv_fetch(pkg,"VERSION",7,FALSE) : Null(GV**);
308
d4bea2fb 309 if (gvp && isGV(gv = *gvp) && SvOK(sv = GvSV(gv))) {
6d4a7be2 310 SV *nsv = sv_newmortal();
311 sv_setsv(nsv, sv);
312 sv = nsv;
313 undef = Nullch;
314 }
315 else {
3280af22 316 sv = (SV*)&PL_sv_undef;
6d4a7be2 317 undef = "(undef)";
318 }
319
1571675a
GS
320 if (items > 1) {
321 STRLEN len;
322 SV *req = ST(1);
323
62658f4d
PM
324 if (undef) {
325 if (pkg)
326 Perl_croak(aTHX_
327 "%s does not define $%s::VERSION--version check failed",
328 HvNAME(pkg), HvNAME(pkg));
329 else {
c05e0e2f 330 const char *str = SvPVx(ST(0), len);
62658f4d
PM
331
332 Perl_croak(aTHX_
333 "%s defines neither package nor VERSION--version check failed", str);
334 }
335 }
1571675a
GS
336 if (!SvNIOK(sv) && SvPOK(sv)) {
337 char *str = SvPVx(sv,len);
338 while (len) {
339 --len;
340 /* XXX could DWIM "1.2.3" here */
341 if (!isDIGIT(str[len]) && str[len] != '.' && str[len] != '_')
342 break;
343 }
344 if (len) {
4305d8ab 345 if (SvNOK(req) && SvPOK(req)) {
1571675a
GS
346 /* they said C<use Foo v1.2.3> and $Foo::VERSION
347 * doesn't look like a float: do string compare */
348 if (sv_cmp(req,sv) == 1) {
d2560b70
RB
349 Perl_croak(aTHX_ "%s v%"VDf" required--"
350 "this is only v%"VDf,
1571675a
GS
351 HvNAME(pkg), req, sv);
352 }
353 goto finish;
354 }
355 /* they said C<use Foo 1.002_003> and $Foo::VERSION
356 * doesn't look like a float: force numeric compare */
155aba94 357 (void)SvUPGRADE(sv, SVt_PVNV);
1571675a
GS
358 SvNVX(sv) = str_to_version(sv);
359 SvPOK_off(sv);
360 SvNOK_on(sv);
361 }
362 }
363 /* if we get here, we're looking for a numeric comparison,
364 * so force the required version into a float, even if they
365 * said C<use Foo v1.2.3> */
4305d8ab 366 if (SvNOK(req) && SvPOK(req)) {
1571675a
GS
367 NV n = SvNV(req);
368 req = sv_newmortal();
369 sv_setnv(req, n);
370 }
371
f6eb1a96 372 if (SvNV(req) > SvNV(sv))
1571675a 373 Perl_croak(aTHX_ "%s version %s required--this is only version %s",
f6eb1a96 374 HvNAME(pkg), SvPV_nolen(req), SvPV_nolen(sv));
2d8e6c8d 375 }
6d4a7be2 376
1571675a 377finish:
6d4a7be2 378 ST(0) = sv;
379
380 XSRETURN(1);
381}
382
18729d3e
JH
383XS(XS_utf8_is_utf8)
384{
385 dXSARGS;
386 if (items != 1)
387 Perl_croak(aTHX_ "Usage: utf8::is_utf8(sv)");
388 {
c501bbfe 389 const SV *sv = ST(0);
18729d3e
JH
390 {
391 if (SvUTF8(sv))
392 XSRETURN_YES;
393 else
394 XSRETURN_NO;
395 }
396 }
397 XSRETURN_EMPTY;
398}
399
1b026014
NIS
400XS(XS_utf8_valid)
401{
18729d3e
JH
402 dXSARGS;
403 if (items != 1)
404 Perl_croak(aTHX_ "Usage: utf8::valid(sv)");
405 {
406 SV * sv = ST(0);
407 {
408 STRLEN len;
c501bbfe
AL
409 const char *s = SvPV(sv,len);
410 if (!SvUTF8(sv) || is_utf8_string((const U8*)s,len))
18729d3e
JH
411 XSRETURN_YES;
412 else
413 XSRETURN_NO;
414 }
415 }
416 XSRETURN_EMPTY;
1b026014
NIS
417}
418
419XS(XS_utf8_encode)
420{
421 dXSARGS;
422 if (items != 1)
423 Perl_croak(aTHX_ "Usage: utf8::encode(sv)");
424 {
425 SV * sv = ST(0);
426
427 sv_utf8_encode(sv);
428 }
429 XSRETURN_EMPTY;
430}
431
432XS(XS_utf8_decode)
433{
434 dXSARGS;
435 if (items != 1)
436 Perl_croak(aTHX_ "Usage: utf8::decode(sv)");
437 {
438 SV * sv = ST(0);
c501bbfe 439 const bool RETVAL = sv_utf8_decode(sv);
1b026014
NIS
440 ST(0) = boolSV(RETVAL);
441 sv_2mortal(ST(0));
442 }
443 XSRETURN(1);
444}
445
446XS(XS_utf8_upgrade)
447{
448 dXSARGS;
449 if (items != 1)
450 Perl_croak(aTHX_ "Usage: utf8::upgrade(sv)");
451 {
452 SV * sv = ST(0);
453 STRLEN RETVAL;
454 dXSTARG;
455
456 RETVAL = sv_utf8_upgrade(sv);
457 XSprePUSH; PUSHi((IV)RETVAL);
458 }
459 XSRETURN(1);
460}
461
462XS(XS_utf8_downgrade)
463{
464 dXSARGS;
465 if (items < 1 || items > 2)
466 Perl_croak(aTHX_ "Usage: utf8::downgrade(sv, failok=0)");
467 {
468 SV * sv = ST(0);
c501bbfe
AL
469 const bool failok = (items < 2) ? 0 : (int)SvIV(ST(1));
470 const bool RETVAL = sv_utf8_downgrade(sv, failok);
1b026014 471
1b026014
NIS
472 ST(0) = boolSV(RETVAL);
473 sv_2mortal(ST(0));
474 }
475 XSRETURN(1);
476}
477
478XS(XS_utf8_native_to_unicode)
479{
480 dXSARGS;
c501bbfe 481 const UV uv = SvUV(ST(0));
b7953727
JH
482
483 if (items > 1)
484 Perl_croak(aTHX_ "Usage: utf8::native_to_unicode(sv)");
485
1b026014
NIS
486 ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv)));
487 XSRETURN(1);
488}
489
490XS(XS_utf8_unicode_to_native)
491{
492 dXSARGS;
c501bbfe 493 const UV uv = SvUV(ST(0));
b7953727
JH
494
495 if (items > 1)
496 Perl_croak(aTHX_ "Usage: utf8::unicode_to_native(sv)");
497
1b026014
NIS
498 ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv)));
499 XSRETURN(1);
500}
501
14a976d6 502XS(XS_Internals_SvREADONLY) /* This is dangerous stuff. */
29569577
JH
503{
504 dXSARGS;
505 SV *sv = SvRV(ST(0));
c501bbfe 506
29569577
JH
507 if (items == 1) {
508 if (SvREADONLY(sv))
509 XSRETURN_YES;
510 else
511 XSRETURN_NO;
512 }
513 else if (items == 2) {
514 if (SvTRUE(ST(1))) {
515 SvREADONLY_on(sv);
516 XSRETURN_YES;
517 }
518 else {
14a976d6 519 /* I hope you really know what you are doing. */
29569577
JH
520 SvREADONLY_off(sv);
521 XSRETURN_NO;
522 }
523 }
14a976d6 524 XSRETURN_UNDEF; /* Can't happen. */
29569577
JH
525}
526
14a976d6 527XS(XS_Internals_SvREFCNT) /* This is dangerous stuff. */
29569577
JH
528{
529 dXSARGS;
530 SV *sv = SvRV(ST(0));
c501bbfe 531
29569577 532 if (items == 1)
14a976d6 533 XSRETURN_IV(SvREFCNT(sv) - 1); /* Minus the ref created for us. */
29569577 534 else if (items == 2) {
14a976d6 535 /* I hope you really know what you are doing. */
29569577
JH
536 SvREFCNT(sv) = SvIV(ST(1));
537 XSRETURN_IV(SvREFCNT(sv));
538 }
14a976d6 539 XSRETURN_UNDEF; /* Can't happen. */
29569577
JH
540}
541
f044d0d1 542XS(XS_Internals_hv_clear_placehold)
dfd4ef2f
NC
543{
544 dXSARGS;
545 HV *hv = (HV *) SvRV(ST(0));
c501bbfe 546
704547c4
AB
547 if (items != 1)
548 Perl_croak(aTHX_ "Usage: UNIVERSAL::hv_clear_placeholders(hv)");
549 hv_clear_placeholders(hv);
dfd4ef2f
NC
550 XSRETURN(0);
551}
5095c3e6 552
5e8f8cda
JH
553XS(XS_Regexp_DESTROY)
554{
5e8f8cda
JH
555}
556
5095c3e6
JH
557XS(XS_PerlIO_get_layers)
558{
559 dXSARGS;
560 if (items < 1 || items % 2 == 0)
561 Perl_croak(aTHX_ "Usage: PerlIO_get_layers(filehandle[,args])");
e0dae7fe 562#ifdef USE_PERLIO
5095c3e6
JH
563 {
564 SV * sv;
565 GV * gv;
566 IO * io;
567 bool input = TRUE;
568 bool details = FALSE;
569
570 if (items > 1) {
5095c3e6
JH
571 SV **svp;
572
573 for (svp = MARK + 2; svp <= SP; svp += 2) {
574 SV **varp = svp;
575 SV **valp = svp + 1;
576 STRLEN klen;
c501bbfe 577 const char *key = SvPV(*varp, klen);
5095c3e6
JH
578
579 switch (*key) {
580 case 'i':
581 if (klen == 5 && memEQ(key, "input", 5)) {
582 input = SvTRUE(*valp);
583 break;
584 }
585 goto fail;
586 case 'o':
587 if (klen == 6 && memEQ(key, "output", 6)) {
588 input = !SvTRUE(*valp);
589 break;
590 }
591 goto fail;
592 case 'd':
593 if (klen == 7 && memEQ(key, "details", 7)) {
594 details = SvTRUE(*valp);
595 break;
596 }
597 goto fail;
598 default:
599 fail:
600 Perl_croak(aTHX_
601 "get_layers: unknown argument '%s'",
602 key);
603 }
604 }
605
606 SP -= (items - 1);
607 }
608
609 sv = POPs;
610 gv = (GV*)sv;
611
612 if (!isGV(sv)) {
613 if (SvROK(sv) && isGV(SvRV(sv)))
614 gv = (GV*)SvRV(sv);
615 else
616 gv = gv_fetchpv(SvPVX(sv), FALSE, SVt_PVIO);
617 }
618
619 if (gv && (io = GvIO(gv))) {
620 dTARGET;
621 AV* av = PerlIO_get_layers(aTHX_ input ?
622 IoIFP(io) : IoOFP(io));
623 I32 i;
624 I32 last = av_len(av);
625 I32 nitem = 0;
626
627 for (i = last; i >= 0; i -= 3) {
628 SV **namsvp;
629 SV **argsvp;
630 SV **flgsvp;
631 bool namok, argok, flgok;
632
633 namsvp = av_fetch(av, i - 2, FALSE);
634 argsvp = av_fetch(av, i - 1, FALSE);
635 flgsvp = av_fetch(av, i, FALSE);
636
637 namok = namsvp && *namsvp && SvPOK(*namsvp);
638 argok = argsvp && *argsvp && SvPOK(*argsvp);
639 flgok = flgsvp && *flgsvp && SvIOK(*flgsvp);
640
641 if (details) {
642 XPUSHs(namok ?
5e7e76a3 643 newSVpv(SvPVX_const(*namsvp), 0) : &PL_sv_undef);
5095c3e6 644 XPUSHs(argok ?
5e7e76a3 645 newSVpv(SvPVX_const(*argsvp), 0) : &PL_sv_undef);
5095c3e6
JH
646 if (flgok)
647 XPUSHi(SvIVX(*flgsvp));
648 else
649 XPUSHs(&PL_sv_undef);
650 nitem += 3;
651 }
652 else {
653 if (namok && argok)
654 XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf"(%"SVf")",
655 *namsvp, *argsvp));
656 else if (namok)
657 XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf, *namsvp));
658 else
659 XPUSHs(&PL_sv_undef);
660 nitem++;
661 if (flgok) {
662 IV flags = SvIVX(*flgsvp);
663
664 if (flags & PERLIO_F_UTF8) {
665 XPUSHs(newSVpvn("utf8", 4));
666 nitem++;
667 }
668 }
669 }
670 }
671
672 SvREFCNT_dec(av);
673
674 XSRETURN(nitem);
675 }
676 }
e0dae7fe 677#endif
5095c3e6
JH
678
679 XSRETURN(0);
680}
681
108e6616 682XS(XS_Internals_hash_seed)
e8e58922 683{
108e6616
NC
684 /* Using dXSARGS would also have dITEM and dSP,
685 * which define 2 unused local variables. */
e503e849 686 dAXMARK;
4a04c497 687 XSRETURN_UV(PERL_HASH_SEED);
e8e58922
JH
688}
689
7f047bfa 690XS(XS_Internals_rehash_seed)
6c2cec5b
NC
691{
692 /* Using dXSARGS would also have dITEM and dSP,
693 * which define 2 unused local variables. */
e503e849 694 dAXMARK;
7f047bfa 695 XSRETURN_UV(PL_rehash_seed);
6c2cec5b
NC
696}
697
cdeba5a7
NC
698XS(XS_Internals_HvREHASH) /* Subject to change */
699{
700 dXSARGS;
701 if (SvROK(ST(0))) {
c501bbfe 702 const HV *hv = (HV *) SvRV(ST(0));
cdeba5a7
NC
703 if (items == 1 && SvTYPE(hv) == SVt_PVHV) {
704 if (HvREHASH(hv))
705 XSRETURN_YES;
706 else
707 XSRETURN_NO;
708 }
709 }
710 Perl_croak(aTHX_ "Internals::HvREHASH $hashref");
711}
583439ab
NC
712
713/*
714 * Local variables:
715 * c-indentation-style: bsd
716 * c-basic-offset: 4
717 * indent-tabs-mode: t
718 * End:
719 *
d8294a4d
NC
720 * ex: set ts=8 sts=4 sw=4 noet:
721 */