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