This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix a2p translation of '{print "a" "b" "c"}'
[perl5.git] / universal.c
CommitLineData
6d4a7be2
PP
1#include "EXTERN.h"
2#include "perl.h"
3#include "XSUB.h"
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
10static SV *
11isa_lookup(stash, name, len, level)
12HV *stash;
13char *name;
14int len;
15int level;
16{
17 AV* av;
18 GV* gv;
19 GV** gvp;
20 HV* hv = Nullhv;
21
22 if (!stash)
23 return &sv_undef;
24
25 if(strEQ(HvNAME(stash), name))
26 return &sv_yes;
27
28 if (level > 100)
29 croak("Recursive inheritance detected");
30
31 gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, FALSE);
32
33 if (gvp && (gv = *gvp) != (GV*)&sv_undef && (hv = GvHV(gv))) {
34 SV* sv;
35 SV** svp = (SV**)hv_fetch(hv, name, len, FALSE);
36 if (svp && (sv = *svp) != (SV*)&sv_undef)
37 return sv;
38 }
39
40 gvp = (GV**)hv_fetch(stash,"ISA",3,FALSE);
41
42 if (gvp && (gv = *gvp) != (GV*)&sv_undef && (av = GvAV(gv))) {
43 if(!hv) {
44 gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, TRUE);
45
46 gv = *gvp;
47
48 if (SvTYPE(gv) != SVt_PVGV)
49 gv_init(gv, stash, "::ISA::CACHE::", 14, TRUE);
50
51 hv = GvHVn(gv);
52 }
53 if(hv) {
54 SV** svp = AvARRAY(av);
55 I32 items = AvFILL(av) + 1;
56 while (items--) {
57 SV* sv = *svp++;
58 HV* basestash = gv_stashsv(sv, FALSE);
59 if (!basestash) {
60 if (dowarn)
61 warn("Can't locate package %s for @%s::ISA",
62 SvPVX(sv), HvNAME(stash));
63 continue;
64 }
65 if(&sv_yes == isa_lookup(basestash, name, len, level + 1)) {
66 (void)hv_store(hv,name,len,&sv_yes,0);
67 return &sv_yes;
68 }
69 }
70 (void)hv_store(hv,name,len,&sv_no,0);
71 }
72 }
73
74 return &sv_no;
75}
76
77static
78XS(XS_UNIVERSAL_isa)
79{
80 dXSARGS;
81 SV *sv, *rv;
760ac839
LW
82 char *name, *type;
83 HV *stash;
6d4a7be2
PP
84
85 if (items != 2)
86 croak("Usage: UNIVERSAL::isa(reference, kind)");
87
760ac839
LW
88 stash = Nullhv;
89 type = Nullch;
6d4a7be2
PP
90 sv = ST(0);
91 name = (char *)SvPV(ST(1),na);
92
760ac839
LW
93 if (SvROK(sv)) {
94 sv = SvRV(sv);
95 type = sv_reftype(sv,0);
96 if(SvOBJECT(sv))
97 stash = SvSTASH(sv);
6d4a7be2
PP
98 }
99 else {
760ac839 100 stash = gv_stashsv(sv, FALSE);
6d4a7be2
PP
101 }
102
760ac839
LW
103 ST(0) = (type && strEQ(type,name)) ||
104 (stash && isa_lookup(stash, name, strlen(name), 0) == &sv_yes)
105 ? &sv_yes
106 : &sv_no;
107
6d4a7be2
PP
108 XSRETURN(1);
109}
110
111static
112XS(XS_UNIVERSAL_can)
113{
114 dXSARGS;
115 SV *sv;
116 char *name;
117 SV *rv;
118 GV *gv;
119 CV *cvp;
120
121 if (items != 2)
122 croak("Usage: UNIVERSAL::can(object-ref, method)");
123
124 sv = ST(0);
125 name = (char *)SvPV(ST(1),na);
126 rv = &sv_undef;
127
128 if(SvROK(sv) && (sv = (SV*)SvRV(sv)) && SvOBJECT(sv)) {
129 gv = gv_fetchmethod(SvSTASH(sv), name);
130
131 if(gv && GvCV(gv)) {
132 /* If the sub is only a stub then we may have a gv to AUTOLOAD */
133 GV **gvp = (GV**)hv_fetch(GvSTASH(gv), name, strlen(name), TRUE);
134 if(gvp && (cvp = GvCV(*gvp))) {
135 rv = sv_newmortal();
136 sv_setsv(rv, newRV((SV*)cvp));
137 }
138 }
139 }
140
141 ST(0) = rv;
142 XSRETURN(1);
143}
144
145static
146XS(XS_UNIVERSAL_is_instance)
147{
148 dXSARGS;
149 ST(0) = SvROK(ST(0)) ? &sv_yes : &sv_no;
150 XSRETURN(1);
151}
152
153static
154XS(XS_UNIVERSAL_class)
155{
156 dXSARGS;
bf3f28f2 157 if(SvROK(ST(0)) && SvOBJECT(SvRV(ST(0)))) {
6d4a7be2 158 SV *sv = sv_newmortal();
bf3f28f2 159 sv_setpv(sv, HvNAME(SvSTASH(SvRV(ST(0)))));
6d4a7be2
PP
160 ST(0) = sv;
161 }
162 XSRETURN(1);
163}
164
165static
166XS(XS_UNIVERSAL_VERSION)
167{
168 dXSARGS;
169 HV *pkg;
170 GV **gvp;
171 GV *gv;
172 SV *sv;
173 char *undef;
174
175 if(SvROK(ST(0))) {
176 sv = (SV*)SvRV(ST(0));
177 if(!SvOBJECT(sv))
178 croak("Cannot find version of an unblessed reference");
179 pkg = SvSTASH(sv);
180 }
181 else {
182 pkg = gv_stashsv(ST(0), FALSE);
183 }
184
185 gvp = pkg ? (GV**)hv_fetch(pkg,"VERSION",7,FALSE) : Null(GV**);
186
187 if (gvp && (gv = *gvp) != (GV*)&sv_undef && (sv = GvSV(gv))) {
188 SV *nsv = sv_newmortal();
189 sv_setsv(nsv, sv);
190 sv = nsv;
191 undef = Nullch;
192 }
193 else {
194 sv = (SV*)&sv_undef;
195 undef = "(undef)";
196 }
197
198 if(items > 1 && (undef || SvNV(ST(1)) > SvNV(sv)))
199 croak("%s version %s required--this is only version %s",
200 HvNAME(pkg),SvPV(ST(1),na),undef ? undef : SvPV(sv,na));
201
202 ST(0) = sv;
203
204 XSRETURN(1);
205}
206
207void
208boot_core_UNIVERSAL()
209{
210 char *file = __FILE__;
211
212 newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file);
213 newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file);
214 newXS("UNIVERSAL::class", XS_UNIVERSAL_class, file);
215 newXS("UNIVERSAL::is_instance", XS_UNIVERSAL_is_instance, file);
216 newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file);
217}