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