2 #define PERL_IN_UNIVERSAL_C
6 * Contributed by Graham Barr <Graham.Barr@tiuk.ti.com>
7 * The main guts of traverse_isa was actually copied from gv_fetchmeth
11 S_isa_lookup(pTHX_ HV *stash, const char *name, int len, int level)
22 if (strEQ(HvNAME(stash), name))
26 Perl_croak(aTHX_ "Recursive inheritance detected in package '%s'",
29 gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, FALSE);
31 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (subgen = GvSV(gv))
34 if (SvIV(subgen) == PL_sub_generation) {
36 SV** svp = (SV**)hv_fetch(hv, name, len, FALSE);
37 if (svp && (sv = *svp) != (SV*)&PL_sv_undef) {
38 DEBUG_o( Perl_deb(aTHX_ "Using cached ISA %s for package %s\n",
39 name, HvNAME(stash)) );
44 DEBUG_o( Perl_deb(aTHX_ "ISA Cache in package %s is stale\n",
47 sv_setiv(subgen, PL_sub_generation);
51 gvp = (GV**)hv_fetch(stash,"ISA",3,FALSE);
53 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (av = GvAV(gv))) {
55 gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, TRUE);
59 if (SvTYPE(gv) != SVt_PVGV)
60 gv_init(gv, stash, "::ISA::CACHE::", 14, TRUE);
65 subgen = newSViv(PL_sub_generation);
70 SV** svp = AvARRAY(av);
71 /* NOTE: No support for tied ISA */
72 I32 items = AvFILLp(av) + 1;
75 HV* basestash = gv_stashsv(sv, FALSE);
77 if (ckWARN(WARN_MISC))
78 Perl_warner(aTHX_ WARN_SYNTAX,
79 "Can't locate package %s for @%s::ISA",
80 SvPVX(sv), HvNAME(stash));
83 if (&PL_sv_yes == isa_lookup(basestash, name, len, level + 1)) {
84 (void)hv_store(hv,name,len,&PL_sv_yes,0);
88 (void)hv_store(hv,name,len,&PL_sv_no,0);
92 return boolSV(strEQ(name, "UNIVERSAL"));
96 =for apidoc sv_derived_from
98 Returns a boolean indicating whether the SV is derived from the specified
99 class. This is the function that implements C<UNIVERSAL::isa>. It works
100 for class names as well as for objects.
106 Perl_sv_derived_from(pTHX_ SV *sv, const char *name)
119 type = sv_reftype(sv,0);
124 stash = gv_stashsv(sv, FALSE);
127 return (type && strEQ(type,name)) ||
128 (stash && isa_lookup(stash, name, strlen(name), 0) == &PL_sv_yes)
135 void XS_UNIVERSAL_isa(pTHXo_ CV *cv);
136 void XS_UNIVERSAL_can(pTHXo_ CV *cv);
137 void XS_UNIVERSAL_VERSION(pTHXo_ CV *cv);
142 XS(XS_utf8_downgrade);
143 XS(XS_utf8_unicode_to_native);
144 XS(XS_utf8_native_to_unicode);
147 Perl_boot_core_UNIVERSAL(pTHX)
149 char *file = __FILE__;
151 newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file);
152 newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file);
153 newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file);
154 newXS("utf8::valid", XS_utf8_valid, file);
155 newXS("utf8::encode", XS_utf8_encode, file);
156 newXS("utf8::decode", XS_utf8_decode, file);
157 newXS("utf8::upgrade", XS_utf8_upgrade, file);
158 newXS("utf8::downgrade", XS_utf8_downgrade, file);
159 newXS("utf8::native_to_unicode", XS_utf8_native_to_unicode, file);
160 newXS("utf8::unicode_to_native", XS_utf8_unicode_to_native, file);
172 Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)");
179 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))))
182 name = (char *)SvPV(ST(1),n_a);
184 ST(0) = boolSV(sv_derived_from(sv, name));
198 Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)");
205 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))))
208 name = (char *)SvPV(ST(1),n_a);
217 pkg = gv_stashsv(sv, FALSE);
221 GV *gv = gv_fetchmethod_autoload(pkg, name, FALSE);
223 rv = sv_2mortal(newRV((SV*)GvCV(gv)));
230 XS(XS_UNIVERSAL_VERSION)
240 sv = (SV*)SvRV(ST(0));
242 Perl_croak(aTHX_ "Cannot find version of an unblessed reference");
246 pkg = gv_stashsv(ST(0), FALSE);
249 gvp = pkg ? (GV**)hv_fetch(pkg,"VERSION",7,FALSE) : Null(GV**);
251 if (gvp && isGV(gv = *gvp) && SvOK(sv = GvSV(gv))) {
252 SV *nsv = sv_newmortal();
258 sv = (SV*)&PL_sv_undef;
267 Perl_croak(aTHX_ "%s does not define $%s::VERSION--version check failed",
268 HvNAME(pkg), HvNAME(pkg));
270 if (!SvNIOK(sv) && SvPOK(sv)) {
271 char *str = SvPVx(sv,len);
274 /* XXX could DWIM "1.2.3" here */
275 if (!isDIGIT(str[len]) && str[len] != '.' && str[len] != '_')
279 if (SvNOK(req) && SvPOK(req)) {
280 /* they said C<use Foo v1.2.3> and $Foo::VERSION
281 * doesn't look like a float: do string compare */
282 if (sv_cmp(req,sv) == 1) {
283 Perl_croak(aTHX_ "%s v%"VDf" required--"
284 "this is only v%"VDf,
285 HvNAME(pkg), req, sv);
289 /* they said C<use Foo 1.002_003> and $Foo::VERSION
290 * doesn't look like a float: force numeric compare */
291 (void)SvUPGRADE(sv, SVt_PVNV);
292 SvNVX(sv) = str_to_version(sv);
297 /* if we get here, we're looking for a numeric comparison,
298 * so force the required version into a float, even if they
299 * said C<use Foo v1.2.3> */
300 if (SvNOK(req) && SvPOK(req)) {
302 req = sv_newmortal();
306 if (SvNV(req) > SvNV(sv))
307 Perl_croak(aTHX_ "%s version %s required--this is only version %s",
308 HvNAME(pkg), SvPV(req,len), SvPV(sv,len));
321 Perl_croak(aTHX_ "Usage: utf8::valid(sv)");
326 char *s = SvPV(sv,len);
327 if (!SvUTF8(sv) || is_utf8_string((U8*)s,len))
340 Perl_croak(aTHX_ "Usage: utf8::encode(sv)");
353 Perl_croak(aTHX_ "Usage: utf8::decode(sv)");
358 RETVAL = sv_utf8_decode(sv);
359 ST(0) = boolSV(RETVAL);
369 Perl_croak(aTHX_ "Usage: utf8::upgrade(sv)");
375 RETVAL = sv_utf8_upgrade(sv);
376 XSprePUSH; PUSHi((IV)RETVAL);
381 XS(XS_utf8_downgrade)
384 if (items < 1 || items > 2)
385 Perl_croak(aTHX_ "Usage: utf8::downgrade(sv, failok=0)");
394 failok = (int)SvIV(ST(1));
397 RETVAL = sv_utf8_downgrade(sv, failok);
398 ST(0) = boolSV(RETVAL);
404 XS(XS_utf8_native_to_unicode)
410 Perl_croak(aTHX_ "Usage: utf8::native_to_unicode(sv)");
412 ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv)));
416 XS(XS_utf8_unicode_to_native)
422 Perl_croak(aTHX_ "Usage: utf8::unicode_to_native(sv)");
424 ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv)));