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