3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 * 2005, 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,
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))
50 hvname = HvNAME_get(stash);
52 if (strEQ(hvname, name))
55 if (strEQ(name, "UNIVERSAL"))
59 Perl_croak(aTHX_ "Recursive inheritance detected in package '%s'",
62 gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, FALSE);
64 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (subgen = GvSV(gv))
67 if (SvIV(subgen) == (IV)PL_sub_generation) {
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",
77 DEBUG_o( Perl_deb(aTHX_ "ISA Cache in package %s is stale\n",
80 sv_setiv(subgen, PL_sub_generation);
84 gvp = (GV**)hv_fetch(stash,"ISA",3,FALSE);
86 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (av = GvAV(gv))) {
88 gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, TRUE);
92 if (SvTYPE(gv) != SVt_PVGV)
93 gv_init(gv, stash, "::ISA::CACHE::", 14, TRUE);
98 subgen = newSViv(PL_sub_generation);
103 SV** svp = AvARRAY(av);
104 /* NOTE: No support for tied ISA */
105 I32 items = AvFILLp(av) + 1;
108 HV* basestash = gv_stashsv(sv, FALSE);
110 if (ckWARN(WARN_MISC))
111 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
112 "Can't locate package %"SVf" for @%s::ISA",
116 if (&PL_sv_yes == isa_lookup(basestash, name, name_stash,
118 (void)hv_store(hv,name,len,&PL_sv_yes,0);
122 (void)hv_store(hv,name,len,&PL_sv_no,0);
129 =head1 SV Manipulation Functions
131 =for apidoc sv_derived_from
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.
141 Perl_sv_derived_from(pTHX_ SV *sv, const char *name)
143 const char *type = Nullch;
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 PERL_XS_EXPORT_C void XS_UNIVERSAL_isa(pTHX_ CV *cv);
172 PERL_XS_EXPORT_C void XS_UNIVERSAL_can(pTHX_ CV *cv);
173 PERL_XS_EXPORT_C void XS_UNIVERSAL_VERSION(pTHX_ CV *cv);
175 XS(XS_version_stringify);
176 XS(XS_version_numify);
177 XS(XS_version_normal);
179 XS(XS_version_boolean);
180 #ifdef HASATTRIBUTE_NORETURN
181 XS(XS_version_noop) __attribute__noreturn__;
185 XS(XS_version_is_alpha);
192 XS(XS_utf8_downgrade);
193 XS(XS_utf8_unicode_to_native);
194 XS(XS_utf8_native_to_unicode);
195 XS(XS_Internals_SvREADONLY);
196 XS(XS_Internals_SvREFCNT);
197 XS(XS_Internals_hv_clear_placehold);
198 XS(XS_PerlIO_get_layers);
199 XS(XS_Regexp_DESTROY);
200 XS(XS_Internals_hash_seed);
201 XS(XS_Internals_rehash_seed);
202 XS(XS_Internals_HvREHASH);
205 Perl_boot_core_UNIVERSAL(pTHX)
207 const char file[] = __FILE__;
209 newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file);
210 newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file);
211 newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file);
213 /* register the overloading (type 'A') magic */
214 PL_amagic_generation++;
215 /* Make it findable via fetchmethod */
216 newXS("version::()", XS_version_noop, file);
217 newXS("version::new", XS_version_new, file);
218 newXS("version::(\"\"", XS_version_stringify, file);
219 newXS("version::stringify", XS_version_stringify, file);
220 newXS("version::(0+", XS_version_numify, file);
221 newXS("version::numify", XS_version_numify, file);
222 newXS("version::normal", XS_version_normal, file);
223 newXS("version::(cmp", XS_version_vcmp, file);
224 newXS("version::(<=>", XS_version_vcmp, file);
225 newXS("version::vcmp", XS_version_vcmp, file);
226 newXS("version::(bool", XS_version_boolean, file);
227 newXS("version::boolean", XS_version_boolean, file);
228 newXS("version::(nomethod", XS_version_noop, file);
229 newXS("version::noop", XS_version_noop, file);
230 newXS("version::is_alpha", XS_version_is_alpha, file);
231 newXS("version::qv", XS_version_qv, file);
233 newXS("utf8::is_utf8", XS_utf8_is_utf8, file);
234 newXS("utf8::valid", XS_utf8_valid, file);
235 newXS("utf8::encode", XS_utf8_encode, file);
236 newXS("utf8::decode", XS_utf8_decode, file);
237 newXS("utf8::upgrade", XS_utf8_upgrade, file);
238 newXS("utf8::downgrade", XS_utf8_downgrade, file);
239 newXS("utf8::native_to_unicode", XS_utf8_native_to_unicode, file);
240 newXS("utf8::unicode_to_native", XS_utf8_unicode_to_native, file);
241 newXSproto("Internals::SvREADONLY",XS_Internals_SvREADONLY, file, "\\[$%@];$");
242 newXSproto("Internals::SvREFCNT",XS_Internals_SvREFCNT, file, "\\[$%@];$");
243 newXSproto("Internals::hv_clear_placeholders",
244 XS_Internals_hv_clear_placehold, file, "\\%");
245 newXSproto("PerlIO::get_layers",
246 XS_PerlIO_get_layers, file, "*;@");
247 newXS("Regexp::DESTROY", XS_Regexp_DESTROY, file);
248 newXSproto("Internals::hash_seed",XS_Internals_hash_seed, file, "");
249 newXSproto("Internals::rehash_seed",XS_Internals_rehash_seed, file, "");
250 newXSproto("Internals::HvREHASH", XS_Internals_HvREHASH, file, "\\%");
261 Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)");
268 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
269 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
272 name = SvPV_nolen_const(ST(1));
274 ST(0) = boolSV(sv_derived_from(sv, name));
287 Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)");
294 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
295 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
298 name = SvPV_nolen_const(ST(1));
307 pkg = gv_stashsv(sv, FALSE);
311 GV *gv = gv_fetchmethod_autoload(pkg, name, FALSE);
313 rv = sv_2mortal(newRV((SV*)GvCV(gv)));
320 XS(XS_UNIVERSAL_VERSION)
330 sv = (SV*)SvRV(ST(0));
332 Perl_croak(aTHX_ "Cannot find version of an unblessed reference");
336 pkg = gv_stashsv(ST(0), FALSE);
339 gvp = pkg ? (GV**)hv_fetch(pkg,"VERSION",7,FALSE) : Null(GV**);
341 if (gvp && isGV(gv = *gvp) && (sv = GvSV(gv)) && SvOK(sv)) {
342 SV *nsv = sv_newmortal();
345 if ( !sv_derived_from(sv, "version"))
350 sv = (SV*)&PL_sv_undef;
359 const char *name = HvNAME_get(pkg);
361 "%s does not define $%s::VERSION--version check failed",
365 "%s defines neither package nor VERSION--version check failed",
366 SvPVx_nolen_const(ST(0)) );
370 if ( !sv_derived_from(req, "version")) {
371 /* req may very well be R/O, so create a new object */
372 SV *nsv = sv_newmortal();
378 if ( vcmp( req, sv ) > 0 )
379 Perl_croak(aTHX_ "%s version %"SVf" (%"SVf") required--"
380 "this is only version %"SVf" (%"SVf")", HvNAME_get(pkg),
381 vnumify(req),vnormal(req),vnumify(sv),vnormal(sv));
384 if ( SvOK(sv) && sv_derived_from(sv, "version") ) {
397 Perl_croak(aTHX_ "Usage: version::new(class, version)");
402 const char *classname;
404 /* get the class if called as an object method */
405 if ( sv_isobject(ST(0)) ) {
406 classname = HvNAME(SvSTASH(SvRV(ST(0))));
409 classname = (char *)SvPV_nolen(ST(0));
413 /* no parameter provided */
414 if ( sv_isobject(ST(0)) ) {
415 /* copy existing object */
419 /* create empty object */
424 else if ( items == 3 ) {
426 Perl_sv_setpvf(aTHX_ vs,"v%s",SvPV_nolen_const(ST(2)));
429 rv = new_version(vs);
430 if ( strcmp(classname,"version") != 0 ) /* inherited new() */
431 sv_bless(rv, gv_stashpv(classname,TRUE));
433 PUSHs(sv_2mortal(rv));
439 XS(XS_version_stringify)
443 Perl_croak(aTHX_ "Usage: version::stringify(lobj, ...)");
448 if (sv_derived_from(ST(0), "version")) {
452 Perl_croak(aTHX_ "lobj is not of type version");
454 PUSHs(sv_2mortal(vstringify(lobj)));
461 XS(XS_version_numify)
465 Perl_croak(aTHX_ "Usage: version::numify(lobj, ...)");
470 if (sv_derived_from(ST(0), "version")) {
474 Perl_croak(aTHX_ "lobj is not of type version");
476 PUSHs(sv_2mortal(vnumify(lobj)));
483 XS(XS_version_normal)
487 Perl_croak(aTHX_ "Usage: version::normal(lobj, ...)");
492 if (sv_derived_from(ST(0), "version")) {
496 Perl_croak(aTHX_ "lobj is not of type version");
498 PUSHs(sv_2mortal(vnormal(lobj)));
509 Perl_croak(aTHX_ "Usage: version::vcmp(lobj, ...)");
514 if (sv_derived_from(ST(0), "version")) {
518 Perl_croak(aTHX_ "lobj is not of type version");
524 IV swap = (IV)SvIV(ST(2));
526 if ( ! sv_derived_from(robj, "version") )
528 robj = new_version(robj);
534 rs = newSViv(vcmp(rvs,lobj));
538 rs = newSViv(vcmp(lobj,rvs));
541 PUSHs(sv_2mortal(rs));
549 XS(XS_version_boolean)
553 Perl_croak(aTHX_ "Usage: version::boolean(lobj, ...)");
558 if (sv_derived_from(ST(0), "version")) {
562 Perl_croak(aTHX_ "lobj is not of type version");
566 rs = newSViv( vcmp(lobj,new_version(newSVpvn("0",1))) );
567 PUSHs(sv_2mortal(rs));
579 Perl_croak(aTHX_ "Usage: version::noop(lobj, ...)");
580 if (sv_derived_from(ST(0), "version"))
581 Perl_croak(aTHX_ "operation not supported with version object");
583 Perl_croak(aTHX_ "lobj is not of type version");
584 #ifndef HASATTRIBUTE_NORETURN
589 XS(XS_version_is_alpha)
593 Perl_croak(aTHX_ "Usage: version::is_alpha(lobj)");
598 if (sv_derived_from(ST(0), "version"))
601 Perl_croak(aTHX_ "lobj is not of type version");
603 if ( hv_exists((HV*)SvRV(lobj), "alpha", 5 ) )
617 Perl_croak(aTHX_ "Usage: version::qv(ver)");
621 if ( !SvVOK(ver) ) /* only need to do with if not already v-string */
623 SV *vs = sv_newmortal();
625 if ( SvNOK(ver) ) /* may get too much accuracy */
628 sprintf(tbuf,"%.9"NVgf, SvNVX(ver));
629 version = savepv(tbuf);
633 version = savesvpv(ver);
635 (void)scan_version(version,vs,TRUE);
642 PUSHs(sv_2mortal(new_version(ver)));
654 Perl_croak(aTHX_ "Usage: utf8::is_utf8(sv)");
656 const SV *sv = ST(0);
671 Perl_croak(aTHX_ "Usage: utf8::valid(sv)");
676 const char *s = SvPV_const(sv,len);
677 if (!SvUTF8(sv) || is_utf8_string((const U8*)s,len))
690 Perl_croak(aTHX_ "Usage: utf8::encode(sv)");
703 Perl_croak(aTHX_ "Usage: utf8::decode(sv)");
706 const bool RETVAL = sv_utf8_decode(sv);
707 ST(0) = boolSV(RETVAL);
717 Perl_croak(aTHX_ "Usage: utf8::upgrade(sv)");
723 RETVAL = sv_utf8_upgrade(sv);
724 XSprePUSH; PUSHi((IV)RETVAL);
729 XS(XS_utf8_downgrade)
732 if (items < 1 || items > 2)
733 Perl_croak(aTHX_ "Usage: utf8::downgrade(sv, failok=0)");
736 const bool failok = (items < 2) ? 0 : (int)SvIV(ST(1));
737 const bool RETVAL = sv_utf8_downgrade(sv, failok);
739 ST(0) = boolSV(RETVAL);
745 XS(XS_utf8_native_to_unicode)
748 const UV uv = SvUV(ST(0));
751 Perl_croak(aTHX_ "Usage: utf8::native_to_unicode(sv)");
753 ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv)));
757 XS(XS_utf8_unicode_to_native)
760 const UV uv = SvUV(ST(0));
763 Perl_croak(aTHX_ "Usage: utf8::unicode_to_native(sv)");
765 ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv)));
769 XS(XS_Internals_SvREADONLY) /* This is dangerous stuff. */
772 SV *sv = SvRV(ST(0));
780 else if (items == 2) {
786 /* I hope you really know what you are doing. */
791 XSRETURN_UNDEF; /* Can't happen. */
794 XS(XS_Internals_SvREFCNT) /* This is dangerous stuff. */
797 SV *sv = SvRV(ST(0));
800 XSRETURN_IV(SvREFCNT(sv) - 1); /* Minus the ref created for us. */
801 else if (items == 2) {
802 /* I hope you really know what you are doing. */
803 SvREFCNT(sv) = SvIV(ST(1));
804 XSRETURN_IV(SvREFCNT(sv));
806 XSRETURN_UNDEF; /* Can't happen. */
809 XS(XS_Internals_hv_clear_placehold)
812 HV *hv = (HV *) SvRV(ST(0));
815 Perl_croak(aTHX_ "Usage: UNIVERSAL::hv_clear_placeholders(hv)");
816 hv_clear_placeholders(hv);
820 XS(XS_Regexp_DESTROY)
825 XS(XS_PerlIO_get_layers)
828 if (items < 1 || items % 2 == 0)
829 Perl_croak(aTHX_ "Usage: PerlIO_get_layers(filehandle[,args])");
836 bool details = FALSE;
841 for (svp = MARK + 2; svp <= SP; svp += 2) {
845 const char *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, FALSE, SVt_PVIO);
887 if (gv && (io = GvIO(gv))) {
889 AV* av = PerlIO_get_layers(aTHX_ input ?
890 IoIFP(io) : IoOFP(io));
892 I32 last = av_len(av);
895 for (i = last; i >= 0; i -= 3) {
899 bool namok, argok, flgok;
901 namsvp = av_fetch(av, i - 2, FALSE);
902 argsvp = av_fetch(av, i - 1, FALSE);
903 flgsvp = av_fetch(av, i, FALSE);
905 namok = namsvp && *namsvp && SvPOK(*namsvp);
906 argok = argsvp && *argsvp && SvPOK(*argsvp);
907 flgok = flgsvp && *flgsvp && SvIOK(*flgsvp);
911 ? newSVpvn(SvPVX_const(*namsvp), SvCUR(*namsvp))
914 ? newSVpvn(SvPVX_const(*argsvp), SvCUR(*argsvp))
917 XPUSHi(SvIVX(*flgsvp));
919 XPUSHs(&PL_sv_undef);
924 XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf"(%"SVf")",
927 XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf, *namsvp));
929 XPUSHs(&PL_sv_undef);
932 IV flags = SvIVX(*flgsvp);
934 if (flags & PERLIO_F_UTF8) {
935 XPUSHs(newSVpvn("utf8", 4));
952 XS(XS_Internals_hash_seed)
954 /* Using dXSARGS would also have dITEM and dSP,
955 * which define 2 unused local variables. */
958 PERL_UNUSED_VAR(mark);
959 XSRETURN_UV(PERL_HASH_SEED);
962 XS(XS_Internals_rehash_seed)
964 /* Using dXSARGS would also have dITEM and dSP,
965 * which define 2 unused local variables. */
968 PERL_UNUSED_VAR(mark);
969 XSRETURN_UV(PL_rehash_seed);
972 XS(XS_Internals_HvREHASH) /* Subject to change */
976 const HV *hv = (HV *) SvRV(ST(0));
977 if (items == 1 && SvTYPE(hv) == SVt_PVHV) {
984 Perl_croak(aTHX_ "Internals::HvREHASH $hashref");
989 * c-indentation-style: bsd
991 * indent-tabs-mode: t
994 * ex: set ts=8 sts=4 sw=4 noet: