This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
various fixes for race conditions under threads: mutex locks based
[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)
3280af22 18 return &PL_sv_undef;
6d4a7be2
PP
19
20 if(strEQ(HvNAME(stash), name))
3280af22 21 return &PL_sv_yes;
6d4a7be2
PP
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
3280af22 28 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (hv = GvHV(gv))) {
6d4a7be2
PP
29 SV* sv;
30 SV** svp = (SV**)hv_fetch(hv, name, len, FALSE);
3280af22 31 if (svp && (sv = *svp) != (SV*)&PL_sv_undef)
6d4a7be2
PP
32 return sv;
33 }
34
35 gvp = (GV**)hv_fetch(stash,"ISA",3,FALSE);
36
3280af22 37 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (av = GvAV(gv))) {
6d4a7be2
PP
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) {
d008e5eb 56 dTHR;
599cee73
PM
57 if (ckWARN(WARN_MISC))
58 warner(WARN_SYNTAX,
59 "Can't locate package %s for @%s::ISA",
6d4a7be2
PP
60 SvPVX(sv), HvNAME(stash));
61 continue;
62 }
3280af22
NIS
63 if(&PL_sv_yes == isa_lookup(basestash, name, len, level + 1)) {
64 (void)hv_store(hv,name,len,&PL_sv_yes,0);
65 return &PL_sv_yes;
6d4a7be2
PP
66 }
67 }
3280af22 68 (void)hv_store(hv,name,len,&PL_sv_no,0);
6d4a7be2
PP
69 }
70 }
71
e09f3e01 72 return boolSV(strEQ(name, "UNIVERSAL"));
6d4a7be2
PP
73}
74
55497cff 75bool
8ac85365 76sv_derived_from(SV *sv, char *name)
55497cff
PP
77{
78 SV *rv;
79 char *type;
80 HV *stash;
81
82 stash = Nullhv;
83 type = Nullch;
84
85 if (SvGMAGICAL(sv))
86 mg_get(sv) ;
87
88 if (SvROK(sv)) {
89 sv = SvRV(sv);
90 type = sv_reftype(sv,0);
91 if(SvOBJECT(sv))
92 stash = SvSTASH(sv);
93 }
94 else {
95 stash = gv_stashsv(sv, FALSE);
96 }
97
98 return (type && strEQ(type,name)) ||
3280af22 99 (stash && isa_lookup(stash, name, strlen(name), 0) == &PL_sv_yes)
55497cff
PP
100 ? TRUE
101 : FALSE ;
102
103}
104
565764a8
DL
105#ifdef PERL_OBJECT
106#define NO_XSLOCKS
107#endif /* PERL_OBJECT */
108
76e3520e 109#include "XSUB.h"
55497cff 110
6d4a7be2
PP
111XS(XS_UNIVERSAL_isa)
112{
113 dXSARGS;
55497cff
PP
114 SV *sv;
115 char *name;
6d4a7be2
PP
116
117 if (items != 2)
118 croak("Usage: UNIVERSAL::isa(reference, kind)");
119
120 sv = ST(0);
3280af22 121 name = (char *)SvPV(ST(1),PL_na);
6d4a7be2 122
54310121 123 ST(0) = boolSV(sv_derived_from(sv, name));
6d4a7be2
PP
124 XSRETURN(1);
125}
126
6d4a7be2
PP
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);
3280af22
NIS
139 name = (char *)SvPV(ST(1),PL_na);
140 rv = &PL_sv_undef;
6d4a7be2 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
6d4a7be2
PP
161XS(XS_UNIVERSAL_VERSION)
162{
163 dXSARGS;
164 HV *pkg;
165 GV **gvp;
166 GV *gv;
167 SV *sv;
168 char *undef;
13826f2c 169 double req;
6d4a7be2
PP
170
171 if(SvROK(ST(0))) {
172 sv = (SV*)SvRV(ST(0));
173 if(!SvOBJECT(sv))
174 croak("Cannot find version of an unblessed reference");
175 pkg = SvSTASH(sv);
176 }
177 else {
178 pkg = gv_stashsv(ST(0), FALSE);
179 }
180
181 gvp = pkg ? (GV**)hv_fetch(pkg,"VERSION",7,FALSE) : Null(GV**);
182
3280af22 183 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (sv = GvSV(gv))) {
6d4a7be2
PP
184 SV *nsv = sv_newmortal();
185 sv_setsv(nsv, sv);
186 sv = nsv;
187 undef = Nullch;
188 }
189 else {
3280af22 190 sv = (SV*)&PL_sv_undef;
6d4a7be2
PP
191 undef = "(undef)";
192 }
193
13826f2c 194 if (items > 1 && (undef || (req = SvNV(ST(1)), req > SvNV(sv))))
6d4a7be2 195 croak("%s version %s required--this is only version %s",
3280af22 196 HvNAME(pkg), SvPV(ST(1),PL_na), undef ? undef : SvPV(sv,PL_na));
6d4a7be2
PP
197
198 ST(0) = sv;
199
200 XSRETURN(1);
201}
202
76e3520e
GS
203#ifdef PERL_OBJECT
204#undef boot_core_UNIVERSAL
205#define boot_core_UNIVERSAL CPerlObj::Perl_boot_core_UNIVERSAL
206#define pPerl this
207#endif
208
6d4a7be2 209void
8ac85365 210boot_core_UNIVERSAL(void)
6d4a7be2
PP
211{
212 char *file = __FILE__;
213
214 newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file);
215 newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file);
6d4a7be2
PP
216 newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file);
217}