Commit | Line | Data |
d6376244 |
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 | |
d31a8517 |
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 | |
6d4a7be2 |
16 | #include "EXTERN.h" |
864dbfa3 |
17 | #define PERL_IN_UNIVERSAL_C |
6d4a7be2 |
18 | #include "perl.h" |
6d4a7be2 |
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 | |
76e3520e |
25 | STATIC SV * |
301daebc |
26 | S_isa_lookup(pTHX_ HV *stash, const char *name, HV* name_stash, |
27 | int len, int level) |
6d4a7be2 |
28 | { |
29 | AV* av; |
30 | GV* gv; |
31 | GV** gvp; |
32 | HV* hv = Nullhv; |
46e4b22b |
33 | SV* subgen = Nullsv; |
6d4a7be2 |
34 | |
301daebc |
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; |
6d4a7be2 |
39 | |
46e4b22b |
40 | if (strEQ(HvNAME(stash), name)) |
3280af22 |
41 | return &PL_sv_yes; |
6d4a7be2 |
42 | |
43 | if (level > 100) |
46e4b22b |
44 | Perl_croak(aTHX_ "Recursive inheritance detected in package '%s'", |
45 | HvNAME(stash)); |
6d4a7be2 |
46 | |
47 | gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, FALSE); |
48 | |
46e4b22b |
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 | } |
6d4a7be2 |
67 | } |
68 | |
69 | gvp = (GV**)hv_fetch(stash,"ISA",3,FALSE); |
46e4b22b |
70 | |
3280af22 |
71 | if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (av = GvAV(gv))) { |
46e4b22b |
72 | if (!hv || !subgen) { |
6d4a7be2 |
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 | |
46e4b22b |
80 | if (!hv) |
81 | hv = GvHVn(gv); |
82 | if (!subgen) { |
83 | subgen = newSViv(PL_sub_generation); |
84 | GvSV(gv) = subgen; |
85 | } |
6d4a7be2 |
86 | } |
46e4b22b |
87 | if (hv) { |
6d4a7be2 |
88 | SV** svp = AvARRAY(av); |
93965878 |
89 | /* NOTE: No support for tied ISA */ |
90 | I32 items = AvFILLp(av) + 1; |
6d4a7be2 |
91 | while (items--) { |
92 | SV* sv = *svp++; |
93 | HV* basestash = gv_stashsv(sv, FALSE); |
94 | if (!basestash) { |
599cee73 |
95 | if (ckWARN(WARN_MISC)) |
9014280d |
96 | Perl_warner(aTHX_ packWARN(WARN_SYNTAX), |
599cee73 |
97 | "Can't locate package %s for @%s::ISA", |
6d4a7be2 |
98 | SvPVX(sv), HvNAME(stash)); |
99 | continue; |
100 | } |
301daebc |
101 | if (&PL_sv_yes == isa_lookup(basestash, name, name_stash, |
102 | len, level + 1)) { |
3280af22 |
103 | (void)hv_store(hv,name,len,&PL_sv_yes,0); |
104 | return &PL_sv_yes; |
6d4a7be2 |
105 | } |
106 | } |
3280af22 |
107 | (void)hv_store(hv,name,len,&PL_sv_no,0); |
6d4a7be2 |
108 | } |
109 | } |
110 | |
e09f3e01 |
111 | return boolSV(strEQ(name, "UNIVERSAL")); |
6d4a7be2 |
112 | } |
113 | |
954c1994 |
114 | /* |
ccfc67b7 |
115 | =head1 SV Manipulation Functions |
116 | |
954c1994 |
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 | |
55497cff |
126 | bool |
864dbfa3 |
127 | Perl_sv_derived_from(pTHX_ SV *sv, const char *name) |
55497cff |
128 | { |
55497cff |
129 | char *type; |
130 | HV *stash; |
301daebc |
131 | HV *name_stash; |
46e4b22b |
132 | |
55497cff |
133 | stash = Nullhv; |
134 | type = Nullch; |
46e4b22b |
135 | |
55497cff |
136 | if (SvGMAGICAL(sv)) |
137 | mg_get(sv) ; |
138 | |
139 | if (SvROK(sv)) { |
140 | sv = SvRV(sv); |
141 | type = sv_reftype(sv,0); |
46e4b22b |
142 | if (SvOBJECT(sv)) |
55497cff |
143 | stash = SvSTASH(sv); |
144 | } |
145 | else { |
146 | stash = gv_stashsv(sv, FALSE); |
147 | } |
46e4b22b |
148 | |
301daebc |
149 | name_stash = gv_stashpv(name, FALSE); |
150 | |
55497cff |
151 | return (type && strEQ(type,name)) || |
301daebc |
152 | (stash && isa_lookup(stash, name, name_stash, strlen(name), 0) |
153 | == &PL_sv_yes) |
55497cff |
154 | ? TRUE |
155 | : FALSE ; |
55497cff |
156 | } |
157 | |
1b026014 |
158 | #include "XSUB.h" |
159 | |
acfe0abc |
160 | void XS_UNIVERSAL_isa(pTHX_ CV *cv); |
161 | void XS_UNIVERSAL_can(pTHX_ CV *cv); |
162 | void XS_UNIVERSAL_VERSION(pTHX_ CV *cv); |
1b026014 |
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); |
29569577 |
170 | XS(XS_Internals_SvREADONLY); |
171 | XS(XS_Internals_SvREFCNT); |
0cb96387 |
172 | |
173 | void |
174 | Perl_boot_core_UNIVERSAL(pTHX) |
175 | { |
176 | char *file = __FILE__; |
177 | |
178 | newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file); |
179 | newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file); |
180 | newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file); |
1b026014 |
181 | newXS("utf8::valid", XS_utf8_valid, file); |
182 | newXS("utf8::encode", XS_utf8_encode, file); |
183 | newXS("utf8::decode", XS_utf8_decode, file); |
184 | newXS("utf8::upgrade", XS_utf8_upgrade, file); |
185 | newXS("utf8::downgrade", XS_utf8_downgrade, file); |
186 | newXS("utf8::native_to_unicode", XS_utf8_native_to_unicode, file); |
187 | newXS("utf8::unicode_to_native", XS_utf8_unicode_to_native, file); |
29569577 |
188 | newXSproto("Internals::SvREADONLY",XS_Internals_SvREADONLY, file, "\\[$%@];$"); |
189 | newXSproto("Internals::SvREFCNT",XS_Internals_SvREFCNT, file, "\\[$%@];$"); |
0cb96387 |
190 | } |
191 | |
55497cff |
192 | |
6d4a7be2 |
193 | XS(XS_UNIVERSAL_isa) |
194 | { |
195 | dXSARGS; |
55497cff |
196 | SV *sv; |
197 | char *name; |
2d8e6c8d |
198 | STRLEN n_a; |
6d4a7be2 |
199 | |
200 | if (items != 2) |
cea2e8a9 |
201 | Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)"); |
6d4a7be2 |
202 | |
203 | sv = ST(0); |
f8f70380 |
204 | |
d3f7f2b2 |
205 | if (SvGMAGICAL(sv)) |
206 | mg_get(sv); |
207 | |
aca069ec |
208 | if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv)))) |
f8f70380 |
209 | XSRETURN_UNDEF; |
210 | |
2d8e6c8d |
211 | name = (char *)SvPV(ST(1),n_a); |
6d4a7be2 |
212 | |
54310121 |
213 | ST(0) = boolSV(sv_derived_from(sv, name)); |
6d4a7be2 |
214 | XSRETURN(1); |
215 | } |
216 | |
6d4a7be2 |
217 | XS(XS_UNIVERSAL_can) |
218 | { |
219 | dXSARGS; |
220 | SV *sv; |
221 | char *name; |
222 | SV *rv; |
6f08146e |
223 | HV *pkg = NULL; |
2d8e6c8d |
224 | STRLEN n_a; |
6d4a7be2 |
225 | |
226 | if (items != 2) |
cea2e8a9 |
227 | Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)"); |
6d4a7be2 |
228 | |
229 | sv = ST(0); |
f8f70380 |
230 | |
d3f7f2b2 |
231 | if (SvGMAGICAL(sv)) |
232 | mg_get(sv); |
233 | |
aca069ec |
234 | if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv)))) |
f8f70380 |
235 | XSRETURN_UNDEF; |
236 | |
2d8e6c8d |
237 | name = (char *)SvPV(ST(1),n_a); |
3280af22 |
238 | rv = &PL_sv_undef; |
6d4a7be2 |
239 | |
46e4b22b |
240 | if (SvROK(sv)) { |
6f08146e |
241 | sv = (SV*)SvRV(sv); |
46e4b22b |
242 | if (SvOBJECT(sv)) |
6f08146e |
243 | pkg = SvSTASH(sv); |
244 | } |
245 | else { |
246 | pkg = gv_stashsv(sv, FALSE); |
247 | } |
248 | |
249 | if (pkg) { |
dc848c6f |
250 | GV *gv = gv_fetchmethod_autoload(pkg, name, FALSE); |
251 | if (gv && isGV(gv)) |
252 | rv = sv_2mortal(newRV((SV*)GvCV(gv))); |
6d4a7be2 |
253 | } |
254 | |
255 | ST(0) = rv; |
256 | XSRETURN(1); |
257 | } |
258 | |
6d4a7be2 |
259 | XS(XS_UNIVERSAL_VERSION) |
260 | { |
261 | dXSARGS; |
262 | HV *pkg; |
263 | GV **gvp; |
264 | GV *gv; |
265 | SV *sv; |
266 | char *undef; |
267 | |
1571675a |
268 | if (SvROK(ST(0))) { |
6d4a7be2 |
269 | sv = (SV*)SvRV(ST(0)); |
1571675a |
270 | if (!SvOBJECT(sv)) |
cea2e8a9 |
271 | Perl_croak(aTHX_ "Cannot find version of an unblessed reference"); |
6d4a7be2 |
272 | pkg = SvSTASH(sv); |
273 | } |
274 | else { |
275 | pkg = gv_stashsv(ST(0), FALSE); |
276 | } |
277 | |
278 | gvp = pkg ? (GV**)hv_fetch(pkg,"VERSION",7,FALSE) : Null(GV**); |
279 | |
d4bea2fb |
280 | if (gvp && isGV(gv = *gvp) && SvOK(sv = GvSV(gv))) { |
6d4a7be2 |
281 | SV *nsv = sv_newmortal(); |
282 | sv_setsv(nsv, sv); |
283 | sv = nsv; |
284 | undef = Nullch; |
285 | } |
286 | else { |
3280af22 |
287 | sv = (SV*)&PL_sv_undef; |
6d4a7be2 |
288 | undef = "(undef)"; |
289 | } |
290 | |
1571675a |
291 | if (items > 1) { |
292 | STRLEN len; |
293 | SV *req = ST(1); |
294 | |
62658f4d |
295 | if (undef) { |
296 | if (pkg) |
297 | Perl_croak(aTHX_ |
298 | "%s does not define $%s::VERSION--version check failed", |
299 | HvNAME(pkg), HvNAME(pkg)); |
300 | else { |
301 | char *str = SvPVx(ST(0), len); |
302 | |
303 | Perl_croak(aTHX_ |
304 | "%s defines neither package nor VERSION--version check failed", str); |
305 | } |
306 | } |
1571675a |
307 | if (!SvNIOK(sv) && SvPOK(sv)) { |
308 | char *str = SvPVx(sv,len); |
309 | while (len) { |
310 | --len; |
311 | /* XXX could DWIM "1.2.3" here */ |
312 | if (!isDIGIT(str[len]) && str[len] != '.' && str[len] != '_') |
313 | break; |
314 | } |
315 | if (len) { |
4305d8ab |
316 | if (SvNOK(req) && SvPOK(req)) { |
1571675a |
317 | /* they said C<use Foo v1.2.3> and $Foo::VERSION |
318 | * doesn't look like a float: do string compare */ |
319 | if (sv_cmp(req,sv) == 1) { |
d2560b70 |
320 | Perl_croak(aTHX_ "%s v%"VDf" required--" |
321 | "this is only v%"VDf, |
1571675a |
322 | HvNAME(pkg), req, sv); |
323 | } |
324 | goto finish; |
325 | } |
326 | /* they said C<use Foo 1.002_003> and $Foo::VERSION |
327 | * doesn't look like a float: force numeric compare */ |
155aba94 |
328 | (void)SvUPGRADE(sv, SVt_PVNV); |
1571675a |
329 | SvNVX(sv) = str_to_version(sv); |
330 | SvPOK_off(sv); |
331 | SvNOK_on(sv); |
332 | } |
333 | } |
334 | /* if we get here, we're looking for a numeric comparison, |
335 | * so force the required version into a float, even if they |
336 | * said C<use Foo v1.2.3> */ |
4305d8ab |
337 | if (SvNOK(req) && SvPOK(req)) { |
1571675a |
338 | NV n = SvNV(req); |
339 | req = sv_newmortal(); |
340 | sv_setnv(req, n); |
341 | } |
342 | |
f6eb1a96 |
343 | if (SvNV(req) > SvNV(sv)) |
1571675a |
344 | Perl_croak(aTHX_ "%s version %s required--this is only version %s", |
f6eb1a96 |
345 | HvNAME(pkg), SvPV_nolen(req), SvPV_nolen(sv)); |
2d8e6c8d |
346 | } |
6d4a7be2 |
347 | |
1571675a |
348 | finish: |
6d4a7be2 |
349 | ST(0) = sv; |
350 | |
351 | XSRETURN(1); |
352 | } |
353 | |
1b026014 |
354 | XS(XS_utf8_valid) |
355 | { |
356 | dXSARGS; |
357 | if (items != 1) |
358 | Perl_croak(aTHX_ "Usage: utf8::valid(sv)"); |
359 | { |
360 | SV * sv = ST(0); |
361 | { |
362 | STRLEN len; |
363 | char *s = SvPV(sv,len); |
364 | if (!SvUTF8(sv) || is_utf8_string((U8*)s,len)) |
365 | XSRETURN_YES; |
366 | else |
367 | XSRETURN_NO; |
368 | } |
369 | } |
370 | XSRETURN_EMPTY; |
371 | } |
372 | |
373 | XS(XS_utf8_encode) |
374 | { |
375 | dXSARGS; |
376 | if (items != 1) |
377 | Perl_croak(aTHX_ "Usage: utf8::encode(sv)"); |
378 | { |
379 | SV * sv = ST(0); |
380 | |
381 | sv_utf8_encode(sv); |
382 | } |
383 | XSRETURN_EMPTY; |
384 | } |
385 | |
386 | XS(XS_utf8_decode) |
387 | { |
388 | dXSARGS; |
389 | if (items != 1) |
390 | Perl_croak(aTHX_ "Usage: utf8::decode(sv)"); |
391 | { |
392 | SV * sv = ST(0); |
393 | bool RETVAL; |
394 | |
395 | RETVAL = sv_utf8_decode(sv); |
396 | ST(0) = boolSV(RETVAL); |
397 | sv_2mortal(ST(0)); |
398 | } |
399 | XSRETURN(1); |
400 | } |
401 | |
402 | XS(XS_utf8_upgrade) |
403 | { |
404 | dXSARGS; |
405 | if (items != 1) |
406 | Perl_croak(aTHX_ "Usage: utf8::upgrade(sv)"); |
407 | { |
408 | SV * sv = ST(0); |
409 | STRLEN RETVAL; |
410 | dXSTARG; |
411 | |
412 | RETVAL = sv_utf8_upgrade(sv); |
413 | XSprePUSH; PUSHi((IV)RETVAL); |
414 | } |
415 | XSRETURN(1); |
416 | } |
417 | |
418 | XS(XS_utf8_downgrade) |
419 | { |
420 | dXSARGS; |
421 | if (items < 1 || items > 2) |
422 | Perl_croak(aTHX_ "Usage: utf8::downgrade(sv, failok=0)"); |
423 | { |
424 | SV * sv = ST(0); |
425 | bool failok; |
426 | bool RETVAL; |
427 | |
428 | if (items < 2) |
429 | failok = 0; |
430 | else { |
431 | failok = (int)SvIV(ST(1)); |
432 | } |
433 | |
434 | RETVAL = sv_utf8_downgrade(sv, failok); |
435 | ST(0) = boolSV(RETVAL); |
436 | sv_2mortal(ST(0)); |
437 | } |
438 | XSRETURN(1); |
439 | } |
440 | |
441 | XS(XS_utf8_native_to_unicode) |
442 | { |
443 | dXSARGS; |
444 | UV uv = SvUV(ST(0)); |
b7953727 |
445 | |
446 | if (items > 1) |
447 | Perl_croak(aTHX_ "Usage: utf8::native_to_unicode(sv)"); |
448 | |
1b026014 |
449 | ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv))); |
450 | XSRETURN(1); |
451 | } |
452 | |
453 | XS(XS_utf8_unicode_to_native) |
454 | { |
455 | dXSARGS; |
456 | UV uv = SvUV(ST(0)); |
b7953727 |
457 | |
458 | if (items > 1) |
459 | Perl_croak(aTHX_ "Usage: utf8::unicode_to_native(sv)"); |
460 | |
1b026014 |
461 | ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv))); |
462 | XSRETURN(1); |
463 | } |
464 | |
14a976d6 |
465 | XS(XS_Internals_SvREADONLY) /* This is dangerous stuff. */ |
29569577 |
466 | { |
467 | dXSARGS; |
468 | SV *sv = SvRV(ST(0)); |
469 | if (items == 1) { |
470 | if (SvREADONLY(sv)) |
471 | XSRETURN_YES; |
472 | else |
473 | XSRETURN_NO; |
474 | } |
475 | else if (items == 2) { |
476 | if (SvTRUE(ST(1))) { |
477 | SvREADONLY_on(sv); |
478 | XSRETURN_YES; |
479 | } |
480 | else { |
14a976d6 |
481 | /* I hope you really know what you are doing. */ |
29569577 |
482 | SvREADONLY_off(sv); |
483 | XSRETURN_NO; |
484 | } |
485 | } |
14a976d6 |
486 | XSRETURN_UNDEF; /* Can't happen. */ |
29569577 |
487 | } |
488 | |
14a976d6 |
489 | XS(XS_Internals_SvREFCNT) /* This is dangerous stuff. */ |
29569577 |
490 | { |
491 | dXSARGS; |
492 | SV *sv = SvRV(ST(0)); |
493 | if (items == 1) |
14a976d6 |
494 | XSRETURN_IV(SvREFCNT(sv) - 1); /* Minus the ref created for us. */ |
29569577 |
495 | else if (items == 2) { |
14a976d6 |
496 | /* I hope you really know what you are doing. */ |
29569577 |
497 | SvREFCNT(sv) = SvIV(ST(1)); |
498 | XSRETURN_IV(SvREFCNT(sv)); |
499 | } |
14a976d6 |
500 | XSRETURN_UNDEF; /* Can't happen. */ |
29569577 |
501 | } |
502 | |