Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | /* sv.c |
79072805 | 2 | * |
1129b882 | 3 | * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, |
83706693 RGS |
4 | * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 by Larry Wall |
5 | * and others | |
79072805 LW |
6 | * |
7 | * You may distribute under the terms of either the GNU General Public | |
8 | * License or the Artistic License, as specified in the README file. | |
9 | * | |
4ac71550 TC |
10 | */ |
11 | ||
12 | /* | |
13 | * 'I wonder what the Entish is for "yes" and "no",' he thought. | |
14 | * --Pippin | |
15 | * | |
16 | * [p.480 of _The Lord of the Rings_, III/iv: "Treebeard"] | |
17 | */ | |
18 | ||
19 | /* | |
645c22ef DM |
20 | * |
21 | * | |
5e045b90 AMS |
22 | * This file contains the code that creates, manipulates and destroys |
23 | * scalar values (SVs). The other types (AV, HV, GV, etc.) reuse the | |
24 | * structure of an SV, so their creation and destruction is handled | |
25 | * here; higher-level functions are in av.c, hv.c, and so on. Opcode | |
26 | * level functions (eg. substr, split, join) for each of the types are | |
27 | * in the pp*.c files. | |
79072805 LW |
28 | */ |
29 | ||
30 | #include "EXTERN.h" | |
864dbfa3 | 31 | #define PERL_IN_SV_C |
79072805 | 32 | #include "perl.h" |
d2f185dc | 33 | #include "regcomp.h" |
79072805 | 34 | |
07208e09 | 35 | #ifndef HAS_C99 |
4b2d6671 | 36 | # if __STDC_VERSION__ >= 199901L && !defined(VMS) |
07208e09 CS |
37 | # define HAS_C99 1 |
38 | # endif | |
39 | #endif | |
40 | #if HAS_C99 | |
41 | # include <stdint.h> | |
42 | #endif | |
43 | ||
51371543 | 44 | #define FCALL *f |
2c5424a7 | 45 | |
2f8ed50e OS |
46 | #ifdef __Lynx__ |
47 | /* Missing proto on LynxOS */ | |
48 | char *gconvert(double, int, int, char *); | |
49 | #endif | |
50 | ||
e23c8137 | 51 | #ifdef PERL_UTF8_CACHE_ASSERT |
ab455f60 | 52 | /* if adding more checks watch out for the following tests: |
e23c8137 JH |
53 | * t/op/index.t t/op/length.t t/op/pat.t t/op/substr.t |
54 | * lib/utf8.t lib/Unicode/Collate/t/index.t | |
55 | * --jhi | |
56 | */ | |
6f207bd3 | 57 | # define ASSERT_UTF8_CACHE(cache) \ |
ab455f60 NC |
58 | STMT_START { if (cache) { assert((cache)[0] <= (cache)[1]); \ |
59 | assert((cache)[2] <= (cache)[3]); \ | |
60 | assert((cache)[3] <= (cache)[1]);} \ | |
61 | } STMT_END | |
e23c8137 | 62 | #else |
6f207bd3 | 63 | # define ASSERT_UTF8_CACHE(cache) NOOP |
e23c8137 JH |
64 | #endif |
65 | ||
f8c7b90f | 66 | #ifdef PERL_OLD_COPY_ON_WRITE |
765f542d | 67 | #define SV_COW_NEXT_SV(sv) INT2PTR(SV *,SvUVX(sv)) |
607fa7f2 | 68 | #define SV_COW_NEXT_SV_SET(current,next) SvUV_set(current, PTR2UV(next)) |
b5ccf5f2 | 69 | /* This is a pessimistic view. Scalar must be purely a read-write PV to copy- |
765f542d | 70 | on-write. */ |
765f542d | 71 | #endif |
645c22ef DM |
72 | |
73 | /* ============================================================================ | |
74 | ||
75 | =head1 Allocation and deallocation of SVs. | |
76 | ||
d2a0f284 JC |
77 | An SV (or AV, HV, etc.) is allocated in two parts: the head (struct |
78 | sv, av, hv...) contains type and reference count information, and for | |
79 | many types, a pointer to the body (struct xrv, xpv, xpviv...), which | |
80 | contains fields specific to each type. Some types store all they need | |
81 | in the head, so don't have a body. | |
82 | ||
486ec47a | 83 | In all but the most memory-paranoid configurations (ex: PURIFY), heads |
d2a0f284 JC |
84 | and bodies are allocated out of arenas, which by default are |
85 | approximately 4K chunks of memory parcelled up into N heads or bodies. | |
93e68bfb JC |
86 | Sv-bodies are allocated by their sv-type, guaranteeing size |
87 | consistency needed to allocate safely from arrays. | |
88 | ||
d2a0f284 JC |
89 | For SV-heads, the first slot in each arena is reserved, and holds a |
90 | link to the next arena, some flags, and a note of the number of slots. | |
91 | Snaked through each arena chain is a linked list of free items; when | |
92 | this becomes empty, an extra arena is allocated and divided up into N | |
93 | items which are threaded into the free list. | |
94 | ||
95 | SV-bodies are similar, but they use arena-sets by default, which | |
96 | separate the link and info from the arena itself, and reclaim the 1st | |
97 | slot in the arena. SV-bodies are further described later. | |
645c22ef DM |
98 | |
99 | The following global variables are associated with arenas: | |
100 | ||
101 | PL_sv_arenaroot pointer to list of SV arenas | |
102 | PL_sv_root pointer to list of free SV structures | |
103 | ||
d2a0f284 JC |
104 | PL_body_arenas head of linked-list of body arenas |
105 | PL_body_roots[] array of pointers to list of free bodies of svtype | |
106 | arrays are indexed by the svtype needed | |
93e68bfb | 107 | |
d2a0f284 JC |
108 | A few special SV heads are not allocated from an arena, but are |
109 | instead directly created in the interpreter structure, eg PL_sv_undef. | |
93e68bfb JC |
110 | The size of arenas can be changed from the default by setting |
111 | PERL_ARENA_SIZE appropriately at compile time. | |
645c22ef DM |
112 | |
113 | The SV arena serves the secondary purpose of allowing still-live SVs | |
114 | to be located and destroyed during final cleanup. | |
115 | ||
116 | At the lowest level, the macros new_SV() and del_SV() grab and free | |
117 | an SV head. (If debugging with -DD, del_SV() calls the function S_del_sv() | |
118 | to return the SV to the free list with error checking.) new_SV() calls | |
119 | more_sv() / sv_add_arena() to add an extra arena if the free list is empty. | |
120 | SVs in the free list have their SvTYPE field set to all ones. | |
121 | ||
ff276b08 | 122 | At the time of very final cleanup, sv_free_arenas() is called from |
645c22ef | 123 | perl_destruct() to physically free all the arenas allocated since the |
6a93a7e5 | 124 | start of the interpreter. |
645c22ef | 125 | |
645c22ef DM |
126 | The function visit() scans the SV arenas list, and calls a specified |
127 | function for each SV it finds which is still live - ie which has an SvTYPE | |
128 | other than all 1's, and a non-zero SvREFCNT. visit() is used by the | |
129 | following functions (specified as [function that calls visit()] / [function | |
130 | called by visit() for each SV]): | |
131 | ||
132 | sv_report_used() / do_report_used() | |
f2524eef | 133 | dump all remaining SVs (debugging aid) |
645c22ef | 134 | |
e4487e9b DM |
135 | sv_clean_objs() / do_clean_objs(),do_clean_named_objs(), |
136 | do_clean_named_io_objs() | |
645c22ef | 137 | Attempt to free all objects pointed to by RVs, |
c3b19b5c | 138 | and try to do the same for all objects indirectly |
645c22ef DM |
139 | referenced by typeglobs too. Called once from |
140 | perl_destruct(), prior to calling sv_clean_all() | |
141 | below. | |
142 | ||
143 | sv_clean_all() / do_clean_all() | |
144 | SvREFCNT_dec(sv) each remaining SV, possibly | |
145 | triggering an sv_free(). It also sets the | |
146 | SVf_BREAK flag on the SV to indicate that the | |
147 | refcnt has been artificially lowered, and thus | |
148 | stopping sv_free() from giving spurious warnings | |
149 | about SVs which unexpectedly have a refcnt | |
150 | of zero. called repeatedly from perl_destruct() | |
151 | until there are no SVs left. | |
152 | ||
93e68bfb | 153 | =head2 Arena allocator API Summary |
645c22ef DM |
154 | |
155 | Private API to rest of sv.c | |
156 | ||
157 | new_SV(), del_SV(), | |
158 | ||
df0f0429 | 159 | new_XPVNV(), del_XPVGV(), |
645c22ef DM |
160 | etc |
161 | ||
162 | Public API: | |
163 | ||
8cf8f3d1 | 164 | sv_report_used(), sv_clean_objs(), sv_clean_all(), sv_free_arenas() |
645c22ef | 165 | |
645c22ef DM |
166 | =cut |
167 | ||
3e8320cc | 168 | * ========================================================================= */ |
645c22ef | 169 | |
4561caa4 CS |
170 | /* |
171 | * "A time to plant, and a time to uproot what was planted..." | |
172 | */ | |
173 | ||
d7a2c63c MHM |
174 | #ifdef PERL_MEM_LOG |
175 | # define MEM_LOG_NEW_SV(sv, file, line, func) \ | |
176 | Perl_mem_log_new_sv(sv, file, line, func) | |
177 | # define MEM_LOG_DEL_SV(sv, file, line, func) \ | |
178 | Perl_mem_log_del_sv(sv, file, line, func) | |
179 | #else | |
180 | # define MEM_LOG_NEW_SV(sv, file, line, func) NOOP | |
181 | # define MEM_LOG_DEL_SV(sv, file, line, func) NOOP | |
182 | #endif | |
183 | ||
fd0854ff | 184 | #ifdef DEBUG_LEAKING_SCALARS |
22162ca8 | 185 | # define FREE_SV_DEBUG_FILE(sv) Safefree((sv)->sv_debug_file) |
d7a2c63c MHM |
186 | # define DEBUG_SV_SERIAL(sv) \ |
187 | DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) del_SV\n", \ | |
188 | PTR2UV(sv), (long)(sv)->sv_debug_serial)) | |
fd0854ff DM |
189 | #else |
190 | # define FREE_SV_DEBUG_FILE(sv) | |
d7a2c63c | 191 | # define DEBUG_SV_SERIAL(sv) NOOP |
fd0854ff DM |
192 | #endif |
193 | ||
48614a46 NC |
194 | #ifdef PERL_POISON |
195 | # define SvARENA_CHAIN(sv) ((sv)->sv_u.svu_rv) | |
daba3364 | 196 | # define SvARENA_CHAIN_SET(sv,val) (sv)->sv_u.svu_rv = MUTABLE_SV((val)) |
48614a46 NC |
197 | /* Whilst I'd love to do this, it seems that things like to check on |
198 | unreferenced scalars | |
7e337ee0 | 199 | # define POSION_SV_HEAD(sv) PoisonNew(sv, 1, struct STRUCT_SV) |
48614a46 | 200 | */ |
7e337ee0 JH |
201 | # define POSION_SV_HEAD(sv) PoisonNew(&SvANY(sv), 1, void *), \ |
202 | PoisonNew(&SvREFCNT(sv), 1, U32) | |
48614a46 NC |
203 | #else |
204 | # define SvARENA_CHAIN(sv) SvANY(sv) | |
3eef1deb | 205 | # define SvARENA_CHAIN_SET(sv,val) SvANY(sv) = (void *)(val) |
48614a46 NC |
206 | # define POSION_SV_HEAD(sv) |
207 | #endif | |
208 | ||
990198f0 DM |
209 | /* Mark an SV head as unused, and add to free list. |
210 | * | |
211 | * If SVf_BREAK is set, skip adding it to the free list, as this SV had | |
212 | * its refcount artificially decremented during global destruction, so | |
213 | * there may be dangling pointers to it. The last thing we want in that | |
214 | * case is for it to be reused. */ | |
215 | ||
053fc874 GS |
216 | #define plant_SV(p) \ |
217 | STMT_START { \ | |
990198f0 | 218 | const U32 old_flags = SvFLAGS(p); \ |
d7a2c63c MHM |
219 | MEM_LOG_DEL_SV(p, __FILE__, __LINE__, FUNCTION__); \ |
220 | DEBUG_SV_SERIAL(p); \ | |
fd0854ff | 221 | FREE_SV_DEBUG_FILE(p); \ |
48614a46 | 222 | POSION_SV_HEAD(p); \ |
053fc874 | 223 | SvFLAGS(p) = SVTYPEMASK; \ |
990198f0 | 224 | if (!(old_flags & SVf_BREAK)) { \ |
3eef1deb | 225 | SvARENA_CHAIN_SET(p, PL_sv_root); \ |
990198f0 DM |
226 | PL_sv_root = (p); \ |
227 | } \ | |
053fc874 GS |
228 | --PL_sv_count; \ |
229 | } STMT_END | |
a0d0e21e | 230 | |
053fc874 GS |
231 | #define uproot_SV(p) \ |
232 | STMT_START { \ | |
233 | (p) = PL_sv_root; \ | |
daba3364 | 234 | PL_sv_root = MUTABLE_SV(SvARENA_CHAIN(p)); \ |
053fc874 GS |
235 | ++PL_sv_count; \ |
236 | } STMT_END | |
237 | ||
645c22ef | 238 | |
cac9b346 NC |
239 | /* make some more SVs by adding another arena */ |
240 | ||
cac9b346 NC |
241 | STATIC SV* |
242 | S_more_sv(pTHX) | |
243 | { | |
97aff369 | 244 | dVAR; |
cac9b346 | 245 | SV* sv; |
9a87bd09 NC |
246 | char *chunk; /* must use New here to match call to */ |
247 | Newx(chunk,PERL_ARENA_SIZE,char); /* Safefree() in sv_free_arenas() */ | |
248 | sv_add_arena(chunk, PERL_ARENA_SIZE, 0); | |
cac9b346 NC |
249 | uproot_SV(sv); |
250 | return sv; | |
251 | } | |
252 | ||
645c22ef DM |
253 | /* new_SV(): return a new, empty SV head */ |
254 | ||
eba0f806 DM |
255 | #ifdef DEBUG_LEAKING_SCALARS |
256 | /* provide a real function for a debugger to play with */ | |
257 | STATIC SV* | |
d7a2c63c | 258 | S_new_SV(pTHX_ const char *file, int line, const char *func) |
eba0f806 DM |
259 | { |
260 | SV* sv; | |
261 | ||
eba0f806 DM |
262 | if (PL_sv_root) |
263 | uproot_SV(sv); | |
264 | else | |
cac9b346 | 265 | sv = S_more_sv(aTHX); |
eba0f806 DM |
266 | SvANY(sv) = 0; |
267 | SvREFCNT(sv) = 1; | |
268 | SvFLAGS(sv) = 0; | |
fd0854ff | 269 | sv->sv_debug_optype = PL_op ? PL_op->op_type : 0; |
e385c3bf DM |
270 | sv->sv_debug_line = (U16) (PL_parser && PL_parser->copline != NOLINE |
271 | ? PL_parser->copline | |
272 | : PL_curcop | |
f24aceb1 DM |
273 | ? CopLINE(PL_curcop) |
274 | : 0 | |
e385c3bf | 275 | ); |
fd0854ff | 276 | sv->sv_debug_inpad = 0; |
cd676548 | 277 | sv->sv_debug_parent = NULL; |
fd0854ff | 278 | sv->sv_debug_file = PL_curcop ? savepv(CopFILE(PL_curcop)): NULL; |
d7a2c63c MHM |
279 | |
280 | sv->sv_debug_serial = PL_sv_serial++; | |
281 | ||
282 | MEM_LOG_NEW_SV(sv, file, line, func); | |
283 | DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) new_SV (from %s:%d [%s])\n", | |
284 | PTR2UV(sv), (long)sv->sv_debug_serial, file, line, func)); | |
285 | ||
eba0f806 DM |
286 | return sv; |
287 | } | |
d7a2c63c | 288 | # define new_SV(p) (p)=S_new_SV(aTHX_ __FILE__, __LINE__, FUNCTION__) |
eba0f806 DM |
289 | |
290 | #else | |
291 | # define new_SV(p) \ | |
053fc874 | 292 | STMT_START { \ |
053fc874 GS |
293 | if (PL_sv_root) \ |
294 | uproot_SV(p); \ | |
295 | else \ | |
cac9b346 | 296 | (p) = S_more_sv(aTHX); \ |
053fc874 GS |
297 | SvANY(p) = 0; \ |
298 | SvREFCNT(p) = 1; \ | |
299 | SvFLAGS(p) = 0; \ | |
d7a2c63c | 300 | MEM_LOG_NEW_SV(p, __FILE__, __LINE__, FUNCTION__); \ |
053fc874 | 301 | } STMT_END |
eba0f806 | 302 | #endif |
463ee0b2 | 303 | |
645c22ef DM |
304 | |
305 | /* del_SV(): return an empty SV head to the free list */ | |
306 | ||
a0d0e21e | 307 | #ifdef DEBUGGING |
4561caa4 | 308 | |
053fc874 GS |
309 | #define del_SV(p) \ |
310 | STMT_START { \ | |
aea4f609 | 311 | if (DEBUG_D_TEST) \ |
053fc874 GS |
312 | del_sv(p); \ |
313 | else \ | |
314 | plant_SV(p); \ | |
053fc874 | 315 | } STMT_END |
a0d0e21e | 316 | |
76e3520e | 317 | STATIC void |
cea2e8a9 | 318 | S_del_sv(pTHX_ SV *p) |
463ee0b2 | 319 | { |
97aff369 | 320 | dVAR; |
7918f24d NC |
321 | |
322 | PERL_ARGS_ASSERT_DEL_SV; | |
323 | ||
aea4f609 | 324 | if (DEBUG_D_TEST) { |
4633a7c4 | 325 | SV* sva; |
a3b680e6 | 326 | bool ok = 0; |
daba3364 | 327 | for (sva = PL_sv_arenaroot; sva; sva = MUTABLE_SV(SvANY(sva))) { |
53c1dcc0 AL |
328 | const SV * const sv = sva + 1; |
329 | const SV * const svend = &sva[SvREFCNT(sva)]; | |
c0ff570e | 330 | if (p >= sv && p < svend) { |
a0d0e21e | 331 | ok = 1; |
c0ff570e NC |
332 | break; |
333 | } | |
a0d0e21e LW |
334 | } |
335 | if (!ok) { | |
9b387841 NC |
336 | Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), |
337 | "Attempt to free non-arena SV: 0x%"UVxf | |
338 | pTHX__FORMAT, PTR2UV(p) pTHX__VALUE); | |
a0d0e21e LW |
339 | return; |
340 | } | |
341 | } | |
4561caa4 | 342 | plant_SV(p); |
463ee0b2 | 343 | } |
a0d0e21e | 344 | |
4561caa4 CS |
345 | #else /* ! DEBUGGING */ |
346 | ||
347 | #define del_SV(p) plant_SV(p) | |
348 | ||
349 | #endif /* DEBUGGING */ | |
463ee0b2 | 350 | |
645c22ef DM |
351 | |
352 | /* | |
ccfc67b7 JH |
353 | =head1 SV Manipulation Functions |
354 | ||
645c22ef DM |
355 | =for apidoc sv_add_arena |
356 | ||
357 | Given a chunk of memory, link it to the head of the list of arenas, | |
358 | and split it into a list of free SVs. | |
359 | ||
360 | =cut | |
361 | */ | |
362 | ||
d2bd4e7f NC |
363 | static void |
364 | S_sv_add_arena(pTHX_ char *const ptr, const U32 size, const U32 flags) | |
463ee0b2 | 365 | { |
97aff369 | 366 | dVAR; |
daba3364 | 367 | SV *const sva = MUTABLE_SV(ptr); |
463ee0b2 LW |
368 | register SV* sv; |
369 | register SV* svend; | |
4633a7c4 | 370 | |
7918f24d NC |
371 | PERL_ARGS_ASSERT_SV_ADD_ARENA; |
372 | ||
4633a7c4 | 373 | /* The first SV in an arena isn't an SV. */ |
3280af22 | 374 | SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */ |
4633a7c4 LW |
375 | SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */ |
376 | SvFLAGS(sva) = flags; /* FAKE if not to be freed */ | |
377 | ||
3280af22 NIS |
378 | PL_sv_arenaroot = sva; |
379 | PL_sv_root = sva + 1; | |
4633a7c4 LW |
380 | |
381 | svend = &sva[SvREFCNT(sva) - 1]; | |
382 | sv = sva + 1; | |
463ee0b2 | 383 | while (sv < svend) { |
3eef1deb | 384 | SvARENA_CHAIN_SET(sv, (sv + 1)); |
03e36789 | 385 | #ifdef DEBUGGING |
978b032e | 386 | SvREFCNT(sv) = 0; |
03e36789 | 387 | #endif |
4b69cbe3 | 388 | /* Must always set typemask because it's always checked in on cleanup |
03e36789 | 389 | when the arenas are walked looking for objects. */ |
8990e307 | 390 | SvFLAGS(sv) = SVTYPEMASK; |
463ee0b2 LW |
391 | sv++; |
392 | } | |
3eef1deb | 393 | SvARENA_CHAIN_SET(sv, 0); |
03e36789 NC |
394 | #ifdef DEBUGGING |
395 | SvREFCNT(sv) = 0; | |
396 | #endif | |
4633a7c4 LW |
397 | SvFLAGS(sv) = SVTYPEMASK; |
398 | } | |
399 | ||
055972dc DM |
400 | /* visit(): call the named function for each non-free SV in the arenas |
401 | * whose flags field matches the flags/mask args. */ | |
645c22ef | 402 | |
5226ed68 | 403 | STATIC I32 |
de37a194 | 404 | S_visit(pTHX_ SVFUNC_t f, const U32 flags, const U32 mask) |
8990e307 | 405 | { |
97aff369 | 406 | dVAR; |
4633a7c4 | 407 | SV* sva; |
5226ed68 | 408 | I32 visited = 0; |
8990e307 | 409 | |
7918f24d NC |
410 | PERL_ARGS_ASSERT_VISIT; |
411 | ||
daba3364 | 412 | for (sva = PL_sv_arenaroot; sva; sva = MUTABLE_SV(SvANY(sva))) { |
53c1dcc0 | 413 | register const SV * const svend = &sva[SvREFCNT(sva)]; |
a3b680e6 | 414 | register SV* sv; |
4561caa4 | 415 | for (sv = sva + 1; sv < svend; ++sv) { |
055972dc DM |
416 | if (SvTYPE(sv) != SVTYPEMASK |
417 | && (sv->sv_flags & mask) == flags | |
418 | && SvREFCNT(sv)) | |
419 | { | |
acfe0abc | 420 | (FCALL)(aTHX_ sv); |
5226ed68 JH |
421 | ++visited; |
422 | } | |
8990e307 LW |
423 | } |
424 | } | |
5226ed68 | 425 | return visited; |
8990e307 LW |
426 | } |
427 | ||
758a08c3 JH |
428 | #ifdef DEBUGGING |
429 | ||
645c22ef DM |
430 | /* called by sv_report_used() for each live SV */ |
431 | ||
432 | static void | |
5fa45a31 | 433 | do_report_used(pTHX_ SV *const sv) |
645c22ef DM |
434 | { |
435 | if (SvTYPE(sv) != SVTYPEMASK) { | |
436 | PerlIO_printf(Perl_debug_log, "****\n"); | |
437 | sv_dump(sv); | |
438 | } | |
439 | } | |
758a08c3 | 440 | #endif |
645c22ef DM |
441 | |
442 | /* | |
443 | =for apidoc sv_report_used | |
444 | ||
445 | Dump the contents of all SVs not yet freed. (Debugging aid). | |
446 | ||
447 | =cut | |
448 | */ | |
449 | ||
8990e307 | 450 | void |
864dbfa3 | 451 | Perl_sv_report_used(pTHX) |
4561caa4 | 452 | { |
ff270d3a | 453 | #ifdef DEBUGGING |
055972dc | 454 | visit(do_report_used, 0, 0); |
96a5add6 AL |
455 | #else |
456 | PERL_UNUSED_CONTEXT; | |
ff270d3a | 457 | #endif |
4561caa4 CS |
458 | } |
459 | ||
645c22ef DM |
460 | /* called by sv_clean_objs() for each live SV */ |
461 | ||
462 | static void | |
de37a194 | 463 | do_clean_objs(pTHX_ SV *const ref) |
645c22ef | 464 | { |
97aff369 | 465 | dVAR; |
ea724faa NC |
466 | assert (SvROK(ref)); |
467 | { | |
823a54a3 AL |
468 | SV * const target = SvRV(ref); |
469 | if (SvOBJECT(target)) { | |
470 | DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(ref))); | |
471 | if (SvWEAKREF(ref)) { | |
472 | sv_del_backref(target, ref); | |
473 | SvWEAKREF_off(ref); | |
474 | SvRV_set(ref, NULL); | |
475 | } else { | |
476 | SvROK_off(ref); | |
477 | SvRV_set(ref, NULL); | |
478 | SvREFCNT_dec(target); | |
479 | } | |
645c22ef DM |
480 | } |
481 | } | |
482 | ||
483 | /* XXX Might want to check arrays, etc. */ | |
484 | } | |
485 | ||
645c22ef | 486 | |
e4487e9b DM |
487 | /* clear any slots in a GV which hold objects - except IO; |
488 | * called by sv_clean_objs() for each live GV */ | |
489 | ||
645c22ef | 490 | static void |
f30de749 | 491 | do_clean_named_objs(pTHX_ SV *const sv) |
645c22ef | 492 | { |
97aff369 | 493 | dVAR; |
57ef47cc | 494 | SV *obj; |
ea724faa | 495 | assert(SvTYPE(sv) == SVt_PVGV); |
d011219a | 496 | assert(isGV_with_GP(sv)); |
57ef47cc DM |
497 | if (!GvGP(sv)) |
498 | return; | |
499 | ||
500 | /* freeing GP entries may indirectly free the current GV; | |
501 | * hold onto it while we mess with the GP slots */ | |
502 | SvREFCNT_inc(sv); | |
503 | ||
504 | if ( ((obj = GvSV(sv) )) && SvOBJECT(obj)) { | |
505 | DEBUG_D((PerlIO_printf(Perl_debug_log, | |
506 | "Cleaning named glob SV object:\n "), sv_dump(obj))); | |
507 | GvSV(sv) = NULL; | |
508 | SvREFCNT_dec(obj); | |
509 | } | |
510 | if ( ((obj = MUTABLE_SV(GvAV(sv)) )) && SvOBJECT(obj)) { | |
511 | DEBUG_D((PerlIO_printf(Perl_debug_log, | |
512 | "Cleaning named glob AV object:\n "), sv_dump(obj))); | |
513 | GvAV(sv) = NULL; | |
514 | SvREFCNT_dec(obj); | |
515 | } | |
516 | if ( ((obj = MUTABLE_SV(GvHV(sv)) )) && SvOBJECT(obj)) { | |
517 | DEBUG_D((PerlIO_printf(Perl_debug_log, | |
518 | "Cleaning named glob HV object:\n "), sv_dump(obj))); | |
519 | GvHV(sv) = NULL; | |
520 | SvREFCNT_dec(obj); | |
521 | } | |
522 | if ( ((obj = MUTABLE_SV(GvCV(sv)) )) && SvOBJECT(obj)) { | |
523 | DEBUG_D((PerlIO_printf(Perl_debug_log, | |
524 | "Cleaning named glob CV object:\n "), sv_dump(obj))); | |
c43ae56f | 525 | GvCV_set(sv, NULL); |
57ef47cc DM |
526 | SvREFCNT_dec(obj); |
527 | } | |
e4487e9b DM |
528 | SvREFCNT_dec(sv); /* undo the inc above */ |
529 | } | |
530 | ||
68b590d9 | 531 | /* clear any IO slots in a GV which hold objects (except stderr, defout); |
e4487e9b DM |
532 | * called by sv_clean_objs() for each live GV */ |
533 | ||
534 | static void | |
535 | do_clean_named_io_objs(pTHX_ SV *const sv) | |
536 | { | |
537 | dVAR; | |
538 | SV *obj; | |
539 | assert(SvTYPE(sv) == SVt_PVGV); | |
540 | assert(isGV_with_GP(sv)); | |
68b590d9 | 541 | if (!GvGP(sv) || sv == (SV*)PL_stderrgv || sv == (SV*)PL_defoutgv) |
e4487e9b DM |
542 | return; |
543 | ||
544 | SvREFCNT_inc(sv); | |
57ef47cc DM |
545 | if ( ((obj = MUTABLE_SV(GvIO(sv)) )) && SvOBJECT(obj)) { |
546 | DEBUG_D((PerlIO_printf(Perl_debug_log, | |
547 | "Cleaning named glob IO object:\n "), sv_dump(obj))); | |
548 | GvIOp(sv) = NULL; | |
549 | SvREFCNT_dec(obj); | |
645c22ef | 550 | } |
57ef47cc | 551 | SvREFCNT_dec(sv); /* undo the inc above */ |
645c22ef | 552 | } |
645c22ef | 553 | |
4155e4fe FC |
554 | /* Void wrapper to pass to visit() */ |
555 | static void | |
556 | do_curse(pTHX_ SV * const sv) { | |
c2910e6c FC |
557 | if ((PL_stderrgv && GvGP(PL_stderrgv) && (SV*)GvIO(PL_stderrgv) == sv) |
558 | || (PL_defoutgv && GvGP(PL_defoutgv) && (SV*)GvIO(PL_defoutgv) == sv)) | |
4155e4fe FC |
559 | return; |
560 | (void)curse(sv, 0); | |
561 | } | |
562 | ||
645c22ef DM |
563 | /* |
564 | =for apidoc sv_clean_objs | |
565 | ||
566 | Attempt to destroy all objects not yet freed | |
567 | ||
568 | =cut | |
569 | */ | |
570 | ||
4561caa4 | 571 | void |
864dbfa3 | 572 | Perl_sv_clean_objs(pTHX) |
4561caa4 | 573 | { |
97aff369 | 574 | dVAR; |
68b590d9 | 575 | GV *olddef, *olderr; |
3280af22 | 576 | PL_in_clean_objs = TRUE; |
055972dc | 577 | visit(do_clean_objs, SVf_ROK, SVf_ROK); |
e4487e9b DM |
578 | /* Some barnacles may yet remain, clinging to typeglobs. |
579 | * Run the non-IO destructors first: they may want to output | |
580 | * error messages, close files etc */ | |
d011219a | 581 | visit(do_clean_named_objs, SVt_PVGV|SVpgv_GP, SVTYPEMASK|SVp_POK|SVpgv_GP); |
e4487e9b | 582 | visit(do_clean_named_io_objs, SVt_PVGV|SVpgv_GP, SVTYPEMASK|SVp_POK|SVpgv_GP); |
4155e4fe FC |
583 | /* And if there are some very tenacious barnacles clinging to arrays, |
584 | closures, or what have you.... */ | |
7f586e41 | 585 | /* XXX This line breaks Tk and Gtk2. See [perl #82542]. |
4155e4fe | 586 | visit(do_curse, SVs_OBJECT, SVs_OBJECT); |
7f586e41 | 587 | */ |
68b590d9 DM |
588 | olddef = PL_defoutgv; |
589 | PL_defoutgv = NULL; /* disable skip of PL_defoutgv */ | |
590 | if (olddef && isGV_with_GP(olddef)) | |
591 | do_clean_named_io_objs(aTHX_ MUTABLE_SV(olddef)); | |
592 | olderr = PL_stderrgv; | |
593 | PL_stderrgv = NULL; /* disable skip of PL_stderrgv */ | |
594 | if (olderr && isGV_with_GP(olderr)) | |
595 | do_clean_named_io_objs(aTHX_ MUTABLE_SV(olderr)); | |
596 | SvREFCNT_dec(olddef); | |
3280af22 | 597 | PL_in_clean_objs = FALSE; |
4561caa4 CS |
598 | } |
599 | ||
645c22ef DM |
600 | /* called by sv_clean_all() for each live SV */ |
601 | ||
602 | static void | |
de37a194 | 603 | do_clean_all(pTHX_ SV *const sv) |
645c22ef | 604 | { |
97aff369 | 605 | dVAR; |
daba3364 | 606 | if (sv == (const SV *) PL_fdpid || sv == (const SV *)PL_strtab) { |
cddfcddc | 607 | /* don't clean pid table and strtab */ |
d17ea597 | 608 | return; |
cddfcddc | 609 | } |
645c22ef DM |
610 | DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) )); |
611 | SvFLAGS(sv) |= SVf_BREAK; | |
612 | SvREFCNT_dec(sv); | |
613 | } | |
614 | ||
615 | /* | |
616 | =for apidoc sv_clean_all | |
617 | ||
618 | Decrement the refcnt of each remaining SV, possibly triggering a | |
619 | cleanup. This function may have to be called multiple times to free | |
ff276b08 | 620 | SVs which are in complex self-referential hierarchies. |
645c22ef DM |
621 | |
622 | =cut | |
623 | */ | |
624 | ||
5226ed68 | 625 | I32 |
864dbfa3 | 626 | Perl_sv_clean_all(pTHX) |
8990e307 | 627 | { |
97aff369 | 628 | dVAR; |
5226ed68 | 629 | I32 cleaned; |
3280af22 | 630 | PL_in_clean_all = TRUE; |
055972dc | 631 | cleaned = visit(do_clean_all, 0,0); |
5226ed68 | 632 | return cleaned; |
8990e307 | 633 | } |
463ee0b2 | 634 | |
5e258f8c JC |
635 | /* |
636 | ARENASETS: a meta-arena implementation which separates arena-info | |
637 | into struct arena_set, which contains an array of struct | |
638 | arena_descs, each holding info for a single arena. By separating | |
639 | the meta-info from the arena, we recover the 1st slot, formerly | |
640 | borrowed for list management. The arena_set is about the size of an | |
39244528 | 641 | arena, avoiding the needless malloc overhead of a naive linked-list. |
5e258f8c JC |
642 | |
643 | The cost is 1 arena-set malloc per ~320 arena-mallocs, + the unused | |
644 | memory in the last arena-set (1/2 on average). In trade, we get | |
645 | back the 1st slot in each arena (ie 1.7% of a CV-arena, less for | |
d2a0f284 | 646 | smaller types). The recovery of the wasted space allows use of |
e15dad31 JC |
647 | small arenas for large, rare body types, by changing array* fields |
648 | in body_details_by_type[] below. | |
5e258f8c | 649 | */ |
5e258f8c | 650 | struct arena_desc { |
398c677b NC |
651 | char *arena; /* the raw storage, allocated aligned */ |
652 | size_t size; /* its size ~4k typ */ | |
e5973ed5 | 653 | svtype utype; /* bodytype stored in arena */ |
5e258f8c JC |
654 | }; |
655 | ||
e6148039 NC |
656 | struct arena_set; |
657 | ||
658 | /* Get the maximum number of elements in set[] such that struct arena_set | |
e15dad31 | 659 | will fit within PERL_ARENA_SIZE, which is probably just under 4K, and |
e6148039 NC |
660 | therefore likely to be 1 aligned memory page. */ |
661 | ||
662 | #define ARENAS_PER_SET ((PERL_ARENA_SIZE - sizeof(struct arena_set*) \ | |
663 | - 2 * sizeof(int)) / sizeof (struct arena_desc)) | |
5e258f8c JC |
664 | |
665 | struct arena_set { | |
666 | struct arena_set* next; | |
0a848332 NC |
667 | unsigned int set_size; /* ie ARENAS_PER_SET */ |
668 | unsigned int curr; /* index of next available arena-desc */ | |
5e258f8c JC |
669 | struct arena_desc set[ARENAS_PER_SET]; |
670 | }; | |
671 | ||
645c22ef DM |
672 | /* |
673 | =for apidoc sv_free_arenas | |
674 | ||
675 | Deallocate the memory used by all arenas. Note that all the individual SV | |
676 | heads and bodies within the arenas must already have been freed. | |
677 | ||
678 | =cut | |
679 | */ | |
4633a7c4 | 680 | void |
864dbfa3 | 681 | Perl_sv_free_arenas(pTHX) |
4633a7c4 | 682 | { |
97aff369 | 683 | dVAR; |
4633a7c4 LW |
684 | SV* sva; |
685 | SV* svanext; | |
0a848332 | 686 | unsigned int i; |
4633a7c4 LW |
687 | |
688 | /* Free arenas here, but be careful about fake ones. (We assume | |
689 | contiguity of the fake ones with the corresponding real ones.) */ | |
690 | ||
3280af22 | 691 | for (sva = PL_sv_arenaroot; sva; sva = svanext) { |
daba3364 | 692 | svanext = MUTABLE_SV(SvANY(sva)); |
4633a7c4 | 693 | while (svanext && SvFAKE(svanext)) |
daba3364 | 694 | svanext = MUTABLE_SV(SvANY(svanext)); |
4633a7c4 LW |
695 | |
696 | if (!SvFAKE(sva)) | |
1df70142 | 697 | Safefree(sva); |
4633a7c4 | 698 | } |
93e68bfb | 699 | |
5e258f8c | 700 | { |
0a848332 NC |
701 | struct arena_set *aroot = (struct arena_set*) PL_body_arenas; |
702 | ||
703 | while (aroot) { | |
704 | struct arena_set *current = aroot; | |
705 | i = aroot->curr; | |
706 | while (i--) { | |
5e258f8c JC |
707 | assert(aroot->set[i].arena); |
708 | Safefree(aroot->set[i].arena); | |
709 | } | |
0a848332 NC |
710 | aroot = aroot->next; |
711 | Safefree(current); | |
5e258f8c JC |
712 | } |
713 | } | |
dc8220bf | 714 | PL_body_arenas = 0; |
fdda85ca | 715 | |
0a848332 NC |
716 | i = PERL_ARENA_ROOTS_SIZE; |
717 | while (i--) | |
93e68bfb | 718 | PL_body_roots[i] = 0; |
93e68bfb | 719 | |
3280af22 NIS |
720 | PL_sv_arenaroot = 0; |
721 | PL_sv_root = 0; | |
4633a7c4 LW |
722 | } |
723 | ||
bd81e77b NC |
724 | /* |
725 | Here are mid-level routines that manage the allocation of bodies out | |
726 | of the various arenas. There are 5 kinds of arenas: | |
29489e7c | 727 | |
bd81e77b NC |
728 | 1. SV-head arenas, which are discussed and handled above |
729 | 2. regular body arenas | |
730 | 3. arenas for reduced-size bodies | |
731 | 4. Hash-Entry arenas | |
29489e7c | 732 | |
bd81e77b NC |
733 | Arena types 2 & 3 are chained by body-type off an array of |
734 | arena-root pointers, which is indexed by svtype. Some of the | |
735 | larger/less used body types are malloced singly, since a large | |
736 | unused block of them is wasteful. Also, several svtypes dont have | |
737 | bodies; the data fits into the sv-head itself. The arena-root | |
738 | pointer thus has a few unused root-pointers (which may be hijacked | |
739 | later for arena types 4,5) | |
29489e7c | 740 | |
bd81e77b NC |
741 | 3 differs from 2 as an optimization; some body types have several |
742 | unused fields in the front of the structure (which are kept in-place | |
743 | for consistency). These bodies can be allocated in smaller chunks, | |
744 | because the leading fields arent accessed. Pointers to such bodies | |
745 | are decremented to point at the unused 'ghost' memory, knowing that | |
746 | the pointers are used with offsets to the real memory. | |
29489e7c | 747 | |
d2a0f284 JC |
748 | |
749 | =head1 SV-Body Allocation | |
750 | ||
751 | Allocation of SV-bodies is similar to SV-heads, differing as follows; | |
752 | the allocation mechanism is used for many body types, so is somewhat | |
753 | more complicated, it uses arena-sets, and has no need for still-live | |
754 | SV detection. | |
755 | ||
756 | At the outermost level, (new|del)_X*V macros return bodies of the | |
757 | appropriate type. These macros call either (new|del)_body_type or | |
758 | (new|del)_body_allocated macro pairs, depending on specifics of the | |
759 | type. Most body types use the former pair, the latter pair is used to | |
760 | allocate body types with "ghost fields". | |
761 | ||
762 | "ghost fields" are fields that are unused in certain types, and | |
69ba284b | 763 | consequently don't need to actually exist. They are declared because |
d2a0f284 JC |
764 | they're part of a "base type", which allows use of functions as |
765 | methods. The simplest examples are AVs and HVs, 2 aggregate types | |
766 | which don't use the fields which support SCALAR semantics. | |
767 | ||
69ba284b | 768 | For these types, the arenas are carved up into appropriately sized |
d2a0f284 JC |
769 | chunks, we thus avoid wasted memory for those unaccessed members. |
770 | When bodies are allocated, we adjust the pointer back in memory by the | |
69ba284b | 771 | size of the part not allocated, so it's as if we allocated the full |
d2a0f284 JC |
772 | structure. (But things will all go boom if you write to the part that |
773 | is "not there", because you'll be overwriting the last members of the | |
774 | preceding structure in memory.) | |
775 | ||
69ba284b NC |
776 | We calculate the correction using the STRUCT_OFFSET macro on the first |
777 | member present. If the allocated structure is smaller (no initial NV | |
778 | actually allocated) then the net effect is to subtract the size of the NV | |
779 | from the pointer, to return a new pointer as if an initial NV were actually | |
780 | allocated. (We were using structures named *_allocated for this, but | |
781 | this turned out to be a subtle bug, because a structure without an NV | |
782 | could have a lower alignment constraint, but the compiler is allowed to | |
783 | optimised accesses based on the alignment constraint of the actual pointer | |
784 | to the full structure, for example, using a single 64 bit load instruction | |
785 | because it "knows" that two adjacent 32 bit members will be 8-byte aligned.) | |
d2a0f284 JC |
786 | |
787 | This is the same trick as was used for NV and IV bodies. Ironically it | |
788 | doesn't need to be used for NV bodies any more, because NV is now at | |
789 | the start of the structure. IV bodies don't need it either, because | |
790 | they are no longer allocated. | |
791 | ||
792 | In turn, the new_body_* allocators call S_new_body(), which invokes | |
793 | new_body_inline macro, which takes a lock, and takes a body off the | |
1e30fcd5 | 794 | linked list at PL_body_roots[sv_type], calling Perl_more_bodies() if |
d2a0f284 JC |
795 | necessary to refresh an empty list. Then the lock is released, and |
796 | the body is returned. | |
797 | ||
99816f8d | 798 | Perl_more_bodies allocates a new arena, and carves it up into an array of N |
d2a0f284 JC |
799 | bodies, which it strings into a linked list. It looks up arena-size |
800 | and body-size from the body_details table described below, thus | |
801 | supporting the multiple body-types. | |
802 | ||
803 | If PURIFY is defined, or PERL_ARENA_SIZE=0, arenas are not used, and | |
804 | the (new|del)_X*V macros are mapped directly to malloc/free. | |
805 | ||
d2a0f284 JC |
806 | For each sv-type, struct body_details bodies_by_type[] carries |
807 | parameters which control these aspects of SV handling: | |
808 | ||
809 | Arena_size determines whether arenas are used for this body type, and if | |
810 | so, how big they are. PURIFY or PERL_ARENA_SIZE=0 set this field to | |
811 | zero, forcing individual mallocs and frees. | |
812 | ||
813 | Body_size determines how big a body is, and therefore how many fit into | |
814 | each arena. Offset carries the body-pointer adjustment needed for | |
69ba284b | 815 | "ghost fields", and is used in *_allocated macros. |
d2a0f284 JC |
816 | |
817 | But its main purpose is to parameterize info needed in | |
818 | Perl_sv_upgrade(). The info here dramatically simplifies the function | |
69ba284b | 819 | vs the implementation in 5.8.8, making it table-driven. All fields |
d2a0f284 JC |
820 | are used for this, except for arena_size. |
821 | ||
822 | For the sv-types that have no bodies, arenas are not used, so those | |
823 | PL_body_roots[sv_type] are unused, and can be overloaded. In | |
824 | something of a special case, SVt_NULL is borrowed for HE arenas; | |
c6f8b1d0 | 825 | PL_body_roots[HE_SVSLOT=SVt_NULL] is filled by S_more_he, but the |
d2a0f284 | 826 | bodies_by_type[SVt_NULL] slot is not used, as the table is not |
c6f8b1d0 | 827 | available in hv.c. |
d2a0f284 | 828 | |
29489e7c DM |
829 | */ |
830 | ||
bd81e77b | 831 | struct body_details { |
0fb58b32 | 832 | U8 body_size; /* Size to allocate */ |
10666ae3 | 833 | U8 copy; /* Size of structure to copy (may be shorter) */ |
0fb58b32 | 834 | U8 offset; |
10666ae3 NC |
835 | unsigned int type : 4; /* We have space for a sanity check. */ |
836 | unsigned int cant_upgrade : 1; /* Cannot upgrade this type */ | |
837 | unsigned int zero_nv : 1; /* zero the NV when upgrading from this */ | |
838 | unsigned int arena : 1; /* Allocated from an arena */ | |
839 | size_t arena_size; /* Size of arena to allocate */ | |
bd81e77b | 840 | }; |
29489e7c | 841 | |
bd81e77b NC |
842 | #define HADNV FALSE |
843 | #define NONV TRUE | |
29489e7c | 844 | |
d2a0f284 | 845 | |
bd81e77b NC |
846 | #ifdef PURIFY |
847 | /* With -DPURFIY we allocate everything directly, and don't use arenas. | |
848 | This seems a rather elegant way to simplify some of the code below. */ | |
849 | #define HASARENA FALSE | |
850 | #else | |
851 | #define HASARENA TRUE | |
852 | #endif | |
853 | #define NOARENA FALSE | |
29489e7c | 854 | |
d2a0f284 JC |
855 | /* Size the arenas to exactly fit a given number of bodies. A count |
856 | of 0 fits the max number bodies into a PERL_ARENA_SIZE.block, | |
857 | simplifying the default. If count > 0, the arena is sized to fit | |
858 | only that many bodies, allowing arenas to be used for large, rare | |
859 | bodies (XPVFM, XPVIO) without undue waste. The arena size is | |
860 | limited by PERL_ARENA_SIZE, so we can safely oversize the | |
861 | declarations. | |
862 | */ | |
95db5f15 MB |
863 | #define FIT_ARENA0(body_size) \ |
864 | ((size_t)(PERL_ARENA_SIZE / body_size) * body_size) | |
865 | #define FIT_ARENAn(count,body_size) \ | |
866 | ( count * body_size <= PERL_ARENA_SIZE) \ | |
867 | ? count * body_size \ | |
868 | : FIT_ARENA0 (body_size) | |
869 | #define FIT_ARENA(count,body_size) \ | |
870 | count \ | |
871 | ? FIT_ARENAn (count, body_size) \ | |
872 | : FIT_ARENA0 (body_size) | |
d2a0f284 | 873 | |
bd81e77b NC |
874 | /* Calculate the length to copy. Specifically work out the length less any |
875 | final padding the compiler needed to add. See the comment in sv_upgrade | |
876 | for why copying the padding proved to be a bug. */ | |
29489e7c | 877 | |
bd81e77b NC |
878 | #define copy_length(type, last_member) \ |
879 | STRUCT_OFFSET(type, last_member) \ | |
daba3364 | 880 | + sizeof (((type*)SvANY((const SV *)0))->last_member) |
29489e7c | 881 | |
bd81e77b | 882 | static const struct body_details bodies_by_type[] = { |
829cd18a NC |
883 | /* HEs use this offset for their arena. */ |
884 | { 0, 0, 0, SVt_NULL, FALSE, NONV, NOARENA, 0 }, | |
d2a0f284 | 885 | |
1cb9cd50 | 886 | /* The bind placeholder pretends to be an RV for now. |
c6f8b1d0 | 887 | Also it's marked as "can't upgrade" to stop anyone using it before it's |
1cb9cd50 NC |
888 | implemented. */ |
889 | { 0, 0, 0, SVt_BIND, TRUE, NONV, NOARENA, 0 }, | |
890 | ||
db93c0c4 NC |
891 | /* IVs are in the head, so the allocation size is 0. */ |
892 | { 0, | |
d2a0f284 | 893 | sizeof(IV), /* This is used to copy out the IV body. */ |
10666ae3 | 894 | STRUCT_OFFSET(XPVIV, xiv_iv), SVt_IV, FALSE, NONV, |
db93c0c4 | 895 | NOARENA /* IVS don't need an arena */, 0 |
d2a0f284 JC |
896 | }, |
897 | ||
bd81e77b | 898 | /* 8 bytes on most ILP32 with IEEE doubles */ |
6e128786 NC |
899 | { sizeof(NV), sizeof(NV), |
900 | STRUCT_OFFSET(XPVNV, xnv_u), | |
901 | SVt_NV, FALSE, HADNV, HASARENA, FIT_ARENA(0, sizeof(NV)) }, | |
d2a0f284 | 902 | |
bd81e77b | 903 | /* 8 bytes on most ILP32 with IEEE doubles */ |
bc337e5c | 904 | { sizeof(XPV) - STRUCT_OFFSET(XPV, xpv_cur), |
889d28b2 NC |
905 | copy_length(XPV, xpv_len) - STRUCT_OFFSET(XPV, xpv_cur), |
906 | + STRUCT_OFFSET(XPV, xpv_cur), | |
69ba284b | 907 | SVt_PV, FALSE, NONV, HASARENA, |
889d28b2 | 908 | FIT_ARENA(0, sizeof(XPV) - STRUCT_OFFSET(XPV, xpv_cur)) }, |
d2a0f284 | 909 | |
bd81e77b | 910 | /* 12 */ |
bc337e5c | 911 | { sizeof(XPVIV) - STRUCT_OFFSET(XPV, xpv_cur), |
889d28b2 NC |
912 | copy_length(XPVIV, xiv_u) - STRUCT_OFFSET(XPV, xpv_cur), |
913 | + STRUCT_OFFSET(XPV, xpv_cur), | |
914 | SVt_PVIV, FALSE, NONV, HASARENA, | |
915 | FIT_ARENA(0, sizeof(XPVIV) - STRUCT_OFFSET(XPV, xpv_cur)) }, | |
d2a0f284 | 916 | |
889d28b2 | 917 | /* 20 */ |
bc337e5c | 918 | { sizeof(XPVNV) - STRUCT_OFFSET(XPV, xpv_cur), |
889d28b2 NC |
919 | copy_length(XPVNV, xnv_u) - STRUCT_OFFSET(XPV, xpv_cur), |
920 | + STRUCT_OFFSET(XPV, xpv_cur), | |
921 | SVt_PVNV, FALSE, HADNV, HASARENA, | |
922 | FIT_ARENA(0, sizeof(XPVNV) - STRUCT_OFFSET(XPV, xpv_cur)) }, | |
d2a0f284 | 923 | |
bd81e77b | 924 | /* 28 */ |
6e128786 | 925 | { sizeof(XPVMG), copy_length(XPVMG, xnv_u), 0, SVt_PVMG, FALSE, HADNV, |
d2a0f284 | 926 | HASARENA, FIT_ARENA(0, sizeof(XPVMG)) }, |
4df7f6af | 927 | |
288b8c02 | 928 | /* something big */ |
601dfd0a NC |
929 | { sizeof(regexp), |
930 | sizeof(regexp), | |
931 | 0, | |
08e44740 | 932 | SVt_REGEXP, FALSE, NONV, HASARENA, |
eaeb1e7f | 933 | FIT_ARENA(0, sizeof(regexp)) |
5c35adbb | 934 | }, |
4df7f6af | 935 | |
bd81e77b | 936 | /* 48 */ |
10666ae3 | 937 | { sizeof(XPVGV), sizeof(XPVGV), 0, SVt_PVGV, TRUE, HADNV, |
d2a0f284 JC |
938 | HASARENA, FIT_ARENA(0, sizeof(XPVGV)) }, |
939 | ||
bd81e77b | 940 | /* 64 */ |
10666ae3 | 941 | { sizeof(XPVLV), sizeof(XPVLV), 0, SVt_PVLV, TRUE, HADNV, |
d2a0f284 JC |
942 | HASARENA, FIT_ARENA(0, sizeof(XPVLV)) }, |
943 | ||
601dfd0a | 944 | { sizeof(XPVAV), |
4f7003f5 | 945 | copy_length(XPVAV, xav_alloc), |
601dfd0a | 946 | 0, |
69ba284b | 947 | SVt_PVAV, TRUE, NONV, HASARENA, |
601dfd0a | 948 | FIT_ARENA(0, sizeof(XPVAV)) }, |
d2a0f284 | 949 | |
601dfd0a | 950 | { sizeof(XPVHV), |
359164a0 | 951 | copy_length(XPVHV, xhv_max), |
601dfd0a | 952 | 0, |
69ba284b | 953 | SVt_PVHV, TRUE, NONV, HASARENA, |
601dfd0a | 954 | FIT_ARENA(0, sizeof(XPVHV)) }, |
d2a0f284 | 955 | |
c84c4652 | 956 | /* 56 */ |
601dfd0a NC |
957 | { sizeof(XPVCV), |
958 | sizeof(XPVCV), | |
959 | 0, | |
69ba284b | 960 | SVt_PVCV, TRUE, NONV, HASARENA, |
601dfd0a | 961 | FIT_ARENA(0, sizeof(XPVCV)) }, |
69ba284b | 962 | |
601dfd0a NC |
963 | { sizeof(XPVFM), |
964 | sizeof(XPVFM), | |
965 | 0, | |
69ba284b | 966 | SVt_PVFM, TRUE, NONV, NOARENA, |
601dfd0a | 967 | FIT_ARENA(20, sizeof(XPVFM)) }, |
d2a0f284 JC |
968 | |
969 | /* XPVIO is 84 bytes, fits 48x */ | |
601dfd0a NC |
970 | { sizeof(XPVIO), |
971 | sizeof(XPVIO), | |
972 | 0, | |
b6f60916 | 973 | SVt_PVIO, TRUE, NONV, HASARENA, |
601dfd0a | 974 | FIT_ARENA(24, sizeof(XPVIO)) }, |
bd81e77b | 975 | }; |
29489e7c | 976 | |
bd81e77b | 977 | #define new_body_allocated(sv_type) \ |
d2a0f284 | 978 | (void *)((char *)S_new_body(aTHX_ sv_type) \ |
bd81e77b | 979 | - bodies_by_type[sv_type].offset) |
29489e7c | 980 | |
26359cfa NC |
981 | /* return a thing to the free list */ |
982 | ||
983 | #define del_body(thing, root) \ | |
984 | STMT_START { \ | |
985 | void ** const thing_copy = (void **)thing; \ | |
986 | *thing_copy = *root; \ | |
987 | *root = (void*)thing_copy; \ | |
988 | } STMT_END | |
29489e7c | 989 | |
bd81e77b | 990 | #ifdef PURIFY |
29489e7c | 991 | |
beeec492 NC |
992 | #define new_XNV() safemalloc(sizeof(XPVNV)) |
993 | #define new_XPVNV() safemalloc(sizeof(XPVNV)) | |
994 | #define new_XPVMG() safemalloc(sizeof(XPVMG)) | |
29489e7c | 995 | |
beeec492 | 996 | #define del_XPVGV(p) safefree(p) |
29489e7c | 997 | |
bd81e77b | 998 | #else /* !PURIFY */ |
29489e7c | 999 | |
65ac1738 | 1000 | #define new_XNV() new_body_allocated(SVt_NV) |
65ac1738 | 1001 | #define new_XPVNV() new_body_allocated(SVt_PVNV) |
65ac1738 | 1002 | #define new_XPVMG() new_body_allocated(SVt_PVMG) |
645c22ef | 1003 | |
26359cfa NC |
1004 | #define del_XPVGV(p) del_body(p + bodies_by_type[SVt_PVGV].offset, \ |
1005 | &PL_body_roots[SVt_PVGV]) | |
1d7c1841 | 1006 | |
bd81e77b | 1007 | #endif /* PURIFY */ |
93e68bfb | 1008 | |
bd81e77b | 1009 | /* no arena for you! */ |
93e68bfb | 1010 | |
bd81e77b | 1011 | #define new_NOARENA(details) \ |
beeec492 | 1012 | safemalloc((details)->body_size + (details)->offset) |
bd81e77b | 1013 | #define new_NOARENAZ(details) \ |
beeec492 | 1014 | safecalloc((details)->body_size + (details)->offset, 1) |
d2a0f284 | 1015 | |
1e30fcd5 NC |
1016 | void * |
1017 | Perl_more_bodies (pTHX_ const svtype sv_type, const size_t body_size, | |
1018 | const size_t arena_size) | |
d2a0f284 JC |
1019 | { |
1020 | dVAR; | |
1021 | void ** const root = &PL_body_roots[sv_type]; | |
99816f8d NC |
1022 | struct arena_desc *adesc; |
1023 | struct arena_set *aroot = (struct arena_set *) PL_body_arenas; | |
1024 | unsigned int curr; | |
d2a0f284 JC |
1025 | char *start; |
1026 | const char *end; | |
02982131 | 1027 | const size_t good_arena_size = Perl_malloc_good_size(arena_size); |
0b2d3faa | 1028 | #if defined(DEBUGGING) && !defined(PERL_GLOBAL_STRUCT_PRIVATE) |
23e9d66c NC |
1029 | static bool done_sanity_check; |
1030 | ||
0b2d3faa JH |
1031 | /* PERL_GLOBAL_STRUCT_PRIVATE cannot coexist with global |
1032 | * variables like done_sanity_check. */ | |
10666ae3 | 1033 | if (!done_sanity_check) { |
ea471437 | 1034 | unsigned int i = SVt_LAST; |
10666ae3 NC |
1035 | |
1036 | done_sanity_check = TRUE; | |
1037 | ||
1038 | while (i--) | |
1039 | assert (bodies_by_type[i].type == i); | |
1040 | } | |
1041 | #endif | |
1042 | ||
02982131 | 1043 | assert(arena_size); |
23e9d66c | 1044 | |
99816f8d NC |
1045 | /* may need new arena-set to hold new arena */ |
1046 | if (!aroot || aroot->curr >= aroot->set_size) { | |
1047 | struct arena_set *newroot; | |
1048 | Newxz(newroot, 1, struct arena_set); | |
1049 | newroot->set_size = ARENAS_PER_SET; | |
1050 | newroot->next = aroot; | |
1051 | aroot = newroot; | |
1052 | PL_body_arenas = (void *) newroot; | |
1053 | DEBUG_m(PerlIO_printf(Perl_debug_log, "new arenaset %p\n", (void*)aroot)); | |
1054 | } | |
1055 | ||
1056 | /* ok, now have arena-set with at least 1 empty/available arena-desc */ | |
1057 | curr = aroot->curr++; | |
1058 | adesc = &(aroot->set[curr]); | |
1059 | assert(!adesc->arena); | |
1060 | ||
1061 | Newx(adesc->arena, good_arena_size, char); | |
1062 | adesc->size = good_arena_size; | |
1063 | adesc->utype = sv_type; | |
1064 | DEBUG_m(PerlIO_printf(Perl_debug_log, "arena %d added: %p size %"UVuf"\n", | |
1065 | curr, (void*)adesc->arena, (UV)good_arena_size)); | |
1066 | ||
1067 | start = (char *) adesc->arena; | |
d2a0f284 | 1068 | |
29657bb6 NC |
1069 | /* Get the address of the byte after the end of the last body we can fit. |
1070 | Remember, this is integer division: */ | |
02982131 | 1071 | end = start + good_arena_size / body_size * body_size; |
d2a0f284 | 1072 | |
486ec47a | 1073 | /* computed count doesn't reflect the 1st slot reservation */ |
d8fca402 NC |
1074 | #if defined(MYMALLOC) || defined(HAS_MALLOC_GOOD_SIZE) |
1075 | DEBUG_m(PerlIO_printf(Perl_debug_log, | |
1076 | "arena %p end %p arena-size %d (from %d) type %d " | |
1077 | "size %d ct %d\n", | |
02982131 NC |
1078 | (void*)start, (void*)end, (int)good_arena_size, |
1079 | (int)arena_size, sv_type, (int)body_size, | |
1080 | (int)good_arena_size / (int)body_size)); | |
d8fca402 | 1081 | #else |
d2a0f284 JC |
1082 | DEBUG_m(PerlIO_printf(Perl_debug_log, |
1083 | "arena %p end %p arena-size %d type %d size %d ct %d\n", | |
6c9570dc | 1084 | (void*)start, (void*)end, |
02982131 NC |
1085 | (int)arena_size, sv_type, (int)body_size, |
1086 | (int)good_arena_size / (int)body_size)); | |
d8fca402 | 1087 | #endif |
d2a0f284 JC |
1088 | *root = (void *)start; |
1089 | ||
29657bb6 NC |
1090 | while (1) { |
1091 | /* Where the next body would start: */ | |
d2a0f284 | 1092 | char * const next = start + body_size; |
29657bb6 NC |
1093 | |
1094 | if (next >= end) { | |
1095 | /* This is the last body: */ | |
1096 | assert(next == end); | |
1097 | ||
1098 | *(void **)start = 0; | |
1099 | return *root; | |
1100 | } | |
1101 | ||
d2a0f284 JC |
1102 | *(void**) start = (void *)next; |
1103 | start = next; | |
1104 | } | |
d2a0f284 JC |
1105 | } |
1106 | ||
1107 | /* grab a new thing from the free list, allocating more if necessary. | |
1108 | The inline version is used for speed in hot routines, and the | |
1109 | function using it serves the rest (unless PURIFY). | |
1110 | */ | |
1111 | #define new_body_inline(xpv, sv_type) \ | |
1112 | STMT_START { \ | |
1113 | void ** const r3wt = &PL_body_roots[sv_type]; \ | |
11b79775 | 1114 | xpv = (PTR_TBL_ENT_t*) (*((void **)(r3wt)) \ |
1e30fcd5 | 1115 | ? *((void **)(r3wt)) : Perl_more_bodies(aTHX_ sv_type, \ |
02982131 NC |
1116 | bodies_by_type[sv_type].body_size,\ |
1117 | bodies_by_type[sv_type].arena_size)); \ | |
d2a0f284 | 1118 | *(r3wt) = *(void**)(xpv); \ |
d2a0f284 JC |
1119 | } STMT_END |
1120 | ||
1121 | #ifndef PURIFY | |
1122 | ||
1123 | STATIC void * | |
de37a194 | 1124 | S_new_body(pTHX_ const svtype sv_type) |
d2a0f284 JC |
1125 | { |
1126 | dVAR; | |
1127 | void *xpv; | |
1128 | new_body_inline(xpv, sv_type); | |
1129 | return xpv; | |
1130 | } | |
1131 | ||
1132 | #endif | |
93e68bfb | 1133 | |
238b27b3 NC |
1134 | static const struct body_details fake_rv = |
1135 | { 0, 0, 0, SVt_IV, FALSE, NONV, NOARENA, 0 }; | |
1136 | ||
bd81e77b NC |
1137 | /* |
1138 | =for apidoc sv_upgrade | |
93e68bfb | 1139 | |
bd81e77b NC |
1140 | Upgrade an SV to a more complex form. Generally adds a new body type to the |
1141 | SV, then copies across as much information as possible from the old body. | |
1142 | You generally want to use the C<SvUPGRADE> macro wrapper. See also C<svtype>. | |
93e68bfb | 1143 | |
bd81e77b | 1144 | =cut |
93e68bfb | 1145 | */ |
93e68bfb | 1146 | |
bd81e77b | 1147 | void |
aad570aa | 1148 | Perl_sv_upgrade(pTHX_ register SV *const sv, svtype new_type) |
cac9b346 | 1149 | { |
97aff369 | 1150 | dVAR; |
bd81e77b NC |
1151 | void* old_body; |
1152 | void* new_body; | |
42d0e0b7 | 1153 | const svtype old_type = SvTYPE(sv); |
d2a0f284 | 1154 | const struct body_details *new_type_details; |
238b27b3 | 1155 | const struct body_details *old_type_details |
bd81e77b | 1156 | = bodies_by_type + old_type; |
4df7f6af | 1157 | SV *referant = NULL; |
cac9b346 | 1158 | |
7918f24d NC |
1159 | PERL_ARGS_ASSERT_SV_UPGRADE; |
1160 | ||
1776cbe8 NC |
1161 | if (old_type == new_type) |
1162 | return; | |
1163 | ||
1164 | /* This clause was purposefully added ahead of the early return above to | |
1165 | the shared string hackery for (sort {$a <=> $b} keys %hash), with the | |
1166 | inference by Nick I-S that it would fix other troublesome cases. See | |
1167 | changes 7162, 7163 (f130fd4589cf5fbb24149cd4db4137c8326f49c1 and parent) | |
1168 | ||
1169 | Given that shared hash key scalars are no longer PVIV, but PV, there is | |
1170 | no longer need to unshare so as to free up the IVX slot for its proper | |
1171 | purpose. So it's safe to move the early return earlier. */ | |
1172 | ||
bd81e77b NC |
1173 | if (new_type != SVt_PV && SvIsCOW(sv)) { |
1174 | sv_force_normal_flags(sv, 0); | |
1175 | } | |
cac9b346 | 1176 | |
bd81e77b | 1177 | old_body = SvANY(sv); |
de042e1d | 1178 | |
bd81e77b NC |
1179 | /* Copying structures onto other structures that have been neatly zeroed |
1180 | has a subtle gotcha. Consider XPVMG | |
cac9b346 | 1181 | |
bd81e77b NC |
1182 | +------+------+------+------+------+-------+-------+ |
1183 | | NV | CUR | LEN | IV | MAGIC | STASH | | |
1184 | +------+------+------+------+------+-------+-------+ | |
1185 | 0 4 8 12 16 20 24 28 | |
645c22ef | 1186 | |
bd81e77b NC |
1187 | where NVs are aligned to 8 bytes, so that sizeof that structure is |
1188 | actually 32 bytes long, with 4 bytes of padding at the end: | |
08742458 | 1189 | |
bd81e77b NC |
1190 | +------+------+------+------+------+-------+-------+------+ |
1191 | | NV | CUR | LEN | IV | MAGIC | STASH | ??? | | |
1192 | +------+------+------+------+------+-------+-------+------+ | |
1193 | 0 4 8 12 16 20 24 28 32 | |
08742458 | 1194 | |
bd81e77b | 1195 | so what happens if you allocate memory for this structure: |
30f9da9e | 1196 | |
bd81e77b NC |
1197 | +------+------+------+------+------+-------+-------+------+------+... |
1198 | | NV | CUR | LEN | IV | MAGIC | STASH | GP | NAME | | |
1199 | +------+------+------+------+------+-------+-------+------+------+... | |
1200 | 0 4 8 12 16 20 24 28 32 36 | |
bfc44f79 | 1201 | |
bd81e77b NC |
1202 | zero it, then copy sizeof(XPVMG) bytes on top of it? Not quite what you |
1203 | expect, because you copy the area marked ??? onto GP. Now, ??? may have | |
1204 | started out as zero once, but it's quite possible that it isn't. So now, | |
1205 | rather than a nicely zeroed GP, you have it pointing somewhere random. | |
1206 | Bugs ensue. | |
bfc44f79 | 1207 | |
bd81e77b NC |
1208 | (In fact, GP ends up pointing at a previous GP structure, because the |
1209 | principle cause of the padding in XPVMG getting garbage is a copy of | |
6c9e42f7 NC |
1210 | sizeof(XPVMG) bytes from a XPVGV structure in sv_unglob. Right now |
1211 | this happens to be moot because XPVGV has been re-ordered, with GP | |
1212 | no longer after STASH) | |
30f9da9e | 1213 | |
bd81e77b NC |
1214 | So we are careful and work out the size of used parts of all the |
1215 | structures. */ | |
bfc44f79 | 1216 | |
bd81e77b NC |
1217 | switch (old_type) { |
1218 | case SVt_NULL: | |
1219 | break; | |
1220 | case SVt_IV: | |
4df7f6af NC |
1221 | if (SvROK(sv)) { |
1222 | referant = SvRV(sv); | |
238b27b3 NC |
1223 | old_type_details = &fake_rv; |
1224 | if (new_type == SVt_NV) | |
1225 | new_type = SVt_PVNV; | |
4df7f6af NC |
1226 | } else { |
1227 | if (new_type < SVt_PVIV) { | |
1228 | new_type = (new_type == SVt_NV) | |
1229 | ? SVt_PVNV : SVt_PVIV; | |
1230 | } | |
bd81e77b NC |
1231 | } |
1232 | break; | |
1233 | case SVt_NV: | |
1234 | if (new_type < SVt_PVNV) { | |
1235 | new_type = SVt_PVNV; | |
bd81e77b NC |
1236 | } |
1237 | break; | |
bd81e77b NC |
1238 | case SVt_PV: |
1239 | assert(new_type > SVt_PV); | |
1240 | assert(SVt_IV < SVt_PV); | |
1241 | assert(SVt_NV < SVt_PV); | |
1242 | break; | |
1243 | case SVt_PVIV: | |
1244 | break; | |
1245 | case SVt_PVNV: | |
1246 | break; | |
1247 | case SVt_PVMG: | |
1248 | /* Because the XPVMG of PL_mess_sv isn't allocated from the arena, | |
1249 | there's no way that it can be safely upgraded, because perl.c | |
1250 | expects to Safefree(SvANY(PL_mess_sv)) */ | |
1251 | assert(sv != PL_mess_sv); | |
1252 | /* This flag bit is used to mean other things in other scalar types. | |
1253 | Given that it only has meaning inside the pad, it shouldn't be set | |
1254 | on anything that can get upgraded. */ | |
00b1698f | 1255 | assert(!SvPAD_TYPED(sv)); |
bd81e77b NC |
1256 | break; |
1257 | default: | |
1258 | if (old_type_details->cant_upgrade) | |
c81225bc NC |
1259 | Perl_croak(aTHX_ "Can't upgrade %s (%" UVuf ") to %" UVuf, |
1260 | sv_reftype(sv, 0), (UV) old_type, (UV) new_type); | |
bd81e77b | 1261 | } |
3376de98 NC |
1262 | |
1263 | if (old_type > new_type) | |
1264 | Perl_croak(aTHX_ "sv_upgrade from type %d down to type %d", | |
1265 | (int)old_type, (int)new_type); | |
1266 | ||
2fa1109b | 1267 | new_type_details = bodies_by_type + new_type; |
645c22ef | 1268 | |
bd81e77b NC |
1269 | SvFLAGS(sv) &= ~SVTYPEMASK; |
1270 | SvFLAGS(sv) |= new_type; | |
932e9ff9 | 1271 | |
ab4416c0 NC |
1272 | /* This can't happen, as SVt_NULL is <= all values of new_type, so one of |
1273 | the return statements above will have triggered. */ | |
1274 | assert (new_type != SVt_NULL); | |
bd81e77b | 1275 | switch (new_type) { |
bd81e77b NC |
1276 | case SVt_IV: |
1277 | assert(old_type == SVt_NULL); | |
1278 | SvANY(sv) = (XPVIV*)((char*)&(sv->sv_u.svu_iv) - STRUCT_OFFSET(XPVIV, xiv_iv)); | |
1279 | SvIV_set(sv, 0); | |
1280 | return; | |
1281 | case SVt_NV: | |
1282 | assert(old_type == SVt_NULL); | |
1283 | SvANY(sv) = new_XNV(); | |
1284 | SvNV_set(sv, 0); | |
1285 | return; | |
bd81e77b | 1286 | case SVt_PVHV: |
bd81e77b | 1287 | case SVt_PVAV: |
d2a0f284 | 1288 | assert(new_type_details->body_size); |
c1ae03ae NC |
1289 | |
1290 | #ifndef PURIFY | |
1291 | assert(new_type_details->arena); | |
d2a0f284 | 1292 | assert(new_type_details->arena_size); |
c1ae03ae | 1293 | /* This points to the start of the allocated area. */ |
d2a0f284 JC |
1294 | new_body_inline(new_body, new_type); |
1295 | Zero(new_body, new_type_details->body_size, char); | |
c1ae03ae NC |
1296 | new_body = ((char *)new_body) - new_type_details->offset; |
1297 | #else | |
1298 | /* We always allocated the full length item with PURIFY. To do this | |
1299 | we fake things so that arena is false for all 16 types.. */ | |
1300 | new_body = new_NOARENAZ(new_type_details); | |
1301 | #endif | |
1302 | SvANY(sv) = new_body; | |
1303 | if (new_type == SVt_PVAV) { | |
1304 | AvMAX(sv) = -1; | |
1305 | AvFILLp(sv) = -1; | |
1306 | AvREAL_only(sv); | |
64484faa | 1307 | if (old_type_details->body_size) { |
ac572bf4 NC |
1308 | AvALLOC(sv) = 0; |
1309 | } else { | |
1310 | /* It will have been zeroed when the new body was allocated. | |
1311 | Lets not write to it, in case it confuses a write-back | |
1312 | cache. */ | |
1313 | } | |
78ac7dd9 NC |
1314 | } else { |
1315 | assert(!SvOK(sv)); | |
1316 | SvOK_off(sv); | |
1317 | #ifndef NODEFAULT_SHAREKEYS | |
1318 | HvSHAREKEYS_on(sv); /* key-sharing on by default */ | |
1319 | #endif | |
1320 | HvMAX(sv) = 7; /* (start with 8 buckets) */ | |
c1ae03ae | 1321 | } |
aeb18a1e | 1322 | |
bd81e77b NC |
1323 | /* SVt_NULL isn't the only thing upgraded to AV or HV. |
1324 | The target created by newSVrv also is, and it can have magic. | |
1325 | However, it never has SvPVX set. | |
1326 | */ | |
4df7f6af NC |
1327 | if (old_type == SVt_IV) { |
1328 | assert(!SvROK(sv)); | |
1329 | } else if (old_type >= SVt_PV) { | |
bd81e77b NC |
1330 | assert(SvPVX_const(sv) == 0); |
1331 | } | |
aeb18a1e | 1332 | |
bd81e77b | 1333 | if (old_type >= SVt_PVMG) { |
e736a858 | 1334 | SvMAGIC_set(sv, ((XPVMG*)old_body)->xmg_u.xmg_magic); |
bd81e77b | 1335 | SvSTASH_set(sv, ((XPVMG*)old_body)->xmg_stash); |
797c7171 NC |
1336 | } else { |
1337 | sv->sv_u.svu_array = NULL; /* or svu_hash */ | |
bd81e77b NC |
1338 | } |
1339 | break; | |
93e68bfb | 1340 | |
93e68bfb | 1341 | |
b9ad13ac NC |
1342 | case SVt_REGEXP: |
1343 | /* This ensures that SvTHINKFIRST(sv) is true, and hence that | |
1344 | sv_force_normal_flags(sv) is called. */ | |
1345 | SvFAKE_on(sv); | |
bd81e77b NC |
1346 | case SVt_PVIV: |
1347 | /* XXX Is this still needed? Was it ever needed? Surely as there is | |
1348 | no route from NV to PVIV, NOK can never be true */ | |
1349 | assert(!SvNOKp(sv)); | |
1350 | assert(!SvNOK(sv)); | |
1351 | case SVt_PVIO: | |
1352 | case SVt_PVFM: | |
bd81e77b NC |
1353 | case SVt_PVGV: |
1354 | case SVt_PVCV: | |
1355 | case SVt_PVLV: | |
1356 | case SVt_PVMG: | |
1357 | case SVt_PVNV: | |
1358 | case SVt_PV: | |
93e68bfb | 1359 | |
d2a0f284 | 1360 | assert(new_type_details->body_size); |
bd81e77b NC |
1361 | /* We always allocated the full length item with PURIFY. To do this |
1362 | we fake things so that arena is false for all 16 types.. */ | |
1363 | if(new_type_details->arena) { | |
1364 | /* This points to the start of the allocated area. */ | |
d2a0f284 JC |
1365 | new_body_inline(new_body, new_type); |
1366 | Zero(new_body, new_type_details->body_size, char); | |
bd81e77b NC |
1367 | new_body = ((char *)new_body) - new_type_details->offset; |
1368 | } else { | |
1369 | new_body = new_NOARENAZ(new_type_details); | |
1370 | } | |
1371 | SvANY(sv) = new_body; | |
5e2fc214 | 1372 | |
bd81e77b | 1373 | if (old_type_details->copy) { |
f9ba3d20 NC |
1374 | /* There is now the potential for an upgrade from something without |
1375 | an offset (PVNV or PVMG) to something with one (PVCV, PVFM) */ | |
1376 | int offset = old_type_details->offset; | |
1377 | int length = old_type_details->copy; | |
1378 | ||
1379 | if (new_type_details->offset > old_type_details->offset) { | |
d4c19fe8 | 1380 | const int difference |
f9ba3d20 NC |
1381 | = new_type_details->offset - old_type_details->offset; |
1382 | offset += difference; | |
1383 | length -= difference; | |
1384 | } | |
1385 | assert (length >= 0); | |
1386 | ||
1387 | Copy((char *)old_body + offset, (char *)new_body + offset, length, | |
1388 | char); | |
bd81e77b NC |
1389 | } |
1390 | ||
1391 | #ifndef NV_ZERO_IS_ALLBITS_ZERO | |
f2524eef | 1392 | /* If NV 0.0 is stores as all bits 0 then Zero() already creates a |
e5ce394c NC |
1393 | * correct 0.0 for us. Otherwise, if the old body didn't have an |
1394 | * NV slot, but the new one does, then we need to initialise the | |
1395 | * freshly created NV slot with whatever the correct bit pattern is | |
1396 | * for 0.0 */ | |
e22a937e NC |
1397 | if (old_type_details->zero_nv && !new_type_details->zero_nv |
1398 | && !isGV_with_GP(sv)) | |
bd81e77b | 1399 | SvNV_set(sv, 0); |
82048762 | 1400 | #endif |
5e2fc214 | 1401 | |
85dca89a NC |
1402 | if (new_type == SVt_PVIO) { |
1403 | IO * const io = MUTABLE_IO(sv); | |
d963bf01 | 1404 | GV *iogv = gv_fetchpvs("IO::File::", GV_ADD, SVt_PVHV); |
85dca89a NC |
1405 | |
1406 | SvOBJECT_on(io); | |
1407 | /* Clear the stashcache because a new IO could overrule a package | |
1408 | name */ | |
1409 | hv_clear(PL_stashcache); | |
1410 | ||
85dca89a | 1411 | SvSTASH_set(io, MUTABLE_HV(SvREFCNT_inc(GvHV(iogv)))); |
f2524eef | 1412 | IoPAGE_LEN(sv) = 60; |
85dca89a | 1413 | } |
4df7f6af NC |
1414 | if (old_type < SVt_PV) { |
1415 | /* referant will be NULL unless the old type was SVt_IV emulating | |
1416 | SVt_RV */ | |
1417 | sv->sv_u.svu_rv = referant; | |
1418 | } | |
bd81e77b NC |
1419 | break; |
1420 | default: | |
afd78fd5 JH |
1421 | Perl_croak(aTHX_ "panic: sv_upgrade to unknown type %lu", |
1422 | (unsigned long)new_type); | |
bd81e77b | 1423 | } |
73171d91 | 1424 | |
db93c0c4 | 1425 | if (old_type > SVt_IV) { |
bd81e77b | 1426 | #ifdef PURIFY |
beeec492 | 1427 | safefree(old_body); |
bd81e77b | 1428 | #else |
bc786448 GG |
1429 | /* Note that there is an assumption that all bodies of types that |
1430 | can be upgraded came from arenas. Only the more complex non- | |
1431 | upgradable types are allowed to be directly malloc()ed. */ | |
1432 | assert(old_type_details->arena); | |
bd81e77b NC |
1433 | del_body((void*)((char*)old_body + old_type_details->offset), |
1434 | &PL_body_roots[old_type]); | |
1435 | #endif | |
1436 | } | |
1437 | } | |
73171d91 | 1438 | |
bd81e77b NC |
1439 | /* |
1440 | =for apidoc sv_backoff | |
73171d91 | 1441 | |
bd81e77b NC |
1442 | Remove any string offset. You should normally use the C<SvOOK_off> macro |
1443 | wrapper instead. | |
73171d91 | 1444 | |
bd81e77b | 1445 | =cut |
73171d91 NC |
1446 | */ |
1447 | ||
bd81e77b | 1448 | int |
aad570aa | 1449 | Perl_sv_backoff(pTHX_ register SV *const sv) |
bd81e77b | 1450 | { |
69240efd | 1451 | STRLEN delta; |
7a4bba22 | 1452 | const char * const s = SvPVX_const(sv); |
7918f24d NC |
1453 | |
1454 | PERL_ARGS_ASSERT_SV_BACKOFF; | |
96a5add6 | 1455 | PERL_UNUSED_CONTEXT; |
7918f24d | 1456 | |
bd81e77b NC |
1457 | assert(SvOOK(sv)); |
1458 | assert(SvTYPE(sv) != SVt_PVHV); | |
1459 | assert(SvTYPE(sv) != SVt_PVAV); | |
7a4bba22 | 1460 | |
69240efd NC |
1461 | SvOOK_offset(sv, delta); |
1462 | ||
7a4bba22 NC |
1463 | SvLEN_set(sv, SvLEN(sv) + delta); |
1464 | SvPV_set(sv, SvPVX(sv) - delta); | |
1465 | Move(s, SvPVX(sv), SvCUR(sv)+1, char); | |
bd81e77b NC |
1466 | SvFLAGS(sv) &= ~SVf_OOK; |
1467 | return 0; | |
1468 | } | |
73171d91 | 1469 | |
bd81e77b NC |
1470 | /* |
1471 | =for apidoc sv_grow | |
73171d91 | 1472 | |
bd81e77b NC |
1473 | Expands the character buffer in the SV. If necessary, uses C<sv_unref> and |
1474 | upgrades the SV to C<SVt_PV>. Returns a pointer to the character buffer. | |
1475 | Use the C<SvGROW> wrapper instead. | |
93e68bfb | 1476 | |
bd81e77b NC |
1477 | =cut |
1478 | */ | |
93e68bfb | 1479 | |
bd81e77b | 1480 | char * |
aad570aa | 1481 | Perl_sv_grow(pTHX_ register SV *const sv, register STRLEN newlen) |
bd81e77b NC |
1482 | { |
1483 | register char *s; | |
93e68bfb | 1484 | |
7918f24d NC |
1485 | PERL_ARGS_ASSERT_SV_GROW; |
1486 | ||
5db06880 NC |
1487 | if (PL_madskills && newlen >= 0x100000) { |
1488 | PerlIO_printf(Perl_debug_log, | |
1489 | "Allocation too large: %"UVxf"\n", (UV)newlen); | |
1490 | } | |
bd81e77b NC |
1491 | #ifdef HAS_64K_LIMIT |
1492 | if (newlen >= 0x10000) { | |
1493 | PerlIO_printf(Perl_debug_log, | |
1494 | "Allocation too large: %"UVxf"\n", (UV)newlen); | |
1495 | my_exit(1); | |
1496 | } | |
1497 | #endif /* HAS_64K_LIMIT */ | |
1498 | if (SvROK(sv)) | |
1499 | sv_unref(sv); | |
1500 | if (SvTYPE(sv) < SVt_PV) { | |
1501 | sv_upgrade(sv, SVt_PV); | |
1502 | s = SvPVX_mutable(sv); | |
1503 | } | |
1504 | else if (SvOOK(sv)) { /* pv is offset? */ | |
1505 | sv_backoff(sv); | |
1506 | s = SvPVX_mutable(sv); | |
1507 | if (newlen > SvLEN(sv)) | |
1508 | newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */ | |
1509 | #ifdef HAS_64K_LIMIT | |
1510 | if (newlen >= 0x10000) | |
1511 | newlen = 0xFFFF; | |
1512 | #endif | |
1513 | } | |
1514 | else | |
1515 | s = SvPVX_mutable(sv); | |
aeb18a1e | 1516 | |
bd81e77b | 1517 | if (newlen > SvLEN(sv)) { /* need more room? */ |
f1200559 WH |
1518 | STRLEN minlen = SvCUR(sv); |
1519 | minlen += (minlen >> PERL_STRLEN_EXPAND_SHIFT) + 10; | |
1520 | if (newlen < minlen) | |
1521 | newlen = minlen; | |
aedff202 | 1522 | #ifndef Perl_safesysmalloc_size |
bd81e77b | 1523 | newlen = PERL_STRLEN_ROUNDUP(newlen); |
bd81e77b | 1524 | #endif |
98653f18 | 1525 | if (SvLEN(sv) && s) { |
10edeb5d | 1526 | s = (char*)saferealloc(s, newlen); |
bd81e77b NC |
1527 | } |
1528 | else { | |
10edeb5d | 1529 | s = (char*)safemalloc(newlen); |
bd81e77b NC |
1530 | if (SvPVX_const(sv) && SvCUR(sv)) { |
1531 | Move(SvPVX_const(sv), s, (newlen < SvCUR(sv)) ? newlen : SvCUR(sv), char); | |
1532 | } | |
1533 | } | |
1534 | SvPV_set(sv, s); | |
ca7c1a29 | 1535 | #ifdef Perl_safesysmalloc_size |
98653f18 NC |
1536 | /* Do this here, do it once, do it right, and then we will never get |
1537 | called back into sv_grow() unless there really is some growing | |
1538 | needed. */ | |
ca7c1a29 | 1539 | SvLEN_set(sv, Perl_safesysmalloc_size(s)); |
98653f18 | 1540 | #else |
bd81e77b | 1541 | SvLEN_set(sv, newlen); |
98653f18 | 1542 | #endif |
bd81e77b NC |
1543 | } |
1544 | return s; | |
1545 | } | |
aeb18a1e | 1546 | |
bd81e77b NC |
1547 | /* |
1548 | =for apidoc sv_setiv | |
932e9ff9 | 1549 | |
bd81e77b NC |
1550 | Copies an integer into the given SV, upgrading first if necessary. |
1551 | Does not handle 'set' magic. See also C<sv_setiv_mg>. | |
463ee0b2 | 1552 | |
bd81e77b NC |
1553 | =cut |
1554 | */ | |
463ee0b2 | 1555 | |
bd81e77b | 1556 | void |
aad570aa | 1557 | Perl_sv_setiv(pTHX_ register SV *const sv, const IV i) |
bd81e77b | 1558 | { |
97aff369 | 1559 | dVAR; |
7918f24d NC |
1560 | |
1561 | PERL_ARGS_ASSERT_SV_SETIV; | |
1562 | ||
bd81e77b NC |
1563 | SV_CHECK_THINKFIRST_COW_DROP(sv); |
1564 | switch (SvTYPE(sv)) { | |
1565 | case SVt_NULL: | |
bd81e77b | 1566 | case SVt_NV: |
3376de98 | 1567 | sv_upgrade(sv, SVt_IV); |
bd81e77b | 1568 | break; |
bd81e77b NC |
1569 | case SVt_PV: |
1570 | sv_upgrade(sv, SVt_PVIV); | |
1571 | break; | |
463ee0b2 | 1572 | |
bd81e77b | 1573 | case SVt_PVGV: |
6e592b3a BM |
1574 | if (!isGV_with_GP(sv)) |
1575 | break; | |
bd81e77b NC |
1576 | case SVt_PVAV: |
1577 | case SVt_PVHV: | |
1578 | case SVt_PVCV: | |
1579 | case SVt_PVFM: | |
1580 | case SVt_PVIO: | |
1581 | Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0), | |
1582 | OP_DESC(PL_op)); | |
42d0e0b7 | 1583 | default: NOOP; |
bd81e77b NC |
1584 | } |
1585 | (void)SvIOK_only(sv); /* validate number */ | |
1586 | SvIV_set(sv, i); | |
1587 | SvTAINT(sv); | |
1588 | } | |
932e9ff9 | 1589 | |
bd81e77b NC |
1590 | /* |
1591 | =for apidoc sv_setiv_mg | |
d33b2eba | 1592 | |
bd81e77b | 1593 | Like C<sv_setiv>, but also handles 'set' magic. |
1c846c1f | 1594 | |
bd81e77b NC |
1595 | =cut |
1596 | */ | |
d33b2eba | 1597 | |
bd81e77b | 1598 | void |
aad570aa | 1599 | Perl_sv_setiv_mg(pTHX_ register SV *const sv, const IV i) |
bd81e77b | 1600 | { |
7918f24d NC |
1601 | PERL_ARGS_ASSERT_SV_SETIV_MG; |
1602 | ||
bd81e77b NC |
1603 | sv_setiv(sv,i); |
1604 | SvSETMAGIC(sv); | |
1605 | } | |
727879eb | 1606 | |
bd81e77b NC |
1607 | /* |
1608 | =for apidoc sv_setuv | |
d33b2eba | 1609 | |
bd81e77b NC |
1610 | Copies an unsigned integer into the given SV, upgrading first if necessary. |
1611 | Does not handle 'set' magic. See also C<sv_setuv_mg>. | |
9b94d1dd | 1612 | |
bd81e77b NC |
1613 | =cut |
1614 | */ | |
d33b2eba | 1615 | |
bd81e77b | 1616 | void |
aad570aa | 1617 | Perl_sv_setuv(pTHX_ register SV *const sv, const UV u) |
bd81e77b | 1618 | { |
7918f24d NC |
1619 | PERL_ARGS_ASSERT_SV_SETUV; |
1620 | ||
bd81e77b NC |
1621 | /* With these two if statements: |
1622 | u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865 | |
d33b2eba | 1623 | |
bd81e77b NC |
1624 | without |
1625 | u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865 | |
1c846c1f | 1626 | |
bd81e77b NC |
1627 | If you wish to remove them, please benchmark to see what the effect is |
1628 | */ | |
1629 | if (u <= (UV)IV_MAX) { | |
1630 | sv_setiv(sv, (IV)u); | |
1631 | return; | |
1632 | } | |
1633 | sv_setiv(sv, 0); | |
1634 | SvIsUV_on(sv); | |
1635 | SvUV_set(sv, u); | |
1636 | } | |
d33b2eba | 1637 | |
bd81e77b NC |
1638 | /* |
1639 | =for apidoc sv_setuv_mg | |
727879eb | 1640 | |
bd81e77b | 1641 | Like C<sv_setuv>, but also handles 'set' magic. |
9b94d1dd | 1642 | |
bd81e77b NC |
1643 | =cut |
1644 | */ | |
5e2fc214 | 1645 | |
bd81e77b | 1646 | void |
aad570aa | 1647 | Perl_sv_setuv_mg(pTHX_ register SV *const sv, const UV u) |
bd81e77b | 1648 | { |
7918f24d NC |
1649 | PERL_ARGS_ASSERT_SV_SETUV_MG; |
1650 | ||
bd81e77b NC |
1651 | sv_setuv(sv,u); |
1652 | SvSETMAGIC(sv); | |
1653 | } | |
5e2fc214 | 1654 | |
954c1994 | 1655 | /* |
bd81e77b | 1656 | =for apidoc sv_setnv |
954c1994 | 1657 | |
bd81e77b NC |
1658 | Copies a double into the given SV, upgrading first if necessary. |
1659 | Does not handle 'set' magic. See also C<sv_setnv_mg>. | |
954c1994 GS |
1660 | |
1661 | =cut | |
1662 | */ | |
1663 | ||
63f97190 | 1664 | void |
aad570aa | 1665 | Perl_sv_setnv(pTHX_ register SV *const sv, const NV num) |
79072805 | 1666 | { |
97aff369 | 1667 | dVAR; |
7918f24d NC |
1668 | |
1669 | PERL_ARGS_ASSERT_SV_SETNV; | |
1670 | ||
bd81e77b NC |
1671 | SV_CHECK_THINKFIRST_COW_DROP(sv); |
1672 | switch (SvTYPE(sv)) { | |
79072805 | 1673 | case SVt_NULL: |
79072805 | 1674 | case SVt_IV: |
bd81e77b | 1675 | sv_upgrade(sv, SVt_NV); |
79072805 LW |
1676 | break; |
1677 | case SVt_PV: | |
79072805 | 1678 | case SVt_PVIV: |
bd81e77b | 1679 | sv_upgrade(sv, SVt_PVNV); |
79072805 | 1680 | break; |
bd4b1eb5 | 1681 | |
bd4b1eb5 | 1682 | case SVt_PVGV: |
6e592b3a BM |
1683 | if (!isGV_with_GP(sv)) |
1684 | break; | |
bd81e77b NC |
1685 | case SVt_PVAV: |
1686 | case SVt_PVHV: | |
79072805 | 1687 | case SVt_PVCV: |
bd81e77b NC |
1688 | case SVt_PVFM: |
1689 | case SVt_PVIO: | |
1690 | Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0), | |
94bbb3f4 | 1691 | OP_DESC(PL_op)); |
42d0e0b7 | 1692 | default: NOOP; |
2068cd4d | 1693 | } |
bd81e77b NC |
1694 | SvNV_set(sv, num); |
1695 | (void)SvNOK_only(sv); /* validate number */ | |
1696 | SvTAINT(sv); | |
79072805 LW |
1697 | } |
1698 | ||
645c22ef | 1699 | /* |
bd81e77b | 1700 | =for apidoc sv_setnv_mg |
645c22ef | 1701 | |
bd81e77b | 1702 | Like C<sv_setnv>, but also handles 'set' magic. |
645c22ef DM |
1703 | |
1704 | =cut | |
1705 | */ | |
1706 | ||
bd81e77b | 1707 | void |
aad570aa | 1708 | Perl_sv_setnv_mg(pTHX_ register SV *const sv, const NV num) |
79072805 | 1709 | { |
7918f24d NC |
1710 | PERL_ARGS_ASSERT_SV_SETNV_MG; |
1711 | ||
bd81e77b NC |
1712 | sv_setnv(sv,num); |
1713 | SvSETMAGIC(sv); | |
79072805 LW |
1714 | } |
1715 | ||
bd81e77b NC |
1716 | /* Print an "isn't numeric" warning, using a cleaned-up, |
1717 | * printable version of the offending string | |
1718 | */ | |
954c1994 | 1719 | |
bd81e77b | 1720 | STATIC void |
aad570aa | 1721 | S_not_a_number(pTHX_ SV *const sv) |
79072805 | 1722 | { |
97aff369 | 1723 | dVAR; |
bd81e77b NC |
1724 | SV *dsv; |
1725 | char tmpbuf[64]; | |
1726 | const char *pv; | |
94463019 | 1727 | |
7918f24d NC |
1728 | PERL_ARGS_ASSERT_NOT_A_NUMBER; |
1729 | ||
94463019 | 1730 | if (DO_UTF8(sv)) { |
84bafc02 | 1731 | dsv = newSVpvs_flags("", SVs_TEMP); |
94463019 JH |
1732 | pv = sv_uni_display(dsv, sv, 10, 0); |
1733 | } else { | |
1734 | char *d = tmpbuf; | |
551405c4 | 1735 | const char * const limit = tmpbuf + sizeof(tmpbuf) - 8; |
94463019 JH |
1736 | /* each *s can expand to 4 chars + "...\0", |
1737 | i.e. need room for 8 chars */ | |
ecdeb87c | 1738 | |
00b6aa41 AL |
1739 | const char *s = SvPVX_const(sv); |
1740 | const char * const end = s + SvCUR(sv); | |
1741 | for ( ; s < end && d < limit; s++ ) { | |
94463019 JH |
1742 | int ch = *s & 0xFF; |
1743 | if (ch & 128 && !isPRINT_LC(ch)) { | |
1744 | *d++ = 'M'; | |
1745 | *d++ = '-'; | |
1746 | ch &= 127; | |
1747 | } | |
1748 | if (ch == '\n') { | |
1749 | *d++ = '\\'; | |
1750 | *d++ = 'n'; | |
1751 | } | |
1752 | else if (ch == '\r') { | |
1753 | *d++ = '\\'; | |
1754 | *d++ = 'r'; | |
1755 | } | |
1756 | else if (ch == '\f') { | |
1757 | *d++ = '\\'; | |
1758 | *d++ = 'f'; | |
1759 | } | |
1760 | else if (ch == '\\') { | |
1761 | *d++ = '\\'; | |
1762 | *d++ = '\\'; | |
1763 | } | |
1764 | else if (ch == '\0') { | |
1765 | *d++ = '\\'; | |
1766 | *d++ = '0'; | |
1767 | } | |
1768 | else if (isPRINT_LC(ch)) | |
1769 | *d++ = ch; | |
1770 | else { | |
1771 | *d++ = '^'; | |
1772 | *d++ = toCTRL(ch); | |
1773 | } | |
1774 | } | |
1775 | if (s < end) { | |
1776 | *d++ = '.'; | |
1777 | *d++ = '.'; | |
1778 | *d++ = '.'; | |
1779 | } | |
1780 | *d = '\0'; | |
1781 | pv = tmpbuf; | |
a0d0e21e | 1782 | } |
a0d0e21e | 1783 | |
533c011a | 1784 | if (PL_op) |
9014280d | 1785 | Perl_warner(aTHX_ packWARN(WARN_NUMERIC), |
94463019 JH |
1786 | "Argument \"%s\" isn't numeric in %s", pv, |
1787 | OP_DESC(PL_op)); | |
a0d0e21e | 1788 | else |
9014280d | 1789 | Perl_warner(aTHX_ packWARN(WARN_NUMERIC), |
94463019 | 1790 | "Argument \"%s\" isn't numeric", pv); |
a0d0e21e LW |
1791 | } |
1792 | ||
c2988b20 NC |
1793 | /* |
1794 | =for apidoc looks_like_number | |
1795 | ||
645c22ef DM |
1796 | Test if the content of an SV looks like a number (or is a number). |
1797 | C<Inf> and C<Infinity> are treated as numbers (so will not issue a | |
1798 | non-numeric warning), even if your atof() doesn't grok them. | |
c2988b20 NC |
1799 | |
1800 | =cut | |
1801 | */ | |
1802 | ||
1803 | I32 | |
aad570aa | 1804 | Perl_looks_like_number(pTHX_ SV *const sv) |
c2988b20 | 1805 | { |
a3b680e6 | 1806 | register const char *sbegin; |
c2988b20 NC |
1807 | STRLEN len; |
1808 | ||
7918f24d NC |
1809 | PERL_ARGS_ASSERT_LOOKS_LIKE_NUMBER; |
1810 | ||
c2988b20 | 1811 | if (SvPOK(sv)) { |
3f7c398e | 1812 | sbegin = SvPVX_const(sv); |
c2988b20 NC |
1813 | len = SvCUR(sv); |
1814 | } | |
1815 | else if (SvPOKp(sv)) | |
83003860 | 1816 | sbegin = SvPV_const(sv, len); |
c2988b20 | 1817 | else |
e0ab1c0e | 1818 | return SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK); |
c2988b20 NC |
1819 | return grok_number(sbegin, len, NULL); |
1820 | } | |
25da4f38 | 1821 | |
19f6321d NC |
1822 | STATIC bool |
1823 | S_glob_2number(pTHX_ GV * const gv) | |
180488f8 NC |
1824 | { |
1825 | const U32 wasfake = SvFLAGS(gv) & SVf_FAKE; | |
1826 | SV *const buffer = sv_newmortal(); | |
1827 | ||
7918f24d NC |
1828 | PERL_ARGS_ASSERT_GLOB_2NUMBER; |
1829 | ||
180488f8 NC |
1830 | /* FAKE globs can get coerced, so need to turn this off temporarily if it |
1831 | is on. */ | |
1832 | SvFAKE_off(gv); | |
1833 | gv_efullname3(buffer, gv, "*"); | |
1834 | SvFLAGS(gv) |= wasfake; | |
1835 | ||
675c862f AL |
1836 | /* We know that all GVs stringify to something that is not-a-number, |
1837 | so no need to test that. */ | |
1838 | if (ckWARN(WARN_NUMERIC)) | |
1839 | not_a_number(buffer); | |
1840 | /* We just want something true to return, so that S_sv_2iuv_common | |
1841 | can tail call us and return true. */ | |
19f6321d | 1842 | return TRUE; |
675c862f AL |
1843 | } |
1844 | ||
25da4f38 IZ |
1845 | /* Actually, ISO C leaves conversion of UV to IV undefined, but |
1846 | until proven guilty, assume that things are not that bad... */ | |
1847 | ||
645c22ef DM |
1848 | /* |
1849 | NV_PRESERVES_UV: | |
1850 | ||
1851 | As 64 bit platforms often have an NV that doesn't preserve all bits of | |
28e5dec8 JH |
1852 | an IV (an assumption perl has been based on to date) it becomes necessary |
1853 | to remove the assumption that the NV always carries enough precision to | |
1854 | recreate the IV whenever needed, and that the NV is the canonical form. | |
1855 | Instead, IV/UV and NV need to be given equal rights. So as to not lose | |
645c22ef | 1856 | precision as a side effect of conversion (which would lead to insanity |
28e5dec8 JH |
1857 | and the dragon(s) in t/op/numconvert.t getting very angry) the intent is |
1858 | 1) to distinguish between IV/UV/NV slots that have cached a valid | |
1859 | conversion where precision was lost and IV/UV/NV slots that have a | |
1860 | valid conversion which has lost no precision | |
645c22ef | 1861 | 2) to ensure that if a numeric conversion to one form is requested that |
28e5dec8 JH |
1862 | would lose precision, the precise conversion (or differently |
1863 | imprecise conversion) is also performed and cached, to prevent | |
1864 | requests for different numeric formats on the same SV causing | |
1865 | lossy conversion chains. (lossless conversion chains are perfectly | |
1866 | acceptable (still)) | |
1867 | ||
1868 | ||
1869 | flags are used: | |
1870 | SvIOKp is true if the IV slot contains a valid value | |
1871 | SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true) | |
1872 | SvNOKp is true if the NV slot contains a valid value | |
1873 | SvNOK is true only if the NV value is accurate | |
1874 | ||
1875 | so | |
645c22ef | 1876 | while converting from PV to NV, check to see if converting that NV to an |
28e5dec8 JH |
1877 | IV(or UV) would lose accuracy over a direct conversion from PV to |
1878 | IV(or UV). If it would, cache both conversions, return NV, but mark | |
1879 | SV as IOK NOKp (ie not NOK). | |
1880 | ||
645c22ef | 1881 | While converting from PV to IV, check to see if converting that IV to an |
28e5dec8 JH |
1882 | NV would lose accuracy over a direct conversion from PV to NV. If it |
1883 | would, cache both conversions, flag similarly. | |
1884 | ||
1885 | Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite | |
1886 | correctly because if IV & NV were set NV *always* overruled. | |
645c22ef DM |
1887 | Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flag's meaning |
1888 | changes - now IV and NV together means that the two are interchangeable: | |
28e5dec8 | 1889 | SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX; |
d460ef45 | 1890 | |
645c22ef DM |
1891 | The benefit of this is that operations such as pp_add know that if |
1892 | SvIOK is true for both left and right operands, then integer addition | |
1893 | can be used instead of floating point (for cases where the result won't | |
1894 | overflow). Before, floating point was always used, which could lead to | |
28e5dec8 JH |
1895 | loss of precision compared with integer addition. |
1896 | ||
1897 | * making IV and NV equal status should make maths accurate on 64 bit | |
1898 | platforms | |
1899 | * may speed up maths somewhat if pp_add and friends start to use | |
645c22ef | 1900 | integers when possible instead of fp. (Hopefully the overhead in |
28e5dec8 JH |
1901 | looking for SvIOK and checking for overflow will not outweigh the |
1902 | fp to integer speedup) | |
1903 | * will slow down integer operations (callers of SvIV) on "inaccurate" | |
1904 | values, as the change from SvIOK to SvIOKp will cause a call into | |
1905 | sv_2iv each time rather than a macro access direct to the IV slot | |
1906 | * should speed up number->string conversion on integers as IV is | |
645c22ef | 1907 | favoured when IV and NV are equally accurate |
28e5dec8 JH |
1908 | |
1909 | #################################################################### | |
645c22ef DM |
1910 | You had better be using SvIOK_notUV if you want an IV for arithmetic: |
1911 | SvIOK is true if (IV or UV), so you might be getting (IV)SvUV. | |
1912 | On the other hand, SvUOK is true iff UV. | |
28e5dec8 JH |
1913 | #################################################################### |
1914 | ||
645c22ef | 1915 | Your mileage will vary depending your CPU's relative fp to integer |
28e5dec8 JH |
1916 | performance ratio. |
1917 | */ | |
1918 | ||
1919 | #ifndef NV_PRESERVES_UV | |
645c22ef DM |
1920 | # define IS_NUMBER_UNDERFLOW_IV 1 |
1921 | # define IS_NUMBER_UNDERFLOW_UV 2 | |
1922 | # define IS_NUMBER_IV_AND_UV 2 | |
1923 | # define IS_NUMBER_OVERFLOW_IV 4 | |
1924 | # define IS_NUMBER_OVERFLOW_UV 5 | |
1925 | ||
1926 | /* sv_2iuv_non_preserve(): private routine for use by sv_2iv() and sv_2uv() */ | |
28e5dec8 JH |
1927 | |
1928 | /* For sv_2nv these three cases are "SvNOK and don't bother casting" */ | |
1929 | STATIC int | |
5de3775c | 1930 | S_sv_2iuv_non_preserve(pTHX_ register SV *const sv |
47031da6 NC |
1931 | # ifdef DEBUGGING |
1932 | , I32 numtype | |
1933 | # endif | |
1934 | ) | |
28e5dec8 | 1935 | { |
97aff369 | 1936 | dVAR; |
7918f24d NC |
1937 | |
1938 | PERL_ARGS_ASSERT_SV_2IUV_NON_PRESERVE; | |
1939 | ||
3f7c398e | 1940 | 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 |
1941 | if (SvNVX(sv) < (NV)IV_MIN) { |
1942 | (void)SvIOKp_on(sv); | |
1943 | (void)SvNOK_on(sv); | |
45977657 | 1944 | SvIV_set(sv, IV_MIN); |
28e5dec8 JH |
1945 | return IS_NUMBER_UNDERFLOW_IV; |
1946 | } | |
1947 | if (SvNVX(sv) > (NV)UV_MAX) { | |
1948 | (void)SvIOKp_on(sv); | |
1949 | (void)SvNOK_on(sv); | |
1950 | SvIsUV_on(sv); | |
607fa7f2 | 1951 | SvUV_set(sv, UV_MAX); |
28e5dec8 JH |
1952 | return IS_NUMBER_OVERFLOW_UV; |
1953 | } | |
c2988b20 NC |
1954 | (void)SvIOKp_on(sv); |
1955 | (void)SvNOK_on(sv); | |
1956 | /* Can't use strtol etc to convert this string. (See truth table in | |
1957 | sv_2iv */ | |
1958 | if (SvNVX(sv) <= (UV)IV_MAX) { | |
45977657 | 1959 | SvIV_set(sv, I_V(SvNVX(sv))); |
c2988b20 NC |
1960 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) { |
1961 | SvIOK_on(sv); /* Integer is precise. NOK, IOK */ | |
1962 | } else { | |
1963 | /* Integer is imprecise. NOK, IOKp */ | |
1964 | } | |
1965 | return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV; | |
1966 | } | |
1967 | SvIsUV_on(sv); | |
607fa7f2 | 1968 | SvUV_set(sv, U_V(SvNVX(sv))); |
c2988b20 NC |
1969 | if ((NV)(SvUVX(sv)) == SvNVX(sv)) { |
1970 | if (SvUVX(sv) == UV_MAX) { | |
1971 | /* As we know that NVs don't preserve UVs, UV_MAX cannot | |
1972 | possibly be preserved by NV. Hence, it must be overflow. | |
1973 | NOK, IOKp */ | |
1974 | return IS_NUMBER_OVERFLOW_UV; | |
1975 | } | |
1976 | SvIOK_on(sv); /* Integer is precise. NOK, UOK */ | |
1977 | } else { | |
1978 | /* Integer is imprecise. NOK, IOKp */ | |
28e5dec8 | 1979 | } |
c2988b20 | 1980 | return IS_NUMBER_OVERFLOW_IV; |
28e5dec8 | 1981 | } |
645c22ef DM |
1982 | #endif /* !NV_PRESERVES_UV*/ |
1983 | ||
af359546 | 1984 | STATIC bool |
7918f24d NC |
1985 | S_sv_2iuv_common(pTHX_ SV *const sv) |
1986 | { | |
97aff369 | 1987 | dVAR; |
7918f24d NC |
1988 | |
1989 | PERL_ARGS_ASSERT_SV_2IUV_COMMON; | |
1990 | ||
af359546 | 1991 | if (SvNOKp(sv)) { |
28e5dec8 JH |
1992 | /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv |
1993 | * without also getting a cached IV/UV from it at the same time | |
1994 | * (ie PV->NV conversion should detect loss of accuracy and cache | |
af359546 NC |
1995 | * IV or UV at same time to avoid this. */ |
1996 | /* IV-over-UV optimisation - choose to cache IV if possible */ | |
25da4f38 IZ |
1997 | |
1998 | if (SvTYPE(sv) == SVt_NV) | |
1999 | sv_upgrade(sv, SVt_PVNV); | |
2000 | ||
28e5dec8 JH |
2001 | (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */ |
2002 | /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost | |
2003 | certainly cast into the IV range at IV_MAX, whereas the correct | |
2004 | answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary | |
2005 | cases go to UV */ | |
cab190d4 JD |
2006 | #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan) |
2007 | if (Perl_isnan(SvNVX(sv))) { | |
2008 | SvUV_set(sv, 0); | |
2009 | SvIsUV_on(sv); | |
fdbe6d7c | 2010 | return FALSE; |
cab190d4 | 2011 | } |
cab190d4 | 2012 | #endif |
28e5dec8 | 2013 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { |
45977657 | 2014 | SvIV_set(sv, I_V(SvNVX(sv))); |
28e5dec8 JH |
2015 | if (SvNVX(sv) == (NV) SvIVX(sv) |
2016 | #ifndef NV_PRESERVES_UV | |
2017 | && (((UV)1 << NV_PRESERVES_UV_BITS) > | |
2018 | (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv))) | |
2019 | /* Don't flag it as "accurately an integer" if the number | |
2020 | came from a (by definition imprecise) NV operation, and | |
2021 | we're outside the range of NV integer precision */ | |
2022 | #endif | |
2023 | ) { | |
a43d94f2 NC |
2024 | if (SvNOK(sv)) |
2025 | SvIOK_on(sv); /* Can this go wrong with rounding? NWC */ | |
2026 | else { | |
2027 | /* scalar has trailing garbage, eg "42a" */ | |
2028 | } | |
28e5dec8 | 2029 | DEBUG_c(PerlIO_printf(Perl_debug_log, |
7234c960 | 2030 | "0x%"UVxf" iv(%"NVgf" => %"IVdf") (precise)\n", |
28e5dec8 JH |
2031 | PTR2UV(sv), |
2032 | SvNVX(sv), | |
2033 | SvIVX(sv))); | |
2034 | ||
2035 | } else { | |
2036 | /* IV not precise. No need to convert from PV, as NV | |
2037 | conversion would already have cached IV if it detected | |
2038 | that PV->IV would be better than PV->NV->IV | |
2039 | flags already correct - don't set public IOK. */ | |
2040 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
7234c960 | 2041 | "0x%"UVxf" iv(%"NVgf" => %"IVdf") (imprecise)\n", |
28e5dec8 JH |
2042 | PTR2UV(sv), |
2043 | SvNVX(sv), | |
2044 | SvIVX(sv))); | |
2045 | } | |
2046 | /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN, | |
2047 | but the cast (NV)IV_MIN rounds to a the value less (more | |
2048 | negative) than IV_MIN which happens to be equal to SvNVX ?? | |
2049 | Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and | |
2050 | NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and | |
2051 | (NV)UVX == NVX are both true, but the values differ. :-( | |
2052 | Hopefully for 2s complement IV_MIN is something like | |
2053 | 0x8000000000000000 which will be exact. NWC */ | |
d460ef45 | 2054 | } |
25da4f38 | 2055 | else { |
607fa7f2 | 2056 | SvUV_set(sv, U_V(SvNVX(sv))); |
28e5dec8 JH |
2057 | if ( |
2058 | (SvNVX(sv) == (NV) SvUVX(sv)) | |
2059 | #ifndef NV_PRESERVES_UV | |
2060 | /* Make sure it's not 0xFFFFFFFFFFFFFFFF */ | |
2061 | /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */ | |
2062 | && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv)) | |
2063 | /* Don't flag it as "accurately an integer" if the number | |
2064 | came from a (by definition imprecise) NV operation, and | |
2065 | we're outside the range of NV integer precision */ | |
2066 | #endif | |
a43d94f2 | 2067 | && SvNOK(sv) |
28e5dec8 JH |
2068 | ) |
2069 | SvIOK_on(sv); | |
25da4f38 | 2070 | SvIsUV_on(sv); |
1c846c1f | 2071 | DEBUG_c(PerlIO_printf(Perl_debug_log, |
57def98f | 2072 | "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n", |
56431972 | 2073 | PTR2UV(sv), |
57def98f JH |
2074 | SvUVX(sv), |
2075 | SvUVX(sv))); | |
25da4f38 | 2076 | } |
748a9306 LW |
2077 | } |
2078 | else if (SvPOKp(sv) && SvLEN(sv)) { | |
c2988b20 | 2079 | UV value; |
504618e9 | 2080 | const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value); |
af359546 | 2081 | /* We want to avoid a possible problem when we cache an IV/ a UV which |
25da4f38 | 2082 | may be later translated to an NV, and the resulting NV is not |
c2988b20 NC |
2083 | the same as the direct translation of the initial string |
2084 | (eg 123.456 can shortcut to the IV 123 with atol(), but we must | |
2085 | be careful to ensure that the value with the .456 is around if the | |
2086 | NV value is requested in the future). | |
1c846c1f | 2087 | |
af359546 | 2088 | This means that if we cache such an IV/a UV, we need to cache the |
25da4f38 | 2089 | NV as well. Moreover, we trade speed for space, and do not |
28e5dec8 | 2090 | cache the NV if we are sure it's not needed. |
25da4f38 | 2091 | */ |
16b7a9a4 | 2092 | |
c2988b20 NC |
2093 | /* SVt_PVNV is one higher than SVt_PVIV, hence this order */ |
2094 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) | |
2095 | == IS_NUMBER_IN_UV) { | |
5e045b90 | 2096 | /* It's definitely an integer, only upgrade to PVIV */ |
28e5dec8 JH |
2097 | if (SvTYPE(sv) < SVt_PVIV) |
2098 | sv_upgrade(sv, SVt_PVIV); | |
f7bbb42a | 2099 | (void)SvIOK_on(sv); |
c2988b20 NC |
2100 | } else if (SvTYPE(sv) < SVt_PVNV) |
2101 | sv_upgrade(sv, SVt_PVNV); | |
28e5dec8 | 2102 | |
f2524eef | 2103 | /* If NVs preserve UVs then we only use the UV value if we know that |
c2988b20 NC |
2104 | we aren't going to call atof() below. If NVs don't preserve UVs |
2105 | then the value returned may have more precision than atof() will | |
2106 | return, even though value isn't perfectly accurate. */ | |
2107 | if ((numtype & (IS_NUMBER_IN_UV | |
2108 | #ifdef NV_PRESERVES_UV | |
2109 | | IS_NUMBER_NOT_INT | |
2110 | #endif | |
2111 | )) == IS_NUMBER_IN_UV) { | |
2112 | /* This won't turn off the public IOK flag if it was set above */ | |
2113 | (void)SvIOKp_on(sv); | |
2114 | ||
2115 | if (!(numtype & IS_NUMBER_NEG)) { | |
2116 | /* positive */; | |
2117 | if (value <= (UV)IV_MAX) { | |
45977657 | 2118 | SvIV_set(sv, (IV)value); |
c2988b20 | 2119 | } else { |
af359546 | 2120 | /* it didn't overflow, and it was positive. */ |
607fa7f2 | 2121 | SvUV_set(sv, value); |
c2988b20 NC |
2122 | SvIsUV_on(sv); |
2123 | } | |
2124 | } else { | |
2125 | /* 2s complement assumption */ | |
2126 | if (value <= (UV)IV_MIN) { | |
45977657 | 2127 | SvIV_set(sv, -(IV)value); |
c2988b20 NC |
2128 | } else { |
2129 | /* Too negative for an IV. This is a double upgrade, but | |
d1be9408 | 2130 | I'm assuming it will be rare. */ |
c2988b20 NC |
2131 | if (SvTYPE(sv) < SVt_PVNV) |
2132 | sv_upgrade(sv, SVt_PVNV); | |
2133 | SvNOK_on(sv); | |
2134 | SvIOK_off(sv); | |
2135 | SvIOKp_on(sv); | |
9d6ce603 | 2136 | SvNV_set(sv, -(NV)value); |
45977657 | 2137 | SvIV_set(sv, IV_MIN); |
c2988b20 NC |
2138 | } |
2139 | } | |
2140 | } | |
2141 | /* For !NV_PRESERVES_UV and IS_NUMBER_IN_UV and IS_NUMBER_NOT_INT we | |
2142 | will be in the previous block to set the IV slot, and the next | |
2143 | block to set the NV slot. So no else here. */ | |
2144 | ||
2145 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) | |
2146 | != IS_NUMBER_IN_UV) { | |
2147 | /* It wasn't an (integer that doesn't overflow the UV). */ | |
3f7c398e | 2148 | SvNV_set(sv, Atof(SvPVX_const(sv))); |
28e5dec8 | 2149 | |
c2988b20 NC |
2150 | if (! numtype && ckWARN(WARN_NUMERIC)) |
2151 | not_a_number(sv); | |
28e5dec8 | 2152 | |
65202027 | 2153 | #if defined(USE_LONG_DOUBLE) |
c2988b20 NC |
2154 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n", |
2155 | PTR2UV(sv), SvNVX(sv))); | |
65202027 | 2156 | #else |
1779d84d | 2157 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"NVgf")\n", |
c2988b20 | 2158 | PTR2UV(sv), SvNVX(sv))); |
65202027 | 2159 | #endif |
28e5dec8 | 2160 | |
28e5dec8 | 2161 | #ifdef NV_PRESERVES_UV |
af359546 NC |
2162 | (void)SvIOKp_on(sv); |
2163 | (void)SvNOK_on(sv); | |
2164 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
2165 | SvIV_set(sv, I_V(SvNVX(sv))); | |
2166 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) { | |
2167 | SvIOK_on(sv); | |
2168 | } else { | |
6f207bd3 | 2169 | NOOP; /* Integer is imprecise. NOK, IOKp */ |
af359546 NC |
2170 | } |
2171 | /* UV will not work better than IV */ | |
2172 | } else { | |
2173 | if (SvNVX(sv) > (NV)UV_MAX) { | |
2174 | SvIsUV_on(sv); | |
2175 | /* Integer is inaccurate. NOK, IOKp, is UV */ | |
2176 | SvUV_set(sv, UV_MAX); | |
af359546 NC |
2177 | } else { |
2178 | SvUV_set(sv, U_V(SvNVX(sv))); | |
2179 | /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs | |
2180 | NV preservse UV so can do correct comparison. */ | |
2181 | if ((NV)(SvUVX(sv)) == SvNVX(sv)) { | |
2182 | SvIOK_on(sv); | |
af359546 | 2183 | } else { |
6f207bd3 | 2184 | NOOP; /* Integer is imprecise. NOK, IOKp, is UV */ |
af359546 NC |
2185 | } |
2186 | } | |
4b0c9573 | 2187 | SvIsUV_on(sv); |
af359546 | 2188 | } |
28e5dec8 | 2189 | #else /* NV_PRESERVES_UV */ |
c2988b20 NC |
2190 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) |
2191 | == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) { | |
af359546 | 2192 | /* The IV/UV slot will have been set from value returned by |
c2988b20 NC |
2193 | grok_number above. The NV slot has just been set using |
2194 | Atof. */ | |
560b0c46 | 2195 | SvNOK_on(sv); |
c2988b20 NC |
2196 | assert (SvIOKp(sv)); |
2197 | } else { | |
2198 | if (((UV)1 << NV_PRESERVES_UV_BITS) > | |
2199 | U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) { | |
2200 | /* Small enough to preserve all bits. */ | |
2201 | (void)SvIOKp_on(sv); | |
2202 | SvNOK_on(sv); | |
45977657 | 2203 | SvIV_set(sv, I_V(SvNVX(sv))); |
c2988b20 NC |
2204 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) |
2205 | SvIOK_on(sv); | |
2206 | /* Assumption: first non-preserved integer is < IV_MAX, | |
2207 | this NV is in the preserved range, therefore: */ | |
2208 | if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv)) | |
2209 | < (UV)IV_MAX)) { | |
32fdb065 | 2210 | 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 |
2211 | } |
2212 | } else { | |
2213 | /* IN_UV NOT_INT | |
2214 | 0 0 already failed to read UV. | |
2215 | 0 1 already failed to read UV. | |
2216 | 1 0 you won't get here in this case. IV/UV | |
2217 | slot set, public IOK, Atof() unneeded. | |
2218 | 1 1 already read UV. | |
2219 | so there's no point in sv_2iuv_non_preserve() attempting | |
2220 | to use atol, strtol, strtoul etc. */ | |
47031da6 | 2221 | # ifdef DEBUGGING |
40a17c4c | 2222 | sv_2iuv_non_preserve (sv, numtype); |
47031da6 NC |
2223 | # else |
2224 | sv_2iuv_non_preserve (sv); | |
2225 | # endif | |
c2988b20 NC |
2226 | } |
2227 | } | |
28e5dec8 | 2228 | #endif /* NV_PRESERVES_UV */ |
a43d94f2 NC |
2229 | /* It might be more code efficient to go through the entire logic above |
2230 | and conditionally set with SvIOKp_on() rather than SvIOK(), but it | |
2231 | gets complex and potentially buggy, so more programmer efficient | |
2232 | to do it this way, by turning off the public flags: */ | |
2233 | if (!numtype) | |
2234 | SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK); | |
25da4f38 | 2235 | } |
af359546 NC |
2236 | } |
2237 | else { | |
675c862f | 2238 | if (isGV_with_GP(sv)) |
159b6efe | 2239 | return glob_2number(MUTABLE_GV(sv)); |
180488f8 | 2240 | |
af359546 NC |
2241 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
2242 | if (!PL_localizing && ckWARN(WARN_UNINITIALIZED)) | |
2243 | report_uninit(sv); | |
2244 | } | |
25da4f38 IZ |
2245 | if (SvTYPE(sv) < SVt_IV) |
2246 | /* Typically the caller expects that sv_any is not NULL now. */ | |
2247 | sv_upgrade(sv, SVt_IV); | |
af359546 NC |
2248 | /* Return 0 from the caller. */ |
2249 | return TRUE; | |
2250 | } | |
2251 | return FALSE; | |
2252 | } | |
2253 | ||
2254 | /* | |
2255 | =for apidoc sv_2iv_flags | |
2256 | ||
2257 | Return the integer value of an SV, doing any necessary string | |
2258 | conversion. If flags includes SV_GMAGIC, does an mg_get() first. | |
2259 | Normally used via the C<SvIV(sv)> and C<SvIVx(sv)> macros. | |
2260 | ||
2261 | =cut | |
2262 | */ | |
2263 | ||
2264 | IV | |
5de3775c | 2265 | Perl_sv_2iv_flags(pTHX_ register SV *const sv, const I32 flags) |
af359546 | 2266 | { |
97aff369 | 2267 | dVAR; |
af359546 | 2268 | if (!sv) |
a0d0e21e | 2269 | return 0; |
cecf5685 NC |
2270 | if (SvGMAGICAL(sv) || (SvTYPE(sv) == SVt_PVGV && SvVALID(sv))) { |
2271 | /* FBMs use the same flag bit as SVf_IVisUV, so must let them | |
50caf62e NC |
2272 | cache IVs just in case. In practice it seems that they never |
2273 | actually anywhere accessible by user Perl code, let alone get used | |
2274 | in anything other than a string context. */ | |
af359546 NC |
2275 | if (flags & SV_GMAGIC) |
2276 | mg_get(sv); | |
2277 | if (SvIOKp(sv)) | |
2278 | return SvIVX(sv); | |
2279 | if (SvNOKp(sv)) { | |
2280 | return I_V(SvNVX(sv)); | |
2281 | } | |
71c558c3 NC |
2282 | if (SvPOKp(sv) && SvLEN(sv)) { |
2283 | UV value; | |
2284 | const int numtype | |
2285 | = grok_number(SvPVX_const(sv), SvCUR(sv), &value); | |
2286 | ||
2287 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) | |
2288 | == IS_NUMBER_IN_UV) { | |
2289 | /* It's definitely an integer */ | |
2290 | if (numtype & IS_NUMBER_NEG) { | |
2291 | if (value < (UV)IV_MIN) | |
2292 | return -(IV)value; | |
2293 | } else { | |
2294 | if (value < (UV)IV_MAX) | |
2295 | return (IV)value; | |
2296 | } | |
2297 | } | |
2298 | if (!numtype) { | |
2299 | if (ckWARN(WARN_NUMERIC)) | |
2300 | not_a_number(sv); | |
2301 | } | |
2302 | return I_V(Atof(SvPVX_const(sv))); | |
2303 | } | |
1c7ff15e NC |
2304 | if (SvROK(sv)) { |
2305 | goto return_rok; | |
af359546 | 2306 | } |
1c7ff15e NC |
2307 | assert(SvTYPE(sv) >= SVt_PVMG); |
2308 | /* This falls through to the report_uninit inside S_sv_2iuv_common. */ | |
4cb1ec55 | 2309 | } else if (SvTHINKFIRST(sv)) { |
af359546 | 2310 | if (SvROK(sv)) { |
1c7ff15e | 2311 | return_rok: |
af359546 | 2312 | if (SvAMAGIC(sv)) { |
aee036bb DM |
2313 | SV * tmpstr; |
2314 | if (flags & SV_SKIP_OVERLOAD) | |
2315 | return 0; | |
31d632c3 | 2316 | tmpstr = AMG_CALLunary(sv, numer_amg); |
af359546 NC |
2317 | if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) { |
2318 | return SvIV(tmpstr); | |
2319 | } | |
2320 | } | |
2321 | return PTR2IV(SvRV(sv)); | |
2322 | } | |
2323 | if (SvIsCOW(sv)) { | |
2324 | sv_force_normal_flags(sv, 0); | |
2325 | } | |
2326 | if (SvREADONLY(sv) && !SvOK(sv)) { | |
2327 | if (ckWARN(WARN_UNINITIALIZED)) | |
2328 | report_uninit(sv); | |
2329 | return 0; | |
2330 | } | |
2331 | } | |
2332 | if (!SvIOKp(sv)) { | |
2333 | if (S_sv_2iuv_common(aTHX_ sv)) | |
2334 | return 0; | |
79072805 | 2335 | } |
1d7c1841 GS |
2336 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n", |
2337 | PTR2UV(sv),SvIVX(sv))); | |
25da4f38 | 2338 | return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv); |
79072805 LW |
2339 | } |
2340 | ||
645c22ef | 2341 | /* |
891f9566 | 2342 | =for apidoc sv_2uv_flags |
645c22ef DM |
2343 | |
2344 | Return the unsigned integer value of an SV, doing any necessary string | |
891f9566 YST |
2345 | conversion. If flags includes SV_GMAGIC, does an mg_get() first. |
2346 | Normally used via the C<SvUV(sv)> and C<SvUVx(sv)> macros. | |
645c22ef DM |
2347 | |
2348 | =cut | |
2349 | */ | |
2350 | ||
ff68c719 | 2351 | UV |
5de3775c | 2352 | Perl_sv_2uv_flags(pTHX_ register SV *const sv, const I32 flags) |
ff68c719 | 2353 | { |
97aff369 | 2354 | dVAR; |
ff68c719 | 2355 | if (!sv) |
2356 | return 0; | |
cecf5685 NC |
2357 | if (SvGMAGICAL(sv) || (SvTYPE(sv) == SVt_PVGV && SvVALID(sv))) { |
2358 | /* FBMs use the same flag bit as SVf_IVisUV, so must let them | |
50caf62e | 2359 | cache IVs just in case. */ |
891f9566 YST |
2360 | if (flags & SV_GMAGIC) |
2361 | mg_get(sv); | |
ff68c719 | 2362 | if (SvIOKp(sv)) |
2363 | return SvUVX(sv); | |
2364 | if (SvNOKp(sv)) | |
2365 | return U_V(SvNVX(sv)); | |
71c558c3 NC |
2366 | if (SvPOKp(sv) && SvLEN(sv)) { |
2367 | UV value; | |
2368 | const int numtype | |
2369 | = grok_number(SvPVX_const(sv), SvCUR(sv), &value); | |
2370 | ||
2371 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) | |
2372 | == IS_NUMBER_IN_UV) { | |
2373 | /* It's definitely an integer */ | |
2374 | if (!(numtype & IS_NUMBER_NEG)) | |
2375 | return value; | |
2376 | } | |
2377 | if (!numtype) { | |
2378 | if (ckWARN(WARN_NUMERIC)) | |
2379 | not_a_number(sv); | |
2380 | } | |
2381 | return U_V(Atof(SvPVX_const(sv))); | |
2382 | } | |
1c7ff15e NC |
2383 | if (SvROK(sv)) { |
2384 | goto return_rok; | |
3fe9a6f1 | 2385 | } |
1c7ff15e NC |
2386 | assert(SvTYPE(sv) >= SVt_PVMG); |
2387 | /* This falls through to the report_uninit inside S_sv_2iuv_common. */ | |
4cb1ec55 | 2388 | } else if (SvTHINKFIRST(sv)) { |
ff68c719 | 2389 | if (SvROK(sv)) { |
1c7ff15e | 2390 | return_rok: |
deb46114 | 2391 | if (SvAMAGIC(sv)) { |
aee036bb DM |
2392 | SV *tmpstr; |
2393 | if (flags & SV_SKIP_OVERLOAD) | |
2394 | return 0; | |
31d632c3 | 2395 | tmpstr = AMG_CALLunary(sv, numer_amg); |
deb46114 NC |
2396 | if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) { |
2397 | return SvUV(tmpstr); | |
2398 | } | |
2399 | } | |
2400 | return PTR2UV(SvRV(sv)); | |
ff68c719 | 2401 | } |
765f542d NC |
2402 | if (SvIsCOW(sv)) { |
2403 | sv_force_normal_flags(sv, 0); | |
8a818333 | 2404 | } |
0336b60e | 2405 | if (SvREADONLY(sv) && !SvOK(sv)) { |
0336b60e | 2406 | if (ckWARN(WARN_UNINITIALIZED)) |
29489e7c | 2407 | report_uninit(sv); |
ff68c719 | 2408 | return 0; |
2409 | } | |
2410 | } | |
af359546 NC |
2411 | if (!SvIOKp(sv)) { |
2412 | if (S_sv_2iuv_common(aTHX_ sv)) | |
2413 | return 0; | |
ff68c719 | 2414 | } |
25da4f38 | 2415 | |
1d7c1841 GS |
2416 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n", |
2417 | PTR2UV(sv),SvUVX(sv))); | |
25da4f38 | 2418 | return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv); |
ff68c719 | 2419 | } |
2420 | ||
645c22ef | 2421 | /* |
196007d1 | 2422 | =for apidoc sv_2nv_flags |
645c22ef DM |
2423 | |
2424 | Return the num value of an SV, doing any necessary string or integer | |
39d5de13 DM |
2425 | conversion. If flags includes SV_GMAGIC, does an mg_get() first. |
2426 | Normally used via the C<SvNV(sv)> and C<SvNVx(sv)> macros. | |
645c22ef DM |
2427 | |
2428 | =cut | |
2429 | */ | |
2430 | ||
65202027 | 2431 | NV |
39d5de13 | 2432 | Perl_sv_2nv_flags(pTHX_ register SV *const sv, const I32 flags) |
79072805 | 2433 | { |
97aff369 | 2434 | dVAR; |
79072805 LW |
2435 | if (!sv) |
2436 | return 0.0; | |
cecf5685 NC |
2437 | if (SvGMAGICAL(sv) || (SvTYPE(sv) == SVt_PVGV && SvVALID(sv))) { |
2438 | /* FBMs use the same flag bit as SVf_IVisUV, so must let them | |
50caf62e | 2439 | cache IVs just in case. */ |
39d5de13 DM |
2440 | if (flags & SV_GMAGIC) |
2441 | mg_get(sv); | |
463ee0b2 LW |
2442 | if (SvNOKp(sv)) |
2443 | return SvNVX(sv); | |
0aa395f8 | 2444 | if ((SvPOKp(sv) && SvLEN(sv)) && !SvIOKp(sv)) { |
041457d9 | 2445 | if (!SvIOKp(sv) && ckWARN(WARN_NUMERIC) && |
504618e9 | 2446 | !grok_number(SvPVX_const(sv), SvCUR(sv), NULL)) |
a0d0e21e | 2447 | not_a_number(sv); |
3f7c398e | 2448 | return Atof(SvPVX_const(sv)); |
a0d0e21e | 2449 | } |
25da4f38 | 2450 | if (SvIOKp(sv)) { |
1c846c1f | 2451 | if (SvIsUV(sv)) |
65202027 | 2452 | return (NV)SvUVX(sv); |
25da4f38 | 2453 | else |
65202027 | 2454 | return (NV)SvIVX(sv); |
47a72cb8 NC |
2455 | } |
2456 | if (SvROK(sv)) { | |
2457 | goto return_rok; | |
2458 | } | |
2459 | assert(SvTYPE(sv) >= SVt_PVMG); | |
2460 | /* This falls through to the report_uninit near the end of the | |
2461 | function. */ | |
2462 | } else if (SvTHINKFIRST(sv)) { | |
a0d0e21e | 2463 | if (SvROK(sv)) { |
47a72cb8 | 2464 | return_rok: |
deb46114 | 2465 | if (SvAMAGIC(sv)) { |
aee036bb DM |
2466 | SV *tmpstr; |
2467 | if (flags & SV_SKIP_OVERLOAD) | |
2468 | return 0; | |
31d632c3 | 2469 | tmpstr = AMG_CALLunary(sv, numer_amg); |
deb46114 NC |
2470 | if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) { |
2471 | return SvNV(tmpstr); | |
2472 | } | |
2473 | } | |
2474 | return PTR2NV(SvRV(sv)); | |
a0d0e21e | 2475 | } |
765f542d NC |
2476 | if (SvIsCOW(sv)) { |
2477 | sv_force_normal_flags(sv, 0); | |
8a818333 | 2478 | } |
0336b60e | 2479 | if (SvREADONLY(sv) && !SvOK(sv)) { |
599cee73 | 2480 | if (ckWARN(WARN_UNINITIALIZED)) |
29489e7c | 2481 | report_uninit(sv); |
ed6116ce LW |
2482 | return 0.0; |
2483 | } | |
79072805 LW |
2484 | } |
2485 | if (SvTYPE(sv) < SVt_NV) { | |
7e25a7e9 NC |
2486 | /* The logic to use SVt_PVNV if necessary is in sv_upgrade. */ |
2487 | sv_upgrade(sv, SVt_NV); | |
906f284f | 2488 | #ifdef USE_LONG_DOUBLE |
097ee67d | 2489 | DEBUG_c({ |
f93f4e46 | 2490 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
2491 | PerlIO_printf(Perl_debug_log, |
2492 | "0x%"UVxf" num(%" PERL_PRIgldbl ")\n", | |
2493 | PTR2UV(sv), SvNVX(sv)); | |
572bbb43 GS |
2494 | RESTORE_NUMERIC_LOCAL(); |
2495 | }); | |
65202027 | 2496 | #else |
572bbb43 | 2497 | DEBUG_c({ |
f93f4e46 | 2498 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1779d84d | 2499 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%"NVgf")\n", |
1d7c1841 | 2500 | PTR2UV(sv), SvNVX(sv)); |
097ee67d JH |
2501 | RESTORE_NUMERIC_LOCAL(); |
2502 | }); | |
572bbb43 | 2503 | #endif |
79072805 LW |
2504 | } |
2505 | else if (SvTYPE(sv) < SVt_PVNV) | |
2506 | sv_upgrade(sv, SVt_PVNV); | |
59d8ce62 NC |
2507 | if (SvNOKp(sv)) { |
2508 | return SvNVX(sv); | |
61604483 | 2509 | } |
59d8ce62 | 2510 | if (SvIOKp(sv)) { |
9d6ce603 | 2511 | SvNV_set(sv, SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv)); |
28e5dec8 | 2512 | #ifdef NV_PRESERVES_UV |
a43d94f2 NC |
2513 | if (SvIOK(sv)) |
2514 | SvNOK_on(sv); | |
2515 | else | |
2516 | SvNOKp_on(sv); | |
28e5dec8 JH |
2517 | #else |
2518 | /* Only set the public NV OK flag if this NV preserves the IV */ | |
2519 | /* Check it's not 0xFFFFFFFFFFFFFFFF */ | |
a43d94f2 NC |
2520 | if (SvIOK(sv) && |
2521 | SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv)))) | |
28e5dec8 JH |
2522 | : (SvIVX(sv) == I_V(SvNVX(sv)))) |
2523 | SvNOK_on(sv); | |
2524 | else | |
2525 | SvNOKp_on(sv); | |
2526 | #endif | |
93a17b20 | 2527 | } |
748a9306 | 2528 | else if (SvPOKp(sv) && SvLEN(sv)) { |
c2988b20 | 2529 | UV value; |
3f7c398e | 2530 | const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value); |
041457d9 | 2531 | if (!SvIOKp(sv) && !numtype && ckWARN(WARN_NUMERIC)) |
a0d0e21e | 2532 | not_a_number(sv); |
28e5dec8 | 2533 | #ifdef NV_PRESERVES_UV |
c2988b20 NC |
2534 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) |
2535 | == IS_NUMBER_IN_UV) { | |
5e045b90 | 2536 | /* It's definitely an integer */ |
9d6ce603 | 2537 | SvNV_set(sv, (numtype & IS_NUMBER_NEG) ? -(NV)value : (NV)value); |
c2988b20 | 2538 | } else |
3f7c398e | 2539 | SvNV_set(sv, Atof(SvPVX_const(sv))); |
a43d94f2 NC |
2540 | if (numtype) |
2541 | SvNOK_on(sv); | |
2542 | else | |
2543 | SvNOKp_on(sv); | |
28e5dec8 | 2544 | #else |
3f7c398e | 2545 | SvNV_set(sv, Atof(SvPVX_const(sv))); |
28e5dec8 JH |
2546 | /* Only set the public NV OK flag if this NV preserves the value in |
2547 | the PV at least as well as an IV/UV would. | |
2548 | Not sure how to do this 100% reliably. */ | |
2549 | /* if that shift count is out of range then Configure's test is | |
2550 | wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS == | |
2551 | UV_BITS */ | |
2552 | if (((UV)1 << NV_PRESERVES_UV_BITS) > | |
c2988b20 | 2553 | U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) { |
28e5dec8 | 2554 | SvNOK_on(sv); /* Definitely small enough to preserve all bits */ |
c2988b20 NC |
2555 | } else if (!(numtype & IS_NUMBER_IN_UV)) { |
2556 | /* Can't use strtol etc to convert this string, so don't try. | |
2557 | sv_2iv and sv_2uv will use the NV to convert, not the PV. */ | |
2558 | SvNOK_on(sv); | |
2559 | } else { | |
2560 | /* value has been set. It may not be precise. */ | |
2561 | if ((numtype & IS_NUMBER_NEG) && (value > (UV)IV_MIN)) { | |
2562 | /* 2s complement assumption for (UV)IV_MIN */ | |
2563 | SvNOK_on(sv); /* Integer is too negative. */ | |
2564 | } else { | |
2565 | SvNOKp_on(sv); | |
2566 | SvIOKp_on(sv); | |
6fa402ec | 2567 | |
c2988b20 | 2568 | if (numtype & IS_NUMBER_NEG) { |
45977657 | 2569 | SvIV_set(sv, -(IV)value); |
c2988b20 | 2570 | } else if (value <= (UV)IV_MAX) { |
45977657 | 2571 | SvIV_set(sv, (IV)value); |
c2988b20 | 2572 | } else { |
607fa7f2 | 2573 | SvUV_set(sv, value); |
c2988b20 NC |
2574 | SvIsUV_on(sv); |
2575 | } | |
2576 | ||
2577 | if (numtype & IS_NUMBER_NOT_INT) { | |
2578 | /* I believe that even if the original PV had decimals, | |
2579 | they are lost beyond the limit of the FP precision. | |
2580 | However, neither is canonical, so both only get p | |
2581 | flags. NWC, 2000/11/25 */ | |
2582 | /* Both already have p flags, so do nothing */ | |
2583 | } else { | |
66a1b24b | 2584 | const NV nv = SvNVX(sv); |
c2988b20 NC |
2585 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { |
2586 | if (SvIVX(sv) == I_V(nv)) { | |
2587 | SvNOK_on(sv); | |
c2988b20 | 2588 | } else { |
c2988b20 NC |
2589 | /* It had no "." so it must be integer. */ |
2590 | } | |
00b6aa41 | 2591 | SvIOK_on(sv); |
c2988b20 NC |
2592 | } else { |
2593 | /* between IV_MAX and NV(UV_MAX). | |
2594 | Could be slightly > UV_MAX */ | |
6fa402ec | 2595 | |
c2988b20 NC |
2596 | if (numtype & IS_NUMBER_NOT_INT) { |
2597 | /* UV and NV both imprecise. */ | |
2598 | } else { | |
66a1b24b | 2599 | const UV nv_as_uv = U_V(nv); |
c2988b20 NC |
2600 | |
2601 | if (value == nv_as_uv && SvUVX(sv) != UV_MAX) { | |
2602 | SvNOK_on(sv); | |
c2988b20 | 2603 | } |
00b6aa41 | 2604 | SvIOK_on(sv); |
c2988b20 NC |
2605 | } |
2606 | } | |
2607 | } | |
2608 | } | |
2609 | } | |
a43d94f2 NC |
2610 | /* It might be more code efficient to go through the entire logic above |
2611 | and conditionally set with SvNOKp_on() rather than SvNOK(), but it | |
2612 | gets complex and potentially buggy, so more programmer efficient | |
2613 | to do it this way, by turning off the public flags: */ | |
2614 | if (!numtype) | |
2615 | SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK); | |
28e5dec8 | 2616 | #endif /* NV_PRESERVES_UV */ |
93a17b20 | 2617 | } |
79072805 | 2618 | else { |
f7877b28 | 2619 | if (isGV_with_GP(sv)) { |
159b6efe | 2620 | glob_2number(MUTABLE_GV(sv)); |
180488f8 NC |
2621 | return 0.0; |
2622 | } | |
2623 | ||
041457d9 | 2624 | if (!PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP) && ckWARN(WARN_UNINITIALIZED)) |
29489e7c | 2625 | report_uninit(sv); |
7e25a7e9 NC |
2626 | assert (SvTYPE(sv) >= SVt_NV); |
2627 | /* Typically the caller expects that sv_any is not NULL now. */ | |
2628 | /* XXX Ilya implies that this is a bug in callers that assume this | |
2629 | and ideally should be fixed. */ | |
a0d0e21e | 2630 | return 0.0; |
79072805 | 2631 | } |
572bbb43 | 2632 | #if defined(USE_LONG_DOUBLE) |
097ee67d | 2633 | DEBUG_c({ |
f93f4e46 | 2634 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
2635 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n", |
2636 | PTR2UV(sv), SvNVX(sv)); | |
572bbb43 GS |
2637 | RESTORE_NUMERIC_LOCAL(); |
2638 | }); | |
65202027 | 2639 | #else |
572bbb43 | 2640 | DEBUG_c({ |
f93f4e46 | 2641 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1779d84d | 2642 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%"NVgf")\n", |
1d7c1841 | 2643 | PTR2UV(sv), SvNVX(sv)); |
097ee67d JH |
2644 | RESTORE_NUMERIC_LOCAL(); |
2645 | }); | |
572bbb43 | 2646 | #endif |
463ee0b2 | 2647 | return SvNVX(sv); |
79072805 LW |
2648 | } |
2649 | ||
800401ee JH |
2650 | /* |
2651 | =for apidoc sv_2num | |
2652 | ||
2653 | Return an SV with the numeric value of the source SV, doing any necessary | |
a196a5fa JH |
2654 | reference or overload conversion. You must use the C<SvNUM(sv)> macro to |
2655 | access this function. | |
800401ee JH |
2656 | |
2657 | =cut | |
2658 | */ | |
2659 | ||
2660 | SV * | |
5de3775c | 2661 | Perl_sv_2num(pTHX_ register SV *const sv) |
800401ee | 2662 | { |
7918f24d NC |
2663 | PERL_ARGS_ASSERT_SV_2NUM; |
2664 | ||
b9ee0594 RGS |
2665 | if (!SvROK(sv)) |
2666 | return sv; | |
800401ee | 2667 | if (SvAMAGIC(sv)) { |
31d632c3 | 2668 | SV * const tmpsv = AMG_CALLunary(sv, numer_amg); |
a02ec77a | 2669 | TAINT_IF(tmpsv && SvTAINTED(tmpsv)); |
800401ee JH |
2670 | if (tmpsv && (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv)))) |
2671 | return sv_2num(tmpsv); | |
2672 | } | |
2673 | return sv_2mortal(newSVuv(PTR2UV(SvRV(sv)))); | |
2674 | } | |
2675 | ||
645c22ef DM |
2676 | /* uiv_2buf(): private routine for use by sv_2pv_flags(): print an IV or |
2677 | * UV as a string towards the end of buf, and return pointers to start and | |
2678 | * end of it. | |
2679 | * | |
2680 | * We assume that buf is at least TYPE_CHARS(UV) long. | |
2681 | */ | |
2682 | ||
864dbfa3 | 2683 | static char * |
5de3775c | 2684 | S_uiv_2buf(char *const buf, const IV iv, UV uv, const int is_uv, char **const peob) |
25da4f38 | 2685 | { |
25da4f38 | 2686 | char *ptr = buf + TYPE_CHARS(UV); |
823a54a3 | 2687 | char * const ebuf = ptr; |
25da4f38 | 2688 | int sign; |
25da4f38 | 2689 | |
7918f24d NC |
2690 | PERL_ARGS_ASSERT_UIV_2BUF; |
2691 | ||
25da4f38 IZ |
2692 | if (is_uv) |
2693 | sign = 0; | |
2694 | else if (iv >= 0) { | |
2695 | uv = iv; | |
2696 | sign = 0; | |
2697 | } else { | |
2698 | uv = -iv; | |
2699 | sign = 1; | |
2700 | } | |
2701 | do { | |
eb160463 | 2702 | *--ptr = '0' + (char)(uv % 10); |
25da4f38 IZ |
2703 | } while (uv /= 10); |
2704 | if (sign) | |
2705 | *--ptr = '-'; | |
2706 | *peob = ebuf; | |
2707 | return ptr; | |
2708 | } | |
2709 | ||
645c22ef DM |
2710 | /* |
2711 | =for apidoc sv_2pv_flags | |
2712 | ||
ff276b08 | 2713 | Returns a pointer to the string value of an SV, and sets *lp to its length. |
645c22ef DM |
2714 | If flags includes SV_GMAGIC, does an mg_get() first. Coerces sv to a string |
2715 | if necessary. | |
2716 | Normally invoked via the C<SvPV_flags> macro. C<sv_2pv()> and C<sv_2pv_nomg> | |
2717 | usually end up here too. | |
2718 | ||
2719 | =cut | |
2720 | */ | |
2721 | ||
8d6d96c1 | 2722 | char * |
5de3775c | 2723 | Perl_sv_2pv_flags(pTHX_ register SV *const sv, STRLEN *const lp, const I32 flags) |
8d6d96c1 | 2724 | { |
97aff369 | 2725 | dVAR; |
79072805 | 2726 | register char *s; |
79072805 | 2727 | |
463ee0b2 | 2728 | if (!sv) { |
cdb061a3 NC |
2729 | if (lp) |
2730 | *lp = 0; | |
73d840c0 | 2731 | return (char *)""; |
463ee0b2 | 2732 | } |
8990e307 | 2733 | if (SvGMAGICAL(sv)) { |
8d6d96c1 HS |
2734 | if (flags & SV_GMAGIC) |
2735 | mg_get(sv); | |
463ee0b2 | 2736 | if (SvPOKp(sv)) { |
cdb061a3 NC |
2737 | if (lp) |
2738 | *lp = SvCUR(sv); | |
10516c54 NC |
2739 | if (flags & SV_MUTABLE_RETURN) |
2740 | return SvPVX_mutable(sv); | |
4d84ee25 NC |
2741 | if (flags & SV_CONST_RETURN) |
2742 | return (char *)SvPVX_const(sv); | |
463ee0b2 LW |
2743 | return SvPVX(sv); |
2744 | } | |
75dfc8ec NC |
2745 | if (SvIOKp(sv) || SvNOKp(sv)) { |
2746 | char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */ | |
75dfc8ec NC |
2747 | STRLEN len; |
2748 | ||
2749 | if (SvIOKp(sv)) { | |
e80fed9d | 2750 | len = SvIsUV(sv) |
d9fad198 JH |
2751 | ? my_snprintf(tbuf, sizeof(tbuf), "%"UVuf, (UV)SvUVX(sv)) |
2752 | : my_snprintf(tbuf, sizeof(tbuf), "%"IVdf, (IV)SvIVX(sv)); | |
29912d93 NC |
2753 | } else if(SvNVX(sv) == 0.0) { |
2754 | tbuf[0] = '0'; | |
2755 | tbuf[1] = 0; | |
2756 | len = 1; | |
75dfc8ec | 2757 | } else { |
e8ada2d0 NC |
2758 | Gconvert(SvNVX(sv), NV_DIG, 0, tbuf); |
2759 | len = strlen(tbuf); | |
75dfc8ec | 2760 | } |
b5b886f0 NC |
2761 | assert(!SvROK(sv)); |
2762 | { | |
75dfc8ec NC |
2763 | dVAR; |
2764 | ||
75dfc8ec NC |
2765 | SvUPGRADE(sv, SVt_PV); |
2766 | if (lp) | |
2767 | *lp = len; | |
2768 | s = SvGROW_mutable(sv, len + 1); | |
2769 | SvCUR_set(sv, len); | |
2770 | SvPOKp_on(sv); | |
10edeb5d | 2771 | return (char*)memcpy(s, tbuf, len + 1); |
75dfc8ec | 2772 | } |
463ee0b2 | 2773 | } |
1c7ff15e NC |
2774 | if (SvROK(sv)) { |
2775 | goto return_rok; | |
2776 | } | |
2777 | assert(SvTYPE(sv) >= SVt_PVMG); | |
2778 | /* This falls through to the report_uninit near the end of the | |
2779 | function. */ | |
2780 | } else if (SvTHINKFIRST(sv)) { | |
ed6116ce | 2781 | if (SvROK(sv)) { |
1c7ff15e | 2782 | return_rok: |
deb46114 | 2783 | if (SvAMAGIC(sv)) { |
aee036bb DM |
2784 | SV *tmpstr; |
2785 | if (flags & SV_SKIP_OVERLOAD) | |
2786 | return NULL; | |
31d632c3 | 2787 | tmpstr = AMG_CALLunary(sv, string_amg); |
a02ec77a | 2788 | TAINT_IF(tmpstr && SvTAINTED(tmpstr)); |
deb46114 NC |
2789 | if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) { |
2790 | /* Unwrap this: */ | |
2791 | /* char *pv = lp ? SvPV(tmpstr, *lp) : SvPV_nolen(tmpstr); | |
2792 | */ | |
2793 | ||
2794 | char *pv; | |
2795 | if ((SvFLAGS(tmpstr) & (SVf_POK)) == SVf_POK) { | |
2796 | if (flags & SV_CONST_RETURN) { | |
2797 | pv = (char *) SvPVX_const(tmpstr); | |
2798 | } else { | |
2799 | pv = (flags & SV_MUTABLE_RETURN) | |
2800 | ? SvPVX_mutable(tmpstr) : SvPVX(tmpstr); | |
2801 | } | |
2802 | if (lp) | |
2803 | *lp = SvCUR(tmpstr); | |
50adf7d2 | 2804 | } else { |
deb46114 | 2805 | pv = sv_2pv_flags(tmpstr, lp, flags); |
50adf7d2 | 2806 | } |
deb46114 NC |
2807 | if (SvUTF8(tmpstr)) |
2808 | SvUTF8_on(sv); | |
2809 | else | |
2810 | SvUTF8_off(sv); | |
2811 | return pv; | |
50adf7d2 | 2812 | } |
deb46114 NC |
2813 | } |
2814 | { | |
fafee734 NC |
2815 | STRLEN len; |
2816 | char *retval; | |
2817 | char *buffer; | |
d2c6dc5e | 2818 | SV *const referent = SvRV(sv); |
d8eae41e NC |
2819 | |
2820 | if (!referent) { | |
fafee734 NC |
2821 | len = 7; |
2822 | retval = buffer = savepvn("NULLREF", len); | |
5c35adbb | 2823 | } else if (SvTYPE(referent) == SVt_REGEXP) { |
d2c6dc5e | 2824 | REGEXP * const re = (REGEXP *)MUTABLE_PTR(referent); |
67d2d14d AB |
2825 | I32 seen_evals = 0; |
2826 | ||
2827 | assert(re); | |
2828 | ||
2829 | /* If the regex is UTF-8 we want the containing scalar to | |
2830 | have an UTF-8 flag too */ | |
2831 | if (RX_UTF8(re)) | |
2832 | SvUTF8_on(sv); | |
2833 | else | |
2834 | SvUTF8_off(sv); | |
2835 | ||
2836 | if ((seen_evals = RX_SEEN_EVALS(re))) | |
2837 | PL_reginterp_cnt += seen_evals; | |
2838 | ||
2839 | if (lp) | |
2840 | *lp = RX_WRAPLEN(re); | |
2841 | ||
2842 | return RX_WRAPPED(re); | |
d8eae41e NC |
2843 | } else { |
2844 | const char *const typestr = sv_reftype(referent, 0); | |
fafee734 NC |
2845 | const STRLEN typelen = strlen(typestr); |
2846 | UV addr = PTR2UV(referent); | |
2847 | const char *stashname = NULL; | |
2848 | STRLEN stashnamelen = 0; /* hush, gcc */ | |
2849 | const char *buffer_end; | |
d8eae41e | 2850 | |
d8eae41e | 2851 | if (SvOBJECT(referent)) { |
fafee734 NC |
2852 | const HEK *const name = HvNAME_HEK(SvSTASH(referent)); |
2853 | ||
2854 | if (name) { | |
2855 | stashname = HEK_KEY(name); | |
2856 | stashnamelen = HEK_LEN(name); | |
2857 | ||
2858 | if (HEK_UTF8(name)) { | |
2859 | SvUTF8_on(sv); | |
2860 | } else { | |
2861 | SvUTF8_off(sv); | |
2862 | } | |
2863 | } else { | |
2864 | stashname = "__ANON__"; | |
2865 | stashnamelen = 8; | |
2866 | } | |
2867 | len = stashnamelen + 1 /* = */ + typelen + 3 /* (0x */ | |
2868 | + 2 * sizeof(UV) + 2 /* )\0 */; | |
2869 | } else { | |
2870 | len = typelen + 3 /* (0x */ | |
2871 | + 2 * sizeof(UV) + 2 /* )\0 */; | |
d8eae41e | 2872 | } |
fafee734 NC |
2873 | |
2874 | Newx(buffer, len, char); | |
2875 | buffer_end = retval = buffer + len; | |
2876 | ||
2877 | /* Working backwards */ | |
2878 | *--retval = '\0'; | |
2879 | *--retval = ')'; | |
2880 | do { | |
2881 | *--retval = PL_hexdigit[addr & 15]; | |
2882 | } while (addr >>= 4); | |
2883 | *--retval = 'x'; | |
2884 | *--retval = '0'; | |
2885 | *--retval = '('; | |
2886 | ||
2887 | retval -= typelen; | |
2888 | memcpy(retval, typestr, typelen); | |
2889 | ||
2890 | if (stashname) { | |
2891 | *--retval = '='; | |
2892 | retval -= stashnamelen; | |
2893 | memcpy(retval, stashname, stashnamelen); | |
2894 | } | |
486ec47a | 2895 | /* retval may not necessarily have reached the start of the |
fafee734 NC |
2896 | buffer here. */ |
2897 | assert (retval >= buffer); | |
2898 | ||
2899 | len = buffer_end - retval - 1; /* -1 for that \0 */ | |
c080367d | 2900 | } |
042dae7a | 2901 | if (lp) |
fafee734 NC |
2902 | *lp = len; |
2903 | SAVEFREEPV(buffer); | |
2904 | return retval; | |
463ee0b2 | 2905 | } |
79072805 | 2906 | } |
0336b60e | 2907 | if (SvREADONLY(sv) && !SvOK(sv)) { |
cdb061a3 NC |
2908 | if (lp) |
2909 | *lp = 0; | |
9f621bb0 NC |
2910 | if (flags & SV_UNDEF_RETURNS_NULL) |
2911 | return NULL; | |
2912 | if (ckWARN(WARN_UNINITIALIZED)) | |
2913 | report_uninit(sv); | |
73d840c0 | 2914 | return (char *)""; |
79072805 | 2915 | } |
79072805 | 2916 | } |
28e5dec8 JH |
2917 | if (SvIOK(sv) || ((SvIOKp(sv) && !SvNOKp(sv)))) { |
2918 | /* I'm assuming that if both IV and NV are equally valid then | |
2919 | converting the IV is going to be more efficient */ | |
e1ec3a88 | 2920 | const U32 isUIOK = SvIsUV(sv); |
28e5dec8 JH |
2921 | char buf[TYPE_CHARS(UV)]; |
2922 | char *ebuf, *ptr; | |
97a130b8 | 2923 | STRLEN len; |
28e5dec8 JH |
2924 | |
2925 | if (SvTYPE(sv) < SVt_PVIV) | |
2926 | sv_upgrade(sv, SVt_PVIV); | |
4ea1d550 | 2927 | ptr = uiv_2buf(buf, SvIVX(sv), SvUVX(sv), isUIOK, &ebuf); |
97a130b8 | 2928 | len = ebuf - ptr; |
5902b6a9 | 2929 | /* inlined from sv_setpvn */ |
97a130b8 NC |
2930 | s = SvGROW_mutable(sv, len + 1); |
2931 | Move(ptr, s, len, char); | |
2932 | s += len; | |
28e5dec8 | 2933 | *s = '\0'; |
28e5dec8 JH |
2934 | } |
2935 | else if (SvNOKp(sv)) { | |
79072805 LW |
2936 | if (SvTYPE(sv) < SVt_PVNV) |
2937 | sv_upgrade(sv, SVt_PVNV); | |
29912d93 NC |
2938 | if (SvNVX(sv) == 0.0) { |
2939 | s = SvGROW_mutable(sv, 2); | |
2940 | *s++ = '0'; | |
2941 | *s = '\0'; | |
2942 | } else { | |
2943 | dSAVE_ERRNO; | |
2944 | /* The +20 is pure guesswork. Configure test needed. --jhi */ | |
2945 | s = SvGROW_mutable(sv, NV_DIG + 20); | |
2946 | /* some Xenix systems wipe out errno here */ | |
2d4389e4 | 2947 | Gconvert(SvNVX(sv), NV_DIG, 0, s); |
29912d93 NC |
2948 | RESTORE_ERRNO; |
2949 | while (*s) s++; | |
bbce6d69 | 2950 | } |
79072805 LW |
2951 | #ifdef hcx |
2952 | if (s[-1] == '.') | |
46fc3d4c | 2953 | *--s = '\0'; |
79072805 LW |
2954 | #endif |
2955 | } | |
79072805 | 2956 | else { |
8d1c3e26 NC |
2957 | if (isGV_with_GP(sv)) { |
2958 | GV *const gv = MUTABLE_GV(sv); | |
2959 | const U32 wasfake = SvFLAGS(gv) & SVf_FAKE; | |
2960 | SV *const buffer = sv_newmortal(); | |
2961 | ||
2962 | /* FAKE globs can get coerced, so need to turn this off temporarily | |
2963 | if it is on. */ | |
2964 | SvFAKE_off(gv); | |
2965 | gv_efullname3(buffer, gv, "*"); | |
2966 | SvFLAGS(gv) |= wasfake; | |
2967 | ||
1809c940 DM |
2968 | if (SvPOK(buffer)) { |
2969 | if (lp) { | |
2970 | *lp = SvCUR(buffer); | |
2971 | } | |
2972 | return SvPVX(buffer); | |
2973 | } | |
2974 | else { | |
2975 | if (lp) | |
2976 | *lp = 0; | |
2977 | return (char *)""; | |
8d1c3e26 | 2978 | } |
8d1c3e26 | 2979 | } |
180488f8 | 2980 | |
cdb061a3 | 2981 | if (lp) |
00b6aa41 | 2982 | *lp = 0; |
9f621bb0 NC |
2983 | if (flags & SV_UNDEF_RETURNS_NULL) |
2984 | return NULL; | |
2985 | if (!PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP) && ckWARN(WARN_UNINITIALIZED)) | |
2986 | report_uninit(sv); | |
25da4f38 IZ |
2987 | if (SvTYPE(sv) < SVt_PV) |
2988 | /* Typically the caller expects that sv_any is not NULL now. */ | |
2989 | sv_upgrade(sv, SVt_PV); | |
73d840c0 | 2990 | return (char *)""; |
79072805 | 2991 | } |
cdb061a3 | 2992 | { |
823a54a3 | 2993 | const STRLEN len = s - SvPVX_const(sv); |
cdb061a3 NC |
2994 | if (lp) |
2995 | *lp = len; | |
2996 | SvCUR_set(sv, len); | |
2997 | } | |
79072805 | 2998 | SvPOK_on(sv); |
1d7c1841 | 2999 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n", |
3f7c398e | 3000 | PTR2UV(sv),SvPVX_const(sv))); |
4d84ee25 NC |
3001 | if (flags & SV_CONST_RETURN) |
3002 | return (char *)SvPVX_const(sv); | |
10516c54 NC |
3003 | if (flags & SV_MUTABLE_RETURN) |
3004 | return SvPVX_mutable(sv); | |
463ee0b2 LW |
3005 | return SvPVX(sv); |
3006 | } | |
3007 | ||
645c22ef | 3008 | /* |
6050d10e JP |
3009 | =for apidoc sv_copypv |
3010 | ||
3011 | Copies a stringified representation of the source SV into the | |
3012 | destination SV. Automatically performs any necessary mg_get and | |
54f0641b | 3013 | coercion of numeric values into strings. Guaranteed to preserve |
2575c402 | 3014 | UTF8 flag even from overloaded objects. Similar in nature to |
54f0641b NIS |
3015 | sv_2pv[_flags] but operates directly on an SV instead of just the |
3016 | string. Mostly uses sv_2pv_flags to do its work, except when that | |
6050d10e JP |
3017 | would lose the UTF-8'ness of the PV. |
3018 | ||
3019 | =cut | |
3020 | */ | |
3021 | ||
3022 | void | |
5de3775c | 3023 | Perl_sv_copypv(pTHX_ SV *const dsv, register SV *const ssv) |
6050d10e | 3024 | { |
446eaa42 | 3025 | STRLEN len; |
53c1dcc0 | 3026 | const char * const s = SvPV_const(ssv,len); |
7918f24d NC |
3027 | |
3028 | PERL_ARGS_ASSERT_SV_COPYPV; | |
3029 | ||
cb50f42d | 3030 | sv_setpvn(dsv,s,len); |
446eaa42 | 3031 | if (SvUTF8(ssv)) |
cb50f42d | 3032 | SvUTF8_on(dsv); |
446eaa42 | 3033 | else |
cb50f42d | 3034 | SvUTF8_off(dsv); |
6050d10e JP |
3035 | } |
3036 | ||
3037 | /* | |
645c22ef DM |
3038 | =for apidoc sv_2pvbyte |
3039 | ||
3040 | Return a pointer to the byte-encoded representation of the SV, and set *lp | |
1e54db1a | 3041 | to its length. May cause the SV to be downgraded from UTF-8 as a |
645c22ef DM |
3042 | side-effect. |
3043 | ||
3044 | Usually accessed via the C<SvPVbyte> macro. | |
3045 | ||
3046 | =cut | |
3047 | */ | |
3048 | ||
7340a771 | 3049 | char * |
5de3775c | 3050 | Perl_sv_2pvbyte(pTHX_ register SV *const sv, STRLEN *const lp) |
7340a771 | 3051 | { |
7918f24d NC |
3052 | PERL_ARGS_ASSERT_SV_2PVBYTE; |
3053 | ||
71eb6d8c | 3054 | SvGETMAGIC(sv); |
0875d2fe | 3055 | sv_utf8_downgrade(sv,0); |
71eb6d8c | 3056 | return lp ? SvPV_nomg(sv,*lp) : SvPV_nomg_nolen(sv); |
7340a771 GS |
3057 | } |
3058 | ||
645c22ef | 3059 | /* |
035cbb0e RGS |
3060 | =for apidoc sv_2pvutf8 |
3061 | ||
3062 | Return a pointer to the UTF-8-encoded representation of the SV, and set *lp | |
3063 | to its length. May cause the SV to be upgraded to UTF-8 as a side-effect. | |
3064 | ||
3065 | Usually accessed via the C<SvPVutf8> macro. | |
3066 | ||
3067 | =cut | |
3068 | */ | |
645c22ef | 3069 | |
7340a771 | 3070 | char * |
7bc54cea | 3071 | Perl_sv_2pvutf8(pTHX_ register SV *const sv, STRLEN *const lp) |
7340a771 | 3072 | { |
7918f24d NC |
3073 | PERL_ARGS_ASSERT_SV_2PVUTF8; |
3074 | ||
035cbb0e RGS |
3075 | sv_utf8_upgrade(sv); |
3076 | return lp ? SvPV(sv,*lp) : SvPV_nolen(sv); | |
7340a771 | 3077 | } |
1c846c1f | 3078 | |
7ee2227d | 3079 | |
645c22ef DM |
3080 | /* |
3081 | =for apidoc sv_2bool | |
3082 | ||
06c841cf FC |
3083 | This macro is only used by sv_true() or its macro equivalent, and only if |
3084 | the latter's argument is neither SvPOK, SvIOK nor SvNOK. | |
3085 | It calls sv_2bool_flags with the SV_GMAGIC flag. | |
3086 | ||
3087 | =for apidoc sv_2bool_flags | |
3088 | ||
3089 | This function is only used by sv_true() and friends, and only if | |
3090 | the latter's argument is neither SvPOK, SvIOK nor SvNOK. If the flags | |
3091 | contain SV_GMAGIC, then it does an mg_get() first. | |
3092 | ||
645c22ef DM |
3093 | |
3094 | =cut | |
3095 | */ | |
3096 | ||
463ee0b2 | 3097 | bool |
06c841cf | 3098 | Perl_sv_2bool_flags(pTHX_ register SV *const sv, const I32 flags) |
463ee0b2 | 3099 | { |
97aff369 | 3100 | dVAR; |
7918f24d | 3101 | |
06c841cf | 3102 | PERL_ARGS_ASSERT_SV_2BOOL_FLAGS; |
7918f24d | 3103 | |
06c841cf | 3104 | if(flags & SV_GMAGIC) SvGETMAGIC(sv); |
463ee0b2 | 3105 | |
a0d0e21e LW |
3106 | if (!SvOK(sv)) |
3107 | return 0; | |
3108 | if (SvROK(sv)) { | |
fabdb6c0 | 3109 | if (SvAMAGIC(sv)) { |
31d632c3 | 3110 | SV * const tmpsv = AMG_CALLunary(sv, bool__amg); |
fabdb6c0 | 3111 | if (tmpsv && (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv)))) |
f2338a2e | 3112 | return cBOOL(SvTRUE(tmpsv)); |
fabdb6c0 AL |
3113 | } |
3114 | return SvRV(sv) != 0; | |
a0d0e21e | 3115 | } |
463ee0b2 | 3116 | if (SvPOKp(sv)) { |
53c1dcc0 AL |
3117 | register XPV* const Xpvtmp = (XPV*)SvANY(sv); |
3118 | if (Xpvtmp && | |
339049b0 | 3119 | (*sv->sv_u.svu_pv > '0' || |
11343788 | 3120 | Xpvtmp->xpv_cur > 1 || |
339049b0 | 3121 | (Xpvtmp->xpv_cur && *sv->sv_u.svu_pv != '0'))) |
463ee0b2 LW |
3122 | return 1; |
3123 | else | |
3124 | return 0; | |
3125 | } | |
3126 | else { | |
3127 | if (SvIOKp(sv)) | |
3128 | return SvIVX(sv) != 0; | |
3129 | else { | |
3130 | if (SvNOKp(sv)) | |
3131 | return SvNVX(sv) != 0.0; | |
180488f8 | 3132 | else { |
f7877b28 | 3133 | if (isGV_with_GP(sv)) |
180488f8 NC |
3134 | return TRUE; |
3135 | else | |
3136 | return FALSE; | |
3137 | } | |
463ee0b2 LW |
3138 | } |
3139 | } | |
79072805 LW |
3140 | } |
3141 | ||
c461cf8f JH |
3142 | /* |
3143 | =for apidoc sv_utf8_upgrade | |
3144 | ||
78ea37eb | 3145 | Converts the PV of an SV to its UTF-8-encoded form. |
645c22ef | 3146 | Forces the SV to string form if it is not already. |
2bbc8d55 | 3147 | Will C<mg_get> on C<sv> if appropriate. |
4411f3b6 | 3148 | Always sets the SvUTF8 flag to avoid future validity checks even |
2bbc8d55 SP |
3149 | if the whole string is the same in UTF-8 as not. |
3150 | Returns the number of bytes in the converted string | |
c461cf8f | 3151 | |
13a6c0e0 JH |
3152 | This is not as a general purpose byte encoding to Unicode interface: |
3153 | use the Encode extension for that. | |
3154 | ||
fe749c9a KW |
3155 | =for apidoc sv_utf8_upgrade_nomg |
3156 | ||
3157 | Like sv_utf8_upgrade, but doesn't do magic on C<sv> | |
3158 | ||
8d6d96c1 HS |
3159 | =for apidoc sv_utf8_upgrade_flags |
3160 | ||
78ea37eb | 3161 | Converts the PV of an SV to its UTF-8-encoded form. |
645c22ef | 3162 | Forces the SV to string form if it is not already. |
8d6d96c1 | 3163 | Always sets the SvUTF8 flag to avoid future validity checks even |
2bbc8d55 SP |
3164 | if all the bytes are invariant in UTF-8. If C<flags> has C<SV_GMAGIC> bit set, |
3165 | will C<mg_get> on C<sv> if appropriate, else not. | |
3166 | Returns the number of bytes in the converted string | |
3167 | C<sv_utf8_upgrade> and | |
8d6d96c1 HS |
3168 | C<sv_utf8_upgrade_nomg> are implemented in terms of this function. |
3169 | ||
13a6c0e0 JH |
3170 | This is not as a general purpose byte encoding to Unicode interface: |
3171 | use the Encode extension for that. | |
3172 | ||
8d6d96c1 | 3173 | =cut |
b3ab6785 KW |
3174 | |
3175 | The grow version is currently not externally documented. It adds a parameter, | |
3176 | extra, which is the number of unused bytes the string of 'sv' is guaranteed to | |
3177 | have free after it upon return. This allows the caller to reserve extra space | |
3178 | that it intends to fill, to avoid extra grows. | |
3179 | ||
3180 | Also externally undocumented for the moment is the flag SV_FORCE_UTF8_UPGRADE, | |
3181 | which can be used to tell this function to not first check to see if there are | |
3182 | any characters that are different in UTF-8 (variant characters) which would | |
3183 | force it to allocate a new string to sv, but to assume there are. Typically | |
3184 | this flag is used by a routine that has already parsed the string to find that | |
3185 | there are such characters, and passes this information on so that the work | |
3186 | doesn't have to be repeated. | |
3187 | ||
3188 | (One might think that the calling routine could pass in the position of the | |
3189 | first such variant, so it wouldn't have to be found again. But that is not the | |
3190 | case, because typically when the caller is likely to use this flag, it won't be | |
3191 | calling this routine unless it finds something that won't fit into a byte. | |
3192 | Otherwise it tries to not upgrade and just use bytes. But some things that | |
3193 | do fit into a byte are variants in utf8, and the caller may not have been | |
3194 | keeping track of these.) | |
3195 | ||
3196 | If the routine itself changes the string, it adds a trailing NUL. Such a NUL | |
3197 | isn't guaranteed due to having other routines do the work in some input cases, | |
3198 | or if the input is already flagged as being in utf8. | |
3199 | ||
3200 | The speed of this could perhaps be improved for many cases if someone wanted to | |
3201 | write a fast function that counts the number of variant characters in a string, | |
3202 | especially if it could return the position of the first one. | |
3203 | ||
8d6d96c1 HS |
3204 | */ |
3205 | ||
3206 | STRLEN | |
b3ab6785 | 3207 | Perl_sv_utf8_upgrade_flags_grow(pTHX_ register SV *const sv, const I32 flags, STRLEN extra) |
8d6d96c1 | 3208 | { |
97aff369 | 3209 | dVAR; |
7918f24d | 3210 | |
b3ab6785 | 3211 | PERL_ARGS_ASSERT_SV_UTF8_UPGRADE_FLAGS_GROW; |
7918f24d | 3212 | |
808c356f RGS |
3213 | if (sv == &PL_sv_undef) |
3214 | return 0; | |
e0e62c2a NIS |
3215 | if (!SvPOK(sv)) { |
3216 | STRLEN len = 0; | |
d52b7888 NC |
3217 | if (SvREADONLY(sv) && (SvPOKp(sv) || SvIOKp(sv) || SvNOKp(sv))) { |
3218 | (void) sv_2pv_flags(sv,&len, flags); | |
b3ab6785 KW |
3219 | if (SvUTF8(sv)) { |
3220 | if (extra) SvGROW(sv, SvCUR(sv) + extra); | |
d52b7888 | 3221 | return len; |
b3ab6785 | 3222 | } |
d52b7888 NC |
3223 | } else { |
3224 | (void) SvPV_force(sv,len); | |
3225 | } | |
e0e62c2a | 3226 | } |
4411f3b6 | 3227 | |
f5cee72b | 3228 | if (SvUTF8(sv)) { |
b3ab6785 | 3229 | if (extra) SvGROW(sv, SvCUR(sv) + extra); |
5fec3b1d | 3230 | return SvCUR(sv); |
f5cee72b | 3231 | } |
5fec3b1d | 3232 | |
765f542d NC |
3233 | if (SvIsCOW(sv)) { |
3234 | sv_force_normal_flags(sv, 0); | |
db42d148 NIS |
3235 | } |
3236 | ||
b3ab6785 | 3237 | if (PL_encoding && !(flags & SV_UTF8_NO_ENCODING)) { |
799ef3cb | 3238 | sv_recode_to_utf8(sv, PL_encoding); |
b3ab6785 KW |
3239 | if (extra) SvGROW(sv, SvCUR(sv) + extra); |
3240 | return SvCUR(sv); | |
3241 | } | |
3242 | ||
4e93345f KW |
3243 | if (SvCUR(sv) == 0) { |
3244 | if (extra) SvGROW(sv, extra); | |
3245 | } else { /* Assume Latin-1/EBCDIC */ | |
c4e7c712 | 3246 | /* This function could be much more efficient if we |
2bbc8d55 | 3247 | * had a FLAG in SVs to signal if there are any variant |
c4e7c712 | 3248 | * chars in the PV. Given that there isn't such a flag |
b3ab6785 KW |
3249 | * make the loop as fast as possible (although there are certainly ways |
3250 | * to speed this up, eg. through vectorization) */ | |
3251 | U8 * s = (U8 *) SvPVX_const(sv); | |
3252 | U8 * e = (U8 *) SvEND(sv); | |
3253 | U8 *t = s; | |
3254 | STRLEN two_byte_count = 0; | |
c4e7c712 | 3255 | |
b3ab6785 KW |
3256 | if (flags & SV_FORCE_UTF8_UPGRADE) goto must_be_utf8; |
3257 | ||
3258 | /* See if really will need to convert to utf8. We mustn't rely on our | |
3259 | * incoming SV being well formed and having a trailing '\0', as certain | |
3260 | * code in pp_formline can send us partially built SVs. */ | |
3261 | ||
c4e7c712 | 3262 | while (t < e) { |
53c1dcc0 | 3263 | const U8 ch = *t++; |
b3ab6785 KW |
3264 | if (NATIVE_IS_INVARIANT(ch)) continue; |
3265 | ||
3266 | t--; /* t already incremented; re-point to first variant */ | |
3267 | two_byte_count = 1; | |
3268 | goto must_be_utf8; | |
c4e7c712 | 3269 | } |
b3ab6785 KW |
3270 | |
3271 | /* utf8 conversion not needed because all are invariants. Mark as | |
3272 | * UTF-8 even if no variant - saves scanning loop */ | |
c4e7c712 | 3273 | SvUTF8_on(sv); |
b3ab6785 KW |
3274 | return SvCUR(sv); |
3275 | ||
3276 | must_be_utf8: | |
3277 | ||
3278 | /* Here, the string should be converted to utf8, either because of an | |
3279 | * input flag (two_byte_count = 0), or because a character that | |
3280 | * requires 2 bytes was found (two_byte_count = 1). t points either to | |
3281 | * the beginning of the string (if we didn't examine anything), or to | |
3282 | * the first variant. In either case, everything from s to t - 1 will | |
3283 | * occupy only 1 byte each on output. | |
3284 | * | |
3285 | * There are two main ways to convert. One is to create a new string | |
3286 | * and go through the input starting from the beginning, appending each | |
3287 | * converted value onto the new string as we go along. It's probably | |
3288 | * best to allocate enough space in the string for the worst possible | |
3289 | * case rather than possibly running out of space and having to | |
3290 | * reallocate and then copy what we've done so far. Since everything | |
3291 | * from s to t - 1 is invariant, the destination can be initialized | |
3292 | * with these using a fast memory copy | |
3293 | * | |
3294 | * The other way is to figure out exactly how big the string should be | |
3295 | * by parsing the entire input. Then you don't have to make it big | |
3296 | * enough to handle the worst possible case, and more importantly, if | |
3297 | * the string you already have is large enough, you don't have to | |
3298 | * allocate a new string, you can copy the last character in the input | |
3299 | * string to the final position(s) that will be occupied by the | |
3300 | * converted string and go backwards, stopping at t, since everything | |
3301 | * before that is invariant. | |
3302 | * | |
3303 | * There are advantages and disadvantages to each method. | |
3304 | * | |
3305 | * In the first method, we can allocate a new string, do the memory | |
3306 | * copy from the s to t - 1, and then proceed through the rest of the | |
3307 | * string byte-by-byte. | |
3308 | * | |
3309 | * In the second method, we proceed through the rest of the input | |
3310 | * string just calculating how big the converted string will be. Then | |
3311 | * there are two cases: | |
3312 | * 1) if the string has enough extra space to handle the converted | |
3313 | * value. We go backwards through the string, converting until we | |
3314 | * get to the position we are at now, and then stop. If this | |
3315 | * position is far enough along in the string, this method is | |
3316 | * faster than the other method. If the memory copy were the same | |
3317 | * speed as the byte-by-byte loop, that position would be about | |
3318 | * half-way, as at the half-way mark, parsing to the end and back | |
3319 | * is one complete string's parse, the same amount as starting | |
3320 | * over and going all the way through. Actually, it would be | |
3321 | * somewhat less than half-way, as it's faster to just count bytes | |
3322 | * than to also copy, and we don't have the overhead of allocating | |
3323 | * a new string, changing the scalar to use it, and freeing the | |
3324 | * existing one. But if the memory copy is fast, the break-even | |
3325 | * point is somewhere after half way. The counting loop could be | |
3326 | * sped up by vectorization, etc, to move the break-even point | |
3327 | * further towards the beginning. | |
3328 | * 2) if the string doesn't have enough space to handle the converted | |
3329 | * value. A new string will have to be allocated, and one might | |
3330 | * as well, given that, start from the beginning doing the first | |
3331 | * method. We've spent extra time parsing the string and in | |
3332 | * exchange all we've gotten is that we know precisely how big to | |
3333 | * make the new one. Perl is more optimized for time than space, | |
3334 | * so this case is a loser. | |
3335 | * So what I've decided to do is not use the 2nd method unless it is | |
3336 | * guaranteed that a new string won't have to be allocated, assuming | |
3337 | * the worst case. I also decided not to put any more conditions on it | |
3338 | * than this, for now. It seems likely that, since the worst case is | |
3339 | * twice as big as the unknown portion of the string (plus 1), we won't | |
3340 | * be guaranteed enough space, causing us to go to the first method, | |
3341 | * unless the string is short, or the first variant character is near | |
3342 | * the end of it. In either of these cases, it seems best to use the | |
3343 | * 2nd method. The only circumstance I can think of where this would | |
3344 | * be really slower is if the string had once had much more data in it | |
3345 | * than it does now, but there is still a substantial amount in it */ | |
3346 | ||
3347 | { | |
3348 | STRLEN invariant_head = t - s; | |
3349 | STRLEN size = invariant_head + (e - t) * 2 + 1 + extra; | |
3350 | if (SvLEN(sv) < size) { | |
3351 | ||
3352 | /* Here, have decided to allocate a new string */ | |
3353 | ||
3354 | U8 *dst; | |
3355 | U8 *d; | |
3356 | ||
3357 | Newx(dst, size, U8); | |
3358 | ||
3359 | /* If no known invariants at the beginning of the input string, | |
3360 | * set so starts from there. Otherwise, can use memory copy to | |
3361 | * get up to where we are now, and then start from here */ | |
3362 | ||
3363 | if (invariant_head <= 0) { | |
3364 | d = dst; | |
3365 | } else { | |
3366 | Copy(s, dst, invariant_head, char); | |
3367 | d = dst + invariant_head; | |
3368 | } | |
3369 | ||
3370 | while (t < e) { | |
3371 | const UV uv = NATIVE8_TO_UNI(*t++); | |
3372 | if (UNI_IS_INVARIANT(uv)) | |
3373 | *d++ = (U8)UNI_TO_NATIVE(uv); | |
3374 | else { | |
3375 | *d++ = (U8)UTF8_EIGHT_BIT_HI(uv); | |
3376 | *d++ = (U8)UTF8_EIGHT_BIT_LO(uv); | |
3377 | } | |
3378 | } | |
3379 | *d = '\0'; | |
3380 | SvPV_free(sv); /* No longer using pre-existing string */ | |
3381 | SvPV_set(sv, (char*)dst); | |
3382 | SvCUR_set(sv, d - dst); | |
3383 | SvLEN_set(sv, size); | |
3384 | } else { | |
3385 | ||
3386 | /* Here, have decided to get the exact size of the string. | |
3387 | * Currently this happens only when we know that there is | |
3388 | * guaranteed enough space to fit the converted string, so | |
3389 | * don't have to worry about growing. If two_byte_count is 0, | |
3390 | * then t points to the first byte of the string which hasn't | |
3391 | * been examined yet. Otherwise two_byte_count is 1, and t | |
3392 | * points to the first byte in the string that will expand to | |
3393 | * two. Depending on this, start examining at t or 1 after t. | |
3394 | * */ | |
3395 | ||
3396 | U8 *d = t + two_byte_count; | |
3397 | ||
3398 | ||
3399 | /* Count up the remaining bytes that expand to two */ | |
3400 | ||
3401 | while (d < e) { | |
3402 | const U8 chr = *d++; | |
3403 | if (! NATIVE_IS_INVARIANT(chr)) two_byte_count++; | |
3404 | } | |
3405 | ||
3406 | /* The string will expand by just the number of bytes that | |
3407 | * occupy two positions. But we are one afterwards because of | |
3408 | * the increment just above. This is the place to put the | |
3409 | * trailing NUL, and to set the length before we decrement */ | |
3410 | ||
3411 | d += two_byte_count; | |
3412 | SvCUR_set(sv, d - s); | |
3413 | *d-- = '\0'; | |
3414 | ||
3415 | ||
3416 | /* Having decremented d, it points to the position to put the | |
3417 | * very last byte of the expanded string. Go backwards through | |
3418 | * the string, copying and expanding as we go, stopping when we | |
3419 | * get to the part that is invariant the rest of the way down */ | |
3420 | ||
3421 | e--; | |
3422 | while (e >= t) { | |
3423 | const U8 ch = NATIVE8_TO_UNI(*e--); | |
3424 | if (UNI_IS_INVARIANT(ch)) { | |
3425 | *d-- = UNI_TO_NATIVE(ch); | |
3426 | } else { | |
3427 | *d-- = (U8)UTF8_EIGHT_BIT_LO(ch); | |
3428 | *d-- = (U8)UTF8_EIGHT_BIT_HI(ch); | |
3429 | } | |
3430 | } | |
3431 | } | |
3432 | } | |
560a288e | 3433 | } |
b3ab6785 KW |
3434 | |
3435 | /* Mark as UTF-8 even if no variant - saves scanning loop */ | |
3436 | SvUTF8_on(sv); | |
4411f3b6 | 3437 | return SvCUR(sv); |
560a288e GS |
3438 | } |
3439 | ||
c461cf8f JH |
3440 | /* |
3441 | =for apidoc sv_utf8_downgrade | |
3442 | ||
78ea37eb | 3443 | Attempts to convert the PV of an SV from characters to bytes. |
2bbc8d55 SP |
3444 | If the PV contains a character that cannot fit |
3445 | in a byte, this conversion will fail; | |
78ea37eb | 3446 | in this case, either returns false or, if C<fail_ok> is not |
c461cf8f JH |
3447 | true, croaks. |
3448 | ||
13a6c0e0 JH |
3449 | This is not as a general purpose Unicode to byte encoding interface: |
3450 | use the Encode extension for that. | |
3451 | ||
c461cf8f JH |
3452 | =cut |
3453 | */ | |
3454 | ||
560a288e | 3455 | bool |
7bc54cea | 3456 | Perl_sv_utf8_downgrade(pTHX_ register SV *const sv, const bool fail_ok) |
560a288e | 3457 | { |
97aff369 | 3458 | dVAR; |
7918f24d NC |
3459 | |
3460 | PERL_ARGS_ASSERT_SV_UTF8_DOWNGRADE; | |
3461 | ||
78ea37eb | 3462 | if (SvPOKp(sv) && SvUTF8(sv)) { |
fa301091 | 3463 | if (SvCUR(sv)) { |
03cfe0ae | 3464 | U8 *s; |
652088fc | 3465 | STRLEN len; |
fa301091 | 3466 | |
765f542d NC |
3467 | if (SvIsCOW(sv)) { |
3468 | sv_force_normal_flags(sv, 0); | |
3469 | } | |
03cfe0ae NIS |
3470 | s = (U8 *) SvPV(sv, len); |
3471 | if (!utf8_to_bytes(s, &len)) { | |
fa301091 JH |
3472 | if (fail_ok) |
3473 | return FALSE; | |
3474 | else { | |
3475 | if (PL_op) | |
3476 | Perl_croak(aTHX_ "Wide character in %s", | |
53e06cf0 | 3477 | OP_DESC(PL_op)); |
fa301091 JH |
3478 | else |
3479 | Perl_croak(aTHX_ "Wide character"); | |
3480 | } | |
4b3603a4 | 3481 | } |
b162af07 | 3482 | SvCUR_set(sv, len); |
67e989fb | 3483 | } |
560a288e | 3484 | } |
ffebcc3e | 3485 | SvUTF8_off(sv); |
560a288e GS |
3486 | return TRUE; |
3487 | } | |
3488 | ||
c461cf8f JH |
3489 | /* |
3490 | =for apidoc sv_utf8_encode | |
3491 | ||
78ea37eb TS |
3492 | Converts the PV of an SV to UTF-8, but then turns the C<SvUTF8> |
3493 | flag off so that it looks like octets again. | |
c461cf8f JH |
3494 | |
3495 | =cut | |
3496 | */ | |
3497 | ||
560a288e | 3498 | void |
7bc54cea | 3499 | Perl_sv_utf8_encode(pTHX_ register SV *const sv) |
560a288e | 3500 | { |
7918f24d NC |
3501 | PERL_ARGS_ASSERT_SV_UTF8_ENCODE; |
3502 | ||
4c94c214 NC |
3503 | if (SvIsCOW(sv)) { |
3504 | sv_force_normal_flags(sv, 0); | |
3505 | } | |
3506 | if (SvREADONLY(sv)) { | |
6ad8f254 | 3507 | Perl_croak_no_modify(aTHX); |
4c94c214 | 3508 | } |
a5f5288a | 3509 | (void) sv_utf8_upgrade(sv); |
560a288e GS |
3510 | SvUTF8_off(sv); |
3511 | } | |
3512 | ||
4411f3b6 NIS |
3513 | /* |
3514 | =for apidoc sv_utf8_decode | |
3515 | ||
78ea37eb TS |
3516 | If the PV of the SV is an octet sequence in UTF-8 |
3517 | and contains a multiple-byte character, the C<SvUTF8> flag is turned on | |
3518 | so that it looks like a character. If the PV contains only single-byte | |
3519 | characters, the C<SvUTF8> flag stays being off. | |
3520 | Scans PV for validity and returns false if the PV is invalid UTF-8. | |
4411f3b6 NIS |
3521 | |
3522 | =cut | |
3523 | */ | |
3524 | ||
560a288e | 3525 | bool |
7bc54cea | 3526 | Perl_sv_utf8_decode(pTHX_ register SV *const sv) |
560a288e | 3527 | { |
7918f24d NC |
3528 | PERL_ARGS_ASSERT_SV_UTF8_DECODE; |
3529 | ||
78ea37eb | 3530 | if (SvPOKp(sv)) { |
93524f2b NC |
3531 | const U8 *c; |
3532 | const U8 *e; | |
9cbac4c7 | 3533 | |
645c22ef DM |
3534 | /* The octets may have got themselves encoded - get them back as |
3535 | * bytes | |
3536 | */ | |
3537 | if (!sv_utf8_downgrade(sv, TRUE)) | |
560a288e GS |
3538 | return FALSE; |
3539 | ||
3540 | /* it is actually just a matter of turning the utf8 flag on, but | |
3541 | * we want to make sure everything inside is valid utf8 first. | |
3542 | */ | |
93524f2b | 3543 | c = (const U8 *) SvPVX_const(sv); |
63cd0674 | 3544 | if (!is_utf8_string(c, SvCUR(sv)+1)) |
67e989fb | 3545 | return FALSE; |
93524f2b | 3546 | e = (const U8 *) SvEND(sv); |
511c2ff0 | 3547 | while (c < e) { |
b64e5050 | 3548 | const U8 ch = *c++; |
c4d5f83a | 3549 | if (!UTF8_IS_INVARIANT(ch)) { |
67e989fb JH |
3550 | SvUTF8_on(sv); |
3551 | break; | |
3552 | } | |
560a288e | 3553 | } |
560a288e GS |
3554 | } |
3555 | return TRUE; | |
3556 | } | |
3557 | ||
954c1994 GS |
3558 | /* |
3559 | =for apidoc sv_setsv | |
3560 | ||
645c22ef DM |
3561 | Copies the contents of the source SV C<ssv> into the destination SV |
3562 | C<dsv>. The source SV may be destroyed if it is mortal, so don't use this | |
3563 | function if the source SV needs to be reused. Does not handle 'set' magic. | |
3564 | Loosely speaking, it performs a copy-by-value, obliterating any previous | |
3565 | content of the destination. | |
3566 | ||
3567 | You probably want to use one of the assortment of wrappers, such as | |
3568 | C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and | |
3569 | C<SvSetMagicSV_nosteal>. | |
3570 | ||
8d6d96c1 HS |
3571 | =for apidoc sv_setsv_flags |
3572 | ||
645c22ef DM |
3573 | Copies the contents of the source SV C<ssv> into the destination SV |
3574 | C<dsv>. The source SV may be destroyed if it is mortal, so don't use this | |
3575 | function if the source SV needs to be reused. Does not handle 'set' magic. | |
3576 | Loosely speaking, it performs a copy-by-value, obliterating any previous | |
3577 | content of the destination. | |
3578 | If the C<flags> parameter has the C<SV_GMAGIC> bit set, will C<mg_get> on | |
5fcdf167 NC |
3579 | C<ssv> if appropriate, else not. If the C<flags> parameter has the |
3580 | C<NOSTEAL> bit set then the buffers of temps will not be stolen. <sv_setsv> | |
3581 | and C<sv_setsv_nomg> are implemented in terms of this function. | |
645c22ef DM |
3582 | |
3583 | You probably want to use one of the assortment of wrappers, such as | |
3584 | C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and | |
3585 | C<SvSetMagicSV_nosteal>. | |
3586 | ||
3587 | This is the primary function for copying scalars, and most other | |
3588 | copy-ish functions and macros use this underneath. | |
8d6d96c1 HS |
3589 | |
3590 | =cut | |
3591 | */ | |
3592 | ||
5d0301b7 | 3593 | static void |
7bc54cea | 3594 | S_glob_assign_glob(pTHX_ SV *const dstr, SV *const sstr, const int dtype) |
5d0301b7 | 3595 | { |
c8bbf675 | 3596 | I32 mro_changes = 0; /* 1 = method, 2 = isa, 3 = recursive isa */ |
3e6edce2 | 3597 | HV *old_stash = NULL; |
dd69841b | 3598 | |
7918f24d NC |
3599 | PERL_ARGS_ASSERT_GLOB_ASSIGN_GLOB; |
3600 | ||
bec4f4b4 | 3601 | if (dtype != SVt_PVGV && !isGV_with_GP(dstr)) { |
5d0301b7 NC |
3602 | const char * const name = GvNAME(sstr); |
3603 | const STRLEN len = GvNAMELEN(sstr); | |
0d092c36 | 3604 | { |
f7877b28 NC |
3605 | if (dtype >= SVt_PV) { |
3606 | SvPV_free(dstr); | |
3607 | SvPV_set(dstr, 0); | |
3608 | SvLEN_set(dstr, 0); | |
3609 | SvCUR_set(dstr, 0); | |
3610 | } | |
0d092c36 | 3611 | SvUPGRADE(dstr, SVt_PVGV); |
dedf8e73 | 3612 | (void)SvOK_off(dstr); |
2e5b91de NC |
3613 | /* FIXME - why are we doing this, then turning it off and on again |
3614 | below? */ | |
3615 | isGV_with_GP_on(dstr); | |
f7877b28 | 3616 | } |
5d0301b7 NC |
3617 | GvSTASH(dstr) = GvSTASH(sstr); |
3618 | if (GvSTASH(dstr)) | |
daba3364 | 3619 | Perl_sv_add_backref(aTHX_ MUTABLE_SV(GvSTASH(dstr)), dstr); |
159b6efe | 3620 | gv_name_set(MUTABLE_GV(dstr), name, len, GV_ADD); |
5d0301b7 NC |
3621 | SvFAKE_on(dstr); /* can coerce to non-glob */ |
3622 | } | |
3623 | ||
159b6efe | 3624 | if(GvGP(MUTABLE_GV(sstr))) { |
dd69841b BB |
3625 | /* If source has method cache entry, clear it */ |
3626 | if(GvCVGEN(sstr)) { | |
3627 | SvREFCNT_dec(GvCV(sstr)); | |
c43ae56f | 3628 | GvCV_set(sstr, NULL); |
dd69841b BB |
3629 | GvCVGEN(sstr) = 0; |
3630 | } | |
3631 | /* If source has a real method, then a method is | |
3632 | going to change */ | |
00169e2c FC |
3633 | else if( |
3634 | GvCV((const GV *)sstr) && GvSTASH(dstr) && HvENAME(GvSTASH(dstr)) | |
3635 | ) { | |
70cd14a1 | 3636 | mro_changes = 1; |
dd69841b BB |
3637 | } |
3638 | } | |
3639 | ||
3640 | /* If dest already had a real method, that's a change as well */ | |
00169e2c FC |
3641 | if( |
3642 | !mro_changes && GvGP(MUTABLE_GV(dstr)) && GvCVu((const GV *)dstr) | |
3643 | && GvSTASH(dstr) && HvENAME(GvSTASH(dstr)) | |
3644 | ) { | |
70cd14a1 | 3645 | mro_changes = 1; |
dd69841b BB |
3646 | } |
3647 | ||
c8bbf675 FC |
3648 | /* We don’t need to check the name of the destination if it was not a |
3649 | glob to begin with. */ | |
3650 | if(dtype == SVt_PVGV) { | |
3651 | const char * const name = GvNAME((const GV *)dstr); | |
00169e2c FC |
3652 | if( |
3653 | strEQ(name,"ISA") | |
3654 | /* The stash may have been detached from the symbol table, so | |
3655 | check its name. */ | |
3656 | && GvSTASH(dstr) && HvENAME(GvSTASH(dstr)) | |
6624142a | 3657 | && GvAV((const GV *)sstr) |
00169e2c | 3658 | ) |
c8bbf675 FC |
3659 | mro_changes = 2; |
3660 | else { | |
3661 | const STRLEN len = GvNAMELEN(dstr); | |
3662 | if (len > 1 && name[len-2] == ':' && name[len-1] == ':') { | |
3663 | mro_changes = 3; | |
3664 | ||
3665 | /* Set aside the old stash, so we can reset isa caches on | |
3666 | its subclasses. */ | |
bf01568a FC |
3667 | if((old_stash = GvHV(dstr))) |
3668 | /* Make sure we do not lose it early. */ | |
3669 | SvREFCNT_inc_simple_void_NN( | |
3670 | sv_2mortal((SV *)old_stash) | |
3671 | ); | |
c8bbf675 FC |
3672 | } |
3673 | } | |
3674 | } | |
70cd14a1 | 3675 | |
159b6efe | 3676 | gp_free(MUTABLE_GV(dstr)); |
2e5b91de | 3677 | isGV_with_GP_off(dstr); |
5d0301b7 | 3678 | (void)SvOK_off(dstr); |
2e5b91de | 3679 | isGV_with_GP_on(dstr); |
dedf8e73 | 3680 | GvINTRO_off(dstr); /* one-shot flag */ |
c43ae56f | 3681 | GvGP_set(dstr, gp_ref(GvGP(sstr))); |
5d0301b7 NC |
3682 | if (SvTAINTED(sstr)) |
3683 | SvTAINT(dstr); | |
3684 | if (GvIMPORTED(dstr) != GVf_IMPORTED | |
3685 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) | |
3686 | { | |
3687 | GvIMPORTED_on(dstr); | |
3688 | } | |
3689 | GvMULTI_on(dstr); | |
6624142a FC |
3690 | if(mro_changes == 2) { |
3691 | MAGIC *mg; | |
3692 | SV * const sref = (SV *)GvAV((const GV *)dstr); | |
3693 | if (SvSMAGICAL(sref) && (mg = mg_find(sref, PERL_MAGIC_isa))) { | |
3694 | if (SvTYPE(mg->mg_obj) != SVt_PVAV) { | |
3695 | AV * const ary = newAV(); | |
3696 | av_push(ary, mg->mg_obj); /* takes the refcount */ | |
3697 | mg->mg_obj = (SV *)ary; | |
3698 | } | |
3699 | av_push((AV *)mg->mg_obj, SvREFCNT_inc_simple_NN(dstr)); | |
3700 | } | |
3701 | else sv_magic(sref, dstr, PERL_MAGIC_isa, NULL, 0); | |
3702 | mro_isa_changed_in(GvSTASH(dstr)); | |
3703 | } | |
c8bbf675 | 3704 | else if(mro_changes == 3) { |
d056e33c | 3705 | HV * const stash = GvHV(dstr); |
78b79c77 | 3706 | if(old_stash ? (HV *)HvENAME_get(old_stash) : stash) |
d056e33c | 3707 | mro_package_moved( |
35759254 | 3708 | stash, old_stash, |
afdbe55d | 3709 | (GV *)dstr, 0 |
d056e33c | 3710 | ); |
c8bbf675 | 3711 | } |
70cd14a1 | 3712 | else if(mro_changes) mro_method_changed_in(GvSTASH(dstr)); |
5d0301b7 NC |
3713 | return; |
3714 | } | |
3715 | ||
b8473700 | 3716 | static void |
7bc54cea | 3717 | S_glob_assign_ref(pTHX_ SV *const dstr, SV *const sstr) |
7918f24d | 3718 | { |
b8473700 NC |
3719 | SV * const sref = SvREFCNT_inc(SvRV(sstr)); |
3720 | SV *dref = NULL; | |
3721 | const int intro = GvINTRO(dstr); | |
2440974c | 3722 | SV **location; |
3386d083 | 3723 | U8 import_flag = 0; |
27242d61 NC |
3724 | const U32 stype = SvTYPE(sref); |
3725 | ||
7918f24d | 3726 | PERL_ARGS_ASSERT_GLOB_ASSIGN_REF; |
b8473700 | 3727 | |
b8473700 NC |
3728 | if (intro) { |
3729 | GvINTRO_off(dstr); /* one-shot flag */ | |
3730 | GvLINE(dstr) = CopLINE(PL_curcop); | |
159b6efe | 3731 | GvEGV(dstr) = MUTABLE_GV(dstr); |
b8473700 NC |
3732 | } |
3733 | GvMULTI_on(dstr); | |
27242d61 | 3734 | switch (stype) { |
b8473700 | 3735 | case SVt_PVCV: |
c43ae56f | 3736 | location = (SV **) &(GvGP(dstr)->gp_cv); /* XXX bypassing GvCV_set */ |
27242d61 NC |
3737 | import_flag = GVf_IMPORTED_CV; |
3738 | goto common; | |
3739 | case SVt_PVHV: | |
3740 | location = (SV **) &GvHV(dstr); | |
3741 | import_flag = GVf_IMPORTED_HV; | |
3742 | goto common; | |
3743 | case SVt_PVAV: | |
3744 | location = (SV **) &GvAV(dstr); | |
3745 | import_flag = GVf_IMPORTED_AV; | |
3746 | goto common; | |
3747 | case SVt_PVIO: | |
3748 | location = (SV **) &GvIOp(dstr); | |
3749 | goto common; | |
3750 | case SVt_PVFM: | |
3751 | location = (SV **) &GvFORM(dstr); | |
ef595a33 | 3752 | goto common; |
27242d61 NC |
3753 | default: |
3754 | location = &GvSV(dstr); | |
3755 | import_flag = GVf_IMPORTED_SV; | |
3756 | common: | |
b8473700 | 3757 | if (intro) { |
27242d61 | 3758 | if (stype == SVt_PVCV) { |
ea726b52 | 3759 | /*if (GvCVGEN(dstr) && (GvCV(dstr) != (const CV *)sref || GvCVGEN(dstr))) {*/ |
5f2fca8a | 3760 | if (GvCVGEN(dstr)) { |
27242d61 | 3761 | SvREFCNT_dec(GvCV(dstr)); |
c43ae56f | 3762 | GvCV_set(dstr, NULL); |
27242d61 | 3763 | GvCVGEN(dstr) = 0; /* Switch off cacheness. */ |
27242d61 | 3764 | } |
b8473700 | 3765 | } |
27242d61 | 3766 | SAVEGENERICSV(*location); |
b8473700 NC |
3767 | } |
3768 | else | |
27242d61 | 3769 | dref = *location; |
5f2fca8a | 3770 | if (stype == SVt_PVCV && (*location != sref || GvCVGEN(dstr))) { |
ea726b52 | 3771 | CV* const cv = MUTABLE_CV(*location); |
b8473700 | 3772 | if (cv) { |
159b6efe | 3773 | if (!GvCVGEN((const GV *)dstr) && |
b8473700 NC |
3774 | (CvROOT(cv) || CvXSUB(cv))) |
3775 | { | |
3776 | /* Redefining a sub - warning is mandatory if | |
3777 | it was a const and its value changed. */ | |
ea726b52 | 3778 | if (CvCONST(cv) && CvCONST((const CV *)sref) |
126f53f3 NC |
3779 | && cv_const_sv(cv) |
3780 | == cv_const_sv((const CV *)sref)) { | |
6f207bd3 | 3781 | NOOP; |
b8473700 NC |
3782 | /* They are 2 constant subroutines generated from |
3783 | the same constant. This probably means that | |
3784 | they are really the "same" proxy subroutine | |
3785 | instantiated in 2 places. Most likely this is | |
3786 | when a constant is exported twice. Don't warn. | |
3787 | */ | |
3788 | } | |
3789 | else if (ckWARN(WARN_REDEFINE) | |
3790 | || (CvCONST(cv) | |
ea726b52 | 3791 | && (!CvCONST((const CV *)sref) |
b8473700 | 3792 | || sv_cmp(cv_const_sv(cv), |
126f53f3 NC |
3793 | cv_const_sv((const CV *) |
3794 | sref))))) { | |
b8473700 | 3795 | Perl_warner(aTHX_ packWARN(WARN_REDEFINE), |
10edeb5d JH |
3796 | (const char *) |
3797 | (CvCONST(cv) | |
3798 | ? "Constant subroutine %s::%s redefined" | |
3799 | : "Subroutine %s::%s redefined"), | |
159b6efe NC |
3800 | HvNAME_get(GvSTASH((const GV *)dstr)), |
3801 | GvENAME(MUTABLE_GV(dstr))); | |
b8473700 NC |
3802 | } |
3803 | } | |
3804 | if (!intro) | |
159b6efe | 3805 | cv_ckproto_len(cv, (const GV *)dstr, |
cbf82dd0 NC |
3806 | SvPOK(sref) ? SvPVX_const(sref) : NULL, |
3807 | SvPOK(sref) ? SvCUR(sref) : 0); | |
b8473700 | 3808 | } |
b8473700 NC |
3809 | GvCVGEN(dstr) = 0; /* Switch off cacheness. */ |
3810 | GvASSUMECV_on(dstr); | |
dd69841b | 3811 | if(GvSTASH(dstr)) mro_method_changed_in(GvSTASH(dstr)); /* sub foo { 1 } sub bar { 2 } *bar = \&foo */ |
b8473700 | 3812 | } |
2440974c | 3813 | *location = sref; |
3386d083 NC |
3814 | if (import_flag && !(GvFLAGS(dstr) & import_flag) |
3815 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) { | |
3816 | GvFLAGS(dstr) |= import_flag; | |
b8473700 | 3817 | } |
3e79609f FC |
3818 | if (stype == SVt_PVHV) { |
3819 | const char * const name = GvNAME((GV*)dstr); | |
3820 | const STRLEN len = GvNAMELEN(dstr); | |
d056e33c FC |
3821 | if ( |
3822 | len > 1 && name[len-2] == ':' && name[len-1] == ':' | |
78b79c77 | 3823 | && (!dref || HvENAME_get(dref)) |
d056e33c FC |
3824 | ) { |
3825 | mro_package_moved( | |
35759254 | 3826 | (HV *)sref, (HV *)dref, |
afdbe55d | 3827 | (GV *)dstr, 0 |
d056e33c | 3828 | ); |
3e79609f FC |
3829 | } |
3830 | } | |
00169e2c | 3831 | else if ( |
a00c27eb FC |
3832 | stype == SVt_PVAV && sref != dref |
3833 | && strEQ(GvNAME((GV*)dstr), "ISA") | |
00169e2c FC |
3834 | /* The stash may have been detached from the symbol table, so |
3835 | check its name before doing anything. */ | |
3836 | && GvSTASH(dstr) && HvENAME(GvSTASH(dstr)) | |
3837 | ) { | |
6624142a | 3838 | MAGIC *mg; |
a5dba54a FC |
3839 | MAGIC * const omg = dref && SvSMAGICAL(dref) |
3840 | ? mg_find(dref, PERL_MAGIC_isa) | |
3841 | : NULL; | |
6624142a FC |
3842 | if (SvSMAGICAL(sref) && (mg = mg_find(sref, PERL_MAGIC_isa))) { |
3843 | if (SvTYPE(mg->mg_obj) != SVt_PVAV) { | |
3844 | AV * const ary = newAV(); | |
3845 | av_push(ary, mg->mg_obj); /* takes the refcount */ | |
3846 | mg->mg_obj = (SV *)ary; | |
3847 | } | |
a5dba54a FC |
3848 | if (omg) { |
3849 | if (SvTYPE(omg->mg_obj) == SVt_PVAV) { | |
3850 | SV **svp = AvARRAY((AV *)omg->mg_obj); | |
3851 | I32 items = AvFILLp((AV *)omg->mg_obj) + 1; | |
3852 | while (items--) | |
3853 | av_push( | |
3854 | (AV *)mg->mg_obj, | |
3855 | SvREFCNT_inc_simple_NN(*svp++) | |
3856 | ); | |
3857 | } | |
3858 | else | |
3859 | av_push( | |
3860 | (AV *)mg->mg_obj, | |
3861 | SvREFCNT_inc_simple_NN(omg->mg_obj) | |
3862 | ); | |
3863 | } | |
3864 | else | |
3865 | av_push((AV *)mg->mg_obj,SvREFCNT_inc_simple_NN(dstr)); | |
6624142a | 3866 | } |
a5dba54a | 3867 | else |
3e1892cc | 3868 | { |
a5dba54a FC |
3869 | sv_magic( |
3870 | sref, omg ? omg->mg_obj : dstr, PERL_MAGIC_isa, NULL, 0 | |
3871 | ); | |
3e1892cc FC |
3872 | mg = mg_find(sref, PERL_MAGIC_isa); |
3873 | } | |
a5dba54a FC |
3874 | /* Since the *ISA assignment could have affected more than |
3875 | one stash, don’t call mro_isa_changed_in directly, but let | |
3e1892cc | 3876 | magic_clearisa do it for us, as it already has the logic for |
a5dba54a | 3877 | dealing with globs vs arrays of globs. */ |
3e1892cc FC |
3878 | assert(mg); |
3879 | Perl_magic_clearisa(aTHX_ NULL, mg); | |
d851b122 | 3880 | } |
b8473700 NC |
3881 | break; |
3882 | } | |
b37c2d43 | 3883 | SvREFCNT_dec(dref); |
b8473700 NC |
3884 | if (SvTAINTED(sstr)) |
3885 | SvTAINT(dstr); | |
3886 | return; | |
3887 | } | |
3888 | ||
8d6d96c1 | 3889 | void |
7bc54cea | 3890 | Perl_sv_setsv_flags(pTHX_ SV *dstr, register SV* sstr, const I32 flags) |
8d6d96c1 | 3891 | { |
97aff369 | 3892 | dVAR; |
8990e307 LW |
3893 | register U32 sflags; |
3894 | register int dtype; | |
42d0e0b7 | 3895 | register svtype stype; |
463ee0b2 | 3896 | |
7918f24d NC |
3897 | PERL_ARGS_ASSERT_SV_SETSV_FLAGS; |
3898 | ||
79072805 LW |
3899 | if (sstr == dstr) |
3900 | return; | |
29f4f0ab NC |
3901 | |
3902 | if (SvIS_FREED(dstr)) { | |
3903 | Perl_croak(aTHX_ "panic: attempt to copy value %" SVf | |
be2597df | 3904 | " to a freed scalar %p", SVfARG(sstr), (void *)dstr); |
29f4f0ab | 3905 | } |
765f542d | 3906 | SV_CHECK_THINKFIRST_COW_DROP(dstr); |
79072805 | 3907 | if (!sstr) |
3280af22 | 3908 | sstr = &PL_sv_undef; |
29f4f0ab | 3909 | if (SvIS_FREED(sstr)) { |
6c9570dc MHM |
3910 | Perl_croak(aTHX_ "panic: attempt to copy freed scalar %p to %p", |
3911 | (void*)sstr, (void*)dstr); | |
29f4f0ab | 3912 | } |
8990e307 LW |
3913 | stype = SvTYPE(sstr); |
3914 | dtype = SvTYPE(dstr); | |
79072805 | 3915 | |
52944de8 | 3916 | (void)SvAMAGIC_off(dstr); |
7a5fa8a2 | 3917 | if ( SvVOK(dstr) ) |
ece467f9 JP |
3918 | { |
3919 | /* need to nuke the magic */ | |
3920 | mg_free(dstr); | |
ece467f9 | 3921 | } |
9e7bc3e8 | 3922 | |
463ee0b2 | 3923 | /* There's a lot of redundancy below but we're going for speed here */ |
79072805 | 3924 | |
8990e307 | 3925 | switch (stype) { |
79072805 | 3926 | case SVt_NULL: |
aece5585 | 3927 | undef_sstr: |
13be902c | 3928 | if (dtype != SVt_PVGV && dtype != SVt_PVLV) { |
20408e3c GS |
3929 | (void)SvOK_off(dstr); |
3930 | return; | |
3931 | } | |
3932 | break; | |
463ee0b2 | 3933 | case SVt_IV: |
aece5585 GA |
3934 | if (SvIOK(sstr)) { |
3935 | switch (dtype) { | |
3936 | case SVt_NULL: | |
8990e307 | 3937 | sv_upgrade(dstr, SVt_IV); |
aece5585 GA |
3938 | break; |
3939 | case SVt_NV: | |
aece5585 | 3940 | case SVt_PV: |
a0d0e21e | 3941 | sv_upgrade(dstr, SVt_PVIV); |
aece5585 | 3942 | break; |
010be86b | 3943 | case SVt_PVGV: |
13be902c | 3944 | case SVt_PVLV: |
010be86b | 3945 | goto end_of_first_switch; |
aece5585 GA |
3946 | } |
3947 | (void)SvIOK_only(dstr); | |
45977657 | 3948 | SvIV_set(dstr, SvIVX(sstr)); |
25da4f38 IZ |
3949 | if (SvIsUV(sstr)) |
3950 | SvIsUV_on(dstr); | |
37c25af0 NC |
3951 | /* SvTAINTED can only be true if the SV has taint magic, which in |
3952 | turn means that the SV type is PVMG (or greater). This is the | |
3953 | case statement for SVt_IV, so this cannot be true (whatever gcov | |
3954 | may say). */ | |
3955 | assert(!SvTAINTED(sstr)); | |
aece5585 | 3956 | return; |
8990e307 | 3957 | } |
4df7f6af NC |
3958 | if (!SvROK(sstr)) |
3959 | goto undef_sstr; | |
3960 | if (dtype < SVt_PV && dtype != SVt_IV) | |
3961 | sv_upgrade(dstr, SVt_IV); | |
3962 | break; | |
aece5585 | 3963 | |
463ee0b2 | 3964 | case SVt_NV: |
aece5585 GA |
3965 | if (SvNOK(sstr)) { |
3966 | switch (dtype) { | |
3967 | case SVt_NULL: | |
3968 | case SVt_IV: | |
8990e307 | 3969 | sv_upgrade(dstr, SVt_NV); |
aece5585 | 3970 | break; |
aece5585 GA |
3971 | case SVt_PV: |
3972 | case SVt_PVIV: | |
a0d0e21e | 3973 | sv_upgrade(dstr, SVt_PVNV); |
aece5585 | 3974 | break; |
010be86b | 3975 | case SVt_PVGV: |
13be902c | 3976 | case SVt_PVLV: |
010be86b | 3977 | goto end_of_first_switch; |
aece5585 | 3978 | } |
9d6ce603 | 3979 | SvNV_set(dstr, SvNVX(sstr)); |
aece5585 | 3980 | (void)SvNOK_only(dstr); |
37c25af0 NC |
3981 | /* SvTAINTED can only be true if the SV has taint magic, which in |
3982 | turn means that the SV type is PVMG (or greater). This is the | |
3983 | case statement for SVt_NV, so this cannot be true (whatever gcov | |
3984 | may say). */ | |
3985 | assert(!SvTAINTED(sstr)); | |
aece5585 | 3986 | return; |
8990e307 | 3987 | } |
aece5585 GA |
3988 | goto undef_sstr; |
3989 | ||
fc36a67e | 3990 | case SVt_PVFM: |
f8c7b90f | 3991 | #ifdef PERL_OLD_COPY_ON_WRITE |
d89fc664 NC |
3992 | if ((SvFLAGS(sstr) & CAN_COW_MASK) == CAN_COW_FLAGS) { |
3993 | if (dtype < SVt_PVIV) | |
3994 | sv_upgrade(dstr, SVt_PVIV); | |
3995 | break; | |
3996 | } | |
3997 | /* Fall through */ | |
3998 | #endif | |
3999 | case SVt_PV: | |
8990e307 | 4000 | if (dtype < SVt_PV) |
463ee0b2 | 4001 | sv_upgrade(dstr, SVt_PV); |
463ee0b2 LW |
4002 | break; |
4003 | case SVt_PVIV: | |
8990e307 | 4004 | if (dtype < SVt_PVIV) |
463ee0b2 | 4005 | sv_upgrade(dstr, SVt_PVIV); |
463ee0b2 LW |
4006 | break; |
4007 | case SVt_PVNV: | |
8990e307 | 4008 | if (dtype < SVt_PVNV) |
463ee0b2 | 4009 | sv_upgrade(dstr, SVt_PVNV); |
463ee0b2 | 4010 | break; |
489f7bfe | 4011 | default: |
a3b680e6 AL |
4012 | { |
4013 | const char * const type = sv_reftype(sstr,0); | |
533c011a | 4014 | if (PL_op) |
94bbb3f4 | 4015 | Perl_croak(aTHX_ "Bizarre copy of %s in %s", type, OP_DESC(PL_op)); |
4633a7c4 | 4016 | else |
a3b680e6 AL |
4017 | Perl_croak(aTHX_ "Bizarre copy of %s", type); |
4018 | } | |
4633a7c4 LW |
4019 | break; |
4020 | ||
f0826785 BM |
4021 | case SVt_REGEXP: |
4022 | if (dtype < SVt_REGEXP) | |
4023 | sv_upgrade(dstr, SVt_REGEXP); | |
4024 | break; | |
4025 | ||
cecf5685 | 4026 | /* case SVt_BIND: */ |
39cb70dc | 4027 | case SVt_PVLV: |
79072805 | 4028 | case SVt_PVGV: |
cecf5685 | 4029 | /* SvVALID means that this PVGV is playing at being an FBM. */ |
79072805 | 4030 | |
489f7bfe | 4031 | case SVt_PVMG: |
8d6d96c1 | 4032 | if (SvGMAGICAL(sstr) && (flags & SV_GMAGIC)) { |
973f89ab | 4033 | mg_get(sstr); |
13be902c | 4034 | if (SvTYPE(sstr) != stype) |
973f89ab | 4035 | stype = SvTYPE(sstr); |
5cf4b255 FC |
4036 | } |
4037 | if (isGV_with_GP(sstr) && dtype <= SVt_PVLV) { | |
d4c19fe8 | 4038 | glob_assign_glob(dstr, sstr, dtype); |
b8c701c1 | 4039 | return; |
973f89ab | 4040 | } |
ded42b9f | 4041 | if (stype == SVt_PVLV) |
862a34c6 | 4042 | SvUPGRADE(dstr, SVt_PVNV); |
ded42b9f | 4043 | else |
42d0e0b7 | 4044 | SvUPGRADE(dstr, (svtype)stype); |
79072805 | 4045 | } |
010be86b | 4046 | end_of_first_switch: |
79072805 | 4047 | |
ff920335 NC |
4048 | /* dstr may have been upgraded. */ |
4049 | dtype = SvTYPE(dstr); | |
8990e307 LW |
4050 | sflags = SvFLAGS(sstr); |
4051 | ||
ba2fdce6 | 4052 | if (dtype == SVt_PVCV || dtype == SVt_PVFM) { |
85324b4d NC |
4053 | /* Assigning to a subroutine sets the prototype. */ |
4054 | if (SvOK(sstr)) { | |
4055 | STRLEN len; | |
4056 | const char *const ptr = SvPV_const(sstr, len); | |
4057 | ||
4058 | SvGROW(dstr, len + 1); | |
4059 | Copy(ptr, SvPVX(dstr), len + 1, char); | |
4060 | SvCUR_set(dstr, len); | |
fcddd32e | 4061 | SvPOK_only(dstr); |
ba2fdce6 | 4062 | SvFLAGS(dstr) |= sflags & SVf_UTF8; |
85324b4d NC |
4063 | } else { |
4064 | SvOK_off(dstr); | |
4065 | } | |
ba2fdce6 NC |
4066 | } else if (dtype == SVt_PVAV || dtype == SVt_PVHV) { |
4067 | const char * const type = sv_reftype(dstr,0); | |
4068 | if (PL_op) | |
94bbb3f4 | 4069 | Perl_croak(aTHX_ "Cannot copy to %s in %s", type, OP_DESC(PL_op)); |
ba2fdce6 NC |
4070 | else |
4071 | Perl_croak(aTHX_ "Cannot copy to %s", type); | |
85324b4d | 4072 | } else if (sflags & SVf_ROK) { |
13be902c | 4073 | if (isGV_with_GP(dstr) |
785bee4f | 4074 | && SvTYPE(SvRV(sstr)) == SVt_PVGV && isGV_with_GP(SvRV(sstr))) { |
acaa9288 NC |
4075 | sstr = SvRV(sstr); |
4076 | if (sstr == dstr) { | |
4077 | if (GvIMPORTED(dstr) != GVf_IMPORTED | |
4078 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) | |
4079 | { | |
4080 | GvIMPORTED_on(dstr); | |
4081 | } | |
4082 | GvMULTI_on(dstr); | |
4083 | return; | |
4084 | } | |
785bee4f NC |
4085 | glob_assign_glob(dstr, sstr, dtype); |
4086 | return; | |
acaa9288 NC |
4087 | } |
4088 | ||
8990e307 | 4089 | if (dtype >= SVt_PV) { |
13be902c | 4090 | if (isGV_with_GP(dstr)) { |
d4c19fe8 | 4091 | glob_assign_ref(dstr, sstr); |
b8c701c1 NC |
4092 | return; |
4093 | } | |
3f7c398e | 4094 | if (SvPVX_const(dstr)) { |
8bd4d4c5 | 4095 | SvPV_free(dstr); |
b162af07 SP |
4096 | SvLEN_set(dstr, 0); |
4097 | SvCUR_set(dstr, 0); | |
a0d0e21e | 4098 | } |
8990e307 | 4099 | } |
a0d0e21e | 4100 | (void)SvOK_off(dstr); |
b162af07 | 4101 | SvRV_set(dstr, SvREFCNT_inc(SvRV(sstr))); |
96d4b0ee | 4102 | SvFLAGS(dstr) |= sflags & SVf_ROK; |
dfd48732 NC |
4103 | assert(!(sflags & SVp_NOK)); |
4104 | assert(!(sflags & SVp_IOK)); | |
4105 | assert(!(sflags & SVf_NOK)); | |
4106 | assert(!(sflags & SVf_IOK)); | |
ed6116ce | 4107 | } |
13be902c | 4108 | else if (isGV_with_GP(dstr)) { |
c0c44674 | 4109 | if (!(sflags & SVf_OK)) { |
a2a5de95 NC |
4110 | Perl_ck_warner(aTHX_ packWARN(WARN_MISC), |
4111 | "Undefined value assigned to typeglob"); | |
c0c44674 NC |
4112 | } |
4113 | else { | |
4114 | GV *gv = gv_fetchsv(sstr, GV_ADD, SVt_PVGV); | |
daba3364 | 4115 | if (dstr != (const SV *)gv) { |
3e79609f FC |
4116 | const char * const name = GvNAME((const GV *)dstr); |
4117 | const STRLEN len = GvNAMELEN(dstr); | |
4118 | HV *old_stash = NULL; | |
4119 | bool reset_isa = FALSE; | |
4120 | if (len > 1 && name[len-2] == ':' && name[len-1] == ':') { | |
4121 | /* Set aside the old stash, so we can reset isa caches | |
4122 | on its subclasses. */ | |
c8eb3813 | 4123 | if((old_stash = GvHV(dstr))) { |
31f1461f FC |
4124 | /* Make sure we do not lose it early. */ |
4125 | SvREFCNT_inc_simple_void_NN( | |
4126 | sv_2mortal((SV *)old_stash) | |
4127 | ); | |
c8eb3813 | 4128 | } |
3e79609f FC |
4129 | reset_isa = TRUE; |
4130 | } | |
4131 | ||
c0c44674 | 4132 | if (GvGP(dstr)) |
159b6efe | 4133 | gp_free(MUTABLE_GV(dstr)); |
c43ae56f | 4134 | GvGP_set(dstr, gp_ref(GvGP(gv))); |
3e79609f FC |
4135 | |
4136 | if (reset_isa) { | |
d056e33c FC |
4137 | HV * const stash = GvHV(dstr); |
4138 | if( | |
78b79c77 | 4139 | old_stash ? (HV *)HvENAME_get(old_stash) : stash |
d056e33c FC |
4140 | ) |
4141 | mro_package_moved( | |
35759254 | 4142 | stash, old_stash, |
afdbe55d | 4143 | (GV *)dstr, 0 |
d056e33c | 4144 | ); |
3e79609f | 4145 | } |
c0c44674 NC |
4146 | } |
4147 | } | |
4148 | } | |
f0826785 BM |
4149 | else if (dtype == SVt_REGEXP && stype == SVt_REGEXP) { |
4150 | reg_temp_copy((REGEXP*)dstr, (REGEXP*)sstr); | |
4151 | } | |
8990e307 | 4152 | else if (sflags & SVp_POK) { |
765f542d | 4153 | bool isSwipe = 0; |
79072805 LW |
4154 | |
4155 | /* | |
4156 | * Check to see if we can just swipe the string. If so, it's a | |
4157 | * possible small lose on short strings, but a big win on long ones. | |
3f7c398e SP |
4158 | * It might even be a win on short strings if SvPVX_const(dstr) |
4159 | * has to be allocated and SvPVX_const(sstr) has to be freed. | |
34482cd6 NC |
4160 | * Likewise if we can set up COW rather than doing an actual copy, we |
4161 | * drop to the else clause, as the swipe code and the COW setup code | |
4162 | * have much in common. | |
79072805 LW |
4163 | */ |
4164 | ||
120fac95 NC |
4165 | /* Whichever path we take through the next code, we want this true, |
4166 | and doing it now facilitates the COW check. */ | |
4167 | (void)SvPOK_only(dstr); | |
4168 | ||
765f542d | 4169 | if ( |
34482cd6 NC |
4170 | /* If we're already COW then this clause is not true, and if COW |
4171 | is allowed then we drop down to the else and make dest COW | |
4172 | with us. If caller hasn't said that we're allowed to COW | |
4173 | shared hash keys then we don't do the COW setup, even if the | |
4174 | source scalar is a shared hash key scalar. */ | |
4175 | (((flags & SV_COW_SHARED_HASH_KEYS) | |
4176 | ? (sflags & (SVf_FAKE|SVf_READONLY)) != (SVf_FAKE|SVf_READONLY) | |
4177 | : 1 /* If making a COW copy is forbidden then the behaviour we | |
4178 | desire is as if the source SV isn't actually already | |
4179 | COW, even if it is. So we act as if the source flags | |
4180 | are not COW, rather than actually testing them. */ | |
4181 | ) | |
f8c7b90f | 4182 | #ifndef PERL_OLD_COPY_ON_WRITE |
34482cd6 NC |
4183 | /* The change that added SV_COW_SHARED_HASH_KEYS makes the logic |
4184 | when PERL_OLD_COPY_ON_WRITE is defined a little wrong. | |
4185 | Conceptually PERL_OLD_COPY_ON_WRITE being defined should | |
4186 | override SV_COW_SHARED_HASH_KEYS, because it means "always COW" | |
4187 | but in turn, it's somewhat dead code, never expected to go | |
4188 | live, but more kept as a placeholder on how to do it better | |
4189 | in a newer implementation. */ | |
4190 | /* If we are COW and dstr is a suitable target then we drop down | |
4191 | into the else and make dest a COW of us. */ | |
b8f9541a NC |
4192 | || (SvFLAGS(dstr) & CAN_COW_MASK) != CAN_COW_FLAGS |
4193 | #endif | |
4194 | ) | |
765f542d | 4195 | && |
765f542d NC |
4196 | !(isSwipe = |
4197 | (sflags & SVs_TEMP) && /* slated for free anyway? */ | |
4198 | !(sflags & SVf_OOK) && /* and not involved in OOK hack? */ | |
5fcdf167 NC |
4199 | (!(flags & SV_NOSTEAL)) && |
4200 | /* and we're allowed to steal temps */ | |
765f542d | 4201 | SvREFCNT(sstr) == 1 && /* and no other references to it? */ |
61e5f455 | 4202 | SvLEN(sstr)) /* and really is a string */ |
f8c7b90f | 4203 | #ifdef PERL_OLD_COPY_ON_WRITE |
cb23d5b1 NC |
4204 | && ((flags & SV_COW_SHARED_HASH_KEYS) |
4205 | ? (!((sflags & CAN_COW_MASK) == CAN_COW_FLAGS | |
4206 | && (SvFLAGS(dstr) & CAN_COW_MASK) == CAN_COW_FLAGS | |
4b1c7d9e | 4207 | && SvTYPE(sstr) >= SVt_PVIV && SvTYPE(sstr) != SVt_PVFM)) |
cb23d5b1 | 4208 | : 1) |
765f542d NC |
4209 | #endif |
4210 | ) { | |
4211 | /* Failed the swipe test, and it's not a shared hash key either. | |
4212 | Have to copy the string. */ | |
4213 | STRLEN len = SvCUR(sstr); | |
4214 | SvGROW(dstr, len + 1); /* inlined from sv_setpvn */ | |
3f7c398e | 4215 | Move(SvPVX_const(sstr),SvPVX(dstr),len,char); |
765f542d NC |
4216 | SvCUR_set(dstr, len); |
4217 | *SvEND(dstr) = '\0'; | |
765f542d | 4218 | } else { |
f8c7b90f | 4219 | /* If PERL_OLD_COPY_ON_WRITE is not defined, then isSwipe will always |
765f542d | 4220 | be true in here. */ |
765f542d NC |
4221 | /* Either it's a shared hash key, or it's suitable for |
4222 | copy-on-write or we can swipe the string. */ | |
46187eeb | 4223 | if (DEBUG_C_TEST) { |
ed252734 | 4224 | PerlIO_printf(Perl_debug_log, "Copy on write: sstr --> dstr\n"); |
e419cbc5 NC |
4225 | sv_dump(sstr); |
4226 | sv_dump(dstr); | |
46187eeb | 4227 | } |
f8c7b90f | 4228 | #ifdef PERL_OLD_COPY_ON_WRITE |
765f542d | 4229 | if (!isSwipe) { |
765f542d NC |
4230 | if ((sflags & (SVf_FAKE | SVf_READONLY)) |
4231 | != (SVf_FAKE | SVf_READONLY)) { | |
4232 | SvREADONLY_on(sstr); | |
4233 | SvFAKE_on(sstr); | |
4234 | /* Make the source SV into a loop of 1. | |
4235 | (about to become 2) */ | |
a29f6d03 | 4236 | SV_COW_NEXT_SV_SET(sstr, sstr); |
765f542d NC |
4237 | } |
4238 | } | |
4239 | #endif | |
4240 | /* Initial code is common. */ | |
94010e71 NC |
4241 | if (SvPVX_const(dstr)) { /* we know that dtype >= SVt_PV */ |
4242 | SvPV_free(dstr); | |
79072805 | 4243 | } |
765f542d | 4244 | |
765f542d NC |
4245 | if (!isSwipe) { |
4246 | /* making another shared SV. */ | |
4247 | STRLEN cur = SvCUR(sstr); | |
4248 | STRLEN len = SvLEN(sstr); | |
f8c7b90f | 4249 | #ifdef PERL_OLD_COPY_ON_WRITE |
765f542d | 4250 | if (len) { |
b8f9541a | 4251 | assert (SvTYPE(dstr) >= SVt_PVIV); |
765f542d NC |
4252 | /* SvIsCOW_normal */ |
4253 | /* splice us in between source and next-after-source. */ | |
a29f6d03 NC |
4254 | SV_COW_NEXT_SV_SET(dstr, SV_COW_NEXT_SV(sstr)); |
4255 | SV_COW_NEXT_SV_SET(sstr, dstr); | |
940132f3 | 4256 | SvPV_set(dstr, SvPVX_mutable(sstr)); |
a604c751 NC |
4257 | } else |
4258 | #endif | |
4259 | { | |
765f542d | 4260 | /* SvIsCOW_shared_hash */ |
46187eeb NC |
4261 | DEBUG_C(PerlIO_printf(Perl_debug_log, |
4262 | "Copy on write: Sharing hash\n")); | |
b8f9541a | 4263 | |
bdd68bc3 | 4264 | assert (SvTYPE(dstr) >= SVt_PV); |
765f542d | 4265 | SvPV_set(dstr, |
d1db91c6 | 4266 | HEK_KEY(share_hek_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sstr))))); |
bdd68bc3 | 4267 | } |
87a1ef3d SP |
4268 | SvLEN_set(dstr, len); |
4269 | SvCUR_set(dstr, cur); | |
765f542d NC |
4270 | SvREADONLY_on(dstr); |
4271 | SvFAKE_on(dstr); | |
765f542d NC |
4272 | } |
4273 | else | |
765f542d | 4274 | { /* Passes the swipe test. */ |
78d1e721 | 4275 | SvPV_set(dstr, SvPVX_mutable(sstr)); |
765f542d NC |
4276 | SvLEN_set(dstr, SvLEN(sstr)); |
4277 | SvCUR_set(dstr, SvCUR(sstr)); | |
4278 | ||
4279 | SvTEMP_off(dstr); | |
4280 | (void)SvOK_off(sstr); /* NOTE: nukes most SvFLAGS on sstr */ | |
6136c704 | 4281 | SvPV_set(sstr, NULL); |
765f542d NC |
4282 | SvLEN_set(sstr, 0); |
4283 | SvCUR_set(sstr, 0); | |
4284 | SvTEMP_off(sstr); | |
4285 | } | |
4286 | } | |
8990e307 | 4287 | if (sflags & SVp_NOK) { |
9d6ce603 | 4288 | SvNV_set(dstr, SvNVX(sstr)); |
79072805 | 4289 | } |
8990e307 | 4290 | if (sflags & SVp_IOK) { |
23525414 NC |
4291 | SvIV_set(dstr, SvIVX(sstr)); |
4292 | /* Must do this otherwise some other overloaded use of 0x80000000 | |
4293 | gets confused. I guess SVpbm_VALID */ | |
2b1c7e3e | 4294 | if (sflags & SVf_IVisUV) |
25da4f38 | 4295 | SvIsUV_on(dstr); |
79072805 | 4296 | } |
96d4b0ee | 4297 | SvFLAGS(dstr) |= sflags & (SVf_IOK|SVp_IOK|SVf_NOK|SVp_NOK|SVf_UTF8); |
4f2da183 | 4298 | { |
b0a11fe1 | 4299 | const MAGIC * const smg = SvVSTRING_mg(sstr); |
4f2da183 NC |
4300 | if (smg) { |
4301 | sv_magic(dstr, NULL, PERL_MAGIC_vstring, | |
4302 | smg->mg_ptr, smg->mg_len); | |
4303 | SvRMAGICAL_on(dstr); | |
4304 | } | |
7a5fa8a2 | 4305 | } |
79072805 | 4306 | } |
5d581361 | 4307 | else if (sflags & (SVp_IOK|SVp_NOK)) { |
c2468cc7 | 4308 | (void)SvOK_off(dstr); |
96d4b0ee | 4309 | SvFLAGS(dstr) |= sflags & (SVf_IOK|SVp_IOK|SVf_IVisUV|SVf_NOK|SVp_NOK); |
5d581361 NC |
4310 | if (sflags & SVp_IOK) { |
4311 | /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */ | |
4312 | SvIV_set(dstr, SvIVX(sstr)); | |
4313 | } | |
3332b3c1 | 4314 | if (sflags & SVp_NOK) { |
9d6ce603 | 4315 | SvNV_set(dstr, SvNVX(sstr)); |
3332b3c1 JH |
4316 | } |
4317 | } | |
79072805 | 4318 | else { |
f7877b28 | 4319 | if (isGV_with_GP(sstr)) { |
180488f8 NC |
4320 | /* This stringification rule for globs is spread in 3 places. |
4321 | This feels bad. FIXME. */ | |
4322 | const U32 wasfake = sflags & SVf_FAKE; | |
4323 | ||
4324 | /* FAKE globs can get coerced, so need to turn this off | |
4325 | temporarily if it is on. */ | |
4326 | SvFAKE_off(sstr); | |
159b6efe | 4327 | gv_efullname3(dstr, MUTABLE_GV(sstr), "*"); |
180488f8 NC |
4328 | SvFLAGS(sstr) |= wasfake; |
4329 | } | |
20408e3c GS |
4330 | else |
4331 | (void)SvOK_off(dstr); | |
a0d0e21e | 4332 | } |
27c9684d AP |
4333 | if (SvTAINTED(sstr)) |
4334 | SvTAINT(dstr); | |
79072805 LW |
4335 | } |
4336 | ||
954c1994 GS |
4337 | /* |
4338 | =for apidoc sv_setsv_mg | |
4339 | ||
4340 | Like C<sv_setsv>, but also handles 'set' magic. | |
4341 | ||
4342 | =cut | |
4343 | */ | |
4344 | ||
79072805 | 4345 | void |
7bc54cea | 4346 | Perl_sv_setsv_mg(pTHX_ SV *const dstr, register SV *const sstr) |
ef50df4b | 4347 | { |
7918f24d NC |
4348 | PERL_ARGS_ASSERT_SV_SETSV_MG; |
4349 | ||
ef50df4b GS |
4350 | sv_setsv(dstr,sstr); |
4351 | SvSETMAGIC(dstr); | |
4352 | } | |
4353 | ||
f8c7b90f | 4354 | #ifdef PERL_OLD_COPY_ON_WRITE |
ed252734 NC |
4355 | SV * |
4356 | Perl_sv_setsv_cow(pTHX_ SV *dstr, SV *sstr) | |
4357 | { | |
4358 | STRLEN cur = SvCUR(sstr); | |
4359 | STRLEN len = SvLEN(sstr); | |
4360 | register char *new_pv; | |
4361 | ||
7918f24d NC |
4362 | PERL_ARGS_ASSERT_SV_SETSV_COW; |
4363 | ||
ed252734 NC |
4364 | if (DEBUG_C_TEST) { |
4365 | PerlIO_printf(Perl_debug_log, "Fast copy on write: %p -> %p\n", | |
6c9570dc | 4366 | (void*)sstr, (void*)dstr); |
ed252734 NC |
4367 | sv_dump(sstr); |
4368 | if (dstr) | |
4369 | sv_dump(dstr); | |
4370 | } | |
4371 | ||
4372 | if (dstr) { | |
4373 | if (SvTHINKFIRST(dstr)) | |
4374 | sv_force_normal_flags(dstr, SV_COW_DROP_PV); | |
3f7c398e SP |
4375 | else if (SvPVX_const(dstr)) |
4376 | Safefree(SvPVX_const(dstr)); | |
ed252734 NC |
4377 | } |
4378 | else | |
4379 | new_SV(dstr); | |
862a34c6 | 4380 | SvUPGRADE(dstr, SVt_PVIV); |
ed252734 NC |
4381 | |
4382 | assert (SvPOK(sstr)); | |
4383 | assert (SvPOKp(sstr)); | |
4384 | assert (!SvIOK(sstr)); | |
4385 | assert (!SvIOKp(sstr)); | |
4386 | assert (!SvNOK(sstr)); | |
4387 | assert (!SvNOKp(sstr)); | |
4388 | ||
4389 | if (SvIsCOW(sstr)) { | |
4390 | ||
4391 | if (SvLEN(sstr) == 0) { | |
4392 | /* source is a COW shared hash key. */ | |
ed252734 NC |
4393 | DEBUG_C(PerlIO_printf(Perl_debug_log, |
4394 | "Fast copy on write: Sharing hash\n")); | |
d1db91c6 | 4395 | new_pv = HEK_KEY(share_hek_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sstr)))); |
ed252734 NC |
4396 | goto common_exit; |
4397 | } | |
4398 | SV_COW_NEXT_SV_SET(dstr, SV_COW_NEXT_SV(sstr)); | |
4399 | } else { | |
4400 | assert ((SvFLAGS(sstr) & CAN_COW_MASK) == CAN_COW_FLAGS); | |
862a34c6 | 4401 | SvUPGRADE(sstr, SVt_PVIV); |
ed252734 NC |
4402 | SvREADONLY_on(sstr); |
4403 | SvFAKE_on(sstr); | |
4404 | DEBUG_C(PerlIO_printf(Perl_debug_log, | |
4405 | "Fast copy on write: Converting sstr to COW\n")); | |
4406 | SV_COW_NEXT_SV_SET(dstr, sstr); | |
4407 | } | |
4408 | SV_COW_NEXT_SV_SET(sstr, dstr); | |
940132f3 | 4409 | new_pv = SvPVX_mutable(sstr); |
ed252734 NC |
4410 | |
4411 | common_exit: | |
4412 | SvPV_set(dstr, new_pv); | |
4413 | SvFLAGS(dstr) = (SVt_PVIV|SVf_POK|SVp_POK|SVf_FAKE|SVf_READONLY); | |
4414 | if (SvUTF8(sstr)) | |
4415 | SvUTF8_on(dstr); | |
87a1ef3d SP |
4416 | SvLEN_set(dstr, len); |
4417 | SvCUR_set(dstr, cur); | |
ed252734 NC |
4418 | if (DEBUG_C_TEST) { |
4419 | sv_dump(dstr); | |
4420 | } | |
4421 | return dstr; | |
4422 | } | |
4423 | #endif | |
4424 | ||
954c1994 GS |
4425 | /* |
4426 | =for apidoc sv_setpvn | |
4427 | ||
4428 | Copies a string into an SV. The C<len> parameter indicates the number of | |
9e09f5f2 MHM |
4429 | bytes to be copied. If the C<ptr> argument is NULL the SV will become |
4430 | undefined. Does not handle 'set' magic. See C<sv_setpvn_mg>. | |
954c1994 GS |
4431 | |
4432 | =cut | |
4433 | */ | |
4434 | ||
ef50df4b | 4435 | void |
2e000ff2 | 4436 | Perl_sv_setpvn(pTHX_ register SV *const sv, register const char *const ptr, register const STRLEN len) |
79072805 | 4437 | { |
97aff369 | 4438 | dVAR; |
c6f8c383 | 4439 | register char *dptr; |
22c522df | 4440 | |
7918f24d NC |
4441 | PERL_ARGS_ASSERT_SV_SETPVN; |
4442 | ||
765f542d | 4443 | SV_CHECK_THINKFIRST_COW_DROP(sv); |
463ee0b2 | 4444 | if (!ptr) { |
a0d0e21e | 4445 | (void)SvOK_off(sv); |
463ee0b2 LW |
4446 | return; |
4447 | } | |
22c522df JH |
4448 | else { |
4449 | /* len is STRLEN which is unsigned, need to copy to signed */ | |
a3b680e6 | 4450 | const IV iv = len; |
9c5ffd7c JH |
4451 | if (iv < 0) |
4452 | Perl_croak(aTHX_ "panic: sv_setpvn called with negative strlen"); | |
22c522df | 4453 | } |
862a34c6 | 4454 | SvUPGRADE(sv, SVt_PV); |
c6f8c383 | 4455 | |
5902b6a9 | 4456 | dptr = SvGROW(sv, len + 1); |
c6f8c383 GA |
4457 | Move(ptr,dptr,len,char); |
4458 | dptr[len] = '\0'; | |
79072805 | 4459 | SvCUR_set(sv, len); |
1aa99e6b | 4460 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 | 4461 | SvTAINT(sv); |
79072805 LW |
4462 | } |
4463 | ||
954c1994 GS |
4464 | /* |
4465 | =for apidoc sv_setpvn_mg | |
4466 | ||
4467 | Like C<sv_setpvn>, but also handles 'set' magic. | |
4468 | ||
4469 | =cut | |
4470 | */ | |
4471 | ||
79072805 | 4472 | void |
2e000ff2 | 4473 | Perl_sv_setpvn_mg(pTHX_ register SV *const sv, register const char *const ptr, register const STRLEN len) |
ef50df4b | 4474 | { |
7918f24d NC |
4475 | PERL_ARGS_ASSERT_SV_SETPVN_MG; |
4476 | ||
ef50df4b GS |
4477 | sv_setpvn(sv,ptr,len); |
4478 | SvSETMAGIC(sv); | |
4479 | } | |
4480 | ||
954c1994 GS |
4481 | /* |
4482 | =for apidoc sv_setpv | |
4483 | ||
4484 | Copies a string into an SV. The string must be null-terminated. Does not | |
4485 | handle 'set' magic. See C<sv_setpv_mg>. | |
4486 | ||
4487 | =cut | |
4488 | */ | |
4489 | ||
ef50df4b | 4490 | void |
2e000ff2 | 4491 | Perl_sv_setpv(pTHX_ register SV *const sv, register const char *const ptr) |
79072805 | 4492 | { |
97aff369 | 4493 | dVAR; |
79072805 LW |
4494 | register STRLEN len; |
4495 | ||
7918f24d NC |
4496 | PERL_ARGS_ASSERT_SV_SETPV; |
4497 | ||
765f542d | 4498 | SV_CHECK_THINKFIRST_COW_DROP(sv); |
463ee0b2 | 4499 | if (!ptr) { |
a0d0e21e | 4500 | (void)SvOK_off(sv); |
463ee0b2 LW |
4501 | return; |
4502 | } | |
79072805 | 4503 | len = strlen(ptr); |
862a34c6 | 4504 | SvUPGRADE(sv, SVt_PV); |
c6f8c383 | 4505 | |
79072805 | 4506 | SvGROW(sv, len + 1); |
463ee0b2 | 4507 | Move(ptr,SvPVX(sv),len+1,char); |
79072805 | 4508 | SvCUR_set(sv, len); |
1aa99e6b | 4509 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 LW |
4510 | SvTAINT(sv); |
4511 | } | |
4512 | ||
954c1994 GS |
4513 | /* |
4514 | =for apidoc sv_setpv_mg | |
4515 | ||
4516 | Like C<sv_setpv>, but also handles 'set' magic. | |
4517 | ||
4518 | =cut | |
4519 | */ | |
4520 | ||
463ee0b2 | 4521 | void |
2e000ff2 | 4522 | Perl_sv_setpv_mg(pTHX_ register SV *const sv, register const char *const ptr) |
ef50df4b | 4523 | { |
7918f24d NC |
4524 | PERL_ARGS_ASSERT_SV_SETPV_MG; |
4525 | ||
ef50df4b GS |
4526 | sv_setpv(sv,ptr); |
4527 | SvSETMAGIC(sv); | |
4528 | } | |
4529 | ||
954c1994 | 4530 | /* |
47518d95 | 4531 | =for apidoc sv_usepvn_flags |
954c1994 | 4532 | |
794a0d33 JH |
4533 | Tells an SV to use C<ptr> to find its string value. Normally the |
4534 | string is stored inside the SV but sv_usepvn allows the SV to use an | |
4535 | outside string. The C<ptr> should point to memory that was allocated | |
c1c21316 NC |
4536 | by C<malloc>. The string length, C<len>, must be supplied. By default |
4537 | this function will realloc (i.e. move) the memory pointed to by C<ptr>, | |
794a0d33 JH |
4538 | so that pointer should not be freed or used by the programmer after |
4539 | giving it to sv_usepvn, and neither should any pointers from "behind" | |
c1c21316 NC |
4540 | that pointer (e.g. ptr + 1) be used. |
4541 | ||
4542 | If C<flags> & SV_SMAGIC is true, will call SvSETMAGIC. If C<flags> & | |
4543 | SV_HAS_TRAILING_NUL is true, then C<ptr[len]> must be NUL, and the realloc | |
cbf82dd0 | 4544 | will be skipped. (i.e. the buffer is actually at least 1 byte longer than |
c1c21316 | 4545 | C<len>, and already meets the requirements for storing in C<SvPVX>) |
954c1994 GS |
4546 | |
4547 | =cut | |
4548 | */ | |
4549 | ||
ef50df4b | 4550 | void |
2e000ff2 | 4551 | Perl_sv_usepvn_flags(pTHX_ SV *const sv, char *ptr, const STRLEN len, const U32 flags) |
463ee0b2 | 4552 | { |
97aff369 | 4553 | dVAR; |
1936d2a7 | 4554 | STRLEN allocate; |
7918f24d NC |
4555 | |
4556 | PERL_ARGS_ASSERT_SV_USEPVN_FLAGS; | |
4557 | ||
765f542d | 4558 | SV_CHECK_THINKFIRST_COW_DROP(sv); |
862a34c6 | 4559 | SvUPGRADE(sv, SVt_PV); |
463ee0b2 | 4560 | if (!ptr) { |
a0d0e21e | 4561 | (void)SvOK_off(sv); |
47518d95 NC |
4562 | if (flags & SV_SMAGIC) |
4563 | SvSETMAGIC(sv); | |
463ee0b2 LW |
4564 | return; |
4565 | } | |
3f7c398e | 4566 | if (SvPVX_const(sv)) |
8bd4d4c5 | 4567 | SvPV_free(sv); |
1936d2a7 | 4568 | |
0b7042f9 | 4569 | #ifdef DEBUGGING |
2e90b4cd NC |
4570 | if (flags & SV_HAS_TRAILING_NUL) |
4571 | assert(ptr[len] == '\0'); | |
0b7042f9 | 4572 | #endif |
2e90b4cd | 4573 | |
c1c21316 | 4574 | allocate = (flags & SV_HAS_TRAILING_NUL) |
5d487c26 | 4575 | ? len + 1 : |
ca7c1a29 | 4576 | #ifdef Perl_safesysmalloc_size |
5d487c26 NC |
4577 | len + 1; |
4578 | #else | |
4579 | PERL_STRLEN_ROUNDUP(len + 1); | |
4580 | #endif | |
cbf82dd0 NC |
4581 | if (flags & SV_HAS_TRAILING_NUL) { |
4582 | /* It's long enough - do nothing. | |
486ec47a | 4583 | Specifically Perl_newCONSTSUB is relying on this. */ |
cbf82dd0 | 4584 | } else { |
69d25b4f | 4585 | #ifdef DEBUGGING |
69d25b4f | 4586 | /* Force a move to shake out bugs in callers. */ |
10edeb5d | 4587 | char *new_ptr = (char*)safemalloc(allocate); |
69d25b4f NC |
4588 | Copy(ptr, new_ptr, len, char); |
4589 | PoisonFree(ptr,len,char); | |
4590 | Safefree(ptr); | |
4591 | ptr = new_ptr; | |
69d25b4f | 4592 | #else |
10edeb5d | 4593 | ptr = (char*) saferealloc (ptr, allocate); |
69d25b4f | 4594 | #endif |
cbf82dd0 | 4595 | } |
ca7c1a29 NC |
4596 | #ifdef Perl_safesysmalloc_size |
4597 | SvLEN_set(sv, Perl_safesysmalloc_size(ptr)); | |
5d487c26 | 4598 | #else |
1936d2a7 | 4599 | SvLEN_set(sv, allocate); |
5d487c26 NC |
4600 | #endif |
4601 | SvCUR_set(sv, len); | |
4602 | SvPV_set(sv, ptr); | |
c1c21316 | 4603 | if (!(flags & SV_HAS_TRAILING_NUL)) { |
97a130b8 | 4604 | ptr[len] = '\0'; |
c1c21316 | 4605 | } |
1aa99e6b | 4606 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 | 4607 | SvTAINT(sv); |
47518d95 NC |
4608 | if (flags & SV_SMAGIC) |
4609 | SvSETMAGIC(sv); | |
ef50df4b GS |
4610 | } |
4611 | ||
f8c7b90f | 4612 | #ifdef PERL_OLD_COPY_ON_WRITE |
765f542d NC |
4613 | /* Need to do this *after* making the SV normal, as we need the buffer |
4614 | pointer to remain valid until after we've copied it. If we let go too early, | |
4615 | another thread could invalidate it by unsharing last of the same hash key | |
4616 | (which it can do by means other than releasing copy-on-write Svs) | |
4617 | or by changing the other copy-on-write SVs in the loop. */ | |
4618 | STATIC void | |
5302ffd4 | 4619 | S_sv_release_COW(pTHX_ register SV *sv, const char *pvx, SV *after) |
765f542d | 4620 | { |
7918f24d NC |
4621 | PERL_ARGS_ASSERT_SV_RELEASE_COW; |
4622 | ||
5302ffd4 | 4623 | { /* this SV was SvIsCOW_normal(sv) */ |
765f542d | 4624 | /* we need to find the SV pointing to us. */ |
cf5629ad | 4625 | SV *current = SV_COW_NEXT_SV(after); |
7a5fa8a2 | 4626 | |
765f542d NC |
4627 | if (current == sv) { |
4628 | /* The SV we point to points back to us (there were only two of us | |
4629 | in the loop.) | |
4630 | Hence other SV is no longer copy on write either. */ | |
4631 | SvFAKE_off(after); | |
4632 | SvREADONLY_off(after); | |
4633 | } else { | |
4634 | /* We need to follow the pointers around the loop. */ | |
4635 | SV *next; | |
4636 | while ((next = SV_COW_NEXT_SV(current)) != sv) { | |
4637 | assert (next); | |
4638 | current = next; | |
4639 | /* don't loop forever if the structure is bust, and we have | |
4640 | a pointer into a closed loop. */ | |
4641 | assert (current != after); | |
3f7c398e | 4642 | assert (SvPVX_const(current) == pvx); |
765f542d NC |
4643 | } |
4644 | /* Make the SV before us point to the SV after us. */ | |
a29f6d03 | 4645 | SV_COW_NEXT_SV_SET(current, after); |
765f542d | 4646 | } |
765f542d NC |
4647 | } |
4648 | } | |
765f542d | 4649 | #endif |
645c22ef DM |
4650 | /* |
4651 | =for apidoc sv_force_normal_flags | |
4652 | ||
4653 | Undo various types of fakery on an SV: if the PV is a shared string, make | |
4654 | a private copy; if we're a ref, stop refing; if we're a glob, downgrade to | |
765f542d NC |
4655 | an xpvmg; if we're a copy-on-write scalar, this is the on-write time when |
4656 | we do the copy, and is also used locally. If C<SV_COW_DROP_PV> is set | |
4657 | then a copy-on-write scalar drops its PV buffer (if any) and becomes | |
4658 | SvPOK_off rather than making a copy. (Used where this scalar is about to be | |
d3050d9d | 4659 | set to some other value.) In addition, the C<flags> parameter gets passed to |
765f542d NC |
4660 | C<sv_unref_flags()> when unrefing. C<sv_force_normal> calls this function |
4661 | with flags set to 0. | |
645c22ef DM |
4662 | |
4663 | =cut | |
4664 | */ | |
4665 | ||
6fc92669 | 4666 | void |
2e000ff2 | 4667 | Perl_sv_force_normal_flags(pTHX_ register SV *const sv, const U32 flags) |
0f15f207 | 4668 | { |
97aff369 | 4669 | dVAR; |
7918f24d NC |
4670 | |
4671 | PERL_ARGS_ASSERT_SV_FORCE_NORMAL_FLAGS; | |
4672 | ||
f8c7b90f | 4673 | #ifdef PERL_OLD_COPY_ON_WRITE |
765f542d | 4674 | if (SvREADONLY(sv)) { |
765f542d | 4675 | if (SvFAKE(sv)) { |
b64e5050 | 4676 | const char * const pvx = SvPVX_const(sv); |
a28509cc AL |
4677 | const STRLEN len = SvLEN(sv); |
4678 | const STRLEN cur = SvCUR(sv); | |
5302ffd4 NC |
4679 | /* next COW sv in the loop. If len is 0 then this is a shared-hash |
4680 | key scalar, so we mustn't attempt to call SV_COW_NEXT_SV(), as | |
4681 | we'll fail an assertion. */ | |
4682 | SV * const next = len ? SV_COW_NEXT_SV(sv) : 0; | |
4683 | ||
46187eeb NC |
4684 | if (DEBUG_C_TEST) { |
4685 | PerlIO_printf(Perl_debug_log, | |
4686 | "Copy on write: Force normal %ld\n", | |
4687 | (long) flags); | |
e419cbc5 | 4688 | sv_dump(sv); |
46187eeb | 4689 | } |
765f542d NC |
4690 | SvFAKE_off(sv); |
4691 | SvREADONLY_off(sv); | |
9f653bb5 | 4692 | /* This SV doesn't own the buffer, so need to Newx() a new one: */ |
6136c704 | 4693 | SvPV_set(sv, NULL); |
87a1ef3d | 4694 | SvLEN_set(sv, 0); |
765f542d NC |
4695 | if (flags & SV_COW_DROP_PV) { |
4696 | /* OK, so we don't need to copy our buffer. */ | |
4697 | SvPOK_off(sv); | |
4698 | } else { | |
4699 | SvGROW(sv, cur + 1); | |
4700 | Move(pvx,SvPVX(sv),cur,char); | |
87a1ef3d | 4701 | SvCUR_set(sv, cur); |
765f542d NC |
4702 | *SvEND(sv) = '\0'; |
4703 | } | |
5302ffd4 NC |
4704 | if (len) { |
4705 | sv_release_COW(sv, pvx, next); | |
4706 | } else { | |
4707 | unshare_hek(SvSHARED_HEK_FROM_PV(pvx)); | |
4708 | } | |
46187eeb | 4709 | if (DEBUG_C_TEST) { |
e419cbc5 | 4710 | sv_dump(sv); |
46187eeb | 4711 | } |
765f542d | 4712 | } |
923e4eb5 | 4713 | else if (IN_PERL_RUNTIME) |
6ad8f254 | 4714 | Perl_croak_no_modify(aTHX); |
765f542d NC |
4715 | } |
4716 | #else | |
2213622d | 4717 | if (SvREADONLY(sv)) { |
1c846c1f | 4718 | if (SvFAKE(sv)) { |
b64e5050 | 4719 | const char * const pvx = SvPVX_const(sv); |
66a1b24b | 4720 | const STRLEN len = SvCUR(sv); |
10bcdfd6 NC |
4721 | SvFAKE_off(sv); |
4722 | SvREADONLY_off(sv); | |
bd61b366 | 4723 | SvPV_set(sv, NULL); |
66a1b24b | 4724 | SvLEN_set(sv, 0); |
1c846c1f | 4725 | SvGROW(sv, len + 1); |
706aa1c9 | 4726 | Move(pvx,SvPVX(sv),len,char); |
1c846c1f | 4727 | *SvEND(sv) = '\0'; |
bdd68bc3 | 4728 | unshare_hek(SvSHARED_HEK_FROM_PV(pvx)); |
1c846c1f | 4729 | } |
923e4eb5 | 4730 | else if (IN_PERL_RUNTIME) |
6ad8f254 | 4731 | Perl_croak_no_modify(aTHX); |
0f15f207 | 4732 | } |
765f542d | 4733 | #endif |
2213622d | 4734 | if (SvROK(sv)) |
840a7b70 | 4735 | sv_unref_flags(sv, flags); |
13be902c | 4736 | else if (SvFAKE(sv) && isGV_with_GP(sv)) |
6fc92669 | 4737 | sv_unglob(sv); |
b9ad13ac | 4738 | else if (SvFAKE(sv) && SvTYPE(sv) == SVt_REGEXP) { |
486ec47a | 4739 | /* Need to downgrade the REGEXP to a simple(r) scalar. This is analogous |
b9ad13ac NC |
4740 | to sv_unglob. We only need it here, so inline it. */ |
4741 | const svtype new_type = SvMAGIC(sv) || SvSTASH(sv) ? SVt_PVMG : SVt_PV; | |
4742 | SV *const temp = newSV_type(new_type); | |
4743 | void *const temp_p = SvANY(sv); | |
4744 | ||
4745 | if (new_type == SVt_PVMG) { | |
4746 | SvMAGIC_set(temp, SvMAGIC(sv)); | |
4747 | SvMAGIC_set(sv, NULL); | |
4748 | SvSTASH_set(temp, SvSTASH(sv)); | |
4749 | SvSTASH_set(sv, NULL); | |
4750 | } | |
4751 | SvCUR_set(temp, SvCUR(sv)); | |
4752 | /* Remember that SvPVX is in the head, not the body. */ | |
4753 | if (SvLEN(temp)) { | |
4754 | SvLEN_set(temp, SvLEN(sv)); | |
4755 | /* This signals "buffer is owned by someone else" in sv_clear, | |
4756 | which is the least effort way to stop it freeing the buffer. | |
4757 | */ | |
4758 | SvLEN_set(sv, SvLEN(sv)+1); | |
4759 | } else { | |
4760 | /* Their buffer is already owned by someone else. */ | |
4761 | SvPVX(sv) = savepvn(SvPVX(sv), SvCUR(sv)); | |
4762 | SvLEN_set(temp, SvCUR(sv)+1); | |
4763 | } | |
4764 | ||
4765 | /* Now swap the rest of the bodies. */ | |
4766 | ||
4767 | SvFLAGS(sv) &= ~(SVf_FAKE|SVTYPEMASK); | |
4768 | SvFLAGS(sv) |= new_type; | |
4769 | SvANY(sv) = SvANY(temp); | |
4770 | ||
4771 | SvFLAGS(temp) &= ~(SVTYPEMASK); | |
4772 | SvFLAGS(temp) |= SVt_REGEXP|SVf_FAKE; | |
4773 | SvANY(temp) = temp_p; | |
4774 | ||
4775 | SvREFCNT_dec(temp); | |
4776 | } | |
0f15f207 | 4777 | } |
1c846c1f | 4778 | |
645c22ef | 4779 | /* |
954c1994 GS |
4780 | =for apidoc sv_chop |
4781 | ||
1c846c1f | 4782 | Efficient removal of characters from the beginning of the string buffer. |
954c1994 GS |
4783 | SvPOK(sv) must be true and the C<ptr> must be a pointer to somewhere inside |
4784 | the string buffer. The C<ptr> becomes the first character of the adjusted | |
645c22ef | 4785 | string. Uses the "OOK hack". |
3f7c398e | 4786 | Beware: after this function returns, C<ptr> and SvPVX_const(sv) may no longer |
31869a79 | 4787 | refer to the same chunk of data. |
954c1994 GS |
4788 | |
4789 | =cut | |
4790 | */ | |
4791 | ||
79072805 | 4792 | void |
2e000ff2 | 4793 | Perl_sv_chop(pTHX_ register SV *const sv, register const char *const ptr) |
79072805 | 4794 | { |
69240efd NC |
4795 | STRLEN delta; |
4796 | STRLEN old_delta; | |
7a4bba22 NC |
4797 | U8 *p; |
4798 | #ifdef DEBUGGING | |
4799 | const U8 *real_start; | |
4800 | #endif | |
6c65d5f9 | 4801 | STRLEN max_delta; |
7a4bba22 | 4802 | |
7918f24d NC |
4803 | PERL_ARGS_ASSERT_SV_CHOP; |
4804 | ||
a0d0e21e | 4805 | if (!ptr || !SvPOKp(sv)) |
79072805 | 4806 | return; |
3f7c398e | 4807 | delta = ptr - SvPVX_const(sv); |
15895f8a NC |
4808 | if (!delta) { |
4809 | /* Nothing to do. */ | |
4810 | return; | |
4811 | } | |
6c65d5f9 NC |
4812 | /* SvPVX(sv) may move in SV_CHECK_THINKFIRST(sv), but after this line, |
4813 | nothing uses the value of ptr any more. */ | |
837cb3ba | 4814 | max_delta = SvLEN(sv) ? SvLEN(sv) : SvCUR(sv); |
6c65d5f9 NC |
4815 | if (ptr <= SvPVX_const(sv)) |
4816 | Perl_croak(aTHX_ "panic: sv_chop ptr=%p, start=%p, end=%p", | |
4817 | ptr, SvPVX_const(sv), SvPVX_const(sv) + max_delta); | |
2213622d | 4818 | SV_CHECK_THINKFIRST(sv); |
6c65d5f9 NC |
4819 | if (delta > max_delta) |
4820 | Perl_croak(aTHX_ "panic: sv_chop ptr=%p (was %p), start=%p, end=%p", | |
4821 | SvPVX_const(sv) + delta, ptr, SvPVX_const(sv), | |
4822 | SvPVX_const(sv) + max_delta); | |
79072805 LW |
4823 | |
4824 | if (!SvOOK(sv)) { | |
50483b2c | 4825 | if (!SvLEN(sv)) { /* make copy of shared string */ |
3f7c398e | 4826 | const char *pvx = SvPVX_const(sv); |
a28509cc | 4827 | const STRLEN len = SvCUR(sv); |
50483b2c | 4828 | SvGROW(sv, len + 1); |
706aa1c9 | 4829 | Move(pvx,SvPVX(sv),len,char); |
50483b2c JD |
4830 | *SvEND(sv) = '\0'; |
4831 | } | |
7a5fa8a2 | 4832 | SvFLAGS(sv) |= SVf_OOK; |
7a4bba22 NC |
4833 | old_delta = 0; |
4834 | } else { | |
69240efd | 4835 | SvOOK_offset(sv, old_delta); |
79072805 | 4836 | } |
b162af07 SP |
4837 | SvLEN_set(sv, SvLEN(sv) - delta); |
4838 | SvCUR_set(sv, SvCUR(sv) - delta); | |
f880fe2f | 4839 | SvPV_set(sv, SvPVX(sv) + delta); |
7a4bba22 NC |
4840 | |
4841 | p = (U8 *)SvPVX_const(sv); | |
4842 | ||
4843 | delta += old_delta; | |
4844 | ||
50af2e61 | 4845 | #ifdef DEBUGGING |
7a4bba22 NC |
4846 | real_start = p - delta; |
4847 | #endif | |
4848 | ||
69240efd NC |
4849 | assert(delta); |
4850 | if (delta < 0x100) { | |
7a4bba22 NC |
4851 | *--p = (U8) delta; |
4852 | } else { | |
69240efd NC |
4853 | *--p = 0; |
4854 | p -= sizeof(STRLEN); | |
4855 | Copy((U8*)&delta, p, sizeof(STRLEN), U8); | |
7a4bba22 NC |
4856 | } |
4857 | ||
4858 | #ifdef DEBUGGING | |
4859 | /* Fill the preceding buffer with sentinals to verify that no-one is | |
4860 | using it. */ | |
4861 | while (p > real_start) { | |
4862 | --p; | |
4863 | *p = (U8)PTR2UV(p); | |
50af2e61 NC |
4864 | } |
4865 | #endif | |
79072805 LW |
4866 | } |
4867 | ||
954c1994 GS |
4868 | /* |
4869 | =for apidoc sv_catpvn | |
4870 | ||
4871 | Concatenates the string onto the end of the string which is in the SV. The | |
1e54db1a JH |
4872 | C<len> indicates number of bytes to copy. If the SV has the UTF-8 |
4873 | status set, then the bytes appended should be valid UTF-8. | |
d5ce4a7c | 4874 | Handles 'get' magic, but not 'set' magic. See C<sv_catpvn_mg>. |
954c1994 | 4875 | |
8d6d96c1 HS |
4876 | =for apidoc sv_catpvn_flags |
4877 | ||
4878 | Concatenates the string onto the end of the string which is in the SV. The | |
1e54db1a JH |
4879 | C<len> indicates number of bytes to copy. If the SV has the UTF-8 |
4880 | status set, then the bytes appended should be valid UTF-8. | |
8d6d96c1 HS |
4881 | If C<flags> has C<SV_GMAGIC> bit set, will C<mg_get> on C<dsv> if |
4882 | appropriate, else not. C<sv_catpvn> and C<sv_catpvn_nomg> are implemented | |
4883 | in terms of this function. | |
4884 | ||
4885 | =cut | |
4886 | */ | |
4887 | ||
4888 | void | |
2e000ff2 | 4889 | Perl_sv_catpvn_flags(pTHX_ register SV *const dsv, register const char *sstr, register const STRLEN slen, const I32 flags) |
8d6d96c1 | 4890 | { |
97aff369 | 4891 | dVAR; |
8d6d96c1 | 4892 | STRLEN dlen; |
fabdb6c0 | 4893 | const char * const dstr = SvPV_force_flags(dsv, dlen, flags); |
8d6d96c1 | 4894 | |
7918f24d NC |
4895 | PERL_ARGS_ASSERT_SV_CATPVN_FLAGS; |
4896 | ||
8d6d96c1 HS |
4897 | SvGROW(dsv, dlen + slen + 1); |
4898 | if (sstr == dstr) | |
3f7c398e | 4899 | sstr = SvPVX_const(dsv); |
8d6d96c1 | 4900 | Move(sstr, SvPVX(dsv) + dlen, slen, char); |
b162af07 | 4901 | SvCUR_set(dsv, SvCUR(dsv) + slen); |
8d6d96c1 HS |
4902 | *SvEND(dsv) = '\0'; |
4903 | (void)SvPOK_only_UTF8(dsv); /* validate pointer */ | |
4904 | SvTAINT(dsv); | |
bddd5118 NC |
4905 | if (flags & SV_SMAGIC) |
4906 | SvSETMAGIC(dsv); | |
79072805 LW |
4907 | } |
4908 | ||
954c1994 | 4909 | /* |
954c1994 GS |
4910 | =for apidoc sv_catsv |
4911 | ||
13e8c8e3 JH |
4912 | Concatenates the string from SV C<ssv> onto the end of the string in |
4913 | SV C<dsv>. Modifies C<dsv> but not C<ssv>. Handles 'get' magic, but | |
4914 | not 'set' magic. See C<sv_catsv_mg>. | |
954c1994 | 4915 | |
8d6d96c1 HS |
4916 | =for apidoc sv_catsv_flags |
4917 | ||
4918 | Concatenates the string from SV C<ssv> onto the end of the string in | |
4919 | SV C<dsv>. Modifies C<dsv> but not C<ssv>. If C<flags> has C<SV_GMAGIC> | |
4920 | bit set, will C<mg_get> on the SVs if appropriate, else not. C<sv_catsv> | |
4921 | and C<sv_catsv_nomg> are implemented in terms of this function. | |
4922 | ||
4923 | =cut */ | |
4924 | ||
ef50df4b | 4925 | void |
2e000ff2 | 4926 | Perl_sv_catsv_flags(pTHX_ SV *const dsv, register SV *const ssv, const I32 flags) |
79072805 | 4927 | { |
97aff369 | 4928 | dVAR; |
7918f24d NC |
4929 | |
4930 | PERL_ARGS_ASSERT_SV_CATSV_FLAGS; | |
4931 | ||
4932 | if (ssv) { | |
00b6aa41 | 4933 | STRLEN slen; |
a9984b10 | 4934 | const char *spv = SvPV_flags_const(ssv, slen, flags); |
00b6aa41 | 4935 | if (spv) { |
bddd5118 NC |
4936 | /* sutf8 and dutf8 were type bool, but under USE_ITHREADS, |
4937 | gcc version 2.95.2 20000220 (Debian GNU/Linux) for | |
4938 | Linux xxx 2.2.17 on sparc64 with gcc -O2, we erroneously | |
4939 | get dutf8 = 0x20000000, (i.e. SVf_UTF8) even though | |
4940 | dsv->sv_flags doesn't have that bit set. | |
4fd84b44 | 4941 | Andy Dougherty 12 Oct 2001 |
bddd5118 NC |
4942 | */ |
4943 | const I32 sutf8 = DO_UTF8(ssv); | |
4944 | I32 dutf8; | |
13e8c8e3 | 4945 | |
bddd5118 NC |
4946 | if (SvGMAGICAL(dsv) && (flags & SV_GMAGIC)) |
4947 | mg_get(dsv); | |
4948 | dutf8 = DO_UTF8(dsv); | |
8d6d96c1 | 4949 | |
bddd5118 NC |
4950 | if (dutf8 != sutf8) { |
4951 | if (dutf8) { | |
4952 | /* Not modifying source SV, so taking a temporary copy. */ | |
59cd0e26 | 4953 | SV* const csv = newSVpvn_flags(spv, slen, SVs_TEMP); |
13e8c8e3 | 4954 | |
bddd5118 NC |
4955 | sv_utf8_upgrade(csv); |
4956 | spv = SvPV_const(csv, slen); | |
4957 | } | |
4958 | else | |
7bf79863 KW |
4959 | /* Leave enough space for the cat that's about to happen */ |
4960 | sv_utf8_upgrade_flags_grow(dsv, 0, slen); | |
13e8c8e3 | 4961 | } |
bddd5118 | 4962 | sv_catpvn_nomg(dsv, spv, slen); |
e84ff256 | 4963 | } |
560a288e | 4964 | } |
bddd5118 NC |
4965 | if (flags & SV_SMAGIC) |
4966 | SvSETMAGIC(dsv); | |
79072805 LW |
4967 | } |
4968 | ||
954c1994 | 4969 | /* |
954c1994 GS |
4970 | =for apidoc sv_catpv |
4971 | ||
4972 | Concatenates the string onto the end of the string which is in the SV. | |
1e54db1a JH |
4973 | If the SV has the UTF-8 status set, then the bytes appended should be |
4974 | valid UTF-8. Handles 'get' magic, but not 'set' magic. See C<sv_catpv_mg>. | |
954c1994 | 4975 | |
d5ce4a7c | 4976 | =cut */ |
954c1994 | 4977 | |
ef50df4b | 4978 | void |
2b021c53 | 4979 | Perl_sv_catpv(pTHX_ register SV *const sv, register const char *ptr) |
79072805 | 4980 | { |
97aff369 | 4981 | dVAR; |
79072805 | 4982 | register STRLEN len; |
463ee0b2 | 4983 | STRLEN tlen; |
748a9306 | 4984 | char *junk; |
79072805 | 4985 | |
7918f24d NC |
4986 | PERL_ARGS_ASSERT_SV_CATPV; |
4987 | ||
0c981600 | 4988 | if (!ptr) |
79072805 | 4989 | return; |
748a9306 | 4990 | junk = SvPV_force(sv, tlen); |
0c981600 | 4991 | len = strlen(ptr); |
463ee0b2 | 4992 | SvGROW(sv, tlen + len + 1); |
0c981600 | 4993 | if (ptr == junk) |
3f7c398e | 4994 | ptr = SvPVX_const(sv); |
0c981600 | 4995 | Move(ptr,SvPVX(sv)+tlen,len+1,char); |
b162af07 | 4996 | SvCUR_set(sv, SvCUR(sv) + len); |
d41ff1b8 | 4997 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 | 4998 | SvTAINT(sv); |
79072805 LW |
4999 | } |
5000 | ||
954c1994 | 5001 | /* |
9dcc53ea Z |
5002 | =for apidoc sv_catpv_flags |
5003 | ||
5004 | Concatenates the string onto the end of the string which is in the SV. | |
5005 | If the SV has the UTF-8 status set, then the bytes appended should | |
5006 | be valid UTF-8. If C<flags> has C<SV_GMAGIC> bit set, will C<mg_get> | |
5007 | on the SVs if appropriate, else not. | |
5008 | ||
5009 | =cut | |
5010 | */ | |
5011 | ||
5012 | void | |
fe00c367 | 5013 | Perl_sv_catpv_flags(pTHX_ SV *dstr, const char *sstr, const I32 flags) |
9dcc53ea Z |
5014 | { |
5015 | PERL_ARGS_ASSERT_SV_CATPV_FLAGS; | |
5016 | sv_catpvn_flags(dstr, sstr, strlen(sstr), flags); | |
5017 | } | |
5018 | ||
5019 | /* | |
954c1994 GS |
5020 | =for apidoc sv_catpv_mg |
5021 | ||
5022 | Like C<sv_catpv>, but also handles 'set' magic. | |
5023 | ||
5024 | =cut | |
5025 | */ | |
5026 | ||
ef50df4b | 5027 | void |
2b021c53 | 5028 | Perl_sv_catpv_mg(pTHX_ register SV *const sv, register const char *const ptr) |
ef50df4b | 5029 | { |
7918f24d NC |
5030 | PERL_ARGS_ASSERT_SV_CATPV_MG; |
5031 | ||
0c981600 | 5032 | sv_catpv(sv,ptr); |
ef50df4b GS |
5033 | SvSETMAGIC(sv); |
5034 | } | |
5035 | ||
645c22ef DM |
5036 | /* |
5037 | =for apidoc newSV | |
5038 | ||
561b68a9 SH |
5039 | Creates a new SV. A non-zero C<len> parameter indicates the number of |
5040 | bytes of preallocated string space the SV should have. An extra byte for a | |
5041 | trailing NUL is also reserved. (SvPOK is not set for the SV even if string | |
5042 | space is allocated.) The reference count for the new SV is set to 1. | |
5043 | ||
5044 | In 5.9.3, newSV() replaces the older NEWSV() API, and drops the first | |
5045 | parameter, I<x>, a debug aid which allowed callers to identify themselves. | |
5046 | This aid has been superseded by a new build option, PERL_MEM_LOG (see | |
5047 | L<perlhack/PERL_MEM_LOG>). The older API is still there for use in XS | |
5048 | modules supporting older perls. | |
645c22ef DM |
5049 | |
5050 | =cut | |
5051 | */ | |
5052 | ||
79072805 | 5053 | SV * |
2b021c53 | 5054 | Perl_newSV(pTHX_ const STRLEN len) |
79072805 | 5055 | { |
97aff369 | 5056 | dVAR; |
79072805 | 5057 | register SV *sv; |
1c846c1f | 5058 | |
4561caa4 | 5059 | new_SV(sv); |
79072805 LW |
5060 | if (len) { |
5061 | sv_upgrade(sv, SVt_PV); | |
5062 | SvGROW(sv, len + 1); | |
5063 | } | |
5064 | return sv; | |
5065 | } | |
954c1994 | 5066 | /* |
92110913 | 5067 | =for apidoc sv_magicext |
954c1994 | 5068 | |
68795e93 | 5069 | Adds magic to an SV, upgrading it if necessary. Applies the |
2d8d5d5a | 5070 | supplied vtable and returns a pointer to the magic added. |
92110913 | 5071 | |
2d8d5d5a SH |
5072 | Note that C<sv_magicext> will allow things that C<sv_magic> will not. |
5073 | In particular, you can add magic to SvREADONLY SVs, and add more than | |
5074 | one instance of the same 'how'. | |
645c22ef | 5075 | |
2d8d5d5a SH |
5076 | If C<namlen> is greater than zero then a C<savepvn> I<copy> of C<name> is |
5077 | stored, if C<namlen> is zero then C<name> is stored as-is and - as another | |
5078 | special case - if C<(name && namlen == HEf_SVKEY)> then C<name> is assumed | |
5079 | to contain an C<SV*> and is stored as-is with its REFCNT incremented. | |
92110913 | 5080 | |
2d8d5d5a | 5081 | (This is now used as a subroutine by C<sv_magic>.) |
954c1994 GS |
5082 | |
5083 | =cut | |
5084 | */ | |
92110913 | 5085 | MAGIC * |
2b021c53 SS |
5086 | Perl_sv_magicext(pTHX_ SV *const sv, SV *const obj, const int how, |
5087 | const MGVTBL *const vtable, const char *const name, const I32 namlen) | |
79072805 | 5088 | { |
97aff369 | 5089 | dVAR; |
79072805 | 5090 | MAGIC* mg; |
68795e93 | 5091 | |
7918f24d NC |
5092 | PERL_ARGS_ASSERT_SV_MAGICEXT; |
5093 | ||
7a7f3e45 | 5094 | SvUPGRADE(sv, SVt_PVMG); |
a02a5408 | 5095 | Newxz(mg, 1, MAGIC); |
79072805 | 5096 | mg->mg_moremagic = SvMAGIC(sv); |
b162af07 | 5097 | SvMAGIC_set(sv, mg); |
75f9d97a | 5098 | |
05f95b08 SB |
5099 | /* Sometimes a magic contains a reference loop, where the sv and |
5100 | object refer to each other. To prevent a reference loop that | |
5101 | would prevent such objects being freed, we look for such loops | |
5102 | and if we find one we avoid incrementing the object refcount. | |
87f0b213 JH |
5103 | |
5104 | Note we cannot do this to avoid self-tie loops as intervening RV must | |
b5ccf5f2 | 5105 | have its REFCNT incremented to keep it in existence. |
87f0b213 JH |
5106 | |
5107 | */ | |
14befaf4 DM |
5108 | if (!obj || obj == sv || |
5109 | how == PERL_MAGIC_arylen || | |
8d2f4536 | 5110 | how == PERL_MAGIC_symtab || |
75f9d97a | 5111 | (SvTYPE(obj) == SVt_PVGV && |
4c4652b6 NC |
5112 | (GvSV(obj) == sv || GvHV(obj) == (const HV *)sv |
5113 | || GvAV(obj) == (const AV *)sv || GvCV(obj) == (const CV *)sv | |
5114 | || GvIOp(obj) == (const IO *)sv || GvFORM(obj) == (const CV *)sv))) | |
75f9d97a | 5115 | { |
8990e307 | 5116 | mg->mg_obj = obj; |
75f9d97a | 5117 | } |
85e6fe83 | 5118 | else { |
b37c2d43 | 5119 | mg->mg_obj = SvREFCNT_inc_simple(obj); |
85e6fe83 LW |
5120 | mg->mg_flags |= MGf_REFCOUNTED; |
5121 | } | |
b5ccf5f2 YST |
5122 | |
5123 | /* Normal self-ties simply pass a null object, and instead of | |
5124 | using mg_obj directly, use the SvTIED_obj macro to produce a | |
5125 | new RV as needed. For glob "self-ties", we are tieing the PVIO | |
5126 | with an RV obj pointing to the glob containing the PVIO. In | |
5127 | this case, to avoid a reference loop, we need to weaken the | |
5128 | reference. | |
5129 | */ | |
5130 | ||
5131 | if (how == PERL_MAGIC_tiedscalar && SvTYPE(sv) == SVt_PVIO && | |
a45c7426 | 5132 | obj && SvROK(obj) && GvIO(SvRV(obj)) == (const IO *)sv) |
b5ccf5f2 YST |
5133 | { |
5134 | sv_rvweaken(obj); | |
5135 | } | |
5136 | ||
79072805 | 5137 | mg->mg_type = how; |
565764a8 | 5138 | mg->mg_len = namlen; |
9cbac4c7 | 5139 | if (name) { |
92110913 | 5140 | if (namlen > 0) |
1edc1566 | 5141 | mg->mg_ptr = savepvn(name, namlen); |
daba3364 NC |
5142 | else if (namlen == HEf_SVKEY) { |
5143 | /* Yes, this is casting away const. This is only for the case of | |
486ec47a | 5144 | HEf_SVKEY. I think we need to document this aberation of the |
daba3364 NC |
5145 | constness of the API, rather than making name non-const, as |
5146 | that change propagating outwards a long way. */ | |
5147 | mg->mg_ptr = (char*)SvREFCNT_inc_simple_NN((SV *)name); | |
5148 | } else | |
92110913 | 5149 | mg->mg_ptr = (char *) name; |
9cbac4c7 | 5150 | } |
53d44271 | 5151 | mg->mg_virtual = (MGVTBL *) vtable; |
68795e93 | 5152 | |
92110913 NIS |
5153 | mg_magical(sv); |
5154 | if (SvGMAGICAL(sv)) | |
5155 | SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK); | |
5156 | return mg; | |
5157 | } | |
5158 | ||
5159 | /* | |
5160 | =for apidoc sv_magic | |
1c846c1f | 5161 | |
92110913 NIS |
5162 | Adds magic to an SV. First upgrades C<sv> to type C<SVt_PVMG> if necessary, |
5163 | then adds a new magic item of type C<how> to the head of the magic list. | |
5164 | ||
2d8d5d5a SH |
5165 | See C<sv_magicext> (which C<sv_magic> now calls) for a description of the |
5166 | handling of the C<name> and C<namlen> arguments. | |
5167 | ||
4509d3fb SB |
5168 | You need to use C<sv_magicext> to add magic to SvREADONLY SVs and also |
5169 | to add more than one instance of the same 'how'. | |
5170 | ||
92110913 NIS |
5171 | =cut |
5172 | */ | |
5173 | ||
5174 | void | |
2b021c53 SS |
5175 | Perl_sv_magic(pTHX_ register SV *const sv, SV *const obj, const int how, |
5176 | const char *const name, const I32 namlen) | |
68795e93 | 5177 | { |
97aff369 | 5178 | dVAR; |
53d44271 | 5179 | const MGVTBL *vtable; |
92110913 | 5180 | MAGIC* mg; |
92110913 | 5181 | |
7918f24d NC |
5182 | PERL_ARGS_ASSERT_SV_MAGIC; |
5183 | ||
f8c7b90f | 5184 | #ifdef PERL_OLD_COPY_ON_WRITE |
765f542d NC |
5185 | if (SvIsCOW(sv)) |
5186 | sv_force_normal_flags(sv, 0); | |
5187 | #endif | |
92110913 | 5188 | if (SvREADONLY(sv)) { |
d8084ca5 DM |
5189 | if ( |
5190 | /* its okay to attach magic to shared strings; the subsequent | |
5191 | * upgrade to PVMG will unshare the string */ | |
5192 | !(SvFAKE(sv) && SvTYPE(sv) < SVt_PVMG) | |
5193 | ||
5194 | && IN_PERL_RUNTIME | |
92110913 NIS |
5195 | && how != PERL_MAGIC_regex_global |
5196 | && how != PERL_MAGIC_bm | |
5197 | && how != PERL_MAGIC_fm | |
5198 | && how != PERL_MAGIC_sv | |
e6469971 | 5199 | && how != PERL_MAGIC_backref |
92110913 NIS |
5200 | ) |
5201 | { | |
6ad8f254 | 5202 | Perl_croak_no_modify(aTHX); |
92110913 NIS |
5203 | } |
5204 | } | |
5205 | if (SvMAGICAL(sv) || (how == PERL_MAGIC_taint && SvTYPE(sv) >= SVt_PVMG)) { | |
5206 | if (SvMAGIC(sv) && (mg = mg_find(sv, how))) { | |
68795e93 NIS |
5207 | /* sv_magic() refuses to add a magic of the same 'how' as an |
5208 | existing one | |
92110913 | 5209 | */ |
2a509ed3 | 5210 | if (how == PERL_MAGIC_taint) { |
92110913 | 5211 | mg->mg_len |= 1; |
2a509ed3 NC |
5212 | /* Any scalar which already had taint magic on which someone |
5213 | (erroneously?) did SvIOK_on() or similar will now be | |
5214 | incorrectly sporting public "OK" flags. */ | |
5215 | SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK); | |
5216 | } | |
92110913 NIS |
5217 | return; |
5218 | } | |
5219 | } | |
68795e93 | 5220 | |
79072805 | 5221 | switch (how) { |
14befaf4 | 5222 | case PERL_MAGIC_sv: |
92110913 | 5223 | vtable = &PL_vtbl_sv; |
79072805 | 5224 | break; |
14befaf4 | 5225 | case PERL_MAGIC_overload: |
92110913 | 5226 | vtable = &PL_vtbl_amagic; |
a0d0e21e | 5227 | break; |
14befaf4 | 5228 | case PERL_MAGIC_overload_elem: |
92110913 | 5229 | vtable = &PL_vtbl_amagicelem; |
a0d0e21e | 5230 | break; |
14befaf4 | 5231 | case PERL_MAGIC_overload_table: |
92110913 | 5232 | vtable = &PL_vtbl_ovrld; |
a0d0e21e | 5233 | break; |
14befaf4 | 5234 | case PERL_MAGIC_bm: |
92110913 | 5235 | vtable = &PL_vtbl_bm; |
79072805 | 5236 | break; |
14befaf4 | 5237 | case PERL_MAGIC_regdata: |
92110913 | 5238 | vtable = &PL_vtbl_regdata; |
6cef1e77 | 5239 | break; |
14befaf4 | 5240 | case PERL_MAGIC_regdatum: |
92110913 | 5241 | vtable = &PL_vtbl_regdatum; |
6cef1e77 | 5242 | break; |
14befaf4 | 5243 | case PERL_MAGIC_env: |
92110913 | 5244 | vtable = &PL_vtbl_env; |
79072805 | 5245 | break; |
14befaf4 | 5246 | case PERL_MAGIC_fm: |
92110913 | 5247 | vtable = &PL_vtbl_fm; |
55497cff | 5248 | break; |
14befaf4 | 5249 | case PERL_MAGIC_envelem: |
92110913 | 5250 | vtable = &PL_vtbl_envelem; |
79072805 | 5251 | break; |
14befaf4 | 5252 | case PERL_MAGIC_regex_global: |
92110913 | 5253 | vtable = &PL_vtbl_mglob; |
93a17b20 | 5254 | break; |
14befaf4 | 5255 | case PERL_MAGIC_isa: |
92110913 | 5256 | vtable = &PL_vtbl_isa; |
463ee0b2 | 5257 | break; |
14befaf4 | 5258 | case PERL_MAGIC_isaelem: |
92110913 | 5259 | vtable = &PL_vtbl_isaelem; |
463ee0b2 | 5260 | break; |
14befaf4 | 5261 | case PERL_MAGIC_nkeys: |
92110913 | 5262 | vtable = &PL_vtbl_nkeys; |
16660edb | 5263 | break; |
14befaf4 | 5264 | case PERL_MAGIC_dbfile: |
aec46f14 | 5265 | vtable = NULL; |
93a17b20 | 5266 | break; |
14befaf4 | 5267 | case PERL_MAGIC_dbline: |
92110913 | 5268 | vtable = &PL_vtbl_dbline; |
79072805 | 5269 | break; |
36477c24 | 5270 | #ifdef USE_LOCALE_COLLATE |
14befaf4 | 5271 | case PERL_MAGIC_collxfrm: |
92110913 | 5272 | vtable = &PL_vtbl_collxfrm; |
bbce6d69 | 5273 | break; |
36477c24 | 5274 | #endif /* USE_LOCALE_COLLATE */ |
14befaf4 | 5275 | case PERL_MAGIC_tied: |
92110913 | 5276 | vtable = &PL_vtbl_pack; |
463ee0b2 | 5277 | break; |
14befaf4 DM |
5278 | case PERL_MAGIC_tiedelem: |
5279 | case PERL_MAGIC_tiedscalar: | |
92110913 | 5280 | vtable = &PL_vtbl_packelem; |
463ee0b2 | 5281 | break; |
14befaf4 | 5282 | case PERL_MAGIC_qr: |
92110913 | 5283 | vtable = &PL_vtbl_regexp; |
c277df42 | 5284 | break; |
14befaf4 | 5285 | case PERL_MAGIC_sig: |
92110913 | 5286 | vtable = &PL_vtbl_sig; |
79072805 | 5287 | break; |
14befaf4 | 5288 | case PERL_MAGIC_sigelem: |
92110913 | 5289 | vtable = &PL_vtbl_sigelem; |
79072805 | 5290 | break; |
14befaf4 | 5291 | case PERL_MAGIC_taint: |
92110913 | 5292 | vtable = &PL_vtbl_taint; |
463ee0b2 | 5293 | break; |
14befaf4 | 5294 | case PERL_MAGIC_uvar: |
92110913 | 5295 | vtable = &PL_vtbl_uvar; |
79072805 | 5296 | break; |
14befaf4 | 5297 | case PERL_MAGIC_vec: |
92110913 | 5298 | vtable = &PL_vtbl_vec; |
79072805 | 5299 | break; |
a3874608 | 5300 | case PERL_MAGIC_arylen_p: |
bfcb3514 | 5301 | case PERL_MAGIC_rhash: |
8d2f4536 | 5302 | case PERL_MAGIC_symtab: |
ece467f9 | 5303 | case PERL_MAGIC_vstring: |
d9088386 | 5304 | case PERL_MAGIC_checkcall: |
aec46f14 | 5305 | vtable = NULL; |
ece467f9 | 5306 | break; |
7e8c5dac HS |
5307 | case PERL_MAGIC_utf8: |
5308 | vtable = &PL_vtbl_utf8; | |
5309 | break; | |
14befaf4 | 5310 | case PERL_MAGIC_substr: |
92110913 | 5311 | vtable = &PL_vtbl_substr; |
79072805 | 5312 | break; |
14befaf4 | 5313 | case PERL_MAGIC_defelem: |
92110913 | 5314 | vtable = &PL_vtbl_defelem; |
5f05dabc | 5315 | break; |
14befaf4 | 5316 | case PERL_MAGIC_arylen: |
92110913 | 5317 | vtable = &PL_vtbl_arylen; |
79072805 | 5318 | break; |
14befaf4 | 5319 | case PERL_MAGIC_pos: |
92110913 | 5320 | vtable = &PL_vtbl_pos; |
a0d0e21e | 5321 | break; |
14befaf4 | 5322 | case PERL_MAGIC_backref: |
92110913 | 5323 | vtable = &PL_vtbl_backref; |
810b8aa5 | 5324 | break; |
b3ca2e83 NC |
5325 | case PERL_MAGIC_hintselem: |
5326 | vtable = &PL_vtbl_hintselem; | |
5327 | break; | |
f747ebd6 Z |
5328 | case PERL_MAGIC_hints: |
5329 | vtable = &PL_vtbl_hints; | |
5330 | break; | |
14befaf4 DM |
5331 | case PERL_MAGIC_ext: |
5332 | /* Reserved for use by extensions not perl internals. */ | |
4633a7c4 LW |
5333 | /* Useful for attaching extension internal data to perl vars. */ |
5334 | /* Note that multiple extensions may clash if magical scalars */ | |
5335 | /* etc holding private data from one are passed to another. */ | |
aec46f14 | 5336 | vtable = NULL; |
a0d0e21e | 5337 | break; |
79072805 | 5338 | default: |
14befaf4 | 5339 | Perl_croak(aTHX_ "Don't know how to handle magic of type \\%o", how); |
463ee0b2 | 5340 | } |
68795e93 | 5341 | |
92110913 | 5342 | /* Rest of work is done else where */ |
aec46f14 | 5343 | mg = sv_magicext(sv,obj,how,vtable,name,namlen); |
68795e93 | 5344 | |
92110913 NIS |
5345 | switch (how) { |
5346 | case PERL_MAGIC_taint: | |
5347 | mg->mg_len = 1; | |
5348 | break; | |
5349 | case PERL_MAGIC_ext: | |
5350 | case PERL_MAGIC_dbfile: | |
5351 | SvRMAGICAL_on(sv); | |
5352 | break; | |
5353 | } | |
463ee0b2 LW |
5354 | } |
5355 | ||
5356 | int | |
b83794c7 | 5357 | S_sv_unmagicext_flags(pTHX_ SV *const sv, const int type, MGVTBL *vtbl, const U32 flags) |
463ee0b2 LW |
5358 | { |
5359 | MAGIC* mg; | |
5360 | MAGIC** mgp; | |
7918f24d | 5361 | |
b83794c7 | 5362 | assert(flags <= 1); |
7918f24d | 5363 | |
91bba347 | 5364 | if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv)) |
463ee0b2 | 5365 | return 0; |
064cf529 | 5366 | mgp = &(((XPVMG*) SvANY(sv))->xmg_u.xmg_magic); |
463ee0b2 | 5367 | for (mg = *mgp; mg; mg = *mgp) { |
b83794c7 FR |
5368 | const MGVTBL* const virt = mg->mg_virtual; |
5369 | if (mg->mg_type == type && (!flags || virt == vtbl)) { | |
463ee0b2 | 5370 | *mgp = mg->mg_moremagic; |
b83794c7 FR |
5371 | if (virt && virt->svt_free) |
5372 | virt->svt_free(aTHX_ sv, mg); | |
14befaf4 | 5373 | if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) { |
92110913 | 5374 | if (mg->mg_len > 0) |
1edc1566 | 5375 | Safefree(mg->mg_ptr); |
565764a8 | 5376 | else if (mg->mg_len == HEf_SVKEY) |
daba3364 | 5377 | SvREFCNT_dec(MUTABLE_SV(mg->mg_ptr)); |
d2923cdd | 5378 | else if (mg->mg_type == PERL_MAGIC_utf8) |
7e8c5dac | 5379 | Safefree(mg->mg_ptr); |
9cbac4c7 | 5380 | } |
a0d0e21e LW |
5381 | if (mg->mg_flags & MGf_REFCOUNTED) |
5382 | SvREFCNT_dec(mg->mg_obj); | |
463ee0b2 LW |
5383 | Safefree(mg); |
5384 | } | |
5385 | else | |
5386 | mgp = &mg->mg_moremagic; | |
79072805 | 5387 | } |
806e7ca7 CS |
5388 | if (SvMAGIC(sv)) { |
5389 | if (SvMAGICAL(sv)) /* if we're under save_magic, wait for restore_magic; */ | |
5390 | mg_magical(sv); /* else fix the flags now */ | |
5391 | } | |
5392 | else { | |
463ee0b2 | 5393 | SvMAGICAL_off(sv); |
c268c2a6 | 5394 | SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT; |
463ee0b2 | 5395 | } |
463ee0b2 | 5396 | return 0; |
79072805 LW |
5397 | } |
5398 | ||
c461cf8f | 5399 | /* |
b83794c7 FR |
5400 | =for apidoc sv_unmagic |
5401 | ||
5402 | Removes all magic of type C<type> from an SV. | |
5403 | ||
5404 | =cut | |
5405 | */ | |
5406 | ||
5407 | int | |
5408 | Perl_sv_unmagic(pTHX_ SV *const sv, const int type) | |
5409 | { | |
5410 | PERL_ARGS_ASSERT_SV_UNMAGIC; | |
5411 | return S_sv_unmagicext_flags(aTHX_ sv, type, NULL, 0); | |
5412 | } | |
5413 | ||
5414 | /* | |
5415 | =for apidoc sv_unmagicext | |
5416 | ||
5417 | Removes all magic of type C<type> with the specified C<vtbl> from an SV. | |
5418 | ||
5419 | =cut | |
5420 | */ | |
5421 | ||
5422 | int | |
5423 | Perl_sv_unmagicext(pTHX_ SV *const sv, const int type, MGVTBL *vtbl) | |
5424 | { | |
5425 | PERL_ARGS_ASSERT_SV_UNMAGICEXT; | |
5426 | return S_sv_unmagicext_flags(aTHX_ sv, type, vtbl, 1); | |
5427 | } | |
5428 | ||
5429 | /* | |
c461cf8f JH |
5430 | =for apidoc sv_rvweaken |
5431 | ||
645c22ef DM |
5432 | Weaken a reference: set the C<SvWEAKREF> flag on this RV; give the |
5433 | referred-to SV C<PERL_MAGIC_backref> magic if it hasn't already; and | |
5434 | push a back-reference to this RV onto the array of backreferences | |
1e73acc8 AS |
5435 | associated with that magic. If the RV is magical, set magic will be |
5436 | called after the RV is cleared. | |
c461cf8f JH |
5437 | |
5438 | =cut | |
5439 | */ | |
5440 | ||
810b8aa5 | 5441 | SV * |
2b021c53 | 5442 | Perl_sv_rvweaken(pTHX_ SV *const sv) |
810b8aa5 GS |
5443 | { |
5444 | SV *tsv; | |
7918f24d NC |
5445 | |
5446 | PERL_ARGS_ASSERT_SV_RVWEAKEN; | |
5447 | ||
810b8aa5 GS |
5448 | if (!SvOK(sv)) /* let undefs pass */ |
5449 | return sv; | |
5450 | if (!SvROK(sv)) | |
cea2e8a9 | 5451 | Perl_croak(aTHX_ "Can't weaken a nonreference"); |
810b8aa5 | 5452 | else if (SvWEAKREF(sv)) { |
a2a5de95 | 5453 | Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Reference is already weak"); |
810b8aa5 GS |
5454 | return sv; |
5455 | } | |
5456 | tsv = SvRV(sv); | |
e15faf7d | 5457 | Perl_sv_add_backref(aTHX_ tsv, sv); |
810b8aa5 | 5458 | SvWEAKREF_on(sv); |
1c846c1f | 5459 | SvREFCNT_dec(tsv); |
810b8aa5 GS |
5460 | return sv; |
5461 | } | |
5462 | ||
645c22ef DM |
5463 | /* Give tsv backref magic if it hasn't already got it, then push a |
5464 | * back-reference to sv onto the array associated with the backref magic. | |
5648c0ae DM |
5465 | * |
5466 | * As an optimisation, if there's only one backref and it's not an AV, | |
5467 | * store it directly in the HvAUX or mg_obj slot, avoiding the need to | |
5468 | * allocate an AV. (Whether the slot holds an AV tells us whether this is | |
5469 | * active.) | |
8ac9a371 DM |
5470 | * |
5471 | * If an HV's backref is stored in magic, it is moved back to HvAUX. | |
645c22ef DM |
5472 | */ |
5473 | ||
fd996479 DM |
5474 | /* A discussion about the backreferences array and its refcount: |
5475 | * | |
5476 | * The AV holding the backreferences is pointed to either as the mg_obj of | |
09aad8f0 DM |
5477 | * PERL_MAGIC_backref, or in the specific case of a HV that has the hv_aux |
5478 | * structure, from the xhv_backreferences field. (A HV without hv_aux will | |
5479 | * have the standard magic instead.) The array is created with a refcount | |
5480 | * of 2. This means that if during global destruction the array gets | |
cef0c2ea DM |
5481 | * picked on before its parent to have its refcount decremented by the |
5482 | * random zapper, it won't actually be freed, meaning it's still there for | |
5483 | * when its parent gets freed. | |
5648c0ae DM |
5484 | * |
5485 | * When the parent SV is freed, the extra ref is killed by | |
5486 | * Perl_sv_kill_backrefs. The other ref is killed, in the case of magic, | |
5487 | * by mg_free() / MGf_REFCOUNTED, or for a hash, by Perl_hv_kill_backrefs. | |
5488 | * | |
5489 | * When a single backref SV is stored directly, it is not reference | |
5490 | * counted. | |
fd996479 DM |
5491 | */ |
5492 | ||
e15faf7d | 5493 | void |
2b021c53 | 5494 | Perl_sv_add_backref(pTHX_ SV *const tsv, SV *const sv) |
810b8aa5 | 5495 | { |
97aff369 | 5496 | dVAR; |
757971c4 | 5497 | SV **svp; |
5648c0ae | 5498 | AV *av = NULL; |
757971c4 | 5499 | MAGIC *mg = NULL; |
86f55936 | 5500 | |
7918f24d NC |
5501 | PERL_ARGS_ASSERT_SV_ADD_BACKREF; |
5502 | ||
5648c0ae DM |
5503 | /* find slot to store array or singleton backref */ |
5504 | ||
86f55936 | 5505 | if (SvTYPE(tsv) == SVt_PVHV) { |
757971c4 | 5506 | svp = (SV**)Perl_hv_backreferences_p(aTHX_ MUTABLE_HV(tsv)); |
09aad8f0 | 5507 | |
757971c4 DM |
5508 | if (!*svp) { |
5509 | if ((mg = mg_find(tsv, PERL_MAGIC_backref))) { | |
5510 | /* Aha. They've got it stowed in magic instead. | |
5511 | * Move it back to xhv_backreferences */ | |
5512 | *svp = mg->mg_obj; | |
cdb996f4 | 5513 | /* Stop mg_free decreasing the reference count. */ |
09aad8f0 DM |
5514 | mg->mg_obj = NULL; |
5515 | /* Stop mg_free even calling the destructor, given that | |
5516 | there's no AV to free up. */ | |
5517 | mg->mg_virtual = 0; | |
5518 | sv_unmagic(tsv, PERL_MAGIC_backref); | |
757971c4 | 5519 | mg = NULL; |
09aad8f0 | 5520 | } |
86f55936 NC |
5521 | } |
5522 | } else { | |
757971c4 DM |
5523 | if (! ((mg = |
5524 | (SvMAGICAL(tsv) ? mg_find(tsv, PERL_MAGIC_backref) : NULL)))) | |
5525 | { | |
5526 | sv_magic(tsv, NULL, PERL_MAGIC_backref, NULL, 0); | |
5527 | mg = mg_find(tsv, PERL_MAGIC_backref); | |
86f55936 | 5528 | } |
757971c4 | 5529 | svp = &(mg->mg_obj); |
810b8aa5 | 5530 | } |
757971c4 | 5531 | |
5648c0ae DM |
5532 | /* create or retrieve the array */ |
5533 | ||
5534 | if ( (!*svp && SvTYPE(sv) == SVt_PVAV) | |
5535 | || (*svp && SvTYPE(*svp) != SVt_PVAV) | |
5536 | ) { | |
5537 | /* create array */ | |
757971c4 DM |
5538 | av = newAV(); |
5539 | AvREAL_off(av); | |
5540 | SvREFCNT_inc_simple_void(av); | |
5541 | /* av now has a refcnt of 2; see discussion above */ | |
5648c0ae DM |
5542 | if (*svp) { |
5543 | /* move single existing backref to the array */ | |
5544 | av_extend(av, 1); | |
5545 | AvARRAY(av)[++AvFILLp(av)] = *svp; /* av_push() */ | |
5546 | } | |
5547 | *svp = (SV*)av; | |
757971c4 DM |
5548 | if (mg) |
5549 | mg->mg_flags |= MGf_REFCOUNTED; | |
757971c4 DM |
5550 | } |
5551 | else | |
5648c0ae | 5552 | av = MUTABLE_AV(*svp); |
757971c4 | 5553 | |
5648c0ae DM |
5554 | if (!av) { |
5555 | /* optimisation: store single backref directly in HvAUX or mg_obj */ | |
5556 | *svp = sv; | |
5557 | return; | |
5558 | } | |
5559 | /* push new backref */ | |
5560 | assert(SvTYPE(av) == SVt_PVAV); | |
d91d49e8 | 5561 | if (AvFILLp(av) >= AvMAX(av)) { |
d91d49e8 MM |
5562 | av_extend(av, AvFILLp(av)+1); |
5563 | } | |
5564 | AvARRAY(av)[++AvFILLp(av)] = sv; /* av_push() */ | |
810b8aa5 GS |
5565 | } |
5566 | ||
645c22ef DM |
5567 | /* delete a back-reference to ourselves from the backref magic associated |
5568 | * with the SV we point to. | |
5569 | */ | |
5570 | ||
4c74a7df DM |
5571 | void |
5572 | Perl_sv_del_backref(pTHX_ SV *const tsv, SV *const sv) | |
810b8aa5 | 5573 | { |
97aff369 | 5574 | dVAR; |
5648c0ae | 5575 | SV **svp = NULL; |
86f55936 | 5576 | |
7918f24d NC |
5577 | PERL_ARGS_ASSERT_SV_DEL_BACKREF; |
5578 | ||
317ec34c | 5579 | if (SvTYPE(tsv) == SVt_PVHV && SvOOK(tsv)) { |
5648c0ae | 5580 | svp = (SV**)Perl_hv_backreferences_p(aTHX_ MUTABLE_HV(tsv)); |
86f55936 | 5581 | } |
5648c0ae DM |
5582 | if (!svp || !*svp) { |
5583 | MAGIC *const mg | |
86f55936 | 5584 | = SvMAGICAL(tsv) ? mg_find(tsv, PERL_MAGIC_backref) : NULL; |
5648c0ae | 5585 | svp = mg ? &(mg->mg_obj) : NULL; |
86f55936 | 5586 | } |
41fae7a1 | 5587 | |
5648c0ae | 5588 | if (!svp || !*svp) |
cea2e8a9 | 5589 | Perl_croak(aTHX_ "panic: del_backref"); |
86f55936 | 5590 | |
5648c0ae | 5591 | if (SvTYPE(*svp) == SVt_PVAV) { |
51698cb3 DM |
5592 | #ifdef DEBUGGING |
5593 | int count = 1; | |
5594 | #endif | |
5648c0ae | 5595 | AV * const av = (AV*)*svp; |
51698cb3 | 5596 | SSize_t fill; |
5648c0ae | 5597 | assert(!SvIS_FREED(av)); |
51698cb3 DM |
5598 | fill = AvFILLp(av); |
5599 | assert(fill > -1); | |
5648c0ae | 5600 | svp = AvARRAY(av); |
51698cb3 DM |
5601 | /* for an SV with N weak references to it, if all those |
5602 | * weak refs are deleted, then sv_del_backref will be called | |
5603 | * N times and O(N^2) compares will be done within the backref | |
5604 | * array. To ameliorate this potential slowness, we: | |
5605 | * 1) make sure this code is as tight as possible; | |
5606 | * 2) when looking for SV, look for it at both the head and tail of the | |
5607 | * array first before searching the rest, since some create/destroy | |
5608 | * patterns will cause the backrefs to be freed in order. | |
5609 | */ | |
5610 | if (*svp == sv) { | |
5611 | AvARRAY(av)++; | |
5612 | AvMAX(av)--; | |
5613 | } | |
5614 | else { | |
5615 | SV **p = &svp[fill]; | |
5616 | SV *const topsv = *p; | |
5617 | if (topsv != sv) { | |
5618 | #ifdef DEBUGGING | |
5619 | count = 0; | |
5620 | #endif | |
5621 | while (--p > svp) { | |
5622 | if (*p == sv) { | |
5623 | /* We weren't the last entry. | |
5624 | An unordered list has this property that you | |
5625 | can take the last element off the end to fill | |
5626 | the hole, and it's still an unordered list :-) | |
5627 | */ | |
5628 | *p = topsv; | |
5629 | #ifdef DEBUGGING | |
5630 | count++; | |
5631 | #else | |
5632 | break; /* should only be one */ | |
254f8c6a | 5633 | #endif |
51698cb3 DM |
5634 | } |
5635 | } | |
6a76db8b | 5636 | } |
6a76db8b | 5637 | } |
51698cb3 DM |
5638 | assert(count ==1); |
5639 | AvFILLp(av) = fill-1; | |
6a76db8b | 5640 | } |
5648c0ae DM |
5641 | else { |
5642 | /* optimisation: only a single backref, stored directly */ | |
5643 | if (*svp != sv) | |
5644 | Perl_croak(aTHX_ "panic: del_backref"); | |
5645 | *svp = NULL; | |
5646 | } | |
5647 | ||
810b8aa5 GS |
5648 | } |
5649 | ||
5648c0ae | 5650 | void |
2b021c53 | 5651 | Perl_sv_kill_backrefs(pTHX_ SV *const sv, AV *const av) |
86f55936 | 5652 | { |
5648c0ae DM |
5653 | SV **svp; |
5654 | SV **last; | |
5655 | bool is_array; | |
86f55936 | 5656 | |
7918f24d | 5657 | PERL_ARGS_ASSERT_SV_KILL_BACKREFS; |
86f55936 | 5658 | |
5648c0ae DM |
5659 | if (!av) |
5660 | return; | |
86f55936 | 5661 | |
5648c0ae DM |
5662 | is_array = (SvTYPE(av) == SVt_PVAV); |
5663 | if (is_array) { | |
cef0c2ea | 5664 | assert(!SvIS_FREED(av)); |
5648c0ae DM |
5665 | svp = AvARRAY(av); |
5666 | if (svp) | |
5667 | last = svp + AvFILLp(av); | |
5668 | } | |
5669 | else { | |
5670 | /* optimisation: only a single backref, stored directly */ | |
5671 | svp = (SV**)&av; | |
5672 | last = svp; | |
5673 | } | |
5674 | ||
5675 | if (svp) { | |
86f55936 NC |
5676 | while (svp <= last) { |
5677 | if (*svp) { | |
5678 | SV *const referrer = *svp; | |
5679 | if (SvWEAKREF(referrer)) { | |
5680 | /* XXX Should we check that it hasn't changed? */ | |
4c74a7df | 5681 | assert(SvROK(referrer)); |
86f55936 NC |
5682 | SvRV_set(referrer, 0); |
5683 | SvOK_off(referrer); | |
5684 | SvWEAKREF_off(referrer); | |
1e73acc8 | 5685 | SvSETMAGIC(referrer); |
86f55936 NC |
5686 | } else if (SvTYPE(referrer) == SVt_PVGV || |
5687 | SvTYPE(referrer) == SVt_PVLV) { | |
803f2748 | 5688 | assert(SvTYPE(sv) == SVt_PVHV); /* stash backref */ |
86f55936 NC |
5689 | /* You lookin' at me? */ |
5690 | assert(GvSTASH(referrer)); | |
1d193675 | 5691 | assert(GvSTASH(referrer) == (const HV *)sv); |
86f55936 | 5692 | GvSTASH(referrer) = 0; |
803f2748 DM |
5693 | } else if (SvTYPE(referrer) == SVt_PVCV || |
5694 | SvTYPE(referrer) == SVt_PVFM) { | |
5695 | if (SvTYPE(sv) == SVt_PVHV) { /* stash backref */ | |
5696 | /* You lookin' at me? */ | |
5697 | assert(CvSTASH(referrer)); | |
5698 | assert(CvSTASH(referrer) == (const HV *)sv); | |
c68d9564 | 5699 | SvANY(MUTABLE_CV(referrer))->xcv_stash = 0; |
803f2748 DM |
5700 | } |
5701 | else { | |
5702 | assert(SvTYPE(sv) == SVt_PVGV); | |
5703 | /* You lookin' at me? */ | |
5704 | assert(CvGV(referrer)); | |
5705 | assert(CvGV(referrer) == (const GV *)sv); | |
5706 | anonymise_cv_maybe(MUTABLE_GV(sv), | |
5707 | MUTABLE_CV(referrer)); | |
5708 | } | |
5709 | ||
86f55936 NC |
5710 | } else { |
5711 | Perl_croak(aTHX_ | |
5712 | "panic: magic_killbackrefs (flags=%"UVxf")", | |
5713 | (UV)SvFLAGS(referrer)); | |
5714 | } | |
5715 | ||
5648c0ae DM |
5716 | if (is_array) |
5717 | *svp = NULL; | |
86f55936 NC |
5718 | } |
5719 | svp++; | |
5720 | } | |
5648c0ae DM |
5721 | } |
5722 | if (is_array) { | |
cef0c2ea | 5723 | AvFILLp(av) = -1; |
5648c0ae | 5724 | SvREFCNT_dec(av); /* remove extra count added by sv_add_backref() */ |
86f55936 | 5725 | } |
5648c0ae | 5726 | return; |
86f55936 NC |
5727 | } |
5728 | ||
954c1994 GS |
5729 | /* |
5730 | =for apidoc sv_insert | |
5731 | ||
5732 | Inserts a string at the specified offset/length within the SV. Similar to | |
c0dd94a0 | 5733 | the Perl substr() function. Handles get magic. |
954c1994 | 5734 | |
c0dd94a0 VP |
5735 | =for apidoc sv_insert_flags |
5736 | ||
5737 | Same as C<sv_insert>, but the extra C<flags> are passed the C<SvPV_force_flags> that applies to C<bigstr>. | |
5738 | ||
5739 | =cut | |
5740 | */ | |
5741 | ||
5742 | void | |
5743 | Perl_sv_insert_flags(pTHX_ SV *const bigstr, const STRLEN offset, const STRLEN len, const char *const little, const STRLEN littlelen, const U32 flags) | |
5744 | { | |
97aff369 | 5745 | dVAR; |
79072805 LW |
5746 | register char *big; |
5747 | register char *mid; | |
5748 | register char *midend; | |
5749 | register char *bigend; | |
5750 | register I32 i; | |
6ff81951 | 5751 | STRLEN curlen; |
1c846c1f | 5752 | |
27aecdc6 | 5753 | PERL_ARGS_ASSERT_SV_INSERT_FLAGS; |
79072805 | 5754 | |
8990e307 | 5755 | if (!bigstr) |
cea2e8a9 | 5756 | Perl_croak(aTHX_ "Can't modify non-existent substring"); |
c0dd94a0 | 5757 | SvPV_force_flags(bigstr, curlen, flags); |
60fa28ff | 5758 | (void)SvPOK_only_UTF8(bigstr); |
6ff81951 GS |
5759 | if (offset + len > curlen) { |
5760 | SvGROW(bigstr, offset+len+1); | |
93524f2b | 5761 | Zero(SvPVX(bigstr)+curlen, offset+len-curlen, char); |
6ff81951 GS |
5762 | SvCUR_set(bigstr, offset+len); |
5763 | } | |
79072805 | 5764 | |
69b47968 | 5765 | SvTAINT(bigstr); |
79072805 LW |
5766 | i = littlelen - len; |
5767 | if (i > 0) { /* string might grow */ | |
a0d0e21e | 5768 | big = SvGROW(bigstr, SvCUR(bigstr) + i + 1); |
79072805 LW |
5769 | mid = big + offset + len; |
5770 | midend = bigend = big + SvCUR(bigstr); | |
5771 | bigend += i; | |
5772 | *bigend = '\0'; | |
5773 | while (midend > mid) /* shove everything down */ | |
5774 | *--bigend = *--midend; | |
5775 | Move(little,big+offset,littlelen,char); | |
b162af07 | 5776 | SvCUR_set(bigstr, SvCUR(bigstr) + i); |
79072805 LW |
5777 | SvSETMAGIC(bigstr); |
5778 | return; | |
5779 | } | |
5780 | else if (i == 0) { | |
463ee0b2 | 5781 | Move(little,SvPVX(bigstr)+offset,len,char); |
79072805 LW |
5782 | SvSETMAGIC(bigstr); |
5783 | return; | |
5784 | } | |
5785 | ||
463ee0b2 | 5786 | big = SvPVX(bigstr); |
79072805 LW |
5787 | mid = big + offset; |
5788 | midend = mid + len; | |
5789 | bigend = big + SvCUR(bigstr); | |
5790 | ||
5791 | if (midend > bigend) | |
cea2e8a9 | 5792 | Perl_croak(aTHX_ "panic: sv_insert"); |
79072805 LW |
5793 | |
5794 | if (mid - big > bigend - midend) { /* faster to shorten from end */ | |
5795 | if (littlelen) { | |
5796 | Move(little, mid, littlelen,char); | |
5797 | mid += littlelen; | |
5798 | } | |
5799 | i = bigend - midend; | |
5800 | if (i > 0) { | |
5801 | Move(midend, mid, i,char); | |
5802 | mid += i; | |
5803 | } | |
5804 | *mid = '\0'; | |
5805 | SvCUR_set(bigstr, mid - big); | |
5806 | } | |
155aba94 | 5807 | else if ((i = mid - big)) { /* faster from front */ |
79072805 LW |
5808 | midend -= littlelen; |
5809 | mid = midend; | |
0d3c21b0 | 5810 | Move(big, midend - i, i, char); |
79072805 | 5811 | sv_chop(bigstr,midend-i); |
79072805 LW |
5812 | if (littlelen) |
5813 | Move(little, mid, littlelen,char); | |
5814 | } | |
5815 | else if (littlelen) { | |
5816 | midend -= littlelen; | |
5817 | sv_chop(bigstr,midend); | |
5818 | Move(little,midend,littlelen,char); | |
5819 | } | |
5820 | else { | |
5821 | sv_chop(bigstr,midend); | |
5822 | } | |
5823 | SvSETMAGIC(bigstr); | |
5824 | } | |
5825 | ||
c461cf8f JH |
5826 | /* |
5827 | =for apidoc sv_replace | |
5828 | ||
5829 | Make the first argument a copy of the second, then delete the original. | |
645c22ef DM |
5830 | The target SV physically takes over ownership of the body of the source SV |
5831 | and inherits its flags; however, the target keeps any magic it owns, | |
5832 | and any magic in the source is discarded. | |
ff276b08 | 5833 | Note that this is a rather specialist SV copying operation; most of the |
645c22ef | 5834 | time you'll want to use C<sv_setsv> or one of its many macro front-ends. |
c461cf8f JH |
5835 | |
5836 | =cut | |
5837 | */ | |
79072805 LW |
5838 | |
5839 | void | |
af828c01 | 5840 | Perl_sv_replace(pTHX_ register SV *const sv, register SV *const nsv) |
79072805 | 5841 | { |
97aff369 | 5842 | dVAR; |
a3b680e6 | 5843 | const U32 refcnt = SvREFCNT(sv); |
7918f24d NC |
5844 | |
5845 | PERL_ARGS_ASSERT_SV_REPLACE; | |
5846 | ||
765f542d | 5847 | SV_CHECK_THINKFIRST_COW_DROP(sv); |
30e5c352 | 5848 | if (SvREFCNT(nsv) != 1) { |
fe13d51d JM |
5849 | Perl_croak(aTHX_ "panic: reference miscount on nsv in sv_replace()" |
5850 | " (%" UVuf " != 1)", (UV) SvREFCNT(nsv)); | |
30e5c352 | 5851 | } |
93a17b20 | 5852 | if (SvMAGICAL(sv)) { |
a0d0e21e LW |
5853 | if (SvMAGICAL(nsv)) |
5854 | mg_free(nsv); | |
5855 | else | |
5856 | sv_upgrade(nsv, SVt_PVMG); | |
b162af07 | 5857 | SvMAGIC_set(nsv, SvMAGIC(sv)); |
a0d0e21e | 5858 | SvFLAGS(nsv) |= SvMAGICAL(sv); |
93a17b20 | 5859 | SvMAGICAL_off(sv); |
b162af07 | 5860 | SvMAGIC_set(sv, NULL); |
93a17b20 | 5861 | } |
79072805 LW |
5862 | SvREFCNT(sv) = 0; |
5863 | sv_clear(sv); | |
477f5d66 | 5864 | assert(!SvREFCNT(sv)); |
fd0854ff DM |
5865 | #ifdef DEBUG_LEAKING_SCALARS |
5866 | sv->sv_flags = nsv->sv_flags; | |
5867 | sv->sv_any = nsv->sv_any; | |
5868 | sv->sv_refcnt = nsv->sv_refcnt; | |
f34d0642 | 5869 | sv->sv_u = nsv->sv_u; |
fd0854ff | 5870 | #else |
79072805 | 5871 | StructCopy(nsv,sv,SV); |
fd0854ff | 5872 | #endif |
4df7f6af | 5873 | if(SvTYPE(sv) == SVt_IV) { |
7b2c381c | 5874 | SvANY(sv) |
339049b0 | 5875 | = (XPVIV*)((char*)&(sv->sv_u.svu_iv) - STRUCT_OFFSET(XPVIV, xiv_iv)); |
7b2c381c NC |
5876 | } |
5877 | ||
fd0854ff | 5878 | |
f8c7b90f | 5879 | #ifdef PERL_OLD_COPY_ON_WRITE |
d3d0e6f1 NC |
5880 | if (SvIsCOW_normal(nsv)) { |
5881 | /* We need to follow the pointers around the loop to make the | |
5882 | previous SV point to sv, rather than nsv. */ | |
5883 | SV *next; | |
5884 | SV *current = nsv; | |
5885 | while ((next = SV_COW_NEXT_SV(current)) != nsv) { | |
5886 | assert(next); | |
5887 | current = next; | |
3f7c398e | 5888 | assert(SvPVX_const(current) == SvPVX_const(nsv)); |
d3d0e6f1 NC |
5889 | } |
5890 | /* Make the SV before us point to the SV after us. */ | |
5891 | if (DEBUG_C_TEST) { | |
5892 | PerlIO_printf(Perl_debug_log, "previous is\n"); | |
5893 | sv_dump(current); | |
a29f6d03 NC |
5894 | PerlIO_printf(Perl_debug_log, |
5895 | "move it from 0x%"UVxf" to 0x%"UVxf"\n", | |
d3d0e6f1 NC |
5896 | (UV) SV_COW_NEXT_SV(current), (UV) sv); |
5897 | } | |
a29f6d03 | 5898 | SV_COW_NEXT_SV_SET(current, sv); |
d3d0e6f1 NC |
5899 | } |
5900 | #endif | |
79072805 | 5901 | SvREFCNT(sv) = refcnt; |
1edc1566 | 5902 | SvFLAGS(nsv) |= SVTYPEMASK; /* Mark as freed */ |
39cf41c2 | 5903 | SvREFCNT(nsv) = 0; |
463ee0b2 | 5904 | del_SV(nsv); |
79072805 LW |
5905 | } |
5906 | ||
803f2748 DM |
5907 | /* We're about to free a GV which has a CV that refers back to us. |
5908 | * If that CV will outlive us, make it anonymous (i.e. fix up its CvGV | |
5909 | * field) */ | |
5910 | ||
5911 | STATIC void | |
5912 | S_anonymise_cv_maybe(pTHX_ GV *gv, CV* cv) | |
5913 | { | |
5914 | char *stash; | |
5915 | SV *gvname; | |
5916 | GV *anongv; | |
5917 | ||
5918 | PERL_ARGS_ASSERT_ANONYMISE_CV_MAYBE; | |
5919 | ||
5920 | /* be assertive! */ | |
5921 | assert(SvREFCNT(gv) == 0); | |
5922 | assert(isGV(gv) && isGV_with_GP(gv)); | |
5923 | assert(GvGP(gv)); | |
5924 | assert(!CvANON(cv)); | |
5925 | assert(CvGV(cv) == gv); | |
5926 | ||
5927 | /* will the CV shortly be freed by gp_free() ? */ | |
5928 | if (GvCV(gv) == cv && GvGP(gv)->gp_refcnt < 2 && SvREFCNT(cv) < 2) { | |
b3f91e91 | 5929 | SvANY(cv)->xcv_gv = NULL; |
803f2748 DM |
5930 | return; |
5931 | } | |
5932 | ||
5933 | /* if not, anonymise: */ | |
5934 | stash = GvSTASH(gv) ? HvNAME(GvSTASH(gv)) : NULL; | |
5935 | gvname = Perl_newSVpvf(aTHX_ "%s::__ANON__", | |
5936 | stash ? stash : "__ANON__"); | |
5937 | anongv = gv_fetchsv(gvname, GV_ADDMULTI, SVt_PVCV); | |
5938 | SvREFCNT_dec(gvname); | |
5939 | ||
5940 | CvANON_on(cv); | |
cfc1e951 | 5941 | CvCVGV_RC_on(cv); |
b3f91e91 | 5942 | SvANY(cv)->xcv_gv = MUTABLE_GV(SvREFCNT_inc(anongv)); |
803f2748 DM |
5943 | } |
5944 | ||
5945 | ||
c461cf8f JH |
5946 | /* |
5947 | =for apidoc sv_clear | |
5948 | ||
645c22ef DM |
5949 | Clear an SV: call any destructors, free up any memory used by the body, |
5950 | and free the body itself. The SV's head is I<not> freed, although | |
5951 | its type is set to all 1's so that it won't inadvertently be assumed | |
5952 | to be live during global destruction etc. | |
5953 | This function should only be called when REFCNT is zero. Most of the time | |
5954 | you'll want to call C<sv_free()> (or its macro wrapper C<SvREFCNT_dec>) | |
5955 | instead. | |
c461cf8f JH |
5956 | |
5957 | =cut | |
5958 | */ | |
5959 | ||
79072805 | 5960 | void |
5239d5c4 | 5961 | Perl_sv_clear(pTHX_ SV *const orig_sv) |
79072805 | 5962 | { |
27da23d5 | 5963 | dVAR; |
dd69841b | 5964 | HV *stash; |
5239d5c4 DM |
5965 | U32 type; |
5966 | const struct body_details *sv_type_details; | |
5967 | SV* iter_sv = NULL; | |
5968 | SV* next_sv = NULL; | |
5969 | register SV *sv = orig_sv; | |
82bb6deb | 5970 | |
7918f24d | 5971 | PERL_ARGS_ASSERT_SV_CLEAR; |
5239d5c4 DM |
5972 | |
5973 | /* within this loop, sv is the SV currently being freed, and | |
5974 | * iter_sv is the most recent AV or whatever that's being iterated | |
5975 | * over to provide more SVs */ | |
5976 | ||
5977 | while (sv) { | |
5978 | ||
df90f6af DM |
5979 | type = SvTYPE(sv); |
5980 | ||
5981 | assert(SvREFCNT(sv) == 0); | |
5982 | assert(SvTYPE(sv) != SVTYPEMASK); | |
5983 | ||
5984 | if (type <= SVt_IV) { | |
5985 | /* See the comment in sv.h about the collusion between this | |
5986 | * early return and the overloading of the NULL slots in the | |
5987 | * size table. */ | |
5988 | if (SvROK(sv)) | |
5989 | goto free_rv; | |
5990 | SvFLAGS(sv) &= SVf_BREAK; | |
5991 | SvFLAGS(sv) |= SVTYPEMASK; | |
5992 | goto free_head; | |
5993 | } | |
82bb6deb | 5994 | |
df90f6af | 5995 | if (SvOBJECT(sv)) { |
4155e4fe | 5996 | if (!curse(sv, 1)) goto get_next_sv; |
93578b34 | 5997 | } |
df90f6af DM |
5998 | if (type >= SVt_PVMG) { |
5999 | if (type == SVt_PVMG && SvPAD_OUR(sv)) { | |
6000 | SvREFCNT_dec(SvOURSTASH(sv)); | |
6001 | } else if (SvMAGIC(sv)) | |
6002 | mg_free(sv); | |
6003 | if (type == SVt_PVMG && SvPAD_TYPED(sv)) | |
6004 | SvREFCNT_dec(SvSTASH(sv)); | |
e7fab884 | 6005 | } |
df90f6af DM |
6006 | switch (type) { |
6007 | /* case SVt_BIND: */ | |
6008 | case SVt_PVIO: | |
6009 | if (IoIFP(sv) && | |
6010 | IoIFP(sv) != PerlIO_stdin() && | |
6011 | IoIFP(sv) != PerlIO_stdout() && | |
6012 | IoIFP(sv) != PerlIO_stderr() && | |
6013 | !(IoFLAGS(sv) & IOf_FAKE_DIRP)) | |
6014 | { | |
6015 | io_close(MUTABLE_IO(sv), FALSE); | |
5239d5c4 | 6016 | } |
df90f6af DM |
6017 | if (IoDIRP(sv) && !(IoFLAGS(sv) & IOf_FAKE_DIRP)) |
6018 | PerlDir_close(IoDIRP(sv)); | |
6019 | IoDIRP(sv) = (DIR*)NULL; | |
6020 | Safefree(IoTOP_NAME(sv)); | |
6021 | Safefree(IoFMT_NAME(sv)); | |
6022 | Safefree(IoBOTTOM_NAME(sv)); | |
6023 | goto freescalar; | |
6024 | case SVt_REGEXP: | |
6025 | /* FIXME for plugins */ | |
6026 | pregfree2((REGEXP*) sv); | |
6027 | goto freescalar; | |
6028 | case SVt_PVCV: | |
6029 | case SVt_PVFM: | |
6030 | cv_undef(MUTABLE_CV(sv)); | |
6031 | /* If we're in a stash, we don't own a reference to it. | |
6032 | * However it does have a back reference to us, which needs to | |
6033 | * be cleared. */ | |
6034 | if ((stash = CvSTASH(sv))) | |
6035 | sv_del_backref(MUTABLE_SV(stash), sv); | |
6036 | goto freescalar; | |
6037 | case SVt_PVHV: | |
6038 | if (PL_last_swash_hv == (const HV *)sv) { | |
6039 | PL_last_swash_hv = NULL; | |
5239d5c4 | 6040 | } |
df90f6af | 6041 | Perl_hv_kill_backrefs(aTHX_ MUTABLE_HV(sv)); |
745edda6 | 6042 | Perl_hv_undef_flags(aTHX_ MUTABLE_HV(sv), HV_NAME_SETALL); |
df90f6af DM |
6043 | break; |
6044 | case SVt_PVAV: | |
db93c0c4 | 6045 | { |
df90f6af DM |
6046 | AV* av = MUTABLE_AV(sv); |
6047 | if (PL_comppad == av) { | |
6048 | PL_comppad = NULL; | |
6049 | PL_curpad = NULL; | |
6050 | } | |
6051 | if (AvREAL(av) && AvFILLp(av) > -1) { | |
6052 | next_sv = AvARRAY(av)[AvFILLp(av)--]; | |
6053 | /* save old iter_sv in top-most slot of AV, | |
6054 | * and pray that it doesn't get wiped in the meantime */ | |
6055 | AvARRAY(av)[AvMAX(av)] = iter_sv; | |
6056 | iter_sv = sv; | |
6057 | goto get_next_sv; /* process this new sv */ | |
6058 | } | |
6059 | Safefree(AvALLOC(av)); | |
db93c0c4 | 6060 | } |
df90f6af DM |
6061 | |
6062 | break; | |
6063 | case SVt_PVLV: | |
6064 | if (LvTYPE(sv) == 'T') { /* for tie: return HE to pool */ | |
6065 | SvREFCNT_dec(HeKEY_sv((HE*)LvTARG(sv))); | |
6066 | HeNEXT((HE*)LvTARG(sv)) = PL_hv_fetch_ent_mh; | |
6067 | PL_hv_fetch_ent_mh = (HE*)LvTARG(sv); | |
6068 | } | |
6069 | else if (LvTYPE(sv) != 't') /* unless tie: unrefcnted fake SV** */ | |
6070 | SvREFCNT_dec(LvTARG(sv)); | |
6071 | case SVt_PVGV: | |
6072 | if (isGV_with_GP(sv)) { | |
6073 | if(GvCVu((const GV *)sv) && (stash = GvSTASH(MUTABLE_GV(sv))) | |
00169e2c | 6074 | && HvENAME_get(stash)) |
df90f6af DM |
6075 | mro_method_changed_in(stash); |
6076 | gp_free(MUTABLE_GV(sv)); | |
6077 | if (GvNAME_HEK(sv)) | |
6078 | unshare_hek(GvNAME_HEK(sv)); | |
6079 | /* If we're in a stash, we don't own a reference to it. | |
6080 | * However it does have a back reference to us, which | |
6081 | * needs to be cleared. */ | |
6082 | if (!SvVALID(sv) && (stash = GvSTASH(sv))) | |
6083 | sv_del_backref(MUTABLE_SV(stash), sv); | |
6084 | } | |
6085 | /* FIXME. There are probably more unreferenced pointers to SVs | |
6086 | * in the interpreter struct that we should check and tidy in | |
6087 | * a similar fashion to this: */ | |
6088 | if ((const GV *)sv == PL_last_in_gv) | |
6089 | PL_last_in_gv = NULL; | |
6090 | case SVt_PVMG: | |
6091 | case SVt_PVNV: | |
6092 | case SVt_PVIV: | |
6093 | case SVt_PV: | |
6094 | freescalar: | |
6095 | /* Don't bother with SvOOK_off(sv); as we're only going to | |
6096 | * free it. */ | |
6097 | if (SvOOK(sv)) { | |
6098 | STRLEN offset; | |
6099 | SvOOK_offset(sv, offset); | |
6100 | SvPV_set(sv, SvPVX_mutable(sv) - offset); | |
6101 | /* Don't even bother with turning off the OOK flag. */ | |
6102 | } | |
6103 | if (SvROK(sv)) { | |
6104 | free_rv: | |
6105 | { | |
6106 | SV * const target = SvRV(sv); | |
6107 | if (SvWEAKREF(sv)) | |
6108 | sv_del_backref(target, sv); | |
6109 | else | |
b98b62bc | 6110 | next_sv = target; |
5302ffd4 | 6111 | } |
df90f6af DM |
6112 | } |
6113 | #ifdef PERL_OLD_COPY_ON_WRITE | |
6114 | else if (SvPVX_const(sv) | |
6115 | && !(SvTYPE(sv) == SVt_PVIO | |
6116 | && !(IoFLAGS(sv) & IOf_FAKE_DIRP))) | |
6117 | { | |
6118 | if (SvIsCOW(sv)) { | |
6119 | if (DEBUG_C_TEST) { | |
6120 | PerlIO_printf(Perl_debug_log, "Copy on write: clear\n"); | |
6121 | sv_dump(sv); | |
6122 | } | |
6123 | if (SvLEN(sv)) { | |
6124 | sv_release_COW(sv, SvPVX_const(sv), SV_COW_NEXT_SV(sv)); | |
6125 | } else { | |
6126 | unshare_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sv))); | |
6127 | } | |
5302ffd4 | 6128 | |
df90f6af DM |
6129 | SvFAKE_off(sv); |
6130 | } else if (SvLEN(sv)) { | |
6131 | Safefree(SvPVX_const(sv)); | |
6132 | } | |
6133 | } | |
765f542d | 6134 | #else |
df90f6af DM |
6135 | else if (SvPVX_const(sv) && SvLEN(sv) |
6136 | && !(SvTYPE(sv) == SVt_PVIO | |
6137 | && !(IoFLAGS(sv) & IOf_FAKE_DIRP))) | |
6138 | Safefree(SvPVX_mutable(sv)); | |
6139 | else if (SvPVX_const(sv) && SvREADONLY(sv) && SvFAKE(sv)) { | |
6140 | unshare_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sv))); | |
6141 | SvFAKE_off(sv); | |
6142 | } | |
765f542d | 6143 | #endif |
df90f6af DM |
6144 | break; |
6145 | case SVt_NV: | |
6146 | break; | |
6147 | } | |
79072805 | 6148 | |
df90f6af | 6149 | free_body: |
5239d5c4 | 6150 | |
df90f6af DM |
6151 | SvFLAGS(sv) &= SVf_BREAK; |
6152 | SvFLAGS(sv) |= SVTYPEMASK; | |
893645bd | 6153 | |
df90f6af DM |
6154 | sv_type_details = bodies_by_type + type; |
6155 | if (sv_type_details->arena) { | |
6156 | del_body(((char *)SvANY(sv) + sv_type_details->offset), | |
6157 | &PL_body_roots[type]); | |
6158 | } | |
6159 | else if (sv_type_details->body_size) { | |
6160 | safefree(SvANY(sv)); | |
6161 | } | |
5239d5c4 DM |
6162 | |
6163 | free_head: | |
6164 | /* caller is responsible for freeing the head of the original sv */ | |
6165 | if (sv != orig_sv && !SvREFCNT(sv)) | |
6166 | del_SV(sv); | |
6167 | ||
6168 | /* grab and free next sv, if any */ | |
6169 | get_next_sv: | |
6170 | while (1) { | |
6171 | sv = NULL; | |
6172 | if (next_sv) { | |
6173 | sv = next_sv; | |
6174 | next_sv = NULL; | |
6175 | } | |
6176 | else if (!iter_sv) { | |
6177 | break; | |
6178 | } else if (SvTYPE(iter_sv) == SVt_PVAV) { | |
6179 | AV *const av = (AV*)iter_sv; | |
6180 | if (AvFILLp(av) > -1) { | |
6181 | sv = AvARRAY(av)[AvFILLp(av)--]; | |
6182 | } | |
6183 | else { /* no more elements of current AV to free */ | |
6184 | sv = iter_sv; | |
6185 | type = SvTYPE(sv); | |
6186 | /* restore previous value, squirrelled away */ | |
6187 | iter_sv = AvARRAY(av)[AvMAX(av)]; | |
6188 | Safefree(AvALLOC(av)); | |
6189 | goto free_body; | |
6190 | } | |
6191 | } | |
6192 | ||
6193 | /* unrolled SvREFCNT_dec and sv_free2 follows: */ | |
6194 | ||
6195 | if (!sv) | |
6196 | continue; | |
6197 | if (!SvREFCNT(sv)) { | |
6198 | sv_free(sv); | |
6199 | continue; | |
6200 | } | |
6201 | if (--(SvREFCNT(sv))) | |
6202 | continue; | |
df90f6af | 6203 | #ifdef DEBUGGING |
5239d5c4 DM |
6204 | if (SvTEMP(sv)) { |
6205 | Perl_ck_warner_d(aTHX_ packWARN(WARN_DEBUGGING), | |
6206 | "Attempt to free temp prematurely: SV 0x%"UVxf | |
6207 | pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE); | |
6208 | continue; | |
6209 | } | |
df90f6af | 6210 | #endif |
5239d5c4 DM |
6211 | if (SvREADONLY(sv) && SvIMMORTAL(sv)) { |
6212 | /* make sure SvREFCNT(sv)==0 happens very seldom */ | |
6213 | SvREFCNT(sv) = (~(U32)0)/2; | |
6214 | continue; | |
6215 | } | |
6216 | break; | |
6217 | } /* while 1 */ | |
6218 | ||
6219 | } /* while sv */ | |
79072805 LW |
6220 | } |
6221 | ||
4155e4fe FC |
6222 | /* This routine curses the sv itself, not the object referenced by sv. So |
6223 | sv does not have to be ROK. */ | |
6224 | ||
6225 | static bool | |
6226 | S_curse(pTHX_ SV * const sv, const bool check_refcnt) { | |
6227 | dVAR; | |
6228 | ||
6229 | PERL_ARGS_ASSERT_CURSE; | |
6230 | assert(SvOBJECT(sv)); | |
6231 | ||
6232 | if (PL_defstash && /* Still have a symbol table? */ | |
6233 | SvDESTROYABLE(sv)) | |
6234 | { | |
6235 | dSP; | |
6236 | HV* stash; | |
6237 | do { | |
6238 | CV* destructor; | |
6239 | stash = SvSTASH(sv); | |
6240 | destructor = StashHANDLER(stash,DESTROY); | |
6241 | if (destructor | |
6242 | /* A constant subroutine can have no side effects, so | |
6243 | don't bother calling it. */ | |
6244 | && !CvCONST(destructor) | |
6245 | /* Don't bother calling an empty destructor */ | |
6246 | && (CvISXSUB(destructor) | |
6247 | || (CvSTART(destructor) | |
6248 | && (CvSTART(destructor)->op_next->op_type | |
6249 | != OP_LEAVESUB)))) | |
6250 | { | |
6251 | SV* const tmpref = newRV(sv); | |
6252 | SvREADONLY_on(tmpref); /* DESTROY() could be naughty */ | |
6253 | ENTER; | |
6254 | PUSHSTACKi(PERLSI_DESTROY); | |
6255 | EXTEND(SP, 2); | |
6256 | PUSHMARK(SP); | |
6257 | PUSHs(tmpref); | |
6258 | PUTBACK; | |
6259 | call_sv(MUTABLE_SV(destructor), | |
6260 | G_DISCARD|G_EVAL|G_KEEPERR|G_VOID); | |
6261 | POPSTACK; | |
6262 | SPAGAIN; | |
6263 | LEAVE; | |
6264 | if(SvREFCNT(tmpref) < 2) { | |
6265 | /* tmpref is not kept alive! */ | |
6266 | SvREFCNT(sv)--; | |
6267 | SvRV_set(tmpref, NULL); | |
6268 | SvROK_off(tmpref); | |
6269 | } | |
6270 | SvREFCNT_dec(tmpref); | |
6271 | } | |
6272 | } while (SvOBJECT(sv) && SvSTASH(sv) != stash); | |
6273 | ||
6274 | ||
6275 | if (check_refcnt && SvREFCNT(sv)) { | |
6276 | if (PL_in_clean_objs) | |
6277 | Perl_croak(aTHX_ | |
6278 | "DESTROY created new reference to dead object '%s'", | |
6279 | HvNAME_get(stash)); | |
6280 | /* DESTROY gave object new lease on life */ | |
6281 | return FALSE; | |
6282 | } | |
6283 | } | |
6284 | ||
6285 | if (SvOBJECT(sv)) { | |
6286 | SvREFCNT_dec(SvSTASH(sv)); /* possibly of changed persuasion */ | |
6287 | SvOBJECT_off(sv); /* Curse the object. */ | |
6288 | if (SvTYPE(sv) != SVt_PVIO) | |
6289 | --PL_sv_objcount;/* XXX Might want something more general */ | |
6290 | } | |
6291 | return TRUE; | |
6292 | } | |
6293 | ||
645c22ef DM |
6294 | /* |
6295 | =for apidoc sv_newref | |
6296 | ||
6297 | Increment an SV's reference count. Use the C<SvREFCNT_inc()> wrapper | |
6298 | instead. | |
6299 | ||
6300 | =cut | |
6301 | */ | |
6302 | ||
79072805 | 6303 | SV * |
af828c01 | 6304 | Perl_sv_newref(pTHX_ SV *const sv) |
79072805 | 6305 | { |
96a5add6 | 6306 | PERL_UNUSED_CONTEXT; |
463ee0b2 | 6307 | if (sv) |
4db098f4 | 6308 | (SvREFCNT(sv))++; |
79072805 LW |
6309 | return sv; |
6310 | } | |
6311 | ||
c461cf8f JH |
6312 | /* |
6313 | =for apidoc sv_free | |
6314 | ||
645c22ef DM |
6315 | Decrement an SV's reference count, and if it drops to zero, call |
6316 | C<sv_clear> to invoke destructors and free up any memory used by | |
6317 | the body; finally, deallocate the SV's head itself. | |
6318 | Normally called via a wrapper macro C<SvREFCNT_dec>. | |
c461cf8f JH |
6319 | |
6320 | =cut | |
6321 | */ | |
6322 | ||
79072805 | 6323 | void |
af828c01 | 6324 | Perl_sv_free(pTHX_ SV *const sv) |
79072805 | 6325 | { |
27da23d5 | 6326 | dVAR; |
79072805 LW |
6327 | if (!sv) |
6328 | return; | |
a0d0e21e LW |
6329 | if (SvREFCNT(sv) == 0) { |
6330 | if (SvFLAGS(sv) & SVf_BREAK) | |
645c22ef DM |
6331 | /* this SV's refcnt has been artificially decremented to |
6332 | * trigger cleanup */ | |
a0d0e21e | 6333 | return; |
3280af22 | 6334 | if (PL_in_clean_all) /* All is fair */ |
1edc1566 | 6335 | return; |
d689ffdd JP |
6336 | if (SvREADONLY(sv) && SvIMMORTAL(sv)) { |
6337 | /* make sure SvREFCNT(sv)==0 happens very seldom */ | |
6338 | SvREFCNT(sv) = (~(U32)0)/2; | |
6339 | return; | |
6340 | } | |
41e4abd8 | 6341 | if (ckWARN_d(WARN_INTERNAL)) { |
41e4abd8 NC |
6342 | #ifdef DEBUG_LEAKING_SCALARS_FORK_DUMP |
6343 | Perl_dump_sv_child(aTHX_ sv); | |
e4c5322d DM |
6344 | #else |
6345 | #ifdef DEBUG_LEAKING_SCALARS | |
bfd95973 | 6346 | sv_dump(sv); |
e4c5322d | 6347 | #endif |
bfd95973 NC |
6348 | #ifdef DEBUG_LEAKING_SCALARS_ABORT |
6349 | if (PL_warnhook == PERL_WARNHOOK_FATAL | |
6350 | || ckDEAD(packWARN(WARN_INTERNAL))) { | |
6351 | /* Don't let Perl_warner cause us to escape our fate: */ | |
6352 | abort(); | |
6353 | } | |
6354 | #endif | |
6355 | /* This may not return: */ | |
6356 | Perl_warner(aTHX_ packWARN(WARN_INTERNAL), | |
6357 | "Attempt to free unreferenced scalar: SV 0x%"UVxf | |
6358 | pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE); | |
41e4abd8 NC |
6359 | #endif |
6360 | } | |
77abb4c6 NC |
6361 | #ifdef DEBUG_LEAKING_SCALARS_ABORT |
6362 | abort(); | |
6363 | #endif | |
79072805 LW |
6364 | return; |
6365 | } | |
4db098f4 | 6366 | if (--(SvREFCNT(sv)) > 0) |
8990e307 | 6367 | return; |
8c4d3c90 NC |
6368 | Perl_sv_free2(aTHX_ sv); |
6369 | } | |
6370 | ||
6371 | void | |
af828c01 | 6372 | Perl_sv_free2(pTHX_ SV *const sv) |
8c4d3c90 | 6373 | { |
27da23d5 | 6374 | dVAR; |
7918f24d NC |
6375 | |
6376 | PERL_ARGS_ASSERT_SV_FREE2; | |
6377 | ||
463ee0b2 LW |
6378 | #ifdef DEBUGGING |
6379 | if (SvTEMP(sv)) { | |
9b387841 NC |
6380 | Perl_ck_warner_d(aTHX_ packWARN(WARN_DEBUGGING), |
6381 | "Attempt to free temp prematurely: SV 0x%"UVxf | |
6382 | pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE); | |
79072805 | 6383 | return; |
79072805 | 6384 | } |
463ee0b2 | 6385 | #endif |
d689ffdd JP |
6386 | if (SvREADONLY(sv) && SvIMMORTAL(sv)) { |
6387 | /* make sure SvREFCNT(sv)==0 happens very seldom */ | |
6388 | SvREFCNT(sv) = (~(U32)0)/2; | |
6389 | return; | |
6390 | } | |
79072805 | 6391 | sv_clear(sv); |
477f5d66 CS |
6392 | if (! SvREFCNT(sv)) |
6393 | del_SV(sv); | |
79072805 LW |
6394 | } |
6395 | ||
954c1994 GS |
6396 | /* |
6397 | =for apidoc sv_len | |
6398 | ||
645c22ef DM |
6399 | Returns the length of the string in the SV. Handles magic and type |
6400 | coercion. See also C<SvCUR>, which gives raw access to the xpv_cur slot. | |
954c1994 GS |
6401 | |
6402 | =cut | |
6403 | */ | |
6404 | ||
79072805 | 6405 | STRLEN |
af828c01 | 6406 | Perl_sv_len(pTHX_ register SV *const sv) |
79072805 | 6407 | { |
463ee0b2 | 6408 | STRLEN len; |
79072805 LW |
6409 | |
6410 | if (!sv) | |
6411 | return 0; | |
6412 | ||
8990e307 | 6413 | if (SvGMAGICAL(sv)) |
565764a8 | 6414 | len = mg_length(sv); |
8990e307 | 6415 | else |
4d84ee25 | 6416 | (void)SvPV_const(sv, len); |
463ee0b2 | 6417 | return len; |
79072805 LW |
6418 | } |
6419 | ||
c461cf8f JH |
6420 | /* |
6421 | =for apidoc sv_len_utf8 | |
6422 | ||
6423 | Returns the number of characters in the string in an SV, counting wide | |
1e54db1a | 6424 | UTF-8 bytes as a single character. Handles magic and type coercion. |
c461cf8f JH |
6425 | |
6426 | =cut | |
6427 | */ | |
6428 | ||
7e8c5dac | 6429 | /* |
c05a5c57 | 6430 | * The length is cached in PERL_MAGIC_utf8, in the mg_len field. Also the |
9564a3bd NC |
6431 | * mg_ptr is used, by sv_pos_u2b() and sv_pos_b2u() - see the comments below. |
6432 | * (Note that the mg_len is not the length of the mg_ptr field. | |
6433 | * This allows the cache to store the character length of the string without | |
6434 | * needing to malloc() extra storage to attach to the mg_ptr.) | |
7a5fa8a2 | 6435 | * |
7e8c5dac HS |
6436 | */ |
6437 | ||
a0ed51b3 | 6438 | STRLEN |
af828c01 | 6439 | Perl_sv_len_utf8(pTHX_ register SV *const sv) |
a0ed51b3 | 6440 | { |
a0ed51b3 LW |
6441 | if (!sv) |
6442 | return 0; | |
6443 | ||
a0ed51b3 | 6444 | if (SvGMAGICAL(sv)) |
b76347f2 | 6445 | return mg_length(sv); |
a0ed51b3 | 6446 | else |
b76347f2 | 6447 | { |
26346457 | 6448 | STRLEN len; |
e62f0680 | 6449 | const U8 *s = (U8*)SvPV_const(sv, len); |
7e8c5dac | 6450 | |
26346457 NC |
6451 | if (PL_utf8cache) { |
6452 | STRLEN ulen; | |
fe5bfecd | 6453 | MAGIC *mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_utf8) : NULL; |
26346457 | 6454 | |
6ef2ab89 NC |
6455 | if (mg && (mg->mg_len != -1 || mg->mg_ptr)) { |
6456 | if (mg->mg_len != -1) | |
6457 | ulen = mg->mg_len; | |
6458 | else { | |
6459 | /* We can use the offset cache for a headstart. | |
6460 | The longer value is stored in the first pair. */ | |
6461 | STRLEN *cache = (STRLEN *) mg->mg_ptr; | |
6462 | ||
6463 | ulen = cache[0] + Perl_utf8_length(aTHX_ s + cache[1], | |
6464 | s + len); | |
6465 | } | |
6466 | ||
26346457 NC |
6467 | if (PL_utf8cache < 0) { |
6468 | const STRLEN real = Perl_utf8_length(aTHX_ s, s + len); | |
9df83ffd | 6469 | assert_uft8_cache_coherent("sv_len_utf8", ulen, real, sv); |
26346457 NC |
6470 | } |
6471 | } | |
6472 | else { | |
6473 | ulen = Perl_utf8_length(aTHX_ s, s + len); | |
ec49a12c | 6474 | utf8_mg_len_cache_update(sv, &mg, ulen); |
cb9e20bb | 6475 | } |
26346457 | 6476 | return ulen; |
7e8c5dac | 6477 | } |
26346457 | 6478 | return Perl_utf8_length(aTHX_ s, s + len); |
7e8c5dac HS |
6479 | } |
6480 | } | |
6481 | ||
9564a3bd NC |
6482 | /* Walk forwards to find the byte corresponding to the passed in UTF-8 |
6483 | offset. */ | |
bdf30dd6 | 6484 | static STRLEN |
721e86b6 | 6485 | S_sv_pos_u2b_forwards(const U8 *const start, const U8 *const send, |
79d2d448 | 6486 | STRLEN *const uoffset_p, bool *const at_end) |
bdf30dd6 NC |
6487 | { |
6488 | const U8 *s = start; | |
3e2d3818 | 6489 | STRLEN uoffset = *uoffset_p; |
bdf30dd6 | 6490 | |
7918f24d NC |
6491 | PERL_ARGS_ASSERT_SV_POS_U2B_FORWARDS; |
6492 | ||
3e2d3818 NC |
6493 | while (s < send && uoffset) { |
6494 | --uoffset; | |
bdf30dd6 | 6495 | s += UTF8SKIP(s); |
3e2d3818 | 6496 | } |
79d2d448 NC |
6497 | if (s == send) { |
6498 | *at_end = TRUE; | |
6499 | } | |
6500 | else if (s > send) { | |
6501 | *at_end = TRUE; | |
bdf30dd6 NC |
6502 | /* This is the existing behaviour. Possibly it should be a croak, as |
6503 | it's actually a bounds error */ | |
6504 | s = send; | |
6505 | } | |
3e2d3818 | 6506 | *uoffset_p -= uoffset; |
bdf30dd6 NC |
6507 | return s - start; |
6508 | } | |
6509 | ||
9564a3bd NC |
6510 | /* Given the length of the string in both bytes and UTF-8 characters, decide |
6511 | whether to walk forwards or backwards to find the byte corresponding to | |
6512 | the passed in UTF-8 offset. */ | |
c336ad0b | 6513 | static STRLEN |
721e86b6 | 6514 | S_sv_pos_u2b_midway(const U8 *const start, const U8 *send, |
503752a1 | 6515 | STRLEN uoffset, const STRLEN uend) |
c336ad0b NC |
6516 | { |
6517 | STRLEN backw = uend - uoffset; | |
7918f24d NC |
6518 | |
6519 | PERL_ARGS_ASSERT_SV_POS_U2B_MIDWAY; | |
6520 | ||
c336ad0b | 6521 | if (uoffset < 2 * backw) { |
25a8a4ef | 6522 | /* The assumption is that going forwards is twice the speed of going |
c336ad0b NC |
6523 | forward (that's where the 2 * backw comes from). |
6524 | (The real figure of course depends on the UTF-8 data.) */ | |
503752a1 NC |
6525 | const U8 *s = start; |
6526 | ||
6527 | while (s < send && uoffset--) | |
6528 | s += UTF8SKIP(s); | |
6529 | assert (s <= send); | |
6530 | if (s > send) | |
6531 | s = send; | |
6532 | return s - start; | |
c336ad0b NC |
6533 | } |
6534 | ||
6535 | while (backw--) { | |
6536 | send--; | |
6537 | while (UTF8_IS_CONTINUATION(*send)) | |
6538 | send--; | |
6539 | } | |
6540 | return send - start; | |
6541 | } | |
6542 | ||
9564a3bd NC |
6543 | /* For the string representation of the given scalar, find the byte |
6544 | corresponding to the passed in UTF-8 offset. uoffset0 and boffset0 | |
6545 | give another position in the string, *before* the sought offset, which | |
6546 | (which is always true, as 0, 0 is a valid pair of positions), which should | |
6547 | help reduce the amount of linear searching. | |
6548 | If *mgp is non-NULL, it should point to the UTF-8 cache magic, which | |
6549 | will be used to reduce the amount of linear searching. The cache will be | |
6550 | created if necessary, and the found value offered to it for update. */ | |
28ccbf94 | 6551 | static STRLEN |
af828c01 | 6552 | S_sv_pos_u2b_cached(pTHX_ SV *const sv, MAGIC **const mgp, const U8 *const start, |
3e2d3818 | 6553 | const U8 *const send, STRLEN uoffset, |
7918f24d NC |
6554 | STRLEN uoffset0, STRLEN boffset0) |
6555 | { | |
7087a21c | 6556 | STRLEN boffset = 0; /* Actually always set, but let's keep gcc happy. */ |
c336ad0b | 6557 | bool found = FALSE; |
79d2d448 | 6558 | bool at_end = FALSE; |
c336ad0b | 6559 | |
7918f24d NC |
6560 | PERL_ARGS_ASSERT_SV_POS_U2B_CACHED; |
6561 | ||
75c33c12 NC |
6562 | assert (uoffset >= uoffset0); |
6563 | ||
48f9cf71 NC |
6564 | if (!uoffset) |
6565 | return 0; | |
6566 | ||
f89a570b CS |
6567 | if (!SvREADONLY(sv) |
6568 | && PL_utf8cache | |
6569 | && (*mgp || (SvTYPE(sv) >= SVt_PVMG && | |
6570 | (*mgp = mg_find(sv, PERL_MAGIC_utf8))))) { | |
d8b2e1f9 NC |
6571 | if ((*mgp)->mg_ptr) { |
6572 | STRLEN *cache = (STRLEN *) (*mgp)->mg_ptr; | |
6573 | if (cache[0] == uoffset) { | |
6574 | /* An exact match. */ | |
6575 | return cache[1]; | |
6576 | } | |
ab455f60 NC |
6577 | if (cache[2] == uoffset) { |
6578 | /* An exact match. */ | |
6579 | return cache[3]; | |
6580 | } | |
668af93f NC |
6581 | |
6582 | if (cache[0] < uoffset) { | |
d8b2e1f9 NC |
6583 | /* The cache already knows part of the way. */ |
6584 | if (cache[0] > uoffset0) { | |
6585 | /* The cache knows more than the passed in pair */ | |
6586 | uoffset0 = cache[0]; | |
6587 | boffset0 = cache[1]; | |
6588 | } | |
6589 | if ((*mgp)->mg_len != -1) { | |
6590 | /* And we know the end too. */ | |
6591 | boffset = boffset0 | |
721e86b6 | 6592 | + sv_pos_u2b_midway(start + boffset0, send, |
d8b2e1f9 NC |
6593 | uoffset - uoffset0, |
6594 | (*mgp)->mg_len - uoffset0); | |
6595 | } else { | |
3e2d3818 | 6596 | uoffset -= uoffset0; |
d8b2e1f9 | 6597 | boffset = boffset0 |
721e86b6 | 6598 | + sv_pos_u2b_forwards(start + boffset0, |
79d2d448 | 6599 | send, &uoffset, &at_end); |
3e2d3818 | 6600 | uoffset += uoffset0; |
d8b2e1f9 | 6601 | } |
dd7c5fd3 NC |
6602 | } |
6603 | else if (cache[2] < uoffset) { | |
6604 | /* We're between the two cache entries. */ | |
6605 | if (cache[2] > uoffset0) { | |
6606 | /* and the cache knows more than the passed in pair */ | |
6607 | uoffset0 = cache[2]; | |
6608 | boffset0 = cache[3]; | |
6609 | } | |
6610 | ||
668af93f | 6611 | boffset = boffset0 |
721e86b6 | 6612 | + sv_pos_u2b_midway(start + boffset0, |
668af93f NC |
6613 | start + cache[1], |
6614 | uoffset - uoffset0, | |
6615 | cache[0] - uoffset0); | |
dd7c5fd3 NC |
6616 | } else { |
6617 | boffset = boffset0 | |
721e86b6 | 6618 | + sv_pos_u2b_midway(start + boffset0, |
dd7c5fd3 NC |
6619 | start + cache[3], |
6620 | uoffset - uoffset0, | |
6621 | cache[2] - uoffset0); | |
d8b2e1f9 | 6622 | } |
668af93f | 6623 | found = TRUE; |
d8b2e1f9 NC |
6624 | } |
6625 | else if ((*mgp)->mg_len != -1) { | |
75c33c12 NC |
6626 | /* If we can take advantage of a passed in offset, do so. */ |
6627 | /* In fact, offset0 is either 0, or less than offset, so don't | |
6628 | need to worry about the other possibility. */ | |
6629 | boffset = boffset0 | |
721e86b6 | 6630 | + sv_pos_u2b_midway(start + boffset0, send, |
75c33c12 NC |
6631 | uoffset - uoffset0, |
6632 | (*mgp)->mg_len - uoffset0); | |
c336ad0b NC |
6633 | found = TRUE; |
6634 | } | |
28ccbf94 | 6635 | } |
c336ad0b NC |
6636 | |
6637 | if (!found || PL_utf8cache < 0) { | |
3e2d3818 NC |
6638 | STRLEN real_boffset; |
6639 | uoffset -= uoffset0; | |
6640 | real_boffset = boffset0 + sv_pos_u2b_forwards(start + boffset0, | |
79d2d448 | 6641 | send, &uoffset, &at_end); |
3e2d3818 | 6642 | uoffset += uoffset0; |
75c33c12 | 6643 | |
9df83ffd NC |
6644 | if (found && PL_utf8cache < 0) |
6645 | assert_uft8_cache_coherent("sv_pos_u2b_cache", boffset, | |
6646 | real_boffset, sv); | |
c336ad0b | 6647 | boffset = real_boffset; |
28ccbf94 | 6648 | } |
0905937d | 6649 | |
79d2d448 NC |
6650 | if (PL_utf8cache) { |
6651 | if (at_end) | |
6652 | utf8_mg_len_cache_update(sv, mgp, uoffset); | |
6653 | else | |
6654 | utf8_mg_pos_cache_update(sv, mgp, boffset, uoffset, send - start); | |
6655 | } | |
28ccbf94 NC |
6656 | return boffset; |
6657 | } | |
6658 | ||
9564a3bd NC |
6659 | |
6660 | /* | |
d931b1be | 6661 | =for apidoc sv_pos_u2b_flags |
9564a3bd NC |
6662 | |
6663 | Converts the value pointed to by offsetp from a count of UTF-8 chars from | |
6664 | the start of the string, to a count of the equivalent number of bytes; if | |
6665 | lenp is non-zero, it does the same to lenp, but this time starting from | |
d931b1be NC |
6666 | the offset, rather than from the start of the string. Handles type coercion. |
6667 | I<flags> is passed to C<SvPV_flags>, and usually should be | |
6668 | C<SV_GMAGIC|SV_CONST_RETURN> to handle magic. | |
9564a3bd NC |
6669 | |
6670 | =cut | |
6671 | */ | |
6672 | ||
6673 | /* | |
d931b1be | 6674 | * sv_pos_u2b_flags() uses, like sv_pos_b2u(), the mg_ptr of the potential |
c05a5c57 | 6675 | * PERL_MAGIC_utf8 of the sv to store the mapping between UTF-8 and |
9564a3bd NC |
6676 | * byte offsets. See also the comments of S_utf8_mg_pos_cache_update(). |
6677 | * | |
6678 | */ | |
6679 | ||
d931b1be NC |
6680 | STRLEN |
6681 | Perl_sv_pos_u2b_flags(pTHX_ SV *const sv, STRLEN uoffset, STRLEN *const lenp, | |
6682 | U32 flags) | |
a0ed51b3 | 6683 | { |
245d4a47 | 6684 | const U8 *start; |
a0ed51b3 | 6685 | STRLEN len; |
d931b1be | 6686 | STRLEN boffset; |
a0ed51b3 | 6687 | |
d931b1be | 6688 | PERL_ARGS_ASSERT_SV_POS_U2B_FLAGS; |
7918f24d | 6689 | |
d931b1be | 6690 | start = (U8*)SvPV_flags(sv, len, flags); |
7e8c5dac | 6691 | if (len) { |
bdf30dd6 | 6692 | const U8 * const send = start + len; |
0905937d | 6693 | MAGIC *mg = NULL; |
d931b1be | 6694 | boffset = sv_pos_u2b_cached(sv, &mg, start, send, uoffset, 0, 0); |
bdf30dd6 | 6695 | |
48f9cf71 NC |
6696 | if (lenp |
6697 | && *lenp /* don't bother doing work for 0, as its bytes equivalent | |
6698 | is 0, and *lenp is already set to that. */) { | |
28ccbf94 | 6699 | /* Convert the relative offset to absolute. */ |
777f7c56 | 6700 | const STRLEN uoffset2 = uoffset + *lenp; |
721e86b6 AL |
6701 | const STRLEN boffset2 |
6702 | = sv_pos_u2b_cached(sv, &mg, start, send, uoffset2, | |
28ccbf94 | 6703 | uoffset, boffset) - boffset; |
bdf30dd6 | 6704 | |
28ccbf94 | 6705 | *lenp = boffset2; |
bdf30dd6 | 6706 | } |
d931b1be NC |
6707 | } else { |
6708 | if (lenp) | |
6709 | *lenp = 0; | |
6710 | boffset = 0; | |
a0ed51b3 | 6711 | } |
e23c8137 | 6712 | |
d931b1be | 6713 | return boffset; |
a0ed51b3 LW |
6714 | } |
6715 | ||
777f7c56 EB |
6716 | /* |
6717 | =for apidoc sv_pos_u2b | |
6718 | ||
6719 | Converts the value pointed to by offsetp from a count of UTF-8 chars from | |
6720 | the start of the string, to a count of the equivalent number of bytes; if | |
6721 | lenp is non-zero, it does the same to lenp, but this time starting from | |
6722 | the offset, rather than from the start of the string. Handles magic and | |
6723 | type coercion. | |
6724 | ||
d931b1be NC |
6725 | Use C<sv_pos_u2b_flags> in preference, which correctly handles strings longer |
6726 | than 2Gb. | |
6727 | ||
777f7c56 EB |
6728 | =cut |
6729 | */ | |
6730 | ||
6731 | /* | |
6732 | * sv_pos_u2b() uses, like sv_pos_b2u(), the mg_ptr of the potential | |
6733 | * PERL_MAGIC_utf8 of the sv to store the mapping between UTF-8 and | |
6734 | * byte offsets. See also the comments of S_utf8_mg_pos_cache_update(). | |
6735 | * | |
6736 | */ | |
6737 | ||
6738 | /* This function is subject to size and sign problems */ | |
6739 | ||
6740 | void | |
6741 | Perl_sv_pos_u2b(pTHX_ register SV *const sv, I32 *const offsetp, I32 *const lenp) | |
6742 | { | |
d931b1be NC |
6743 | PERL_ARGS_ASSERT_SV_POS_U2B; |
6744 | ||
777f7c56 EB |
6745 | if (lenp) { |
6746 | STRLEN ulen = (STRLEN)*lenp; | |
d931b1be NC |
6747 | *offsetp = (I32)sv_pos_u2b_flags(sv, (STRLEN)*offsetp, &ulen, |
6748 | SV_GMAGIC|SV_CONST_RETURN); | |
777f7c56 EB |
6749 | *lenp = (I32)ulen; |
6750 | } else { | |
d931b1be NC |
6751 | *offsetp = (I32)sv_pos_u2b_flags(sv, (STRLEN)*offsetp, NULL, |
6752 | SV_GMAGIC|SV_CONST_RETURN); | |
777f7c56 | 6753 | } |
777f7c56 EB |
6754 | } |
6755 | ||
ec49a12c NC |
6756 | static void |
6757 | S_utf8_mg_len_cache_update(pTHX_ SV *const sv, MAGIC **const mgp, | |
6758 | const STRLEN ulen) | |
6759 | { | |
6760 | PERL_ARGS_ASSERT_UTF8_MG_LEN_CACHE_UPDATE; | |
6761 | if (SvREADONLY(sv)) | |
6762 | return; | |
6763 | ||
6764 | if (!*mgp && (SvTYPE(sv) < SVt_PVMG || | |
6765 | !(*mgp = mg_find(sv, PERL_MAGIC_utf8)))) { | |
6766 | *mgp = sv_magicext(sv, 0, PERL_MAGIC_utf8, &PL_vtbl_utf8, 0, 0); | |
6767 | } | |
6768 | assert(*mgp); | |
6769 | ||
6770 | (*mgp)->mg_len = ulen; | |
6771 | /* For now, treat "overflowed" as "still unknown". See RT #72924. */ | |
6772 | if (ulen != (STRLEN) (*mgp)->mg_len) | |
6773 | (*mgp)->mg_len = -1; | |
6774 | } | |
6775 | ||
9564a3bd NC |
6776 | /* Create and update the UTF8 magic offset cache, with the proffered utf8/ |
6777 | byte length pairing. The (byte) length of the total SV is passed in too, | |
6778 | as blen, because for some (more esoteric) SVs, the call to SvPV_const() | |
6779 | may not have updated SvCUR, so we can't rely on reading it directly. | |
6780 | ||
6781 | The proffered utf8/byte length pairing isn't used if the cache already has | |
6782 | two pairs, and swapping either for the proffered pair would increase the | |
6783 | RMS of the intervals between known byte offsets. | |
6784 | ||
6785 | The cache itself consists of 4 STRLEN values | |
6786 | 0: larger UTF-8 offset | |
6787 | 1: corresponding byte offset | |
6788 | 2: smaller UTF-8 offset | |
6789 | 3: corresponding byte offset | |
6790 | ||
6791 | Unused cache pairs have the value 0, 0. | |
6792 | Keeping the cache "backwards" means that the invariant of | |
6793 | cache[0] >= cache[2] is maintained even with empty slots, which means that | |
6794 | the code that uses it doesn't need to worry if only 1 entry has actually | |
6795 | been set to non-zero. It also makes the "position beyond the end of the | |
6796 | cache" logic much simpler, as the first slot is always the one to start | |
6797 | from. | |
645c22ef | 6798 | */ |
ec07b5e0 | 6799 | static void |
ac1e9476 SS |
6800 | S_utf8_mg_pos_cache_update(pTHX_ SV *const sv, MAGIC **const mgp, const STRLEN byte, |
6801 | const STRLEN utf8, const STRLEN blen) | |
ec07b5e0 NC |
6802 | { |
6803 | STRLEN *cache; | |
7918f24d NC |
6804 | |
6805 | PERL_ARGS_ASSERT_UTF8_MG_POS_CACHE_UPDATE; | |
6806 | ||
ec07b5e0 NC |
6807 | if (SvREADONLY(sv)) |
6808 | return; | |
6809 | ||
f89a570b CS |
6810 | if (!*mgp && (SvTYPE(sv) < SVt_PVMG || |
6811 | !(*mgp = mg_find(sv, PERL_MAGIC_utf8)))) { | |
ec07b5e0 NC |
6812 | *mgp = sv_magicext(sv, 0, PERL_MAGIC_utf8, (MGVTBL*)&PL_vtbl_utf8, 0, |
6813 | 0); | |
6814 | (*mgp)->mg_len = -1; | |
6815 | } | |
6816 | assert(*mgp); | |
6817 | ||
6818 | if (!(cache = (STRLEN *)(*mgp)->mg_ptr)) { | |
6819 | Newxz(cache, PERL_MAGIC_UTF8_CACHESIZE * 2, STRLEN); | |
6820 | (*mgp)->mg_ptr = (char *) cache; | |
6821 | } | |
6822 | assert(cache); | |
6823 | ||
ab8be49d NC |
6824 | if (PL_utf8cache < 0 && SvPOKp(sv)) { |
6825 | /* SvPOKp() because it's possible that sv has string overloading, and | |
6826 | therefore is a reference, hence SvPVX() is actually a pointer. | |
6827 | This cures the (very real) symptoms of RT 69422, but I'm not actually | |
6828 | sure whether we should even be caching the results of UTF-8 | |
6829 | operations on overloading, given that nothing stops overloading | |
6830 | returning a different value every time it's called. */ | |
ef816a78 | 6831 | const U8 *start = (const U8 *) SvPVX_const(sv); |
6448472a | 6832 | const STRLEN realutf8 = utf8_length(start, start + byte); |
ec07b5e0 | 6833 | |
9df83ffd NC |
6834 | assert_uft8_cache_coherent("utf8_mg_pos_cache_update", utf8, realutf8, |
6835 | sv); | |
ec07b5e0 | 6836 | } |
ab455f60 NC |
6837 | |
6838 | /* Cache is held with the later position first, to simplify the code | |
6839 | that deals with unbounded ends. */ | |
6840 | ||
6841 | ASSERT_UTF8_CACHE(cache); | |
6842 | if (cache[1] == 0) { | |
6843 | /* Cache is totally empty */ | |
6844 | cache[0] = utf8; | |
6845 | cache[1] = byte; | |
6846 | } else if (cache[3] == 0) { | |
6847 | if (byte > cache[1]) { | |
6848 | /* New one is larger, so goes first. */ | |
6849 | cache[2] = cache[0]; | |
6850 | cache[3] = cache[1]; | |
6851 | cache[0] = utf8; | |
6852 | cache[1] = byte; | |
6853 | } else { | |
6854 | cache[2] = utf8; | |
6855 | cache[3] = byte; | |
6856 | } | |
6857 | } else { | |
6858 | #define THREEWAY_SQUARE(a,b,c,d) \ | |
6859 | ((float)((d) - (c))) * ((float)((d) - (c))) \ | |
6860 | + ((float)((c) - (b))) * ((float)((c) - (b))) \ | |
6861 | + ((float)((b) - (a))) * ((float)((b) - (a))) | |
6862 | ||
6863 | /* Cache has 2 slots in use, and we know three potential pairs. | |
6864 | Keep the two that give the lowest RMS distance. Do the | |
486ec47a | 6865 | calculation in bytes simply because we always know the byte |
ab455f60 NC |
6866 | length. squareroot has the same ordering as the positive value, |
6867 | so don't bother with the actual square root. */ | |
6868 | const float existing = THREEWAY_SQUARE(0, cache[3], cache[1], blen); | |
6869 | if (byte > cache[1]) { | |
6870 | /* New position is after the existing pair of pairs. */ | |
6871 | const float keep_earlier | |
6872 | = THREEWAY_SQUARE(0, cache[3], byte, blen); | |
6873 | const float keep_later | |
6874 | = THREEWAY_SQUARE(0, cache[1], byte, blen); | |
6875 | ||
6876 | if (keep_later < keep_earlier) { | |
6877 | if (keep_later < existing) { | |
6878 | cache[2] = cache[0]; | |
6879 | cache[3] = cache[1]; | |
6880 | cache[0] = utf8; | |
6881 | cache[1] = byte; | |
6882 | } | |
6883 | } | |
6884 | else { | |
6885 | if (keep_earlier < existing) { | |
6886 | cache[0] = utf8; | |
6887 | cache[1] = byte; | |
6888 | } | |
6889 | } | |
6890 | } | |
57d7fbf1 NC |
6891 | else if (byte > cache[3]) { |
6892 | /* New position is between the existing pair of pairs. */ | |
6893 | const float keep_earlier | |
6894 | = THREEWAY_SQUARE(0, cache[3], byte, blen); | |
6895 | const float keep_later | |
6896 | = THREEWAY_SQUARE(0, byte, cache[1], blen); | |
6897 | ||
6898 | if (keep_later < keep_earlier) { | |
6899 | if (keep_later < existing) { | |
6900 | cache[2] = utf8; | |
6901 | cache[3] = byte; | |
6902 | } | |
6903 | } | |
6904 | else { | |
6905 | if (keep_earlier < existing) { | |
6906 | cache[0] = utf8; | |
6907 | cache[1] = byte; | |
6908 | } | |
6909 | } | |
6910 | } | |
6911 | else { | |
6912 | /* New position is before the existing pair of pairs. */ | |
6913 | const float keep_earlier | |
6914 | = THREEWAY_SQUARE(0, byte, cache[3], blen); | |
6915 | const float keep_later | |
6916 | = THREEWAY_SQUARE(0, byte, cache[1], blen); | |
6917 | ||
6918 | if (keep_later < keep_earlier) { | |
6919 | if (keep_later < existing) { | |
6920 | cache[2] = utf8; | |
6921 | cache[3] = byte; | |
6922 | } | |
6923 | } | |
6924 | else { | |
6925 | if (keep_earlier < existing) { | |
6926 | cache[0] = cache[2]; | |
6927 | cache[1] = cache[3]; | |
6928 | cache[2] = utf8; | |
6929 | cache[3] = byte; | |
6930 | } | |
6931 | } | |
6932 | } | |
ab455f60 | 6933 | } |
0905937d | 6934 | ASSERT_UTF8_CACHE(cache); |
ec07b5e0 NC |
6935 | } |
6936 | ||
ec07b5e0 | 6937 | /* We already know all of the way, now we may be able to walk back. The same |
25a8a4ef NC |
6938 | assumption is made as in S_sv_pos_u2b_midway(), namely that walking |
6939 | backward is half the speed of walking forward. */ | |
ec07b5e0 | 6940 | static STRLEN |
ac1e9476 SS |
6941 | S_sv_pos_b2u_midway(pTHX_ const U8 *const s, const U8 *const target, |
6942 | const U8 *end, STRLEN endu) | |
ec07b5e0 NC |
6943 | { |
6944 | const STRLEN forw = target - s; | |
6945 | STRLEN backw = end - target; | |
6946 | ||
7918f24d NC |
6947 | PERL_ARGS_ASSERT_SV_POS_B2U_MIDWAY; |
6948 | ||
ec07b5e0 | 6949 | if (forw < 2 * backw) { |
6448472a | 6950 | return utf8_length(s, target); |
ec07b5e0 NC |
6951 | } |
6952 | ||
6953 | while (end > target) { | |
6954 | end--; | |
6955 | while (UTF8_IS_CONTINUATION(*end)) { | |
6956 | end--; | |
6957 | } | |
6958 | endu--; | |
6959 | } | |
6960 | return endu; | |
6961 | } | |
6962 | ||
9564a3bd NC |
6963 | /* |
6964 | =for apidoc sv_pos_b2u | |
6965 | ||
6966 | Converts the value pointed to by offsetp from a count of bytes from the | |
6967 | start of the string, to a count of the equivalent number of UTF-8 chars. | |
6968 | Handles magic and type coercion. | |
6969 | ||
6970 | =cut | |
6971 | */ | |
6972 | ||
6973 | /* | |
6974 | * sv_pos_b2u() uses, like sv_pos_u2b(), the mg_ptr of the potential | |
c05a5c57 | 6975 | * PERL_MAGIC_utf8 of the sv to store the mapping between UTF-8 and |
9564a3bd NC |
6976 | * byte offsets. |
6977 | * | |
6978 | */ | |
a0ed51b3 | 6979 | void |
ac1e9476 | 6980 | Perl_sv_pos_b2u(pTHX_ register SV *const sv, I32 *const offsetp) |
a0ed51b3 | 6981 | { |
83003860 | 6982 | const U8* s; |
ec07b5e0 | 6983 | const STRLEN byte = *offsetp; |
7087a21c | 6984 | STRLEN len = 0; /* Actually always set, but let's keep gcc happy. */ |
ab455f60 | 6985 | STRLEN blen; |
ec07b5e0 NC |
6986 | MAGIC* mg = NULL; |
6987 | const U8* send; | |
a922f900 | 6988 | bool found = FALSE; |
a0ed51b3 | 6989 | |
7918f24d NC |
6990 | PERL_ARGS_ASSERT_SV_POS_B2U; |
6991 | ||
a0ed51b3 LW |
6992 | if (!sv) |
6993 | return; | |
6994 | ||
ab455f60 | 6995 | s = (const U8*)SvPV_const(sv, blen); |
7e8c5dac | 6996 | |
ab455f60 | 6997 | if (blen < byte) |
ec07b5e0 | 6998 | Perl_croak(aTHX_ "panic: sv_pos_b2u: bad byte offset"); |
7e8c5dac | 6999 | |
ec07b5e0 | 7000 | send = s + byte; |
a67d7df9 | 7001 | |
f89a570b CS |
7002 | if (!SvREADONLY(sv) |
7003 | && PL_utf8cache | |
7004 | && SvTYPE(sv) >= SVt_PVMG | |
7005 | && (mg = mg_find(sv, PERL_MAGIC_utf8))) | |
7006 | { | |
ffca234a | 7007 | if (mg->mg_ptr) { |
d4c19fe8 | 7008 | STRLEN * const cache = (STRLEN *) mg->mg_ptr; |
b9f984a5 | 7009 | if (cache[1] == byte) { |
ec07b5e0 NC |
7010 | /* An exact match. */ |
7011 | *offsetp = cache[0]; | |
ec07b5e0 | 7012 | return; |
7e8c5dac | 7013 | } |
ab455f60 NC |
7014 | if (cache[3] == byte) { |
7015 | /* An exact match. */ | |
7016 | *offsetp = cache[2]; | |
7017 | return; | |
7018 | } | |
668af93f NC |
7019 | |
7020 | if (cache[1] < byte) { | |
ec07b5e0 | 7021 | /* We already know part of the way. */ |
b9f984a5 NC |
7022 | if (mg->mg_len != -1) { |
7023 | /* Actually, we know the end too. */ | |
7024 | len = cache[0] | |
7025 | + S_sv_pos_b2u_midway(aTHX_ s + cache[1], send, | |
ab455f60 | 7026 | s + blen, mg->mg_len - cache[0]); |
b9f984a5 | 7027 | } else { |
6448472a | 7028 | len = cache[0] + utf8_length(s + cache[1], send); |
b9f984a5 | 7029 | } |
7e8c5dac | 7030 | } |
9f985e4c NC |
7031 | else if (cache[3] < byte) { |
7032 | /* We're between the two cached pairs, so we do the calculation | |
7033 | offset by the byte/utf-8 positions for the earlier pair, | |
7034 | then add the utf-8 characters from the string start to | |
7035 | there. */ | |
7036 | len = S_sv_pos_b2u_midway(aTHX_ s + cache[3], send, | |
7037 | s + cache[1], cache[0] - cache[2]) | |
7038 | + cache[2]; | |
7039 | ||
7040 | } | |
7041 | else { /* cache[3] > byte */ | |
7042 | len = S_sv_pos_b2u_midway(aTHX_ s, send, s + cache[3], | |
7043 | cache[2]); | |
7e8c5dac | 7044 | |
7e8c5dac | 7045 | } |
ec07b5e0 | 7046 | ASSERT_UTF8_CACHE(cache); |
a922f900 | 7047 | found = TRUE; |
ffca234a | 7048 | } else if (mg->mg_len != -1) { |
ab455f60 | 7049 | len = S_sv_pos_b2u_midway(aTHX_ s, send, s + blen, mg->mg_len); |
a922f900 | 7050 | found = TRUE; |
7e8c5dac | 7051 | } |
a0ed51b3 | 7052 | } |
a922f900 | 7053 | if (!found || PL_utf8cache < 0) { |
6448472a | 7054 | const STRLEN real_len = utf8_length(s, send); |
a922f900 | 7055 | |
9df83ffd NC |
7056 | if (found && PL_utf8cache < 0) |
7057 | assert_uft8_cache_coherent("sv_pos_b2u", len, real_len, sv); | |
a922f900 | 7058 | len = real_len; |
ec07b5e0 NC |
7059 | } |
7060 | *offsetp = len; | |
7061 | ||
0d7caf4c NC |
7062 | if (PL_utf8cache) { |
7063 | if (blen == byte) | |
7064 | utf8_mg_len_cache_update(sv, &mg, len); | |
7065 | else | |
7066 | utf8_mg_pos_cache_update(sv, &mg, byte, len, blen); | |
7067 | } | |
a0ed51b3 LW |
7068 | } |
7069 | ||
9df83ffd NC |
7070 | static void |
7071 | S_assert_uft8_cache_coherent(pTHX_ const char *const func, STRLEN from_cache, | |
7072 | STRLEN real, SV *const sv) | |
7073 | { | |
7074 | PERL_ARGS_ASSERT_ASSERT_UFT8_CACHE_COHERENT; | |
7075 | ||
7076 | /* As this is debugging only code, save space by keeping this test here, | |
7077 | rather than inlining it in all the callers. */ | |
7078 | if (from_cache == real) | |
7079 | return; | |
7080 | ||
7081 | /* Need to turn the assertions off otherwise we may recurse infinitely | |
7082 | while printing error messages. */ | |
7083 | SAVEI8(PL_utf8cache); | |
7084 | PL_utf8cache = 0; | |
7085 | Perl_croak(aTHX_ "panic: %s cache %"UVuf" real %"UVuf" for %"SVf, | |
7086 | func, (UV) from_cache, (UV) real, SVfARG(sv)); | |
7087 | } | |
7088 | ||
954c1994 GS |
7089 | /* |
7090 | =for apidoc sv_eq | |
7091 | ||
7092 | Returns a boolean indicating whether the strings in the two SVs are | |
645c22ef DM |
7093 | identical. Is UTF-8 and 'use bytes' aware, handles get magic, and will |
7094 | coerce its args to strings if necessary. | |
954c1994 | 7095 | |
078504b2 FC |
7096 | =for apidoc sv_eq_flags |
7097 | ||
7098 | Returns a boolean indicating whether the strings in the two SVs are | |
7099 | identical. Is UTF-8 and 'use bytes' aware and coerces its args to strings | |
7100 | if necessary. If the flags include SV_GMAGIC, it handles get-magic, too. | |
7101 | ||
954c1994 GS |
7102 | =cut |
7103 | */ | |
7104 | ||
79072805 | 7105 | I32 |
31c72c81 | 7106 | Perl_sv_eq_flags(pTHX_ register SV *sv1, register SV *sv2, const U32 flags) |
79072805 | 7107 | { |
97aff369 | 7108 | dVAR; |
e1ec3a88 | 7109 | const char *pv1; |
463ee0b2 | 7110 | STRLEN cur1; |
e1ec3a88 | 7111 | const char *pv2; |
463ee0b2 | 7112 | STRLEN cur2; |
e01b9e88 | 7113 | I32 eq = 0; |
bd61b366 | 7114 | char *tpv = NULL; |
a0714e2c | 7115 | SV* svrecode = NULL; |
79072805 | 7116 | |
e01b9e88 | 7117 | if (!sv1) { |
79072805 LW |
7118 | pv1 = ""; |
7119 | cur1 = 0; | |
7120 | } | |
ced497e2 YST |
7121 | else { |
7122 | /* if pv1 and pv2 are the same, second SvPV_const call may | |
078504b2 FC |
7123 | * invalidate pv1 (if we are handling magic), so we may need to |
7124 | * make a copy */ | |
7125 | if (sv1 == sv2 && flags & SV_GMAGIC | |
7126 | && (SvTHINKFIRST(sv1) || SvGMAGICAL(sv1))) { | |
ced497e2 | 7127 | pv1 = SvPV_const(sv1, cur1); |
59cd0e26 | 7128 | sv1 = newSVpvn_flags(pv1, cur1, SVs_TEMP | SvUTF8(sv2)); |
ced497e2 | 7129 | } |
078504b2 | 7130 | pv1 = SvPV_flags_const(sv1, cur1, flags); |
ced497e2 | 7131 | } |
79072805 | 7132 | |
e01b9e88 SC |
7133 | if (!sv2){ |
7134 | pv2 = ""; | |
7135 | cur2 = 0; | |
92d29cee | 7136 | } |
e01b9e88 | 7137 | else |
078504b2 | 7138 | pv2 = SvPV_flags_const(sv2, cur2, flags); |
79072805 | 7139 | |
cf48d248 | 7140 | if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) { |
799ef3cb JH |
7141 | /* Differing utf8ness. |
7142 | * Do not UTF8size the comparands as a side-effect. */ | |
7143 | if (PL_encoding) { | |
7144 | if (SvUTF8(sv1)) { | |
553e1bcc AT |
7145 | svrecode = newSVpvn(pv2, cur2); |
7146 | sv_recode_to_utf8(svrecode, PL_encoding); | |
93524f2b | 7147 | pv2 = SvPV_const(svrecode, cur2); |
799ef3cb JH |
7148 | } |
7149 | else { | |
553e1bcc AT |
7150 | svrecode = newSVpvn(pv1, cur1); |
7151 | sv_recode_to_utf8(svrecode, PL_encoding); | |
93524f2b | 7152 | pv1 = SvPV_const(svrecode, cur1); |
799ef3cb JH |
7153 | } |
7154 | /* Now both are in UTF-8. */ | |
0a1bd7ac DM |
7155 | if (cur1 != cur2) { |
7156 | SvREFCNT_dec(svrecode); | |
799ef3cb | 7157 | return FALSE; |
0a1bd7ac | 7158 | } |
799ef3cb JH |
7159 | } |
7160 | else { | |
799ef3cb | 7161 | if (SvUTF8(sv1)) { |
fed3ba5d NC |
7162 | /* sv1 is the UTF-8 one */ |
7163 | return bytes_cmp_utf8((const U8*)pv2, cur2, | |
7164 | (const U8*)pv1, cur1) == 0; | |
799ef3cb JH |
7165 | } |
7166 | else { | |
fed3ba5d NC |
7167 | /* sv2 is the UTF-8 one */ |
7168 | return bytes_cmp_utf8((const U8*)pv1, cur1, | |
7169 | (const U8*)pv2, cur2) == 0; | |
799ef3cb JH |
7170 | } |
7171 | } | |
cf48d248 JH |
7172 | } |
7173 | ||
7174 | if (cur1 == cur2) | |
765f542d | 7175 | eq = (pv1 == pv2) || memEQ(pv1, pv2, cur1); |
e01b9e88 | 7176 | |
b37c2d43 | 7177 | SvREFCNT_dec(svrecode); |
553e1bcc AT |
7178 | if (tpv) |
7179 | Safefree(tpv); | |
cf48d248 | 7180 | |
e01b9e88 | 7181 | return eq; |
79072805 LW |
7182 | } |
7183 | ||
954c1994 GS |
7184 | /* |
7185 | =for apidoc sv_cmp | |
7186 | ||
7187 | Compares the strings in two SVs. Returns -1, 0, or 1 indicating whether the | |
7188 | string in C<sv1> is less than, equal to, or greater than the string in | |
645c22ef DM |
7189 | C<sv2>. Is UTF-8 and 'use bytes' aware, handles get magic, and will |
7190 | coerce its args to strings if necessary. See also C<sv_cmp_locale>. | |
954c1994 | 7191 | |
078504b2 FC |
7192 | =for apidoc sv_cmp_flags |
7193 | ||
7194 | Compares the strings in two SVs. Returns -1, 0, or 1 indicating whether the | |
7195 | string in C<sv1> is less than, equal to, or greater than the string in | |
7196 | C<sv2>. Is UTF-8 and 'use bytes' aware and will coerce its args to strings | |
7197 | if necessary. If the flags include SV_GMAGIC, it handles get magic. See | |
7198 | also C<sv_cmp_locale_flags>. | |
7199 | ||
954c1994 GS |
7200 | =cut |
7201 | */ | |
7202 | ||
79072805 | 7203 | I32 |
ac1e9476 | 7204 | Perl_sv_cmp(pTHX_ register SV *const sv1, register SV *const sv2) |
79072805 | 7205 | { |
078504b2 FC |
7206 | return sv_cmp_flags(sv1, sv2, SV_GMAGIC); |
7207 | } | |
7208 | ||
7209 | I32 | |
31c72c81 NC |
7210 | Perl_sv_cmp_flags(pTHX_ register SV *const sv1, register SV *const sv2, |
7211 | const U32 flags) | |
078504b2 | 7212 | { |
97aff369 | 7213 | dVAR; |
560a288e | 7214 | STRLEN cur1, cur2; |
e1ec3a88 | 7215 | const char *pv1, *pv2; |
bd61b366 | 7216 | char *tpv = NULL; |
cf48d248 | 7217 | I32 cmp; |
a0714e2c | 7218 | SV *svrecode = NULL; |
560a288e | 7219 | |
e01b9e88 SC |
7220 | if (!sv1) { |
7221 | pv1 = ""; | |
560a288e GS |
7222 | cur1 = 0; |
7223 | } | |
e01b9e88 | 7224 | else |
078504b2 | 7225 | pv1 = SvPV_flags_const(sv1, cur1, flags); |
560a288e | 7226 | |
553e1bcc | 7227 | if (!sv2) { |
e01b9e88 | 7228 | pv2 = ""; |
560a288e GS |
7229 | cur2 = 0; |
7230 | } | |
e01b9e88 | 7231 | else |
078504b2 | 7232 | pv2 = SvPV_flags_const(sv2, cur2, flags); |
79072805 | 7233 | |
cf48d248 | 7234 | if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) { |
799ef3cb JH |
7235 | /* Differing utf8ness. |
7236 | * Do not UTF8size the comparands as a side-effect. */ | |
cf48d248 | 7237 | if (SvUTF8(sv1)) { |
799ef3cb | 7238 | if (PL_encoding) { |
553e1bcc AT |
7239 | svrecode = newSVpvn(pv2, cur2); |
7240 | sv_recode_to_utf8(svrecode, PL_encoding); | |
93524f2b | 7241 | pv2 = SvPV_const(svrecode, cur2); |
799ef3cb JH |
7242 | } |
7243 | else { | |
fed3ba5d NC |
7244 | const int retval = -bytes_cmp_utf8((const U8*)pv2, cur2, |
7245 | (const U8*)pv1, cur1); | |
7246 | return retval ? retval < 0 ? -1 : +1 : 0; | |
799ef3cb | 7247 | } |
cf48d248 JH |
7248 | } |
7249 | else { | |
799ef3cb | 7250 | if (PL_encoding) { |
553e1bcc AT |
7251 | svrecode = newSVpvn(pv1, cur1); |
7252 | sv_recode_to_utf8(svrecode, PL_encoding); | |
93524f2b | 7253 | pv1 = SvPV_const(svrecode, cur1); |
799ef3cb JH |
7254 | } |
7255 | else { | |
fed3ba5d NC |
7256 | const int retval = bytes_cmp_utf8((const U8*)pv1, cur1, |
7257 | (const U8*)pv2, cur2); | |
7258 | return retval ? retval < 0 ? -1 : +1 : 0; | |
799ef3cb | 7259 | } |
cf48d248 JH |
7260 | } |
7261 | } | |
7262 | ||
e01b9e88 | 7263 | if (!cur1) { |
cf48d248 | 7264 | cmp = cur2 ? -1 : 0; |
e01b9e88 | 7265 | } else if (!cur2) { |
cf48d248 JH |
7266 | cmp = 1; |
7267 | } else { | |
e1ec3a88 | 7268 | const I32 retval = memcmp((const void*)pv1, (const void*)pv2, cur1 < cur2 ? cur1 : cur2); |
e01b9e88 SC |
7269 | |
7270 | if (retval) { | |
cf48d248 | 7271 | cmp = retval < 0 ? -1 : 1; |
e01b9e88 | 7272 | } else if (cur1 == cur2) { |
cf48d248 JH |
7273 | cmp = 0; |
7274 | } else { | |
7275 | cmp = cur1 < cur2 ? -1 : 1; | |
e01b9e88 | 7276 | } |
cf48d248 | 7277 | } |
16660edb | 7278 | |
b37c2d43 | 7279 | SvREFCNT_dec(svrecode); |
553e1bcc AT |
7280 | if (tpv) |
7281 | Safefree(tpv); | |
cf48d248 JH |
7282 | |
7283 | return cmp; | |
bbce6d69 | 7284 | } |
16660edb | 7285 | |
c461cf8f JH |
7286 | /* |
7287 | =for apidoc sv_cmp_locale | |
7288 | ||
645c22ef DM |
7289 | Compares the strings in two SVs in a locale-aware manner. Is UTF-8 and |
7290 | 'use bytes' aware, handles get magic, and will coerce its args to strings | |
d77cdebf | 7291 | if necessary. See also C<sv_cmp>. |
c461cf8f | 7292 | |
078504b2 FC |
7293 | =for apidoc sv_cmp_locale_flags |
7294 | ||
7295 | Compares the strings in two SVs in a locale-aware manner. Is UTF-8 and | |
7296 | 'use bytes' aware and will coerce its args to strings if necessary. If the | |
7297 | flags contain SV_GMAGIC, it handles get magic. See also C<sv_cmp_flags>. | |
7298 | ||
c461cf8f JH |
7299 | =cut |
7300 | */ | |
7301 | ||
bbce6d69 | 7302 | I32 |
ac1e9476 | 7303 | Perl_sv_cmp_locale(pTHX_ register SV *const sv1, register SV *const sv2) |
bbce6d69 | 7304 | { |
078504b2 FC |
7305 | return sv_cmp_locale_flags(sv1, sv2, SV_GMAGIC); |
7306 | } | |
7307 | ||
7308 | I32 | |
31c72c81 NC |
7309 | Perl_sv_cmp_locale_flags(pTHX_ register SV *const sv1, register SV *const sv2, |
7310 | const U32 flags) | |
078504b2 | 7311 | { |
97aff369 | 7312 | dVAR; |
36477c24 | 7313 | #ifdef USE_LOCALE_COLLATE |
16660edb | 7314 | |
bbce6d69 | 7315 | char *pv1, *pv2; |
7316 | STRLEN len1, len2; | |
7317 | I32 retval; | |
16660edb | 7318 | |
3280af22 | 7319 | if (PL_collation_standard) |
bbce6d69 | 7320 | goto raw_compare; |
16660edb | 7321 | |
bbce6d69 | 7322 | len1 = 0; |
078504b2 | 7323 | pv1 = sv1 ? sv_collxfrm_flags(sv1, &len1, flags) : (char *) NULL; |
bbce6d69 | 7324 | len2 = 0; |
078504b2 | 7325 | pv2 = sv2 ? sv_collxfrm_flags(sv2, &len2, flags) : (char *) NULL; |
16660edb | 7326 | |
bbce6d69 | 7327 | if (!pv1 || !len1) { |
7328 | if (pv2 && len2) | |
7329 | return -1; | |
7330 | else | |
7331 | goto raw_compare; | |
7332 | } | |
7333 | else { | |
7334 | if (!pv2 || !len2) | |
7335 | return 1; | |
7336 | } | |
16660edb | 7337 | |
bbce6d69 | 7338 | retval = memcmp((void*)pv1, (void*)pv2, len1 < len2 ? len1 : len2); |
16660edb | 7339 | |
bbce6d69 | 7340 | if (retval) |
16660edb | 7341 | return retval < 0 ? -1 : 1; |
7342 | ||
bbce6d69 | 7343 | /* |
7344 | * When the result of collation is equality, that doesn't mean | |
7345 | * that there are no differences -- some locales exclude some | |
7346 | * characters from consideration. So to avoid false equalities, | |
7347 | * we use the raw string as a tiebreaker. | |
7348 | */ | |
16660edb | 7349 | |
bbce6d69 | 7350 | raw_compare: |
5f66b61c | 7351 | /*FALLTHROUGH*/ |
16660edb | 7352 | |
36477c24 | 7353 | #endif /* USE_LOCALE_COLLATE */ |
16660edb | 7354 | |
bbce6d69 | 7355 | return sv_cmp(sv1, sv2); |
7356 | } | |
79072805 | 7357 | |
645c22ef | 7358 | |
36477c24 | 7359 | #ifdef USE_LOCALE_COLLATE |
645c22ef | 7360 | |
7a4c00b4 | 7361 | /* |
645c22ef DM |
7362 | =for apidoc sv_collxfrm |
7363 | ||
078504b2 FC |
7364 | This calls C<sv_collxfrm_flags> with the SV_GMAGIC flag. See |
7365 | C<sv_collxfrm_flags>. | |
7366 | ||
7367 | =for apidoc sv_collxfrm_flags | |
7368 | ||
7369 | Add Collate Transform magic to an SV if it doesn't already have it. If the | |
7370 | flags contain SV_GMAGIC, it handles get-magic. | |
645c22ef DM |
7371 | |
7372 | Any scalar variable may carry PERL_MAGIC_collxfrm magic that contains the | |
7373 | scalar data of the variable, but transformed to such a format that a normal | |
7374 | memory comparison can be used to compare the data according to the locale | |
7375 | settings. | |
7376 | ||
7377 | =cut | |
7378 | */ | |
7379 | ||
bbce6d69 | 7380 | char * |
078504b2 | 7381 | Perl_sv_collxfrm_flags(pTHX_ SV *const sv, STRLEN *const nxp, const I32 flags) |
bbce6d69 | 7382 | { |
97aff369 | 7383 | dVAR; |
7a4c00b4 | 7384 | MAGIC *mg; |
16660edb | 7385 | |
078504b2 | 7386 | PERL_ARGS_ASSERT_SV_COLLXFRM_FLAGS; |
7918f24d | 7387 | |
14befaf4 | 7388 | mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_collxfrm) : (MAGIC *) NULL; |
3280af22 | 7389 | if (!mg || !mg->mg_ptr || *(U32*)mg->mg_ptr != PL_collation_ix) { |
93524f2b NC |
7390 | const char *s; |
7391 | char *xf; | |
bbce6d69 | 7392 | STRLEN len, xlen; |
7393 | ||
7a4c00b4 | 7394 | if (mg) |
7395 | Safefree(mg->mg_ptr); | |
078504b2 | 7396 | s = SvPV_flags_const(sv, len, flags); |
bbce6d69 | 7397 | if ((xf = mem_collxfrm(s, len, &xlen))) { |
7a4c00b4 | 7398 | if (! mg) { |
d83f0a82 NC |
7399 | #ifdef PERL_OLD_COPY_ON_WRITE |
7400 | if (SvIsCOW(sv)) | |
7401 | sv_force_normal_flags(sv, 0); | |
7402 | #endif | |
7403 | mg = sv_magicext(sv, 0, PERL_MAGIC_collxfrm, &PL_vtbl_collxfrm, | |
7404 | 0, 0); | |
7a4c00b4 | 7405 | assert(mg); |
bbce6d69 | 7406 | } |
7a4c00b4 | 7407 | mg->mg_ptr = xf; |
565764a8 | 7408 | mg->mg_len = xlen; |
7a4c00b4 | 7409 | } |
7410 | else { | |
ff0cee69 | 7411 | if (mg) { |
7412 | mg->mg_ptr = NULL; | |
565764a8 | 7413 | mg->mg_len = -1; |
ff0cee69 | 7414 | } |
bbce6d69 | 7415 | } |
7416 | } | |
7a4c00b4 | 7417 | if (mg && mg->mg_ptr) { |
565764a8 | 7418 | *nxp = mg->mg_len; |
3280af22 | 7419 | return mg->mg_ptr + sizeof(PL_collation_ix); |
bbce6d69 | 7420 | } |
7421 | else { | |
7422 | *nxp = 0; | |
7423 | return NULL; | |
16660edb | 7424 | } |
79072805 LW |
7425 | } |
7426 | ||
36477c24 | 7427 | #endif /* USE_LOCALE_COLLATE */ |
bbce6d69 | 7428 | |
f80c2205 NC |
7429 | static char * |
7430 | S_sv_gets_append_to_utf8(pTHX_ SV *const sv, PerlIO *const fp, I32 append) | |
7431 | { | |
7432 | SV * const tsv = newSV(0); | |
7433 | ENTER; | |
7434 | SAVEFREESV(tsv); | |
7435 | sv_gets(tsv, fp, 0); | |
7436 | sv_utf8_upgrade_nomg(tsv); | |
7437 | SvCUR_set(sv,append); | |
7438 | sv_catsv(sv,tsv); | |
7439 | LEAVE; | |
7440 | return (SvCUR(sv) - append) ? SvPVX(sv) : NULL; | |
7441 | } | |
7442 | ||
7443 | static char * | |
7444 | S_sv_gets_read_record(pTHX_ SV *const sv, PerlIO *const fp, I32 append) | |
7445 | { | |
7446 | I32 bytesread; | |
7447 | const U32 recsize = SvUV(SvRV(PL_rs)); /* RsRECORD() guarantees > 0. */ | |
7448 | /* Grab the size of the record we're getting */ | |
7449 | char *const buffer = SvGROW(sv, (STRLEN)(recsize + append + 1)) + append; | |
7450 | #ifdef VMS | |
7451 | int fd; | |
7452 | #endif | |
7453 | ||
7454 | /* Go yank in */ | |
7455 | #ifdef VMS | |
7456 | /* VMS wants read instead of fread, because fread doesn't respect */ | |
7457 | /* RMS record boundaries. This is not necessarily a good thing to be */ | |
7458 | /* doing, but we've got no other real choice - except avoid stdio | |
7459 | as implementation - perhaps write a :vms layer ? | |
7460 | */ | |
7461 | fd = PerlIO_fileno(fp); | |
7462 | if (fd != -1) { | |
7463 | bytesread = PerlLIO_read(fd, buffer, recsize); | |
7464 | } | |
7465 | else /* in-memory file from PerlIO::Scalar */ | |
7466 | #endif | |
7467 | { | |
7468 | bytesread = PerlIO_read(fp, buffer, recsize); | |
7469 | } | |
7470 | ||
7471 | if (bytesread < 0) | |
7472 | bytesread = 0; | |
7473 | SvCUR_set(sv, bytesread + append); | |
7474 | buffer[bytesread] = '\0'; | |
7475 | return (SvCUR(sv) - append) ? SvPVX(sv) : NULL; | |
7476 | } | |
7477 | ||
c461cf8f JH |
7478 | /* |
7479 | =for apidoc sv_gets | |
7480 | ||
7481 | Get a line from the filehandle and store it into the SV, optionally | |
7482 | appending to the currently-stored string. | |
7483 | ||
7484 | =cut | |
7485 | */ | |
7486 | ||
79072805 | 7487 | char * |
ac1e9476 | 7488 | Perl_sv_gets(pTHX_ register SV *const sv, register PerlIO *const fp, I32 append) |
79072805 | 7489 | { |
97aff369 | 7490 | dVAR; |
e1ec3a88 | 7491 | const char *rsptr; |
c07a80fd | 7492 | STRLEN rslen; |
7493 | register STDCHAR rslast; | |
7494 | register STDCHAR *bp; | |
7495 | register I32 cnt; | |
9c5ffd7c | 7496 | I32 i = 0; |
8bfdd7d9 | 7497 | I32 rspara = 0; |
c07a80fd | 7498 | |
7918f24d NC |
7499 | PERL_ARGS_ASSERT_SV_GETS; |
7500 | ||
bc44a8a2 NC |
7501 | if (SvTHINKFIRST(sv)) |
7502 | sv_force_normal_flags(sv, append ? 0 : SV_COW_DROP_PV); | |
765f542d NC |
7503 | /* XXX. If you make this PVIV, then copy on write can copy scalars read |
7504 | from <>. | |
7505 | However, perlbench says it's slower, because the existing swipe code | |
7506 | is faster than copy on write. | |
7507 | Swings and roundabouts. */ | |
862a34c6 | 7508 | SvUPGRADE(sv, SVt_PV); |
99491443 | 7509 | |
ff68c719 | 7510 | SvSCREAM_off(sv); |
efd8b2ba AE |
7511 | |
7512 | if (append) { | |
7513 | if (PerlIO_isutf8(fp)) { | |
7514 | if (!SvUTF8(sv)) { | |
7515 | sv_utf8_upgrade_nomg(sv); | |
7516 | sv_pos_u2b(sv,&append,0); | |
7517 | } | |
7518 | } else if (SvUTF8(sv)) { | |
f80c2205 | 7519 | return S_sv_gets_append_to_utf8(aTHX_ sv, fp, append); |
efd8b2ba AE |
7520 | } |
7521 | } | |
7522 | ||
7523 | SvPOK_only(sv); | |
05dee287 JJ |
7524 | if (!append) { |
7525 | SvCUR_set(sv,0); | |
7526 | } | |
efd8b2ba AE |
7527 | if (PerlIO_isutf8(fp)) |
7528 | SvUTF8_on(sv); | |
c07a80fd | 7529 | |
923e4eb5 | 7530 | if (IN_PERL_COMPILETIME) { |
8bfdd7d9 HS |
7531 | /* we always read code in line mode */ |
7532 | rsptr = "\n"; | |
7533 | rslen = 1; | |
7534 | } | |
7535 | else if (RsSNARF(PL_rs)) { | |
7a5fa8a2 | 7536 | /* If it is a regular disk file use size from stat() as estimate |
acbd132f JH |
7537 | of amount we are going to read -- may result in mallocing |
7538 | more memory than we really need if the layers below reduce | |
7539 | the size we read (e.g. CRLF or a gzip layer). | |
e468d35b | 7540 | */ |
e311fd51 | 7541 | Stat_t st; |
e468d35b | 7542 | if (!PerlLIO_fstat(PerlIO_fileno(fp), &st) && S_ISREG(st.st_mode)) { |
f54cb97a | 7543 | const Off_t offset = PerlIO_tell(fp); |
58f1856e | 7544 | if (offset != (Off_t) -1 && st.st_size + append > offset) { |
e468d35b NIS |
7545 | (void) SvGROW(sv, (STRLEN)((st.st_size - offset) + append + 1)); |
7546 | } | |
7547 | } | |
c07a80fd | 7548 | rsptr = NULL; |
7549 | rslen = 0; | |
7550 | } | |
3280af22 | 7551 | else if (RsRECORD(PL_rs)) { |
f80c2205 | 7552 | return S_sv_gets_read_record(aTHX_ sv, fp, append); |
5b2b9c68 | 7553 | } |
3280af22 | 7554 | else if (RsPARA(PL_rs)) { |
c07a80fd | 7555 | rsptr = "\n\n"; |
7556 | rslen = 2; | |
8bfdd7d9 | 7557 | rspara = 1; |
c07a80fd | 7558 | } |
7d59b7e4 NIS |
7559 | else { |
7560 | /* Get $/ i.e. PL_rs into same encoding as stream wants */ | |
7561 | if (PerlIO_isutf8(fp)) { | |
7562 | rsptr = SvPVutf8(PL_rs, rslen); | |
7563 | } | |
7564 | else { | |
7565 | if (SvUTF8(PL_rs)) { | |
7566 | if (!sv_utf8_downgrade(PL_rs, TRUE)) { | |
7567 | Perl_croak(aTHX_ "Wide character in $/"); | |
7568 | } | |
7569 | } | |
93524f2b | 7570 | rsptr = SvPV_const(PL_rs, rslen); |
7d59b7e4 NIS |
7571 | } |
7572 | } | |
7573 | ||
c07a80fd | 7574 | rslast = rslen ? rsptr[rslen - 1] : '\0'; |
7575 | ||
8bfdd7d9 | 7576 | if (rspara) { /* have to do this both before and after */ |
79072805 | 7577 | do { /* to make sure file boundaries work right */ |
760ac839 | 7578 | if (PerlIO_eof(fp)) |
a0d0e21e | 7579 | return 0; |
760ac839 | 7580 | i = PerlIO_getc(fp); |
79072805 | 7581 | if (i != '\n') { |
a0d0e21e LW |
7582 | if (i == -1) |
7583 | return 0; | |
760ac839 | 7584 | PerlIO_ungetc(fp,i); |
79072805 LW |
7585 | break; |
7586 | } | |
7587 | } while (i != EOF); | |
7588 | } | |
c07a80fd | 7589 | |
760ac839 LW |
7590 | /* See if we know enough about I/O mechanism to cheat it ! */ |
7591 | ||
7592 | /* This used to be #ifdef test - it is made run-time test for ease | |
1c846c1f | 7593 | of abstracting out stdio interface. One call should be cheap |
760ac839 LW |
7594 | enough here - and may even be a macro allowing compile |
7595 | time optimization. | |
7596 | */ | |
7597 | ||
7598 | if (PerlIO_fast_gets(fp)) { | |
7599 | ||
7600 | /* | |
7601 | * We're going to steal some values from the stdio struct | |
7602 | * and put EVERYTHING in the innermost loop into registers. | |
7603 | */ | |
7604 | register STDCHAR *ptr; | |
7605 | STRLEN bpx; | |
7606 | I32 shortbuffered; | |
7607 | ||
16660edb | 7608 | #if defined(VMS) && defined(PERLIO_IS_STDIO) |
7609 | /* An ungetc()d char is handled separately from the regular | |
7610 | * buffer, so we getc() it back out and stuff it in the buffer. | |
7611 | */ | |
7612 | i = PerlIO_getc(fp); | |
7613 | if (i == EOF) return 0; | |
7614 | *(--((*fp)->_ptr)) = (unsigned char) i; | |
7615 | (*fp)->_cnt++; | |
7616 | #endif | |
c07a80fd | 7617 | |
c2960299 | 7618 | /* Here is some breathtakingly efficient cheating */ |
c07a80fd | 7619 | |
a20bf0c3 | 7620 | cnt = PerlIO_get_cnt(fp); /* get count into register */ |
e468d35b | 7621 | /* make sure we have the room */ |
7a5fa8a2 | 7622 | if ((I32)(SvLEN(sv) - append) <= cnt + 1) { |
e468d35b | 7623 | /* Not room for all of it |
7a5fa8a2 | 7624 | if we are looking for a separator and room for some |
e468d35b NIS |
7625 | */ |
7626 | if (rslen && cnt > 80 && (I32)SvLEN(sv) > append) { | |
7a5fa8a2 | 7627 | /* just process what we have room for */ |
79072805 LW |
7628 | shortbuffered = cnt - SvLEN(sv) + append + 1; |
7629 | cnt -= shortbuffered; | |
7630 | } | |
7631 | else { | |
7632 | shortbuffered = 0; | |
bbce6d69 | 7633 | /* remember that cnt can be negative */ |
eb160463 | 7634 | SvGROW(sv, (STRLEN)(append + (cnt <= 0 ? 2 : (cnt + 1)))); |
79072805 LW |
7635 | } |
7636 | } | |
7a5fa8a2 | 7637 | else |
79072805 | 7638 | shortbuffered = 0; |
3f7c398e | 7639 | bp = (STDCHAR*)SvPVX_const(sv) + append; /* move these two too to registers */ |
a20bf0c3 | 7640 | ptr = (STDCHAR*)PerlIO_get_ptr(fp); |
16660edb | 7641 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 7642 | "Screamer: entering, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt)); |
16660edb | 7643 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
ba7abf9d | 7644 | "Screamer: entering: PerlIO * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n", |
1c846c1f | 7645 | PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp), |
1d7c1841 | 7646 | PTR2UV(PerlIO_has_base(fp) ? PerlIO_get_base(fp) : 0))); |
79072805 LW |
7647 | for (;;) { |
7648 | screamer: | |
93a17b20 | 7649 | if (cnt > 0) { |
c07a80fd | 7650 | if (rslen) { |
760ac839 LW |
7651 | while (cnt > 0) { /* this | eat */ |
7652 | cnt--; | |
c07a80fd | 7653 | if ((*bp++ = *ptr++) == rslast) /* really | dust */ |
7654 | goto thats_all_folks; /* screams | sed :-) */ | |
7655 | } | |
7656 | } | |
7657 | else { | |
1c846c1f NIS |
7658 | Copy(ptr, bp, cnt, char); /* this | eat */ |
7659 | bp += cnt; /* screams | dust */ | |
c07a80fd | 7660 | ptr += cnt; /* louder | sed :-) */ |
a5f75d66 | 7661 | cnt = 0; |
0f93bb20 NC |
7662 | assert (!shortbuffered); |
7663 | goto cannot_be_shortbuffered; | |
93a17b20 | 7664 | } |
79072805 LW |
7665 | } |
7666 | ||
748a9306 | 7667 | if (shortbuffered) { /* oh well, must extend */ |
79072805 LW |
7668 | cnt = shortbuffered; |
7669 | shortbuffered = 0; | |
3f7c398e | 7670 | bpx = bp - (STDCHAR*)SvPVX_const(sv); /* box up before relocation */ |
79072805 LW |
7671 | SvCUR_set(sv, bpx); |
7672 | SvGROW(sv, SvLEN(sv) + append + cnt + 2); | |
3f7c398e | 7673 | bp = (STDCHAR*)SvPVX_const(sv) + bpx; /* unbox after relocation */ |
79072805 LW |
7674 | continue; |
7675 | } | |
7676 | ||
0f93bb20 | 7677 | cannot_be_shortbuffered: |
16660edb | 7678 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 GS |
7679 | "Screamer: going to getc, ptr=%"UVuf", cnt=%ld\n", |
7680 | PTR2UV(ptr),(long)cnt)); | |
cc00df79 | 7681 | PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* deregisterize cnt and ptr */ |
ad9e76a8 NC |
7682 | |
7683 | DEBUG_Pv(PerlIO_printf(Perl_debug_log, | |
1d7c1841 | 7684 | "Screamer: pre: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n", |
1c846c1f | 7685 | PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp), |
1d7c1841 | 7686 | PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0))); |
ad9e76a8 | 7687 | |
1c846c1f | 7688 | /* This used to call 'filbuf' in stdio form, but as that behaves like |
774d564b | 7689 | getc when cnt <= 0 we use PerlIO_getc here to avoid introducing |
7690 | another abstraction. */ | |
760ac839 | 7691 | i = PerlIO_getc(fp); /* get more characters */ |
ad9e76a8 NC |
7692 | |
7693 | DEBUG_Pv(PerlIO_printf(Perl_debug_log, | |
1d7c1841 | 7694 | "Screamer: post: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n", |
1c846c1f | 7695 | PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp), |
1d7c1841 | 7696 | PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0))); |
ad9e76a8 | 7697 | |
a20bf0c3 JH |
7698 | cnt = PerlIO_get_cnt(fp); |
7699 | ptr = (STDCHAR*)PerlIO_get_ptr(fp); /* reregisterize cnt and ptr */ | |
16660edb | 7700 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 7701 | "Screamer: after getc, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt)); |
79072805 | 7702 | |
748a9306 LW |
7703 | if (i == EOF) /* all done for ever? */ |
7704 | goto thats_really_all_folks; | |
7705 | ||
3f7c398e | 7706 | bpx = bp - (STDCHAR*)SvPVX_const(sv); /* box up before relocation */ |
79072805 LW |
7707 | SvCUR_set(sv, bpx); |
7708 | SvGROW(sv, bpx + cnt + 2); | |
3f7c398e | 7709 | bp = (STDCHAR*)SvPVX_const(sv) + bpx; /* unbox after relocation */ |
c07a80fd | 7710 | |
eb160463 | 7711 | *bp++ = (STDCHAR)i; /* store character from PerlIO_getc */ |
79072805 | 7712 | |
c07a80fd | 7713 | if (rslen && (STDCHAR)i == rslast) /* all done for now? */ |
79072805 | 7714 | goto thats_all_folks; |
79072805 LW |
7715 | } |
7716 | ||
7717 | thats_all_folks: | |
3f7c398e | 7718 | if ((rslen > 1 && (STRLEN)(bp - (STDCHAR*)SvPVX_const(sv)) < rslen) || |
36477c24 | 7719 | memNE((char*)bp - rslen, rsptr, rslen)) |
760ac839 | 7720 | goto screamer; /* go back to the fray */ |
79072805 LW |
7721 | thats_really_all_folks: |
7722 | if (shortbuffered) | |
7723 | cnt += shortbuffered; | |
16660edb | 7724 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 7725 | "Screamer: quitting, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt)); |
cc00df79 | 7726 | PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* put these back or we're in trouble */ |
16660edb | 7727 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 7728 | "Screamer: end: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n", |
1c846c1f | 7729 | PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp), |
1d7c1841 | 7730 | PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0))); |
79072805 | 7731 | *bp = '\0'; |
3f7c398e | 7732 | SvCUR_set(sv, bp - (STDCHAR*)SvPVX_const(sv)); /* set length */ |
16660edb | 7733 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
fb73857a | 7734 | "Screamer: done, len=%ld, string=|%.*s|\n", |
3f7c398e | 7735 | (long)SvCUR(sv),(int)SvCUR(sv),SvPVX_const(sv))); |
760ac839 LW |
7736 | } |
7737 | else | |
79072805 | 7738 | { |
6edd2cd5 | 7739 | /*The big, slow, and stupid way. */ |
27da23d5 | 7740 | #ifdef USE_HEAP_INSTEAD_OF_STACK /* Even slower way. */ |
cbbf8932 | 7741 | STDCHAR *buf = NULL; |
a02a5408 | 7742 | Newx(buf, 8192, STDCHAR); |
6edd2cd5 | 7743 | assert(buf); |
4d2c4e07 | 7744 | #else |
6edd2cd5 | 7745 | STDCHAR buf[8192]; |
4d2c4e07 | 7746 | #endif |
79072805 | 7747 | |
760ac839 | 7748 | screamer2: |
c07a80fd | 7749 | if (rslen) { |
00b6aa41 | 7750 | register const STDCHAR * const bpe = buf + sizeof(buf); |
760ac839 | 7751 | bp = buf; |
eb160463 | 7752 | while ((i = PerlIO_getc(fp)) != EOF && (*bp++ = (STDCHAR)i) != rslast && bp < bpe) |
760ac839 LW |
7753 | ; /* keep reading */ |
7754 | cnt = bp - buf; | |
c07a80fd | 7755 | } |
7756 | else { | |
760ac839 | 7757 | cnt = PerlIO_read(fp,(char*)buf, sizeof(buf)); |
486ec47a | 7758 | /* Accommodate broken VAXC compiler, which applies U8 cast to |
16660edb | 7759 | * both args of ?: operator, causing EOF to change into 255 |
7760 | */ | |
37be0adf | 7761 | if (cnt > 0) |
cbe9e203 JH |
7762 | i = (U8)buf[cnt - 1]; |
7763 | else | |
37be0adf | 7764 | i = EOF; |
c07a80fd | 7765 | } |
79072805 | 7766 | |
cbe9e203 JH |
7767 | if (cnt < 0) |
7768 | cnt = 0; /* we do need to re-set the sv even when cnt <= 0 */ | |
7769 | if (append) | |
7770 | sv_catpvn(sv, (char *) buf, cnt); | |
7771 | else | |
7772 | sv_setpvn(sv, (char *) buf, cnt); | |
c07a80fd | 7773 | |
7774 | if (i != EOF && /* joy */ | |
7775 | (!rslen || | |
7776 | SvCUR(sv) < rslen || | |
3f7c398e | 7777 | memNE(SvPVX_const(sv) + SvCUR(sv) - rslen, rsptr, rslen))) |
79072805 LW |
7778 | { |
7779 | append = -1; | |
63e4d877 CS |
7780 | /* |
7781 | * If we're reading from a TTY and we get a short read, | |
7782 | * indicating that the user hit his EOF character, we need | |
7783 | * to notice it now, because if we try to read from the TTY | |
7784 | * again, the EOF condition will disappear. | |
7785 | * | |
7786 | * The comparison of cnt to sizeof(buf) is an optimization | |
7787 | * that prevents unnecessary calls to feof(). | |
7788 | * | |
7789 | * - jik 9/25/96 | |
7790 | */ | |
bb7a0f54 | 7791 | if (!(cnt < (I32)sizeof(buf) && PerlIO_eof(fp))) |
63e4d877 | 7792 | goto screamer2; |
79072805 | 7793 | } |
6edd2cd5 | 7794 | |
27da23d5 | 7795 | #ifdef USE_HEAP_INSTEAD_OF_STACK |
6edd2cd5 JH |
7796 | Safefree(buf); |
7797 | #endif | |
79072805 LW |
7798 | } |
7799 | ||
8bfdd7d9 | 7800 | if (rspara) { /* have to do this both before and after */ |
c07a80fd | 7801 | while (i != EOF) { /* to make sure file boundaries work right */ |
760ac839 | 7802 | i = PerlIO_getc(fp); |
79072805 | 7803 | if (i != '\n') { |
760ac839 | 7804 | PerlIO_ungetc(fp,i); |
79072805 LW |
7805 | break; |
7806 | } | |
7807 | } | |
7808 | } | |
c07a80fd | 7809 | |
bd61b366 | 7810 | return (SvCUR(sv) - append) ? SvPVX(sv) : NULL; |
79072805 LW |
7811 | } |
7812 | ||
954c1994 GS |
7813 | /* |
7814 | =for apidoc sv_inc | |
7815 | ||
645c22ef | 7816 | Auto-increment of the value in the SV, doing string to numeric conversion |
6f1401dc | 7817 | if necessary. Handles 'get' magic and operator overloading. |
954c1994 GS |
7818 | |
7819 | =cut | |
7820 | */ | |
7821 | ||
79072805 | 7822 | void |
ac1e9476 | 7823 | Perl_sv_inc(pTHX_ register SV *const sv) |
79072805 | 7824 | { |
6f1401dc DM |
7825 | if (!sv) |
7826 | return; | |
7827 | SvGETMAGIC(sv); | |
7828 | sv_inc_nomg(sv); | |
7829 | } | |
7830 | ||
7831 | /* | |
7832 | =for apidoc sv_inc_nomg | |
7833 | ||
7834 | Auto-increment of the value in the SV, doing string to numeric conversion | |
7835 | if necessary. Handles operator overloading. Skips handling 'get' magic. | |
7836 | ||
7837 | =cut | |
7838 | */ | |
7839 | ||
7840 | void | |
7841 | Perl_sv_inc_nomg(pTHX_ register SV *const sv) | |
7842 | { | |
97aff369 | 7843 | dVAR; |
79072805 | 7844 | register char *d; |
463ee0b2 | 7845 | int flags; |
79072805 LW |
7846 | |
7847 | if (!sv) | |
7848 | return; | |
ed6116ce | 7849 | if (SvTHINKFIRST(sv)) { |
765f542d NC |
7850 | if (SvIsCOW(sv)) |
7851 | sv_force_normal_flags(sv, 0); | |
0f15f207 | 7852 | if (SvREADONLY(sv)) { |
923e4eb5 | 7853 | if (IN_PERL_RUNTIME) |
6ad8f254 | 7854 | Perl_croak_no_modify(aTHX); |
0f15f207 | 7855 | } |
a0d0e21e | 7856 | if (SvROK(sv)) { |
b5be31e9 | 7857 | IV i; |
31d632c3 | 7858 | if (SvAMAGIC(sv) && AMG_CALLunary(sv, inc_amg)) |
9e7bc3e8 | 7859 | return; |
56431972 | 7860 | i = PTR2IV(SvRV(sv)); |
b5be31e9 SM |
7861 | sv_unref(sv); |
7862 | sv_setiv(sv, i); | |
a0d0e21e | 7863 | } |
ed6116ce | 7864 | } |
8990e307 | 7865 | flags = SvFLAGS(sv); |
28e5dec8 JH |
7866 | if ((flags & (SVp_NOK|SVp_IOK)) == SVp_NOK) { |
7867 | /* It's (privately or publicly) a float, but not tested as an | |
7868 | integer, so test it to see. */ | |
d460ef45 | 7869 | (void) SvIV(sv); |
28e5dec8 JH |
7870 | flags = SvFLAGS(sv); |
7871 | } | |
7872 | if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) { | |
7873 | /* It's publicly an integer, or privately an integer-not-float */ | |
59d8ce62 | 7874 | #ifdef PERL_PRESERVE_IVUV |
28e5dec8 | 7875 | oops_its_int: |
59d8ce62 | 7876 | #endif |
25da4f38 IZ |
7877 | if (SvIsUV(sv)) { |
7878 | if (SvUVX(sv) == UV_MAX) | |
a1e868e7 | 7879 | sv_setnv(sv, UV_MAX_P1); |
25da4f38 IZ |
7880 | else |
7881 | (void)SvIOK_only_UV(sv); | |
607fa7f2 | 7882 | SvUV_set(sv, SvUVX(sv) + 1); |
25da4f38 IZ |
7883 | } else { |
7884 | if (SvIVX(sv) == IV_MAX) | |
28e5dec8 | 7885 | sv_setuv(sv, (UV)IV_MAX + 1); |
25da4f38 IZ |
7886 | else { |
7887 | (void)SvIOK_only(sv); | |
45977657 | 7888 | SvIV_set(sv, SvIVX(sv) + 1); |
1c846c1f | 7889 | } |
55497cff | 7890 | } |
79072805 LW |
7891 | return; |
7892 | } | |
28e5dec8 | 7893 | if (flags & SVp_NOK) { |
b88df990 | 7894 | const NV was = SvNVX(sv); |
b68c599a | 7895 | if (NV_OVERFLOWS_INTEGERS_AT && |
a2a5de95 NC |
7896 | was >= NV_OVERFLOWS_INTEGERS_AT) { |
7897 | Perl_ck_warner(aTHX_ packWARN(WARN_IMPRECISION), | |
7898 | "Lost precision when incrementing %" NVff " by 1", | |
7899 | was); | |
b88df990 | 7900 | } |
28e5dec8 | 7901 | (void)SvNOK_only(sv); |
b68c599a | 7902 | SvNV_set(sv, was + 1.0); |
28e5dec8 JH |
7903 | return; |
7904 | } | |
7905 | ||
3f7c398e | 7906 | if (!(flags & SVp_POK) || !*SvPVX_const(sv)) { |
28e5dec8 | 7907 | if ((flags & SVTYPEMASK) < SVt_PVIV) |
f5282e15 | 7908 | sv_upgrade(sv, ((flags & SVTYPEMASK) > SVt_IV ? SVt_PVIV : SVt_IV)); |
28e5dec8 | 7909 | (void)SvIOK_only(sv); |
45977657 | 7910 | SvIV_set(sv, 1); |
79072805 LW |
7911 | return; |
7912 | } | |
463ee0b2 | 7913 | d = SvPVX(sv); |
79072805 LW |
7914 | while (isALPHA(*d)) d++; |
7915 | while (isDIGIT(*d)) d++; | |
6aff239d | 7916 | if (d < SvEND(sv)) { |
28e5dec8 | 7917 | #ifdef PERL_PRESERVE_IVUV |
d1be9408 | 7918 | /* Got to punt this as an integer if needs be, but we don't issue |
28e5dec8 JH |
7919 | warnings. Probably ought to make the sv_iv_please() that does |
7920 | the conversion if possible, and silently. */ | |
504618e9 | 7921 | const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), NULL); |
28e5dec8 JH |
7922 | if (numtype && !(numtype & IS_NUMBER_INFINITY)) { |
7923 | /* Need to try really hard to see if it's an integer. | |
7924 | 9.22337203685478e+18 is an integer. | |
7925 | but "9.22337203685478e+18" + 0 is UV=9223372036854779904 | |
7926 | so $a="9.22337203685478e+18"; $a+0; $a++ | |
7927 | needs to be the same as $a="9.22337203685478e+18"; $a++ | |
7928 | or we go insane. */ | |
d460ef45 | 7929 | |
28e5dec8 JH |
7930 | (void) sv_2iv(sv); |
7931 | if (SvIOK(sv)) | |
7932 | goto oops_its_int; | |
7933 | ||
7934 | /* sv_2iv *should* have made this an NV */ | |
7935 | if (flags & SVp_NOK) { | |
7936 | (void)SvNOK_only(sv); | |
9d6ce603 | 7937 | SvNV_set(sv, SvNVX(sv) + 1.0); |
28e5dec8 JH |
7938 | return; |
7939 | } | |
7940 | /* I don't think we can get here. Maybe I should assert this | |
7941 | And if we do get here I suspect that sv_setnv will croak. NWC | |
7942 | Fall through. */ | |
7943 | #if defined(USE_LONG_DOUBLE) | |
7944 | 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 | 7945 | SvPVX_const(sv), SvIVX(sv), SvNVX(sv))); |
28e5dec8 | 7946 | #else |
1779d84d | 7947 | 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 | 7948 | SvPVX_const(sv), SvIVX(sv), SvNVX(sv))); |
28e5dec8 JH |
7949 | #endif |
7950 | } | |
7951 | #endif /* PERL_PRESERVE_IVUV */ | |
3f7c398e | 7952 | sv_setnv(sv,Atof(SvPVX_const(sv)) + 1.0); |
79072805 LW |
7953 | return; |
7954 | } | |
7955 | d--; | |
3f7c398e | 7956 | while (d >= SvPVX_const(sv)) { |
79072805 LW |
7957 | if (isDIGIT(*d)) { |
7958 | if (++*d <= '9') | |
7959 | return; | |
7960 | *(d--) = '0'; | |
7961 | } | |
7962 | else { | |
9d116dd7 JH |
7963 | #ifdef EBCDIC |
7964 | /* MKS: The original code here died if letters weren't consecutive. | |
7965 | * at least it didn't have to worry about non-C locales. The | |
7966 | * new code assumes that ('z'-'a')==('Z'-'A'), letters are | |
1c846c1f | 7967 | * arranged in order (although not consecutively) and that only |
9d116dd7 JH |
7968 | * [A-Za-z] are accepted by isALPHA in the C locale. |
7969 | */ | |
7970 | if (*d != 'z' && *d != 'Z') { | |
7971 | do { ++*d; } while (!isALPHA(*d)); | |
7972 | return; | |
7973 | } | |
7974 | *(d--) -= 'z' - 'a'; | |
7975 | #else | |
79072805 LW |
7976 | ++*d; |
7977 | if (isALPHA(*d)) | |
7978 | return; | |
7979 | *(d--) -= 'z' - 'a' + 1; | |
9d116dd7 | 7980 | #endif |
79072805 LW |
7981 | } |
7982 | } | |
7983 | /* oh,oh, the number grew */ | |
7984 | SvGROW(sv, SvCUR(sv) + 2); | |
b162af07 | 7985 | SvCUR_set(sv, SvCUR(sv) + 1); |
3f7c398e | 7986 | for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX_const(sv); d--) |
79072805 LW |
7987 | *d = d[-1]; |
7988 | if (isDIGIT(d[1])) | |
7989 | *d = '1'; | |
7990 | else | |
7991 | *d = d[1]; | |
7992 | } | |
7993 | ||
954c1994 GS |
7994 | /* |
7995 | =for apidoc sv_dec | |
7996 | ||
645c22ef | 7997 | Auto-decrement of the value in the SV, doing string to numeric conversion |
6f1401dc | 7998 | if necessary. Handles 'get' magic and operator overloading. |
954c1994 GS |
7999 | |
8000 | =cut | |
8001 | */ | |
8002 | ||
79072805 | 8003 | void |
ac1e9476 | 8004 | Perl_sv_dec(pTHX_ register SV *const sv) |
79072805 | 8005 | { |
97aff369 | 8006 | dVAR; |
6f1401dc DM |
8007 | if (!sv) |
8008 | return; | |
8009 | SvGETMAGIC(sv); | |
8010 | sv_dec_nomg(sv); | |
8011 | } | |
8012 | ||
8013 | /* | |
8014 | =for apidoc sv_dec_nomg | |
8015 | ||
8016 | Auto-decrement of the value in the SV, doing string to numeric conversion | |
8017 | if necessary. Handles operator overloading. Skips handling 'get' magic. | |
8018 | ||
8019 | =cut | |
8020 | */ | |
8021 | ||
8022 | void | |
8023 | Perl_sv_dec_nomg(pTHX_ register SV *const sv) | |
8024 | { | |
8025 | dVAR; | |
463ee0b2 LW |
8026 | int flags; |
8027 | ||
79072805 LW |
8028 | if (!sv) |
8029 | return; | |
ed6116ce | 8030 | if (SvTHINKFIRST(sv)) { |
765f542d NC |
8031 | if (SvIsCOW(sv)) |
8032 | sv_force_normal_flags(sv, 0); | |
0f15f207 | 8033 | if (SvREADONLY(sv)) { |
923e4eb5 | 8034 | if (IN_PERL_RUNTIME) |
6ad8f254 | 8035 | Perl_croak_no_modify(aTHX); |
0f15f207 | 8036 | } |
a0d0e21e | 8037 | if (SvROK(sv)) { |
b5be31e9 | 8038 | IV i; |
31d632c3 | 8039 | if (SvAMAGIC(sv) && AMG_CALLunary(sv, dec_amg)) |
9e7bc3e8 | 8040 | return; |
56431972 | 8041 | i = PTR2IV(SvRV(sv)); |
b5be31e9 SM |
8042 | sv_unref(sv); |
8043 | sv_setiv(sv, i); | |
a0d0e21e | 8044 | } |
ed6116ce | 8045 | } |
28e5dec8 JH |
8046 | /* Unlike sv_inc we don't have to worry about string-never-numbers |
8047 | and keeping them magic. But we mustn't warn on punting */ | |
8990e307 | 8048 | flags = SvFLAGS(sv); |
28e5dec8 JH |
8049 | if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) { |
8050 | /* It's publicly an integer, or privately an integer-not-float */ | |
59d8ce62 | 8051 | #ifdef PERL_PRESERVE_IVUV |
28e5dec8 | 8052 | oops_its_int: |
59d8ce62 | 8053 | #endif |
25da4f38 IZ |
8054 | if (SvIsUV(sv)) { |
8055 | if (SvUVX(sv) == 0) { | |
8056 | (void)SvIOK_only(sv); | |
45977657 | 8057 | SvIV_set(sv, -1); |
25da4f38 IZ |
8058 | } |
8059 | else { | |
8060 | (void)SvIOK_only_UV(sv); | |
f4eee32f | 8061 | SvUV_set(sv, SvUVX(sv) - 1); |
1c846c1f | 8062 | } |
25da4f38 | 8063 | } else { |
b88df990 NC |
8064 | if (SvIVX(sv) == IV_MIN) { |
8065 | sv_setnv(sv, (NV)IV_MIN); | |
8066 | goto oops_its_num; | |
8067 | } | |
25da4f38 IZ |
8068 | else { |
8069 | (void)SvIOK_only(sv); | |
45977657 | 8070 | SvIV_set(sv, SvIVX(sv) - 1); |
1c846c1f | 8071 | } |
55497cff | 8072 | } |
8073 | return; | |
8074 | } | |
28e5dec8 | 8075 | if (flags & SVp_NOK) { |
b88df990 NC |
8076 | oops_its_num: |
8077 | { | |
8078 | const NV was = SvNVX(sv); | |
b68c599a | 8079 | if (NV_OVERFLOWS_INTEGERS_AT && |
a2a5de95 NC |
8080 | was <= -NV_OVERFLOWS_INTEGERS_AT) { |
8081 | Perl_ck_warner(aTHX_ packWARN(WARN_IMPRECISION), | |
8082 | "Lost precision when decrementing %" NVff " by 1", | |
8083 | was); | |
b88df990 NC |
8084 | } |
8085 | (void)SvNOK_only(sv); | |
b68c599a | 8086 | SvNV_set(sv, was - 1.0); |
b88df990 NC |
8087 | return; |
8088 | } | |
28e5dec8 | 8089 | } |
8990e307 | 8090 | if (!(flags & SVp_POK)) { |
ef088171 NC |
8091 | if ((flags & SVTYPEMASK) < SVt_PVIV) |
8092 | sv_upgrade(sv, ((flags & SVTYPEMASK) > SVt_IV) ? SVt_PVIV : SVt_IV); | |
8093 | SvIV_set(sv, -1); | |
8094 | (void)SvIOK_only(sv); | |
79072805 LW |
8095 | return; |
8096 | } | |
28e5dec8 JH |
8097 | #ifdef PERL_PRESERVE_IVUV |
8098 | { | |
504618e9 | 8099 | const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), NULL); |
28e5dec8 JH |
8100 | if (numtype && !(numtype & IS_NUMBER_INFINITY)) { |
8101 | /* Need to try really hard to see if it's an integer. | |
8102 | 9.22337203685478e+18 is an integer. | |
8103 | but "9.22337203685478e+18" + 0 is UV=9223372036854779904 | |
8104 | so $a="9.22337203685478e+18"; $a+0; $a-- | |
8105 | needs to be the same as $a="9.22337203685478e+18"; $a-- | |
8106 | or we go insane. */ | |
d460ef45 | 8107 | |
28e5dec8 JH |
8108 | (void) sv_2iv(sv); |
8109 | if (SvIOK(sv)) | |
8110 | goto oops_its_int; | |
8111 | ||
8112 | /* sv_2iv *should* have made this an NV */ | |
8113 | if (flags & SVp_NOK) { | |
8114 | (void)SvNOK_only(sv); | |
9d6ce603 | 8115 | SvNV_set(sv, SvNVX(sv) - 1.0); |
28e5dec8 JH |
8116 | return; |
8117 | } | |
8118 | /* I don't think we can get here. Maybe I should assert this | |
8119 | And if we do get here I suspect that sv_setnv will croak. NWC | |
8120 | Fall through. */ | |
8121 | #if defined(USE_LONG_DOUBLE) | |
8122 | 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 | 8123 | SvPVX_const(sv), SvIVX(sv), SvNVX(sv))); |
28e5dec8 | 8124 | #else |
1779d84d | 8125 | 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 | 8126 | SvPVX_const(sv), SvIVX(sv), SvNVX(sv))); |
28e5dec8 JH |
8127 | #endif |
8128 | } | |
8129 | } | |
8130 | #endif /* PERL_PRESERVE_IVUV */ | |
3f7c398e | 8131 | sv_setnv(sv,Atof(SvPVX_const(sv)) - 1.0); /* punt */ |
79072805 LW |
8132 | } |
8133 | ||
81041c50 YO |
8134 | /* this define is used to eliminate a chunk of duplicated but shared logic |
8135 | * it has the suffix __SV_C to signal that it isnt API, and isnt meant to be | |
8136 | * used anywhere but here - yves | |
8137 | */ | |
8138 | #define PUSH_EXTEND_MORTAL__SV_C(AnSv) \ | |
8139 | STMT_START { \ | |
8140 | EXTEND_MORTAL(1); \ | |
8141 | PL_tmps_stack[++PL_tmps_ix] = (AnSv); \ | |
8142 | } STMT_END | |
8143 | ||
954c1994 GS |
8144 | /* |
8145 | =for apidoc sv_mortalcopy | |
8146 | ||
645c22ef | 8147 | Creates a new SV which is a copy of the original SV (using C<sv_setsv>). |
d4236ebc DM |
8148 | The new SV is marked as mortal. It will be destroyed "soon", either by an |
8149 | explicit call to FREETMPS, or by an implicit call at places such as | |
8150 | statement boundaries. See also C<sv_newmortal> and C<sv_2mortal>. | |
954c1994 GS |
8151 | |
8152 | =cut | |
8153 | */ | |
8154 | ||
79072805 LW |
8155 | /* Make a string that will exist for the duration of the expression |
8156 | * evaluation. Actually, it may have to last longer than that, but | |
8157 | * hopefully we won't free it until it has been assigned to a | |
8158 | * permanent location. */ | |
8159 | ||
8160 | SV * | |
ac1e9476 | 8161 | Perl_sv_mortalcopy(pTHX_ SV *const oldstr) |
79072805 | 8162 | { |
97aff369 | 8163 | dVAR; |
463ee0b2 | 8164 | register SV *sv; |
b881518d | 8165 | |
4561caa4 | 8166 | new_SV(sv); |
79072805 | 8167 | sv_setsv(sv,oldstr); |
81041c50 | 8168 | PUSH_EXTEND_MORTAL__SV_C(sv); |
8990e307 LW |
8169 | SvTEMP_on(sv); |
8170 | return sv; | |
8171 | } | |
8172 | ||
954c1994 GS |
8173 | /* |
8174 | =for apidoc sv_newmortal | |
8175 | ||
645c22ef | 8176 | Creates a new null SV which is mortal. The reference count of the SV is |
d4236ebc DM |
8177 | set to 1. It will be destroyed "soon", either by an explicit call to |
8178 | FREETMPS, or by an implicit call at places such as statement boundaries. | |
8179 | See also C<sv_mortalcopy> and C<sv_2mortal>. | |
954c1994 GS |
8180 | |
8181 | =cut | |
8182 | */ | |
8183 | ||
8990e307 | 8184 | SV * |
864dbfa3 | 8185 | Perl_sv_newmortal(pTHX) |
8990e307 | 8186 | { |
97aff369 | 8187 | dVAR; |
8990e307 LW |
8188 | register SV *sv; |
8189 | ||
4561caa4 | 8190 | new_SV(sv); |
8990e307 | 8191 | SvFLAGS(sv) = SVs_TEMP; |
81041c50 | 8192 | PUSH_EXTEND_MORTAL__SV_C(sv); |
79072805 LW |
8193 | return sv; |
8194 | } | |
8195 | ||
59cd0e26 NC |
8196 | |
8197 | /* | |
8198 | =for apidoc newSVpvn_flags | |
8199 | ||
8200 | Creates a new SV and copies a string into it. The reference count for the | |
8201 | SV is set to 1. Note that if C<len> is zero, Perl will create a zero length | |
8202 | string. You are responsible for ensuring that the source string is at least | |
8203 | C<len> bytes long. If the C<s> argument is NULL the new SV will be undefined. | |
8204 | Currently the only flag bits accepted are C<SVf_UTF8> and C<SVs_TEMP>. | |
d9f0b464 | 8205 | If C<SVs_TEMP> is set, then C<sv_2mortal()> is called on the result before |
c790c9b6 KW |
8206 | returning. If C<SVf_UTF8> is set, C<s> is considered to be in UTF-8 and the |
8207 | C<SVf_UTF8> flag will be set on the new SV. | |
59cd0e26 NC |
8208 | C<newSVpvn_utf8()> is a convenience wrapper for this function, defined as |
8209 | ||
8210 | #define newSVpvn_utf8(s, len, u) \ | |
8211 | newSVpvn_flags((s), (len), (u) ? SVf_UTF8 : 0) | |
8212 | ||
8213 | =cut | |
8214 | */ | |
8215 | ||
8216 | SV * | |
23f13727 | 8217 | Perl_newSVpvn_flags(pTHX_ const char *const s, const STRLEN len, const U32 flags) |
59cd0e26 NC |
8218 | { |
8219 | dVAR; | |
8220 | register SV *sv; | |
8221 | ||
8222 | /* All the flags we don't support must be zero. | |
8223 | And we're new code so I'm going to assert this from the start. */ | |
8224 | assert(!(flags & ~(SVf_UTF8|SVs_TEMP))); | |
8225 | new_SV(sv); | |
8226 | sv_setpvn(sv,s,len); | |
d21488d7 YO |
8227 | |
8228 | /* This code used to a sv_2mortal(), however we now unroll the call to sv_2mortal() | |
486ec47a | 8229 | * and do what it does ourselves here. |
d21488d7 YO |
8230 | * Since we have asserted that flags can only have the SVf_UTF8 and/or SVs_TEMP flags |
8231 | * set above we can use it to enable the sv flags directly (bypassing SvTEMP_on), which | |
8232 | * in turn means we dont need to mask out the SVf_UTF8 flag below, which means that we | |
486ec47a | 8233 | * eliminate quite a few steps than it looks - Yves (explaining patch by gfx) |
d21488d7 YO |
8234 | */ |
8235 | ||
6dfeccca GF |
8236 | SvFLAGS(sv) |= flags; |
8237 | ||
8238 | if(flags & SVs_TEMP){ | |
81041c50 | 8239 | PUSH_EXTEND_MORTAL__SV_C(sv); |
6dfeccca GF |
8240 | } |
8241 | ||
8242 | return sv; | |
59cd0e26 NC |
8243 | } |
8244 | ||
954c1994 GS |
8245 | /* |
8246 | =for apidoc sv_2mortal | |
8247 | ||
d4236ebc DM |
8248 | Marks an existing SV as mortal. The SV will be destroyed "soon", either |
8249 | by an explicit call to FREETMPS, or by an implicit call at places such as | |
37d2ac18 NC |
8250 | statement boundaries. SvTEMP() is turned on which means that the SV's |
8251 | string buffer can be "stolen" if this SV is copied. See also C<sv_newmortal> | |
8252 | and C<sv_mortalcopy>. | |
954c1994 GS |
8253 | |
8254 | =cut | |
8255 | */ | |
8256 | ||
79072805 | 8257 | SV * |
23f13727 | 8258 | Perl_sv_2mortal(pTHX_ register SV *const sv) |
79072805 | 8259 | { |
27da23d5 | 8260 | dVAR; |
79072805 | 8261 | if (!sv) |
7a5b473e | 8262 | return NULL; |
d689ffdd | 8263 | if (SvREADONLY(sv) && SvIMMORTAL(sv)) |
11162842 | 8264 | return sv; |
81041c50 | 8265 | PUSH_EXTEND_MORTAL__SV_C(sv); |
8990e307 | 8266 | SvTEMP_on(sv); |
79072805 LW |
8267 | return sv; |
8268 | } | |
8269 | ||
954c1994 GS |
8270 | /* |
8271 | =for apidoc newSVpv | |
8272 | ||
8273 | Creates a new SV and copies a string into it. The reference count for the | |
8274 | SV is set to 1. If C<len> is zero, Perl will compute the length using | |
8275 | strlen(). For efficiency, consider using C<newSVpvn> instead. | |
8276 | ||
8277 | =cut | |
8278 | */ | |
8279 | ||
79072805 | 8280 | SV * |
23f13727 | 8281 | Perl_newSVpv(pTHX_ const char *const s, const STRLEN len) |
79072805 | 8282 | { |
97aff369 | 8283 | dVAR; |
463ee0b2 | 8284 | register SV *sv; |
79072805 | 8285 | |
4561caa4 | 8286 | new_SV(sv); |
ddfa59c7 | 8287 | sv_setpvn(sv, s, len || s == NULL ? len : strlen(s)); |
79072805 LW |
8288 | return sv; |
8289 | } | |
8290 | ||
954c1994 GS |
8291 | /* |
8292 | =for apidoc newSVpvn | |
8293 | ||
8294 | Creates a new SV and copies a string into it. The reference count for the | |
1c846c1f | 8295 | SV is set to 1. Note that if C<len> is zero, Perl will create a zero length |
954c1994 | 8296 | string. You are responsible for ensuring that the source string is at least |
9e09f5f2 | 8297 | C<len> bytes long. If the C<s> argument is NULL the new SV will be undefined. |
954c1994 GS |
8298 | |
8299 | =cut | |
8300 | */ | |
8301 | ||
9da1e3b5 | 8302 | SV * |
23f13727 | 8303 | Perl_newSVpvn(pTHX_ const char *const s, const STRLEN len) |
9da1e3b5 | 8304 | { |
97aff369 | 8305 | dVAR; |
9da1e3b5 MUN |
8306 | register SV *sv; |
8307 | ||
8308 | new_SV(sv); | |
9da1e3b5 MUN |
8309 | sv_setpvn(sv,s,len); |
8310 | return sv; | |
8311 | } | |
8312 | ||
740cce10 | 8313 | /* |
926f8064 | 8314 | =for apidoc newSVhek |
bd08039b NC |
8315 | |
8316 | Creates a new SV from the hash key structure. It will generate scalars that | |
5aaec2b4 NC |
8317 | point to the shared string table where possible. Returns a new (undefined) |
8318 | SV if the hek is NULL. | |
bd08039b NC |
8319 | |
8320 | =cut | |
8321 | */ | |
8322 | ||
8323 | SV * | |
23f13727 | 8324 | Perl_newSVhek(pTHX_ const HEK *const hek) |
bd08039b | 8325 | { |
97aff369 | 8326 | dVAR; |
5aaec2b4 NC |
8327 | if (!hek) { |
8328 | SV *sv; | |
8329 | ||
8330 | new_SV(sv); | |
8331 | return sv; | |
8332 | } | |
8333 | ||
bd08039b NC |
8334 | if (HEK_LEN(hek) == HEf_SVKEY) { |
8335 | return newSVsv(*(SV**)HEK_KEY(hek)); | |
8336 | } else { | |
8337 | const int flags = HEK_FLAGS(hek); | |
8338 | if (flags & HVhek_WASUTF8) { | |
8339 | /* Trouble :-) | |
8340 | Andreas would like keys he put in as utf8 to come back as utf8 | |
8341 | */ | |
8342 | STRLEN utf8_len = HEK_LEN(hek); | |
678febd7 NC |
8343 | SV * const sv = newSV_type(SVt_PV); |
8344 | char *as_utf8 = (char *)bytes_to_utf8 ((U8*)HEK_KEY(hek), &utf8_len); | |
8345 | /* bytes_to_utf8() allocates a new string, which we can repurpose: */ | |
8346 | sv_usepvn_flags(sv, as_utf8, utf8_len, SV_HAS_TRAILING_NUL); | |
bd08039b | 8347 | SvUTF8_on (sv); |
bd08039b | 8348 | return sv; |
45e34800 | 8349 | } else if (flags & (HVhek_REHASH|HVhek_UNSHARED)) { |
bd08039b NC |
8350 | /* We don't have a pointer to the hv, so we have to replicate the |
8351 | flag into every HEK. This hv is using custom a hasing | |
8352 | algorithm. Hence we can't return a shared string scalar, as | |
8353 | that would contain the (wrong) hash value, and might get passed | |
45e34800 NC |
8354 | into an hv routine with a regular hash. |
8355 | Similarly, a hash that isn't using shared hash keys has to have | |
8356 | the flag in every key so that we know not to try to call | |
8357 | share_hek_kek on it. */ | |
bd08039b | 8358 | |
b64e5050 | 8359 | SV * const sv = newSVpvn (HEK_KEY(hek), HEK_LEN(hek)); |
bd08039b NC |
8360 | if (HEK_UTF8(hek)) |
8361 | SvUTF8_on (sv); | |
8362 | return sv; | |
8363 | } | |
8364 | /* This will be overwhelminly the most common case. */ | |
409dfe77 NC |
8365 | { |
8366 | /* Inline most of newSVpvn_share(), because share_hek_hek() is far | |
8367 | more efficient than sharepvn(). */ | |
8368 | SV *sv; | |
8369 | ||
8370 | new_SV(sv); | |
8371 | sv_upgrade(sv, SVt_PV); | |
8372 | SvPV_set(sv, (char *)HEK_KEY(share_hek_hek(hek))); | |
8373 | SvCUR_set(sv, HEK_LEN(hek)); | |
8374 | SvLEN_set(sv, 0); | |
8375 | SvREADONLY_on(sv); | |
8376 | SvFAKE_on(sv); | |
8377 | SvPOK_on(sv); | |
8378 | if (HEK_UTF8(hek)) | |
8379 | SvUTF8_on(sv); | |
8380 | return sv; | |
8381 | } | |
bd08039b NC |
8382 | } |
8383 | } | |
8384 | ||
1c846c1f NIS |
8385 | /* |
8386 | =for apidoc newSVpvn_share | |
8387 | ||
3f7c398e | 8388 | Creates a new SV with its SvPVX_const pointing to a shared string in the string |
645c22ef | 8389 | table. If the string does not already exist in the table, it is created |
758fcfc1 VP |
8390 | first. Turns on READONLY and FAKE. If the C<hash> parameter is non-zero, that |
8391 | value is used; otherwise the hash is computed. The string's hash can be later | |
8392 | be retrieved from the SV with the C<SvSHARED_HASH()> macro. The idea here is | |
8393 | that as the string table is used for shared hash keys these strings will have | |
8394 | SvPVX_const == HeKEY and hash lookup will avoid string compare. | |
1c846c1f NIS |
8395 | |
8396 | =cut | |
8397 | */ | |
8398 | ||
8399 | SV * | |
c3654f1a | 8400 | Perl_newSVpvn_share(pTHX_ const char *src, I32 len, U32 hash) |
1c846c1f | 8401 | { |
97aff369 | 8402 | dVAR; |
1c846c1f | 8403 | register SV *sv; |
c3654f1a | 8404 | bool is_utf8 = FALSE; |
a51caccf NC |
8405 | const char *const orig_src = src; |
8406 | ||
c3654f1a | 8407 | if (len < 0) { |
77caf834 | 8408 | STRLEN tmplen = -len; |
c3654f1a | 8409 | is_utf8 = TRUE; |
75a54232 | 8410 | /* See the note in hv.c:hv_fetch() --jhi */ |
e1ec3a88 | 8411 | src = (char*)bytes_from_utf8((const U8*)src, &tmplen, &is_utf8); |
75a54232 JH |
8412 | len = tmplen; |
8413 | } | |
1c846c1f | 8414 | if (!hash) |
5afd6d42 | 8415 | PERL_HASH(hash, src, len); |
1c846c1f | 8416 | new_SV(sv); |
f46ee248 NC |
8417 | /* The logic for this is inlined in S_mro_get_linear_isa_dfs(), so if it |
8418 | changes here, update it there too. */ | |
bdd68bc3 | 8419 | sv_upgrade(sv, SVt_PV); |
f880fe2f | 8420 | SvPV_set(sv, sharepvn(src, is_utf8?-len:len, hash)); |
b162af07 | 8421 | SvCUR_set(sv, len); |
b162af07 | 8422 | SvLEN_set(sv, 0); |
1c846c1f NIS |
8423 | SvREADONLY_on(sv); |
8424 | SvFAKE_on(sv); | |
8425 | SvPOK_on(sv); | |
c3654f1a IH |
8426 | if (is_utf8) |
8427 | SvUTF8_on(sv); | |
a51caccf NC |
8428 | if (src != orig_src) |
8429 | Safefree(src); | |
1c846c1f NIS |
8430 | return sv; |
8431 | } | |
8432 | ||
9dcc53ea Z |
8433 | /* |
8434 | =for apidoc newSVpv_share | |
8435 | ||
8436 | Like C<newSVpvn_share>, but takes a nul-terminated string instead of a | |
8437 | string/length pair. | |
8438 | ||
8439 | =cut | |
8440 | */ | |
8441 | ||
8442 | SV * | |
8443 | Perl_newSVpv_share(pTHX_ const char *src, U32 hash) | |
8444 | { | |
8445 | return newSVpvn_share(src, strlen(src), hash); | |
8446 | } | |
645c22ef | 8447 | |
cea2e8a9 | 8448 | #if defined(PERL_IMPLICIT_CONTEXT) |
645c22ef DM |
8449 | |
8450 | /* pTHX_ magic can't cope with varargs, so this is a no-context | |
8451 | * version of the main function, (which may itself be aliased to us). | |
8452 | * Don't access this version directly. | |
8453 | */ | |
8454 | ||
46fc3d4c | 8455 | SV * |
23f13727 | 8456 | Perl_newSVpvf_nocontext(const char *const pat, ...) |
46fc3d4c | 8457 | { |
cea2e8a9 | 8458 | dTHX; |
46fc3d4c | 8459 | register SV *sv; |
8460 | va_list args; | |
7918f24d NC |
8461 | |
8462 | PERL_ARGS_ASSERT_NEWSVPVF_NOCONTEXT; | |
8463 | ||
46fc3d4c | 8464 | va_start(args, pat); |
c5be433b | 8465 | sv = vnewSVpvf(pat, &args); |
46fc3d4c | 8466 | va_end(args); |
8467 | return sv; | |
8468 | } | |
cea2e8a9 | 8469 | #endif |
46fc3d4c | 8470 | |
954c1994 GS |
8471 | /* |
8472 | =for apidoc newSVpvf | |
8473 | ||
645c22ef | 8474 | Creates a new SV and initializes it with the string formatted like |
954c1994 GS |
8475 | C<sprintf>. |
8476 | ||
8477 | =cut | |
8478 | */ | |
8479 | ||
cea2e8a9 | 8480 | SV * |
23f13727 | 8481 | Perl_newSVpvf(pTHX_ const char *const pat, ...) |
cea2e8a9 GS |
8482 | { |
8483 | register SV *sv; | |
8484 | va_list args; | |
7918f24d NC |
8485 | |
8486 | PERL_ARGS_ASSERT_NEWSVPVF; | |
8487 | ||
cea2e8a9 | 8488 | va_start(args, pat); |
c5be433b | 8489 | sv = vnewSVpvf(pat, &args); |
cea2e8a9 GS |
8490 | va_end(args); |
8491 | return sv; | |
8492 | } | |
46fc3d4c | 8493 | |
645c22ef DM |
8494 | /* backend for newSVpvf() and newSVpvf_nocontext() */ |
8495 | ||
79072805 | 8496 | SV * |
23f13727 | 8497 | Perl_vnewSVpvf(pTHX_ const char *const pat, va_list *const args) |
c5be433b | 8498 | { |
97aff369 | 8499 | dVAR; |
c5be433b | 8500 | register SV *sv; |
7918f24d NC |
8501 | |
8502 | PERL_ARGS_ASSERT_VNEWSVPVF; | |
8503 | ||
c5be433b | 8504 | new_SV(sv); |
4608196e | 8505 | sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL); |
c5be433b GS |
8506 | return sv; |
8507 | } | |
8508 | ||
954c1994 GS |
8509 | /* |
8510 | =for apidoc newSVnv | |
8511 | ||
8512 | Creates a new SV and copies a floating point value into it. | |
8513 | The reference count for the SV is set to 1. | |
8514 | ||
8515 | =cut | |
8516 | */ | |
8517 | ||
c5be433b | 8518 | SV * |
23f13727 | 8519 | Perl_newSVnv(pTHX_ const NV n) |
79072805 | 8520 | { |
97aff369 | 8521 | dVAR; |
463ee0b2 | 8522 | register SV *sv; |
79072805 | 8523 | |
4561caa4 | 8524 | new_SV(sv); |
79072805 LW |
8525 | sv_setnv(sv,n); |
8526 | return sv; | |
8527 | } | |
8528 | ||
954c1994 GS |
8529 | /* |
8530 | =for apidoc newSViv | |
8531 | ||
8532 | Creates a new SV and copies an integer into it. The reference count for the | |
8533 | SV is set to 1. | |
8534 | ||
8535 | =cut | |
8536 | */ | |
8537 | ||
79072805 | 8538 | SV * |
23f13727 | 8539 | Perl_newSViv(pTHX_ const IV i) |
79072805 | 8540 | { |
97aff369 | 8541 | dVAR; |
463ee0b2 | 8542 | register SV *sv; |
79072805 | 8543 | |
4561caa4 | 8544 | new_SV(sv); |
79072805 LW |
8545 | sv_setiv(sv,i); |
8546 | return sv; | |
8547 | } | |
8548 | ||
954c1994 | 8549 | /* |
1a3327fb JH |
8550 | =for apidoc newSVuv |
8551 | ||
8552 | Creates a new SV and copies an unsigned integer into it. | |
8553 | The reference count for the SV is set to 1. | |
8554 | ||
8555 | =cut | |
8556 | */ | |
8557 | ||
8558 | SV * | |
23f13727 | 8559 | Perl_newSVuv(pTHX_ const UV u) |
1a3327fb | 8560 | { |
97aff369 | 8561 | dVAR; |
1a3327fb JH |
8562 | register SV *sv; |
8563 | ||
8564 | new_SV(sv); | |
8565 | sv_setuv(sv,u); | |
8566 | return sv; | |
8567 | } | |
8568 | ||
8569 | /* | |
b9f83d2f NC |
8570 | =for apidoc newSV_type |
8571 | ||
c41f7ed2 | 8572 | Creates a new SV, of the type specified. The reference count for the new SV |
b9f83d2f NC |
8573 | is set to 1. |
8574 | ||
8575 | =cut | |
8576 | */ | |
8577 | ||
8578 | SV * | |
fe9845cc | 8579 | Perl_newSV_type(pTHX_ const svtype type) |
b9f83d2f NC |
8580 | { |
8581 | register SV *sv; | |
8582 | ||
8583 | new_SV(sv); | |
8584 | sv_upgrade(sv, type); | |
8585 | return sv; | |
8586 | } | |
8587 | ||
8588 | /* | |
954c1994 GS |
8589 | =for apidoc newRV_noinc |
8590 | ||
8591 | Creates an RV wrapper for an SV. The reference count for the original | |
8592 | SV is B<not> incremented. | |
8593 | ||
8594 | =cut | |
8595 | */ | |
8596 | ||
2304df62 | 8597 | SV * |
23f13727 | 8598 | Perl_newRV_noinc(pTHX_ SV *const tmpRef) |
2304df62 | 8599 | { |
97aff369 | 8600 | dVAR; |
4df7f6af | 8601 | register SV *sv = newSV_type(SVt_IV); |
7918f24d NC |
8602 | |
8603 | PERL_ARGS_ASSERT_NEWRV_NOINC; | |
8604 | ||
76e3520e | 8605 | SvTEMP_off(tmpRef); |
b162af07 | 8606 | SvRV_set(sv, tmpRef); |
2304df62 | 8607 | SvROK_on(sv); |
2304df62 AD |
8608 | return sv; |
8609 | } | |
8610 | ||
ff276b08 | 8611 | /* newRV_inc is the official function name to use now. |
645c22ef DM |
8612 | * newRV_inc is in fact #defined to newRV in sv.h |
8613 | */ | |
8614 | ||
5f05dabc | 8615 | SV * |
23f13727 | 8616 | Perl_newRV(pTHX_ SV *const sv) |
5f05dabc | 8617 | { |
97aff369 | 8618 | dVAR; |
7918f24d NC |
8619 | |
8620 | PERL_ARGS_ASSERT_NEWRV; | |
8621 | ||
7f466ec7 | 8622 | return newRV_noinc(SvREFCNT_inc_simple_NN(sv)); |
5f05dabc | 8623 | } |
5f05dabc | 8624 | |
954c1994 GS |
8625 | /* |
8626 | =for apidoc newSVsv | |
8627 | ||
8628 | Creates a new SV which is an exact duplicate of the original SV. | |
645c22ef | 8629 | (Uses C<sv_setsv>). |
954c1994 GS |
8630 | |
8631 | =cut | |
8632 | */ | |
8633 | ||
79072805 | 8634 | SV * |
23f13727 | 8635 | Perl_newSVsv(pTHX_ register SV *const old) |
79072805 | 8636 | { |
97aff369 | 8637 | dVAR; |
463ee0b2 | 8638 | register SV *sv; |
79072805 LW |
8639 | |
8640 | if (!old) | |
7a5b473e | 8641 | return NULL; |
8990e307 | 8642 | if (SvTYPE(old) == SVTYPEMASK) { |
9b387841 | 8643 | Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), "semi-panic: attempt to dup freed string"); |
a0714e2c | 8644 | return NULL; |
79072805 | 8645 | } |
4561caa4 | 8646 | new_SV(sv); |
e90aabeb NC |
8647 | /* SV_GMAGIC is the default for sv_setv() |
8648 | SV_NOSTEAL prevents TEMP buffers being, well, stolen, and saves games | |
8649 | with SvTEMP_off and SvTEMP_on round a call to sv_setsv. */ | |
8650 | sv_setsv_flags(sv, old, SV_GMAGIC | SV_NOSTEAL); | |
463ee0b2 | 8651 | return sv; |
79072805 LW |
8652 | } |
8653 | ||
645c22ef DM |
8654 | /* |
8655 | =for apidoc sv_reset | |
8656 | ||
8657 | Underlying implementation for the C<reset> Perl function. | |
8658 | Note that the perl-level function is vaguely deprecated. | |
8659 | ||
8660 | =cut | |
8661 | */ | |
8662 | ||
79072805 | 8663 | void |
23f13727 | 8664 | Perl_sv_reset(pTHX_ register const char *s, HV *const stash) |
79072805 | 8665 | { |
27da23d5 | 8666 | dVAR; |
4802d5d7 | 8667 | char todo[PERL_UCHAR_MAX+1]; |
79072805 | 8668 | |
7918f24d NC |
8669 | PERL_ARGS_ASSERT_SV_RESET; |
8670 | ||
49d8d3a1 MB |
8671 | if (!stash) |
8672 | return; | |
8673 | ||
79072805 | 8674 | if (!*s) { /* reset ?? searches */ |
daba3364 | 8675 | MAGIC * const mg = mg_find((const SV *)stash, PERL_MAGIC_symtab); |
8d2f4536 | 8676 | if (mg) { |
c2b1997a NC |
8677 | const U32 count = mg->mg_len / sizeof(PMOP**); |
8678 | PMOP **pmp = (PMOP**) mg->mg_ptr; | |
8679 | PMOP *const *const end = pmp + count; | |
8680 | ||
8681 | while (pmp < end) { | |
c737faaf | 8682 | #ifdef USE_ITHREADS |
c2b1997a | 8683 | SvREADONLY_off(PL_regex_pad[(*pmp)->op_pmoffset]); |
c737faaf | 8684 | #else |
c2b1997a | 8685 | (*pmp)->op_pmflags &= ~PMf_USED; |
c737faaf | 8686 | #endif |
c2b1997a | 8687 | ++pmp; |
8d2f4536 | 8688 | } |
79072805 LW |
8689 | } |
8690 | return; | |
8691 | } | |
8692 | ||
8693 | /* reset variables */ | |
8694 | ||
8695 | if (!HvARRAY(stash)) | |
8696 | return; | |
463ee0b2 LW |
8697 | |
8698 | Zero(todo, 256, char); | |
79072805 | 8699 | while (*s) { |
b464bac0 AL |
8700 | I32 max; |
8701 | I32 i = (unsigned char)*s; | |
79072805 LW |
8702 | if (s[1] == '-') { |
8703 | s += 2; | |
8704 | } | |
4802d5d7 | 8705 | max = (unsigned char)*s++; |
79072805 | 8706 | for ( ; i <= max; i++) { |
463ee0b2 LW |
8707 | todo[i] = 1; |
8708 | } | |
a0d0e21e | 8709 | for (i = 0; i <= (I32) HvMAX(stash); i++) { |
b464bac0 | 8710 | HE *entry; |
79072805 | 8711 | for (entry = HvARRAY(stash)[i]; |
9e35f4b3 GS |
8712 | entry; |
8713 | entry = HeNEXT(entry)) | |
8714 | { | |
b464bac0 AL |
8715 | register GV *gv; |
8716 | register SV *sv; | |
8717 | ||
1edc1566 | 8718 | if (!todo[(U8)*HeKEY(entry)]) |
463ee0b2 | 8719 | continue; |
159b6efe | 8720 | gv = MUTABLE_GV(HeVAL(entry)); |
79072805 | 8721 | sv = GvSV(gv); |
e203899d NC |
8722 | if (sv) { |
8723 | if (SvTHINKFIRST(sv)) { | |
8724 | if (!SvREADONLY(sv) && SvROK(sv)) | |
8725 | sv_unref(sv); | |
8726 | /* XXX Is this continue a bug? Why should THINKFIRST | |
8727 | exempt us from resetting arrays and hashes? */ | |
8728 | continue; | |
8729 | } | |
8730 | SvOK_off(sv); | |
8731 | if (SvTYPE(sv) >= SVt_PV) { | |
8732 | SvCUR_set(sv, 0); | |
bd61b366 | 8733 | if (SvPVX_const(sv) != NULL) |
e203899d NC |
8734 | *SvPVX(sv) = '\0'; |
8735 | SvTAINT(sv); | |
8736 | } | |
79072805 LW |
8737 | } |
8738 | if (GvAV(gv)) { | |
8739 | av_clear(GvAV(gv)); | |
8740 | } | |
bfcb3514 | 8741 | if (GvHV(gv) && !HvNAME_get(GvHV(gv))) { |
b0269e46 AB |
8742 | #if defined(VMS) |
8743 | Perl_die(aTHX_ "Can't reset %%ENV on this system"); | |
8744 | #else /* ! VMS */ | |
463ee0b2 | 8745 | hv_clear(GvHV(gv)); |
b0269e46 AB |
8746 | # if defined(USE_ENVIRON_ARRAY) |
8747 | if (gv == PL_envgv) | |
8748 | my_clearenv(); | |
8749 | # endif /* USE_ENVIRON_ARRAY */ | |
8750 | #endif /* VMS */ | |
79072805 LW |
8751 | } |
8752 | } | |
8753 | } | |
8754 | } | |
8755 | } | |
8756 | ||
645c22ef DM |
8757 | /* |
8758 | =for apidoc sv_2io | |
8759 | ||
8760 | Using various gambits, try to get an IO from an SV: the IO slot if its a | |
8761 | GV; or the recursive result if we're an RV; or the IO slot of the symbol | |
8762 | named after the PV if we're a string. | |
8763 | ||
8764 | =cut | |
8765 | */ | |
8766 | ||
46fc3d4c | 8767 | IO* |
23f13727 | 8768 | Perl_sv_2io(pTHX_ SV *const sv) |
46fc3d4c | 8769 | { |
8770 | IO* io; | |
8771 | GV* gv; | |
8772 | ||
7918f24d NC |
8773 | PERL_ARGS_ASSERT_SV_2IO; |
8774 | ||
46fc3d4c | 8775 | switch (SvTYPE(sv)) { |
8776 | case SVt_PVIO: | |
a45c7426 | 8777 | io = MUTABLE_IO(sv); |
46fc3d4c | 8778 | break; |
8779 | case SVt_PVGV: | |
13be902c | 8780 | case SVt_PVLV: |
6e592b3a | 8781 | if (isGV_with_GP(sv)) { |
159b6efe | 8782 | gv = MUTABLE_GV(sv); |
6e592b3a BM |
8783 | io = GvIO(gv); |
8784 | if (!io) | |
8785 | Perl_croak(aTHX_ "Bad filehandle: %s", GvNAME(gv)); | |
8786 | break; | |
8787 | } | |
8788 | /* FALL THROUGH */ | |
46fc3d4c | 8789 | default: |
8790 | if (!SvOK(sv)) | |
cea2e8a9 | 8791 | Perl_croak(aTHX_ PL_no_usym, "filehandle"); |
46fc3d4c | 8792 | if (SvROK(sv)) |
8793 | return sv_2io(SvRV(sv)); | |
f776e3cd | 8794 | gv = gv_fetchsv(sv, 0, SVt_PVIO); |
46fc3d4c | 8795 | if (gv) |
8796 | io = GvIO(gv); | |
8797 | else | |
8798 | io = 0; | |
8799 | if (!io) | |
be2597df | 8800 | Perl_croak(aTHX_ "Bad filehandle: %"SVf, SVfARG(sv)); |
46fc3d4c | 8801 | break; |
8802 | } | |
8803 | return io; | |
8804 | } | |
8805 | ||
645c22ef DM |
8806 | /* |
8807 | =for apidoc sv_2cv | |
8808 | ||
8809 | Using various gambits, try to get a CV from an SV; in addition, try if | |
8810 | possible to set C<*st> and C<*gvp> to the stash and GV associated with it. | |
8e324704 | 8811 | The flags in C<lref> are passed to gv_fetchsv. |
645c22ef DM |
8812 | |
8813 | =cut | |
8814 | */ | |
8815 | ||
79072805 | 8816 | CV * |
23f13727 | 8817 | Perl_sv_2cv(pTHX_ SV *sv, HV **const st, GV **const gvp, const I32 lref) |
79072805 | 8818 | { |
27da23d5 | 8819 | dVAR; |
a0714e2c | 8820 | GV *gv = NULL; |
601f1833 | 8821 | CV *cv = NULL; |
79072805 | 8822 | |
7918f24d NC |
8823 | PERL_ARGS_ASSERT_SV_2CV; |
8824 | ||
85dec29a NC |
8825 | if (!sv) { |
8826 | *st = NULL; | |
8827 | *gvp = NULL; | |
8828 | return NULL; | |
8829 | } | |
79072805 | 8830 | switch (SvTYPE(sv)) { |
79072805 LW |
8831 | case SVt_PVCV: |
8832 | *st = CvSTASH(sv); | |
a0714e2c | 8833 | *gvp = NULL; |
ea726b52 | 8834 | return MUTABLE_CV(sv); |
79072805 LW |
8835 | case SVt_PVHV: |
8836 | case SVt_PVAV: | |
ef58ba18 | 8837 | *st = NULL; |
a0714e2c | 8838 | *gvp = NULL; |
601f1833 | 8839 | return NULL; |
8990e307 | 8840 | case SVt_PVGV: |
6e592b3a | 8841 | if (isGV_with_GP(sv)) { |
159b6efe | 8842 | gv = MUTABLE_GV(sv); |
6e592b3a BM |
8843 | *gvp = gv; |
8844 | *st = GvESTASH(gv); | |
8845 | goto fix_gv; | |
8846 | } | |
8847 | /* FALL THROUGH */ | |
8990e307 | 8848 | |
79072805 | 8849 | default: |
a0d0e21e | 8850 | if (SvROK(sv)) { |
c4f3bd1e | 8851 | SvGETMAGIC(sv); |
93d7320b DM |
8852 | if (SvAMAGIC(sv)) |
8853 | sv = amagic_deref_call(sv, to_cv_amg); | |
8897dcaa NC |
8854 | /* At this point I'd like to do SPAGAIN, but really I need to |
8855 | force it upon my callers. Hmmm. This is a mess... */ | |
f5284f61 | 8856 | |
62f274bf GS |
8857 | sv = SvRV(sv); |
8858 | if (SvTYPE(sv) == SVt_PVCV) { | |
ea726b52 | 8859 | cv = MUTABLE_CV(sv); |
a0714e2c | 8860 | *gvp = NULL; |
62f274bf GS |
8861 | *st = CvSTASH(cv); |
8862 | return cv; | |
8863 | } | |
6e592b3a | 8864 | else if(isGV_with_GP(sv)) |
159b6efe | 8865 | gv = MUTABLE_GV(sv); |
62f274bf | 8866 | else |
cea2e8a9 | 8867 | Perl_croak(aTHX_ "Not a subroutine reference"); |
a0d0e21e | 8868 | } |
6e592b3a | 8869 | else if (isGV_with_GP(sv)) { |
9d0f7ed7 | 8870 | SvGETMAGIC(sv); |
159b6efe | 8871 | gv = MUTABLE_GV(sv); |
9d0f7ed7 | 8872 | } |
79072805 | 8873 | else |
9d0f7ed7 | 8874 | gv = gv_fetchsv(sv, lref, SVt_PVCV); /* Calls get magic */ |
79072805 | 8875 | *gvp = gv; |
ef58ba18 NC |
8876 | if (!gv) { |
8877 | *st = NULL; | |
601f1833 | 8878 | return NULL; |
ef58ba18 | 8879 | } |
e26df76a | 8880 | /* Some flags to gv_fetchsv mean don't really create the GV */ |
6e592b3a | 8881 | if (!isGV_with_GP(gv)) { |
e26df76a NC |
8882 | *st = NULL; |
8883 | return NULL; | |
8884 | } | |
79072805 | 8885 | *st = GvESTASH(gv); |
8990e307 | 8886 | fix_gv: |
8ebc5c01 | 8887 | if (lref && !GvCVu(gv)) { |
4633a7c4 | 8888 | SV *tmpsv; |
748a9306 | 8889 | ENTER; |
561b68a9 | 8890 | tmpsv = newSV(0); |
bd61b366 | 8891 | gv_efullname3(tmpsv, gv, NULL); |
f6ec51f7 GS |
8892 | /* XXX this is probably not what they think they're getting. |
8893 | * It has the same effect as "sub name;", i.e. just a forward | |
8894 | * declaration! */ | |
774d564b | 8895 | newSUB(start_subparse(FALSE, 0), |
4633a7c4 | 8896 | newSVOP(OP_CONST, 0, tmpsv), |
5f66b61c | 8897 | NULL, NULL); |
748a9306 | 8898 | LEAVE; |
8ebc5c01 | 8899 | if (!GvCVu(gv)) |
35c1215d | 8900 | Perl_croak(aTHX_ "Unable to create sub named \"%"SVf"\"", |
4052d21c | 8901 | SVfARG(SvOK(sv) ? sv : &PL_sv_no)); |
8990e307 | 8902 | } |
8ebc5c01 | 8903 | return GvCVu(gv); |
79072805 LW |
8904 | } |
8905 | } | |
8906 | ||
c461cf8f JH |
8907 | /* |
8908 | =for apidoc sv_true | |
8909 | ||
8910 | Returns true if the SV has a true value by Perl's rules. | |
645c22ef DM |
8911 | Use the C<SvTRUE> macro instead, which may call C<sv_true()> or may |
8912 | instead use an in-line version. | |
c461cf8f JH |
8913 | |
8914 | =cut | |
8915 | */ | |
8916 | ||
79072805 | 8917 | I32 |
23f13727 | 8918 | Perl_sv_true(pTHX_ register SV *const sv) |
79072805 | 8919 | { |
8990e307 LW |
8920 | if (!sv) |
8921 | return 0; | |
79072805 | 8922 | if (SvPOK(sv)) { |
823a54a3 AL |
8923 | register const XPV* const tXpv = (XPV*)SvANY(sv); |
8924 | if (tXpv && | |
c2f1de04 | 8925 | (tXpv->xpv_cur > 1 || |
339049b0 | 8926 | (tXpv->xpv_cur && *sv->sv_u.svu_pv != '0'))) |
79072805 LW |
8927 | return 1; |
8928 | else | |
8929 | return 0; | |
8930 | } | |
8931 | else { | |
8932 | if (SvIOK(sv)) | |
463ee0b2 | 8933 | return SvIVX(sv) != 0; |
79072805 LW |
8934 | else { |
8935 | if (SvNOK(sv)) | |
463ee0b2 | 8936 | return SvNVX(sv) != 0.0; |
79072805 | 8937 | else |
463ee0b2 | 8938 | return sv_2bool(sv); |
79072805 LW |
8939 | } |
8940 | } | |
8941 | } | |
79072805 | 8942 | |
645c22ef | 8943 | /* |
c461cf8f JH |
8944 | =for apidoc sv_pvn_force |
8945 | ||
8946 | Get a sensible string out of the SV somehow. | |
645c22ef DM |
8947 | A private implementation of the C<SvPV_force> macro for compilers which |
8948 | can't cope with complex macro expressions. Always use the macro instead. | |
c461cf8f | 8949 | |
8d6d96c1 HS |
8950 | =for apidoc sv_pvn_force_flags |
8951 | ||
8952 | Get a sensible string out of the SV somehow. | |
8953 | If C<flags> has C<SV_GMAGIC> bit set, will C<mg_get> on C<sv> if | |
8954 | appropriate, else not. C<sv_pvn_force> and C<sv_pvn_force_nomg> are | |
8955 | implemented in terms of this function. | |
645c22ef DM |
8956 | You normally want to use the various wrapper macros instead: see |
8957 | C<SvPV_force> and C<SvPV_force_nomg> | |
8d6d96c1 HS |
8958 | |
8959 | =cut | |
8960 | */ | |
8961 | ||
8962 | char * | |
12964ddd | 8963 | Perl_sv_pvn_force_flags(pTHX_ SV *const sv, STRLEN *const lp, const I32 flags) |
8d6d96c1 | 8964 | { |
97aff369 | 8965 | dVAR; |
7918f24d NC |
8966 | |
8967 | PERL_ARGS_ASSERT_SV_PVN_FORCE_FLAGS; | |
8968 | ||
6fc92669 | 8969 | if (SvTHINKFIRST(sv) && !SvROK(sv)) |
765f542d | 8970 | sv_force_normal_flags(sv, 0); |
1c846c1f | 8971 | |
a0d0e21e | 8972 | if (SvPOK(sv)) { |
13c5b33c NC |
8973 | if (lp) |
8974 | *lp = SvCUR(sv); | |
a0d0e21e LW |
8975 | } |
8976 | else { | |
a3b680e6 | 8977 | char *s; |
13c5b33c NC |
8978 | STRLEN len; |
8979 | ||
4d84ee25 | 8980 | if (SvREADONLY(sv) && !(flags & SV_MUTABLE_RETURN)) { |
b64e5050 | 8981 | const char * const ref = sv_reftype(sv,0); |
4d84ee25 NC |
8982 | if (PL_op) |
8983 | Perl_croak(aTHX_ "Can't coerce readonly %s to string in %s", | |
94bbb3f4 | 8984 | ref, OP_DESC(PL_op)); |
4d84ee25 | 8985 | else |
b64e5050 | 8986 | Perl_croak(aTHX_ "Can't coerce readonly %s to string", ref); |
4d84ee25 | 8987 | } |
1f257c95 NC |
8988 | if ((SvTYPE(sv) > SVt_PVLV && SvTYPE(sv) != SVt_PVFM) |
8989 | || isGV_with_GP(sv)) | |
cea2e8a9 | 8990 | Perl_croak(aTHX_ "Can't coerce %s to string in %s", sv_reftype(sv,0), |
94bbb3f4 | 8991 | OP_DESC(PL_op)); |
b64e5050 | 8992 | s = sv_2pv_flags(sv, &len, flags); |
13c5b33c NC |
8993 | if (lp) |
8994 | *lp = len; | |
8995 | ||
3f7c398e | 8996 | if (s != SvPVX_const(sv)) { /* Almost, but not quite, sv_setpvn() */ |
a0d0e21e LW |
8997 | if (SvROK(sv)) |
8998 | sv_unref(sv); | |
862a34c6 | 8999 | SvUPGRADE(sv, SVt_PV); /* Never FALSE */ |
a0d0e21e | 9000 | SvGROW(sv, len + 1); |
706aa1c9 | 9001 | Move(s,SvPVX(sv),len,char); |
a0d0e21e | 9002 | SvCUR_set(sv, len); |
97a130b8 | 9003 | SvPVX(sv)[len] = '\0'; |
a0d0e21e LW |
9004 | } |
9005 | if (!SvPOK(sv)) { | |
9006 | SvPOK_on(sv); /* validate pointer */ | |
9007 | SvTAINT(sv); | |
1d7c1841 | 9008 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n", |
3f7c398e | 9009 | PTR2UV(sv),SvPVX_const(sv))); |
a0d0e21e LW |
9010 | } |
9011 | } | |
4d84ee25 | 9012 | return SvPVX_mutable(sv); |
a0d0e21e LW |
9013 | } |
9014 | ||
645c22ef | 9015 | /* |
645c22ef DM |
9016 | =for apidoc sv_pvbyten_force |
9017 | ||
0feed65a | 9018 | The backend for the C<SvPVbytex_force> macro. Always use the macro instead. |
645c22ef DM |
9019 | |
9020 | =cut | |
9021 | */ | |
9022 | ||
7340a771 | 9023 | char * |
12964ddd | 9024 | Perl_sv_pvbyten_force(pTHX_ SV *const sv, STRLEN *const lp) |
7340a771 | 9025 | { |
7918f24d NC |
9026 | PERL_ARGS_ASSERT_SV_PVBYTEN_FORCE; |
9027 | ||
46ec2f14 | 9028 | sv_pvn_force(sv,lp); |
ffebcc3e | 9029 | sv_utf8_downgrade(sv,0); |
46ec2f14 TS |
9030 | *lp = SvCUR(sv); |
9031 | return SvPVX(sv); | |
7340a771 GS |
9032 | } |
9033 | ||
645c22ef | 9034 | /* |
c461cf8f JH |
9035 | =for apidoc sv_pvutf8n_force |
9036 | ||
0feed65a | 9037 | The backend for the C<SvPVutf8x_force> macro. Always use the macro instead. |
c461cf8f JH |
9038 | |
9039 | =cut | |
9040 | */ | |
9041 | ||
7340a771 | 9042 | char * |
12964ddd | 9043 | Perl_sv_pvutf8n_force(pTHX_ SV *const sv, STRLEN *const lp) |
7340a771 | 9044 | { |
7918f24d NC |
9045 | PERL_ARGS_ASSERT_SV_PVUTF8N_FORCE; |
9046 | ||
46ec2f14 | 9047 | sv_pvn_force(sv,lp); |
560a288e | 9048 | sv_utf8_upgrade(sv); |
46ec2f14 TS |
9049 | *lp = SvCUR(sv); |
9050 | return SvPVX(sv); | |
7340a771 GS |
9051 | } |
9052 | ||
c461cf8f | 9053 | /* |
cba0b539 | 9054 | =for apidoc sv_reftype |
05c0d6bb | 9055 | |
cba0b539 | 9056 | Returns a string describing what the SV is a reference to. |
c461cf8f JH |
9057 | |
9058 | =cut | |
9059 | */ | |
9060 | ||
2b388283 | 9061 | const char * |
cba0b539 | 9062 | Perl_sv_reftype(pTHX_ const SV *const sv, const int ob) |
a0d0e21e | 9063 | { |
cba0b539 | 9064 | PERL_ARGS_ASSERT_SV_REFTYPE; |
7918f24d | 9065 | |
cba0b539 | 9066 | /* The fact that I don't need to downcast to char * everywhere, only in ?: |
07409e01 | 9067 | inside return suggests a const propagation bug in g++. */ |
c86bf373 | 9068 | if (ob && SvOBJECT(sv)) { |
1b6737cc | 9069 | char * const name = HvNAME_get(SvSTASH(sv)); |
cba0b539 | 9070 | return name ? name : (char *) "__ANON__"; |
c86bf373 | 9071 | } |
a0d0e21e LW |
9072 | else { |
9073 | switch (SvTYPE(sv)) { | |
9074 | case SVt_NULL: | |
9075 | case SVt_IV: | |
9076 | case SVt_NV: | |
a0d0e21e LW |
9077 | case SVt_PV: |
9078 | case SVt_PVIV: | |
9079 | case SVt_PVNV: | |
9080 | case SVt_PVMG: | |
1cb0ed9b | 9081 | if (SvVOK(sv)) |
cba0b539 | 9082 | return "VSTRING"; |
a0d0e21e | 9083 | if (SvROK(sv)) |
cba0b539 | 9084 | return "REF"; |
a0d0e21e | 9085 | else |
cba0b539 FR |
9086 | return "SCALAR"; |
9087 | ||
9088 | case SVt_PVLV: return (char *) (SvROK(sv) ? "REF" | |
9089 | /* tied lvalues should appear to be | |
486ec47a | 9090 | * scalars for backwards compatibility */ |
cba0b539 FR |
9091 | : (LvTYPE(sv) == 't' || LvTYPE(sv) == 'T') |
9092 | ? "SCALAR" : "LVALUE"); | |
9093 | case SVt_PVAV: return "ARRAY"; | |
9094 | case SVt_PVHV: return "HASH"; | |
9095 | case SVt_PVCV: return "CODE"; | |
9096 | case SVt_PVGV: return (char *) (isGV_with_GP(sv) | |
9097 | ? "GLOB" : "SCALAR"); | |
9098 | case SVt_PVFM: return "FORMAT"; | |
9099 | case SVt_PVIO: return "IO"; | |
9100 | case SVt_BIND: return "BIND"; | |
9101 | case SVt_REGEXP: return "REGEXP"; | |
9102 | default: return "UNKNOWN"; | |
a0d0e21e LW |
9103 | } |
9104 | } | |
9105 | } | |
9106 | ||
954c1994 GS |
9107 | /* |
9108 | =for apidoc sv_isobject | |
9109 | ||
9110 | Returns a boolean indicating whether the SV is an RV pointing to a blessed | |
9111 | object. If the SV is not an RV, or if the object is not blessed, then this | |
9112 | will return false. | |
9113 | ||
9114 | =cut | |
9115 | */ | |
9116 | ||
463ee0b2 | 9117 | int |
864dbfa3 | 9118 | Perl_sv_isobject(pTHX_ SV *sv) |
85e6fe83 | 9119 | { |
68dc0745 | 9120 | if (!sv) |
9121 | return 0; | |
5b295bef | 9122 | SvGETMAGIC(sv); |
85e6fe83 LW |
9123 | if (!SvROK(sv)) |
9124 | return 0; | |
daba3364 | 9125 | sv = SvRV(sv); |
85e6fe83 LW |
9126 | if (!SvOBJECT(sv)) |
9127 | return 0; | |
9128 | return 1; | |
9129 | } | |
9130 | ||
954c1994 GS |
9131 | /* |
9132 | =for apidoc sv_isa | |
9133 | ||
9134 | Returns a boolean indicating whether the SV is blessed into the specified | |
9135 | class. This does not check for subtypes; use C<sv_derived_from> to verify | |
9136 | an inheritance relationship. | |
9137 | ||
9138 | =cut | |
9139 | */ | |
9140 | ||
85e6fe83 | 9141 | int |
12964ddd | 9142 | Perl_sv_isa(pTHX_ SV *sv, const char *const name) |
463ee0b2 | 9143 | { |
bfcb3514 | 9144 | const char *hvname; |
7918f24d NC |
9145 | |
9146 | PERL_ARGS_ASSERT_SV_ISA; | |
9147 | ||
68dc0745 | 9148 | if (!sv) |
9149 | return 0; | |
5b295bef | 9150 | SvGETMAGIC(sv); |
ed6116ce | 9151 | if (!SvROK(sv)) |
463ee0b2 | 9152 | return 0; |
daba3364 | 9153 | sv = SvRV(sv); |
ed6116ce | 9154 | if (!SvOBJECT(sv)) |
463ee0b2 | 9155 | return 0; |
bfcb3514 NC |
9156 | hvname = HvNAME_get(SvSTASH(sv)); |
9157 | if (!hvname) | |
e27ad1f2 | 9158 | return 0; |
463ee0b2 | 9159 | |
bfcb3514 | 9160 | return strEQ(hvname, name); |
463ee0b2 LW |
9161 | } |
9162 | ||
954c1994 GS |
9163 | /* |
9164 | =for apidoc newSVrv | |
9165 | ||
9166 | Creates a new SV for the RV, C<rv>, to point to. If C<rv> is not an RV then | |
9167 | it will be upgraded to one. If C<classname> is non-null then the new SV will | |
9168 | be blessed in the specified package. The new SV is returned and its | |
9169 | reference count is 1. | |
9170 | ||
9171 | =cut | |
9172 | */ | |
9173 | ||
463ee0b2 | 9174 | SV* |
12964ddd | 9175 | Perl_newSVrv(pTHX_ SV *const rv, const char *const classname) |
463ee0b2 | 9176 | { |
97aff369 | 9177 | dVAR; |
463ee0b2 LW |
9178 | SV *sv; |
9179 | ||
7918f24d NC |
9180 | PERL_ARGS_ASSERT_NEWSVRV; |
9181 | ||
4561caa4 | 9182 | new_SV(sv); |
51cf62d8 | 9183 | |
765f542d | 9184 | SV_CHECK_THINKFIRST_COW_DROP(rv); |
52944de8 | 9185 | (void)SvAMAGIC_off(rv); |
51cf62d8 | 9186 | |
0199fce9 | 9187 | if (SvTYPE(rv) >= SVt_PVMG) { |
a3b680e6 | 9188 | const U32 refcnt = SvREFCNT(rv); |
0199fce9 JD |
9189 | SvREFCNT(rv) = 0; |
9190 | sv_clear(rv); | |
9191 | SvFLAGS(rv) = 0; | |
9192 | SvREFCNT(rv) = refcnt; | |
0199fce9 | 9193 | |
4df7f6af | 9194 | sv_upgrade(rv, SVt_IV); |
dc5494d2 NC |
9195 | } else if (SvROK(rv)) { |
9196 | SvREFCNT_dec(SvRV(rv)); | |
43230e26 NC |
9197 | } else { |
9198 | prepare_SV_for_RV(rv); | |
0199fce9 | 9199 | } |
51cf62d8 | 9200 | |
0c34ef67 | 9201 | SvOK_off(rv); |
b162af07 | 9202 | SvRV_set(rv, sv); |
ed6116ce | 9203 | SvROK_on(rv); |
463ee0b2 | 9204 | |
a0d0e21e | 9205 | if (classname) { |
da51bb9b | 9206 | HV* const stash = gv_stashpv(classname, GV_ADD); |
a0d0e21e LW |
9207 | (void)sv_bless(rv, stash); |
9208 | } | |
9209 | return sv; | |
9210 | } | |
9211 | ||
954c1994 GS |
9212 | /* |
9213 | =for apidoc sv_setref_pv | |
9214 | ||
9215 | Copies a pointer into a new SV, optionally blessing the SV. The C<rv> | |
9216 | argument will be upgraded to an RV. That RV will be modified to point to | |
9217 | the new SV. If the C<pv> argument is NULL then C<PL_sv_undef> will be placed | |
9218 | into the SV. The C<classname> argument indicates the package for the | |
bd61b366 | 9219 | blessing. Set C<classname> to C<NULL> to avoid the blessing. The new SV |
d34c2299 | 9220 | will have a reference count of 1, and the RV will be returned. |
954c1994 GS |
9221 | |
9222 | Do not use with other Perl types such as HV, AV, SV, CV, because those | |
9223 | objects will become corrupted by the pointer copy process. | |
9224 | ||
9225 | Note that C<sv_setref_pvn> copies the string while this copies the pointer. | |
9226 | ||
9227 | =cut | |
9228 | */ | |
9229 | ||
a0d0e21e | 9230 | SV* |
12964ddd | 9231 | Perl_sv_setref_pv(pTHX_ SV *const rv, const char *const classname, void *const pv) |
a0d0e21e | 9232 | { |
97aff369 | 9233 | dVAR; |
7918f24d NC |
9234 | |
9235 | PERL_ARGS_ASSERT_SV_SETREF_PV; | |
9236 | ||
189b2af5 | 9237 | if (!pv) { |
3280af22 | 9238 | sv_setsv(rv, &PL_sv_undef); |
189b2af5 GS |
9239 | SvSETMAGIC(rv); |
9240 | } | |
a0d0e21e | 9241 | else |
56431972 | 9242 | sv_setiv(newSVrv(rv,classname), PTR2IV(pv)); |
a0d0e21e LW |
9243 | return rv; |
9244 | } | |
9245 | ||
954c1994 GS |
9246 | /* |
9247 | =for apidoc sv_setref_iv | |
9248 | ||
9249 | Copies an integer into a new SV, optionally blessing the SV. The C<rv> | |
9250 | argument will be upgraded to an RV. That RV will be modified to point to | |
9251 | the new SV. The C<classname> argument indicates the package for the | |
bd61b366 | 9252 | blessing. Set C<classname> to C<NULL> to avoid the blessing. The new SV |
d34c2299 | 9253 | will have a reference count of 1, and the RV will be returned. |
954c1994 GS |
9254 | |
9255 | =cut | |
9256 | */ | |
9257 | ||
a0d0e21e | 9258 | SV* |
12964ddd | 9259 | Perl_sv_setref_iv(pTHX_ SV *const rv, const char *const classname, const IV iv) |
a0d0e21e | 9260 | { |
7918f24d NC |
9261 | PERL_ARGS_ASSERT_SV_SETREF_IV; |
9262 | ||
a0d0e21e LW |
9263 | sv_setiv(newSVrv(rv,classname), iv); |
9264 | return rv; | |
9265 | } | |
9266 | ||
954c1994 | 9267 | /* |
e1c57cef JH |
9268 | =for apidoc sv_setref_uv |
9269 | ||
9270 | Copies an unsigned integer into a new SV, optionally blessing the SV. The C<rv> | |
9271 | argument will be upgraded to an RV. That RV will be modified to point to | |
9272 | the new SV. The C<classname> argument indicates the package for the | |
bd61b366 | 9273 | blessing. Set C<classname> to C<NULL> to avoid the blessing. The new SV |
d34c2299 | 9274 | will have a reference count of 1, and the RV will be returned. |
e1c57cef JH |
9275 | |
9276 | =cut | |
9277 | */ | |
9278 | ||
9279 | SV* | |
12964ddd | 9280 | Perl_sv_setref_uv(pTHX_ SV *const rv, const char *const classname, const UV uv) |
e1c57cef | 9281 | { |
7918f24d NC |
9282 | PERL_ARGS_ASSERT_SV_SETREF_UV; |
9283 | ||
e1c57cef JH |
9284 | sv_setuv(newSVrv(rv,classname), uv); |
9285 | return rv; | |
9286 | } | |
9287 | ||
9288 | /* | |
954c1994 GS |
9289 | =for apidoc sv_setref_nv |
9290 | ||
9291 | Copies a double into a new SV, optionally blessing the SV. The C<rv> | |
9292 | argument will be upgraded to an RV. That RV will be modified to point to | |
9293 | the new SV. The C<classname> argument indicates the package for the | |
bd61b366 | 9294 | blessing. Set C<classname> to C<NULL> to avoid the blessing. The new SV |
d34c2299 | 9295 | will have a reference count of 1, and the RV will be returned. |
954c1994 GS |
9296 | |
9297 | =cut | |
9298 | */ | |
9299 | ||
a0d0e21e | 9300 | SV* |
12964ddd | 9301 | Perl_sv_setref_nv(pTHX_ SV *const rv, const char *const classname, const NV nv) |
a0d0e21e | 9302 | { |
7918f24d NC |
9303 | PERL_ARGS_ASSERT_SV_SETREF_NV; |
9304 | ||
a0d0e21e LW |
9305 | sv_setnv(newSVrv(rv,classname), nv); |
9306 | return rv; | |
9307 | } | |
463ee0b2 | 9308 | |
954c1994 GS |
9309 | /* |
9310 | =for apidoc sv_setref_pvn | |
9311 | ||
9312 | Copies a string into a new SV, optionally blessing the SV. The length of the | |
9313 | string must be specified with C<n>. The C<rv> argument will be upgraded to | |
9314 | an RV. That RV will be modified to point to the new SV. The C<classname> | |
9315 | argument indicates the package for the blessing. Set C<classname> to | |
bd61b366 | 9316 | C<NULL> to avoid the blessing. The new SV will have a reference count |
d34c2299 | 9317 | of 1, and the RV will be returned. |
954c1994 GS |
9318 | |
9319 | Note that C<sv_setref_pv> copies the pointer while this copies the string. | |
9320 | ||
9321 | =cut | |
9322 | */ | |
9323 | ||
a0d0e21e | 9324 | SV* |
12964ddd SS |
9325 | Perl_sv_setref_pvn(pTHX_ SV *const rv, const char *const classname, |
9326 | const char *const pv, const STRLEN n) | |
a0d0e21e | 9327 | { |
7918f24d NC |
9328 | PERL_ARGS_ASSERT_SV_SETREF_PVN; |
9329 | ||
a0d0e21e | 9330 | sv_setpvn(newSVrv(rv,classname), pv, n); |
463ee0b2 LW |
9331 | return rv; |
9332 | } | |
9333 | ||
954c1994 GS |
9334 | /* |
9335 | =for apidoc sv_bless | |
9336 | ||
9337 | Blesses an SV into a specified package. The SV must be an RV. The package | |
9338 | must be designated by its stash (see C<gv_stashpv()>). The reference count | |
9339 | of the SV is unaffected. | |
9340 | ||
9341 | =cut | |
9342 | */ | |
9343 | ||
a0d0e21e | 9344 | SV* |
12964ddd | 9345 | Perl_sv_bless(pTHX_ SV *const sv, HV *const stash) |
a0d0e21e | 9346 | { |
97aff369 | 9347 | dVAR; |
76e3520e | 9348 | SV *tmpRef; |
7918f24d NC |
9349 | |
9350 | PERL_ARGS_ASSERT_SV_BLESS; | |
9351 | ||
a0d0e21e | 9352 | if (!SvROK(sv)) |
cea2e8a9 | 9353 | Perl_croak(aTHX_ "Can't bless non-reference value"); |
76e3520e GS |
9354 | tmpRef = SvRV(sv); |
9355 | if (SvFLAGS(tmpRef) & (SVs_OBJECT|SVf_READONLY)) { | |
e0744413 NC |
9356 | if (SvIsCOW(tmpRef)) |
9357 | sv_force_normal_flags(tmpRef, 0); | |
76e3520e | 9358 | if (SvREADONLY(tmpRef)) |
6ad8f254 | 9359 | Perl_croak_no_modify(aTHX); |
76e3520e GS |
9360 | if (SvOBJECT(tmpRef)) { |
9361 | if (SvTYPE(tmpRef) != SVt_PVIO) | |
3280af22 | 9362 | --PL_sv_objcount; |
76e3520e | 9363 | SvREFCNT_dec(SvSTASH(tmpRef)); |
2e3febc6 | 9364 | } |
a0d0e21e | 9365 | } |
76e3520e GS |
9366 | SvOBJECT_on(tmpRef); |
9367 | if (SvTYPE(tmpRef) != SVt_PVIO) | |
3280af22 | 9368 | ++PL_sv_objcount; |
862a34c6 | 9369 | SvUPGRADE(tmpRef, SVt_PVMG); |
85fbaab2 | 9370 | SvSTASH_set(tmpRef, MUTABLE_HV(SvREFCNT_inc_simple(stash))); |
a0d0e21e | 9371 | |
2e3febc6 CS |
9372 | if (Gv_AMG(stash)) |
9373 | SvAMAGIC_on(sv); | |
9374 | else | |
52944de8 | 9375 | (void)SvAMAGIC_off(sv); |
a0d0e21e | 9376 | |
1edbfb88 AB |
9377 | if(SvSMAGICAL(tmpRef)) |
9378 | if(mg_find(tmpRef, PERL_MAGIC_ext) || mg_find(tmpRef, PERL_MAGIC_uvar)) | |
9379 | mg_set(tmpRef); | |
9380 | ||
9381 | ||
ecdeb87c | 9382 | |
a0d0e21e LW |
9383 | return sv; |
9384 | } | |
9385 | ||
13be902c FC |
9386 | /* Downgrades a PVGV to a PVMG. If it’s actually a PVLV, we leave the type |
9387 | * as it is after unglobbing it. | |
645c22ef DM |
9388 | */ |
9389 | ||
76e3520e | 9390 | STATIC void |
89e38212 | 9391 | S_sv_unglob(pTHX_ SV *const sv) |
a0d0e21e | 9392 | { |
97aff369 | 9393 | dVAR; |
850fabdf | 9394 | void *xpvmg; |
dd69841b | 9395 | HV *stash; |
b37c2d43 | 9396 | SV * const temp = sv_newmortal(); |
850fabdf | 9397 | |
7918f24d NC |
9398 | PERL_ARGS_ASSERT_SV_UNGLOB; |
9399 | ||
13be902c | 9400 | assert(SvTYPE(sv) == SVt_PVGV || SvTYPE(sv) == SVt_PVLV); |
a0d0e21e | 9401 | SvFAKE_off(sv); |
159b6efe | 9402 | gv_efullname3(temp, MUTABLE_GV(sv), "*"); |
180488f8 | 9403 | |
f7877b28 | 9404 | if (GvGP(sv)) { |
159b6efe NC |
9405 | if(GvCVu((const GV *)sv) && (stash = GvSTASH(MUTABLE_GV(sv))) |
9406 | && HvNAME_get(stash)) | |
dd69841b | 9407 | mro_method_changed_in(stash); |
159b6efe | 9408 | gp_free(MUTABLE_GV(sv)); |
f7877b28 | 9409 | } |
e826b3c7 | 9410 | if (GvSTASH(sv)) { |
daba3364 | 9411 | sv_del_backref(MUTABLE_SV(GvSTASH(sv)), sv); |
5c284bb0 | 9412 | GvSTASH(sv) = NULL; |
e826b3c7 | 9413 | } |
a5f75d66 | 9414 | GvMULTI_off(sv); |
acda4c6a NC |
9415 | if (GvNAME_HEK(sv)) { |
9416 | unshare_hek(GvNAME_HEK(sv)); | |
9417 | } | |
2e5b91de | 9418 | isGV_with_GP_off(sv); |
850fabdf | 9419 | |
13be902c FC |
9420 | if(SvTYPE(sv) == SVt_PVGV) { |
9421 | /* need to keep SvANY(sv) in the right arena */ | |
9422 | xpvmg = new_XPVMG(); | |
9423 | StructCopy(SvANY(sv), xpvmg, XPVMG); | |
9424 | del_XPVGV(SvANY(sv)); | |
9425 | SvANY(sv) = xpvmg; | |
850fabdf | 9426 | |
13be902c FC |
9427 | SvFLAGS(sv) &= ~SVTYPEMASK; |
9428 | SvFLAGS(sv) |= SVt_PVMG; | |
9429 | } | |
180488f8 NC |
9430 | |
9431 | /* Intentionally not calling any local SET magic, as this isn't so much a | |
9432 | set operation as merely an internal storage change. */ | |
9433 | sv_setsv_flags(sv, temp, 0); | |
a0d0e21e LW |
9434 | } |
9435 | ||
954c1994 | 9436 | /* |
840a7b70 | 9437 | =for apidoc sv_unref_flags |
954c1994 GS |
9438 | |
9439 | Unsets the RV status of the SV, and decrements the reference count of | |
9440 | whatever was being referenced by the RV. This can almost be thought of | |
840a7b70 IZ |
9441 | as a reversal of C<newSVrv>. The C<cflags> argument can contain |
9442 | C<SV_IMMEDIATE_UNREF> to force the reference count to be decremented | |
9443 | (otherwise the decrementing is conditional on the reference count being | |
9444 | different from one or the reference being a readonly SV). | |
7889fe52 | 9445 | See C<SvROK_off>. |
954c1994 GS |
9446 | |
9447 | =cut | |
9448 | */ | |
9449 | ||
ed6116ce | 9450 | void |
89e38212 | 9451 | Perl_sv_unref_flags(pTHX_ SV *const ref, const U32 flags) |
ed6116ce | 9452 | { |
b64e5050 | 9453 | SV* const target = SvRV(ref); |
810b8aa5 | 9454 | |
7918f24d NC |
9455 | PERL_ARGS_ASSERT_SV_UNREF_FLAGS; |
9456 | ||
e15faf7d NC |
9457 | if (SvWEAKREF(ref)) { |
9458 | sv_del_backref(target, ref); | |
9459 | SvWEAKREF_off(ref); | |
9460 | SvRV_set(ref, NULL); | |
810b8aa5 GS |
9461 | return; |
9462 | } | |
e15faf7d NC |
9463 | SvRV_set(ref, NULL); |
9464 | SvROK_off(ref); | |
9465 | /* You can't have a || SvREADONLY(target) here, as $a = $$a, where $a was | |
04ca4930 | 9466 | assigned to as BEGIN {$a = \"Foo"} will fail. */ |
e15faf7d NC |
9467 | if (SvREFCNT(target) != 1 || (flags & SV_IMMEDIATE_UNREF)) |
9468 | SvREFCNT_dec(target); | |
840a7b70 | 9469 | else /* XXX Hack, but hard to make $a=$a->[1] work otherwise */ |
e15faf7d | 9470 | sv_2mortal(target); /* Schedule for freeing later */ |
ed6116ce | 9471 | } |
8990e307 | 9472 | |
840a7b70 | 9473 | /* |
645c22ef DM |
9474 | =for apidoc sv_untaint |
9475 | ||
9476 | Untaint an SV. Use C<SvTAINTED_off> instead. | |
9477 | =cut | |
9478 | */ | |
9479 | ||
bbce6d69 | 9480 | void |
89e38212 | 9481 | Perl_sv_untaint(pTHX_ SV *const sv) |
bbce6d69 | 9482 | { |
7918f24d NC |
9483 | PERL_ARGS_ASSERT_SV_UNTAINT; |
9484 | ||
13f57bf8 | 9485 | if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) { |
b64e5050 | 9486 | MAGIC * const mg = mg_find(sv, PERL_MAGIC_taint); |
36477c24 | 9487 | if (mg) |
565764a8 | 9488 | mg->mg_len &= ~1; |
36477c24 | 9489 | } |
bbce6d69 | 9490 | } |
9491 | ||
645c22ef DM |
9492 | /* |
9493 | =for apidoc sv_tainted | |
9494 | ||
9495 | Test an SV for taintedness. Use C<SvTAINTED> instead. | |
9496 | =cut | |
9497 | */ | |
9498 | ||
bbce6d69 | 9499 | bool |
89e38212 | 9500 | Perl_sv_tainted(pTHX_ SV *const sv) |
bbce6d69 | 9501 | { |
7918f24d NC |
9502 | PERL_ARGS_ASSERT_SV_TAINTED; |
9503 | ||
13f57bf8 | 9504 | if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) { |
823a54a3 | 9505 | const MAGIC * const mg = mg_find(sv, PERL_MAGIC_taint); |
2ddb8a4f | 9506 | if (mg && (mg->mg_len & 1) ) |
36477c24 | 9507 | return TRUE; |
9508 | } | |
9509 | return FALSE; | |
bbce6d69 | 9510 | } |
9511 | ||
09540bc3 JH |
9512 | /* |
9513 | =for apidoc sv_setpviv | |
9514 | ||
9515 | Copies an integer into the given SV, also updating its string value. | |
9516 | Does not handle 'set' magic. See C<sv_setpviv_mg>. | |
9517 | ||
9518 | =cut | |
9519 | */ | |
9520 | ||
9521 | void | |
89e38212 | 9522 | Perl_sv_setpviv(pTHX_ SV *const sv, const IV iv) |
09540bc3 JH |
9523 | { |
9524 | char buf[TYPE_CHARS(UV)]; | |
9525 | char *ebuf; | |
b64e5050 | 9526 | char * const ptr = uiv_2buf(buf, iv, 0, 0, &ebuf); |
09540bc3 | 9527 | |
7918f24d NC |
9528 | PERL_ARGS_ASSERT_SV_SETPVIV; |
9529 | ||
09540bc3 JH |
9530 | sv_setpvn(sv, ptr, ebuf - ptr); |
9531 | } | |
9532 | ||
9533 | /* | |
9534 | =for apidoc sv_setpviv_mg | |
9535 | ||
9536 | Like C<sv_setpviv>, but also handles 'set' magic. | |
9537 | ||
9538 | =cut | |
9539 | */ | |
9540 | ||
9541 | void | |
89e38212 | 9542 | Perl_sv_setpviv_mg(pTHX_ SV *const sv, const IV iv) |
09540bc3 | 9543 | { |
7918f24d NC |
9544 | PERL_ARGS_ASSERT_SV_SETPVIV_MG; |
9545 | ||
df7eb254 | 9546 | sv_setpviv(sv, iv); |
09540bc3 JH |
9547 | SvSETMAGIC(sv); |
9548 | } | |
9549 | ||
cea2e8a9 | 9550 | #if defined(PERL_IMPLICIT_CONTEXT) |
645c22ef DM |
9551 | |
9552 | /* pTHX_ magic can't cope with varargs, so this is a no-context | |
9553 | * version of the main function, (which may itself be aliased to us). | |
9554 | * Don't access this version directly. | |
9555 | */ | |
9556 | ||
cea2e8a9 | 9557 | void |
89e38212 | 9558 | Perl_sv_setpvf_nocontext(SV *const sv, const char *const pat, ...) |
cea2e8a9 GS |
9559 | { |
9560 | dTHX; | |
9561 | va_list args; | |
7918f24d NC |
9562 | |
9563 | PERL_ARGS_ASSERT_SV_SETPVF_NOCONTEXT; | |
9564 | ||
cea2e8a9 | 9565 | va_start(args, pat); |
c5be433b | 9566 | sv_vsetpvf(sv, pat, &args); |
cea2e8a9 GS |
9567 | va_end(args); |
9568 | } | |
9569 | ||
645c22ef DM |
9570 | /* pTHX_ magic can't cope with varargs, so this is a no-context |
9571 | * version of the main function, (which may itself be aliased to us). | |
9572 | * Don't access this version directly. | |
9573 | */ | |
cea2e8a9 GS |
9574 | |
9575 | void | |
89e38212 | 9576 | Perl_sv_setpvf_mg_nocontext(SV *const sv, const char *const pat, ...) |
cea2e8a9 GS |
9577 | { |
9578 | dTHX; | |
9579 | va_list args; | |
7918f24d NC |
9580 | |
9581 | PERL_ARGS_ASSERT_SV_SETPVF_MG_NOCONTEXT; | |
9582 | ||
cea2e8a9 | 9583 | va_start(args, pat); |
c5be433b | 9584 | sv_vsetpvf_mg(sv, pat, &args); |
cea2e8a9 | 9585 | va_end(args); |
cea2e8a9 GS |
9586 | } |
9587 | #endif | |
9588 | ||
954c1994 GS |
9589 | /* |
9590 | =for apidoc sv_setpvf | |
9591 | ||
bffc3d17 SH |
9592 | Works like C<sv_catpvf> but copies the text into the SV instead of |
9593 | appending it. Does not handle 'set' magic. See C<sv_setpvf_mg>. | |
954c1994 GS |
9594 | |
9595 | =cut | |
9596 | */ | |
9597 | ||
46fc3d4c | 9598 | void |
89e38212 | 9599 | Perl_sv_setpvf(pTHX_ SV *const sv, const char *const pat, ...) |
46fc3d4c | 9600 | { |
9601 | va_list args; | |
7918f24d NC |
9602 | |
9603 | PERL_ARGS_ASSERT_SV_SETPVF; | |
9604 | ||
46fc3d4c | 9605 | va_start(args, pat); |
c5be433b | 9606 | sv_vsetpvf(sv, pat, &args); |
46fc3d4c | 9607 | va_end(args); |
9608 | } | |
9609 | ||
bffc3d17 SH |
9610 | /* |
9611 | =for apidoc sv_vsetpvf | |
9612 | ||
9613 | Works like C<sv_vcatpvf> but copies the text into the SV instead of | |
9614 | appending it. Does not handle 'set' magic. See C<sv_vsetpvf_mg>. | |
9615 | ||
9616 | Usually used via its frontend C<sv_setpvf>. | |
9617 | ||
9618 | =cut | |
9619 | */ | |
645c22ef | 9620 | |
c5be433b | 9621 | void |
89e38212 | 9622 | Perl_sv_vsetpvf(pTHX_ SV *const sv, const char *const pat, va_list *const args) |
c5be433b | 9623 | { |
7918f24d NC |
9624 | PERL_ARGS_ASSERT_SV_VSETPVF; |
9625 | ||
4608196e | 9626 | sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL); |
c5be433b | 9627 | } |
ef50df4b | 9628 | |
954c1994 GS |
9629 | /* |
9630 | =for apidoc sv_setpvf_mg | |
9631 | ||
9632 | Like C<sv_setpvf>, but also handles 'set' magic. | |
9633 | ||
9634 | =cut | |
9635 | */ | |
9636 | ||
ef50df4b | 9637 | void |
89e38212 | 9638 | Perl_sv_setpvf_mg(pTHX_ SV *const sv, const char *const pat, ...) |
ef50df4b GS |
9639 | { |
9640 | va_list args; | |
7918f24d NC |
9641 | |
9642 | PERL_ARGS_ASSERT_SV_SETPVF_MG; | |
9643 | ||
ef50df4b | 9644 | va_start(args, pat); |
c5be433b | 9645 | sv_vsetpvf_mg(sv, pat, &args); |
ef50df4b | 9646 | va_end(args); |
c5be433b GS |
9647 | } |
9648 | ||
bffc3d17 SH |
9649 | /* |
9650 | =for apidoc sv_vsetpvf_mg | |
9651 | ||
9652 | Like C<sv_vsetpvf>, but also handles 'set' magic. | |
9653 | ||
9654 | Usually used via its frontend C<sv_setpvf_mg>. | |
9655 | ||
9656 | =cut | |
9657 | */ | |
645c22ef | 9658 | |
c5be433b | 9659 | void |
89e38212 | 9660 | Perl_sv_vsetpvf_mg(pTHX_ SV *const sv, const char *const pat, va_list *const args) |
c5be433b | 9661 | { |
7918f24d NC |
9662 | PERL_ARGS_ASSERT_SV_VSETPVF_MG; |
9663 | ||
4608196e | 9664 | sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL); |
ef50df4b GS |
9665 | SvSETMAGIC(sv); |
9666 | } | |
9667 | ||
cea2e8a9 | 9668 | #if defined(PERL_IMPLICIT_CONTEXT) |
645c22ef DM |
9669 | |
9670 | /* pTHX_ magic can't cope with varargs, so this is a no-context | |
9671 | * version of the main function, (which may itself be aliased to us). | |
9672 | * Don't access this version directly. | |
9673 | */ | |
9674 | ||
cea2e8a9 | 9675 | void |
89e38212 | 9676 | Perl_sv_catpvf_nocontext(SV *const sv, const char *const pat, ...) |
cea2e8a9 GS |
9677 | { |
9678 | dTHX; | |
9679 | va_list args; | |
7918f24d NC |
9680 | |
9681 | PERL_ARGS_ASSERT_SV_CATPVF_NOCONTEXT; | |
9682 | ||
cea2e8a9 | 9683 | va_start(args, pat); |
c5be433b | 9684 | sv_vcatpvf(sv, pat, &args); |
cea2e8a9 GS |
9685 | va_end(args); |
9686 | } | |
9687 | ||
645c22ef DM |
9688 | /* pTHX_ magic can't cope with varargs, so this is a no-context |
9689 | * version of the main function, (which may itself be aliased to us). | |
9690 | * Don't access this version directly. | |
9691 | */ | |
9692 | ||
cea2e8a9 | 9693 | void |
89e38212 | 9694 | Perl_sv_catpvf_mg_nocontext(SV *const sv, const char *const pat, ...) |
cea2e8a9 GS |
9695 | { |
9696 | dTHX; | |
9697 | va_list args; | |
7918f24d NC |
9698 | |
9699 | PERL_ARGS_ASSERT_SV_CATPVF_MG_NOCONTEXT; | |
9700 | ||
cea2e8a9 | 9701 | va_start(args, pat); |
c5be433b | 9702 | sv_vcatpvf_mg(sv, pat, &args); |
cea2e8a9 | 9703 | va_end(args); |
cea2e8a9 GS |
9704 | } |
9705 | #endif | |
9706 | ||
954c1994 GS |
9707 | /* |
9708 | =for apidoc sv_catpvf | |
9709 | ||
d5ce4a7c GA |
9710 | Processes its arguments like C<sprintf> and appends the formatted |
9711 | output to an SV. If the appended data contains "wide" characters | |
9712 | (including, but not limited to, SVs with a UTF-8 PV formatted with %s, | |
9713 | and characters >255 formatted with %c), the original SV might get | |
bffc3d17 | 9714 | upgraded to UTF-8. Handles 'get' magic, but not 'set' magic. See |
cdd94ca7 NC |
9715 | C<sv_catpvf_mg>. If the original SV was UTF-8, the pattern should be |
9716 | valid UTF-8; if the original SV was bytes, the pattern should be too. | |
954c1994 | 9717 | |
d5ce4a7c | 9718 | =cut */ |
954c1994 | 9719 | |
46fc3d4c | 9720 | void |
66ceb532 | 9721 | Perl_sv_catpvf(pTHX_ SV *const sv, const char *const pat, ...) |
46fc3d4c | 9722 | { |
9723 | va_list args; | |
7918f24d NC |
9724 | |
9725 | PERL_ARGS_ASSERT_SV_CATPVF; | |
9726 | ||
46fc3d4c | 9727 | va_start(args, pat); |
c5be433b | 9728 | sv_vcatpvf(sv, pat, &args); |
46fc3d4c | 9729 | va_end(args); |
9730 | } | |
9731 | ||
bffc3d17 SH |
9732 | /* |
9733 | =for apidoc sv_vcatpvf | |
9734 | ||
9735 | Processes its arguments like C<vsprintf> and appends the formatted output | |
9736 | to an SV. Does not handle 'set' magic. See C<sv_vcatpvf_mg>. | |
9737 | ||
9738 | Usually used via its frontend C<sv_catpvf>. | |
9739 | ||
9740 | =cut | |
9741 | */ | |
645c22ef | 9742 | |
ef50df4b | 9743 | void |
66ceb532 | 9744 | Perl_sv_vcatpvf(pTHX_ SV *const sv, const char *const pat, va_list *const args) |
c5be433b | 9745 | { |
7918f24d NC |
9746 | PERL_ARGS_ASSERT_SV_VCATPVF; |
9747 | ||
4608196e | 9748 | sv_vcatpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL); |
c5be433b GS |
9749 | } |
9750 | ||
954c1994 GS |
9751 | /* |
9752 | =for apidoc sv_catpvf_mg | |
9753 | ||
9754 | Like C<sv_catpvf>, but also handles 'set' magic. | |
9755 | ||
9756 | =cut | |
9757 | */ | |
9758 | ||
c5be433b | 9759 | void |
66ceb532 | 9760 | Perl_sv_catpvf_mg(pTHX_ SV *const sv, const char *const pat, ...) |
ef50df4b GS |
9761 | { |
9762 | va_list args; | |
7918f24d NC |
9763 | |
9764 | PERL_ARGS_ASSERT_SV_CATPVF_MG; | |
9765 | ||
ef50df4b | 9766 | va_start(args, pat); |
c5be433b | 9767 | sv_vcatpvf_mg(sv, pat, &args); |
ef50df4b | 9768 | va_end(args); |
c5be433b GS |
9769 | } |
9770 | ||
bffc3d17 SH |
9771 | /* |
9772 | =for apidoc sv_vcatpvf_mg | |
9773 | ||
9774 | Like C<sv_vcatpvf>, but also handles 'set' magic. | |
9775 | ||
9776 | Usually used via its frontend C<sv_catpvf_mg>. | |
9777 | ||
9778 | =cut | |
9779 | */ | |
645c22ef | 9780 | |
c5be433b | 9781 | void |
66ceb532 | 9782 | Perl_sv_vcatpvf_mg(pTHX_ SV *const sv, const char *const pat, va_list *const args) |
c5be433b | 9783 | { |
7918f24d NC |
9784 | PERL_ARGS_ASSERT_SV_VCATPVF_MG; |
9785 | ||
4608196e | 9786 | sv_vcatpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL); |
ef50df4b GS |
9787 | SvSETMAGIC(sv); |
9788 | } | |
9789 | ||
954c1994 GS |
9790 | /* |
9791 | =for apidoc sv_vsetpvfn | |
9792 | ||
bffc3d17 | 9793 | Works like C<sv_vcatpvfn> but copies the text into the SV instead of |
954c1994 GS |
9794 | appending it. |
9795 | ||
bffc3d17 | 9796 | Usually used via one of its frontends C<sv_vsetpvf> and C<sv_vsetpvf_mg>. |
645c22ef | 9797 | |
954c1994 GS |
9798 | =cut |
9799 | */ | |
9800 | ||
46fc3d4c | 9801 | void |
66ceb532 SS |
9802 | Perl_sv_vsetpvfn(pTHX_ SV *const sv, const char *const pat, const STRLEN patlen, |
9803 | va_list *const args, SV **const svargs, const I32 svmax, bool *const maybe_tainted) | |
46fc3d4c | 9804 | { |
7918f24d NC |
9805 | PERL_ARGS_ASSERT_SV_VSETPVFN; |
9806 | ||
76f68e9b | 9807 | sv_setpvs(sv, ""); |
7d5ea4e7 | 9808 | sv_vcatpvfn(sv, pat, patlen, args, svargs, svmax, maybe_tainted); |
46fc3d4c | 9809 | } |
9810 | ||
7baa4690 HS |
9811 | |
9812 | /* | |
9813 | * Warn of missing argument to sprintf, and then return a defined value | |
9814 | * to avoid inappropriate "use of uninit" warnings [perl #71000]. | |
9815 | */ | |
9816 | #define WARN_MISSING WARN_UNINITIALIZED /* Not sure we want a new category */ | |
9817 | STATIC SV* | |
81ae3cde | 9818 | S_vcatpvfn_missing_argument(pTHX) { |
7baa4690 HS |
9819 | if (ckWARN(WARN_MISSING)) { |
9820 | Perl_warner(aTHX_ packWARN(WARN_MISSING), "Missing argument in %s", | |
9821 | PL_op ? OP_DESC(PL_op) : "sv_vcatpvfn()"); | |
9822 | } | |
9823 | return &PL_sv_no; | |
9824 | } | |
9825 | ||
9826 | ||
2d00ba3b | 9827 | STATIC I32 |
66ceb532 | 9828 | S_expect_number(pTHX_ char **const pattern) |
211dfcf1 | 9829 | { |
97aff369 | 9830 | dVAR; |
211dfcf1 | 9831 | I32 var = 0; |
7918f24d NC |
9832 | |
9833 | PERL_ARGS_ASSERT_EXPECT_NUMBER; | |
9834 | ||
211dfcf1 HS |
9835 | switch (**pattern) { |
9836 | case '1': case '2': case '3': | |
9837 | case '4': case '5': case '6': | |
9838 | case '7': case '8': case '9': | |
2fba7546 GA |
9839 | var = *(*pattern)++ - '0'; |
9840 | while (isDIGIT(**pattern)) { | |
5f66b61c | 9841 | const I32 tmp = var * 10 + (*(*pattern)++ - '0'); |
2fba7546 | 9842 | if (tmp < var) |
94bbb3f4 | 9843 | Perl_croak(aTHX_ "Integer overflow in format string for %s", (PL_op ? OP_DESC(PL_op) : "sv_vcatpvfn")); |
2fba7546 GA |
9844 | var = tmp; |
9845 | } | |
211dfcf1 HS |
9846 | } |
9847 | return var; | |
9848 | } | |
211dfcf1 | 9849 | |
c445ea15 | 9850 | STATIC char * |
66ceb532 | 9851 | S_F0convert(NV nv, char *const endbuf, STRLEN *const len) |
4151a5fe | 9852 | { |
a3b680e6 | 9853 | const int neg = nv < 0; |
4151a5fe | 9854 | UV uv; |
4151a5fe | 9855 | |
7918f24d NC |
9856 | PERL_ARGS_ASSERT_F0CONVERT; |
9857 | ||
4151a5fe IZ |
9858 | if (neg) |
9859 | nv = -nv; | |
9860 | if (nv < UV_MAX) { | |
b464bac0 | 9861 | char *p = endbuf; |
4151a5fe | 9862 | nv += 0.5; |
028f8eaa | 9863 | uv = (UV)nv; |
4151a5fe IZ |
9864 | if (uv & 1 && uv == nv) |
9865 | uv--; /* Round to even */ | |
9866 | do { | |
a3b680e6 | 9867 | const unsigned dig = uv % 10; |
4151a5fe IZ |
9868 | *--p = '0' + dig; |
9869 | } while (uv /= 10); | |
9870 | if (neg) | |
9871 | *--p = '-'; | |
9872 | *len = endbuf - p; | |
9873 | return p; | |
9874 | } | |
bd61b366 | 9875 | return NULL; |
4151a5fe IZ |
9876 | } |
9877 | ||
9878 | ||
954c1994 GS |
9879 | /* |
9880 | =for apidoc sv_vcatpvfn | |
9881 | ||
9882 | Processes its arguments like C<vsprintf> and appends the formatted output | |
9883 | to an SV. Uses an array of SVs if the C style variable argument list is | |
9884 | missing (NULL). When running with taint checks enabled, indicates via | |
9885 | C<maybe_tainted> if results are untrustworthy (often due to the use of | |
9886 | locales). | |
9887 | ||
bffc3d17 | 9888 | Usually used via one of its frontends C<sv_vcatpvf> and C<sv_vcatpvf_mg>. |
645c22ef | 9889 | |
954c1994 GS |
9890 | =cut |
9891 | */ | |
9892 | ||
8896765a RB |
9893 | |
9894 | #define VECTORIZE_ARGS vecsv = va_arg(*args, SV*);\ | |
9895 | vecstr = (U8*)SvPV_const(vecsv,veclen);\ | |
9896 | vec_utf8 = DO_UTF8(vecsv); | |
9897 | ||
1ef29b0e RGS |
9898 | /* XXX maybe_tainted is never assigned to, so the doc above is lying. */ |
9899 | ||
46fc3d4c | 9900 | void |
66ceb532 SS |
9901 | Perl_sv_vcatpvfn(pTHX_ SV *const sv, const char *const pat, const STRLEN patlen, |
9902 | va_list *const args, SV **const svargs, const I32 svmax, bool *const maybe_tainted) | |
46fc3d4c | 9903 | { |
97aff369 | 9904 | dVAR; |
46fc3d4c | 9905 | char *p; |
9906 | char *q; | |
a3b680e6 | 9907 | const char *patend; |
fc36a67e | 9908 | STRLEN origlen; |
46fc3d4c | 9909 | I32 svix = 0; |
27da23d5 | 9910 | static const char nullstr[] = "(null)"; |
a0714e2c | 9911 | SV *argsv = NULL; |
b464bac0 AL |
9912 | bool has_utf8 = DO_UTF8(sv); /* has the result utf8? */ |
9913 | const bool pat_utf8 = has_utf8; /* the pattern is in utf8? */ | |
a0714e2c | 9914 | SV *nsv = NULL; |
4151a5fe IZ |
9915 | /* Times 4: a decimal digit takes more than 3 binary digits. |
9916 | * NV_DIG: mantissa takes than many decimal digits. | |
9917 | * Plus 32: Playing safe. */ | |
9918 | char ebuf[IV_DIG * 4 + NV_DIG + 32]; | |
9919 | /* large enough for "%#.#f" --chip */ | |
9920 | /* what about long double NVs? --jhi */ | |
db79b45b | 9921 | |
7918f24d | 9922 | PERL_ARGS_ASSERT_SV_VCATPVFN; |
53c1dcc0 AL |
9923 | PERL_UNUSED_ARG(maybe_tainted); |
9924 | ||
46fc3d4c | 9925 | /* no matter what, this is a string now */ |
fc36a67e | 9926 | (void)SvPV_force(sv, origlen); |
46fc3d4c | 9927 | |
8896765a | 9928 | /* special-case "", "%s", and "%-p" (SVf - see below) */ |
46fc3d4c | 9929 | if (patlen == 0) |
9930 | return; | |
0dbb1585 | 9931 | if (patlen == 2 && pat[0] == '%' && pat[1] == 's') { |
2d03de9c AL |
9932 | if (args) { |
9933 | const char * const s = va_arg(*args, char*); | |
9934 | sv_catpv(sv, s ? s : nullstr); | |
9935 | } | |
9936 | else if (svix < svmax) { | |
9937 | sv_catsv(sv, *svargs); | |
2d03de9c | 9938 | } |
5b98cd54 VP |
9939 | else |
9940 | S_vcatpvfn_missing_argument(aTHX); | |
2d03de9c | 9941 | return; |
0dbb1585 | 9942 | } |
8896765a RB |
9943 | if (args && patlen == 3 && pat[0] == '%' && |
9944 | pat[1] == '-' && pat[2] == 'p') { | |
daba3364 | 9945 | argsv = MUTABLE_SV(va_arg(*args, void*)); |
8896765a | 9946 | sv_catsv(sv, argsv); |
8896765a | 9947 | return; |
46fc3d4c | 9948 | } |
9949 | ||
1d917b39 | 9950 | #ifndef USE_LONG_DOUBLE |
4151a5fe | 9951 | /* special-case "%.<number>[gf]" */ |
7af36d83 | 9952 | if ( !args && patlen <= 5 && pat[0] == '%' && pat[1] == '.' |
4151a5fe IZ |
9953 | && (pat[patlen-1] == 'g' || pat[patlen-1] == 'f') ) { |
9954 | unsigned digits = 0; | |
9955 | const char *pp; | |
9956 | ||
9957 | pp = pat + 2; | |
9958 | while (*pp >= '0' && *pp <= '9') | |
9959 | digits = 10 * digits + (*pp++ - '0'); | |
95ea86d5 NC |
9960 | if (pp - pat == (int)patlen - 1 && svix < svmax) { |
9961 | const NV nv = SvNV(*svargs); | |
4151a5fe | 9962 | if (*pp == 'g') { |
2873255c NC |
9963 | /* Add check for digits != 0 because it seems that some |
9964 | gconverts are buggy in this case, and we don't yet have | |
9965 | a Configure test for this. */ | |
9966 | if (digits && digits < sizeof(ebuf) - NV_DIG - 10) { | |
9967 | /* 0, point, slack */ | |
2e59c212 | 9968 | Gconvert(nv, (int)digits, 0, ebuf); |
4151a5fe IZ |
9969 | sv_catpv(sv, ebuf); |
9970 | if (*ebuf) /* May return an empty string for digits==0 */ | |
9971 | return; | |
9972 | } | |
9973 | } else if (!digits) { | |
9974 | STRLEN l; | |
9975 | ||
9976 | if ((p = F0convert(nv, ebuf + sizeof ebuf, &l))) { | |
9977 | sv_catpvn(sv, p, l); | |
9978 | return; | |
9979 | } | |
9980 | } | |
9981 | } | |
9982 | } | |
1d917b39 | 9983 | #endif /* !USE_LONG_DOUBLE */ |
4151a5fe | 9984 | |
2cf2cfc6 | 9985 | if (!args && svix < svmax && DO_UTF8(*svargs)) |
205f51d8 | 9986 | has_utf8 = TRUE; |
2cf2cfc6 | 9987 | |
46fc3d4c | 9988 | patend = (char*)pat + patlen; |
9989 | for (p = (char*)pat; p < patend; p = q) { | |
9990 | bool alt = FALSE; | |
9991 | bool left = FALSE; | |
b22c7a20 | 9992 | bool vectorize = FALSE; |
211dfcf1 | 9993 | bool vectorarg = FALSE; |
2cf2cfc6 | 9994 | bool vec_utf8 = FALSE; |
46fc3d4c | 9995 | char fill = ' '; |
9996 | char plus = 0; | |
9997 | char intsize = 0; | |
9998 | STRLEN width = 0; | |
fc36a67e | 9999 | STRLEN zeros = 0; |
46fc3d4c | 10000 | bool has_precis = FALSE; |
10001 | STRLEN precis = 0; | |
c445ea15 | 10002 | const I32 osvix = svix; |
2cf2cfc6 | 10003 | bool is_utf8 = FALSE; /* is this item utf8? */ |
20f6aaab AS |
10004 | #ifdef HAS_LDBL_SPRINTF_BUG |
10005 | /* This is to try to fix a bug with irix/nonstop-ux/powerux and | |
205f51d8 | 10006 | with sfio - Allen <allens@cpan.org> */ |
20f6aaab AS |
10007 | bool fix_ldbl_sprintf_bug = FALSE; |
10008 | #endif | |
205f51d8 | 10009 | |
46fc3d4c | 10010 | char esignbuf[4]; |
89ebb4a3 | 10011 | U8 utf8buf[UTF8_MAXBYTES+1]; |
46fc3d4c | 10012 | STRLEN esignlen = 0; |
10013 | ||
bd61b366 | 10014 | const char *eptr = NULL; |
1d1ac7bc | 10015 | const char *fmtstart; |
fc36a67e | 10016 | STRLEN elen = 0; |
a0714e2c | 10017 | SV *vecsv = NULL; |
4608196e | 10018 | const U8 *vecstr = NULL; |
b22c7a20 | 10019 | STRLEN veclen = 0; |
934abaf1 | 10020 | char c = 0; |
46fc3d4c | 10021 | int i; |
9c5ffd7c | 10022 | unsigned base = 0; |
8c8eb53c RB |
10023 | IV iv = 0; |
10024 | UV uv = 0; | |
9e5b023a JH |
10025 | /* we need a long double target in case HAS_LONG_DOUBLE but |
10026 | not USE_LONG_DOUBLE | |
10027 | */ | |
35fff930 | 10028 | #if defined(HAS_LONG_DOUBLE) && LONG_DOUBLESIZE > DOUBLESIZE |
9e5b023a JH |
10029 | long double nv; |
10030 | #else | |
65202027 | 10031 | NV nv; |
9e5b023a | 10032 | #endif |
46fc3d4c | 10033 | STRLEN have; |
10034 | STRLEN need; | |
10035 | STRLEN gap; | |
7af36d83 | 10036 | const char *dotstr = "."; |
b22c7a20 | 10037 | STRLEN dotstrlen = 1; |
211dfcf1 | 10038 | I32 efix = 0; /* explicit format parameter index */ |
eb3fce90 | 10039 | I32 ewix = 0; /* explicit width index */ |
211dfcf1 HS |
10040 | I32 epix = 0; /* explicit precision index */ |
10041 | I32 evix = 0; /* explicit vector index */ | |
eb3fce90 | 10042 | bool asterisk = FALSE; |
46fc3d4c | 10043 | |
211dfcf1 | 10044 | /* echo everything up to the next format specification */ |
46fc3d4c | 10045 | for (q = p; q < patend && *q != '%'; ++q) ; |
10046 | if (q > p) { | |
db79b45b JH |
10047 | if (has_utf8 && !pat_utf8) |
10048 | sv_catpvn_utf8_upgrade(sv, p, q - p, nsv); | |
10049 | else | |
10050 | sv_catpvn(sv, p, q - p); | |
46fc3d4c | 10051 | p = q; |
10052 | } | |
10053 | if (q++ >= patend) | |
10054 | break; | |
10055 | ||
1d1ac7bc MHM |
10056 | fmtstart = q; |
10057 | ||
211dfcf1 HS |
10058 | /* |
10059 | We allow format specification elements in this order: | |
10060 | \d+\$ explicit format parameter index | |
10061 | [-+ 0#]+ flags | |
a472f209 | 10062 | v|\*(\d+\$)?v vector with optional (optionally specified) arg |
f3583277 | 10063 | 0 flag (as above): repeated to allow "v02" |
211dfcf1 HS |
10064 | \d+|\*(\d+\$)? width using optional (optionally specified) arg |
10065 | \.(\d*|\*(\d+\$)?) precision using optional (optionally specified) arg | |
10066 | [hlqLV] size | |
8896765a RB |
10067 | [%bcdefginopsuxDFOUX] format (mandatory) |
10068 | */ | |
10069 | ||
10070 | if (args) { | |
10071 | /* | |
10072 | As of perl5.9.3, printf format checking is on by default. | |
10073 | Internally, perl uses %p formats to provide an escape to | |
10074 | some extended formatting. This block deals with those | |
10075 | extensions: if it does not match, (char*)q is reset and | |
10076 | the normal format processing code is used. | |
10077 | ||
10078 | Currently defined extensions are: | |
10079 | %p include pointer address (standard) | |
10080 | %-p (SVf) include an SV (previously %_) | |
10081 | %-<num>p include an SV with precision <num> | |
8896765a RB |
10082 | %<num>p reserved for future extensions |
10083 | ||
10084 | Robin Barker 2005-07-14 | |
f46d31f2 RB |
10085 | |
10086 | %1p (VDf) removed. RMB 2007-10-19 | |
211dfcf1 | 10087 | */ |
8896765a RB |
10088 | char* r = q; |
10089 | bool sv = FALSE; | |
10090 | STRLEN n = 0; | |
10091 | if (*q == '-') | |
10092 | sv = *q++; | |
c445ea15 | 10093 | n = expect_number(&q); |
8896765a RB |
10094 | if (*q++ == 'p') { |
10095 | if (sv) { /* SVf */ | |
10096 | if (n) { | |
10097 | precis = n; | |
10098 | has_precis = TRUE; | |
10099 | } | |
daba3364 | 10100 | argsv = MUTABLE_SV(va_arg(*args, void*)); |
4ea561bc | 10101 | eptr = SvPV_const(argsv, elen); |
8896765a RB |
10102 | if (DO_UTF8(argsv)) |
10103 | is_utf8 = TRUE; | |
10104 | goto string; | |
10105 | } | |
8896765a | 10106 | else if (n) { |
9b387841 NC |
10107 | Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), |
10108 | "internal %%<num>p might conflict with future printf extensions"); | |
8896765a RB |
10109 | } |
10110 | } | |
10111 | q = r; | |
10112 | } | |
10113 | ||
c445ea15 | 10114 | if ( (width = expect_number(&q)) ) { |
211dfcf1 HS |
10115 | if (*q == '$') { |
10116 | ++q; | |
10117 | efix = width; | |
10118 | } else { | |
10119 | goto gotwidth; | |
10120 | } | |
10121 | } | |
10122 | ||
fc36a67e | 10123 | /* FLAGS */ |
10124 | ||
46fc3d4c | 10125 | while (*q) { |
10126 | switch (*q) { | |
10127 | case ' ': | |
10128 | case '+': | |
9911cee9 TS |
10129 | if (plus == '+' && *q == ' ') /* '+' over ' ' */ |
10130 | q++; | |
10131 | else | |
10132 | plus = *q++; | |
46fc3d4c | 10133 | continue; |
10134 | ||
10135 | case '-': | |
10136 | left = TRUE; | |
10137 | q++; | |
10138 | continue; | |
10139 | ||
10140 | case '0': | |
10141 | fill = *q++; | |
10142 | continue; | |
10143 | ||
10144 | case '#': | |
10145 | alt = TRUE; | |
10146 | q++; | |
10147 | continue; | |
10148 | ||
fc36a67e | 10149 | default: |
10150 | break; | |
10151 | } | |
10152 | break; | |
10153 | } | |
46fc3d4c | 10154 | |
211dfcf1 | 10155 | tryasterisk: |
eb3fce90 | 10156 | if (*q == '*') { |
211dfcf1 | 10157 | q++; |
c445ea15 | 10158 | if ( (ewix = expect_number(&q)) ) |
211dfcf1 HS |
10159 | if (*q++ != '$') |
10160 | goto unknown; | |
eb3fce90 | 10161 | asterisk = TRUE; |
211dfcf1 HS |
10162 | } |
10163 | if (*q == 'v') { | |
eb3fce90 | 10164 | q++; |
211dfcf1 HS |
10165 | if (vectorize) |
10166 | goto unknown; | |
9cbac4c7 | 10167 | if ((vectorarg = asterisk)) { |
211dfcf1 HS |
10168 | evix = ewix; |
10169 | ewix = 0; | |
10170 | asterisk = FALSE; | |
10171 | } | |
10172 | vectorize = TRUE; | |
10173 | goto tryasterisk; | |
eb3fce90 JH |
10174 | } |
10175 | ||
211dfcf1 | 10176 | if (!asterisk) |
858a90f9 | 10177 | { |
7a5fa8a2 | 10178 | if( *q == '0' ) |
f3583277 | 10179 | fill = *q++; |
c445ea15 | 10180 | width = expect_number(&q); |
858a90f9 | 10181 | } |
211dfcf1 HS |
10182 | |
10183 | if (vectorize) { | |
10184 | if (vectorarg) { | |
10185 | if (args) | |
10186 | vecsv = va_arg(*args, SV*); | |
7ad96abb NC |
10187 | else if (evix) { |
10188 | vecsv = (evix > 0 && evix <= svmax) | |
81ae3cde | 10189 | ? svargs[evix-1] : S_vcatpvfn_missing_argument(aTHX); |
7ad96abb | 10190 | } else { |
7baa4690 | 10191 | vecsv = svix < svmax |
81ae3cde | 10192 | ? svargs[svix++] : S_vcatpvfn_missing_argument(aTHX); |
7ad96abb | 10193 | } |
245d4a47 | 10194 | dotstr = SvPV_const(vecsv, dotstrlen); |
640283f5 NC |
10195 | /* Keep the DO_UTF8 test *after* the SvPV call, else things go |
10196 | bad with tied or overloaded values that return UTF8. */ | |
211dfcf1 | 10197 | if (DO_UTF8(vecsv)) |
2cf2cfc6 | 10198 | is_utf8 = TRUE; |
640283f5 NC |
10199 | else if (has_utf8) { |
10200 | vecsv = sv_mortalcopy(vecsv); | |
10201 | sv_utf8_upgrade(vecsv); | |
10202 | dotstr = SvPV_const(vecsv, dotstrlen); | |
10203 | is_utf8 = TRUE; | |
10204 | } | |
211dfcf1 HS |
10205 | } |
10206 | if (args) { | |
8896765a | 10207 | VECTORIZE_ARGS |
eb3fce90 | 10208 | } |
7ad96abb | 10209 | else if (efix ? (efix > 0 && efix <= svmax) : svix < svmax) { |
211dfcf1 | 10210 | vecsv = svargs[efix ? efix-1 : svix++]; |
245d4a47 | 10211 | vecstr = (U8*)SvPV_const(vecsv,veclen); |
2cf2cfc6 | 10212 | vec_utf8 = DO_UTF8(vecsv); |
96b8f7ce JP |
10213 | |
10214 | /* if this is a version object, we need to convert | |
10215 | * back into v-string notation and then let the | |
10216 | * vectorize happen normally | |
d7aa5382 | 10217 | */ |
96b8f7ce JP |
10218 | if (sv_derived_from(vecsv, "version")) { |
10219 | char *version = savesvpv(vecsv); | |
85fbaab2 | 10220 | if ( hv_exists(MUTABLE_HV(SvRV(vecsv)), "alpha", 5 ) ) { |
34ba6322 SP |
10221 | Perl_warner(aTHX_ packWARN(WARN_INTERNAL), |
10222 | "vector argument not supported with alpha versions"); | |
10223 | goto unknown; | |
10224 | } | |
96b8f7ce | 10225 | vecsv = sv_newmortal(); |
65b06e02 | 10226 | scan_vstring(version, version + veclen, vecsv); |
96b8f7ce JP |
10227 | vecstr = (U8*)SvPV_const(vecsv, veclen); |
10228 | vec_utf8 = DO_UTF8(vecsv); | |
10229 | Safefree(version); | |
d7aa5382 | 10230 | } |
211dfcf1 HS |
10231 | } |
10232 | else { | |
10233 | vecstr = (U8*)""; | |
10234 | veclen = 0; | |
10235 | } | |
eb3fce90 | 10236 | } |
fc36a67e | 10237 | |
eb3fce90 | 10238 | if (asterisk) { |
fc36a67e | 10239 | if (args) |
10240 | i = va_arg(*args, int); | |
10241 | else | |
eb3fce90 JH |
10242 | i = (ewix ? ewix <= svmax : svix < svmax) ? |
10243 | SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0; | |
fc36a67e | 10244 | left |= (i < 0); |
10245 | width = (i < 0) ? -i : i; | |
fc36a67e | 10246 | } |
211dfcf1 | 10247 | gotwidth: |
fc36a67e | 10248 | |
10249 | /* PRECISION */ | |
46fc3d4c | 10250 | |
fc36a67e | 10251 | if (*q == '.') { |
10252 | q++; | |
10253 | if (*q == '*') { | |
211dfcf1 | 10254 | q++; |
c445ea15 | 10255 | if ( ((epix = expect_number(&q))) && (*q++ != '$') ) |
7b8dd722 HS |
10256 | goto unknown; |
10257 | /* XXX: todo, support specified precision parameter */ | |
10258 | if (epix) | |
211dfcf1 | 10259 | goto unknown; |
46fc3d4c | 10260 | if (args) |
10261 | i = va_arg(*args, int); | |
10262 | else | |
eb3fce90 JH |
10263 | i = (ewix ? ewix <= svmax : svix < svmax) |
10264 | ? SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0; | |
9911cee9 TS |
10265 | precis = i; |
10266 | has_precis = !(i < 0); | |
fc36a67e | 10267 | } |
10268 | else { | |
10269 | precis = 0; | |
10270 | while (isDIGIT(*q)) | |
10271 | precis = precis * 10 + (*q++ - '0'); | |
9911cee9 | 10272 | has_precis = TRUE; |
fc36a67e | 10273 | } |
fc36a67e | 10274 | } |
46fc3d4c | 10275 | |
fc36a67e | 10276 | /* SIZE */ |
46fc3d4c | 10277 | |
fc36a67e | 10278 | switch (*q) { |
c623ac67 GS |
10279 | #ifdef WIN32 |
10280 | case 'I': /* Ix, I32x, and I64x */ | |
10281 | # ifdef WIN64 | |
10282 | if (q[1] == '6' && q[2] == '4') { | |
10283 | q += 3; | |
10284 | intsize = 'q'; | |
10285 | break; | |
10286 | } | |
10287 | # endif | |
10288 | if (q[1] == '3' && q[2] == '2') { | |
10289 | q += 3; | |
10290 | break; | |
10291 | } | |
10292 | # ifdef WIN64 | |
10293 | intsize = 'q'; | |
10294 | # endif | |
10295 | q++; | |
10296 | break; | |
10297 | #endif | |
9e5b023a | 10298 | #if defined(HAS_QUAD) || defined(HAS_LONG_DOUBLE) |
6f9bb7fd | 10299 | case 'L': /* Ld */ |
5f66b61c | 10300 | /*FALLTHROUGH*/ |
e5c81feb | 10301 | #ifdef HAS_QUAD |
6f9bb7fd | 10302 | case 'q': /* qd */ |
9e5b023a | 10303 | #endif |
6f9bb7fd GS |
10304 | intsize = 'q'; |
10305 | q++; | |
10306 | break; | |
10307 | #endif | |
fc36a67e | 10308 | case 'l': |
9e5b023a | 10309 | #if defined(HAS_QUAD) || defined(HAS_LONG_DOUBLE) |
07208e09 | 10310 | if (*++q == 'l') { /* lld, llf */ |
fc36a67e | 10311 | intsize = 'q'; |
07208e09 CS |
10312 | ++q; |
10313 | } | |
10314 | else | |
fc36a67e | 10315 | #endif |
07208e09 CS |
10316 | intsize = 'l'; |
10317 | break; | |
fc36a67e | 10318 | case 'h': |
07208e09 CS |
10319 | if (*++q == 'h') { /* hhd, hhu */ |
10320 | intsize = 'c'; | |
10321 | ++q; | |
10322 | } | |
10323 | else | |
10324 | intsize = 'h'; | |
10325 | break; | |
fc36a67e | 10326 | case 'V': |
07208e09 CS |
10327 | case 'z': |
10328 | case 't': | |
10329 | #if HAS_C99 | |
10330 | case 'j': | |
10331 | #endif | |
fc36a67e | 10332 | intsize = *q++; |
46fc3d4c | 10333 | break; |
10334 | } | |
10335 | ||
fc36a67e | 10336 | /* CONVERSION */ |
10337 | ||
211dfcf1 HS |
10338 | if (*q == '%') { |
10339 | eptr = q++; | |
10340 | elen = 1; | |
26372e71 GA |
10341 | if (vectorize) { |
10342 | c = '%'; | |
10343 | goto unknown; | |
10344 | } | |
211dfcf1 HS |
10345 | goto string; |
10346 | } | |
10347 | ||
26372e71 | 10348 | if (!vectorize && !args) { |
86c51f8b NC |
10349 | if (efix) { |
10350 | const I32 i = efix-1; | |
7baa4690 | 10351 | argsv = (i >= 0 && i < svmax) |
81ae3cde | 10352 | ? svargs[i] : S_vcatpvfn_missing_argument(aTHX); |
86c51f8b NC |
10353 | } else { |
10354 | argsv = (svix >= 0 && svix < svmax) | |
81ae3cde | 10355 | ? svargs[svix++] : S_vcatpvfn_missing_argument(aTHX); |
86c51f8b | 10356 | } |
863811b2 | 10357 | } |
211dfcf1 | 10358 | |
46fc3d4c | 10359 | switch (c = *q++) { |
10360 | ||
10361 | /* STRINGS */ | |
10362 | ||
46fc3d4c | 10363 | case 'c': |
26372e71 GA |
10364 | if (vectorize) |
10365 | goto unknown; | |
4ea561bc | 10366 | uv = (args) ? va_arg(*args, int) : SvIV(argsv); |
1bd104fb JH |
10367 | if ((uv > 255 || |
10368 | (!UNI_IS_INVARIANT(uv) && SvUTF8(sv))) | |
0064a8a9 | 10369 | && !IN_BYTES) { |
dfe13c55 | 10370 | eptr = (char*)utf8buf; |
9041c2e3 | 10371 | elen = uvchr_to_utf8((U8*)eptr, uv) - utf8buf; |
2cf2cfc6 | 10372 | is_utf8 = TRUE; |
7e2040f0 GS |
10373 | } |
10374 | else { | |
10375 | c = (char)uv; | |
10376 | eptr = &c; | |
10377 | elen = 1; | |
a0ed51b3 | 10378 | } |
46fc3d4c | 10379 | goto string; |
10380 | ||
46fc3d4c | 10381 | case 's': |
26372e71 GA |
10382 | if (vectorize) |
10383 | goto unknown; | |
10384 | if (args) { | |
fc36a67e | 10385 | eptr = va_arg(*args, char*); |
c635e13b | 10386 | if (eptr) |
10387 | elen = strlen(eptr); | |
10388 | else { | |
27da23d5 | 10389 | eptr = (char *)nullstr; |
c635e13b | 10390 | elen = sizeof nullstr - 1; |
10391 | } | |
46fc3d4c | 10392 | } |
211dfcf1 | 10393 | else { |
4ea561bc | 10394 | eptr = SvPV_const(argsv, elen); |
7e2040f0 | 10395 | if (DO_UTF8(argsv)) { |
c494f1f4 | 10396 | STRLEN old_precis = precis; |
a0ed51b3 | 10397 | if (has_precis && precis < elen) { |
c494f1f4 | 10398 | STRLEN ulen = sv_len_utf8(argsv); |
9ef5ed94 | 10399 | I32 p = precis > ulen ? ulen : precis; |
7e2040f0 | 10400 | sv_pos_u2b(argsv, &p, 0); /* sticks at end */ |
a0ed51b3 LW |
10401 | precis = p; |
10402 | } | |
10403 | if (width) { /* fudge width (can't fudge elen) */ | |
59b61096 AV |
10404 | if (has_precis && precis < elen) |
10405 | width += precis - old_precis; | |
10406 | else | |
10407 | width += elen - sv_len_utf8(argsv); | |
a0ed51b3 | 10408 | } |
2cf2cfc6 | 10409 | is_utf8 = TRUE; |
a0ed51b3 LW |
10410 | } |
10411 | } | |
fc36a67e | 10412 | |
46fc3d4c | 10413 | string: |
9ef5ed94 | 10414 | if (has_precis && precis < elen) |
46fc3d4c | 10415 | elen = precis; |
10416 | break; | |
10417 | ||
10418 | /* INTEGERS */ | |
10419 | ||
fc36a67e | 10420 | case 'p': |
be75b157 | 10421 | if (alt || vectorize) |
c2e66d9e | 10422 | goto unknown; |
211dfcf1 | 10423 | uv = PTR2UV(args ? va_arg(*args, void*) : argsv); |
fc36a67e | 10424 | base = 16; |
10425 | goto integer; | |
10426 | ||
46fc3d4c | 10427 | case 'D': |
29fe7a80 | 10428 | #ifdef IV_IS_QUAD |
22f3ae8c | 10429 | intsize = 'q'; |
29fe7a80 | 10430 | #else |
46fc3d4c | 10431 | intsize = 'l'; |
29fe7a80 | 10432 | #endif |
5f66b61c | 10433 | /*FALLTHROUGH*/ |
46fc3d4c | 10434 | case 'd': |
10435 | case 'i': | |
8896765a RB |
10436 | #if vdNUMBER |
10437 | format_vd: | |
10438 | #endif | |
b22c7a20 | 10439 | if (vectorize) { |
ba210ebe | 10440 | STRLEN ulen; |
211dfcf1 HS |
10441 | if (!veclen) |
10442 | continue; | |
2cf2cfc6 A |
10443 | if (vec_utf8) |
10444 | uv = utf8n_to_uvchr(vecstr, veclen, &ulen, | |
10445 | UTF8_ALLOW_ANYUV); | |
b22c7a20 | 10446 | else { |
e83d50c9 | 10447 | uv = *vecstr; |
b22c7a20 GS |
10448 | ulen = 1; |
10449 | } | |
10450 | vecstr += ulen; | |
10451 | veclen -= ulen; | |
e83d50c9 JP |
10452 | if (plus) |
10453 | esignbuf[esignlen++] = plus; | |
b22c7a20 GS |
10454 | } |
10455 | else if (args) { | |
46fc3d4c | 10456 | switch (intsize) { |
07208e09 | 10457 | case 'c': iv = (char)va_arg(*args, int); break; |
46fc3d4c | 10458 | case 'h': iv = (short)va_arg(*args, int); break; |
46fc3d4c | 10459 | case 'l': iv = va_arg(*args, long); break; |
fc36a67e | 10460 | case 'V': iv = va_arg(*args, IV); break; |
07208e09 CS |
10461 | case 'z': iv = va_arg(*args, SSize_t); break; |
10462 | case 't': iv = va_arg(*args, ptrdiff_t); break; | |
b10c0dba | 10463 | default: iv = va_arg(*args, int); break; |
07208e09 CS |
10464 | #if HAS_C99 |
10465 | case 'j': iv = va_arg(*args, intmax_t); break; | |
10466 | #endif | |
53f65a9e | 10467 | case 'q': |
cf2093f6 | 10468 | #ifdef HAS_QUAD |
53f65a9e HS |
10469 | iv = va_arg(*args, Quad_t); break; |
10470 | #else | |
10471 | goto unknown; | |
cf2093f6 | 10472 | #endif |
46fc3d4c | 10473 | } |
10474 | } | |
10475 | else { | |
4ea561bc | 10476 | IV tiv = SvIV(argsv); /* work around GCC bug #13488 */ |
46fc3d4c | 10477 | switch (intsize) { |
07208e09 | 10478 | case 'c': iv = (char)tiv; break; |
b10c0dba MHM |
10479 | case 'h': iv = (short)tiv; break; |
10480 | case 'l': iv = (long)tiv; break; | |
10481 | case 'V': | |
10482 | default: iv = tiv; break; | |
53f65a9e | 10483 | case 'q': |
cf2093f6 | 10484 | #ifdef HAS_QUAD |
53f65a9e HS |
10485 | iv = (Quad_t)tiv; break; |
10486 | #else | |
10487 | goto unknown; | |
cf2093f6 | 10488 | #endif |
46fc3d4c | 10489 | } |
10490 | } | |
e83d50c9 JP |
10491 | if ( !vectorize ) /* we already set uv above */ |
10492 | { | |
10493 | if (iv >= 0) { | |
10494 | uv = iv; | |
10495 | if (plus) | |
10496 | esignbuf[esignlen++] = plus; | |
10497 | } | |
10498 | else { | |
10499 | uv = -iv; | |
10500 | esignbuf[esignlen++] = '-'; | |
10501 | } | |
46fc3d4c | 10502 | } |
10503 | base = 10; | |
10504 | goto integer; | |
10505 | ||
fc36a67e | 10506 | case 'U': |
29fe7a80 | 10507 | #ifdef IV_IS_QUAD |
22f3ae8c | 10508 | intsize = 'q'; |
29fe7a80 | 10509 | #else |
fc36a67e | 10510 | intsize = 'l'; |
29fe7a80 | 10511 | #endif |
5f66b61c | 10512 | /*FALLTHROUGH*/ |
fc36a67e | 10513 | case 'u': |
10514 | base = 10; | |
10515 | goto uns_integer; | |
10516 | ||
7ff06cc7 | 10517 | case 'B': |
4f19785b WSI |
10518 | case 'b': |
10519 | base = 2; | |
10520 | goto uns_integer; | |
10521 | ||
46fc3d4c | 10522 | case 'O': |
29fe7a80 | 10523 | #ifdef IV_IS_QUAD |
22f3ae8c | 10524 | intsize = 'q'; |
29fe7a80 | 10525 | #else |
46fc3d4c | 10526 | intsize = 'l'; |
29fe7a80 | 10527 | #endif |
5f66b61c | 10528 | /*FALLTHROUGH*/ |
46fc3d4c | 10529 | case 'o': |
10530 | base = 8; | |
10531 | goto uns_integer; | |
10532 | ||
10533 | case 'X': | |
46fc3d4c | 10534 | case 'x': |
10535 | base = 16; | |
46fc3d4c | 10536 | |
10537 | uns_integer: | |
b22c7a20 | 10538 | if (vectorize) { |
ba210ebe | 10539 | STRLEN ulen; |
b22c7a20 | 10540 | vector: |
211dfcf1 HS |
10541 | if (!veclen) |
10542 | continue; | |
2cf2cfc6 A |
10543 | if (vec_utf8) |
10544 | uv = utf8n_to_uvchr(vecstr, veclen, &ulen, | |
10545 | UTF8_ALLOW_ANYUV); | |
b22c7a20 | 10546 | else { |
a05b299f | 10547 | uv = *vecstr; |
b22c7a20 GS |
10548 | ulen = 1; |
10549 | } | |
10550 | vecstr += ulen; | |
10551 | veclen -= ulen; | |
10552 | } | |
10553 | else if (args) { | |
46fc3d4c | 10554 | switch (intsize) { |
07208e09 | 10555 | case 'c': uv = (unsigned char)va_arg(*args, unsigned); break; |
46fc3d4c | 10556 | case 'h': uv = (unsigned short)va_arg(*args, unsigned); break; |
46fc3d4c | 10557 | case 'l': uv = va_arg(*args, unsigned long); break; |
fc36a67e | 10558 | case 'V': uv = va_arg(*args, UV); break; |
07208e09 CS |
10559 | case 'z': uv = va_arg(*args, Size_t); break; |
10560 | case 't': uv = va_arg(*args, ptrdiff_t); break; /* will sign extend, but there is no uptrdiff_t, so oh well */ | |
10561 | #if HAS_C99 | |
10562 | case 'j': uv = va_arg(*args, uintmax_t); break; | |
10563 | #endif | |
b10c0dba | 10564 | default: uv = va_arg(*args, unsigned); break; |
53f65a9e | 10565 | case 'q': |
cf2093f6 | 10566 | #ifdef HAS_QUAD |
53f65a9e HS |
10567 | uv = va_arg(*args, Uquad_t); break; |
10568 | #else | |
10569 | goto unknown; | |
cf2093f6 | 10570 | #endif |
46fc3d4c | 10571 | } |
10572 | } | |
10573 | else { | |
4ea561bc | 10574 | UV tuv = SvUV(argsv); /* work around GCC bug #13488 */ |
46fc3d4c | 10575 | switch (intsize) { |
07208e09 | 10576 | case 'c': uv = (unsigned char)tuv; break; |
b10c0dba MHM |
10577 | case 'h': uv = (unsigned short)tuv; break; |
10578 | case 'l': uv = (unsigned long)tuv; break; | |
10579 | case 'V': | |
10580 | default: uv = tuv; break; | |
53f65a9e | 10581 | case 'q': |
cf2093f6 | 10582 | #ifdef HAS_QUAD |
53f65a9e HS |
10583 | uv = (Uquad_t)tuv; break; |
10584 | #else | |
10585 | goto unknown; | |
cf2093f6 | 10586 | #endif |
46fc3d4c | 10587 | } |
10588 | } | |
10589 | ||
10590 | integer: | |
4d84ee25 NC |
10591 | { |
10592 | char *ptr = ebuf + sizeof ebuf; | |
1387f30c DD |
10593 | bool tempalt = uv ? alt : FALSE; /* Vectors can't change alt */ |
10594 | zeros = 0; | |
10595 | ||
4d84ee25 NC |
10596 | switch (base) { |
10597 | unsigned dig; | |
10598 | case 16: | |
14eb61ab | 10599 | p = (char *)((c == 'X') ? PL_hexdigit + 16 : PL_hexdigit); |
4d84ee25 NC |
10600 | do { |
10601 | dig = uv & 15; | |
10602 | *--ptr = p[dig]; | |
10603 | } while (uv >>= 4); | |
1387f30c | 10604 | if (tempalt) { |
4d84ee25 NC |
10605 | esignbuf[esignlen++] = '0'; |
10606 | esignbuf[esignlen++] = c; /* 'x' or 'X' */ | |
10607 | } | |
10608 | break; | |
10609 | case 8: | |
10610 | do { | |
10611 | dig = uv & 7; | |
10612 | *--ptr = '0' + dig; | |
10613 | } while (uv >>= 3); | |
10614 | if (alt && *ptr != '0') | |
10615 | *--ptr = '0'; | |
10616 | break; | |
10617 | case 2: | |
10618 | do { | |
10619 | dig = uv & 1; | |
10620 | *--ptr = '0' + dig; | |
10621 | } while (uv >>= 1); | |
1387f30c | 10622 | if (tempalt) { |
4d84ee25 | 10623 | esignbuf[esignlen++] = '0'; |
7ff06cc7 | 10624 | esignbuf[esignlen++] = c; |
4d84ee25 NC |
10625 | } |
10626 | break; | |
10627 | default: /* it had better be ten or less */ | |
10628 | do { | |
10629 | dig = uv % base; | |
10630 | *--ptr = '0' + dig; | |
10631 | } while (uv /= base); | |
10632 | break; | |
46fc3d4c | 10633 | } |
4d84ee25 NC |
10634 | elen = (ebuf + sizeof ebuf) - ptr; |
10635 | eptr = ptr; | |
10636 | if (has_precis) { | |
10637 | if (precis > elen) | |
10638 | zeros = precis - elen; | |
e6bb52fd TS |
10639 | else if (precis == 0 && elen == 1 && *eptr == '0' |
10640 | && !(base == 8 && alt)) /* "%#.0o" prints "0" */ | |
4d84ee25 | 10641 | elen = 0; |
9911cee9 TS |
10642 | |
10643 | /* a precision nullifies the 0 flag. */ | |
10644 | if (fill == '0') | |
10645 | fill = ' '; | |
eda88b6d | 10646 | } |
c10ed8b9 | 10647 | } |
46fc3d4c | 10648 | break; |
10649 | ||
10650 | /* FLOATING POINT */ | |
10651 | ||
fc36a67e | 10652 | case 'F': |
10653 | c = 'f'; /* maybe %F isn't supported here */ | |
5f66b61c | 10654 | /*FALLTHROUGH*/ |
46fc3d4c | 10655 | case 'e': case 'E': |
fc36a67e | 10656 | case 'f': |
46fc3d4c | 10657 | case 'g': case 'G': |
26372e71 GA |
10658 | if (vectorize) |
10659 | goto unknown; | |
46fc3d4c | 10660 | |
10661 | /* This is evil, but floating point is even more evil */ | |
10662 | ||
9e5b023a JH |
10663 | /* for SV-style calling, we can only get NV |
10664 | for C-style calling, we assume %f is double; | |
10665 | for simplicity we allow any of %Lf, %llf, %qf for long double | |
10666 | */ | |
10667 | switch (intsize) { | |
10668 | case 'V': | |
10669 | #if defined(USE_LONG_DOUBLE) | |
10670 | intsize = 'q'; | |
10671 | #endif | |
10672 | break; | |
8a2e3f14 | 10673 | /* [perl #20339] - we should accept and ignore %lf rather than die */ |
00e17364 | 10674 | case 'l': |
5f66b61c | 10675 | /*FALLTHROUGH*/ |
9e5b023a JH |
10676 | default: |
10677 | #if defined(USE_LONG_DOUBLE) | |
10678 | intsize = args ? 0 : 'q'; | |
10679 | #endif | |
10680 | break; | |
10681 | case 'q': | |
10682 | #if defined(HAS_LONG_DOUBLE) | |
10683 | break; | |
10684 | #else | |
5f66b61c | 10685 | /*FALLTHROUGH*/ |
9e5b023a | 10686 | #endif |
07208e09 | 10687 | case 'c': |
9e5b023a | 10688 | case 'h': |
07208e09 CS |
10689 | case 'z': |
10690 | case 't': | |
10691 | case 'j': | |
9e5b023a JH |
10692 | goto unknown; |
10693 | } | |
10694 | ||
10695 | /* now we need (long double) if intsize == 'q', else (double) */ | |
26372e71 | 10696 | nv = (args) ? |
35fff930 JH |
10697 | #if LONG_DOUBLESIZE > DOUBLESIZE |
10698 | intsize == 'q' ? | |
205f51d8 AS |
10699 | va_arg(*args, long double) : |
10700 | va_arg(*args, double) | |
35fff930 | 10701 | #else |
205f51d8 | 10702 | va_arg(*args, double) |
35fff930 | 10703 | #endif |
4ea561bc | 10704 | : SvNV(argsv); |
fc36a67e | 10705 | |
10706 | need = 0; | |
3952c29a NC |
10707 | /* nv * 0 will be NaN for NaN, +Inf and -Inf, and 0 for anything |
10708 | else. frexp() has some unspecified behaviour for those three */ | |
10709 | if (c != 'e' && c != 'E' && (nv * 0) == 0) { | |
fc36a67e | 10710 | i = PERL_INT_MIN; |
9e5b023a JH |
10711 | /* FIXME: if HAS_LONG_DOUBLE but not USE_LONG_DOUBLE this |
10712 | will cast our (long double) to (double) */ | |
73b309ea | 10713 | (void)Perl_frexp(nv, &i); |
fc36a67e | 10714 | if (i == PERL_INT_MIN) |
cea2e8a9 | 10715 | Perl_die(aTHX_ "panic: frexp"); |
c635e13b | 10716 | if (i > 0) |
fc36a67e | 10717 | need = BIT_DIGITS(i); |
10718 | } | |
10719 | need += has_precis ? precis : 6; /* known default */ | |
20f6aaab | 10720 | |
fc36a67e | 10721 | if (need < width) |
10722 | need = width; | |
10723 | ||
20f6aaab AS |
10724 | #ifdef HAS_LDBL_SPRINTF_BUG |
10725 | /* This is to try to fix a bug with irix/nonstop-ux/powerux and | |
205f51d8 AS |
10726 | with sfio - Allen <allens@cpan.org> */ |
10727 | ||
10728 | # ifdef DBL_MAX | |
10729 | # define MY_DBL_MAX DBL_MAX | |
10730 | # else /* XXX guessing! HUGE_VAL may be defined as infinity, so not using */ | |
10731 | # if DOUBLESIZE >= 8 | |
10732 | # define MY_DBL_MAX 1.7976931348623157E+308L | |
10733 | # else | |
10734 | # define MY_DBL_MAX 3.40282347E+38L | |
10735 | # endif | |
10736 | # endif | |
10737 | ||
10738 | # ifdef HAS_LDBL_SPRINTF_BUG_LESS1 /* only between -1L & 1L - Allen */ | |
10739 | # define MY_DBL_MAX_BUG 1L | |
20f6aaab | 10740 | # else |
205f51d8 | 10741 | # define MY_DBL_MAX_BUG MY_DBL_MAX |
20f6aaab | 10742 | # endif |
20f6aaab | 10743 | |
205f51d8 AS |
10744 | # ifdef DBL_MIN |
10745 | # define MY_DBL_MIN DBL_MIN | |
10746 | # else /* XXX guessing! -Allen */ | |
10747 | # if DOUBLESIZE >= 8 | |
10748 | # define MY_DBL_MIN 2.2250738585072014E-308L | |
10749 | # else | |
10750 | # define MY_DBL_MIN 1.17549435E-38L | |
10751 | # endif | |
10752 | # endif | |
20f6aaab | 10753 | |
205f51d8 AS |
10754 | if ((intsize == 'q') && (c == 'f') && |
10755 | ((nv < MY_DBL_MAX_BUG) && (nv > -MY_DBL_MAX_BUG)) && | |
10756 | (need < DBL_DIG)) { | |
10757 | /* it's going to be short enough that | |
10758 | * long double precision is not needed */ | |
10759 | ||
10760 | if ((nv <= 0L) && (nv >= -0L)) | |
10761 | fix_ldbl_sprintf_bug = TRUE; /* 0 is 0 - easiest */ | |
10762 | else { | |
10763 | /* would use Perl_fp_class as a double-check but not | |
10764 | * functional on IRIX - see perl.h comments */ | |
10765 | ||
10766 | if ((nv >= MY_DBL_MIN) || (nv <= -MY_DBL_MIN)) { | |
10767 | /* It's within the range that a double can represent */ | |
10768 | #if defined(DBL_MAX) && !defined(DBL_MIN) | |
10769 | if ((nv >= ((long double)1/DBL_MAX)) || | |
10770 | (nv <= (-(long double)1/DBL_MAX))) | |
20f6aaab | 10771 | #endif |
205f51d8 | 10772 | fix_ldbl_sprintf_bug = TRUE; |
20f6aaab | 10773 | } |
205f51d8 AS |
10774 | } |
10775 | if (fix_ldbl_sprintf_bug == TRUE) { | |
10776 | double temp; | |
10777 | ||
10778 | intsize = 0; | |
10779 | temp = (double)nv; | |
10780 | nv = (NV)temp; | |
10781 | } | |
20f6aaab | 10782 | } |
205f51d8 AS |
10783 | |
10784 | # undef MY_DBL_MAX | |
10785 | # undef MY_DBL_MAX_BUG | |
10786 | # undef MY_DBL_MIN | |
10787 | ||
20f6aaab AS |
10788 | #endif /* HAS_LDBL_SPRINTF_BUG */ |
10789 | ||
46fc3d4c | 10790 | need += 20; /* fudge factor */ |
80252599 GS |
10791 | if (PL_efloatsize < need) { |
10792 | Safefree(PL_efloatbuf); | |
10793 | PL_efloatsize = need + 20; /* more fudge */ | |
a02a5408 | 10794 | Newx(PL_efloatbuf, PL_efloatsize, char); |
7d5ea4e7 | 10795 | PL_efloatbuf[0] = '\0'; |
46fc3d4c | 10796 | } |
10797 | ||
4151a5fe IZ |
10798 | if ( !(width || left || plus || alt) && fill != '0' |
10799 | && has_precis && intsize != 'q' ) { /* Shortcuts */ | |
2873255c NC |
10800 | /* See earlier comment about buggy Gconvert when digits, |
10801 | aka precis is 0 */ | |
10802 | if ( c == 'g' && precis) { | |
2e59c212 | 10803 | Gconvert((NV)nv, (int)precis, 0, PL_efloatbuf); |
4150c189 NC |
10804 | /* May return an empty string for digits==0 */ |
10805 | if (*PL_efloatbuf) { | |
10806 | elen = strlen(PL_efloatbuf); | |
4151a5fe | 10807 | goto float_converted; |
4150c189 | 10808 | } |
4151a5fe IZ |
10809 | } else if ( c == 'f' && !precis) { |
10810 | if ((eptr = F0convert(nv, ebuf + sizeof ebuf, &elen))) | |
10811 | break; | |
10812 | } | |
10813 | } | |
4d84ee25 NC |
10814 | { |
10815 | char *ptr = ebuf + sizeof ebuf; | |
10816 | *--ptr = '\0'; | |
10817 | *--ptr = c; | |
10818 | /* FIXME: what to do if HAS_LONG_DOUBLE but not PERL_PRIfldbl? */ | |
9e5b023a | 10819 | #if defined(HAS_LONG_DOUBLE) && defined(PERL_PRIfldbl) |
4d84ee25 NC |
10820 | if (intsize == 'q') { |
10821 | /* Copy the one or more characters in a long double | |
10822 | * format before the 'base' ([efgEFG]) character to | |
10823 | * the format string. */ | |
10824 | static char const prifldbl[] = PERL_PRIfldbl; | |
10825 | char const *p = prifldbl + sizeof(prifldbl) - 3; | |
10826 | while (p >= prifldbl) { *--ptr = *p--; } | |
10827 | } | |
65202027 | 10828 | #endif |
4d84ee25 NC |
10829 | if (has_precis) { |
10830 | base = precis; | |
10831 | do { *--ptr = '0' + (base % 10); } while (base /= 10); | |
10832 | *--ptr = '.'; | |
10833 | } | |
10834 | if (width) { | |
10835 | base = width; | |
10836 | do { *--ptr = '0' + (base % 10); } while (base /= 10); | |
10837 | } | |
10838 | if (fill == '0') | |
10839 | *--ptr = fill; | |
10840 | if (left) | |
10841 | *--ptr = '-'; | |
10842 | if (plus) | |
10843 | *--ptr = plus; | |
10844 | if (alt) | |
10845 | *--ptr = '#'; | |
10846 | *--ptr = '%'; | |
10847 | ||
10848 | /* No taint. Otherwise we are in the strange situation | |
10849 | * where printf() taints but print($float) doesn't. | |
10850 | * --jhi */ | |
9e5b023a | 10851 | #if defined(HAS_LONG_DOUBLE) |
4150c189 | 10852 | elen = ((intsize == 'q') |
d9fad198 JH |
10853 | ? my_snprintf(PL_efloatbuf, PL_efloatsize, ptr, nv) |
10854 | : my_snprintf(PL_efloatbuf, PL_efloatsize, ptr, (double)nv)); | |
9e5b023a | 10855 | #else |
4150c189 | 10856 | elen = my_sprintf(PL_efloatbuf, ptr, nv); |
9e5b023a | 10857 | #endif |
4d84ee25 | 10858 | } |
4151a5fe | 10859 | float_converted: |
80252599 | 10860 | eptr = PL_efloatbuf; |
46fc3d4c | 10861 | break; |
10862 | ||
fc36a67e | 10863 | /* SPECIAL */ |
10864 | ||
10865 | case 'n': | |
26372e71 GA |
10866 | if (vectorize) |
10867 | goto unknown; | |
fc36a67e | 10868 | i = SvCUR(sv) - origlen; |
26372e71 | 10869 | if (args) { |
c635e13b | 10870 | switch (intsize) { |
07208e09 | 10871 | case 'c': *(va_arg(*args, char*)) = i; break; |
c635e13b | 10872 | case 'h': *(va_arg(*args, short*)) = i; break; |
10873 | default: *(va_arg(*args, int*)) = i; break; | |
10874 | case 'l': *(va_arg(*args, long*)) = i; break; | |
10875 | case 'V': *(va_arg(*args, IV*)) = i; break; | |
07208e09 CS |
10876 | case 'z': *(va_arg(*args, SSize_t*)) = i; break; |
10877 | case 't': *(va_arg(*args, ptrdiff_t*)) = i; break; | |
10878 | #if HAS_C99 | |
10879 | case 'j': *(va_arg(*args, intmax_t*)) = i; break; | |
10880 | #endif | |
53f65a9e | 10881 | case 'q': |
cf2093f6 | 10882 | #ifdef HAS_QUAD |
53f65a9e HS |
10883 | *(va_arg(*args, Quad_t*)) = i; break; |
10884 | #else | |
10885 | goto unknown; | |
cf2093f6 | 10886 | #endif |
c635e13b | 10887 | } |
fc36a67e | 10888 | } |
9dd79c3f | 10889 | else |
211dfcf1 | 10890 | sv_setuv_mg(argsv, (UV)i); |
fc36a67e | 10891 | continue; /* not "break" */ |
10892 | ||
10893 | /* UNKNOWN */ | |
10894 | ||
46fc3d4c | 10895 | default: |
fc36a67e | 10896 | unknown: |
041457d9 DM |
10897 | if (!args |
10898 | && (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF) | |
10899 | && ckWARN(WARN_PRINTF)) | |
10900 | { | |
c4420975 | 10901 | SV * const msg = sv_newmortal(); |
35c1215d NC |
10902 | Perl_sv_setpvf(aTHX_ msg, "Invalid conversion in %sprintf: ", |
10903 | (PL_op->op_type == OP_PRTF) ? "" : "s"); | |
1d1ac7bc MHM |
10904 | if (fmtstart < patend) { |
10905 | const char * const fmtend = q < patend ? q : patend; | |
10906 | const char * f; | |
10907 | sv_catpvs(msg, "\"%"); | |
10908 | for (f = fmtstart; f < fmtend; f++) { | |
10909 | if (isPRINT(*f)) { | |
10910 | sv_catpvn(msg, f, 1); | |
10911 | } else { | |
10912 | Perl_sv_catpvf(aTHX_ msg, | |
10913 | "\\%03"UVof, (UV)*f & 0xFF); | |
10914 | } | |
10915 | } | |
10916 | sv_catpvs(msg, "\""); | |
10917 | } else { | |
396482e1 | 10918 | sv_catpvs(msg, "end of string"); |
1d1ac7bc | 10919 | } |
be2597df | 10920 | Perl_warner(aTHX_ packWARN(WARN_PRINTF), "%"SVf, SVfARG(msg)); /* yes, this is reentrant */ |
c635e13b | 10921 | } |
fb73857a | 10922 | |
10923 | /* output mangled stuff ... */ | |
10924 | if (c == '\0') | |
10925 | --q; | |
46fc3d4c | 10926 | eptr = p; |
10927 | elen = q - p; | |
fb73857a | 10928 | |
10929 | /* ... right here, because formatting flags should not apply */ | |
10930 | SvGROW(sv, SvCUR(sv) + elen + 1); | |
10931 | p = SvEND(sv); | |
4459522c | 10932 | Copy(eptr, p, elen, char); |
fb73857a | 10933 | p += elen; |
10934 | *p = '\0'; | |
3f7c398e | 10935 | SvCUR_set(sv, p - SvPVX_const(sv)); |
58e33a90 | 10936 | svix = osvix; |
fb73857a | 10937 | continue; /* not "break" */ |
46fc3d4c | 10938 | } |
10939 | ||
cc61b222 TS |
10940 | if (is_utf8 != has_utf8) { |
10941 | if (is_utf8) { | |
10942 | if (SvCUR(sv)) | |
10943 | sv_utf8_upgrade(sv); | |
10944 | } | |
10945 | else { | |
10946 | const STRLEN old_elen = elen; | |
59cd0e26 | 10947 | SV * const nsv = newSVpvn_flags(eptr, elen, SVs_TEMP); |
cc61b222 TS |
10948 | sv_utf8_upgrade(nsv); |
10949 | eptr = SvPVX_const(nsv); | |
10950 | elen = SvCUR(nsv); | |
10951 | ||
10952 | if (width) { /* fudge width (can't fudge elen) */ | |
10953 | width += elen - old_elen; | |
10954 | } | |
10955 | is_utf8 = TRUE; | |
10956 | } | |
10957 | } | |
10958 | ||
6c94ec8b | 10959 | have = esignlen + zeros + elen; |
ed2b91d2 | 10960 | if (have < zeros) |
f1f66076 | 10961 | Perl_croak_nocontext("%s", PL_memory_wrap); |
6c94ec8b | 10962 | |
46fc3d4c | 10963 | need = (have > width ? have : width); |
10964 | gap = need - have; | |
10965 | ||
d2641cbd | 10966 | if (need >= (((STRLEN)~0) - SvCUR(sv) - dotstrlen - 1)) |
f1f66076 | 10967 | Perl_croak_nocontext("%s", PL_memory_wrap); |
b22c7a20 | 10968 | SvGROW(sv, SvCUR(sv) + need + dotstrlen + 1); |
46fc3d4c | 10969 | p = SvEND(sv); |
10970 | if (esignlen && fill == '0') { | |
53c1dcc0 | 10971 | int i; |
eb160463 | 10972 | for (i = 0; i < (int)esignlen; i++) |
46fc3d4c | 10973 | *p++ = esignbuf[i]; |
10974 | } | |
10975 | if (gap && !left) { | |
10976 | memset(p, fill, gap); | |
10977 | p += gap; | |
10978 | } | |
10979 | if (esignlen && fill != '0') { | |
53c1dcc0 | 10980 | int i; |
eb160463 | 10981 | for (i = 0; i < (int)esignlen; i++) |
46fc3d4c | 10982 | *p++ = esignbuf[i]; |
10983 | } | |
fc36a67e | 10984 | if (zeros) { |
53c1dcc0 | 10985 | int i; |
fc36a67e | 10986 | for (i = zeros; i; i--) |
10987 | *p++ = '0'; | |
10988 | } | |
46fc3d4c | 10989 | if (elen) { |
4459522c | 10990 | Copy(eptr, p, elen, char); |
46fc3d4c | 10991 | p += elen; |
10992 | } | |
10993 | if (gap && left) { | |
10994 | memset(p, ' ', gap); | |
10995 | p += gap; | |
10996 | } | |
b22c7a20 GS |
10997 | if (vectorize) { |
10998 | if (veclen) { | |
4459522c | 10999 | Copy(dotstr, p, dotstrlen, char); |
b22c7a20 GS |
11000 | p += dotstrlen; |
11001 | } | |
11002 | else | |
11003 | vectorize = FALSE; /* done iterating over vecstr */ | |
11004 | } | |
2cf2cfc6 A |
11005 | if (is_utf8) |
11006 | has_utf8 = TRUE; | |
11007 | if (has_utf8) | |
7e2040f0 | 11008 | SvUTF8_on(sv); |
46fc3d4c | 11009 | *p = '\0'; |
3f7c398e | 11010 | SvCUR_set(sv, p - SvPVX_const(sv)); |
b22c7a20 GS |
11011 | if (vectorize) { |
11012 | esignlen = 0; | |
11013 | goto vector; | |
11014 | } | |
46fc3d4c | 11015 | } |
3e6bd4bf | 11016 | SvTAINT(sv); |
46fc3d4c | 11017 | } |
51371543 | 11018 | |
645c22ef DM |
11019 | /* ========================================================================= |
11020 | ||
11021 | =head1 Cloning an interpreter | |
11022 | ||
11023 | All the macros and functions in this section are for the private use of | |
11024 | the main function, perl_clone(). | |
11025 | ||
f2fc5c80 | 11026 | The foo_dup() functions make an exact copy of an existing foo thingy. |
645c22ef DM |
11027 | During the course of a cloning, a hash table is used to map old addresses |
11028 | to new addresses. The table is created and manipulated with the | |
11029 | ptr_table_* functions. | |
11030 | ||
11031 | =cut | |
11032 | ||
3e8320cc | 11033 | * =========================================================================*/ |
645c22ef DM |
11034 | |
11035 | ||
1d7c1841 GS |
11036 | #if defined(USE_ITHREADS) |
11037 | ||
d4c19fe8 | 11038 | /* XXX Remove this so it doesn't have to go thru the macro and return for nothing */ |
1d7c1841 GS |
11039 | #ifndef GpREFCNT_inc |
11040 | # define GpREFCNT_inc(gp) ((gp) ? (++(gp)->gp_refcnt, (gp)) : (GP*)NULL) | |
11041 | #endif | |
11042 | ||
11043 | ||
a41cc44e | 11044 | /* Certain cases in Perl_ss_dup have been merged, by relying on the fact |
3e07292d | 11045 | that currently av_dup, gv_dup and hv_dup are the same as sv_dup. |
538f2e76 NC |
11046 | If this changes, please unmerge ss_dup. |
11047 | Likewise, sv_dup_inc_multiple() relies on this fact. */ | |
a09252eb | 11048 | #define sv_dup_inc_NN(s,t) SvREFCNT_inc_NN(sv_dup_inc(s,t)) |
502c6561 | 11049 | #define av_dup(s,t) MUTABLE_AV(sv_dup((const SV *)s,t)) |
a09252eb | 11050 | #define av_dup_inc(s,t) MUTABLE_AV(sv_dup_inc((const SV *)s,t)) |
85fbaab2 | 11051 | #define hv_dup(s,t) MUTABLE_HV(sv_dup((const SV *)s,t)) |
a09252eb | 11052 | #define hv_dup_inc(s,t) MUTABLE_HV(sv_dup_inc((const SV *)s,t)) |
daba3364 | 11053 | #define cv_dup(s,t) MUTABLE_CV(sv_dup((const SV *)s,t)) |
a09252eb | 11054 | #define cv_dup_inc(s,t) MUTABLE_CV(sv_dup_inc((const SV *)s,t)) |
daba3364 | 11055 | #define io_dup(s,t) MUTABLE_IO(sv_dup((const SV *)s,t)) |
a09252eb | 11056 | #define io_dup_inc(s,t) MUTABLE_IO(sv_dup_inc((const SV *)s,t)) |
159b6efe | 11057 | #define gv_dup(s,t) MUTABLE_GV(sv_dup((const SV *)s,t)) |
a09252eb | 11058 | #define gv_dup_inc(s,t) MUTABLE_GV(sv_dup_inc((const SV *)s,t)) |
6136c704 AL |
11059 | #define SAVEPV(p) ((p) ? savepv(p) : NULL) |
11060 | #define SAVEPVN(p,n) ((p) ? savepvn(p,n) : NULL) | |
8cf8f3d1 | 11061 | |
199e78b7 DM |
11062 | /* clone a parser */ |
11063 | ||
11064 | yy_parser * | |
66ceb532 | 11065 | Perl_parser_dup(pTHX_ const yy_parser *const proto, CLONE_PARAMS *const param) |
199e78b7 DM |
11066 | { |
11067 | yy_parser *parser; | |
11068 | ||
7918f24d NC |
11069 | PERL_ARGS_ASSERT_PARSER_DUP; |
11070 | ||
199e78b7 DM |
11071 | if (!proto) |
11072 | return NULL; | |
11073 | ||
7c197c94 DM |
11074 | /* look for it in the table first */ |
11075 | parser = (yy_parser *)ptr_table_fetch(PL_ptr_table, proto); | |
11076 | if (parser) | |
11077 | return parser; | |
11078 | ||
11079 | /* create anew and remember what it is */ | |
199e78b7 | 11080 | Newxz(parser, 1, yy_parser); |
7c197c94 | 11081 | ptr_table_store(PL_ptr_table, proto, parser); |
199e78b7 | 11082 | |
199e78b7 DM |
11083 | /* XXX these not yet duped */ |
11084 | parser->old_parser = NULL; | |
11085 | parser->stack = NULL; | |
11086 | parser->ps = NULL; | |
11087 | parser->stack_size = 0; | |
11088 | /* XXX parser->stack->state = 0; */ | |
11089 | ||
11090 | /* XXX eventually, just Copy() most of the parser struct ? */ | |
11091 | ||
11092 | parser->lex_brackets = proto->lex_brackets; | |
11093 | parser->lex_casemods = proto->lex_casemods; | |
11094 | parser->lex_brackstack = savepvn(proto->lex_brackstack, | |
11095 | (proto->lex_brackets < 120 ? 120 : proto->lex_brackets)); | |
11096 | parser->lex_casestack = savepvn(proto->lex_casestack, | |
11097 | (proto->lex_casemods < 12 ? 12 : proto->lex_casemods)); | |
11098 | parser->lex_defer = proto->lex_defer; | |
11099 | parser->lex_dojoin = proto->lex_dojoin; | |
11100 | parser->lex_expect = proto->lex_expect; | |
11101 | parser->lex_formbrack = proto->lex_formbrack; | |
11102 | parser->lex_inpat = proto->lex_inpat; | |
11103 | parser->lex_inwhat = proto->lex_inwhat; | |
11104 | parser->lex_op = proto->lex_op; | |
11105 | parser->lex_repl = sv_dup_inc(proto->lex_repl, param); | |
11106 | parser->lex_starts = proto->lex_starts; | |
11107 | parser->lex_stuff = sv_dup_inc(proto->lex_stuff, param); | |
11108 | parser->multi_close = proto->multi_close; | |
11109 | parser->multi_open = proto->multi_open; | |
11110 | parser->multi_start = proto->multi_start; | |
670a9cb2 | 11111 | parser->multi_end = proto->multi_end; |
199e78b7 DM |
11112 | parser->pending_ident = proto->pending_ident; |
11113 | parser->preambled = proto->preambled; | |
11114 | parser->sublex_info = proto->sublex_info; /* XXX not quite right */ | |
bdc0bf6f | 11115 | parser->linestr = sv_dup_inc(proto->linestr, param); |
53a7735b DM |
11116 | parser->expect = proto->expect; |
11117 | parser->copline = proto->copline; | |
f06b5848 | 11118 | parser->last_lop_op = proto->last_lop_op; |
bc177e6b | 11119 | parser->lex_state = proto->lex_state; |
2f9285f8 | 11120 | parser->rsfp = fp_dup(proto->rsfp, '<', param); |
5486870f DM |
11121 | /* rsfp_filters entries have fake IoDIRP() */ |
11122 | parser->rsfp_filters= av_dup_inc(proto->rsfp_filters, param); | |
12bd6ede DM |
11123 | parser->in_my = proto->in_my; |
11124 | parser->in_my_stash = hv_dup(proto->in_my_stash, param); | |
13765c85 | 11125 | parser->error_count = proto->error_count; |
bc177e6b | 11126 | |
53a7735b | 11127 | |
f06b5848 DM |
11128 | parser->linestr = sv_dup_inc(proto->linestr, param); |
11129 | ||
11130 | { | |
1e05feb3 AL |
11131 | char * const ols = SvPVX(proto->linestr); |
11132 | char * const ls = SvPVX(parser->linestr); | |
f06b5848 DM |
11133 | |
11134 | parser->bufptr = ls + (proto->bufptr >= ols ? | |
11135 | proto->bufptr - ols : 0); | |
11136 | parser->oldbufptr = ls + (proto->oldbufptr >= ols ? | |
11137 | proto->oldbufptr - ols : 0); | |
11138 | parser->oldoldbufptr= ls + (proto->oldoldbufptr >= ols ? | |
11139 | proto->oldoldbufptr - ols : 0); | |
11140 | parser->linestart = ls + (proto->linestart >= ols ? | |
11141 | proto->linestart - ols : 0); | |
11142 | parser->last_uni = ls + (proto->last_uni >= ols ? | |
11143 | proto->last_uni - ols : 0); | |
11144 | parser->last_lop = ls + (proto->last_lop >= ols ? | |
11145 | proto->last_lop - ols : 0); | |
11146 | ||
11147 | parser->bufend = ls + SvCUR(parser->linestr); | |
11148 | } | |
199e78b7 | 11149 | |
14047fc9 DM |
11150 | Copy(proto->tokenbuf, parser->tokenbuf, 256, char); |
11151 | ||
2f9285f8 | 11152 | |
199e78b7 DM |
11153 | #ifdef PERL_MAD |
11154 | parser->endwhite = proto->endwhite; | |
11155 | parser->faketokens = proto->faketokens; | |
11156 | parser->lasttoke = proto->lasttoke; | |
11157 | parser->nextwhite = proto->nextwhite; | |
11158 | parser->realtokenstart = proto->realtokenstart; | |
11159 | parser->skipwhite = proto->skipwhite; | |
11160 | parser->thisclose = proto->thisclose; | |
11161 | parser->thismad = proto->thismad; | |
11162 | parser->thisopen = proto->thisopen; | |
11163 | parser->thisstuff = proto->thisstuff; | |
11164 | parser->thistoken = proto->thistoken; | |
11165 | parser->thiswhite = proto->thiswhite; | |
fb205e7a DM |
11166 | |
11167 | Copy(proto->nexttoke, parser->nexttoke, 5, NEXTTOKE); | |
11168 | parser->curforce = proto->curforce; | |
11169 | #else | |
11170 | Copy(proto->nextval, parser->nextval, 5, YYSTYPE); | |
11171 | Copy(proto->nexttype, parser->nexttype, 5, I32); | |
11172 | parser->nexttoke = proto->nexttoke; | |
199e78b7 | 11173 | #endif |
f0c5aa00 DM |
11174 | |
11175 | /* XXX should clone saved_curcop here, but we aren't passed | |
11176 | * proto_perl; so do it in perl_clone_using instead */ | |
11177 | ||
199e78b7 DM |
11178 | return parser; |
11179 | } | |
11180 | ||
d2d73c3e | 11181 | |
d2d73c3e | 11182 | /* duplicate a file handle */ |
645c22ef | 11183 | |
1d7c1841 | 11184 | PerlIO * |
3be3cdd6 | 11185 | Perl_fp_dup(pTHX_ PerlIO *const fp, const char type, CLONE_PARAMS *const param) |
1d7c1841 GS |
11186 | { |
11187 | PerlIO *ret; | |
53c1dcc0 | 11188 | |
7918f24d | 11189 | PERL_ARGS_ASSERT_FP_DUP; |
53c1dcc0 | 11190 | PERL_UNUSED_ARG(type); |
73d840c0 | 11191 | |
1d7c1841 GS |
11192 | if (!fp) |
11193 | return (PerlIO*)NULL; | |
11194 | ||
11195 | /* look for it in the table first */ | |
11196 | ret = (PerlIO*)ptr_table_fetch(PL_ptr_table, fp); | |
11197 | if (ret) | |
11198 | return ret; | |
11199 | ||
11200 | /* create anew and remember what it is */ | |
ecdeb87c | 11201 | ret = PerlIO_fdupopen(aTHX_ fp, param, PERLIO_DUP_CLONE); |
1d7c1841 GS |
11202 | ptr_table_store(PL_ptr_table, fp, ret); |
11203 | return ret; | |
11204 | } | |
11205 | ||
645c22ef DM |
11206 | /* duplicate a directory handle */ |
11207 | ||
1d7c1841 | 11208 | DIR * |
60b22aca | 11209 | Perl_dirp_dup(pTHX_ DIR *const dp, CLONE_PARAMS *const param) |
1d7c1841 | 11210 | { |
11a11ecf | 11211 | DIR *ret; |
60b22aca JD |
11212 | |
11213 | #ifdef HAS_FCHDIR | |
11a11ecf FC |
11214 | DIR *pwd; |
11215 | register const Direntry_t *dirent; | |
11216 | char smallbuf[256]; | |
11217 | char *name = NULL; | |
11218 | STRLEN len = -1; | |
11219 | long pos; | |
11220 | #endif | |
11221 | ||
96a5add6 | 11222 | PERL_UNUSED_CONTEXT; |
60b22aca | 11223 | PERL_ARGS_ASSERT_DIRP_DUP; |
11a11ecf | 11224 | |
1d7c1841 GS |
11225 | if (!dp) |
11226 | return (DIR*)NULL; | |
60b22aca | 11227 | |
11a11ecf FC |
11228 | /* look for it in the table first */ |
11229 | ret = (DIR*)ptr_table_fetch(PL_ptr_table, dp); | |
11230 | if (ret) | |
11231 | return ret; | |
11232 | ||
60b22aca JD |
11233 | #ifdef HAS_FCHDIR |
11234 | ||
11235 | PERL_UNUSED_ARG(param); | |
11236 | ||
11a11ecf FC |
11237 | /* create anew */ |
11238 | ||
11239 | /* open the current directory (so we can switch back) */ | |
11240 | if (!(pwd = PerlDir_open("."))) return (DIR *)NULL; | |
11241 | ||
11242 | /* chdir to our dir handle and open the present working directory */ | |
11243 | if (fchdir(my_dirfd(dp)) < 0 || !(ret = PerlDir_open("."))) { | |
11244 | PerlDir_close(pwd); | |
11245 | return (DIR *)NULL; | |
11246 | } | |
11247 | /* Now we should have two dir handles pointing to the same dir. */ | |
11248 | ||
11249 | /* Be nice to the calling code and chdir back to where we were. */ | |
11250 | fchdir(my_dirfd(pwd)); /* If this fails, then what? */ | |
11251 | ||
11252 | /* We have no need of the pwd handle any more. */ | |
11253 | PerlDir_close(pwd); | |
11254 | ||
11255 | #ifdef DIRNAMLEN | |
11256 | # define d_namlen(d) (d)->d_namlen | |
11257 | #else | |
11258 | # define d_namlen(d) strlen((d)->d_name) | |
11259 | #endif | |
11260 | /* Iterate once through dp, to get the file name at the current posi- | |
11261 | tion. Then step back. */ | |
11262 | pos = PerlDir_tell(dp); | |
11263 | if ((dirent = PerlDir_read(dp))) { | |
11264 | len = d_namlen(dirent); | |
11265 | if (len <= sizeof smallbuf) name = smallbuf; | |
11266 | else Newx(name, len, char); | |
11267 | Move(dirent->d_name, name, len, char); | |
11268 | } | |
11269 | PerlDir_seek(dp, pos); | |
11270 | ||
11271 | /* Iterate through the new dir handle, till we find a file with the | |
11272 | right name. */ | |
11273 | if (!dirent) /* just before the end */ | |
11274 | for(;;) { | |
11275 | pos = PerlDir_tell(ret); | |
11276 | if (PerlDir_read(ret)) continue; /* not there yet */ | |
11277 | PerlDir_seek(ret, pos); /* step back */ | |
11278 | break; | |
11279 | } | |
11280 | else { | |
11281 | const long pos0 = PerlDir_tell(ret); | |
11282 | for(;;) { | |
11283 | pos = PerlDir_tell(ret); | |
11284 | if ((dirent = PerlDir_read(ret))) { | |
11285 | if (len == d_namlen(dirent) | |
11286 | && memEQ(name, dirent->d_name, len)) { | |
11287 | /* found it */ | |
11288 | PerlDir_seek(ret, pos); /* step back */ | |
11289 | break; | |
11290 | } | |
11291 | /* else we are not there yet; keep iterating */ | |
11292 | } | |
11293 | else { /* This is not meant to happen. The best we can do is | |
11294 | reset the iterator to the beginning. */ | |
11295 | PerlDir_seek(ret, pos0); | |
11296 | break; | |
11297 | } | |
11298 | } | |
11299 | } | |
11300 | #undef d_namlen | |
11301 | ||
11302 | if (name && name != smallbuf) | |
11303 | Safefree(name); | |
60b22aca JD |
11304 | #endif |
11305 | ||
11306 | #ifdef WIN32 | |
11307 | ret = win32_dirp_dup(dp, param); | |
11308 | #endif | |
11a11ecf FC |
11309 | |
11310 | /* pop it in the pointer table */ | |
60b22aca JD |
11311 | if (ret) |
11312 | ptr_table_store(PL_ptr_table, dp, ret); | |
11a11ecf FC |
11313 | |
11314 | return ret; | |
1d7c1841 GS |
11315 | } |
11316 | ||
ff276b08 | 11317 | /* duplicate a typeglob */ |
645c22ef | 11318 | |
1d7c1841 | 11319 | GP * |
66ceb532 | 11320 | Perl_gp_dup(pTHX_ GP *const gp, CLONE_PARAMS *const param) |
1d7c1841 GS |
11321 | { |
11322 | GP *ret; | |
b37c2d43 | 11323 | |
7918f24d NC |
11324 | PERL_ARGS_ASSERT_GP_DUP; |
11325 | ||
1d7c1841 GS |
11326 | if (!gp) |
11327 | return (GP*)NULL; | |
11328 | /* look for it in the table first */ | |
11329 | ret = (GP*)ptr_table_fetch(PL_ptr_table, gp); | |
11330 | if (ret) | |
11331 | return ret; | |
11332 | ||
11333 | /* create anew and remember what it is */ | |
a02a5408 | 11334 | Newxz(ret, 1, GP); |
1d7c1841 GS |
11335 | ptr_table_store(PL_ptr_table, gp, ret); |
11336 | ||
11337 | /* clone */ | |
46d65037 NC |
11338 | /* ret->gp_refcnt must be 0 before any other dups are called. We're relying |
11339 | on Newxz() to do this for us. */ | |
d2d73c3e AB |
11340 | ret->gp_sv = sv_dup_inc(gp->gp_sv, param); |
11341 | ret->gp_io = io_dup_inc(gp->gp_io, param); | |
11342 | ret->gp_form = cv_dup_inc(gp->gp_form, param); | |
11343 | ret->gp_av = av_dup_inc(gp->gp_av, param); | |
11344 | ret->gp_hv = hv_dup_inc(gp->gp_hv, param); | |
11345 | ret->gp_egv = gv_dup(gp->gp_egv, param);/* GvEGV is not refcounted */ | |
11346 | ret->gp_cv = cv_dup_inc(gp->gp_cv, param); | |
1d7c1841 | 11347 | ret->gp_cvgen = gp->gp_cvgen; |
1d7c1841 | 11348 | ret->gp_line = gp->gp_line; |
566771cc | 11349 | ret->gp_file_hek = hek_dup(gp->gp_file_hek, param); |
1d7c1841 GS |
11350 | return ret; |
11351 | } | |
11352 | ||
645c22ef DM |
11353 | /* duplicate a chain of magic */ |
11354 | ||
1d7c1841 | 11355 | MAGIC * |
b88ec9b8 | 11356 | Perl_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *const param) |
1d7c1841 | 11357 | { |
c160a186 | 11358 | MAGIC *mgret = NULL; |
0228edf6 | 11359 | MAGIC **mgprev_p = &mgret; |
7918f24d NC |
11360 | |
11361 | PERL_ARGS_ASSERT_MG_DUP; | |
11362 | ||
1d7c1841 GS |
11363 | for (; mg; mg = mg->mg_moremagic) { |
11364 | MAGIC *nmg; | |
803f2748 DM |
11365 | |
11366 | if ((param->flags & CLONEf_JOIN_IN) | |
11367 | && mg->mg_type == PERL_MAGIC_backref) | |
11368 | /* when joining, we let the individual SVs add themselves to | |
11369 | * backref as needed. */ | |
11370 | continue; | |
11371 | ||
45f7fcc8 | 11372 | Newx(nmg, 1, MAGIC); |
0228edf6 NC |
11373 | *mgprev_p = nmg; |
11374 | mgprev_p = &(nmg->mg_moremagic); | |
11375 | ||
45f7fcc8 NC |
11376 | /* There was a comment "XXX copy dynamic vtable?" but as we don't have |
11377 | dynamic vtables, I'm not sure why Sarathy wrote it. The comment dates | |
11378 | from the original commit adding Perl_mg_dup() - revision 4538. | |
11379 | Similarly there is the annotation "XXX random ptr?" next to the | |
11380 | assignment to nmg->mg_ptr. */ | |
11381 | *nmg = *mg; | |
11382 | ||
288b8c02 | 11383 | /* FIXME for plugins |
45f7fcc8 NC |
11384 | if (nmg->mg_type == PERL_MAGIC_qr) { |
11385 | nmg->mg_obj = MUTABLE_SV(CALLREGDUPE((REGEXP*)nmg->mg_obj, param)); | |
1d7c1841 | 11386 | } |
288b8c02 NC |
11387 | else |
11388 | */ | |
5648c0ae DM |
11389 | nmg->mg_obj = (nmg->mg_flags & MGf_REFCOUNTED) |
11390 | ? nmg->mg_type == PERL_MAGIC_backref | |
11391 | /* The backref AV has its reference | |
11392 | * count deliberately bumped by 1 */ | |
11393 | ? SvREFCNT_inc(av_dup_inc((const AV *) | |
11394 | nmg->mg_obj, param)) | |
11395 | : sv_dup_inc(nmg->mg_obj, param) | |
11396 | : sv_dup(nmg->mg_obj, param); | |
45f7fcc8 NC |
11397 | |
11398 | if (nmg->mg_ptr && nmg->mg_type != PERL_MAGIC_regex_global) { | |
11399 | if (nmg->mg_len > 0) { | |
11400 | nmg->mg_ptr = SAVEPVN(nmg->mg_ptr, nmg->mg_len); | |
11401 | if (nmg->mg_type == PERL_MAGIC_overload_table && | |
11402 | AMT_AMAGIC((AMT*)nmg->mg_ptr)) | |
14befaf4 | 11403 | { |
0bcc34c2 | 11404 | AMT * const namtp = (AMT*)nmg->mg_ptr; |
538f2e76 NC |
11405 | sv_dup_inc_multiple((SV**)(namtp->table), |
11406 | (SV**)(namtp->table), NofAMmeth, param); | |
1d7c1841 GS |
11407 | } |
11408 | } | |
45f7fcc8 NC |
11409 | else if (nmg->mg_len == HEf_SVKEY) |
11410 | nmg->mg_ptr = (char*)sv_dup_inc((const SV *)nmg->mg_ptr, param); | |
1d7c1841 | 11411 | } |
45f7fcc8 | 11412 | if ((nmg->mg_flags & MGf_DUP) && nmg->mg_virtual && nmg->mg_virtual->svt_dup) { |
16c91539 | 11413 | nmg->mg_virtual->svt_dup(aTHX_ nmg, param); |
68795e93 | 11414 | } |
1d7c1841 GS |
11415 | } |
11416 | return mgret; | |
11417 | } | |
11418 | ||
4674ade5 NC |
11419 | #endif /* USE_ITHREADS */ |
11420 | ||
db93c0c4 NC |
11421 | struct ptr_tbl_arena { |
11422 | struct ptr_tbl_arena *next; | |
11423 | struct ptr_tbl_ent array[1023/3]; /* as ptr_tbl_ent has 3 pointers. */ | |
11424 | }; | |
11425 | ||
645c22ef DM |
11426 | /* create a new pointer-mapping table */ |
11427 | ||
1d7c1841 GS |
11428 | PTR_TBL_t * |
11429 | Perl_ptr_table_new(pTHX) | |
11430 | { | |
11431 | PTR_TBL_t *tbl; | |
96a5add6 AL |
11432 | PERL_UNUSED_CONTEXT; |
11433 | ||
b3a120bf | 11434 | Newx(tbl, 1, PTR_TBL_t); |
1d7c1841 GS |
11435 | tbl->tbl_max = 511; |
11436 | tbl->tbl_items = 0; | |
db93c0c4 NC |
11437 | tbl->tbl_arena = NULL; |
11438 | tbl->tbl_arena_next = NULL; | |
11439 | tbl->tbl_arena_end = NULL; | |
a02a5408 | 11440 | Newxz(tbl->tbl_ary, tbl->tbl_max + 1, PTR_TBL_ENT_t*); |
1d7c1841 GS |
11441 | return tbl; |
11442 | } | |
11443 | ||
7119fd33 NC |
11444 | #define PTR_TABLE_HASH(ptr) \ |
11445 | ((PTR2UV(ptr) >> 3) ^ (PTR2UV(ptr) >> (3 + 7)) ^ (PTR2UV(ptr) >> (3 + 17))) | |
134ca3d6 | 11446 | |
645c22ef DM |
11447 | /* map an existing pointer using a table */ |
11448 | ||
7bf61b54 | 11449 | STATIC PTR_TBL_ENT_t * |
1eb6e4ca | 11450 | S_ptr_table_find(PTR_TBL_t *const tbl, const void *const sv) |
7918f24d | 11451 | { |
1d7c1841 | 11452 | PTR_TBL_ENT_t *tblent; |
4373e329 | 11453 | const UV hash = PTR_TABLE_HASH(sv); |
7918f24d NC |
11454 | |
11455 | PERL_ARGS_ASSERT_PTR_TABLE_FIND; | |
11456 | ||
1d7c1841 GS |
11457 | tblent = tbl->tbl_ary[hash & tbl->tbl_max]; |
11458 | for (; tblent; tblent = tblent->next) { | |
11459 | if (tblent->oldval == sv) | |
7bf61b54 | 11460 | return tblent; |
1d7c1841 | 11461 | } |
d4c19fe8 | 11462 | return NULL; |
7bf61b54 NC |
11463 | } |
11464 | ||
11465 | void * | |
1eb6e4ca | 11466 | Perl_ptr_table_fetch(pTHX_ PTR_TBL_t *const tbl, const void *const sv) |
7bf61b54 | 11467 | { |
b0e6ae5b | 11468 | PTR_TBL_ENT_t const *const tblent = ptr_table_find(tbl, sv); |
7918f24d NC |
11469 | |
11470 | PERL_ARGS_ASSERT_PTR_TABLE_FETCH; | |
96a5add6 | 11471 | PERL_UNUSED_CONTEXT; |
7918f24d | 11472 | |
d4c19fe8 | 11473 | return tblent ? tblent->newval : NULL; |
1d7c1841 GS |
11474 | } |
11475 | ||
645c22ef DM |
11476 | /* add a new entry to a pointer-mapping table */ |
11477 | ||
1d7c1841 | 11478 | void |
1eb6e4ca | 11479 | Perl_ptr_table_store(pTHX_ PTR_TBL_t *const tbl, const void *const oldsv, void *const newsv) |
1d7c1841 | 11480 | { |
0c9fdfe0 | 11481 | PTR_TBL_ENT_t *tblent = ptr_table_find(tbl, oldsv); |
7918f24d NC |
11482 | |
11483 | PERL_ARGS_ASSERT_PTR_TABLE_STORE; | |
96a5add6 | 11484 | PERL_UNUSED_CONTEXT; |
1d7c1841 | 11485 | |
7bf61b54 NC |
11486 | if (tblent) { |
11487 | tblent->newval = newsv; | |
11488 | } else { | |
11489 | const UV entry = PTR_TABLE_HASH(oldsv) & tbl->tbl_max; | |
11490 | ||
db93c0c4 NC |
11491 | if (tbl->tbl_arena_next == tbl->tbl_arena_end) { |
11492 | struct ptr_tbl_arena *new_arena; | |
11493 | ||
11494 | Newx(new_arena, 1, struct ptr_tbl_arena); | |
11495 | new_arena->next = tbl->tbl_arena; | |
11496 | tbl->tbl_arena = new_arena; | |
11497 | tbl->tbl_arena_next = new_arena->array; | |
11498 | tbl->tbl_arena_end = new_arena->array | |
11499 | + sizeof(new_arena->array) / sizeof(new_arena->array[0]); | |
11500 | } | |
11501 | ||
11502 | tblent = tbl->tbl_arena_next++; | |
d2a0f284 | 11503 | |
7bf61b54 NC |
11504 | tblent->oldval = oldsv; |
11505 | tblent->newval = newsv; | |
11506 | tblent->next = tbl->tbl_ary[entry]; | |
11507 | tbl->tbl_ary[entry] = tblent; | |
11508 | tbl->tbl_items++; | |
11509 | if (tblent->next && tbl->tbl_items > tbl->tbl_max) | |
11510 | ptr_table_split(tbl); | |
1d7c1841 | 11511 | } |
1d7c1841 GS |
11512 | } |
11513 | ||
645c22ef DM |
11514 | /* double the hash bucket size of an existing ptr table */ |
11515 | ||
1d7c1841 | 11516 | void |
1eb6e4ca | 11517 | Perl_ptr_table_split(pTHX_ PTR_TBL_t *const tbl) |
1d7c1841 GS |
11518 | { |
11519 | PTR_TBL_ENT_t **ary = tbl->tbl_ary; | |
4373e329 | 11520 | const UV oldsize = tbl->tbl_max + 1; |
1d7c1841 GS |
11521 | UV newsize = oldsize * 2; |
11522 | UV i; | |
7918f24d NC |
11523 | |
11524 | PERL_ARGS_ASSERT_PTR_TABLE_SPLIT; | |
96a5add6 | 11525 | PERL_UNUSED_CONTEXT; |
1d7c1841 GS |
11526 | |
11527 | Renew(ary, newsize, PTR_TBL_ENT_t*); | |
11528 | Zero(&ary[oldsize], newsize-oldsize, PTR_TBL_ENT_t*); | |
11529 | tbl->tbl_max = --newsize; | |
11530 | tbl->tbl_ary = ary; | |
11531 | for (i=0; i < oldsize; i++, ary++) { | |
4c9d89c5 NC |
11532 | PTR_TBL_ENT_t **entp = ary; |
11533 | PTR_TBL_ENT_t *ent = *ary; | |
11534 | PTR_TBL_ENT_t **curentp; | |
11535 | if (!ent) | |
1d7c1841 GS |
11536 | continue; |
11537 | curentp = ary + oldsize; | |
4c9d89c5 | 11538 | do { |
134ca3d6 | 11539 | if ((newsize & PTR_TABLE_HASH(ent->oldval)) != i) { |
1d7c1841 GS |
11540 | *entp = ent->next; |
11541 | ent->next = *curentp; | |
11542 | *curentp = ent; | |
1d7c1841 GS |
11543 | } |
11544 | else | |
11545 | entp = &ent->next; | |
4c9d89c5 NC |
11546 | ent = *entp; |
11547 | } while (ent); | |
1d7c1841 GS |
11548 | } |
11549 | } | |
11550 | ||
645c22ef | 11551 | /* remove all the entries from a ptr table */ |
5c5ade3e | 11552 | /* Deprecated - will be removed post 5.14 */ |
645c22ef | 11553 | |
a0739874 | 11554 | void |
1eb6e4ca | 11555 | Perl_ptr_table_clear(pTHX_ PTR_TBL_t *const tbl) |
a0739874 | 11556 | { |
d5cefff9 | 11557 | if (tbl && tbl->tbl_items) { |
db93c0c4 | 11558 | struct ptr_tbl_arena *arena = tbl->tbl_arena; |
a0739874 | 11559 | |
db93c0c4 | 11560 | Zero(tbl->tbl_ary, tbl->tbl_max + 1, struct ptr_tbl_ent **); |
ab1e7f95 | 11561 | |
db93c0c4 NC |
11562 | while (arena) { |
11563 | struct ptr_tbl_arena *next = arena->next; | |
11564 | ||
11565 | Safefree(arena); | |
11566 | arena = next; | |
11567 | }; | |
a0739874 | 11568 | |
d5cefff9 | 11569 | tbl->tbl_items = 0; |
db93c0c4 NC |
11570 | tbl->tbl_arena = NULL; |
11571 | tbl->tbl_arena_next = NULL; | |
11572 | tbl->tbl_arena_end = NULL; | |
d5cefff9 | 11573 | } |
a0739874 DM |
11574 | } |
11575 | ||
645c22ef DM |
11576 | /* clear and free a ptr table */ |
11577 | ||
a0739874 | 11578 | void |
1eb6e4ca | 11579 | Perl_ptr_table_free(pTHX_ PTR_TBL_t *const tbl) |
a0739874 | 11580 | { |
5c5ade3e NC |
11581 | struct ptr_tbl_arena *arena; |
11582 | ||
a0739874 DM |
11583 | if (!tbl) { |
11584 | return; | |
11585 | } | |
5c5ade3e NC |
11586 | |
11587 | arena = tbl->tbl_arena; | |
11588 | ||
11589 | while (arena) { | |
11590 | struct ptr_tbl_arena *next = arena->next; | |
11591 | ||
11592 | Safefree(arena); | |
11593 | arena = next; | |
11594 | } | |
11595 | ||
a0739874 DM |
11596 | Safefree(tbl->tbl_ary); |
11597 | Safefree(tbl); | |
11598 | } | |
11599 | ||
4674ade5 | 11600 | #if defined(USE_ITHREADS) |
5bd07a3d | 11601 | |
83841fad | 11602 | void |
1eb6e4ca | 11603 | Perl_rvpv_dup(pTHX_ SV *const dstr, const SV *const sstr, CLONE_PARAMS *const param) |
83841fad | 11604 | { |
7918f24d NC |
11605 | PERL_ARGS_ASSERT_RVPV_DUP; |
11606 | ||
83841fad | 11607 | if (SvROK(sstr)) { |
803f2748 DM |
11608 | if (SvWEAKREF(sstr)) { |
11609 | SvRV_set(dstr, sv_dup(SvRV_const(sstr), param)); | |
11610 | if (param->flags & CLONEf_JOIN_IN) { | |
11611 | /* if joining, we add any back references individually rather | |
11612 | * than copying the whole backref array */ | |
11613 | Perl_sv_add_backref(aTHX_ SvRV(dstr), dstr); | |
11614 | } | |
11615 | } | |
11616 | else | |
11617 | SvRV_set(dstr, sv_dup_inc(SvRV_const(sstr), param)); | |
83841fad | 11618 | } |
3f7c398e | 11619 | else if (SvPVX_const(sstr)) { |
83841fad NIS |
11620 | /* Has something there */ |
11621 | if (SvLEN(sstr)) { | |
68795e93 | 11622 | /* Normal PV - clone whole allocated space */ |
3f7c398e | 11623 | SvPV_set(dstr, SAVEPVN(SvPVX_const(sstr), SvLEN(sstr)-1)); |
d3d0e6f1 NC |
11624 | if (SvREADONLY(sstr) && SvFAKE(sstr)) { |
11625 | /* Not that normal - actually sstr is copy on write. | |
486ec47a | 11626 | But we are a true, independent SV, so: */ |
d3d0e6f1 NC |
11627 | SvREADONLY_off(dstr); |
11628 | SvFAKE_off(dstr); | |
11629 | } | |
68795e93 | 11630 | } |
83841fad NIS |
11631 | else { |
11632 | /* Special case - not normally malloced for some reason */ | |
f7877b28 NC |
11633 | if (isGV_with_GP(sstr)) { |
11634 | /* Don't need to do anything here. */ | |
11635 | } | |
11636 | else if ((SvREADONLY(sstr) && SvFAKE(sstr))) { | |
ef10be65 NC |
11637 | /* A "shared" PV - clone it as "shared" PV */ |
11638 | SvPV_set(dstr, | |
11639 | HEK_KEY(hek_dup(SvSHARED_HEK_FROM_PV(SvPVX_const(sstr)), | |
11640 | param))); | |
83841fad NIS |
11641 | } |
11642 | else { | |
11643 | /* Some other special case - random pointer */ | |
d2c6dc5e | 11644 | SvPV_set(dstr, (char *) SvPVX_const(sstr)); |
d3d0e6f1 | 11645 | } |
83841fad NIS |
11646 | } |
11647 | } | |
11648 | else { | |
4608196e | 11649 | /* Copy the NULL */ |
4df7f6af | 11650 | SvPV_set(dstr, NULL); |
83841fad NIS |
11651 | } |
11652 | } | |
11653 | ||
538f2e76 NC |
11654 | /* duplicate a list of SVs. source and dest may point to the same memory. */ |
11655 | static SV ** | |
11656 | S_sv_dup_inc_multiple(pTHX_ SV *const *source, SV **dest, | |
11657 | SSize_t items, CLONE_PARAMS *const param) | |
11658 | { | |
11659 | PERL_ARGS_ASSERT_SV_DUP_INC_MULTIPLE; | |
11660 | ||
11661 | while (items-- > 0) { | |
11662 | *dest++ = sv_dup_inc(*source++, param); | |
11663 | } | |
11664 | ||
11665 | return dest; | |
11666 | } | |
11667 | ||
662fb8b2 NC |
11668 | /* duplicate an SV of any type (including AV, HV etc) */ |
11669 | ||
d08d57ef NC |
11670 | static SV * |
11671 | S_sv_dup_common(pTHX_ const SV *const sstr, CLONE_PARAMS *const param) | |
1d7c1841 | 11672 | { |
27da23d5 | 11673 | dVAR; |
1d7c1841 GS |
11674 | SV *dstr; |
11675 | ||
d08d57ef | 11676 | PERL_ARGS_ASSERT_SV_DUP_COMMON; |
7918f24d | 11677 | |
bfd95973 NC |
11678 | if (SvTYPE(sstr) == SVTYPEMASK) { |
11679 | #ifdef DEBUG_LEAKING_SCALARS_ABORT | |
11680 | abort(); | |
11681 | #endif | |
6136c704 | 11682 | return NULL; |
bfd95973 | 11683 | } |
1d7c1841 | 11684 | /* look for it in the table first */ |
daba3364 | 11685 | dstr = MUTABLE_SV(ptr_table_fetch(PL_ptr_table, sstr)); |
1d7c1841 GS |
11686 | if (dstr) |
11687 | return dstr; | |
11688 | ||
0405e91e AB |
11689 | if(param->flags & CLONEf_JOIN_IN) { |
11690 | /** We are joining here so we don't want do clone | |
11691 | something that is bad **/ | |
eb86f8b3 | 11692 | if (SvTYPE(sstr) == SVt_PVHV) { |
9bde8eb0 | 11693 | const HEK * const hvname = HvNAME_HEK(sstr); |
96bafef9 | 11694 | if (hvname) { |
eb86f8b3 | 11695 | /** don't clone stashes if they already exist **/ |
96bafef9 DM |
11696 | dstr = MUTABLE_SV(gv_stashpvn(HEK_KEY(hvname), HEK_LEN(hvname), 0)); |
11697 | ptr_table_store(PL_ptr_table, sstr, dstr); | |
11698 | return dstr; | |
11699 | } | |
0405e91e AB |
11700 | } |
11701 | } | |
11702 | ||
1d7c1841 GS |
11703 | /* create anew and remember what it is */ |
11704 | new_SV(dstr); | |
fd0854ff DM |
11705 | |
11706 | #ifdef DEBUG_LEAKING_SCALARS | |
11707 | dstr->sv_debug_optype = sstr->sv_debug_optype; | |
11708 | dstr->sv_debug_line = sstr->sv_debug_line; | |
11709 | dstr->sv_debug_inpad = sstr->sv_debug_inpad; | |
cd676548 | 11710 | dstr->sv_debug_parent = (SV*)sstr; |
de61950a | 11711 | FREE_SV_DEBUG_FILE(dstr); |
fd0854ff | 11712 | dstr->sv_debug_file = savepv(sstr->sv_debug_file); |
fd0854ff DM |
11713 | #endif |
11714 | ||
1d7c1841 GS |
11715 | ptr_table_store(PL_ptr_table, sstr, dstr); |
11716 | ||
11717 | /* clone */ | |
11718 | SvFLAGS(dstr) = SvFLAGS(sstr); | |
11719 | SvFLAGS(dstr) &= ~SVf_OOK; /* don't propagate OOK hack */ | |
11720 | SvREFCNT(dstr) = 0; /* must be before any other dups! */ | |
11721 | ||
11722 | #ifdef DEBUGGING | |
3f7c398e | 11723 | if (SvANY(sstr) && PL_watch_pvx && SvPVX_const(sstr) == PL_watch_pvx) |
1d7c1841 | 11724 | PerlIO_printf(Perl_debug_log, "watch at %p hit, found string \"%s\"\n", |
6c9570dc | 11725 | (void*)PL_watch_pvx, SvPVX_const(sstr)); |
1d7c1841 GS |
11726 | #endif |
11727 | ||
9660f481 DM |
11728 | /* don't clone objects whose class has asked us not to */ |
11729 | if (SvOBJECT(sstr) && ! (SvFLAGS(SvSTASH(sstr)) & SVphv_CLONEABLE)) { | |
33de8e4a | 11730 | SvFLAGS(dstr) = 0; |
9660f481 DM |
11731 | return dstr; |
11732 | } | |
11733 | ||
1d7c1841 GS |
11734 | switch (SvTYPE(sstr)) { |
11735 | case SVt_NULL: | |
11736 | SvANY(dstr) = NULL; | |
11737 | break; | |
11738 | case SVt_IV: | |
339049b0 | 11739 | SvANY(dstr) = (XPVIV*)((char*)&(dstr->sv_u.svu_iv) - STRUCT_OFFSET(XPVIV, xiv_iv)); |
4df7f6af NC |
11740 | if(SvROK(sstr)) { |
11741 | Perl_rvpv_dup(aTHX_ dstr, sstr, param); | |
11742 | } else { | |
11743 | SvIV_set(dstr, SvIVX(sstr)); | |
11744 | } | |
1d7c1841 GS |
11745 | break; |
11746 | case SVt_NV: | |
11747 | SvANY(dstr) = new_XNV(); | |
9d6ce603 | 11748 | SvNV_set(dstr, SvNVX(sstr)); |
1d7c1841 | 11749 | break; |
cecf5685 | 11750 | /* case SVt_BIND: */ |
662fb8b2 NC |
11751 | default: |
11752 | { | |
11753 | /* These are all the types that need complex bodies allocating. */ | |
662fb8b2 | 11754 | void *new_body; |
2bcc16b3 NC |
11755 | const svtype sv_type = SvTYPE(sstr); |
11756 | const struct body_details *const sv_type_details | |
11757 | = bodies_by_type + sv_type; | |
662fb8b2 | 11758 | |
93e68bfb | 11759 | switch (sv_type) { |
662fb8b2 | 11760 | default: |
bb263b4e | 11761 | Perl_croak(aTHX_ "Bizarre SvTYPE [%" IVdf "]", (IV)SvTYPE(sstr)); |
662fb8b2 NC |
11762 | break; |
11763 | ||
662fb8b2 | 11764 | case SVt_PVGV: |
c22188b4 NC |
11765 | case SVt_PVIO: |
11766 | case SVt_PVFM: | |
11767 | case SVt_PVHV: | |
11768 | case SVt_PVAV: | |
662fb8b2 | 11769 | case SVt_PVCV: |
662fb8b2 | 11770 | case SVt_PVLV: |
5c35adbb | 11771 | case SVt_REGEXP: |
662fb8b2 | 11772 | case SVt_PVMG: |
662fb8b2 | 11773 | case SVt_PVNV: |
662fb8b2 | 11774 | case SVt_PVIV: |
662fb8b2 | 11775 | case SVt_PV: |
d2a0f284 | 11776 | assert(sv_type_details->body_size); |
c22188b4 | 11777 | if (sv_type_details->arena) { |
d2a0f284 | 11778 | new_body_inline(new_body, sv_type); |
c22188b4 | 11779 | new_body |
b9502f15 | 11780 | = (void*)((char*)new_body - sv_type_details->offset); |
c22188b4 NC |
11781 | } else { |
11782 | new_body = new_NOARENA(sv_type_details); | |
11783 | } | |
1d7c1841 | 11784 | } |
662fb8b2 NC |
11785 | assert(new_body); |
11786 | SvANY(dstr) = new_body; | |
11787 | ||
2bcc16b3 | 11788 | #ifndef PURIFY |
b9502f15 NC |
11789 | Copy(((char*)SvANY(sstr)) + sv_type_details->offset, |
11790 | ((char*)SvANY(dstr)) + sv_type_details->offset, | |
f32993d6 | 11791 | sv_type_details->copy, char); |
2bcc16b3 NC |
11792 | #else |
11793 | Copy(((char*)SvANY(sstr)), | |
11794 | ((char*)SvANY(dstr)), | |
d2a0f284 | 11795 | sv_type_details->body_size + sv_type_details->offset, char); |
2bcc16b3 | 11796 | #endif |
662fb8b2 | 11797 | |
f7877b28 | 11798 | if (sv_type != SVt_PVAV && sv_type != SVt_PVHV |
5bb89d25 NC |
11799 | && !isGV_with_GP(dstr) |
11800 | && !(sv_type == SVt_PVIO && !(IoFLAGS(dstr) & IOf_FAKE_DIRP))) | |
662fb8b2 NC |
11801 | Perl_rvpv_dup(aTHX_ dstr, sstr, param); |
11802 | ||
11803 | /* The Copy above means that all the source (unduplicated) pointers | |
11804 | are now in the destination. We can check the flags and the | |
11805 | pointers in either, but it's possible that there's less cache | |
11806 | missing by always going for the destination. | |
11807 | FIXME - instrument and check that assumption */ | |
f32993d6 | 11808 | if (sv_type >= SVt_PVMG) { |
885ffcb3 | 11809 | if ((sv_type == SVt_PVMG) && SvPAD_OUR(dstr)) { |
73d95100 | 11810 | SvOURSTASH_set(dstr, hv_dup_inc(SvOURSTASH(dstr), param)); |
e736a858 | 11811 | } else if (SvMAGIC(dstr)) |
662fb8b2 NC |
11812 | SvMAGIC_set(dstr, mg_dup(SvMAGIC(dstr), param)); |
11813 | if (SvSTASH(dstr)) | |
11814 | SvSTASH_set(dstr, hv_dup_inc(SvSTASH(dstr), param)); | |
1d7c1841 | 11815 | } |
662fb8b2 | 11816 | |
f32993d6 NC |
11817 | /* The cast silences a GCC warning about unhandled types. */ |
11818 | switch ((int)sv_type) { | |
662fb8b2 NC |
11819 | case SVt_PV: |
11820 | break; | |
11821 | case SVt_PVIV: | |
11822 | break; | |
11823 | case SVt_PVNV: | |
11824 | break; | |
11825 | case SVt_PVMG: | |
11826 | break; | |
5c35adbb | 11827 | case SVt_REGEXP: |
288b8c02 | 11828 | /* FIXME for plugins */ |
d2f13c59 | 11829 | re_dup_guts((REGEXP*) sstr, (REGEXP*) dstr, param); |
f708cfc1 | 11830 | break; |
662fb8b2 NC |
11831 | case SVt_PVLV: |
11832 | /* XXX LvTARGOFF sometimes holds PMOP* when DEBUGGING */ | |
11833 | if (LvTYPE(dstr) == 't') /* for tie: unrefcnted fake (SV**) */ | |
11834 | LvTARG(dstr) = dstr; | |
11835 | else if (LvTYPE(dstr) == 'T') /* for tie: fake HE */ | |
daba3364 | 11836 | LvTARG(dstr) = MUTABLE_SV(he_dup((HE*)LvTARG(dstr), 0, param)); |
662fb8b2 NC |
11837 | else |
11838 | LvTARG(dstr) = sv_dup_inc(LvTARG(dstr), param); | |
662fb8b2 | 11839 | case SVt_PVGV: |
61e14cb4 | 11840 | /* non-GP case already handled above */ |
cecf5685 | 11841 | if(isGV_with_GP(sstr)) { |
566771cc | 11842 | GvNAME_HEK(dstr) = hek_dup(GvNAME_HEK(dstr), param); |
39cb70dc NC |
11843 | /* Don't call sv_add_backref here as it's going to be |
11844 | created as part of the magic cloning of the symbol | |
27bca322 FC |
11845 | table--unless this is during a join and the stash |
11846 | is not actually being cloned. */ | |
f7877b28 NC |
11847 | /* Danger Will Robinson - GvGP(dstr) isn't initialised |
11848 | at the point of this comment. */ | |
39cb70dc | 11849 | GvSTASH(dstr) = hv_dup(GvSTASH(dstr), param); |
ab95db60 DM |
11850 | if (param->flags & CLONEf_JOIN_IN) |
11851 | Perl_sv_add_backref(aTHX_ MUTABLE_SV(GvSTASH(dstr)), dstr); | |
c43ae56f | 11852 | GvGP_set(dstr, gp_dup(GvGP(sstr), param)); |
f7877b28 | 11853 | (void)GpREFCNT_inc(GvGP(dstr)); |
61e14cb4 | 11854 | } |
662fb8b2 NC |
11855 | break; |
11856 | case SVt_PVIO: | |
5486870f | 11857 | /* PL_parser->rsfp_filters entries have fake IoDIRP() */ |
662fb8b2 NC |
11858 | if(IoFLAGS(dstr) & IOf_FAKE_DIRP) { |
11859 | /* I have no idea why fake dirp (rsfps) | |
11860 | should be treated differently but otherwise | |
11861 | we end up with leaks -- sky*/ | |
11862 | IoTOP_GV(dstr) = gv_dup_inc(IoTOP_GV(dstr), param); | |
11863 | IoFMT_GV(dstr) = gv_dup_inc(IoFMT_GV(dstr), param); | |
11864 | IoBOTTOM_GV(dstr) = gv_dup_inc(IoBOTTOM_GV(dstr), param); | |
11865 | } else { | |
11866 | IoTOP_GV(dstr) = gv_dup(IoTOP_GV(dstr), param); | |
11867 | IoFMT_GV(dstr) = gv_dup(IoFMT_GV(dstr), param); | |
11868 | IoBOTTOM_GV(dstr) = gv_dup(IoBOTTOM_GV(dstr), param); | |
100ce7e1 | 11869 | if (IoDIRP(dstr)) { |
60b22aca | 11870 | IoDIRP(dstr) = dirp_dup(IoDIRP(dstr), param); |
100ce7e1 | 11871 | } else { |
6f207bd3 | 11872 | NOOP; |
100ce7e1 NC |
11873 | /* IoDIRP(dstr) is already a copy of IoDIRP(sstr) */ |
11874 | } | |
6f7e8353 | 11875 | IoIFP(dstr) = fp_dup(IoIFP(sstr), IoTYPE(dstr), param); |
662fb8b2 | 11876 | } |
6f7e8353 NC |
11877 | if (IoOFP(dstr) == IoIFP(sstr)) |
11878 | IoOFP(dstr) = IoIFP(dstr); | |
11879 | else | |
11880 | IoOFP(dstr) = fp_dup(IoOFP(dstr), IoTYPE(dstr), param); | |
662fb8b2 NC |
11881 | IoTOP_NAME(dstr) = SAVEPV(IoTOP_NAME(dstr)); |
11882 | IoFMT_NAME(dstr) = SAVEPV(IoFMT_NAME(dstr)); | |
11883 | IoBOTTOM_NAME(dstr) = SAVEPV(IoBOTTOM_NAME(dstr)); | |
11884 | break; | |
11885 | case SVt_PVAV: | |
2779b694 KB |
11886 | /* avoid cloning an empty array */ |
11887 | if (AvARRAY((const AV *)sstr) && AvFILLp((const AV *)sstr) >= 0) { | |
662fb8b2 | 11888 | SV **dst_ary, **src_ary; |
502c6561 | 11889 | SSize_t items = AvFILLp((const AV *)sstr) + 1; |
662fb8b2 | 11890 | |
502c6561 NC |
11891 | src_ary = AvARRAY((const AV *)sstr); |
11892 | Newxz(dst_ary, AvMAX((const AV *)sstr)+1, SV*); | |
662fb8b2 | 11893 | ptr_table_store(PL_ptr_table, src_ary, dst_ary); |
502c6561 NC |
11894 | AvARRAY(MUTABLE_AV(dstr)) = dst_ary; |
11895 | AvALLOC((const AV *)dstr) = dst_ary; | |
11896 | if (AvREAL((const AV *)sstr)) { | |
538f2e76 NC |
11897 | dst_ary = sv_dup_inc_multiple(src_ary, dst_ary, items, |
11898 | param); | |
662fb8b2 NC |
11899 | } |
11900 | else { | |
11901 | while (items-- > 0) | |
11902 | *dst_ary++ = sv_dup(*src_ary++, param); | |
11903 | } | |
502c6561 | 11904 | items = AvMAX((const AV *)sstr) - AvFILLp((const AV *)sstr); |
662fb8b2 NC |
11905 | while (items-- > 0) { |
11906 | *dst_ary++ = &PL_sv_undef; | |
11907 | } | |
bfcb3514 | 11908 | } |
662fb8b2 | 11909 | else { |
502c6561 NC |
11910 | AvARRAY(MUTABLE_AV(dstr)) = NULL; |
11911 | AvALLOC((const AV *)dstr) = (SV**)NULL; | |
2779b694 KB |
11912 | AvMAX( (const AV *)dstr) = -1; |
11913 | AvFILLp((const AV *)dstr) = -1; | |
b79f7545 | 11914 | } |
662fb8b2 NC |
11915 | break; |
11916 | case SVt_PVHV: | |
1d193675 | 11917 | if (HvARRAY((const HV *)sstr)) { |
7e265ef3 AL |
11918 | STRLEN i = 0; |
11919 | const bool sharekeys = !!HvSHAREKEYS(sstr); | |
11920 | XPVHV * const dxhv = (XPVHV*)SvANY(dstr); | |
11921 | XPVHV * const sxhv = (XPVHV*)SvANY(sstr); | |
11922 | char *darray; | |
11923 | Newx(darray, PERL_HV_ARRAY_ALLOC_BYTES(dxhv->xhv_max+1) | |
11924 | + (SvOOK(sstr) ? sizeof(struct xpvhv_aux) : 0), | |
11925 | char); | |
11926 | HvARRAY(dstr) = (HE**)darray; | |
11927 | while (i <= sxhv->xhv_max) { | |
11928 | const HE * const source = HvARRAY(sstr)[i]; | |
11929 | HvARRAY(dstr)[i] = source | |
11930 | ? he_dup(source, sharekeys, param) : 0; | |
11931 | ++i; | |
11932 | } | |
11933 | if (SvOOK(sstr)) { | |
7e265ef3 AL |
11934 | const struct xpvhv_aux * const saux = HvAUX(sstr); |
11935 | struct xpvhv_aux * const daux = HvAUX(dstr); | |
11936 | /* This flag isn't copied. */ | |
11937 | /* SvOOK_on(hv) attacks the IV flags. */ | |
11938 | SvFLAGS(dstr) |= SVf_OOK; | |
11939 | ||
b7247a80 | 11940 | if (saux->xhv_name_count) { |
36b0d498 | 11941 | HEK ** const sname = saux->xhv_name_u.xhvnameu_names; |
78b79c77 FC |
11942 | const I32 count |
11943 | = saux->xhv_name_count < 0 | |
11944 | ? -saux->xhv_name_count | |
11945 | : saux->xhv_name_count; | |
b7247a80 FC |
11946 | HEK **shekp = sname + count; |
11947 | HEK **dhekp; | |
15d9236d NC |
11948 | Newx(daux->xhv_name_u.xhvnameu_names, count, HEK *); |
11949 | dhekp = daux->xhv_name_u.xhvnameu_names + count; | |
b7247a80 FC |
11950 | while (shekp-- > sname) { |
11951 | dhekp--; | |
11952 | *dhekp = hek_dup(*shekp, param); | |
11953 | } | |
11954 | } | |
15d9236d NC |
11955 | else { |
11956 | daux->xhv_name_u.xhvnameu_name | |
11957 | = hek_dup(saux->xhv_name_u.xhvnameu_name, | |
11958 | param); | |
11959 | } | |
b7247a80 | 11960 | daux->xhv_name_count = saux->xhv_name_count; |
7e265ef3 AL |
11961 | |
11962 | daux->xhv_riter = saux->xhv_riter; | |
11963 | daux->xhv_eiter = saux->xhv_eiter | |
11964 | ? he_dup(saux->xhv_eiter, | |
f2338a2e | 11965 | cBOOL(HvSHAREKEYS(sstr)), param) : 0; |
b17f5ab7 | 11966 | /* backref array needs refcnt=2; see sv_add_backref */ |
7e265ef3 | 11967 | daux->xhv_backreferences = |
ab95db60 DM |
11968 | (param->flags & CLONEf_JOIN_IN) |
11969 | /* when joining, we let the individual GVs and | |
11970 | * CVs add themselves to backref as | |
11971 | * needed. This avoids pulling in stuff | |
11972 | * that isn't required, and simplifies the | |
11973 | * case where stashes aren't cloned back | |
11974 | * if they already exist in the parent | |
11975 | * thread */ | |
11976 | ? NULL | |
11977 | : saux->xhv_backreferences | |
5648c0ae DM |
11978 | ? (SvTYPE(saux->xhv_backreferences) == SVt_PVAV) |
11979 | ? MUTABLE_AV(SvREFCNT_inc( | |
11980 | sv_dup_inc((const SV *) | |
11981 | saux->xhv_backreferences, param))) | |
11982 | : MUTABLE_AV(sv_dup((const SV *) | |
11983 | saux->xhv_backreferences, param)) | |
86f55936 | 11984 | : 0; |
e1a479c5 BB |
11985 | |
11986 | daux->xhv_mro_meta = saux->xhv_mro_meta | |
11987 | ? mro_meta_dup(saux->xhv_mro_meta, param) | |
11988 | : 0; | |
11989 | ||
7e265ef3 | 11990 | /* Record stashes for possible cloning in Perl_clone(). */ |
605aedcc | 11991 | if (HvNAME(sstr)) |
7e265ef3 | 11992 | av_push(param->stashes, dstr); |
662fb8b2 | 11993 | } |
662fb8b2 | 11994 | } |
7e265ef3 | 11995 | else |
85fbaab2 | 11996 | HvARRAY(MUTABLE_HV(dstr)) = NULL; |
662fb8b2 | 11997 | break; |
662fb8b2 | 11998 | case SVt_PVCV: |
bb172083 NC |
11999 | if (!(param->flags & CLONEf_COPY_STACKS)) { |
12000 | CvDEPTH(dstr) = 0; | |
12001 | } | |
4c74a7df | 12002 | /*FALLTHROUGH*/ |
bb172083 | 12003 | case SVt_PVFM: |
662fb8b2 | 12004 | /* NOTE: not refcounted */ |
c68d9564 Z |
12005 | SvANY(MUTABLE_CV(dstr))->xcv_stash = |
12006 | hv_dup(CvSTASH(dstr), param); | |
ab95db60 DM |
12007 | if ((param->flags & CLONEf_JOIN_IN) && CvSTASH(dstr)) |
12008 | Perl_sv_add_backref(aTHX_ MUTABLE_SV(CvSTASH(dstr)), dstr); | |
f352ce09 NC |
12009 | if (!CvISXSUB(dstr)) { |
12010 | OP_REFCNT_LOCK; | |
d04ba589 | 12011 | CvROOT(dstr) = OpREFCNT_inc(CvROOT(dstr)); |
f352ce09 NC |
12012 | OP_REFCNT_UNLOCK; |
12013 | CvFILE(dstr) = SAVEPV(CvFILE(dstr)); | |
12014 | } else if (CvCONST(dstr)) { | |
d32faaf3 | 12015 | CvXSUBANY(dstr).any_ptr = |
daba3364 | 12016 | sv_dup_inc((const SV *)CvXSUBANY(dstr).any_ptr, param); |
662fb8b2 NC |
12017 | } |
12018 | /* don't dup if copying back - CvGV isn't refcounted, so the | |
12019 | * duped GV may never be freed. A bit of a hack! DAPM */ | |
b3f91e91 | 12020 | SvANY(MUTABLE_CV(dstr))->xcv_gv = |
cfc1e951 | 12021 | CvCVGV_RC(dstr) |
803f2748 DM |
12022 | ? gv_dup_inc(CvGV(sstr), param) |
12023 | : (param->flags & CLONEf_JOIN_IN) | |
12024 | ? NULL | |
12025 | : gv_dup(CvGV(sstr), param); | |
12026 | ||
d5b1589c | 12027 | CvPADLIST(dstr) = padlist_dup(CvPADLIST(sstr), param); |
662fb8b2 NC |
12028 | CvOUTSIDE(dstr) = |
12029 | CvWEAKOUTSIDE(sstr) | |
12030 | ? cv_dup( CvOUTSIDE(dstr), param) | |
12031 | : cv_dup_inc(CvOUTSIDE(dstr), param); | |
662fb8b2 | 12032 | break; |
bfcb3514 | 12033 | } |
1d7c1841 | 12034 | } |
1d7c1841 GS |
12035 | } |
12036 | ||
12037 | if (SvOBJECT(dstr) && SvTYPE(dstr) != SVt_PVIO) | |
12038 | ++PL_sv_objcount; | |
12039 | ||
12040 | return dstr; | |
d2d73c3e | 12041 | } |
1d7c1841 | 12042 | |
a09252eb NC |
12043 | SV * |
12044 | Perl_sv_dup_inc(pTHX_ const SV *const sstr, CLONE_PARAMS *const param) | |
12045 | { | |
12046 | PERL_ARGS_ASSERT_SV_DUP_INC; | |
d08d57ef NC |
12047 | return sstr ? SvREFCNT_inc(sv_dup_common(sstr, param)) : NULL; |
12048 | } | |
12049 | ||
12050 | SV * | |
12051 | Perl_sv_dup(pTHX_ const SV *const sstr, CLONE_PARAMS *const param) | |
12052 | { | |
12053 | SV *dstr = sstr ? sv_dup_common(sstr, param) : NULL; | |
12054 | PERL_ARGS_ASSERT_SV_DUP; | |
12055 | ||
04518cc3 NC |
12056 | /* Track every SV that (at least initially) had a reference count of 0. |
12057 | We need to do this by holding an actual reference to it in this array. | |
12058 | If we attempt to cheat, turn AvREAL_off(), and store only pointers | |
12059 | (akin to the stashes hash, and the perl stack), we come unstuck if | |
12060 | a weak reference (or other SV legitimately SvREFCNT() == 0 for this | |
12061 | thread) is manipulated in a CLONE method, because CLONE runs before the | |
12062 | unreferenced array is walked to find SVs still with SvREFCNT() == 0 | |
12063 | (and fix things up by giving each a reference via the temps stack). | |
12064 | Instead, during CLONE, if the 0-referenced SV has SvREFCNT_inc() and | |
12065 | then SvREFCNT_dec(), it will be cleaned up (and added to the free list) | |
12066 | before the walk of unreferenced happens and a reference to that is SV | |
12067 | added to the temps stack. At which point we have the same SV considered | |
12068 | to be in use, and free to be re-used. Not good. | |
12069 | */ | |
d08d57ef NC |
12070 | if (dstr && !(param->flags & CLONEf_COPY_STACKS) && !SvREFCNT(dstr)) { |
12071 | assert(param->unreferenced); | |
04518cc3 | 12072 | av_push(param->unreferenced, SvREFCNT_inc(dstr)); |
d08d57ef NC |
12073 | } |
12074 | ||
12075 | return dstr; | |
a09252eb NC |
12076 | } |
12077 | ||
645c22ef DM |
12078 | /* duplicate a context */ |
12079 | ||
1d7c1841 | 12080 | PERL_CONTEXT * |
a8fc9800 | 12081 | Perl_cx_dup(pTHX_ PERL_CONTEXT *cxs, I32 ix, I32 max, CLONE_PARAMS* param) |
1d7c1841 GS |
12082 | { |
12083 | PERL_CONTEXT *ncxs; | |
12084 | ||
7918f24d NC |
12085 | PERL_ARGS_ASSERT_CX_DUP; |
12086 | ||
1d7c1841 GS |
12087 | if (!cxs) |
12088 | return (PERL_CONTEXT*)NULL; | |
12089 | ||
12090 | /* look for it in the table first */ | |
12091 | ncxs = (PERL_CONTEXT*)ptr_table_fetch(PL_ptr_table, cxs); | |
12092 | if (ncxs) | |
12093 | return ncxs; | |
12094 | ||
12095 | /* create anew and remember what it is */ | |
c2d565bf | 12096 | Newx(ncxs, max + 1, PERL_CONTEXT); |
1d7c1841 | 12097 | ptr_table_store(PL_ptr_table, cxs, ncxs); |
c2d565bf | 12098 | Copy(cxs, ncxs, max + 1, PERL_CONTEXT); |
1d7c1841 GS |
12099 | |
12100 | while (ix >= 0) { | |
c445ea15 | 12101 | PERL_CONTEXT * const ncx = &ncxs[ix]; |
c2d565bf | 12102 | if (CxTYPE(ncx) == CXt_SUBST) { |
1d7c1841 GS |
12103 | Perl_croak(aTHX_ "Cloning substitution context is unimplemented"); |
12104 | } | |
12105 | else { | |
c2d565bf | 12106 | switch (CxTYPE(ncx)) { |
1d7c1841 | 12107 | case CXt_SUB: |
c2d565bf NC |
12108 | ncx->blk_sub.cv = (ncx->blk_sub.olddepth == 0 |
12109 | ? cv_dup_inc(ncx->blk_sub.cv, param) | |
12110 | : cv_dup(ncx->blk_sub.cv,param)); | |
bafb2adc | 12111 | ncx->blk_sub.argarray = (CxHASARGS(ncx) |
c2d565bf NC |
12112 | ? av_dup_inc(ncx->blk_sub.argarray, |
12113 | param) | |
7d49f689 | 12114 | : NULL); |
c2d565bf NC |
12115 | ncx->blk_sub.savearray = av_dup_inc(ncx->blk_sub.savearray, |
12116 | param); | |
d8d97e70 | 12117 | ncx->blk_sub.oldcomppad = (PAD*)ptr_table_fetch(PL_ptr_table, |
c2d565bf | 12118 | ncx->blk_sub.oldcomppad); |
1d7c1841 GS |
12119 | break; |
12120 | case CXt_EVAL: | |
c2d565bf NC |
12121 | ncx->blk_eval.old_namesv = sv_dup_inc(ncx->blk_eval.old_namesv, |
12122 | param); | |
12123 | ncx->blk_eval.cur_text = sv_dup(ncx->blk_eval.cur_text, param); | |
1d7c1841 | 12124 | break; |
d01136d6 | 12125 | case CXt_LOOP_LAZYSV: |
d01136d6 BS |
12126 | ncx->blk_loop.state_u.lazysv.end |
12127 | = sv_dup_inc(ncx->blk_loop.state_u.lazysv.end, param); | |
840fe433 | 12128 | /* We are taking advantage of av_dup_inc and sv_dup_inc |
486ec47a | 12129 | actually being the same function, and order equivalence of |
840fe433 NC |
12130 | the two unions. |
12131 | We can assert the later [but only at run time :-(] */ | |
12132 | assert ((void *) &ncx->blk_loop.state_u.ary.ary == | |
12133 | (void *) &ncx->blk_loop.state_u.lazysv.cur); | |
3b719c58 | 12134 | case CXt_LOOP_FOR: |
d01136d6 BS |
12135 | ncx->blk_loop.state_u.ary.ary |
12136 | = av_dup_inc(ncx->blk_loop.state_u.ary.ary, param); | |
12137 | case CXt_LOOP_LAZYIV: | |
3b719c58 | 12138 | case CXt_LOOP_PLAIN: |
e846cb92 | 12139 | if (CxPADLOOP(ncx)) { |
df530c37 | 12140 | ncx->blk_loop.itervar_u.oldcomppad |
e846cb92 | 12141 | = (PAD*)ptr_table_fetch(PL_ptr_table, |
df530c37 | 12142 | ncx->blk_loop.itervar_u.oldcomppad); |
e846cb92 | 12143 | } else { |
df530c37 DM |
12144 | ncx->blk_loop.itervar_u.gv |
12145 | = gv_dup((const GV *)ncx->blk_loop.itervar_u.gv, | |
12146 | param); | |
e846cb92 | 12147 | } |
1d7c1841 GS |
12148 | break; |
12149 | case CXt_FORMAT: | |
f9c764c5 NC |
12150 | ncx->blk_format.cv = cv_dup(ncx->blk_format.cv, param); |
12151 | ncx->blk_format.gv = gv_dup(ncx->blk_format.gv, param); | |
12152 | ncx->blk_format.dfoutgv = gv_dup_inc(ncx->blk_format.dfoutgv, | |
c2d565bf | 12153 | param); |
1d7c1841 GS |
12154 | break; |
12155 | case CXt_BLOCK: | |
12156 | case CXt_NULL: | |
12157 | break; | |
12158 | } | |
12159 | } | |
12160 | --ix; | |
12161 | } | |
12162 | return ncxs; | |
12163 | } | |
12164 | ||
645c22ef DM |
12165 | /* duplicate a stack info structure */ |
12166 | ||
1d7c1841 | 12167 | PERL_SI * |
a8fc9800 | 12168 | Perl_si_dup(pTHX_ PERL_SI *si, CLONE_PARAMS* param) |
1d7c1841 GS |
12169 | { |
12170 | PERL_SI *nsi; | |
12171 | ||
7918f24d NC |
12172 | PERL_ARGS_ASSERT_SI_DUP; |
12173 | ||
1d7c1841 GS |
12174 | if (!si) |
12175 | return (PERL_SI*)NULL; | |
12176 | ||
12177 | /* look for it in the table first */ | |
12178 | nsi = (PERL_SI*)ptr_table_fetch(PL_ptr_table, si); | |
12179 | if (nsi) | |
12180 | return nsi; | |
12181 | ||
12182 | /* create anew and remember what it is */ | |
a02a5408 | 12183 | Newxz(nsi, 1, PERL_SI); |
1d7c1841 GS |
12184 | ptr_table_store(PL_ptr_table, si, nsi); |
12185 | ||
d2d73c3e | 12186 | nsi->si_stack = av_dup_inc(si->si_stack, param); |
1d7c1841 GS |
12187 | nsi->si_cxix = si->si_cxix; |
12188 | nsi->si_cxmax = si->si_cxmax; | |
d2d73c3e | 12189 | nsi->si_cxstack = cx_dup(si->si_cxstack, si->si_cxix, si->si_cxmax, param); |
1d7c1841 | 12190 | nsi->si_type = si->si_type; |
d2d73c3e AB |
12191 | nsi->si_prev = si_dup(si->si_prev, param); |
12192 | nsi->si_next = si_dup(si->si_next, param); | |
1d7c1841 GS |
12193 | nsi->si_markoff = si->si_markoff; |
12194 | ||
12195 | return nsi; | |
12196 | } | |
12197 | ||
12198 | #define POPINT(ss,ix) ((ss)[--(ix)].any_i32) | |
12199 | #define TOPINT(ss,ix) ((ss)[ix].any_i32) | |
12200 | #define POPLONG(ss,ix) ((ss)[--(ix)].any_long) | |
12201 | #define TOPLONG(ss,ix) ((ss)[ix].any_long) | |
12202 | #define POPIV(ss,ix) ((ss)[--(ix)].any_iv) | |
12203 | #define TOPIV(ss,ix) ((ss)[ix].any_iv) | |
c6bf6a65 NC |
12204 | #define POPUV(ss,ix) ((ss)[--(ix)].any_uv) |
12205 | #define TOPUV(ss,ix) ((ss)[ix].any_uv) | |
38d8b13e HS |
12206 | #define POPBOOL(ss,ix) ((ss)[--(ix)].any_bool) |
12207 | #define TOPBOOL(ss,ix) ((ss)[ix].any_bool) | |
1d7c1841 GS |
12208 | #define POPPTR(ss,ix) ((ss)[--(ix)].any_ptr) |
12209 | #define TOPPTR(ss,ix) ((ss)[ix].any_ptr) | |
12210 | #define POPDPTR(ss,ix) ((ss)[--(ix)].any_dptr) | |
12211 | #define TOPDPTR(ss,ix) ((ss)[ix].any_dptr) | |
12212 | #define POPDXPTR(ss,ix) ((ss)[--(ix)].any_dxptr) | |
12213 | #define TOPDXPTR(ss,ix) ((ss)[ix].any_dxptr) | |
12214 | ||
12215 | /* XXXXX todo */ | |
12216 | #define pv_dup_inc(p) SAVEPV(p) | |
12217 | #define pv_dup(p) SAVEPV(p) | |
12218 | #define svp_dup_inc(p,pp) any_dup(p,pp) | |
12219 | ||
645c22ef DM |
12220 | /* map any object to the new equivent - either something in the |
12221 | * ptr table, or something in the interpreter structure | |
12222 | */ | |
12223 | ||
1d7c1841 | 12224 | void * |
53c1dcc0 | 12225 | Perl_any_dup(pTHX_ void *v, const PerlInterpreter *proto_perl) |
1d7c1841 GS |
12226 | { |
12227 | void *ret; | |
12228 | ||
7918f24d NC |
12229 | PERL_ARGS_ASSERT_ANY_DUP; |
12230 | ||
1d7c1841 GS |
12231 | if (!v) |
12232 | return (void*)NULL; | |
12233 | ||
12234 | /* look for it in the table first */ | |
12235 | ret = ptr_table_fetch(PL_ptr_table, v); | |
12236 | if (ret) | |
12237 | return ret; | |
12238 | ||
12239 | /* see if it is part of the interpreter structure */ | |
12240 | if (v >= (void*)proto_perl && v < (void*)(proto_perl+1)) | |
acfe0abc | 12241 | ret = (void*)(((char*)aTHX) + (((char*)v) - (char*)proto_perl)); |
05ec9bb3 | 12242 | else { |
1d7c1841 | 12243 | ret = v; |
05ec9bb3 | 12244 | } |
1d7c1841 GS |
12245 | |
12246 | return ret; | |
12247 | } | |
12248 | ||
645c22ef DM |
12249 | /* duplicate the save stack */ |
12250 | ||
1d7c1841 | 12251 | ANY * |
a8fc9800 | 12252 | Perl_ss_dup(pTHX_ PerlInterpreter *proto_perl, CLONE_PARAMS* param) |
1d7c1841 | 12253 | { |
53d44271 | 12254 | dVAR; |
907b3e23 DM |
12255 | ANY * const ss = proto_perl->Isavestack; |
12256 | const I32 max = proto_perl->Isavestack_max; | |
12257 | I32 ix = proto_perl->Isavestack_ix; | |
1d7c1841 | 12258 | ANY *nss; |
daba3364 | 12259 | const SV *sv; |
1d193675 NC |
12260 | const GV *gv; |
12261 | const AV *av; | |
12262 | const HV *hv; | |
1d7c1841 GS |
12263 | void* ptr; |
12264 | int intval; | |
12265 | long longval; | |
12266 | GP *gp; | |
12267 | IV iv; | |
b24356f5 | 12268 | I32 i; |
c4e33207 | 12269 | char *c = NULL; |
1d7c1841 | 12270 | void (*dptr) (void*); |
acfe0abc | 12271 | void (*dxptr) (pTHX_ void*); |
1d7c1841 | 12272 | |
7918f24d NC |
12273 | PERL_ARGS_ASSERT_SS_DUP; |
12274 | ||
a02a5408 | 12275 | Newxz(nss, max, ANY); |
1d7c1841 GS |
12276 | |
12277 | while (ix > 0) { | |
c6bf6a65 NC |
12278 | const UV uv = POPUV(ss,ix); |
12279 | const U8 type = (U8)uv & SAVE_MASK; | |
12280 | ||
12281 | TOPUV(nss,ix) = uv; | |
b24356f5 | 12282 | switch (type) { |
cdcdfc56 NC |
12283 | case SAVEt_CLEARSV: |
12284 | break; | |
3e07292d | 12285 | case SAVEt_HELEM: /* hash element */ |
daba3364 | 12286 | sv = (const SV *)POPPTR(ss,ix); |
3e07292d NC |
12287 | TOPPTR(nss,ix) = sv_dup_inc(sv, param); |
12288 | /* fall through */ | |
1d7c1841 | 12289 | case SAVEt_ITEM: /* normal string */ |
0d1db40e | 12290 | case SAVEt_GVSV: /* scalar slot in GV */ |
a41cc44e | 12291 | case SAVEt_SV: /* scalar reference */ |
daba3364 | 12292 | sv = (const SV *)POPPTR(ss,ix); |
d2d73c3e | 12293 | TOPPTR(nss,ix) = sv_dup_inc(sv, param); |
3e07292d NC |
12294 | /* fall through */ |
12295 | case SAVEt_FREESV: | |
12296 | case SAVEt_MORTALIZESV: | |
daba3364 | 12297 | sv = (const SV *)POPPTR(ss,ix); |
d2d73c3e | 12298 | TOPPTR(nss,ix) = sv_dup_inc(sv, param); |
1d7c1841 | 12299 | break; |
05ec9bb3 NIS |
12300 | case SAVEt_SHARED_PVREF: /* char* in shared space */ |
12301 | c = (char*)POPPTR(ss,ix); | |
12302 | TOPPTR(nss,ix) = savesharedpv(c); | |
12303 | ptr = POPPTR(ss,ix); | |
12304 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
12305 | break; | |
1d7c1841 GS |
12306 | case SAVEt_GENERIC_SVREF: /* generic sv */ |
12307 | case SAVEt_SVREF: /* scalar reference */ | |
daba3364 | 12308 | sv = (const SV *)POPPTR(ss,ix); |
d2d73c3e | 12309 | TOPPTR(nss,ix) = sv_dup_inc(sv, param); |
1d7c1841 GS |
12310 | ptr = POPPTR(ss,ix); |
12311 | TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */ | |
12312 | break; | |
a41cc44e | 12313 | case SAVEt_HV: /* hash reference */ |
1d7c1841 | 12314 | case SAVEt_AV: /* array reference */ |
daba3364 | 12315 | sv = (const SV *) POPPTR(ss,ix); |
337d28f5 | 12316 | TOPPTR(nss,ix) = sv_dup_inc(sv, param); |
3e07292d NC |
12317 | /* fall through */ |
12318 | case SAVEt_COMPPAD: | |
12319 | case SAVEt_NSTAB: | |
daba3364 | 12320 | sv = (const SV *) POPPTR(ss,ix); |
3e07292d | 12321 | TOPPTR(nss,ix) = sv_dup(sv, param); |
1d7c1841 GS |
12322 | break; |
12323 | case SAVEt_INT: /* int reference */ | |
12324 | ptr = POPPTR(ss,ix); | |
12325 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
12326 | intval = (int)POPINT(ss,ix); | |
12327 | TOPINT(nss,ix) = intval; | |
12328 | break; | |
12329 | case SAVEt_LONG: /* long reference */ | |
12330 | ptr = POPPTR(ss,ix); | |
12331 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
12332 | longval = (long)POPLONG(ss,ix); | |
12333 | TOPLONG(nss,ix) = longval; | |
12334 | break; | |
12335 | case SAVEt_I32: /* I32 reference */ | |
88effcc9 | 12336 | case SAVEt_COP_ARYBASE: /* call CopARYBASE_set */ |
1d7c1841 GS |
12337 | ptr = POPPTR(ss,ix); |
12338 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
1ccabee8 | 12339 | i = POPINT(ss,ix); |
1d7c1841 GS |
12340 | TOPINT(nss,ix) = i; |
12341 | break; | |
12342 | case SAVEt_IV: /* IV reference */ | |
12343 | ptr = POPPTR(ss,ix); | |
12344 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
12345 | iv = POPIV(ss,ix); | |
12346 | TOPIV(nss,ix) = iv; | |
12347 | break; | |
a41cc44e NC |
12348 | case SAVEt_HPTR: /* HV* reference */ |
12349 | case SAVEt_APTR: /* AV* reference */ | |
1d7c1841 GS |
12350 | case SAVEt_SPTR: /* SV* reference */ |
12351 | ptr = POPPTR(ss,ix); | |
12352 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
daba3364 | 12353 | sv = (const SV *)POPPTR(ss,ix); |
d2d73c3e | 12354 | TOPPTR(nss,ix) = sv_dup(sv, param); |
1d7c1841 GS |
12355 | break; |
12356 | case SAVEt_VPTR: /* random* reference */ | |
12357 | ptr = POPPTR(ss,ix); | |
12358 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
65504245 | 12359 | /* Fall through */ |
994d373a | 12360 | case SAVEt_INT_SMALL: |
89abef21 | 12361 | case SAVEt_I32_SMALL: |
c9441fce | 12362 | case SAVEt_I16: /* I16 reference */ |
6c61c2d4 | 12363 | case SAVEt_I8: /* I8 reference */ |
65504245 | 12364 | case SAVEt_BOOL: |
1d7c1841 GS |
12365 | ptr = POPPTR(ss,ix); |
12366 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
12367 | break; | |
b03d03b0 | 12368 | case SAVEt_GENERIC_PVREF: /* generic char* */ |
1d7c1841 GS |
12369 | case SAVEt_PPTR: /* char* reference */ |
12370 | ptr = POPPTR(ss,ix); | |
12371 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
12372 | c = (char*)POPPTR(ss,ix); | |
12373 | TOPPTR(nss,ix) = pv_dup(c); | |
12374 | break; | |
1d7c1841 GS |
12375 | case SAVEt_GP: /* scalar reference */ |
12376 | gp = (GP*)POPPTR(ss,ix); | |
d2d73c3e | 12377 | TOPPTR(nss,ix) = gp = gp_dup(gp, param); |
1d7c1841 | 12378 | (void)GpREFCNT_inc(gp); |
10507e11 FC |
12379 | gv = (const GV *)POPPTR(ss,ix); |
12380 | TOPPTR(nss,ix) = gv_dup_inc(gv, param); | |
b9e00b79 | 12381 | break; |
1d7c1841 GS |
12382 | case SAVEt_FREEOP: |
12383 | ptr = POPPTR(ss,ix); | |
12384 | if (ptr && (((OP*)ptr)->op_private & OPpREFCOUNTED)) { | |
12385 | /* these are assumed to be refcounted properly */ | |
53c1dcc0 | 12386 | OP *o; |
1d7c1841 GS |
12387 | switch (((OP*)ptr)->op_type) { |
12388 | case OP_LEAVESUB: | |
12389 | case OP_LEAVESUBLV: | |
12390 | case OP_LEAVEEVAL: | |
12391 | case OP_LEAVE: | |
12392 | case OP_SCOPE: | |
12393 | case OP_LEAVEWRITE: | |
e977893f GS |
12394 | TOPPTR(nss,ix) = ptr; |
12395 | o = (OP*)ptr; | |
d3c72c2a | 12396 | OP_REFCNT_LOCK; |
594cd643 | 12397 | (void) OpREFCNT_inc(o); |
d3c72c2a | 12398 | OP_REFCNT_UNLOCK; |
1d7c1841 GS |
12399 | break; |
12400 | default: | |
5f66b61c | 12401 | TOPPTR(nss,ix) = NULL; |
1d7c1841 GS |
12402 | break; |
12403 | } | |
12404 | } | |
12405 | else | |
5f66b61c | 12406 | TOPPTR(nss,ix) = NULL; |
1d7c1841 | 12407 | break; |
3987a177 Z |
12408 | case SAVEt_FREECOPHH: |
12409 | ptr = POPPTR(ss,ix); | |
12410 | TOPPTR(nss,ix) = cophh_copy((COPHH *)ptr); | |
12411 | break; | |
1d7c1841 | 12412 | case SAVEt_DELETE: |
1d193675 | 12413 | hv = (const HV *)POPPTR(ss,ix); |
d2d73c3e | 12414 | TOPPTR(nss,ix) = hv_dup_inc(hv, param); |
35d4f826 NC |
12415 | i = POPINT(ss,ix); |
12416 | TOPINT(nss,ix) = i; | |
8e41545f NC |
12417 | /* Fall through */ |
12418 | case SAVEt_FREEPV: | |
1d7c1841 GS |
12419 | c = (char*)POPPTR(ss,ix); |
12420 | TOPPTR(nss,ix) = pv_dup_inc(c); | |
35d4f826 | 12421 | break; |
3e07292d | 12422 | case SAVEt_STACK_POS: /* Position on Perl stack */ |
1d7c1841 GS |
12423 | i = POPINT(ss,ix); |
12424 | TOPINT(nss,ix) = i; | |
12425 | break; | |
12426 | case SAVEt_DESTRUCTOR: | |
12427 | ptr = POPPTR(ss,ix); | |
12428 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */ | |
12429 | dptr = POPDPTR(ss,ix); | |
8141890a JH |
12430 | TOPDPTR(nss,ix) = DPTR2FPTR(void (*)(void*), |
12431 | any_dup(FPTR2DPTR(void *, dptr), | |
12432 | proto_perl)); | |
1d7c1841 GS |
12433 | break; |
12434 | case SAVEt_DESTRUCTOR_X: | |
12435 | ptr = POPPTR(ss,ix); | |
12436 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */ | |
12437 | dxptr = POPDXPTR(ss,ix); | |
8141890a JH |
12438 | TOPDXPTR(nss,ix) = DPTR2FPTR(void (*)(pTHX_ void*), |
12439 | any_dup(FPTR2DPTR(void *, dxptr), | |
12440 | proto_perl)); | |
1d7c1841 GS |
12441 | break; |
12442 | case SAVEt_REGCONTEXT: | |
12443 | case SAVEt_ALLOC: | |
1be36ce0 | 12444 | ix -= uv >> SAVE_TIGHT_SHIFT; |
1d7c1841 | 12445 | break; |
1d7c1841 | 12446 | case SAVEt_AELEM: /* array element */ |
daba3364 | 12447 | sv = (const SV *)POPPTR(ss,ix); |
d2d73c3e | 12448 | TOPPTR(nss,ix) = sv_dup_inc(sv, param); |
1d7c1841 GS |
12449 | i = POPINT(ss,ix); |
12450 | TOPINT(nss,ix) = i; | |
502c6561 | 12451 | av = (const AV *)POPPTR(ss,ix); |
d2d73c3e | 12452 | TOPPTR(nss,ix) = av_dup_inc(av, param); |
1d7c1841 | 12453 | break; |
1d7c1841 GS |
12454 | case SAVEt_OP: |
12455 | ptr = POPPTR(ss,ix); | |
12456 | TOPPTR(nss,ix) = ptr; | |
12457 | break; | |
12458 | case SAVEt_HINTS: | |
b3ca2e83 | 12459 | ptr = POPPTR(ss,ix); |
20439bc7 | 12460 | ptr = cophh_copy((COPHH*)ptr); |
cbb1fbea | 12461 | TOPPTR(nss,ix) = ptr; |
601cee3b NC |
12462 | i = POPINT(ss,ix); |
12463 | TOPINT(nss,ix) = i; | |
a8f8b6a7 | 12464 | if (i & HINT_LOCALIZE_HH) { |
1d193675 | 12465 | hv = (const HV *)POPPTR(ss,ix); |
a8f8b6a7 NC |
12466 | TOPPTR(nss,ix) = hv_dup_inc(hv, param); |
12467 | } | |
1d7c1841 | 12468 | break; |
09edbca0 | 12469 | case SAVEt_PADSV_AND_MORTALIZE: |
c3564e5c GS |
12470 | longval = (long)POPLONG(ss,ix); |
12471 | TOPLONG(nss,ix) = longval; | |
12472 | ptr = POPPTR(ss,ix); | |
12473 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
daba3364 | 12474 | sv = (const SV *)POPPTR(ss,ix); |
09edbca0 | 12475 | TOPPTR(nss,ix) = sv_dup_inc(sv, param); |
c3564e5c | 12476 | break; |
8bd2680e MHM |
12477 | case SAVEt_SET_SVFLAGS: |
12478 | i = POPINT(ss,ix); | |
12479 | TOPINT(nss,ix) = i; | |
12480 | i = POPINT(ss,ix); | |
12481 | TOPINT(nss,ix) = i; | |
daba3364 | 12482 | sv = (const SV *)POPPTR(ss,ix); |
8bd2680e MHM |
12483 | TOPPTR(nss,ix) = sv_dup(sv, param); |
12484 | break; | |
5bfb7d0e NC |
12485 | case SAVEt_RE_STATE: |
12486 | { | |
12487 | const struct re_save_state *const old_state | |
12488 | = (struct re_save_state *) | |
12489 | (ss + ix - SAVESTACK_ALLOC_FOR_RE_SAVE_STATE); | |
12490 | struct re_save_state *const new_state | |
12491 | = (struct re_save_state *) | |
12492 | (nss + ix - SAVESTACK_ALLOC_FOR_RE_SAVE_STATE); | |
12493 | ||
12494 | Copy(old_state, new_state, 1, struct re_save_state); | |
12495 | ix -= SAVESTACK_ALLOC_FOR_RE_SAVE_STATE; | |
12496 | ||
12497 | new_state->re_state_bostr | |
12498 | = pv_dup(old_state->re_state_bostr); | |
12499 | new_state->re_state_reginput | |
12500 | = pv_dup(old_state->re_state_reginput); | |
5bfb7d0e NC |
12501 | new_state->re_state_regeol |
12502 | = pv_dup(old_state->re_state_regeol); | |
f0ab9afb NC |
12503 | new_state->re_state_regoffs |
12504 | = (regexp_paren_pair*) | |
12505 | any_dup(old_state->re_state_regoffs, proto_perl); | |
5bfb7d0e | 12506 | new_state->re_state_reglastparen |
11b79775 DD |
12507 | = (U32*) any_dup(old_state->re_state_reglastparen, |
12508 | proto_perl); | |
5bfb7d0e | 12509 | new_state->re_state_reglastcloseparen |
11b79775 | 12510 | = (U32*)any_dup(old_state->re_state_reglastcloseparen, |
5bfb7d0e | 12511 | proto_perl); |
5bfb7d0e NC |
12512 | /* XXX This just has to be broken. The old save_re_context |
12513 | code did SAVEGENERICPV(PL_reg_start_tmp); | |
12514 | PL_reg_start_tmp is char **. | |
12515 | Look above to what the dup code does for | |
12516 | SAVEt_GENERIC_PVREF | |
12517 | It can never have worked. | |
12518 | So this is merely a faithful copy of the exiting bug: */ | |
12519 | new_state->re_state_reg_start_tmp | |
12520 | = (char **) pv_dup((char *) | |
12521 | old_state->re_state_reg_start_tmp); | |
12522 | /* I assume that it only ever "worked" because no-one called | |
12523 | (pseudo)fork while the regexp engine had re-entered itself. | |
12524 | */ | |
5bfb7d0e NC |
12525 | #ifdef PERL_OLD_COPY_ON_WRITE |
12526 | new_state->re_state_nrs | |
12527 | = sv_dup(old_state->re_state_nrs, param); | |
12528 | #endif | |
12529 | new_state->re_state_reg_magic | |
11b79775 DD |
12530 | = (MAGIC*) any_dup(old_state->re_state_reg_magic, |
12531 | proto_perl); | |
5bfb7d0e | 12532 | new_state->re_state_reg_oldcurpm |
11b79775 DD |
12533 | = (PMOP*) any_dup(old_state->re_state_reg_oldcurpm, |
12534 | proto_perl); | |
5bfb7d0e | 12535 | new_state->re_state_reg_curpm |
11b79775 DD |
12536 | = (PMOP*) any_dup(old_state->re_state_reg_curpm, |
12537 | proto_perl); | |
5bfb7d0e NC |
12538 | new_state->re_state_reg_oldsaved |
12539 | = pv_dup(old_state->re_state_reg_oldsaved); | |
12540 | new_state->re_state_reg_poscache | |
12541 | = pv_dup(old_state->re_state_reg_poscache); | |
5bfb7d0e NC |
12542 | new_state->re_state_reg_starttry |
12543 | = pv_dup(old_state->re_state_reg_starttry); | |
5bfb7d0e NC |
12544 | break; |
12545 | } | |
68da3b2f NC |
12546 | case SAVEt_COMPILE_WARNINGS: |
12547 | ptr = POPPTR(ss,ix); | |
12548 | TOPPTR(nss,ix) = DUP_WARNINGS((STRLEN*)ptr); | |
7b6dd8c3 | 12549 | break; |
7c197c94 DM |
12550 | case SAVEt_PARSER: |
12551 | ptr = POPPTR(ss,ix); | |
456084a8 | 12552 | TOPPTR(nss,ix) = parser_dup((const yy_parser*)ptr, param); |
7c197c94 | 12553 | break; |
1d7c1841 | 12554 | default: |
147bc374 NC |
12555 | Perl_croak(aTHX_ |
12556 | "panic: ss_dup inconsistency (%"IVdf")", (IV) type); | |
1d7c1841 GS |
12557 | } |
12558 | } | |
12559 | ||
bd81e77b NC |
12560 | return nss; |
12561 | } | |
12562 | ||
12563 | ||
12564 | /* if sv is a stash, call $class->CLONE_SKIP(), and set the SVphv_CLONEABLE | |
12565 | * flag to the result. This is done for each stash before cloning starts, | |
12566 | * so we know which stashes want their objects cloned */ | |
12567 | ||
12568 | static void | |
f30de749 | 12569 | do_mark_cloneable_stash(pTHX_ SV *const sv) |
bd81e77b | 12570 | { |
1d193675 | 12571 | const HEK * const hvname = HvNAME_HEK((const HV *)sv); |
bd81e77b | 12572 | if (hvname) { |
85fbaab2 | 12573 | GV* const cloner = gv_fetchmethod_autoload(MUTABLE_HV(sv), "CLONE_SKIP", 0); |
bd81e77b NC |
12574 | SvFLAGS(sv) |= SVphv_CLONEABLE; /* clone objects by default */ |
12575 | if (cloner && GvCV(cloner)) { | |
12576 | dSP; | |
12577 | UV status; | |
12578 | ||
12579 | ENTER; | |
12580 | SAVETMPS; | |
12581 | PUSHMARK(SP); | |
6e449a3a | 12582 | mXPUSHs(newSVhek(hvname)); |
bd81e77b | 12583 | PUTBACK; |
daba3364 | 12584 | call_sv(MUTABLE_SV(GvCV(cloner)), G_SCALAR); |
bd81e77b NC |
12585 | SPAGAIN; |
12586 | status = POPu; | |
12587 | PUTBACK; | |
12588 | FREETMPS; | |
12589 | LEAVE; | |
12590 | if (status) | |
12591 | SvFLAGS(sv) &= ~SVphv_CLONEABLE; | |
12592 | } | |
12593 | } | |
12594 | } | |
12595 | ||
12596 | ||
12597 | ||
12598 | /* | |
12599 | =for apidoc perl_clone | |
12600 | ||
12601 | Create and return a new interpreter by cloning the current one. | |
12602 | ||
12603 | perl_clone takes these flags as parameters: | |
12604 | ||
12605 | CLONEf_COPY_STACKS - is used to, well, copy the stacks also, | |
12606 | without it we only clone the data and zero the stacks, | |
12607 | with it we copy the stacks and the new perl interpreter is | |
12608 | ready to run at the exact same point as the previous one. | |
12609 | The pseudo-fork code uses COPY_STACKS while the | |
878090d5 | 12610 | threads->create doesn't. |
bd81e77b NC |
12611 | |
12612 | CLONEf_KEEP_PTR_TABLE | |
12613 | perl_clone keeps a ptr_table with the pointer of the old | |
12614 | variable as a key and the new variable as a value, | |
12615 | this allows it to check if something has been cloned and not | |
12616 | clone it again but rather just use the value and increase the | |
12617 | refcount. If KEEP_PTR_TABLE is not set then perl_clone will kill | |
12618 | the ptr_table using the function | |
12619 | C<ptr_table_free(PL_ptr_table); PL_ptr_table = NULL;>, | |
12620 | reason to keep it around is if you want to dup some of your own | |
12621 | variable who are outside the graph perl scans, example of this | |
12622 | code is in threads.xs create | |
12623 | ||
12624 | CLONEf_CLONE_HOST | |
12625 | This is a win32 thing, it is ignored on unix, it tells perls | |
12626 | win32host code (which is c++) to clone itself, this is needed on | |
12627 | win32 if you want to run two threads at the same time, | |
12628 | if you just want to do some stuff in a separate perl interpreter | |
12629 | and then throw it away and return to the original one, | |
12630 | you don't need to do anything. | |
12631 | ||
12632 | =cut | |
12633 | */ | |
12634 | ||
12635 | /* XXX the above needs expanding by someone who actually understands it ! */ | |
12636 | EXTERN_C PerlInterpreter * | |
12637 | perl_clone_host(PerlInterpreter* proto_perl, UV flags); | |
12638 | ||
12639 | PerlInterpreter * | |
12640 | perl_clone(PerlInterpreter *proto_perl, UV flags) | |
12641 | { | |
12642 | dVAR; | |
12643 | #ifdef PERL_IMPLICIT_SYS | |
12644 | ||
7918f24d NC |
12645 | PERL_ARGS_ASSERT_PERL_CLONE; |
12646 | ||
bd81e77b NC |
12647 | /* perlhost.h so we need to call into it |
12648 | to clone the host, CPerlHost should have a c interface, sky */ | |
12649 | ||
12650 | if (flags & CLONEf_CLONE_HOST) { | |
12651 | return perl_clone_host(proto_perl,flags); | |
12652 | } | |
12653 | return perl_clone_using(proto_perl, flags, | |
12654 | proto_perl->IMem, | |
12655 | proto_perl->IMemShared, | |
12656 | proto_perl->IMemParse, | |
12657 | proto_perl->IEnv, | |
12658 | proto_perl->IStdIO, | |
12659 | proto_perl->ILIO, | |
12660 | proto_perl->IDir, | |
12661 | proto_perl->ISock, | |
12662 | proto_perl->IProc); | |
12663 | } | |
12664 | ||
12665 | PerlInterpreter * | |
12666 | perl_clone_using(PerlInterpreter *proto_perl, UV flags, | |
12667 | struct IPerlMem* ipM, struct IPerlMem* ipMS, | |
12668 | struct IPerlMem* ipMP, struct IPerlEnv* ipE, | |
12669 | struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO, | |
12670 | struct IPerlDir* ipD, struct IPerlSock* ipS, | |
12671 | struct IPerlProc* ipP) | |
12672 | { | |
12673 | /* XXX many of the string copies here can be optimized if they're | |
12674 | * constants; they need to be allocated as common memory and just | |
12675 | * their pointers copied. */ | |
12676 | ||
12677 | IV i; | |
12678 | CLONE_PARAMS clone_params; | |
5f66b61c | 12679 | CLONE_PARAMS* const param = &clone_params; |
bd81e77b | 12680 | |
5f66b61c | 12681 | PerlInterpreter * const my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter)); |
7918f24d NC |
12682 | |
12683 | PERL_ARGS_ASSERT_PERL_CLONE_USING; | |
bd81e77b NC |
12684 | #else /* !PERL_IMPLICIT_SYS */ |
12685 | IV i; | |
12686 | CLONE_PARAMS clone_params; | |
12687 | CLONE_PARAMS* param = &clone_params; | |
5f66b61c | 12688 | PerlInterpreter * const my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter)); |
7918f24d NC |
12689 | |
12690 | PERL_ARGS_ASSERT_PERL_CLONE; | |
b59cce4c | 12691 | #endif /* PERL_IMPLICIT_SYS */ |
7918f24d | 12692 | |
bd81e77b NC |
12693 | /* for each stash, determine whether its objects should be cloned */ |
12694 | S_visit(proto_perl, do_mark_cloneable_stash, SVt_PVHV, SVTYPEMASK); | |
12695 | PERL_SET_THX(my_perl); | |
12696 | ||
b59cce4c | 12697 | #ifdef DEBUGGING |
7e337ee0 | 12698 | PoisonNew(my_perl, 1, PerlInterpreter); |
5f66b61c AL |
12699 | PL_op = NULL; |
12700 | PL_curcop = NULL; | |
bd81e77b NC |
12701 | PL_markstack = 0; |
12702 | PL_scopestack = 0; | |
cbdd5331 | 12703 | PL_scopestack_name = 0; |
bd81e77b NC |
12704 | PL_savestack = 0; |
12705 | PL_savestack_ix = 0; | |
12706 | PL_savestack_max = -1; | |
12707 | PL_sig_pending = 0; | |
b8328dae | 12708 | PL_parser = NULL; |
bd81e77b | 12709 | Zero(&PL_debug_pad, 1, struct perl_debug_pad); |
02d9cd5e | 12710 | # ifdef DEBUG_LEAKING_SCALARS |
c895a371 | 12711 | PL_sv_serial = (((UV)my_perl >> 2) & 0xfff) * 1000000; |
02d9cd5e | 12712 | # endif |
b59cce4c | 12713 | #else /* !DEBUGGING */ |
bd81e77b | 12714 | Zero(my_perl, 1, PerlInterpreter); |
b59cce4c | 12715 | #endif /* DEBUGGING */ |
742421a6 DM |
12716 | |
12717 | #ifdef PERL_IMPLICIT_SYS | |
12718 | /* host pointers */ | |
12719 | PL_Mem = ipM; | |
12720 | PL_MemShared = ipMS; | |
12721 | PL_MemParse = ipMP; | |
12722 | PL_Env = ipE; | |
12723 | PL_StdIO = ipStd; | |
12724 | PL_LIO = ipLIO; | |
12725 | PL_Dir = ipD; | |
12726 | PL_Sock = ipS; | |
12727 | PL_Proc = ipP; | |
12728 | #endif /* PERL_IMPLICIT_SYS */ | |
12729 | ||
bd81e77b | 12730 | param->flags = flags; |
f7abe70b NC |
12731 | /* Nothing in the core code uses this, but we make it available to |
12732 | extensions (using mg_dup). */ | |
bd81e77b | 12733 | param->proto_perl = proto_perl; |
f7abe70b NC |
12734 | /* Likely nothing will use this, but it is initialised to be consistent |
12735 | with Perl_clone_params_new(). */ | |
ec2fb142 | 12736 | param->new_perl = my_perl; |
d08d57ef | 12737 | param->unreferenced = NULL; |
bd81e77b | 12738 | |
7cb608b5 NC |
12739 | INIT_TRACK_MEMPOOL(my_perl->Imemory_debug_header, my_perl); |
12740 | ||
fdda85ca | 12741 | PL_body_arenas = NULL; |
bd81e77b NC |
12742 | Zero(&PL_body_roots, 1, PL_body_roots); |
12743 | ||
bd81e77b NC |
12744 | PL_sv_count = 0; |
12745 | PL_sv_objcount = 0; | |
a0714e2c SS |
12746 | PL_sv_root = NULL; |
12747 | PL_sv_arenaroot = NULL; | |
bd81e77b NC |
12748 | |
12749 | PL_debug = proto_perl->Idebug; | |
12750 | ||
12751 | PL_hash_seed = proto_perl->Ihash_seed; | |
12752 | PL_rehash_seed = proto_perl->Irehash_seed; | |
12753 | ||
12754 | #ifdef USE_REENTRANT_API | |
12755 | /* XXX: things like -Dm will segfault here in perlio, but doing | |
12756 | * PERL_SET_CONTEXT(proto_perl); | |
12757 | * breaks too many other things | |
12758 | */ | |
12759 | Perl_reentrant_init(aTHX); | |
12760 | #endif | |
12761 | ||
12762 | /* create SV map for pointer relocation */ | |
12763 | PL_ptr_table = ptr_table_new(); | |
12764 | ||
12765 | /* initialize these special pointers as early as possible */ | |
12766 | SvANY(&PL_sv_undef) = NULL; | |
12767 | SvREFCNT(&PL_sv_undef) = (~(U32)0)/2; | |
12768 | SvFLAGS(&PL_sv_undef) = SVf_READONLY|SVt_NULL; | |
12769 | ptr_table_store(PL_ptr_table, &proto_perl->Isv_undef, &PL_sv_undef); | |
12770 | ||
12771 | SvANY(&PL_sv_no) = new_XPVNV(); | |
12772 | SvREFCNT(&PL_sv_no) = (~(U32)0)/2; | |
12773 | SvFLAGS(&PL_sv_no) = SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK | |
12774 | |SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV; | |
bb7a0f54 | 12775 | SvPV_set(&PL_sv_no, savepvn(PL_No, 0)); |
bd81e77b NC |
12776 | SvCUR_set(&PL_sv_no, 0); |
12777 | SvLEN_set(&PL_sv_no, 1); | |
12778 | SvIV_set(&PL_sv_no, 0); | |
12779 | SvNV_set(&PL_sv_no, 0); | |
12780 | ptr_table_store(PL_ptr_table, &proto_perl->Isv_no, &PL_sv_no); | |
12781 | ||
12782 | SvANY(&PL_sv_yes) = new_XPVNV(); | |
12783 | SvREFCNT(&PL_sv_yes) = (~(U32)0)/2; | |
12784 | SvFLAGS(&PL_sv_yes) = SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK | |
12785 | |SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV; | |
bb7a0f54 | 12786 | SvPV_set(&PL_sv_yes, savepvn(PL_Yes, 1)); |
bd81e77b NC |
12787 | SvCUR_set(&PL_sv_yes, 1); |
12788 | SvLEN_set(&PL_sv_yes, 2); | |
12789 | SvIV_set(&PL_sv_yes, 1); | |
12790 | SvNV_set(&PL_sv_yes, 1); | |
12791 | ptr_table_store(PL_ptr_table, &proto_perl->Isv_yes, &PL_sv_yes); | |
12792 | ||
a1f97a07 DM |
12793 | /* dbargs array probably holds garbage */ |
12794 | PL_dbargs = NULL; | |
7fa38291 | 12795 | |
bd81e77b NC |
12796 | /* create (a non-shared!) shared string table */ |
12797 | PL_strtab = newHV(); | |
12798 | HvSHAREKEYS_off(PL_strtab); | |
12799 | hv_ksplit(PL_strtab, HvTOTALKEYS(proto_perl->Istrtab)); | |
12800 | ptr_table_store(PL_ptr_table, proto_perl->Istrtab, PL_strtab); | |
12801 | ||
12802 | PL_compiling = proto_perl->Icompiling; | |
12803 | ||
12804 | /* These two PVs will be free'd special way so must set them same way op.c does */ | |
12805 | PL_compiling.cop_stashpv = savesharedpv(PL_compiling.cop_stashpv); | |
12806 | ptr_table_store(PL_ptr_table, proto_perl->Icompiling.cop_stashpv, PL_compiling.cop_stashpv); | |
12807 | ||
12808 | PL_compiling.cop_file = savesharedpv(PL_compiling.cop_file); | |
12809 | ptr_table_store(PL_ptr_table, proto_perl->Icompiling.cop_file, PL_compiling.cop_file); | |
12810 | ||
12811 | ptr_table_store(PL_ptr_table, &proto_perl->Icompiling, &PL_compiling); | |
72dc9ed5 | 12812 | PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings); |
20439bc7 | 12813 | CopHINTHASH_set(&PL_compiling, cophh_copy(CopHINTHASH_get(&PL_compiling))); |
907b3e23 | 12814 | PL_curcop = (COP*)any_dup(proto_perl->Icurcop, proto_perl); |
5892a4d4 NC |
12815 | #ifdef PERL_DEBUG_READONLY_OPS |
12816 | PL_slabs = NULL; | |
12817 | PL_slab_count = 0; | |
12818 | #endif | |
bd81e77b NC |
12819 | |
12820 | /* pseudo environmental stuff */ | |
12821 | PL_origargc = proto_perl->Iorigargc; | |
12822 | PL_origargv = proto_perl->Iorigargv; | |
12823 | ||
12824 | param->stashes = newAV(); /* Setup array of objects to call clone on */ | |
842c4123 NC |
12825 | /* This makes no difference to the implementation, as it always pushes |
12826 | and shifts pointers to other SVs without changing their reference | |
12827 | count, with the array becoming empty before it is freed. However, it | |
12828 | makes it conceptually clear what is going on, and will avoid some | |
12829 | work inside av.c, filling slots between AvFILL() and AvMAX() with | |
12830 | &PL_sv_undef, and SvREFCNT_dec()ing those. */ | |
12831 | AvREAL_off(param->stashes); | |
bd81e77b | 12832 | |
d08d57ef NC |
12833 | if (!(flags & CLONEf_COPY_STACKS)) { |
12834 | param->unreferenced = newAV(); | |
d08d57ef NC |
12835 | } |
12836 | ||
bd81e77b NC |
12837 | /* Set tainting stuff before PerlIO_debug can possibly get called */ |
12838 | PL_tainting = proto_perl->Itainting; | |
12839 | PL_taint_warn = proto_perl->Itaint_warn; | |
12840 | ||
12841 | #ifdef PERLIO_LAYERS | |
12842 | /* Clone PerlIO tables as soon as we can handle general xx_dup() */ | |
12843 | PerlIO_clone(aTHX_ proto_perl, param); | |
12844 | #endif | |
12845 | ||
12846 | PL_envgv = gv_dup(proto_perl->Ienvgv, param); | |
12847 | PL_incgv = gv_dup(proto_perl->Iincgv, param); | |
12848 | PL_hintgv = gv_dup(proto_perl->Ihintgv, param); | |
12849 | PL_origfilename = SAVEPV(proto_perl->Iorigfilename); | |
12850 | PL_diehook = sv_dup_inc(proto_perl->Idiehook, param); | |
12851 | PL_warnhook = sv_dup_inc(proto_perl->Iwarnhook, param); | |
12852 | ||
12853 | /* switches */ | |
12854 | PL_minus_c = proto_perl->Iminus_c; | |
12855 | PL_patchlevel = sv_dup_inc(proto_perl->Ipatchlevel, param); | |
1e8125c6 | 12856 | PL_apiversion = sv_dup_inc(proto_perl->Iapiversion, param); |
bd81e77b NC |
12857 | PL_localpatches = proto_perl->Ilocalpatches; |
12858 | PL_splitstr = proto_perl->Isplitstr; | |
bd81e77b NC |
12859 | PL_minus_n = proto_perl->Iminus_n; |
12860 | PL_minus_p = proto_perl->Iminus_p; | |
12861 | PL_minus_l = proto_perl->Iminus_l; | |
12862 | PL_minus_a = proto_perl->Iminus_a; | |
bc9b29db | 12863 | PL_minus_E = proto_perl->Iminus_E; |
bd81e77b NC |
12864 | PL_minus_F = proto_perl->Iminus_F; |
12865 | PL_doswitches = proto_perl->Idoswitches; | |
12866 | PL_dowarn = proto_perl->Idowarn; | |
bd81e77b NC |
12867 | PL_sawampersand = proto_perl->Isawampersand; |
12868 | PL_unsafe = proto_perl->Iunsafe; | |
12869 | PL_inplace = SAVEPV(proto_perl->Iinplace); | |
12870 | PL_e_script = sv_dup_inc(proto_perl->Ie_script, param); | |
12871 | PL_perldb = proto_perl->Iperldb; | |
12872 | PL_perl_destruct_level = proto_perl->Iperl_destruct_level; | |
12873 | PL_exit_flags = proto_perl->Iexit_flags; | |
12874 | ||
12875 | /* magical thingies */ | |
12876 | /* XXX time(&PL_basetime) when asked for? */ | |
12877 | PL_basetime = proto_perl->Ibasetime; | |
12878 | PL_formfeed = sv_dup(proto_perl->Iformfeed, param); | |
12879 | ||
12880 | PL_maxsysfd = proto_perl->Imaxsysfd; | |
bd81e77b NC |
12881 | PL_statusvalue = proto_perl->Istatusvalue; |
12882 | #ifdef VMS | |
12883 | PL_statusvalue_vms = proto_perl->Istatusvalue_vms; | |
12884 | #else | |
12885 | PL_statusvalue_posix = proto_perl->Istatusvalue_posix; | |
12886 | #endif | |
12887 | PL_encoding = sv_dup(proto_perl->Iencoding, param); | |
12888 | ||
76f68e9b MHM |
12889 | sv_setpvs(PERL_DEBUG_PAD(0), ""); /* For regex debugging. */ |
12890 | sv_setpvs(PERL_DEBUG_PAD(1), ""); /* ext/re needs these */ | |
12891 | sv_setpvs(PERL_DEBUG_PAD(2), ""); /* even without DEBUGGING. */ | |
bd81e77b | 12892 | |
84da74a7 | 12893 | |
f9f4320a | 12894 | /* RE engine related */ |
84da74a7 YO |
12895 | Zero(&PL_reg_state, 1, struct re_save_state); |
12896 | PL_reginterp_cnt = 0; | |
12897 | PL_regmatch_slab = NULL; | |
12898 | ||
bd81e77b | 12899 | /* Clone the regex array */ |
937c6efd NC |
12900 | /* ORANGE FIXME for plugins, probably in the SV dup code. |
12901 | newSViv(PTR2IV(CALLREGDUPE( | |
12902 | INT2PTR(REGEXP *, SvIVX(regex)), param)))) | |
12903 | */ | |
12904 | PL_regex_padav = av_dup_inc(proto_perl->Iregex_padav, param); | |
bd81e77b NC |
12905 | PL_regex_pad = AvARRAY(PL_regex_padav); |
12906 | ||
12907 | /* shortcuts to various I/O objects */ | |
b2ea9a00 | 12908 | PL_ofsgv = gv_dup_inc(proto_perl->Iofsgv, param); |
bd81e77b NC |
12909 | PL_stdingv = gv_dup(proto_perl->Istdingv, param); |
12910 | PL_stderrgv = gv_dup(proto_perl->Istderrgv, param); | |
12911 | PL_defgv = gv_dup(proto_perl->Idefgv, param); | |
12912 | PL_argvgv = gv_dup(proto_perl->Iargvgv, param); | |
12913 | PL_argvoutgv = gv_dup(proto_perl->Iargvoutgv, param); | |
12914 | PL_argvout_stack = av_dup_inc(proto_perl->Iargvout_stack, param); | |
1d7c1841 | 12915 | |
bd81e77b NC |
12916 | /* shortcuts to regexp stuff */ |
12917 | PL_replgv = gv_dup(proto_perl->Ireplgv, param); | |
9660f481 | 12918 | |
bd81e77b NC |
12919 | /* shortcuts to misc objects */ |
12920 | PL_errgv = gv_dup(proto_perl->Ierrgv, param); | |
9660f481 | 12921 | |
bd81e77b NC |
12922 | /* shortcuts to debugging objects */ |
12923 | PL_DBgv = gv_dup(proto_perl->IDBgv, param); | |
12924 | PL_DBline = gv_dup(proto_perl->IDBline, param); | |
12925 | PL_DBsub = gv_dup(proto_perl->IDBsub, param); | |
12926 | PL_DBsingle = sv_dup(proto_perl->IDBsingle, param); | |
12927 | PL_DBtrace = sv_dup(proto_perl->IDBtrace, param); | |
12928 | PL_DBsignal = sv_dup(proto_perl->IDBsignal, param); | |
9660f481 | 12929 | |
bd81e77b | 12930 | /* symbol tables */ |
907b3e23 DM |
12931 | PL_defstash = hv_dup_inc(proto_perl->Idefstash, param); |
12932 | PL_curstash = hv_dup(proto_perl->Icurstash, param); | |
bd81e77b NC |
12933 | PL_debstash = hv_dup(proto_perl->Idebstash, param); |
12934 | PL_globalstash = hv_dup(proto_perl->Iglobalstash, param); | |
12935 | PL_curstname = sv_dup_inc(proto_perl->Icurstname, param); | |
12936 | ||
12937 | PL_beginav = av_dup_inc(proto_perl->Ibeginav, param); | |
12938 | PL_beginav_save = av_dup_inc(proto_perl->Ibeginav_save, param); | |
12939 | PL_checkav_save = av_dup_inc(proto_perl->Icheckav_save, param); | |
3c10abe3 AG |
12940 | PL_unitcheckav = av_dup_inc(proto_perl->Iunitcheckav, param); |
12941 | PL_unitcheckav_save = av_dup_inc(proto_perl->Iunitcheckav_save, param); | |
bd81e77b NC |
12942 | PL_endav = av_dup_inc(proto_perl->Iendav, param); |
12943 | PL_checkav = av_dup_inc(proto_perl->Icheckav, param); | |
12944 | PL_initav = av_dup_inc(proto_perl->Iinitav, param); | |
12945 | ||
12946 | PL_sub_generation = proto_perl->Isub_generation; | |
dd69841b | 12947 | PL_isarev = hv_dup_inc(proto_perl->Iisarev, param); |
bd81e77b NC |
12948 | |
12949 | /* funky return mechanisms */ | |
12950 | PL_forkprocess = proto_perl->Iforkprocess; | |
12951 | ||
12952 | /* subprocess state */ | |
12953 | PL_fdpid = av_dup_inc(proto_perl->Ifdpid, param); | |
12954 | ||
12955 | /* internal state */ | |
12956 | PL_maxo = proto_perl->Imaxo; | |
12957 | if (proto_perl->Iop_mask) | |
12958 | PL_op_mask = SAVEPVN(proto_perl->Iop_mask, PL_maxo); | |
12959 | else | |
bd61b366 | 12960 | PL_op_mask = NULL; |
bd81e77b NC |
12961 | /* PL_asserting = proto_perl->Iasserting; */ |
12962 | ||
12963 | /* current interpreter roots */ | |
12964 | PL_main_cv = cv_dup_inc(proto_perl->Imain_cv, param); | |
d3c72c2a | 12965 | OP_REFCNT_LOCK; |
bd81e77b | 12966 | PL_main_root = OpREFCNT_inc(proto_perl->Imain_root); |
d3c72c2a | 12967 | OP_REFCNT_UNLOCK; |
bd81e77b NC |
12968 | PL_main_start = proto_perl->Imain_start; |
12969 | PL_eval_root = proto_perl->Ieval_root; | |
12970 | PL_eval_start = proto_perl->Ieval_start; | |
12971 | ||
12972 | /* runtime control stuff */ | |
12973 | PL_curcopdb = (COP*)any_dup(proto_perl->Icurcopdb, proto_perl); | |
bd81e77b NC |
12974 | |
12975 | PL_filemode = proto_perl->Ifilemode; | |
12976 | PL_lastfd = proto_perl->Ilastfd; | |
12977 | PL_oldname = proto_perl->Ioldname; /* XXX not quite right */ | |
12978 | PL_Argv = NULL; | |
bd61b366 | 12979 | PL_Cmd = NULL; |
bd81e77b | 12980 | PL_gensym = proto_perl->Igensym; |
bd81e77b NC |
12981 | PL_preambleav = av_dup_inc(proto_perl->Ipreambleav, param); |
12982 | PL_laststatval = proto_perl->Ilaststatval; | |
12983 | PL_laststype = proto_perl->Ilaststype; | |
a0714e2c | 12984 | PL_mess_sv = NULL; |
bd81e77b NC |
12985 | |
12986 | PL_ors_sv = sv_dup_inc(proto_perl->Iors_sv, param); | |
12987 | ||
12988 | /* interpreter atexit processing */ | |
12989 | PL_exitlistlen = proto_perl->Iexitlistlen; | |
12990 | if (PL_exitlistlen) { | |
12991 | Newx(PL_exitlist, PL_exitlistlen, PerlExitListEntry); | |
12992 | Copy(proto_perl->Iexitlist, PL_exitlist, PL_exitlistlen, PerlExitListEntry); | |
9660f481 | 12993 | } |
bd81e77b NC |
12994 | else |
12995 | PL_exitlist = (PerlExitListEntry*)NULL; | |
f16dd614 DM |
12996 | |
12997 | PL_my_cxt_size = proto_perl->Imy_cxt_size; | |
4c901e72 | 12998 | if (PL_my_cxt_size) { |
f16dd614 DM |
12999 | Newx(PL_my_cxt_list, PL_my_cxt_size, void *); |
13000 | Copy(proto_perl->Imy_cxt_list, PL_my_cxt_list, PL_my_cxt_size, void *); | |
53d44271 | 13001 | #ifdef PERL_GLOBAL_STRUCT_PRIVATE |
bae1192d | 13002 | Newx(PL_my_cxt_keys, PL_my_cxt_size, const char *); |
53d44271 JH |
13003 | Copy(proto_perl->Imy_cxt_keys, PL_my_cxt_keys, PL_my_cxt_size, char *); |
13004 | #endif | |
f16dd614 | 13005 | } |
53d44271 | 13006 | else { |
f16dd614 | 13007 | PL_my_cxt_list = (void**)NULL; |
53d44271 | 13008 | #ifdef PERL_GLOBAL_STRUCT_PRIVATE |
bae1192d | 13009 | PL_my_cxt_keys = (const char**)NULL; |
53d44271 JH |
13010 | #endif |
13011 | } | |
bd81e77b NC |
13012 | PL_modglobal = hv_dup_inc(proto_perl->Imodglobal, param); |
13013 | PL_custom_op_names = hv_dup_inc(proto_perl->Icustom_op_names,param); | |
13014 | PL_custom_op_descs = hv_dup_inc(proto_perl->Icustom_op_descs,param); | |
1830b3d9 | 13015 | PL_custom_ops = hv_dup_inc(proto_perl->Icustom_ops, param); |
bd81e77b NC |
13016 | |
13017 | PL_profiledata = NULL; | |
9660f481 | 13018 | |
bd81e77b | 13019 | PL_compcv = cv_dup(proto_perl->Icompcv, param); |
9660f481 | 13020 | |
bd81e77b | 13021 | PAD_CLONE_VARS(proto_perl, param); |
9660f481 | 13022 | |
bd81e77b NC |
13023 | #ifdef HAVE_INTERP_INTERN |
13024 | sys_intern_dup(&proto_perl->Isys_intern, &PL_sys_intern); | |
13025 | #endif | |
645c22ef | 13026 | |
bd81e77b NC |
13027 | /* more statics moved here */ |
13028 | PL_generation = proto_perl->Igeneration; | |
13029 | PL_DBcv = cv_dup(proto_perl->IDBcv, param); | |
645c22ef | 13030 | |
bd81e77b NC |
13031 | PL_in_clean_objs = proto_perl->Iin_clean_objs; |
13032 | PL_in_clean_all = proto_perl->Iin_clean_all; | |
6a78b4db | 13033 | |
bd81e77b NC |
13034 | PL_uid = proto_perl->Iuid; |
13035 | PL_euid = proto_perl->Ieuid; | |
13036 | PL_gid = proto_perl->Igid; | |
13037 | PL_egid = proto_perl->Iegid; | |
13038 | PL_nomemok = proto_perl->Inomemok; | |
13039 | PL_an = proto_perl->Ian; | |
13040 | PL_evalseq = proto_perl->Ievalseq; | |
13041 | PL_origenviron = proto_perl->Iorigenviron; /* XXX not quite right */ | |
13042 | PL_origalen = proto_perl->Iorigalen; | |
13043 | #ifdef PERL_USES_PL_PIDSTATUS | |
13044 | PL_pidstatus = newHV(); /* XXX flag for cloning? */ | |
13045 | #endif | |
13046 | PL_osname = SAVEPV(proto_perl->Iosname); | |
13047 | PL_sighandlerp = proto_perl->Isighandlerp; | |
6a78b4db | 13048 | |
bd81e77b | 13049 | PL_runops = proto_perl->Irunops; |
6a78b4db | 13050 | |
199e78b7 DM |
13051 | PL_parser = parser_dup(proto_perl->Iparser, param); |
13052 | ||
f0c5aa00 DM |
13053 | /* XXX this only works if the saved cop has already been cloned */ |
13054 | if (proto_perl->Iparser) { | |
13055 | PL_parser->saved_curcop = (COP*)any_dup( | |
13056 | proto_perl->Iparser->saved_curcop, | |
13057 | proto_perl); | |
13058 | } | |
13059 | ||
bd81e77b NC |
13060 | PL_subline = proto_perl->Isubline; |
13061 | PL_subname = sv_dup_inc(proto_perl->Isubname, param); | |
c43294b8 | 13062 | |
bd81e77b NC |
13063 | #ifdef FCRYPT |
13064 | PL_cryptseen = proto_perl->Icryptseen; | |
13065 | #endif | |
1d7c1841 | 13066 | |
bd81e77b | 13067 | PL_hints = proto_perl->Ihints; |
1d7c1841 | 13068 | |
bd81e77b | 13069 | PL_amagic_generation = proto_perl->Iamagic_generation; |
d2d73c3e | 13070 | |
bd81e77b NC |
13071 | #ifdef USE_LOCALE_COLLATE |
13072 | PL_collation_ix = proto_perl->Icollation_ix; | |
13073 | PL_collation_name = SAVEPV(proto_perl->Icollation_name); | |
13074 | PL_collation_standard = proto_perl->Icollation_standard; | |
13075 | PL_collxfrm_base = proto_perl->Icollxfrm_base; | |
13076 | PL_collxfrm_mult = proto_perl->Icollxfrm_mult; | |
13077 | #endif /* USE_LOCALE_COLLATE */ | |
1d7c1841 | 13078 | |
bd81e77b NC |
13079 | #ifdef USE_LOCALE_NUMERIC |
13080 | PL_numeric_name = SAVEPV(proto_perl->Inumeric_name); | |
13081 | PL_numeric_standard = proto_perl->Inumeric_standard; | |
13082 | PL_numeric_local = proto_perl->Inumeric_local; | |
13083 | PL_numeric_radix_sv = sv_dup_inc(proto_perl->Inumeric_radix_sv, param); | |
13084 | #endif /* !USE_LOCALE_NUMERIC */ | |
1d7c1841 | 13085 | |
bd81e77b NC |
13086 | /* utf8 character classes */ |
13087 | PL_utf8_alnum = sv_dup_inc(proto_perl->Iutf8_alnum, param); | |
bd81e77b NC |
13088 | PL_utf8_ascii = sv_dup_inc(proto_perl->Iutf8_ascii, param); |
13089 | PL_utf8_alpha = sv_dup_inc(proto_perl->Iutf8_alpha, param); | |
13090 | PL_utf8_space = sv_dup_inc(proto_perl->Iutf8_space, param); | |
13091 | PL_utf8_cntrl = sv_dup_inc(proto_perl->Iutf8_cntrl, param); | |
13092 | PL_utf8_graph = sv_dup_inc(proto_perl->Iutf8_graph, param); | |
13093 | PL_utf8_digit = sv_dup_inc(proto_perl->Iutf8_digit, param); | |
13094 | PL_utf8_upper = sv_dup_inc(proto_perl->Iutf8_upper, param); | |
13095 | PL_utf8_lower = sv_dup_inc(proto_perl->Iutf8_lower, param); | |
13096 | PL_utf8_print = sv_dup_inc(proto_perl->Iutf8_print, param); | |
13097 | PL_utf8_punct = sv_dup_inc(proto_perl->Iutf8_punct, param); | |
13098 | PL_utf8_xdigit = sv_dup_inc(proto_perl->Iutf8_xdigit, param); | |
13099 | PL_utf8_mark = sv_dup_inc(proto_perl->Iutf8_mark, param); | |
37e2e78e KW |
13100 | PL_utf8_X_begin = sv_dup_inc(proto_perl->Iutf8_X_begin, param); |
13101 | PL_utf8_X_extend = sv_dup_inc(proto_perl->Iutf8_X_extend, param); | |
13102 | PL_utf8_X_prepend = sv_dup_inc(proto_perl->Iutf8_X_prepend, param); | |
13103 | PL_utf8_X_non_hangul = sv_dup_inc(proto_perl->Iutf8_X_non_hangul, param); | |
13104 | PL_utf8_X_L = sv_dup_inc(proto_perl->Iutf8_X_L, param); | |
13105 | PL_utf8_X_LV = sv_dup_inc(proto_perl->Iutf8_X_LV, param); | |
13106 | PL_utf8_X_LVT = sv_dup_inc(proto_perl->Iutf8_X_LVT, param); | |
13107 | PL_utf8_X_T = sv_dup_inc(proto_perl->Iutf8_X_T, param); | |
13108 | PL_utf8_X_V = sv_dup_inc(proto_perl->Iutf8_X_V, param); | |
13109 | PL_utf8_X_LV_LVT_V = sv_dup_inc(proto_perl->Iutf8_X_LV_LVT_V, param); | |
bd81e77b NC |
13110 | PL_utf8_toupper = sv_dup_inc(proto_perl->Iutf8_toupper, param); |
13111 | PL_utf8_totitle = sv_dup_inc(proto_perl->Iutf8_totitle, param); | |
13112 | PL_utf8_tolower = sv_dup_inc(proto_perl->Iutf8_tolower, param); | |
13113 | PL_utf8_tofold = sv_dup_inc(proto_perl->Iutf8_tofold, param); | |
13114 | PL_utf8_idstart = sv_dup_inc(proto_perl->Iutf8_idstart, param); | |
13115 | PL_utf8_idcont = sv_dup_inc(proto_perl->Iutf8_idcont, param); | |
1d7c1841 | 13116 | |
bd81e77b NC |
13117 | /* Did the locale setup indicate UTF-8? */ |
13118 | PL_utf8locale = proto_perl->Iutf8locale; | |
13119 | /* Unicode features (see perlrun/-C) */ | |
13120 | PL_unicode = proto_perl->Iunicode; | |
1d7c1841 | 13121 | |
bd81e77b NC |
13122 | /* Pre-5.8 signals control */ |
13123 | PL_signals = proto_perl->Isignals; | |
1d7c1841 | 13124 | |
bd81e77b NC |
13125 | /* times() ticks per second */ |
13126 | PL_clocktick = proto_perl->Iclocktick; | |
1d7c1841 | 13127 | |
bd81e77b NC |
13128 | /* Recursion stopper for PerlIO_find_layer */ |
13129 | PL_in_load_module = proto_perl->Iin_load_module; | |
8df990a8 | 13130 | |
bd81e77b NC |
13131 | /* sort() routine */ |
13132 | PL_sort_RealCmp = proto_perl->Isort_RealCmp; | |
e5dd39fc | 13133 | |
bd81e77b NC |
13134 | /* Not really needed/useful since the reenrant_retint is "volatile", |
13135 | * but do it for consistency's sake. */ | |
13136 | PL_reentrant_retint = proto_perl->Ireentrant_retint; | |
1d7c1841 | 13137 | |
bd81e77b NC |
13138 | /* Hooks to shared SVs and locks. */ |
13139 | PL_sharehook = proto_perl->Isharehook; | |
13140 | PL_lockhook = proto_perl->Ilockhook; | |
13141 | PL_unlockhook = proto_perl->Iunlockhook; | |
13142 | PL_threadhook = proto_perl->Ithreadhook; | |
eba16661 | 13143 | PL_destroyhook = proto_perl->Idestroyhook; |
92f022bb | 13144 | PL_signalhook = proto_perl->Isignalhook; |
1d7c1841 | 13145 | |
bd81e77b NC |
13146 | #ifdef THREADS_HAVE_PIDS |
13147 | PL_ppid = proto_perl->Ippid; | |
13148 | #endif | |
1d7c1841 | 13149 | |
bd81e77b | 13150 | /* swatch cache */ |
5c284bb0 | 13151 | PL_last_swash_hv = NULL; /* reinits on demand */ |
bd81e77b NC |
13152 | PL_last_swash_klen = 0; |
13153 | PL_last_swash_key[0]= '\0'; | |
13154 | PL_last_swash_tmps = (U8*)NULL; | |
13155 | PL_last_swash_slen = 0; | |
1d7c1841 | 13156 | |
bd81e77b NC |
13157 | PL_glob_index = proto_perl->Iglob_index; |
13158 | PL_srand_called = proto_perl->Isrand_called; | |
05ec9bb3 | 13159 | |
bd81e77b NC |
13160 | if (proto_perl->Ipsig_pend) { |
13161 | Newxz(PL_psig_pend, SIG_SIZE, int); | |
13162 | } | |
13163 | else { | |
13164 | PL_psig_pend = (int*)NULL; | |
13165 | } | |
05ec9bb3 | 13166 | |
d525a7b2 NC |
13167 | if (proto_perl->Ipsig_name) { |
13168 | Newx(PL_psig_name, 2 * SIG_SIZE, SV*); | |
13169 | sv_dup_inc_multiple(proto_perl->Ipsig_name, PL_psig_name, 2 * SIG_SIZE, | |
538f2e76 | 13170 | param); |
d525a7b2 | 13171 | PL_psig_ptr = PL_psig_name + SIG_SIZE; |
bd81e77b NC |
13172 | } |
13173 | else { | |
13174 | PL_psig_ptr = (SV**)NULL; | |
13175 | PL_psig_name = (SV**)NULL; | |
13176 | } | |
05ec9bb3 | 13177 | |
907b3e23 | 13178 | /* intrpvar.h stuff */ |
1d7c1841 | 13179 | |
bd81e77b NC |
13180 | if (flags & CLONEf_COPY_STACKS) { |
13181 | /* next allocation will be PL_tmps_stack[PL_tmps_ix+1] */ | |
907b3e23 DM |
13182 | PL_tmps_ix = proto_perl->Itmps_ix; |
13183 | PL_tmps_max = proto_perl->Itmps_max; | |
13184 | PL_tmps_floor = proto_perl->Itmps_floor; | |
e92c6be8 | 13185 | Newx(PL_tmps_stack, PL_tmps_max, SV*); |
1d8a41fe JD |
13186 | sv_dup_inc_multiple(proto_perl->Itmps_stack, PL_tmps_stack, |
13187 | PL_tmps_ix+1, param); | |
d2d73c3e | 13188 | |
bd81e77b | 13189 | /* next PUSHMARK() sets *(PL_markstack_ptr+1) */ |
907b3e23 | 13190 | i = proto_perl->Imarkstack_max - proto_perl->Imarkstack; |
bd81e77b | 13191 | Newxz(PL_markstack, i, I32); |
907b3e23 DM |
13192 | PL_markstack_max = PL_markstack + (proto_perl->Imarkstack_max |
13193 | - proto_perl->Imarkstack); | |
13194 | PL_markstack_ptr = PL_markstack + (proto_perl->Imarkstack_ptr | |
13195 | - proto_perl->Imarkstack); | |
13196 | Copy(proto_perl->Imarkstack, PL_markstack, | |
bd81e77b | 13197 | PL_markstack_ptr - PL_markstack + 1, I32); |
d2d73c3e | 13198 | |
bd81e77b NC |
13199 | /* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix] |
13200 | * NOTE: unlike the others! */ | |
907b3e23 DM |
13201 | PL_scopestack_ix = proto_perl->Iscopestack_ix; |
13202 | PL_scopestack_max = proto_perl->Iscopestack_max; | |
bd81e77b | 13203 | Newxz(PL_scopestack, PL_scopestack_max, I32); |
907b3e23 | 13204 | Copy(proto_perl->Iscopestack, PL_scopestack, PL_scopestack_ix, I32); |
d419787a | 13205 | |
cbdd5331 JD |
13206 | #ifdef DEBUGGING |
13207 | Newxz(PL_scopestack_name, PL_scopestack_max, const char *); | |
13208 | Copy(proto_perl->Iscopestack_name, PL_scopestack_name, PL_scopestack_ix, const char *); | |
13209 | #endif | |
bd81e77b | 13210 | /* NOTE: si_dup() looks at PL_markstack */ |
907b3e23 | 13211 | PL_curstackinfo = si_dup(proto_perl->Icurstackinfo, param); |
d2d73c3e | 13212 | |
bd81e77b | 13213 | /* PL_curstack = PL_curstackinfo->si_stack; */ |
907b3e23 DM |
13214 | PL_curstack = av_dup(proto_perl->Icurstack, param); |
13215 | PL_mainstack = av_dup(proto_perl->Imainstack, param); | |
1d7c1841 | 13216 | |
bd81e77b NC |
13217 | /* next PUSHs() etc. set *(PL_stack_sp+1) */ |
13218 | PL_stack_base = AvARRAY(PL_curstack); | |
907b3e23 DM |
13219 | PL_stack_sp = PL_stack_base + (proto_perl->Istack_sp |
13220 | - proto_perl->Istack_base); | |
bd81e77b | 13221 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
1d7c1841 | 13222 | |
bd81e77b NC |
13223 | /* next SSPUSHFOO() sets PL_savestack[PL_savestack_ix] |
13224 | * NOTE: unlike the others! */ | |
907b3e23 DM |
13225 | PL_savestack_ix = proto_perl->Isavestack_ix; |
13226 | PL_savestack_max = proto_perl->Isavestack_max; | |
bd81e77b NC |
13227 | /*Newxz(PL_savestack, PL_savestack_max, ANY);*/ |
13228 | PL_savestack = ss_dup(proto_perl, param); | |
13229 | } | |
13230 | else { | |
13231 | init_stacks(); | |
13232 | ENTER; /* perl_destruct() wants to LEAVE; */ | |
13233 | } | |
1d7c1841 | 13234 | |
907b3e23 | 13235 | PL_start_env = proto_perl->Istart_env; /* XXXXXX */ |
bd81e77b | 13236 | PL_top_env = &PL_start_env; |
1d7c1841 | 13237 | |
907b3e23 | 13238 | PL_op = proto_perl->Iop; |
4a4c6fe3 | 13239 | |
a0714e2c | 13240 | PL_Sv = NULL; |
bd81e77b | 13241 | PL_Xpv = (XPV*)NULL; |
24792b8d | 13242 | my_perl->Ina = proto_perl->Ina; |
1fcf4c12 | 13243 | |
907b3e23 DM |
13244 | PL_statbuf = proto_perl->Istatbuf; |
13245 | PL_statcache = proto_perl->Istatcache; | |
13246 | PL_statgv = gv_dup(proto_perl->Istatgv, param); | |
13247 | PL_statname = sv_dup_inc(proto_perl->Istatname, param); | |
bd81e77b | 13248 | #ifdef HAS_TIMES |
907b3e23 | 13249 | PL_timesbuf = proto_perl->Itimesbuf; |
bd81e77b | 13250 | #endif |
1d7c1841 | 13251 | |
907b3e23 DM |
13252 | PL_tainted = proto_perl->Itainted; |
13253 | PL_curpm = proto_perl->Icurpm; /* XXX No PMOP ref count */ | |
13254 | PL_rs = sv_dup_inc(proto_perl->Irs, param); | |
13255 | PL_last_in_gv = gv_dup(proto_perl->Ilast_in_gv, param); | |
907b3e23 DM |
13256 | PL_defoutgv = gv_dup_inc(proto_perl->Idefoutgv, param); |
13257 | PL_chopset = proto_perl->Ichopset; /* XXX never deallocated */ | |
13258 | PL_toptarget = sv_dup_inc(proto_perl->Itoptarget, param); | |
13259 | PL_bodytarget = sv_dup_inc(proto_perl->Ibodytarget, param); | |
13260 | PL_formtarget = sv_dup(proto_perl->Iformtarget, param); | |
13261 | ||
febb3a6d | 13262 | PL_restartjmpenv = proto_perl->Irestartjmpenv; |
907b3e23 DM |
13263 | PL_restartop = proto_perl->Irestartop; |
13264 | PL_in_eval = proto_perl->Iin_eval; | |
13265 | PL_delaymagic = proto_perl->Idelaymagic; | |
9ebf26ad | 13266 | PL_phase = proto_perl->Iphase; |
907b3e23 DM |
13267 | PL_localizing = proto_perl->Ilocalizing; |
13268 | ||
13269 | PL_errors = sv_dup_inc(proto_perl->Ierrors, param); | |
4608196e | 13270 | PL_hv_fetch_ent_mh = NULL; |
907b3e23 | 13271 | PL_modcount = proto_perl->Imodcount; |
5f66b61c | 13272 | PL_lastgotoprobe = NULL; |
907b3e23 | 13273 | PL_dumpindent = proto_perl->Idumpindent; |
1d7c1841 | 13274 | |
907b3e23 DM |
13275 | PL_sortcop = (OP*)any_dup(proto_perl->Isortcop, proto_perl); |
13276 | PL_sortstash = hv_dup(proto_perl->Isortstash, param); | |
13277 | PL_firstgv = gv_dup(proto_perl->Ifirstgv, param); | |
13278 | PL_secondgv = gv_dup(proto_perl->Isecondgv, param); | |
bd61b366 | 13279 | PL_efloatbuf = NULL; /* reinits on demand */ |
bd81e77b | 13280 | PL_efloatsize = 0; /* reinits on demand */ |
d2d73c3e | 13281 | |
bd81e77b | 13282 | /* regex stuff */ |
1d7c1841 | 13283 | |
bd81e77b NC |
13284 | PL_screamfirst = NULL; |
13285 | PL_screamnext = NULL; | |
13286 | PL_maxscream = -1; /* reinits on demand */ | |
a0714e2c | 13287 | PL_lastscream = NULL; |
1d7c1841 | 13288 | |
1d7c1841 | 13289 | |
907b3e23 | 13290 | PL_regdummy = proto_perl->Iregdummy; |
bd81e77b NC |
13291 | PL_colorset = 0; /* reinits PL_colors[] */ |
13292 | /*PL_colors[6] = {0,0,0,0,0,0};*/ | |
1d7c1841 | 13293 | |
84da74a7 | 13294 | |
1d7c1841 | 13295 | |
bd81e77b | 13296 | /* Pluggable optimizer */ |
907b3e23 | 13297 | PL_peepp = proto_perl->Ipeepp; |
1a0a2ba9 | 13298 | PL_rpeepp = proto_perl->Irpeepp; |
f37b8c3f VP |
13299 | /* op_free() hook */ |
13300 | PL_opfreehook = proto_perl->Iopfreehook; | |
1d7c1841 | 13301 | |
bd81e77b | 13302 | PL_stashcache = newHV(); |
1d7c1841 | 13303 | |
b7185faf | 13304 | PL_watchaddr = (char **) ptr_table_fetch(PL_ptr_table, |
907b3e23 | 13305 | proto_perl->Iwatchaddr); |
b7185faf DM |
13306 | PL_watchok = PL_watchaddr ? * PL_watchaddr : NULL; |
13307 | if (PL_debug && PL_watchaddr) { | |
13308 | PerlIO_printf(Perl_debug_log, | |
13309 | "WATCHING: %"UVxf" cloned as %"UVxf" with value %"UVxf"\n", | |
907b3e23 | 13310 | PTR2UV(proto_perl->Iwatchaddr), PTR2UV(PL_watchaddr), |
b7185faf DM |
13311 | PTR2UV(PL_watchok)); |
13312 | } | |
13313 | ||
a3e6e81e | 13314 | PL_registered_mros = hv_dup_inc(proto_perl->Iregistered_mros, param); |
1930840b | 13315 | PL_blockhooks = av_dup_inc(proto_perl->Iblockhooks, param); |
2726813d | 13316 | PL_utf8_foldclosures = hv_dup_inc(proto_perl->Iutf8_foldclosures, param); |
a3e6e81e | 13317 | |
bd81e77b NC |
13318 | /* Call the ->CLONE method, if it exists, for each of the stashes |
13319 | identified by sv_dup() above. | |
13320 | */ | |
13321 | while(av_len(param->stashes) != -1) { | |
85fbaab2 | 13322 | HV* const stash = MUTABLE_HV(av_shift(param->stashes)); |
bd81e77b NC |
13323 | GV* const cloner = gv_fetchmethod_autoload(stash, "CLONE", 0); |
13324 | if (cloner && GvCV(cloner)) { | |
13325 | dSP; | |
13326 | ENTER; | |
13327 | SAVETMPS; | |
13328 | PUSHMARK(SP); | |
6e449a3a | 13329 | mXPUSHs(newSVhek(HvNAME_HEK(stash))); |
bd81e77b | 13330 | PUTBACK; |
daba3364 | 13331 | call_sv(MUTABLE_SV(GvCV(cloner)), G_DISCARD); |
bd81e77b NC |
13332 | FREETMPS; |
13333 | LEAVE; | |
13334 | } | |
1d7c1841 | 13335 | } |
1d7c1841 | 13336 | |
b0b93b3c DM |
13337 | if (!(flags & CLONEf_KEEP_PTR_TABLE)) { |
13338 | ptr_table_free(PL_ptr_table); | |
13339 | PL_ptr_table = NULL; | |
13340 | } | |
13341 | ||
d08d57ef | 13342 | if (!(flags & CLONEf_COPY_STACKS)) { |
e4295668 | 13343 | unreferenced_to_tmp_stack(param->unreferenced); |
d08d57ef | 13344 | } |
b0b93b3c | 13345 | |
bd81e77b | 13346 | SvREFCNT_dec(param->stashes); |
1d7c1841 | 13347 | |
bd81e77b NC |
13348 | /* orphaned? eg threads->new inside BEGIN or use */ |
13349 | if (PL_compcv && ! SvREFCNT(PL_compcv)) { | |
b37c2d43 | 13350 | SvREFCNT_inc_simple_void(PL_compcv); |
bd81e77b NC |
13351 | SAVEFREESV(PL_compcv); |
13352 | } | |
dd2155a4 | 13353 | |
bd81e77b NC |
13354 | return my_perl; |
13355 | } | |
1d7c1841 | 13356 | |
e4295668 NC |
13357 | static void |
13358 | S_unreferenced_to_tmp_stack(pTHX_ AV *const unreferenced) | |
13359 | { | |
13360 | PERL_ARGS_ASSERT_UNREFERENCED_TO_TMP_STACK; | |
13361 | ||
13362 | if (AvFILLp(unreferenced) > -1) { | |
13363 | SV **svp = AvARRAY(unreferenced); | |
13364 | SV **const last = svp + AvFILLp(unreferenced); | |
13365 | SSize_t count = 0; | |
13366 | ||
13367 | do { | |
04518cc3 | 13368 | if (SvREFCNT(*svp) == 1) |
e4295668 NC |
13369 | ++count; |
13370 | } while (++svp <= last); | |
13371 | ||
13372 | EXTEND_MORTAL(count); | |
13373 | svp = AvARRAY(unreferenced); | |
13374 | ||
13375 | do { | |
04518cc3 NC |
13376 | if (SvREFCNT(*svp) == 1) { |
13377 | /* Our reference is the only one to this SV. This means that | |
13378 | in this thread, the scalar effectively has a 0 reference. | |
13379 | That doesn't work (cleanup never happens), so donate our | |
13380 | reference to it onto the save stack. */ | |
13381 | PL_tmps_stack[++PL_tmps_ix] = *svp; | |
13382 | } else { | |
13383 | /* As an optimisation, because we are already walking the | |
13384 | entire array, instead of above doing either | |
13385 | SvREFCNT_inc(*svp) or *svp = &PL_sv_undef, we can instead | |
13386 | release our reference to the scalar, so that at the end of | |
13387 | the array owns zero references to the scalars it happens to | |
13388 | point to. We are effectively converting the array from | |
13389 | AvREAL() on to AvREAL() off. This saves the av_clear() | |
13390 | (triggered by the SvREFCNT_dec(unreferenced) below) from | |
13391 | walking the array a second time. */ | |
13392 | SvREFCNT_dec(*svp); | |
13393 | } | |
13394 | ||
e4295668 | 13395 | } while (++svp <= last); |
04518cc3 | 13396 | AvREAL_off(unreferenced); |
e4295668 NC |
13397 | } |
13398 | SvREFCNT_dec(unreferenced); | |
13399 | } | |
13400 | ||
f7abe70b NC |
13401 | void |
13402 | Perl_clone_params_del(CLONE_PARAMS *param) | |
13403 | { | |
90d4a638 NC |
13404 | /* This seemingly funky ordering keeps the build with PERL_GLOBAL_STRUCT |
13405 | happy: */ | |
1db366cc NC |
13406 | PerlInterpreter *const to = param->new_perl; |
13407 | dTHXa(to); | |
90d4a638 | 13408 | PerlInterpreter *const was = PERL_GET_THX; |
f7abe70b NC |
13409 | |
13410 | PERL_ARGS_ASSERT_CLONE_PARAMS_DEL; | |
13411 | ||
1db366cc NC |
13412 | if (was != to) { |
13413 | PERL_SET_THX(to); | |
13414 | } | |
f7abe70b | 13415 | |
1db366cc | 13416 | SvREFCNT_dec(param->stashes); |
e4295668 NC |
13417 | if (param->unreferenced) |
13418 | unreferenced_to_tmp_stack(param->unreferenced); | |
f7abe70b | 13419 | |
1db366cc | 13420 | Safefree(param); |
f7abe70b | 13421 | |
1db366cc NC |
13422 | if (was != to) { |
13423 | PERL_SET_THX(was); | |
f7abe70b NC |
13424 | } |
13425 | } | |
13426 | ||
13427 | CLONE_PARAMS * | |
13428 | Perl_clone_params_new(PerlInterpreter *const from, PerlInterpreter *const to) | |
13429 | { | |
90d4a638 | 13430 | dVAR; |
f7abe70b NC |
13431 | /* Need to play this game, as newAV() can call safesysmalloc(), and that |
13432 | does a dTHX; to get the context from thread local storage. | |
13433 | FIXME - under PERL_CORE Newx(), Safefree() and friends should expand to | |
13434 | a version that passes in my_perl. */ | |
13435 | PerlInterpreter *const was = PERL_GET_THX; | |
13436 | CLONE_PARAMS *param; | |
f7abe70b NC |
13437 | |
13438 | PERL_ARGS_ASSERT_CLONE_PARAMS_NEW; | |
13439 | ||
13440 | if (was != to) { | |
13441 | PERL_SET_THX(to); | |
13442 | } | |
13443 | ||
13444 | /* Given that we've set the context, we can do this unshared. */ | |
13445 | Newx(param, 1, CLONE_PARAMS); | |
13446 | ||
13447 | param->flags = 0; | |
13448 | param->proto_perl = from; | |
1db366cc | 13449 | param->new_perl = to; |
f7abe70b NC |
13450 | param->stashes = (AV *)Perl_newSV_type(to, SVt_PVAV); |
13451 | AvREAL_off(param->stashes); | |
d08d57ef | 13452 | param->unreferenced = (AV *)Perl_newSV_type(to, SVt_PVAV); |
f7abe70b | 13453 | |
f7abe70b NC |
13454 | if (was != to) { |
13455 | PERL_SET_THX(was); | |
13456 | } | |
13457 | return param; | |
13458 | } | |
13459 | ||
bd81e77b | 13460 | #endif /* USE_ITHREADS */ |
1d7c1841 | 13461 | |
bd81e77b NC |
13462 | /* |
13463 | =head1 Unicode Support | |
1d7c1841 | 13464 | |
bd81e77b | 13465 | =for apidoc sv_recode_to_utf8 |
1d7c1841 | 13466 | |
bd81e77b NC |
13467 | The encoding is assumed to be an Encode object, on entry the PV |
13468 | of the sv is assumed to be octets in that encoding, and the sv | |
13469 | will be converted into Unicode (and UTF-8). | |
1d7c1841 | 13470 | |
bd81e77b NC |
13471 | If the sv already is UTF-8 (or if it is not POK), or if the encoding |
13472 | is not a reference, nothing is done to the sv. If the encoding is not | |
13473 | an C<Encode::XS> Encoding object, bad things will happen. | |
13474 | (See F<lib/encoding.pm> and L<Encode>). | |
1d7c1841 | 13475 | |
bd81e77b | 13476 | The PV of the sv is returned. |
1d7c1841 | 13477 | |
bd81e77b | 13478 | =cut */ |
1d7c1841 | 13479 | |
bd81e77b NC |
13480 | char * |
13481 | Perl_sv_recode_to_utf8(pTHX_ SV *sv, SV *encoding) | |
13482 | { | |
13483 | dVAR; | |
7918f24d NC |
13484 | |
13485 | PERL_ARGS_ASSERT_SV_RECODE_TO_UTF8; | |
13486 | ||
bd81e77b NC |
13487 | if (SvPOK(sv) && !SvUTF8(sv) && !IN_BYTES && SvROK(encoding)) { |
13488 | SV *uni; | |
13489 | STRLEN len; | |
13490 | const char *s; | |
13491 | dSP; | |
13492 | ENTER; | |
13493 | SAVETMPS; | |
13494 | save_re_context(); | |
13495 | PUSHMARK(sp); | |
13496 | EXTEND(SP, 3); | |
13497 | XPUSHs(encoding); | |
13498 | XPUSHs(sv); | |
13499 | /* | |
13500 | NI-S 2002/07/09 | |
13501 | Passing sv_yes is wrong - it needs to be or'ed set of constants | |
13502 | for Encode::XS, while UTf-8 decode (currently) assumes a true value means | |
13503 | remove converted chars from source. | |
1d7c1841 | 13504 | |
bd81e77b | 13505 | Both will default the value - let them. |
1d7c1841 | 13506 | |
bd81e77b NC |
13507 | XPUSHs(&PL_sv_yes); |
13508 | */ | |
13509 | PUTBACK; | |
13510 | call_method("decode", G_SCALAR); | |
13511 | SPAGAIN; | |
13512 | uni = POPs; | |
13513 | PUTBACK; | |
13514 | s = SvPV_const(uni, len); | |
13515 | if (s != SvPVX_const(sv)) { | |
13516 | SvGROW(sv, len + 1); | |
13517 | Move(s, SvPVX(sv), len + 1, char); | |
13518 | SvCUR_set(sv, len); | |
13519 | } | |
13520 | FREETMPS; | |
13521 | LEAVE; | |
13522 | SvUTF8_on(sv); | |
13523 | return SvPVX(sv); | |
389edf32 | 13524 | } |
bd81e77b NC |
13525 | return SvPOKp(sv) ? SvPVX(sv) : NULL; |
13526 | } | |
1d7c1841 | 13527 | |
bd81e77b NC |
13528 | /* |
13529 | =for apidoc sv_cat_decode | |
1d7c1841 | 13530 | |
bd81e77b NC |
13531 | The encoding is assumed to be an Encode object, the PV of the ssv is |
13532 | assumed to be octets in that encoding and decoding the input starts | |
13533 | from the position which (PV + *offset) pointed to. The dsv will be | |
13534 | concatenated the decoded UTF-8 string from ssv. Decoding will terminate | |
13535 | when the string tstr appears in decoding output or the input ends on | |
13536 | the PV of the ssv. The value which the offset points will be modified | |
13537 | to the last input position on the ssv. | |
1d7c1841 | 13538 | |
bd81e77b | 13539 | Returns TRUE if the terminator was found, else returns FALSE. |
1d7c1841 | 13540 | |
bd81e77b NC |
13541 | =cut */ |
13542 | ||
13543 | bool | |
13544 | Perl_sv_cat_decode(pTHX_ SV *dsv, SV *encoding, | |
13545 | SV *ssv, int *offset, char *tstr, int tlen) | |
13546 | { | |
13547 | dVAR; | |
13548 | bool ret = FALSE; | |
7918f24d NC |
13549 | |
13550 | PERL_ARGS_ASSERT_SV_CAT_DECODE; | |
13551 | ||
bd81e77b NC |
13552 | if (SvPOK(ssv) && SvPOK(dsv) && SvROK(encoding) && offset) { |
13553 | SV *offsv; | |
13554 | dSP; | |
13555 | ENTER; | |
13556 | SAVETMPS; | |
13557 | save_re_context(); | |
13558 | PUSHMARK(sp); | |
13559 | EXTEND(SP, 6); | |
13560 | XPUSHs(encoding); | |
13561 | XPUSHs(dsv); | |
13562 | XPUSHs(ssv); | |
6e449a3a MHM |
13563 | offsv = newSViv(*offset); |
13564 | mXPUSHs(offsv); | |
13565 | mXPUSHp(tstr, tlen); | |
bd81e77b NC |
13566 | PUTBACK; |
13567 | call_method("cat_decode", G_SCALAR); | |
13568 | SPAGAIN; | |
13569 | ret = SvTRUE(TOPs); | |
13570 | *offset = SvIV(offsv); | |
13571 | PUTBACK; | |
13572 | FREETMPS; | |
13573 | LEAVE; | |
389edf32 | 13574 | } |
bd81e77b NC |
13575 | else |
13576 | Perl_croak(aTHX_ "Invalid argument to sv_cat_decode"); | |
13577 | return ret; | |
1d7c1841 | 13578 | |
bd81e77b | 13579 | } |
1d7c1841 | 13580 | |
bd81e77b NC |
13581 | /* --------------------------------------------------------------------- |
13582 | * | |
13583 | * support functions for report_uninit() | |
13584 | */ | |
1d7c1841 | 13585 | |
bd81e77b NC |
13586 | /* the maxiumum size of array or hash where we will scan looking |
13587 | * for the undefined element that triggered the warning */ | |
1d7c1841 | 13588 | |
bd81e77b | 13589 | #define FUV_MAX_SEARCH_SIZE 1000 |
1d7c1841 | 13590 | |
bd81e77b NC |
13591 | /* Look for an entry in the hash whose value has the same SV as val; |
13592 | * If so, return a mortal copy of the key. */ | |
1d7c1841 | 13593 | |
bd81e77b | 13594 | STATIC SV* |
6c1b357c | 13595 | S_find_hash_subscript(pTHX_ const HV *const hv, const SV *const val) |
bd81e77b NC |
13596 | { |
13597 | dVAR; | |
13598 | register HE **array; | |
13599 | I32 i; | |
6c3182a5 | 13600 | |
7918f24d NC |
13601 | PERL_ARGS_ASSERT_FIND_HASH_SUBSCRIPT; |
13602 | ||
bd81e77b NC |
13603 | if (!hv || SvMAGICAL(hv) || !HvARRAY(hv) || |
13604 | (HvTOTALKEYS(hv) > FUV_MAX_SEARCH_SIZE)) | |
a0714e2c | 13605 | return NULL; |
6c3182a5 | 13606 | |
bd81e77b | 13607 | array = HvARRAY(hv); |
6c3182a5 | 13608 | |
bd81e77b NC |
13609 | for (i=HvMAX(hv); i>0; i--) { |
13610 | register HE *entry; | |
13611 | for (entry = array[i]; entry; entry = HeNEXT(entry)) { | |
13612 | if (HeVAL(entry) != val) | |
13613 | continue; | |
13614 | if ( HeVAL(entry) == &PL_sv_undef || | |
13615 | HeVAL(entry) == &PL_sv_placeholder) | |
13616 | continue; | |
13617 | if (!HeKEY(entry)) | |
a0714e2c | 13618 | return NULL; |
bd81e77b NC |
13619 | if (HeKLEN(entry) == HEf_SVKEY) |
13620 | return sv_mortalcopy(HeKEY_sv(entry)); | |
a663657d | 13621 | return sv_2mortal(newSVhek(HeKEY_hek(entry))); |
bd81e77b NC |
13622 | } |
13623 | } | |
a0714e2c | 13624 | return NULL; |
bd81e77b | 13625 | } |
6c3182a5 | 13626 | |
bd81e77b NC |
13627 | /* Look for an entry in the array whose value has the same SV as val; |
13628 | * If so, return the index, otherwise return -1. */ | |
6c3182a5 | 13629 | |
bd81e77b | 13630 | STATIC I32 |
6c1b357c | 13631 | S_find_array_subscript(pTHX_ const AV *const av, const SV *const val) |
bd81e77b | 13632 | { |
97aff369 | 13633 | dVAR; |
7918f24d NC |
13634 | |
13635 | PERL_ARGS_ASSERT_FIND_ARRAY_SUBSCRIPT; | |
13636 | ||
bd81e77b NC |
13637 | if (!av || SvMAGICAL(av) || !AvARRAY(av) || |
13638 | (AvFILLp(av) > FUV_MAX_SEARCH_SIZE)) | |
13639 | return -1; | |
57c6e6d2 | 13640 | |
4a021917 AL |
13641 | if (val != &PL_sv_undef) { |
13642 | SV ** const svp = AvARRAY(av); | |
13643 | I32 i; | |
13644 | ||
13645 | for (i=AvFILLp(av); i>=0; i--) | |
13646 | if (svp[i] == val) | |
13647 | return i; | |
bd81e77b NC |
13648 | } |
13649 | return -1; | |
13650 | } | |
15a5279a | 13651 | |
bd81e77b NC |
13652 | /* S_varname(): return the name of a variable, optionally with a subscript. |
13653 | * If gv is non-zero, use the name of that global, along with gvtype (one | |
13654 | * of "$", "@", "%"); otherwise use the name of the lexical at pad offset | |
13655 | * targ. Depending on the value of the subscript_type flag, return: | |
13656 | */ | |
bce260cd | 13657 | |
bd81e77b NC |
13658 | #define FUV_SUBSCRIPT_NONE 1 /* "@foo" */ |
13659 | #define FUV_SUBSCRIPT_ARRAY 2 /* "$foo[aindex]" */ | |
13660 | #define FUV_SUBSCRIPT_HASH 3 /* "$foo{keyname}" */ | |
13661 | #define FUV_SUBSCRIPT_WITHIN 4 /* "within @foo" */ | |
bce260cd | 13662 | |
bd81e77b | 13663 | STATIC SV* |
6c1b357c NC |
13664 | S_varname(pTHX_ const GV *const gv, const char gvtype, PADOFFSET targ, |
13665 | const SV *const keyname, I32 aindex, int subscript_type) | |
bd81e77b | 13666 | { |
1d7c1841 | 13667 | |
bd81e77b NC |
13668 | SV * const name = sv_newmortal(); |
13669 | if (gv) { | |
13670 | char buffer[2]; | |
13671 | buffer[0] = gvtype; | |
13672 | buffer[1] = 0; | |
1d7c1841 | 13673 | |
bd81e77b | 13674 | /* as gv_fullname4(), but add literal '^' for $^FOO names */ |
66fe0623 | 13675 | |
bd81e77b | 13676 | gv_fullname4(name, gv, buffer, 0); |
1d7c1841 | 13677 | |
bd81e77b NC |
13678 | if ((unsigned int)SvPVX(name)[1] <= 26) { |
13679 | buffer[0] = '^'; | |
13680 | buffer[1] = SvPVX(name)[1] + 'A' - 1; | |
1d7c1841 | 13681 | |
bd81e77b NC |
13682 | /* Swap the 1 unprintable control character for the 2 byte pretty |
13683 | version - ie substr($name, 1, 1) = $buffer; */ | |
13684 | sv_insert(name, 1, 1, buffer, 2); | |
1d7c1841 | 13685 | } |
bd81e77b NC |
13686 | } |
13687 | else { | |
289b91d9 | 13688 | CV * const cv = find_runcv(NULL); |
bd81e77b NC |
13689 | SV *sv; |
13690 | AV *av; | |
1d7c1841 | 13691 | |
bd81e77b | 13692 | if (!cv || !CvPADLIST(cv)) |
a0714e2c | 13693 | return NULL; |
502c6561 | 13694 | av = MUTABLE_AV((*av_fetch(CvPADLIST(cv), 0, FALSE))); |
bd81e77b | 13695 | sv = *av_fetch(av, targ, FALSE); |
f8503592 | 13696 | sv_setpvn(name, SvPV_nolen_const(sv), SvCUR(sv)); |
bd81e77b | 13697 | } |
1d7c1841 | 13698 | |
bd81e77b | 13699 | if (subscript_type == FUV_SUBSCRIPT_HASH) { |
561b68a9 | 13700 | SV * const sv = newSV(0); |
bd81e77b NC |
13701 | *SvPVX(name) = '$'; |
13702 | Perl_sv_catpvf(aTHX_ name, "{%s}", | |
13703 | pv_display(sv,SvPVX_const(keyname), SvCUR(keyname), 0, 32)); | |
13704 | SvREFCNT_dec(sv); | |
13705 | } | |
13706 | else if (subscript_type == FUV_SUBSCRIPT_ARRAY) { | |
13707 | *SvPVX(name) = '$'; | |
13708 | Perl_sv_catpvf(aTHX_ name, "[%"IVdf"]", (IV)aindex); | |
13709 | } | |
84335ee9 NC |
13710 | else if (subscript_type == FUV_SUBSCRIPT_WITHIN) { |
13711 | /* We know that name has no magic, so can use 0 instead of SV_GMAGIC */ | |
13712 | Perl_sv_insert_flags(aTHX_ name, 0, 0, STR_WITH_LEN("within "), 0); | |
13713 | } | |
1d7c1841 | 13714 | |
bd81e77b NC |
13715 | return name; |
13716 | } | |
1d7c1841 | 13717 | |
1d7c1841 | 13718 | |
bd81e77b NC |
13719 | /* |
13720 | =for apidoc find_uninit_var | |
1d7c1841 | 13721 | |
bd81e77b NC |
13722 | Find the name of the undefined variable (if any) that caused the operator o |
13723 | to issue a "Use of uninitialized value" warning. | |
13724 | If match is true, only return a name if it's value matches uninit_sv. | |
13725 | So roughly speaking, if a unary operator (such as OP_COS) generates a | |
13726 | warning, then following the direct child of the op may yield an | |
13727 | OP_PADSV or OP_GV that gives the name of the undefined variable. On the | |
13728 | other hand, with OP_ADD there are two branches to follow, so we only print | |
13729 | the variable name if we get an exact match. | |
1d7c1841 | 13730 | |
bd81e77b | 13731 | The name is returned as a mortal SV. |
1d7c1841 | 13732 | |
bd81e77b NC |
13733 | Assumes that PL_op is the op that originally triggered the error, and that |
13734 | PL_comppad/PL_curpad points to the currently executing pad. | |
1d7c1841 | 13735 | |
bd81e77b NC |
13736 | =cut |
13737 | */ | |
1d7c1841 | 13738 | |
bd81e77b | 13739 | STATIC SV * |
6c1b357c NC |
13740 | S_find_uninit_var(pTHX_ const OP *const obase, const SV *const uninit_sv, |
13741 | bool match) | |
bd81e77b NC |
13742 | { |
13743 | dVAR; | |
13744 | SV *sv; | |
6c1b357c NC |
13745 | const GV *gv; |
13746 | const OP *o, *o2, *kid; | |
1d7c1841 | 13747 | |
bd81e77b NC |
13748 | if (!obase || (match && (!uninit_sv || uninit_sv == &PL_sv_undef || |
13749 | uninit_sv == &PL_sv_placeholder))) | |
a0714e2c | 13750 | return NULL; |
1d7c1841 | 13751 | |
bd81e77b | 13752 | switch (obase->op_type) { |
1d7c1841 | 13753 | |
bd81e77b NC |
13754 | case OP_RV2AV: |
13755 | case OP_RV2HV: | |
13756 | case OP_PADAV: | |
13757 | case OP_PADHV: | |
13758 | { | |
13759 | const bool pad = (obase->op_type == OP_PADAV || obase->op_type == OP_PADHV); | |
13760 | const bool hash = (obase->op_type == OP_PADHV || obase->op_type == OP_RV2HV); | |
13761 | I32 index = 0; | |
a0714e2c | 13762 | SV *keysv = NULL; |
bd81e77b | 13763 | int subscript_type = FUV_SUBSCRIPT_WITHIN; |
1d7c1841 | 13764 | |
bd81e77b NC |
13765 | if (pad) { /* @lex, %lex */ |
13766 | sv = PAD_SVl(obase->op_targ); | |
a0714e2c | 13767 | gv = NULL; |
bd81e77b NC |
13768 | } |
13769 | else { | |
13770 | if (cUNOPx(obase)->op_first->op_type == OP_GV) { | |
13771 | /* @global, %global */ | |
13772 | gv = cGVOPx_gv(cUNOPx(obase)->op_first); | |
13773 | if (!gv) | |
13774 | break; | |
daba3364 | 13775 | sv = hash ? MUTABLE_SV(GvHV(gv)): MUTABLE_SV(GvAV(gv)); |
bd81e77b NC |
13776 | } |
13777 | else /* @{expr}, %{expr} */ | |
13778 | return find_uninit_var(cUNOPx(obase)->op_first, | |
13779 | uninit_sv, match); | |
13780 | } | |
1d7c1841 | 13781 | |
bd81e77b NC |
13782 | /* attempt to find a match within the aggregate */ |
13783 | if (hash) { | |
85fbaab2 | 13784 | keysv = find_hash_subscript((const HV*)sv, uninit_sv); |
bd81e77b NC |
13785 | if (keysv) |
13786 | subscript_type = FUV_SUBSCRIPT_HASH; | |
13787 | } | |
13788 | else { | |
502c6561 | 13789 | index = find_array_subscript((const AV *)sv, uninit_sv); |
bd81e77b NC |
13790 | if (index >= 0) |
13791 | subscript_type = FUV_SUBSCRIPT_ARRAY; | |
13792 | } | |
1d7c1841 | 13793 | |
bd81e77b NC |
13794 | if (match && subscript_type == FUV_SUBSCRIPT_WITHIN) |
13795 | break; | |
1d7c1841 | 13796 | |
bd81e77b NC |
13797 | return varname(gv, hash ? '%' : '@', obase->op_targ, |
13798 | keysv, index, subscript_type); | |
13799 | } | |
1d7c1841 | 13800 | |
bd81e77b NC |
13801 | case OP_PADSV: |
13802 | if (match && PAD_SVl(obase->op_targ) != uninit_sv) | |
13803 | break; | |
a0714e2c SS |
13804 | return varname(NULL, '$', obase->op_targ, |
13805 | NULL, 0, FUV_SUBSCRIPT_NONE); | |
1d7c1841 | 13806 | |
bd81e77b NC |
13807 | case OP_GVSV: |
13808 | gv = cGVOPx_gv(obase); | |
249534c3 | 13809 | if (!gv || (match && GvSV(gv) != uninit_sv) || !GvSTASH(gv)) |
bd81e77b | 13810 | break; |
a0714e2c | 13811 | return varname(gv, '$', 0, NULL, 0, FUV_SUBSCRIPT_NONE); |
1d7c1841 | 13812 | |
bd81e77b NC |
13813 | case OP_AELEMFAST: |
13814 | if (obase->op_flags & OPf_SPECIAL) { /* lexical array */ | |
13815 | if (match) { | |
13816 | SV **svp; | |
502c6561 | 13817 | AV *av = MUTABLE_AV(PAD_SV(obase->op_targ)); |
bd81e77b NC |
13818 | if (!av || SvRMAGICAL(av)) |
13819 | break; | |
13820 | svp = av_fetch(av, (I32)obase->op_private, FALSE); | |
13821 | if (!svp || *svp != uninit_sv) | |
13822 | break; | |
13823 | } | |
a0714e2c SS |
13824 | return varname(NULL, '$', obase->op_targ, |
13825 | NULL, (I32)obase->op_private, FUV_SUBSCRIPT_ARRAY); | |
bd81e77b NC |
13826 | } |
13827 | else { | |
13828 | gv = cGVOPx_gv(obase); | |
13829 | if (!gv) | |
13830 | break; | |
13831 | if (match) { | |
13832 | SV **svp; | |
6c1b357c | 13833 | AV *const av = GvAV(gv); |
bd81e77b NC |
13834 | if (!av || SvRMAGICAL(av)) |
13835 | break; | |
13836 | svp = av_fetch(av, (I32)obase->op_private, FALSE); | |
13837 | if (!svp || *svp != uninit_sv) | |
13838 | break; | |
13839 | } | |
13840 | return varname(gv, '$', 0, | |
a0714e2c | 13841 | NULL, (I32)obase->op_private, FUV_SUBSCRIPT_ARRAY); |
bd81e77b NC |
13842 | } |
13843 | break; | |
1d7c1841 | 13844 | |
bd81e77b NC |
13845 | case OP_EXISTS: |
13846 | o = cUNOPx(obase)->op_first; | |
13847 | if (!o || o->op_type != OP_NULL || | |
13848 | ! (o->op_targ == OP_AELEM || o->op_targ == OP_HELEM)) | |
13849 | break; | |
13850 | return find_uninit_var(cBINOPo->op_last, uninit_sv, match); | |
a2efc822 | 13851 | |
bd81e77b NC |
13852 | case OP_AELEM: |
13853 | case OP_HELEM: | |
13854 | if (PL_op == obase) | |
13855 | /* $a[uninit_expr] or $h{uninit_expr} */ | |
13856 | return find_uninit_var(cBINOPx(obase)->op_last, uninit_sv, match); | |
081fc587 | 13857 | |
a0714e2c | 13858 | gv = NULL; |
bd81e77b NC |
13859 | o = cBINOPx(obase)->op_first; |
13860 | kid = cBINOPx(obase)->op_last; | |
8cf8f3d1 | 13861 | |
bd81e77b | 13862 | /* get the av or hv, and optionally the gv */ |
a0714e2c | 13863 | sv = NULL; |
bd81e77b NC |
13864 | if (o->op_type == OP_PADAV || o->op_type == OP_PADHV) { |
13865 | sv = PAD_SV(o->op_targ); | |
13866 | } | |
13867 | else if ((o->op_type == OP_RV2AV || o->op_type == OP_RV2HV) | |
13868 | && cUNOPo->op_first->op_type == OP_GV) | |
13869 | { | |
13870 | gv = cGVOPx_gv(cUNOPo->op_first); | |
13871 | if (!gv) | |
13872 | break; | |
daba3364 NC |
13873 | sv = o->op_type |
13874 | == OP_RV2HV ? MUTABLE_SV(GvHV(gv)) : MUTABLE_SV(GvAV(gv)); | |
bd81e77b NC |
13875 | } |
13876 | if (!sv) | |
13877 | break; | |
13878 | ||
13879 | if (kid && kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid))) { | |
13880 | /* index is constant */ | |
13881 | if (match) { | |
13882 | if (SvMAGICAL(sv)) | |
13883 | break; | |
13884 | if (obase->op_type == OP_HELEM) { | |
85fbaab2 | 13885 | HE* he = hv_fetch_ent(MUTABLE_HV(sv), cSVOPx_sv(kid), 0, 0); |
bd81e77b NC |
13886 | if (!he || HeVAL(he) != uninit_sv) |
13887 | break; | |
13888 | } | |
13889 | else { | |
502c6561 | 13890 | SV * const * const svp = av_fetch(MUTABLE_AV(sv), SvIV(cSVOPx_sv(kid)), FALSE); |
bd81e77b NC |
13891 | if (!svp || *svp != uninit_sv) |
13892 | break; | |
13893 | } | |
13894 | } | |
13895 | if (obase->op_type == OP_HELEM) | |
13896 | return varname(gv, '%', o->op_targ, | |
13897 | cSVOPx_sv(kid), 0, FUV_SUBSCRIPT_HASH); | |
13898 | else | |
a0714e2c | 13899 | return varname(gv, '@', o->op_targ, NULL, |
bd81e77b | 13900 | SvIV(cSVOPx_sv(kid)), FUV_SUBSCRIPT_ARRAY); |
bd81e77b NC |
13901 | } |
13902 | else { | |
13903 | /* index is an expression; | |
13904 | * attempt to find a match within the aggregate */ | |
13905 | if (obase->op_type == OP_HELEM) { | |
85fbaab2 | 13906 | SV * const keysv = find_hash_subscript((const HV*)sv, uninit_sv); |
bd81e77b NC |
13907 | if (keysv) |
13908 | return varname(gv, '%', o->op_targ, | |
13909 | keysv, 0, FUV_SUBSCRIPT_HASH); | |
13910 | } | |
13911 | else { | |
502c6561 NC |
13912 | const I32 index |
13913 | = find_array_subscript((const AV *)sv, uninit_sv); | |
bd81e77b NC |
13914 | if (index >= 0) |
13915 | return varname(gv, '@', o->op_targ, | |
a0714e2c | 13916 | NULL, index, FUV_SUBSCRIPT_ARRAY); |
bd81e77b NC |
13917 | } |
13918 | if (match) | |
13919 | break; | |
13920 | return varname(gv, | |
13921 | (o->op_type == OP_PADAV || o->op_type == OP_RV2AV) | |
13922 | ? '@' : '%', | |
a0714e2c | 13923 | o->op_targ, NULL, 0, FUV_SUBSCRIPT_WITHIN); |
f284b03f | 13924 | } |
bd81e77b | 13925 | break; |
dc507217 | 13926 | |
bd81e77b NC |
13927 | case OP_AASSIGN: |
13928 | /* only examine RHS */ | |
13929 | return find_uninit_var(cBINOPx(obase)->op_first, uninit_sv, match); | |
6d26897e | 13930 | |
bd81e77b NC |
13931 | case OP_OPEN: |
13932 | o = cUNOPx(obase)->op_first; | |
13933 | if (o->op_type == OP_PUSHMARK) | |
13934 | o = o->op_sibling; | |
1d7c1841 | 13935 | |
bd81e77b NC |
13936 | if (!o->op_sibling) { |
13937 | /* one-arg version of open is highly magical */ | |
a0ae6670 | 13938 | |
bd81e77b NC |
13939 | if (o->op_type == OP_GV) { /* open FOO; */ |
13940 | gv = cGVOPx_gv(o); | |
13941 | if (match && GvSV(gv) != uninit_sv) | |
13942 | break; | |
13943 | return varname(gv, '$', 0, | |
a0714e2c | 13944 | NULL, 0, FUV_SUBSCRIPT_NONE); |
bd81e77b NC |
13945 | } |
13946 | /* other possibilities not handled are: | |
13947 | * open $x; or open my $x; should return '${*$x}' | |
13948 | * open expr; should return '$'.expr ideally | |
13949 | */ | |
13950 | break; | |
13951 | } | |
13952 | goto do_op; | |
ccfc67b7 | 13953 | |
bd81e77b NC |
13954 | /* ops where $_ may be an implicit arg */ |
13955 | case OP_TRANS: | |
13956 | case OP_SUBST: | |
13957 | case OP_MATCH: | |
13958 | if ( !(obase->op_flags & OPf_STACKED)) { | |
13959 | if (uninit_sv == ((obase->op_private & OPpTARGET_MY) | |
13960 | ? PAD_SVl(obase->op_targ) | |
13961 | : DEFSV)) | |
13962 | { | |
13963 | sv = sv_newmortal(); | |
76f68e9b | 13964 | sv_setpvs(sv, "$_"); |
bd81e77b NC |
13965 | return sv; |
13966 | } | |
13967 | } | |
13968 | goto do_op; | |
9f4817db | 13969 | |
bd81e77b NC |
13970 | case OP_PRTF: |
13971 | case OP_PRINT: | |
3ef1310e | 13972 | case OP_SAY: |
fa8d1836 | 13973 | match = 1; /* print etc can return undef on defined args */ |
bd81e77b NC |
13974 | /* skip filehandle as it can't produce 'undef' warning */ |
13975 | o = cUNOPx(obase)->op_first; | |
13976 | if ((obase->op_flags & OPf_STACKED) && o->op_type == OP_PUSHMARK) | |
13977 | o = o->op_sibling->op_sibling; | |
13978 | goto do_op2; | |
9f4817db | 13979 | |
9f4817db | 13980 | |
50edf520 | 13981 | case OP_ENTEREVAL: /* could be eval $undef or $x='$undef'; eval $x */ |
bd81e77b | 13982 | case OP_RV2SV: |
8b0dea50 DM |
13983 | case OP_CUSTOM: /* XS or custom code could trigger random warnings */ |
13984 | ||
13985 | /* the following ops are capable of returning PL_sv_undef even for | |
13986 | * defined arg(s) */ | |
13987 | ||
13988 | case OP_BACKTICK: | |
13989 | case OP_PIPE_OP: | |
13990 | case OP_FILENO: | |
13991 | case OP_BINMODE: | |
13992 | case OP_TIED: | |
13993 | case OP_GETC: | |
13994 | case OP_SYSREAD: | |
13995 | case OP_SEND: | |
13996 | case OP_IOCTL: | |
13997 | case OP_SOCKET: | |
13998 | case OP_SOCKPAIR: | |
13999 | case OP_BIND: | |
14000 | case OP_CONNECT: | |
14001 | case OP_LISTEN: | |
14002 | case OP_ACCEPT: | |
14003 | case OP_SHUTDOWN: | |
14004 | case OP_SSOCKOPT: | |
14005 | case OP_GETPEERNAME: | |
14006 | case OP_FTRREAD: | |
14007 | case OP_FTRWRITE: | |
14008 | case OP_FTREXEC: | |
14009 | case OP_FTROWNED: | |
14010 | case OP_FTEREAD: | |
14011 | case OP_FTEWRITE: | |
14012 | case OP_FTEEXEC: | |
14013 | case OP_FTEOWNED: | |
14014 | case OP_FTIS: | |
14015 | case OP_FTZERO: | |
14016 | case OP_FTSIZE: | |
14017 | case OP_FTFILE: | |
14018 | case OP_FTDIR: | |
14019 | case OP_FTLINK: | |
14020 | case OP_FTPIPE: | |
14021 | case OP_FTSOCK: | |
14022 | case OP_FTBLK: | |
14023 | case OP_FTCHR: | |
14024 | case OP_FTTTY: | |
14025 | case OP_FTSUID: | |
14026 | case OP_FTSGID: | |
14027 | case OP_FTSVTX: | |
14028 | case OP_FTTEXT: | |
14029 | case OP_FTBINARY: | |
14030 | case OP_FTMTIME: | |
14031 | case OP_FTATIME: | |
14032 | case OP_FTCTIME: | |
14033 | case OP_READLINK: | |
14034 | case OP_OPEN_DIR: | |
14035 | case OP_READDIR: | |
14036 | case OP_TELLDIR: | |
14037 | case OP_SEEKDIR: | |
14038 | case OP_REWINDDIR: | |
14039 | case OP_CLOSEDIR: | |
14040 | case OP_GMTIME: | |
14041 | case OP_ALARM: | |
14042 | case OP_SEMGET: | |
14043 | case OP_GETLOGIN: | |
14044 | case OP_UNDEF: | |
14045 | case OP_SUBSTR: | |
14046 | case OP_AEACH: | |
14047 | case OP_EACH: | |
14048 | case OP_SORT: | |
14049 | case OP_CALLER: | |
14050 | case OP_DOFILE: | |
fa8d1836 DM |
14051 | case OP_PROTOTYPE: |
14052 | case OP_NCMP: | |
14053 | case OP_SMARTMATCH: | |
14054 | case OP_UNPACK: | |
14055 | case OP_SYSOPEN: | |
14056 | case OP_SYSSEEK: | |
8b0dea50 | 14057 | match = 1; |
bd81e77b | 14058 | goto do_op; |
9f4817db | 14059 | |
7697b7e7 DM |
14060 | case OP_ENTERSUB: |
14061 | case OP_GOTO: | |
a2fb3d36 DM |
14062 | /* XXX tmp hack: these two may call an XS sub, and currently |
14063 | XS subs don't have a SUB entry on the context stack, so CV and | |
14064 | pad determination goes wrong, and BAD things happen. So, just | |
14065 | don't try to determine the value under those circumstances. | |
7697b7e7 DM |
14066 | Need a better fix at dome point. DAPM 11/2007 */ |
14067 | break; | |
14068 | ||
4f187fc9 VP |
14069 | case OP_FLIP: |
14070 | case OP_FLOP: | |
14071 | { | |
14072 | GV * const gv = gv_fetchpvs(".", GV_NOTQUAL, SVt_PV); | |
14073 | if (gv && GvSV(gv) == uninit_sv) | |
14074 | return newSVpvs_flags("$.", SVs_TEMP); | |
14075 | goto do_op; | |
14076 | } | |
8b0dea50 | 14077 | |
cc4b8646 DM |
14078 | case OP_POS: |
14079 | /* def-ness of rval pos() is independent of the def-ness of its arg */ | |
14080 | if ( !(obase->op_flags & OPf_MOD)) | |
14081 | break; | |
14082 | ||
bd81e77b NC |
14083 | case OP_SCHOMP: |
14084 | case OP_CHOMP: | |
14085 | if (SvROK(PL_rs) && uninit_sv == SvRV(PL_rs)) | |
84bafc02 | 14086 | return newSVpvs_flags("${$/}", SVs_TEMP); |
5f66b61c | 14087 | /*FALLTHROUGH*/ |
5d170f3a | 14088 | |
bd81e77b NC |
14089 | default: |
14090 | do_op: | |
14091 | if (!(obase->op_flags & OPf_KIDS)) | |
14092 | break; | |
14093 | o = cUNOPx(obase)->op_first; | |
14094 | ||
14095 | do_op2: | |
14096 | if (!o) | |
14097 | break; | |
f9893866 | 14098 | |
bd81e77b NC |
14099 | /* if all except one arg are constant, or have no side-effects, |
14100 | * or are optimized away, then it's unambiguous */ | |
5f66b61c | 14101 | o2 = NULL; |
bd81e77b | 14102 | for (kid=o; kid; kid = kid->op_sibling) { |
e15d5972 AL |
14103 | if (kid) { |
14104 | const OPCODE type = kid->op_type; | |
14105 | if ( (type == OP_CONST && SvOK(cSVOPx_sv(kid))) | |
14106 | || (type == OP_NULL && ! (kid->op_flags & OPf_KIDS)) | |
14107 | || (type == OP_PUSHMARK) | |
6d1f0892 FC |
14108 | || ( |
14109 | /* @$a and %$a, but not @a or %a */ | |
14110 | (type == OP_RV2AV || type == OP_RV2HV) | |
14111 | && cUNOPx(kid)->op_first | |
14112 | && cUNOPx(kid)->op_first->op_type != OP_GV | |
14113 | ) | |
bd81e77b | 14114 | ) |
bd81e77b | 14115 | continue; |
e15d5972 | 14116 | } |
bd81e77b | 14117 | if (o2) { /* more than one found */ |
5f66b61c | 14118 | o2 = NULL; |
bd81e77b NC |
14119 | break; |
14120 | } | |
14121 | o2 = kid; | |
14122 | } | |
14123 | if (o2) | |
14124 | return find_uninit_var(o2, uninit_sv, match); | |
7a5fa8a2 | 14125 | |
bd81e77b NC |
14126 | /* scan all args */ |
14127 | while (o) { | |
14128 | sv = find_uninit_var(o, uninit_sv, 1); | |
14129 | if (sv) | |
14130 | return sv; | |
14131 | o = o->op_sibling; | |
d0063567 | 14132 | } |
bd81e77b | 14133 | break; |
f9893866 | 14134 | } |
a0714e2c | 14135 | return NULL; |
9f4817db JH |
14136 | } |
14137 | ||
220e2d4e | 14138 | |
bd81e77b NC |
14139 | /* |
14140 | =for apidoc report_uninit | |
68795e93 | 14141 | |
bd81e77b | 14142 | Print appropriate "Use of uninitialized variable" warning |
220e2d4e | 14143 | |
bd81e77b NC |
14144 | =cut |
14145 | */ | |
220e2d4e | 14146 | |
bd81e77b | 14147 | void |
b3dbd76e | 14148 | Perl_report_uninit(pTHX_ const SV *uninit_sv) |
220e2d4e | 14149 | { |
97aff369 | 14150 | dVAR; |
bd81e77b | 14151 | if (PL_op) { |
a0714e2c | 14152 | SV* varname = NULL; |
bd81e77b NC |
14153 | if (uninit_sv) { |
14154 | varname = find_uninit_var(PL_op, uninit_sv,0); | |
14155 | if (varname) | |
14156 | sv_insert(varname, 0, 0, " ", 1); | |
14157 | } | |
14158 | Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit, | |
14159 | varname ? SvPV_nolen_const(varname) : "", | |
14160 | " in ", OP_DESC(PL_op)); | |
220e2d4e | 14161 | } |
a73e8557 | 14162 | else |
bd81e77b NC |
14163 | Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit, |
14164 | "", "", ""); | |
220e2d4e | 14165 | } |
f9893866 | 14166 | |
241d1a3b NC |
14167 | /* |
14168 | * Local variables: | |
14169 | * c-indentation-style: bsd | |
14170 | * c-basic-offset: 4 | |
14171 | * indent-tabs-mode: t | |
14172 | * End: | |
14173 | * | |
37442d52 RGS |
14174 | * ex: set ts=8 sts=4 sw=4 noet: |
14175 | */ |