This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Stop '"__attribute__format__" redefined' warnings from gcc on Win32
[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)) {
444 if ( SvOBJECT(GvSV(sv)) ||
445 (GvAV(sv) && SvOBJECT(GvAV(sv))) ||
446 (GvHV(sv) && SvOBJECT(GvHV(sv))) ||
447 (GvIO(sv) && SvOBJECT(GvIO(sv))) ||
448 (GvCV(sv) && SvOBJECT(GvCV(sv))) )
449 {
450 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning named glob object:\n "), sv_dump(sv)));
ec5f3c78 451 SvFLAGS(sv) |= SVf_BREAK;
645c22ef
DM
452 SvREFCNT_dec(sv);
453 }
454 }
455}
456#endif
457
458/*
459=for apidoc sv_clean_objs
460
461Attempt to destroy all objects not yet freed
462
463=cut
464*/
465
4561caa4 466void
864dbfa3 467Perl_sv_clean_objs(pTHX)
4561caa4 468{
3280af22 469 PL_in_clean_objs = TRUE;
055972dc 470 visit(do_clean_objs, SVf_ROK, SVf_ROK);
4561caa4 471#ifndef DISABLE_DESTRUCTOR_KLUDGE
2d0f3c12 472 /* some barnacles may yet remain, clinging to typeglobs */
055972dc 473 visit(do_clean_named_objs, SVt_PVGV, SVTYPEMASK);
4561caa4 474#endif
3280af22 475 PL_in_clean_objs = FALSE;
4561caa4
CS
476}
477
645c22ef
DM
478/* called by sv_clean_all() for each live SV */
479
480static void
acfe0abc 481do_clean_all(pTHX_ SV *sv)
645c22ef
DM
482{
483 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) ));
484 SvFLAGS(sv) |= SVf_BREAK;
0e705b3b
DM
485 if (PL_comppad == (AV*)sv) {
486 PL_comppad = Nullav;
487 PL_curpad = Null(SV**);
488 }
645c22ef
DM
489 SvREFCNT_dec(sv);
490}
491
492/*
493=for apidoc sv_clean_all
494
495Decrement the refcnt of each remaining SV, possibly triggering a
496cleanup. This function may have to be called multiple times to free
ff276b08 497SVs which are in complex self-referential hierarchies.
645c22ef
DM
498
499=cut
500*/
501
5226ed68 502I32
864dbfa3 503Perl_sv_clean_all(pTHX)
8990e307 504{
5226ed68 505 I32 cleaned;
3280af22 506 PL_in_clean_all = TRUE;
055972dc 507 cleaned = visit(do_clean_all, 0,0);
3280af22 508 PL_in_clean_all = FALSE;
5226ed68 509 return cleaned;
8990e307 510}
463ee0b2 511
7cfef17e
NC
512static void
513S_free_arena(pTHX_ void **root) {
514 while (root) {
1b6737cc 515 void ** const next = *(void **)root;
7cfef17e
NC
516 Safefree(root);
517 root = next;
518 }
519}
520
645c22ef
DM
521/*
522=for apidoc sv_free_arenas
523
524Deallocate the memory used by all arenas. Note that all the individual SV
525heads and bodies within the arenas must already have been freed.
526
527=cut
528*/
529
7cfef17e
NC
530#define free_arena(name) \
531 STMT_START { \
532 S_free_arena(aTHX_ (void**) PL_ ## name ## _arenaroot); \
533 PL_ ## name ## _arenaroot = 0; \
534 PL_ ## name ## _root = 0; \
535 } STMT_END
536
4633a7c4 537void
864dbfa3 538Perl_sv_free_arenas(pTHX)
4633a7c4
LW
539{
540 SV* sva;
541 SV* svanext;
542
543 /* Free arenas here, but be careful about fake ones. (We assume
544 contiguity of the fake ones with the corresponding real ones.) */
545
3280af22 546 for (sva = PL_sv_arenaroot; sva; sva = svanext) {
4633a7c4
LW
547 svanext = (SV*) SvANY(sva);
548 while (svanext && SvFAKE(svanext))
549 svanext = (SV*) SvANY(svanext);
550
551 if (!SvFAKE(sva))
1df70142 552 Safefree(sva);
4633a7c4 553 }
8b4f5e17 554
7cfef17e
NC
555 free_arena(xnv);
556 free_arena(xpv);
557 free_arena(xpviv);
558 free_arena(xpvnv);
559 free_arena(xpvcv);
560 free_arena(xpvav);
561 free_arena(xpvhv);
562 free_arena(xpvmg);
563 free_arena(xpvgv);
564 free_arena(xpvlv);
565 free_arena(xpvbm);
566 free_arena(he);
567#if defined(USE_ITHREADS)
568 free_arena(pte);
569#endif
612f20c3 570
3280af22
NIS
571 if (PL_nice_chunk)
572 Safefree(PL_nice_chunk);
573 PL_nice_chunk = Nullch;
574 PL_nice_chunk_size = 0;
575 PL_sv_arenaroot = 0;
576 PL_sv_root = 0;
4633a7c4
LW
577}
578
29489e7c
DM
579/* ---------------------------------------------------------------------
580 *
581 * support functions for report_uninit()
582 */
583
584/* the maxiumum size of array or hash where we will scan looking
585 * for the undefined element that triggered the warning */
586
587#define FUV_MAX_SEARCH_SIZE 1000
588
589/* Look for an entry in the hash whose value has the same SV as val;
590 * If so, return a mortal copy of the key. */
591
592STATIC SV*
593S_find_hash_subscript(pTHX_ HV *hv, SV* val)
594{
27da23d5 595 dVAR;
29489e7c 596 register HE **array;
29489e7c
DM
597 I32 i;
598
599 if (!hv || SvMAGICAL(hv) || !HvARRAY(hv) ||
600 (HvTOTALKEYS(hv) > FUV_MAX_SEARCH_SIZE))
601 return Nullsv;
602
603 array = HvARRAY(hv);
604
605 for (i=HvMAX(hv); i>0; i--) {
f54cb97a 606 register HE *entry;
29489e7c
DM
607 for (entry = array[i]; entry; entry = HeNEXT(entry)) {
608 if (HeVAL(entry) != val)
609 continue;
610 if ( HeVAL(entry) == &PL_sv_undef ||
611 HeVAL(entry) == &PL_sv_placeholder)
612 continue;
613 if (!HeKEY(entry))
614 return Nullsv;
615 if (HeKLEN(entry) == HEf_SVKEY)
616 return sv_mortalcopy(HeKEY_sv(entry));
617 return sv_2mortal(newSVpvn(HeKEY(entry), HeKLEN(entry)));
618 }
619 }
620 return Nullsv;
621}
622
623/* Look for an entry in the array whose value has the same SV as val;
624 * If so, return the index, otherwise return -1. */
625
626STATIC I32
627S_find_array_subscript(pTHX_ AV *av, SV* val)
628{
629 SV** svp;
630 I32 i;
631 if (!av || SvMAGICAL(av) || !AvARRAY(av) ||
632 (AvFILLp(av) > FUV_MAX_SEARCH_SIZE))
633 return -1;
634
635 svp = AvARRAY(av);
636 for (i=AvFILLp(av); i>=0; i--) {
637 if (svp[i] == val && svp[i] != &PL_sv_undef)
638 return i;
639 }
640 return -1;
641}
642
643/* S_varname(): return the name of a variable, optionally with a subscript.
644 * If gv is non-zero, use the name of that global, along with gvtype (one
645 * of "$", "@", "%"); otherwise use the name of the lexical at pad offset
646 * targ. Depending on the value of the subscript_type flag, return:
647 */
648
649#define FUV_SUBSCRIPT_NONE 1 /* "@foo" */
650#define FUV_SUBSCRIPT_ARRAY 2 /* "$foo[aindex]" */
651#define FUV_SUBSCRIPT_HASH 3 /* "$foo{keyname}" */
652#define FUV_SUBSCRIPT_WITHIN 4 /* "within @foo" */
653
654STATIC SV*
bfed75c6 655S_varname(pTHX_ GV *gv, const char *gvtype, PADOFFSET targ,
29489e7c
DM
656 SV* keyname, I32 aindex, int subscript_type)
657{
29489e7c 658
a3b680e6 659 SV * const name = sv_newmortal();
29489e7c
DM
660 if (gv) {
661
662 /* simulate gv_fullname4(), but add literal '^' for $^FOO names
663 * XXX get rid of all this if gv_fullnameX() ever supports this
664 * directly */
665
bfed75c6 666 const char *p;
53c1dcc0 667 HV * const hv = GvSTASH(gv);
29489e7c
DM
668 sv_setpv(name, gvtype);
669 if (!hv)
670 p = "???";
bfcb3514 671 else if (!(p=HvNAME_get(hv)))
29489e7c 672 p = "__ANON__";
29489e7c
DM
673 if (strNE(p, "main")) {
674 sv_catpv(name,p);
675 sv_catpvn(name,"::", 2);
676 }
677 if (GvNAMELEN(gv)>= 1 &&
678 ((unsigned int)*GvNAME(gv)) <= 26)
679 { /* handle $^FOO */
680 Perl_sv_catpvf(aTHX_ name,"^%c", *GvNAME(gv) + 'A' - 1);
681 sv_catpvn(name,GvNAME(gv)+1,GvNAMELEN(gv)-1);
682 }
683 else
684 sv_catpvn(name,GvNAME(gv),GvNAMELEN(gv));
685 }
686 else {
53c1dcc0
AL
687 U32 unused;
688 CV * const cv = find_runcv(&unused);
689 SV *sv;
690 AV *av;
691
29489e7c 692 if (!cv || !CvPADLIST(cv))
1b6737cc 693 return Nullsv;
29489e7c
DM
694 av = (AV*)(*av_fetch(CvPADLIST(cv), 0, FALSE));
695 sv = *av_fetch(av, targ, FALSE);
696 /* SvLEN in a pad name is not to be trusted */
f9926b10 697 sv_setpv(name, SvPV_nolen_const(sv));
29489e7c
DM
698 }
699
700 if (subscript_type == FUV_SUBSCRIPT_HASH) {
1b6737cc 701 SV * const sv = NEWSV(0,0);
29489e7c 702 *SvPVX(name) = '$';
29489e7c 703 Perl_sv_catpvf(aTHX_ name, "{%s}",
3f7c398e 704 pv_display(sv,SvPVX_const(keyname), SvCUR(keyname), 0, 32));
29489e7c
DM
705 SvREFCNT_dec(sv);
706 }
707 else if (subscript_type == FUV_SUBSCRIPT_ARRAY) {
708 *SvPVX(name) = '$';
265a12b8 709 Perl_sv_catpvf(aTHX_ name, "[%"IVdf"]", (IV)aindex);
29489e7c
DM
710 }
711 else if (subscript_type == FUV_SUBSCRIPT_WITHIN)
712 sv_insert(name, 0, 0, "within ", 7);
713
714 return name;
715}
716
717
718/*
719=for apidoc find_uninit_var
720
721Find the name of the undefined variable (if any) that caused the operator o
722to issue a "Use of uninitialized value" warning.
723If match is true, only return a name if it's value matches uninit_sv.
724So roughly speaking, if a unary operator (such as OP_COS) generates a
725warning, then following the direct child of the op may yield an
726OP_PADSV or OP_GV that gives the name of the undefined variable. On the
727other hand, with OP_ADD there are two branches to follow, so we only print
728the variable name if we get an exact match.
729
730The name is returned as a mortal SV.
731
732Assumes that PL_op is the op that originally triggered the error, and that
733PL_comppad/PL_curpad points to the currently executing pad.
734
735=cut
736*/
737
738STATIC SV *
739S_find_uninit_var(pTHX_ OP* obase, SV* uninit_sv, bool match)
740{
27da23d5 741 dVAR;
29489e7c
DM
742 SV *sv;
743 AV *av;
29489e7c
DM
744 GV *gv;
745 OP *o, *o2, *kid;
746
747 if (!obase || (match && (!uninit_sv || uninit_sv == &PL_sv_undef ||
748 uninit_sv == &PL_sv_placeholder)))
749 return Nullsv;
750
751 switch (obase->op_type) {
752
753 case OP_RV2AV:
754 case OP_RV2HV:
755 case OP_PADAV:
756 case OP_PADHV:
757 {
f54cb97a
AL
758 const bool pad = (obase->op_type == OP_PADAV || obase->op_type == OP_PADHV);
759 const bool hash = (obase->op_type == OP_PADHV || obase->op_type == OP_RV2HV);
112dcc46
RGS
760 I32 index = 0;
761 SV *keysv = Nullsv;
29489e7c
DM
762 int subscript_type = FUV_SUBSCRIPT_WITHIN;
763
764 if (pad) { /* @lex, %lex */
765 sv = PAD_SVl(obase->op_targ);
766 gv = Nullgv;
767 }
768 else {
769 if (cUNOPx(obase)->op_first->op_type == OP_GV) {
770 /* @global, %global */
771 gv = cGVOPx_gv(cUNOPx(obase)->op_first);
772 if (!gv)
773 break;
774 sv = hash ? (SV*)GvHV(gv): (SV*)GvAV(gv);
775 }
776 else /* @{expr}, %{expr} */
777 return find_uninit_var(cUNOPx(obase)->op_first,
778 uninit_sv, match);
779 }
780
781 /* attempt to find a match within the aggregate */
782 if (hash) {
783 keysv = S_find_hash_subscript(aTHX_ (HV*)sv, uninit_sv);
784 if (keysv)
785 subscript_type = FUV_SUBSCRIPT_HASH;
786 }
787 else {
788 index = S_find_array_subscript(aTHX_ (AV*)sv, uninit_sv);
789 if (index >= 0)
790 subscript_type = FUV_SUBSCRIPT_ARRAY;
791 }
792
793 if (match && subscript_type == FUV_SUBSCRIPT_WITHIN)
794 break;
795
1b6737cc 796 return varname(gv, hash ? "%" : "@", obase->op_targ,
29489e7c
DM
797 keysv, index, subscript_type);
798 }
799
800 case OP_PADSV:
801 if (match && PAD_SVl(obase->op_targ) != uninit_sv)
802 break;
1b6737cc 803 return varname(Nullgv, "$", obase->op_targ,
29489e7c
DM
804 Nullsv, 0, FUV_SUBSCRIPT_NONE);
805
806 case OP_GVSV:
807 gv = cGVOPx_gv(obase);
808 if (!gv || (match && GvSV(gv) != uninit_sv))
809 break;
1b6737cc 810 return varname(gv, "$", 0, Nullsv, 0, FUV_SUBSCRIPT_NONE);
29489e7c
DM
811
812 case OP_AELEMFAST:
813 if (obase->op_flags & OPf_SPECIAL) { /* lexical array */
814 if (match) {
1b6737cc 815 SV **svp;
29489e7c
DM
816 av = (AV*)PAD_SV(obase->op_targ);
817 if (!av || SvRMAGICAL(av))
818 break;
819 svp = av_fetch(av, (I32)obase->op_private, FALSE);
820 if (!svp || *svp != uninit_sv)
821 break;
822 }
1b6737cc 823 return varname(Nullgv, "$", obase->op_targ,
29489e7c
DM
824 Nullsv, (I32)obase->op_private, FUV_SUBSCRIPT_ARRAY);
825 }
826 else {
827 gv = cGVOPx_gv(obase);
828 if (!gv)
829 break;
830 if (match) {
1b6737cc 831 SV **svp;
29489e7c
DM
832 av = GvAV(gv);
833 if (!av || SvRMAGICAL(av))
834 break;
835 svp = av_fetch(av, (I32)obase->op_private, FALSE);
836 if (!svp || *svp != uninit_sv)
837 break;
838 }
1b6737cc 839 return varname(gv, "$", 0,
29489e7c
DM
840 Nullsv, (I32)obase->op_private, FUV_SUBSCRIPT_ARRAY);
841 }
842 break;
843
844 case OP_EXISTS:
845 o = cUNOPx(obase)->op_first;
846 if (!o || o->op_type != OP_NULL ||
847 ! (o->op_targ == OP_AELEM || o->op_targ == OP_HELEM))
848 break;
849 return find_uninit_var(cBINOPo->op_last, uninit_sv, match);
850
851 case OP_AELEM:
852 case OP_HELEM:
853 if (PL_op == obase)
854 /* $a[uninit_expr] or $h{uninit_expr} */
855 return find_uninit_var(cBINOPx(obase)->op_last, uninit_sv, match);
856
857 gv = Nullgv;
858 o = cBINOPx(obase)->op_first;
859 kid = cBINOPx(obase)->op_last;
860
861 /* get the av or hv, and optionally the gv */
862 sv = Nullsv;
863 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV) {
864 sv = PAD_SV(o->op_targ);
865 }
866 else if ((o->op_type == OP_RV2AV || o->op_type == OP_RV2HV)
867 && cUNOPo->op_first->op_type == OP_GV)
868 {
869 gv = cGVOPx_gv(cUNOPo->op_first);
870 if (!gv)
871 break;
872 sv = o->op_type == OP_RV2HV ? (SV*)GvHV(gv) : (SV*)GvAV(gv);
873 }
874 if (!sv)
875 break;
876
877 if (kid && kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid))) {
878 /* index is constant */
879 if (match) {
880 if (SvMAGICAL(sv))
881 break;
882 if (obase->op_type == OP_HELEM) {
883 HE* he = hv_fetch_ent((HV*)sv, cSVOPx_sv(kid), 0, 0);
884 if (!he || HeVAL(he) != uninit_sv)
885 break;
886 }
887 else {
1b6737cc 888 SV ** const svp = av_fetch((AV*)sv, SvIV(cSVOPx_sv(kid)), FALSE);
29489e7c
DM
889 if (!svp || *svp != uninit_sv)
890 break;
891 }
892 }
893 if (obase->op_type == OP_HELEM)
1b6737cc 894 return varname(gv, "%", o->op_targ,
29489e7c
DM
895 cSVOPx_sv(kid), 0, FUV_SUBSCRIPT_HASH);
896 else
1b6737cc 897 return varname(gv, "@", o->op_targ, Nullsv,
29489e7c
DM
898 SvIV(cSVOPx_sv(kid)), FUV_SUBSCRIPT_ARRAY);
899 ;
900 }
901 else {
902 /* index is an expression;
903 * attempt to find a match within the aggregate */
904 if (obase->op_type == OP_HELEM) {
53c1dcc0 905 SV * const keysv = S_find_hash_subscript(aTHX_ (HV*)sv, uninit_sv);
29489e7c 906 if (keysv)
1b6737cc 907 return varname(gv, "%", o->op_targ,
29489e7c
DM
908 keysv, 0, FUV_SUBSCRIPT_HASH);
909 }
910 else {
f54cb97a 911 const I32 index = S_find_array_subscript(aTHX_ (AV*)sv, uninit_sv);
29489e7c 912 if (index >= 0)
1b6737cc 913 return varname(gv, "@", o->op_targ,
29489e7c
DM
914 Nullsv, index, FUV_SUBSCRIPT_ARRAY);
915 }
916 if (match)
917 break;
1b6737cc 918 return varname(gv,
29489e7c
DM
919 (o->op_type == OP_PADAV || o->op_type == OP_RV2AV)
920 ? "@" : "%",
921 o->op_targ, Nullsv, 0, FUV_SUBSCRIPT_WITHIN);
922 }
923
924 break;
925
926 case OP_AASSIGN:
927 /* only examine RHS */
928 return find_uninit_var(cBINOPx(obase)->op_first, uninit_sv, match);
929
930 case OP_OPEN:
931 o = cUNOPx(obase)->op_first;
932 if (o->op_type == OP_PUSHMARK)
933 o = o->op_sibling;
934
935 if (!o->op_sibling) {
936 /* one-arg version of open is highly magical */
937
938 if (o->op_type == OP_GV) { /* open FOO; */
939 gv = cGVOPx_gv(o);
940 if (match && GvSV(gv) != uninit_sv)
941 break;
1b6737cc 942 return varname(gv, "$", 0,
29489e7c
DM
943 Nullsv, 0, FUV_SUBSCRIPT_NONE);
944 }
945 /* other possibilities not handled are:
946 * open $x; or open my $x; should return '${*$x}'
947 * open expr; should return '$'.expr ideally
948 */
949 break;
950 }
951 goto do_op;
952
953 /* ops where $_ may be an implicit arg */
954 case OP_TRANS:
955 case OP_SUBST:
956 case OP_MATCH:
957 if ( !(obase->op_flags & OPf_STACKED)) {
958 if (uninit_sv == ((obase->op_private & OPpTARGET_MY)
959 ? PAD_SVl(obase->op_targ)
960 : DEFSV))
961 {
962 sv = sv_newmortal();
616d8c9c 963 sv_setpvn(sv, "$_", 2);
29489e7c
DM
964 return sv;
965 }
966 }
967 goto do_op;
968
969 case OP_PRTF:
970 case OP_PRINT:
971 /* skip filehandle as it can't produce 'undef' warning */
972 o = cUNOPx(obase)->op_first;
973 if ((obase->op_flags & OPf_STACKED) && o->op_type == OP_PUSHMARK)
974 o = o->op_sibling->op_sibling;
975 goto do_op2;
976
977
e21bd382 978 case OP_RV2SV:
29489e7c
DM
979 case OP_CUSTOM:
980 case OP_ENTERSUB:
981 match = 1; /* XS or custom code could trigger random warnings */
982 goto do_op;
983
984 case OP_SCHOMP:
985 case OP_CHOMP:
986 if (SvROK(PL_rs) && uninit_sv == SvRV(PL_rs))
987 return sv_2mortal(newSVpv("${$/}", 0));
988 /* FALL THROUGH */
989
990 default:
991 do_op:
992 if (!(obase->op_flags & OPf_KIDS))
993 break;
994 o = cUNOPx(obase)->op_first;
995
996 do_op2:
997 if (!o)
998 break;
999
1000 /* if all except one arg are constant, or have no side-effects,
1001 * or are optimized away, then it's unambiguous */
1002 o2 = Nullop;
1003 for (kid=o; kid; kid = kid->op_sibling) {
1004 if (kid &&
1005 ( (kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid)))
1006 || (kid->op_type == OP_NULL && ! (kid->op_flags & OPf_KIDS))
1007 || (kid->op_type == OP_PUSHMARK)
1008 )
1009 )
1010 continue;
1011 if (o2) { /* more than one found */
1012 o2 = Nullop;
1013 break;
1014 }
1015 o2 = kid;
1016 }
1017 if (o2)
1018 return find_uninit_var(o2, uninit_sv, match);
1019
1020 /* scan all args */
1021 while (o) {
1022 sv = find_uninit_var(o, uninit_sv, 1);
1023 if (sv)
1024 return sv;
1025 o = o->op_sibling;
1026 }
1027 break;
1028 }
1029 return Nullsv;
1030}
1031
1032
645c22ef
DM
1033/*
1034=for apidoc report_uninit
1035
1036Print appropriate "Use of uninitialized variable" warning
1037
1038=cut
1039*/
1040
1d7c1841 1041void
29489e7c
DM
1042Perl_report_uninit(pTHX_ SV* uninit_sv)
1043{
1044 if (PL_op) {
112dcc46 1045 SV* varname = Nullsv;
29489e7c
DM
1046 if (uninit_sv) {
1047 varname = find_uninit_var(PL_op, uninit_sv,0);
1048 if (varname)
1049 sv_insert(varname, 0, 0, " ", 1);
1050 }
9014280d 1051 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
93524f2b 1052 varname ? SvPV_nolen_const(varname) : "",
29489e7c
DM
1053 " in ", OP_DESC(PL_op));
1054 }
1d7c1841 1055 else
29489e7c
DM
1056 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
1057 "", "", "");
1d7c1841
GS
1058}
1059
de042e1d 1060STATIC void *
e3bbdc67 1061S_more_bodies (pTHX_ void **arena_root, void **root, size_t size)
cac9b346 1062{
e3bbdc67
NC
1063 char *start;
1064 const char *end;
53c1dcc0 1065 const size_t count = PERL_ARENA_SIZE/size;
e3bbdc67
NC
1066 New(0, start, count*size, char);
1067 *((void **) start) = *arena_root;
1068 *arena_root = (void *)start;
cac9b346 1069
e3bbdc67 1070 end = start + (count-1) * size;
cac9b346 1071
e3bbdc67
NC
1072 /* The initial slot is used to link the arenas together, so it isn't to be
1073 linked into the list of ready-to-use bodies. */
cac9b346 1074
e3bbdc67 1075 start += size;
cac9b346 1076
e3bbdc67 1077 *root = (void *)start;
cac9b346 1078
e3bbdc67 1079 while (start < end) {
53c1dcc0 1080 char * const next = start + size;
e3bbdc67
NC
1081 *(void**) start = (void *)next;
1082 start = next;
cac9b346 1083 }
e3bbdc67 1084 *(void **)start = 0;
de042e1d
NC
1085
1086 return *root;
cac9b346
NC
1087}
1088
aeb18a1e 1089/* grab a new thing from the free list, allocating more if necessary */
645c22ef 1090
aeb18a1e 1091STATIC void *
dd690478 1092S_new_body(pTHX_ void **arena_root, void **root, size_t size)
932e9ff9 1093{
aeb18a1e 1094 void *xpv;
932e9ff9 1095 LOCK_SV_MUTEX;
aeb18a1e
NC
1096 xpv = *root ? *root : S_more_bodies(aTHX_ arena_root, root, size);
1097 *root = *(void**)xpv;
932e9ff9 1098 UNLOCK_SV_MUTEX;
dd690478 1099 return xpv;
932e9ff9
VB
1100}
1101
aeb18a1e 1102/* return a thing to the free list */
645c22ef 1103
cb4415b8
NC
1104#define del_body(thing, root) \
1105 STMT_START { \
1106 LOCK_SV_MUTEX; \
1107 *(void **)thing = *root; \
1108 *root = (void*)thing; \
1109 UNLOCK_SV_MUTEX; \
1110 } STMT_END
932e9ff9 1111
aeb18a1e
NC
1112/* Conventionally we simply malloc() a big block of memory, then divide it
1113 up into lots of the thing that we're allocating.
645c22ef 1114
aeb18a1e
NC
1115 This macro will expand to call to S_new_body. So for XPVBM (with ithreads),
1116 it would become
932e9ff9 1117
aeb18a1e
NC
1118 S_new_body(my_perl, (void**)&(my_perl->Ixpvbm_arenaroot),
1119 (void**)&(my_perl->Ixpvbm_root), sizeof(XPVBM), 0)
1120*/
645c22ef 1121
aeb18a1e
NC
1122#define new_body(TYPE,lctype) \
1123 S_new_body(aTHX_ (void**)&PL_ ## lctype ## _arenaroot, \
1124 (void**)&PL_ ## lctype ## _root, \
dd690478
NC
1125 sizeof(TYPE))
1126
cb4415b8
NC
1127#define del_body_type(p,TYPE,lctype) \
1128 del_body((void*)p, (void**)&PL_ ## lctype ## _root)
aeb18a1e
NC
1129
1130/* But for some types, we cheat. The type starts with some members that are
1131 never accessed. So we allocate the substructure, starting at the first used
1132 member, then adjust the pointer back in memory by the size of the bit not
1133 allocated, so it's as if we allocated the full structure.
1134 (But things will all go boom if you write to the part that is "not there",
1135 because you'll be overwriting the last members of the preceding structure
1136 in memory.)
1137
1138 We calculate the correction using the STRUCT_OFFSET macro. For example, if
1139 xpv_allocated is the same structure as XPV then the two OFFSETs sum to zero,
1140 and the pointer is unchanged. If the allocated structure is smaller (no
1141 initial NV actually allocated) then the net effect is to subtract the size
1142 of the NV from the pointer, to return a new pointer as if an initial NV were
1143 actually allocated.
1144
1145 This is the same trick as was used for NV and IV bodies. Ironically it
1146 doesn't need to be used for NV bodies any more, because NV is now at the
1147 start of the structure. IV bodies don't need it either, because they are
1148 no longer allocated. */
1149
1150#define new_body_allocated(TYPE,lctype,member) \
dd690478
NC
1151 (void*)((char*)S_new_body(aTHX_ (void**)&PL_ ## lctype ## _arenaroot, \
1152 (void**)&PL_ ## lctype ## _root, \
1153 sizeof(lctype ## _allocated)) - \
1154 STRUCT_OFFSET(TYPE, member) \
1155 + STRUCT_OFFSET(lctype ## _allocated, member))
aeb18a1e
NC
1156
1157
aeb18a1e 1158#define del_body_allocated(p,TYPE,lctype,member) \
cb4415b8
NC
1159 del_body((void*)((char*)p + STRUCT_OFFSET(TYPE, member) \
1160 - STRUCT_OFFSET(lctype ## _allocated, member)), \
1161 (void**)&PL_ ## lctype ## _root)
932e9ff9 1162
7bab3ede
MB
1163#define my_safemalloc(s) (void*)safemalloc(s)
1164#define my_safefree(p) safefree((char*)p)
463ee0b2 1165
d33b2eba 1166#ifdef PURIFY
463ee0b2 1167
d33b2eba
GS
1168#define new_XNV() my_safemalloc(sizeof(XPVNV))
1169#define del_XNV(p) my_safefree(p)
463ee0b2 1170
d33b2eba
GS
1171#define new_XPV() my_safemalloc(sizeof(XPV))
1172#define del_XPV(p) my_safefree(p)
9b94d1dd 1173
d33b2eba
GS
1174#define new_XPVIV() my_safemalloc(sizeof(XPVIV))
1175#define del_XPVIV(p) my_safefree(p)
932e9ff9 1176
d33b2eba
GS
1177#define new_XPVNV() my_safemalloc(sizeof(XPVNV))
1178#define del_XPVNV(p) my_safefree(p)
932e9ff9 1179
d33b2eba
GS
1180#define new_XPVCV() my_safemalloc(sizeof(XPVCV))
1181#define del_XPVCV(p) my_safefree(p)
932e9ff9 1182
d33b2eba
GS
1183#define new_XPVAV() my_safemalloc(sizeof(XPVAV))
1184#define del_XPVAV(p) my_safefree(p)
1185
1186#define new_XPVHV() my_safemalloc(sizeof(XPVHV))
1187#define del_XPVHV(p) my_safefree(p)
1c846c1f 1188
d33b2eba
GS
1189#define new_XPVMG() my_safemalloc(sizeof(XPVMG))
1190#define del_XPVMG(p) my_safefree(p)
1191
727879eb
NC
1192#define new_XPVGV() my_safemalloc(sizeof(XPVGV))
1193#define del_XPVGV(p) my_safefree(p)
1194
d33b2eba
GS
1195#define new_XPVLV() my_safemalloc(sizeof(XPVLV))
1196#define del_XPVLV(p) my_safefree(p)
1197
1198#define new_XPVBM() my_safemalloc(sizeof(XPVBM))
1199#define del_XPVBM(p) my_safefree(p)
1200
1201#else /* !PURIFY */
1202
aeb18a1e 1203#define new_XNV() new_body(NV, xnv)
cb4415b8 1204#define del_XNV(p) del_body_type(p, NV, xnv)
9b94d1dd 1205
aeb18a1e
NC
1206#define new_XPV() new_body_allocated(XPV, xpv, xpv_cur)
1207#define del_XPV(p) del_body_allocated(p, XPV, xpv, xpv_cur)
d33b2eba 1208
aeb18a1e
NC
1209#define new_XPVIV() new_body_allocated(XPVIV, xpviv, xpv_cur)
1210#define del_XPVIV(p) del_body_allocated(p, XPVIV, xpviv, xpv_cur)
d33b2eba 1211
aeb18a1e 1212#define new_XPVNV() new_body(XPVNV, xpvnv)
cb4415b8 1213#define del_XPVNV(p) del_body_type(p, XPVNV, xpvnv)
d33b2eba 1214
aeb18a1e 1215#define new_XPVCV() new_body(XPVCV, xpvcv)
cb4415b8 1216#define del_XPVCV(p) del_body_type(p, XPVCV, xpvcv)
d33b2eba 1217
aeb18a1e
NC
1218#define new_XPVAV() new_body_allocated(XPVAV, xpvav, xav_fill)
1219#define del_XPVAV(p) del_body_allocated(p, XPVAV, xpvav, xav_fill)
d33b2eba 1220
aeb18a1e
NC
1221#define new_XPVHV() new_body_allocated(XPVHV, xpvhv, xhv_fill)
1222#define del_XPVHV(p) del_body_allocated(p, XPVHV, xpvhv, xhv_fill)
1c846c1f 1223
aeb18a1e 1224#define new_XPVMG() new_body(XPVMG, xpvmg)
cb4415b8 1225#define del_XPVMG(p) del_body_type(p, XPVMG, xpvmg)
d33b2eba 1226
aeb18a1e 1227#define new_XPVGV() new_body(XPVGV, xpvgv)
cb4415b8 1228#define del_XPVGV(p) del_body_type(p, XPVGV, xpvgv)
727879eb 1229
aeb18a1e 1230#define new_XPVLV() new_body(XPVLV, xpvlv)
cb4415b8 1231#define del_XPVLV(p) del_body_type(p, XPVLV, xpvlv)
d33b2eba 1232
aeb18a1e 1233#define new_XPVBM() new_body(XPVBM, xpvbm)
cb4415b8 1234#define del_XPVBM(p) del_body_type(p, XPVBM, xpvbm)
d33b2eba
GS
1235
1236#endif /* PURIFY */
9b94d1dd 1237
d33b2eba
GS
1238#define new_XPVFM() my_safemalloc(sizeof(XPVFM))
1239#define del_XPVFM(p) my_safefree(p)
1c846c1f 1240
d33b2eba
GS
1241#define new_XPVIO() my_safemalloc(sizeof(XPVIO))
1242#define del_XPVIO(p) my_safefree(p)
8990e307 1243
954c1994
GS
1244/*
1245=for apidoc sv_upgrade
1246
ff276b08 1247Upgrade an SV to a more complex form. Generally adds a new body type to the
645c22ef 1248SV, then copies across as much information as possible from the old body.
ff276b08 1249You generally want to use the C<SvUPGRADE> macro wrapper. See also C<svtype>.
954c1994
GS
1250
1251=cut
1252*/
1253
63f97190 1254void
864dbfa3 1255Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt)
79072805 1256{
9a085840 1257 void** old_body_arena;
878cc751 1258 size_t old_body_offset;
4cbc76b1 1259 size_t old_body_length; /* Well, the length to copy. */
878cc751 1260 void* old_body;
16b305e3
NC
1261#ifndef NV_ZERO_IS_ALLBITS_ZERO
1262 /* If NV 0.0 is store as all bits 0 then Zero() already creates a correct
1263 0.0 for us. */
4cbc76b1 1264 bool zero_nv = TRUE;
16b305e3 1265#endif
403d36eb
NC
1266 void* new_body;
1267 size_t new_body_length;
1268 size_t new_body_offset;
1269 void** new_body_arena;
1270 void** new_body_arenaroot;
53c1dcc0 1271 const U32 old_type = SvTYPE(sv);
79072805 1272
765f542d
NC
1273 if (mt != SVt_PV && SvIsCOW(sv)) {
1274 sv_force_normal_flags(sv, 0);
f130fd45
NIS
1275 }
1276
79072805 1277 if (SvTYPE(sv) == mt)
63f97190 1278 return;
79072805 1279
f5282e15 1280 if (SvTYPE(sv) > mt)
921edb34
RGS
1281 Perl_croak(aTHX_ "sv_upgrade from type %d down to type %d",
1282 (int)SvTYPE(sv), (int)mt);
f5282e15 1283
d2e56290 1284
878cc751
NC
1285 old_body = SvANY(sv);
1286 old_body_arena = 0;
1287 old_body_offset = 0;
4cbc76b1 1288 old_body_length = 0;
403d36eb
NC
1289 new_body_offset = 0;
1290 new_body_length = ~0;
1291
1292 /* Copying structures onto other structures that have been neatly zeroed
1293 has a subtle gotcha. Consider XPVMG
1294
1295 +------+------+------+------+------+-------+-------+
1296 | NV | CUR | LEN | IV | MAGIC | STASH |
1297 +------+------+------+------+------+-------+-------+
1298 0 4 8 12 16 20 24 28
1299
1300 where NVs are aligned to 8 bytes, so that sizeof that structure is
1301 actually 32 bytes long, with 4 bytes of padding at the end:
1302
1303 +------+------+------+------+------+-------+-------+------+
1304 | NV | CUR | LEN | IV | MAGIC | STASH | ??? |
1305 +------+------+------+------+------+-------+-------+------+
1306 0 4 8 12 16 20 24 28 32
1307
1308 so what happens if you allocate memory for this structure:
1309
1310 +------+------+------+------+------+-------+-------+------+------+...
1311 | NV | CUR | LEN | IV | MAGIC | STASH | GP | NAME |
1312 +------+------+------+------+------+-------+-------+------+------+...
1313 0 4 8 12 16 20 24 28 32 36
1314
1315 zero it, then copy sizeof(XPVMG) bytes on top of it? Not quite what you
1316 expect, because you copy the area marked ??? onto GP. Now, ??? may have
1317 started out as zero once, but it's quite possible that it isn't. So now,
1318 rather than a nicely zeroed GP, you have it pointing somewhere random.
1319 Bugs ensue.
1320
1321 (In fact, GP ends up pointing at a previous GP structure, because the
1322 principle cause of the padding in XPVMG getting garbage is a copy of
1323 sizeof(XPVMG) bytes from a XPVGV structure in sv_unglob)
1324
1325 So we are careful and work out the size of used parts of all the
1326 structures. */
878cc751 1327
79072805
LW
1328 switch (SvTYPE(sv)) {
1329 case SVt_NULL:
79072805 1330 break;
79072805 1331 case SVt_IV:
ed6116ce 1332 if (mt == SVt_NV)
463ee0b2 1333 mt = SVt_PVNV;
ed6116ce
LW
1334 else if (mt < SVt_PVIV)
1335 mt = SVt_PVIV;
4cbc76b1
NC
1336 old_body_offset = STRUCT_OFFSET(XPVIV, xiv_iv);
1337 old_body_length = sizeof(IV);
79072805
LW
1338 break;
1339 case SVt_NV:
9a085840 1340 old_body_arena = (void **) &PL_xnv_root;
4cbc76b1 1341 old_body_length = sizeof(NV);
16b305e3 1342#ifndef NV_ZERO_IS_ALLBITS_ZERO
4cbc76b1 1343 zero_nv = FALSE;
16b305e3 1344#endif
ed6116ce 1345 if (mt < SVt_PVNV)
79072805
LW
1346 mt = SVt_PVNV;
1347 break;
ed6116ce 1348 case SVt_RV:
ed6116ce 1349 break;
79072805 1350 case SVt_PV:
9a085840 1351 old_body_arena = (void **) &PL_xpv_root;
878cc751
NC
1352 old_body_offset = STRUCT_OFFSET(XPV, xpv_cur)
1353 - STRUCT_OFFSET(xpv_allocated, xpv_cur);
403d36eb
NC
1354 old_body_length = STRUCT_OFFSET(XPV, xpv_len)
1355 + sizeof (((XPV*)SvANY(sv))->xpv_len)
1356 - old_body_offset;
748a9306
LW
1357 if (mt <= SVt_IV)
1358 mt = SVt_PVIV;
1359 else if (mt == SVt_NV)
1360 mt = SVt_PVNV;
79072805
LW
1361 break;
1362 case SVt_PVIV:
9a085840 1363 old_body_arena = (void **) &PL_xpviv_root;
878cc751
NC
1364 old_body_offset = STRUCT_OFFSET(XPVIV, xpv_cur)
1365 - STRUCT_OFFSET(xpviv_allocated, xpv_cur);
403d36eb
NC
1366 old_body_length = STRUCT_OFFSET(XPVIV, xiv_u)
1367 + sizeof (((XPVIV*)SvANY(sv))->xiv_u)
1368 - old_body_offset;
79072805
LW
1369 break;
1370 case SVt_PVNV:
9a085840 1371 old_body_arena = (void **) &PL_xpvnv_root;
403d36eb
NC
1372 old_body_length = STRUCT_OFFSET(XPVNV, xiv_u)
1373 + sizeof (((XPVNV*)SvANY(sv))->xiv_u);
16b305e3 1374#ifndef NV_ZERO_IS_ALLBITS_ZERO
4cbc76b1 1375 zero_nv = FALSE;
16b305e3 1376#endif
79072805
LW
1377 break;
1378 case SVt_PVMG:
0ec50a73
NC
1379 /* Because the XPVMG of PL_mess_sv isn't allocated from the arena,
1380 there's no way that it can be safely upgraded, because perl.c
1381 expects to Safefree(SvANY(PL_mess_sv)) */
1382 assert(sv != PL_mess_sv);
bce8f412
NC
1383 /* This flag bit is used to mean other things in other scalar types.
1384 Given that it only has meaning inside the pad, it shouldn't be set
1385 on anything that can get upgraded. */
1386 assert((SvFLAGS(sv) & SVpad_TYPED) == 0);
9a085840 1387 old_body_arena = (void **) &PL_xpvmg_root;
403d36eb
NC
1388 old_body_length = STRUCT_OFFSET(XPVMG, xmg_stash)
1389 + sizeof (((XPVMG*)SvANY(sv))->xmg_stash);
16b305e3 1390#ifndef NV_ZERO_IS_ALLBITS_ZERO
4cbc76b1 1391 zero_nv = FALSE;
16b305e3 1392#endif
79072805
LW
1393 break;
1394 default:
cea2e8a9 1395 Perl_croak(aTHX_ "Can't upgrade that kind of scalar");
79072805
LW
1396 }
1397
ffb05e06
NC
1398 SvFLAGS(sv) &= ~SVTYPEMASK;
1399 SvFLAGS(sv) |= mt;
1400
79072805
LW
1401 switch (mt) {
1402 case SVt_NULL:
cea2e8a9 1403 Perl_croak(aTHX_ "Can't upgrade to undef");
79072805 1404 case SVt_IV:
4cbc76b1 1405 assert(old_type == SVt_NULL);
339049b0 1406 SvANY(sv) = (XPVIV*)((char*)&(sv->sv_u.svu_iv) - STRUCT_OFFSET(XPVIV, xiv_iv));
403d36eb 1407 SvIV_set(sv, 0);
85274cbc 1408 return;
79072805 1409 case SVt_NV:
4cbc76b1 1410 assert(old_type == SVt_NULL);
79072805 1411 SvANY(sv) = new_XNV();
403d36eb 1412 SvNV_set(sv, 0);
85274cbc 1413 return;
ed6116ce 1414 case SVt_RV:
4cbc76b1 1415 assert(old_type == SVt_NULL);
339049b0 1416 SvANY(sv) = &sv->sv_u.svu_rv;
403d36eb 1417 SvRV_set(sv, 0);
85274cbc 1418 return;
79072805
LW
1419 case SVt_PVHV:
1420 SvANY(sv) = new_XPVHV();
463ee0b2
LW
1421 HvFILL(sv) = 0;
1422 HvMAX(sv) = 0;
8aacddc1 1423 HvTOTALKEYS(sv) = 0;
bd4b1eb5 1424
2068cd4d
NC
1425 goto hv_av_common;
1426
1427 case SVt_PVAV:
1428 SvANY(sv) = new_XPVAV();
1429 AvMAX(sv) = -1;
1430 AvFILLp(sv) = -1;
1431 AvALLOC(sv) = 0;
1432 AvREAL_only(sv);
1433
1434 hv_av_common:
1435 /* SVt_NULL isn't the only thing upgraded to AV or HV.
1436 The target created by newSVrv also is, and it can have magic.
1437 However, it never has SvPVX set.
1438 */
1439 if (old_type >= SVt_RV) {
1440 assert(SvPVX_const(sv) == 0);
8bd4d4c5 1441 }
2068cd4d
NC
1442
1443 /* Could put this in the else clause below, as PVMG must have SvPVX
1444 0 already (the assertion above) */
bd4b1eb5 1445 SvPV_set(sv, (char*)0);
2068cd4d
NC
1446
1447 if (old_type >= SVt_PVMG) {
1448 SvMAGIC_set(sv, ((XPVMG*)old_body)->xmg_magic);
1449 SvSTASH_set(sv, ((XPVMG*)old_body)->xmg_stash);
1450 } else {
1451 SvMAGIC_set(sv, 0);
1452 SvSTASH_set(sv, 0);
1453 }
79072805 1454 break;
bd4b1eb5
NC
1455
1456 case SVt_PVIO:
403d36eb
NC
1457 new_body = new_XPVIO();
1458 new_body_length = sizeof(XPVIO);
1459 goto zero;
bd4b1eb5 1460 case SVt_PVFM:
403d36eb
NC
1461 new_body = new_XPVFM();
1462 new_body_length = sizeof(XPVFM);
1463 goto zero;
1464
bd4b1eb5 1465 case SVt_PVBM:
403d36eb
NC
1466 new_body_length = sizeof(XPVBM);
1467 new_body_arena = (void **) &PL_xpvbm_root;
1468 new_body_arenaroot = (void **) &PL_xpvbm_arenaroot;
1469 goto new_body;
bd4b1eb5 1470 case SVt_PVGV:
403d36eb
NC
1471 new_body_length = sizeof(XPVGV);
1472 new_body_arena = (void **) &PL_xpvgv_root;
1473 new_body_arenaroot = (void **) &PL_xpvgv_arenaroot;
1474 goto new_body;
79072805 1475 case SVt_PVCV:
403d36eb
NC
1476 new_body_length = sizeof(XPVCV);
1477 new_body_arena = (void **) &PL_xpvcv_root;
1478 new_body_arenaroot = (void **) &PL_xpvcv_arenaroot;
1479 goto new_body;
bd4b1eb5 1480 case SVt_PVLV:
403d36eb
NC
1481 new_body_length = sizeof(XPVLV);
1482 new_body_arena = (void **) &PL_xpvlv_root;
1483 new_body_arenaroot = (void **) &PL_xpvlv_arenaroot;
1484 goto new_body;
1485 case SVt_PVMG:
1486 new_body_length = sizeof(XPVMG);
1487 new_body_arena = (void **) &PL_xpvmg_root;
1488 new_body_arenaroot = (void **) &PL_xpvmg_arenaroot;
1489 goto new_body;
1490 case SVt_PVNV:
1491 new_body_length = sizeof(XPVNV);
1492 new_body_arena = (void **) &PL_xpvnv_root;
1493 new_body_arenaroot = (void **) &PL_xpvnv_arenaroot;
1494 goto new_body;
1495 case SVt_PVIV:
1496 new_body_offset = STRUCT_OFFSET(XPVIV, xpv_cur)
1497 - STRUCT_OFFSET(xpviv_allocated, xpv_cur);
1498 new_body_length = sizeof(XPVIV) - new_body_offset;
1499 new_body_arena = (void **) &PL_xpviv_root;
1500 new_body_arenaroot = (void **) &PL_xpviv_arenaroot;
1501 /* XXX Is this still needed? Was it ever needed? Surely as there is
1502 no route from NV to PVIV, NOK can never be true */
1503 if (SvNIOK(sv))
1504 (void)SvIOK_on(sv);
1505 SvNOK_off(sv);
1506 goto new_body_no_NV;
1507 case SVt_PV:
1508 new_body_offset = STRUCT_OFFSET(XPV, xpv_cur)
1509 - STRUCT_OFFSET(xpv_allocated, xpv_cur);
1510 new_body_length = sizeof(XPV) - new_body_offset;
1511 new_body_arena = (void **) &PL_xpv_root;
1512 new_body_arenaroot = (void **) &PL_xpv_arenaroot;
1513 new_body_no_NV:
1514 /* PV and PVIV don't have an NV slot. */
16b305e3 1515#ifndef NV_ZERO_IS_ALLBITS_ZERO
403d36eb 1516 zero_nv = FALSE;
16b305e3 1517#endif
403d36eb 1518
16b305e3
NC
1519 new_body:
1520 assert(new_body_length);
403d36eb 1521#ifndef PURIFY
16b305e3
NC
1522 /* This points to the start of the allocated area. */
1523 new_body = S_new_body(aTHX_ new_body_arenaroot, new_body_arena,
1524 new_body_length);
403d36eb 1525#else
16b305e3
NC
1526 /* We always allocated the full length item with PURIFY */
1527 new_body_length += new_body_offset;
1528 new_body_offset = 0;
1529 new_body = my_safemalloc(new_body_length);
403d36eb
NC
1530
1531#endif
16b305e3
NC
1532 zero:
1533 Zero(new_body, new_body_length, char);
1534 new_body = ((char *)new_body) - new_body_offset;
1535 SvANY(sv) = new_body;
1536
1537 if (old_body_length) {
1538 Copy((char *)old_body + old_body_offset,
1539 (char *)new_body + old_body_offset,
1540 old_body_length, char);
1541 }
403d36eb 1542
16b305e3
NC
1543#ifndef NV_ZERO_IS_ALLBITS_ZERO
1544 if (zero_nv)
1545 SvNV_set(sv, 0);
1546#endif
403d36eb 1547
16b305e3
NC
1548 if (mt == SVt_PVIO)
1549 IoPAGE_LEN(sv) = 60;
1550 if (old_type < SVt_RV)
1551 SvPV_set(sv, 0);
8990e307 1552 break;
403d36eb
NC
1553 default:
1554 Perl_croak(aTHX_ "panic: sv_upgrade to unknown type %lu", mt);
8990e307 1555 }
878cc751
NC
1556
1557
1558 if (old_body_arena) {
1559#ifdef PURIFY
ee6954bb 1560 my_safefree(old_body);
878cc751 1561#else
cb4415b8
NC
1562 del_body((void*)((char*)old_body + old_body_offset),
1563 old_body_arena);
878cc751 1564#endif
2068cd4d 1565 }
79072805
LW
1566}
1567
645c22ef
DM
1568/*
1569=for apidoc sv_backoff
1570
1571Remove any string offset. You should normally use the C<SvOOK_off> macro
1572wrapper instead.
1573
1574=cut
1575*/
1576
79072805 1577int
864dbfa3 1578Perl_sv_backoff(pTHX_ register SV *sv)
79072805
LW
1579{
1580 assert(SvOOK(sv));
b79f7545
NC
1581 assert(SvTYPE(sv) != SVt_PVHV);
1582 assert(SvTYPE(sv) != SVt_PVAV);
463ee0b2 1583 if (SvIVX(sv)) {
53c1dcc0 1584 const char * const s = SvPVX_const(sv);
b162af07 1585 SvLEN_set(sv, SvLEN(sv) + SvIVX(sv));
f880fe2f 1586 SvPV_set(sv, SvPVX(sv) - SvIVX(sv));
79072805 1587 SvIV_set(sv, 0);
463ee0b2 1588 Move(s, SvPVX(sv), SvCUR(sv)+1, char);
79072805
LW
1589 }
1590 SvFLAGS(sv) &= ~SVf_OOK;
a0d0e21e 1591 return 0;
79072805
LW
1592}
1593
954c1994
GS
1594/*
1595=for apidoc sv_grow
1596
645c22ef
DM
1597Expands the character buffer in the SV. If necessary, uses C<sv_unref> and
1598upgrades the SV to C<SVt_PV>. Returns a pointer to the character buffer.
1599Use the C<SvGROW> wrapper instead.
954c1994
GS
1600
1601=cut
1602*/
1603
79072805 1604char *
864dbfa3 1605Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen)
79072805
LW
1606{
1607 register char *s;
1608
55497cff 1609#ifdef HAS_64K_LIMIT
79072805 1610 if (newlen >= 0x10000) {
1d7c1841
GS
1611 PerlIO_printf(Perl_debug_log,
1612 "Allocation too large: %"UVxf"\n", (UV)newlen);
79072805
LW
1613 my_exit(1);
1614 }
55497cff 1615#endif /* HAS_64K_LIMIT */
a0d0e21e
LW
1616 if (SvROK(sv))
1617 sv_unref(sv);
79072805
LW
1618 if (SvTYPE(sv) < SVt_PV) {
1619 sv_upgrade(sv, SVt_PV);
93524f2b 1620 s = SvPVX_mutable(sv);
79072805
LW
1621 }
1622 else if (SvOOK(sv)) { /* pv is offset? */
1623 sv_backoff(sv);
93524f2b 1624 s = SvPVX_mutable(sv);
79072805
LW
1625 if (newlen > SvLEN(sv))
1626 newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
c6f8c383
GA
1627#ifdef HAS_64K_LIMIT
1628 if (newlen >= 0x10000)
1629 newlen = 0xFFFF;
1630#endif
79072805 1631 }
bc44a8a2 1632 else
4d84ee25 1633 s = SvPVX_mutable(sv);
54f0641b 1634
79072805 1635 if (newlen > SvLEN(sv)) { /* need more room? */
7a9b70e9 1636 newlen = PERL_STRLEN_ROUNDUP(newlen);
8d6dde3e 1637 if (SvLEN(sv) && s) {
7bab3ede 1638#ifdef MYMALLOC
93524f2b 1639 const STRLEN l = malloced_size((void*)SvPVX_const(sv));
8d6dde3e
IZ
1640 if (newlen <= l) {
1641 SvLEN_set(sv, l);
1642 return s;
1643 } else
c70c8a0a 1644#endif
1936d2a7 1645 s = saferealloc(s, newlen);
8d6dde3e 1646 }
bfed75c6 1647 else {
1936d2a7 1648 s = safemalloc(newlen);
3f7c398e
SP
1649 if (SvPVX_const(sv) && SvCUR(sv)) {
1650 Move(SvPVX_const(sv), s, (newlen < SvCUR(sv)) ? newlen : SvCUR(sv), char);
40565179 1651 }
4e83176d 1652 }
79072805 1653 SvPV_set(sv, s);
e1ec3a88 1654 SvLEN_set(sv, newlen);
79072805
LW
1655 }
1656 return s;
1657}
1658
954c1994
GS
1659/*
1660=for apidoc sv_setiv
1661
645c22ef
DM
1662Copies an integer into the given SV, upgrading first if necessary.
1663Does not handle 'set' magic. See also C<sv_setiv_mg>.
954c1994
GS
1664
1665=cut
1666*/
1667
79072805 1668void
864dbfa3 1669Perl_sv_setiv(pTHX_ register SV *sv, IV i)
79072805 1670{
765f542d 1671 SV_CHECK_THINKFIRST_COW_DROP(sv);
463ee0b2
LW
1672 switch (SvTYPE(sv)) {
1673 case SVt_NULL:
79072805 1674 sv_upgrade(sv, SVt_IV);
463ee0b2
LW
1675 break;
1676 case SVt_NV:
1677 sv_upgrade(sv, SVt_PVNV);
1678 break;
ed6116ce 1679 case SVt_RV:
463ee0b2 1680 case SVt_PV:
79072805 1681 sv_upgrade(sv, SVt_PVIV);
463ee0b2 1682 break;
a0d0e21e
LW
1683
1684 case SVt_PVGV:
a0d0e21e
LW
1685 case SVt_PVAV:
1686 case SVt_PVHV:
1687 case SVt_PVCV:
1688 case SVt_PVFM:
1689 case SVt_PVIO:
411caa50 1690 Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0),
53e06cf0 1691 OP_DESC(PL_op));
463ee0b2 1692 }
a0d0e21e 1693 (void)SvIOK_only(sv); /* validate number */
45977657 1694 SvIV_set(sv, i);
463ee0b2 1695 SvTAINT(sv);
79072805
LW
1696}
1697
954c1994
GS
1698/*
1699=for apidoc sv_setiv_mg
1700
1701Like C<sv_setiv>, but also handles 'set' magic.
1702
1703=cut
1704*/
1705
79072805 1706void
864dbfa3 1707Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i)
ef50df4b
GS
1708{
1709 sv_setiv(sv,i);
1710 SvSETMAGIC(sv);
1711}
1712
954c1994
GS
1713/*
1714=for apidoc sv_setuv
1715
645c22ef
DM
1716Copies an unsigned integer into the given SV, upgrading first if necessary.
1717Does not handle 'set' magic. See also C<sv_setuv_mg>.
954c1994
GS
1718
1719=cut
1720*/
1721
ef50df4b 1722void
864dbfa3 1723Perl_sv_setuv(pTHX_ register SV *sv, UV u)
55497cff 1724{
55ada374
NC
1725 /* With these two if statements:
1726 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
d460ef45 1727
55ada374
NC
1728 without
1729 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
d460ef45 1730
55ada374
NC
1731 If you wish to remove them, please benchmark to see what the effect is
1732 */
28e5dec8
JH
1733 if (u <= (UV)IV_MAX) {
1734 sv_setiv(sv, (IV)u);
1735 return;
1736 }
25da4f38
IZ
1737 sv_setiv(sv, 0);
1738 SvIsUV_on(sv);
607fa7f2 1739 SvUV_set(sv, u);
55497cff 1740}
1741
954c1994
GS
1742/*
1743=for apidoc sv_setuv_mg
1744
1745Like C<sv_setuv>, but also handles 'set' magic.
1746
1747=cut
1748*/
1749
55497cff 1750void
864dbfa3 1751Perl_sv_setuv_mg(pTHX_ register SV *sv, UV u)
ef50df4b 1752{
55ada374
NC
1753 /* With these two if statements:
1754 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
d460ef45 1755
55ada374
NC
1756 without
1757 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
d460ef45 1758
55ada374
NC
1759 If you wish to remove them, please benchmark to see what the effect is
1760 */
28e5dec8
JH
1761 if (u <= (UV)IV_MAX) {
1762 sv_setiv(sv, (IV)u);
1763 } else {
1764 sv_setiv(sv, 0);
1765 SvIsUV_on(sv);
1766 sv_setuv(sv,u);
1767 }
ef50df4b
GS
1768 SvSETMAGIC(sv);
1769}
1770
954c1994
GS
1771/*
1772=for apidoc sv_setnv
1773
645c22ef
DM
1774Copies a double into the given SV, upgrading first if necessary.
1775Does not handle 'set' magic. See also C<sv_setnv_mg>.
954c1994
GS
1776
1777=cut
1778*/
1779
ef50df4b 1780void
65202027 1781Perl_sv_setnv(pTHX_ register SV *sv, NV num)
79072805 1782{
765f542d 1783 SV_CHECK_THINKFIRST_COW_DROP(sv);
a0d0e21e
LW
1784 switch (SvTYPE(sv)) {
1785 case SVt_NULL:
1786 case SVt_IV:
79072805 1787 sv_upgrade(sv, SVt_NV);
a0d0e21e 1788 break;
a0d0e21e
LW
1789 case SVt_RV:
1790 case SVt_PV:
1791 case SVt_PVIV:
79072805 1792 sv_upgrade(sv, SVt_PVNV);
a0d0e21e 1793 break;
827b7e14 1794
a0d0e21e 1795 case SVt_PVGV:
a0d0e21e
LW
1796 case SVt_PVAV:
1797 case SVt_PVHV:
1798 case SVt_PVCV:
1799 case SVt_PVFM:
1800 case SVt_PVIO:
411caa50 1801 Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0),
53e06cf0 1802 OP_NAME(PL_op));
79072805 1803 }
9d6ce603 1804 SvNV_set(sv, num);
a0d0e21e 1805 (void)SvNOK_only(sv); /* validate number */
463ee0b2 1806 SvTAINT(sv);
79072805
LW
1807}
1808
954c1994
GS
1809/*
1810=for apidoc sv_setnv_mg
1811
1812Like C<sv_setnv>, but also handles 'set' magic.
1813
1814=cut
1815*/
1816
ef50df4b 1817void
65202027 1818Perl_sv_setnv_mg(pTHX_ register SV *sv, NV num)
ef50df4b
GS
1819{
1820 sv_setnv(sv,num);
1821 SvSETMAGIC(sv);
1822}
1823
645c22ef
DM
1824/* Print an "isn't numeric" warning, using a cleaned-up,
1825 * printable version of the offending string
1826 */
1827
76e3520e 1828STATIC void
cea2e8a9 1829S_not_a_number(pTHX_ SV *sv)
a0d0e21e 1830{
94463019
JH
1831 SV *dsv;
1832 char tmpbuf[64];
1b6737cc 1833 const char *pv;
94463019
JH
1834
1835 if (DO_UTF8(sv)) {
1836 dsv = sv_2mortal(newSVpv("", 0));
1837 pv = sv_uni_display(dsv, sv, 10, 0);
1838 } else {
1839 char *d = tmpbuf;
1840 char *limit = tmpbuf + sizeof(tmpbuf) - 8;
1841 /* each *s can expand to 4 chars + "...\0",
1842 i.e. need room for 8 chars */
ecdeb87c 1843
e62f0680
NC
1844 const char *s, *end;
1845 for (s = SvPVX_const(sv), end = s + SvCUR(sv); s < end && d < limit;
1846 s++) {
94463019
JH
1847 int ch = *s & 0xFF;
1848 if (ch & 128 && !isPRINT_LC(ch)) {
1849 *d++ = 'M';
1850 *d++ = '-';
1851 ch &= 127;
1852 }
1853 if (ch == '\n') {
1854 *d++ = '\\';
1855 *d++ = 'n';
1856 }
1857 else if (ch == '\r') {
1858 *d++ = '\\';
1859 *d++ = 'r';
1860 }
1861 else if (ch == '\f') {
1862 *d++ = '\\';
1863 *d++ = 'f';
1864 }
1865 else if (ch == '\\') {
1866 *d++ = '\\';
1867 *d++ = '\\';
1868 }
1869 else if (ch == '\0') {
1870 *d++ = '\\';
1871 *d++ = '0';
1872 }
1873 else if (isPRINT_LC(ch))
1874 *d++ = ch;
1875 else {
1876 *d++ = '^';
1877 *d++ = toCTRL(ch);
1878 }
1879 }
1880 if (s < end) {
1881 *d++ = '.';
1882 *d++ = '.';
1883 *d++ = '.';
1884 }
1885 *d = '\0';
1886 pv = tmpbuf;
a0d0e21e 1887 }
a0d0e21e 1888
533c011a 1889 if (PL_op)
9014280d 1890 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
94463019
JH
1891 "Argument \"%s\" isn't numeric in %s", pv,
1892 OP_DESC(PL_op));
a0d0e21e 1893 else
9014280d 1894 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
94463019 1895 "Argument \"%s\" isn't numeric", pv);
a0d0e21e
LW
1896}
1897
c2988b20
NC
1898/*
1899=for apidoc looks_like_number
1900
645c22ef
DM
1901Test if the content of an SV looks like a number (or is a number).
1902C<Inf> and C<Infinity> are treated as numbers (so will not issue a
1903non-numeric warning), even if your atof() doesn't grok them.
c2988b20
NC
1904
1905=cut
1906*/
1907
1908I32
1909Perl_looks_like_number(pTHX_ SV *sv)
1910{
a3b680e6 1911 register const char *sbegin;
c2988b20
NC
1912 STRLEN len;
1913
1914 if (SvPOK(sv)) {
3f7c398e 1915 sbegin = SvPVX_const(sv);
c2988b20
NC
1916 len = SvCUR(sv);
1917 }
1918 else if (SvPOKp(sv))
83003860 1919 sbegin = SvPV_const(sv, len);
c2988b20 1920 else
e0ab1c0e 1921 return SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK);
c2988b20
NC
1922 return grok_number(sbegin, len, NULL);
1923}
25da4f38
IZ
1924
1925/* Actually, ISO C leaves conversion of UV to IV undefined, but
1926 until proven guilty, assume that things are not that bad... */
1927
645c22ef
DM
1928/*
1929 NV_PRESERVES_UV:
1930
1931 As 64 bit platforms often have an NV that doesn't preserve all bits of
28e5dec8
JH
1932 an IV (an assumption perl has been based on to date) it becomes necessary
1933 to remove the assumption that the NV always carries enough precision to
1934 recreate the IV whenever needed, and that the NV is the canonical form.
1935 Instead, IV/UV and NV need to be given equal rights. So as to not lose
645c22ef 1936 precision as a side effect of conversion (which would lead to insanity
28e5dec8
JH
1937 and the dragon(s) in t/op/numconvert.t getting very angry) the intent is
1938 1) to distinguish between IV/UV/NV slots that have cached a valid
1939 conversion where precision was lost and IV/UV/NV slots that have a
1940 valid conversion which has lost no precision
645c22ef 1941 2) to ensure that if a numeric conversion to one form is requested that
28e5dec8
JH
1942 would lose precision, the precise conversion (or differently
1943 imprecise conversion) is also performed and cached, to prevent
1944 requests for different numeric formats on the same SV causing
1945 lossy conversion chains. (lossless conversion chains are perfectly
1946 acceptable (still))
1947
1948
1949 flags are used:
1950 SvIOKp is true if the IV slot contains a valid value
1951 SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true)
1952 SvNOKp is true if the NV slot contains a valid value
1953 SvNOK is true only if the NV value is accurate
1954
1955 so
645c22ef 1956 while converting from PV to NV, check to see if converting that NV to an
28e5dec8
JH
1957 IV(or UV) would lose accuracy over a direct conversion from PV to
1958 IV(or UV). If it would, cache both conversions, return NV, but mark
1959 SV as IOK NOKp (ie not NOK).
1960
645c22ef 1961 While converting from PV to IV, check to see if converting that IV to an
28e5dec8
JH
1962 NV would lose accuracy over a direct conversion from PV to NV. If it
1963 would, cache both conversions, flag similarly.
1964
1965 Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite
1966 correctly because if IV & NV were set NV *always* overruled.
645c22ef
DM
1967 Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flag's meaning
1968 changes - now IV and NV together means that the two are interchangeable:
28e5dec8 1969 SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX;
d460ef45 1970
645c22ef
DM
1971 The benefit of this is that operations such as pp_add know that if
1972 SvIOK is true for both left and right operands, then integer addition
1973 can be used instead of floating point (for cases where the result won't
1974 overflow). Before, floating point was always used, which could lead to
28e5dec8
JH
1975 loss of precision compared with integer addition.
1976
1977 * making IV and NV equal status should make maths accurate on 64 bit
1978 platforms
1979 * may speed up maths somewhat if pp_add and friends start to use
645c22ef 1980 integers when possible instead of fp. (Hopefully the overhead in
28e5dec8
JH
1981 looking for SvIOK and checking for overflow will not outweigh the
1982 fp to integer speedup)
1983 * will slow down integer operations (callers of SvIV) on "inaccurate"
1984 values, as the change from SvIOK to SvIOKp will cause a call into
1985 sv_2iv each time rather than a macro access direct to the IV slot
1986 * should speed up number->string conversion on integers as IV is
645c22ef 1987 favoured when IV and NV are equally accurate
28e5dec8
JH
1988
1989 ####################################################################
645c22ef
DM
1990 You had better be using SvIOK_notUV if you want an IV for arithmetic:
1991 SvIOK is true if (IV or UV), so you might be getting (IV)SvUV.
1992 On the other hand, SvUOK is true iff UV.
28e5dec8
JH
1993 ####################################################################
1994
645c22ef 1995 Your mileage will vary depending your CPU's relative fp to integer
28e5dec8
JH
1996 performance ratio.
1997*/
1998
1999#ifndef NV_PRESERVES_UV
645c22ef
DM
2000# define IS_NUMBER_UNDERFLOW_IV 1
2001# define IS_NUMBER_UNDERFLOW_UV 2
2002# define IS_NUMBER_IV_AND_UV 2
2003# define IS_NUMBER_OVERFLOW_IV 4
2004# define IS_NUMBER_OVERFLOW_UV 5
2005
2006/* sv_2iuv_non_preserve(): private routine for use by sv_2iv() and sv_2uv() */
28e5dec8
JH
2007
2008/* For sv_2nv these three cases are "SvNOK and don't bother casting" */
2009STATIC int
645c22ef 2010S_sv_2iuv_non_preserve(pTHX_ register SV *sv, I32 numtype)
28e5dec8 2011{
3f7c398e 2012 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
2013 if (SvNVX(sv) < (NV)IV_MIN) {
2014 (void)SvIOKp_on(sv);
2015 (void)SvNOK_on(sv);
45977657 2016 SvIV_set(sv, IV_MIN);
28e5dec8
JH
2017 return IS_NUMBER_UNDERFLOW_IV;
2018 }
2019 if (SvNVX(sv) > (NV)UV_MAX) {
2020 (void)SvIOKp_on(sv);
2021 (void)SvNOK_on(sv);
2022 SvIsUV_on(sv);
607fa7f2 2023 SvUV_set(sv, UV_MAX);
28e5dec8
JH
2024 return IS_NUMBER_OVERFLOW_UV;
2025 }
c2988b20
NC
2026 (void)SvIOKp_on(sv);
2027 (void)SvNOK_on(sv);
2028 /* Can't use strtol etc to convert this string. (See truth table in
2029 sv_2iv */
2030 if (SvNVX(sv) <= (UV)IV_MAX) {
45977657 2031 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
2032 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2033 SvIOK_on(sv); /* Integer is precise. NOK, IOK */
2034 } else {
2035 /* Integer is imprecise. NOK, IOKp */
2036 }
2037 return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV;
2038 }
2039 SvIsUV_on(sv);
607fa7f2 2040 SvUV_set(sv, U_V(SvNVX(sv)));
c2988b20
NC
2041 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2042 if (SvUVX(sv) == UV_MAX) {
2043 /* As we know that NVs don't preserve UVs, UV_MAX cannot
2044 possibly be preserved by NV. Hence, it must be overflow.
2045 NOK, IOKp */
2046 return IS_NUMBER_OVERFLOW_UV;
2047 }
2048 SvIOK_on(sv); /* Integer is precise. NOK, UOK */
2049 } else {
2050 /* Integer is imprecise. NOK, IOKp */
28e5dec8 2051 }
c2988b20 2052 return IS_NUMBER_OVERFLOW_IV;
28e5dec8 2053}
645c22ef
DM
2054#endif /* !NV_PRESERVES_UV*/
2055
891f9566
YST
2056/* sv_2iv() is now a macro using Perl_sv_2iv_flags();
2057 * this function provided for binary compatibility only
2058 */
2059
2060IV
2061Perl_sv_2iv(pTHX_ register SV *sv)
2062{
2063 return sv_2iv_flags(sv, SV_GMAGIC);
2064}
2065
645c22ef 2066/*
891f9566 2067=for apidoc sv_2iv_flags
645c22ef 2068
891f9566
YST
2069Return the integer value of an SV, doing any necessary string
2070conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2071Normally used via the C<SvIV(sv)> and C<SvIVx(sv)> macros.
645c22ef
DM
2072
2073=cut
2074*/
28e5dec8 2075
a0d0e21e 2076IV
891f9566 2077Perl_sv_2iv_flags(pTHX_ register SV *sv, I32 flags)
79072805
LW
2078{
2079 if (!sv)
2080 return 0;
8990e307 2081 if (SvGMAGICAL(sv)) {
891f9566
YST
2082 if (flags & SV_GMAGIC)
2083 mg_get(sv);
463ee0b2
LW
2084 if (SvIOKp(sv))
2085 return SvIVX(sv);
748a9306 2086 if (SvNOKp(sv)) {
25da4f38 2087 return I_V(SvNVX(sv));
748a9306 2088 }
36477c24 2089 if (SvPOKp(sv) && SvLEN(sv))
2090 return asIV(sv);
3fe9a6f1 2091 if (!SvROK(sv)) {
d008e5eb 2092 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 2093 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
29489e7c 2094 report_uninit(sv);
c6ee37c5 2095 }
36477c24 2096 return 0;
3fe9a6f1 2097 }
463ee0b2 2098 }
ed6116ce 2099 if (SvTHINKFIRST(sv)) {
a0d0e21e 2100 if (SvROK(sv)) {
a0d0e21e 2101 SV* tmpstr;
1554e226 2102 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
b4b9a328 2103 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
9e7bc3e8 2104 return SvIV(tmpstr);
56431972 2105 return PTR2IV(SvRV(sv));
a0d0e21e 2106 }
765f542d
NC
2107 if (SvIsCOW(sv)) {
2108 sv_force_normal_flags(sv, 0);
47deb5e7 2109 }
0336b60e 2110 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 2111 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 2112 report_uninit(sv);
ed6116ce
LW
2113 return 0;
2114 }
79072805 2115 }
25da4f38
IZ
2116 if (SvIOKp(sv)) {
2117 if (SvIsUV(sv)) {
2118 return (IV)(SvUVX(sv));
2119 }
2120 else {
2121 return SvIVX(sv);
2122 }
463ee0b2 2123 }
748a9306 2124 if (SvNOKp(sv)) {
28e5dec8
JH
2125 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2126 * without also getting a cached IV/UV from it at the same time
2127 * (ie PV->NV conversion should detect loss of accuracy and cache
2128 * IV or UV at same time to avoid this. NWC */
25da4f38
IZ
2129
2130 if (SvTYPE(sv) == SVt_NV)
2131 sv_upgrade(sv, SVt_PVNV);
2132
28e5dec8
JH
2133 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2134 /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost
2135 certainly cast into the IV range at IV_MAX, whereas the correct
2136 answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary
2137 cases go to UV */
2138 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
45977657 2139 SvIV_set(sv, I_V(SvNVX(sv)));
28e5dec8
JH
2140 if (SvNVX(sv) == (NV) SvIVX(sv)
2141#ifndef NV_PRESERVES_UV
2142 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2143 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2144 /* Don't flag it as "accurately an integer" if the number
2145 came from a (by definition imprecise) NV operation, and
2146 we're outside the range of NV integer precision */
2147#endif
2148 ) {
2149 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2150 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2151 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (precise)\n",
28e5dec8
JH
2152 PTR2UV(sv),
2153 SvNVX(sv),
2154 SvIVX(sv)));
2155
2156 } else {
2157 /* IV not precise. No need to convert from PV, as NV
2158 conversion would already have cached IV if it detected
2159 that PV->IV would be better than PV->NV->IV
2160 flags already correct - don't set public IOK. */
2161 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2162 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (imprecise)\n",
28e5dec8
JH
2163 PTR2UV(sv),
2164 SvNVX(sv),
2165 SvIVX(sv)));
2166 }
2167 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2168 but the cast (NV)IV_MIN rounds to a the value less (more
2169 negative) than IV_MIN which happens to be equal to SvNVX ??
2170 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2171 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2172 (NV)UVX == NVX are both true, but the values differ. :-(
2173 Hopefully for 2s complement IV_MIN is something like
2174 0x8000000000000000 which will be exact. NWC */
d460ef45 2175 }
25da4f38 2176 else {
607fa7f2 2177 SvUV_set(sv, U_V(SvNVX(sv)));
28e5dec8
JH
2178 if (
2179 (SvNVX(sv) == (NV) SvUVX(sv))
2180#ifndef NV_PRESERVES_UV
2181 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2182 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2183 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2184 /* Don't flag it as "accurately an integer" if the number
2185 came from a (by definition imprecise) NV operation, and
2186 we're outside the range of NV integer precision */
2187#endif
2188 )
2189 SvIOK_on(sv);
25da4f38
IZ
2190 SvIsUV_on(sv);
2191 ret_iv_max:
1c846c1f 2192 DEBUG_c(PerlIO_printf(Perl_debug_log,
57def98f 2193 "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n",
56431972 2194 PTR2UV(sv),
57def98f
JH
2195 SvUVX(sv),
2196 SvUVX(sv)));
25da4f38
IZ
2197 return (IV)SvUVX(sv);
2198 }
748a9306
LW
2199 }
2200 else if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20 2201 UV value;
504618e9 2202 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
25da4f38
IZ
2203 /* We want to avoid a possible problem when we cache an IV which
2204 may be later translated to an NV, and the resulting NV is not
c2988b20
NC
2205 the same as the direct translation of the initial string
2206 (eg 123.456 can shortcut to the IV 123 with atol(), but we must
2207 be careful to ensure that the value with the .456 is around if the
2208 NV value is requested in the future).
1c846c1f 2209
25da4f38
IZ
2210 This means that if we cache such an IV, we need to cache the
2211 NV as well. Moreover, we trade speed for space, and do not
28e5dec8 2212 cache the NV if we are sure it's not needed.
25da4f38 2213 */
16b7a9a4 2214
c2988b20
NC
2215 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2216 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2217 == IS_NUMBER_IN_UV) {
5e045b90 2218 /* It's definitely an integer, only upgrade to PVIV */
28e5dec8
JH
2219 if (SvTYPE(sv) < SVt_PVIV)
2220 sv_upgrade(sv, SVt_PVIV);
f7bbb42a 2221 (void)SvIOK_on(sv);
c2988b20
NC
2222 } else if (SvTYPE(sv) < SVt_PVNV)
2223 sv_upgrade(sv, SVt_PVNV);
28e5dec8 2224
c2988b20
NC
2225 /* If NV preserves UV then we only use the UV value if we know that
2226 we aren't going to call atof() below. If NVs don't preserve UVs
2227 then the value returned may have more precision than atof() will
2228 return, even though value isn't perfectly accurate. */
2229 if ((numtype & (IS_NUMBER_IN_UV
2230#ifdef NV_PRESERVES_UV
2231 | IS_NUMBER_NOT_INT
2232#endif
2233 )) == IS_NUMBER_IN_UV) {
2234 /* This won't turn off the public IOK flag if it was set above */
2235 (void)SvIOKp_on(sv);
2236
2237 if (!(numtype & IS_NUMBER_NEG)) {
2238 /* positive */;
2239 if (value <= (UV)IV_MAX) {
45977657 2240 SvIV_set(sv, (IV)value);
c2988b20 2241 } else {
607fa7f2 2242 SvUV_set(sv, value);
c2988b20
NC
2243 SvIsUV_on(sv);
2244 }
2245 } else {
2246 /* 2s complement assumption */
2247 if (value <= (UV)IV_MIN) {
45977657 2248 SvIV_set(sv, -(IV)value);
c2988b20
NC
2249 } else {
2250 /* Too negative for an IV. This is a double upgrade, but
d1be9408 2251 I'm assuming it will be rare. */
c2988b20
NC
2252 if (SvTYPE(sv) < SVt_PVNV)
2253 sv_upgrade(sv, SVt_PVNV);
2254 SvNOK_on(sv);
2255 SvIOK_off(sv);
2256 SvIOKp_on(sv);
9d6ce603 2257 SvNV_set(sv, -(NV)value);
45977657 2258 SvIV_set(sv, IV_MIN);
c2988b20
NC
2259 }
2260 }
2261 }
2262 /* For !NV_PRESERVES_UV and IS_NUMBER_IN_UV and IS_NUMBER_NOT_INT we
2263 will be in the previous block to set the IV slot, and the next
2264 block to set the NV slot. So no else here. */
2265
2266 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2267 != IS_NUMBER_IN_UV) {
2268 /* It wasn't an (integer that doesn't overflow the UV). */
3f7c398e 2269 SvNV_set(sv, Atof(SvPVX_const(sv)));
28e5dec8 2270
c2988b20
NC
2271 if (! numtype && ckWARN(WARN_NUMERIC))
2272 not_a_number(sv);
28e5dec8 2273
65202027 2274#if defined(USE_LONG_DOUBLE)
c2988b20
NC
2275 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n",
2276 PTR2UV(sv), SvNVX(sv)));
65202027 2277#else
1779d84d 2278 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"NVgf")\n",
c2988b20 2279 PTR2UV(sv), SvNVX(sv)));
65202027 2280#endif
28e5dec8
JH
2281
2282
2283#ifdef NV_PRESERVES_UV
c2988b20
NC
2284 (void)SvIOKp_on(sv);
2285 (void)SvNOK_on(sv);
2286 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
45977657 2287 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
2288 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2289 SvIOK_on(sv);
28e5dec8 2290 } else {
c2988b20
NC
2291 /* Integer is imprecise. NOK, IOKp */
2292 }
2293 /* UV will not work better than IV */
2294 } else {
2295 if (SvNVX(sv) > (NV)UV_MAX) {
2296 SvIsUV_on(sv);
2297 /* Integer is inaccurate. NOK, IOKp, is UV */
607fa7f2 2298 SvUV_set(sv, UV_MAX);
c2988b20
NC
2299 SvIsUV_on(sv);
2300 } else {
607fa7f2 2301 SvUV_set(sv, U_V(SvNVX(sv)));
c2988b20
NC
2302 /* 0xFFFFFFFFFFFFFFFF not an issue in here */
2303 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2304 SvIOK_on(sv);
28e5dec8
JH
2305 SvIsUV_on(sv);
2306 } else {
c2988b20
NC
2307 /* Integer is imprecise. NOK, IOKp, is UV */
2308 SvIsUV_on(sv);
28e5dec8 2309 }
28e5dec8 2310 }
c2988b20
NC
2311 goto ret_iv_max;
2312 }
28e5dec8 2313#else /* NV_PRESERVES_UV */
c2988b20
NC
2314 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2315 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
2316 /* The IV slot will have been set from value returned by
2317 grok_number above. The NV slot has just been set using
2318 Atof. */
560b0c46 2319 SvNOK_on(sv);
c2988b20
NC
2320 assert (SvIOKp(sv));
2321 } else {
2322 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2323 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2324 /* Small enough to preserve all bits. */
2325 (void)SvIOKp_on(sv);
2326 SvNOK_on(sv);
45977657 2327 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
2328 if ((NV)(SvIVX(sv)) == SvNVX(sv))
2329 SvIOK_on(sv);
2330 /* Assumption: first non-preserved integer is < IV_MAX,
2331 this NV is in the preserved range, therefore: */
2332 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
2333 < (UV)IV_MAX)) {
32fdb065 2334 Perl_croak(aTHX_ "sv_2iv assumed (U_V(fabs((double)SvNVX(sv))) < (UV)IV_MAX) but SvNVX(sv)=%"NVgf" U_V is 0x%"UVxf", IV_MAX is 0x%"UVxf"\n", SvNVX(sv), U_V(SvNVX(sv)), (UV)IV_MAX);
c2988b20
NC
2335 }
2336 } else {
2337 /* IN_UV NOT_INT
2338 0 0 already failed to read UV.
2339 0 1 already failed to read UV.
2340 1 0 you won't get here in this case. IV/UV
2341 slot set, public IOK, Atof() unneeded.
2342 1 1 already read UV.
2343 so there's no point in sv_2iuv_non_preserve() attempting
2344 to use atol, strtol, strtoul etc. */
2345 if (sv_2iuv_non_preserve (sv, numtype)
2346 >= IS_NUMBER_OVERFLOW_IV)
2347 goto ret_iv_max;
2348 }
2349 }
28e5dec8 2350#endif /* NV_PRESERVES_UV */
25da4f38 2351 }
28e5dec8 2352 } else {
599cee73 2353 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
29489e7c 2354 report_uninit(sv);
25da4f38
IZ
2355 if (SvTYPE(sv) < SVt_IV)
2356 /* Typically the caller expects that sv_any is not NULL now. */
2357 sv_upgrade(sv, SVt_IV);
a0d0e21e 2358 return 0;
79072805 2359 }
1d7c1841
GS
2360 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n",
2361 PTR2UV(sv),SvIVX(sv)));
25da4f38 2362 return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv);
79072805
LW
2363}
2364
891f9566
YST
2365/* sv_2uv() is now a macro using Perl_sv_2uv_flags();
2366 * this function provided for binary compatibility only
2367 */
2368
2369UV
2370Perl_sv_2uv(pTHX_ register SV *sv)
2371{
2372 return sv_2uv_flags(sv, SV_GMAGIC);
2373}
2374
645c22ef 2375/*
891f9566 2376=for apidoc sv_2uv_flags
645c22ef
DM
2377
2378Return the unsigned integer value of an SV, doing any necessary string
891f9566
YST
2379conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2380Normally used via the C<SvUV(sv)> and C<SvUVx(sv)> macros.
645c22ef
DM
2381
2382=cut
2383*/
2384
ff68c719 2385UV
891f9566 2386Perl_sv_2uv_flags(pTHX_ register SV *sv, I32 flags)
ff68c719 2387{
2388 if (!sv)
2389 return 0;
2390 if (SvGMAGICAL(sv)) {
891f9566
YST
2391 if (flags & SV_GMAGIC)
2392 mg_get(sv);
ff68c719 2393 if (SvIOKp(sv))
2394 return SvUVX(sv);
2395 if (SvNOKp(sv))
2396 return U_V(SvNVX(sv));
36477c24 2397 if (SvPOKp(sv) && SvLEN(sv))
2398 return asUV(sv);
3fe9a6f1 2399 if (!SvROK(sv)) {
d008e5eb 2400 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 2401 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
29489e7c 2402 report_uninit(sv);
c6ee37c5 2403 }
36477c24 2404 return 0;
3fe9a6f1 2405 }
ff68c719 2406 }
2407 if (SvTHINKFIRST(sv)) {
2408 if (SvROK(sv)) {
ff68c719 2409 SV* tmpstr;
1554e226 2410 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
b4b9a328 2411 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
9e7bc3e8 2412 return SvUV(tmpstr);
56431972 2413 return PTR2UV(SvRV(sv));
ff68c719 2414 }
765f542d
NC
2415 if (SvIsCOW(sv)) {
2416 sv_force_normal_flags(sv, 0);
8a818333 2417 }
0336b60e 2418 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 2419 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 2420 report_uninit(sv);
ff68c719 2421 return 0;
2422 }
2423 }
25da4f38
IZ
2424 if (SvIOKp(sv)) {
2425 if (SvIsUV(sv)) {
2426 return SvUVX(sv);
2427 }
2428 else {
2429 return (UV)SvIVX(sv);
2430 }
ff68c719 2431 }
2432 if (SvNOKp(sv)) {
28e5dec8
JH
2433 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2434 * without also getting a cached IV/UV from it at the same time
2435 * (ie PV->NV conversion should detect loss of accuracy and cache
2436 * IV or UV at same time to avoid this. */
2437 /* IV-over-UV optimisation - choose to cache IV if possible */
2438
25da4f38
IZ
2439 if (SvTYPE(sv) == SVt_NV)
2440 sv_upgrade(sv, SVt_PVNV);
28e5dec8
JH
2441
2442 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2443 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
45977657 2444 SvIV_set(sv, I_V(SvNVX(sv)));
28e5dec8
JH
2445 if (SvNVX(sv) == (NV) SvIVX(sv)
2446#ifndef NV_PRESERVES_UV
2447 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2448 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2449 /* Don't flag it as "accurately an integer" if the number
2450 came from a (by definition imprecise) NV operation, and
2451 we're outside the range of NV integer precision */
2452#endif
2453 ) {
2454 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2455 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2456 "0x%"UVxf" uv(%"NVgf" => %"IVdf") (precise)\n",
28e5dec8
JH
2457 PTR2UV(sv),
2458 SvNVX(sv),
2459 SvIVX(sv)));
2460
2461 } else {
2462 /* IV not precise. No need to convert from PV, as NV
2463 conversion would already have cached IV if it detected
2464 that PV->IV would be better than PV->NV->IV
2465 flags already correct - don't set public IOK. */
2466 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2467 "0x%"UVxf" uv(%"NVgf" => %"IVdf") (imprecise)\n",
28e5dec8
JH
2468 PTR2UV(sv),
2469 SvNVX(sv),
2470 SvIVX(sv)));
2471 }
2472 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2473 but the cast (NV)IV_MIN rounds to a the value less (more
2474 negative) than IV_MIN which happens to be equal to SvNVX ??
2475 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2476 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2477 (NV)UVX == NVX are both true, but the values differ. :-(
2478 Hopefully for 2s complement IV_MIN is something like
2479 0x8000000000000000 which will be exact. NWC */
d460ef45 2480 }
28e5dec8 2481 else {
607fa7f2 2482 SvUV_set(sv, U_V(SvNVX(sv)));
28e5dec8
JH
2483 if (
2484 (SvNVX(sv) == (NV) SvUVX(sv))
2485#ifndef NV_PRESERVES_UV
2486 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2487 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2488 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2489 /* Don't flag it as "accurately an integer" if the number
2490 came from a (by definition imprecise) NV operation, and
2491 we're outside the range of NV integer precision */
2492#endif
2493 )
2494 SvIOK_on(sv);
2495 SvIsUV_on(sv);
1c846c1f 2496 DEBUG_c(PerlIO_printf(Perl_debug_log,
28e5dec8 2497 "0x%"UVxf" 2uv(%"UVuf" => %"IVdf") (as unsigned)\n",
57def98f 2498 PTR2UV(sv),
28e5dec8
JH
2499 SvUVX(sv),
2500 SvUVX(sv)));
25da4f38 2501 }
ff68c719 2502 }
2503 else if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20 2504 UV value;
504618e9 2505 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
25da4f38
IZ
2506
2507 /* We want to avoid a possible problem when we cache a UV which
2508 may be later translated to an NV, and the resulting NV is not
2509 the translation of the initial data.
1c846c1f 2510
25da4f38
IZ
2511 This means that if we cache such a UV, we need to cache the
2512 NV as well. Moreover, we trade speed for space, and do not
2513 cache the NV if not needed.
2514 */
16b7a9a4 2515
c2988b20
NC
2516 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2517 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2518 == IS_NUMBER_IN_UV) {
5e045b90 2519 /* It's definitely an integer, only upgrade to PVIV */
28e5dec8 2520 if (SvTYPE(sv) < SVt_PVIV)
f7bbb42a
JH
2521 sv_upgrade(sv, SVt_PVIV);
2522 (void)SvIOK_on(sv);
c2988b20
NC
2523 } else if (SvTYPE(sv) < SVt_PVNV)
2524 sv_upgrade(sv, SVt_PVNV);
d460ef45 2525
c2988b20
NC
2526 /* If NV preserves UV then we only use the UV value if we know that
2527 we aren't going to call atof() below. If NVs don't preserve UVs
2528 then the value returned may have more precision than atof() will
2529 return, even though it isn't accurate. */
2530 if ((numtype & (IS_NUMBER_IN_UV
2531#ifdef NV_PRESERVES_UV
2532 | IS_NUMBER_NOT_INT
2533#endif
2534 )) == IS_NUMBER_IN_UV) {
2535 /* This won't turn off the public IOK flag if it was set above */
2536 (void)SvIOKp_on(sv);
2537
2538 if (!(numtype & IS_NUMBER_NEG)) {
2539 /* positive */;
2540 if (value <= (UV)IV_MAX) {
45977657 2541 SvIV_set(sv, (IV)value);
28e5dec8
JH
2542 } else {
2543 /* it didn't overflow, and it was positive. */
607fa7f2 2544 SvUV_set(sv, value);
28e5dec8
JH
2545 SvIsUV_on(sv);
2546 }
c2988b20
NC
2547 } else {
2548 /* 2s complement assumption */
2549 if (value <= (UV)IV_MIN) {
45977657 2550 SvIV_set(sv, -(IV)value);
c2988b20
NC
2551 } else {
2552 /* Too negative for an IV. This is a double upgrade, but
d1be9408 2553 I'm assuming it will be rare. */
c2988b20
NC
2554 if (SvTYPE(sv) < SVt_PVNV)
2555 sv_upgrade(sv, SVt_PVNV);
2556 SvNOK_on(sv);
2557 SvIOK_off(sv);
2558 SvIOKp_on(sv);
9d6ce603 2559 SvNV_set(sv, -(NV)value);
45977657 2560 SvIV_set(sv, IV_MIN);
c2988b20
NC
2561 }
2562 }
2563 }
2564
2565 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2566 != IS_NUMBER_IN_UV) {
2567 /* It wasn't an integer, or it overflowed the UV. */
3f7c398e 2568 SvNV_set(sv, Atof(SvPVX_const(sv)));
28e5dec8 2569
c2988b20 2570 if (! numtype && ckWARN(WARN_NUMERIC))
28e5dec8
JH
2571 not_a_number(sv);
2572
2573#if defined(USE_LONG_DOUBLE)
c2988b20
NC
2574 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%" PERL_PRIgldbl ")\n",
2575 PTR2UV(sv), SvNVX(sv)));
28e5dec8 2576#else
1779d84d 2577 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"NVgf")\n",
c2988b20 2578 PTR2UV(sv), SvNVX(sv)));
28e5dec8
JH
2579#endif
2580
2581#ifdef NV_PRESERVES_UV
c2988b20
NC
2582 (void)SvIOKp_on(sv);
2583 (void)SvNOK_on(sv);
2584 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
45977657 2585 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
2586 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2587 SvIOK_on(sv);
2588 } else {
2589 /* Integer is imprecise. NOK, IOKp */
2590 }
2591 /* UV will not work better than IV */
2592 } else {
2593 if (SvNVX(sv) > (NV)UV_MAX) {
2594 SvIsUV_on(sv);
2595 /* Integer is inaccurate. NOK, IOKp, is UV */
607fa7f2 2596 SvUV_set(sv, UV_MAX);
c2988b20
NC
2597 SvIsUV_on(sv);
2598 } else {
607fa7f2 2599 SvUV_set(sv, U_V(SvNVX(sv)));
c2988b20
NC
2600 /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs
2601 NV preservse UV so can do correct comparison. */
2602 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2603 SvIOK_on(sv);
2604 SvIsUV_on(sv);
2605 } else {
2606 /* Integer is imprecise. NOK, IOKp, is UV */
2607 SvIsUV_on(sv);
2608 }
2609 }
2610 }
28e5dec8 2611#else /* NV_PRESERVES_UV */
c2988b20
NC
2612 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2613 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
2614 /* The UV slot will have been set from value returned by
2615 grok_number above. The NV slot has just been set using
2616 Atof. */
560b0c46 2617 SvNOK_on(sv);
c2988b20
NC
2618 assert (SvIOKp(sv));
2619 } else {
2620 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2621 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2622 /* Small enough to preserve all bits. */
2623 (void)SvIOKp_on(sv);
2624 SvNOK_on(sv);
45977657 2625 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
2626 if ((NV)(SvIVX(sv)) == SvNVX(sv))
2627 SvIOK_on(sv);
2628 /* Assumption: first non-preserved integer is < IV_MAX,
2629 this NV is in the preserved range, therefore: */
2630 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
2631 < (UV)IV_MAX)) {
32fdb065 2632 Perl_croak(aTHX_ "sv_2uv assumed (U_V(fabs((double)SvNVX(sv))) < (UV)IV_MAX) but SvNVX(sv)=%"NVgf" U_V is 0x%"UVxf", IV_MAX is 0x%"UVxf"\n", SvNVX(sv), U_V(SvNVX(sv)), (UV)IV_MAX);
c2988b20
NC
2633 }
2634 } else
2635 sv_2iuv_non_preserve (sv, numtype);
2636 }
28e5dec8 2637#endif /* NV_PRESERVES_UV */
f7bbb42a 2638 }
ff68c719 2639 }
2640 else {
d008e5eb 2641 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 2642 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
29489e7c 2643 report_uninit(sv);
c6ee37c5 2644 }
25da4f38
IZ
2645 if (SvTYPE(sv) < SVt_IV)
2646 /* Typically the caller expects that sv_any is not NULL now. */
2647 sv_upgrade(sv, SVt_IV);
ff68c719 2648 return 0;
2649 }
25da4f38 2650
1d7c1841
GS
2651 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n",
2652 PTR2UV(sv),SvUVX(sv)));
25da4f38 2653 return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv);
ff68c719 2654}
2655
645c22ef
DM
2656/*
2657=for apidoc sv_2nv
2658
2659Return the num value of an SV, doing any necessary string or integer
2660conversion, magic etc. Normally used via the C<SvNV(sv)> and C<SvNVx(sv)>
2661macros.
2662
2663=cut
2664*/
2665
65202027 2666NV
864dbfa3 2667Perl_sv_2nv(pTHX_ register SV *sv)
79072805
LW
2668{
2669 if (!sv)
2670 return 0.0;
8990e307 2671 if (SvGMAGICAL(sv)) {
463ee0b2
LW
2672 mg_get(sv);
2673 if (SvNOKp(sv))
2674 return SvNVX(sv);
a0d0e21e 2675 if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20 2676 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) &&
504618e9 2677 !grok_number(SvPVX_const(sv), SvCUR(sv), NULL))
a0d0e21e 2678 not_a_number(sv);
3f7c398e 2679 return Atof(SvPVX_const(sv));
a0d0e21e 2680 }
25da4f38 2681 if (SvIOKp(sv)) {
1c846c1f 2682 if (SvIsUV(sv))
65202027 2683 return (NV)SvUVX(sv);
25da4f38 2684 else
65202027 2685 return (NV)SvIVX(sv);
25da4f38 2686 }
16d20bd9 2687 if (!SvROK(sv)) {
d008e5eb 2688 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 2689 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
29489e7c 2690 report_uninit(sv);
c6ee37c5 2691 }
66a1b24b 2692 return (NV)0;
16d20bd9 2693 }
463ee0b2 2694 }
ed6116ce 2695 if (SvTHINKFIRST(sv)) {
a0d0e21e 2696 if (SvROK(sv)) {
a0d0e21e 2697 SV* tmpstr;
1554e226 2698 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
b4b9a328 2699 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
9e7bc3e8 2700 return SvNV(tmpstr);
56431972 2701 return PTR2NV(SvRV(sv));
a0d0e21e 2702 }
765f542d
NC
2703 if (SvIsCOW(sv)) {
2704 sv_force_normal_flags(sv, 0);
8a818333 2705 }
0336b60e 2706 if (SvREADONLY(sv) && !SvOK(sv)) {
599cee73 2707 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 2708 report_uninit(sv);
ed6116ce
LW
2709 return 0.0;
2710 }
79072805
LW
2711 }
2712 if (SvTYPE(sv) < SVt_NV) {
463ee0b2
LW
2713 if (SvTYPE(sv) == SVt_IV)
2714 sv_upgrade(sv, SVt_PVNV);
2715 else
2716 sv_upgrade(sv, SVt_NV);
906f284f 2717#ifdef USE_LONG_DOUBLE
097ee67d 2718 DEBUG_c({
f93f4e46 2719 STORE_NUMERIC_LOCAL_SET_STANDARD();
1d7c1841
GS
2720 PerlIO_printf(Perl_debug_log,
2721 "0x%"UVxf" num(%" PERL_PRIgldbl ")\n",
2722 PTR2UV(sv), SvNVX(sv));
572bbb43
GS
2723 RESTORE_NUMERIC_LOCAL();
2724 });
65202027 2725#else
572bbb43 2726 DEBUG_c({
f93f4e46 2727 STORE_NUMERIC_LOCAL_SET_STANDARD();
1779d84d 2728 PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%"NVgf")\n",
1d7c1841 2729 PTR2UV(sv), SvNVX(sv));
097ee67d
JH
2730 RESTORE_NUMERIC_LOCAL();
2731 });
572bbb43 2732#endif
79072805
LW
2733 }
2734 else if (SvTYPE(sv) < SVt_PVNV)
2735 sv_upgrade(sv, SVt_PVNV);
59d8ce62
NC
2736 if (SvNOKp(sv)) {
2737 return SvNVX(sv);
61604483 2738 }
59d8ce62 2739 if (SvIOKp(sv)) {
9d6ce603 2740 SvNV_set(sv, SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv));
28e5dec8
JH
2741#ifdef NV_PRESERVES_UV
2742 SvNOK_on(sv);
2743#else
2744 /* Only set the public NV OK flag if this NV preserves the IV */
2745 /* Check it's not 0xFFFFFFFFFFFFFFFF */
2746 if (SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv))))
2747 : (SvIVX(sv) == I_V(SvNVX(sv))))
2748 SvNOK_on(sv);
2749 else
2750 SvNOKp_on(sv);
2751#endif
93a17b20 2752 }
748a9306 2753 else if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20 2754 UV value;
3f7c398e 2755 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
c2988b20 2756 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !numtype)
a0d0e21e 2757 not_a_number(sv);
28e5dec8 2758#ifdef NV_PRESERVES_UV
c2988b20
NC
2759 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2760 == IS_NUMBER_IN_UV) {
5e045b90 2761 /* It's definitely an integer */
9d6ce603 2762 SvNV_set(sv, (numtype & IS_NUMBER_NEG) ? -(NV)value : (NV)value);
c2988b20 2763 } else
3f7c398e 2764 SvNV_set(sv, Atof(SvPVX_const(sv)));
28e5dec8
JH
2765 SvNOK_on(sv);
2766#else
3f7c398e 2767 SvNV_set(sv, Atof(SvPVX_const(sv)));
28e5dec8
JH
2768 /* Only set the public NV OK flag if this NV preserves the value in
2769 the PV at least as well as an IV/UV would.
2770 Not sure how to do this 100% reliably. */
2771 /* if that shift count is out of range then Configure's test is
2772 wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS ==
2773 UV_BITS */
2774 if (((UV)1 << NV_PRESERVES_UV_BITS) >
c2988b20 2775 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
28e5dec8 2776 SvNOK_on(sv); /* Definitely small enough to preserve all bits */
c2988b20
NC
2777 } else if (!(numtype & IS_NUMBER_IN_UV)) {
2778 /* Can't use strtol etc to convert this string, so don't try.
2779 sv_2iv and sv_2uv will use the NV to convert, not the PV. */
2780 SvNOK_on(sv);
2781 } else {
2782 /* value has been set. It may not be precise. */
2783 if ((numtype & IS_NUMBER_NEG) && (value > (UV)IV_MIN)) {
2784 /* 2s complement assumption for (UV)IV_MIN */
2785 SvNOK_on(sv); /* Integer is too negative. */
2786 } else {
2787 SvNOKp_on(sv);
2788 SvIOKp_on(sv);
6fa402ec 2789
c2988b20 2790 if (numtype & IS_NUMBER_NEG) {
45977657 2791 SvIV_set(sv, -(IV)value);
c2988b20 2792 } else if (value <= (UV)IV_MAX) {
45977657 2793 SvIV_set(sv, (IV)value);
c2988b20 2794 } else {
607fa7f2 2795 SvUV_set(sv, value);
c2988b20
NC
2796 SvIsUV_on(sv);
2797 }
2798
2799 if (numtype & IS_NUMBER_NOT_INT) {
2800 /* I believe that even if the original PV had decimals,
2801 they are lost beyond the limit of the FP precision.
2802 However, neither is canonical, so both only get p
2803 flags. NWC, 2000/11/25 */
2804 /* Both already have p flags, so do nothing */
2805 } else {
66a1b24b 2806 const NV nv = SvNVX(sv);
c2988b20
NC
2807 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2808 if (SvIVX(sv) == I_V(nv)) {
2809 SvNOK_on(sv);
2810 SvIOK_on(sv);
2811 } else {
2812 SvIOK_on(sv);
2813 /* It had no "." so it must be integer. */
2814 }
2815 } else {
2816 /* between IV_MAX and NV(UV_MAX).
2817 Could be slightly > UV_MAX */
6fa402ec 2818
c2988b20
NC
2819 if (numtype & IS_NUMBER_NOT_INT) {
2820 /* UV and NV both imprecise. */
2821 } else {
66a1b24b 2822 const UV nv_as_uv = U_V(nv);
c2988b20
NC
2823
2824 if (value == nv_as_uv && SvUVX(sv) != UV_MAX) {
2825 SvNOK_on(sv);
2826 SvIOK_on(sv);
2827 } else {
2828 SvIOK_on(sv);
2829 }
2830 }
2831 }
2832 }
2833 }
2834 }
28e5dec8 2835#endif /* NV_PRESERVES_UV */
93a17b20 2836 }
79072805 2837 else {
599cee73 2838 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
29489e7c 2839 report_uninit(sv);
25da4f38
IZ
2840 if (SvTYPE(sv) < SVt_NV)
2841 /* Typically the caller expects that sv_any is not NULL now. */
28e5dec8
JH
2842 /* XXX Ilya implies that this is a bug in callers that assume this
2843 and ideally should be fixed. */
25da4f38 2844 sv_upgrade(sv, SVt_NV);
a0d0e21e 2845 return 0.0;
79072805 2846 }
572bbb43 2847#if defined(USE_LONG_DOUBLE)
097ee67d 2848 DEBUG_c({
f93f4e46 2849 STORE_NUMERIC_LOCAL_SET_STANDARD();
1d7c1841
GS
2850 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n",
2851 PTR2UV(sv), SvNVX(sv));
572bbb43
GS
2852 RESTORE_NUMERIC_LOCAL();
2853 });
65202027 2854#else
572bbb43 2855 DEBUG_c({
f93f4e46 2856 STORE_NUMERIC_LOCAL_SET_STANDARD();
1779d84d 2857 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%"NVgf")\n",
1d7c1841 2858 PTR2UV(sv), SvNVX(sv));
097ee67d
JH
2859 RESTORE_NUMERIC_LOCAL();
2860 });
572bbb43 2861#endif
463ee0b2 2862 return SvNVX(sv);
79072805
LW
2863}
2864
645c22ef
DM
2865/* asIV(): extract an integer from the string value of an SV.
2866 * Caller must validate PVX */
2867
76e3520e 2868STATIC IV
cea2e8a9 2869S_asIV(pTHX_ SV *sv)
36477c24 2870{
c2988b20 2871 UV value;
66a1b24b 2872 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
c2988b20
NC
2873
2874 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2875 == IS_NUMBER_IN_UV) {
645c22ef 2876 /* It's definitely an integer */
c2988b20
NC
2877 if (numtype & IS_NUMBER_NEG) {
2878 if (value < (UV)IV_MIN)
2879 return -(IV)value;
2880 } else {
2881 if (value < (UV)IV_MAX)
2882 return (IV)value;
2883 }
2884 }
d008e5eb 2885 if (!numtype) {
d008e5eb
GS
2886 if (ckWARN(WARN_NUMERIC))
2887 not_a_number(sv);
2888 }
3f7c398e 2889 return I_V(Atof(SvPVX_const(sv)));
36477c24 2890}
2891
645c22ef
DM
2892/* asUV(): extract an unsigned integer from the string value of an SV
2893 * Caller must validate PVX */
2894
76e3520e 2895STATIC UV
cea2e8a9 2896S_asUV(pTHX_ SV *sv)
36477c24 2897{
c2988b20 2898 UV value;
504618e9 2899 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
36477c24 2900
c2988b20
NC
2901 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2902 == IS_NUMBER_IN_UV) {
645c22ef 2903 /* It's definitely an integer */
6fa402ec 2904 if (!(numtype & IS_NUMBER_NEG))
c2988b20
NC
2905 return value;
2906 }
d008e5eb 2907 if (!numtype) {
d008e5eb
GS
2908 if (ckWARN(WARN_NUMERIC))
2909 not_a_number(sv);
2910 }
3f7c398e 2911 return U_V(Atof(SvPVX_const(sv)));
36477c24 2912}
2913
645c22ef
DM
2914/*
2915=for apidoc sv_2pv_nolen
2916
2917Like C<sv_2pv()>, but doesn't return the length too. You should usually
2918use the macro wrapper C<SvPV_nolen(sv)> instead.
2919=cut
2920*/
2921
79072805 2922char *
864dbfa3 2923Perl_sv_2pv_nolen(pTHX_ register SV *sv)
1fa8b10d 2924{
dafda6d1 2925 return sv_2pv(sv, 0);
1fa8b10d
JD
2926}
2927
645c22ef
DM
2928/* uiv_2buf(): private routine for use by sv_2pv_flags(): print an IV or
2929 * UV as a string towards the end of buf, and return pointers to start and
2930 * end of it.
2931 *
2932 * We assume that buf is at least TYPE_CHARS(UV) long.
2933 */
2934
864dbfa3 2935static char *
25da4f38
IZ
2936uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob)
2937{
25da4f38
IZ
2938 char *ptr = buf + TYPE_CHARS(UV);
2939 char *ebuf = ptr;
2940 int sign;
25da4f38
IZ
2941
2942 if (is_uv)
2943 sign = 0;
2944 else if (iv >= 0) {
2945 uv = iv;
2946 sign = 0;
2947 } else {
2948 uv = -iv;
2949 sign = 1;
2950 }
2951 do {
eb160463 2952 *--ptr = '0' + (char)(uv % 10);
25da4f38
IZ
2953 } while (uv /= 10);
2954 if (sign)
2955 *--ptr = '-';
2956 *peob = ebuf;
2957 return ptr;
2958}
2959
09540bc3
JH
2960/* sv_2pv() is now a macro using Perl_sv_2pv_flags();
2961 * this function provided for binary compatibility only
2962 */
2963
2964char *
2965Perl_sv_2pv(pTHX_ register SV *sv, STRLEN *lp)
2966{
2967 return sv_2pv_flags(sv, lp, SV_GMAGIC);
2968}
2969
645c22ef
DM
2970/*
2971=for apidoc sv_2pv_flags
2972
ff276b08 2973Returns a pointer to the string value of an SV, and sets *lp to its length.
645c22ef
DM
2974If flags includes SV_GMAGIC, does an mg_get() first. Coerces sv to a string
2975if necessary.
2976Normally invoked via the C<SvPV_flags> macro. C<sv_2pv()> and C<sv_2pv_nomg>
2977usually end up here too.
2978
2979=cut
2980*/
2981
8d6d96c1
HS
2982char *
2983Perl_sv_2pv_flags(pTHX_ register SV *sv, STRLEN *lp, I32 flags)
2984{
79072805
LW
2985 register char *s;
2986 int olderrno;
cb50f42d 2987 SV *tsv, *origsv;
25da4f38
IZ
2988 char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */
2989 char *tmpbuf = tbuf;
79072805 2990
463ee0b2 2991 if (!sv) {
cdb061a3
NC
2992 if (lp)
2993 *lp = 0;
73d840c0 2994 return (char *)"";
463ee0b2 2995 }
8990e307 2996 if (SvGMAGICAL(sv)) {
8d6d96c1
HS
2997 if (flags & SV_GMAGIC)
2998 mg_get(sv);
463ee0b2 2999 if (SvPOKp(sv)) {
cdb061a3
NC
3000 if (lp)
3001 *lp = SvCUR(sv);
10516c54
NC
3002 if (flags & SV_MUTABLE_RETURN)
3003 return SvPVX_mutable(sv);
4d84ee25
NC
3004 if (flags & SV_CONST_RETURN)
3005 return (char *)SvPVX_const(sv);
463ee0b2
LW
3006 return SvPVX(sv);
3007 }
cf2093f6 3008 if (SvIOKp(sv)) {
1c846c1f 3009 if (SvIsUV(sv))
57def98f 3010 (void)sprintf(tmpbuf,"%"UVuf, (UV)SvUVX(sv));
cf2093f6 3011 else
57def98f 3012 (void)sprintf(tmpbuf,"%"IVdf, (IV)SvIVX(sv));
46fc3d4c 3013 tsv = Nullsv;
a0d0e21e 3014 goto tokensave;
463ee0b2
LW
3015 }
3016 if (SvNOKp(sv)) {
2d4389e4 3017 Gconvert(SvNVX(sv), NV_DIG, 0, tmpbuf);
46fc3d4c 3018 tsv = Nullsv;
a0d0e21e 3019 goto tokensave;
463ee0b2 3020 }
16d20bd9 3021 if (!SvROK(sv)) {
d008e5eb 3022 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 3023 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
29489e7c 3024 report_uninit(sv);
c6ee37c5 3025 }
cdb061a3
NC
3026 if (lp)
3027 *lp = 0;
73d840c0 3028 return (char *)"";
16d20bd9 3029 }
463ee0b2 3030 }
ed6116ce
LW
3031 if (SvTHINKFIRST(sv)) {
3032 if (SvROK(sv)) {
a0d0e21e 3033 SV* tmpstr;
e1ec3a88 3034 register const char *typestr;
1554e226 3035 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)) &&
b4b9a328 3036 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
50adf7d2
NC
3037 /* Unwrap this: */
3038 /* char *pv = lp ? SvPV(tmpstr, *lp) : SvPV_nolen(tmpstr); */
3039
3040 char *pv;
3041 if ((SvFLAGS(tmpstr) & (SVf_POK)) == SVf_POK) {
3042 if (flags & SV_CONST_RETURN) {
3043 pv = (char *) SvPVX_const(tmpstr);
3044 } else {
3045 pv = (flags & SV_MUTABLE_RETURN)
3046 ? SvPVX_mutable(tmpstr) : SvPVX(tmpstr);
3047 }
3048 if (lp)
3049 *lp = SvCUR(tmpstr);
3050 } else {
3051 pv = sv_2pv_flags(tmpstr, lp, flags);
3052 }
446eaa42
YST
3053 if (SvUTF8(tmpstr))
3054 SvUTF8_on(sv);
3055 else
3056 SvUTF8_off(sv);
3057 return pv;
3058 }
cb50f42d 3059 origsv = sv;
ed6116ce
LW
3060 sv = (SV*)SvRV(sv);
3061 if (!sv)
e1ec3a88 3062 typestr = "NULLREF";
ed6116ce 3063 else {
f9277f47
IZ
3064 MAGIC *mg;
3065
ed6116ce 3066 switch (SvTYPE(sv)) {
f9277f47
IZ
3067 case SVt_PVMG:
3068 if ( ((SvFLAGS(sv) &
1c846c1f 3069 (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG))
faf82a0b 3070 == (SVs_OBJECT|SVs_SMG))
14befaf4 3071 && (mg = mg_find(sv, PERL_MAGIC_qr))) {
e1ec3a88 3072 const regexp *re = (regexp *)mg->mg_obj;
1bd3ad17 3073
2cd61cdb 3074 if (!mg->mg_ptr) {
e1ec3a88 3075 const char *fptr = "msix";
8782bef2
GB
3076 char reflags[6];
3077 char ch;
3078 int left = 0;
3079 int right = 4;
ff385a1b 3080 char need_newline = 0;
eb160463 3081 U16 reganch = (U16)((re->reganch & PMf_COMPILETIME) >> 12);
8782bef2 3082
155aba94 3083 while((ch = *fptr++)) {
8782bef2
GB
3084 if(reganch & 1) {
3085 reflags[left++] = ch;
3086 }
3087 else {
3088 reflags[right--] = ch;
3089 }
3090 reganch >>= 1;
3091 }
3092 if(left != 4) {
3093 reflags[left] = '-';
3094 left = 5;
3095 }
3096
3097 mg->mg_len = re->prelen + 4 + left;
ff385a1b
JF
3098 /*
3099 * If /x was used, we have to worry about a regex
3100 * ending with a comment later being embedded
3101 * within another regex. If so, we don't want this
3102 * regex's "commentization" to leak out to the
3103 * right part of the enclosing regex, we must cap
3104 * it with a newline.
3105 *
3106 * So, if /x was used, we scan backwards from the
3107 * end of the regex. If we find a '#' before we
3108 * find a newline, we need to add a newline
3109 * ourself. If we find a '\n' first (or if we
3110 * don't find '#' or '\n'), we don't need to add
3111 * anything. -jfriedl
3112 */
3113 if (PMf_EXTENDED & re->reganch)
3114 {
e1ec3a88 3115 const char *endptr = re->precomp + re->prelen;
ff385a1b
JF
3116 while (endptr >= re->precomp)
3117 {
e1ec3a88 3118 const char c = *(endptr--);
ff385a1b
JF
3119 if (c == '\n')
3120 break; /* don't need another */
3121 if (c == '#') {
3122 /* we end while in a comment, so we
3123 need a newline */
3124 mg->mg_len++; /* save space for it */
3125 need_newline = 1; /* note to add it */
ab01544f 3126 break;
ff385a1b
JF
3127 }
3128 }
3129 }
3130
8782bef2
GB
3131 New(616, mg->mg_ptr, mg->mg_len + 1 + left, char);
3132 Copy("(?", mg->mg_ptr, 2, char);
3133 Copy(reflags, mg->mg_ptr+2, left, char);
3134 Copy(":", mg->mg_ptr+left+2, 1, char);
3135 Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char);
ff385a1b
JF
3136 if (need_newline)
3137 mg->mg_ptr[mg->mg_len - 2] = '\n';
1bd3ad17
IZ
3138 mg->mg_ptr[mg->mg_len - 1] = ')';
3139 mg->mg_ptr[mg->mg_len] = 0;
3140 }
3280af22 3141 PL_reginterp_cnt += re->program[0].next_off;
cb50f42d
YST
3142
3143 if (re->reganch & ROPT_UTF8)
3144 SvUTF8_on(origsv);
3145 else
3146 SvUTF8_off(origsv);
cdb061a3
NC
3147 if (lp)
3148 *lp = mg->mg_len;
1bd3ad17 3149 return mg->mg_ptr;
f9277f47
IZ
3150 }
3151 /* Fall through */
ed6116ce
LW
3152 case SVt_NULL:
3153 case SVt_IV:
3154 case SVt_NV:
3155 case SVt_RV:
3156 case SVt_PV:
3157 case SVt_PVIV:
3158 case SVt_PVNV:
e1ec3a88
AL
3159 case SVt_PVBM: typestr = SvROK(sv) ? "REF" : "SCALAR"; break;
3160 case SVt_PVLV: typestr = SvROK(sv) ? "REF"
be65207d
DM
3161 /* tied lvalues should appear to be
3162 * scalars for backwards compatitbility */
3163 : (LvTYPE(sv) == 't' || LvTYPE(sv) == 'T')
3164 ? "SCALAR" : "LVALUE"; break;
e1ec3a88
AL
3165 case SVt_PVAV: typestr = "ARRAY"; break;
3166 case SVt_PVHV: typestr = "HASH"; break;
3167 case SVt_PVCV: typestr = "CODE"; break;
3168 case SVt_PVGV: typestr = "GLOB"; break;
3169 case SVt_PVFM: typestr = "FORMAT"; break;
3170 case SVt_PVIO: typestr = "IO"; break;
3171 default: typestr = "UNKNOWN"; break;
ed6116ce 3172 }
46fc3d4c 3173 tsv = NEWSV(0,0);
a5cb6b62 3174 if (SvOBJECT(sv)) {
bfcb3514 3175 const char *name = HvNAME_get(SvSTASH(sv));
a5cb6b62 3176 Perl_sv_setpvf(aTHX_ tsv, "%s=%s(0x%"UVxf")",
e1ec3a88 3177 name ? name : "__ANON__" , typestr, PTR2UV(sv));
a5cb6b62 3178 }
ed6116ce 3179 else
e1ec3a88 3180 Perl_sv_setpvf(aTHX_ tsv, "%s(0x%"UVxf")", typestr, PTR2UV(sv));
a0d0e21e 3181 goto tokensaveref;
463ee0b2 3182 }
cdb061a3
NC
3183 if (lp)
3184 *lp = strlen(typestr);
73d840c0 3185 return (char *)typestr;
79072805 3186 }
0336b60e 3187 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 3188 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 3189 report_uninit(sv);
cdb061a3
NC
3190 if (lp)
3191 *lp = 0;
73d840c0 3192 return (char *)"";
79072805 3193 }
79072805 3194 }
28e5dec8
JH
3195 if (SvIOK(sv) || ((SvIOKp(sv) && !SvNOKp(sv)))) {
3196 /* I'm assuming that if both IV and NV are equally valid then
3197 converting the IV is going to be more efficient */
e1ec3a88
AL
3198 const U32 isIOK = SvIOK(sv);
3199 const U32 isUIOK = SvIsUV(sv);
28e5dec8
JH
3200 char buf[TYPE_CHARS(UV)];
3201 char *ebuf, *ptr;
3202
3203 if (SvTYPE(sv) < SVt_PVIV)
3204 sv_upgrade(sv, SVt_PVIV);
3205 if (isUIOK)
3206 ptr = uiv_2buf(buf, 0, SvUVX(sv), 1, &ebuf);
3207 else
3208 ptr = uiv_2buf(buf, SvIVX(sv), 0, 0, &ebuf);
5902b6a9
NC
3209 /* inlined from sv_setpvn */
3210 SvGROW_mutable(sv, (STRLEN)(ebuf - ptr + 1));
4d84ee25 3211 Move(ptr,SvPVX_mutable(sv),ebuf - ptr,char);
28e5dec8
JH
3212 SvCUR_set(sv, ebuf - ptr);
3213 s = SvEND(sv);
3214 *s = '\0';
3215 if (isIOK)
3216 SvIOK_on(sv);
3217 else
3218 SvIOKp_on(sv);
3219 if (isUIOK)
3220 SvIsUV_on(sv);
3221 }
3222 else if (SvNOKp(sv)) {
79072805
LW
3223 if (SvTYPE(sv) < SVt_PVNV)
3224 sv_upgrade(sv, SVt_PVNV);
1c846c1f 3225 /* The +20 is pure guesswork. Configure test needed. --jhi */
5902b6a9 3226 s = SvGROW_mutable(sv, NV_DIG + 20);
79072805 3227 olderrno = errno; /* some Xenix systems wipe out errno here */
79072805 3228#ifdef apollo
463ee0b2 3229 if (SvNVX(sv) == 0.0)
79072805
LW
3230 (void)strcpy(s,"0");
3231 else
3232#endif /*apollo*/
bbce6d69 3233 {
2d4389e4 3234 Gconvert(SvNVX(sv), NV_DIG, 0, s);
bbce6d69 3235 }
79072805 3236 errno = olderrno;
a0d0e21e
LW
3237#ifdef FIXNEGATIVEZERO
3238 if (*s == '-' && s[1] == '0' && !s[2])
3239 strcpy(s,"0");
3240#endif
79072805
LW
3241 while (*s) s++;
3242#ifdef hcx
3243 if (s[-1] == '.')
46fc3d4c 3244 *--s = '\0';
79072805
LW
3245#endif
3246 }
79072805 3247 else {
0336b60e
IZ
3248 if (ckWARN(WARN_UNINITIALIZED)
3249 && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
29489e7c 3250 report_uninit(sv);
cdb061a3 3251 if (lp)
a0d0e21e 3252 *lp = 0;
25da4f38
IZ
3253 if (SvTYPE(sv) < SVt_PV)
3254 /* Typically the caller expects that sv_any is not NULL now. */
3255 sv_upgrade(sv, SVt_PV);
73d840c0 3256 return (char *)"";
79072805 3257 }
cdb061a3
NC
3258 {
3259 STRLEN len = s - SvPVX_const(sv);
3260 if (lp)
3261 *lp = len;
3262 SvCUR_set(sv, len);
3263 }
79072805 3264 SvPOK_on(sv);
1d7c1841 3265 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
3f7c398e 3266 PTR2UV(sv),SvPVX_const(sv)));
4d84ee25
NC
3267 if (flags & SV_CONST_RETURN)
3268 return (char *)SvPVX_const(sv);
10516c54
NC
3269 if (flags & SV_MUTABLE_RETURN)
3270 return SvPVX_mutable(sv);
463ee0b2 3271 return SvPVX(sv);
a0d0e21e
LW
3272
3273 tokensave:
3274 if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */
3275 /* Sneaky stuff here */
3276
3277 tokensaveref:
46fc3d4c 3278 if (!tsv)
96827780 3279 tsv = newSVpv(tmpbuf, 0);
46fc3d4c 3280 sv_2mortal(tsv);
cdb061a3
NC
3281 if (lp)
3282 *lp = SvCUR(tsv);
46fc3d4c 3283 return SvPVX(tsv);
a0d0e21e
LW
3284 }
3285 else {
27da23d5 3286 dVAR;
a0d0e21e 3287 STRLEN len;
73d840c0 3288 const char *t;
46fc3d4c 3289
3290 if (tsv) {
3291 sv_2mortal(tsv);
3f7c398e 3292 t = SvPVX_const(tsv);
46fc3d4c 3293 len = SvCUR(tsv);
3294 }
3295 else {
96827780
MB
3296 t = tmpbuf;
3297 len = strlen(tmpbuf);
46fc3d4c 3298 }
a0d0e21e 3299#ifdef FIXNEGATIVEZERO
46fc3d4c 3300 if (len == 2 && t[0] == '-' && t[1] == '0') {
3301 t = "0";
3302 len = 1;
3303 }
a0d0e21e 3304#endif
862a34c6 3305 SvUPGRADE(sv, SVt_PV);
cdb061a3
NC
3306 if (lp)
3307 *lp = len;
5902b6a9 3308 s = SvGROW_mutable(sv, len + 1);
a0d0e21e 3309 SvCUR_set(sv, len);
6bf554b4 3310 SvPOKp_on(sv);
e90e2364 3311 return strcpy(s, t);
a0d0e21e 3312 }
463ee0b2
LW
3313}
3314
645c22ef 3315/*
6050d10e
JP
3316=for apidoc sv_copypv
3317
3318Copies a stringified representation of the source SV into the
3319destination SV. Automatically performs any necessary mg_get and
54f0641b 3320coercion of numeric values into strings. Guaranteed to preserve
6050d10e 3321UTF-8 flag even from overloaded objects. Similar in nature to
54f0641b
NIS
3322sv_2pv[_flags] but operates directly on an SV instead of just the
3323string. Mostly uses sv_2pv_flags to do its work, except when that
6050d10e
JP
3324would lose the UTF-8'ness of the PV.
3325
3326=cut
3327*/
3328
3329void
3330Perl_sv_copypv(pTHX_ SV *dsv, register SV *ssv)
3331{
446eaa42 3332 STRLEN len;
53c1dcc0 3333 const char * const s = SvPV_const(ssv,len);
cb50f42d 3334 sv_setpvn(dsv,s,len);
446eaa42 3335 if (SvUTF8(ssv))
cb50f42d 3336 SvUTF8_on(dsv);
446eaa42 3337 else
cb50f42d 3338 SvUTF8_off(dsv);
6050d10e
JP
3339}
3340
3341/*
645c22ef
DM
3342=for apidoc sv_2pvbyte_nolen
3343
3344Return a pointer to the byte-encoded representation of the SV.
1e54db1a 3345May cause the SV to be downgraded from UTF-8 as a side-effect.
645c22ef
DM
3346
3347Usually accessed via the C<SvPVbyte_nolen> macro.
3348
3349=cut
3350*/
3351
7340a771
GS
3352char *
3353Perl_sv_2pvbyte_nolen(pTHX_ register SV *sv)
3354{
dafda6d1 3355 return sv_2pvbyte(sv, 0);
7340a771
GS
3356}
3357
645c22ef
DM
3358/*
3359=for apidoc sv_2pvbyte
3360
3361Return a pointer to the byte-encoded representation of the SV, and set *lp
1e54db1a 3362to its length. May cause the SV to be downgraded from UTF-8 as a
645c22ef
DM
3363side-effect.
3364
3365Usually accessed via the C<SvPVbyte> macro.
3366
3367=cut
3368*/
3369
7340a771
GS
3370char *
3371Perl_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp)
3372{
0875d2fe 3373 sv_utf8_downgrade(sv,0);
97972285 3374 return lp ? SvPV(sv,*lp) : SvPV_nolen(sv);
7340a771
GS
3375}
3376
645c22ef
DM
3377/*
3378=for apidoc sv_2pvutf8_nolen
3379
1e54db1a
JH
3380Return a pointer to the UTF-8-encoded representation of the SV.
3381May cause the SV to be upgraded to UTF-8 as a side-effect.
645c22ef
DM
3382
3383Usually accessed via the C<SvPVutf8_nolen> macro.
3384
3385=cut
3386*/
3387
7340a771
GS
3388char *
3389Perl_sv_2pvutf8_nolen(pTHX_ register SV *sv)
3390{
dafda6d1 3391 return sv_2pvutf8(sv, 0);
7340a771
GS
3392}
3393
645c22ef
DM
3394/*
3395=for apidoc sv_2pvutf8
3396
1e54db1a
JH
3397Return a pointer to the UTF-8-encoded representation of the SV, and set *lp
3398to its length. May cause the SV to be upgraded to UTF-8 as a side-effect.
645c22ef
DM
3399
3400Usually accessed via the C<SvPVutf8> macro.
3401
3402=cut
3403*/
3404
7340a771
GS
3405char *
3406Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp)
3407{
560a288e 3408 sv_utf8_upgrade(sv);
7d59b7e4 3409 return SvPV(sv,*lp);
7340a771 3410}
1c846c1f 3411
645c22ef
DM
3412/*
3413=for apidoc sv_2bool
3414
3415This function is only called on magical items, and is only used by
8cf8f3d1 3416sv_true() or its macro equivalent.
645c22ef
DM
3417
3418=cut
3419*/
3420
463ee0b2 3421bool
864dbfa3 3422Perl_sv_2bool(pTHX_ register SV *sv)
463ee0b2 3423{
8990e307 3424 if (SvGMAGICAL(sv))
463ee0b2
LW
3425 mg_get(sv);
3426
a0d0e21e
LW
3427 if (!SvOK(sv))
3428 return 0;
3429 if (SvROK(sv)) {
a0d0e21e 3430 SV* tmpsv;
1554e226 3431 if (SvAMAGIC(sv) && (tmpsv=AMG_CALLun(sv,bool_)) &&
9e3013b1 3432 (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv))))
8a31060d 3433 return (bool)SvTRUE(tmpsv);
a0d0e21e
LW
3434 return SvRV(sv) != 0;
3435 }
463ee0b2 3436 if (SvPOKp(sv)) {
53c1dcc0
AL
3437 register XPV* const Xpvtmp = (XPV*)SvANY(sv);
3438 if (Xpvtmp &&
339049b0 3439 (*sv->sv_u.svu_pv > '0' ||
11343788 3440 Xpvtmp->xpv_cur > 1 ||
339049b0 3441 (Xpvtmp->xpv_cur && *sv->sv_u.svu_pv != '0')))
463ee0b2
LW
3442 return 1;
3443 else
3444 return 0;
3445 }
3446 else {
3447 if (SvIOKp(sv))
3448 return SvIVX(sv) != 0;
3449 else {
3450 if (SvNOKp(sv))
3451 return SvNVX(sv) != 0.0;
3452 else
3453 return FALSE;
3454 }
3455 }
79072805
LW
3456}
3457
09540bc3
JH
3458/* sv_utf8_upgrade() is now a macro using sv_utf8_upgrade_flags();
3459 * this function provided for binary compatibility only
3460 */
3461
3462
3463STRLEN
3464Perl_sv_utf8_upgrade(pTHX_ register SV *sv)
3465{
3466 return sv_utf8_upgrade_flags(sv, SV_GMAGIC);
3467}
3468
c461cf8f
JH
3469/*
3470=for apidoc sv_utf8_upgrade
3471
78ea37eb 3472Converts the PV of an SV to its UTF-8-encoded form.
645c22ef 3473Forces the SV to string form if it is not already.
4411f3b6
NIS
3474Always sets the SvUTF8 flag to avoid future validity checks even
3475if all the bytes have hibit clear.
c461cf8f 3476
13a6c0e0
JH
3477This is not as a general purpose byte encoding to Unicode interface:
3478use the Encode extension for that.
3479
8d6d96c1
HS
3480=for apidoc sv_utf8_upgrade_flags
3481
78ea37eb 3482Converts the PV of an SV to its UTF-8-encoded form.
645c22ef 3483Forces the SV to string form if it is not already.
8d6d96c1
HS
3484Always sets the SvUTF8 flag to avoid future validity checks even
3485if all the bytes have hibit clear. If C<flags> has C<SV_GMAGIC> bit set,
3486will C<mg_get> on C<sv> if appropriate, else not. C<sv_utf8_upgrade> and
3487C<sv_utf8_upgrade_nomg> are implemented in terms of this function.
3488
13a6c0e0
JH
3489This is not as a general purpose byte encoding to Unicode interface:
3490use the Encode extension for that.
3491
8d6d96c1
HS
3492=cut
3493*/
3494
3495STRLEN
3496Perl_sv_utf8_upgrade_flags(pTHX_ register SV *sv, I32 flags)
3497{
808c356f
RGS
3498 if (sv == &PL_sv_undef)
3499 return 0;
e0e62c2a
NIS
3500 if (!SvPOK(sv)) {
3501 STRLEN len = 0;
d52b7888
NC
3502 if (SvREADONLY(sv) && (SvPOKp(sv) || SvIOKp(sv) || SvNOKp(sv))) {
3503 (void) sv_2pv_flags(sv,&len, flags);
3504 if (SvUTF8(sv))
3505 return len;
3506 } else {
3507 (void) SvPV_force(sv,len);
3508 }
e0e62c2a 3509 }
4411f3b6 3510
f5cee72b 3511 if (SvUTF8(sv)) {
5fec3b1d 3512 return SvCUR(sv);
f5cee72b 3513 }
5fec3b1d 3514
765f542d
NC
3515 if (SvIsCOW(sv)) {
3516 sv_force_normal_flags(sv, 0);
db42d148
NIS
3517 }
3518
88632417 3519 if (PL_encoding && !(flags & SV_UTF8_NO_ENCODING))
799ef3cb 3520 sv_recode_to_utf8(sv, PL_encoding);
9f4817db 3521 else { /* Assume Latin-1/EBCDIC */
c4e7c712
NC
3522 /* This function could be much more efficient if we
3523 * had a FLAG in SVs to signal if there are any hibit
3524 * chars in the PV. Given that there isn't such a flag
3525 * make the loop as fast as possible. */
93524f2b
NC
3526 const U8 *s = (U8 *) SvPVX_const(sv);
3527 const U8 *e = (U8 *) SvEND(sv);
3528 const U8 *t = s;
c4e7c712
NC
3529 int hibit = 0;
3530
3531 while (t < e) {
53c1dcc0 3532 const U8 ch = *t++;
c4e7c712
NC
3533 if ((hibit = !NATIVE_IS_INVARIANT(ch)))
3534 break;
3535 }
3536 if (hibit) {
3537 STRLEN len = SvCUR(sv) + 1; /* Plus the \0 */
53c1dcc0 3538 U8 * const recoded = bytes_to_utf8((U8*)s, &len);
c4e7c712
NC
3539
3540 SvPV_free(sv); /* No longer using what was there before. */
3541
1e2ebb21 3542 SvPV_set(sv, (char*)recoded);
c4e7c712
NC
3543 SvCUR_set(sv, len - 1);
3544 SvLEN_set(sv, len); /* No longer know the real size. */
3545 }
3546 /* Mark as UTF-8 even if no hibit - saves scanning loop */
3547 SvUTF8_on(sv);
560a288e 3548 }
4411f3b6 3549 return SvCUR(sv);
560a288e
GS
3550}
3551
c461cf8f
JH
3552/*
3553=for apidoc sv_utf8_downgrade
3554
78ea37eb
TS
3555Attempts to convert the PV of an SV from characters to bytes.
3556If the PV contains a character beyond byte, this conversion will fail;
3557in this case, either returns false or, if C<fail_ok> is not
c461cf8f
JH
3558true, croaks.
3559
13a6c0e0
JH
3560This is not as a general purpose Unicode to byte encoding interface:
3561use the Encode extension for that.
3562
c461cf8f
JH
3563=cut
3564*/
3565
560a288e
GS
3566bool
3567Perl_sv_utf8_downgrade(pTHX_ register SV* sv, bool fail_ok)
3568{
78ea37eb 3569 if (SvPOKp(sv) && SvUTF8(sv)) {
fa301091 3570 if (SvCUR(sv)) {
03cfe0ae 3571 U8 *s;
652088fc 3572 STRLEN len;
fa301091 3573
765f542d
NC
3574 if (SvIsCOW(sv)) {
3575 sv_force_normal_flags(sv, 0);
3576 }
03cfe0ae
NIS
3577 s = (U8 *) SvPV(sv, len);
3578 if (!utf8_to_bytes(s, &len)) {
fa301091
JH
3579 if (fail_ok)
3580 return FALSE;
3581 else {
3582 if (PL_op)
3583 Perl_croak(aTHX_ "Wide character in %s",
53e06cf0 3584 OP_DESC(PL_op));
fa301091
JH
3585 else
3586 Perl_croak(aTHX_ "Wide character");
3587 }
4b3603a4 3588 }
b162af07 3589 SvCUR_set(sv, len);
67e989fb 3590 }
560a288e 3591 }
ffebcc3e 3592 SvUTF8_off(sv);
560a288e
GS
3593 return TRUE;
3594}
3595
c461cf8f
JH
3596/*
3597=for apidoc sv_utf8_encode
3598
78ea37eb
TS
3599Converts the PV of an SV to UTF-8, but then turns the C<SvUTF8>
3600flag off so that it looks like octets again.
c461cf8f
JH
3601
3602=cut
3603*/
3604
560a288e
GS
3605void
3606Perl_sv_utf8_encode(pTHX_ register SV *sv)
3607{
4411f3b6 3608 (void) sv_utf8_upgrade(sv);
4c94c214
NC
3609 if (SvIsCOW(sv)) {
3610 sv_force_normal_flags(sv, 0);
3611 }
3612 if (SvREADONLY(sv)) {
3613 Perl_croak(aTHX_ PL_no_modify);
3614 }
560a288e
GS
3615 SvUTF8_off(sv);
3616}
3617
4411f3b6
NIS
3618/*
3619=for apidoc sv_utf8_decode
3620
78ea37eb
TS
3621If the PV of the SV is an octet sequence in UTF-8
3622and contains a multiple-byte character, the C<SvUTF8> flag is turned on
3623so that it looks like a character. If the PV contains only single-byte
3624characters, the C<SvUTF8> flag stays being off.
3625Scans PV for validity and returns false if the PV is invalid UTF-8.
4411f3b6
NIS
3626
3627=cut
3628*/
3629
560a288e
GS
3630bool
3631Perl_sv_utf8_decode(pTHX_ register SV *sv)
3632{
78ea37eb 3633 if (SvPOKp(sv)) {
93524f2b
NC
3634 const U8 *c;
3635 const U8 *e;
9cbac4c7 3636
645c22ef
DM
3637 /* The octets may have got themselves encoded - get them back as
3638 * bytes
3639 */
3640 if (!sv_utf8_downgrade(sv, TRUE))
560a288e
GS
3641 return FALSE;
3642
3643 /* it is actually just a matter of turning the utf8 flag on, but
3644 * we want to make sure everything inside is valid utf8 first.
3645 */
93524f2b 3646 c = (const U8 *) SvPVX_const(sv);
63cd0674 3647 if (!is_utf8_string(c, SvCUR(sv)+1))
67e989fb 3648 return FALSE;
93524f2b 3649 e = (const U8 *) SvEND(sv);
511c2ff0 3650 while (c < e) {
c4d5f83a
NIS
3651 U8 ch = *c++;
3652 if (!UTF8_IS_INVARIANT(ch)) {
67e989fb
JH
3653 SvUTF8_on(sv);
3654 break;
3655 }
560a288e 3656 }
560a288e
GS
3657 }
3658 return TRUE;
3659}
3660
09540bc3
JH
3661/* sv_setsv() is now a macro using Perl_sv_setsv_flags();
3662 * this function provided for binary compatibility only
3663 */
3664
3665void
3666Perl_sv_setsv(pTHX_ SV *dstr, register SV *sstr)
3667{
3668 sv_setsv_flags(dstr, sstr, SV_GMAGIC);
3669}
3670
954c1994
GS
3671/*
3672=for apidoc sv_setsv
3673
645c22ef
DM
3674Copies the contents of the source SV C<ssv> into the destination SV
3675C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
3676function if the source SV needs to be reused. Does not handle 'set' magic.
3677Loosely speaking, it performs a copy-by-value, obliterating any previous
3678content of the destination.
3679
3680You probably want to use one of the assortment of wrappers, such as
3681C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
3682C<SvSetMagicSV_nosteal>.
3683
8d6d96c1
HS
3684=for apidoc sv_setsv_flags
3685
645c22ef
DM
3686Copies the contents of the source SV C<ssv> into the destination SV
3687C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
3688function if the source SV needs to be reused. Does not handle 'set' magic.
3689Loosely speaking, it performs a copy-by-value, obliterating any previous
3690content of the destination.
3691If the C<flags> parameter has the C<SV_GMAGIC> bit set, will C<mg_get> on
5fcdf167
NC
3692C<ssv> if appropriate, else not. If the C<flags> parameter has the
3693C<NOSTEAL> bit set then the buffers of temps will not be stolen. <sv_setsv>
3694and C<sv_setsv_nomg> are implemented in terms of this function.
645c22ef
DM
3695
3696You probably want to use one of the assortment of wrappers, such as
3697C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
3698C<SvSetMagicSV_nosteal>.
3699
3700This is the primary function for copying scalars, and most other
3701copy-ish functions and macros use this underneath.
8d6d96c1
HS
3702
3703=cut
3704*/
3705
3706void
3707Perl_sv_setsv_flags(pTHX_ SV *dstr, register SV *sstr, I32 flags)
3708{
8990e307
LW
3709 register U32 sflags;
3710 register int dtype;
3711 register int stype;
463ee0b2 3712
79072805
LW
3713 if (sstr == dstr)
3714 return;
765f542d 3715 SV_CHECK_THINKFIRST_COW_DROP(dstr);
79072805 3716 if (!sstr)
3280af22 3717 sstr = &PL_sv_undef;
8990e307
LW
3718 stype = SvTYPE(sstr);
3719 dtype = SvTYPE(dstr);
79072805 3720
a0d0e21e 3721 SvAMAGIC_off(dstr);
7a5fa8a2 3722 if ( SvVOK(dstr) )
ece467f9
JP
3723 {
3724 /* need to nuke the magic */
3725 mg_free(dstr);
3726 SvRMAGICAL_off(dstr);
3727 }
9e7bc3e8 3728
463ee0b2 3729 /* There's a lot of redundancy below but we're going for speed here */
79072805 3730
8990e307 3731 switch (stype) {
79072805 3732 case SVt_NULL:
aece5585 3733 undef_sstr:
20408e3c
GS
3734 if (dtype != SVt_PVGV) {
3735 (void)SvOK_off(dstr);
3736 return;
3737 }
3738 break;
463ee0b2 3739 case SVt_IV:
aece5585
GA
3740 if (SvIOK(sstr)) {
3741 switch (dtype) {
3742 case SVt_NULL:
8990e307 3743 sv_upgrade(dstr, SVt_IV);
aece5585
GA
3744 break;
3745 case SVt_NV:
8990e307 3746 sv_upgrade(dstr, SVt_PVNV);
aece5585
GA
3747 break;
3748 case SVt_RV:
3749 case SVt_PV:
a0d0e21e 3750 sv_upgrade(dstr, SVt_PVIV);
aece5585
GA
3751 break;
3752 }
3753 (void)SvIOK_only(dstr);
45977657 3754 SvIV_set(dstr, SvIVX(sstr));
25da4f38
IZ
3755 if (SvIsUV(sstr))
3756 SvIsUV_on(dstr);
27c9684d
AP
3757 if (SvTAINTED(sstr))
3758 SvTAINT(dstr);
aece5585 3759 return;
8990e307 3760 }
aece5585
GA
3761 goto undef_sstr;
3762
463ee0b2 3763 case SVt_NV:
aece5585
GA
3764 if (SvNOK(sstr)) {
3765 switch (dtype) {
3766 case SVt_NULL:
3767 case SVt_IV:
8990e307 3768 sv_upgrade(dstr, SVt_NV);
aece5585
GA
3769 break;
3770 case SVt_RV:
3771 case SVt_PV:
3772 case SVt_PVIV:
a0d0e21e 3773 sv_upgrade(dstr, SVt_PVNV);
aece5585
GA
3774 break;
3775 }
9d6ce603 3776 SvNV_set(dstr, SvNVX(sstr));
aece5585 3777 (void)SvNOK_only(dstr);
27c9684d
AP
3778 if (SvTAINTED(sstr))
3779 SvTAINT(dstr);
aece5585 3780 return;
8990e307 3781 }
aece5585
GA
3782 goto undef_sstr;
3783
ed6116ce 3784 case SVt_RV:
8990e307 3785 if (dtype < SVt_RV)
ed6116ce 3786 sv_upgrade(dstr, SVt_RV);
c07a80fd 3787 else if (dtype == SVt_PVGV &&
23bb1b96 3788 SvROK(sstr) && SvTYPE(SvRV(sstr)) == SVt_PVGV) {
c07a80fd 3789 sstr = SvRV(sstr);
a5f75d66 3790 if (sstr == dstr) {
1d7c1841
GS
3791 if (GvIMPORTED(dstr) != GVf_IMPORTED
3792 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3793 {
a5f75d66 3794 GvIMPORTED_on(dstr);
1d7c1841 3795 }
a5f75d66
AD
3796 GvMULTI_on(dstr);
3797 return;
3798 }
c07a80fd 3799 goto glob_assign;
3800 }
ed6116ce 3801 break;
fc36a67e 3802 case SVt_PVFM:
f8c7b90f 3803#ifdef PERL_OLD_COPY_ON_WRITE
d89fc664
NC
3804 if ((SvFLAGS(sstr) & CAN_COW_MASK) == CAN_COW_FLAGS) {
3805 if (dtype < SVt_PVIV)
3806 sv_upgrade(dstr, SVt_PVIV);
3807 break;
3808 }
3809 /* Fall through */
3810#endif
3811 case SVt_PV:
8990e307 3812 if (dtype < SVt_PV)
463ee0b2 3813 sv_upgrade(dstr, SVt_PV);
463ee0b2
LW
3814 break;
3815 case SVt_PVIV:
8990e307 3816 if (dtype < SVt_PVIV)
463ee0b2 3817 sv_upgrade(dstr, SVt_PVIV);
463ee0b2
LW
3818 break;
3819 case SVt_PVNV:
8990e307 3820 if (dtype < SVt_PVNV)
463ee0b2 3821 sv_upgrade(dstr, SVt_PVNV);
463ee0b2 3822 break;
4633a7c4
LW
3823 case SVt_PVAV:
3824 case SVt_PVHV:
3825 case SVt_PVCV:
4633a7c4 3826 case SVt_PVIO:
a3b680e6
AL
3827 {
3828 const char * const type = sv_reftype(sstr,0);
533c011a 3829 if (PL_op)
a3b680e6 3830 Perl_croak(aTHX_ "Bizarre copy of %s in %s", type, OP_NAME(PL_op));
4633a7c4 3831 else
a3b680e6
AL
3832 Perl_croak(aTHX_ "Bizarre copy of %s", type);
3833 }
4633a7c4
LW
3834 break;
3835
79072805 3836 case SVt_PVGV:
8990e307 3837 if (dtype <= SVt_PVGV) {
c07a80fd 3838 glob_assign:
a5f75d66 3839 if (dtype != SVt_PVGV) {
a3b680e6
AL
3840 const char * const name = GvNAME(sstr);
3841 const STRLEN len = GvNAMELEN(sstr);
b76195c2
DM
3842 /* don't upgrade SVt_PVLV: it can hold a glob */
3843 if (dtype != SVt_PVLV)
3844 sv_upgrade(dstr, SVt_PVGV);
14befaf4 3845 sv_magic(dstr, dstr, PERL_MAGIC_glob, Nullch, 0);
e15faf7d
NC
3846 GvSTASH(dstr) = GvSTASH(sstr);
3847 if (GvSTASH(dstr))
3848 Perl_sv_add_backref(aTHX_ (SV*)GvSTASH(dstr), dstr);
a0d0e21e
LW
3849 GvNAME(dstr) = savepvn(name, len);
3850 GvNAMELEN(dstr) = len;
3851 SvFAKE_on(dstr); /* can coerce to non-glob */
3852 }
7bac28a0 3853 /* ahem, death to those who redefine active sort subs */
3280af22
NIS
3854 else if (PL_curstackinfo->si_type == PERLSI_SORT
3855 && GvCV(dstr) && PL_sortcop == CvSTART(GvCV(dstr)))
cea2e8a9 3856 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s",
7bac28a0 3857 GvNAME(dstr));
5bd07a3d 3858
7fb37951
AMS
3859#ifdef GV_UNIQUE_CHECK
3860 if (GvUNIQUE((GV*)dstr)) {
5bd07a3d
DM
3861 Perl_croak(aTHX_ PL_no_modify);
3862 }
3863#endif
3864
a0d0e21e 3865 (void)SvOK_off(dstr);
a5f75d66 3866 GvINTRO_off(dstr); /* one-shot flag */
1edc1566 3867 gp_free((GV*)dstr);
79072805 3868 GvGP(dstr) = gp_ref(GvGP(sstr));
27c9684d
AP
3869 if (SvTAINTED(sstr))
3870 SvTAINT(dstr);
1d7c1841
GS
3871 if (GvIMPORTED(dstr) != GVf_IMPORTED
3872 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3873 {
a5f75d66 3874 GvIMPORTED_on(dstr);
1d7c1841 3875 }
a5f75d66 3876 GvMULTI_on(dstr);
79072805
LW
3877 return;
3878 }
3879 /* FALL THROUGH */
3880
3881 default:
8d6d96c1 3882 if (SvGMAGICAL(sstr) && (flags & SV_GMAGIC)) {
973f89ab 3883 mg_get(sstr);
eb160463 3884 if ((int)SvTYPE(sstr) != stype) {
973f89ab
CS
3885 stype = SvTYPE(sstr);
3886 if (stype == SVt_PVGV && dtype <= SVt_PVGV)
3887 goto glob_assign;
3888 }
3889 }
ded42b9f 3890 if (stype == SVt_PVLV)
862a34c6 3891 SvUPGRADE(dstr, SVt_PVNV);
ded42b9f 3892 else
862a34c6 3893 SvUPGRADE(dstr, (U32)stype);
79072805
LW
3894 }
3895
8990e307
LW
3896 sflags = SvFLAGS(sstr);
3897
3898 if (sflags & SVf_ROK) {
3899 if (dtype >= SVt_PV) {
3900 if (dtype == SVt_PVGV) {
3901 SV *sref = SvREFCNT_inc(SvRV(sstr));
3902 SV *dref = 0;
a3b680e6 3903 const int intro = GvINTRO(dstr);
a0d0e21e 3904
7fb37951
AMS
3905#ifdef GV_UNIQUE_CHECK
3906 if (GvUNIQUE((GV*)dstr)) {
5bd07a3d
DM
3907 Perl_croak(aTHX_ PL_no_modify);
3908 }
3909#endif
3910
a0d0e21e 3911 if (intro) {
a5f75d66 3912 GvINTRO_off(dstr); /* one-shot flag */
1d7c1841 3913 GvLINE(dstr) = CopLINE(PL_curcop);
1edc1566 3914 GvEGV(dstr) = (GV*)dstr;
a0d0e21e 3915 }
a5f75d66 3916 GvMULTI_on(dstr);
8990e307
LW
3917 switch (SvTYPE(sref)) {
3918 case SVt_PVAV:
a0d0e21e 3919 if (intro)
890ed176 3920 SAVEGENERICSV(GvAV(dstr));
a0d0e21e
LW
3921 else
3922 dref = (SV*)GvAV(dstr);
8990e307 3923 GvAV(dstr) = (AV*)sref;
39bac7f7 3924 if (!GvIMPORTED_AV(dstr)
1d7c1841
GS
3925 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3926 {
a5f75d66 3927 GvIMPORTED_AV_on(dstr);
1d7c1841 3928 }
8990e307
LW
3929 break;
3930 case SVt_PVHV:
a0d0e21e 3931 if (intro)
890ed176 3932 SAVEGENERICSV(GvHV(dstr));
a0d0e21e
LW
3933 else
3934 dref = (SV*)GvHV(dstr);
8990e307 3935 GvHV(dstr) = (HV*)sref;
39bac7f7 3936 if (!GvIMPORTED_HV(dstr)
1d7c1841
GS
3937 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3938 {
a5f75d66 3939 GvIMPORTED_HV_on(dstr);
1d7c1841 3940 }
8990e307
LW
3941 break;
3942 case SVt_PVCV:
8ebc5c01 3943 if (intro) {
3944 if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) {
3945 SvREFCNT_dec(GvCV(dstr));
3946 GvCV(dstr) = Nullcv;
68dc0745 3947 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
3280af22 3948 PL_sub_generation++;
8ebc5c01 3949 }
890ed176 3950 SAVEGENERICSV(GvCV(dstr));
8ebc5c01 3951 }
68dc0745 3952 else
3953 dref = (SV*)GvCV(dstr);
3954 if (GvCV(dstr) != (CV*)sref) {
748a9306 3955 CV* cv = GvCV(dstr);
4633a7c4 3956 if (cv) {
68dc0745 3957 if (!GvCVGEN((GV*)dstr) &&
3958 (CvROOT(cv) || CvXSUB(cv)))
3959 {
7bac28a0 3960 /* ahem, death to those who redefine
3961 * active sort subs */
3280af22
NIS
3962 if (PL_curstackinfo->si_type == PERLSI_SORT &&
3963 PL_sortcop == CvSTART(cv))
1c846c1f 3964 Perl_croak(aTHX_
7bac28a0 3965 "Can't redefine active sort subroutine %s",
3966 GvENAME((GV*)dstr));
beab0874
JT
3967 /* Redefining a sub - warning is mandatory if
3968 it was a const and its value changed. */
3969 if (ckWARN(WARN_REDEFINE)
3970 || (CvCONST(cv)
3971 && (!CvCONST((CV*)sref)
3972 || sv_cmp(cv_const_sv(cv),
3973 cv_const_sv((CV*)sref)))))
3974 {
9014280d 3975 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
beab0874 3976 CvCONST(cv)
910764e6
RGS
3977 ? "Constant subroutine %s::%s redefined"
3978 : "Subroutine %s::%s redefined",
bfcb3514 3979 HvNAME_get(GvSTASH((GV*)dstr)),
beab0874
JT
3980 GvENAME((GV*)dstr));
3981 }
9607fc9c 3982 }
fb24441d
RGS
3983 if (!intro)
3984 cv_ckproto(cv, (GV*)dstr,
93524f2b
NC
3985 SvPOK(sref)
3986 ? SvPVX_const(sref) : Nullch);
4633a7c4 3987 }
a5f75d66 3988 GvCV(dstr) = (CV*)sref;
7a4c00b4 3989 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
a5f75d66 3990 GvASSUMECV_on(dstr);
3280af22 3991 PL_sub_generation++;
a5f75d66 3992 }
39bac7f7 3993 if (!GvIMPORTED_CV(dstr)
1d7c1841
GS
3994 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3995 {
a5f75d66 3996 GvIMPORTED_CV_on(dstr);
1d7c1841 3997 }
8990e307 3998 break;
91bba347
LW
3999 case SVt_PVIO:
4000 if (intro)
890ed176 4001 SAVEGENERICSV(GvIOp(dstr));
91bba347
LW
4002 else
4003 dref = (SV*)GvIOp(dstr);
4004 GvIOp(dstr) = (IO*)sref;
4005 break;
f4d13ee9
JH
4006 case SVt_PVFM:
4007 if (intro)
890ed176 4008 SAVEGENERICSV(GvFORM(dstr));
f4d13ee9
JH
4009 else
4010 dref = (SV*)GvFORM(dstr);
4011 GvFORM(dstr) = (CV*)sref;
4012 break;
8990e307 4013 default:
a0d0e21e 4014 if (intro)
890ed176 4015 SAVEGENERICSV(GvSV(dstr));
a0d0e21e
LW
4016 else
4017 dref = (SV*)GvSV(dstr);
8990e307 4018 GvSV(dstr) = sref;
39bac7f7 4019 if (!GvIMPORTED_SV(dstr)
1d7c1841
GS
4020 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4021 {
a5f75d66 4022 GvIMPORTED_SV_on(dstr);
1d7c1841 4023 }
8990e307
LW
4024 break;
4025 }
4026 if (dref)
4027 SvREFCNT_dec(dref);
27c9684d
AP
4028 if (SvTAINTED(sstr))
4029 SvTAINT(dstr);
8990e307
LW
4030 return;
4031 }
3f7c398e 4032 if (SvPVX_const(dstr)) {
8bd4d4c5 4033 SvPV_free(dstr);
b162af07
SP
4034 SvLEN_set(dstr, 0);
4035 SvCUR_set(dstr, 0);
a0d0e21e 4036 }
8990e307 4037 }
a0d0e21e 4038 (void)SvOK_off(dstr);
b162af07 4039 SvRV_set(dstr, SvREFCNT_inc(SvRV(sstr)));
ed6116ce 4040 SvROK_on(dstr);
8990e307 4041 if (sflags & SVp_NOK) {
3332b3c1
JH
4042 SvNOKp_on(dstr);
4043 /* Only set the public OK flag if the source has public OK. */
4044 if (sflags & SVf_NOK)
4045 SvFLAGS(dstr) |= SVf_NOK;
9d6ce603 4046 SvNV_set(dstr, SvNVX(sstr));
ed6116ce 4047 }
8990e307 4048 if (sflags & SVp_IOK) {
3332b3c1
JH
4049 (void)SvIOKp_on(dstr);
4050 if (sflags & SVf_IOK)
4051 SvFLAGS(dstr) |= SVf_IOK;
2b1c7e3e 4052 if (sflags & SVf_IVisUV)
25da4f38 4053 SvIsUV_on(dstr);
45977657 4054 SvIV_set(dstr, SvIVX(sstr));
ed6116ce 4055 }
a0d0e21e
LW
4056 if (SvAMAGIC(sstr)) {
4057 SvAMAGIC_on(dstr);
4058 }
ed6116ce 4059 }
8990e307 4060 else if (sflags & SVp_POK) {
765f542d 4061 bool isSwipe = 0;
79072805
LW
4062
4063 /*
4064 * Check to see if we can just swipe the string. If so, it's a
4065 * possible small lose on short strings, but a big win on long ones.
3f7c398e
SP
4066 * It might even be a win on short strings if SvPVX_const(dstr)
4067 * has to be allocated and SvPVX_const(sstr) has to be freed.
79072805
LW
4068 */
4069
120fac95
NC
4070 /* Whichever path we take through the next code, we want this true,
4071 and doing it now facilitates the COW check. */
4072 (void)SvPOK_only(dstr);
4073
765f542d 4074 if (
b8f9541a
NC
4075 /* We're not already COW */
4076 ((sflags & (SVf_FAKE | SVf_READONLY)) != (SVf_FAKE | SVf_READONLY)
f8c7b90f 4077#ifndef PERL_OLD_COPY_ON_WRITE
b8f9541a
NC
4078 /* or we are, but dstr isn't a suitable target. */
4079 || (SvFLAGS(dstr) & CAN_COW_MASK) != CAN_COW_FLAGS
4080#endif
4081 )
765f542d 4082 &&
765f542d
NC
4083 !(isSwipe =
4084 (sflags & SVs_TEMP) && /* slated for free anyway? */
4085 !(sflags & SVf_OOK) && /* and not involved in OOK hack? */
5fcdf167
NC
4086 (!(flags & SV_NOSTEAL)) &&
4087 /* and we're allowed to steal temps */
765f542d
NC
4088 SvREFCNT(sstr) == 1 && /* and no other references to it? */
4089 SvLEN(sstr) && /* and really is a string */
645c22ef 4090 /* and won't be needed again, potentially */
765f542d 4091 !(PL_op && PL_op->op_type == OP_AASSIGN))
f8c7b90f 4092#ifdef PERL_OLD_COPY_ON_WRITE
765f542d 4093 && !((sflags & CAN_COW_MASK) == CAN_COW_FLAGS
120fac95 4094 && (SvFLAGS(dstr) & CAN_COW_MASK) == CAN_COW_FLAGS
765f542d
NC
4095 && SvTYPE(sstr) >= SVt_PVIV)
4096#endif
4097 ) {
4098 /* Failed the swipe test, and it's not a shared hash key either.
4099 Have to copy the string. */
4100 STRLEN len = SvCUR(sstr);
4101 SvGROW(dstr, len + 1); /* inlined from sv_setpvn */
3f7c398e 4102 Move(SvPVX_const(sstr),SvPVX(dstr),len,char);
765f542d
NC
4103 SvCUR_set(dstr, len);
4104 *SvEND(dstr) = '\0';
765f542d 4105 } else {
f8c7b90f 4106 /* If PERL_OLD_COPY_ON_WRITE is not defined, then isSwipe will always
765f542d 4107 be true in here. */
765f542d
NC
4108 /* Either it's a shared hash key, or it's suitable for
4109 copy-on-write or we can swipe the string. */
46187eeb 4110 if (DEBUG_C_TEST) {
ed252734 4111 PerlIO_printf(Perl_debug_log, "Copy on write: sstr --> dstr\n");
e419cbc5
NC
4112 sv_dump(sstr);
4113 sv_dump(dstr);
46187eeb 4114 }
f8c7b90f 4115#ifdef PERL_OLD_COPY_ON_WRITE
765f542d
NC
4116 if (!isSwipe) {
4117 /* I believe I should acquire a global SV mutex if
4118 it's a COW sv (not a shared hash key) to stop
4119 it going un copy-on-write.
4120 If the source SV has gone un copy on write between up there
4121 and down here, then (assert() that) it is of the correct
4122 form to make it copy on write again */
4123 if ((sflags & (SVf_FAKE | SVf_READONLY))
4124 != (SVf_FAKE | SVf_READONLY)) {
4125 SvREADONLY_on(sstr);
4126 SvFAKE_on(sstr);
4127 /* Make the source SV into a loop of 1.
4128 (about to become 2) */
a29f6d03 4129 SV_COW_NEXT_SV_SET(sstr, sstr);
765f542d
NC
4130 }
4131 }
4132#endif
4133 /* Initial code is common. */
94010e71
NC
4134 if (SvPVX_const(dstr)) { /* we know that dtype >= SVt_PV */
4135 SvPV_free(dstr);
79072805 4136 }
765f542d 4137
765f542d
NC
4138 if (!isSwipe) {
4139 /* making another shared SV. */
4140 STRLEN cur = SvCUR(sstr);
4141 STRLEN len = SvLEN(sstr);
f8c7b90f 4142#ifdef PERL_OLD_COPY_ON_WRITE
765f542d 4143 if (len) {
b8f9541a 4144 assert (SvTYPE(dstr) >= SVt_PVIV);
765f542d
NC
4145 /* SvIsCOW_normal */
4146 /* splice us in between source and next-after-source. */
a29f6d03
NC
4147 SV_COW_NEXT_SV_SET(dstr, SV_COW_NEXT_SV(sstr));
4148 SV_COW_NEXT_SV_SET(sstr, dstr);
940132f3 4149 SvPV_set(dstr, SvPVX_mutable(sstr));
a604c751
NC
4150 } else
4151#endif
4152 {
765f542d 4153 /* SvIsCOW_shared_hash */
46187eeb
NC
4154 DEBUG_C(PerlIO_printf(Perl_debug_log,
4155 "Copy on write: Sharing hash\n"));
b8f9541a 4156
bdd68bc3 4157 assert (SvTYPE(dstr) >= SVt_PV);
765f542d 4158 SvPV_set(dstr,
d1db91c6 4159 HEK_KEY(share_hek_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sstr)))));
bdd68bc3 4160 }
87a1ef3d
SP
4161 SvLEN_set(dstr, len);
4162 SvCUR_set(dstr, cur);
765f542d
NC
4163 SvREADONLY_on(dstr);
4164 SvFAKE_on(dstr);
4165 /* Relesase a global SV mutex. */
4166 }
4167 else
765f542d 4168 { /* Passes the swipe test. */
78d1e721 4169 SvPV_set(dstr, SvPVX_mutable(sstr));
765f542d
NC
4170 SvLEN_set(dstr, SvLEN(sstr));
4171 SvCUR_set(dstr, SvCUR(sstr));
4172
4173 SvTEMP_off(dstr);
4174 (void)SvOK_off(sstr); /* NOTE: nukes most SvFLAGS on sstr */
4175 SvPV_set(sstr, Nullch);
4176 SvLEN_set(sstr, 0);
4177 SvCUR_set(sstr, 0);
4178 SvTEMP_off(sstr);
4179 }
4180 }
9aa983d2 4181 if (sflags & SVf_UTF8)
a7cb1f99 4182 SvUTF8_on(dstr);
8990e307 4183 if (sflags & SVp_NOK) {
3332b3c1
JH
4184 SvNOKp_on(dstr);
4185 if (sflags & SVf_NOK)
4186 SvFLAGS(dstr) |= SVf_NOK;
9d6ce603 4187 SvNV_set(dstr, SvNVX(sstr));
79072805 4188 }
8990e307 4189 if (sflags & SVp_IOK) {
3332b3c1
JH
4190 (void)SvIOKp_on(dstr);
4191 if (sflags & SVf_IOK)
4192 SvFLAGS(dstr) |= SVf_IOK;
2b1c7e3e 4193 if (sflags & SVf_IVisUV)
25da4f38 4194 SvIsUV_on(dstr);
45977657 4195 SvIV_set(dstr, SvIVX(sstr));
79072805 4196 }
92f0c265 4197 if (SvVOK(sstr)) {
7a5fa8a2 4198 MAGIC *smg = mg_find(sstr,PERL_MAGIC_vstring);
ece467f9
JP
4199 sv_magic(dstr, NULL, PERL_MAGIC_vstring,
4200 smg->mg_ptr, smg->mg_len);
439cb1c4 4201 SvRMAGICAL_on(dstr);
7a5fa8a2 4202 }
79072805 4203 }
8990e307 4204 else if (sflags & SVp_IOK) {
3332b3c1
JH
4205 if (sflags & SVf_IOK)
4206 (void)SvIOK_only(dstr);
4207 else {
9cbac4c7
DM
4208 (void)SvOK_off(dstr);
4209 (void)SvIOKp_on(dstr);
3332b3c1
JH
4210 }
4211 /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */
2b1c7e3e 4212 if (sflags & SVf_IVisUV)
25da4f38 4213 SvIsUV_on(dstr);
45977657 4214 SvIV_set(dstr, SvIVX(sstr));
3332b3c1
JH
4215 if (sflags & SVp_NOK) {
4216 if (sflags & SVf_NOK)
4217 (void)SvNOK_on(dstr);
4218 else
4219 (void)SvNOKp_on(dstr);
9d6ce603 4220 SvNV_set(dstr, SvNVX(sstr));
3332b3c1
JH
4221 }
4222 }
4223 else if (sflags & SVp_NOK) {
4224 if (sflags & SVf_NOK)
4225 (void)SvNOK_only(dstr);
4226 else {
9cbac4c7 4227 (void)SvOK_off(dstr);
3332b3c1
JH
4228 SvNOKp_on(dstr);
4229 }
9d6ce603 4230 SvNV_set(dstr, SvNVX(sstr));
79072805
LW
4231 }
4232 else {
20408e3c 4233 if (dtype == SVt_PVGV) {
e476b1b5 4234 if (ckWARN(WARN_MISC))
9014280d 4235 Perl_warner(aTHX_ packWARN(WARN_MISC), "Undefined value assigned to typeglob");
20408e3c
GS
4236 }
4237 else
4238 (void)SvOK_off(dstr);
a0d0e21e 4239 }
27c9684d
AP
4240 if (SvTAINTED(sstr))
4241 SvTAINT(dstr);
79072805
LW
4242}
4243
954c1994
GS
4244/*
4245=for apidoc sv_setsv_mg
4246
4247Like C<sv_setsv>, but also handles 'set' magic.
4248
4249=cut
4250*/
4251
79072805 4252void
864dbfa3 4253Perl_sv_setsv_mg(pTHX_ SV *dstr, register SV *sstr)
ef50df4b
GS
4254{
4255 sv_setsv(dstr,sstr);
4256 SvSETMAGIC(dstr);
4257}
4258
f8c7b90f 4259#ifdef PERL_OLD_COPY_ON_WRITE
ed252734
NC
4260SV *
4261Perl_sv_setsv_cow(pTHX_ SV *dstr, SV *sstr)
4262{
4263 STRLEN cur = SvCUR(sstr);
4264 STRLEN len = SvLEN(sstr);
4265 register char *new_pv;
4266
4267 if (DEBUG_C_TEST) {
4268 PerlIO_printf(Perl_debug_log, "Fast copy on write: %p -> %p\n",
4269 sstr, dstr);
4270 sv_dump(sstr);
4271 if (dstr)
4272 sv_dump(dstr);
4273 }
4274
4275 if (dstr) {
4276 if (SvTHINKFIRST(dstr))
4277 sv_force_normal_flags(dstr, SV_COW_DROP_PV);
3f7c398e
SP
4278 else if (SvPVX_const(dstr))
4279 Safefree(SvPVX_const(dstr));
ed252734
NC
4280 }
4281 else
4282 new_SV(dstr);
862a34c6 4283 SvUPGRADE(dstr, SVt_PVIV);
ed252734
NC
4284
4285 assert (SvPOK(sstr));
4286 assert (SvPOKp(sstr));
4287 assert (!SvIOK(sstr));
4288 assert (!SvIOKp(sstr));
4289 assert (!SvNOK(sstr));
4290 assert (!SvNOKp(sstr));
4291
4292 if (SvIsCOW(sstr)) {
4293
4294 if (SvLEN(sstr) == 0) {
4295 /* source is a COW shared hash key. */
ed252734
NC
4296 DEBUG_C(PerlIO_printf(Perl_debug_log,
4297 "Fast copy on write: Sharing hash\n"));
d1db91c6 4298 new_pv = HEK_KEY(share_hek_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sstr))));
ed252734
NC
4299 goto common_exit;
4300 }
4301 SV_COW_NEXT_SV_SET(dstr, SV_COW_NEXT_SV(sstr));
4302 } else {
4303 assert ((SvFLAGS(sstr) & CAN_COW_MASK) == CAN_COW_FLAGS);
862a34c6 4304 SvUPGRADE(sstr, SVt_PVIV);
ed252734
NC
4305 SvREADONLY_on(sstr);
4306 SvFAKE_on(sstr);
4307 DEBUG_C(PerlIO_printf(Perl_debug_log,
4308 "Fast copy on write: Converting sstr to COW\n"));
4309 SV_COW_NEXT_SV_SET(dstr, sstr);
4310 }
4311 SV_COW_NEXT_SV_SET(sstr, dstr);
940132f3 4312 new_pv = SvPVX_mutable(sstr);
ed252734
NC
4313
4314 common_exit:
4315 SvPV_set(dstr, new_pv);
4316 SvFLAGS(dstr) = (SVt_PVIV|SVf_POK|SVp_POK|SVf_FAKE|SVf_READONLY);
4317 if (SvUTF8(sstr))
4318 SvUTF8_on(dstr);
87a1ef3d
SP
4319 SvLEN_set(dstr, len);
4320 SvCUR_set(dstr, cur);
ed252734
NC
4321 if (DEBUG_C_TEST) {
4322 sv_dump(dstr);
4323 }
4324 return dstr;
4325}
4326#endif
4327
954c1994
GS
4328/*
4329=for apidoc sv_setpvn
4330
4331Copies a string into an SV. The C<len> parameter indicates the number of
9e09f5f2
MHM
4332bytes to be copied. If the C<ptr> argument is NULL the SV will become
4333undefined. Does not handle 'set' magic. See C<sv_setpvn_mg>.
954c1994
GS
4334
4335=cut
4336*/
4337
ef50df4b 4338void
864dbfa3 4339Perl_sv_setpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
79072805 4340{
c6f8c383 4341 register char *dptr;
22c522df 4342
765f542d 4343 SV_CHECK_THINKFIRST_COW_DROP(sv);
463ee0b2 4344 if (!ptr) {
a0d0e21e 4345 (void)SvOK_off(sv);
463ee0b2
LW
4346 return;
4347 }
22c522df
JH
4348 else {
4349 /* len is STRLEN which is unsigned, need to copy to signed */
a3b680e6 4350 const IV iv = len;
9c5ffd7c
JH
4351 if (iv < 0)
4352 Perl_croak(aTHX_ "panic: sv_setpvn called with negative strlen");
22c522df 4353 }
862a34c6 4354 SvUPGRADE(sv, SVt_PV);
c6f8c383 4355
5902b6a9 4356 dptr = SvGROW(sv, len + 1);
c6f8c383
GA
4357 Move(ptr,dptr,len,char);
4358 dptr[len] = '\0';
79072805 4359 SvCUR_set(sv, len);
1aa99e6b 4360 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2 4361 SvTAINT(sv);
79072805
LW
4362}
4363
954c1994
GS
4364/*
4365=for apidoc sv_setpvn_mg
4366
4367Like C<sv_setpvn>, but also handles 'set' magic.
4368
4369=cut
4370*/
4371
79072805 4372void
864dbfa3 4373Perl_sv_setpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
ef50df4b
GS
4374{
4375 sv_setpvn(sv,ptr,len);
4376 SvSETMAGIC(sv);
4377}
4378
954c1994
GS
4379/*
4380=for apidoc sv_setpv
4381
4382Copies a string into an SV. The string must be null-terminated. Does not
4383handle 'set' magic. See C<sv_setpv_mg>.
4384
4385=cut
4386*/
4387
ef50df4b 4388void
864dbfa3 4389Perl_sv_setpv(pTHX_ register SV *sv, register const char *ptr)
79072805
LW
4390{
4391 register STRLEN len;
4392
765f542d 4393 SV_CHECK_THINKFIRST_COW_DROP(sv);
463ee0b2 4394 if (!ptr) {
a0d0e21e 4395 (void)SvOK_off(sv);
463ee0b2
LW
4396 return;
4397 }
79072805 4398 len = strlen(ptr);
862a34c6 4399 SvUPGRADE(sv, SVt_PV);
c6f8c383 4400
79072805 4401 SvGROW(sv, len + 1);
463ee0b2 4402 Move(ptr,SvPVX(sv),len+1,char);
79072805 4403 SvCUR_set(sv, len);
1aa99e6b 4404 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2
LW
4405 SvTAINT(sv);
4406}
4407
954c1994
GS
4408/*
4409=for apidoc sv_setpv_mg
4410
4411Like C<sv_setpv>, but also handles 'set' magic.
4412
4413=cut
4414*/
4415
463ee0b2 4416void
864dbfa3 4417Perl_sv_setpv_mg(pTHX_ register SV *sv, register const char *ptr)
ef50df4b
GS
4418{
4419 sv_setpv(sv,ptr);
4420 SvSETMAGIC(sv);
4421}
4422
954c1994
GS
4423/*
4424=for apidoc sv_usepvn
4425
4426Tells an SV to use C<ptr> to find its string value. Normally the string is
1c846c1f 4427stored inside the SV but sv_usepvn allows the SV to use an outside string.
954c1994
GS
4428The C<ptr> should point to memory that was allocated by C<malloc>. The
4429string length, C<len>, must be supplied. This function will realloc the
4430memory pointed to by C<ptr>, so that pointer should not be freed or used by
4431the programmer after giving it to sv_usepvn. Does not handle 'set' magic.
4432See C<sv_usepvn_mg>.
4433
4434=cut
4435*/
4436
ef50df4b 4437void
864dbfa3 4438Perl_sv_usepvn(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
463ee0b2 4439{
1936d2a7 4440 STRLEN allocate;
765f542d 4441 SV_CHECK_THINKFIRST_COW_DROP(sv);
862a34c6 4442 SvUPGRADE(sv, SVt_PV);
463ee0b2 4443 if (!ptr) {
a0d0e21e 4444 (void)SvOK_off(sv);
463ee0b2
LW
4445 return;
4446 }
3f7c398e 4447 if (SvPVX_const(sv))
8bd4d4c5 4448 SvPV_free(sv);
1936d2a7
NC
4449
4450 allocate = PERL_STRLEN_ROUNDUP(len + 1);
7a9b70e9 4451 ptr = saferealloc (ptr, allocate);
f880fe2f 4452 SvPV_set(sv, ptr);
463ee0b2 4453 SvCUR_set(sv, len);
1936d2a7 4454 SvLEN_set(sv, allocate);
463ee0b2 4455 *SvEND(sv) = '\0';
1aa99e6b 4456 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2 4457 SvTAINT(sv);
79072805
LW
4458}
4459
954c1994
GS
4460/*
4461=for apidoc sv_usepvn_mg
4462
4463Like C<sv_usepvn>, but also handles 'set' magic.
4464
4465=cut
4466*/
4467
ef50df4b 4468void
864dbfa3 4469Perl_sv_usepvn_mg(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
ef50df4b 4470{
51c1089b 4471 sv_usepvn(sv,ptr,len);
ef50df4b
GS
4472 SvSETMAGIC(sv);
4473}
4474
f8c7b90f 4475#ifdef PERL_OLD_COPY_ON_WRITE
765f542d
NC
4476/* Need to do this *after* making the SV normal, as we need the buffer
4477 pointer to remain valid until after we've copied it. If we let go too early,
4478 another thread could invalidate it by unsharing last of the same hash key
4479 (which it can do by means other than releasing copy-on-write Svs)
4480 or by changing the other copy-on-write SVs in the loop. */
4481STATIC void
bdd68bc3 4482S_sv_release_COW(pTHX_ register SV *sv, const char *pvx, STRLEN len, SV *after)
765f542d
NC
4483{
4484 if (len) { /* this SV was SvIsCOW_normal(sv) */
4485 /* we need to find the SV pointing to us. */
4486 SV *current = SV_COW_NEXT_SV(after);
7a5fa8a2 4487
765f542d
NC
4488 if (current == sv) {
4489 /* The SV we point to points back to us (there were only two of us
4490 in the loop.)
4491 Hence other SV is no longer copy on write either. */
4492 SvFAKE_off(after);
4493 SvREADONLY_off(after);
4494 } else {
4495 /* We need to follow the pointers around the loop. */
4496 SV *next;
4497 while ((next = SV_COW_NEXT_SV(current)) != sv) {
4498 assert (next);
4499 current = next;
4500 /* don't loop forever if the structure is bust, and we have
4501 a pointer into a closed loop. */
4502 assert (current != after);
3f7c398e 4503 assert (SvPVX_const(current) == pvx);
765f542d
NC
4504 }
4505 /* Make the SV before us point to the SV after us. */
a29f6d03 4506 SV_COW_NEXT_SV_SET(current, after);
765f542d
NC
4507 }
4508 } else {
bdd68bc3 4509 unshare_hek(SvSHARED_HEK_FROM_PV(pvx));
765f542d
NC
4510 }
4511}
4512
4513int
4514Perl_sv_release_IVX(pTHX_ register SV *sv)
4515{
4516 if (SvIsCOW(sv))
4517 sv_force_normal_flags(sv, 0);
0c34ef67
MHM
4518 SvOOK_off(sv);
4519 return 0;
765f542d
NC
4520}
4521#endif
645c22ef
DM
4522/*
4523=for apidoc sv_force_normal_flags
4524
4525Undo various types of fakery on an SV: if the PV is a shared string, make
4526a private copy; if we're a ref, stop refing; if we're a glob, downgrade to
765f542d
NC
4527an xpvmg; if we're a copy-on-write scalar, this is the on-write time when
4528we do the copy, and is also used locally. If C<SV_COW_DROP_PV> is set
4529then a copy-on-write scalar drops its PV buffer (if any) and becomes
4530SvPOK_off rather than making a copy. (Used where this scalar is about to be
d3050d9d 4531set to some other value.) In addition, the C<flags> parameter gets passed to
765f542d
NC
4532C<sv_unref_flags()> when unrefing. C<sv_force_normal> calls this function
4533with flags set to 0.
645c22ef
DM
4534
4535=cut
4536*/
4537
6fc92669 4538void
840a7b70 4539Perl_sv_force_normal_flags(pTHX_ register SV *sv, U32 flags)
0f15f207 4540{
f8c7b90f 4541#ifdef PERL_OLD_COPY_ON_WRITE
765f542d
NC
4542 if (SvREADONLY(sv)) {
4543 /* At this point I believe I should acquire a global SV mutex. */
4544 if (SvFAKE(sv)) {
a28509cc
AL
4545 const char *pvx = SvPVX_const(sv);
4546 const STRLEN len = SvLEN(sv);
4547 const STRLEN cur = SvCUR(sv);
a28509cc 4548 SV * const next = SV_COW_NEXT_SV(sv); /* next COW sv in the loop. */
46187eeb
NC
4549 if (DEBUG_C_TEST) {
4550 PerlIO_printf(Perl_debug_log,
4551 "Copy on write: Force normal %ld\n",
4552 (long) flags);
e419cbc5 4553 sv_dump(sv);
46187eeb 4554 }
765f542d
NC
4555 SvFAKE_off(sv);
4556 SvREADONLY_off(sv);
4557 /* This SV doesn't own the buffer, so need to New() a new one: */
f880fe2f 4558 SvPV_set(sv, (char*)0);
87a1ef3d 4559 SvLEN_set(sv, 0);
765f542d
NC
4560 if (flags & SV_COW_DROP_PV) {
4561 /* OK, so we don't need to copy our buffer. */
4562 SvPOK_off(sv);
4563 } else {
4564 SvGROW(sv, cur + 1);
4565 Move(pvx,SvPVX(sv),cur,char);
87a1ef3d 4566 SvCUR_set(sv, cur);
765f542d
NC
4567 *SvEND(sv) = '\0';
4568 }
bdd68bc3 4569 sv_release_COW(sv, pvx, len, next);
46187eeb 4570 if (DEBUG_C_TEST) {
e419cbc5 4571 sv_dump(sv);
46187eeb 4572 }
765f542d 4573 }
923e4eb5 4574 else if (IN_PERL_RUNTIME)
765f542d
NC
4575 Perl_croak(aTHX_ PL_no_modify);
4576 /* At this point I believe that I can drop the global SV mutex. */
4577 }
4578#else
2213622d 4579 if (SvREADONLY(sv)) {
1c846c1f 4580 if (SvFAKE(sv)) {
a433f3d2 4581 const char *pvx = SvPVX_const(sv);
66a1b24b 4582 const STRLEN len = SvCUR(sv);
10bcdfd6
NC
4583 SvFAKE_off(sv);
4584 SvREADONLY_off(sv);
66a1b24b
AL
4585 SvPV_set(sv, Nullch);
4586 SvLEN_set(sv, 0);
1c846c1f 4587 SvGROW(sv, len + 1);
3f7c398e 4588 Move(pvx,SvPVX_const(sv),len,char);
1c846c1f 4589 *SvEND(sv) = '\0';
bdd68bc3 4590 unshare_hek(SvSHARED_HEK_FROM_PV(pvx));
1c846c1f 4591 }
923e4eb5 4592 else if (IN_PERL_RUNTIME)
cea2e8a9 4593 Perl_croak(aTHX_ PL_no_modify);
0f15f207 4594 }
765f542d 4595#endif
2213622d 4596 if (SvROK(sv))
840a7b70 4597 sv_unref_flags(sv, flags);
6fc92669
GS
4598 else if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV)
4599 sv_unglob(sv);
0f15f207 4600}
1c846c1f 4601
645c22ef
DM
4602/*
4603=for apidoc sv_force_normal
4604
4605Undo various types of fakery on an SV: if the PV is a shared string, make
4606a private copy; if we're a ref, stop refing; if we're a glob, downgrade to
4607an xpvmg. See also C<sv_force_normal_flags>.
4608
4609=cut
4610*/
4611
840a7b70
IZ
4612void
4613Perl_sv_force_normal(pTHX_ register SV *sv)
4614{
4615 sv_force_normal_flags(sv, 0);
4616}
4617
954c1994
GS
4618/*
4619=for apidoc sv_chop
4620
1c846c1f 4621Efficient removal of characters from the beginning of the string buffer.
954c1994
GS
4622SvPOK(sv) must be true and the C<ptr> must be a pointer to somewhere inside
4623the string buffer. The C<ptr> becomes the first character of the adjusted
645c22ef 4624string. Uses the "OOK hack".
3f7c398e 4625Beware: after this function returns, C<ptr> and SvPVX_const(sv) may no longer
31869a79 4626refer to the same chunk of data.
954c1994
GS
4627
4628=cut
4629*/
4630
79072805 4631void
f54cb97a 4632Perl_sv_chop(pTHX_ register SV *sv, register const char *ptr)
79072805
LW
4633{
4634 register STRLEN delta;
a0d0e21e 4635 if (!ptr || !SvPOKp(sv))
79072805 4636 return;
3f7c398e 4637 delta = ptr - SvPVX_const(sv);
2213622d 4638 SV_CHECK_THINKFIRST(sv);
79072805
LW
4639 if (SvTYPE(sv) < SVt_PVIV)
4640 sv_upgrade(sv,SVt_PVIV);
4641
4642 if (!SvOOK(sv)) {
50483b2c 4643 if (!SvLEN(sv)) { /* make copy of shared string */
3f7c398e 4644 const char *pvx = SvPVX_const(sv);
a28509cc 4645 const STRLEN len = SvCUR(sv);
50483b2c 4646 SvGROW(sv, len + 1);
3f7c398e 4647 Move(pvx,SvPVX_const(sv),len,char);
50483b2c
JD
4648 *SvEND(sv) = '\0';
4649 }
45977657 4650 SvIV_set(sv, 0);
a4bfb290
AB
4651 /* Same SvOOK_on but SvOOK_on does a SvIOK_off
4652 and we do that anyway inside the SvNIOK_off
4653 */
7a5fa8a2 4654 SvFLAGS(sv) |= SVf_OOK;
79072805 4655 }
a4bfb290 4656 SvNIOK_off(sv);
b162af07
SP
4657 SvLEN_set(sv, SvLEN(sv) - delta);
4658 SvCUR_set(sv, SvCUR(sv) - delta);
f880fe2f 4659 SvPV_set(sv, SvPVX(sv) + delta);
45977657 4660 SvIV_set(sv, SvIVX(sv) + delta);
79072805
LW
4661}
4662
09540bc3
JH
4663/* sv_catpvn() is now a macro using Perl_sv_catpvn_flags();
4664 * this function provided for binary compatibility only
4665 */
4666
4667void
4668Perl_sv_catpvn(pTHX_ SV *dsv, const char* sstr, STRLEN slen)
4669{
4670 sv_catpvn_flags(dsv, sstr, slen, SV_GMAGIC);
4671}
4672
954c1994
GS
4673/*
4674=for apidoc sv_catpvn
4675
4676Concatenates the string onto the end of the string which is in the SV. The
1e54db1a
JH
4677C<len> indicates number of bytes to copy. If the SV has the UTF-8
4678status set, then the bytes appended should be valid UTF-8.
d5ce4a7c 4679Handles 'get' magic, but not 'set' magic. See C<sv_catpvn_mg>.
954c1994 4680
8d6d96c1
HS
4681=for apidoc sv_catpvn_flags
4682
4683Concatenates the string onto the end of the string which is in the SV. The
1e54db1a
JH
4684C<len> indicates number of bytes to copy. If the SV has the UTF-8
4685status set, then the bytes appended should be valid UTF-8.
8d6d96c1
HS
4686If C<flags> has C<SV_GMAGIC> bit set, will C<mg_get> on C<dsv> if
4687appropriate, else not. C<sv_catpvn> and C<sv_catpvn_nomg> are implemented
4688in terms of this function.
4689
4690=cut
4691*/
4692
4693void
4694Perl_sv_catpvn_flags(pTHX_ register SV *dsv, register const char *sstr, register STRLEN slen, I32 flags)
4695{
4696 STRLEN dlen;
f54cb97a 4697 const char *dstr = SvPV_force_flags(dsv, dlen, flags);
8d6d96c1 4698
8d6d96c1
HS
4699 SvGROW(dsv, dlen + slen + 1);
4700 if (sstr == dstr)
3f7c398e 4701 sstr = SvPVX_const(dsv);
8d6d96c1 4702 Move(sstr, SvPVX(dsv) + dlen, slen, char);
b162af07 4703 SvCUR_set(dsv, SvCUR(dsv) + slen);
8d6d96c1
HS
4704 *SvEND(dsv) = '\0';
4705 (void)SvPOK_only_UTF8(dsv); /* validate pointer */
4706 SvTAINT(dsv);
79072805
LW
4707}
4708
954c1994
GS
4709/*
4710=for apidoc sv_catpvn_mg
4711
4712Like C<sv_catpvn>, but also handles 'set' magic.
4713
4714=cut
4715*/
4716
79072805 4717void
864dbfa3 4718Perl_sv_catpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
ef50df4b
GS
4719{
4720 sv_catpvn(sv,ptr,len);
4721 SvSETMAGIC(sv);
4722}
4723
09540bc3
JH
4724/* sv_catsv() is now a macro using Perl_sv_catsv_flags();
4725 * this function provided for binary compatibility only
4726 */
4727
4728void
4729Perl_sv_catsv(pTHX_ SV *dstr, register SV *sstr)
4730{
4731 sv_catsv_flags(dstr, sstr, SV_GMAGIC);
4732}
4733
954c1994
GS
4734/*
4735=for apidoc sv_catsv
4736
13e8c8e3
JH
4737Concatenates the string from SV C<ssv> onto the end of the string in
4738SV C<dsv>. Modifies C<dsv> but not C<ssv>. Handles 'get' magic, but
4739not 'set' magic. See C<sv_catsv_mg>.
954c1994 4740
8d6d96c1
HS
4741=for apidoc sv_catsv_flags
4742
4743Concatenates the string from SV C<ssv> onto the end of the string in
4744SV C<dsv>. Modifies C<dsv> but not C<ssv>. If C<flags> has C<SV_GMAGIC>
4745bit set, will C<mg_get> on the SVs if appropriate, else not. C<sv_catsv>
4746and C<sv_catsv_nomg> are implemented in terms of this function.
4747
4748=cut */
4749
ef50df4b 4750void
8d6d96c1 4751Perl_sv_catsv_flags(pTHX_ SV *dsv, register SV *ssv, I32 flags)
79072805 4752{
4d84ee25 4753 const char *spv;
13e8c8e3 4754 STRLEN slen;
46199a12 4755 if (!ssv)
79072805 4756 return;
4d84ee25 4757 if ((spv = SvPV_const(ssv, slen))) {
4fd84b44
AD
4758 /* sutf8 and dutf8 were type bool, but under USE_ITHREADS,
4759 gcc version 2.95.2 20000220 (Debian GNU/Linux) for
8cf8f3d1
NIS
4760 Linux xxx 2.2.17 on sparc64 with gcc -O2, we erroneously
4761 get dutf8 = 0x20000000, (i.e. SVf_UTF8) even though
4fd84b44
AD
4762 dsv->sv_flags doesn't have that bit set.
4763 Andy Dougherty 12 Oct 2001
4764 */
b464bac0 4765 const I32 sutf8 = DO_UTF8(ssv);
4fd84b44 4766 I32 dutf8;
13e8c8e3 4767
8d6d96c1
HS
4768 if (SvGMAGICAL(dsv) && (flags & SV_GMAGIC))
4769 mg_get(dsv);
4770 dutf8 = DO_UTF8(dsv);
4771
4772 if (dutf8 != sutf8) {
13e8c8e3 4773 if (dutf8) {
46199a12 4774 /* Not modifying source SV, so taking a temporary copy. */
8d6d96c1 4775 SV* csv = sv_2mortal(newSVpvn(spv, slen));
13e8c8e3 4776
46199a12 4777 sv_utf8_upgrade(csv);
93524f2b 4778 spv = SvPV_const(csv, slen);
13e8c8e3 4779 }
8d6d96c1
HS
4780 else
4781 sv_utf8_upgrade_nomg(dsv);
e84ff256 4782 }
8d6d96c1 4783 sv_catpvn_nomg(dsv, spv, slen);
560a288e 4784 }
79072805
LW
4785}
4786
954c1994
GS
4787/*
4788=for apidoc sv_catsv_mg
4789
4790Like C<sv_catsv>, but also handles 'set' magic.
4791
4792=cut
4793*/
4794
79072805 4795void
46199a12 4796Perl_sv_catsv_mg(pTHX_ SV *dsv, register SV *ssv)
ef50df4b 4797{
46199a12
JH
4798 sv_catsv(dsv,ssv);
4799 SvSETMAGIC(dsv);
ef50df4b
GS
4800}
4801
954c1994
GS
4802/*
4803=for apidoc sv_catpv
4804
4805Concatenates the string onto the end of the string which is in the SV.
1e54db1a
JH
4806If the SV has the UTF-8 status set, then the bytes appended should be
4807valid UTF-8. Handles 'get' magic, but not 'set' magic. See C<sv_catpv_mg>.
954c1994 4808
d5ce4a7c 4809=cut */
954c1994 4810
ef50df4b 4811void
0c981600 4812Perl_sv_catpv(pTHX_ register SV *sv, register const char *ptr)
79072805
LW
4813{
4814 register STRLEN len;
463ee0b2 4815 STRLEN tlen;
748a9306 4816 char *junk;
79072805 4817
0c981600 4818 if (!ptr)
79072805 4819 return;
748a9306 4820 junk = SvPV_force(sv, tlen);
0c981600 4821 len = strlen(ptr);
463ee0b2 4822 SvGROW(sv, tlen + len + 1);
0c981600 4823 if (ptr == junk)
3f7c398e 4824 ptr = SvPVX_const(sv);
0c981600 4825 Move(ptr,SvPVX(sv)+tlen,len+1,char);
b162af07 4826 SvCUR_set(sv, SvCUR(sv) + len);
d41ff1b8 4827 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2 4828 SvTAINT(sv);
79072805
LW
4829}
4830
954c1994
GS
4831/*
4832=for apidoc sv_catpv_mg
4833
4834Like C<sv_catpv>, but also handles 'set' magic.
4835
4836=cut
4837*/
4838
ef50df4b 4839void
0c981600 4840Perl_sv_catpv_mg(pTHX_ register SV *sv, register const char *ptr)
ef50df4b 4841{
0c981600 4842 sv_catpv(sv,ptr);
ef50df4b
GS
4843 SvSETMAGIC(sv);
4844}
4845
645c22ef
DM
4846/*
4847=for apidoc newSV
4848
4849Create a new null SV, or if len > 0, create a new empty SVt_PV type SV
4850with an initial PV allocation of len+1. Normally accessed via the C<NEWSV>
4851macro.
4852
4853=cut
4854*/
4855
79072805 4856SV *
864dbfa3 4857Perl_newSV(pTHX_ STRLEN len)
79072805
LW
4858{
4859 register SV *sv;
1c846c1f 4860
4561caa4 4861 new_SV(sv);
79072805
LW
4862 if (len) {
4863 sv_upgrade(sv, SVt_PV);
4864 SvGROW(sv, len + 1);
4865 }
4866 return sv;
4867}
954c1994 4868/*
92110913 4869=for apidoc sv_magicext
954c1994 4870
68795e93 4871Adds magic to an SV, upgrading it if necessary. Applies the
2d8d5d5a 4872supplied vtable and returns a pointer to the magic added.
92110913 4873
2d8d5d5a
SH
4874Note that C<sv_magicext> will allow things that C<sv_magic> will not.
4875In particular, you can add magic to SvREADONLY SVs, and add more than
4876one instance of the same 'how'.
645c22ef 4877
2d8d5d5a
SH
4878If C<namlen> is greater than zero then a C<savepvn> I<copy> of C<name> is
4879stored, if C<namlen> is zero then C<name> is stored as-is and - as another
4880special case - if C<(name && namlen == HEf_SVKEY)> then C<name> is assumed
4881to contain an C<SV*> and is stored as-is with its REFCNT incremented.
92110913 4882
2d8d5d5a 4883(This is now used as a subroutine by C<sv_magic>.)
954c1994
GS
4884
4885=cut
4886*/
92110913 4887MAGIC *
e1ec3a88 4888Perl_sv_magicext(pTHX_ SV* sv, SV* obj, int how, const MGVTBL *vtable,
92110913 4889 const char* name, I32 namlen)
79072805
LW
4890{
4891 MAGIC* mg;
68795e93 4892
92110913 4893 if (SvTYPE(sv) < SVt_PVMG) {
862a34c6 4894 SvUPGRADE(sv, SVt_PVMG);
463ee0b2 4895 }
79072805
LW
4896 Newz(702,mg, 1, MAGIC);
4897 mg->mg_moremagic = SvMAGIC(sv);
b162af07 4898 SvMAGIC_set(sv, mg);
75f9d97a 4899
05f95b08
SB
4900 /* Sometimes a magic contains a reference loop, where the sv and
4901 object refer to each other. To prevent a reference loop that
4902 would prevent such objects being freed, we look for such loops
4903 and if we find one we avoid incrementing the object refcount.
87f0b213
JH
4904
4905 Note we cannot do this to avoid self-tie loops as intervening RV must
b5ccf5f2 4906 have its REFCNT incremented to keep it in existence.
87f0b213
JH
4907
4908 */
14befaf4
DM
4909 if (!obj || obj == sv ||
4910 how == PERL_MAGIC_arylen ||
4911 how == PERL_MAGIC_qr ||
8d2f4536 4912 how == PERL_MAGIC_symtab ||
75f9d97a
JH
4913 (SvTYPE(obj) == SVt_PVGV &&
4914 (GvSV(obj) == sv || GvHV(obj) == (HV*)sv || GvAV(obj) == (AV*)sv ||
4915 GvCV(obj) == (CV*)sv || GvIOp(obj) == (IO*)sv ||
2628be26 4916 GvFORM(obj) == (CV*)sv)))
75f9d97a 4917 {
8990e307 4918 mg->mg_obj = obj;
75f9d97a 4919 }
85e6fe83 4920 else {
8990e307 4921 mg->mg_obj = SvREFCNT_inc(obj);
85e6fe83
LW
4922 mg->mg_flags |= MGf_REFCOUNTED;
4923 }
b5ccf5f2
YST
4924
4925 /* Normal self-ties simply pass a null object, and instead of
4926 using mg_obj directly, use the SvTIED_obj macro to produce a
4927 new RV as needed. For glob "self-ties", we are tieing the PVIO
4928 with an RV obj pointing to the glob containing the PVIO. In
4929 this case, to avoid a reference loop, we need to weaken the
4930 reference.
4931 */
4932
4933 if (how == PERL_MAGIC_tiedscalar && SvTYPE(sv) == SVt_PVIO &&
4934 obj && SvROK(obj) && GvIO(SvRV(obj)) == (IO*)sv)
4935 {
4936 sv_rvweaken(obj);
4937 }
4938
79072805 4939 mg->mg_type = how;
565764a8 4940 mg->mg_len = namlen;
9cbac4c7 4941 if (name) {
92110913 4942 if (namlen > 0)
1edc1566 4943 mg->mg_ptr = savepvn(name, namlen);
c6ee37c5 4944 else if (namlen == HEf_SVKEY)
1edc1566 4945 mg->mg_ptr = (char*)SvREFCNT_inc((SV*)name);
68795e93 4946 else
92110913 4947 mg->mg_ptr = (char *) name;
9cbac4c7 4948 }
92110913 4949 mg->mg_virtual = vtable;
68795e93 4950
92110913
NIS
4951 mg_magical(sv);
4952 if (SvGMAGICAL(sv))
4953 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
4954 return mg;
4955}
4956
4957/*
4958=for apidoc sv_magic
1c846c1f 4959
92110913
NIS
4960Adds magic to an SV. First upgrades C<sv> to type C<SVt_PVMG> if necessary,
4961then adds a new magic item of type C<how> to the head of the magic list.
4962
2d8d5d5a
SH
4963See C<sv_magicext> (which C<sv_magic> now calls) for a description of the
4964handling of the C<name> and C<namlen> arguments.
4965
4509d3fb
SB
4966You need to use C<sv_magicext> to add magic to SvREADONLY SVs and also
4967to add more than one instance of the same 'how'.
4968
92110913
NIS
4969=cut
4970*/
4971
4972void
4973Perl_sv_magic(pTHX_ register SV *sv, SV *obj, int how, const char *name, I32 namlen)
68795e93 4974{
e1ec3a88 4975 const MGVTBL *vtable = 0;
92110913 4976 MAGIC* mg;
92110913 4977
f8c7b90f 4978#ifdef PERL_OLD_COPY_ON_WRITE
765f542d
NC
4979 if (SvIsCOW(sv))
4980 sv_force_normal_flags(sv, 0);
4981#endif
92110913 4982 if (SvREADONLY(sv)) {
923e4eb5 4983 if (IN_PERL_RUNTIME
92110913
NIS
4984 && how != PERL_MAGIC_regex_global
4985 && how != PERL_MAGIC_bm
4986 && how != PERL_MAGIC_fm
4987 && how != PERL_MAGIC_sv
e6469971 4988 && how != PERL_MAGIC_backref
92110913
NIS
4989 )
4990 {
4991 Perl_croak(aTHX_ PL_no_modify);
4992 }
4993 }
4994 if (SvMAGICAL(sv) || (how == PERL_MAGIC_taint && SvTYPE(sv) >= SVt_PVMG)) {
4995 if (SvMAGIC(sv) && (mg = mg_find(sv, how))) {
68795e93
NIS
4996 /* sv_magic() refuses to add a magic of the same 'how' as an
4997 existing one
92110913
NIS
4998 */
4999 if (how == PERL_MAGIC_taint)
5000 mg->mg_len |= 1;
5001 return;
5002 }
5003 }
68795e93 5004
79072805 5005 switch (how) {
14befaf4 5006 case PERL_MAGIC_sv:
92110913 5007 vtable = &PL_vtbl_sv;
79072805 5008 break;
14befaf4 5009 case PERL_MAGIC_overload:
92110913 5010 vtable = &PL_vtbl_amagic;
a0d0e21e 5011 break;
14befaf4 5012 case PERL_MAGIC_overload_elem:
92110913 5013 vtable = &PL_vtbl_amagicelem;
a0d0e21e 5014 break;
14befaf4 5015 case PERL_MAGIC_overload_table:
92110913 5016 vtable = &PL_vtbl_ovrld;
a0d0e21e 5017 break;
14befaf4 5018 case PERL_MAGIC_bm:
92110913 5019 vtable = &PL_vtbl_bm;
79072805 5020 break;
14befaf4 5021 case PERL_MAGIC_regdata:
92110913 5022 vtable = &PL_vtbl_regdata;
6cef1e77 5023 break;
14befaf4 5024 case PERL_MAGIC_regdatum:
92110913 5025 vtable = &PL_vtbl_regdatum;
6cef1e77 5026 break;
14befaf4 5027 case PERL_MAGIC_env:
92110913 5028 vtable = &PL_vtbl_env;
79072805 5029 break;
14befaf4 5030 case PERL_MAGIC_fm:
92110913 5031 vtable = &PL_vtbl_fm;
55497cff 5032 break;
14befaf4 5033 case PERL_MAGIC_envelem:
92110913 5034 vtable = &PL_vtbl_envelem;
79072805 5035 break;
14befaf4 5036 case PERL_MAGIC_regex_global:
92110913 5037 vtable = &PL_vtbl_mglob;
93a17b20 5038 break;
14befaf4 5039 case PERL_MAGIC_isa:
92110913 5040 vtable = &PL_vtbl_isa;
463ee0b2 5041 break;
14befaf4 5042 case PERL_MAGIC_isaelem:
92110913 5043 vtable = &PL_vtbl_isaelem;
463ee0b2 5044 break;
14befaf4 5045 case PERL_MAGIC_nkeys:
92110913 5046 vtable = &PL_vtbl_nkeys;
16660edb 5047 break;
14befaf4 5048 case PERL_MAGIC_dbfile:
92110913 5049 vtable = 0;
93a17b20 5050 break;
14befaf4 5051 case PERL_MAGIC_dbline:
92110913 5052 vtable = &PL_vtbl_dbline;
79072805 5053 break;
36477c24 5054#ifdef USE_LOCALE_COLLATE
14befaf4 5055 case PERL_MAGIC_collxfrm:
92110913 5056 vtable = &PL_vtbl_collxfrm;
bbce6d69 5057 break;
36477c24 5058#endif /* USE_LOCALE_COLLATE */
14befaf4 5059 case PERL_MAGIC_tied:
92110913 5060 vtable = &PL_vtbl_pack;
463ee0b2 5061 break;
14befaf4
DM
5062 case PERL_MAGIC_tiedelem:
5063 case PERL_MAGIC_tiedscalar:
92110913 5064 vtable = &PL_vtbl_packelem;
463ee0b2 5065 break;
14befaf4 5066 case PERL_MAGIC_qr:
92110913 5067 vtable = &PL_vtbl_regexp;
c277df42 5068 break;
14befaf4 5069 case PERL_MAGIC_sig:
92110913 5070 vtable = &PL_vtbl_sig;
79072805 5071 break;
14befaf4 5072 case PERL_MAGIC_sigelem:
92110913 5073 vtable = &PL_vtbl_sigelem;
79072805 5074 break;
14befaf4 5075 case PERL_MAGIC_taint:
92110913 5076 vtable = &PL_vtbl_taint;
463ee0b2 5077 break;
14befaf4 5078 case PERL_MAGIC_uvar:
92110913 5079 vtable = &PL_vtbl_uvar;
79072805 5080 break;
14befaf4 5081 case PERL_MAGIC_vec:
92110913 5082 vtable = &PL_vtbl_vec;
79072805 5083 break;
a3874608 5084 case PERL_MAGIC_arylen_p:
bfcb3514 5085 case PERL_MAGIC_rhash:
8d2f4536 5086 case PERL_MAGIC_symtab:
ece467f9
JP
5087 case PERL_MAGIC_vstring:
5088 vtable = 0;
5089 break;
7e8c5dac
HS
5090 case PERL_MAGIC_utf8:
5091 vtable = &PL_vtbl_utf8;
5092 break;
14befaf4 5093 case PERL_MAGIC_substr:
92110913 5094 vtable = &PL_vtbl_substr;
79072805 5095 break;
14befaf4 5096 case PERL_MAGIC_defelem:
92110913 5097 vtable = &PL_vtbl_defelem;
5f05dabc 5098 break;
14befaf4 5099 case PERL_MAGIC_glob:
92110913 5100 vtable = &PL_vtbl_glob;
79072805 5101 break;
14befaf4 5102 case PERL_MAGIC_arylen:
92110913 5103 vtable = &PL_vtbl_arylen;
79072805 5104 break;
14befaf4 5105 case PERL_MAGIC_pos:
92110913 5106 vtable = &PL_vtbl_pos;
a0d0e21e 5107 break;
14befaf4 5108 case PERL_MAGIC_backref:
92110913 5109 vtable = &PL_vtbl_backref;
810b8aa5 5110 break;
14befaf4
DM
5111 case PERL_MAGIC_ext:
5112 /* Reserved for use by extensions not perl internals. */
4633a7c4
LW
5113 /* Useful for attaching extension internal data to perl vars. */
5114 /* Note that multiple extensions may clash if magical scalars */
5115 /* etc holding private data from one are passed to another. */
a0d0e21e 5116 break;
79072805 5117 default:
14befaf4 5118 Perl_croak(aTHX_ "Don't know how to handle magic of type \\%o", how);
463ee0b2 5119 }
68795e93 5120
92110913 5121 /* Rest of work is done else where */
27da23d5 5122 mg = sv_magicext(sv,obj,how,(MGVTBL*)vtable,name,namlen);
68795e93 5123
92110913
NIS
5124 switch (how) {
5125 case PERL_MAGIC_taint:
5126 mg->mg_len = 1;
5127 break;
5128 case PERL_MAGIC_ext:
5129 case PERL_MAGIC_dbfile:
5130 SvRMAGICAL_on(sv);
5131 break;
5132 }
463ee0b2
LW
5133}
5134
c461cf8f
JH
5135/*
5136=for apidoc sv_unmagic
5137
645c22ef 5138Removes all magic of type C<type> from an SV.
c461cf8f
JH
5139
5140=cut
5141*/
5142
463ee0b2 5143int
864dbfa3 5144Perl_sv_unmagic(pTHX_ SV *sv, int type)
463ee0b2
LW
5145{
5146 MAGIC* mg;
5147 MAGIC** mgp;
91bba347 5148 if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv))
463ee0b2
LW
5149 return 0;
5150 mgp = &SvMAGIC(sv);
5151 for (mg = *mgp; mg; mg = *mgp) {
5152 if (mg->mg_type == type) {
e1ec3a88 5153 const MGVTBL* const vtbl = mg->mg_virtual;
463ee0b2 5154 *mgp = mg->mg_moremagic;
1d7c1841 5155 if (vtbl && vtbl->svt_free)
fc0dc3b3 5156 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
14befaf4 5157 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
92110913 5158 if (mg->mg_len > 0)
1edc1566 5159 Safefree(mg->mg_ptr);
565764a8 5160 else if (mg->mg_len == HEf_SVKEY)
1edc1566 5161 SvREFCNT_dec((SV*)mg->mg_ptr);
7e8c5dac
HS
5162 else if (mg->mg_type == PERL_MAGIC_utf8 && mg->mg_ptr)
5163 Safefree(mg->mg_ptr);
9cbac4c7 5164 }
a0d0e21e
LW
5165 if (mg->mg_flags & MGf_REFCOUNTED)
5166 SvREFCNT_dec(mg->mg_obj);
463ee0b2
LW
5167 Safefree(mg);
5168 }
5169 else
5170 mgp = &mg->mg_moremagic;
79072805 5171 }
91bba347 5172 if (!SvMAGIC(sv)) {
463ee0b2 5173 SvMAGICAL_off(sv);
06759ea0 5174 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_NOK|SVp_POK)) >> PRIVSHIFT;
463ee0b2
LW
5175 }
5176
5177 return 0;
79072805
LW
5178}
5179
c461cf8f
JH
5180/*
5181=for apidoc sv_rvweaken
5182
645c22ef
DM
5183Weaken a reference: set the C<SvWEAKREF> flag on this RV; give the
5184referred-to SV C<PERL_MAGIC_backref> magic if it hasn't already; and
5185push a back-reference to this RV onto the array of backreferences
5186associated with that magic.
c461cf8f
JH
5187
5188=cut
5189*/
5190
810b8aa5 5191SV *
864dbfa3 5192Perl_sv_rvweaken(pTHX_ SV *sv)
810b8aa5
GS
5193{
5194 SV *tsv;
5195 if (!SvOK(sv)) /* let undefs pass */
5196 return sv;
5197 if (!SvROK(sv))
cea2e8a9 5198 Perl_croak(aTHX_ "Can't weaken a nonreference");
810b8aa5 5199 else if (SvWEAKREF(sv)) {
810b8aa5 5200 if (ckWARN(WARN_MISC))
9014280d 5201 Perl_warner(aTHX_ packWARN(WARN_MISC), "Reference is already weak");
810b8aa5
GS
5202 return sv;
5203 }
5204 tsv = SvRV(sv);
e15faf7d 5205 Perl_sv_add_backref(aTHX_ tsv, sv);
810b8aa5 5206 SvWEAKREF_on(sv);
1c846c1f 5207 SvREFCNT_dec(tsv);
810b8aa5
GS
5208 return sv;
5209}
5210
645c22ef
DM
5211/* Give tsv backref magic if it hasn't already got it, then push a
5212 * back-reference to sv onto the array associated with the backref magic.
5213 */
5214
e15faf7d
NC
5215void
5216Perl_sv_add_backref(pTHX_ SV *tsv, SV *sv)
810b8aa5
GS
5217{
5218 AV *av;
5219 MAGIC *mg;
14befaf4 5220 if (SvMAGICAL(tsv) && (mg = mg_find(tsv, PERL_MAGIC_backref)))
810b8aa5
GS
5221 av = (AV*)mg->mg_obj;
5222 else {
5223 av = newAV();
14befaf4 5224 sv_magic(tsv, (SV*)av, PERL_MAGIC_backref, NULL, 0);
d99b02a1
DM
5225 /* av now has a refcnt of 2, which avoids it getting freed
5226 * before us during global cleanup. The extra ref is removed
5227 * by magic_killbackrefs() when tsv is being freed */
810b8aa5 5228 }
d91d49e8 5229 if (AvFILLp(av) >= AvMAX(av)) {
d91d49e8
MM
5230 av_extend(av, AvFILLp(av)+1);
5231 }
5232 AvARRAY(av)[++AvFILLp(av)] = sv; /* av_push() */
810b8aa5
GS
5233}
5234
645c22ef
DM
5235/* delete a back-reference to ourselves from the backref magic associated
5236 * with the SV we point to.
5237 */
5238
1c846c1f 5239STATIC void
e15faf7d 5240S_sv_del_backref(pTHX_ SV *tsv, SV *sv)
810b8aa5
GS
5241{
5242 AV *av;
5243 SV **svp;
5244 I32 i;
c04a4dfe 5245 MAGIC *mg = NULL;
e15faf7d
NC
5246 if (!SvMAGICAL(tsv) || !(mg = mg_find(tsv, PERL_MAGIC_backref))) {
5247 if (PL_in_clean_all)
5248 return;
5249 }
14befaf4 5250 if (!SvMAGICAL(tsv) || !(mg = mg_find(tsv, PERL_MAGIC_backref)))
cea2e8a9 5251 Perl_croak(aTHX_ "panic: del_backref");
810b8aa5
GS
5252 av = (AV *)mg->mg_obj;
5253 svp = AvARRAY(av);
6a76db8b
NC
5254 /* We shouldn't be in here more than once, but for paranoia reasons lets
5255 not assume this. */
5256 for (i = AvFILLp(av); i >= 0; i--) {
5257 if (svp[i] == sv) {
5258 const SSize_t fill = AvFILLp(av);
5259 if (i != fill) {
5260 /* We weren't the last entry.
5261 An unordered list has this property that you can take the
5262 last element off the end to fill the hole, and it's still
5263 an unordered list :-)
5264 */
5265 svp[i] = svp[fill];
5266 }
5267 svp[fill] = Nullsv;
5268 AvFILLp(av) = fill - 1;
5269 }
5270 }
810b8aa5
GS
5271}
5272
954c1994
GS
5273/*
5274=for apidoc sv_insert
5275
5276Inserts a string at the specified offset/length within the SV. Similar to
5277the Perl substr() function.
5278
5279=cut
5280*/
5281
79072805 5282void
e1ec3a88 5283Perl_sv_insert(pTHX_ SV *bigstr, STRLEN offset, STRLEN len, const char *little, STRLEN littlelen)
79072805
LW
5284{
5285 register char *big;
5286 register char *mid;
5287 register char *midend;
5288 register char *bigend;
5289 register I32 i;
6ff81951 5290 STRLEN curlen;
1c846c1f 5291
79072805 5292
8990e307 5293 if (!bigstr)
cea2e8a9 5294 Perl_croak(aTHX_ "Can't modify non-existent substring");
6ff81951 5295 SvPV_force(bigstr, curlen);
60fa28ff 5296 (void)SvPOK_only_UTF8(bigstr);
6ff81951
GS
5297 if (offset + len > curlen) {
5298 SvGROW(bigstr, offset+len+1);
93524f2b 5299 Zero(SvPVX(bigstr)+curlen, offset+len-curlen, char);
6ff81951
GS
5300 SvCUR_set(bigstr, offset+len);
5301 }
79072805 5302
69b47968 5303 SvTAINT(bigstr);
79072805
LW
5304 i = littlelen - len;
5305 if (i > 0) { /* string might grow */
a0d0e21e 5306 big = SvGROW(bigstr, SvCUR(bigstr) + i + 1);
79072805
LW
5307 mid = big + offset + len;
5308 midend = bigend = big + SvCUR(bigstr);
5309 bigend += i;
5310 *bigend = '\0';
5311 while (midend > mid) /* shove everything down */
5312 *--bigend = *--midend;
5313 Move(little,big+offset,littlelen,char);
b162af07 5314 SvCUR_set(bigstr, SvCUR(bigstr) + i);
79072805
LW
5315 SvSETMAGIC(bigstr);
5316 return;
5317 }
5318 else if (i == 0) {
463ee0b2 5319 Move(little,SvPVX(bigstr)+offset,len,char);
79072805
LW
5320 SvSETMAGIC(bigstr);
5321 return;
5322 }
5323
463ee0b2 5324 big = SvPVX(bigstr);
79072805
LW
5325 mid = big + offset;
5326 midend = mid + len;
5327 bigend = big + SvCUR(bigstr);
5328
5329 if (midend > bigend)
cea2e8a9 5330 Perl_croak(aTHX_ "panic: sv_insert");
79072805
LW
5331
5332 if (mid - big > bigend - midend) { /* faster to shorten from end */
5333 if (littlelen) {
5334 Move(little, mid, littlelen,char);
5335 mid += littlelen;
5336 }
5337 i = bigend - midend;
5338 if (i > 0) {
5339 Move(midend, mid, i,char);
5340 mid += i;
5341 }
5342 *mid = '\0';
5343 SvCUR_set(bigstr, mid - big);
5344 }
155aba94 5345 else if ((i = mid - big)) { /* faster from front */
79072805
LW
5346 midend -= littlelen;
5347 mid = midend;
5348 sv_chop(bigstr,midend-i);
5349 big += i;
5350 while (i--)
5351 *--midend = *--big;
5352 if (littlelen)
5353 Move(little, mid, littlelen,char);
5354 }
5355 else if (littlelen) {
5356 midend -= littlelen;
5357 sv_chop(bigstr,midend);
5358 Move(little,midend,littlelen,char);
5359 }
5360 else {
5361 sv_chop(bigstr,midend);
5362 }
5363 SvSETMAGIC(bigstr);
5364}
5365
c461cf8f
JH
5366/*
5367=for apidoc sv_replace
5368
5369Make the first argument a copy of the second, then delete the original.
645c22ef
DM
5370The target SV physically takes over ownership of the body of the source SV
5371and inherits its flags; however, the target keeps any magic it owns,
5372and any magic in the source is discarded.
ff276b08 5373Note that this is a rather specialist SV copying operation; most of the
645c22ef 5374time you'll want to use C<sv_setsv> or one of its many macro front-ends.
c461cf8f
JH
5375
5376=cut
5377*/
79072805
LW
5378
5379void
864dbfa3 5380Perl_sv_replace(pTHX_ register SV *sv, register SV *nsv)
79072805 5381{
a3b680e6 5382 const U32 refcnt = SvREFCNT(sv);
765f542d 5383 SV_CHECK_THINKFIRST_COW_DROP(sv);
0453d815 5384 if (SvREFCNT(nsv) != 1 && ckWARN_d(WARN_INTERNAL))
9014280d 5385 Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "Reference miscount in sv_replace()");
93a17b20 5386 if (SvMAGICAL(sv)) {
a0d0e21e
LW
5387 if (SvMAGICAL(nsv))
5388 mg_free(nsv);
5389 else
5390 sv_upgrade(nsv, SVt_PVMG);
b162af07 5391 SvMAGIC_set(nsv, SvMAGIC(sv));
a0d0e21e 5392 SvFLAGS(nsv) |= SvMAGICAL(sv);
93a17b20 5393 SvMAGICAL_off(sv);
b162af07 5394 SvMAGIC_set(sv, NULL);
93a17b20 5395 }
79072805
LW
5396 SvREFCNT(sv) = 0;
5397 sv_clear(sv);
477f5d66 5398 assert(!SvREFCNT(sv));
fd0854ff
DM
5399#ifdef DEBUG_LEAKING_SCALARS
5400 sv->sv_flags = nsv->sv_flags;
5401 sv->sv_any = nsv->sv_any;
5402 sv->sv_refcnt = nsv->sv_refcnt;
f34d0642 5403 sv->sv_u = nsv->sv_u;
fd0854ff 5404#else
79072805 5405 StructCopy(nsv,sv,SV);
fd0854ff 5406#endif
7b2c381c
NC
5407 /* Currently could join these into one piece of pointer arithmetic, but
5408 it would be unclear. */
5409 if(SvTYPE(sv) == SVt_IV)
5410 SvANY(sv)
339049b0 5411 = (XPVIV*)((char*)&(sv->sv_u.svu_iv) - STRUCT_OFFSET(XPVIV, xiv_iv));
7b2c381c 5412 else if (SvTYPE(sv) == SVt_RV) {
339049b0 5413 SvANY(sv) = &sv->sv_u.svu_rv;
7b2c381c
NC
5414 }
5415
fd0854ff 5416
f8c7b90f 5417#ifdef PERL_OLD_COPY_ON_WRITE
d3d0e6f1
NC
5418 if (SvIsCOW_normal(nsv)) {
5419 /* We need to follow the pointers around the loop to make the
5420 previous SV point to sv, rather than nsv. */
5421 SV *next;
5422 SV *current = nsv;
5423 while ((next = SV_COW_NEXT_SV(current)) != nsv) {
5424 assert(next);
5425 current = next;
3f7c398e 5426 assert(SvPVX_const(current) == SvPVX_const(nsv));
d3d0e6f1
NC
5427 }
5428 /* Make the SV before us point to the SV after us. */
5429 if (DEBUG_C_TEST) {
5430 PerlIO_printf(Perl_debug_log, "previous is\n");
5431 sv_dump(current);
a29f6d03
NC
5432 PerlIO_printf(Perl_debug_log,
5433 "move it from 0x%"UVxf" to 0x%"UVxf"\n",
d3d0e6f1
NC
5434 (UV) SV_COW_NEXT_SV(current), (UV) sv);
5435 }
a29f6d03 5436 SV_COW_NEXT_SV_SET(current, sv);
d3d0e6f1
NC
5437 }
5438#endif
79072805 5439 SvREFCNT(sv) = refcnt;
1edc1566 5440 SvFLAGS(nsv) |= SVTYPEMASK; /* Mark as freed */
39cf41c2 5441 SvREFCNT(nsv) = 0;
463ee0b2 5442 del_SV(nsv);
79072805
LW
5443}
5444
c461cf8f
JH
5445/*
5446=for apidoc sv_clear
5447
645c22ef
DM
5448Clear an SV: call any destructors, free up any memory used by the body,
5449and free the body itself. The SV's head is I<not> freed, although
5450its type is set to all 1's so that it won't inadvertently be assumed
5451to be live during global destruction etc.
5452This function should only be called when REFCNT is zero. Most of the time
5453you'll want to call C<sv_free()> (or its macro wrapper C<SvREFCNT_dec>)
5454instead.
c461cf8f
JH
5455
5456=cut
5457*/
5458
79072805 5459void
864dbfa3 5460Perl_sv_clear(pTHX_ register SV *sv)
79072805 5461{
27da23d5 5462 dVAR;
ec12f114 5463 HV* stash;
79072805
LW
5464 assert(sv);
5465 assert(SvREFCNT(sv) == 0);
5466
ed6116ce 5467 if (SvOBJECT(sv)) {
3280af22 5468 if (PL_defstash) { /* Still have a symbol table? */
39644a26 5469 dSP;
d460ef45 5470 do {
b464bac0 5471 CV* destructor;
4e8e7886 5472 stash = SvSTASH(sv);
32251b26 5473 destructor = StashHANDLER(stash,DESTROY);
4e8e7886 5474 if (destructor) {
1b6737cc 5475 SV* const tmpref = newRV(sv);
5cc433a6 5476 SvREADONLY_on(tmpref); /* DESTROY() could be naughty */
4e8e7886 5477 ENTER;
e788e7d3 5478 PUSHSTACKi(PERLSI_DESTROY);
4e8e7886
GS
5479 EXTEND(SP, 2);
5480 PUSHMARK(SP);
5cc433a6 5481 PUSHs(tmpref);
4e8e7886 5482 PUTBACK;
44389ee9 5483 call_sv((SV*)destructor, G_DISCARD|G_EVAL|G_KEEPERR|G_VOID);
7a5fa8a2
NIS
5484
5485
d3acc0f7 5486 POPSTACK;
3095d977 5487 SPAGAIN;
4e8e7886 5488 LEAVE;
5cc433a6
AB
5489 if(SvREFCNT(tmpref) < 2) {
5490 /* tmpref is not kept alive! */
5491 SvREFCNT(sv)--;
b162af07 5492 SvRV_set(tmpref, NULL);
5cc433a6
AB
5493 SvROK_off(tmpref);
5494 }
5495 SvREFCNT_dec(tmpref);
4e8e7886
GS
5496 }
5497 } while (SvOBJECT(sv) && SvSTASH(sv) != stash);
8ebc5c01 5498
6f44e0a4
JP
5499
5500 if (SvREFCNT(sv)) {
5501 if (PL_in_clean_objs)
cea2e8a9 5502 Perl_croak(aTHX_ "DESTROY created new reference to dead object '%s'",
bfcb3514 5503 HvNAME_get(stash));
6f44e0a4
JP
5504 /* DESTROY gave object new lease on life */
5505 return;
5506 }
a0d0e21e 5507 }
4e8e7886 5508
a0d0e21e 5509 if (SvOBJECT(sv)) {
4e8e7886 5510 SvREFCNT_dec(SvSTASH(sv)); /* possibly of changed persuasion */
a0d0e21e
LW
5511 SvOBJECT_off(sv); /* Curse the object. */
5512 if (SvTYPE(sv) != SVt_PVIO)
3280af22 5513 --PL_sv_objcount; /* XXX Might want something more general */
a0d0e21e 5514 }
463ee0b2 5515 }
524189f1
JH
5516 if (SvTYPE(sv) >= SVt_PVMG) {
5517 if (SvMAGIC(sv))
5518 mg_free(sv);
bce8f412 5519 if (SvTYPE(sv) == SVt_PVMG && SvFLAGS(sv) & SVpad_TYPED)
524189f1
JH
5520 SvREFCNT_dec(SvSTASH(sv));
5521 }
ec12f114 5522 stash = NULL;
79072805 5523 switch (SvTYPE(sv)) {
8990e307 5524 case SVt_PVIO:
df0bd2f4
GS
5525 if (IoIFP(sv) &&
5526 IoIFP(sv) != PerlIO_stdin() &&
5f05dabc 5527 IoIFP(sv) != PerlIO_stdout() &&
5528 IoIFP(sv) != PerlIO_stderr())
93578b34 5529 {
f2b5be74 5530 io_close((IO*)sv, FALSE);
93578b34 5531 }
1d7c1841 5532 if (IoDIRP(sv) && !(IoFLAGS(sv) & IOf_FAKE_DIRP))
1236053a 5533 PerlDir_close(IoDIRP(sv));
1d7c1841 5534 IoDIRP(sv) = (DIR*)NULL;
8990e307
LW
5535 Safefree(IoTOP_NAME(sv));
5536 Safefree(IoFMT_NAME(sv));
5537 Safefree(IoBOTTOM_NAME(sv));
a0d0e21e 5538 /* FALL THROUGH */
79072805 5539 case SVt_PVBM:
a0d0e21e 5540 goto freescalar;
79072805 5541 case SVt_PVCV:
748a9306 5542 case SVt_PVFM:
85e6fe83 5543 cv_undef((CV*)sv);
a0d0e21e 5544 goto freescalar;
79072805 5545 case SVt_PVHV:
85e6fe83 5546 hv_undef((HV*)sv);
a0d0e21e 5547 break;
79072805 5548 case SVt_PVAV:
85e6fe83 5549 av_undef((AV*)sv);
a0d0e21e 5550 break;
02270b4e 5551 case SVt_PVLV:
dd28f7bb
DM
5552 if (LvTYPE(sv) == 'T') { /* for tie: return HE to pool */
5553 SvREFCNT_dec(HeKEY_sv((HE*)LvTARG(sv)));
5554 HeNEXT((HE*)LvTARG(sv)) = PL_hv_fetch_ent_mh;
5555 PL_hv_fetch_ent_mh = (HE*)LvTARG(sv);
5556 }
5557 else if (LvTYPE(sv) != 't') /* unless tie: unrefcnted fake SV** */
5558 SvREFCNT_dec(LvTARG(sv));
02270b4e 5559 goto freescalar;
a0d0e21e 5560 case SVt_PVGV:
1edc1566 5561 gp_free((GV*)sv);
a0d0e21e 5562 Safefree(GvNAME(sv));
ec12f114
JPC
5563 /* cannot decrease stash refcount yet, as we might recursively delete
5564 ourselves when the refcnt drops to zero. Delay SvREFCNT_dec
5565 of stash until current sv is completely gone.
5566 -- JohnPC, 27 Mar 1998 */
5567 stash = GvSTASH(sv);
a0d0e21e 5568 /* FALL THROUGH */
79072805 5569 case SVt_PVMG:
79072805
LW
5570 case SVt_PVNV:
5571 case SVt_PVIV:
a0d0e21e 5572 freescalar:
5228ca4e
NC
5573 /* Don't bother with SvOOK_off(sv); as we're only going to free it. */
5574 if (SvOOK(sv)) {
93524f2b 5575 SvPV_set(sv, SvPVX_mutable(sv) - SvIVX(sv));
5228ca4e
NC
5576 /* Don't even bother with turning off the OOK flag. */
5577 }
79072805
LW
5578 /* FALL THROUGH */
5579 case SVt_PV:
a0d0e21e 5580 case SVt_RV:
810b8aa5 5581 if (SvROK(sv)) {
e15faf7d 5582 SV *target = SvRV(sv);
810b8aa5 5583 if (SvWEAKREF(sv))
e15faf7d 5584 sv_del_backref(target, sv);
810b8aa5 5585 else
e15faf7d 5586 SvREFCNT_dec(target);
810b8aa5 5587 }
f8c7b90f 5588#ifdef PERL_OLD_COPY_ON_WRITE
3f7c398e 5589 else if (SvPVX_const(sv)) {
765f542d
NC
5590 if (SvIsCOW(sv)) {
5591 /* I believe I need to grab the global SV mutex here and
5592 then recheck the COW status. */
46187eeb
NC
5593 if (DEBUG_C_TEST) {
5594 PerlIO_printf(Perl_debug_log, "Copy on write: clear\n");
e419cbc5 5595 sv_dump(sv);
46187eeb 5596 }
bdd68bc3
NC
5597 sv_release_COW(sv, SvPVX_const(sv), SvLEN(sv),
5598 SV_COW_NEXT_SV(sv));
765f542d
NC
5599 /* And drop it here. */
5600 SvFAKE_off(sv);
5601 } else if (SvLEN(sv)) {
3f7c398e 5602 Safefree(SvPVX_const(sv));
765f542d
NC
5603 }
5604 }
5605#else
3f7c398e 5606 else if (SvPVX_const(sv) && SvLEN(sv))
94010e71 5607 Safefree(SvPVX_mutable(sv));
3f7c398e 5608 else if (SvPVX_const(sv) && SvREADONLY(sv) && SvFAKE(sv)) {
bdd68bc3 5609 unshare_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sv)));
1c846c1f
NIS
5610 SvFAKE_off(sv);
5611 }
765f542d 5612#endif
79072805 5613 break;
a0d0e21e 5614/*
79072805 5615 case SVt_NV:
79072805 5616 case SVt_IV:
79072805
LW
5617 case SVt_NULL:
5618 break;
a0d0e21e 5619*/
79072805
LW
5620 }
5621
5622 switch (SvTYPE(sv)) {
5623 case SVt_NULL:
5624 break;
79072805 5625 case SVt_IV:
79072805
LW
5626 break;
5627 case SVt_NV:
5628 del_XNV(SvANY(sv));
5629 break;
ed6116ce 5630 case SVt_RV:
ed6116ce 5631 break;
79072805
LW
5632 case SVt_PV:
5633 del_XPV(SvANY(sv));
5634 break;
5635 case SVt_PVIV:
5636 del_XPVIV(SvANY(sv));
5637 break;
5638 case SVt_PVNV:
5639 del_XPVNV(SvANY(sv));
5640 break;
5641 case SVt_PVMG:
5642 del_XPVMG(SvANY(sv));
5643 break;
5644 case SVt_PVLV:
5645 del_XPVLV(SvANY(sv));
5646 break;
5647 case SVt_PVAV:
5648 del_XPVAV(SvANY(sv));
5649 break;
5650 case SVt_PVHV:
5651 del_XPVHV(SvANY(sv));
5652 break;
5653 case SVt_PVCV:
5654 del_XPVCV(SvANY(sv));
5655 break;
5656 case SVt_PVGV:
5657 del_XPVGV(SvANY(sv));
ec12f114
JPC
5658 /* code duplication for increased performance. */
5659 SvFLAGS(sv) &= SVf_BREAK;
5660 SvFLAGS(sv) |= SVTYPEMASK;
5661 /* decrease refcount of the stash that owns this GV, if any */
5662 if (stash)
e15faf7d 5663 sv_del_backref((SV*)stash, sv);
ec12f114 5664 return; /* not break, SvFLAGS reset already happened */
79072805
LW
5665 case SVt_PVBM:
5666 del_XPVBM(SvANY(sv));
5667 break;
5668 case SVt_PVFM:
5669 del_XPVFM(SvANY(sv));
5670 break;
8990e307
LW
5671 case SVt_PVIO:
5672 del_XPVIO(SvANY(sv));
5673 break;
79072805 5674 }
a0d0e21e 5675 SvFLAGS(sv) &= SVf_BREAK;
8990e307 5676 SvFLAGS(sv) |= SVTYPEMASK;
79072805
LW
5677}
5678
645c22ef
DM
5679/*
5680=for apidoc sv_newref
5681
5682Increment an SV's reference count. Use the C<SvREFCNT_inc()> wrapper
5683instead.
5684
5685=cut
5686*/
5687
79072805 5688SV *
864dbfa3 5689Perl_sv_newref(pTHX_ SV *sv)
79072805 5690{
463ee0b2 5691 if (sv)
4db098f4 5692 (SvREFCNT(sv))++;
79072805
LW
5693 return sv;
5694}
5695
c461cf8f
JH
5696/*
5697=for apidoc sv_free
5698
645c22ef
DM
5699Decrement an SV's reference count, and if it drops to zero, call
5700C<sv_clear> to invoke destructors and free up any memory used by
5701the body; finally, deallocate the SV's head itself.
5702Normally called via a wrapper macro C<SvREFCNT_dec>.
c461cf8f
JH
5703
5704=cut
5705*/
5706
79072805 5707void
864dbfa3 5708Perl_sv_free(pTHX_ SV *sv)
79072805 5709{
27da23d5 5710 dVAR;
79072805
LW
5711 if (!sv)
5712 return;
a0d0e21e
LW
5713 if (SvREFCNT(sv) == 0) {
5714 if (SvFLAGS(sv) & SVf_BREAK)
645c22ef
DM
5715 /* this SV's refcnt has been artificially decremented to
5716 * trigger cleanup */
a0d0e21e 5717 return;
3280af22 5718 if (PL_in_clean_all) /* All is fair */
1edc1566 5719 return;
d689ffdd
JP
5720 if (SvREADONLY(sv) && SvIMMORTAL(sv)) {
5721 /* make sure SvREFCNT(sv)==0 happens very seldom */
5722 SvREFCNT(sv) = (~(U32)0)/2;
5723 return;
5724 }
0453d815 5725 if (ckWARN_d(WARN_INTERNAL))
d5dede04 5726 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
472d47bc
SB
5727 "Attempt to free unreferenced scalar: SV 0x%"UVxf
5728 pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
79072805
LW
5729 return;
5730 }
4db098f4 5731 if (--(SvREFCNT(sv)) > 0)
8990e307 5732 return;
8c4d3c90
NC
5733 Perl_sv_free2(aTHX_ sv);
5734}
5735
5736void
5737Perl_sv_free2(pTHX_ SV *sv)
5738{
27da23d5 5739 dVAR;
463ee0b2
LW
5740#ifdef DEBUGGING
5741 if (SvTEMP(sv)) {
0453d815 5742 if (ckWARN_d(WARN_DEBUGGING))
9014280d 5743 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING),
472d47bc
SB
5744 "Attempt to free temp prematurely: SV 0x%"UVxf
5745 pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
79072805 5746 return;
79072805 5747 }
463ee0b2 5748#endif
d689ffdd
JP
5749 if (SvREADONLY(sv) && SvIMMORTAL(sv)) {
5750 /* make sure SvREFCNT(sv)==0 happens very seldom */
5751 SvREFCNT(sv) = (~(U32)0)/2;
5752 return;
5753 }
79072805 5754 sv_clear(sv);
477f5d66
CS
5755 if (! SvREFCNT(sv))
5756 del_SV(sv);
79072805
LW
5757}
5758
954c1994
GS
5759/*
5760=for apidoc sv_len
5761
645c22ef
DM
5762Returns the length of the string in the SV. Handles magic and type
5763coercion. See also C<SvCUR>, which gives raw access to the xpv_cur slot.
954c1994
GS
5764
5765=cut
5766*/
5767
79072805 5768STRLEN
864dbfa3 5769Perl_sv_len(pTHX_ register SV *sv)
79072805 5770{
463ee0b2 5771 STRLEN len;
79072805
LW
5772
5773 if (!sv)
5774 return 0;
5775
8990e307 5776 if (SvGMAGICAL(sv))
565764a8 5777 len = mg_length(sv);
8990e307 5778 else
4d84ee25 5779 (void)SvPV_const(sv, len);
463ee0b2 5780 return len;
79072805
LW
5781}
5782
c461cf8f
JH
5783/*
5784=for apidoc sv_len_utf8
5785
5786Returns the number of characters in the string in an SV, counting wide
1e54db1a 5787UTF-8 bytes as a single character. Handles magic and type coercion.
c461cf8f
JH
5788
5789=cut
5790*/
5791
7e8c5dac
HS
5792/*
5793 * The length is cached in PERL_UTF8_magic, in the mg_len field. Also the
5794 * mg_ptr is used, by sv_pos_u2b(), see the comments of S_utf8_mg_pos_init().
5795 * (Note that the mg_len is not the length of the mg_ptr field.)
7a5fa8a2 5796 *
7e8c5dac
HS
5797 */
5798
a0ed51b3 5799STRLEN
864dbfa3 5800Perl_sv_len_utf8(pTHX_ register SV *sv)
a0ed51b3 5801{
a0ed51b3
LW
5802 if (!sv)
5803 return 0;
5804
a0ed51b3 5805 if (SvGMAGICAL(sv))
b76347f2 5806 return mg_length(sv);
a0ed51b3 5807 else
b76347f2 5808 {
7e8c5dac 5809 STRLEN len, ulen;
e62f0680 5810 const U8 *s = (U8*)SvPV_const(sv, len);
7e8c5dac
HS
5811 MAGIC *mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_utf8) : 0;
5812
e23c8137 5813 if (mg && mg->mg_len != -1 && (mg->mg_len > 0 || len == 0)) {
7e8c5dac 5814 ulen = mg->mg_len;
e23c8137
JH
5815#ifdef PERL_UTF8_CACHE_ASSERT
5816 assert(ulen == Perl_utf8_length(aTHX_ s, s + len));
5817#endif
5818 }
7e8c5dac
HS
5819 else {
5820 ulen = Perl_utf8_length(aTHX_ s, s + len);
5821 if (!mg && !SvREADONLY(sv)) {
5822 sv_magic(sv, 0, PERL_MAGIC_utf8, 0, 0);
5823 mg = mg_find(sv, PERL_MAGIC_utf8);
5824 assert(mg);
5825 }
5826 if (mg)
5827 mg->mg_len = ulen;
5828 }
5829 return ulen;
5830 }
5831}
5832
5833/* S_utf8_mg_pos_init() is used to initialize the mg_ptr field of
5834 * a PERL_UTF8_magic. The mg_ptr is used to store the mapping
5835 * between UTF-8 and byte offsets. There are two (substr offset and substr
5836 * length, the i offset, PERL_MAGIC_UTF8_CACHESIZE) times two (UTF-8 offset
5837 * and byte offset) cache positions.
5838 *
5839 * The mg_len field is used by sv_len_utf8(), see its comments.
5840 * Note that the mg_len is not the length of the mg_ptr field.
5841 *
5842 */
5843STATIC bool
245d4a47
NC
5844S_utf8_mg_pos_init(pTHX_ SV *sv, MAGIC **mgp, STRLEN **cachep, I32 i,
5845 I32 offsetp, const U8 *s, const U8 *start)
7e8c5dac 5846{
7a5fa8a2 5847 bool found = FALSE;
7e8c5dac
HS
5848
5849 if (SvMAGICAL(sv) && !SvREADONLY(sv)) {
8f78557a 5850 if (!*mgp)
27da23d5 5851 *mgp = sv_magicext(sv, 0, PERL_MAGIC_utf8, (MGVTBL*)&PL_vtbl_utf8, 0, 0);
7e8c5dac 5852 assert(*mgp);
b76347f2 5853
7e8c5dac
HS
5854 if ((*mgp)->mg_ptr)
5855 *cachep = (STRLEN *) (*mgp)->mg_ptr;
5856 else {
5857 Newz(0, *cachep, PERL_MAGIC_UTF8_CACHESIZE * 2, STRLEN);
5858 (*mgp)->mg_ptr = (char *) *cachep;
5859 }
5860 assert(*cachep);
5861
a3b680e6 5862 (*cachep)[i] = offsetp;
7e8c5dac
HS
5863 (*cachep)[i+1] = s - start;
5864 found = TRUE;
a0ed51b3 5865 }
7e8c5dac
HS
5866
5867 return found;
a0ed51b3
LW
5868}
5869
645c22ef 5870/*
7e8c5dac
HS
5871 * S_utf8_mg_pos() is used to query and update mg_ptr field of
5872 * a PERL_UTF8_magic. The mg_ptr is used to store the mapping
5873 * between UTF-8 and byte offsets. See also the comments of
5874 * S_utf8_mg_pos_init().
5875 *
5876 */
5877STATIC bool
245d4a47 5878S_utf8_mg_pos(pTHX_ SV *sv, MAGIC **mgp, STRLEN **cachep, I32 i, I32 *offsetp, I32 uoff, const U8 **sp, const U8 *start, const U8 *send)
7e8c5dac
HS
5879{
5880 bool found = FALSE;
5881
5882 if (SvMAGICAL(sv) && !SvREADONLY(sv)) {
5883 if (!*mgp)
5884 *mgp = mg_find(sv, PERL_MAGIC_utf8);
5885 if (*mgp && (*mgp)->mg_ptr) {
5886 *cachep = (STRLEN *) (*mgp)->mg_ptr;
e23c8137 5887 ASSERT_UTF8_CACHE(*cachep);
667208dd 5888 if ((*cachep)[i] == (STRLEN)uoff) /* An exact match. */
7a5fa8a2 5889 found = TRUE;
7e8c5dac
HS
5890 else { /* We will skip to the right spot. */
5891 STRLEN forw = 0;
5892 STRLEN backw = 0;
a3b680e6 5893 const U8* p = NULL;
7e8c5dac
HS
5894
5895 /* The assumption is that going backward is half
5896 * the speed of going forward (that's where the
5897 * 2 * backw in the below comes from). (The real
5898 * figure of course depends on the UTF-8 data.) */
5899
667208dd 5900 if ((*cachep)[i] > (STRLEN)uoff) {
7e8c5dac 5901 forw = uoff;
667208dd 5902 backw = (*cachep)[i] - (STRLEN)uoff;
7e8c5dac
HS
5903
5904 if (forw < 2 * backw)
5905 p = start;
5906 else
5907 p = start + (*cachep)[i+1];
5908 }
5909 /* Try this only for the substr offset (i == 0),
5910 * not for the substr length (i == 2). */
5911 else if (i == 0) { /* (*cachep)[i] < uoff */
a3b680e6 5912 const STRLEN ulen = sv_len_utf8(sv);
7e8c5dac 5913
667208dd
JH
5914 if ((STRLEN)uoff < ulen) {
5915 forw = (STRLEN)uoff - (*cachep)[i];
5916 backw = ulen - (STRLEN)uoff;
7e8c5dac
HS
5917
5918 if (forw < 2 * backw)
5919 p = start + (*cachep)[i+1];
5920 else
5921 p = send;
5922 }
5923
5924 /* If the string is not long enough for uoff,
5925 * we could extend it, but not at this low a level. */
5926 }
5927
5928 if (p) {
5929 if (forw < 2 * backw) {
5930 while (forw--)
5931 p += UTF8SKIP(p);
5932 }
5933 else {
5934 while (backw--) {
5935 p--;
5936 while (UTF8_IS_CONTINUATION(*p))
5937 p--;
5938 }
5939 }
5940
5941 /* Update the cache. */
667208dd 5942 (*cachep)[i] = (STRLEN)uoff;
7e8c5dac 5943 (*cachep)[i+1] = p - start;
8f78557a
AE
5944
5945 /* Drop the stale "length" cache */
5946 if (i == 0) {
5947 (*cachep)[2] = 0;
5948 (*cachep)[3] = 0;
5949 }
7a5fa8a2 5950
7e8c5dac
HS
5951 found = TRUE;
5952 }
5953 }
5954 if (found) { /* Setup the return values. */
5955 *offsetp = (*cachep)[i+1];
5956 *sp = start + *offsetp;
5957 if (*sp >= send) {
5958 *sp = send;
5959 *offsetp = send - start;
5960 }
5961 else if (*sp < start) {
5962 *sp = start;
5963 *offsetp = 0;
5964 }
5965 }
5966 }
e23c8137
JH
5967#ifdef PERL_UTF8_CACHE_ASSERT
5968 if (found) {
5969 U8 *s = start;
5970 I32 n = uoff;
5971
5972 while (n-- && s < send)
5973 s += UTF8SKIP(s);
5974
5975 if (i == 0) {
5976 assert(*offsetp == s - start);
5977 assert((*cachep)[0] == (STRLEN)uoff);
5978 assert((*cachep)[1] == *offsetp);
5979 }
5980 ASSERT_UTF8_CACHE(*cachep);
5981 }
5982#endif
7e8c5dac 5983 }
e23c8137 5984
7e8c5dac
HS
5985 return found;
5986}
7a5fa8a2 5987
7e8c5dac 5988/*
645c22ef
DM
5989=for apidoc sv_pos_u2b
5990
1e54db1a 5991Converts the value pointed to by offsetp from a count of UTF-8 chars from
645c22ef
DM
5992the start of the string, to a count of the equivalent number of bytes; if
5993lenp is non-zero, it does the same to lenp, but this time starting from
5994the offset, rather than from the start of the string. Handles magic and
5995type coercion.
5996
5997=cut
5998*/
5999
7e8c5dac
HS
6000/*
6001 * sv_pos_u2b() uses, like sv_pos_b2u(), the mg_ptr of the potential
6002 * PERL_UTF8_magic of the sv to store the mapping between UTF-8 and
6003 * byte offsets. See also the comments of S_utf8_mg_pos().
6004 *
6005 */
6006
a0ed51b3 6007void
864dbfa3 6008Perl_sv_pos_u2b(pTHX_ register SV *sv, I32* offsetp, I32* lenp)
a0ed51b3 6009{
245d4a47 6010 const U8 *start;
a0ed51b3
LW
6011 STRLEN len;
6012
6013 if (!sv)
6014 return;
6015
245d4a47 6016 start = (U8*)SvPV_const(sv, len);
7e8c5dac 6017 if (len) {
b464bac0
AL
6018 STRLEN boffset = 0;
6019 STRLEN *cache = 0;
245d4a47
NC
6020 const U8 *s = start;
6021 I32 uoffset = *offsetp;
6022 const U8 *send = s + len;
6023 MAGIC *mg = 0;
6024 bool found = FALSE;
7e8c5dac 6025
bdf77a2a 6026 if (utf8_mg_pos(sv, &mg, &cache, 0, offsetp, *offsetp, &s, start, send))
7e8c5dac
HS
6027 found = TRUE;
6028 if (!found && uoffset > 0) {
6029 while (s < send && uoffset--)
6030 s += UTF8SKIP(s);
6031 if (s >= send)
6032 s = send;
a3b680e6 6033 if (utf8_mg_pos_init(sv, &mg, &cache, 0, *offsetp, s, start))
7e8c5dac
HS
6034 boffset = cache[1];
6035 *offsetp = s - start;
6036 }
6037 if (lenp) {
6038 found = FALSE;
6039 start = s;
ec062429 6040 if (utf8_mg_pos(sv, &mg, &cache, 2, lenp, *lenp, &s, start, send)) {
7e8c5dac
HS
6041 *lenp -= boffset;
6042 found = TRUE;
6043 }
6044 if (!found && *lenp > 0) {
6045 I32 ulen = *lenp;
6046 if (ulen > 0)
6047 while (s < send && ulen--)
6048 s += UTF8SKIP(s);
6049 if (s >= send)
6050 s = send;
a3b680e6 6051 utf8_mg_pos_init(sv, &mg, &cache, 2, *lenp, s, start);
7e8c5dac
HS
6052 }
6053 *lenp = s - start;
6054 }
e23c8137 6055 ASSERT_UTF8_CACHE(cache);
7e8c5dac
HS
6056 }
6057 else {
6058 *offsetp = 0;
6059 if (lenp)
6060 *lenp = 0;
a0ed51b3 6061 }
e23c8137 6062
a0ed51b3
LW
6063 return;
6064}
6065
645c22ef
DM
6066/*
6067=for apidoc sv_pos_b2u
6068
6069Converts the value pointed to by offsetp from a count of bytes from the
1e54db1a 6070start of the string, to a count of the equivalent number of UTF-8 chars.
645c22ef
DM
6071Handles magic and type coercion.
6072
6073=cut
6074*/
6075
7e8c5dac
HS
6076/*
6077 * sv_pos_b2u() uses, like sv_pos_u2b(), the mg_ptr of the potential
6078 * PERL_UTF8_magic of the sv to store the mapping between UTF-8 and
6079 * byte offsets. See also the comments of S_utf8_mg_pos().
6080 *
6081 */
6082
a0ed51b3 6083void
7e8c5dac 6084Perl_sv_pos_b2u(pTHX_ register SV* sv, I32* offsetp)
a0ed51b3 6085{
83003860 6086 const U8* s;
a0ed51b3
LW
6087 STRLEN len;
6088
6089 if (!sv)
6090 return;
6091
83003860 6092 s = (const U8*)SvPV_const(sv, len);
eb160463 6093 if ((I32)len < *offsetp)
a0dbb045 6094 Perl_croak(aTHX_ "panic: sv_pos_b2u: bad byte offset");
7e8c5dac 6095 else {
83003860 6096 const U8* send = s + *offsetp;
7e8c5dac
HS
6097 MAGIC* mg = NULL;
6098 STRLEN *cache = NULL;
6099
6100 len = 0;
6101
6102 if (SvMAGICAL(sv) && !SvREADONLY(sv)) {
6103 mg = mg_find(sv, PERL_MAGIC_utf8);
6104 if (mg && mg->mg_ptr) {
6105 cache = (STRLEN *) mg->mg_ptr;
c5661c80 6106 if (cache[1] == (STRLEN)*offsetp) {
7e8c5dac
HS
6107 /* An exact match. */
6108 *offsetp = cache[0];
6109
6110 return;
6111 }
c5661c80 6112 else if (cache[1] < (STRLEN)*offsetp) {
7e8c5dac
HS
6113 /* We already know part of the way. */
6114 len = cache[0];
6115 s += cache[1];
7a5fa8a2 6116 /* Let the below loop do the rest. */
7e8c5dac
HS
6117 }
6118 else { /* cache[1] > *offsetp */
6119 /* We already know all of the way, now we may
6120 * be able to walk back. The same assumption
6121 * is made as in S_utf8_mg_pos(), namely that
6122 * walking backward is twice slower than
6123 * walking forward. */
6124 STRLEN forw = *offsetp;
6125 STRLEN backw = cache[1] - *offsetp;
6126
6127 if (!(forw < 2 * backw)) {
83003860 6128 const U8 *p = s + cache[1];
7e8c5dac 6129 STRLEN ubackw = 0;
7a5fa8a2 6130
a5b510f2
AE
6131 cache[1] -= backw;
6132
7e8c5dac
HS
6133 while (backw--) {
6134 p--;
0aeb64d0 6135 while (UTF8_IS_CONTINUATION(*p)) {
7e8c5dac 6136 p--;
0aeb64d0
JH
6137 backw--;
6138 }
7e8c5dac
HS
6139 ubackw++;
6140 }
6141
6142 cache[0] -= ubackw;
0aeb64d0 6143 *offsetp = cache[0];
a67d7df9
TS
6144
6145 /* Drop the stale "length" cache */
6146 cache[2] = 0;
6147 cache[3] = 0;
6148
0aeb64d0 6149 return;
7e8c5dac
HS
6150 }
6151 }
6152 }
e23c8137 6153 ASSERT_UTF8_CACHE(cache);
a0dbb045 6154 }
7e8c5dac
HS
6155
6156 while (s < send) {
6157 STRLEN n = 1;
6158
6159 /* Call utf8n_to_uvchr() to validate the sequence
6160 * (unless a simple non-UTF character) */
6161 if (!UTF8_IS_INVARIANT(*s))
6162 utf8n_to_uvchr(s, UTF8SKIP(s), &n, 0);
6163 if (n > 0) {
6164 s += n;
6165 len++;
6166 }
6167 else
6168 break;
6169 }
6170
6171 if (!SvREADONLY(sv)) {
6172 if (!mg) {
6173 sv_magic(sv, 0, PERL_MAGIC_utf8, 0, 0);
6174 mg = mg_find(sv, PERL_MAGIC_utf8);
6175 }
6176 assert(mg);
6177
6178 if (!mg->mg_ptr) {
979acdb5 6179 Newz(0, cache, PERL_MAGIC_UTF8_CACHESIZE * 2, STRLEN);
7e8c5dac
HS
6180 mg->mg_ptr = (char *) cache;
6181 }
6182 assert(cache);
6183
6184 cache[0] = len;
6185 cache[1] = *offsetp;
a67d7df9
TS
6186 /* Drop the stale "length" cache */
6187 cache[2] = 0;
6188 cache[3] = 0;
7e8c5dac
HS
6189 }
6190
6191 *offsetp = len;
a0ed51b3 6192 }
a0ed51b3
LW
6193 return;
6194}
6195
954c1994
GS
6196/*
6197=for apidoc sv_eq
6198
6199Returns a boolean indicating whether the strings in the two SVs are
645c22ef
DM
6200identical. Is UTF-8 and 'use bytes' aware, handles get magic, and will
6201coerce its args to strings if necessary.
954c1994
GS
6202
6203=cut
6204*/
6205
79072805 6206I32
e01b9e88 6207Perl_sv_eq(pTHX_ register SV *sv1, register SV *sv2)
79072805 6208{
e1ec3a88 6209 const char *pv1;
463ee0b2 6210 STRLEN cur1;
e1ec3a88 6211 const char *pv2;
463ee0b2 6212 STRLEN cur2;
e01b9e88 6213 I32 eq = 0;
553e1bcc
AT
6214 char *tpv = Nullch;
6215 SV* svrecode = Nullsv;
79072805 6216
e01b9e88 6217 if (!sv1) {
79072805
LW
6218 pv1 = "";
6219 cur1 = 0;
6220 }
463ee0b2 6221 else
4d84ee25 6222 pv1 = SvPV_const(sv1, cur1);
79072805 6223
e01b9e88
SC
6224 if (!sv2){
6225 pv2 = "";
6226 cur2 = 0;
92d29cee 6227 }
e01b9e88 6228 else
4d84ee25 6229 pv2 = SvPV_const(sv2, cur2);
79072805 6230
cf48d248 6231 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
799ef3cb
JH
6232 /* Differing utf8ness.
6233 * Do not UTF8size the comparands as a side-effect. */
6234 if (PL_encoding) {
6235 if (SvUTF8(sv1)) {
553e1bcc
AT
6236 svrecode = newSVpvn(pv2, cur2);
6237 sv_recode_to_utf8(svrecode, PL_encoding);
93524f2b 6238 pv2 = SvPV_const(svrecode, cur2);
799ef3cb
JH
6239 }
6240 else {
553e1bcc
AT
6241 svrecode = newSVpvn(pv1, cur1);
6242 sv_recode_to_utf8(svrecode, PL_encoding);
93524f2b 6243 pv1 = SvPV_const(svrecode, cur1);
799ef3cb
JH
6244 }
6245 /* Now both are in UTF-8. */
0a1bd7ac
DM
6246 if (cur1 != cur2) {
6247 SvREFCNT_dec(svrecode);
799ef3cb 6248 return FALSE;
0a1bd7ac 6249 }
799ef3cb
JH
6250 }
6251 else {
6252 bool is_utf8 = TRUE;
6253
6254 if (SvUTF8(sv1)) {
6255 /* sv1 is the UTF-8 one,
6256 * if is equal it must be downgrade-able */
e1ec3a88 6257 char *pv = (char*)bytes_from_utf8((const U8*)pv1,
799ef3cb
JH
6258 &cur1, &is_utf8);
6259 if (pv != pv1)
553e1bcc 6260 pv1 = tpv = pv;
799ef3cb
JH
6261 }
6262 else {
6263 /* sv2 is the UTF-8 one,
6264 * if is equal it must be downgrade-able */
e1ec3a88 6265 char *pv = (char *)bytes_from_utf8((const U8*)pv2,
799ef3cb
JH
6266 &cur2, &is_utf8);
6267 if (pv != pv2)
553e1bcc 6268 pv2 = tpv = pv;
799ef3cb
JH
6269 }
6270 if (is_utf8) {
6271 /* Downgrade not possible - cannot be eq */
bf694877 6272 assert (tpv == 0);
799ef3cb
JH
6273 return FALSE;
6274 }
6275 }
cf48d248
JH
6276 }
6277
6278 if (cur1 == cur2)
765f542d 6279 eq = (pv1 == pv2) || memEQ(pv1, pv2, cur1);
e01b9e88 6280
553e1bcc
AT
6281 if (svrecode)
6282 SvREFCNT_dec(svrecode);
799ef3cb 6283
553e1bcc
AT
6284 if (tpv)
6285 Safefree(tpv);
cf48d248 6286
e01b9e88 6287 return eq;
79072805
LW
6288}
6289
954c1994
GS
6290/*
6291=for apidoc sv_cmp
6292
6293Compares the strings in two SVs. Returns -1, 0, or 1 indicating whether the
6294string in C<sv1> is less than, equal to, or greater than the string in
645c22ef
DM
6295C<sv2>. Is UTF-8 and 'use bytes' aware, handles get magic, and will
6296coerce its args to strings if necessary. See also C<sv_cmp_locale>.
954c1994
GS
6297
6298=cut
6299*/
6300
79072805 6301I32
e01b9e88 6302Perl_sv_cmp(pTHX_ register SV *sv1, register SV *sv2)
79072805 6303{
560a288e 6304 STRLEN cur1, cur2;
e1ec3a88
AL
6305 const char *pv1, *pv2;
6306 char *tpv = Nullch;
cf48d248 6307 I32 cmp;
553e1bcc 6308 SV *svrecode = Nullsv;
560a288e 6309
e01b9e88
SC
6310 if (!sv1) {
6311 pv1 = "";
560a288e
GS
6312 cur1 = 0;
6313 }
e01b9e88 6314 else
4d84ee25 6315 pv1 = SvPV_const(sv1, cur1);
560a288e 6316
553e1bcc 6317 if (!sv2) {
e01b9e88 6318 pv2 = "";
560a288e
GS
6319 cur2 = 0;
6320 }
e01b9e88 6321 else
4d84ee25 6322 pv2 = SvPV_const(sv2, cur2);
79072805 6323
cf48d248 6324 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
799ef3cb
JH
6325 /* Differing utf8ness.
6326 * Do not UTF8size the comparands as a side-effect. */
cf48d248 6327 if (SvUTF8(sv1)) {
799ef3cb 6328 if (PL_encoding) {
553e1bcc
AT
6329 svrecode = newSVpvn(pv2, cur2);
6330 sv_recode_to_utf8(svrecode, PL_encoding);
93524f2b 6331 pv2 = SvPV_const(svrecode, cur2);
799ef3cb
JH
6332 }
6333 else {
e1ec3a88 6334 pv2 = tpv = (char*)bytes_to_utf8((const U8*)pv2, &cur2);
799ef3cb 6335 }
cf48d248
JH
6336 }
6337 else {
799ef3cb 6338 if (PL_encoding) {
553e1bcc
AT
6339 svrecode = newSVpvn(pv1, cur1);
6340 sv_recode_to_utf8(svrecode, PL_encoding);
93524f2b 6341 pv1 = SvPV_const(svrecode, cur1);
799ef3cb
JH
6342 }
6343 else {
e1ec3a88 6344 pv1 = tpv = (char*)bytes_to_utf8((const U8*)pv1, &cur1);
799ef3cb 6345 }
cf48d248
JH
6346 }
6347 }
6348
e01b9e88 6349 if (!cur1) {
cf48d248 6350 cmp = cur2 ? -1 : 0;
e01b9e88 6351 } else if (!cur2) {
cf48d248
JH
6352 cmp = 1;
6353 } else {
e1ec3a88 6354 const I32 retval = memcmp((const void*)pv1, (const void*)pv2, cur1 < cur2 ? cur1 : cur2);
e01b9e88
SC
6355
6356 if (retval) {
cf48d248 6357 cmp = retval < 0 ? -1 : 1;
e01b9e88 6358 } else if (cur1 == cur2) {
cf48d248
JH
6359 cmp = 0;
6360 } else {
6361 cmp = cur1 < cur2 ? -1 : 1;
e01b9e88 6362 }
cf48d248 6363 }
16660edb 6364
553e1bcc
AT
6365 if (svrecode)
6366 SvREFCNT_dec(svrecode);
799ef3cb 6367
553e1bcc
AT
6368 if (tpv)
6369 Safefree(tpv);
cf48d248
JH
6370
6371 return cmp;
bbce6d69 6372}
16660edb 6373
c461cf8f
JH
6374/*
6375=for apidoc sv_cmp_locale
6376
645c22ef
DM
6377Compares the strings in two SVs in a locale-aware manner. Is UTF-8 and
6378'use bytes' aware, handles get magic, and will coerce its args to strings
6379if necessary. See also C<sv_cmp_locale>. See also C<sv_cmp>.
c461cf8f
JH
6380
6381=cut
6382*/
6383
bbce6d69 6384I32
864dbfa3 6385Perl_sv_cmp_locale(pTHX_ register SV *sv1, register SV *sv2)
bbce6d69 6386{
36477c24 6387#ifdef USE_LOCALE_COLLATE
16660edb 6388
bbce6d69 6389 char *pv1, *pv2;
6390 STRLEN len1, len2;
6391 I32 retval;
16660edb 6392
3280af22 6393 if (PL_collation_standard)
bbce6d69 6394 goto raw_compare;
16660edb 6395
bbce6d69 6396 len1 = 0;
8ac85365 6397 pv1 = sv1 ? sv_collxfrm(sv1, &len1) : (char *) NULL;
bbce6d69 6398 len2 = 0;
8ac85365 6399 pv2 = sv2 ? sv_collxfrm(sv2, &len2) : (char *) NULL;
16660edb 6400
bbce6d69 6401 if (!pv1 || !len1) {
6402 if (pv2 && len2)
6403 return -1;
6404 else
6405 goto raw_compare;
6406 }
6407 else {
6408 if (!pv2 || !len2)
6409 return 1;
6410 }
16660edb 6411
bbce6d69 6412 retval = memcmp((void*)pv1, (void*)pv2, len1 < len2 ? len1 : len2);
16660edb 6413
bbce6d69 6414 if (retval)
16660edb 6415 return retval < 0 ? -1 : 1;
6416
bbce6d69 6417 /*
6418 * When the result of collation is equality, that doesn't mean
6419 * that there are no differences -- some locales exclude some
6420 * characters from consideration. So to avoid false equalities,
6421 * we use the raw string as a tiebreaker.
6422 */
16660edb 6423
bbce6d69 6424 raw_compare:
6425 /* FALL THROUGH */
16660edb 6426
36477c24 6427#endif /* USE_LOCALE_COLLATE */
16660edb 6428
bbce6d69 6429 return sv_cmp(sv1, sv2);
6430}
79072805 6431
645c22ef 6432
36477c24 6433#ifdef USE_LOCALE_COLLATE
645c22ef 6434
7a4c00b4 6435/*
645c22ef
DM
6436=for apidoc sv_collxfrm
6437
6438Add Collate Transform magic to an SV if it doesn't already have it.
6439
6440Any scalar variable may carry PERL_MAGIC_collxfrm magic that contains the
6441scalar data of the variable, but transformed to such a format that a normal
6442memory comparison can be used to compare the data according to the locale
6443settings.
6444
6445=cut
6446*/
6447
bbce6d69 6448char *
864dbfa3 6449Perl_sv_collxfrm(pTHX_ SV *sv, STRLEN *nxp)
bbce6d69 6450{
7a4c00b4 6451 MAGIC *mg;
16660edb 6452
14befaf4 6453 mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_collxfrm) : (MAGIC *) NULL;
3280af22 6454 if (!mg || !mg->mg_ptr || *(U32*)mg->mg_ptr != PL_collation_ix) {
93524f2b
NC
6455 const char *s;
6456 char *xf;
bbce6d69 6457 STRLEN len, xlen;
6458
7a4c00b4 6459 if (mg)
6460 Safefree(mg->mg_ptr);
93524f2b 6461 s = SvPV_const(sv, len);
bbce6d69 6462 if ((xf = mem_collxfrm(s, len, &xlen))) {
ff0cee69 6463 if (SvREADONLY(sv)) {
6464 SAVEFREEPV(xf);
6465 *nxp = xlen;
3280af22 6466 return xf + sizeof(PL_collation_ix);
ff0cee69 6467 }
7a4c00b4 6468 if (! mg) {
14befaf4
DM
6469 sv_magic(sv, 0, PERL_MAGIC_collxfrm, 0, 0);
6470 mg = mg_find(sv, PERL_MAGIC_collxfrm);
7a4c00b4 6471 assert(mg);
bbce6d69 6472 }
7a4c00b4 6473 mg->mg_ptr = xf;
565764a8 6474 mg->mg_len = xlen;
7a4c00b4 6475 }
6476 else {
ff0cee69 6477 if (mg) {
6478 mg->mg_ptr = NULL;
565764a8 6479 mg->mg_len = -1;
ff0cee69 6480 }
bbce6d69 6481 }
6482 }
7a4c00b4 6483 if (mg && mg->mg_ptr) {
565764a8 6484 *nxp = mg->mg_len;
3280af22 6485 return mg->mg_ptr + sizeof(PL_collation_ix);
bbce6d69 6486 }
6487 else {
6488 *nxp = 0;
6489 return NULL;
16660edb 6490 }
79072805
LW
6491}
6492
36477c24 6493#endif /* USE_LOCALE_COLLATE */
bbce6d69 6494
c461cf8f
JH
6495/*
6496=for apidoc sv_gets
6497
6498Get a line from the filehandle and store it into the SV, optionally
6499appending to the currently-stored string.
6500
6501=cut
6502*/
6503
79072805 6504char *
864dbfa3 6505Perl_sv_gets(pTHX_ register SV *sv, register PerlIO *fp, I32 append)
79072805 6506{
e1ec3a88 6507 const char *rsptr;
c07a80fd 6508 STRLEN rslen;
6509 register STDCHAR rslast;
6510 register STDCHAR *bp;
6511 register I32 cnt;
9c5ffd7c 6512 I32 i = 0;
8bfdd7d9 6513 I32 rspara = 0;
e311fd51 6514 I32 recsize;
c07a80fd 6515
bc44a8a2
NC
6516 if (SvTHINKFIRST(sv))
6517 sv_force_normal_flags(sv, append ? 0 : SV_COW_DROP_PV);
765f542d
NC
6518 /* XXX. If you make this PVIV, then copy on write can copy scalars read
6519 from <>.
6520 However, perlbench says it's slower, because the existing swipe code
6521 is faster than copy on write.
6522 Swings and roundabouts. */
862a34c6 6523 SvUPGRADE(sv, SVt_PV);
99491443 6524
ff68c719 6525 SvSCREAM_off(sv);
efd8b2ba
AE
6526
6527 if (append) {
6528 if (PerlIO_isutf8(fp)) {
6529 if (!SvUTF8(sv)) {
6530 sv_utf8_upgrade_nomg(sv);
6531 sv_pos_u2b(sv,&append,0);
6532 }
6533 } else if (SvUTF8(sv)) {
1b6737cc 6534 SV * const tsv = NEWSV(0,0);
efd8b2ba
AE
6535 sv_gets(tsv, fp, 0);
6536 sv_utf8_upgrade_nomg(tsv);
6537 SvCUR_set(sv,append);
6538 sv_catsv(sv,tsv);
6539 sv_free(tsv);
6540 goto return_string_or_null;
6541 }
6542 }
6543
6544 SvPOK_only(sv);
6545 if (PerlIO_isutf8(fp))
6546 SvUTF8_on(sv);
c07a80fd 6547
923e4eb5 6548 if (IN_PERL_COMPILETIME) {
8bfdd7d9
HS
6549 /* we always read code in line mode */
6550 rsptr = "\n";
6551 rslen = 1;
6552 }
6553 else if (RsSNARF(PL_rs)) {
7a5fa8a2
NIS
6554 /* If it is a regular disk file use size from stat() as estimate
6555 of amount we are going to read - may result in malloc-ing
6556 more memory than we realy need if layers bellow reduce
e468d35b
NIS
6557 size we read (e.g. CRLF or a gzip layer)
6558 */
e311fd51 6559 Stat_t st;
e468d35b 6560 if (!PerlLIO_fstat(PerlIO_fileno(fp), &st) && S_ISREG(st.st_mode)) {
f54cb97a 6561 const Off_t offset = PerlIO_tell(fp);
58f1856e 6562 if (offset != (Off_t) -1 && st.st_size + append > offset) {
e468d35b
NIS
6563 (void) SvGROW(sv, (STRLEN)((st.st_size - offset) + append + 1));
6564 }
6565 }
c07a80fd 6566 rsptr = NULL;
6567 rslen = 0;
6568 }
3280af22 6569 else if (RsRECORD(PL_rs)) {
e311fd51 6570 I32 bytesread;
5b2b9c68
HM
6571 char *buffer;
6572
6573 /* Grab the size of the record we're getting */
3280af22 6574 recsize = SvIV(SvRV(PL_rs));
e311fd51 6575 buffer = SvGROW(sv, (STRLEN)(recsize + append + 1)) + append;
5b2b9c68
HM
6576 /* Go yank in */
6577#ifdef VMS
6578 /* VMS wants read instead of fread, because fread doesn't respect */
6579 /* RMS record boundaries. This is not necessarily a good thing to be */
e468d35b
NIS
6580 /* doing, but we've got no other real choice - except avoid stdio
6581 as implementation - perhaps write a :vms layer ?
6582 */
5b2b9c68
HM
6583 bytesread = PerlLIO_read(PerlIO_fileno(fp), buffer, recsize);
6584#else
6585 bytesread = PerlIO_read(fp, buffer, recsize);
6586#endif
27e6ca2d
AE
6587 if (bytesread < 0)
6588 bytesread = 0;
e311fd51 6589 SvCUR_set(sv, bytesread += append);
e670df4e 6590 buffer[bytesread] = '\0';
efd8b2ba 6591 goto return_string_or_null;
5b2b9c68 6592 }
3280af22 6593 else if (RsPARA(PL_rs)) {
c07a80fd 6594 rsptr = "\n\n";
6595 rslen = 2;
8bfdd7d9 6596 rspara = 1;
c07a80fd 6597 }
7d59b7e4
NIS
6598 else {
6599 /* Get $/ i.e. PL_rs into same encoding as stream wants */
6600 if (PerlIO_isutf8(fp)) {
6601 rsptr = SvPVutf8(PL_rs, rslen);
6602 }
6603 else {
6604 if (SvUTF8(PL_rs)) {
6605 if (!sv_utf8_downgrade(PL_rs, TRUE)) {
6606 Perl_croak(aTHX_ "Wide character in $/");
6607 }
6608 }
93524f2b 6609 rsptr = SvPV_const(PL_rs, rslen);
7d59b7e4
NIS
6610 }
6611 }
6612
c07a80fd 6613 rslast = rslen ? rsptr[rslen - 1] : '\0';
6614
8bfdd7d9 6615 if (rspara) { /* have to do this both before and after */
79072805 6616 do { /* to make sure file boundaries work right */
760ac839 6617 if (PerlIO_eof(fp))
a0d0e21e 6618 return 0;
760ac839 6619 i = PerlIO_getc(fp);
79072805 6620 if (i != '\n') {
a0d0e21e
LW
6621 if (i == -1)
6622 return 0;
760ac839 6623 PerlIO_ungetc(fp,i);
79072805
LW
6624 break;
6625 }
6626 } while (i != EOF);
6627 }
c07a80fd 6628
760ac839
LW
6629 /* See if we know enough about I/O mechanism to cheat it ! */
6630
6631 /* This used to be #ifdef test - it is made run-time test for ease
1c846c1f 6632 of abstracting out stdio interface. One call should be cheap
760ac839
LW
6633 enough here - and may even be a macro allowing compile
6634 time optimization.
6635 */
6636
6637 if (PerlIO_fast_gets(fp)) {
6638
6639 /*
6640 * We're going to steal some values from the stdio struct
6641 * and put EVERYTHING in the innermost loop into registers.
6642 */
6643 register STDCHAR *ptr;
6644 STRLEN bpx;
6645 I32 shortbuffered;
6646
16660edb 6647#if defined(VMS) && defined(PERLIO_IS_STDIO)
6648 /* An ungetc()d char is handled separately from the regular
6649 * buffer, so we getc() it back out and stuff it in the buffer.
6650 */
6651 i = PerlIO_getc(fp);
6652 if (i == EOF) return 0;
6653 *(--((*fp)->_ptr)) = (unsigned char) i;
6654 (*fp)->_cnt++;
6655#endif
c07a80fd 6656
c2960299 6657 /* Here is some breathtakingly efficient cheating */
c07a80fd 6658
a20bf0c3 6659 cnt = PerlIO_get_cnt(fp); /* get count into register */
e468d35b 6660 /* make sure we have the room */
7a5fa8a2 6661 if ((I32)(SvLEN(sv) - append) <= cnt + 1) {
e468d35b 6662 /* Not room for all of it
7a5fa8a2 6663 if we are looking for a separator and room for some
e468d35b
NIS
6664 */
6665 if (rslen && cnt > 80 && (I32)SvLEN(sv) > append) {
7a5fa8a2 6666 /* just process what we have room for */
79072805
LW
6667 shortbuffered = cnt - SvLEN(sv) + append + 1;
6668 cnt -= shortbuffered;
6669 }
6670 else {
6671 shortbuffered = 0;
bbce6d69 6672 /* remember that cnt can be negative */
eb160463 6673 SvGROW(sv, (STRLEN)(append + (cnt <= 0 ? 2 : (cnt + 1))));
79072805
LW
6674 }
6675 }
7a5fa8a2 6676 else
79072805 6677 shortbuffered = 0;
3f7c398e 6678 bp = (STDCHAR*)SvPVX_const(sv) + append; /* move these two too to registers */
a20bf0c3 6679 ptr = (STDCHAR*)PerlIO_get_ptr(fp);
16660edb 6680 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 6681 "Screamer: entering, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
16660edb 6682 DEBUG_P(PerlIO_printf(Perl_debug_log,
ba7abf9d 6683 "Screamer: entering: PerlIO * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
1c846c1f 6684 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
1d7c1841 6685 PTR2UV(PerlIO_has_base(fp) ? PerlIO_get_base(fp) : 0)));
79072805
LW
6686 for (;;) {
6687 screamer:
93a17b20 6688 if (cnt > 0) {
c07a80fd 6689 if (rslen) {
760ac839
LW
6690 while (cnt > 0) { /* this | eat */
6691 cnt--;
c07a80fd 6692 if ((*bp++ = *ptr++) == rslast) /* really | dust */
6693 goto thats_all_folks; /* screams | sed :-) */
6694 }
6695 }
6696 else {
1c846c1f
NIS
6697 Copy(ptr, bp, cnt, char); /* this | eat */
6698 bp += cnt; /* screams | dust */
c07a80fd 6699 ptr += cnt; /* louder | sed :-) */
a5f75d66 6700 cnt = 0;
93a17b20 6701 }
79072805
LW
6702 }
6703
748a9306 6704 if (shortbuffered) { /* oh well, must extend */
79072805
LW
6705 cnt = shortbuffered;
6706 shortbuffered = 0;
3f7c398e 6707 bpx = bp - (STDCHAR*)SvPVX_const(sv); /* box up before relocation */
79072805
LW
6708 SvCUR_set(sv, bpx);
6709 SvGROW(sv, SvLEN(sv) + append + cnt + 2);
3f7c398e 6710 bp = (STDCHAR*)SvPVX_const(sv) + bpx; /* unbox after relocation */
79072805
LW
6711 continue;
6712 }
6713
16660edb 6714 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841
GS
6715 "Screamer: going to getc, ptr=%"UVuf", cnt=%ld\n",
6716 PTR2UV(ptr),(long)cnt));
cc00df79 6717 PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* deregisterize cnt and ptr */
ba7abf9d 6718#if 0
16660edb 6719 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 6720 "Screamer: pre: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
1c846c1f 6721 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
1d7c1841 6722 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
ba7abf9d 6723#endif
1c846c1f 6724 /* This used to call 'filbuf' in stdio form, but as that behaves like
774d564b 6725 getc when cnt <= 0 we use PerlIO_getc here to avoid introducing
6726 another abstraction. */
760ac839 6727 i = PerlIO_getc(fp); /* get more characters */
ba7abf9d 6728#if 0
16660edb 6729 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 6730 "Screamer: post: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
1c846c1f 6731 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
1d7c1841 6732 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
ba7abf9d 6733#endif
a20bf0c3
JH
6734 cnt = PerlIO_get_cnt(fp);
6735 ptr = (STDCHAR*)PerlIO_get_ptr(fp); /* reregisterize cnt and ptr */
16660edb 6736 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 6737 "Screamer: after getc, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
79072805 6738
748a9306
LW
6739 if (i == EOF) /* all done for ever? */
6740 goto thats_really_all_folks;
6741
3f7c398e 6742 bpx = bp - (STDCHAR*)SvPVX_const(sv); /* box up before relocation */
79072805
LW
6743 SvCUR_set(sv, bpx);
6744 SvGROW(sv, bpx + cnt + 2);
3f7c398e 6745 bp = (STDCHAR*)SvPVX_const(sv) + bpx; /* unbox after relocation */
c07a80fd 6746
eb160463 6747 *bp++ = (STDCHAR)i; /* store character from PerlIO_getc */
79072805 6748
c07a80fd 6749 if (rslen && (STDCHAR)i == rslast) /* all done for now? */
79072805 6750 goto thats_all_folks;
79072805
LW
6751 }
6752
6753thats_all_folks:
3f7c398e 6754 if ((rslen > 1 && (STRLEN)(bp - (STDCHAR*)SvPVX_const(sv)) < rslen) ||
36477c24 6755 memNE((char*)bp - rslen, rsptr, rslen))
760ac839 6756 goto screamer; /* go back to the fray */
79072805
LW
6757thats_really_all_folks:
6758 if (shortbuffered)
6759 cnt += shortbuffered;
16660edb 6760 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 6761 "Screamer: quitting, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
cc00df79 6762 PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* put these back or we're in trouble */
16660edb 6763 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 6764 "Screamer: end: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
1c846c1f 6765 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
1d7c1841 6766 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
79072805 6767 *bp = '\0';
3f7c398e 6768 SvCUR_set(sv, bp - (STDCHAR*)SvPVX_const(sv)); /* set length */
16660edb 6769 DEBUG_P(PerlIO_printf(Perl_debug_log,
fb73857a 6770 "Screamer: done, len=%ld, string=|%.*s|\n",
3f7c398e 6771 (long)SvCUR(sv),(int)SvCUR(sv),SvPVX_const(sv)));
760ac839
LW
6772 }
6773 else
79072805 6774 {
6edd2cd5 6775 /*The big, slow, and stupid way. */
27da23d5 6776#ifdef USE_HEAP_INSTEAD_OF_STACK /* Even slower way. */
6edd2cd5
JH
6777 STDCHAR *buf = 0;
6778 New(0, buf, 8192, STDCHAR);
6779 assert(buf);
4d2c4e07 6780#else
6edd2cd5 6781 STDCHAR buf[8192];
4d2c4e07 6782#endif
79072805 6783
760ac839 6784screamer2:
c07a80fd 6785 if (rslen) {
6867be6d 6786 const register STDCHAR *bpe = buf + sizeof(buf);
760ac839 6787 bp = buf;
eb160463 6788 while ((i = PerlIO_getc(fp)) != EOF && (*bp++ = (STDCHAR)i) != rslast && bp < bpe)
760ac839
LW
6789 ; /* keep reading */
6790 cnt = bp - buf;
c07a80fd 6791 }
6792 else {
760ac839 6793 cnt = PerlIO_read(fp,(char*)buf, sizeof(buf));
16660edb 6794 /* Accomodate broken VAXC compiler, which applies U8 cast to
6795 * both args of ?: operator, causing EOF to change into 255
6796 */
37be0adf 6797 if (cnt > 0)
cbe9e203
JH
6798 i = (U8)buf[cnt - 1];
6799 else
37be0adf 6800 i = EOF;
c07a80fd 6801 }
79072805 6802
cbe9e203
JH
6803 if (cnt < 0)
6804 cnt = 0; /* we do need to re-set the sv even when cnt <= 0 */
6805 if (append)
6806 sv_catpvn(sv, (char *) buf, cnt);
6807 else
6808 sv_setpvn(sv, (char *) buf, cnt);
c07a80fd 6809
6810 if (i != EOF && /* joy */
6811 (!rslen ||
6812 SvCUR(sv) < rslen ||
3f7c398e 6813 memNE(SvPVX_const(sv) + SvCUR(sv) - rslen, rsptr, rslen)))
79072805
LW
6814 {
6815 append = -1;
63e4d877
CS
6816 /*
6817 * If we're reading from a TTY and we get a short read,
6818 * indicating that the user hit his EOF character, we need
6819 * to notice it now, because if we try to read from the TTY
6820 * again, the EOF condition will disappear.
6821 *
6822 * The comparison of cnt to sizeof(buf) is an optimization
6823 * that prevents unnecessary calls to feof().
6824 *
6825 * - jik 9/25/96
6826 */
6827 if (!(cnt < sizeof(buf) && PerlIO_eof(fp)))
6828 goto screamer2;
79072805 6829 }
6edd2cd5 6830
27da23d5 6831#ifdef USE_HEAP_INSTEAD_OF_STACK
6edd2cd5
JH
6832 Safefree(buf);
6833#endif
79072805
LW
6834 }
6835
8bfdd7d9 6836 if (rspara) { /* have to do this both before and after */
c07a80fd 6837 while (i != EOF) { /* to make sure file boundaries work right */
760ac839 6838 i = PerlIO_getc(fp);
79072805 6839 if (i != '\n') {
760ac839 6840 PerlIO_ungetc(fp,i);
79072805
LW
6841 break;
6842 }
6843 }
6844 }
c07a80fd 6845
efd8b2ba 6846return_string_or_null:
c07a80fd 6847 return (SvCUR(sv) - append) ? SvPVX(sv) : Nullch;
79072805
LW
6848}
6849
954c1994
GS
6850/*
6851=for apidoc sv_inc
6852
645c22ef
DM
6853Auto-increment of the value in the SV, doing string to numeric conversion
6854if necessary. Handles 'get' magic.
954c1994
GS
6855
6856=cut
6857*/
6858
79072805 6859void
864dbfa3 6860Perl_sv_inc(pTHX_ register SV *sv)
79072805
LW
6861{
6862 register char *d;
463ee0b2 6863 int flags;
79072805
LW
6864
6865 if (!sv)
6866 return;
b23a5f78
GB
6867 if (SvGMAGICAL(sv))
6868 mg_get(sv);
ed6116ce 6869 if (SvTHINKFIRST(sv)) {
765f542d
NC
6870 if (SvIsCOW(sv))
6871 sv_force_normal_flags(sv, 0);
0f15f207 6872 if (SvREADONLY(sv)) {
923e4eb5 6873 if (IN_PERL_RUNTIME)
cea2e8a9 6874 Perl_croak(aTHX_ PL_no_modify);
0f15f207 6875 }
a0d0e21e 6876 if (SvROK(sv)) {
b5be31e9 6877 IV i;
9e7bc3e8
JD
6878 if (SvAMAGIC(sv) && AMG_CALLun(sv,inc))
6879 return;
56431972 6880 i = PTR2IV(SvRV(sv));
b5be31e9
SM
6881 sv_unref(sv);
6882 sv_setiv(sv, i);
a0d0e21e 6883 }
ed6116ce 6884 }
8990e307 6885 flags = SvFLAGS(sv);
28e5dec8
JH
6886 if ((flags & (SVp_NOK|SVp_IOK)) == SVp_NOK) {
6887 /* It's (privately or publicly) a float, but not tested as an
6888 integer, so test it to see. */
d460ef45 6889 (void) SvIV(sv);
28e5dec8
JH
6890 flags = SvFLAGS(sv);
6891 }
6892 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
6893 /* It's publicly an integer, or privately an integer-not-float */
59d8ce62 6894#ifdef PERL_PRESERVE_IVUV
28e5dec8 6895 oops_its_int:
59d8ce62 6896#endif
25da4f38
IZ
6897 if (SvIsUV(sv)) {
6898 if (SvUVX(sv) == UV_MAX)
a1e868e7 6899 sv_setnv(sv, UV_MAX_P1);
25da4f38
IZ
6900 else
6901 (void)SvIOK_only_UV(sv);
607fa7f2 6902 SvUV_set(sv, SvUVX(sv) + 1);
25da4f38
IZ
6903 } else {
6904 if (SvIVX(sv) == IV_MAX)
28e5dec8 6905 sv_setuv(sv, (UV)IV_MAX + 1);
25da4f38
IZ
6906 else {
6907 (void)SvIOK_only(sv);
45977657 6908 SvIV_set(sv, SvIVX(sv) + 1);
1c846c1f 6909 }
55497cff 6910 }
79072805
LW
6911 return;
6912 }
28e5dec8
JH
6913 if (flags & SVp_NOK) {
6914 (void)SvNOK_only(sv);
9d6ce603 6915 SvNV_set(sv, SvNVX(sv) + 1.0);
28e5dec8
JH
6916 return;
6917 }
6918
3f7c398e 6919 if (!(flags & SVp_POK) || !*SvPVX_const(sv)) {
28e5dec8 6920 if ((flags & SVTYPEMASK) < SVt_PVIV)
f5282e15 6921 sv_upgrade(sv, ((flags & SVTYPEMASK) > SVt_IV ? SVt_PVIV : SVt_IV));
28e5dec8 6922 (void)SvIOK_only(sv);
45977657 6923 SvIV_set(sv, 1);
79072805
LW
6924 return;
6925 }
463ee0b2 6926 d = SvPVX(sv);
79072805
LW
6927 while (isALPHA(*d)) d++;
6928 while (isDIGIT(*d)) d++;
6929 if (*d) {
28e5dec8 6930#ifdef PERL_PRESERVE_IVUV
d1be9408 6931 /* Got to punt this as an integer if needs be, but we don't issue
28e5dec8
JH
6932 warnings. Probably ought to make the sv_iv_please() that does
6933 the conversion if possible, and silently. */
504618e9 6934 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), NULL);
28e5dec8
JH
6935 if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
6936 /* Need to try really hard to see if it's an integer.
6937 9.22337203685478e+18 is an integer.
6938 but "9.22337203685478e+18" + 0 is UV=9223372036854779904
6939 so $a="9.22337203685478e+18"; $a+0; $a++
6940 needs to be the same as $a="9.22337203685478e+18"; $a++
6941 or we go insane. */
d460ef45 6942
28e5dec8
JH
6943 (void) sv_2iv(sv);
6944 if (SvIOK(sv))
6945 goto oops_its_int;
6946
6947 /* sv_2iv *should* have made this an NV */
6948 if (flags & SVp_NOK) {
6949 (void)SvNOK_only(sv);
9d6ce603 6950 SvNV_set(sv, SvNVX(sv) + 1.0);
28e5dec8
JH
6951 return;
6952 }
6953 /* I don't think we can get here. Maybe I should assert this
6954 And if we do get here I suspect that sv_setnv will croak. NWC
6955 Fall through. */
6956#if defined(USE_LONG_DOUBLE)
6957 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"PERL_PRIgldbl"\n",
3f7c398e 6958 SvPVX_const(sv), SvIVX(sv), SvNVX(sv)));
28e5dec8 6959#else
1779d84d 6960 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"NVgf"\n",
3f7c398e 6961 SvPVX_const(sv), SvIVX(sv), SvNVX(sv)));
28e5dec8
JH
6962#endif
6963 }
6964#endif /* PERL_PRESERVE_IVUV */
3f7c398e 6965 sv_setnv(sv,Atof(SvPVX_const(sv)) + 1.0);
79072805
LW
6966 return;
6967 }
6968 d--;
3f7c398e 6969 while (d >= SvPVX_const(sv)) {
79072805
LW
6970 if (isDIGIT(*d)) {
6971 if (++*d <= '9')
6972 return;
6973 *(d--) = '0';
6974 }
6975 else {
9d116dd7
JH
6976#ifdef EBCDIC
6977 /* MKS: The original code here died if letters weren't consecutive.
6978 * at least it didn't have to worry about non-C locales. The
6979 * new code assumes that ('z'-'a')==('Z'-'A'), letters are
1c846c1f 6980 * arranged in order (although not consecutively) and that only
9d116dd7
JH
6981 * [A-Za-z] are accepted by isALPHA in the C locale.
6982 */
6983 if (*d != 'z' && *d != 'Z') {
6984 do { ++*d; } while (!isALPHA(*d));
6985 return;
6986 }
6987 *(d--) -= 'z' - 'a';
6988#else
79072805
LW
6989 ++*d;
6990 if (isALPHA(*d))
6991 return;
6992 *(d--) -= 'z' - 'a' + 1;
9d116dd7 6993#endif
79072805
LW
6994 }
6995 }
6996 /* oh,oh, the number grew */
6997 SvGROW(sv, SvCUR(sv) + 2);
b162af07 6998 SvCUR_set(sv, SvCUR(sv) + 1);
3f7c398e 6999 for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX_const(sv); d--)
79072805
LW
7000 *d = d[-1];
7001 if (isDIGIT(d[1]))
7002 *d = '1';
7003 else
7004 *d = d[1];
7005}
7006
954c1994
GS
7007/*
7008=for apidoc sv_dec
7009
645c22ef
DM
7010Auto-decrement of the value in the SV, doing string to numeric conversion
7011if necessary. Handles 'get' magic.
954c1994
GS
7012
7013=cut
7014*/
7015
79072805 7016void
864dbfa3 7017Perl_sv_dec(pTHX_ register SV *sv)
79072805 7018{
463ee0b2
LW
7019 int flags;
7020
79072805
LW
7021 if (!sv)
7022 return;
b23a5f78
GB
7023 if (SvGMAGICAL(sv))
7024 mg_get(sv);
ed6116ce 7025 if (SvTHINKFIRST(sv)) {
765f542d
NC
7026 if (SvIsCOW(sv))
7027 sv_force_normal_flags(sv, 0);
0f15f207 7028 if (SvREADONLY(sv)) {
923e4eb5 7029 if (IN_PERL_RUNTIME)
cea2e8a9 7030 Perl_croak(aTHX_ PL_no_modify);
0f15f207 7031 }
a0d0e21e 7032 if (SvROK(sv)) {
b5be31e9 7033 IV i;
9e7bc3e8
JD
7034 if (SvAMAGIC(sv) && AMG_CALLun(sv,dec))
7035 return;
56431972 7036 i = PTR2IV(SvRV(sv));
b5be31e9
SM
7037 sv_unref(sv);
7038 sv_setiv(sv, i);
a0d0e21e 7039 }
ed6116ce 7040 }
28e5dec8
JH
7041 /* Unlike sv_inc we don't have to worry about string-never-numbers
7042 and keeping them magic. But we mustn't warn on punting */
8990e307 7043 flags = SvFLAGS(sv);
28e5dec8
JH
7044 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
7045 /* It's publicly an integer, or privately an integer-not-float */
59d8ce62 7046#ifdef PERL_PRESERVE_IVUV
28e5dec8 7047 oops_its_int:
59d8ce62 7048#endif
25da4f38
IZ
7049 if (SvIsUV(sv)) {
7050 if (SvUVX(sv) == 0) {
7051 (void)SvIOK_only(sv);
45977657 7052 SvIV_set(sv, -1);
25da4f38
IZ
7053 }
7054 else {
7055 (void)SvIOK_only_UV(sv);
607fa7f2 7056 SvUV_set(sv, SvUVX(sv) + 1);
1c846c1f 7057 }
25da4f38
IZ
7058 } else {
7059 if (SvIVX(sv) == IV_MIN)
65202027 7060 sv_setnv(sv, (NV)IV_MIN - 1.0);
25da4f38
IZ
7061 else {
7062 (void)SvIOK_only(sv);
45977657 7063 SvIV_set(sv, SvIVX(sv) - 1);
1c846c1f 7064 }
55497cff 7065 }
7066 return;
7067 }
28e5dec8 7068 if (flags & SVp_NOK) {
9d6ce603 7069 SvNV_set(sv, SvNVX(sv) - 1.0);
28e5dec8
JH
7070 (void)SvNOK_only(sv);
7071 return;
7072 }
8990e307 7073 if (!(flags & SVp_POK)) {
4633a7c4
LW
7074 if ((flags & SVTYPEMASK) < SVt_PVNV)
7075 sv_upgrade(sv, SVt_NV);
f599b64b 7076 SvNV_set(sv, 1.0);
a0d0e21e 7077 (void)SvNOK_only(sv);
79072805
LW
7078 return;
7079 }
28e5dec8
JH
7080#ifdef PERL_PRESERVE_IVUV
7081 {
504618e9 7082 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), NULL);
28e5dec8
JH
7083 if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
7084 /* Need to try really hard to see if it's an integer.
7085 9.22337203685478e+18 is an integer.
7086 but "9.22337203685478e+18" + 0 is UV=9223372036854779904
7087 so $a="9.22337203685478e+18"; $a+0; $a--
7088 needs to be the same as $a="9.22337203685478e+18"; $a--
7089 or we go insane. */
d460ef45 7090
28e5dec8
JH
7091 (void) sv_2iv(sv);
7092 if (SvIOK(sv))
7093 goto oops_its_int;
7094
7095 /* sv_2iv *should* have made this an NV */
7096 if (flags & SVp_NOK) {
7097 (void)SvNOK_only(sv);
9d6ce603 7098 SvNV_set(sv, SvNVX(sv) - 1.0);
28e5dec8
JH
7099 return;
7100 }
7101 /* I don't think we can get here. Maybe I should assert this
7102 And if we do get here I suspect that sv_setnv will croak. NWC
7103 Fall through. */
7104#if defined(USE_LONG_DOUBLE)
7105 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"PERL_PRIgldbl"\n",
3f7c398e 7106 SvPVX_const(sv), SvIVX(sv), SvNVX(sv)));
28e5dec8 7107#else
1779d84d 7108 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"NVgf"\n",
3f7c398e 7109 SvPVX_const(sv), SvIVX(sv), SvNVX(sv)));
28e5dec8
JH
7110#endif
7111 }
7112 }
7113#endif /* PERL_PRESERVE_IVUV */
3f7c398e 7114 sv_setnv(sv,Atof(SvPVX_const(sv)) - 1.0); /* punt */
79072805
LW
7115}
7116
954c1994
GS
7117/*
7118=for apidoc sv_mortalcopy
7119
645c22ef 7120Creates a new SV which is a copy of the original SV (using C<sv_setsv>).
d4236ebc
DM
7121The new SV is marked as mortal. It will be destroyed "soon", either by an
7122explicit call to FREETMPS, or by an implicit call at places such as
7123statement boundaries. See also C<sv_newmortal> and C<sv_2mortal>.
954c1994
GS
7124
7125=cut
7126*/
7127
79072805
LW
7128/* Make a string that will exist for the duration of the expression
7129 * evaluation. Actually, it may have to last longer than that, but
7130 * hopefully we won't free it until it has been assigned to a
7131 * permanent location. */
7132
7133SV *
864dbfa3 7134Perl_sv_mortalcopy(pTHX_ SV *oldstr)
79072805 7135{
463ee0b2 7136 register SV *sv;
b881518d 7137
4561caa4 7138 new_SV(sv);
79072805 7139 sv_setsv(sv,oldstr);
677b06e3
GS
7140 EXTEND_MORTAL(1);
7141 PL_tmps_stack[++PL_tmps_ix] = sv;
8990e307
LW
7142 SvTEMP_on(sv);
7143 return sv;
7144}
7145
954c1994
GS
7146/*
7147=for apidoc sv_newmortal
7148
645c22ef 7149Creates a new null SV which is mortal. The reference count of the SV is
d4236ebc
DM
7150set to 1. It will be destroyed "soon", either by an explicit call to
7151FREETMPS, or by an implicit call at places such as statement boundaries.
7152See also C<sv_mortalcopy> and C<sv_2mortal>.
954c1994
GS
7153
7154=cut
7155*/
7156
8990e307 7157SV *
864dbfa3 7158Perl_sv_newmortal(pTHX)
8990e307
LW
7159{
7160 register SV *sv;
7161
4561caa4 7162 new_SV(sv);
8990e307 7163 SvFLAGS(sv) = SVs_TEMP;
677b06e3
GS
7164 EXTEND_MORTAL(1);
7165 PL_tmps_stack[++PL_tmps_ix] = sv;
79072805
LW
7166 return sv;
7167}
7168
954c1994
GS
7169/*
7170=for apidoc sv_2mortal
7171
d4236ebc
DM
7172Marks an existing SV as mortal. The SV will be destroyed "soon", either
7173by an explicit call to FREETMPS, or by an implicit call at places such as
37d2ac18
NC
7174statement boundaries. SvTEMP() is turned on which means that the SV's
7175string buffer can be "stolen" if this SV is copied. See also C<sv_newmortal>
7176and C<sv_mortalcopy>.
954c1994
GS
7177
7178=cut
7179*/
7180
79072805 7181SV *
864dbfa3 7182Perl_sv_2mortal(pTHX_ register SV *sv)
79072805 7183{
27da23d5 7184 dVAR;
79072805
LW
7185 if (!sv)
7186 return sv;
d689ffdd 7187 if (SvREADONLY(sv) && SvIMMORTAL(sv))
11162842 7188 return sv;
677b06e3
GS
7189 EXTEND_MORTAL(1);
7190 PL_tmps_stack[++PL_tmps_ix] = sv;
8990e307 7191 SvTEMP_on(sv);
79072805
LW
7192 return sv;
7193}
7194
954c1994
GS
7195/*
7196=for apidoc newSVpv
7197
7198Creates a new SV and copies a string into it. The reference count for the
7199SV is set to 1. If C<len> is zero, Perl will compute the length using
7200strlen(). For efficiency, consider using C<newSVpvn> instead.
7201
7202=cut
7203*/
7204
79072805 7205SV *
864dbfa3 7206Perl_newSVpv(pTHX_ const char *s, STRLEN len)
79072805 7207{
463ee0b2 7208 register SV *sv;
79072805 7209
4561caa4 7210 new_SV(sv);
616d8c9c 7211 sv_setpvn(sv,s,len ? len : strlen(s));
79072805
LW
7212 return sv;
7213}
7214
954c1994
GS
7215/*
7216=for apidoc newSVpvn
7217
7218Creates a new SV and copies a string into it. The reference count for the
1c846c1f 7219SV is set to 1. Note that if C<len> is zero, Perl will create a zero length
954c1994 7220string. You are responsible for ensuring that the source string is at least
9e09f5f2 7221C<len> bytes long. If the C<s> argument is NULL the new SV will be undefined.
954c1994
GS
7222
7223=cut
7224*/
7225
9da1e3b5 7226SV *
864dbfa3 7227Perl_newSVpvn(pTHX_ const char *s, STRLEN len)
9da1e3b5
MUN
7228{
7229 register SV *sv;
7230
7231 new_SV(sv);
9da1e3b5
MUN
7232 sv_setpvn(sv,s,len);
7233 return sv;
7234}
7235
bd08039b
NC
7236
7237/*
926f8064 7238=for apidoc newSVhek
bd08039b
NC
7239
7240Creates a new SV from the hash key structure. It will generate scalars that
5aaec2b4
NC
7241point to the shared string table where possible. Returns a new (undefined)
7242SV if the hek is NULL.
bd08039b
NC
7243
7244=cut
7245*/
7246
7247SV *
c1b02ed8 7248Perl_newSVhek(pTHX_ const HEK *hek)
bd08039b 7249{
5aaec2b4
NC
7250 if (!hek) {
7251 SV *sv;
7252
7253 new_SV(sv);
7254 return sv;
7255 }
7256
bd08039b
NC
7257 if (HEK_LEN(hek) == HEf_SVKEY) {
7258 return newSVsv(*(SV**)HEK_KEY(hek));
7259 } else {
7260 const int flags = HEK_FLAGS(hek);
7261 if (flags & HVhek_WASUTF8) {
7262 /* Trouble :-)
7263 Andreas would like keys he put in as utf8 to come back as utf8
7264 */
7265 STRLEN utf8_len = HEK_LEN(hek);
7266 U8 *as_utf8 = bytes_to_utf8 ((U8*)HEK_KEY(hek), &utf8_len);
7267 SV *sv = newSVpvn ((char*)as_utf8, utf8_len);
7268
7269 SvUTF8_on (sv);
7270 Safefree (as_utf8); /* bytes_to_utf8() allocates a new string */
7271 return sv;
7272 } else if (flags & HVhek_REHASH) {
7273 /* We don't have a pointer to the hv, so we have to replicate the
7274 flag into every HEK. This hv is using custom a hasing
7275 algorithm. Hence we can't return a shared string scalar, as
7276 that would contain the (wrong) hash value, and might get passed
7277 into an hv routine with a regular hash */
7278
7279 SV *sv = newSVpvn (HEK_KEY(hek), HEK_LEN(hek));
7280 if (HEK_UTF8(hek))
7281 SvUTF8_on (sv);
7282 return sv;
7283 }
7284 /* This will be overwhelminly the most common case. */
7285 return newSVpvn_share(HEK_KEY(hek),
7286 (HEK_UTF8(hek) ? -HEK_LEN(hek) : HEK_LEN(hek)),
7287 HEK_HASH(hek));
7288 }
7289}
7290
1c846c1f
NIS
7291/*
7292=for apidoc newSVpvn_share
7293
3f7c398e 7294Creates a new SV with its SvPVX_const pointing to a shared string in the string
645c22ef
DM
7295table. If the string does not already exist in the table, it is created
7296first. Turns on READONLY and FAKE. The string's hash is stored in the UV
7297slot of the SV; if the C<hash> parameter is non-zero, that value is used;
7298otherwise the hash is computed. The idea here is that as the string table
3f7c398e 7299is used for shared hash keys these strings will have SvPVX_const == HeKEY and
645c22ef 7300hash lookup will avoid string compare.
1c846c1f
NIS
7301
7302=cut
7303*/
7304
7305SV *
c3654f1a 7306Perl_newSVpvn_share(pTHX_ const char *src, I32 len, U32 hash)
1c846c1f
NIS
7307{
7308 register SV *sv;
c3654f1a
IH
7309 bool is_utf8 = FALSE;
7310 if (len < 0) {
77caf834 7311 STRLEN tmplen = -len;
c3654f1a 7312 is_utf8 = TRUE;
75a54232 7313 /* See the note in hv.c:hv_fetch() --jhi */
e1ec3a88 7314 src = (char*)bytes_from_utf8((const U8*)src, &tmplen, &is_utf8);
75a54232
JH
7315 len = tmplen;
7316 }
1c846c1f 7317 if (!hash)
5afd6d42 7318 PERL_HASH(hash, src, len);
1c846c1f 7319 new_SV(sv);
bdd68bc3 7320 sv_upgrade(sv, SVt_PV);
f880fe2f 7321 SvPV_set(sv, sharepvn(src, is_utf8?-len:len, hash));
b162af07 7322 SvCUR_set(sv, len);
b162af07 7323 SvLEN_set(sv, 0);
1c846c1f
NIS
7324 SvREADONLY_on(sv);
7325 SvFAKE_on(sv);
7326 SvPOK_on(sv);
c3654f1a
IH
7327 if (is_utf8)
7328 SvUTF8_on(sv);
1c846c1f
NIS
7329 return sv;
7330}
7331
645c22ef 7332
cea2e8a9 7333#if defined(PERL_IMPLICIT_CONTEXT)
645c22ef
DM
7334
7335/* pTHX_ magic can't cope with varargs, so this is a no-context
7336 * version of the main function, (which may itself be aliased to us).
7337 * Don't access this version directly.
7338 */
7339
46fc3d4c 7340SV *
cea2e8a9 7341Perl_newSVpvf_nocontext(const char* pat, ...)
46fc3d4c 7342{
cea2e8a9 7343 dTHX;
46fc3d4c 7344 register SV *sv;
7345 va_list args;
46fc3d4c 7346 va_start(args, pat);
c5be433b 7347 sv = vnewSVpvf(pat, &args);
46fc3d4c 7348 va_end(args);
7349 return sv;
7350}
cea2e8a9 7351#endif
46fc3d4c 7352
954c1994
GS
7353/*
7354=for apidoc newSVpvf
7355
645c22ef 7356Creates a new SV and initializes it with the string formatted like
954c1994
GS
7357C<sprintf>.
7358
7359=cut
7360*/
7361
cea2e8a9
GS
7362SV *
7363Perl_newSVpvf(pTHX_ const char* pat, ...)
7364{
7365 register SV *sv;
7366 va_list args;
cea2e8a9 7367 va_start(args, pat);
c5be433b 7368 sv = vnewSVpvf(pat, &args);
cea2e8a9
GS
7369 va_end(args);
7370 return sv;
7371}
46fc3d4c 7372
645c22ef
DM
7373/* backend for newSVpvf() and newSVpvf_nocontext() */
7374
79072805 7375SV *
c5be433b
GS
7376Perl_vnewSVpvf(pTHX_ const char* pat, va_list* args)
7377{
7378 register SV *sv;
7379 new_SV(sv);
7380 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
7381 return sv;
7382}
7383
954c1994
GS
7384/*
7385=for apidoc newSVnv
7386
7387Creates a new SV and copies a floating point value into it.
7388The reference count for the SV is set to 1.
7389
7390=cut
7391*/
7392
c5be433b 7393SV *
65202027 7394Perl_newSVnv(pTHX_ NV n)
79072805 7395{
463ee0b2 7396 register SV *sv;
79072805 7397
4561caa4 7398 new_SV(sv);
79072805
LW
7399 sv_setnv(sv,n);
7400 return sv;
7401}
7402
954c1994
GS
7403/*
7404=for apidoc newSViv
7405
7406Creates a new SV and copies an integer into it. The reference count for the
7407SV is set to 1.
7408
7409=cut
7410*/
7411
79072805 7412SV *
864dbfa3 7413Perl_newSViv(pTHX_ IV i)
79072805 7414{
463ee0b2 7415 register SV *sv;
79072805 7416
4561caa4 7417 new_SV(sv);
79072805
LW
7418 sv_setiv(sv,i);
7419 return sv;
7420}
7421
954c1994 7422/*
1a3327fb
JH
7423=for apidoc newSVuv
7424
7425Creates a new SV and copies an unsigned integer into it.
7426The reference count for the SV is set to 1.
7427
7428=cut
7429*/
7430
7431SV *
7432Perl_newSVuv(pTHX_ UV u)
7433{
7434 register SV *sv;
7435
7436 new_SV(sv);
7437 sv_setuv(sv,u);
7438 return sv;
7439}
7440
7441/*
954c1994
GS
7442=for apidoc newRV_noinc
7443
7444Creates an RV wrapper for an SV. The reference count for the original
7445SV is B<not> incremented.
7446
7447=cut
7448*/
7449
2304df62 7450SV *
864dbfa3 7451Perl_newRV_noinc(pTHX_ SV *tmpRef)
2304df62
AD
7452{
7453 register SV *sv;
7454
4561caa4 7455 new_SV(sv);
2304df62 7456 sv_upgrade(sv, SVt_RV);
76e3520e 7457 SvTEMP_off(tmpRef);
b162af07 7458 SvRV_set(sv, tmpRef);
2304df62 7459 SvROK_on(sv);
2304df62
AD
7460 return sv;
7461}
7462
ff276b08 7463/* newRV_inc is the official function name to use now.
645c22ef
DM
7464 * newRV_inc is in fact #defined to newRV in sv.h
7465 */
7466
5f05dabc 7467SV *
864dbfa3 7468Perl_newRV(pTHX_ SV *tmpRef)
5f05dabc 7469{
5f6447b6 7470 return newRV_noinc(SvREFCNT_inc(tmpRef));
5f05dabc 7471}
5f05dabc 7472
954c1994
GS
7473/*
7474=for apidoc newSVsv
7475
7476Creates a new SV which is an exact duplicate of the original SV.
645c22ef 7477(Uses C<sv_setsv>).
954c1994
GS
7478
7479=cut
7480*/
7481
79072805 7482SV *
864dbfa3 7483Perl_newSVsv(pTHX_ register SV *old)
79072805 7484{
463ee0b2 7485 register SV *sv;
79072805
LW
7486
7487 if (!old)
7488 return Nullsv;
8990e307 7489 if (SvTYPE(old) == SVTYPEMASK) {
0453d815 7490 if (ckWARN_d(WARN_INTERNAL))
9014280d 7491 Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "semi-panic: attempt to dup freed string");
79072805
LW
7492 return Nullsv;
7493 }
4561caa4 7494 new_SV(sv);
e90aabeb
NC
7495 /* SV_GMAGIC is the default for sv_setv()
7496 SV_NOSTEAL prevents TEMP buffers being, well, stolen, and saves games
7497 with SvTEMP_off and SvTEMP_on round a call to sv_setsv. */
7498 sv_setsv_flags(sv, old, SV_GMAGIC | SV_NOSTEAL);
463ee0b2 7499 return sv;
79072805
LW
7500}
7501
645c22ef
DM
7502/*
7503=for apidoc sv_reset
7504
7505Underlying implementation for the C<reset> Perl function.
7506Note that the perl-level function is vaguely deprecated.
7507
7508=cut
7509*/
7510
79072805 7511void
e1ec3a88 7512Perl_sv_reset(pTHX_ register const char *s, HV *stash)
79072805 7513{
27da23d5 7514 dVAR;
4802d5d7 7515 char todo[PERL_UCHAR_MAX+1];
79072805 7516
49d8d3a1
MB
7517 if (!stash)
7518 return;
7519
79072805 7520 if (!*s) { /* reset ?? searches */
8d2f4536
NC
7521 MAGIC *mg = mg_find((SV *)stash, PERL_MAGIC_symtab);
7522 if (mg) {
7523 PMOP *pm = (PMOP *) mg->mg_obj;
7524 while (pm) {
7525 pm->op_pmdynflags &= ~PMdf_USED;
7526 pm = pm->op_pmnext;
7527 }
79072805
LW
7528 }
7529 return;
7530 }
7531
7532 /* reset variables */
7533
7534 if (!HvARRAY(stash))
7535 return;
463ee0b2
LW
7536
7537 Zero(todo, 256, char);
79072805 7538 while (*s) {
b464bac0
AL
7539 I32 max;
7540 I32 i = (unsigned char)*s;
79072805
LW
7541 if (s[1] == '-') {
7542 s += 2;
7543 }
4802d5d7 7544 max = (unsigned char)*s++;
79072805 7545 for ( ; i <= max; i++) {
463ee0b2
LW
7546 todo[i] = 1;
7547 }
a0d0e21e 7548 for (i = 0; i <= (I32) HvMAX(stash); i++) {
b464bac0 7549 HE *entry;
79072805 7550 for (entry = HvARRAY(stash)[i];
9e35f4b3
GS
7551 entry;
7552 entry = HeNEXT(entry))
7553 {
b464bac0
AL
7554 register GV *gv;
7555 register SV *sv;
7556
1edc1566 7557 if (!todo[(U8)*HeKEY(entry)])
463ee0b2 7558 continue;
1edc1566 7559 gv = (GV*)HeVAL(entry);
79072805 7560 sv = GvSV(gv);
9e35f4b3
GS
7561 if (SvTHINKFIRST(sv)) {
7562 if (!SvREADONLY(sv) && SvROK(sv))
7563 sv_unref(sv);
7564 continue;
7565 }
0c34ef67 7566 SvOK_off(sv);
79072805
LW
7567 if (SvTYPE(sv) >= SVt_PV) {
7568 SvCUR_set(sv, 0);
3f7c398e 7569 if (SvPVX_const(sv) != Nullch)
463ee0b2 7570 *SvPVX(sv) = '\0';
44a8e56a 7571 SvTAINT(sv);
79072805
LW
7572 }
7573 if (GvAV(gv)) {
7574 av_clear(GvAV(gv));
7575 }
bfcb3514 7576 if (GvHV(gv) && !HvNAME_get(GvHV(gv))) {
463ee0b2 7577 hv_clear(GvHV(gv));
2f42fcb0 7578#ifndef PERL_MICRO
fa6a1c44 7579#ifdef USE_ENVIRON_ARRAY
4efc5df6
GS
7580 if (gv == PL_envgv
7581# ifdef USE_ITHREADS
7582 && PL_curinterp == aTHX
7583# endif
7584 )
7585 {
79072805 7586 environ[0] = Nullch;
4efc5df6 7587 }
a0d0e21e 7588#endif
2f42fcb0 7589#endif /* !PERL_MICRO */
79072805
LW
7590 }
7591 }
7592 }
7593 }
7594}
7595
645c22ef
DM
7596/*
7597=for apidoc sv_2io
7598
7599Using various gambits, try to get an IO from an SV: the IO slot if its a
7600GV; or the recursive result if we're an RV; or the IO slot of the symbol
7601named after the PV if we're a string.
7602
7603=cut
7604*/
7605
46fc3d4c 7606IO*
864dbfa3 7607Perl_sv_2io(pTHX_ SV *sv)
46fc3d4c 7608{
7609 IO* io;
7610 GV* gv;
7611
7612 switch (SvTYPE(sv)) {
7613 case SVt_PVIO:
7614 io = (IO*)sv;
7615 break;
7616 case SVt_PVGV:
7617 gv = (GV*)sv;
7618 io = GvIO(gv);
7619 if (!io)
cea2e8a9 7620 Perl_croak(aTHX_ "Bad filehandle: %s", GvNAME(gv));
46fc3d4c 7621 break;
7622 default:
7623 if (!SvOK(sv))
cea2e8a9 7624 Perl_croak(aTHX_ PL_no_usym, "filehandle");
46fc3d4c 7625 if (SvROK(sv))
7626 return sv_2io(SvRV(sv));
7a5fd60d 7627 gv = gv_fetchsv(sv, FALSE, SVt_PVIO);
46fc3d4c 7628 if (gv)
7629 io = GvIO(gv);
7630 else
7631 io = 0;
7632 if (!io)
35c1215d 7633 Perl_croak(aTHX_ "Bad filehandle: %"SVf, sv);
46fc3d4c 7634 break;
7635 }
7636 return io;
7637}
7638
645c22ef
DM
7639/*
7640=for apidoc sv_2cv
7641
7642Using various gambits, try to get a CV from an SV; in addition, try if
7643possible to set C<*st> and C<*gvp> to the stash and GV associated with it.
7644
7645=cut
7646*/
7647
79072805 7648CV *
864dbfa3 7649Perl_sv_2cv(pTHX_ SV *sv, HV **st, GV **gvp, I32 lref)
79072805 7650{
27da23d5 7651 dVAR;
c04a4dfe
JH
7652 GV *gv = Nullgv;
7653 CV *cv = Nullcv;
79072805
LW
7654
7655 if (!sv)
93a17b20 7656 return *gvp = Nullgv, Nullcv;
79072805 7657 switch (SvTYPE(sv)) {
79072805
LW
7658 case SVt_PVCV:
7659 *st = CvSTASH(sv);
7660 *gvp = Nullgv;
7661 return (CV*)sv;
7662 case SVt_PVHV:
7663 case SVt_PVAV:
7664 *gvp = Nullgv;
7665 return Nullcv;
8990e307
LW
7666 case SVt_PVGV:
7667 gv = (GV*)sv;
a0d0e21e 7668 *gvp = gv;
8990e307
LW
7669 *st = GvESTASH(gv);
7670 goto fix_gv;
7671
79072805 7672 default:
a0d0e21e
LW
7673 if (SvGMAGICAL(sv))
7674 mg_get(sv);
7675 if (SvROK(sv)) {
f5284f61
IZ
7676 SV **sp = &sv; /* Used in tryAMAGICunDEREF macro. */
7677 tryAMAGICunDEREF(to_cv);
7678
62f274bf
GS
7679 sv = SvRV(sv);
7680 if (SvTYPE(sv) == SVt_PVCV) {
7681 cv = (CV*)sv;
7682 *gvp = Nullgv;
7683 *st = CvSTASH(cv);
7684 return cv;
7685 }
7686 else if(isGV(sv))
7687 gv = (GV*)sv;
7688 else
cea2e8a9 7689 Perl_croak(aTHX_ "Not a subroutine reference");
a0d0e21e 7690 }
62f274bf 7691 else if (isGV(sv))
79072805
LW
7692 gv = (GV*)sv;
7693 else
7a5fd60d 7694 gv = gv_fetchsv(sv, lref, SVt_PVCV);
79072805
LW
7695 *gvp = gv;
7696 if (!gv)
7697 return Nullcv;
7698 *st = GvESTASH(gv);
8990e307 7699 fix_gv:
8ebc5c01 7700 if (lref && !GvCVu(gv)) {
4633a7c4 7701 SV *tmpsv;
748a9306 7702 ENTER;
4633a7c4 7703 tmpsv = NEWSV(704,0);
16660edb 7704 gv_efullname3(tmpsv, gv, Nullch);
f6ec51f7
GS
7705 /* XXX this is probably not what they think they're getting.
7706 * It has the same effect as "sub name;", i.e. just a forward
7707 * declaration! */
774d564b 7708 newSUB(start_subparse(FALSE, 0),
4633a7c4
LW
7709 newSVOP(OP_CONST, 0, tmpsv),
7710 Nullop,
8990e307 7711 Nullop);
748a9306 7712 LEAVE;
8ebc5c01 7713 if (!GvCVu(gv))
35c1215d
NC
7714 Perl_croak(aTHX_ "Unable to create sub named \"%"SVf"\"",
7715 sv);
8990e307 7716 }
8ebc5c01 7717 return GvCVu(gv);
79072805
LW
7718 }
7719}
7720
c461cf8f
JH
7721/*
7722=for apidoc sv_true
7723
7724Returns true if the SV has a true value by Perl's rules.
645c22ef
DM
7725Use the C<SvTRUE> macro instead, which may call C<sv_true()> or may
7726instead use an in-line version.
c461cf8f
JH
7727
7728=cut
7729*/
7730
79072805 7731I32
864dbfa3 7732Perl_sv_true(pTHX_ register SV *sv)
79072805 7733{
8990e307
LW
7734 if (!sv)
7735 return 0;
79072805 7736 if (SvPOK(sv)) {
e1ec3a88 7737 const register XPV* tXpv;
4e35701f 7738 if ((tXpv = (XPV*)SvANY(sv)) &&
c2f1de04 7739 (tXpv->xpv_cur > 1 ||
339049b0 7740 (tXpv->xpv_cur && *sv->sv_u.svu_pv != '0')))
79072805
LW
7741 return 1;
7742 else
7743 return 0;
7744 }
7745 else {
7746 if (SvIOK(sv))
463ee0b2 7747 return SvIVX(sv) != 0;
79072805
LW
7748 else {
7749 if (SvNOK(sv))
463ee0b2 7750 return SvNVX(sv) != 0.0;
79072805 7751 else
463ee0b2 7752 return sv_2bool(sv);
79072805
LW
7753 }
7754 }
7755}
79072805 7756
645c22ef
DM
7757/*
7758=for apidoc sv_iv
7759
7760A private implementation of the C<SvIVx> macro for compilers which can't
7761cope with complex macro expressions. Always use the macro instead.
7762
7763=cut
7764*/
7765
ff68c719 7766IV
864dbfa3 7767Perl_sv_iv(pTHX_ register SV *sv)
85e6fe83 7768{
25da4f38
IZ
7769 if (SvIOK(sv)) {
7770 if (SvIsUV(sv))
7771 return (IV)SvUVX(sv);
ff68c719 7772 return SvIVX(sv);
25da4f38 7773 }
ff68c719 7774 return sv_2iv(sv);
85e6fe83 7775}
85e6fe83 7776
645c22ef
DM
7777/*
7778=for apidoc sv_uv
7779
7780A private implementation of the C<SvUVx> macro for compilers which can't
7781cope with complex macro expressions. Always use the macro instead.
7782
7783=cut
7784*/
7785
ff68c719 7786UV
864dbfa3 7787Perl_sv_uv(pTHX_ register SV *sv)
ff68c719 7788{
25da4f38
IZ
7789 if (SvIOK(sv)) {
7790 if (SvIsUV(sv))
7791 return SvUVX(sv);
7792 return (UV)SvIVX(sv);
7793 }
ff68c719 7794 return sv_2uv(sv);
7795}
85e6fe83 7796
645c22ef
DM
7797/*
7798=for apidoc sv_nv
7799
7800A private implementation of the C<SvNVx> macro for compilers which can't
7801cope with complex macro expressions. Always use the macro instead.
7802
7803=cut
7804*/
7805
65202027 7806NV
864dbfa3 7807Perl_sv_nv(pTHX_ register SV *sv)
79072805 7808{
ff68c719 7809 if (SvNOK(sv))
7810 return SvNVX(sv);
7811 return sv_2nv(sv);
79072805 7812}
79072805 7813
09540bc3
JH
7814/* sv_pv() is now a macro using SvPV_nolen();
7815 * this function provided for binary compatibility only
7816 */
7817
7818char *
7819Perl_sv_pv(pTHX_ SV *sv)
7820{
09540bc3
JH
7821 if (SvPOK(sv))
7822 return SvPVX(sv);
7823
93524f2b 7824 return sv_2pv(sv, 0);
09540bc3
JH
7825}
7826
645c22ef
DM
7827/*
7828=for apidoc sv_pv
7829
baca2b92 7830Use the C<SvPV_nolen> macro instead
645c22ef 7831
645c22ef
DM
7832=for apidoc sv_pvn
7833
7834A private implementation of the C<SvPV> macro for compilers which can't
7835cope with complex macro expressions. Always use the macro instead.
7836
7837=cut
7838*/
7839
1fa8b10d 7840char *
864dbfa3 7841Perl_sv_pvn(pTHX_ SV *sv, STRLEN *lp)
79072805 7842{
85e6fe83
LW
7843 if (SvPOK(sv)) {
7844 *lp = SvCUR(sv);
a0d0e21e 7845 return SvPVX(sv);
85e6fe83 7846 }
463ee0b2 7847 return sv_2pv(sv, lp);
79072805 7848}
79072805 7849
6e9d1081
NC
7850
7851char *
7852Perl_sv_pvn_nomg(pTHX_ register SV *sv, STRLEN *lp)
7853{
7854 if (SvPOK(sv)) {
7855 *lp = SvCUR(sv);
7856 return SvPVX(sv);
7857 }
7858 return sv_2pv_flags(sv, lp, 0);
7859}
7860
09540bc3
JH
7861/* sv_pvn_force() is now a macro using Perl_sv_pvn_force_flags();
7862 * this function provided for binary compatibility only
7863 */
7864
7865char *
7866Perl_sv_pvn_force(pTHX_ SV *sv, STRLEN *lp)
7867{
7868 return sv_pvn_force_flags(sv, lp, SV_GMAGIC);
7869}
7870
c461cf8f
JH
7871/*
7872=for apidoc sv_pvn_force
7873
7874Get a sensible string out of the SV somehow.
645c22ef
DM
7875A private implementation of the C<SvPV_force> macro for compilers which
7876can't cope with complex macro expressions. Always use the macro instead.
c461cf8f 7877
8d6d96c1
HS
7878=for apidoc sv_pvn_force_flags
7879
7880Get a sensible string out of the SV somehow.
7881If C<flags> has C<SV_GMAGIC> bit set, will C<mg_get> on C<sv> if
7882appropriate, else not. C<sv_pvn_force> and C<sv_pvn_force_nomg> are
7883implemented in terms of this function.
645c22ef
DM
7884You normally want to use the various wrapper macros instead: see
7885C<SvPV_force> and C<SvPV_force_nomg>
8d6d96c1
HS
7886
7887=cut
7888*/
7889
7890char *
7891Perl_sv_pvn_force_flags(pTHX_ SV *sv, STRLEN *lp, I32 flags)
7892{
a0d0e21e 7893
6fc92669 7894 if (SvTHINKFIRST(sv) && !SvROK(sv))
765f542d 7895 sv_force_normal_flags(sv, 0);
1c846c1f 7896
a0d0e21e 7897 if (SvPOK(sv)) {
13c5b33c
NC
7898 if (lp)
7899 *lp = SvCUR(sv);
a0d0e21e
LW
7900 }
7901 else {
a3b680e6 7902 char *s;
13c5b33c
NC
7903 STRLEN len;
7904
4d84ee25
NC
7905 if (SvREADONLY(sv) && !(flags & SV_MUTABLE_RETURN)) {
7906 if (PL_op)
7907 Perl_croak(aTHX_ "Can't coerce readonly %s to string in %s",
7908 sv_reftype(sv,0), OP_NAME(PL_op));
7909 else
7910 Perl_croak(aTHX_ "Can't coerce readonly %s to string",
7911 sv_reftype(sv,0));
7912 }
748a9306 7913 if (SvTYPE(sv) > SVt_PVLV && SvTYPE(sv) != SVt_PVFM) {
cea2e8a9 7914 Perl_croak(aTHX_ "Can't coerce %s to string in %s", sv_reftype(sv,0),
53e06cf0 7915 OP_NAME(PL_op));
a0d0e21e 7916 }
4633a7c4 7917 else
13c5b33c
NC
7918 s = sv_2pv_flags(sv, &len, flags);
7919 if (lp)
7920 *lp = len;
7921
3f7c398e 7922 if (s != SvPVX_const(sv)) { /* Almost, but not quite, sv_setpvn() */
a0d0e21e
LW
7923 if (SvROK(sv))
7924 sv_unref(sv);
862a34c6 7925 SvUPGRADE(sv, SVt_PV); /* Never FALSE */
a0d0e21e 7926 SvGROW(sv, len + 1);
3f7c398e 7927 Move(s,SvPVX_const(sv),len,char);
a0d0e21e
LW
7928 SvCUR_set(sv, len);
7929 *SvEND(sv) = '\0';
7930 }
7931 if (!SvPOK(sv)) {
7932 SvPOK_on(sv); /* validate pointer */
7933 SvTAINT(sv);
1d7c1841 7934 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
3f7c398e 7935 PTR2UV(sv),SvPVX_const(sv)));
a0d0e21e
LW
7936 }
7937 }
4d84ee25 7938 return SvPVX_mutable(sv);
a0d0e21e
LW
7939}
7940
09540bc3
JH
7941/* sv_pvbyte () is now a macro using Perl_sv_2pv_flags();
7942 * this function provided for binary compatibility only
7943 */
7944
7945char *
7946Perl_sv_pvbyte(pTHX_ SV *sv)
7947{
7948 sv_utf8_downgrade(sv,0);
7949 return sv_pv(sv);
7950}
7951
645c22ef
DM
7952/*
7953=for apidoc sv_pvbyte
7954
baca2b92 7955Use C<SvPVbyte_nolen> instead.
645c22ef 7956
645c22ef
DM
7957=for apidoc sv_pvbyten
7958
7959A private implementation of the C<SvPVbyte> macro for compilers
7960which can't cope with complex macro expressions. Always use the macro
7961instead.
7962
7963=cut
7964*/
7965
7340a771
GS
7966char *
7967Perl_sv_pvbyten(pTHX_ SV *sv, STRLEN *lp)
7968{
ffebcc3e 7969 sv_utf8_downgrade(sv,0);
7340a771
GS
7970 return sv_pvn(sv,lp);
7971}
7972
645c22ef
DM
7973/*
7974=for apidoc sv_pvbyten_force
7975
7976A private implementation of the C<SvPVbytex_force> macro for compilers
7977which can't cope with complex macro expressions. Always use the macro
7978instead.
7979
7980=cut
7981*/
7982
7340a771
GS
7983char *
7984Perl_sv_pvbyten_force(pTHX_ SV *sv, STRLEN *lp)
7985{
46ec2f14 7986 sv_pvn_force(sv,lp);
ffebcc3e 7987 sv_utf8_downgrade(sv,0);
46ec2f14
TS
7988 *lp = SvCUR(sv);
7989 return SvPVX(sv);
7340a771
GS
7990}
7991
09540bc3
JH
7992/* sv_pvutf8 () is now a macro using Perl_sv_2pv_flags();
7993 * this function provided for binary compatibility only
7994 */
7995
7996char *
7997Perl_sv_pvutf8(pTHX_ SV *sv)
7998{
7999 sv_utf8_upgrade(sv);
8000 return sv_pv(sv);
8001}
8002
645c22ef
DM
8003/*
8004=for apidoc sv_pvutf8
8005
baca2b92 8006Use the C<SvPVutf8_nolen> macro instead
645c22ef 8007
645c22ef
DM
8008=for apidoc sv_pvutf8n
8009
8010A private implementation of the C<SvPVutf8> macro for compilers
8011which can't cope with complex macro expressions. Always use the macro
8012instead.
8013
8014=cut
8015*/
8016
7340a771
GS
8017char *
8018Perl_sv_pvutf8n(pTHX_ SV *sv, STRLEN *lp)
8019{
560a288e 8020 sv_utf8_upgrade(sv);
7340a771
GS
8021 return sv_pvn(sv,lp);
8022}
8023
c461cf8f
JH
8024/*
8025=for apidoc sv_pvutf8n_force
8026
645c22ef
DM
8027A private implementation of the C<SvPVutf8_force> macro for compilers
8028which can't cope with complex macro expressions. Always use the macro
8029instead.
c461cf8f
JH
8030
8031=cut
8032*/
8033
7340a771
GS
8034char *
8035Perl_sv_pvutf8n_force(pTHX_ SV *sv, STRLEN *lp)
8036{
46ec2f14 8037 sv_pvn_force(sv,lp);
560a288e 8038 sv_utf8_upgrade(sv);
46ec2f14
TS
8039 *lp = SvCUR(sv);
8040 return SvPVX(sv);
7340a771
GS
8041}
8042
c461cf8f
JH
8043/*
8044=for apidoc sv_reftype
8045
8046Returns a string describing what the SV is a reference to.
8047
8048=cut
8049*/
8050
1cb0ed9b 8051char *
bfed75c6 8052Perl_sv_reftype(pTHX_ const SV *sv, int ob)
a0d0e21e 8053{
07409e01
NC
8054 /* The fact that I don't need to downcast to char * everywhere, only in ?:
8055 inside return suggests a const propagation bug in g++. */
c86bf373 8056 if (ob && SvOBJECT(sv)) {
1b6737cc 8057 char * const name = HvNAME_get(SvSTASH(sv));
07409e01 8058 return name ? name : (char *) "__ANON__";
c86bf373 8059 }
a0d0e21e
LW
8060 else {
8061 switch (SvTYPE(sv)) {
8062 case SVt_NULL:
8063 case SVt_IV:
8064 case SVt_NV:
8065 case SVt_RV:
8066 case SVt_PV:
8067 case SVt_PVIV:
8068 case SVt_PVNV:
8069 case SVt_PVMG:
8070 case SVt_PVBM:
1cb0ed9b 8071 if (SvVOK(sv))
439cb1c4 8072 return "VSTRING";
a0d0e21e
LW
8073 if (SvROK(sv))
8074 return "REF";
8075 else
8076 return "SCALAR";
1cb0ed9b 8077
07409e01 8078 case SVt_PVLV: return (char *) (SvROK(sv) ? "REF"
be65207d
DM
8079 /* tied lvalues should appear to be
8080 * scalars for backwards compatitbility */
8081 : (LvTYPE(sv) == 't' || LvTYPE(sv) == 'T')
07409e01 8082 ? "SCALAR" : "LVALUE");
a0d0e21e
LW
8083 case SVt_PVAV: return "ARRAY";
8084 case SVt_PVHV: return "HASH";
8085 case SVt_PVCV: return "CODE";
8086 case SVt_PVGV: return "GLOB";
1d2dff63 8087 case SVt_PVFM: return "FORMAT";
27f9d8f3 8088 case SVt_PVIO: return "IO";
a0d0e21e
LW
8089 default: return "UNKNOWN";
8090 }
8091 }
8092}
8093
954c1994
GS
8094/*
8095=for apidoc sv_isobject
8096
8097Returns a boolean indicating whether the SV is an RV pointing to a blessed
8098object. If the SV is not an RV, or if the object is not blessed, then this
8099will return false.
8100
8101=cut
8102*/
8103
463ee0b2 8104int
864dbfa3 8105Perl_sv_isobject(pTHX_ SV *sv)
85e6fe83 8106{
68dc0745 8107 if (!sv)
8108 return 0;
8109 if (SvGMAGICAL(sv))
8110 mg_get(sv);
85e6fe83
LW
8111 if (!SvROK(sv))
8112 return 0;
8113 sv = (SV*)SvRV(sv);
8114 if (!SvOBJECT(sv))
8115 return 0;
8116 return 1;
8117}
8118
954c1994
GS
8119/*
8120=for apidoc sv_isa
8121
8122Returns a boolean indicating whether the SV is blessed into the specified
8123class. This does not check for subtypes; use C<sv_derived_from> to verify
8124an inheritance relationship.
8125
8126=cut
8127*/
8128
85e6fe83 8129int
864dbfa3 8130Perl_sv_isa(pTHX_ SV *sv, const char *name)
463ee0b2 8131{
bfcb3514 8132 const char *hvname;
68dc0745 8133 if (!sv)
8134 return 0;
8135 if (SvGMAGICAL(sv))
8136 mg_get(sv);
ed6116ce 8137 if (!SvROK(sv))
463ee0b2 8138 return 0;
ed6116ce
LW
8139 sv = (SV*)SvRV(sv);
8140 if (!SvOBJECT(sv))
463ee0b2 8141 return 0;
bfcb3514
NC
8142 hvname = HvNAME_get(SvSTASH(sv));
8143 if (!hvname)
e27ad1f2 8144 return 0;
463ee0b2 8145
bfcb3514 8146 return strEQ(hvname, name);
463ee0b2
LW
8147}
8148
954c1994
GS
8149/*
8150=for apidoc newSVrv
8151
8152Creates a new SV for the RV, C<rv>, to point to. If C<rv> is not an RV then
8153it will be upgraded to one. If C<classname> is non-null then the new SV will
8154be blessed in the specified package. The new SV is returned and its
8155reference count is 1.
8156
8157=cut
8158*/
8159
463ee0b2 8160SV*
864dbfa3 8161Perl_newSVrv(pTHX_ SV *rv, const char *classname)
463ee0b2 8162{
463ee0b2
LW
8163 SV *sv;
8164
4561caa4 8165 new_SV(sv);
51cf62d8 8166
765f542d 8167 SV_CHECK_THINKFIRST_COW_DROP(rv);
51cf62d8 8168 SvAMAGIC_off(rv);
51cf62d8 8169
0199fce9 8170 if (SvTYPE(rv) >= SVt_PVMG) {
a3b680e6 8171 const U32 refcnt = SvREFCNT(rv);
0199fce9
JD
8172 SvREFCNT(rv) = 0;
8173 sv_clear(rv);
8174 SvFLAGS(rv) = 0;
8175 SvREFCNT(rv) = refcnt;
8176 }
8177
51cf62d8 8178 if (SvTYPE(rv) < SVt_RV)
0199fce9
JD
8179 sv_upgrade(rv, SVt_RV);
8180 else if (SvTYPE(rv) > SVt_RV) {
8bd4d4c5 8181 SvPV_free(rv);
0199fce9
JD
8182 SvCUR_set(rv, 0);
8183 SvLEN_set(rv, 0);
8184 }
51cf62d8 8185
0c34ef67 8186 SvOK_off(rv);
b162af07 8187 SvRV_set(rv, sv);
ed6116ce 8188 SvROK_on(rv);
463ee0b2 8189
a0d0e21e 8190 if (classname) {
1b6737cc 8191 HV* const stash = gv_stashpv(classname, TRUE);
a0d0e21e
LW
8192 (void)sv_bless(rv, stash);
8193 }
8194 return sv;
8195}
8196
954c1994
GS
8197/*
8198=for apidoc sv_setref_pv
8199
8200Copies a pointer into a new SV, optionally blessing the SV. The C<rv>
8201argument will be upgraded to an RV. That RV will be modified to point to
8202the new SV. If the C<pv> argument is NULL then C<PL_sv_undef> will be placed
8203into the SV. The C<classname> argument indicates the package for the
8204blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
d34c2299 8205will have a reference count of 1, and the RV will be returned.
954c1994
GS
8206
8207Do not use with other Perl types such as HV, AV, SV, CV, because those
8208objects will become corrupted by the pointer copy process.
8209
8210Note that C<sv_setref_pvn> copies the string while this copies the pointer.
8211
8212=cut
8213*/
8214
a0d0e21e 8215SV*
864dbfa3 8216Perl_sv_setref_pv(pTHX_ SV *rv, const char *classname, void *pv)
a0d0e21e 8217{
189b2af5 8218 if (!pv) {
3280af22 8219 sv_setsv(rv, &PL_sv_undef);
189b2af5
GS
8220 SvSETMAGIC(rv);
8221 }
a0d0e21e 8222 else
56431972 8223 sv_setiv(newSVrv(rv,classname), PTR2IV(pv));
a0d0e21e
LW
8224 return rv;
8225}
8226
954c1994
GS
8227/*
8228=for apidoc sv_setref_iv
8229
8230Copies an integer into a new SV, optionally blessing the SV. The C<rv>
8231argument will be upgraded to an RV. That RV will be modified to point to
8232the new SV. The C<classname> argument indicates the package for the
8233blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
d34c2299 8234will have a reference count of 1, and the RV will be returned.
954c1994
GS
8235
8236=cut
8237*/
8238
a0d0e21e 8239SV*
864dbfa3 8240Perl_sv_setref_iv(pTHX_ SV *rv, const char *classname, IV iv)
a0d0e21e
LW
8241{
8242 sv_setiv(newSVrv(rv,classname), iv);
8243 return rv;
8244}
8245
954c1994 8246/*
e1c57cef
JH
8247=for apidoc sv_setref_uv
8248
8249Copies an unsigned integer into a new SV, optionally blessing the SV. The C<rv>
8250argument will be upgraded to an RV. That RV will be modified to point to
8251the new SV. The C<classname> argument indicates the package for the
8252blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
d34c2299 8253will have a reference count of 1, and the RV will be returned.
e1c57cef
JH
8254
8255=cut
8256*/
8257
8258SV*
8259Perl_sv_setref_uv(pTHX_ SV *rv, const char *classname, UV uv)
8260{
8261 sv_setuv(newSVrv(rv,classname), uv);
8262 return rv;
8263}
8264
8265/*
954c1994
GS
8266=for apidoc sv_setref_nv
8267
8268Copies a double into a new SV, optionally blessing the SV. The C<rv>
8269argument will be upgraded to an RV. That RV will be modified to point to
8270the new SV. The C<classname> argument indicates the package for the
8271blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
d34c2299 8272will have a reference count of 1, and the RV will be returned.
954c1994
GS
8273
8274=cut
8275*/
8276
a0d0e21e 8277SV*
65202027 8278Perl_sv_setref_nv(pTHX_ SV *rv, const char *classname, NV nv)
a0d0e21e
LW
8279{
8280 sv_setnv(newSVrv(rv,classname), nv);
8281 return rv;
8282}
463ee0b2 8283
954c1994
GS
8284/*
8285=for apidoc sv_setref_pvn
8286
8287Copies a string into a new SV, optionally blessing the SV. The length of the
8288string must be specified with C<n>. The C<rv> argument will be upgraded to
8289an RV. That RV will be modified to point to the new SV. The C<classname>
8290argument indicates the package for the blessing. Set C<classname> to
7a5fa8a2 8291C<Nullch> to avoid the blessing. The new SV will have a reference count
d34c2299 8292of 1, and the RV will be returned.
954c1994
GS
8293
8294Note that C<sv_setref_pv> copies the pointer while this copies the string.
8295
8296=cut
8297*/
8298
a0d0e21e 8299SV*
1b6737cc 8300Perl_sv_setref_pvn(pTHX_ SV *rv, const char *classname, const char *pv, STRLEN n)
a0d0e21e
LW
8301{
8302 sv_setpvn(newSVrv(rv,classname), pv, n);
463ee0b2
LW
8303 return rv;
8304}
8305
954c1994
GS
8306/*
8307=for apidoc sv_bless
8308
8309Blesses an SV into a specified package. The SV must be an RV. The package
8310must be designated by its stash (see C<gv_stashpv()>). The reference count
8311of the SV is unaffected.
8312
8313=cut
8314*/
8315
a0d0e21e 8316SV*
864dbfa3 8317Perl_sv_bless(pTHX_ SV *sv, HV *stash)
a0d0e21e 8318{
76e3520e 8319 SV *tmpRef;
a0d0e21e 8320 if (!SvROK(sv))
cea2e8a9 8321 Perl_croak(aTHX_ "Can't bless non-reference value");
76e3520e
GS
8322 tmpRef = SvRV(sv);
8323 if (SvFLAGS(tmpRef) & (SVs_OBJECT|SVf_READONLY)) {
8324 if (SvREADONLY(tmpRef))
cea2e8a9 8325 Perl_croak(aTHX_ PL_no_modify);
76e3520e
GS
8326 if (SvOBJECT(tmpRef)) {
8327 if (SvTYPE(tmpRef) != SVt_PVIO)
3280af22 8328 --PL_sv_objcount;
76e3520e 8329 SvREFCNT_dec(SvSTASH(tmpRef));
2e3febc6 8330 }
a0d0e21e 8331 }
76e3520e
GS
8332 SvOBJECT_on(tmpRef);
8333 if (SvTYPE(tmpRef) != SVt_PVIO)
3280af22 8334 ++PL_sv_objcount;
862a34c6 8335 SvUPGRADE(tmpRef, SVt_PVMG);
b162af07 8336 SvSTASH_set(tmpRef, (HV*)SvREFCNT_inc(stash));
a0d0e21e 8337
2e3febc6
CS
8338 if (Gv_AMG(stash))
8339 SvAMAGIC_on(sv);
8340 else
8341 SvAMAGIC_off(sv);
a0d0e21e 8342
1edbfb88
AB
8343 if(SvSMAGICAL(tmpRef))
8344 if(mg_find(tmpRef, PERL_MAGIC_ext) || mg_find(tmpRef, PERL_MAGIC_uvar))
8345 mg_set(tmpRef);
8346
8347
ecdeb87c 8348
a0d0e21e
LW
8349 return sv;
8350}
8351
645c22ef 8352/* Downgrades a PVGV to a PVMG.
645c22ef
DM
8353 */
8354
76e3520e 8355STATIC void
cea2e8a9 8356S_sv_unglob(pTHX_ SV *sv)
a0d0e21e 8357{
850fabdf
GS
8358 void *xpvmg;
8359
a0d0e21e
LW
8360 assert(SvTYPE(sv) == SVt_PVGV);
8361 SvFAKE_off(sv);
8362 if (GvGP(sv))
1edc1566 8363 gp_free((GV*)sv);
e826b3c7 8364 if (GvSTASH(sv)) {
e15faf7d 8365 sv_del_backref((SV*)GvSTASH(sv), sv);
e826b3c7
GS
8366 GvSTASH(sv) = Nullhv;
8367 }
14befaf4 8368 sv_unmagic(sv, PERL_MAGIC_glob);
a0d0e21e 8369 Safefree(GvNAME(sv));
a5f75d66 8370 GvMULTI_off(sv);
850fabdf
GS
8371
8372 /* need to keep SvANY(sv) in the right arena */
8373 xpvmg = new_XPVMG();
8374 StructCopy(SvANY(sv), xpvmg, XPVMG);
8375 del_XPVGV(SvANY(sv));
8376 SvANY(sv) = xpvmg;
8377
a0d0e21e
LW
8378 SvFLAGS(sv) &= ~SVTYPEMASK;
8379 SvFLAGS(sv) |= SVt_PVMG;
8380}
8381
954c1994 8382/*
840a7b70 8383=for apidoc sv_unref_flags
954c1994
GS
8384
8385Unsets the RV status of the SV, and decrements the reference count of
8386whatever was being referenced by the RV. This can almost be thought of
840a7b70
IZ
8387as a reversal of C<newSVrv>. The C<cflags> argument can contain
8388C<SV_IMMEDIATE_UNREF> to force the reference count to be decremented
8389(otherwise the decrementing is conditional on the reference count being
8390different from one or the reference being a readonly SV).
7889fe52 8391See C<SvROK_off>.
954c1994
GS
8392
8393=cut
8394*/
8395
ed6116ce 8396void
e15faf7d 8397Perl_sv_unref_flags(pTHX_ SV *ref, U32 flags)
ed6116ce 8398{
e15faf7d 8399 SV* target = SvRV(ref);
810b8aa5 8400
e15faf7d
NC
8401 if (SvWEAKREF(ref)) {
8402 sv_del_backref(target, ref);
8403 SvWEAKREF_off(ref);
8404 SvRV_set(ref, NULL);
810b8aa5
GS
8405 return;
8406 }
e15faf7d
NC
8407 SvRV_set(ref, NULL);
8408 SvROK_off(ref);
8409 /* You can't have a || SvREADONLY(target) here, as $a = $$a, where $a was
04ca4930 8410 assigned to as BEGIN {$a = \"Foo"} will fail. */
e15faf7d
NC
8411 if (SvREFCNT(target) != 1 || (flags & SV_IMMEDIATE_UNREF))
8412 SvREFCNT_dec(target);
840a7b70 8413 else /* XXX Hack, but hard to make $a=$a->[1] work otherwise */
e15faf7d 8414 sv_2mortal(target); /* Schedule for freeing later */
ed6116ce 8415}
8990e307 8416
840a7b70
IZ
8417/*
8418=for apidoc sv_unref
8419
8420Unsets the RV status of the SV, and decrements the reference count of
8421whatever was being referenced by the RV. This can almost be thought of
8422as a reversal of C<newSVrv>. This is C<sv_unref_flags> with the C<flag>
7889fe52 8423being zero. See C<SvROK_off>.
840a7b70
IZ
8424
8425=cut
8426*/
8427
8428void
8429Perl_sv_unref(pTHX_ SV *sv)
8430{
8431 sv_unref_flags(sv, 0);
8432}
8433
645c22ef
DM
8434/*
8435=for apidoc sv_taint
8436
8437Taint an SV. Use C<SvTAINTED_on> instead.
8438=cut
8439*/
8440
bbce6d69 8441void
864dbfa3 8442Perl_sv_taint(pTHX_ SV *sv)
bbce6d69 8443{
14befaf4 8444 sv_magic((sv), Nullsv, PERL_MAGIC_taint, Nullch, 0);
bbce6d69 8445}
8446
645c22ef
DM
8447/*
8448=for apidoc sv_untaint
8449
8450Untaint an SV. Use C<SvTAINTED_off> instead.
8451=cut
8452*/
8453
bbce6d69 8454void
864dbfa3 8455Perl_sv_untaint(pTHX_ SV *sv)
bbce6d69 8456{
13f57bf8 8457 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
14befaf4 8458 MAGIC *mg = mg_find(sv, PERL_MAGIC_taint);
36477c24 8459 if (mg)
565764a8 8460 mg->mg_len &= ~1;
36477c24 8461 }
bbce6d69 8462}
8463
645c22ef
DM
8464/*
8465=for apidoc sv_tainted
8466
8467Test an SV for taintedness. Use C<SvTAINTED> instead.
8468=cut
8469*/
8470
bbce6d69 8471bool
864dbfa3 8472Perl_sv_tainted(pTHX_ SV *sv)
bbce6d69 8473{
13f57bf8 8474 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
a28509cc 8475 MAGIC * const mg = mg_find(sv, PERL_MAGIC_taint);
2ddb8a4f 8476 if (mg && (mg->mg_len & 1) )
36477c24 8477 return TRUE;
8478 }
8479 return FALSE;
bbce6d69 8480}
8481
09540bc3
JH
8482/*
8483=for apidoc sv_setpviv
8484
8485Copies an integer into the given SV, also updating its string value.
8486Does not handle 'set' magic. See C<sv_setpviv_mg>.
8487
8488=cut
8489*/
8490
8491void
8492Perl_sv_setpviv(pTHX_ SV *sv, IV iv)
8493{
8494 char buf[TYPE_CHARS(UV)];
8495 char *ebuf;
8496 char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf);
8497
8498 sv_setpvn(sv, ptr, ebuf - ptr);
8499}
8500
8501/*
8502=for apidoc sv_setpviv_mg
8503
8504Like C<sv_setpviv>, but also handles 'set' magic.
8505
8506=cut
8507*/
8508
8509void
8510Perl_sv_setpviv_mg(pTHX_ SV *sv, IV iv)
8511{
8512 char buf[TYPE_CHARS(UV)];
8513 char *ebuf;
8514 char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf);
8515
8516 sv_setpvn(sv, ptr, ebuf - ptr);
8517 SvSETMAGIC(sv);
8518}
8519
cea2e8a9 8520#if defined(PERL_IMPLICIT_CONTEXT)
645c22ef
DM
8521
8522/* pTHX_ magic can't cope with varargs, so this is a no-context
8523 * version of the main function, (which may itself be aliased to us).
8524 * Don't access this version directly.
8525 */
8526
cea2e8a9
GS
8527void
8528Perl_sv_setpvf_nocontext(SV *sv, const char* pat, ...)
8529{
8530 dTHX;
8531 va_list args;
8532 va_start(args, pat);
c5be433b 8533 sv_vsetpvf(sv, pat, &args);
cea2e8a9
GS
8534 va_end(args);
8535}
8536
645c22ef
DM
8537/* pTHX_ magic can't cope with varargs, so this is a no-context
8538 * version of the main function, (which may itself be aliased to us).
8539 * Don't access this version directly.
8540 */
cea2e8a9
GS
8541
8542void
8543Perl_sv_setpvf_mg_nocontext(SV *sv, const char* pat, ...)
8544{
8545 dTHX;
8546 va_list args;
8547 va_start(args, pat);
c5be433b 8548 sv_vsetpvf_mg(sv, pat, &args);
cea2e8a9 8549 va_end(args);
cea2e8a9
GS
8550}
8551#endif
8552
954c1994
GS
8553/*
8554=for apidoc sv_setpvf
8555
bffc3d17
SH
8556Works like C<sv_catpvf> but copies the text into the SV instead of
8557appending it. Does not handle 'set' magic. See C<sv_setpvf_mg>.
954c1994
GS
8558
8559=cut
8560*/
8561
46fc3d4c 8562void
864dbfa3 8563Perl_sv_setpvf(pTHX_ SV *sv, const char* pat, ...)
46fc3d4c 8564{
8565 va_list args;
46fc3d4c 8566 va_start(args, pat);
c5be433b 8567 sv_vsetpvf(sv, pat, &args);
46fc3d4c 8568 va_end(args);
8569}
8570
bffc3d17
SH
8571/*
8572=for apidoc sv_vsetpvf
8573
8574Works like C<sv_vcatpvf> but copies the text into the SV instead of
8575appending it. Does not handle 'set' magic. See C<sv_vsetpvf_mg>.
8576
8577Usually used via its frontend C<sv_setpvf>.
8578
8579=cut
8580*/
645c22ef 8581
c5be433b
GS
8582void
8583Perl_sv_vsetpvf(pTHX_ SV *sv, const char* pat, va_list* args)
8584{
8585 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
8586}
ef50df4b 8587
954c1994
GS
8588/*
8589=for apidoc sv_setpvf_mg
8590
8591Like C<sv_setpvf>, but also handles 'set' magic.
8592
8593=cut
8594*/
8595
ef50df4b 8596void
864dbfa3 8597Perl_sv_setpvf_mg(pTHX_ SV *sv, const char* pat, ...)
ef50df4b
GS
8598{
8599 va_list args;
ef50df4b 8600 va_start(args, pat);
c5be433b 8601 sv_vsetpvf_mg(sv, pat, &args);
ef50df4b 8602 va_end(args);
c5be433b
GS
8603}
8604
bffc3d17
SH
8605/*
8606=for apidoc sv_vsetpvf_mg
8607
8608Like C<sv_vsetpvf>, but also handles 'set' magic.
8609
8610Usually used via its frontend C<sv_setpvf_mg>.
8611
8612=cut
8613*/
645c22ef 8614
c5be433b
GS
8615void
8616Perl_sv_vsetpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args)
8617{
8618 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
ef50df4b
GS
8619 SvSETMAGIC(sv);
8620}
8621
cea2e8a9 8622#if defined(PERL_IMPLICIT_CONTEXT)
645c22ef
DM
8623
8624/* pTHX_ magic can't cope with varargs, so this is a no-context
8625 * version of the main function, (which may itself be aliased to us).
8626 * Don't access this version directly.
8627 */
8628
cea2e8a9
GS
8629void
8630Perl_sv_catpvf_nocontext(SV *sv, const char* pat, ...)
8631{
8632 dTHX;
8633 va_list args;
8634 va_start(args, pat);
c5be433b 8635 sv_vcatpvf(sv, pat, &args);
cea2e8a9
GS
8636 va_end(args);
8637}
8638
645c22ef
DM
8639/* pTHX_ magic can't cope with varargs, so this is a no-context
8640 * version of the main function, (which may itself be aliased to us).
8641 * Don't access this version directly.
8642 */
8643
cea2e8a9
GS
8644void
8645Perl_sv_catpvf_mg_nocontext(SV *sv, const char* pat, ...)
8646{
8647 dTHX;
8648 va_list args;
8649 va_start(args, pat);
c5be433b 8650 sv_vcatpvf_mg(sv, pat, &args);
cea2e8a9 8651 va_end(args);
cea2e8a9
GS
8652}
8653#endif
8654
954c1994
GS
8655/*
8656=for apidoc sv_catpvf
8657
d5ce4a7c
GA
8658Processes its arguments like C<sprintf> and appends the formatted
8659output to an SV. If the appended data contains "wide" characters
8660(including, but not limited to, SVs with a UTF-8 PV formatted with %s,
8661and characters >255 formatted with %c), the original SV might get
bffc3d17 8662upgraded to UTF-8. Handles 'get' magic, but not 'set' magic. See
cdd94ca7
NC
8663C<sv_catpvf_mg>. If the original SV was UTF-8, the pattern should be
8664valid UTF-8; if the original SV was bytes, the pattern should be too.
954c1994 8665
d5ce4a7c 8666=cut */
954c1994 8667
46fc3d4c 8668void
864dbfa3 8669Perl_sv_catpvf(pTHX_ SV *sv, const char* pat, ...)
46fc3d4c 8670{
8671 va_list args;
46fc3d4c 8672 va_start(args, pat);
c5be433b 8673 sv_vcatpvf(sv, pat, &args);
46fc3d4c 8674 va_end(args);
8675}
8676
bffc3d17
SH
8677/*
8678=for apidoc sv_vcatpvf
8679
8680Processes its arguments like C<vsprintf> and appends the formatted output
8681to an SV. Does not handle 'set' magic. See C<sv_vcatpvf_mg>.
8682
8683Usually used via its frontend C<sv_catpvf>.
8684
8685=cut
8686*/
645c22ef 8687
ef50df4b 8688void
c5be433b
GS
8689Perl_sv_vcatpvf(pTHX_ SV *sv, const char* pat, va_list* args)
8690{
8691 sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
8692}
8693
954c1994
GS
8694/*
8695=for apidoc sv_catpvf_mg
8696
8697Like C<sv_catpvf>, but also handles 'set' magic.
8698
8699=cut
8700*/
8701
c5be433b 8702void
864dbfa3 8703Perl_sv_catpvf_mg(pTHX_ SV *sv, const char* pat, ...)
ef50df4b
GS
8704{
8705 va_list args;
ef50df4b 8706 va_start(args, pat);
c5be433b 8707 sv_vcatpvf_mg(sv, pat, &args);
ef50df4b 8708 va_end(args);
c5be433b
GS
8709}
8710
bffc3d17
SH
8711/*
8712=for apidoc sv_vcatpvf_mg
8713
8714Like C<sv_vcatpvf>, but also handles 'set' magic.
8715
8716Usually used via its frontend C<sv_catpvf_mg>.
8717
8718=cut
8719*/
645c22ef 8720
c5be433b
GS
8721void
8722Perl_sv_vcatpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args)
8723{
8724 sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
ef50df4b
GS
8725 SvSETMAGIC(sv);
8726}
8727
954c1994
GS
8728/*
8729=for apidoc sv_vsetpvfn
8730
bffc3d17 8731Works like C<sv_vcatpvfn> but copies the text into the SV instead of
954c1994
GS
8732appending it.
8733
bffc3d17 8734Usually used via one of its frontends C<sv_vsetpvf> and C<sv_vsetpvf_mg>.
645c22ef 8735
954c1994
GS
8736=cut
8737*/
8738
46fc3d4c 8739void
7d5ea4e7 8740Perl_sv_vsetpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted)
46fc3d4c 8741{
8742 sv_setpvn(sv, "", 0);
7d5ea4e7 8743 sv_vcatpvfn(sv, pat, patlen, args, svargs, svmax, maybe_tainted);
46fc3d4c 8744}
8745
645c22ef
DM
8746/* private function for use in sv_vcatpvfn via the EXPECT_NUMBER macro */
8747
2d00ba3b 8748STATIC I32
9dd79c3f 8749S_expect_number(pTHX_ char** pattern)
211dfcf1
HS
8750{
8751 I32 var = 0;
8752 switch (**pattern) {
8753 case '1': case '2': case '3':
8754 case '4': case '5': case '6':
8755 case '7': case '8': case '9':
8756 while (isDIGIT(**pattern))
8757 var = var * 10 + (*(*pattern)++ - '0');
8758 }
8759 return var;
8760}
9dd79c3f 8761#define EXPECT_NUMBER(pattern, var) (var = S_expect_number(aTHX_ &pattern))
211dfcf1 8762
4151a5fe
IZ
8763static char *
8764F0convert(NV nv, char *endbuf, STRLEN *len)
8765{
a3b680e6 8766 const int neg = nv < 0;
4151a5fe 8767 UV uv;
4151a5fe
IZ
8768
8769 if (neg)
8770 nv = -nv;
8771 if (nv < UV_MAX) {
b464bac0 8772 char *p = endbuf;
4151a5fe 8773 nv += 0.5;
028f8eaa 8774 uv = (UV)nv;
4151a5fe
IZ
8775 if (uv & 1 && uv == nv)
8776 uv--; /* Round to even */
8777 do {
a3b680e6 8778 const unsigned dig = uv % 10;
4151a5fe
IZ
8779 *--p = '0' + dig;
8780 } while (uv /= 10);
8781 if (neg)
8782 *--p = '-';
8783 *len = endbuf - p;
8784 return p;
8785 }
8786 return Nullch;
8787}
8788
8789
954c1994
GS
8790/*
8791=for apidoc sv_vcatpvfn
8792
8793Processes its arguments like C<vsprintf> and appends the formatted output
8794to an SV. Uses an array of SVs if the C style variable argument list is
8795missing (NULL). When running with taint checks enabled, indicates via
8796C<maybe_tainted> if results are untrustworthy (often due to the use of
8797locales).
8798
bffc3d17 8799Usually used via one of its frontends C<sv_vcatpvf> and C<sv_vcatpvf_mg>.
645c22ef 8800
954c1994
GS
8801=cut
8802*/
8803
1ef29b0e
RGS
8804/* XXX maybe_tainted is never assigned to, so the doc above is lying. */
8805
46fc3d4c 8806void
7d5ea4e7 8807Perl_sv_vcatpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted)
46fc3d4c 8808{
8809 char *p;
8810 char *q;
a3b680e6 8811 const char *patend;
fc36a67e 8812 STRLEN origlen;
46fc3d4c 8813 I32 svix = 0;
27da23d5 8814 static const char nullstr[] = "(null)";
9c5ffd7c 8815 SV *argsv = Nullsv;
b464bac0
AL
8816 bool has_utf8 = DO_UTF8(sv); /* has the result utf8? */
8817 const bool pat_utf8 = has_utf8; /* the pattern is in utf8? */
db79b45b 8818 SV *nsv = Nullsv;
4151a5fe
IZ
8819 /* Times 4: a decimal digit takes more than 3 binary digits.
8820 * NV_DIG: mantissa takes than many decimal digits.
8821 * Plus 32: Playing safe. */
8822 char ebuf[IV_DIG * 4 + NV_DIG + 32];
8823 /* large enough for "%#.#f" --chip */
8824 /* what about long double NVs? --jhi */
db79b45b 8825
53c1dcc0
AL
8826 PERL_UNUSED_ARG(maybe_tainted);
8827
46fc3d4c 8828 /* no matter what, this is a string now */
fc36a67e 8829 (void)SvPV_force(sv, origlen);
46fc3d4c 8830
0dbb1585 8831 /* special-case "", "%s", and "%-p" (SVf) */
46fc3d4c 8832 if (patlen == 0)
8833 return;
0dbb1585 8834 if (patlen == 2 && pat[0] == '%' && pat[1] == 's') {
c635e13b 8835 if (args) {
53c1dcc0 8836 const char * const s = va_arg(*args, char*);
c635e13b 8837 sv_catpv(sv, s ? s : nullstr);
8838 }
7e2040f0 8839 else if (svix < svmax) {
fc36a67e 8840 sv_catsv(sv, *svargs);
7e2040f0
GS
8841 if (DO_UTF8(*svargs))
8842 SvUTF8_on(sv);
8843 }
fc36a67e 8844 return;
0dbb1585
AL
8845 }
8846 if (patlen == 3 && pat[0] == '%' &&
8847 pat[1] == '-' && pat[2] == 'p') {
fc36a67e 8848 if (args) {
7e2040f0
GS
8849 argsv = va_arg(*args, SV*);
8850 sv_catsv(sv, argsv);
8851 if (DO_UTF8(argsv))
8852 SvUTF8_on(sv);
fc36a67e 8853 return;
8854 }
46fc3d4c 8855 }
8856
1d917b39 8857#ifndef USE_LONG_DOUBLE
4151a5fe 8858 /* special-case "%.<number>[gf]" */
7af36d83 8859 if ( !args && patlen <= 5 && pat[0] == '%' && pat[1] == '.'
4151a5fe
IZ
8860 && (pat[patlen-1] == 'g' || pat[patlen-1] == 'f') ) {
8861 unsigned digits = 0;
8862 const char *pp;
8863
8864 pp = pat + 2;
8865 while (*pp >= '0' && *pp <= '9')
8866 digits = 10 * digits + (*pp++ - '0');
028f8eaa 8867 if (pp - pat == (int)patlen - 1) {
4151a5fe
IZ
8868 NV nv;
8869
7af36d83 8870 if (svix < svmax)
4151a5fe
IZ
8871 nv = SvNV(*svargs);
8872 else
8873 return;
8874 if (*pp == 'g') {
2873255c
NC
8875 /* Add check for digits != 0 because it seems that some
8876 gconverts are buggy in this case, and we don't yet have
8877 a Configure test for this. */
8878 if (digits && digits < sizeof(ebuf) - NV_DIG - 10) {
8879 /* 0, point, slack */
2e59c212 8880 Gconvert(nv, (int)digits, 0, ebuf);
4151a5fe
IZ
8881 sv_catpv(sv, ebuf);
8882 if (*ebuf) /* May return an empty string for digits==0 */
8883 return;
8884 }
8885 } else if (!digits) {
8886 STRLEN l;
8887
8888 if ((p = F0convert(nv, ebuf + sizeof ebuf, &l))) {
8889 sv_catpvn(sv, p, l);
8890 return;
8891 }
8892 }
8893 }
8894 }
1d917b39 8895#endif /* !USE_LONG_DOUBLE */
4151a5fe 8896
2cf2cfc6 8897 if (!args && svix < svmax && DO_UTF8(*svargs))
205f51d8 8898 has_utf8 = TRUE;
2cf2cfc6 8899
46fc3d4c 8900 patend = (char*)pat + patlen;
8901 for (p = (char*)pat; p < patend; p = q) {
8902 bool alt = FALSE;
8903 bool left = FALSE;
b22c7a20 8904 bool vectorize = FALSE;
211dfcf1 8905 bool vectorarg = FALSE;
2cf2cfc6 8906 bool vec_utf8 = FALSE;
46fc3d4c 8907 char fill = ' ';
8908 char plus = 0;
8909 char intsize = 0;
8910 STRLEN width = 0;
fc36a67e 8911 STRLEN zeros = 0;
46fc3d4c 8912 bool has_precis = FALSE;
8913 STRLEN precis = 0;
58e33a90 8914 I32 osvix = svix;
2cf2cfc6 8915 bool is_utf8 = FALSE; /* is this item utf8? */
20f6aaab
AS
8916#ifdef HAS_LDBL_SPRINTF_BUG
8917 /* This is to try to fix a bug with irix/nonstop-ux/powerux and
205f51d8 8918 with sfio - Allen <allens@cpan.org> */
20f6aaab
AS
8919 bool fix_ldbl_sprintf_bug = FALSE;
8920#endif
205f51d8 8921
46fc3d4c 8922 char esignbuf[4];
89ebb4a3 8923 U8 utf8buf[UTF8_MAXBYTES+1];
46fc3d4c 8924 STRLEN esignlen = 0;
8925
4d84ee25 8926 const char *eptr = Nullch;
fc36a67e 8927 STRLEN elen = 0;
81f715da 8928 SV *vecsv = Nullsv;
245d4a47 8929 const U8 *vecstr = Null(U8*);
b22c7a20 8930 STRLEN veclen = 0;
934abaf1 8931 char c = 0;
46fc3d4c 8932 int i;
9c5ffd7c 8933 unsigned base = 0;
8c8eb53c
RB
8934 IV iv = 0;
8935 UV uv = 0;
9e5b023a
JH
8936 /* we need a long double target in case HAS_LONG_DOUBLE but
8937 not USE_LONG_DOUBLE
8938 */
35fff930 8939#if defined(HAS_LONG_DOUBLE) && LONG_DOUBLESIZE > DOUBLESIZE
9e5b023a
JH
8940 long double nv;
8941#else
65202027 8942 NV nv;
9e5b023a 8943#endif
46fc3d4c 8944 STRLEN have;
8945 STRLEN need;
8946 STRLEN gap;
7af36d83 8947 const char *dotstr = ".";
b22c7a20 8948 STRLEN dotstrlen = 1;
211dfcf1 8949 I32 efix = 0; /* explicit format parameter index */
eb3fce90 8950 I32 ewix = 0; /* explicit width index */
211dfcf1
HS
8951 I32 epix = 0; /* explicit precision index */
8952 I32 evix = 0; /* explicit vector index */
eb3fce90 8953 bool asterisk = FALSE;
46fc3d4c 8954
211dfcf1 8955 /* echo everything up to the next format specification */
46fc3d4c 8956 for (q = p; q < patend && *q != '%'; ++q) ;
8957 if (q > p) {
db79b45b
JH
8958 if (has_utf8 && !pat_utf8)
8959 sv_catpvn_utf8_upgrade(sv, p, q - p, nsv);
8960 else
8961 sv_catpvn(sv, p, q - p);
46fc3d4c 8962 p = q;
8963 }
8964 if (q++ >= patend)
8965 break;
8966
211dfcf1
HS
8967/*
8968 We allow format specification elements in this order:
8969 \d+\$ explicit format parameter index
8970 [-+ 0#]+ flags
a472f209 8971 v|\*(\d+\$)?v vector with optional (optionally specified) arg
f3583277 8972 0 flag (as above): repeated to allow "v02"
211dfcf1
HS
8973 \d+|\*(\d+\$)? width using optional (optionally specified) arg
8974 \.(\d*|\*(\d+\$)?) precision using optional (optionally specified) arg
8975 [hlqLV] size
8976 [%bcdefginopsux_DFOUX] format (mandatory)
8977*/
8978 if (EXPECT_NUMBER(q, width)) {
8979 if (*q == '$') {
8980 ++q;
8981 efix = width;
8982 } else {
8983 goto gotwidth;
8984 }
8985 }
8986
fc36a67e 8987 /* FLAGS */
8988
46fc3d4c 8989 while (*q) {
8990 switch (*q) {
8991 case ' ':
8992 case '+':
8993 plus = *q++;
8994 continue;
8995
8996 case '-':
8997 left = TRUE;
8998 q++;
8999 continue;
9000
9001 case '0':
9002 fill = *q++;
9003 continue;
9004
9005 case '#':
9006 alt = TRUE;
9007 q++;
9008 continue;
9009
fc36a67e 9010 default:
9011 break;
9012 }
9013 break;
9014 }
46fc3d4c 9015
211dfcf1 9016 tryasterisk:
eb3fce90 9017 if (*q == '*') {
211dfcf1
HS
9018 q++;
9019 if (EXPECT_NUMBER(q, ewix))
9020 if (*q++ != '$')
9021 goto unknown;
eb3fce90 9022 asterisk = TRUE;
211dfcf1
HS
9023 }
9024 if (*q == 'v') {
eb3fce90 9025 q++;
211dfcf1
HS
9026 if (vectorize)
9027 goto unknown;
9cbac4c7 9028 if ((vectorarg = asterisk)) {
211dfcf1
HS
9029 evix = ewix;
9030 ewix = 0;
9031 asterisk = FALSE;
9032 }
9033 vectorize = TRUE;
9034 goto tryasterisk;
eb3fce90
JH
9035 }
9036
211dfcf1 9037 if (!asterisk)
7a5fa8a2 9038 if( *q == '0' )
f3583277 9039 fill = *q++;
211dfcf1
HS
9040 EXPECT_NUMBER(q, width);
9041
9042 if (vectorize) {
9043 if (vectorarg) {
9044 if (args)
9045 vecsv = va_arg(*args, SV*);
9046 else
9047 vecsv = (evix ? evix <= svmax : svix < svmax) ?
3a7a539e 9048 svargs[evix ? evix-1 : svix++] : &PL_sv_undef;
245d4a47 9049 dotstr = SvPV_const(vecsv, dotstrlen);
211dfcf1 9050 if (DO_UTF8(vecsv))
2cf2cfc6 9051 is_utf8 = TRUE;
211dfcf1
HS
9052 }
9053 if (args) {
9054 vecsv = va_arg(*args, SV*);
245d4a47 9055 vecstr = (U8*)SvPV_const(vecsv,veclen);
2cf2cfc6 9056 vec_utf8 = DO_UTF8(vecsv);
eb3fce90 9057 }
211dfcf1
HS
9058 else if (efix ? efix <= svmax : svix < svmax) {
9059 vecsv = svargs[efix ? efix-1 : svix++];
245d4a47 9060 vecstr = (U8*)SvPV_const(vecsv,veclen);
2cf2cfc6 9061 vec_utf8 = DO_UTF8(vecsv);
d7aa5382 9062 /* if this is a version object, we need to return the
3f7c398e 9063 * stringified representation (which the SvPVX_const has
d7aa5382
JP
9064 * already done for us), but not vectorize the args
9065 */
9066 if ( *q == 'd' && sv_derived_from(vecsv,"version") )
9067 {
9068 q++; /* skip past the rest of the %vd format */
245d4a47 9069 eptr = (const char *) vecstr;
d7aa5382
JP
9070 elen = strlen(eptr);
9071 vectorize=FALSE;
9072 goto string;
9073 }
211dfcf1
HS
9074 }
9075 else {
9076 vecstr = (U8*)"";
9077 veclen = 0;
9078 }
eb3fce90 9079 }
fc36a67e 9080
eb3fce90 9081 if (asterisk) {
fc36a67e 9082 if (args)
9083 i = va_arg(*args, int);
9084 else
eb3fce90
JH
9085 i = (ewix ? ewix <= svmax : svix < svmax) ?
9086 SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0;
fc36a67e 9087 left |= (i < 0);
9088 width = (i < 0) ? -i : i;
fc36a67e 9089 }
211dfcf1 9090 gotwidth:
fc36a67e 9091
9092 /* PRECISION */
46fc3d4c 9093
fc36a67e 9094 if (*q == '.') {
9095 q++;
9096 if (*q == '*') {
211dfcf1 9097 q++;
7b8dd722
HS
9098 if (EXPECT_NUMBER(q, epix) && *q++ != '$')
9099 goto unknown;
9100 /* XXX: todo, support specified precision parameter */
9101 if (epix)
211dfcf1 9102 goto unknown;
46fc3d4c 9103 if (args)
9104 i = va_arg(*args, int);
9105 else
eb3fce90
JH
9106 i = (ewix ? ewix <= svmax : svix < svmax)
9107 ? SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0;
fc36a67e 9108 precis = (i < 0) ? 0 : i;
fc36a67e 9109 }
9110 else {
9111 precis = 0;
9112 while (isDIGIT(*q))
9113 precis = precis * 10 + (*q++ - '0');
9114 }
9115 has_precis = TRUE;
9116 }
46fc3d4c 9117
fc36a67e 9118 /* SIZE */
46fc3d4c 9119
fc36a67e 9120 switch (*q) {
c623ac67
GS
9121#ifdef WIN32
9122 case 'I': /* Ix, I32x, and I64x */
9123# ifdef WIN64
9124 if (q[1] == '6' && q[2] == '4') {
9125 q += 3;
9126 intsize = 'q';
9127 break;
9128 }
9129# endif
9130 if (q[1] == '3' && q[2] == '2') {
9131 q += 3;
9132 break;
9133 }
9134# ifdef WIN64
9135 intsize = 'q';
9136# endif
9137 q++;
9138 break;
9139#endif
9e5b023a 9140#if defined(HAS_QUAD) || defined(HAS_LONG_DOUBLE)
6f9bb7fd 9141 case 'L': /* Ld */
e5c81feb 9142 /* FALL THROUGH */
e5c81feb 9143#ifdef HAS_QUAD
6f9bb7fd 9144 case 'q': /* qd */
9e5b023a 9145#endif
6f9bb7fd
GS
9146 intsize = 'q';
9147 q++;
9148 break;
9149#endif
fc36a67e 9150 case 'l':
9e5b023a 9151#if defined(HAS_QUAD) || defined(HAS_LONG_DOUBLE)
205f51d8 9152 if (*(q + 1) == 'l') { /* lld, llf */
fc36a67e 9153 intsize = 'q';
9154 q += 2;
46fc3d4c 9155 break;
cf2093f6 9156 }
fc36a67e 9157#endif
6f9bb7fd 9158 /* FALL THROUGH */
fc36a67e 9159 case 'h':
cf2093f6 9160 /* FALL THROUGH */
fc36a67e 9161 case 'V':
9162 intsize = *q++;
46fc3d4c 9163 break;
9164 }
9165
fc36a67e 9166 /* CONVERSION */
9167
211dfcf1
HS
9168 if (*q == '%') {
9169 eptr = q++;
9170 elen = 1;
9171 goto string;
9172 }
9173
be75b157
HS
9174 if (vectorize)
9175 argsv = vecsv;
9176 else if (!args)
211dfcf1
HS
9177 argsv = (efix ? efix <= svmax : svix < svmax) ?
9178 svargs[efix ? efix-1 : svix++] : &PL_sv_undef;
9179
46fc3d4c 9180 switch (c = *q++) {
9181
9182 /* STRINGS */
9183
46fc3d4c 9184 case 'c':
be75b157 9185 uv = (args && !vectorize) ? va_arg(*args, int) : SvIVx(argsv);
1bd104fb
JH
9186 if ((uv > 255 ||
9187 (!UNI_IS_INVARIANT(uv) && SvUTF8(sv)))
0064a8a9 9188 && !IN_BYTES) {
dfe13c55 9189 eptr = (char*)utf8buf;
9041c2e3 9190 elen = uvchr_to_utf8((U8*)eptr, uv) - utf8buf;
2cf2cfc6 9191 is_utf8 = TRUE;
7e2040f0
GS
9192 }
9193 else {
9194 c = (char)uv;
9195 eptr = &c;
9196 elen = 1;
a0ed51b3 9197 }
46fc3d4c 9198 goto string;
9199
46fc3d4c 9200 case 's':
be75b157 9201 if (args && !vectorize) {
fc36a67e 9202 eptr = va_arg(*args, char*);
c635e13b 9203 if (eptr)
1d7c1841
GS
9204#ifdef MACOS_TRADITIONAL
9205 /* On MacOS, %#s format is used for Pascal strings */
9206 if (alt)
9207 elen = *eptr++;
9208 else
9209#endif
c635e13b 9210 elen = strlen(eptr);
9211 else {
27da23d5 9212 eptr = (char *)nullstr;
c635e13b 9213 elen = sizeof nullstr - 1;
9214 }
46fc3d4c 9215 }
211dfcf1 9216 else {
4d84ee25 9217 eptr = SvPVx_const(argsv, elen);
7e2040f0 9218 if (DO_UTF8(argsv)) {
a0ed51b3
LW
9219 if (has_precis && precis < elen) {
9220 I32 p = precis;
7e2040f0 9221 sv_pos_u2b(argsv, &p, 0); /* sticks at end */
a0ed51b3
LW
9222 precis = p;
9223 }
9224 if (width) { /* fudge width (can't fudge elen) */
7e2040f0 9225 width += elen - sv_len_utf8(argsv);
a0ed51b3 9226 }
2cf2cfc6 9227 is_utf8 = TRUE;
a0ed51b3
LW
9228 }
9229 }
fc36a67e 9230
46fc3d4c 9231 string:
b22c7a20 9232 vectorize = FALSE;
46fc3d4c 9233 if (has_precis && elen > precis)
9234 elen = precis;
9235 break;
9236
9237 /* INTEGERS */
9238
fc36a67e 9239 case 'p':
0dbb1585 9240 if (left && args) { /* SVf */
5df617be 9241 left = FALSE;
0dbb1585
AL
9242 if (width) {
9243 precis = width;
9244 has_precis = TRUE;
9245 width = 0;
9246 }
9247 if (vectorize)
9248 goto unknown;
9249 argsv = va_arg(*args, SV*);
4d84ee25 9250 eptr = SvPVx_const(argsv, elen);
0dbb1585
AL
9251 if (DO_UTF8(argsv))
9252 is_utf8 = TRUE;
9253 goto string;
5df617be 9254 }
be75b157 9255 if (alt || vectorize)
c2e66d9e 9256 goto unknown;
211dfcf1 9257 uv = PTR2UV(args ? va_arg(*args, void*) : argsv);
fc36a67e 9258 base = 16;
9259 goto integer;
9260
46fc3d4c 9261 case 'D':
29fe7a80 9262#ifdef IV_IS_QUAD
22f3ae8c 9263 intsize = 'q';
29fe7a80 9264#else
46fc3d4c 9265 intsize = 'l';
29fe7a80 9266#endif
46fc3d4c 9267 /* FALL THROUGH */
9268 case 'd':
9269 case 'i':
b22c7a20 9270 if (vectorize) {
ba210ebe 9271 STRLEN ulen;
211dfcf1
HS
9272 if (!veclen)
9273 continue;
2cf2cfc6
A
9274 if (vec_utf8)
9275 uv = utf8n_to_uvchr(vecstr, veclen, &ulen,
9276 UTF8_ALLOW_ANYUV);
b22c7a20 9277 else {
e83d50c9 9278 uv = *vecstr;
b22c7a20
GS
9279 ulen = 1;
9280 }
9281 vecstr += ulen;
9282 veclen -= ulen;
e83d50c9
JP
9283 if (plus)
9284 esignbuf[esignlen++] = plus;
b22c7a20
GS
9285 }
9286 else if (args) {
46fc3d4c 9287 switch (intsize) {
9288 case 'h': iv = (short)va_arg(*args, int); break;
46fc3d4c 9289 case 'l': iv = va_arg(*args, long); break;
fc36a67e 9290 case 'V': iv = va_arg(*args, IV); break;
b10c0dba 9291 default: iv = va_arg(*args, int); break;
cf2093f6
JH
9292#ifdef HAS_QUAD
9293 case 'q': iv = va_arg(*args, Quad_t); break;
9294#endif
46fc3d4c 9295 }
9296 }
9297 else {
b10c0dba 9298 IV tiv = SvIVx(argsv); /* work around GCC bug #13488 */
46fc3d4c 9299 switch (intsize) {
b10c0dba
MHM
9300 case 'h': iv = (short)tiv; break;
9301 case 'l': iv = (long)tiv; break;
9302 case 'V':
9303 default: iv = tiv; break;
cf2093f6 9304#ifdef HAS_QUAD
b10c0dba 9305 case 'q': iv = (Quad_t)tiv; break;
cf2093f6 9306#endif
46fc3d4c 9307 }
9308 }
e83d50c9
JP
9309 if ( !vectorize ) /* we already set uv above */
9310 {
9311 if (iv >= 0) {
9312 uv = iv;
9313 if (plus)
9314 esignbuf[esignlen++] = plus;
9315 }
9316 else {
9317 uv = -iv;
9318 esignbuf[esignlen++] = '-';
9319 }
46fc3d4c 9320 }
9321 base = 10;
9322 goto integer;
9323
fc36a67e 9324 case 'U':
29fe7a80 9325#ifdef IV_IS_QUAD
22f3ae8c 9326 intsize = 'q';
29fe7a80 9327#else
fc36a67e 9328 intsize = 'l';
29fe7a80 9329#endif
fc36a67e 9330 /* FALL THROUGH */
9331 case 'u':
9332 base = 10;
9333 goto uns_integer;
9334
4f19785b
WSI
9335 case 'b':
9336 base = 2;
9337 goto uns_integer;
9338
46fc3d4c 9339 case 'O':
29fe7a80 9340#ifdef IV_IS_QUAD
22f3ae8c 9341 intsize = 'q';
29fe7a80 9342#else
46fc3d4c 9343 intsize = 'l';
29fe7a80 9344#endif
46fc3d4c 9345 /* FALL THROUGH */
9346 case 'o':
9347 base = 8;
9348 goto uns_integer;
9349
9350 case 'X':
46fc3d4c 9351 case 'x':
9352 base = 16;
46fc3d4c 9353
9354 uns_integer:
b22c7a20 9355 if (vectorize) {
ba210ebe 9356 STRLEN ulen;
b22c7a20 9357 vector:
211dfcf1
HS
9358 if (!veclen)
9359 continue;
2cf2cfc6
A
9360 if (vec_utf8)
9361 uv = utf8n_to_uvchr(vecstr, veclen, &ulen,
9362 UTF8_ALLOW_ANYUV);
b22c7a20 9363 else {
a05b299f 9364 uv = *vecstr;
b22c7a20
GS
9365 ulen = 1;
9366 }
9367 vecstr += ulen;
9368 veclen -= ulen;
9369 }
9370 else if (args) {
46fc3d4c 9371 switch (intsize) {
9372 case 'h': uv = (unsigned short)va_arg(*args, unsigned); break;
46fc3d4c 9373 case 'l': uv = va_arg(*args, unsigned long); break;
fc36a67e 9374 case 'V': uv = va_arg(*args, UV); break;
b10c0dba 9375 default: uv = va_arg(*args, unsigned); break;
cf2093f6 9376#ifdef HAS_QUAD
9e3321a5 9377 case 'q': uv = va_arg(*args, Uquad_t); break;
cf2093f6 9378#endif
46fc3d4c 9379 }
9380 }
9381 else {
b10c0dba 9382 UV tuv = SvUVx(argsv); /* work around GCC bug #13488 */
46fc3d4c 9383 switch (intsize) {
b10c0dba
MHM
9384 case 'h': uv = (unsigned short)tuv; break;
9385 case 'l': uv = (unsigned long)tuv; break;
9386 case 'V':
9387 default: uv = tuv; break;
cf2093f6 9388#ifdef HAS_QUAD
b10c0dba 9389 case 'q': uv = (Uquad_t)tuv; break;
cf2093f6 9390#endif
46fc3d4c 9391 }
9392 }
9393
9394 integer:
4d84ee25
NC
9395 {
9396 char *ptr = ebuf + sizeof ebuf;
9397 switch (base) {
9398 unsigned dig;
9399 case 16:
9400 if (!uv)
9401 alt = FALSE;
9402 p = (char*)((c == 'X')
9403 ? "0123456789ABCDEF" : "0123456789abcdef");
9404 do {
9405 dig = uv & 15;
9406 *--ptr = p[dig];
9407 } while (uv >>= 4);
9408 if (alt) {
9409 esignbuf[esignlen++] = '0';
9410 esignbuf[esignlen++] = c; /* 'x' or 'X' */
9411 }
9412 break;
9413 case 8:
9414 do {
9415 dig = uv & 7;
9416 *--ptr = '0' + dig;
9417 } while (uv >>= 3);
9418 if (alt && *ptr != '0')
9419 *--ptr = '0';
9420 break;
9421 case 2:
9422 do {
9423 dig = uv & 1;
9424 *--ptr = '0' + dig;
9425 } while (uv >>= 1);
9426 if (alt) {
9427 esignbuf[esignlen++] = '0';
9428 esignbuf[esignlen++] = 'b';
9429 }
9430 break;
9431 default: /* it had better be ten or less */
9432 do {
9433 dig = uv % base;
9434 *--ptr = '0' + dig;
9435 } while (uv /= base);
9436 break;
46fc3d4c 9437 }
4d84ee25
NC
9438 elen = (ebuf + sizeof ebuf) - ptr;
9439 eptr = ptr;
9440 if (has_precis) {
9441 if (precis > elen)
9442 zeros = precis - elen;
9443 else if (precis == 0 && elen == 1 && *eptr == '0')
9444 elen = 0;
eda88b6d 9445 }
c10ed8b9 9446 }
46fc3d4c 9447 break;
9448
9449 /* FLOATING POINT */
9450
fc36a67e 9451 case 'F':
9452 c = 'f'; /* maybe %F isn't supported here */
9453 /* FALL THROUGH */
46fc3d4c 9454 case 'e': case 'E':
fc36a67e 9455 case 'f':
46fc3d4c 9456 case 'g': case 'G':
9457
9458 /* This is evil, but floating point is even more evil */
9459
9e5b023a
JH
9460 /* for SV-style calling, we can only get NV
9461 for C-style calling, we assume %f is double;
9462 for simplicity we allow any of %Lf, %llf, %qf for long double
9463 */
9464 switch (intsize) {
9465 case 'V':
9466#if defined(USE_LONG_DOUBLE)
9467 intsize = 'q';
9468#endif
9469 break;
8a2e3f14 9470/* [perl #20339] - we should accept and ignore %lf rather than die */
00e17364
HS
9471 case 'l':
9472 /* FALL THROUGH */
9e5b023a
JH
9473 default:
9474#if defined(USE_LONG_DOUBLE)
9475 intsize = args ? 0 : 'q';
9476#endif
9477 break;
9478 case 'q':
9479#if defined(HAS_LONG_DOUBLE)
9480 break;
9481#else
9482 /* FALL THROUGH */
9483#endif
9484 case 'h':
9e5b023a
JH
9485 goto unknown;
9486 }
9487
9488 /* now we need (long double) if intsize == 'q', else (double) */
be75b157 9489 nv = (args && !vectorize) ?
35fff930
JH
9490#if LONG_DOUBLESIZE > DOUBLESIZE
9491 intsize == 'q' ?
205f51d8
AS
9492 va_arg(*args, long double) :
9493 va_arg(*args, double)
35fff930 9494#else
205f51d8 9495 va_arg(*args, double)
35fff930 9496#endif
9e5b023a 9497 : SvNVx(argsv);
fc36a67e 9498
9499 need = 0;
be75b157 9500 vectorize = FALSE;
fc36a67e 9501 if (c != 'e' && c != 'E') {
9502 i = PERL_INT_MIN;
9e5b023a
JH
9503 /* FIXME: if HAS_LONG_DOUBLE but not USE_LONG_DOUBLE this
9504 will cast our (long double) to (double) */
73b309ea 9505 (void)Perl_frexp(nv, &i);
fc36a67e 9506 if (i == PERL_INT_MIN)
cea2e8a9 9507 Perl_die(aTHX_ "panic: frexp");
c635e13b 9508 if (i > 0)
fc36a67e 9509 need = BIT_DIGITS(i);
9510 }
9511 need += has_precis ? precis : 6; /* known default */
20f6aaab 9512
fc36a67e 9513 if (need < width)
9514 need = width;
9515
20f6aaab
AS
9516#ifdef HAS_LDBL_SPRINTF_BUG
9517 /* This is to try to fix a bug with irix/nonstop-ux/powerux and
205f51d8
AS
9518 with sfio - Allen <allens@cpan.org> */
9519
9520# ifdef DBL_MAX
9521# define MY_DBL_MAX DBL_MAX
9522# else /* XXX guessing! HUGE_VAL may be defined as infinity, so not using */
9523# if DOUBLESIZE >= 8
9524# define MY_DBL_MAX 1.7976931348623157E+308L
9525# else
9526# define MY_DBL_MAX 3.40282347E+38L
9527# endif
9528# endif
9529
9530# ifdef HAS_LDBL_SPRINTF_BUG_LESS1 /* only between -1L & 1L - Allen */
9531# define MY_DBL_MAX_BUG 1L
20f6aaab 9532# else
205f51d8 9533# define MY_DBL_MAX_BUG MY_DBL_MAX
20f6aaab 9534# endif
20f6aaab 9535
205f51d8
AS
9536# ifdef DBL_MIN
9537# define MY_DBL_MIN DBL_MIN
9538# else /* XXX guessing! -Allen */
9539# if DOUBLESIZE >= 8
9540# define MY_DBL_MIN 2.2250738585072014E-308L
9541# else
9542# define MY_DBL_MIN 1.17549435E-38L
9543# endif
9544# endif
20f6aaab 9545
205f51d8
AS
9546 if ((intsize == 'q') && (c == 'f') &&
9547 ((nv < MY_DBL_MAX_BUG) && (nv > -MY_DBL_MAX_BUG)) &&
9548 (need < DBL_DIG)) {
9549 /* it's going to be short enough that
9550 * long double precision is not needed */
9551
9552 if ((nv <= 0L) && (nv >= -0L))
9553 fix_ldbl_sprintf_bug = TRUE; /* 0 is 0 - easiest */
9554 else {
9555 /* would use Perl_fp_class as a double-check but not
9556 * functional on IRIX - see perl.h comments */
9557
9558 if ((nv >= MY_DBL_MIN) || (nv <= -MY_DBL_MIN)) {
9559 /* It's within the range that a double can represent */
9560#if defined(DBL_MAX) && !defined(DBL_MIN)
9561 if ((nv >= ((long double)1/DBL_MAX)) ||
9562 (nv <= (-(long double)1/DBL_MAX)))
20f6aaab 9563#endif
205f51d8 9564 fix_ldbl_sprintf_bug = TRUE;
20f6aaab 9565 }
205f51d8
AS
9566 }
9567 if (fix_ldbl_sprintf_bug == TRUE) {
9568 double temp;
9569
9570 intsize = 0;
9571 temp = (double)nv;
9572 nv = (NV)temp;
9573 }
20f6aaab 9574 }
205f51d8
AS
9575
9576# undef MY_DBL_MAX
9577# undef MY_DBL_MAX_BUG
9578# undef MY_DBL_MIN
9579
20f6aaab
AS
9580#endif /* HAS_LDBL_SPRINTF_BUG */
9581
46fc3d4c 9582 need += 20; /* fudge factor */
80252599
GS
9583 if (PL_efloatsize < need) {
9584 Safefree(PL_efloatbuf);
9585 PL_efloatsize = need + 20; /* more fudge */
9586 New(906, PL_efloatbuf, PL_efloatsize, char);
7d5ea4e7 9587 PL_efloatbuf[0] = '\0';
46fc3d4c 9588 }
9589
4151a5fe
IZ
9590 if ( !(width || left || plus || alt) && fill != '0'
9591 && has_precis && intsize != 'q' ) { /* Shortcuts */
2873255c
NC
9592 /* See earlier comment about buggy Gconvert when digits,
9593 aka precis is 0 */
9594 if ( c == 'g' && precis) {
2e59c212 9595 Gconvert((NV)nv, (int)precis, 0, PL_efloatbuf);
4151a5fe
IZ
9596 if (*PL_efloatbuf) /* May return an empty string for digits==0 */
9597 goto float_converted;
9598 } else if ( c == 'f' && !precis) {
9599 if ((eptr = F0convert(nv, ebuf + sizeof ebuf, &elen)))
9600 break;
9601 }
9602 }
4d84ee25
NC
9603 {
9604 char *ptr = ebuf + sizeof ebuf;
9605 *--ptr = '\0';
9606 *--ptr = c;
9607 /* FIXME: what to do if HAS_LONG_DOUBLE but not PERL_PRIfldbl? */
9e5b023a 9608#if defined(HAS_LONG_DOUBLE) && defined(PERL_PRIfldbl)
4d84ee25
NC
9609 if (intsize == 'q') {
9610 /* Copy the one or more characters in a long double
9611 * format before the 'base' ([efgEFG]) character to
9612 * the format string. */
9613 static char const prifldbl[] = PERL_PRIfldbl;
9614 char const *p = prifldbl + sizeof(prifldbl) - 3;
9615 while (p >= prifldbl) { *--ptr = *p--; }
9616 }
65202027 9617#endif
4d84ee25
NC
9618 if (has_precis) {
9619 base = precis;
9620 do { *--ptr = '0' + (base % 10); } while (base /= 10);
9621 *--ptr = '.';
9622 }
9623 if (width) {
9624 base = width;
9625 do { *--ptr = '0' + (base % 10); } while (base /= 10);
9626 }
9627 if (fill == '0')
9628 *--ptr = fill;
9629 if (left)
9630 *--ptr = '-';
9631 if (plus)
9632 *--ptr = plus;
9633 if (alt)
9634 *--ptr = '#';
9635 *--ptr = '%';
9636
9637 /* No taint. Otherwise we are in the strange situation
9638 * where printf() taints but print($float) doesn't.
9639 * --jhi */
9e5b023a 9640#if defined(HAS_LONG_DOUBLE)
4d84ee25
NC
9641 if (intsize == 'q')
9642 (void)sprintf(PL_efloatbuf, ptr, nv);
9643 else
9644 (void)sprintf(PL_efloatbuf, ptr, (double)nv);
9e5b023a 9645#else
4d84ee25 9646 (void)sprintf(PL_efloatbuf, ptr, nv);
9e5b023a 9647#endif
4d84ee25 9648 }
4151a5fe 9649 float_converted:
80252599
GS
9650 eptr = PL_efloatbuf;
9651 elen = strlen(PL_efloatbuf);
46fc3d4c 9652 break;
9653
fc36a67e 9654 /* SPECIAL */
9655
9656 case 'n':
9657 i = SvCUR(sv) - origlen;
be75b157 9658 if (args && !vectorize) {
c635e13b 9659 switch (intsize) {
9660 case 'h': *(va_arg(*args, short*)) = i; break;
9661 default: *(va_arg(*args, int*)) = i; break;
9662 case 'l': *(va_arg(*args, long*)) = i; break;
9663 case 'V': *(va_arg(*args, IV*)) = i; break;
cf2093f6
JH
9664#ifdef HAS_QUAD
9665 case 'q': *(va_arg(*args, Quad_t*)) = i; break;
9666#endif
c635e13b 9667 }
fc36a67e 9668 }
9dd79c3f 9669 else
211dfcf1 9670 sv_setuv_mg(argsv, (UV)i);
be75b157 9671 vectorize = FALSE;
fc36a67e 9672 continue; /* not "break" */
9673
9674 /* UNKNOWN */
9675
46fc3d4c 9676 default:
fc36a67e 9677 unknown:
599cee73 9678 if (!args && ckWARN(WARN_PRINTF) &&
533c011a 9679 (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF)) {
c635e13b 9680 SV *msg = sv_newmortal();
35c1215d
NC
9681 Perl_sv_setpvf(aTHX_ msg, "Invalid conversion in %sprintf: ",
9682 (PL_op->op_type == OP_PRTF) ? "" : "s");
0f4b6630 9683 if (c) {
0f4b6630 9684 if (isPRINT(c))
1c846c1f 9685 Perl_sv_catpvf(aTHX_ msg,
0f4b6630
JH
9686 "\"%%%c\"", c & 0xFF);
9687 else
9688 Perl_sv_catpvf(aTHX_ msg,
57def98f 9689 "\"%%\\%03"UVof"\"",
0f4b6630 9690 (UV)c & 0xFF);
0f4b6630 9691 } else
c635e13b 9692 sv_catpv(msg, "end of string");
9014280d 9693 Perl_warner(aTHX_ packWARN(WARN_PRINTF), "%"SVf, msg); /* yes, this is reentrant */
c635e13b 9694 }
fb73857a 9695
9696 /* output mangled stuff ... */
9697 if (c == '\0')
9698 --q;
46fc3d4c 9699 eptr = p;
9700 elen = q - p;
fb73857a 9701
9702 /* ... right here, because formatting flags should not apply */
9703 SvGROW(sv, SvCUR(sv) + elen + 1);
9704 p = SvEND(sv);
4459522c 9705 Copy(eptr, p, elen, char);
fb73857a 9706 p += elen;
9707 *p = '\0';
3f7c398e 9708 SvCUR_set(sv, p - SvPVX_const(sv));
58e33a90 9709 svix = osvix;
fb73857a 9710 continue; /* not "break" */
46fc3d4c 9711 }
9712
6c94ec8b
HS
9713 /* calculate width before utf8_upgrade changes it */
9714 have = esignlen + zeros + elen;
9715
d2876be5
JH
9716 if (is_utf8 != has_utf8) {
9717 if (is_utf8) {
9718 if (SvCUR(sv))
9719 sv_utf8_upgrade(sv);
9720 }
9721 else {
53c1dcc0 9722 SV * const nsv = sv_2mortal(newSVpvn(eptr, elen));
d2876be5 9723 sv_utf8_upgrade(nsv);
93524f2b 9724 eptr = SvPVX_const(nsv);
d2876be5
JH
9725 elen = SvCUR(nsv);
9726 }
9727 SvGROW(sv, SvCUR(sv) + elen + 1);
9728 p = SvEND(sv);
9729 *p = '\0';
9730 }
6af65485 9731
46fc3d4c 9732 need = (have > width ? have : width);
9733 gap = need - have;
9734
b22c7a20 9735 SvGROW(sv, SvCUR(sv) + need + dotstrlen + 1);
46fc3d4c 9736 p = SvEND(sv);
9737 if (esignlen && fill == '0') {
53c1dcc0 9738 int i;
eb160463 9739 for (i = 0; i < (int)esignlen; i++)
46fc3d4c 9740 *p++ = esignbuf[i];
9741 }
9742 if (gap && !left) {
9743 memset(p, fill, gap);
9744 p += gap;
9745 }
9746 if (esignlen && fill != '0') {
53c1dcc0 9747 int i;
eb160463 9748 for (i = 0; i < (int)esignlen; i++)
46fc3d4c 9749 *p++ = esignbuf[i];
9750 }
fc36a67e 9751 if (zeros) {
53c1dcc0 9752 int i;
fc36a67e 9753 for (i = zeros; i; i--)
9754 *p++ = '0';
9755 }
46fc3d4c 9756 if (elen) {
4459522c 9757 Copy(eptr, p, elen, char);
46fc3d4c 9758 p += elen;
9759 }
9760 if (gap && left) {
9761 memset(p, ' ', gap);
9762 p += gap;
9763 }
b22c7a20
GS
9764 if (vectorize) {
9765 if (veclen) {
4459522c 9766 Copy(dotstr, p, dotstrlen, char);
b22c7a20
GS
9767 p += dotstrlen;
9768 }
9769 else
9770 vectorize = FALSE; /* done iterating over vecstr */
9771 }
2cf2cfc6
A
9772 if (is_utf8)
9773 has_utf8 = TRUE;
9774 if (has_utf8)
7e2040f0 9775 SvUTF8_on(sv);
46fc3d4c 9776 *p = '\0';
3f7c398e 9777 SvCUR_set(sv, p - SvPVX_const(sv));
b22c7a20
GS
9778 if (vectorize) {
9779 esignlen = 0;
9780 goto vector;
9781 }
46fc3d4c 9782 }
9783}
51371543 9784
645c22ef
DM
9785/* =========================================================================
9786
9787=head1 Cloning an interpreter
9788
9789All the macros and functions in this section are for the private use of
9790the main function, perl_clone().
9791
9792The foo_dup() functions make an exact copy of an existing foo thinngy.
9793During the course of a cloning, a hash table is used to map old addresses
9794to new addresses. The table is created and manipulated with the
9795ptr_table_* functions.
9796
9797=cut
9798
9799============================================================================*/
9800
9801
1d7c1841
GS
9802#if defined(USE_ITHREADS)
9803
1d7c1841
GS
9804#ifndef GpREFCNT_inc
9805# define GpREFCNT_inc(gp) ((gp) ? (++(gp)->gp_refcnt, (gp)) : (GP*)NULL)
9806#endif
9807
9808
d2d73c3e
AB
9809#define sv_dup_inc(s,t) SvREFCNT_inc(sv_dup(s,t))
9810#define av_dup(s,t) (AV*)sv_dup((SV*)s,t)
9811#define av_dup_inc(s,t) (AV*)SvREFCNT_inc(sv_dup((SV*)s,t))
9812#define hv_dup(s,t) (HV*)sv_dup((SV*)s,t)
9813#define hv_dup_inc(s,t) (HV*)SvREFCNT_inc(sv_dup((SV*)s,t))
9814#define cv_dup(s,t) (CV*)sv_dup((SV*)s,t)
9815#define cv_dup_inc(s,t) (CV*)SvREFCNT_inc(sv_dup((SV*)s,t))
9816#define io_dup(s,t) (IO*)sv_dup((SV*)s,t)
9817#define io_dup_inc(s,t) (IO*)SvREFCNT_inc(sv_dup((SV*)s,t))
9818#define gv_dup(s,t) (GV*)sv_dup((SV*)s,t)
9819#define gv_dup_inc(s,t) (GV*)SvREFCNT_inc(sv_dup((SV*)s,t))
1d7c1841
GS
9820#define SAVEPV(p) (p ? savepv(p) : Nullch)
9821#define SAVEPVN(p,n) (p ? savepvn(p,n) : Nullch)
8cf8f3d1 9822
d2d73c3e 9823
d2f185dc
AMS
9824/* Duplicate a regexp. Required reading: pregcomp() and pregfree() in
9825 regcomp.c. AMS 20010712 */
645c22ef 9826
1d7c1841 9827REGEXP *
53c1dcc0 9828Perl_re_dup(pTHX_ const REGEXP *r, CLONE_PARAMS *param)
1d7c1841 9829{
27da23d5 9830 dVAR;
d2f185dc
AMS
9831 REGEXP *ret;
9832 int i, len, npar;
9833 struct reg_substr_datum *s;
9834
9835 if (!r)
9836 return (REGEXP *)NULL;
9837
9838 if ((ret = (REGEXP *)ptr_table_fetch(PL_ptr_table, r)))
9839 return ret;
9840
9841 len = r->offsets[0];
9842 npar = r->nparens+1;
9843
9844 Newc(0, ret, sizeof(regexp) + (len+1)*sizeof(regnode), char, regexp);
9845 Copy(r->program, ret->program, len+1, regnode);
9846
9847 New(0, ret->startp, npar, I32);
9848 Copy(r->startp, ret->startp, npar, I32);
9849 New(0, ret->endp, npar, I32);
9850 Copy(r->startp, ret->startp, npar, I32);
9851
d2f185dc
AMS
9852 New(0, ret->substrs, 1, struct reg_substr_data);
9853 for (s = ret->substrs->data, i = 0; i < 3; i++, s++) {
9854 s->min_offset = r->substrs->data[i].min_offset;
9855 s->max_offset = r->substrs->data[i].max_offset;
9856 s->substr = sv_dup_inc(r->substrs->data[i].substr, param);
33b8afdf 9857 s->utf8_substr = sv_dup_inc(r->substrs->data[i].utf8_substr, param);
d2f185dc
AMS
9858 }
9859
70612e96 9860 ret->regstclass = NULL;
d2f185dc
AMS
9861 if (r->data) {
9862 struct reg_data *d;
e1ec3a88 9863 const int count = r->data->count;
53c1dcc0 9864 int i;
d2f185dc
AMS
9865
9866 Newc(0, d, sizeof(struct reg_data) + count*sizeof(void *),
9867 char, struct reg_data);
9868 New(0, d->what, count, U8);
9869
9870 d->count = count;
9871 for (i = 0; i < count; i++) {
9872 d->what[i] = r->data->what[i];
9873 switch (d->what[i]) {
a3621e74
YO
9874 /* legal options are one of: sfpont
9875 see also regcomp.h and pregfree() */
d2f185dc
AMS
9876 case 's':
9877 d->data[i] = sv_dup_inc((SV *)r->data->data[i], param);
9878 break;
9879 case 'p':
9880 d->data[i] = av_dup_inc((AV *)r->data->data[i], param);
9881 break;
9882 case 'f':
9883 /* This is cheating. */
9884 New(0, d->data[i], 1, struct regnode_charclass_class);
9885 StructCopy(r->data->data[i], d->data[i],
9886 struct regnode_charclass_class);
70612e96 9887 ret->regstclass = (regnode*)d->data[i];
d2f185dc
AMS
9888 break;
9889 case 'o':
33773810
AMS
9890 /* Compiled op trees are readonly, and can thus be
9891 shared without duplication. */
b34c0dd4 9892 OP_REFCNT_LOCK;
9b978d73 9893 d->data[i] = (void*)OpREFCNT_inc((OP*)r->data->data[i]);
b34c0dd4 9894 OP_REFCNT_UNLOCK;
9b978d73 9895 break;
d2f185dc
AMS
9896 case 'n':
9897 d->data[i] = r->data->data[i];
9898 break;
a3621e74
YO
9899 case 't':
9900 d->data[i] = r->data->data[i];
9901 OP_REFCNT_LOCK;
9902 ((reg_trie_data*)d->data[i])->refcount++;
9903 OP_REFCNT_UNLOCK;
9904 break;
9905 default:
9906 Perl_croak(aTHX_ "panic: re_dup unknown data code '%c'", r->data->what[i]);
d2f185dc
AMS
9907 }
9908 }
9909
9910 ret->data = d;
9911 }
9912 else
9913 ret->data = NULL;
9914
9915 New(0, ret->offsets, 2*len+1, U32);
9916 Copy(r->offsets, ret->offsets, 2*len+1, U32);
9917
e01c5899 9918 ret->precomp = SAVEPVN(r->precomp, r->prelen);
d2f185dc
AMS
9919 ret->refcnt = r->refcnt;
9920 ret->minlen = r->minlen;
9921 ret->prelen = r->prelen;
9922 ret->nparens = r->nparens;
9923 ret->lastparen = r->lastparen;
9924 ret->lastcloseparen = r->lastcloseparen;
9925 ret->reganch = r->reganch;
9926
70612e96
RG
9927 ret->sublen = r->sublen;
9928
9929 if (RX_MATCH_COPIED(ret))
e01c5899 9930 ret->subbeg = SAVEPVN(r->subbeg, r->sublen);
70612e96
RG
9931 else
9932 ret->subbeg = Nullch;
f8c7b90f 9933#ifdef PERL_OLD_COPY_ON_WRITE
9a26048b
NC
9934 ret->saved_copy = Nullsv;
9935#endif
70612e96 9936
d2f185dc
AMS
9937 ptr_table_store(PL_ptr_table, r, ret);
9938 return ret;
1d7c1841
GS
9939}
9940
d2d73c3e 9941/* duplicate a file handle */
645c22ef 9942
1d7c1841 9943PerlIO *
a8fc9800 9944Perl_fp_dup(pTHX_ PerlIO *fp, char type, CLONE_PARAMS *param)
1d7c1841
GS
9945{
9946 PerlIO *ret;
53c1dcc0
AL
9947
9948 PERL_UNUSED_ARG(type);
73d840c0 9949
1d7c1841
GS
9950 if (!fp)
9951 return (PerlIO*)NULL;
9952
9953 /* look for it in the table first */
9954 ret = (PerlIO*)ptr_table_fetch(PL_ptr_table, fp);
9955 if (ret)
9956 return ret;
9957
9958 /* create anew and remember what it is */
ecdeb87c 9959 ret = PerlIO_fdupopen(aTHX_ fp, param, PERLIO_DUP_CLONE);
1d7c1841
GS
9960 ptr_table_store(PL_ptr_table, fp, ret);
9961 return ret;
9962}
9963
645c22ef
DM
9964/* duplicate a directory handle */
9965
1d7c1841
GS
9966DIR *
9967Perl_dirp_dup(pTHX_ DIR *dp)
9968{
9969 if (!dp)
9970 return (DIR*)NULL;
9971 /* XXX TODO */
9972 return dp;
9973}
9974
ff276b08 9975/* duplicate a typeglob */
645c22ef 9976
1d7c1841 9977GP *
a8fc9800 9978Perl_gp_dup(pTHX_ GP *gp, CLONE_PARAMS* param)
1d7c1841
GS
9979{
9980 GP *ret;
9981 if (!gp)
9982 return (GP*)NULL;
9983 /* look for it in the table first */
9984 ret = (GP*)ptr_table_fetch(PL_ptr_table, gp);
9985 if (ret)
9986 return ret;
9987
9988 /* create anew and remember what it is */
9989 Newz(0, ret, 1, GP);
9990 ptr_table_store(PL_ptr_table, gp, ret);
9991
9992 /* clone */
9993 ret->gp_refcnt = 0; /* must be before any other dups! */
d2d73c3e
AB
9994 ret->gp_sv = sv_dup_inc(gp->gp_sv, param);
9995 ret->gp_io = io_dup_inc(gp->gp_io, param);
9996 ret->gp_form = cv_dup_inc(gp->gp_form, param);
9997 ret->gp_av = av_dup_inc(gp->gp_av, param);
9998 ret->gp_hv = hv_dup_inc(gp->gp_hv, param);
9999 ret->gp_egv = gv_dup(gp->gp_egv, param);/* GvEGV is not refcounted */
10000 ret->gp_cv = cv_dup_inc(gp->gp_cv, param);
1d7c1841
GS
10001 ret->gp_cvgen = gp->gp_cvgen;
10002 ret->gp_flags = gp->gp_flags;
10003 ret->gp_line = gp->gp_line;
10004 ret->gp_file = gp->gp_file; /* points to COP.cop_file */
10005 return ret;
10006}
10007
645c22ef
DM
10008/* duplicate a chain of magic */
10009
1d7c1841 10010MAGIC *
a8fc9800 10011Perl_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS* param)
1d7c1841 10012{
cb359b41
JH
10013 MAGIC *mgprev = (MAGIC*)NULL;
10014 MAGIC *mgret;
1d7c1841
GS
10015 if (!mg)
10016 return (MAGIC*)NULL;
10017 /* look for it in the table first */
10018 mgret = (MAGIC*)ptr_table_fetch(PL_ptr_table, mg);
10019 if (mgret)
10020 return mgret;
10021
10022 for (; mg; mg = mg->mg_moremagic) {
10023 MAGIC *nmg;
10024 Newz(0, nmg, 1, MAGIC);
cb359b41 10025 if (mgprev)
1d7c1841 10026 mgprev->mg_moremagic = nmg;
cb359b41
JH
10027 else
10028 mgret = nmg;
1d7c1841
GS
10029 nmg->mg_virtual = mg->mg_virtual; /* XXX copy dynamic vtable? */
10030 nmg->mg_private = mg->mg_private;
10031 nmg->mg_type = mg->mg_type;
10032 nmg->mg_flags = mg->mg_flags;
14befaf4 10033 if (mg->mg_type == PERL_MAGIC_qr) {
d2f185dc 10034 nmg->mg_obj = (SV*)re_dup((REGEXP*)mg->mg_obj, param);
1d7c1841 10035 }
05bd4103 10036 else if(mg->mg_type == PERL_MAGIC_backref) {
7fc63493 10037 const AV * const av = (AV*) mg->mg_obj;
fdc9a813
AE
10038 SV **svp;
10039 I32 i;
7fc63493 10040 (void)SvREFCNT_inc(nmg->mg_obj = (SV*)newAV());
fdc9a813
AE
10041 svp = AvARRAY(av);
10042 for (i = AvFILLp(av); i >= 0; i--) {
3a81978b 10043 if (!svp[i]) continue;
fdc9a813
AE
10044 av_push((AV*)nmg->mg_obj,sv_dup(svp[i],param));
10045 }
05bd4103 10046 }
8d2f4536
NC
10047 else if (mg->mg_type == PERL_MAGIC_symtab) {
10048 nmg->mg_obj = mg->mg_obj;
10049 }
1d7c1841
GS
10050 else {
10051 nmg->mg_obj = (mg->mg_flags & MGf_REFCOUNTED)
d2d73c3e
AB
10052 ? sv_dup_inc(mg->mg_obj, param)
10053 : sv_dup(mg->mg_obj, param);
1d7c1841
GS
10054 }
10055 nmg->mg_len = mg->mg_len;
10056 nmg->mg_ptr = mg->mg_ptr; /* XXX random ptr? */
14befaf4 10057 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
68795e93 10058 if (mg->mg_len > 0) {
1d7c1841 10059 nmg->mg_ptr = SAVEPVN(mg->mg_ptr, mg->mg_len);
14befaf4
DM
10060 if (mg->mg_type == PERL_MAGIC_overload_table &&
10061 AMT_AMAGIC((AMT*)mg->mg_ptr))
10062 {
1d7c1841
GS
10063 AMT *amtp = (AMT*)mg->mg_ptr;
10064 AMT *namtp = (AMT*)nmg->mg_ptr;
10065 I32 i;
10066 for (i = 1; i < NofAMmeth; i++) {
d2d73c3e 10067 namtp->table[i] = cv_dup_inc(amtp->table[i], param);
1d7c1841
GS
10068 }
10069 }
10070 }
10071 else if (mg->mg_len == HEf_SVKEY)
d2d73c3e 10072 nmg->mg_ptr = (char*)sv_dup_inc((SV*)mg->mg_ptr, param);
1d7c1841 10073 }
68795e93
NIS
10074 if ((mg->mg_flags & MGf_DUP) && mg->mg_virtual && mg->mg_virtual->svt_dup) {
10075 CALL_FPTR(nmg->mg_virtual->svt_dup)(aTHX_ nmg, param);
10076 }
1d7c1841
GS
10077 mgprev = nmg;
10078 }
10079 return mgret;
10080}
10081
645c22ef
DM
10082/* create a new pointer-mapping table */
10083
1d7c1841
GS
10084PTR_TBL_t *
10085Perl_ptr_table_new(pTHX)
10086{
10087 PTR_TBL_t *tbl;
10088 Newz(0, tbl, 1, PTR_TBL_t);
10089 tbl->tbl_max = 511;
10090 tbl->tbl_items = 0;
10091 Newz(0, tbl->tbl_ary, tbl->tbl_max + 1, PTR_TBL_ENT_t*);
10092 return tbl;
10093}
10094
134ca3d6
DM
10095#if (PTRSIZE == 8)
10096# define PTR_TABLE_HASH(ptr) (PTR2UV(ptr) >> 3)
10097#else
10098# define PTR_TABLE_HASH(ptr) (PTR2UV(ptr) >> 2)
10099#endif
10100
437a6bf1 10101#define new_pte() new_body(struct ptr_tbl_ent, pte)
cb4415b8 10102#define del_pte(p) del_body_type(p, struct ptr_tbl_ent, pte)
32e691d0 10103
645c22ef
DM
10104/* map an existing pointer using a table */
10105
1d7c1841 10106void *
53c1dcc0 10107Perl_ptr_table_fetch(pTHX_ PTR_TBL_t *tbl, const void *sv)
1d7c1841
GS
10108{
10109 PTR_TBL_ENT_t *tblent;
4373e329 10110 const UV hash = PTR_TABLE_HASH(sv);
1d7c1841
GS
10111 assert(tbl);
10112 tblent = tbl->tbl_ary[hash & tbl->tbl_max];
10113 for (; tblent; tblent = tblent->next) {
10114 if (tblent->oldval == sv)
10115 return tblent->newval;
10116 }
10117 return (void*)NULL;
10118}
10119
645c22ef
DM
10120/* add a new entry to a pointer-mapping table */
10121
1d7c1841 10122void
53c1dcc0 10123Perl_ptr_table_store(pTHX_ PTR_TBL_t *tbl, const void *oldv, void *newv)
1d7c1841
GS
10124{
10125 PTR_TBL_ENT_t *tblent, **otblent;
10126 /* XXX this may be pessimal on platforms where pointers aren't good
10127 * hash values e.g. if they grow faster in the most significant
10128 * bits */
4373e329 10129 const UV hash = PTR_TABLE_HASH(oldv);
14cade97 10130 bool empty = 1;
1d7c1841
GS
10131
10132 assert(tbl);
10133 otblent = &tbl->tbl_ary[hash & tbl->tbl_max];
14cade97 10134 for (tblent = *otblent; tblent; empty=0, tblent = tblent->next) {
1d7c1841
GS
10135 if (tblent->oldval == oldv) {
10136 tblent->newval = newv;
1d7c1841
GS
10137 return;
10138 }
10139 }
437a6bf1 10140 tblent = new_pte();
1d7c1841
GS
10141 tblent->oldval = oldv;
10142 tblent->newval = newv;
10143 tblent->next = *otblent;
10144 *otblent = tblent;
10145 tbl->tbl_items++;
14cade97 10146 if (!empty && tbl->tbl_items > tbl->tbl_max)
1d7c1841
GS
10147 ptr_table_split(tbl);
10148}
10149
645c22ef
DM
10150/* double the hash bucket size of an existing ptr table */
10151
1d7c1841
GS
10152void
10153Perl_ptr_table_split(pTHX_ PTR_TBL_t *tbl)
10154{
10155 PTR_TBL_ENT_t **ary = tbl->tbl_ary;
4373e329 10156 const UV oldsize = tbl->tbl_max + 1;
1d7c1841
GS
10157 UV newsize = oldsize * 2;
10158 UV i;
10159
10160 Renew(ary, newsize, PTR_TBL_ENT_t*);
10161 Zero(&ary[oldsize], newsize-oldsize, PTR_TBL_ENT_t*);
10162 tbl->tbl_max = --newsize;
10163 tbl->tbl_ary = ary;
10164 for (i=0; i < oldsize; i++, ary++) {
10165 PTR_TBL_ENT_t **curentp, **entp, *ent;
10166 if (!*ary)
10167 continue;
10168 curentp = ary + oldsize;
10169 for (entp = ary, ent = *ary; ent; ent = *entp) {
134ca3d6 10170 if ((newsize & PTR_TABLE_HASH(ent->oldval)) != i) {
1d7c1841
GS
10171 *entp = ent->next;
10172 ent->next = *curentp;
10173 *curentp = ent;
10174 continue;
10175 }
10176 else
10177 entp = &ent->next;
10178 }
10179 }
10180}
10181
645c22ef
DM
10182/* remove all the entries from a ptr table */
10183
a0739874
DM
10184void
10185Perl_ptr_table_clear(pTHX_ PTR_TBL_t *tbl)
10186{
10187 register PTR_TBL_ENT_t **array;
10188 register PTR_TBL_ENT_t *entry;
a0739874
DM
10189 UV riter = 0;
10190 UV max;
10191
10192 if (!tbl || !tbl->tbl_items) {
10193 return;
10194 }
10195
10196 array = tbl->tbl_ary;
10197 entry = array[0];
10198 max = tbl->tbl_max;
10199
10200 for (;;) {
10201 if (entry) {
4373e329 10202 PTR_TBL_ENT_t *oentry = entry;
a0739874 10203 entry = entry->next;
437a6bf1 10204 del_pte(oentry);
a0739874
DM
10205 }
10206 if (!entry) {
10207 if (++riter > max) {
10208 break;
10209 }
10210 entry = array[riter];
10211 }
10212 }
10213
10214 tbl->tbl_items = 0;
10215}
10216
645c22ef
DM
10217/* clear and free a ptr table */
10218
a0739874
DM
10219void
10220Perl_ptr_table_free(pTHX_ PTR_TBL_t *tbl)
10221{
10222 if (!tbl) {
10223 return;
10224 }
10225 ptr_table_clear(tbl);
10226 Safefree(tbl->tbl_ary);
10227 Safefree(tbl);
10228}
10229
5bd07a3d 10230
83841fad
NIS
10231void
10232Perl_rvpv_dup(pTHX_ SV *dstr, SV *sstr, CLONE_PARAMS* param)
10233{
10234 if (SvROK(sstr)) {
b162af07
SP
10235 SvRV_set(dstr, SvWEAKREF(sstr)
10236 ? sv_dup(SvRV(sstr), param)
10237 : sv_dup_inc(SvRV(sstr), param));
f880fe2f 10238
83841fad 10239 }
3f7c398e 10240 else if (SvPVX_const(sstr)) {
83841fad
NIS
10241 /* Has something there */
10242 if (SvLEN(sstr)) {
68795e93 10243 /* Normal PV - clone whole allocated space */
3f7c398e 10244 SvPV_set(dstr, SAVEPVN(SvPVX_const(sstr), SvLEN(sstr)-1));
d3d0e6f1
NC
10245 if (SvREADONLY(sstr) && SvFAKE(sstr)) {
10246 /* Not that normal - actually sstr is copy on write.
10247 But we are a true, independant SV, so: */
10248 SvREADONLY_off(dstr);
10249 SvFAKE_off(dstr);
10250 }
68795e93 10251 }
83841fad
NIS
10252 else {
10253 /* Special case - not normally malloced for some reason */
ef10be65
NC
10254 if ((SvREADONLY(sstr) && SvFAKE(sstr))) {
10255 /* A "shared" PV - clone it as "shared" PV */
10256 SvPV_set(dstr,
10257 HEK_KEY(hek_dup(SvSHARED_HEK_FROM_PV(SvPVX_const(sstr)),
10258 param)));
83841fad
NIS
10259 }
10260 else {
10261 /* Some other special case - random pointer */
f880fe2f 10262 SvPV_set(dstr, SvPVX(sstr));
d3d0e6f1 10263 }
83841fad
NIS
10264 }
10265 }
10266 else {
10267 /* Copy the Null */
f880fe2f 10268 if (SvTYPE(dstr) == SVt_RV)
b162af07 10269 SvRV_set(dstr, NULL);
f880fe2f
SP
10270 else
10271 SvPV_set(dstr, 0);
83841fad
NIS
10272 }
10273}
10274
662fb8b2
NC
10275/* duplicate an SV of any type (including AV, HV etc) */
10276
1d7c1841 10277SV *
a8fc9800 10278Perl_sv_dup(pTHX_ SV *sstr, CLONE_PARAMS* param)
1d7c1841 10279{
27da23d5 10280 dVAR;
1d7c1841
GS
10281 SV *dstr;
10282
10283 if (!sstr || SvTYPE(sstr) == SVTYPEMASK)
10284 return Nullsv;
10285 /* look for it in the table first */
10286 dstr = (SV*)ptr_table_fetch(PL_ptr_table, sstr);
10287 if (dstr)
10288 return dstr;
10289
0405e91e
AB
10290 if(param->flags & CLONEf_JOIN_IN) {
10291 /** We are joining here so we don't want do clone
10292 something that is bad **/
bfcb3514 10293 const char *hvname;
0405e91e
AB
10294
10295 if(SvTYPE(sstr) == SVt_PVHV &&
bfcb3514 10296 (hvname = HvNAME_get(sstr))) {
0405e91e 10297 /** don't clone stashes if they already exist **/
bfcb3514 10298 HV* old_stash = gv_stashpv(hvname,0);
0405e91e
AB
10299 return (SV*) old_stash;
10300 }
10301 }
10302
1d7c1841
GS
10303 /* create anew and remember what it is */
10304 new_SV(dstr);
fd0854ff
DM
10305
10306#ifdef DEBUG_LEAKING_SCALARS
10307 dstr->sv_debug_optype = sstr->sv_debug_optype;
10308 dstr->sv_debug_line = sstr->sv_debug_line;
10309 dstr->sv_debug_inpad = sstr->sv_debug_inpad;
10310 dstr->sv_debug_cloned = 1;
10311# ifdef NETWARE
10312 dstr->sv_debug_file = savepv(sstr->sv_debug_file);
10313# else
10314 dstr->sv_debug_file = savesharedpv(sstr->sv_debug_file);
10315# endif
10316#endif
10317
1d7c1841
GS
10318 ptr_table_store(PL_ptr_table, sstr, dstr);
10319
10320 /* clone */
10321 SvFLAGS(dstr) = SvFLAGS(sstr);
10322 SvFLAGS(dstr) &= ~SVf_OOK; /* don't propagate OOK hack */
10323 SvREFCNT(dstr) = 0; /* must be before any other dups! */
10324
10325#ifdef DEBUGGING
3f7c398e 10326 if (SvANY(sstr) && PL_watch_pvx && SvPVX_const(sstr) == PL_watch_pvx)
1d7c1841 10327 PerlIO_printf(Perl_debug_log, "watch at %p hit, found string \"%s\"\n",
3f7c398e 10328 PL_watch_pvx, SvPVX_const(sstr));
1d7c1841
GS
10329#endif
10330
9660f481
DM
10331 /* don't clone objects whose class has asked us not to */
10332 if (SvOBJECT(sstr) && ! (SvFLAGS(SvSTASH(sstr)) & SVphv_CLONEABLE)) {
10333 SvFLAGS(dstr) &= ~SVTYPEMASK;
10334 SvOBJECT_off(dstr);
10335 return dstr;
10336 }
10337
1d7c1841
GS
10338 switch (SvTYPE(sstr)) {
10339 case SVt_NULL:
10340 SvANY(dstr) = NULL;
10341 break;
10342 case SVt_IV:
339049b0 10343 SvANY(dstr) = (XPVIV*)((char*)&(dstr->sv_u.svu_iv) - STRUCT_OFFSET(XPVIV, xiv_iv));
45977657 10344 SvIV_set(dstr, SvIVX(sstr));
1d7c1841
GS
10345 break;
10346 case SVt_NV:
10347 SvANY(dstr) = new_XNV();
9d6ce603 10348 SvNV_set(dstr, SvNVX(sstr));
1d7c1841
GS
10349 break;
10350 case SVt_RV:
339049b0 10351 SvANY(dstr) = &(dstr->sv_u.svu_rv);
83841fad 10352 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
1d7c1841 10353 break;
662fb8b2
NC
10354 default:
10355 {
10356 /* These are all the types that need complex bodies allocating. */
10357 size_t new_body_length;
10358 size_t new_body_offset = 0;
10359 void **new_body_arena;
10360 void **new_body_arenaroot;
10361 void *new_body;
10362
10363 switch (SvTYPE(sstr)) {
10364 default:
10365 Perl_croak(aTHX_ "Bizarre SvTYPE [%" IVdf "]",
10366 (IV)SvTYPE(sstr));
10367 break;
10368
10369 case SVt_PVIO:
10370 new_body = new_XPVIO();
10371 new_body_length = sizeof(XPVIO);
10372 break;
10373 case SVt_PVFM:
10374 new_body = new_XPVFM();
10375 new_body_length = sizeof(XPVFM);
10376 break;
10377
10378 case SVt_PVHV:
10379 new_body_arena = (void **) &PL_xpvhv_root;
10380 new_body_arenaroot = (void **) &PL_xpvhv_arenaroot;
10381 new_body_offset = STRUCT_OFFSET(XPVHV, xhv_fill)
10382 - STRUCT_OFFSET(xpvhv_allocated, xhv_fill);
10383 new_body_length = STRUCT_OFFSET(XPVHV, xmg_stash)
10384 + sizeof (((XPVHV*)SvANY(sstr))->xmg_stash)
10385 - new_body_offset;
10386 goto new_body;
10387 case SVt_PVAV:
10388 new_body_arena = (void **) &PL_xpvav_root;
10389 new_body_arenaroot = (void **) &PL_xpvav_arenaroot;
10390 new_body_offset = STRUCT_OFFSET(XPVAV, xav_fill)
10391 - STRUCT_OFFSET(xpvav_allocated, xav_fill);
10392 new_body_length = STRUCT_OFFSET(XPVHV, xmg_stash)
10393 + sizeof (((XPVHV*)SvANY(sstr))->xmg_stash)
10394 - new_body_offset;
10395 goto new_body;
10396 case SVt_PVBM:
10397 new_body_length = sizeof(XPVBM);
10398 new_body_arena = (void **) &PL_xpvbm_root;
10399 new_body_arenaroot = (void **) &PL_xpvbm_arenaroot;
10400 goto new_body;
10401 case SVt_PVGV:
10402 if (GvUNIQUE((GV*)sstr)) {
0abe3f7c 10403 /* Do sharing here. */
662fb8b2
NC
10404 }
10405 new_body_length = sizeof(XPVGV);
10406 new_body_arena = (void **) &PL_xpvgv_root;
10407 new_body_arenaroot = (void **) &PL_xpvgv_arenaroot;
10408 goto new_body;
10409 case SVt_PVCV:
10410 new_body_length = sizeof(XPVCV);
10411 new_body_arena = (void **) &PL_xpvcv_root;
10412 new_body_arenaroot = (void **) &PL_xpvcv_arenaroot;
10413 goto new_body;
10414 case SVt_PVLV:
10415 new_body_length = sizeof(XPVLV);
10416 new_body_arena = (void **) &PL_xpvlv_root;
10417 new_body_arenaroot = (void **) &PL_xpvlv_arenaroot;
10418 goto new_body;
10419 case SVt_PVMG:
10420 new_body_length = sizeof(XPVMG);
10421 new_body_arena = (void **) &PL_xpvmg_root;
10422 new_body_arenaroot = (void **) &PL_xpvmg_arenaroot;
10423 goto new_body;
10424 case SVt_PVNV:
10425 new_body_length = sizeof(XPVNV);
10426 new_body_arena = (void **) &PL_xpvnv_root;
10427 new_body_arenaroot = (void **) &PL_xpvnv_arenaroot;
10428 goto new_body;
10429 case SVt_PVIV:
10430 new_body_offset = STRUCT_OFFSET(XPVIV, xpv_cur)
10431 - STRUCT_OFFSET(xpviv_allocated, xpv_cur);
10432 new_body_length = sizeof(XPVIV) - new_body_offset;
10433 new_body_arena = (void **) &PL_xpviv_root;
10434 new_body_arenaroot = (void **) &PL_xpviv_arenaroot;
10435 goto new_body;
10436 case SVt_PV:
10437 new_body_offset = STRUCT_OFFSET(XPV, xpv_cur)
10438 - STRUCT_OFFSET(xpv_allocated, xpv_cur);
10439 new_body_length = sizeof(XPV) - new_body_offset;
10440 new_body_arena = (void **) &PL_xpv_root;
10441 new_body_arenaroot = (void **) &PL_xpv_arenaroot;
10442 new_body:
10443 assert(new_body_length);
10444#ifndef PURIFY
dd690478
NC
10445 new_body = (void*)((char*)S_new_body(aTHX_ new_body_arenaroot,
10446 new_body_arena,
10447 new_body_length)
10448 - new_body_offset);
662fb8b2
NC
10449#else
10450 /* We always allocated the full length item with PURIFY */
10451 new_body_length += new_body_offset;
10452 new_body_offset = 0;
10453 new_body = my_safemalloc(new_body_length);
5bd07a3d 10454#endif
1d7c1841 10455 }
662fb8b2
NC
10456 assert(new_body);
10457 SvANY(dstr) = new_body;
10458
10459 Copy(((char*)SvANY(sstr)) + new_body_offset,
10460 ((char*)SvANY(dstr)) + new_body_offset,
10461 new_body_length, char);
10462
10463 if (SvTYPE(sstr) != SVt_PVAV && SvTYPE(sstr) != SVt_PVHV)
10464 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
10465
10466 /* The Copy above means that all the source (unduplicated) pointers
10467 are now in the destination. We can check the flags and the
10468 pointers in either, but it's possible that there's less cache
10469 missing by always going for the destination.
10470 FIXME - instrument and check that assumption */
10471 if (SvTYPE(sstr) >= SVt_PVMG) {
10472 if (SvMAGIC(dstr))
10473 SvMAGIC_set(dstr, mg_dup(SvMAGIC(dstr), param));
10474 if (SvSTASH(dstr))
10475 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(dstr), param));
1d7c1841 10476 }
662fb8b2
NC
10477
10478 switch (SvTYPE(sstr)) {
10479 case SVt_PV:
10480 break;
10481 case SVt_PVIV:
10482 break;
10483 case SVt_PVNV:
10484 break;
10485 case SVt_PVMG:
10486 break;
10487 case SVt_PVBM:
10488 break;
10489 case SVt_PVLV:
10490 /* XXX LvTARGOFF sometimes holds PMOP* when DEBUGGING */
10491 if (LvTYPE(dstr) == 't') /* for tie: unrefcnted fake (SV**) */
10492 LvTARG(dstr) = dstr;
10493 else if (LvTYPE(dstr) == 'T') /* for tie: fake HE */
10494 LvTARG(dstr) = (SV*)he_dup((HE*)LvTARG(dstr), 0, param);
10495 else
10496 LvTARG(dstr) = sv_dup_inc(LvTARG(dstr), param);
10497 break;
10498 case SVt_PVGV:
10499 GvNAME(dstr) = SAVEPVN(GvNAME(dstr), GvNAMELEN(dstr));
e15faf7d
NC
10500 GvSTASH(dstr) = hv_dup(GvSTASH(dstr), param);
10501 /* Don't call sv_add_backref here as it's going to be created
10502 as part of the magic cloning of the symbol table. */
662fb8b2
NC
10503 GvGP(dstr) = gp_dup(GvGP(dstr), param);
10504 (void)GpREFCNT_inc(GvGP(dstr));
10505 break;
10506 case SVt_PVIO:
10507 IoIFP(dstr) = fp_dup(IoIFP(dstr), IoTYPE(dstr), param);
10508 if (IoOFP(dstr) == IoIFP(sstr))
10509 IoOFP(dstr) = IoIFP(dstr);
10510 else
10511 IoOFP(dstr) = fp_dup(IoOFP(dstr), IoTYPE(dstr), param);
10512 /* PL_rsfp_filters entries have fake IoDIRP() */
10513 if (IoDIRP(dstr) && !(IoFLAGS(dstr) & IOf_FAKE_DIRP))
10514 IoDIRP(dstr) = dirp_dup(IoDIRP(dstr));
10515 if(IoFLAGS(dstr) & IOf_FAKE_DIRP) {
10516 /* I have no idea why fake dirp (rsfps)
10517 should be treated differently but otherwise
10518 we end up with leaks -- sky*/
10519 IoTOP_GV(dstr) = gv_dup_inc(IoTOP_GV(dstr), param);
10520 IoFMT_GV(dstr) = gv_dup_inc(IoFMT_GV(dstr), param);
10521 IoBOTTOM_GV(dstr) = gv_dup_inc(IoBOTTOM_GV(dstr), param);
10522 } else {
10523 IoTOP_GV(dstr) = gv_dup(IoTOP_GV(dstr), param);
10524 IoFMT_GV(dstr) = gv_dup(IoFMT_GV(dstr), param);
10525 IoBOTTOM_GV(dstr) = gv_dup(IoBOTTOM_GV(dstr), param);
10526 }
10527 IoTOP_NAME(dstr) = SAVEPV(IoTOP_NAME(dstr));
10528 IoFMT_NAME(dstr) = SAVEPV(IoFMT_NAME(dstr));
10529 IoBOTTOM_NAME(dstr) = SAVEPV(IoBOTTOM_NAME(dstr));
10530 break;
10531 case SVt_PVAV:
10532 if (AvARRAY((AV*)sstr)) {
10533 SV **dst_ary, **src_ary;
10534 SSize_t items = AvFILLp((AV*)sstr) + 1;
10535
10536 src_ary = AvARRAY((AV*)sstr);
10537 Newz(0, dst_ary, AvMAX((AV*)sstr)+1, SV*);
10538 ptr_table_store(PL_ptr_table, src_ary, dst_ary);
10539 SvPV_set(dstr, (char*)dst_ary);
10540 AvALLOC((AV*)dstr) = dst_ary;
10541 if (AvREAL((AV*)sstr)) {
10542 while (items-- > 0)
10543 *dst_ary++ = sv_dup_inc(*src_ary++, param);
10544 }
10545 else {
10546 while (items-- > 0)
10547 *dst_ary++ = sv_dup(*src_ary++, param);
10548 }
10549 items = AvMAX((AV*)sstr) - AvFILLp((AV*)sstr);
10550 while (items-- > 0) {
10551 *dst_ary++ = &PL_sv_undef;
10552 }
bfcb3514 10553 }
662fb8b2
NC
10554 else {
10555 SvPV_set(dstr, Nullch);
10556 AvALLOC((AV*)dstr) = (SV**)NULL;
b79f7545 10557 }
662fb8b2
NC
10558 break;
10559 case SVt_PVHV:
10560 {
10561 HEK *hvname = 0;
10562
10563 if (HvARRAY((HV*)sstr)) {
10564 STRLEN i = 0;
10565 const bool sharekeys = !!HvSHAREKEYS(sstr);
10566 XPVHV * const dxhv = (XPVHV*)SvANY(dstr);
10567 XPVHV * const sxhv = (XPVHV*)SvANY(sstr);
10568 char *darray;
10569 New(0, darray,
10570 PERL_HV_ARRAY_ALLOC_BYTES(dxhv->xhv_max+1)
10571 + (SvOOK(sstr) ? sizeof(struct xpvhv_aux) : 0),
10572 char);
10573 HvARRAY(dstr) = (HE**)darray;
10574 while (i <= sxhv->xhv_max) {
10575 HE *source = HvARRAY(sstr)[i];
10576 HvARRAY(dstr)[i] = source
10577 ? he_dup(source, sharekeys, param) : 0;
10578 ++i;
10579 }
10580 if (SvOOK(sstr)) {
10581 struct xpvhv_aux *saux = HvAUX(sstr);
10582 struct xpvhv_aux *daux = HvAUX(dstr);
10583 /* This flag isn't copied. */
10584 /* SvOOK_on(hv) attacks the IV flags. */
10585 SvFLAGS(dstr) |= SVf_OOK;
10586
10587 hvname = saux->xhv_name;
dd690478
NC
10588 daux->xhv_name
10589 = hvname ? hek_dup(hvname, param) : hvname;
662fb8b2
NC
10590
10591 daux->xhv_riter = saux->xhv_riter;
10592 daux->xhv_eiter = saux->xhv_eiter
dd690478
NC
10593 ? he_dup(saux->xhv_eiter,
10594 (bool)!!HvSHAREKEYS(sstr), param) : 0;
662fb8b2
NC
10595 }
10596 }
10597 else {
10598 SvPV_set(dstr, Nullch);
10599 }
10600 /* Record stashes for possible cloning in Perl_clone(). */
10601 if(hvname)
10602 av_push(param->stashes, dstr);
10603 }
10604 break;
10605 case SVt_PVFM:
10606 case SVt_PVCV:
10607 /* NOTE: not refcounted */
10608 CvSTASH(dstr) = hv_dup(CvSTASH(dstr), param);
10609 OP_REFCNT_LOCK;
10610 CvROOT(dstr) = OpREFCNT_inc(CvROOT(dstr));
10611 OP_REFCNT_UNLOCK;
10612 if (CvCONST(dstr)) {
10613 CvXSUBANY(dstr).any_ptr = GvUNIQUE(CvGV(dstr)) ?
10614 SvREFCNT_inc(CvXSUBANY(dstr).any_ptr) :
10615 sv_dup_inc((SV *)CvXSUBANY(dstr).any_ptr, param);
10616 }
10617 /* don't dup if copying back - CvGV isn't refcounted, so the
10618 * duped GV may never be freed. A bit of a hack! DAPM */
10619 CvGV(dstr) = (param->flags & CLONEf_JOIN_IN) ?
10620 Nullgv : gv_dup(CvGV(dstr), param) ;
10621 if (!(param->flags & CLONEf_COPY_STACKS)) {
10622 CvDEPTH(dstr) = 0;
10623 }
10624 PAD_DUP(CvPADLIST(dstr), CvPADLIST(sstr), param);
10625 CvOUTSIDE(dstr) =
10626 CvWEAKOUTSIDE(sstr)
10627 ? cv_dup( CvOUTSIDE(dstr), param)
10628 : cv_dup_inc(CvOUTSIDE(dstr), param);
10629 if (!CvXSUB(dstr))
10630 CvFILE(dstr) = SAVEPV(CvFILE(dstr));
10631 break;
bfcb3514 10632 }
1d7c1841 10633 }
1d7c1841
GS
10634 }
10635
10636 if (SvOBJECT(dstr) && SvTYPE(dstr) != SVt_PVIO)
10637 ++PL_sv_objcount;
10638
10639 return dstr;
d2d73c3e 10640 }
1d7c1841 10641
645c22ef
DM
10642/* duplicate a context */
10643
1d7c1841 10644PERL_CONTEXT *
a8fc9800 10645Perl_cx_dup(pTHX_ PERL_CONTEXT *cxs, I32 ix, I32 max, CLONE_PARAMS* param)
1d7c1841
GS
10646{
10647 PERL_CONTEXT *ncxs;
10648
10649 if (!cxs)
10650 return (PERL_CONTEXT*)NULL;
10651
10652 /* look for it in the table first */
10653 ncxs = (PERL_CONTEXT*)ptr_table_fetch(PL_ptr_table, cxs);
10654 if (ncxs)
10655 return ncxs;
10656
10657 /* create anew and remember what it is */
10658 Newz(56, ncxs, max + 1, PERL_CONTEXT);
10659 ptr_table_store(PL_ptr_table, cxs, ncxs);
10660
10661 while (ix >= 0) {
10662 PERL_CONTEXT *cx = &cxs[ix];
10663 PERL_CONTEXT *ncx = &ncxs[ix];
10664 ncx->cx_type = cx->cx_type;
10665 if (CxTYPE(cx) == CXt_SUBST) {
10666 Perl_croak(aTHX_ "Cloning substitution context is unimplemented");
10667 }
10668 else {
10669 ncx->blk_oldsp = cx->blk_oldsp;
10670 ncx->blk_oldcop = cx->blk_oldcop;
1d7c1841
GS
10671 ncx->blk_oldmarksp = cx->blk_oldmarksp;
10672 ncx->blk_oldscopesp = cx->blk_oldscopesp;
10673 ncx->blk_oldpm = cx->blk_oldpm;
10674 ncx->blk_gimme = cx->blk_gimme;
10675 switch (CxTYPE(cx)) {
10676 case CXt_SUB:
10677 ncx->blk_sub.cv = (cx->blk_sub.olddepth == 0
d2d73c3e
AB
10678 ? cv_dup_inc(cx->blk_sub.cv, param)
10679 : cv_dup(cx->blk_sub.cv,param));
1d7c1841 10680 ncx->blk_sub.argarray = (cx->blk_sub.hasargs
d2d73c3e 10681 ? av_dup_inc(cx->blk_sub.argarray, param)
1d7c1841 10682 : Nullav);
d2d73c3e 10683 ncx->blk_sub.savearray = av_dup_inc(cx->blk_sub.savearray, param);
1d7c1841
GS
10684 ncx->blk_sub.olddepth = cx->blk_sub.olddepth;
10685 ncx->blk_sub.hasargs = cx->blk_sub.hasargs;
10686 ncx->blk_sub.lval = cx->blk_sub.lval;
f39bc417 10687 ncx->blk_sub.retop = cx->blk_sub.retop;
1d7c1841
GS
10688 break;
10689 case CXt_EVAL:
10690 ncx->blk_eval.old_in_eval = cx->blk_eval.old_in_eval;
10691 ncx->blk_eval.old_op_type = cx->blk_eval.old_op_type;
b47cad08 10692 ncx->blk_eval.old_namesv = sv_dup_inc(cx->blk_eval.old_namesv, param);
1d7c1841 10693 ncx->blk_eval.old_eval_root = cx->blk_eval.old_eval_root;
d2d73c3e 10694 ncx->blk_eval.cur_text = sv_dup(cx->blk_eval.cur_text, param);
f39bc417 10695 ncx->blk_eval.retop = cx->blk_eval.retop;
1d7c1841
GS
10696 break;
10697 case CXt_LOOP:
10698 ncx->blk_loop.label = cx->blk_loop.label;
10699 ncx->blk_loop.resetsp = cx->blk_loop.resetsp;
10700 ncx->blk_loop.redo_op = cx->blk_loop.redo_op;
10701 ncx->blk_loop.next_op = cx->blk_loop.next_op;
10702 ncx->blk_loop.last_op = cx->blk_loop.last_op;
10703 ncx->blk_loop.iterdata = (CxPADLOOP(cx)
10704 ? cx->blk_loop.iterdata
d2d73c3e 10705 : gv_dup((GV*)cx->blk_loop.iterdata, param));
f3548bdc
DM
10706 ncx->blk_loop.oldcomppad
10707 = (PAD*)ptr_table_fetch(PL_ptr_table,
10708 cx->blk_loop.oldcomppad);
d2d73c3e
AB
10709 ncx->blk_loop.itersave = sv_dup_inc(cx->blk_loop.itersave, param);
10710 ncx->blk_loop.iterlval = sv_dup_inc(cx->blk_loop.iterlval, param);
10711 ncx->blk_loop.iterary = av_dup_inc(cx->blk_loop.iterary, param);
1d7c1841
GS
10712 ncx->blk_loop.iterix = cx->blk_loop.iterix;
10713 ncx->blk_loop.itermax = cx->blk_loop.itermax;
10714 break;
10715 case CXt_FORMAT:
d2d73c3e
AB
10716 ncx->blk_sub.cv = cv_dup(cx->blk_sub.cv, param);
10717 ncx->blk_sub.gv = gv_dup(cx->blk_sub.gv, param);
10718 ncx->blk_sub.dfoutgv = gv_dup_inc(cx->blk_sub.dfoutgv, param);
1d7c1841 10719 ncx->blk_sub.hasargs = cx->blk_sub.hasargs;
f39bc417 10720 ncx->blk_sub.retop = cx->blk_sub.retop;
1d7c1841
GS
10721 break;
10722 case CXt_BLOCK:
10723 case CXt_NULL:
10724 break;
10725 }
10726 }
10727 --ix;
10728 }
10729 return ncxs;
10730}
10731
645c22ef
DM
10732/* duplicate a stack info structure */
10733
1d7c1841 10734PERL_SI *
a8fc9800 10735Perl_si_dup(pTHX_ PERL_SI *si, CLONE_PARAMS* param)
1d7c1841
GS
10736{
10737 PERL_SI *nsi;
10738
10739 if (!si)
10740 return (PERL_SI*)NULL;
10741
10742 /* look for it in the table first */
10743 nsi = (PERL_SI*)ptr_table_fetch(PL_ptr_table, si);
10744 if (nsi)
10745 return nsi;
10746
10747 /* create anew and remember what it is */
10748 Newz(56, nsi, 1, PERL_SI);
10749 ptr_table_store(PL_ptr_table, si, nsi);
10750
d2d73c3e 10751 nsi->si_stack = av_dup_inc(si->si_stack, param);
1d7c1841
GS
10752 nsi->si_cxix = si->si_cxix;
10753 nsi->si_cxmax = si->si_cxmax;
d2d73c3e 10754 nsi->si_cxstack = cx_dup(si->si_cxstack, si->si_cxix, si->si_cxmax, param);
1d7c1841 10755 nsi->si_type = si->si_type;
d2d73c3e
AB
10756 nsi->si_prev = si_dup(si->si_prev, param);
10757 nsi->si_next = si_dup(si->si_next, param);
1d7c1841
GS
10758 nsi->si_markoff = si->si_markoff;
10759
10760 return nsi;
10761}
10762
10763#define POPINT(ss,ix) ((ss)[--(ix)].any_i32)
10764#define TOPINT(ss,ix) ((ss)[ix].any_i32)
10765#define POPLONG(ss,ix) ((ss)[--(ix)].any_long)
10766#define TOPLONG(ss,ix) ((ss)[ix].any_long)
10767#define POPIV(ss,ix) ((ss)[--(ix)].any_iv)
10768#define TOPIV(ss,ix) ((ss)[ix].any_iv)
38d8b13e
HS
10769#define POPBOOL(ss,ix) ((ss)[--(ix)].any_bool)
10770#define TOPBOOL(ss,ix) ((ss)[ix].any_bool)
1d7c1841
GS
10771#define POPPTR(ss,ix) ((ss)[--(ix)].any_ptr)
10772#define TOPPTR(ss,ix) ((ss)[ix].any_ptr)
10773#define POPDPTR(ss,ix) ((ss)[--(ix)].any_dptr)
10774#define TOPDPTR(ss,ix) ((ss)[ix].any_dptr)
10775#define POPDXPTR(ss,ix) ((ss)[--(ix)].any_dxptr)
10776#define TOPDXPTR(ss,ix) ((ss)[ix].any_dxptr)
10777
10778/* XXXXX todo */
10779#define pv_dup_inc(p) SAVEPV(p)
10780#define pv_dup(p) SAVEPV(p)
10781#define svp_dup_inc(p,pp) any_dup(p,pp)
10782
645c22ef
DM
10783/* map any object to the new equivent - either something in the
10784 * ptr table, or something in the interpreter structure
10785 */
10786
1d7c1841 10787void *
53c1dcc0 10788Perl_any_dup(pTHX_ void *v, const PerlInterpreter *proto_perl)
1d7c1841
GS
10789{
10790 void *ret;
10791
10792 if (!v)
10793 return (void*)NULL;
10794
10795 /* look for it in the table first */
10796 ret = ptr_table_fetch(PL_ptr_table, v);
10797 if (ret)
10798 return ret;
10799
10800 /* see if it is part of the interpreter structure */
10801 if (v >= (void*)proto_perl && v < (void*)(proto_perl+1))
acfe0abc 10802 ret = (void*)(((char*)aTHX) + (((char*)v) - (char*)proto_perl));
05ec9bb3 10803 else {
1d7c1841 10804 ret = v;
05ec9bb3 10805 }
1d7c1841
GS
10806
10807 return ret;
10808}
10809
645c22ef
DM
10810/* duplicate the save stack */
10811
1d7c1841 10812ANY *
a8fc9800 10813Perl_ss_dup(pTHX_ PerlInterpreter *proto_perl, CLONE_PARAMS* param)
1d7c1841 10814{
53c1dcc0
AL
10815 ANY * const ss = proto_perl->Tsavestack;
10816 const I32 max = proto_perl->Tsavestack_max;
10817 I32 ix = proto_perl->Tsavestack_ix;
1d7c1841
GS
10818 ANY *nss;
10819 SV *sv;
10820 GV *gv;
10821 AV *av;
10822 HV *hv;
10823 void* ptr;
10824 int intval;
10825 long longval;
10826 GP *gp;
10827 IV iv;
c4e33207 10828 char *c = NULL;
1d7c1841 10829 void (*dptr) (void*);
acfe0abc 10830 void (*dxptr) (pTHX_ void*);
1d7c1841
GS
10831
10832 Newz(54, nss, max, ANY);
10833
10834 while (ix > 0) {
b464bac0 10835 I32 i = POPINT(ss,ix);
1d7c1841
GS
10836 TOPINT(nss,ix) = i;
10837 switch (i) {
10838 case SAVEt_ITEM: /* normal string */
10839 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 10840 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841 10841 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 10842 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841
GS
10843 break;
10844 case SAVEt_SV: /* scalar reference */
10845 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 10846 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841 10847 gv = (GV*)POPPTR(ss,ix);
d2d73c3e 10848 TOPPTR(nss,ix) = gv_dup_inc(gv, param);
1d7c1841 10849 break;
f4dd75d9
GS
10850 case SAVEt_GENERIC_PVREF: /* generic char* */
10851 c = (char*)POPPTR(ss,ix);
10852 TOPPTR(nss,ix) = pv_dup(c);
10853 ptr = POPPTR(ss,ix);
10854 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10855 break;
05ec9bb3
NIS
10856 case SAVEt_SHARED_PVREF: /* char* in shared space */
10857 c = (char*)POPPTR(ss,ix);
10858 TOPPTR(nss,ix) = savesharedpv(c);
10859 ptr = POPPTR(ss,ix);
10860 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10861 break;
1d7c1841
GS
10862 case SAVEt_GENERIC_SVREF: /* generic sv */
10863 case SAVEt_SVREF: /* scalar reference */
10864 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 10865 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841
GS
10866 ptr = POPPTR(ss,ix);
10867 TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */
10868 break;
10869 case SAVEt_AV: /* array reference */
10870 av = (AV*)POPPTR(ss,ix);
d2d73c3e 10871 TOPPTR(nss,ix) = av_dup_inc(av, param);
1d7c1841 10872 gv = (GV*)POPPTR(ss,ix);
d2d73c3e 10873 TOPPTR(nss,ix) = gv_dup(gv, param);
1d7c1841
GS
10874 break;
10875 case SAVEt_HV: /* hash reference */
10876 hv = (HV*)POPPTR(ss,ix);
d2d73c3e 10877 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
1d7c1841 10878 gv = (GV*)POPPTR(ss,ix);
d2d73c3e 10879 TOPPTR(nss,ix) = gv_dup(gv, param);
1d7c1841
GS
10880 break;
10881 case SAVEt_INT: /* int reference */
10882 ptr = POPPTR(ss,ix);
10883 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10884 intval = (int)POPINT(ss,ix);
10885 TOPINT(nss,ix) = intval;
10886 break;
10887 case SAVEt_LONG: /* long reference */
10888 ptr = POPPTR(ss,ix);
10889 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10890 longval = (long)POPLONG(ss,ix);
10891 TOPLONG(nss,ix) = longval;
10892 break;
10893 case SAVEt_I32: /* I32 reference */
10894 case SAVEt_I16: /* I16 reference */
10895 case SAVEt_I8: /* I8 reference */
10896 ptr = POPPTR(ss,ix);
10897 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10898 i = POPINT(ss,ix);
10899 TOPINT(nss,ix) = i;
10900 break;
10901 case SAVEt_IV: /* IV reference */
10902 ptr = POPPTR(ss,ix);
10903 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10904 iv = POPIV(ss,ix);
10905 TOPIV(nss,ix) = iv;
10906 break;
10907 case SAVEt_SPTR: /* SV* reference */
10908 ptr = POPPTR(ss,ix);
10909 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10910 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 10911 TOPPTR(nss,ix) = sv_dup(sv, param);
1d7c1841
GS
10912 break;
10913 case SAVEt_VPTR: /* random* reference */
10914 ptr = POPPTR(ss,ix);
10915 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10916 ptr = POPPTR(ss,ix);
10917 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10918 break;
10919 case SAVEt_PPTR: /* char* reference */
10920 ptr = POPPTR(ss,ix);
10921 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10922 c = (char*)POPPTR(ss,ix);
10923 TOPPTR(nss,ix) = pv_dup(c);
10924 break;
10925 case SAVEt_HPTR: /* HV* reference */
10926 ptr = POPPTR(ss,ix);
10927 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10928 hv = (HV*)POPPTR(ss,ix);
d2d73c3e 10929 TOPPTR(nss,ix) = hv_dup(hv, param);
1d7c1841
GS
10930 break;
10931 case SAVEt_APTR: /* AV* reference */
10932 ptr = POPPTR(ss,ix);
10933 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10934 av = (AV*)POPPTR(ss,ix);
d2d73c3e 10935 TOPPTR(nss,ix) = av_dup(av, param);
1d7c1841
GS
10936 break;
10937 case SAVEt_NSTAB:
10938 gv = (GV*)POPPTR(ss,ix);
d2d73c3e 10939 TOPPTR(nss,ix) = gv_dup(gv, param);
1d7c1841
GS
10940 break;
10941 case SAVEt_GP: /* scalar reference */
10942 gp = (GP*)POPPTR(ss,ix);
d2d73c3e 10943 TOPPTR(nss,ix) = gp = gp_dup(gp, param);
1d7c1841
GS
10944 (void)GpREFCNT_inc(gp);
10945 gv = (GV*)POPPTR(ss,ix);
2ed3c8fc 10946 TOPPTR(nss,ix) = gv_dup_inc(gv, param);
1d7c1841
GS
10947 c = (char*)POPPTR(ss,ix);
10948 TOPPTR(nss,ix) = pv_dup(c);
10949 iv = POPIV(ss,ix);
10950 TOPIV(nss,ix) = iv;
10951 iv = POPIV(ss,ix);
10952 TOPIV(nss,ix) = iv;
10953 break;
10954 case SAVEt_FREESV:
26d9b02f 10955 case SAVEt_MORTALIZESV:
1d7c1841 10956 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 10957 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841
GS
10958 break;
10959 case SAVEt_FREEOP:
10960 ptr = POPPTR(ss,ix);
10961 if (ptr && (((OP*)ptr)->op_private & OPpREFCOUNTED)) {
10962 /* these are assumed to be refcounted properly */
53c1dcc0 10963 OP *o;
1d7c1841
GS
10964 switch (((OP*)ptr)->op_type) {
10965 case OP_LEAVESUB:
10966 case OP_LEAVESUBLV:
10967 case OP_LEAVEEVAL:
10968 case OP_LEAVE:
10969 case OP_SCOPE:
10970 case OP_LEAVEWRITE:
e977893f
GS
10971 TOPPTR(nss,ix) = ptr;
10972 o = (OP*)ptr;
10973 OpREFCNT_inc(o);
1d7c1841
GS
10974 break;
10975 default:
10976 TOPPTR(nss,ix) = Nullop;
10977 break;
10978 }
10979 }
10980 else
10981 TOPPTR(nss,ix) = Nullop;
10982 break;
10983 case SAVEt_FREEPV:
10984 c = (char*)POPPTR(ss,ix);
10985 TOPPTR(nss,ix) = pv_dup_inc(c);
10986 break;
10987 case SAVEt_CLEARSV:
10988 longval = POPLONG(ss,ix);
10989 TOPLONG(nss,ix) = longval;
10990 break;
10991 case SAVEt_DELETE:
10992 hv = (HV*)POPPTR(ss,ix);
d2d73c3e 10993 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
1d7c1841
GS
10994 c = (char*)POPPTR(ss,ix);
10995 TOPPTR(nss,ix) = pv_dup_inc(c);
10996 i = POPINT(ss,ix);
10997 TOPINT(nss,ix) = i;
10998 break;
10999 case SAVEt_DESTRUCTOR:
11000 ptr = POPPTR(ss,ix);
11001 TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */
11002 dptr = POPDPTR(ss,ix);
8141890a
JH
11003 TOPDPTR(nss,ix) = DPTR2FPTR(void (*)(void*),
11004 any_dup(FPTR2DPTR(void *, dptr),
11005 proto_perl));
1d7c1841
GS
11006 break;
11007 case SAVEt_DESTRUCTOR_X:
11008 ptr = POPPTR(ss,ix);
11009 TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */
11010 dxptr = POPDXPTR(ss,ix);
8141890a
JH
11011 TOPDXPTR(nss,ix) = DPTR2FPTR(void (*)(pTHX_ void*),
11012 any_dup(FPTR2DPTR(void *, dxptr),
11013 proto_perl));
1d7c1841
GS
11014 break;
11015 case SAVEt_REGCONTEXT:
11016 case SAVEt_ALLOC:
11017 i = POPINT(ss,ix);
11018 TOPINT(nss,ix) = i;
11019 ix -= i;
11020 break;
11021 case SAVEt_STACK_POS: /* Position on Perl stack */
11022 i = POPINT(ss,ix);
11023 TOPINT(nss,ix) = i;
11024 break;
11025 case SAVEt_AELEM: /* array element */
11026 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 11027 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841
GS
11028 i = POPINT(ss,ix);
11029 TOPINT(nss,ix) = i;
11030 av = (AV*)POPPTR(ss,ix);
d2d73c3e 11031 TOPPTR(nss,ix) = av_dup_inc(av, param);
1d7c1841
GS
11032 break;
11033 case SAVEt_HELEM: /* hash element */
11034 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 11035 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841 11036 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 11037 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841 11038 hv = (HV*)POPPTR(ss,ix);
d2d73c3e 11039 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
1d7c1841
GS
11040 break;
11041 case SAVEt_OP:
11042 ptr = POPPTR(ss,ix);
11043 TOPPTR(nss,ix) = ptr;
11044 break;
11045 case SAVEt_HINTS:
11046 i = POPINT(ss,ix);
11047 TOPINT(nss,ix) = i;
11048 break;
c4410b1b
GS
11049 case SAVEt_COMPPAD:
11050 av = (AV*)POPPTR(ss,ix);
58ed4fbe 11051 TOPPTR(nss,ix) = av_dup(av, param);
c4410b1b 11052 break;
c3564e5c
GS
11053 case SAVEt_PADSV:
11054 longval = (long)POPLONG(ss,ix);
11055 TOPLONG(nss,ix) = longval;
11056 ptr = POPPTR(ss,ix);
11057 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11058 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 11059 TOPPTR(nss,ix) = sv_dup(sv, param);
c3564e5c 11060 break;
a1bb4754 11061 case SAVEt_BOOL:
38d8b13e 11062 ptr = POPPTR(ss,ix);
b9609c01 11063 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
38d8b13e 11064 longval = (long)POPBOOL(ss,ix);
b9609c01 11065 TOPBOOL(nss,ix) = (bool)longval;
a1bb4754 11066 break;
8bd2680e
MHM
11067 case SAVEt_SET_SVFLAGS:
11068 i = POPINT(ss,ix);
11069 TOPINT(nss,ix) = i;
11070 i = POPINT(ss,ix);
11071 TOPINT(nss,ix) = i;
11072 sv = (SV*)POPPTR(ss,ix);
11073 TOPPTR(nss,ix) = sv_dup(sv, param);
11074 break;
1d7c1841
GS
11075 default:
11076 Perl_croak(aTHX_ "panic: ss_dup inconsistency");
11077 }
11078 }
11079
11080 return nss;
11081}
11082
9660f481
DM
11083
11084/* if sv is a stash, call $class->CLONE_SKIP(), and set the SVphv_CLONEABLE
11085 * flag to the result. This is done for each stash before cloning starts,
11086 * so we know which stashes want their objects cloned */
11087
11088static void
11089do_mark_cloneable_stash(pTHX_ SV *sv)
11090{
53c1dcc0 11091 const HEK * const hvname = HvNAME_HEK((HV*)sv);
bfcb3514 11092 if (hvname) {
53c1dcc0 11093 GV* const cloner = gv_fetchmethod_autoload((HV*)sv, "CLONE_SKIP", 0);
9660f481
DM
11094 SvFLAGS(sv) |= SVphv_CLONEABLE; /* clone objects by default */
11095 if (cloner && GvCV(cloner)) {
11096 dSP;
11097 UV status;
11098
11099 ENTER;
11100 SAVETMPS;
11101 PUSHMARK(SP);
84bda14a 11102 XPUSHs(sv_2mortal(newSVhek(hvname)));
9660f481
DM
11103 PUTBACK;
11104 call_sv((SV*)GvCV(cloner), G_SCALAR);
11105 SPAGAIN;
11106 status = POPu;
11107 PUTBACK;
11108 FREETMPS;
11109 LEAVE;
11110 if (status)
11111 SvFLAGS(sv) &= ~SVphv_CLONEABLE;
11112 }
11113 }
11114}
11115
11116
11117
645c22ef
DM
11118/*
11119=for apidoc perl_clone
11120
11121Create and return a new interpreter by cloning the current one.
11122
4be49ee6 11123perl_clone takes these flags as parameters:
6a78b4db 11124
7a5fa8a2
NIS
11125CLONEf_COPY_STACKS - is used to, well, copy the stacks also,
11126without it we only clone the data and zero the stacks,
11127with it we copy the stacks and the new perl interpreter is
11128ready to run at the exact same point as the previous one.
11129The pseudo-fork code uses COPY_STACKS while the
6a78b4db
AB
11130threads->new doesn't.
11131
11132CLONEf_KEEP_PTR_TABLE
7a5fa8a2
NIS
11133perl_clone keeps a ptr_table with the pointer of the old
11134variable as a key and the new variable as a value,
11135this allows it to check if something has been cloned and not
11136clone it again but rather just use the value and increase the
11137refcount. If KEEP_PTR_TABLE is not set then perl_clone will kill
11138the ptr_table using the function
11139C<ptr_table_free(PL_ptr_table); PL_ptr_table = NULL;>,
11140reason to keep it around is if you want to dup some of your own
11141variable who are outside the graph perl scans, example of this
6a78b4db
AB
11142code is in threads.xs create
11143
11144CLONEf_CLONE_HOST
7a5fa8a2
NIS
11145This is a win32 thing, it is ignored on unix, it tells perls
11146win32host code (which is c++) to clone itself, this is needed on
11147win32 if you want to run two threads at the same time,
11148if you just want to do some stuff in a separate perl interpreter
11149and then throw it away and return to the original one,
6a78b4db
AB
11150you don't need to do anything.
11151
645c22ef
DM
11152=cut
11153*/
11154
11155/* XXX the above needs expanding by someone who actually understands it ! */
3fc56081
NK
11156EXTERN_C PerlInterpreter *
11157perl_clone_host(PerlInterpreter* proto_perl, UV flags);
645c22ef 11158
1d7c1841
GS
11159PerlInterpreter *
11160perl_clone(PerlInterpreter *proto_perl, UV flags)
11161{
27da23d5 11162 dVAR;
1d7c1841 11163#ifdef PERL_IMPLICIT_SYS
c43294b8
AB
11164
11165 /* perlhost.h so we need to call into it
11166 to clone the host, CPerlHost should have a c interface, sky */
11167
11168 if (flags & CLONEf_CLONE_HOST) {
11169 return perl_clone_host(proto_perl,flags);
11170 }
11171 return perl_clone_using(proto_perl, flags,
1d7c1841
GS
11172 proto_perl->IMem,
11173 proto_perl->IMemShared,
11174 proto_perl->IMemParse,
11175 proto_perl->IEnv,
11176 proto_perl->IStdIO,
11177 proto_perl->ILIO,
11178 proto_perl->IDir,
11179 proto_perl->ISock,
11180 proto_perl->IProc);
11181}
11182
11183PerlInterpreter *
11184perl_clone_using(PerlInterpreter *proto_perl, UV flags,
11185 struct IPerlMem* ipM, struct IPerlMem* ipMS,
11186 struct IPerlMem* ipMP, struct IPerlEnv* ipE,
11187 struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO,
11188 struct IPerlDir* ipD, struct IPerlSock* ipS,
11189 struct IPerlProc* ipP)
11190{
11191 /* XXX many of the string copies here can be optimized if they're
11192 * constants; they need to be allocated as common memory and just
11193 * their pointers copied. */
11194
8fc9efbd 11195 IV i;
64aa0685
GS
11196 CLONE_PARAMS clone_params;
11197 CLONE_PARAMS* param = &clone_params;
d2d73c3e 11198
1d7c1841 11199 PerlInterpreter *my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter));
9660f481
DM
11200 /* for each stash, determine whether its objects should be cloned */
11201 S_visit(proto_perl, do_mark_cloneable_stash, SVt_PVHV, SVTYPEMASK);
ba869deb 11202 PERL_SET_THX(my_perl);
1d7c1841 11203
acfe0abc 11204# ifdef DEBUGGING
a4530404 11205 Poison(my_perl, 1, PerlInterpreter);
fd0854ff 11206 PL_op = Nullop;
c008732b 11207 PL_curcop = (COP *)Nullop;
1d7c1841
GS
11208 PL_markstack = 0;
11209 PL_scopestack = 0;
11210 PL_savestack = 0;
22f7c9c9
JH
11211 PL_savestack_ix = 0;
11212 PL_savestack_max = -1;
66fe0623 11213 PL_sig_pending = 0;
25596c82 11214 Zero(&PL_debug_pad, 1, struct perl_debug_pad);
acfe0abc 11215# else /* !DEBUGGING */
1d7c1841 11216 Zero(my_perl, 1, PerlInterpreter);
acfe0abc 11217# endif /* DEBUGGING */
1d7c1841
GS
11218
11219 /* host pointers */
11220 PL_Mem = ipM;
11221 PL_MemShared = ipMS;
11222 PL_MemParse = ipMP;
11223 PL_Env = ipE;
11224 PL_StdIO = ipStd;
11225 PL_LIO = ipLIO;
11226 PL_Dir = ipD;
11227 PL_Sock = ipS;
11228 PL_Proc = ipP;
1d7c1841
GS
11229#else /* !PERL_IMPLICIT_SYS */
11230 IV i;
64aa0685
GS
11231 CLONE_PARAMS clone_params;
11232 CLONE_PARAMS* param = &clone_params;
1d7c1841 11233 PerlInterpreter *my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter));
9660f481
DM
11234 /* for each stash, determine whether its objects should be cloned */
11235 S_visit(proto_perl, do_mark_cloneable_stash, SVt_PVHV, SVTYPEMASK);
ba869deb 11236 PERL_SET_THX(my_perl);
1d7c1841
GS
11237
11238# ifdef DEBUGGING
a4530404 11239 Poison(my_perl, 1, PerlInterpreter);
fd0854ff 11240 PL_op = Nullop;
c008732b 11241 PL_curcop = (COP *)Nullop;
1d7c1841
GS
11242 PL_markstack = 0;
11243 PL_scopestack = 0;
11244 PL_savestack = 0;
22f7c9c9
JH
11245 PL_savestack_ix = 0;
11246 PL_savestack_max = -1;
66fe0623 11247 PL_sig_pending = 0;
25596c82 11248 Zero(&PL_debug_pad, 1, struct perl_debug_pad);
1d7c1841
GS
11249# else /* !DEBUGGING */
11250 Zero(my_perl, 1, PerlInterpreter);
11251# endif /* DEBUGGING */
11252#endif /* PERL_IMPLICIT_SYS */
83236556 11253 param->flags = flags;
59b40662 11254 param->proto_perl = proto_perl;
1d7c1841
GS
11255
11256 /* arena roots */
612f20c3 11257 PL_xnv_arenaroot = NULL;
1d7c1841 11258 PL_xnv_root = NULL;
612f20c3 11259 PL_xpv_arenaroot = NULL;
1d7c1841 11260 PL_xpv_root = NULL;
612f20c3 11261 PL_xpviv_arenaroot = NULL;
1d7c1841 11262 PL_xpviv_root = NULL;
612f20c3 11263 PL_xpvnv_arenaroot = NULL;
1d7c1841 11264 PL_xpvnv_root = NULL;
612f20c3 11265 PL_xpvcv_arenaroot = NULL;
1d7c1841 11266 PL_xpvcv_root = NULL;
612f20c3 11267 PL_xpvav_arenaroot = NULL;
1d7c1841 11268 PL_xpvav_root = NULL;
612f20c3 11269 PL_xpvhv_arenaroot = NULL;
1d7c1841 11270 PL_xpvhv_root = NULL;
612f20c3 11271 PL_xpvmg_arenaroot = NULL;
1d7c1841 11272 PL_xpvmg_root = NULL;
7552b40b
DM
11273 PL_xpvgv_arenaroot = NULL;
11274 PL_xpvgv_root = NULL;
612f20c3 11275 PL_xpvlv_arenaroot = NULL;
1d7c1841 11276 PL_xpvlv_root = NULL;
612f20c3 11277 PL_xpvbm_arenaroot = NULL;
1d7c1841 11278 PL_xpvbm_root = NULL;
612f20c3 11279 PL_he_arenaroot = NULL;
1d7c1841 11280 PL_he_root = NULL;
892b45be 11281#if defined(USE_ITHREADS)
32e691d0
NC
11282 PL_pte_arenaroot = NULL;
11283 PL_pte_root = NULL;
892b45be 11284#endif
1d7c1841
GS
11285 PL_nice_chunk = NULL;
11286 PL_nice_chunk_size = 0;
11287 PL_sv_count = 0;
11288 PL_sv_objcount = 0;
11289 PL_sv_root = Nullsv;
11290 PL_sv_arenaroot = Nullsv;
11291
11292 PL_debug = proto_perl->Idebug;
11293
8df990a8
NC
11294 PL_hash_seed = proto_perl->Ihash_seed;
11295 PL_rehash_seed = proto_perl->Irehash_seed;
11296
e5dd39fc 11297#ifdef USE_REENTRANT_API
68853529
SB
11298 /* XXX: things like -Dm will segfault here in perlio, but doing
11299 * PERL_SET_CONTEXT(proto_perl);
11300 * breaks too many other things
11301 */
59bd0823 11302 Perl_reentrant_init(aTHX);
e5dd39fc
AB
11303#endif
11304
1d7c1841
GS
11305 /* create SV map for pointer relocation */
11306 PL_ptr_table = ptr_table_new();
11307
11308 /* initialize these special pointers as early as possible */
11309 SvANY(&PL_sv_undef) = NULL;
11310 SvREFCNT(&PL_sv_undef) = (~(U32)0)/2;
11311 SvFLAGS(&PL_sv_undef) = SVf_READONLY|SVt_NULL;
11312 ptr_table_store(PL_ptr_table, &proto_perl->Isv_undef, &PL_sv_undef);
11313
1d7c1841 11314 SvANY(&PL_sv_no) = new_XPVNV();
1d7c1841 11315 SvREFCNT(&PL_sv_no) = (~(U32)0)/2;
0309f36e
NC
11316 SvFLAGS(&PL_sv_no) = SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
11317 |SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV;
f880fe2f 11318 SvPV_set(&PL_sv_no, SAVEPVN(PL_No, 0));
b162af07
SP
11319 SvCUR_set(&PL_sv_no, 0);
11320 SvLEN_set(&PL_sv_no, 1);
45977657 11321 SvIV_set(&PL_sv_no, 0);
9d6ce603 11322 SvNV_set(&PL_sv_no, 0);
1d7c1841
GS
11323 ptr_table_store(PL_ptr_table, &proto_perl->Isv_no, &PL_sv_no);
11324
1d7c1841 11325 SvANY(&PL_sv_yes) = new_XPVNV();
1d7c1841 11326 SvREFCNT(&PL_sv_yes) = (~(U32)0)/2;
0309f36e
NC
11327 SvFLAGS(&PL_sv_yes) = SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
11328 |SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV;
f880fe2f 11329 SvPV_set(&PL_sv_yes, SAVEPVN(PL_Yes, 1));
b162af07
SP
11330 SvCUR_set(&PL_sv_yes, 1);
11331 SvLEN_set(&PL_sv_yes, 2);
45977657 11332 SvIV_set(&PL_sv_yes, 1);
9d6ce603 11333 SvNV_set(&PL_sv_yes, 1);
1d7c1841
GS
11334 ptr_table_store(PL_ptr_table, &proto_perl->Isv_yes, &PL_sv_yes);
11335
05ec9bb3 11336 /* create (a non-shared!) shared string table */
1d7c1841
GS
11337 PL_strtab = newHV();
11338 HvSHAREKEYS_off(PL_strtab);
c4a9c09d 11339 hv_ksplit(PL_strtab, HvTOTALKEYS(proto_perl->Istrtab));
1d7c1841
GS
11340 ptr_table_store(PL_ptr_table, proto_perl->Istrtab, PL_strtab);
11341
05ec9bb3
NIS
11342 PL_compiling = proto_perl->Icompiling;
11343
11344 /* These two PVs will be free'd special way so must set them same way op.c does */
11345 PL_compiling.cop_stashpv = savesharedpv(PL_compiling.cop_stashpv);
11346 ptr_table_store(PL_ptr_table, proto_perl->Icompiling.cop_stashpv, PL_compiling.cop_stashpv);
11347
11348 PL_compiling.cop_file = savesharedpv(PL_compiling.cop_file);
11349 ptr_table_store(PL_ptr_table, proto_perl->Icompiling.cop_file, PL_compiling.cop_file);
11350
1d7c1841
GS
11351 ptr_table_store(PL_ptr_table, &proto_perl->Icompiling, &PL_compiling);
11352 if (!specialWARN(PL_compiling.cop_warnings))
d2d73c3e 11353 PL_compiling.cop_warnings = sv_dup_inc(PL_compiling.cop_warnings, param);
ac27b0f5 11354 if (!specialCopIO(PL_compiling.cop_io))
d2d73c3e 11355 PL_compiling.cop_io = sv_dup_inc(PL_compiling.cop_io, param);
1d7c1841
GS
11356 PL_curcop = (COP*)any_dup(proto_perl->Tcurcop, proto_perl);
11357
11358 /* pseudo environmental stuff */
11359 PL_origargc = proto_perl->Iorigargc;
e2975953 11360 PL_origargv = proto_perl->Iorigargv;
d2d73c3e 11361
d2d73c3e
AB
11362 param->stashes = newAV(); /* Setup array of objects to call clone on */
11363
a1ea730d 11364#ifdef PERLIO_LAYERS
3a1ee7e8
NIS
11365 /* Clone PerlIO tables as soon as we can handle general xx_dup() */
11366 PerlIO_clone(aTHX_ proto_perl, param);
a1ea730d 11367#endif
d2d73c3e
AB
11368
11369 PL_envgv = gv_dup(proto_perl->Ienvgv, param);
11370 PL_incgv = gv_dup(proto_perl->Iincgv, param);
11371 PL_hintgv = gv_dup(proto_perl->Ihintgv, param);
1d7c1841 11372 PL_origfilename = SAVEPV(proto_perl->Iorigfilename);
d2d73c3e
AB
11373 PL_diehook = sv_dup_inc(proto_perl->Idiehook, param);
11374 PL_warnhook = sv_dup_inc(proto_perl->Iwarnhook, param);
1d7c1841
GS
11375
11376 /* switches */
11377 PL_minus_c = proto_perl->Iminus_c;
d2d73c3e 11378 PL_patchlevel = sv_dup_inc(proto_perl->Ipatchlevel, param);
1d7c1841
GS
11379 PL_localpatches = proto_perl->Ilocalpatches;
11380 PL_splitstr = proto_perl->Isplitstr;
11381 PL_preprocess = proto_perl->Ipreprocess;
11382 PL_minus_n = proto_perl->Iminus_n;
11383 PL_minus_p = proto_perl->Iminus_p;
11384 PL_minus_l = proto_perl->Iminus_l;
11385 PL_minus_a = proto_perl->Iminus_a;
11386 PL_minus_F = proto_perl->Iminus_F;
11387 PL_doswitches = proto_perl->Idoswitches;
11388 PL_dowarn = proto_perl->Idowarn;
11389 PL_doextract = proto_perl->Idoextract;
11390 PL_sawampersand = proto_perl->Isawampersand;
11391 PL_unsafe = proto_perl->Iunsafe;
11392 PL_inplace = SAVEPV(proto_perl->Iinplace);
d2d73c3e 11393 PL_e_script = sv_dup_inc(proto_perl->Ie_script, param);
1d7c1841
GS
11394 PL_perldb = proto_perl->Iperldb;
11395 PL_perl_destruct_level = proto_perl->Iperl_destruct_level;
1cbb0781 11396 PL_exit_flags = proto_perl->Iexit_flags;
1d7c1841
GS
11397
11398 /* magical thingies */
11399 /* XXX time(&PL_basetime) when asked for? */
11400 PL_basetime = proto_perl->Ibasetime;
d2d73c3e 11401 PL_formfeed = sv_dup(proto_perl->Iformfeed, param);
1d7c1841
GS
11402
11403 PL_maxsysfd = proto_perl->Imaxsysfd;
11404 PL_multiline = proto_perl->Imultiline;
11405 PL_statusvalue = proto_perl->Istatusvalue;
11406#ifdef VMS
11407 PL_statusvalue_vms = proto_perl->Istatusvalue_vms;
11408#endif
0a378802 11409 PL_encoding = sv_dup(proto_perl->Iencoding, param);
1d7c1841 11410
4a4c6fe3 11411 sv_setpvn(PERL_DEBUG_PAD(0), "", 0); /* For regex debugging. */
1f483ca1
JH
11412 sv_setpvn(PERL_DEBUG_PAD(1), "", 0); /* ext/re needs these */
11413 sv_setpvn(PERL_DEBUG_PAD(2), "", 0); /* even without DEBUGGING. */
4a4c6fe3 11414
d2f185dc
AMS
11415 /* Clone the regex array */
11416 PL_regex_padav = newAV();
11417 {
a3b680e6 11418 const I32 len = av_len((AV*)proto_perl->Iregex_padav);
53c1dcc0 11419 SV** const regexen = AvARRAY((AV*)proto_perl->Iregex_padav);
b464bac0 11420 IV i;
0f95fc41
AB
11421 av_push(PL_regex_padav,
11422 sv_dup_inc(regexen[0],param));
11423 for(i = 1; i <= len; i++) {
11424 if(SvREPADTMP(regexen[i])) {
11425 av_push(PL_regex_padav, sv_dup_inc(regexen[i], param));
8cf8f3d1 11426 } else {
0f95fc41
AB
11427 av_push(PL_regex_padav,
11428 SvREFCNT_inc(
8cf8f3d1 11429 newSViv(PTR2IV(re_dup(INT2PTR(REGEXP *,
cbfa9890 11430 SvIVX(regexen[i])), param)))
0f95fc41
AB
11431 ));
11432 }
d2f185dc
AMS
11433 }
11434 }
11435 PL_regex_pad = AvARRAY(PL_regex_padav);
1fcf4c12 11436
1d7c1841 11437 /* shortcuts to various I/O objects */
d2d73c3e
AB
11438 PL_stdingv = gv_dup(proto_perl->Istdingv, param);
11439 PL_stderrgv = gv_dup(proto_perl->Istderrgv, param);
11440 PL_defgv = gv_dup(proto_perl->Idefgv, param);
11441 PL_argvgv = gv_dup(proto_perl->Iargvgv, param);
11442 PL_argvoutgv = gv_dup(proto_perl->Iargvoutgv, param);
11443 PL_argvout_stack = av_dup_inc(proto_perl->Iargvout_stack, param);
1d7c1841
GS
11444
11445 /* shortcuts to regexp stuff */
d2d73c3e 11446 PL_replgv = gv_dup(proto_perl->Ireplgv, param);
1d7c1841
GS
11447
11448 /* shortcuts to misc objects */
d2d73c3e 11449 PL_errgv = gv_dup(proto_perl->Ierrgv, param);
1d7c1841
GS
11450
11451 /* shortcuts to debugging objects */
d2d73c3e
AB
11452 PL_DBgv = gv_dup(proto_perl->IDBgv, param);
11453 PL_DBline = gv_dup(proto_perl->IDBline, param);
11454 PL_DBsub = gv_dup(proto_perl->IDBsub, param);
11455 PL_DBsingle = sv_dup(proto_perl->IDBsingle, param);
11456 PL_DBtrace = sv_dup(proto_perl->IDBtrace, param);
11457 PL_DBsignal = sv_dup(proto_perl->IDBsignal, param);
06492da6 11458 PL_DBassertion = sv_dup(proto_perl->IDBassertion, param);
d2d73c3e
AB
11459 PL_lineary = av_dup(proto_perl->Ilineary, param);
11460 PL_dbargs = av_dup(proto_perl->Idbargs, param);
1d7c1841
GS
11461
11462 /* symbol tables */
d2d73c3e
AB
11463 PL_defstash = hv_dup_inc(proto_perl->Tdefstash, param);
11464 PL_curstash = hv_dup(proto_perl->Tcurstash, param);
d2d73c3e
AB
11465 PL_debstash = hv_dup(proto_perl->Idebstash, param);
11466 PL_globalstash = hv_dup(proto_perl->Iglobalstash, param);
11467 PL_curstname = sv_dup_inc(proto_perl->Icurstname, param);
11468
11469 PL_beginav = av_dup_inc(proto_perl->Ibeginav, param);
ee1c5a4e 11470 PL_beginav_save = av_dup_inc(proto_perl->Ibeginav_save, param);
ece599bd 11471 PL_checkav_save = av_dup_inc(proto_perl->Icheckav_save, param);
d2d73c3e
AB
11472 PL_endav = av_dup_inc(proto_perl->Iendav, param);
11473 PL_checkav = av_dup_inc(proto_perl->Icheckav, param);
11474 PL_initav = av_dup_inc(proto_perl->Iinitav, param);
1d7c1841
GS
11475
11476 PL_sub_generation = proto_perl->Isub_generation;
11477
11478 /* funky return mechanisms */
11479 PL_forkprocess = proto_perl->Iforkprocess;
11480
11481 /* subprocess state */
d2d73c3e 11482 PL_fdpid = av_dup_inc(proto_perl->Ifdpid, param);
1d7c1841
GS
11483
11484 /* internal state */
11485 PL_tainting = proto_perl->Itainting;
7135f00b 11486 PL_taint_warn = proto_perl->Itaint_warn;
1d7c1841
GS
11487 PL_maxo = proto_perl->Imaxo;
11488 if (proto_perl->Iop_mask)
11489 PL_op_mask = SAVEPVN(proto_perl->Iop_mask, PL_maxo);
11490 else
11491 PL_op_mask = Nullch;
06492da6 11492 /* PL_asserting = proto_perl->Iasserting; */
1d7c1841
GS
11493
11494 /* current interpreter roots */
d2d73c3e 11495 PL_main_cv = cv_dup_inc(proto_perl->Imain_cv, param);
1d7c1841
GS
11496 PL_main_root = OpREFCNT_inc(proto_perl->Imain_root);
11497 PL_main_start = proto_perl->Imain_start;
e977893f 11498 PL_eval_root = proto_perl->Ieval_root;
1d7c1841
GS
11499 PL_eval_start = proto_perl->Ieval_start;
11500
11501 /* runtime control stuff */
11502 PL_curcopdb = (COP*)any_dup(proto_perl->Icurcopdb, proto_perl);
11503 PL_copline = proto_perl->Icopline;
11504
11505 PL_filemode = proto_perl->Ifilemode;
11506 PL_lastfd = proto_perl->Ilastfd;
11507 PL_oldname = proto_perl->Ioldname; /* XXX not quite right */
11508 PL_Argv = NULL;
11509 PL_Cmd = Nullch;
11510 PL_gensym = proto_perl->Igensym;
11511 PL_preambled = proto_perl->Ipreambled;
d2d73c3e 11512 PL_preambleav = av_dup_inc(proto_perl->Ipreambleav, param);
1d7c1841
GS
11513 PL_laststatval = proto_perl->Ilaststatval;
11514 PL_laststype = proto_perl->Ilaststype;
11515 PL_mess_sv = Nullsv;
11516
d2d73c3e 11517 PL_ors_sv = sv_dup_inc(proto_perl->Iors_sv, param);
1d7c1841
GS
11518
11519 /* interpreter atexit processing */
11520 PL_exitlistlen = proto_perl->Iexitlistlen;
11521 if (PL_exitlistlen) {
11522 New(0, PL_exitlist, PL_exitlistlen, PerlExitListEntry);
11523 Copy(proto_perl->Iexitlist, PL_exitlist, PL_exitlistlen, PerlExitListEntry);
11524 }
11525 else
11526 PL_exitlist = (PerlExitListEntry*)NULL;
d2d73c3e 11527 PL_modglobal = hv_dup_inc(proto_perl->Imodglobal, param);
19e8ce8e
AB
11528 PL_custom_op_names = hv_dup_inc(proto_perl->Icustom_op_names,param);
11529 PL_custom_op_descs = hv_dup_inc(proto_perl->Icustom_op_descs,param);
1d7c1841
GS
11530
11531 PL_profiledata = NULL;
a8fc9800 11532 PL_rsfp = fp_dup(proto_perl->Irsfp, '<', param);
1d7c1841 11533 /* PL_rsfp_filters entries have fake IoDIRP() */
d2d73c3e 11534 PL_rsfp_filters = av_dup_inc(proto_perl->Irsfp_filters, param);
1d7c1841 11535
d2d73c3e 11536 PL_compcv = cv_dup(proto_perl->Icompcv, param);
dd2155a4
DM
11537
11538 PAD_CLONE_VARS(proto_perl, param);
1d7c1841
GS
11539
11540#ifdef HAVE_INTERP_INTERN
11541 sys_intern_dup(&proto_perl->Isys_intern, &PL_sys_intern);
11542#endif
11543
11544 /* more statics moved here */
11545 PL_generation = proto_perl->Igeneration;
d2d73c3e 11546 PL_DBcv = cv_dup(proto_perl->IDBcv, param);
1d7c1841
GS
11547
11548 PL_in_clean_objs = proto_perl->Iin_clean_objs;
11549 PL_in_clean_all = proto_perl->Iin_clean_all;
11550
11551 PL_uid = proto_perl->Iuid;
11552 PL_euid = proto_perl->Ieuid;
11553 PL_gid = proto_perl->Igid;
11554 PL_egid = proto_perl->Iegid;
11555 PL_nomemok = proto_perl->Inomemok;
11556 PL_an = proto_perl->Ian;
1d7c1841
GS
11557 PL_evalseq = proto_perl->Ievalseq;
11558 PL_origenviron = proto_perl->Iorigenviron; /* XXX not quite right */
11559 PL_origalen = proto_perl->Iorigalen;
11560 PL_pidstatus = newHV(); /* XXX flag for cloning? */
11561 PL_osname = SAVEPV(proto_perl->Iosname);
5c728af0 11562 PL_sh_path_compat = proto_perl->Ish_path_compat; /* XXX never deallocated */
1d7c1841
GS
11563 PL_sighandlerp = proto_perl->Isighandlerp;
11564
11565
11566 PL_runops = proto_perl->Irunops;
11567
11568 Copy(proto_perl->Itokenbuf, PL_tokenbuf, 256, char);
11569
11570#ifdef CSH
11571 PL_cshlen = proto_perl->Icshlen;
74f1b2b8 11572 PL_cshname = proto_perl->Icshname; /* XXX never deallocated */
1d7c1841
GS
11573#endif
11574
11575 PL_lex_state = proto_perl->Ilex_state;
11576 PL_lex_defer = proto_perl->Ilex_defer;
11577 PL_lex_expect = proto_perl->Ilex_expect;
11578 PL_lex_formbrack = proto_perl->Ilex_formbrack;
11579 PL_lex_dojoin = proto_perl->Ilex_dojoin;
11580 PL_lex_starts = proto_perl->Ilex_starts;
d2d73c3e
AB
11581 PL_lex_stuff = sv_dup_inc(proto_perl->Ilex_stuff, param);
11582 PL_lex_repl = sv_dup_inc(proto_perl->Ilex_repl, param);
1d7c1841
GS
11583 PL_lex_op = proto_perl->Ilex_op;
11584 PL_lex_inpat = proto_perl->Ilex_inpat;
11585 PL_lex_inwhat = proto_perl->Ilex_inwhat;
11586 PL_lex_brackets = proto_perl->Ilex_brackets;
11587 i = (PL_lex_brackets < 120 ? 120 : PL_lex_brackets);
11588 PL_lex_brackstack = SAVEPVN(proto_perl->Ilex_brackstack,i);
11589 PL_lex_casemods = proto_perl->Ilex_casemods;
11590 i = (PL_lex_casemods < 12 ? 12 : PL_lex_casemods);
11591 PL_lex_casestack = SAVEPVN(proto_perl->Ilex_casestack,i);
11592
11593 Copy(proto_perl->Inextval, PL_nextval, 5, YYSTYPE);
11594 Copy(proto_perl->Inexttype, PL_nexttype, 5, I32);
11595 PL_nexttoke = proto_perl->Inexttoke;
11596
1d773130
TB
11597 /* XXX This is probably masking the deeper issue of why
11598 * SvANY(proto_perl->Ilinestr) can be NULL at this point. For test case:
11599 * http://archive.develooper.com/perl5-porters%40perl.org/msg83298.html
11600 * (A little debugging with a watchpoint on it may help.)
11601 */
389edf32
TB
11602 if (SvANY(proto_perl->Ilinestr)) {
11603 PL_linestr = sv_dup_inc(proto_perl->Ilinestr, param);
3f7c398e 11604 i = proto_perl->Ibufptr - SvPVX_const(proto_perl->Ilinestr);
389edf32 11605 PL_bufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
3f7c398e 11606 i = proto_perl->Ioldbufptr - SvPVX_const(proto_perl->Ilinestr);
389edf32 11607 PL_oldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
3f7c398e 11608 i = proto_perl->Ioldoldbufptr - SvPVX_const(proto_perl->Ilinestr);
389edf32 11609 PL_oldoldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
3f7c398e 11610 i = proto_perl->Ilinestart - SvPVX_const(proto_perl->Ilinestr);
389edf32
TB
11611 PL_linestart = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
11612 }
11613 else {
11614 PL_linestr = NEWSV(65,79);
11615 sv_upgrade(PL_linestr,SVt_PVIV);
11616 sv_setpvn(PL_linestr,"",0);
11617 PL_bufptr = PL_oldbufptr = PL_oldoldbufptr = PL_linestart = SvPVX(PL_linestr);
11618 }
1d7c1841 11619 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
1d7c1841
GS
11620 PL_pending_ident = proto_perl->Ipending_ident;
11621 PL_sublex_info = proto_perl->Isublex_info; /* XXX not quite right */
11622
11623 PL_expect = proto_perl->Iexpect;
11624
11625 PL_multi_start = proto_perl->Imulti_start;
11626 PL_multi_end = proto_perl->Imulti_end;
11627 PL_multi_open = proto_perl->Imulti_open;
11628 PL_multi_close = proto_perl->Imulti_close;
11629
11630 PL_error_count = proto_perl->Ierror_count;
11631 PL_subline = proto_perl->Isubline;
d2d73c3e 11632 PL_subname = sv_dup_inc(proto_perl->Isubname, param);
1d7c1841 11633
1d773130 11634 /* XXX See comment on SvANY(proto_perl->Ilinestr) above */
389edf32 11635 if (SvANY(proto_perl->Ilinestr)) {
3f7c398e 11636 i = proto_perl->Ilast_uni - SvPVX_const(proto_perl->Ilinestr);
389edf32 11637 PL_last_uni = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
3f7c398e 11638 i = proto_perl->Ilast_lop - SvPVX_const(proto_perl->Ilinestr);
389edf32
TB
11639 PL_last_lop = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
11640 PL_last_lop_op = proto_perl->Ilast_lop_op;
11641 }
11642 else {
11643 PL_last_uni = SvPVX(PL_linestr);
11644 PL_last_lop = SvPVX(PL_linestr);
11645 PL_last_lop_op = 0;
11646 }
1d7c1841 11647 PL_in_my = proto_perl->Iin_my;
d2d73c3e 11648 PL_in_my_stash = hv_dup(proto_perl->Iin_my_stash, param);
1d7c1841
GS
11649#ifdef FCRYPT
11650 PL_cryptseen = proto_perl->Icryptseen;
11651#endif
11652
11653 PL_hints = proto_perl->Ihints;
11654
11655 PL_amagic_generation = proto_perl->Iamagic_generation;
11656
11657#ifdef USE_LOCALE_COLLATE
11658 PL_collation_ix = proto_perl->Icollation_ix;
11659 PL_collation_name = SAVEPV(proto_perl->Icollation_name);
11660 PL_collation_standard = proto_perl->Icollation_standard;
11661 PL_collxfrm_base = proto_perl->Icollxfrm_base;
11662 PL_collxfrm_mult = proto_perl->Icollxfrm_mult;
11663#endif /* USE_LOCALE_COLLATE */
11664
11665#ifdef USE_LOCALE_NUMERIC
11666 PL_numeric_name = SAVEPV(proto_perl->Inumeric_name);
11667 PL_numeric_standard = proto_perl->Inumeric_standard;
11668 PL_numeric_local = proto_perl->Inumeric_local;
d2d73c3e 11669 PL_numeric_radix_sv = sv_dup_inc(proto_perl->Inumeric_radix_sv, param);
1d7c1841
GS
11670#endif /* !USE_LOCALE_NUMERIC */
11671
11672 /* utf8 character classes */
d2d73c3e
AB
11673 PL_utf8_alnum = sv_dup_inc(proto_perl->Iutf8_alnum, param);
11674 PL_utf8_alnumc = sv_dup_inc(proto_perl->Iutf8_alnumc, param);
11675 PL_utf8_ascii = sv_dup_inc(proto_perl->Iutf8_ascii, param);
11676 PL_utf8_alpha = sv_dup_inc(proto_perl->Iutf8_alpha, param);
11677 PL_utf8_space = sv_dup_inc(proto_perl->Iutf8_space, param);
11678 PL_utf8_cntrl = sv_dup_inc(proto_perl->Iutf8_cntrl, param);
11679 PL_utf8_graph = sv_dup_inc(proto_perl->Iutf8_graph, param);
11680 PL_utf8_digit = sv_dup_inc(proto_perl->Iutf8_digit, param);
11681 PL_utf8_upper = sv_dup_inc(proto_perl->Iutf8_upper, param);
11682 PL_utf8_lower = sv_dup_inc(proto_perl->Iutf8_lower, param);
11683 PL_utf8_print = sv_dup_inc(proto_perl->Iutf8_print, param);
11684 PL_utf8_punct = sv_dup_inc(proto_perl->Iutf8_punct, param);
11685 PL_utf8_xdigit = sv_dup_inc(proto_perl->Iutf8_xdigit, param);
11686 PL_utf8_mark = sv_dup_inc(proto_perl->Iutf8_mark, param);
11687 PL_utf8_toupper = sv_dup_inc(proto_perl->Iutf8_toupper, param);
11688 PL_utf8_totitle = sv_dup_inc(proto_perl->Iutf8_totitle, param);
11689 PL_utf8_tolower = sv_dup_inc(proto_perl->Iutf8_tolower, param);
b4e400f9 11690 PL_utf8_tofold = sv_dup_inc(proto_perl->Iutf8_tofold, param);
82686b01
JH
11691 PL_utf8_idstart = sv_dup_inc(proto_perl->Iutf8_idstart, param);
11692 PL_utf8_idcont = sv_dup_inc(proto_perl->Iutf8_idcont, param);
1d7c1841 11693
6c3182a5 11694 /* Did the locale setup indicate UTF-8? */
9769094f 11695 PL_utf8locale = proto_perl->Iutf8locale;
6c3182a5
JH
11696 /* Unicode features (see perlrun/-C) */
11697 PL_unicode = proto_perl->Iunicode;
11698
11699 /* Pre-5.8 signals control */
11700 PL_signals = proto_perl->Isignals;
11701
11702 /* times() ticks per second */
11703 PL_clocktick = proto_perl->Iclocktick;
11704
11705 /* Recursion stopper for PerlIO_find_layer */
11706 PL_in_load_module = proto_perl->Iin_load_module;
11707
11708 /* sort() routine */
11709 PL_sort_RealCmp = proto_perl->Isort_RealCmp;
11710
57c6e6d2
JH
11711 /* Not really needed/useful since the reenrant_retint is "volatile",
11712 * but do it for consistency's sake. */
11713 PL_reentrant_retint = proto_perl->Ireentrant_retint;
11714
15a5279a
JH
11715 /* Hooks to shared SVs and locks. */
11716 PL_sharehook = proto_perl->Isharehook;
11717 PL_lockhook = proto_perl->Ilockhook;
11718 PL_unlockhook = proto_perl->Iunlockhook;
11719 PL_threadhook = proto_perl->Ithreadhook;
11720
bce260cd
JH
11721 PL_runops_std = proto_perl->Irunops_std;
11722 PL_runops_dbg = proto_perl->Irunops_dbg;
11723
11724#ifdef THREADS_HAVE_PIDS
11725 PL_ppid = proto_perl->Ippid;
11726#endif
11727
1d7c1841
GS
11728 /* swatch cache */
11729 PL_last_swash_hv = Nullhv; /* reinits on demand */
11730 PL_last_swash_klen = 0;
11731 PL_last_swash_key[0]= '\0';
11732 PL_last_swash_tmps = (U8*)NULL;
11733 PL_last_swash_slen = 0;
11734
1d7c1841
GS
11735 PL_glob_index = proto_perl->Iglob_index;
11736 PL_srand_called = proto_perl->Isrand_called;
11737 PL_uudmap['M'] = 0; /* reinits on demand */
11738 PL_bitcount = Nullch; /* reinits on demand */
11739
66fe0623
NIS
11740 if (proto_perl->Ipsig_pend) {
11741 Newz(0, PL_psig_pend, SIG_SIZE, int);
9dd79c3f 11742 }
66fe0623
NIS
11743 else {
11744 PL_psig_pend = (int*)NULL;
11745 }
11746
1d7c1841 11747 if (proto_perl->Ipsig_ptr) {
76d3c696
JH
11748 Newz(0, PL_psig_ptr, SIG_SIZE, SV*);
11749 Newz(0, PL_psig_name, SIG_SIZE, SV*);
76d3c696 11750 for (i = 1; i < SIG_SIZE; i++) {
d2d73c3e
AB
11751 PL_psig_ptr[i] = sv_dup_inc(proto_perl->Ipsig_ptr[i], param);
11752 PL_psig_name[i] = sv_dup_inc(proto_perl->Ipsig_name[i], param);
1d7c1841
GS
11753 }
11754 }
11755 else {
11756 PL_psig_ptr = (SV**)NULL;
11757 PL_psig_name = (SV**)NULL;
11758 }
11759
11760 /* thrdvar.h stuff */
11761
a0739874 11762 if (flags & CLONEf_COPY_STACKS) {
1d7c1841
GS
11763 /* next allocation will be PL_tmps_stack[PL_tmps_ix+1] */
11764 PL_tmps_ix = proto_perl->Ttmps_ix;
11765 PL_tmps_max = proto_perl->Ttmps_max;
11766 PL_tmps_floor = proto_perl->Ttmps_floor;
11767 Newz(50, PL_tmps_stack, PL_tmps_max, SV*);
11768 i = 0;
11769 while (i <= PL_tmps_ix) {
d2d73c3e 11770 PL_tmps_stack[i] = sv_dup_inc(proto_perl->Ttmps_stack[i], param);
1d7c1841
GS
11771 ++i;
11772 }
11773
11774 /* next PUSHMARK() sets *(PL_markstack_ptr+1) */
11775 i = proto_perl->Tmarkstack_max - proto_perl->Tmarkstack;
11776 Newz(54, PL_markstack, i, I32);
11777 PL_markstack_max = PL_markstack + (proto_perl->Tmarkstack_max
11778 - proto_perl->Tmarkstack);
11779 PL_markstack_ptr = PL_markstack + (proto_perl->Tmarkstack_ptr
11780 - proto_perl->Tmarkstack);
11781 Copy(proto_perl->Tmarkstack, PL_markstack,
11782 PL_markstack_ptr - PL_markstack + 1, I32);
11783
11784 /* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix]
11785 * NOTE: unlike the others! */
11786 PL_scopestack_ix = proto_perl->Tscopestack_ix;
11787 PL_scopestack_max = proto_perl->Tscopestack_max;
11788 Newz(54, PL_scopestack, PL_scopestack_max, I32);
11789 Copy(proto_perl->Tscopestack, PL_scopestack, PL_scopestack_ix, I32);
11790
1d7c1841 11791 /* NOTE: si_dup() looks at PL_markstack */
d2d73c3e 11792 PL_curstackinfo = si_dup(proto_perl->Tcurstackinfo, param);
1d7c1841
GS
11793
11794 /* PL_curstack = PL_curstackinfo->si_stack; */
d2d73c3e
AB
11795 PL_curstack = av_dup(proto_perl->Tcurstack, param);
11796 PL_mainstack = av_dup(proto_perl->Tmainstack, param);
1d7c1841
GS
11797
11798 /* next PUSHs() etc. set *(PL_stack_sp+1) */
11799 PL_stack_base = AvARRAY(PL_curstack);
11800 PL_stack_sp = PL_stack_base + (proto_perl->Tstack_sp
11801 - proto_perl->Tstack_base);
11802 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
11803
11804 /* next SSPUSHFOO() sets PL_savestack[PL_savestack_ix]
11805 * NOTE: unlike the others! */
11806 PL_savestack_ix = proto_perl->Tsavestack_ix;
11807 PL_savestack_max = proto_perl->Tsavestack_max;
11808 /*Newz(54, PL_savestack, PL_savestack_max, ANY);*/
d2d73c3e 11809 PL_savestack = ss_dup(proto_perl, param);
1d7c1841
GS
11810 }
11811 else {
11812 init_stacks();
985e7056 11813 ENTER; /* perl_destruct() wants to LEAVE; */
1d7c1841
GS
11814 }
11815
11816 PL_start_env = proto_perl->Tstart_env; /* XXXXXX */
11817 PL_top_env = &PL_start_env;
11818
11819 PL_op = proto_perl->Top;
11820
11821 PL_Sv = Nullsv;
11822 PL_Xpv = (XPV*)NULL;
11823 PL_na = proto_perl->Tna;
11824
11825 PL_statbuf = proto_perl->Tstatbuf;
11826 PL_statcache = proto_perl->Tstatcache;
d2d73c3e
AB
11827 PL_statgv = gv_dup(proto_perl->Tstatgv, param);
11828 PL_statname = sv_dup_inc(proto_perl->Tstatname, param);
1d7c1841
GS
11829#ifdef HAS_TIMES
11830 PL_timesbuf = proto_perl->Ttimesbuf;
11831#endif
11832
11833 PL_tainted = proto_perl->Ttainted;
11834 PL_curpm = proto_perl->Tcurpm; /* XXX No PMOP ref count */
d2d73c3e
AB
11835 PL_rs = sv_dup_inc(proto_perl->Trs, param);
11836 PL_last_in_gv = gv_dup(proto_perl->Tlast_in_gv, param);
11837 PL_ofs_sv = sv_dup_inc(proto_perl->Tofs_sv, param);
11838 PL_defoutgv = gv_dup_inc(proto_perl->Tdefoutgv, param);
1d7c1841 11839 PL_chopset = proto_perl->Tchopset; /* XXX never deallocated */
d2d73c3e
AB
11840 PL_toptarget = sv_dup_inc(proto_perl->Ttoptarget, param);
11841 PL_bodytarget = sv_dup_inc(proto_perl->Tbodytarget, param);
11842 PL_formtarget = sv_dup(proto_perl->Tformtarget, param);
1d7c1841
GS
11843
11844 PL_restartop = proto_perl->Trestartop;
11845 PL_in_eval = proto_perl->Tin_eval;
11846 PL_delaymagic = proto_perl->Tdelaymagic;
11847 PL_dirty = proto_perl->Tdirty;
11848 PL_localizing = proto_perl->Tlocalizing;
11849
d2d73c3e 11850 PL_errors = sv_dup_inc(proto_perl->Terrors, param);
dd28f7bb 11851 PL_hv_fetch_ent_mh = Nullhe;
1d7c1841
GS
11852 PL_modcount = proto_perl->Tmodcount;
11853 PL_lastgotoprobe = Nullop;
11854 PL_dumpindent = proto_perl->Tdumpindent;
11855
11856 PL_sortcop = (OP*)any_dup(proto_perl->Tsortcop, proto_perl);
d2d73c3e
AB
11857 PL_sortstash = hv_dup(proto_perl->Tsortstash, param);
11858 PL_firstgv = gv_dup(proto_perl->Tfirstgv, param);
11859 PL_secondgv = gv_dup(proto_perl->Tsecondgv, param);
1d7c1841
GS
11860 PL_sortcxix = proto_perl->Tsortcxix;
11861 PL_efloatbuf = Nullch; /* reinits on demand */
11862 PL_efloatsize = 0; /* reinits on demand */
11863
11864 /* regex stuff */
11865
11866 PL_screamfirst = NULL;
11867 PL_screamnext = NULL;
11868 PL_maxscream = -1; /* reinits on demand */
11869 PL_lastscream = Nullsv;
11870
11871 PL_watchaddr = NULL;
11872 PL_watchok = Nullch;
11873
11874 PL_regdummy = proto_perl->Tregdummy;
1d7c1841
GS
11875 PL_regprecomp = Nullch;
11876 PL_regnpar = 0;
11877 PL_regsize = 0;
1d7c1841
GS
11878 PL_colorset = 0; /* reinits PL_colors[] */
11879 /*PL_colors[6] = {0,0,0,0,0,0};*/
1d7c1841
GS
11880 PL_reginput = Nullch;
11881 PL_regbol = Nullch;
11882 PL_regeol = Nullch;
11883 PL_regstartp = (I32*)NULL;
11884 PL_regendp = (I32*)NULL;
11885 PL_reglastparen = (U32*)NULL;
2d862feb 11886 PL_reglastcloseparen = (U32*)NULL;
1d7c1841 11887 PL_regtill = Nullch;
1d7c1841
GS
11888 PL_reg_start_tmp = (char**)NULL;
11889 PL_reg_start_tmpl = 0;
11890 PL_regdata = (struct reg_data*)NULL;
11891 PL_bostr = Nullch;
11892 PL_reg_flags = 0;
11893 PL_reg_eval_set = 0;
11894 PL_regnarrate = 0;
11895 PL_regprogram = (regnode*)NULL;
11896 PL_regindent = 0;
11897 PL_regcc = (CURCUR*)NULL;
11898 PL_reg_call_cc = (struct re_cc_state*)NULL;
11899 PL_reg_re = (regexp*)NULL;
11900 PL_reg_ganch = Nullch;
11901 PL_reg_sv = Nullsv;
53c4c00c 11902 PL_reg_match_utf8 = FALSE;
1d7c1841
GS
11903 PL_reg_magic = (MAGIC*)NULL;
11904 PL_reg_oldpos = 0;
11905 PL_reg_oldcurpm = (PMOP*)NULL;
11906 PL_reg_curpm = (PMOP*)NULL;
11907 PL_reg_oldsaved = Nullch;
11908 PL_reg_oldsavedlen = 0;
f8c7b90f 11909#ifdef PERL_OLD_COPY_ON_WRITE
504cff3b 11910 PL_nrs = Nullsv;
ed252734 11911#endif
1d7c1841
GS
11912 PL_reg_maxiter = 0;
11913 PL_reg_leftiter = 0;
11914 PL_reg_poscache = Nullch;
11915 PL_reg_poscache_size= 0;
11916
11917 /* RE engine - function pointers */
11918 PL_regcompp = proto_perl->Tregcompp;
11919 PL_regexecp = proto_perl->Tregexecp;
11920 PL_regint_start = proto_perl->Tregint_start;
11921 PL_regint_string = proto_perl->Tregint_string;
11922 PL_regfree = proto_perl->Tregfree;
11923
11924 PL_reginterp_cnt = 0;
11925 PL_reg_starttry = 0;
11926
a2efc822
SC
11927 /* Pluggable optimizer */
11928 PL_peepp = proto_perl->Tpeepp;
11929
081fc587
AB
11930 PL_stashcache = newHV();
11931
a0739874
DM
11932 if (!(flags & CLONEf_KEEP_PTR_TABLE)) {
11933 ptr_table_free(PL_ptr_table);
11934 PL_ptr_table = NULL;
11935 }
8cf8f3d1 11936
f284b03f
AMS
11937 /* Call the ->CLONE method, if it exists, for each of the stashes
11938 identified by sv_dup() above.
11939 */
d2d73c3e 11940 while(av_len(param->stashes) != -1) {
53c1dcc0
AL
11941 HV* const stash = (HV*) av_shift(param->stashes);
11942 GV* const cloner = gv_fetchmethod_autoload(stash, "CLONE", 0);
f284b03f
AMS
11943 if (cloner && GvCV(cloner)) {
11944 dSP;
11945 ENTER;
11946 SAVETMPS;
11947 PUSHMARK(SP);
84bda14a 11948 XPUSHs(sv_2mortal(newSVhek(HvNAME_HEK(stash))));
f284b03f
AMS
11949 PUTBACK;
11950 call_sv((SV*)GvCV(cloner), G_DISCARD);
11951 FREETMPS;
11952 LEAVE;
11953 }
4a09accc 11954 }
a0739874 11955
dc507217 11956 SvREFCNT_dec(param->stashes);
dc507217 11957
6d26897e
DM
11958 /* orphaned? eg threads->new inside BEGIN or use */
11959 if (PL_compcv && ! SvREFCNT(PL_compcv)) {
a3b680e6 11960 (void)SvREFCNT_inc(PL_compcv);
6d26897e
DM
11961 SAVEFREESV(PL_compcv);
11962 }
11963
1d7c1841 11964 return my_perl;
1d7c1841
GS
11965}
11966
1d7c1841 11967#endif /* USE_ITHREADS */
a0ae6670 11968
9f4817db 11969/*
ccfc67b7
JH
11970=head1 Unicode Support
11971
9f4817db
JH
11972=for apidoc sv_recode_to_utf8
11973
5d170f3a
JH
11974The encoding is assumed to be an Encode object, on entry the PV
11975of the sv is assumed to be octets in that encoding, and the sv
11976will be converted into Unicode (and UTF-8).
9f4817db 11977
5d170f3a
JH
11978If the sv already is UTF-8 (or if it is not POK), or if the encoding
11979is not a reference, nothing is done to the sv. If the encoding is not
1768d7eb
JH
11980an C<Encode::XS> Encoding object, bad things will happen.
11981(See F<lib/encoding.pm> and L<Encode>).
9f4817db 11982
5d170f3a 11983The PV of the sv is returned.
9f4817db 11984
5d170f3a
JH
11985=cut */
11986
11987char *
11988Perl_sv_recode_to_utf8(pTHX_ SV *sv, SV *encoding)
11989{
27da23d5 11990 dVAR;
220e2d4e 11991 if (SvPOK(sv) && !SvUTF8(sv) && !IN_BYTES && SvROK(encoding)) {
d0063567
DK
11992 SV *uni;
11993 STRLEN len;
93524f2b 11994 const char *s;
d0063567
DK
11995 dSP;
11996 ENTER;
11997 SAVETMPS;
220e2d4e 11998 save_re_context();
d0063567
DK
11999 PUSHMARK(sp);
12000 EXTEND(SP, 3);
12001 XPUSHs(encoding);
12002 XPUSHs(sv);
7a5fa8a2 12003/*
f9893866
NIS
12004 NI-S 2002/07/09
12005 Passing sv_yes is wrong - it needs to be or'ed set of constants
7a5fa8a2 12006 for Encode::XS, while UTf-8 decode (currently) assumes a true value means
f9893866
NIS
12007 remove converted chars from source.
12008
12009 Both will default the value - let them.
7a5fa8a2 12010
d0063567 12011 XPUSHs(&PL_sv_yes);
f9893866 12012*/
d0063567
DK
12013 PUTBACK;
12014 call_method("decode", G_SCALAR);
12015 SPAGAIN;
12016 uni = POPs;
12017 PUTBACK;
93524f2b 12018 s = SvPV_const(uni, len);
3f7c398e 12019 if (s != SvPVX_const(sv)) {
d0063567 12020 SvGROW(sv, len + 1);
93524f2b 12021 Move(s, SvPVX(sv), len + 1, char);
d0063567 12022 SvCUR_set(sv, len);
d0063567
DK
12023 }
12024 FREETMPS;
12025 LEAVE;
d0063567 12026 SvUTF8_on(sv);
95899a2a 12027 return SvPVX(sv);
f9893866 12028 }
95899a2a 12029 return SvPOKp(sv) ? SvPVX(sv) : NULL;
9f4817db
JH
12030}
12031
220e2d4e
IH
12032/*
12033=for apidoc sv_cat_decode
12034
12035The encoding is assumed to be an Encode object, the PV of the ssv is
12036assumed to be octets in that encoding and decoding the input starts
12037from the position which (PV + *offset) pointed to. The dsv will be
12038concatenated the decoded UTF-8 string from ssv. Decoding will terminate
12039when the string tstr appears in decoding output or the input ends on
12040the PV of the ssv. The value which the offset points will be modified
12041to the last input position on the ssv.
68795e93 12042
220e2d4e
IH
12043Returns TRUE if the terminator was found, else returns FALSE.
12044
12045=cut */
12046
12047bool
12048Perl_sv_cat_decode(pTHX_ SV *dsv, SV *encoding,
12049 SV *ssv, int *offset, char *tstr, int tlen)
12050{
27da23d5 12051 dVAR;
a73e8557 12052 bool ret = FALSE;
220e2d4e 12053 if (SvPOK(ssv) && SvPOK(dsv) && SvROK(encoding) && offset) {
220e2d4e
IH
12054 SV *offsv;
12055 dSP;
12056 ENTER;
12057 SAVETMPS;
12058 save_re_context();
12059 PUSHMARK(sp);
12060 EXTEND(SP, 6);
12061 XPUSHs(encoding);
12062 XPUSHs(dsv);
12063 XPUSHs(ssv);
12064 XPUSHs(offsv = sv_2mortal(newSViv(*offset)));
12065 XPUSHs(sv_2mortal(newSVpvn(tstr, tlen)));
12066 PUTBACK;
12067 call_method("cat_decode", G_SCALAR);
12068 SPAGAIN;
12069 ret = SvTRUE(TOPs);
12070 *offset = SvIV(offsv);
12071 PUTBACK;
12072 FREETMPS;
12073 LEAVE;
220e2d4e 12074 }
a73e8557
JH
12075 else
12076 Perl_croak(aTHX_ "Invalid argument to sv_cat_decode");
12077 return ret;
220e2d4e 12078}
f9893866 12079
241d1a3b
NC
12080/*
12081 * Local variables:
12082 * c-indentation-style: bsd
12083 * c-basic-offset: 4
12084 * indent-tabs-mode: t
12085 * End:
12086 *
37442d52
RGS
12087 * ex: set ts=8 sts=4 sw=4 noet:
12088 */