This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
avoid accidental #line directives (from Rick Delaney
[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
954c1994
GS
76/*
77=for apidoc sv_derived_from
78
79Returns a boolean indicating whether the SV is derived from the specified
80class. This is the function that implements C<UNIVERSAL::isa>. It works
81for class names as well as for objects.
82
83=cut
84*/
85
55497cff 86bool
864dbfa3 87Perl_sv_derived_from(pTHX_ SV *sv, const char *name)
55497cff
PP
88{
89 SV *rv;
90 char *type;
91 HV *stash;
92
93 stash = Nullhv;
94 type = Nullch;
95
96 if (SvGMAGICAL(sv))
97 mg_get(sv) ;
98
99 if (SvROK(sv)) {
100 sv = SvRV(sv);
101 type = sv_reftype(sv,0);
102 if(SvOBJECT(sv))
103 stash = SvSTASH(sv);
104 }
105 else {
106 stash = gv_stashsv(sv, FALSE);
107 }
108
109 return (type && strEQ(type,name)) ||
3280af22 110 (stash && isa_lookup(stash, name, strlen(name), 0) == &PL_sv_yes)
55497cff
PP
111 ? TRUE
112 : FALSE ;
113
114}
115
0cb96387
GS
116void XS_UNIVERSAL_isa(pTHXo_ CV *cv);
117void XS_UNIVERSAL_can(pTHXo_ CV *cv);
118void XS_UNIVERSAL_VERSION(pTHXo_ CV *cv);
119
120void
121Perl_boot_core_UNIVERSAL(pTHX)
122{
123 char *file = __FILE__;
124
125 newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file);
126 newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file);
127 newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file);
128}
129
76e3520e 130#include "XSUB.h"
55497cff 131
6d4a7be2
PP
132XS(XS_UNIVERSAL_isa)
133{
134 dXSARGS;
55497cff
PP
135 SV *sv;
136 char *name;
2d8e6c8d 137 STRLEN n_a;
6d4a7be2
PP
138
139 if (items != 2)
cea2e8a9 140 Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)");
6d4a7be2
PP
141
142 sv = ST(0);
2d8e6c8d 143 name = (char *)SvPV(ST(1),n_a);
6d4a7be2 144
54310121 145 ST(0) = boolSV(sv_derived_from(sv, name));
6d4a7be2
PP
146 XSRETURN(1);
147}
148
6d4a7be2
PP
149XS(XS_UNIVERSAL_can)
150{
151 dXSARGS;
152 SV *sv;
153 char *name;
154 SV *rv;
6f08146e 155 HV *pkg = NULL;
2d8e6c8d 156 STRLEN n_a;
6d4a7be2
PP
157
158 if (items != 2)
cea2e8a9 159 Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)");
6d4a7be2
PP
160
161 sv = ST(0);
2d8e6c8d 162 name = (char *)SvPV(ST(1),n_a);
3280af22 163 rv = &PL_sv_undef;
6d4a7be2 164
6f08146e
NIS
165 if(SvROK(sv)) {
166 sv = (SV*)SvRV(sv);
167 if(SvOBJECT(sv))
168 pkg = SvSTASH(sv);
169 }
170 else {
171 pkg = gv_stashsv(sv, FALSE);
172 }
173
174 if (pkg) {
dc848c6f
PP
175 GV *gv = gv_fetchmethod_autoload(pkg, name, FALSE);
176 if (gv && isGV(gv))
177 rv = sv_2mortal(newRV((SV*)GvCV(gv)));
6d4a7be2
PP
178 }
179
180 ST(0) = rv;
181 XSRETURN(1);
182}
183
6d4a7be2
PP
184XS(XS_UNIVERSAL_VERSION)
185{
186 dXSARGS;
187 HV *pkg;
188 GV **gvp;
189 GV *gv;
190 SV *sv;
191 char *undef;
cad2e5aa 192 NV req;
6d4a7be2
PP
193
194 if(SvROK(ST(0))) {
195 sv = (SV*)SvRV(ST(0));
196 if(!SvOBJECT(sv))
cea2e8a9 197 Perl_croak(aTHX_ "Cannot find version of an unblessed reference");
6d4a7be2
PP
198 pkg = SvSTASH(sv);
199 }
200 else {
201 pkg = gv_stashsv(ST(0), FALSE);
202 }
203
204 gvp = pkg ? (GV**)hv_fetch(pkg,"VERSION",7,FALSE) : Null(GV**);
205
d4bea2fb 206 if (gvp && isGV(gv = *gvp) && SvOK(sv = GvSV(gv))) {
6d4a7be2
PP
207 SV *nsv = sv_newmortal();
208 sv_setsv(nsv, sv);
209 sv = nsv;
210 undef = Nullch;
211 }
212 else {
3280af22 213 sv = (SV*)&PL_sv_undef;
6d4a7be2
PP
214 undef = "(undef)";
215 }
216
2d8e6c8d
GS
217 if (items > 1 && (undef || (req = SvNV(ST(1)), req > SvNV(sv)))) {
218 STRLEN n_a;
cea2e8a9 219 Perl_croak(aTHX_ "%s version %s required--this is only version %s",
2d8e6c8d
GS
220 HvNAME(pkg), SvPV(ST(1),n_a), undef ? undef : SvPV(sv,n_a));
221 }
6d4a7be2
PP
222
223 ST(0) = sv;
224
225 XSRETURN(1);
226}
227