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