This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Silence empty if-statement warning.
[perl5.git] / universal.c
CommitLineData
d6376244
JH
1/* universal.c
2 *
b5f8cc5c 3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
acde74e1 4 * 2005, 2006, 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().
19 */
20
6d4a7be2 21#include "EXTERN.h"
864dbfa3 22#define PERL_IN_UNIVERSAL_C
6d4a7be2 23#include "perl.h"
6d4a7be2 24
39f7a870
JH
25#ifdef USE_PERLIO
26#include "perliol.h" /* For the PERLIO_F_XXX */
27#endif
28
6d4a7be2
PP
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
a9ec700e 34STATIC bool
34692224 35S_isa_lookup(pTHX_ HV *stash, const char *name, const HV* const name_stash,
301daebc 36 int len, int level)
6d4a7be2 37{
97aff369 38 dVAR;
6d4a7be2
PP
39 AV* av;
40 GV* gv;
41 GV** gvp;
5c284bb0 42 HV* hv = NULL;
c445ea15 43 SV* subgen = NULL;
bfcb3514 44 const char *hvname;
6d4a7be2 45
301daebc
MS
46 /* A stash/class can go by many names (ie. User == main::User), so
47 we compare the stash itself just in case */
34692224 48 if (name_stash && ((const HV *)stash == name_stash))
a9ec700e 49 return TRUE;
6d4a7be2 50
bfcb3514
NC
51 hvname = HvNAME_get(stash);
52
53 if (strEQ(hvname, name))
a9ec700e 54 return TRUE;
6d4a7be2 55
a1d407e8 56 if (strEQ(name, "UNIVERSAL"))
a9ec700e 57 return TRUE;
a1d407e8 58
6d4a7be2 59 if (level > 100)
46e4b22b 60 Perl_croak(aTHX_ "Recursive inheritance detected in package '%s'",
bfcb3514 61 hvname);
6d4a7be2 62
017a3ce5 63 gvp = (GV**)hv_fetchs(stash, "::ISA::CACHE::", FALSE);
6d4a7be2 64
46e4b22b
GS
65 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (subgen = GvSV(gv))
66 && (hv = GvHV(gv)))
67 {
eb160463 68 if (SvIV(subgen) == (IV)PL_sub_generation) {
7452cf6a 69 SV** const svp = (SV**)hv_fetch(hv, name, len, FALSE);
34692224
AL
70 if (svp) {
71 SV * const sv = *svp;
17bcfd59 72#ifdef DEBUGGING
34692224
AL
73 if (sv != &PL_sv_undef)
74 DEBUG_o( Perl_deb(aTHX_ "Using cached ISA %s for package %s\n",
75 name, hvname) );
17bcfd59 76#endif
a9ec700e 77 return (sv == &PL_sv_yes);
46e4b22b
GS
78 }
79 }
80 else {
81 DEBUG_o( Perl_deb(aTHX_ "ISA Cache in package %s is stale\n",
bfcb3514 82 hvname) );
46e4b22b
GS
83 hv_clear(hv);
84 sv_setiv(subgen, PL_sub_generation);
85 }
6d4a7be2
PP
86 }
87
017a3ce5 88 gvp = (GV**)hv_fetchs(stash, "ISA", FALSE);
46e4b22b 89
3280af22 90 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (av = GvAV(gv))) {
46e4b22b 91 if (!hv || !subgen) {
017a3ce5 92 gvp = (GV**)hv_fetchs(stash, "::ISA::CACHE::", TRUE);
6d4a7be2
PP
93
94 gv = *gvp;
95
96 if (SvTYPE(gv) != SVt_PVGV)
97 gv_init(gv, stash, "::ISA::CACHE::", 14, TRUE);
98
46e4b22b
GS
99 if (!hv)
100 hv = GvHVn(gv);
101 if (!subgen) {
102 subgen = newSViv(PL_sub_generation);
103 GvSV(gv) = subgen;
104 }
6d4a7be2 105 }
46e4b22b 106 if (hv) {
6d4a7be2 107 SV** svp = AvARRAY(av);
93965878
NIS
108 /* NOTE: No support for tied ISA */
109 I32 items = AvFILLp(av) + 1;
6d4a7be2 110 while (items--) {
c4420975
AL
111 SV* const sv = *svp++;
112 HV* const basestash = gv_stashsv(sv, FALSE);
6d4a7be2 113 if (!basestash) {
599cee73 114 if (ckWARN(WARN_MISC))
9014280d 115 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
bfcb3514 116 "Can't locate package %"SVf" for @%s::ISA",
95b63a38 117 (void*)sv, hvname);
6d4a7be2
PP
118 continue;
119 }
a9ec700e 120 if (isa_lookup(basestash, name, name_stash, len, level + 1)) {
3280af22 121 (void)hv_store(hv,name,len,&PL_sv_yes,0);
a9ec700e 122 return TRUE;
6d4a7be2
PP
123 }
124 }
3280af22 125 (void)hv_store(hv,name,len,&PL_sv_no,0);
6d4a7be2
PP
126 }
127 }
a9ec700e 128 return FALSE;
6d4a7be2
PP
129}
130
954c1994 131/*
ccfc67b7
JH
132=head1 SV Manipulation Functions
133
954c1994
GS
134=for apidoc sv_derived_from
135
6885da0e 136Returns a boolean indicating whether the SV is derived from the specified class
137I<at the C level>. To check derivation at the Perl level, call C<isa()> as a
138normal Perl method.
954c1994
GS
139
140=cut
141*/
142
55497cff 143bool
864dbfa3 144Perl_sv_derived_from(pTHX_ SV *sv, const char *name)
55497cff 145{
97aff369 146 dVAR;
0b6f4f5c 147 HV *stash;
46e4b22b 148
5b295bef 149 SvGETMAGIC(sv);
55497cff
PP
150
151 if (SvROK(sv)) {
0b6f4f5c 152 const char *type;
55497cff
PP
153 sv = SvRV(sv);
154 type = sv_reftype(sv,0);
0b6f4f5c
AL
155 if (type && strEQ(type,name))
156 return TRUE;
157 stash = SvOBJECT(sv) ? SvSTASH(sv) : NULL;
55497cff
PP
158 }
159 else {
160 stash = gv_stashsv(sv, FALSE);
161 }
46e4b22b 162
0b6f4f5c
AL
163 if (stash) {
164 HV * const name_stash = gv_stashpv(name, FALSE);
a9ec700e 165 return isa_lookup(stash, name, name_stash, strlen(name), 0);
0b6f4f5c
AL
166 }
167 else
168 return FALSE;
301daebc 169
55497cff
PP
170}
171
cbc021f9 172/*
173=for apidoc sv_does
174
175Returns a boolean indicating whether the SV performs a specific, named role.
176The SV can be a Perl object or the name of a Perl class.
177
178=cut
179*/
180
1b026014
NIS
181#include "XSUB.h"
182
cbc021f9 183bool
184Perl_sv_does(pTHX_ SV *sv, const char *name)
185{
186 const char *classname;
187 bool does_it;
188
189 dSP;
190 ENTER;
191 SAVETMPS;
192
193 SvGETMAGIC(sv);
194
195 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
196 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
197 return FALSE;
198
199 if (sv_isobject(sv)) {
200 classname = sv_reftype(SvRV(sv),TRUE);
201 } else {
202 classname = SvPV(sv,PL_na);
203 }
204
205 if (strEQ(name,classname))
206 return TRUE;
207
208 PUSHMARK(SP);
209 XPUSHs(sv);
210 XPUSHs(sv_2mortal(newSVpv(name, 0)));
211 PUTBACK;
212
213 call_method("isa", G_SCALAR);
214 SPAGAIN;
215
216 does_it = SvTRUE( TOPs );
217 FREETMPS;
218 LEAVE;
219
220 return does_it;
221}
222
27da23d5
JH
223PERL_XS_EXPORT_C void XS_UNIVERSAL_isa(pTHX_ CV *cv);
224PERL_XS_EXPORT_C void XS_UNIVERSAL_can(pTHX_ CV *cv);
cbc021f9 225PERL_XS_EXPORT_C void XS_UNIVERSAL_DOES(pTHX_ CV *cv);
27da23d5 226PERL_XS_EXPORT_C void XS_UNIVERSAL_VERSION(pTHX_ CV *cv);
439cb1c4
JP
227XS(XS_version_new);
228XS(XS_version_stringify);
229XS(XS_version_numify);
9137345a 230XS(XS_version_normal);
439cb1c4
JP
231XS(XS_version_vcmp);
232XS(XS_version_boolean);
2dfd8427
AL
233#ifdef HASATTRIBUTE_NORETURN
234XS(XS_version_noop) __attribute__noreturn__;
235#else
439cb1c4 236XS(XS_version_noop);
2dfd8427 237#endif
c8d69e4a 238XS(XS_version_is_alpha);
137d6fc0 239XS(XS_version_qv);
8800c35a 240XS(XS_utf8_is_utf8);
1b026014
NIS
241XS(XS_utf8_valid);
242XS(XS_utf8_encode);
243XS(XS_utf8_decode);
244XS(XS_utf8_upgrade);
245XS(XS_utf8_downgrade);
246XS(XS_utf8_unicode_to_native);
247XS(XS_utf8_native_to_unicode);
29569577
JH
248XS(XS_Internals_SvREADONLY);
249XS(XS_Internals_SvREFCNT);
f044d0d1 250XS(XS_Internals_hv_clear_placehold);
39f7a870 251XS(XS_PerlIO_get_layers);
39cff0d9 252XS(XS_Regexp_DESTROY);
9a7034eb 253XS(XS_Internals_hash_seed);
008fb0c0 254XS(XS_Internals_rehash_seed);
05619474 255XS(XS_Internals_HvREHASH);
e1234d8e 256XS(XS_Internals_inc_sub_generation);
0cb96387
GS
257
258void
259Perl_boot_core_UNIVERSAL(pTHX)
260{
97aff369 261 dVAR;
157e3fc8 262 static const char file[] = __FILE__;
0cb96387
GS
263
264 newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file);
265 newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file);
cbc021f9 266 newXS("UNIVERSAL::DOES", XS_UNIVERSAL_DOES, file);
0cb96387 267 newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file);
439cb1c4 268 {
ad63d80f
JP
269 /* register the overloading (type 'A') magic */
270 PL_amagic_generation++;
439cb1c4 271 /* Make it findable via fetchmethod */
be2ebcad 272 newXS("version::()", XS_version_noop, file);
439cb1c4
JP
273 newXS("version::new", XS_version_new, file);
274 newXS("version::(\"\"", XS_version_stringify, file);
275 newXS("version::stringify", XS_version_stringify, file);
276 newXS("version::(0+", XS_version_numify, file);
277 newXS("version::numify", XS_version_numify, file);
9137345a 278 newXS("version::normal", XS_version_normal, file);
439cb1c4
JP
279 newXS("version::(cmp", XS_version_vcmp, file);
280 newXS("version::(<=>", XS_version_vcmp, file);
281 newXS("version::vcmp", XS_version_vcmp, file);
282 newXS("version::(bool", XS_version_boolean, file);
283 newXS("version::boolean", XS_version_boolean, file);
284 newXS("version::(nomethod", XS_version_noop, file);
285 newXS("version::noop", XS_version_noop, file);
c8d69e4a 286 newXS("version::is_alpha", XS_version_is_alpha, file);
137d6fc0 287 newXS("version::qv", XS_version_qv, file);
439cb1c4 288 }
8800c35a 289 newXS("utf8::is_utf8", XS_utf8_is_utf8, file);
1b026014
NIS
290 newXS("utf8::valid", XS_utf8_valid, file);
291 newXS("utf8::encode", XS_utf8_encode, file);
292 newXS("utf8::decode", XS_utf8_decode, file);
293 newXS("utf8::upgrade", XS_utf8_upgrade, file);
294 newXS("utf8::downgrade", XS_utf8_downgrade, file);
295 newXS("utf8::native_to_unicode", XS_utf8_native_to_unicode, file);
296 newXS("utf8::unicode_to_native", XS_utf8_unicode_to_native, file);
29569577
JH
297 newXSproto("Internals::SvREADONLY",XS_Internals_SvREADONLY, file, "\\[$%@];$");
298 newXSproto("Internals::SvREFCNT",XS_Internals_SvREFCNT, file, "\\[$%@];$");
dfd4ef2f 299 newXSproto("Internals::hv_clear_placeholders",
f044d0d1 300 XS_Internals_hv_clear_placehold, file, "\\%");
9d569fce
JH
301 newXSproto("PerlIO::get_layers",
302 XS_PerlIO_get_layers, file, "*;@");
39cff0d9 303 newXS("Regexp::DESTROY", XS_Regexp_DESTROY, file);
9a7034eb 304 newXSproto("Internals::hash_seed",XS_Internals_hash_seed, file, "");
008fb0c0 305 newXSproto("Internals::rehash_seed",XS_Internals_rehash_seed, file, "");
05619474 306 newXSproto("Internals::HvREHASH", XS_Internals_HvREHASH, file, "\\%");
e1234d8e
NC
307 newXSproto("Internals::inc_sub_generation",XS_Internals_inc_sub_generation,
308 file, "");
0cb96387
GS
309}
310
55497cff 311
6d4a7be2
PP
312XS(XS_UNIVERSAL_isa)
313{
97aff369 314 dVAR;
6d4a7be2 315 dXSARGS;
6d4a7be2
PP
316
317 if (items != 2)
cea2e8a9 318 Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)");
c4420975
AL
319 else {
320 SV * const sv = ST(0);
321 const char *name;
6d4a7be2 322
c4420975 323 SvGETMAGIC(sv);
d3f7f2b2 324
c4420975
AL
325 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
326 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
327 XSRETURN_UNDEF;
f8f70380 328
c4420975 329 name = SvPV_nolen_const(ST(1));
6d4a7be2 330
c4420975
AL
331 ST(0) = boolSV(sv_derived_from(sv, name));
332 XSRETURN(1);
333 }
6d4a7be2
PP
334}
335
6d4a7be2
PP
336XS(XS_UNIVERSAL_can)
337{
97aff369 338 dVAR;
6d4a7be2
PP
339 dXSARGS;
340 SV *sv;
6867be6d 341 const char *name;
6d4a7be2 342 SV *rv;
6f08146e 343 HV *pkg = NULL;
6d4a7be2
PP
344
345 if (items != 2)
cea2e8a9 346 Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)");
6d4a7be2
PP
347
348 sv = ST(0);
f8f70380 349
5b295bef 350 SvGETMAGIC(sv);
d3f7f2b2 351
253ecd6d
RGS
352 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
353 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
f8f70380
GS
354 XSRETURN_UNDEF;
355
0510663f 356 name = SvPV_nolen_const(ST(1));
3280af22 357 rv = &PL_sv_undef;
6d4a7be2 358
46e4b22b 359 if (SvROK(sv)) {
6f08146e 360 sv = (SV*)SvRV(sv);
46e4b22b 361 if (SvOBJECT(sv))
6f08146e
NIS
362 pkg = SvSTASH(sv);
363 }
364 else {
365 pkg = gv_stashsv(sv, FALSE);
366 }
367
368 if (pkg) {
c4420975 369 GV * const gv = gv_fetchmethod_autoload(pkg, name, FALSE);
dc848c6f
PP
370 if (gv && isGV(gv))
371 rv = sv_2mortal(newRV((SV*)GvCV(gv)));
6d4a7be2
PP
372 }
373
374 ST(0) = rv;
375 XSRETURN(1);
376}
377
cbc021f9 378XS(XS_UNIVERSAL_DOES)
379{
380 dVAR;
381 dXSARGS;
382
383 if (items != 2)
384 Perl_croak(aTHX_ "Usage: invocant->does(kind)");
385 else {
386 SV * const sv = ST(0);
387 const char *name;
388
389 name = SvPV_nolen_const(ST(1));
390 if (sv_does( sv, name ))
391 XSRETURN_YES;
392
393 XSRETURN_NO;
394 }
395}
396
6d4a7be2
PP
397XS(XS_UNIVERSAL_VERSION)
398{
97aff369 399 dVAR;
6d4a7be2
PP
400 dXSARGS;
401 HV *pkg;
402 GV **gvp;
403 GV *gv;
404 SV *sv;
e1ec3a88 405 const char *undef;
6d4a7be2 406
1571675a 407 if (SvROK(ST(0))) {
6d4a7be2 408 sv = (SV*)SvRV(ST(0));
1571675a 409 if (!SvOBJECT(sv))
cea2e8a9 410 Perl_croak(aTHX_ "Cannot find version of an unblessed reference");
6d4a7be2
PP
411 pkg = SvSTASH(sv);
412 }
413 else {
414 pkg = gv_stashsv(ST(0), FALSE);
415 }
416
4608196e 417 gvp = pkg ? (GV**)hv_fetchs(pkg, "VERSION", FALSE) : NULL;
6d4a7be2 418
0008872a 419 if (gvp && isGV(gv = *gvp) && (sv = GvSV(gv)) && SvOK(sv)) {
c4420975 420 SV * const nsv = sv_newmortal();
6d4a7be2
PP
421 sv_setsv(nsv, sv);
422 sv = nsv;
137d6fc0
JP
423 if ( !sv_derived_from(sv, "version"))
424 upg_version(sv);
c445ea15 425 undef = NULL;
6d4a7be2
PP
426 }
427 else {
3280af22 428 sv = (SV*)&PL_sv_undef;
6d4a7be2
PP
429 undef = "(undef)";
430 }
431
1571675a 432 if (items > 1) {
1571675a
GS
433 SV *req = ST(1);
434
62658f4d 435 if (undef) {
bfcb3514 436 if (pkg) {
c4420975 437 const char * const name = HvNAME_get(pkg);
a3b680e6 438 Perl_croak(aTHX_
bfcb3514
NC
439 "%s does not define $%s::VERSION--version check failed",
440 name, name);
441 } else {
a3b680e6
AL
442 Perl_croak(aTHX_
443 "%s defines neither package nor VERSION--version check failed",
0510663f 444 SvPVx_nolen_const(ST(0)) );
62658f4d
PM
445 }
446 }
ad63d80f 447
137d6fc0
JP
448 if ( !sv_derived_from(req, "version")) {
449 /* req may very well be R/O, so create a new object */
c4420975 450 SV * const nsv = sv_newmortal();
137d6fc0
JP
451 sv_setsv(nsv, req);
452 req = nsv;
453 upg_version(req);
454 }
1571675a 455
137d6fc0 456 if ( vcmp( req, sv ) > 0 )
b9381830 457 Perl_croak(aTHX_ "%s version %"SVf" (%"SVf") required--"
95b63a38
JH
458 "this is only version %"SVf" (%"SVf")", HvNAME_get(pkg),
459 (void*)vnumify(req),
460 (void*)vnormal(req),
461 (void*)vnumify(sv),
462 (void*)vnormal(sv));
2d8e6c8d 463 }
6d4a7be2 464
2b140d5b 465 if ( SvOK(sv) && sv_derived_from(sv, "version") ) {
b38a9dc5 466 ST(0) = vnumify(sv);
13f8f398
JP
467 } else {
468 ST(0) = sv;
b38a9dc5 469 }
6d4a7be2
PP
470
471 XSRETURN(1);
472}
473
439cb1c4
JP
474XS(XS_version_new)
475{
97aff369 476 dVAR;
439cb1c4 477 dXSARGS;
129318bd 478 if (items > 3)
439cb1c4
JP
479 Perl_croak(aTHX_ "Usage: version::new(class, version)");
480 SP -= items;
481 {
137d6fc0
JP
482 SV *vs = ST(1);
483 SV *rv;
c4420975
AL
484 const char * const classname =
485 sv_isobject(ST(0)) /* get the class if called as an object method */
486 ? HvNAME(SvSTASH(SvRV(ST(0))))
487 : (char *)SvPV_nolen(ST(0));
9137345a 488
92dcf8ce
JP
489 if ( items == 1 || vs == &PL_sv_undef ) { /* no param or explicit undef */
490 /* create empty object */
491 vs = sv_newmortal();
492 sv_setpvn(vs,"",0);
9137345a
JP
493 }
494 else if ( items == 3 ) {
495 vs = sv_newmortal();
cfd0369c 496 Perl_sv_setpvf(aTHX_ vs,"v%s",SvPV_nolen_const(ST(2)));
129318bd 497 }
439cb1c4 498
137d6fc0 499 rv = new_version(vs);
0723351e
NC
500 if ( strcmp(classname,"version") != 0 ) /* inherited new() */
501 sv_bless(rv, gv_stashpv(classname,TRUE));
137d6fc0
JP
502
503 PUSHs(sv_2mortal(rv));
439cb1c4
JP
504 PUTBACK;
505 return;
506 }
507}
508
509XS(XS_version_stringify)
510{
97aff369 511 dVAR;
41be1fbd
JH
512 dXSARGS;
513 if (items < 1)
514 Perl_croak(aTHX_ "Usage: version::stringify(lobj, ...)");
515 SP -= items;
516 {
7452cf6a 517 SV * lobj;
41be1fbd
JH
518
519 if (sv_derived_from(ST(0), "version")) {
9137345a 520 lobj = SvRV(ST(0));
41be1fbd
JH
521 }
522 else
523 Perl_croak(aTHX_ "lobj is not of type version");
524
137d6fc0 525 PUSHs(sv_2mortal(vstringify(lobj)));
41be1fbd
JH
526
527 PUTBACK;
528 return;
529 }
439cb1c4
JP
530}
531
532XS(XS_version_numify)
533{
97aff369 534 dVAR;
41be1fbd
JH
535 dXSARGS;
536 if (items < 1)
537 Perl_croak(aTHX_ "Usage: version::numify(lobj, ...)");
538 SP -= items;
539 {
7452cf6a 540 SV * lobj;
41be1fbd
JH
541
542 if (sv_derived_from(ST(0), "version")) {
9137345a 543 lobj = SvRV(ST(0));
41be1fbd
JH
544 }
545 else
546 Perl_croak(aTHX_ "lobj is not of type version");
547
137d6fc0 548 PUSHs(sv_2mortal(vnumify(lobj)));
41be1fbd
JH
549
550 PUTBACK;
551 return;
552 }
439cb1c4
JP
553}
554
9137345a
JP
555XS(XS_version_normal)
556{
97aff369 557 dVAR;
9137345a
JP
558 dXSARGS;
559 if (items < 1)
560 Perl_croak(aTHX_ "Usage: version::normal(lobj, ...)");
561 SP -= items;
562 {
7452cf6a 563 SV * lobj;
9137345a
JP
564
565 if (sv_derived_from(ST(0), "version")) {
566 lobj = SvRV(ST(0));
567 }
568 else
569 Perl_croak(aTHX_ "lobj is not of type version");
570
571 PUSHs(sv_2mortal(vnormal(lobj)));
572
573 PUTBACK;
574 return;
575 }
576}
577
439cb1c4
JP
578XS(XS_version_vcmp)
579{
97aff369 580 dVAR;
41be1fbd
JH
581 dXSARGS;
582 if (items < 1)
583 Perl_croak(aTHX_ "Usage: version::vcmp(lobj, ...)");
584 SP -= items;
585 {
7452cf6a 586 SV * lobj;
41be1fbd
JH
587
588 if (sv_derived_from(ST(0), "version")) {
9137345a 589 lobj = SvRV(ST(0));
41be1fbd
JH
590 }
591 else
592 Perl_croak(aTHX_ "lobj is not of type version");
593
594 {
595 SV *rs;
596 SV *rvs;
597 SV * robj = ST(1);
7452cf6a 598 const IV swap = (IV)SvIV(ST(2));
41be1fbd
JH
599
600 if ( ! sv_derived_from(robj, "version") )
601 {
602 robj = new_version(robj);
603 }
604 rvs = SvRV(robj);
605
606 if ( swap )
607 {
608 rs = newSViv(vcmp(rvs,lobj));
609 }
610 else
611 {
612 rs = newSViv(vcmp(lobj,rvs));
613 }
614
137d6fc0 615 PUSHs(sv_2mortal(rs));
41be1fbd
JH
616 }
617
618 PUTBACK;
619 return;
620 }
439cb1c4
JP
621}
622
623XS(XS_version_boolean)
624{
97aff369
JH
625 dVAR;
626 dXSARGS;
627 if (items < 1)
628 Perl_croak(aTHX_ "Usage: version::boolean(lobj, ...)");
629 SP -= items;
c4420975
AL
630 if (sv_derived_from(ST(0), "version")) {
631 SV * const lobj = SvRV(ST(0));
396482e1 632 SV * const rs = newSViv( vcmp(lobj,new_version(newSVpvs("0"))) );
c4420975
AL
633 PUSHs(sv_2mortal(rs));
634 PUTBACK;
635 return;
636 }
637 else
638 Perl_croak(aTHX_ "lobj is not of type version");
439cb1c4
JP
639}
640
641XS(XS_version_noop)
642{
97aff369 643 dVAR;
2dfd8427
AL
644 dXSARGS;
645 if (items < 1)
646 Perl_croak(aTHX_ "Usage: version::noop(lobj, ...)");
647 if (sv_derived_from(ST(0), "version"))
648 Perl_croak(aTHX_ "operation not supported with version object");
649 else
650 Perl_croak(aTHX_ "lobj is not of type version");
651#ifndef HASATTRIBUTE_NORETURN
652 XSRETURN_EMPTY;
653#endif
439cb1c4
JP
654}
655
c8d69e4a
JP
656XS(XS_version_is_alpha)
657{
97aff369 658 dVAR;
c8d69e4a
JP
659 dXSARGS;
660 if (items != 1)
661 Perl_croak(aTHX_ "Usage: version::is_alpha(lobj)");
662 SP -= items;
c4420975
AL
663 if (sv_derived_from(ST(0), "version")) {
664 SV * const lobj = ST(0);
665 if ( hv_exists((HV*)SvRV(lobj), "alpha", 5 ) )
666 XSRETURN_YES;
667 else
668 XSRETURN_NO;
c8d69e4a
JP
669 PUTBACK;
670 return;
671 }
c4420975
AL
672 else
673 Perl_croak(aTHX_ "lobj is not of type version");
c8d69e4a
JP
674}
675
137d6fc0
JP
676XS(XS_version_qv)
677{
97aff369 678 dVAR;
137d6fc0
JP
679 dXSARGS;
680 if (items != 1)
681 Perl_croak(aTHX_ "Usage: version::qv(ver)");
682 SP -= items;
683 {
684 SV * ver = ST(0);
c4420975
AL
685 if ( !SvVOK(ver) ) { /* only need to do with if not already v-string */
686 SV * const vs = sv_newmortal();
137d6fc0
JP
687 char *version;
688 if ( SvNOK(ver) ) /* may get too much accuracy */
689 {
690 char tbuf[64];
d9fad198 691 const STRLEN len = my_snprintf(tbuf, sizeof(tbuf), "%.9"NVgf, SvNVX(ver));
86c11942 692 version = savepvn(tbuf, len);
137d6fc0
JP
693 }
694 else
695 {
2e0de35c 696 version = savesvpv(ver);
137d6fc0
JP
697 }
698 (void)scan_version(version,vs,TRUE);
699 Safefree(version);
700
701 PUSHs(vs);
702 }
703 else
704 {
705 PUSHs(sv_2mortal(new_version(ver)));
706 }
707
708 PUTBACK;
709 return;
710 }
711}
712
8800c35a
JH
713XS(XS_utf8_is_utf8)
714{
97aff369 715 dVAR;
41be1fbd
JH
716 dXSARGS;
717 if (items != 1)
718 Perl_croak(aTHX_ "Usage: utf8::is_utf8(sv)");
c4420975
AL
719 else {
720 const SV * const sv = ST(0);
721 if (SvUTF8(sv))
722 XSRETURN_YES;
723 else
724 XSRETURN_NO;
41be1fbd
JH
725 }
726 XSRETURN_EMPTY;
8800c35a
JH
727}
728
1b026014
NIS
729XS(XS_utf8_valid)
730{
97aff369 731 dVAR;
41be1fbd
JH
732 dXSARGS;
733 if (items != 1)
734 Perl_croak(aTHX_ "Usage: utf8::valid(sv)");
c4420975
AL
735 else {
736 SV * const sv = ST(0);
737 STRLEN len;
738 const char * const s = SvPV_const(sv,len);
739 if (!SvUTF8(sv) || is_utf8_string((const U8*)s,len))
740 XSRETURN_YES;
741 else
742 XSRETURN_NO;
743 }
41be1fbd 744 XSRETURN_EMPTY;
1b026014
NIS
745}
746
747XS(XS_utf8_encode)
748{
97aff369 749 dVAR;
1b026014
NIS
750 dXSARGS;
751 if (items != 1)
752 Perl_croak(aTHX_ "Usage: utf8::encode(sv)");
c4420975 753 sv_utf8_encode(ST(0));
1b026014
NIS
754 XSRETURN_EMPTY;
755}
756
757XS(XS_utf8_decode)
758{
97aff369 759 dVAR;
1b026014
NIS
760 dXSARGS;
761 if (items != 1)
762 Perl_croak(aTHX_ "Usage: utf8::decode(sv)");
c4420975
AL
763 else {
764 SV * const sv = ST(0);
6867be6d 765 const bool RETVAL = sv_utf8_decode(sv);
1b026014
NIS
766 ST(0) = boolSV(RETVAL);
767 sv_2mortal(ST(0));
768 }
769 XSRETURN(1);
770}
771
772XS(XS_utf8_upgrade)
773{
97aff369 774 dVAR;
1b026014
NIS
775 dXSARGS;
776 if (items != 1)
777 Perl_croak(aTHX_ "Usage: utf8::upgrade(sv)");
c4420975
AL
778 else {
779 SV * const sv = ST(0);
1b026014
NIS
780 STRLEN RETVAL;
781 dXSTARG;
782
783 RETVAL = sv_utf8_upgrade(sv);
784 XSprePUSH; PUSHi((IV)RETVAL);
785 }
786 XSRETURN(1);
787}
788
789XS(XS_utf8_downgrade)
790{
97aff369 791 dVAR;
1b026014
NIS
792 dXSARGS;
793 if (items < 1 || items > 2)
794 Perl_croak(aTHX_ "Usage: utf8::downgrade(sv, failok=0)");
c4420975
AL
795 else {
796 SV * const sv = ST(0);
6867be6d
AL
797 const bool failok = (items < 2) ? 0 : (int)SvIV(ST(1));
798 const bool RETVAL = sv_utf8_downgrade(sv, failok);
1b026014 799
1b026014
NIS
800 ST(0) = boolSV(RETVAL);
801 sv_2mortal(ST(0));
802 }
803 XSRETURN(1);
804}
805
806XS(XS_utf8_native_to_unicode)
807{
97aff369 808 dVAR;
1b026014 809 dXSARGS;
6867be6d 810 const UV uv = SvUV(ST(0));
b7953727
JH
811
812 if (items > 1)
813 Perl_croak(aTHX_ "Usage: utf8::native_to_unicode(sv)");
814
1b026014
NIS
815 ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv)));
816 XSRETURN(1);
817}
818
819XS(XS_utf8_unicode_to_native)
820{
97aff369 821 dVAR;
1b026014 822 dXSARGS;
6867be6d 823 const UV uv = SvUV(ST(0));
b7953727
JH
824
825 if (items > 1)
826 Perl_croak(aTHX_ "Usage: utf8::unicode_to_native(sv)");
827
1b026014
NIS
828 ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv)));
829 XSRETURN(1);
830}
831
14a976d6 832XS(XS_Internals_SvREADONLY) /* This is dangerous stuff. */
29569577 833{
97aff369 834 dVAR;
29569577 835 dXSARGS;
c4420975 836 SV * const sv = SvRV(ST(0));
6867be6d 837
29569577
JH
838 if (items == 1) {
839 if (SvREADONLY(sv))
840 XSRETURN_YES;
841 else
842 XSRETURN_NO;
843 }
844 else if (items == 2) {
845 if (SvTRUE(ST(1))) {
846 SvREADONLY_on(sv);
847 XSRETURN_YES;
848 }
849 else {
14a976d6 850 /* I hope you really know what you are doing. */
29569577
JH
851 SvREADONLY_off(sv);
852 XSRETURN_NO;
853 }
854 }
14a976d6 855 XSRETURN_UNDEF; /* Can't happen. */
29569577
JH
856}
857
14a976d6 858XS(XS_Internals_SvREFCNT) /* This is dangerous stuff. */
29569577 859{
97aff369 860 dVAR;
29569577 861 dXSARGS;
c4420975 862 SV * const sv = SvRV(ST(0));
6867be6d 863
29569577 864 if (items == 1)
14a976d6 865 XSRETURN_IV(SvREFCNT(sv) - 1); /* Minus the ref created for us. */
29569577 866 else if (items == 2) {
14a976d6 867 /* I hope you really know what you are doing. */
29569577
JH
868 SvREFCNT(sv) = SvIV(ST(1));
869 XSRETURN_IV(SvREFCNT(sv));
870 }
14a976d6 871 XSRETURN_UNDEF; /* Can't happen. */
29569577
JH
872}
873
f044d0d1 874XS(XS_Internals_hv_clear_placehold)
dfd4ef2f 875{
97aff369 876 dVAR;
dfd4ef2f 877 dXSARGS;
6867be6d 878
3540d4ce
AB
879 if (items != 1)
880 Perl_croak(aTHX_ "Usage: UNIVERSAL::hv_clear_placeholders(hv)");
c4420975
AL
881 else {
882 HV * const hv = (HV *) SvRV(ST(0));
883 hv_clear_placeholders(hv);
884 XSRETURN(0);
885 }
dfd4ef2f 886}
39f7a870 887
39cff0d9
AE
888XS(XS_Regexp_DESTROY)
889{
96a5add6 890 PERL_UNUSED_CONTEXT;
53c1dcc0 891 PERL_UNUSED_ARG(cv);
39cff0d9
AE
892}
893
39f7a870
JH
894XS(XS_PerlIO_get_layers)
895{
97aff369 896 dVAR;
39f7a870
JH
897 dXSARGS;
898 if (items < 1 || items % 2 == 0)
899 Perl_croak(aTHX_ "Usage: PerlIO_get_layers(filehandle[,args])");
5fef3b4a 900#ifdef USE_PERLIO
39f7a870
JH
901 {
902 SV * sv;
903 GV * gv;
904 IO * io;
905 bool input = TRUE;
906 bool details = FALSE;
907
908 if (items > 1) {
c4420975 909 SV * const *svp;
39f7a870 910 for (svp = MARK + 2; svp <= SP; svp += 2) {
c4420975
AL
911 SV * const * const varp = svp;
912 SV * const * const valp = svp + 1;
39f7a870 913 STRLEN klen;
c4420975 914 const char * const key = SvPV_const(*varp, klen);
39f7a870
JH
915
916 switch (*key) {
917 case 'i':
918 if (klen == 5 && memEQ(key, "input", 5)) {
919 input = SvTRUE(*valp);
920 break;
921 }
922 goto fail;
923 case 'o':
924 if (klen == 6 && memEQ(key, "output", 6)) {
925 input = !SvTRUE(*valp);
926 break;
927 }
928 goto fail;
929 case 'd':
930 if (klen == 7 && memEQ(key, "details", 7)) {
931 details = SvTRUE(*valp);
932 break;
933 }
934 goto fail;
935 default:
936 fail:
937 Perl_croak(aTHX_
938 "get_layers: unknown argument '%s'",
939 key);
940 }
941 }
942
943 SP -= (items - 1);
944 }
945
946 sv = POPs;
947 gv = (GV*)sv;
948
949 if (!isGV(sv)) {
950 if (SvROK(sv) && isGV(SvRV(sv)))
951 gv = (GV*)SvRV(sv);
671d49be 952 else if (SvPOKp(sv))
f776e3cd 953 gv = gv_fetchsv(sv, 0, SVt_PVIO);
39f7a870
JH
954 }
955
956 if (gv && (io = GvIO(gv))) {
957 dTARGET;
c4420975 958 AV* const av = PerlIO_get_layers(aTHX_ input ?
39f7a870
JH
959 IoIFP(io) : IoOFP(io));
960 I32 i;
c4420975 961 const I32 last = av_len(av);
39f7a870
JH
962 I32 nitem = 0;
963
964 for (i = last; i >= 0; i -= 3) {
c4420975
AL
965 SV * const * const namsvp = av_fetch(av, i - 2, FALSE);
966 SV * const * const argsvp = av_fetch(av, i - 1, FALSE);
967 SV * const * const flgsvp = av_fetch(av, i, FALSE);
39f7a870 968
c4420975
AL
969 const bool namok = namsvp && *namsvp && SvPOK(*namsvp);
970 const bool argok = argsvp && *argsvp && SvPOK(*argsvp);
971 const bool flgok = flgsvp && *flgsvp && SvIOK(*flgsvp);
39f7a870
JH
972
973 if (details) {
ec3bab8e
NC
974 XPUSHs(namok
975 ? newSVpvn(SvPVX_const(*namsvp), SvCUR(*namsvp))
976 : &PL_sv_undef);
977 XPUSHs(argok
978 ? newSVpvn(SvPVX_const(*argsvp), SvCUR(*argsvp))
979 : &PL_sv_undef);
39f7a870
JH
980 if (flgok)
981 XPUSHi(SvIVX(*flgsvp));
982 else
983 XPUSHs(&PL_sv_undef);
984 nitem += 3;
985 }
986 else {
987 if (namok && argok)
988 XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf"(%"SVf")",
95b63a38
JH
989 (void*)*namsvp,
990 (void*)*argsvp));
39f7a870 991 else if (namok)
95b63a38
JH
992 XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf,
993 (void*)*namsvp));
39f7a870
JH
994 else
995 XPUSHs(&PL_sv_undef);
996 nitem++;
997 if (flgok) {
c4420975 998 const IV flags = SvIVX(*flgsvp);
39f7a870
JH
999
1000 if (flags & PERLIO_F_UTF8) {
396482e1 1001 XPUSHs(newSVpvs("utf8"));
39f7a870
JH
1002 nitem++;
1003 }
1004 }
1005 }
1006 }
1007
1008 SvREFCNT_dec(av);
1009
1010 XSRETURN(nitem);
1011 }
1012 }
5fef3b4a 1013#endif
39f7a870
JH
1014
1015 XSRETURN(0);
1016}
1017
9a7034eb 1018XS(XS_Internals_hash_seed)
c910b28a 1019{
97aff369 1020 dVAR;
c85d3f85
NC
1021 /* Using dXSARGS would also have dITEM and dSP,
1022 * which define 2 unused local variables. */
557b887a 1023 dAXMARK;
53c1dcc0 1024 PERL_UNUSED_ARG(cv);
ad73156c 1025 PERL_UNUSED_VAR(mark);
81eaca17 1026 XSRETURN_UV(PERL_HASH_SEED);
c910b28a
JH
1027}
1028
008fb0c0 1029XS(XS_Internals_rehash_seed)
8e90d776 1030{
97aff369 1031 dVAR;
8e90d776
NC
1032 /* Using dXSARGS would also have dITEM and dSP,
1033 * which define 2 unused local variables. */
557b887a 1034 dAXMARK;
53c1dcc0 1035 PERL_UNUSED_ARG(cv);
ad73156c 1036 PERL_UNUSED_VAR(mark);
008fb0c0 1037 XSRETURN_UV(PL_rehash_seed);
8e90d776
NC
1038}
1039
05619474
NC
1040XS(XS_Internals_HvREHASH) /* Subject to change */
1041{
97aff369 1042 dVAR;
05619474
NC
1043 dXSARGS;
1044 if (SvROK(ST(0))) {
c4420975 1045 const HV * const hv = (HV *) SvRV(ST(0));
05619474
NC
1046 if (items == 1 && SvTYPE(hv) == SVt_PVHV) {
1047 if (HvREHASH(hv))
1048 XSRETURN_YES;
1049 else
1050 XSRETURN_NO;
1051 }
1052 }
1053 Perl_croak(aTHX_ "Internals::HvREHASH $hashref");
1054}
241d1a3b 1055
e1234d8e
NC
1056XS(XS_Internals_inc_sub_generation)
1057{
97aff369 1058 dVAR;
e1234d8e
NC
1059 /* Using dXSARGS would also have dITEM and dSP,
1060 * which define 2 unused local variables. */
1061 dAXMARK;
1062 PERL_UNUSED_ARG(cv);
1063 PERL_UNUSED_VAR(mark);
1064 ++PL_sub_generation;
1065 XSRETURN_EMPTY;
1066}
1067
241d1a3b
NC
1068/*
1069 * Local variables:
1070 * c-indentation-style: bsd
1071 * c-basic-offset: 4
1072 * indent-tabs-mode: t
1073 * End:
1074 *
37442d52
RGS
1075 * ex: set ts=8 sts=4 sw=4 noet:
1076 */