Commit | Line | Data |
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 |
10 | STATIC SV * |
cea2e8a9 |
11 | S_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 | |
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 | |
55497cff |
105 | bool |
864dbfa3 |
106 | Perl_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 | |
acfe0abc |
135 | void XS_UNIVERSAL_isa(pTHX_ CV *cv); |
136 | void XS_UNIVERSAL_can(pTHX_ CV *cv); |
137 | void XS_UNIVERSAL_VERSION(pTHX_ CV *cv); |
1b026014 |
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); |
1b1f1335 |
145 | XS(XS_access_readonly); |
0cb96387 |
146 | |
147 | void |
148 | Perl_boot_core_UNIVERSAL(pTHX) |
149 | { |
150 | char *file = __FILE__; |
151 | |
152 | newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file); |
153 | newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file); |
154 | newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file); |
1b026014 |
155 | newXS("utf8::valid", XS_utf8_valid, file); |
156 | newXS("utf8::encode", XS_utf8_encode, file); |
157 | newXS("utf8::decode", XS_utf8_decode, file); |
158 | newXS("utf8::upgrade", XS_utf8_upgrade, file); |
159 | newXS("utf8::downgrade", XS_utf8_downgrade, file); |
160 | newXS("utf8::native_to_unicode", XS_utf8_native_to_unicode, file); |
161 | newXS("utf8::unicode_to_native", XS_utf8_unicode_to_native, file); |
1b1f1335 |
162 | newXSproto("access::readonly",XS_access_readonly, file, "\\[$%@];$"); |
0cb96387 |
163 | } |
164 | |
55497cff |
165 | |
6d4a7be2 |
166 | XS(XS_UNIVERSAL_isa) |
167 | { |
168 | dXSARGS; |
55497cff |
169 | SV *sv; |
170 | char *name; |
2d8e6c8d |
171 | STRLEN n_a; |
6d4a7be2 |
172 | |
173 | if (items != 2) |
cea2e8a9 |
174 | Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)"); |
6d4a7be2 |
175 | |
176 | sv = ST(0); |
f8f70380 |
177 | |
d3f7f2b2 |
178 | if (SvGMAGICAL(sv)) |
179 | mg_get(sv); |
180 | |
aca069ec |
181 | if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv)))) |
f8f70380 |
182 | XSRETURN_UNDEF; |
183 | |
2d8e6c8d |
184 | name = (char *)SvPV(ST(1),n_a); |
6d4a7be2 |
185 | |
54310121 |
186 | ST(0) = boolSV(sv_derived_from(sv, name)); |
6d4a7be2 |
187 | XSRETURN(1); |
188 | } |
189 | |
6d4a7be2 |
190 | XS(XS_UNIVERSAL_can) |
191 | { |
192 | dXSARGS; |
193 | SV *sv; |
194 | char *name; |
195 | SV *rv; |
6f08146e |
196 | HV *pkg = NULL; |
2d8e6c8d |
197 | STRLEN n_a; |
6d4a7be2 |
198 | |
199 | if (items != 2) |
cea2e8a9 |
200 | Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)"); |
6d4a7be2 |
201 | |
202 | sv = ST(0); |
f8f70380 |
203 | |
d3f7f2b2 |
204 | if (SvGMAGICAL(sv)) |
205 | mg_get(sv); |
206 | |
aca069ec |
207 | if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv)))) |
f8f70380 |
208 | XSRETURN_UNDEF; |
209 | |
2d8e6c8d |
210 | name = (char *)SvPV(ST(1),n_a); |
3280af22 |
211 | rv = &PL_sv_undef; |
6d4a7be2 |
212 | |
46e4b22b |
213 | if (SvROK(sv)) { |
6f08146e |
214 | sv = (SV*)SvRV(sv); |
46e4b22b |
215 | if (SvOBJECT(sv)) |
6f08146e |
216 | pkg = SvSTASH(sv); |
217 | } |
218 | else { |
219 | pkg = gv_stashsv(sv, FALSE); |
220 | } |
221 | |
222 | if (pkg) { |
dc848c6f |
223 | GV *gv = gv_fetchmethod_autoload(pkg, name, FALSE); |
224 | if (gv && isGV(gv)) |
225 | rv = sv_2mortal(newRV((SV*)GvCV(gv))); |
6d4a7be2 |
226 | } |
227 | |
228 | ST(0) = rv; |
229 | XSRETURN(1); |
230 | } |
231 | |
6d4a7be2 |
232 | XS(XS_UNIVERSAL_VERSION) |
233 | { |
234 | dXSARGS; |
235 | HV *pkg; |
236 | GV **gvp; |
237 | GV *gv; |
238 | SV *sv; |
239 | char *undef; |
240 | |
1571675a |
241 | if (SvROK(ST(0))) { |
6d4a7be2 |
242 | sv = (SV*)SvRV(ST(0)); |
1571675a |
243 | if (!SvOBJECT(sv)) |
cea2e8a9 |
244 | Perl_croak(aTHX_ "Cannot find version of an unblessed reference"); |
6d4a7be2 |
245 | pkg = SvSTASH(sv); |
246 | } |
247 | else { |
248 | pkg = gv_stashsv(ST(0), FALSE); |
249 | } |
250 | |
251 | gvp = pkg ? (GV**)hv_fetch(pkg,"VERSION",7,FALSE) : Null(GV**); |
252 | |
d4bea2fb |
253 | if (gvp && isGV(gv = *gvp) && SvOK(sv = GvSV(gv))) { |
6d4a7be2 |
254 | SV *nsv = sv_newmortal(); |
255 | sv_setsv(nsv, sv); |
256 | sv = nsv; |
257 | undef = Nullch; |
258 | } |
259 | else { |
3280af22 |
260 | sv = (SV*)&PL_sv_undef; |
6d4a7be2 |
261 | undef = "(undef)"; |
262 | } |
263 | |
1571675a |
264 | if (items > 1) { |
265 | STRLEN len; |
266 | SV *req = ST(1); |
267 | |
268 | if (undef) |
269 | Perl_croak(aTHX_ "%s does not define $%s::VERSION--version check failed", |
270 | HvNAME(pkg), HvNAME(pkg)); |
271 | |
272 | if (!SvNIOK(sv) && SvPOK(sv)) { |
273 | char *str = SvPVx(sv,len); |
274 | while (len) { |
275 | --len; |
276 | /* XXX could DWIM "1.2.3" here */ |
277 | if (!isDIGIT(str[len]) && str[len] != '.' && str[len] != '_') |
278 | break; |
279 | } |
280 | if (len) { |
4305d8ab |
281 | if (SvNOK(req) && SvPOK(req)) { |
1571675a |
282 | /* they said C<use Foo v1.2.3> and $Foo::VERSION |
283 | * doesn't look like a float: do string compare */ |
284 | if (sv_cmp(req,sv) == 1) { |
d2560b70 |
285 | Perl_croak(aTHX_ "%s v%"VDf" required--" |
286 | "this is only v%"VDf, |
1571675a |
287 | HvNAME(pkg), req, sv); |
288 | } |
289 | goto finish; |
290 | } |
291 | /* they said C<use Foo 1.002_003> and $Foo::VERSION |
292 | * doesn't look like a float: force numeric compare */ |
155aba94 |
293 | (void)SvUPGRADE(sv, SVt_PVNV); |
1571675a |
294 | SvNVX(sv) = str_to_version(sv); |
295 | SvPOK_off(sv); |
296 | SvNOK_on(sv); |
297 | } |
298 | } |
299 | /* if we get here, we're looking for a numeric comparison, |
300 | * so force the required version into a float, even if they |
301 | * said C<use Foo v1.2.3> */ |
4305d8ab |
302 | if (SvNOK(req) && SvPOK(req)) { |
1571675a |
303 | NV n = SvNV(req); |
304 | req = sv_newmortal(); |
305 | sv_setnv(req, n); |
306 | } |
307 | |
f6eb1a96 |
308 | if (SvNV(req) > SvNV(sv)) |
1571675a |
309 | Perl_croak(aTHX_ "%s version %s required--this is only version %s", |
f6eb1a96 |
310 | HvNAME(pkg), SvPV_nolen(req), SvPV_nolen(sv)); |
2d8e6c8d |
311 | } |
6d4a7be2 |
312 | |
1571675a |
313 | finish: |
6d4a7be2 |
314 | ST(0) = sv; |
315 | |
316 | XSRETURN(1); |
317 | } |
318 | |
1b026014 |
319 | XS(XS_utf8_valid) |
320 | { |
321 | dXSARGS; |
322 | if (items != 1) |
323 | Perl_croak(aTHX_ "Usage: utf8::valid(sv)"); |
324 | { |
325 | SV * sv = ST(0); |
326 | { |
327 | STRLEN len; |
328 | char *s = SvPV(sv,len); |
329 | if (!SvUTF8(sv) || is_utf8_string((U8*)s,len)) |
330 | XSRETURN_YES; |
331 | else |
332 | XSRETURN_NO; |
333 | } |
334 | } |
335 | XSRETURN_EMPTY; |
336 | } |
337 | |
338 | XS(XS_utf8_encode) |
339 | { |
340 | dXSARGS; |
341 | if (items != 1) |
342 | Perl_croak(aTHX_ "Usage: utf8::encode(sv)"); |
343 | { |
344 | SV * sv = ST(0); |
345 | |
346 | sv_utf8_encode(sv); |
347 | } |
348 | XSRETURN_EMPTY; |
349 | } |
350 | |
351 | XS(XS_utf8_decode) |
352 | { |
353 | dXSARGS; |
354 | if (items != 1) |
355 | Perl_croak(aTHX_ "Usage: utf8::decode(sv)"); |
356 | { |
357 | SV * sv = ST(0); |
358 | bool RETVAL; |
359 | |
360 | RETVAL = sv_utf8_decode(sv); |
361 | ST(0) = boolSV(RETVAL); |
362 | sv_2mortal(ST(0)); |
363 | } |
364 | XSRETURN(1); |
365 | } |
366 | |
367 | XS(XS_utf8_upgrade) |
368 | { |
369 | dXSARGS; |
370 | if (items != 1) |
371 | Perl_croak(aTHX_ "Usage: utf8::upgrade(sv)"); |
372 | { |
373 | SV * sv = ST(0); |
374 | STRLEN RETVAL; |
375 | dXSTARG; |
376 | |
377 | RETVAL = sv_utf8_upgrade(sv); |
378 | XSprePUSH; PUSHi((IV)RETVAL); |
379 | } |
380 | XSRETURN(1); |
381 | } |
382 | |
383 | XS(XS_utf8_downgrade) |
384 | { |
385 | dXSARGS; |
386 | if (items < 1 || items > 2) |
387 | Perl_croak(aTHX_ "Usage: utf8::downgrade(sv, failok=0)"); |
388 | { |
389 | SV * sv = ST(0); |
390 | bool failok; |
391 | bool RETVAL; |
392 | |
393 | if (items < 2) |
394 | failok = 0; |
395 | else { |
396 | failok = (int)SvIV(ST(1)); |
397 | } |
398 | |
399 | RETVAL = sv_utf8_downgrade(sv, failok); |
400 | ST(0) = boolSV(RETVAL); |
401 | sv_2mortal(ST(0)); |
402 | } |
403 | XSRETURN(1); |
404 | } |
405 | |
406 | XS(XS_utf8_native_to_unicode) |
407 | { |
408 | dXSARGS; |
409 | UV uv = SvUV(ST(0)); |
b7953727 |
410 | |
411 | if (items > 1) |
412 | Perl_croak(aTHX_ "Usage: utf8::native_to_unicode(sv)"); |
413 | |
1b026014 |
414 | ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv))); |
415 | XSRETURN(1); |
416 | } |
417 | |
418 | XS(XS_utf8_unicode_to_native) |
419 | { |
420 | dXSARGS; |
421 | UV uv = SvUV(ST(0)); |
b7953727 |
422 | |
423 | if (items > 1) |
424 | Perl_croak(aTHX_ "Usage: utf8::unicode_to_native(sv)"); |
425 | |
1b026014 |
426 | ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv))); |
427 | XSRETURN(1); |
428 | } |
429 | |
1b1f1335 |
430 | XS(XS_access_readonly) |
431 | { |
432 | dXSARGS; |
433 | SV *sv = SvRV(ST(0)); |
434 | IV old = SvREADONLY(sv); |
435 | if (items == 2) { |
436 | if (SvTRUE(ST(1))) { |
437 | SvREADONLY_on(sv); |
438 | } |
439 | else { |
440 | SvREADONLY_off(sv); |
441 | } |
442 | } |
443 | if (old) |
444 | XSRETURN_YES; |
445 | else |
446 | XSRETURN_NO; |
447 | } |
1b026014 |
448 | |