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,
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** const 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;
107 SV* const sv = *svp++;
108 HV* const 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)
150 type = sv_reftype(sv,0);
151 if (type && strEQ(type,name))
153 stash = SvOBJECT(sv) ? SvSTASH(sv) : NULL;
156 stash = gv_stashsv(sv, FALSE);
160 HV * const name_stash = gv_stashpv(name, FALSE);
161 return isa_lookup(stash, name, name_stash, strlen(name), 0) == &PL_sv_yes;
170 PERL_XS_EXPORT_C void XS_UNIVERSAL_isa(pTHX_ CV *cv);
171 PERL_XS_EXPORT_C void XS_UNIVERSAL_can(pTHX_ CV *cv);
172 PERL_XS_EXPORT_C void XS_UNIVERSAL_VERSION(pTHX_ CV *cv);
178 XS(XS_utf8_downgrade);
179 XS(XS_utf8_unicode_to_native);
180 XS(XS_utf8_native_to_unicode);
181 XS(XS_Internals_SvREADONLY);
182 XS(XS_Internals_SvREFCNT);
183 XS(XS_Internals_hv_clear_placehold);
184 XS(XS_PerlIO_get_layers);
185 XS(XS_Regexp_DESTROY);
186 XS(XS_Internals_hash_seed);
187 XS(XS_Internals_rehash_seed);
188 XS(XS_Internals_HvREHASH);
191 Perl_boot_core_UNIVERSAL(pTHX)
193 const char file[] = __FILE__;
195 newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, (char *)file);
196 newXS("UNIVERSAL::can", XS_UNIVERSAL_can, (char *)file);
197 newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, (char *)file);
198 newXS("utf8::is_utf8", XS_utf8_is_utf8, (char *)file);
199 newXS("utf8::valid", XS_utf8_valid, (char *)file);
200 newXS("utf8::encode", XS_utf8_encode, (char *)file);
201 newXS("utf8::decode", XS_utf8_decode, (char *)file);
202 newXS("utf8::upgrade", XS_utf8_upgrade, (char *)file);
203 newXS("utf8::downgrade", XS_utf8_downgrade, (char *)file);
204 newXS("utf8::native_to_unicode", XS_utf8_native_to_unicode, (char *)file);
205 newXS("utf8::unicode_to_native", XS_utf8_unicode_to_native, (char *)file);
206 newXSproto("Internals::SvREADONLY",XS_Internals_SvREADONLY, (char *)file, "\\[$%@];$");
207 newXSproto("Internals::SvREFCNT",XS_Internals_SvREFCNT, (char *)file, "\\[$%@];$");
208 newXSproto("Internals::hv_clear_placeholders",
209 XS_Internals_hv_clear_placehold, (char *)file, "\\%");
210 newXSproto("PerlIO::get_layers",
211 XS_PerlIO_get_layers, (char *)file, "*;@");
212 newXS("Regexp::DESTROY", XS_Regexp_DESTROY, (char *)file);
213 newXSproto("Internals::hash_seed",XS_Internals_hash_seed, (char *)file, "");
214 newXSproto("Internals::rehash_seed",XS_Internals_rehash_seed, (char *)file, "");
215 newXSproto("Internals::HvREHASH", XS_Internals_HvREHASH, (char *)file, "\\%");
224 Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)");
226 SV * const sv = ST(0);
231 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
232 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
235 name = SvPV_nolen_const(ST(1));
237 ST(0) = boolSV(sv_derived_from(sv, name));
251 Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)");
257 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
258 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
261 name = SvPV_nolen_const(ST(1));
270 pkg = gv_stashsv(sv, FALSE);
274 GV * const gv = gv_fetchmethod_autoload(pkg, name, FALSE);
276 rv = sv_2mortal(newRV((SV*)GvCV(gv)));
283 XS(XS_UNIVERSAL_VERSION)
293 sv = (SV*)SvRV(ST(0));
295 Perl_croak(aTHX_ "Cannot find version of an unblessed reference");
299 pkg = gv_stashsv(ST(0), FALSE);
302 gvp = pkg ? (GV**)hv_fetch(pkg,"VERSION",7,FALSE) : NULL;
304 if (gvp && isGV(gv = *gvp) && (sv = GvSV(gv)) && SvOK(sv)) {
305 SV * const nsv = sv_newmortal();
311 sv = (SV*)&PL_sv_undef;
320 const char * const name = HvNAME_get(pkg);
322 "%s does not define $%s::VERSION--version check failed",
326 "%s defines neither package nor VERSION--version check failed",
327 SvPVx_nolen_const(ST(0)) );
330 if (!SvNIOK(sv) && SvPOK(sv)) {
332 const char *const str = SvPV_const(sv,len);
335 /* XXX could DWIM "1.2.3" here */
336 if (!isDIGIT(str[len]) && str[len] != '.' && str[len] != '_')
340 if (SvNOK(req) && SvPOK(req)) {
341 /* they said C<use Foo v1.2.3> and $Foo::VERSION
342 * doesn't look like a float: do string compare */
343 if (sv_cmp(req,sv) == 1) {
344 Perl_croak(aTHX_ "%s v%"VDf" required--"
345 "this is only v%"VDf,
346 HvNAME(pkg), req, sv);
350 /* they said C<use Foo 1.002_003> and $Foo::VERSION
351 * doesn't look like a float: force numeric compare */
352 (void)SvUPGRADE(sv, SVt_PVNV);
353 SvNVX(sv) = str_to_version(sv);
358 /* if we get here, we're looking for a numeric comparison,
359 * so force the required version into a float, even if they
360 * said C<use Foo v1.2.3> */
361 if (SvNOK(req) && SvPOK(req)) {
363 req = sv_newmortal();
367 if (SvNV(req) > SvNV(sv))
368 Perl_croak(aTHX_ "%s version %s required--this is only version %s",
369 HvNAME_get(pkg), SvPV_nolen(req), SvPV_nolen(sv));
382 Perl_croak(aTHX_ "Usage: utf8::is_utf8(sv)");
384 const SV * const sv = ST(0);
397 Perl_croak(aTHX_ "Usage: utf8::valid(sv)");
399 SV * const sv = ST(0);
401 const char * const s = SvPV_const(sv,len);
402 if (!SvUTF8(sv) || is_utf8_string((U8*)s,len))
414 Perl_croak(aTHX_ "Usage: utf8::encode(sv)");
415 sv_utf8_encode(ST(0));
423 Perl_croak(aTHX_ "Usage: utf8::decode(sv)");
425 SV * const sv = ST(0);
426 const bool RETVAL = sv_utf8_decode(sv);
427 ST(0) = boolSV(RETVAL);
437 Perl_croak(aTHX_ "Usage: utf8::upgrade(sv)");
439 SV * const sv = ST(0);
443 RETVAL = sv_utf8_upgrade(sv);
444 XSprePUSH; PUSHi((IV)RETVAL);
449 XS(XS_utf8_downgrade)
452 if (items < 1 || items > 2)
453 Perl_croak(aTHX_ "Usage: utf8::downgrade(sv, failok=0)");
455 SV * const sv = ST(0);
456 const bool failok = (items < 2) ? 0 : (int)SvIV(ST(1));
457 const bool RETVAL = sv_utf8_downgrade(sv, failok);
459 ST(0) = boolSV(RETVAL);
465 XS(XS_utf8_native_to_unicode)
468 const UV uv = SvUV(ST(0));
471 Perl_croak(aTHX_ "Usage: utf8::native_to_unicode(sv)");
473 ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv)));
477 XS(XS_utf8_unicode_to_native)
480 const UV uv = SvUV(ST(0));
483 Perl_croak(aTHX_ "Usage: utf8::unicode_to_native(sv)");
485 ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv)));
489 XS(XS_Internals_SvREADONLY) /* This is dangerous stuff. */
492 SV * const sv = SvRV(ST(0));
500 else if (items == 2) {
506 /* I hope you really know what you are doing. */
511 XSRETURN_UNDEF; /* Can't happen. */
514 XS(XS_Internals_SvREFCNT) /* This is dangerous stuff. */
517 SV * const sv = SvRV(ST(0));
520 XSRETURN_IV(SvREFCNT(sv) - 1); /* Minus the ref created for us. */
521 else if (items == 2) {
522 /* I hope you really know what you are doing. */
523 SvREFCNT(sv) = SvIV(ST(1));
524 XSRETURN_IV(SvREFCNT(sv));
526 XSRETURN_UNDEF; /* Can't happen. */
529 XS(XS_Internals_hv_clear_placehold)
534 Perl_croak(aTHX_ "Usage: UNIVERSAL::hv_clear_placeholders(hv)");
536 HV * const hv = (HV *) SvRV(ST(0));
537 hv_clear_placeholders(hv);
542 XS(XS_Regexp_DESTROY)
547 XS(XS_PerlIO_get_layers)
550 if (items < 1 || items % 2 == 0)
551 Perl_croak(aTHX_ "Usage: PerlIO_get_layers(filehandle[,args])");
558 bool details = FALSE;
562 for (svp = MARK + 2; svp <= SP; svp += 2) {
563 SV * const * const varp = svp;
564 SV * const * const valp = svp + 1;
566 const char * const key = SvPV_const(*varp, klen);
570 if (klen == 5 && memEQ(key, "input", 5)) {
571 input = SvTRUE(*valp);
576 if (klen == 6 && memEQ(key, "output", 6)) {
577 input = !SvTRUE(*valp);
582 if (klen == 7 && memEQ(key, "details", 7)) {
583 details = SvTRUE(*valp);
590 "get_layers: unknown argument '%s'",
602 if (SvROK(sv) && isGV(SvRV(sv)))
605 gv = gv_fetchsv(sv, 0, SVt_PVIO);
608 if (gv && (io = GvIO(gv))) {
610 AV* const av = PerlIO_get_layers(aTHX_ input ?
611 IoIFP(io) : IoOFP(io));
613 const I32 last = av_len(av);
616 for (i = last; i >= 0; i -= 3) {
617 SV * const * const namsvp = av_fetch(av, i - 2, FALSE);
618 SV * const * const argsvp = av_fetch(av, i - 1, FALSE);
619 SV * const * const flgsvp = av_fetch(av, i, FALSE);
621 const bool namok = namsvp && *namsvp && SvPOK(*namsvp);
622 const bool argok = argsvp && *argsvp && SvPOK(*argsvp);
623 const bool flgok = flgsvp && *flgsvp && SvIOK(*flgsvp);
627 ? newSVpvn(SvPVX_const(*namsvp), SvCUR(*namsvp))
630 ? newSVpvn(SvPVX_const(*argsvp), SvCUR(*argsvp))
633 XPUSHi(SvIVX(*flgsvp));
635 XPUSHs(&PL_sv_undef);
640 XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf"(%"SVf")",
643 XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf, *namsvp));
645 XPUSHs(&PL_sv_undef);
648 const IV flags = SvIVX(*flgsvp);
650 if (flags & PERLIO_F_UTF8) {
651 XPUSHs(newSVpvn("utf8", 4));
668 XS(XS_Internals_hash_seed)
670 /* Using dXSARGS would also have dITEM and dSP,
671 * which define 2 unused local variables. */
674 PERL_UNUSED_VAR(mark);
675 XSRETURN_UV(PERL_HASH_SEED);
678 XS(XS_Internals_rehash_seed)
680 /* Using dXSARGS would also have dITEM and dSP,
681 * which define 2 unused local variables. */
684 PERL_UNUSED_VAR(mark);
685 XSRETURN_UV(PL_rehash_seed);
688 XS(XS_Internals_HvREHASH) /* Subject to change */
692 const HV * const hv = (HV *) SvRV(ST(0));
693 if (items == 1 && SvTYPE(hv) == SVt_PVHV) {
700 Perl_croak(aTHX_ "Internals::HvREHASH $hashref");
705 * c-indentation-style: bsd
707 * indent-tabs-mode: t
710 * ex: set ts=8 sts=4 sw=4 noet: