This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate mainline (in near desperate attempt to get Win32 to build...)
[perl5.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, int len, int level)
12 {
13     AV* av;
14     GV* gv;
15     GV** gvp;
16     HV* hv = Nullhv;
17     SV* subgen = Nullsv;
18
19     if (!stash)
20         return &PL_sv_undef;
21
22     if (strEQ(HvNAME(stash), name))
23         return &PL_sv_yes;
24
25     if (level > 100)
26         Perl_croak(aTHX_ "Recursive inheritance detected in package '%s'",
27                    HvNAME(stash));
28
29     gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, FALSE);
30
31     if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (subgen = GvSV(gv))
32         && (hv = GvHV(gv)))
33     {
34         if (SvIV(subgen) == PL_sub_generation) {
35             SV* sv;
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)) );
40                 return sv;
41             }
42         }
43         else {
44             DEBUG_o( Perl_deb(aTHX_ "ISA Cache in package %s is stale\n",
45                               HvNAME(stash)) );
46             hv_clear(hv);
47             sv_setiv(subgen, PL_sub_generation);
48         }
49     }
50
51     gvp = (GV**)hv_fetch(stash,"ISA",3,FALSE);
52
53     if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (av = GvAV(gv))) {
54         if (!hv || !subgen) {
55             gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, TRUE);
56
57             gv = *gvp;
58
59             if (SvTYPE(gv) != SVt_PVGV)
60                 gv_init(gv, stash, "::ISA::CACHE::", 14, TRUE);
61
62             if (!hv)
63                 hv = GvHVn(gv);
64             if (!subgen) {
65                 subgen = newSViv(PL_sub_generation);
66                 GvSV(gv) = subgen;
67             }
68         }
69         if (hv) {
70             SV** svp = AvARRAY(av);
71             /* NOTE: No support for tied ISA */
72             I32 items = AvFILLp(av) + 1;
73             while (items--) {
74                 SV* sv = *svp++;
75                 HV* basestash = gv_stashsv(sv, FALSE);
76                 if (!basestash) {
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));
81                     continue;
82                 }
83                 if (&PL_sv_yes == isa_lookup(basestash, name, len, level + 1)) {
84                     (void)hv_store(hv,name,len,&PL_sv_yes,0);
85                     return &PL_sv_yes;
86                 }
87             }
88             (void)hv_store(hv,name,len,&PL_sv_no,0);
89         }
90     }
91
92     return boolSV(strEQ(name, "UNIVERSAL"));
93 }
94
95 /*
96 =for apidoc sv_derived_from
97
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.
101
102 =cut
103 */
104
105 bool
106 Perl_sv_derived_from(pTHX_ SV *sv, const char *name)
107 {
108     char *type;
109     HV *stash;
110
111     stash = Nullhv;
112     type = Nullch;
113
114     if (SvGMAGICAL(sv))
115         mg_get(sv) ;
116
117     if (SvROK(sv)) {
118         sv = SvRV(sv);
119         type = sv_reftype(sv,0);
120         if (SvOBJECT(sv))
121             stash = SvSTASH(sv);
122     }
123     else {
124         stash = gv_stashsv(sv, FALSE);
125     }
126
127     return (type && strEQ(type,name)) ||
128             (stash && isa_lookup(stash, name, strlen(name), 0) == &PL_sv_yes)
129         ? TRUE
130         : FALSE ;
131 }
132
133 #include "XSUB.h"
134
135 void XS_UNIVERSAL_isa(pTHXo_ CV *cv);
136 void XS_UNIVERSAL_can(pTHXo_ CV *cv);
137 void XS_UNIVERSAL_VERSION(pTHXo_ CV *cv);
138 XS(XS_utf8_valid);
139 XS(XS_utf8_encode);
140 XS(XS_utf8_decode);
141 XS(XS_utf8_upgrade);
142 XS(XS_utf8_downgrade);
143 XS(XS_utf8_unicode_to_native);
144 XS(XS_utf8_native_to_unicode);
145
146 void
147 Perl_boot_core_UNIVERSAL(pTHX)
148 {
149     char *file = __FILE__;
150
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);
161 }
162
163
164 XS(XS_UNIVERSAL_isa)
165 {
166     dXSARGS;
167     SV *sv;
168     char *name;
169     STRLEN n_a;
170
171     if (items != 2)
172         Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)");
173
174     sv = ST(0);
175
176     if (SvGMAGICAL(sv))
177         mg_get(sv);
178
179     if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))))
180         XSRETURN_UNDEF;
181
182     name = (char *)SvPV(ST(1),n_a);
183
184     ST(0) = boolSV(sv_derived_from(sv, name));
185     XSRETURN(1);
186 }
187
188 XS(XS_UNIVERSAL_can)
189 {
190     dXSARGS;
191     SV   *sv;
192     char *name;
193     SV   *rv;
194     HV   *pkg = NULL;
195     STRLEN n_a;
196
197     if (items != 2)
198         Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)");
199
200     sv = ST(0);
201
202     if (SvGMAGICAL(sv))
203         mg_get(sv);
204
205     if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))))
206         XSRETURN_UNDEF;
207
208     name = (char *)SvPV(ST(1),n_a);
209     rv = &PL_sv_undef;
210
211     if (SvROK(sv)) {
212         sv = (SV*)SvRV(sv);
213         if (SvOBJECT(sv))
214             pkg = SvSTASH(sv);
215     }
216     else {
217         pkg = gv_stashsv(sv, FALSE);
218     }
219
220     if (pkg) {
221         GV *gv = gv_fetchmethod_autoload(pkg, name, FALSE);
222         if (gv && isGV(gv))
223             rv = sv_2mortal(newRV((SV*)GvCV(gv)));
224     }
225
226     ST(0) = rv;
227     XSRETURN(1);
228 }
229
230 XS(XS_UNIVERSAL_VERSION)
231 {
232     dXSARGS;
233     HV *pkg;
234     GV **gvp;
235     GV *gv;
236     SV *sv;
237     char *undef;
238
239     if (SvROK(ST(0))) {
240         sv = (SV*)SvRV(ST(0));
241         if (!SvOBJECT(sv))
242             Perl_croak(aTHX_ "Cannot find version of an unblessed reference");
243         pkg = SvSTASH(sv);
244     }
245     else {
246         pkg = gv_stashsv(ST(0), FALSE);
247     }
248
249     gvp = pkg ? (GV**)hv_fetch(pkg,"VERSION",7,FALSE) : Null(GV**);
250
251     if (gvp && isGV(gv = *gvp) && SvOK(sv = GvSV(gv))) {
252         SV *nsv = sv_newmortal();
253         sv_setsv(nsv, sv);
254         sv = nsv;
255         undef = Nullch;
256     }
257     else {
258         sv = (SV*)&PL_sv_undef;
259         undef = "(undef)";
260     }
261
262     if (items > 1) {
263         STRLEN len;
264         SV *req = ST(1);
265
266         if (undef)
267             Perl_croak(aTHX_ "%s does not define $%s::VERSION--version check failed",
268                        HvNAME(pkg), HvNAME(pkg));
269
270         if (!SvNIOK(sv) && SvPOK(sv)) {
271             char *str = SvPVx(sv,len);
272             while (len) {
273                 --len;
274                 /* XXX could DWIM "1.2.3" here */
275                 if (!isDIGIT(str[len]) && str[len] != '.' && str[len] != '_')
276                     break;
277             }
278             if (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);
286                     }
287                     goto finish;
288                 }
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);
293                 SvPOK_off(sv);
294                 SvNOK_on(sv);
295             }
296         }
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)) {
301             NV n = SvNV(req);
302             req = sv_newmortal();
303             sv_setnv(req, n);
304         }
305
306         if (SvNV(req) > SvNV(sv))
307             Perl_croak(aTHX_ "%s version %s required--this is only version %s",
308                        HvNAME(pkg), SvPV_nolen(req), SvPV_nolen(sv));
309     }
310
311 finish:
312     ST(0) = sv;
313
314     XSRETURN(1);
315 }
316
317 XS(XS_utf8_valid)
318 {
319     dXSARGS;
320     if (items != 1)
321         Perl_croak(aTHX_ "Usage: utf8::valid(sv)");
322     {
323         SV *    sv = ST(0);
324  {
325   STRLEN len;
326   char *s = SvPV(sv,len);
327   if (!SvUTF8(sv) || is_utf8_string((U8*)s,len))
328    XSRETURN_YES;
329   else
330    XSRETURN_NO;
331  }
332     }
333     XSRETURN_EMPTY;
334 }
335
336 XS(XS_utf8_encode)
337 {
338     dXSARGS;
339     if (items != 1)
340         Perl_croak(aTHX_ "Usage: utf8::encode(sv)");
341     {
342         SV *    sv = ST(0);
343
344         sv_utf8_encode(sv);
345     }
346     XSRETURN_EMPTY;
347 }
348
349 XS(XS_utf8_decode)
350 {
351     dXSARGS;
352     if (items != 1)
353         Perl_croak(aTHX_ "Usage: utf8::decode(sv)");
354     {
355         SV *    sv = ST(0);
356         bool    RETVAL;
357
358         RETVAL = sv_utf8_decode(sv);
359         ST(0) = boolSV(RETVAL);
360         sv_2mortal(ST(0));
361     }
362     XSRETURN(1);
363 }
364
365 XS(XS_utf8_upgrade)
366 {
367     dXSARGS;
368     if (items != 1)
369         Perl_croak(aTHX_ "Usage: utf8::upgrade(sv)");
370     {
371         SV *    sv = ST(0);
372         STRLEN  RETVAL;
373         dXSTARG;
374
375         RETVAL = sv_utf8_upgrade(sv);
376         XSprePUSH; PUSHi((IV)RETVAL);
377     }
378     XSRETURN(1);
379 }
380
381 XS(XS_utf8_downgrade)
382 {
383     dXSARGS;
384     if (items < 1 || items > 2)
385         Perl_croak(aTHX_ "Usage: utf8::downgrade(sv, failok=0)");
386     {
387         SV *    sv = ST(0);
388         bool    failok;
389         bool    RETVAL;
390
391         if (items < 2)
392             failok = 0;
393         else {
394             failok = (int)SvIV(ST(1));
395         }
396
397         RETVAL = sv_utf8_downgrade(sv, failok);
398         ST(0) = boolSV(RETVAL);
399         sv_2mortal(ST(0));
400     }
401     XSRETURN(1);
402 }
403
404 XS(XS_utf8_native_to_unicode)
405 {
406  dXSARGS;
407  UV uv = SvUV(ST(0));
408
409  if (items > 1)
410      Perl_croak(aTHX_ "Usage: utf8::native_to_unicode(sv)");
411
412  ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv)));
413  XSRETURN(1);
414 }
415
416 XS(XS_utf8_unicode_to_native)
417 {
418  dXSARGS;
419  UV uv = SvUV(ST(0));
420
421  if (items > 1)
422      Perl_croak(aTHX_ "Usage: utf8::unicode_to_native(sv)");
423
424  ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv)));
425  XSRETURN(1);
426 }
427
428