Test case for #10433/#10424.
[p5sagit/p5-mst-13.2.git] / universal.c
CommitLineData
6d4a7be2 1#include "EXTERN.h"
864dbfa3 2#define PERL_IN_UNIVERSAL_C
6d4a7be2 3#include "perl.h"
6d4a7be2 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
76e3520e 10STATIC SV *
cea2e8a9 11S_isa_lookup(pTHX_ HV *stash, const char *name, int len, int level)
6d4a7be2 12{
13 AV* av;
14 GV* gv;
15 GV** gvp;
16 HV* hv = Nullhv;
46e4b22b 17 SV* subgen = Nullsv;
6d4a7be2 18
19 if (!stash)
3280af22 20 return &PL_sv_undef;
6d4a7be2 21
46e4b22b 22 if (strEQ(HvNAME(stash), name))
3280af22 23 return &PL_sv_yes;
6d4a7be2 24
25 if (level > 100)
46e4b22b 26 Perl_croak(aTHX_ "Recursive inheritance detected in package '%s'",
27 HvNAME(stash));
6d4a7be2 28
29 gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, FALSE);
30
46e4b22b 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 }
6d4a7be2 49 }
50
51 gvp = (GV**)hv_fetch(stash,"ISA",3,FALSE);
46e4b22b 52
3280af22 53 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (av = GvAV(gv))) {
46e4b22b 54 if (!hv || !subgen) {
6d4a7be2 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
46e4b22b 62 if (!hv)
63 hv = GvHVn(gv);
64 if (!subgen) {
65 subgen = newSViv(PL_sub_generation);
66 GvSV(gv) = subgen;
67 }
6d4a7be2 68 }
46e4b22b 69 if (hv) {
6d4a7be2 70 SV** svp = AvARRAY(av);
93965878 71 /* NOTE: No support for tied ISA */
72 I32 items = AvFILLp(av) + 1;
6d4a7be2 73 while (items--) {
74 SV* sv = *svp++;
75 HV* basestash = gv_stashsv(sv, FALSE);
76 if (!basestash) {
599cee73 77 if (ckWARN(WARN_MISC))
cea2e8a9 78 Perl_warner(aTHX_ WARN_SYNTAX,
599cee73 79 "Can't locate package %s for @%s::ISA",
6d4a7be2 80 SvPVX(sv), HvNAME(stash));
81 continue;
82 }
46e4b22b 83 if (&PL_sv_yes == isa_lookup(basestash, name, len, level + 1)) {
3280af22 84 (void)hv_store(hv,name,len,&PL_sv_yes,0);
85 return &PL_sv_yes;
6d4a7be2 86 }
87 }
3280af22 88 (void)hv_store(hv,name,len,&PL_sv_no,0);
6d4a7be2 89 }
90 }
91
e09f3e01 92 return boolSV(strEQ(name, "UNIVERSAL"));
6d4a7be2 93}
94
954c1994 95/*
96=for apidoc sv_derived_from
97
98Returns a boolean indicating whether the SV is derived from the specified
99class. This is the function that implements C<UNIVERSAL::isa>. It works
100for class names as well as for objects.
101
102=cut
103*/
104
55497cff 105bool
864dbfa3 106Perl_sv_derived_from(pTHX_ SV *sv, const char *name)
55497cff 107{
55497cff 108 char *type;
109 HV *stash;
46e4b22b 110
55497cff 111 stash = Nullhv;
112 type = Nullch;
46e4b22b 113
55497cff 114 if (SvGMAGICAL(sv))
115 mg_get(sv) ;
116
117 if (SvROK(sv)) {
118 sv = SvRV(sv);
119 type = sv_reftype(sv,0);
46e4b22b 120 if (SvOBJECT(sv))
55497cff 121 stash = SvSTASH(sv);
122 }
123 else {
124 stash = gv_stashsv(sv, FALSE);
125 }
46e4b22b 126
55497cff 127 return (type && strEQ(type,name)) ||
3280af22 128 (stash && isa_lookup(stash, name, strlen(name), 0) == &PL_sv_yes)
55497cff 129 ? TRUE
130 : FALSE ;
55497cff 131}
132
1b026014 133#include "XSUB.h"
134
0cb96387 135void XS_UNIVERSAL_isa(pTHXo_ CV *cv);
136void XS_UNIVERSAL_can(pTHXo_ CV *cv);
137void XS_UNIVERSAL_VERSION(pTHXo_ CV *cv);
1b026014 138XS(XS_utf8_valid);
139XS(XS_utf8_encode);
140XS(XS_utf8_decode);
141XS(XS_utf8_upgrade);
142XS(XS_utf8_downgrade);
143XS(XS_utf8_unicode_to_native);
144XS(XS_utf8_native_to_unicode);
0cb96387 145
146void
147Perl_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);
1b026014 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);
0cb96387 161}
162
55497cff 163
6d4a7be2 164XS(XS_UNIVERSAL_isa)
165{
166 dXSARGS;
55497cff 167 SV *sv;
168 char *name;
2d8e6c8d 169 STRLEN n_a;
6d4a7be2 170
171 if (items != 2)
cea2e8a9 172 Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)");
6d4a7be2 173
174 sv = ST(0);
f8f70380 175
d3f7f2b2 176 if (SvGMAGICAL(sv))
177 mg_get(sv);
178
aca069ec 179 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))))
f8f70380 180 XSRETURN_UNDEF;
181
2d8e6c8d 182 name = (char *)SvPV(ST(1),n_a);
6d4a7be2 183
54310121 184 ST(0) = boolSV(sv_derived_from(sv, name));
6d4a7be2 185 XSRETURN(1);
186}
187
6d4a7be2 188XS(XS_UNIVERSAL_can)
189{
190 dXSARGS;
191 SV *sv;
192 char *name;
193 SV *rv;
6f08146e 194 HV *pkg = NULL;
2d8e6c8d 195 STRLEN n_a;
6d4a7be2 196
197 if (items != 2)
cea2e8a9 198 Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)");
6d4a7be2 199
200 sv = ST(0);
f8f70380 201
d3f7f2b2 202 if (SvGMAGICAL(sv))
203 mg_get(sv);
204
aca069ec 205 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))))
f8f70380 206 XSRETURN_UNDEF;
207
2d8e6c8d 208 name = (char *)SvPV(ST(1),n_a);
3280af22 209 rv = &PL_sv_undef;
6d4a7be2 210
46e4b22b 211 if (SvROK(sv)) {
6f08146e 212 sv = (SV*)SvRV(sv);
46e4b22b 213 if (SvOBJECT(sv))
6f08146e 214 pkg = SvSTASH(sv);
215 }
216 else {
217 pkg = gv_stashsv(sv, FALSE);
218 }
219
220 if (pkg) {
dc848c6f 221 GV *gv = gv_fetchmethod_autoload(pkg, name, FALSE);
222 if (gv && isGV(gv))
223 rv = sv_2mortal(newRV((SV*)GvCV(gv)));
6d4a7be2 224 }
225
226 ST(0) = rv;
227 XSRETURN(1);
228}
229
6d4a7be2 230XS(XS_UNIVERSAL_VERSION)
231{
232 dXSARGS;
233 HV *pkg;
234 GV **gvp;
235 GV *gv;
236 SV *sv;
237 char *undef;
238
1571675a 239 if (SvROK(ST(0))) {
6d4a7be2 240 sv = (SV*)SvRV(ST(0));
1571675a 241 if (!SvOBJECT(sv))
cea2e8a9 242 Perl_croak(aTHX_ "Cannot find version of an unblessed reference");
6d4a7be2 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
d4bea2fb 251 if (gvp && isGV(gv = *gvp) && SvOK(sv = GvSV(gv))) {
6d4a7be2 252 SV *nsv = sv_newmortal();
253 sv_setsv(nsv, sv);
254 sv = nsv;
255 undef = Nullch;
256 }
257 else {
3280af22 258 sv = (SV*)&PL_sv_undef;
6d4a7be2 259 undef = "(undef)";
260 }
261
1571675a 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) {
4305d8ab 279 if (SvNOK(req) && SvPOK(req)) {
1571675a 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) {
d2560b70 283 Perl_croak(aTHX_ "%s v%"VDf" required--"
284 "this is only v%"VDf,
1571675a 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 */
155aba94 291 (void)SvUPGRADE(sv, SVt_PVNV);
1571675a 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> */
4305d8ab 300 if (SvNOK(req) && SvPOK(req)) {
1571675a 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(req,len), SvPV(sv,len));
2d8e6c8d 309 }
6d4a7be2 310
1571675a 311finish:
6d4a7be2 312 ST(0) = sv;
313
314 XSRETURN(1);
315}
316
1b026014 317XS(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
336XS(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
349XS(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
365XS(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
381XS(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
404XS(XS_utf8_native_to_unicode)
405{
406 dXSARGS;
407 UV uv = SvUV(ST(0));
b7953727 408
409 if (items > 1)
410 Perl_croak(aTHX_ "Usage: utf8::native_to_unicode(sv)");
411
1b026014 412 ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv)));
413 XSRETURN(1);
414}
415
416XS(XS_utf8_unicode_to_native)
417{
418 dXSARGS;
419 UV uv = SvUV(ST(0));
b7953727 420
421 if (items > 1)
422 Perl_croak(aTHX_ "Usage: utf8::unicode_to_native(sv)");
423
1b026014 424 ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv)));
425 XSRETURN(1);
426}
427
428