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