3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 * 2005, 2006, 2007 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, const HV* const 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 && ((const HV *)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) {
69 SV** const svp = (SV**)hv_fetch(hv, name, len, FALSE);
73 if (sv != &PL_sv_undef)
74 DEBUG_o( Perl_deb(aTHX_ "Using cached ISA %s for package %s\n",
77 return (sv == &PL_sv_yes);
81 DEBUG_o( Perl_deb(aTHX_ "ISA Cache in package %s is stale\n",
84 sv_setiv(subgen, PL_sub_generation);
88 gvp = (GV**)hv_fetchs(stash, "ISA", FALSE);
90 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (av = GvAV(gv))) {
92 gvp = (GV**)hv_fetchs(stash, "::ISA::CACHE::", TRUE);
96 if (SvTYPE(gv) != SVt_PVGV)
97 gv_init(gv, stash, "::ISA::CACHE::", 14, TRUE);
102 subgen = newSViv(PL_sub_generation);
107 SV** svp = AvARRAY(av);
108 /* NOTE: No support for tied ISA */
109 I32 items = AvFILLp(av) + 1;
111 SV* const sv = *svp++;
112 HV* const basestash = gv_stashsv(sv, 0);
114 if (ckWARN(WARN_MISC))
115 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
116 "Can't locate package %"SVf" for @%s::ISA",
120 if (isa_lookup(basestash, name, name_stash, len, level + 1)) {
121 (void)hv_store(hv,name,len,&PL_sv_yes,0);
125 (void)hv_store(hv,name,len,&PL_sv_no,0);
132 =head1 SV Manipulation Functions
134 =for apidoc sv_derived_from
136 Returns a boolean indicating whether the SV is derived from the specified class
137 I<at the C level>. To check derivation at the Perl level, call C<isa()> as a
144 Perl_sv_derived_from(pTHX_ SV *sv, const char *name)
154 type = sv_reftype(sv,0);
155 if (type && strEQ(type,name))
157 stash = SvOBJECT(sv) ? SvSTASH(sv) : NULL;
160 stash = gv_stashsv(sv, 0);
164 HV * const name_stash = gv_stashpv(name, 0);
165 return isa_lookup(stash, name, name_stash, strlen(name), 0);
175 Returns a boolean indicating whether the SV performs a specific, named role.
176 The SV can be a Perl object or the name of a Perl class.
184 Perl_sv_does(pTHX_ SV *sv, const char *name)
186 const char *classname;
195 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
196 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
199 if (sv_isobject(sv)) {
200 classname = sv_reftype(SvRV(sv),TRUE);
202 classname = SvPV(sv,PL_na);
205 if (strEQ(name,classname))
210 XPUSHs(sv_2mortal(newSVpv(name, 0)));
213 call_method("isa", G_SCALAR);
216 does_it = SvTRUE( TOPs );
223 PERL_XS_EXPORT_C void XS_UNIVERSAL_isa(pTHX_ CV *cv);
224 PERL_XS_EXPORT_C void XS_UNIVERSAL_can(pTHX_ CV *cv);
225 PERL_XS_EXPORT_C void XS_UNIVERSAL_DOES(pTHX_ CV *cv);
226 PERL_XS_EXPORT_C void XS_UNIVERSAL_VERSION(pTHX_ CV *cv);
228 XS(XS_version_stringify);
229 XS(XS_version_numify);
230 XS(XS_version_normal);
232 XS(XS_version_boolean);
233 #ifdef HASATTRIBUTE_NORETURN
234 XS(XS_version_noop) __attribute__noreturn__;
238 XS(XS_version_is_alpha);
245 XS(XS_utf8_downgrade);
246 XS(XS_utf8_unicode_to_native);
247 XS(XS_utf8_native_to_unicode);
248 XS(XS_Internals_SvREADONLY);
249 XS(XS_Internals_SvREFCNT);
250 XS(XS_Internals_hv_clear_placehold);
251 XS(XS_PerlIO_get_layers);
252 XS(XS_Regexp_DESTROY);
253 XS(XS_Internals_hash_seed);
254 XS(XS_Internals_rehash_seed);
255 XS(XS_Internals_HvREHASH);
256 XS(XS_Internals_inc_sub_generation);
259 Perl_boot_core_UNIVERSAL(pTHX)
262 static const char file[] = __FILE__;
264 newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file);
265 newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file);
266 newXS("UNIVERSAL::DOES", XS_UNIVERSAL_DOES, file);
267 newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file);
269 /* register the overloading (type 'A') magic */
270 PL_amagic_generation++;
271 /* Make it findable via fetchmethod */
272 newXS("version::()", XS_version_noop, file);
273 newXS("version::new", XS_version_new, file);
274 newXS("version::(\"\"", XS_version_stringify, file);
275 newXS("version::stringify", XS_version_stringify, file);
276 newXS("version::(0+", XS_version_numify, file);
277 newXS("version::numify", XS_version_numify, file);
278 newXS("version::normal", XS_version_normal, file);
279 newXS("version::(cmp", XS_version_vcmp, file);
280 newXS("version::(<=>", XS_version_vcmp, file);
281 newXS("version::vcmp", XS_version_vcmp, file);
282 newXS("version::(bool", XS_version_boolean, file);
283 newXS("version::boolean", XS_version_boolean, file);
284 newXS("version::(nomethod", XS_version_noop, file);
285 newXS("version::noop", XS_version_noop, file);
286 newXS("version::is_alpha", XS_version_is_alpha, file);
287 newXS("version::qv", XS_version_qv, file);
289 newXS("utf8::is_utf8", XS_utf8_is_utf8, file);
290 newXS("utf8::valid", XS_utf8_valid, file);
291 newXS("utf8::encode", XS_utf8_encode, file);
292 newXS("utf8::decode", XS_utf8_decode, file);
293 newXS("utf8::upgrade", XS_utf8_upgrade, file);
294 newXS("utf8::downgrade", XS_utf8_downgrade, file);
295 newXS("utf8::native_to_unicode", XS_utf8_native_to_unicode, file);
296 newXS("utf8::unicode_to_native", XS_utf8_unicode_to_native, file);
297 newXSproto("Internals::SvREADONLY",XS_Internals_SvREADONLY, file, "\\[$%@];$");
298 newXSproto("Internals::SvREFCNT",XS_Internals_SvREFCNT, file, "\\[$%@];$");
299 newXSproto("Internals::hv_clear_placeholders",
300 XS_Internals_hv_clear_placehold, file, "\\%");
301 newXSproto("PerlIO::get_layers",
302 XS_PerlIO_get_layers, file, "*;@");
303 newXS("Regexp::DESTROY", XS_Regexp_DESTROY, file);
304 newXSproto("Internals::hash_seed",XS_Internals_hash_seed, file, "");
305 newXSproto("Internals::rehash_seed",XS_Internals_rehash_seed, file, "");
306 newXSproto("Internals::HvREHASH", XS_Internals_HvREHASH, file, "\\%");
307 newXSproto("Internals::inc_sub_generation",XS_Internals_inc_sub_generation,
318 Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)");
320 SV * const sv = ST(0);
325 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
326 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
329 name = SvPV_nolen_const(ST(1));
331 ST(0) = boolSV(sv_derived_from(sv, name));
346 Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)");
352 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
353 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
356 name = SvPV_nolen_const(ST(1));
365 pkg = gv_stashsv(sv, 0);
369 GV * const gv = gv_fetchmethod_autoload(pkg, name, FALSE);
371 rv = sv_2mortal(newRV((SV*)GvCV(gv)));
378 XS(XS_UNIVERSAL_DOES)
384 Perl_croak(aTHX_ "Usage: invocant->does(kind)");
386 SV * const sv = ST(0);
389 name = SvPV_nolen_const(ST(1));
390 if (sv_does( sv, name ))
397 XS(XS_UNIVERSAL_VERSION)
408 sv = (SV*)SvRV(ST(0));
410 Perl_croak(aTHX_ "Cannot find version of an unblessed reference");
414 pkg = gv_stashsv(ST(0), 0);
417 gvp = pkg ? (GV**)hv_fetchs(pkg, "VERSION", FALSE) : NULL;
419 if (gvp && isGV(gv = *gvp) && (sv = GvSV(gv)) && SvOK(sv)) {
420 SV * const nsv = sv_newmortal();
423 if ( !sv_derived_from(sv, "version"))
428 sv = (SV*)&PL_sv_undef;
437 const char * const name = HvNAME_get(pkg);
439 "%s does not define $%s::VERSION--version check failed",
443 "%s defines neither package nor VERSION--version check failed",
444 SvPVx_nolen_const(ST(0)) );
448 if ( !sv_derived_from(req, "version")) {
449 /* req may very well be R/O, so create a new object */
450 SV * const nsv = sv_newmortal();
456 if ( vcmp( req, sv ) > 0 )
457 Perl_croak(aTHX_ "%s version %"SVf" (%"SVf") required--"
458 "this is only version %"SVf" (%"SVf")", HvNAME_get(pkg),
459 SVfARG(vnumify(req)),
460 SVfARG(vnormal(req)),
462 SVfARG(vnormal(sv)));
465 if ( SvOK(sv) && sv_derived_from(sv, "version") ) {
479 Perl_croak(aTHX_ "Usage: version::new(class, version)");
484 const char * const classname =
485 sv_isobject(ST(0)) /* get the class if called as an object method */
486 ? HvNAME(SvSTASH(SvRV(ST(0))))
487 : (char *)SvPV_nolen(ST(0));
489 if ( items == 1 || vs == &PL_sv_undef ) { /* no param or explicit undef */
490 /* create empty object */
494 else if ( items == 3 ) {
496 Perl_sv_setpvf(aTHX_ vs,"v%s",SvPV_nolen_const(ST(2)));
499 rv = new_version(vs);
500 if ( strcmp(classname,"version") != 0 ) /* inherited new() */
501 sv_bless(rv, gv_stashpv(classname, GV_ADD));
503 PUSHs(sv_2mortal(rv));
509 XS(XS_version_stringify)
514 Perl_croak(aTHX_ "Usage: version::stringify(lobj, ...)");
519 if (sv_derived_from(ST(0), "version")) {
523 Perl_croak(aTHX_ "lobj is not of type version");
525 PUSHs(sv_2mortal(vstringify(lobj)));
532 XS(XS_version_numify)
537 Perl_croak(aTHX_ "Usage: version::numify(lobj, ...)");
542 if (sv_derived_from(ST(0), "version")) {
546 Perl_croak(aTHX_ "lobj is not of type version");
548 PUSHs(sv_2mortal(vnumify(lobj)));
555 XS(XS_version_normal)
560 Perl_croak(aTHX_ "Usage: version::normal(lobj, ...)");
565 if (sv_derived_from(ST(0), "version")) {
569 Perl_croak(aTHX_ "lobj is not of type version");
571 PUSHs(sv_2mortal(vnormal(lobj)));
583 Perl_croak(aTHX_ "Usage: version::vcmp(lobj, ...)");
588 if (sv_derived_from(ST(0), "version")) {
592 Perl_croak(aTHX_ "lobj is not of type version");
598 const IV swap = (IV)SvIV(ST(2));
600 if ( ! sv_derived_from(robj, "version") )
602 robj = new_version(robj);
608 rs = newSViv(vcmp(rvs,lobj));
612 rs = newSViv(vcmp(lobj,rvs));
615 PUSHs(sv_2mortal(rs));
623 XS(XS_version_boolean)
628 Perl_croak(aTHX_ "Usage: version::boolean(lobj, ...)");
630 if (sv_derived_from(ST(0), "version")) {
631 SV * const lobj = SvRV(ST(0));
632 SV * const rs = newSViv( vcmp(lobj,new_version(newSVpvs("0"))) );
633 PUSHs(sv_2mortal(rs));
638 Perl_croak(aTHX_ "lobj is not of type version");
646 Perl_croak(aTHX_ "Usage: version::noop(lobj, ...)");
647 if (sv_derived_from(ST(0), "version"))
648 Perl_croak(aTHX_ "operation not supported with version object");
650 Perl_croak(aTHX_ "lobj is not of type version");
651 #ifndef HASATTRIBUTE_NORETURN
656 XS(XS_version_is_alpha)
661 Perl_croak(aTHX_ "Usage: version::is_alpha(lobj)");
663 if (sv_derived_from(ST(0), "version")) {
664 SV * const lobj = ST(0);
665 if ( hv_exists((HV*)SvRV(lobj), "alpha", 5 ) )
673 Perl_croak(aTHX_ "lobj is not of type version");
681 Perl_croak(aTHX_ "Usage: version::qv(ver)");
685 if ( !SvVOK(ver) ) { /* only need to do with if not already v-string */
686 SV * const vs = sv_newmortal();
688 if ( SvNOK(ver) ) /* may get too much accuracy */
691 #ifdef USE_LOCALE_NUMERIC
692 char *loc = setlocale(LC_NUMERIC, "C");
694 STRLEN len = my_snprintf(tbuf, sizeof(tbuf), "%.9"NVgf, SvNVX(ver));
695 #ifdef USE_LOCALE_NUMERIC
696 setlocale(LC_NUMERIC, loc);
698 while (tbuf[len-1] == '0' && len > 0) len--;
699 version = savepvn(tbuf, len);
703 version = savesvpv(ver);
705 (void)scan_version(version,vs,TRUE);
712 PUSHs(sv_2mortal(new_version(ver)));
725 Perl_croak(aTHX_ "Usage: utf8::is_utf8(sv)");
727 const SV * const sv = ST(0);
741 Perl_croak(aTHX_ "Usage: utf8::valid(sv)");
743 SV * const sv = ST(0);
745 const char * const s = SvPV_const(sv,len);
746 if (!SvUTF8(sv) || is_utf8_string((const U8*)s,len))
759 Perl_croak(aTHX_ "Usage: utf8::encode(sv)");
760 sv_utf8_encode(ST(0));
769 Perl_croak(aTHX_ "Usage: utf8::decode(sv)");
771 SV * const sv = ST(0);
772 const bool RETVAL = sv_utf8_decode(sv);
773 ST(0) = boolSV(RETVAL);
784 Perl_croak(aTHX_ "Usage: utf8::upgrade(sv)");
786 SV * const sv = ST(0);
790 RETVAL = sv_utf8_upgrade(sv);
791 XSprePUSH; PUSHi((IV)RETVAL);
796 XS(XS_utf8_downgrade)
800 if (items < 1 || items > 2)
801 Perl_croak(aTHX_ "Usage: utf8::downgrade(sv, failok=0)");
803 SV * const sv = ST(0);
804 const bool failok = (items < 2) ? 0 : (int)SvIV(ST(1));
805 const bool RETVAL = sv_utf8_downgrade(sv, failok);
807 ST(0) = boolSV(RETVAL);
813 XS(XS_utf8_native_to_unicode)
817 const UV uv = SvUV(ST(0));
820 Perl_croak(aTHX_ "Usage: utf8::native_to_unicode(sv)");
822 ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv)));
826 XS(XS_utf8_unicode_to_native)
830 const UV uv = SvUV(ST(0));
833 Perl_croak(aTHX_ "Usage: utf8::unicode_to_native(sv)");
835 ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv)));
839 XS(XS_Internals_SvREADONLY) /* This is dangerous stuff. */
843 SV * const sv = SvRV(ST(0));
851 else if (items == 2) {
857 /* I hope you really know what you are doing. */
862 XSRETURN_UNDEF; /* Can't happen. */
865 XS(XS_Internals_SvREFCNT) /* This is dangerous stuff. */
869 SV * const sv = SvRV(ST(0));
872 XSRETURN_IV(SvREFCNT(sv) - 1); /* Minus the ref created for us. */
873 else if (items == 2) {
874 /* I hope you really know what you are doing. */
875 SvREFCNT(sv) = SvIV(ST(1));
876 XSRETURN_IV(SvREFCNT(sv));
878 XSRETURN_UNDEF; /* Can't happen. */
881 XS(XS_Internals_hv_clear_placehold)
887 Perl_croak(aTHX_ "Usage: UNIVERSAL::hv_clear_placeholders(hv)");
889 HV * const hv = (HV *) SvRV(ST(0));
890 hv_clear_placeholders(hv);
895 XS(XS_Regexp_DESTROY)
901 XS(XS_PerlIO_get_layers)
905 if (items < 1 || items % 2 == 0)
906 Perl_croak(aTHX_ "Usage: PerlIO_get_layers(filehandle[,args])");
913 bool details = FALSE;
917 for (svp = MARK + 2; svp <= SP; svp += 2) {
918 SV * const * const varp = svp;
919 SV * const * const valp = svp + 1;
921 const char * const key = SvPV_const(*varp, klen);
925 if (klen == 5 && memEQ(key, "input", 5)) {
926 input = SvTRUE(*valp);
931 if (klen == 6 && memEQ(key, "output", 6)) {
932 input = !SvTRUE(*valp);
937 if (klen == 7 && memEQ(key, "details", 7)) {
938 details = SvTRUE(*valp);
945 "get_layers: unknown argument '%s'",
957 if (SvROK(sv) && isGV(SvRV(sv)))
960 gv = gv_fetchsv(sv, 0, SVt_PVIO);
963 if (gv && (io = GvIO(gv))) {
965 AV* const av = PerlIO_get_layers(aTHX_ input ?
966 IoIFP(io) : IoOFP(io));
968 const I32 last = av_len(av);
971 for (i = last; i >= 0; i -= 3) {
972 SV * const * const namsvp = av_fetch(av, i - 2, FALSE);
973 SV * const * const argsvp = av_fetch(av, i - 1, FALSE);
974 SV * const * const flgsvp = av_fetch(av, i, FALSE);
976 const bool namok = namsvp && *namsvp && SvPOK(*namsvp);
977 const bool argok = argsvp && *argsvp && SvPOK(*argsvp);
978 const bool flgok = flgsvp && *flgsvp && SvIOK(*flgsvp);
982 ? newSVpvn(SvPVX_const(*namsvp), SvCUR(*namsvp))
985 ? newSVpvn(SvPVX_const(*argsvp), SvCUR(*argsvp))
988 XPUSHi(SvIVX(*flgsvp));
990 XPUSHs(&PL_sv_undef);
995 XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf"(%"SVf")",
999 XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf,
1002 XPUSHs(&PL_sv_undef);
1005 const IV flags = SvIVX(*flgsvp);
1007 if (flags & PERLIO_F_UTF8) {
1008 XPUSHs(newSVpvs("utf8"));
1025 XS(XS_Internals_hash_seed)
1028 /* Using dXSARGS would also have dITEM and dSP,
1029 * which define 2 unused local variables. */
1031 PERL_UNUSED_ARG(cv);
1032 PERL_UNUSED_VAR(mark);
1033 XSRETURN_UV(PERL_HASH_SEED);
1036 XS(XS_Internals_rehash_seed)
1039 /* Using dXSARGS would also have dITEM and dSP,
1040 * which define 2 unused local variables. */
1042 PERL_UNUSED_ARG(cv);
1043 PERL_UNUSED_VAR(mark);
1044 XSRETURN_UV(PL_rehash_seed);
1047 XS(XS_Internals_HvREHASH) /* Subject to change */
1052 const HV * const hv = (HV *) SvRV(ST(0));
1053 if (items == 1 && SvTYPE(hv) == SVt_PVHV) {
1060 Perl_croak(aTHX_ "Internals::HvREHASH $hashref");
1063 XS(XS_Internals_inc_sub_generation)
1066 /* Using dXSARGS would also have dITEM and dSP,
1067 * which define 2 unused local variables. */
1069 PERL_UNUSED_ARG(cv);
1070 PERL_UNUSED_VAR(mark);
1071 ++PL_sub_generation;
1077 * c-indentation-style: bsd
1079 * indent-tabs-mode: t
1082 * ex: set ts=8 sts=4 sw=4 noet: