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, const HV* const 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 && ((const HV *)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_fetchs(stash, "::ISA::CACHE::", FALSE);
64 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (subgen = GvSV(gv))
67 if (SvIV(subgen) == (IV)PL_sub_generation) {
68 SV** const svp = (SV**)hv_fetch(hv, name, len, FALSE);
72 if (sv != &PL_sv_undef)
73 DEBUG_o( Perl_deb(aTHX_ "Using cached ISA %s for package %s\n",
76 return (sv == &PL_sv_yes);
80 DEBUG_o( Perl_deb(aTHX_ "ISA Cache in package %s is stale\n",
83 sv_setiv(subgen, PL_sub_generation);
87 gvp = (GV**)hv_fetchs(stash, "ISA", FALSE);
89 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (av = GvAV(gv))) {
91 gvp = (GV**)hv_fetchs(stash, "::ISA::CACHE::", TRUE);
95 if (SvTYPE(gv) != SVt_PVGV)
96 gv_init(gv, stash, "::ISA::CACHE::", 14, TRUE);
101 subgen = newSViv(PL_sub_generation);
106 SV** svp = AvARRAY(av);
107 /* NOTE: No support for tied ISA */
108 I32 items = AvFILLp(av) + 1;
110 SV* const sv = *svp++;
111 HV* const basestash = gv_stashsv(sv, FALSE);
113 if (ckWARN(WARN_MISC))
114 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
115 "Can't locate package %"SVf" for @%s::ISA",
119 if (isa_lookup(basestash, name, name_stash, len, level + 1)) {
120 (void)hv_store(hv,name,len,&PL_sv_yes,0);
124 (void)hv_store(hv,name,len,&PL_sv_no,0);
131 =head1 SV Manipulation Functions
133 =for apidoc sv_derived_from
135 Returns a boolean indicating whether the SV is derived from the specified
136 class. This is the function that implements C<UNIVERSAL::isa>. It works
137 for class names as well as for objects.
143 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);
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);
180 XS(XS_utf8_downgrade);
181 XS(XS_utf8_unicode_to_native);
182 XS(XS_utf8_native_to_unicode);
183 XS(XS_Internals_SvREADONLY);
184 XS(XS_Internals_SvREFCNT);
185 XS(XS_Internals_hv_clear_placehold);
186 XS(XS_PerlIO_get_layers);
187 XS(XS_Regexp_DESTROY);
188 XS(XS_Internals_hash_seed);
189 XS(XS_Internals_rehash_seed);
190 XS(XS_Internals_HvREHASH);
193 Perl_boot_core_UNIVERSAL(pTHX)
195 static const char file[] = __FILE__;
197 newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, (char *)file);
198 newXS("UNIVERSAL::can", XS_UNIVERSAL_can, (char *)file);
199 newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, (char *)file);
200 newXS("utf8::is_utf8", XS_utf8_is_utf8, (char *)file);
201 newXS("utf8::valid", XS_utf8_valid, (char *)file);
202 newXS("utf8::encode", XS_utf8_encode, (char *)file);
203 newXS("utf8::decode", XS_utf8_decode, (char *)file);
204 newXS("utf8::upgrade", XS_utf8_upgrade, (char *)file);
205 newXS("utf8::downgrade", XS_utf8_downgrade, (char *)file);
206 newXS("utf8::native_to_unicode", XS_utf8_native_to_unicode, (char *)file);
207 newXS("utf8::unicode_to_native", XS_utf8_unicode_to_native, (char *)file);
208 newXSproto("Internals::SvREADONLY",XS_Internals_SvREADONLY, (char *)file, "\\[$%@];$");
209 newXSproto("Internals::SvREFCNT",XS_Internals_SvREFCNT, (char *)file, "\\[$%@];$");
210 newXSproto("Internals::hv_clear_placeholders",
211 XS_Internals_hv_clear_placehold, (char *)file, "\\%");
212 newXSproto("PerlIO::get_layers",
213 XS_PerlIO_get_layers, (char *)file, "*;@");
214 newXS("Regexp::DESTROY", XS_Regexp_DESTROY, (char *)file);
215 newXSproto("Internals::hash_seed",XS_Internals_hash_seed, (char *)file, "");
216 newXSproto("Internals::rehash_seed",XS_Internals_rehash_seed, (char *)file, "");
217 newXSproto("Internals::HvREHASH", XS_Internals_HvREHASH, (char *)file, "\\%");
226 Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)");
228 SV * const sv = ST(0);
233 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
234 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
237 name = SvPV_nolen_const(ST(1));
239 ST(0) = boolSV(sv_derived_from(sv, name));
253 Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)");
259 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
260 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
263 name = SvPV_nolen_const(ST(1));
272 pkg = gv_stashsv(sv, FALSE);
276 GV * const gv = gv_fetchmethod_autoload(pkg, name, FALSE);
278 rv = sv_2mortal(newRV((SV*)GvCV(gv)));
285 XS(XS_UNIVERSAL_VERSION)
295 sv = (SV*)SvRV(ST(0));
297 Perl_croak(aTHX_ "Cannot find version of an unblessed reference");
301 pkg = gv_stashsv(ST(0), FALSE);
304 gvp = pkg ? (GV**)hv_fetchs(pkg, "VERSION", FALSE) : NULL;
306 if (gvp && isGV(gv = *gvp) && (sv = GvSV(gv)) && SvOK(sv)) {
307 SV * const nsv = sv_newmortal();
313 sv = (SV*)&PL_sv_undef;
322 const char * const name = HvNAME_get(pkg);
324 "%s does not define $%s::VERSION--version check failed",
328 "%s defines neither package nor VERSION--version check failed",
329 SvPVx_nolen_const(ST(0)) );
332 if (!SvNIOK(sv) && SvPOK(sv)) {
334 const char *const str = SvPV_const(sv,len);
337 /* XXX could DWIM "1.2.3" here */
338 if (!isDIGIT(str[len]) && str[len] != '.' && str[len] != '_')
342 if (SvNOK(req) && SvPOK(req)) {
343 /* they said C<use Foo v1.2.3> and $Foo::VERSION
344 * doesn't look like a float: do string compare */
345 if (sv_cmp(req,sv) == 1) {
346 Perl_croak(aTHX_ "%s v%"VDf" required--"
347 "this is only v%"VDf,
348 HvNAME(pkg), req, sv);
352 /* they said C<use Foo 1.002_003> and $Foo::VERSION
353 * doesn't look like a float: force numeric compare */
354 (void)SvUPGRADE(sv, SVt_PVNV);
355 SvNVX(sv) = str_to_version(sv);
360 /* if we get here, we're looking for a numeric comparison,
361 * so force the required version into a float, even if they
362 * said C<use Foo v1.2.3> */
363 if (SvNOK(req) && SvPOK(req)) {
365 req = sv_newmortal();
369 if (SvNV(req) > SvNV(sv))
370 Perl_croak(aTHX_ "%s version %s required--this is only version %s",
371 HvNAME_get(pkg), SvPV_nolen(req), SvPV_nolen(sv));
384 Perl_croak(aTHX_ "Usage: utf8::is_utf8(sv)");
386 const SV * const sv = ST(0);
399 Perl_croak(aTHX_ "Usage: utf8::valid(sv)");
401 SV * const sv = ST(0);
403 const char * const s = SvPV_const(sv,len);
404 if (!SvUTF8(sv) || is_utf8_string((U8*)s,len))
416 Perl_croak(aTHX_ "Usage: utf8::encode(sv)");
417 sv_utf8_encode(ST(0));
425 Perl_croak(aTHX_ "Usage: utf8::decode(sv)");
427 SV * const sv = ST(0);
428 const bool RETVAL = sv_utf8_decode(sv);
429 ST(0) = boolSV(RETVAL);
439 Perl_croak(aTHX_ "Usage: utf8::upgrade(sv)");
441 SV * const sv = ST(0);
445 RETVAL = sv_utf8_upgrade(sv);
446 XSprePUSH; PUSHi((IV)RETVAL);
451 XS(XS_utf8_downgrade)
454 if (items < 1 || items > 2)
455 Perl_croak(aTHX_ "Usage: utf8::downgrade(sv, failok=0)");
457 SV * const sv = ST(0);
458 const bool failok = (items < 2) ? 0 : (int)SvIV(ST(1));
459 const bool RETVAL = sv_utf8_downgrade(sv, failok);
461 ST(0) = boolSV(RETVAL);
467 XS(XS_utf8_native_to_unicode)
470 const UV uv = SvUV(ST(0));
473 Perl_croak(aTHX_ "Usage: utf8::native_to_unicode(sv)");
475 ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv)));
479 XS(XS_utf8_unicode_to_native)
482 const UV uv = SvUV(ST(0));
485 Perl_croak(aTHX_ "Usage: utf8::unicode_to_native(sv)");
487 ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv)));
491 XS(XS_Internals_SvREADONLY) /* This is dangerous stuff. */
494 SV * const sv = SvRV(ST(0));
502 else if (items == 2) {
508 /* I hope you really know what you are doing. */
513 XSRETURN_UNDEF; /* Can't happen. */
516 XS(XS_Internals_SvREFCNT) /* This is dangerous stuff. */
519 SV * const sv = SvRV(ST(0));
522 XSRETURN_IV(SvREFCNT(sv) - 1); /* Minus the ref created for us. */
523 else if (items == 2) {
524 /* I hope you really know what you are doing. */
525 SvREFCNT(sv) = SvIV(ST(1));
526 XSRETURN_IV(SvREFCNT(sv));
528 XSRETURN_UNDEF; /* Can't happen. */
531 XS(XS_Internals_hv_clear_placehold)
536 Perl_croak(aTHX_ "Usage: UNIVERSAL::hv_clear_placeholders(hv)");
538 HV * const hv = (HV *) SvRV(ST(0));
539 hv_clear_placeholders(hv);
544 XS(XS_Regexp_DESTROY)
550 XS(XS_PerlIO_get_layers)
553 if (items < 1 || items % 2 == 0)
554 Perl_croak(aTHX_ "Usage: PerlIO_get_layers(filehandle[,args])");
561 bool details = FALSE;
565 for (svp = MARK + 2; svp <= SP; svp += 2) {
566 SV * const * const varp = svp;
567 SV * const * const valp = svp + 1;
569 const char * const key = SvPV_const(*varp, klen);
573 if (klen == 5 && memEQ(key, "input", 5)) {
574 input = SvTRUE(*valp);
579 if (klen == 6 && memEQ(key, "output", 6)) {
580 input = !SvTRUE(*valp);
585 if (klen == 7 && memEQ(key, "details", 7)) {
586 details = SvTRUE(*valp);
593 "get_layers: unknown argument '%s'",
605 if (SvROK(sv) && isGV(SvRV(sv)))
608 gv = gv_fetchsv(sv, 0, SVt_PVIO);
611 if (gv && (io = GvIO(gv))) {
613 AV* const av = PerlIO_get_layers(aTHX_ input ?
614 IoIFP(io) : IoOFP(io));
616 const I32 last = av_len(av);
619 for (i = last; i >= 0; i -= 3) {
620 SV * const * const namsvp = av_fetch(av, i - 2, FALSE);
621 SV * const * const argsvp = av_fetch(av, i - 1, FALSE);
622 SV * const * const flgsvp = av_fetch(av, i, FALSE);
624 const bool namok = namsvp && *namsvp && SvPOK(*namsvp);
625 const bool argok = argsvp && *argsvp && SvPOK(*argsvp);
626 const bool flgok = flgsvp && *flgsvp && SvIOK(*flgsvp);
630 ? newSVpvn(SvPVX_const(*namsvp), SvCUR(*namsvp))
633 ? newSVpvn(SvPVX_const(*argsvp), SvCUR(*argsvp))
636 XPUSHi(SvIVX(*flgsvp));
638 XPUSHs(&PL_sv_undef);
643 XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf"(%"SVf")",
647 XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf,
650 XPUSHs(&PL_sv_undef);
653 const IV flags = SvIVX(*flgsvp);
655 if (flags & PERLIO_F_UTF8) {
656 XPUSHs(newSVpvs("utf8"));
673 XS(XS_Internals_hash_seed)
675 /* Using dXSARGS would also have dITEM and dSP,
676 * which define 2 unused local variables. */
679 PERL_UNUSED_VAR(mark);
680 XSRETURN_UV(PERL_HASH_SEED);
683 XS(XS_Internals_rehash_seed)
685 /* Using dXSARGS would also have dITEM and dSP,
686 * which define 2 unused local variables. */
689 PERL_UNUSED_VAR(mark);
690 XSRETURN_UV(PL_rehash_seed);
693 XS(XS_Internals_HvREHASH) /* Subject to change */
697 const HV * const hv = (HV *) SvRV(ST(0));
698 if (items == 1 && SvTYPE(hv) == SVt_PVHV) {
705 Perl_croak(aTHX_ "Internals::HvREHASH $hashref");
710 * c-indentation-style: bsd
712 * indent-tabs-mode: t
715 * ex: set ts=8 sts=4 sw=4 noet: