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