This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade to Math::BigInt 1.54.
[perl5.git] / universal.c
... / ...
CommitLineData
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
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
16#include "EXTERN.h"
17#define PERL_IN_UNIVERSAL_C
18#include "perl.h"
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
25STATIC SV *
26S_isa_lookup(pTHX_ HV *stash, const char *name, HV* name_stash,
27 int len, int level)
28{
29 AV* av;
30 GV* gv;
31 GV** gvp;
32 HV* hv = Nullhv;
33 SV* subgen = Nullsv;
34
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;
39
40 if (strEQ(HvNAME(stash), name))
41 return &PL_sv_yes;
42
43 if (level > 100)
44 Perl_croak(aTHX_ "Recursive inheritance detected in package '%s'",
45 HvNAME(stash));
46
47 gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, FALSE);
48
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 }
67 }
68
69 gvp = (GV**)hv_fetch(stash,"ISA",3,FALSE);
70
71 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (av = GvAV(gv))) {
72 if (!hv || !subgen) {
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
80 if (!hv)
81 hv = GvHVn(gv);
82 if (!subgen) {
83 subgen = newSViv(PL_sub_generation);
84 GvSV(gv) = subgen;
85 }
86 }
87 if (hv) {
88 SV** svp = AvARRAY(av);
89 /* NOTE: No support for tied ISA */
90 I32 items = AvFILLp(av) + 1;
91 while (items--) {
92 SV* sv = *svp++;
93 HV* basestash = gv_stashsv(sv, FALSE);
94 if (!basestash) {
95 if (ckWARN(WARN_MISC))
96 Perl_warner(aTHX_ WARN_SYNTAX,
97 "Can't locate package %s for @%s::ISA",
98 SvPVX(sv), HvNAME(stash));
99 continue;
100 }
101 if (&PL_sv_yes == isa_lookup(basestash, name, name_stash,
102 len, level + 1)) {
103 (void)hv_store(hv,name,len,&PL_sv_yes,0);
104 return &PL_sv_yes;
105 }
106 }
107 (void)hv_store(hv,name,len,&PL_sv_no,0);
108 }
109 }
110
111 return boolSV(strEQ(name, "UNIVERSAL"));
112}
113
114/*
115=head1 SV Manipulation Functions
116
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
126bool
127Perl_sv_derived_from(pTHX_ SV *sv, const char *name)
128{
129 char *type;
130 HV *stash;
131 HV *name_stash;
132
133 stash = Nullhv;
134 type = Nullch;
135
136 if (SvGMAGICAL(sv))
137 mg_get(sv) ;
138
139 if (SvROK(sv)) {
140 sv = SvRV(sv);
141 type = sv_reftype(sv,0);
142 if (SvOBJECT(sv))
143 stash = SvSTASH(sv);
144 }
145 else {
146 stash = gv_stashsv(sv, FALSE);
147 }
148
149 name_stash = gv_stashpv(name, FALSE);
150
151 return (type && strEQ(type,name)) ||
152 (stash && isa_lookup(stash, name, name_stash, strlen(name), 0)
153 == &PL_sv_yes)
154 ? TRUE
155 : FALSE ;
156}
157
158#include "XSUB.h"
159
160void XS_UNIVERSAL_isa(pTHX_ CV *cv);
161void XS_UNIVERSAL_can(pTHX_ CV *cv);
162void XS_UNIVERSAL_VERSION(pTHX_ CV *cv);
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);
170XS(XS_access_readonly);
171
172void
173Perl_boot_core_UNIVERSAL(pTHX)
174{
175 char *file = __FILE__;
176
177 newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file);
178 newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file);
179 newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file);
180 newXS("utf8::valid", XS_utf8_valid, file);
181 newXS("utf8::encode", XS_utf8_encode, file);
182 newXS("utf8::decode", XS_utf8_decode, file);
183 newXS("utf8::upgrade", XS_utf8_upgrade, file);
184 newXS("utf8::downgrade", XS_utf8_downgrade, file);
185 newXS("utf8::native_to_unicode", XS_utf8_native_to_unicode, file);
186 newXS("utf8::unicode_to_native", XS_utf8_unicode_to_native, file);
187 newXSproto("access::readonly",XS_access_readonly, file, "\\[$%@];$");
188}
189
190
191XS(XS_UNIVERSAL_isa)
192{
193 dXSARGS;
194 SV *sv;
195 char *name;
196 STRLEN n_a;
197
198 if (items != 2)
199 Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)");
200
201 sv = ST(0);
202
203 if (SvGMAGICAL(sv))
204 mg_get(sv);
205
206 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))))
207 XSRETURN_UNDEF;
208
209 name = (char *)SvPV(ST(1),n_a);
210
211 ST(0) = boolSV(sv_derived_from(sv, name));
212 XSRETURN(1);
213}
214
215XS(XS_UNIVERSAL_can)
216{
217 dXSARGS;
218 SV *sv;
219 char *name;
220 SV *rv;
221 HV *pkg = NULL;
222 STRLEN n_a;
223
224 if (items != 2)
225 Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)");
226
227 sv = ST(0);
228
229 if (SvGMAGICAL(sv))
230 mg_get(sv);
231
232 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))))
233 XSRETURN_UNDEF;
234
235 name = (char *)SvPV(ST(1),n_a);
236 rv = &PL_sv_undef;
237
238 if (SvROK(sv)) {
239 sv = (SV*)SvRV(sv);
240 if (SvOBJECT(sv))
241 pkg = SvSTASH(sv);
242 }
243 else {
244 pkg = gv_stashsv(sv, FALSE);
245 }
246
247 if (pkg) {
248 GV *gv = gv_fetchmethod_autoload(pkg, name, FALSE);
249 if (gv && isGV(gv))
250 rv = sv_2mortal(newRV((SV*)GvCV(gv)));
251 }
252
253 ST(0) = rv;
254 XSRETURN(1);
255}
256
257XS(XS_UNIVERSAL_VERSION)
258{
259 dXSARGS;
260 HV *pkg;
261 GV **gvp;
262 GV *gv;
263 SV *sv;
264 char *undef;
265
266 if (SvROK(ST(0))) {
267 sv = (SV*)SvRV(ST(0));
268 if (!SvOBJECT(sv))
269 Perl_croak(aTHX_ "Cannot find version of an unblessed reference");
270 pkg = SvSTASH(sv);
271 }
272 else {
273 pkg = gv_stashsv(ST(0), FALSE);
274 }
275
276 gvp = pkg ? (GV**)hv_fetch(pkg,"VERSION",7,FALSE) : Null(GV**);
277
278 if (gvp && isGV(gv = *gvp) && SvOK(sv = GvSV(gv))) {
279 SV *nsv = sv_newmortal();
280 sv_setsv(nsv, sv);
281 sv = nsv;
282 undef = Nullch;
283 }
284 else {
285 sv = (SV*)&PL_sv_undef;
286 undef = "(undef)";
287 }
288
289 if (items > 1) {
290 STRLEN len;
291 SV *req = ST(1);
292
293 if (undef) {
294 if (pkg)
295 Perl_croak(aTHX_
296 "%s does not define $%s::VERSION--version check failed",
297 HvNAME(pkg), HvNAME(pkg));
298 else {
299 char *str = SvPVx(ST(0), len);
300
301 Perl_croak(aTHX_
302 "%s defines neither package nor VERSION--version check failed", str);
303 }
304 }
305 if (!SvNIOK(sv) && SvPOK(sv)) {
306 char *str = SvPVx(sv,len);
307 while (len) {
308 --len;
309 /* XXX could DWIM "1.2.3" here */
310 if (!isDIGIT(str[len]) && str[len] != '.' && str[len] != '_')
311 break;
312 }
313 if (len) {
314 if (SvNOK(req) && SvPOK(req)) {
315 /* they said C<use Foo v1.2.3> and $Foo::VERSION
316 * doesn't look like a float: do string compare */
317 if (sv_cmp(req,sv) == 1) {
318 Perl_croak(aTHX_ "%s v%"VDf" required--"
319 "this is only v%"VDf,
320 HvNAME(pkg), req, sv);
321 }
322 goto finish;
323 }
324 /* they said C<use Foo 1.002_003> and $Foo::VERSION
325 * doesn't look like a float: force numeric compare */
326 (void)SvUPGRADE(sv, SVt_PVNV);
327 SvNVX(sv) = str_to_version(sv);
328 SvPOK_off(sv);
329 SvNOK_on(sv);
330 }
331 }
332 /* if we get here, we're looking for a numeric comparison,
333 * so force the required version into a float, even if they
334 * said C<use Foo v1.2.3> */
335 if (SvNOK(req) && SvPOK(req)) {
336 NV n = SvNV(req);
337 req = sv_newmortal();
338 sv_setnv(req, n);
339 }
340
341 if (SvNV(req) > SvNV(sv))
342 Perl_croak(aTHX_ "%s version %s required--this is only version %s",
343 HvNAME(pkg), SvPV_nolen(req), SvPV_nolen(sv));
344 }
345
346finish:
347 ST(0) = sv;
348
349 XSRETURN(1);
350}
351
352XS(XS_utf8_valid)
353{
354 dXSARGS;
355 if (items != 1)
356 Perl_croak(aTHX_ "Usage: utf8::valid(sv)");
357 {
358 SV * sv = ST(0);
359 {
360 STRLEN len;
361 char *s = SvPV(sv,len);
362 if (!SvUTF8(sv) || is_utf8_string((U8*)s,len))
363 XSRETURN_YES;
364 else
365 XSRETURN_NO;
366 }
367 }
368 XSRETURN_EMPTY;
369}
370
371XS(XS_utf8_encode)
372{
373 dXSARGS;
374 if (items != 1)
375 Perl_croak(aTHX_ "Usage: utf8::encode(sv)");
376 {
377 SV * sv = ST(0);
378
379 sv_utf8_encode(sv);
380 }
381 XSRETURN_EMPTY;
382}
383
384XS(XS_utf8_decode)
385{
386 dXSARGS;
387 if (items != 1)
388 Perl_croak(aTHX_ "Usage: utf8::decode(sv)");
389 {
390 SV * sv = ST(0);
391 bool RETVAL;
392
393 RETVAL = sv_utf8_decode(sv);
394 ST(0) = boolSV(RETVAL);
395 sv_2mortal(ST(0));
396 }
397 XSRETURN(1);
398}
399
400XS(XS_utf8_upgrade)
401{
402 dXSARGS;
403 if (items != 1)
404 Perl_croak(aTHX_ "Usage: utf8::upgrade(sv)");
405 {
406 SV * sv = ST(0);
407 STRLEN RETVAL;
408 dXSTARG;
409
410 RETVAL = sv_utf8_upgrade(sv);
411 XSprePUSH; PUSHi((IV)RETVAL);
412 }
413 XSRETURN(1);
414}
415
416XS(XS_utf8_downgrade)
417{
418 dXSARGS;
419 if (items < 1 || items > 2)
420 Perl_croak(aTHX_ "Usage: utf8::downgrade(sv, failok=0)");
421 {
422 SV * sv = ST(0);
423 bool failok;
424 bool RETVAL;
425
426 if (items < 2)
427 failok = 0;
428 else {
429 failok = (int)SvIV(ST(1));
430 }
431
432 RETVAL = sv_utf8_downgrade(sv, failok);
433 ST(0) = boolSV(RETVAL);
434 sv_2mortal(ST(0));
435 }
436 XSRETURN(1);
437}
438
439XS(XS_utf8_native_to_unicode)
440{
441 dXSARGS;
442 UV uv = SvUV(ST(0));
443
444 if (items > 1)
445 Perl_croak(aTHX_ "Usage: utf8::native_to_unicode(sv)");
446
447 ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv)));
448 XSRETURN(1);
449}
450
451XS(XS_utf8_unicode_to_native)
452{
453 dXSARGS;
454 UV uv = SvUV(ST(0));
455
456 if (items > 1)
457 Perl_croak(aTHX_ "Usage: utf8::unicode_to_native(sv)");
458
459 ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv)));
460 XSRETURN(1);
461}
462
463XS(XS_access_readonly)
464{
465 dXSARGS;
466 SV *sv = SvRV(ST(0));
467 IV old = SvREADONLY(sv);
468 if (items == 2) {
469 if (SvTRUE(ST(1))) {
470 SvREADONLY_on(sv);
471 }
472 else {
473 SvREADONLY_off(sv);
474 }
475 }
476 if (old)
477 XSRETURN_YES;
478 else
479 XSRETURN_NO;
480}
481