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 | |
76e3520e |
120 | #include "XSUB.h" |
55497cff |
121 | |
6d4a7be2 |
122 | XS(XS_UNIVERSAL_isa) |
123 | { |
124 | dXSARGS; |
55497cff |
125 | SV *sv; |
126 | char *name; |
2d8e6c8d |
127 | STRLEN n_a; |
6d4a7be2 |
128 | |
129 | if (items != 2) |
cea2e8a9 |
130 | Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)"); |
6d4a7be2 |
131 | |
132 | sv = ST(0); |
2d8e6c8d |
133 | name = (char *)SvPV(ST(1),n_a); |
6d4a7be2 |
134 | |
54310121 |
135 | ST(0) = boolSV(sv_derived_from(sv, name)); |
6d4a7be2 |
136 | XSRETURN(1); |
137 | } |
138 | |
6d4a7be2 |
139 | XS(XS_UNIVERSAL_can) |
140 | { |
141 | dXSARGS; |
142 | SV *sv; |
143 | char *name; |
144 | SV *rv; |
6f08146e |
145 | HV *pkg = NULL; |
2d8e6c8d |
146 | STRLEN n_a; |
6d4a7be2 |
147 | |
148 | if (items != 2) |
cea2e8a9 |
149 | Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)"); |
6d4a7be2 |
150 | |
151 | sv = ST(0); |
2d8e6c8d |
152 | name = (char *)SvPV(ST(1),n_a); |
3280af22 |
153 | rv = &PL_sv_undef; |
6d4a7be2 |
154 | |
6f08146e |
155 | if(SvROK(sv)) { |
156 | sv = (SV*)SvRV(sv); |
157 | if(SvOBJECT(sv)) |
158 | pkg = SvSTASH(sv); |
159 | } |
160 | else { |
161 | pkg = gv_stashsv(sv, FALSE); |
162 | } |
163 | |
164 | if (pkg) { |
dc848c6f |
165 | GV *gv = gv_fetchmethod_autoload(pkg, name, FALSE); |
166 | if (gv && isGV(gv)) |
167 | rv = sv_2mortal(newRV((SV*)GvCV(gv))); |
6d4a7be2 |
168 | } |
169 | |
170 | ST(0) = rv; |
171 | XSRETURN(1); |
172 | } |
173 | |
6d4a7be2 |
174 | XS(XS_UNIVERSAL_VERSION) |
175 | { |
176 | dXSARGS; |
177 | HV *pkg; |
178 | GV **gvp; |
179 | GV *gv; |
180 | SV *sv; |
181 | char *undef; |
cad2e5aa |
182 | NV req; |
6d4a7be2 |
183 | |
184 | if(SvROK(ST(0))) { |
185 | sv = (SV*)SvRV(ST(0)); |
186 | if(!SvOBJECT(sv)) |
cea2e8a9 |
187 | Perl_croak(aTHX_ "Cannot find version of an unblessed reference"); |
6d4a7be2 |
188 | pkg = SvSTASH(sv); |
189 | } |
190 | else { |
191 | pkg = gv_stashsv(ST(0), FALSE); |
192 | } |
193 | |
194 | gvp = pkg ? (GV**)hv_fetch(pkg,"VERSION",7,FALSE) : Null(GV**); |
195 | |
d4bea2fb |
196 | if (gvp && isGV(gv = *gvp) && SvOK(sv = GvSV(gv))) { |
6d4a7be2 |
197 | SV *nsv = sv_newmortal(); |
198 | sv_setsv(nsv, sv); |
199 | sv = nsv; |
200 | undef = Nullch; |
201 | } |
202 | else { |
3280af22 |
203 | sv = (SV*)&PL_sv_undef; |
6d4a7be2 |
204 | undef = "(undef)"; |
205 | } |
206 | |
2d8e6c8d |
207 | if (items > 1 && (undef || (req = SvNV(ST(1)), req > SvNV(sv)))) { |
208 | STRLEN n_a; |
cea2e8a9 |
209 | Perl_croak(aTHX_ "%s version %s required--this is only version %s", |
2d8e6c8d |
210 | HvNAME(pkg), SvPV(ST(1),n_a), undef ? undef : SvPV(sv,n_a)); |
211 | } |
6d4a7be2 |
212 | |
213 | ST(0) = sv; |
214 | |
215 | XSRETURN(1); |
216 | } |
217 | |