3 * Copyright (c) 1997-2002, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 #define PERL_IN_UNIVERSAL_C
15 * Contributed by Graham Barr <Graham.Barr@tiuk.ti.com>
16 * The main guts of traverse_isa was actually copied from gv_fetchmeth
20 S_isa_lookup(pTHX_ HV *stash, const char *name, HV* name_stash,
29 /* A stash/class can go by many names (ie. User == main::User), so
30 we compare the stash itself just in case */
31 if (name_stash && (stash == name_stash))
34 if (strEQ(HvNAME(stash), name))
38 Perl_croak(aTHX_ "Recursive inheritance detected in package '%s'",
41 gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, FALSE);
43 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (subgen = GvSV(gv))
46 if (SvIV(subgen) == PL_sub_generation) {
48 SV** svp = (SV**)hv_fetch(hv, name, len, FALSE);
49 if (svp && (sv = *svp) != (SV*)&PL_sv_undef) {
50 DEBUG_o( Perl_deb(aTHX_ "Using cached ISA %s for package %s\n",
51 name, HvNAME(stash)) );
56 DEBUG_o( Perl_deb(aTHX_ "ISA Cache in package %s is stale\n",
59 sv_setiv(subgen, PL_sub_generation);
63 gvp = (GV**)hv_fetch(stash,"ISA",3,FALSE);
65 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (av = GvAV(gv))) {
67 gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, TRUE);
71 if (SvTYPE(gv) != SVt_PVGV)
72 gv_init(gv, stash, "::ISA::CACHE::", 14, TRUE);
77 subgen = newSViv(PL_sub_generation);
82 SV** svp = AvARRAY(av);
83 /* NOTE: No support for tied ISA */
84 I32 items = AvFILLp(av) + 1;
87 HV* basestash = gv_stashsv(sv, FALSE);
89 if (ckWARN(WARN_MISC))
90 Perl_warner(aTHX_ WARN_SYNTAX,
91 "Can't locate package %s for @%s::ISA",
92 SvPVX(sv), HvNAME(stash));
95 if (&PL_sv_yes == isa_lookup(basestash, name, name_stash,
97 (void)hv_store(hv,name,len,&PL_sv_yes,0);
101 (void)hv_store(hv,name,len,&PL_sv_no,0);
105 return boolSV(strEQ(name, "UNIVERSAL"));
109 =head1 SV Manipulation Functions
111 =for apidoc sv_derived_from
113 Returns a boolean indicating whether the SV is derived from the specified
114 class. This is the function that implements C<UNIVERSAL::isa>. It works
115 for class names as well as for objects.
121 Perl_sv_derived_from(pTHX_ SV *sv, const char *name)
135 type = sv_reftype(sv,0);
140 stash = gv_stashsv(sv, FALSE);
143 name_stash = gv_stashpv(name, FALSE);
145 return (type && strEQ(type,name)) ||
146 (stash && isa_lookup(stash, name, name_stash, strlen(name), 0)
154 void XS_UNIVERSAL_isa(pTHX_ CV *cv);
155 void XS_UNIVERSAL_can(pTHX_ CV *cv);
156 void XS_UNIVERSAL_VERSION(pTHX_ CV *cv);
161 XS(XS_utf8_downgrade);
162 XS(XS_utf8_unicode_to_native);
163 XS(XS_utf8_native_to_unicode);
164 XS(XS_access_readonly);
167 Perl_boot_core_UNIVERSAL(pTHX)
169 char *file = __FILE__;
171 newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file);
172 newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file);
173 newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file);
174 newXS("utf8::valid", XS_utf8_valid, file);
175 newXS("utf8::encode", XS_utf8_encode, file);
176 newXS("utf8::decode", XS_utf8_decode, file);
177 newXS("utf8::upgrade", XS_utf8_upgrade, file);
178 newXS("utf8::downgrade", XS_utf8_downgrade, file);
179 newXS("utf8::native_to_unicode", XS_utf8_native_to_unicode, file);
180 newXS("utf8::unicode_to_native", XS_utf8_unicode_to_native, file);
181 newXSproto("access::readonly",XS_access_readonly, file, "\\[$%@];$");
193 Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)");
200 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))))
203 name = (char *)SvPV(ST(1),n_a);
205 ST(0) = boolSV(sv_derived_from(sv, name));
219 Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)");
226 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))))
229 name = (char *)SvPV(ST(1),n_a);
238 pkg = gv_stashsv(sv, FALSE);
242 GV *gv = gv_fetchmethod_autoload(pkg, name, FALSE);
244 rv = sv_2mortal(newRV((SV*)GvCV(gv)));
251 XS(XS_UNIVERSAL_VERSION)
261 sv = (SV*)SvRV(ST(0));
263 Perl_croak(aTHX_ "Cannot find version of an unblessed reference");
267 pkg = gv_stashsv(ST(0), FALSE);
270 gvp = pkg ? (GV**)hv_fetch(pkg,"VERSION",7,FALSE) : Null(GV**);
272 if (gvp && isGV(gv = *gvp) && SvOK(sv = GvSV(gv))) {
273 SV *nsv = sv_newmortal();
279 sv = (SV*)&PL_sv_undef;
288 Perl_croak(aTHX_ "%s does not define $%s::VERSION--version check failed",
289 HvNAME(pkg), HvNAME(pkg));
291 if (!SvNIOK(sv) && SvPOK(sv)) {
292 char *str = SvPVx(sv,len);
295 /* XXX could DWIM "1.2.3" here */
296 if (!isDIGIT(str[len]) && str[len] != '.' && str[len] != '_')
300 if (SvNOK(req) && SvPOK(req)) {
301 /* they said C<use Foo v1.2.3> and $Foo::VERSION
302 * doesn't look like a float: do string compare */
303 if (sv_cmp(req,sv) == 1) {
304 Perl_croak(aTHX_ "%s v%"VDf" required--"
305 "this is only v%"VDf,
306 HvNAME(pkg), req, sv);
310 /* they said C<use Foo 1.002_003> and $Foo::VERSION
311 * doesn't look like a float: force numeric compare */
312 (void)SvUPGRADE(sv, SVt_PVNV);
313 SvNVX(sv) = str_to_version(sv);
318 /* if we get here, we're looking for a numeric comparison,
319 * so force the required version into a float, even if they
320 * said C<use Foo v1.2.3> */
321 if (SvNOK(req) && SvPOK(req)) {
323 req = sv_newmortal();
327 if (SvNV(req) > SvNV(sv))
328 Perl_croak(aTHX_ "%s version %s required--this is only version %s",
329 HvNAME(pkg), SvPV_nolen(req), SvPV_nolen(sv));
342 Perl_croak(aTHX_ "Usage: utf8::valid(sv)");
347 char *s = SvPV(sv,len);
348 if (!SvUTF8(sv) || is_utf8_string((U8*)s,len))
361 Perl_croak(aTHX_ "Usage: utf8::encode(sv)");
374 Perl_croak(aTHX_ "Usage: utf8::decode(sv)");
379 RETVAL = sv_utf8_decode(sv);
380 ST(0) = boolSV(RETVAL);
390 Perl_croak(aTHX_ "Usage: utf8::upgrade(sv)");
396 RETVAL = sv_utf8_upgrade(sv);
397 XSprePUSH; PUSHi((IV)RETVAL);
402 XS(XS_utf8_downgrade)
405 if (items < 1 || items > 2)
406 Perl_croak(aTHX_ "Usage: utf8::downgrade(sv, failok=0)");
415 failok = (int)SvIV(ST(1));
418 RETVAL = sv_utf8_downgrade(sv, failok);
419 ST(0) = boolSV(RETVAL);
425 XS(XS_utf8_native_to_unicode)
431 Perl_croak(aTHX_ "Usage: utf8::native_to_unicode(sv)");
433 ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv)));
437 XS(XS_utf8_unicode_to_native)
443 Perl_croak(aTHX_ "Usage: utf8::unicode_to_native(sv)");
445 ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv)));
449 XS(XS_access_readonly)
452 SV *sv = SvRV(ST(0));
453 IV old = SvREADONLY(sv);