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