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