This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
RE: $Win32::VERSION problem
[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;
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)
387 Perl_croak(aTHX_ "Usage: invocant->does(kind)");
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
JP
427 if ( !sv_derived_from(sv, "version"))
428 upg_version(sv);
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 */
c4420975 454 SV * const nsv = sv_newmortal();
137d6fc0
JP
455 sv_setsv(nsv, req);
456 req = nsv;
457 upg_version(req);
458 }
1571675a 459
137d6fc0 460 if ( vcmp( req, sv ) > 0 )
b9381830 461 Perl_croak(aTHX_ "%s version %"SVf" (%"SVf") required--"
95b63a38 462 "this is only version %"SVf" (%"SVf")", HvNAME_get(pkg),
be2597df
MHM
463 SVfARG(vnumify(req)),
464 SVfARG(vnormal(req)),
465 SVfARG(vnumify(sv)),
466 SVfARG(vnormal(sv)));
2d8e6c8d 467 }
6d4a7be2 468
2b140d5b 469 if ( SvOK(sv) && sv_derived_from(sv, "version") ) {
b38a9dc5 470 ST(0) = vnumify(sv);
13f8f398
JP
471 } else {
472 ST(0) = sv;
b38a9dc5 473 }
6d4a7be2
PP
474
475 XSRETURN(1);
476}
477
439cb1c4
JP
478XS(XS_version_new)
479{
97aff369 480 dVAR;
439cb1c4 481 dXSARGS;
58c0efa5 482 PERL_UNUSED_ARG(cv);
129318bd 483 if (items > 3)
439cb1c4
JP
484 Perl_croak(aTHX_ "Usage: version::new(class, version)");
485 SP -= items;
486 {
137d6fc0
JP
487 SV *vs = ST(1);
488 SV *rv;
c4420975
AL
489 const char * const classname =
490 sv_isobject(ST(0)) /* get the class if called as an object method */
491 ? HvNAME(SvSTASH(SvRV(ST(0))))
492 : (char *)SvPV_nolen(ST(0));
9137345a 493
92dcf8ce
JP
494 if ( items == 1 || vs == &PL_sv_undef ) { /* no param or explicit undef */
495 /* create empty object */
496 vs = sv_newmortal();
497 sv_setpvn(vs,"",0);
9137345a
JP
498 }
499 else if ( items == 3 ) {
500 vs = sv_newmortal();
cfd0369c 501 Perl_sv_setpvf(aTHX_ vs,"v%s",SvPV_nolen_const(ST(2)));
129318bd 502 }
439cb1c4 503
137d6fc0 504 rv = new_version(vs);
0723351e 505 if ( strcmp(classname,"version") != 0 ) /* inherited new() */
da51bb9b 506 sv_bless(rv, gv_stashpv(classname, GV_ADD));
137d6fc0
JP
507
508 PUSHs(sv_2mortal(rv));
439cb1c4
JP
509 PUTBACK;
510 return;
511 }
512}
513
514XS(XS_version_stringify)
515{
97aff369 516 dVAR;
41be1fbd 517 dXSARGS;
58c0efa5 518 PERL_UNUSED_ARG(cv);
41be1fbd
JH
519 if (items < 1)
520 Perl_croak(aTHX_ "Usage: version::stringify(lobj, ...)");
521 SP -= items;
522 {
7452cf6a 523 SV * lobj;
41be1fbd
JH
524
525 if (sv_derived_from(ST(0), "version")) {
9137345a 526 lobj = SvRV(ST(0));
41be1fbd
JH
527 }
528 else
529 Perl_croak(aTHX_ "lobj is not of type version");
530
137d6fc0 531 PUSHs(sv_2mortal(vstringify(lobj)));
41be1fbd
JH
532
533 PUTBACK;
534 return;
535 }
439cb1c4
JP
536}
537
538XS(XS_version_numify)
539{
97aff369 540 dVAR;
41be1fbd 541 dXSARGS;
58c0efa5 542 PERL_UNUSED_ARG(cv);
41be1fbd
JH
543 if (items < 1)
544 Perl_croak(aTHX_ "Usage: version::numify(lobj, ...)");
545 SP -= items;
546 {
7452cf6a 547 SV * lobj;
41be1fbd
JH
548
549 if (sv_derived_from(ST(0), "version")) {
9137345a 550 lobj = SvRV(ST(0));
41be1fbd
JH
551 }
552 else
553 Perl_croak(aTHX_ "lobj is not of type version");
554
137d6fc0 555 PUSHs(sv_2mortal(vnumify(lobj)));
41be1fbd
JH
556
557 PUTBACK;
558 return;
559 }
439cb1c4
JP
560}
561
9137345a
JP
562XS(XS_version_normal)
563{
97aff369 564 dVAR;
9137345a 565 dXSARGS;
58c0efa5 566 PERL_UNUSED_ARG(cv);
9137345a
JP
567 if (items < 1)
568 Perl_croak(aTHX_ "Usage: version::normal(lobj, ...)");
569 SP -= items;
570 {
7452cf6a 571 SV * lobj;
9137345a
JP
572
573 if (sv_derived_from(ST(0), "version")) {
574 lobj = SvRV(ST(0));
575 }
576 else
577 Perl_croak(aTHX_ "lobj is not of type version");
578
579 PUSHs(sv_2mortal(vnormal(lobj)));
580
581 PUTBACK;
582 return;
583 }
584}
585
439cb1c4
JP
586XS(XS_version_vcmp)
587{
97aff369 588 dVAR;
41be1fbd 589 dXSARGS;
58c0efa5 590 PERL_UNUSED_ARG(cv);
41be1fbd
JH
591 if (items < 1)
592 Perl_croak(aTHX_ "Usage: version::vcmp(lobj, ...)");
593 SP -= items;
594 {
7452cf6a 595 SV * lobj;
41be1fbd
JH
596
597 if (sv_derived_from(ST(0), "version")) {
9137345a 598 lobj = SvRV(ST(0));
41be1fbd
JH
599 }
600 else
601 Perl_croak(aTHX_ "lobj is not of type version");
602
603 {
604 SV *rs;
605 SV *rvs;
606 SV * robj = ST(1);
7452cf6a 607 const IV swap = (IV)SvIV(ST(2));
41be1fbd
JH
608
609 if ( ! sv_derived_from(robj, "version") )
610 {
611 robj = new_version(robj);
612 }
613 rvs = SvRV(robj);
614
615 if ( swap )
616 {
617 rs = newSViv(vcmp(rvs,lobj));
618 }
619 else
620 {
621 rs = newSViv(vcmp(lobj,rvs));
622 }
623
137d6fc0 624 PUSHs(sv_2mortal(rs));
41be1fbd
JH
625 }
626
627 PUTBACK;
628 return;
629 }
439cb1c4
JP
630}
631
632XS(XS_version_boolean)
633{
97aff369
JH
634 dVAR;
635 dXSARGS;
58c0efa5 636 PERL_UNUSED_ARG(cv);
97aff369
JH
637 if (items < 1)
638 Perl_croak(aTHX_ "Usage: version::boolean(lobj, ...)");
639 SP -= items;
c4420975
AL
640 if (sv_derived_from(ST(0), "version")) {
641 SV * const lobj = SvRV(ST(0));
396482e1 642 SV * const rs = newSViv( vcmp(lobj,new_version(newSVpvs("0"))) );
c4420975
AL
643 PUSHs(sv_2mortal(rs));
644 PUTBACK;
645 return;
646 }
647 else
648 Perl_croak(aTHX_ "lobj is not of type version");
439cb1c4
JP
649}
650
651XS(XS_version_noop)
652{
97aff369 653 dVAR;
2dfd8427 654 dXSARGS;
58c0efa5 655 PERL_UNUSED_ARG(cv);
2dfd8427
AL
656 if (items < 1)
657 Perl_croak(aTHX_ "Usage: version::noop(lobj, ...)");
658 if (sv_derived_from(ST(0), "version"))
659 Perl_croak(aTHX_ "operation not supported with version object");
660 else
661 Perl_croak(aTHX_ "lobj is not of type version");
662#ifndef HASATTRIBUTE_NORETURN
663 XSRETURN_EMPTY;
664#endif
439cb1c4
JP
665}
666
c8d69e4a
JP
667XS(XS_version_is_alpha)
668{
97aff369 669 dVAR;
c8d69e4a 670 dXSARGS;
58c0efa5 671 PERL_UNUSED_ARG(cv);
c8d69e4a
JP
672 if (items != 1)
673 Perl_croak(aTHX_ "Usage: version::is_alpha(lobj)");
674 SP -= items;
c4420975
AL
675 if (sv_derived_from(ST(0), "version")) {
676 SV * const lobj = ST(0);
677 if ( hv_exists((HV*)SvRV(lobj), "alpha", 5 ) )
678 XSRETURN_YES;
679 else
680 XSRETURN_NO;
c8d69e4a
JP
681 PUTBACK;
682 return;
683 }
c4420975
AL
684 else
685 Perl_croak(aTHX_ "lobj is not of type version");
c8d69e4a
JP
686}
687
137d6fc0
JP
688XS(XS_version_qv)
689{
97aff369 690 dVAR;
137d6fc0 691 dXSARGS;
58c0efa5 692 PERL_UNUSED_ARG(cv);
137d6fc0
JP
693 if (items != 1)
694 Perl_croak(aTHX_ "Usage: version::qv(ver)");
695 SP -= items;
696 {
697 SV * ver = ST(0);
c4420975
AL
698 if ( !SvVOK(ver) ) { /* only need to do with if not already v-string */
699 SV * const vs = sv_newmortal();
137d6fc0
JP
700 char *version;
701 if ( SvNOK(ver) ) /* may get too much accuracy */
702 {
703 char tbuf[64];
b5b5a8f0
RGS
704#ifdef USE_LOCALE_NUMERIC
705 char *loc = setlocale(LC_NUMERIC, "C");
706#endif
63e3af20 707 STRLEN len = my_snprintf(tbuf, sizeof(tbuf), "%.9"NVgf, SvNVX(ver));
b5b5a8f0
RGS
708#ifdef USE_LOCALE_NUMERIC
709 setlocale(LC_NUMERIC, loc);
710#endif
63e3af20 711 while (tbuf[len-1] == '0' && len > 0) len--;
86c11942 712 version = savepvn(tbuf, len);
137d6fc0
JP
713 }
714 else
715 {
2e0de35c 716 version = savesvpv(ver);
137d6fc0
JP
717 }
718 (void)scan_version(version,vs,TRUE);
719 Safefree(version);
720
721 PUSHs(vs);
722 }
723 else
724 {
725 PUSHs(sv_2mortal(new_version(ver)));
726 }
727
728 PUTBACK;
729 return;
730 }
731}
732
8800c35a
JH
733XS(XS_utf8_is_utf8)
734{
97aff369 735 dVAR;
41be1fbd 736 dXSARGS;
58c0efa5 737 PERL_UNUSED_ARG(cv);
41be1fbd
JH
738 if (items != 1)
739 Perl_croak(aTHX_ "Usage: utf8::is_utf8(sv)");
c4420975
AL
740 else {
741 const SV * const sv = ST(0);
742 if (SvUTF8(sv))
743 XSRETURN_YES;
744 else
745 XSRETURN_NO;
41be1fbd
JH
746 }
747 XSRETURN_EMPTY;
8800c35a
JH
748}
749
1b026014
NIS
750XS(XS_utf8_valid)
751{
97aff369 752 dVAR;
41be1fbd 753 dXSARGS;
58c0efa5 754 PERL_UNUSED_ARG(cv);
41be1fbd
JH
755 if (items != 1)
756 Perl_croak(aTHX_ "Usage: utf8::valid(sv)");
c4420975
AL
757 else {
758 SV * const sv = ST(0);
759 STRLEN len;
760 const char * const s = SvPV_const(sv,len);
761 if (!SvUTF8(sv) || is_utf8_string((const U8*)s,len))
762 XSRETURN_YES;
763 else
764 XSRETURN_NO;
765 }
41be1fbd 766 XSRETURN_EMPTY;
1b026014
NIS
767}
768
769XS(XS_utf8_encode)
770{
97aff369 771 dVAR;
1b026014 772 dXSARGS;
58c0efa5 773 PERL_UNUSED_ARG(cv);
1b026014
NIS
774 if (items != 1)
775 Perl_croak(aTHX_ "Usage: utf8::encode(sv)");
c4420975 776 sv_utf8_encode(ST(0));
1b026014
NIS
777 XSRETURN_EMPTY;
778}
779
780XS(XS_utf8_decode)
781{
97aff369 782 dVAR;
1b026014 783 dXSARGS;
58c0efa5 784 PERL_UNUSED_ARG(cv);
1b026014
NIS
785 if (items != 1)
786 Perl_croak(aTHX_ "Usage: utf8::decode(sv)");
c4420975
AL
787 else {
788 SV * const sv = ST(0);
6867be6d 789 const bool RETVAL = sv_utf8_decode(sv);
1b026014
NIS
790 ST(0) = boolSV(RETVAL);
791 sv_2mortal(ST(0));
792 }
793 XSRETURN(1);
794}
795
796XS(XS_utf8_upgrade)
797{
97aff369 798 dVAR;
1b026014 799 dXSARGS;
58c0efa5 800 PERL_UNUSED_ARG(cv);
1b026014
NIS
801 if (items != 1)
802 Perl_croak(aTHX_ "Usage: utf8::upgrade(sv)");
c4420975
AL
803 else {
804 SV * const sv = ST(0);
1b026014
NIS
805 STRLEN RETVAL;
806 dXSTARG;
807
808 RETVAL = sv_utf8_upgrade(sv);
809 XSprePUSH; PUSHi((IV)RETVAL);
810 }
811 XSRETURN(1);
812}
813
814XS(XS_utf8_downgrade)
815{
97aff369 816 dVAR;
1b026014 817 dXSARGS;
58c0efa5 818 PERL_UNUSED_ARG(cv);
1b026014
NIS
819 if (items < 1 || items > 2)
820 Perl_croak(aTHX_ "Usage: utf8::downgrade(sv, failok=0)");
c4420975
AL
821 else {
822 SV * const sv = ST(0);
6867be6d
AL
823 const bool failok = (items < 2) ? 0 : (int)SvIV(ST(1));
824 const bool RETVAL = sv_utf8_downgrade(sv, failok);
1b026014 825
1b026014
NIS
826 ST(0) = boolSV(RETVAL);
827 sv_2mortal(ST(0));
828 }
829 XSRETURN(1);
830}
831
832XS(XS_utf8_native_to_unicode)
833{
97aff369 834 dVAR;
1b026014 835 dXSARGS;
6867be6d 836 const UV uv = SvUV(ST(0));
58c0efa5 837 PERL_UNUSED_ARG(cv);
b7953727
JH
838
839 if (items > 1)
840 Perl_croak(aTHX_ "Usage: utf8::native_to_unicode(sv)");
841
1b026014
NIS
842 ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv)));
843 XSRETURN(1);
844}
845
846XS(XS_utf8_unicode_to_native)
847{
97aff369 848 dVAR;
1b026014 849 dXSARGS;
6867be6d 850 const UV uv = SvUV(ST(0));
58c0efa5 851 PERL_UNUSED_ARG(cv);
b7953727
JH
852
853 if (items > 1)
854 Perl_croak(aTHX_ "Usage: utf8::unicode_to_native(sv)");
855
1b026014
NIS
856 ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv)));
857 XSRETURN(1);
858}
859
14a976d6 860XS(XS_Internals_SvREADONLY) /* This is dangerous stuff. */
29569577 861{
97aff369 862 dVAR;
29569577 863 dXSARGS;
c4420975 864 SV * const sv = SvRV(ST(0));
58c0efa5 865 PERL_UNUSED_ARG(cv);
6867be6d 866
29569577
JH
867 if (items == 1) {
868 if (SvREADONLY(sv))
869 XSRETURN_YES;
870 else
871 XSRETURN_NO;
872 }
873 else if (items == 2) {
874 if (SvTRUE(ST(1))) {
875 SvREADONLY_on(sv);
876 XSRETURN_YES;
877 }
878 else {
14a976d6 879 /* I hope you really know what you are doing. */
29569577
JH
880 SvREADONLY_off(sv);
881 XSRETURN_NO;
882 }
883 }
14a976d6 884 XSRETURN_UNDEF; /* Can't happen. */
29569577
JH
885}
886
14a976d6 887XS(XS_Internals_SvREFCNT) /* This is dangerous stuff. */
29569577 888{
97aff369 889 dVAR;
29569577 890 dXSARGS;
c4420975 891 SV * const sv = SvRV(ST(0));
58c0efa5 892 PERL_UNUSED_ARG(cv);
6867be6d 893
29569577 894 if (items == 1)
14a976d6 895 XSRETURN_IV(SvREFCNT(sv) - 1); /* Minus the ref created for us. */
29569577 896 else if (items == 2) {
14a976d6 897 /* I hope you really know what you are doing. */
29569577
JH
898 SvREFCNT(sv) = SvIV(ST(1));
899 XSRETURN_IV(SvREFCNT(sv));
900 }
14a976d6 901 XSRETURN_UNDEF; /* Can't happen. */
29569577
JH
902}
903
f044d0d1 904XS(XS_Internals_hv_clear_placehold)
dfd4ef2f 905{
97aff369 906 dVAR;
dfd4ef2f 907 dXSARGS;
58c0efa5 908 PERL_UNUSED_ARG(cv);
6867be6d 909
3540d4ce
AB
910 if (items != 1)
911 Perl_croak(aTHX_ "Usage: UNIVERSAL::hv_clear_placeholders(hv)");
c4420975
AL
912 else {
913 HV * const hv = (HV *) SvRV(ST(0));
914 hv_clear_placeholders(hv);
915 XSRETURN(0);
916 }
dfd4ef2f 917}
39f7a870 918
39cff0d9
AE
919XS(XS_Regexp_DESTROY)
920{
96a5add6 921 PERL_UNUSED_CONTEXT;
53c1dcc0 922 PERL_UNUSED_ARG(cv);
39cff0d9
AE
923}
924
39f7a870
JH
925XS(XS_PerlIO_get_layers)
926{
97aff369 927 dVAR;
39f7a870 928 dXSARGS;
58c0efa5 929 PERL_UNUSED_ARG(cv);
39f7a870
JH
930 if (items < 1 || items % 2 == 0)
931 Perl_croak(aTHX_ "Usage: PerlIO_get_layers(filehandle[,args])");
5fef3b4a 932#ifdef USE_PERLIO
39f7a870
JH
933 {
934 SV * sv;
935 GV * gv;
936 IO * io;
937 bool input = TRUE;
938 bool details = FALSE;
939
940 if (items > 1) {
c4420975 941 SV * const *svp;
39f7a870 942 for (svp = MARK + 2; svp <= SP; svp += 2) {
c4420975
AL
943 SV * const * const varp = svp;
944 SV * const * const valp = svp + 1;
39f7a870 945 STRLEN klen;
c4420975 946 const char * const key = SvPV_const(*varp, klen);
39f7a870
JH
947
948 switch (*key) {
949 case 'i':
950 if (klen == 5 && memEQ(key, "input", 5)) {
951 input = SvTRUE(*valp);
952 break;
953 }
954 goto fail;
955 case 'o':
956 if (klen == 6 && memEQ(key, "output", 6)) {
957 input = !SvTRUE(*valp);
958 break;
959 }
960 goto fail;
961 case 'd':
962 if (klen == 7 && memEQ(key, "details", 7)) {
963 details = SvTRUE(*valp);
964 break;
965 }
966 goto fail;
967 default:
968 fail:
969 Perl_croak(aTHX_
970 "get_layers: unknown argument '%s'",
971 key);
972 }
973 }
974
975 SP -= (items - 1);
976 }
977
978 sv = POPs;
979 gv = (GV*)sv;
980
981 if (!isGV(sv)) {
982 if (SvROK(sv) && isGV(SvRV(sv)))
983 gv = (GV*)SvRV(sv);
671d49be 984 else if (SvPOKp(sv))
f776e3cd 985 gv = gv_fetchsv(sv, 0, SVt_PVIO);
39f7a870
JH
986 }
987
988 if (gv && (io = GvIO(gv))) {
989 dTARGET;
c4420975 990 AV* const av = PerlIO_get_layers(aTHX_ input ?
39f7a870
JH
991 IoIFP(io) : IoOFP(io));
992 I32 i;
c4420975 993 const I32 last = av_len(av);
39f7a870
JH
994 I32 nitem = 0;
995
996 for (i = last; i >= 0; i -= 3) {
c4420975
AL
997 SV * const * const namsvp = av_fetch(av, i - 2, FALSE);
998 SV * const * const argsvp = av_fetch(av, i - 1, FALSE);
999 SV * const * const flgsvp = av_fetch(av, i, FALSE);
39f7a870 1000
c4420975
AL
1001 const bool namok = namsvp && *namsvp && SvPOK(*namsvp);
1002 const bool argok = argsvp && *argsvp && SvPOK(*argsvp);
1003 const bool flgok = flgsvp && *flgsvp && SvIOK(*flgsvp);
39f7a870
JH
1004
1005 if (details) {
ec3bab8e
NC
1006 XPUSHs(namok
1007 ? newSVpvn(SvPVX_const(*namsvp), SvCUR(*namsvp))
1008 : &PL_sv_undef);
1009 XPUSHs(argok
1010 ? newSVpvn(SvPVX_const(*argsvp), SvCUR(*argsvp))
1011 : &PL_sv_undef);
39f7a870
JH
1012 if (flgok)
1013 XPUSHi(SvIVX(*flgsvp));
1014 else
1015 XPUSHs(&PL_sv_undef);
1016 nitem += 3;
1017 }
1018 else {
1019 if (namok && argok)
1020 XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf"(%"SVf")",
be2597df
MHM
1021 SVfARG(*namsvp),
1022 SVfARG(*argsvp)));
39f7a870 1023 else if (namok)
95b63a38 1024 XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf,
be2597df 1025 SVfARG(*namsvp)));
39f7a870
JH
1026 else
1027 XPUSHs(&PL_sv_undef);
1028 nitem++;
1029 if (flgok) {
c4420975 1030 const IV flags = SvIVX(*flgsvp);
39f7a870
JH
1031
1032 if (flags & PERLIO_F_UTF8) {
396482e1 1033 XPUSHs(newSVpvs("utf8"));
39f7a870
JH
1034 nitem++;
1035 }
1036 }
1037 }
1038 }
1039
1040 SvREFCNT_dec(av);
1041
1042 XSRETURN(nitem);
1043 }
1044 }
5fef3b4a 1045#endif
39f7a870
JH
1046
1047 XSRETURN(0);
1048}
1049
9a7034eb 1050XS(XS_Internals_hash_seed)
c910b28a 1051{
97aff369 1052 dVAR;
c85d3f85
NC
1053 /* Using dXSARGS would also have dITEM and dSP,
1054 * which define 2 unused local variables. */
557b887a 1055 dAXMARK;
53c1dcc0 1056 PERL_UNUSED_ARG(cv);
ad73156c 1057 PERL_UNUSED_VAR(mark);
81eaca17 1058 XSRETURN_UV(PERL_HASH_SEED);
c910b28a
JH
1059}
1060
008fb0c0 1061XS(XS_Internals_rehash_seed)
8e90d776 1062{
97aff369 1063 dVAR;
8e90d776
NC
1064 /* Using dXSARGS would also have dITEM and dSP,
1065 * which define 2 unused local variables. */
557b887a 1066 dAXMARK;
53c1dcc0 1067 PERL_UNUSED_ARG(cv);
ad73156c 1068 PERL_UNUSED_VAR(mark);
008fb0c0 1069 XSRETURN_UV(PL_rehash_seed);
8e90d776
NC
1070}
1071
05619474
NC
1072XS(XS_Internals_HvREHASH) /* Subject to change */
1073{
97aff369 1074 dVAR;
05619474 1075 dXSARGS;
58c0efa5 1076 PERL_UNUSED_ARG(cv);
05619474 1077 if (SvROK(ST(0))) {
c4420975 1078 const HV * const hv = (HV *) SvRV(ST(0));
05619474
NC
1079 if (items == 1 && SvTYPE(hv) == SVt_PVHV) {
1080 if (HvREHASH(hv))
1081 XSRETURN_YES;
1082 else
1083 XSRETURN_NO;
1084 }
1085 }
1086 Perl_croak(aTHX_ "Internals::HvREHASH $hashref");
1087}
241d1a3b 1088
e1234d8e
NC
1089XS(XS_Internals_inc_sub_generation)
1090{
97aff369 1091 dVAR;
e1234d8e
NC
1092 /* Using dXSARGS would also have dITEM and dSP,
1093 * which define 2 unused local variables. */
1094 dAXMARK;
1095 PERL_UNUSED_ARG(cv);
1096 PERL_UNUSED_VAR(mark);
1097 ++PL_sub_generation;
1098 XSRETURN_EMPTY;
1099}
1100
241d1a3b
NC
1101/*
1102 * Local variables:
1103 * c-indentation-style: bsd
1104 * c-basic-offset: 4
1105 * indent-tabs-mode: t
1106 * End:
1107 *
37442d52
RGS
1108 * ex: set ts=8 sts=4 sw=4 noet:
1109 */