3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
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);
182 XS(XS_utf8_downgrade);
183 XS(XS_utf8_unicode_to_native);
184 XS(XS_utf8_native_to_unicode);
185 XS(XS_Internals_SvREADONLY);
186 XS(XS_Internals_SvREFCNT);
187 XS(XS_Internals_hv_clear_placehold);
188 XS(XS_PerlIO_get_layers);
189 XS(XS_Regexp_DESTROY);
192 Perl_boot_core_UNIVERSAL(pTHX)
194 char *file = __FILE__;
196 newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file);
197 newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file);
198 newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file);
200 /* register the overloading (type 'A') magic */
201 PL_amagic_generation++;
202 /* Make it findable via fetchmethod */
203 newXS("version::()", XS_version_noop, file);
204 newXS("version::new", XS_version_new, file);
205 newXS("version::(\"\"", XS_version_stringify, file);
206 newXS("version::stringify", XS_version_stringify, file);
207 newXS("version::(0+", XS_version_numify, file);
208 newXS("version::numify", XS_version_numify, file);
209 newXS("version::(cmp", XS_version_vcmp, file);
210 newXS("version::(<=>", XS_version_vcmp, file);
211 newXS("version::vcmp", XS_version_vcmp, file);
212 newXS("version::(bool", XS_version_boolean, file);
213 newXS("version::boolean", XS_version_boolean, file);
214 newXS("version::(nomethod", XS_version_noop, file);
215 newXS("version::noop", XS_version_noop, file);
216 newXS("version::is_alpha", XS_version_is_alpha, file);
218 newXS("utf8::is_utf8", XS_utf8_is_utf8, file);
219 newXS("utf8::valid", XS_utf8_valid, file);
220 newXS("utf8::encode", XS_utf8_encode, file);
221 newXS("utf8::decode", XS_utf8_decode, file);
222 newXS("utf8::upgrade", XS_utf8_upgrade, file);
223 newXS("utf8::downgrade", XS_utf8_downgrade, file);
224 newXS("utf8::native_to_unicode", XS_utf8_native_to_unicode, file);
225 newXS("utf8::unicode_to_native", XS_utf8_unicode_to_native, file);
226 newXSproto("Internals::SvREADONLY",XS_Internals_SvREADONLY, file, "\\[$%@];$");
227 newXSproto("Internals::SvREFCNT",XS_Internals_SvREFCNT, file, "\\[$%@];$");
228 newXSproto("Internals::hv_clear_placeholders",
229 XS_Internals_hv_clear_placehold, file, "\\%");
230 newXSproto("PerlIO::get_layers",
231 XS_PerlIO_get_layers, file, "*;@");
232 newXS("Regexp::DESTROY", XS_Regexp_DESTROY, file);
244 Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)");
251 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
252 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
255 name = (char *)SvPV(ST(1),n_a);
257 ST(0) = boolSV(sv_derived_from(sv, name));
271 Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)");
278 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
279 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
282 name = (char *)SvPV(ST(1),n_a);
291 pkg = gv_stashsv(sv, FALSE);
295 GV *gv = gv_fetchmethod_autoload(pkg, name, FALSE);
297 rv = sv_2mortal(newRV((SV*)GvCV(gv)));
304 XS(XS_UNIVERSAL_VERSION)
314 sv = (SV*)SvRV(ST(0));
316 Perl_croak(aTHX_ "Cannot find version of an unblessed reference");
320 pkg = gv_stashsv(ST(0), FALSE);
323 gvp = pkg ? (GV**)hv_fetch(pkg,"VERSION",7,FALSE) : Null(GV**);
325 if (gvp && isGV(gv = *gvp) && SvOK(sv = GvSV(gv))) {
326 SV *nsv = sv_newmortal();
332 sv = (SV*)&PL_sv_undef;
343 "%s does not define $%s::VERSION--version check failed",
344 HvNAME(pkg), HvNAME(pkg));
346 char *str = SvPVx(ST(0), len);
349 "%s defines neither package nor VERSION--version check failed", str);
352 if ( !sv_derived_from(sv, "version"))
353 sv = new_version(sv);
355 if ( !sv_derived_from(req, "version"))
356 req = new_version(req);
358 if ( vcmp( SvRV(req), SvRV(sv) ) > 0 )
360 "%s version %"SVf" required--this is only version %"SVf,
361 HvNAME(pkg), req, sv);
373 Perl_croak(aTHX_ "Usage: version::new(class, version)");
376 /* char * class = (char *)SvPV_nolen(ST(0)); */
380 char *vs = savepvn(SvPVX(ST(2)),SvCUR(ST(2)));
381 version = Perl_newSVpvf(aTHX_ "v%s",vs);
384 PUSHs(new_version(version));
390 XS(XS_version_stringify)
394 Perl_croak(aTHX_ "Usage: version::stringify(lobj, ...)");
399 if (sv_derived_from(ST(0), "version")) {
400 SV *tmp = SvRV(ST(0));
404 Perl_croak(aTHX_ "lobj is not of type version");
407 PUSHs(vstringify(lobj));
415 XS(XS_version_numify)
419 Perl_croak(aTHX_ "Usage: version::numify(lobj, ...)");
424 if (sv_derived_from(ST(0), "version")) {
425 SV *tmp = SvRV(ST(0));
429 Perl_croak(aTHX_ "lobj is not of type version");
432 PUSHs(vnumify(lobj));
444 Perl_croak(aTHX_ "Usage: version::vcmp(lobj, ...)");
449 if (sv_derived_from(ST(0), "version")) {
450 SV *tmp = SvRV(ST(0));
454 Perl_croak(aTHX_ "lobj is not of type version");
460 IV swap = (IV)SvIV(ST(2));
462 if ( ! sv_derived_from(robj, "version") )
464 robj = new_version(robj);
470 rs = newSViv(vcmp(rvs,lobj));
474 rs = newSViv(vcmp(lobj,rvs));
485 XS(XS_version_boolean)
489 Perl_croak(aTHX_ "Usage: version::boolean(lobj, ...)");
494 if (sv_derived_from(ST(0), "version")) {
495 SV *tmp = SvRV(ST(0));
499 Perl_croak(aTHX_ "lobj is not of type version");
503 rs = newSViv( vcmp(lobj,new_version(newSVpvn("0",1))) );
516 Perl_croak(aTHX_ "Usage: version::noop(lobj, ...)");
520 if (sv_derived_from(ST(0), "version")) {
521 SV *tmp = SvRV(ST(0));
525 Perl_croak(aTHX_ "lobj is not of type version");
528 Perl_croak(aTHX_ "operation not supported with version object");
535 XS(XS_version_is_alpha)
539 Perl_croak(aTHX_ "Usage: version::is_alpha(lobj)");
544 if (sv_derived_from(ST(0), "version")) {
545 SV *tmp = SvRV(ST(0));
549 Perl_croak(aTHX_ "lobj is not of type version");
551 I32 len = av_len((AV *)lobj);
552 I32 digit = SvIVX(*av_fetch((AV *)lobj, len, 0));
567 Perl_croak(aTHX_ "Usage: utf8::is_utf8(sv)");
584 Perl_croak(aTHX_ "Usage: utf8::valid(sv)");
589 char *s = SvPV(sv,len);
590 if (!SvUTF8(sv) || is_utf8_string((U8*)s,len))
603 Perl_croak(aTHX_ "Usage: utf8::encode(sv)");
616 Perl_croak(aTHX_ "Usage: utf8::decode(sv)");
621 RETVAL = sv_utf8_decode(sv);
622 ST(0) = boolSV(RETVAL);
632 Perl_croak(aTHX_ "Usage: utf8::upgrade(sv)");
638 RETVAL = sv_utf8_upgrade(sv);
639 XSprePUSH; PUSHi((IV)RETVAL);
644 XS(XS_utf8_downgrade)
647 if (items < 1 || items > 2)
648 Perl_croak(aTHX_ "Usage: utf8::downgrade(sv, failok=0)");
657 failok = (int)SvIV(ST(1));
660 RETVAL = sv_utf8_downgrade(sv, failok);
661 ST(0) = boolSV(RETVAL);
667 XS(XS_utf8_native_to_unicode)
673 Perl_croak(aTHX_ "Usage: utf8::native_to_unicode(sv)");
675 ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv)));
679 XS(XS_utf8_unicode_to_native)
685 Perl_croak(aTHX_ "Usage: utf8::unicode_to_native(sv)");
687 ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv)));
691 XS(XS_Internals_SvREADONLY) /* This is dangerous stuff. */
694 SV *sv = SvRV(ST(0));
701 else if (items == 2) {
707 /* I hope you really know what you are doing. */
712 XSRETURN_UNDEF; /* Can't happen. */
715 XS(XS_Internals_SvREFCNT) /* This is dangerous stuff. */
718 SV *sv = SvRV(ST(0));
720 XSRETURN_IV(SvREFCNT(sv) - 1); /* Minus the ref created for us. */
721 else if (items == 2) {
722 /* I hope you really know what you are doing. */
723 SvREFCNT(sv) = SvIV(ST(1));
724 XSRETURN_IV(SvREFCNT(sv));
726 XSRETURN_UNDEF; /* Can't happen. */
729 /* Maybe this should return the number of placeholders found in scalar context,
730 and a list of them in list context. */
731 XS(XS_Internals_hv_clear_placehold)
734 HV *hv = (HV *) SvRV(ST(0));
736 /* I don't care how many parameters were passed in, but I want to avoid
737 the unused variable warning. */
739 items = (I32)HvPLACEHOLDERS(hv);
743 I32 riter = HvRITER(hv);
744 HE *eiter = HvEITER(hv);
746 /* This may look suboptimal with the items *after* the iternext, but
747 it's quite deliberate. We only get here with items==0 if we've
748 just deleted the last placeholder in the hash. If we've just done
749 that then it means that the hash is in lazy delete mode, and the
750 HE is now only referenced in our iterator. If we just quit the loop
751 and discarded our iterator then the HE leaks. So we do the && the
752 other way to ensure iternext is called just one more time, which
753 has the side effect of triggering the lazy delete. */
754 while ((entry = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS))
756 SV *val = hv_iterval(hv, entry);
758 if (val == &PL_sv_undef) {
760 /* It seems that I have to go back in the front of the hash
761 API to delete a hash, even though I have a HE structure
762 pointing to the very entry I want to delete, and could hold
763 onto the previous HE that points to it. And it's easier to
764 go in with SVs as I can then specify the precomputed hash,
765 and don't have fun and games with utf8 keys. */
766 SV *key = hv_iterkeysv(entry);
768 hv_delete_ent (hv, key, G_DISCARD, HeHASH(entry));
779 XS(XS_Regexp_DESTROY)
784 XS(XS_PerlIO_get_layers)
787 if (items < 1 || items % 2 == 0)
788 Perl_croak(aTHX_ "Usage: PerlIO_get_layers(filehandle[,args])");
795 bool details = FALSE;
800 for (svp = MARK + 2; svp <= SP; svp += 2) {
804 char *key = SvPV(*varp, klen);
808 if (klen == 5 && memEQ(key, "input", 5)) {
809 input = SvTRUE(*valp);
814 if (klen == 6 && memEQ(key, "output", 6)) {
815 input = !SvTRUE(*valp);
820 if (klen == 7 && memEQ(key, "details", 7)) {
821 details = SvTRUE(*valp);
828 "get_layers: unknown argument '%s'",
840 if (SvROK(sv) && isGV(SvRV(sv)))
843 gv = gv_fetchpv(SvPVX(sv), FALSE, SVt_PVIO);
846 if (gv && (io = GvIO(gv))) {
848 AV* av = PerlIO_get_layers(aTHX_ input ?
849 IoIFP(io) : IoOFP(io));
851 I32 last = av_len(av);
854 for (i = last; i >= 0; i -= 3) {
858 bool namok, argok, flgok;
860 namsvp = av_fetch(av, i - 2, FALSE);
861 argsvp = av_fetch(av, i - 1, FALSE);
862 flgsvp = av_fetch(av, i, FALSE);
864 namok = namsvp && *namsvp && SvPOK(*namsvp);
865 argok = argsvp && *argsvp && SvPOK(*argsvp);
866 flgok = flgsvp && *flgsvp && SvIOK(*flgsvp);
870 newSVpv(SvPVX(*namsvp), 0) : &PL_sv_undef);
872 newSVpv(SvPVX(*argsvp), 0) : &PL_sv_undef);
874 XPUSHi(SvIVX(*flgsvp));
876 XPUSHs(&PL_sv_undef);
881 XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf"(%"SVf")",
884 XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf, *namsvp));
886 XPUSHs(&PL_sv_undef);
889 IV flags = SvIVX(*flgsvp);
891 if (flags & PERLIO_F_UTF8) {
892 XPUSHs(newSVpvn("utf8", 4));