3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 * 2005, 2006, 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,
46 /* A stash/class can go by many names (ie. User == main::User), so
47 we compare the stash itself just in case */
48 if (name_stash && (stash == name_stash))
51 hvname = HvNAME_get(stash);
53 if (strEQ(hvname, name))
56 if (strEQ(name, "UNIVERSAL"))
60 Perl_croak(aTHX_ "Recursive inheritance detected in package '%s'",
63 gvp = (GV**)hv_fetchs(stash, "::ISA::CACHE::", FALSE);
65 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (subgen = GvSV(gv))
68 if (SvIV(subgen) == (IV)PL_sub_generation) {
70 SV** const svp = (SV**)hv_fetch(hv, name, len, FALSE);
71 if (svp && (sv = *svp) != (SV*)&PL_sv_undef) {
72 DEBUG_o( Perl_deb(aTHX_ "Using cached ISA %s for package %s\n",
78 DEBUG_o( Perl_deb(aTHX_ "ISA Cache in package %s is stale\n",
81 sv_setiv(subgen, PL_sub_generation);
85 gvp = (GV**)hv_fetchs(stash, "ISA", FALSE);
87 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (av = GvAV(gv))) {
89 gvp = (GV**)hv_fetchs(stash, "::ISA::CACHE::", TRUE);
93 if (SvTYPE(gv) != SVt_PVGV)
94 gv_init(gv, stash, "::ISA::CACHE::", 14, TRUE);
99 subgen = newSViv(PL_sub_generation);
104 SV** svp = AvARRAY(av);
105 /* NOTE: No support for tied ISA */
106 I32 items = AvFILLp(av) + 1;
108 SV* const sv = *svp++;
109 HV* const basestash = gv_stashsv(sv, FALSE);
111 if (ckWARN(WARN_MISC))
112 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
113 "Can't locate package %"SVf" for @%s::ISA",
117 if (&PL_sv_yes == isa_lookup(basestash, name, name_stash,
119 (void)hv_store(hv,name,len,&PL_sv_yes,0);
123 (void)hv_store(hv,name,len,&PL_sv_no,0);
130 =head1 SV Manipulation Functions
132 =for apidoc sv_derived_from
134 Returns a boolean indicating whether the SV is derived from the specified
135 class. This is the function that implements C<UNIVERSAL::isa>. It works
136 for class names as well as for objects.
142 Perl_sv_derived_from(pTHX_ SV *sv, const char *name)
152 type = sv_reftype(sv,0);
153 if (type && strEQ(type,name))
155 stash = SvOBJECT(sv) ? SvSTASH(sv) : NULL;
158 stash = gv_stashsv(sv, FALSE);
162 HV * const name_stash = gv_stashpv(name, FALSE);
163 return isa_lookup(stash, name, name_stash, strlen(name), 0) == &PL_sv_yes;
172 PERL_XS_EXPORT_C void XS_UNIVERSAL_isa(pTHX_ CV *cv);
173 PERL_XS_EXPORT_C void XS_UNIVERSAL_can(pTHX_ CV *cv);
174 PERL_XS_EXPORT_C void XS_UNIVERSAL_VERSION(pTHX_ CV *cv);
176 XS(XS_version_stringify);
177 XS(XS_version_numify);
178 XS(XS_version_normal);
180 XS(XS_version_boolean);
181 #ifdef HASATTRIBUTE_NORETURN
182 XS(XS_version_noop) __attribute__noreturn__;
186 XS(XS_version_is_alpha);
193 XS(XS_utf8_downgrade);
194 XS(XS_utf8_unicode_to_native);
195 XS(XS_utf8_native_to_unicode);
196 XS(XS_Internals_SvREADONLY);
197 XS(XS_Internals_SvREFCNT);
198 XS(XS_Internals_hv_clear_placehold);
199 XS(XS_PerlIO_get_layers);
200 XS(XS_Regexp_DESTROY);
201 XS(XS_Internals_hash_seed);
202 XS(XS_Internals_rehash_seed);
203 XS(XS_Internals_HvREHASH);
204 XS(XS_Internals_inc_sub_generation);
207 Perl_boot_core_UNIVERSAL(pTHX)
210 static const char file[] = __FILE__;
212 newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file);
213 newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file);
214 newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file);
216 /* register the overloading (type 'A') magic */
217 PL_amagic_generation++;
218 /* Make it findable via fetchmethod */
219 newXS("version::()", XS_version_noop, file);
220 newXS("version::new", XS_version_new, file);
221 newXS("version::(\"\"", XS_version_stringify, file);
222 newXS("version::stringify", XS_version_stringify, file);
223 newXS("version::(0+", XS_version_numify, file);
224 newXS("version::numify", XS_version_numify, file);
225 newXS("version::normal", XS_version_normal, file);
226 newXS("version::(cmp", XS_version_vcmp, file);
227 newXS("version::(<=>", XS_version_vcmp, file);
228 newXS("version::vcmp", XS_version_vcmp, file);
229 newXS("version::(bool", XS_version_boolean, file);
230 newXS("version::boolean", XS_version_boolean, file);
231 newXS("version::(nomethod", XS_version_noop, file);
232 newXS("version::noop", XS_version_noop, file);
233 newXS("version::is_alpha", XS_version_is_alpha, file);
234 newXS("version::qv", XS_version_qv, file);
236 newXS("utf8::is_utf8", XS_utf8_is_utf8, file);
237 newXS("utf8::valid", XS_utf8_valid, file);
238 newXS("utf8::encode", XS_utf8_encode, file);
239 newXS("utf8::decode", XS_utf8_decode, file);
240 newXS("utf8::upgrade", XS_utf8_upgrade, file);
241 newXS("utf8::downgrade", XS_utf8_downgrade, file);
242 newXS("utf8::native_to_unicode", XS_utf8_native_to_unicode, file);
243 newXS("utf8::unicode_to_native", XS_utf8_unicode_to_native, file);
244 newXSproto("Internals::SvREADONLY",XS_Internals_SvREADONLY, file, "\\[$%@];$");
245 newXSproto("Internals::SvREFCNT",XS_Internals_SvREFCNT, file, "\\[$%@];$");
246 newXSproto("Internals::hv_clear_placeholders",
247 XS_Internals_hv_clear_placehold, file, "\\%");
248 newXSproto("PerlIO::get_layers",
249 XS_PerlIO_get_layers, file, "*;@");
250 newXS("Regexp::DESTROY", XS_Regexp_DESTROY, file);
251 newXSproto("Internals::hash_seed",XS_Internals_hash_seed, file, "");
252 newXSproto("Internals::rehash_seed",XS_Internals_rehash_seed, file, "");
253 newXSproto("Internals::HvREHASH", XS_Internals_HvREHASH, file, "\\%");
254 newXSproto("Internals::inc_sub_generation",XS_Internals_inc_sub_generation,
265 Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)");
267 SV * const sv = ST(0);
272 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
273 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
276 name = SvPV_nolen_const(ST(1));
278 ST(0) = boolSV(sv_derived_from(sv, name));
293 Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)");
299 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
300 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
303 name = SvPV_nolen_const(ST(1));
312 pkg = gv_stashsv(sv, FALSE);
316 GV * const gv = gv_fetchmethod_autoload(pkg, name, FALSE);
318 rv = sv_2mortal(newRV((SV*)GvCV(gv)));
325 XS(XS_UNIVERSAL_VERSION)
336 sv = (SV*)SvRV(ST(0));
338 Perl_croak(aTHX_ "Cannot find version of an unblessed reference");
342 pkg = gv_stashsv(ST(0), FALSE);
345 gvp = pkg ? (GV**)hv_fetchs(pkg, "VERSION", FALSE) : NULL;
347 if (gvp && isGV(gv = *gvp) && (sv = GvSV(gv)) && SvOK(sv)) {
348 SV * const nsv = sv_newmortal();
351 if ( !sv_derived_from(sv, "version"))
356 sv = (SV*)&PL_sv_undef;
365 const char * const name = HvNAME_get(pkg);
367 "%s does not define $%s::VERSION--version check failed",
371 "%s defines neither package nor VERSION--version check failed",
372 SvPVx_nolen_const(ST(0)) );
376 if ( !sv_derived_from(req, "version")) {
377 /* req may very well be R/O, so create a new object */
378 SV * const nsv = sv_newmortal();
384 if ( vcmp( req, sv ) > 0 )
385 Perl_croak(aTHX_ "%s version %"SVf" (%"SVf") required--"
386 "this is only version %"SVf" (%"SVf")", HvNAME_get(pkg),
387 vnumify(req),vnormal(req),vnumify(sv),vnormal(sv));
390 if ( SvOK(sv) && sv_derived_from(sv, "version") ) {
404 Perl_croak(aTHX_ "Usage: version::new(class, version)");
409 const char * const classname =
410 sv_isobject(ST(0)) /* get the class if called as an object method */
411 ? HvNAME(SvSTASH(SvRV(ST(0))))
412 : (char *)SvPV_nolen(ST(0));
415 /* no parameter provided */
416 if ( sv_isobject(ST(0)) ) {
417 /* copy existing object */
421 /* create empty object */
426 else if ( items == 3 ) {
428 Perl_sv_setpvf(aTHX_ vs,"v%s",SvPV_nolen_const(ST(2)));
431 rv = new_version(vs);
432 if ( strcmp(classname,"version") != 0 ) /* inherited new() */
433 sv_bless(rv, gv_stashpv(classname,TRUE));
435 PUSHs(sv_2mortal(rv));
441 XS(XS_version_stringify)
446 Perl_croak(aTHX_ "Usage: version::stringify(lobj, ...)");
451 if (sv_derived_from(ST(0), "version")) {
455 Perl_croak(aTHX_ "lobj is not of type version");
457 PUSHs(sv_2mortal(vstringify(lobj)));
464 XS(XS_version_numify)
469 Perl_croak(aTHX_ "Usage: version::numify(lobj, ...)");
474 if (sv_derived_from(ST(0), "version")) {
478 Perl_croak(aTHX_ "lobj is not of type version");
480 PUSHs(sv_2mortal(vnumify(lobj)));
487 XS(XS_version_normal)
492 Perl_croak(aTHX_ "Usage: version::normal(lobj, ...)");
497 if (sv_derived_from(ST(0), "version")) {
501 Perl_croak(aTHX_ "lobj is not of type version");
503 PUSHs(sv_2mortal(vnormal(lobj)));
515 Perl_croak(aTHX_ "Usage: version::vcmp(lobj, ...)");
520 if (sv_derived_from(ST(0), "version")) {
524 Perl_croak(aTHX_ "lobj is not of type version");
530 const IV swap = (IV)SvIV(ST(2));
532 if ( ! sv_derived_from(robj, "version") )
534 robj = new_version(robj);
540 rs = newSViv(vcmp(rvs,lobj));
544 rs = newSViv(vcmp(lobj,rvs));
547 PUSHs(sv_2mortal(rs));
555 XS(XS_version_boolean)
560 Perl_croak(aTHX_ "Usage: version::boolean(lobj, ...)");
562 if (sv_derived_from(ST(0), "version")) {
563 SV * const lobj = SvRV(ST(0));
564 SV * const rs = newSViv( vcmp(lobj,new_version(newSVpvs("0"))) );
565 PUSHs(sv_2mortal(rs));
570 Perl_croak(aTHX_ "lobj is not of type version");
578 Perl_croak(aTHX_ "Usage: version::noop(lobj, ...)");
579 if (sv_derived_from(ST(0), "version"))
580 Perl_croak(aTHX_ "operation not supported with version object");
582 Perl_croak(aTHX_ "lobj is not of type version");
583 #ifndef HASATTRIBUTE_NORETURN
588 XS(XS_version_is_alpha)
593 Perl_croak(aTHX_ "Usage: version::is_alpha(lobj)");
595 if (sv_derived_from(ST(0), "version")) {
596 SV * const lobj = ST(0);
597 if ( hv_exists((HV*)SvRV(lobj), "alpha", 5 ) )
605 Perl_croak(aTHX_ "lobj is not of type version");
613 Perl_croak(aTHX_ "Usage: version::qv(ver)");
617 if ( !SvVOK(ver) ) { /* only need to do with if not already v-string */
618 SV * const vs = sv_newmortal();
620 if ( SvNOK(ver) ) /* may get too much accuracy */
623 const STRLEN len = my_sprintf(tbuf,"%.9"NVgf, SvNVX(ver));
624 version = savepvn(tbuf, len);
628 version = savesvpv(ver);
630 (void)scan_version(version,vs,TRUE);
637 PUSHs(sv_2mortal(new_version(ver)));
650 Perl_croak(aTHX_ "Usage: utf8::is_utf8(sv)");
652 const SV * const sv = ST(0);
666 Perl_croak(aTHX_ "Usage: utf8::valid(sv)");
668 SV * const sv = ST(0);
670 const char * const s = SvPV_const(sv,len);
671 if (!SvUTF8(sv) || is_utf8_string((const U8*)s,len))
684 Perl_croak(aTHX_ "Usage: utf8::encode(sv)");
685 sv_utf8_encode(ST(0));
694 Perl_croak(aTHX_ "Usage: utf8::decode(sv)");
696 SV * const sv = ST(0);
697 const bool RETVAL = sv_utf8_decode(sv);
698 ST(0) = boolSV(RETVAL);
709 Perl_croak(aTHX_ "Usage: utf8::upgrade(sv)");
711 SV * const sv = ST(0);
715 RETVAL = sv_utf8_upgrade(sv);
716 XSprePUSH; PUSHi((IV)RETVAL);
721 XS(XS_utf8_downgrade)
725 if (items < 1 || items > 2)
726 Perl_croak(aTHX_ "Usage: utf8::downgrade(sv, failok=0)");
728 SV * const sv = ST(0);
729 const bool failok = (items < 2) ? 0 : (int)SvIV(ST(1));
730 const bool RETVAL = sv_utf8_downgrade(sv, failok);
732 ST(0) = boolSV(RETVAL);
738 XS(XS_utf8_native_to_unicode)
742 const UV uv = SvUV(ST(0));
745 Perl_croak(aTHX_ "Usage: utf8::native_to_unicode(sv)");
747 ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv)));
751 XS(XS_utf8_unicode_to_native)
755 const UV uv = SvUV(ST(0));
758 Perl_croak(aTHX_ "Usage: utf8::unicode_to_native(sv)");
760 ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv)));
764 XS(XS_Internals_SvREADONLY) /* This is dangerous stuff. */
768 SV * const sv = SvRV(ST(0));
776 else if (items == 2) {
782 /* I hope you really know what you are doing. */
787 XSRETURN_UNDEF; /* Can't happen. */
790 XS(XS_Internals_SvREFCNT) /* This is dangerous stuff. */
794 SV * const sv = SvRV(ST(0));
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));
803 XSRETURN_UNDEF; /* Can't happen. */
806 XS(XS_Internals_hv_clear_placehold)
812 Perl_croak(aTHX_ "Usage: UNIVERSAL::hv_clear_placeholders(hv)");
814 HV * const hv = (HV *) SvRV(ST(0));
815 hv_clear_placeholders(hv);
820 XS(XS_Regexp_DESTROY)
825 XS(XS_PerlIO_get_layers)
829 if (items < 1 || items % 2 == 0)
830 Perl_croak(aTHX_ "Usage: PerlIO_get_layers(filehandle[,args])");
837 bool details = FALSE;
841 for (svp = MARK + 2; svp <= SP; svp += 2) {
842 SV * const * const varp = svp;
843 SV * const * const valp = svp + 1;
845 const char * const key = SvPV_const(*varp, klen);
849 if (klen == 5 && memEQ(key, "input", 5)) {
850 input = SvTRUE(*valp);
855 if (klen == 6 && memEQ(key, "output", 6)) {
856 input = !SvTRUE(*valp);
861 if (klen == 7 && memEQ(key, "details", 7)) {
862 details = SvTRUE(*valp);
869 "get_layers: unknown argument '%s'",
881 if (SvROK(sv) && isGV(SvRV(sv)))
884 gv = gv_fetchsv(sv, 0, SVt_PVIO);
887 if (gv && (io = GvIO(gv))) {
889 AV* const av = PerlIO_get_layers(aTHX_ input ?
890 IoIFP(io) : IoOFP(io));
892 const I32 last = av_len(av);
895 for (i = last; i >= 0; i -= 3) {
896 SV * const * const namsvp = av_fetch(av, i - 2, FALSE);
897 SV * const * const argsvp = av_fetch(av, i - 1, FALSE);
898 SV * const * const flgsvp = av_fetch(av, i, FALSE);
900 const bool namok = namsvp && *namsvp && SvPOK(*namsvp);
901 const bool argok = argsvp && *argsvp && SvPOK(*argsvp);
902 const bool flgok = flgsvp && *flgsvp && SvIOK(*flgsvp);
906 ? newSVpvn(SvPVX_const(*namsvp), SvCUR(*namsvp))
909 ? newSVpvn(SvPVX_const(*argsvp), SvCUR(*argsvp))
912 XPUSHi(SvIVX(*flgsvp));
914 XPUSHs(&PL_sv_undef);
919 XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf"(%"SVf")",
922 XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf, *namsvp));
924 XPUSHs(&PL_sv_undef);
927 const IV flags = SvIVX(*flgsvp);
929 if (flags & PERLIO_F_UTF8) {
930 XPUSHs(newSVpvs("utf8"));
947 XS(XS_Internals_hash_seed)
950 /* Using dXSARGS would also have dITEM and dSP,
951 * which define 2 unused local variables. */
954 PERL_UNUSED_VAR(mark);
955 XSRETURN_UV(PERL_HASH_SEED);
958 XS(XS_Internals_rehash_seed)
961 /* Using dXSARGS would also have dITEM and dSP,
962 * which define 2 unused local variables. */
965 PERL_UNUSED_VAR(mark);
966 XSRETURN_UV(PL_rehash_seed);
969 XS(XS_Internals_HvREHASH) /* Subject to change */
974 const HV * const hv = (HV *) SvRV(ST(0));
975 if (items == 1 && SvTYPE(hv) == SVt_PVHV) {
982 Perl_croak(aTHX_ "Internals::HvREHASH $hashref");
985 XS(XS_Internals_inc_sub_generation)
988 /* Using dXSARGS would also have dITEM and dSP,
989 * which define 2 unused local variables. */
992 PERL_UNUSED_VAR(mark);
999 * c-indentation-style: bsd
1001 * indent-tabs-mode: t
1004 * ex: set ts=8 sts=4 sw=4 noet: