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