Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | /* sv.c |
79072805 | 2 | * |
e6906430 JH |
3 | * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
4 | * 2000, 2001, 2002, 2003, by Larry Wall and others | |
79072805 LW |
5 | * |
6 | * You may distribute under the terms of either the GNU General Public | |
7 | * License or the Artistic License, as specified in the README file. | |
8 | * | |
a0d0e21e | 9 | * "I wonder what the Entish is for 'yes' and 'no'," he thought. |
645c22ef DM |
10 | * |
11 | * | |
5e045b90 AMS |
12 | * This file contains the code that creates, manipulates and destroys |
13 | * scalar values (SVs). The other types (AV, HV, GV, etc.) reuse the | |
14 | * structure of an SV, so their creation and destruction is handled | |
15 | * here; higher-level functions are in av.c, hv.c, and so on. Opcode | |
16 | * level functions (eg. substr, split, join) for each of the types are | |
17 | * in the pp*.c files. | |
79072805 LW |
18 | */ |
19 | ||
20 | #include "EXTERN.h" | |
864dbfa3 | 21 | #define PERL_IN_SV_C |
79072805 | 22 | #include "perl.h" |
d2f185dc | 23 | #include "regcomp.h" |
79072805 | 24 | |
51371543 | 25 | #define FCALL *f |
645c22ef | 26 | |
d6218095 JH |
27 | #ifdef PERL_UTF8_CACHE_ASSERT |
28 | /* The cache element 0 is the Unicode offset; | |
29 | * the cache element 1 is the byte offset of the element 0; | |
30 | * the cache element 2 is the Unicode length of the substring; | |
31 | * the cache element 3 is the byte length of the substring; | |
32 | * The checking of the substring side would be good | |
33 | * but substr() has enough code paths to make my head spin; | |
34 | * if adding more checks watch out for the following tests: | |
35 | * t/op/index.t t/op/length.t t/op/pat.t t/op/substr.t | |
36 | * lib/utf8.t lib/Unicode/Collate/t/index.t | |
37 | * --jhi | |
38 | */ | |
39 | #define ASSERT_UTF8_CACHE(cache) \ | |
40 | STMT_START { if (cache) { assert((cache)[0] <= (cache)[1]); } } STMT_END | |
41 | #else | |
42 | #define ASSERT_UTF8_CACHE(cache) NOOP | |
43 | #endif | |
44 | ||
645c22ef DM |
45 | /* ============================================================================ |
46 | ||
47 | =head1 Allocation and deallocation of SVs. | |
48 | ||
5e045b90 AMS |
49 | An SV (or AV, HV, etc.) is allocated in two parts: the head (struct sv, |
50 | av, hv...) contains type and reference count information, as well as a | |
51 | pointer to the body (struct xrv, xpv, xpviv...), which contains fields | |
52 | specific to each type. | |
53 | ||
54 | Normally, this allocation is done using arenas, which are approximately | |
55 | 1K chunks of memory parcelled up into N heads or bodies. The first slot | |
56 | in each arena is reserved, and is used to hold a link to the next arena. | |
57 | In the case of heads, the unused first slot also contains some flags and | |
58 | a note of the number of slots. Snaked through each arena chain is a | |
59 | linked list of free items; when this becomes empty, an extra arena is | |
60 | allocated and divided up into N items which are threaded into the free | |
61 | list. | |
645c22ef DM |
62 | |
63 | The following global variables are associated with arenas: | |
64 | ||
65 | PL_sv_arenaroot pointer to list of SV arenas | |
66 | PL_sv_root pointer to list of free SV structures | |
67 | ||
68 | PL_foo_arenaroot pointer to list of foo arenas, | |
69 | PL_foo_root pointer to list of free foo bodies | |
70 | ... for foo in xiv, xnv, xrv, xpv etc. | |
71 | ||
72 | Note that some of the larger and more rarely used body types (eg xpvio) | |
73 | are not allocated using arenas, but are instead just malloc()/free()ed as | |
74 | required. Also, if PURIFY is defined, arenas are abandoned altogether, | |
75 | with all items individually malloc()ed. In addition, a few SV heads are | |
76 | not allocated from an arena, but are instead directly created as static | |
77 | or auto variables, eg PL_sv_undef. | |
78 | ||
79 | The SV arena serves the secondary purpose of allowing still-live SVs | |
80 | to be located and destroyed during final cleanup. | |
81 | ||
82 | At the lowest level, the macros new_SV() and del_SV() grab and free | |
83 | an SV head. (If debugging with -DD, del_SV() calls the function S_del_sv() | |
84 | to return the SV to the free list with error checking.) new_SV() calls | |
85 | more_sv() / sv_add_arena() to add an extra arena if the free list is empty. | |
86 | SVs in the free list have their SvTYPE field set to all ones. | |
87 | ||
88 | Similarly, there are macros new_XIV()/del_XIV(), new_XNV()/del_XNV() etc | |
89 | that allocate and return individual body types. Normally these are mapped | |
ff276b08 RG |
90 | to the arena-manipulating functions new_xiv()/del_xiv() etc, but may be |
91 | instead mapped directly to malloc()/free() if PURIFY is defined. The | |
645c22ef DM |
92 | new/del functions remove from, or add to, the appropriate PL_foo_root |
93 | list, and call more_xiv() etc to add a new arena if the list is empty. | |
94 | ||
ff276b08 | 95 | At the time of very final cleanup, sv_free_arenas() is called from |
645c22ef DM |
96 | perl_destruct() to physically free all the arenas allocated since the |
97 | start of the interpreter. Note that this also clears PL_he_arenaroot, | |
98 | which is otherwise dealt with in hv.c. | |
99 | ||
100 | Manipulation of any of the PL_*root pointers is protected by enclosing | |
101 | LOCK_SV_MUTEX; ... UNLOCK_SV_MUTEX calls which should Do the Right Thing | |
102 | if threads are enabled. | |
103 | ||
104 | The function visit() scans the SV arenas list, and calls a specified | |
105 | function for each SV it finds which is still live - ie which has an SvTYPE | |
106 | other than all 1's, and a non-zero SvREFCNT. visit() is used by the | |
107 | following functions (specified as [function that calls visit()] / [function | |
108 | called by visit() for each SV]): | |
109 | ||
110 | sv_report_used() / do_report_used() | |
111 | dump all remaining SVs (debugging aid) | |
112 | ||
113 | sv_clean_objs() / do_clean_objs(),do_clean_named_objs() | |
114 | Attempt to free all objects pointed to by RVs, | |
115 | and, unless DISABLE_DESTRUCTOR_KLUDGE is defined, | |
116 | try to do the same for all objects indirectly | |
117 | referenced by typeglobs too. Called once from | |
118 | perl_destruct(), prior to calling sv_clean_all() | |
119 | below. | |
120 | ||
121 | sv_clean_all() / do_clean_all() | |
122 | SvREFCNT_dec(sv) each remaining SV, possibly | |
123 | triggering an sv_free(). It also sets the | |
124 | SVf_BREAK flag on the SV to indicate that the | |
125 | refcnt has been artificially lowered, and thus | |
126 | stopping sv_free() from giving spurious warnings | |
127 | about SVs which unexpectedly have a refcnt | |
128 | of zero. called repeatedly from perl_destruct() | |
129 | until there are no SVs left. | |
130 | ||
131 | =head2 Summary | |
132 | ||
133 | Private API to rest of sv.c | |
134 | ||
135 | new_SV(), del_SV(), | |
136 | ||
137 | new_XIV(), del_XIV(), | |
138 | new_XNV(), del_XNV(), | |
139 | etc | |
140 | ||
141 | Public API: | |
142 | ||
8cf8f3d1 | 143 | sv_report_used(), sv_clean_objs(), sv_clean_all(), sv_free_arenas() |
645c22ef DM |
144 | |
145 | ||
146 | =cut | |
147 | ||
148 | ============================================================================ */ | |
149 | ||
150 | ||
51371543 | 151 | |
4561caa4 CS |
152 | /* |
153 | * "A time to plant, and a time to uproot what was planted..." | |
154 | */ | |
155 | ||
053fc874 GS |
156 | #define plant_SV(p) \ |
157 | STMT_START { \ | |
158 | SvANY(p) = (void *)PL_sv_root; \ | |
159 | SvFLAGS(p) = SVTYPEMASK; \ | |
160 | PL_sv_root = (p); \ | |
161 | --PL_sv_count; \ | |
162 | } STMT_END | |
a0d0e21e | 163 | |
fba3b22e | 164 | /* sv_mutex must be held while calling uproot_SV() */ |
053fc874 GS |
165 | #define uproot_SV(p) \ |
166 | STMT_START { \ | |
167 | (p) = PL_sv_root; \ | |
168 | PL_sv_root = (SV*)SvANY(p); \ | |
169 | ++PL_sv_count; \ | |
170 | } STMT_END | |
171 | ||
645c22ef DM |
172 | |
173 | /* new_SV(): return a new, empty SV head */ | |
174 | ||
c240c76d JH |
175 | #ifdef DEBUG_LEAKING_SCALARS |
176 | /* provide a real function for a debugger to play with */ | |
177 | STATIC SV* | |
178 | S_new_SV(pTHX) | |
179 | { | |
180 | SV* sv; | |
181 | ||
182 | LOCK_SV_MUTEX; | |
183 | if (PL_sv_root) | |
184 | uproot_SV(sv); | |
185 | else | |
186 | sv = more_sv(); | |
187 | UNLOCK_SV_MUTEX; | |
188 | SvANY(sv) = 0; | |
189 | SvREFCNT(sv) = 1; | |
190 | SvFLAGS(sv) = 0; | |
191 | return sv; | |
192 | } | |
193 | # define new_SV(p) (p)=S_new_SV(aTHX) | |
194 | ||
195 | #else | |
196 | # define new_SV(p) \ | |
053fc874 GS |
197 | STMT_START { \ |
198 | LOCK_SV_MUTEX; \ | |
199 | if (PL_sv_root) \ | |
200 | uproot_SV(p); \ | |
201 | else \ | |
202 | (p) = more_sv(); \ | |
203 | UNLOCK_SV_MUTEX; \ | |
204 | SvANY(p) = 0; \ | |
205 | SvREFCNT(p) = 1; \ | |
206 | SvFLAGS(p) = 0; \ | |
207 | } STMT_END | |
c240c76d | 208 | #endif |
463ee0b2 | 209 | |
645c22ef DM |
210 | |
211 | /* del_SV(): return an empty SV head to the free list */ | |
212 | ||
a0d0e21e | 213 | #ifdef DEBUGGING |
4561caa4 | 214 | |
053fc874 GS |
215 | #define del_SV(p) \ |
216 | STMT_START { \ | |
217 | LOCK_SV_MUTEX; \ | |
aea4f609 | 218 | if (DEBUG_D_TEST) \ |
053fc874 GS |
219 | del_sv(p); \ |
220 | else \ | |
221 | plant_SV(p); \ | |
222 | UNLOCK_SV_MUTEX; \ | |
223 | } STMT_END | |
a0d0e21e | 224 | |
76e3520e | 225 | STATIC void |
cea2e8a9 | 226 | S_del_sv(pTHX_ SV *p) |
463ee0b2 | 227 | { |
aea4f609 | 228 | if (DEBUG_D_TEST) { |
4633a7c4 | 229 | SV* sva; |
a0d0e21e LW |
230 | SV* sv; |
231 | SV* svend; | |
232 | int ok = 0; | |
3280af22 | 233 | for (sva = PL_sv_arenaroot; sva; sva = (SV *) SvANY(sva)) { |
4633a7c4 LW |
234 | sv = sva + 1; |
235 | svend = &sva[SvREFCNT(sva)]; | |
a0d0e21e LW |
236 | if (p >= sv && p < svend) |
237 | ok = 1; | |
238 | } | |
239 | if (!ok) { | |
0453d815 | 240 | if (ckWARN_d(WARN_INTERNAL)) |
9014280d | 241 | Perl_warner(aTHX_ packWARN(WARN_INTERNAL), |
1d7c1841 GS |
242 | "Attempt to free non-arena SV: 0x%"UVxf, |
243 | PTR2UV(p)); | |
a0d0e21e LW |
244 | return; |
245 | } | |
246 | } | |
4561caa4 | 247 | plant_SV(p); |
463ee0b2 | 248 | } |
a0d0e21e | 249 | |
4561caa4 CS |
250 | #else /* ! DEBUGGING */ |
251 | ||
252 | #define del_SV(p) plant_SV(p) | |
253 | ||
254 | #endif /* DEBUGGING */ | |
463ee0b2 | 255 | |
645c22ef DM |
256 | |
257 | /* | |
ccfc67b7 JH |
258 | =head1 SV Manipulation Functions |
259 | ||
645c22ef DM |
260 | =for apidoc sv_add_arena |
261 | ||
262 | Given a chunk of memory, link it to the head of the list of arenas, | |
263 | and split it into a list of free SVs. | |
264 | ||
265 | =cut | |
266 | */ | |
267 | ||
4633a7c4 | 268 | void |
864dbfa3 | 269 | Perl_sv_add_arena(pTHX_ char *ptr, U32 size, U32 flags) |
463ee0b2 | 270 | { |
4633a7c4 | 271 | SV* sva = (SV*)ptr; |
463ee0b2 LW |
272 | register SV* sv; |
273 | register SV* svend; | |
14dd3ad8 | 274 | Zero(ptr, size, char); |
4633a7c4 LW |
275 | |
276 | /* The first SV in an arena isn't an SV. */ | |
3280af22 | 277 | SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */ |
4633a7c4 LW |
278 | SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */ |
279 | SvFLAGS(sva) = flags; /* FAKE if not to be freed */ | |
280 | ||
3280af22 NIS |
281 | PL_sv_arenaroot = sva; |
282 | PL_sv_root = sva + 1; | |
4633a7c4 LW |
283 | |
284 | svend = &sva[SvREFCNT(sva) - 1]; | |
285 | sv = sva + 1; | |
463ee0b2 | 286 | while (sv < svend) { |
a0d0e21e | 287 | SvANY(sv) = (void *)(SV*)(sv + 1); |
8990e307 | 288 | SvFLAGS(sv) = SVTYPEMASK; |
463ee0b2 LW |
289 | sv++; |
290 | } | |
291 | SvANY(sv) = 0; | |
4633a7c4 LW |
292 | SvFLAGS(sv) = SVTYPEMASK; |
293 | } | |
294 | ||
645c22ef DM |
295 | /* make some more SVs by adding another arena */ |
296 | ||
fba3b22e | 297 | /* sv_mutex must be held while calling more_sv() */ |
76e3520e | 298 | STATIC SV* |
cea2e8a9 | 299 | S_more_sv(pTHX) |
4633a7c4 | 300 | { |
4561caa4 CS |
301 | register SV* sv; |
302 | ||
3280af22 NIS |
303 | if (PL_nice_chunk) { |
304 | sv_add_arena(PL_nice_chunk, PL_nice_chunk_size, 0); | |
305 | PL_nice_chunk = Nullch; | |
30ad99e7 | 306 | PL_nice_chunk_size = 0; |
c07a80fd | 307 | } |
1edc1566 | 308 | else { |
309 | char *chunk; /* must use New here to match call to */ | |
310 | New(704,chunk,1008,char); /* Safefree() in sv_free_arenas() */ | |
311 | sv_add_arena(chunk, 1008, 0); | |
312 | } | |
4561caa4 CS |
313 | uproot_SV(sv); |
314 | return sv; | |
463ee0b2 LW |
315 | } |
316 | ||
ff276b08 | 317 | /* visit(): call the named function for each non-free SV in the arenas. */ |
645c22ef | 318 | |
5226ed68 | 319 | STATIC I32 |
cea2e8a9 | 320 | S_visit(pTHX_ SVFUNC_t f) |
8990e307 | 321 | { |
4633a7c4 | 322 | SV* sva; |
8990e307 LW |
323 | SV* sv; |
324 | register SV* svend; | |
5226ed68 | 325 | I32 visited = 0; |
8990e307 | 326 | |
3280af22 | 327 | for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) { |
4633a7c4 | 328 | svend = &sva[SvREFCNT(sva)]; |
4561caa4 | 329 | for (sv = sva + 1; sv < svend; ++sv) { |
f25c30a3 | 330 | if (SvTYPE(sv) != SVTYPEMASK && SvREFCNT(sv)) { |
acfe0abc | 331 | (FCALL)(aTHX_ sv); |
5226ed68 JH |
332 | ++visited; |
333 | } | |
8990e307 LW |
334 | } |
335 | } | |
5226ed68 | 336 | return visited; |
8990e307 LW |
337 | } |
338 | ||
758a08c3 JH |
339 | #ifdef DEBUGGING |
340 | ||
645c22ef DM |
341 | /* called by sv_report_used() for each live SV */ |
342 | ||
343 | static void | |
acfe0abc | 344 | do_report_used(pTHX_ SV *sv) |
645c22ef DM |
345 | { |
346 | if (SvTYPE(sv) != SVTYPEMASK) { | |
347 | PerlIO_printf(Perl_debug_log, "****\n"); | |
348 | sv_dump(sv); | |
349 | } | |
350 | } | |
758a08c3 | 351 | #endif |
645c22ef DM |
352 | |
353 | /* | |
354 | =for apidoc sv_report_used | |
355 | ||
356 | Dump the contents of all SVs not yet freed. (Debugging aid). | |
357 | ||
358 | =cut | |
359 | */ | |
360 | ||
8990e307 | 361 | void |
864dbfa3 | 362 | Perl_sv_report_used(pTHX) |
4561caa4 | 363 | { |
ff270d3a | 364 | #ifdef DEBUGGING |
0b94c7bb | 365 | visit(do_report_used); |
ff270d3a | 366 | #endif |
4561caa4 CS |
367 | } |
368 | ||
645c22ef DM |
369 | /* called by sv_clean_objs() for each live SV */ |
370 | ||
371 | static void | |
acfe0abc | 372 | do_clean_objs(pTHX_ SV *sv) |
645c22ef DM |
373 | { |
374 | SV* rv; | |
375 | ||
376 | if (SvROK(sv) && SvOBJECT(rv = SvRV(sv))) { | |
377 | DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(sv))); | |
378 | if (SvWEAKREF(sv)) { | |
379 | sv_del_backref(sv); | |
380 | SvWEAKREF_off(sv); | |
381 | SvRV(sv) = 0; | |
382 | } else { | |
383 | SvROK_off(sv); | |
384 | SvRV(sv) = 0; | |
385 | SvREFCNT_dec(rv); | |
386 | } | |
387 | } | |
388 | ||
389 | /* XXX Might want to check arrays, etc. */ | |
390 | } | |
391 | ||
392 | /* called by sv_clean_objs() for each live SV */ | |
393 | ||
394 | #ifndef DISABLE_DESTRUCTOR_KLUDGE | |
395 | static void | |
acfe0abc | 396 | do_clean_named_objs(pTHX_ SV *sv) |
645c22ef DM |
397 | { |
398 | if (SvTYPE(sv) == SVt_PVGV && GvGP(sv)) { | |
399 | if ( SvOBJECT(GvSV(sv)) || | |
400 | (GvAV(sv) && SvOBJECT(GvAV(sv))) || | |
401 | (GvHV(sv) && SvOBJECT(GvHV(sv))) || | |
402 | (GvIO(sv) && SvOBJECT(GvIO(sv))) || | |
403 | (GvCV(sv) && SvOBJECT(GvCV(sv))) ) | |
404 | { | |
405 | DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning named glob object:\n "), sv_dump(sv))); | |
406 | SvREFCNT_dec(sv); | |
407 | } | |
408 | } | |
409 | } | |
410 | #endif | |
411 | ||
412 | /* | |
413 | =for apidoc sv_clean_objs | |
414 | ||
415 | Attempt to destroy all objects not yet freed | |
416 | ||
417 | =cut | |
418 | */ | |
419 | ||
4561caa4 | 420 | void |
864dbfa3 | 421 | Perl_sv_clean_objs(pTHX) |
4561caa4 | 422 | { |
3280af22 | 423 | PL_in_clean_objs = TRUE; |
0b94c7bb | 424 | visit(do_clean_objs); |
4561caa4 | 425 | #ifndef DISABLE_DESTRUCTOR_KLUDGE |
2d0f3c12 | 426 | /* some barnacles may yet remain, clinging to typeglobs */ |
0b94c7bb | 427 | visit(do_clean_named_objs); |
4561caa4 | 428 | #endif |
3280af22 | 429 | PL_in_clean_objs = FALSE; |
4561caa4 CS |
430 | } |
431 | ||
645c22ef DM |
432 | /* called by sv_clean_all() for each live SV */ |
433 | ||
434 | static void | |
acfe0abc | 435 | do_clean_all(pTHX_ SV *sv) |
645c22ef DM |
436 | { |
437 | DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) )); | |
438 | SvFLAGS(sv) |= SVf_BREAK; | |
439 | SvREFCNT_dec(sv); | |
440 | } | |
441 | ||
442 | /* | |
443 | =for apidoc sv_clean_all | |
444 | ||
445 | Decrement the refcnt of each remaining SV, possibly triggering a | |
446 | cleanup. This function may have to be called multiple times to free | |
ff276b08 | 447 | SVs which are in complex self-referential hierarchies. |
645c22ef DM |
448 | |
449 | =cut | |
450 | */ | |
451 | ||
5226ed68 | 452 | I32 |
864dbfa3 | 453 | Perl_sv_clean_all(pTHX) |
8990e307 | 454 | { |
5226ed68 | 455 | I32 cleaned; |
3280af22 | 456 | PL_in_clean_all = TRUE; |
5226ed68 | 457 | cleaned = visit(do_clean_all); |
3280af22 | 458 | PL_in_clean_all = FALSE; |
5226ed68 | 459 | return cleaned; |
8990e307 | 460 | } |
463ee0b2 | 461 | |
645c22ef DM |
462 | /* |
463 | =for apidoc sv_free_arenas | |
464 | ||
465 | Deallocate the memory used by all arenas. Note that all the individual SV | |
466 | heads and bodies within the arenas must already have been freed. | |
467 | ||
468 | =cut | |
469 | */ | |
470 | ||
4633a7c4 | 471 | void |
864dbfa3 | 472 | Perl_sv_free_arenas(pTHX) |
4633a7c4 LW |
473 | { |
474 | SV* sva; | |
475 | SV* svanext; | |
612f20c3 | 476 | XPV *arena, *arenanext; |
4633a7c4 LW |
477 | |
478 | /* Free arenas here, but be careful about fake ones. (We assume | |
479 | contiguity of the fake ones with the corresponding real ones.) */ | |
480 | ||
3280af22 | 481 | for (sva = PL_sv_arenaroot; sva; sva = svanext) { |
4633a7c4 LW |
482 | svanext = (SV*) SvANY(sva); |
483 | while (svanext && SvFAKE(svanext)) | |
484 | svanext = (SV*) SvANY(svanext); | |
485 | ||
486 | if (!SvFAKE(sva)) | |
1edc1566 | 487 | Safefree((void *)sva); |
4633a7c4 | 488 | } |
5f05dabc | 489 | |
612f20c3 GS |
490 | for (arena = PL_xiv_arenaroot; arena; arena = arenanext) { |
491 | arenanext = (XPV*)arena->xpv_pv; | |
492 | Safefree(arena); | |
493 | } | |
494 | PL_xiv_arenaroot = 0; | |
495 | ||
496 | for (arena = PL_xnv_arenaroot; arena; arena = arenanext) { | |
497 | arenanext = (XPV*)arena->xpv_pv; | |
498 | Safefree(arena); | |
499 | } | |
500 | PL_xnv_arenaroot = 0; | |
501 | ||
502 | for (arena = PL_xrv_arenaroot; arena; arena = arenanext) { | |
503 | arenanext = (XPV*)arena->xpv_pv; | |
504 | Safefree(arena); | |
505 | } | |
506 | PL_xrv_arenaroot = 0; | |
507 | ||
508 | for (arena = PL_xpv_arenaroot; arena; arena = arenanext) { | |
509 | arenanext = (XPV*)arena->xpv_pv; | |
510 | Safefree(arena); | |
511 | } | |
512 | PL_xpv_arenaroot = 0; | |
513 | ||
514 | for (arena = (XPV*)PL_xpviv_arenaroot; arena; arena = arenanext) { | |
515 | arenanext = (XPV*)arena->xpv_pv; | |
516 | Safefree(arena); | |
517 | } | |
518 | PL_xpviv_arenaroot = 0; | |
519 | ||
520 | for (arena = (XPV*)PL_xpvnv_arenaroot; arena; arena = arenanext) { | |
521 | arenanext = (XPV*)arena->xpv_pv; | |
522 | Safefree(arena); | |
523 | } | |
524 | PL_xpvnv_arenaroot = 0; | |
525 | ||
526 | for (arena = (XPV*)PL_xpvcv_arenaroot; arena; arena = arenanext) { | |
527 | arenanext = (XPV*)arena->xpv_pv; | |
528 | Safefree(arena); | |
529 | } | |
530 | PL_xpvcv_arenaroot = 0; | |
531 | ||
532 | for (arena = (XPV*)PL_xpvav_arenaroot; arena; arena = arenanext) { | |
533 | arenanext = (XPV*)arena->xpv_pv; | |
534 | Safefree(arena); | |
535 | } | |
536 | PL_xpvav_arenaroot = 0; | |
537 | ||
538 | for (arena = (XPV*)PL_xpvhv_arenaroot; arena; arena = arenanext) { | |
539 | arenanext = (XPV*)arena->xpv_pv; | |
540 | Safefree(arena); | |
541 | } | |
542 | PL_xpvhv_arenaroot = 0; | |
543 | ||
544 | for (arena = (XPV*)PL_xpvmg_arenaroot; arena; arena = arenanext) { | |
545 | arenanext = (XPV*)arena->xpv_pv; | |
546 | Safefree(arena); | |
547 | } | |
548 | PL_xpvmg_arenaroot = 0; | |
549 | ||
550 | for (arena = (XPV*)PL_xpvlv_arenaroot; arena; arena = arenanext) { | |
551 | arenanext = (XPV*)arena->xpv_pv; | |
552 | Safefree(arena); | |
553 | } | |
554 | PL_xpvlv_arenaroot = 0; | |
555 | ||
556 | for (arena = (XPV*)PL_xpvbm_arenaroot; arena; arena = arenanext) { | |
557 | arenanext = (XPV*)arena->xpv_pv; | |
558 | Safefree(arena); | |
559 | } | |
560 | PL_xpvbm_arenaroot = 0; | |
561 | ||
562 | for (arena = (XPV*)PL_he_arenaroot; arena; arena = arenanext) { | |
563 | arenanext = (XPV*)arena->xpv_pv; | |
564 | Safefree(arena); | |
565 | } | |
566 | PL_he_arenaroot = 0; | |
567 | ||
3280af22 NIS |
568 | if (PL_nice_chunk) |
569 | Safefree(PL_nice_chunk); | |
570 | PL_nice_chunk = Nullch; | |
571 | PL_nice_chunk_size = 0; | |
572 | PL_sv_arenaroot = 0; | |
573 | PL_sv_root = 0; | |
4633a7c4 LW |
574 | } |
575 | ||
645c22ef DM |
576 | /* |
577 | =for apidoc report_uninit | |
578 | ||
579 | Print appropriate "Use of uninitialized variable" warning | |
580 | ||
581 | =cut | |
582 | */ | |
583 | ||
1d7c1841 GS |
584 | void |
585 | Perl_report_uninit(pTHX) | |
586 | { | |
587 | if (PL_op) | |
9014280d | 588 | Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit, |
53e06cf0 | 589 | " in ", OP_DESC(PL_op)); |
1d7c1841 | 590 | else |
9014280d | 591 | Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit, "", ""); |
1d7c1841 GS |
592 | } |
593 | ||
645c22ef DM |
594 | /* grab a new IV body from the free list, allocating more if necessary */ |
595 | ||
76e3520e | 596 | STATIC XPVIV* |
cea2e8a9 | 597 | S_new_xiv(pTHX) |
463ee0b2 | 598 | { |
ea7c11a3 | 599 | IV* xiv; |
cbe51380 GS |
600 | LOCK_SV_MUTEX; |
601 | if (!PL_xiv_root) | |
602 | more_xiv(); | |
603 | xiv = PL_xiv_root; | |
604 | /* | |
605 | * See comment in more_xiv() -- RAM. | |
606 | */ | |
607 | PL_xiv_root = *(IV**)xiv; | |
608 | UNLOCK_SV_MUTEX; | |
609 | return (XPVIV*)((char*)xiv - STRUCT_OFFSET(XPVIV, xiv_iv)); | |
463ee0b2 LW |
610 | } |
611 | ||
645c22ef DM |
612 | /* return an IV body to the free list */ |
613 | ||
76e3520e | 614 | STATIC void |
cea2e8a9 | 615 | S_del_xiv(pTHX_ XPVIV *p) |
463ee0b2 | 616 | { |
23e6a22f | 617 | IV* xiv = (IV*)((char*)(p) + STRUCT_OFFSET(XPVIV, xiv_iv)); |
cbe51380 | 618 | LOCK_SV_MUTEX; |
3280af22 NIS |
619 | *(IV**)xiv = PL_xiv_root; |
620 | PL_xiv_root = xiv; | |
cbe51380 | 621 | UNLOCK_SV_MUTEX; |
463ee0b2 LW |
622 | } |
623 | ||
645c22ef DM |
624 | /* allocate another arena's worth of IV bodies */ |
625 | ||
cbe51380 | 626 | STATIC void |
cea2e8a9 | 627 | S_more_xiv(pTHX) |
463ee0b2 | 628 | { |
ea7c11a3 SM |
629 | register IV* xiv; |
630 | register IV* xivend; | |
8c52afec IZ |
631 | XPV* ptr; |
632 | New(705, ptr, 1008/sizeof(XPV), XPV); | |
645c22ef | 633 | ptr->xpv_pv = (char*)PL_xiv_arenaroot; /* linked list of xiv arenas */ |
3280af22 | 634 | PL_xiv_arenaroot = ptr; /* to keep Purify happy */ |
a0d0e21e | 635 | |
ea7c11a3 SM |
636 | xiv = (IV*) ptr; |
637 | xivend = &xiv[1008 / sizeof(IV) - 1]; | |
645c22ef | 638 | xiv += (sizeof(XPV) - 1) / sizeof(IV) + 1; /* fudge by size of XPV */ |
3280af22 | 639 | PL_xiv_root = xiv; |
463ee0b2 | 640 | while (xiv < xivend) { |
ea7c11a3 | 641 | *(IV**)xiv = (IV *)(xiv + 1); |
463ee0b2 LW |
642 | xiv++; |
643 | } | |
ea7c11a3 | 644 | *(IV**)xiv = 0; |
463ee0b2 LW |
645 | } |
646 | ||
645c22ef DM |
647 | /* grab a new NV body from the free list, allocating more if necessary */ |
648 | ||
76e3520e | 649 | STATIC XPVNV* |
cea2e8a9 | 650 | S_new_xnv(pTHX) |
463ee0b2 | 651 | { |
65202027 | 652 | NV* xnv; |
cbe51380 GS |
653 | LOCK_SV_MUTEX; |
654 | if (!PL_xnv_root) | |
655 | more_xnv(); | |
656 | xnv = PL_xnv_root; | |
65202027 | 657 | PL_xnv_root = *(NV**)xnv; |
cbe51380 GS |
658 | UNLOCK_SV_MUTEX; |
659 | return (XPVNV*)((char*)xnv - STRUCT_OFFSET(XPVNV, xnv_nv)); | |
463ee0b2 LW |
660 | } |
661 | ||
645c22ef DM |
662 | /* return an NV body to the free list */ |
663 | ||
76e3520e | 664 | STATIC void |
cea2e8a9 | 665 | S_del_xnv(pTHX_ XPVNV *p) |
463ee0b2 | 666 | { |
65202027 | 667 | NV* xnv = (NV*)((char*)(p) + STRUCT_OFFSET(XPVNV, xnv_nv)); |
cbe51380 | 668 | LOCK_SV_MUTEX; |
65202027 | 669 | *(NV**)xnv = PL_xnv_root; |
3280af22 | 670 | PL_xnv_root = xnv; |
cbe51380 | 671 | UNLOCK_SV_MUTEX; |
463ee0b2 LW |
672 | } |
673 | ||
645c22ef DM |
674 | /* allocate another arena's worth of NV bodies */ |
675 | ||
cbe51380 | 676 | STATIC void |
cea2e8a9 | 677 | S_more_xnv(pTHX) |
463ee0b2 | 678 | { |
65202027 DS |
679 | register NV* xnv; |
680 | register NV* xnvend; | |
612f20c3 GS |
681 | XPV *ptr; |
682 | New(711, ptr, 1008/sizeof(XPV), XPV); | |
683 | ptr->xpv_pv = (char*)PL_xnv_arenaroot; | |
684 | PL_xnv_arenaroot = ptr; | |
685 | ||
686 | xnv = (NV*) ptr; | |
65202027 DS |
687 | xnvend = &xnv[1008 / sizeof(NV) - 1]; |
688 | xnv += (sizeof(XPVIV) - 1) / sizeof(NV) + 1; /* fudge by sizeof XPVIV */ | |
3280af22 | 689 | PL_xnv_root = xnv; |
463ee0b2 | 690 | while (xnv < xnvend) { |
65202027 | 691 | *(NV**)xnv = (NV*)(xnv + 1); |
463ee0b2 LW |
692 | xnv++; |
693 | } | |
65202027 | 694 | *(NV**)xnv = 0; |
463ee0b2 LW |
695 | } |
696 | ||
645c22ef DM |
697 | /* grab a new struct xrv from the free list, allocating more if necessary */ |
698 | ||
76e3520e | 699 | STATIC XRV* |
cea2e8a9 | 700 | S_new_xrv(pTHX) |
ed6116ce LW |
701 | { |
702 | XRV* xrv; | |
cbe51380 GS |
703 | LOCK_SV_MUTEX; |
704 | if (!PL_xrv_root) | |
705 | more_xrv(); | |
706 | xrv = PL_xrv_root; | |
707 | PL_xrv_root = (XRV*)xrv->xrv_rv; | |
708 | UNLOCK_SV_MUTEX; | |
709 | return xrv; | |
ed6116ce LW |
710 | } |
711 | ||
645c22ef DM |
712 | /* return a struct xrv to the free list */ |
713 | ||
76e3520e | 714 | STATIC void |
cea2e8a9 | 715 | S_del_xrv(pTHX_ XRV *p) |
ed6116ce | 716 | { |
cbe51380 | 717 | LOCK_SV_MUTEX; |
3280af22 NIS |
718 | p->xrv_rv = (SV*)PL_xrv_root; |
719 | PL_xrv_root = p; | |
cbe51380 | 720 | UNLOCK_SV_MUTEX; |
ed6116ce LW |
721 | } |
722 | ||
645c22ef DM |
723 | /* allocate another arena's worth of struct xrv */ |
724 | ||
cbe51380 | 725 | STATIC void |
cea2e8a9 | 726 | S_more_xrv(pTHX) |
ed6116ce | 727 | { |
ed6116ce LW |
728 | register XRV* xrv; |
729 | register XRV* xrvend; | |
612f20c3 GS |
730 | XPV *ptr; |
731 | New(712, ptr, 1008/sizeof(XPV), XPV); | |
732 | ptr->xpv_pv = (char*)PL_xrv_arenaroot; | |
733 | PL_xrv_arenaroot = ptr; | |
734 | ||
735 | xrv = (XRV*) ptr; | |
ed6116ce | 736 | xrvend = &xrv[1008 / sizeof(XRV) - 1]; |
612f20c3 GS |
737 | xrv += (sizeof(XPV) - 1) / sizeof(XRV) + 1; |
738 | PL_xrv_root = xrv; | |
ed6116ce LW |
739 | while (xrv < xrvend) { |
740 | xrv->xrv_rv = (SV*)(xrv + 1); | |
741 | xrv++; | |
742 | } | |
743 | xrv->xrv_rv = 0; | |
ed6116ce LW |
744 | } |
745 | ||
645c22ef DM |
746 | /* grab a new struct xpv from the free list, allocating more if necessary */ |
747 | ||
76e3520e | 748 | STATIC XPV* |
cea2e8a9 | 749 | S_new_xpv(pTHX) |
463ee0b2 LW |
750 | { |
751 | XPV* xpv; | |
cbe51380 GS |
752 | LOCK_SV_MUTEX; |
753 | if (!PL_xpv_root) | |
754 | more_xpv(); | |
755 | xpv = PL_xpv_root; | |
756 | PL_xpv_root = (XPV*)xpv->xpv_pv; | |
757 | UNLOCK_SV_MUTEX; | |
758 | return xpv; | |
463ee0b2 LW |
759 | } |
760 | ||
645c22ef DM |
761 | /* return a struct xpv to the free list */ |
762 | ||
76e3520e | 763 | STATIC void |
cea2e8a9 | 764 | S_del_xpv(pTHX_ XPV *p) |
463ee0b2 | 765 | { |
cbe51380 | 766 | LOCK_SV_MUTEX; |
3280af22 NIS |
767 | p->xpv_pv = (char*)PL_xpv_root; |
768 | PL_xpv_root = p; | |
cbe51380 | 769 | UNLOCK_SV_MUTEX; |
463ee0b2 LW |
770 | } |
771 | ||
645c22ef DM |
772 | /* allocate another arena's worth of struct xpv */ |
773 | ||
cbe51380 | 774 | STATIC void |
cea2e8a9 | 775 | S_more_xpv(pTHX) |
463ee0b2 | 776 | { |
463ee0b2 LW |
777 | register XPV* xpv; |
778 | register XPV* xpvend; | |
612f20c3 GS |
779 | New(713, xpv, 1008/sizeof(XPV), XPV); |
780 | xpv->xpv_pv = (char*)PL_xpv_arenaroot; | |
781 | PL_xpv_arenaroot = xpv; | |
782 | ||
463ee0b2 | 783 | xpvend = &xpv[1008 / sizeof(XPV) - 1]; |
612f20c3 | 784 | PL_xpv_root = ++xpv; |
463ee0b2 LW |
785 | while (xpv < xpvend) { |
786 | xpv->xpv_pv = (char*)(xpv + 1); | |
787 | xpv++; | |
788 | } | |
789 | xpv->xpv_pv = 0; | |
463ee0b2 LW |
790 | } |
791 | ||
645c22ef DM |
792 | /* grab a new struct xpviv from the free list, allocating more if necessary */ |
793 | ||
932e9ff9 VB |
794 | STATIC XPVIV* |
795 | S_new_xpviv(pTHX) | |
796 | { | |
797 | XPVIV* xpviv; | |
798 | LOCK_SV_MUTEX; | |
799 | if (!PL_xpviv_root) | |
800 | more_xpviv(); | |
801 | xpviv = PL_xpviv_root; | |
802 | PL_xpviv_root = (XPVIV*)xpviv->xpv_pv; | |
803 | UNLOCK_SV_MUTEX; | |
804 | return xpviv; | |
805 | } | |
806 | ||
645c22ef DM |
807 | /* return a struct xpviv to the free list */ |
808 | ||
932e9ff9 VB |
809 | STATIC void |
810 | S_del_xpviv(pTHX_ XPVIV *p) | |
811 | { | |
812 | LOCK_SV_MUTEX; | |
813 | p->xpv_pv = (char*)PL_xpviv_root; | |
814 | PL_xpviv_root = p; | |
815 | UNLOCK_SV_MUTEX; | |
816 | } | |
817 | ||
645c22ef DM |
818 | /* allocate another arena's worth of struct xpviv */ |
819 | ||
932e9ff9 VB |
820 | STATIC void |
821 | S_more_xpviv(pTHX) | |
822 | { | |
823 | register XPVIV* xpviv; | |
824 | register XPVIV* xpvivend; | |
612f20c3 GS |
825 | New(714, xpviv, 1008/sizeof(XPVIV), XPVIV); |
826 | xpviv->xpv_pv = (char*)PL_xpviv_arenaroot; | |
827 | PL_xpviv_arenaroot = xpviv; | |
828 | ||
932e9ff9 | 829 | xpvivend = &xpviv[1008 / sizeof(XPVIV) - 1]; |
612f20c3 | 830 | PL_xpviv_root = ++xpviv; |
932e9ff9 VB |
831 | while (xpviv < xpvivend) { |
832 | xpviv->xpv_pv = (char*)(xpviv + 1); | |
833 | xpviv++; | |
834 | } | |
835 | xpviv->xpv_pv = 0; | |
836 | } | |
837 | ||
645c22ef DM |
838 | /* grab a new struct xpvnv from the free list, allocating more if necessary */ |
839 | ||
932e9ff9 VB |
840 | STATIC XPVNV* |
841 | S_new_xpvnv(pTHX) | |
842 | { | |
843 | XPVNV* xpvnv; | |
844 | LOCK_SV_MUTEX; | |
845 | if (!PL_xpvnv_root) | |
846 | more_xpvnv(); | |
847 | xpvnv = PL_xpvnv_root; | |
848 | PL_xpvnv_root = (XPVNV*)xpvnv->xpv_pv; | |
849 | UNLOCK_SV_MUTEX; | |
850 | return xpvnv; | |
851 | } | |
852 | ||
645c22ef DM |
853 | /* return a struct xpvnv to the free list */ |
854 | ||
932e9ff9 VB |
855 | STATIC void |
856 | S_del_xpvnv(pTHX_ XPVNV *p) | |
857 | { | |
858 | LOCK_SV_MUTEX; | |
859 | p->xpv_pv = (char*)PL_xpvnv_root; | |
860 | PL_xpvnv_root = p; | |
861 | UNLOCK_SV_MUTEX; | |
862 | } | |
863 | ||
645c22ef DM |
864 | /* allocate another arena's worth of struct xpvnv */ |
865 | ||
932e9ff9 VB |
866 | STATIC void |
867 | S_more_xpvnv(pTHX) | |
868 | { | |
869 | register XPVNV* xpvnv; | |
870 | register XPVNV* xpvnvend; | |
612f20c3 GS |
871 | New(715, xpvnv, 1008/sizeof(XPVNV), XPVNV); |
872 | xpvnv->xpv_pv = (char*)PL_xpvnv_arenaroot; | |
873 | PL_xpvnv_arenaroot = xpvnv; | |
874 | ||
932e9ff9 | 875 | xpvnvend = &xpvnv[1008 / sizeof(XPVNV) - 1]; |
612f20c3 | 876 | PL_xpvnv_root = ++xpvnv; |
932e9ff9 VB |
877 | while (xpvnv < xpvnvend) { |
878 | xpvnv->xpv_pv = (char*)(xpvnv + 1); | |
879 | xpvnv++; | |
880 | } | |
881 | xpvnv->xpv_pv = 0; | |
882 | } | |
883 | ||
645c22ef DM |
884 | /* grab a new struct xpvcv from the free list, allocating more if necessary */ |
885 | ||
932e9ff9 VB |
886 | STATIC XPVCV* |
887 | S_new_xpvcv(pTHX) | |
888 | { | |
889 | XPVCV* xpvcv; | |
890 | LOCK_SV_MUTEX; | |
891 | if (!PL_xpvcv_root) | |
892 | more_xpvcv(); | |
893 | xpvcv = PL_xpvcv_root; | |
894 | PL_xpvcv_root = (XPVCV*)xpvcv->xpv_pv; | |
895 | UNLOCK_SV_MUTEX; | |
896 | return xpvcv; | |
897 | } | |
898 | ||
645c22ef DM |
899 | /* return a struct xpvcv to the free list */ |
900 | ||
932e9ff9 VB |
901 | STATIC void |
902 | S_del_xpvcv(pTHX_ XPVCV *p) | |
903 | { | |
904 | LOCK_SV_MUTEX; | |
905 | p->xpv_pv = (char*)PL_xpvcv_root; | |
906 | PL_xpvcv_root = p; | |
907 | UNLOCK_SV_MUTEX; | |
908 | } | |
909 | ||
645c22ef DM |
910 | /* allocate another arena's worth of struct xpvcv */ |
911 | ||
932e9ff9 VB |
912 | STATIC void |
913 | S_more_xpvcv(pTHX) | |
914 | { | |
915 | register XPVCV* xpvcv; | |
916 | register XPVCV* xpvcvend; | |
612f20c3 GS |
917 | New(716, xpvcv, 1008/sizeof(XPVCV), XPVCV); |
918 | xpvcv->xpv_pv = (char*)PL_xpvcv_arenaroot; | |
919 | PL_xpvcv_arenaroot = xpvcv; | |
920 | ||
932e9ff9 | 921 | xpvcvend = &xpvcv[1008 / sizeof(XPVCV) - 1]; |
612f20c3 | 922 | PL_xpvcv_root = ++xpvcv; |
932e9ff9 VB |
923 | while (xpvcv < xpvcvend) { |
924 | xpvcv->xpv_pv = (char*)(xpvcv + 1); | |
925 | xpvcv++; | |
926 | } | |
927 | xpvcv->xpv_pv = 0; | |
928 | } | |
929 | ||
645c22ef DM |
930 | /* grab a new struct xpvav from the free list, allocating more if necessary */ |
931 | ||
932e9ff9 VB |
932 | STATIC XPVAV* |
933 | S_new_xpvav(pTHX) | |
934 | { | |
935 | XPVAV* xpvav; | |
936 | LOCK_SV_MUTEX; | |
937 | if (!PL_xpvav_root) | |
938 | more_xpvav(); | |
939 | xpvav = PL_xpvav_root; | |
940 | PL_xpvav_root = (XPVAV*)xpvav->xav_array; | |
941 | UNLOCK_SV_MUTEX; | |
942 | return xpvav; | |
943 | } | |
944 | ||
645c22ef DM |
945 | /* return a struct xpvav to the free list */ |
946 | ||
932e9ff9 VB |
947 | STATIC void |
948 | S_del_xpvav(pTHX_ XPVAV *p) | |
949 | { | |
950 | LOCK_SV_MUTEX; | |
951 | p->xav_array = (char*)PL_xpvav_root; | |
952 | PL_xpvav_root = p; | |
953 | UNLOCK_SV_MUTEX; | |
954 | } | |
955 | ||
645c22ef DM |
956 | /* allocate another arena's worth of struct xpvav */ |
957 | ||
932e9ff9 VB |
958 | STATIC void |
959 | S_more_xpvav(pTHX) | |
960 | { | |
961 | register XPVAV* xpvav; | |
962 | register XPVAV* xpvavend; | |
612f20c3 GS |
963 | New(717, xpvav, 1008/sizeof(XPVAV), XPVAV); |
964 | xpvav->xav_array = (char*)PL_xpvav_arenaroot; | |
965 | PL_xpvav_arenaroot = xpvav; | |
966 | ||
932e9ff9 | 967 | xpvavend = &xpvav[1008 / sizeof(XPVAV) - 1]; |
612f20c3 | 968 | PL_xpvav_root = ++xpvav; |
932e9ff9 VB |
969 | while (xpvav < xpvavend) { |
970 | xpvav->xav_array = (char*)(xpvav + 1); | |
971 | xpvav++; | |
972 | } | |
973 | xpvav->xav_array = 0; | |
974 | } | |
975 | ||
645c22ef DM |
976 | /* grab a new struct xpvhv from the free list, allocating more if necessary */ |
977 | ||
932e9ff9 VB |
978 | STATIC XPVHV* |
979 | S_new_xpvhv(pTHX) | |
980 | { | |
981 | XPVHV* xpvhv; | |
982 | LOCK_SV_MUTEX; | |
983 | if (!PL_xpvhv_root) | |
984 | more_xpvhv(); | |
985 | xpvhv = PL_xpvhv_root; | |
986 | PL_xpvhv_root = (XPVHV*)xpvhv->xhv_array; | |
987 | UNLOCK_SV_MUTEX; | |
988 | return xpvhv; | |
989 | } | |
990 | ||
645c22ef DM |
991 | /* return a struct xpvhv to the free list */ |
992 | ||
932e9ff9 VB |
993 | STATIC void |
994 | S_del_xpvhv(pTHX_ XPVHV *p) | |
995 | { | |
996 | LOCK_SV_MUTEX; | |
997 | p->xhv_array = (char*)PL_xpvhv_root; | |
998 | PL_xpvhv_root = p; | |
999 | UNLOCK_SV_MUTEX; | |
1000 | } | |
1001 | ||
645c22ef DM |
1002 | /* allocate another arena's worth of struct xpvhv */ |
1003 | ||
932e9ff9 VB |
1004 | STATIC void |
1005 | S_more_xpvhv(pTHX) | |
1006 | { | |
1007 | register XPVHV* xpvhv; | |
1008 | register XPVHV* xpvhvend; | |
612f20c3 GS |
1009 | New(718, xpvhv, 1008/sizeof(XPVHV), XPVHV); |
1010 | xpvhv->xhv_array = (char*)PL_xpvhv_arenaroot; | |
1011 | PL_xpvhv_arenaroot = xpvhv; | |
1012 | ||
932e9ff9 | 1013 | xpvhvend = &xpvhv[1008 / sizeof(XPVHV) - 1]; |
612f20c3 | 1014 | PL_xpvhv_root = ++xpvhv; |
932e9ff9 VB |
1015 | while (xpvhv < xpvhvend) { |
1016 | xpvhv->xhv_array = (char*)(xpvhv + 1); | |
1017 | xpvhv++; | |
1018 | } | |
1019 | xpvhv->xhv_array = 0; | |
1020 | } | |
1021 | ||
645c22ef DM |
1022 | /* grab a new struct xpvmg from the free list, allocating more if necessary */ |
1023 | ||
932e9ff9 VB |
1024 | STATIC XPVMG* |
1025 | S_new_xpvmg(pTHX) | |
1026 | { | |
1027 | XPVMG* xpvmg; | |
1028 | LOCK_SV_MUTEX; | |
1029 | if (!PL_xpvmg_root) | |
1030 | more_xpvmg(); | |
1031 | xpvmg = PL_xpvmg_root; | |
1032 | PL_xpvmg_root = (XPVMG*)xpvmg->xpv_pv; | |
1033 | UNLOCK_SV_MUTEX; | |
1034 | return xpvmg; | |
1035 | } | |
1036 | ||
645c22ef DM |
1037 | /* return a struct xpvmg to the free list */ |
1038 | ||
932e9ff9 VB |
1039 | STATIC void |
1040 | S_del_xpvmg(pTHX_ XPVMG *p) | |
1041 | { | |
1042 | LOCK_SV_MUTEX; | |
1043 | p->xpv_pv = (char*)PL_xpvmg_root; | |
1044 | PL_xpvmg_root = p; | |
1045 | UNLOCK_SV_MUTEX; | |
1046 | } | |
1047 | ||
645c22ef DM |
1048 | /* allocate another arena's worth of struct xpvmg */ |
1049 | ||
932e9ff9 VB |
1050 | STATIC void |
1051 | S_more_xpvmg(pTHX) | |
1052 | { | |
1053 | register XPVMG* xpvmg; | |
1054 | register XPVMG* xpvmgend; | |
612f20c3 GS |
1055 | New(719, xpvmg, 1008/sizeof(XPVMG), XPVMG); |
1056 | xpvmg->xpv_pv = (char*)PL_xpvmg_arenaroot; | |
1057 | PL_xpvmg_arenaroot = xpvmg; | |
1058 | ||
932e9ff9 | 1059 | xpvmgend = &xpvmg[1008 / sizeof(XPVMG) - 1]; |
612f20c3 | 1060 | PL_xpvmg_root = ++xpvmg; |
932e9ff9 VB |
1061 | while (xpvmg < xpvmgend) { |
1062 | xpvmg->xpv_pv = (char*)(xpvmg + 1); | |
1063 | xpvmg++; | |
1064 | } | |
1065 | xpvmg->xpv_pv = 0; | |
1066 | } | |
1067 | ||
645c22ef DM |
1068 | /* grab a new struct xpvlv from the free list, allocating more if necessary */ |
1069 | ||
932e9ff9 VB |
1070 | STATIC XPVLV* |
1071 | S_new_xpvlv(pTHX) | |
1072 | { | |
1073 | XPVLV* xpvlv; | |
1074 | LOCK_SV_MUTEX; | |
1075 | if (!PL_xpvlv_root) | |
1076 | more_xpvlv(); | |
1077 | xpvlv = PL_xpvlv_root; | |
1078 | PL_xpvlv_root = (XPVLV*)xpvlv->xpv_pv; | |
1079 | UNLOCK_SV_MUTEX; | |
1080 | return xpvlv; | |
1081 | } | |
1082 | ||
645c22ef DM |
1083 | /* return a struct xpvlv to the free list */ |
1084 | ||
932e9ff9 VB |
1085 | STATIC void |
1086 | S_del_xpvlv(pTHX_ XPVLV *p) | |
1087 | { | |
1088 | LOCK_SV_MUTEX; | |
1089 | p->xpv_pv = (char*)PL_xpvlv_root; | |
1090 | PL_xpvlv_root = p; | |
1091 | UNLOCK_SV_MUTEX; | |
1092 | } | |
1093 | ||
645c22ef DM |
1094 | /* allocate another arena's worth of struct xpvlv */ |
1095 | ||
932e9ff9 VB |
1096 | STATIC void |
1097 | S_more_xpvlv(pTHX) | |
1098 | { | |
1099 | register XPVLV* xpvlv; | |
1100 | register XPVLV* xpvlvend; | |
612f20c3 GS |
1101 | New(720, xpvlv, 1008/sizeof(XPVLV), XPVLV); |
1102 | xpvlv->xpv_pv = (char*)PL_xpvlv_arenaroot; | |
1103 | PL_xpvlv_arenaroot = xpvlv; | |
1104 | ||
932e9ff9 | 1105 | xpvlvend = &xpvlv[1008 / sizeof(XPVLV) - 1]; |
612f20c3 | 1106 | PL_xpvlv_root = ++xpvlv; |
932e9ff9 VB |
1107 | while (xpvlv < xpvlvend) { |
1108 | xpvlv->xpv_pv = (char*)(xpvlv + 1); | |
1109 | xpvlv++; | |
1110 | } | |
1111 | xpvlv->xpv_pv = 0; | |
1112 | } | |
1113 | ||
645c22ef DM |
1114 | /* grab a new struct xpvbm from the free list, allocating more if necessary */ |
1115 | ||
932e9ff9 VB |
1116 | STATIC XPVBM* |
1117 | S_new_xpvbm(pTHX) | |
1118 | { | |
1119 | XPVBM* xpvbm; | |
1120 | LOCK_SV_MUTEX; | |
1121 | if (!PL_xpvbm_root) | |
1122 | more_xpvbm(); | |
1123 | xpvbm = PL_xpvbm_root; | |
1124 | PL_xpvbm_root = (XPVBM*)xpvbm->xpv_pv; | |
1125 | UNLOCK_SV_MUTEX; | |
1126 | return xpvbm; | |
1127 | } | |
1128 | ||
645c22ef DM |
1129 | /* return a struct xpvbm to the free list */ |
1130 | ||
932e9ff9 VB |
1131 | STATIC void |
1132 | S_del_xpvbm(pTHX_ XPVBM *p) | |
1133 | { | |
1134 | LOCK_SV_MUTEX; | |
1135 | p->xpv_pv = (char*)PL_xpvbm_root; | |
1136 | PL_xpvbm_root = p; | |
1137 | UNLOCK_SV_MUTEX; | |
1138 | } | |
1139 | ||
645c22ef DM |
1140 | /* allocate another arena's worth of struct xpvbm */ |
1141 | ||
932e9ff9 VB |
1142 | STATIC void |
1143 | S_more_xpvbm(pTHX) | |
1144 | { | |
1145 | register XPVBM* xpvbm; | |
1146 | register XPVBM* xpvbmend; | |
612f20c3 GS |
1147 | New(721, xpvbm, 1008/sizeof(XPVBM), XPVBM); |
1148 | xpvbm->xpv_pv = (char*)PL_xpvbm_arenaroot; | |
1149 | PL_xpvbm_arenaroot = xpvbm; | |
1150 | ||
932e9ff9 | 1151 | xpvbmend = &xpvbm[1008 / sizeof(XPVBM) - 1]; |
612f20c3 | 1152 | PL_xpvbm_root = ++xpvbm; |
932e9ff9 VB |
1153 | while (xpvbm < xpvbmend) { |
1154 | xpvbm->xpv_pv = (char*)(xpvbm + 1); | |
1155 | xpvbm++; | |
1156 | } | |
1157 | xpvbm->xpv_pv = 0; | |
1158 | } | |
1159 | ||
2fe0f101 JH |
1160 | #define my_safemalloc(s) (void*)safemalloc(s) |
1161 | #define my_safefree(p) safefree((char*)p) | |
463ee0b2 | 1162 | |
d33b2eba | 1163 | #ifdef PURIFY |
463ee0b2 | 1164 | |
d33b2eba GS |
1165 | #define new_XIV() my_safemalloc(sizeof(XPVIV)) |
1166 | #define del_XIV(p) my_safefree(p) | |
ed6116ce | 1167 | |
d33b2eba GS |
1168 | #define new_XNV() my_safemalloc(sizeof(XPVNV)) |
1169 | #define del_XNV(p) my_safefree(p) | |
463ee0b2 | 1170 | |
d33b2eba GS |
1171 | #define new_XRV() my_safemalloc(sizeof(XRV)) |
1172 | #define del_XRV(p) my_safefree(p) | |
8c52afec | 1173 | |
d33b2eba GS |
1174 | #define new_XPV() my_safemalloc(sizeof(XPV)) |
1175 | #define del_XPV(p) my_safefree(p) | |
9b94d1dd | 1176 | |
d33b2eba GS |
1177 | #define new_XPVIV() my_safemalloc(sizeof(XPVIV)) |
1178 | #define del_XPVIV(p) my_safefree(p) | |
932e9ff9 | 1179 | |
d33b2eba GS |
1180 | #define new_XPVNV() my_safemalloc(sizeof(XPVNV)) |
1181 | #define del_XPVNV(p) my_safefree(p) | |
932e9ff9 | 1182 | |
d33b2eba GS |
1183 | #define new_XPVCV() my_safemalloc(sizeof(XPVCV)) |
1184 | #define del_XPVCV(p) my_safefree(p) | |
932e9ff9 | 1185 | |
d33b2eba GS |
1186 | #define new_XPVAV() my_safemalloc(sizeof(XPVAV)) |
1187 | #define del_XPVAV(p) my_safefree(p) | |
1188 | ||
1189 | #define new_XPVHV() my_safemalloc(sizeof(XPVHV)) | |
1190 | #define del_XPVHV(p) my_safefree(p) | |
1c846c1f | 1191 | |
d33b2eba GS |
1192 | #define new_XPVMG() my_safemalloc(sizeof(XPVMG)) |
1193 | #define del_XPVMG(p) my_safefree(p) | |
1194 | ||
1195 | #define new_XPVLV() my_safemalloc(sizeof(XPVLV)) | |
1196 | #define del_XPVLV(p) my_safefree(p) | |
1197 | ||
1198 | #define new_XPVBM() my_safemalloc(sizeof(XPVBM)) | |
1199 | #define del_XPVBM(p) my_safefree(p) | |
1200 | ||
1201 | #else /* !PURIFY */ | |
1202 | ||
1203 | #define new_XIV() (void*)new_xiv() | |
1204 | #define del_XIV(p) del_xiv((XPVIV*) p) | |
1205 | ||
1206 | #define new_XNV() (void*)new_xnv() | |
1207 | #define del_XNV(p) del_xnv((XPVNV*) p) | |
9b94d1dd | 1208 | |
d33b2eba GS |
1209 | #define new_XRV() (void*)new_xrv() |
1210 | #define del_XRV(p) del_xrv((XRV*) p) | |
9b94d1dd | 1211 | |
d33b2eba GS |
1212 | #define new_XPV() (void*)new_xpv() |
1213 | #define del_XPV(p) del_xpv((XPV *)p) | |
1214 | ||
1215 | #define new_XPVIV() (void*)new_xpviv() | |
1216 | #define del_XPVIV(p) del_xpviv((XPVIV *)p) | |
1217 | ||
1218 | #define new_XPVNV() (void*)new_xpvnv() | |
1219 | #define del_XPVNV(p) del_xpvnv((XPVNV *)p) | |
1220 | ||
1221 | #define new_XPVCV() (void*)new_xpvcv() | |
1222 | #define del_XPVCV(p) del_xpvcv((XPVCV *)p) | |
1223 | ||
1224 | #define new_XPVAV() (void*)new_xpvav() | |
1225 | #define del_XPVAV(p) del_xpvav((XPVAV *)p) | |
1226 | ||
1227 | #define new_XPVHV() (void*)new_xpvhv() | |
1228 | #define del_XPVHV(p) del_xpvhv((XPVHV *)p) | |
1c846c1f | 1229 | |
d33b2eba GS |
1230 | #define new_XPVMG() (void*)new_xpvmg() |
1231 | #define del_XPVMG(p) del_xpvmg((XPVMG *)p) | |
1232 | ||
1233 | #define new_XPVLV() (void*)new_xpvlv() | |
1234 | #define del_XPVLV(p) del_xpvlv((XPVLV *)p) | |
1235 | ||
1236 | #define new_XPVBM() (void*)new_xpvbm() | |
1237 | #define del_XPVBM(p) del_xpvbm((XPVBM *)p) | |
1238 | ||
1239 | #endif /* PURIFY */ | |
9b94d1dd | 1240 | |
d33b2eba GS |
1241 | #define new_XPVGV() my_safemalloc(sizeof(XPVGV)) |
1242 | #define del_XPVGV(p) my_safefree(p) | |
1c846c1f | 1243 | |
d33b2eba GS |
1244 | #define new_XPVFM() my_safemalloc(sizeof(XPVFM)) |
1245 | #define del_XPVFM(p) my_safefree(p) | |
1c846c1f | 1246 | |
d33b2eba GS |
1247 | #define new_XPVIO() my_safemalloc(sizeof(XPVIO)) |
1248 | #define del_XPVIO(p) my_safefree(p) | |
8990e307 | 1249 | |
954c1994 GS |
1250 | /* |
1251 | =for apidoc sv_upgrade | |
1252 | ||
ff276b08 | 1253 | Upgrade an SV to a more complex form. Generally adds a new body type to the |
645c22ef | 1254 | SV, then copies across as much information as possible from the old body. |
ff276b08 | 1255 | You generally want to use the C<SvUPGRADE> macro wrapper. See also C<svtype>. |
954c1994 GS |
1256 | |
1257 | =cut | |
1258 | */ | |
1259 | ||
79072805 | 1260 | bool |
864dbfa3 | 1261 | Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt) |
79072805 | 1262 | { |
c04a4dfe JH |
1263 | char* pv = NULL; |
1264 | U32 cur = 0; | |
1265 | U32 len = 0; | |
1266 | IV iv = 0; | |
1267 | NV nv = 0.0; | |
1268 | MAGIC* magic = NULL; | |
1269 | HV* stash = Nullhv; | |
79072805 | 1270 | |
f130fd45 NIS |
1271 | if (mt != SVt_PV && SvREADONLY(sv) && SvFAKE(sv)) { |
1272 | sv_force_normal(sv); | |
1273 | } | |
1274 | ||
79072805 LW |
1275 | if (SvTYPE(sv) == mt) |
1276 | return TRUE; | |
1277 | ||
a5f75d66 AD |
1278 | if (mt < SVt_PVIV) |
1279 | (void)SvOOK_off(sv); | |
1280 | ||
79072805 LW |
1281 | switch (SvTYPE(sv)) { |
1282 | case SVt_NULL: | |
1283 | pv = 0; | |
1284 | cur = 0; | |
1285 | len = 0; | |
1286 | iv = 0; | |
1287 | nv = 0.0; | |
1288 | magic = 0; | |
1289 | stash = 0; | |
1290 | break; | |
79072805 LW |
1291 | case SVt_IV: |
1292 | pv = 0; | |
1293 | cur = 0; | |
1294 | len = 0; | |
463ee0b2 | 1295 | iv = SvIVX(sv); |
65202027 | 1296 | nv = (NV)SvIVX(sv); |
79072805 LW |
1297 | del_XIV(SvANY(sv)); |
1298 | magic = 0; | |
1299 | stash = 0; | |
ed6116ce | 1300 | if (mt == SVt_NV) |
463ee0b2 | 1301 | mt = SVt_PVNV; |
ed6116ce LW |
1302 | else if (mt < SVt_PVIV) |
1303 | mt = SVt_PVIV; | |
79072805 LW |
1304 | break; |
1305 | case SVt_NV: | |
1306 | pv = 0; | |
1307 | cur = 0; | |
1308 | len = 0; | |
463ee0b2 | 1309 | nv = SvNVX(sv); |
1bd302c3 | 1310 | iv = I_V(nv); |
79072805 LW |
1311 | magic = 0; |
1312 | stash = 0; | |
1313 | del_XNV(SvANY(sv)); | |
1314 | SvANY(sv) = 0; | |
ed6116ce | 1315 | if (mt < SVt_PVNV) |
79072805 LW |
1316 | mt = SVt_PVNV; |
1317 | break; | |
ed6116ce LW |
1318 | case SVt_RV: |
1319 | pv = (char*)SvRV(sv); | |
1320 | cur = 0; | |
1321 | len = 0; | |
56431972 RB |
1322 | iv = PTR2IV(pv); |
1323 | nv = PTR2NV(pv); | |
ed6116ce LW |
1324 | del_XRV(SvANY(sv)); |
1325 | magic = 0; | |
1326 | stash = 0; | |
1327 | break; | |
79072805 | 1328 | case SVt_PV: |
463ee0b2 | 1329 | pv = SvPVX(sv); |
79072805 LW |
1330 | cur = SvCUR(sv); |
1331 | len = SvLEN(sv); | |
1332 | iv = 0; | |
1333 | nv = 0.0; | |
1334 | magic = 0; | |
1335 | stash = 0; | |
1336 | del_XPV(SvANY(sv)); | |
748a9306 LW |
1337 | if (mt <= SVt_IV) |
1338 | mt = SVt_PVIV; | |
1339 | else if (mt == SVt_NV) | |
1340 | mt = SVt_PVNV; | |
79072805 LW |
1341 | break; |
1342 | case SVt_PVIV: | |
463ee0b2 | 1343 | pv = SvPVX(sv); |
79072805 LW |
1344 | cur = SvCUR(sv); |
1345 | len = SvLEN(sv); | |
463ee0b2 | 1346 | iv = SvIVX(sv); |
79072805 LW |
1347 | nv = 0.0; |
1348 | magic = 0; | |
1349 | stash = 0; | |
1350 | del_XPVIV(SvANY(sv)); | |
1351 | break; | |
1352 | case SVt_PVNV: | |
463ee0b2 | 1353 | pv = SvPVX(sv); |
79072805 LW |
1354 | cur = SvCUR(sv); |
1355 | len = SvLEN(sv); | |
463ee0b2 LW |
1356 | iv = SvIVX(sv); |
1357 | nv = SvNVX(sv); | |
79072805 LW |
1358 | magic = 0; |
1359 | stash = 0; | |
1360 | del_XPVNV(SvANY(sv)); | |
1361 | break; | |
1362 | case SVt_PVMG: | |
463ee0b2 | 1363 | pv = SvPVX(sv); |
79072805 LW |
1364 | cur = SvCUR(sv); |
1365 | len = SvLEN(sv); | |
463ee0b2 LW |
1366 | iv = SvIVX(sv); |
1367 | nv = SvNVX(sv); | |
79072805 LW |
1368 | magic = SvMAGIC(sv); |
1369 | stash = SvSTASH(sv); | |
1370 | del_XPVMG(SvANY(sv)); | |
1371 | break; | |
1372 | default: | |
cea2e8a9 | 1373 | Perl_croak(aTHX_ "Can't upgrade that kind of scalar"); |
79072805 LW |
1374 | } |
1375 | ||
1376 | switch (mt) { | |
1377 | case SVt_NULL: | |
cea2e8a9 | 1378 | Perl_croak(aTHX_ "Can't upgrade to undef"); |
79072805 LW |
1379 | case SVt_IV: |
1380 | SvANY(sv) = new_XIV(); | |
463ee0b2 | 1381 | SvIVX(sv) = iv; |
79072805 LW |
1382 | break; |
1383 | case SVt_NV: | |
1384 | SvANY(sv) = new_XNV(); | |
463ee0b2 | 1385 | SvNVX(sv) = nv; |
79072805 | 1386 | break; |
ed6116ce LW |
1387 | case SVt_RV: |
1388 | SvANY(sv) = new_XRV(); | |
1389 | SvRV(sv) = (SV*)pv; | |
ed6116ce | 1390 | break; |
79072805 LW |
1391 | case SVt_PV: |
1392 | SvANY(sv) = new_XPV(); | |
463ee0b2 | 1393 | SvPVX(sv) = pv; |
79072805 LW |
1394 | SvCUR(sv) = cur; |
1395 | SvLEN(sv) = len; | |
1396 | break; | |
1397 | case SVt_PVIV: | |
1398 | SvANY(sv) = new_XPVIV(); | |
463ee0b2 | 1399 | SvPVX(sv) = pv; |
79072805 LW |
1400 | SvCUR(sv) = cur; |
1401 | SvLEN(sv) = len; | |
463ee0b2 | 1402 | SvIVX(sv) = iv; |
79072805 | 1403 | if (SvNIOK(sv)) |
a0d0e21e | 1404 | (void)SvIOK_on(sv); |
79072805 LW |
1405 | SvNOK_off(sv); |
1406 | break; | |
1407 | case SVt_PVNV: | |
1408 | SvANY(sv) = new_XPVNV(); | |
463ee0b2 | 1409 | SvPVX(sv) = pv; |
79072805 LW |
1410 | SvCUR(sv) = cur; |
1411 | SvLEN(sv) = len; | |
463ee0b2 LW |
1412 | SvIVX(sv) = iv; |
1413 | SvNVX(sv) = nv; | |
79072805 LW |
1414 | break; |
1415 | case SVt_PVMG: | |
1416 | SvANY(sv) = new_XPVMG(); | |
463ee0b2 | 1417 | SvPVX(sv) = pv; |
79072805 LW |
1418 | SvCUR(sv) = cur; |
1419 | SvLEN(sv) = len; | |
463ee0b2 LW |
1420 | SvIVX(sv) = iv; |
1421 | SvNVX(sv) = nv; | |
79072805 LW |
1422 | SvMAGIC(sv) = magic; |
1423 | SvSTASH(sv) = stash; | |
1424 | break; | |
1425 | case SVt_PVLV: | |
1426 | SvANY(sv) = new_XPVLV(); | |
463ee0b2 | 1427 | SvPVX(sv) = pv; |
79072805 LW |
1428 | SvCUR(sv) = cur; |
1429 | SvLEN(sv) = len; | |
463ee0b2 LW |
1430 | SvIVX(sv) = iv; |
1431 | SvNVX(sv) = nv; | |
79072805 LW |
1432 | SvMAGIC(sv) = magic; |
1433 | SvSTASH(sv) = stash; | |
1434 | LvTARGOFF(sv) = 0; | |
1435 | LvTARGLEN(sv) = 0; | |
1436 | LvTARG(sv) = 0; | |
1437 | LvTYPE(sv) = 0; | |
1438 | break; | |
1439 | case SVt_PVAV: | |
1440 | SvANY(sv) = new_XPVAV(); | |
463ee0b2 LW |
1441 | if (pv) |
1442 | Safefree(pv); | |
2304df62 | 1443 | SvPVX(sv) = 0; |
d1bf51dd | 1444 | AvMAX(sv) = -1; |
93965878 | 1445 | AvFILLp(sv) = -1; |
463ee0b2 LW |
1446 | SvIVX(sv) = 0; |
1447 | SvNVX(sv) = 0.0; | |
1448 | SvMAGIC(sv) = magic; | |
1449 | SvSTASH(sv) = stash; | |
1450 | AvALLOC(sv) = 0; | |
79072805 LW |
1451 | AvARYLEN(sv) = 0; |
1452 | AvFLAGS(sv) = 0; | |
1453 | break; | |
1454 | case SVt_PVHV: | |
1455 | SvANY(sv) = new_XPVHV(); | |
463ee0b2 LW |
1456 | if (pv) |
1457 | Safefree(pv); | |
1458 | SvPVX(sv) = 0; | |
1459 | HvFILL(sv) = 0; | |
1460 | HvMAX(sv) = 0; | |
8aacddc1 NIS |
1461 | HvTOTALKEYS(sv) = 0; |
1462 | HvPLACEHOLDERS(sv) = 0; | |
79072805 LW |
1463 | SvMAGIC(sv) = magic; |
1464 | SvSTASH(sv) = stash; | |
79072805 LW |
1465 | HvRITER(sv) = 0; |
1466 | HvEITER(sv) = 0; | |
1467 | HvPMROOT(sv) = 0; | |
1468 | HvNAME(sv) = 0; | |
79072805 LW |
1469 | break; |
1470 | case SVt_PVCV: | |
1471 | SvANY(sv) = new_XPVCV(); | |
748a9306 | 1472 | Zero(SvANY(sv), 1, XPVCV); |
463ee0b2 | 1473 | SvPVX(sv) = pv; |
79072805 LW |
1474 | SvCUR(sv) = cur; |
1475 | SvLEN(sv) = len; | |
463ee0b2 LW |
1476 | SvIVX(sv) = iv; |
1477 | SvNVX(sv) = nv; | |
79072805 LW |
1478 | SvMAGIC(sv) = magic; |
1479 | SvSTASH(sv) = stash; | |
79072805 LW |
1480 | break; |
1481 | case SVt_PVGV: | |
1482 | SvANY(sv) = new_XPVGV(); | |
463ee0b2 | 1483 | SvPVX(sv) = pv; |
79072805 LW |
1484 | SvCUR(sv) = cur; |
1485 | SvLEN(sv) = len; | |
463ee0b2 LW |
1486 | SvIVX(sv) = iv; |
1487 | SvNVX(sv) = nv; | |
79072805 LW |
1488 | SvMAGIC(sv) = magic; |
1489 | SvSTASH(sv) = stash; | |
93a17b20 | 1490 | GvGP(sv) = 0; |
79072805 LW |
1491 | GvNAME(sv) = 0; |
1492 | GvNAMELEN(sv) = 0; | |
1493 | GvSTASH(sv) = 0; | |
a5f75d66 | 1494 | GvFLAGS(sv) = 0; |
79072805 LW |
1495 | break; |
1496 | case SVt_PVBM: | |
1497 | SvANY(sv) = new_XPVBM(); | |
463ee0b2 | 1498 | SvPVX(sv) = pv; |
79072805 LW |
1499 | SvCUR(sv) = cur; |
1500 | SvLEN(sv) = len; | |
463ee0b2 LW |
1501 | SvIVX(sv) = iv; |
1502 | SvNVX(sv) = nv; | |
79072805 LW |
1503 | SvMAGIC(sv) = magic; |
1504 | SvSTASH(sv) = stash; | |
1505 | BmRARE(sv) = 0; | |
1506 | BmUSEFUL(sv) = 0; | |
1507 | BmPREVIOUS(sv) = 0; | |
1508 | break; | |
1509 | case SVt_PVFM: | |
1510 | SvANY(sv) = new_XPVFM(); | |
748a9306 | 1511 | Zero(SvANY(sv), 1, XPVFM); |
463ee0b2 | 1512 | SvPVX(sv) = pv; |
79072805 LW |
1513 | SvCUR(sv) = cur; |
1514 | SvLEN(sv) = len; | |
463ee0b2 LW |
1515 | SvIVX(sv) = iv; |
1516 | SvNVX(sv) = nv; | |
79072805 LW |
1517 | SvMAGIC(sv) = magic; |
1518 | SvSTASH(sv) = stash; | |
79072805 | 1519 | break; |
8990e307 LW |
1520 | case SVt_PVIO: |
1521 | SvANY(sv) = new_XPVIO(); | |
748a9306 | 1522 | Zero(SvANY(sv), 1, XPVIO); |
8990e307 LW |
1523 | SvPVX(sv) = pv; |
1524 | SvCUR(sv) = cur; | |
1525 | SvLEN(sv) = len; | |
1526 | SvIVX(sv) = iv; | |
1527 | SvNVX(sv) = nv; | |
1528 | SvMAGIC(sv) = magic; | |
1529 | SvSTASH(sv) = stash; | |
85e6fe83 | 1530 | IoPAGE_LEN(sv) = 60; |
8990e307 LW |
1531 | break; |
1532 | } | |
1533 | SvFLAGS(sv) &= ~SVTYPEMASK; | |
1534 | SvFLAGS(sv) |= mt; | |
79072805 LW |
1535 | return TRUE; |
1536 | } | |
1537 | ||
645c22ef DM |
1538 | /* |
1539 | =for apidoc sv_backoff | |
1540 | ||
1541 | Remove any string offset. You should normally use the C<SvOOK_off> macro | |
1542 | wrapper instead. | |
1543 | ||
1544 | =cut | |
1545 | */ | |
1546 | ||
79072805 | 1547 | int |
864dbfa3 | 1548 | Perl_sv_backoff(pTHX_ register SV *sv) |
79072805 LW |
1549 | { |
1550 | assert(SvOOK(sv)); | |
463ee0b2 LW |
1551 | if (SvIVX(sv)) { |
1552 | char *s = SvPVX(sv); | |
1553 | SvLEN(sv) += SvIVX(sv); | |
1554 | SvPVX(sv) -= SvIVX(sv); | |
79072805 | 1555 | SvIV_set(sv, 0); |
463ee0b2 | 1556 | Move(s, SvPVX(sv), SvCUR(sv)+1, char); |
79072805 LW |
1557 | } |
1558 | SvFLAGS(sv) &= ~SVf_OOK; | |
a0d0e21e | 1559 | return 0; |
79072805 LW |
1560 | } |
1561 | ||
954c1994 GS |
1562 | /* |
1563 | =for apidoc sv_grow | |
1564 | ||
645c22ef DM |
1565 | Expands the character buffer in the SV. If necessary, uses C<sv_unref> and |
1566 | upgrades the SV to C<SVt_PV>. Returns a pointer to the character buffer. | |
1567 | Use the C<SvGROW> wrapper instead. | |
954c1994 GS |
1568 | |
1569 | =cut | |
1570 | */ | |
1571 | ||
79072805 | 1572 | char * |
864dbfa3 | 1573 | Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen) |
79072805 LW |
1574 | { |
1575 | register char *s; | |
1576 | ||
54f0641b NIS |
1577 | |
1578 | ||
55497cff | 1579 | #ifdef HAS_64K_LIMIT |
79072805 | 1580 | if (newlen >= 0x10000) { |
1d7c1841 GS |
1581 | PerlIO_printf(Perl_debug_log, |
1582 | "Allocation too large: %"UVxf"\n", (UV)newlen); | |
79072805 LW |
1583 | my_exit(1); |
1584 | } | |
55497cff | 1585 | #endif /* HAS_64K_LIMIT */ |
a0d0e21e LW |
1586 | if (SvROK(sv)) |
1587 | sv_unref(sv); | |
79072805 LW |
1588 | if (SvTYPE(sv) < SVt_PV) { |
1589 | sv_upgrade(sv, SVt_PV); | |
463ee0b2 | 1590 | s = SvPVX(sv); |
79072805 LW |
1591 | } |
1592 | else if (SvOOK(sv)) { /* pv is offset? */ | |
1593 | sv_backoff(sv); | |
463ee0b2 | 1594 | s = SvPVX(sv); |
79072805 LW |
1595 | if (newlen > SvLEN(sv)) |
1596 | newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */ | |
c6f8c383 GA |
1597 | #ifdef HAS_64K_LIMIT |
1598 | if (newlen >= 0x10000) | |
1599 | newlen = 0xFFFF; | |
1600 | #endif | |
79072805 LW |
1601 | } |
1602 | else | |
463ee0b2 | 1603 | s = SvPVX(sv); |
54f0641b | 1604 | |
79072805 | 1605 | if (newlen > SvLEN(sv)) { /* need more room? */ |
8d6dde3e | 1606 | if (SvLEN(sv) && s) { |
2fe0f101 | 1607 | #ifdef MYMALLOC |
8d6dde3e IZ |
1608 | STRLEN l = malloced_size((void*)SvPVX(sv)); |
1609 | if (newlen <= l) { | |
1610 | SvLEN_set(sv, l); | |
1611 | return s; | |
1612 | } else | |
c70c8a0a | 1613 | #endif |
79072805 | 1614 | Renew(s,newlen,char); |
8d6dde3e | 1615 | } |
4e83176d | 1616 | else { |
ee5f0761 AMS |
1617 | /* sv_force_normal_flags() must not try to unshare the new |
1618 | PVX we allocate below. AMS 20010713 */ | |
4e83176d | 1619 | if (SvREADONLY(sv) && SvFAKE(sv)) { |
4e83176d AMS |
1620 | SvFAKE_off(sv); |
1621 | SvREADONLY_off(sv); | |
4e83176d AMS |
1622 | } |
1623 | New(703, s, newlen, char); | |
40565179 | 1624 | if (SvPVX(sv) && SvCUR(sv)) { |
54f0641b | 1625 | Move(SvPVX(sv), s, (newlen < SvCUR(sv)) ? newlen : SvCUR(sv), char); |
40565179 | 1626 | } |
4e83176d | 1627 | } |
79072805 LW |
1628 | SvPV_set(sv, s); |
1629 | SvLEN_set(sv, newlen); | |
1630 | } | |
1631 | return s; | |
1632 | } | |
1633 | ||
954c1994 GS |
1634 | /* |
1635 | =for apidoc sv_setiv | |
1636 | ||
645c22ef DM |
1637 | Copies an integer into the given SV, upgrading first if necessary. |
1638 | Does not handle 'set' magic. See also C<sv_setiv_mg>. | |
954c1994 GS |
1639 | |
1640 | =cut | |
1641 | */ | |
1642 | ||
79072805 | 1643 | void |
864dbfa3 | 1644 | Perl_sv_setiv(pTHX_ register SV *sv, IV i) |
79072805 | 1645 | { |
2213622d | 1646 | SV_CHECK_THINKFIRST(sv); |
463ee0b2 LW |
1647 | switch (SvTYPE(sv)) { |
1648 | case SVt_NULL: | |
79072805 | 1649 | sv_upgrade(sv, SVt_IV); |
463ee0b2 LW |
1650 | break; |
1651 | case SVt_NV: | |
1652 | sv_upgrade(sv, SVt_PVNV); | |
1653 | break; | |
ed6116ce | 1654 | case SVt_RV: |
463ee0b2 | 1655 | case SVt_PV: |
79072805 | 1656 | sv_upgrade(sv, SVt_PVIV); |
463ee0b2 | 1657 | break; |
a0d0e21e LW |
1658 | |
1659 | case SVt_PVGV: | |
a0d0e21e LW |
1660 | case SVt_PVAV: |
1661 | case SVt_PVHV: | |
1662 | case SVt_PVCV: | |
1663 | case SVt_PVFM: | |
1664 | case SVt_PVIO: | |
411caa50 | 1665 | Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0), |
53e06cf0 | 1666 | OP_DESC(PL_op)); |
463ee0b2 | 1667 | } |
a0d0e21e | 1668 | (void)SvIOK_only(sv); /* validate number */ |
a5f75d66 | 1669 | SvIVX(sv) = i; |
463ee0b2 | 1670 | SvTAINT(sv); |
79072805 LW |
1671 | } |
1672 | ||
954c1994 GS |
1673 | /* |
1674 | =for apidoc sv_setiv_mg | |
1675 | ||
1676 | Like C<sv_setiv>, but also handles 'set' magic. | |
1677 | ||
1678 | =cut | |
1679 | */ | |
1680 | ||
79072805 | 1681 | void |
864dbfa3 | 1682 | Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i) |
ef50df4b GS |
1683 | { |
1684 | sv_setiv(sv,i); | |
1685 | SvSETMAGIC(sv); | |
1686 | } | |
1687 | ||
954c1994 GS |
1688 | /* |
1689 | =for apidoc sv_setuv | |
1690 | ||
645c22ef DM |
1691 | Copies an unsigned integer into the given SV, upgrading first if necessary. |
1692 | Does not handle 'set' magic. See also C<sv_setuv_mg>. | |
954c1994 GS |
1693 | |
1694 | =cut | |
1695 | */ | |
1696 | ||
ef50df4b | 1697 | void |
864dbfa3 | 1698 | Perl_sv_setuv(pTHX_ register SV *sv, UV u) |
55497cff | 1699 | { |
55ada374 NC |
1700 | /* With these two if statements: |
1701 | u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865 | |
d460ef45 | 1702 | |
55ada374 NC |
1703 | without |
1704 | u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865 | |
d460ef45 | 1705 | |
55ada374 NC |
1706 | If you wish to remove them, please benchmark to see what the effect is |
1707 | */ | |
28e5dec8 JH |
1708 | if (u <= (UV)IV_MAX) { |
1709 | sv_setiv(sv, (IV)u); | |
1710 | return; | |
1711 | } | |
25da4f38 IZ |
1712 | sv_setiv(sv, 0); |
1713 | SvIsUV_on(sv); | |
1714 | SvUVX(sv) = u; | |
55497cff | 1715 | } |
1716 | ||
954c1994 GS |
1717 | /* |
1718 | =for apidoc sv_setuv_mg | |
1719 | ||
1720 | Like C<sv_setuv>, but also handles 'set' magic. | |
1721 | ||
1722 | =cut | |
1723 | */ | |
1724 | ||
55497cff | 1725 | void |
864dbfa3 | 1726 | Perl_sv_setuv_mg(pTHX_ register SV *sv, UV u) |
ef50df4b | 1727 | { |
55ada374 NC |
1728 | /* With these two if statements: |
1729 | u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865 | |
d460ef45 | 1730 | |
55ada374 NC |
1731 | without |
1732 | u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865 | |
d460ef45 | 1733 | |
55ada374 NC |
1734 | If you wish to remove them, please benchmark to see what the effect is |
1735 | */ | |
28e5dec8 JH |
1736 | if (u <= (UV)IV_MAX) { |
1737 | sv_setiv(sv, (IV)u); | |
1738 | } else { | |
1739 | sv_setiv(sv, 0); | |
1740 | SvIsUV_on(sv); | |
1741 | sv_setuv(sv,u); | |
1742 | } | |
ef50df4b GS |
1743 | SvSETMAGIC(sv); |
1744 | } | |
1745 | ||
954c1994 GS |
1746 | /* |
1747 | =for apidoc sv_setnv | |
1748 | ||
645c22ef DM |
1749 | Copies a double into the given SV, upgrading first if necessary. |
1750 | Does not handle 'set' magic. See also C<sv_setnv_mg>. | |
954c1994 GS |
1751 | |
1752 | =cut | |
1753 | */ | |
1754 | ||
ef50df4b | 1755 | void |
65202027 | 1756 | Perl_sv_setnv(pTHX_ register SV *sv, NV num) |
79072805 | 1757 | { |
2213622d | 1758 | SV_CHECK_THINKFIRST(sv); |
a0d0e21e LW |
1759 | switch (SvTYPE(sv)) { |
1760 | case SVt_NULL: | |
1761 | case SVt_IV: | |
79072805 | 1762 | sv_upgrade(sv, SVt_NV); |
a0d0e21e | 1763 | break; |
a0d0e21e LW |
1764 | case SVt_RV: |
1765 | case SVt_PV: | |
1766 | case SVt_PVIV: | |
79072805 | 1767 | sv_upgrade(sv, SVt_PVNV); |
a0d0e21e | 1768 | break; |
827b7e14 | 1769 | |
a0d0e21e | 1770 | case SVt_PVGV: |
a0d0e21e LW |
1771 | case SVt_PVAV: |
1772 | case SVt_PVHV: | |
1773 | case SVt_PVCV: | |
1774 | case SVt_PVFM: | |
1775 | case SVt_PVIO: | |
411caa50 | 1776 | Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0), |
53e06cf0 | 1777 | OP_NAME(PL_op)); |
79072805 | 1778 | } |
463ee0b2 | 1779 | SvNVX(sv) = num; |
a0d0e21e | 1780 | (void)SvNOK_only(sv); /* validate number */ |
463ee0b2 | 1781 | SvTAINT(sv); |
79072805 LW |
1782 | } |
1783 | ||
954c1994 GS |
1784 | /* |
1785 | =for apidoc sv_setnv_mg | |
1786 | ||
1787 | Like C<sv_setnv>, but also handles 'set' magic. | |
1788 | ||
1789 | =cut | |
1790 | */ | |
1791 | ||
ef50df4b | 1792 | void |
65202027 | 1793 | Perl_sv_setnv_mg(pTHX_ register SV *sv, NV num) |
ef50df4b GS |
1794 | { |
1795 | sv_setnv(sv,num); | |
1796 | SvSETMAGIC(sv); | |
1797 | } | |
1798 | ||
645c22ef DM |
1799 | /* Print an "isn't numeric" warning, using a cleaned-up, |
1800 | * printable version of the offending string | |
1801 | */ | |
1802 | ||
76e3520e | 1803 | STATIC void |
cea2e8a9 | 1804 | S_not_a_number(pTHX_ SV *sv) |
a0d0e21e | 1805 | { |
94463019 JH |
1806 | SV *dsv; |
1807 | char tmpbuf[64]; | |
1808 | char *pv; | |
1809 | ||
1810 | if (DO_UTF8(sv)) { | |
1811 | dsv = sv_2mortal(newSVpv("", 0)); | |
1812 | pv = sv_uni_display(dsv, sv, 10, 0); | |
1813 | } else { | |
1814 | char *d = tmpbuf; | |
1815 | char *limit = tmpbuf + sizeof(tmpbuf) - 8; | |
1816 | /* each *s can expand to 4 chars + "...\0", | |
1817 | i.e. need room for 8 chars */ | |
ecdeb87c | 1818 | |
94463019 JH |
1819 | char *s, *end; |
1820 | for (s = SvPVX(sv), end = s + SvCUR(sv); s < end && d < limit; s++) { | |
1821 | int ch = *s & 0xFF; | |
1822 | if (ch & 128 && !isPRINT_LC(ch)) { | |
1823 | *d++ = 'M'; | |
1824 | *d++ = '-'; | |
1825 | ch &= 127; | |
1826 | } | |
1827 | if (ch == '\n') { | |
1828 | *d++ = '\\'; | |
1829 | *d++ = 'n'; | |
1830 | } | |
1831 | else if (ch == '\r') { | |
1832 | *d++ = '\\'; | |
1833 | *d++ = 'r'; | |
1834 | } | |
1835 | else if (ch == '\f') { | |
1836 | *d++ = '\\'; | |
1837 | *d++ = 'f'; | |
1838 | } | |
1839 | else if (ch == '\\') { | |
1840 | *d++ = '\\'; | |
1841 | *d++ = '\\'; | |
1842 | } | |
1843 | else if (ch == '\0') { | |
1844 | *d++ = '\\'; | |
1845 | *d++ = '0'; | |
1846 | } | |
1847 | else if (isPRINT_LC(ch)) | |
1848 | *d++ = ch; | |
1849 | else { | |
1850 | *d++ = '^'; | |
1851 | *d++ = toCTRL(ch); | |
1852 | } | |
1853 | } | |
1854 | if (s < end) { | |
1855 | *d++ = '.'; | |
1856 | *d++ = '.'; | |
1857 | *d++ = '.'; | |
1858 | } | |
1859 | *d = '\0'; | |
1860 | pv = tmpbuf; | |
a0d0e21e | 1861 | } |
a0d0e21e | 1862 | |
533c011a | 1863 | if (PL_op) |
9014280d | 1864 | Perl_warner(aTHX_ packWARN(WARN_NUMERIC), |
94463019 JH |
1865 | "Argument \"%s\" isn't numeric in %s", pv, |
1866 | OP_DESC(PL_op)); | |
a0d0e21e | 1867 | else |
9014280d | 1868 | Perl_warner(aTHX_ packWARN(WARN_NUMERIC), |
94463019 | 1869 | "Argument \"%s\" isn't numeric", pv); |
a0d0e21e LW |
1870 | } |
1871 | ||
c2988b20 NC |
1872 | /* |
1873 | =for apidoc looks_like_number | |
1874 | ||
645c22ef DM |
1875 | Test if the content of an SV looks like a number (or is a number). |
1876 | C<Inf> and C<Infinity> are treated as numbers (so will not issue a | |
1877 | non-numeric warning), even if your atof() doesn't grok them. | |
c2988b20 NC |
1878 | |
1879 | =cut | |
1880 | */ | |
1881 | ||
1882 | I32 | |
1883 | Perl_looks_like_number(pTHX_ SV *sv) | |
1884 | { | |
1885 | register char *sbegin; | |
1886 | STRLEN len; | |
1887 | ||
1888 | if (SvPOK(sv)) { | |
1889 | sbegin = SvPVX(sv); | |
1890 | len = SvCUR(sv); | |
1891 | } | |
1892 | else if (SvPOKp(sv)) | |
1893 | sbegin = SvPV(sv, len); | |
1894 | else | |
1895 | return 1; /* Historic. Wrong? */ | |
1896 | return grok_number(sbegin, len, NULL); | |
1897 | } | |
25da4f38 IZ |
1898 | |
1899 | /* Actually, ISO C leaves conversion of UV to IV undefined, but | |
1900 | until proven guilty, assume that things are not that bad... */ | |
1901 | ||
645c22ef DM |
1902 | /* |
1903 | NV_PRESERVES_UV: | |
1904 | ||
1905 | As 64 bit platforms often have an NV that doesn't preserve all bits of | |
28e5dec8 JH |
1906 | an IV (an assumption perl has been based on to date) it becomes necessary |
1907 | to remove the assumption that the NV always carries enough precision to | |
1908 | recreate the IV whenever needed, and that the NV is the canonical form. | |
1909 | Instead, IV/UV and NV need to be given equal rights. So as to not lose | |
645c22ef | 1910 | precision as a side effect of conversion (which would lead to insanity |
28e5dec8 JH |
1911 | and the dragon(s) in t/op/numconvert.t getting very angry) the intent is |
1912 | 1) to distinguish between IV/UV/NV slots that have cached a valid | |
1913 | conversion where precision was lost and IV/UV/NV slots that have a | |
1914 | valid conversion which has lost no precision | |
645c22ef | 1915 | 2) to ensure that if a numeric conversion to one form is requested that |
28e5dec8 JH |
1916 | would lose precision, the precise conversion (or differently |
1917 | imprecise conversion) is also performed and cached, to prevent | |
1918 | requests for different numeric formats on the same SV causing | |
1919 | lossy conversion chains. (lossless conversion chains are perfectly | |
1920 | acceptable (still)) | |
1921 | ||
1922 | ||
1923 | flags are used: | |
1924 | SvIOKp is true if the IV slot contains a valid value | |
1925 | SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true) | |
1926 | SvNOKp is true if the NV slot contains a valid value | |
1927 | SvNOK is true only if the NV value is accurate | |
1928 | ||
1929 | so | |
645c22ef | 1930 | while converting from PV to NV, check to see if converting that NV to an |
28e5dec8 JH |
1931 | IV(or UV) would lose accuracy over a direct conversion from PV to |
1932 | IV(or UV). If it would, cache both conversions, return NV, but mark | |
1933 | SV as IOK NOKp (ie not NOK). | |
1934 | ||
645c22ef | 1935 | While converting from PV to IV, check to see if converting that IV to an |
28e5dec8 JH |
1936 | NV would lose accuracy over a direct conversion from PV to NV. If it |
1937 | would, cache both conversions, flag similarly. | |
1938 | ||
1939 | Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite | |
1940 | correctly because if IV & NV were set NV *always* overruled. | |
645c22ef DM |
1941 | Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flag's meaning |
1942 | changes - now IV and NV together means that the two are interchangeable: | |
28e5dec8 | 1943 | SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX; |
d460ef45 | 1944 | |
645c22ef DM |
1945 | The benefit of this is that operations such as pp_add know that if |
1946 | SvIOK is true for both left and right operands, then integer addition | |
1947 | can be used instead of floating point (for cases where the result won't | |
1948 | overflow). Before, floating point was always used, which could lead to | |
28e5dec8 JH |
1949 | loss of precision compared with integer addition. |
1950 | ||
1951 | * making IV and NV equal status should make maths accurate on 64 bit | |
1952 | platforms | |
1953 | * may speed up maths somewhat if pp_add and friends start to use | |
645c22ef | 1954 | integers when possible instead of fp. (Hopefully the overhead in |
28e5dec8 JH |
1955 | looking for SvIOK and checking for overflow will not outweigh the |
1956 | fp to integer speedup) | |
1957 | * will slow down integer operations (callers of SvIV) on "inaccurate" | |
1958 | values, as the change from SvIOK to SvIOKp will cause a call into | |
1959 | sv_2iv each time rather than a macro access direct to the IV slot | |
1960 | * should speed up number->string conversion on integers as IV is | |
645c22ef | 1961 | favoured when IV and NV are equally accurate |
28e5dec8 JH |
1962 | |
1963 | #################################################################### | |
645c22ef DM |
1964 | You had better be using SvIOK_notUV if you want an IV for arithmetic: |
1965 | SvIOK is true if (IV or UV), so you might be getting (IV)SvUV. | |
1966 | On the other hand, SvUOK is true iff UV. | |
28e5dec8 JH |
1967 | #################################################################### |
1968 | ||
645c22ef | 1969 | Your mileage will vary depending your CPU's relative fp to integer |
28e5dec8 JH |
1970 | performance ratio. |
1971 | */ | |
1972 | ||
1973 | #ifndef NV_PRESERVES_UV | |
645c22ef DM |
1974 | # define IS_NUMBER_UNDERFLOW_IV 1 |
1975 | # define IS_NUMBER_UNDERFLOW_UV 2 | |
1976 | # define IS_NUMBER_IV_AND_UV 2 | |
1977 | # define IS_NUMBER_OVERFLOW_IV 4 | |
1978 | # define IS_NUMBER_OVERFLOW_UV 5 | |
1979 | ||
1980 | /* sv_2iuv_non_preserve(): private routine for use by sv_2iv() and sv_2uv() */ | |
28e5dec8 JH |
1981 | |
1982 | /* For sv_2nv these three cases are "SvNOK and don't bother casting" */ | |
1983 | STATIC int | |
645c22ef | 1984 | S_sv_2iuv_non_preserve(pTHX_ register SV *sv, I32 numtype) |
28e5dec8 | 1985 | { |
1779d84d | 1986 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_2iuv_non '%s', IV=0x%"UVxf" NV=%"NVgf" inttype=%"UVXf"\n", SvPVX(sv), SvIVX(sv), SvNVX(sv), (UV)numtype)); |
28e5dec8 JH |
1987 | if (SvNVX(sv) < (NV)IV_MIN) { |
1988 | (void)SvIOKp_on(sv); | |
1989 | (void)SvNOK_on(sv); | |
1990 | SvIVX(sv) = IV_MIN; | |
1991 | return IS_NUMBER_UNDERFLOW_IV; | |
1992 | } | |
1993 | if (SvNVX(sv) > (NV)UV_MAX) { | |
1994 | (void)SvIOKp_on(sv); | |
1995 | (void)SvNOK_on(sv); | |
1996 | SvIsUV_on(sv); | |
1997 | SvUVX(sv) = UV_MAX; | |
1998 | return IS_NUMBER_OVERFLOW_UV; | |
1999 | } | |
c2988b20 NC |
2000 | (void)SvIOKp_on(sv); |
2001 | (void)SvNOK_on(sv); | |
2002 | /* Can't use strtol etc to convert this string. (See truth table in | |
2003 | sv_2iv */ | |
2004 | if (SvNVX(sv) <= (UV)IV_MAX) { | |
2005 | SvIVX(sv) = I_V(SvNVX(sv)); | |
2006 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) { | |
2007 | SvIOK_on(sv); /* Integer is precise. NOK, IOK */ | |
2008 | } else { | |
2009 | /* Integer is imprecise. NOK, IOKp */ | |
2010 | } | |
2011 | return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV; | |
2012 | } | |
2013 | SvIsUV_on(sv); | |
2014 | SvUVX(sv) = U_V(SvNVX(sv)); | |
2015 | if ((NV)(SvUVX(sv)) == SvNVX(sv)) { | |
2016 | if (SvUVX(sv) == UV_MAX) { | |
2017 | /* As we know that NVs don't preserve UVs, UV_MAX cannot | |
2018 | possibly be preserved by NV. Hence, it must be overflow. | |
2019 | NOK, IOKp */ | |
2020 | return IS_NUMBER_OVERFLOW_UV; | |
2021 | } | |
2022 | SvIOK_on(sv); /* Integer is precise. NOK, UOK */ | |
2023 | } else { | |
2024 | /* Integer is imprecise. NOK, IOKp */ | |
28e5dec8 | 2025 | } |
c2988b20 | 2026 | return IS_NUMBER_OVERFLOW_IV; |
28e5dec8 | 2027 | } |
645c22ef DM |
2028 | #endif /* !NV_PRESERVES_UV*/ |
2029 | ||
2030 | /* | |
2031 | =for apidoc sv_2iv | |
2032 | ||
2033 | Return the integer value of an SV, doing any necessary string conversion, | |
2034 | magic etc. Normally used via the C<SvIV(sv)> and C<SvIVx(sv)> macros. | |
2035 | ||
2036 | =cut | |
2037 | */ | |
28e5dec8 | 2038 | |
a0d0e21e | 2039 | IV |
864dbfa3 | 2040 | Perl_sv_2iv(pTHX_ register SV *sv) |
79072805 LW |
2041 | { |
2042 | if (!sv) | |
2043 | return 0; | |
8990e307 | 2044 | if (SvGMAGICAL(sv)) { |
463ee0b2 LW |
2045 | mg_get(sv); |
2046 | if (SvIOKp(sv)) | |
2047 | return SvIVX(sv); | |
748a9306 | 2048 | if (SvNOKp(sv)) { |
25da4f38 | 2049 | return I_V(SvNVX(sv)); |
748a9306 | 2050 | } |
36477c24 | 2051 | if (SvPOKp(sv) && SvLEN(sv)) |
2052 | return asIV(sv); | |
3fe9a6f1 | 2053 | if (!SvROK(sv)) { |
d008e5eb | 2054 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 2055 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
1d7c1841 | 2056 | report_uninit(); |
c6ee37c5 | 2057 | } |
36477c24 | 2058 | return 0; |
3fe9a6f1 | 2059 | } |
463ee0b2 | 2060 | } |
ed6116ce | 2061 | if (SvTHINKFIRST(sv)) { |
a0d0e21e | 2062 | if (SvROK(sv)) { |
a0d0e21e | 2063 | SV* tmpstr; |
1554e226 | 2064 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) && |
b4b9a328 | 2065 | (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) |
9e7bc3e8 | 2066 | return SvIV(tmpstr); |
56431972 | 2067 | return PTR2IV(SvRV(sv)); |
a0d0e21e | 2068 | } |
47deb5e7 NIS |
2069 | if (SvREADONLY(sv) && SvFAKE(sv)) { |
2070 | sv_force_normal(sv); | |
2071 | } | |
0336b60e | 2072 | if (SvREADONLY(sv) && !SvOK(sv)) { |
0336b60e | 2073 | if (ckWARN(WARN_UNINITIALIZED)) |
1d7c1841 | 2074 | report_uninit(); |
ed6116ce LW |
2075 | return 0; |
2076 | } | |
79072805 | 2077 | } |
25da4f38 IZ |
2078 | if (SvIOKp(sv)) { |
2079 | if (SvIsUV(sv)) { | |
2080 | return (IV)(SvUVX(sv)); | |
2081 | } | |
2082 | else { | |
2083 | return SvIVX(sv); | |
2084 | } | |
463ee0b2 | 2085 | } |
748a9306 | 2086 | if (SvNOKp(sv)) { |
28e5dec8 JH |
2087 | /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv |
2088 | * without also getting a cached IV/UV from it at the same time | |
2089 | * (ie PV->NV conversion should detect loss of accuracy and cache | |
2090 | * IV or UV at same time to avoid this. NWC */ | |
25da4f38 IZ |
2091 | |
2092 | if (SvTYPE(sv) == SVt_NV) | |
2093 | sv_upgrade(sv, SVt_PVNV); | |
2094 | ||
28e5dec8 JH |
2095 | (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */ |
2096 | /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost | |
2097 | certainly cast into the IV range at IV_MAX, whereas the correct | |
2098 | answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary | |
2099 | cases go to UV */ | |
2100 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
748a9306 | 2101 | SvIVX(sv) = I_V(SvNVX(sv)); |
28e5dec8 JH |
2102 | if (SvNVX(sv) == (NV) SvIVX(sv) |
2103 | #ifndef NV_PRESERVES_UV | |
2104 | && (((UV)1 << NV_PRESERVES_UV_BITS) > | |
2105 | (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv))) | |
2106 | /* Don't flag it as "accurately an integer" if the number | |
2107 | came from a (by definition imprecise) NV operation, and | |
2108 | we're outside the range of NV integer precision */ | |
2109 | #endif | |
2110 | ) { | |
2111 | SvIOK_on(sv); /* Can this go wrong with rounding? NWC */ | |
2112 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
7234c960 | 2113 | "0x%"UVxf" iv(%"NVgf" => %"IVdf") (precise)\n", |
28e5dec8 JH |
2114 | PTR2UV(sv), |
2115 | SvNVX(sv), | |
2116 | SvIVX(sv))); | |
2117 | ||
2118 | } else { | |
2119 | /* IV not precise. No need to convert from PV, as NV | |
2120 | conversion would already have cached IV if it detected | |
2121 | that PV->IV would be better than PV->NV->IV | |
2122 | flags already correct - don't set public IOK. */ | |
2123 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
7234c960 | 2124 | "0x%"UVxf" iv(%"NVgf" => %"IVdf") (imprecise)\n", |
28e5dec8 JH |
2125 | PTR2UV(sv), |
2126 | SvNVX(sv), | |
2127 | SvIVX(sv))); | |
2128 | } | |
2129 | /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN, | |
2130 | but the cast (NV)IV_MIN rounds to a the value less (more | |
2131 | negative) than IV_MIN which happens to be equal to SvNVX ?? | |
2132 | Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and | |
2133 | NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and | |
2134 | (NV)UVX == NVX are both true, but the values differ. :-( | |
2135 | Hopefully for 2s complement IV_MIN is something like | |
2136 | 0x8000000000000000 which will be exact. NWC */ | |
d460ef45 | 2137 | } |
25da4f38 | 2138 | else { |
ff68c719 | 2139 | SvUVX(sv) = U_V(SvNVX(sv)); |
28e5dec8 JH |
2140 | if ( |
2141 | (SvNVX(sv) == (NV) SvUVX(sv)) | |
2142 | #ifndef NV_PRESERVES_UV | |
2143 | /* Make sure it's not 0xFFFFFFFFFFFFFFFF */ | |
2144 | /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */ | |
2145 | && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv)) | |
2146 | /* Don't flag it as "accurately an integer" if the number | |
2147 | came from a (by definition imprecise) NV operation, and | |
2148 | we're outside the range of NV integer precision */ | |
2149 | #endif | |
2150 | ) | |
2151 | SvIOK_on(sv); | |
25da4f38 IZ |
2152 | SvIsUV_on(sv); |
2153 | ret_iv_max: | |
1c846c1f | 2154 | DEBUG_c(PerlIO_printf(Perl_debug_log, |
57def98f | 2155 | "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n", |
56431972 | 2156 | PTR2UV(sv), |
57def98f JH |
2157 | SvUVX(sv), |
2158 | SvUVX(sv))); | |
25da4f38 IZ |
2159 | return (IV)SvUVX(sv); |
2160 | } | |
748a9306 LW |
2161 | } |
2162 | else if (SvPOKp(sv) && SvLEN(sv)) { | |
c2988b20 NC |
2163 | UV value; |
2164 | int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value); | |
25da4f38 IZ |
2165 | /* We want to avoid a possible problem when we cache an IV which |
2166 | may be later translated to an NV, and the resulting NV is not | |
c2988b20 NC |
2167 | the same as the direct translation of the initial string |
2168 | (eg 123.456 can shortcut to the IV 123 with atol(), but we must | |
2169 | be careful to ensure that the value with the .456 is around if the | |
2170 | NV value is requested in the future). | |
1c846c1f | 2171 | |
25da4f38 IZ |
2172 | This means that if we cache such an IV, we need to cache the |
2173 | NV as well. Moreover, we trade speed for space, and do not | |
28e5dec8 | 2174 | cache the NV if we are sure it's not needed. |
25da4f38 | 2175 | */ |
16b7a9a4 | 2176 | |
c2988b20 NC |
2177 | /* SVt_PVNV is one higher than SVt_PVIV, hence this order */ |
2178 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) | |
2179 | == IS_NUMBER_IN_UV) { | |
5e045b90 | 2180 | /* It's definitely an integer, only upgrade to PVIV */ |
28e5dec8 JH |
2181 | if (SvTYPE(sv) < SVt_PVIV) |
2182 | sv_upgrade(sv, SVt_PVIV); | |
f7bbb42a | 2183 | (void)SvIOK_on(sv); |
c2988b20 NC |
2184 | } else if (SvTYPE(sv) < SVt_PVNV) |
2185 | sv_upgrade(sv, SVt_PVNV); | |
28e5dec8 | 2186 | |
c2988b20 NC |
2187 | /* If NV preserves UV then we only use the UV value if we know that |
2188 | we aren't going to call atof() below. If NVs don't preserve UVs | |
2189 | then the value returned may have more precision than atof() will | |
2190 | return, even though value isn't perfectly accurate. */ | |
2191 | if ((numtype & (IS_NUMBER_IN_UV | |
2192 | #ifdef NV_PRESERVES_UV | |
2193 | | IS_NUMBER_NOT_INT | |
2194 | #endif | |
2195 | )) == IS_NUMBER_IN_UV) { | |
2196 | /* This won't turn off the public IOK flag if it was set above */ | |
2197 | (void)SvIOKp_on(sv); | |
2198 | ||
2199 | if (!(numtype & IS_NUMBER_NEG)) { | |
2200 | /* positive */; | |
2201 | if (value <= (UV)IV_MAX) { | |
2202 | SvIVX(sv) = (IV)value; | |
2203 | } else { | |
2204 | SvUVX(sv) = value; | |
2205 | SvIsUV_on(sv); | |
2206 | } | |
2207 | } else { | |
2208 | /* 2s complement assumption */ | |
2209 | if (value <= (UV)IV_MIN) { | |
2210 | SvIVX(sv) = -(IV)value; | |
2211 | } else { | |
2212 | /* Too negative for an IV. This is a double upgrade, but | |
d1be9408 | 2213 | I'm assuming it will be rare. */ |
c2988b20 NC |
2214 | if (SvTYPE(sv) < SVt_PVNV) |
2215 | sv_upgrade(sv, SVt_PVNV); | |
2216 | SvNOK_on(sv); | |
2217 | SvIOK_off(sv); | |
2218 | SvIOKp_on(sv); | |
2219 | SvNVX(sv) = -(NV)value; | |
2220 | SvIVX(sv) = IV_MIN; | |
2221 | } | |
2222 | } | |
2223 | } | |
2224 | /* For !NV_PRESERVES_UV and IS_NUMBER_IN_UV and IS_NUMBER_NOT_INT we | |
2225 | will be in the previous block to set the IV slot, and the next | |
2226 | block to set the NV slot. So no else here. */ | |
2227 | ||
2228 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) | |
2229 | != IS_NUMBER_IN_UV) { | |
2230 | /* It wasn't an (integer that doesn't overflow the UV). */ | |
2231 | SvNVX(sv) = Atof(SvPVX(sv)); | |
28e5dec8 | 2232 | |
c2988b20 NC |
2233 | if (! numtype && ckWARN(WARN_NUMERIC)) |
2234 | not_a_number(sv); | |
28e5dec8 | 2235 | |
65202027 | 2236 | #if defined(USE_LONG_DOUBLE) |
c2988b20 NC |
2237 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n", |
2238 | PTR2UV(sv), SvNVX(sv))); | |
65202027 | 2239 | #else |
1779d84d | 2240 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"NVgf")\n", |
c2988b20 | 2241 | PTR2UV(sv), SvNVX(sv))); |
65202027 | 2242 | #endif |
28e5dec8 JH |
2243 | |
2244 | ||
2245 | #ifdef NV_PRESERVES_UV | |
c2988b20 NC |
2246 | (void)SvIOKp_on(sv); |
2247 | (void)SvNOK_on(sv); | |
2248 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
2249 | SvIVX(sv) = I_V(SvNVX(sv)); | |
2250 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) { | |
2251 | SvIOK_on(sv); | |
28e5dec8 | 2252 | } else { |
c2988b20 NC |
2253 | /* Integer is imprecise. NOK, IOKp */ |
2254 | } | |
2255 | /* UV will not work better than IV */ | |
2256 | } else { | |
2257 | if (SvNVX(sv) > (NV)UV_MAX) { | |
2258 | SvIsUV_on(sv); | |
2259 | /* Integer is inaccurate. NOK, IOKp, is UV */ | |
2260 | SvUVX(sv) = UV_MAX; | |
2261 | SvIsUV_on(sv); | |
2262 | } else { | |
2263 | SvUVX(sv) = U_V(SvNVX(sv)); | |
2264 | /* 0xFFFFFFFFFFFFFFFF not an issue in here */ | |
2265 | if ((NV)(SvUVX(sv)) == SvNVX(sv)) { | |
2266 | SvIOK_on(sv); | |
28e5dec8 JH |
2267 | SvIsUV_on(sv); |
2268 | } else { | |
c2988b20 NC |
2269 | /* Integer is imprecise. NOK, IOKp, is UV */ |
2270 | SvIsUV_on(sv); | |
28e5dec8 | 2271 | } |
28e5dec8 | 2272 | } |
c2988b20 NC |
2273 | goto ret_iv_max; |
2274 | } | |
28e5dec8 | 2275 | #else /* NV_PRESERVES_UV */ |
c2988b20 NC |
2276 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) |
2277 | == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) { | |
2278 | /* The IV slot will have been set from value returned by | |
2279 | grok_number above. The NV slot has just been set using | |
2280 | Atof. */ | |
560b0c46 | 2281 | SvNOK_on(sv); |
c2988b20 NC |
2282 | assert (SvIOKp(sv)); |
2283 | } else { | |
2284 | if (((UV)1 << NV_PRESERVES_UV_BITS) > | |
2285 | U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) { | |
2286 | /* Small enough to preserve all bits. */ | |
2287 | (void)SvIOKp_on(sv); | |
2288 | SvNOK_on(sv); | |
2289 | SvIVX(sv) = I_V(SvNVX(sv)); | |
2290 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) | |
2291 | SvIOK_on(sv); | |
2292 | /* Assumption: first non-preserved integer is < IV_MAX, | |
2293 | this NV is in the preserved range, therefore: */ | |
2294 | if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv)) | |
2295 | < (UV)IV_MAX)) { | |
6b6e5432 | 2296 | 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 |
2297 | } |
2298 | } else { | |
2299 | /* IN_UV NOT_INT | |
2300 | 0 0 already failed to read UV. | |
2301 | 0 1 already failed to read UV. | |
2302 | 1 0 you won't get here in this case. IV/UV | |
2303 | slot set, public IOK, Atof() unneeded. | |
2304 | 1 1 already read UV. | |
2305 | so there's no point in sv_2iuv_non_preserve() attempting | |
2306 | to use atol, strtol, strtoul etc. */ | |
2307 | if (sv_2iuv_non_preserve (sv, numtype) | |
2308 | >= IS_NUMBER_OVERFLOW_IV) | |
2309 | goto ret_iv_max; | |
2310 | } | |
2311 | } | |
28e5dec8 | 2312 | #endif /* NV_PRESERVES_UV */ |
25da4f38 | 2313 | } |
28e5dec8 | 2314 | } else { |
599cee73 | 2315 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP)) |
1d7c1841 | 2316 | report_uninit(); |
25da4f38 IZ |
2317 | if (SvTYPE(sv) < SVt_IV) |
2318 | /* Typically the caller expects that sv_any is not NULL now. */ | |
2319 | sv_upgrade(sv, SVt_IV); | |
a0d0e21e | 2320 | return 0; |
79072805 | 2321 | } |
1d7c1841 GS |
2322 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n", |
2323 | PTR2UV(sv),SvIVX(sv))); | |
25da4f38 | 2324 | return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv); |
79072805 LW |
2325 | } |
2326 | ||
645c22ef DM |
2327 | /* |
2328 | =for apidoc sv_2uv | |
2329 | ||
2330 | Return the unsigned integer value of an SV, doing any necessary string | |
2331 | conversion, magic etc. Normally used via the C<SvUV(sv)> and C<SvUVx(sv)> | |
2332 | macros. | |
2333 | ||
2334 | =cut | |
2335 | */ | |
2336 | ||
ff68c719 | 2337 | UV |
864dbfa3 | 2338 | Perl_sv_2uv(pTHX_ register SV *sv) |
ff68c719 | 2339 | { |
2340 | if (!sv) | |
2341 | return 0; | |
2342 | if (SvGMAGICAL(sv)) { | |
2343 | mg_get(sv); | |
2344 | if (SvIOKp(sv)) | |
2345 | return SvUVX(sv); | |
2346 | if (SvNOKp(sv)) | |
2347 | return U_V(SvNVX(sv)); | |
36477c24 | 2348 | if (SvPOKp(sv) && SvLEN(sv)) |
2349 | return asUV(sv); | |
3fe9a6f1 | 2350 | if (!SvROK(sv)) { |
d008e5eb | 2351 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 2352 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
1d7c1841 | 2353 | report_uninit(); |
c6ee37c5 | 2354 | } |
36477c24 | 2355 | return 0; |
3fe9a6f1 | 2356 | } |
ff68c719 | 2357 | } |
2358 | if (SvTHINKFIRST(sv)) { | |
2359 | if (SvROK(sv)) { | |
ff68c719 | 2360 | SV* tmpstr; |
1554e226 | 2361 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) && |
b4b9a328 | 2362 | (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) |
9e7bc3e8 | 2363 | return SvUV(tmpstr); |
56431972 | 2364 | return PTR2UV(SvRV(sv)); |
ff68c719 | 2365 | } |
8a818333 NIS |
2366 | if (SvREADONLY(sv) && SvFAKE(sv)) { |
2367 | sv_force_normal(sv); | |
2368 | } | |
0336b60e | 2369 | if (SvREADONLY(sv) && !SvOK(sv)) { |
0336b60e | 2370 | if (ckWARN(WARN_UNINITIALIZED)) |
1d7c1841 | 2371 | report_uninit(); |
ff68c719 | 2372 | return 0; |
2373 | } | |
2374 | } | |
25da4f38 IZ |
2375 | if (SvIOKp(sv)) { |
2376 | if (SvIsUV(sv)) { | |
2377 | return SvUVX(sv); | |
2378 | } | |
2379 | else { | |
2380 | return (UV)SvIVX(sv); | |
2381 | } | |
ff68c719 | 2382 | } |
2383 | if (SvNOKp(sv)) { | |
28e5dec8 JH |
2384 | /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv |
2385 | * without also getting a cached IV/UV from it at the same time | |
2386 | * (ie PV->NV conversion should detect loss of accuracy and cache | |
2387 | * IV or UV at same time to avoid this. */ | |
2388 | /* IV-over-UV optimisation - choose to cache IV if possible */ | |
2389 | ||
25da4f38 IZ |
2390 | if (SvTYPE(sv) == SVt_NV) |
2391 | sv_upgrade(sv, SVt_PVNV); | |
28e5dec8 JH |
2392 | |
2393 | (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */ | |
2394 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
f7bbb42a | 2395 | SvIVX(sv) = I_V(SvNVX(sv)); |
28e5dec8 JH |
2396 | if (SvNVX(sv) == (NV) SvIVX(sv) |
2397 | #ifndef NV_PRESERVES_UV | |
2398 | && (((UV)1 << NV_PRESERVES_UV_BITS) > | |
2399 | (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv))) | |
2400 | /* Don't flag it as "accurately an integer" if the number | |
2401 | came from a (by definition imprecise) NV operation, and | |
2402 | we're outside the range of NV integer precision */ | |
2403 | #endif | |
2404 | ) { | |
2405 | SvIOK_on(sv); /* Can this go wrong with rounding? NWC */ | |
2406 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
7234c960 | 2407 | "0x%"UVxf" uv(%"NVgf" => %"IVdf") (precise)\n", |
28e5dec8 JH |
2408 | PTR2UV(sv), |
2409 | SvNVX(sv), | |
2410 | SvIVX(sv))); | |
2411 | ||
2412 | } else { | |
2413 | /* IV not precise. No need to convert from PV, as NV | |
2414 | conversion would already have cached IV if it detected | |
2415 | that PV->IV would be better than PV->NV->IV | |
2416 | flags already correct - don't set public IOK. */ | |
2417 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
7234c960 | 2418 | "0x%"UVxf" uv(%"NVgf" => %"IVdf") (imprecise)\n", |
28e5dec8 JH |
2419 | PTR2UV(sv), |
2420 | SvNVX(sv), | |
2421 | SvIVX(sv))); | |
2422 | } | |
2423 | /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN, | |
2424 | but the cast (NV)IV_MIN rounds to a the value less (more | |
2425 | negative) than IV_MIN which happens to be equal to SvNVX ?? | |
2426 | Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and | |
2427 | NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and | |
2428 | (NV)UVX == NVX are both true, but the values differ. :-( | |
2429 | Hopefully for 2s complement IV_MIN is something like | |
2430 | 0x8000000000000000 which will be exact. NWC */ | |
d460ef45 | 2431 | } |
28e5dec8 JH |
2432 | else { |
2433 | SvUVX(sv) = U_V(SvNVX(sv)); | |
2434 | if ( | |
2435 | (SvNVX(sv) == (NV) SvUVX(sv)) | |
2436 | #ifndef NV_PRESERVES_UV | |
2437 | /* Make sure it's not 0xFFFFFFFFFFFFFFFF */ | |
2438 | /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */ | |
2439 | && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv)) | |
2440 | /* Don't flag it as "accurately an integer" if the number | |
2441 | came from a (by definition imprecise) NV operation, and | |
2442 | we're outside the range of NV integer precision */ | |
2443 | #endif | |
2444 | ) | |
2445 | SvIOK_on(sv); | |
2446 | SvIsUV_on(sv); | |
1c846c1f | 2447 | DEBUG_c(PerlIO_printf(Perl_debug_log, |
28e5dec8 | 2448 | "0x%"UVxf" 2uv(%"UVuf" => %"IVdf") (as unsigned)\n", |
57def98f | 2449 | PTR2UV(sv), |
28e5dec8 JH |
2450 | SvUVX(sv), |
2451 | SvUVX(sv))); | |
25da4f38 | 2452 | } |
ff68c719 | 2453 | } |
2454 | else if (SvPOKp(sv) && SvLEN(sv)) { | |
c2988b20 NC |
2455 | UV value; |
2456 | int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value); | |
25da4f38 IZ |
2457 | |
2458 | /* We want to avoid a possible problem when we cache a UV which | |
2459 | may be later translated to an NV, and the resulting NV is not | |
2460 | the translation of the initial data. | |
1c846c1f | 2461 | |
25da4f38 IZ |
2462 | This means that if we cache such a UV, we need to cache the |
2463 | NV as well. Moreover, we trade speed for space, and do not | |
2464 | cache the NV if not needed. | |
2465 | */ | |
16b7a9a4 | 2466 | |
c2988b20 NC |
2467 | /* SVt_PVNV is one higher than SVt_PVIV, hence this order */ |
2468 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) | |
2469 | == IS_NUMBER_IN_UV) { | |
5e045b90 | 2470 | /* It's definitely an integer, only upgrade to PVIV */ |
28e5dec8 | 2471 | if (SvTYPE(sv) < SVt_PVIV) |
f7bbb42a JH |
2472 | sv_upgrade(sv, SVt_PVIV); |
2473 | (void)SvIOK_on(sv); | |
c2988b20 NC |
2474 | } else if (SvTYPE(sv) < SVt_PVNV) |
2475 | sv_upgrade(sv, SVt_PVNV); | |
d460ef45 | 2476 | |
c2988b20 NC |
2477 | /* If NV preserves UV then we only use the UV value if we know that |
2478 | we aren't going to call atof() below. If NVs don't preserve UVs | |
2479 | then the value returned may have more precision than atof() will | |
2480 | return, even though it isn't accurate. */ | |
2481 | if ((numtype & (IS_NUMBER_IN_UV | |
2482 | #ifdef NV_PRESERVES_UV | |
2483 | | IS_NUMBER_NOT_INT | |
2484 | #endif | |
2485 | )) == IS_NUMBER_IN_UV) { | |
2486 | /* This won't turn off the public IOK flag if it was set above */ | |
2487 | (void)SvIOKp_on(sv); | |
2488 | ||
2489 | if (!(numtype & IS_NUMBER_NEG)) { | |
2490 | /* positive */; | |
2491 | if (value <= (UV)IV_MAX) { | |
2492 | SvIVX(sv) = (IV)value; | |
28e5dec8 JH |
2493 | } else { |
2494 | /* it didn't overflow, and it was positive. */ | |
c2988b20 | 2495 | SvUVX(sv) = value; |
28e5dec8 JH |
2496 | SvIsUV_on(sv); |
2497 | } | |
c2988b20 NC |
2498 | } else { |
2499 | /* 2s complement assumption */ | |
2500 | if (value <= (UV)IV_MIN) { | |
2501 | SvIVX(sv) = -(IV)value; | |
2502 | } else { | |
2503 | /* Too negative for an IV. This is a double upgrade, but | |
d1be9408 | 2504 | I'm assuming it will be rare. */ |
c2988b20 NC |
2505 | if (SvTYPE(sv) < SVt_PVNV) |
2506 | sv_upgrade(sv, SVt_PVNV); | |
2507 | SvNOK_on(sv); | |
2508 | SvIOK_off(sv); | |
2509 | SvIOKp_on(sv); | |
2510 | SvNVX(sv) = -(NV)value; | |
2511 | SvIVX(sv) = IV_MIN; | |
2512 | } | |
2513 | } | |
2514 | } | |
2515 | ||
2516 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) | |
2517 | != IS_NUMBER_IN_UV) { | |
2518 | /* It wasn't an integer, or it overflowed the UV. */ | |
2519 | SvNVX(sv) = Atof(SvPVX(sv)); | |
28e5dec8 | 2520 | |
c2988b20 | 2521 | if (! numtype && ckWARN(WARN_NUMERIC)) |
28e5dec8 JH |
2522 | not_a_number(sv); |
2523 | ||
2524 | #if defined(USE_LONG_DOUBLE) | |
c2988b20 NC |
2525 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%" PERL_PRIgldbl ")\n", |
2526 | PTR2UV(sv), SvNVX(sv))); | |
28e5dec8 | 2527 | #else |
1779d84d | 2528 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"NVgf")\n", |
c2988b20 | 2529 | PTR2UV(sv), SvNVX(sv))); |
28e5dec8 JH |
2530 | #endif |
2531 | ||
2532 | #ifdef NV_PRESERVES_UV | |
c2988b20 NC |
2533 | (void)SvIOKp_on(sv); |
2534 | (void)SvNOK_on(sv); | |
2535 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
2536 | SvIVX(sv) = I_V(SvNVX(sv)); | |
2537 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) { | |
2538 | SvIOK_on(sv); | |
2539 | } else { | |
2540 | /* Integer is imprecise. NOK, IOKp */ | |
2541 | } | |
2542 | /* UV will not work better than IV */ | |
2543 | } else { | |
2544 | if (SvNVX(sv) > (NV)UV_MAX) { | |
2545 | SvIsUV_on(sv); | |
2546 | /* Integer is inaccurate. NOK, IOKp, is UV */ | |
2547 | SvUVX(sv) = UV_MAX; | |
2548 | SvIsUV_on(sv); | |
2549 | } else { | |
2550 | SvUVX(sv) = U_V(SvNVX(sv)); | |
2551 | /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs | |
2552 | NV preservse UV so can do correct comparison. */ | |
2553 | if ((NV)(SvUVX(sv)) == SvNVX(sv)) { | |
2554 | SvIOK_on(sv); | |
2555 | SvIsUV_on(sv); | |
2556 | } else { | |
2557 | /* Integer is imprecise. NOK, IOKp, is UV */ | |
2558 | SvIsUV_on(sv); | |
2559 | } | |
2560 | } | |
2561 | } | |
28e5dec8 | 2562 | #else /* NV_PRESERVES_UV */ |
c2988b20 NC |
2563 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) |
2564 | == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) { | |
2565 | /* The UV slot will have been set from value returned by | |
2566 | grok_number above. The NV slot has just been set using | |
2567 | Atof. */ | |
560b0c46 | 2568 | SvNOK_on(sv); |
c2988b20 NC |
2569 | assert (SvIOKp(sv)); |
2570 | } else { | |
2571 | if (((UV)1 << NV_PRESERVES_UV_BITS) > | |
2572 | U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) { | |
2573 | /* Small enough to preserve all bits. */ | |
2574 | (void)SvIOKp_on(sv); | |
2575 | SvNOK_on(sv); | |
2576 | SvIVX(sv) = I_V(SvNVX(sv)); | |
2577 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) | |
2578 | SvIOK_on(sv); | |
2579 | /* Assumption: first non-preserved integer is < IV_MAX, | |
2580 | this NV is in the preserved range, therefore: */ | |
2581 | if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv)) | |
2582 | < (UV)IV_MAX)) { | |
6b6e5432 | 2583 | Perl_croak(aTHX_ "sv_2uv assumed (U_V(fabs((double)SvNVX(sv))) < (UV)IV_MAX) but SvNVX(sv)=%"NVgf" U_V is 0x%"UVxf", IV_MAX is 0x%"UVxf"\n", SvNVX(sv), U_V(SvNVX(sv)), (UV)IV_MAX); |
c2988b20 NC |
2584 | } |
2585 | } else | |
2586 | sv_2iuv_non_preserve (sv, numtype); | |
2587 | } | |
28e5dec8 | 2588 | #endif /* NV_PRESERVES_UV */ |
f7bbb42a | 2589 | } |
ff68c719 | 2590 | } |
2591 | else { | |
d008e5eb | 2592 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 2593 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
1d7c1841 | 2594 | report_uninit(); |
c6ee37c5 | 2595 | } |
25da4f38 IZ |
2596 | if (SvTYPE(sv) < SVt_IV) |
2597 | /* Typically the caller expects that sv_any is not NULL now. */ | |
2598 | sv_upgrade(sv, SVt_IV); | |
ff68c719 | 2599 | return 0; |
2600 | } | |
25da4f38 | 2601 | |
1d7c1841 GS |
2602 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n", |
2603 | PTR2UV(sv),SvUVX(sv))); | |
25da4f38 | 2604 | return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv); |
ff68c719 | 2605 | } |
2606 | ||
645c22ef DM |
2607 | /* |
2608 | =for apidoc sv_2nv | |
2609 | ||
2610 | Return the num value of an SV, doing any necessary string or integer | |
2611 | conversion, magic etc. Normally used via the C<SvNV(sv)> and C<SvNVx(sv)> | |
2612 | macros. | |
2613 | ||
2614 | =cut | |
2615 | */ | |
2616 | ||
65202027 | 2617 | NV |
864dbfa3 | 2618 | Perl_sv_2nv(pTHX_ register SV *sv) |
79072805 LW |
2619 | { |
2620 | if (!sv) | |
2621 | return 0.0; | |
8990e307 | 2622 | if (SvGMAGICAL(sv)) { |
463ee0b2 LW |
2623 | mg_get(sv); |
2624 | if (SvNOKp(sv)) | |
2625 | return SvNVX(sv); | |
a0d0e21e | 2626 | if (SvPOKp(sv) && SvLEN(sv)) { |
c2988b20 NC |
2627 | if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && |
2628 | !grok_number(SvPVX(sv), SvCUR(sv), NULL)) | |
a0d0e21e | 2629 | not_a_number(sv); |
097ee67d | 2630 | return Atof(SvPVX(sv)); |
a0d0e21e | 2631 | } |
25da4f38 | 2632 | if (SvIOKp(sv)) { |
1c846c1f | 2633 | if (SvIsUV(sv)) |
65202027 | 2634 | return (NV)SvUVX(sv); |
25da4f38 | 2635 | else |
65202027 | 2636 | return (NV)SvIVX(sv); |
25da4f38 | 2637 | } |
16d20bd9 | 2638 | if (!SvROK(sv)) { |
d008e5eb | 2639 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 2640 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
1d7c1841 | 2641 | report_uninit(); |
c6ee37c5 | 2642 | } |
16d20bd9 AD |
2643 | return 0; |
2644 | } | |
463ee0b2 | 2645 | } |
ed6116ce | 2646 | if (SvTHINKFIRST(sv)) { |
a0d0e21e | 2647 | if (SvROK(sv)) { |
a0d0e21e | 2648 | SV* tmpstr; |
1554e226 | 2649 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) && |
b4b9a328 | 2650 | (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) |
9e7bc3e8 | 2651 | return SvNV(tmpstr); |
56431972 | 2652 | return PTR2NV(SvRV(sv)); |
a0d0e21e | 2653 | } |
8a818333 NIS |
2654 | if (SvREADONLY(sv) && SvFAKE(sv)) { |
2655 | sv_force_normal(sv); | |
2656 | } | |
0336b60e | 2657 | if (SvREADONLY(sv) && !SvOK(sv)) { |
599cee73 | 2658 | if (ckWARN(WARN_UNINITIALIZED)) |
1d7c1841 | 2659 | report_uninit(); |
ed6116ce LW |
2660 | return 0.0; |
2661 | } | |
79072805 LW |
2662 | } |
2663 | if (SvTYPE(sv) < SVt_NV) { | |
463ee0b2 LW |
2664 | if (SvTYPE(sv) == SVt_IV) |
2665 | sv_upgrade(sv, SVt_PVNV); | |
2666 | else | |
2667 | sv_upgrade(sv, SVt_NV); | |
906f284f | 2668 | #ifdef USE_LONG_DOUBLE |
097ee67d | 2669 | DEBUG_c({ |
f93f4e46 | 2670 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
2671 | PerlIO_printf(Perl_debug_log, |
2672 | "0x%"UVxf" num(%" PERL_PRIgldbl ")\n", | |
2673 | PTR2UV(sv), SvNVX(sv)); | |
572bbb43 GS |
2674 | RESTORE_NUMERIC_LOCAL(); |
2675 | }); | |
65202027 | 2676 | #else |
572bbb43 | 2677 | DEBUG_c({ |
f93f4e46 | 2678 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1779d84d | 2679 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%"NVgf")\n", |
1d7c1841 | 2680 | PTR2UV(sv), SvNVX(sv)); |
097ee67d JH |
2681 | RESTORE_NUMERIC_LOCAL(); |
2682 | }); | |
572bbb43 | 2683 | #endif |
79072805 LW |
2684 | } |
2685 | else if (SvTYPE(sv) < SVt_PVNV) | |
2686 | sv_upgrade(sv, SVt_PVNV); | |
59d8ce62 NC |
2687 | if (SvNOKp(sv)) { |
2688 | return SvNVX(sv); | |
61604483 | 2689 | } |
59d8ce62 | 2690 | if (SvIOKp(sv)) { |
65202027 | 2691 | SvNVX(sv) = SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv); |
28e5dec8 JH |
2692 | #ifdef NV_PRESERVES_UV |
2693 | SvNOK_on(sv); | |
2694 | #else | |
2695 | /* Only set the public NV OK flag if this NV preserves the IV */ | |
2696 | /* Check it's not 0xFFFFFFFFFFFFFFFF */ | |
2697 | if (SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv)))) | |
2698 | : (SvIVX(sv) == I_V(SvNVX(sv)))) | |
2699 | SvNOK_on(sv); | |
2700 | else | |
2701 | SvNOKp_on(sv); | |
2702 | #endif | |
93a17b20 | 2703 | } |
748a9306 | 2704 | else if (SvPOKp(sv) && SvLEN(sv)) { |
c2988b20 NC |
2705 | UV value; |
2706 | int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value); | |
2707 | if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !numtype) | |
a0d0e21e | 2708 | not_a_number(sv); |
28e5dec8 | 2709 | #ifdef NV_PRESERVES_UV |
c2988b20 NC |
2710 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) |
2711 | == IS_NUMBER_IN_UV) { | |
5e045b90 | 2712 | /* It's definitely an integer */ |
c2988b20 NC |
2713 | SvNVX(sv) = (numtype & IS_NUMBER_NEG) ? -(NV)value : (NV)value; |
2714 | } else | |
2715 | SvNVX(sv) = Atof(SvPVX(sv)); | |
28e5dec8 JH |
2716 | SvNOK_on(sv); |
2717 | #else | |
c2988b20 | 2718 | SvNVX(sv) = Atof(SvPVX(sv)); |
28e5dec8 JH |
2719 | /* Only set the public NV OK flag if this NV preserves the value in |
2720 | the PV at least as well as an IV/UV would. | |
2721 | Not sure how to do this 100% reliably. */ | |
2722 | /* if that shift count is out of range then Configure's test is | |
2723 | wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS == | |
2724 | UV_BITS */ | |
2725 | if (((UV)1 << NV_PRESERVES_UV_BITS) > | |
c2988b20 | 2726 | U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) { |
28e5dec8 | 2727 | SvNOK_on(sv); /* Definitely small enough to preserve all bits */ |
c2988b20 NC |
2728 | } else if (!(numtype & IS_NUMBER_IN_UV)) { |
2729 | /* Can't use strtol etc to convert this string, so don't try. | |
2730 | sv_2iv and sv_2uv will use the NV to convert, not the PV. */ | |
2731 | SvNOK_on(sv); | |
2732 | } else { | |
2733 | /* value has been set. It may not be precise. */ | |
2734 | if ((numtype & IS_NUMBER_NEG) && (value > (UV)IV_MIN)) { | |
2735 | /* 2s complement assumption for (UV)IV_MIN */ | |
2736 | SvNOK_on(sv); /* Integer is too negative. */ | |
2737 | } else { | |
2738 | SvNOKp_on(sv); | |
2739 | SvIOKp_on(sv); | |
6fa402ec | 2740 | |
c2988b20 NC |
2741 | if (numtype & IS_NUMBER_NEG) { |
2742 | SvIVX(sv) = -(IV)value; | |
2743 | } else if (value <= (UV)IV_MAX) { | |
2744 | SvIVX(sv) = (IV)value; | |
2745 | } else { | |
2746 | SvUVX(sv) = value; | |
2747 | SvIsUV_on(sv); | |
2748 | } | |
2749 | ||
2750 | if (numtype & IS_NUMBER_NOT_INT) { | |
2751 | /* I believe that even if the original PV had decimals, | |
2752 | they are lost beyond the limit of the FP precision. | |
2753 | However, neither is canonical, so both only get p | |
2754 | flags. NWC, 2000/11/25 */ | |
2755 | /* Both already have p flags, so do nothing */ | |
2756 | } else { | |
2757 | NV nv = SvNVX(sv); | |
2758 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
2759 | if (SvIVX(sv) == I_V(nv)) { | |
2760 | SvNOK_on(sv); | |
2761 | SvIOK_on(sv); | |
2762 | } else { | |
2763 | SvIOK_on(sv); | |
2764 | /* It had no "." so it must be integer. */ | |
2765 | } | |
2766 | } else { | |
2767 | /* between IV_MAX and NV(UV_MAX). | |
2768 | Could be slightly > UV_MAX */ | |
6fa402ec | 2769 | |
c2988b20 NC |
2770 | if (numtype & IS_NUMBER_NOT_INT) { |
2771 | /* UV and NV both imprecise. */ | |
2772 | } else { | |
2773 | UV nv_as_uv = U_V(nv); | |
2774 | ||
2775 | if (value == nv_as_uv && SvUVX(sv) != UV_MAX) { | |
2776 | SvNOK_on(sv); | |
2777 | SvIOK_on(sv); | |
2778 | } else { | |
2779 | SvIOK_on(sv); | |
2780 | } | |
2781 | } | |
2782 | } | |
2783 | } | |
2784 | } | |
2785 | } | |
28e5dec8 | 2786 | #endif /* NV_PRESERVES_UV */ |
93a17b20 | 2787 | } |
79072805 | 2788 | else { |
599cee73 | 2789 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP)) |
1d7c1841 | 2790 | report_uninit(); |
25da4f38 IZ |
2791 | if (SvTYPE(sv) < SVt_NV) |
2792 | /* Typically the caller expects that sv_any is not NULL now. */ | |
28e5dec8 JH |
2793 | /* XXX Ilya implies that this is a bug in callers that assume this |
2794 | and ideally should be fixed. */ | |
25da4f38 | 2795 | sv_upgrade(sv, SVt_NV); |
a0d0e21e | 2796 | return 0.0; |
79072805 | 2797 | } |
572bbb43 | 2798 | #if defined(USE_LONG_DOUBLE) |
097ee67d | 2799 | DEBUG_c({ |
f93f4e46 | 2800 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
2801 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n", |
2802 | PTR2UV(sv), SvNVX(sv)); | |
572bbb43 GS |
2803 | RESTORE_NUMERIC_LOCAL(); |
2804 | }); | |
65202027 | 2805 | #else |
572bbb43 | 2806 | DEBUG_c({ |
f93f4e46 | 2807 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1779d84d | 2808 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%"NVgf")\n", |
1d7c1841 | 2809 | PTR2UV(sv), SvNVX(sv)); |
097ee67d JH |
2810 | RESTORE_NUMERIC_LOCAL(); |
2811 | }); | |
572bbb43 | 2812 | #endif |
463ee0b2 | 2813 | return SvNVX(sv); |
79072805 LW |
2814 | } |
2815 | ||
645c22ef DM |
2816 | /* asIV(): extract an integer from the string value of an SV. |
2817 | * Caller must validate PVX */ | |
2818 | ||
76e3520e | 2819 | STATIC IV |
cea2e8a9 | 2820 | S_asIV(pTHX_ SV *sv) |
36477c24 | 2821 | { |
c2988b20 NC |
2822 | UV value; |
2823 | int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value); | |
2824 | ||
2825 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) | |
2826 | == IS_NUMBER_IN_UV) { | |
645c22ef | 2827 | /* It's definitely an integer */ |
c2988b20 NC |
2828 | if (numtype & IS_NUMBER_NEG) { |
2829 | if (value < (UV)IV_MIN) | |
2830 | return -(IV)value; | |
2831 | } else { | |
2832 | if (value < (UV)IV_MAX) | |
2833 | return (IV)value; | |
2834 | } | |
2835 | } | |
d008e5eb | 2836 | if (!numtype) { |
d008e5eb GS |
2837 | if (ckWARN(WARN_NUMERIC)) |
2838 | not_a_number(sv); | |
2839 | } | |
c2988b20 | 2840 | return I_V(Atof(SvPVX(sv))); |
36477c24 | 2841 | } |
2842 | ||
645c22ef DM |
2843 | /* asUV(): extract an unsigned integer from the string value of an SV |
2844 | * Caller must validate PVX */ | |
2845 | ||
76e3520e | 2846 | STATIC UV |
cea2e8a9 | 2847 | S_asUV(pTHX_ SV *sv) |
36477c24 | 2848 | { |
c2988b20 NC |
2849 | UV value; |
2850 | int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value); | |
36477c24 | 2851 | |
c2988b20 NC |
2852 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) |
2853 | == IS_NUMBER_IN_UV) { | |
645c22ef | 2854 | /* It's definitely an integer */ |
6fa402ec | 2855 | if (!(numtype & IS_NUMBER_NEG)) |
c2988b20 NC |
2856 | return value; |
2857 | } | |
d008e5eb | 2858 | if (!numtype) { |
d008e5eb GS |
2859 | if (ckWARN(WARN_NUMERIC)) |
2860 | not_a_number(sv); | |
2861 | } | |
097ee67d | 2862 | return U_V(Atof(SvPVX(sv))); |
36477c24 | 2863 | } |
2864 | ||
645c22ef DM |
2865 | /* |
2866 | =for apidoc sv_2pv_nolen | |
2867 | ||
2868 | Like C<sv_2pv()>, but doesn't return the length too. You should usually | |
2869 | use the macro wrapper C<SvPV_nolen(sv)> instead. | |
2870 | =cut | |
2871 | */ | |
2872 | ||
79072805 | 2873 | char * |
864dbfa3 | 2874 | Perl_sv_2pv_nolen(pTHX_ register SV *sv) |
1fa8b10d JD |
2875 | { |
2876 | STRLEN n_a; | |
2877 | return sv_2pv(sv, &n_a); | |
2878 | } | |
2879 | ||
645c22ef DM |
2880 | /* uiv_2buf(): private routine for use by sv_2pv_flags(): print an IV or |
2881 | * UV as a string towards the end of buf, and return pointers to start and | |
2882 | * end of it. | |
2883 | * | |
2884 | * We assume that buf is at least TYPE_CHARS(UV) long. | |
2885 | */ | |
2886 | ||
864dbfa3 | 2887 | static char * |
25da4f38 IZ |
2888 | uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob) |
2889 | { | |
25da4f38 IZ |
2890 | char *ptr = buf + TYPE_CHARS(UV); |
2891 | char *ebuf = ptr; | |
2892 | int sign; | |
25da4f38 IZ |
2893 | |
2894 | if (is_uv) | |
2895 | sign = 0; | |
2896 | else if (iv >= 0) { | |
2897 | uv = iv; | |
2898 | sign = 0; | |
2899 | } else { | |
2900 | uv = -iv; | |
2901 | sign = 1; | |
2902 | } | |
2903 | do { | |
eb160463 | 2904 | *--ptr = '0' + (char)(uv % 10); |
25da4f38 IZ |
2905 | } while (uv /= 10); |
2906 | if (sign) | |
2907 | *--ptr = '-'; | |
2908 | *peob = ebuf; | |
2909 | return ptr; | |
2910 | } | |
2911 | ||
d34f9d2e JH |
2912 | /* sv_2pv() is now a macro using Perl_sv_2pv_flags(); |
2913 | * this function provided for binary compatibility only | |
2914 | */ | |
2915 | ||
2916 | char * | |
2917 | Perl_sv_2pv(pTHX_ register SV *sv, STRLEN *lp) | |
2918 | { | |
2919 | return sv_2pv_flags(sv, lp, SV_GMAGIC); | |
2920 | } | |
2921 | ||
645c22ef DM |
2922 | /* |
2923 | =for apidoc sv_2pv_flags | |
2924 | ||
ff276b08 | 2925 | Returns a pointer to the string value of an SV, and sets *lp to its length. |
645c22ef DM |
2926 | If flags includes SV_GMAGIC, does an mg_get() first. Coerces sv to a string |
2927 | if necessary. | |
2928 | Normally invoked via the C<SvPV_flags> macro. C<sv_2pv()> and C<sv_2pv_nomg> | |
2929 | usually end up here too. | |
2930 | ||
2931 | =cut | |
2932 | */ | |
2933 | ||
8d6d96c1 HS |
2934 | char * |
2935 | Perl_sv_2pv_flags(pTHX_ register SV *sv, STRLEN *lp, I32 flags) | |
2936 | { | |
79072805 LW |
2937 | register char *s; |
2938 | int olderrno; | |
5b7ea690 | 2939 | SV *tsv, *origsv; |
25da4f38 IZ |
2940 | char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */ |
2941 | char *tmpbuf = tbuf; | |
79072805 | 2942 | |
463ee0b2 LW |
2943 | if (!sv) { |
2944 | *lp = 0; | |
2945 | return ""; | |
2946 | } | |
8990e307 | 2947 | if (SvGMAGICAL(sv)) { |
8d6d96c1 HS |
2948 | if (flags & SV_GMAGIC) |
2949 | mg_get(sv); | |
463ee0b2 LW |
2950 | if (SvPOKp(sv)) { |
2951 | *lp = SvCUR(sv); | |
2952 | return SvPVX(sv); | |
2953 | } | |
cf2093f6 | 2954 | if (SvIOKp(sv)) { |
1c846c1f | 2955 | if (SvIsUV(sv)) |
57def98f | 2956 | (void)sprintf(tmpbuf,"%"UVuf, (UV)SvUVX(sv)); |
cf2093f6 | 2957 | else |
57def98f | 2958 | (void)sprintf(tmpbuf,"%"IVdf, (IV)SvIVX(sv)); |
46fc3d4c | 2959 | tsv = Nullsv; |
a0d0e21e | 2960 | goto tokensave; |
463ee0b2 LW |
2961 | } |
2962 | if (SvNOKp(sv)) { | |
2d4389e4 | 2963 | Gconvert(SvNVX(sv), NV_DIG, 0, tmpbuf); |
46fc3d4c | 2964 | tsv = Nullsv; |
a0d0e21e | 2965 | goto tokensave; |
463ee0b2 | 2966 | } |
16d20bd9 | 2967 | if (!SvROK(sv)) { |
d008e5eb | 2968 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 2969 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
1d7c1841 | 2970 | report_uninit(); |
c6ee37c5 | 2971 | } |
16d20bd9 AD |
2972 | *lp = 0; |
2973 | return ""; | |
2974 | } | |
463ee0b2 | 2975 | } |
ed6116ce LW |
2976 | if (SvTHINKFIRST(sv)) { |
2977 | if (SvROK(sv)) { | |
a0d0e21e | 2978 | SV* tmpstr; |
1554e226 | 2979 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)) && |
b4b9a328 | 2980 | (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) { |
5b7ea690 JH |
2981 | char *pv = SvPV(tmpstr, *lp); |
2982 | if (SvUTF8(tmpstr)) | |
2983 | SvUTF8_on(sv); | |
2984 | else | |
2985 | SvUTF8_off(sv); | |
2986 | return pv; | |
2987 | } | |
2988 | origsv = sv; | |
ed6116ce LW |
2989 | sv = (SV*)SvRV(sv); |
2990 | if (!sv) | |
2991 | s = "NULLREF"; | |
2992 | else { | |
f9277f47 IZ |
2993 | MAGIC *mg; |
2994 | ||
ed6116ce | 2995 | switch (SvTYPE(sv)) { |
f9277f47 IZ |
2996 | case SVt_PVMG: |
2997 | if ( ((SvFLAGS(sv) & | |
1c846c1f | 2998 | (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG)) |
5835a535 | 2999 | == (SVs_OBJECT|SVs_SMG)) |
14befaf4 | 3000 | && (mg = mg_find(sv, PERL_MAGIC_qr))) { |
2cd61cdb | 3001 | regexp *re = (regexp *)mg->mg_obj; |
1bd3ad17 | 3002 | |
2cd61cdb | 3003 | if (!mg->mg_ptr) { |
8782bef2 GB |
3004 | char *fptr = "msix"; |
3005 | char reflags[6]; | |
3006 | char ch; | |
3007 | int left = 0; | |
3008 | int right = 4; | |
ff385a1b | 3009 | char need_newline = 0; |
eb160463 | 3010 | U16 reganch = (U16)((re->reganch & PMf_COMPILETIME) >> 12); |
8782bef2 | 3011 | |
155aba94 | 3012 | while((ch = *fptr++)) { |
8782bef2 GB |
3013 | if(reganch & 1) { |
3014 | reflags[left++] = ch; | |
3015 | } | |
3016 | else { | |
3017 | reflags[right--] = ch; | |
3018 | } | |
3019 | reganch >>= 1; | |
3020 | } | |
3021 | if(left != 4) { | |
3022 | reflags[left] = '-'; | |
3023 | left = 5; | |
3024 | } | |
3025 | ||
3026 | mg->mg_len = re->prelen + 4 + left; | |
ff385a1b JF |
3027 | /* |
3028 | * If /x was used, we have to worry about a regex | |
3029 | * ending with a comment later being embedded | |
3030 | * within another regex. If so, we don't want this | |
3031 | * regex's "commentization" to leak out to the | |
3032 | * right part of the enclosing regex, we must cap | |
3033 | * it with a newline. | |
3034 | * | |
3035 | * So, if /x was used, we scan backwards from the | |
3036 | * end of the regex. If we find a '#' before we | |
3037 | * find a newline, we need to add a newline | |
3038 | * ourself. If we find a '\n' first (or if we | |
3039 | * don't find '#' or '\n'), we don't need to add | |
3040 | * anything. -jfriedl | |
3041 | */ | |
3042 | if (PMf_EXTENDED & re->reganch) | |
3043 | { | |
3044 | char *endptr = re->precomp + re->prelen; | |
3045 | while (endptr >= re->precomp) | |
3046 | { | |
3047 | char c = *(endptr--); | |
3048 | if (c == '\n') | |
3049 | break; /* don't need another */ | |
3050 | if (c == '#') { | |
3051 | /* we end while in a comment, so we | |
3052 | need a newline */ | |
3053 | mg->mg_len++; /* save space for it */ | |
3054 | need_newline = 1; /* note to add it */ | |
5b7ea690 | 3055 | break; |
ff385a1b JF |
3056 | } |
3057 | } | |
3058 | } | |
3059 | ||
8782bef2 GB |
3060 | New(616, mg->mg_ptr, mg->mg_len + 1 + left, char); |
3061 | Copy("(?", mg->mg_ptr, 2, char); | |
3062 | Copy(reflags, mg->mg_ptr+2, left, char); | |
3063 | Copy(":", mg->mg_ptr+left+2, 1, char); | |
3064 | Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char); | |
ff385a1b JF |
3065 | if (need_newline) |
3066 | mg->mg_ptr[mg->mg_len - 2] = '\n'; | |
1bd3ad17 IZ |
3067 | mg->mg_ptr[mg->mg_len - 1] = ')'; |
3068 | mg->mg_ptr[mg->mg_len] = 0; | |
3069 | } | |
3280af22 | 3070 | PL_reginterp_cnt += re->program[0].next_off; |
5b7ea690 JH |
3071 | |
3072 | if (re->reganch & ROPT_UTF8) | |
3073 | SvUTF8_on(origsv); | |
3074 | else | |
3075 | SvUTF8_off(origsv); | |
1bd3ad17 IZ |
3076 | *lp = mg->mg_len; |
3077 | return mg->mg_ptr; | |
f9277f47 IZ |
3078 | } |
3079 | /* Fall through */ | |
ed6116ce LW |
3080 | case SVt_NULL: |
3081 | case SVt_IV: | |
3082 | case SVt_NV: | |
3083 | case SVt_RV: | |
3084 | case SVt_PV: | |
3085 | case SVt_PVIV: | |
3086 | case SVt_PVNV: | |
81689caa HS |
3087 | case SVt_PVBM: if (SvROK(sv)) |
3088 | s = "REF"; | |
3089 | else | |
3090 | s = "SCALAR"; break; | |
ac388100 JH |
3091 | case SVt_PVLV: s = SvROK(sv) ? "REF" |
3092 | /* tied lvalues should appear to be | |
3093 | * scalars for backwards compatitbility */ | |
3094 | : (LvTYPE(sv) == 't' || LvTYPE(sv) == 'T') | |
3095 | ? "SCALAR" : "LVALUE"; break; | |
ed6116ce LW |
3096 | case SVt_PVAV: s = "ARRAY"; break; |
3097 | case SVt_PVHV: s = "HASH"; break; | |
3098 | case SVt_PVCV: s = "CODE"; break; | |
3099 | case SVt_PVGV: s = "GLOB"; break; | |
1d2dff63 | 3100 | case SVt_PVFM: s = "FORMAT"; break; |
36477c24 | 3101 | case SVt_PVIO: s = "IO"; break; |
ed6116ce LW |
3102 | default: s = "UNKNOWN"; break; |
3103 | } | |
46fc3d4c | 3104 | tsv = NEWSV(0,0); |
c86bf373 AMS |
3105 | if (SvOBJECT(sv)) { |
3106 | HV *svs = SvSTASH(sv); | |
3107 | Perl_sv_setpvf( | |
3108 | aTHX_ tsv, "%s=%s", | |
3109 | /* [20011101.072] This bandaid for C<package;> | |
3110 | should eventually be removed. AMS 20011103 */ | |
3111 | (svs ? HvNAME(svs) : "<none>"), s | |
3112 | ); | |
3113 | } | |
ed6116ce | 3114 | else |
46fc3d4c | 3115 | sv_setpv(tsv, s); |
57def98f | 3116 | Perl_sv_catpvf(aTHX_ tsv, "(0x%"UVxf")", PTR2UV(sv)); |
a0d0e21e | 3117 | goto tokensaveref; |
463ee0b2 | 3118 | } |
ed6116ce LW |
3119 | *lp = strlen(s); |
3120 | return s; | |
79072805 | 3121 | } |
0336b60e | 3122 | if (SvREADONLY(sv) && !SvOK(sv)) { |
0336b60e | 3123 | if (ckWARN(WARN_UNINITIALIZED)) |
1d7c1841 | 3124 | report_uninit(); |
ed6116ce LW |
3125 | *lp = 0; |
3126 | return ""; | |
79072805 | 3127 | } |
79072805 | 3128 | } |
28e5dec8 JH |
3129 | if (SvIOK(sv) || ((SvIOKp(sv) && !SvNOKp(sv)))) { |
3130 | /* I'm assuming that if both IV and NV are equally valid then | |
3131 | converting the IV is going to be more efficient */ | |
3132 | U32 isIOK = SvIOK(sv); | |
3133 | U32 isUIOK = SvIsUV(sv); | |
3134 | char buf[TYPE_CHARS(UV)]; | |
3135 | char *ebuf, *ptr; | |
3136 | ||
3137 | if (SvTYPE(sv) < SVt_PVIV) | |
3138 | sv_upgrade(sv, SVt_PVIV); | |
3139 | if (isUIOK) | |
3140 | ptr = uiv_2buf(buf, 0, SvUVX(sv), 1, &ebuf); | |
3141 | else | |
3142 | ptr = uiv_2buf(buf, SvIVX(sv), 0, 0, &ebuf); | |
eb160463 | 3143 | SvGROW(sv, (STRLEN)(ebuf - ptr + 1)); /* inlined from sv_setpvn */ |
28e5dec8 JH |
3144 | Move(ptr,SvPVX(sv),ebuf - ptr,char); |
3145 | SvCUR_set(sv, ebuf - ptr); | |
3146 | s = SvEND(sv); | |
3147 | *s = '\0'; | |
3148 | if (isIOK) | |
3149 | SvIOK_on(sv); | |
3150 | else | |
3151 | SvIOKp_on(sv); | |
3152 | if (isUIOK) | |
3153 | SvIsUV_on(sv); | |
3154 | } | |
3155 | else if (SvNOKp(sv)) { | |
79072805 LW |
3156 | if (SvTYPE(sv) < SVt_PVNV) |
3157 | sv_upgrade(sv, SVt_PVNV); | |
1c846c1f | 3158 | /* The +20 is pure guesswork. Configure test needed. --jhi */ |
59155cc0 | 3159 | SvGROW(sv, NV_DIG + 20); |
463ee0b2 | 3160 | s = SvPVX(sv); |
79072805 | 3161 | olderrno = errno; /* some Xenix systems wipe out errno here */ |
79072805 | 3162 | #ifdef apollo |
463ee0b2 | 3163 | if (SvNVX(sv) == 0.0) |
79072805 LW |
3164 | (void)strcpy(s,"0"); |
3165 | else | |
3166 | #endif /*apollo*/ | |
bbce6d69 | 3167 | { |
2d4389e4 | 3168 | Gconvert(SvNVX(sv), NV_DIG, 0, s); |
bbce6d69 | 3169 | } |
79072805 | 3170 | errno = olderrno; |
a0d0e21e LW |
3171 | #ifdef FIXNEGATIVEZERO |
3172 | if (*s == '-' && s[1] == '0' && !s[2]) | |
3173 | strcpy(s,"0"); | |
3174 | #endif | |
79072805 LW |
3175 | while (*s) s++; |
3176 | #ifdef hcx | |
3177 | if (s[-1] == '.') | |
46fc3d4c | 3178 | *--s = '\0'; |
79072805 LW |
3179 | #endif |
3180 | } | |
79072805 | 3181 | else { |
0336b60e IZ |
3182 | if (ckWARN(WARN_UNINITIALIZED) |
3183 | && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP)) | |
1d7c1841 | 3184 | report_uninit(); |
a0d0e21e | 3185 | *lp = 0; |
25da4f38 IZ |
3186 | if (SvTYPE(sv) < SVt_PV) |
3187 | /* Typically the caller expects that sv_any is not NULL now. */ | |
3188 | sv_upgrade(sv, SVt_PV); | |
a0d0e21e | 3189 | return ""; |
79072805 | 3190 | } |
463ee0b2 LW |
3191 | *lp = s - SvPVX(sv); |
3192 | SvCUR_set(sv, *lp); | |
79072805 | 3193 | SvPOK_on(sv); |
1d7c1841 GS |
3194 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n", |
3195 | PTR2UV(sv),SvPVX(sv))); | |
463ee0b2 | 3196 | return SvPVX(sv); |
a0d0e21e LW |
3197 | |
3198 | tokensave: | |
3199 | if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */ | |
3200 | /* Sneaky stuff here */ | |
3201 | ||
3202 | tokensaveref: | |
46fc3d4c | 3203 | if (!tsv) |
96827780 | 3204 | tsv = newSVpv(tmpbuf, 0); |
46fc3d4c | 3205 | sv_2mortal(tsv); |
3206 | *lp = SvCUR(tsv); | |
3207 | return SvPVX(tsv); | |
a0d0e21e LW |
3208 | } |
3209 | else { | |
3210 | STRLEN len; | |
46fc3d4c | 3211 | char *t; |
3212 | ||
3213 | if (tsv) { | |
3214 | sv_2mortal(tsv); | |
3215 | t = SvPVX(tsv); | |
3216 | len = SvCUR(tsv); | |
3217 | } | |
3218 | else { | |
96827780 MB |
3219 | t = tmpbuf; |
3220 | len = strlen(tmpbuf); | |
46fc3d4c | 3221 | } |
a0d0e21e | 3222 | #ifdef FIXNEGATIVEZERO |
46fc3d4c | 3223 | if (len == 2 && t[0] == '-' && t[1] == '0') { |
3224 | t = "0"; | |
3225 | len = 1; | |
3226 | } | |
a0d0e21e LW |
3227 | #endif |
3228 | (void)SvUPGRADE(sv, SVt_PV); | |
46fc3d4c | 3229 | *lp = len; |
a0d0e21e LW |
3230 | s = SvGROW(sv, len + 1); |
3231 | SvCUR_set(sv, len); | |
46fc3d4c | 3232 | (void)strcpy(s, t); |
6bf554b4 | 3233 | SvPOKp_on(sv); |
a0d0e21e LW |
3234 | return s; |
3235 | } | |
463ee0b2 LW |
3236 | } |
3237 | ||
645c22ef | 3238 | /* |
6050d10e JP |
3239 | =for apidoc sv_copypv |
3240 | ||
3241 | Copies a stringified representation of the source SV into the | |
3242 | destination SV. Automatically performs any necessary mg_get and | |
54f0641b | 3243 | coercion of numeric values into strings. Guaranteed to preserve |
6050d10e | 3244 | UTF-8 flag even from overloaded objects. Similar in nature to |
54f0641b NIS |
3245 | sv_2pv[_flags] but operates directly on an SV instead of just the |
3246 | string. Mostly uses sv_2pv_flags to do its work, except when that | |
6050d10e JP |
3247 | would lose the UTF-8'ness of the PV. |
3248 | ||
3249 | =cut | |
3250 | */ | |
3251 | ||
3252 | void | |
3253 | Perl_sv_copypv(pTHX_ SV *dsv, register SV *ssv) | |
3254 | { | |
5b7ea690 JH |
3255 | STRLEN len; |
3256 | char *s; | |
3257 | s = SvPV(ssv,len); | |
3258 | sv_setpvn(dsv,s,len); | |
3259 | if (SvUTF8(ssv)) | |
3260 | SvUTF8_on(dsv); | |
3261 | else | |
3262 | SvUTF8_off(dsv); | |
6050d10e JP |
3263 | } |
3264 | ||
3265 | /* | |
645c22ef DM |
3266 | =for apidoc sv_2pvbyte_nolen |
3267 | ||
3268 | Return a pointer to the byte-encoded representation of the SV. | |
cd458e05 | 3269 | May cause the SV to be downgraded from UTF-8 as a side-effect. |
645c22ef DM |
3270 | |
3271 | Usually accessed via the C<SvPVbyte_nolen> macro. | |
3272 | ||
3273 | =cut | |
3274 | */ | |
3275 | ||
7340a771 GS |
3276 | char * |
3277 | Perl_sv_2pvbyte_nolen(pTHX_ register SV *sv) | |
3278 | { | |
560a288e GS |
3279 | STRLEN n_a; |
3280 | return sv_2pvbyte(sv, &n_a); | |
7340a771 GS |
3281 | } |
3282 | ||
645c22ef DM |
3283 | /* |
3284 | =for apidoc sv_2pvbyte | |
3285 | ||
3286 | Return a pointer to the byte-encoded representation of the SV, and set *lp | |
cd458e05 | 3287 | to its length. May cause the SV to be downgraded from UTF-8 as a |
645c22ef DM |
3288 | side-effect. |
3289 | ||
3290 | Usually accessed via the C<SvPVbyte> macro. | |
3291 | ||
3292 | =cut | |
3293 | */ | |
3294 | ||
7340a771 GS |
3295 | char * |
3296 | Perl_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp) | |
3297 | { | |
0875d2fe NIS |
3298 | sv_utf8_downgrade(sv,0); |
3299 | return SvPV(sv,*lp); | |
7340a771 GS |
3300 | } |
3301 | ||
645c22ef DM |
3302 | /* |
3303 | =for apidoc sv_2pvutf8_nolen | |
3304 | ||
cd458e05 JH |
3305 | Return a pointer to the UTF-8-encoded representation of the SV. |
3306 | May cause the SV to be upgraded to UTF-8 as a side-effect. | |
645c22ef DM |
3307 | |
3308 | Usually accessed via the C<SvPVutf8_nolen> macro. | |
3309 | ||
3310 | =cut | |
3311 | */ | |
3312 | ||
7340a771 GS |
3313 | char * |
3314 | Perl_sv_2pvutf8_nolen(pTHX_ register SV *sv) | |
3315 | { | |
560a288e GS |
3316 | STRLEN n_a; |
3317 | return sv_2pvutf8(sv, &n_a); | |
7340a771 GS |
3318 | } |
3319 | ||
645c22ef DM |
3320 | /* |
3321 | =for apidoc sv_2pvutf8 | |
3322 | ||
cd458e05 JH |
3323 | Return a pointer to the UTF-8-encoded representation of the SV, and set *lp |
3324 | to its length. May cause the SV to be upgraded to UTF-8 as a side-effect. | |
645c22ef DM |
3325 | |
3326 | Usually accessed via the C<SvPVutf8> macro. | |
3327 | ||
3328 | =cut | |
3329 | */ | |
3330 | ||
7340a771 GS |
3331 | char * |
3332 | Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp) | |
3333 | { | |
560a288e | 3334 | sv_utf8_upgrade(sv); |
7d59b7e4 | 3335 | return SvPV(sv,*lp); |
7340a771 | 3336 | } |
1c846c1f | 3337 | |
645c22ef DM |
3338 | /* |
3339 | =for apidoc sv_2bool | |
3340 | ||
3341 | This function is only called on magical items, and is only used by | |
8cf8f3d1 | 3342 | sv_true() or its macro equivalent. |
645c22ef DM |
3343 | |
3344 | =cut | |
3345 | */ | |
3346 | ||
463ee0b2 | 3347 | bool |
864dbfa3 | 3348 | Perl_sv_2bool(pTHX_ register SV *sv) |
463ee0b2 | 3349 | { |
8990e307 | 3350 | if (SvGMAGICAL(sv)) |
463ee0b2 LW |
3351 | mg_get(sv); |
3352 | ||
a0d0e21e LW |
3353 | if (!SvOK(sv)) |
3354 | return 0; | |
3355 | if (SvROK(sv)) { | |
a0d0e21e | 3356 | SV* tmpsv; |
1554e226 | 3357 | if (SvAMAGIC(sv) && (tmpsv=AMG_CALLun(sv,bool_)) && |
9e3013b1 | 3358 | (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv)))) |
8a31060d | 3359 | return (bool)SvTRUE(tmpsv); |
a0d0e21e LW |
3360 | return SvRV(sv) != 0; |
3361 | } | |
463ee0b2 | 3362 | if (SvPOKp(sv)) { |
11343788 MB |
3363 | register XPV* Xpvtmp; |
3364 | if ((Xpvtmp = (XPV*)SvANY(sv)) && | |
3365 | (*Xpvtmp->xpv_pv > '0' || | |
3366 | Xpvtmp->xpv_cur > 1 || | |
3367 | (Xpvtmp->xpv_cur && *Xpvtmp->xpv_pv != '0'))) | |
463ee0b2 LW |
3368 | return 1; |
3369 | else | |
3370 | return 0; | |
3371 | } | |
3372 | else { | |
3373 | if (SvIOKp(sv)) | |
3374 | return SvIVX(sv) != 0; | |
3375 | else { | |
3376 | if (SvNOKp(sv)) | |
3377 | return SvNVX(sv) != 0.0; | |
3378 | else | |
3379 | return FALSE; | |
3380 | } | |
3381 | } | |
79072805 LW |
3382 | } |
3383 | ||
d34f9d2e JH |
3384 | /* sv_utf8_upgrade() is now a macro using sv_utf8_upgrade_flags(); |
3385 | * this function provided for binary compatibility only | |
3386 | */ | |
3387 | ||
3388 | ||
3389 | STRLEN | |
3390 | Perl_sv_utf8_upgrade(pTHX_ register SV *sv) | |
3391 | { | |
3392 | return sv_utf8_upgrade_flags(sv, SV_GMAGIC); | |
3393 | } | |
3394 | ||
c461cf8f JH |
3395 | /* |
3396 | =for apidoc sv_utf8_upgrade | |
3397 | ||
cd458e05 | 3398 | Convert the PV of an SV to its UTF-8-encoded form. |
645c22ef | 3399 | Forces the SV to string form if it is not already. |
4411f3b6 NIS |
3400 | Always sets the SvUTF8 flag to avoid future validity checks even |
3401 | if all the bytes have hibit clear. | |
c461cf8f | 3402 | |
13a6c0e0 JH |
3403 | This is not as a general purpose byte encoding to Unicode interface: |
3404 | use the Encode extension for that. | |
3405 | ||
8d6d96c1 HS |
3406 | =for apidoc sv_utf8_upgrade_flags |
3407 | ||
cd458e05 | 3408 | Convert the PV of an SV to its UTF-8-encoded form. |
645c22ef | 3409 | Forces the SV to string form if it is not already. |
8d6d96c1 HS |
3410 | Always sets the SvUTF8 flag to avoid future validity checks even |
3411 | if all the bytes have hibit clear. If C<flags> has C<SV_GMAGIC> bit set, | |
3412 | will C<mg_get> on C<sv> if appropriate, else not. C<sv_utf8_upgrade> and | |
3413 | C<sv_utf8_upgrade_nomg> are implemented in terms of this function. | |
3414 | ||
13a6c0e0 JH |
3415 | This is not as a general purpose byte encoding to Unicode interface: |
3416 | use the Encode extension for that. | |
3417 | ||
8d6d96c1 HS |
3418 | =cut |
3419 | */ | |
3420 | ||
3421 | STRLEN | |
3422 | Perl_sv_utf8_upgrade_flags(pTHX_ register SV *sv, I32 flags) | |
3423 | { | |
db42d148 | 3424 | U8 *s, *t, *e; |
511c2ff0 | 3425 | int hibit = 0; |
560a288e | 3426 | |
4411f3b6 NIS |
3427 | if (!sv) |
3428 | return 0; | |
3429 | ||
e0e62c2a NIS |
3430 | if (!SvPOK(sv)) { |
3431 | STRLEN len = 0; | |
8d6d96c1 | 3432 | (void) sv_2pv_flags(sv,&len, flags); |
e0e62c2a NIS |
3433 | if (!SvPOK(sv)) |
3434 | return len; | |
3435 | } | |
4411f3b6 NIS |
3436 | |
3437 | if (SvUTF8(sv)) | |
3438 | return SvCUR(sv); | |
560a288e | 3439 | |
db42d148 NIS |
3440 | if (SvREADONLY(sv) && SvFAKE(sv)) { |
3441 | sv_force_normal(sv); | |
3442 | } | |
3443 | ||
6563884d | 3444 | if (PL_encoding && !(flags & SV_UTF8_NO_ENCODING)) |
799ef3cb | 3445 | sv_recode_to_utf8(sv, PL_encoding); |
9f4817db | 3446 | else { /* Assume Latin-1/EBCDIC */ |
0a378802 JH |
3447 | /* This function could be much more efficient if we |
3448 | * had a FLAG in SVs to signal if there are any hibit | |
3449 | * chars in the PV. Given that there isn't such a flag | |
3450 | * make the loop as fast as possible. */ | |
3451 | s = (U8 *) SvPVX(sv); | |
3452 | e = (U8 *) SvEND(sv); | |
3453 | t = s; | |
3454 | while (t < e) { | |
3455 | U8 ch = *t++; | |
3456 | if ((hibit = !NATIVE_IS_INVARIANT(ch))) | |
3457 | break; | |
3458 | } | |
3459 | if (hibit) { | |
3460 | STRLEN len; | |
ecdeb87c | 3461 | |
0a378802 JH |
3462 | len = SvCUR(sv) + 1; /* Plus the \0 */ |
3463 | SvPVX(sv) = (char*)bytes_to_utf8((U8*)s, &len); | |
3464 | SvCUR(sv) = len - 1; | |
3465 | if (SvLEN(sv) != 0) | |
3466 | Safefree(s); /* No longer using what was there before. */ | |
3467 | SvLEN(sv) = len; /* No longer know the real size. */ | |
3468 | } | |
9f4817db JH |
3469 | /* Mark as UTF-8 even if no hibit - saves scanning loop */ |
3470 | SvUTF8_on(sv); | |
560a288e | 3471 | } |
4411f3b6 | 3472 | return SvCUR(sv); |
560a288e GS |
3473 | } |
3474 | ||
c461cf8f JH |
3475 | /* |
3476 | =for apidoc sv_utf8_downgrade | |
3477 | ||
cd458e05 | 3478 | Attempt to convert the PV of an SV from UTF-8-encoded to byte encoding. |
c461cf8f JH |
3479 | This may not be possible if the PV contains non-byte encoding characters; |
3480 | if this is the case, either returns false or, if C<fail_ok> is not | |
3481 | true, croaks. | |
3482 | ||
13a6c0e0 JH |
3483 | This is not as a general purpose Unicode to byte encoding interface: |
3484 | use the Encode extension for that. | |
3485 | ||
c461cf8f JH |
3486 | =cut |
3487 | */ | |
3488 | ||
560a288e GS |
3489 | bool |
3490 | Perl_sv_utf8_downgrade(pTHX_ register SV* sv, bool fail_ok) | |
3491 | { | |
3492 | if (SvPOK(sv) && SvUTF8(sv)) { | |
fa301091 | 3493 | if (SvCUR(sv)) { |
03cfe0ae | 3494 | U8 *s; |
652088fc | 3495 | STRLEN len; |
fa301091 | 3496 | |
652088fc JH |
3497 | if (SvREADONLY(sv) && SvFAKE(sv)) |
3498 | sv_force_normal(sv); | |
03cfe0ae NIS |
3499 | s = (U8 *) SvPV(sv, len); |
3500 | if (!utf8_to_bytes(s, &len)) { | |
fa301091 JH |
3501 | if (fail_ok) |
3502 | return FALSE; | |
3503 | else { | |
3504 | if (PL_op) | |
3505 | Perl_croak(aTHX_ "Wide character in %s", | |
53e06cf0 | 3506 | OP_DESC(PL_op)); |
fa301091 JH |
3507 | else |
3508 | Perl_croak(aTHX_ "Wide character"); | |
3509 | } | |
4b3603a4 | 3510 | } |
fa301091 | 3511 | SvCUR(sv) = len; |
67e989fb | 3512 | } |
560a288e | 3513 | } |
ffebcc3e | 3514 | SvUTF8_off(sv); |
560a288e GS |
3515 | return TRUE; |
3516 | } | |
3517 | ||
c461cf8f JH |
3518 | /* |
3519 | =for apidoc sv_utf8_encode | |
3520 | ||
cd458e05 | 3521 | Convert the PV of an SV to UTF-8-encoded, but then turn off the C<SvUTF8> |
4411f3b6 NIS |
3522 | flag so that it looks like octets again. Used as a building block |
3523 | for encode_utf8 in Encode.xs | |
c461cf8f JH |
3524 | |
3525 | =cut | |
3526 | */ | |
3527 | ||
560a288e GS |
3528 | void |
3529 | Perl_sv_utf8_encode(pTHX_ register SV *sv) | |
3530 | { | |
4411f3b6 | 3531 | (void) sv_utf8_upgrade(sv); |
560a288e GS |
3532 | SvUTF8_off(sv); |
3533 | } | |
3534 | ||
4411f3b6 NIS |
3535 | /* |
3536 | =for apidoc sv_utf8_decode | |
3537 | ||
3538 | Convert the octets in the PV from UTF-8 to chars. Scan for validity and then | |
645c22ef | 3539 | turn off SvUTF8 if needed so that we see characters. Used as a building block |
4411f3b6 NIS |
3540 | for decode_utf8 in Encode.xs |
3541 | ||
3542 | =cut | |
3543 | */ | |
3544 | ||
560a288e GS |
3545 | bool |
3546 | Perl_sv_utf8_decode(pTHX_ register SV *sv) | |
3547 | { | |
3548 | if (SvPOK(sv)) { | |
63cd0674 NIS |
3549 | U8 *c; |
3550 | U8 *e; | |
9cbac4c7 | 3551 | |
645c22ef DM |
3552 | /* The octets may have got themselves encoded - get them back as |
3553 | * bytes | |
3554 | */ | |
3555 | if (!sv_utf8_downgrade(sv, TRUE)) | |
560a288e GS |
3556 | return FALSE; |
3557 | ||
3558 | /* it is actually just a matter of turning the utf8 flag on, but | |
3559 | * we want to make sure everything inside is valid utf8 first. | |
3560 | */ | |
63cd0674 NIS |
3561 | c = (U8 *) SvPVX(sv); |
3562 | if (!is_utf8_string(c, SvCUR(sv)+1)) | |
67e989fb | 3563 | return FALSE; |
63cd0674 | 3564 | e = (U8 *) SvEND(sv); |
511c2ff0 | 3565 | while (c < e) { |
c4d5f83a NIS |
3566 | U8 ch = *c++; |
3567 | if (!UTF8_IS_INVARIANT(ch)) { | |
67e989fb JH |
3568 | SvUTF8_on(sv); |
3569 | break; | |
3570 | } | |
560a288e | 3571 | } |
560a288e GS |
3572 | } |
3573 | return TRUE; | |
3574 | } | |
3575 | ||
d34f9d2e JH |
3576 | /* sv_setsv() is now a macro using Perl_sv_setsv_flags(); |
3577 | * this function provided for binary compatibility only | |
3578 | */ | |
3579 | ||
3580 | void | |
3581 | Perl_sv_setsv(pTHX_ SV *dstr, register SV *sstr) | |
3582 | { | |
3583 | sv_setsv_flags(dstr, sstr, SV_GMAGIC); | |
3584 | } | |
3585 | ||
954c1994 GS |
3586 | /* |
3587 | =for apidoc sv_setsv | |
3588 | ||
645c22ef DM |
3589 | Copies the contents of the source SV C<ssv> into the destination SV |
3590 | C<dsv>. The source SV may be destroyed if it is mortal, so don't use this | |
3591 | function if the source SV needs to be reused. Does not handle 'set' magic. | |
3592 | Loosely speaking, it performs a copy-by-value, obliterating any previous | |
3593 | content of the destination. | |
3594 | ||
3595 | You probably want to use one of the assortment of wrappers, such as | |
3596 | C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and | |
3597 | C<SvSetMagicSV_nosteal>. | |
3598 | ||
8d6d96c1 HS |
3599 | =for apidoc sv_setsv_flags |
3600 | ||
645c22ef DM |
3601 | Copies the contents of the source SV C<ssv> into the destination SV |
3602 | C<dsv>. The source SV may be destroyed if it is mortal, so don't use this | |
3603 | function if the source SV needs to be reused. Does not handle 'set' magic. | |
3604 | Loosely speaking, it performs a copy-by-value, obliterating any previous | |
3605 | content of the destination. | |
3606 | If the C<flags> parameter has the C<SV_GMAGIC> bit set, will C<mg_get> on | |
3607 | C<ssv> if appropriate, else not. C<sv_setsv> and C<sv_setsv_nomg> are | |
3608 | implemented in terms of this function. | |
3609 | ||
3610 | You probably want to use one of the assortment of wrappers, such as | |
3611 | C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and | |
3612 | C<SvSetMagicSV_nosteal>. | |
3613 | ||
3614 | This is the primary function for copying scalars, and most other | |
3615 | copy-ish functions and macros use this underneath. | |
8d6d96c1 HS |
3616 | |
3617 | =cut | |
3618 | */ | |
3619 | ||
3620 | void | |
3621 | Perl_sv_setsv_flags(pTHX_ SV *dstr, register SV *sstr, I32 flags) | |
3622 | { | |
8990e307 LW |
3623 | register U32 sflags; |
3624 | register int dtype; | |
3625 | register int stype; | |
463ee0b2 | 3626 | |
79072805 LW |
3627 | if (sstr == dstr) |
3628 | return; | |
2213622d | 3629 | SV_CHECK_THINKFIRST(dstr); |
79072805 | 3630 | if (!sstr) |
3280af22 | 3631 | sstr = &PL_sv_undef; |
8990e307 LW |
3632 | stype = SvTYPE(sstr); |
3633 | dtype = SvTYPE(dstr); | |
79072805 | 3634 | |
a0d0e21e | 3635 | SvAMAGIC_off(dstr); |
7d7ce6cc JH |
3636 | if ( SvVOK(dstr) ) |
3637 | { | |
3638 | /* need to nuke the magic */ | |
3639 | mg_free(dstr); | |
3640 | SvRMAGICAL_off(dstr); | |
3641 | } | |
9e7bc3e8 | 3642 | |
463ee0b2 | 3643 | /* There's a lot of redundancy below but we're going for speed here */ |
79072805 | 3644 | |
8990e307 | 3645 | switch (stype) { |
79072805 | 3646 | case SVt_NULL: |
aece5585 | 3647 | undef_sstr: |
20408e3c GS |
3648 | if (dtype != SVt_PVGV) { |
3649 | (void)SvOK_off(dstr); | |
3650 | return; | |
3651 | } | |
3652 | break; | |
463ee0b2 | 3653 | case SVt_IV: |
aece5585 GA |
3654 | if (SvIOK(sstr)) { |
3655 | switch (dtype) { | |
3656 | case SVt_NULL: | |
8990e307 | 3657 | sv_upgrade(dstr, SVt_IV); |
aece5585 GA |
3658 | break; |
3659 | case SVt_NV: | |
8990e307 | 3660 | sv_upgrade(dstr, SVt_PVNV); |
aece5585 GA |
3661 | break; |
3662 | case SVt_RV: | |
3663 | case SVt_PV: | |
a0d0e21e | 3664 | sv_upgrade(dstr, SVt_PVIV); |
aece5585 GA |
3665 | break; |
3666 | } | |
3667 | (void)SvIOK_only(dstr); | |
3668 | SvIVX(dstr) = SvIVX(sstr); | |
25da4f38 IZ |
3669 | if (SvIsUV(sstr)) |
3670 | SvIsUV_on(dstr); | |
27c9684d AP |
3671 | if (SvTAINTED(sstr)) |
3672 | SvTAINT(dstr); | |
aece5585 | 3673 | return; |
8990e307 | 3674 | } |
aece5585 GA |
3675 | goto undef_sstr; |
3676 | ||
463ee0b2 | 3677 | case SVt_NV: |
aece5585 GA |
3678 | if (SvNOK(sstr)) { |
3679 | switch (dtype) { | |
3680 | case SVt_NULL: | |
3681 | case SVt_IV: | |
8990e307 | 3682 | sv_upgrade(dstr, SVt_NV); |
aece5585 GA |
3683 | break; |
3684 | case SVt_RV: | |
3685 | case SVt_PV: | |
3686 | case SVt_PVIV: | |
a0d0e21e | 3687 | sv_upgrade(dstr, SVt_PVNV); |
aece5585 GA |
3688 | break; |
3689 | } | |
3690 | SvNVX(dstr) = SvNVX(sstr); | |
3691 | (void)SvNOK_only(dstr); | |
27c9684d AP |
3692 | if (SvTAINTED(sstr)) |
3693 | SvTAINT(dstr); | |
aece5585 | 3694 | return; |
8990e307 | 3695 | } |
aece5585 GA |
3696 | goto undef_sstr; |
3697 | ||
ed6116ce | 3698 | case SVt_RV: |
8990e307 | 3699 | if (dtype < SVt_RV) |
ed6116ce | 3700 | sv_upgrade(dstr, SVt_RV); |
c07a80fd | 3701 | else if (dtype == SVt_PVGV && |
5e8f8cda | 3702 | SvROK(sstr) && SvTYPE(SvRV(sstr)) == SVt_PVGV) { |
c07a80fd | 3703 | sstr = SvRV(sstr); |
a5f75d66 | 3704 | if (sstr == dstr) { |
1d7c1841 GS |
3705 | if (GvIMPORTED(dstr) != GVf_IMPORTED |
3706 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) | |
3707 | { | |
a5f75d66 | 3708 | GvIMPORTED_on(dstr); |
1d7c1841 | 3709 | } |
a5f75d66 AD |
3710 | GvMULTI_on(dstr); |
3711 | return; | |
3712 | } | |
c07a80fd | 3713 | goto glob_assign; |
3714 | } | |
ed6116ce | 3715 | break; |
463ee0b2 | 3716 | case SVt_PV: |
fc36a67e | 3717 | case SVt_PVFM: |
8990e307 | 3718 | if (dtype < SVt_PV) |
463ee0b2 | 3719 | sv_upgrade(dstr, SVt_PV); |
463ee0b2 LW |
3720 | break; |
3721 | case SVt_PVIV: | |
8990e307 | 3722 | if (dtype < SVt_PVIV) |
463ee0b2 | 3723 | sv_upgrade(dstr, SVt_PVIV); |
463ee0b2 LW |
3724 | break; |
3725 | case SVt_PVNV: | |
8990e307 | 3726 | if (dtype < SVt_PVNV) |
463ee0b2 | 3727 | sv_upgrade(dstr, SVt_PVNV); |
463ee0b2 | 3728 | break; |
4633a7c4 LW |
3729 | case SVt_PVAV: |
3730 | case SVt_PVHV: | |
3731 | case SVt_PVCV: | |
4633a7c4 | 3732 | case SVt_PVIO: |
533c011a | 3733 | if (PL_op) |
cea2e8a9 | 3734 | Perl_croak(aTHX_ "Bizarre copy of %s in %s", sv_reftype(sstr, 0), |
53e06cf0 | 3735 | OP_NAME(PL_op)); |
4633a7c4 | 3736 | else |
cea2e8a9 | 3737 | Perl_croak(aTHX_ "Bizarre copy of %s", sv_reftype(sstr, 0)); |
4633a7c4 LW |
3738 | break; |
3739 | ||
79072805 | 3740 | case SVt_PVGV: |
8990e307 | 3741 | if (dtype <= SVt_PVGV) { |
c07a80fd | 3742 | glob_assign: |
a5f75d66 | 3743 | if (dtype != SVt_PVGV) { |
a0d0e21e LW |
3744 | char *name = GvNAME(sstr); |
3745 | STRLEN len = GvNAMELEN(sstr); | |
463ee0b2 | 3746 | sv_upgrade(dstr, SVt_PVGV); |
14befaf4 | 3747 | sv_magic(dstr, dstr, PERL_MAGIC_glob, Nullch, 0); |
85aff577 | 3748 | GvSTASH(dstr) = (HV*)SvREFCNT_inc(GvSTASH(sstr)); |
a0d0e21e LW |
3749 | GvNAME(dstr) = savepvn(name, len); |
3750 | GvNAMELEN(dstr) = len; | |
3751 | SvFAKE_on(dstr); /* can coerce to non-glob */ | |
3752 | } | |
7bac28a0 | 3753 | /* ahem, death to those who redefine active sort subs */ |
3280af22 NIS |
3754 | else if (PL_curstackinfo->si_type == PERLSI_SORT |
3755 | && GvCV(dstr) && PL_sortcop == CvSTART(GvCV(dstr))) | |
cea2e8a9 | 3756 | Perl_croak(aTHX_ "Can't redefine active sort subroutine %s", |
7bac28a0 | 3757 | GvNAME(dstr)); |
5bd07a3d | 3758 | |
7fb37951 AMS |
3759 | #ifdef GV_UNIQUE_CHECK |
3760 | if (GvUNIQUE((GV*)dstr)) { | |
5bd07a3d DM |
3761 | Perl_croak(aTHX_ PL_no_modify); |
3762 | } | |
3763 | #endif | |
3764 | ||
a0d0e21e | 3765 | (void)SvOK_off(dstr); |
a5f75d66 | 3766 | GvINTRO_off(dstr); /* one-shot flag */ |
1edc1566 | 3767 | gp_free((GV*)dstr); |
79072805 | 3768 | GvGP(dstr) = gp_ref(GvGP(sstr)); |
27c9684d AP |
3769 | if (SvTAINTED(sstr)) |
3770 | SvTAINT(dstr); | |
1d7c1841 GS |
3771 | if (GvIMPORTED(dstr) != GVf_IMPORTED |
3772 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) | |
3773 | { | |
a5f75d66 | 3774 | GvIMPORTED_on(dstr); |
1d7c1841 | 3775 | } |
a5f75d66 | 3776 | GvMULTI_on(dstr); |
79072805 LW |
3777 | return; |
3778 | } | |
3779 | /* FALL THROUGH */ | |
3780 | ||
3781 | default: | |
8d6d96c1 | 3782 | if (SvGMAGICAL(sstr) && (flags & SV_GMAGIC)) { |
973f89ab | 3783 | mg_get(sstr); |
eb160463 | 3784 | if ((int)SvTYPE(sstr) != stype) { |
973f89ab CS |
3785 | stype = SvTYPE(sstr); |
3786 | if (stype == SVt_PVGV && dtype <= SVt_PVGV) | |
3787 | goto glob_assign; | |
3788 | } | |
3789 | } | |
ded42b9f | 3790 | if (stype == SVt_PVLV) |
6fc92669 | 3791 | (void)SvUPGRADE(dstr, SVt_PVNV); |
ded42b9f | 3792 | else |
eb160463 | 3793 | (void)SvUPGRADE(dstr, (U32)stype); |
79072805 LW |
3794 | } |
3795 | ||
8990e307 LW |
3796 | sflags = SvFLAGS(sstr); |
3797 | ||
3798 | if (sflags & SVf_ROK) { | |
3799 | if (dtype >= SVt_PV) { | |
3800 | if (dtype == SVt_PVGV) { | |
3801 | SV *sref = SvREFCNT_inc(SvRV(sstr)); | |
3802 | SV *dref = 0; | |
a5f75d66 | 3803 | int intro = GvINTRO(dstr); |
a0d0e21e | 3804 | |
7fb37951 AMS |
3805 | #ifdef GV_UNIQUE_CHECK |
3806 | if (GvUNIQUE((GV*)dstr)) { | |
5bd07a3d DM |
3807 | Perl_croak(aTHX_ PL_no_modify); |
3808 | } | |
3809 | #endif | |
3810 | ||
a0d0e21e | 3811 | if (intro) { |
a5f75d66 | 3812 | GvINTRO_off(dstr); /* one-shot flag */ |
1d7c1841 | 3813 | GvLINE(dstr) = CopLINE(PL_curcop); |
1edc1566 | 3814 | GvEGV(dstr) = (GV*)dstr; |
a0d0e21e | 3815 | } |
a5f75d66 | 3816 | GvMULTI_on(dstr); |
8990e307 LW |
3817 | switch (SvTYPE(sref)) { |
3818 | case SVt_PVAV: | |
a0d0e21e | 3819 | if (intro) |
9b1c5f7e | 3820 | SAVEGENERICSV(GvAV(dstr)); |
a0d0e21e LW |
3821 | else |
3822 | dref = (SV*)GvAV(dstr); | |
8990e307 | 3823 | GvAV(dstr) = (AV*)sref; |
39bac7f7 | 3824 | if (!GvIMPORTED_AV(dstr) |
1d7c1841 GS |
3825 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
3826 | { | |
a5f75d66 | 3827 | GvIMPORTED_AV_on(dstr); |
1d7c1841 | 3828 | } |
8990e307 LW |
3829 | break; |
3830 | case SVt_PVHV: | |
a0d0e21e | 3831 | if (intro) |
9b1c5f7e | 3832 | SAVEGENERICSV(GvHV(dstr)); |
a0d0e21e LW |
3833 | else |
3834 | dref = (SV*)GvHV(dstr); | |
8990e307 | 3835 | GvHV(dstr) = (HV*)sref; |
39bac7f7 | 3836 | if (!GvIMPORTED_HV(dstr) |
1d7c1841 GS |
3837 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
3838 | { | |
a5f75d66 | 3839 | GvIMPORTED_HV_on(dstr); |
1d7c1841 | 3840 | } |
8990e307 LW |
3841 | break; |
3842 | case SVt_PVCV: | |
8ebc5c01 | 3843 | if (intro) { |
3844 | if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) { | |
3845 | SvREFCNT_dec(GvCV(dstr)); | |
3846 | GvCV(dstr) = Nullcv; | |
68dc0745 | 3847 | GvCVGEN(dstr) = 0; /* Switch off cacheness. */ |
3280af22 | 3848 | PL_sub_generation++; |
8ebc5c01 | 3849 | } |
9b1c5f7e | 3850 | SAVEGENERICSV(GvCV(dstr)); |
8ebc5c01 | 3851 | } |
68dc0745 | 3852 | else |
3853 | dref = (SV*)GvCV(dstr); | |
3854 | if (GvCV(dstr) != (CV*)sref) { | |
748a9306 | 3855 | CV* cv = GvCV(dstr); |
4633a7c4 | 3856 | if (cv) { |
68dc0745 | 3857 | if (!GvCVGEN((GV*)dstr) && |
3858 | (CvROOT(cv) || CvXSUB(cv))) | |
3859 | { | |
7bac28a0 | 3860 | /* ahem, death to those who redefine |
3861 | * active sort subs */ | |
3280af22 NIS |
3862 | if (PL_curstackinfo->si_type == PERLSI_SORT && |
3863 | PL_sortcop == CvSTART(cv)) | |
1c846c1f | 3864 | Perl_croak(aTHX_ |
7bac28a0 | 3865 | "Can't redefine active sort subroutine %s", |
3866 | GvENAME((GV*)dstr)); | |
beab0874 JT |
3867 | /* Redefining a sub - warning is mandatory if |
3868 | it was a const and its value changed. */ | |
3869 | if (ckWARN(WARN_REDEFINE) | |
3870 | || (CvCONST(cv) | |
3871 | && (!CvCONST((CV*)sref) | |
3872 | || sv_cmp(cv_const_sv(cv), | |
3873 | cv_const_sv((CV*)sref))))) | |
3874 | { | |
9014280d | 3875 | Perl_warner(aTHX_ packWARN(WARN_REDEFINE), |
beab0874 | 3876 | CvCONST(cv) |
910764e6 RGS |
3877 | ? "Constant subroutine %s::%s redefined" |
3878 | : "Subroutine %s::%s redefined", | |
3879 | HvNAME(GvSTASH((GV*)dstr)), | |
beab0874 JT |
3880 | GvENAME((GV*)dstr)); |
3881 | } | |
9607fc9c | 3882 | } |
fb24441d RGS |
3883 | if (!intro) |
3884 | cv_ckproto(cv, (GV*)dstr, | |
3885 | SvPOK(sref) ? SvPVX(sref) : Nullch); | |
4633a7c4 | 3886 | } |
a5f75d66 | 3887 | GvCV(dstr) = (CV*)sref; |
7a4c00b4 | 3888 | GvCVGEN(dstr) = 0; /* Switch off cacheness. */ |
a5f75d66 | 3889 | GvASSUMECV_on(dstr); |
3280af22 | 3890 | PL_sub_generation++; |
a5f75d66 | 3891 | } |
39bac7f7 | 3892 | if (!GvIMPORTED_CV(dstr) |
1d7c1841 GS |
3893 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
3894 | { | |
a5f75d66 | 3895 | GvIMPORTED_CV_on(dstr); |
1d7c1841 | 3896 | } |
8990e307 | 3897 | break; |
91bba347 LW |
3898 | case SVt_PVIO: |
3899 | if (intro) | |
9b1c5f7e | 3900 | SAVEGENERICSV(GvIOp(dstr)); |
91bba347 LW |
3901 | else |
3902 | dref = (SV*)GvIOp(dstr); | |
3903 | GvIOp(dstr) = (IO*)sref; | |
3904 | break; | |
f4d13ee9 JH |
3905 | case SVt_PVFM: |
3906 | if (intro) | |
9b1c5f7e | 3907 | SAVEGENERICSV(GvFORM(dstr)); |
f4d13ee9 JH |
3908 | else |
3909 | dref = (SV*)GvFORM(dstr); | |
3910 | GvFORM(dstr) = (CV*)sref; | |
3911 | break; | |
8990e307 | 3912 | default: |
a0d0e21e | 3913 | if (intro) |
9b1c5f7e | 3914 | SAVEGENERICSV(GvSV(dstr)); |
a0d0e21e LW |
3915 | else |
3916 | dref = (SV*)GvSV(dstr); | |
8990e307 | 3917 | GvSV(dstr) = sref; |
39bac7f7 | 3918 | if (!GvIMPORTED_SV(dstr) |
1d7c1841 GS |
3919 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
3920 | { | |
a5f75d66 | 3921 | GvIMPORTED_SV_on(dstr); |
1d7c1841 | 3922 | } |
8990e307 LW |
3923 | break; |
3924 | } | |
3925 | if (dref) | |
3926 | SvREFCNT_dec(dref); | |
27c9684d AP |
3927 | if (SvTAINTED(sstr)) |
3928 | SvTAINT(dstr); | |
8990e307 LW |
3929 | return; |
3930 | } | |
a0d0e21e | 3931 | if (SvPVX(dstr)) { |
760ac839 | 3932 | (void)SvOOK_off(dstr); /* backoff */ |
50483b2c JD |
3933 | if (SvLEN(dstr)) |
3934 | Safefree(SvPVX(dstr)); | |
a0d0e21e LW |
3935 | SvLEN(dstr)=SvCUR(dstr)=0; |
3936 | } | |
8990e307 | 3937 | } |
a0d0e21e | 3938 | (void)SvOK_off(dstr); |
8990e307 | 3939 | SvRV(dstr) = SvREFCNT_inc(SvRV(sstr)); |
ed6116ce | 3940 | SvROK_on(dstr); |
8990e307 | 3941 | if (sflags & SVp_NOK) { |
3332b3c1 JH |
3942 | SvNOKp_on(dstr); |
3943 | /* Only set the public OK flag if the source has public OK. */ | |
3944 | if (sflags & SVf_NOK) | |
3945 | SvFLAGS(dstr) |= SVf_NOK; | |
ed6116ce LW |
3946 | SvNVX(dstr) = SvNVX(sstr); |
3947 | } | |
8990e307 | 3948 | if (sflags & SVp_IOK) { |
3332b3c1 JH |
3949 | (void)SvIOKp_on(dstr); |
3950 | if (sflags & SVf_IOK) | |
3951 | SvFLAGS(dstr) |= SVf_IOK; | |
2b1c7e3e | 3952 | if (sflags & SVf_IVisUV) |
25da4f38 | 3953 | SvIsUV_on(dstr); |
3332b3c1 | 3954 | SvIVX(dstr) = SvIVX(sstr); |
ed6116ce | 3955 | } |
a0d0e21e LW |
3956 | if (SvAMAGIC(sstr)) { |
3957 | SvAMAGIC_on(dstr); | |
3958 | } | |
ed6116ce | 3959 | } |
8990e307 | 3960 | else if (sflags & SVp_POK) { |
79072805 LW |
3961 | |
3962 | /* | |
3963 | * Check to see if we can just swipe the string. If so, it's a | |
3964 | * possible small lose on short strings, but a big win on long ones. | |
463ee0b2 LW |
3965 | * It might even be a win on short strings if SvPVX(dstr) |
3966 | * has to be allocated and SvPVX(sstr) has to be freed. | |
79072805 LW |
3967 | */ |
3968 | ||
ff68c719 | 3969 | if (SvTEMP(sstr) && /* slated for free anyway? */ |
01b73108 | 3970 | SvREFCNT(sstr) == 1 && /* and no other references to it? */ |
1c846c1f | 3971 | !(sflags & SVf_OOK) && /* and not involved in OOK hack? */ |
4c8f17b9 | 3972 | SvLEN(sstr) && /* and really is a string */ |
645c22ef DM |
3973 | /* and won't be needed again, potentially */ |
3974 | !(PL_op && PL_op->op_type == OP_AASSIGN)) | |
a5f75d66 | 3975 | { |
adbc6bb1 | 3976 | if (SvPVX(dstr)) { /* we know that dtype >= SVt_PV */ |
a5f75d66 AD |
3977 | if (SvOOK(dstr)) { |
3978 | SvFLAGS(dstr) &= ~SVf_OOK; | |
3979 | Safefree(SvPVX(dstr) - SvIVX(dstr)); | |
3980 | } | |
50483b2c | 3981 | else if (SvLEN(dstr)) |
a5f75d66 | 3982 | Safefree(SvPVX(dstr)); |
79072805 | 3983 | } |
a5f75d66 | 3984 | (void)SvPOK_only(dstr); |
463ee0b2 | 3985 | SvPV_set(dstr, SvPVX(sstr)); |
79072805 LW |
3986 | SvLEN_set(dstr, SvLEN(sstr)); |
3987 | SvCUR_set(dstr, SvCUR(sstr)); | |
f4e86e0f | 3988 | |
79072805 | 3989 | SvTEMP_off(dstr); |
645c22ef | 3990 | (void)SvOK_off(sstr); /* NOTE: nukes most SvFLAGS on sstr */ |
79072805 LW |
3991 | SvPV_set(sstr, Nullch); |
3992 | SvLEN_set(sstr, 0); | |
a5f75d66 AD |
3993 | SvCUR_set(sstr, 0); |
3994 | SvTEMP_off(sstr); | |
79072805 | 3995 | } |
645c22ef | 3996 | else { /* have to copy actual string */ |
8990e307 | 3997 | STRLEN len = SvCUR(sstr); |
645c22ef | 3998 | SvGROW(dstr, len + 1); /* inlined from sv_setpvn */ |
8990e307 LW |
3999 | Move(SvPVX(sstr),SvPVX(dstr),len,char); |
4000 | SvCUR_set(dstr, len); | |
4001 | *SvEND(dstr) = '\0'; | |
a0d0e21e | 4002 | (void)SvPOK_only(dstr); |
79072805 | 4003 | } |
9aa983d2 | 4004 | if (sflags & SVf_UTF8) |
a7cb1f99 | 4005 | SvUTF8_on(dstr); |
79072805 | 4006 | /*SUPPRESS 560*/ |
8990e307 | 4007 | if (sflags & SVp_NOK) { |
3332b3c1 JH |
4008 | SvNOKp_on(dstr); |
4009 | if (sflags & SVf_NOK) | |
4010 | SvFLAGS(dstr) |= SVf_NOK; | |
463ee0b2 | 4011 | SvNVX(dstr) = SvNVX(sstr); |
79072805 | 4012 | } |
8990e307 | 4013 | if (sflags & SVp_IOK) { |
3332b3c1 JH |
4014 | (void)SvIOKp_on(dstr); |
4015 | if (sflags & SVf_IOK) | |
4016 | SvFLAGS(dstr) |= SVf_IOK; | |
2b1c7e3e | 4017 | if (sflags & SVf_IVisUV) |
25da4f38 | 4018 | SvIsUV_on(dstr); |
463ee0b2 | 4019 | SvIVX(dstr) = SvIVX(sstr); |
79072805 | 4020 | } |
7d7ce6cc JH |
4021 | if ( SvVOK(sstr) ) { |
4022 | MAGIC *smg = mg_find(sstr,PERL_MAGIC_vstring); | |
4023 | sv_magic(dstr, NULL, PERL_MAGIC_vstring, | |
4024 | smg->mg_ptr, smg->mg_len); | |
4025 | SvRMAGICAL_on(dstr); | |
4026 | } | |
79072805 | 4027 | } |
8990e307 | 4028 | else if (sflags & SVp_IOK) { |
3332b3c1 JH |
4029 | if (sflags & SVf_IOK) |
4030 | (void)SvIOK_only(dstr); | |
4031 | else { | |
9cbac4c7 DM |
4032 | (void)SvOK_off(dstr); |
4033 | (void)SvIOKp_on(dstr); | |
3332b3c1 JH |
4034 | } |
4035 | /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */ | |
2b1c7e3e | 4036 | if (sflags & SVf_IVisUV) |
25da4f38 | 4037 | SvIsUV_on(dstr); |
3332b3c1 JH |
4038 | SvIVX(dstr) = SvIVX(sstr); |
4039 | if (sflags & SVp_NOK) { | |
4040 | if (sflags & SVf_NOK) | |
4041 | (void)SvNOK_on(dstr); | |
4042 | else | |
4043 | (void)SvNOKp_on(dstr); | |
4044 | SvNVX(dstr) = SvNVX(sstr); | |
4045 | } | |
4046 | } | |
4047 | else if (sflags & SVp_NOK) { | |
4048 | if (sflags & SVf_NOK) | |
4049 | (void)SvNOK_only(dstr); | |
4050 | else { | |
9cbac4c7 | 4051 | (void)SvOK_off(dstr); |
3332b3c1 JH |
4052 | SvNOKp_on(dstr); |
4053 | } | |
4054 | SvNVX(dstr) = SvNVX(sstr); | |
79072805 LW |
4055 | } |
4056 | else { | |
20408e3c | 4057 | if (dtype == SVt_PVGV) { |
e476b1b5 | 4058 | if (ckWARN(WARN_MISC)) |
9014280d | 4059 | Perl_warner(aTHX_ packWARN(WARN_MISC), "Undefined value assigned to typeglob"); |
20408e3c GS |
4060 | } |
4061 | else | |
4062 | (void)SvOK_off(dstr); | |
a0d0e21e | 4063 | } |
27c9684d AP |
4064 | if (SvTAINTED(sstr)) |
4065 | SvTAINT(dstr); | |
79072805 LW |
4066 | } |
4067 | ||
954c1994 GS |
4068 | /* |
4069 | =for apidoc sv_setsv_mg | |
4070 | ||
4071 | Like C<sv_setsv>, but also handles 'set' magic. | |
4072 | ||
4073 | =cut | |
4074 | */ | |
4075 | ||
79072805 | 4076 | void |
864dbfa3 | 4077 | Perl_sv_setsv_mg(pTHX_ SV *dstr, register SV *sstr) |
ef50df4b GS |
4078 | { |
4079 | sv_setsv(dstr,sstr); | |
4080 | SvSETMAGIC(dstr); | |
4081 | } | |
4082 | ||
954c1994 GS |
4083 | /* |
4084 | =for apidoc sv_setpvn | |
4085 | ||
4086 | Copies a string into an SV. The C<len> parameter indicates the number of | |
4087 | bytes to be copied. Does not handle 'set' magic. See C<sv_setpvn_mg>. | |
4088 | ||
4089 | =cut | |
4090 | */ | |
4091 | ||
ef50df4b | 4092 | void |
864dbfa3 | 4093 | Perl_sv_setpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len) |
79072805 | 4094 | { |
c6f8c383 | 4095 | register char *dptr; |
22c522df | 4096 | |
2213622d | 4097 | SV_CHECK_THINKFIRST(sv); |
463ee0b2 | 4098 | if (!ptr) { |
a0d0e21e | 4099 | (void)SvOK_off(sv); |
463ee0b2 LW |
4100 | return; |
4101 | } | |
22c522df JH |
4102 | else { |
4103 | /* len is STRLEN which is unsigned, need to copy to signed */ | |
4104 | IV iv = len; | |
9c5ffd7c JH |
4105 | if (iv < 0) |
4106 | Perl_croak(aTHX_ "panic: sv_setpvn called with negative strlen"); | |
22c522df | 4107 | } |
6fc92669 | 4108 | (void)SvUPGRADE(sv, SVt_PV); |
c6f8c383 | 4109 | |
79072805 | 4110 | SvGROW(sv, len + 1); |
c6f8c383 GA |
4111 | dptr = SvPVX(sv); |
4112 | Move(ptr,dptr,len,char); | |
4113 | dptr[len] = '\0'; | |
79072805 | 4114 | SvCUR_set(sv, len); |
1aa99e6b | 4115 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 | 4116 | SvTAINT(sv); |
79072805 LW |
4117 | } |
4118 | ||
954c1994 GS |
4119 | /* |
4120 | =for apidoc sv_setpvn_mg | |
4121 | ||
4122 | Like C<sv_setpvn>, but also handles 'set' magic. | |
4123 | ||
4124 | =cut | |
4125 | */ | |
4126 | ||
79072805 | 4127 | void |
864dbfa3 | 4128 | Perl_sv_setpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len) |
ef50df4b GS |
4129 | { |
4130 | sv_setpvn(sv,ptr,len); | |
4131 | SvSETMAGIC(sv); | |
4132 | } | |
4133 | ||
954c1994 GS |
4134 | /* |
4135 | =for apidoc sv_setpv | |
4136 | ||
4137 | Copies a string into an SV. The string must be null-terminated. Does not | |
4138 | handle 'set' magic. See C<sv_setpv_mg>. | |
4139 | ||
4140 | =cut | |
4141 | */ | |
4142 | ||
ef50df4b | 4143 | void |
864dbfa3 | 4144 | Perl_sv_setpv(pTHX_ register SV *sv, register const char *ptr) |
79072805 LW |
4145 | { |
4146 | register STRLEN len; | |
4147 | ||
2213622d | 4148 | SV_CHECK_THINKFIRST(sv); |
463ee0b2 | 4149 | if (!ptr) { |
a0d0e21e | 4150 | (void)SvOK_off(sv); |
463ee0b2 LW |
4151 | return; |
4152 | } | |
79072805 | 4153 | len = strlen(ptr); |
6fc92669 | 4154 | (void)SvUPGRADE(sv, SVt_PV); |
c6f8c383 | 4155 | |
79072805 | 4156 | SvGROW(sv, len + 1); |
463ee0b2 | 4157 | Move(ptr,SvPVX(sv),len+1,char); |
79072805 | 4158 | SvCUR_set(sv, len); |
1aa99e6b | 4159 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 LW |
4160 | SvTAINT(sv); |
4161 | } | |
4162 | ||
954c1994 GS |
4163 | /* |
4164 | =for apidoc sv_setpv_mg | |
4165 | ||
4166 | Like C<sv_setpv>, but also handles 'set' magic. | |
4167 | ||
4168 | =cut | |
4169 | */ | |
4170 | ||
463ee0b2 | 4171 | void |
864dbfa3 | 4172 | Perl_sv_setpv_mg(pTHX_ register SV *sv, register const char *ptr) |
ef50df4b GS |
4173 | { |
4174 | sv_setpv(sv,ptr); | |
4175 | SvSETMAGIC(sv); | |
4176 | } | |
4177 | ||
954c1994 GS |
4178 | /* |
4179 | =for apidoc sv_usepvn | |
4180 | ||
4181 | Tells an SV to use C<ptr> to find its string value. Normally the string is | |
1c846c1f | 4182 | stored inside the SV but sv_usepvn allows the SV to use an outside string. |
954c1994 GS |
4183 | The C<ptr> should point to memory that was allocated by C<malloc>. The |
4184 | string length, C<len>, must be supplied. This function will realloc the | |
4185 | memory pointed to by C<ptr>, so that pointer should not be freed or used by | |
4186 | the programmer after giving it to sv_usepvn. Does not handle 'set' magic. | |
4187 | See C<sv_usepvn_mg>. | |
4188 | ||
4189 | =cut | |
4190 | */ | |
4191 | ||
ef50df4b | 4192 | void |
864dbfa3 | 4193 | Perl_sv_usepvn(pTHX_ register SV *sv, register char *ptr, register STRLEN len) |
463ee0b2 | 4194 | { |
2213622d | 4195 | SV_CHECK_THINKFIRST(sv); |
c6f8c383 | 4196 | (void)SvUPGRADE(sv, SVt_PV); |
463ee0b2 | 4197 | if (!ptr) { |
a0d0e21e | 4198 | (void)SvOK_off(sv); |
463ee0b2 LW |
4199 | return; |
4200 | } | |
a0ed51b3 | 4201 | (void)SvOOK_off(sv); |
50483b2c | 4202 | if (SvPVX(sv) && SvLEN(sv)) |
463ee0b2 LW |
4203 | Safefree(SvPVX(sv)); |
4204 | Renew(ptr, len+1, char); | |
4205 | SvPVX(sv) = ptr; | |
4206 | SvCUR_set(sv, len); | |
4207 | SvLEN_set(sv, len+1); | |
4208 | *SvEND(sv) = '\0'; | |
1aa99e6b | 4209 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 | 4210 | SvTAINT(sv); |
79072805 LW |
4211 | } |
4212 | ||
954c1994 GS |
4213 | /* |
4214 | =for apidoc sv_usepvn_mg | |
4215 | ||
4216 | Like C<sv_usepvn>, but also handles 'set' magic. | |
4217 | ||
4218 | =cut | |
4219 | */ | |
4220 | ||
ef50df4b | 4221 | void |
864dbfa3 | 4222 | Perl_sv_usepvn_mg(pTHX_ register SV *sv, register char *ptr, register STRLEN len) |
ef50df4b | 4223 | { |
51c1089b | 4224 | sv_usepvn(sv,ptr,len); |
ef50df4b GS |
4225 | SvSETMAGIC(sv); |
4226 | } | |
4227 | ||
645c22ef DM |
4228 | /* |
4229 | =for apidoc sv_force_normal_flags | |
4230 | ||
4231 | Undo various types of fakery on an SV: if the PV is a shared string, make | |
4232 | a private copy; if we're a ref, stop refing; if we're a glob, downgrade to | |
4233 | an xpvmg. The C<flags> parameter gets passed to C<sv_unref_flags()> | |
4234 | when unrefing. C<sv_force_normal> calls this function with flags set to 0. | |
4235 | ||
4236 | =cut | |
4237 | */ | |
4238 | ||
6fc92669 | 4239 | void |
840a7b70 | 4240 | Perl_sv_force_normal_flags(pTHX_ register SV *sv, U32 flags) |
0f15f207 | 4241 | { |
2213622d | 4242 | if (SvREADONLY(sv)) { |
1c846c1f NIS |
4243 | if (SvFAKE(sv)) { |
4244 | char *pvx = SvPVX(sv); | |
4245 | STRLEN len = SvCUR(sv); | |
4246 | U32 hash = SvUVX(sv); | |
4247 | SvGROW(sv, len + 1); | |
4248 | Move(pvx,SvPVX(sv),len,char); | |
4249 | *SvEND(sv) = '\0'; | |
4250 | SvFAKE_off(sv); | |
4251 | SvREADONLY_off(sv); | |
25716404 | 4252 | unsharepvn(pvx, SvUTF8(sv) ? -(I32)len : len, hash); |
1c846c1f NIS |
4253 | } |
4254 | else if (PL_curcop != &PL_compiling) | |
cea2e8a9 | 4255 | Perl_croak(aTHX_ PL_no_modify); |
0f15f207 | 4256 | } |
2213622d | 4257 | if (SvROK(sv)) |
840a7b70 | 4258 | sv_unref_flags(sv, flags); |
6fc92669 GS |
4259 | else if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV) |
4260 | sv_unglob(sv); | |
0f15f207 | 4261 | } |
1c846c1f | 4262 | |
645c22ef DM |
4263 | /* |
4264 | =for apidoc sv_force_normal | |
4265 | ||
4266 | Undo various types of fakery on an SV: if the PV is a shared string, make | |
4267 | a private copy; if we're a ref, stop refing; if we're a glob, downgrade to | |
4268 | an xpvmg. See also C<sv_force_normal_flags>. | |
4269 | ||
4270 | =cut | |
4271 | */ | |
4272 | ||
840a7b70 IZ |
4273 | void |
4274 | Perl_sv_force_normal(pTHX_ register SV *sv) | |
4275 | { | |
4276 | sv_force_normal_flags(sv, 0); | |
4277 | } | |
4278 | ||
954c1994 GS |
4279 | /* |
4280 | =for apidoc sv_chop | |
4281 | ||
1c846c1f | 4282 | Efficient removal of characters from the beginning of the string buffer. |
954c1994 GS |
4283 | SvPOK(sv) must be true and the C<ptr> must be a pointer to somewhere inside |
4284 | the string buffer. The C<ptr> becomes the first character of the adjusted | |
645c22ef | 4285 | string. Uses the "OOK hack". |
b9219079 JH |
4286 | Beware: after this function returns, C<ptr> and SvPVX(sv) may no longer |
4287 | refer to the same chunk of data. | |
954c1994 GS |
4288 | |
4289 | =cut | |
4290 | */ | |
4291 | ||
79072805 | 4292 | void |
645c22ef | 4293 | Perl_sv_chop(pTHX_ register SV *sv, register char *ptr) |
79072805 LW |
4294 | { |
4295 | register STRLEN delta; | |
a0d0e21e | 4296 | if (!ptr || !SvPOKp(sv)) |
79072805 | 4297 | return; |
b9219079 | 4298 | delta = ptr - SvPVX(sv); |
2213622d | 4299 | SV_CHECK_THINKFIRST(sv); |
79072805 LW |
4300 | if (SvTYPE(sv) < SVt_PVIV) |
4301 | sv_upgrade(sv,SVt_PVIV); | |
4302 | ||
4303 | if (!SvOOK(sv)) { | |
50483b2c JD |
4304 | if (!SvLEN(sv)) { /* make copy of shared string */ |
4305 | char *pvx = SvPVX(sv); | |
4306 | STRLEN len = SvCUR(sv); | |
4307 | SvGROW(sv, len + 1); | |
4308 | Move(pvx,SvPVX(sv),len,char); | |
4309 | *SvEND(sv) = '\0'; | |
4310 | } | |
463ee0b2 | 4311 | SvIVX(sv) = 0; |
e33c26d0 JH |
4312 | /* Same SvOOK_on but SvOOK_on does a SvIOK_off |
4313 | and we do that anyway inside the SvNIOK_off | |
4314 | */ | |
4315 | SvFLAGS(sv) |= SVf_OOK; | |
79072805 | 4316 | } |
e33c26d0 | 4317 | SvNIOK_off(sv); |
79072805 LW |
4318 | SvLEN(sv) -= delta; |
4319 | SvCUR(sv) -= delta; | |
463ee0b2 LW |
4320 | SvPVX(sv) += delta; |
4321 | SvIVX(sv) += delta; | |
79072805 LW |
4322 | } |
4323 | ||
d34f9d2e JH |
4324 | /* sv_catpvn() is now a macro using Perl_sv_catpvn_flags(); |
4325 | * this function provided for binary compatibility only | |
4326 | */ | |
4327 | ||
4328 | void | |
4329 | Perl_sv_catpvn(pTHX_ SV *dsv, const char* sstr, STRLEN slen) | |
4330 | { | |
4331 | sv_catpvn_flags(dsv, sstr, slen, SV_GMAGIC); | |
4332 | } | |
4333 | ||
954c1994 GS |
4334 | /* |
4335 | =for apidoc sv_catpvn | |
4336 | ||
4337 | Concatenates the string onto the end of the string which is in the SV. The | |
cd458e05 JH |
4338 | C<len> indicates number of bytes to copy. If the SV has the UTF-8 |
4339 | status set, then the bytes appended should be valid UTF-8. | |
d5ce4a7c | 4340 | Handles 'get' magic, but not 'set' magic. See C<sv_catpvn_mg>. |
954c1994 | 4341 | |
8d6d96c1 HS |
4342 | =for apidoc sv_catpvn_flags |
4343 | ||
4344 | Concatenates the string onto the end of the string which is in the SV. The | |
cd458e05 JH |
4345 | C<len> indicates number of bytes to copy. If the SV has the UTF-8 |
4346 | status set, then the bytes appended should be valid UTF-8. | |
8d6d96c1 HS |
4347 | If C<flags> has C<SV_GMAGIC> bit set, will C<mg_get> on C<dsv> if |
4348 | appropriate, else not. C<sv_catpvn> and C<sv_catpvn_nomg> are implemented | |
4349 | in terms of this function. | |
4350 | ||
4351 | =cut | |
4352 | */ | |
4353 | ||
4354 | void | |
4355 | Perl_sv_catpvn_flags(pTHX_ register SV *dsv, register const char *sstr, register STRLEN slen, I32 flags) | |
4356 | { | |
4357 | STRLEN dlen; | |
4358 | char *dstr; | |
4359 | ||
4360 | dstr = SvPV_force_flags(dsv, dlen, flags); | |
4361 | SvGROW(dsv, dlen + slen + 1); | |
4362 | if (sstr == dstr) | |
4363 | sstr = SvPVX(dsv); | |
4364 | Move(sstr, SvPVX(dsv) + dlen, slen, char); | |
4365 | SvCUR(dsv) += slen; | |
4366 | *SvEND(dsv) = '\0'; | |
4367 | (void)SvPOK_only_UTF8(dsv); /* validate pointer */ | |
4368 | SvTAINT(dsv); | |
79072805 LW |
4369 | } |
4370 | ||
954c1994 GS |
4371 | /* |
4372 | =for apidoc sv_catpvn_mg | |
4373 | ||
4374 | Like C<sv_catpvn>, but also handles 'set' magic. | |
4375 | ||
4376 | =cut | |
4377 | */ | |
4378 | ||
79072805 | 4379 | void |
864dbfa3 | 4380 | Perl_sv_catpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len) |
ef50df4b GS |
4381 | { |
4382 | sv_catpvn(sv,ptr,len); | |
4383 | SvSETMAGIC(sv); | |
4384 | } | |
4385 | ||
d34f9d2e JH |
4386 | /* sv_catsv() is now a macro using Perl_sv_catsv_flags(); |
4387 | * this function provided for binary compatibility only | |
4388 | */ | |
4389 | ||
4390 | void | |
4391 | Perl_sv_catsv(pTHX_ SV *dstr, register SV *sstr) | |
4392 | { | |
4393 | sv_catsv_flags(dstr, sstr, SV_GMAGIC); | |
4394 | } | |
4395 | ||
954c1994 GS |
4396 | /* |
4397 | =for apidoc sv_catsv | |
4398 | ||
13e8c8e3 JH |
4399 | Concatenates the string from SV C<ssv> onto the end of the string in |
4400 | SV C<dsv>. Modifies C<dsv> but not C<ssv>. Handles 'get' magic, but | |
4401 | not 'set' magic. See C<sv_catsv_mg>. | |
954c1994 | 4402 | |
8d6d96c1 HS |
4403 | =for apidoc sv_catsv_flags |
4404 | ||
4405 | Concatenates the string from SV C<ssv> onto the end of the string in | |
4406 | SV C<dsv>. Modifies C<dsv> but not C<ssv>. If C<flags> has C<SV_GMAGIC> | |
4407 | bit set, will C<mg_get> on the SVs if appropriate, else not. C<sv_catsv> | |
4408 | and C<sv_catsv_nomg> are implemented in terms of this function. | |
4409 | ||
4410 | =cut */ | |
4411 | ||
ef50df4b | 4412 | void |
8d6d96c1 | 4413 | Perl_sv_catsv_flags(pTHX_ SV *dsv, register SV *ssv, I32 flags) |
79072805 | 4414 | { |
13e8c8e3 JH |
4415 | char *spv; |
4416 | STRLEN slen; | |
46199a12 | 4417 | if (!ssv) |
79072805 | 4418 | return; |
46199a12 | 4419 | if ((spv = SvPV(ssv, slen))) { |
4fd84b44 AD |
4420 | /* sutf8 and dutf8 were type bool, but under USE_ITHREADS, |
4421 | gcc version 2.95.2 20000220 (Debian GNU/Linux) for | |
8cf8f3d1 NIS |
4422 | Linux xxx 2.2.17 on sparc64 with gcc -O2, we erroneously |
4423 | get dutf8 = 0x20000000, (i.e. SVf_UTF8) even though | |
4fd84b44 AD |
4424 | dsv->sv_flags doesn't have that bit set. |
4425 | Andy Dougherty 12 Oct 2001 | |
4426 | */ | |
4427 | I32 sutf8 = DO_UTF8(ssv); | |
4428 | I32 dutf8; | |
13e8c8e3 | 4429 | |
8d6d96c1 HS |
4430 | if (SvGMAGICAL(dsv) && (flags & SV_GMAGIC)) |
4431 | mg_get(dsv); | |
4432 | dutf8 = DO_UTF8(dsv); | |
4433 | ||
4434 | if (dutf8 != sutf8) { | |
13e8c8e3 | 4435 | if (dutf8) { |
46199a12 | 4436 | /* Not modifying source SV, so taking a temporary copy. */ |
8d6d96c1 | 4437 | SV* csv = sv_2mortal(newSVpvn(spv, slen)); |
13e8c8e3 | 4438 | |
46199a12 | 4439 | sv_utf8_upgrade(csv); |
8d6d96c1 | 4440 | spv = SvPV(csv, slen); |
13e8c8e3 | 4441 | } |
8d6d96c1 HS |
4442 | else |
4443 | sv_utf8_upgrade_nomg(dsv); | |
e84ff256 | 4444 | } |
8d6d96c1 | 4445 | sv_catpvn_nomg(dsv, spv, slen); |
560a288e | 4446 | } |
79072805 LW |
4447 | } |
4448 | ||
954c1994 GS |
4449 | /* |
4450 | =for apidoc sv_catsv_mg | |
4451 | ||
4452 | Like C<sv_catsv>, but also handles 'set' magic. | |
4453 | ||
4454 | =cut | |
4455 | */ | |
4456 | ||
79072805 | 4457 | void |
46199a12 | 4458 | Perl_sv_catsv_mg(pTHX_ SV *dsv, register SV *ssv) |
ef50df4b | 4459 | { |
46199a12 JH |
4460 | sv_catsv(dsv,ssv); |
4461 | SvSETMAGIC(dsv); | |
ef50df4b GS |
4462 | } |
4463 | ||
954c1994 GS |
4464 | /* |
4465 | =for apidoc sv_catpv | |
4466 | ||
4467 | Concatenates the string onto the end of the string which is in the SV. | |
cd458e05 JH |
4468 | If the SV has the UTF-8 status set, then the bytes appended should be |
4469 | valid UTF-8. Handles 'get' magic, but not 'set' magic. See C<sv_catpv_mg>. | |
954c1994 | 4470 | |
d5ce4a7c | 4471 | =cut */ |
954c1994 | 4472 | |
ef50df4b | 4473 | void |
0c981600 | 4474 | Perl_sv_catpv(pTHX_ register SV *sv, register const char *ptr) |
79072805 LW |
4475 | { |
4476 | register STRLEN len; | |
463ee0b2 | 4477 | STRLEN tlen; |
748a9306 | 4478 | char *junk; |
79072805 | 4479 | |
0c981600 | 4480 | if (!ptr) |
79072805 | 4481 | return; |
748a9306 | 4482 | junk = SvPV_force(sv, tlen); |
0c981600 | 4483 | len = strlen(ptr); |
463ee0b2 | 4484 | SvGROW(sv, tlen + len + 1); |
0c981600 JH |
4485 | if (ptr == junk) |
4486 | ptr = SvPVX(sv); | |
4487 | Move(ptr,SvPVX(sv)+tlen,len+1,char); | |
79072805 | 4488 | SvCUR(sv) += len; |
d41ff1b8 | 4489 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 | 4490 | SvTAINT(sv); |
79072805 LW |
4491 | } |
4492 | ||
954c1994 GS |
4493 | /* |
4494 | =for apidoc sv_catpv_mg | |
4495 | ||
4496 | Like C<sv_catpv>, but also handles 'set' magic. | |
4497 | ||
4498 | =cut | |
4499 | */ | |
4500 | ||
ef50df4b | 4501 | void |
0c981600 | 4502 | Perl_sv_catpv_mg(pTHX_ register SV *sv, register const char *ptr) |
ef50df4b | 4503 | { |
0c981600 | 4504 | sv_catpv(sv,ptr); |
ef50df4b GS |
4505 | SvSETMAGIC(sv); |
4506 | } | |
4507 | ||
645c22ef DM |
4508 | /* |
4509 | =for apidoc newSV | |
4510 | ||
4511 | Create a new null SV, or if len > 0, create a new empty SVt_PV type SV | |
4512 | with an initial PV allocation of len+1. Normally accessed via the C<NEWSV> | |
4513 | macro. | |
4514 | ||
4515 | =cut | |
4516 | */ | |
4517 | ||
79072805 | 4518 | SV * |
864dbfa3 | 4519 | Perl_newSV(pTHX_ STRLEN len) |
79072805 LW |
4520 | { |
4521 | register SV *sv; | |
1c846c1f | 4522 | |
4561caa4 | 4523 | new_SV(sv); |
79072805 LW |
4524 | if (len) { |
4525 | sv_upgrade(sv, SVt_PV); | |
4526 | SvGROW(sv, len + 1); | |
4527 | } | |
4528 | return sv; | |
4529 | } | |
954c1994 | 4530 | /* |
92110913 | 4531 | =for apidoc sv_magicext |
954c1994 | 4532 | |
68795e93 | 4533 | Adds magic to an SV, upgrading it if necessary. Applies the |
92110913 NIS |
4534 | supplied vtable and returns pointer to the magic added. |
4535 | ||
4536 | Note that sv_magicext will allow things that sv_magic will not. | |
68795e93 | 4537 | In particular you can add magic to SvREADONLY SVs and and more than |
92110913 | 4538 | one instance of the same 'how' |
645c22ef | 4539 | |
92110913 | 4540 | I C<namelen> is greater then zero then a savepvn() I<copy> of C<name> is stored, |
68795e93 NIS |
4541 | if C<namelen> is zero then C<name> is stored as-is and - as another special |
4542 | case - if C<(name && namelen == HEf_SVKEY)> then C<name> is assumed to contain | |
92110913 NIS |
4543 | an C<SV*> and has its REFCNT incremented |
4544 | ||
4545 | (This is now used as a subroutine by sv_magic.) | |
954c1994 GS |
4546 | |
4547 | =cut | |
4548 | */ | |
92110913 NIS |
4549 | MAGIC * |
4550 | Perl_sv_magicext(pTHX_ SV* sv, SV* obj, int how, MGVTBL *vtable, | |
4551 | const char* name, I32 namlen) | |
79072805 LW |
4552 | { |
4553 | MAGIC* mg; | |
68795e93 | 4554 | |
92110913 NIS |
4555 | if (SvTYPE(sv) < SVt_PVMG) { |
4556 | (void)SvUPGRADE(sv, SVt_PVMG); | |
463ee0b2 | 4557 | } |
79072805 LW |
4558 | Newz(702,mg, 1, MAGIC); |
4559 | mg->mg_moremagic = SvMAGIC(sv); | |
79072805 | 4560 | SvMAGIC(sv) = mg; |
75f9d97a | 4561 | |
18808301 | 4562 | /* Some magic sontains a reference loop, where the sv and object refer to |
bb03859b RS |
4563 | each other. To prevent a reference loop that would prevent such |
4564 | objects being freed, we look for such loops and if we find one we | |
87f0b213 JH |
4565 | avoid incrementing the object refcount. |
4566 | ||
4567 | Note we cannot do this to avoid self-tie loops as intervening RV must | |
8ccffa6a | 4568 | have its REFCNT incremented to keep it in existence. |
87f0b213 JH |
4569 | |
4570 | */ | |
14befaf4 DM |
4571 | if (!obj || obj == sv || |
4572 | how == PERL_MAGIC_arylen || | |
4573 | how == PERL_MAGIC_qr || | |
75f9d97a JH |
4574 | (SvTYPE(obj) == SVt_PVGV && |
4575 | (GvSV(obj) == sv || GvHV(obj) == (HV*)sv || GvAV(obj) == (AV*)sv || | |
4576 | GvCV(obj) == (CV*)sv || GvIOp(obj) == (IO*)sv || | |
2628be26 | 4577 | GvFORM(obj) == (CV*)sv))) |
75f9d97a | 4578 | { |
8990e307 | 4579 | mg->mg_obj = obj; |
75f9d97a | 4580 | } |
85e6fe83 | 4581 | else { |
8990e307 | 4582 | mg->mg_obj = SvREFCNT_inc(obj); |
85e6fe83 LW |
4583 | mg->mg_flags |= MGf_REFCOUNTED; |
4584 | } | |
8ccffa6a YST |
4585 | |
4586 | /* Normal self-ties simply pass a null object, and instead of | |
4587 | using mg_obj directly, use the SvTIED_obj macro to produce a | |
4588 | new RV as needed. For glob "self-ties", we are tieing the PVIO | |
4589 | with an RV obj pointing to the glob containing the PVIO. In | |
4590 | this case, to avoid a reference loop, we need to weaken the | |
4591 | reference. | |
4592 | */ | |
4593 | ||
4594 | if (how == PERL_MAGIC_tiedscalar && SvTYPE(sv) == SVt_PVIO && | |
4595 | obj && SvROK(obj) && GvIO(SvRV(obj)) == (IO*)sv) | |
4596 | { | |
4597 | sv_rvweaken(obj); | |
4598 | } | |
4599 | ||
79072805 | 4600 | mg->mg_type = how; |
565764a8 | 4601 | mg->mg_len = namlen; |
9cbac4c7 | 4602 | if (name) { |
92110913 | 4603 | if (namlen > 0) |
1edc1566 | 4604 | mg->mg_ptr = savepvn(name, namlen); |
c6ee37c5 | 4605 | else if (namlen == HEf_SVKEY) |
1edc1566 | 4606 | mg->mg_ptr = (char*)SvREFCNT_inc((SV*)name); |
68795e93 | 4607 | else |
92110913 | 4608 | mg->mg_ptr = (char *) name; |
9cbac4c7 | 4609 | } |
92110913 | 4610 | mg->mg_virtual = vtable; |
68795e93 | 4611 | |
92110913 NIS |
4612 | mg_magical(sv); |
4613 | if (SvGMAGICAL(sv)) | |
4614 | SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK); | |
4615 | return mg; | |
4616 | } | |
4617 | ||
4618 | /* | |
4619 | =for apidoc sv_magic | |
1c846c1f | 4620 | |
92110913 NIS |
4621 | Adds magic to an SV. First upgrades C<sv> to type C<SVt_PVMG> if necessary, |
4622 | then adds a new magic item of type C<how> to the head of the magic list. | |
4623 | ||
4624 | =cut | |
4625 | */ | |
4626 | ||
4627 | void | |
4628 | Perl_sv_magic(pTHX_ register SV *sv, SV *obj, int how, const char *name, I32 namlen) | |
68795e93 | 4629 | { |
92110913 NIS |
4630 | MAGIC* mg; |
4631 | MGVTBL *vtable = 0; | |
4632 | ||
4633 | if (SvREADONLY(sv)) { | |
4634 | if (PL_curcop != &PL_compiling | |
4635 | && how != PERL_MAGIC_regex_global | |
4636 | && how != PERL_MAGIC_bm | |
4637 | && how != PERL_MAGIC_fm | |
4638 | && how != PERL_MAGIC_sv | |
4639 | ) | |
4640 | { | |
4641 | Perl_croak(aTHX_ PL_no_modify); | |
4642 | } | |
4643 | } | |
4644 | if (SvMAGICAL(sv) || (how == PERL_MAGIC_taint && SvTYPE(sv) >= SVt_PVMG)) { | |
4645 | if (SvMAGIC(sv) && (mg = mg_find(sv, how))) { | |
68795e93 NIS |
4646 | /* sv_magic() refuses to add a magic of the same 'how' as an |
4647 | existing one | |
92110913 NIS |
4648 | */ |
4649 | if (how == PERL_MAGIC_taint) | |
4650 | mg->mg_len |= 1; | |
4651 | return; | |
4652 | } | |
4653 | } | |
68795e93 | 4654 | |
79072805 | 4655 | switch (how) { |
14befaf4 | 4656 | case PERL_MAGIC_sv: |
92110913 | 4657 | vtable = &PL_vtbl_sv; |
79072805 | 4658 | break; |
14befaf4 | 4659 | case PERL_MAGIC_overload: |
92110913 | 4660 | vtable = &PL_vtbl_amagic; |
a0d0e21e | 4661 | break; |
14befaf4 | 4662 | case PERL_MAGIC_overload_elem: |
92110913 | 4663 | vtable = &PL_vtbl_amagicelem; |
a0d0e21e | 4664 | break; |
14befaf4 | 4665 | case PERL_MAGIC_overload_table: |
92110913 | 4666 | vtable = &PL_vtbl_ovrld; |
a0d0e21e | 4667 | break; |
14befaf4 | 4668 | case PERL_MAGIC_bm: |
92110913 | 4669 | vtable = &PL_vtbl_bm; |
79072805 | 4670 | break; |
14befaf4 | 4671 | case PERL_MAGIC_regdata: |
92110913 | 4672 | vtable = &PL_vtbl_regdata; |
6cef1e77 | 4673 | break; |
14befaf4 | 4674 | case PERL_MAGIC_regdatum: |
92110913 | 4675 | vtable = &PL_vtbl_regdatum; |
6cef1e77 | 4676 | break; |
14befaf4 | 4677 | case PERL_MAGIC_env: |
92110913 | 4678 | vtable = &PL_vtbl_env; |
79072805 | 4679 | break; |
14befaf4 | 4680 | case PERL_MAGIC_fm: |
92110913 | 4681 | vtable = &PL_vtbl_fm; |
55497cff | 4682 | break; |
14befaf4 | 4683 | case PERL_MAGIC_envelem: |
92110913 | 4684 | vtable = &PL_vtbl_envelem; |
79072805 | 4685 | break; |
14befaf4 | 4686 | case PERL_MAGIC_regex_global: |
92110913 | 4687 | vtable = &PL_vtbl_mglob; |
93a17b20 | 4688 | break; |
14befaf4 | 4689 | case PERL_MAGIC_isa: |
92110913 | 4690 | vtable = &PL_vtbl_isa; |
463ee0b2 | 4691 | break; |
14befaf4 | 4692 | case PERL_MAGIC_isaelem: |
92110913 | 4693 | vtable = &PL_vtbl_isaelem; |
463ee0b2 | 4694 | break; |
14befaf4 | 4695 | case PERL_MAGIC_nkeys: |
92110913 | 4696 | vtable = &PL_vtbl_nkeys; |
16660edb | 4697 | break; |
14befaf4 | 4698 | case PERL_MAGIC_dbfile: |
92110913 | 4699 | vtable = 0; |
93a17b20 | 4700 | break; |
14befaf4 | 4701 | case PERL_MAGIC_dbline: |
92110913 | 4702 | vtable = &PL_vtbl_dbline; |
79072805 | 4703 | break; |
4d1ff10f | 4704 | #ifdef USE_5005THREADS |
14befaf4 | 4705 | case PERL_MAGIC_mutex: |
92110913 | 4706 | vtable = &PL_vtbl_mutex; |
f93b4edd | 4707 | break; |
4d1ff10f | 4708 | #endif /* USE_5005THREADS */ |
36477c24 | 4709 | #ifdef USE_LOCALE_COLLATE |
14befaf4 | 4710 | case PERL_MAGIC_collxfrm: |
92110913 | 4711 | vtable = &PL_vtbl_collxfrm; |
bbce6d69 | 4712 | break; |
36477c24 | 4713 | #endif /* USE_LOCALE_COLLATE */ |
14befaf4 | 4714 | case PERL_MAGIC_tied: |
92110913 | 4715 | vtable = &PL_vtbl_pack; |
463ee0b2 | 4716 | break; |
14befaf4 DM |
4717 | case PERL_MAGIC_tiedelem: |
4718 | case PERL_MAGIC_tiedscalar: | |
92110913 | 4719 | vtable = &PL_vtbl_packelem; |
463ee0b2 | 4720 | break; |
14befaf4 | 4721 | case PERL_MAGIC_qr: |
92110913 | 4722 | vtable = &PL_vtbl_regexp; |
c277df42 | 4723 | break; |
14befaf4 | 4724 | case PERL_MAGIC_sig: |
92110913 | 4725 | vtable = &PL_vtbl_sig; |
79072805 | 4726 | break; |
14befaf4 | 4727 | case PERL_MAGIC_sigelem: |
92110913 | 4728 | vtable = &PL_vtbl_sigelem; |
79072805 | 4729 | break; |
14befaf4 | 4730 | case PERL_MAGIC_taint: |
92110913 | 4731 | vtable = &PL_vtbl_taint; |
463ee0b2 | 4732 | break; |
14befaf4 | 4733 | case PERL_MAGIC_uvar: |
92110913 | 4734 | vtable = &PL_vtbl_uvar; |
79072805 | 4735 | break; |
14befaf4 | 4736 | case PERL_MAGIC_vec: |
92110913 | 4737 | vtable = &PL_vtbl_vec; |
79072805 | 4738 | break; |
7d7ce6cc JH |
4739 | case PERL_MAGIC_vstring: |
4740 | vtable = 0; | |
4741 | break; | |
323eb6b5 JH |
4742 | case PERL_MAGIC_utf8: |
4743 | vtable = &PL_vtbl_utf8; | |
4744 | break; | |
14befaf4 | 4745 | case PERL_MAGIC_substr: |
92110913 | 4746 | vtable = &PL_vtbl_substr; |
79072805 | 4747 | break; |
14befaf4 | 4748 | case PERL_MAGIC_defelem: |
92110913 | 4749 | vtable = &PL_vtbl_defelem; |
5f05dabc | 4750 | break; |
14befaf4 | 4751 | case PERL_MAGIC_glob: |
92110913 | 4752 | vtable = &PL_vtbl_glob; |
79072805 | 4753 | break; |
14befaf4 | 4754 | case PERL_MAGIC_arylen: |
92110913 | 4755 | vtable = &PL_vtbl_arylen; |
79072805 | 4756 | break; |
14befaf4 | 4757 | case PERL_MAGIC_pos: |
92110913 | 4758 | vtable = &PL_vtbl_pos; |
a0d0e21e | 4759 | break; |
14befaf4 | 4760 | case PERL_MAGIC_backref: |
92110913 | 4761 | vtable = &PL_vtbl_backref; |
810b8aa5 | 4762 | break; |
14befaf4 DM |
4763 | case PERL_MAGIC_ext: |
4764 | /* Reserved for use by extensions not perl internals. */ | |
4633a7c4 LW |
4765 | /* Useful for attaching extension internal data to perl vars. */ |
4766 | /* Note that multiple extensions may clash if magical scalars */ | |
4767 | /* etc holding private data from one are passed to another. */ | |
a0d0e21e | 4768 | break; |
79072805 | 4769 | default: |
14befaf4 | 4770 | Perl_croak(aTHX_ "Don't know how to handle magic of type \\%o", how); |
463ee0b2 | 4771 | } |
68795e93 | 4772 | |
92110913 NIS |
4773 | /* Rest of work is done else where */ |
4774 | mg = sv_magicext(sv,obj,how,vtable,name,namlen); | |
68795e93 | 4775 | |
92110913 NIS |
4776 | switch (how) { |
4777 | case PERL_MAGIC_taint: | |
4778 | mg->mg_len = 1; | |
4779 | break; | |
4780 | case PERL_MAGIC_ext: | |
4781 | case PERL_MAGIC_dbfile: | |
4782 | SvRMAGICAL_on(sv); | |
4783 | break; | |
4784 | } | |
463ee0b2 LW |
4785 | } |
4786 | ||
c461cf8f JH |
4787 | /* |
4788 | =for apidoc sv_unmagic | |
4789 | ||
645c22ef | 4790 | Removes all magic of type C<type> from an SV. |
c461cf8f JH |
4791 | |
4792 | =cut | |
4793 | */ | |
4794 | ||
463ee0b2 | 4795 | int |
864dbfa3 | 4796 | Perl_sv_unmagic(pTHX_ SV *sv, int type) |
463ee0b2 LW |
4797 | { |
4798 | MAGIC* mg; | |
4799 | MAGIC** mgp; | |
91bba347 | 4800 | if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv)) |
463ee0b2 LW |
4801 | return 0; |
4802 | mgp = &SvMAGIC(sv); | |
4803 | for (mg = *mgp; mg; mg = *mgp) { | |
4804 | if (mg->mg_type == type) { | |
4805 | MGVTBL* vtbl = mg->mg_virtual; | |
4806 | *mgp = mg->mg_moremagic; | |
1d7c1841 | 4807 | if (vtbl && vtbl->svt_free) |
fc0dc3b3 | 4808 | CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg); |
14befaf4 | 4809 | if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) { |
92110913 | 4810 | if (mg->mg_len > 0) |
1edc1566 | 4811 | Safefree(mg->mg_ptr); |
565764a8 | 4812 | else if (mg->mg_len == HEf_SVKEY) |
1edc1566 | 4813 | SvREFCNT_dec((SV*)mg->mg_ptr); |
323eb6b5 JH |
4814 | else if (mg->mg_type == PERL_MAGIC_utf8 && mg->mg_ptr) |
4815 | Safefree(mg->mg_ptr); | |
9cbac4c7 | 4816 | } |
a0d0e21e LW |
4817 | if (mg->mg_flags & MGf_REFCOUNTED) |
4818 | SvREFCNT_dec(mg->mg_obj); | |
463ee0b2 LW |
4819 | Safefree(mg); |
4820 | } | |
4821 | else | |
4822 | mgp = &mg->mg_moremagic; | |
79072805 | 4823 | } |
91bba347 | 4824 | if (!SvMAGIC(sv)) { |
463ee0b2 | 4825 | SvMAGICAL_off(sv); |
06759ea0 | 4826 | SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_NOK|SVp_POK)) >> PRIVSHIFT; |
463ee0b2 LW |
4827 | } |
4828 | ||
4829 | return 0; | |
79072805 LW |
4830 | } |
4831 | ||
c461cf8f JH |
4832 | /* |
4833 | =for apidoc sv_rvweaken | |
4834 | ||
645c22ef DM |
4835 | Weaken a reference: set the C<SvWEAKREF> flag on this RV; give the |
4836 | referred-to SV C<PERL_MAGIC_backref> magic if it hasn't already; and | |
4837 | push a back-reference to this RV onto the array of backreferences | |
4838 | associated with that magic. | |
c461cf8f JH |
4839 | |
4840 | =cut | |
4841 | */ | |
4842 | ||
810b8aa5 | 4843 | SV * |
864dbfa3 | 4844 | Perl_sv_rvweaken(pTHX_ SV *sv) |
810b8aa5 GS |
4845 | { |
4846 | SV *tsv; | |
4847 | if (!SvOK(sv)) /* let undefs pass */ | |
4848 | return sv; | |
4849 | if (!SvROK(sv)) | |
cea2e8a9 | 4850 | Perl_croak(aTHX_ "Can't weaken a nonreference"); |
810b8aa5 | 4851 | else if (SvWEAKREF(sv)) { |
810b8aa5 | 4852 | if (ckWARN(WARN_MISC)) |
9014280d | 4853 | Perl_warner(aTHX_ packWARN(WARN_MISC), "Reference is already weak"); |
810b8aa5 GS |
4854 | return sv; |
4855 | } | |
4856 | tsv = SvRV(sv); | |
4857 | sv_add_backref(tsv, sv); | |
4858 | SvWEAKREF_on(sv); | |
1c846c1f | 4859 | SvREFCNT_dec(tsv); |
810b8aa5 GS |
4860 | return sv; |
4861 | } | |
4862 | ||
645c22ef DM |
4863 | /* Give tsv backref magic if it hasn't already got it, then push a |
4864 | * back-reference to sv onto the array associated with the backref magic. | |
4865 | */ | |
4866 | ||
810b8aa5 | 4867 | STATIC void |
cea2e8a9 | 4868 | S_sv_add_backref(pTHX_ SV *tsv, SV *sv) |
810b8aa5 GS |
4869 | { |
4870 | AV *av; | |
4871 | MAGIC *mg; | |
14befaf4 | 4872 | if (SvMAGICAL(tsv) && (mg = mg_find(tsv, PERL_MAGIC_backref))) |
810b8aa5 GS |
4873 | av = (AV*)mg->mg_obj; |
4874 | else { | |
4875 | av = newAV(); | |
14befaf4 | 4876 | sv_magic(tsv, (SV*)av, PERL_MAGIC_backref, NULL, 0); |
219ef941 JH |
4877 | /* av now has a refcnt of 2, which avoids it getting freed |
4878 | * before us during global cleanup. The extra ref is removed | |
4879 | * by magic_killbackrefs() when tsv is being freed */ | |
810b8aa5 | 4880 | } |
f8cf5370 JH |
4881 | if (AvFILLp(av) >= AvMAX(av)) { |
4882 | SV **svp = AvARRAY(av); | |
4883 | I32 i = AvFILLp(av); | |
4884 | while (i >= 0) { | |
4885 | if (svp[i] == &PL_sv_undef) { | |
4886 | svp[i] = sv; /* reuse the slot */ | |
4887 | return; | |
4888 | } | |
4889 | i--; | |
4890 | } | |
4891 | av_extend(av, AvFILLp(av)+1); | |
4892 | } | |
4893 | AvARRAY(av)[++AvFILLp(av)] = sv; /* av_push() */ | |
810b8aa5 GS |
4894 | } |
4895 | ||
645c22ef DM |
4896 | /* delete a back-reference to ourselves from the backref magic associated |
4897 | * with the SV we point to. | |
4898 | */ | |
4899 | ||
1c846c1f | 4900 | STATIC void |
cea2e8a9 | 4901 | S_sv_del_backref(pTHX_ SV *sv) |
810b8aa5 GS |
4902 | { |
4903 | AV *av; | |
4904 | SV **svp; | |
4905 | I32 i; | |
4906 | SV *tsv = SvRV(sv); | |
c04a4dfe | 4907 | MAGIC *mg = NULL; |
14befaf4 | 4908 | if (!SvMAGICAL(tsv) || !(mg = mg_find(tsv, PERL_MAGIC_backref))) |
cea2e8a9 | 4909 | Perl_croak(aTHX_ "panic: del_backref"); |
810b8aa5 GS |
4910 | av = (AV *)mg->mg_obj; |
4911 | svp = AvARRAY(av); | |
4912 | i = AvFILLp(av); | |
4913 | while (i >= 0) { | |
4914 | if (svp[i] == sv) { | |
4915 | svp[i] = &PL_sv_undef; /* XXX */ | |
4916 | } | |
4917 | i--; | |
4918 | } | |
4919 | } | |
4920 | ||
954c1994 GS |
4921 | /* |
4922 | =for apidoc sv_insert | |
4923 | ||
4924 | Inserts a string at the specified offset/length within the SV. Similar to | |
4925 | the Perl substr() function. | |
4926 | ||
4927 | =cut | |
4928 | */ | |
4929 | ||
79072805 | 4930 | void |
864dbfa3 | 4931 | Perl_sv_insert(pTHX_ SV *bigstr, STRLEN offset, STRLEN len, char *little, STRLEN littlelen) |
79072805 LW |
4932 | { |
4933 | register char *big; | |
4934 | register char *mid; | |
4935 | register char *midend; | |
4936 | register char *bigend; | |
4937 | register I32 i; | |
6ff81951 | 4938 | STRLEN curlen; |
1c846c1f | 4939 | |
79072805 | 4940 | |
8990e307 | 4941 | if (!bigstr) |
cea2e8a9 | 4942 | Perl_croak(aTHX_ "Can't modify non-existent substring"); |
6ff81951 | 4943 | SvPV_force(bigstr, curlen); |
60fa28ff | 4944 | (void)SvPOK_only_UTF8(bigstr); |
6ff81951 GS |
4945 | if (offset + len > curlen) { |
4946 | SvGROW(bigstr, offset+len+1); | |
4947 | Zero(SvPVX(bigstr)+curlen, offset+len-curlen, char); | |
4948 | SvCUR_set(bigstr, offset+len); | |
4949 | } | |
79072805 | 4950 | |
69b47968 | 4951 | SvTAINT(bigstr); |
79072805 LW |
4952 | i = littlelen - len; |
4953 | if (i > 0) { /* string might grow */ | |
a0d0e21e | 4954 | big = SvGROW(bigstr, SvCUR(bigstr) + i + 1); |
79072805 LW |
4955 | mid = big + offset + len; |
4956 | midend = bigend = big + SvCUR(bigstr); | |
4957 | bigend += i; | |
4958 | *bigend = '\0'; | |
4959 | while (midend > mid) /* shove everything down */ | |
4960 | *--bigend = *--midend; | |
4961 | Move(little,big+offset,littlelen,char); | |
4962 | SvCUR(bigstr) += i; | |
4963 | SvSETMAGIC(bigstr); | |
4964 | return; | |
4965 | } | |
4966 | else if (i == 0) { | |
463ee0b2 | 4967 | Move(little,SvPVX(bigstr)+offset,len,char); |
79072805 LW |
4968 | SvSETMAGIC(bigstr); |
4969 | return; | |
4970 | } | |
4971 | ||
463ee0b2 | 4972 | big = SvPVX(bigstr); |
79072805 LW |
4973 | mid = big + offset; |
4974 | midend = mid + len; | |
4975 | bigend = big + SvCUR(bigstr); | |
4976 | ||
4977 | if (midend > bigend) | |
cea2e8a9 | 4978 | Perl_croak(aTHX_ "panic: sv_insert"); |
79072805 LW |
4979 | |
4980 | if (mid - big > bigend - midend) { /* faster to shorten from end */ | |
4981 | if (littlelen) { | |
4982 | Move(little, mid, littlelen,char); | |
4983 | mid += littlelen; | |
4984 | } | |
4985 | i = bigend - midend; | |
4986 | if (i > 0) { | |
4987 | Move(midend, mid, i,char); | |
4988 | mid += i; | |
4989 | } | |
4990 | *mid = '\0'; | |
4991 | SvCUR_set(bigstr, mid - big); | |
4992 | } | |
4993 | /*SUPPRESS 560*/ | |
155aba94 | 4994 | else if ((i = mid - big)) { /* faster from front */ |
79072805 LW |
4995 | midend -= littlelen; |
4996 | mid = midend; | |
4997 | sv_chop(bigstr,midend-i); | |
4998 | big += i; | |
4999 | while (i--) | |
5000 | *--midend = *--big; | |
5001 | if (littlelen) | |
5002 | Move(little, mid, littlelen,char); | |
5003 | } | |
5004 | else if (littlelen) { | |
5005 | midend -= littlelen; | |
5006 | sv_chop(bigstr,midend); | |
5007 | Move(little,midend,littlelen,char); | |
5008 | } | |
5009 | else { | |
5010 | sv_chop(bigstr,midend); | |
5011 | } | |
5012 | SvSETMAGIC(bigstr); | |
5013 | } | |
5014 | ||
c461cf8f JH |
5015 | /* |
5016 | =for apidoc sv_replace | |
5017 | ||
5018 | Make the first argument a copy of the second, then delete the original. | |
645c22ef DM |
5019 | The target SV physically takes over ownership of the body of the source SV |
5020 | and inherits its flags; however, the target keeps any magic it owns, | |
5021 | and any magic in the source is discarded. | |
ff276b08 | 5022 | Note that this is a rather specialist SV copying operation; most of the |
645c22ef | 5023 | time you'll want to use C<sv_setsv> or one of its many macro front-ends. |
c461cf8f JH |
5024 | |
5025 | =cut | |
5026 | */ | |
79072805 LW |
5027 | |
5028 | void | |
864dbfa3 | 5029 | Perl_sv_replace(pTHX_ register SV *sv, register SV *nsv) |
79072805 LW |
5030 | { |
5031 | U32 refcnt = SvREFCNT(sv); | |
2213622d | 5032 | SV_CHECK_THINKFIRST(sv); |
0453d815 | 5033 | if (SvREFCNT(nsv) != 1 && ckWARN_d(WARN_INTERNAL)) |
9014280d | 5034 | Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "Reference miscount in sv_replace()"); |
93a17b20 | 5035 | if (SvMAGICAL(sv)) { |
a0d0e21e LW |
5036 | if (SvMAGICAL(nsv)) |
5037 | mg_free(nsv); | |
5038 | else | |
5039 | sv_upgrade(nsv, SVt_PVMG); | |
93a17b20 | 5040 | SvMAGIC(nsv) = SvMAGIC(sv); |
a0d0e21e | 5041 | SvFLAGS(nsv) |= SvMAGICAL(sv); |
93a17b20 LW |
5042 | SvMAGICAL_off(sv); |
5043 | SvMAGIC(sv) = 0; | |
5044 | } | |
79072805 LW |
5045 | SvREFCNT(sv) = 0; |
5046 | sv_clear(sv); | |
477f5d66 | 5047 | assert(!SvREFCNT(sv)); |
79072805 LW |
5048 | StructCopy(nsv,sv,SV); |
5049 | SvREFCNT(sv) = refcnt; | |
1edc1566 | 5050 | SvFLAGS(nsv) |= SVTYPEMASK; /* Mark as freed */ |
007262b0 | 5051 | SvREFCNT(nsv) = 0; |
463ee0b2 | 5052 | del_SV(nsv); |
79072805 LW |
5053 | } |
5054 | ||
c461cf8f JH |
5055 | /* |
5056 | =for apidoc sv_clear | |
5057 | ||
645c22ef DM |
5058 | Clear an SV: call any destructors, free up any memory used by the body, |
5059 | and free the body itself. The SV's head is I<not> freed, although | |
5060 | its type is set to all 1's so that it won't inadvertently be assumed | |
5061 | to be live during global destruction etc. | |
5062 | This function should only be called when REFCNT is zero. Most of the time | |
5063 | you'll want to call C<sv_free()> (or its macro wrapper C<SvREFCNT_dec>) | |
5064 | instead. | |
c461cf8f JH |
5065 | |
5066 | =cut | |
5067 | */ | |
5068 | ||
79072805 | 5069 | void |
864dbfa3 | 5070 | Perl_sv_clear(pTHX_ register SV *sv) |
79072805 | 5071 | { |
ec12f114 | 5072 | HV* stash; |
79072805 LW |
5073 | assert(sv); |
5074 | assert(SvREFCNT(sv) == 0); | |
5075 | ||
ed6116ce | 5076 | if (SvOBJECT(sv)) { |
3280af22 | 5077 | if (PL_defstash) { /* Still have a symbol table? */ |
39644a26 | 5078 | dSP; |
32251b26 | 5079 | CV* destructor; |
a0d0e21e | 5080 | |
3e5ba712 | 5081 | |
8ebc5c01 | 5082 | |
d460ef45 | 5083 | do { |
4e8e7886 | 5084 | stash = SvSTASH(sv); |
32251b26 | 5085 | destructor = StashHANDLER(stash,DESTROY); |
4e8e7886 | 5086 | if (destructor) { |
3e5ba712 JH |
5087 | SV* tmpref = newRV(sv); |
5088 | SvREADONLY_on(tmpref); /* DESTROY() could be naughty */ | |
4e8e7886 | 5089 | ENTER; |
e788e7d3 | 5090 | PUSHSTACKi(PERLSI_DESTROY); |
4e8e7886 GS |
5091 | EXTEND(SP, 2); |
5092 | PUSHMARK(SP); | |
3e5ba712 | 5093 | PUSHs(tmpref); |
4e8e7886 | 5094 | PUTBACK; |
44389ee9 | 5095 | call_sv((SV*)destructor, G_DISCARD|G_EVAL|G_KEEPERR|G_VOID); |
3e5ba712 JH |
5096 | |
5097 | ||
d3acc0f7 | 5098 | POPSTACK; |
3095d977 | 5099 | SPAGAIN; |
4e8e7886 | 5100 | LEAVE; |
3e5ba712 JH |
5101 | if(SvREFCNT(tmpref) < 2) { |
5102 | /* tmpref is not kept alive! */ | |
5103 | SvREFCNT(sv)--; | |
5104 | SvRV(tmpref) = 0; | |
5105 | SvROK_off(tmpref); | |
5106 | } | |
5107 | SvREFCNT_dec(tmpref); | |
4e8e7886 GS |
5108 | } |
5109 | } while (SvOBJECT(sv) && SvSTASH(sv) != stash); | |
8ebc5c01 | 5110 | |
6f44e0a4 JP |
5111 | |
5112 | if (SvREFCNT(sv)) { | |
5113 | if (PL_in_clean_objs) | |
cea2e8a9 | 5114 | Perl_croak(aTHX_ "DESTROY created new reference to dead object '%s'", |
6f44e0a4 JP |
5115 | HvNAME(stash)); |
5116 | /* DESTROY gave object new lease on life */ | |
5117 | return; | |
5118 | } | |
a0d0e21e | 5119 | } |
4e8e7886 | 5120 | |
a0d0e21e | 5121 | if (SvOBJECT(sv)) { |
4e8e7886 | 5122 | SvREFCNT_dec(SvSTASH(sv)); /* possibly of changed persuasion */ |
a0d0e21e LW |
5123 | SvOBJECT_off(sv); /* Curse the object. */ |
5124 | if (SvTYPE(sv) != SVt_PVIO) | |
3280af22 | 5125 | --PL_sv_objcount; /* XXX Might want something more general */ |
a0d0e21e | 5126 | } |
463ee0b2 | 5127 | } |
524189f1 JH |
5128 | if (SvTYPE(sv) >= SVt_PVMG) { |
5129 | if (SvMAGIC(sv)) | |
5130 | mg_free(sv); | |
5131 | if (SvFLAGS(sv) & SVpad_TYPED) | |
5132 | SvREFCNT_dec(SvSTASH(sv)); | |
5133 | } | |
ec12f114 | 5134 | stash = NULL; |
79072805 | 5135 | switch (SvTYPE(sv)) { |
8990e307 | 5136 | case SVt_PVIO: |
df0bd2f4 GS |
5137 | if (IoIFP(sv) && |
5138 | IoIFP(sv) != PerlIO_stdin() && | |
5f05dabc | 5139 | IoIFP(sv) != PerlIO_stdout() && |
5140 | IoIFP(sv) != PerlIO_stderr()) | |
93578b34 | 5141 | { |
f2b5be74 | 5142 | io_close((IO*)sv, FALSE); |
93578b34 | 5143 | } |
1d7c1841 | 5144 | if (IoDIRP(sv) && !(IoFLAGS(sv) & IOf_FAKE_DIRP)) |
1236053a | 5145 | PerlDir_close(IoDIRP(sv)); |
1d7c1841 | 5146 | IoDIRP(sv) = (DIR*)NULL; |
8990e307 LW |
5147 | Safefree(IoTOP_NAME(sv)); |
5148 | Safefree(IoFMT_NAME(sv)); | |
5149 | Safefree(IoBOTTOM_NAME(sv)); | |
a0d0e21e | 5150 | /* FALL THROUGH */ |
79072805 | 5151 | case SVt_PVBM: |
a0d0e21e | 5152 | goto freescalar; |
79072805 | 5153 | case SVt_PVCV: |
748a9306 | 5154 | case SVt_PVFM: |
85e6fe83 | 5155 | cv_undef((CV*)sv); |
a0d0e21e | 5156 | goto freescalar; |
79072805 | 5157 | case SVt_PVHV: |
85e6fe83 | 5158 | hv_undef((HV*)sv); |
a0d0e21e | 5159 | break; |
79072805 | 5160 | case SVt_PVAV: |
85e6fe83 | 5161 | av_undef((AV*)sv); |
a0d0e21e | 5162 | break; |
02270b4e | 5163 | case SVt_PVLV: |
73c86719 JH |
5164 | if (LvTYPE(sv) == 'T') { /* for tie: return HE to pool */ |
5165 | SvREFCNT_dec(HeKEY_sv((HE*)LvTARG(sv))); | |
5166 | HeNEXT((HE*)LvTARG(sv)) = PL_hv_fetch_ent_mh; | |
5167 | PL_hv_fetch_ent_mh = (HE*)LvTARG(sv); | |
5168 | } | |
5169 | else if (LvTYPE(sv) != 't') /* unless tie: unrefcnted fake SV** */ | |
5170 | SvREFCNT_dec(LvTARG(sv)); | |
02270b4e | 5171 | goto freescalar; |
a0d0e21e | 5172 | case SVt_PVGV: |
1edc1566 | 5173 | gp_free((GV*)sv); |
a0d0e21e | 5174 | Safefree(GvNAME(sv)); |
ec12f114 JPC |
5175 | /* cannot decrease stash refcount yet, as we might recursively delete |
5176 | ourselves when the refcnt drops to zero. Delay SvREFCNT_dec | |
5177 | of stash until current sv is completely gone. | |
5178 | -- JohnPC, 27 Mar 1998 */ | |
5179 | stash = GvSTASH(sv); | |
a0d0e21e | 5180 | /* FALL THROUGH */ |
79072805 | 5181 | case SVt_PVMG: |
79072805 LW |
5182 | case SVt_PVNV: |
5183 | case SVt_PVIV: | |
a0d0e21e LW |
5184 | freescalar: |
5185 | (void)SvOOK_off(sv); | |
79072805 LW |
5186 | /* FALL THROUGH */ |
5187 | case SVt_PV: | |
a0d0e21e | 5188 | case SVt_RV: |
810b8aa5 GS |
5189 | if (SvROK(sv)) { |
5190 | if (SvWEAKREF(sv)) | |
5191 | sv_del_backref(sv); | |
5192 | else | |
5193 | SvREFCNT_dec(SvRV(sv)); | |
5194 | } | |
1edc1566 | 5195 | else if (SvPVX(sv) && SvLEN(sv)) |
463ee0b2 | 5196 | Safefree(SvPVX(sv)); |
1c846c1f | 5197 | else if (SvPVX(sv) && SvREADONLY(sv) && SvFAKE(sv)) { |
25716404 GS |
5198 | unsharepvn(SvPVX(sv), |
5199 | SvUTF8(sv) ? -(I32)SvCUR(sv) : SvCUR(sv), | |
5200 | SvUVX(sv)); | |
1c846c1f NIS |
5201 | SvFAKE_off(sv); |
5202 | } | |
79072805 | 5203 | break; |
a0d0e21e | 5204 | /* |
79072805 | 5205 | case SVt_NV: |
79072805 | 5206 | case SVt_IV: |
79072805 LW |
5207 | case SVt_NULL: |
5208 | break; | |
a0d0e21e | 5209 | */ |
79072805 LW |
5210 | } |
5211 | ||
5212 | switch (SvTYPE(sv)) { | |
5213 | case SVt_NULL: | |
5214 | break; | |
79072805 LW |
5215 | case SVt_IV: |
5216 | del_XIV(SvANY(sv)); | |
5217 | break; | |
5218 | case SVt_NV: | |
5219 | del_XNV(SvANY(sv)); | |
5220 | break; | |
ed6116ce LW |
5221 | case SVt_RV: |
5222 | del_XRV(SvANY(sv)); | |
5223 | break; | |
79072805 LW |
5224 | case SVt_PV: |
5225 | del_XPV(SvANY(sv)); | |
5226 | break; | |
5227 | case SVt_PVIV: | |
5228 | del_XPVIV(SvANY(sv)); | |
5229 | break; | |
5230 | case SVt_PVNV: | |
5231 | del_XPVNV(SvANY(sv)); | |
5232 | break; | |
5233 | case SVt_PVMG: | |
5234 | del_XPVMG(SvANY(sv)); | |
5235 | break; | |
5236 | case SVt_PVLV: | |
5237 | del_XPVLV(SvANY(sv)); | |
5238 | break; | |
5239 | case SVt_PVAV: | |
5240 | del_XPVAV(SvANY(sv)); | |
5241 | break; | |
5242 | case SVt_PVHV: | |
5243 | del_XPVHV(SvANY(sv)); | |
5244 | break; | |
5245 | case SVt_PVCV: | |
5246 | del_XPVCV(SvANY(sv)); | |
5247 | break; | |
5248 | case SVt_PVGV: | |
5249 | del_XPVGV(SvANY(sv)); | |
ec12f114 JPC |
5250 | /* code duplication for increased performance. */ |
5251 | SvFLAGS(sv) &= SVf_BREAK; | |
5252 | SvFLAGS(sv) |= SVTYPEMASK; | |
5253 | /* decrease refcount of the stash that owns this GV, if any */ | |
5254 | if (stash) | |
5255 | SvREFCNT_dec(stash); | |
5256 | return; /* not break, SvFLAGS reset already happened */ | |
79072805 LW |
5257 | case SVt_PVBM: |
5258 | del_XPVBM(SvANY(sv)); | |
5259 | break; | |
5260 | case SVt_PVFM: | |
5261 | del_XPVFM(SvANY(sv)); | |
5262 | break; | |
8990e307 LW |
5263 | case SVt_PVIO: |
5264 | del_XPVIO(SvANY(sv)); | |
5265 | break; | |
79072805 | 5266 | } |
a0d0e21e | 5267 | SvFLAGS(sv) &= SVf_BREAK; |
8990e307 | 5268 | SvFLAGS(sv) |= SVTYPEMASK; |
79072805 LW |
5269 | } |
5270 | ||
645c22ef DM |
5271 | /* |
5272 | =for apidoc sv_newref | |
5273 | ||
5274 | Increment an SV's reference count. Use the C<SvREFCNT_inc()> wrapper | |
5275 | instead. | |
5276 | ||
5277 | =cut | |
5278 | */ | |
5279 | ||
79072805 | 5280 | SV * |
864dbfa3 | 5281 | Perl_sv_newref(pTHX_ SV *sv) |
79072805 | 5282 | { |
463ee0b2 | 5283 | if (sv) |
dce16143 | 5284 | ATOMIC_INC(SvREFCNT(sv)); |
79072805 LW |
5285 | return sv; |
5286 | } | |
5287 | ||
c461cf8f JH |
5288 | /* |
5289 | =for apidoc sv_free | |
5290 | ||
645c22ef DM |
5291 | Decrement an SV's reference count, and if it drops to zero, call |
5292 | C<sv_clear> to invoke destructors and free up any memory used by | |
5293 | the body; finally, deallocate the SV's head itself. | |
5294 | Normally called via a wrapper macro C<SvREFCNT_dec>. | |
c461cf8f JH |
5295 | |
5296 | =cut | |
5297 | */ | |
5298 | ||
79072805 | 5299 | void |
864dbfa3 | 5300 | Perl_sv_free(pTHX_ SV *sv) |
79072805 | 5301 | { |
dce16143 MB |
5302 | int refcount_is_zero; |
5303 | ||
79072805 LW |
5304 | if (!sv) |
5305 | return; | |
a0d0e21e LW |
5306 | if (SvREFCNT(sv) == 0) { |
5307 | if (SvFLAGS(sv) & SVf_BREAK) | |
645c22ef DM |
5308 | /* this SV's refcnt has been artificially decremented to |
5309 | * trigger cleanup */ | |
a0d0e21e | 5310 | return; |
3280af22 | 5311 | if (PL_in_clean_all) /* All is fair */ |
1edc1566 | 5312 | return; |
d689ffdd JP |
5313 | if (SvREADONLY(sv) && SvIMMORTAL(sv)) { |
5314 | /* make sure SvREFCNT(sv)==0 happens very seldom */ | |
5315 | SvREFCNT(sv) = (~(U32)0)/2; | |
5316 | return; | |
5317 | } | |
0453d815 | 5318 | if (ckWARN_d(WARN_INTERNAL)) |
9014280d | 5319 | Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "Attempt to free unreferenced scalar"); |
79072805 LW |
5320 | return; |
5321 | } | |
dce16143 | 5322 | ATOMIC_DEC_AND_TEST(refcount_is_zero, SvREFCNT(sv)); |
b881518d | 5323 | if (!refcount_is_zero) |
8990e307 | 5324 | return; |
463ee0b2 LW |
5325 | #ifdef DEBUGGING |
5326 | if (SvTEMP(sv)) { | |
0453d815 | 5327 | if (ckWARN_d(WARN_DEBUGGING)) |
9014280d | 5328 | Perl_warner(aTHX_ packWARN(WARN_DEBUGGING), |
1d7c1841 GS |
5329 | "Attempt to free temp prematurely: SV 0x%"UVxf, |
5330 | PTR2UV(sv)); | |
79072805 | 5331 | return; |
79072805 | 5332 | } |
463ee0b2 | 5333 | #endif |
d689ffdd JP |
5334 | if (SvREADONLY(sv) && SvIMMORTAL(sv)) { |
5335 | /* make sure SvREFCNT(sv)==0 happens very seldom */ | |
5336 | SvREFCNT(sv) = (~(U32)0)/2; | |
5337 | return; | |
5338 | } | |
79072805 | 5339 | sv_clear(sv); |
477f5d66 CS |
5340 | if (! SvREFCNT(sv)) |
5341 | del_SV(sv); | |
79072805 LW |
5342 | } |
5343 | ||
954c1994 GS |
5344 | /* |
5345 | =for apidoc sv_len | |
5346 | ||
645c22ef DM |
5347 | Returns the length of the string in the SV. Handles magic and type |
5348 | coercion. See also C<SvCUR>, which gives raw access to the xpv_cur slot. | |
954c1994 GS |
5349 | |
5350 | =cut | |
5351 | */ | |
5352 | ||
79072805 | 5353 | STRLEN |
864dbfa3 | 5354 | Perl_sv_len(pTHX_ register SV *sv) |
79072805 | 5355 | { |
463ee0b2 | 5356 | STRLEN len; |
79072805 LW |
5357 | |
5358 | if (!sv) | |
5359 | return 0; | |
5360 | ||
8990e307 | 5361 | if (SvGMAGICAL(sv)) |
565764a8 | 5362 | len = mg_length(sv); |
8990e307 | 5363 | else |
497b47a8 | 5364 | (void)SvPV(sv, len); |
463ee0b2 | 5365 | return len; |
79072805 LW |
5366 | } |
5367 | ||
c461cf8f JH |
5368 | /* |
5369 | =for apidoc sv_len_utf8 | |
5370 | ||
5371 | Returns the number of characters in the string in an SV, counting wide | |
cd458e05 | 5372 | UTF-8 bytes as a single character. Handles magic and type coercion. |
c461cf8f JH |
5373 | |
5374 | =cut | |
5375 | */ | |
5376 | ||
323eb6b5 JH |
5377 | /* |
5378 | * The length is cached in PERL_UTF8_magic, in the mg_len field. Also the | |
5379 | * mg_ptr is used, by sv_pos_u2b(), see the comments of S_utf8_mg_pos_init(). | |
5380 | * (Note that the mg_len is not the length of the mg_ptr field.) | |
5381 | * | |
5382 | */ | |
5383 | ||
a0ed51b3 | 5384 | STRLEN |
864dbfa3 | 5385 | Perl_sv_len_utf8(pTHX_ register SV *sv) |
a0ed51b3 | 5386 | { |
a0ed51b3 LW |
5387 | if (!sv) |
5388 | return 0; | |
5389 | ||
a0ed51b3 | 5390 | if (SvGMAGICAL(sv)) |
b76347f2 | 5391 | return mg_length(sv); |
a0ed51b3 | 5392 | else |
b76347f2 | 5393 | { |
323eb6b5 | 5394 | STRLEN len, ulen; |
b76347f2 | 5395 | U8 *s = (U8*)SvPV(sv, len); |
323eb6b5 JH |
5396 | MAGIC *mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_utf8) : 0; |
5397 | ||
d6218095 | 5398 | if (mg && mg->mg_len != -1 && (mg->mg_len > 0 || len == 0)) { |
323eb6b5 | 5399 | ulen = mg->mg_len; |
d6218095 JH |
5400 | #ifdef PERL_UTF8_CACHE_ASSERT |
5401 | assert(ulen == Perl_utf8_length(aTHX_ s, s + len)); | |
5402 | #endif | |
5403 | } | |
323eb6b5 JH |
5404 | else { |
5405 | ulen = Perl_utf8_length(aTHX_ s, s + len); | |
5406 | if (!mg && !SvREADONLY(sv)) { | |
5407 | sv_magic(sv, 0, PERL_MAGIC_utf8, 0, 0); | |
5408 | mg = mg_find(sv, PERL_MAGIC_utf8); | |
5409 | assert(mg); | |
5410 | } | |
5411 | if (mg) | |
5412 | mg->mg_len = ulen; | |
5413 | } | |
5414 | return ulen; | |
5415 | } | |
5416 | } | |
5417 | ||
5418 | /* S_utf8_mg_pos_init() is used to initialize the mg_ptr field of | |
5419 | * a PERL_UTF8_magic. The mg_ptr is used to store the mapping | |
5420 | * between UTF-8 and byte offsets. There are two (substr offset and substr | |
5421 | * length, the i offset, PERL_MAGIC_UTF8_CACHESIZE) times two (UTF-8 offset | |
5422 | * and byte offset) cache positions. | |
5423 | * | |
5424 | * The mg_len field is used by sv_len_utf8(), see its comments. | |
5425 | * Note that the mg_len is not the length of the mg_ptr field. | |
5426 | * | |
5427 | */ | |
5428 | STATIC bool | |
91eb8809 | 5429 | S_utf8_mg_pos_init(pTHX_ SV *sv, MAGIC **mgp, STRLEN **cachep, I32 i, I32 *offsetp, U8 *s, U8 *start) |
323eb6b5 JH |
5430 | { |
5431 | bool found = FALSE; | |
5432 | ||
5433 | if (SvMAGICAL(sv) && !SvREADONLY(sv)) { | |
5434 | if (!*mgp) { | |
5435 | sv_magic(sv, 0, PERL_MAGIC_utf8, 0, 0); | |
5436 | *mgp = mg_find(sv, PERL_MAGIC_utf8); | |
5437 | } | |
5438 | assert(*mgp); | |
b76347f2 | 5439 | |
323eb6b5 JH |
5440 | if ((*mgp)->mg_ptr) |
5441 | *cachep = (STRLEN *) (*mgp)->mg_ptr; | |
5442 | else { | |
5443 | Newz(0, *cachep, PERL_MAGIC_UTF8_CACHESIZE * 2, STRLEN); | |
5444 | (*mgp)->mg_ptr = (char *) *cachep; | |
5445 | } | |
5446 | assert(*cachep); | |
5447 | ||
5448 | (*cachep)[i] = *offsetp; | |
5449 | (*cachep)[i+1] = s - start; | |
5450 | found = TRUE; | |
5451 | } | |
5452 | ||
5453 | return found; | |
5454 | } | |
5455 | ||
5456 | /* | |
5457 | * S_utf8_mg_pos() is used to query and update mg_ptr field of | |
5458 | * a PERL_UTF8_magic. The mg_ptr is used to store the mapping | |
5459 | * between UTF-8 and byte offsets. See also the comments of | |
5460 | * S_utf8_mg_pos_init(). | |
5461 | * | |
5462 | */ | |
5463 | STATIC bool | |
91eb8809 | 5464 | S_utf8_mg_pos(pTHX_ SV *sv, MAGIC **mgp, STRLEN **cachep, I32 i, I32 *offsetp, I32 uoff, U8 **sp, U8 *start, U8 *send) |
323eb6b5 JH |
5465 | { |
5466 | bool found = FALSE; | |
5467 | ||
5468 | if (SvMAGICAL(sv) && !SvREADONLY(sv)) { | |
5469 | if (!*mgp) | |
5470 | *mgp = mg_find(sv, PERL_MAGIC_utf8); | |
5471 | if (*mgp && (*mgp)->mg_ptr) { | |
5472 | *cachep = (STRLEN *) (*mgp)->mg_ptr; | |
d6218095 | 5473 | ASSERT_UTF8_CACHE(*cachep); |
91eb8809 | 5474 | if ((*cachep)[i] == (STRLEN)uoff) /* An exact match. */ |
323eb6b5 JH |
5475 | found = TRUE; |
5476 | else { /* We will skip to the right spot. */ | |
5477 | STRLEN forw = 0; | |
5478 | STRLEN backw = 0; | |
5479 | U8* p = NULL; | |
5480 | ||
5481 | /* The assumption is that going backward is half | |
5482 | * the speed of going forward (that's where the | |
5483 | * 2 * backw in the below comes from). (The real | |
5484 | * figure of course depends on the UTF-8 data.) */ | |
5485 | ||
91eb8809 | 5486 | if ((*cachep)[i] > (STRLEN)uoff) { |
323eb6b5 | 5487 | forw = uoff; |
91eb8809 | 5488 | backw = (*cachep)[i] - (STRLEN)uoff; |
323eb6b5 JH |
5489 | |
5490 | if (forw < 2 * backw) | |
5491 | p = start; | |
5492 | else | |
5493 | p = start + (*cachep)[i+1]; | |
5494 | } | |
5495 | /* Try this only for the substr offset (i == 0), | |
5496 | * not for the substr length (i == 2). */ | |
5497 | else if (i == 0) { /* (*cachep)[i] < uoff */ | |
5498 | STRLEN ulen = sv_len_utf8(sv); | |
5499 | ||
91eb8809 JH |
5500 | if ((STRLEN)uoff < ulen) { |
5501 | forw = (STRLEN)uoff - (*cachep)[i]; | |
5502 | backw = ulen - (STRLEN)uoff; | |
323eb6b5 JH |
5503 | |
5504 | if (forw < 2 * backw) | |
5505 | p = start + (*cachep)[i+1]; | |
5506 | else | |
5507 | p = send; | |
5508 | } | |
5509 | ||
5510 | /* If the string is not long enough for uoff, | |
5511 | * we could extend it, but not at this low a level. */ | |
5512 | } | |
5513 | ||
5514 | if (p) { | |
5515 | if (forw < 2 * backw) { | |
5516 | while (forw--) | |
5517 | p += UTF8SKIP(p); | |
5518 | } | |
5519 | else { | |
5520 | while (backw--) { | |
5521 | p--; | |
5522 | while (UTF8_IS_CONTINUATION(*p)) | |
5523 | p--; | |
5524 | } | |
5525 | } | |
5526 | ||
5527 | /* Update the cache. */ | |
91eb8809 | 5528 | (*cachep)[i] = (STRLEN)uoff; |
323eb6b5 JH |
5529 | (*cachep)[i+1] = p - start; |
5530 | ||
5531 | found = TRUE; | |
5532 | } | |
5533 | } | |
5534 | if (found) { /* Setup the return values. */ | |
5535 | *offsetp = (*cachep)[i+1]; | |
5536 | *sp = start + *offsetp; | |
5537 | if (*sp >= send) { | |
5538 | *sp = send; | |
5539 | *offsetp = send - start; | |
5540 | } | |
5541 | else if (*sp < start) { | |
5542 | *sp = start; | |
5543 | *offsetp = 0; | |
5544 | } | |
5545 | } | |
d6218095 JH |
5546 | } |
5547 | #ifdef PERL_UTF8_CACHE_ASSERT | |
5548 | if (found) { | |
5549 | U8 *s = start; | |
5550 | I32 n = uoff; | |
5551 | ||
5552 | while (n-- && s < send) | |
5553 | s += UTF8SKIP(s); | |
5554 | ||
5555 | if (i == 0) { | |
5556 | assert(*offsetp == s - start); | |
5557 | assert((*cachep)[0] == (STRLEN)uoff); | |
5558 | assert((*cachep)[1] == *offsetp); | |
5559 | } | |
5560 | ASSERT_UTF8_CACHE(*cachep); | |
5561 | } | |
5562 | #endif | |
a0ed51b3 | 5563 | } |
323eb6b5 JH |
5564 | |
5565 | return found; | |
a0ed51b3 LW |
5566 | } |
5567 | ||
645c22ef DM |
5568 | /* |
5569 | =for apidoc sv_pos_u2b | |
5570 | ||
cd458e05 | 5571 | Converts the value pointed to by offsetp from a count of UTF-8 chars from |
645c22ef DM |
5572 | the start of the string, to a count of the equivalent number of bytes; if |
5573 | lenp is non-zero, it does the same to lenp, but this time starting from | |
5574 | the offset, rather than from the start of the string. Handles magic and | |
5575 | type coercion. | |
5576 | ||
5577 | =cut | |
5578 | */ | |
5579 | ||
323eb6b5 JH |
5580 | /* |
5581 | * sv_pos_u2b() uses, like sv_pos_b2u(), the mg_ptr of the potential | |
5582 | * PERL_UTF8_magic of the sv to store the mapping between UTF-8 and | |
5583 | * byte offsets. See also the comments of S_utf8_mg_pos(). | |
5584 | * | |
5585 | */ | |
5586 | ||
a0ed51b3 | 5587 | void |
864dbfa3 | 5588 | Perl_sv_pos_u2b(pTHX_ register SV *sv, I32* offsetp, I32* lenp) |
a0ed51b3 | 5589 | { |
dfe13c55 GS |
5590 | U8 *start; |
5591 | U8 *s; | |
a0ed51b3 | 5592 | STRLEN len; |
323eb6b5 JH |
5593 | STRLEN *cache = 0; |
5594 | STRLEN boffset = 0; | |
a0ed51b3 LW |
5595 | |
5596 | if (!sv) | |
5597 | return; | |
5598 | ||
dfe13c55 | 5599 | start = s = (U8*)SvPV(sv, len); |
323eb6b5 JH |
5600 | if (len) { |
5601 | I32 uoffset = *offsetp; | |
5602 | U8 *send = s + len; | |
5603 | MAGIC *mg = 0; | |
5604 | bool found = FALSE; | |
5605 | ||
5eed253f | 5606 | if (utf8_mg_pos(sv, &mg, &cache, 0, offsetp, *offsetp, &s, start, send)) |
323eb6b5 JH |
5607 | found = TRUE; |
5608 | if (!found && uoffset > 0) { | |
5609 | while (s < send && uoffset--) | |
5610 | s += UTF8SKIP(s); | |
5611 | if (s >= send) | |
5612 | s = send; | |
5eed253f | 5613 | if (utf8_mg_pos_init(sv, &mg, &cache, 0, offsetp, s, start)) |
323eb6b5 JH |
5614 | boffset = cache[1]; |
5615 | *offsetp = s - start; | |
5616 | } | |
5617 | if (lenp) { | |
5618 | found = FALSE; | |
5619 | start = s; | |
5eed253f | 5620 | if (utf8_mg_pos(sv, &mg, &cache, 2, lenp, *lenp + *offsetp, &s, start, send)) { |
323eb6b5 JH |
5621 | *lenp -= boffset; |
5622 | found = TRUE; | |
5623 | } | |
5624 | if (!found && *lenp > 0) { | |
5625 | I32 ulen = *lenp; | |
5626 | if (ulen > 0) | |
5627 | while (s < send && ulen--) | |
5628 | s += UTF8SKIP(s); | |
5629 | if (s >= send) | |
5630 | s = send; | |
5eed253f | 5631 | if (utf8_mg_pos_init(sv, &mg, &cache, 2, lenp, s, start)) |
323eb6b5 JH |
5632 | cache[2] += *offsetp; |
5633 | } | |
5634 | *lenp = s - start; | |
5635 | } | |
d6218095 | 5636 | ASSERT_UTF8_CACHE(cache); |
323eb6b5 JH |
5637 | } |
5638 | else { | |
5639 | *offsetp = 0; | |
5640 | if (lenp) | |
5641 | *lenp = 0; | |
a0ed51b3 | 5642 | } |
d6218095 | 5643 | |
a0ed51b3 LW |
5644 | return; |
5645 | } | |
5646 | ||
645c22ef DM |
5647 | /* |
5648 | =for apidoc sv_pos_b2u | |
5649 | ||
5650 | Converts the value pointed to by offsetp from a count of bytes from the | |
cd458e05 | 5651 | start of the string, to a count of the equivalent number of UTF-8 chars. |
645c22ef DM |
5652 | Handles magic and type coercion. |
5653 | ||
5654 | =cut | |
5655 | */ | |
5656 | ||
323eb6b5 JH |
5657 | /* |
5658 | * sv_pos_b2u() uses, like sv_pos_u2b(), the mg_ptr of the potential | |
5659 | * PERL_UTF8_magic of the sv to store the mapping between UTF-8 and | |
5660 | * byte offsets. See also the comments of S_utf8_mg_pos(). | |
5661 | * | |
5662 | */ | |
5663 | ||
a0ed51b3 | 5664 | void |
323eb6b5 | 5665 | Perl_sv_pos_b2u(pTHX_ register SV* sv, I32* offsetp) |
a0ed51b3 | 5666 | { |
323eb6b5 | 5667 | U8* s; |
a0ed51b3 LW |
5668 | STRLEN len; |
5669 | ||
5670 | if (!sv) | |
5671 | return; | |
5672 | ||
dfe13c55 | 5673 | s = (U8*)SvPV(sv, len); |
eb160463 | 5674 | if ((I32)len < *offsetp) |
a0dbb045 | 5675 | Perl_croak(aTHX_ "panic: sv_pos_b2u: bad byte offset"); |
323eb6b5 JH |
5676 | else { |
5677 | U8* send = s + *offsetp; | |
5678 | MAGIC* mg = NULL; | |
5679 | STRLEN *cache = NULL; | |
5680 | ||
5681 | len = 0; | |
5682 | ||
5683 | if (SvMAGICAL(sv) && !SvREADONLY(sv)) { | |
5684 | mg = mg_find(sv, PERL_MAGIC_utf8); | |
5685 | if (mg && mg->mg_ptr) { | |
5686 | cache = (STRLEN *) mg->mg_ptr; | |
c43a4d73 | 5687 | if (cache[1] == (STRLEN)*offsetp) { |
323eb6b5 JH |
5688 | /* An exact match. */ |
5689 | *offsetp = cache[0]; | |
5690 | ||
5691 | return; | |
5692 | } | |
c43a4d73 | 5693 | else if (cache[1] < (STRLEN)*offsetp) { |
323eb6b5 JH |
5694 | /* We already know part of the way. */ |
5695 | len = cache[0]; | |
5696 | s += cache[1]; | |
5697 | /* Let the below loop do the rest. */ | |
5698 | } | |
5699 | else { /* cache[1] > *offsetp */ | |
5700 | /* We already know all of the way, now we may | |
5701 | * be able to walk back. The same assumption | |
5702 | * is made as in S_utf8_mg_pos(), namely that | |
5703 | * walking backward is twice slower than | |
5704 | * walking forward. */ | |
5705 | STRLEN forw = *offsetp; | |
5706 | STRLEN backw = cache[1] - *offsetp; | |
5707 | ||
5708 | if (!(forw < 2 * backw)) { | |
5709 | U8 *p = s + cache[1]; | |
5710 | STRLEN ubackw = 0; | |
5711 | ||
338501c1 JH |
5712 | cache[1] -= backw; |
5713 | ||
323eb6b5 | 5714 | while (backw--) { |
b9219079 JH |
5715 | p--; |
5716 | while (UTF8_IS_CONTINUATION(*p)) { | |
5717 | p--; | |
5718 | backw--; | |
5719 | } | |
5720 | ubackw++; | |
323eb6b5 | 5721 | } |
b9219079 JH |
5722 | |
5723 | cache[0] -= ubackw; | |
5724 | *offsetp = cache[0]; | |
5725 | return; | |
5726 | } | |
5727 | } | |
5728 | } | |
d6218095 | 5729 | ASSERT_UTF8_CACHE(cache); |
323eb6b5 JH |
5730 | } |
5731 | ||
5732 | while (s < send) { | |
5733 | STRLEN n = 1; | |
5734 | ||
5735 | /* Call utf8n_to_uvchr() to validate the sequence | |
5736 | * (unless a simple non-UTF character) */ | |
5737 | if (!UTF8_IS_INVARIANT(*s)) | |
5738 | utf8n_to_uvchr(s, UTF8SKIP(s), &n, 0); | |
5739 | if (n > 0) { | |
5740 | s += n; | |
5741 | len++; | |
5742 | } | |
5743 | else | |
5744 | break; | |
5745 | } | |
5746 | ||
5747 | if (!SvREADONLY(sv)) { | |
5748 | if (!mg) { | |
5749 | sv_magic(sv, 0, PERL_MAGIC_utf8, 0, 0); | |
5750 | mg = mg_find(sv, PERL_MAGIC_utf8); | |
5751 | } | |
5752 | assert(mg); | |
5753 | ||
5754 | if (!mg->mg_ptr) { | |
5755 | Newz(0, cache, PERL_MAGIC_UTF8_CACHESIZE * 2, STRLEN); | |
5756 | mg->mg_ptr = (char *) cache; | |
5757 | } | |
5758 | assert(cache); | |
5759 | ||
5760 | cache[0] = len; | |
5761 | cache[1] = *offsetp; | |
5762 | } | |
5763 | ||
5764 | *offsetp = len; | |
a0ed51b3 | 5765 | } |
323eb6b5 | 5766 | |
a0ed51b3 LW |
5767 | return; |
5768 | } | |
5769 | ||
954c1994 GS |
5770 | /* |
5771 | =for apidoc sv_eq | |
5772 | ||
5773 | Returns a boolean indicating whether the strings in the two SVs are | |
645c22ef DM |
5774 | identical. Is UTF-8 and 'use bytes' aware, handles get magic, and will |
5775 | coerce its args to strings if necessary. | |
954c1994 GS |
5776 | |
5777 | =cut | |
5778 | */ | |
5779 | ||
79072805 | 5780 | I32 |
e01b9e88 | 5781 | Perl_sv_eq(pTHX_ register SV *sv1, register SV *sv2) |
79072805 LW |
5782 | { |
5783 | char *pv1; | |
463ee0b2 | 5784 | STRLEN cur1; |
79072805 | 5785 | char *pv2; |
463ee0b2 | 5786 | STRLEN cur2; |
e01b9e88 | 5787 | I32 eq = 0; |
553e1bcc AT |
5788 | char *tpv = Nullch; |
5789 | SV* svrecode = Nullsv; | |
79072805 | 5790 | |
e01b9e88 | 5791 | if (!sv1) { |
79072805 LW |
5792 | pv1 = ""; |
5793 | cur1 = 0; | |
5794 | } | |
463ee0b2 | 5795 | else |
e01b9e88 | 5796 | pv1 = SvPV(sv1, cur1); |
79072805 | 5797 | |
e01b9e88 SC |
5798 | if (!sv2){ |
5799 | pv2 = ""; | |
5800 | cur2 = 0; | |
92d29cee | 5801 | } |
e01b9e88 SC |
5802 | else |
5803 | pv2 = SvPV(sv2, cur2); | |
79072805 | 5804 | |
cf48d248 | 5805 | if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) { |
799ef3cb JH |
5806 | /* Differing utf8ness. |
5807 | * Do not UTF8size the comparands as a side-effect. */ | |
5808 | if (PL_encoding) { | |
5809 | if (SvUTF8(sv1)) { | |
553e1bcc AT |
5810 | svrecode = newSVpvn(pv2, cur2); |
5811 | sv_recode_to_utf8(svrecode, PL_encoding); | |
5812 | pv2 = SvPV(svrecode, cur2); | |
799ef3cb JH |
5813 | } |
5814 | else { | |
553e1bcc AT |
5815 | svrecode = newSVpvn(pv1, cur1); |
5816 | sv_recode_to_utf8(svrecode, PL_encoding); | |
5817 | pv1 = SvPV(svrecode, cur1); | |
799ef3cb JH |
5818 | } |
5819 | /* Now both are in UTF-8. */ | |
5820 | if (cur1 != cur2) | |
5821 | return FALSE; | |
5822 | } | |
5823 | else { | |
5824 | bool is_utf8 = TRUE; | |
5825 | ||
5826 | if (SvUTF8(sv1)) { | |
5827 | /* sv1 is the UTF-8 one, | |
5828 | * if is equal it must be downgrade-able */ | |
5829 | char *pv = (char*)bytes_from_utf8((U8*)pv1, | |
5830 | &cur1, &is_utf8); | |
5831 | if (pv != pv1) | |
553e1bcc | 5832 | pv1 = tpv = pv; |
799ef3cb JH |
5833 | } |
5834 | else { | |
5835 | /* sv2 is the UTF-8 one, | |
5836 | * if is equal it must be downgrade-able */ | |
5837 | char *pv = (char *)bytes_from_utf8((U8*)pv2, | |
5838 | &cur2, &is_utf8); | |
5839 | if (pv != pv2) | |
553e1bcc | 5840 | pv2 = tpv = pv; |
799ef3cb JH |
5841 | } |
5842 | if (is_utf8) { | |
5843 | /* Downgrade not possible - cannot be eq */ | |
5844 | return FALSE; | |
5845 | } | |
5846 | } | |
cf48d248 JH |
5847 | } |
5848 | ||
5849 | if (cur1 == cur2) | |
5850 | eq = memEQ(pv1, pv2, cur1); | |
e01b9e88 | 5851 | |
553e1bcc AT |
5852 | if (svrecode) |
5853 | SvREFCNT_dec(svrecode); | |
799ef3cb | 5854 | |
553e1bcc AT |
5855 | if (tpv) |
5856 | Safefree(tpv); | |
cf48d248 | 5857 | |
e01b9e88 | 5858 | return eq; |
79072805 LW |
5859 | } |
5860 | ||
954c1994 GS |
5861 | /* |
5862 | =for apidoc sv_cmp | |
5863 | ||
5864 | Compares the strings in two SVs. Returns -1, 0, or 1 indicating whether the | |
5865 | string in C<sv1> is less than, equal to, or greater than the string in | |
645c22ef DM |
5866 | C<sv2>. Is UTF-8 and 'use bytes' aware, handles get magic, and will |
5867 | coerce its args to strings if necessary. See also C<sv_cmp_locale>. | |
954c1994 GS |
5868 | |
5869 | =cut | |
5870 | */ | |
5871 | ||
79072805 | 5872 | I32 |
e01b9e88 | 5873 | Perl_sv_cmp(pTHX_ register SV *sv1, register SV *sv2) |
79072805 | 5874 | { |
560a288e | 5875 | STRLEN cur1, cur2; |
553e1bcc | 5876 | char *pv1, *pv2, *tpv = Nullch; |
cf48d248 | 5877 | I32 cmp; |
553e1bcc | 5878 | SV *svrecode = Nullsv; |
560a288e | 5879 | |
e01b9e88 SC |
5880 | if (!sv1) { |
5881 | pv1 = ""; | |
560a288e GS |
5882 | cur1 = 0; |
5883 | } | |
e01b9e88 SC |
5884 | else |
5885 | pv1 = SvPV(sv1, cur1); | |
560a288e | 5886 | |
553e1bcc | 5887 | if (!sv2) { |
e01b9e88 | 5888 | pv2 = ""; |
560a288e GS |
5889 | cur2 = 0; |
5890 | } | |
e01b9e88 SC |
5891 | else |
5892 | pv2 = SvPV(sv2, cur2); | |
79072805 | 5893 | |
cf48d248 | 5894 | if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) { |
799ef3cb JH |
5895 | /* Differing utf8ness. |
5896 | * Do not UTF8size the comparands as a side-effect. */ | |
cf48d248 | 5897 | if (SvUTF8(sv1)) { |
799ef3cb | 5898 | if (PL_encoding) { |
553e1bcc AT |
5899 | svrecode = newSVpvn(pv2, cur2); |
5900 | sv_recode_to_utf8(svrecode, PL_encoding); | |
5901 | pv2 = SvPV(svrecode, cur2); | |
799ef3cb JH |
5902 | } |
5903 | else { | |
553e1bcc | 5904 | pv2 = tpv = (char*)bytes_to_utf8((U8*)pv2, &cur2); |
799ef3cb | 5905 | } |
cf48d248 JH |
5906 | } |
5907 | else { | |
799ef3cb | 5908 | if (PL_encoding) { |
553e1bcc AT |
5909 | svrecode = newSVpvn(pv1, cur1); |
5910 | sv_recode_to_utf8(svrecode, PL_encoding); | |
5911 | pv1 = SvPV(svrecode, cur1); | |
799ef3cb JH |
5912 | } |
5913 | else { | |
553e1bcc | 5914 | pv1 = tpv = (char*)bytes_to_utf8((U8*)pv1, &cur1); |
799ef3cb | 5915 | } |
cf48d248 JH |
5916 | } |
5917 | } | |
5918 | ||
e01b9e88 | 5919 | if (!cur1) { |
cf48d248 | 5920 | cmp = cur2 ? -1 : 0; |
e01b9e88 | 5921 | } else if (!cur2) { |
cf48d248 JH |
5922 | cmp = 1; |
5923 | } else { | |
5924 | I32 retval = memcmp((void*)pv1, (void*)pv2, cur1 < cur2 ? cur1 : cur2); | |
e01b9e88 SC |
5925 | |
5926 | if (retval) { | |
cf48d248 | 5927 | cmp = retval < 0 ? -1 : 1; |
e01b9e88 | 5928 | } else if (cur1 == cur2) { |
cf48d248 JH |
5929 | cmp = 0; |
5930 | } else { | |
5931 | cmp = cur1 < cur2 ? -1 : 1; | |
e01b9e88 | 5932 | } |
cf48d248 | 5933 | } |
16660edb | 5934 | |
553e1bcc AT |
5935 | if (svrecode) |
5936 | SvREFCNT_dec(svrecode); | |
799ef3cb | 5937 | |
553e1bcc AT |
5938 | if (tpv) |
5939 | Safefree(tpv); | |
cf48d248 JH |
5940 | |
5941 | return cmp; | |
bbce6d69 | 5942 | } |
16660edb | 5943 | |
c461cf8f JH |
5944 | /* |
5945 | =for apidoc sv_cmp_locale | |
5946 | ||
645c22ef DM |
5947 | Compares the strings in two SVs in a locale-aware manner. Is UTF-8 and |
5948 | 'use bytes' aware, handles get magic, and will coerce its args to strings | |
5949 | if necessary. See also C<sv_cmp_locale>. See also C<sv_cmp>. | |
c461cf8f JH |
5950 | |
5951 | =cut | |
5952 | */ | |
5953 | ||
bbce6d69 | 5954 | I32 |
864dbfa3 | 5955 | Perl_sv_cmp_locale(pTHX_ register SV *sv1, register SV *sv2) |
bbce6d69 | 5956 | { |
36477c24 | 5957 | #ifdef USE_LOCALE_COLLATE |
16660edb | 5958 | |
bbce6d69 | 5959 | char *pv1, *pv2; |
5960 | STRLEN len1, len2; | |
5961 | I32 retval; | |
16660edb | 5962 | |
3280af22 | 5963 | if (PL_collation_standard) |
bbce6d69 | 5964 | goto raw_compare; |
16660edb | 5965 | |
bbce6d69 | 5966 | len1 = 0; |
8ac85365 | 5967 | pv1 = sv1 ? sv_collxfrm(sv1, &len1) : (char *) NULL; |
bbce6d69 | 5968 | len2 = 0; |
8ac85365 | 5969 | pv2 = sv2 ? sv_collxfrm(sv2, &len2) : (char *) NULL; |
16660edb | 5970 | |
bbce6d69 | 5971 | if (!pv1 || !len1) { |
5972 | if (pv2 && len2) | |
5973 | return -1; | |
5974 | else | |
5975 | goto raw_compare; | |
5976 | } | |
5977 | else { | |
5978 | if (!pv2 || !len2) | |
5979 | return 1; | |
5980 | } | |
16660edb | 5981 | |
bbce6d69 | 5982 | retval = memcmp((void*)pv1, (void*)pv2, len1 < len2 ? len1 : len2); |
16660edb | 5983 | |
bbce6d69 | 5984 | if (retval) |
16660edb | 5985 | return retval < 0 ? -1 : 1; |
5986 | ||
bbce6d69 | 5987 | /* |
5988 | * When the result of collation is equality, that doesn't mean | |
5989 | * that there are no differences -- some locales exclude some | |
5990 | * characters from consideration. So to avoid false equalities, | |
5991 | * we use the raw string as a tiebreaker. | |
5992 | */ | |
16660edb | 5993 | |
bbce6d69 | 5994 | raw_compare: |
5995 | /* FALL THROUGH */ | |
16660edb | 5996 | |
36477c24 | 5997 | #endif /* USE_LOCALE_COLLATE */ |
16660edb | 5998 | |
bbce6d69 | 5999 | return sv_cmp(sv1, sv2); |
6000 | } | |
79072805 | 6001 | |
645c22ef | 6002 | |
36477c24 | 6003 | #ifdef USE_LOCALE_COLLATE |
645c22ef | 6004 | |
7a4c00b4 | 6005 | /* |
645c22ef DM |
6006 | =for apidoc sv_collxfrm |
6007 | ||
6008 | Add Collate Transform magic to an SV if it doesn't already have it. | |
6009 | ||
6010 | Any scalar variable may carry PERL_MAGIC_collxfrm magic that contains the | |
6011 | scalar data of the variable, but transformed to such a format that a normal | |
6012 | memory comparison can be used to compare the data according to the locale | |
6013 | settings. | |
6014 | ||
6015 | =cut | |
6016 | */ | |
6017 | ||
bbce6d69 | 6018 | char * |
864dbfa3 | 6019 | Perl_sv_collxfrm(pTHX_ SV *sv, STRLEN *nxp) |
bbce6d69 | 6020 | { |
7a4c00b4 | 6021 | MAGIC *mg; |
16660edb | 6022 | |
14befaf4 | 6023 | mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_collxfrm) : (MAGIC *) NULL; |
3280af22 | 6024 | if (!mg || !mg->mg_ptr || *(U32*)mg->mg_ptr != PL_collation_ix) { |
bbce6d69 | 6025 | char *s, *xf; |
6026 | STRLEN len, xlen; | |
6027 | ||
7a4c00b4 | 6028 | if (mg) |
6029 | Safefree(mg->mg_ptr); | |
bbce6d69 | 6030 | s = SvPV(sv, len); |
6031 | if ((xf = mem_collxfrm(s, len, &xlen))) { | |
ff0cee69 | 6032 | if (SvREADONLY(sv)) { |
6033 | SAVEFREEPV(xf); | |
6034 | *nxp = xlen; | |
3280af22 | 6035 | return xf + sizeof(PL_collation_ix); |
ff0cee69 | 6036 | } |
7a4c00b4 | 6037 | if (! mg) { |
14befaf4 DM |
6038 | sv_magic(sv, 0, PERL_MAGIC_collxfrm, 0, 0); |
6039 | mg = mg_find(sv, PERL_MAGIC_collxfrm); | |
7a4c00b4 | 6040 | assert(mg); |
bbce6d69 | 6041 | } |
7a4c00b4 | 6042 | mg->mg_ptr = xf; |
565764a8 | 6043 | mg->mg_len = xlen; |
7a4c00b4 | 6044 | } |
6045 | else { | |
ff0cee69 | 6046 | if (mg) { |
6047 | mg->mg_ptr = NULL; | |
565764a8 | 6048 | mg->mg_len = -1; |
ff0cee69 | 6049 | } |
bbce6d69 | 6050 | } |
6051 | } | |
7a4c00b4 | 6052 | if (mg && mg->mg_ptr) { |
565764a8 | 6053 | *nxp = mg->mg_len; |
3280af22 | 6054 | return mg->mg_ptr + sizeof(PL_collation_ix); |
bbce6d69 | 6055 | } |
6056 | else { | |
6057 | *nxp = 0; | |
6058 | return NULL; | |
16660edb | 6059 | } |
79072805 LW |
6060 | } |
6061 | ||
36477c24 | 6062 | #endif /* USE_LOCALE_COLLATE */ |
bbce6d69 | 6063 | |
c461cf8f JH |
6064 | /* |
6065 | =for apidoc sv_gets | |
6066 | ||
6067 | Get a line from the filehandle and store it into the SV, optionally | |
6068 | appending to the currently-stored string. | |
6069 | ||
6070 | =cut | |
6071 | */ | |
6072 | ||
79072805 | 6073 | char * |
864dbfa3 | 6074 | Perl_sv_gets(pTHX_ register SV *sv, register PerlIO *fp, I32 append) |
79072805 | 6075 | { |
c07a80fd | 6076 | char *rsptr; |
6077 | STRLEN rslen; | |
6078 | register STDCHAR rslast; | |
6079 | register STDCHAR *bp; | |
6080 | register I32 cnt; | |
9c5ffd7c | 6081 | I32 i = 0; |
8bfdd7d9 | 6082 | I32 rspara = 0; |
fe1d0b35 | 6083 | I32 recsize; |
c07a80fd | 6084 | |
2213622d | 6085 | SV_CHECK_THINKFIRST(sv); |
6fc92669 | 6086 | (void)SvUPGRADE(sv, SVt_PV); |
99491443 | 6087 | |
ff68c719 | 6088 | SvSCREAM_off(sv); |
251c53ad AE |
6089 | |
6090 | if (append) { | |
6091 | if (PerlIO_isutf8(fp)) { | |
6092 | if (!SvUTF8(sv)) { | |
6093 | sv_utf8_upgrade_nomg(sv); | |
6094 | sv_pos_u2b(sv,&append,0); | |
6095 | } | |
6096 | } else if (SvUTF8(sv)) { | |
6097 | SV *tsv = NEWSV(0,0); | |
6098 | sv_gets(tsv, fp, 0); | |
6099 | sv_utf8_upgrade_nomg(tsv); | |
6100 | SvCUR_set(sv,append); | |
6101 | sv_catsv(sv,tsv); | |
6102 | sv_free(tsv); | |
6103 | goto return_string_or_null; | |
6104 | } | |
6105 | } | |
6106 | ||
6107 | SvPOK_only(sv); | |
6108 | if (PerlIO_isutf8(fp)) | |
6109 | SvUTF8_on(sv); | |
c07a80fd | 6110 | |
8bfdd7d9 HS |
6111 | if (PL_curcop == &PL_compiling) { |
6112 | /* we always read code in line mode */ | |
6113 | rsptr = "\n"; | |
6114 | rslen = 1; | |
6115 | } | |
6116 | else if (RsSNARF(PL_rs)) { | |
338501c1 JH |
6117 | /* If it is a regular disk file use size from stat() as estimate |
6118 | of amount we are going to read - may result in malloc-ing | |
6119 | more memory than we realy need if layers bellow reduce | |
6120 | size we read (e.g. CRLF or a gzip layer) | |
6121 | */ | |
fe1d0b35 | 6122 | Stat_t st; |
338501c1 JH |
6123 | if (!PerlLIO_fstat(PerlIO_fileno(fp), &st) && S_ISREG(st.st_mode)) { |
6124 | Off_t offset = PerlIO_tell(fp); | |
bfd7eeef | 6125 | if (offset != (Off_t) -1 && st.st_size + append > offset) { |
338501c1 JH |
6126 | (void) SvGROW(sv, (STRLEN)((st.st_size - offset) + append + 1)); |
6127 | } | |
6128 | } | |
c07a80fd | 6129 | rsptr = NULL; |
6130 | rslen = 0; | |
6131 | } | |
3280af22 | 6132 | else if (RsRECORD(PL_rs)) { |
fe1d0b35 | 6133 | I32 bytesread; |
5b2b9c68 HM |
6134 | char *buffer; |
6135 | ||
6136 | /* Grab the size of the record we're getting */ | |
3280af22 | 6137 | recsize = SvIV(SvRV(PL_rs)); |
fe1d0b35 | 6138 | buffer = SvGROW(sv, (STRLEN)(recsize + append + 1)) + append; |
5b2b9c68 HM |
6139 | /* Go yank in */ |
6140 | #ifdef VMS | |
6141 | /* VMS wants read instead of fread, because fread doesn't respect */ | |
6142 | /* RMS record boundaries. This is not necessarily a good thing to be */ | |
338501c1 JH |
6143 | /* doing, but we've got no other real choice - except avoid stdio |
6144 | as implementation - perhaps write a :vms layer ? | |
6145 | */ | |
5b2b9c68 HM |
6146 | bytesread = PerlLIO_read(PerlIO_fileno(fp), buffer, recsize); |
6147 | #else | |
6148 | bytesread = PerlIO_read(fp, buffer, recsize); | |
6149 | #endif | |
e3b9cd39 JH |
6150 | if (bytesread < 0) |
6151 | bytesread = 0; | |
fe1d0b35 | 6152 | SvCUR_set(sv, bytesread += append); |
e670df4e | 6153 | buffer[bytesread] = '\0'; |
251c53ad | 6154 | goto return_string_or_null; |
5b2b9c68 | 6155 | } |
3280af22 | 6156 | else if (RsPARA(PL_rs)) { |
c07a80fd | 6157 | rsptr = "\n\n"; |
6158 | rslen = 2; | |
8bfdd7d9 | 6159 | rspara = 1; |
c07a80fd | 6160 | } |
7d59b7e4 NIS |
6161 | else { |
6162 | /* Get $/ i.e. PL_rs into same encoding as stream wants */ | |
6163 | if (PerlIO_isutf8(fp)) { | |
6164 | rsptr = SvPVutf8(PL_rs, rslen); | |
6165 | } | |
6166 | else { | |
6167 | if (SvUTF8(PL_rs)) { | |
6168 | if (!sv_utf8_downgrade(PL_rs, TRUE)) { | |
6169 | Perl_croak(aTHX_ "Wide character in $/"); | |
6170 | } | |
6171 | } | |
6172 | rsptr = SvPV(PL_rs, rslen); | |
6173 | } | |
6174 | } | |
6175 | ||
c07a80fd | 6176 | rslast = rslen ? rsptr[rslen - 1] : '\0'; |
6177 | ||
8bfdd7d9 | 6178 | if (rspara) { /* have to do this both before and after */ |
79072805 | 6179 | do { /* to make sure file boundaries work right */ |
760ac839 | 6180 | if (PerlIO_eof(fp)) |
a0d0e21e | 6181 | return 0; |
760ac839 | 6182 | i = PerlIO_getc(fp); |
79072805 | 6183 | if (i != '\n') { |
a0d0e21e LW |
6184 | if (i == -1) |
6185 | return 0; | |
760ac839 | 6186 | PerlIO_ungetc(fp,i); |
79072805 LW |
6187 | break; |
6188 | } | |
6189 | } while (i != EOF); | |
6190 | } | |
c07a80fd | 6191 | |
760ac839 LW |
6192 | /* See if we know enough about I/O mechanism to cheat it ! */ |
6193 | ||
6194 | /* This used to be #ifdef test - it is made run-time test for ease | |
1c846c1f | 6195 | of abstracting out stdio interface. One call should be cheap |
760ac839 LW |
6196 | enough here - and may even be a macro allowing compile |
6197 | time optimization. | |
6198 | */ | |
6199 | ||
6200 | if (PerlIO_fast_gets(fp)) { | |
6201 | ||
6202 | /* | |
6203 | * We're going to steal some values from the stdio struct | |
6204 | * and put EVERYTHING in the innermost loop into registers. | |
6205 | */ | |
6206 | register STDCHAR *ptr; | |
6207 | STRLEN bpx; | |
6208 | I32 shortbuffered; | |
6209 | ||
16660edb | 6210 | #if defined(VMS) && defined(PERLIO_IS_STDIO) |
6211 | /* An ungetc()d char is handled separately from the regular | |
6212 | * buffer, so we getc() it back out and stuff it in the buffer. | |
6213 | */ | |
6214 | i = PerlIO_getc(fp); | |
6215 | if (i == EOF) return 0; | |
6216 | *(--((*fp)->_ptr)) = (unsigned char) i; | |
6217 | (*fp)->_cnt++; | |
6218 | #endif | |
c07a80fd | 6219 | |
c2960299 | 6220 | /* Here is some breathtakingly efficient cheating */ |
c07a80fd | 6221 | |
a20bf0c3 | 6222 | cnt = PerlIO_get_cnt(fp); /* get count into register */ |
338501c1 JH |
6223 | /* make sure we have the room */ |
6224 | if ((I32)(SvLEN(sv) - append) <= cnt + 1) { | |
6225 | /* Not room for all of it | |
6226 | if we are looking for a separator and room for some | |
6227 | */ | |
6228 | if (rslen && cnt > 80 && (I32)SvLEN(sv) > append) { | |
6229 | /* just process what we have room for */ | |
79072805 LW |
6230 | shortbuffered = cnt - SvLEN(sv) + append + 1; |
6231 | cnt -= shortbuffered; | |
6232 | } | |
6233 | else { | |
6234 | shortbuffered = 0; | |
bbce6d69 | 6235 | /* remember that cnt can be negative */ |
eb160463 | 6236 | SvGROW(sv, (STRLEN)(append + (cnt <= 0 ? 2 : (cnt + 1)))); |
79072805 LW |
6237 | } |
6238 | } | |
338501c1 | 6239 | else |
79072805 | 6240 | shortbuffered = 0; |
c07a80fd | 6241 | bp = (STDCHAR*)SvPVX(sv) + append; /* move these two too to registers */ |
a20bf0c3 | 6242 | ptr = (STDCHAR*)PerlIO_get_ptr(fp); |
16660edb | 6243 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 6244 | "Screamer: entering, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt)); |
16660edb | 6245 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
ba7abf9d | 6246 | "Screamer: entering: PerlIO * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n", |
1c846c1f | 6247 | PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp), |
1d7c1841 | 6248 | PTR2UV(PerlIO_has_base(fp) ? PerlIO_get_base(fp) : 0))); |
79072805 LW |
6249 | for (;;) { |
6250 | screamer: | |
93a17b20 | 6251 | if (cnt > 0) { |
c07a80fd | 6252 | if (rslen) { |
760ac839 LW |
6253 | while (cnt > 0) { /* this | eat */ |
6254 | cnt--; | |
c07a80fd | 6255 | if ((*bp++ = *ptr++) == rslast) /* really | dust */ |
6256 | goto thats_all_folks; /* screams | sed :-) */ | |
6257 | } | |
6258 | } | |
6259 | else { | |
1c846c1f NIS |
6260 | Copy(ptr, bp, cnt, char); /* this | eat */ |
6261 | bp += cnt; /* screams | dust */ | |
c07a80fd | 6262 | ptr += cnt; /* louder | sed :-) */ |
a5f75d66 | 6263 | cnt = 0; |
93a17b20 | 6264 | } |
79072805 LW |
6265 | } |
6266 | ||
748a9306 | 6267 | if (shortbuffered) { /* oh well, must extend */ |
79072805 LW |
6268 | cnt = shortbuffered; |
6269 | shortbuffered = 0; | |
c07a80fd | 6270 | bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */ |
79072805 LW |
6271 | SvCUR_set(sv, bpx); |
6272 | SvGROW(sv, SvLEN(sv) + append + cnt + 2); | |
c07a80fd | 6273 | bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */ |
79072805 LW |
6274 | continue; |
6275 | } | |
6276 | ||
16660edb | 6277 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 GS |
6278 | "Screamer: going to getc, ptr=%"UVuf", cnt=%ld\n", |
6279 | PTR2UV(ptr),(long)cnt)); | |
cc00df79 | 6280 | PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* deregisterize cnt and ptr */ |
ba7abf9d | 6281 | #if 0 |
16660edb | 6282 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 6283 | "Screamer: pre: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n", |
1c846c1f | 6284 | PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp), |
1d7c1841 | 6285 | PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0))); |
ba7abf9d | 6286 | #endif |
1c846c1f | 6287 | /* This used to call 'filbuf' in stdio form, but as that behaves like |
774d564b | 6288 | getc when cnt <= 0 we use PerlIO_getc here to avoid introducing |
6289 | another abstraction. */ | |
760ac839 | 6290 | i = PerlIO_getc(fp); /* get more characters */ |
ba7abf9d | 6291 | #if 0 |
16660edb | 6292 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 6293 | "Screamer: post: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n", |
1c846c1f | 6294 | PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp), |
1d7c1841 | 6295 | PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0))); |
ba7abf9d | 6296 | #endif |
a20bf0c3 JH |
6297 | cnt = PerlIO_get_cnt(fp); |
6298 | ptr = (STDCHAR*)PerlIO_get_ptr(fp); /* reregisterize cnt and ptr */ | |
16660edb | 6299 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 6300 | "Screamer: after getc, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt)); |
79072805 | 6301 | |
748a9306 LW |
6302 | if (i == EOF) /* all done for ever? */ |
6303 | goto thats_really_all_folks; | |
6304 | ||
c07a80fd | 6305 | bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */ |
79072805 LW |
6306 | SvCUR_set(sv, bpx); |
6307 | SvGROW(sv, bpx + cnt + 2); | |
c07a80fd | 6308 | bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */ |
6309 | ||
eb160463 | 6310 | *bp++ = (STDCHAR)i; /* store character from PerlIO_getc */ |
79072805 | 6311 | |
c07a80fd | 6312 | if (rslen && (STDCHAR)i == rslast) /* all done for now? */ |
79072805 | 6313 | goto thats_all_folks; |
79072805 LW |
6314 | } |
6315 | ||
6316 | thats_all_folks: | |
eb160463 | 6317 | if ((rslen > 1 && (STRLEN)(bp - (STDCHAR*)SvPVX(sv)) < rslen) || |
36477c24 | 6318 | memNE((char*)bp - rslen, rsptr, rslen)) |
760ac839 | 6319 | goto screamer; /* go back to the fray */ |
79072805 LW |
6320 | thats_really_all_folks: |
6321 | if (shortbuffered) | |
6322 | cnt += shortbuffered; | |
16660edb | 6323 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 6324 | "Screamer: quitting, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt)); |
cc00df79 | 6325 | PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* put these back or we're in trouble */ |
16660edb | 6326 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 6327 | "Screamer: end: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n", |
1c846c1f | 6328 | PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp), |
1d7c1841 | 6329 | PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0))); |
79072805 | 6330 | *bp = '\0'; |
760ac839 | 6331 | SvCUR_set(sv, bp - (STDCHAR*)SvPVX(sv)); /* set length */ |
16660edb | 6332 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
fb73857a | 6333 | "Screamer: done, len=%ld, string=|%.*s|\n", |
6334 | (long)SvCUR(sv),(int)SvCUR(sv),SvPVX(sv))); | |
760ac839 LW |
6335 | } |
6336 | else | |
79072805 | 6337 | { |
2ee2a917 JH |
6338 | /*The big, slow, and stupid way. */ |
6339 | ||
6340 | /* Any stack-challenged places. */ | |
f73bace3 | 6341 | #if defined(EPOC) |
2ee2a917 JH |
6342 | /* EPOC: need to work around SDK features. * |
6343 | * On WINS: MS VC5 generates calls to _chkstk, * | |
6344 | * if a "large" stack frame is allocated. * | |
6345 | * gcc on MARM does not generate calls like these. */ | |
6346 | # define USEHEAPINSTEADOFSTACK | |
6347 | #endif | |
6348 | ||
6349 | #ifdef USEHEAPINSTEADOFSTACK | |
6350 | STDCHAR *buf = 0; | |
6351 | New(0, buf, 8192, STDCHAR); | |
6352 | assert(buf); | |
4d2c4e07 | 6353 | #else |
2ee2a917 | 6354 | STDCHAR buf[8192]; |
4d2c4e07 | 6355 | #endif |
79072805 | 6356 | |
760ac839 | 6357 | screamer2: |
c07a80fd | 6358 | if (rslen) { |
760ac839 LW |
6359 | register STDCHAR *bpe = buf + sizeof(buf); |
6360 | bp = buf; | |
eb160463 | 6361 | while ((i = PerlIO_getc(fp)) != EOF && (*bp++ = (STDCHAR)i) != rslast && bp < bpe) |
760ac839 LW |
6362 | ; /* keep reading */ |
6363 | cnt = bp - buf; | |
c07a80fd | 6364 | } |
6365 | else { | |
760ac839 | 6366 | cnt = PerlIO_read(fp,(char*)buf, sizeof(buf)); |
16660edb | 6367 | /* Accomodate broken VAXC compiler, which applies U8 cast to |
6368 | * both args of ?: operator, causing EOF to change into 255 | |
6369 | */ | |
37be0adf | 6370 | if (cnt > 0) |
cbe9e203 JH |
6371 | i = (U8)buf[cnt - 1]; |
6372 | else | |
37be0adf | 6373 | i = EOF; |
c07a80fd | 6374 | } |
79072805 | 6375 | |
cbe9e203 JH |
6376 | if (cnt < 0) |
6377 | cnt = 0; /* we do need to re-set the sv even when cnt <= 0 */ | |
6378 | if (append) | |
6379 | sv_catpvn(sv, (char *) buf, cnt); | |
6380 | else | |
6381 | sv_setpvn(sv, (char *) buf, cnt); | |
c07a80fd | 6382 | |
6383 | if (i != EOF && /* joy */ | |
6384 | (!rslen || | |
6385 | SvCUR(sv) < rslen || | |
36477c24 | 6386 | memNE(SvPVX(sv) + SvCUR(sv) - rslen, rsptr, rslen))) |
79072805 LW |
6387 | { |
6388 | append = -1; | |
63e4d877 CS |
6389 | /* |
6390 | * If we're reading from a TTY and we get a short read, | |
6391 | * indicating that the user hit his EOF character, we need | |
6392 | * to notice it now, because if we try to read from the TTY | |
6393 | * again, the EOF condition will disappear. | |
6394 | * | |
6395 | * The comparison of cnt to sizeof(buf) is an optimization | |
6396 | * that prevents unnecessary calls to feof(). | |
6397 | * | |
6398 | * - jik 9/25/96 | |
6399 | */ | |
6400 | if (!(cnt < sizeof(buf) && PerlIO_eof(fp))) | |
6401 | goto screamer2; | |
79072805 | 6402 | } |
2ee2a917 JH |
6403 | |
6404 | #ifdef USEHEAPINSTEADOFSTACK | |
6405 | Safefree(buf); | |
6406 | #endif | |
79072805 LW |
6407 | } |
6408 | ||
8bfdd7d9 | 6409 | if (rspara) { /* have to do this both before and after */ |
c07a80fd | 6410 | while (i != EOF) { /* to make sure file boundaries work right */ |
760ac839 | 6411 | i = PerlIO_getc(fp); |
79072805 | 6412 | if (i != '\n') { |
760ac839 | 6413 | PerlIO_ungetc(fp,i); |
79072805 LW |
6414 | break; |
6415 | } | |
6416 | } | |
6417 | } | |
c07a80fd | 6418 | |
251c53ad | 6419 | return_string_or_null: |
c07a80fd | 6420 | return (SvCUR(sv) - append) ? SvPVX(sv) : Nullch; |
79072805 LW |
6421 | } |
6422 | ||
954c1994 GS |
6423 | /* |
6424 | =for apidoc sv_inc | |
6425 | ||
645c22ef DM |
6426 | Auto-increment of the value in the SV, doing string to numeric conversion |
6427 | if necessary. Handles 'get' magic. | |
954c1994 GS |
6428 | |
6429 | =cut | |
6430 | */ | |
6431 | ||
79072805 | 6432 | void |
864dbfa3 | 6433 | Perl_sv_inc(pTHX_ register SV *sv) |
79072805 LW |
6434 | { |
6435 | register char *d; | |
463ee0b2 | 6436 | int flags; |
79072805 LW |
6437 | |
6438 | if (!sv) | |
6439 | return; | |
b23a5f78 GB |
6440 | if (SvGMAGICAL(sv)) |
6441 | mg_get(sv); | |
ed6116ce | 6442 | if (SvTHINKFIRST(sv)) { |
3510b4a1 NC |
6443 | if (SvREADONLY(sv) && SvFAKE(sv)) |
6444 | sv_force_normal(sv); | |
0f15f207 | 6445 | if (SvREADONLY(sv)) { |
3280af22 | 6446 | if (PL_curcop != &PL_compiling) |
cea2e8a9 | 6447 | Perl_croak(aTHX_ PL_no_modify); |
0f15f207 | 6448 | } |
a0d0e21e | 6449 | if (SvROK(sv)) { |
b5be31e9 | 6450 | IV i; |
9e7bc3e8 JD |
6451 | if (SvAMAGIC(sv) && AMG_CALLun(sv,inc)) |
6452 | return; | |
56431972 | 6453 | i = PTR2IV(SvRV(sv)); |
b5be31e9 SM |
6454 | sv_unref(sv); |
6455 | sv_setiv(sv, i); | |
a0d0e21e | 6456 | } |
ed6116ce | 6457 | } |
8990e307 | 6458 | flags = SvFLAGS(sv); |
28e5dec8 JH |
6459 | if ((flags & (SVp_NOK|SVp_IOK)) == SVp_NOK) { |
6460 | /* It's (privately or publicly) a float, but not tested as an | |
6461 | integer, so test it to see. */ | |
d460ef45 | 6462 | (void) SvIV(sv); |
28e5dec8 JH |
6463 | flags = SvFLAGS(sv); |
6464 | } | |
6465 | if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) { | |
6466 | /* It's publicly an integer, or privately an integer-not-float */ | |
59d8ce62 | 6467 | #ifdef PERL_PRESERVE_IVUV |
28e5dec8 | 6468 | oops_its_int: |
59d8ce62 | 6469 | #endif |
25da4f38 IZ |
6470 | if (SvIsUV(sv)) { |
6471 | if (SvUVX(sv) == UV_MAX) | |
a1e868e7 | 6472 | sv_setnv(sv, UV_MAX_P1); |
25da4f38 IZ |
6473 | else |
6474 | (void)SvIOK_only_UV(sv); | |
6475 | ++SvUVX(sv); | |
6476 | } else { | |
6477 | if (SvIVX(sv) == IV_MAX) | |
28e5dec8 | 6478 | sv_setuv(sv, (UV)IV_MAX + 1); |
25da4f38 IZ |
6479 | else { |
6480 | (void)SvIOK_only(sv); | |
6481 | ++SvIVX(sv); | |
1c846c1f | 6482 | } |
55497cff | 6483 | } |
79072805 LW |
6484 | return; |
6485 | } | |
28e5dec8 JH |
6486 | if (flags & SVp_NOK) { |
6487 | (void)SvNOK_only(sv); | |
6488 | SvNVX(sv) += 1.0; | |
6489 | return; | |
6490 | } | |
6491 | ||
8990e307 | 6492 | if (!(flags & SVp_POK) || !*SvPVX(sv)) { |
28e5dec8 JH |
6493 | if ((flags & SVTYPEMASK) < SVt_PVIV) |
6494 | sv_upgrade(sv, SVt_IV); | |
6495 | (void)SvIOK_only(sv); | |
6496 | SvIVX(sv) = 1; | |
79072805 LW |
6497 | return; |
6498 | } | |
463ee0b2 | 6499 | d = SvPVX(sv); |
79072805 LW |
6500 | while (isALPHA(*d)) d++; |
6501 | while (isDIGIT(*d)) d++; | |
6502 | if (*d) { | |
28e5dec8 | 6503 | #ifdef PERL_PRESERVE_IVUV |
d1be9408 | 6504 | /* Got to punt this as an integer if needs be, but we don't issue |
28e5dec8 JH |
6505 | warnings. Probably ought to make the sv_iv_please() that does |
6506 | the conversion if possible, and silently. */ | |
c2988b20 | 6507 | int numtype = grok_number(SvPVX(sv), SvCUR(sv), NULL); |
28e5dec8 JH |
6508 | if (numtype && !(numtype & IS_NUMBER_INFINITY)) { |
6509 | /* Need to try really hard to see if it's an integer. | |
6510 | 9.22337203685478e+18 is an integer. | |
6511 | but "9.22337203685478e+18" + 0 is UV=9223372036854779904 | |
6512 | so $a="9.22337203685478e+18"; $a+0; $a++ | |
6513 | needs to be the same as $a="9.22337203685478e+18"; $a++ | |
6514 | or we go insane. */ | |
d460ef45 | 6515 | |
28e5dec8 JH |
6516 | (void) sv_2iv(sv); |
6517 | if (SvIOK(sv)) | |
6518 | goto oops_its_int; | |
6519 | ||
6520 | /* sv_2iv *should* have made this an NV */ | |
6521 | if (flags & SVp_NOK) { | |
6522 | (void)SvNOK_only(sv); | |
6523 | SvNVX(sv) += 1.0; | |
6524 | return; | |
6525 | } | |
6526 | /* I don't think we can get here. Maybe I should assert this | |
6527 | And if we do get here I suspect that sv_setnv will croak. NWC | |
6528 | Fall through. */ | |
6529 | #if defined(USE_LONG_DOUBLE) | |
6530 | 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", | |
6531 | SvPVX(sv), SvIVX(sv), SvNVX(sv))); | |
6532 | #else | |
1779d84d | 6533 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"NVgf"\n", |
28e5dec8 JH |
6534 | SvPVX(sv), SvIVX(sv), SvNVX(sv))); |
6535 | #endif | |
6536 | } | |
6537 | #endif /* PERL_PRESERVE_IVUV */ | |
6538 | sv_setnv(sv,Atof(SvPVX(sv)) + 1.0); | |
79072805 LW |
6539 | return; |
6540 | } | |
6541 | d--; | |
463ee0b2 | 6542 | while (d >= SvPVX(sv)) { |
79072805 LW |
6543 | if (isDIGIT(*d)) { |
6544 | if (++*d <= '9') | |
6545 | return; | |
6546 | *(d--) = '0'; | |
6547 | } | |
6548 | else { | |
9d116dd7 JH |
6549 | #ifdef EBCDIC |
6550 | /* MKS: The original code here died if letters weren't consecutive. | |
6551 | * at least it didn't have to worry about non-C locales. The | |
6552 | * new code assumes that ('z'-'a')==('Z'-'A'), letters are | |
1c846c1f | 6553 | * arranged in order (although not consecutively) and that only |
9d116dd7 JH |
6554 | * [A-Za-z] are accepted by isALPHA in the C locale. |
6555 | */ | |
6556 | if (*d != 'z' && *d != 'Z') { | |
6557 | do { ++*d; } while (!isALPHA(*d)); | |
6558 | return; | |
6559 | } | |
6560 | *(d--) -= 'z' - 'a'; | |
6561 | #else | |
79072805 LW |
6562 | ++*d; |
6563 | if (isALPHA(*d)) | |
6564 | return; | |
6565 | *(d--) -= 'z' - 'a' + 1; | |
9d116dd7 | 6566 | #endif |
79072805 LW |
6567 | } |
6568 | } | |
6569 | /* oh,oh, the number grew */ | |
6570 | SvGROW(sv, SvCUR(sv) + 2); | |
6571 | SvCUR(sv)++; | |
463ee0b2 | 6572 | for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX(sv); d--) |
79072805 LW |
6573 | *d = d[-1]; |
6574 | if (isDIGIT(d[1])) | |
6575 | *d = '1'; | |
6576 | else | |
6577 | *d = d[1]; | |
6578 | } | |
6579 | ||
954c1994 GS |
6580 | /* |
6581 | =for apidoc sv_dec | |
6582 | ||
645c22ef DM |
6583 | Auto-decrement of the value in the SV, doing string to numeric conversion |
6584 | if necessary. Handles 'get' magic. | |
954c1994 GS |
6585 | |
6586 | =cut | |
6587 | */ | |
6588 | ||
79072805 | 6589 | void |
864dbfa3 | 6590 | Perl_sv_dec(pTHX_ register SV *sv) |
79072805 | 6591 | { |
463ee0b2 LW |
6592 | int flags; |
6593 | ||
79072805 LW |
6594 | if (!sv) |
6595 | return; | |
b23a5f78 GB |
6596 | if (SvGMAGICAL(sv)) |
6597 | mg_get(sv); | |
ed6116ce | 6598 | if (SvTHINKFIRST(sv)) { |
3510b4a1 NC |
6599 | if (SvREADONLY(sv) && SvFAKE(sv)) |
6600 | sv_force_normal(sv); | |
0f15f207 | 6601 | if (SvREADONLY(sv)) { |
3280af22 | 6602 | if (PL_curcop != &PL_compiling) |
cea2e8a9 | 6603 | Perl_croak(aTHX_ PL_no_modify); |
0f15f207 | 6604 | } |
a0d0e21e | 6605 | if (SvROK(sv)) { |
b5be31e9 | 6606 | IV i; |
9e7bc3e8 JD |
6607 | if (SvAMAGIC(sv) && AMG_CALLun(sv,dec)) |
6608 | return; | |
56431972 | 6609 | i = PTR2IV(SvRV(sv)); |
b5be31e9 SM |
6610 | sv_unref(sv); |
6611 | sv_setiv(sv, i); | |
a0d0e21e | 6612 | } |
ed6116ce | 6613 | } |
28e5dec8 JH |
6614 | /* Unlike sv_inc we don't have to worry about string-never-numbers |
6615 | and keeping them magic. But we mustn't warn on punting */ | |
8990e307 | 6616 | flags = SvFLAGS(sv); |
28e5dec8 JH |
6617 | if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) { |
6618 | /* It's publicly an integer, or privately an integer-not-float */ | |
59d8ce62 | 6619 | #ifdef PERL_PRESERVE_IVUV |
28e5dec8 | 6620 | oops_its_int: |
59d8ce62 | 6621 | #endif |
25da4f38 IZ |
6622 | if (SvIsUV(sv)) { |
6623 | if (SvUVX(sv) == 0) { | |
6624 | (void)SvIOK_only(sv); | |
6625 | SvIVX(sv) = -1; | |
6626 | } | |
6627 | else { | |
6628 | (void)SvIOK_only_UV(sv); | |
6629 | --SvUVX(sv); | |
1c846c1f | 6630 | } |
25da4f38 IZ |
6631 | } else { |
6632 | if (SvIVX(sv) == IV_MIN) | |
65202027 | 6633 | sv_setnv(sv, (NV)IV_MIN - 1.0); |
25da4f38 IZ |
6634 | else { |
6635 | (void)SvIOK_only(sv); | |
6636 | --SvIVX(sv); | |
1c846c1f | 6637 | } |
55497cff | 6638 | } |
6639 | return; | |
6640 | } | |
28e5dec8 JH |
6641 | if (flags & SVp_NOK) { |
6642 | SvNVX(sv) -= 1.0; | |
6643 | (void)SvNOK_only(sv); | |
6644 | return; | |
6645 | } | |
8990e307 | 6646 | if (!(flags & SVp_POK)) { |
4633a7c4 LW |
6647 | if ((flags & SVTYPEMASK) < SVt_PVNV) |
6648 | sv_upgrade(sv, SVt_NV); | |
463ee0b2 | 6649 | SvNVX(sv) = -1.0; |
a0d0e21e | 6650 | (void)SvNOK_only(sv); |
79072805 LW |
6651 | return; |
6652 | } | |
28e5dec8 JH |
6653 | #ifdef PERL_PRESERVE_IVUV |
6654 | { | |
c2988b20 | 6655 | int numtype = grok_number(SvPVX(sv), SvCUR(sv), NULL); |
28e5dec8 JH |
6656 | if (numtype && !(numtype & IS_NUMBER_INFINITY)) { |
6657 | /* Need to try really hard to see if it's an integer. | |
6658 | 9.22337203685478e+18 is an integer. | |
6659 | but "9.22337203685478e+18" + 0 is UV=9223372036854779904 | |
6660 | so $a="9.22337203685478e+18"; $a+0; $a-- | |
6661 | needs to be the same as $a="9.22337203685478e+18"; $a-- | |
6662 | or we go insane. */ | |
d460ef45 | 6663 | |
28e5dec8 JH |
6664 | (void) sv_2iv(sv); |
6665 | if (SvIOK(sv)) | |
6666 | goto oops_its_int; | |
6667 | ||
6668 | /* sv_2iv *should* have made this an NV */ | |
6669 | if (flags & SVp_NOK) { | |
6670 | (void)SvNOK_only(sv); | |
6671 | SvNVX(sv) -= 1.0; | |
6672 | return; | |
6673 | } | |
6674 | /* I don't think we can get here. Maybe I should assert this | |
6675 | And if we do get here I suspect that sv_setnv will croak. NWC | |
6676 | Fall through. */ | |
6677 | #if defined(USE_LONG_DOUBLE) | |
6678 | 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", | |
6679 | SvPVX(sv), SvIVX(sv), SvNVX(sv))); | |
6680 | #else | |
1779d84d | 6681 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"NVgf"\n", |
28e5dec8 JH |
6682 | SvPVX(sv), SvIVX(sv), SvNVX(sv))); |
6683 | #endif | |
6684 | } | |
6685 | } | |
6686 | #endif /* PERL_PRESERVE_IVUV */ | |
097ee67d | 6687 | sv_setnv(sv,Atof(SvPVX(sv)) - 1.0); /* punt */ |
79072805 LW |
6688 | } |
6689 | ||
954c1994 GS |
6690 | /* |
6691 | =for apidoc sv_mortalcopy | |
6692 | ||
645c22ef | 6693 | Creates a new SV which is a copy of the original SV (using C<sv_setsv>). |
d4236ebc DM |
6694 | The new SV is marked as mortal. It will be destroyed "soon", either by an |
6695 | explicit call to FREETMPS, or by an implicit call at places such as | |
6696 | statement boundaries. See also C<sv_newmortal> and C<sv_2mortal>. | |
954c1994 GS |
6697 | |
6698 | =cut | |
6699 | */ | |
6700 | ||
79072805 LW |
6701 | /* Make a string that will exist for the duration of the expression |
6702 | * evaluation. Actually, it may have to last longer than that, but | |
6703 | * hopefully we won't free it until it has been assigned to a | |
6704 | * permanent location. */ | |
6705 | ||
6706 | SV * | |
864dbfa3 | 6707 | Perl_sv_mortalcopy(pTHX_ SV *oldstr) |
79072805 | 6708 | { |
463ee0b2 | 6709 | register SV *sv; |
b881518d | 6710 | |
4561caa4 | 6711 | new_SV(sv); |
79072805 | 6712 | sv_setsv(sv,oldstr); |
677b06e3 GS |
6713 | EXTEND_MORTAL(1); |
6714 | PL_tmps_stack[++PL_tmps_ix] = sv; | |
8990e307 LW |
6715 | SvTEMP_on(sv); |
6716 | return sv; | |
6717 | } | |
6718 | ||
954c1994 GS |
6719 | /* |
6720 | =for apidoc sv_newmortal | |
6721 | ||
645c22ef | 6722 | Creates a new null SV which is mortal. The reference count of the SV is |
d4236ebc DM |
6723 | set to 1. It will be destroyed "soon", either by an explicit call to |
6724 | FREETMPS, or by an implicit call at places such as statement boundaries. | |
6725 | See also C<sv_mortalcopy> and C<sv_2mortal>. | |
954c1994 GS |
6726 | |
6727 | =cut | |
6728 | */ | |
6729 | ||
8990e307 | 6730 | SV * |
864dbfa3 | 6731 | Perl_sv_newmortal(pTHX) |
8990e307 LW |
6732 | { |
6733 | register SV *sv; | |
6734 | ||
4561caa4 | 6735 | new_SV(sv); |
8990e307 | 6736 | SvFLAGS(sv) = SVs_TEMP; |
677b06e3 GS |
6737 | EXTEND_MORTAL(1); |
6738 | PL_tmps_stack[++PL_tmps_ix] = sv; | |
79072805 LW |
6739 | return sv; |
6740 | } | |
6741 | ||
954c1994 GS |
6742 | /* |
6743 | =for apidoc sv_2mortal | |
6744 | ||
d4236ebc DM |
6745 | Marks an existing SV as mortal. The SV will be destroyed "soon", either |
6746 | by an explicit call to FREETMPS, or by an implicit call at places such as | |
6747 | statement boundaries. See also C<sv_newmortal> and C<sv_mortalcopy>. | |
954c1994 GS |
6748 | |
6749 | =cut | |
6750 | */ | |
6751 | ||
79072805 | 6752 | SV * |
864dbfa3 | 6753 | Perl_sv_2mortal(pTHX_ register SV *sv) |
79072805 LW |
6754 | { |
6755 | if (!sv) | |
6756 | return sv; | |
d689ffdd | 6757 | if (SvREADONLY(sv) && SvIMMORTAL(sv)) |
11162842 | 6758 | return sv; |
677b06e3 GS |
6759 | EXTEND_MORTAL(1); |
6760 | PL_tmps_stack[++PL_tmps_ix] = sv; | |
8990e307 | 6761 | SvTEMP_on(sv); |
79072805 LW |
6762 | return sv; |
6763 | } | |
6764 | ||
954c1994 GS |
6765 | /* |
6766 | =for apidoc newSVpv | |
6767 | ||
6768 | Creates a new SV and copies a string into it. The reference count for the | |
6769 | SV is set to 1. If C<len> is zero, Perl will compute the length using | |
6770 | strlen(). For efficiency, consider using C<newSVpvn> instead. | |
6771 | ||
6772 | =cut | |
6773 | */ | |
6774 | ||
79072805 | 6775 | SV * |
864dbfa3 | 6776 | Perl_newSVpv(pTHX_ const char *s, STRLEN len) |
79072805 | 6777 | { |
463ee0b2 | 6778 | register SV *sv; |
79072805 | 6779 | |
4561caa4 | 6780 | new_SV(sv); |
79072805 LW |
6781 | if (!len) |
6782 | len = strlen(s); | |
6783 | sv_setpvn(sv,s,len); | |
6784 | return sv; | |
6785 | } | |
6786 | ||
954c1994 GS |
6787 | /* |
6788 | =for apidoc newSVpvn | |
6789 | ||
6790 | Creates a new SV and copies a string into it. The reference count for the | |
1c846c1f | 6791 | SV is set to 1. Note that if C<len> is zero, Perl will create a zero length |
954c1994 GS |
6792 | string. You are responsible for ensuring that the source string is at least |
6793 | C<len> bytes long. | |
6794 | ||
6795 | =cut | |
6796 | */ | |
6797 | ||
9da1e3b5 | 6798 | SV * |
864dbfa3 | 6799 | Perl_newSVpvn(pTHX_ const char *s, STRLEN len) |
9da1e3b5 MUN |
6800 | { |
6801 | register SV *sv; | |
6802 | ||
6803 | new_SV(sv); | |
9da1e3b5 MUN |
6804 | sv_setpvn(sv,s,len); |
6805 | return sv; | |
6806 | } | |
6807 | ||
1c846c1f NIS |
6808 | /* |
6809 | =for apidoc newSVpvn_share | |
6810 | ||
645c22ef DM |
6811 | Creates a new SV with its SvPVX pointing to a shared string in the string |
6812 | table. If the string does not already exist in the table, it is created | |
6813 | first. Turns on READONLY and FAKE. The string's hash is stored in the UV | |
6814 | slot of the SV; if the C<hash> parameter is non-zero, that value is used; | |
6815 | otherwise the hash is computed. The idea here is that as the string table | |
6816 | is used for shared hash keys these strings will have SvPVX == HeKEY and | |
6817 | hash lookup will avoid string compare. | |
1c846c1f NIS |
6818 | |
6819 | =cut | |
6820 | */ | |
6821 | ||
6822 | SV * | |
c3654f1a | 6823 | Perl_newSVpvn_share(pTHX_ const char *src, I32 len, U32 hash) |
1c846c1f NIS |
6824 | { |
6825 | register SV *sv; | |
c3654f1a IH |
6826 | bool is_utf8 = FALSE; |
6827 | if (len < 0) { | |
77caf834 | 6828 | STRLEN tmplen = -len; |
c3654f1a | 6829 | is_utf8 = TRUE; |
75a54232 JH |
6830 | /* See the note in hv.c:hv_fetch() --jhi */ |
6831 | src = (char*)bytes_from_utf8((U8*)src, &tmplen, &is_utf8); | |
6832 | len = tmplen; | |
6833 | } | |
1c846c1f | 6834 | if (!hash) |
5afd6d42 | 6835 | PERL_HASH(hash, src, len); |
1c846c1f NIS |
6836 | new_SV(sv); |
6837 | sv_upgrade(sv, SVt_PVIV); | |
c3654f1a | 6838 | SvPVX(sv) = sharepvn(src, is_utf8?-len:len, hash); |
1c846c1f NIS |
6839 | SvCUR(sv) = len; |
6840 | SvUVX(sv) = hash; | |
6841 | SvLEN(sv) = 0; | |
6842 | SvREADONLY_on(sv); | |
6843 | SvFAKE_on(sv); | |
6844 | SvPOK_on(sv); | |
c3654f1a IH |
6845 | if (is_utf8) |
6846 | SvUTF8_on(sv); | |
1c846c1f NIS |
6847 | return sv; |
6848 | } | |
6849 | ||
645c22ef | 6850 | |
cea2e8a9 | 6851 | #if defined(PERL_IMPLICIT_CONTEXT) |
645c22ef DM |
6852 | |
6853 | /* pTHX_ magic can't cope with varargs, so this is a no-context | |
6854 | * version of the main function, (which may itself be aliased to us). | |
6855 | * Don't access this version directly. | |
6856 | */ | |
6857 | ||
46fc3d4c | 6858 | SV * |
cea2e8a9 | 6859 | Perl_newSVpvf_nocontext(const char* pat, ...) |
46fc3d4c | 6860 | { |
cea2e8a9 | 6861 | dTHX; |
46fc3d4c | 6862 | register SV *sv; |
6863 | va_list args; | |
46fc3d4c | 6864 | va_start(args, pat); |
c5be433b | 6865 | sv = vnewSVpvf(pat, &args); |
46fc3d4c | 6866 | va_end(args); |
6867 | return sv; | |
6868 | } | |
cea2e8a9 | 6869 | #endif |
46fc3d4c | 6870 | |
954c1994 GS |
6871 | /* |
6872 | =for apidoc newSVpvf | |
6873 | ||
645c22ef | 6874 | Creates a new SV and initializes it with the string formatted like |
954c1994 GS |
6875 | C<sprintf>. |
6876 | ||
6877 | =cut | |
6878 | */ | |
6879 | ||
cea2e8a9 GS |
6880 | SV * |
6881 | Perl_newSVpvf(pTHX_ const char* pat, ...) | |
6882 | { | |
6883 | register SV *sv; | |
6884 | va_list args; | |
cea2e8a9 | 6885 | va_start(args, pat); |
c5be433b | 6886 | sv = vnewSVpvf(pat, &args); |
cea2e8a9 GS |
6887 | va_end(args); |
6888 | return sv; | |
6889 | } | |
46fc3d4c | 6890 | |
645c22ef DM |
6891 | /* backend for newSVpvf() and newSVpvf_nocontext() */ |
6892 | ||
79072805 | 6893 | SV * |
c5be433b GS |
6894 | Perl_vnewSVpvf(pTHX_ const char* pat, va_list* args) |
6895 | { | |
6896 | register SV *sv; | |
6897 | new_SV(sv); | |
6898 | sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
6899 | return sv; | |
6900 | } | |
6901 | ||
954c1994 GS |
6902 | /* |
6903 | =for apidoc newSVnv | |
6904 | ||
6905 | Creates a new SV and copies a floating point value into it. | |
6906 | The reference count for the SV is set to 1. | |
6907 | ||
6908 | =cut | |
6909 | */ | |
6910 | ||
c5be433b | 6911 | SV * |
65202027 | 6912 | Perl_newSVnv(pTHX_ NV n) |
79072805 | 6913 | { |
463ee0b2 | 6914 | register SV *sv; |
79072805 | 6915 | |
4561caa4 | 6916 | new_SV(sv); |
79072805 LW |
6917 | sv_setnv(sv,n); |
6918 | return sv; | |
6919 | } | |
6920 | ||
954c1994 GS |
6921 | /* |
6922 | =for apidoc newSViv | |
6923 | ||
6924 | Creates a new SV and copies an integer into it. The reference count for the | |
6925 | SV is set to 1. | |
6926 | ||
6927 | =cut | |
6928 | */ | |
6929 | ||
79072805 | 6930 | SV * |
864dbfa3 | 6931 | Perl_newSViv(pTHX_ IV i) |
79072805 | 6932 | { |
463ee0b2 | 6933 | register SV *sv; |
79072805 | 6934 | |
4561caa4 | 6935 | new_SV(sv); |
79072805 LW |
6936 | sv_setiv(sv,i); |
6937 | return sv; | |
6938 | } | |
6939 | ||
954c1994 | 6940 | /* |
1a3327fb JH |
6941 | =for apidoc newSVuv |
6942 | ||
6943 | Creates a new SV and copies an unsigned integer into it. | |
6944 | The reference count for the SV is set to 1. | |
6945 | ||
6946 | =cut | |
6947 | */ | |
6948 | ||
6949 | SV * | |
6950 | Perl_newSVuv(pTHX_ UV u) | |
6951 | { | |
6952 | register SV *sv; | |
6953 | ||
6954 | new_SV(sv); | |
6955 | sv_setuv(sv,u); | |
6956 | return sv; | |
6957 | } | |
6958 | ||
6959 | /* | |
954c1994 GS |
6960 | =for apidoc newRV_noinc |
6961 | ||
6962 | Creates an RV wrapper for an SV. The reference count for the original | |
6963 | SV is B<not> incremented. | |
6964 | ||
6965 | =cut | |
6966 | */ | |
6967 | ||
2304df62 | 6968 | SV * |
864dbfa3 | 6969 | Perl_newRV_noinc(pTHX_ SV *tmpRef) |
2304df62 AD |
6970 | { |
6971 | register SV *sv; | |
6972 | ||
4561caa4 | 6973 | new_SV(sv); |
2304df62 | 6974 | sv_upgrade(sv, SVt_RV); |
76e3520e | 6975 | SvTEMP_off(tmpRef); |
d689ffdd | 6976 | SvRV(sv) = tmpRef; |
2304df62 | 6977 | SvROK_on(sv); |
2304df62 AD |
6978 | return sv; |
6979 | } | |
6980 | ||
ff276b08 | 6981 | /* newRV_inc is the official function name to use now. |
645c22ef DM |
6982 | * newRV_inc is in fact #defined to newRV in sv.h |
6983 | */ | |
6984 | ||
5f05dabc | 6985 | SV * |
864dbfa3 | 6986 | Perl_newRV(pTHX_ SV *tmpRef) |
5f05dabc | 6987 | { |
5f6447b6 | 6988 | return newRV_noinc(SvREFCNT_inc(tmpRef)); |
5f05dabc | 6989 | } |
5f05dabc | 6990 | |
954c1994 GS |
6991 | /* |
6992 | =for apidoc newSVsv | |
6993 | ||
6994 | Creates a new SV which is an exact duplicate of the original SV. | |
645c22ef | 6995 | (Uses C<sv_setsv>). |
954c1994 GS |
6996 | |
6997 | =cut | |
6998 | */ | |
6999 | ||
79072805 | 7000 | SV * |
864dbfa3 | 7001 | Perl_newSVsv(pTHX_ register SV *old) |
79072805 | 7002 | { |
463ee0b2 | 7003 | register SV *sv; |
79072805 LW |
7004 | |
7005 | if (!old) | |
7006 | return Nullsv; | |
8990e307 | 7007 | if (SvTYPE(old) == SVTYPEMASK) { |
0453d815 | 7008 | if (ckWARN_d(WARN_INTERNAL)) |
9014280d | 7009 | Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "semi-panic: attempt to dup freed string"); |
79072805 LW |
7010 | return Nullsv; |
7011 | } | |
4561caa4 | 7012 | new_SV(sv); |
ff68c719 | 7013 | if (SvTEMP(old)) { |
7014 | SvTEMP_off(old); | |
463ee0b2 | 7015 | sv_setsv(sv,old); |
ff68c719 | 7016 | SvTEMP_on(old); |
79072805 LW |
7017 | } |
7018 | else | |
463ee0b2 LW |
7019 | sv_setsv(sv,old); |
7020 | return sv; | |
79072805 LW |
7021 | } |
7022 | ||
645c22ef DM |
7023 | /* |
7024 | =for apidoc sv_reset | |
7025 | ||
7026 | Underlying implementation for the C<reset> Perl function. | |
7027 | Note that the perl-level function is vaguely deprecated. | |
7028 | ||
7029 | =cut | |
7030 | */ | |
7031 | ||
79072805 | 7032 | void |
864dbfa3 | 7033 | Perl_sv_reset(pTHX_ register char *s, HV *stash) |
79072805 LW |
7034 | { |
7035 | register HE *entry; | |
7036 | register GV *gv; | |
7037 | register SV *sv; | |
7038 | register I32 i; | |
7039 | register PMOP *pm; | |
7040 | register I32 max; | |
4802d5d7 | 7041 | char todo[PERL_UCHAR_MAX+1]; |
79072805 | 7042 | |
49d8d3a1 MB |
7043 | if (!stash) |
7044 | return; | |
7045 | ||
79072805 LW |
7046 | if (!*s) { /* reset ?? searches */ |
7047 | for (pm = HvPMROOT(stash); pm; pm = pm->op_pmnext) { | |
48c036b1 | 7048 | pm->op_pmdynflags &= ~PMdf_USED; |
79072805 LW |
7049 | } |
7050 | return; | |
7051 | } | |
7052 | ||
7053 | /* reset variables */ | |
7054 | ||
7055 | if (!HvARRAY(stash)) | |
7056 | return; | |
463ee0b2 LW |
7057 | |
7058 | Zero(todo, 256, char); | |
79072805 | 7059 | while (*s) { |
4802d5d7 | 7060 | i = (unsigned char)*s; |
79072805 LW |
7061 | if (s[1] == '-') { |
7062 | s += 2; | |
7063 | } | |
4802d5d7 | 7064 | max = (unsigned char)*s++; |
79072805 | 7065 | for ( ; i <= max; i++) { |
463ee0b2 LW |
7066 | todo[i] = 1; |
7067 | } | |
a0d0e21e | 7068 | for (i = 0; i <= (I32) HvMAX(stash); i++) { |
79072805 | 7069 | for (entry = HvARRAY(stash)[i]; |
9e35f4b3 GS |
7070 | entry; |
7071 | entry = HeNEXT(entry)) | |
7072 | { | |
1edc1566 | 7073 | if (!todo[(U8)*HeKEY(entry)]) |
463ee0b2 | 7074 | continue; |
1edc1566 | 7075 | gv = (GV*)HeVAL(entry); |
79072805 | 7076 | sv = GvSV(gv); |
9e35f4b3 GS |
7077 | if (SvTHINKFIRST(sv)) { |
7078 | if (!SvREADONLY(sv) && SvROK(sv)) | |
7079 | sv_unref(sv); | |
7080 | continue; | |
7081 | } | |
a0d0e21e | 7082 | (void)SvOK_off(sv); |
79072805 LW |
7083 | if (SvTYPE(sv) >= SVt_PV) { |
7084 | SvCUR_set(sv, 0); | |
463ee0b2 LW |
7085 | if (SvPVX(sv) != Nullch) |
7086 | *SvPVX(sv) = '\0'; | |
44a8e56a | 7087 | SvTAINT(sv); |
79072805 LW |
7088 | } |
7089 | if (GvAV(gv)) { | |
7090 | av_clear(GvAV(gv)); | |
7091 | } | |
44a8e56a | 7092 | if (GvHV(gv) && !HvNAME(GvHV(gv))) { |
463ee0b2 | 7093 | hv_clear(GvHV(gv)); |
75a5c1c6 | 7094 | #ifndef PERL_MICRO |
fa6a1c44 | 7095 | #ifdef USE_ENVIRON_ARRAY |
4efc5df6 GS |
7096 | if (gv == PL_envgv |
7097 | # ifdef USE_ITHREADS | |
7098 | && PL_curinterp == aTHX | |
7099 | # endif | |
7100 | ) | |
7101 | { | |
79072805 | 7102 | environ[0] = Nullch; |
4efc5df6 | 7103 | } |
a0d0e21e | 7104 | #endif |
75a5c1c6 | 7105 | #endif /* !PERL_MICRO */ |
79072805 LW |
7106 | } |
7107 | } | |
7108 | } | |
7109 | } | |
7110 | } | |
7111 | ||
645c22ef DM |
7112 | /* |
7113 | =for apidoc sv_2io | |
7114 | ||
7115 | Using various gambits, try to get an IO from an SV: the IO slot if its a | |
7116 | GV; or the recursive result if we're an RV; or the IO slot of the symbol | |
7117 | named after the PV if we're a string. | |
7118 | ||
7119 | =cut | |
7120 | */ | |
7121 | ||
46fc3d4c | 7122 | IO* |
864dbfa3 | 7123 | Perl_sv_2io(pTHX_ SV *sv) |
46fc3d4c | 7124 | { |
7125 | IO* io; | |
7126 | GV* gv; | |
2d8e6c8d | 7127 | STRLEN n_a; |
46fc3d4c | 7128 | |
7129 | switch (SvTYPE(sv)) { | |
7130 | case SVt_PVIO: | |
7131 | io = (IO*)sv; | |
7132 | break; | |
7133 | case SVt_PVGV: | |
7134 | gv = (GV*)sv; | |
7135 | io = GvIO(gv); | |
7136 | if (!io) | |
cea2e8a9 | 7137 | Perl_croak(aTHX_ "Bad filehandle: %s", GvNAME(gv)); |
46fc3d4c | 7138 | break; |
7139 | default: | |
7140 | if (!SvOK(sv)) | |
cea2e8a9 | 7141 | Perl_croak(aTHX_ PL_no_usym, "filehandle"); |
46fc3d4c | 7142 | if (SvROK(sv)) |
7143 | return sv_2io(SvRV(sv)); | |
2d8e6c8d | 7144 | gv = gv_fetchpv(SvPV(sv,n_a), FALSE, SVt_PVIO); |
46fc3d4c | 7145 | if (gv) |
7146 | io = GvIO(gv); | |
7147 | else | |
7148 | io = 0; | |
7149 | if (!io) | |
c293eb2b | 7150 | Perl_croak(aTHX_ "Bad filehandle: %"SVf, sv); |
46fc3d4c | 7151 | break; |
7152 | } | |
7153 | return io; | |
7154 | } | |
7155 | ||
645c22ef DM |
7156 | /* |
7157 | =for apidoc sv_2cv | |
7158 | ||
7159 | Using various gambits, try to get a CV from an SV; in addition, try if | |
7160 | possible to set C<*st> and C<*gvp> to the stash and GV associated with it. | |
7161 | ||
7162 | =cut | |
7163 | */ | |
7164 | ||
79072805 | 7165 | CV * |
864dbfa3 | 7166 | Perl_sv_2cv(pTHX_ SV *sv, HV **st, GV **gvp, I32 lref) |
79072805 | 7167 | { |
c04a4dfe JH |
7168 | GV *gv = Nullgv; |
7169 | CV *cv = Nullcv; | |
2d8e6c8d | 7170 | STRLEN n_a; |
79072805 LW |
7171 | |
7172 | if (!sv) | |
93a17b20 | 7173 | return *gvp = Nullgv, Nullcv; |
79072805 | 7174 | switch (SvTYPE(sv)) { |
79072805 LW |
7175 | case SVt_PVCV: |
7176 | *st = CvSTASH(sv); | |
7177 | *gvp = Nullgv; | |
7178 | return (CV*)sv; | |
7179 | case SVt_PVHV: | |
7180 | case SVt_PVAV: | |
7181 | *gvp = Nullgv; | |
7182 | return Nullcv; | |
8990e307 LW |
7183 | case SVt_PVGV: |
7184 | gv = (GV*)sv; | |
a0d0e21e | 7185 | *gvp = gv; |
8990e307 LW |
7186 | *st = GvESTASH(gv); |
7187 | goto fix_gv; | |
7188 | ||
79072805 | 7189 | default: |
a0d0e21e LW |
7190 | if (SvGMAGICAL(sv)) |
7191 | mg_get(sv); | |
7192 | if (SvROK(sv)) { | |
f5284f61 IZ |
7193 | SV **sp = &sv; /* Used in tryAMAGICunDEREF macro. */ |
7194 | tryAMAGICunDEREF(to_cv); | |
7195 | ||
62f274bf GS |
7196 | sv = SvRV(sv); |
7197 | if (SvTYPE(sv) == SVt_PVCV) { | |
7198 | cv = (CV*)sv; | |
7199 | *gvp = Nullgv; | |
7200 | *st = CvSTASH(cv); | |
7201 | return cv; | |
7202 | } | |
7203 | else if(isGV(sv)) | |
7204 | gv = (GV*)sv; | |
7205 | else | |
cea2e8a9 | 7206 | Perl_croak(aTHX_ "Not a subroutine reference"); |
a0d0e21e | 7207 | } |
62f274bf | 7208 | else if (isGV(sv)) |
79072805 LW |
7209 | gv = (GV*)sv; |
7210 | else | |
2d8e6c8d | 7211 | gv = gv_fetchpv(SvPV(sv, n_a), lref, SVt_PVCV); |
79072805 LW |
7212 | *gvp = gv; |
7213 | if (!gv) | |
7214 | return Nullcv; | |
7215 | *st = GvESTASH(gv); | |
8990e307 | 7216 | fix_gv: |
8ebc5c01 | 7217 | if (lref && !GvCVu(gv)) { |
4633a7c4 | 7218 | SV *tmpsv; |
748a9306 | 7219 | ENTER; |
4633a7c4 | 7220 | tmpsv = NEWSV(704,0); |
16660edb | 7221 | gv_efullname3(tmpsv, gv, Nullch); |
f6ec51f7 GS |
7222 | /* XXX this is probably not what they think they're getting. |
7223 | * It has the same effect as "sub name;", i.e. just a forward | |
7224 | * declaration! */ | |
774d564b | 7225 | newSUB(start_subparse(FALSE, 0), |
4633a7c4 LW |
7226 | newSVOP(OP_CONST, 0, tmpsv), |
7227 | Nullop, | |
8990e307 | 7228 | Nullop); |
748a9306 | 7229 | LEAVE; |
8ebc5c01 | 7230 | if (!GvCVu(gv)) |
c293eb2b NC |
7231 | Perl_croak(aTHX_ "Unable to create sub named \"%"SVf"\"", |
7232 | sv); | |
8990e307 | 7233 | } |
8ebc5c01 | 7234 | return GvCVu(gv); |
79072805 LW |
7235 | } |
7236 | } | |
7237 | ||
c461cf8f JH |
7238 | /* |
7239 | =for apidoc sv_true | |
7240 | ||
7241 | Returns true if the SV has a true value by Perl's rules. | |
645c22ef DM |
7242 | Use the C<SvTRUE> macro instead, which may call C<sv_true()> or may |
7243 | instead use an in-line version. | |
c461cf8f JH |
7244 | |
7245 | =cut | |
7246 | */ | |
7247 | ||
79072805 | 7248 | I32 |
864dbfa3 | 7249 | Perl_sv_true(pTHX_ register SV *sv) |
79072805 | 7250 | { |
8990e307 LW |
7251 | if (!sv) |
7252 | return 0; | |
79072805 | 7253 | if (SvPOK(sv)) { |
4e35701f NIS |
7254 | register XPV* tXpv; |
7255 | if ((tXpv = (XPV*)SvANY(sv)) && | |
c2f1de04 | 7256 | (tXpv->xpv_cur > 1 || |
4e35701f | 7257 | (tXpv->xpv_cur && *tXpv->xpv_pv != '0'))) |
79072805 LW |
7258 | return 1; |
7259 | else | |
7260 | return 0; | |
7261 | } | |
7262 | else { | |
7263 | if (SvIOK(sv)) | |
463ee0b2 | 7264 | return SvIVX(sv) != 0; |
79072805 LW |
7265 | else { |
7266 | if (SvNOK(sv)) | |
463ee0b2 | 7267 | return SvNVX(sv) != 0.0; |
79072805 | 7268 | else |
463ee0b2 | 7269 | return sv_2bool(sv); |
79072805 LW |
7270 | } |
7271 | } | |
7272 | } | |
79072805 | 7273 | |
645c22ef DM |
7274 | /* |
7275 | =for apidoc sv_iv | |
7276 | ||
7277 | A private implementation of the C<SvIVx> macro for compilers which can't | |
7278 | cope with complex macro expressions. Always use the macro instead. | |
7279 | ||
7280 | =cut | |
7281 | */ | |
7282 | ||
ff68c719 | 7283 | IV |
864dbfa3 | 7284 | Perl_sv_iv(pTHX_ register SV *sv) |
85e6fe83 | 7285 | { |
25da4f38 IZ |
7286 | if (SvIOK(sv)) { |
7287 | if (SvIsUV(sv)) | |
7288 | return (IV)SvUVX(sv); | |
ff68c719 | 7289 | return SvIVX(sv); |
25da4f38 | 7290 | } |
ff68c719 | 7291 | return sv_2iv(sv); |
85e6fe83 | 7292 | } |
85e6fe83 | 7293 | |
645c22ef DM |
7294 | /* |
7295 | =for apidoc sv_uv | |
7296 | ||
7297 | A private implementation of the C<SvUVx> macro for compilers which can't | |
7298 | cope with complex macro expressions. Always use the macro instead. | |
7299 | ||
7300 | =cut | |
7301 | */ | |
7302 | ||
ff68c719 | 7303 | UV |
864dbfa3 | 7304 | Perl_sv_uv(pTHX_ register SV *sv) |
ff68c719 | 7305 | { |
25da4f38 IZ |
7306 | if (SvIOK(sv)) { |
7307 | if (SvIsUV(sv)) | |
7308 | return SvUVX(sv); | |
7309 | return (UV)SvIVX(sv); | |
7310 | } | |
ff68c719 | 7311 | return sv_2uv(sv); |
7312 | } | |
85e6fe83 | 7313 | |
645c22ef DM |
7314 | /* |
7315 | =for apidoc sv_nv | |
7316 | ||
7317 | A private implementation of the C<SvNVx> macro for compilers which can't | |
7318 | cope with complex macro expressions. Always use the macro instead. | |
7319 | ||
7320 | =cut | |
7321 | */ | |
7322 | ||
65202027 | 7323 | NV |
864dbfa3 | 7324 | Perl_sv_nv(pTHX_ register SV *sv) |
79072805 | 7325 | { |
ff68c719 | 7326 | if (SvNOK(sv)) |
7327 | return SvNVX(sv); | |
7328 | return sv_2nv(sv); | |
79072805 | 7329 | } |
79072805 | 7330 | |
d34f9d2e JH |
7331 | /* sv_pv() is now a macro using SvPV_nolen(); |
7332 | * this function provided for binary compatibility only | |
7333 | */ | |
7334 | ||
7335 | char * | |
7336 | Perl_sv_pv(pTHX_ SV *sv) | |
7337 | { | |
7338 | STRLEN n_a; | |
7339 | ||
7340 | if (SvPOK(sv)) | |
7341 | return SvPVX(sv); | |
7342 | ||
7343 | return sv_2pv(sv, &n_a); | |
7344 | } | |
7345 | ||
645c22ef DM |
7346 | /* |
7347 | =for apidoc sv_pv | |
7348 | ||
baca2b92 | 7349 | Use the C<SvPV_nolen> macro instead |
645c22ef | 7350 | |
645c22ef DM |
7351 | =for apidoc sv_pvn |
7352 | ||
7353 | A private implementation of the C<SvPV> macro for compilers which can't | |
7354 | cope with complex macro expressions. Always use the macro instead. | |
7355 | ||
7356 | =cut | |
7357 | */ | |
7358 | ||
1fa8b10d | 7359 | char * |
864dbfa3 | 7360 | Perl_sv_pvn(pTHX_ SV *sv, STRLEN *lp) |
79072805 | 7361 | { |
85e6fe83 LW |
7362 | if (SvPOK(sv)) { |
7363 | *lp = SvCUR(sv); | |
a0d0e21e | 7364 | return SvPVX(sv); |
85e6fe83 | 7365 | } |
463ee0b2 | 7366 | return sv_2pv(sv, lp); |
79072805 | 7367 | } |
79072805 | 7368 | |
6e9d1081 NC |
7369 | |
7370 | char * | |
7371 | Perl_sv_pvn_nomg(pTHX_ register SV *sv, STRLEN *lp) | |
7372 | { | |
7373 | if (SvPOK(sv)) { | |
7374 | *lp = SvCUR(sv); | |
7375 | return SvPVX(sv); | |
7376 | } | |
7377 | return sv_2pv_flags(sv, lp, 0); | |
7378 | } | |
7379 | ||
d34f9d2e JH |
7380 | /* sv_pvn_force() is now a macro using Perl_sv_pvn_force_flags(); |
7381 | * this function provided for binary compatibility only | |
7382 | */ | |
7383 | ||
7384 | char * | |
7385 | Perl_sv_pvn_force(pTHX_ SV *sv, STRLEN *lp) | |
7386 | { | |
7387 | return sv_pvn_force_flags(sv, lp, SV_GMAGIC); | |
7388 | } | |
7389 | ||
c461cf8f JH |
7390 | /* |
7391 | =for apidoc sv_pvn_force | |
7392 | ||
7393 | Get a sensible string out of the SV somehow. | |
645c22ef DM |
7394 | A private implementation of the C<SvPV_force> macro for compilers which |
7395 | can't cope with complex macro expressions. Always use the macro instead. | |
c461cf8f | 7396 | |
8d6d96c1 HS |
7397 | =for apidoc sv_pvn_force_flags |
7398 | ||
7399 | Get a sensible string out of the SV somehow. | |
7400 | If C<flags> has C<SV_GMAGIC> bit set, will C<mg_get> on C<sv> if | |
7401 | appropriate, else not. C<sv_pvn_force> and C<sv_pvn_force_nomg> are | |
7402 | implemented in terms of this function. | |
645c22ef DM |
7403 | You normally want to use the various wrapper macros instead: see |
7404 | C<SvPV_force> and C<SvPV_force_nomg> | |
8d6d96c1 HS |
7405 | |
7406 | =cut | |
7407 | */ | |
7408 | ||
7409 | char * | |
7410 | Perl_sv_pvn_force_flags(pTHX_ SV *sv, STRLEN *lp, I32 flags) | |
7411 | { | |
c04a4dfe | 7412 | char *s = NULL; |
a0d0e21e | 7413 | |
6fc92669 GS |
7414 | if (SvTHINKFIRST(sv) && !SvROK(sv)) |
7415 | sv_force_normal(sv); | |
1c846c1f | 7416 | |
a0d0e21e LW |
7417 | if (SvPOK(sv)) { |
7418 | *lp = SvCUR(sv); | |
7419 | } | |
7420 | else { | |
748a9306 | 7421 | if (SvTYPE(sv) > SVt_PVLV && SvTYPE(sv) != SVt_PVFM) { |
cea2e8a9 | 7422 | Perl_croak(aTHX_ "Can't coerce %s to string in %s", sv_reftype(sv,0), |
53e06cf0 | 7423 | OP_NAME(PL_op)); |
a0d0e21e | 7424 | } |
4633a7c4 | 7425 | else |
8d6d96c1 | 7426 | s = sv_2pv_flags(sv, lp, flags); |
a0d0e21e LW |
7427 | if (s != SvPVX(sv)) { /* Almost, but not quite, sv_setpvn() */ |
7428 | STRLEN len = *lp; | |
1c846c1f | 7429 | |
a0d0e21e LW |
7430 | if (SvROK(sv)) |
7431 | sv_unref(sv); | |
7432 | (void)SvUPGRADE(sv, SVt_PV); /* Never FALSE */ | |
7433 | SvGROW(sv, len + 1); | |
7434 | Move(s,SvPVX(sv),len,char); | |
7435 | SvCUR_set(sv, len); | |
7436 | *SvEND(sv) = '\0'; | |
7437 | } | |
7438 | if (!SvPOK(sv)) { | |
7439 | SvPOK_on(sv); /* validate pointer */ | |
7440 | SvTAINT(sv); | |
1d7c1841 GS |
7441 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n", |
7442 | PTR2UV(sv),SvPVX(sv))); | |
a0d0e21e LW |
7443 | } |
7444 | } | |
7445 | return SvPVX(sv); | |
7446 | } | |
7447 | ||
d34f9d2e JH |
7448 | /* sv_pvbyte () is now a macro using Perl_sv_2pv_flags(); |
7449 | * this function provided for binary compatibility only | |
7450 | */ | |
7451 | ||
7452 | char * | |
7453 | Perl_sv_pvbyte(pTHX_ SV *sv) | |
7454 | { | |
7455 | sv_utf8_downgrade(sv,0); | |
7456 | return sv_pv(sv); | |
7457 | } | |
7458 | ||
645c22ef DM |
7459 | /* |
7460 | =for apidoc sv_pvbyte | |
7461 | ||
baca2b92 | 7462 | Use C<SvPVbyte_nolen> instead. |
645c22ef | 7463 | |
645c22ef DM |
7464 | =for apidoc sv_pvbyten |
7465 | ||
7466 | A private implementation of the C<SvPVbyte> macro for compilers | |
7467 | which can't cope with complex macro expressions. Always use the macro | |
7468 | instead. | |
7469 | ||
7470 | =cut | |
7471 | */ | |
7472 | ||
7340a771 GS |
7473 | char * |
7474 | Perl_sv_pvbyten(pTHX_ SV *sv, STRLEN *lp) | |
7475 | { | |
ffebcc3e | 7476 | sv_utf8_downgrade(sv,0); |
7340a771 GS |
7477 | return sv_pvn(sv,lp); |
7478 | } | |
7479 | ||
645c22ef DM |
7480 | /* |
7481 | =for apidoc sv_pvbyten_force | |
7482 | ||
7483 | A private implementation of the C<SvPVbytex_force> macro for compilers | |
7484 | which can't cope with complex macro expressions. Always use the macro | |
7485 | instead. | |
7486 | ||
7487 | =cut | |
7488 | */ | |
7489 | ||
7340a771 GS |
7490 | char * |
7491 | Perl_sv_pvbyten_force(pTHX_ SV *sv, STRLEN *lp) | |
7492 | { | |
ffebcc3e | 7493 | sv_utf8_downgrade(sv,0); |
7340a771 GS |
7494 | return sv_pvn_force(sv,lp); |
7495 | } | |
7496 | ||
d34f9d2e JH |
7497 | /* sv_pvutf8 () is now a macro using Perl_sv_2pv_flags(); |
7498 | * this function provided for binary compatibility only | |
7499 | */ | |
7500 | ||
7501 | char * | |
7502 | Perl_sv_pvutf8(pTHX_ SV *sv) | |
7503 | { | |
7504 | sv_utf8_upgrade(sv); | |
7505 | return sv_pv(sv); | |
7506 | } | |
7507 | ||
645c22ef DM |
7508 | /* |
7509 | =for apidoc sv_pvutf8 | |
7510 | ||
baca2b92 | 7511 | Use the C<SvPVutf8_nolen> macro instead |
645c22ef | 7512 | |
645c22ef DM |
7513 | =for apidoc sv_pvutf8n |
7514 | ||
7515 | A private implementation of the C<SvPVutf8> macro for compilers | |
7516 | which can't cope with complex macro expressions. Always use the macro | |
7517 | instead. | |
7518 | ||
7519 | =cut | |
7520 | */ | |
7521 | ||
7340a771 GS |
7522 | char * |
7523 | Perl_sv_pvutf8n(pTHX_ SV *sv, STRLEN *lp) | |
7524 | { | |
560a288e | 7525 | sv_utf8_upgrade(sv); |
7340a771 GS |
7526 | return sv_pvn(sv,lp); |
7527 | } | |
7528 | ||
c461cf8f JH |
7529 | /* |
7530 | =for apidoc sv_pvutf8n_force | |
7531 | ||
645c22ef DM |
7532 | A private implementation of the C<SvPVutf8_force> macro for compilers |
7533 | which can't cope with complex macro expressions. Always use the macro | |
7534 | instead. | |
c461cf8f JH |
7535 | |
7536 | =cut | |
7537 | */ | |
7538 | ||
7340a771 GS |
7539 | char * |
7540 | Perl_sv_pvutf8n_force(pTHX_ SV *sv, STRLEN *lp) | |
7541 | { | |
560a288e | 7542 | sv_utf8_upgrade(sv); |
7340a771 GS |
7543 | return sv_pvn_force(sv,lp); |
7544 | } | |
7545 | ||
c461cf8f JH |
7546 | /* |
7547 | =for apidoc sv_reftype | |
7548 | ||
7549 | Returns a string describing what the SV is a reference to. | |
7550 | ||
7551 | =cut | |
7552 | */ | |
7553 | ||
7340a771 | 7554 | char * |
864dbfa3 | 7555 | Perl_sv_reftype(pTHX_ SV *sv, int ob) |
a0d0e21e | 7556 | { |
c86bf373 AMS |
7557 | if (ob && SvOBJECT(sv)) { |
7558 | HV *svs = SvSTASH(sv); | |
7559 | /* [20011101.072] This bandaid for C<package;> should eventually | |
7560 | be removed. AMS 20011103 */ | |
7561 | return (svs ? HvNAME(svs) : "<none>"); | |
7562 | } | |
a0d0e21e LW |
7563 | else { |
7564 | switch (SvTYPE(sv)) { | |
7565 | case SVt_NULL: | |
7566 | case SVt_IV: | |
7567 | case SVt_NV: | |
7568 | case SVt_RV: | |
7569 | case SVt_PV: | |
7570 | case SVt_PVIV: | |
7571 | case SVt_PVNV: | |
7572 | case SVt_PVMG: | |
7573 | case SVt_PVBM: | |
7574 | if (SvROK(sv)) | |
7575 | return "REF"; | |
7576 | else | |
7577 | return "SCALAR"; | |
ac388100 JH |
7578 | |
7579 | case SVt_PVLV: return SvROK(sv) ? "REF" | |
7580 | /* tied lvalues should appear to be | |
7581 | * scalars for backwards compatitbility */ | |
7582 | : (LvTYPE(sv) == 't' || LvTYPE(sv) == 'T') | |
7583 | ? "SCALAR" : "LVALUE"; | |
a0d0e21e LW |
7584 | case SVt_PVAV: return "ARRAY"; |
7585 | case SVt_PVHV: return "HASH"; | |
7586 | case SVt_PVCV: return "CODE"; | |
7587 | case SVt_PVGV: return "GLOB"; | |
1d2dff63 | 7588 | case SVt_PVFM: return "FORMAT"; |
27f9d8f3 | 7589 | case SVt_PVIO: return "IO"; |
a0d0e21e LW |
7590 | default: return "UNKNOWN"; |
7591 | } | |
7592 | } | |
7593 | } | |
7594 | ||
954c1994 GS |
7595 | /* |
7596 | =for apidoc sv_isobject | |
7597 | ||
7598 | Returns a boolean indicating whether the SV is an RV pointing to a blessed | |
7599 | object. If the SV is not an RV, or if the object is not blessed, then this | |
7600 | will return false. | |
7601 | ||
7602 | =cut | |
7603 | */ | |
7604 | ||
463ee0b2 | 7605 | int |
864dbfa3 | 7606 | Perl_sv_isobject(pTHX_ SV *sv) |
85e6fe83 | 7607 | { |
68dc0745 | 7608 | if (!sv) |
7609 | return 0; | |
7610 | if (SvGMAGICAL(sv)) | |
7611 | mg_get(sv); | |
85e6fe83 LW |
7612 | if (!SvROK(sv)) |
7613 | return 0; | |
7614 | sv = (SV*)SvRV(sv); | |
7615 | if (!SvOBJECT(sv)) | |
7616 | return 0; | |
7617 | return 1; | |
7618 | } | |
7619 | ||
954c1994 GS |
7620 | /* |
7621 | =for apidoc sv_isa | |
7622 | ||
7623 | Returns a boolean indicating whether the SV is blessed into the specified | |
7624 | class. This does not check for subtypes; use C<sv_derived_from> to verify | |
7625 | an inheritance relationship. | |
7626 | ||
7627 | =cut | |
7628 | */ | |
7629 | ||
85e6fe83 | 7630 | int |
864dbfa3 | 7631 | Perl_sv_isa(pTHX_ SV *sv, const char *name) |
463ee0b2 | 7632 | { |
68dc0745 | 7633 | if (!sv) |
7634 | return 0; | |
7635 | if (SvGMAGICAL(sv)) | |
7636 | mg_get(sv); | |
ed6116ce | 7637 | if (!SvROK(sv)) |
463ee0b2 | 7638 | return 0; |
ed6116ce LW |
7639 | sv = (SV*)SvRV(sv); |
7640 | if (!SvOBJECT(sv)) | |
463ee0b2 LW |
7641 | return 0; |
7642 | ||
7643 | return strEQ(HvNAME(SvSTASH(sv)), name); | |
7644 | } | |
7645 | ||
954c1994 GS |
7646 | /* |
7647 | =for apidoc newSVrv | |
7648 | ||
7649 | Creates a new SV for the RV, C<rv>, to point to. If C<rv> is not an RV then | |
7650 | it will be upgraded to one. If C<classname> is non-null then the new SV will | |
7651 | be blessed in the specified package. The new SV is returned and its | |
7652 | reference count is 1. | |
7653 | ||
7654 | =cut | |
7655 | */ | |
7656 | ||
463ee0b2 | 7657 | SV* |
864dbfa3 | 7658 | Perl_newSVrv(pTHX_ SV *rv, const char *classname) |
463ee0b2 | 7659 | { |
463ee0b2 LW |
7660 | SV *sv; |
7661 | ||
4561caa4 | 7662 | new_SV(sv); |
51cf62d8 | 7663 | |
2213622d | 7664 | SV_CHECK_THINKFIRST(rv); |
51cf62d8 | 7665 | SvAMAGIC_off(rv); |
51cf62d8 | 7666 | |
0199fce9 JD |
7667 | if (SvTYPE(rv) >= SVt_PVMG) { |
7668 | U32 refcnt = SvREFCNT(rv); | |
7669 | SvREFCNT(rv) = 0; | |
7670 | sv_clear(rv); | |
7671 | SvFLAGS(rv) = 0; | |
7672 | SvREFCNT(rv) = refcnt; | |
7673 | } | |
7674 | ||
51cf62d8 | 7675 | if (SvTYPE(rv) < SVt_RV) |
0199fce9 JD |
7676 | sv_upgrade(rv, SVt_RV); |
7677 | else if (SvTYPE(rv) > SVt_RV) { | |
7678 | (void)SvOOK_off(rv); | |
7679 | if (SvPVX(rv) && SvLEN(rv)) | |
7680 | Safefree(SvPVX(rv)); | |
7681 | SvCUR_set(rv, 0); | |
7682 | SvLEN_set(rv, 0); | |
7683 | } | |
51cf62d8 OT |
7684 | |
7685 | (void)SvOK_off(rv); | |
053fc874 | 7686 | SvRV(rv) = sv; |
ed6116ce | 7687 | SvROK_on(rv); |
463ee0b2 | 7688 | |
a0d0e21e LW |
7689 | if (classname) { |
7690 | HV* stash = gv_stashpv(classname, TRUE); | |
7691 | (void)sv_bless(rv, stash); | |
7692 | } | |
7693 | return sv; | |
7694 | } | |
7695 | ||
954c1994 GS |
7696 | /* |
7697 | =for apidoc sv_setref_pv | |
7698 | ||
7699 | Copies a pointer into a new SV, optionally blessing the SV. The C<rv> | |
7700 | argument will be upgraded to an RV. That RV will be modified to point to | |
7701 | the new SV. If the C<pv> argument is NULL then C<PL_sv_undef> will be placed | |
7702 | into the SV. The C<classname> argument indicates the package for the | |
7703 | blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV | |
89e79dea | 7704 | will have a reference count of 1, and the RV will be returned. |
954c1994 GS |
7705 | |
7706 | Do not use with other Perl types such as HV, AV, SV, CV, because those | |
7707 | objects will become corrupted by the pointer copy process. | |
7708 | ||
7709 | Note that C<sv_setref_pvn> copies the string while this copies the pointer. | |
7710 | ||
7711 | =cut | |
7712 | */ | |
7713 | ||
a0d0e21e | 7714 | SV* |
864dbfa3 | 7715 | Perl_sv_setref_pv(pTHX_ SV *rv, const char *classname, void *pv) |
a0d0e21e | 7716 | { |
189b2af5 | 7717 | if (!pv) { |
3280af22 | 7718 | sv_setsv(rv, &PL_sv_undef); |
189b2af5 GS |
7719 | SvSETMAGIC(rv); |
7720 | } | |
a0d0e21e | 7721 | else |
56431972 | 7722 | sv_setiv(newSVrv(rv,classname), PTR2IV(pv)); |
a0d0e21e LW |
7723 | return rv; |
7724 | } | |
7725 | ||
954c1994 GS |
7726 | /* |
7727 | =for apidoc sv_setref_iv | |
7728 | ||
7729 | Copies an integer into a new SV, optionally blessing the SV. The C<rv> | |
7730 | argument will be upgraded to an RV. That RV will be modified to point to | |
7731 | the new SV. The C<classname> argument indicates the package for the | |
7732 | blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV | |
89e79dea | 7733 | will have a reference count of 1, and the RV will be returned. |
954c1994 GS |
7734 | |
7735 | =cut | |
7736 | */ | |
7737 | ||
a0d0e21e | 7738 | SV* |
864dbfa3 | 7739 | Perl_sv_setref_iv(pTHX_ SV *rv, const char *classname, IV iv) |
a0d0e21e LW |
7740 | { |
7741 | sv_setiv(newSVrv(rv,classname), iv); | |
7742 | return rv; | |
7743 | } | |
7744 | ||
954c1994 | 7745 | /* |
e1c57cef JH |
7746 | =for apidoc sv_setref_uv |
7747 | ||
7748 | Copies an unsigned integer into a new SV, optionally blessing the SV. The C<rv> | |
7749 | argument will be upgraded to an RV. That RV will be modified to point to | |
7750 | the new SV. The C<classname> argument indicates the package for the | |
7751 | blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV | |
89e79dea | 7752 | will have a reference count of 1, and the RV will be returned. |
e1c57cef JH |
7753 | |
7754 | =cut | |
7755 | */ | |
7756 | ||
7757 | SV* | |
7758 | Perl_sv_setref_uv(pTHX_ SV *rv, const char *classname, UV uv) | |
7759 | { | |
7760 | sv_setuv(newSVrv(rv,classname), uv); | |
7761 | return rv; | |
7762 | } | |
7763 | ||
7764 | /* | |
954c1994 GS |
7765 | =for apidoc sv_setref_nv |
7766 | ||
7767 | Copies a double into a new SV, optionally blessing the SV. The C<rv> | |
7768 | argument will be upgraded to an RV. That RV will be modified to point to | |
7769 | the new SV. The C<classname> argument indicates the package for the | |
7770 | blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV | |
89e79dea | 7771 | will have a reference count of 1, and the RV will be returned. |
954c1994 GS |
7772 | |
7773 | =cut | |
7774 | */ | |
7775 | ||
a0d0e21e | 7776 | SV* |
65202027 | 7777 | Perl_sv_setref_nv(pTHX_ SV *rv, const char *classname, NV nv) |
a0d0e21e LW |
7778 | { |
7779 | sv_setnv(newSVrv(rv,classname), nv); | |
7780 | return rv; | |
7781 | } | |
463ee0b2 | 7782 | |
954c1994 GS |
7783 | /* |
7784 | =for apidoc sv_setref_pvn | |
7785 | ||
7786 | Copies a string into a new SV, optionally blessing the SV. The length of the | |
7787 | string must be specified with C<n>. The C<rv> argument will be upgraded to | |
7788 | an RV. That RV will be modified to point to the new SV. The C<classname> | |
7789 | argument indicates the package for the blessing. Set C<classname> to | |
89e79dea JH |
7790 | C<Nullch> to avoid the blessing. The new SV will have a reference count |
7791 | of 1, and the RV will be returned. | |
954c1994 GS |
7792 | |
7793 | Note that C<sv_setref_pv> copies the pointer while this copies the string. | |
7794 | ||
7795 | =cut | |
7796 | */ | |
7797 | ||
a0d0e21e | 7798 | SV* |
864dbfa3 | 7799 | Perl_sv_setref_pvn(pTHX_ SV *rv, const char *classname, char *pv, STRLEN n) |
a0d0e21e LW |
7800 | { |
7801 | sv_setpvn(newSVrv(rv,classname), pv, n); | |
463ee0b2 LW |
7802 | return rv; |
7803 | } | |
7804 | ||
954c1994 GS |
7805 | /* |
7806 | =for apidoc sv_bless | |
7807 | ||
7808 | Blesses an SV into a specified package. The SV must be an RV. The package | |
7809 | must be designated by its stash (see C<gv_stashpv()>). The reference count | |
7810 | of the SV is unaffected. | |
7811 | ||
7812 | =cut | |
7813 | */ | |
7814 | ||
a0d0e21e | 7815 | SV* |
864dbfa3 | 7816 | Perl_sv_bless(pTHX_ SV *sv, HV *stash) |
a0d0e21e | 7817 | { |
76e3520e | 7818 | SV *tmpRef; |
a0d0e21e | 7819 | if (!SvROK(sv)) |
cea2e8a9 | 7820 | Perl_croak(aTHX_ "Can't bless non-reference value"); |
76e3520e GS |
7821 | tmpRef = SvRV(sv); |
7822 | if (SvFLAGS(tmpRef) & (SVs_OBJECT|SVf_READONLY)) { | |
7823 | if (SvREADONLY(tmpRef)) | |
cea2e8a9 | 7824 | Perl_croak(aTHX_ PL_no_modify); |
76e3520e GS |
7825 | if (SvOBJECT(tmpRef)) { |
7826 | if (SvTYPE(tmpRef) != SVt_PVIO) | |
3280af22 | 7827 | --PL_sv_objcount; |
76e3520e | 7828 | SvREFCNT_dec(SvSTASH(tmpRef)); |
2e3febc6 | 7829 | } |
a0d0e21e | 7830 | } |
76e3520e GS |
7831 | SvOBJECT_on(tmpRef); |
7832 | if (SvTYPE(tmpRef) != SVt_PVIO) | |
3280af22 | 7833 | ++PL_sv_objcount; |
76e3520e GS |
7834 | (void)SvUPGRADE(tmpRef, SVt_PVMG); |
7835 | SvSTASH(tmpRef) = (HV*)SvREFCNT_inc(stash); | |
a0d0e21e | 7836 | |
2e3febc6 CS |
7837 | if (Gv_AMG(stash)) |
7838 | SvAMAGIC_on(sv); | |
7839 | else | |
7840 | SvAMAGIC_off(sv); | |
a0d0e21e | 7841 | |
1edbfb88 AB |
7842 | if(SvSMAGICAL(tmpRef)) |
7843 | if(mg_find(tmpRef, PERL_MAGIC_ext) || mg_find(tmpRef, PERL_MAGIC_uvar)) | |
7844 | mg_set(tmpRef); | |
7845 | ||
7846 | ||
ecdeb87c | 7847 | |
a0d0e21e LW |
7848 | return sv; |
7849 | } | |
7850 | ||
645c22ef | 7851 | /* Downgrades a PVGV to a PVMG. |
645c22ef DM |
7852 | */ |
7853 | ||
76e3520e | 7854 | STATIC void |
cea2e8a9 | 7855 | S_sv_unglob(pTHX_ SV *sv) |
a0d0e21e | 7856 | { |
850fabdf GS |
7857 | void *xpvmg; |
7858 | ||
a0d0e21e LW |
7859 | assert(SvTYPE(sv) == SVt_PVGV); |
7860 | SvFAKE_off(sv); | |
7861 | if (GvGP(sv)) | |
1edc1566 | 7862 | gp_free((GV*)sv); |
e826b3c7 GS |
7863 | if (GvSTASH(sv)) { |
7864 | SvREFCNT_dec(GvSTASH(sv)); | |
7865 | GvSTASH(sv) = Nullhv; | |
7866 | } | |
14befaf4 | 7867 | sv_unmagic(sv, PERL_MAGIC_glob); |
a0d0e21e | 7868 | Safefree(GvNAME(sv)); |
a5f75d66 | 7869 | GvMULTI_off(sv); |
850fabdf GS |
7870 | |
7871 | /* need to keep SvANY(sv) in the right arena */ | |
7872 | xpvmg = new_XPVMG(); | |
7873 | StructCopy(SvANY(sv), xpvmg, XPVMG); | |
7874 | del_XPVGV(SvANY(sv)); | |
7875 | SvANY(sv) = xpvmg; | |
7876 | ||
a0d0e21e LW |
7877 | SvFLAGS(sv) &= ~SVTYPEMASK; |
7878 | SvFLAGS(sv) |= SVt_PVMG; | |
7879 | } | |
7880 | ||
954c1994 | 7881 | /* |
840a7b70 | 7882 | =for apidoc sv_unref_flags |
954c1994 GS |
7883 | |
7884 | Unsets the RV status of the SV, and decrements the reference count of | |
7885 | whatever was being referenced by the RV. This can almost be thought of | |
840a7b70 IZ |
7886 | as a reversal of C<newSVrv>. The C<cflags> argument can contain |
7887 | C<SV_IMMEDIATE_UNREF> to force the reference count to be decremented | |
7888 | (otherwise the decrementing is conditional on the reference count being | |
7889 | different from one or the reference being a readonly SV). | |
7889fe52 | 7890 | See C<SvROK_off>. |
954c1994 GS |
7891 | |
7892 | =cut | |
7893 | */ | |
7894 | ||
ed6116ce | 7895 | void |
840a7b70 | 7896 | Perl_sv_unref_flags(pTHX_ SV *sv, U32 flags) |
ed6116ce | 7897 | { |
a0d0e21e | 7898 | SV* rv = SvRV(sv); |
810b8aa5 GS |
7899 | |
7900 | if (SvWEAKREF(sv)) { | |
7901 | sv_del_backref(sv); | |
7902 | SvWEAKREF_off(sv); | |
7903 | SvRV(sv) = 0; | |
7904 | return; | |
7905 | } | |
ed6116ce LW |
7906 | SvRV(sv) = 0; |
7907 | SvROK_off(sv); | |
5e8f8cda JH |
7908 | /* You can't have a || SvREADONLY(rv) here, as $a = $$a, where $a was |
7909 | assigned to as BEGIN {$a = \"Foo"} will fail. */ | |
7910 | if (SvREFCNT(rv) != 1 || (flags & SV_IMMEDIATE_UNREF)) | |
4633a7c4 | 7911 | SvREFCNT_dec(rv); |
840a7b70 | 7912 | else /* XXX Hack, but hard to make $a=$a->[1] work otherwise */ |
4633a7c4 | 7913 | sv_2mortal(rv); /* Schedule for freeing later */ |
ed6116ce | 7914 | } |
8990e307 | 7915 | |
840a7b70 IZ |
7916 | /* |
7917 | =for apidoc sv_unref | |
7918 | ||
7919 | Unsets the RV status of the SV, and decrements the reference count of | |
7920 | whatever was being referenced by the RV. This can almost be thought of | |
7921 | as a reversal of C<newSVrv>. This is C<sv_unref_flags> with the C<flag> | |
7889fe52 | 7922 | being zero. See C<SvROK_off>. |
840a7b70 IZ |
7923 | |
7924 | =cut | |
7925 | */ | |
7926 | ||
7927 | void | |
7928 | Perl_sv_unref(pTHX_ SV *sv) | |
7929 | { | |
7930 | sv_unref_flags(sv, 0); | |
7931 | } | |
7932 | ||
645c22ef DM |
7933 | /* |
7934 | =for apidoc sv_taint | |
7935 | ||
7936 | Taint an SV. Use C<SvTAINTED_on> instead. | |
7937 | =cut | |
7938 | */ | |
7939 | ||
bbce6d69 | 7940 | void |
864dbfa3 | 7941 | Perl_sv_taint(pTHX_ SV *sv) |
bbce6d69 | 7942 | { |
14befaf4 | 7943 | sv_magic((sv), Nullsv, PERL_MAGIC_taint, Nullch, 0); |
bbce6d69 | 7944 | } |
7945 | ||
645c22ef DM |
7946 | /* |
7947 | =for apidoc sv_untaint | |
7948 | ||
7949 | Untaint an SV. Use C<SvTAINTED_off> instead. | |
7950 | =cut | |
7951 | */ | |
7952 | ||
bbce6d69 | 7953 | void |
864dbfa3 | 7954 | Perl_sv_untaint(pTHX_ SV *sv) |
bbce6d69 | 7955 | { |
13f57bf8 | 7956 | if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) { |
14befaf4 | 7957 | MAGIC *mg = mg_find(sv, PERL_MAGIC_taint); |
36477c24 | 7958 | if (mg) |
565764a8 | 7959 | mg->mg_len &= ~1; |
36477c24 | 7960 | } |
bbce6d69 | 7961 | } |
7962 | ||
645c22ef DM |
7963 | /* |
7964 | =for apidoc sv_tainted | |
7965 | ||
7966 | Test an SV for taintedness. Use C<SvTAINTED> instead. | |
7967 | =cut | |
7968 | */ | |
7969 | ||
bbce6d69 | 7970 | bool |
864dbfa3 | 7971 | Perl_sv_tainted(pTHX_ SV *sv) |
bbce6d69 | 7972 | { |
13f57bf8 | 7973 | if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) { |
14befaf4 | 7974 | MAGIC *mg = mg_find(sv, PERL_MAGIC_taint); |
155aba94 | 7975 | if (mg && ((mg->mg_len & 1) || ((mg->mg_len & 2) && mg->mg_obj == sv))) |
36477c24 | 7976 | return TRUE; |
7977 | } | |
7978 | return FALSE; | |
bbce6d69 | 7979 | } |
7980 | ||
d34f9d2e JH |
7981 | /* |
7982 | =for apidoc sv_setpviv | |
7983 | ||
7984 | Copies an integer into the given SV, also updating its string value. | |
7985 | Does not handle 'set' magic. See C<sv_setpviv_mg>. | |
7986 | ||
7987 | =cut | |
7988 | */ | |
7989 | ||
7990 | void | |
7991 | Perl_sv_setpviv(pTHX_ SV *sv, IV iv) | |
7992 | { | |
7993 | char buf[TYPE_CHARS(UV)]; | |
7994 | char *ebuf; | |
7995 | char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf); | |
7996 | ||
7997 | sv_setpvn(sv, ptr, ebuf - ptr); | |
7998 | } | |
7999 | ||
8000 | /* | |
8001 | =for apidoc sv_setpviv_mg | |
8002 | ||
8003 | Like C<sv_setpviv>, but also handles 'set' magic. | |
8004 | ||
8005 | =cut | |
8006 | */ | |
8007 | ||
8008 | void | |
8009 | Perl_sv_setpviv_mg(pTHX_ SV *sv, IV iv) | |
8010 | { | |
8011 | char buf[TYPE_CHARS(UV)]; | |
8012 | char *ebuf; | |
8013 | char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf); | |
8014 | ||
8015 | sv_setpvn(sv, ptr, ebuf - ptr); | |
8016 | SvSETMAGIC(sv); | |
8017 | } | |
8018 | ||
cea2e8a9 | 8019 | #if defined(PERL_IMPLICIT_CONTEXT) |
645c22ef DM |
8020 | |
8021 | /* pTHX_ magic can't cope with varargs, so this is a no-context | |
8022 | * version of the main function, (which may itself be aliased to us). | |
8023 | * Don't access this version directly. | |
8024 | */ | |
8025 | ||
cea2e8a9 GS |
8026 | void |
8027 | Perl_sv_setpvf_nocontext(SV *sv, const char* pat, ...) | |
8028 | { | |
8029 | dTHX; | |
8030 | va_list args; | |
8031 | va_start(args, pat); | |
c5be433b | 8032 | sv_vsetpvf(sv, pat, &args); |
cea2e8a9 GS |
8033 | va_end(args); |
8034 | } | |
8035 | ||
645c22ef DM |
8036 | /* pTHX_ magic can't cope with varargs, so this is a no-context |
8037 | * version of the main function, (which may itself be aliased to us). | |
8038 | * Don't access this version directly. | |
8039 | */ | |
cea2e8a9 GS |
8040 | |
8041 | void | |
8042 | Perl_sv_setpvf_mg_nocontext(SV *sv, const char* pat, ...) | |
8043 | { | |
8044 | dTHX; | |
8045 | va_list args; | |
8046 | va_start(args, pat); | |
c5be433b | 8047 | sv_vsetpvf_mg(sv, pat, &args); |
cea2e8a9 | 8048 | va_end(args); |
cea2e8a9 GS |
8049 | } |
8050 | #endif | |
8051 | ||
954c1994 GS |
8052 | /* |
8053 | =for apidoc sv_setpvf | |
8054 | ||
8055 | Processes its arguments like C<sprintf> and sets an SV to the formatted | |
8056 | output. Does not handle 'set' magic. See C<sv_setpvf_mg>. | |
8057 | ||
8058 | =cut | |
8059 | */ | |
8060 | ||
46fc3d4c | 8061 | void |
864dbfa3 | 8062 | Perl_sv_setpvf(pTHX_ SV *sv, const char* pat, ...) |
46fc3d4c | 8063 | { |
8064 | va_list args; | |
46fc3d4c | 8065 | va_start(args, pat); |
c5be433b | 8066 | sv_vsetpvf(sv, pat, &args); |
46fc3d4c | 8067 | va_end(args); |
8068 | } | |
8069 | ||
645c22ef DM |
8070 | /* backend for C<sv_setpvf> and C<sv_setpvf_nocontext> */ |
8071 | ||
c5be433b GS |
8072 | void |
8073 | Perl_sv_vsetpvf(pTHX_ SV *sv, const char* pat, va_list* args) | |
8074 | { | |
8075 | sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
8076 | } | |
ef50df4b | 8077 | |
954c1994 GS |
8078 | /* |
8079 | =for apidoc sv_setpvf_mg | |
8080 | ||
8081 | Like C<sv_setpvf>, but also handles 'set' magic. | |
8082 | ||
8083 | =cut | |
8084 | */ | |
8085 | ||
ef50df4b | 8086 | void |
864dbfa3 | 8087 | Perl_sv_setpvf_mg(pTHX_ SV *sv, const char* pat, ...) |
ef50df4b GS |
8088 | { |
8089 | va_list args; | |
ef50df4b | 8090 | va_start(args, pat); |
c5be433b | 8091 | sv_vsetpvf_mg(sv, pat, &args); |
ef50df4b | 8092 | va_end(args); |
c5be433b GS |
8093 | } |
8094 | ||
645c22ef DM |
8095 | /* backend for C<sv_setpvf_mg> C<setpvf_mg_nocontext> */ |
8096 | ||
c5be433b GS |
8097 | void |
8098 | Perl_sv_vsetpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args) | |
8099 | { | |
8100 | sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
ef50df4b GS |
8101 | SvSETMAGIC(sv); |
8102 | } | |
8103 | ||
cea2e8a9 | 8104 | #if defined(PERL_IMPLICIT_CONTEXT) |
645c22ef DM |
8105 | |
8106 | /* pTHX_ magic can't cope with varargs, so this is a no-context | |
8107 | * version of the main function, (which may itself be aliased to us). | |
8108 | * Don't access this version directly. | |
8109 | */ | |
8110 | ||
cea2e8a9 GS |
8111 | void |
8112 | Perl_sv_catpvf_nocontext(SV *sv, const char* pat, ...) | |
8113 | { | |
8114 | dTHX; | |
8115 | va_list args; | |
8116 | va_start(args, pat); | |
c5be433b | 8117 | sv_vcatpvf(sv, pat, &args); |
cea2e8a9 GS |
8118 | va_end(args); |
8119 | } | |
8120 | ||
645c22ef DM |
8121 | /* pTHX_ magic can't cope with varargs, so this is a no-context |
8122 | * version of the main function, (which may itself be aliased to us). | |
8123 | * Don't access this version directly. | |
8124 | */ | |
8125 | ||
cea2e8a9 GS |
8126 | void |
8127 | Perl_sv_catpvf_mg_nocontext(SV *sv, const char* pat, ...) | |
8128 | { | |
8129 | dTHX; | |
8130 | va_list args; | |
8131 | va_start(args, pat); | |
c5be433b | 8132 | sv_vcatpvf_mg(sv, pat, &args); |
cea2e8a9 | 8133 | va_end(args); |
cea2e8a9 GS |
8134 | } |
8135 | #endif | |
8136 | ||
954c1994 GS |
8137 | /* |
8138 | =for apidoc sv_catpvf | |
8139 | ||
d5ce4a7c GA |
8140 | Processes its arguments like C<sprintf> and appends the formatted |
8141 | output to an SV. If the appended data contains "wide" characters | |
8142 | (including, but not limited to, SVs with a UTF-8 PV formatted with %s, | |
8143 | and characters >255 formatted with %c), the original SV might get | |
8144 | upgraded to UTF-8. Handles 'get' magic, but not 'set' magic. | |
8145 | C<SvSETMAGIC()> must typically be called after calling this function | |
8146 | to handle 'set' magic. | |
954c1994 | 8147 | |
d5ce4a7c | 8148 | =cut */ |
954c1994 | 8149 | |
46fc3d4c | 8150 | void |
864dbfa3 | 8151 | Perl_sv_catpvf(pTHX_ SV *sv, const char* pat, ...) |
46fc3d4c | 8152 | { |
8153 | va_list args; | |
46fc3d4c | 8154 | va_start(args, pat); |
c5be433b | 8155 | sv_vcatpvf(sv, pat, &args); |
46fc3d4c | 8156 | va_end(args); |
8157 | } | |
8158 | ||
645c22ef DM |
8159 | /* backend for C<sv_catpvf> and C<catpvf_mg_nocontext> */ |
8160 | ||
ef50df4b | 8161 | void |
c5be433b GS |
8162 | Perl_sv_vcatpvf(pTHX_ SV *sv, const char* pat, va_list* args) |
8163 | { | |
8164 | sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
8165 | } | |
8166 | ||
954c1994 GS |
8167 | /* |
8168 | =for apidoc sv_catpvf_mg | |
8169 | ||
8170 | Like C<sv_catpvf>, but also handles 'set' magic. | |
8171 | ||
8172 | =cut | |
8173 | */ | |
8174 | ||
c5be433b | 8175 | void |
864dbfa3 | 8176 | Perl_sv_catpvf_mg(pTHX_ SV *sv, const char* pat, ...) |
ef50df4b GS |
8177 | { |
8178 | va_list args; | |
ef50df4b | 8179 | va_start(args, pat); |
c5be433b | 8180 | sv_vcatpvf_mg(sv, pat, &args); |
ef50df4b | 8181 | va_end(args); |
c5be433b GS |
8182 | } |
8183 | ||
645c22ef DM |
8184 | /* backend for C<catpvf_mg> and C<catpvf_mg_nocontext> */ |
8185 | ||
c5be433b GS |
8186 | void |
8187 | Perl_sv_vcatpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args) | |
8188 | { | |
8189 | sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
ef50df4b GS |
8190 | SvSETMAGIC(sv); |
8191 | } | |
8192 | ||
954c1994 GS |
8193 | /* |
8194 | =for apidoc sv_vsetpvfn | |
8195 | ||
8196 | Works like C<vcatpvfn> but copies the text into the SV instead of | |
8197 | appending it. | |
8198 | ||
645c22ef DM |
8199 | Usually used via one of its frontends C<sv_setpvf> and C<sv_setpvf_mg>. |
8200 | ||
954c1994 GS |
8201 | =cut |
8202 | */ | |
8203 | ||
46fc3d4c | 8204 | void |
7d5ea4e7 | 8205 | Perl_sv_vsetpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted) |
46fc3d4c | 8206 | { |
8207 | sv_setpvn(sv, "", 0); | |
7d5ea4e7 | 8208 | sv_vcatpvfn(sv, pat, patlen, args, svargs, svmax, maybe_tainted); |
46fc3d4c | 8209 | } |
8210 | ||
645c22ef DM |
8211 | /* private function for use in sv_vcatpvfn via the EXPECT_NUMBER macro */ |
8212 | ||
2d00ba3b | 8213 | STATIC I32 |
9dd79c3f | 8214 | S_expect_number(pTHX_ char** pattern) |
211dfcf1 HS |
8215 | { |
8216 | I32 var = 0; | |
8217 | switch (**pattern) { | |
8218 | case '1': case '2': case '3': | |
8219 | case '4': case '5': case '6': | |
8220 | case '7': case '8': case '9': | |
8221 | while (isDIGIT(**pattern)) | |
8222 | var = var * 10 + (*(*pattern)++ - '0'); | |
8223 | } | |
8224 | return var; | |
8225 | } | |
9dd79c3f | 8226 | #define EXPECT_NUMBER(pattern, var) (var = S_expect_number(aTHX_ &pattern)) |
211dfcf1 | 8227 | |
954c1994 GS |
8228 | /* |
8229 | =for apidoc sv_vcatpvfn | |
8230 | ||
8231 | Processes its arguments like C<vsprintf> and appends the formatted output | |
8232 | to an SV. Uses an array of SVs if the C style variable argument list is | |
8233 | missing (NULL). When running with taint checks enabled, indicates via | |
8234 | C<maybe_tainted> if results are untrustworthy (often due to the use of | |
8235 | locales). | |
8236 | ||
645c22ef DM |
8237 | Usually used via one of its frontends C<sv_catpvf> and C<sv_catpvf_mg>. |
8238 | ||
954c1994 GS |
8239 | =cut |
8240 | */ | |
8241 | ||
46fc3d4c | 8242 | void |
7d5ea4e7 | 8243 | Perl_sv_vcatpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted) |
46fc3d4c | 8244 | { |
8245 | char *p; | |
8246 | char *q; | |
8247 | char *patend; | |
fc36a67e | 8248 | STRLEN origlen; |
46fc3d4c | 8249 | I32 svix = 0; |
c635e13b | 8250 | static char nullstr[] = "(null)"; |
9c5ffd7c | 8251 | SV *argsv = Nullsv; |
5835a535 JH |
8252 | bool has_utf8; /* has the result utf8? */ |
8253 | bool pat_utf8; /* the pattern is in utf8? */ | |
8254 | SV *nsv = Nullsv; | |
8255 | ||
8256 | has_utf8 = pat_utf8 = DO_UTF8(sv); | |
46fc3d4c | 8257 | |
8258 | /* no matter what, this is a string now */ | |
fc36a67e | 8259 | (void)SvPV_force(sv, origlen); |
46fc3d4c | 8260 | |
fc36a67e | 8261 | /* special-case "", "%s", and "%_" */ |
46fc3d4c | 8262 | if (patlen == 0) |
8263 | return; | |
fc36a67e | 8264 | if (patlen == 2 && pat[0] == '%') { |
8265 | switch (pat[1]) { | |
8266 | case 's': | |
c635e13b | 8267 | if (args) { |
8268 | char *s = va_arg(*args, char*); | |
8269 | sv_catpv(sv, s ? s : nullstr); | |
8270 | } | |
7e2040f0 | 8271 | else if (svix < svmax) { |
fc36a67e | 8272 | sv_catsv(sv, *svargs); |
7e2040f0 GS |
8273 | if (DO_UTF8(*svargs)) |
8274 | SvUTF8_on(sv); | |
8275 | } | |
fc36a67e | 8276 | return; |
8277 | case '_': | |
8278 | if (args) { | |
7e2040f0 GS |
8279 | argsv = va_arg(*args, SV*); |
8280 | sv_catsv(sv, argsv); | |
8281 | if (DO_UTF8(argsv)) | |
8282 | SvUTF8_on(sv); | |
fc36a67e | 8283 | return; |
8284 | } | |
8285 | /* See comment on '_' below */ | |
8286 | break; | |
8287 | } | |
46fc3d4c | 8288 | } |
8289 | ||
2cf2cfc6 | 8290 | if (!args && svix < svmax && DO_UTF8(*svargs)) |
5b7ea690 | 8291 | has_utf8 = TRUE; |
2cf2cfc6 | 8292 | |
46fc3d4c | 8293 | patend = (char*)pat + patlen; |
8294 | for (p = (char*)pat; p < patend; p = q) { | |
8295 | bool alt = FALSE; | |
8296 | bool left = FALSE; | |
b22c7a20 | 8297 | bool vectorize = FALSE; |
211dfcf1 | 8298 | bool vectorarg = FALSE; |
2cf2cfc6 | 8299 | bool vec_utf8 = FALSE; |
46fc3d4c | 8300 | char fill = ' '; |
8301 | char plus = 0; | |
8302 | char intsize = 0; | |
8303 | STRLEN width = 0; | |
fc36a67e | 8304 | STRLEN zeros = 0; |
46fc3d4c | 8305 | bool has_precis = FALSE; |
8306 | STRLEN precis = 0; | |
d34f9d2e | 8307 | I32 osvix = svix; |
2cf2cfc6 | 8308 | bool is_utf8 = FALSE; /* is this item utf8? */ |
5b7ea690 JH |
8309 | #ifdef HAS_LDBL_SPRINTF_BUG |
8310 | /* This is to try to fix a bug with irix/nonstop-ux/powerux and | |
8311 | with sfio - Allen <allens@cpan.org> */ | |
8312 | bool fix_ldbl_sprintf_bug = FALSE; | |
8313 | #endif | |
8314 | ||
46fc3d4c | 8315 | char esignbuf[4]; |
ad391ad9 | 8316 | U8 utf8buf[UTF8_MAXLEN+1]; |
46fc3d4c | 8317 | STRLEN esignlen = 0; |
8318 | ||
8319 | char *eptr = Nullch; | |
fc36a67e | 8320 | STRLEN elen = 0; |
089c015b JH |
8321 | /* Times 4: a decimal digit takes more than 3 binary digits. |
8322 | * NV_DIG: mantissa takes than many decimal digits. | |
8323 | * Plus 32: Playing safe. */ | |
8324 | char ebuf[IV_DIG * 4 + NV_DIG + 32]; | |
5b7ea690 | 8325 | /* large enough for "%#.#f" --chip */ |
2d4389e4 | 8326 | /* what about long double NVs? --jhi */ |
b22c7a20 | 8327 | |
81f715da | 8328 | SV *vecsv = Nullsv; |
a05b299f | 8329 | U8 *vecstr = Null(U8*); |
b22c7a20 | 8330 | STRLEN veclen = 0; |
934abaf1 | 8331 | char c = 0; |
46fc3d4c | 8332 | int i; |
9c5ffd7c | 8333 | unsigned base = 0; |
8c8eb53c RB |
8334 | IV iv = 0; |
8335 | UV uv = 0; | |
9e5b023a JH |
8336 | /* we need a long double target in case HAS_LONG_DOUBLE but |
8337 | not USE_LONG_DOUBLE | |
8338 | */ | |
35fff930 | 8339 | #if defined(HAS_LONG_DOUBLE) && LONG_DOUBLESIZE > DOUBLESIZE |
9e5b023a JH |
8340 | long double nv; |
8341 | #else | |
65202027 | 8342 | NV nv; |
9e5b023a | 8343 | #endif |
46fc3d4c | 8344 | STRLEN have; |
8345 | STRLEN need; | |
8346 | STRLEN gap; | |
b22c7a20 GS |
8347 | char *dotstr = "."; |
8348 | STRLEN dotstrlen = 1; | |
211dfcf1 | 8349 | I32 efix = 0; /* explicit format parameter index */ |
eb3fce90 | 8350 | I32 ewix = 0; /* explicit width index */ |
211dfcf1 HS |
8351 | I32 epix = 0; /* explicit precision index */ |
8352 | I32 evix = 0; /* explicit vector index */ | |
eb3fce90 | 8353 | bool asterisk = FALSE; |
46fc3d4c | 8354 | |
211dfcf1 | 8355 | /* echo everything up to the next format specification */ |
46fc3d4c | 8356 | for (q = p; q < patend && *q != '%'; ++q) ; |
8357 | if (q > p) { | |
5835a535 JH |
8358 | if (has_utf8 && !pat_utf8) |
8359 | sv_catpvn_utf8_upgrade(sv, p, q - p, nsv); | |
8360 | else | |
8361 | sv_catpvn(sv, p, q - p); | |
46fc3d4c | 8362 | p = q; |
8363 | } | |
8364 | if (q++ >= patend) | |
8365 | break; | |
8366 | ||
211dfcf1 HS |
8367 | /* |
8368 | We allow format specification elements in this order: | |
8369 | \d+\$ explicit format parameter index | |
8370 | [-+ 0#]+ flags | |
a472f209 | 8371 | v|\*(\d+\$)?v vector with optional (optionally specified) arg |
f8cf5370 | 8372 | 0 flag (as above): repeated to allow "v02" |
211dfcf1 HS |
8373 | \d+|\*(\d+\$)? width using optional (optionally specified) arg |
8374 | \.(\d*|\*(\d+\$)?) precision using optional (optionally specified) arg | |
8375 | [hlqLV] size | |
8376 | [%bcdefginopsux_DFOUX] format (mandatory) | |
8377 | */ | |
8378 | if (EXPECT_NUMBER(q, width)) { | |
8379 | if (*q == '$') { | |
8380 | ++q; | |
8381 | efix = width; | |
8382 | } else { | |
8383 | goto gotwidth; | |
8384 | } | |
8385 | } | |
8386 | ||
fc36a67e | 8387 | /* FLAGS */ |
8388 | ||
46fc3d4c | 8389 | while (*q) { |
8390 | switch (*q) { | |
8391 | case ' ': | |
8392 | case '+': | |
8393 | plus = *q++; | |
8394 | continue; | |
8395 | ||
8396 | case '-': | |
8397 | left = TRUE; | |
8398 | q++; | |
8399 | continue; | |
8400 | ||
8401 | case '0': | |
8402 | fill = *q++; | |
8403 | continue; | |
8404 | ||
8405 | case '#': | |
8406 | alt = TRUE; | |
8407 | q++; | |
8408 | continue; | |
8409 | ||
fc36a67e | 8410 | default: |
8411 | break; | |
8412 | } | |
8413 | break; | |
8414 | } | |
46fc3d4c | 8415 | |
211dfcf1 | 8416 | tryasterisk: |
eb3fce90 | 8417 | if (*q == '*') { |
211dfcf1 HS |
8418 | q++; |
8419 | if (EXPECT_NUMBER(q, ewix)) | |
8420 | if (*q++ != '$') | |
8421 | goto unknown; | |
eb3fce90 | 8422 | asterisk = TRUE; |
211dfcf1 HS |
8423 | } |
8424 | if (*q == 'v') { | |
eb3fce90 | 8425 | q++; |
211dfcf1 HS |
8426 | if (vectorize) |
8427 | goto unknown; | |
9cbac4c7 | 8428 | if ((vectorarg = asterisk)) { |
211dfcf1 HS |
8429 | evix = ewix; |
8430 | ewix = 0; | |
8431 | asterisk = FALSE; | |
8432 | } | |
8433 | vectorize = TRUE; | |
8434 | goto tryasterisk; | |
eb3fce90 JH |
8435 | } |
8436 | ||
211dfcf1 | 8437 | if (!asterisk) |
f8cf5370 JH |
8438 | if( *q == '0' ) |
8439 | fill = *q++; | |
211dfcf1 HS |
8440 | EXPECT_NUMBER(q, width); |
8441 | ||
8442 | if (vectorize) { | |
8443 | if (vectorarg) { | |
8444 | if (args) | |
8445 | vecsv = va_arg(*args, SV*); | |
8446 | else | |
8447 | vecsv = (evix ? evix <= svmax : svix < svmax) ? | |
219ef941 | 8448 | svargs[evix ? evix-1 : svix++] : &PL_sv_undef; |
4459522c | 8449 | dotstr = SvPVx(vecsv, dotstrlen); |
211dfcf1 | 8450 | if (DO_UTF8(vecsv)) |
2cf2cfc6 | 8451 | is_utf8 = TRUE; |
211dfcf1 HS |
8452 | } |
8453 | if (args) { | |
8454 | vecsv = va_arg(*args, SV*); | |
8455 | vecstr = (U8*)SvPVx(vecsv,veclen); | |
2cf2cfc6 | 8456 | vec_utf8 = DO_UTF8(vecsv); |
eb3fce90 | 8457 | } |
211dfcf1 HS |
8458 | else if (efix ? efix <= svmax : svix < svmax) { |
8459 | vecsv = svargs[efix ? efix-1 : svix++]; | |
8460 | vecstr = (U8*)SvPVx(vecsv,veclen); | |
2cf2cfc6 | 8461 | vec_utf8 = DO_UTF8(vecsv); |
211dfcf1 HS |
8462 | } |
8463 | else { | |
8464 | vecstr = (U8*)""; | |
8465 | veclen = 0; | |
8466 | } | |
eb3fce90 | 8467 | } |
fc36a67e | 8468 | |
eb3fce90 | 8469 | if (asterisk) { |
fc36a67e | 8470 | if (args) |
8471 | i = va_arg(*args, int); | |
8472 | else | |
eb3fce90 JH |
8473 | i = (ewix ? ewix <= svmax : svix < svmax) ? |
8474 | SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0; | |
fc36a67e | 8475 | left |= (i < 0); |
8476 | width = (i < 0) ? -i : i; | |
fc36a67e | 8477 | } |
211dfcf1 | 8478 | gotwidth: |
fc36a67e | 8479 | |
8480 | /* PRECISION */ | |
46fc3d4c | 8481 | |
fc36a67e | 8482 | if (*q == '.') { |
8483 | q++; | |
8484 | if (*q == '*') { | |
211dfcf1 | 8485 | q++; |
7b8dd722 HS |
8486 | if (EXPECT_NUMBER(q, epix) && *q++ != '$') |
8487 | goto unknown; | |
8488 | /* XXX: todo, support specified precision parameter */ | |
8489 | if (epix) | |
211dfcf1 | 8490 | goto unknown; |
46fc3d4c | 8491 | if (args) |
8492 | i = va_arg(*args, int); | |
8493 | else | |
eb3fce90 JH |
8494 | i = (ewix ? ewix <= svmax : svix < svmax) |
8495 | ? SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0; | |
fc36a67e | 8496 | precis = (i < 0) ? 0 : i; |
fc36a67e | 8497 | } |
8498 | else { | |
8499 | precis = 0; | |
8500 | while (isDIGIT(*q)) | |
8501 | precis = precis * 10 + (*q++ - '0'); | |
8502 | } | |
8503 | has_precis = TRUE; | |
8504 | } | |
46fc3d4c | 8505 | |
fc36a67e | 8506 | /* SIZE */ |
46fc3d4c | 8507 | |
fc36a67e | 8508 | switch (*q) { |
c623ac67 GS |
8509 | #ifdef WIN32 |
8510 | case 'I': /* Ix, I32x, and I64x */ | |
8511 | # ifdef WIN64 | |
8512 | if (q[1] == '6' && q[2] == '4') { | |
8513 | q += 3; | |
8514 | intsize = 'q'; | |
8515 | break; | |
8516 | } | |
8517 | # endif | |
8518 | if (q[1] == '3' && q[2] == '2') { | |
8519 | q += 3; | |
8520 | break; | |
8521 | } | |
8522 | # ifdef WIN64 | |
8523 | intsize = 'q'; | |
8524 | # endif | |
8525 | q++; | |
8526 | break; | |
8527 | #endif | |
9e5b023a | 8528 | #if defined(HAS_QUAD) || defined(HAS_LONG_DOUBLE) |
6f9bb7fd | 8529 | case 'L': /* Ld */ |
e5c81feb | 8530 | /* FALL THROUGH */ |
e5c81feb | 8531 | #ifdef HAS_QUAD |
6f9bb7fd | 8532 | case 'q': /* qd */ |
9e5b023a | 8533 | #endif |
6f9bb7fd GS |
8534 | intsize = 'q'; |
8535 | q++; | |
8536 | break; | |
8537 | #endif | |
fc36a67e | 8538 | case 'l': |
9e5b023a | 8539 | #if defined(HAS_QUAD) || defined(HAS_LONG_DOUBLE) |
5b7ea690 | 8540 | if (*(q + 1) == 'l') { /* lld, llf */ |
fc36a67e | 8541 | intsize = 'q'; |
8542 | q += 2; | |
46fc3d4c | 8543 | break; |
cf2093f6 | 8544 | } |
fc36a67e | 8545 | #endif |
6f9bb7fd | 8546 | /* FALL THROUGH */ |
fc36a67e | 8547 | case 'h': |
cf2093f6 | 8548 | /* FALL THROUGH */ |
fc36a67e | 8549 | case 'V': |
8550 | intsize = *q++; | |
46fc3d4c | 8551 | break; |
8552 | } | |
8553 | ||
fc36a67e | 8554 | /* CONVERSION */ |
8555 | ||
211dfcf1 HS |
8556 | if (*q == '%') { |
8557 | eptr = q++; | |
8558 | elen = 1; | |
8559 | goto string; | |
8560 | } | |
8561 | ||
be75b157 HS |
8562 | if (vectorize) |
8563 | argsv = vecsv; | |
8564 | else if (!args) | |
211dfcf1 HS |
8565 | argsv = (efix ? efix <= svmax : svix < svmax) ? |
8566 | svargs[efix ? efix-1 : svix++] : &PL_sv_undef; | |
8567 | ||
46fc3d4c | 8568 | switch (c = *q++) { |
8569 | ||
8570 | /* STRINGS */ | |
8571 | ||
46fc3d4c | 8572 | case 'c': |
be75b157 | 8573 | uv = (args && !vectorize) ? va_arg(*args, int) : SvIVx(argsv); |
1bd104fb JH |
8574 | if ((uv > 255 || |
8575 | (!UNI_IS_INVARIANT(uv) && SvUTF8(sv))) | |
0064a8a9 | 8576 | && !IN_BYTES) { |
dfe13c55 | 8577 | eptr = (char*)utf8buf; |
9041c2e3 | 8578 | elen = uvchr_to_utf8((U8*)eptr, uv) - utf8buf; |
2cf2cfc6 | 8579 | is_utf8 = TRUE; |
7e2040f0 GS |
8580 | } |
8581 | else { | |
8582 | c = (char)uv; | |
8583 | eptr = &c; | |
8584 | elen = 1; | |
a0ed51b3 | 8585 | } |
46fc3d4c | 8586 | goto string; |
8587 | ||
46fc3d4c | 8588 | case 's': |
be75b157 | 8589 | if (args && !vectorize) { |
fc36a67e | 8590 | eptr = va_arg(*args, char*); |
c635e13b | 8591 | if (eptr) |
1d7c1841 GS |
8592 | #ifdef MACOS_TRADITIONAL |
8593 | /* On MacOS, %#s format is used for Pascal strings */ | |
8594 | if (alt) | |
8595 | elen = *eptr++; | |
8596 | else | |
8597 | #endif | |
c635e13b | 8598 | elen = strlen(eptr); |
8599 | else { | |
8600 | eptr = nullstr; | |
8601 | elen = sizeof nullstr - 1; | |
8602 | } | |
46fc3d4c | 8603 | } |
211dfcf1 | 8604 | else { |
7e2040f0 GS |
8605 | eptr = SvPVx(argsv, elen); |
8606 | if (DO_UTF8(argsv)) { | |
a0ed51b3 LW |
8607 | if (has_precis && precis < elen) { |
8608 | I32 p = precis; | |
7e2040f0 | 8609 | sv_pos_u2b(argsv, &p, 0); /* sticks at end */ |
a0ed51b3 LW |
8610 | precis = p; |
8611 | } | |
8612 | if (width) { /* fudge width (can't fudge elen) */ | |
7e2040f0 | 8613 | width += elen - sv_len_utf8(argsv); |
a0ed51b3 | 8614 | } |
2cf2cfc6 | 8615 | is_utf8 = TRUE; |
a0ed51b3 LW |
8616 | } |
8617 | } | |
46fc3d4c | 8618 | goto string; |
8619 | ||
fc36a67e | 8620 | case '_': |
8621 | /* | |
8622 | * The "%_" hack might have to be changed someday, | |
8623 | * if ISO or ANSI decide to use '_' for something. | |
8624 | * So we keep it hidden from users' code. | |
8625 | */ | |
be75b157 | 8626 | if (!args || vectorize) |
fc36a67e | 8627 | goto unknown; |
211dfcf1 | 8628 | argsv = va_arg(*args, SV*); |
7e2040f0 GS |
8629 | eptr = SvPVx(argsv, elen); |
8630 | if (DO_UTF8(argsv)) | |
2cf2cfc6 | 8631 | is_utf8 = TRUE; |
fc36a67e | 8632 | |
46fc3d4c | 8633 | string: |
b22c7a20 | 8634 | vectorize = FALSE; |
46fc3d4c | 8635 | if (has_precis && elen > precis) |
8636 | elen = precis; | |
8637 | break; | |
8638 | ||
8639 | /* INTEGERS */ | |
8640 | ||
fc36a67e | 8641 | case 'p': |
be75b157 | 8642 | if (alt || vectorize) |
c2e66d9e | 8643 | goto unknown; |
211dfcf1 | 8644 | uv = PTR2UV(args ? va_arg(*args, void*) : argsv); |
fc36a67e | 8645 | base = 16; |
8646 | goto integer; | |
8647 | ||
46fc3d4c | 8648 | case 'D': |
29fe7a80 | 8649 | #ifdef IV_IS_QUAD |
22f3ae8c | 8650 | intsize = 'q'; |
29fe7a80 | 8651 | #else |
46fc3d4c | 8652 | intsize = 'l'; |
29fe7a80 | 8653 | #endif |
46fc3d4c | 8654 | /* FALL THROUGH */ |
8655 | case 'd': | |
8656 | case 'i': | |
b22c7a20 | 8657 | if (vectorize) { |
ba210ebe | 8658 | STRLEN ulen; |
211dfcf1 HS |
8659 | if (!veclen) |
8660 | continue; | |
2cf2cfc6 A |
8661 | if (vec_utf8) |
8662 | uv = utf8n_to_uvchr(vecstr, veclen, &ulen, | |
8663 | UTF8_ALLOW_ANYUV); | |
b22c7a20 | 8664 | else { |
e83d50c9 | 8665 | uv = *vecstr; |
b22c7a20 GS |
8666 | ulen = 1; |
8667 | } | |
8668 | vecstr += ulen; | |
8669 | veclen -= ulen; | |
e83d50c9 JP |
8670 | if (plus) |
8671 | esignbuf[esignlen++] = plus; | |
b22c7a20 GS |
8672 | } |
8673 | else if (args) { | |
46fc3d4c | 8674 | switch (intsize) { |
8675 | case 'h': iv = (short)va_arg(*args, int); break; | |
8676 | default: iv = va_arg(*args, int); break; | |
8677 | case 'l': iv = va_arg(*args, long); break; | |
fc36a67e | 8678 | case 'V': iv = va_arg(*args, IV); break; |
cf2093f6 JH |
8679 | #ifdef HAS_QUAD |
8680 | case 'q': iv = va_arg(*args, Quad_t); break; | |
8681 | #endif | |
46fc3d4c | 8682 | } |
8683 | } | |
8684 | else { | |
211dfcf1 | 8685 | iv = SvIVx(argsv); |
46fc3d4c | 8686 | switch (intsize) { |
8687 | case 'h': iv = (short)iv; break; | |
be28567c | 8688 | default: break; |
46fc3d4c | 8689 | case 'l': iv = (long)iv; break; |
fc36a67e | 8690 | case 'V': break; |
cf2093f6 JH |
8691 | #ifdef HAS_QUAD |
8692 | case 'q': iv = (Quad_t)iv; break; | |
8693 | #endif | |
46fc3d4c | 8694 | } |
8695 | } | |
e83d50c9 JP |
8696 | if ( !vectorize ) /* we already set uv above */ |
8697 | { | |
8698 | if (iv >= 0) { | |
8699 | uv = iv; | |
8700 | if (plus) | |
8701 | esignbuf[esignlen++] = plus; | |
8702 | } | |
8703 | else { | |
8704 | uv = -iv; | |
8705 | esignbuf[esignlen++] = '-'; | |
8706 | } | |
46fc3d4c | 8707 | } |
8708 | base = 10; | |
8709 | goto integer; | |
8710 | ||
fc36a67e | 8711 | case 'U': |
29fe7a80 | 8712 | #ifdef IV_IS_QUAD |
22f3ae8c | 8713 | intsize = 'q'; |
29fe7a80 | 8714 | #else |
fc36a67e | 8715 | intsize = 'l'; |
29fe7a80 | 8716 | #endif |
fc36a67e | 8717 | /* FALL THROUGH */ |
8718 | case 'u': | |
8719 | base = 10; | |
8720 | goto uns_integer; | |
8721 | ||
4f19785b WSI |
8722 | case 'b': |
8723 | base = 2; | |
8724 | goto uns_integer; | |
8725 | ||
46fc3d4c | 8726 | case 'O': |
29fe7a80 | 8727 | #ifdef IV_IS_QUAD |
22f3ae8c | 8728 | intsize = 'q'; |
29fe7a80 | 8729 | #else |
46fc3d4c | 8730 | intsize = 'l'; |
29fe7a80 | 8731 | #endif |
46fc3d4c | 8732 | /* FALL THROUGH */ |
8733 | case 'o': | |
8734 | base = 8; | |
8735 | goto uns_integer; | |
8736 | ||
8737 | case 'X': | |
46fc3d4c | 8738 | case 'x': |
8739 | base = 16; | |
46fc3d4c | 8740 | |
8741 | uns_integer: | |
b22c7a20 | 8742 | if (vectorize) { |
ba210ebe | 8743 | STRLEN ulen; |
b22c7a20 | 8744 | vector: |
211dfcf1 HS |
8745 | if (!veclen) |
8746 | continue; | |
2cf2cfc6 A |
8747 | if (vec_utf8) |
8748 | uv = utf8n_to_uvchr(vecstr, veclen, &ulen, | |
8749 | UTF8_ALLOW_ANYUV); | |
b22c7a20 | 8750 | else { |
a05b299f | 8751 | uv = *vecstr; |
b22c7a20 GS |
8752 | ulen = 1; |
8753 | } | |
8754 | vecstr += ulen; | |
8755 | veclen -= ulen; | |
8756 | } | |
8757 | else if (args) { | |
46fc3d4c | 8758 | switch (intsize) { |
8759 | case 'h': uv = (unsigned short)va_arg(*args, unsigned); break; | |
8760 | default: uv = va_arg(*args, unsigned); break; | |
8761 | case 'l': uv = va_arg(*args, unsigned long); break; | |
fc36a67e | 8762 | case 'V': uv = va_arg(*args, UV); break; |
cf2093f6 JH |
8763 | #ifdef HAS_QUAD |
8764 | case 'q': uv = va_arg(*args, Quad_t); break; | |
8765 | #endif | |
46fc3d4c | 8766 | } |
8767 | } | |
8768 | else { | |
211dfcf1 | 8769 | uv = SvUVx(argsv); |
46fc3d4c | 8770 | switch (intsize) { |
8771 | case 'h': uv = (unsigned short)uv; break; | |
be28567c | 8772 | default: break; |
46fc3d4c | 8773 | case 'l': uv = (unsigned long)uv; break; |
fc36a67e | 8774 | case 'V': break; |
cf2093f6 JH |
8775 | #ifdef HAS_QUAD |
8776 | case 'q': uv = (Quad_t)uv; break; | |
8777 | #endif | |
46fc3d4c | 8778 | } |
8779 | } | |
8780 | ||
8781 | integer: | |
46fc3d4c | 8782 | eptr = ebuf + sizeof ebuf; |
fc36a67e | 8783 | switch (base) { |
8784 | unsigned dig; | |
8785 | case 16: | |
c10ed8b9 HS |
8786 | if (!uv) |
8787 | alt = FALSE; | |
1d7c1841 GS |
8788 | p = (char*)((c == 'X') |
8789 | ? "0123456789ABCDEF" : "0123456789abcdef"); | |
fc36a67e | 8790 | do { |
8791 | dig = uv & 15; | |
8792 | *--eptr = p[dig]; | |
8793 | } while (uv >>= 4); | |
8794 | if (alt) { | |
46fc3d4c | 8795 | esignbuf[esignlen++] = '0'; |
fc36a67e | 8796 | esignbuf[esignlen++] = c; /* 'x' or 'X' */ |
46fc3d4c | 8797 | } |
fc36a67e | 8798 | break; |
8799 | case 8: | |
8800 | do { | |
8801 | dig = uv & 7; | |
8802 | *--eptr = '0' + dig; | |
8803 | } while (uv >>= 3); | |
8804 | if (alt && *eptr != '0') | |
8805 | *--eptr = '0'; | |
8806 | break; | |
4f19785b WSI |
8807 | case 2: |
8808 | do { | |
8809 | dig = uv & 1; | |
8810 | *--eptr = '0' + dig; | |
8811 | } while (uv >>= 1); | |
eda88b6d JH |
8812 | if (alt) { |
8813 | esignbuf[esignlen++] = '0'; | |
7481bb52 | 8814 | esignbuf[esignlen++] = 'b'; |
eda88b6d | 8815 | } |
4f19785b | 8816 | break; |
fc36a67e | 8817 | default: /* it had better be ten or less */ |
6bc102ca | 8818 | #if defined(PERL_Y2KWARN) |
e476b1b5 | 8819 | if (ckWARN(WARN_Y2K)) { |
6bc102ca GS |
8820 | STRLEN n; |
8821 | char *s = SvPV(sv,n); | |
8822 | if (n >= 2 && s[n-2] == '1' && s[n-1] == '9' | |
8823 | && (n == 2 || !isDIGIT(s[n-3]))) | |
8824 | { | |
9014280d | 8825 | Perl_warner(aTHX_ packWARN(WARN_Y2K), |
6bc102ca GS |
8826 | "Possible Y2K bug: %%%c %s", |
8827 | c, "format string following '19'"); | |
8828 | } | |
8829 | } | |
8830 | #endif | |
fc36a67e | 8831 | do { |
8832 | dig = uv % base; | |
8833 | *--eptr = '0' + dig; | |
8834 | } while (uv /= base); | |
8835 | break; | |
46fc3d4c | 8836 | } |
8837 | elen = (ebuf + sizeof ebuf) - eptr; | |
c10ed8b9 HS |
8838 | if (has_precis) { |
8839 | if (precis > elen) | |
8840 | zeros = precis - elen; | |
8841 | else if (precis == 0 && elen == 1 && *eptr == '0') | |
8842 | elen = 0; | |
8843 | } | |
46fc3d4c | 8844 | break; |
8845 | ||
8846 | /* FLOATING POINT */ | |
8847 | ||
fc36a67e | 8848 | case 'F': |
8849 | c = 'f'; /* maybe %F isn't supported here */ | |
8850 | /* FALL THROUGH */ | |
46fc3d4c | 8851 | case 'e': case 'E': |
fc36a67e | 8852 | case 'f': |
46fc3d4c | 8853 | case 'g': case 'G': |
8854 | ||
8855 | /* This is evil, but floating point is even more evil */ | |
8856 | ||
9e5b023a JH |
8857 | /* for SV-style calling, we can only get NV |
8858 | for C-style calling, we assume %f is double; | |
8859 | for simplicity we allow any of %Lf, %llf, %qf for long double | |
8860 | */ | |
8861 | switch (intsize) { | |
8862 | case 'V': | |
8863 | #if defined(USE_LONG_DOUBLE) | |
8864 | intsize = 'q'; | |
8865 | #endif | |
8866 | break; | |
f8cf5370 JH |
8867 | /* [perl #20339] - we should accept and ignore %lf rather than die */ |
8868 | case 'l': | |
8869 | /* FALL THROUGH */ | |
9e5b023a JH |
8870 | default: |
8871 | #if defined(USE_LONG_DOUBLE) | |
8872 | intsize = args ? 0 : 'q'; | |
8873 | #endif | |
8874 | break; | |
8875 | case 'q': | |
8876 | #if defined(HAS_LONG_DOUBLE) | |
8877 | break; | |
8878 | #else | |
8879 | /* FALL THROUGH */ | |
8880 | #endif | |
8881 | case 'h': | |
9e5b023a JH |
8882 | goto unknown; |
8883 | } | |
8884 | ||
8885 | /* now we need (long double) if intsize == 'q', else (double) */ | |
be75b157 | 8886 | nv = (args && !vectorize) ? |
35fff930 JH |
8887 | #if LONG_DOUBLESIZE > DOUBLESIZE |
8888 | intsize == 'q' ? | |
5b7ea690 JH |
8889 | va_arg(*args, long double) : |
8890 | va_arg(*args, double) | |
35fff930 | 8891 | #else |
5b7ea690 | 8892 | va_arg(*args, double) |
35fff930 | 8893 | #endif |
9e5b023a | 8894 | : SvNVx(argsv); |
fc36a67e | 8895 | |
8896 | need = 0; | |
be75b157 | 8897 | vectorize = FALSE; |
fc36a67e | 8898 | if (c != 'e' && c != 'E') { |
8899 | i = PERL_INT_MIN; | |
9e5b023a JH |
8900 | /* FIXME: if HAS_LONG_DOUBLE but not USE_LONG_DOUBLE this |
8901 | will cast our (long double) to (double) */ | |
73b309ea | 8902 | (void)Perl_frexp(nv, &i); |
fc36a67e | 8903 | if (i == PERL_INT_MIN) |
cea2e8a9 | 8904 | Perl_die(aTHX_ "panic: frexp"); |
c635e13b | 8905 | if (i > 0) |
fc36a67e | 8906 | need = BIT_DIGITS(i); |
8907 | } | |
8908 | need += has_precis ? precis : 6; /* known default */ | |
5b7ea690 | 8909 | |
fc36a67e | 8910 | if (need < width) |
8911 | need = width; | |
8912 | ||
5b7ea690 JH |
8913 | #ifdef HAS_LDBL_SPRINTF_BUG |
8914 | /* This is to try to fix a bug with irix/nonstop-ux/powerux and | |
8915 | with sfio - Allen <allens@cpan.org> */ | |
8916 | ||
8917 | # ifdef DBL_MAX | |
8918 | # define MY_DBL_MAX DBL_MAX | |
8919 | # else /* XXX guessing! HUGE_VAL may be defined as infinity, so not using */ | |
8920 | # if DOUBLESIZE >= 8 | |
8921 | # define MY_DBL_MAX 1.7976931348623157E+308L | |
8922 | # else | |
8923 | # define MY_DBL_MAX 3.40282347E+38L | |
8924 | # endif | |
8925 | # endif | |
8926 | ||
8927 | # ifdef HAS_LDBL_SPRINTF_BUG_LESS1 /* only between -1L & 1L - Allen */ | |
8928 | # define MY_DBL_MAX_BUG 1L | |
8929 | # else | |
8930 | # define MY_DBL_MAX_BUG MY_DBL_MAX | |
8931 | # endif | |
8932 | ||
8933 | # ifdef DBL_MIN | |
8934 | # define MY_DBL_MIN DBL_MIN | |
8935 | # else /* XXX guessing! -Allen */ | |
8936 | # if DOUBLESIZE >= 8 | |
8937 | # define MY_DBL_MIN 2.2250738585072014E-308L | |
8938 | # else | |
8939 | # define MY_DBL_MIN 1.17549435E-38L | |
8940 | # endif | |
8941 | # endif | |
8942 | ||
8943 | if ((intsize == 'q') && (c == 'f') && | |
8944 | ((nv < MY_DBL_MAX_BUG) && (nv > -MY_DBL_MAX_BUG)) && | |
8945 | (need < DBL_DIG)) { | |
8946 | /* it's going to be short enough that | |
8947 | * long double precision is not needed */ | |
8948 | ||
8949 | if ((nv <= 0L) && (nv >= -0L)) | |
8950 | fix_ldbl_sprintf_bug = TRUE; /* 0 is 0 - easiest */ | |
8951 | else { | |
8952 | /* would use Perl_fp_class as a double-check but not | |
8953 | * functional on IRIX - see perl.h comments */ | |
8954 | ||
8955 | if ((nv >= MY_DBL_MIN) || (nv <= -MY_DBL_MIN)) { | |
8956 | /* It's within the range that a double can represent */ | |
8957 | #if defined(DBL_MAX) && !defined(DBL_MIN) | |
8958 | if ((nv >= ((long double)1/DBL_MAX)) || | |
8959 | (nv <= (-(long double)1/DBL_MAX))) | |
8960 | #endif | |
8961 | fix_ldbl_sprintf_bug = TRUE; | |
8962 | } | |
8963 | } | |
8964 | if (fix_ldbl_sprintf_bug == TRUE) { | |
8965 | double temp; | |
8966 | ||
8967 | intsize = 0; | |
8968 | temp = (double)nv; | |
8969 | nv = (NV)temp; | |
8970 | } | |
8971 | } | |
8972 | ||
8973 | # undef MY_DBL_MAX | |
8974 | # undef MY_DBL_MAX_BUG | |
8975 | # undef MY_DBL_MIN | |
8976 | ||
8977 | #endif /* HAS_LDBL_SPRINTF_BUG */ | |
8978 | ||
46fc3d4c | 8979 | need += 20; /* fudge factor */ |
80252599 GS |
8980 | if (PL_efloatsize < need) { |
8981 | Safefree(PL_efloatbuf); | |
8982 | PL_efloatsize = need + 20; /* more fudge */ | |
8983 | New(906, PL_efloatbuf, PL_efloatsize, char); | |
7d5ea4e7 | 8984 | PL_efloatbuf[0] = '\0'; |
46fc3d4c | 8985 | } |
8986 | ||
8987 | eptr = ebuf + sizeof ebuf; | |
8988 | *--eptr = '\0'; | |
8989 | *--eptr = c; | |
9e5b023a JH |
8990 | /* FIXME: what to do if HAS_LONG_DOUBLE but not PERL_PRIfldbl? */ |
8991 | #if defined(HAS_LONG_DOUBLE) && defined(PERL_PRIfldbl) | |
8992 | if (intsize == 'q') { | |
e5c81feb JH |
8993 | /* Copy the one or more characters in a long double |
8994 | * format before the 'base' ([efgEFG]) character to | |
8995 | * the format string. */ | |
8996 | static char const prifldbl[] = PERL_PRIfldbl; | |
8997 | char const *p = prifldbl + sizeof(prifldbl) - 3; | |
8998 | while (p >= prifldbl) { *--eptr = *p--; } | |
cf2093f6 | 8999 | } |
65202027 | 9000 | #endif |
46fc3d4c | 9001 | if (has_precis) { |
9002 | base = precis; | |
9003 | do { *--eptr = '0' + (base % 10); } while (base /= 10); | |
9004 | *--eptr = '.'; | |
9005 | } | |
9006 | if (width) { | |
9007 | base = width; | |
9008 | do { *--eptr = '0' + (base % 10); } while (base /= 10); | |
9009 | } | |
9010 | if (fill == '0') | |
9011 | *--eptr = fill; | |
84902520 TB |
9012 | if (left) |
9013 | *--eptr = '-'; | |
46fc3d4c | 9014 | if (plus) |
9015 | *--eptr = plus; | |
9016 | if (alt) | |
9017 | *--eptr = '#'; | |
9018 | *--eptr = '%'; | |
9019 | ||
ff9121f8 JH |
9020 | /* No taint. Otherwise we are in the strange situation |
9021 | * where printf() taints but print($float) doesn't. | |
bda0f7a5 | 9022 | * --jhi */ |
9e5b023a JH |
9023 | #if defined(HAS_LONG_DOUBLE) |
9024 | if (intsize == 'q') | |
9025 | (void)sprintf(PL_efloatbuf, eptr, nv); | |
9026 | else | |
9027 | (void)sprintf(PL_efloatbuf, eptr, (double)nv); | |
9028 | #else | |
dd8482fc | 9029 | (void)sprintf(PL_efloatbuf, eptr, nv); |
9e5b023a | 9030 | #endif |
80252599 GS |
9031 | eptr = PL_efloatbuf; |
9032 | elen = strlen(PL_efloatbuf); | |
46fc3d4c | 9033 | break; |
9034 | ||
fc36a67e | 9035 | /* SPECIAL */ |
9036 | ||
9037 | case 'n': | |
9038 | i = SvCUR(sv) - origlen; | |
be75b157 | 9039 | if (args && !vectorize) { |
c635e13b | 9040 | switch (intsize) { |
9041 | case 'h': *(va_arg(*args, short*)) = i; break; | |
9042 | default: *(va_arg(*args, int*)) = i; break; | |
9043 | case 'l': *(va_arg(*args, long*)) = i; break; | |
9044 | case 'V': *(va_arg(*args, IV*)) = i; break; | |
cf2093f6 JH |
9045 | #ifdef HAS_QUAD |
9046 | case 'q': *(va_arg(*args, Quad_t*)) = i; break; | |
9047 | #endif | |
c635e13b | 9048 | } |
fc36a67e | 9049 | } |
9dd79c3f | 9050 | else |
211dfcf1 | 9051 | sv_setuv_mg(argsv, (UV)i); |
be75b157 | 9052 | vectorize = FALSE; |
fc36a67e | 9053 | continue; /* not "break" */ |
9054 | ||
9055 | /* UNKNOWN */ | |
9056 | ||
46fc3d4c | 9057 | default: |
fc36a67e | 9058 | unknown: |
599cee73 | 9059 | if (!args && ckWARN(WARN_PRINTF) && |
533c011a | 9060 | (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF)) { |
c635e13b | 9061 | SV *msg = sv_newmortal(); |
c293eb2b NC |
9062 | Perl_sv_setpvf(aTHX_ msg, "Invalid conversion in %sprintf: ", |
9063 | (PL_op->op_type == OP_PRTF) ? "" : "s"); | |
0f4b6630 | 9064 | if (c) { |
0f4b6630 | 9065 | if (isPRINT(c)) |
1c846c1f | 9066 | Perl_sv_catpvf(aTHX_ msg, |
0f4b6630 JH |
9067 | "\"%%%c\"", c & 0xFF); |
9068 | else | |
9069 | Perl_sv_catpvf(aTHX_ msg, | |
57def98f | 9070 | "\"%%\\%03"UVof"\"", |
0f4b6630 | 9071 | (UV)c & 0xFF); |
0f4b6630 | 9072 | } else |
c635e13b | 9073 | sv_catpv(msg, "end of string"); |
9014280d | 9074 | Perl_warner(aTHX_ packWARN(WARN_PRINTF), "%"SVf, msg); /* yes, this is reentrant */ |
c635e13b | 9075 | } |
fb73857a | 9076 | |
9077 | /* output mangled stuff ... */ | |
9078 | if (c == '\0') | |
9079 | --q; | |
46fc3d4c | 9080 | eptr = p; |
9081 | elen = q - p; | |
fb73857a | 9082 | |
9083 | /* ... right here, because formatting flags should not apply */ | |
9084 | SvGROW(sv, SvCUR(sv) + elen + 1); | |
9085 | p = SvEND(sv); | |
4459522c | 9086 | Copy(eptr, p, elen, char); |
fb73857a | 9087 | p += elen; |
9088 | *p = '\0'; | |
9089 | SvCUR(sv) = p - SvPVX(sv); | |
d34f9d2e | 9090 | svix = osvix; |
fb73857a | 9091 | continue; /* not "break" */ |
46fc3d4c | 9092 | } |
9093 | ||
d2876be5 JH |
9094 | if (is_utf8 != has_utf8) { |
9095 | if (is_utf8) { | |
9096 | if (SvCUR(sv)) | |
9097 | sv_utf8_upgrade(sv); | |
9098 | } | |
9099 | else { | |
9100 | SV *nsv = sv_2mortal(newSVpvn(eptr, elen)); | |
9101 | sv_utf8_upgrade(nsv); | |
9102 | eptr = SvPVX(nsv); | |
9103 | elen = SvCUR(nsv); | |
9104 | } | |
9105 | SvGROW(sv, SvCUR(sv) + elen + 1); | |
9106 | p = SvEND(sv); | |
9107 | *p = '\0'; | |
9108 | } | |
18729d3e JH |
9109 | /* Use memchr() instead of strchr(), as eptr is not guaranteed */ |
9110 | /* to point to a null-terminated string. */ | |
9111 | if (left && ckWARN(WARN_PRINTF) && memchr(eptr, '\n', elen) && | |
67bae6b3 JH |
9112 | (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF)) |
9113 | Perl_warner(aTHX_ packWARN(WARN_PRINTF), | |
9114 | "Newline in left-justified string for %sprintf", | |
9115 | (PL_op->op_type == OP_PRTF) ? "" : "s"); | |
d2876be5 | 9116 | |
fc36a67e | 9117 | have = esignlen + zeros + elen; |
46fc3d4c | 9118 | need = (have > width ? have : width); |
9119 | gap = need - have; | |
9120 | ||
b22c7a20 | 9121 | SvGROW(sv, SvCUR(sv) + need + dotstrlen + 1); |
46fc3d4c | 9122 | p = SvEND(sv); |
9123 | if (esignlen && fill == '0') { | |
eb160463 | 9124 | for (i = 0; i < (int)esignlen; i++) |
46fc3d4c | 9125 | *p++ = esignbuf[i]; |
9126 | } | |
9127 | if (gap && !left) { | |
9128 | memset(p, fill, gap); | |
9129 | p += gap; | |
9130 | } | |
9131 | if (esignlen && fill != '0') { | |
eb160463 | 9132 | for (i = 0; i < (int)esignlen; i++) |
46fc3d4c | 9133 | *p++ = esignbuf[i]; |
9134 | } | |
fc36a67e | 9135 | if (zeros) { |
9136 | for (i = zeros; i; i--) | |
9137 | *p++ = '0'; | |
9138 | } | |
46fc3d4c | 9139 | if (elen) { |
4459522c | 9140 | Copy(eptr, p, elen, char); |
46fc3d4c | 9141 | p += elen; |
9142 | } | |
9143 | if (gap && left) { | |
9144 | memset(p, ' ', gap); | |
9145 | p += gap; | |
9146 | } | |
b22c7a20 GS |
9147 | if (vectorize) { |
9148 | if (veclen) { | |
4459522c | 9149 | Copy(dotstr, p, dotstrlen, char); |
b22c7a20 GS |
9150 | p += dotstrlen; |
9151 | } | |
9152 | else | |
9153 | vectorize = FALSE; /* done iterating over vecstr */ | |
9154 | } | |
2cf2cfc6 A |
9155 | if (is_utf8) |
9156 | has_utf8 = TRUE; | |
9157 | if (has_utf8) | |
7e2040f0 | 9158 | SvUTF8_on(sv); |
46fc3d4c | 9159 | *p = '\0'; |
9160 | SvCUR(sv) = p - SvPVX(sv); | |
b22c7a20 GS |
9161 | if (vectorize) { |
9162 | esignlen = 0; | |
9163 | goto vector; | |
9164 | } | |
46fc3d4c | 9165 | } |
9166 | } | |
51371543 | 9167 | |
645c22ef DM |
9168 | /* ========================================================================= |
9169 | ||
9170 | =head1 Cloning an interpreter | |
9171 | ||
9172 | All the macros and functions in this section are for the private use of | |
9173 | the main function, perl_clone(). | |
9174 | ||
9175 | The foo_dup() functions make an exact copy of an existing foo thinngy. | |
9176 | During the course of a cloning, a hash table is used to map old addresses | |
9177 | to new addresses. The table is created and manipulated with the | |
9178 | ptr_table_* functions. | |
9179 | ||
9180 | =cut | |
9181 | ||
9182 | ============================================================================*/ | |
9183 | ||
9184 | ||
1d7c1841 GS |
9185 | #if defined(USE_ITHREADS) |
9186 | ||
4d1ff10f AB |
9187 | #if defined(USE_5005THREADS) |
9188 | # include "error: USE_5005THREADS and USE_ITHREADS are incompatible" | |
1d7c1841 GS |
9189 | #endif |
9190 | ||
1d7c1841 GS |
9191 | #ifndef GpREFCNT_inc |
9192 | # define GpREFCNT_inc(gp) ((gp) ? (++(gp)->gp_refcnt, (gp)) : (GP*)NULL) | |
9193 | #endif | |
9194 | ||
9195 | ||
d2d73c3e AB |
9196 | #define sv_dup_inc(s,t) SvREFCNT_inc(sv_dup(s,t)) |
9197 | #define av_dup(s,t) (AV*)sv_dup((SV*)s,t) | |
9198 | #define av_dup_inc(s,t) (AV*)SvREFCNT_inc(sv_dup((SV*)s,t)) | |
9199 | #define hv_dup(s,t) (HV*)sv_dup((SV*)s,t) | |
9200 | #define hv_dup_inc(s,t) (HV*)SvREFCNT_inc(sv_dup((SV*)s,t)) | |
9201 | #define cv_dup(s,t) (CV*)sv_dup((SV*)s,t) | |
9202 | #define cv_dup_inc(s,t) (CV*)SvREFCNT_inc(sv_dup((SV*)s,t)) | |
9203 | #define io_dup(s,t) (IO*)sv_dup((SV*)s,t) | |
9204 | #define io_dup_inc(s,t) (IO*)SvREFCNT_inc(sv_dup((SV*)s,t)) | |
9205 | #define gv_dup(s,t) (GV*)sv_dup((SV*)s,t) | |
9206 | #define gv_dup_inc(s,t) (GV*)SvREFCNT_inc(sv_dup((SV*)s,t)) | |
1d7c1841 GS |
9207 | #define SAVEPV(p) (p ? savepv(p) : Nullch) |
9208 | #define SAVEPVN(p,n) (p ? savepvn(p,n) : Nullch) | |
8cf8f3d1 | 9209 | |
d2d73c3e | 9210 | |
d2f185dc AMS |
9211 | /* Duplicate a regexp. Required reading: pregcomp() and pregfree() in |
9212 | regcomp.c. AMS 20010712 */ | |
645c22ef | 9213 | |
1d7c1841 | 9214 | REGEXP * |
a8fc9800 | 9215 | Perl_re_dup(pTHX_ REGEXP *r, CLONE_PARAMS *param) |
1d7c1841 | 9216 | { |
d2f185dc AMS |
9217 | REGEXP *ret; |
9218 | int i, len, npar; | |
9219 | struct reg_substr_datum *s; | |
9220 | ||
9221 | if (!r) | |
9222 | return (REGEXP *)NULL; | |
9223 | ||
9224 | if ((ret = (REGEXP *)ptr_table_fetch(PL_ptr_table, r))) | |
9225 | return ret; | |
9226 | ||
9227 | len = r->offsets[0]; | |
9228 | npar = r->nparens+1; | |
9229 | ||
9230 | Newc(0, ret, sizeof(regexp) + (len+1)*sizeof(regnode), char, regexp); | |
9231 | Copy(r->program, ret->program, len+1, regnode); | |
9232 | ||
9233 | New(0, ret->startp, npar, I32); | |
9234 | Copy(r->startp, ret->startp, npar, I32); | |
9235 | New(0, ret->endp, npar, I32); | |
9236 | Copy(r->startp, ret->startp, npar, I32); | |
9237 | ||
d2f185dc AMS |
9238 | New(0, ret->substrs, 1, struct reg_substr_data); |
9239 | for (s = ret->substrs->data, i = 0; i < 3; i++, s++) { | |
9240 | s->min_offset = r->substrs->data[i].min_offset; | |
9241 | s->max_offset = r->substrs->data[i].max_offset; | |
9242 | s->substr = sv_dup_inc(r->substrs->data[i].substr, param); | |
33b8afdf | 9243 | s->utf8_substr = sv_dup_inc(r->substrs->data[i].utf8_substr, param); |
d2f185dc AMS |
9244 | } |
9245 | ||
70612e96 | 9246 | ret->regstclass = NULL; |
d2f185dc AMS |
9247 | if (r->data) { |
9248 | struct reg_data *d; | |
9249 | int count = r->data->count; | |
9250 | ||
9251 | Newc(0, d, sizeof(struct reg_data) + count*sizeof(void *), | |
9252 | char, struct reg_data); | |
9253 | New(0, d->what, count, U8); | |
9254 | ||
9255 | d->count = count; | |
9256 | for (i = 0; i < count; i++) { | |
9257 | d->what[i] = r->data->what[i]; | |
9258 | switch (d->what[i]) { | |
9259 | case 's': | |
9260 | d->data[i] = sv_dup_inc((SV *)r->data->data[i], param); | |
9261 | break; | |
9262 | case 'p': | |
9263 | d->data[i] = av_dup_inc((AV *)r->data->data[i], param); | |
9264 | break; | |
9265 | case 'f': | |
9266 | /* This is cheating. */ | |
9267 | New(0, d->data[i], 1, struct regnode_charclass_class); | |
9268 | StructCopy(r->data->data[i], d->data[i], | |
9269 | struct regnode_charclass_class); | |
70612e96 | 9270 | ret->regstclass = (regnode*)d->data[i]; |
d2f185dc AMS |
9271 | break; |
9272 | case 'o': | |
33773810 AMS |
9273 | /* Compiled op trees are readonly, and can thus be |
9274 | shared without duplication. */ | |
9b978d73 DM |
9275 | d->data[i] = (void*)OpREFCNT_inc((OP*)r->data->data[i]); |
9276 | break; | |
d2f185dc AMS |
9277 | case 'n': |
9278 | d->data[i] = r->data->data[i]; | |
9279 | break; | |
9280 | } | |
9281 | } | |
9282 | ||
9283 | ret->data = d; | |
9284 | } | |
9285 | else | |
9286 | ret->data = NULL; | |
9287 | ||
9288 | New(0, ret->offsets, 2*len+1, U32); | |
9289 | Copy(r->offsets, ret->offsets, 2*len+1, U32); | |
9290 | ||
9291 | ret->precomp = SAVEPV(r->precomp); | |
d2f185dc AMS |
9292 | ret->refcnt = r->refcnt; |
9293 | ret->minlen = r->minlen; | |
9294 | ret->prelen = r->prelen; | |
9295 | ret->nparens = r->nparens; | |
9296 | ret->lastparen = r->lastparen; | |
9297 | ret->lastcloseparen = r->lastcloseparen; | |
9298 | ret->reganch = r->reganch; | |
9299 | ||
70612e96 RG |
9300 | ret->sublen = r->sublen; |
9301 | ||
9302 | if (RX_MATCH_COPIED(ret)) | |
9303 | ret->subbeg = SAVEPV(r->subbeg); | |
9304 | else | |
9305 | ret->subbeg = Nullch; | |
9306 | ||
d2f185dc AMS |
9307 | ptr_table_store(PL_ptr_table, r, ret); |
9308 | return ret; | |
1d7c1841 GS |
9309 | } |
9310 | ||
d2d73c3e | 9311 | /* duplicate a file handle */ |
645c22ef | 9312 | |
1d7c1841 | 9313 | PerlIO * |
a8fc9800 | 9314 | Perl_fp_dup(pTHX_ PerlIO *fp, char type, CLONE_PARAMS *param) |
1d7c1841 GS |
9315 | { |
9316 | PerlIO *ret; | |
9317 | if (!fp) | |
9318 | return (PerlIO*)NULL; | |
9319 | ||
9320 | /* look for it in the table first */ | |
9321 | ret = (PerlIO*)ptr_table_fetch(PL_ptr_table, fp); | |
9322 | if (ret) | |
9323 | return ret; | |
9324 | ||
9325 | /* create anew and remember what it is */ | |
ecdeb87c | 9326 | ret = PerlIO_fdupopen(aTHX_ fp, param, PERLIO_DUP_CLONE); |
1d7c1841 GS |
9327 | ptr_table_store(PL_ptr_table, fp, ret); |
9328 | return ret; | |
9329 | } | |
9330 | ||
645c22ef DM |
9331 | /* duplicate a directory handle */ |
9332 | ||
1d7c1841 GS |
9333 | DIR * |
9334 | Perl_dirp_dup(pTHX_ DIR *dp) | |
9335 | { | |
9336 | if (!dp) | |
9337 | return (DIR*)NULL; | |
9338 | /* XXX TODO */ | |
9339 | return dp; | |
9340 | } | |
9341 | ||
ff276b08 | 9342 | /* duplicate a typeglob */ |
645c22ef | 9343 | |
1d7c1841 | 9344 | GP * |
a8fc9800 | 9345 | Perl_gp_dup(pTHX_ GP *gp, CLONE_PARAMS* param) |
1d7c1841 GS |
9346 | { |
9347 | GP *ret; | |
9348 | if (!gp) | |
9349 | return (GP*)NULL; | |
9350 | /* look for it in the table first */ | |
9351 | ret = (GP*)ptr_table_fetch(PL_ptr_table, gp); | |
9352 | if (ret) | |
9353 | return ret; | |
9354 | ||
9355 | /* create anew and remember what it is */ | |
9356 | Newz(0, ret, 1, GP); | |
9357 | ptr_table_store(PL_ptr_table, gp, ret); | |
9358 | ||
9359 | /* clone */ | |
9360 | ret->gp_refcnt = 0; /* must be before any other dups! */ | |
d2d73c3e AB |
9361 | ret->gp_sv = sv_dup_inc(gp->gp_sv, param); |
9362 | ret->gp_io = io_dup_inc(gp->gp_io, param); | |
9363 | ret->gp_form = cv_dup_inc(gp->gp_form, param); | |
9364 | ret->gp_av = av_dup_inc(gp->gp_av, param); | |
9365 | ret->gp_hv = hv_dup_inc(gp->gp_hv, param); | |
9366 | ret->gp_egv = gv_dup(gp->gp_egv, param);/* GvEGV is not refcounted */ | |
9367 | ret->gp_cv = cv_dup_inc(gp->gp_cv, param); | |
1d7c1841 GS |
9368 | ret->gp_cvgen = gp->gp_cvgen; |
9369 | ret->gp_flags = gp->gp_flags; | |
9370 | ret->gp_line = gp->gp_line; | |
9371 | ret->gp_file = gp->gp_file; /* points to COP.cop_file */ | |
9372 | return ret; | |
9373 | } | |
9374 | ||
645c22ef DM |
9375 | /* duplicate a chain of magic */ |
9376 | ||
1d7c1841 | 9377 | MAGIC * |
a8fc9800 | 9378 | Perl_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS* param) |
1d7c1841 | 9379 | { |
cb359b41 JH |
9380 | MAGIC *mgprev = (MAGIC*)NULL; |
9381 | MAGIC *mgret; | |
1d7c1841 GS |
9382 | if (!mg) |
9383 | return (MAGIC*)NULL; | |
9384 | /* look for it in the table first */ | |
9385 | mgret = (MAGIC*)ptr_table_fetch(PL_ptr_table, mg); | |
9386 | if (mgret) | |
9387 | return mgret; | |
9388 | ||
9389 | for (; mg; mg = mg->mg_moremagic) { | |
9390 | MAGIC *nmg; | |
9391 | Newz(0, nmg, 1, MAGIC); | |
cb359b41 | 9392 | if (mgprev) |
1d7c1841 | 9393 | mgprev->mg_moremagic = nmg; |
cb359b41 JH |
9394 | else |
9395 | mgret = nmg; | |
1d7c1841 GS |
9396 | nmg->mg_virtual = mg->mg_virtual; /* XXX copy dynamic vtable? */ |
9397 | nmg->mg_private = mg->mg_private; | |
9398 | nmg->mg_type = mg->mg_type; | |
9399 | nmg->mg_flags = mg->mg_flags; | |
14befaf4 | 9400 | if (mg->mg_type == PERL_MAGIC_qr) { |
d2f185dc | 9401 | nmg->mg_obj = (SV*)re_dup((REGEXP*)mg->mg_obj, param); |
1d7c1841 | 9402 | } |
05bd4103 JH |
9403 | else if(mg->mg_type == PERL_MAGIC_backref) { |
9404 | AV *av = (AV*) mg->mg_obj; | |
9405 | SV **svp; | |
9406 | I32 i; | |
9407 | nmg->mg_obj = (SV*)newAV(); | |
9408 | svp = AvARRAY(av); | |
9409 | i = AvFILLp(av); | |
9410 | while (i >= 0) { | |
9411 | av_push((AV*)nmg->mg_obj,sv_dup(svp[i],param)); | |
9412 | i--; | |
9413 | } | |
9414 | } | |
1d7c1841 GS |
9415 | else { |
9416 | nmg->mg_obj = (mg->mg_flags & MGf_REFCOUNTED) | |
d2d73c3e AB |
9417 | ? sv_dup_inc(mg->mg_obj, param) |
9418 | : sv_dup(mg->mg_obj, param); | |
1d7c1841 GS |
9419 | } |
9420 | nmg->mg_len = mg->mg_len; | |
9421 | nmg->mg_ptr = mg->mg_ptr; /* XXX random ptr? */ | |
14befaf4 | 9422 | if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) { |
68795e93 | 9423 | if (mg->mg_len > 0) { |
1d7c1841 | 9424 | nmg->mg_ptr = SAVEPVN(mg->mg_ptr, mg->mg_len); |
14befaf4 DM |
9425 | if (mg->mg_type == PERL_MAGIC_overload_table && |
9426 | AMT_AMAGIC((AMT*)mg->mg_ptr)) | |
9427 | { | |
1d7c1841 GS |
9428 | AMT *amtp = (AMT*)mg->mg_ptr; |
9429 | AMT *namtp = (AMT*)nmg->mg_ptr; | |
9430 | I32 i; | |
9431 | for (i = 1; i < NofAMmeth; i++) { | |
d2d73c3e | 9432 | namtp->table[i] = cv_dup_inc(amtp->table[i], param); |
1d7c1841 GS |
9433 | } |
9434 | } | |
9435 | } | |
9436 | else if (mg->mg_len == HEf_SVKEY) | |
d2d73c3e | 9437 | nmg->mg_ptr = (char*)sv_dup_inc((SV*)mg->mg_ptr, param); |
1d7c1841 | 9438 | } |
68795e93 NIS |
9439 | if ((mg->mg_flags & MGf_DUP) && mg->mg_virtual && mg->mg_virtual->svt_dup) { |
9440 | CALL_FPTR(nmg->mg_virtual->svt_dup)(aTHX_ nmg, param); | |
9441 | } | |
1d7c1841 GS |
9442 | mgprev = nmg; |
9443 | } | |
9444 | return mgret; | |
9445 | } | |
9446 | ||
645c22ef DM |
9447 | /* create a new pointer-mapping table */ |
9448 | ||
1d7c1841 GS |
9449 | PTR_TBL_t * |
9450 | Perl_ptr_table_new(pTHX) | |
9451 | { | |
9452 | PTR_TBL_t *tbl; | |
9453 | Newz(0, tbl, 1, PTR_TBL_t); | |
9454 | tbl->tbl_max = 511; | |
9455 | tbl->tbl_items = 0; | |
9456 | Newz(0, tbl->tbl_ary, tbl->tbl_max + 1, PTR_TBL_ENT_t*); | |
9457 | return tbl; | |
9458 | } | |
9459 | ||
645c22ef DM |
9460 | /* map an existing pointer using a table */ |
9461 | ||
1d7c1841 GS |
9462 | void * |
9463 | Perl_ptr_table_fetch(pTHX_ PTR_TBL_t *tbl, void *sv) | |
9464 | { | |
9465 | PTR_TBL_ENT_t *tblent; | |
d2a79402 | 9466 | UV hash = PTR2UV(sv); |
1d7c1841 GS |
9467 | assert(tbl); |
9468 | tblent = tbl->tbl_ary[hash & tbl->tbl_max]; | |
9469 | for (; tblent; tblent = tblent->next) { | |
9470 | if (tblent->oldval == sv) | |
9471 | return tblent->newval; | |
9472 | } | |
9473 | return (void*)NULL; | |
9474 | } | |
9475 | ||
645c22ef DM |
9476 | /* add a new entry to a pointer-mapping table */ |
9477 | ||
1d7c1841 GS |
9478 | void |
9479 | Perl_ptr_table_store(pTHX_ PTR_TBL_t *tbl, void *oldv, void *newv) | |
9480 | { | |
9481 | PTR_TBL_ENT_t *tblent, **otblent; | |
9482 | /* XXX this may be pessimal on platforms where pointers aren't good | |
9483 | * hash values e.g. if they grow faster in the most significant | |
9484 | * bits */ | |
d2a79402 | 9485 | UV hash = PTR2UV(oldv); |
1d7c1841 GS |
9486 | bool i = 1; |
9487 | ||
9488 | assert(tbl); | |
9489 | otblent = &tbl->tbl_ary[hash & tbl->tbl_max]; | |
9490 | for (tblent = *otblent; tblent; i=0, tblent = tblent->next) { | |
9491 | if (tblent->oldval == oldv) { | |
9492 | tblent->newval = newv; | |
1d7c1841 GS |
9493 | return; |
9494 | } | |
9495 | } | |
9496 | Newz(0, tblent, 1, PTR_TBL_ENT_t); | |
9497 | tblent->oldval = oldv; | |
9498 | tblent->newval = newv; | |
9499 | tblent->next = *otblent; | |
9500 | *otblent = tblent; | |
9501 | tbl->tbl_items++; | |
9502 | if (i && tbl->tbl_items > tbl->tbl_max) | |
9503 | ptr_table_split(tbl); | |
9504 | } | |
9505 | ||
645c22ef DM |
9506 | /* double the hash bucket size of an existing ptr table */ |
9507 | ||
1d7c1841 GS |
9508 | void |
9509 | Perl_ptr_table_split(pTHX_ PTR_TBL_t *tbl) | |
9510 | { | |
9511 | PTR_TBL_ENT_t **ary = tbl->tbl_ary; | |
9512 | UV oldsize = tbl->tbl_max + 1; | |
9513 | UV newsize = oldsize * 2; | |
9514 | UV i; | |
9515 | ||
9516 | Renew(ary, newsize, PTR_TBL_ENT_t*); | |
9517 | Zero(&ary[oldsize], newsize-oldsize, PTR_TBL_ENT_t*); | |
9518 | tbl->tbl_max = --newsize; | |
9519 | tbl->tbl_ary = ary; | |
9520 | for (i=0; i < oldsize; i++, ary++) { | |
9521 | PTR_TBL_ENT_t **curentp, **entp, *ent; | |
9522 | if (!*ary) | |
9523 | continue; | |
9524 | curentp = ary + oldsize; | |
9525 | for (entp = ary, ent = *ary; ent; ent = *entp) { | |
d2a79402 | 9526 | if ((newsize & PTR2UV(ent->oldval)) != i) { |
1d7c1841 GS |
9527 | *entp = ent->next; |
9528 | ent->next = *curentp; | |
9529 | *curentp = ent; | |
9530 | continue; | |
9531 | } | |
9532 | else | |
9533 | entp = &ent->next; | |
9534 | } | |
9535 | } | |
9536 | } | |
9537 | ||
645c22ef DM |
9538 | /* remove all the entries from a ptr table */ |
9539 | ||
a0739874 DM |
9540 | void |
9541 | Perl_ptr_table_clear(pTHX_ PTR_TBL_t *tbl) | |
9542 | { | |
9543 | register PTR_TBL_ENT_t **array; | |
9544 | register PTR_TBL_ENT_t *entry; | |
9545 | register PTR_TBL_ENT_t *oentry = Null(PTR_TBL_ENT_t*); | |
9546 | UV riter = 0; | |
9547 | UV max; | |
9548 | ||
9549 | if (!tbl || !tbl->tbl_items) { | |
9550 | return; | |
9551 | } | |
9552 | ||
9553 | array = tbl->tbl_ary; | |
9554 | entry = array[0]; | |
9555 | max = tbl->tbl_max; | |
9556 | ||
9557 | for (;;) { | |
9558 | if (entry) { | |
9559 | oentry = entry; | |
9560 | entry = entry->next; | |
9561 | Safefree(oentry); | |
9562 | } | |
9563 | if (!entry) { | |
9564 | if (++riter > max) { | |
9565 | break; | |
9566 | } | |
9567 | entry = array[riter]; | |
9568 | } | |
9569 | } | |
9570 | ||
9571 | tbl->tbl_items = 0; | |
9572 | } | |
9573 | ||
645c22ef DM |
9574 | /* clear and free a ptr table */ |
9575 | ||
a0739874 DM |
9576 | void |
9577 | Perl_ptr_table_free(pTHX_ PTR_TBL_t *tbl) | |
9578 | { | |
9579 | if (!tbl) { | |
9580 | return; | |
9581 | } | |
9582 | ptr_table_clear(tbl); | |
9583 | Safefree(tbl->tbl_ary); | |
9584 | Safefree(tbl); | |
9585 | } | |
9586 | ||
1d7c1841 GS |
9587 | #ifdef DEBUGGING |
9588 | char *PL_watch_pvx; | |
9589 | #endif | |
9590 | ||
645c22ef DM |
9591 | /* attempt to make everything in the typeglob readonly */ |
9592 | ||
5bd07a3d | 9593 | STATIC SV * |
59b40662 | 9594 | S_gv_share(pTHX_ SV *sstr, CLONE_PARAMS *param) |
5bd07a3d DM |
9595 | { |
9596 | GV *gv = (GV*)sstr; | |
59b40662 | 9597 | SV *sv = ¶m->proto_perl->Isv_no; /* just need SvREADONLY-ness */ |
5bd07a3d DM |
9598 | |
9599 | if (GvIO(gv) || GvFORM(gv)) { | |
7fb37951 | 9600 | GvUNIQUE_off(gv); /* GvIOs cannot be shared. nor can GvFORMs */ |
5bd07a3d DM |
9601 | } |
9602 | else if (!GvCV(gv)) { | |
9603 | GvCV(gv) = (CV*)sv; | |
9604 | } | |
9605 | else { | |
9606 | /* CvPADLISTs cannot be shared */ | |
37e20706 | 9607 | if (!SvREADONLY(GvCV(gv)) && !CvXSUB(GvCV(gv))) { |
7fb37951 | 9608 | GvUNIQUE_off(gv); |
5bd07a3d DM |
9609 | } |
9610 | } | |
9611 | ||
7fb37951 | 9612 | if (!GvUNIQUE(gv)) { |
5bd07a3d DM |
9613 | #if 0 |
9614 | PerlIO_printf(Perl_debug_log, "gv_share: unable to share %s::%s\n", | |
9615 | HvNAME(GvSTASH(gv)), GvNAME(gv)); | |
9616 | #endif | |
9617 | return Nullsv; | |
9618 | } | |
9619 | ||
4411f3b6 | 9620 | /* |
5bd07a3d DM |
9621 | * write attempts will die with |
9622 | * "Modification of a read-only value attempted" | |
9623 | */ | |
9624 | if (!GvSV(gv)) { | |
9625 | GvSV(gv) = sv; | |
9626 | } | |
9627 | else { | |
9628 | SvREADONLY_on(GvSV(gv)); | |
9629 | } | |
9630 | ||
9631 | if (!GvAV(gv)) { | |
9632 | GvAV(gv) = (AV*)sv; | |
9633 | } | |
9634 | else { | |
9635 | SvREADONLY_on(GvAV(gv)); | |
9636 | } | |
9637 | ||
9638 | if (!GvHV(gv)) { | |
9639 | GvHV(gv) = (HV*)sv; | |
9640 | } | |
9641 | else { | |
9642 | SvREADONLY_on(GvAV(gv)); | |
9643 | } | |
9644 | ||
9645 | return sstr; /* he_dup() will SvREFCNT_inc() */ | |
9646 | } | |
9647 | ||
645c22ef DM |
9648 | /* duplicate an SV of any type (including AV, HV etc) */ |
9649 | ||
83841fad NIS |
9650 | void |
9651 | Perl_rvpv_dup(pTHX_ SV *dstr, SV *sstr, CLONE_PARAMS* param) | |
9652 | { | |
9653 | if (SvROK(sstr)) { | |
9654 | SvRV(dstr) = SvWEAKREF(sstr) | |
9655 | ? sv_dup(SvRV(sstr), param) | |
9656 | : sv_dup_inc(SvRV(sstr), param); | |
9657 | } | |
9658 | else if (SvPVX(sstr)) { | |
9659 | /* Has something there */ | |
9660 | if (SvLEN(sstr)) { | |
68795e93 | 9661 | /* Normal PV - clone whole allocated space */ |
83841fad | 9662 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); |
68795e93 | 9663 | } |
83841fad NIS |
9664 | else { |
9665 | /* Special case - not normally malloced for some reason */ | |
9666 | if (SvREADONLY(sstr) && SvFAKE(sstr)) { | |
9667 | /* A "shared" PV - clone it as unshared string */ | |
3e5ba712 | 9668 | if(SvPADTMP(sstr)) { |
efb84706 JH |
9669 | /* However, some of them live in the pad |
9670 | and they should not have these flags | |
9671 | turned off */ | |
3e5ba712 JH |
9672 | |
9673 | SvPVX(dstr) = sharepvn(SvPVX(sstr), SvCUR(sstr), | |
9674 | SvUVX(sstr)); | |
9675 | SvUVX(dstr) = SvUVX(sstr); | |
9676 | } else { | |
9677 | ||
9678 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvCUR(sstr)); | |
9679 | SvFAKE_off(dstr); | |
9680 | SvREADONLY_off(dstr); | |
efb84706 | 9681 | } |
83841fad NIS |
9682 | } |
9683 | else { | |
9684 | /* Some other special case - random pointer */ | |
9685 | SvPVX(dstr) = SvPVX(sstr); | |
9686 | } | |
9687 | } | |
9688 | } | |
9689 | else { | |
9690 | /* Copy the Null */ | |
9691 | SvPVX(dstr) = SvPVX(sstr); | |
9692 | } | |
9693 | } | |
9694 | ||
1d7c1841 | 9695 | SV * |
a8fc9800 | 9696 | Perl_sv_dup(pTHX_ SV *sstr, CLONE_PARAMS* param) |
1d7c1841 | 9697 | { |
1d7c1841 GS |
9698 | SV *dstr; |
9699 | ||
9700 | if (!sstr || SvTYPE(sstr) == SVTYPEMASK) | |
9701 | return Nullsv; | |
9702 | /* look for it in the table first */ | |
9703 | dstr = (SV*)ptr_table_fetch(PL_ptr_table, sstr); | |
9704 | if (dstr) | |
9705 | return dstr; | |
9706 | ||
457d3ae8 JH |
9707 | if(param->flags & CLONEf_JOIN_IN) { |
9708 | /** We are joining here so we don't want do clone | |
9709 | something that is bad **/ | |
9710 | ||
9711 | if(SvTYPE(sstr) == SVt_PVHV && | |
9712 | HvNAME(sstr)) { | |
9713 | /** don't clone stashes if they already exist **/ | |
9714 | HV* old_stash = gv_stashpv(HvNAME(sstr),0); | |
9715 | return (SV*) old_stash; | |
9716 | } | |
9717 | } | |
9718 | ||
1d7c1841 GS |
9719 | /* create anew and remember what it is */ |
9720 | new_SV(dstr); | |
9721 | ptr_table_store(PL_ptr_table, sstr, dstr); | |
9722 | ||
9723 | /* clone */ | |
9724 | SvFLAGS(dstr) = SvFLAGS(sstr); | |
9725 | SvFLAGS(dstr) &= ~SVf_OOK; /* don't propagate OOK hack */ | |
9726 | SvREFCNT(dstr) = 0; /* must be before any other dups! */ | |
9727 | ||
9728 | #ifdef DEBUGGING | |
9729 | if (SvANY(sstr) && PL_watch_pvx && SvPVX(sstr) == PL_watch_pvx) | |
9730 | PerlIO_printf(Perl_debug_log, "watch at %p hit, found string \"%s\"\n", | |
9731 | PL_watch_pvx, SvPVX(sstr)); | |
9732 | #endif | |
9733 | ||
9734 | switch (SvTYPE(sstr)) { | |
9735 | case SVt_NULL: | |
9736 | SvANY(dstr) = NULL; | |
9737 | break; | |
9738 | case SVt_IV: | |
9739 | SvANY(dstr) = new_XIV(); | |
9740 | SvIVX(dstr) = SvIVX(sstr); | |
9741 | break; | |
9742 | case SVt_NV: | |
9743 | SvANY(dstr) = new_XNV(); | |
9744 | SvNVX(dstr) = SvNVX(sstr); | |
9745 | break; | |
9746 | case SVt_RV: | |
9747 | SvANY(dstr) = new_XRV(); | |
83841fad | 9748 | Perl_rvpv_dup(aTHX_ dstr, sstr, param); |
1d7c1841 GS |
9749 | break; |
9750 | case SVt_PV: | |
9751 | SvANY(dstr) = new_XPV(); | |
9752 | SvCUR(dstr) = SvCUR(sstr); | |
9753 | SvLEN(dstr) = SvLEN(sstr); | |
83841fad | 9754 | Perl_rvpv_dup(aTHX_ dstr, sstr, param); |
1d7c1841 GS |
9755 | break; |
9756 | case SVt_PVIV: | |
9757 | SvANY(dstr) = new_XPVIV(); | |
9758 | SvCUR(dstr) = SvCUR(sstr); | |
9759 | SvLEN(dstr) = SvLEN(sstr); | |
9760 | SvIVX(dstr) = SvIVX(sstr); | |
83841fad | 9761 | Perl_rvpv_dup(aTHX_ dstr, sstr, param); |
1d7c1841 GS |
9762 | break; |
9763 | case SVt_PVNV: | |
9764 | SvANY(dstr) = new_XPVNV(); | |
9765 | SvCUR(dstr) = SvCUR(sstr); | |
9766 | SvLEN(dstr) = SvLEN(sstr); | |
9767 | SvIVX(dstr) = SvIVX(sstr); | |
9768 | SvNVX(dstr) = SvNVX(sstr); | |
83841fad | 9769 | Perl_rvpv_dup(aTHX_ dstr, sstr, param); |
1d7c1841 GS |
9770 | break; |
9771 | case SVt_PVMG: | |
9772 | SvANY(dstr) = new_XPVMG(); | |
9773 | SvCUR(dstr) = SvCUR(sstr); | |
9774 | SvLEN(dstr) = SvLEN(sstr); | |
9775 | SvIVX(dstr) = SvIVX(sstr); | |
9776 | SvNVX(dstr) = SvNVX(sstr); | |
d2d73c3e AB |
9777 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr), param); |
9778 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr), param); | |
83841fad | 9779 | Perl_rvpv_dup(aTHX_ dstr, sstr, param); |
1d7c1841 GS |
9780 | break; |
9781 | case SVt_PVBM: | |
9782 | SvANY(dstr) = new_XPVBM(); | |
9783 | SvCUR(dstr) = SvCUR(sstr); | |
9784 | SvLEN(dstr) = SvLEN(sstr); | |
9785 | SvIVX(dstr) = SvIVX(sstr); | |
9786 | SvNVX(dstr) = SvNVX(sstr); | |
d2d73c3e AB |
9787 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr), param); |
9788 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr), param); | |
83841fad | 9789 | Perl_rvpv_dup(aTHX_ dstr, sstr, param); |
1d7c1841 GS |
9790 | BmRARE(dstr) = BmRARE(sstr); |
9791 | BmUSEFUL(dstr) = BmUSEFUL(sstr); | |
9792 | BmPREVIOUS(dstr)= BmPREVIOUS(sstr); | |
9793 | break; | |
9794 | case SVt_PVLV: | |
9795 | SvANY(dstr) = new_XPVLV(); | |
9796 | SvCUR(dstr) = SvCUR(sstr); | |
9797 | SvLEN(dstr) = SvLEN(sstr); | |
9798 | SvIVX(dstr) = SvIVX(sstr); | |
9799 | SvNVX(dstr) = SvNVX(sstr); | |
d2d73c3e AB |
9800 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr), param); |
9801 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr), param); | |
83841fad | 9802 | Perl_rvpv_dup(aTHX_ dstr, sstr, param); |
1d7c1841 GS |
9803 | LvTARGOFF(dstr) = LvTARGOFF(sstr); /* XXX sometimes holds PMOP* when DEBUGGING */ |
9804 | LvTARGLEN(dstr) = LvTARGLEN(sstr); | |
73c86719 JH |
9805 | if (LvTYPE(sstr) == 't') /* for tie: unrefcnted fake (SV**) */ |
9806 | LvTARG(dstr) = dstr; | |
9807 | else if (LvTYPE(sstr) == 'T') /* for tie: fake HE */ | |
9808 | LvTARG(dstr) = (SV*)he_dup((HE*)LvTARG(sstr), 0, param); | |
9809 | else | |
9810 | LvTARG(dstr) = sv_dup_inc(LvTARG(sstr), param); | |
1d7c1841 GS |
9811 | LvTYPE(dstr) = LvTYPE(sstr); |
9812 | break; | |
9813 | case SVt_PVGV: | |
7fb37951 | 9814 | if (GvUNIQUE((GV*)sstr)) { |
5bd07a3d | 9815 | SV *share; |
59b40662 | 9816 | if ((share = gv_share(sstr, param))) { |
5bd07a3d DM |
9817 | del_SV(dstr); |
9818 | dstr = share; | |
37e20706 | 9819 | ptr_table_store(PL_ptr_table, sstr, dstr); |
5bd07a3d DM |
9820 | #if 0 |
9821 | PerlIO_printf(Perl_debug_log, "sv_dup: sharing %s::%s\n", | |
9822 | HvNAME(GvSTASH(share)), GvNAME(share)); | |
9823 | #endif | |
9824 | break; | |
9825 | } | |
9826 | } | |
1d7c1841 GS |
9827 | SvANY(dstr) = new_XPVGV(); |
9828 | SvCUR(dstr) = SvCUR(sstr); | |
9829 | SvLEN(dstr) = SvLEN(sstr); | |
9830 | SvIVX(dstr) = SvIVX(sstr); | |
9831 | SvNVX(dstr) = SvNVX(sstr); | |
d2d73c3e AB |
9832 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr), param); |
9833 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr), param); | |
83841fad | 9834 | Perl_rvpv_dup(aTHX_ dstr, sstr, param); |
1d7c1841 GS |
9835 | GvNAMELEN(dstr) = GvNAMELEN(sstr); |
9836 | GvNAME(dstr) = SAVEPVN(GvNAME(sstr), GvNAMELEN(sstr)); | |
d2d73c3e | 9837 | GvSTASH(dstr) = hv_dup_inc(GvSTASH(sstr), param); |
1d7c1841 | 9838 | GvFLAGS(dstr) = GvFLAGS(sstr); |
d2d73c3e | 9839 | GvGP(dstr) = gp_dup(GvGP(sstr), param); |
1d7c1841 GS |
9840 | (void)GpREFCNT_inc(GvGP(dstr)); |
9841 | break; | |
9842 | case SVt_PVIO: | |
9843 | SvANY(dstr) = new_XPVIO(); | |
9844 | SvCUR(dstr) = SvCUR(sstr); | |
9845 | SvLEN(dstr) = SvLEN(sstr); | |
9846 | SvIVX(dstr) = SvIVX(sstr); | |
9847 | SvNVX(dstr) = SvNVX(sstr); | |
d2d73c3e AB |
9848 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr), param); |
9849 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr), param); | |
83841fad | 9850 | Perl_rvpv_dup(aTHX_ dstr, sstr, param); |
a8fc9800 | 9851 | IoIFP(dstr) = fp_dup(IoIFP(sstr), IoTYPE(sstr), param); |
1d7c1841 GS |
9852 | if (IoOFP(sstr) == IoIFP(sstr)) |
9853 | IoOFP(dstr) = IoIFP(dstr); | |
9854 | else | |
a8fc9800 | 9855 | IoOFP(dstr) = fp_dup(IoOFP(sstr), IoTYPE(sstr), param); |
1d7c1841 GS |
9856 | /* PL_rsfp_filters entries have fake IoDIRP() */ |
9857 | if (IoDIRP(sstr) && !(IoFLAGS(sstr) & IOf_FAKE_DIRP)) | |
9858 | IoDIRP(dstr) = dirp_dup(IoDIRP(sstr)); | |
9859 | else | |
9860 | IoDIRP(dstr) = IoDIRP(sstr); | |
9861 | IoLINES(dstr) = IoLINES(sstr); | |
9862 | IoPAGE(dstr) = IoPAGE(sstr); | |
9863 | IoPAGE_LEN(dstr) = IoPAGE_LEN(sstr); | |
9864 | IoLINES_LEFT(dstr) = IoLINES_LEFT(sstr); | |
4376702e JH |
9865 | if(IoFLAGS(sstr) & IOf_FAKE_DIRP) { |
9866 | /* I have no idea why fake dirp (rsfps) | |
9867 | should be treaded differently but otherwise | |
9868 | we end up with leaks -- sky*/ | |
9869 | IoTOP_GV(dstr) = gv_dup_inc(IoTOP_GV(sstr), param); | |
9870 | IoFMT_GV(dstr) = gv_dup_inc(IoFMT_GV(sstr), param); | |
9871 | IoBOTTOM_GV(dstr) = gv_dup_inc(IoBOTTOM_GV(sstr), param); | |
9872 | } else { | |
9873 | IoTOP_GV(dstr) = gv_dup(IoTOP_GV(sstr), param); | |
9874 | IoFMT_GV(dstr) = gv_dup(IoFMT_GV(sstr), param); | |
9875 | IoBOTTOM_GV(dstr) = gv_dup(IoBOTTOM_GV(sstr), param); | |
9876 | } | |
1d7c1841 | 9877 | IoTOP_NAME(dstr) = SAVEPV(IoTOP_NAME(sstr)); |
1d7c1841 | 9878 | IoFMT_NAME(dstr) = SAVEPV(IoFMT_NAME(sstr)); |
1d7c1841 | 9879 | IoBOTTOM_NAME(dstr) = SAVEPV(IoBOTTOM_NAME(sstr)); |
1d7c1841 GS |
9880 | IoSUBPROCESS(dstr) = IoSUBPROCESS(sstr); |
9881 | IoTYPE(dstr) = IoTYPE(sstr); | |
9882 | IoFLAGS(dstr) = IoFLAGS(sstr); | |
9883 | break; | |
9884 | case SVt_PVAV: | |
9885 | SvANY(dstr) = new_XPVAV(); | |
9886 | SvCUR(dstr) = SvCUR(sstr); | |
9887 | SvLEN(dstr) = SvLEN(sstr); | |
9888 | SvIVX(dstr) = SvIVX(sstr); | |
9889 | SvNVX(dstr) = SvNVX(sstr); | |
d2d73c3e AB |
9890 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr), param); |
9891 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr), param); | |
9892 | AvARYLEN((AV*)dstr) = sv_dup_inc(AvARYLEN((AV*)sstr), param); | |
1d7c1841 GS |
9893 | AvFLAGS((AV*)dstr) = AvFLAGS((AV*)sstr); |
9894 | if (AvARRAY((AV*)sstr)) { | |
9895 | SV **dst_ary, **src_ary; | |
9896 | SSize_t items = AvFILLp((AV*)sstr) + 1; | |
9897 | ||
9898 | src_ary = AvARRAY((AV*)sstr); | |
9899 | Newz(0, dst_ary, AvMAX((AV*)sstr)+1, SV*); | |
9900 | ptr_table_store(PL_ptr_table, src_ary, dst_ary); | |
9901 | SvPVX(dstr) = (char*)dst_ary; | |
9902 | AvALLOC((AV*)dstr) = dst_ary; | |
9903 | if (AvREAL((AV*)sstr)) { | |
9904 | while (items-- > 0) | |
d2d73c3e | 9905 | *dst_ary++ = sv_dup_inc(*src_ary++, param); |
1d7c1841 GS |
9906 | } |
9907 | else { | |
9908 | while (items-- > 0) | |
d2d73c3e | 9909 | *dst_ary++ = sv_dup(*src_ary++, param); |
1d7c1841 GS |
9910 | } |
9911 | items = AvMAX((AV*)sstr) - AvFILLp((AV*)sstr); | |
9912 | while (items-- > 0) { | |
9913 | *dst_ary++ = &PL_sv_undef; | |
9914 | } | |
9915 | } | |
9916 | else { | |
9917 | SvPVX(dstr) = Nullch; | |
9918 | AvALLOC((AV*)dstr) = (SV**)NULL; | |
9919 | } | |
9920 | break; | |
9921 | case SVt_PVHV: | |
9922 | SvANY(dstr) = new_XPVHV(); | |
9923 | SvCUR(dstr) = SvCUR(sstr); | |
9924 | SvLEN(dstr) = SvLEN(sstr); | |
9925 | SvIVX(dstr) = SvIVX(sstr); | |
9926 | SvNVX(dstr) = SvNVX(sstr); | |
d2d73c3e AB |
9927 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr), param); |
9928 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr), param); | |
1d7c1841 GS |
9929 | HvRITER((HV*)dstr) = HvRITER((HV*)sstr); |
9930 | if (HvARRAY((HV*)sstr)) { | |
1d7c1841 GS |
9931 | STRLEN i = 0; |
9932 | XPVHV *dxhv = (XPVHV*)SvANY(dstr); | |
9933 | XPVHV *sxhv = (XPVHV*)SvANY(sstr); | |
9934 | Newz(0, dxhv->xhv_array, | |
9935 | PERL_HV_ARRAY_ALLOC_BYTES(dxhv->xhv_max+1), char); | |
9936 | while (i <= sxhv->xhv_max) { | |
9937 | ((HE**)dxhv->xhv_array)[i] = he_dup(((HE**)sxhv->xhv_array)[i], | |
eb160463 GS |
9938 | (bool)!!HvSHAREKEYS(sstr), |
9939 | param); | |
1d7c1841 GS |
9940 | ++i; |
9941 | } | |
eb160463 GS |
9942 | dxhv->xhv_eiter = he_dup(sxhv->xhv_eiter, |
9943 | (bool)!!HvSHAREKEYS(sstr), param); | |
1d7c1841 GS |
9944 | } |
9945 | else { | |
9946 | SvPVX(dstr) = Nullch; | |
9947 | HvEITER((HV*)dstr) = (HE*)NULL; | |
9948 | } | |
9949 | HvPMROOT((HV*)dstr) = HvPMROOT((HV*)sstr); /* XXX */ | |
9950 | HvNAME((HV*)dstr) = SAVEPV(HvNAME((HV*)sstr)); | |
c43294b8 | 9951 | /* Record stashes for possible cloning in Perl_clone(). */ |
6676db26 | 9952 | if(HvNAME((HV*)dstr)) |
d2d73c3e | 9953 | av_push(param->stashes, dstr); |
1d7c1841 GS |
9954 | break; |
9955 | case SVt_PVFM: | |
9956 | SvANY(dstr) = new_XPVFM(); | |
9957 | FmLINES(dstr) = FmLINES(sstr); | |
9958 | goto dup_pvcv; | |
9959 | /* NOTREACHED */ | |
9960 | case SVt_PVCV: | |
9961 | SvANY(dstr) = new_XPVCV(); | |
d2d73c3e | 9962 | dup_pvcv: |
1d7c1841 GS |
9963 | SvCUR(dstr) = SvCUR(sstr); |
9964 | SvLEN(dstr) = SvLEN(sstr); | |
9965 | SvIVX(dstr) = SvIVX(sstr); | |
9966 | SvNVX(dstr) = SvNVX(sstr); | |
d2d73c3e AB |
9967 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr), param); |
9968 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr), param); | |
83841fad | 9969 | Perl_rvpv_dup(aTHX_ dstr, sstr, param); |
d2d73c3e | 9970 | CvSTASH(dstr) = hv_dup(CvSTASH(sstr), param); /* NOTE: not refcounted */ |
1d7c1841 GS |
9971 | CvSTART(dstr) = CvSTART(sstr); |
9972 | CvROOT(dstr) = OpREFCNT_inc(CvROOT(sstr)); | |
9973 | CvXSUB(dstr) = CvXSUB(sstr); | |
9974 | CvXSUBANY(dstr) = CvXSUBANY(sstr); | |
01485f8b DM |
9975 | if (CvCONST(sstr)) { |
9976 | CvXSUBANY(dstr).any_ptr = GvUNIQUE(CvGV(sstr)) ? | |
9977 | SvREFCNT_inc(CvXSUBANY(sstr).any_ptr) : | |
9978 | sv_dup_inc(CvXSUBANY(sstr).any_ptr, param); | |
9979 | } | |
d2d73c3e AB |
9980 | CvGV(dstr) = gv_dup(CvGV(sstr), param); |
9981 | if (param->flags & CLONEf_COPY_STACKS) { | |
9982 | CvDEPTH(dstr) = CvDEPTH(sstr); | |
9983 | } else { | |
9984 | CvDEPTH(dstr) = 0; | |
9985 | } | |
9755d405 | 9986 | PAD_DUP(CvPADLIST(dstr), CvPADLIST(sstr), param); |
d7afa7f5 JH |
9987 | CvOUTSIDE_SEQ(dstr) = CvOUTSIDE_SEQ(sstr); |
9988 | CvOUTSIDE(dstr) = | |
9989 | CvWEAKOUTSIDE(sstr) | |
9990 | ? cv_dup( CvOUTSIDE(sstr), param) | |
9991 | : cv_dup_inc(CvOUTSIDE(sstr), param); | |
1d7c1841 | 9992 | CvFLAGS(dstr) = CvFLAGS(sstr); |
54356c7d | 9993 | CvFILE(dstr) = CvXSUB(sstr) ? CvFILE(sstr) : SAVEPV(CvFILE(sstr)); |
1d7c1841 GS |
9994 | break; |
9995 | default: | |
c803eecc | 9996 | Perl_croak(aTHX_ "Bizarre SvTYPE [%" IVdf "]", (IV)SvTYPE(sstr)); |
1d7c1841 GS |
9997 | break; |
9998 | } | |
9999 | ||
10000 | if (SvOBJECT(dstr) && SvTYPE(dstr) != SVt_PVIO) | |
10001 | ++PL_sv_objcount; | |
10002 | ||
10003 | return dstr; | |
d2d73c3e | 10004 | } |
1d7c1841 | 10005 | |
645c22ef DM |
10006 | /* duplicate a context */ |
10007 | ||
1d7c1841 | 10008 | PERL_CONTEXT * |
a8fc9800 | 10009 | Perl_cx_dup(pTHX_ PERL_CONTEXT *cxs, I32 ix, I32 max, CLONE_PARAMS* param) |
1d7c1841 GS |
10010 | { |
10011 | PERL_CONTEXT *ncxs; | |
10012 | ||
10013 | if (!cxs) | |
10014 | return (PERL_CONTEXT*)NULL; | |
10015 | ||
10016 | /* look for it in the table first */ | |
10017 | ncxs = (PERL_CONTEXT*)ptr_table_fetch(PL_ptr_table, cxs); | |
10018 | if (ncxs) | |
10019 | return ncxs; | |
10020 | ||
10021 | /* create anew and remember what it is */ | |
10022 | Newz(56, ncxs, max + 1, PERL_CONTEXT); | |
10023 | ptr_table_store(PL_ptr_table, cxs, ncxs); | |
10024 | ||
10025 | while (ix >= 0) { | |
10026 | PERL_CONTEXT *cx = &cxs[ix]; | |
10027 | PERL_CONTEXT *ncx = &ncxs[ix]; | |
10028 | ncx->cx_type = cx->cx_type; | |
10029 | if (CxTYPE(cx) == CXt_SUBST) { | |
10030 | Perl_croak(aTHX_ "Cloning substitution context is unimplemented"); | |
10031 | } | |
10032 | else { | |
10033 | ncx->blk_oldsp = cx->blk_oldsp; | |
10034 | ncx->blk_oldcop = cx->blk_oldcop; | |
10035 | ncx->blk_oldretsp = cx->blk_oldretsp; | |
10036 | ncx->blk_oldmarksp = cx->blk_oldmarksp; | |
10037 | ncx->blk_oldscopesp = cx->blk_oldscopesp; | |
10038 | ncx->blk_oldpm = cx->blk_oldpm; | |
10039 | ncx->blk_gimme = cx->blk_gimme; | |
10040 | switch (CxTYPE(cx)) { | |
10041 | case CXt_SUB: | |
10042 | ncx->blk_sub.cv = (cx->blk_sub.olddepth == 0 | |
d2d73c3e AB |
10043 | ? cv_dup_inc(cx->blk_sub.cv, param) |
10044 | : cv_dup(cx->blk_sub.cv,param)); | |
1d7c1841 | 10045 | ncx->blk_sub.argarray = (cx->blk_sub.hasargs |
d2d73c3e | 10046 | ? av_dup_inc(cx->blk_sub.argarray, param) |
1d7c1841 | 10047 | : Nullav); |
d2d73c3e | 10048 | ncx->blk_sub.savearray = av_dup_inc(cx->blk_sub.savearray, param); |
1d7c1841 GS |
10049 | ncx->blk_sub.olddepth = cx->blk_sub.olddepth; |
10050 | ncx->blk_sub.hasargs = cx->blk_sub.hasargs; | |
10051 | ncx->blk_sub.lval = cx->blk_sub.lval; | |
10052 | break; | |
10053 | case CXt_EVAL: | |
10054 | ncx->blk_eval.old_in_eval = cx->blk_eval.old_in_eval; | |
10055 | ncx->blk_eval.old_op_type = cx->blk_eval.old_op_type; | |
b47cad08 | 10056 | ncx->blk_eval.old_namesv = sv_dup_inc(cx->blk_eval.old_namesv, param); |
1d7c1841 | 10057 | ncx->blk_eval.old_eval_root = cx->blk_eval.old_eval_root; |
d2d73c3e | 10058 | ncx->blk_eval.cur_text = sv_dup(cx->blk_eval.cur_text, param); |
1d7c1841 GS |
10059 | break; |
10060 | case CXt_LOOP: | |
10061 | ncx->blk_loop.label = cx->blk_loop.label; | |
10062 | ncx->blk_loop.resetsp = cx->blk_loop.resetsp; | |
10063 | ncx->blk_loop.redo_op = cx->blk_loop.redo_op; | |
10064 | ncx->blk_loop.next_op = cx->blk_loop.next_op; | |
10065 | ncx->blk_loop.last_op = cx->blk_loop.last_op; | |
10066 | ncx->blk_loop.iterdata = (CxPADLOOP(cx) | |
10067 | ? cx->blk_loop.iterdata | |
d2d73c3e | 10068 | : gv_dup((GV*)cx->blk_loop.iterdata, param)); |
d7afa7f5 JH |
10069 | ncx->blk_loop.oldcomppad |
10070 | = (PAD*)ptr_table_fetch(PL_ptr_table, | |
10071 | cx->blk_loop.oldcomppad); | |
d2d73c3e AB |
10072 | ncx->blk_loop.itersave = sv_dup_inc(cx->blk_loop.itersave, param); |
10073 | ncx->blk_loop.iterlval = sv_dup_inc(cx->blk_loop.iterlval, param); | |
10074 | ncx->blk_loop.iterary = av_dup_inc(cx->blk_loop.iterary, param); | |
1d7c1841 GS |
10075 | ncx->blk_loop.iterix = cx->blk_loop.iterix; |
10076 | ncx->blk_loop.itermax = cx->blk_loop.itermax; | |
10077 | break; | |
10078 | case CXt_FORMAT: | |
d2d73c3e AB |
10079 | ncx->blk_sub.cv = cv_dup(cx->blk_sub.cv, param); |
10080 | ncx->blk_sub.gv = gv_dup(cx->blk_sub.gv, param); | |
10081 | ncx->blk_sub.dfoutgv = gv_dup_inc(cx->blk_sub.dfoutgv, param); | |
1d7c1841 GS |
10082 | ncx->blk_sub.hasargs = cx->blk_sub.hasargs; |
10083 | break; | |
10084 | case CXt_BLOCK: | |
10085 | case CXt_NULL: | |
10086 | break; | |
10087 | } | |
10088 | } | |
10089 | --ix; | |
10090 | } | |
10091 | return ncxs; | |
10092 | } | |
10093 | ||
645c22ef DM |
10094 | /* duplicate a stack info structure */ |
10095 | ||
1d7c1841 | 10096 | PERL_SI * |
a8fc9800 | 10097 | Perl_si_dup(pTHX_ PERL_SI *si, CLONE_PARAMS* param) |
1d7c1841 GS |
10098 | { |
10099 | PERL_SI *nsi; | |
10100 | ||
10101 | if (!si) | |
10102 | return (PERL_SI*)NULL; | |
10103 | ||
10104 | /* look for it in the table first */ | |
10105 | nsi = (PERL_SI*)ptr_table_fetch(PL_ptr_table, si); | |
10106 | if (nsi) | |
10107 | return nsi; | |
10108 | ||
10109 | /* create anew and remember what it is */ | |
10110 | Newz(56, nsi, 1, PERL_SI); | |
10111 | ptr_table_store(PL_ptr_table, si, nsi); | |
10112 | ||
d2d73c3e | 10113 | nsi->si_stack = av_dup_inc(si->si_stack, param); |
1d7c1841 GS |
10114 | nsi->si_cxix = si->si_cxix; |
10115 | nsi->si_cxmax = si->si_cxmax; | |
d2d73c3e | 10116 | nsi->si_cxstack = cx_dup(si->si_cxstack, si->si_cxix, si->si_cxmax, param); |
1d7c1841 | 10117 | nsi->si_type = si->si_type; |
d2d73c3e AB |
10118 | nsi->si_prev = si_dup(si->si_prev, param); |
10119 | nsi->si_next = si_dup(si->si_next, param); | |
1d7c1841 GS |
10120 | nsi->si_markoff = si->si_markoff; |
10121 | ||
10122 | return nsi; | |
10123 | } | |
10124 | ||
10125 | #define POPINT(ss,ix) ((ss)[--(ix)].any_i32) | |
10126 | #define TOPINT(ss,ix) ((ss)[ix].any_i32) | |
10127 | #define POPLONG(ss,ix) ((ss)[--(ix)].any_long) | |
10128 | #define TOPLONG(ss,ix) ((ss)[ix].any_long) | |
10129 | #define POPIV(ss,ix) ((ss)[--(ix)].any_iv) | |
10130 | #define TOPIV(ss,ix) ((ss)[ix].any_iv) | |
21637969 YST |
10131 | #define POPBOOL(ss,ix) ((ss)[--(ix)].any_bool) |
10132 | #define TOPBOOL(ss,ix) ((ss)[ix].any_bool) | |
1d7c1841 GS |
10133 | #define POPPTR(ss,ix) ((ss)[--(ix)].any_ptr) |
10134 | #define TOPPTR(ss,ix) ((ss)[ix].any_ptr) | |
10135 | #define POPDPTR(ss,ix) ((ss)[--(ix)].any_dptr) | |
10136 | #define TOPDPTR(ss,ix) ((ss)[ix].any_dptr) | |
10137 | #define POPDXPTR(ss,ix) ((ss)[--(ix)].any_dxptr) | |
10138 | #define TOPDXPTR(ss,ix) ((ss)[ix].any_dxptr) | |
10139 | ||
10140 | /* XXXXX todo */ | |
10141 | #define pv_dup_inc(p) SAVEPV(p) | |
10142 | #define pv_dup(p) SAVEPV(p) | |
10143 | #define svp_dup_inc(p,pp) any_dup(p,pp) | |
10144 | ||
645c22ef DM |
10145 | /* map any object to the new equivent - either something in the |
10146 | * ptr table, or something in the interpreter structure | |
10147 | */ | |
10148 | ||
1d7c1841 GS |
10149 | void * |
10150 | Perl_any_dup(pTHX_ void *v, PerlInterpreter *proto_perl) | |
10151 | { | |
10152 | void *ret; | |
10153 | ||
10154 | if (!v) | |
10155 | return (void*)NULL; | |
10156 | ||
10157 | /* look for it in the table first */ | |
10158 | ret = ptr_table_fetch(PL_ptr_table, v); | |
10159 | if (ret) | |
10160 | return ret; | |
10161 | ||
10162 | /* see if it is part of the interpreter structure */ | |
10163 | if (v >= (void*)proto_perl && v < (void*)(proto_perl+1)) | |
acfe0abc | 10164 | ret = (void*)(((char*)aTHX) + (((char*)v) - (char*)proto_perl)); |
05ec9bb3 | 10165 | else { |
1d7c1841 | 10166 | ret = v; |
05ec9bb3 | 10167 | } |
1d7c1841 GS |
10168 | |
10169 | return ret; | |
10170 | } | |
10171 | ||
645c22ef DM |
10172 | /* duplicate the save stack */ |
10173 | ||
1d7c1841 | 10174 | ANY * |
a8fc9800 | 10175 | Perl_ss_dup(pTHX_ PerlInterpreter *proto_perl, CLONE_PARAMS* param) |
1d7c1841 GS |
10176 | { |
10177 | ANY *ss = proto_perl->Tsavestack; | |
10178 | I32 ix = proto_perl->Tsavestack_ix; | |
10179 | I32 max = proto_perl->Tsavestack_max; | |
10180 | ANY *nss; | |
10181 | SV *sv; | |
10182 | GV *gv; | |
10183 | AV *av; | |
10184 | HV *hv; | |
10185 | void* ptr; | |
10186 | int intval; | |
10187 | long longval; | |
10188 | GP *gp; | |
10189 | IV iv; | |
10190 | I32 i; | |
c4e33207 | 10191 | char *c = NULL; |
1d7c1841 | 10192 | void (*dptr) (void*); |
acfe0abc | 10193 | void (*dxptr) (pTHX_ void*); |
e977893f | 10194 | OP *o; |
1d7c1841 GS |
10195 | |
10196 | Newz(54, nss, max, ANY); | |
10197 | ||
10198 | while (ix > 0) { | |
10199 | i = POPINT(ss,ix); | |
10200 | TOPINT(nss,ix) = i; | |
10201 | switch (i) { | |
10202 | case SAVEt_ITEM: /* normal string */ | |
10203 | sv = (SV*)POPPTR(ss,ix); | |
d2d73c3e | 10204 | TOPPTR(nss,ix) = sv_dup_inc(sv, param); |
1d7c1841 | 10205 | sv = (SV*)POPPTR(ss,ix); |
d2d73c3e | 10206 | TOPPTR(nss,ix) = sv_dup_inc(sv, param); |
1d7c1841 GS |
10207 | break; |
10208 | case SAVEt_SV: /* scalar reference */ | |
10209 | sv = (SV*)POPPTR(ss,ix); | |
d2d73c3e | 10210 | TOPPTR(nss,ix) = sv_dup_inc(sv, param); |
1d7c1841 | 10211 | gv = (GV*)POPPTR(ss,ix); |
d2d73c3e | 10212 | TOPPTR(nss,ix) = gv_dup_inc(gv, param); |
1d7c1841 | 10213 | break; |
f4dd75d9 GS |
10214 | case SAVEt_GENERIC_PVREF: /* generic char* */ |
10215 | c = (char*)POPPTR(ss,ix); | |
10216 | TOPPTR(nss,ix) = pv_dup(c); | |
10217 | ptr = POPPTR(ss,ix); | |
10218 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
10219 | break; | |
05ec9bb3 NIS |
10220 | case SAVEt_SHARED_PVREF: /* char* in shared space */ |
10221 | c = (char*)POPPTR(ss,ix); | |
10222 | TOPPTR(nss,ix) = savesharedpv(c); | |
10223 | ptr = POPPTR(ss,ix); | |
10224 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
10225 | break; | |
1d7c1841 GS |
10226 | case SAVEt_GENERIC_SVREF: /* generic sv */ |
10227 | case SAVEt_SVREF: /* scalar reference */ | |
10228 | sv = (SV*)POPPTR(ss,ix); | |
d2d73c3e | 10229 | TOPPTR(nss,ix) = sv_dup_inc(sv, param); |
1d7c1841 GS |
10230 | ptr = POPPTR(ss,ix); |
10231 | TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */ | |
10232 | break; | |
10233 | case SAVEt_AV: /* array reference */ | |
10234 | av = (AV*)POPPTR(ss,ix); | |
d2d73c3e | 10235 | TOPPTR(nss,ix) = av_dup_inc(av, param); |
1d7c1841 | 10236 | gv = (GV*)POPPTR(ss,ix); |
d2d73c3e | 10237 | TOPPTR(nss,ix) = gv_dup(gv, param); |
1d7c1841 GS |
10238 | break; |
10239 | case SAVEt_HV: /* hash reference */ | |
10240 | hv = (HV*)POPPTR(ss,ix); | |
d2d73c3e | 10241 | TOPPTR(nss,ix) = hv_dup_inc(hv, param); |
1d7c1841 | 10242 | gv = (GV*)POPPTR(ss,ix); |
d2d73c3e | 10243 | TOPPTR(nss,ix) = gv_dup(gv, param); |
1d7c1841 GS |
10244 | break; |
10245 | case SAVEt_INT: /* int reference */ | |
10246 | ptr = POPPTR(ss,ix); | |
10247 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
10248 | intval = (int)POPINT(ss,ix); | |
10249 | TOPINT(nss,ix) = intval; | |
10250 | break; | |
10251 | case SAVEt_LONG: /* long reference */ | |
10252 | ptr = POPPTR(ss,ix); | |
10253 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
10254 | longval = (long)POPLONG(ss,ix); | |
10255 | TOPLONG(nss,ix) = longval; | |
10256 | break; | |
10257 | case SAVEt_I32: /* I32 reference */ | |
10258 | case SAVEt_I16: /* I16 reference */ | |
10259 | case SAVEt_I8: /* I8 reference */ | |
10260 | ptr = POPPTR(ss,ix); | |
10261 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
10262 | i = POPINT(ss,ix); | |
10263 | TOPINT(nss,ix) = i; | |
10264 | break; | |
10265 | case SAVEt_IV: /* IV reference */ | |
10266 | ptr = POPPTR(ss,ix); | |
10267 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
10268 | iv = POPIV(ss,ix); | |
10269 | TOPIV(nss,ix) = iv; | |
10270 | break; | |
10271 | case SAVEt_SPTR: /* SV* reference */ | |
10272 | ptr = POPPTR(ss,ix); | |
10273 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
10274 | sv = (SV*)POPPTR(ss,ix); | |
d2d73c3e | 10275 | TOPPTR(nss,ix) = sv_dup(sv, param); |
1d7c1841 GS |
10276 | break; |
10277 | case SAVEt_VPTR: /* random* reference */ | |
10278 | ptr = POPPTR(ss,ix); | |
10279 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
10280 | ptr = POPPTR(ss,ix); | |
10281 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
10282 | break; | |
10283 | case SAVEt_PPTR: /* char* reference */ | |
10284 | ptr = POPPTR(ss,ix); | |
10285 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
10286 | c = (char*)POPPTR(ss,ix); | |
10287 | TOPPTR(nss,ix) = pv_dup(c); | |
10288 | break; | |
10289 | case SAVEt_HPTR: /* HV* reference */ | |
10290 | ptr = POPPTR(ss,ix); | |
10291 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
10292 | hv = (HV*)POPPTR(ss,ix); | |
d2d73c3e | 10293 | TOPPTR(nss,ix) = hv_dup(hv, param); |
1d7c1841 GS |
10294 | break; |
10295 | case SAVEt_APTR: /* AV* reference */ | |
10296 | ptr = POPPTR(ss,ix); | |
10297 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
10298 | av = (AV*)POPPTR(ss,ix); | |
d2d73c3e | 10299 | TOPPTR(nss,ix) = av_dup(av, param); |
1d7c1841 GS |
10300 | break; |
10301 | case SAVEt_NSTAB: | |
10302 | gv = (GV*)POPPTR(ss,ix); | |
d2d73c3e | 10303 | TOPPTR(nss,ix) = gv_dup(gv, param); |
1d7c1841 GS |
10304 | break; |
10305 | case SAVEt_GP: /* scalar reference */ | |
10306 | gp = (GP*)POPPTR(ss,ix); | |
d2d73c3e | 10307 | TOPPTR(nss,ix) = gp = gp_dup(gp, param); |
1d7c1841 GS |
10308 | (void)GpREFCNT_inc(gp); |
10309 | gv = (GV*)POPPTR(ss,ix); | |
2ed3c8fc | 10310 | TOPPTR(nss,ix) = gv_dup_inc(gv, param); |
1d7c1841 GS |
10311 | c = (char*)POPPTR(ss,ix); |
10312 | TOPPTR(nss,ix) = pv_dup(c); | |
10313 | iv = POPIV(ss,ix); | |
10314 | TOPIV(nss,ix) = iv; | |
10315 | iv = POPIV(ss,ix); | |
10316 | TOPIV(nss,ix) = iv; | |
10317 | break; | |
10318 | case SAVEt_FREESV: | |
26d9b02f | 10319 | case SAVEt_MORTALIZESV: |
1d7c1841 | 10320 | sv = (SV*)POPPTR(ss,ix); |
d2d73c3e | 10321 | TOPPTR(nss,ix) = sv_dup_inc(sv, param); |
1d7c1841 GS |
10322 | break; |
10323 | case SAVEt_FREEOP: | |
10324 | ptr = POPPTR(ss,ix); | |
10325 | if (ptr && (((OP*)ptr)->op_private & OPpREFCOUNTED)) { | |
10326 | /* these are assumed to be refcounted properly */ | |
10327 | switch (((OP*)ptr)->op_type) { | |
10328 | case OP_LEAVESUB: | |
10329 | case OP_LEAVESUBLV: | |
10330 | case OP_LEAVEEVAL: | |
10331 | case OP_LEAVE: | |
10332 | case OP_SCOPE: | |
10333 | case OP_LEAVEWRITE: | |
e977893f GS |
10334 | TOPPTR(nss,ix) = ptr; |
10335 | o = (OP*)ptr; | |
10336 | OpREFCNT_inc(o); | |
1d7c1841 GS |
10337 | break; |
10338 | default: | |
10339 | TOPPTR(nss,ix) = Nullop; | |
10340 | break; | |
10341 | } | |
10342 | } | |
10343 | else | |
10344 | TOPPTR(nss,ix) = Nullop; | |
10345 | break; | |
10346 | case SAVEt_FREEPV: | |
10347 | c = (char*)POPPTR(ss,ix); | |
10348 | TOPPTR(nss,ix) = pv_dup_inc(c); | |
10349 | break; | |
10350 | case SAVEt_CLEARSV: | |
10351 | longval = POPLONG(ss,ix); | |
10352 | TOPLONG(nss,ix) = longval; | |
10353 | break; | |
10354 | case SAVEt_DELETE: | |
10355 | hv = (HV*)POPPTR(ss,ix); | |
d2d73c3e | 10356 | TOPPTR(nss,ix) = hv_dup_inc(hv, param); |
1d7c1841 GS |
10357 | c = (char*)POPPTR(ss,ix); |
10358 | TOPPTR(nss,ix) = pv_dup_inc(c); | |
10359 | i = POPINT(ss,ix); | |
10360 | TOPINT(nss,ix) = i; | |
10361 | break; | |
10362 | case SAVEt_DESTRUCTOR: | |
10363 | ptr = POPPTR(ss,ix); | |
10364 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */ | |
10365 | dptr = POPDPTR(ss,ix); | |
ef75a179 | 10366 | TOPDPTR(nss,ix) = (void (*)(void*))any_dup((void *)dptr, proto_perl); |
1d7c1841 GS |
10367 | break; |
10368 | case SAVEt_DESTRUCTOR_X: | |
10369 | ptr = POPPTR(ss,ix); | |
10370 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */ | |
10371 | dxptr = POPDXPTR(ss,ix); | |
acfe0abc | 10372 | TOPDXPTR(nss,ix) = (void (*)(pTHX_ void*))any_dup((void *)dxptr, proto_perl); |
1d7c1841 GS |
10373 | break; |
10374 | case SAVEt_REGCONTEXT: | |
10375 | case SAVEt_ALLOC: | |
10376 | i = POPINT(ss,ix); | |
10377 | TOPINT(nss,ix) = i; | |
10378 | ix -= i; | |
10379 | break; | |
10380 | case SAVEt_STACK_POS: /* Position on Perl stack */ | |
10381 | i = POPINT(ss,ix); | |
10382 | TOPINT(nss,ix) = i; | |
10383 | break; | |
10384 | case SAVEt_AELEM: /* array element */ | |
10385 | sv = (SV*)POPPTR(ss,ix); | |
d2d73c3e | 10386 | TOPPTR(nss,ix) = sv_dup_inc(sv, param); |
1d7c1841 GS |
10387 | i = POPINT(ss,ix); |
10388 | TOPINT(nss,ix) = i; | |
10389 | av = (AV*)POPPTR(ss,ix); | |
d2d73c3e | 10390 | TOPPTR(nss,ix) = av_dup_inc(av, param); |
1d7c1841 GS |
10391 | break; |
10392 | case SAVEt_HELEM: /* hash element */ | |
10393 | sv = (SV*)POPPTR(ss,ix); | |
d2d73c3e | 10394 | TOPPTR(nss,ix) = sv_dup_inc(sv, param); |
1d7c1841 | 10395 | sv = (SV*)POPPTR(ss,ix); |
d2d73c3e | 10396 | TOPPTR(nss,ix) = sv_dup_inc(sv, param); |
1d7c1841 | 10397 | hv = (HV*)POPPTR(ss,ix); |
d2d73c3e | 10398 | TOPPTR(nss,ix) = hv_dup_inc(hv, param); |
1d7c1841 GS |
10399 | break; |
10400 | case SAVEt_OP: | |
10401 | ptr = POPPTR(ss,ix); | |
10402 | TOPPTR(nss,ix) = ptr; | |
10403 | break; | |
10404 | case SAVEt_HINTS: | |
10405 | i = POPINT(ss,ix); | |
10406 | TOPINT(nss,ix) = i; | |
10407 | break; | |
c4410b1b GS |
10408 | case SAVEt_COMPPAD: |
10409 | av = (AV*)POPPTR(ss,ix); | |
d2d73c3e | 10410 | TOPPTR(nss,ix) = av_dup(av, param); |
c4410b1b | 10411 | break; |
c3564e5c GS |
10412 | case SAVEt_PADSV: |
10413 | longval = (long)POPLONG(ss,ix); | |
10414 | TOPLONG(nss,ix) = longval; | |
10415 | ptr = POPPTR(ss,ix); | |
10416 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
10417 | sv = (SV*)POPPTR(ss,ix); | |
d2d73c3e | 10418 | TOPPTR(nss,ix) = sv_dup(sv, param); |
c3564e5c | 10419 | break; |
4f4e7967 | 10420 | case SAVEt_BOOL: |
21637969 | 10421 | ptr = POPPTR(ss,ix); |
4f4e7967 | 10422 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); |
21637969 | 10423 | longval = (long)POPBOOL(ss,ix); |
4f4e7967 JH |
10424 | TOPBOOL(nss,ix) = (bool)longval; |
10425 | break; | |
1d7c1841 GS |
10426 | default: |
10427 | Perl_croak(aTHX_ "panic: ss_dup inconsistency"); | |
10428 | } | |
10429 | } | |
10430 | ||
10431 | return nss; | |
10432 | } | |
10433 | ||
645c22ef DM |
10434 | /* |
10435 | =for apidoc perl_clone | |
10436 | ||
10437 | Create and return a new interpreter by cloning the current one. | |
10438 | ||
ac388100 | 10439 | perl_clone takes these flags as parameters: |
4f4e7967 JH |
10440 | |
10441 | CLONEf_COPY_STACKS - is used to, well, copy the stacks also, | |
10442 | without it we only clone the data and zero the stacks, | |
10443 | with it we copy the stacks and the new perl interpreter is | |
10444 | ready to run at the exact same point as the previous one. | |
10445 | The pseudo-fork code uses COPY_STACKS while the | |
10446 | threads->new doesn't. | |
10447 | ||
10448 | CLONEf_KEEP_PTR_TABLE | |
10449 | perl_clone keeps a ptr_table with the pointer of the old | |
10450 | variable as a key and the new variable as a value, | |
10451 | this allows it to check if something has been cloned and not | |
10452 | clone it again but rather just use the value and increase the | |
10453 | refcount. If KEEP_PTR_TABLE is not set then perl_clone will kill | |
10454 | the ptr_table using the function | |
10455 | C<ptr_table_free(PL_ptr_table); PL_ptr_table = NULL;>, | |
10456 | reason to keep it around is if you want to dup some of your own | |
10457 | variable who are outside the graph perl scans, example of this | |
10458 | code is in threads.xs create | |
10459 | ||
10460 | CLONEf_CLONE_HOST | |
10461 | This is a win32 thing, it is ignored on unix, it tells perls | |
10462 | win32host code (which is c++) to clone itself, this is needed on | |
10463 | win32 if you want to run two threads at the same time, | |
10464 | if you just want to do some stuff in a separate perl interpreter | |
10465 | and then throw it away and return to the original one, | |
10466 | you don't need to do anything. | |
10467 | ||
645c22ef DM |
10468 | =cut |
10469 | */ | |
10470 | ||
10471 | /* XXX the above needs expanding by someone who actually understands it ! */ | |
3fc56081 NK |
10472 | EXTERN_C PerlInterpreter * |
10473 | perl_clone_host(PerlInterpreter* proto_perl, UV flags); | |
645c22ef | 10474 | |
1d7c1841 GS |
10475 | PerlInterpreter * |
10476 | perl_clone(PerlInterpreter *proto_perl, UV flags) | |
10477 | { | |
1d7c1841 | 10478 | #ifdef PERL_IMPLICIT_SYS |
c43294b8 AB |
10479 | |
10480 | /* perlhost.h so we need to call into it | |
10481 | to clone the host, CPerlHost should have a c interface, sky */ | |
10482 | ||
10483 | if (flags & CLONEf_CLONE_HOST) { | |
10484 | return perl_clone_host(proto_perl,flags); | |
10485 | } | |
10486 | return perl_clone_using(proto_perl, flags, | |
1d7c1841 GS |
10487 | proto_perl->IMem, |
10488 | proto_perl->IMemShared, | |
10489 | proto_perl->IMemParse, | |
10490 | proto_perl->IEnv, | |
10491 | proto_perl->IStdIO, | |
10492 | proto_perl->ILIO, | |
10493 | proto_perl->IDir, | |
10494 | proto_perl->ISock, | |
10495 | proto_perl->IProc); | |
10496 | } | |
10497 | ||
10498 | PerlInterpreter * | |
10499 | perl_clone_using(PerlInterpreter *proto_perl, UV flags, | |
10500 | struct IPerlMem* ipM, struct IPerlMem* ipMS, | |
10501 | struct IPerlMem* ipMP, struct IPerlEnv* ipE, | |
10502 | struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO, | |
10503 | struct IPerlDir* ipD, struct IPerlSock* ipS, | |
10504 | struct IPerlProc* ipP) | |
10505 | { | |
10506 | /* XXX many of the string copies here can be optimized if they're | |
10507 | * constants; they need to be allocated as common memory and just | |
10508 | * their pointers copied. */ | |
10509 | ||
10510 | IV i; | |
64aa0685 GS |
10511 | CLONE_PARAMS clone_params; |
10512 | CLONE_PARAMS* param = &clone_params; | |
d2d73c3e | 10513 | |
1d7c1841 | 10514 | PerlInterpreter *my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter)); |
ba869deb | 10515 | PERL_SET_THX(my_perl); |
1d7c1841 | 10516 | |
acfe0abc | 10517 | # ifdef DEBUGGING |
a4530404 | 10518 | Poison(my_perl, 1, PerlInterpreter); |
1d7c1841 GS |
10519 | PL_markstack = 0; |
10520 | PL_scopestack = 0; | |
10521 | PL_savestack = 0; | |
26776375 JH |
10522 | PL_savestack_ix = 0; |
10523 | PL_savestack_max = -1; | |
1d7c1841 | 10524 | PL_retstack = 0; |
66fe0623 | 10525 | PL_sig_pending = 0; |
25596c82 | 10526 | Zero(&PL_debug_pad, 1, struct perl_debug_pad); |
acfe0abc | 10527 | # else /* !DEBUGGING */ |
1d7c1841 | 10528 | Zero(my_perl, 1, PerlInterpreter); |
acfe0abc | 10529 | # endif /* DEBUGGING */ |
1d7c1841 GS |
10530 | |
10531 | /* host pointers */ | |
10532 | PL_Mem = ipM; | |
10533 | PL_MemShared = ipMS; | |
10534 | PL_MemParse = ipMP; | |
10535 | PL_Env = ipE; | |
10536 | PL_StdIO = ipStd; | |
10537 | PL_LIO = ipLIO; | |
10538 | PL_Dir = ipD; | |
10539 | PL_Sock = ipS; | |
10540 | PL_Proc = ipP; | |
1d7c1841 GS |
10541 | #else /* !PERL_IMPLICIT_SYS */ |
10542 | IV i; | |
64aa0685 GS |
10543 | CLONE_PARAMS clone_params; |
10544 | CLONE_PARAMS* param = &clone_params; | |
1d7c1841 | 10545 | PerlInterpreter *my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter)); |
ba869deb | 10546 | PERL_SET_THX(my_perl); |
1d7c1841 | 10547 | |
d2d73c3e AB |
10548 | |
10549 | ||
1d7c1841 | 10550 | # ifdef DEBUGGING |
a4530404 | 10551 | Poison(my_perl, 1, PerlInterpreter); |
1d7c1841 GS |
10552 | PL_markstack = 0; |
10553 | PL_scopestack = 0; | |
10554 | PL_savestack = 0; | |
26776375 JH |
10555 | PL_savestack_ix = 0; |
10556 | PL_savestack_max = -1; | |
1d7c1841 | 10557 | PL_retstack = 0; |
66fe0623 | 10558 | PL_sig_pending = 0; |
25596c82 | 10559 | Zero(&PL_debug_pad, 1, struct perl_debug_pad); |
1d7c1841 GS |
10560 | # else /* !DEBUGGING */ |
10561 | Zero(my_perl, 1, PerlInterpreter); | |
10562 | # endif /* DEBUGGING */ | |
10563 | #endif /* PERL_IMPLICIT_SYS */ | |
83236556 | 10564 | param->flags = flags; |
59b40662 | 10565 | param->proto_perl = proto_perl; |
1d7c1841 GS |
10566 | |
10567 | /* arena roots */ | |
10568 | PL_xiv_arenaroot = NULL; | |
10569 | PL_xiv_root = NULL; | |
612f20c3 | 10570 | PL_xnv_arenaroot = NULL; |
1d7c1841 | 10571 | PL_xnv_root = NULL; |
612f20c3 | 10572 | PL_xrv_arenaroot = NULL; |
1d7c1841 | 10573 | PL_xrv_root = NULL; |
612f20c3 | 10574 | PL_xpv_arenaroot = NULL; |
1d7c1841 | 10575 | PL_xpv_root = NULL; |
612f20c3 | 10576 | PL_xpviv_arenaroot = NULL; |
1d7c1841 | 10577 | PL_xpviv_root = NULL; |
612f20c3 | 10578 | PL_xpvnv_arenaroot = NULL; |
1d7c1841 | 10579 | PL_xpvnv_root = NULL; |
612f20c3 | 10580 | PL_xpvcv_arenaroot = NULL; |
1d7c1841 | 10581 | PL_xpvcv_root = NULL; |
612f20c3 | 10582 | PL_xpvav_arenaroot = NULL; |
1d7c1841 | 10583 | PL_xpvav_root = NULL; |
612f20c3 | 10584 | PL_xpvhv_arenaroot = NULL; |
1d7c1841 | 10585 | PL_xpvhv_root = NULL; |
612f20c3 | 10586 | PL_xpvmg_arenaroot = NULL; |
1d7c1841 | 10587 | PL_xpvmg_root = NULL; |
612f20c3 | 10588 | PL_xpvlv_arenaroot = NULL; |
1d7c1841 | 10589 | PL_xpvlv_root = NULL; |
612f20c3 | 10590 | PL_xpvbm_arenaroot = NULL; |
1d7c1841 | 10591 | PL_xpvbm_root = NULL; |
612f20c3 | 10592 | PL_he_arenaroot = NULL; |
1d7c1841 GS |
10593 | PL_he_root = NULL; |
10594 | PL_nice_chunk = NULL; | |
10595 | PL_nice_chunk_size = 0; | |
10596 | PL_sv_count = 0; | |
10597 | PL_sv_objcount = 0; | |
10598 | PL_sv_root = Nullsv; | |
10599 | PL_sv_arenaroot = Nullsv; | |
10600 | ||
10601 | PL_debug = proto_perl->Idebug; | |
10602 | ||
e5dd39fc | 10603 | #ifdef USE_REENTRANT_API |
59bd0823 | 10604 | Perl_reentrant_init(aTHX); |
e5dd39fc AB |
10605 | #endif |
10606 | ||
1d7c1841 GS |
10607 | /* create SV map for pointer relocation */ |
10608 | PL_ptr_table = ptr_table_new(); | |
10609 | ||
10610 | /* initialize these special pointers as early as possible */ | |
10611 | SvANY(&PL_sv_undef) = NULL; | |
10612 | SvREFCNT(&PL_sv_undef) = (~(U32)0)/2; | |
10613 | SvFLAGS(&PL_sv_undef) = SVf_READONLY|SVt_NULL; | |
10614 | ptr_table_store(PL_ptr_table, &proto_perl->Isv_undef, &PL_sv_undef); | |
10615 | ||
1d7c1841 | 10616 | SvANY(&PL_sv_no) = new_XPVNV(); |
1d7c1841 GS |
10617 | SvREFCNT(&PL_sv_no) = (~(U32)0)/2; |
10618 | SvFLAGS(&PL_sv_no) = SVp_NOK|SVf_NOK|SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV; | |
10619 | SvPVX(&PL_sv_no) = SAVEPVN(PL_No, 0); | |
10620 | SvCUR(&PL_sv_no) = 0; | |
10621 | SvLEN(&PL_sv_no) = 1; | |
10622 | SvNVX(&PL_sv_no) = 0; | |
10623 | ptr_table_store(PL_ptr_table, &proto_perl->Isv_no, &PL_sv_no); | |
10624 | ||
1d7c1841 | 10625 | SvANY(&PL_sv_yes) = new_XPVNV(); |
1d7c1841 GS |
10626 | SvREFCNT(&PL_sv_yes) = (~(U32)0)/2; |
10627 | SvFLAGS(&PL_sv_yes) = SVp_NOK|SVf_NOK|SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV; | |
10628 | SvPVX(&PL_sv_yes) = SAVEPVN(PL_Yes, 1); | |
10629 | SvCUR(&PL_sv_yes) = 1; | |
10630 | SvLEN(&PL_sv_yes) = 2; | |
10631 | SvNVX(&PL_sv_yes) = 1; | |
10632 | ptr_table_store(PL_ptr_table, &proto_perl->Isv_yes, &PL_sv_yes); | |
10633 | ||
05ec9bb3 | 10634 | /* create (a non-shared!) shared string table */ |
1d7c1841 GS |
10635 | PL_strtab = newHV(); |
10636 | HvSHAREKEYS_off(PL_strtab); | |
10637 | hv_ksplit(PL_strtab, 512); | |
10638 | ptr_table_store(PL_ptr_table, proto_perl->Istrtab, PL_strtab); | |
10639 | ||
05ec9bb3 NIS |
10640 | PL_compiling = proto_perl->Icompiling; |
10641 | ||
10642 | /* These two PVs will be free'd special way so must set them same way op.c does */ | |
10643 | PL_compiling.cop_stashpv = savesharedpv(PL_compiling.cop_stashpv); | |
10644 | ptr_table_store(PL_ptr_table, proto_perl->Icompiling.cop_stashpv, PL_compiling.cop_stashpv); | |
10645 | ||
10646 | PL_compiling.cop_file = savesharedpv(PL_compiling.cop_file); | |
10647 | ptr_table_store(PL_ptr_table, proto_perl->Icompiling.cop_file, PL_compiling.cop_file); | |
10648 | ||
1d7c1841 GS |
10649 | ptr_table_store(PL_ptr_table, &proto_perl->Icompiling, &PL_compiling); |
10650 | if (!specialWARN(PL_compiling.cop_warnings)) | |
d2d73c3e | 10651 | PL_compiling.cop_warnings = sv_dup_inc(PL_compiling.cop_warnings, param); |
ac27b0f5 | 10652 | if (!specialCopIO(PL_compiling.cop_io)) |
d2d73c3e | 10653 | PL_compiling.cop_io = sv_dup_inc(PL_compiling.cop_io, param); |
1d7c1841 GS |
10654 | PL_curcop = (COP*)any_dup(proto_perl->Tcurcop, proto_perl); |
10655 | ||
10656 | /* pseudo environmental stuff */ | |
10657 | PL_origargc = proto_perl->Iorigargc; | |
7223e9d8 | 10658 | PL_origargv = proto_perl->Iorigargv; |
d2d73c3e | 10659 | |
d2d73c3e AB |
10660 | param->stashes = newAV(); /* Setup array of objects to call clone on */ |
10661 | ||
a1ea730d | 10662 | #ifdef PERLIO_LAYERS |
3a1ee7e8 NIS |
10663 | /* Clone PerlIO tables as soon as we can handle general xx_dup() */ |
10664 | PerlIO_clone(aTHX_ proto_perl, param); | |
a1ea730d | 10665 | #endif |
d2d73c3e AB |
10666 | |
10667 | PL_envgv = gv_dup(proto_perl->Ienvgv, param); | |
10668 | PL_incgv = gv_dup(proto_perl->Iincgv, param); | |
10669 | PL_hintgv = gv_dup(proto_perl->Ihintgv, param); | |
1d7c1841 | 10670 | PL_origfilename = SAVEPV(proto_perl->Iorigfilename); |
d2d73c3e AB |
10671 | PL_diehook = sv_dup_inc(proto_perl->Idiehook, param); |
10672 | PL_warnhook = sv_dup_inc(proto_perl->Iwarnhook, param); | |
1d7c1841 GS |
10673 | |
10674 | /* switches */ | |
10675 | PL_minus_c = proto_perl->Iminus_c; | |
d2d73c3e | 10676 | PL_patchlevel = sv_dup_inc(proto_perl->Ipatchlevel, param); |
1d7c1841 GS |
10677 | PL_localpatches = proto_perl->Ilocalpatches; |
10678 | PL_splitstr = proto_perl->Isplitstr; | |
10679 | PL_preprocess = proto_perl->Ipreprocess; | |
10680 | PL_minus_n = proto_perl->Iminus_n; | |
10681 | PL_minus_p = proto_perl->Iminus_p; | |
10682 | PL_minus_l = proto_perl->Iminus_l; | |
10683 | PL_minus_a = proto_perl->Iminus_a; | |
10684 | PL_minus_F = proto_perl->Iminus_F; | |
10685 | PL_doswitches = proto_perl->Idoswitches; | |
10686 | PL_dowarn = proto_perl->Idowarn; | |
10687 | PL_doextract = proto_perl->Idoextract; | |
10688 | PL_sawampersand = proto_perl->Isawampersand; | |
10689 | PL_unsafe = proto_perl->Iunsafe; | |
10690 | PL_inplace = SAVEPV(proto_perl->Iinplace); | |
d2d73c3e | 10691 | PL_e_script = sv_dup_inc(proto_perl->Ie_script, param); |
1d7c1841 GS |
10692 | PL_perldb = proto_perl->Iperldb; |
10693 | PL_perl_destruct_level = proto_perl->Iperl_destruct_level; | |
1cbb0781 | 10694 | PL_exit_flags = proto_perl->Iexit_flags; |
1d7c1841 GS |
10695 | |
10696 | /* magical thingies */ | |
10697 | /* XXX time(&PL_basetime) when asked for? */ | |
10698 | PL_basetime = proto_perl->Ibasetime; | |
d2d73c3e | 10699 | PL_formfeed = sv_dup(proto_perl->Iformfeed, param); |
1d7c1841 GS |
10700 | |
10701 | PL_maxsysfd = proto_perl->Imaxsysfd; | |
10702 | PL_multiline = proto_perl->Imultiline; | |
10703 | PL_statusvalue = proto_perl->Istatusvalue; | |
10704 | #ifdef VMS | |
10705 | PL_statusvalue_vms = proto_perl->Istatusvalue_vms; | |
10706 | #endif | |
0a378802 | 10707 | PL_encoding = sv_dup(proto_perl->Iencoding, param); |
1d7c1841 | 10708 | |
4a4c6fe3 | 10709 | sv_setpvn(PERL_DEBUG_PAD(0), "", 0); /* For regex debugging. */ |
1f483ca1 JH |
10710 | sv_setpvn(PERL_DEBUG_PAD(1), "", 0); /* ext/re needs these */ |
10711 | sv_setpvn(PERL_DEBUG_PAD(2), "", 0); /* even without DEBUGGING. */ | |
4a4c6fe3 | 10712 | |
d2f185dc AMS |
10713 | /* Clone the regex array */ |
10714 | PL_regex_padav = newAV(); | |
10715 | { | |
10716 | I32 len = av_len((AV*)proto_perl->Iregex_padav); | |
10717 | SV** regexen = AvARRAY((AV*)proto_perl->Iregex_padav); | |
0f95fc41 AB |
10718 | av_push(PL_regex_padav, |
10719 | sv_dup_inc(regexen[0],param)); | |
10720 | for(i = 1; i <= len; i++) { | |
10721 | if(SvREPADTMP(regexen[i])) { | |
10722 | av_push(PL_regex_padav, sv_dup_inc(regexen[i], param)); | |
8cf8f3d1 | 10723 | } else { |
0f95fc41 AB |
10724 | av_push(PL_regex_padav, |
10725 | SvREFCNT_inc( | |
8cf8f3d1 | 10726 | newSViv(PTR2IV(re_dup(INT2PTR(REGEXP *, |
cbfa9890 | 10727 | SvIVX(regexen[i])), param))) |
0f95fc41 AB |
10728 | )); |
10729 | } | |
d2f185dc AMS |
10730 | } |
10731 | } | |
10732 | PL_regex_pad = AvARRAY(PL_regex_padav); | |
1fcf4c12 | 10733 | |
1d7c1841 | 10734 | /* shortcuts to various I/O objects */ |
d2d73c3e AB |
10735 | PL_stdingv = gv_dup(proto_perl->Istdingv, param); |
10736 | PL_stderrgv = gv_dup(proto_perl->Istderrgv, param); | |
10737 | PL_defgv = gv_dup(proto_perl->Idefgv, param); | |
10738 | PL_argvgv = gv_dup(proto_perl->Iargvgv, param); | |
10739 | PL_argvoutgv = gv_dup(proto_perl->Iargvoutgv, param); | |
10740 | PL_argvout_stack = av_dup_inc(proto_perl->Iargvout_stack, param); | |
1d7c1841 GS |
10741 | |
10742 | /* shortcuts to regexp stuff */ | |
d2d73c3e | 10743 | PL_replgv = gv_dup(proto_perl->Ireplgv, param); |
1d7c1841 GS |
10744 | |
10745 | /* shortcuts to misc objects */ | |
d2d73c3e | 10746 | PL_errgv = gv_dup(proto_perl->Ierrgv, param); |
1d7c1841 GS |
10747 | |
10748 | /* shortcuts to debugging objects */ | |
d2d73c3e AB |
10749 | PL_DBgv = gv_dup(proto_perl->IDBgv, param); |
10750 | PL_DBline = gv_dup(proto_perl->IDBline, param); | |
10751 | PL_DBsub = gv_dup(proto_perl->IDBsub, param); | |
10752 | PL_DBsingle = sv_dup(proto_perl->IDBsingle, param); | |
10753 | PL_DBtrace = sv_dup(proto_perl->IDBtrace, param); | |
10754 | PL_DBsignal = sv_dup(proto_perl->IDBsignal, param); | |
10755 | PL_lineary = av_dup(proto_perl->Ilineary, param); | |
10756 | PL_dbargs = av_dup(proto_perl->Idbargs, param); | |
1d7c1841 GS |
10757 | |
10758 | /* symbol tables */ | |
d2d73c3e AB |
10759 | PL_defstash = hv_dup_inc(proto_perl->Tdefstash, param); |
10760 | PL_curstash = hv_dup(proto_perl->Tcurstash, param); | |
10761 | PL_nullstash = hv_dup(proto_perl->Inullstash, param); | |
10762 | PL_debstash = hv_dup(proto_perl->Idebstash, param); | |
10763 | PL_globalstash = hv_dup(proto_perl->Iglobalstash, param); | |
10764 | PL_curstname = sv_dup_inc(proto_perl->Icurstname, param); | |
10765 | ||
10766 | PL_beginav = av_dup_inc(proto_perl->Ibeginav, param); | |
ee1c5a4e | 10767 | PL_beginav_save = av_dup_inc(proto_perl->Ibeginav_save, param); |
5b7ea690 | 10768 | PL_checkav_save = av_dup_inc(proto_perl->Icheckav_save, param); |
d2d73c3e AB |
10769 | PL_endav = av_dup_inc(proto_perl->Iendav, param); |
10770 | PL_checkav = av_dup_inc(proto_perl->Icheckav, param); | |
10771 | PL_initav = av_dup_inc(proto_perl->Iinitav, param); | |
1d7c1841 GS |
10772 | |
10773 | PL_sub_generation = proto_perl->Isub_generation; | |
10774 | ||
10775 | /* funky return mechanisms */ | |
10776 | PL_forkprocess = proto_perl->Iforkprocess; | |
10777 | ||
10778 | /* subprocess state */ | |
d2d73c3e | 10779 | PL_fdpid = av_dup_inc(proto_perl->Ifdpid, param); |
1d7c1841 GS |
10780 | |
10781 | /* internal state */ | |
10782 | PL_tainting = proto_perl->Itainting; | |
654c77f7 | 10783 | PL_taint_warn = proto_perl->Itaint_warn; |
1d7c1841 GS |
10784 | PL_maxo = proto_perl->Imaxo; |
10785 | if (proto_perl->Iop_mask) | |
10786 | PL_op_mask = SAVEPVN(proto_perl->Iop_mask, PL_maxo); | |
10787 | else | |
10788 | PL_op_mask = Nullch; | |
10789 | ||
10790 | /* current interpreter roots */ | |
d2d73c3e | 10791 | PL_main_cv = cv_dup_inc(proto_perl->Imain_cv, param); |
1d7c1841 GS |
10792 | PL_main_root = OpREFCNT_inc(proto_perl->Imain_root); |
10793 | PL_main_start = proto_perl->Imain_start; | |
e977893f | 10794 | PL_eval_root = proto_perl->Ieval_root; |
1d7c1841 GS |
10795 | PL_eval_start = proto_perl->Ieval_start; |
10796 | ||
10797 | /* runtime control stuff */ | |
10798 | PL_curcopdb = (COP*)any_dup(proto_perl->Icurcopdb, proto_perl); | |
10799 | PL_copline = proto_perl->Icopline; | |
10800 | ||
10801 | PL_filemode = proto_perl->Ifilemode; | |
10802 | PL_lastfd = proto_perl->Ilastfd; | |
10803 | PL_oldname = proto_perl->Ioldname; /* XXX not quite right */ | |
10804 | PL_Argv = NULL; | |
10805 | PL_Cmd = Nullch; | |
10806 | PL_gensym = proto_perl->Igensym; | |
10807 | PL_preambled = proto_perl->Ipreambled; | |
d2d73c3e | 10808 | PL_preambleav = av_dup_inc(proto_perl->Ipreambleav, param); |
1d7c1841 GS |
10809 | PL_laststatval = proto_perl->Ilaststatval; |
10810 | PL_laststype = proto_perl->Ilaststype; | |
10811 | PL_mess_sv = Nullsv; | |
10812 | ||
d2d73c3e | 10813 | PL_ors_sv = sv_dup_inc(proto_perl->Iors_sv, param); |
1d7c1841 GS |
10814 | PL_ofmt = SAVEPV(proto_perl->Iofmt); |
10815 | ||
10816 | /* interpreter atexit processing */ | |
10817 | PL_exitlistlen = proto_perl->Iexitlistlen; | |
10818 | if (PL_exitlistlen) { | |
10819 | New(0, PL_exitlist, PL_exitlistlen, PerlExitListEntry); | |
10820 | Copy(proto_perl->Iexitlist, PL_exitlist, PL_exitlistlen, PerlExitListEntry); | |
10821 | } | |
10822 | else | |
10823 | PL_exitlist = (PerlExitListEntry*)NULL; | |
d2d73c3e | 10824 | PL_modglobal = hv_dup_inc(proto_perl->Imodglobal, param); |
19e8ce8e AB |
10825 | PL_custom_op_names = hv_dup_inc(proto_perl->Icustom_op_names,param); |
10826 | PL_custom_op_descs = hv_dup_inc(proto_perl->Icustom_op_descs,param); | |
1d7c1841 GS |
10827 | |
10828 | PL_profiledata = NULL; | |
a8fc9800 | 10829 | PL_rsfp = fp_dup(proto_perl->Irsfp, '<', param); |
1d7c1841 | 10830 | /* PL_rsfp_filters entries have fake IoDIRP() */ |
d2d73c3e | 10831 | PL_rsfp_filters = av_dup_inc(proto_perl->Irsfp_filters, param); |
1d7c1841 | 10832 | |
d2d73c3e | 10833 | PL_compcv = cv_dup(proto_perl->Icompcv, param); |
9755d405 JH |
10834 | |
10835 | PAD_CLONE_VARS(proto_perl, param); | |
1d7c1841 GS |
10836 | |
10837 | #ifdef HAVE_INTERP_INTERN | |
10838 | sys_intern_dup(&proto_perl->Isys_intern, &PL_sys_intern); | |
10839 | #endif | |
10840 | ||
10841 | /* more statics moved here */ | |
10842 | PL_generation = proto_perl->Igeneration; | |
d2d73c3e | 10843 | PL_DBcv = cv_dup(proto_perl->IDBcv, param); |
1d7c1841 GS |
10844 | |
10845 | PL_in_clean_objs = proto_perl->Iin_clean_objs; | |
10846 | PL_in_clean_all = proto_perl->Iin_clean_all; | |
10847 | ||
10848 | PL_uid = proto_perl->Iuid; | |
10849 | PL_euid = proto_perl->Ieuid; | |
10850 | PL_gid = proto_perl->Igid; | |
10851 | PL_egid = proto_perl->Iegid; | |
10852 | PL_nomemok = proto_perl->Inomemok; | |
10853 | PL_an = proto_perl->Ian; | |
1d7c1841 GS |
10854 | PL_op_seqmax = proto_perl->Iop_seqmax; |
10855 | PL_evalseq = proto_perl->Ievalseq; | |
10856 | PL_origenviron = proto_perl->Iorigenviron; /* XXX not quite right */ | |
10857 | PL_origalen = proto_perl->Iorigalen; | |
10858 | PL_pidstatus = newHV(); /* XXX flag for cloning? */ | |
10859 | PL_osname = SAVEPV(proto_perl->Iosname); | |
8257dec7 | 10860 | PL_sh_path_compat = proto_perl->Ish_path_compat; /* XXX never deallocated */ |
1d7c1841 GS |
10861 | PL_sighandlerp = proto_perl->Isighandlerp; |
10862 | ||
10863 | ||
10864 | PL_runops = proto_perl->Irunops; | |
10865 | ||
10866 | Copy(proto_perl->Itokenbuf, PL_tokenbuf, 256, char); | |
10867 | ||
10868 | #ifdef CSH | |
10869 | PL_cshlen = proto_perl->Icshlen; | |
74f1b2b8 | 10870 | PL_cshname = proto_perl->Icshname; /* XXX never deallocated */ |
1d7c1841 GS |
10871 | #endif |
10872 | ||
10873 | PL_lex_state = proto_perl->Ilex_state; | |
10874 | PL_lex_defer = proto_perl->Ilex_defer; | |
10875 | PL_lex_expect = proto_perl->Ilex_expect; | |
10876 | PL_lex_formbrack = proto_perl->Ilex_formbrack; | |
10877 | PL_lex_dojoin = proto_perl->Ilex_dojoin; | |
10878 | PL_lex_starts = proto_perl->Ilex_starts; | |
d2d73c3e AB |
10879 | PL_lex_stuff = sv_dup_inc(proto_perl->Ilex_stuff, param); |
10880 | PL_lex_repl = sv_dup_inc(proto_perl->Ilex_repl, param); | |
1d7c1841 GS |
10881 | PL_lex_op = proto_perl->Ilex_op; |
10882 | PL_lex_inpat = proto_perl->Ilex_inpat; | |
10883 | PL_lex_inwhat = proto_perl->Ilex_inwhat; | |
10884 | PL_lex_brackets = proto_perl->Ilex_brackets; | |
10885 | i = (PL_lex_brackets < 120 ? 120 : PL_lex_brackets); | |
10886 | PL_lex_brackstack = SAVEPVN(proto_perl->Ilex_brackstack,i); | |
10887 | PL_lex_casemods = proto_perl->Ilex_casemods; | |
10888 | i = (PL_lex_casemods < 12 ? 12 : PL_lex_casemods); | |
10889 | PL_lex_casestack = SAVEPVN(proto_perl->Ilex_casestack,i); | |
10890 | ||
10891 | Copy(proto_perl->Inextval, PL_nextval, 5, YYSTYPE); | |
10892 | Copy(proto_perl->Inexttype, PL_nexttype, 5, I32); | |
10893 | PL_nexttoke = proto_perl->Inexttoke; | |
10894 | ||
1d773130 TB |
10895 | /* XXX This is probably masking the deeper issue of why |
10896 | * SvANY(proto_perl->Ilinestr) can be NULL at this point. For test case: | |
10897 | * http://archive.develooper.com/perl5-porters%40perl.org/msg83298.html | |
10898 | * (A little debugging with a watchpoint on it may help.) | |
10899 | */ | |
389edf32 TB |
10900 | if (SvANY(proto_perl->Ilinestr)) { |
10901 | PL_linestr = sv_dup_inc(proto_perl->Ilinestr, param); | |
10902 | i = proto_perl->Ibufptr - SvPVX(proto_perl->Ilinestr); | |
10903 | PL_bufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
10904 | i = proto_perl->Ioldbufptr - SvPVX(proto_perl->Ilinestr); | |
10905 | PL_oldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
10906 | i = proto_perl->Ioldoldbufptr - SvPVX(proto_perl->Ilinestr); | |
10907 | PL_oldoldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
10908 | i = proto_perl->Ilinestart - SvPVX(proto_perl->Ilinestr); | |
10909 | PL_linestart = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
10910 | } | |
10911 | else { | |
10912 | PL_linestr = NEWSV(65,79); | |
10913 | sv_upgrade(PL_linestr,SVt_PVIV); | |
10914 | sv_setpvn(PL_linestr,"",0); | |
10915 | PL_bufptr = PL_oldbufptr = PL_oldoldbufptr = PL_linestart = SvPVX(PL_linestr); | |
10916 | } | |
1d7c1841 | 10917 | PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr); |
1d7c1841 GS |
10918 | PL_pending_ident = proto_perl->Ipending_ident; |
10919 | PL_sublex_info = proto_perl->Isublex_info; /* XXX not quite right */ | |
10920 | ||
10921 | PL_expect = proto_perl->Iexpect; | |
10922 | ||
10923 | PL_multi_start = proto_perl->Imulti_start; | |
10924 | PL_multi_end = proto_perl->Imulti_end; | |
10925 | PL_multi_open = proto_perl->Imulti_open; | |
10926 | PL_multi_close = proto_perl->Imulti_close; | |
10927 | ||
10928 | PL_error_count = proto_perl->Ierror_count; | |
10929 | PL_subline = proto_perl->Isubline; | |
d2d73c3e | 10930 | PL_subname = sv_dup_inc(proto_perl->Isubname, param); |
1d7c1841 | 10931 | |
1d773130 | 10932 | /* XXX See comment on SvANY(proto_perl->Ilinestr) above */ |
389edf32 TB |
10933 | if (SvANY(proto_perl->Ilinestr)) { |
10934 | i = proto_perl->Ilast_uni - SvPVX(proto_perl->Ilinestr); | |
10935 | PL_last_uni = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
10936 | i = proto_perl->Ilast_lop - SvPVX(proto_perl->Ilinestr); | |
10937 | PL_last_lop = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
10938 | PL_last_lop_op = proto_perl->Ilast_lop_op; | |
10939 | } | |
10940 | else { | |
10941 | PL_last_uni = SvPVX(PL_linestr); | |
10942 | PL_last_lop = SvPVX(PL_linestr); | |
10943 | PL_last_lop_op = 0; | |
10944 | } | |
1d7c1841 | 10945 | PL_in_my = proto_perl->Iin_my; |
d2d73c3e | 10946 | PL_in_my_stash = hv_dup(proto_perl->Iin_my_stash, param); |
1d7c1841 GS |
10947 | #ifdef FCRYPT |
10948 | PL_cryptseen = proto_perl->Icryptseen; | |
10949 | #endif | |
10950 | ||
10951 | PL_hints = proto_perl->Ihints; | |
10952 | ||
10953 | PL_amagic_generation = proto_perl->Iamagic_generation; | |
10954 | ||
10955 | #ifdef USE_LOCALE_COLLATE | |
10956 | PL_collation_ix = proto_perl->Icollation_ix; | |
10957 | PL_collation_name = SAVEPV(proto_perl->Icollation_name); | |
10958 | PL_collation_standard = proto_perl->Icollation_standard; | |
10959 | PL_collxfrm_base = proto_perl->Icollxfrm_base; | |
10960 | PL_collxfrm_mult = proto_perl->Icollxfrm_mult; | |
10961 | #endif /* USE_LOCALE_COLLATE */ | |
10962 | ||
10963 | #ifdef USE_LOCALE_NUMERIC | |
10964 | PL_numeric_name = SAVEPV(proto_perl->Inumeric_name); | |
10965 | PL_numeric_standard = proto_perl->Inumeric_standard; | |
10966 | PL_numeric_local = proto_perl->Inumeric_local; | |
d2d73c3e | 10967 | PL_numeric_radix_sv = sv_dup_inc(proto_perl->Inumeric_radix_sv, param); |
1d7c1841 GS |
10968 | #endif /* !USE_LOCALE_NUMERIC */ |
10969 | ||
10970 | /* utf8 character classes */ | |
d2d73c3e AB |
10971 | PL_utf8_alnum = sv_dup_inc(proto_perl->Iutf8_alnum, param); |
10972 | PL_utf8_alnumc = sv_dup_inc(proto_perl->Iutf8_alnumc, param); | |
10973 | PL_utf8_ascii = sv_dup_inc(proto_perl->Iutf8_ascii, param); | |
10974 | PL_utf8_alpha = sv_dup_inc(proto_perl->Iutf8_alpha, param); | |
10975 | PL_utf8_space = sv_dup_inc(proto_perl->Iutf8_space, param); | |
10976 | PL_utf8_cntrl = sv_dup_inc(proto_perl->Iutf8_cntrl, param); | |
10977 | PL_utf8_graph = sv_dup_inc(proto_perl->Iutf8_graph, param); | |
10978 | PL_utf8_digit = sv_dup_inc(proto_perl->Iutf8_digit, param); | |
10979 | PL_utf8_upper = sv_dup_inc(proto_perl->Iutf8_upper, param); | |
10980 | PL_utf8_lower = sv_dup_inc(proto_perl->Iutf8_lower, param); | |
10981 | PL_utf8_print = sv_dup_inc(proto_perl->Iutf8_print, param); | |
10982 | PL_utf8_punct = sv_dup_inc(proto_perl->Iutf8_punct, param); | |
10983 | PL_utf8_xdigit = sv_dup_inc(proto_perl->Iutf8_xdigit, param); | |
10984 | PL_utf8_mark = sv_dup_inc(proto_perl->Iutf8_mark, param); | |
10985 | PL_utf8_toupper = sv_dup_inc(proto_perl->Iutf8_toupper, param); | |
10986 | PL_utf8_totitle = sv_dup_inc(proto_perl->Iutf8_totitle, param); | |
10987 | PL_utf8_tolower = sv_dup_inc(proto_perl->Iutf8_tolower, param); | |
b4e400f9 | 10988 | PL_utf8_tofold = sv_dup_inc(proto_perl->Iutf8_tofold, param); |
82686b01 JH |
10989 | PL_utf8_idstart = sv_dup_inc(proto_perl->Iutf8_idstart, param); |
10990 | PL_utf8_idcont = sv_dup_inc(proto_perl->Iutf8_idcont, param); | |
1d7c1841 | 10991 | |
654c77f7 | 10992 | /* Did the locale setup indicate UTF-8? */ |
30260be2 | 10993 | PL_utf8locale = proto_perl->Iutf8locale; |
654c77f7 JH |
10994 | /* Unicode features (see perlrun/-C) */ |
10995 | PL_unicode = proto_perl->Iunicode; | |
10996 | ||
10997 | /* Pre-5.8 signals control */ | |
10998 | PL_signals = proto_perl->Isignals; | |
10999 | ||
11000 | /* times() ticks per second */ | |
11001 | PL_clocktick = proto_perl->Iclocktick; | |
11002 | ||
11003 | /* Recursion stopper for PerlIO_find_layer */ | |
11004 | PL_in_load_module = proto_perl->Iin_load_module; | |
11005 | ||
11006 | /* sort() routine */ | |
11007 | PL_sort_RealCmp = proto_perl->Isort_RealCmp; | |
11008 | ||
630cd32b JH |
11009 | /* Not really needed/useful since the reenrant_retint is "volatile", |
11010 | * but do it for consistency's sake. */ | |
11011 | PL_reentrant_retint = proto_perl->Ireentrant_retint; | |
11012 | ||
0710cc63 JH |
11013 | /* Hooks to shared SVs and locks. */ |
11014 | PL_sharehook = proto_perl->Isharehook; | |
11015 | PL_lockhook = proto_perl->Ilockhook; | |
11016 | PL_unlockhook = proto_perl->Iunlockhook; | |
11017 | PL_threadhook = proto_perl->Ithreadhook; | |
11018 | ||
11019 | PL_runops_std = proto_perl->Irunops_std; | |
11020 | PL_runops_dbg = proto_perl->Irunops_dbg; | |
11021 | ||
11022 | #ifdef THREADS_HAVE_PIDS | |
11023 | PL_ppid = proto_perl->Ippid; | |
11024 | #endif | |
11025 | ||
1d7c1841 GS |
11026 | /* swatch cache */ |
11027 | PL_last_swash_hv = Nullhv; /* reinits on demand */ | |
11028 | PL_last_swash_klen = 0; | |
11029 | PL_last_swash_key[0]= '\0'; | |
11030 | PL_last_swash_tmps = (U8*)NULL; | |
11031 | PL_last_swash_slen = 0; | |
11032 | ||
11033 | /* perly.c globals */ | |
11034 | PL_yydebug = proto_perl->Iyydebug; | |
11035 | PL_yynerrs = proto_perl->Iyynerrs; | |
11036 | PL_yyerrflag = proto_perl->Iyyerrflag; | |
11037 | PL_yychar = proto_perl->Iyychar; | |
11038 | PL_yyval = proto_perl->Iyyval; | |
11039 | PL_yylval = proto_perl->Iyylval; | |
11040 | ||
11041 | PL_glob_index = proto_perl->Iglob_index; | |
11042 | PL_srand_called = proto_perl->Isrand_called; | |
c71f9582 | 11043 | PL_hash_seed = proto_perl->Ihash_seed; |
1d7c1841 GS |
11044 | PL_uudmap['M'] = 0; /* reinits on demand */ |
11045 | PL_bitcount = Nullch; /* reinits on demand */ | |
11046 | ||
66fe0623 NIS |
11047 | if (proto_perl->Ipsig_pend) { |
11048 | Newz(0, PL_psig_pend, SIG_SIZE, int); | |
9dd79c3f | 11049 | } |
66fe0623 NIS |
11050 | else { |
11051 | PL_psig_pend = (int*)NULL; | |
11052 | } | |
11053 | ||
1d7c1841 | 11054 | if (proto_perl->Ipsig_ptr) { |
76d3c696 JH |
11055 | Newz(0, PL_psig_ptr, SIG_SIZE, SV*); |
11056 | Newz(0, PL_psig_name, SIG_SIZE, SV*); | |
76d3c696 | 11057 | for (i = 1; i < SIG_SIZE; i++) { |
d2d73c3e AB |
11058 | PL_psig_ptr[i] = sv_dup_inc(proto_perl->Ipsig_ptr[i], param); |
11059 | PL_psig_name[i] = sv_dup_inc(proto_perl->Ipsig_name[i], param); | |
1d7c1841 GS |
11060 | } |
11061 | } | |
11062 | else { | |
11063 | PL_psig_ptr = (SV**)NULL; | |
11064 | PL_psig_name = (SV**)NULL; | |
11065 | } | |
11066 | ||
11067 | /* thrdvar.h stuff */ | |
11068 | ||
a0739874 | 11069 | if (flags & CLONEf_COPY_STACKS) { |
1d7c1841 GS |
11070 | /* next allocation will be PL_tmps_stack[PL_tmps_ix+1] */ |
11071 | PL_tmps_ix = proto_perl->Ttmps_ix; | |
11072 | PL_tmps_max = proto_perl->Ttmps_max; | |
11073 | PL_tmps_floor = proto_perl->Ttmps_floor; | |
11074 | Newz(50, PL_tmps_stack, PL_tmps_max, SV*); | |
11075 | i = 0; | |
11076 | while (i <= PL_tmps_ix) { | |
d2d73c3e | 11077 | PL_tmps_stack[i] = sv_dup_inc(proto_perl->Ttmps_stack[i], param); |
1d7c1841 GS |
11078 | ++i; |
11079 | } | |
11080 | ||
11081 | /* next PUSHMARK() sets *(PL_markstack_ptr+1) */ | |
11082 | i = proto_perl->Tmarkstack_max - proto_perl->Tmarkstack; | |
11083 | Newz(54, PL_markstack, i, I32); | |
11084 | PL_markstack_max = PL_markstack + (proto_perl->Tmarkstack_max | |
11085 | - proto_perl->Tmarkstack); | |
11086 | PL_markstack_ptr = PL_markstack + (proto_perl->Tmarkstack_ptr | |
11087 | - proto_perl->Tmarkstack); | |
11088 | Copy(proto_perl->Tmarkstack, PL_markstack, | |
11089 | PL_markstack_ptr - PL_markstack + 1, I32); | |
11090 | ||
11091 | /* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix] | |
11092 | * NOTE: unlike the others! */ | |
11093 | PL_scopestack_ix = proto_perl->Tscopestack_ix; | |
11094 | PL_scopestack_max = proto_perl->Tscopestack_max; | |
11095 | Newz(54, PL_scopestack, PL_scopestack_max, I32); | |
11096 | Copy(proto_perl->Tscopestack, PL_scopestack, PL_scopestack_ix, I32); | |
11097 | ||
11098 | /* next push_return() sets PL_retstack[PL_retstack_ix] | |
11099 | * NOTE: unlike the others! */ | |
11100 | PL_retstack_ix = proto_perl->Tretstack_ix; | |
11101 | PL_retstack_max = proto_perl->Tretstack_max; | |
11102 | Newz(54, PL_retstack, PL_retstack_max, OP*); | |
ce0a1ae0 | 11103 | Copy(proto_perl->Tretstack, PL_retstack, PL_retstack_ix, OP*); |
1d7c1841 GS |
11104 | |
11105 | /* NOTE: si_dup() looks at PL_markstack */ | |
d2d73c3e | 11106 | PL_curstackinfo = si_dup(proto_perl->Tcurstackinfo, param); |
1d7c1841 GS |
11107 | |
11108 | /* PL_curstack = PL_curstackinfo->si_stack; */ | |
d2d73c3e AB |
11109 | PL_curstack = av_dup(proto_perl->Tcurstack, param); |
11110 | PL_mainstack = av_dup(proto_perl->Tmainstack, param); | |
1d7c1841 GS |
11111 | |
11112 | /* next PUSHs() etc. set *(PL_stack_sp+1) */ | |
11113 | PL_stack_base = AvARRAY(PL_curstack); | |
11114 | PL_stack_sp = PL_stack_base + (proto_perl->Tstack_sp | |
11115 | - proto_perl->Tstack_base); | |
11116 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); | |
11117 | ||
11118 | /* next SSPUSHFOO() sets PL_savestack[PL_savestack_ix] | |
11119 | * NOTE: unlike the others! */ | |
11120 | PL_savestack_ix = proto_perl->Tsavestack_ix; | |
11121 | PL_savestack_max = proto_perl->Tsavestack_max; | |
11122 | /*Newz(54, PL_savestack, PL_savestack_max, ANY);*/ | |
d2d73c3e | 11123 | PL_savestack = ss_dup(proto_perl, param); |
1d7c1841 GS |
11124 | } |
11125 | else { | |
11126 | init_stacks(); | |
985e7056 | 11127 | ENTER; /* perl_destruct() wants to LEAVE; */ |
1d7c1841 GS |
11128 | } |
11129 | ||
11130 | PL_start_env = proto_perl->Tstart_env; /* XXXXXX */ | |
11131 | PL_top_env = &PL_start_env; | |
11132 | ||
11133 | PL_op = proto_perl->Top; | |
11134 | ||
11135 | PL_Sv = Nullsv; | |
11136 | PL_Xpv = (XPV*)NULL; | |
11137 | PL_na = proto_perl->Tna; | |
11138 | ||
11139 | PL_statbuf = proto_perl->Tstatbuf; | |
11140 | PL_statcache = proto_perl->Tstatcache; | |
d2d73c3e AB |
11141 | PL_statgv = gv_dup(proto_perl->Tstatgv, param); |
11142 | PL_statname = sv_dup_inc(proto_perl->Tstatname, param); | |
1d7c1841 GS |
11143 | #ifdef HAS_TIMES |
11144 | PL_timesbuf = proto_perl->Ttimesbuf; | |
11145 | #endif | |
11146 | ||
11147 | PL_tainted = proto_perl->Ttainted; | |
11148 | PL_curpm = proto_perl->Tcurpm; /* XXX No PMOP ref count */ | |
d2d73c3e AB |
11149 | PL_rs = sv_dup_inc(proto_perl->Trs, param); |
11150 | PL_last_in_gv = gv_dup(proto_perl->Tlast_in_gv, param); | |
11151 | PL_ofs_sv = sv_dup_inc(proto_perl->Tofs_sv, param); | |
11152 | PL_defoutgv = gv_dup_inc(proto_perl->Tdefoutgv, param); | |
1d7c1841 | 11153 | PL_chopset = proto_perl->Tchopset; /* XXX never deallocated */ |
d2d73c3e AB |
11154 | PL_toptarget = sv_dup_inc(proto_perl->Ttoptarget, param); |
11155 | PL_bodytarget = sv_dup_inc(proto_perl->Tbodytarget, param); | |
11156 | PL_formtarget = sv_dup(proto_perl->Tformtarget, param); | |
1d7c1841 GS |
11157 | |
11158 | PL_restartop = proto_perl->Trestartop; | |
11159 | PL_in_eval = proto_perl->Tin_eval; | |
11160 | PL_delaymagic = proto_perl->Tdelaymagic; | |
11161 | PL_dirty = proto_perl->Tdirty; | |
11162 | PL_localizing = proto_perl->Tlocalizing; | |
11163 | ||
14dd3ad8 | 11164 | #ifdef PERL_FLEXIBLE_EXCEPTIONS |
1d7c1841 | 11165 | PL_protect = proto_perl->Tprotect; |
14dd3ad8 | 11166 | #endif |
d2d73c3e | 11167 | PL_errors = sv_dup_inc(proto_perl->Terrors, param); |
73c86719 | 11168 | PL_hv_fetch_ent_mh = Nullhe; |
1d7c1841 GS |
11169 | PL_modcount = proto_perl->Tmodcount; |
11170 | PL_lastgotoprobe = Nullop; | |
11171 | PL_dumpindent = proto_perl->Tdumpindent; | |
11172 | ||
11173 | PL_sortcop = (OP*)any_dup(proto_perl->Tsortcop, proto_perl); | |
d2d73c3e AB |
11174 | PL_sortstash = hv_dup(proto_perl->Tsortstash, param); |
11175 | PL_firstgv = gv_dup(proto_perl->Tfirstgv, param); | |
11176 | PL_secondgv = gv_dup(proto_perl->Tsecondgv, param); | |
1d7c1841 GS |
11177 | PL_sortcxix = proto_perl->Tsortcxix; |
11178 | PL_efloatbuf = Nullch; /* reinits on demand */ | |
11179 | PL_efloatsize = 0; /* reinits on demand */ | |
11180 | ||
11181 | /* regex stuff */ | |
11182 | ||
11183 | PL_screamfirst = NULL; | |
11184 | PL_screamnext = NULL; | |
11185 | PL_maxscream = -1; /* reinits on demand */ | |
11186 | PL_lastscream = Nullsv; | |
11187 | ||
11188 | PL_watchaddr = NULL; | |
11189 | PL_watchok = Nullch; | |
11190 | ||
11191 | PL_regdummy = proto_perl->Tregdummy; | |
11192 | PL_regcomp_parse = Nullch; | |
11193 | PL_regxend = Nullch; | |
11194 | PL_regcode = (regnode*)NULL; | |
11195 | PL_regnaughty = 0; | |
11196 | PL_regsawback = 0; | |
11197 | PL_regprecomp = Nullch; | |
11198 | PL_regnpar = 0; | |
11199 | PL_regsize = 0; | |
11200 | PL_regflags = 0; | |
11201 | PL_regseen = 0; | |
11202 | PL_seen_zerolen = 0; | |
11203 | PL_seen_evals = 0; | |
11204 | PL_regcomp_rx = (regexp*)NULL; | |
11205 | PL_extralen = 0; | |
11206 | PL_colorset = 0; /* reinits PL_colors[] */ | |
11207 | /*PL_colors[6] = {0,0,0,0,0,0};*/ | |
11208 | PL_reg_whilem_seen = 0; | |
11209 | PL_reginput = Nullch; | |
11210 | PL_regbol = Nullch; | |
11211 | PL_regeol = Nullch; | |
11212 | PL_regstartp = (I32*)NULL; | |
11213 | PL_regendp = (I32*)NULL; | |
11214 | PL_reglastparen = (U32*)NULL; | |
4e58e0cf | 11215 | PL_reglastcloseparen = (U32*)NULL; |
1d7c1841 | 11216 | PL_regtill = Nullch; |
1d7c1841 GS |
11217 | PL_reg_start_tmp = (char**)NULL; |
11218 | PL_reg_start_tmpl = 0; | |
11219 | PL_regdata = (struct reg_data*)NULL; | |
11220 | PL_bostr = Nullch; | |
11221 | PL_reg_flags = 0; | |
11222 | PL_reg_eval_set = 0; | |
11223 | PL_regnarrate = 0; | |
11224 | PL_regprogram = (regnode*)NULL; | |
11225 | PL_regindent = 0; | |
11226 | PL_regcc = (CURCUR*)NULL; | |
11227 | PL_reg_call_cc = (struct re_cc_state*)NULL; | |
11228 | PL_reg_re = (regexp*)NULL; | |
11229 | PL_reg_ganch = Nullch; | |
11230 | PL_reg_sv = Nullsv; | |
53c4c00c | 11231 | PL_reg_match_utf8 = FALSE; |
1d7c1841 GS |
11232 | PL_reg_magic = (MAGIC*)NULL; |
11233 | PL_reg_oldpos = 0; | |
11234 | PL_reg_oldcurpm = (PMOP*)NULL; | |
11235 | PL_reg_curpm = (PMOP*)NULL; | |
11236 | PL_reg_oldsaved = Nullch; | |
11237 | PL_reg_oldsavedlen = 0; | |
11238 | PL_reg_maxiter = 0; | |
11239 | PL_reg_leftiter = 0; | |
11240 | PL_reg_poscache = Nullch; | |
11241 | PL_reg_poscache_size= 0; | |
11242 | ||
11243 | /* RE engine - function pointers */ | |
11244 | PL_regcompp = proto_perl->Tregcompp; | |
11245 | PL_regexecp = proto_perl->Tregexecp; | |
11246 | PL_regint_start = proto_perl->Tregint_start; | |
11247 | PL_regint_string = proto_perl->Tregint_string; | |
11248 | PL_regfree = proto_perl->Tregfree; | |
11249 | ||
11250 | PL_reginterp_cnt = 0; | |
11251 | PL_reg_starttry = 0; | |
11252 | ||
a2efc822 SC |
11253 | /* Pluggable optimizer */ |
11254 | PL_peepp = proto_perl->Tpeepp; | |
11255 | ||
c0401c5d JH |
11256 | PL_stashcache = newHV(); |
11257 | ||
a0739874 DM |
11258 | if (!(flags & CLONEf_KEEP_PTR_TABLE)) { |
11259 | ptr_table_free(PL_ptr_table); | |
11260 | PL_ptr_table = NULL; | |
11261 | } | |
8cf8f3d1 | 11262 | |
f284b03f AMS |
11263 | /* Call the ->CLONE method, if it exists, for each of the stashes |
11264 | identified by sv_dup() above. | |
11265 | */ | |
d2d73c3e AB |
11266 | while(av_len(param->stashes) != -1) { |
11267 | HV* stash = (HV*) av_shift(param->stashes); | |
f284b03f AMS |
11268 | GV* cloner = gv_fetchmethod_autoload(stash, "CLONE", 0); |
11269 | if (cloner && GvCV(cloner)) { | |
11270 | dSP; | |
11271 | ENTER; | |
11272 | SAVETMPS; | |
11273 | PUSHMARK(SP); | |
dc507217 | 11274 | XPUSHs(sv_2mortal(newSVpv(HvNAME(stash), 0))); |
f284b03f AMS |
11275 | PUTBACK; |
11276 | call_sv((SV*)GvCV(cloner), G_DISCARD); | |
11277 | FREETMPS; | |
11278 | LEAVE; | |
11279 | } | |
4a09accc | 11280 | } |
a0739874 | 11281 | |
dc507217 | 11282 | SvREFCNT_dec(param->stashes); |
dc507217 | 11283 | |
1d7c1841 | 11284 | return my_perl; |
1d7c1841 GS |
11285 | } |
11286 | ||
1d7c1841 | 11287 | #endif /* USE_ITHREADS */ |
a0ae6670 | 11288 | |
9f4817db | 11289 | /* |
ccfc67b7 JH |
11290 | =head1 Unicode Support |
11291 | ||
9f4817db JH |
11292 | =for apidoc sv_recode_to_utf8 |
11293 | ||
5d170f3a JH |
11294 | The encoding is assumed to be an Encode object, on entry the PV |
11295 | of the sv is assumed to be octets in that encoding, and the sv | |
11296 | will be converted into Unicode (and UTF-8). | |
9f4817db | 11297 | |
5d170f3a JH |
11298 | If the sv already is UTF-8 (or if it is not POK), or if the encoding |
11299 | is not a reference, nothing is done to the sv. If the encoding is not | |
1768d7eb JH |
11300 | an C<Encode::XS> Encoding object, bad things will happen. |
11301 | (See F<lib/encoding.pm> and L<Encode>). | |
9f4817db | 11302 | |
5d170f3a | 11303 | The PV of the sv is returned. |
9f4817db | 11304 | |
5d170f3a JH |
11305 | =cut */ |
11306 | ||
11307 | char * | |
11308 | Perl_sv_recode_to_utf8(pTHX_ SV *sv, SV *encoding) | |
11309 | { | |
975adce1 | 11310 | if (SvPOK(sv) && !SvUTF8(sv) && !IN_BYTES && SvROK(encoding)) { |
5b7ea690 JH |
11311 | SV *uni; |
11312 | STRLEN len; | |
11313 | char *s; | |
11314 | dSP; | |
11315 | ENTER; | |
11316 | SAVETMPS; | |
975adce1 | 11317 | save_re_context(); |
5b7ea690 JH |
11318 | PUSHMARK(sp); |
11319 | EXTEND(SP, 3); | |
11320 | XPUSHs(encoding); | |
11321 | XPUSHs(sv); | |
f9893866 NIS |
11322 | /* |
11323 | NI-S 2002/07/09 | |
11324 | Passing sv_yes is wrong - it needs to be or'ed set of constants | |
11325 | for Encode::XS, while UTf-8 decode (currently) assumes a true value means | |
11326 | remove converted chars from source. | |
11327 | ||
11328 | Both will default the value - let them. | |
11329 | ||
5b7ea690 | 11330 | XPUSHs(&PL_sv_yes); |
f9893866 | 11331 | */ |
5b7ea690 JH |
11332 | PUTBACK; |
11333 | call_method("decode", G_SCALAR); | |
11334 | SPAGAIN; | |
11335 | uni = POPs; | |
11336 | PUTBACK; | |
11337 | s = SvPV(uni, len); | |
5b7ea690 JH |
11338 | if (s != SvPVX(sv)) { |
11339 | SvGROW(sv, len + 1); | |
11340 | Move(s, SvPVX(sv), len, char); | |
11341 | SvCUR_set(sv, len); | |
11342 | SvPVX(sv)[len] = 0; | |
11343 | } | |
11344 | FREETMPS; | |
11345 | LEAVE; | |
5b7ea690 | 11346 | SvUTF8_on(sv); |
f9893866 NIS |
11347 | } |
11348 | return SvPVX(sv); | |
9f4817db JH |
11349 | } |
11350 | ||
975adce1 JH |
11351 | /* |
11352 | =for apidoc sv_cat_decode | |
11353 | ||
11354 | The encoding is assumed to be an Encode object, the PV of the ssv is | |
11355 | assumed to be octets in that encoding and decoding the input starts | |
11356 | from the position which (PV + *offset) pointed to. The dsv will be | |
11357 | concatenated the decoded UTF-8 string from ssv. Decoding will terminate | |
11358 | when the string tstr appears in decoding output or the input ends on | |
11359 | the PV of the ssv. The value which the offset points will be modified | |
11360 | to the last input position on the ssv. | |
68795e93 | 11361 | |
975adce1 JH |
11362 | Returns TRUE if the terminator was found, else returns FALSE. |
11363 | ||
11364 | =cut */ | |
11365 | ||
11366 | bool | |
11367 | Perl_sv_cat_decode(pTHX_ SV *dsv, SV *encoding, | |
11368 | SV *ssv, int *offset, char *tstr, int tlen) | |
11369 | { | |
c04fee9e | 11370 | bool ret = FALSE; |
975adce1 | 11371 | if (SvPOK(ssv) && SvPOK(dsv) && SvROK(encoding) && offset) { |
975adce1 JH |
11372 | SV *offsv; |
11373 | dSP; | |
11374 | ENTER; | |
11375 | SAVETMPS; | |
11376 | save_re_context(); | |
11377 | PUSHMARK(sp); | |
11378 | EXTEND(SP, 6); | |
11379 | XPUSHs(encoding); | |
11380 | XPUSHs(dsv); | |
11381 | XPUSHs(ssv); | |
11382 | XPUSHs(offsv = sv_2mortal(newSViv(*offset))); | |
11383 | XPUSHs(sv_2mortal(newSVpvn(tstr, tlen))); | |
11384 | PUTBACK; | |
11385 | call_method("cat_decode", G_SCALAR); | |
11386 | SPAGAIN; | |
11387 | ret = SvTRUE(TOPs); | |
11388 | *offset = SvIV(offsv); | |
11389 | PUTBACK; | |
11390 | FREETMPS; | |
11391 | LEAVE; | |
975adce1 | 11392 | } |
c04fee9e JH |
11393 | else |
11394 | Perl_croak(aTHX_ "Invalid argument to sv_cat_decode"); | |
11395 | return ret; | |
975adce1 | 11396 | } |
f9893866 | 11397 |