Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | /* sv.c |
79072805 | 2 | * |
4bb101f2 | 3 | * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
241d1a3b | 4 | * 2000, 2001, 2002, 2003, 2004, 2005, by Larry Wall and others |
79072805 LW |
5 | * |
6 | * You may distribute under the terms of either the GNU General Public | |
7 | * License or the Artistic License, as specified in the README file. | |
8 | * | |
a0d0e21e | 9 | * "I wonder what the Entish is for 'yes' and 'no'," he thought. |
645c22ef DM |
10 | * |
11 | * | |
5e045b90 AMS |
12 | * This file contains the code that creates, manipulates and destroys |
13 | * scalar values (SVs). The other types (AV, HV, GV, etc.) reuse the | |
14 | * structure of an SV, so their creation and destruction is handled | |
15 | * here; higher-level functions are in av.c, hv.c, and so on. Opcode | |
16 | * level functions (eg. substr, split, join) for each of the types are | |
17 | * in the pp*.c files. | |
79072805 LW |
18 | */ |
19 | ||
20 | #include "EXTERN.h" | |
864dbfa3 | 21 | #define PERL_IN_SV_C |
79072805 | 22 | #include "perl.h" |
d2f185dc | 23 | #include "regcomp.h" |
79072805 | 24 | |
51371543 | 25 | #define FCALL *f |
2c5424a7 | 26 | |
2f8ed50e OS |
27 | #ifdef __Lynx__ |
28 | /* Missing proto on LynxOS */ | |
29 | char *gconvert(double, int, int, char *); | |
30 | #endif | |
31 | ||
e23c8137 JH |
32 | #ifdef PERL_UTF8_CACHE_ASSERT |
33 | /* The cache element 0 is the Unicode offset; | |
34 | * the cache element 1 is the byte offset of the element 0; | |
35 | * the cache element 2 is the Unicode length of the substring; | |
36 | * the cache element 3 is the byte length of the substring; | |
37 | * The checking of the substring side would be good | |
38 | * but substr() has enough code paths to make my head spin; | |
39 | * if adding more checks watch out for the following tests: | |
40 | * t/op/index.t t/op/length.t t/op/pat.t t/op/substr.t | |
41 | * lib/utf8.t lib/Unicode/Collate/t/index.t | |
42 | * --jhi | |
43 | */ | |
44 | #define ASSERT_UTF8_CACHE(cache) \ | |
45 | STMT_START { if (cache) { assert((cache)[0] <= (cache)[1]); } } STMT_END | |
46 | #else | |
47 | #define ASSERT_UTF8_CACHE(cache) NOOP | |
48 | #endif | |
49 | ||
f8c7b90f | 50 | #ifdef PERL_OLD_COPY_ON_WRITE |
765f542d | 51 | #define SV_COW_NEXT_SV(sv) INT2PTR(SV *,SvUVX(sv)) |
607fa7f2 | 52 | #define SV_COW_NEXT_SV_SET(current,next) SvUV_set(current, PTR2UV(next)) |
b5ccf5f2 | 53 | /* This is a pessimistic view. Scalar must be purely a read-write PV to copy- |
765f542d | 54 | on-write. */ |
765f542d | 55 | #endif |
645c22ef DM |
56 | |
57 | /* ============================================================================ | |
58 | ||
59 | =head1 Allocation and deallocation of SVs. | |
60 | ||
5e045b90 AMS |
61 | An SV (or AV, HV, etc.) is allocated in two parts: the head (struct sv, |
62 | av, hv...) contains type and reference count information, as well as a | |
63 | pointer to the body (struct xrv, xpv, xpviv...), which contains fields | |
64 | specific to each type. | |
65 | ||
4977e971 NC |
66 | Normally, this allocation is done using arenas, which by default are |
67 | approximately 4K chunks of memory parcelled up into N heads or bodies. The | |
68 | first slot in each arena is reserved, and is used to hold a link to the next | |
69 | arena. In the case of heads, the unused first slot also contains some flags | |
70 | and a note of the number of slots. Snaked through each arena chain is a | |
5e045b90 | 71 | linked list of free items; when this becomes empty, an extra arena is |
4977e971 | 72 | allocated and divided up into N items which are threaded into the free list. |
645c22ef DM |
73 | |
74 | The following global variables are associated with arenas: | |
75 | ||
76 | PL_sv_arenaroot pointer to list of SV arenas | |
77 | PL_sv_root pointer to list of free SV structures | |
78 | ||
79 | PL_foo_arenaroot pointer to list of foo arenas, | |
80 | PL_foo_root pointer to list of free foo bodies | |
81 | ... for foo in xiv, xnv, xrv, xpv etc. | |
82 | ||
83 | Note that some of the larger and more rarely used body types (eg xpvio) | |
84 | are not allocated using arenas, but are instead just malloc()/free()ed as | |
85 | required. Also, if PURIFY is defined, arenas are abandoned altogether, | |
86 | with all items individually malloc()ed. In addition, a few SV heads are | |
87 | not allocated from an arena, but are instead directly created as static | |
4977e971 NC |
88 | or auto variables, eg PL_sv_undef. The size of arenas can be changed from |
89 | the default by setting PERL_ARENA_SIZE appropriately at compile time. | |
645c22ef DM |
90 | |
91 | The SV arena serves the secondary purpose of allowing still-live SVs | |
92 | to be located and destroyed during final cleanup. | |
93 | ||
94 | At the lowest level, the macros new_SV() and del_SV() grab and free | |
95 | an SV head. (If debugging with -DD, del_SV() calls the function S_del_sv() | |
96 | to return the SV to the free list with error checking.) new_SV() calls | |
97 | more_sv() / sv_add_arena() to add an extra arena if the free list is empty. | |
98 | SVs in the free list have their SvTYPE field set to all ones. | |
99 | ||
100 | Similarly, there are macros new_XIV()/del_XIV(), new_XNV()/del_XNV() etc | |
101 | that allocate and return individual body types. Normally these are mapped | |
ff276b08 RG |
102 | to the arena-manipulating functions new_xiv()/del_xiv() etc, but may be |
103 | instead mapped directly to malloc()/free() if PURIFY is defined. The | |
645c22ef DM |
104 | new/del functions remove from, or add to, the appropriate PL_foo_root |
105 | list, and call more_xiv() etc to add a new arena if the list is empty. | |
106 | ||
ff276b08 | 107 | At the time of very final cleanup, sv_free_arenas() is called from |
645c22ef DM |
108 | perl_destruct() to physically free all the arenas allocated since the |
109 | start of the interpreter. Note that this also clears PL_he_arenaroot, | |
110 | which is otherwise dealt with in hv.c. | |
111 | ||
112 | Manipulation of any of the PL_*root pointers is protected by enclosing | |
113 | LOCK_SV_MUTEX; ... UNLOCK_SV_MUTEX calls which should Do the Right Thing | |
114 | if threads are enabled. | |
115 | ||
116 | The function visit() scans the SV arenas list, and calls a specified | |
117 | function for each SV it finds which is still live - ie which has an SvTYPE | |
118 | other than all 1's, and a non-zero SvREFCNT. visit() is used by the | |
119 | following functions (specified as [function that calls visit()] / [function | |
120 | called by visit() for each SV]): | |
121 | ||
122 | sv_report_used() / do_report_used() | |
123 | dump all remaining SVs (debugging aid) | |
124 | ||
125 | sv_clean_objs() / do_clean_objs(),do_clean_named_objs() | |
126 | Attempt to free all objects pointed to by RVs, | |
127 | and, unless DISABLE_DESTRUCTOR_KLUDGE is defined, | |
128 | try to do the same for all objects indirectly | |
129 | referenced by typeglobs too. Called once from | |
130 | perl_destruct(), prior to calling sv_clean_all() | |
131 | below. | |
132 | ||
133 | sv_clean_all() / do_clean_all() | |
134 | SvREFCNT_dec(sv) each remaining SV, possibly | |
135 | triggering an sv_free(). It also sets the | |
136 | SVf_BREAK flag on the SV to indicate that the | |
137 | refcnt has been artificially lowered, and thus | |
138 | stopping sv_free() from giving spurious warnings | |
139 | about SVs which unexpectedly have a refcnt | |
140 | of zero. called repeatedly from perl_destruct() | |
141 | until there are no SVs left. | |
142 | ||
143 | =head2 Summary | |
144 | ||
145 | Private API to rest of sv.c | |
146 | ||
147 | new_SV(), del_SV(), | |
148 | ||
149 | new_XIV(), del_XIV(), | |
150 | new_XNV(), del_XNV(), | |
151 | etc | |
152 | ||
153 | Public API: | |
154 | ||
8cf8f3d1 | 155 | sv_report_used(), sv_clean_objs(), sv_clean_all(), sv_free_arenas() |
645c22ef DM |
156 | |
157 | ||
158 | =cut | |
159 | ||
160 | ============================================================================ */ | |
161 | ||
162 | ||
51371543 | 163 | |
4561caa4 CS |
164 | /* |
165 | * "A time to plant, and a time to uproot what was planted..." | |
166 | */ | |
167 | ||
cac9b346 | 168 | |
fd0854ff DM |
169 | #ifdef DEBUG_LEAKING_SCALARS |
170 | # ifdef NETWARE | |
171 | # define FREE_SV_DEBUG_FILE(sv) PerlMemfree((sv)->sv_debug_file) | |
172 | # else | |
173 | # define FREE_SV_DEBUG_FILE(sv) PerlMemShared_free((sv)->sv_debug_file) | |
174 | # endif | |
175 | #else | |
176 | # define FREE_SV_DEBUG_FILE(sv) | |
177 | #endif | |
178 | ||
053fc874 GS |
179 | #define plant_SV(p) \ |
180 | STMT_START { \ | |
fd0854ff | 181 | FREE_SV_DEBUG_FILE(p); \ |
053fc874 GS |
182 | SvANY(p) = (void *)PL_sv_root; \ |
183 | SvFLAGS(p) = SVTYPEMASK; \ | |
184 | PL_sv_root = (p); \ | |
185 | --PL_sv_count; \ | |
186 | } STMT_END | |
a0d0e21e | 187 | |
fba3b22e | 188 | /* sv_mutex must be held while calling uproot_SV() */ |
053fc874 GS |
189 | #define uproot_SV(p) \ |
190 | STMT_START { \ | |
191 | (p) = PL_sv_root; \ | |
192 | PL_sv_root = (SV*)SvANY(p); \ | |
193 | ++PL_sv_count; \ | |
194 | } STMT_END | |
195 | ||
645c22ef | 196 | |
cac9b346 NC |
197 | /* make some more SVs by adding another arena */ |
198 | ||
199 | /* sv_mutex must be held while calling more_sv() */ | |
200 | STATIC SV* | |
201 | S_more_sv(pTHX) | |
202 | { | |
203 | SV* sv; | |
204 | ||
205 | if (PL_nice_chunk) { | |
206 | sv_add_arena(PL_nice_chunk, PL_nice_chunk_size, 0); | |
207 | PL_nice_chunk = Nullch; | |
208 | PL_nice_chunk_size = 0; | |
209 | } | |
210 | else { | |
211 | char *chunk; /* must use New here to match call to */ | |
2e7ed132 NC |
212 | New(704,chunk,PERL_ARENA_SIZE,char); /* Safefree() in sv_free_arenas() */ |
213 | sv_add_arena(chunk, PERL_ARENA_SIZE, 0); | |
cac9b346 NC |
214 | } |
215 | uproot_SV(sv); | |
216 | return sv; | |
217 | } | |
218 | ||
645c22ef DM |
219 | /* new_SV(): return a new, empty SV head */ |
220 | ||
eba0f806 DM |
221 | #ifdef DEBUG_LEAKING_SCALARS |
222 | /* provide a real function for a debugger to play with */ | |
223 | STATIC SV* | |
224 | S_new_SV(pTHX) | |
225 | { | |
226 | SV* sv; | |
227 | ||
228 | LOCK_SV_MUTEX; | |
229 | if (PL_sv_root) | |
230 | uproot_SV(sv); | |
231 | else | |
cac9b346 | 232 | sv = S_more_sv(aTHX); |
eba0f806 DM |
233 | UNLOCK_SV_MUTEX; |
234 | SvANY(sv) = 0; | |
235 | SvREFCNT(sv) = 1; | |
236 | SvFLAGS(sv) = 0; | |
fd0854ff DM |
237 | sv->sv_debug_optype = PL_op ? PL_op->op_type : 0; |
238 | sv->sv_debug_line = (U16) ((PL_copline == NOLINE) ? | |
239 | (PL_curcop ? CopLINE(PL_curcop) : 0) : PL_copline); | |
240 | sv->sv_debug_inpad = 0; | |
241 | sv->sv_debug_cloned = 0; | |
242 | # ifdef NETWARE | |
243 | sv->sv_debug_file = PL_curcop ? savepv(CopFILE(PL_curcop)): NULL; | |
244 | # else | |
245 | sv->sv_debug_file = PL_curcop ? savesharedpv(CopFILE(PL_curcop)): NULL; | |
246 | # endif | |
247 | ||
eba0f806 DM |
248 | return sv; |
249 | } | |
250 | # define new_SV(p) (p)=S_new_SV(aTHX) | |
251 | ||
252 | #else | |
253 | # define new_SV(p) \ | |
053fc874 GS |
254 | STMT_START { \ |
255 | LOCK_SV_MUTEX; \ | |
256 | if (PL_sv_root) \ | |
257 | uproot_SV(p); \ | |
258 | else \ | |
cac9b346 | 259 | (p) = S_more_sv(aTHX); \ |
053fc874 GS |
260 | UNLOCK_SV_MUTEX; \ |
261 | SvANY(p) = 0; \ | |
262 | SvREFCNT(p) = 1; \ | |
263 | SvFLAGS(p) = 0; \ | |
264 | } STMT_END | |
eba0f806 | 265 | #endif |
463ee0b2 | 266 | |
645c22ef DM |
267 | |
268 | /* del_SV(): return an empty SV head to the free list */ | |
269 | ||
a0d0e21e | 270 | #ifdef DEBUGGING |
4561caa4 | 271 | |
053fc874 GS |
272 | #define del_SV(p) \ |
273 | STMT_START { \ | |
274 | LOCK_SV_MUTEX; \ | |
aea4f609 | 275 | if (DEBUG_D_TEST) \ |
053fc874 GS |
276 | del_sv(p); \ |
277 | else \ | |
278 | plant_SV(p); \ | |
279 | UNLOCK_SV_MUTEX; \ | |
280 | } STMT_END | |
a0d0e21e | 281 | |
76e3520e | 282 | STATIC void |
cea2e8a9 | 283 | S_del_sv(pTHX_ SV *p) |
463ee0b2 | 284 | { |
aea4f609 | 285 | if (DEBUG_D_TEST) { |
4633a7c4 | 286 | SV* sva; |
a3b680e6 | 287 | bool ok = 0; |
3280af22 | 288 | for (sva = PL_sv_arenaroot; sva; sva = (SV *) SvANY(sva)) { |
a3b680e6 AL |
289 | SV *sv = sva + 1; |
290 | SV *svend = &sva[SvREFCNT(sva)]; | |
c0ff570e | 291 | if (p >= sv && p < svend) { |
a0d0e21e | 292 | ok = 1; |
c0ff570e NC |
293 | break; |
294 | } | |
a0d0e21e LW |
295 | } |
296 | if (!ok) { | |
0453d815 | 297 | if (ckWARN_d(WARN_INTERNAL)) |
9014280d | 298 | Perl_warner(aTHX_ packWARN(WARN_INTERNAL), |
472d47bc SB |
299 | "Attempt to free non-arena SV: 0x%"UVxf |
300 | pTHX__FORMAT, PTR2UV(p) pTHX__VALUE); | |
a0d0e21e LW |
301 | return; |
302 | } | |
303 | } | |
4561caa4 | 304 | plant_SV(p); |
463ee0b2 | 305 | } |
a0d0e21e | 306 | |
4561caa4 CS |
307 | #else /* ! DEBUGGING */ |
308 | ||
309 | #define del_SV(p) plant_SV(p) | |
310 | ||
311 | #endif /* DEBUGGING */ | |
463ee0b2 | 312 | |
645c22ef DM |
313 | |
314 | /* | |
ccfc67b7 JH |
315 | =head1 SV Manipulation Functions |
316 | ||
645c22ef DM |
317 | =for apidoc sv_add_arena |
318 | ||
319 | Given a chunk of memory, link it to the head of the list of arenas, | |
320 | and split it into a list of free SVs. | |
321 | ||
322 | =cut | |
323 | */ | |
324 | ||
4633a7c4 | 325 | void |
864dbfa3 | 326 | Perl_sv_add_arena(pTHX_ char *ptr, U32 size, U32 flags) |
463ee0b2 | 327 | { |
4633a7c4 | 328 | SV* sva = (SV*)ptr; |
463ee0b2 LW |
329 | register SV* sv; |
330 | register SV* svend; | |
4633a7c4 LW |
331 | |
332 | /* The first SV in an arena isn't an SV. */ | |
3280af22 | 333 | SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */ |
4633a7c4 LW |
334 | SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */ |
335 | SvFLAGS(sva) = flags; /* FAKE if not to be freed */ | |
336 | ||
3280af22 NIS |
337 | PL_sv_arenaroot = sva; |
338 | PL_sv_root = sva + 1; | |
4633a7c4 LW |
339 | |
340 | svend = &sva[SvREFCNT(sva) - 1]; | |
341 | sv = sva + 1; | |
463ee0b2 | 342 | while (sv < svend) { |
a0d0e21e | 343 | SvANY(sv) = (void *)(SV*)(sv + 1); |
03e36789 | 344 | #ifdef DEBUGGING |
978b032e | 345 | SvREFCNT(sv) = 0; |
03e36789 NC |
346 | #endif |
347 | /* Must always set typemask because it's awlays checked in on cleanup | |
348 | when the arenas are walked looking for objects. */ | |
8990e307 | 349 | SvFLAGS(sv) = SVTYPEMASK; |
463ee0b2 LW |
350 | sv++; |
351 | } | |
352 | SvANY(sv) = 0; | |
03e36789 NC |
353 | #ifdef DEBUGGING |
354 | SvREFCNT(sv) = 0; | |
355 | #endif | |
4633a7c4 LW |
356 | SvFLAGS(sv) = SVTYPEMASK; |
357 | } | |
358 | ||
055972dc DM |
359 | /* visit(): call the named function for each non-free SV in the arenas |
360 | * whose flags field matches the flags/mask args. */ | |
645c22ef | 361 | |
5226ed68 | 362 | STATIC I32 |
055972dc | 363 | S_visit(pTHX_ SVFUNC_t f, U32 flags, U32 mask) |
8990e307 | 364 | { |
4633a7c4 | 365 | SV* sva; |
5226ed68 | 366 | I32 visited = 0; |
8990e307 | 367 | |
3280af22 | 368 | for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) { |
a3b680e6 AL |
369 | register SV * const svend = &sva[SvREFCNT(sva)]; |
370 | register SV* sv; | |
4561caa4 | 371 | for (sv = sva + 1; sv < svend; ++sv) { |
055972dc DM |
372 | if (SvTYPE(sv) != SVTYPEMASK |
373 | && (sv->sv_flags & mask) == flags | |
374 | && SvREFCNT(sv)) | |
375 | { | |
acfe0abc | 376 | (FCALL)(aTHX_ sv); |
5226ed68 JH |
377 | ++visited; |
378 | } | |
8990e307 LW |
379 | } |
380 | } | |
5226ed68 | 381 | return visited; |
8990e307 LW |
382 | } |
383 | ||
758a08c3 JH |
384 | #ifdef DEBUGGING |
385 | ||
645c22ef DM |
386 | /* called by sv_report_used() for each live SV */ |
387 | ||
388 | static void | |
acfe0abc | 389 | do_report_used(pTHX_ SV *sv) |
645c22ef DM |
390 | { |
391 | if (SvTYPE(sv) != SVTYPEMASK) { | |
392 | PerlIO_printf(Perl_debug_log, "****\n"); | |
393 | sv_dump(sv); | |
394 | } | |
395 | } | |
758a08c3 | 396 | #endif |
645c22ef DM |
397 | |
398 | /* | |
399 | =for apidoc sv_report_used | |
400 | ||
401 | Dump the contents of all SVs not yet freed. (Debugging aid). | |
402 | ||
403 | =cut | |
404 | */ | |
405 | ||
8990e307 | 406 | void |
864dbfa3 | 407 | Perl_sv_report_used(pTHX) |
4561caa4 | 408 | { |
ff270d3a | 409 | #ifdef DEBUGGING |
055972dc | 410 | visit(do_report_used, 0, 0); |
ff270d3a | 411 | #endif |
4561caa4 CS |
412 | } |
413 | ||
645c22ef DM |
414 | /* called by sv_clean_objs() for each live SV */ |
415 | ||
416 | static void | |
acfe0abc | 417 | do_clean_objs(pTHX_ SV *sv) |
645c22ef DM |
418 | { |
419 | SV* rv; | |
420 | ||
421 | if (SvROK(sv) && SvOBJECT(rv = SvRV(sv))) { | |
422 | DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(sv))); | |
423 | if (SvWEAKREF(sv)) { | |
424 | sv_del_backref(sv); | |
425 | SvWEAKREF_off(sv); | |
b162af07 | 426 | SvRV_set(sv, NULL); |
645c22ef DM |
427 | } else { |
428 | SvROK_off(sv); | |
b162af07 | 429 | SvRV_set(sv, NULL); |
645c22ef DM |
430 | SvREFCNT_dec(rv); |
431 | } | |
432 | } | |
433 | ||
434 | /* XXX Might want to check arrays, etc. */ | |
435 | } | |
436 | ||
437 | /* called by sv_clean_objs() for each live SV */ | |
438 | ||
439 | #ifndef DISABLE_DESTRUCTOR_KLUDGE | |
440 | static void | |
acfe0abc | 441 | do_clean_named_objs(pTHX_ SV *sv) |
645c22ef DM |
442 | { |
443 | if (SvTYPE(sv) == SVt_PVGV && GvGP(sv)) { | |
444 | if ( SvOBJECT(GvSV(sv)) || | |
445 | (GvAV(sv) && SvOBJECT(GvAV(sv))) || | |
446 | (GvHV(sv) && SvOBJECT(GvHV(sv))) || | |
447 | (GvIO(sv) && SvOBJECT(GvIO(sv))) || | |
448 | (GvCV(sv) && SvOBJECT(GvCV(sv))) ) | |
449 | { | |
450 | DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning named glob object:\n "), sv_dump(sv))); | |
ec5f3c78 | 451 | SvFLAGS(sv) |= SVf_BREAK; |
645c22ef DM |
452 | SvREFCNT_dec(sv); |
453 | } | |
454 | } | |
455 | } | |
456 | #endif | |
457 | ||
458 | /* | |
459 | =for apidoc sv_clean_objs | |
460 | ||
461 | Attempt to destroy all objects not yet freed | |
462 | ||
463 | =cut | |
464 | */ | |
465 | ||
4561caa4 | 466 | void |
864dbfa3 | 467 | Perl_sv_clean_objs(pTHX) |
4561caa4 | 468 | { |
3280af22 | 469 | PL_in_clean_objs = TRUE; |
055972dc | 470 | visit(do_clean_objs, SVf_ROK, SVf_ROK); |
4561caa4 | 471 | #ifndef DISABLE_DESTRUCTOR_KLUDGE |
2d0f3c12 | 472 | /* some barnacles may yet remain, clinging to typeglobs */ |
055972dc | 473 | visit(do_clean_named_objs, SVt_PVGV, SVTYPEMASK); |
4561caa4 | 474 | #endif |
3280af22 | 475 | PL_in_clean_objs = FALSE; |
4561caa4 CS |
476 | } |
477 | ||
645c22ef DM |
478 | /* called by sv_clean_all() for each live SV */ |
479 | ||
480 | static void | |
acfe0abc | 481 | do_clean_all(pTHX_ SV *sv) |
645c22ef DM |
482 | { |
483 | DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) )); | |
484 | SvFLAGS(sv) |= SVf_BREAK; | |
0e705b3b DM |
485 | if (PL_comppad == (AV*)sv) { |
486 | PL_comppad = Nullav; | |
487 | PL_curpad = Null(SV**); | |
488 | } | |
645c22ef DM |
489 | SvREFCNT_dec(sv); |
490 | } | |
491 | ||
492 | /* | |
493 | =for apidoc sv_clean_all | |
494 | ||
495 | Decrement the refcnt of each remaining SV, possibly triggering a | |
496 | cleanup. This function may have to be called multiple times to free | |
ff276b08 | 497 | SVs which are in complex self-referential hierarchies. |
645c22ef DM |
498 | |
499 | =cut | |
500 | */ | |
501 | ||
5226ed68 | 502 | I32 |
864dbfa3 | 503 | Perl_sv_clean_all(pTHX) |
8990e307 | 504 | { |
5226ed68 | 505 | I32 cleaned; |
3280af22 | 506 | PL_in_clean_all = TRUE; |
055972dc | 507 | cleaned = visit(do_clean_all, 0,0); |
3280af22 | 508 | PL_in_clean_all = FALSE; |
5226ed68 | 509 | return cleaned; |
8990e307 | 510 | } |
463ee0b2 | 511 | |
645c22ef DM |
512 | /* |
513 | =for apidoc sv_free_arenas | |
514 | ||
515 | Deallocate the memory used by all arenas. Note that all the individual SV | |
516 | heads and bodies within the arenas must already have been freed. | |
517 | ||
518 | =cut | |
519 | */ | |
520 | ||
4633a7c4 | 521 | void |
864dbfa3 | 522 | Perl_sv_free_arenas(pTHX) |
4633a7c4 LW |
523 | { |
524 | SV* sva; | |
525 | SV* svanext; | |
7b2c381c | 526 | void *arena, *arenanext; |
4633a7c4 LW |
527 | |
528 | /* Free arenas here, but be careful about fake ones. (We assume | |
529 | contiguity of the fake ones with the corresponding real ones.) */ | |
530 | ||
3280af22 | 531 | for (sva = PL_sv_arenaroot; sva; sva = svanext) { |
4633a7c4 LW |
532 | svanext = (SV*) SvANY(sva); |
533 | while (svanext && SvFAKE(svanext)) | |
534 | svanext = (SV*) SvANY(svanext); | |
535 | ||
536 | if (!SvFAKE(sva)) | |
1df70142 | 537 | Safefree(sva); |
4633a7c4 | 538 | } |
5f05dabc | 539 | |
612f20c3 | 540 | for (arena = PL_xnv_arenaroot; arena; arena = arenanext) { |
7b2c381c | 541 | arenanext = *(void **)arena; |
612f20c3 GS |
542 | Safefree(arena); |
543 | } | |
544 | PL_xnv_arenaroot = 0; | |
bf9cdc68 | 545 | PL_xnv_root = 0; |
612f20c3 | 546 | |
612f20c3 | 547 | for (arena = PL_xpv_arenaroot; arena; arena = arenanext) { |
7b2c381c | 548 | arenanext = *(void **)arena; |
612f20c3 GS |
549 | Safefree(arena); |
550 | } | |
551 | PL_xpv_arenaroot = 0; | |
bf9cdc68 | 552 | PL_xpv_root = 0; |
612f20c3 | 553 | |
7b2c381c NC |
554 | for (arena = PL_xpviv_arenaroot; arena; arena = arenanext) { |
555 | arenanext = *(void **)arena; | |
612f20c3 GS |
556 | Safefree(arena); |
557 | } | |
558 | PL_xpviv_arenaroot = 0; | |
bf9cdc68 | 559 | PL_xpviv_root = 0; |
612f20c3 | 560 | |
7b2c381c NC |
561 | for (arena = PL_xpvnv_arenaroot; arena; arena = arenanext) { |
562 | arenanext = *(void **)arena; | |
612f20c3 GS |
563 | Safefree(arena); |
564 | } | |
565 | PL_xpvnv_arenaroot = 0; | |
bf9cdc68 | 566 | PL_xpvnv_root = 0; |
612f20c3 | 567 | |
7b2c381c NC |
568 | for (arena = PL_xpvcv_arenaroot; arena; arena = arenanext) { |
569 | arenanext = *(void **)arena; | |
612f20c3 GS |
570 | Safefree(arena); |
571 | } | |
572 | PL_xpvcv_arenaroot = 0; | |
bf9cdc68 | 573 | PL_xpvcv_root = 0; |
612f20c3 | 574 | |
7b2c381c NC |
575 | for (arena = PL_xpvav_arenaroot; arena; arena = arenanext) { |
576 | arenanext = *(void **)arena; | |
612f20c3 GS |
577 | Safefree(arena); |
578 | } | |
579 | PL_xpvav_arenaroot = 0; | |
bf9cdc68 | 580 | PL_xpvav_root = 0; |
612f20c3 | 581 | |
7b2c381c NC |
582 | for (arena = PL_xpvhv_arenaroot; arena; arena = arenanext) { |
583 | arenanext = *(void **)arena; | |
612f20c3 GS |
584 | Safefree(arena); |
585 | } | |
586 | PL_xpvhv_arenaroot = 0; | |
bf9cdc68 | 587 | PL_xpvhv_root = 0; |
612f20c3 | 588 | |
7b2c381c NC |
589 | for (arena = PL_xpvmg_arenaroot; arena; arena = arenanext) { |
590 | arenanext = *(void **)arena; | |
612f20c3 GS |
591 | Safefree(arena); |
592 | } | |
593 | PL_xpvmg_arenaroot = 0; | |
bf9cdc68 | 594 | PL_xpvmg_root = 0; |
612f20c3 | 595 | |
7b2c381c NC |
596 | for (arena = PL_xpvgv_arenaroot; arena; arena = arenanext) { |
597 | arenanext = *(void **)arena; | |
727879eb NC |
598 | Safefree(arena); |
599 | } | |
600 | PL_xpvgv_arenaroot = 0; | |
601 | PL_xpvgv_root = 0; | |
602 | ||
7b2c381c NC |
603 | for (arena = PL_xpvlv_arenaroot; arena; arena = arenanext) { |
604 | arenanext = *(void **)arena; | |
612f20c3 GS |
605 | Safefree(arena); |
606 | } | |
607 | PL_xpvlv_arenaroot = 0; | |
bf9cdc68 | 608 | PL_xpvlv_root = 0; |
612f20c3 | 609 | |
7b2c381c NC |
610 | for (arena = PL_xpvbm_arenaroot; arena; arena = arenanext) { |
611 | arenanext = *(void **)arena; | |
612f20c3 GS |
612 | Safefree(arena); |
613 | } | |
614 | PL_xpvbm_arenaroot = 0; | |
bf9cdc68 | 615 | PL_xpvbm_root = 0; |
612f20c3 | 616 | |
b1135e3d NC |
617 | { |
618 | HE *he; | |
619 | HE *he_next; | |
620 | for (he = PL_he_arenaroot; he; he = he_next) { | |
621 | he_next = HeNEXT(he); | |
622 | Safefree(he); | |
623 | } | |
612f20c3 GS |
624 | } |
625 | PL_he_arenaroot = 0; | |
bf9cdc68 | 626 | PL_he_root = 0; |
612f20c3 | 627 | |
892b45be | 628 | #if defined(USE_ITHREADS) |
b1135e3d NC |
629 | { |
630 | struct ptr_tbl_ent *pte; | |
631 | struct ptr_tbl_ent *pte_next; | |
632 | for (pte = PL_pte_arenaroot; pte; pte = pte_next) { | |
633 | pte_next = pte->next; | |
634 | Safefree(pte); | |
635 | } | |
32e691d0 NC |
636 | } |
637 | PL_pte_arenaroot = 0; | |
638 | PL_pte_root = 0; | |
892b45be | 639 | #endif |
32e691d0 | 640 | |
3280af22 NIS |
641 | if (PL_nice_chunk) |
642 | Safefree(PL_nice_chunk); | |
643 | PL_nice_chunk = Nullch; | |
644 | PL_nice_chunk_size = 0; | |
645 | PL_sv_arenaroot = 0; | |
646 | PL_sv_root = 0; | |
4633a7c4 LW |
647 | } |
648 | ||
29489e7c DM |
649 | /* --------------------------------------------------------------------- |
650 | * | |
651 | * support functions for report_uninit() | |
652 | */ | |
653 | ||
654 | /* the maxiumum size of array or hash where we will scan looking | |
655 | * for the undefined element that triggered the warning */ | |
656 | ||
657 | #define FUV_MAX_SEARCH_SIZE 1000 | |
658 | ||
659 | /* Look for an entry in the hash whose value has the same SV as val; | |
660 | * If so, return a mortal copy of the key. */ | |
661 | ||
662 | STATIC SV* | |
663 | S_find_hash_subscript(pTHX_ HV *hv, SV* val) | |
664 | { | |
27da23d5 | 665 | dVAR; |
29489e7c | 666 | register HE **array; |
29489e7c DM |
667 | I32 i; |
668 | ||
669 | if (!hv || SvMAGICAL(hv) || !HvARRAY(hv) || | |
670 | (HvTOTALKEYS(hv) > FUV_MAX_SEARCH_SIZE)) | |
671 | return Nullsv; | |
672 | ||
673 | array = HvARRAY(hv); | |
674 | ||
675 | for (i=HvMAX(hv); i>0; i--) { | |
f54cb97a | 676 | register HE *entry; |
29489e7c DM |
677 | for (entry = array[i]; entry; entry = HeNEXT(entry)) { |
678 | if (HeVAL(entry) != val) | |
679 | continue; | |
680 | if ( HeVAL(entry) == &PL_sv_undef || | |
681 | HeVAL(entry) == &PL_sv_placeholder) | |
682 | continue; | |
683 | if (!HeKEY(entry)) | |
684 | return Nullsv; | |
685 | if (HeKLEN(entry) == HEf_SVKEY) | |
686 | return sv_mortalcopy(HeKEY_sv(entry)); | |
687 | return sv_2mortal(newSVpvn(HeKEY(entry), HeKLEN(entry))); | |
688 | } | |
689 | } | |
690 | return Nullsv; | |
691 | } | |
692 | ||
693 | /* Look for an entry in the array whose value has the same SV as val; | |
694 | * If so, return the index, otherwise return -1. */ | |
695 | ||
696 | STATIC I32 | |
697 | S_find_array_subscript(pTHX_ AV *av, SV* val) | |
698 | { | |
699 | SV** svp; | |
700 | I32 i; | |
701 | if (!av || SvMAGICAL(av) || !AvARRAY(av) || | |
702 | (AvFILLp(av) > FUV_MAX_SEARCH_SIZE)) | |
703 | return -1; | |
704 | ||
705 | svp = AvARRAY(av); | |
706 | for (i=AvFILLp(av); i>=0; i--) { | |
707 | if (svp[i] == val && svp[i] != &PL_sv_undef) | |
708 | return i; | |
709 | } | |
710 | return -1; | |
711 | } | |
712 | ||
713 | /* S_varname(): return the name of a variable, optionally with a subscript. | |
714 | * If gv is non-zero, use the name of that global, along with gvtype (one | |
715 | * of "$", "@", "%"); otherwise use the name of the lexical at pad offset | |
716 | * targ. Depending on the value of the subscript_type flag, return: | |
717 | */ | |
718 | ||
719 | #define FUV_SUBSCRIPT_NONE 1 /* "@foo" */ | |
720 | #define FUV_SUBSCRIPT_ARRAY 2 /* "$foo[aindex]" */ | |
721 | #define FUV_SUBSCRIPT_HASH 3 /* "$foo{keyname}" */ | |
722 | #define FUV_SUBSCRIPT_WITHIN 4 /* "within @foo" */ | |
723 | ||
724 | STATIC SV* | |
bfed75c6 | 725 | S_varname(pTHX_ GV *gv, const char *gvtype, PADOFFSET targ, |
29489e7c DM |
726 | SV* keyname, I32 aindex, int subscript_type) |
727 | { | |
728 | AV *av; | |
a3b680e6 | 729 | SV *sv; |
29489e7c | 730 | |
a3b680e6 | 731 | SV * const name = sv_newmortal(); |
29489e7c DM |
732 | if (gv) { |
733 | ||
734 | /* simulate gv_fullname4(), but add literal '^' for $^FOO names | |
735 | * XXX get rid of all this if gv_fullnameX() ever supports this | |
736 | * directly */ | |
737 | ||
bfed75c6 | 738 | const char *p; |
29489e7c DM |
739 | HV *hv = GvSTASH(gv); |
740 | sv_setpv(name, gvtype); | |
741 | if (!hv) | |
742 | p = "???"; | |
bfcb3514 | 743 | else if (!(p=HvNAME_get(hv))) |
29489e7c | 744 | p = "__ANON__"; |
29489e7c DM |
745 | if (strNE(p, "main")) { |
746 | sv_catpv(name,p); | |
747 | sv_catpvn(name,"::", 2); | |
748 | } | |
749 | if (GvNAMELEN(gv)>= 1 && | |
750 | ((unsigned int)*GvNAME(gv)) <= 26) | |
751 | { /* handle $^FOO */ | |
752 | Perl_sv_catpvf(aTHX_ name,"^%c", *GvNAME(gv) + 'A' - 1); | |
753 | sv_catpvn(name,GvNAME(gv)+1,GvNAMELEN(gv)-1); | |
754 | } | |
755 | else | |
756 | sv_catpvn(name,GvNAME(gv),GvNAMELEN(gv)); | |
757 | } | |
758 | else { | |
759 | U32 u; | |
760 | CV *cv = find_runcv(&u); | |
761 | if (!cv || !CvPADLIST(cv)) | |
762 | return Nullsv;; | |
763 | av = (AV*)(*av_fetch(CvPADLIST(cv), 0, FALSE)); | |
764 | sv = *av_fetch(av, targ, FALSE); | |
765 | /* SvLEN in a pad name is not to be trusted */ | |
f9926b10 | 766 | sv_setpv(name, SvPV_nolen_const(sv)); |
29489e7c DM |
767 | } |
768 | ||
769 | if (subscript_type == FUV_SUBSCRIPT_HASH) { | |
770 | *SvPVX(name) = '$'; | |
771 | sv = NEWSV(0,0); | |
772 | Perl_sv_catpvf(aTHX_ name, "{%s}", | |
3f7c398e | 773 | pv_display(sv,SvPVX_const(keyname), SvCUR(keyname), 0, 32)); |
29489e7c DM |
774 | SvREFCNT_dec(sv); |
775 | } | |
776 | else if (subscript_type == FUV_SUBSCRIPT_ARRAY) { | |
777 | *SvPVX(name) = '$'; | |
265a12b8 | 778 | Perl_sv_catpvf(aTHX_ name, "[%"IVdf"]", (IV)aindex); |
29489e7c DM |
779 | } |
780 | else if (subscript_type == FUV_SUBSCRIPT_WITHIN) | |
781 | sv_insert(name, 0, 0, "within ", 7); | |
782 | ||
783 | return name; | |
784 | } | |
785 | ||
786 | ||
787 | /* | |
788 | =for apidoc find_uninit_var | |
789 | ||
790 | Find the name of the undefined variable (if any) that caused the operator o | |
791 | to issue a "Use of uninitialized value" warning. | |
792 | If match is true, only return a name if it's value matches uninit_sv. | |
793 | So roughly speaking, if a unary operator (such as OP_COS) generates a | |
794 | warning, then following the direct child of the op may yield an | |
795 | OP_PADSV or OP_GV that gives the name of the undefined variable. On the | |
796 | other hand, with OP_ADD there are two branches to follow, so we only print | |
797 | the variable name if we get an exact match. | |
798 | ||
799 | The name is returned as a mortal SV. | |
800 | ||
801 | Assumes that PL_op is the op that originally triggered the error, and that | |
802 | PL_comppad/PL_curpad points to the currently executing pad. | |
803 | ||
804 | =cut | |
805 | */ | |
806 | ||
807 | STATIC SV * | |
808 | S_find_uninit_var(pTHX_ OP* obase, SV* uninit_sv, bool match) | |
809 | { | |
27da23d5 | 810 | dVAR; |
29489e7c DM |
811 | SV *sv; |
812 | AV *av; | |
813 | SV **svp; | |
814 | GV *gv; | |
815 | OP *o, *o2, *kid; | |
816 | ||
817 | if (!obase || (match && (!uninit_sv || uninit_sv == &PL_sv_undef || | |
818 | uninit_sv == &PL_sv_placeholder))) | |
819 | return Nullsv; | |
820 | ||
821 | switch (obase->op_type) { | |
822 | ||
823 | case OP_RV2AV: | |
824 | case OP_RV2HV: | |
825 | case OP_PADAV: | |
826 | case OP_PADHV: | |
827 | { | |
f54cb97a AL |
828 | const bool pad = (obase->op_type == OP_PADAV || obase->op_type == OP_PADHV); |
829 | const bool hash = (obase->op_type == OP_PADHV || obase->op_type == OP_RV2HV); | |
112dcc46 RGS |
830 | I32 index = 0; |
831 | SV *keysv = Nullsv; | |
29489e7c DM |
832 | int subscript_type = FUV_SUBSCRIPT_WITHIN; |
833 | ||
834 | if (pad) { /* @lex, %lex */ | |
835 | sv = PAD_SVl(obase->op_targ); | |
836 | gv = Nullgv; | |
837 | } | |
838 | else { | |
839 | if (cUNOPx(obase)->op_first->op_type == OP_GV) { | |
840 | /* @global, %global */ | |
841 | gv = cGVOPx_gv(cUNOPx(obase)->op_first); | |
842 | if (!gv) | |
843 | break; | |
844 | sv = hash ? (SV*)GvHV(gv): (SV*)GvAV(gv); | |
845 | } | |
846 | else /* @{expr}, %{expr} */ | |
847 | return find_uninit_var(cUNOPx(obase)->op_first, | |
848 | uninit_sv, match); | |
849 | } | |
850 | ||
851 | /* attempt to find a match within the aggregate */ | |
852 | if (hash) { | |
853 | keysv = S_find_hash_subscript(aTHX_ (HV*)sv, uninit_sv); | |
854 | if (keysv) | |
855 | subscript_type = FUV_SUBSCRIPT_HASH; | |
856 | } | |
857 | else { | |
858 | index = S_find_array_subscript(aTHX_ (AV*)sv, uninit_sv); | |
859 | if (index >= 0) | |
860 | subscript_type = FUV_SUBSCRIPT_ARRAY; | |
861 | } | |
862 | ||
863 | if (match && subscript_type == FUV_SUBSCRIPT_WITHIN) | |
864 | break; | |
865 | ||
866 | return S_varname(aTHX_ gv, hash ? "%" : "@", obase->op_targ, | |
867 | keysv, index, subscript_type); | |
868 | } | |
869 | ||
870 | case OP_PADSV: | |
871 | if (match && PAD_SVl(obase->op_targ) != uninit_sv) | |
872 | break; | |
873 | return S_varname(aTHX_ Nullgv, "$", obase->op_targ, | |
874 | Nullsv, 0, FUV_SUBSCRIPT_NONE); | |
875 | ||
876 | case OP_GVSV: | |
877 | gv = cGVOPx_gv(obase); | |
878 | if (!gv || (match && GvSV(gv) != uninit_sv)) | |
879 | break; | |
880 | return S_varname(aTHX_ gv, "$", 0, Nullsv, 0, FUV_SUBSCRIPT_NONE); | |
881 | ||
882 | case OP_AELEMFAST: | |
883 | if (obase->op_flags & OPf_SPECIAL) { /* lexical array */ | |
884 | if (match) { | |
885 | av = (AV*)PAD_SV(obase->op_targ); | |
886 | if (!av || SvRMAGICAL(av)) | |
887 | break; | |
888 | svp = av_fetch(av, (I32)obase->op_private, FALSE); | |
889 | if (!svp || *svp != uninit_sv) | |
890 | break; | |
891 | } | |
892 | return S_varname(aTHX_ Nullgv, "$", obase->op_targ, | |
893 | Nullsv, (I32)obase->op_private, FUV_SUBSCRIPT_ARRAY); | |
894 | } | |
895 | else { | |
896 | gv = cGVOPx_gv(obase); | |
897 | if (!gv) | |
898 | break; | |
899 | if (match) { | |
900 | av = GvAV(gv); | |
901 | if (!av || SvRMAGICAL(av)) | |
902 | break; | |
903 | svp = av_fetch(av, (I32)obase->op_private, FALSE); | |
904 | if (!svp || *svp != uninit_sv) | |
905 | break; | |
906 | } | |
907 | return S_varname(aTHX_ gv, "$", 0, | |
908 | Nullsv, (I32)obase->op_private, FUV_SUBSCRIPT_ARRAY); | |
909 | } | |
910 | break; | |
911 | ||
912 | case OP_EXISTS: | |
913 | o = cUNOPx(obase)->op_first; | |
914 | if (!o || o->op_type != OP_NULL || | |
915 | ! (o->op_targ == OP_AELEM || o->op_targ == OP_HELEM)) | |
916 | break; | |
917 | return find_uninit_var(cBINOPo->op_last, uninit_sv, match); | |
918 | ||
919 | case OP_AELEM: | |
920 | case OP_HELEM: | |
921 | if (PL_op == obase) | |
922 | /* $a[uninit_expr] or $h{uninit_expr} */ | |
923 | return find_uninit_var(cBINOPx(obase)->op_last, uninit_sv, match); | |
924 | ||
925 | gv = Nullgv; | |
926 | o = cBINOPx(obase)->op_first; | |
927 | kid = cBINOPx(obase)->op_last; | |
928 | ||
929 | /* get the av or hv, and optionally the gv */ | |
930 | sv = Nullsv; | |
931 | if (o->op_type == OP_PADAV || o->op_type == OP_PADHV) { | |
932 | sv = PAD_SV(o->op_targ); | |
933 | } | |
934 | else if ((o->op_type == OP_RV2AV || o->op_type == OP_RV2HV) | |
935 | && cUNOPo->op_first->op_type == OP_GV) | |
936 | { | |
937 | gv = cGVOPx_gv(cUNOPo->op_first); | |
938 | if (!gv) | |
939 | break; | |
940 | sv = o->op_type == OP_RV2HV ? (SV*)GvHV(gv) : (SV*)GvAV(gv); | |
941 | } | |
942 | if (!sv) | |
943 | break; | |
944 | ||
945 | if (kid && kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid))) { | |
946 | /* index is constant */ | |
947 | if (match) { | |
948 | if (SvMAGICAL(sv)) | |
949 | break; | |
950 | if (obase->op_type == OP_HELEM) { | |
951 | HE* he = hv_fetch_ent((HV*)sv, cSVOPx_sv(kid), 0, 0); | |
952 | if (!he || HeVAL(he) != uninit_sv) | |
953 | break; | |
954 | } | |
955 | else { | |
956 | svp = av_fetch((AV*)sv, SvIV(cSVOPx_sv(kid)), FALSE); | |
957 | if (!svp || *svp != uninit_sv) | |
958 | break; | |
959 | } | |
960 | } | |
961 | if (obase->op_type == OP_HELEM) | |
962 | return S_varname(aTHX_ gv, "%", o->op_targ, | |
963 | cSVOPx_sv(kid), 0, FUV_SUBSCRIPT_HASH); | |
964 | else | |
965 | return S_varname(aTHX_ gv, "@", o->op_targ, Nullsv, | |
966 | SvIV(cSVOPx_sv(kid)), FUV_SUBSCRIPT_ARRAY); | |
967 | ; | |
968 | } | |
969 | else { | |
970 | /* index is an expression; | |
971 | * attempt to find a match within the aggregate */ | |
972 | if (obase->op_type == OP_HELEM) { | |
973 | SV *keysv = S_find_hash_subscript(aTHX_ (HV*)sv, uninit_sv); | |
974 | if (keysv) | |
975 | return S_varname(aTHX_ gv, "%", o->op_targ, | |
976 | keysv, 0, FUV_SUBSCRIPT_HASH); | |
977 | } | |
978 | else { | |
f54cb97a | 979 | const I32 index = S_find_array_subscript(aTHX_ (AV*)sv, uninit_sv); |
29489e7c | 980 | if (index >= 0) |
f54cb97a | 981 | return S_varname(aTHX_ gv, "@", o->op_targ, |
29489e7c DM |
982 | Nullsv, index, FUV_SUBSCRIPT_ARRAY); |
983 | } | |
984 | if (match) | |
985 | break; | |
986 | return S_varname(aTHX_ gv, | |
987 | (o->op_type == OP_PADAV || o->op_type == OP_RV2AV) | |
988 | ? "@" : "%", | |
989 | o->op_targ, Nullsv, 0, FUV_SUBSCRIPT_WITHIN); | |
990 | } | |
991 | ||
992 | break; | |
993 | ||
994 | case OP_AASSIGN: | |
995 | /* only examine RHS */ | |
996 | return find_uninit_var(cBINOPx(obase)->op_first, uninit_sv, match); | |
997 | ||
998 | case OP_OPEN: | |
999 | o = cUNOPx(obase)->op_first; | |
1000 | if (o->op_type == OP_PUSHMARK) | |
1001 | o = o->op_sibling; | |
1002 | ||
1003 | if (!o->op_sibling) { | |
1004 | /* one-arg version of open is highly magical */ | |
1005 | ||
1006 | if (o->op_type == OP_GV) { /* open FOO; */ | |
1007 | gv = cGVOPx_gv(o); | |
1008 | if (match && GvSV(gv) != uninit_sv) | |
1009 | break; | |
7a5fa8a2 | 1010 | return S_varname(aTHX_ gv, "$", 0, |
29489e7c DM |
1011 | Nullsv, 0, FUV_SUBSCRIPT_NONE); |
1012 | } | |
1013 | /* other possibilities not handled are: | |
1014 | * open $x; or open my $x; should return '${*$x}' | |
1015 | * open expr; should return '$'.expr ideally | |
1016 | */ | |
1017 | break; | |
1018 | } | |
1019 | goto do_op; | |
1020 | ||
1021 | /* ops where $_ may be an implicit arg */ | |
1022 | case OP_TRANS: | |
1023 | case OP_SUBST: | |
1024 | case OP_MATCH: | |
1025 | if ( !(obase->op_flags & OPf_STACKED)) { | |
1026 | if (uninit_sv == ((obase->op_private & OPpTARGET_MY) | |
1027 | ? PAD_SVl(obase->op_targ) | |
1028 | : DEFSV)) | |
1029 | { | |
1030 | sv = sv_newmortal(); | |
616d8c9c | 1031 | sv_setpvn(sv, "$_", 2); |
29489e7c DM |
1032 | return sv; |
1033 | } | |
1034 | } | |
1035 | goto do_op; | |
1036 | ||
1037 | case OP_PRTF: | |
1038 | case OP_PRINT: | |
1039 | /* skip filehandle as it can't produce 'undef' warning */ | |
1040 | o = cUNOPx(obase)->op_first; | |
1041 | if ((obase->op_flags & OPf_STACKED) && o->op_type == OP_PUSHMARK) | |
1042 | o = o->op_sibling->op_sibling; | |
1043 | goto do_op2; | |
1044 | ||
1045 | ||
e21bd382 | 1046 | case OP_RV2SV: |
29489e7c DM |
1047 | case OP_CUSTOM: |
1048 | case OP_ENTERSUB: | |
1049 | match = 1; /* XS or custom code could trigger random warnings */ | |
1050 | goto do_op; | |
1051 | ||
1052 | case OP_SCHOMP: | |
1053 | case OP_CHOMP: | |
1054 | if (SvROK(PL_rs) && uninit_sv == SvRV(PL_rs)) | |
1055 | return sv_2mortal(newSVpv("${$/}", 0)); | |
1056 | /* FALL THROUGH */ | |
1057 | ||
1058 | default: | |
1059 | do_op: | |
1060 | if (!(obase->op_flags & OPf_KIDS)) | |
1061 | break; | |
1062 | o = cUNOPx(obase)->op_first; | |
1063 | ||
1064 | do_op2: | |
1065 | if (!o) | |
1066 | break; | |
1067 | ||
1068 | /* if all except one arg are constant, or have no side-effects, | |
1069 | * or are optimized away, then it's unambiguous */ | |
1070 | o2 = Nullop; | |
1071 | for (kid=o; kid; kid = kid->op_sibling) { | |
1072 | if (kid && | |
1073 | ( (kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid))) | |
1074 | || (kid->op_type == OP_NULL && ! (kid->op_flags & OPf_KIDS)) | |
1075 | || (kid->op_type == OP_PUSHMARK) | |
1076 | ) | |
1077 | ) | |
1078 | continue; | |
1079 | if (o2) { /* more than one found */ | |
1080 | o2 = Nullop; | |
1081 | break; | |
1082 | } | |
1083 | o2 = kid; | |
1084 | } | |
1085 | if (o2) | |
1086 | return find_uninit_var(o2, uninit_sv, match); | |
1087 | ||
1088 | /* scan all args */ | |
1089 | while (o) { | |
1090 | sv = find_uninit_var(o, uninit_sv, 1); | |
1091 | if (sv) | |
1092 | return sv; | |
1093 | o = o->op_sibling; | |
1094 | } | |
1095 | break; | |
1096 | } | |
1097 | return Nullsv; | |
1098 | } | |
1099 | ||
1100 | ||
645c22ef DM |
1101 | /* |
1102 | =for apidoc report_uninit | |
1103 | ||
1104 | Print appropriate "Use of uninitialized variable" warning | |
1105 | ||
1106 | =cut | |
1107 | */ | |
1108 | ||
1d7c1841 | 1109 | void |
29489e7c DM |
1110 | Perl_report_uninit(pTHX_ SV* uninit_sv) |
1111 | { | |
1112 | if (PL_op) { | |
112dcc46 | 1113 | SV* varname = Nullsv; |
29489e7c DM |
1114 | if (uninit_sv) { |
1115 | varname = find_uninit_var(PL_op, uninit_sv,0); | |
1116 | if (varname) | |
1117 | sv_insert(varname, 0, 0, " ", 1); | |
1118 | } | |
9014280d | 1119 | Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit, |
93524f2b | 1120 | varname ? SvPV_nolen_const(varname) : "", |
29489e7c DM |
1121 | " in ", OP_DESC(PL_op)); |
1122 | } | |
1d7c1841 | 1123 | else |
29489e7c DM |
1124 | Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit, |
1125 | "", "", ""); | |
1d7c1841 GS |
1126 | } |
1127 | ||
de042e1d | 1128 | STATIC void * |
e3bbdc67 | 1129 | S_more_bodies (pTHX_ void **arena_root, void **root, size_t size) |
cac9b346 | 1130 | { |
e3bbdc67 NC |
1131 | char *start; |
1132 | const char *end; | |
1133 | size_t count = PERL_ARENA_SIZE/size; | |
1134 | New(0, start, count*size, char); | |
1135 | *((void **) start) = *arena_root; | |
1136 | *arena_root = (void *)start; | |
cac9b346 | 1137 | |
e3bbdc67 | 1138 | end = start + (count-1) * size; |
cac9b346 | 1139 | |
e3bbdc67 NC |
1140 | /* The initial slot is used to link the arenas together, so it isn't to be |
1141 | linked into the list of ready-to-use bodies. */ | |
cac9b346 | 1142 | |
e3bbdc67 | 1143 | start += size; |
cac9b346 | 1144 | |
e3bbdc67 | 1145 | *root = (void *)start; |
cac9b346 | 1146 | |
e3bbdc67 NC |
1147 | while (start < end) { |
1148 | char *next = start + size; | |
1149 | *(void**) start = (void *)next; | |
1150 | start = next; | |
cac9b346 | 1151 | } |
e3bbdc67 | 1152 | *(void **)start = 0; |
de042e1d NC |
1153 | |
1154 | return *root; | |
cac9b346 NC |
1155 | } |
1156 | ||
e3bbdc67 NC |
1157 | #define more_thingy(TYPE,lctype) \ |
1158 | S_more_bodies(aTHX_ (void**)&PL_## lctype ## _arenaroot, \ | |
1159 | (void**)&PL_ ## lctype ## _root, \ | |
1160 | sizeof(TYPE)) | |
cac9b346 | 1161 | |
e3bbdc67 NC |
1162 | #define more_thingy_allocated(lctype) \ |
1163 | S_more_bodies(aTHX_ (void**)&PL_## lctype ## _arenaroot, \ | |
1164 | (void**)&PL_ ## lctype ## _root, \ | |
1165 | sizeof(lctype ## _allocated)) | |
cac9b346 | 1166 | |
cac9b346 | 1167 | |
e3bbdc67 NC |
1168 | #define more_xnv() more_thingy(NV, xnv) |
1169 | #define more_xpv() more_thingy_allocated(xpv) | |
1170 | #define more_xpviv() more_thingy_allocated(xpviv) | |
1171 | #define more_xpvnv() more_thingy(XPVNV, xpvnv) | |
1172 | #define more_xpvcv() more_thingy(XPVCV, xpvcv) | |
1173 | #define more_xpvav() more_thingy_allocated(xpvav) | |
1174 | #define more_xpvhv() more_thingy_allocated(xpvhv) | |
1175 | #define more_xpvgv() more_thingy(XPVGV, xpvgv) | |
1176 | #define more_xpvmg() more_thingy(XPVMG, xpvmg) | |
1177 | #define more_xpvbm() more_thingy(XPVBM, xpvbm) | |
1178 | #define more_xpvlv() more_thingy(XPVLV, xpvlv) | |
cac9b346 | 1179 | |
950dc694 | 1180 | |
cac9b346 NC |
1181 | /* grab a new NV body from the free list, allocating more if necessary */ |
1182 | ||
1183 | STATIC XPVNV* | |
1184 | S_new_xnv(pTHX) | |
1185 | { | |
1186 | NV* xnv; | |
1187 | LOCK_SV_MUTEX; | |
de042e1d | 1188 | xnv = PL_xnv_root ? PL_xnv_root : more_xnv(); |
cac9b346 NC |
1189 | PL_xnv_root = *(NV**)xnv; |
1190 | UNLOCK_SV_MUTEX; | |
1191 | return (XPVNV*)((char*)xnv - STRUCT_OFFSET(XPVNV, xnv_nv)); | |
1192 | } | |
1193 | ||
1194 | /* return an NV body to the free list */ | |
1195 | ||
1196 | STATIC void | |
1197 | S_del_xnv(pTHX_ XPVNV *p) | |
1198 | { | |
1199 | NV* xnv = (NV*)((char*)(p) + STRUCT_OFFSET(XPVNV, xnv_nv)); | |
1200 | LOCK_SV_MUTEX; | |
1201 | *(NV**)xnv = PL_xnv_root; | |
1202 | PL_xnv_root = xnv; | |
1203 | UNLOCK_SV_MUTEX; | |
ed6116ce LW |
1204 | } |
1205 | ||
645c22ef DM |
1206 | /* grab a new struct xpv from the free list, allocating more if necessary */ |
1207 | ||
76e3520e | 1208 | STATIC XPV* |
cea2e8a9 | 1209 | S_new_xpv(pTHX) |
463ee0b2 | 1210 | { |
59813432 | 1211 | xpv_allocated* xpv; |
cbe51380 | 1212 | LOCK_SV_MUTEX; |
de042e1d | 1213 | xpv = PL_xpv_root ? PL_xpv_root : more_xpv(); |
59813432 | 1214 | PL_xpv_root = *(xpv_allocated**)xpv; |
cbe51380 | 1215 | UNLOCK_SV_MUTEX; |
59813432 NC |
1216 | /* If xpv_allocated is the same structure as XPV then the two OFFSETs |
1217 | sum to zero, and the pointer is unchanged. If the allocated structure | |
1218 | is smaller (no initial IV actually allocated) then the net effect is | |
1219 | to subtract the size of the IV from the pointer, to return a new pointer | |
1220 | as if an initial IV were actually allocated. */ | |
1221 | return (XPV*)((char*)xpv - STRUCT_OFFSET(XPV, xpv_cur) | |
1222 | + STRUCT_OFFSET(xpv_allocated, xpv_cur)); | |
463ee0b2 LW |
1223 | } |
1224 | ||
645c22ef DM |
1225 | /* return a struct xpv to the free list */ |
1226 | ||
76e3520e | 1227 | STATIC void |
cea2e8a9 | 1228 | S_del_xpv(pTHX_ XPV *p) |
463ee0b2 | 1229 | { |
59813432 NC |
1230 | xpv_allocated* xpv |
1231 | = (xpv_allocated*)((char*)(p) + STRUCT_OFFSET(XPV, xpv_cur) | |
1232 | - STRUCT_OFFSET(xpv_allocated, xpv_cur)); | |
cbe51380 | 1233 | LOCK_SV_MUTEX; |
59813432 NC |
1234 | *(xpv_allocated**)xpv = PL_xpv_root; |
1235 | PL_xpv_root = xpv; | |
cbe51380 | 1236 | UNLOCK_SV_MUTEX; |
463ee0b2 LW |
1237 | } |
1238 | ||
645c22ef DM |
1239 | /* grab a new struct xpviv from the free list, allocating more if necessary */ |
1240 | ||
932e9ff9 VB |
1241 | STATIC XPVIV* |
1242 | S_new_xpviv(pTHX) | |
1243 | { | |
311a25d9 | 1244 | xpviv_allocated* xpviv; |
932e9ff9 | 1245 | LOCK_SV_MUTEX; |
de042e1d | 1246 | xpviv = PL_xpviv_root ? PL_xpviv_root : more_xpviv(); |
311a25d9 | 1247 | PL_xpviv_root = *(xpviv_allocated**)xpviv; |
932e9ff9 | 1248 | UNLOCK_SV_MUTEX; |
311a25d9 NC |
1249 | /* If xpviv_allocated is the same structure as XPVIV then the two OFFSETs |
1250 | sum to zero, and the pointer is unchanged. If the allocated structure | |
1251 | is smaller (no initial IV actually allocated) then the net effect is | |
1252 | to subtract the size of the IV from the pointer, to return a new pointer | |
1253 | as if an initial IV were actually allocated. */ | |
1254 | return (XPVIV*)((char*)xpviv - STRUCT_OFFSET(XPVIV, xpv_cur) | |
1255 | + STRUCT_OFFSET(xpviv_allocated, xpv_cur)); | |
932e9ff9 VB |
1256 | } |
1257 | ||
645c22ef DM |
1258 | /* return a struct xpviv to the free list */ |
1259 | ||
932e9ff9 VB |
1260 | STATIC void |
1261 | S_del_xpviv(pTHX_ XPVIV *p) | |
1262 | { | |
311a25d9 NC |
1263 | xpviv_allocated* xpviv |
1264 | = (xpviv_allocated*)((char*)(p) + STRUCT_OFFSET(XPVIV, xpv_cur) | |
1265 | - STRUCT_OFFSET(xpviv_allocated, xpv_cur)); | |
932e9ff9 | 1266 | LOCK_SV_MUTEX; |
311a25d9 NC |
1267 | *(xpviv_allocated**)xpviv = PL_xpviv_root; |
1268 | PL_xpviv_root = xpviv; | |
932e9ff9 VB |
1269 | UNLOCK_SV_MUTEX; |
1270 | } | |
1271 | ||
645c22ef DM |
1272 | /* grab a new struct xpvnv from the free list, allocating more if necessary */ |
1273 | ||
932e9ff9 VB |
1274 | STATIC XPVNV* |
1275 | S_new_xpvnv(pTHX) | |
1276 | { | |
1277 | XPVNV* xpvnv; | |
1278 | LOCK_SV_MUTEX; | |
de042e1d | 1279 | xpvnv = PL_xpvnv_root ? PL_xpvnv_root : more_xpvnv(); |
7b2c381c | 1280 | PL_xpvnv_root = *(XPVNV**)xpvnv; |
932e9ff9 VB |
1281 | UNLOCK_SV_MUTEX; |
1282 | return xpvnv; | |
1283 | } | |
1284 | ||
645c22ef DM |
1285 | /* return a struct xpvnv to the free list */ |
1286 | ||
932e9ff9 VB |
1287 | STATIC void |
1288 | S_del_xpvnv(pTHX_ XPVNV *p) | |
1289 | { | |
1290 | LOCK_SV_MUTEX; | |
7b2c381c | 1291 | *(XPVNV**)p = PL_xpvnv_root; |
932e9ff9 VB |
1292 | PL_xpvnv_root = p; |
1293 | UNLOCK_SV_MUTEX; | |
1294 | } | |
1295 | ||
645c22ef DM |
1296 | /* grab a new struct xpvcv from the free list, allocating more if necessary */ |
1297 | ||
932e9ff9 VB |
1298 | STATIC XPVCV* |
1299 | S_new_xpvcv(pTHX) | |
1300 | { | |
1301 | XPVCV* xpvcv; | |
1302 | LOCK_SV_MUTEX; | |
de042e1d | 1303 | xpvcv = PL_xpvcv_root ? PL_xpvcv_root : more_xpvcv(); |
7b2c381c | 1304 | PL_xpvcv_root = *(XPVCV**)xpvcv; |
932e9ff9 VB |
1305 | UNLOCK_SV_MUTEX; |
1306 | return xpvcv; | |
1307 | } | |
1308 | ||
645c22ef DM |
1309 | /* return a struct xpvcv to the free list */ |
1310 | ||
932e9ff9 VB |
1311 | STATIC void |
1312 | S_del_xpvcv(pTHX_ XPVCV *p) | |
1313 | { | |
1314 | LOCK_SV_MUTEX; | |
7b2c381c | 1315 | *(XPVCV**)p = PL_xpvcv_root; |
932e9ff9 VB |
1316 | PL_xpvcv_root = p; |
1317 | UNLOCK_SV_MUTEX; | |
1318 | } | |
1319 | ||
645c22ef DM |
1320 | /* grab a new struct xpvav from the free list, allocating more if necessary */ |
1321 | ||
932e9ff9 VB |
1322 | STATIC XPVAV* |
1323 | S_new_xpvav(pTHX) | |
1324 | { | |
59813432 | 1325 | xpvav_allocated* xpvav; |
932e9ff9 | 1326 | LOCK_SV_MUTEX; |
de042e1d | 1327 | xpvav = PL_xpvav_root ? PL_xpvav_root : more_xpvav(); |
59813432 | 1328 | PL_xpvav_root = *(xpvav_allocated**)xpvav; |
932e9ff9 | 1329 | UNLOCK_SV_MUTEX; |
59813432 NC |
1330 | return (XPVAV*)((char*)xpvav - STRUCT_OFFSET(XPVAV, xav_fill) |
1331 | + STRUCT_OFFSET(xpvav_allocated, xav_fill)); | |
932e9ff9 VB |
1332 | } |
1333 | ||
645c22ef DM |
1334 | /* return a struct xpvav to the free list */ |
1335 | ||
932e9ff9 VB |
1336 | STATIC void |
1337 | S_del_xpvav(pTHX_ XPVAV *p) | |
1338 | { | |
59813432 NC |
1339 | xpvav_allocated* xpvav |
1340 | = (xpvav_allocated*)((char*)(p) + STRUCT_OFFSET(XPVAV, xav_fill) | |
1341 | - STRUCT_OFFSET(xpvav_allocated, xav_fill)); | |
932e9ff9 | 1342 | LOCK_SV_MUTEX; |
59813432 NC |
1343 | *(xpvav_allocated**)xpvav = PL_xpvav_root; |
1344 | PL_xpvav_root = xpvav; | |
932e9ff9 VB |
1345 | UNLOCK_SV_MUTEX; |
1346 | } | |
1347 | ||
645c22ef DM |
1348 | /* grab a new struct xpvhv from the free list, allocating more if necessary */ |
1349 | ||
932e9ff9 VB |
1350 | STATIC XPVHV* |
1351 | S_new_xpvhv(pTHX) | |
1352 | { | |
59813432 | 1353 | xpvhv_allocated* xpvhv; |
932e9ff9 | 1354 | LOCK_SV_MUTEX; |
de042e1d | 1355 | xpvhv = PL_xpvhv_root ? PL_xpvhv_root : more_xpvhv(); |
59813432 | 1356 | PL_xpvhv_root = *(xpvhv_allocated**)xpvhv; |
932e9ff9 | 1357 | UNLOCK_SV_MUTEX; |
59813432 NC |
1358 | return (XPVHV*)((char*)xpvhv - STRUCT_OFFSET(XPVHV, xhv_fill) |
1359 | + STRUCT_OFFSET(xpvhv_allocated, xhv_fill)); | |
932e9ff9 VB |
1360 | } |
1361 | ||
645c22ef DM |
1362 | /* return a struct xpvhv to the free list */ |
1363 | ||
932e9ff9 VB |
1364 | STATIC void |
1365 | S_del_xpvhv(pTHX_ XPVHV *p) | |
1366 | { | |
59813432 NC |
1367 | xpvhv_allocated* xpvhv |
1368 | = (xpvhv_allocated*)((char*)(p) + STRUCT_OFFSET(XPVHV, xhv_fill) | |
1369 | - STRUCT_OFFSET(xpvhv_allocated, xhv_fill)); | |
932e9ff9 | 1370 | LOCK_SV_MUTEX; |
59813432 NC |
1371 | *(xpvhv_allocated**)xpvhv = PL_xpvhv_root; |
1372 | PL_xpvhv_root = xpvhv; | |
932e9ff9 VB |
1373 | UNLOCK_SV_MUTEX; |
1374 | } | |
1375 | ||
645c22ef DM |
1376 | /* grab a new struct xpvmg from the free list, allocating more if necessary */ |
1377 | ||
932e9ff9 VB |
1378 | STATIC XPVMG* |
1379 | S_new_xpvmg(pTHX) | |
1380 | { | |
1381 | XPVMG* xpvmg; | |
1382 | LOCK_SV_MUTEX; | |
de042e1d | 1383 | xpvmg = PL_xpvmg_root ? PL_xpvmg_root : more_xpvmg(); |
7b2c381c | 1384 | PL_xpvmg_root = *(XPVMG**)xpvmg; |
932e9ff9 VB |
1385 | UNLOCK_SV_MUTEX; |
1386 | return xpvmg; | |
1387 | } | |
1388 | ||
645c22ef DM |
1389 | /* return a struct xpvmg to the free list */ |
1390 | ||
932e9ff9 VB |
1391 | STATIC void |
1392 | S_del_xpvmg(pTHX_ XPVMG *p) | |
1393 | { | |
1394 | LOCK_SV_MUTEX; | |
7b2c381c | 1395 | *(XPVMG**)p = PL_xpvmg_root; |
932e9ff9 VB |
1396 | PL_xpvmg_root = p; |
1397 | UNLOCK_SV_MUTEX; | |
1398 | } | |
1399 | ||
727879eb NC |
1400 | /* grab a new struct xpvgv from the free list, allocating more if necessary */ |
1401 | ||
1402 | STATIC XPVGV* | |
1403 | S_new_xpvgv(pTHX) | |
1404 | { | |
1405 | XPVGV* xpvgv; | |
1406 | LOCK_SV_MUTEX; | |
de042e1d | 1407 | xpvgv = PL_xpvgv_root ? PL_xpvgv_root : more_xpvgv(); |
7b2c381c | 1408 | PL_xpvgv_root = *(XPVGV**)xpvgv; |
727879eb NC |
1409 | UNLOCK_SV_MUTEX; |
1410 | return xpvgv; | |
1411 | } | |
1412 | ||
1413 | /* return a struct xpvgv to the free list */ | |
1414 | ||
1415 | STATIC void | |
1416 | S_del_xpvgv(pTHX_ XPVGV *p) | |
1417 | { | |
1418 | LOCK_SV_MUTEX; | |
7b2c381c | 1419 | *(XPVGV**)p = PL_xpvgv_root; |
727879eb NC |
1420 | PL_xpvgv_root = p; |
1421 | UNLOCK_SV_MUTEX; | |
1422 | } | |
1423 | ||
645c22ef DM |
1424 | /* grab a new struct xpvlv from the free list, allocating more if necessary */ |
1425 | ||
932e9ff9 VB |
1426 | STATIC XPVLV* |
1427 | S_new_xpvlv(pTHX) | |
1428 | { | |
1429 | XPVLV* xpvlv; | |
1430 | LOCK_SV_MUTEX; | |
de042e1d | 1431 | xpvlv = PL_xpvlv_root ? PL_xpvlv_root : more_xpvlv(); |
7b2c381c | 1432 | PL_xpvlv_root = *(XPVLV**)xpvlv; |
932e9ff9 VB |
1433 | UNLOCK_SV_MUTEX; |
1434 | return xpvlv; | |
1435 | } | |
1436 | ||
645c22ef DM |
1437 | /* return a struct xpvlv to the free list */ |
1438 | ||
932e9ff9 VB |
1439 | STATIC void |
1440 | S_del_xpvlv(pTHX_ XPVLV *p) | |
1441 | { | |
1442 | LOCK_SV_MUTEX; | |
7b2c381c | 1443 | *(XPVLV**)p = PL_xpvlv_root; |
932e9ff9 VB |
1444 | PL_xpvlv_root = p; |
1445 | UNLOCK_SV_MUTEX; | |
1446 | } | |
1447 | ||
645c22ef DM |
1448 | /* grab a new struct xpvbm from the free list, allocating more if necessary */ |
1449 | ||
932e9ff9 VB |
1450 | STATIC XPVBM* |
1451 | S_new_xpvbm(pTHX) | |
1452 | { | |
1453 | XPVBM* xpvbm; | |
1454 | LOCK_SV_MUTEX; | |
de042e1d | 1455 | xpvbm = PL_xpvbm_root ? PL_xpvbm_root : more_xpvbm(); |
7b2c381c | 1456 | PL_xpvbm_root = *(XPVBM**)xpvbm; |
932e9ff9 VB |
1457 | UNLOCK_SV_MUTEX; |
1458 | return xpvbm; | |
1459 | } | |
1460 | ||
645c22ef DM |
1461 | /* return a struct xpvbm to the free list */ |
1462 | ||
932e9ff9 VB |
1463 | STATIC void |
1464 | S_del_xpvbm(pTHX_ XPVBM *p) | |
1465 | { | |
1466 | LOCK_SV_MUTEX; | |
7b2c381c | 1467 | *(XPVBM**)p = PL_xpvbm_root; |
932e9ff9 VB |
1468 | PL_xpvbm_root = p; |
1469 | UNLOCK_SV_MUTEX; | |
1470 | } | |
1471 | ||
7bab3ede MB |
1472 | #define my_safemalloc(s) (void*)safemalloc(s) |
1473 | #define my_safefree(p) safefree((char*)p) | |
463ee0b2 | 1474 | |
d33b2eba | 1475 | #ifdef PURIFY |
463ee0b2 | 1476 | |
d33b2eba GS |
1477 | #define new_XNV() my_safemalloc(sizeof(XPVNV)) |
1478 | #define del_XNV(p) my_safefree(p) | |
463ee0b2 | 1479 | |
d33b2eba GS |
1480 | #define new_XPV() my_safemalloc(sizeof(XPV)) |
1481 | #define del_XPV(p) my_safefree(p) | |
9b94d1dd | 1482 | |
d33b2eba GS |
1483 | #define new_XPVIV() my_safemalloc(sizeof(XPVIV)) |
1484 | #define del_XPVIV(p) my_safefree(p) | |
932e9ff9 | 1485 | |
d33b2eba GS |
1486 | #define new_XPVNV() my_safemalloc(sizeof(XPVNV)) |
1487 | #define del_XPVNV(p) my_safefree(p) | |
932e9ff9 | 1488 | |
d33b2eba GS |
1489 | #define new_XPVCV() my_safemalloc(sizeof(XPVCV)) |
1490 | #define del_XPVCV(p) my_safefree(p) | |
932e9ff9 | 1491 | |
d33b2eba GS |
1492 | #define new_XPVAV() my_safemalloc(sizeof(XPVAV)) |
1493 | #define del_XPVAV(p) my_safefree(p) | |
1494 | ||
1495 | #define new_XPVHV() my_safemalloc(sizeof(XPVHV)) | |
1496 | #define del_XPVHV(p) my_safefree(p) | |
1c846c1f | 1497 | |
d33b2eba GS |
1498 | #define new_XPVMG() my_safemalloc(sizeof(XPVMG)) |
1499 | #define del_XPVMG(p) my_safefree(p) | |
1500 | ||
727879eb NC |
1501 | #define new_XPVGV() my_safemalloc(sizeof(XPVGV)) |
1502 | #define del_XPVGV(p) my_safefree(p) | |
1503 | ||
d33b2eba GS |
1504 | #define new_XPVLV() my_safemalloc(sizeof(XPVLV)) |
1505 | #define del_XPVLV(p) my_safefree(p) | |
1506 | ||
1507 | #define new_XPVBM() my_safemalloc(sizeof(XPVBM)) | |
1508 | #define del_XPVBM(p) my_safefree(p) | |
1509 | ||
1510 | #else /* !PURIFY */ | |
1511 | ||
d33b2eba GS |
1512 | #define new_XNV() (void*)new_xnv() |
1513 | #define del_XNV(p) del_xnv((XPVNV*) p) | |
9b94d1dd | 1514 | |
d33b2eba GS |
1515 | #define new_XPV() (void*)new_xpv() |
1516 | #define del_XPV(p) del_xpv((XPV *)p) | |
1517 | ||
1518 | #define new_XPVIV() (void*)new_xpviv() | |
1519 | #define del_XPVIV(p) del_xpviv((XPVIV *)p) | |
1520 | ||
1521 | #define new_XPVNV() (void*)new_xpvnv() | |
1522 | #define del_XPVNV(p) del_xpvnv((XPVNV *)p) | |
1523 | ||
1524 | #define new_XPVCV() (void*)new_xpvcv() | |
1525 | #define del_XPVCV(p) del_xpvcv((XPVCV *)p) | |
1526 | ||
1527 | #define new_XPVAV() (void*)new_xpvav() | |
1528 | #define del_XPVAV(p) del_xpvav((XPVAV *)p) | |
1529 | ||
1530 | #define new_XPVHV() (void*)new_xpvhv() | |
1531 | #define del_XPVHV(p) del_xpvhv((XPVHV *)p) | |
1c846c1f | 1532 | |
d33b2eba GS |
1533 | #define new_XPVMG() (void*)new_xpvmg() |
1534 | #define del_XPVMG(p) del_xpvmg((XPVMG *)p) | |
1535 | ||
727879eb NC |
1536 | #define new_XPVGV() (void*)new_xpvgv() |
1537 | #define del_XPVGV(p) del_xpvgv((XPVGV *)p) | |
1538 | ||
d33b2eba GS |
1539 | #define new_XPVLV() (void*)new_xpvlv() |
1540 | #define del_XPVLV(p) del_xpvlv((XPVLV *)p) | |
1541 | ||
1542 | #define new_XPVBM() (void*)new_xpvbm() | |
1543 | #define del_XPVBM(p) del_xpvbm((XPVBM *)p) | |
1544 | ||
1545 | #endif /* PURIFY */ | |
9b94d1dd | 1546 | |
d33b2eba GS |
1547 | #define new_XPVFM() my_safemalloc(sizeof(XPVFM)) |
1548 | #define del_XPVFM(p) my_safefree(p) | |
1c846c1f | 1549 | |
d33b2eba GS |
1550 | #define new_XPVIO() my_safemalloc(sizeof(XPVIO)) |
1551 | #define del_XPVIO(p) my_safefree(p) | |
8990e307 | 1552 | |
954c1994 GS |
1553 | /* |
1554 | =for apidoc sv_upgrade | |
1555 | ||
ff276b08 | 1556 | Upgrade an SV to a more complex form. Generally adds a new body type to the |
645c22ef | 1557 | SV, then copies across as much information as possible from the old body. |
ff276b08 | 1558 | You generally want to use the C<SvUPGRADE> macro wrapper. See also C<svtype>. |
954c1994 GS |
1559 | |
1560 | =cut | |
1561 | */ | |
1562 | ||
63f97190 | 1563 | void |
864dbfa3 | 1564 | Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt) |
79072805 | 1565 | { |
e763e3dc | 1566 | |
d2e56290 NC |
1567 | char* pv; |
1568 | U32 cur; | |
1569 | U32 len; | |
1570 | IV iv; | |
1571 | NV nv; | |
1572 | MAGIC* magic; | |
1573 | HV* stash; | |
79072805 | 1574 | |
765f542d NC |
1575 | if (mt != SVt_PV && SvIsCOW(sv)) { |
1576 | sv_force_normal_flags(sv, 0); | |
f130fd45 NIS |
1577 | } |
1578 | ||
79072805 | 1579 | if (SvTYPE(sv) == mt) |
63f97190 | 1580 | return; |
79072805 | 1581 | |
d2e56290 NC |
1582 | pv = NULL; |
1583 | cur = 0; | |
1584 | len = 0; | |
1585 | iv = 0; | |
1586 | nv = 0.0; | |
1587 | magic = NULL; | |
1588 | stash = Nullhv; | |
1589 | ||
79072805 LW |
1590 | switch (SvTYPE(sv)) { |
1591 | case SVt_NULL: | |
79072805 | 1592 | break; |
79072805 | 1593 | case SVt_IV: |
463ee0b2 | 1594 | iv = SvIVX(sv); |
ed6116ce | 1595 | if (mt == SVt_NV) |
463ee0b2 | 1596 | mt = SVt_PVNV; |
ed6116ce LW |
1597 | else if (mt < SVt_PVIV) |
1598 | mt = SVt_PVIV; | |
79072805 LW |
1599 | break; |
1600 | case SVt_NV: | |
463ee0b2 | 1601 | nv = SvNVX(sv); |
79072805 | 1602 | del_XNV(SvANY(sv)); |
ed6116ce | 1603 | if (mt < SVt_PVNV) |
79072805 LW |
1604 | mt = SVt_PVNV; |
1605 | break; | |
ed6116ce LW |
1606 | case SVt_RV: |
1607 | pv = (char*)SvRV(sv); | |
ed6116ce | 1608 | break; |
79072805 | 1609 | case SVt_PV: |
4d84ee25 | 1610 | pv = SvPVX_mutable(sv); |
79072805 LW |
1611 | cur = SvCUR(sv); |
1612 | len = SvLEN(sv); | |
79072805 | 1613 | del_XPV(SvANY(sv)); |
748a9306 LW |
1614 | if (mt <= SVt_IV) |
1615 | mt = SVt_PVIV; | |
1616 | else if (mt == SVt_NV) | |
1617 | mt = SVt_PVNV; | |
79072805 LW |
1618 | break; |
1619 | case SVt_PVIV: | |
4d84ee25 | 1620 | pv = SvPVX_mutable(sv); |
79072805 LW |
1621 | cur = SvCUR(sv); |
1622 | len = SvLEN(sv); | |
463ee0b2 | 1623 | iv = SvIVX(sv); |
79072805 LW |
1624 | del_XPVIV(SvANY(sv)); |
1625 | break; | |
1626 | case SVt_PVNV: | |
4d84ee25 | 1627 | pv = SvPVX_mutable(sv); |
79072805 LW |
1628 | cur = SvCUR(sv); |
1629 | len = SvLEN(sv); | |
463ee0b2 LW |
1630 | iv = SvIVX(sv); |
1631 | nv = SvNVX(sv); | |
79072805 LW |
1632 | del_XPVNV(SvANY(sv)); |
1633 | break; | |
1634 | case SVt_PVMG: | |
0ec50a73 NC |
1635 | /* Because the XPVMG of PL_mess_sv isn't allocated from the arena, |
1636 | there's no way that it can be safely upgraded, because perl.c | |
1637 | expects to Safefree(SvANY(PL_mess_sv)) */ | |
1638 | assert(sv != PL_mess_sv); | |
bce8f412 NC |
1639 | /* This flag bit is used to mean other things in other scalar types. |
1640 | Given that it only has meaning inside the pad, it shouldn't be set | |
1641 | on anything that can get upgraded. */ | |
1642 | assert((SvFLAGS(sv) & SVpad_TYPED) == 0); | |
4d84ee25 | 1643 | pv = SvPVX_mutable(sv); |
79072805 LW |
1644 | cur = SvCUR(sv); |
1645 | len = SvLEN(sv); | |
463ee0b2 LW |
1646 | iv = SvIVX(sv); |
1647 | nv = SvNVX(sv); | |
79072805 LW |
1648 | magic = SvMAGIC(sv); |
1649 | stash = SvSTASH(sv); | |
1650 | del_XPVMG(SvANY(sv)); | |
1651 | break; | |
1652 | default: | |
cea2e8a9 | 1653 | Perl_croak(aTHX_ "Can't upgrade that kind of scalar"); |
79072805 LW |
1654 | } |
1655 | ||
ffb05e06 NC |
1656 | SvFLAGS(sv) &= ~SVTYPEMASK; |
1657 | SvFLAGS(sv) |= mt; | |
1658 | ||
79072805 LW |
1659 | switch (mt) { |
1660 | case SVt_NULL: | |
cea2e8a9 | 1661 | Perl_croak(aTHX_ "Can't upgrade to undef"); |
79072805 | 1662 | case SVt_IV: |
339049b0 | 1663 | SvANY(sv) = (XPVIV*)((char*)&(sv->sv_u.svu_iv) - STRUCT_OFFSET(XPVIV, xiv_iv)); |
45977657 | 1664 | SvIV_set(sv, iv); |
79072805 LW |
1665 | break; |
1666 | case SVt_NV: | |
1667 | SvANY(sv) = new_XNV(); | |
9d6ce603 | 1668 | SvNV_set(sv, nv); |
79072805 | 1669 | break; |
ed6116ce | 1670 | case SVt_RV: |
339049b0 | 1671 | SvANY(sv) = &sv->sv_u.svu_rv; |
b162af07 | 1672 | SvRV_set(sv, (SV*)pv); |
ed6116ce | 1673 | break; |
79072805 LW |
1674 | case SVt_PVHV: |
1675 | SvANY(sv) = new_XPVHV(); | |
463ee0b2 LW |
1676 | HvFILL(sv) = 0; |
1677 | HvMAX(sv) = 0; | |
8aacddc1 | 1678 | HvTOTALKEYS(sv) = 0; |
bd4b1eb5 NC |
1679 | |
1680 | /* Fall through... */ | |
1681 | if (0) { | |
1682 | case SVt_PVAV: | |
1683 | SvANY(sv) = new_XPVAV(); | |
1684 | AvMAX(sv) = -1; | |
1685 | AvFILLp(sv) = -1; | |
1686 | AvALLOC(sv) = 0; | |
11ca45c0 | 1687 | AvREAL_only(sv); |
bd4b1eb5 NC |
1688 | } |
1689 | /* to here. */ | |
c2bfdfaf NC |
1690 | /* XXX? Only SVt_NULL is ever upgraded to AV or HV? */ |
1691 | assert(!pv); | |
8bd4d4c5 NC |
1692 | /* FIXME. Should be able to remove all this if()... if the above |
1693 | assertion is genuinely always true. */ | |
1694 | if(SvOOK(sv)) { | |
1695 | pv -= iv; | |
1696 | SvFLAGS(sv) &= ~SVf_OOK; | |
1697 | } | |
1698 | Safefree(pv); | |
bd4b1eb5 | 1699 | SvPV_set(sv, (char*)0); |
b162af07 SP |
1700 | SvMAGIC_set(sv, magic); |
1701 | SvSTASH_set(sv, stash); | |
79072805 | 1702 | break; |
bd4b1eb5 NC |
1703 | |
1704 | case SVt_PVIO: | |
1705 | SvANY(sv) = new_XPVIO(); | |
1706 | Zero(SvANY(sv), 1, XPVIO); | |
1707 | IoPAGE_LEN(sv) = 60; | |
1708 | goto set_magic_common; | |
1709 | case SVt_PVFM: | |
1710 | SvANY(sv) = new_XPVFM(); | |
1711 | Zero(SvANY(sv), 1, XPVFM); | |
1712 | goto set_magic_common; | |
1713 | case SVt_PVBM: | |
1714 | SvANY(sv) = new_XPVBM(); | |
1715 | BmRARE(sv) = 0; | |
1716 | BmUSEFUL(sv) = 0; | |
1717 | BmPREVIOUS(sv) = 0; | |
1718 | goto set_magic_common; | |
1719 | case SVt_PVGV: | |
1720 | SvANY(sv) = new_XPVGV(); | |
1721 | GvGP(sv) = 0; | |
1722 | GvNAME(sv) = 0; | |
1723 | GvNAMELEN(sv) = 0; | |
1724 | GvSTASH(sv) = 0; | |
1725 | GvFLAGS(sv) = 0; | |
1726 | goto set_magic_common; | |
79072805 LW |
1727 | case SVt_PVCV: |
1728 | SvANY(sv) = new_XPVCV(); | |
748a9306 | 1729 | Zero(SvANY(sv), 1, XPVCV); |
bd4b1eb5 NC |
1730 | goto set_magic_common; |
1731 | case SVt_PVLV: | |
1732 | SvANY(sv) = new_XPVLV(); | |
1733 | LvTARGOFF(sv) = 0; | |
1734 | LvTARGLEN(sv) = 0; | |
1735 | LvTARG(sv) = 0; | |
1736 | LvTYPE(sv) = 0; | |
93a17b20 | 1737 | GvGP(sv) = 0; |
79072805 LW |
1738 | GvNAME(sv) = 0; |
1739 | GvNAMELEN(sv) = 0; | |
1740 | GvSTASH(sv) = 0; | |
a5f75d66 | 1741 | GvFLAGS(sv) = 0; |
bd4b1eb5 NC |
1742 | /* Fall through. */ |
1743 | if (0) { | |
1744 | case SVt_PVMG: | |
1745 | SvANY(sv) = new_XPVMG(); | |
1746 | } | |
1747 | set_magic_common: | |
b162af07 SP |
1748 | SvMAGIC_set(sv, magic); |
1749 | SvSTASH_set(sv, stash); | |
bd4b1eb5 NC |
1750 | /* Fall through. */ |
1751 | if (0) { | |
1752 | case SVt_PVNV: | |
1753 | SvANY(sv) = new_XPVNV(); | |
1754 | } | |
9d6ce603 | 1755 | SvNV_set(sv, nv); |
bd4b1eb5 NC |
1756 | /* Fall through. */ |
1757 | if (0) { | |
1758 | case SVt_PVIV: | |
1759 | SvANY(sv) = new_XPVIV(); | |
1760 | if (SvNIOK(sv)) | |
1761 | (void)SvIOK_on(sv); | |
1762 | SvNOK_off(sv); | |
1763 | } | |
1764 | SvIV_set(sv, iv); | |
1765 | /* Fall through. */ | |
1766 | if (0) { | |
1767 | case SVt_PV: | |
1768 | SvANY(sv) = new_XPV(); | |
1769 | } | |
f880fe2f | 1770 | SvPV_set(sv, pv); |
b162af07 SP |
1771 | SvCUR_set(sv, cur); |
1772 | SvLEN_set(sv, len); | |
8990e307 LW |
1773 | break; |
1774 | } | |
79072805 LW |
1775 | } |
1776 | ||
645c22ef DM |
1777 | /* |
1778 | =for apidoc sv_backoff | |
1779 | ||
1780 | Remove any string offset. You should normally use the C<SvOOK_off> macro | |
1781 | wrapper instead. | |
1782 | ||
1783 | =cut | |
1784 | */ | |
1785 | ||
79072805 | 1786 | int |
864dbfa3 | 1787 | Perl_sv_backoff(pTHX_ register SV *sv) |
79072805 LW |
1788 | { |
1789 | assert(SvOOK(sv)); | |
b79f7545 NC |
1790 | assert(SvTYPE(sv) != SVt_PVHV); |
1791 | assert(SvTYPE(sv) != SVt_PVAV); | |
463ee0b2 | 1792 | if (SvIVX(sv)) { |
3f7c398e | 1793 | const char *s = SvPVX_const(sv); |
b162af07 | 1794 | SvLEN_set(sv, SvLEN(sv) + SvIVX(sv)); |
f880fe2f | 1795 | SvPV_set(sv, SvPVX(sv) - SvIVX(sv)); |
79072805 | 1796 | SvIV_set(sv, 0); |
463ee0b2 | 1797 | Move(s, SvPVX(sv), SvCUR(sv)+1, char); |
79072805 LW |
1798 | } |
1799 | SvFLAGS(sv) &= ~SVf_OOK; | |
a0d0e21e | 1800 | return 0; |
79072805 LW |
1801 | } |
1802 | ||
954c1994 GS |
1803 | /* |
1804 | =for apidoc sv_grow | |
1805 | ||
645c22ef DM |
1806 | Expands the character buffer in the SV. If necessary, uses C<sv_unref> and |
1807 | upgrades the SV to C<SVt_PV>. Returns a pointer to the character buffer. | |
1808 | Use the C<SvGROW> wrapper instead. | |
954c1994 GS |
1809 | |
1810 | =cut | |
1811 | */ | |
1812 | ||
79072805 | 1813 | char * |
864dbfa3 | 1814 | Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen) |
79072805 LW |
1815 | { |
1816 | register char *s; | |
1817 | ||
55497cff | 1818 | #ifdef HAS_64K_LIMIT |
79072805 | 1819 | if (newlen >= 0x10000) { |
1d7c1841 GS |
1820 | PerlIO_printf(Perl_debug_log, |
1821 | "Allocation too large: %"UVxf"\n", (UV)newlen); | |
79072805 LW |
1822 | my_exit(1); |
1823 | } | |
55497cff | 1824 | #endif /* HAS_64K_LIMIT */ |
a0d0e21e LW |
1825 | if (SvROK(sv)) |
1826 | sv_unref(sv); | |
79072805 LW |
1827 | if (SvTYPE(sv) < SVt_PV) { |
1828 | sv_upgrade(sv, SVt_PV); | |
93524f2b | 1829 | s = SvPVX_mutable(sv); |
79072805 LW |
1830 | } |
1831 | else if (SvOOK(sv)) { /* pv is offset? */ | |
1832 | sv_backoff(sv); | |
93524f2b | 1833 | s = SvPVX_mutable(sv); |
79072805 LW |
1834 | if (newlen > SvLEN(sv)) |
1835 | newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */ | |
c6f8c383 GA |
1836 | #ifdef HAS_64K_LIMIT |
1837 | if (newlen >= 0x10000) | |
1838 | newlen = 0xFFFF; | |
1839 | #endif | |
79072805 | 1840 | } |
bc44a8a2 | 1841 | else |
4d84ee25 | 1842 | s = SvPVX_mutable(sv); |
54f0641b | 1843 | |
79072805 | 1844 | if (newlen > SvLEN(sv)) { /* need more room? */ |
7a9b70e9 | 1845 | newlen = PERL_STRLEN_ROUNDUP(newlen); |
8d6dde3e | 1846 | if (SvLEN(sv) && s) { |
7bab3ede | 1847 | #ifdef MYMALLOC |
93524f2b | 1848 | const STRLEN l = malloced_size((void*)SvPVX_const(sv)); |
8d6dde3e IZ |
1849 | if (newlen <= l) { |
1850 | SvLEN_set(sv, l); | |
1851 | return s; | |
1852 | } else | |
c70c8a0a | 1853 | #endif |
1936d2a7 | 1854 | s = saferealloc(s, newlen); |
8d6dde3e | 1855 | } |
bfed75c6 | 1856 | else { |
1936d2a7 | 1857 | s = safemalloc(newlen); |
3f7c398e SP |
1858 | if (SvPVX_const(sv) && SvCUR(sv)) { |
1859 | Move(SvPVX_const(sv), s, (newlen < SvCUR(sv)) ? newlen : SvCUR(sv), char); | |
40565179 | 1860 | } |
4e83176d | 1861 | } |
79072805 | 1862 | SvPV_set(sv, s); |
e1ec3a88 | 1863 | SvLEN_set(sv, newlen); |
79072805 LW |
1864 | } |
1865 | return s; | |
1866 | } | |
1867 | ||
954c1994 GS |
1868 | /* |
1869 | =for apidoc sv_setiv | |
1870 | ||
645c22ef DM |
1871 | Copies an integer into the given SV, upgrading first if necessary. |
1872 | Does not handle 'set' magic. See also C<sv_setiv_mg>. | |
954c1994 GS |
1873 | |
1874 | =cut | |
1875 | */ | |
1876 | ||
79072805 | 1877 | void |
864dbfa3 | 1878 | Perl_sv_setiv(pTHX_ register SV *sv, IV i) |
79072805 | 1879 | { |
765f542d | 1880 | SV_CHECK_THINKFIRST_COW_DROP(sv); |
463ee0b2 LW |
1881 | switch (SvTYPE(sv)) { |
1882 | case SVt_NULL: | |
79072805 | 1883 | sv_upgrade(sv, SVt_IV); |
463ee0b2 LW |
1884 | break; |
1885 | case SVt_NV: | |
1886 | sv_upgrade(sv, SVt_PVNV); | |
1887 | break; | |
ed6116ce | 1888 | case SVt_RV: |
463ee0b2 | 1889 | case SVt_PV: |
79072805 | 1890 | sv_upgrade(sv, SVt_PVIV); |
463ee0b2 | 1891 | break; |
a0d0e21e LW |
1892 | |
1893 | case SVt_PVGV: | |
a0d0e21e LW |
1894 | case SVt_PVAV: |
1895 | case SVt_PVHV: | |
1896 | case SVt_PVCV: | |
1897 | case SVt_PVFM: | |
1898 | case SVt_PVIO: | |
411caa50 | 1899 | Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0), |
53e06cf0 | 1900 | OP_DESC(PL_op)); |
463ee0b2 | 1901 | } |
a0d0e21e | 1902 | (void)SvIOK_only(sv); /* validate number */ |
45977657 | 1903 | SvIV_set(sv, i); |
463ee0b2 | 1904 | SvTAINT(sv); |
79072805 LW |
1905 | } |
1906 | ||
954c1994 GS |
1907 | /* |
1908 | =for apidoc sv_setiv_mg | |
1909 | ||
1910 | Like C<sv_setiv>, but also handles 'set' magic. | |
1911 | ||
1912 | =cut | |
1913 | */ | |
1914 | ||
79072805 | 1915 | void |
864dbfa3 | 1916 | Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i) |
ef50df4b GS |
1917 | { |
1918 | sv_setiv(sv,i); | |
1919 | SvSETMAGIC(sv); | |
1920 | } | |
1921 | ||
954c1994 GS |
1922 | /* |
1923 | =for apidoc sv_setuv | |
1924 | ||
645c22ef DM |
1925 | Copies an unsigned integer into the given SV, upgrading first if necessary. |
1926 | Does not handle 'set' magic. See also C<sv_setuv_mg>. | |
954c1994 GS |
1927 | |
1928 | =cut | |
1929 | */ | |
1930 | ||
ef50df4b | 1931 | void |
864dbfa3 | 1932 | Perl_sv_setuv(pTHX_ register SV *sv, UV u) |
55497cff | 1933 | { |
55ada374 NC |
1934 | /* With these two if statements: |
1935 | u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865 | |
d460ef45 | 1936 | |
55ada374 NC |
1937 | without |
1938 | u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865 | |
d460ef45 | 1939 | |
55ada374 NC |
1940 | If you wish to remove them, please benchmark to see what the effect is |
1941 | */ | |
28e5dec8 JH |
1942 | if (u <= (UV)IV_MAX) { |
1943 | sv_setiv(sv, (IV)u); | |
1944 | return; | |
1945 | } | |
25da4f38 IZ |
1946 | sv_setiv(sv, 0); |
1947 | SvIsUV_on(sv); | |
607fa7f2 | 1948 | SvUV_set(sv, u); |
55497cff | 1949 | } |
1950 | ||
954c1994 GS |
1951 | /* |
1952 | =for apidoc sv_setuv_mg | |
1953 | ||
1954 | Like C<sv_setuv>, but also handles 'set' magic. | |
1955 | ||
1956 | =cut | |
1957 | */ | |
1958 | ||
55497cff | 1959 | void |
864dbfa3 | 1960 | Perl_sv_setuv_mg(pTHX_ register SV *sv, UV u) |
ef50df4b | 1961 | { |
55ada374 NC |
1962 | /* With these two if statements: |
1963 | u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865 | |
d460ef45 | 1964 | |
55ada374 NC |
1965 | without |
1966 | u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865 | |
d460ef45 | 1967 | |
55ada374 NC |
1968 | If you wish to remove them, please benchmark to see what the effect is |
1969 | */ | |
28e5dec8 JH |
1970 | if (u <= (UV)IV_MAX) { |
1971 | sv_setiv(sv, (IV)u); | |
1972 | } else { | |
1973 | sv_setiv(sv, 0); | |
1974 | SvIsUV_on(sv); | |
1975 | sv_setuv(sv,u); | |
1976 | } | |
ef50df4b GS |
1977 | SvSETMAGIC(sv); |
1978 | } | |
1979 | ||
954c1994 GS |
1980 | /* |
1981 | =for apidoc sv_setnv | |
1982 | ||
645c22ef DM |
1983 | Copies a double into the given SV, upgrading first if necessary. |
1984 | Does not handle 'set' magic. See also C<sv_setnv_mg>. | |
954c1994 GS |
1985 | |
1986 | =cut | |
1987 | */ | |
1988 | ||
ef50df4b | 1989 | void |
65202027 | 1990 | Perl_sv_setnv(pTHX_ register SV *sv, NV num) |
79072805 | 1991 | { |
765f542d | 1992 | SV_CHECK_THINKFIRST_COW_DROP(sv); |
a0d0e21e LW |
1993 | switch (SvTYPE(sv)) { |
1994 | case SVt_NULL: | |
1995 | case SVt_IV: | |
79072805 | 1996 | sv_upgrade(sv, SVt_NV); |
a0d0e21e | 1997 | break; |
a0d0e21e LW |
1998 | case SVt_RV: |
1999 | case SVt_PV: | |
2000 | case SVt_PVIV: | |
79072805 | 2001 | sv_upgrade(sv, SVt_PVNV); |
a0d0e21e | 2002 | break; |
827b7e14 | 2003 | |
a0d0e21e | 2004 | case SVt_PVGV: |
a0d0e21e LW |
2005 | case SVt_PVAV: |
2006 | case SVt_PVHV: | |
2007 | case SVt_PVCV: | |
2008 | case SVt_PVFM: | |
2009 | case SVt_PVIO: | |
411caa50 | 2010 | Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0), |
53e06cf0 | 2011 | OP_NAME(PL_op)); |
79072805 | 2012 | } |
9d6ce603 | 2013 | SvNV_set(sv, num); |
a0d0e21e | 2014 | (void)SvNOK_only(sv); /* validate number */ |
463ee0b2 | 2015 | SvTAINT(sv); |
79072805 LW |
2016 | } |
2017 | ||
954c1994 GS |
2018 | /* |
2019 | =for apidoc sv_setnv_mg | |
2020 | ||
2021 | Like C<sv_setnv>, but also handles 'set' magic. | |
2022 | ||
2023 | =cut | |
2024 | */ | |
2025 | ||
ef50df4b | 2026 | void |
65202027 | 2027 | Perl_sv_setnv_mg(pTHX_ register SV *sv, NV num) |
ef50df4b GS |
2028 | { |
2029 | sv_setnv(sv,num); | |
2030 | SvSETMAGIC(sv); | |
2031 | } | |
2032 | ||
645c22ef DM |
2033 | /* Print an "isn't numeric" warning, using a cleaned-up, |
2034 | * printable version of the offending string | |
2035 | */ | |
2036 | ||
76e3520e | 2037 | STATIC void |
cea2e8a9 | 2038 | S_not_a_number(pTHX_ SV *sv) |
a0d0e21e | 2039 | { |
94463019 JH |
2040 | SV *dsv; |
2041 | char tmpbuf[64]; | |
2042 | char *pv; | |
2043 | ||
2044 | if (DO_UTF8(sv)) { | |
2045 | dsv = sv_2mortal(newSVpv("", 0)); | |
2046 | pv = sv_uni_display(dsv, sv, 10, 0); | |
2047 | } else { | |
2048 | char *d = tmpbuf; | |
2049 | char *limit = tmpbuf + sizeof(tmpbuf) - 8; | |
2050 | /* each *s can expand to 4 chars + "...\0", | |
2051 | i.e. need room for 8 chars */ | |
ecdeb87c | 2052 | |
e62f0680 NC |
2053 | const char *s, *end; |
2054 | for (s = SvPVX_const(sv), end = s + SvCUR(sv); s < end && d < limit; | |
2055 | s++) { | |
94463019 JH |
2056 | int ch = *s & 0xFF; |
2057 | if (ch & 128 && !isPRINT_LC(ch)) { | |
2058 | *d++ = 'M'; | |
2059 | *d++ = '-'; | |
2060 | ch &= 127; | |
2061 | } | |
2062 | if (ch == '\n') { | |
2063 | *d++ = '\\'; | |
2064 | *d++ = 'n'; | |
2065 | } | |
2066 | else if (ch == '\r') { | |
2067 | *d++ = '\\'; | |
2068 | *d++ = 'r'; | |
2069 | } | |
2070 | else if (ch == '\f') { | |
2071 | *d++ = '\\'; | |
2072 | *d++ = 'f'; | |
2073 | } | |
2074 | else if (ch == '\\') { | |
2075 | *d++ = '\\'; | |
2076 | *d++ = '\\'; | |
2077 | } | |
2078 | else if (ch == '\0') { | |
2079 | *d++ = '\\'; | |
2080 | *d++ = '0'; | |
2081 | } | |
2082 | else if (isPRINT_LC(ch)) | |
2083 | *d++ = ch; | |
2084 | else { | |
2085 | *d++ = '^'; | |
2086 | *d++ = toCTRL(ch); | |
2087 | } | |
2088 | } | |
2089 | if (s < end) { | |
2090 | *d++ = '.'; | |
2091 | *d++ = '.'; | |
2092 | *d++ = '.'; | |
2093 | } | |
2094 | *d = '\0'; | |
2095 | pv = tmpbuf; | |
a0d0e21e | 2096 | } |
a0d0e21e | 2097 | |
533c011a | 2098 | if (PL_op) |
9014280d | 2099 | Perl_warner(aTHX_ packWARN(WARN_NUMERIC), |
94463019 JH |
2100 | "Argument \"%s\" isn't numeric in %s", pv, |
2101 | OP_DESC(PL_op)); | |
a0d0e21e | 2102 | else |
9014280d | 2103 | Perl_warner(aTHX_ packWARN(WARN_NUMERIC), |
94463019 | 2104 | "Argument \"%s\" isn't numeric", pv); |
a0d0e21e LW |
2105 | } |
2106 | ||
c2988b20 NC |
2107 | /* |
2108 | =for apidoc looks_like_number | |
2109 | ||
645c22ef DM |
2110 | Test if the content of an SV looks like a number (or is a number). |
2111 | C<Inf> and C<Infinity> are treated as numbers (so will not issue a | |
2112 | non-numeric warning), even if your atof() doesn't grok them. | |
c2988b20 NC |
2113 | |
2114 | =cut | |
2115 | */ | |
2116 | ||
2117 | I32 | |
2118 | Perl_looks_like_number(pTHX_ SV *sv) | |
2119 | { | |
a3b680e6 | 2120 | register const char *sbegin; |
c2988b20 NC |
2121 | STRLEN len; |
2122 | ||
2123 | if (SvPOK(sv)) { | |
3f7c398e | 2124 | sbegin = SvPVX_const(sv); |
c2988b20 NC |
2125 | len = SvCUR(sv); |
2126 | } | |
2127 | else if (SvPOKp(sv)) | |
83003860 | 2128 | sbegin = SvPV_const(sv, len); |
c2988b20 | 2129 | else |
e0ab1c0e | 2130 | return SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK); |
c2988b20 NC |
2131 | return grok_number(sbegin, len, NULL); |
2132 | } | |
25da4f38 IZ |
2133 | |
2134 | /* Actually, ISO C leaves conversion of UV to IV undefined, but | |
2135 | until proven guilty, assume that things are not that bad... */ | |
2136 | ||
645c22ef DM |
2137 | /* |
2138 | NV_PRESERVES_UV: | |
2139 | ||
2140 | As 64 bit platforms often have an NV that doesn't preserve all bits of | |
28e5dec8 JH |
2141 | an IV (an assumption perl has been based on to date) it becomes necessary |
2142 | to remove the assumption that the NV always carries enough precision to | |
2143 | recreate the IV whenever needed, and that the NV is the canonical form. | |
2144 | Instead, IV/UV and NV need to be given equal rights. So as to not lose | |
645c22ef | 2145 | precision as a side effect of conversion (which would lead to insanity |
28e5dec8 JH |
2146 | and the dragon(s) in t/op/numconvert.t getting very angry) the intent is |
2147 | 1) to distinguish between IV/UV/NV slots that have cached a valid | |
2148 | conversion where precision was lost and IV/UV/NV slots that have a | |
2149 | valid conversion which has lost no precision | |
645c22ef | 2150 | 2) to ensure that if a numeric conversion to one form is requested that |
28e5dec8 JH |
2151 | would lose precision, the precise conversion (or differently |
2152 | imprecise conversion) is also performed and cached, to prevent | |
2153 | requests for different numeric formats on the same SV causing | |
2154 | lossy conversion chains. (lossless conversion chains are perfectly | |
2155 | acceptable (still)) | |
2156 | ||
2157 | ||
2158 | flags are used: | |
2159 | SvIOKp is true if the IV slot contains a valid value | |
2160 | SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true) | |
2161 | SvNOKp is true if the NV slot contains a valid value | |
2162 | SvNOK is true only if the NV value is accurate | |
2163 | ||
2164 | so | |
645c22ef | 2165 | while converting from PV to NV, check to see if converting that NV to an |
28e5dec8 JH |
2166 | IV(or UV) would lose accuracy over a direct conversion from PV to |
2167 | IV(or UV). If it would, cache both conversions, return NV, but mark | |
2168 | SV as IOK NOKp (ie not NOK). | |
2169 | ||
645c22ef | 2170 | While converting from PV to IV, check to see if converting that IV to an |
28e5dec8 JH |
2171 | NV would lose accuracy over a direct conversion from PV to NV. If it |
2172 | would, cache both conversions, flag similarly. | |
2173 | ||
2174 | Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite | |
2175 | correctly because if IV & NV were set NV *always* overruled. | |
645c22ef DM |
2176 | Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flag's meaning |
2177 | changes - now IV and NV together means that the two are interchangeable: | |
28e5dec8 | 2178 | SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX; |
d460ef45 | 2179 | |
645c22ef DM |
2180 | The benefit of this is that operations such as pp_add know that if |
2181 | SvIOK is true for both left and right operands, then integer addition | |
2182 | can be used instead of floating point (for cases where the result won't | |
2183 | overflow). Before, floating point was always used, which could lead to | |
28e5dec8 JH |
2184 | loss of precision compared with integer addition. |
2185 | ||
2186 | * making IV and NV equal status should make maths accurate on 64 bit | |
2187 | platforms | |
2188 | * may speed up maths somewhat if pp_add and friends start to use | |
645c22ef | 2189 | integers when possible instead of fp. (Hopefully the overhead in |
28e5dec8 JH |
2190 | looking for SvIOK and checking for overflow will not outweigh the |
2191 | fp to integer speedup) | |
2192 | * will slow down integer operations (callers of SvIV) on "inaccurate" | |
2193 | values, as the change from SvIOK to SvIOKp will cause a call into | |
2194 | sv_2iv each time rather than a macro access direct to the IV slot | |
2195 | * should speed up number->string conversion on integers as IV is | |
645c22ef | 2196 | favoured when IV and NV are equally accurate |
28e5dec8 JH |
2197 | |
2198 | #################################################################### | |
645c22ef DM |
2199 | You had better be using SvIOK_notUV if you want an IV for arithmetic: |
2200 | SvIOK is true if (IV or UV), so you might be getting (IV)SvUV. | |
2201 | On the other hand, SvUOK is true iff UV. | |
28e5dec8 JH |
2202 | #################################################################### |
2203 | ||
645c22ef | 2204 | Your mileage will vary depending your CPU's relative fp to integer |
28e5dec8 JH |
2205 | performance ratio. |
2206 | */ | |
2207 | ||
2208 | #ifndef NV_PRESERVES_UV | |
645c22ef DM |
2209 | # define IS_NUMBER_UNDERFLOW_IV 1 |
2210 | # define IS_NUMBER_UNDERFLOW_UV 2 | |
2211 | # define IS_NUMBER_IV_AND_UV 2 | |
2212 | # define IS_NUMBER_OVERFLOW_IV 4 | |
2213 | # define IS_NUMBER_OVERFLOW_UV 5 | |
2214 | ||
2215 | /* sv_2iuv_non_preserve(): private routine for use by sv_2iv() and sv_2uv() */ | |
28e5dec8 JH |
2216 | |
2217 | /* For sv_2nv these three cases are "SvNOK and don't bother casting" */ | |
2218 | STATIC int | |
645c22ef | 2219 | S_sv_2iuv_non_preserve(pTHX_ register SV *sv, I32 numtype) |
28e5dec8 | 2220 | { |
3f7c398e | 2221 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_2iuv_non '%s', IV=0x%"UVxf" NV=%"NVgf" inttype=%"UVXf"\n", SvPVX_const(sv), SvIVX(sv), SvNVX(sv), (UV)numtype)); |
28e5dec8 JH |
2222 | if (SvNVX(sv) < (NV)IV_MIN) { |
2223 | (void)SvIOKp_on(sv); | |
2224 | (void)SvNOK_on(sv); | |
45977657 | 2225 | SvIV_set(sv, IV_MIN); |
28e5dec8 JH |
2226 | return IS_NUMBER_UNDERFLOW_IV; |
2227 | } | |
2228 | if (SvNVX(sv) > (NV)UV_MAX) { | |
2229 | (void)SvIOKp_on(sv); | |
2230 | (void)SvNOK_on(sv); | |
2231 | SvIsUV_on(sv); | |
607fa7f2 | 2232 | SvUV_set(sv, UV_MAX); |
28e5dec8 JH |
2233 | return IS_NUMBER_OVERFLOW_UV; |
2234 | } | |
c2988b20 NC |
2235 | (void)SvIOKp_on(sv); |
2236 | (void)SvNOK_on(sv); | |
2237 | /* Can't use strtol etc to convert this string. (See truth table in | |
2238 | sv_2iv */ | |
2239 | if (SvNVX(sv) <= (UV)IV_MAX) { | |
45977657 | 2240 | SvIV_set(sv, I_V(SvNVX(sv))); |
c2988b20 NC |
2241 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) { |
2242 | SvIOK_on(sv); /* Integer is precise. NOK, IOK */ | |
2243 | } else { | |
2244 | /* Integer is imprecise. NOK, IOKp */ | |
2245 | } | |
2246 | return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV; | |
2247 | } | |
2248 | SvIsUV_on(sv); | |
607fa7f2 | 2249 | SvUV_set(sv, U_V(SvNVX(sv))); |
c2988b20 NC |
2250 | if ((NV)(SvUVX(sv)) == SvNVX(sv)) { |
2251 | if (SvUVX(sv) == UV_MAX) { | |
2252 | /* As we know that NVs don't preserve UVs, UV_MAX cannot | |
2253 | possibly be preserved by NV. Hence, it must be overflow. | |
2254 | NOK, IOKp */ | |
2255 | return IS_NUMBER_OVERFLOW_UV; | |
2256 | } | |
2257 | SvIOK_on(sv); /* Integer is precise. NOK, UOK */ | |
2258 | } else { | |
2259 | /* Integer is imprecise. NOK, IOKp */ | |
28e5dec8 | 2260 | } |
c2988b20 | 2261 | return IS_NUMBER_OVERFLOW_IV; |
28e5dec8 | 2262 | } |
645c22ef DM |
2263 | #endif /* !NV_PRESERVES_UV*/ |
2264 | ||
891f9566 YST |
2265 | /* sv_2iv() is now a macro using Perl_sv_2iv_flags(); |
2266 | * this function provided for binary compatibility only | |
2267 | */ | |
2268 | ||
2269 | IV | |
2270 | Perl_sv_2iv(pTHX_ register SV *sv) | |
2271 | { | |
2272 | return sv_2iv_flags(sv, SV_GMAGIC); | |
2273 | } | |
2274 | ||
645c22ef | 2275 | /* |
891f9566 | 2276 | =for apidoc sv_2iv_flags |
645c22ef | 2277 | |
891f9566 YST |
2278 | Return the integer value of an SV, doing any necessary string |
2279 | conversion. If flags includes SV_GMAGIC, does an mg_get() first. | |
2280 | Normally used via the C<SvIV(sv)> and C<SvIVx(sv)> macros. | |
645c22ef DM |
2281 | |
2282 | =cut | |
2283 | */ | |
28e5dec8 | 2284 | |
a0d0e21e | 2285 | IV |
891f9566 | 2286 | Perl_sv_2iv_flags(pTHX_ register SV *sv, I32 flags) |
79072805 LW |
2287 | { |
2288 | if (!sv) | |
2289 | return 0; | |
8990e307 | 2290 | if (SvGMAGICAL(sv)) { |
891f9566 YST |
2291 | if (flags & SV_GMAGIC) |
2292 | mg_get(sv); | |
463ee0b2 LW |
2293 | if (SvIOKp(sv)) |
2294 | return SvIVX(sv); | |
748a9306 | 2295 | if (SvNOKp(sv)) { |
25da4f38 | 2296 | return I_V(SvNVX(sv)); |
748a9306 | 2297 | } |
36477c24 | 2298 | if (SvPOKp(sv) && SvLEN(sv)) |
2299 | return asIV(sv); | |
3fe9a6f1 | 2300 | if (!SvROK(sv)) { |
d008e5eb | 2301 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 2302 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
29489e7c | 2303 | report_uninit(sv); |
c6ee37c5 | 2304 | } |
36477c24 | 2305 | return 0; |
3fe9a6f1 | 2306 | } |
463ee0b2 | 2307 | } |
ed6116ce | 2308 | if (SvTHINKFIRST(sv)) { |
a0d0e21e | 2309 | if (SvROK(sv)) { |
a0d0e21e | 2310 | SV* tmpstr; |
1554e226 | 2311 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) && |
b4b9a328 | 2312 | (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) |
9e7bc3e8 | 2313 | return SvIV(tmpstr); |
56431972 | 2314 | return PTR2IV(SvRV(sv)); |
a0d0e21e | 2315 | } |
765f542d NC |
2316 | if (SvIsCOW(sv)) { |
2317 | sv_force_normal_flags(sv, 0); | |
47deb5e7 | 2318 | } |
0336b60e | 2319 | if (SvREADONLY(sv) && !SvOK(sv)) { |
0336b60e | 2320 | if (ckWARN(WARN_UNINITIALIZED)) |
29489e7c | 2321 | report_uninit(sv); |
ed6116ce LW |
2322 | return 0; |
2323 | } | |
79072805 | 2324 | } |
25da4f38 IZ |
2325 | if (SvIOKp(sv)) { |
2326 | if (SvIsUV(sv)) { | |
2327 | return (IV)(SvUVX(sv)); | |
2328 | } | |
2329 | else { | |
2330 | return SvIVX(sv); | |
2331 | } | |
463ee0b2 | 2332 | } |
748a9306 | 2333 | if (SvNOKp(sv)) { |
28e5dec8 JH |
2334 | /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv |
2335 | * without also getting a cached IV/UV from it at the same time | |
2336 | * (ie PV->NV conversion should detect loss of accuracy and cache | |
2337 | * IV or UV at same time to avoid this. NWC */ | |
25da4f38 IZ |
2338 | |
2339 | if (SvTYPE(sv) == SVt_NV) | |
2340 | sv_upgrade(sv, SVt_PVNV); | |
2341 | ||
28e5dec8 JH |
2342 | (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */ |
2343 | /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost | |
2344 | certainly cast into the IV range at IV_MAX, whereas the correct | |
2345 | answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary | |
2346 | cases go to UV */ | |
2347 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
45977657 | 2348 | SvIV_set(sv, I_V(SvNVX(sv))); |
28e5dec8 JH |
2349 | if (SvNVX(sv) == (NV) SvIVX(sv) |
2350 | #ifndef NV_PRESERVES_UV | |
2351 | && (((UV)1 << NV_PRESERVES_UV_BITS) > | |
2352 | (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv))) | |
2353 | /* Don't flag it as "accurately an integer" if the number | |
2354 | came from a (by definition imprecise) NV operation, and | |
2355 | we're outside the range of NV integer precision */ | |
2356 | #endif | |
2357 | ) { | |
2358 | SvIOK_on(sv); /* Can this go wrong with rounding? NWC */ | |
2359 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
7234c960 | 2360 | "0x%"UVxf" iv(%"NVgf" => %"IVdf") (precise)\n", |
28e5dec8 JH |
2361 | PTR2UV(sv), |
2362 | SvNVX(sv), | |
2363 | SvIVX(sv))); | |
2364 | ||
2365 | } else { | |
2366 | /* IV not precise. No need to convert from PV, as NV | |
2367 | conversion would already have cached IV if it detected | |
2368 | that PV->IV would be better than PV->NV->IV | |
2369 | flags already correct - don't set public IOK. */ | |
2370 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
7234c960 | 2371 | "0x%"UVxf" iv(%"NVgf" => %"IVdf") (imprecise)\n", |
28e5dec8 JH |
2372 | PTR2UV(sv), |
2373 | SvNVX(sv), | |
2374 | SvIVX(sv))); | |
2375 | } | |
2376 | /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN, | |
2377 | but the cast (NV)IV_MIN rounds to a the value less (more | |
2378 | negative) than IV_MIN which happens to be equal to SvNVX ?? | |
2379 | Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and | |
2380 | NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and | |
2381 | (NV)UVX == NVX are both true, but the values differ. :-( | |
2382 | Hopefully for 2s complement IV_MIN is something like | |
2383 | 0x8000000000000000 which will be exact. NWC */ | |
d460ef45 | 2384 | } |
25da4f38 | 2385 | else { |
607fa7f2 | 2386 | SvUV_set(sv, U_V(SvNVX(sv))); |
28e5dec8 JH |
2387 | if ( |
2388 | (SvNVX(sv) == (NV) SvUVX(sv)) | |
2389 | #ifndef NV_PRESERVES_UV | |
2390 | /* Make sure it's not 0xFFFFFFFFFFFFFFFF */ | |
2391 | /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */ | |
2392 | && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv)) | |
2393 | /* Don't flag it as "accurately an integer" if the number | |
2394 | came from a (by definition imprecise) NV operation, and | |
2395 | we're outside the range of NV integer precision */ | |
2396 | #endif | |
2397 | ) | |
2398 | SvIOK_on(sv); | |
25da4f38 IZ |
2399 | SvIsUV_on(sv); |
2400 | ret_iv_max: | |
1c846c1f | 2401 | DEBUG_c(PerlIO_printf(Perl_debug_log, |
57def98f | 2402 | "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n", |
56431972 | 2403 | PTR2UV(sv), |
57def98f JH |
2404 | SvUVX(sv), |
2405 | SvUVX(sv))); | |
25da4f38 IZ |
2406 | return (IV)SvUVX(sv); |
2407 | } | |
748a9306 LW |
2408 | } |
2409 | else if (SvPOKp(sv) && SvLEN(sv)) { | |
c2988b20 | 2410 | UV value; |
504618e9 | 2411 | const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value); |
25da4f38 IZ |
2412 | /* We want to avoid a possible problem when we cache an IV which |
2413 | may be later translated to an NV, and the resulting NV is not | |
c2988b20 NC |
2414 | the same as the direct translation of the initial string |
2415 | (eg 123.456 can shortcut to the IV 123 with atol(), but we must | |
2416 | be careful to ensure that the value with the .456 is around if the | |
2417 | NV value is requested in the future). | |
1c846c1f | 2418 | |
25da4f38 IZ |
2419 | This means that if we cache such an IV, we need to cache the |
2420 | NV as well. Moreover, we trade speed for space, and do not | |
28e5dec8 | 2421 | cache the NV if we are sure it's not needed. |
25da4f38 | 2422 | */ |
16b7a9a4 | 2423 | |
c2988b20 NC |
2424 | /* SVt_PVNV is one higher than SVt_PVIV, hence this order */ |
2425 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) | |
2426 | == IS_NUMBER_IN_UV) { | |
5e045b90 | 2427 | /* It's definitely an integer, only upgrade to PVIV */ |
28e5dec8 JH |
2428 | if (SvTYPE(sv) < SVt_PVIV) |
2429 | sv_upgrade(sv, SVt_PVIV); | |
f7bbb42a | 2430 | (void)SvIOK_on(sv); |
c2988b20 NC |
2431 | } else if (SvTYPE(sv) < SVt_PVNV) |
2432 | sv_upgrade(sv, SVt_PVNV); | |
28e5dec8 | 2433 | |
c2988b20 NC |
2434 | /* If NV preserves UV then we only use the UV value if we know that |
2435 | we aren't going to call atof() below. If NVs don't preserve UVs | |
2436 | then the value returned may have more precision than atof() will | |
2437 | return, even though value isn't perfectly accurate. */ | |
2438 | if ((numtype & (IS_NUMBER_IN_UV | |
2439 | #ifdef NV_PRESERVES_UV | |
2440 | | IS_NUMBER_NOT_INT | |
2441 | #endif | |
2442 | )) == IS_NUMBER_IN_UV) { | |
2443 | /* This won't turn off the public IOK flag if it was set above */ | |
2444 | (void)SvIOKp_on(sv); | |
2445 | ||
2446 | if (!(numtype & IS_NUMBER_NEG)) { | |
2447 | /* positive */; | |
2448 | if (value <= (UV)IV_MAX) { | |
45977657 | 2449 | SvIV_set(sv, (IV)value); |
c2988b20 | 2450 | } else { |
607fa7f2 | 2451 | SvUV_set(sv, value); |
c2988b20 NC |
2452 | SvIsUV_on(sv); |
2453 | } | |
2454 | } else { | |
2455 | /* 2s complement assumption */ | |
2456 | if (value <= (UV)IV_MIN) { | |
45977657 | 2457 | SvIV_set(sv, -(IV)value); |
c2988b20 NC |
2458 | } else { |
2459 | /* Too negative for an IV. This is a double upgrade, but | |
d1be9408 | 2460 | I'm assuming it will be rare. */ |
c2988b20 NC |
2461 | if (SvTYPE(sv) < SVt_PVNV) |
2462 | sv_upgrade(sv, SVt_PVNV); | |
2463 | SvNOK_on(sv); | |
2464 | SvIOK_off(sv); | |
2465 | SvIOKp_on(sv); | |
9d6ce603 | 2466 | SvNV_set(sv, -(NV)value); |
45977657 | 2467 | SvIV_set(sv, IV_MIN); |
c2988b20 NC |
2468 | } |
2469 | } | |
2470 | } | |
2471 | /* For !NV_PRESERVES_UV and IS_NUMBER_IN_UV and IS_NUMBER_NOT_INT we | |
2472 | will be in the previous block to set the IV slot, and the next | |
2473 | block to set the NV slot. So no else here. */ | |
2474 | ||
2475 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) | |
2476 | != IS_NUMBER_IN_UV) { | |
2477 | /* It wasn't an (integer that doesn't overflow the UV). */ | |
3f7c398e | 2478 | SvNV_set(sv, Atof(SvPVX_const(sv))); |
28e5dec8 | 2479 | |
c2988b20 NC |
2480 | if (! numtype && ckWARN(WARN_NUMERIC)) |
2481 | not_a_number(sv); | |
28e5dec8 | 2482 | |
65202027 | 2483 | #if defined(USE_LONG_DOUBLE) |
c2988b20 NC |
2484 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n", |
2485 | PTR2UV(sv), SvNVX(sv))); | |
65202027 | 2486 | #else |
1779d84d | 2487 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"NVgf")\n", |
c2988b20 | 2488 | PTR2UV(sv), SvNVX(sv))); |
65202027 | 2489 | #endif |
28e5dec8 JH |
2490 | |
2491 | ||
2492 | #ifdef NV_PRESERVES_UV | |
c2988b20 NC |
2493 | (void)SvIOKp_on(sv); |
2494 | (void)SvNOK_on(sv); | |
2495 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
45977657 | 2496 | SvIV_set(sv, I_V(SvNVX(sv))); |
c2988b20 NC |
2497 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) { |
2498 | SvIOK_on(sv); | |
28e5dec8 | 2499 | } else { |
c2988b20 NC |
2500 | /* Integer is imprecise. NOK, IOKp */ |
2501 | } | |
2502 | /* UV will not work better than IV */ | |
2503 | } else { | |
2504 | if (SvNVX(sv) > (NV)UV_MAX) { | |
2505 | SvIsUV_on(sv); | |
2506 | /* Integer is inaccurate. NOK, IOKp, is UV */ | |
607fa7f2 | 2507 | SvUV_set(sv, UV_MAX); |
c2988b20 NC |
2508 | SvIsUV_on(sv); |
2509 | } else { | |
607fa7f2 | 2510 | SvUV_set(sv, U_V(SvNVX(sv))); |
c2988b20 NC |
2511 | /* 0xFFFFFFFFFFFFFFFF not an issue in here */ |
2512 | if ((NV)(SvUVX(sv)) == SvNVX(sv)) { | |
2513 | SvIOK_on(sv); | |
28e5dec8 JH |
2514 | SvIsUV_on(sv); |
2515 | } else { | |
c2988b20 NC |
2516 | /* Integer is imprecise. NOK, IOKp, is UV */ |
2517 | SvIsUV_on(sv); | |
28e5dec8 | 2518 | } |
28e5dec8 | 2519 | } |
c2988b20 NC |
2520 | goto ret_iv_max; |
2521 | } | |
28e5dec8 | 2522 | #else /* NV_PRESERVES_UV */ |
c2988b20 NC |
2523 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) |
2524 | == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) { | |
2525 | /* The IV slot will have been set from value returned by | |
2526 | grok_number above. The NV slot has just been set using | |
2527 | Atof. */ | |
560b0c46 | 2528 | SvNOK_on(sv); |
c2988b20 NC |
2529 | assert (SvIOKp(sv)); |
2530 | } else { | |
2531 | if (((UV)1 << NV_PRESERVES_UV_BITS) > | |
2532 | U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) { | |
2533 | /* Small enough to preserve all bits. */ | |
2534 | (void)SvIOKp_on(sv); | |
2535 | SvNOK_on(sv); | |
45977657 | 2536 | SvIV_set(sv, I_V(SvNVX(sv))); |
c2988b20 NC |
2537 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) |
2538 | SvIOK_on(sv); | |
2539 | /* Assumption: first non-preserved integer is < IV_MAX, | |
2540 | this NV is in the preserved range, therefore: */ | |
2541 | if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv)) | |
2542 | < (UV)IV_MAX)) { | |
32fdb065 | 2543 | Perl_croak(aTHX_ "sv_2iv assumed (U_V(fabs((double)SvNVX(sv))) < (UV)IV_MAX) but SvNVX(sv)=%"NVgf" U_V is 0x%"UVxf", IV_MAX is 0x%"UVxf"\n", SvNVX(sv), U_V(SvNVX(sv)), (UV)IV_MAX); |
c2988b20 NC |
2544 | } |
2545 | } else { | |
2546 | /* IN_UV NOT_INT | |
2547 | 0 0 already failed to read UV. | |
2548 | 0 1 already failed to read UV. | |
2549 | 1 0 you won't get here in this case. IV/UV | |
2550 | slot set, public IOK, Atof() unneeded. | |
2551 | 1 1 already read UV. | |
2552 | so there's no point in sv_2iuv_non_preserve() attempting | |
2553 | to use atol, strtol, strtoul etc. */ | |
2554 | if (sv_2iuv_non_preserve (sv, numtype) | |
2555 | >= IS_NUMBER_OVERFLOW_IV) | |
2556 | goto ret_iv_max; | |
2557 | } | |
2558 | } | |
28e5dec8 | 2559 | #endif /* NV_PRESERVES_UV */ |
25da4f38 | 2560 | } |
28e5dec8 | 2561 | } else { |
599cee73 | 2562 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP)) |
29489e7c | 2563 | report_uninit(sv); |
25da4f38 IZ |
2564 | if (SvTYPE(sv) < SVt_IV) |
2565 | /* Typically the caller expects that sv_any is not NULL now. */ | |
2566 | sv_upgrade(sv, SVt_IV); | |
a0d0e21e | 2567 | return 0; |
79072805 | 2568 | } |
1d7c1841 GS |
2569 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n", |
2570 | PTR2UV(sv),SvIVX(sv))); | |
25da4f38 | 2571 | return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv); |
79072805 LW |
2572 | } |
2573 | ||
891f9566 YST |
2574 | /* sv_2uv() is now a macro using Perl_sv_2uv_flags(); |
2575 | * this function provided for binary compatibility only | |
2576 | */ | |
2577 | ||
2578 | UV | |
2579 | Perl_sv_2uv(pTHX_ register SV *sv) | |
2580 | { | |
2581 | return sv_2uv_flags(sv, SV_GMAGIC); | |
2582 | } | |
2583 | ||
645c22ef | 2584 | /* |
891f9566 | 2585 | =for apidoc sv_2uv_flags |
645c22ef DM |
2586 | |
2587 | Return the unsigned integer value of an SV, doing any necessary string | |
891f9566 YST |
2588 | conversion. If flags includes SV_GMAGIC, does an mg_get() first. |
2589 | Normally used via the C<SvUV(sv)> and C<SvUVx(sv)> macros. | |
645c22ef DM |
2590 | |
2591 | =cut | |
2592 | */ | |
2593 | ||
ff68c719 | 2594 | UV |
891f9566 | 2595 | Perl_sv_2uv_flags(pTHX_ register SV *sv, I32 flags) |
ff68c719 | 2596 | { |
2597 | if (!sv) | |
2598 | return 0; | |
2599 | if (SvGMAGICAL(sv)) { | |
891f9566 YST |
2600 | if (flags & SV_GMAGIC) |
2601 | mg_get(sv); | |
ff68c719 | 2602 | if (SvIOKp(sv)) |
2603 | return SvUVX(sv); | |
2604 | if (SvNOKp(sv)) | |
2605 | return U_V(SvNVX(sv)); | |
36477c24 | 2606 | if (SvPOKp(sv) && SvLEN(sv)) |
2607 | return asUV(sv); | |
3fe9a6f1 | 2608 | if (!SvROK(sv)) { |
d008e5eb | 2609 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 2610 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
29489e7c | 2611 | report_uninit(sv); |
c6ee37c5 | 2612 | } |
36477c24 | 2613 | return 0; |
3fe9a6f1 | 2614 | } |
ff68c719 | 2615 | } |
2616 | if (SvTHINKFIRST(sv)) { | |
2617 | if (SvROK(sv)) { | |
ff68c719 | 2618 | SV* tmpstr; |
1554e226 | 2619 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) && |
b4b9a328 | 2620 | (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) |
9e7bc3e8 | 2621 | return SvUV(tmpstr); |
56431972 | 2622 | return PTR2UV(SvRV(sv)); |
ff68c719 | 2623 | } |
765f542d NC |
2624 | if (SvIsCOW(sv)) { |
2625 | sv_force_normal_flags(sv, 0); | |
8a818333 | 2626 | } |
0336b60e | 2627 | if (SvREADONLY(sv) && !SvOK(sv)) { |
0336b60e | 2628 | if (ckWARN(WARN_UNINITIALIZED)) |
29489e7c | 2629 | report_uninit(sv); |
ff68c719 | 2630 | return 0; |
2631 | } | |
2632 | } | |
25da4f38 IZ |
2633 | if (SvIOKp(sv)) { |
2634 | if (SvIsUV(sv)) { | |
2635 | return SvUVX(sv); | |
2636 | } | |
2637 | else { | |
2638 | return (UV)SvIVX(sv); | |
2639 | } | |
ff68c719 | 2640 | } |
2641 | if (SvNOKp(sv)) { | |
28e5dec8 JH |
2642 | /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv |
2643 | * without also getting a cached IV/UV from it at the same time | |
2644 | * (ie PV->NV conversion should detect loss of accuracy and cache | |
2645 | * IV or UV at same time to avoid this. */ | |
2646 | /* IV-over-UV optimisation - choose to cache IV if possible */ | |
2647 | ||
25da4f38 IZ |
2648 | if (SvTYPE(sv) == SVt_NV) |
2649 | sv_upgrade(sv, SVt_PVNV); | |
28e5dec8 JH |
2650 | |
2651 | (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */ | |
2652 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
45977657 | 2653 | SvIV_set(sv, I_V(SvNVX(sv))); |
28e5dec8 JH |
2654 | if (SvNVX(sv) == (NV) SvIVX(sv) |
2655 | #ifndef NV_PRESERVES_UV | |
2656 | && (((UV)1 << NV_PRESERVES_UV_BITS) > | |
2657 | (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv))) | |
2658 | /* Don't flag it as "accurately an integer" if the number | |
2659 | came from a (by definition imprecise) NV operation, and | |
2660 | we're outside the range of NV integer precision */ | |
2661 | #endif | |
2662 | ) { | |
2663 | SvIOK_on(sv); /* Can this go wrong with rounding? NWC */ | |
2664 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
7234c960 | 2665 | "0x%"UVxf" uv(%"NVgf" => %"IVdf") (precise)\n", |
28e5dec8 JH |
2666 | PTR2UV(sv), |
2667 | SvNVX(sv), | |
2668 | SvIVX(sv))); | |
2669 | ||
2670 | } else { | |
2671 | /* IV not precise. No need to convert from PV, as NV | |
2672 | conversion would already have cached IV if it detected | |
2673 | that PV->IV would be better than PV->NV->IV | |
2674 | flags already correct - don't set public IOK. */ | |
2675 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
7234c960 | 2676 | "0x%"UVxf" uv(%"NVgf" => %"IVdf") (imprecise)\n", |
28e5dec8 JH |
2677 | PTR2UV(sv), |
2678 | SvNVX(sv), | |
2679 | SvIVX(sv))); | |
2680 | } | |
2681 | /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN, | |
2682 | but the cast (NV)IV_MIN rounds to a the value less (more | |
2683 | negative) than IV_MIN which happens to be equal to SvNVX ?? | |
2684 | Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and | |
2685 | NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and | |
2686 | (NV)UVX == NVX are both true, but the values differ. :-( | |
2687 | Hopefully for 2s complement IV_MIN is something like | |
2688 | 0x8000000000000000 which will be exact. NWC */ | |
d460ef45 | 2689 | } |
28e5dec8 | 2690 | else { |
607fa7f2 | 2691 | SvUV_set(sv, U_V(SvNVX(sv))); |
28e5dec8 JH |
2692 | if ( |
2693 | (SvNVX(sv) == (NV) SvUVX(sv)) | |
2694 | #ifndef NV_PRESERVES_UV | |
2695 | /* Make sure it's not 0xFFFFFFFFFFFFFFFF */ | |
2696 | /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */ | |
2697 | && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv)) | |
2698 | /* Don't flag it as "accurately an integer" if the number | |
2699 | came from a (by definition imprecise) NV operation, and | |
2700 | we're outside the range of NV integer precision */ | |
2701 | #endif | |
2702 | ) | |
2703 | SvIOK_on(sv); | |
2704 | SvIsUV_on(sv); | |
1c846c1f | 2705 | DEBUG_c(PerlIO_printf(Perl_debug_log, |
28e5dec8 | 2706 | "0x%"UVxf" 2uv(%"UVuf" => %"IVdf") (as unsigned)\n", |
57def98f | 2707 | PTR2UV(sv), |
28e5dec8 JH |
2708 | SvUVX(sv), |
2709 | SvUVX(sv))); | |
25da4f38 | 2710 | } |
ff68c719 | 2711 | } |
2712 | else if (SvPOKp(sv) && SvLEN(sv)) { | |
c2988b20 | 2713 | UV value; |
504618e9 | 2714 | const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value); |
25da4f38 IZ |
2715 | |
2716 | /* We want to avoid a possible problem when we cache a UV which | |
2717 | may be later translated to an NV, and the resulting NV is not | |
2718 | the translation of the initial data. | |
1c846c1f | 2719 | |
25da4f38 IZ |
2720 | This means that if we cache such a UV, we need to cache the |
2721 | NV as well. Moreover, we trade speed for space, and do not | |
2722 | cache the NV if not needed. | |
2723 | */ | |
16b7a9a4 | 2724 | |
c2988b20 NC |
2725 | /* SVt_PVNV is one higher than SVt_PVIV, hence this order */ |
2726 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) | |
2727 | == IS_NUMBER_IN_UV) { | |
5e045b90 | 2728 | /* It's definitely an integer, only upgrade to PVIV */ |
28e5dec8 | 2729 | if (SvTYPE(sv) < SVt_PVIV) |
f7bbb42a JH |
2730 | sv_upgrade(sv, SVt_PVIV); |
2731 | (void)SvIOK_on(sv); | |
c2988b20 NC |
2732 | } else if (SvTYPE(sv) < SVt_PVNV) |
2733 | sv_upgrade(sv, SVt_PVNV); | |
d460ef45 | 2734 | |
c2988b20 NC |
2735 | /* If NV preserves UV then we only use the UV value if we know that |
2736 | we aren't going to call atof() below. If NVs don't preserve UVs | |
2737 | then the value returned may have more precision than atof() will | |
2738 | return, even though it isn't accurate. */ | |
2739 | if ((numtype & (IS_NUMBER_IN_UV | |
2740 | #ifdef NV_PRESERVES_UV | |
2741 | | IS_NUMBER_NOT_INT | |
2742 | #endif | |
2743 | )) == IS_NUMBER_IN_UV) { | |
2744 | /* This won't turn off the public IOK flag if it was set above */ | |
2745 | (void)SvIOKp_on(sv); | |
2746 | ||
2747 | if (!(numtype & IS_NUMBER_NEG)) { | |
2748 | /* positive */; | |
2749 | if (value <= (UV)IV_MAX) { | |
45977657 | 2750 | SvIV_set(sv, (IV)value); |
28e5dec8 JH |
2751 | } else { |
2752 | /* it didn't overflow, and it was positive. */ | |
607fa7f2 | 2753 | SvUV_set(sv, value); |
28e5dec8 JH |
2754 | SvIsUV_on(sv); |
2755 | } | |
c2988b20 NC |
2756 | } else { |
2757 | /* 2s complement assumption */ | |
2758 | if (value <= (UV)IV_MIN) { | |
45977657 | 2759 | SvIV_set(sv, -(IV)value); |
c2988b20 NC |
2760 | } else { |
2761 | /* Too negative for an IV. This is a double upgrade, but | |
d1be9408 | 2762 | I'm assuming it will be rare. */ |
c2988b20 NC |
2763 | if (SvTYPE(sv) < SVt_PVNV) |
2764 | sv_upgrade(sv, SVt_PVNV); | |
2765 | SvNOK_on(sv); | |
2766 | SvIOK_off(sv); | |
2767 | SvIOKp_on(sv); | |
9d6ce603 | 2768 | SvNV_set(sv, -(NV)value); |
45977657 | 2769 | SvIV_set(sv, IV_MIN); |
c2988b20 NC |
2770 | } |
2771 | } | |
2772 | } | |
2773 | ||
2774 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) | |
2775 | != IS_NUMBER_IN_UV) { | |
2776 | /* It wasn't an integer, or it overflowed the UV. */ | |
3f7c398e | 2777 | SvNV_set(sv, Atof(SvPVX_const(sv))); |
28e5dec8 | 2778 | |
c2988b20 | 2779 | if (! numtype && ckWARN(WARN_NUMERIC)) |
28e5dec8 JH |
2780 | not_a_number(sv); |
2781 | ||
2782 | #if defined(USE_LONG_DOUBLE) | |
c2988b20 NC |
2783 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%" PERL_PRIgldbl ")\n", |
2784 | PTR2UV(sv), SvNVX(sv))); | |
28e5dec8 | 2785 | #else |
1779d84d | 2786 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"NVgf")\n", |
c2988b20 | 2787 | PTR2UV(sv), SvNVX(sv))); |
28e5dec8 JH |
2788 | #endif |
2789 | ||
2790 | #ifdef NV_PRESERVES_UV | |
c2988b20 NC |
2791 | (void)SvIOKp_on(sv); |
2792 | (void)SvNOK_on(sv); | |
2793 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
45977657 | 2794 | SvIV_set(sv, I_V(SvNVX(sv))); |
c2988b20 NC |
2795 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) { |
2796 | SvIOK_on(sv); | |
2797 | } else { | |
2798 | /* Integer is imprecise. NOK, IOKp */ | |
2799 | } | |
2800 | /* UV will not work better than IV */ | |
2801 | } else { | |
2802 | if (SvNVX(sv) > (NV)UV_MAX) { | |
2803 | SvIsUV_on(sv); | |
2804 | /* Integer is inaccurate. NOK, IOKp, is UV */ | |
607fa7f2 | 2805 | SvUV_set(sv, UV_MAX); |
c2988b20 NC |
2806 | SvIsUV_on(sv); |
2807 | } else { | |
607fa7f2 | 2808 | SvUV_set(sv, U_V(SvNVX(sv))); |
c2988b20 NC |
2809 | /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs |
2810 | NV preservse UV so can do correct comparison. */ | |
2811 | if ((NV)(SvUVX(sv)) == SvNVX(sv)) { | |
2812 | SvIOK_on(sv); | |
2813 | SvIsUV_on(sv); | |
2814 | } else { | |
2815 | /* Integer is imprecise. NOK, IOKp, is UV */ | |
2816 | SvIsUV_on(sv); | |
2817 | } | |
2818 | } | |
2819 | } | |
28e5dec8 | 2820 | #else /* NV_PRESERVES_UV */ |
c2988b20 NC |
2821 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) |
2822 | == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) { | |
2823 | /* The UV slot will have been set from value returned by | |
2824 | grok_number above. The NV slot has just been set using | |
2825 | Atof. */ | |
560b0c46 | 2826 | SvNOK_on(sv); |
c2988b20 NC |
2827 | assert (SvIOKp(sv)); |
2828 | } else { | |
2829 | if (((UV)1 << NV_PRESERVES_UV_BITS) > | |
2830 | U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) { | |
2831 | /* Small enough to preserve all bits. */ | |
2832 | (void)SvIOKp_on(sv); | |
2833 | SvNOK_on(sv); | |
45977657 | 2834 | SvIV_set(sv, I_V(SvNVX(sv))); |
c2988b20 NC |
2835 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) |
2836 | SvIOK_on(sv); | |
2837 | /* Assumption: first non-preserved integer is < IV_MAX, | |
2838 | this NV is in the preserved range, therefore: */ | |
2839 | if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv)) | |
2840 | < (UV)IV_MAX)) { | |
32fdb065 | 2841 | Perl_croak(aTHX_ "sv_2uv assumed (U_V(fabs((double)SvNVX(sv))) < (UV)IV_MAX) but SvNVX(sv)=%"NVgf" U_V is 0x%"UVxf", IV_MAX is 0x%"UVxf"\n", SvNVX(sv), U_V(SvNVX(sv)), (UV)IV_MAX); |
c2988b20 NC |
2842 | } |
2843 | } else | |
2844 | sv_2iuv_non_preserve (sv, numtype); | |
2845 | } | |
28e5dec8 | 2846 | #endif /* NV_PRESERVES_UV */ |
f7bbb42a | 2847 | } |
ff68c719 | 2848 | } |
2849 | else { | |
d008e5eb | 2850 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 2851 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
29489e7c | 2852 | report_uninit(sv); |
c6ee37c5 | 2853 | } |
25da4f38 IZ |
2854 | if (SvTYPE(sv) < SVt_IV) |
2855 | /* Typically the caller expects that sv_any is not NULL now. */ | |
2856 | sv_upgrade(sv, SVt_IV); | |
ff68c719 | 2857 | return 0; |
2858 | } | |
25da4f38 | 2859 | |
1d7c1841 GS |
2860 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n", |
2861 | PTR2UV(sv),SvUVX(sv))); | |
25da4f38 | 2862 | return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv); |
ff68c719 | 2863 | } |
2864 | ||
645c22ef DM |
2865 | /* |
2866 | =for apidoc sv_2nv | |
2867 | ||
2868 | Return the num value of an SV, doing any necessary string or integer | |
2869 | conversion, magic etc. Normally used via the C<SvNV(sv)> and C<SvNVx(sv)> | |
2870 | macros. | |
2871 | ||
2872 | =cut | |
2873 | */ | |
2874 | ||
65202027 | 2875 | NV |
864dbfa3 | 2876 | Perl_sv_2nv(pTHX_ register SV *sv) |
79072805 LW |
2877 | { |
2878 | if (!sv) | |
2879 | return 0.0; | |
8990e307 | 2880 | if (SvGMAGICAL(sv)) { |
463ee0b2 LW |
2881 | mg_get(sv); |
2882 | if (SvNOKp(sv)) | |
2883 | return SvNVX(sv); | |
a0d0e21e | 2884 | if (SvPOKp(sv) && SvLEN(sv)) { |
c2988b20 | 2885 | if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && |
504618e9 | 2886 | !grok_number(SvPVX_const(sv), SvCUR(sv), NULL)) |
a0d0e21e | 2887 | not_a_number(sv); |
3f7c398e | 2888 | return Atof(SvPVX_const(sv)); |
a0d0e21e | 2889 | } |
25da4f38 | 2890 | if (SvIOKp(sv)) { |
1c846c1f | 2891 | if (SvIsUV(sv)) |
65202027 | 2892 | return (NV)SvUVX(sv); |
25da4f38 | 2893 | else |
65202027 | 2894 | return (NV)SvIVX(sv); |
25da4f38 | 2895 | } |
16d20bd9 | 2896 | if (!SvROK(sv)) { |
d008e5eb | 2897 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 2898 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
29489e7c | 2899 | report_uninit(sv); |
c6ee37c5 | 2900 | } |
66a1b24b | 2901 | return (NV)0; |
16d20bd9 | 2902 | } |
463ee0b2 | 2903 | } |
ed6116ce | 2904 | if (SvTHINKFIRST(sv)) { |
a0d0e21e | 2905 | if (SvROK(sv)) { |
a0d0e21e | 2906 | SV* tmpstr; |
1554e226 | 2907 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) && |
b4b9a328 | 2908 | (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) |
9e7bc3e8 | 2909 | return SvNV(tmpstr); |
56431972 | 2910 | return PTR2NV(SvRV(sv)); |
a0d0e21e | 2911 | } |
765f542d NC |
2912 | if (SvIsCOW(sv)) { |
2913 | sv_force_normal_flags(sv, 0); | |
8a818333 | 2914 | } |
0336b60e | 2915 | if (SvREADONLY(sv) && !SvOK(sv)) { |
599cee73 | 2916 | if (ckWARN(WARN_UNINITIALIZED)) |
29489e7c | 2917 | report_uninit(sv); |
ed6116ce LW |
2918 | return 0.0; |
2919 | } | |
79072805 LW |
2920 | } |
2921 | if (SvTYPE(sv) < SVt_NV) { | |
463ee0b2 LW |
2922 | if (SvTYPE(sv) == SVt_IV) |
2923 | sv_upgrade(sv, SVt_PVNV); | |
2924 | else | |
2925 | sv_upgrade(sv, SVt_NV); | |
906f284f | 2926 | #ifdef USE_LONG_DOUBLE |
097ee67d | 2927 | DEBUG_c({ |
f93f4e46 | 2928 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
2929 | PerlIO_printf(Perl_debug_log, |
2930 | "0x%"UVxf" num(%" PERL_PRIgldbl ")\n", | |
2931 | PTR2UV(sv), SvNVX(sv)); | |
572bbb43 GS |
2932 | RESTORE_NUMERIC_LOCAL(); |
2933 | }); | |
65202027 | 2934 | #else |
572bbb43 | 2935 | DEBUG_c({ |
f93f4e46 | 2936 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1779d84d | 2937 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%"NVgf")\n", |
1d7c1841 | 2938 | PTR2UV(sv), SvNVX(sv)); |
097ee67d JH |
2939 | RESTORE_NUMERIC_LOCAL(); |
2940 | }); | |
572bbb43 | 2941 | #endif |
79072805 LW |
2942 | } |
2943 | else if (SvTYPE(sv) < SVt_PVNV) | |
2944 | sv_upgrade(sv, SVt_PVNV); | |
59d8ce62 NC |
2945 | if (SvNOKp(sv)) { |
2946 | return SvNVX(sv); | |
61604483 | 2947 | } |
59d8ce62 | 2948 | if (SvIOKp(sv)) { |
9d6ce603 | 2949 | SvNV_set(sv, SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv)); |
28e5dec8 JH |
2950 | #ifdef NV_PRESERVES_UV |
2951 | SvNOK_on(sv); | |
2952 | #else | |
2953 | /* Only set the public NV OK flag if this NV preserves the IV */ | |
2954 | /* Check it's not 0xFFFFFFFFFFFFFFFF */ | |
2955 | if (SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv)))) | |
2956 | : (SvIVX(sv) == I_V(SvNVX(sv)))) | |
2957 | SvNOK_on(sv); | |
2958 | else | |
2959 | SvNOKp_on(sv); | |
2960 | #endif | |
93a17b20 | 2961 | } |
748a9306 | 2962 | else if (SvPOKp(sv) && SvLEN(sv)) { |
c2988b20 | 2963 | UV value; |
3f7c398e | 2964 | const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value); |
c2988b20 | 2965 | if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !numtype) |
a0d0e21e | 2966 | not_a_number(sv); |
28e5dec8 | 2967 | #ifdef NV_PRESERVES_UV |
c2988b20 NC |
2968 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) |
2969 | == IS_NUMBER_IN_UV) { | |
5e045b90 | 2970 | /* It's definitely an integer */ |
9d6ce603 | 2971 | SvNV_set(sv, (numtype & IS_NUMBER_NEG) ? -(NV)value : (NV)value); |
c2988b20 | 2972 | } else |
3f7c398e | 2973 | SvNV_set(sv, Atof(SvPVX_const(sv))); |
28e5dec8 JH |
2974 | SvNOK_on(sv); |
2975 | #else | |
3f7c398e | 2976 | SvNV_set(sv, Atof(SvPVX_const(sv))); |
28e5dec8 JH |
2977 | /* Only set the public NV OK flag if this NV preserves the value in |
2978 | the PV at least as well as an IV/UV would. | |
2979 | Not sure how to do this 100% reliably. */ | |
2980 | /* if that shift count is out of range then Configure's test is | |
2981 | wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS == | |
2982 | UV_BITS */ | |
2983 | if (((UV)1 << NV_PRESERVES_UV_BITS) > | |
c2988b20 | 2984 | U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) { |
28e5dec8 | 2985 | SvNOK_on(sv); /* Definitely small enough to preserve all bits */ |
c2988b20 NC |
2986 | } else if (!(numtype & IS_NUMBER_IN_UV)) { |
2987 | /* Can't use strtol etc to convert this string, so don't try. | |
2988 | sv_2iv and sv_2uv will use the NV to convert, not the PV. */ | |
2989 | SvNOK_on(sv); | |
2990 | } else { | |
2991 | /* value has been set. It may not be precise. */ | |
2992 | if ((numtype & IS_NUMBER_NEG) && (value > (UV)IV_MIN)) { | |
2993 | /* 2s complement assumption for (UV)IV_MIN */ | |
2994 | SvNOK_on(sv); /* Integer is too negative. */ | |
2995 | } else { | |
2996 | SvNOKp_on(sv); | |
2997 | SvIOKp_on(sv); | |
6fa402ec | 2998 | |
c2988b20 | 2999 | if (numtype & IS_NUMBER_NEG) { |
45977657 | 3000 | SvIV_set(sv, -(IV)value); |
c2988b20 | 3001 | } else if (value <= (UV)IV_MAX) { |
45977657 | 3002 | SvIV_set(sv, (IV)value); |
c2988b20 | 3003 | } else { |
607fa7f2 | 3004 | SvUV_set(sv, value); |
c2988b20 NC |
3005 | SvIsUV_on(sv); |
3006 | } | |
3007 | ||
3008 | if (numtype & IS_NUMBER_NOT_INT) { | |
3009 | /* I believe that even if the original PV had decimals, | |
3010 | they are lost beyond the limit of the FP precision. | |
3011 | However, neither is canonical, so both only get p | |
3012 | flags. NWC, 2000/11/25 */ | |
3013 | /* Both already have p flags, so do nothing */ | |
3014 | } else { | |
66a1b24b | 3015 | const NV nv = SvNVX(sv); |
c2988b20 NC |
3016 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { |
3017 | if (SvIVX(sv) == I_V(nv)) { | |
3018 | SvNOK_on(sv); | |
3019 | SvIOK_on(sv); | |
3020 | } else { | |
3021 | SvIOK_on(sv); | |
3022 | /* It had no "." so it must be integer. */ | |
3023 | } | |
3024 | } else { | |
3025 | /* between IV_MAX and NV(UV_MAX). | |
3026 | Could be slightly > UV_MAX */ | |
6fa402ec | 3027 | |
c2988b20 NC |
3028 | if (numtype & IS_NUMBER_NOT_INT) { |
3029 | /* UV and NV both imprecise. */ | |
3030 | } else { | |
66a1b24b | 3031 | const UV nv_as_uv = U_V(nv); |
c2988b20 NC |
3032 | |
3033 | if (value == nv_as_uv && SvUVX(sv) != UV_MAX) { | |
3034 | SvNOK_on(sv); | |
3035 | SvIOK_on(sv); | |
3036 | } else { | |
3037 | SvIOK_on(sv); | |
3038 | } | |
3039 | } | |
3040 | } | |
3041 | } | |
3042 | } | |
3043 | } | |
28e5dec8 | 3044 | #endif /* NV_PRESERVES_UV */ |
93a17b20 | 3045 | } |
79072805 | 3046 | else { |
599cee73 | 3047 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP)) |
29489e7c | 3048 | report_uninit(sv); |
25da4f38 IZ |
3049 | if (SvTYPE(sv) < SVt_NV) |
3050 | /* Typically the caller expects that sv_any is not NULL now. */ | |
28e5dec8 JH |
3051 | /* XXX Ilya implies that this is a bug in callers that assume this |
3052 | and ideally should be fixed. */ | |
25da4f38 | 3053 | sv_upgrade(sv, SVt_NV); |
a0d0e21e | 3054 | return 0.0; |
79072805 | 3055 | } |
572bbb43 | 3056 | #if defined(USE_LONG_DOUBLE) |
097ee67d | 3057 | DEBUG_c({ |
f93f4e46 | 3058 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
3059 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n", |
3060 | PTR2UV(sv), SvNVX(sv)); | |
572bbb43 GS |
3061 | RESTORE_NUMERIC_LOCAL(); |
3062 | }); | |
65202027 | 3063 | #else |
572bbb43 | 3064 | DEBUG_c({ |
f93f4e46 | 3065 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1779d84d | 3066 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%"NVgf")\n", |
1d7c1841 | 3067 | PTR2UV(sv), SvNVX(sv)); |
097ee67d JH |
3068 | RESTORE_NUMERIC_LOCAL(); |
3069 | }); | |
572bbb43 | 3070 | #endif |
463ee0b2 | 3071 | return SvNVX(sv); |
79072805 LW |
3072 | } |
3073 | ||
645c22ef DM |
3074 | /* asIV(): extract an integer from the string value of an SV. |
3075 | * Caller must validate PVX */ | |
3076 | ||
76e3520e | 3077 | STATIC IV |
cea2e8a9 | 3078 | S_asIV(pTHX_ SV *sv) |
36477c24 | 3079 | { |
c2988b20 | 3080 | UV value; |
66a1b24b | 3081 | const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value); |
c2988b20 NC |
3082 | |
3083 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) | |
3084 | == IS_NUMBER_IN_UV) { | |
645c22ef | 3085 | /* It's definitely an integer */ |
c2988b20 NC |
3086 | if (numtype & IS_NUMBER_NEG) { |
3087 | if (value < (UV)IV_MIN) | |
3088 | return -(IV)value; | |
3089 | } else { | |
3090 | if (value < (UV)IV_MAX) | |
3091 | return (IV)value; | |
3092 | } | |
3093 | } | |
d008e5eb | 3094 | if (!numtype) { |
d008e5eb GS |
3095 | if (ckWARN(WARN_NUMERIC)) |
3096 | not_a_number(sv); | |
3097 | } | |
3f7c398e | 3098 | return I_V(Atof(SvPVX_const(sv))); |
36477c24 | 3099 | } |
3100 | ||
645c22ef DM |
3101 | /* asUV(): extract an unsigned integer from the string value of an SV |
3102 | * Caller must validate PVX */ | |
3103 | ||
76e3520e | 3104 | STATIC UV |
cea2e8a9 | 3105 | S_asUV(pTHX_ SV *sv) |
36477c24 | 3106 | { |
c2988b20 | 3107 | UV value; |
504618e9 | 3108 | const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value); |
36477c24 | 3109 | |
c2988b20 NC |
3110 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) |
3111 | == IS_NUMBER_IN_UV) { | |
645c22ef | 3112 | /* It's definitely an integer */ |
6fa402ec | 3113 | if (!(numtype & IS_NUMBER_NEG)) |
c2988b20 NC |
3114 | return value; |
3115 | } | |
d008e5eb | 3116 | if (!numtype) { |
d008e5eb GS |
3117 | if (ckWARN(WARN_NUMERIC)) |
3118 | not_a_number(sv); | |
3119 | } | |
3f7c398e | 3120 | return U_V(Atof(SvPVX_const(sv))); |
36477c24 | 3121 | } |
3122 | ||
645c22ef DM |
3123 | /* |
3124 | =for apidoc sv_2pv_nolen | |
3125 | ||
3126 | Like C<sv_2pv()>, but doesn't return the length too. You should usually | |
3127 | use the macro wrapper C<SvPV_nolen(sv)> instead. | |
3128 | =cut | |
3129 | */ | |
3130 | ||
79072805 | 3131 | char * |
864dbfa3 | 3132 | Perl_sv_2pv_nolen(pTHX_ register SV *sv) |
1fa8b10d | 3133 | { |
dafda6d1 | 3134 | return sv_2pv(sv, 0); |
1fa8b10d JD |
3135 | } |
3136 | ||
645c22ef DM |
3137 | /* uiv_2buf(): private routine for use by sv_2pv_flags(): print an IV or |
3138 | * UV as a string towards the end of buf, and return pointers to start and | |
3139 | * end of it. | |
3140 | * | |
3141 | * We assume that buf is at least TYPE_CHARS(UV) long. | |
3142 | */ | |
3143 | ||
864dbfa3 | 3144 | static char * |
25da4f38 IZ |
3145 | uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob) |
3146 | { | |
25da4f38 IZ |
3147 | char *ptr = buf + TYPE_CHARS(UV); |
3148 | char *ebuf = ptr; | |
3149 | int sign; | |
25da4f38 IZ |
3150 | |
3151 | if (is_uv) | |
3152 | sign = 0; | |
3153 | else if (iv >= 0) { | |
3154 | uv = iv; | |
3155 | sign = 0; | |
3156 | } else { | |
3157 | uv = -iv; | |
3158 | sign = 1; | |
3159 | } | |
3160 | do { | |
eb160463 | 3161 | *--ptr = '0' + (char)(uv % 10); |
25da4f38 IZ |
3162 | } while (uv /= 10); |
3163 | if (sign) | |
3164 | *--ptr = '-'; | |
3165 | *peob = ebuf; | |
3166 | return ptr; | |
3167 | } | |
3168 | ||
09540bc3 JH |
3169 | /* sv_2pv() is now a macro using Perl_sv_2pv_flags(); |
3170 | * this function provided for binary compatibility only | |
3171 | */ | |
3172 | ||
3173 | char * | |
3174 | Perl_sv_2pv(pTHX_ register SV *sv, STRLEN *lp) | |
3175 | { | |
3176 | return sv_2pv_flags(sv, lp, SV_GMAGIC); | |
3177 | } | |
3178 | ||
645c22ef DM |
3179 | /* |
3180 | =for apidoc sv_2pv_flags | |
3181 | ||
ff276b08 | 3182 | Returns a pointer to the string value of an SV, and sets *lp to its length. |
645c22ef DM |
3183 | If flags includes SV_GMAGIC, does an mg_get() first. Coerces sv to a string |
3184 | if necessary. | |
3185 | Normally invoked via the C<SvPV_flags> macro. C<sv_2pv()> and C<sv_2pv_nomg> | |
3186 | usually end up here too. | |
3187 | ||
3188 | =cut | |
3189 | */ | |
3190 | ||
8d6d96c1 HS |
3191 | char * |
3192 | Perl_sv_2pv_flags(pTHX_ register SV *sv, STRLEN *lp, I32 flags) | |
3193 | { | |
79072805 LW |
3194 | register char *s; |
3195 | int olderrno; | |
cb50f42d | 3196 | SV *tsv, *origsv; |
25da4f38 IZ |
3197 | char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */ |
3198 | char *tmpbuf = tbuf; | |
79072805 | 3199 | |
463ee0b2 | 3200 | if (!sv) { |
cdb061a3 NC |
3201 | if (lp) |
3202 | *lp = 0; | |
73d840c0 | 3203 | return (char *)""; |
463ee0b2 | 3204 | } |
8990e307 | 3205 | if (SvGMAGICAL(sv)) { |
8d6d96c1 HS |
3206 | if (flags & SV_GMAGIC) |
3207 | mg_get(sv); | |
463ee0b2 | 3208 | if (SvPOKp(sv)) { |
cdb061a3 NC |
3209 | if (lp) |
3210 | *lp = SvCUR(sv); | |
10516c54 NC |
3211 | if (flags & SV_MUTABLE_RETURN) |
3212 | return SvPVX_mutable(sv); | |
4d84ee25 NC |
3213 | if (flags & SV_CONST_RETURN) |
3214 | return (char *)SvPVX_const(sv); | |
463ee0b2 LW |
3215 | return SvPVX(sv); |
3216 | } | |
cf2093f6 | 3217 | if (SvIOKp(sv)) { |
1c846c1f | 3218 | if (SvIsUV(sv)) |
57def98f | 3219 | (void)sprintf(tmpbuf,"%"UVuf, (UV)SvUVX(sv)); |
cf2093f6 | 3220 | else |
57def98f | 3221 | (void)sprintf(tmpbuf,"%"IVdf, (IV)SvIVX(sv)); |
46fc3d4c | 3222 | tsv = Nullsv; |
a0d0e21e | 3223 | goto tokensave; |
463ee0b2 LW |
3224 | } |
3225 | if (SvNOKp(sv)) { | |
2d4389e4 | 3226 | Gconvert(SvNVX(sv), NV_DIG, 0, tmpbuf); |
46fc3d4c | 3227 | tsv = Nullsv; |
a0d0e21e | 3228 | goto tokensave; |
463ee0b2 | 3229 | } |
16d20bd9 | 3230 | if (!SvROK(sv)) { |
d008e5eb | 3231 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 3232 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
29489e7c | 3233 | report_uninit(sv); |
c6ee37c5 | 3234 | } |
cdb061a3 NC |
3235 | if (lp) |
3236 | *lp = 0; | |
73d840c0 | 3237 | return (char *)""; |
16d20bd9 | 3238 | } |
463ee0b2 | 3239 | } |
ed6116ce LW |
3240 | if (SvTHINKFIRST(sv)) { |
3241 | if (SvROK(sv)) { | |
a0d0e21e | 3242 | SV* tmpstr; |
e1ec3a88 | 3243 | register const char *typestr; |
1554e226 | 3244 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)) && |
b4b9a328 | 3245 | (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) { |
50adf7d2 NC |
3246 | /* Unwrap this: */ |
3247 | /* char *pv = lp ? SvPV(tmpstr, *lp) : SvPV_nolen(tmpstr); */ | |
3248 | ||
3249 | char *pv; | |
3250 | if ((SvFLAGS(tmpstr) & (SVf_POK)) == SVf_POK) { | |
3251 | if (flags & SV_CONST_RETURN) { | |
3252 | pv = (char *) SvPVX_const(tmpstr); | |
3253 | } else { | |
3254 | pv = (flags & SV_MUTABLE_RETURN) | |
3255 | ? SvPVX_mutable(tmpstr) : SvPVX(tmpstr); | |
3256 | } | |
3257 | if (lp) | |
3258 | *lp = SvCUR(tmpstr); | |
3259 | } else { | |
3260 | pv = sv_2pv_flags(tmpstr, lp, flags); | |
3261 | } | |
446eaa42 YST |
3262 | if (SvUTF8(tmpstr)) |
3263 | SvUTF8_on(sv); | |
3264 | else | |
3265 | SvUTF8_off(sv); | |
3266 | return pv; | |
3267 | } | |
cb50f42d | 3268 | origsv = sv; |
ed6116ce LW |
3269 | sv = (SV*)SvRV(sv); |
3270 | if (!sv) | |
e1ec3a88 | 3271 | typestr = "NULLREF"; |
ed6116ce | 3272 | else { |
f9277f47 IZ |
3273 | MAGIC *mg; |
3274 | ||
ed6116ce | 3275 | switch (SvTYPE(sv)) { |
f9277f47 IZ |
3276 | case SVt_PVMG: |
3277 | if ( ((SvFLAGS(sv) & | |
1c846c1f | 3278 | (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG)) |
faf82a0b | 3279 | == (SVs_OBJECT|SVs_SMG)) |
14befaf4 | 3280 | && (mg = mg_find(sv, PERL_MAGIC_qr))) { |
e1ec3a88 | 3281 | const regexp *re = (regexp *)mg->mg_obj; |
1bd3ad17 | 3282 | |
2cd61cdb | 3283 | if (!mg->mg_ptr) { |
e1ec3a88 | 3284 | const char *fptr = "msix"; |
8782bef2 GB |
3285 | char reflags[6]; |
3286 | char ch; | |
3287 | int left = 0; | |
3288 | int right = 4; | |
ff385a1b | 3289 | char need_newline = 0; |
eb160463 | 3290 | U16 reganch = (U16)((re->reganch & PMf_COMPILETIME) >> 12); |
8782bef2 | 3291 | |
155aba94 | 3292 | while((ch = *fptr++)) { |
8782bef2 GB |
3293 | if(reganch & 1) { |
3294 | reflags[left++] = ch; | |
3295 | } | |
3296 | else { | |
3297 | reflags[right--] = ch; | |
3298 | } | |
3299 | reganch >>= 1; | |
3300 | } | |
3301 | if(left != 4) { | |
3302 | reflags[left] = '-'; | |
3303 | left = 5; | |
3304 | } | |
3305 | ||
3306 | mg->mg_len = re->prelen + 4 + left; | |
ff385a1b JF |
3307 | /* |
3308 | * If /x was used, we have to worry about a regex | |
3309 | * ending with a comment later being embedded | |
3310 | * within another regex. If so, we don't want this | |
3311 | * regex's "commentization" to leak out to the | |
3312 | * right part of the enclosing regex, we must cap | |
3313 | * it with a newline. | |
3314 | * | |
3315 | * So, if /x was used, we scan backwards from the | |
3316 | * end of the regex. If we find a '#' before we | |
3317 | * find a newline, we need to add a newline | |
3318 | * ourself. If we find a '\n' first (or if we | |
3319 | * don't find '#' or '\n'), we don't need to add | |
3320 | * anything. -jfriedl | |
3321 | */ | |
3322 | if (PMf_EXTENDED & re->reganch) | |
3323 | { | |
e1ec3a88 | 3324 | const char *endptr = re->precomp + re->prelen; |
ff385a1b JF |
3325 | while (endptr >= re->precomp) |
3326 | { | |
e1ec3a88 | 3327 | const char c = *(endptr--); |
ff385a1b JF |
3328 | if (c == '\n') |
3329 | break; /* don't need another */ | |
3330 | if (c == '#') { | |
3331 | /* we end while in a comment, so we | |
3332 | need a newline */ | |
3333 | mg->mg_len++; /* save space for it */ | |
3334 | need_newline = 1; /* note to add it */ | |
ab01544f | 3335 | break; |
ff385a1b JF |
3336 | } |
3337 | } | |
3338 | } | |
3339 | ||
8782bef2 GB |
3340 | New(616, mg->mg_ptr, mg->mg_len + 1 + left, char); |
3341 | Copy("(?", mg->mg_ptr, 2, char); | |
3342 | Copy(reflags, mg->mg_ptr+2, left, char); | |
3343 | Copy(":", mg->mg_ptr+left+2, 1, char); | |
3344 | Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char); | |
ff385a1b JF |
3345 | if (need_newline) |
3346 | mg->mg_ptr[mg->mg_len - 2] = '\n'; | |
1bd3ad17 IZ |
3347 | mg->mg_ptr[mg->mg_len - 1] = ')'; |
3348 | mg->mg_ptr[mg->mg_len] = 0; | |
3349 | } | |
3280af22 | 3350 | PL_reginterp_cnt += re->program[0].next_off; |
cb50f42d YST |
3351 | |
3352 | if (re->reganch & ROPT_UTF8) | |
3353 | SvUTF8_on(origsv); | |
3354 | else | |
3355 | SvUTF8_off(origsv); | |
cdb061a3 NC |
3356 | if (lp) |
3357 | *lp = mg->mg_len; | |
1bd3ad17 | 3358 | return mg->mg_ptr; |
f9277f47 IZ |
3359 | } |
3360 | /* Fall through */ | |
ed6116ce LW |
3361 | case SVt_NULL: |
3362 | case SVt_IV: | |
3363 | case SVt_NV: | |
3364 | case SVt_RV: | |
3365 | case SVt_PV: | |
3366 | case SVt_PVIV: | |
3367 | case SVt_PVNV: | |
e1ec3a88 AL |
3368 | case SVt_PVBM: typestr = SvROK(sv) ? "REF" : "SCALAR"; break; |
3369 | case SVt_PVLV: typestr = SvROK(sv) ? "REF" | |
be65207d DM |
3370 | /* tied lvalues should appear to be |
3371 | * scalars for backwards compatitbility */ | |
3372 | : (LvTYPE(sv) == 't' || LvTYPE(sv) == 'T') | |
3373 | ? "SCALAR" : "LVALUE"; break; | |
e1ec3a88 AL |
3374 | case SVt_PVAV: typestr = "ARRAY"; break; |
3375 | case SVt_PVHV: typestr = "HASH"; break; | |
3376 | case SVt_PVCV: typestr = "CODE"; break; | |
3377 | case SVt_PVGV: typestr = "GLOB"; break; | |
3378 | case SVt_PVFM: typestr = "FORMAT"; break; | |
3379 | case SVt_PVIO: typestr = "IO"; break; | |
3380 | default: typestr = "UNKNOWN"; break; | |
ed6116ce | 3381 | } |
46fc3d4c | 3382 | tsv = NEWSV(0,0); |
a5cb6b62 | 3383 | if (SvOBJECT(sv)) { |
bfcb3514 | 3384 | const char *name = HvNAME_get(SvSTASH(sv)); |
a5cb6b62 | 3385 | Perl_sv_setpvf(aTHX_ tsv, "%s=%s(0x%"UVxf")", |
e1ec3a88 | 3386 | name ? name : "__ANON__" , typestr, PTR2UV(sv)); |
a5cb6b62 | 3387 | } |
ed6116ce | 3388 | else |
e1ec3a88 | 3389 | Perl_sv_setpvf(aTHX_ tsv, "%s(0x%"UVxf")", typestr, PTR2UV(sv)); |
a0d0e21e | 3390 | goto tokensaveref; |
463ee0b2 | 3391 | } |
cdb061a3 NC |
3392 | if (lp) |
3393 | *lp = strlen(typestr); | |
73d840c0 | 3394 | return (char *)typestr; |
79072805 | 3395 | } |
0336b60e | 3396 | if (SvREADONLY(sv) && !SvOK(sv)) { |
0336b60e | 3397 | if (ckWARN(WARN_UNINITIALIZED)) |
29489e7c | 3398 | report_uninit(sv); |
cdb061a3 NC |
3399 | if (lp) |
3400 | *lp = 0; | |
73d840c0 | 3401 | return (char *)""; |
79072805 | 3402 | } |
79072805 | 3403 | } |
28e5dec8 JH |
3404 | if (SvIOK(sv) || ((SvIOKp(sv) && !SvNOKp(sv)))) { |
3405 | /* I'm assuming that if both IV and NV are equally valid then | |
3406 | converting the IV is going to be more efficient */ | |
e1ec3a88 AL |
3407 | const U32 isIOK = SvIOK(sv); |
3408 | const U32 isUIOK = SvIsUV(sv); | |
28e5dec8 JH |
3409 | char buf[TYPE_CHARS(UV)]; |
3410 | char *ebuf, *ptr; | |
3411 | ||
3412 | if (SvTYPE(sv) < SVt_PVIV) | |
3413 | sv_upgrade(sv, SVt_PVIV); | |
3414 | if (isUIOK) | |
3415 | ptr = uiv_2buf(buf, 0, SvUVX(sv), 1, &ebuf); | |
3416 | else | |
3417 | ptr = uiv_2buf(buf, SvIVX(sv), 0, 0, &ebuf); | |
5902b6a9 NC |
3418 | /* inlined from sv_setpvn */ |
3419 | SvGROW_mutable(sv, (STRLEN)(ebuf - ptr + 1)); | |
4d84ee25 | 3420 | Move(ptr,SvPVX_mutable(sv),ebuf - ptr,char); |
28e5dec8 JH |
3421 | SvCUR_set(sv, ebuf - ptr); |
3422 | s = SvEND(sv); | |
3423 | *s = '\0'; | |
3424 | if (isIOK) | |
3425 | SvIOK_on(sv); | |
3426 | else | |
3427 | SvIOKp_on(sv); | |
3428 | if (isUIOK) | |
3429 | SvIsUV_on(sv); | |
3430 | } | |
3431 | else if (SvNOKp(sv)) { | |
79072805 LW |
3432 | if (SvTYPE(sv) < SVt_PVNV) |
3433 | sv_upgrade(sv, SVt_PVNV); | |
1c846c1f | 3434 | /* The +20 is pure guesswork. Configure test needed. --jhi */ |
5902b6a9 | 3435 | s = SvGROW_mutable(sv, NV_DIG + 20); |
79072805 | 3436 | olderrno = errno; /* some Xenix systems wipe out errno here */ |
79072805 | 3437 | #ifdef apollo |
463ee0b2 | 3438 | if (SvNVX(sv) == 0.0) |
79072805 LW |
3439 | (void)strcpy(s,"0"); |
3440 | else | |
3441 | #endif /*apollo*/ | |
bbce6d69 | 3442 | { |
2d4389e4 | 3443 | Gconvert(SvNVX(sv), NV_DIG, 0, s); |
bbce6d69 | 3444 | } |
79072805 | 3445 | errno = olderrno; |
a0d0e21e LW |
3446 | #ifdef FIXNEGATIVEZERO |
3447 | if (*s == '-' && s[1] == '0' && !s[2]) | |
3448 | strcpy(s,"0"); | |
3449 | #endif | |
79072805 LW |
3450 | while (*s) s++; |
3451 | #ifdef hcx | |
3452 | if (s[-1] == '.') | |
46fc3d4c | 3453 | *--s = '\0'; |
79072805 LW |
3454 | #endif |
3455 | } | |
79072805 | 3456 | else { |
0336b60e IZ |
3457 | if (ckWARN(WARN_UNINITIALIZED) |
3458 | && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP)) | |
29489e7c | 3459 | report_uninit(sv); |
cdb061a3 | 3460 | if (lp) |
a0d0e21e | 3461 | *lp = 0; |
25da4f38 IZ |
3462 | if (SvTYPE(sv) < SVt_PV) |
3463 | /* Typically the caller expects that sv_any is not NULL now. */ | |
3464 | sv_upgrade(sv, SVt_PV); | |
73d840c0 | 3465 | return (char *)""; |
79072805 | 3466 | } |
cdb061a3 NC |
3467 | { |
3468 | STRLEN len = s - SvPVX_const(sv); | |
3469 | if (lp) | |
3470 | *lp = len; | |
3471 | SvCUR_set(sv, len); | |
3472 | } | |
79072805 | 3473 | SvPOK_on(sv); |
1d7c1841 | 3474 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n", |
3f7c398e | 3475 | PTR2UV(sv),SvPVX_const(sv))); |
4d84ee25 NC |
3476 | if (flags & SV_CONST_RETURN) |
3477 | return (char *)SvPVX_const(sv); | |
10516c54 NC |
3478 | if (flags & SV_MUTABLE_RETURN) |
3479 | return SvPVX_mutable(sv); | |
463ee0b2 | 3480 | return SvPVX(sv); |
a0d0e21e LW |
3481 | |
3482 | tokensave: | |
3483 | if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */ | |
3484 | /* Sneaky stuff here */ | |
3485 | ||
3486 | tokensaveref: | |
46fc3d4c | 3487 | if (!tsv) |
96827780 | 3488 | tsv = newSVpv(tmpbuf, 0); |
46fc3d4c | 3489 | sv_2mortal(tsv); |
cdb061a3 NC |
3490 | if (lp) |
3491 | *lp = SvCUR(tsv); | |
46fc3d4c | 3492 | return SvPVX(tsv); |
a0d0e21e LW |
3493 | } |
3494 | else { | |
27da23d5 | 3495 | dVAR; |
a0d0e21e | 3496 | STRLEN len; |
73d840c0 | 3497 | const char *t; |
46fc3d4c | 3498 | |
3499 | if (tsv) { | |
3500 | sv_2mortal(tsv); | |
3f7c398e | 3501 | t = SvPVX_const(tsv); |
46fc3d4c | 3502 | len = SvCUR(tsv); |
3503 | } | |
3504 | else { | |
96827780 MB |
3505 | t = tmpbuf; |
3506 | len = strlen(tmpbuf); | |
46fc3d4c | 3507 | } |
a0d0e21e | 3508 | #ifdef FIXNEGATIVEZERO |
46fc3d4c | 3509 | if (len == 2 && t[0] == '-' && t[1] == '0') { |
3510 | t = "0"; | |
3511 | len = 1; | |
3512 | } | |
a0d0e21e | 3513 | #endif |
862a34c6 | 3514 | SvUPGRADE(sv, SVt_PV); |
cdb061a3 NC |
3515 | if (lp) |
3516 | *lp = len; | |
5902b6a9 | 3517 | s = SvGROW_mutable(sv, len + 1); |
a0d0e21e | 3518 | SvCUR_set(sv, len); |
6bf554b4 | 3519 | SvPOKp_on(sv); |
e90e2364 | 3520 | return strcpy(s, t); |
a0d0e21e | 3521 | } |
463ee0b2 LW |
3522 | } |
3523 | ||
645c22ef | 3524 | /* |
6050d10e JP |
3525 | =for apidoc sv_copypv |
3526 | ||
3527 | Copies a stringified representation of the source SV into the | |
3528 | destination SV. Automatically performs any necessary mg_get and | |
54f0641b | 3529 | coercion of numeric values into strings. Guaranteed to preserve |
6050d10e | 3530 | UTF-8 flag even from overloaded objects. Similar in nature to |
54f0641b NIS |
3531 | sv_2pv[_flags] but operates directly on an SV instead of just the |
3532 | string. Mostly uses sv_2pv_flags to do its work, except when that | |
6050d10e JP |
3533 | would lose the UTF-8'ness of the PV. |
3534 | ||
3535 | =cut | |
3536 | */ | |
3537 | ||
3538 | void | |
3539 | Perl_sv_copypv(pTHX_ SV *dsv, register SV *ssv) | |
3540 | { | |
446eaa42 | 3541 | STRLEN len; |
4d84ee25 NC |
3542 | const char *s; |
3543 | s = SvPV_const(ssv,len); | |
cb50f42d | 3544 | sv_setpvn(dsv,s,len); |
446eaa42 | 3545 | if (SvUTF8(ssv)) |
cb50f42d | 3546 | SvUTF8_on(dsv); |
446eaa42 | 3547 | else |
cb50f42d | 3548 | SvUTF8_off(dsv); |
6050d10e JP |
3549 | } |
3550 | ||
3551 | /* | |
645c22ef DM |
3552 | =for apidoc sv_2pvbyte_nolen |
3553 | ||
3554 | Return a pointer to the byte-encoded representation of the SV. | |
1e54db1a | 3555 | May cause the SV to be downgraded from UTF-8 as a side-effect. |
645c22ef DM |
3556 | |
3557 | Usually accessed via the C<SvPVbyte_nolen> macro. | |
3558 | ||
3559 | =cut | |
3560 | */ | |
3561 | ||
7340a771 GS |
3562 | char * |
3563 | Perl_sv_2pvbyte_nolen(pTHX_ register SV *sv) | |
3564 | { | |
dafda6d1 | 3565 | return sv_2pvbyte(sv, 0); |
7340a771 GS |
3566 | } |
3567 | ||
645c22ef DM |
3568 | /* |
3569 | =for apidoc sv_2pvbyte | |
3570 | ||
3571 | Return a pointer to the byte-encoded representation of the SV, and set *lp | |
1e54db1a | 3572 | to its length. May cause the SV to be downgraded from UTF-8 as a |
645c22ef DM |
3573 | side-effect. |
3574 | ||
3575 | Usually accessed via the C<SvPVbyte> macro. | |
3576 | ||
3577 | =cut | |
3578 | */ | |
3579 | ||
7340a771 GS |
3580 | char * |
3581 | Perl_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp) | |
3582 | { | |
0875d2fe | 3583 | sv_utf8_downgrade(sv,0); |
97972285 | 3584 | return lp ? SvPV(sv,*lp) : SvPV_nolen(sv); |
7340a771 GS |
3585 | } |
3586 | ||
645c22ef DM |
3587 | /* |
3588 | =for apidoc sv_2pvutf8_nolen | |
3589 | ||
1e54db1a JH |
3590 | Return a pointer to the UTF-8-encoded representation of the SV. |
3591 | May cause the SV to be upgraded to UTF-8 as a side-effect. | |
645c22ef DM |
3592 | |
3593 | Usually accessed via the C<SvPVutf8_nolen> macro. | |
3594 | ||
3595 | =cut | |
3596 | */ | |
3597 | ||
7340a771 GS |
3598 | char * |
3599 | Perl_sv_2pvutf8_nolen(pTHX_ register SV *sv) | |
3600 | { | |
dafda6d1 | 3601 | return sv_2pvutf8(sv, 0); |
7340a771 GS |
3602 | } |
3603 | ||
645c22ef DM |
3604 | /* |
3605 | =for apidoc sv_2pvutf8 | |
3606 | ||
1e54db1a JH |
3607 | Return a pointer to the UTF-8-encoded representation of the SV, and set *lp |
3608 | to its length. May cause the SV to be upgraded to UTF-8 as a side-effect. | |
645c22ef DM |
3609 | |
3610 | Usually accessed via the C<SvPVutf8> macro. | |
3611 | ||
3612 | =cut | |
3613 | */ | |
3614 | ||
7340a771 GS |
3615 | char * |
3616 | Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp) | |
3617 | { | |
560a288e | 3618 | sv_utf8_upgrade(sv); |
7d59b7e4 | 3619 | return SvPV(sv,*lp); |
7340a771 | 3620 | } |
1c846c1f | 3621 | |
645c22ef DM |
3622 | /* |
3623 | =for apidoc sv_2bool | |
3624 | ||
3625 | This function is only called on magical items, and is only used by | |
8cf8f3d1 | 3626 | sv_true() or its macro equivalent. |
645c22ef DM |
3627 | |
3628 | =cut | |
3629 | */ | |
3630 | ||
463ee0b2 | 3631 | bool |
864dbfa3 | 3632 | Perl_sv_2bool(pTHX_ register SV *sv) |
463ee0b2 | 3633 | { |
8990e307 | 3634 | if (SvGMAGICAL(sv)) |
463ee0b2 LW |
3635 | mg_get(sv); |
3636 | ||
a0d0e21e LW |
3637 | if (!SvOK(sv)) |
3638 | return 0; | |
3639 | if (SvROK(sv)) { | |
a0d0e21e | 3640 | SV* tmpsv; |
1554e226 | 3641 | if (SvAMAGIC(sv) && (tmpsv=AMG_CALLun(sv,bool_)) && |
9e3013b1 | 3642 | (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv)))) |
8a31060d | 3643 | return (bool)SvTRUE(tmpsv); |
a0d0e21e LW |
3644 | return SvRV(sv) != 0; |
3645 | } | |
463ee0b2 | 3646 | if (SvPOKp(sv)) { |
11343788 MB |
3647 | register XPV* Xpvtmp; |
3648 | if ((Xpvtmp = (XPV*)SvANY(sv)) && | |
339049b0 | 3649 | (*sv->sv_u.svu_pv > '0' || |
11343788 | 3650 | Xpvtmp->xpv_cur > 1 || |
339049b0 | 3651 | (Xpvtmp->xpv_cur && *sv->sv_u.svu_pv != '0'))) |
463ee0b2 LW |
3652 | return 1; |
3653 | else | |
3654 | return 0; | |
3655 | } | |
3656 | else { | |
3657 | if (SvIOKp(sv)) | |
3658 | return SvIVX(sv) != 0; | |
3659 | else { | |
3660 | if (SvNOKp(sv)) | |
3661 | return SvNVX(sv) != 0.0; | |
3662 | else | |
3663 | return FALSE; | |
3664 | } | |
3665 | } | |
79072805 LW |
3666 | } |
3667 | ||
09540bc3 JH |
3668 | /* sv_utf8_upgrade() is now a macro using sv_utf8_upgrade_flags(); |
3669 | * this function provided for binary compatibility only | |
3670 | */ | |
3671 | ||
3672 | ||
3673 | STRLEN | |
3674 | Perl_sv_utf8_upgrade(pTHX_ register SV *sv) | |
3675 | { | |
3676 | return sv_utf8_upgrade_flags(sv, SV_GMAGIC); | |
3677 | } | |
3678 | ||
c461cf8f JH |
3679 | /* |
3680 | =for apidoc sv_utf8_upgrade | |
3681 | ||
78ea37eb | 3682 | Converts the PV of an SV to its UTF-8-encoded form. |
645c22ef | 3683 | Forces the SV to string form if it is not already. |
4411f3b6 NIS |
3684 | Always sets the SvUTF8 flag to avoid future validity checks even |
3685 | if all the bytes have hibit clear. | |
c461cf8f | 3686 | |
13a6c0e0 JH |
3687 | This is not as a general purpose byte encoding to Unicode interface: |
3688 | use the Encode extension for that. | |
3689 | ||
8d6d96c1 HS |
3690 | =for apidoc sv_utf8_upgrade_flags |
3691 | ||
78ea37eb | 3692 | Converts the PV of an SV to its UTF-8-encoded form. |
645c22ef | 3693 | Forces the SV to string form if it is not already. |
8d6d96c1 HS |
3694 | Always sets the SvUTF8 flag to avoid future validity checks even |
3695 | if all the bytes have hibit clear. If C<flags> has C<SV_GMAGIC> bit set, | |
3696 | will C<mg_get> on C<sv> if appropriate, else not. C<sv_utf8_upgrade> and | |
3697 | C<sv_utf8_upgrade_nomg> are implemented in terms of this function. | |
3698 | ||
13a6c0e0 JH |
3699 | This is not as a general purpose byte encoding to Unicode interface: |
3700 | use the Encode extension for that. | |
3701 | ||
8d6d96c1 HS |
3702 | =cut |
3703 | */ | |
3704 | ||
3705 | STRLEN | |
3706 | Perl_sv_utf8_upgrade_flags(pTHX_ register SV *sv, I32 flags) | |
3707 | { | |
808c356f RGS |
3708 | if (sv == &PL_sv_undef) |
3709 | return 0; | |
e0e62c2a NIS |
3710 | if (!SvPOK(sv)) { |
3711 | STRLEN len = 0; | |
d52b7888 NC |
3712 | if (SvREADONLY(sv) && (SvPOKp(sv) || SvIOKp(sv) || SvNOKp(sv))) { |
3713 | (void) sv_2pv_flags(sv,&len, flags); | |
3714 | if (SvUTF8(sv)) | |
3715 | return len; | |
3716 | } else { | |
3717 | (void) SvPV_force(sv,len); | |
3718 | } | |
e0e62c2a | 3719 | } |
4411f3b6 | 3720 | |
f5cee72b | 3721 | if (SvUTF8(sv)) { |
5fec3b1d | 3722 | return SvCUR(sv); |
f5cee72b | 3723 | } |
5fec3b1d | 3724 | |
765f542d NC |
3725 | if (SvIsCOW(sv)) { |
3726 | sv_force_normal_flags(sv, 0); | |
db42d148 NIS |
3727 | } |
3728 | ||
88632417 | 3729 | if (PL_encoding && !(flags & SV_UTF8_NO_ENCODING)) |
799ef3cb | 3730 | sv_recode_to_utf8(sv, PL_encoding); |
9f4817db | 3731 | else { /* Assume Latin-1/EBCDIC */ |
c4e7c712 NC |
3732 | /* This function could be much more efficient if we |
3733 | * had a FLAG in SVs to signal if there are any hibit | |
3734 | * chars in the PV. Given that there isn't such a flag | |
3735 | * make the loop as fast as possible. */ | |
93524f2b NC |
3736 | const U8 *s = (U8 *) SvPVX_const(sv); |
3737 | const U8 *e = (U8 *) SvEND(sv); | |
3738 | const U8 *t = s; | |
c4e7c712 NC |
3739 | int hibit = 0; |
3740 | ||
3741 | while (t < e) { | |
3742 | U8 ch = *t++; | |
3743 | if ((hibit = !NATIVE_IS_INVARIANT(ch))) | |
3744 | break; | |
3745 | } | |
3746 | if (hibit) { | |
3747 | STRLEN len = SvCUR(sv) + 1; /* Plus the \0 */ | |
1e2ebb21 | 3748 | U8 *recoded = bytes_to_utf8((U8*)s, &len); |
c4e7c712 NC |
3749 | |
3750 | SvPV_free(sv); /* No longer using what was there before. */ | |
3751 | ||
1e2ebb21 | 3752 | SvPV_set(sv, (char*)recoded); |
c4e7c712 NC |
3753 | SvCUR_set(sv, len - 1); |
3754 | SvLEN_set(sv, len); /* No longer know the real size. */ | |
3755 | } | |
3756 | /* Mark as UTF-8 even if no hibit - saves scanning loop */ | |
3757 | SvUTF8_on(sv); | |
560a288e | 3758 | } |
4411f3b6 | 3759 | return SvCUR(sv); |
560a288e GS |
3760 | } |
3761 | ||
c461cf8f JH |
3762 | /* |
3763 | =for apidoc sv_utf8_downgrade | |
3764 | ||
78ea37eb TS |
3765 | Attempts to convert the PV of an SV from characters to bytes. |
3766 | If the PV contains a character beyond byte, this conversion will fail; | |
3767 | in this case, either returns false or, if C<fail_ok> is not | |
c461cf8f JH |
3768 | true, croaks. |
3769 | ||
13a6c0e0 JH |
3770 | This is not as a general purpose Unicode to byte encoding interface: |
3771 | use the Encode extension for that. | |
3772 | ||
c461cf8f JH |
3773 | =cut |
3774 | */ | |
3775 | ||
560a288e GS |
3776 | bool |
3777 | Perl_sv_utf8_downgrade(pTHX_ register SV* sv, bool fail_ok) | |
3778 | { | |
78ea37eb | 3779 | if (SvPOKp(sv) && SvUTF8(sv)) { |
fa301091 | 3780 | if (SvCUR(sv)) { |
03cfe0ae | 3781 | U8 *s; |
652088fc | 3782 | STRLEN len; |
fa301091 | 3783 | |
765f542d NC |
3784 | if (SvIsCOW(sv)) { |
3785 | sv_force_normal_flags(sv, 0); | |
3786 | } | |
03cfe0ae NIS |
3787 | s = (U8 *) SvPV(sv, len); |
3788 | if (!utf8_to_bytes(s, &len)) { | |