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