Cope with completely empty lines within the Changes file.
[p5sagit/p5-mst-13.2.git] / universal.c
1 /*    universal.c
2  *
3  *    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4  *    2005, by Larry Wall and others
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
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
17 /* This file contains the code that implements the functions in Perl's
18  * UNIVERSAL package, such as UNIVERSAL->can().
19  */
20
21 #include "EXTERN.h"
22 #define PERL_IN_UNIVERSAL_C
23 #include "perl.h"
24
25 #ifdef USE_PERLIO
26 #include "perliol.h" /* For the PERLIO_F_XXX */
27 #endif
28
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
34 STATIC SV *
35 S_isa_lookup(pTHX_ HV *stash, const char *name, HV* name_stash,
36              int len, int level)
37 {
38     AV* av;
39     GV* gv;
40     GV** gvp;
41     HV* hv = Nullhv;
42     SV* subgen = Nullsv;
43     const char *hvname;
44
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;
49
50     hvname = HvNAME_get(stash);
51
52     if (strEQ(hvname, name))
53         return &PL_sv_yes;
54
55     if (strEQ(name, "UNIVERSAL"))
56         return &PL_sv_yes;
57
58     if (level > 100)
59         Perl_croak(aTHX_ "Recursive inheritance detected in package '%s'",
60                    hvname);
61
62     gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, FALSE);
63
64     if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (subgen = GvSV(gv))
65         && (hv = GvHV(gv)))
66     {
67         if (SvIV(subgen) == (IV)PL_sub_generation) {
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",
72                                   name, hvname) );
73                 return sv;
74             }
75         }
76         else {
77             DEBUG_o( Perl_deb(aTHX_ "ISA Cache in package %s is stale\n",
78                               hvname) );
79             hv_clear(hv);
80             sv_setiv(subgen, PL_sub_generation);
81         }
82     }
83
84     gvp = (GV**)hv_fetch(stash,"ISA",3,FALSE);
85
86     if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (av = GvAV(gv))) {
87         if (!hv || !subgen) {
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
95             if (!hv)
96                 hv = GvHVn(gv);
97             if (!subgen) {
98                 subgen = newSViv(PL_sub_generation);
99                 GvSV(gv) = subgen;
100             }
101         }
102         if (hv) {
103             SV** svp = AvARRAY(av);
104             /* NOTE: No support for tied ISA */
105             I32 items = AvFILLp(av) + 1;
106             while (items--) {
107                 SV* sv = *svp++;
108                 HV* basestash = gv_stashsv(sv, FALSE);
109                 if (!basestash) {
110                     if (ckWARN(WARN_MISC))
111                         Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
112                                     "Can't locate package %"SVf" for @%s::ISA",
113                                     sv, hvname);
114                     continue;
115                 }
116                 if (&PL_sv_yes == isa_lookup(basestash, name, name_stash, 
117                                              len, level + 1)) {
118                     (void)hv_store(hv,name,len,&PL_sv_yes,0);
119                     return &PL_sv_yes;
120                 }
121             }
122             (void)hv_store(hv,name,len,&PL_sv_no,0);
123         }
124     }
125     return &PL_sv_no;
126 }
127
128 /*
129 =head1 SV Manipulation Functions
130
131 =for apidoc sv_derived_from
132
133 Returns a boolean indicating whether the SV is derived from the specified
134 class.  This is the function that implements C<UNIVERSAL::isa>.  It works
135 for class names as well as for objects.
136
137 =cut
138 */
139
140 bool
141 Perl_sv_derived_from(pTHX_ SV *sv, const char *name)
142 {
143     const char *type = Nullch;
144     HV *stash = Nullhv;
145     HV *name_stash;
146
147     SvGETMAGIC(sv);
148
149     if (SvROK(sv)) {
150         sv = SvRV(sv);
151         type = sv_reftype(sv,0);
152         if (SvOBJECT(sv))
153             stash = SvSTASH(sv);
154     }
155     else {
156         stash = gv_stashsv(sv, FALSE);
157     }
158
159     name_stash = gv_stashpv(name, FALSE);
160
161     return (type && strEQ(type,name)) ||
162             (stash && isa_lookup(stash, name, name_stash, strlen(name), 0) 
163              == &PL_sv_yes)
164         ? TRUE
165         : FALSE ;
166 }
167
168 #include "XSUB.h"
169
170 PERL_XS_EXPORT_C void XS_UNIVERSAL_isa(pTHX_ CV *cv);
171 PERL_XS_EXPORT_C void XS_UNIVERSAL_can(pTHX_ CV *cv);
172 PERL_XS_EXPORT_C void XS_UNIVERSAL_VERSION(pTHX_ CV *cv);
173 XS(XS_version_new);
174 XS(XS_version_stringify);
175 XS(XS_version_numify);
176 XS(XS_version_normal);
177 XS(XS_version_vcmp);
178 XS(XS_version_boolean);
179 #ifdef HASATTRIBUTE_NORETURN
180 XS(XS_version_noop) __attribute__noreturn__;
181 #else
182 XS(XS_version_noop);
183 #endif
184 XS(XS_version_is_alpha);
185 XS(XS_version_qv);
186 XS(XS_utf8_is_utf8);
187 XS(XS_utf8_valid);
188 XS(XS_utf8_encode);
189 XS(XS_utf8_decode);
190 XS(XS_utf8_upgrade);
191 XS(XS_utf8_downgrade);
192 XS(XS_utf8_unicode_to_native);
193 XS(XS_utf8_native_to_unicode);
194 XS(XS_Internals_SvREADONLY);
195 XS(XS_Internals_SvREFCNT);
196 XS(XS_Internals_hv_clear_placehold);
197 XS(XS_PerlIO_get_layers);
198 XS(XS_Regexp_DESTROY);
199 XS(XS_Internals_hash_seed);
200 XS(XS_Internals_rehash_seed);
201 XS(XS_Internals_HvREHASH);
202
203 void
204 Perl_boot_core_UNIVERSAL(pTHX)
205 {
206     const char file[] = __FILE__;
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);
211     {
212         /* register the overloading (type 'A') magic */
213         PL_amagic_generation++;
214         /* Make it findable via fetchmethod */
215         newXS("version::()", XS_version_noop, file);
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);
221         newXS("version::normal", XS_version_normal, file);
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);
229         newXS("version::is_alpha", XS_version_is_alpha, file);
230         newXS("version::qv", XS_version_qv, file);
231     }
232     newXS("utf8::is_utf8", XS_utf8_is_utf8, file);
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);
240     newXSproto("Internals::SvREADONLY",XS_Internals_SvREADONLY, file, "\\[$%@];$");
241     newXSproto("Internals::SvREFCNT",XS_Internals_SvREFCNT, file, "\\[$%@];$");
242     newXSproto("Internals::hv_clear_placeholders",
243                XS_Internals_hv_clear_placehold, file, "\\%");
244     newXSproto("PerlIO::get_layers",
245                XS_PerlIO_get_layers, file, "*;@");
246     newXS("Regexp::DESTROY", XS_Regexp_DESTROY, file);
247     newXSproto("Internals::hash_seed",XS_Internals_hash_seed, file, "");
248     newXSproto("Internals::rehash_seed",XS_Internals_rehash_seed, file, "");
249     newXSproto("Internals::HvREHASH", XS_Internals_HvREHASH, file, "\\%");
250 }
251
252
253 XS(XS_UNIVERSAL_isa)
254 {
255     dXSARGS;
256     SV *sv;
257     const char *name;
258
259     if (items != 2)
260         Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)");
261
262     sv = ST(0);
263
264     SvGETMAGIC(sv);
265
266     if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
267                 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
268         XSRETURN_UNDEF;
269
270     name = SvPV_nolen_const(ST(1));
271
272     ST(0) = boolSV(sv_derived_from(sv, name));
273     XSRETURN(1);
274 }
275
276 XS(XS_UNIVERSAL_can)
277 {
278     dXSARGS;
279     SV   *sv;
280     const char *name;
281     SV   *rv;
282     HV   *pkg = NULL;
283
284     if (items != 2)
285         Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)");
286
287     sv = ST(0);
288
289     SvGETMAGIC(sv);
290
291     if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
292                 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
293         XSRETURN_UNDEF;
294
295     name = SvPV_nolen_const(ST(1));
296     rv = &PL_sv_undef;
297
298     if (SvROK(sv)) {
299         sv = (SV*)SvRV(sv);
300         if (SvOBJECT(sv))
301             pkg = SvSTASH(sv);
302     }
303     else {
304         pkg = gv_stashsv(sv, FALSE);
305     }
306
307     if (pkg) {
308         GV *gv = gv_fetchmethod_autoload(pkg, name, FALSE);
309         if (gv && isGV(gv))
310             rv = sv_2mortal(newRV((SV*)GvCV(gv)));
311     }
312
313     ST(0) = rv;
314     XSRETURN(1);
315 }
316
317 XS(XS_UNIVERSAL_VERSION)
318 {
319     dXSARGS;
320     HV *pkg;
321     GV **gvp;
322     GV *gv;
323     SV *sv;
324     const char *undef;
325
326     if (SvROK(ST(0))) {
327         sv = (SV*)SvRV(ST(0));
328         if (!SvOBJECT(sv))
329             Perl_croak(aTHX_ "Cannot find version of an unblessed reference");
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
338     if (gvp && isGV(gv = *gvp) && (sv = GvSV(gv)) && SvOK(sv)) {
339         SV *nsv = sv_newmortal();
340         sv_setsv(nsv, sv);
341         sv = nsv;
342         if ( !sv_derived_from(sv, "version"))
343             upg_version(sv);
344         undef = Nullch;
345     }
346     else {
347         sv = (SV*)&PL_sv_undef;
348         undef = "(undef)";
349     }
350
351     if (items > 1) {
352         SV *req = ST(1);
353
354         if (undef) {
355             if (pkg) {
356                 const char *name = HvNAME_get(pkg);
357                 Perl_croak(aTHX_
358                            "%s does not define $%s::VERSION--version check failed",
359                            name, name);
360             } else {
361                 Perl_croak(aTHX_
362                              "%s defines neither package nor VERSION--version check failed",
363                              SvPVx_nolen_const(ST(0)) );
364              }
365         }
366
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         }
374
375         if ( vcmp( req, sv ) > 0 )
376             Perl_croak(aTHX_ "%s version %"SVf" (%"SVf") required--"
377                     "this is only version %"SVf" (%"SVf")", HvNAME_get(pkg),
378                     vnumify(req),vnormal(req),vnumify(sv),vnormal(sv));
379     }
380
381     if ( SvOK(sv) && sv_derived_from(sv, "version") ) {
382         ST(0) = vnumify(sv);
383     } else {
384         ST(0) = sv;
385     }
386
387     XSRETURN(1);
388 }
389
390 XS(XS_version_new)
391 {
392     dXSARGS;
393     if (items > 3)
394         Perl_croak(aTHX_ "Usage: version::new(class, version)");
395     SP -= items;
396     {
397         SV *vs = ST(1);
398         SV *rv;
399         const char *classname;
400
401         /* get the class if called as an object method */
402         if ( sv_isobject(ST(0)) ) {
403             classname = HvNAME(SvSTASH(SvRV(ST(0))));
404         }
405         else {
406             classname = (char *)SvPV_nolen(ST(0));
407         }
408
409         if ( items == 1 ) {
410             /* no parameter provided */
411             if ( sv_isobject(ST(0)) ) {
412                 /* copy existing object */
413                 vs = ST(0);
414             }
415             else {
416                 /* create empty object */
417                 vs = sv_newmortal();
418                 sv_setpvn(vs,"",0);
419             }
420         }
421         else if ( items == 3 ) {
422             vs = sv_newmortal();
423             Perl_sv_setpvf(aTHX_ vs,"v%s",SvPV_nolen_const(ST(2)));
424         }
425
426         rv = new_version(vs);
427         if ( strcmp(classname,"version") != 0 ) /* inherited new() */
428             sv_bless(rv, gv_stashpv(classname,TRUE));
429
430         PUSHs(sv_2mortal(rv));
431         PUTBACK;
432         return;
433     }
434 }
435
436 XS(XS_version_stringify)
437 {
438      dXSARGS;
439      if (items < 1)
440           Perl_croak(aTHX_ "Usage: version::stringify(lobj, ...)");
441      SP -= items;
442      {
443           SV *  lobj = Nullsv;
444
445           if (sv_derived_from(ST(0), "version")) {
446                lobj = SvRV(ST(0));
447           }
448           else
449                Perl_croak(aTHX_ "lobj is not of type version");
450
451           PUSHs(sv_2mortal(vstringify(lobj)));
452
453           PUTBACK;
454           return;
455      }
456 }
457
458 XS(XS_version_numify)
459 {
460      dXSARGS;
461      if (items < 1)
462           Perl_croak(aTHX_ "Usage: version::numify(lobj, ...)");
463      SP -= items;
464      {
465           SV *  lobj = Nullsv;
466
467           if (sv_derived_from(ST(0), "version")) {
468                lobj = SvRV(ST(0));
469           }
470           else
471                Perl_croak(aTHX_ "lobj is not of type version");
472
473           PUSHs(sv_2mortal(vnumify(lobj)));
474
475           PUTBACK;
476           return;
477      }
478 }
479
480 XS(XS_version_normal)
481 {
482      dXSARGS;
483      if (items < 1)
484           Perl_croak(aTHX_ "Usage: version::normal(lobj, ...)");
485      SP -= items;
486      {
487           SV *  lobj = Nullsv;
488
489           if (sv_derived_from(ST(0), "version")) {
490                lobj = SvRV(ST(0));
491           }
492           else
493                Perl_croak(aTHX_ "lobj is not of type version");
494
495           PUSHs(sv_2mortal(vnormal(lobj)));
496
497           PUTBACK;
498           return;
499      }
500 }
501
502 XS(XS_version_vcmp)
503 {
504      dXSARGS;
505      if (items < 1)
506           Perl_croak(aTHX_ "Usage: version::vcmp(lobj, ...)");
507      SP -= items;
508      {
509           SV *  lobj = Nullsv;
510
511           if (sv_derived_from(ST(0), "version")) {
512                lobj = SvRV(ST(0));
513           }
514           else
515                Perl_croak(aTHX_ "lobj is not of type version");
516
517           {
518                SV       *rs;
519                SV       *rvs;
520                SV * robj = ST(1);
521                IV        swap = (IV)SvIV(ST(2));
522
523                if ( ! sv_derived_from(robj, "version") )
524                {
525                     robj = new_version(robj);
526                }
527                rvs = SvRV(robj);
528
529                if ( swap )
530                {
531                     rs = newSViv(vcmp(rvs,lobj));
532                }
533                else
534                {
535                     rs = newSViv(vcmp(lobj,rvs));
536                }
537
538                PUSHs(sv_2mortal(rs));
539           }
540
541           PUTBACK;
542           return;
543      }
544 }
545
546 XS(XS_version_boolean)
547 {
548      dXSARGS;
549      if (items < 1)
550           Perl_croak(aTHX_ "Usage: version::boolean(lobj, ...)");
551      SP -= items;
552      {
553           SV *  lobj = Nullsv;
554
555           if (sv_derived_from(ST(0), "version")) {
556                lobj = SvRV(ST(0));
557           }
558           else
559                Perl_croak(aTHX_ "lobj is not of type version");
560
561           {
562                SV       *rs;
563                rs = newSViv( vcmp(lobj,new_version(newSVpvn("0",1))) );
564                PUSHs(sv_2mortal(rs));
565           }
566
567           PUTBACK;
568           return;
569      }
570 }
571
572 XS(XS_version_noop)
573 {
574     dXSARGS;
575     if (items < 1)
576         Perl_croak(aTHX_ "Usage: version::noop(lobj, ...)");
577     if (sv_derived_from(ST(0), "version"))
578         Perl_croak(aTHX_ "operation not supported with version object");
579     else
580         Perl_croak(aTHX_ "lobj is not of type version");
581 #ifndef HASATTRIBUTE_NORETURN
582     XSRETURN_EMPTY;
583 #endif
584 }
585
586 XS(XS_version_is_alpha)
587 {
588     dXSARGS;
589     if (items != 1)
590         Perl_croak(aTHX_ "Usage: version::is_alpha(lobj)");
591     SP -= items;
592     {
593         SV * lobj = Nullsv;
594
595         if (sv_derived_from(ST(0), "version"))
596                 lobj = ST(0);
597         else
598                 Perl_croak(aTHX_ "lobj is not of type version");
599 {
600     if ( hv_exists((HV*)SvRV(lobj), "alpha", 5 ) )
601         XSRETURN_YES;
602     else
603         XSRETURN_NO;
604 }
605         PUTBACK;
606         return;
607     }
608 }
609
610 XS(XS_version_qv)
611 {
612     dXSARGS;
613     if (items != 1)
614         Perl_croak(aTHX_ "Usage: version::qv(ver)");
615     SP -= items;
616     {
617         SV *    ver = ST(0);
618         if ( !SvVOK(ver) ) /* only need to do with if not already v-string */
619         {
620             SV *vs = sv_newmortal();
621             char *version;
622             if ( SvNOK(ver) ) /* may get too much accuracy */
623             {
624                 char tbuf[64];
625                 sprintf(tbuf,"%.9"NVgf, SvNVX(ver));
626                 version = savepv(tbuf);
627             }
628             else
629             {
630                 version = savesvpv(ver);
631             }
632             (void)scan_version(version,vs,TRUE);
633             Safefree(version);
634
635             PUSHs(vs);
636         }
637         else
638         {
639             PUSHs(sv_2mortal(new_version(ver)));
640         }
641
642         PUTBACK;
643         return;
644     }
645 }
646
647 XS(XS_utf8_is_utf8)
648 {
649      dXSARGS;
650      if (items != 1)
651           Perl_croak(aTHX_ "Usage: utf8::is_utf8(sv)");
652      {
653           const SV *sv = ST(0);
654           {
655                if (SvUTF8(sv))
656                     XSRETURN_YES;
657                else
658                     XSRETURN_NO;
659           }
660      }
661      XSRETURN_EMPTY;
662 }
663
664 XS(XS_utf8_valid)
665 {
666      dXSARGS;
667      if (items != 1)
668           Perl_croak(aTHX_ "Usage: utf8::valid(sv)");
669      {
670           SV *  sv = ST(0);
671           {
672                STRLEN len;
673                const char *s = SvPV_const(sv,len);
674                if (!SvUTF8(sv) || is_utf8_string((const U8*)s,len))
675                     XSRETURN_YES;
676                else
677                     XSRETURN_NO;
678           }
679      }
680      XSRETURN_EMPTY;
681 }
682
683 XS(XS_utf8_encode)
684 {
685     dXSARGS;
686     if (items != 1)
687         Perl_croak(aTHX_ "Usage: utf8::encode(sv)");
688     {
689         SV *    sv = ST(0);
690
691         sv_utf8_encode(sv);
692     }
693     XSRETURN_EMPTY;
694 }
695
696 XS(XS_utf8_decode)
697 {
698     dXSARGS;
699     if (items != 1)
700         Perl_croak(aTHX_ "Usage: utf8::decode(sv)");
701     {
702         SV *    sv = ST(0);
703         const bool RETVAL = sv_utf8_decode(sv);
704         ST(0) = boolSV(RETVAL);
705         sv_2mortal(ST(0));
706     }
707     XSRETURN(1);
708 }
709
710 XS(XS_utf8_upgrade)
711 {
712     dXSARGS;
713     if (items != 1)
714         Perl_croak(aTHX_ "Usage: utf8::upgrade(sv)");
715     {
716         SV *    sv = ST(0);
717         STRLEN  RETVAL;
718         dXSTARG;
719
720         RETVAL = sv_utf8_upgrade(sv);
721         XSprePUSH; PUSHi((IV)RETVAL);
722     }
723     XSRETURN(1);
724 }
725
726 XS(XS_utf8_downgrade)
727 {
728     dXSARGS;
729     if (items < 1 || items > 2)
730         Perl_croak(aTHX_ "Usage: utf8::downgrade(sv, failok=0)");
731     {
732         SV *    sv = ST(0);
733         const bool failok = (items < 2) ? 0 : (int)SvIV(ST(1));
734         const bool RETVAL = sv_utf8_downgrade(sv, failok);
735
736         ST(0) = boolSV(RETVAL);
737         sv_2mortal(ST(0));
738     }
739     XSRETURN(1);
740 }
741
742 XS(XS_utf8_native_to_unicode)
743 {
744  dXSARGS;
745  const UV uv = SvUV(ST(0));
746
747  if (items > 1)
748      Perl_croak(aTHX_ "Usage: utf8::native_to_unicode(sv)");
749
750  ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv)));
751  XSRETURN(1);
752 }
753
754 XS(XS_utf8_unicode_to_native)
755 {
756  dXSARGS;
757  const UV uv = SvUV(ST(0));
758
759  if (items > 1)
760      Perl_croak(aTHX_ "Usage: utf8::unicode_to_native(sv)");
761
762  ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv)));
763  XSRETURN(1);
764 }
765
766 XS(XS_Internals_SvREADONLY)     /* This is dangerous stuff. */
767 {
768     dXSARGS;
769     SV *sv = SvRV(ST(0));
770
771     if (items == 1) {
772          if (SvREADONLY(sv))
773              XSRETURN_YES;
774          else
775              XSRETURN_NO;
776     }
777     else if (items == 2) {
778         if (SvTRUE(ST(1))) {
779             SvREADONLY_on(sv);
780             XSRETURN_YES;
781         }
782         else {
783             /* I hope you really know what you are doing. */
784             SvREADONLY_off(sv);
785             XSRETURN_NO;
786         }
787     }
788     XSRETURN_UNDEF; /* Can't happen. */
789 }
790
791 XS(XS_Internals_SvREFCNT)       /* This is dangerous stuff. */
792 {
793     dXSARGS;
794     SV *sv = SvRV(ST(0));
795
796     if (items == 1)
797          XSRETURN_IV(SvREFCNT(sv) - 1); /* Minus the ref created for us. */
798     else if (items == 2) {
799          /* I hope you really know what you are doing. */
800          SvREFCNT(sv) = SvIV(ST(1));
801          XSRETURN_IV(SvREFCNT(sv));
802     }
803     XSRETURN_UNDEF; /* Can't happen. */
804 }
805
806 XS(XS_Internals_hv_clear_placehold)
807 {
808     dXSARGS;
809     HV *hv = (HV *) SvRV(ST(0));
810
811     if (items != 1)
812         Perl_croak(aTHX_ "Usage: UNIVERSAL::hv_clear_placeholders(hv)");
813     hv_clear_placeholders(hv);
814     XSRETURN(0);
815 }
816
817 XS(XS_Regexp_DESTROY)
818 {
819     PERL_UNUSED_ARG(cv);
820 }
821
822 XS(XS_PerlIO_get_layers)
823 {
824     dXSARGS;
825     if (items < 1 || items % 2 == 0)
826         Perl_croak(aTHX_ "Usage: PerlIO_get_layers(filehandle[,args])");
827 #ifdef USE_PERLIO
828     {
829         SV *    sv;
830         GV *    gv;
831         IO *    io;
832         bool    input = TRUE;
833         bool    details = FALSE;
834
835         if (items > 1) {
836              SV **svp;
837              
838              for (svp = MARK + 2; svp <= SP; svp += 2) {
839                   SV **varp = svp;
840                   SV **valp = svp + 1;
841                   STRLEN klen;
842                   const char *key = SvPV_const(*varp, klen);
843
844                   switch (*key) {
845                   case 'i':
846                        if (klen == 5 && memEQ(key, "input", 5)) {
847                             input = SvTRUE(*valp);
848                             break;
849                        }
850                        goto fail;
851                   case 'o': 
852                        if (klen == 6 && memEQ(key, "output", 6)) {
853                             input = !SvTRUE(*valp);
854                             break;
855                        }
856                        goto fail;
857                   case 'd':
858                        if (klen == 7 && memEQ(key, "details", 7)) {
859                             details = SvTRUE(*valp);
860                             break;
861                        }
862                        goto fail;
863                   default:
864                   fail:
865                        Perl_croak(aTHX_
866                                   "get_layers: unknown argument '%s'",
867                                   key);
868                   }
869              }
870
871              SP -= (items - 1);
872         }
873
874         sv = POPs;
875         gv = (GV*)sv;
876
877         if (!isGV(sv)) {
878              if (SvROK(sv) && isGV(SvRV(sv)))
879                   gv = (GV*)SvRV(sv);
880              else
881                   gv = gv_fetchsv(sv, FALSE, SVt_PVIO);
882         }
883
884         if (gv && (io = GvIO(gv))) {
885              dTARGET;
886              AV* av = PerlIO_get_layers(aTHX_ input ?
887                                         IoIFP(io) : IoOFP(io));
888              I32 i;
889              I32 last = av_len(av);
890              I32 nitem = 0;
891              
892              for (i = last; i >= 0; i -= 3) {
893                   SV **namsvp;
894                   SV **argsvp;
895                   SV **flgsvp;
896                   bool namok, argok, flgok;
897
898                   namsvp = av_fetch(av, i - 2, FALSE);
899                   argsvp = av_fetch(av, i - 1, FALSE);
900                   flgsvp = av_fetch(av, i,     FALSE);
901
902                   namok = namsvp && *namsvp && SvPOK(*namsvp);
903                   argok = argsvp && *argsvp && SvPOK(*argsvp);
904                   flgok = flgsvp && *flgsvp && SvIOK(*flgsvp);
905
906                   if (details) {
907                        XPUSHs(namok
908                               ? newSVpvn(SvPVX_const(*namsvp), SvCUR(*namsvp))
909                               : &PL_sv_undef);
910                        XPUSHs(argok
911                               ? newSVpvn(SvPVX_const(*argsvp), SvCUR(*argsvp))
912                               : &PL_sv_undef);
913                        if (flgok)
914                             XPUSHi(SvIVX(*flgsvp));
915                        else
916                             XPUSHs(&PL_sv_undef);
917                        nitem += 3;
918                   }
919                   else {
920                        if (namok && argok)
921                             XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf"(%"SVf")",
922                                                *namsvp, *argsvp));
923                        else if (namok)
924                             XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf, *namsvp));
925                        else
926                             XPUSHs(&PL_sv_undef);
927                        nitem++;
928                        if (flgok) {
929                             IV flags = SvIVX(*flgsvp);
930
931                             if (flags & PERLIO_F_UTF8) {
932                                  XPUSHs(newSVpvn("utf8", 4));
933                                  nitem++;
934                             }
935                        }
936                   }
937              }
938
939              SvREFCNT_dec(av);
940
941              XSRETURN(nitem);
942         }
943     }
944 #endif
945
946     XSRETURN(0);
947 }
948
949 XS(XS_Internals_hash_seed)
950 {
951     /* Using dXSARGS would also have dITEM and dSP,
952      * which define 2 unused local variables.  */
953     dAXMARK;
954     PERL_UNUSED_ARG(cv);
955     PERL_UNUSED_VAR(mark);
956     XSRETURN_UV(PERL_HASH_SEED);
957 }
958
959 XS(XS_Internals_rehash_seed)
960 {
961     /* Using dXSARGS would also have dITEM and dSP,
962      * which define 2 unused local variables.  */
963     dAXMARK;
964     PERL_UNUSED_ARG(cv);
965     PERL_UNUSED_VAR(mark);
966     XSRETURN_UV(PL_rehash_seed);
967 }
968
969 XS(XS_Internals_HvREHASH)       /* Subject to change  */
970 {
971     dXSARGS;
972     if (SvROK(ST(0))) {
973         const HV *hv = (HV *) SvRV(ST(0));
974         if (items == 1 && SvTYPE(hv) == SVt_PVHV) {
975             if (HvREHASH(hv))
976                 XSRETURN_YES;
977             else
978                 XSRETURN_NO;
979         }
980     }
981     Perl_croak(aTHX_ "Internals::HvREHASH $hashref");
982 }
983
984 /*
985  * Local variables:
986  * c-indentation-style: bsd
987  * c-basic-offset: 4
988  * indent-tabs-mode: t
989  * End:
990  *
991  * ex: set ts=8 sts=4 sw=4 noet:
992  */