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