This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
integrate cfgperl changes into mainline
[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 *
cea2e8a9 11S_isa_lookup(pTHX_ HV *stash, const char *name, int len, int level)
6d4a7be2
PP
12{
13 AV* av;
14 GV* gv;
15 GV** gvp;
16 HV* hv = Nullhv;
17
18 if (!stash)
3280af22 19 return &PL_sv_undef;
6d4a7be2
PP
20
21 if(strEQ(HvNAME(stash), name))
3280af22 22 return &PL_sv_yes;
6d4a7be2
PP
23
24 if (level > 100)
cea2e8a9 25 Perl_croak(aTHX_ "Recursive inheritance detected in package '%s'", HvNAME(stash));
6d4a7be2
PP
26
27 gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, FALSE);
28
3280af22 29 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (hv = GvHV(gv))) {
6d4a7be2
PP
30 SV* sv;
31 SV** svp = (SV**)hv_fetch(hv, name, len, FALSE);
3280af22 32 if (svp && (sv = *svp) != (SV*)&PL_sv_undef)
6d4a7be2
PP
33 return sv;
34 }
35
36 gvp = (GV**)hv_fetch(stash,"ISA",3,FALSE);
37
3280af22 38 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (av = GvAV(gv))) {
6d4a7be2
PP
39 if(!hv) {
40 gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, TRUE);
41
42 gv = *gvp;
43
44 if (SvTYPE(gv) != SVt_PVGV)
45 gv_init(gv, stash, "::ISA::CACHE::", 14, TRUE);
46
47 hv = GvHVn(gv);
48 }
49 if(hv) {
50 SV** svp = AvARRAY(av);
93965878
NIS
51 /* NOTE: No support for tied ISA */
52 I32 items = AvFILLp(av) + 1;
6d4a7be2
PP
53 while (items--) {
54 SV* sv = *svp++;
55 HV* basestash = gv_stashsv(sv, FALSE);
56 if (!basestash) {
d008e5eb 57 dTHR;
599cee73 58 if (ckWARN(WARN_MISC))
cea2e8a9 59 Perl_warner(aTHX_ WARN_SYNTAX,
599cee73 60 "Can't locate package %s for @%s::ISA",
6d4a7be2
PP
61 SvPVX(sv), HvNAME(stash));
62 continue;
63 }
3280af22
NIS
64 if(&PL_sv_yes == isa_lookup(basestash, name, len, level + 1)) {
65 (void)hv_store(hv,name,len,&PL_sv_yes,0);
66 return &PL_sv_yes;
6d4a7be2
PP
67 }
68 }
3280af22 69 (void)hv_store(hv,name,len,&PL_sv_no,0);
6d4a7be2
PP
70 }
71 }
72
e09f3e01 73 return boolSV(strEQ(name, "UNIVERSAL"));
6d4a7be2
PP
74}
75
55497cff 76bool
864dbfa3 77Perl_sv_derived_from(pTHX_ SV *sv, const char *name)
55497cff
PP
78{
79 SV *rv;
80 char *type;
81 HV *stash;
82
83 stash = Nullhv;
84 type = Nullch;
85
86 if (SvGMAGICAL(sv))
87 mg_get(sv) ;
88
89 if (SvROK(sv)) {
90 sv = SvRV(sv);
91 type = sv_reftype(sv,0);
92 if(SvOBJECT(sv))
93 stash = SvSTASH(sv);
94 }
95 else {
96 stash = gv_stashsv(sv, FALSE);
97 }
98
99 return (type && strEQ(type,name)) ||
3280af22 100 (stash && isa_lookup(stash, name, strlen(name), 0) == &PL_sv_yes)
55497cff
PP
101 ? TRUE
102 : FALSE ;
103
104}
105
565764a8
DL
106#ifdef PERL_OBJECT
107#define NO_XSLOCKS
108#endif /* PERL_OBJECT */
109
76e3520e 110#include "XSUB.h"
55497cff 111
6d4a7be2
PP
112XS(XS_UNIVERSAL_isa)
113{
114 dXSARGS;
55497cff
PP
115 SV *sv;
116 char *name;
2d8e6c8d 117 STRLEN n_a;
6d4a7be2
PP
118
119 if (items != 2)
cea2e8a9 120 Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)");
6d4a7be2
PP
121
122 sv = ST(0);
2d8e6c8d 123 name = (char *)SvPV(ST(1),n_a);
6d4a7be2 124
54310121 125 ST(0) = boolSV(sv_derived_from(sv, name));
6d4a7be2
PP
126 XSRETURN(1);
127}
128
6d4a7be2
PP
129XS(XS_UNIVERSAL_can)
130{
131 dXSARGS;
132 SV *sv;
133 char *name;
134 SV *rv;
6f08146e 135 HV *pkg = NULL;
2d8e6c8d 136 STRLEN n_a;
6d4a7be2
PP
137
138 if (items != 2)
cea2e8a9 139 Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)");
6d4a7be2
PP
140
141 sv = ST(0);
2d8e6c8d 142 name = (char *)SvPV(ST(1),n_a);
3280af22 143 rv = &PL_sv_undef;
6d4a7be2 144
6f08146e
NIS
145 if(SvROK(sv)) {
146 sv = (SV*)SvRV(sv);
147 if(SvOBJECT(sv))
148 pkg = SvSTASH(sv);
149 }
150 else {
151 pkg = gv_stashsv(sv, FALSE);
152 }
153
154 if (pkg) {
dc848c6f
PP
155 GV *gv = gv_fetchmethod_autoload(pkg, name, FALSE);
156 if (gv && isGV(gv))
157 rv = sv_2mortal(newRV((SV*)GvCV(gv)));
6d4a7be2
PP
158 }
159
160 ST(0) = rv;
161 XSRETURN(1);
162}
163
6d4a7be2
PP
164XS(XS_UNIVERSAL_VERSION)
165{
166 dXSARGS;
167 HV *pkg;
168 GV **gvp;
169 GV *gv;
170 SV *sv;
171 char *undef;
13826f2c 172 double req;
6d4a7be2
PP
173
174 if(SvROK(ST(0))) {
175 sv = (SV*)SvRV(ST(0));
176 if(!SvOBJECT(sv))
cea2e8a9 177 Perl_croak(aTHX_ "Cannot find version of an unblessed reference");
6d4a7be2
PP
178 pkg = SvSTASH(sv);
179 }
180 else {
181 pkg = gv_stashsv(ST(0), FALSE);
182 }
183
184 gvp = pkg ? (GV**)hv_fetch(pkg,"VERSION",7,FALSE) : Null(GV**);
185
3280af22 186 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (sv = GvSV(gv))) {
6d4a7be2
PP
187 SV *nsv = sv_newmortal();
188 sv_setsv(nsv, sv);
189 sv = nsv;
190 undef = Nullch;
191 }
192 else {
3280af22 193 sv = (SV*)&PL_sv_undef;
6d4a7be2
PP
194 undef = "(undef)";
195 }
196
2d8e6c8d
GS
197 if (items > 1 && (undef || (req = SvNV(ST(1)), req > SvNV(sv)))) {
198 STRLEN n_a;
cea2e8a9 199 Perl_croak(aTHX_ "%s version %s required--this is only version %s",
2d8e6c8d
GS
200 HvNAME(pkg), SvPV(ST(1),n_a), undef ? undef : SvPV(sv,n_a));
201 }
6d4a7be2
PP
202
203 ST(0) = sv;
204
205 XSRETURN(1);
206}
207
76e3520e
GS
208#ifdef PERL_OBJECT
209#undef boot_core_UNIVERSAL
210#define boot_core_UNIVERSAL CPerlObj::Perl_boot_core_UNIVERSAL
211#define pPerl this
212#endif
213
6d4a7be2 214void
864dbfa3 215Perl_boot_core_UNIVERSAL(pTHX)
6d4a7be2
PP
216{
217 char *file = __FILE__;
218
219 newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file);
220 newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file);
6d4a7be2
PP
221 newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file);
222}