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