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))
49 Perl_croak(aTHX_ "Recursive inheritance detected in package '%s'",
52 gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, FALSE);
54 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (subgen = GvSV(gv))
57 if (SvIV(subgen) == (IV)PL_sub_generation) {
59 SV** svp = (SV**)hv_fetch(hv, name, len, FALSE);
60 if (svp && (sv = *svp) != (SV*)&PL_sv_undef) {
61 DEBUG_o( Perl_deb(aTHX_ "Using cached ISA %s for package %s\n",
62 name, HvNAME(stash)) );
67 DEBUG_o( Perl_deb(aTHX_ "ISA Cache in package %s is stale\n",
70 sv_setiv(subgen, PL_sub_generation);
74 gvp = (GV**)hv_fetch(stash,"ISA",3,FALSE);
76 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (av = GvAV(gv))) {
78 gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, TRUE);
82 if (SvTYPE(gv) != SVt_PVGV)
83 gv_init(gv, stash, "::ISA::CACHE::", 14, TRUE);
88 subgen = newSViv(PL_sub_generation);
93 SV** svp = AvARRAY(av);
94 /* NOTE: No support for tied ISA */
95 I32 items = AvFILLp(av) + 1;
98 HV* basestash = gv_stashsv(sv, FALSE);
100 if (ckWARN(WARN_MISC))
101 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
102 "Can't locate package %"SVf" for @%s::ISA",
106 if (&PL_sv_yes == isa_lookup(basestash, name, name_stash,
108 (void)hv_store(hv,name,len,&PL_sv_yes,0);
112 (void)hv_store(hv,name,len,&PL_sv_no,0);
116 return boolSV(strEQ(name, "UNIVERSAL"));
120 =head1 SV Manipulation Functions
122 =for apidoc sv_derived_from
124 Returns a boolean indicating whether the SV is derived from the specified
125 class. This is the function that implements C<UNIVERSAL::isa>. It works
126 for class names as well as for objects.
132 Perl_sv_derived_from(pTHX_ SV *sv, const char *name)
146 type = sv_reftype(sv,0);
151 stash = gv_stashsv(sv, FALSE);
154 name_stash = gv_stashpv(name, FALSE);
156 return (type && strEQ(type,name)) ||
157 (stash && isa_lookup(stash, name, name_stash, strlen(name), 0)
165 void XS_UNIVERSAL_isa(pTHX_ CV *cv);
166 void XS_UNIVERSAL_can(pTHX_ CV *cv);
167 void XS_UNIVERSAL_VERSION(pTHX_ CV *cv);
172 XS(XS_utf8_downgrade);
173 XS(XS_utf8_unicode_to_native);
174 XS(XS_utf8_native_to_unicode);
175 XS(XS_Internals_SvREADONLY);
176 XS(XS_Internals_SvREFCNT);
177 XS(XS_Internals_hv_clear_placehold);
178 XS(XS_PerlIO_get_layers);
181 Perl_boot_core_UNIVERSAL(pTHX)
183 char *file = __FILE__;
185 newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file);
186 newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file);
187 newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file);
188 newXS("utf8::valid", XS_utf8_valid, file);
189 newXS("utf8::encode", XS_utf8_encode, file);
190 newXS("utf8::decode", XS_utf8_decode, file);
191 newXS("utf8::upgrade", XS_utf8_upgrade, file);
192 newXS("utf8::downgrade", XS_utf8_downgrade, file);
193 newXS("utf8::native_to_unicode", XS_utf8_native_to_unicode, file);
194 newXS("utf8::unicode_to_native", XS_utf8_unicode_to_native, file);
195 newXSproto("Internals::SvREADONLY",XS_Internals_SvREADONLY, file, "\\[$%@];$");
196 newXSproto("Internals::SvREFCNT",XS_Internals_SvREFCNT, file, "\\[$%@];$");
197 newXSproto("Internals::hv_clear_placeholders",
198 XS_Internals_hv_clear_placehold, file, "\\%");
199 newXSproto("PerlIO::get_layers",
200 XS_PerlIO_get_layers, file, "*;@");
212 Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)");
219 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
220 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
223 name = (char *)SvPV(ST(1),n_a);
225 ST(0) = boolSV(sv_derived_from(sv, name));
239 Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)");
246 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
247 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
250 name = (char *)SvPV(ST(1),n_a);
259 pkg = gv_stashsv(sv, FALSE);
263 GV *gv = gv_fetchmethod_autoload(pkg, name, FALSE);
265 rv = sv_2mortal(newRV((SV*)GvCV(gv)));
272 XS(XS_UNIVERSAL_VERSION)
282 sv = (SV*)SvRV(ST(0));
284 Perl_croak(aTHX_ "Cannot find version of an unblessed reference");
288 pkg = gv_stashsv(ST(0), FALSE);
291 gvp = pkg ? (GV**)hv_fetch(pkg,"VERSION",7,FALSE) : Null(GV**);
293 if (gvp && isGV(gv = *gvp) && SvOK(sv = GvSV(gv))) {
294 SV *nsv = sv_newmortal();
300 sv = (SV*)&PL_sv_undef;
311 "%s does not define $%s::VERSION--version check failed",
312 HvNAME(pkg), HvNAME(pkg));
314 char *str = SvPVx(ST(0), len);
317 "%s defines neither package nor VERSION--version check failed", str);
320 if (!SvNIOK(sv) && SvPOK(sv)) {
321 char *str = SvPVx(sv,len);
324 /* XXX could DWIM "1.2.3" here */
325 if (!isDIGIT(str[len]) && str[len] != '.' && str[len] != '_')
329 if (SvNOK(req) && SvPOK(req)) {
330 /* they said C<use Foo v1.2.3> and $Foo::VERSION
331 * doesn't look like a float: do string compare */
332 if (sv_cmp(req,sv) == 1) {
333 Perl_croak(aTHX_ "%s v%"VDf" required--"
334 "this is only v%"VDf,
335 HvNAME(pkg), req, sv);
339 /* they said C<use Foo 1.002_003> and $Foo::VERSION
340 * doesn't look like a float: force numeric compare */
341 (void)SvUPGRADE(sv, SVt_PVNV);
342 SvNVX(sv) = str_to_version(sv);
347 /* if we get here, we're looking for a numeric comparison,
348 * so force the required version into a float, even if they
349 * said C<use Foo v1.2.3> */
350 if (SvNOK(req) && SvPOK(req)) {
352 req = sv_newmortal();
356 if (SvNV(req) > SvNV(sv))
357 Perl_croak(aTHX_ "%s version %s required--this is only version %s",
358 HvNAME(pkg), SvPV_nolen(req), SvPV_nolen(sv));
371 Perl_croak(aTHX_ "Usage: utf8::valid(sv)");
376 char *s = SvPV(sv,len);
377 if (!SvUTF8(sv) || is_utf8_string((U8*)s,len))
390 Perl_croak(aTHX_ "Usage: utf8::encode(sv)");
403 Perl_croak(aTHX_ "Usage: utf8::decode(sv)");
408 RETVAL = sv_utf8_decode(sv);
409 ST(0) = boolSV(RETVAL);
419 Perl_croak(aTHX_ "Usage: utf8::upgrade(sv)");
425 RETVAL = sv_utf8_upgrade(sv);
426 XSprePUSH; PUSHi((IV)RETVAL);
431 XS(XS_utf8_downgrade)
434 if (items < 1 || items > 2)
435 Perl_croak(aTHX_ "Usage: utf8::downgrade(sv, failok=0)");
444 failok = (int)SvIV(ST(1));
447 RETVAL = sv_utf8_downgrade(sv, failok);
448 ST(0) = boolSV(RETVAL);
454 XS(XS_utf8_native_to_unicode)
460 Perl_croak(aTHX_ "Usage: utf8::native_to_unicode(sv)");
462 ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv)));
466 XS(XS_utf8_unicode_to_native)
472 Perl_croak(aTHX_ "Usage: utf8::unicode_to_native(sv)");
474 ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv)));
478 XS(XS_Internals_SvREADONLY) /* This is dangerous stuff. */
481 SV *sv = SvRV(ST(0));
488 else if (items == 2) {
494 /* I hope you really know what you are doing. */
499 XSRETURN_UNDEF; /* Can't happen. */
502 XS(XS_Internals_SvREFCNT) /* This is dangerous stuff. */
505 SV *sv = SvRV(ST(0));
507 XSRETURN_IV(SvREFCNT(sv) - 1); /* Minus the ref created for us. */
508 else if (items == 2) {
509 /* I hope you really know what you are doing. */
510 SvREFCNT(sv) = SvIV(ST(1));
511 XSRETURN_IV(SvREFCNT(sv));
513 XSRETURN_UNDEF; /* Can't happen. */
516 /* Maybe this should return the number of placeholders found in scalar context,
517 and a list of them in list context. */
518 XS(XS_Internals_hv_clear_placehold)
521 HV *hv = (HV *) SvRV(ST(0));
523 /* I don't care how many parameters were passed in, but I want to avoid
524 the unused variable warning. */
526 items = (I32)HvPLACEHOLDERS(hv);
530 I32 riter = HvRITER(hv);
531 HE *eiter = HvEITER(hv);
533 /* This may look suboptimal with the items *after* the iternext, but
534 it's quite deliberate. We only get here with items==0 if we've
535 just deleted the last placeholder in the hash. If we've just done
536 that then it means that the hash is in lazy delete mode, and the
537 HE is now only referenced in our iterator. If we just quit the loop
538 and discarded our iterator then the HE leaks. So we do the && the
539 other way to ensure iternext is called just one more time, which
540 has the side effect of triggering the lazy delete. */
541 while ((entry = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS))
543 SV *val = hv_iterval(hv, entry);
545 if (val == &PL_sv_undef) {
547 /* It seems that I have to go back in the front of the hash
548 API to delete a hash, even though I have a HE structure
549 pointing to the very entry I want to delete, and could hold
550 onto the previous HE that points to it. And it's easier to
551 go in with SVs as I can then specify the precomputed hash,
552 and don't have fun and games with utf8 keys. */
553 SV *key = hv_iterkeysv(entry);
555 hv_delete_ent (hv, key, G_DISCARD, HeHASH(entry));
566 XS(XS_PerlIO_get_layers)
569 if (items < 1 || items % 2 == 0)
570 Perl_croak(aTHX_ "Usage: PerlIO_get_layers(filehandle[,args])");
577 bool details = FALSE;
582 for (svp = MARK + 2; svp <= SP; svp += 2) {
586 char *key = SvPV(*varp, klen);
590 if (klen == 5 && memEQ(key, "input", 5)) {
591 input = SvTRUE(*valp);
596 if (klen == 6 && memEQ(key, "output", 6)) {
597 input = !SvTRUE(*valp);
602 if (klen == 7 && memEQ(key, "details", 7)) {
603 details = SvTRUE(*valp);
610 "get_layers: unknown argument '%s'",
622 if (SvROK(sv) && isGV(SvRV(sv)))
625 gv = gv_fetchpv(SvPVX(sv), FALSE, SVt_PVIO);
628 if (gv && (io = GvIO(gv))) {
630 AV* av = PerlIO_get_layers(aTHX_ input ?
631 IoIFP(io) : IoOFP(io));
633 I32 last = av_len(av);
636 for (i = last; i >= 0; i -= 3) {
640 bool namok, argok, flgok;
642 namsvp = av_fetch(av, i - 2, FALSE);
643 argsvp = av_fetch(av, i - 1, FALSE);
644 flgsvp = av_fetch(av, i, FALSE);
646 namok = namsvp && *namsvp && SvPOK(*namsvp);
647 argok = argsvp && *argsvp && SvPOK(*argsvp);
648 flgok = flgsvp && *flgsvp && SvIOK(*flgsvp);
652 newSVpv(SvPVX(*namsvp), 0) : &PL_sv_undef);
654 newSVpv(SvPVX(*argsvp), 0) : &PL_sv_undef);
656 XPUSHi(SvIVX(*flgsvp));
658 XPUSHs(&PL_sv_undef);
663 XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf"(%"SVf")",
666 XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf, *namsvp));
668 XPUSHs(&PL_sv_undef);
671 IV flags = SvIVX(*flgsvp);
673 if (flags & PERLIO_F_UTF8) {
674 XPUSHs(newSVpvn("utf8", 4));