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
17 /* This file contains the code that implements the functions in Perl's
18 * UNIVERSAL package, such as UNIVERSAL->can().
22 #define PERL_IN_UNIVERSAL_C
26 #include "perliol.h" /* For the PERLIO_F_XXX */
30 * Contributed by Graham Barr <Graham.Barr@tiuk.ti.com>
31 * The main guts of traverse_isa was actually copied from gv_fetchmeth
35 S_isa_lookup(pTHX_ HV *stash, const char *name, HV* name_stash,
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))
49 if (strEQ(HvNAME(stash), name))
52 if (strEQ(name, "UNIVERSAL"))
56 Perl_croak(aTHX_ "Recursive inheritance detected in package '%s'",
59 gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, FALSE);
61 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (subgen = GvSV(gv))
64 if (SvIV(subgen) == (IV)PL_sub_generation) {
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)) );
74 DEBUG_o( Perl_deb(aTHX_ "ISA Cache in package %s is stale\n",
77 sv_setiv(subgen, PL_sub_generation);
81 gvp = (GV**)hv_fetch(stash,"ISA",3,FALSE);
83 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (av = GvAV(gv))) {
85 gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, TRUE);
89 if (SvTYPE(gv) != SVt_PVGV)
90 gv_init(gv, stash, "::ISA::CACHE::", 14, TRUE);
95 subgen = newSViv(PL_sub_generation);
100 SV** svp = AvARRAY(av);
101 /* NOTE: No support for tied ISA */
102 I32 items = AvFILLp(av) + 1;
105 HV* basestash = gv_stashsv(sv, FALSE);
107 if (ckWARN(WARN_MISC))
108 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
109 "Can't locate package %"SVf" for @%s::ISA",
113 if (&PL_sv_yes == isa_lookup(basestash, name, name_stash,
115 (void)hv_store(hv,name,len,&PL_sv_yes,0);
119 (void)hv_store(hv,name,len,&PL_sv_no,0);
126 =head1 SV Manipulation Functions
128 =for apidoc sv_derived_from
130 Returns a boolean indicating whether the SV is derived from the specified
131 class. This is the function that implements C<UNIVERSAL::isa>. It works
132 for class names as well as for objects.
138 Perl_sv_derived_from(pTHX_ SV *sv, const char *name)
152 type = sv_reftype(sv,0);
157 stash = gv_stashsv(sv, FALSE);
160 name_stash = gv_stashpv(name, FALSE);
162 return (type && strEQ(type,name)) ||
163 (stash && isa_lookup(stash, name, name_stash, strlen(name), 0)
171 void XS_UNIVERSAL_isa(pTHX_ CV *cv);
172 void XS_UNIVERSAL_can(pTHX_ CV *cv);
173 void XS_UNIVERSAL_VERSION(pTHX_ CV *cv);
175 XS(XS_version_stringify);
176 XS(XS_version_numify);
178 XS(XS_version_boolean);
180 XS(XS_version_is_alpha);
187 XS(XS_utf8_downgrade);
188 XS(XS_utf8_unicode_to_native);
189 XS(XS_utf8_native_to_unicode);
190 XS(XS_Internals_SvREADONLY);
191 XS(XS_Internals_SvREFCNT);
192 XS(XS_Internals_hv_clear_placehold);
193 XS(XS_PerlIO_get_layers);
194 XS(XS_Regexp_DESTROY);
195 XS(XS_Internals_hash_seed);
196 XS(XS_Internals_rehash_seed);
197 XS(XS_Internals_HvREHASH);
200 Perl_boot_core_UNIVERSAL(pTHX)
202 char *file = __FILE__;
204 newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file);
205 newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file);
206 newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file);
208 /* register the overloading (type 'A') magic */
209 PL_amagic_generation++;
210 /* Make it findable via fetchmethod */
211 newXS("version::()", XS_version_noop, file);
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);
224 newXS("version::is_alpha", XS_version_is_alpha, file);
225 newXS("version::qv", XS_version_qv, file);
227 newXS("utf8::is_utf8", XS_utf8_is_utf8, file);
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);
235 newXSproto("Internals::SvREADONLY",XS_Internals_SvREADONLY, file, "\\[$%@];$");
236 newXSproto("Internals::SvREFCNT",XS_Internals_SvREFCNT, file, "\\[$%@];$");
237 newXSproto("Internals::hv_clear_placeholders",
238 XS_Internals_hv_clear_placehold, file, "\\%");
239 newXSproto("PerlIO::get_layers",
240 XS_PerlIO_get_layers, file, "*;@");
241 newXS("Regexp::DESTROY", XS_Regexp_DESTROY, file);
242 newXSproto("Internals::hash_seed",XS_Internals_hash_seed, file, "");
243 newXSproto("Internals::rehash_seed",XS_Internals_rehash_seed, file, "");
244 newXSproto("Internals::HvREHASH", XS_Internals_HvREHASH, file, "\\%");
256 Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)");
263 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
264 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
267 name = (char *)SvPV(ST(1),n_a);
269 ST(0) = boolSV(sv_derived_from(sv, name));
283 Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)");
290 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
291 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
294 name = (char *)SvPV(ST(1),n_a);
303 pkg = gv_stashsv(sv, FALSE);
307 GV *gv = gv_fetchmethod_autoload(pkg, name, FALSE);
309 rv = sv_2mortal(newRV((SV*)GvCV(gv)));
316 XS(XS_UNIVERSAL_VERSION)
326 sv = (SV*)SvRV(ST(0));
328 Perl_croak(aTHX_ "Cannot find version of an unblessed reference");
332 pkg = gv_stashsv(ST(0), FALSE);
335 gvp = pkg ? (GV**)hv_fetch(pkg,"VERSION",7,FALSE) : Null(GV**);
337 if (gvp && isGV(gv = *gvp) && SvOK(sv = GvSV(gv))) {
338 SV *nsv = sv_newmortal();
341 if ( !sv_derived_from(sv, "version"))
346 sv = (SV*)&PL_sv_undef;
357 "%s does not define $%s::VERSION--version check failed",
358 HvNAME(pkg), HvNAME(pkg));
360 char *str = SvPVx(ST(0), len);
363 "%s defines neither package nor VERSION--version check failed", str);
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();
375 if ( vcmp( req, sv ) > 0 )
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));
381 if ( SvOK(sv) && sv_derived_from(sv, "version") ) {
394 Perl_croak(aTHX_ "Usage: version::new(class, version)");
397 char * class = (char *)SvPV_nolen(ST(0));
403 Perl_sv_setpvf(aTHX_ vs,"v%s",SvPV_nolen(ST(2)));
406 rv = new_version(vs);
407 if ( strcmp(class,"version") != 0 ) /* inherited new() */
408 sv_bless(rv, gv_stashpv(class,TRUE));
410 PUSHs(sv_2mortal(rv));
416 XS(XS_version_stringify)
420 Perl_croak(aTHX_ "Usage: version::stringify(lobj, ...)");
425 if (sv_derived_from(ST(0), "version")) {
426 SV *tmp = SvRV(ST(0));
430 Perl_croak(aTHX_ "lobj is not of type version");
432 PUSHs(sv_2mortal(vstringify(lobj)));
439 XS(XS_version_numify)
443 Perl_croak(aTHX_ "Usage: version::numify(lobj, ...)");
448 if (sv_derived_from(ST(0), "version")) {
449 SV *tmp = SvRV(ST(0));
453 Perl_croak(aTHX_ "lobj is not of type version");
455 PUSHs(sv_2mortal(vnumify(lobj)));
466 Perl_croak(aTHX_ "Usage: version::vcmp(lobj, ...)");
471 if (sv_derived_from(ST(0), "version")) {
472 SV *tmp = SvRV(ST(0));
476 Perl_croak(aTHX_ "lobj is not of type version");
482 IV swap = (IV)SvIV(ST(2));
484 if ( ! sv_derived_from(robj, "version") )
486 robj = new_version(robj);
492 rs = newSViv(vcmp(rvs,lobj));
496 rs = newSViv(vcmp(lobj,rvs));
499 PUSHs(sv_2mortal(rs));
507 XS(XS_version_boolean)
511 Perl_croak(aTHX_ "Usage: version::boolean(lobj, ...)");
516 if (sv_derived_from(ST(0), "version")) {
517 SV *tmp = SvRV(ST(0));
521 Perl_croak(aTHX_ "lobj is not of type version");
525 rs = newSViv( vcmp(lobj,new_version(newSVpvn("0",1))) );
526 PUSHs(sv_2mortal(rs));
538 Perl_croak(aTHX_ "Usage: version::noop(lobj, ...)");
542 if (sv_derived_from(ST(0), "version")) {
543 SV *tmp = SvRV(ST(0));
547 Perl_croak(aTHX_ "lobj is not of type version");
550 Perl_croak(aTHX_ "operation not supported with version object");
557 XS(XS_version_is_alpha)
561 Perl_croak(aTHX_ "Usage: version::is_alpha(lobj)");
566 if (sv_derived_from(ST(0), "version")) {
567 SV *tmp = SvRV(ST(0));
571 Perl_croak(aTHX_ "lobj is not of type version");
573 I32 len = av_len((AV *)lobj);
574 I32 digit = SvIVX(*av_fetch((AV *)lobj, len, 0));
589 Perl_croak(aTHX_ "Usage: version::qv(ver)");
593 if ( !SvVOK(ver) ) /* only need to do with if not already v-string */
595 SV *vs = sv_newmortal();
597 if ( SvNOK(ver) ) /* may get too much accuracy */
600 sprintf(tbuf,"%.9"NVgf, SvNVX(ver));
601 version = savepv(tbuf);
605 version = savepv(SvPV_nolen(ver));
607 (void)scan_version(version,vs,TRUE);
614 PUSHs(sv_2mortal(new_version(ver)));
626 Perl_croak(aTHX_ "Usage: utf8::is_utf8(sv)");
643 Perl_croak(aTHX_ "Usage: utf8::valid(sv)");
648 char *s = SvPV(sv,len);
649 if (!SvUTF8(sv) || is_utf8_string((U8*)s,len))
662 Perl_croak(aTHX_ "Usage: utf8::encode(sv)");
675 Perl_croak(aTHX_ "Usage: utf8::decode(sv)");
680 RETVAL = sv_utf8_decode(sv);
681 ST(0) = boolSV(RETVAL);
691 Perl_croak(aTHX_ "Usage: utf8::upgrade(sv)");
697 RETVAL = sv_utf8_upgrade(sv);
698 XSprePUSH; PUSHi((IV)RETVAL);
703 XS(XS_utf8_downgrade)
706 if (items < 1 || items > 2)
707 Perl_croak(aTHX_ "Usage: utf8::downgrade(sv, failok=0)");
716 failok = (int)SvIV(ST(1));
719 RETVAL = sv_utf8_downgrade(sv, failok);
720 ST(0) = boolSV(RETVAL);
726 XS(XS_utf8_native_to_unicode)
732 Perl_croak(aTHX_ "Usage: utf8::native_to_unicode(sv)");
734 ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv)));
738 XS(XS_utf8_unicode_to_native)
744 Perl_croak(aTHX_ "Usage: utf8::unicode_to_native(sv)");
746 ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv)));
750 XS(XS_Internals_SvREADONLY) /* This is dangerous stuff. */
753 SV *sv = SvRV(ST(0));
760 else if (items == 2) {
766 /* I hope you really know what you are doing. */
771 XSRETURN_UNDEF; /* Can't happen. */
774 XS(XS_Internals_SvREFCNT) /* This is dangerous stuff. */
777 SV *sv = SvRV(ST(0));
779 XSRETURN_IV(SvREFCNT(sv) - 1); /* Minus the ref created for us. */
780 else if (items == 2) {
781 /* I hope you really know what you are doing. */
782 SvREFCNT(sv) = SvIV(ST(1));
783 XSRETURN_IV(SvREFCNT(sv));
785 XSRETURN_UNDEF; /* Can't happen. */
788 XS(XS_Internals_hv_clear_placehold)
791 HV *hv = (HV *) SvRV(ST(0));
793 Perl_croak(aTHX_ "Usage: UNIVERSAL::hv_clear_placeholders(hv)");
794 hv_clear_placeholders(hv);
798 XS(XS_Regexp_DESTROY)
803 XS(XS_PerlIO_get_layers)
806 if (items < 1 || items % 2 == 0)
807 Perl_croak(aTHX_ "Usage: PerlIO_get_layers(filehandle[,args])");
814 bool details = FALSE;
819 for (svp = MARK + 2; svp <= SP; svp += 2) {
823 char *key = SvPV(*varp, klen);
827 if (klen == 5 && memEQ(key, "input", 5)) {
828 input = SvTRUE(*valp);
833 if (klen == 6 && memEQ(key, "output", 6)) {
834 input = !SvTRUE(*valp);
839 if (klen == 7 && memEQ(key, "details", 7)) {
840 details = SvTRUE(*valp);
847 "get_layers: unknown argument '%s'",
859 if (SvROK(sv) && isGV(SvRV(sv)))
862 gv = gv_fetchsv(sv, FALSE, SVt_PVIO);
865 if (gv && (io = GvIO(gv))) {
867 AV* av = PerlIO_get_layers(aTHX_ input ?
868 IoIFP(io) : IoOFP(io));
870 I32 last = av_len(av);
873 for (i = last; i >= 0; i -= 3) {
877 bool namok, argok, flgok;
879 namsvp = av_fetch(av, i - 2, FALSE);
880 argsvp = av_fetch(av, i - 1, FALSE);
881 flgsvp = av_fetch(av, i, FALSE);
883 namok = namsvp && *namsvp && SvPOK(*namsvp);
884 argok = argsvp && *argsvp && SvPOK(*argsvp);
885 flgok = flgsvp && *flgsvp && SvIOK(*flgsvp);
889 newSVpv(SvPVX(*namsvp), 0) : &PL_sv_undef);
891 newSVpv(SvPVX(*argsvp), 0) : &PL_sv_undef);
893 XPUSHi(SvIVX(*flgsvp));
895 XPUSHs(&PL_sv_undef);
900 XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf"(%"SVf")",
903 XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf, *namsvp));
905 XPUSHs(&PL_sv_undef);
908 IV flags = SvIVX(*flgsvp);
910 if (flags & PERLIO_F_UTF8) {
911 XPUSHs(newSVpvn("utf8", 4));
928 XS(XS_Internals_hash_seed)
930 /* Using dXSARGS would also have dITEM and dSP,
931 * which define 2 unused local variables. */
933 XSRETURN_UV(PERL_HASH_SEED);
936 XS(XS_Internals_rehash_seed)
938 /* Using dXSARGS would also have dITEM and dSP,
939 * which define 2 unused local variables. */
941 XSRETURN_UV(PL_rehash_seed);
944 XS(XS_Internals_HvREHASH) /* Subject to change */
948 HV *hv = (HV *) SvRV(ST(0));
949 if (items == 1 && SvTYPE(hv) == SVt_PVHV) {
956 Perl_croak(aTHX_ "Internals::HvREHASH $hashref");