This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Disambiguate "Can't locate"
[perl5.git] / universal.c
CommitLineData
d6376244
JH
1/* universal.c
2 *
3 * Copyright (c) 1997-2002, Larry Wall
4 *
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
7 *
8 */
9
d31a8517
AT
10/*
11 * "The roots of those mountains must be roots indeed; there must be
12 * great secrets buried there which have not been discovered since the
13 * beginning." --Gandalf, relating Gollum's story
14 */
15
6d4a7be2 16#include "EXTERN.h"
864dbfa3 17#define PERL_IN_UNIVERSAL_C
6d4a7be2 18#include "perl.h"
6d4a7be2
PP
19
20/*
21 * Contributed by Graham Barr <Graham.Barr@tiuk.ti.com>
22 * The main guts of traverse_isa was actually copied from gv_fetchmeth
23 */
24
76e3520e 25STATIC SV *
301daebc
MS
26S_isa_lookup(pTHX_ HV *stash, const char *name, HV* name_stash,
27 int len, int level)
6d4a7be2
PP
28{
29 AV* av;
30 GV* gv;
31 GV** gvp;
32 HV* hv = Nullhv;
46e4b22b 33 SV* subgen = Nullsv;
6d4a7be2 34
301daebc
MS
35 /* A stash/class can go by many names (ie. User == main::User), so
36 we compare the stash itself just in case */
37 if (name_stash && (stash == name_stash))
38 return &PL_sv_yes;
6d4a7be2 39
46e4b22b 40 if (strEQ(HvNAME(stash), name))
3280af22 41 return &PL_sv_yes;
6d4a7be2
PP
42
43 if (level > 100)
46e4b22b
GS
44 Perl_croak(aTHX_ "Recursive inheritance detected in package '%s'",
45 HvNAME(stash));
6d4a7be2
PP
46
47 gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, FALSE);
48
46e4b22b
GS
49 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (subgen = GvSV(gv))
50 && (hv = GvHV(gv)))
51 {
52 if (SvIV(subgen) == PL_sub_generation) {
53 SV* sv;
54 SV** svp = (SV**)hv_fetch(hv, name, len, FALSE);
55 if (svp && (sv = *svp) != (SV*)&PL_sv_undef) {
56 DEBUG_o( Perl_deb(aTHX_ "Using cached ISA %s for package %s\n",
57 name, HvNAME(stash)) );
58 return sv;
59 }
60 }
61 else {
62 DEBUG_o( Perl_deb(aTHX_ "ISA Cache in package %s is stale\n",
63 HvNAME(stash)) );
64 hv_clear(hv);
65 sv_setiv(subgen, PL_sub_generation);
66 }
6d4a7be2
PP
67 }
68
69 gvp = (GV**)hv_fetch(stash,"ISA",3,FALSE);
46e4b22b 70
3280af22 71 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (av = GvAV(gv))) {
46e4b22b 72 if (!hv || !subgen) {
6d4a7be2
PP
73 gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, TRUE);
74
75 gv = *gvp;
76
77 if (SvTYPE(gv) != SVt_PVGV)
78 gv_init(gv, stash, "::ISA::CACHE::", 14, TRUE);
79
46e4b22b
GS
80 if (!hv)
81 hv = GvHVn(gv);
82 if (!subgen) {
83 subgen = newSViv(PL_sub_generation);
84 GvSV(gv) = subgen;
85 }
6d4a7be2 86 }
46e4b22b 87 if (hv) {
6d4a7be2 88 SV** svp = AvARRAY(av);
93965878
NIS
89 /* NOTE: No support for tied ISA */
90 I32 items = AvFILLp(av) + 1;
6d4a7be2
PP
91 while (items--) {
92 SV* sv = *svp++;
93 HV* basestash = gv_stashsv(sv, FALSE);
94 if (!basestash) {
599cee73 95 if (ckWARN(WARN_MISC))
9014280d 96 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
599cee73 97 "Can't locate package %s for @%s::ISA",
6d4a7be2
PP
98 SvPVX(sv), HvNAME(stash));
99 continue;
100 }
301daebc
MS
101 if (&PL_sv_yes == isa_lookup(basestash, name, name_stash,
102 len, level + 1)) {
3280af22
NIS
103 (void)hv_store(hv,name,len,&PL_sv_yes,0);
104 return &PL_sv_yes;
6d4a7be2
PP
105 }
106 }
3280af22 107 (void)hv_store(hv,name,len,&PL_sv_no,0);
6d4a7be2
PP
108 }
109 }
110
e09f3e01 111 return boolSV(strEQ(name, "UNIVERSAL"));
6d4a7be2
PP
112}
113
954c1994 114/*
ccfc67b7
JH
115=head1 SV Manipulation Functions
116
954c1994
GS
117=for apidoc sv_derived_from
118
119Returns a boolean indicating whether the SV is derived from the specified
120class. This is the function that implements C<UNIVERSAL::isa>. It works
121for class names as well as for objects.
122
123=cut
124*/
125
55497cff 126bool
864dbfa3 127Perl_sv_derived_from(pTHX_ SV *sv, const char *name)
55497cff 128{
55497cff
PP
129 char *type;
130 HV *stash;
301daebc 131 HV *name_stash;
46e4b22b 132
55497cff
PP
133 stash = Nullhv;
134 type = Nullch;
46e4b22b 135
55497cff
PP
136 if (SvGMAGICAL(sv))
137 mg_get(sv) ;
138
139 if (SvROK(sv)) {
140 sv = SvRV(sv);
141 type = sv_reftype(sv,0);
46e4b22b 142 if (SvOBJECT(sv))
55497cff
PP
143 stash = SvSTASH(sv);
144 }
145 else {
146 stash = gv_stashsv(sv, FALSE);
147 }
46e4b22b 148
301daebc
MS
149 name_stash = gv_stashpv(name, FALSE);
150
55497cff 151 return (type && strEQ(type,name)) ||
301daebc
MS
152 (stash && isa_lookup(stash, name, name_stash, strlen(name), 0)
153 == &PL_sv_yes)
55497cff
PP
154 ? TRUE
155 : FALSE ;
55497cff
PP
156}
157
1b026014
NIS
158#include "XSUB.h"
159
acfe0abc
GS
160void XS_UNIVERSAL_isa(pTHX_ CV *cv);
161void XS_UNIVERSAL_can(pTHX_ CV *cv);
162void XS_UNIVERSAL_VERSION(pTHX_ CV *cv);
1b026014
NIS
163XS(XS_utf8_valid);
164XS(XS_utf8_encode);
165XS(XS_utf8_decode);
166XS(XS_utf8_upgrade);
167XS(XS_utf8_downgrade);
168XS(XS_utf8_unicode_to_native);
169XS(XS_utf8_native_to_unicode);
0cb96387
GS
170
171void
172Perl_boot_core_UNIVERSAL(pTHX)
173{
174 char *file = __FILE__;
175
176 newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file);
177 newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file);
178 newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file);
1b026014
NIS
179 newXS("utf8::valid", XS_utf8_valid, file);
180 newXS("utf8::encode", XS_utf8_encode, file);
181 newXS("utf8::decode", XS_utf8_decode, file);
182 newXS("utf8::upgrade", XS_utf8_upgrade, file);
183 newXS("utf8::downgrade", XS_utf8_downgrade, file);
184 newXS("utf8::native_to_unicode", XS_utf8_native_to_unicode, file);
185 newXS("utf8::unicode_to_native", XS_utf8_unicode_to_native, file);
0cb96387
GS
186}
187
55497cff 188
6d4a7be2
PP
189XS(XS_UNIVERSAL_isa)
190{
191 dXSARGS;
55497cff
PP
192 SV *sv;
193 char *name;
2d8e6c8d 194 STRLEN n_a;
6d4a7be2
PP
195
196 if (items != 2)
cea2e8a9 197 Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)");
6d4a7be2
PP
198
199 sv = ST(0);
f8f70380 200
d3f7f2b2
GS
201 if (SvGMAGICAL(sv))
202 mg_get(sv);
203
aca069ec 204 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))))
f8f70380
GS
205 XSRETURN_UNDEF;
206
2d8e6c8d 207 name = (char *)SvPV(ST(1),n_a);
6d4a7be2 208
54310121 209 ST(0) = boolSV(sv_derived_from(sv, name));
6d4a7be2
PP
210 XSRETURN(1);
211}
212
6d4a7be2
PP
213XS(XS_UNIVERSAL_can)
214{
215 dXSARGS;
216 SV *sv;
217 char *name;
218 SV *rv;
6f08146e 219 HV *pkg = NULL;
2d8e6c8d 220 STRLEN n_a;
6d4a7be2
PP
221
222 if (items != 2)
cea2e8a9 223 Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)");
6d4a7be2
PP
224
225 sv = ST(0);
f8f70380 226
d3f7f2b2
GS
227 if (SvGMAGICAL(sv))
228 mg_get(sv);
229
aca069ec 230 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))))
f8f70380
GS
231 XSRETURN_UNDEF;
232
2d8e6c8d 233 name = (char *)SvPV(ST(1),n_a);
3280af22 234 rv = &PL_sv_undef;
6d4a7be2 235
46e4b22b 236 if (SvROK(sv)) {
6f08146e 237 sv = (SV*)SvRV(sv);
46e4b22b 238 if (SvOBJECT(sv))
6f08146e
NIS
239 pkg = SvSTASH(sv);
240 }
241 else {
242 pkg = gv_stashsv(sv, FALSE);
243 }
244
245 if (pkg) {
dc848c6f
PP
246 GV *gv = gv_fetchmethod_autoload(pkg, name, FALSE);
247 if (gv && isGV(gv))
248 rv = sv_2mortal(newRV((SV*)GvCV(gv)));
6d4a7be2
PP
249 }
250
251 ST(0) = rv;
252 XSRETURN(1);
253}
254
6d4a7be2
PP
255XS(XS_UNIVERSAL_VERSION)
256{
257 dXSARGS;
258 HV *pkg;
259 GV **gvp;
260 GV *gv;
261 SV *sv;
262 char *undef;
263
1571675a 264 if (SvROK(ST(0))) {
6d4a7be2 265 sv = (SV*)SvRV(ST(0));
1571675a 266 if (!SvOBJECT(sv))
cea2e8a9 267 Perl_croak(aTHX_ "Cannot find version of an unblessed reference");
6d4a7be2
PP
268 pkg = SvSTASH(sv);
269 }
270 else {
271 pkg = gv_stashsv(ST(0), FALSE);
272 }
273
274 gvp = pkg ? (GV**)hv_fetch(pkg,"VERSION",7,FALSE) : Null(GV**);
275
d4bea2fb 276 if (gvp && isGV(gv = *gvp) && SvOK(sv = GvSV(gv))) {
6d4a7be2
PP
277 SV *nsv = sv_newmortal();
278 sv_setsv(nsv, sv);
279 sv = nsv;
280 undef = Nullch;
281 }
282 else {
3280af22 283 sv = (SV*)&PL_sv_undef;
6d4a7be2
PP
284 undef = "(undef)";
285 }
286
1571675a
GS
287 if (items > 1) {
288 STRLEN len;
289 SV *req = ST(1);
290
62658f4d
PM
291 if (undef) {
292 if (pkg)
293 Perl_croak(aTHX_
294 "%s does not define $%s::VERSION--version check failed",
295 HvNAME(pkg), HvNAME(pkg));
296 else {
297 char *str = SvPVx(ST(0), len);
298
299 Perl_croak(aTHX_
300 "%s defines neither package nor VERSION--version check failed", str);
301 }
302 }
1571675a
GS
303 if (!SvNIOK(sv) && SvPOK(sv)) {
304 char *str = SvPVx(sv,len);
305 while (len) {
306 --len;
307 /* XXX could DWIM "1.2.3" here */
308 if (!isDIGIT(str[len]) && str[len] != '.' && str[len] != '_')
309 break;
310 }
311 if (len) {
4305d8ab 312 if (SvNOK(req) && SvPOK(req)) {
1571675a
GS
313 /* they said C<use Foo v1.2.3> and $Foo::VERSION
314 * doesn't look like a float: do string compare */
315 if (sv_cmp(req,sv) == 1) {
d2560b70
RB
316 Perl_croak(aTHX_ "%s v%"VDf" required--"
317 "this is only v%"VDf,
1571675a
GS
318 HvNAME(pkg), req, sv);
319 }
320 goto finish;
321 }
322 /* they said C<use Foo 1.002_003> and $Foo::VERSION
323 * doesn't look like a float: force numeric compare */
155aba94 324 (void)SvUPGRADE(sv, SVt_PVNV);
1571675a
GS
325 SvNVX(sv) = str_to_version(sv);
326 SvPOK_off(sv);
327 SvNOK_on(sv);
328 }
329 }
330 /* if we get here, we're looking for a numeric comparison,
331 * so force the required version into a float, even if they
332 * said C<use Foo v1.2.3> */
4305d8ab 333 if (SvNOK(req) && SvPOK(req)) {
1571675a
GS
334 NV n = SvNV(req);
335 req = sv_newmortal();
336 sv_setnv(req, n);
337 }
338
f6eb1a96 339 if (SvNV(req) > SvNV(sv))
1571675a 340 Perl_croak(aTHX_ "%s version %s required--this is only version %s",
f6eb1a96 341 HvNAME(pkg), SvPV_nolen(req), SvPV_nolen(sv));
2d8e6c8d 342 }
6d4a7be2 343
1571675a 344finish:
6d4a7be2
PP
345 ST(0) = sv;
346
347 XSRETURN(1);
348}
349
1b026014
NIS
350XS(XS_utf8_valid)
351{
352 dXSARGS;
353 if (items != 1)
354 Perl_croak(aTHX_ "Usage: utf8::valid(sv)");
355 {
356 SV * sv = ST(0);
357 {
358 STRLEN len;
359 char *s = SvPV(sv,len);
360 if (!SvUTF8(sv) || is_utf8_string((U8*)s,len))
361 XSRETURN_YES;
362 else
363 XSRETURN_NO;
364 }
365 }
366 XSRETURN_EMPTY;
367}
368
369XS(XS_utf8_encode)
370{
371 dXSARGS;
372 if (items != 1)
373 Perl_croak(aTHX_ "Usage: utf8::encode(sv)");
374 {
375 SV * sv = ST(0);
376
377 sv_utf8_encode(sv);
378 }
379 XSRETURN_EMPTY;
380}
381
382XS(XS_utf8_decode)
383{
384 dXSARGS;
385 if (items != 1)
386 Perl_croak(aTHX_ "Usage: utf8::decode(sv)");
387 {
388 SV * sv = ST(0);
389 bool RETVAL;
390
391 RETVAL = sv_utf8_decode(sv);
392 ST(0) = boolSV(RETVAL);
393 sv_2mortal(ST(0));
394 }
395 XSRETURN(1);
396}
397
398XS(XS_utf8_upgrade)
399{
400 dXSARGS;
401 if (items != 1)
402 Perl_croak(aTHX_ "Usage: utf8::upgrade(sv)");
403 {
404 SV * sv = ST(0);
405 STRLEN RETVAL;
406 dXSTARG;
407
408 RETVAL = sv_utf8_upgrade(sv);
409 XSprePUSH; PUSHi((IV)RETVAL);
410 }
411 XSRETURN(1);
412}
413
414XS(XS_utf8_downgrade)
415{
416 dXSARGS;
417 if (items < 1 || items > 2)
418 Perl_croak(aTHX_ "Usage: utf8::downgrade(sv, failok=0)");
419 {
420 SV * sv = ST(0);
421 bool failok;
422 bool RETVAL;
423
424 if (items < 2)
425 failok = 0;
426 else {
427 failok = (int)SvIV(ST(1));
428 }
429
430 RETVAL = sv_utf8_downgrade(sv, failok);
431 ST(0) = boolSV(RETVAL);
432 sv_2mortal(ST(0));
433 }
434 XSRETURN(1);
435}
436
437XS(XS_utf8_native_to_unicode)
438{
439 dXSARGS;
440 UV uv = SvUV(ST(0));
b7953727
JH
441
442 if (items > 1)
443 Perl_croak(aTHX_ "Usage: utf8::native_to_unicode(sv)");
444
1b026014
NIS
445 ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv)));
446 XSRETURN(1);
447}
448
449XS(XS_utf8_unicode_to_native)
450{
451 dXSARGS;
452 UV uv = SvUV(ST(0));
b7953727
JH
453
454 if (items > 1)
455 Perl_croak(aTHX_ "Usage: utf8::unicode_to_native(sv)");
456
1b026014
NIS
457 ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv)));
458 XSRETURN(1);
459}
460