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