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