This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
performance tweaking op.c
[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
PP
37{
38 AV* av;
39 GV* gv;
40 GV** gvp;
5c284bb0 41 HV* hv = NULL;
c445ea15 42 SV* subgen = NULL;
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 68 SV* sv;
7452cf6a 69 SV** const svp = (SV**)hv_fetch(hv, name, len, FALSE);
46e4b22b
GS
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 106 while (items--) {
c4420975
AL
107 SV* const sv = *svp++;
108 HV* const basestash = gv_stashsv(sv, FALSE);
6d4a7be2 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{
0b6f4f5c 143 HV *stash;
46e4b22b 144
5b295bef 145 SvGETMAGIC(sv);
55497cff
PP
146
147 if (SvROK(sv)) {
0b6f4f5c 148 const char *type;
55497cff
PP
149 sv = SvRV(sv);
150 type = sv_reftype(sv,0);
0b6f4f5c
AL
151 if (type && strEQ(type,name))
152 return TRUE;
153 stash = SvOBJECT(sv) ? SvSTASH(sv) : NULL;
55497cff
PP
154 }
155 else {
156 stash = gv_stashsv(sv, FALSE);
157 }
46e4b22b 158
0b6f4f5c
AL
159 if (stash) {
160 HV * const name_stash = gv_stashpv(name, FALSE);
161 return isa_lookup(stash, name, name_stash, strlen(name), 0) == &PL_sv_yes;
162 }
163 else
164 return FALSE;
301daebc 165
55497cff
PP
166}
167
1b026014
NIS
168#include "XSUB.h"
169
27da23d5
JH
170PERL_XS_EXPORT_C void XS_UNIVERSAL_isa(pTHX_ CV *cv);
171PERL_XS_EXPORT_C void XS_UNIVERSAL_can(pTHX_ CV *cv);
172PERL_XS_EXPORT_C void XS_UNIVERSAL_VERSION(pTHX_ CV *cv);
439cb1c4
JP
173XS(XS_version_new);
174XS(XS_version_stringify);
175XS(XS_version_numify);
9137345a 176XS(XS_version_normal);
439cb1c4
JP
177XS(XS_version_vcmp);
178XS(XS_version_boolean);
2dfd8427
AL
179#ifdef HASATTRIBUTE_NORETURN
180XS(XS_version_noop) __attribute__noreturn__;
181#else
439cb1c4 182XS(XS_version_noop);
2dfd8427 183#endif
c8d69e4a 184XS(XS_version_is_alpha);
137d6fc0 185XS(XS_version_qv);
8800c35a 186XS(XS_utf8_is_utf8);
1b026014
NIS
187XS(XS_utf8_valid);
188XS(XS_utf8_encode);
189XS(XS_utf8_decode);
190XS(XS_utf8_upgrade);
191XS(XS_utf8_downgrade);
192XS(XS_utf8_unicode_to_native);
193XS(XS_utf8_native_to_unicode);
29569577
JH
194XS(XS_Internals_SvREADONLY);
195XS(XS_Internals_SvREFCNT);
f044d0d1 196XS(XS_Internals_hv_clear_placehold);
39f7a870 197XS(XS_PerlIO_get_layers);
39cff0d9 198XS(XS_Regexp_DESTROY);
9a7034eb 199XS(XS_Internals_hash_seed);
008fb0c0 200XS(XS_Internals_rehash_seed);
05619474 201XS(XS_Internals_HvREHASH);
e1234d8e 202XS(XS_Internals_inc_sub_generation);
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, "\\%");
e1234d8e
NC
251 newXSproto("Internals::inc_sub_generation",XS_Internals_inc_sub_generation,
252 file, "");
0cb96387
GS
253}
254
55497cff 255
6d4a7be2
PP
256XS(XS_UNIVERSAL_isa)
257{
258 dXSARGS;
6d4a7be2
PP
259
260 if (items != 2)
cea2e8a9 261 Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)");
c4420975
AL
262 else {
263 SV * const sv = ST(0);
264 const char *name;
6d4a7be2 265
c4420975 266 SvGETMAGIC(sv);
d3f7f2b2 267
c4420975
AL
268 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
269 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
270 XSRETURN_UNDEF;
f8f70380 271
c4420975 272 name = SvPV_nolen_const(ST(1));
6d4a7be2 273
c4420975
AL
274 ST(0) = boolSV(sv_derived_from(sv, name));
275 XSRETURN(1);
276 }
6d4a7be2
PP
277}
278
6d4a7be2
PP
279XS(XS_UNIVERSAL_can)
280{
281 dXSARGS;
282 SV *sv;
6867be6d 283 const char *name;
6d4a7be2 284 SV *rv;
6f08146e 285 HV *pkg = NULL;
6d4a7be2
PP
286
287 if (items != 2)
cea2e8a9 288 Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)");
6d4a7be2
PP
289
290 sv = ST(0);
f8f70380 291
5b295bef 292 SvGETMAGIC(sv);
d3f7f2b2 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) {
c4420975 311 GV * const gv = gv_fetchmethod_autoload(pkg, name, FALSE);
dc848c6f
PP
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
0008872a 341 if (gvp && isGV(gv = *gvp) && (sv = GvSV(gv)) && SvOK(sv)) {
c4420975 342 SV * const nsv = sv_newmortal();
6d4a7be2
PP
343 sv_setsv(nsv, sv);
344 sv = nsv;
137d6fc0
JP
345 if ( !sv_derived_from(sv, "version"))
346 upg_version(sv);
c445ea15 347 undef = NULL;
6d4a7be2
PP
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 358 if (pkg) {
c4420975 359 const char * const 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 */
c4420975 372 SV * const nsv = sv_newmortal();
137d6fc0
JP
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;
c4420975
AL
402 const char * const classname =
403 sv_isobject(ST(0)) /* get the class if called as an object method */
404 ? HvNAME(SvSTASH(SvRV(ST(0))))
405 : (char *)SvPV_nolen(ST(0));
9137345a
JP
406
407 if ( items == 1 ) {
408 /* no parameter provided */
409 if ( sv_isobject(ST(0)) ) {
410 /* copy existing object */
411 vs = ST(0);
412 }
413 else {
414 /* create empty object */
415 vs = sv_newmortal();
1946a074 416 sv_setpvn(vs,"",0);
9137345a
JP
417 }
418 }
419 else if ( items == 3 ) {
420 vs = sv_newmortal();
cfd0369c 421 Perl_sv_setpvf(aTHX_ vs,"v%s",SvPV_nolen_const(ST(2)));
129318bd 422 }
439cb1c4 423
137d6fc0 424 rv = new_version(vs);
0723351e
NC
425 if ( strcmp(classname,"version") != 0 ) /* inherited new() */
426 sv_bless(rv, gv_stashpv(classname,TRUE));
137d6fc0
JP
427
428 PUSHs(sv_2mortal(rv));
439cb1c4
JP
429 PUTBACK;
430 return;
431 }
432}
433
434XS(XS_version_stringify)
435{
41be1fbd
JH
436 dXSARGS;
437 if (items < 1)
438 Perl_croak(aTHX_ "Usage: version::stringify(lobj, ...)");
439 SP -= items;
440 {
7452cf6a 441 SV * lobj;
41be1fbd
JH
442
443 if (sv_derived_from(ST(0), "version")) {
9137345a 444 lobj = SvRV(ST(0));
41be1fbd
JH
445 }
446 else
447 Perl_croak(aTHX_ "lobj is not of type version");
448
137d6fc0 449 PUSHs(sv_2mortal(vstringify(lobj)));
41be1fbd
JH
450
451 PUTBACK;
452 return;
453 }
439cb1c4
JP
454}
455
456XS(XS_version_numify)
457{
41be1fbd
JH
458 dXSARGS;
459 if (items < 1)
460 Perl_croak(aTHX_ "Usage: version::numify(lobj, ...)");
461 SP -= items;
462 {
7452cf6a 463 SV * lobj;
41be1fbd
JH
464
465 if (sv_derived_from(ST(0), "version")) {
9137345a 466 lobj = SvRV(ST(0));
41be1fbd
JH
467 }
468 else
469 Perl_croak(aTHX_ "lobj is not of type version");
470
137d6fc0 471 PUSHs(sv_2mortal(vnumify(lobj)));
41be1fbd
JH
472
473 PUTBACK;
474 return;
475 }
439cb1c4
JP
476}
477
9137345a
JP
478XS(XS_version_normal)
479{
480 dXSARGS;
481 if (items < 1)
482 Perl_croak(aTHX_ "Usage: version::normal(lobj, ...)");
483 SP -= items;
484 {
7452cf6a 485 SV * lobj;
9137345a
JP
486
487 if (sv_derived_from(ST(0), "version")) {
488 lobj = SvRV(ST(0));
489 }
490 else
491 Perl_croak(aTHX_ "lobj is not of type version");
492
493 PUSHs(sv_2mortal(vnormal(lobj)));
494
495 PUTBACK;
496 return;
497 }
498}
499
439cb1c4
JP
500XS(XS_version_vcmp)
501{
41be1fbd
JH
502 dXSARGS;
503 if (items < 1)
504 Perl_croak(aTHX_ "Usage: version::vcmp(lobj, ...)");
505 SP -= items;
506 {
7452cf6a 507 SV * lobj;
41be1fbd
JH
508
509 if (sv_derived_from(ST(0), "version")) {
9137345a 510 lobj = SvRV(ST(0));
41be1fbd
JH
511 }
512 else
513 Perl_croak(aTHX_ "lobj is not of type version");
514
515 {
516 SV *rs;
517 SV *rvs;
518 SV * robj = ST(1);
7452cf6a 519 const IV swap = (IV)SvIV(ST(2));
41be1fbd
JH
520
521 if ( ! sv_derived_from(robj, "version") )
522 {
523 robj = new_version(robj);
524 }
525 rvs = SvRV(robj);
526
527 if ( swap )
528 {
529 rs = newSViv(vcmp(rvs,lobj));
530 }
531 else
532 {
533 rs = newSViv(vcmp(lobj,rvs));
534 }
535
137d6fc0 536 PUSHs(sv_2mortal(rs));
41be1fbd
JH
537 }
538
539 PUTBACK;
540 return;
541 }
439cb1c4
JP
542}
543
544XS(XS_version_boolean)
545{
41be1fbd
JH
546 dXSARGS;
547 if (items < 1)
548 Perl_croak(aTHX_ "Usage: version::boolean(lobj, ...)");
549 SP -= items;
c4420975
AL
550 if (sv_derived_from(ST(0), "version")) {
551 SV * const lobj = SvRV(ST(0));
396482e1 552 SV * const rs = newSViv( vcmp(lobj,new_version(newSVpvs("0"))) );
c4420975
AL
553 PUSHs(sv_2mortal(rs));
554 PUTBACK;
555 return;
556 }
557 else
558 Perl_croak(aTHX_ "lobj is not of type version");
439cb1c4
JP
559}
560
561XS(XS_version_noop)
562{
2dfd8427
AL
563 dXSARGS;
564 if (items < 1)
565 Perl_croak(aTHX_ "Usage: version::noop(lobj, ...)");
566 if (sv_derived_from(ST(0), "version"))
567 Perl_croak(aTHX_ "operation not supported with version object");
568 else
569 Perl_croak(aTHX_ "lobj is not of type version");
570#ifndef HASATTRIBUTE_NORETURN
571 XSRETURN_EMPTY;
572#endif
439cb1c4
JP
573}
574
c8d69e4a
JP
575XS(XS_version_is_alpha)
576{
577 dXSARGS;
578 if (items != 1)
579 Perl_croak(aTHX_ "Usage: version::is_alpha(lobj)");
580 SP -= items;
c4420975
AL
581 if (sv_derived_from(ST(0), "version")) {
582 SV * const lobj = ST(0);
583 if ( hv_exists((HV*)SvRV(lobj), "alpha", 5 ) )
584 XSRETURN_YES;
585 else
586 XSRETURN_NO;
c8d69e4a
JP
587 PUTBACK;
588 return;
589 }
c4420975
AL
590 else
591 Perl_croak(aTHX_ "lobj is not of type version");
c8d69e4a
JP
592}
593
137d6fc0
JP
594XS(XS_version_qv)
595{
596 dXSARGS;
597 if (items != 1)
598 Perl_croak(aTHX_ "Usage: version::qv(ver)");
599 SP -= items;
600 {
601 SV * ver = ST(0);
c4420975
AL
602 if ( !SvVOK(ver) ) { /* only need to do with if not already v-string */
603 SV * const vs = sv_newmortal();
137d6fc0
JP
604 char *version;
605 if ( SvNOK(ver) ) /* may get too much accuracy */
606 {
607 char tbuf[64];
86c11942
NC
608 const STRLEN len = my_sprintf(tbuf,"%.9"NVgf, SvNVX(ver));
609 version = savepvn(tbuf, len);
137d6fc0
JP
610 }
611 else
612 {
2e0de35c 613 version = savesvpv(ver);
137d6fc0
JP
614 }
615 (void)scan_version(version,vs,TRUE);
616 Safefree(version);
617
618 PUSHs(vs);
619 }
620 else
621 {
622 PUSHs(sv_2mortal(new_version(ver)));
623 }
624
625 PUTBACK;
626 return;
627 }
628}
629
8800c35a
JH
630XS(XS_utf8_is_utf8)
631{
41be1fbd
JH
632 dXSARGS;
633 if (items != 1)
634 Perl_croak(aTHX_ "Usage: utf8::is_utf8(sv)");
c4420975
AL
635 else {
636 const SV * const sv = ST(0);
637 if (SvUTF8(sv))
638 XSRETURN_YES;
639 else
640 XSRETURN_NO;
41be1fbd
JH
641 }
642 XSRETURN_EMPTY;
8800c35a
JH
643}
644
1b026014
NIS
645XS(XS_utf8_valid)
646{
41be1fbd
JH
647 dXSARGS;
648 if (items != 1)
649 Perl_croak(aTHX_ "Usage: utf8::valid(sv)");
c4420975
AL
650 else {
651 SV * const sv = ST(0);
652 STRLEN len;
653 const char * const s = SvPV_const(sv,len);
654 if (!SvUTF8(sv) || is_utf8_string((const U8*)s,len))
655 XSRETURN_YES;
656 else
657 XSRETURN_NO;
658 }
41be1fbd 659 XSRETURN_EMPTY;
1b026014
NIS
660}
661
662XS(XS_utf8_encode)
663{
664 dXSARGS;
665 if (items != 1)
666 Perl_croak(aTHX_ "Usage: utf8::encode(sv)");
c4420975 667 sv_utf8_encode(ST(0));
1b026014
NIS
668 XSRETURN_EMPTY;
669}
670
671XS(XS_utf8_decode)
672{
673 dXSARGS;
674 if (items != 1)
675 Perl_croak(aTHX_ "Usage: utf8::decode(sv)");
c4420975
AL
676 else {
677 SV * const sv = ST(0);
6867be6d 678 const bool RETVAL = sv_utf8_decode(sv);
1b026014
NIS
679 ST(0) = boolSV(RETVAL);
680 sv_2mortal(ST(0));
681 }
682 XSRETURN(1);
683}
684
685XS(XS_utf8_upgrade)
686{
687 dXSARGS;
688 if (items != 1)
689 Perl_croak(aTHX_ "Usage: utf8::upgrade(sv)");
c4420975
AL
690 else {
691 SV * const sv = ST(0);
1b026014
NIS
692 STRLEN RETVAL;
693 dXSTARG;
694
695 RETVAL = sv_utf8_upgrade(sv);
696 XSprePUSH; PUSHi((IV)RETVAL);
697 }
698 XSRETURN(1);
699}
700
701XS(XS_utf8_downgrade)
702{
703 dXSARGS;
704 if (items < 1 || items > 2)
705 Perl_croak(aTHX_ "Usage: utf8::downgrade(sv, failok=0)");
c4420975
AL
706 else {
707 SV * const sv = ST(0);
6867be6d
AL
708 const bool failok = (items < 2) ? 0 : (int)SvIV(ST(1));
709 const bool RETVAL = sv_utf8_downgrade(sv, failok);
1b026014 710
1b026014
NIS
711 ST(0) = boolSV(RETVAL);
712 sv_2mortal(ST(0));
713 }
714 XSRETURN(1);
715}
716
717XS(XS_utf8_native_to_unicode)
718{
719 dXSARGS;
6867be6d 720 const UV uv = SvUV(ST(0));
b7953727
JH
721
722 if (items > 1)
723 Perl_croak(aTHX_ "Usage: utf8::native_to_unicode(sv)");
724
1b026014
NIS
725 ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv)));
726 XSRETURN(1);
727}
728
729XS(XS_utf8_unicode_to_native)
730{
731 dXSARGS;
6867be6d 732 const UV uv = SvUV(ST(0));
b7953727
JH
733
734 if (items > 1)
735 Perl_croak(aTHX_ "Usage: utf8::unicode_to_native(sv)");
736
1b026014
NIS
737 ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv)));
738 XSRETURN(1);
739}
740
14a976d6 741XS(XS_Internals_SvREADONLY) /* This is dangerous stuff. */
29569577
JH
742{
743 dXSARGS;
c4420975 744 SV * const sv = SvRV(ST(0));
6867be6d 745
29569577
JH
746 if (items == 1) {
747 if (SvREADONLY(sv))
748 XSRETURN_YES;
749 else
750 XSRETURN_NO;
751 }
752 else if (items == 2) {
753 if (SvTRUE(ST(1))) {
754 SvREADONLY_on(sv);
755 XSRETURN_YES;
756 }
757 else {
14a976d6 758 /* I hope you really know what you are doing. */
29569577
JH
759 SvREADONLY_off(sv);
760 XSRETURN_NO;
761 }
762 }
14a976d6 763 XSRETURN_UNDEF; /* Can't happen. */
29569577
JH
764}
765
14a976d6 766XS(XS_Internals_SvREFCNT) /* This is dangerous stuff. */
29569577
JH
767{
768 dXSARGS;
c4420975 769 SV * const sv = SvRV(ST(0));
6867be6d 770
29569577 771 if (items == 1)
14a976d6 772 XSRETURN_IV(SvREFCNT(sv) - 1); /* Minus the ref created for us. */
29569577 773 else if (items == 2) {
14a976d6 774 /* I hope you really know what you are doing. */
29569577
JH
775 SvREFCNT(sv) = SvIV(ST(1));
776 XSRETURN_IV(SvREFCNT(sv));
777 }
14a976d6 778 XSRETURN_UNDEF; /* Can't happen. */
29569577
JH
779}
780
f044d0d1 781XS(XS_Internals_hv_clear_placehold)
dfd4ef2f
NC
782{
783 dXSARGS;
6867be6d 784
3540d4ce
AB
785 if (items != 1)
786 Perl_croak(aTHX_ "Usage: UNIVERSAL::hv_clear_placeholders(hv)");
c4420975
AL
787 else {
788 HV * const hv = (HV *) SvRV(ST(0));
789 hv_clear_placeholders(hv);
790 XSRETURN(0);
791 }
dfd4ef2f 792}
39f7a870 793
39cff0d9
AE
794XS(XS_Regexp_DESTROY)
795{
53c1dcc0 796 PERL_UNUSED_ARG(cv);
39cff0d9
AE
797}
798
39f7a870
JH
799XS(XS_PerlIO_get_layers)
800{
801 dXSARGS;
802 if (items < 1 || items % 2 == 0)
803 Perl_croak(aTHX_ "Usage: PerlIO_get_layers(filehandle[,args])");
5fef3b4a 804#ifdef USE_PERLIO
39f7a870
JH
805 {
806 SV * sv;
807 GV * gv;
808 IO * io;
809 bool input = TRUE;
810 bool details = FALSE;
811
812 if (items > 1) {
c4420975 813 SV * const *svp;
39f7a870 814 for (svp = MARK + 2; svp <= SP; svp += 2) {
c4420975
AL
815 SV * const * const varp = svp;
816 SV * const * const valp = svp + 1;
39f7a870 817 STRLEN klen;
c4420975 818 const char * const key = SvPV_const(*varp, klen);
39f7a870
JH
819
820 switch (*key) {
821 case 'i':
822 if (klen == 5 && memEQ(key, "input", 5)) {
823 input = SvTRUE(*valp);
824 break;
825 }
826 goto fail;
827 case 'o':
828 if (klen == 6 && memEQ(key, "output", 6)) {
829 input = !SvTRUE(*valp);
830 break;
831 }
832 goto fail;
833 case 'd':
834 if (klen == 7 && memEQ(key, "details", 7)) {
835 details = SvTRUE(*valp);
836 break;
837 }
838 goto fail;
839 default:
840 fail:
841 Perl_croak(aTHX_
842 "get_layers: unknown argument '%s'",
843 key);
844 }
845 }
846
847 SP -= (items - 1);
848 }
849
850 sv = POPs;
851 gv = (GV*)sv;
852
853 if (!isGV(sv)) {
854 if (SvROK(sv) && isGV(SvRV(sv)))
855 gv = (GV*)SvRV(sv);
671d49be 856 else if (SvPOKp(sv))
f776e3cd 857 gv = gv_fetchsv(sv, 0, SVt_PVIO);
39f7a870
JH
858 }
859
860 if (gv && (io = GvIO(gv))) {
861 dTARGET;
c4420975 862 AV* const av = PerlIO_get_layers(aTHX_ input ?
39f7a870
JH
863 IoIFP(io) : IoOFP(io));
864 I32 i;
c4420975 865 const I32 last = av_len(av);
39f7a870
JH
866 I32 nitem = 0;
867
868 for (i = last; i >= 0; i -= 3) {
c4420975
AL
869 SV * const * const namsvp = av_fetch(av, i - 2, FALSE);
870 SV * const * const argsvp = av_fetch(av, i - 1, FALSE);
871 SV * const * const flgsvp = av_fetch(av, i, FALSE);
39f7a870 872
c4420975
AL
873 const bool namok = namsvp && *namsvp && SvPOK(*namsvp);
874 const bool argok = argsvp && *argsvp && SvPOK(*argsvp);
875 const bool flgok = flgsvp && *flgsvp && SvIOK(*flgsvp);
39f7a870
JH
876
877 if (details) {
ec3bab8e
NC
878 XPUSHs(namok
879 ? newSVpvn(SvPVX_const(*namsvp), SvCUR(*namsvp))
880 : &PL_sv_undef);
881 XPUSHs(argok
882 ? newSVpvn(SvPVX_const(*argsvp), SvCUR(*argsvp))
883 : &PL_sv_undef);
39f7a870
JH
884 if (flgok)
885 XPUSHi(SvIVX(*flgsvp));
886 else
887 XPUSHs(&PL_sv_undef);
888 nitem += 3;
889 }
890 else {
891 if (namok && argok)
892 XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf"(%"SVf")",
893 *namsvp, *argsvp));
894 else if (namok)
895 XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf, *namsvp));
896 else
897 XPUSHs(&PL_sv_undef);
898 nitem++;
899 if (flgok) {
c4420975 900 const IV flags = SvIVX(*flgsvp);
39f7a870
JH
901
902 if (flags & PERLIO_F_UTF8) {
396482e1 903 XPUSHs(newSVpvs("utf8"));
39f7a870
JH
904 nitem++;
905 }
906 }
907 }
908 }
909
910 SvREFCNT_dec(av);
911
912 XSRETURN(nitem);
913 }
914 }
5fef3b4a 915#endif
39f7a870
JH
916
917 XSRETURN(0);
918}
919
9a7034eb 920XS(XS_Internals_hash_seed)
c910b28a 921{
c85d3f85
NC
922 /* Using dXSARGS would also have dITEM and dSP,
923 * which define 2 unused local variables. */
557b887a 924 dAXMARK;
53c1dcc0 925 PERL_UNUSED_ARG(cv);
ad73156c 926 PERL_UNUSED_VAR(mark);
81eaca17 927 XSRETURN_UV(PERL_HASH_SEED);
c910b28a
JH
928}
929
008fb0c0 930XS(XS_Internals_rehash_seed)
8e90d776
NC
931{
932 /* Using dXSARGS would also have dITEM and dSP,
933 * which define 2 unused local variables. */
557b887a 934 dAXMARK;
53c1dcc0 935 PERL_UNUSED_ARG(cv);
ad73156c 936 PERL_UNUSED_VAR(mark);
008fb0c0 937 XSRETURN_UV(PL_rehash_seed);
8e90d776
NC
938}
939
05619474
NC
940XS(XS_Internals_HvREHASH) /* Subject to change */
941{
942 dXSARGS;
943 if (SvROK(ST(0))) {
c4420975 944 const HV * const hv = (HV *) SvRV(ST(0));
05619474
NC
945 if (items == 1 && SvTYPE(hv) == SVt_PVHV) {
946 if (HvREHASH(hv))
947 XSRETURN_YES;
948 else
949 XSRETURN_NO;
950 }
951 }
952 Perl_croak(aTHX_ "Internals::HvREHASH $hashref");
953}
241d1a3b 954
e1234d8e
NC
955XS(XS_Internals_inc_sub_generation)
956{
957 /* Using dXSARGS would also have dITEM and dSP,
958 * which define 2 unused local variables. */
959 dAXMARK;
960 PERL_UNUSED_ARG(cv);
961 PERL_UNUSED_VAR(mark);
962 ++PL_sub_generation;
963 XSRETURN_EMPTY;
964}
965
241d1a3b
NC
966/*
967 * Local variables:
968 * c-indentation-style: bsd
969 * c-basic-offset: 4
970 * indent-tabs-mode: t
971 * End:
972 *
37442d52
RGS
973 * ex: set ts=8 sts=4 sw=4 noet:
974 */