This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[asperl] various tweaks
[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)
24 croak("Recursive inheritance detected");
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
76e3520e 103#include "XSUB.h"
55497cff 104
6d4a7be2
PP
105static
106XS(XS_UNIVERSAL_isa)
107{
108 dXSARGS;
55497cff
PP
109 SV *sv;
110 char *name;
6d4a7be2
PP
111
112 if (items != 2)
113 croak("Usage: UNIVERSAL::isa(reference, kind)");
114
115 sv = ST(0);
116 name = (char *)SvPV(ST(1),na);
117
54310121 118 ST(0) = boolSV(sv_derived_from(sv, name));
6d4a7be2
PP
119 XSRETURN(1);
120}
121
122static
123XS(XS_UNIVERSAL_can)
124{
125 dXSARGS;
126 SV *sv;
127 char *name;
128 SV *rv;
6f08146e 129 HV *pkg = NULL;
6d4a7be2
PP
130
131 if (items != 2)
132 croak("Usage: UNIVERSAL::can(object-ref, method)");
133
134 sv = ST(0);
135 name = (char *)SvPV(ST(1),na);
136 rv = &sv_undef;
137
6f08146e
NIS
138 if(SvROK(sv)) {
139 sv = (SV*)SvRV(sv);
140 if(SvOBJECT(sv))
141 pkg = SvSTASH(sv);
142 }
143 else {
144 pkg = gv_stashsv(sv, FALSE);
145 }
146
147 if (pkg) {
dc848c6f
PP
148 GV *gv = gv_fetchmethod_autoload(pkg, name, FALSE);
149 if (gv && isGV(gv))
150 rv = sv_2mortal(newRV((SV*)GvCV(gv)));
6d4a7be2
PP
151 }
152
153 ST(0) = rv;
154 XSRETURN(1);
155}
156
157static
6d4a7be2
PP
158XS(XS_UNIVERSAL_VERSION)
159{
160 dXSARGS;
161 HV *pkg;
162 GV **gvp;
163 GV *gv;
164 SV *sv;
165 char *undef;
13826f2c 166 double req;
6d4a7be2
PP
167
168 if(SvROK(ST(0))) {
169 sv = (SV*)SvRV(ST(0));
170 if(!SvOBJECT(sv))
171 croak("Cannot find version of an unblessed reference");
172 pkg = SvSTASH(sv);
173 }
174 else {
175 pkg = gv_stashsv(ST(0), FALSE);
176 }
177
178 gvp = pkg ? (GV**)hv_fetch(pkg,"VERSION",7,FALSE) : Null(GV**);
179
180 if (gvp && (gv = *gvp) != (GV*)&sv_undef && (sv = GvSV(gv))) {
181 SV *nsv = sv_newmortal();
182 sv_setsv(nsv, sv);
183 sv = nsv;
184 undef = Nullch;
185 }
186 else {
187 sv = (SV*)&sv_undef;
188 undef = "(undef)";
189 }
190
13826f2c 191 if (items > 1 && (undef || (req = SvNV(ST(1)), req > SvNV(sv))))
6d4a7be2 192 croak("%s version %s required--this is only version %s",
13826f2c 193 HvNAME(pkg), SvPV(ST(1),na), undef ? undef : SvPV(sv,na));
6d4a7be2
PP
194
195 ST(0) = sv;
196
197 XSRETURN(1);
198}
199
76e3520e
GS
200#ifdef PERL_OBJECT
201#undef boot_core_UNIVERSAL
202#define boot_core_UNIVERSAL CPerlObj::Perl_boot_core_UNIVERSAL
203#define pPerl this
204#endif
205
6d4a7be2 206void
8ac85365 207boot_core_UNIVERSAL(void)
6d4a7be2
PP
208{
209 char *file = __FILE__;
210
211 newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file);
212 newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file);
6d4a7be2
PP
213 newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file);
214}