This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
utf.pm needs to require Carp before croak()ing
[perl5.git] / universal.c
CommitLineData
d6376244
JH
1/* universal.c
2 *
b5f8cc5c 3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4bb101f2 4 * 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;
6d4a7be2 43
301daebc
MS
44 /* A stash/class can go by many names (ie. User == main::User), so
45 we compare the stash itself just in case */
46 if (name_stash && (stash == name_stash))
47 return &PL_sv_yes;
6d4a7be2 48
46e4b22b 49 if (strEQ(HvNAME(stash), name))
3280af22 50 return &PL_sv_yes;
6d4a7be2 51
a1d407e8
DM
52 if (strEQ(name, "UNIVERSAL"))
53 return &PL_sv_yes;
54
6d4a7be2 55 if (level > 100)
46e4b22b
GS
56 Perl_croak(aTHX_ "Recursive inheritance detected in package '%s'",
57 HvNAME(stash));
6d4a7be2
PP
58
59 gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, FALSE);
60
46e4b22b
GS
61 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (subgen = GvSV(gv))
62 && (hv = GvHV(gv)))
63 {
eb160463 64 if (SvIV(subgen) == (IV)PL_sub_generation) {
46e4b22b
GS
65 SV* sv;
66 SV** svp = (SV**)hv_fetch(hv, name, len, FALSE);
67 if (svp && (sv = *svp) != (SV*)&PL_sv_undef) {
68 DEBUG_o( Perl_deb(aTHX_ "Using cached ISA %s for package %s\n",
69 name, HvNAME(stash)) );
70 return sv;
71 }
72 }
73 else {
74 DEBUG_o( Perl_deb(aTHX_ "ISA Cache in package %s is stale\n",
75 HvNAME(stash)) );
76 hv_clear(hv);
77 sv_setiv(subgen, PL_sub_generation);
78 }
6d4a7be2
PP
79 }
80
81 gvp = (GV**)hv_fetch(stash,"ISA",3,FALSE);
46e4b22b 82
3280af22 83 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (av = GvAV(gv))) {
46e4b22b 84 if (!hv || !subgen) {
6d4a7be2
PP
85 gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, TRUE);
86
87 gv = *gvp;
88
89 if (SvTYPE(gv) != SVt_PVGV)
90 gv_init(gv, stash, "::ISA::CACHE::", 14, TRUE);
91
46e4b22b
GS
92 if (!hv)
93 hv = GvHVn(gv);
94 if (!subgen) {
95 subgen = newSViv(PL_sub_generation);
96 GvSV(gv) = subgen;
97 }
6d4a7be2 98 }
46e4b22b 99 if (hv) {
6d4a7be2 100 SV** svp = AvARRAY(av);
93965878
NIS
101 /* NOTE: No support for tied ISA */
102 I32 items = AvFILLp(av) + 1;
6d4a7be2
PP
103 while (items--) {
104 SV* sv = *svp++;
105 HV* basestash = gv_stashsv(sv, FALSE);
106 if (!basestash) {
599cee73 107 if (ckWARN(WARN_MISC))
9014280d 108 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
35c1215d
NC
109 "Can't locate package %"SVf" for @%s::ISA",
110 sv, HvNAME(stash));
6d4a7be2
PP
111 continue;
112 }
301daebc
MS
113 if (&PL_sv_yes == isa_lookup(basestash, name, name_stash,
114 len, level + 1)) {
3280af22
NIS
115 (void)hv_store(hv,name,len,&PL_sv_yes,0);
116 return &PL_sv_yes;
6d4a7be2
PP
117 }
118 }
3280af22 119 (void)hv_store(hv,name,len,&PL_sv_no,0);
6d4a7be2
PP
120 }
121 }
a1d407e8 122 return &PL_sv_no;
6d4a7be2
PP
123}
124
954c1994 125/*
ccfc67b7
JH
126=head1 SV Manipulation Functions
127
954c1994
GS
128=for apidoc sv_derived_from
129
130Returns a boolean indicating whether the SV is derived from the specified
131class. This is the function that implements C<UNIVERSAL::isa>. It works
132for class names as well as for objects.
133
134=cut
135*/
136
55497cff 137bool
864dbfa3 138Perl_sv_derived_from(pTHX_ SV *sv, const char *name)
55497cff 139{
55497cff
PP
140 char *type;
141 HV *stash;
301daebc 142 HV *name_stash;
46e4b22b 143
55497cff
PP
144 stash = Nullhv;
145 type = Nullch;
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
acfe0abc
GS
171void XS_UNIVERSAL_isa(pTHX_ CV *cv);
172void XS_UNIVERSAL_can(pTHX_ CV *cv);
173void 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{
202 char *file = __FILE__;
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
PP
251 SV *sv;
252 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
2d8e6c8d 267 name = (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;
277 char *name;
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
2d8e6c8d 294 name = (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;
323 char *undef;
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
GS
350 if (items > 1) {
351 STRLEN len;
352 SV *req = ST(1);
353
62658f4d
PM
354 if (undef) {
355 if (pkg)
356 Perl_croak(aTHX_
357 "%s does not define $%s::VERSION--version check failed",
358 HvNAME(pkg), HvNAME(pkg));
359 else {
360 char *str = SvPVx(ST(0), len);
361
362 Perl_croak(aTHX_
363 "%s defines neither package nor VERSION--version check failed", str);
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
JP
376 Perl_croak(aTHX_ "%s version %"SVf" (%"SVf") required--"
377 "this is only version %"SVf" (%"SVf")", HvNAME(pkg),
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 char * class = (char *)SvPV_nolen(ST(0));
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
JP
406 rv = new_version(vs);
407 if ( strcmp(class,"version") != 0 ) /* inherited new() */
408 sv_bless(rv, gv_stashpv(class,TRUE));
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 {
423 SV * lobj;
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 {
446 SV * lobj;
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 {
469 SV * lobj;
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 {
514 SV * lobj;
515
516 if (sv_derived_from(ST(0), "version")) {
517 SV *tmp = SvRV(ST(0));
518 lobj = tmp;
519 }
520 else
521 Perl_croak(aTHX_ "lobj is not of type version");
522
523 {
524 SV *rs;
525 rs = newSViv( vcmp(lobj,new_version(newSVpvn("0",1))) );
137d6fc0 526 PUSHs(sv_2mortal(rs));
41be1fbd
JH
527 }
528
529 PUTBACK;
530 return;
531 }
439cb1c4
JP
532}
533
534XS(XS_version_noop)
535{
41be1fbd
JH
536 dXSARGS;
537 if (items < 1)
538 Perl_croak(aTHX_ "Usage: version::noop(lobj, ...)");
539 {
540 SV * lobj;
541
542 if (sv_derived_from(ST(0), "version")) {
543 SV *tmp = SvRV(ST(0));
544 lobj = tmp;
545 }
546 else
547 Perl_croak(aTHX_ "lobj is not of type version");
548
549 {
550 Perl_croak(aTHX_ "operation not supported with version object");
551 }
552
553 }
554 XSRETURN_EMPTY;
439cb1c4
JP
555}
556
c8d69e4a
JP
557XS(XS_version_is_alpha)
558{
559 dXSARGS;
560 if (items != 1)
561 Perl_croak(aTHX_ "Usage: version::is_alpha(lobj)");
562 SP -= items;
563 {
564 SV *lobj;
565
566 if (sv_derived_from(ST(0), "version")) {
567 SV *tmp = SvRV(ST(0));
568 lobj = tmp;
569 }
570 else
571 Perl_croak(aTHX_ "lobj is not of type version");
572{
573 I32 len = av_len((AV *)lobj);
574 I32 digit = SvIVX(*av_fetch((AV *)lobj, len, 0));
575 if ( digit < 0 )
576 XSRETURN_YES;
577 else
578 XSRETURN_NO;
579}
580 PUTBACK;
581 return;
582 }
583}
584
137d6fc0
JP
585XS(XS_version_qv)
586{
587 dXSARGS;
588 if (items != 1)
589 Perl_croak(aTHX_ "Usage: version::qv(ver)");
590 SP -= items;
591 {
592 SV * ver = ST(0);
593 if ( !SvVOK(ver) ) /* only need to do with if not already v-string */
594 {
595 SV *vs = sv_newmortal();
596 char *version;
597 if ( SvNOK(ver) ) /* may get too much accuracy */
598 {
599 char tbuf[64];
600 sprintf(tbuf,"%.9"NVgf, SvNVX(ver));
601 version = savepv(tbuf);
602 }
603 else
604 {
2e0de35c 605 version = savesvpv(ver);
137d6fc0
JP
606 }
607 (void)scan_version(version,vs,TRUE);
608 Safefree(version);
609
610 PUSHs(vs);
611 }
612 else
613 {
614 PUSHs(sv_2mortal(new_version(ver)));
615 }
616
617 PUTBACK;
618 return;
619 }
620}
621
8800c35a
JH
622XS(XS_utf8_is_utf8)
623{
41be1fbd
JH
624 dXSARGS;
625 if (items != 1)
626 Perl_croak(aTHX_ "Usage: utf8::is_utf8(sv)");
627 {
628 SV * sv = ST(0);
629 {
630 if (SvUTF8(sv))
631 XSRETURN_YES;
632 else
633 XSRETURN_NO;
634 }
635 }
636 XSRETURN_EMPTY;
8800c35a
JH
637}
638
1b026014
NIS
639XS(XS_utf8_valid)
640{
41be1fbd
JH
641 dXSARGS;
642 if (items != 1)
643 Perl_croak(aTHX_ "Usage: utf8::valid(sv)");
644 {
645 SV * sv = ST(0);
646 {
647 STRLEN len;
648 char *s = SvPV(sv,len);
649 if (!SvUTF8(sv) || is_utf8_string((U8*)s,len))
650 XSRETURN_YES;
651 else
652 XSRETURN_NO;
653 }
654 }
655 XSRETURN_EMPTY;
1b026014
NIS
656}
657
658XS(XS_utf8_encode)
659{
660 dXSARGS;
661 if (items != 1)
662 Perl_croak(aTHX_ "Usage: utf8::encode(sv)");
663 {
664 SV * sv = ST(0);
665
666 sv_utf8_encode(sv);
667 }
668 XSRETURN_EMPTY;
669}
670
671XS(XS_utf8_decode)
672{
673 dXSARGS;
674 if (items != 1)
675 Perl_croak(aTHX_ "Usage: utf8::decode(sv)");
676 {
677 SV * sv = ST(0);
678 bool RETVAL;
679
680 RETVAL = sv_utf8_decode(sv);
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);
710 bool failok;
711 bool RETVAL;
712
713 if (items < 2)
714 failok = 0;
715 else {
716 failok = (int)SvIV(ST(1));
717 }
718
719 RETVAL = sv_utf8_downgrade(sv, failok);
720 ST(0) = boolSV(RETVAL);
721 sv_2mortal(ST(0));
722 }
723 XSRETURN(1);
724}
725
726XS(XS_utf8_native_to_unicode)
727{
728 dXSARGS;
729 UV uv = SvUV(ST(0));
b7953727
JH
730
731 if (items > 1)
732 Perl_croak(aTHX_ "Usage: utf8::native_to_unicode(sv)");
733
1b026014
NIS
734 ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv)));
735 XSRETURN(1);
736}
737
738XS(XS_utf8_unicode_to_native)
739{
740 dXSARGS;
741 UV uv = SvUV(ST(0));
b7953727
JH
742
743 if (items > 1)
744 Perl_croak(aTHX_ "Usage: utf8::unicode_to_native(sv)");
745
1b026014
NIS
746 ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv)));
747 XSRETURN(1);
748}
749
14a976d6 750XS(XS_Internals_SvREADONLY) /* This is dangerous stuff. */
29569577
JH
751{
752 dXSARGS;
753 SV *sv = SvRV(ST(0));
754 if (items == 1) {
755 if (SvREADONLY(sv))
756 XSRETURN_YES;
757 else
758 XSRETURN_NO;
759 }
760 else if (items == 2) {
761 if (SvTRUE(ST(1))) {
762 SvREADONLY_on(sv);
763 XSRETURN_YES;
764 }
765 else {
14a976d6 766 /* I hope you really know what you are doing. */
29569577
JH
767 SvREADONLY_off(sv);
768 XSRETURN_NO;
769 }
770 }
14a976d6 771 XSRETURN_UNDEF; /* Can't happen. */
29569577
JH
772}
773
14a976d6 774XS(XS_Internals_SvREFCNT) /* This is dangerous stuff. */
29569577
JH
775{
776 dXSARGS;
777 SV *sv = SvRV(ST(0));
778 if (items == 1)
14a976d6 779 XSRETURN_IV(SvREFCNT(sv) - 1); /* Minus the ref created for us. */
29569577 780 else if (items == 2) {
14a976d6 781 /* I hope you really know what you are doing. */
29569577
JH
782 SvREFCNT(sv) = SvIV(ST(1));
783 XSRETURN_IV(SvREFCNT(sv));
784 }
14a976d6 785 XSRETURN_UNDEF; /* Can't happen. */
29569577
JH
786}
787
f044d0d1 788XS(XS_Internals_hv_clear_placehold)
dfd4ef2f
NC
789{
790 dXSARGS;
791 HV *hv = (HV *) SvRV(ST(0));
3540d4ce
AB
792 if (items != 1)
793 Perl_croak(aTHX_ "Usage: UNIVERSAL::hv_clear_placeholders(hv)");
794 hv_clear_placeholders(hv);
dfd4ef2f
NC
795 XSRETURN(0);
796}
39f7a870 797
39cff0d9
AE
798XS(XS_Regexp_DESTROY)
799{
800
801}
802
39f7a870
JH
803XS(XS_PerlIO_get_layers)
804{
805 dXSARGS;
806 if (items < 1 || items % 2 == 0)
807 Perl_croak(aTHX_ "Usage: PerlIO_get_layers(filehandle[,args])");
5fef3b4a 808#ifdef USE_PERLIO
39f7a870
JH
809 {
810 SV * sv;
811 GV * gv;
812 IO * io;
813 bool input = TRUE;
814 bool details = FALSE;
815
816 if (items > 1) {
39f7a870
JH
817 SV **svp;
818
819 for (svp = MARK + 2; svp <= SP; svp += 2) {
820 SV **varp = svp;
821 SV **valp = svp + 1;
822 STRLEN klen;
823 char *key = SvPV(*varp, klen);
824
825 switch (*key) {
826 case 'i':
827 if (klen == 5 && memEQ(key, "input", 5)) {
828 input = SvTRUE(*valp);
829 break;
830 }
831 goto fail;
832 case 'o':
833 if (klen == 6 && memEQ(key, "output", 6)) {
834 input = !SvTRUE(*valp);
835 break;
836 }
837 goto fail;
838 case 'd':
839 if (klen == 7 && memEQ(key, "details", 7)) {
840 details = SvTRUE(*valp);
841 break;
842 }
843 goto fail;
844 default:
845 fail:
846 Perl_croak(aTHX_
847 "get_layers: unknown argument '%s'",
848 key);
849 }
850 }
851
852 SP -= (items - 1);
853 }
854
855 sv = POPs;
856 gv = (GV*)sv;
857
858 if (!isGV(sv)) {
859 if (SvROK(sv) && isGV(SvRV(sv)))
860 gv = (GV*)SvRV(sv);
861 else
7a5fd60d 862 gv = gv_fetchsv(sv, FALSE, SVt_PVIO);
39f7a870
JH
863 }
864
865 if (gv && (io = GvIO(gv))) {
866 dTARGET;
867 AV* av = PerlIO_get_layers(aTHX_ input ?
868 IoIFP(io) : IoOFP(io));
869 I32 i;
870 I32 last = av_len(av);
871 I32 nitem = 0;
872
873 for (i = last; i >= 0; i -= 3) {
874 SV **namsvp;
875 SV **argsvp;
876 SV **flgsvp;
877 bool namok, argok, flgok;
878
879 namsvp = av_fetch(av, i - 2, FALSE);
880 argsvp = av_fetch(av, i - 1, FALSE);
881 flgsvp = av_fetch(av, i, FALSE);
882
883 namok = namsvp && *namsvp && SvPOK(*namsvp);
884 argok = argsvp && *argsvp && SvPOK(*argsvp);
885 flgok = flgsvp && *flgsvp && SvIOK(*flgsvp);
886
887 if (details) {
888 XPUSHs(namok ?
889 newSVpv(SvPVX(*namsvp), 0) : &PL_sv_undef);
890 XPUSHs(argok ?
891 newSVpv(SvPVX(*argsvp), 0) : &PL_sv_undef);
892 if (flgok)
893 XPUSHi(SvIVX(*flgsvp));
894 else
895 XPUSHs(&PL_sv_undef);
896 nitem += 3;
897 }
898 else {
899 if (namok && argok)
900 XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf"(%"SVf")",
901 *namsvp, *argsvp));
902 else if (namok)
903 XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf, *namsvp));
904 else
905 XPUSHs(&PL_sv_undef);
906 nitem++;
907 if (flgok) {
908 IV flags = SvIVX(*flgsvp);
909
910 if (flags & PERLIO_F_UTF8) {
911 XPUSHs(newSVpvn("utf8", 4));
912 nitem++;
913 }
914 }
915 }
916 }
917
918 SvREFCNT_dec(av);
919
920 XSRETURN(nitem);
921 }
922 }
5fef3b4a 923#endif
39f7a870
JH
924
925 XSRETURN(0);
926}
927
9a7034eb 928XS(XS_Internals_hash_seed)
c910b28a 929{
c85d3f85
NC
930 /* Using dXSARGS would also have dITEM and dSP,
931 * which define 2 unused local variables. */
932 dMARK; dAX;
81eaca17 933 XSRETURN_UV(PERL_HASH_SEED);
c910b28a
JH
934}
935
008fb0c0 936XS(XS_Internals_rehash_seed)
8e90d776
NC
937{
938 /* Using dXSARGS would also have dITEM and dSP,
939 * which define 2 unused local variables. */
940 dMARK; dAX;
008fb0c0 941 XSRETURN_UV(PL_rehash_seed);
8e90d776
NC
942}
943
05619474
NC
944XS(XS_Internals_HvREHASH) /* Subject to change */
945{
946 dXSARGS;
947 if (SvROK(ST(0))) {
948 HV *hv = (HV *) SvRV(ST(0));
949 if (items == 1 && SvTYPE(hv) == SVt_PVHV) {
950 if (HvREHASH(hv))
951 XSRETURN_YES;
952 else
953 XSRETURN_NO;
954 }
955 }
956 Perl_croak(aTHX_ "Internals::HvREHASH $hashref");
957}