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