This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make use VERSION also load feature.pm
[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().
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
9e0d86f8 65 if (gvp && (gv = *gvp) && isGV_with_GP(gv) && (subgen = GvSV(gv))
46e4b22b
GS
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
9e0d86f8 90 if (gvp && (gv = *gvp) && isGV_with_GP(gv) && (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 111 SV* const sv = *svp++;
da51bb9b 112 HV* const basestash = gv_stashsv(sv, 0);
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",
be2597df 117 SVfARG(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 {
da51bb9b 160 stash = gv_stashsv(sv, 0);
55497cff 161 }
46e4b22b 162
0b6f4f5c 163 if (stash) {
da51bb9b 164 HV * const name_stash = gv_stashpv(name, 0);
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 {
da51bb9b 365 pkg = gv_stashsv(sv, 0);
6f08146e
NIS
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 {
da51bb9b 414 pkg = gv_stashsv(ST(0), 0);
6d4a7be2
PP
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 458 "this is only version %"SVf" (%"SVf")", HvNAME_get(pkg),
be2597df
MHM
459 SVfARG(vnumify(req)),
460 SVfARG(vnormal(req)),
461 SVfARG(vnumify(sv)),
462 SVfARG(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 500 if ( strcmp(classname,"version") != 0 ) /* inherited new() */
da51bb9b 501 sv_bless(rv, gv_stashpv(classname, GV_ADD));
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];
b5b5a8f0
RGS
691#ifdef USE_LOCALE_NUMERIC
692 char *loc = setlocale(LC_NUMERIC, "C");
693#endif
63e3af20 694 STRLEN len = my_snprintf(tbuf, sizeof(tbuf), "%.9"NVgf, SvNVX(ver));
b5b5a8f0
RGS
695#ifdef USE_LOCALE_NUMERIC
696 setlocale(LC_NUMERIC, loc);
697#endif
63e3af20 698 while (tbuf[len-1] == '0' && len > 0) len--;
86c11942 699 version = savepvn(tbuf, len);
137d6fc0
JP
700 }
701 else
702 {
2e0de35c 703 version = savesvpv(ver);
137d6fc0
JP
704 }
705 (void)scan_version(version,vs,TRUE);
706 Safefree(version);
707
708 PUSHs(vs);
709 }
710 else
711 {
712 PUSHs(sv_2mortal(new_version(ver)));
713 }
714
715 PUTBACK;
716 return;
717 }
718}
719
8800c35a
JH
720XS(XS_utf8_is_utf8)
721{
97aff369 722 dVAR;
41be1fbd
JH
723 dXSARGS;
724 if (items != 1)
725 Perl_croak(aTHX_ "Usage: utf8::is_utf8(sv)");
c4420975
AL
726 else {
727 const SV * const sv = ST(0);
728 if (SvUTF8(sv))
729 XSRETURN_YES;
730 else
731 XSRETURN_NO;
41be1fbd
JH
732 }
733 XSRETURN_EMPTY;
8800c35a
JH
734}
735
1b026014
NIS
736XS(XS_utf8_valid)
737{
97aff369 738 dVAR;
41be1fbd
JH
739 dXSARGS;
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
NIS
757 dXSARGS;
758 if (items != 1)
759 Perl_croak(aTHX_ "Usage: utf8::encode(sv)");
c4420975 760 sv_utf8_encode(ST(0));
1b026014
NIS
761 XSRETURN_EMPTY;
762}
763
764XS(XS_utf8_decode)
765{
97aff369 766 dVAR;
1b026014
NIS
767 dXSARGS;
768 if (items != 1)
769 Perl_croak(aTHX_ "Usage: utf8::decode(sv)");
c4420975
AL
770 else {
771 SV * const sv = ST(0);
6867be6d 772 const bool RETVAL = sv_utf8_decode(sv);
1b026014
NIS
773 ST(0) = boolSV(RETVAL);
774 sv_2mortal(ST(0));
775 }
776 XSRETURN(1);
777}
778
779XS(XS_utf8_upgrade)
780{
97aff369 781 dVAR;
1b026014
NIS
782 dXSARGS;
783 if (items != 1)
784 Perl_croak(aTHX_ "Usage: utf8::upgrade(sv)");
c4420975
AL
785 else {
786 SV * const sv = ST(0);
1b026014
NIS
787 STRLEN RETVAL;
788 dXSTARG;
789
790 RETVAL = sv_utf8_upgrade(sv);
791 XSprePUSH; PUSHi((IV)RETVAL);
792 }
793 XSRETURN(1);
794}
795
796XS(XS_utf8_downgrade)
797{
97aff369 798 dVAR;
1b026014
NIS
799 dXSARGS;
800 if (items < 1 || items > 2)
801 Perl_croak(aTHX_ "Usage: utf8::downgrade(sv, failok=0)");
c4420975
AL
802 else {
803 SV * const sv = ST(0);
6867be6d
AL
804 const bool failok = (items < 2) ? 0 : (int)SvIV(ST(1));
805 const bool RETVAL = sv_utf8_downgrade(sv, failok);
1b026014 806
1b026014
NIS
807 ST(0) = boolSV(RETVAL);
808 sv_2mortal(ST(0));
809 }
810 XSRETURN(1);
811}
812
813XS(XS_utf8_native_to_unicode)
814{
97aff369 815 dVAR;
1b026014 816 dXSARGS;
6867be6d 817 const UV uv = SvUV(ST(0));
b7953727
JH
818
819 if (items > 1)
820 Perl_croak(aTHX_ "Usage: utf8::native_to_unicode(sv)");
821
1b026014
NIS
822 ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv)));
823 XSRETURN(1);
824}
825
826XS(XS_utf8_unicode_to_native)
827{
97aff369 828 dVAR;
1b026014 829 dXSARGS;
6867be6d 830 const UV uv = SvUV(ST(0));
b7953727
JH
831
832 if (items > 1)
833 Perl_croak(aTHX_ "Usage: utf8::unicode_to_native(sv)");
834
1b026014
NIS
835 ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv)));
836 XSRETURN(1);
837}
838
14a976d6 839XS(XS_Internals_SvREADONLY) /* This is dangerous stuff. */
29569577 840{
97aff369 841 dVAR;
29569577 842 dXSARGS;
c4420975 843 SV * const sv = SvRV(ST(0));
6867be6d 844
29569577
JH
845 if (items == 1) {
846 if (SvREADONLY(sv))
847 XSRETURN_YES;
848 else
849 XSRETURN_NO;
850 }
851 else if (items == 2) {
852 if (SvTRUE(ST(1))) {
853 SvREADONLY_on(sv);
854 XSRETURN_YES;
855 }
856 else {
14a976d6 857 /* I hope you really know what you are doing. */
29569577
JH
858 SvREADONLY_off(sv);
859 XSRETURN_NO;
860 }
861 }
14a976d6 862 XSRETURN_UNDEF; /* Can't happen. */
29569577
JH
863}
864
14a976d6 865XS(XS_Internals_SvREFCNT) /* This is dangerous stuff. */
29569577 866{
97aff369 867 dVAR;
29569577 868 dXSARGS;
c4420975 869 SV * const sv = SvRV(ST(0));
6867be6d 870
29569577 871 if (items == 1)
14a976d6 872 XSRETURN_IV(SvREFCNT(sv) - 1); /* Minus the ref created for us. */
29569577 873 else if (items == 2) {
14a976d6 874 /* I hope you really know what you are doing. */
29569577
JH
875 SvREFCNT(sv) = SvIV(ST(1));
876 XSRETURN_IV(SvREFCNT(sv));
877 }
14a976d6 878 XSRETURN_UNDEF; /* Can't happen. */
29569577
JH
879}
880
f044d0d1 881XS(XS_Internals_hv_clear_placehold)
dfd4ef2f 882{
97aff369 883 dVAR;
dfd4ef2f 884 dXSARGS;
6867be6d 885
3540d4ce
AB
886 if (items != 1)
887 Perl_croak(aTHX_ "Usage: UNIVERSAL::hv_clear_placeholders(hv)");
c4420975
AL
888 else {
889 HV * const hv = (HV *) SvRV(ST(0));
890 hv_clear_placeholders(hv);
891 XSRETURN(0);
892 }
dfd4ef2f 893}
39f7a870 894
39cff0d9
AE
895XS(XS_Regexp_DESTROY)
896{
96a5add6 897 PERL_UNUSED_CONTEXT;
53c1dcc0 898 PERL_UNUSED_ARG(cv);
39cff0d9
AE
899}
900
39f7a870
JH
901XS(XS_PerlIO_get_layers)
902{
97aff369 903 dVAR;
39f7a870
JH
904 dXSARGS;
905 if (items < 1 || items % 2 == 0)
906 Perl_croak(aTHX_ "Usage: PerlIO_get_layers(filehandle[,args])");
5fef3b4a 907#ifdef USE_PERLIO
39f7a870
JH
908 {
909 SV * sv;
910 GV * gv;
911 IO * io;
912 bool input = TRUE;
913 bool details = FALSE;
914
915 if (items > 1) {
c4420975 916 SV * const *svp;
39f7a870 917 for (svp = MARK + 2; svp <= SP; svp += 2) {
c4420975
AL
918 SV * const * const varp = svp;
919 SV * const * const valp = svp + 1;
39f7a870 920 STRLEN klen;
c4420975 921 const char * const key = SvPV_const(*varp, klen);
39f7a870
JH
922
923 switch (*key) {
924 case 'i':
925 if (klen == 5 && memEQ(key, "input", 5)) {
926 input = SvTRUE(*valp);
927 break;
928 }
929 goto fail;
930 case 'o':
931 if (klen == 6 && memEQ(key, "output", 6)) {
932 input = !SvTRUE(*valp);
933 break;
934 }
935 goto fail;
936 case 'd':
937 if (klen == 7 && memEQ(key, "details", 7)) {
938 details = SvTRUE(*valp);
939 break;
940 }
941 goto fail;
942 default:
943 fail:
944 Perl_croak(aTHX_
945 "get_layers: unknown argument '%s'",
946 key);
947 }
948 }
949
950 SP -= (items - 1);
951 }
952
953 sv = POPs;
954 gv = (GV*)sv;
955
956 if (!isGV(sv)) {
957 if (SvROK(sv) && isGV(SvRV(sv)))
958 gv = (GV*)SvRV(sv);
671d49be 959 else if (SvPOKp(sv))
f776e3cd 960 gv = gv_fetchsv(sv, 0, SVt_PVIO);
39f7a870
JH
961 }
962
963 if (gv && (io = GvIO(gv))) {
964 dTARGET;
c4420975 965 AV* const av = PerlIO_get_layers(aTHX_ input ?
39f7a870
JH
966 IoIFP(io) : IoOFP(io));
967 I32 i;
c4420975 968 const I32 last = av_len(av);
39f7a870
JH
969 I32 nitem = 0;
970
971 for (i = last; i >= 0; i -= 3) {
c4420975
AL
972 SV * const * const namsvp = av_fetch(av, i - 2, FALSE);
973 SV * const * const argsvp = av_fetch(av, i - 1, FALSE);
974 SV * const * const flgsvp = av_fetch(av, i, FALSE);
39f7a870 975
c4420975
AL
976 const bool namok = namsvp && *namsvp && SvPOK(*namsvp);
977 const bool argok = argsvp && *argsvp && SvPOK(*argsvp);
978 const bool flgok = flgsvp && *flgsvp && SvIOK(*flgsvp);
39f7a870
JH
979
980 if (details) {
ec3bab8e
NC
981 XPUSHs(namok
982 ? newSVpvn(SvPVX_const(*namsvp), SvCUR(*namsvp))
983 : &PL_sv_undef);
984 XPUSHs(argok
985 ? newSVpvn(SvPVX_const(*argsvp), SvCUR(*argsvp))
986 : &PL_sv_undef);
39f7a870
JH
987 if (flgok)
988 XPUSHi(SvIVX(*flgsvp));
989 else
990 XPUSHs(&PL_sv_undef);
991 nitem += 3;
992 }
993 else {
994 if (namok && argok)
995 XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf"(%"SVf")",
be2597df
MHM
996 SVfARG(*namsvp),
997 SVfARG(*argsvp)));
39f7a870 998 else if (namok)
95b63a38 999 XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf,
be2597df 1000 SVfARG(*namsvp)));
39f7a870
JH
1001 else
1002 XPUSHs(&PL_sv_undef);
1003 nitem++;
1004 if (flgok) {
c4420975 1005 const IV flags = SvIVX(*flgsvp);
39f7a870
JH
1006
1007 if (flags & PERLIO_F_UTF8) {
396482e1 1008 XPUSHs(newSVpvs("utf8"));
39f7a870
JH
1009 nitem++;
1010 }
1011 }
1012 }
1013 }
1014
1015 SvREFCNT_dec(av);
1016
1017 XSRETURN(nitem);
1018 }
1019 }
5fef3b4a 1020#endif
39f7a870
JH
1021
1022 XSRETURN(0);
1023}
1024
9a7034eb 1025XS(XS_Internals_hash_seed)
c910b28a 1026{
97aff369 1027 dVAR;
c85d3f85
NC
1028 /* Using dXSARGS would also have dITEM and dSP,
1029 * which define 2 unused local variables. */
557b887a 1030 dAXMARK;
53c1dcc0 1031 PERL_UNUSED_ARG(cv);
ad73156c 1032 PERL_UNUSED_VAR(mark);
81eaca17 1033 XSRETURN_UV(PERL_HASH_SEED);
c910b28a
JH
1034}
1035
008fb0c0 1036XS(XS_Internals_rehash_seed)
8e90d776 1037{
97aff369 1038 dVAR;
8e90d776
NC
1039 /* Using dXSARGS would also have dITEM and dSP,
1040 * which define 2 unused local variables. */
557b887a 1041 dAXMARK;
53c1dcc0 1042 PERL_UNUSED_ARG(cv);
ad73156c 1043 PERL_UNUSED_VAR(mark);
008fb0c0 1044 XSRETURN_UV(PL_rehash_seed);
8e90d776
NC
1045}
1046
05619474
NC
1047XS(XS_Internals_HvREHASH) /* Subject to change */
1048{
97aff369 1049 dVAR;
05619474
NC
1050 dXSARGS;
1051 if (SvROK(ST(0))) {
c4420975 1052 const HV * const hv = (HV *) SvRV(ST(0));
05619474
NC
1053 if (items == 1 && SvTYPE(hv) == SVt_PVHV) {
1054 if (HvREHASH(hv))
1055 XSRETURN_YES;
1056 else
1057 XSRETURN_NO;
1058 }
1059 }
1060 Perl_croak(aTHX_ "Internals::HvREHASH $hashref");
1061}
241d1a3b 1062
e1234d8e
NC
1063XS(XS_Internals_inc_sub_generation)
1064{
97aff369 1065 dVAR;
e1234d8e
NC
1066 /* Using dXSARGS would also have dITEM and dSP,
1067 * which define 2 unused local variables. */
1068 dAXMARK;
1069 PERL_UNUSED_ARG(cv);
1070 PERL_UNUSED_VAR(mark);
1071 ++PL_sub_generation;
1072 XSRETURN_EMPTY;
1073}
1074
241d1a3b
NC
1075/*
1076 * Local variables:
1077 * c-indentation-style: bsd
1078 * c-basic-offset: 4
1079 * indent-tabs-mode: t
1080 * End:
1081 *
37442d52
RGS
1082 * ex: set ts=8 sts=4 sw=4 noet:
1083 */