Re: bless() bug ? Why fails reblessing of 'main::Object' to 'Object' ?
[p5sagit/p5-mst-13.2.git] / universal.c
1 #include "EXTERN.h"
2 #define PERL_IN_UNIVERSAL_C
3 #include "perl.h"
4
5 /*
6  * Contributed by Graham Barr  <Graham.Barr@tiuk.ti.com>
7  * The main guts of traverse_isa was actually copied from gv_fetchmeth
8  */
9
10 STATIC SV *
11 S_isa_lookup(pTHX_ HV *stash, const char *name, HV* name_stash,
12              int len, int level)
13 {
14     AV* av;
15     GV* gv;
16     GV** gvp;
17     HV* hv = Nullhv;
18     SV* subgen = Nullsv;
19
20     /* A stash/class can go by many names (ie. User == main::User), so 
21        we compare the stash itself just in case */
22     if (name_stash && (stash == name_stash))
23         return &PL_sv_yes;
24
25     if (strEQ(HvNAME(stash), name))
26         return &PL_sv_yes;
27
28     if (level > 100)
29         Perl_croak(aTHX_ "Recursive inheritance detected in package '%s'",
30                    HvNAME(stash));
31
32     gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, FALSE);
33
34     if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (subgen = GvSV(gv))
35         && (hv = GvHV(gv)))
36     {
37         if (SvIV(subgen) == PL_sub_generation) {
38             SV* sv;
39             SV** svp = (SV**)hv_fetch(hv, name, len, FALSE);
40             if (svp && (sv = *svp) != (SV*)&PL_sv_undef) {
41                 DEBUG_o( Perl_deb(aTHX_ "Using cached ISA %s for package %s\n",
42                                   name, HvNAME(stash)) );
43                 return sv;
44             }
45         }
46         else {
47             DEBUG_o( Perl_deb(aTHX_ "ISA Cache in package %s is stale\n",
48                               HvNAME(stash)) );
49             hv_clear(hv);
50             sv_setiv(subgen, PL_sub_generation);
51         }
52     }
53
54     gvp = (GV**)hv_fetch(stash,"ISA",3,FALSE);
55
56     if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (av = GvAV(gv))) {
57         if (!hv || !subgen) {
58             gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, TRUE);
59
60             gv = *gvp;
61
62             if (SvTYPE(gv) != SVt_PVGV)
63                 gv_init(gv, stash, "::ISA::CACHE::", 14, TRUE);
64
65             if (!hv)
66                 hv = GvHVn(gv);
67             if (!subgen) {
68                 subgen = newSViv(PL_sub_generation);
69                 GvSV(gv) = subgen;
70             }
71         }
72         if (hv) {
73             SV** svp = AvARRAY(av);
74             /* NOTE: No support for tied ISA */
75             I32 items = AvFILLp(av) + 1;
76             while (items--) {
77                 SV* sv = *svp++;
78                 HV* basestash = gv_stashsv(sv, FALSE);
79                 if (!basestash) {
80                     if (ckWARN(WARN_MISC))
81                         Perl_warner(aTHX_ WARN_SYNTAX,
82                              "Can't locate package %s for @%s::ISA",
83                             SvPVX(sv), HvNAME(stash));
84                     continue;
85                 }
86                 if (&PL_sv_yes == isa_lookup(basestash, name, name_stash, 
87                                              len, level + 1)) {
88                     (void)hv_store(hv,name,len,&PL_sv_yes,0);
89                     return &PL_sv_yes;
90                 }
91             }
92             (void)hv_store(hv,name,len,&PL_sv_no,0);
93         }
94     }
95
96     return boolSV(strEQ(name, "UNIVERSAL"));
97 }
98
99 /*
100 =head1 SV Manipulation Functions
101
102 =for apidoc sv_derived_from
103
104 Returns a boolean indicating whether the SV is derived from the specified
105 class.  This is the function that implements C<UNIVERSAL::isa>.  It works
106 for class names as well as for objects.
107
108 =cut
109 */
110
111 bool
112 Perl_sv_derived_from(pTHX_ SV *sv, const char *name)
113 {
114     char *type;
115     HV *stash;
116     HV *name_stash;
117
118     stash = Nullhv;
119     type = Nullch;
120
121     if (SvGMAGICAL(sv))
122         mg_get(sv) ;
123
124     if (SvROK(sv)) {
125         sv = SvRV(sv);
126         type = sv_reftype(sv,0);
127         if (SvOBJECT(sv))
128             stash = SvSTASH(sv);
129     }
130     else {
131         stash = gv_stashsv(sv, FALSE);
132     }
133
134     name_stash = gv_stashpv(name, FALSE);
135
136     return (type && strEQ(type,name)) ||
137             (stash && isa_lookup(stash, name, name_stash, strlen(name), 0) 
138              == &PL_sv_yes)
139         ? TRUE
140         : FALSE ;
141 }
142
143 #include "XSUB.h"
144
145 void XS_UNIVERSAL_isa(pTHX_ CV *cv);
146 void XS_UNIVERSAL_can(pTHX_ CV *cv);
147 void XS_UNIVERSAL_VERSION(pTHX_ CV *cv);
148 XS(XS_utf8_valid);
149 XS(XS_utf8_encode);
150 XS(XS_utf8_decode);
151 XS(XS_utf8_upgrade);
152 XS(XS_utf8_downgrade);
153 XS(XS_utf8_unicode_to_native);
154 XS(XS_utf8_native_to_unicode);
155 XS(XS_access_readonly);
156
157 void
158 Perl_boot_core_UNIVERSAL(pTHX)
159 {
160     char *file = __FILE__;
161
162     newXS("UNIVERSAL::isa",             XS_UNIVERSAL_isa,         file);
163     newXS("UNIVERSAL::can",             XS_UNIVERSAL_can,         file);
164     newXS("UNIVERSAL::VERSION",         XS_UNIVERSAL_VERSION,     file);
165     newXS("utf8::valid", XS_utf8_valid, file);
166     newXS("utf8::encode", XS_utf8_encode, file);
167     newXS("utf8::decode", XS_utf8_decode, file);
168     newXS("utf8::upgrade", XS_utf8_upgrade, file);
169     newXS("utf8::downgrade", XS_utf8_downgrade, file);
170     newXS("utf8::native_to_unicode", XS_utf8_native_to_unicode, file);
171     newXS("utf8::unicode_to_native", XS_utf8_unicode_to_native, file);
172     newXSproto("access::readonly",XS_access_readonly, file, "\\[$%@];$");
173 }
174
175
176 XS(XS_UNIVERSAL_isa)
177 {
178     dXSARGS;
179     SV *sv;
180     char *name;
181     STRLEN n_a;
182
183     if (items != 2)
184         Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)");
185
186     sv = ST(0);
187
188     if (SvGMAGICAL(sv))
189         mg_get(sv);
190
191     if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))))
192         XSRETURN_UNDEF;
193
194     name = (char *)SvPV(ST(1),n_a);
195
196     ST(0) = boolSV(sv_derived_from(sv, name));
197     XSRETURN(1);
198 }
199
200 XS(XS_UNIVERSAL_can)
201 {
202     dXSARGS;
203     SV   *sv;
204     char *name;
205     SV   *rv;
206     HV   *pkg = NULL;
207     STRLEN n_a;
208
209     if (items != 2)
210         Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)");
211
212     sv = ST(0);
213
214     if (SvGMAGICAL(sv))
215         mg_get(sv);
216
217     if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))))
218         XSRETURN_UNDEF;
219
220     name = (char *)SvPV(ST(1),n_a);
221     rv = &PL_sv_undef;
222
223     if (SvROK(sv)) {
224         sv = (SV*)SvRV(sv);
225         if (SvOBJECT(sv))
226             pkg = SvSTASH(sv);
227     }
228     else {
229         pkg = gv_stashsv(sv, FALSE);
230     }
231
232     if (pkg) {
233         GV *gv = gv_fetchmethod_autoload(pkg, name, FALSE);
234         if (gv && isGV(gv))
235             rv = sv_2mortal(newRV((SV*)GvCV(gv)));
236     }
237
238     ST(0) = rv;
239     XSRETURN(1);
240 }
241
242 XS(XS_UNIVERSAL_VERSION)
243 {
244     dXSARGS;
245     HV *pkg;
246     GV **gvp;
247     GV *gv;
248     SV *sv;
249     char *undef;
250
251     if (SvROK(ST(0))) {
252         sv = (SV*)SvRV(ST(0));
253         if (!SvOBJECT(sv))
254             Perl_croak(aTHX_ "Cannot find version of an unblessed reference");
255         pkg = SvSTASH(sv);
256     }
257     else {
258         pkg = gv_stashsv(ST(0), FALSE);
259     }
260
261     gvp = pkg ? (GV**)hv_fetch(pkg,"VERSION",7,FALSE) : Null(GV**);
262
263     if (gvp && isGV(gv = *gvp) && SvOK(sv = GvSV(gv))) {
264         SV *nsv = sv_newmortal();
265         sv_setsv(nsv, sv);
266         sv = nsv;
267         undef = Nullch;
268     }
269     else {
270         sv = (SV*)&PL_sv_undef;
271         undef = "(undef)";
272     }
273
274     if (items > 1) {
275         STRLEN len;
276         SV *req = ST(1);
277
278         if (undef)
279             Perl_croak(aTHX_ "%s does not define $%s::VERSION--version check failed",
280                        HvNAME(pkg), HvNAME(pkg));
281
282         if (!SvNIOK(sv) && SvPOK(sv)) {
283             char *str = SvPVx(sv,len);
284             while (len) {
285                 --len;
286                 /* XXX could DWIM "1.2.3" here */
287                 if (!isDIGIT(str[len]) && str[len] != '.' && str[len] != '_')
288                     break;
289             }
290             if (len) {
291                 if (SvNOK(req) && SvPOK(req)) {
292                     /* they said C<use Foo v1.2.3> and $Foo::VERSION
293                      * doesn't look like a float: do string compare */
294                     if (sv_cmp(req,sv) == 1) {
295                         Perl_croak(aTHX_ "%s v%"VDf" required--"
296                                    "this is only v%"VDf,
297                                    HvNAME(pkg), req, sv);
298                     }
299                     goto finish;
300                 }
301                 /* they said C<use Foo 1.002_003> and $Foo::VERSION
302                  * doesn't look like a float: force numeric compare */
303                 (void)SvUPGRADE(sv, SVt_PVNV);
304                 SvNVX(sv) = str_to_version(sv);
305                 SvPOK_off(sv);
306                 SvNOK_on(sv);
307             }
308         }
309         /* if we get here, we're looking for a numeric comparison,
310          * so force the required version into a float, even if they
311          * said C<use Foo v1.2.3> */
312         if (SvNOK(req) && SvPOK(req)) {
313             NV n = SvNV(req);
314             req = sv_newmortal();
315             sv_setnv(req, n);
316         }
317
318         if (SvNV(req) > SvNV(sv))
319             Perl_croak(aTHX_ "%s version %s required--this is only version %s",
320                        HvNAME(pkg), SvPV_nolen(req), SvPV_nolen(sv));
321     }
322
323 finish:
324     ST(0) = sv;
325
326     XSRETURN(1);
327 }
328
329 XS(XS_utf8_valid)
330 {
331     dXSARGS;
332     if (items != 1)
333         Perl_croak(aTHX_ "Usage: utf8::valid(sv)");
334     {
335         SV *    sv = ST(0);
336  {
337   STRLEN len;
338   char *s = SvPV(sv,len);
339   if (!SvUTF8(sv) || is_utf8_string((U8*)s,len))
340    XSRETURN_YES;
341   else
342    XSRETURN_NO;
343  }
344     }
345     XSRETURN_EMPTY;
346 }
347
348 XS(XS_utf8_encode)
349 {
350     dXSARGS;
351     if (items != 1)
352         Perl_croak(aTHX_ "Usage: utf8::encode(sv)");
353     {
354         SV *    sv = ST(0);
355
356         sv_utf8_encode(sv);
357     }
358     XSRETURN_EMPTY;
359 }
360
361 XS(XS_utf8_decode)
362 {
363     dXSARGS;
364     if (items != 1)
365         Perl_croak(aTHX_ "Usage: utf8::decode(sv)");
366     {
367         SV *    sv = ST(0);
368         bool    RETVAL;
369
370         RETVAL = sv_utf8_decode(sv);
371         ST(0) = boolSV(RETVAL);
372         sv_2mortal(ST(0));
373     }
374     XSRETURN(1);
375 }
376
377 XS(XS_utf8_upgrade)
378 {
379     dXSARGS;
380     if (items != 1)
381         Perl_croak(aTHX_ "Usage: utf8::upgrade(sv)");
382     {
383         SV *    sv = ST(0);
384         STRLEN  RETVAL;
385         dXSTARG;
386
387         RETVAL = sv_utf8_upgrade(sv);
388         XSprePUSH; PUSHi((IV)RETVAL);
389     }
390     XSRETURN(1);
391 }
392
393 XS(XS_utf8_downgrade)
394 {
395     dXSARGS;
396     if (items < 1 || items > 2)
397         Perl_croak(aTHX_ "Usage: utf8::downgrade(sv, failok=0)");
398     {
399         SV *    sv = ST(0);
400         bool    failok;
401         bool    RETVAL;
402
403         if (items < 2)
404             failok = 0;
405         else {
406             failok = (int)SvIV(ST(1));
407         }
408
409         RETVAL = sv_utf8_downgrade(sv, failok);
410         ST(0) = boolSV(RETVAL);
411         sv_2mortal(ST(0));
412     }
413     XSRETURN(1);
414 }
415
416 XS(XS_utf8_native_to_unicode)
417 {
418  dXSARGS;
419  UV uv = SvUV(ST(0));
420
421  if (items > 1)
422      Perl_croak(aTHX_ "Usage: utf8::native_to_unicode(sv)");
423
424  ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv)));
425  XSRETURN(1);
426 }
427
428 XS(XS_utf8_unicode_to_native)
429 {
430  dXSARGS;
431  UV uv = SvUV(ST(0));
432
433  if (items > 1)
434      Perl_croak(aTHX_ "Usage: utf8::unicode_to_native(sv)");
435
436  ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv)));
437  XSRETURN(1);
438 }
439
440 XS(XS_access_readonly)
441 {
442     dXSARGS;
443     SV *sv = SvRV(ST(0));
444     IV old = SvREADONLY(sv);
445     if (items == 2) {
446         if (SvTRUE(ST(1))) {
447             SvREADONLY_on(sv);
448         }
449         else {
450             SvREADONLY_off(sv);
451         }
452     }
453     if (old)
454         XSRETURN_YES;
455     else
456         XSRETURN_NO;
457 }
458