3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 * by Larry Wall and others
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.
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
18 #define PERL_IN_UNIVERSAL_C
22 #include "perliol.h" /* For the PERLIO_F_XXX */
26 * Contributed by Graham Barr <Graham.Barr@tiuk.ti.com>
27 * The main guts of traverse_isa was actually copied from gv_fetchmeth
31 S_isa_lookup(pTHX_ HV *stash, const char *name, HV* name_stash,
40 /* A stash/class can go by many names (ie. User == main::User), so
41 we compare the stash itself just in case */
42 if (name_stash && (stash == name_stash))
45 if (strEQ(HvNAME(stash), name))
48 if (strEQ(name, "UNIVERSAL"))
52 Perl_croak(aTHX_ "Recursive inheritance detected in package '%s'",
55 gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, FALSE);
57 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (subgen = GvSV(gv))
60 if (SvIV(subgen) == (IV)PL_sub_generation) {
62 SV** svp = (SV**)hv_fetch(hv, name, len, FALSE);
63 if (svp && (sv = *svp) != (SV*)&PL_sv_undef) {
64 DEBUG_o( Perl_deb(aTHX_ "Using cached ISA %s for package %s\n",
65 name, HvNAME(stash)) );
70 DEBUG_o( Perl_deb(aTHX_ "ISA Cache in package %s is stale\n",
73 sv_setiv(subgen, PL_sub_generation);
77 gvp = (GV**)hv_fetch(stash,"ISA",3,FALSE);
79 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (av = GvAV(gv))) {
81 gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, TRUE);
85 if (SvTYPE(gv) != SVt_PVGV)
86 gv_init(gv, stash, "::ISA::CACHE::", 14, TRUE);
91 subgen = newSViv(PL_sub_generation);
96 SV** svp = AvARRAY(av);
97 /* NOTE: No support for tied ISA */
98 I32 items = AvFILLp(av) + 1;
101 HV* basestash = gv_stashsv(sv, FALSE);
103 if (ckWARN(WARN_MISC))
104 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
105 "Can't locate package %"SVf" for @%s::ISA",
109 if (&PL_sv_yes == isa_lookup(basestash, name, name_stash,
111 (void)hv_store(hv,name,len,&PL_sv_yes,0);
115 (void)hv_store(hv,name,len,&PL_sv_no,0);
122 =head1 SV Manipulation Functions
124 =for apidoc sv_derived_from
126 Returns a boolean indicating whether the SV is derived from the specified
127 class. This is the function that implements C<UNIVERSAL::isa>. It works
128 for class names as well as for objects.
134 Perl_sv_derived_from(pTHX_ SV *sv, const char *name)
148 type = sv_reftype(sv,0);
153 stash = gv_stashsv(sv, FALSE);
156 name_stash = gv_stashpv(name, FALSE);
158 return (type && strEQ(type,name)) ||
159 (stash && isa_lookup(stash, name, name_stash, strlen(name), 0)
167 void XS_UNIVERSAL_isa(pTHX_ CV *cv);
168 void XS_UNIVERSAL_can(pTHX_ CV *cv);
169 void XS_UNIVERSAL_VERSION(pTHX_ CV *cv);
171 XS(XS_version_stringify);
172 XS(XS_version_numify);
174 XS(XS_version_boolean);
176 XS(XS_version_is_alpha);
183 XS(XS_utf8_downgrade);
184 XS(XS_utf8_unicode_to_native);
185 XS(XS_utf8_native_to_unicode);
186 XS(XS_Internals_SvREADONLY);
187 XS(XS_Internals_SvREFCNT);
188 XS(XS_Internals_hv_clear_placehold);
189 XS(XS_PerlIO_get_layers);
190 XS(XS_Regexp_DESTROY);
191 XS(XS_Internals_hash_seed);
192 XS(XS_Internals_rehash_seed);
193 XS(XS_Internals_HvREHASH);
196 Perl_boot_core_UNIVERSAL(pTHX)
198 char *file = __FILE__;
200 newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file);
201 newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file);
202 newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file);
204 /* register the overloading (type 'A') magic */
205 PL_amagic_generation++;
206 /* Make it findable via fetchmethod */
207 newXS("version::()", XS_version_noop, file);
208 newXS("version::new", XS_version_new, file);
209 newXS("version::(\"\"", XS_version_stringify, file);
210 newXS("version::stringify", XS_version_stringify, file);
211 newXS("version::(0+", XS_version_numify, file);
212 newXS("version::numify", XS_version_numify, file);
213 newXS("version::(cmp", XS_version_vcmp, file);
214 newXS("version::(<=>", XS_version_vcmp, file);
215 newXS("version::vcmp", XS_version_vcmp, file);
216 newXS("version::(bool", XS_version_boolean, file);
217 newXS("version::boolean", XS_version_boolean, file);
218 newXS("version::(nomethod", XS_version_noop, file);
219 newXS("version::noop", XS_version_noop, file);
220 newXS("version::is_alpha", XS_version_is_alpha, file);
221 newXS("version::qv", XS_version_qv, file);
223 newXS("utf8::is_utf8", XS_utf8_is_utf8, file);
224 newXS("utf8::valid", XS_utf8_valid, file);
225 newXS("utf8::encode", XS_utf8_encode, file);
226 newXS("utf8::decode", XS_utf8_decode, file);
227 newXS("utf8::upgrade", XS_utf8_upgrade, file);
228 newXS("utf8::downgrade", XS_utf8_downgrade, file);
229 newXS("utf8::native_to_unicode", XS_utf8_native_to_unicode, file);
230 newXS("utf8::unicode_to_native", XS_utf8_unicode_to_native, file);
231 newXSproto("Internals::SvREADONLY",XS_Internals_SvREADONLY, file, "\\[$%@];$");
232 newXSproto("Internals::SvREFCNT",XS_Internals_SvREFCNT, file, "\\[$%@];$");
233 newXSproto("Internals::hv_clear_placeholders",
234 XS_Internals_hv_clear_placehold, file, "\\%");
235 newXSproto("PerlIO::get_layers",
236 XS_PerlIO_get_layers, file, "*;@");
237 newXS("Regexp::DESTROY", XS_Regexp_DESTROY, file);
238 newXSproto("Internals::hash_seed",XS_Internals_hash_seed, file, "");
239 newXSproto("Internals::rehash_seed",XS_Internals_rehash_seed, file, "");
240 newXSproto("Internals::HvREHASH", XS_Internals_HvREHASH, file, "\\%");
252 Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)");
259 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
260 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
263 name = (char *)SvPV(ST(1),n_a);
265 ST(0) = boolSV(sv_derived_from(sv, name));
279 Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)");
286 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
287 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
290 name = (char *)SvPV(ST(1),n_a);
299 pkg = gv_stashsv(sv, FALSE);
303 GV *gv = gv_fetchmethod_autoload(pkg, name, FALSE);
305 rv = sv_2mortal(newRV((SV*)GvCV(gv)));
312 XS(XS_UNIVERSAL_VERSION)
322 sv = (SV*)SvRV(ST(0));
324 Perl_croak(aTHX_ "Cannot find version of an unblessed reference");
328 pkg = gv_stashsv(ST(0), FALSE);
331 gvp = pkg ? (GV**)hv_fetch(pkg,"VERSION",7,FALSE) : Null(GV**);
333 if (gvp && isGV(gv = *gvp) && SvOK(sv = GvSV(gv))) {
334 SV *nsv = sv_newmortal();
337 if ( !sv_derived_from(sv, "version"))
342 sv = (SV*)&PL_sv_undef;
353 "%s does not define $%s::VERSION--version check failed",
354 HvNAME(pkg), HvNAME(pkg));
356 char *str = SvPVx(ST(0), len);
359 "%s defines neither package nor VERSION--version check failed", str);
363 if ( !sv_derived_from(req, "version")) {
364 /* req may very well be R/O, so create a new object */
365 SV *nsv = sv_newmortal();
371 if ( vcmp( req, sv ) > 0 )
372 Perl_croak(aTHX_ "%s version %"SVf" (%"SVf") required--"
373 "this is only version %"SVf" (%"SVf")", HvNAME(pkg),
374 vnumify(req),vnormal(req),vnumify(sv),vnormal(sv));
377 if ( sv_derived_from(sv, "version") ) {
390 Perl_croak(aTHX_ "Usage: version::new(class, version)");
393 char * class = (char *)SvPV_nolen(ST(0));
399 Perl_sv_setpvf(aTHX_ vs,"v%s",SvPV_nolen(ST(2)));
402 rv = new_version(vs);
403 if ( strcmp(class,"version") != 0 ) /* inherited new() */
404 sv_bless(rv, gv_stashpv(class,TRUE));
406 PUSHs(sv_2mortal(rv));
412 XS(XS_version_stringify)
416 Perl_croak(aTHX_ "Usage: version::stringify(lobj, ...)");
421 if (sv_derived_from(ST(0), "version")) {
422 SV *tmp = SvRV(ST(0));
426 Perl_croak(aTHX_ "lobj is not of type version");
428 PUSHs(sv_2mortal(vstringify(lobj)));
435 XS(XS_version_numify)
439 Perl_croak(aTHX_ "Usage: version::numify(lobj, ...)");
444 if (sv_derived_from(ST(0), "version")) {
445 SV *tmp = SvRV(ST(0));
449 Perl_croak(aTHX_ "lobj is not of type version");
451 PUSHs(sv_2mortal(vnumify(lobj)));
462 Perl_croak(aTHX_ "Usage: version::vcmp(lobj, ...)");
467 if (sv_derived_from(ST(0), "version")) {
468 SV *tmp = SvRV(ST(0));
472 Perl_croak(aTHX_ "lobj is not of type version");
478 IV swap = (IV)SvIV(ST(2));
480 if ( ! sv_derived_from(robj, "version") )
482 robj = new_version(robj);
488 rs = newSViv(vcmp(rvs,lobj));
492 rs = newSViv(vcmp(lobj,rvs));
495 PUSHs(sv_2mortal(rs));
503 XS(XS_version_boolean)
507 Perl_croak(aTHX_ "Usage: version::boolean(lobj, ...)");
512 if (sv_derived_from(ST(0), "version")) {
513 SV *tmp = SvRV(ST(0));
517 Perl_croak(aTHX_ "lobj is not of type version");
521 rs = newSViv( vcmp(lobj,new_version(newSVpvn("0",1))) );
522 PUSHs(sv_2mortal(rs));
534 Perl_croak(aTHX_ "Usage: version::noop(lobj, ...)");
538 if (sv_derived_from(ST(0), "version")) {
539 SV *tmp = SvRV(ST(0));
543 Perl_croak(aTHX_ "lobj is not of type version");
546 Perl_croak(aTHX_ "operation not supported with version object");
553 XS(XS_version_is_alpha)
557 Perl_croak(aTHX_ "Usage: version::is_alpha(lobj)");
562 if (sv_derived_from(ST(0), "version")) {
563 SV *tmp = SvRV(ST(0));
567 Perl_croak(aTHX_ "lobj is not of type version");
569 I32 len = av_len((AV *)lobj);
570 I32 digit = SvIVX(*av_fetch((AV *)lobj, len, 0));
585 Perl_croak(aTHX_ "Usage: version::qv(ver)");
589 if ( !SvVOK(ver) ) /* only need to do with if not already v-string */
591 SV *vs = sv_newmortal();
593 if ( SvNOK(ver) ) /* may get too much accuracy */
596 sprintf(tbuf,"%.9"NVgf, SvNVX(ver));
597 version = savepv(tbuf);
601 version = savepv(SvPV_nolen(ver));
603 (void)scan_version(version,vs,TRUE);
610 PUSHs(sv_2mortal(new_version(ver)));
622 Perl_croak(aTHX_ "Usage: utf8::is_utf8(sv)");
639 Perl_croak(aTHX_ "Usage: utf8::valid(sv)");
644 char *s = SvPV(sv,len);
645 if (!SvUTF8(sv) || is_utf8_string((U8*)s,len))
658 Perl_croak(aTHX_ "Usage: utf8::encode(sv)");
671 Perl_croak(aTHX_ "Usage: utf8::decode(sv)");
676 RETVAL = sv_utf8_decode(sv);
677 ST(0) = boolSV(RETVAL);
687 Perl_croak(aTHX_ "Usage: utf8::upgrade(sv)");
693 RETVAL = sv_utf8_upgrade(sv);
694 XSprePUSH; PUSHi((IV)RETVAL);
699 XS(XS_utf8_downgrade)
702 if (items < 1 || items > 2)
703 Perl_croak(aTHX_ "Usage: utf8::downgrade(sv, failok=0)");
712 failok = (int)SvIV(ST(1));
715 RETVAL = sv_utf8_downgrade(sv, failok);
716 ST(0) = boolSV(RETVAL);
722 XS(XS_utf8_native_to_unicode)
728 Perl_croak(aTHX_ "Usage: utf8::native_to_unicode(sv)");
730 ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv)));
734 XS(XS_utf8_unicode_to_native)
740 Perl_croak(aTHX_ "Usage: utf8::unicode_to_native(sv)");
742 ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv)));
746 XS(XS_Internals_SvREADONLY) /* This is dangerous stuff. */
749 SV *sv = SvRV(ST(0));
756 else if (items == 2) {
762 /* I hope you really know what you are doing. */
767 XSRETURN_UNDEF; /* Can't happen. */
770 XS(XS_Internals_SvREFCNT) /* This is dangerous stuff. */
773 SV *sv = SvRV(ST(0));
775 XSRETURN_IV(SvREFCNT(sv) - 1); /* Minus the ref created for us. */
776 else if (items == 2) {
777 /* I hope you really know what you are doing. */
778 SvREFCNT(sv) = SvIV(ST(1));
779 XSRETURN_IV(SvREFCNT(sv));
781 XSRETURN_UNDEF; /* Can't happen. */
784 XS(XS_Internals_hv_clear_placehold)
787 HV *hv = (HV *) SvRV(ST(0));
789 Perl_croak(aTHX_ "Usage: UNIVERSAL::hv_clear_placeholders(hv)");
790 hv_clear_placeholders(hv);
794 XS(XS_Regexp_DESTROY)
799 XS(XS_PerlIO_get_layers)
802 if (items < 1 || items % 2 == 0)
803 Perl_croak(aTHX_ "Usage: PerlIO_get_layers(filehandle[,args])");
810 bool details = FALSE;
815 for (svp = MARK + 2; svp <= SP; svp += 2) {
819 char *key = SvPV(*varp, klen);
823 if (klen == 5 && memEQ(key, "input", 5)) {
824 input = SvTRUE(*valp);
829 if (klen == 6 && memEQ(key, "output", 6)) {
830 input = !SvTRUE(*valp);
835 if (klen == 7 && memEQ(key, "details", 7)) {
836 details = SvTRUE(*valp);
843 "get_layers: unknown argument '%s'",
855 if (SvROK(sv) && isGV(SvRV(sv)))
858 gv = gv_fetchpv(SvPVX(sv), FALSE, SVt_PVIO);
861 if (gv && (io = GvIO(gv))) {
863 AV* av = PerlIO_get_layers(aTHX_ input ?
864 IoIFP(io) : IoOFP(io));
866 I32 last = av_len(av);
869 for (i = last; i >= 0; i -= 3) {
873 bool namok, argok, flgok;
875 namsvp = av_fetch(av, i - 2, FALSE);
876 argsvp = av_fetch(av, i - 1, FALSE);
877 flgsvp = av_fetch(av, i, FALSE);
879 namok = namsvp && *namsvp && SvPOK(*namsvp);
880 argok = argsvp && *argsvp && SvPOK(*argsvp);
881 flgok = flgsvp && *flgsvp && SvIOK(*flgsvp);
885 newSVpv(SvPVX(*namsvp), 0) : &PL_sv_undef);
887 newSVpv(SvPVX(*argsvp), 0) : &PL_sv_undef);
889 XPUSHi(SvIVX(*flgsvp));
891 XPUSHs(&PL_sv_undef);
896 XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf"(%"SVf")",
899 XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf, *namsvp));
901 XPUSHs(&PL_sv_undef);
904 IV flags = SvIVX(*flgsvp);
906 if (flags & PERLIO_F_UTF8) {
907 XPUSHs(newSVpvn("utf8", 4));
924 XS(XS_Internals_hash_seed)
926 /* Using dXSARGS would also have dITEM and dSP,
927 * which define 2 unused local variables. */
929 XSRETURN_UV(PERL_HASH_SEED);
932 XS(XS_Internals_rehash_seed)
934 /* Using dXSARGS would also have dITEM and dSP,
935 * which define 2 unused local variables. */
937 XSRETURN_UV(PL_rehash_seed);
940 XS(XS_Internals_HvREHASH) /* Subject to change */
944 HV *hv = (HV *) SvRV(ST(0));
945 if (items == 1 && SvTYPE(hv) == SVt_PVHV) {
952 Perl_croak(aTHX_ "Internals::HvREHASH $hashref");