Commit | Line | Data |
6d4a7be2 |
1 | #include "EXTERN.h" |
864dbfa3 |
2 | #define PERL_IN_UNIVERSAL_C |
6d4a7be2 |
3 | #include "perl.h" |
6d4a7be2 |
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 |
10 | STATIC SV * |
cea2e8a9 |
11 | S_isa_lookup(pTHX_ HV *stash, const char *name, int len, int level) |
6d4a7be2 |
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 |
20 | |
21 | if(strEQ(HvNAME(stash), name)) |
3280af22 |
22 | return &PL_sv_yes; |
6d4a7be2 |
23 | |
24 | if (level > 100) |
cea2e8a9 |
25 | Perl_croak(aTHX_ "Recursive inheritance detected in package '%s'", HvNAME(stash)); |
6d4a7be2 |
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 |
30 | SV* sv; |
31 | SV** svp = (SV**)hv_fetch(hv, name, len, FALSE); |
3280af22 |
32 | if (svp && (sv = *svp) != (SV*)&PL_sv_undef) |
6d4a7be2 |
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 |
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 |
51 | /* NOTE: No support for tied ISA */ |
52 | I32 items = AvFILLp(av) + 1; |
6d4a7be2 |
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 |
61 | SvPVX(sv), HvNAME(stash)); |
62 | continue; |
63 | } |
3280af22 |
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 |
67 | } |
68 | } |
3280af22 |
69 | (void)hv_store(hv,name,len,&PL_sv_no,0); |
6d4a7be2 |
70 | } |
71 | } |
72 | |
e09f3e01 |
73 | return boolSV(strEQ(name, "UNIVERSAL")); |
6d4a7be2 |
74 | } |
75 | |
55497cff |
76 | bool |
864dbfa3 |
77 | Perl_sv_derived_from(pTHX_ SV *sv, const char *name) |
55497cff |
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 |
101 | ? TRUE |
102 | : FALSE ; |
103 | |
104 | } |
105 | |
0cb96387 |
106 | void XS_UNIVERSAL_isa(pTHXo_ CV *cv); |
107 | void XS_UNIVERSAL_can(pTHXo_ CV *cv); |
108 | void XS_UNIVERSAL_VERSION(pTHXo_ CV *cv); |
109 | |
110 | void |
111 | Perl_boot_core_UNIVERSAL(pTHX) |
112 | { |
113 | char *file = __FILE__; |
114 | |
115 | newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file); |
116 | newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file); |
117 | newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file); |
118 | } |
119 | |
565764a8 |
120 | #ifdef PERL_OBJECT |
121 | #define NO_XSLOCKS |
122 | #endif /* PERL_OBJECT */ |
123 | |
76e3520e |
124 | #include "XSUB.h" |
55497cff |
125 | |
6d4a7be2 |
126 | XS(XS_UNIVERSAL_isa) |
127 | { |
128 | dXSARGS; |
55497cff |
129 | SV *sv; |
130 | char *name; |
2d8e6c8d |
131 | STRLEN n_a; |
6d4a7be2 |
132 | |
133 | if (items != 2) |
cea2e8a9 |
134 | Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)"); |
6d4a7be2 |
135 | |
136 | sv = ST(0); |
2d8e6c8d |
137 | name = (char *)SvPV(ST(1),n_a); |
6d4a7be2 |
138 | |
54310121 |
139 | ST(0) = boolSV(sv_derived_from(sv, name)); |
6d4a7be2 |
140 | XSRETURN(1); |
141 | } |
142 | |
6d4a7be2 |
143 | XS(XS_UNIVERSAL_can) |
144 | { |
145 | dXSARGS; |
146 | SV *sv; |
147 | char *name; |
148 | SV *rv; |
6f08146e |
149 | HV *pkg = NULL; |
2d8e6c8d |
150 | STRLEN n_a; |
6d4a7be2 |
151 | |
152 | if (items != 2) |
cea2e8a9 |
153 | Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)"); |
6d4a7be2 |
154 | |
155 | sv = ST(0); |
2d8e6c8d |
156 | name = (char *)SvPV(ST(1),n_a); |
3280af22 |
157 | rv = &PL_sv_undef; |
6d4a7be2 |
158 | |
6f08146e |
159 | if(SvROK(sv)) { |
160 | sv = (SV*)SvRV(sv); |
161 | if(SvOBJECT(sv)) |
162 | pkg = SvSTASH(sv); |
163 | } |
164 | else { |
165 | pkg = gv_stashsv(sv, FALSE); |
166 | } |
167 | |
168 | if (pkg) { |
dc848c6f |
169 | GV *gv = gv_fetchmethod_autoload(pkg, name, FALSE); |
170 | if (gv && isGV(gv)) |
171 | rv = sv_2mortal(newRV((SV*)GvCV(gv))); |
6d4a7be2 |
172 | } |
173 | |
174 | ST(0) = rv; |
175 | XSRETURN(1); |
176 | } |
177 | |
6d4a7be2 |
178 | XS(XS_UNIVERSAL_VERSION) |
179 | { |
180 | dXSARGS; |
181 | HV *pkg; |
182 | GV **gvp; |
183 | GV *gv; |
184 | SV *sv; |
185 | char *undef; |
cad2e5aa |
186 | NV req; |
6d4a7be2 |
187 | |
188 | if(SvROK(ST(0))) { |
189 | sv = (SV*)SvRV(ST(0)); |
190 | if(!SvOBJECT(sv)) |
cea2e8a9 |
191 | Perl_croak(aTHX_ "Cannot find version of an unblessed reference"); |
6d4a7be2 |
192 | pkg = SvSTASH(sv); |
193 | } |
194 | else { |
195 | pkg = gv_stashsv(ST(0), FALSE); |
196 | } |
197 | |
198 | gvp = pkg ? (GV**)hv_fetch(pkg,"VERSION",7,FALSE) : Null(GV**); |
199 | |
d4bea2fb |
200 | if (gvp && isGV(gv = *gvp) && SvOK(sv = GvSV(gv))) { |
6d4a7be2 |
201 | SV *nsv = sv_newmortal(); |
202 | sv_setsv(nsv, sv); |
203 | sv = nsv; |
204 | undef = Nullch; |
205 | } |
206 | else { |
3280af22 |
207 | sv = (SV*)&PL_sv_undef; |
6d4a7be2 |
208 | undef = "(undef)"; |
209 | } |
210 | |
2d8e6c8d |
211 | if (items > 1 && (undef || (req = SvNV(ST(1)), req > SvNV(sv)))) { |
212 | STRLEN n_a; |
cea2e8a9 |
213 | Perl_croak(aTHX_ "%s version %s required--this is only version %s", |
2d8e6c8d |
214 | HvNAME(pkg), SvPV(ST(1),n_a), undef ? undef : SvPV(sv,n_a)); |
215 | } |
6d4a7be2 |
216 | |
217 | ST(0) = sv; |
218 | |
219 | XSRETURN(1); |
220 | } |
221 | |