Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | /* sv.c |
79072805 | 2 | * |
4bb101f2 | 3 | * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
241d1a3b | 4 | * 2000, 2001, 2002, 2003, 2004, 2005, by Larry Wall and others |
79072805 LW |
5 | * |
6 | * You may distribute under the terms of either the GNU General Public | |
7 | * License or the Artistic License, as specified in the README file. | |
8 | * | |
a0d0e21e | 9 | * "I wonder what the Entish is for 'yes' and 'no'," he thought. |
645c22ef DM |
10 | * |
11 | * | |
5e045b90 AMS |
12 | * This file contains the code that creates, manipulates and destroys |
13 | * scalar values (SVs). The other types (AV, HV, GV, etc.) reuse the | |
14 | * structure of an SV, so their creation and destruction is handled | |
15 | * here; higher-level functions are in av.c, hv.c, and so on. Opcode | |
16 | * level functions (eg. substr, split, join) for each of the types are | |
17 | * in the pp*.c files. | |
79072805 LW |
18 | */ |
19 | ||
20 | #include "EXTERN.h" | |
864dbfa3 | 21 | #define PERL_IN_SV_C |
79072805 | 22 | #include "perl.h" |
d2f185dc | 23 | #include "regcomp.h" |
79072805 | 24 | |
51371543 | 25 | #define FCALL *f |
2c5424a7 | 26 | |
2f8ed50e OS |
27 | #ifdef __Lynx__ |
28 | /* Missing proto on LynxOS */ | |
29 | char *gconvert(double, int, int, char *); | |
30 | #endif | |
31 | ||
e23c8137 JH |
32 | #ifdef PERL_UTF8_CACHE_ASSERT |
33 | /* The cache element 0 is the Unicode offset; | |
34 | * the cache element 1 is the byte offset of the element 0; | |
35 | * the cache element 2 is the Unicode length of the substring; | |
36 | * the cache element 3 is the byte length of the substring; | |
37 | * The checking of the substring side would be good | |
38 | * but substr() has enough code paths to make my head spin; | |
39 | * if adding more checks watch out for the following tests: | |
40 | * t/op/index.t t/op/length.t t/op/pat.t t/op/substr.t | |
41 | * lib/utf8.t lib/Unicode/Collate/t/index.t | |
42 | * --jhi | |
43 | */ | |
44 | #define ASSERT_UTF8_CACHE(cache) \ | |
45 | STMT_START { if (cache) { assert((cache)[0] <= (cache)[1]); } } STMT_END | |
46 | #else | |
47 | #define ASSERT_UTF8_CACHE(cache) NOOP | |
48 | #endif | |
49 | ||
f8c7b90f | 50 | #ifdef PERL_OLD_COPY_ON_WRITE |
765f542d | 51 | #define SV_COW_NEXT_SV(sv) INT2PTR(SV *,SvUVX(sv)) |
607fa7f2 | 52 | #define SV_COW_NEXT_SV_SET(current,next) SvUV_set(current, PTR2UV(next)) |
b5ccf5f2 | 53 | /* This is a pessimistic view. Scalar must be purely a read-write PV to copy- |
765f542d | 54 | on-write. */ |
765f542d | 55 | #endif |
645c22ef DM |
56 | |
57 | /* ============================================================================ | |
58 | ||
59 | =head1 Allocation and deallocation of SVs. | |
60 | ||
5e045b90 AMS |
61 | An SV (or AV, HV, etc.) is allocated in two parts: the head (struct sv, |
62 | av, hv...) contains type and reference count information, as well as a | |
63 | pointer to the body (struct xrv, xpv, xpviv...), which contains fields | |
64 | specific to each type. | |
65 | ||
4977e971 NC |
66 | Normally, this allocation is done using arenas, which by default are |
67 | approximately 4K chunks of memory parcelled up into N heads or bodies. The | |
68 | first slot in each arena is reserved, and is used to hold a link to the next | |
69 | arena. In the case of heads, the unused first slot also contains some flags | |
70 | and a note of the number of slots. Snaked through each arena chain is a | |
5e045b90 | 71 | linked list of free items; when this becomes empty, an extra arena is |
4977e971 | 72 | allocated and divided up into N items which are threaded into the free list. |
645c22ef DM |
73 | |
74 | The following global variables are associated with arenas: | |
75 | ||
76 | PL_sv_arenaroot pointer to list of SV arenas | |
77 | PL_sv_root pointer to list of free SV structures | |
78 | ||
79 | PL_foo_arenaroot pointer to list of foo arenas, | |
80 | PL_foo_root pointer to list of free foo bodies | |
81 | ... for foo in xiv, xnv, xrv, xpv etc. | |
82 | ||
83 | Note that some of the larger and more rarely used body types (eg xpvio) | |
84 | are not allocated using arenas, but are instead just malloc()/free()ed as | |
85 | required. Also, if PURIFY is defined, arenas are abandoned altogether, | |
86 | with all items individually malloc()ed. In addition, a few SV heads are | |
87 | not allocated from an arena, but are instead directly created as static | |
4977e971 NC |
88 | or auto variables, eg PL_sv_undef. The size of arenas can be changed from |
89 | the default by setting PERL_ARENA_SIZE appropriately at compile time. | |
645c22ef DM |
90 | |
91 | The SV arena serves the secondary purpose of allowing still-live SVs | |
92 | to be located and destroyed during final cleanup. | |
93 | ||
94 | At the lowest level, the macros new_SV() and del_SV() grab and free | |
95 | an SV head. (If debugging with -DD, del_SV() calls the function S_del_sv() | |
96 | to return the SV to the free list with error checking.) new_SV() calls | |
97 | more_sv() / sv_add_arena() to add an extra arena if the free list is empty. | |
98 | SVs in the free list have their SvTYPE field set to all ones. | |
99 | ||
100 | Similarly, there are macros new_XIV()/del_XIV(), new_XNV()/del_XNV() etc | |
101 | that allocate and return individual body types. Normally these are mapped | |
ff276b08 RG |
102 | to the arena-manipulating functions new_xiv()/del_xiv() etc, but may be |
103 | instead mapped directly to malloc()/free() if PURIFY is defined. The | |
645c22ef DM |
104 | new/del functions remove from, or add to, the appropriate PL_foo_root |
105 | list, and call more_xiv() etc to add a new arena if the list is empty. | |
106 | ||
ff276b08 | 107 | At the time of very final cleanup, sv_free_arenas() is called from |
645c22ef DM |
108 | perl_destruct() to physically free all the arenas allocated since the |
109 | start of the interpreter. Note that this also clears PL_he_arenaroot, | |
110 | which is otherwise dealt with in hv.c. | |
111 | ||
112 | Manipulation of any of the PL_*root pointers is protected by enclosing | |
113 | LOCK_SV_MUTEX; ... UNLOCK_SV_MUTEX calls which should Do the Right Thing | |
114 | if threads are enabled. | |
115 | ||
116 | The function visit() scans the SV arenas list, and calls a specified | |
117 | function for each SV it finds which is still live - ie which has an SvTYPE | |
118 | other than all 1's, and a non-zero SvREFCNT. visit() is used by the | |
119 | following functions (specified as [function that calls visit()] / [function | |
120 | called by visit() for each SV]): | |
121 | ||
122 | sv_report_used() / do_report_used() | |
123 | dump all remaining SVs (debugging aid) | |
124 | ||
125 | sv_clean_objs() / do_clean_objs(),do_clean_named_objs() | |
126 | Attempt to free all objects pointed to by RVs, | |
127 | and, unless DISABLE_DESTRUCTOR_KLUDGE is defined, | |
128 | try to do the same for all objects indirectly | |
129 | referenced by typeglobs too. Called once from | |
130 | perl_destruct(), prior to calling sv_clean_all() | |
131 | below. | |
132 | ||
133 | sv_clean_all() / do_clean_all() | |
134 | SvREFCNT_dec(sv) each remaining SV, possibly | |
135 | triggering an sv_free(). It also sets the | |
136 | SVf_BREAK flag on the SV to indicate that the | |
137 | refcnt has been artificially lowered, and thus | |
138 | stopping sv_free() from giving spurious warnings | |
139 | about SVs which unexpectedly have a refcnt | |
140 | of zero. called repeatedly from perl_destruct() | |
141 | until there are no SVs left. | |
142 | ||
143 | =head2 Summary | |
144 | ||
145 | Private API to rest of sv.c | |
146 | ||
147 | new_SV(), del_SV(), | |
148 | ||
149 | new_XIV(), del_XIV(), | |
150 | new_XNV(), del_XNV(), | |
151 | etc | |
152 | ||
153 | Public API: | |
154 | ||
8cf8f3d1 | 155 | sv_report_used(), sv_clean_objs(), sv_clean_all(), sv_free_arenas() |
645c22ef DM |
156 | |
157 | ||
158 | =cut | |
159 | ||
160 | ============================================================================ */ | |
161 | ||
162 | ||
51371543 | 163 | |
4561caa4 CS |
164 | /* |
165 | * "A time to plant, and a time to uproot what was planted..." | |
166 | */ | |
167 | ||
cac9b346 | 168 | |
fd0854ff DM |
169 | #ifdef DEBUG_LEAKING_SCALARS |
170 | # ifdef NETWARE | |
171 | # define FREE_SV_DEBUG_FILE(sv) PerlMemfree((sv)->sv_debug_file) | |
172 | # else | |
173 | # define FREE_SV_DEBUG_FILE(sv) PerlMemShared_free((sv)->sv_debug_file) | |
174 | # endif | |
175 | #else | |
176 | # define FREE_SV_DEBUG_FILE(sv) | |
177 | #endif | |
178 | ||
053fc874 GS |
179 | #define plant_SV(p) \ |
180 | STMT_START { \ | |
fd0854ff | 181 | FREE_SV_DEBUG_FILE(p); \ |
053fc874 GS |
182 | SvANY(p) = (void *)PL_sv_root; \ |
183 | SvFLAGS(p) = SVTYPEMASK; \ | |
184 | PL_sv_root = (p); \ | |
185 | --PL_sv_count; \ | |
186 | } STMT_END | |
a0d0e21e | 187 | |
fba3b22e | 188 | /* sv_mutex must be held while calling uproot_SV() */ |
053fc874 GS |
189 | #define uproot_SV(p) \ |
190 | STMT_START { \ | |
191 | (p) = PL_sv_root; \ | |
192 | PL_sv_root = (SV*)SvANY(p); \ | |
193 | ++PL_sv_count; \ | |
194 | } STMT_END | |
195 | ||
645c22ef | 196 | |
cac9b346 NC |
197 | /* make some more SVs by adding another arena */ |
198 | ||
199 | /* sv_mutex must be held while calling more_sv() */ | |
200 | STATIC SV* | |
201 | S_more_sv(pTHX) | |
202 | { | |
203 | SV* sv; | |
204 | ||
205 | if (PL_nice_chunk) { | |
206 | sv_add_arena(PL_nice_chunk, PL_nice_chunk_size, 0); | |
207 | PL_nice_chunk = Nullch; | |
208 | PL_nice_chunk_size = 0; | |
209 | } | |
210 | else { | |
211 | char *chunk; /* must use New here to match call to */ | |
2e7ed132 NC |
212 | New(704,chunk,PERL_ARENA_SIZE,char); /* Safefree() in sv_free_arenas() */ |
213 | sv_add_arena(chunk, PERL_ARENA_SIZE, 0); | |
cac9b346 NC |
214 | } |
215 | uproot_SV(sv); | |
216 | return sv; | |
217 | } | |
218 | ||
645c22ef DM |
219 | /* new_SV(): return a new, empty SV head */ |
220 | ||
eba0f806 DM |
221 | #ifdef DEBUG_LEAKING_SCALARS |
222 | /* provide a real function for a debugger to play with */ | |
223 | STATIC SV* | |
224 | S_new_SV(pTHX) | |
225 | { | |
226 | SV* sv; | |
227 | ||
228 | LOCK_SV_MUTEX; | |
229 | if (PL_sv_root) | |
230 | uproot_SV(sv); | |
231 | else | |
cac9b346 | 232 | sv = S_more_sv(aTHX); |
eba0f806 DM |
233 | UNLOCK_SV_MUTEX; |
234 | SvANY(sv) = 0; | |
235 | SvREFCNT(sv) = 1; | |
236 | SvFLAGS(sv) = 0; | |
fd0854ff DM |
237 | sv->sv_debug_optype = PL_op ? PL_op->op_type : 0; |
238 | sv->sv_debug_line = (U16) ((PL_copline == NOLINE) ? | |
239 | (PL_curcop ? CopLINE(PL_curcop) : 0) : PL_copline); | |
240 | sv->sv_debug_inpad = 0; | |
241 | sv->sv_debug_cloned = 0; | |
242 | # ifdef NETWARE | |
243 | sv->sv_debug_file = PL_curcop ? savepv(CopFILE(PL_curcop)): NULL; | |
244 | # else | |
245 | sv->sv_debug_file = PL_curcop ? savesharedpv(CopFILE(PL_curcop)): NULL; | |
246 | # endif | |
247 | ||
eba0f806 DM |
248 | return sv; |
249 | } | |
250 | # define new_SV(p) (p)=S_new_SV(aTHX) | |
251 | ||
252 | #else | |
253 | # define new_SV(p) \ | |
053fc874 GS |
254 | STMT_START { \ |
255 | LOCK_SV_MUTEX; \ | |
256 | if (PL_sv_root) \ | |
257 | uproot_SV(p); \ | |
258 | else \ | |
cac9b346 | 259 | (p) = S_more_sv(aTHX); \ |
053fc874 GS |
260 | UNLOCK_SV_MUTEX; \ |
261 | SvANY(p) = 0; \ | |
262 | SvREFCNT(p) = 1; \ | |
263 | SvFLAGS(p) = 0; \ | |
264 | } STMT_END | |
eba0f806 | 265 | #endif |
463ee0b2 | 266 | |
645c22ef DM |
267 | |
268 | /* del_SV(): return an empty SV head to the free list */ | |
269 | ||
a0d0e21e | 270 | #ifdef DEBUGGING |
4561caa4 | 271 | |
053fc874 GS |
272 | #define del_SV(p) \ |
273 | STMT_START { \ | |
274 | LOCK_SV_MUTEX; \ | |
aea4f609 | 275 | if (DEBUG_D_TEST) \ |
053fc874 GS |
276 | del_sv(p); \ |
277 | else \ | |
278 | plant_SV(p); \ | |
279 | UNLOCK_SV_MUTEX; \ | |
280 | } STMT_END | |
a0d0e21e | 281 | |
76e3520e | 282 | STATIC void |
cea2e8a9 | 283 | S_del_sv(pTHX_ SV *p) |
463ee0b2 | 284 | { |
aea4f609 | 285 | if (DEBUG_D_TEST) { |
4633a7c4 | 286 | SV* sva; |
a3b680e6 | 287 | bool ok = 0; |
3280af22 | 288 | for (sva = PL_sv_arenaroot; sva; sva = (SV *) SvANY(sva)) { |
53c1dcc0 AL |
289 | const SV * const sv = sva + 1; |
290 | const SV * const svend = &sva[SvREFCNT(sva)]; | |
c0ff570e | 291 | if (p >= sv && p < svend) { |
a0d0e21e | 292 | ok = 1; |
c0ff570e NC |
293 | break; |
294 | } | |
a0d0e21e LW |
295 | } |
296 | if (!ok) { | |
0453d815 | 297 | if (ckWARN_d(WARN_INTERNAL)) |
9014280d | 298 | Perl_warner(aTHX_ packWARN(WARN_INTERNAL), |
472d47bc SB |
299 | "Attempt to free non-arena SV: 0x%"UVxf |
300 | pTHX__FORMAT, PTR2UV(p) pTHX__VALUE); | |
a0d0e21e LW |
301 | return; |
302 | } | |
303 | } | |
4561caa4 | 304 | plant_SV(p); |
463ee0b2 | 305 | } |
a0d0e21e | 306 | |
4561caa4 CS |
307 | #else /* ! DEBUGGING */ |
308 | ||
309 | #define del_SV(p) plant_SV(p) | |
310 | ||
311 | #endif /* DEBUGGING */ | |
463ee0b2 | 312 | |
645c22ef DM |
313 | |
314 | /* | |
ccfc67b7 JH |
315 | =head1 SV Manipulation Functions |
316 | ||
645c22ef DM |
317 | =for apidoc sv_add_arena |
318 | ||
319 | Given a chunk of memory, link it to the head of the list of arenas, | |
320 | and split it into a list of free SVs. | |
321 | ||
322 | =cut | |
323 | */ | |
324 | ||
4633a7c4 | 325 | void |
864dbfa3 | 326 | Perl_sv_add_arena(pTHX_ char *ptr, U32 size, U32 flags) |
463ee0b2 | 327 | { |
4633a7c4 | 328 | SV* sva = (SV*)ptr; |
463ee0b2 LW |
329 | register SV* sv; |
330 | register SV* svend; | |
4633a7c4 LW |
331 | |
332 | /* The first SV in an arena isn't an SV. */ | |
3280af22 | 333 | SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */ |
4633a7c4 LW |
334 | SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */ |
335 | SvFLAGS(sva) = flags; /* FAKE if not to be freed */ | |
336 | ||
3280af22 NIS |
337 | PL_sv_arenaroot = sva; |
338 | PL_sv_root = sva + 1; | |
4633a7c4 LW |
339 | |
340 | svend = &sva[SvREFCNT(sva) - 1]; | |
341 | sv = sva + 1; | |
463ee0b2 | 342 | while (sv < svend) { |
a0d0e21e | 343 | SvANY(sv) = (void *)(SV*)(sv + 1); |
03e36789 | 344 | #ifdef DEBUGGING |
978b032e | 345 | SvREFCNT(sv) = 0; |
03e36789 NC |
346 | #endif |
347 | /* Must always set typemask because it's awlays checked in on cleanup | |
348 | when the arenas are walked looking for objects. */ | |
8990e307 | 349 | SvFLAGS(sv) = SVTYPEMASK; |
463ee0b2 LW |
350 | sv++; |
351 | } | |
352 | SvANY(sv) = 0; | |
03e36789 NC |
353 | #ifdef DEBUGGING |
354 | SvREFCNT(sv) = 0; | |
355 | #endif | |
4633a7c4 LW |
356 | SvFLAGS(sv) = SVTYPEMASK; |
357 | } | |
358 | ||
055972dc DM |
359 | /* visit(): call the named function for each non-free SV in the arenas |
360 | * whose flags field matches the flags/mask args. */ | |
645c22ef | 361 | |
5226ed68 | 362 | STATIC I32 |
055972dc | 363 | S_visit(pTHX_ SVFUNC_t f, U32 flags, U32 mask) |
8990e307 | 364 | { |
4633a7c4 | 365 | SV* sva; |
5226ed68 | 366 | I32 visited = 0; |
8990e307 | 367 | |
3280af22 | 368 | for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) { |
53c1dcc0 | 369 | register const SV * const svend = &sva[SvREFCNT(sva)]; |
a3b680e6 | 370 | register SV* sv; |
4561caa4 | 371 | for (sv = sva + 1; sv < svend; ++sv) { |
055972dc DM |
372 | if (SvTYPE(sv) != SVTYPEMASK |
373 | && (sv->sv_flags & mask) == flags | |
374 | && SvREFCNT(sv)) | |
375 | { | |
acfe0abc | 376 | (FCALL)(aTHX_ sv); |
5226ed68 JH |
377 | ++visited; |
378 | } | |
8990e307 LW |
379 | } |
380 | } | |
5226ed68 | 381 | return visited; |
8990e307 LW |
382 | } |
383 | ||
758a08c3 JH |
384 | #ifdef DEBUGGING |
385 | ||
645c22ef DM |
386 | /* called by sv_report_used() for each live SV */ |
387 | ||
388 | static void | |
acfe0abc | 389 | do_report_used(pTHX_ SV *sv) |
645c22ef DM |
390 | { |
391 | if (SvTYPE(sv) != SVTYPEMASK) { | |
392 | PerlIO_printf(Perl_debug_log, "****\n"); | |
393 | sv_dump(sv); | |
394 | } | |
395 | } | |
758a08c3 | 396 | #endif |
645c22ef DM |
397 | |
398 | /* | |
399 | =for apidoc sv_report_used | |
400 | ||
401 | Dump the contents of all SVs not yet freed. (Debugging aid). | |
402 | ||
403 | =cut | |
404 | */ | |
405 | ||
8990e307 | 406 | void |
864dbfa3 | 407 | Perl_sv_report_used(pTHX) |
4561caa4 | 408 | { |
ff270d3a | 409 | #ifdef DEBUGGING |
055972dc | 410 | visit(do_report_used, 0, 0); |
ff270d3a | 411 | #endif |
4561caa4 CS |
412 | } |
413 | ||
645c22ef DM |
414 | /* called by sv_clean_objs() for each live SV */ |
415 | ||
416 | static void | |
acfe0abc | 417 | do_clean_objs(pTHX_ SV *sv) |
645c22ef DM |
418 | { |
419 | SV* rv; | |
420 | ||
421 | if (SvROK(sv) && SvOBJECT(rv = SvRV(sv))) { | |
422 | DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(sv))); | |
423 | if (SvWEAKREF(sv)) { | |
424 | sv_del_backref(sv); | |
425 | SvWEAKREF_off(sv); | |
b162af07 | 426 | SvRV_set(sv, NULL); |
645c22ef DM |
427 | } else { |
428 | SvROK_off(sv); | |
b162af07 | 429 | SvRV_set(sv, NULL); |
645c22ef DM |
430 | SvREFCNT_dec(rv); |
431 | } | |
432 | } | |
433 | ||
434 | /* XXX Might want to check arrays, etc. */ | |
435 | } | |
436 | ||
437 | /* called by sv_clean_objs() for each live SV */ | |
438 | ||
439 | #ifndef DISABLE_DESTRUCTOR_KLUDGE | |
440 | static void | |
acfe0abc | 441 | do_clean_named_objs(pTHX_ SV *sv) |
645c22ef DM |
442 | { |
443 | if (SvTYPE(sv) == SVt_PVGV && GvGP(sv)) { | |
444 | if ( SvOBJECT(GvSV(sv)) || | |
445 | (GvAV(sv) && SvOBJECT(GvAV(sv))) || | |
446 | (GvHV(sv) && SvOBJECT(GvHV(sv))) || | |
447 | (GvIO(sv) && SvOBJECT(GvIO(sv))) || | |
448 | (GvCV(sv) && SvOBJECT(GvCV(sv))) ) | |
449 | { | |
450 | DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning named glob object:\n "), sv_dump(sv))); | |
ec5f3c78 | 451 | SvFLAGS(sv) |= SVf_BREAK; |
645c22ef DM |
452 | SvREFCNT_dec(sv); |
453 | } | |
454 | } | |
455 | } | |
456 | #endif | |
457 | ||
458 | /* | |
459 | =for apidoc sv_clean_objs | |
460 | ||
461 | Attempt to destroy all objects not yet freed | |
462 | ||
463 | =cut | |
464 | */ | |
465 | ||
4561caa4 | 466 | void |
864dbfa3 | 467 | Perl_sv_clean_objs(pTHX) |
4561caa4 | 468 | { |
3280af22 | 469 | PL_in_clean_objs = TRUE; |
055972dc | 470 | visit(do_clean_objs, SVf_ROK, SVf_ROK); |
4561caa4 | 471 | #ifndef DISABLE_DESTRUCTOR_KLUDGE |
2d0f3c12 | 472 | /* some barnacles may yet remain, clinging to typeglobs */ |
055972dc | 473 | visit(do_clean_named_objs, SVt_PVGV, SVTYPEMASK); |
4561caa4 | 474 | #endif |
3280af22 | 475 | PL_in_clean_objs = FALSE; |
4561caa4 CS |
476 | } |
477 | ||
645c22ef DM |
478 | /* called by sv_clean_all() for each live SV */ |
479 | ||
480 | static void | |
acfe0abc | 481 | do_clean_all(pTHX_ SV *sv) |
645c22ef DM |
482 | { |
483 | DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) )); | |
484 | SvFLAGS(sv) |= SVf_BREAK; | |
0e705b3b DM |
485 | if (PL_comppad == (AV*)sv) { |
486 | PL_comppad = Nullav; | |
487 | PL_curpad = Null(SV**); | |
488 | } | |
645c22ef DM |
489 | SvREFCNT_dec(sv); |
490 | } | |
491 | ||
492 | /* | |
493 | =for apidoc sv_clean_all | |
494 | ||
495 | Decrement the refcnt of each remaining SV, possibly triggering a | |
496 | cleanup. This function may have to be called multiple times to free | |
ff276b08 | 497 | SVs which are in complex self-referential hierarchies. |
645c22ef DM |
498 | |
499 | =cut | |
500 | */ | |
501 | ||
5226ed68 | 502 | I32 |
864dbfa3 | 503 | Perl_sv_clean_all(pTHX) |
8990e307 | 504 | { |
5226ed68 | 505 | I32 cleaned; |
3280af22 | 506 | PL_in_clean_all = TRUE; |
055972dc | 507 | cleaned = visit(do_clean_all, 0,0); |
3280af22 | 508 | PL_in_clean_all = FALSE; |
5226ed68 | 509 | return cleaned; |
8990e307 | 510 | } |
463ee0b2 | 511 | |
7cfef17e NC |
512 | static void |
513 | S_free_arena(pTHX_ void **root) { | |
514 | while (root) { | |
515 | void **next = *(void **)root; | |
516 | Safefree(root); | |
517 | root = next; | |
518 | } | |
519 | } | |
520 | ||
645c22ef DM |
521 | /* |
522 | =for apidoc sv_free_arenas | |
523 | ||
524 | Deallocate the memory used by all arenas. Note that all the individual SV | |
525 | heads and bodies within the arenas must already have been freed. | |
526 | ||
527 | =cut | |
528 | */ | |
529 | ||
7cfef17e NC |
530 | #define free_arena(name) \ |
531 | STMT_START { \ | |
532 | S_free_arena(aTHX_ (void**) PL_ ## name ## _arenaroot); \ | |
533 | PL_ ## name ## _arenaroot = 0; \ | |
534 | PL_ ## name ## _root = 0; \ | |
535 | } STMT_END | |
536 | ||
4633a7c4 | 537 | void |
864dbfa3 | 538 | Perl_sv_free_arenas(pTHX) |
4633a7c4 LW |
539 | { |
540 | SV* sva; | |
541 | SV* svanext; | |
542 | ||
543 | /* Free arenas here, but be careful about fake ones. (We assume | |
544 | contiguity of the fake ones with the corresponding real ones.) */ | |
545 | ||
3280af22 | 546 | for (sva = PL_sv_arenaroot; sva; sva = svanext) { |
4633a7c4 LW |
547 | svanext = (SV*) SvANY(sva); |
548 | while (svanext && SvFAKE(svanext)) | |
549 | svanext = (SV*) SvANY(svanext); | |
550 | ||
551 | if (!SvFAKE(sva)) | |
1df70142 | 552 | Safefree(sva); |
4633a7c4 | 553 | } |
8b4f5e17 | 554 | |
7cfef17e NC |
555 | free_arena(xnv); |
556 | free_arena(xpv); | |
557 | free_arena(xpviv); | |
558 | free_arena(xpvnv); | |
559 | free_arena(xpvcv); | |
560 | free_arena(xpvav); | |
561 | free_arena(xpvhv); | |
562 | free_arena(xpvmg); | |
563 | free_arena(xpvgv); | |
564 | free_arena(xpvlv); | |
565 | free_arena(xpvbm); | |
566 | free_arena(he); | |
567 | #if defined(USE_ITHREADS) | |
568 | free_arena(pte); | |
569 | #endif | |
612f20c3 | 570 | |
3280af22 NIS |
571 | if (PL_nice_chunk) |
572 | Safefree(PL_nice_chunk); | |
573 | PL_nice_chunk = Nullch; | |
574 | PL_nice_chunk_size = 0; | |
575 | PL_sv_arenaroot = 0; | |
576 | PL_sv_root = 0; | |
4633a7c4 LW |
577 | } |
578 | ||
29489e7c DM |
579 | /* --------------------------------------------------------------------- |
580 | * | |
581 | * support functions for report_uninit() | |
582 | */ | |
583 | ||
584 | /* the maxiumum size of array or hash where we will scan looking | |
585 | * for the undefined element that triggered the warning */ | |
586 | ||
587 | #define FUV_MAX_SEARCH_SIZE 1000 | |
588 | ||
589 | /* Look for an entry in the hash whose value has the same SV as val; | |
590 | * If so, return a mortal copy of the key. */ | |
591 | ||
592 | STATIC SV* | |
593 | S_find_hash_subscript(pTHX_ HV *hv, SV* val) | |
594 | { | |
27da23d5 | 595 | dVAR; |
29489e7c | 596 | register HE **array; |
29489e7c DM |
597 | I32 i; |
598 | ||
599 | if (!hv || SvMAGICAL(hv) || !HvARRAY(hv) || | |
600 | (HvTOTALKEYS(hv) > FUV_MAX_SEARCH_SIZE)) | |
601 | return Nullsv; | |
602 | ||
603 | array = HvARRAY(hv); | |
604 | ||
605 | for (i=HvMAX(hv); i>0; i--) { | |
f54cb97a | 606 | register HE *entry; |
29489e7c DM |
607 | for (entry = array[i]; entry; entry = HeNEXT(entry)) { |
608 | if (HeVAL(entry) != val) | |
609 | continue; | |
610 | if ( HeVAL(entry) == &PL_sv_undef || | |
611 | HeVAL(entry) == &PL_sv_placeholder) | |
612 | continue; | |
613 | if (!HeKEY(entry)) | |
614 | return Nullsv; | |
615 | if (HeKLEN(entry) == HEf_SVKEY) | |
616 | return sv_mortalcopy(HeKEY_sv(entry)); | |
617 | return sv_2mortal(newSVpvn(HeKEY(entry), HeKLEN(entry))); | |
618 | } | |
619 | } | |
620 | return Nullsv; | |
621 | } | |
622 | ||
623 | /* Look for an entry in the array whose value has the same SV as val; | |
624 | * If so, return the index, otherwise return -1. */ | |
625 | ||
626 | STATIC I32 | |
627 | S_find_array_subscript(pTHX_ AV *av, SV* val) | |
628 | { | |
629 | SV** svp; | |
630 | I32 i; | |
631 | if (!av || SvMAGICAL(av) || !AvARRAY(av) || | |
632 | (AvFILLp(av) > FUV_MAX_SEARCH_SIZE)) | |
633 | return -1; | |
634 | ||
635 | svp = AvARRAY(av); | |
636 | for (i=AvFILLp(av); i>=0; i--) { | |
637 | if (svp[i] == val && svp[i] != &PL_sv_undef) | |
638 | return i; | |
639 | } | |
640 | return -1; | |
641 | } | |
642 | ||
643 | /* S_varname(): return the name of a variable, optionally with a subscript. | |
644 | * If gv is non-zero, use the name of that global, along with gvtype (one | |
645 | * of "$", "@", "%"); otherwise use the name of the lexical at pad offset | |
646 | * targ. Depending on the value of the subscript_type flag, return: | |
647 | */ | |
648 | ||
649 | #define FUV_SUBSCRIPT_NONE 1 /* "@foo" */ | |
650 | #define FUV_SUBSCRIPT_ARRAY 2 /* "$foo[aindex]" */ | |
651 | #define FUV_SUBSCRIPT_HASH 3 /* "$foo{keyname}" */ | |
652 | #define FUV_SUBSCRIPT_WITHIN 4 /* "within @foo" */ | |
653 | ||
654 | STATIC SV* | |
bfed75c6 | 655 | S_varname(pTHX_ GV *gv, const char *gvtype, PADOFFSET targ, |
29489e7c DM |
656 | SV* keyname, I32 aindex, int subscript_type) |
657 | { | |
29489e7c | 658 | |
a3b680e6 | 659 | SV * const name = sv_newmortal(); |
29489e7c DM |
660 | if (gv) { |
661 | ||
662 | /* simulate gv_fullname4(), but add literal '^' for $^FOO names | |
663 | * XXX get rid of all this if gv_fullnameX() ever supports this | |
664 | * directly */ | |
665 | ||
bfed75c6 | 666 | const char *p; |
53c1dcc0 | 667 | HV * const hv = GvSTASH(gv); |
29489e7c DM |
668 | sv_setpv(name, gvtype); |
669 | if (!hv) | |
670 | p = "???"; | |
bfcb3514 | 671 | else if (!(p=HvNAME_get(hv))) |
29489e7c | 672 | p = "__ANON__"; |
29489e7c DM |
673 | if (strNE(p, "main")) { |
674 | sv_catpv(name,p); | |
675 | sv_catpvn(name,"::", 2); | |
676 | } | |
677 | if (GvNAMELEN(gv)>= 1 && | |
678 | ((unsigned int)*GvNAME(gv)) <= 26) | |
679 | { /* handle $^FOO */ | |
680 | Perl_sv_catpvf(aTHX_ name,"^%c", *GvNAME(gv) + 'A' - 1); | |
681 | sv_catpvn(name,GvNAME(gv)+1,GvNAMELEN(gv)-1); | |
682 | } | |
683 | else | |
684 | sv_catpvn(name,GvNAME(gv),GvNAMELEN(gv)); | |
685 | } | |
686 | else { | |
53c1dcc0 AL |
687 | U32 unused; |
688 | CV * const cv = find_runcv(&unused); | |
689 | SV *sv; | |
690 | AV *av; | |
691 | ||
29489e7c DM |
692 | if (!cv || !CvPADLIST(cv)) |
693 | return Nullsv;; | |
694 | av = (AV*)(*av_fetch(CvPADLIST(cv), 0, FALSE)); | |
695 | sv = *av_fetch(av, targ, FALSE); | |
696 | /* SvLEN in a pad name is not to be trusted */ | |
f9926b10 | 697 | sv_setpv(name, SvPV_nolen_const(sv)); |
29489e7c DM |
698 | } |
699 | ||
700 | if (subscript_type == FUV_SUBSCRIPT_HASH) { | |
53c1dcc0 | 701 | SV *sv; |
29489e7c DM |
702 | *SvPVX(name) = '$'; |
703 | sv = NEWSV(0,0); | |
704 | Perl_sv_catpvf(aTHX_ name, "{%s}", | |
3f7c398e | 705 | pv_display(sv,SvPVX_const(keyname), SvCUR(keyname), 0, 32)); |
29489e7c DM |
706 | SvREFCNT_dec(sv); |
707 | } | |
708 | else if (subscript_type == FUV_SUBSCRIPT_ARRAY) { | |
709 | *SvPVX(name) = '$'; | |
265a12b8 | 710 | Perl_sv_catpvf(aTHX_ name, "[%"IVdf"]", (IV)aindex); |
29489e7c DM |
711 | } |
712 | else if (subscript_type == FUV_SUBSCRIPT_WITHIN) | |
713 | sv_insert(name, 0, 0, "within ", 7); | |
714 | ||
715 | return name; | |
716 | } | |
717 | ||
718 | ||
719 | /* | |
720 | =for apidoc find_uninit_var | |
721 | ||
722 | Find the name of the undefined variable (if any) that caused the operator o | |
723 | to issue a "Use of uninitialized value" warning. | |
724 | If match is true, only return a name if it's value matches uninit_sv. | |
725 | So roughly speaking, if a unary operator (such as OP_COS) generates a | |
726 | warning, then following the direct child of the op may yield an | |
727 | OP_PADSV or OP_GV that gives the name of the undefined variable. On the | |
728 | other hand, with OP_ADD there are two branches to follow, so we only print | |
729 | the variable name if we get an exact match. | |
730 | ||
731 | The name is returned as a mortal SV. | |
732 | ||
733 | Assumes that PL_op is the op that originally triggered the error, and that | |
734 | PL_comppad/PL_curpad points to the currently executing pad. | |
735 | ||
736 | =cut | |
737 | */ | |
738 | ||
739 | STATIC SV * | |
740 | S_find_uninit_var(pTHX_ OP* obase, SV* uninit_sv, bool match) | |
741 | { | |
27da23d5 | 742 | dVAR; |
29489e7c DM |
743 | SV *sv; |
744 | AV *av; | |
745 | SV **svp; | |
746 | GV *gv; | |
747 | OP *o, *o2, *kid; | |
748 | ||
749 | if (!obase || (match && (!uninit_sv || uninit_sv == &PL_sv_undef || | |
750 | uninit_sv == &PL_sv_placeholder))) | |
751 | return Nullsv; | |
752 | ||
753 | switch (obase->op_type) { | |
754 | ||
755 | case OP_RV2AV: | |
756 | case OP_RV2HV: | |
757 | case OP_PADAV: | |
758 | case OP_PADHV: | |
759 | { | |
f54cb97a AL |
760 | const bool pad = (obase->op_type == OP_PADAV || obase->op_type == OP_PADHV); |
761 | const bool hash = (obase->op_type == OP_PADHV || obase->op_type == OP_RV2HV); | |
112dcc46 RGS |
762 | I32 index = 0; |
763 | SV *keysv = Nullsv; | |
29489e7c DM |
764 | int subscript_type = FUV_SUBSCRIPT_WITHIN; |
765 | ||
766 | if (pad) { /* @lex, %lex */ | |
767 | sv = PAD_SVl(obase->op_targ); | |
768 | gv = Nullgv; | |
769 | } | |
770 | else { | |
771 | if (cUNOPx(obase)->op_first->op_type == OP_GV) { | |
772 | /* @global, %global */ | |
773 | gv = cGVOPx_gv(cUNOPx(obase)->op_first); | |
774 | if (!gv) | |
775 | break; | |
776 | sv = hash ? (SV*)GvHV(gv): (SV*)GvAV(gv); | |
777 | } | |
778 | else /* @{expr}, %{expr} */ | |
779 | return find_uninit_var(cUNOPx(obase)->op_first, | |
780 | uninit_sv, match); | |
781 | } | |
782 | ||
783 | /* attempt to find a match within the aggregate */ | |
784 | if (hash) { | |
785 | keysv = S_find_hash_subscript(aTHX_ (HV*)sv, uninit_sv); | |
786 | if (keysv) | |
787 | subscript_type = FUV_SUBSCRIPT_HASH; | |
788 | } | |
789 | else { | |
790 | index = S_find_array_subscript(aTHX_ (AV*)sv, uninit_sv); | |
791 | if (index >= 0) | |
792 | subscript_type = FUV_SUBSCRIPT_ARRAY; | |
793 | } | |
794 | ||
795 | if (match && subscript_type == FUV_SUBSCRIPT_WITHIN) | |
796 | break; | |
797 | ||
798 | return S_varname(aTHX_ gv, hash ? "%" : "@", obase->op_targ, | |
799 | keysv, index, subscript_type); | |
800 | } | |
801 | ||
802 | case OP_PADSV: | |
803 | if (match && PAD_SVl(obase->op_targ) != uninit_sv) | |
804 | break; | |
805 | return S_varname(aTHX_ Nullgv, "$", obase->op_targ, | |
806 | Nullsv, 0, FUV_SUBSCRIPT_NONE); | |
807 | ||
808 | case OP_GVSV: | |
809 | gv = cGVOPx_gv(obase); | |
810 | if (!gv || (match && GvSV(gv) != uninit_sv)) | |
811 | break; | |
812 | return S_varname(aTHX_ gv, "$", 0, Nullsv, 0, FUV_SUBSCRIPT_NONE); | |
813 | ||
814 | case OP_AELEMFAST: | |
815 | if (obase->op_flags & OPf_SPECIAL) { /* lexical array */ | |
816 | if (match) { | |
817 | av = (AV*)PAD_SV(obase->op_targ); | |
818 | if (!av || SvRMAGICAL(av)) | |
819 | break; | |
820 | svp = av_fetch(av, (I32)obase->op_private, FALSE); | |
821 | if (!svp || *svp != uninit_sv) | |
822 | break; | |
823 | } | |
824 | return S_varname(aTHX_ Nullgv, "$", obase->op_targ, | |
825 | Nullsv, (I32)obase->op_private, FUV_SUBSCRIPT_ARRAY); | |
826 | } | |
827 | else { | |
828 | gv = cGVOPx_gv(obase); | |
829 | if (!gv) | |
830 | break; | |
831 | if (match) { | |
832 | av = GvAV(gv); | |
833 | if (!av || SvRMAGICAL(av)) | |
834 | break; | |
835 | svp = av_fetch(av, (I32)obase->op_private, FALSE); | |
836 | if (!svp || *svp != uninit_sv) | |
837 | break; | |
838 | } | |
839 | return S_varname(aTHX_ gv, "$", 0, | |
840 | Nullsv, (I32)obase->op_private, FUV_SUBSCRIPT_ARRAY); | |
841 | } | |
842 | break; | |
843 | ||
844 | case OP_EXISTS: | |
845 | o = cUNOPx(obase)->op_first; | |
846 | if (!o || o->op_type != OP_NULL || | |
847 | ! (o->op_targ == OP_AELEM || o->op_targ == OP_HELEM)) | |
848 | break; | |
849 | return find_uninit_var(cBINOPo->op_last, uninit_sv, match); | |
850 | ||
851 | case OP_AELEM: | |
852 | case OP_HELEM: | |
853 | if (PL_op == obase) | |
854 | /* $a[uninit_expr] or $h{uninit_expr} */ | |
855 | return find_uninit_var(cBINOPx(obase)->op_last, uninit_sv, match); | |
856 | ||
857 | gv = Nullgv; | |
858 | o = cBINOPx(obase)->op_first; | |
859 | kid = cBINOPx(obase)->op_last; | |
860 | ||
861 | /* get the av or hv, and optionally the gv */ | |
862 | sv = Nullsv; | |
863 | if (o->op_type == OP_PADAV || o->op_type == OP_PADHV) { | |
864 | sv = PAD_SV(o->op_targ); | |
865 | } | |
866 | else if ((o->op_type == OP_RV2AV || o->op_type == OP_RV2HV) | |
867 | && cUNOPo->op_first->op_type == OP_GV) | |
868 | { | |
869 | gv = cGVOPx_gv(cUNOPo->op_first); | |
870 | if (!gv) | |
871 | break; | |
872 | sv = o->op_type == OP_RV2HV ? (SV*)GvHV(gv) : (SV*)GvAV(gv); | |
873 | } | |
874 | if (!sv) | |
875 | break; | |
876 | ||
877 | if (kid && kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid))) { | |
878 | /* index is constant */ | |
879 | if (match) { | |
880 | if (SvMAGICAL(sv)) | |
881 | break; | |
882 | if (obase->op_type == OP_HELEM) { | |
883 | HE* he = hv_fetch_ent((HV*)sv, cSVOPx_sv(kid), 0, 0); | |
884 | if (!he || HeVAL(he) != uninit_sv) | |
885 | break; | |
886 | } | |
887 | else { | |
888 | svp = av_fetch((AV*)sv, SvIV(cSVOPx_sv(kid)), FALSE); | |
889 | if (!svp || *svp != uninit_sv) | |
890 | break; | |
891 | } | |
892 | } | |
893 | if (obase->op_type == OP_HELEM) | |
894 | return S_varname(aTHX_ gv, "%", o->op_targ, | |
895 | cSVOPx_sv(kid), 0, FUV_SUBSCRIPT_HASH); | |
896 | else | |
897 | return S_varname(aTHX_ gv, "@", o->op_targ, Nullsv, | |
898 | SvIV(cSVOPx_sv(kid)), FUV_SUBSCRIPT_ARRAY); | |
899 | ; | |
900 | } | |
901 | else { | |
902 | /* index is an expression; | |
903 | * attempt to find a match within the aggregate */ | |
904 | if (obase->op_type == OP_HELEM) { | |
53c1dcc0 | 905 | SV * const keysv = S_find_hash_subscript(aTHX_ (HV*)sv, uninit_sv); |
29489e7c DM |
906 | if (keysv) |
907 | return S_varname(aTHX_ gv, "%", o->op_targ, | |
908 | keysv, 0, FUV_SUBSCRIPT_HASH); | |
909 | } | |
910 | else { | |
f54cb97a | 911 | const I32 index = S_find_array_subscript(aTHX_ (AV*)sv, uninit_sv); |
29489e7c | 912 | if (index >= 0) |
f54cb97a | 913 | return S_varname(aTHX_ gv, "@", o->op_targ, |
29489e7c DM |
914 | Nullsv, index, FUV_SUBSCRIPT_ARRAY); |
915 | } | |
916 | if (match) | |
917 | break; | |
918 | return S_varname(aTHX_ gv, | |
919 | (o->op_type == OP_PADAV || o->op_type == OP_RV2AV) | |
920 | ? "@" : "%", | |
921 | o->op_targ, Nullsv, 0, FUV_SUBSCRIPT_WITHIN); | |
922 | } | |
923 | ||
924 | break; | |
925 | ||
926 | case OP_AASSIGN: | |
927 | /* only examine RHS */ | |
928 | return find_uninit_var(cBINOPx(obase)->op_first, uninit_sv, match); | |
929 | ||
930 | case OP_OPEN: | |
931 | o = cUNOPx(obase)->op_first; | |
932 | if (o->op_type == OP_PUSHMARK) | |
933 | o = o->op_sibling; | |
934 | ||
935 | if (!o->op_sibling) { | |
936 | /* one-arg version of open is highly magical */ | |
937 | ||
938 | if (o->op_type == OP_GV) { /* open FOO; */ | |
939 | gv = cGVOPx_gv(o); | |
940 | if (match && GvSV(gv) != uninit_sv) | |
941 | break; | |
7a5fa8a2 | 942 | return S_varname(aTHX_ gv, "$", 0, |
29489e7c DM |
943 | Nullsv, 0, FUV_SUBSCRIPT_NONE); |
944 | } | |
945 | /* other possibilities not handled are: | |
946 | * open $x; or open my $x; should return '${*$x}' | |
947 | * open expr; should return '$'.expr ideally | |
948 | */ | |
949 | break; | |
950 | } | |
951 | goto do_op; | |
952 | ||
953 | /* ops where $_ may be an implicit arg */ | |
954 | case OP_TRANS: | |
955 | case OP_SUBST: | |
956 | case OP_MATCH: | |
957 | if ( !(obase->op_flags & OPf_STACKED)) { | |
958 | if (uninit_sv == ((obase->op_private & OPpTARGET_MY) | |
959 | ? PAD_SVl(obase->op_targ) | |
960 | : DEFSV)) | |
961 | { | |
962 | sv = sv_newmortal(); | |
616d8c9c | 963 | sv_setpvn(sv, "$_", 2); |
29489e7c DM |
964 | return sv; |
965 | } | |
966 | } | |
967 | goto do_op; | |
968 | ||
969 | case OP_PRTF: | |
970 | case OP_PRINT: | |
971 | /* skip filehandle as it can't produce 'undef' warning */ | |
972 | o = cUNOPx(obase)->op_first; | |
973 | if ((obase->op_flags & OPf_STACKED) && o->op_type == OP_PUSHMARK) | |
974 | o = o->op_sibling->op_sibling; | |
975 | goto do_op2; | |
976 | ||
977 | ||
e21bd382 | 978 | case OP_RV2SV: |
29489e7c DM |
979 | case OP_CUSTOM: |
980 | case OP_ENTERSUB: | |
981 | match = 1; /* XS or custom code could trigger random warnings */ | |
982 | goto do_op; | |
983 | ||
984 | case OP_SCHOMP: | |
985 | case OP_CHOMP: | |
986 | if (SvROK(PL_rs) && uninit_sv == SvRV(PL_rs)) | |
987 | return sv_2mortal(newSVpv("${$/}", 0)); | |
988 | /* FALL THROUGH */ | |
989 | ||
990 | default: | |
991 | do_op: | |
992 | if (!(obase->op_flags & OPf_KIDS)) | |
993 | break; | |
994 | o = cUNOPx(obase)->op_first; | |
995 | ||
996 | do_op2: | |
997 | if (!o) | |
998 | break; | |
999 | ||
1000 | /* if all except one arg are constant, or have no side-effects, | |
1001 | * or are optimized away, then it's unambiguous */ | |
1002 | o2 = Nullop; | |
1003 | for (kid=o; kid; kid = kid->op_sibling) { | |
1004 | if (kid && | |
1005 | ( (kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid))) | |
1006 | || (kid->op_type == OP_NULL && ! (kid->op_flags & OPf_KIDS)) | |
1007 | || (kid->op_type == OP_PUSHMARK) | |
1008 | ) | |
1009 | ) | |
1010 | continue; | |
1011 | if (o2) { /* more than one found */ | |
1012 | o2 = Nullop; | |
1013 | break; | |
1014 | } | |
1015 | o2 = kid; | |
1016 | } | |
1017 | if (o2) | |
1018 | return find_uninit_var(o2, uninit_sv, match); | |
1019 | ||
1020 | /* scan all args */ | |
1021 | while (o) { | |
1022 | sv = find_uninit_var(o, uninit_sv, 1); | |
1023 | if (sv) | |
1024 | return sv; | |
1025 | o = o->op_sibling; | |
1026 | } | |
1027 | break; | |
1028 | } | |
1029 | return Nullsv; | |
1030 | } | |
1031 | ||
1032 | ||
645c22ef DM |
1033 | /* |
1034 | =for apidoc report_uninit | |
1035 | ||
1036 | Print appropriate "Use of uninitialized variable" warning | |
1037 | ||
1038 | =cut | |
1039 | */ | |
1040 | ||
1d7c1841 | 1041 | void |
29489e7c DM |
1042 | Perl_report_uninit(pTHX_ SV* uninit_sv) |
1043 | { | |
1044 | if (PL_op) { | |
112dcc46 | 1045 | SV* varname = Nullsv; |
29489e7c DM |
1046 | if (uninit_sv) { |
1047 | varname = find_uninit_var(PL_op, uninit_sv,0); | |
1048 | if (varname) | |
1049 | sv_insert(varname, 0, 0, " ", 1); | |
1050 | } | |
9014280d | 1051 | Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit, |
93524f2b | 1052 | varname ? SvPV_nolen_const(varname) : "", |
29489e7c DM |
1053 | " in ", OP_DESC(PL_op)); |
1054 | } | |
1d7c1841 | 1055 | else |
29489e7c DM |
1056 | Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit, |
1057 | "", "", ""); | |
1d7c1841 GS |
1058 | } |
1059 | ||
de042e1d | 1060 | STATIC void * |
e3bbdc67 | 1061 | S_more_bodies (pTHX_ void **arena_root, void **root, size_t size) |
cac9b346 | 1062 | { |
e3bbdc67 NC |
1063 | char *start; |
1064 | const char *end; | |
53c1dcc0 | 1065 | const size_t count = PERL_ARENA_SIZE/size; |
e3bbdc67 NC |
1066 | New(0, start, count*size, char); |
1067 | *((void **) start) = *arena_root; | |
1068 | *arena_root = (void *)start; | |
cac9b346 | 1069 | |
e3bbdc67 | 1070 | end = start + (count-1) * size; |
cac9b346 | 1071 | |
e3bbdc67 NC |
1072 | /* The initial slot is used to link the arenas together, so it isn't to be |
1073 | linked into the list of ready-to-use bodies. */ | |
cac9b346 | 1074 | |
e3bbdc67 | 1075 | start += size; |
cac9b346 | 1076 | |
e3bbdc67 | 1077 | *root = (void *)start; |
cac9b346 | 1078 | |
e3bbdc67 | 1079 | while (start < end) { |
53c1dcc0 | 1080 | char * const next = start + size; |
e3bbdc67 NC |
1081 | *(void**) start = (void *)next; |
1082 | start = next; | |
cac9b346 | 1083 | } |
e3bbdc67 | 1084 | *(void **)start = 0; |
de042e1d NC |
1085 | |
1086 | return *root; | |
cac9b346 NC |
1087 | } |
1088 | ||
aeb18a1e | 1089 | /* grab a new thing from the free list, allocating more if necessary */ |
645c22ef | 1090 | |
aeb18a1e | 1091 | STATIC void * |
dd690478 | 1092 | S_new_body(pTHX_ void **arena_root, void **root, size_t size) |
932e9ff9 | 1093 | { |
aeb18a1e | 1094 | void *xpv; |
932e9ff9 | 1095 | LOCK_SV_MUTEX; |
aeb18a1e NC |
1096 | xpv = *root ? *root : S_more_bodies(aTHX_ arena_root, root, size); |
1097 | *root = *(void**)xpv; | |
932e9ff9 | 1098 | UNLOCK_SV_MUTEX; |
dd690478 | 1099 | return xpv; |
932e9ff9 VB |
1100 | } |
1101 | ||
aeb18a1e | 1102 | /* return a thing to the free list */ |
645c22ef | 1103 | |
cb4415b8 NC |
1104 | #define del_body(thing, root) \ |
1105 | STMT_START { \ | |
1106 | LOCK_SV_MUTEX; \ | |
1107 | *(void **)thing = *root; \ | |
1108 | *root = (void*)thing; \ | |
1109 | UNLOCK_SV_MUTEX; \ | |
1110 | } STMT_END | |
932e9ff9 | 1111 | |
aeb18a1e NC |
1112 | /* Conventionally we simply malloc() a big block of memory, then divide it |
1113 | up into lots of the thing that we're allocating. | |
645c22ef | 1114 | |
aeb18a1e NC |
1115 | This macro will expand to call to S_new_body. So for XPVBM (with ithreads), |
1116 | it would become | |
932e9ff9 | 1117 | |
aeb18a1e NC |
1118 | S_new_body(my_perl, (void**)&(my_perl->Ixpvbm_arenaroot), |
1119 | (void**)&(my_perl->Ixpvbm_root), sizeof(XPVBM), 0) | |
1120 | */ | |
645c22ef | 1121 | |
aeb18a1e NC |
1122 | #define new_body(TYPE,lctype) \ |
1123 | S_new_body(aTHX_ (void**)&PL_ ## lctype ## _arenaroot, \ | |
1124 | (void**)&PL_ ## lctype ## _root, \ | |
dd690478 NC |
1125 | sizeof(TYPE)) |
1126 | ||
cb4415b8 NC |
1127 | #define del_body_type(p,TYPE,lctype) \ |
1128 | del_body((void*)p, (void**)&PL_ ## lctype ## _root) | |
aeb18a1e NC |
1129 | |
1130 | /* But for some types, we cheat. The type starts with some members that are | |
1131 | never accessed. So we allocate the substructure, starting at the first used | |
1132 | member, then adjust the pointer back in memory by the size of the bit not | |
1133 | allocated, so it's as if we allocated the full structure. | |
1134 | (But things will all go boom if you write to the part that is "not there", | |
1135 | because you'll be overwriting the last members of the preceding structure | |
1136 | in memory.) | |
1137 | ||
1138 | We calculate the correction using the STRUCT_OFFSET macro. For example, if | |
1139 | xpv_allocated is the same structure as XPV then the two OFFSETs sum to zero, | |
1140 | and the pointer is unchanged. If the allocated structure is smaller (no | |
1141 | initial NV actually allocated) then the net effect is to subtract the size | |
1142 | of the NV from the pointer, to return a new pointer as if an initial NV were | |
1143 | actually allocated. | |
1144 | ||
1145 | This is the same trick as was used for NV and IV bodies. Ironically it | |
1146 | doesn't need to be used for NV bodies any more, because NV is now at the | |
1147 | start of the structure. IV bodies don't need it either, because they are | |
1148 | no longer allocated. */ | |
1149 | ||
1150 | #define new_body_allocated(TYPE,lctype,member) \ | |
dd690478 NC |
1151 | (void*)((char*)S_new_body(aTHX_ (void**)&PL_ ## lctype ## _arenaroot, \ |
1152 | (void**)&PL_ ## lctype ## _root, \ | |
1153 | sizeof(lctype ## _allocated)) - \ | |
1154 | STRUCT_OFFSET(TYPE, member) \ | |
1155 | + STRUCT_OFFSET(lctype ## _allocated, member)) | |
aeb18a1e NC |
1156 | |
1157 | ||
aeb18a1e | 1158 | #define del_body_allocated(p,TYPE,lctype,member) \ |
cb4415b8 NC |
1159 | del_body((void*)((char*)p + STRUCT_OFFSET(TYPE, member) \ |
1160 | - STRUCT_OFFSET(lctype ## _allocated, member)), \ | |
1161 | (void**)&PL_ ## lctype ## _root) | |
932e9ff9 | 1162 | |
7bab3ede MB |
1163 | #define my_safemalloc(s) (void*)safemalloc(s) |
1164 | #define my_safefree(p) safefree((char*)p) | |
463ee0b2 | 1165 | |
d33b2eba | 1166 | #ifdef PURIFY |
463ee0b2 | 1167 | |
d33b2eba GS |
1168 | #define new_XNV() my_safemalloc(sizeof(XPVNV)) |
1169 | #define del_XNV(p) my_safefree(p) | |
463ee0b2 | 1170 | |
d33b2eba GS |
1171 | #define new_XPV() my_safemalloc(sizeof(XPV)) |
1172 | #define del_XPV(p) my_safefree(p) | |
9b94d1dd | 1173 | |
d33b2eba GS |
1174 | #define new_XPVIV() my_safemalloc(sizeof(XPVIV)) |
1175 | #define del_XPVIV(p) my_safefree(p) | |
932e9ff9 | 1176 | |
d33b2eba GS |
1177 | #define new_XPVNV() my_safemalloc(sizeof(XPVNV)) |
1178 | #define del_XPVNV(p) my_safefree(p) | |
932e9ff9 | 1179 | |
d33b2eba GS |
1180 | #define new_XPVCV() my_safemalloc(sizeof(XPVCV)) |
1181 | #define del_XPVCV(p) my_safefree(p) | |
932e9ff9 | 1182 | |
d33b2eba GS |
1183 | #define new_XPVAV() my_safemalloc(sizeof(XPVAV)) |
1184 | #define del_XPVAV(p) my_safefree(p) | |
1185 | ||
1186 | #define new_XPVHV() my_safemalloc(sizeof(XPVHV)) | |
1187 | #define del_XPVHV(p) my_safefree(p) | |
1c846c1f | 1188 | |
d33b2eba GS |
1189 | #define new_XPVMG() my_safemalloc(sizeof(XPVMG)) |
1190 | #define del_XPVMG(p) my_safefree(p) | |
1191 | ||
727879eb NC |
1192 | #define new_XPVGV() my_safemalloc(sizeof(XPVGV)) |
1193 | #define del_XPVGV(p) my_safefree(p) | |
1194 | ||
d33b2eba GS |
1195 | #define new_XPVLV() my_safemalloc(sizeof(XPVLV)) |
1196 | #define del_XPVLV(p) my_safefree(p) | |
1197 | ||
1198 | #define new_XPVBM() my_safemalloc(sizeof(XPVBM)) | |
1199 | #define del_XPVBM(p) my_safefree(p) | |
1200 | ||
1201 | #else /* !PURIFY */ | |
1202 | ||
aeb18a1e | 1203 | #define new_XNV() new_body(NV, xnv) |
cb4415b8 | 1204 | #define del_XNV(p) del_body_type(p, NV, xnv) |
9b94d1dd | 1205 | |
aeb18a1e NC |
1206 | #define new_XPV() new_body_allocated(XPV, xpv, xpv_cur) |
1207 | #define del_XPV(p) del_body_allocated(p, XPV, xpv, xpv_cur) | |
d33b2eba | 1208 | |
aeb18a1e NC |
1209 | #define new_XPVIV() new_body_allocated(XPVIV, xpviv, xpv_cur) |
1210 | #define del_XPVIV(p) del_body_allocated(p, XPVIV, xpviv, xpv_cur) | |
d33b2eba | 1211 | |
aeb18a1e | 1212 | #define new_XPVNV() new_body(XPVNV, xpvnv) |
cb4415b8 | 1213 | #define del_XPVNV(p) del_body_type(p, XPVNV, xpvnv) |
d33b2eba | 1214 | |
aeb18a1e | 1215 | #define new_XPVCV() new_body(XPVCV, xpvcv) |
cb4415b8 | 1216 | #define del_XPVCV(p) del_body_type(p, XPVCV, xpvcv) |
d33b2eba | 1217 | |
aeb18a1e NC |
1218 | #define new_XPVAV() new_body_allocated(XPVAV, xpvav, xav_fill) |
1219 | #define del_XPVAV(p) del_body_allocated(p, XPVAV, xpvav, xav_fill) | |
d33b2eba | 1220 | |
aeb18a1e NC |
1221 | #define new_XPVHV() new_body_allocated(XPVHV, xpvhv, xhv_fill) |
1222 | #define del_XPVHV(p) del_body_allocated(p, XPVHV, xpvhv, xhv_fill) | |
1c846c1f | 1223 | |
aeb18a1e | 1224 | #define new_XPVMG() new_body(XPVMG, xpvmg) |
cb4415b8 | 1225 | #define del_XPVMG(p) del_body_type(p, XPVMG, xpvmg) |
d33b2eba | 1226 | |
aeb18a1e | 1227 | #define new_XPVGV() new_body(XPVGV, xpvgv) |
cb4415b8 | 1228 | #define del_XPVGV(p) del_body_type(p, XPVGV, xpvgv) |
727879eb | 1229 | |
aeb18a1e | 1230 | #define new_XPVLV() new_body(XPVLV, xpvlv) |
cb4415b8 | 1231 | #define del_XPVLV(p) del_body_type(p, XPVLV, xpvlv) |
d33b2eba | 1232 | |
aeb18a1e | 1233 | #define new_XPVBM() new_body(XPVBM, xpvbm) |
cb4415b8 | 1234 | #define del_XPVBM(p) del_body_type(p, XPVBM, xpvbm) |
d33b2eba GS |
1235 | |
1236 | #endif /* PURIFY */ | |
9b94d1dd | 1237 | |
d33b2eba GS |
1238 | #define new_XPVFM() my_safemalloc(sizeof(XPVFM)) |
1239 | #define del_XPVFM(p) my_safefree(p) | |
1c846c1f | 1240 | |
d33b2eba GS |
1241 | #define new_XPVIO() my_safemalloc(sizeof(XPVIO)) |
1242 | #define del_XPVIO(p) my_safefree(p) | |
8990e307 | 1243 | |
954c1994 GS |
1244 | /* |
1245 | =for apidoc sv_upgrade | |
1246 | ||
ff276b08 | 1247 | Upgrade an SV to a more complex form. Generally adds a new body type to the |
645c22ef | 1248 | SV, then copies across as much information as possible from the old body. |
ff276b08 | 1249 | You generally want to use the C<SvUPGRADE> macro wrapper. See also C<svtype>. |
954c1994 GS |
1250 | |
1251 | =cut | |
1252 | */ | |
1253 | ||
63f97190 | 1254 | void |
864dbfa3 | 1255 | Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt) |
79072805 | 1256 | { |
9a085840 | 1257 | void** old_body_arena; |
878cc751 | 1258 | size_t old_body_offset; |
4cbc76b1 | 1259 | size_t old_body_length; /* Well, the length to copy. */ |
878cc751 | 1260 | void* old_body; |
16b305e3 NC |
1261 | #ifndef NV_ZERO_IS_ALLBITS_ZERO |
1262 | /* If NV 0.0 is store as all bits 0 then Zero() already creates a correct | |
1263 | 0.0 for us. */ | |
4cbc76b1 | 1264 | bool zero_nv = TRUE; |
16b305e3 | 1265 | #endif |
403d36eb NC |
1266 | void* new_body; |
1267 | size_t new_body_length; | |
1268 | size_t new_body_offset; | |
1269 | void** new_body_arena; | |
1270 | void** new_body_arenaroot; | |
53c1dcc0 | 1271 | const U32 old_type = SvTYPE(sv); |
79072805 | 1272 | |
765f542d NC |
1273 | if (mt != SVt_PV && SvIsCOW(sv)) { |
1274 | sv_force_normal_flags(sv, 0); | |
f130fd45 NIS |
1275 | } |
1276 | ||
79072805 | 1277 | if (SvTYPE(sv) == mt) |
63f97190 | 1278 | return; |
79072805 | 1279 | |
f5282e15 | 1280 | if (SvTYPE(sv) > mt) |
921edb34 RGS |
1281 | Perl_croak(aTHX_ "sv_upgrade from type %d down to type %d", |
1282 | (int)SvTYPE(sv), (int)mt); | |
f5282e15 | 1283 | |
d2e56290 | 1284 | |
878cc751 NC |
1285 | old_body = SvANY(sv); |
1286 | old_body_arena = 0; | |
1287 | old_body_offset = 0; | |
4cbc76b1 | 1288 | old_body_length = 0; |
403d36eb NC |
1289 | new_body_offset = 0; |
1290 | new_body_length = ~0; | |
1291 | ||
1292 | /* Copying structures onto other structures that have been neatly zeroed | |
1293 | has a subtle gotcha. Consider XPVMG | |
1294 | ||
1295 | +------+------+------+------+------+-------+-------+ | |
1296 | | NV | CUR | LEN | IV | MAGIC | STASH | | |
1297 | +------+------+------+------+------+-------+-------+ | |
1298 | 0 4 8 12 16 20 24 28 | |
1299 | ||
1300 | where NVs are aligned to 8 bytes, so that sizeof that structure is | |
1301 | actually 32 bytes long, with 4 bytes of padding at the end: | |
1302 | ||
1303 | +------+------+------+------+------+-------+-------+------+ | |
1304 | | NV | CUR | LEN | IV | MAGIC | STASH | ??? | | |
1305 | +------+------+------+------+------+-------+-------+------+ | |
1306 | 0 4 8 12 16 20 24 28 32 | |
1307 | ||
1308 | so what happens if you allocate memory for this structure: | |
1309 | ||
1310 | +------+------+------+------+------+-------+-------+------+------+... | |
1311 | | NV | CUR | LEN | IV | MAGIC | STASH | GP | NAME | | |
1312 | +------+------+------+------+------+-------+-------+------+------+... | |
1313 | 0 4 8 12 16 20 24 28 32 36 | |
1314 | ||
1315 | zero it, then copy sizeof(XPVMG) bytes on top of it? Not quite what you | |
1316 | expect, because you copy the area marked ??? onto GP. Now, ??? may have | |
1317 | started out as zero once, but it's quite possible that it isn't. So now, | |
1318 | rather than a nicely zeroed GP, you have it pointing somewhere random. | |
1319 | Bugs ensue. | |
1320 | ||
1321 | (In fact, GP ends up pointing at a previous GP structure, because the | |
1322 | principle cause of the padding in XPVMG getting garbage is a copy of | |
1323 | sizeof(XPVMG) bytes from a XPVGV structure in sv_unglob) | |
1324 | ||
1325 | So we are careful and work out the size of used parts of all the | |
1326 | structures. */ | |
878cc751 | 1327 | |
79072805 LW |
1328 | switch (SvTYPE(sv)) { |
1329 | case SVt_NULL: | |
79072805 | 1330 | break; |
79072805 | 1331 | case SVt_IV: |
ed6116ce | 1332 | if (mt == SVt_NV) |
463ee0b2 | 1333 | mt = SVt_PVNV; |
ed6116ce LW |
1334 | else if (mt < SVt_PVIV) |
1335 | mt = SVt_PVIV; | |
4cbc76b1 NC |
1336 | old_body_offset = STRUCT_OFFSET(XPVIV, xiv_iv); |
1337 | old_body_length = sizeof(IV); | |
79072805 LW |
1338 | break; |
1339 | case SVt_NV: | |
9a085840 | 1340 | old_body_arena = (void **) &PL_xnv_root; |
4cbc76b1 | 1341 | old_body_length = sizeof(NV); |
16b305e3 | 1342 | #ifndef NV_ZERO_IS_ALLBITS_ZERO |
4cbc76b1 | 1343 | zero_nv = FALSE; |
16b305e3 | 1344 | #endif |
ed6116ce | 1345 | if (mt < SVt_PVNV) |
79072805 LW |
1346 | mt = SVt_PVNV; |
1347 | break; | |
ed6116ce | 1348 | case SVt_RV: |
ed6116ce | 1349 | break; |
79072805 | 1350 | case SVt_PV: |
9a085840 | 1351 | old_body_arena = (void **) &PL_xpv_root; |
878cc751 NC |
1352 | old_body_offset = STRUCT_OFFSET(XPV, xpv_cur) |
1353 | - STRUCT_OFFSET(xpv_allocated, xpv_cur); | |
403d36eb NC |
1354 | old_body_length = STRUCT_OFFSET(XPV, xpv_len) |
1355 | + sizeof (((XPV*)SvANY(sv))->xpv_len) | |
1356 | - old_body_offset; | |
748a9306 LW |
1357 | if (mt <= SVt_IV) |
1358 | mt = SVt_PVIV; | |
1359 | else if (mt == SVt_NV) | |
1360 | mt = SVt_PVNV; | |
79072805 LW |
1361 | break; |
1362 | case SVt_PVIV: | |
9a085840 | 1363 | old_body_arena = (void **) &PL_xpviv_root; |
878cc751 NC |
1364 | old_body_offset = STRUCT_OFFSET(XPVIV, xpv_cur) |
1365 | - STRUCT_OFFSET(xpviv_allocated, xpv_cur); | |
403d36eb NC |
1366 | old_body_length = STRUCT_OFFSET(XPVIV, xiv_u) |
1367 | + sizeof (((XPVIV*)SvANY(sv))->xiv_u) | |
1368 | - old_body_offset; | |
79072805 LW |
1369 | break; |
1370 | case SVt_PVNV: | |
9a085840 | 1371 | old_body_arena = (void **) &PL_xpvnv_root; |
403d36eb NC |
1372 | old_body_length = STRUCT_OFFSET(XPVNV, xiv_u) |
1373 | + sizeof (((XPVNV*)SvANY(sv))->xiv_u); | |
16b305e3 | 1374 | #ifndef NV_ZERO_IS_ALLBITS_ZERO |
4cbc76b1 | 1375 | zero_nv = FALSE; |
16b305e3 | 1376 | #endif |
79072805 LW |
1377 | break; |
1378 | case SVt_PVMG: | |
0ec50a73 NC |
1379 | /* Because the XPVMG of PL_mess_sv isn't allocated from the arena, |
1380 | there's no way that it can be safely upgraded, because perl.c | |
1381 | expects to Safefree(SvANY(PL_mess_sv)) */ | |
1382 | assert(sv != PL_mess_sv); | |
bce8f412 NC |
1383 | /* This flag bit is used to mean other things in other scalar types. |
1384 | Given that it only has meaning inside the pad, it shouldn't be set | |
1385 | on anything that can get upgraded. */ | |
1386 | assert((SvFLAGS(sv) & SVpad_TYPED) == 0); | |
9a085840 | 1387 | old_body_arena = (void **) &PL_xpvmg_root; |
403d36eb NC |
1388 | old_body_length = STRUCT_OFFSET(XPVMG, xmg_stash) |
1389 | + sizeof (((XPVMG*)SvANY(sv))->xmg_stash); | |
16b305e3 | 1390 | #ifndef NV_ZERO_IS_ALLBITS_ZERO |
4cbc76b1 | 1391 | zero_nv = FALSE; |
16b305e3 | 1392 | #endif |
79072805 LW |
1393 | break; |
1394 | default: | |
cea2e8a9 | 1395 | Perl_croak(aTHX_ "Can't upgrade that kind of scalar"); |
79072805 LW |
1396 | } |
1397 | ||
ffb05e06 NC |
1398 | SvFLAGS(sv) &= ~SVTYPEMASK; |
1399 | SvFLAGS(sv) |= mt; | |
1400 | ||
79072805 LW |
1401 | switch (mt) { |
1402 | case SVt_NULL: | |
cea2e8a9 | 1403 | Perl_croak(aTHX_ "Can't upgrade to undef"); |
79072805 | 1404 | case SVt_IV: |
4cbc76b1 | 1405 | assert(old_type == SVt_NULL); |
339049b0 | 1406 | SvANY(sv) = (XPVIV*)((char*)&(sv->sv_u.svu_iv) - STRUCT_OFFSET(XPVIV, xiv_iv)); |
403d36eb | 1407 | SvIV_set(sv, 0); |
85274cbc | 1408 | return; |
79072805 | 1409 | case SVt_NV: |
4cbc76b1 | 1410 | assert(old_type == SVt_NULL); |
79072805 | 1411 | SvANY(sv) = new_XNV(); |
403d36eb | 1412 | SvNV_set(sv, 0); |
85274cbc | 1413 | return; |
ed6116ce | 1414 | case SVt_RV: |
4cbc76b1 | 1415 | assert(old_type == SVt_NULL); |
339049b0 | 1416 | SvANY(sv) = &sv->sv_u.svu_rv; |
403d36eb | 1417 | SvRV_set(sv, 0); |
85274cbc | 1418 | return; |
79072805 LW |
1419 | case SVt_PVHV: |
1420 | SvANY(sv) = new_XPVHV(); | |
463ee0b2 LW |
1421 | HvFILL(sv) = 0; |
1422 | HvMAX(sv) = 0; | |
8aacddc1 | 1423 | HvTOTALKEYS(sv) = 0; |
bd4b1eb5 | 1424 | |
2068cd4d NC |
1425 | goto hv_av_common; |
1426 | ||
1427 | case SVt_PVAV: | |
1428 | SvANY(sv) = new_XPVAV(); | |
1429 | AvMAX(sv) = -1; | |
1430 | AvFILLp(sv) = -1; | |
1431 | AvALLOC(sv) = 0; | |
1432 | AvREAL_only(sv); | |
1433 | ||
1434 | hv_av_common: | |
1435 | /* SVt_NULL isn't the only thing upgraded to AV or HV. | |
1436 | The target created by newSVrv also is, and it can have magic. | |
1437 | However, it never has SvPVX set. | |
1438 | */ | |
1439 | if (old_type >= SVt_RV) { | |
1440 | assert(SvPVX_const(sv) == 0); | |
8bd4d4c5 | 1441 | } |
2068cd4d NC |
1442 | |
1443 | /* Could put this in the else clause below, as PVMG must have SvPVX | |
1444 | 0 already (the assertion above) */ | |
bd4b1eb5 | 1445 | SvPV_set(sv, (char*)0); |
2068cd4d NC |
1446 | |
1447 | if (old_type >= SVt_PVMG) { | |
1448 | SvMAGIC_set(sv, ((XPVMG*)old_body)->xmg_magic); | |
1449 | SvSTASH_set(sv, ((XPVMG*)old_body)->xmg_stash); | |
1450 | } else { | |
1451 | SvMAGIC_set(sv, 0); | |
1452 | SvSTASH_set(sv, 0); | |
1453 | } | |
79072805 | 1454 | break; |
bd4b1eb5 NC |
1455 | |
1456 | case SVt_PVIO: | |
403d36eb NC |
1457 | new_body = new_XPVIO(); |
1458 | new_body_length = sizeof(XPVIO); | |
1459 | goto zero; | |
bd4b1eb5 | 1460 | case SVt_PVFM: |
403d36eb NC |
1461 | new_body = new_XPVFM(); |
1462 | new_body_length = sizeof(XPVFM); | |
1463 | goto zero; | |
1464 | ||
bd4b1eb5 | 1465 | case SVt_PVBM: |
403d36eb NC |
1466 | new_body_length = sizeof(XPVBM); |
1467 | new_body_arena = (void **) &PL_xpvbm_root; | |
1468 | new_body_arenaroot = (void **) &PL_xpvbm_arenaroot; | |
1469 | goto new_body; | |
bd4b1eb5 | 1470 | case SVt_PVGV: |
403d36eb NC |
1471 | new_body_length = sizeof(XPVGV); |
1472 | new_body_arena = (void **) &PL_xpvgv_root; | |
1473 | new_body_arenaroot = (void **) &PL_xpvgv_arenaroot; | |
1474 | goto new_body; | |
79072805 | 1475 | case SVt_PVCV: |
403d36eb NC |
1476 | new_body_length = sizeof(XPVCV); |
1477 | new_body_arena = (void **) &PL_xpvcv_root; | |
1478 | new_body_arenaroot = (void **) &PL_xpvcv_arenaroot; | |
1479 | goto new_body; | |
bd4b1eb5 | 1480 | case SVt_PVLV: |
403d36eb NC |
1481 | new_body_length = sizeof(XPVLV); |
1482 | new_body_arena = (void **) &PL_xpvlv_root; | |
1483 | new_body_arenaroot = (void **) &PL_xpvlv_arenaroot; | |
1484 | goto new_body; | |
1485 | case SVt_PVMG: | |
1486 | new_body_length = sizeof(XPVMG); | |
1487 | new_body_arena = (void **) &PL_xpvmg_root; | |
1488 | new_body_arenaroot = (void **) &PL_xpvmg_arenaroot; | |
1489 | goto new_body; | |
1490 | case SVt_PVNV: | |
1491 | new_body_length = sizeof(XPVNV); | |
1492 | new_body_arena = (void **) &PL_xpvnv_root; | |
1493 | new_body_arenaroot = (void **) &PL_xpvnv_arenaroot; | |
1494 | goto new_body; | |
1495 | case SVt_PVIV: | |
1496 | new_body_offset = STRUCT_OFFSET(XPVIV, xpv_cur) | |
1497 | - STRUCT_OFFSET(xpviv_allocated, xpv_cur); | |
1498 | new_body_length = sizeof(XPVIV) - new_body_offset; | |
1499 | new_body_arena = (void **) &PL_xpviv_root; | |
1500 | new_body_arenaroot = (void **) &PL_xpviv_arenaroot; | |
1501 | /* XXX Is this still needed? Was it ever needed? Surely as there is | |
1502 | no route from NV to PVIV, NOK can never be true */ | |
1503 | if (SvNIOK(sv)) | |
1504 | (void)SvIOK_on(sv); | |
1505 | SvNOK_off(sv); | |
1506 | goto new_body_no_NV; | |
1507 | case SVt_PV: | |
1508 | new_body_offset = STRUCT_OFFSET(XPV, xpv_cur) | |
1509 | - STRUCT_OFFSET(xpv_allocated, xpv_cur); | |
1510 | new_body_length = sizeof(XPV) - new_body_offset; | |
1511 | new_body_arena = (void **) &PL_xpv_root; | |
1512 | new_body_arenaroot = (void **) &PL_xpv_arenaroot; | |
1513 | new_body_no_NV: | |
1514 | /* PV and PVIV don't have an NV slot. */ | |
16b305e3 | 1515 | #ifndef NV_ZERO_IS_ALLBITS_ZERO |
403d36eb | 1516 | zero_nv = FALSE; |
16b305e3 | 1517 | #endif |
403d36eb | 1518 | |
16b305e3 NC |
1519 | new_body: |
1520 | assert(new_body_length); | |
403d36eb | 1521 | #ifndef PURIFY |
16b305e3 NC |
1522 | /* This points to the start of the allocated area. */ |
1523 | new_body = S_new_body(aTHX_ new_body_arenaroot, new_body_arena, | |
1524 | new_body_length); | |
403d36eb | 1525 | #else |
16b305e3 NC |
1526 | /* We always allocated the full length item with PURIFY */ |
1527 | new_body_length += new_body_offset; | |
1528 | new_body_offset = 0; | |
1529 | new_body = my_safemalloc(new_body_length); | |
403d36eb NC |
1530 | |
1531 | #endif | |
16b305e3 NC |
1532 | zero: |
1533 | Zero(new_body, new_body_length, char); | |
1534 | new_body = ((char *)new_body) - new_body_offset; | |
1535 | SvANY(sv) = new_body; | |
1536 | ||
1537 | if (old_body_length) { | |
1538 | Copy((char *)old_body + old_body_offset, | |
1539 | (char *)new_body + old_body_offset, | |
1540 | old_body_length, char); | |
1541 | } | |
403d36eb | 1542 | |
16b305e3 NC |
1543 | #ifndef NV_ZERO_IS_ALLBITS_ZERO |
1544 | if (zero_nv) | |
1545 | SvNV_set(sv, 0); | |
1546 | #endif | |
403d36eb | 1547 | |
16b305e3 NC |
1548 | if (mt == SVt_PVIO) |
1549 | IoPAGE_LEN(sv) = 60; | |
1550 | if (old_type < SVt_RV) | |
1551 | SvPV_set(sv, 0); | |
8990e307 | 1552 | break; |
403d36eb NC |
1553 | default: |
1554 | Perl_croak(aTHX_ "panic: sv_upgrade to unknown type %lu", mt); | |
8990e307 | 1555 | } |
878cc751 NC |
1556 | |
1557 | ||
1558 | if (old_body_arena) { | |
1559 | #ifdef PURIFY | |
ee6954bb | 1560 | my_safefree(old_body); |
878cc751 | 1561 | #else |
cb4415b8 NC |
1562 | del_body((void*)((char*)old_body + old_body_offset), |
1563 | old_body_arena); | |
878cc751 | 1564 | #endif |
2068cd4d | 1565 | } |
79072805 LW |
1566 | } |
1567 | ||
645c22ef DM |
1568 | /* |
1569 | =for apidoc sv_backoff | |
1570 | ||
1571 | Remove any string offset. You should normally use the C<SvOOK_off> macro | |
1572 | wrapper instead. | |
1573 | ||
1574 | =cut | |
1575 | */ | |
1576 | ||
79072805 | 1577 | int |
864dbfa3 | 1578 | Perl_sv_backoff(pTHX_ register SV *sv) |
79072805 LW |
1579 | { |
1580 | assert(SvOOK(sv)); | |
b79f7545 NC |
1581 | assert(SvTYPE(sv) != SVt_PVHV); |
1582 | assert(SvTYPE(sv) != SVt_PVAV); | |
463ee0b2 | 1583 | if (SvIVX(sv)) { |
53c1dcc0 | 1584 | const char * const s = SvPVX_const(sv); |
b162af07 | 1585 | SvLEN_set(sv, SvLEN(sv) + SvIVX(sv)); |
f880fe2f | 1586 | SvPV_set(sv, SvPVX(sv) - SvIVX(sv)); |
79072805 | 1587 | SvIV_set(sv, 0); |
463ee0b2 | 1588 | Move(s, SvPVX(sv), SvCUR(sv)+1, char); |
79072805 LW |
1589 | } |
1590 | SvFLAGS(sv) &= ~SVf_OOK; | |
a0d0e21e | 1591 | return 0; |
79072805 LW |
1592 | } |
1593 | ||
954c1994 GS |
1594 | /* |
1595 | =for apidoc sv_grow | |
1596 | ||
645c22ef DM |
1597 | Expands the character buffer in the SV. If necessary, uses C<sv_unref> and |
1598 | upgrades the SV to C<SVt_PV>. Returns a pointer to the character buffer. | |
1599 | Use the C<SvGROW> wrapper instead. | |
954c1994 GS |
1600 | |
1601 | =cut | |
1602 | */ | |
1603 | ||
79072805 | 1604 | char * |
864dbfa3 | 1605 | Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen) |
79072805 LW |
1606 | { |
1607 | register char *s; | |
1608 | ||
55497cff | 1609 | #ifdef HAS_64K_LIMIT |
79072805 | 1610 | if (newlen >= 0x10000) { |
1d7c1841 GS |
1611 | PerlIO_printf(Perl_debug_log, |
1612 | "Allocation too large: %"UVxf"\n", (UV)newlen); | |
79072805 LW |
1613 | my_exit(1); |
1614 | } | |
55497cff | 1615 | #endif /* HAS_64K_LIMIT */ |
a0d0e21e LW |
1616 | if (SvROK(sv)) |
1617 | sv_unref(sv); | |
79072805 LW |
1618 | if (SvTYPE(sv) < SVt_PV) { |
1619 | sv_upgrade(sv, SVt_PV); | |
93524f2b | 1620 | s = SvPVX_mutable(sv); |
79072805 LW |
1621 | } |
1622 | else if (SvOOK(sv)) { /* pv is offset? */ | |
1623 | sv_backoff(sv); | |
93524f2b | 1624 | s = SvPVX_mutable(sv); |
79072805 LW |
1625 | if (newlen > SvLEN(sv)) |
1626 | newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */ | |
c6f8c383 GA |
1627 | #ifdef HAS_64K_LIMIT |
1628 | if (newlen >= 0x10000) | |
1629 | newlen = 0xFFFF; | |
1630 | #endif | |
79072805 | 1631 | } |
bc44a8a2 | 1632 | else |
4d84ee25 | 1633 | s = SvPVX_mutable(sv); |
54f0641b | 1634 | |
79072805 | 1635 | if (newlen > SvLEN(sv)) { /* need more room? */ |
7a9b70e9 | 1636 | newlen = PERL_STRLEN_ROUNDUP(newlen); |
8d6dde3e | 1637 | if (SvLEN(sv) && s) { |
7bab3ede | 1638 | #ifdef MYMALLOC |
93524f2b | 1639 | const STRLEN l = malloced_size((void*)SvPVX_const(sv)); |
8d6dde3e IZ |
1640 | if (newlen <= l) { |
1641 | SvLEN_set(sv, l); | |
1642 | return s; | |
1643 | } else | |
c70c8a0a | 1644 | #endif |
1936d2a7 | 1645 | s = saferealloc(s, newlen); |
8d6dde3e | 1646 | } |
bfed75c6 | 1647 | else { |
1936d2a7 | 1648 | s = safemalloc(newlen); |
3f7c398e SP |
1649 | if (SvPVX_const(sv) && SvCUR(sv)) { |
1650 | Move(SvPVX_const(sv), s, (newlen < SvCUR(sv)) ? newlen : SvCUR(sv), char); | |
40565179 | 1651 | } |
4e83176d | 1652 | } |
79072805 | 1653 | SvPV_set(sv, s); |
e1ec3a88 | 1654 | SvLEN_set(sv, newlen); |
79072805 LW |
1655 | } |
1656 | return s; | |
1657 | } | |
1658 | ||
954c1994 GS |
1659 | /* |
1660 | =for apidoc sv_setiv | |
1661 | ||
645c22ef DM |
1662 | Copies an integer into the given SV, upgrading first if necessary. |
1663 | Does not handle 'set' magic. See also C<sv_setiv_mg>. | |
954c1994 GS |
1664 | |
1665 | =cut | |
1666 | */ | |
1667 | ||
79072805 | 1668 | void |
864dbfa3 | 1669 | Perl_sv_setiv(pTHX_ register SV *sv, IV i) |
79072805 | 1670 | { |
765f542d | 1671 | SV_CHECK_THINKFIRST_COW_DROP(sv); |
463ee0b2 LW |
1672 | switch (SvTYPE(sv)) { |
1673 | case SVt_NULL: | |
79072805 | 1674 | sv_upgrade(sv, SVt_IV); |
463ee0b2 LW |
1675 | break; |
1676 | case SVt_NV: | |
1677 | sv_upgrade(sv, SVt_PVNV); | |
1678 | break; | |
ed6116ce | 1679 | case SVt_RV: |
463ee0b2 | 1680 | case SVt_PV: |
79072805 | 1681 | sv_upgrade(sv, SVt_PVIV); |
463ee0b2 | 1682 | break; |
a0d0e21e LW |
1683 | |
1684 | case SVt_PVGV: | |
a0d0e21e LW |
1685 | case SVt_PVAV: |
1686 | case SVt_PVHV: | |
1687 | case SVt_PVCV: | |
1688 | case SVt_PVFM: | |
1689 | case SVt_PVIO: | |
411caa50 | 1690 | Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0), |
53e06cf0 | 1691 | OP_DESC(PL_op)); |
463ee0b2 | 1692 | } |
a0d0e21e | 1693 | (void)SvIOK_only(sv); /* validate number */ |
45977657 | 1694 | SvIV_set(sv, i); |
463ee0b2 | 1695 | SvTAINT(sv); |
79072805 LW |
1696 | } |
1697 | ||
954c1994 GS |
1698 | /* |
1699 | =for apidoc sv_setiv_mg | |
1700 | ||
1701 | Like C<sv_setiv>, but also handles 'set' magic. | |
1702 | ||
1703 | =cut | |
1704 | */ | |
1705 | ||
79072805 | 1706 | void |
864dbfa3 | 1707 | Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i) |
ef50df4b GS |
1708 | { |
1709 | sv_setiv(sv,i); | |
1710 | SvSETMAGIC(sv); | |
1711 | } | |
1712 | ||
954c1994 GS |
1713 | /* |
1714 | =for apidoc sv_setuv | |
1715 | ||
645c22ef DM |
1716 | Copies an unsigned integer into the given SV, upgrading first if necessary. |
1717 | Does not handle 'set' magic. See also C<sv_setuv_mg>. | |
954c1994 GS |
1718 | |
1719 | =cut | |
1720 | */ | |
1721 | ||
ef50df4b | 1722 | void |
864dbfa3 | 1723 | Perl_sv_setuv(pTHX_ register SV *sv, UV u) |
55497cff | 1724 | { |
55ada374 NC |
1725 | /* With these two if statements: |
1726 | u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865 | |
d460ef45 | 1727 | |
55ada374 NC |
1728 | without |
1729 | u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865 | |
d460ef45 | 1730 | |
55ada374 NC |
1731 | If you wish to remove them, please benchmark to see what the effect is |
1732 | */ | |
28e5dec8 JH |
1733 | if (u <= (UV)IV_MAX) { |
1734 | sv_setiv(sv, (IV)u); | |
1735 | return; | |
1736 | } | |
25da4f38 IZ |
1737 | sv_setiv(sv, 0); |
1738 | SvIsUV_on(sv); | |
607fa7f2 | 1739 | SvUV_set(sv, u); |
55497cff | 1740 | } |
1741 | ||
954c1994 GS |
1742 | /* |
1743 | =for apidoc sv_setuv_mg | |
1744 | ||
1745 | Like C<sv_setuv>, but also handles 'set' magic. | |
1746 | ||
1747 | =cut | |
1748 | */ | |
1749 | ||
55497cff | 1750 | void |
864dbfa3 | 1751 | Perl_sv_setuv_mg(pTHX_ register SV *sv, UV u) |
ef50df4b | 1752 | { |
55ada374 NC |
1753 | /* With these two if statements: |
1754 | u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865 | |
d460ef45 | 1755 | |
55ada374 NC |
1756 | without |
1757 | u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865 | |
d460ef45 | 1758 | |
55ada374 NC |
1759 | If you wish to remove them, please benchmark to see what the effect is |
1760 | */ | |
28e5dec8 JH |
1761 | if (u <= (UV)IV_MAX) { |
1762 | sv_setiv(sv, (IV)u); | |
1763 | } else { | |
1764 | sv_setiv(sv, 0); | |
1765 | SvIsUV_on(sv); | |
1766 | sv_setuv(sv,u); | |
1767 | } | |
ef50df4b GS |
1768 | SvSETMAGIC(sv); |
1769 | } | |
1770 | ||
954c1994 GS |
1771 | /* |
1772 | =for apidoc sv_setnv | |
1773 | ||
645c22ef DM |
1774 | Copies a double into the given SV, upgrading first if necessary. |
1775 | Does not handle 'set' magic. See also C<sv_setnv_mg>. | |
954c1994 GS |
1776 | |
1777 | =cut | |
1778 | */ | |
1779 | ||
ef50df4b | 1780 | void |
65202027 | 1781 | Perl_sv_setnv(pTHX_ register SV *sv, NV num) |
79072805 | 1782 | { |
765f542d | 1783 | SV_CHECK_THINKFIRST_COW_DROP(sv); |
a0d0e21e LW |
1784 | switch (SvTYPE(sv)) { |
1785 | case SVt_NULL: | |
1786 | case SVt_IV: | |
79072805 | 1787 | sv_upgrade(sv, SVt_NV); |
a0d0e21e | 1788 | break; |
a0d0e21e LW |
1789 | case SVt_RV: |
1790 | case SVt_PV: | |
1791 | case SVt_PVIV: | |
79072805 | 1792 | sv_upgrade(sv, SVt_PVNV); |
a0d0e21e | 1793 | break; |
827b7e14 | 1794 | |
a0d0e21e | 1795 | case SVt_PVGV: |
a0d0e21e LW |
1796 | case SVt_PVAV: |
1797 | case SVt_PVHV: | |
1798 | case SVt_PVCV: | |
1799 | case SVt_PVFM: | |
1800 | case SVt_PVIO: | |
411caa50 | 1801 | Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0), |
53e06cf0 | 1802 | OP_NAME(PL_op)); |
79072805 | 1803 | } |
9d6ce603 | 1804 | SvNV_set(sv, num); |
a0d0e21e | 1805 | (void)SvNOK_only(sv); /* validate number */ |
463ee0b2 | 1806 | SvTAINT(sv); |
79072805 LW |
1807 | } |
1808 | ||
954c1994 GS |
1809 | /* |
1810 | =for apidoc sv_setnv_mg | |
1811 | ||
1812 | Like C<sv_setnv>, but also handles 'set' magic. | |
1813 | ||
1814 | =cut | |
1815 | */ | |
1816 | ||
ef50df4b | 1817 | void |
65202027 | 1818 | Perl_sv_setnv_mg(pTHX_ register SV *sv, NV num) |
ef50df4b GS |
1819 | { |
1820 | sv_setnv(sv,num); | |
1821 | SvSETMAGIC(sv); | |
1822 | } | |
1823 | ||
645c22ef DM |
1824 | /* Print an "isn't numeric" warning, using a cleaned-up, |
1825 | * printable version of the offending string | |
1826 | */ | |
1827 | ||
76e3520e | 1828 | STATIC void |
cea2e8a9 | 1829 | S_not_a_number(pTHX_ SV *sv) |
a0d0e21e | 1830 | { |
94463019 JH |
1831 | SV *dsv; |
1832 | char tmpbuf[64]; | |
1833 | char *pv; | |
1834 | ||
1835 | if (DO_UTF8(sv)) { | |
1836 | dsv = sv_2mortal(newSVpv("", 0)); | |
1837 | pv = sv_uni_display(dsv, sv, 10, 0); | |
1838 | } else { | |
1839 | char *d = tmpbuf; | |
1840 | char *limit = tmpbuf + sizeof(tmpbuf) - 8; | |
1841 | /* each *s can expand to 4 chars + "...\0", | |
1842 | i.e. need room for 8 chars */ | |
ecdeb87c | 1843 | |
e62f0680 NC |
1844 | const char *s, *end; |
1845 | for (s = SvPVX_const(sv), end = s + SvCUR(sv); s < end && d < limit; | |
1846 | s++) { | |
94463019 JH |
1847 | int ch = *s & 0xFF; |
1848 | if (ch & 128 && !isPRINT_LC(ch)) { | |
1849 | *d++ = 'M'; | |
1850 | *d++ = '-'; | |
1851 | ch &= 127; | |
1852 | } | |
1853 | if (ch == '\n') { | |
1854 | *d++ = '\\'; | |
1855 | *d++ = 'n'; | |
1856 | } | |
1857 | else if (ch == '\r') { | |
1858 | *d++ = '\\'; | |
1859 | *d++ = 'r'; | |
1860 | } | |
1861 | else if (ch == '\f') { | |
1862 | *d++ = '\\'; | |
1863 | *d++ = 'f'; | |
1864 | } | |
1865 | else if (ch == '\\') { | |
1866 | *d++ = '\\'; | |
1867 | *d++ = '\\'; | |
1868 | } | |
1869 | else if (ch == '\0') { | |
1870 | *d++ = '\\'; | |
1871 | *d++ = '0'; | |
1872 | } | |
1873 | else if (isPRINT_LC(ch)) | |
1874 | *d++ = ch; | |
1875 | else { | |
1876 | *d++ = '^'; | |
1877 | *d++ = toCTRL(ch); | |
1878 | } | |
1879 | } | |
1880 | if (s < end) { | |
1881 | *d++ = '.'; | |
1882 | *d++ = '.'; | |
1883 | *d++ = '.'; | |
1884 | } | |
1885 | *d = '\0'; | |
1886 | pv = tmpbuf; | |
a0d0e21e | 1887 | } |
a0d0e21e | 1888 | |
533c011a | 1889 | if (PL_op) |
9014280d | 1890 | Perl_warner(aTHX_ packWARN(WARN_NUMERIC), |
94463019 JH |
1891 | "Argument \"%s\" isn't numeric in %s", pv, |
1892 | OP_DESC(PL_op)); | |
a0d0e21e | 1893 | else |
9014280d | 1894 | Perl_warner(aTHX_ packWARN(WARN_NUMERIC), |
94463019 | 1895 | "Argument \"%s\" isn't numeric", pv); |
a0d0e21e LW |
1896 | } |
1897 | ||
c2988b20 NC |
1898 | /* |
1899 | =for apidoc looks_like_number | |
1900 | ||
645c22ef DM |
1901 | Test if the content of an SV looks like a number (or is a number). |
1902 | C<Inf> and C<Infinity> are treated as numbers (so will not issue a | |
1903 | non-numeric warning), even if your atof() doesn't grok them. | |
c2988b20 NC |
1904 | |
1905 | =cut | |
1906 | */ | |
1907 | ||
1908 | I32 | |
1909 | Perl_looks_like_number(pTHX_ SV *sv) | |
1910 | { | |
a3b680e6 | 1911 | register const char *sbegin; |
c2988b20 NC |
1912 | STRLEN len; |
1913 | ||
1914 | if (SvPOK(sv)) { | |
3f7c398e | 1915 | sbegin = SvPVX_const(sv); |
c2988b20 NC |
1916 | len = SvCUR(sv); |
1917 | } | |
1918 | else if (SvPOKp(sv)) | |
83003860 | 1919 | sbegin = SvPV_const(sv, len); |
c2988b20 | 1920 | else |
e0ab1c0e | 1921 | return SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK); |
c2988b20 NC |
1922 | return grok_number(sbegin, len, NULL); |
1923 | } | |
25da4f38 IZ |
1924 | |
1925 | /* Actually, ISO C leaves conversion of UV to IV undefined, but | |
1926 | until proven guilty, assume that things are not that bad... */ | |
1927 | ||
645c22ef DM |
1928 | /* |
1929 | NV_PRESERVES_UV: | |
1930 | ||
1931 | As 64 bit platforms often have an NV that doesn't preserve all bits of | |
28e5dec8 JH |
1932 | an IV (an assumption perl has been based on to date) it becomes necessary |
1933 | to remove the assumption that the NV always carries enough precision to | |
1934 | recreate the IV whenever needed, and that the NV is the canonical form. | |
1935 | Instead, IV/UV and NV need to be given equal rights. So as to not lose | |
645c22ef | 1936 | precision as a side effect of conversion (which would lead to insanity |
28e5dec8 JH |
1937 | and the dragon(s) in t/op/numconvert.t getting very angry) the intent is |
1938 | 1) to distinguish between IV/UV/NV slots that have cached a valid | |
1939 | conversion where precision was lost and IV/UV/NV slots that have a | |
1940 | valid conversion which has lost no precision | |
645c22ef | 1941 | 2) to ensure that if a numeric conversion to one form is requested that |
28e5dec8 JH |
1942 | would lose precision, the precise conversion (or differently |
1943 | imprecise conversion) is also performed and cached, to prevent | |
1944 | requests for different numeric formats on the same SV causing | |
1945 | lossy conversion chains. (lossless conversion chains are perfectly | |
1946 | acceptable (still)) | |
1947 | ||
1948 | ||
1949 | flags are used: | |
1950 | SvIOKp is true if the IV slot contains a valid value | |
1951 | SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true) | |
1952 | SvNOKp is true if the NV slot contains a valid value | |
1953 | SvNOK is true only if the NV value is accurate | |
1954 | ||
1955 | so | |
645c22ef | 1956 | while converting from PV to NV, check to see if converting that NV to an |
28e5dec8 JH |
1957 | IV(or UV) would lose accuracy over a direct conversion from PV to |
1958 | IV(or UV). If it would, cache both conversions, return NV, but mark | |
1959 | SV as IOK NOKp (ie not NOK). | |
1960 | ||
645c22ef | 1961 | While converting from PV to IV, check to see if converting that IV to an |
28e5dec8 JH |
1962 | NV would lose accuracy over a direct conversion from PV to NV. If it |
1963 | would, cache both conversions, flag similarly. | |
1964 | ||
1965 | Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite | |
1966 | correctly because if IV & NV were set NV *always* overruled. | |
645c22ef DM |
1967 | Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flag's meaning |
1968 | changes - now IV and NV together means that the two are interchangeable: | |
28e5dec8 | 1969 | SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX; |
d460ef45 | 1970 | |
645c22ef DM |
1971 | The benefit of this is that operations such as pp_add know that if |
1972 | SvIOK is true for both left and right operands, then integer addition | |
1973 | can be used instead of floating point (for cases where the result won't | |
1974 | overflow). Before, floating point was always used, which could lead to | |
28e5dec8 JH |
1975 | loss of precision compared with integer addition. |
1976 | ||
1977 | * making IV and NV equal status should make maths accurate on 64 bit | |
1978 | platforms | |
1979 | * may speed up maths somewhat if pp_add and friends start to use | |
645c22ef | 1980 | integers when possible instead of fp. (Hopefully the overhead in |
28e5dec8 JH |
1981 | looking for SvIOK and checking for overflow will not outweigh the |
1982 | fp to integer speedup) | |
1983 | * will slow down integer operations (callers of SvIV) on "inaccurate" | |
1984 | values, as the change from SvIOK to SvIOKp will cause a call into | |
1985 | sv_2iv each time rather than a macro access direct to the IV slot | |
1986 | * should speed up number->string conversion on integers as IV is | |
645c22ef | 1987 | favoured when IV and NV are equally accurate |
28e5dec8 JH |
1988 | |
1989 | #################################################################### | |
645c22ef DM |
1990 | You had better be using SvIOK_notUV if you want an IV for arithmetic: |
1991 | SvIOK is true if (IV or UV), so you might be getting (IV)SvUV. | |
1992 | On the other hand, SvUOK is true iff UV. | |
28e5dec8 JH |
1993 | #################################################################### |
1994 | ||
645c22ef | 1995 | Your mileage will vary depending your CPU's relative fp to integer |
28e5dec8 JH |
1996 | performance ratio. |
1997 | */ | |
1998 | ||
1999 | #ifndef NV_PRESERVES_UV | |
645c22ef DM |
2000 | # define IS_NUMBER_UNDERFLOW_IV 1 |
2001 | # define IS_NUMBER_UNDERFLOW_UV 2 | |
2002 | # define IS_NUMBER_IV_AND_UV 2 | |
2003 | # define IS_NUMBER_OVERFLOW_IV 4 | |
2004 | # define IS_NUMBER_OVERFLOW_UV 5 | |
2005 | ||
2006 | /* sv_2iuv_non_preserve(): private routine for use by sv_2iv() and sv_2uv() */ | |
28e5dec8 JH |
2007 | |
2008 | /* For sv_2nv these three cases are "SvNOK and don't bother casting" */ | |
2009 | STATIC int | |
645c22ef | 2010 | S_sv_2iuv_non_preserve(pTHX_ register SV *sv, I32 numtype) |
28e5dec8 | 2011 | { |
3f7c398e | 2012 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_2iuv_non '%s', IV=0x%"UVxf" NV=%"NVgf" inttype=%"UVXf"\n", SvPVX_const(sv), SvIVX(sv), SvNVX(sv), (UV)numtype)); |
28e5dec8 JH |
2013 | if (SvNVX(sv) < (NV)IV_MIN) { |
2014 | (void)SvIOKp_on(sv); | |
2015 | (void)SvNOK_on(sv); | |
45977657 | 2016 | SvIV_set(sv, IV_MIN); |
28e5dec8 JH |
2017 | return IS_NUMBER_UNDERFLOW_IV; |
2018 | } | |
2019 | if (SvNVX(sv) > (NV)UV_MAX) { | |
2020 | (void)SvIOKp_on(sv); | |
2021 | (void)SvNOK_on(sv); | |
2022 | SvIsUV_on(sv); | |
607fa7f2 | 2023 | SvUV_set(sv, UV_MAX); |
28e5dec8 JH |
2024 | return IS_NUMBER_OVERFLOW_UV; |
2025 | } | |
c2988b20 NC |
2026 | (void)SvIOKp_on(sv); |
2027 | (void)SvNOK_on(sv); | |
2028 | /* Can't use strtol etc to convert this string. (See truth table in | |
2029 | sv_2iv */ | |
2030 | if (SvNVX(sv) <= (UV)IV_MAX) { | |
45977657 | 2031 | SvIV_set(sv, I_V(SvNVX(sv))); |
c2988b20 NC |
2032 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) { |
2033 | SvIOK_on(sv); /* Integer is precise. NOK, IOK */ | |
2034 | } else { | |
2035 | /* Integer is imprecise. NOK, IOKp */ | |
2036 | } | |
2037 | return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV; | |
2038 | } | |
2039 | SvIsUV_on(sv); | |
607fa7f2 | 2040 | SvUV_set(sv, U_V(SvNVX(sv))); |
c2988b20 NC |
2041 | if ((NV)(SvUVX(sv)) == SvNVX(sv)) { |
2042 | if (SvUVX(sv) == UV_MAX) { | |
2043 | /* As we know that NVs don't preserve UVs, UV_MAX cannot | |
2044 | possibly be preserved by NV. Hence, it must be overflow. | |
2045 | NOK, IOKp */ | |
2046 | return IS_NUMBER_OVERFLOW_UV; | |
2047 | } | |
2048 | SvIOK_on(sv); /* Integer is precise. NOK, UOK */ | |
2049 | } else { | |
2050 | /* Integer is imprecise. NOK, IOKp */ | |
28e5dec8 | 2051 | } |
c2988b20 | 2052 | return IS_NUMBER_OVERFLOW_IV; |
28e5dec8 | 2053 | } |
645c22ef DM |
2054 | #endif /* !NV_PRESERVES_UV*/ |
2055 | ||
891f9566 YST |
2056 | /* sv_2iv() is now a macro using Perl_sv_2iv_flags(); |
2057 | * this function provided for binary compatibility only | |
2058 | */ | |
2059 | ||
2060 | IV | |
2061 | Perl_sv_2iv(pTHX_ register SV *sv) | |
2062 | { | |
2063 | return sv_2iv_flags(sv, SV_GMAGIC); | |
2064 | } | |
2065 | ||
645c22ef | 2066 | /* |
891f9566 | 2067 | =for apidoc sv_2iv_flags |
645c22ef | 2068 | |
891f9566 YST |
2069 | Return the integer value of an SV, doing any necessary string |
2070 | conversion. If flags includes SV_GMAGIC, does an mg_get() first. | |
2071 | Normally used via the C<SvIV(sv)> and C<SvIVx(sv)> macros. | |
645c22ef DM |
2072 | |
2073 | =cut | |
2074 | */ | |
28e5dec8 | 2075 | |
a0d0e21e | 2076 | IV |
891f9566 | 2077 | Perl_sv_2iv_flags(pTHX_ register SV *sv, I32 flags) |
79072805 LW |
2078 | { |
2079 | if (!sv) | |
2080 | return 0; | |
8990e307 | 2081 | if (SvGMAGICAL(sv)) { |
891f9566 YST |
2082 | if (flags & SV_GMAGIC) |
2083 | mg_get(sv); | |
463ee0b2 LW |
2084 | if (SvIOKp(sv)) |
2085 | return SvIVX(sv); | |
748a9306 | 2086 | if (SvNOKp(sv)) { |
25da4f38 | 2087 | return I_V(SvNVX(sv)); |
748a9306 | 2088 | } |
36477c24 | 2089 | if (SvPOKp(sv) && SvLEN(sv)) |
2090 | return asIV(sv); | |
3fe9a6f1 | 2091 | if (!SvROK(sv)) { |
d008e5eb | 2092 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 2093 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
29489e7c | 2094 | report_uninit(sv); |
c6ee37c5 | 2095 | } |
36477c24 | 2096 | return 0; |
3fe9a6f1 | 2097 | } |
463ee0b2 | 2098 | } |
ed6116ce | 2099 | if (SvTHINKFIRST(sv)) { |
a0d0e21e | 2100 | if (SvROK(sv)) { |
a0d0e21e | 2101 | SV* tmpstr; |
1554e226 | 2102 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) && |
b4b9a328 | 2103 | (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) |
9e7bc3e8 | 2104 | return SvIV(tmpstr); |
56431972 | 2105 | return PTR2IV(SvRV(sv)); |
a0d0e21e | 2106 | } |
765f542d NC |
2107 | if (SvIsCOW(sv)) { |
2108 | sv_force_normal_flags(sv, 0); | |
47deb5e7 | 2109 | } |
0336b60e | 2110 | if (SvREADONLY(sv) && !SvOK(sv)) { |
0336b60e | 2111 | if (ckWARN(WARN_UNINITIALIZED)) |
29489e7c | 2112 | report_uninit(sv); |
ed6116ce LW |
2113 | return 0; |
2114 | } | |
79072805 | 2115 | } |
25da4f38 IZ |
2116 | if (SvIOKp(sv)) { |
2117 | if (SvIsUV(sv)) { | |
2118 | return (IV)(SvUVX(sv)); | |
2119 | } | |
2120 | else { | |
2121 | return SvIVX(sv); | |
2122 | } | |
463ee0b2 | 2123 | } |
748a9306 | 2124 | if (SvNOKp(sv)) { |
28e5dec8 JH |
2125 | /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv |
2126 | * without also getting a cached IV/UV from it at the same time | |
2127 | * (ie PV->NV conversion should detect loss of accuracy and cache | |
2128 | * IV or UV at same time to avoid this. NWC */ | |
25da4f38 IZ |
2129 | |
2130 | if (SvTYPE(sv) == SVt_NV) | |
2131 | sv_upgrade(sv, SVt_PVNV); | |
2132 | ||
28e5dec8 JH |
2133 | (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */ |
2134 | /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost | |
2135 | certainly cast into the IV range at IV_MAX, whereas the correct | |
2136 | answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary | |
2137 | cases go to UV */ | |
2138 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
45977657 | 2139 | SvIV_set(sv, I_V(SvNVX(sv))); |
28e5dec8 JH |
2140 | if (SvNVX(sv) == (NV) SvIVX(sv) |
2141 | #ifndef NV_PRESERVES_UV | |
2142 | && (((UV)1 << NV_PRESERVES_UV_BITS) > | |
2143 | (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv))) | |
2144 | /* Don't flag it as "accurately an integer" if the number | |
2145 | came from a (by definition imprecise) NV operation, and | |
2146 | we're outside the range of NV integer precision */ | |
2147 | #endif | |
2148 | ) { | |
2149 | SvIOK_on(sv); /* Can this go wrong with rounding? NWC */ | |
2150 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
7234c960 | 2151 | "0x%"UVxf" iv(%"NVgf" => %"IVdf") (precise)\n", |
28e5dec8 JH |
2152 | PTR2UV(sv), |
2153 | SvNVX(sv), | |
2154 | SvIVX(sv))); | |
2155 | ||
2156 | } else { | |
2157 | /* IV not precise. No need to convert from PV, as NV | |
2158 | conversion would already have cached IV if it detected | |
2159 | that PV->IV would be better than PV->NV->IV | |
2160 | flags already correct - don't set public IOK. */ | |
2161 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
7234c960 | 2162 | "0x%"UVxf" iv(%"NVgf" => %"IVdf") (imprecise)\n", |
28e5dec8 JH |
2163 | PTR2UV(sv), |
2164 | SvNVX(sv), | |
2165 | SvIVX(sv))); | |
2166 | } | |
2167 | /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN, | |
2168 | but the cast (NV)IV_MIN rounds to a the value less (more | |
2169 | negative) than IV_MIN which happens to be equal to SvNVX ?? | |
2170 | Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and | |
2171 | NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and | |
2172 | (NV)UVX == NVX are both true, but the values differ. :-( | |
2173 | Hopefully for 2s complement IV_MIN is something like | |
2174 | 0x8000000000000000 which will be exact. NWC */ | |
d460ef45 | 2175 | } |
25da4f38 | 2176 | else { |
607fa7f2 | 2177 | SvUV_set(sv, U_V(SvNVX(sv))); |
28e5dec8 JH |
2178 | if ( |
2179 | (SvNVX(sv) == (NV) SvUVX(sv)) | |
2180 | #ifndef NV_PRESERVES_UV | |
2181 | /* Make sure it's not 0xFFFFFFFFFFFFFFFF */ | |
2182 | /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */ | |
2183 | && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv)) | |
2184 | /* Don't flag it as "accurately an integer" if the number | |
2185 | came from a (by definition imprecise) NV operation, and | |
2186 | we're outside the range of NV integer precision */ | |
2187 | #endif | |
2188 | ) | |
2189 | SvIOK_on(sv); | |
25da4f38 IZ |
2190 | SvIsUV_on(sv); |
2191 | ret_iv_max: | |
1c846c1f | 2192 | DEBUG_c(PerlIO_printf(Perl_debug_log, |
57def98f | 2193 | "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n", |
56431972 | 2194 | PTR2UV(sv), |
57def98f JH |
2195 | SvUVX(sv), |
2196 | SvUVX(sv))); | |
25da4f38 IZ |
2197 | return (IV)SvUVX(sv); |
2198 | } | |
748a9306 LW |
2199 | } |
2200 | else if (SvPOKp(sv) && SvLEN(sv)) { | |
c2988b20 | 2201 | UV value; |
504618e9 | 2202 | const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value); |
25da4f38 IZ |
2203 | /* We want to avoid a possible problem when we cache an IV which |
2204 | may be later translated to an NV, and the resulting NV is not | |
c2988b20 NC |
2205 | the same as the direct translation of the initial string |
2206 | (eg 123.456 can shortcut to the IV 123 with atol(), but we must | |
2207 | be careful to ensure that the value with the .456 is around if the | |
2208 | NV value is requested in the future). | |
1c846c1f | 2209 | |
25da4f38 IZ |
2210 | This means that if we cache such an IV, we need to cache the |
2211 | NV as well. Moreover, we trade speed for space, and do not | |
28e5dec8 | 2212 | cache the NV if we are sure it's not needed. |
25da4f38 | 2213 | */ |
16b7a9a4 | 2214 | |
c2988b20 NC |
2215 | /* SVt_PVNV is one higher than SVt_PVIV, hence this order */ |
2216 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) | |
2217 | == IS_NUMBER_IN_UV) { | |
5e045b90 | 2218 | /* It's definitely an integer, only upgrade to PVIV */ |
28e5dec8 JH |
2219 | if (SvTYPE(sv) < SVt_PVIV) |
2220 | sv_upgrade(sv, SVt_PVIV); | |
f7bbb42a | 2221 | (void)SvIOK_on(sv); |
c2988b20 NC |
2222 | } else if (SvTYPE(sv) < SVt_PVNV) |
2223 | sv_upgrade(sv, SVt_PVNV); | |
28e5dec8 | 2224 | |
c2988b20 NC |
2225 | /* If NV preserves UV then we only use the UV value if we know that |
2226 | we aren't going to call atof() below. If NVs don't preserve UVs | |
2227 | then the value returned may have more precision than atof() will | |
2228 | return, even though value isn't perfectly accurate. */ | |
2229 | if ((numtype & (IS_NUMBER_IN_UV | |
2230 | #ifdef NV_PRESERVES_UV | |
2231 | | IS_NUMBER_NOT_INT | |
2232 | #endif | |
2233 | )) == IS_NUMBER_IN_UV) { | |
2234 | /* This won't turn off the public IOK flag if it was set above */ | |
2235 | (void)SvIOKp_on(sv); | |
2236 | ||
2237 | if (!(numtype & IS_NUMBER_NEG)) { | |
2238 | /* positive */; | |
2239 | if (value <= (UV)IV_MAX) { | |
45977657 | 2240 | SvIV_set(sv, (IV)value); |
c2988b20 | 2241 | } else { |
607fa7f2 | 2242 | SvUV_set(sv, value); |
c2988b20 NC |
2243 | SvIsUV_on(sv); |
2244 | } | |
2245 | } else { | |
2246 | /* 2s complement assumption */ | |
2247 | if (value <= (UV)IV_MIN) { | |
45977657 | 2248 | SvIV_set(sv, -(IV)value); |
c2988b20 NC |
2249 | } else { |
2250 | /* Too negative for an IV. This is a double upgrade, but | |
d1be9408 | 2251 | I'm assuming it will be rare. */ |
c2988b20 NC |
2252 | if (SvTYPE(sv) < SVt_PVNV) |
2253 | sv_upgrade(sv, SVt_PVNV); | |
2254 | SvNOK_on(sv); | |
2255 | SvIOK_off(sv); | |
2256 | SvIOKp_on(sv); | |
9d6ce603 | 2257 | SvNV_set(sv, -(NV)value); |
45977657 | 2258 | SvIV_set(sv, IV_MIN); |
c2988b20 NC |
2259 | } |
2260 | } | |
2261 | } | |
2262 | /* For !NV_PRESERVES_UV and IS_NUMBER_IN_UV and IS_NUMBER_NOT_INT we | |
2263 | will be in the previous block to set the IV slot, and the next | |
2264 | block to set the NV slot. So no else here. */ | |
2265 | ||
2266 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) | |
2267 | != IS_NUMBER_IN_UV) { | |
2268 | /* It wasn't an (integer that doesn't overflow the UV). */ | |
3f7c398e | 2269 | SvNV_set(sv, Atof(SvPVX_const(sv))); |
28e5dec8 | 2270 | |
c2988b20 NC |
2271 | if (! numtype && ckWARN(WARN_NUMERIC)) |
2272 | not_a_number(sv); | |
28e5dec8 | 2273 | |
65202027 | 2274 | #if defined(USE_LONG_DOUBLE) |
c2988b20 NC |
2275 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n", |
2276 | PTR2UV(sv), SvNVX(sv))); | |
65202027 | 2277 | #else |
1779d84d | 2278 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"NVgf")\n", |
c2988b20 | 2279 | PTR2UV(sv), SvNVX(sv))); |
65202027 | 2280 | #endif |
28e5dec8 JH |
2281 | |
2282 | ||
2283 | #ifdef NV_PRESERVES_UV | |
c2988b20 NC |
2284 | (void)SvIOKp_on(sv); |
2285 | (void)SvNOK_on(sv); | |
2286 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
45977657 | 2287 | SvIV_set(sv, I_V(SvNVX(sv))); |
c2988b20 NC |
2288 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) { |
2289 | SvIOK_on(sv); | |
28e5dec8 | 2290 | } else { |
c2988b20 NC |
2291 | /* Integer is imprecise. NOK, IOKp */ |
2292 | } | |
2293 | /* UV will not work better than IV */ | |
2294 | } else { | |
2295 | if (SvNVX(sv) > (NV)UV_MAX) { | |
2296 | SvIsUV_on(sv); | |
2297 | /* Integer is inaccurate. NOK, IOKp, is UV */ | |
607fa7f2 | 2298 | SvUV_set(sv, UV_MAX); |
c2988b20 NC |
2299 | SvIsUV_on(sv); |
2300 | } else { | |
607fa7f2 | 2301 | SvUV_set(sv, U_V(SvNVX(sv))); |
c2988b20 NC |
2302 | /* 0xFFFFFFFFFFFFFFFF not an issue in here */ |
2303 | if ((NV)(SvUVX(sv)) == SvNVX(sv)) { | |
2304 | SvIOK_on(sv); | |
28e5dec8 JH |
2305 | SvIsUV_on(sv); |
2306 | } else { | |
c2988b20 NC |
2307 | /* Integer is imprecise. NOK, IOKp, is UV */ |
2308 | SvIsUV_on(sv); | |
28e5dec8 | 2309 | } |
28e5dec8 | 2310 | } |
c2988b20 NC |
2311 | goto ret_iv_max; |
2312 | } | |
28e5dec8 | 2313 | #else /* NV_PRESERVES_UV */ |
c2988b20 NC |
2314 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) |
2315 | == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) { | |
2316 | /* The IV slot will have been set from value returned by | |
2317 | grok_number above. The NV slot has just been set using | |
2318 | Atof. */ | |
560b0c46 | 2319 | SvNOK_on(sv); |
c2988b20 NC |
2320 | assert (SvIOKp(sv)); |
2321 | } else { | |
2322 | if (((UV)1 << NV_PRESERVES_UV_BITS) > | |
2323 | U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) { | |
2324 | /* Small enough to preserve all bits. */ | |
2325 | (void)SvIOKp_on(sv); | |
2326 | SvNOK_on(sv); | |
45977657 | 2327 | SvIV_set(sv, I_V(SvNVX(sv))); |
c2988b20 NC |
2328 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) |
2329 | SvIOK_on(sv); | |
2330 | /* Assumption: first non-preserved integer is < IV_MAX, | |
2331 | this NV is in the preserved range, therefore: */ | |
2332 | if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv)) | |
2333 | < (UV)IV_MAX)) { | |
32fdb065 | 2334 | Perl_croak(aTHX_ "sv_2iv assumed (U_V(fabs((double)SvNVX(sv))) < (UV)IV_MAX) but SvNVX(sv)=%"NVgf" U_V is 0x%"UVxf", IV_MAX is 0x%"UVxf"\n", SvNVX(sv), U_V(SvNVX(sv)), (UV)IV_MAX); |
c2988b20 NC |
2335 | } |
2336 | } else { | |
2337 | /* IN_UV NOT_INT | |
2338 | 0 0 already failed to read UV. | |
2339 | 0 1 already failed to read UV. | |
2340 | 1 0 you won't get here in this case. IV/UV | |
2341 | slot set, public IOK, Atof() unneeded. | |
2342 | 1 1 already read UV. | |
2343 | so there's no point in sv_2iuv_non_preserve() attempting | |
2344 | to use atol, strtol, strtoul etc. */ | |
2345 | if (sv_2iuv_non_preserve (sv, numtype) | |
2346 | >= IS_NUMBER_OVERFLOW_IV) | |
2347 | goto ret_iv_max; | |
2348 | } | |
2349 | } | |
28e5dec8 | 2350 | #endif /* NV_PRESERVES_UV */ |
25da4f38 | 2351 | } |
28e5dec8 | 2352 | } else { |
599cee73 | 2353 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP)) |
29489e7c | 2354 | report_uninit(sv); |
25da4f38 IZ |
2355 | if (SvTYPE(sv) < SVt_IV) |
2356 | /* Typically the caller expects that sv_any is not NULL now. */ | |
2357 | sv_upgrade(sv, SVt_IV); | |
a0d0e21e | 2358 | return 0; |
79072805 | 2359 | } |
1d7c1841 GS |
2360 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n", |
2361 | PTR2UV(sv),SvIVX(sv))); | |
25da4f38 | 2362 | return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv); |
79072805 LW |
2363 | } |
2364 | ||
891f9566 YST |
2365 | /* sv_2uv() is now a macro using Perl_sv_2uv_flags(); |
2366 | * this function provided for binary compatibility only | |
2367 | */ | |
2368 | ||
2369 | UV | |
2370 | Perl_sv_2uv(pTHX_ register SV *sv) | |
2371 | { | |
2372 | return sv_2uv_flags(sv, SV_GMAGIC); | |
2373 | } | |
2374 | ||
645c22ef | 2375 | /* |
891f9566 | 2376 | =for apidoc sv_2uv_flags |
645c22ef DM |
2377 | |
2378 | Return the unsigned integer value of an SV, doing any necessary string | |
891f9566 YST |
2379 | conversion. If flags includes SV_GMAGIC, does an mg_get() first. |
2380 | Normally used via the C<SvUV(sv)> and C<SvUVx(sv)> macros. | |
645c22ef DM |
2381 | |
2382 | =cut | |
2383 | */ | |
2384 | ||
ff68c719 | 2385 | UV |
891f9566 | 2386 | Perl_sv_2uv_flags(pTHX_ register SV *sv, I32 flags) |
ff68c719 | 2387 | { |
2388 | if (!sv) | |
2389 | return 0; | |
2390 | if (SvGMAGICAL(sv)) { | |
891f9566 YST |
2391 | if (flags & SV_GMAGIC) |
2392 | mg_get(sv); | |
ff68c719 | 2393 | if (SvIOKp(sv)) |
2394 | return SvUVX(sv); | |
2395 | if (SvNOKp(sv)) | |
2396 | return U_V(SvNVX(sv)); | |
36477c24 | 2397 | if (SvPOKp(sv) && SvLEN(sv)) |
2398 | return asUV(sv); | |
3fe9a6f1 | 2399 | if (!SvROK(sv)) { |
d008e5eb | 2400 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 2401 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
29489e7c | 2402 | report_uninit(sv); |
c6ee37c5 | 2403 | } |
36477c24 | 2404 | return 0; |
3fe9a6f1 | 2405 | } |
ff68c719 | 2406 | } |
2407 | if (SvTHINKFIRST(sv)) { | |
2408 | if (SvROK(sv)) { | |
ff68c719 | 2409 | SV* tmpstr; |
1554e226 | 2410 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) && |
b4b9a328 | 2411 | (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) |
9e7bc3e8 | 2412 | return SvUV(tmpstr); |
56431972 | 2413 | return PTR2UV(SvRV(sv)); |
ff68c719 | 2414 | } |
765f542d NC |
2415 | if (SvIsCOW(sv)) { |
2416 | sv_force_normal_flags(sv, 0); | |
8a818333 | 2417 | } |
0336b60e | 2418 | if (SvREADONLY(sv) && !SvOK(sv)) { |
0336b60e | 2419 | if (ckWARN(WARN_UNINITIALIZED)) |
29489e7c | 2420 | report_uninit(sv); |
ff68c719 | 2421 | return 0; |
2422 | } | |
2423 | } | |
25da4f38 IZ |
2424 | if (SvIOKp(sv)) { |
2425 | if (SvIsUV(sv)) { | |
2426 | return SvUVX(sv); | |
2427 | } | |
2428 | else { | |
2429 | return (UV)SvIVX(sv); | |
2430 | } | |
ff68c719 | 2431 | } |
2432 | if (SvNOKp(sv)) { | |
28e5dec8 JH |
2433 | /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv |
2434 | * without also getting a cached IV/UV from it at the same time | |
2435 | * (ie PV->NV conversion should detect loss of accuracy and cache | |
2436 | * IV or UV at same time to avoid this. */ | |
2437 | /* IV-over-UV optimisation - choose to cache IV if possible */ | |
2438 | ||
25da4f38 IZ |
2439 | if (SvTYPE(sv) == SVt_NV) |
2440 | sv_upgrade(sv, SVt_PVNV); | |
28e5dec8 JH |
2441 | |
2442 | (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */ | |
2443 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
45977657 | 2444 | SvIV_set(sv, I_V(SvNVX(sv))); |
28e5dec8 JH |
2445 | if (SvNVX(sv) == (NV) SvIVX(sv) |
2446 | #ifndef NV_PRESERVES_UV | |
2447 | && (((UV)1 << NV_PRESERVES_UV_BITS) > | |
2448 | (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv))) | |
2449 | /* Don't flag it as "accurately an integer" if the number | |
2450 | came from a (by definition imprecise) NV operation, and | |
2451 | we're outside the range of NV integer precision */ | |
2452 | #endif | |
2453 | ) { | |
2454 | SvIOK_on(sv); /* Can this go wrong with rounding? NWC */ | |
2455 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
7234c960 | 2456 | "0x%"UVxf" uv(%"NVgf" => %"IVdf") (precise)\n", |
28e5dec8 JH |
2457 | PTR2UV(sv), |
2458 | SvNVX(sv), | |
2459 | SvIVX(sv))); | |
2460 | ||
2461 | } else { | |
2462 | /* IV not precise. No need to convert from PV, as NV | |
2463 | conversion would already have cached IV if it detected | |
2464 | that PV->IV would be better than PV->NV->IV | |
2465 | flags already correct - don't set public IOK. */ | |
2466 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
7234c960 | 2467 | "0x%"UVxf" uv(%"NVgf" => %"IVdf") (imprecise)\n", |
28e5dec8 JH |
2468 | PTR2UV(sv), |
2469 | SvNVX(sv), | |
2470 | SvIVX(sv))); | |
2471 | } | |
2472 | /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN, | |
2473 | but the cast (NV)IV_MIN rounds to a the value less (more | |
2474 | negative) than IV_MIN which happens to be equal to SvNVX ?? | |
2475 | Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and | |
2476 | NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and | |
2477 | (NV)UVX == NVX are both true, but the values differ. :-( | |
2478 | Hopefully for 2s complement IV_MIN is something like | |
2479 | 0x8000000000000000 which will be exact. NWC */ | |
d460ef45 | 2480 | } |
28e5dec8 | 2481 | else { |
607fa7f2 | 2482 | SvUV_set(sv, U_V(SvNVX(sv))); |
28e5dec8 JH |
2483 | if ( |
2484 | (SvNVX(sv) == (NV) SvUVX(sv)) | |
2485 | #ifndef NV_PRESERVES_UV | |
2486 | /* Make sure it's not 0xFFFFFFFFFFFFFFFF */ | |
2487 | /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */ | |
2488 | && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv)) | |
2489 | /* Don't flag it as "accurately an integer" if the number | |
2490 | came from a (by definition imprecise) NV operation, and | |
2491 | we're outside the range of NV integer precision */ | |
2492 | #endif | |
2493 | ) | |
2494 | SvIOK_on(sv); | |
2495 | SvIsUV_on(sv); | |
1c846c1f | 2496 | DEBUG_c(PerlIO_printf(Perl_debug_log, |
28e5dec8 | 2497 | "0x%"UVxf" 2uv(%"UVuf" => %"IVdf") (as unsigned)\n", |
57def98f | 2498 | PTR2UV(sv), |
28e5dec8 JH |
2499 | SvUVX(sv), |
2500 | SvUVX(sv))); | |
25da4f38 | 2501 | } |
ff68c719 | 2502 | } |
2503 | else if (SvPOKp(sv) && SvLEN(sv)) { | |
c2988b20 | 2504 | UV value; |
504618e9 | 2505 | const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value); |
25da4f38 IZ |
2506 | |
2507 | /* We want to avoid a possible problem when we cache a UV which | |
2508 | may be later translated to an NV, and the resulting NV is not | |
2509 | the translation of the initial data. | |
1c846c1f | 2510 | |
25da4f38 IZ |
2511 | This means that if we cache such a UV, we need to cache the |
2512 | NV as well. Moreover, we trade speed for space, and do not | |
2513 | cache the NV if not needed. | |
2514 | */ | |
16b7a9a4 | 2515 | |
c2988b20 NC |
2516 | /* SVt_PVNV is one higher than SVt_PVIV, hence this order */ |
2517 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) | |
2518 | == IS_NUMBER_IN_UV) { | |
5e045b90 | 2519 | /* It's definitely an integer, only upgrade to PVIV */ |
28e5dec8 | 2520 | if (SvTYPE(sv) < SVt_PVIV) |
f7bbb42a JH |
2521 | sv_upgrade(sv, SVt_PVIV); |
2522 | (void)SvIOK_on(sv); | |
c2988b20 NC |
2523 | } else if (SvTYPE(sv) < SVt_PVNV) |
2524 | sv_upgrade(sv, SVt_PVNV); | |
d460ef45 | 2525 | |
c2988b20 NC |
2526 | /* If NV preserves UV then we only use the UV value if we know that |
2527 | we aren't going to call atof() below. If NVs don't preserve UVs | |
2528 | then the value returned may have more precision than atof() will | |
2529 | return, even though it isn't accurate. */ | |
2530 | if ((numtype & (IS_NUMBER_IN_UV | |
2531 | #ifdef NV_PRESERVES_UV | |
2532 | | IS_NUMBER_NOT_INT | |
2533 | #endif | |
2534 | )) == IS_NUMBER_IN_UV) { | |
2535 | /* This won't turn off the public IOK flag if it was set above */ | |
2536 | (void)SvIOKp_on(sv); | |
2537 | ||
2538 | if (!(numtype & IS_NUMBER_NEG)) { | |
2539 | /* positive */; | |
2540 | if (value <= (UV)IV_MAX) { | |
45977657 | 2541 | SvIV_set(sv, (IV)value); |
28e5dec8 JH |
2542 | } else { |
2543 | /* it didn't overflow, and it was positive. */ | |
607fa7f2 | 2544 | SvUV_set(sv, value); |
28e5dec8 JH |
2545 | SvIsUV_on(sv); |
2546 | } | |
c2988b20 NC |
2547 | } else { |
2548 | /* 2s complement assumption */ | |
2549 | if (value <= (UV)IV_MIN) { | |
45977657 | 2550 | SvIV_set(sv, -(IV)value); |
c2988b20 NC |
2551 | } else { |
2552 | /* Too negative for an IV. This is a double upgrade, but | |
d1be9408 | 2553 | I'm assuming it will be rare. */ |
c2988b20 NC |
2554 | if (SvTYPE(sv) < SVt_PVNV) |
2555 | sv_upgrade(sv, SVt_PVNV); | |
2556 | SvNOK_on(sv); | |
2557 | SvIOK_off(sv); | |
2558 | SvIOKp_on(sv); | |
9d6ce603 | 2559 | SvNV_set(sv, -(NV)value); |
45977657 | 2560 | SvIV_set(sv, IV_MIN); |
c2988b20 NC |
2561 | } |
2562 | } | |
2563 | } | |
2564 | ||
2565 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) | |
2566 | != IS_NUMBER_IN_UV) { | |
2567 | /* It wasn't an integer, or it overflowed the UV. */ | |
3f7c398e | 2568 | SvNV_set(sv, Atof(SvPVX_const(sv))); |
28e5dec8 | 2569 | |
c2988b20 | 2570 | if (! numtype && ckWARN(WARN_NUMERIC)) |
28e5dec8 JH |
2571 | not_a_number(sv); |
2572 | ||
2573 | #if defined(USE_LONG_DOUBLE) | |
c2988b20 NC |
2574 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%" PERL_PRIgldbl ")\n", |
2575 | PTR2UV(sv), SvNVX(sv))); | |
28e5dec8 | 2576 | #else |
1779d84d | 2577 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"NVgf")\n", |
c2988b20 | 2578 | PTR2UV(sv), SvNVX(sv))); |
28e5dec8 JH |
2579 | #endif |
2580 | ||
2581 | #ifdef NV_PRESERVES_UV | |
c2988b20 NC |
2582 | (void)SvIOKp_on(sv); |
2583 | (void)SvNOK_on(sv); | |
2584 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
45977657 | 2585 | SvIV_set(sv, I_V(SvNVX(sv))); |
c2988b20 NC |
2586 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) { |
2587 | SvIOK_on(sv); | |
2588 | } else { | |
2589 | /* Integer is imprecise. NOK, IOKp */ | |
2590 | } | |
2591 | /* UV will not work better than IV */ | |
2592 | } else { | |
2593 | if (SvNVX(sv) > (NV)UV_MAX) { | |
2594 | SvIsUV_on(sv); | |
2595 | /* Integer is inaccurate. NOK, IOKp, is UV */ | |
607fa7f2 | 2596 | SvUV_set(sv, UV_MAX); |
c2988b20 NC |
2597 | SvIsUV_on(sv); |
2598 | } else { | |
607fa7f2 | 2599 | SvUV_set(sv, U_V(SvNVX(sv))); |
c2988b20 NC |
2600 | /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs |
2601 | NV preservse UV so can do correct comparison. */ | |
2602 | if ((NV)(SvUVX(sv)) == SvNVX(sv)) { | |
2603 | SvIOK_on(sv); | |
2604 | SvIsUV_on(sv); | |
2605 | } else { | |
2606 | /* Integer is imprecise. NOK, IOKp, is UV */ | |
2607 | SvIsUV_on(sv); | |
2608 | } | |
2609 | } | |
2610 | } | |
28e5dec8 | 2611 | #else /* NV_PRESERVES_UV */ |
c2988b20 NC |
2612 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) |
2613 | == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) { | |
2614 | /* The UV slot will have been set from value returned by | |
2615 | grok_number above. The NV slot has just been set using | |
2616 | Atof. */ | |
560b0c46 | 2617 | SvNOK_on(sv); |
c2988b20 NC |
2618 | assert (SvIOKp(sv)); |
2619 | } else { | |
2620 | if (((UV)1 << NV_PRESERVES_UV_BITS) > | |
2621 | U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) { | |
2622 | /* Small enough to preserve all bits. */ | |
2623 | (void)SvIOKp_on(sv); | |
2624 | SvNOK_on(sv); | |
45977657 | 2625 | SvIV_set(sv, I_V(SvNVX(sv))); |
c2988b20 NC |
2626 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) |
2627 | SvIOK_on(sv); | |
2628 | /* Assumption: first non-preserved integer is < IV_MAX, | |
2629 | this NV is in the preserved range, therefore: */ | |
2630 | if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv)) | |
2631 | < (UV)IV_MAX)) { | |
32fdb065 | 2632 | Perl_croak(aTHX_ "sv_2uv assumed (U_V(fabs((double)SvNVX(sv))) < (UV)IV_MAX) but SvNVX(sv)=%"NVgf" U_V is 0x%"UVxf", IV_MAX is 0x%"UVxf"\n", SvNVX(sv), U_V(SvNVX(sv)), (UV)IV_MAX); |
c2988b20 NC |
2633 | } |
2634 | } else | |
2635 | sv_2iuv_non_preserve (sv, numtype); | |
2636 | } | |
28e5dec8 | 2637 | #endif /* NV_PRESERVES_UV */ |
f7bbb42a | 2638 | } |
ff68c719 | 2639 | } |
2640 | else { | |
d008e5eb | 2641 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 2642 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
29489e7c | 2643 | report_uninit(sv); |
c6ee37c5 | 2644 | } |
25da4f38 IZ |
2645 | if (SvTYPE(sv) < SVt_IV) |
2646 | /* Typically the caller expects that sv_any is not NULL now. */ | |
2647 | sv_upgrade(sv, SVt_IV); | |
ff68c719 | 2648 | return 0; |
2649 | } | |
25da4f38 | 2650 | |
1d7c1841 GS |
2651 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n", |
2652 | PTR2UV(sv),SvUVX(sv))); | |
25da4f38 | 2653 | return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv); |
ff68c719 | 2654 | } |
2655 | ||
645c22ef DM |
2656 | /* |
2657 | =for apidoc sv_2nv | |
2658 | ||
2659 | Return the num value of an SV, doing any necessary string or integer | |
2660 | conversion, magic etc. Normally used via the C<SvNV(sv)> and C<SvNVx(sv)> | |
2661 | macros. | |
2662 | ||
2663 | =cut | |
2664 | */ | |
2665 | ||
65202027 | 2666 | NV |
864dbfa3 | 2667 | Perl_sv_2nv(pTHX_ register SV *sv) |
79072805 LW |
2668 | { |
2669 | if (!sv) | |
2670 | return 0.0; | |
8990e307 | 2671 | if (SvGMAGICAL(sv)) { |
463ee0b2 LW |
2672 | mg_get(sv); |
2673 | if (SvNOKp(sv)) | |
2674 | return SvNVX(sv); | |
a0d0e21e | 2675 | if (SvPOKp(sv) && SvLEN(sv)) { |
c2988b20 | 2676 | if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && |
504618e9 | 2677 | !grok_number(SvPVX_const(sv), SvCUR(sv), NULL)) |
a0d0e21e | 2678 | not_a_number(sv); |
3f7c398e | 2679 | return Atof(SvPVX_const(sv)); |
a0d0e21e | 2680 | } |
25da4f38 | 2681 | if (SvIOKp(sv)) { |
1c846c1f | 2682 | if (SvIsUV(sv)) |
65202027 | 2683 | return (NV)SvUVX(sv); |
25da4f38 | 2684 | else |
65202027 | 2685 | return (NV)SvIVX(sv); |
25da4f38 | 2686 | } |
16d20bd9 | 2687 | if (!SvROK(sv)) { |
d008e5eb | 2688 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 2689 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
29489e7c | 2690 | report_uninit(sv); |
c6ee37c5 | 2691 | } |
66a1b24b | 2692 | return (NV)0; |
16d20bd9 | 2693 | } |
463ee0b2 | 2694 | } |
ed6116ce | 2695 | if (SvTHINKFIRST(sv)) { |
a0d0e21e | 2696 | if (SvROK(sv)) { |
a0d0e21e | 2697 | SV* tmpstr; |
1554e226 | 2698 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) && |
b4b9a328 | 2699 | (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) |
9e7bc3e8 | 2700 | return SvNV(tmpstr); |
56431972 | 2701 | return PTR2NV(SvRV(sv)); |
a0d0e21e | 2702 | } |
765f542d NC |
2703 | if (SvIsCOW(sv)) { |
2704 | sv_force_normal_flags(sv, 0); | |
8a818333 | 2705 | } |
0336b60e | 2706 | if (SvREADONLY(sv) && !SvOK(sv)) { |
599cee73 | 2707 | if (ckWARN(WARN_UNINITIALIZED)) |
29489e7c | 2708 | report_uninit(sv); |
ed6116ce LW |
2709 | return 0.0; |
2710 | } | |
79072805 LW |
2711 | } |
2712 | if (SvTYPE(sv) < SVt_NV) { | |
463ee0b2 LW |
2713 | if (SvTYPE(sv) == SVt_IV) |
2714 | sv_upgrade(sv, SVt_PVNV); | |
2715 | else | |
2716 | sv_upgrade(sv, SVt_NV); | |
906f284f | 2717 | #ifdef USE_LONG_DOUBLE |
097ee67d | 2718 | DEBUG_c({ |
f93f4e46 | 2719 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
2720 | PerlIO_printf(Perl_debug_log, |
2721 | "0x%"UVxf" num(%" PERL_PRIgldbl ")\n", | |
2722 | PTR2UV(sv), SvNVX(sv)); | |
572bbb43 GS |
2723 | RESTORE_NUMERIC_LOCAL(); |
2724 | }); | |
65202027 | 2725 | #else |
572bbb43 | 2726 | DEBUG_c({ |
f93f4e46 | 2727 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1779d84d | 2728 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%"NVgf")\n", |
1d7c1841 | 2729 | PTR2UV(sv), SvNVX(sv)); |
097ee67d JH |
2730 | RESTORE_NUMERIC_LOCAL(); |
2731 | }); | |
572bbb43 | 2732 | #endif |
79072805 LW |
2733 | } |
2734 | else if (SvTYPE(sv) < SVt_PVNV) | |
2735 | sv_upgrade(sv, SVt_PVNV); | |
59d8ce62 NC |
2736 | if (SvNOKp(sv)) { |
2737 | return SvNVX(sv); | |
61604483 | 2738 | } |
59d8ce62 | 2739 | if (SvIOKp(sv)) { |
9d6ce603 | 2740 | SvNV_set(sv, SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv)); |
28e5dec8 JH |
2741 | #ifdef NV_PRESERVES_UV |
2742 | SvNOK_on(sv); | |
2743 | #else | |
2744 | /* Only set the public NV OK flag if this NV preserves the IV */ | |
2745 | /* Check it's not 0xFFFFFFFFFFFFFFFF */ | |
2746 | if (SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv)))) | |
2747 | : (SvIVX(sv) == I_V(SvNVX(sv)))) | |
2748 | SvNOK_on(sv); | |
2749 | else | |
2750 | SvNOKp_on(sv); | |
2751 | #endif | |
93a17b20 | 2752 | } |
748a9306 | 2753 | else if (SvPOKp(sv) && SvLEN(sv)) { |
c2988b20 | 2754 | UV value; |
3f7c398e | 2755 | const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value); |
c2988b20 | 2756 | if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !numtype) |
a0d0e21e | 2757 | not_a_number(sv); |
28e5dec8 | 2758 | #ifdef NV_PRESERVES_UV |
c2988b20 NC |
2759 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) |
2760 | == IS_NUMBER_IN_UV) { | |
5e045b90 | 2761 | /* It's definitely an integer */ |
9d6ce603 | 2762 | SvNV_set(sv, (numtype & IS_NUMBER_NEG) ? -(NV)value : (NV)value); |
c2988b20 | 2763 | } else |
3f7c398e | 2764 | SvNV_set(sv, Atof(SvPVX_const(sv))); |
28e5dec8 JH |
2765 | SvNOK_on(sv); |
2766 | #else | |
3f7c398e | 2767 | SvNV_set(sv, Atof(SvPVX_const(sv))); |
28e5dec8 JH |
2768 | /* Only set the public NV OK flag if this NV preserves the value in |
2769 | the PV at least as well as an IV/UV would. | |
2770 | Not sure how to do this 100% reliably. */ | |
2771 | /* if that shift count is out of range then Configure's test is | |
2772 | wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS == | |
2773 | UV_BITS */ | |
2774 | if (((UV)1 << NV_PRESERVES_UV_BITS) > | |
c2988b20 | 2775 | U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) { |
28e5dec8 | 2776 | SvNOK_on(sv); /* Definitely small enough to preserve all bits */ |
c2988b20 NC |
2777 | } else if (!(numtype & IS_NUMBER_IN_UV)) { |
2778 | /* Can't use strtol etc to convert this string, so don't try. | |
2779 | sv_2iv and sv_2uv will use the NV to convert, not the PV. */ | |
2780 | SvNOK_on(sv); | |
2781 | } else { | |
2782 | /* value has been set. It may not be precise. */ | |
2783 | if ((numtype & IS_NUMBER_NEG) && (value > (UV)IV_MIN)) { | |
2784 | /* 2s complement assumption for (UV)IV_MIN */ | |
2785 | SvNOK_on(sv); /* Integer is too negative. */ | |
2786 | } else { | |
2787 | SvNOKp_on(sv); | |
2788 | SvIOKp_on(sv); | |
6fa402ec | 2789 | |
c2988b20 | 2790 | if (numtype & IS_NUMBER_NEG) { |
45977657 | 2791 | SvIV_set(sv, -(IV)value); |
c2988b20 | 2792 | } else if (value <= (UV)IV_MAX) { |
45977657 | 2793 | SvIV_set(sv, (IV)value); |
c2988b20 | 2794 | } else { |
607fa7f2 | 2795 | SvUV_set(sv, value); |
c2988b20 NC |
2796 | SvIsUV_on(sv); |
2797 | } | |
2798 | ||
2799 | if (numtype & IS_NUMBER_NOT_INT) { | |
2800 | /* I believe that even if the original PV had decimals, | |
2801 | they are lost beyond the limit of the FP precision. | |
2802 | However, neither is canonical, so both only get p | |
2803 | flags. NWC, 2000/11/25 */ | |
2804 | /* Both already have p flags, so do nothing */ | |
2805 | } else { | |
66a1b24b | 2806 | const NV nv = SvNVX(sv); |
c2988b20 NC |
2807 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { |
2808 | if (SvIVX(sv) == I_V(nv)) { | |
2809 | SvNOK_on(sv); | |
2810 | SvIOK_on(sv); | |
2811 | } else { | |
2812 | SvIOK_on(sv); | |
2813 | /* It had no "." so it must be integer. */ | |
2814 | } | |
2815 | } else { | |
2816 | /* between IV_MAX and NV(UV_MAX). | |
2817 | Could be slightly > UV_MAX */ | |
6fa402ec | 2818 | |
c2988b20 NC |
2819 | if (numtype & IS_NUMBER_NOT_INT) { |
2820 | /* UV and NV both imprecise. */ | |
2821 | } else { | |
66a1b24b | 2822 | const UV nv_as_uv = U_V(nv); |
c2988b20 NC |
2823 | |
2824 | if (value == nv_as_uv && SvUVX(sv) != UV_MAX) { | |
2825 | SvNOK_on(sv); | |
2826 | SvIOK_on(sv); | |
2827 | } else { | |
2828 | SvIOK_on(sv); | |
2829 | } | |
2830 | } | |
2831 | } | |
2832 | } | |
2833 | } | |
2834 | } | |
28e5dec8 | 2835 | #endif /* NV_PRESERVES_UV */ |
93a17b20 | 2836 | } |
79072805 | 2837 | else { |
599cee73 | 2838 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP)) |
29489e7c | 2839 | report_uninit(sv); |
25da4f38 IZ |
2840 | if (SvTYPE(sv) < SVt_NV) |
2841 | /* Typically the caller expects that sv_any is not NULL now. */ | |
28e5dec8 JH |
2842 | /* XXX Ilya implies that this is a bug in callers that assume this |
2843 | and ideally should be fixed. */ | |
25da4f38 | 2844 | sv_upgrade(sv, SVt_NV); |
a0d0e21e | 2845 | return 0.0; |
79072805 | 2846 | } |
572bbb43 | 2847 | #if defined(USE_LONG_DOUBLE) |
097ee67d | 2848 | DEBUG_c({ |
f93f4e46 | 2849 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
2850 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n", |
2851 | PTR2UV(sv), SvNVX(sv)); | |
572bbb43 GS |
2852 | RESTORE_NUMERIC_LOCAL(); |
2853 | }); | |
65202027 | 2854 | #else |
572bbb43 | 2855 | DEBUG_c({ |
f93f4e46 | 2856 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1779d84d | 2857 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%"NVgf")\n", |
1d7c1841 | 2858 | PTR2UV(sv), SvNVX(sv)); |
097ee67d JH |
2859 | RESTORE_NUMERIC_LOCAL(); |
2860 | }); | |
572bbb43 | 2861 | #endif |
463ee0b2 | 2862 | return SvNVX(sv); |
79072805 LW |
2863 | } |
2864 | ||
645c22ef DM |
2865 | /* asIV(): extract an integer from the string value of an SV. |
2866 | * Caller must validate PVX */ | |
2867 | ||
76e3520e | 2868 | STATIC IV |
cea2e8a9 | 2869 | S_asIV(pTHX_ SV *sv) |
36477c24 | 2870 | { |
c2988b20 | 2871 | UV value; |
66a1b24b | 2872 | const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value); |
c2988b20 NC |
2873 | |
2874 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) | |
2875 | == IS_NUMBER_IN_UV) { | |
645c22ef | 2876 | /* It's definitely an integer */ |
c2988b20 NC |
2877 | if (numtype & IS_NUMBER_NEG) { |
2878 | if (value < (UV)IV_MIN) | |
2879 | return -(IV)value; | |
2880 | } else { | |
2881 | if (value < (UV)IV_MAX) | |
2882 | return (IV)value; | |
2883 | } | |
2884 | } | |
d008e5eb | 2885 | if (!numtype) { |
d008e5eb GS |
2886 | if (ckWARN(WARN_NUMERIC)) |
2887 | not_a_number(sv); | |
2888 | } | |
3f7c398e | 2889 | return I_V(Atof(SvPVX_const(sv))); |
36477c24 | 2890 | } |
2891 | ||
645c22ef DM |
2892 | /* asUV(): extract an unsigned integer from the string value of an SV |
2893 | * Caller must validate PVX */ | |
2894 | ||
76e3520e | 2895 | STATIC UV |
cea2e8a9 | 2896 | S_asUV(pTHX_ SV *sv) |
36477c24 | 2897 | { |
c2988b20 | 2898 | UV value; |
504618e9 | 2899 | const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value); |
36477c24 | 2900 | |
c2988b20 NC |
2901 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) |
2902 | == IS_NUMBER_IN_UV) { | |
645c22ef | 2903 | /* It's definitely an integer */ |
6fa402ec | 2904 | if (!(numtype & IS_NUMBER_NEG)) |
c2988b20 NC |
2905 | return value; |
2906 | } | |
d008e5eb | 2907 | if (!numtype) { |
d008e5eb GS |
2908 | if (ckWARN(WARN_NUMERIC)) |
2909 | not_a_number(sv); | |
2910 | } | |
3f7c398e | 2911 | return U_V(Atof(SvPVX_const(sv))); |
36477c24 | 2912 | } |
2913 | ||
645c22ef DM |
2914 | /* |
2915 | =for apidoc sv_2pv_nolen | |
2916 | ||
2917 | Like C<sv_2pv()>, but doesn't return the length too. You should usually | |
2918 | use the macro wrapper C<SvPV_nolen(sv)> instead. | |
2919 | =cut | |
2920 | */ | |
2921 | ||
79072805 | 2922 | char * |
864dbfa3 | 2923 | Perl_sv_2pv_nolen(pTHX_ register SV *sv) |
1fa8b10d | 2924 | { |
dafda6d1 | 2925 | return sv_2pv(sv, 0); |
1fa8b10d JD |
2926 | } |
2927 | ||
645c22ef DM |
2928 | /* uiv_2buf(): private routine for use by sv_2pv_flags(): print an IV or |
2929 | * UV as a string towards the end of buf, and return pointers to start and | |
2930 | * end of it. | |
2931 | * | |
2932 | * We assume that buf is at least TYPE_CHARS(UV) long. | |
2933 | */ | |
2934 | ||
864dbfa3 | 2935 | static char * |
25da4f38 IZ |
2936 | uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob) |
2937 | { | |
25da4f38 IZ |
2938 | char *ptr = buf + TYPE_CHARS(UV); |
2939 | char *ebuf = ptr; | |
2940 | int sign; | |
25da4f38 IZ |
2941 | |
2942 | if (is_uv) | |
2943 | sign = 0; | |
2944 | else if (iv >= 0) { | |
2945 | uv = iv; | |
2946 | sign = 0; | |
2947 | } else { | |
2948 | uv = -iv; | |
2949 | sign = 1; | |
2950 | } | |
2951 | do { | |
eb160463 | 2952 | *--ptr = '0' + (char)(uv % 10); |
25da4f38 IZ |
2953 | } while (uv /= 10); |
2954 | if (sign) | |
2955 | *--ptr = '-'; | |
2956 | *peob = ebuf; | |
2957 | return ptr; | |
2958 | } | |
2959 | ||
09540bc3 JH |
2960 | /* sv_2pv() is now a macro using Perl_sv_2pv_flags(); |
2961 | * this function provided for binary compatibility only | |
2962 | */ | |
2963 | ||
2964 | char * | |
2965 | Perl_sv_2pv(pTHX_ register SV *sv, STRLEN *lp) | |
2966 | { | |
2967 | return sv_2pv_flags(sv, lp, SV_GMAGIC); | |
2968 | } | |
2969 | ||
645c22ef DM |
2970 | /* |
2971 | =for apidoc sv_2pv_flags | |
2972 | ||
ff276b08 | 2973 | Returns a pointer to the string value of an SV, and sets *lp to its length. |
645c22ef DM |
2974 | If flags includes SV_GMAGIC, does an mg_get() first. Coerces sv to a string |
2975 | if necessary. | |
2976 | Normally invoked via the C<SvPV_flags> macro. C<sv_2pv()> and C<sv_2pv_nomg> | |
2977 | usually end up here too. | |
2978 | ||
2979 | =cut | |
2980 | */ | |
2981 | ||
8d6d96c1 HS |
2982 | char * |
2983 | Perl_sv_2pv_flags(pTHX_ register SV *sv, STRLEN *lp, I32 flags) | |
2984 | { | |
79072805 LW |
2985 | register char *s; |
2986 | int olderrno; | |
cb50f42d | 2987 | SV *tsv, *origsv; |
25da4f38 IZ |
2988 | char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */ |
2989 | char *tmpbuf = tbuf; | |
79072805 | 2990 | |
463ee0b2 | 2991 | if (!sv) { |
cdb061a3 NC |
2992 | if (lp) |
2993 | *lp = 0; | |
73d840c0 | 2994 | return (char *)""; |
463ee0b2 | 2995 | } |
8990e307 | 2996 | if (SvGMAGICAL(sv)) { |
8d6d96c1 HS |
2997 | if (flags & SV_GMAGIC) |
2998 | mg_get(sv); | |
463ee0b2 | 2999 | if (SvPOKp(sv)) { |
cdb061a3 NC |
3000 | if (lp) |
3001 | *lp = SvCUR(sv); | |
10516c54 NC |
3002 | if (flags & SV_MUTABLE_RETURN) |
3003 | return SvPVX_mutable(sv); | |
4d84ee25 NC |
3004 | if (flags & SV_CONST_RETURN) |
3005 | return (char *)SvPVX_const(sv); | |
463ee0b2 LW |
3006 | return SvPVX(sv); |
3007 | } | |
cf2093f6 | 3008 | if (SvIOKp(sv)) { |
1c846c1f | 3009 | if (SvIsUV(sv)) |
57def98f | 3010 | (void)sprintf(tmpbuf,"%"UVuf, (UV)SvUVX(sv)); |
cf2093f6 | 3011 | else |
57def98f | 3012 | (void)sprintf(tmpbuf,"%"IVdf, (IV)SvIVX(sv)); |
46fc3d4c | 3013 | tsv = Nullsv; |
a0d0e21e | 3014 | goto tokensave; |
463ee0b2 LW |
3015 | } |
3016 | if (SvNOKp(sv)) { | |
2d4389e4 | 3017 | Gconvert(SvNVX(sv), NV_DIG, 0, tmpbuf); |
46fc3d4c | 3018 | tsv = Nullsv; |
a0d0e21e | 3019 | goto tokensave; |
463ee0b2 | 3020 | } |
16d20bd9 | 3021 | if (!SvROK(sv)) { |
d008e5eb | 3022 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 3023 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
29489e7c | 3024 | report_uninit(sv); |
c6ee37c5 | 3025 | } |
cdb061a3 NC |
3026 | if (lp) |
3027 | *lp = 0; | |
73d840c0 | 3028 | return (char *)""; |
16d20bd9 | 3029 | } |
463ee0b2 | 3030 | } |
ed6116ce LW |
3031 | if (SvTHINKFIRST(sv)) { |
3032 | if (SvROK(sv)) { | |
a0d0e21e | 3033 | SV* tmpstr; |
e1ec3a88 | 3034 | register const char *typestr; |
1554e226 | 3035 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)) && |
b4b9a328 | 3036 | (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) { |
50adf7d2 NC |
3037 | /* Unwrap this: */ |
3038 | /* char *pv = lp ? SvPV(tmpstr, *lp) : SvPV_nolen(tmpstr); */ | |
3039 | ||
3040 | char *pv; | |
3041 | if ((SvFLAGS(tmpstr) & (SVf_POK)) == SVf_POK) { | |
3042 | if (flags & SV_CONST_RETURN) { | |
3043 | pv = (char *) SvPVX_const(tmpstr); | |
3044 | } else { | |
3045 | pv = (flags & SV_MUTABLE_RETURN) | |
3046 | ? SvPVX_mutable(tmpstr) : SvPVX(tmpstr); | |
3047 | } | |
3048 | if (lp) | |
3049 | *lp = SvCUR(tmpstr); | |
3050 | } else { | |
3051 | pv = sv_2pv_flags(tmpstr, lp, flags); | |
3052 | } | |
446eaa42 YST |
3053 | if (SvUTF8(tmpstr)) |
3054 | SvUTF8_on(sv); | |
3055 | else | |
3056 | SvUTF8_off(sv); | |
3057 | return pv; | |
3058 | } | |
cb50f42d | 3059 | origsv = sv; |
ed6116ce LW |
3060 | sv = (SV*)SvRV(sv); |
3061 | if (!sv) | |
e1ec3a88 | 3062 | typestr = "NULLREF"; |
ed6116ce | 3063 | else { |
f9277f47 IZ |
3064 | MAGIC *mg; |
3065 | ||
ed6116ce | 3066 | switch (SvTYPE(sv)) { |
f9277f47 IZ |
3067 | case SVt_PVMG: |
3068 | if ( ((SvFLAGS(sv) & | |
1c846c1f | 3069 | (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG)) |
faf82a0b | 3070 | == (SVs_OBJECT|SVs_SMG)) |
14befaf4 | 3071 | && (mg = mg_find(sv, PERL_MAGIC_qr))) { |
e1ec3a88 | 3072 | const regexp *re = (regexp *)mg->mg_obj; |
1bd3ad17 | 3073 | |
2cd61cdb | 3074 | if (!mg->mg_ptr) { |
e1ec3a88 | 3075 | const char *fptr = "msix"; |
8782bef2 GB |
3076 | char reflags[6]; |
3077 | char ch; | |
3078 | int left = 0; | |
3079 | int right = 4; | |
ff385a1b | 3080 | char need_newline = 0; |
eb160463 | 3081 | U16 reganch = (U16)((re->reganch & PMf_COMPILETIME) >> 12); |
8782bef2 | 3082 | |
155aba94 | 3083 | while((ch = *fptr++)) { |
8782bef2 GB |
3084 | if(reganch & 1) { |
3085 | reflags[left++] = ch; | |
3086 | } | |
3087 | else { | |
3088 | reflags[right--] = ch; | |
3089 | } | |
3090 | reganch >>= 1; | |
3091 | } | |
3092 | if(left != 4) { | |
3093 | reflags[left] = '-'; | |
3094 | left = 5; | |
3095 | } | |
3096 | ||
3097 | mg->mg_len = re->prelen + 4 + left; | |
ff385a1b JF |
3098 | /* |
3099 | * If /x was used, we have to worry about a regex | |
3100 | * ending with a comment later being embedded | |
3101 | * within another regex. If so, we don't want this | |
3102 | * regex's "commentization" to leak out to the | |
3103 | * right part of the enclosing regex, we must cap | |
3104 | * it with a newline. | |
3105 | * | |
3106 | * So, if /x was used, we scan backwards from the | |
3107 | * end of the regex. If we find a '#' before we | |
3108 | * find a newline, we need to add a newline | |
3109 | * ourself. If we find a '\n' first (or if we | |
3110 | * don't find '#' or '\n'), we don't need to add | |
3111 | * anything. -jfriedl | |
3112 | */ | |
3113 | if (PMf_EXTENDED & re->reganch) | |
3114 | { | |
e1ec3a88 | 3115 | const char *endptr = re->precomp + re->prelen; |
ff385a1b JF |
3116 | while (endptr >= re->precomp) |
3117 | { | |
e1ec3a88 | 3118 | const char c = *(endptr--); |
ff385a1b JF |
3119 | if (c == '\n') |
3120 | break; /* don't need another */ | |
3121 | if (c == '#') { | |
3122 | /* we end while in a comment, so we | |
3123 | need a newline */ | |
3124 | mg->mg_len++; /* save space for it */ | |
3125 | need_newline = 1; /* note to add it */ | |
ab01544f | 3126 | break; |
ff385a1b JF |
3127 | } |
3128 | } | |
3129 | } | |
3130 | ||
8782bef2 GB |
3131 | New(616, mg->mg_ptr, mg->mg_len + 1 + left, char); |
3132 | Copy("(?", mg->mg_ptr, 2, char); | |
3133 | Copy(reflags, mg->mg_ptr+2, left, char); | |
3134 | Copy(":", mg->mg_ptr+left+2, 1, char); | |
3135 | Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char); | |
ff385a1b JF |
3136 | if (need_newline) |
3137 | mg->mg_ptr[mg->mg_len - 2] = '\n'; | |
1bd3ad17 IZ |
3138 | mg->mg_ptr[mg->mg_len - 1] = ')'; |
3139 | mg->mg_ptr[mg->mg_len] = 0; | |
3140 | } | |
3280af22 | 3141 | PL_reginterp_cnt += re->program[0].next_off; |
cb50f42d YST |
3142 | |
3143 | if (re->reganch & ROPT_UTF8) | |
3144 | SvUTF8_on(origsv); | |
3145 | else | |
3146 | SvUTF8_off(origsv); | |
cdb061a3 NC |
3147 | if (lp) |
3148 | *lp = mg->mg_len; | |
1bd3ad17 | 3149 | return mg->mg_ptr; |
f9277f47 IZ |
3150 | } |
3151 | /* Fall through */ | |
ed6116ce LW |
3152 | case SVt_NULL: |
3153 | case SVt_IV: | |
3154 | case SVt_NV: | |
3155 | case SVt_RV: | |
3156 | case SVt_PV: | |
3157 | case SVt_PVIV: | |
3158 | case SVt_PVNV: | |
e1ec3a88 AL |
3159 | case SVt_PVBM: typestr = SvROK(sv) ? "REF" : "SCALAR"; break; |
3160 | case SVt_PVLV: typestr = SvROK(sv) ? "REF" | |
be65207d DM |
3161 | /* tied lvalues should appear to be |
3162 | * scalars for backwards compatitbility */ | |
3163 | : (LvTYPE(sv) == 't' || LvTYPE(sv) == 'T') | |
3164 | ? "SCALAR" : "LVALUE"; break; | |
e1ec3a88 AL |
3165 | case SVt_PVAV: typestr = "ARRAY"; break; |
3166 | case SVt_PVHV: typestr = "HASH"; break; | |
3167 | case SVt_PVCV: typestr = "CODE"; break; | |
3168 | case SVt_PVGV: typestr = "GLOB"; break; | |
3169 | case SVt_PVFM: typestr = "FORMAT"; break; | |
3170 | case SVt_PVIO: typestr = "IO"; break; | |
3171 | default: typestr = "UNKNOWN"; break; | |
ed6116ce | 3172 | } |
46fc3d4c | 3173 | tsv = NEWSV(0,0); |
a5cb6b62 | 3174 | if (SvOBJECT(sv)) { |
bfcb3514 | 3175 | const char *name = HvNAME_get(SvSTASH(sv)); |
a5cb6b62 | 3176 | Perl_sv_setpvf(aTHX_ tsv, "%s=%s(0x%"UVxf")", |
e1ec3a88 | 3177 | name ? name : "__ANON__" , typestr, PTR2UV(sv)); |
a5cb6b62 | 3178 | } |
ed6116ce | 3179 | else |
e1ec3a88 | 3180 | Perl_sv_setpvf(aTHX_ tsv, "%s(0x%"UVxf")", typestr, PTR2UV(sv)); |
a0d0e21e | 3181 | goto tokensaveref; |
463ee0b2 | 3182 | } |
cdb061a3 NC |
3183 | if (lp) |
3184 | *lp = strlen(typestr); | |
73d840c0 | 3185 | return (char *)typestr; |
79072805 | 3186 | } |
0336b60e | 3187 | if (SvREADONLY(sv) && !SvOK(sv)) { |
0336b60e | 3188 | if (ckWARN(WARN_UNINITIALIZED)) |
29489e7c | 3189 | report_uninit(sv); |
cdb061a3 NC |
3190 | if (lp) |
3191 | *lp = 0; | |
73d840c0 | 3192 | return (char *)""; |
79072805 | 3193 | } |
79072805 | 3194 | } |
28e5dec8 JH |
3195 | if (SvIOK(sv) || ((SvIOKp(sv) && !SvNOKp(sv)))) { |
3196 | /* I'm assuming that if both IV and NV are equally valid then | |
3197 | converting the IV is going to be more efficient */ | |
e1ec3a88 AL |
3198 | const U32 isIOK = SvIOK(sv); |
3199 | const U32 isUIOK = SvIsUV(sv); | |
28e5dec8 JH |
3200 | char buf[TYPE_CHARS(UV)]; |
3201 | char *ebuf, *ptr; | |
3202 | ||
3203 | if (SvTYPE(sv) < SVt_PVIV) | |
3204 | sv_upgrade(sv, SVt_PVIV); | |
3205 | if (isUIOK) | |
3206 | ptr = uiv_2buf(buf, 0, SvUVX(sv), 1, &ebuf); | |
3207 | else | |
3208 | ptr = uiv_2buf(buf, SvIVX(sv), 0, 0, &ebuf); | |
5902b6a9 NC |
3209 | /* inlined from sv_setpvn */ |
3210 | SvGROW_mutable(sv, (STRLEN)(ebuf - ptr + 1)); | |
4d84ee25 | 3211 | Move(ptr,SvPVX_mutable(sv),ebuf - ptr,char); |
28e5dec8 JH |
3212 | SvCUR_set(sv, ebuf - ptr); |
3213 | s = SvEND(sv); | |
3214 | *s = '\0'; | |
3215 | if (isIOK) | |
3216 | SvIOK_on(sv); | |
3217 | else | |
3218 | SvIOKp_on(sv); | |
3219 | if (isUIOK) | |
3220 | SvIsUV_on(sv); | |
3221 | } | |
3222 | else if (SvNOKp(sv)) { | |
79072805 LW |
3223 | if (SvTYPE(sv) < SVt_PVNV) |
3224 | sv_upgrade(sv, SVt_PVNV); | |
1c846c1f | 3225 | /* The +20 is pure guesswork. Configure test needed. --jhi */ |
5902b6a9 | 3226 | s = SvGROW_mutable(sv, NV_DIG + 20); |
79072805 | 3227 | olderrno = errno; /* some Xenix systems wipe out errno here */ |
79072805 | 3228 | #ifdef apollo |
463ee0b2 | 3229 | if (SvNVX(sv) == 0.0) |
79072805 LW |
3230 | (void)strcpy(s,"0"); |
3231 | else | |
3232 | #endif /*apollo*/ | |
bbce6d69 | 3233 | { |
2d4389e4 | 3234 | Gconvert(SvNVX(sv), NV_DIG, 0, s); |
bbce6d69 | 3235 | } |
79072805 | 3236 | errno = olderrno; |
a0d0e21e LW |
3237 | #ifdef FIXNEGATIVEZERO |
3238 | if (*s == '-' && s[1] == '0' && !s[2]) | |
3239 | strcpy(s,"0"); | |
3240 | #endif | |
79072805 LW |
3241 | while (*s) s++; |
3242 | #ifdef hcx | |
3243 | if (s[-1] == '.') | |
46fc3d4c | 3244 | *--s = '\0'; |
79072805 LW |
3245 | #endif |
3246 | } | |
79072805 | 3247 | else { |
0336b60e IZ |
3248 | if (ckWARN(WARN_UNINITIALIZED) |
3249 | && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP)) | |
29489e7c | 3250 | report_uninit(sv); |
cdb061a3 | 3251 | if (lp) |
a0d0e21e | 3252 | *lp = 0; |
25da4f38 IZ |
3253 | if (SvTYPE(sv) < SVt_PV) |
3254 | /* Typically the caller expects that sv_any is not NULL now. */ | |
3255 | sv_upgrade(sv, SVt_PV); | |
73d840c0 | 3256 | return (char *)""; |
79072805 | 3257 | } |
cdb061a3 NC |
3258 | { |
3259 | STRLEN len = s - SvPVX_const(sv); | |
3260 | if (lp) | |
3261 | *lp = len; | |
3262 | SvCUR_set(sv, len); | |
3263 | } | |
79072805 | 3264 | SvPOK_on(sv); |
1d7c1841 | 3265 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n", |
3f7c398e | 3266 | PTR2UV(sv),SvPVX_const(sv))); |
4d84ee25 NC |
3267 | if (flags & SV_CONST_RETURN) |
3268 | return (char *)SvPVX_const(sv); | |
10516c54 NC |
3269 | if (flags & SV_MUTABLE_RETURN) |
3270 | return SvPVX_mutable(sv); | |
463ee0b2 | 3271 | return SvPVX(sv); |
a0d0e21e LW |
3272 | |
3273 | tokensave: | |
3274 | if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */ | |
3275 | /* Sneaky stuff here */ | |
3276 | ||
3277 | tokensaveref: | |
46fc3d4c | 3278 | if (!tsv) |
96827780 | 3279 | tsv = newSVpv(tmpbuf, 0); |
46fc3d4c | 3280 | sv_2mortal(tsv); |
cdb061a3 NC |
3281 | if (lp) |
3282 | *lp = SvCUR(tsv); | |
46fc3d4c | 3283 | return SvPVX(tsv); |
a0d0e21e LW |
3284 | } |
3285 | else { | |
27da23d5 | 3286 | dVAR; |
a0d0e21e | 3287 | STRLEN len; |
73d840c0 | 3288 | const char *t; |
46fc3d4c | 3289 | |
3290 | if (tsv) { | |
3291 | sv_2mortal(tsv); | |
3f7c398e | 3292 | t = SvPVX_const(tsv); |
46fc3d4c | 3293 | len = SvCUR(tsv); |
3294 | } | |
3295 | else { | |
96827780 MB |
3296 | t = tmpbuf; |
3297 | len = strlen(tmpbuf); | |
46fc3d4c | 3298 | } |
a0d0e21e | 3299 | #ifdef FIXNEGATIVEZERO |
46fc3d4c | 3300 | if (len == 2 && t[0] == '-' && t[1] == '0') { |
3301 | t = "0"; | |
3302 | len = 1; | |
3303 | } | |
a0d0e21e | 3304 | #endif |
862a34c6 | 3305 | SvUPGRADE(sv, SVt_PV); |
cdb061a3 NC |
3306 | if (lp) |
3307 | *lp = len; | |
5902b6a9 | 3308 | s = SvGROW_mutable(sv, len + 1); |
a0d0e21e | 3309 | SvCUR_set(sv, len); |
6bf554b4 | 3310 | SvPOKp_on(sv); |
e90e2364 | 3311 | return strcpy(s, t); |
a0d0e21e | 3312 | } |
463ee0b2 LW |
3313 | } |
3314 | ||
645c22ef | 3315 | /* |
6050d10e JP |
3316 | =for apidoc sv_copypv |
3317 | ||
3318 | Copies a stringified representation of the source SV into the | |
3319 | destination SV. Automatically performs any necessary mg_get and | |
54f0641b | 3320 | coercion of numeric values into strings. Guaranteed to preserve |
6050d10e | 3321 | UTF-8 flag even from overloaded objects. Similar in nature to |
54f0641b NIS |
3322 | sv_2pv[_flags] but operates directly on an SV instead of just the |
3323 | string. Mostly uses sv_2pv_flags to do its work, except when that | |
6050d10e JP |
3324 | would lose the UTF-8'ness of the PV. |
3325 | ||
3326 | =cut | |
3327 | */ | |
3328 | ||
3329 | void | |
3330 | Perl_sv_copypv(pTHX_ SV *dsv, register SV *ssv) | |
3331 | { | |
446eaa42 | 3332 | STRLEN len; |
53c1dcc0 | 3333 | const char * const s = SvPV_const(ssv,len); |
cb50f42d | 3334 | sv_setpvn(dsv,s,len); |
446eaa42 | 3335 | if (SvUTF8(ssv)) |
cb50f42d | 3336 | SvUTF8_on(dsv); |
446eaa42 | 3337 | else |
cb50f42d | 3338 | SvUTF8_off(dsv); |
6050d10e JP |
3339 | } |
3340 | ||
3341 | /* | |
645c22ef DM |
3342 | =for apidoc sv_2pvbyte_nolen |
3343 | ||
3344 | Return a pointer to the byte-encoded representation of the SV. | |
1e54db1a | 3345 | May cause the SV to be downgraded from UTF-8 as a side-effect. |
645c22ef DM |
3346 | |
3347 | Usually accessed via the C<SvPVbyte_nolen> macro. | |
3348 | ||
3349 | =cut | |
3350 | */ | |
3351 | ||
7340a771 GS |
3352 | char * |
3353 | Perl_sv_2pvbyte_nolen(pTHX_ register SV *sv) | |
3354 | { | |
dafda6d1 | 3355 | return sv_2pvbyte(sv, 0); |
7340a771 GS |
3356 | } |
3357 | ||
645c22ef DM |
3358 | /* |
3359 | =for apidoc sv_2pvbyte | |
3360 | ||
3361 | Return a pointer to the byte-encoded representation of the SV, and set *lp | |
1e54db1a | 3362 | to its length. May cause the SV to be downgraded from UTF-8 as a |
645c22ef DM |
3363 | side-effect. |
3364 | ||
3365 | Usually accessed via the C<SvPVbyte> macro. | |
3366 | ||
3367 | =cut | |
3368 | */ | |
3369 | ||
7340a771 GS |
3370 | char * |
3371 | Perl_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp) | |
3372 | { | |
0875d2fe | 3373 | sv_utf8_downgrade(sv,0); |
97972285 | 3374 | return lp ? SvPV(sv,*lp) : SvPV_nolen(sv); |
7340a771 GS |
3375 | } |
3376 | ||
645c22ef DM |
3377 | /* |
3378 | =for apidoc sv_2pvutf8_nolen | |
3379 | ||
1e54db1a JH |
3380 | Return a pointer to the UTF-8-encoded representation of the SV. |
3381 | May cause the SV to be upgraded to UTF-8 as a side-effect. | |
645c22ef DM |
3382 | |
3383 | Usually accessed via the C<SvPVutf8_nolen> macro. | |
3384 | ||
3385 | =cut | |
3386 | */ | |
3387 | ||
7340a771 GS |
3388 | char * |
3389 | Perl_sv_2pvutf8_nolen(pTHX_ register SV *sv) | |
3390 | { | |
dafda6d1 | 3391 | return sv_2pvutf8(sv, 0); |
7340a771 GS |
3392 | } |
3393 | ||
645c22ef DM |
3394 | /* |
3395 | =for apidoc sv_2pvutf8 | |
3396 | ||
1e54db1a JH |
3397 | Return a pointer to the UTF-8-encoded representation of the SV, and set *lp |
3398 | to its length. May cause the SV to be upgraded to UTF-8 as a side-effect. | |
645c22ef DM |
3399 | |
3400 | Usually accessed via the C<SvPVutf8> macro. | |
3401 | ||
3402 | =cut | |
3403 | */ | |
3404 | ||
7340a771 GS |
3405 | char * |
3406 | Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp) | |
3407 | { | |
560a288e | 3408 | sv_utf8_upgrade(sv); |
7d59b7e4 | 3409 | return SvPV(sv,*lp); |
7340a771 | 3410 | } |
1c846c1f | 3411 | |
645c22ef DM |
3412 | /* |
3413 | =for apidoc sv_2bool | |
3414 | ||
3415 | This function is only called on magical items, and is only used by | |
8cf8f3d1 | 3416 | sv_true() or its macro equivalent. |
645c22ef DM |
3417 | |
3418 | =cut | |
3419 | */ | |
3420 | ||
463ee0b2 | 3421 | bool |
864dbfa3 | 3422 | Perl_sv_2bool(pTHX_ register SV *sv) |
463ee0b2 | 3423 | { |
8990e307 | 3424 | if (SvGMAGICAL(sv)) |
463ee0b2 LW |
3425 | mg_get(sv); |
3426 | ||
a0d0e21e LW |
3427 | if (!SvOK(sv)) |
3428 | return 0; | |
3429 | if (SvROK(sv)) { | |
a0d0e21e | 3430 | SV* tmpsv; |
1554e226 | 3431 | if (SvAMAGIC(sv) && (tmpsv=AMG_CALLun(sv,bool_)) && |
9e3013b1 | 3432 | (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv)))) |
8a31060d | 3433 | return (bool)SvTRUE(tmpsv); |
a0d0e21e LW |
3434 | return SvRV(sv) != 0; |
3435 | } | |
463ee0b2 | 3436 | if (SvPOKp(sv)) { |
53c1dcc0 AL |
3437 | register XPV* const Xpvtmp = (XPV*)SvANY(sv); |
3438 | if (Xpvtmp && | |
339049b0 | 3439 | (*sv->sv_u.svu_pv > '0' || |
11343788 | 3440 | Xpvtmp->xpv_cur > 1 || |
339049b0 | 3441 | (Xpvtmp->xpv_cur && *sv->sv_u.svu_pv != '0'))) |
463ee0b2 LW |
3442 | return 1; |
3443 | else | |
3444 | return 0; | |
3445 | } | |
3446 | else { | |
3447 | if (SvIOKp(sv)) | |
3448 | return SvIVX(sv) != 0; | |
3449 | else { | |
3450 | if (SvNOKp(sv)) | |
3451 | return SvNVX(sv) != 0.0; | |
3452 | else | |
3453 | return FALSE; | |
3454 | } | |
3455 | } | |
79072805 LW |
3456 | } |
3457 | ||
09540bc3 JH |
3458 | /* sv_utf8_upgrade() is now a macro using sv_utf8_upgrade_flags(); |
3459 | * this function provided for binary compatibility only | |
3460 | */ | |
3461 | ||
3462 | ||
3463 | STRLEN | |
3464 | Perl_sv_utf8_upgrade(pTHX_ register SV *sv) | |
3465 | { | |
3466 | return sv_utf8_upgrade_flags(sv, SV_GMAGIC); | |
3467 | } | |
3468 | ||
c461cf8f JH |
3469 | /* |
3470 | =for apidoc sv_utf8_upgrade | |
3471 | ||
78ea37eb | 3472 | Converts the PV of an SV to its UTF-8-encoded form. |
645c22ef | 3473 | Forces the SV to string form if it is not already. |
4411f3b6 NIS |
3474 | Always sets the SvUTF8 flag to avoid future validity checks even |
3475 | if all the bytes have hibit clear. | |
c461cf8f | 3476 | |
13a6c0e0 JH |
3477 | This is not as a general purpose byte encoding to Unicode interface: |
3478 | use the Encode extension for that. | |
3479 | ||
8d6d96c1 HS |
3480 | =for apidoc sv_utf8_upgrade_flags |
3481 | ||
78ea37eb | 3482 | Converts the PV of an SV to its UTF-8-encoded form. |
645c22ef | 3483 | Forces the SV to string form if it is not already. |
8d6d96c1 HS |
3484 | Always sets the SvUTF8 flag to avoid future validity checks even |
3485 | if all the bytes have hibit clear. If C<flags> has C<SV_GMAGIC> bit set, | |
3486 | will C<mg_get> on C<sv> if appropriate, else not. C<sv_utf8_upgrade> and | |
3487 | C<sv_utf8_upgrade_nomg> are implemented in terms of this function. | |
3488 | ||
13a6c0e0 JH |
3489 | This is not as a general purpose byte encoding to Unicode interface: |
3490 | use the Encode extension for that. | |
3491 | ||
8d6d96c1 HS |
3492 | =cut |
3493 | */ | |
3494 | ||
3495 | STRLEN | |
3496 | Perl_sv_utf8_upgrade_flags(pTHX_ register SV *sv, I32 flags) | |
3497 | { | |
808c356f RGS |
3498 | if (sv == &PL_sv_undef) |
3499 | return 0; | |
e0e62c2a NIS |
3500 | if (!SvPOK(sv)) { |
3501 | STRLEN len = 0; | |
d52b7888 NC |
3502 | if (SvREADONLY(sv) && (SvPOKp(sv) || SvIOKp(sv) || SvNOKp(sv))) { |
3503 | (void) sv_2pv_flags(sv,&len, flags); | |
3504 | if (SvUTF8(sv)) | |
3505 | return len; | |
3506 | } else { | |
3507 | (void) SvPV_force(sv,len); | |
3508 | } | |
e0e62c2a | 3509 | } |
4411f3b6 | 3510 | |
f5cee72b | 3511 | if (SvUTF8(sv)) { |
5fec3b1d | 3512 | return SvCUR(sv); |
f5cee72b | 3513 | } |
5fec3b1d | 3514 | |
765f542d NC |
3515 | if (SvIsCOW(sv)) { |
3516 | sv_force_normal_flags(sv, 0); | |
db42d148 NIS |
3517 | } |
3518 | ||
88632417 | 3519 | if (PL_encoding && !(flags & SV_UTF8_NO_ENCODING)) |
799ef3cb | 3520 | sv_recode_to_utf8(sv, PL_encoding); |
9f4817db | 3521 | else { /* Assume Latin-1/EBCDIC */ |
c4e7c712 NC |
3522 | /* This function could be much more efficient if we |
3523 | * had a FLAG in SVs to signal if there are any hibit | |
3524 | * chars in the PV. Given that there isn't such a flag | |
3525 | * make the loop as fast as possible. */ | |
93524f2b NC |
3526 | const U8 *s = (U8 *) SvPVX_const(sv); |
3527 | const U8 *e = (U8 *) SvEND(sv); | |
3528 | const U8 *t = s; | |
c4e7c712 NC |
3529 | int hibit = 0; |
3530 | ||
3531 | while (t < e) { | |
53c1dcc0 | 3532 | const U8 ch = *t++; |
c4e7c712 NC |
3533 | if ((hibit = !NATIVE_IS_INVARIANT(ch))) |
3534 | break; | |
3535 | } | |
3536 | if (hibit) { | |
3537 | STRLEN len = SvCUR(sv) + 1; /* Plus the \0 */ | |
53c1dcc0 | 3538 | U8 * const recoded = bytes_to_utf8((U8*)s, &len); |
c4e7c712 NC |
3539 | |
3540 | SvPV_free(sv); /* No longer using what was there before. */ | |
3541 | ||
1e2ebb21 | 3542 | SvPV_set(sv, (char*)recoded); |
c4e7c712 NC |
3543 | SvCUR_set(sv, len - 1); |
3544 | SvLEN_set(sv, len); /* No longer know the real size. */ | |
3545 | } | |
3546 | /* Mark as UTF-8 even if no hibit - saves scanning loop */ | |
3547 | SvUTF8_on(sv); | |
560a288e | 3548 | } |
4411f3b6 | 3549 | return SvCUR(sv); |
560a288e GS |
3550 | } |
3551 | ||
c461cf8f JH |
3552 | /* |
3553 | =for apidoc sv_utf8_downgrade | |
3554 | ||
78ea37eb TS |
3555 | Attempts to convert the PV of an SV from characters to bytes. |
3556 | If the PV contains a character beyond byte, this conversion will fail; | |
3557 | in this case, either returns false or, if C<fail_ok> is not | |
c461cf8f JH |
3558 | true, croaks. |
3559 | ||
13a6c0e0 JH |
3560 | This is not as a general purpose Unicode to byte encoding interface: |
3561 | use the Encode extension for that. | |
3562 | ||
c461cf8f JH |
3563 | =cut |
3564 | */ | |
3565 | ||
560a288e GS |
3566 | bool |
3567 | Perl_sv_utf8_downgrade(pTHX_ register SV* sv, bool fail_ok) | |
3568 | { | |
78ea37eb | 3569 | if (SvPOKp(sv) && SvUTF8(sv)) { |
fa301091 | 3570 | if (SvCUR(sv)) { |
03cfe0ae | 3571 | U8 *s; |
652088fc | 3572 | STRLEN len; |
fa301091 | 3573 | |
765f542d NC |
3574 | if (SvIsCOW(sv)) { |
3575 | sv_force_normal_flags(sv, 0); | |
3576 | } | |
03cfe0ae NIS |
3577 | s = (U8 *) SvPV(sv, len); |
3578 | if (!utf8_to_bytes(s, &len)) { | |
fa301091 JH |
3579 | if (fail_ok) |
3580 | return FALSE; | |
3581 | else { | |
3582 | if (PL_op) | |
3583 | Perl_croak(aTHX_ "Wide character in %s", | |
53e06cf0 | 3584 | OP_DESC(PL_op)); |
fa301091 JH |
3585 | else |
3586 | Perl_croak(aTHX_ "Wide character"); | |
3587 | } | |
4b3603a4 | 3588 | } |
b162af07 | 3589 | SvCUR_set(sv, len); |
67e989fb | 3590 | } |
560a288e | 3591 | } |
ffebcc3e | 3592 | SvUTF8_off(sv); |
560a288e GS |
3593 | return TRUE; |
3594 | } | |
3595 | ||
c461cf8f JH |
3596 | /* |
3597 | =for apidoc sv_utf8_encode | |
3598 | ||
78ea37eb TS |
3599 | Converts the PV of an SV to UTF-8, but then turns the C<SvUTF8> |
3600 | flag off so that it looks like octets again. | |
c461cf8f JH |
3601 | |
3602 | =cut | |
3603 | */ | |
3604 | ||
560a288e GS |
3605 | void |
3606 | Perl_sv_utf8_encode(pTHX_ register SV *sv) | |
3607 | { | |
4411f3b6 | 3608 | (void) sv_utf8_upgrade(sv); |
4c94c214 NC |
3609 | if (SvIsCOW(sv)) { |
3610 | sv_force_normal_flags(sv, 0); | |
3611 | } | |
3612 | if (SvREADONLY(sv)) { | |
3613 | Perl_croak(aTHX_ PL_no_modify); | |
3614 | } | |
560a288e GS |
3615 | SvUTF8_off(sv); |
3616 | } | |
3617 | ||
4411f3b6 NIS |
3618 | /* |
3619 | =for apidoc sv_utf8_decode | |
3620 | ||
78ea37eb TS |
3621 | If the PV of the SV is an octet sequence in UTF-8 |
3622 | and contains a multiple-byte character, the C<SvUTF8> flag is turned on | |
3623 | so that it looks like a character. If the PV contains only single-byte | |
3624 | characters, the C<SvUTF8> flag stays being off. | |
3625 | Scans PV for validity and returns false if the PV is invalid UTF-8. | |
4411f3b6 NIS |
3626 | |
3627 | =cut | |
3628 | */ | |
3629 | ||
560a288e GS |
3630 | bool |
3631 | Perl_sv_utf8_decode(pTHX_ register SV *sv) | |
3632 | { | |
78ea37eb | 3633 | if (SvPOKp(sv)) { |
93524f2b NC |
3634 | const U8 *c; |
3635 | const U8 *e; | |
9cbac4c7 | 3636 | |
645c22ef DM |
3637 | /* The octets may have got themselves encoded - get them back as |
3638 | * bytes | |
3639 | */ | |
3640 | if (!sv_utf8_downgrade(sv, TRUE)) | |
560a288e GS |
3641 | return FALSE; |
3642 | ||
3643 | /* it is actually just a matter of turning the utf8 flag on, but | |
3644 | * we want to make sure everything inside is valid utf8 first. | |
3645 | */ | |
93524f2b | 3646 | c = (const U8 *) SvPVX_const(sv); |
63cd0674 | 3647 | if (!is_utf8_string(c, SvCUR(sv)+1)) |
67e989fb | 3648 | return FALSE; |
93524f2b | 3649 | e = (const U8 *) SvEND(sv); |
511c2ff0 | 3650 | while (c < e) { |
c4d5f83a NIS |
3651 | U8 ch = *c++; |
3652 | if (!UTF8_IS_INVARIANT(ch)) { | |
67e989fb JH |
3653 | SvUTF8_on(sv); |
3654 | break; | |
3655 | } | |
560a288e | 3656 | } |
560a288e GS |
3657 | } |
3658 | return TRUE; | |
3659 | } | |
3660 | ||
09540bc3 JH |
3661 | /* sv_setsv() is now a macro using Perl_sv_setsv_flags(); |
3662 | * this function provided for binary compatibility only | |
3663 | */ | |
3664 | ||
3665 | void | |
3666 | Perl_sv_setsv(pTHX_ SV *dstr, register SV *sstr) | |
3667 | { | |
3668 | sv_setsv_flags(dstr, sstr, SV_GMAGIC); | |
3669 | } | |
3670 | ||
954c1994 GS |
3671 | /* |
3672 | =for apidoc sv_setsv | |
3673 | ||
645c22ef DM |
3674 | Copies the contents of the source SV C<ssv> into the destination SV |
3675 | C<dsv>. The source SV may be destroyed if it is mortal, so don't use this | |
3676 | function if the source SV needs to be reused. Does not handle 'set' magic. | |
3677 | Loosely speaking, it performs a copy-by-value, obliterating any previous | |
3678 | content of the destination. | |
3679 | ||
3680 | You probably want to use one of the assortment of wrappers, such as | |
3681 | C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and | |
3682 | C<SvSetMagicSV_nosteal>. | |
3683 | ||
8d6d96c1 HS |
3684 | =for apidoc sv_setsv_flags |
3685 | ||
645c22ef DM |
3686 | Copies the contents of the source SV C<ssv> into the destination SV |
3687 | C<dsv>. The source SV may be destroyed if it is mortal, so don't use this | |
3688 | function if the source SV needs to be reused. Does not handle 'set' magic. | |
3689 | Loosely speaking, it performs a copy-by-value, obliterating any previous | |
3690 | content of the destination. | |
3691 | If the C<flags> parameter has the C<SV_GMAGIC> bit set, will C<mg_get> on | |
5fcdf167 NC |
3692 | C<ssv> if appropriate, else not. If the C<flags> parameter has the |
3693 | C<NOSTEAL> bit set then the buffers of temps will not be stolen. <sv_setsv> | |
3694 | and C<sv_setsv_nomg> are implemented in terms of this function. | |
645c22ef DM |
3695 | |
3696 | You probably want to use one of the assortment of wrappers, such as | |
3697 | C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and | |
3698 | C<SvSetMagicSV_nosteal>. | |
3699 | ||
3700 | This is the primary function for copying scalars, and most other | |
3701 | copy-ish functions and macros use this underneath. | |
8d6d96c1 HS |
3702 | |
3703 | =cut | |
3704 | */ | |
3705 | ||
3706 | void | |
3707 | Perl_sv_setsv_flags(pTHX_ SV *dstr, register SV *sstr, I32 flags) | |
3708 | { | |
8990e307 LW |
3709 | register U32 sflags; |
3710 | register int dtype; | |
3711 | register int stype; | |
463ee0b2 | 3712 | |
79072805 LW |
3713 | if (sstr == dstr) |
3714 | return; | |
765f542d | 3715 | SV_CHECK_THINKFIRST_COW_DROP(dstr); |
79072805 | 3716 | if (!sstr) |
3280af22 | 3717 | sstr = &PL_sv_undef; |
8990e307 LW |
3718 | stype = SvTYPE(sstr); |
3719 | dtype = SvTYPE(dstr); | |
79072805 | 3720 | |
a0d0e21e | 3721 | SvAMAGIC_off(dstr); |
7a5fa8a2 | 3722 | if ( SvVOK(dstr) ) |
ece467f9 JP |
3723 | { |
3724 | /* need to nuke the magic */ | |
3725 | mg_free(dstr); | |
3726 | SvRMAGICAL_off(dstr); | |
3727 | } | |
9e7bc3e8 | 3728 | |
463ee0b2 | 3729 | /* There's a lot of redundancy below but we're going for speed here */ |
79072805 | 3730 | |
8990e307 | 3731 | switch (stype) { |
79072805 | 3732 | case SVt_NULL: |
aece5585 | 3733 | undef_sstr: |
20408e3c GS |
3734 | if (dtype != SVt_PVGV) { |
3735 | (void)SvOK_off(dstr); | |
3736 | return; | |
3737 | } | |
3738 | break; | |
463ee0b2 | 3739 | case SVt_IV: |
aece5585 GA |
3740 | if (SvIOK(sstr)) { |
3741 | switch (dtype) { | |
3742 | case SVt_NULL: | |
8990e307 | 3743 | sv_upgrade(dstr, SVt_IV); |
aece5585 GA |
3744 | break; |
3745 | case SVt_NV: | |
8990e307 | 3746 | sv_upgrade(dstr, SVt_PVNV); |
aece5585 GA |
3747 | break; |
3748 | case SVt_RV: | |
3749 | case SVt_PV: | |
a0d0e21e | 3750 | sv_upgrade(dstr, SVt_PVIV); |
aece5585 GA |
3751 | break; |
3752 | } | |
3753 | (void)SvIOK_only(dstr); | |
45977657 | 3754 | SvIV_set(dstr, SvIVX(sstr)); |
25da4f38 IZ |
3755 | if (SvIsUV(sstr)) |
3756 | SvIsUV_on(dstr); | |
27c9684d AP |
3757 | if (SvTAINTED(sstr)) |
3758 | SvTAINT(dstr); | |
aece5585 | 3759 | return; |
8990e307 | 3760 | } |
aece5585 GA |
3761 | goto undef_sstr; |
3762 | ||
463ee0b2 | 3763 | case SVt_NV: |
aece5585 GA |
3764 | if (SvNOK(sstr)) { |
3765 | switch (dtype) { | |
3766 | case SVt_NULL: | |
3767 | case SVt_IV: | |
8990e307 | 3768 | sv_upgrade(dstr, SVt_NV); |
aece5585 GA |
3769 | break; |
3770 | case SVt_RV: | |
3771 | case SVt_PV: | |
3772 | case SVt_PVIV: | |
a0d0e21e | 3773 | sv_upgrade(dstr, SVt_PVNV); |
aece5585 GA |
3774 | break; |
3775 | } | |
9d6ce603 | 3776 | SvNV_set(dstr, SvNVX(sstr)); |
aece5585 | 3777 | (void)SvNOK_only(dstr); |
27c9684d AP |
3778 | if (SvTAINTED(sstr)) |
3779 | SvTAINT(dstr); | |
aece5585 | 3780 | return; |
8990e307 | 3781 | } |
aece5585 GA |
3782 | goto undef_sstr; |
3783 | ||
ed6116ce | 3784 | case SVt_RV: |
8990e307 | 3785 | if (dtype < SVt_RV) |
ed6116ce | 3786 | sv_upgrade(dstr, SVt_RV); |
c07a80fd | 3787 | else if (dtype == SVt_PVGV && |
23bb1b96 | 3788 | SvROK(sstr) && SvTYPE(SvRV(sstr)) == SVt_PVGV) { |
c07a80fd | 3789 | sstr = SvRV(sstr); |
a5f75d66 | 3790 | if (sstr == dstr) { |
1d7c1841 GS |
3791 | if (GvIMPORTED(dstr) != GVf_IMPORTED |
3792 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) | |
3793 | { | |
a5f75d66 | 3794 | GvIMPORTED_on(dstr); |
1d7c1841 | 3795 | } |
a5f75d66 AD |
3796 | GvMULTI_on(dstr); |
3797 | return; | |
3798 | } | |
c07a80fd | 3799 | goto glob_assign; |
3800 | } | |
ed6116ce | 3801 | break; |
fc36a67e | 3802 | case SVt_PVFM: |
f8c7b90f | 3803 | #ifdef PERL_OLD_COPY_ON_WRITE |
d89fc664 NC |
3804 | if ((SvFLAGS(sstr) & CAN_COW_MASK) == CAN_COW_FLAGS) { |
3805 | if (dtype < SVt_PVIV) | |
3806 | sv_upgrade(dstr, SVt_PVIV); | |
3807 | break; | |
3808 | } | |
3809 | /* Fall through */ | |
3810 | #endif | |
3811 | case SVt_PV: | |
8990e307 | 3812 | if (dtype < SVt_PV) |
463ee0b2 | 3813 | sv_upgrade(dstr, SVt_PV); |
463ee0b2 LW |
3814 | break; |
3815 | case SVt_PVIV: | |
8990e307 | 3816 | if (dtype < SVt_PVIV) |
463ee0b2 | 3817 | sv_upgrade(dstr, SVt_PVIV); |
463ee0b2 LW |
3818 | break; |
3819 | case SVt_PVNV: | |
8990e307 | 3820 | if (dtype < SVt_PVNV) |
463ee0b2 | 3821 | sv_upgrade(dstr, SVt_PVNV); |
463ee0b2 | 3822 | break; |
4633a7c4 LW |
3823 | case SVt_PVAV: |
3824 | case SVt_PVHV: | |
3825 | case SVt_PVCV: | |
4633a7c4 | 3826 | case SVt_PVIO: |
a3b680e6 AL |
3827 | { |
3828 | const char * const type = sv_reftype(sstr,0); | |
533c011a | 3829 | if (PL_op) |
a3b680e6 | 3830 | Perl_croak(aTHX_ "Bizarre copy of %s in %s", type, OP_NAME(PL_op)); |
4633a7c4 | 3831 | else |
a3b680e6 AL |
3832 | Perl_croak(aTHX_ "Bizarre copy of %s", type); |
3833 | } | |
4633a7c4 LW |
3834 | break; |
3835 | ||
79072805 | 3836 | case SVt_PVGV: |
8990e307 | 3837 | if (dtype <= SVt_PVGV) { |
c07a80fd | 3838 | glob_assign: |
a5f75d66 | 3839 | if (dtype != SVt_PVGV) { |
a3b680e6 AL |
3840 | const char * const name = GvNAME(sstr); |
3841 | const STRLEN len = GvNAMELEN(sstr); | |
b76195c2 DM |
3842 | /* don't upgrade SVt_PVLV: it can hold a glob */ |
3843 | if (dtype != SVt_PVLV) | |
3844 | sv_upgrade(dstr, SVt_PVGV); | |
14befaf4 | 3845 | sv_magic(dstr, dstr, PERL_MAGIC_glob, Nullch, 0); |
85aff577 | 3846 | GvSTASH(dstr) = (HV*)SvREFCNT_inc(GvSTASH(sstr)); |
a0d0e21e LW |
3847 | GvNAME(dstr) = savepvn(name, len); |
3848 | GvNAMELEN(dstr) = len; | |
3849 | SvFAKE_on(dstr); /* can coerce to non-glob */ | |
3850 | } | |
7bac28a0 | 3851 | /* ahem, death to those who redefine active sort subs */ |
3280af22 NIS |
3852 | else if (PL_curstackinfo->si_type == PERLSI_SORT |
3853 | && GvCV(dstr) && PL_sortcop == CvSTART(GvCV(dstr))) | |
cea2e8a9 | 3854 | Perl_croak(aTHX_ "Can't redefine active sort subroutine %s", |
7bac28a0 | 3855 | GvNAME(dstr)); |
5bd07a3d | 3856 | |
7fb37951 AMS |
3857 | #ifdef GV_UNIQUE_CHECK |
3858 | if (GvUNIQUE((GV*)dstr)) { | |
5bd07a3d DM |
3859 | Perl_croak(aTHX_ PL_no_modify); |
3860 | } | |
3861 | #endif | |
3862 | ||
a0d0e21e | 3863 | (void)SvOK_off(dstr); |
a5f75d66 | 3864 | GvINTRO_off(dstr); /* one-shot flag */ |
1edc1566 | 3865 | gp_free((GV*)dstr); |
79072805 | 3866 | GvGP(dstr) = gp_ref(GvGP(sstr)); |
27c9684d AP |
3867 | if (SvTAINTED(sstr)) |
3868 | SvTAINT(dstr); | |
1d7c1841 GS |
3869 | if (GvIMPORTED(dstr) != GVf_IMPORTED |
3870 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) | |
3871 | { | |
a5f75d66 | 3872 | GvIMPORTED_on(dstr); |
1d7c1841 | 3873 | } |
a5f75d66 | 3874 | GvMULTI_on(dstr); |
79072805 LW |
3875 | return; |
3876 | } | |
3877 | /* FALL THROUGH */ | |
3878 | ||
3879 | default: | |
8d6d96c1 | 3880 | if (SvGMAGICAL(sstr) && (flags & SV_GMAGIC)) { |
973f89ab | 3881 | mg_get(sstr); |
eb160463 | 3882 | if ((int)SvTYPE(sstr) != stype) { |
973f89ab CS |
3883 | stype = SvTYPE(sstr); |
3884 | if (stype == SVt_PVGV && dtype <= SVt_PVGV) | |
3885 | goto glob_assign; | |
3886 | } | |
3887 | } | |
ded42b9f | 3888 | if (stype == SVt_PVLV) |
862a34c6 | 3889 | SvUPGRADE(dstr, SVt_PVNV); |
ded42b9f | 3890 | else |
862a34c6 | 3891 | SvUPGRADE(dstr, (U32)stype); |
79072805 LW |
3892 | } |
3893 | ||
8990e307 LW |
3894 | sflags = SvFLAGS(sstr); |
3895 | ||
3896 | if (sflags & SVf_ROK) { | |
3897 | if (dtype >= SVt_PV) { | |
3898 | if (dtype == SVt_PVGV) { | |
3899 | SV *sref = SvREFCNT_inc(SvRV(sstr)); | |
3900 | SV *dref = 0; | |
a3b680e6 | 3901 | const int intro = GvINTRO(dstr); |
a0d0e21e | 3902 | |
7fb37951 AMS |
3903 | #ifdef GV_UNIQUE_CHECK |
3904 | if (GvUNIQUE((GV*)dstr)) { | |
5bd07a3d DM |
3905 | Perl_croak(aTHX_ PL_no_modify); |
3906 | } | |
3907 | #endif | |
3908 | ||
a0d0e21e | 3909 | if (intro) { |
a5f75d66 | 3910 | GvINTRO_off(dstr); /* one-shot flag */ |
1d7c1841 | 3911 | GvLINE(dstr) = CopLINE(PL_curcop); |
1edc1566 | 3912 | GvEGV(dstr) = (GV*)dstr; |
a0d0e21e | 3913 | } |
a5f75d66 | 3914 | GvMULTI_on(dstr); |
8990e307 LW |
3915 | switch (SvTYPE(sref)) { |
3916 | case SVt_PVAV: | |
a0d0e21e | 3917 | if (intro) |
890ed176 | 3918 | SAVEGENERICSV(GvAV(dstr)); |
a0d0e21e LW |
3919 | else |
3920 | dref = (SV*)GvAV(dstr); | |
8990e307 | 3921 | GvAV(dstr) = (AV*)sref; |
39bac7f7 | 3922 | if (!GvIMPORTED_AV(dstr) |
1d7c1841 GS |
3923 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
3924 | { | |
a5f75d66 | 3925 | GvIMPORTED_AV_on(dstr); |
1d7c1841 | 3926 | } |
8990e307 LW |
3927 | break; |
3928 | case SVt_PVHV: | |
a0d0e21e | 3929 | if (intro) |
890ed176 | 3930 | SAVEGENERICSV(GvHV(dstr)); |
a0d0e21e LW |
3931 | else |
3932 | dref = (SV*)GvHV(dstr); | |
8990e307 | 3933 | GvHV(dstr) = (HV*)sref; |
39bac7f7 | 3934 | if (!GvIMPORTED_HV(dstr) |
1d7c1841 GS |
3935 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
3936 | { | |
a5f75d66 | 3937 | GvIMPORTED_HV_on(dstr); |
1d7c1841 | 3938 | } |
8990e307 LW |
3939 | break; |
3940 | case SVt_PVCV: | |
8ebc5c01 | 3941 | if (intro) { |
3942 | if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) { | |
3943 | SvREFCNT_dec(GvCV(dstr)); | |
3944 | GvCV(dstr) = Nullcv; | |
68dc0745 | 3945 | GvCVGEN(dstr) = 0; /* Switch off cacheness. */ |
3280af22 | 3946 | PL_sub_generation++; |
8ebc5c01 | 3947 | } |
890ed176 | 3948 | SAVEGENERICSV(GvCV(dstr)); |
8ebc5c01 | 3949 | } |
68dc0745 | 3950 | else |
3951 | dref = (SV*)GvCV(dstr); | |
3952 | if (GvCV(dstr) != (CV*)sref) { | |
748a9306 | 3953 | CV* cv = GvCV(dstr); |
4633a7c4 | 3954 | if (cv) { |
68dc0745 | 3955 | if (!GvCVGEN((GV*)dstr) && |
3956 | (CvROOT(cv) || CvXSUB(cv))) | |
3957 | { | |
7bac28a0 | 3958 | /* ahem, death to those who redefine |
3959 | * active sort subs */ | |
3280af22 NIS |
3960 | if (PL_curstackinfo->si_type == PERLSI_SORT && |
3961 | PL_sortcop == CvSTART(cv)) | |
1c846c1f | 3962 | Perl_croak(aTHX_ |
7bac28a0 | 3963 | "Can't redefine active sort subroutine %s", |
3964 | GvENAME((GV*)dstr)); | |
beab0874 JT |
3965 | /* Redefining a sub - warning is mandatory if |
3966 | it was a const and its value changed. */ | |
3967 | if (ckWARN(WARN_REDEFINE) | |
3968 | || (CvCONST(cv) | |
3969 | && (!CvCONST((CV*)sref) | |
3970 | || sv_cmp(cv_const_sv(cv), | |
3971 | cv_const_sv((CV*)sref))))) | |
3972 | { | |
9014280d | 3973 | Perl_warner(aTHX_ packWARN(WARN_REDEFINE), |
beab0874 | 3974 | CvCONST(cv) |
910764e6 RGS |
3975 | ? "Constant subroutine %s::%s redefined" |
3976 | : "Subroutine %s::%s redefined", | |
bfcb3514 | 3977 | HvNAME_get(GvSTASH((GV*)dstr)), |
beab0874 JT |
3978 | GvENAME((GV*)dstr)); |
3979 | } | |
9607fc9c | 3980 | } |
fb24441d RGS |
3981 | if (!intro) |
3982 | cv_ckproto(cv, (GV*)dstr, | |
93524f2b NC |
3983 | SvPOK(sref) |
3984 | ? SvPVX_const(sref) : Nullch); | |
4633a7c4 | 3985 | } |
a5f75d66 | 3986 | GvCV(dstr) = (CV*)sref; |
7a4c00b4 | 3987 | GvCVGEN(dstr) = 0; /* Switch off cacheness. */ |
a5f75d66 | 3988 | GvASSUMECV_on(dstr); |
3280af22 | 3989 | PL_sub_generation++; |
a5f75d66 | 3990 | } |
39bac7f7 | 3991 | if (!GvIMPORTED_CV(dstr) |
1d7c1841 GS |
3992 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
3993 | { | |
a5f75d66 | 3994 | GvIMPORTED_CV_on(dstr); |
1d7c1841 | 3995 | } |
8990e307 | 3996 | break; |
91bba347 LW |
3997 | case SVt_PVIO: |
3998 | if (intro) | |
890ed176 | 3999 | SAVEGENERICSV(GvIOp(dstr)); |
91bba347 LW |
4000 | else |
4001 | dref = (SV*)GvIOp(dstr); | |
4002 | GvIOp(dstr) = (IO*)sref; | |
4003 | break; | |
f4d13ee9 JH |
4004 | case SVt_PVFM: |
4005 | if (intro) | |
890ed176 | 4006 | SAVEGENERICSV(GvFORM(dstr)); |
f4d13ee9 JH |
4007 | else |
4008 | dref = (SV*)GvFORM(dstr); | |
4009 | GvFORM(dstr) = (CV*)sref; | |
4010 | break; | |
8990e307 | 4011 | default: |
a0d0e21e | 4012 | if (intro) |
890ed176 | 4013 | SAVEGENERICSV(GvSV(dstr)); |
a0d0e21e LW |
4014 | else |
4015 | dref = (SV*)GvSV(dstr); | |
8990e307 | 4016 | GvSV(dstr) = sref; |
39bac7f7 | 4017 | if (!GvIMPORTED_SV(dstr) |
1d7c1841 GS |
4018 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
4019 | { | |
a5f75d66 | 4020 | GvIMPORTED_SV_on(dstr); |
1d7c1841 | 4021 | } |
8990e307 LW |
4022 | break; |
4023 | } | |
4024 | if (dref) | |
4025 | SvREFCNT_dec(dref); | |
27c9684d AP |
4026 | if (SvTAINTED(sstr)) |
4027 | SvTAINT(dstr); | |
8990e307 LW |
4028 | return; |
4029 | } | |
3f7c398e | 4030 | if (SvPVX_const(dstr)) { |
8bd4d4c5 | 4031 | SvPV_free(dstr); |
b162af07 SP |
4032 | SvLEN_set(dstr, 0); |
4033 | SvCUR_set(dstr, 0); | |
a0d0e21e | 4034 | } |
8990e307 | 4035 | } |
a0d0e21e | 4036 | (void)SvOK_off(dstr); |
b162af07 | 4037 | SvRV_set(dstr, SvREFCNT_inc(SvRV(sstr))); |
ed6116ce | 4038 | SvROK_on(dstr); |
8990e307 | 4039 | if (sflags & SVp_NOK) { |
3332b3c1 JH |
4040 | SvNOKp_on(dstr); |
4041 | /* Only set the public OK flag if the source has public OK. */ | |
4042 | if (sflags & SVf_NOK) | |
4043 | SvFLAGS(dstr) |= SVf_NOK; | |
9d6ce603 | 4044 | SvNV_set(dstr, SvNVX(sstr)); |
ed6116ce | 4045 | } |
8990e307 | 4046 | if (sflags & SVp_IOK) { |
3332b3c1 JH |
4047 | (void)SvIOKp_on(dstr); |
4048 | if (sflags & SVf_IOK) | |
4049 | SvFLAGS(dstr) |= SVf_IOK; | |
2b1c7e3e | 4050 | if (sflags & SVf_IVisUV) |
25da4f38 | 4051 | SvIsUV_on(dstr); |
45977657 | 4052 | SvIV_set(dstr, SvIVX(sstr)); |
ed6116ce | 4053 | } |
a0d0e21e LW |
4054 | if (SvAMAGIC(sstr)) { |
4055 | SvAMAGIC_on(dstr); | |
4056 | } | |
ed6116ce | 4057 | } |
8990e307 | 4058 | else if (sflags & SVp_POK) { |
765f542d | 4059 | bool isSwipe = 0; |
79072805 LW |
4060 | |
4061 | /* | |
4062 | * Check to see if we can just swipe the string. If so, it's a | |
4063 | * possible small lose on short strings, but a big win on long ones. | |
3f7c398e SP |
4064 | * It might even be a win on short strings if SvPVX_const(dstr) |
4065 | * has to be allocated and SvPVX_const(sstr) has to be freed. | |
79072805 LW |
4066 | */ |
4067 | ||
120fac95 NC |
4068 | /* Whichever path we take through the next code, we want this true, |
4069 | and doing it now facilitates the COW check. */ | |
4070 | (void)SvPOK_only(dstr); | |
4071 | ||
765f542d | 4072 | if ( |
b8f9541a NC |
4073 | /* We're not already COW */ |
4074 | ((sflags & (SVf_FAKE | SVf_READONLY)) != (SVf_FAKE | SVf_READONLY) | |
f8c7b90f | 4075 | #ifndef PERL_OLD_COPY_ON_WRITE |
b8f9541a NC |
4076 | /* or we are, but dstr isn't a suitable target. */ |
4077 | || (SvFLAGS(dstr) & CAN_COW_MASK) != CAN_COW_FLAGS | |
4078 | #endif | |
4079 | ) | |
765f542d | 4080 | && |
765f542d NC |
4081 | !(isSwipe = |
4082 | (sflags & SVs_TEMP) && /* slated for free anyway? */ | |
4083 | !(sflags & SVf_OOK) && /* and not involved in OOK hack? */ | |
5fcdf167 NC |
4084 | (!(flags & SV_NOSTEAL)) && |
4085 | /* and we're allowed to steal temps */ | |
765f542d NC |
4086 | SvREFCNT(sstr) == 1 && /* and no other references to it? */ |
4087 | SvLEN(sstr) && /* and really is a string */ | |
645c22ef | 4088 | /* and won't be needed again, potentially */ |
765f542d | 4089 | !(PL_op && PL_op->op_type == OP_AASSIGN)) |
f8c7b90f | 4090 | #ifdef PERL_OLD_COPY_ON_WRITE |
765f542d | 4091 | && !((sflags & CAN_COW_MASK) == CAN_COW_FLAGS |
120fac95 | 4092 | && (SvFLAGS(dstr) & CAN_COW_MASK) == CAN_COW_FLAGS |
765f542d NC |
4093 | && SvTYPE(sstr) >= SVt_PVIV) |
4094 | #endif | |
4095 | ) { | |
4096 | /* Failed the swipe test, and it's not a shared hash key either. | |
4097 | Have to copy the string. */ | |
4098 | STRLEN len = SvCUR(sstr); | |
4099 | SvGROW(dstr, len + 1); /* inlined from sv_setpvn */ | |
3f7c398e | 4100 | Move(SvPVX_const(sstr),SvPVX(dstr),len,char); |
765f542d NC |
4101 | SvCUR_set(dstr, len); |
4102 | *SvEND(dstr) = '\0'; | |
765f542d | 4103 | } else { |
f8c7b90f | 4104 | /* If PERL_OLD_COPY_ON_WRITE is not defined, then isSwipe will always |
765f542d | 4105 | be true in here. */ |
765f542d NC |
4106 | /* Either it's a shared hash key, or it's suitable for |
4107 | copy-on-write or we can swipe the string. */ | |
46187eeb | 4108 | if (DEBUG_C_TEST) { |
ed252734 | 4109 | PerlIO_printf(Perl_debug_log, "Copy on write: sstr --> dstr\n"); |
e419cbc5 NC |
4110 | sv_dump(sstr); |
4111 | sv_dump(dstr); | |
46187eeb | 4112 | } |
f8c7b90f | 4113 | #ifdef PERL_OLD_COPY_ON_WRITE |
765f542d NC |
4114 | if (!isSwipe) { |
4115 | /* I believe I should acquire a global SV mutex if | |
4116 | it's a COW sv (not a shared hash key) to stop | |
4117 | it going un copy-on-write. | |
4118 | If the source SV has gone un copy on write between up there | |
4119 | and down here, then (assert() that) it is of the correct | |
4120 | form to make it copy on write again */ | |
4121 | if ((sflags & (SVf_FAKE | SVf_READONLY)) | |
4122 | != (SVf_FAKE | SVf_READONLY)) { | |
4123 | SvREADONLY_on(sstr); | |
4124 | SvFAKE_on(sstr); | |
4125 | /* Make the source SV into a loop of 1. | |
4126 | (about to become 2) */ | |
a29f6d03 | 4127 | SV_COW_NEXT_SV_SET(sstr, sstr); |
765f542d NC |
4128 | } |
4129 | } | |
4130 | #endif | |
4131 | /* Initial code is common. */ | |
3f7c398e | 4132 | if (SvPVX_const(dstr)) { /* we know that dtype >= SVt_PV */ |
a5f75d66 AD |
4133 | if (SvOOK(dstr)) { |
4134 | SvFLAGS(dstr) &= ~SVf_OOK; | |
3f7c398e | 4135 | Safefree(SvPVX_const(dstr) - SvIVX(dstr)); |
a5f75d66 | 4136 | } |
50483b2c | 4137 | else if (SvLEN(dstr)) |
3f7c398e | 4138 | Safefree(SvPVX_const(dstr)); |
79072805 | 4139 | } |
765f542d | 4140 | |
765f542d NC |
4141 | if (!isSwipe) { |
4142 | /* making another shared SV. */ | |
4143 | STRLEN cur = SvCUR(sstr); | |
4144 | STRLEN len = SvLEN(sstr); | |
f8c7b90f | 4145 | #ifdef PERL_OLD_COPY_ON_WRITE |
765f542d | 4146 | if (len) { |
b8f9541a | 4147 | assert (SvTYPE(dstr) >= SVt_PVIV); |
765f542d NC |
4148 | /* SvIsCOW_normal */ |
4149 | /* splice us in between source and next-after-source. */ | |
a29f6d03 NC |
4150 | SV_COW_NEXT_SV_SET(dstr, SV_COW_NEXT_SV(sstr)); |
4151 | SV_COW_NEXT_SV_SET(sstr, dstr); | |
940132f3 | 4152 | SvPV_set(dstr, SvPVX_mutable(sstr)); |
a604c751 NC |
4153 | } else |
4154 | #endif | |
4155 | { | |
765f542d | 4156 | /* SvIsCOW_shared_hash */ |
46187eeb NC |
4157 | DEBUG_C(PerlIO_printf(Perl_debug_log, |
4158 | "Copy on write: Sharing hash\n")); | |
b8f9541a | 4159 | |
bdd68bc3 | 4160 | assert (SvTYPE(dstr) >= SVt_PV); |
765f542d | 4161 | SvPV_set(dstr, |
d1db91c6 | 4162 | HEK_KEY(share_hek_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sstr))))); |
bdd68bc3 | 4163 | } |
87a1ef3d SP |
4164 | SvLEN_set(dstr, len); |
4165 | SvCUR_set(dstr, cur); | |
765f542d NC |
4166 | SvREADONLY_on(dstr); |
4167 | SvFAKE_on(dstr); | |
4168 | /* Relesase a global SV mutex. */ | |
4169 | } | |
4170 | else | |
765f542d | 4171 | { /* Passes the swipe test. */ |
78d1e721 | 4172 | SvPV_set(dstr, SvPVX_mutable(sstr)); |
765f542d NC |
4173 | SvLEN_set(dstr, SvLEN(sstr)); |
4174 | SvCUR_set(dstr, SvCUR(sstr)); | |
4175 | ||
4176 | SvTEMP_off(dstr); | |
4177 | (void)SvOK_off(sstr); /* NOTE: nukes most SvFLAGS on sstr */ | |
4178 | SvPV_set(sstr, Nullch); | |
4179 | SvLEN_set(sstr, 0); | |
4180 | SvCUR_set(sstr, 0); | |
4181 | SvTEMP_off(sstr); | |
4182 | } | |
4183 | } | |
9aa983d2 | 4184 | if (sflags & SVf_UTF8) |
a7cb1f99 | 4185 | SvUTF8_on(dstr); |
8990e307 | 4186 | if (sflags & SVp_NOK) { |
3332b3c1 JH |
4187 | SvNOKp_on(dstr); |
4188 | if (sflags & SVf_NOK) | |
4189 | SvFLAGS(dstr) |= SVf_NOK; | |
9d6ce603 | 4190 | SvNV_set(dstr, SvNVX(sstr)); |
79072805 | 4191 | } |
8990e307 | 4192 | if (sflags & SVp_IOK) { |
3332b3c1 JH |
4193 | (void)SvIOKp_on(dstr); |
4194 | if (sflags & SVf_IOK) | |
4195 | SvFLAGS(dstr) |= SVf_IOK; | |
2b1c7e3e | 4196 | if (sflags & SVf_IVisUV) |
25da4f38 | 4197 | SvIsUV_on(dstr); |
45977657 | 4198 | SvIV_set(dstr, SvIVX(sstr)); |
79072805 | 4199 | } |
92f0c265 | 4200 | if (SvVOK(sstr)) { |
7a5fa8a2 | 4201 | MAGIC *smg = mg_find(sstr,PERL_MAGIC_vstring); |
ece467f9 JP |
4202 | sv_magic(dstr, NULL, PERL_MAGIC_vstring, |
4203 | smg->mg_ptr, smg->mg_len); | |
439cb1c4 | 4204 | SvRMAGICAL_on(dstr); |
7a5fa8a2 | 4205 | } |
79072805 | 4206 | } |
8990e307 | 4207 | else if (sflags & SVp_IOK) { |
3332b3c1 JH |
4208 | if (sflags & SVf_IOK) |
4209 | (void)SvIOK_only(dstr); | |
4210 | else { | |
9cbac4c7 DM |
4211 | (void)SvOK_off(dstr); |
4212 | (void)SvIOKp_on(dstr); | |
3332b3c1 JH |
4213 | } |
4214 | /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */ | |
2b1c7e3e | 4215 | if (sflags & SVf_IVisUV) |
25da4f38 | 4216 | SvIsUV_on(dstr); |
45977657 | 4217 | SvIV_set(dstr, SvIVX(sstr)); |
3332b3c1 JH |
4218 | if (sflags & SVp_NOK) { |
4219 | if (sflags & SVf_NOK) | |
4220 | (void)SvNOK_on(dstr); | |
4221 | else | |
4222 | (void)SvNOKp_on(dstr); | |
9d6ce603 | 4223 | SvNV_set(dstr, SvNVX(sstr)); |
3332b3c1 JH |
4224 | } |
4225 | } | |
4226 | else if (sflags & SVp_NOK) { | |
4227 | if (sflags & SVf_NOK) | |
4228 | (void)SvNOK_only(dstr); | |
4229 | else { | |
9cbac4c7 | 4230 | (void)SvOK_off(dstr); |
3332b3c1 JH |
4231 | SvNOKp_on(dstr); |
4232 | } | |
9d6ce603 | 4233 | SvNV_set(dstr, SvNVX(sstr)); |
79072805 LW |
4234 | } |
4235 | else { | |
20408e3c | 4236 | if (dtype == SVt_PVGV) { |
e476b1b5 | 4237 | if (ckWARN(WARN_MISC)) |
9014280d | 4238 | Perl_warner(aTHX_ packWARN(WARN_MISC), "Undefined value assigned to typeglob"); |
20408e3c GS |
4239 | } |
4240 | else | |
4241 | (void)SvOK_off(dstr); | |
a0d0e21e | 4242 | } |
27c9684d AP |
4243 | if (SvTAINTED(sstr)) |
4244 | SvTAINT(dstr); | |
79072805 LW |
4245 | } |
4246 | ||
954c1994 GS |
4247 | /* |
4248 | =for apidoc sv_setsv_mg | |
4249 | ||
4250 | Like C<sv_setsv>, but also handles 'set' magic. | |
4251 | ||
4252 | =cut | |
4253 | */ | |
4254 | ||
79072805 | 4255 | void |
864dbfa3 | 4256 | Perl_sv_setsv_mg(pTHX_ SV *dstr, register SV *sstr) |
ef50df4b GS |
4257 | { |
4258 | sv_setsv(dstr,sstr); | |
4259 | SvSETMAGIC(dstr); | |
4260 | } | |
4261 | ||
f8c7b90f | 4262 | #ifdef PERL_OLD_COPY_ON_WRITE |
ed252734 NC |
4263 | SV * |
4264 | Perl_sv_setsv_cow(pTHX_ SV *dstr, SV *sstr) | |
4265 | { | |
4266 | STRLEN cur = SvCUR(sstr); | |
4267 | STRLEN len = SvLEN(sstr); | |
4268 | register char *new_pv; | |
4269 | ||
4270 | if (DEBUG_C_TEST) { | |
4271 | PerlIO_printf(Perl_debug_log, "Fast copy on write: %p -> %p\n", | |
4272 | sstr, dstr); | |
4273 | sv_dump(sstr); | |
4274 | if (dstr) | |
4275 | sv_dump(dstr); | |
4276 | } | |
4277 | ||
4278 | if (dstr) { | |
4279 | if (SvTHINKFIRST(dstr)) | |
4280 | sv_force_normal_flags(dstr, SV_COW_DROP_PV); | |
3f7c398e SP |
4281 | else if (SvPVX_const(dstr)) |
4282 | Safefree(SvPVX_const(dstr)); | |
ed252734 NC |
4283 | } |
4284 | else | |
4285 | new_SV(dstr); | |
862a34c6 | 4286 | SvUPGRADE(dstr, SVt_PVIV); |
ed252734 NC |
4287 | |
4288 | assert (SvPOK(sstr)); | |
4289 | assert (SvPOKp(sstr)); | |
4290 | assert (!SvIOK(sstr)); | |
4291 | assert (!SvIOKp(sstr)); | |
4292 | assert (!SvNOK(sstr)); | |
4293 | assert (!SvNOKp(sstr)); | |
4294 | ||
4295 | if (SvIsCOW(sstr)) { | |
4296 | ||
4297 | if (SvLEN(sstr) == 0) { | |
4298 | /* source is a COW shared hash key. */ | |
ed252734 NC |
4299 | DEBUG_C(PerlIO_printf(Perl_debug_log, |
4300 | "Fast copy on write: Sharing hash\n")); | |
d1db91c6 | 4301 | new_pv = HEK_KEY(share_hek_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sstr)))); |
ed252734 NC |
4302 | goto common_exit; |
4303 | } | |
4304 | SV_COW_NEXT_SV_SET(dstr, SV_COW_NEXT_SV(sstr)); | |
4305 | } else { | |
4306 | assert ((SvFLAGS(sstr) & CAN_COW_MASK) == CAN_COW_FLAGS); | |
862a34c6 | 4307 | SvUPGRADE(sstr, SVt_PVIV); |
ed252734 NC |
4308 | SvREADONLY_on(sstr); |
4309 | SvFAKE_on(sstr); | |
4310 | DEBUG_C(PerlIO_printf(Perl_debug_log, | |
4311 | "Fast copy on write: Converting sstr to COW\n")); | |
4312 | SV_COW_NEXT_SV_SET(dstr, sstr); | |
4313 | } | |
4314 | SV_COW_NEXT_SV_SET(sstr, dstr); | |
940132f3 | 4315 | new_pv = SvPVX_mutable(sstr); |
ed252734 NC |
4316 | |
4317 | common_exit: | |
4318 | SvPV_set(dstr, new_pv); | |
4319 | SvFLAGS(dstr) = (SVt_PVIV|SVf_POK|SVp_POK|SVf_FAKE|SVf_READONLY); | |
4320 | if (SvUTF8(sstr)) | |
4321 | SvUTF8_on(dstr); | |
87a1ef3d SP |
4322 | SvLEN_set(dstr, len); |
4323 | SvCUR_set(dstr, cur); | |
ed252734 NC |
4324 | if (DEBUG_C_TEST) { |
4325 | sv_dump(dstr); | |
4326 | } | |
4327 | return dstr; | |
4328 | } | |
4329 | #endif | |
4330 | ||
954c1994 GS |
4331 | /* |
4332 | =for apidoc sv_setpvn | |
4333 | ||
4334 | Copies a string into an SV. The C<len> parameter indicates the number of | |
9e09f5f2 MHM |
4335 | bytes to be copied. If the C<ptr> argument is NULL the SV will become |
4336 | undefined. Does not handle 'set' magic. See C<sv_setpvn_mg>. | |
954c1994 GS |
4337 | |
4338 | =cut | |
4339 | */ | |
4340 | ||
ef50df4b | 4341 | void |
864dbfa3 | 4342 | Perl_sv_setpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len) |
79072805 | 4343 | { |
c6f8c383 | 4344 | register char *dptr; |
22c522df | 4345 | |
765f542d | 4346 | SV_CHECK_THINKFIRST_COW_DROP(sv); |
463ee0b2 | 4347 | if (!ptr) { |
a0d0e21e | 4348 | (void)SvOK_off(sv); |
463ee0b2 LW |
4349 | return; |
4350 | } | |
22c522df JH |
4351 | else { |
4352 | /* len is STRLEN which is unsigned, need to copy to signed */ | |
a3b680e6 | 4353 | const IV iv = len; |
9c5ffd7c JH |
4354 | if (iv < 0) |
4355 | Perl_croak(aTHX_ "panic: sv_setpvn called with negative strlen"); | |
22c522df | 4356 | } |
862a34c6 | 4357 | SvUPGRADE(sv, SVt_PV); |
c6f8c383 | 4358 | |
5902b6a9 | 4359 | dptr = SvGROW(sv, len + 1); |
c6f8c383 GA |
4360 | Move(ptr,dptr,len,char); |
4361 | dptr[len] = '\0'; | |
79072805 | 4362 | SvCUR_set(sv, len); |
1aa99e6b | 4363 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 | 4364 | SvTAINT(sv); |
79072805 LW |
4365 | } |
4366 | ||
954c1994 GS |
4367 | /* |
4368 | =for apidoc sv_setpvn_mg | |
4369 | ||
4370 | Like C<sv_setpvn>, but also handles 'set' magic. | |
4371 | ||
4372 | =cut | |
4373 | */ | |
4374 | ||
79072805 | 4375 | void |
864dbfa3 | 4376 | Perl_sv_setpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len) |
ef50df4b GS |
4377 | { |
4378 | sv_setpvn(sv,ptr,len); | |
4379 | SvSETMAGIC(sv); | |
4380 | } | |
4381 | ||
954c1994 GS |
4382 | /* |
4383 | =for apidoc sv_setpv | |
4384 | ||
4385 | Copies a string into an SV. The string must be null-terminated. Does not | |
4386 | handle 'set' magic. See C<sv_setpv_mg>. | |
4387 | ||
4388 | =cut | |
4389 | */ | |
4390 | ||
ef50df4b | 4391 | void |
864dbfa3 | 4392 | Perl_sv_setpv(pTHX_ register SV *sv, register const char *ptr) |
79072805 LW |
4393 | { |
4394 | register STRLEN len; | |
4395 | ||
765f542d | 4396 | SV_CHECK_THINKFIRST_COW_DROP(sv); |
463ee0b2 | 4397 | if (!ptr) { |
a0d0e21e | 4398 | (void)SvOK_off(sv); |
463ee0b2 LW |
4399 | return; |
4400 | } | |
79072805 | 4401 | len = strlen(ptr); |
862a34c6 | 4402 | SvUPGRADE(sv, SVt_PV); |
c6f8c383 | 4403 | |
79072805 | 4404 | SvGROW(sv, len + 1); |
463ee0b2 | 4405 | Move(ptr,SvPVX(sv),len+1,char); |
79072805 | 4406 | SvCUR_set(sv, len); |
1aa99e6b | 4407 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 LW |
4408 | SvTAINT(sv); |
4409 | } | |
4410 | ||
954c1994 GS |
4411 | /* |
4412 | =for apidoc sv_setpv_mg | |
4413 | ||
4414 | Like C<sv_setpv>, but also handles 'set' magic. | |
4415 | ||
4416 | =cut | |
4417 | */ | |
4418 | ||
463ee0b2 | 4419 | void |
864dbfa3 | 4420 | Perl_sv_setpv_mg(pTHX_ register SV *sv, register const char *ptr) |
ef50df4b GS |
4421 | { |
4422 | sv_setpv(sv,ptr); | |
4423 | SvSETMAGIC(sv); | |
4424 | } | |
4425 | ||
954c1994 GS |
4426 | /* |
4427 | =for apidoc sv_usepvn | |
4428 | ||
4429 | Tells an SV to use C<ptr> to find its string value. Normally the string is | |
1c846c1f | 4430 | stored inside the SV but sv_usepvn allows the SV to use an outside string. |
954c1994 GS |
4431 | The C<ptr> should point to memory that was allocated by C<malloc>. The |
4432 | string length, C<len>, must be supplied. This function will realloc the | |
4433 | memory pointed to by C<ptr>, so that pointer should not be freed or used by | |
4434 | the programmer after giving it to sv_usepvn. Does not handle 'set' magic. | |
4435 | See C<sv_usepvn_mg>. | |
4436 | ||
4437 | =cut | |
4438 | */ | |
4439 | ||
ef50df4b | 4440 | void |
864dbfa3 | 4441 | Perl_sv_usepvn(pTHX_ register SV *sv, register char *ptr, register STRLEN len) |
463ee0b2 | 4442 | { |
1936d2a7 | 4443 | STRLEN allocate; |
765f542d | 4444 | SV_CHECK_THINKFIRST_COW_DROP(sv); |
862a34c6 | 4445 | SvUPGRADE(sv, SVt_PV); |
463ee0b2 | 4446 | if (!ptr) { |
a0d0e21e | 4447 | (void)SvOK_off(sv); |
463ee0b2 LW |
4448 | return; |
4449 | } | |
3f7c398e | 4450 | if (SvPVX_const(sv)) |
8bd4d4c5 | 4451 | SvPV_free(sv); |
1936d2a7 NC |
4452 | |
4453 | allocate = PERL_STRLEN_ROUNDUP(len + 1); | |
7a9b70e9 | 4454 | ptr = saferealloc (ptr, allocate); |
f880fe2f | 4455 | SvPV_set(sv, ptr); |
463ee0b2 | 4456 | SvCUR_set(sv, len); |
1936d2a7 | 4457 | SvLEN_set(sv, allocate); |
463ee0b2 | 4458 | *SvEND(sv) = '\0'; |
1aa99e6b | 4459 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 | 4460 | SvTAINT(sv); |
79072805 LW |
4461 | } |
4462 | ||
954c1994 GS |
4463 | /* |
4464 | =for apidoc sv_usepvn_mg | |
4465 | ||
4466 | Like C<sv_usepvn>, but also handles 'set' magic. | |
4467 | ||
4468 | =cut | |
4469 | */ | |
4470 | ||
ef50df4b | 4471 | void |
864dbfa3 | 4472 | Perl_sv_usepvn_mg(pTHX_ register SV *sv, register char *ptr, register STRLEN len) |
ef50df4b | 4473 | { |
51c1089b | 4474 | sv_usepvn(sv,ptr,len); |
ef50df4b GS |
4475 | SvSETMAGIC(sv); |
4476 | } | |
4477 | ||
f8c7b90f | 4478 | #ifdef PERL_OLD_COPY_ON_WRITE |
765f542d NC |
4479 | /* Need to do this *after* making the SV normal, as we need the buffer |
4480 | pointer to remain valid until after we've copied it. If we let go too early, | |
4481 | another thread could invalidate it by unsharing last of the same hash key | |
4482 | (which it can do by means other than releasing copy-on-write Svs) | |
4483 | or by changing the other copy-on-write SVs in the loop. */ | |
4484 | STATIC void | |
bdd68bc3 | 4485 | S_sv_release_COW(pTHX_ register SV *sv, const char *pvx, STRLEN len, SV *after) |
765f542d NC |
4486 | { |
4487 | if (len) { /* this SV was SvIsCOW_normal(sv) */ | |
4488 | /* we need to find the SV pointing to us. */ | |
4489 | SV *current = SV_COW_NEXT_SV(after); | |
7a5fa8a2 | 4490 | |
765f542d NC |
4491 | if (current == sv) { |
4492 | /* The SV we point to points back to us (there were only two of us | |
4493 | in the loop.) | |
4494 | Hence other SV is no longer copy on write either. */ | |
4495 | SvFAKE_off(after); | |
4496 | SvREADONLY_off(after); | |
4497 | } else { | |
4498 | /* We need to follow the pointers around the loop. */ | |
4499 | SV *next; | |
4500 | while ((next = SV_COW_NEXT_SV(current)) != sv) { | |
4501 | assert (next); | |
4502 | current = next; | |
4503 | /* don't loop forever if the structure is bust, and we have | |
4504 | a pointer into a closed loop. */ | |
4505 | assert (current != after); | |
3f7c398e | 4506 | assert (SvPVX_const(current) == pvx); |
765f542d NC |
4507 | } |
4508 | /* Make the SV before us point to the SV after us. */ | |
a29f6d03 | 4509 | SV_COW_NEXT_SV_SET(current, after); |
765f542d NC |
4510 | } |
4511 | } else { | |
bdd68bc3 | 4512 | unshare_hek(SvSHARED_HEK_FROM_PV(pvx)); |
765f542d NC |
4513 | } |
4514 | } | |
4515 | ||
4516 | int | |
4517 | Perl_sv_release_IVX(pTHX_ register SV *sv) | |
4518 | { | |
4519 | if (SvIsCOW(sv)) | |
4520 | sv_force_normal_flags(sv, 0); | |
0c34ef67 MHM |
4521 | SvOOK_off(sv); |
4522 | return 0; | |
765f542d NC |
4523 | } |
4524 | #endif | |
645c22ef DM |
4525 | /* |
4526 | =for apidoc sv_force_normal_flags | |
4527 | ||
4528 | Undo various types of fakery on an SV: if the PV is a shared string, make | |
4529 | a private copy; if we're a ref, stop refing; if we're a glob, downgrade to | |
765f542d NC |
4530 | an xpvmg; if we're a copy-on-write scalar, this is the on-write time when |
4531 | we do the copy, and is also used locally. If C<SV_COW_DROP_PV> is set | |
4532 | then a copy-on-write scalar drops its PV buffer (if any) and becomes | |
4533 | SvPOK_off rather than making a copy. (Used where this scalar is about to be | |
d3050d9d | 4534 | set to some other value.) In addition, the C<flags> parameter gets passed to |
765f542d NC |
4535 | C<sv_unref_flags()> when unrefing. C<sv_force_normal> calls this function |
4536 | with flags set to 0. | |
645c22ef DM |
4537 | |
4538 | =cut | |
4539 | */ | |
4540 | ||
6fc92669 | 4541 | void |
840a7b70 | 4542 | Perl_sv_force_normal_flags(pTHX_ register SV *sv, U32 flags) |
0f15f207 | 4543 | { |
f8c7b90f | 4544 | #ifdef PERL_OLD_COPY_ON_WRITE |
765f542d NC |
4545 | if (SvREADONLY(sv)) { |
4546 | /* At this point I believe I should acquire a global SV mutex. */ | |
4547 | if (SvFAKE(sv)) { | |
a28509cc AL |
4548 | const char *pvx = SvPVX_const(sv); |
4549 | const STRLEN len = SvLEN(sv); | |
4550 | const STRLEN cur = SvCUR(sv); | |
a28509cc | 4551 | SV * const next = SV_COW_NEXT_SV(sv); /* next COW sv in the loop. */ |
46187eeb NC |
4552 | if (DEBUG_C_TEST) { |
4553 | PerlIO_printf(Perl_debug_log, | |
4554 | "Copy on write: Force normal %ld\n", | |
4555 | (long) flags); | |
e419cbc5 | 4556 | sv_dump(sv); |
46187eeb | 4557 | } |
765f542d NC |
4558 | SvFAKE_off(sv); |
4559 | SvREADONLY_off(sv); | |
4560 | /* This SV doesn't own the buffer, so need to New() a new one: */ | |
f880fe2f | 4561 | SvPV_set(sv, (char*)0); |
87a1ef3d | 4562 | SvLEN_set(sv, 0); |
765f542d NC |
4563 | if (flags & SV_COW_DROP_PV) { |
4564 | /* OK, so we don't need to copy our buffer. */ | |
4565 | SvPOK_off(sv); | |
4566 | } else { | |
4567 | SvGROW(sv, cur + 1); | |
4568 | Move(pvx,SvPVX(sv),cur,char); | |
87a1ef3d | 4569 | SvCUR_set(sv, cur); |
765f542d NC |
4570 | *SvEND(sv) = '\0'; |
4571 | } | |
bdd68bc3 | 4572 | sv_release_COW(sv, pvx, len, next); |
46187eeb | 4573 | if (DEBUG_C_TEST) { |
e419cbc5 | 4574 | sv_dump(sv); |
46187eeb | 4575 | } |
765f542d | 4576 | } |
923e4eb5 | 4577 | else if (IN_PERL_RUNTIME) |
765f542d NC |
4578 | Perl_croak(aTHX_ PL_no_modify); |
4579 | /* At this point I believe that I can drop the global SV mutex. */ | |
4580 | } | |
4581 | #else | |
2213622d | 4582 | if (SvREADONLY(sv)) { |
1c846c1f | 4583 | if (SvFAKE(sv)) { |
a433f3d2 | 4584 | const char *pvx = SvPVX_const(sv); |
66a1b24b | 4585 | const STRLEN len = SvCUR(sv); |
10bcdfd6 NC |
4586 | SvFAKE_off(sv); |
4587 | SvREADONLY_off(sv); | |
66a1b24b AL |
4588 | SvPV_set(sv, Nullch); |
4589 | SvLEN_set(sv, 0); | |
1c846c1f | 4590 | SvGROW(sv, len + 1); |
3f7c398e | 4591 | Move(pvx,SvPVX_const(sv),len,char); |
1c846c1f | 4592 | *SvEND(sv) = '\0'; |
bdd68bc3 | 4593 | unshare_hek(SvSHARED_HEK_FROM_PV(pvx)); |
1c846c1f | 4594 | } |
923e4eb5 | 4595 | else if (IN_PERL_RUNTIME) |
cea2e8a9 | 4596 | Perl_croak(aTHX_ PL_no_modify); |
0f15f207 | 4597 | } |
765f542d | 4598 | #endif |
2213622d | 4599 | if (SvROK(sv)) |
840a7b70 | 4600 | sv_unref_flags(sv, flags); |
6fc92669 GS |
4601 | else if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV) |
4602 | sv_unglob(sv); | |
0f15f207 | 4603 | } |
1c846c1f | 4604 | |
645c22ef DM |
4605 | /* |
4606 | =for apidoc sv_force_normal | |
4607 | ||
4608 | Undo various types of fakery on an SV: if the PV is a shared string, make | |
4609 | a private copy; if we're a ref, stop refing; if we're a glob, downgrade to | |
4610 | an xpvmg. See also C<sv_force_normal_flags>. | |
4611 | ||
4612 | =cut | |
4613 | */ | |
4614 | ||
840a7b70 IZ |
4615 | void |
4616 | Perl_sv_force_normal(pTHX_ register SV *sv) | |
4617 | { | |
4618 | sv_force_normal_flags(sv, 0); | |
4619 | } | |
4620 | ||
954c1994 GS |
4621 | /* |
4622 | =for apidoc sv_chop | |
4623 | ||
1c846c1f | 4624 | Efficient removal of characters from the beginning of the string buffer. |
954c1994 GS |
4625 | SvPOK(sv) must be true and the C<ptr> must be a pointer to somewhere inside |
4626 | the string buffer. The C<ptr> becomes the first character of the adjusted | |
645c22ef | 4627 | string. Uses the "OOK hack". |
3f7c398e | 4628 | Beware: after this function returns, C<ptr> and SvPVX_const(sv) may no longer |
31869a79 | 4629 | refer to the same chunk of data. |
954c1994 GS |
4630 | |
4631 | =cut | |
4632 | */ | |
4633 | ||
79072805 | 4634 | void |
f54cb97a | 4635 | Perl_sv_chop(pTHX_ register SV *sv, register const char *ptr) |
79072805 LW |
4636 | { |
4637 | register STRLEN delta; | |
a0d0e21e | 4638 | if (!ptr || !SvPOKp(sv)) |
79072805 | 4639 | return; |
3f7c398e | 4640 | delta = ptr - SvPVX_const(sv); |
2213622d | 4641 | SV_CHECK_THINKFIRST(sv); |
79072805 LW |
4642 | if (SvTYPE(sv) < SVt_PVIV) |
4643 | sv_upgrade(sv,SVt_PVIV); | |
4644 | ||
4645 | if (!SvOOK(sv)) { | |
50483b2c | 4646 | if (!SvLEN(sv)) { /* make copy of shared string */ |
3f7c398e | 4647 | const char *pvx = SvPVX_const(sv); |
a28509cc | 4648 | const STRLEN len = SvCUR(sv); |
50483b2c | 4649 | SvGROW(sv, len + 1); |
3f7c398e | 4650 | Move(pvx,SvPVX_const(sv),len,char); |
50483b2c JD |
4651 | *SvEND(sv) = '\0'; |
4652 | } | |
45977657 | 4653 | SvIV_set(sv, 0); |
a4bfb290 AB |
4654 | /* Same SvOOK_on but SvOOK_on does a SvIOK_off |
4655 | and we do that anyway inside the SvNIOK_off | |
4656 | */ | |
7a5fa8a2 | 4657 | SvFLAGS(sv) |= SVf_OOK; |
79072805 | 4658 | } |
a4bfb290 | 4659 | SvNIOK_off(sv); |
b162af07 SP |
4660 | SvLEN_set(sv, SvLEN(sv) - delta); |
4661 | SvCUR_set(sv, SvCUR(sv) - delta); | |
f880fe2f | 4662 | SvPV_set(sv, SvPVX(sv) + delta); |
45977657 | 4663 | SvIV_set(sv, SvIVX(sv) + delta); |
79072805 LW |
4664 | } |
4665 | ||
09540bc3 JH |
4666 | /* sv_catpvn() is now a macro using Perl_sv_catpvn_flags(); |
4667 | * this function provided for binary compatibility only | |
4668 | */ | |
4669 | ||
4670 | void | |
4671 | Perl_sv_catpvn(pTHX_ SV *dsv, const char* sstr, STRLEN slen) | |
4672 | { | |
4673 | sv_catpvn_flags(dsv, sstr, slen, SV_GMAGIC); | |
4674 | } | |
4675 | ||
954c1994 GS |
4676 | /* |
4677 | =for apidoc sv_catpvn | |
4678 | ||
4679 | Concatenates the string onto the end of the string which is in the SV. The | |
1e54db1a JH |
4680 | C<len> indicates number of bytes to copy. If the SV has the UTF-8 |
4681 | status set, then the bytes appended should be valid UTF-8. | |
d5ce4a7c | 4682 | Handles 'get' magic, but not 'set' magic. See C<sv_catpvn_mg>. |
954c1994 | 4683 | |
8d6d96c1 HS |
4684 | =for apidoc sv_catpvn_flags |
4685 | ||
4686 | Concatenates the string onto the end of the string which is in the SV. The | |
1e54db1a JH |
4687 | C<len> indicates number of bytes to copy. If the SV has the UTF-8 |
4688 | status set, then the bytes appended should be valid UTF-8. | |
8d6d96c1 HS |
4689 | If C<flags> has C<SV_GMAGIC> bit set, will C<mg_get> on C<dsv> if |
4690 | appropriate, else not. C<sv_catpvn> and C<sv_catpvn_nomg> are implemented | |
4691 | in terms of this function. | |
4692 | ||
4693 | =cut | |
4694 | */ | |
4695 | ||
4696 | void | |
4697 | Perl_sv_catpvn_flags(pTHX_ register SV *dsv, register const char *sstr, register STRLEN slen, I32 flags) | |
4698 | { | |
4699 | STRLEN dlen; | |
f54cb97a | 4700 | const char *dstr = SvPV_force_flags(dsv, dlen, flags); |
8d6d96c1 | 4701 | |
8d6d96c1 HS |
4702 | SvGROW(dsv, dlen + slen + 1); |
4703 | if (sstr == dstr) | |
3f7c398e | 4704 | sstr = SvPVX_const(dsv); |
8d6d96c1 | 4705 | Move(sstr, SvPVX(dsv) + dlen, slen, char); |
b162af07 | 4706 | SvCUR_set(dsv, SvCUR(dsv) + slen); |
8d6d96c1 HS |
4707 | *SvEND(dsv) = '\0'; |
4708 | (void)SvPOK_only_UTF8(dsv); /* validate pointer */ | |
4709 | SvTAINT(dsv); | |
79072805 LW |
4710 | } |
4711 | ||
954c1994 GS |
4712 | /* |
4713 | =for apidoc sv_catpvn_mg | |
4714 | ||
4715 | Like C<sv_catpvn>, but also handles 'set' magic. | |
4716 | ||
4717 | =cut | |
4718 | */ | |
4719 | ||
79072805 | 4720 | void |
864dbfa3 | 4721 | Perl_sv_catpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len) |
ef50df4b GS |
4722 | { |
4723 | sv_catpvn(sv,ptr,len); | |
4724 | SvSETMAGIC(sv); | |
4725 | } | |
4726 | ||
09540bc3 JH |
4727 | /* sv_catsv() is now a macro using Perl_sv_catsv_flags(); |
4728 | * this function provided for binary compatibility only | |
4729 | */ | |
4730 | ||
4731 | void | |
4732 | Perl_sv_catsv(pTHX_ SV *dstr, register SV *sstr) | |
4733 | { | |
4734 | sv_catsv_flags(dstr, sstr, SV_GMAGIC); | |
4735 | } | |
4736 | ||
954c1994 GS |
4737 | /* |
4738 | =for apidoc sv_catsv | |
4739 | ||
13e8c8e3 JH |
4740 | Concatenates the string from SV C<ssv> onto the end of the string in |
4741 | SV C<dsv>. Modifies C<dsv> but not C<ssv>. Handles 'get' magic, but | |
4742 | not 'set' magic. See C<sv_catsv_mg>. | |
954c1994 | 4743 | |
8d6d96c1 HS |
4744 | =for apidoc sv_catsv_flags |
4745 | ||
4746 | Concatenates the string from SV C<ssv> onto the end of the string in | |
4747 | SV C<dsv>. Modifies C<dsv> but not C<ssv>. If C<flags> has C<SV_GMAGIC> | |
4748 | bit set, will C<mg_get> on the SVs if appropriate, else not. C<sv_catsv> | |
4749 | and C<sv_catsv_nomg> are implemented in terms of this function. | |
4750 | ||
4751 | =cut */ | |
4752 | ||
ef50df4b | 4753 | void |
8d6d96c1 | 4754 | Perl_sv_catsv_flags(pTHX_ SV *dsv, register SV *ssv, I32 flags) |
79072805 | 4755 | { |
4d84ee25 | 4756 | const char *spv; |
13e8c8e3 | 4757 | STRLEN slen; |
46199a12 | 4758 | if (!ssv) |
79072805 | 4759 | return; |
4d84ee25 | 4760 | if ((spv = SvPV_const(ssv, slen))) { |
4fd84b44 AD |
4761 | /* sutf8 and dutf8 were type bool, but under USE_ITHREADS, |
4762 | gcc version 2.95.2 20000220 (Debian GNU/Linux) for | |
8cf8f3d1 NIS |
4763 | Linux xxx 2.2.17 on sparc64 with gcc -O2, we erroneously |
4764 | get dutf8 = 0x20000000, (i.e. SVf_UTF8) even though | |
4fd84b44 AD |
4765 | dsv->sv_flags doesn't have that bit set. |
4766 | Andy Dougherty 12 Oct 2001 | |
4767 | */ | |
b464bac0 | 4768 | const I32 sutf8 = DO_UTF8(ssv); |
4fd84b44 | 4769 | I32 dutf8; |
13e8c8e3 | 4770 | |
8d6d96c1 HS |
4771 | if (SvGMAGICAL(dsv) && (flags & SV_GMAGIC)) |
4772 | mg_get(dsv); | |
4773 | dutf8 = DO_UTF8(dsv); | |
4774 | ||
4775 | if (dutf8 != sutf8) { | |
13e8c8e3 | 4776 | if (dutf8) { |
46199a12 | 4777 | /* Not modifying source SV, so taking a temporary copy. */ |
8d6d96c1 | 4778 | SV* csv = sv_2mortal(newSVpvn(spv, slen)); |
13e8c8e3 | 4779 | |
46199a12 | 4780 | sv_utf8_upgrade(csv); |
93524f2b | 4781 | spv = SvPV_const(csv, slen); |
13e8c8e3 | 4782 | } |
8d6d96c1 HS |
4783 | else |
4784 | sv_utf8_upgrade_nomg(dsv); | |
e84ff256 | 4785 | } |
8d6d96c1 | 4786 | sv_catpvn_nomg(dsv, spv, slen); |
560a288e | 4787 | } |
79072805 LW |
4788 | } |
4789 | ||
954c1994 GS |
4790 | /* |
4791 | =for apidoc sv_catsv_mg | |
4792 | ||
4793 | Like C<sv_catsv>, but also handles 'set' magic. | |
4794 | ||
4795 | =cut | |
4796 | */ | |
4797 | ||
79072805 | 4798 | void |
46199a12 | 4799 | Perl_sv_catsv_mg(pTHX_ SV *dsv, register SV *ssv) |
ef50df4b | 4800 | { |
46199a12 JH |
4801 | sv_catsv(dsv,ssv); |
4802 | SvSETMAGIC(dsv); | |
ef50df4b GS |
4803 | } |
4804 | ||
954c1994 GS |
4805 | /* |
4806 | =for apidoc sv_catpv | |
4807 | ||
4808 | Concatenates the string onto the end of the string which is in the SV. | |
1e54db1a JH |
4809 | If the SV has the UTF-8 status set, then the bytes appended should be |
4810 | valid UTF-8. Handles 'get' magic, but not 'set' magic. See C<sv_catpv_mg>. | |
954c1994 | 4811 | |
d5ce4a7c | 4812 | =cut */ |
954c1994 | 4813 | |
ef50df4b | 4814 | void |
0c981600 | 4815 | Perl_sv_catpv(pTHX_ register SV *sv, register const char *ptr) |
79072805 LW |
4816 | { |
4817 | register STRLEN len; | |
463ee0b2 | 4818 | STRLEN tlen; |
748a9306 | 4819 | char *junk; |
79072805 | 4820 | |
0c981600 | 4821 | if (!ptr) |
79072805 | 4822 | return; |
748a9306 | 4823 | junk = SvPV_force(sv, tlen); |
0c981600 | 4824 | len = strlen(ptr); |
463ee0b2 | 4825 | SvGROW(sv, tlen + len + 1); |
0c981600 | 4826 | if (ptr == junk) |
3f7c398e | 4827 | ptr = SvPVX_const(sv); |
0c981600 | 4828 | Move(ptr,SvPVX(sv)+tlen,len+1,char); |
b162af07 | 4829 | SvCUR_set(sv, SvCUR(sv) + len); |
d41ff1b8 | 4830 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 | 4831 | SvTAINT(sv); |
79072805 LW |
4832 | } |
4833 | ||
954c1994 GS |
4834 | /* |
4835 | =for apidoc sv_catpv_mg | |
4836 | ||
4837 | Like C<sv_catpv>, but also handles 'set' magic. | |
4838 | ||
4839 | =cut | |
4840 | */ | |
4841 | ||
ef50df4b | 4842 | void |
0c981600 | 4843 | Perl_sv_catpv_mg(pTHX_ register SV *sv, register const char *ptr) |
ef50df4b | 4844 | { |
0c981600 | 4845 | sv_catpv(sv,ptr); |
ef50df4b GS |
4846 | SvSETMAGIC(sv); |
4847 | } | |
4848 | ||
645c22ef DM |
4849 | /* |
4850 | =for apidoc newSV | |
4851 | ||
4852 | Create a new null SV, or if len > 0, create a new empty SVt_PV type SV | |
4853 | with an initial PV allocation of len+1. Normally accessed via the C<NEWSV> | |
4854 | macro. | |
4855 | ||
4856 | =cut | |
4857 | */ | |
4858 | ||
79072805 | 4859 | SV * |
864dbfa3 | 4860 | Perl_newSV(pTHX_ STRLEN len) |
79072805 LW |
4861 | { |
4862 | register SV *sv; | |
1c846c1f | 4863 | |
4561caa4 | 4864 | new_SV(sv); |
79072805 LW |
4865 | if (len) { |
4866 | sv_upgrade(sv, SVt_PV); | |
4867 | SvGROW(sv, len + 1); | |
4868 | } | |
4869 | return sv; | |
4870 | } | |
954c1994 | 4871 | /* |
92110913 | 4872 | =for apidoc sv_magicext |
954c1994 | 4873 | |
68795e93 | 4874 | Adds magic to an SV, upgrading it if necessary. Applies the |
2d8d5d5a | 4875 | supplied vtable and returns a pointer to the magic added. |
92110913 | 4876 | |
2d8d5d5a SH |
4877 | Note that C<sv_magicext> will allow things that C<sv_magic> will not. |
4878 | In particular, you can add magic to SvREADONLY SVs, and add more than | |
4879 | one instance of the same 'how'. | |
645c22ef | 4880 | |
2d8d5d5a SH |
4881 | If C<namlen> is greater than zero then a C<savepvn> I<copy> of C<name> is |
4882 | stored, if C<namlen> is zero then C<name> is stored as-is and - as another | |
4883 | special case - if C<(name && namlen == HEf_SVKEY)> then C<name> is assumed | |
4884 | to contain an C<SV*> and is stored as-is with its REFCNT incremented. | |
92110913 | 4885 | |
2d8d5d5a | 4886 | (This is now used as a subroutine by C<sv_magic>.) |
954c1994 GS |
4887 | |
4888 | =cut | |
4889 | */ | |
92110913 | 4890 | MAGIC * |
e1ec3a88 | 4891 | Perl_sv_magicext(pTHX_ SV* sv, SV* obj, int how, const MGVTBL *vtable, |
92110913 | 4892 | const char* name, I32 namlen) |
79072805 LW |
4893 | { |
4894 | MAGIC* mg; | |
68795e93 | 4895 | |
92110913 | 4896 | if (SvTYPE(sv) < SVt_PVMG) { |
862a34c6 | 4897 | SvUPGRADE(sv, SVt_PVMG); |
463ee0b2 | 4898 | } |
79072805 LW |
4899 | Newz(702,mg, 1, MAGIC); |
4900 | mg->mg_moremagic = SvMAGIC(sv); | |
b162af07 | 4901 | SvMAGIC_set(sv, mg); |
75f9d97a | 4902 | |
05f95b08 SB |
4903 | /* Sometimes a magic contains a reference loop, where the sv and |
4904 | object refer to each other. To prevent a reference loop that | |
4905 | would prevent such objects being freed, we look for such loops | |
4906 | and if we find one we avoid incrementing the object refcount. | |
87f0b213 JH |
4907 | |
4908 | Note we cannot do this to avoid self-tie loops as intervening RV must | |
b5ccf5f2 | 4909 | have its REFCNT incremented to keep it in existence. |
87f0b213 JH |
4910 | |
4911 | */ | |
14befaf4 DM |
4912 | if (!obj || obj == sv || |
4913 | how == PERL_MAGIC_arylen || | |
4914 | how == PERL_MAGIC_qr || | |
8d2f4536 | 4915 | how == PERL_MAGIC_symtab || |
75f9d97a JH |
4916 | (SvTYPE(obj) == SVt_PVGV && |
4917 | (GvSV(obj) == sv || GvHV(obj) == (HV*)sv || GvAV(obj) == (AV*)sv || | |
4918 | GvCV(obj) == (CV*)sv || GvIOp(obj) == (IO*)sv || | |
2628be26 | 4919 | GvFORM(obj) == (CV*)sv))) |
75f9d97a | 4920 | { |
8990e307 | 4921 | mg->mg_obj = obj; |
75f9d97a | 4922 | } |
85e6fe83 | 4923 | else { |
8990e307 | 4924 | mg->mg_obj = SvREFCNT_inc(obj); |
85e6fe83 LW |
4925 | mg->mg_flags |= MGf_REFCOUNTED; |
4926 | } | |
b5ccf5f2 YST |
4927 | |
4928 | /* Normal self-ties simply pass a null object, and instead of | |
4929 | using mg_obj directly, use the SvTIED_obj macro to produce a | |
4930 | new RV as needed. For glob "self-ties", we are tieing the PVIO | |
4931 | with an RV obj pointing to the glob containing the PVIO. In | |
4932 | this case, to avoid a reference loop, we need to weaken the | |
4933 | reference. | |
4934 | */ | |
4935 | ||
4936 | if (how == PERL_MAGIC_tiedscalar && SvTYPE(sv) == SVt_PVIO && | |
4937 | obj && SvROK(obj) && GvIO(SvRV(obj)) == (IO*)sv) | |
4938 | { | |
4939 | sv_rvweaken(obj); | |
4940 | } | |
4941 | ||
79072805 | 4942 | mg->mg_type = how; |
565764a8 | 4943 | mg->mg_len = namlen; |
9cbac4c7 | 4944 | if (name) { |
92110913 | 4945 | if (namlen > 0) |
1edc1566 | 4946 | mg->mg_ptr = savepvn(name, namlen); |
c6ee37c5 | 4947 | else if (namlen == HEf_SVKEY) |
1edc1566 | 4948 | mg->mg_ptr = (char*)SvREFCNT_inc((SV*)name); |
68795e93 | 4949 | else |
92110913 | 4950 | mg->mg_ptr = (char *) name; |
9cbac4c7 | 4951 | } |
92110913 | 4952 | mg->mg_virtual = vtable; |
68795e93 | 4953 | |
92110913 NIS |
4954 | mg_magical(sv); |
4955 | if (SvGMAGICAL(sv)) | |
4956 | SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK); | |
4957 | return mg; | |
4958 | } | |
4959 | ||
4960 | /* | |
4961 | =for apidoc sv_magic | |
1c846c1f | 4962 | |
92110913 NIS |
4963 | Adds magic to an SV. First upgrades C<sv> to type C<SVt_PVMG> if necessary, |
4964 | then adds a new magic item of type C<how> to the head of the magic list. | |
4965 | ||
2d8d5d5a SH |
4966 | See C<sv_magicext> (which C<sv_magic> now calls) for a description of the |
4967 | handling of the C<name> and C<namlen> arguments. | |
4968 | ||
4509d3fb SB |
4969 | You need to use C<sv_magicext> to add magic to SvREADONLY SVs and also |
4970 | to add more than one instance of the same 'how'. | |
4971 | ||
92110913 NIS |
4972 | =cut |
4973 | */ | |
4974 | ||
4975 | void | |
4976 | Perl_sv_magic(pTHX_ register SV *sv, SV *obj, int how, const char *name, I32 namlen) | |
68795e93 | 4977 | { |
e1ec3a88 | 4978 | const MGVTBL *vtable = 0; |
92110913 | 4979 | MAGIC* mg; |
92110913 | 4980 | |
f8c7b90f | 4981 | #ifdef PERL_OLD_COPY_ON_WRITE |
765f542d NC |
4982 | if (SvIsCOW(sv)) |
4983 | sv_force_normal_flags(sv, 0); | |
4984 | #endif | |
92110913 | 4985 | if (SvREADONLY(sv)) { |
923e4eb5 | 4986 | if (IN_PERL_RUNTIME |
92110913 NIS |
4987 | && how != PERL_MAGIC_regex_global |
4988 | && how != PERL_MAGIC_bm | |
4989 | && how != PERL_MAGIC_fm | |
4990 | && how != PERL_MAGIC_sv | |
e6469971 | 4991 | && how != PERL_MAGIC_backref |
92110913 NIS |
4992 | ) |
4993 | { | |
4994 | Perl_croak(aTHX_ PL_no_modify); | |
4995 | } | |
4996 | } | |
4997 | if (SvMAGICAL(sv) || (how == PERL_MAGIC_taint && SvTYPE(sv) >= SVt_PVMG)) { | |
4998 | if (SvMAGIC(sv) && (mg = mg_find(sv, how))) { | |
68795e93 NIS |
4999 | /* sv_magic() refuses to add a magic of the same 'how' as an |
5000 | existing one | |
92110913 NIS |
5001 | */ |
5002 | if (how == PERL_MAGIC_taint) | |
5003 | mg->mg_len |= 1; | |
5004 | return; | |
5005 | } | |
5006 | } | |
68795e93 | 5007 | |
79072805 | 5008 | switch (how) { |
14befaf4 | 5009 | case PERL_MAGIC_sv: |
92110913 | 5010 | vtable = &PL_vtbl_sv; |
79072805 | 5011 | break; |
14befaf4 | 5012 | case PERL_MAGIC_overload: |
92110913 | 5013 | vtable = &PL_vtbl_amagic; |
a0d0e21e | 5014 | break; |
14befaf4 | 5015 | case PERL_MAGIC_overload_elem: |
92110913 | 5016 | vtable = &PL_vtbl_amagicelem; |
a0d0e21e | 5017 | break; |
14befaf4 | 5018 | case PERL_MAGIC_overload_table: |
92110913 | 5019 | vtable = &PL_vtbl_ovrld; |
a0d0e21e | 5020 | break; |
14befaf4 | 5021 | case PERL_MAGIC_bm: |
92110913 | 5022 | vtable = &PL_vtbl_bm; |
79072805 | 5023 | break; |
14befaf4 | 5024 | case PERL_MAGIC_regdata: |
92110913 | 5025 | vtable = &PL_vtbl_regdata; |
6cef1e77 | 5026 | break; |
14befaf4 | 5027 | case PERL_MAGIC_regdatum: |
92110913 | 5028 | vtable = &PL_vtbl_regdatum; |
6cef1e77 | 5029 | break; |
14befaf4 | 5030 | case PERL_MAGIC_env: |
92110913 | 5031 | vtable = &PL_vtbl_env; |
79072805 | 5032 | break; |
14befaf4 | 5033 | case PERL_MAGIC_fm: |
92110913 | 5034 | vtable = &PL_vtbl_fm; |
55497cff | 5035 | break; |
14befaf4 | 5036 | case PERL_MAGIC_envelem: |
92110913 | 5037 | vtable = &PL_vtbl_envelem; |
79072805 | 5038 | break; |
14befaf4 | 5039 | case PERL_MAGIC_regex_global: |
92110913 | 5040 | vtable = &PL_vtbl_mglob; |
93a17b20 | 5041 | break; |
14befaf4 | 5042 | case PERL_MAGIC_isa: |
92110913 | 5043 | vtable = &PL_vtbl_isa; |
463ee0b2 | 5044 | break; |
14befaf4 | 5045 | case PERL_MAGIC_isaelem: |
92110913 | 5046 | vtable = &PL_vtbl_isaelem; |
463ee0b2 | 5047 | break; |
14befaf4 | 5048 | case PERL_MAGIC_nkeys: |
92110913 | 5049 | vtable = &PL_vtbl_nkeys; |
16660edb | 5050 | break; |
14befaf4 | 5051 | case PERL_MAGIC_dbfile: |
92110913 | 5052 | vtable = 0; |
93a17b20 | 5053 | break; |
14befaf4 | 5054 | case PERL_MAGIC_dbline: |
92110913 | 5055 | vtable = &PL_vtbl_dbline; |
79072805 | 5056 | break; |
36477c24 | 5057 | #ifdef USE_LOCALE_COLLATE |
14befaf4 | 5058 | case PERL_MAGIC_collxfrm: |
92110913 | 5059 | vtable = &PL_vtbl_collxfrm; |
bbce6d69 | 5060 | break; |
36477c24 | 5061 | #endif /* USE_LOCALE_COLLATE */ |
14befaf4 | 5062 | case PERL_MAGIC_tied: |
92110913 | 5063 | vtable = &PL_vtbl_pack; |
463ee0b2 | 5064 | break; |
14befaf4 DM |
5065 | case PERL_MAGIC_tiedelem: |
5066 | case PERL_MAGIC_tiedscalar: | |
92110913 | 5067 | vtable = &PL_vtbl_packelem; |
463ee0b2 | 5068 | break; |
14befaf4 | 5069 | case PERL_MAGIC_qr: |
92110913 | 5070 | vtable = &PL_vtbl_regexp; |
c277df42 | 5071 | break; |
14befaf4 | 5072 | case PERL_MAGIC_sig: |
92110913 | 5073 | vtable = &PL_vtbl_sig; |
79072805 | 5074 | break; |
14befaf4 | 5075 | case PERL_MAGIC_sigelem: |
92110913 | 5076 | vtable = &PL_vtbl_sigelem; |
79072805 | 5077 | break; |
14befaf4 | 5078 | case PERL_MAGIC_taint: |
92110913 | 5079 | vtable = &PL_vtbl_taint; |
463ee0b2 | 5080 | break; |
14befaf4 | 5081 | case PERL_MAGIC_uvar: |
92110913 | 5082 | vtable = &PL_vtbl_uvar; |
79072805 | 5083 | break; |
14befaf4 | 5084 | case PERL_MAGIC_vec: |
92110913 | 5085 | vtable = &PL_vtbl_vec; |
79072805 | 5086 | break; |
a3874608 | 5087 | case PERL_MAGIC_arylen_p: |
bfcb3514 | 5088 | case PERL_MAGIC_rhash: |
8d2f4536 | 5089 | case PERL_MAGIC_symtab: |
ece467f9 JP |
5090 | case PERL_MAGIC_vstring: |
5091 | vtable = 0; | |
5092 | break; | |
7e8c5dac HS |
5093 | case PERL_MAGIC_utf8: |
5094 | vtable = &PL_vtbl_utf8; | |
5095 | break; | |
14befaf4 | 5096 | case PERL_MAGIC_substr: |
92110913 | 5097 | vtable = &PL_vtbl_substr; |
79072805 | 5098 | break; |
14befaf4 | 5099 | case PERL_MAGIC_defelem: |
92110913 | 5100 | vtable = &PL_vtbl_defelem; |
5f05dabc | 5101 | break; |
14befaf4 | 5102 | case PERL_MAGIC_glob: |
92110913 | 5103 | vtable = &PL_vtbl_glob; |
79072805 | 5104 | break; |
14befaf4 | 5105 | case PERL_MAGIC_arylen: |
92110913 | 5106 | vtable = &PL_vtbl_arylen; |
79072805 | 5107 | break; |
14befaf4 | 5108 | case PERL_MAGIC_pos: |
92110913 | 5109 | vtable = &PL_vtbl_pos; |
a0d0e21e | 5110 | break; |
14befaf4 | 5111 | case PERL_MAGIC_backref: |
92110913 | 5112 | vtable = &PL_vtbl_backref; |
810b8aa5 | 5113 | break; |
14befaf4 DM |
5114 | case PERL_MAGIC_ext: |
5115 | /* Reserved for use by extensions not perl internals. */ | |
4633a7c4 LW |
5116 | /* Useful for attaching extension internal data to perl vars. */ |
5117 | /* Note that multiple extensions may clash if magical scalars */ | |
5118 | /* etc holding private data from one are passed to another. */ | |
a0d0e21e | 5119 | break; |
79072805 | 5120 | default: |
14befaf4 | 5121 | Perl_croak(aTHX_ "Don't know how to handle magic of type \\%o", how); |
463ee0b2 | 5122 | } |
68795e93 | 5123 | |
92110913 | 5124 | /* Rest of work is done else where */ |
27da23d5 | 5125 | mg = sv_magicext(sv,obj,how,(MGVTBL*)vtable,name,namlen); |
68795e93 | 5126 | |
92110913 NIS |
5127 | switch (how) { |
5128 | case PERL_MAGIC_taint: | |
5129 | mg->mg_len = 1; | |
5130 | break; | |
5131 | case PERL_MAGIC_ext: | |
5132 | case PERL_MAGIC_dbfile: | |
5133 | SvRMAGICAL_on(sv); | |
5134 | break; | |
5135 | } | |
463ee0b2 LW |
5136 | } |
5137 | ||
c461cf8f JH |
5138 | /* |
5139 | =for apidoc sv_unmagic | |
5140 | ||
645c22ef | 5141 | Removes all magic of type C<type> from an SV. |
c461cf8f JH |
5142 | |
5143 | =cut | |
5144 | */ | |
5145 | ||
463ee0b2 | 5146 | int |
864dbfa3 | 5147 | Perl_sv_unmagic(pTHX_ SV *sv, int type) |
463ee0b2 LW |
5148 | { |
5149 | MAGIC* mg; | |
5150 | MAGIC** mgp; | |
91bba347 | 5151 | if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv)) |
463ee0b2 LW |
5152 | return 0; |
5153 | mgp = &SvMAGIC(sv); | |
5154 | for (mg = *mgp; mg; mg = *mgp) { | |
5155 | if (mg->mg_type == type) { | |
e1ec3a88 | 5156 | const MGVTBL* const vtbl = mg->mg_virtual; |
463ee0b2 | 5157 | *mgp = mg->mg_moremagic; |
1d7c1841 | 5158 | if (vtbl && vtbl->svt_free) |
fc0dc3b3 | 5159 | CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg); |
14befaf4 | 5160 | if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) { |
92110913 | 5161 | if (mg->mg_len > 0) |
1edc1566 | 5162 | Safefree(mg->mg_ptr); |
565764a8 | 5163 | else if (mg->mg_len == HEf_SVKEY) |
1edc1566 | 5164 | SvREFCNT_dec((SV*)mg->mg_ptr); |
7e8c5dac HS |
5165 | else if (mg->mg_type == PERL_MAGIC_utf8 && mg->mg_ptr) |
5166 | Safefree(mg->mg_ptr); | |
9cbac4c7 | 5167 | } |
a0d0e21e LW |
5168 | if (mg->mg_flags & MGf_REFCOUNTED) |
5169 | SvREFCNT_dec(mg->mg_obj); | |
463ee0b2 LW |
5170 | Safefree(mg); |
5171 | } | |
5172 | else | |
5173 | mgp = &mg->mg_moremagic; | |
79072805 | 5174 | } |
91bba347 | 5175 | if (!SvMAGIC(sv)) { |
463ee0b2 | 5176 | SvMAGICAL_off(sv); |
06759ea0 | 5177 | SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_NOK|SVp_POK)) >> PRIVSHIFT; |
463ee0b2 LW |
5178 | } |
5179 | ||
5180 | return 0; | |
79072805 LW |
5181 | } |
5182 | ||
c461cf8f JH |
5183 | /* |
5184 | =for apidoc sv_rvweaken | |
5185 | ||
645c22ef DM |
5186 | Weaken a reference: set the C<SvWEAKREF> flag on this RV; give the |
5187 | referred-to SV C<PERL_MAGIC_backref> magic if it hasn't already; and | |
5188 | push a back-reference to this RV onto the array of backreferences | |
5189 | associated with that magic. | |
c461cf8f JH |
5190 | |
5191 | =cut | |
5192 | */ | |
5193 | ||
810b8aa5 | 5194 | SV * |
864dbfa3 | 5195 | Perl_sv_rvweaken(pTHX_ SV *sv) |
810b8aa5 GS |
5196 | { |
5197 | SV *tsv; | |
5198 | if (!SvOK(sv)) /* let undefs pass */ | |
5199 | return sv; | |
5200 | if (!SvROK(sv)) | |
cea2e8a9 | 5201 | Perl_croak(aTHX_ "Can't weaken a nonreference"); |
810b8aa5 | 5202 | else if (SvWEAKREF(sv)) { |
810b8aa5 | 5203 | if (ckWARN(WARN_MISC)) |
9014280d | 5204 | Perl_warner(aTHX_ packWARN(WARN_MISC), "Reference is already weak"); |
810b8aa5 GS |
5205 | return sv; |
5206 | } | |
5207 | tsv = SvRV(sv); | |
5208 | sv_add_backref(tsv, sv); | |
5209 | SvWEAKREF_on(sv); | |
1c846c1f | 5210 | SvREFCNT_dec(tsv); |
810b8aa5 GS |
5211 | return sv; |
5212 | } | |
5213 | ||
645c22ef DM |
5214 | /* Give tsv backref magic if it hasn't already got it, then push a |
5215 | * back-reference to sv onto the array associated with the backref magic. | |
5216 | */ | |
5217 | ||
810b8aa5 | 5218 | STATIC void |
cea2e8a9 | 5219 | S_sv_add_backref(pTHX_ SV *tsv, SV *sv) |
810b8aa5 GS |
5220 | { |
5221 | AV *av; | |
5222 | MAGIC *mg; | |
14befaf4 | 5223 | if (SvMAGICAL(tsv) && (mg = mg_find(tsv, PERL_MAGIC_backref))) |
810b8aa5 GS |
5224 | av = (AV*)mg->mg_obj; |
5225 | else { | |
5226 | av = newAV(); | |
14befaf4 | 5227 | sv_magic(tsv, (SV*)av, PERL_MAGIC_backref, NULL, 0); |
d99b02a1 DM |
5228 | /* av now has a refcnt of 2, which avoids it getting freed |
5229 | * before us during global cleanup. The extra ref is removed | |
5230 | * by magic_killbackrefs() when tsv is being freed */ | |
810b8aa5 | 5231 | } |
d91d49e8 | 5232 | if (AvFILLp(av) >= AvMAX(av)) { |
fdc9a813 | 5233 | I32 i; |
d91d49e8 | 5234 | SV **svp = AvARRAY(av); |
fdc9a813 AE |
5235 | for (i = AvFILLp(av); i >= 0; i--) |
5236 | if (!svp[i]) { | |
d91d49e8 MM |
5237 | svp[i] = sv; /* reuse the slot */ |
5238 | return; | |
5239 | } | |
d91d49e8 MM |
5240 | av_extend(av, AvFILLp(av)+1); |
5241 | } | |
5242 | AvARRAY(av)[++AvFILLp(av)] = sv; /* av_push() */ | |
810b8aa5 GS |
5243 | } |
5244 | ||
645c22ef DM |
5245 | /* delete a back-reference to ourselves from the backref magic associated |
5246 | * with the SV we point to. | |
5247 | */ | |
5248 | ||
1c846c1f | 5249 | STATIC void |
cea2e8a9 | 5250 | S_sv_del_backref(pTHX_ SV *sv) |
810b8aa5 GS |
5251 | { |
5252 | AV *av; | |
5253 | SV **svp; | |
5254 | I32 i; | |
5255 | SV *tsv = SvRV(sv); | |
c04a4dfe | 5256 | MAGIC *mg = NULL; |
14befaf4 | 5257 | if (!SvMAGICAL(tsv) || !(mg = mg_find(tsv, PERL_MAGIC_backref))) |
cea2e8a9 | 5258 | Perl_croak(aTHX_ "panic: del_backref"); |
810b8aa5 GS |
5259 | av = (AV *)mg->mg_obj; |
5260 | svp = AvARRAY(av); | |
fdc9a813 AE |
5261 | for (i = AvFILLp(av); i >= 0; i--) |
5262 | if (svp[i] == sv) svp[i] = Nullsv; | |
810b8aa5 GS |
5263 | } |
5264 | ||
954c1994 GS |
5265 | /* |
5266 | =for apidoc sv_insert | |
5267 | ||
5268 | Inserts a string at the specified offset/length within the SV. Similar to | |
5269 | the Perl substr() function. | |
5270 | ||
5271 | =cut | |
5272 | */ | |
5273 | ||
79072805 | 5274 | void |
e1ec3a88 | 5275 | Perl_sv_insert(pTHX_ SV *bigstr, STRLEN offset, STRLEN len, const char *little, STRLEN littlelen) |
79072805 LW |
5276 | { |
5277 | register char *big; | |
5278 | register char *mid; | |
5279 | register char *midend; | |
5280 | register char *bigend; | |
5281 | register I32 i; | |
6ff81951 | 5282 | STRLEN curlen; |
1c846c1f | 5283 | |
79072805 | 5284 | |
8990e307 | 5285 | if (!bigstr) |
cea2e8a9 | 5286 | Perl_croak(aTHX_ "Can't modify non-existent substring"); |
6ff81951 | 5287 | SvPV_force(bigstr, curlen); |
60fa28ff | 5288 | (void)SvPOK_only_UTF8(bigstr); |
6ff81951 GS |
5289 | if (offset + len > curlen) { |
5290 | SvGROW(bigstr, offset+len+1); | |
93524f2b | 5291 | Zero(SvPVX(bigstr)+curlen, offset+len-curlen, char); |
6ff81951 GS |
5292 | SvCUR_set(bigstr, offset+len); |
5293 | } | |
79072805 | 5294 | |
69b47968 | 5295 | SvTAINT(bigstr); |
79072805 LW |
5296 | i = littlelen - len; |
5297 | if (i > 0) { /* string might grow */ | |
a0d0e21e | 5298 | big = SvGROW(bigstr, SvCUR(bigstr) + i + 1); |
79072805 LW |
5299 | mid = big + offset + len; |
5300 | midend = bigend = big + SvCUR(bigstr); | |
5301 | bigend += i; | |
5302 | *bigend = '\0'; | |
5303 | while (midend > mid) /* shove everything down */ | |
5304 | *--bigend = *--midend; | |
5305 | Move(little,big+offset,littlelen,char); | |
b162af07 | 5306 | SvCUR_set(bigstr, SvCUR(bigstr) + i); |
79072805 LW |
5307 | SvSETMAGIC(bigstr); |
5308 | return; | |
5309 | } | |
5310 | else if (i == 0) { | |
463ee0b2 | 5311 | Move(little,SvPVX(bigstr)+offset,len,char); |
79072805 LW |
5312 | SvSETMAGIC(bigstr); |
5313 | return; | |
5314 | } | |
5315 | ||
463ee0b2 | 5316 | big = SvPVX(bigstr); |
79072805 LW |
5317 | mid = big + offset; |
5318 | midend = mid + len; | |
5319 | bigend = big + SvCUR(bigstr); | |
5320 | ||
5321 | if (midend > bigend) | |
cea2e8a9 | 5322 | Perl_croak(aTHX_ "panic: sv_insert"); |
79072805 LW |
5323 | |
5324 | if (mid - big > bigend - midend) { /* faster to shorten from end */ | |
5325 | if (littlelen) { | |
5326 | Move(little, mid, littlelen,char); | |
5327 | mid += littlelen; | |
5328 | } | |
5329 | i = bigend - midend; | |
5330 | if (i > 0) { | |
5331 | Move(midend, mid, i,char); | |
5332 | mid += i; | |
5333 | } | |
5334 | *mid = '\0'; | |
5335 | SvCUR_set(bigstr, mid - big); | |
5336 | } | |
155aba94 | 5337 | else if ((i = mid - big)) { /* faster from front */ |
79072805 LW |
5338 | midend -= littlelen; |
5339 | mid = midend; | |
5340 | sv_chop(bigstr,midend-i); | |
5341 | big += i; | |
5342 | while (i--) | |
5343 | *--midend = *--big; | |
5344 | if (littlelen) | |
5345 | Move(little, mid, littlelen,char); | |
5346 | } | |
5347 | else if (littlelen) { | |
5348 | midend -= littlelen; | |
5349 | sv_chop(bigstr,midend); | |
5350 | Move(little,midend,littlelen,char); | |
5351 | } | |
5352 | else { | |
5353 | sv_chop(bigstr,midend); | |
5354 | } | |
5355 | SvSETMAGIC(bigstr); | |
5356 | } | |
5357 | ||
c461cf8f JH |
5358 | /* |
5359 | =for apidoc sv_replace | |
5360 | ||
5361 | Make the first argument a copy of the second, then delete the original. | |
645c22ef DM |
5362 | The target SV physically takes over ownership of the body of the source SV |
5363 | and inherits its flags; however, the target keeps any magic it owns, | |
5364 | and any magic in the source is discarded. | |
ff276b08 | 5365 | Note that this is a rather specialist SV copying operation; most of the |
645c22ef | 5366 | time you'll want to use C<sv_setsv> or one of its many macro front-ends. |
c461cf8f JH |
5367 | |
5368 | =cut | |
5369 | */ | |
79072805 LW |
5370 | |
5371 | void | |
864dbfa3 | 5372 | Perl_sv_replace(pTHX_ register SV *sv, register SV *nsv) |
79072805 | 5373 | { |
a3b680e6 | 5374 | const U32 refcnt = SvREFCNT(sv); |
765f542d | 5375 | SV_CHECK_THINKFIRST_COW_DROP(sv); |
0453d815 | 5376 | if (SvREFCNT(nsv) != 1 && ckWARN_d(WARN_INTERNAL)) |
9014280d | 5377 | Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "Reference miscount in sv_replace()"); |
93a17b20 | 5378 | if (SvMAGICAL(sv)) { |
a0d0e21e LW |
5379 | if (SvMAGICAL(nsv)) |
5380 | mg_free(nsv); | |
5381 | else | |
5382 | sv_upgrade(nsv, SVt_PVMG); | |
b162af07 | 5383 | SvMAGIC_set(nsv, SvMAGIC(sv)); |
a0d0e21e | 5384 | SvFLAGS(nsv) |= SvMAGICAL(sv); |
93a17b20 | 5385 | SvMAGICAL_off(sv); |
b162af07 | 5386 | SvMAGIC_set(sv, NULL); |
93a17b20 | 5387 | } |
79072805 LW |
5388 | SvREFCNT(sv) = 0; |
5389 | sv_clear(sv); | |
477f5d66 | 5390 | assert(!SvREFCNT(sv)); |
fd0854ff DM |
5391 | #ifdef DEBUG_LEAKING_SCALARS |
5392 | sv->sv_flags = nsv->sv_flags; | |
5393 | sv->sv_any = nsv->sv_any; | |
5394 | sv->sv_refcnt = nsv->sv_refcnt; | |
f34d0642 | 5395 | sv->sv_u = nsv->sv_u; |
fd0854ff | 5396 | #else |
79072805 | 5397 | StructCopy(nsv,sv,SV); |
fd0854ff | 5398 | #endif |
7b2c381c NC |
5399 | /* Currently could join these into one piece of pointer arithmetic, but |
5400 | it would be unclear. */ | |
5401 | if(SvTYPE(sv) == SVt_IV) | |
5402 | SvANY(sv) | |
339049b0 | 5403 | = (XPVIV*)((char*)&(sv->sv_u.svu_iv) - STRUCT_OFFSET(XPVIV, xiv_iv)); |
7b2c381c | 5404 | else if (SvTYPE(sv) == SVt_RV) { |
339049b0 | 5405 | SvANY(sv) = &sv->sv_u.svu_rv; |
7b2c381c NC |
5406 | } |
5407 | ||
fd0854ff | 5408 | |
f8c7b90f | 5409 | #ifdef PERL_OLD_COPY_ON_WRITE |
d3d0e6f1 NC |
5410 | if (SvIsCOW_normal(nsv)) { |
5411 | /* We need to follow the pointers around the loop to make the | |
5412 | previous SV point to sv, rather than nsv. */ | |
5413 | SV *next; | |
5414 | SV *current = nsv; | |
5415 | while ((next = SV_COW_NEXT_SV(current)) != nsv) { | |
5416 | assert(next); | |
5417 | current = next; | |
3f7c398e | 5418 | assert(SvPVX_const(current) == SvPVX_const(nsv)); |
d3d0e6f1 NC |
5419 | } |
5420 | /* Make the SV before us point to the SV after us. */ | |
5421 | if (DEBUG_C_TEST) { | |
5422 | PerlIO_printf(Perl_debug_log, "previous is\n"); | |
5423 | sv_dump(current); | |
a29f6d03 NC |
5424 | PerlIO_printf(Perl_debug_log, |
5425 | "move it from 0x%"UVxf" to 0x%"UVxf"\n", | |
d3d0e6f1 NC |
5426 | (UV) SV_COW_NEXT_SV(current), (UV) sv); |
5427 | } | |
a29f6d03 | 5428 | SV_COW_NEXT_SV_SET(current, sv); |
d3d0e6f1 NC |
5429 | } |
5430 | #endif | |
79072805 | 5431 | SvREFCNT(sv) = refcnt; |
1edc1566 | 5432 | SvFLAGS(nsv) |= SVTYPEMASK; /* Mark as freed */ |
39cf41c2 | 5433 | SvREFCNT(nsv) = 0; |
463ee0b2 | 5434 | del_SV(nsv); |
79072805 LW |
5435 | } |
5436 | ||
c461cf8f JH |
5437 | /* |
5438 | =for apidoc sv_clear | |
5439 | ||
645c22ef DM |
5440 | Clear an SV: call any destructors, free up any memory used by the body, |
5441 | and free the body itself. The SV's head is I<not> freed, although | |
5442 | its type is set to all 1's so that it won't inadvertently be assumed | |
5443 | to be live during global destruction etc. | |
5444 | This function should only be called when REFCNT is zero. Most of the time | |
5445 | you'll want to call C<sv_free()> (or its macro wrapper C<SvREFCNT_dec>) | |
5446 | instead. | |
c461cf8f JH |
5447 | |
5448 | =cut | |
5449 | */ | |
5450 | ||
79072805 | 5451 | void |
864dbfa3 | 5452 | Perl_sv_clear(pTHX_ register SV *sv) |
79072805 | 5453 | { |
27da23d5 | 5454 | dVAR; |
ec12f114 | 5455 | HV* stash; |
79072805 LW |
5456 | assert(sv); |
5457 | assert(SvREFCNT(sv) == 0); | |
5458 | ||
ed6116ce | 5459 | if (SvOBJECT(sv)) { |
3280af22 | 5460 | if (PL_defstash) { /* Still have a symbol table? */ |
39644a26 | 5461 | dSP; |
d460ef45 | 5462 | do { |
b464bac0 | 5463 | CV* destructor; |
4e8e7886 | 5464 | stash = SvSTASH(sv); |
32251b26 | 5465 | destructor = StashHANDLER(stash,DESTROY); |
4e8e7886 | 5466 | if (destructor) { |
5cc433a6 AB |
5467 | SV* tmpref = newRV(sv); |
5468 | SvREADONLY_on(tmpref); /* DESTROY() could be naughty */ | |
4e8e7886 | 5469 | ENTER; |
e788e7d3 | 5470 | PUSHSTACKi(PERLSI_DESTROY); |
4e8e7886 GS |
5471 | EXTEND(SP, 2); |
5472 | PUSHMARK(SP); | |
5cc433a6 | 5473 | PUSHs(tmpref); |
4e8e7886 | 5474 | PUTBACK; |
44389ee9 | 5475 | call_sv((SV*)destructor, G_DISCARD|G_EVAL|G_KEEPERR|G_VOID); |
7a5fa8a2 NIS |
5476 | |
5477 | ||
d3acc0f7 | 5478 | POPSTACK; |
3095d977 | 5479 | SPAGAIN; |
4e8e7886 | 5480 | LEAVE; |
5cc433a6 AB |
5481 | if(SvREFCNT(tmpref) < 2) { |
5482 | /* tmpref is not kept alive! */ | |
5483 | SvREFCNT(sv)--; | |
b162af07 | 5484 | SvRV_set(tmpref, NULL); |
5cc433a6 AB |
5485 | SvROK_off(tmpref); |
5486 | } | |
5487 | SvREFCNT_dec(tmpref); | |
4e8e7886 GS |
5488 | } |
5489 | } while (SvOBJECT(sv) && SvSTASH(sv) != stash); | |
8ebc5c01 | 5490 | |
6f44e0a4 JP |
5491 | |
5492 | if (SvREFCNT(sv)) { | |
5493 | if (PL_in_clean_objs) | |
cea2e8a9 | 5494 | Perl_croak(aTHX_ "DESTROY created new reference to dead object '%s'", |
bfcb3514 | 5495 | HvNAME_get(stash)); |
6f44e0a4 JP |
5496 | /* DESTROY gave object new lease on life */ |
5497 | return; | |
5498 | } | |
a0d0e21e | 5499 | } |
4e8e7886 | 5500 | |
a0d0e21e | 5501 | if (SvOBJECT(sv)) { |
4e8e7886 | 5502 | SvREFCNT_dec(SvSTASH(sv)); /* possibly of changed persuasion */ |
a0d0e21e LW |
5503 | SvOBJECT_off(sv); /* Curse the object. */ |
5504 | if (SvTYPE(sv) != SVt_PVIO) | |
3280af22 | 5505 | --PL_sv_objcount; /* XXX Might want something more general */ |
a0d0e21e | 5506 | } |
463ee0b2 | 5507 | } |
524189f1 JH |
5508 | if (SvTYPE(sv) >= SVt_PVMG) { |
5509 | if (SvMAGIC(sv)) | |
5510 | mg_free(sv); | |
bce8f412 | 5511 | if (SvTYPE(sv) == SVt_PVMG && SvFLAGS(sv) & SVpad_TYPED) |
524189f1 JH |
5512 | SvREFCNT_dec(SvSTASH(sv)); |
5513 | } | |
ec12f114 | 5514 | stash = NULL; |
79072805 | 5515 | switch (SvTYPE(sv)) { |
8990e307 | 5516 | case SVt_PVIO: |
df0bd2f4 GS |
5517 | if (IoIFP(sv) && |
5518 | IoIFP(sv) != PerlIO_stdin() && | |
5f05dabc | 5519 | IoIFP(sv) != PerlIO_stdout() && |
5520 | IoIFP(sv) != PerlIO_stderr()) | |
93578b34 | 5521 | { |
f2b5be74 | 5522 | io_close((IO*)sv, FALSE); |
93578b34 | 5523 | } |
1d7c1841 | 5524 | if (IoDIRP(sv) && !(IoFLAGS(sv) & IOf_FAKE_DIRP)) |
1236053a | 5525 | PerlDir_close(IoDIRP(sv)); |
1d7c1841 | 5526 | IoDIRP(sv) = (DIR*)NULL; |
8990e307 LW |
5527 | Safefree(IoTOP_NAME(sv)); |
5528 | Safefree(IoFMT_NAME(sv)); | |
5529 | Safefree(IoBOTTOM_NAME(sv)); | |
a0d0e21e | 5530 | /* FALL THROUGH */ |
79072805 | 5531 | case SVt_PVBM: |
a0d0e21e | 5532 | goto freescalar; |
79072805 | 5533 | case SVt_PVCV: |
748a9306 | 5534 | case SVt_PVFM: |
85e6fe83 | 5535 | cv_undef((CV*)sv); |
a0d0e21e | 5536 | goto freescalar; |
79072805 | 5537 | case SVt_PVHV: |
85e6fe83 | 5538 | hv_undef((HV*)sv); |
a0d0e21e | 5539 | break; |
79072805 | 5540 | case SVt_PVAV: |
85e6fe83 | 5541 | av_undef((AV*)sv); |
a0d0e21e | 5542 | break; |
02270b4e | 5543 | case SVt_PVLV: |
dd28f7bb DM |
5544 | if (LvTYPE(sv) == 'T') { /* for tie: return HE to pool */ |
5545 | SvREFCNT_dec(HeKEY_sv((HE*)LvTARG(sv))); | |
5546 | HeNEXT((HE*)LvTARG(sv)) = PL_hv_fetch_ent_mh; | |
5547 | PL_hv_fetch_ent_mh = (HE*)LvTARG(sv); | |
5548 | } | |
5549 | else if (LvTYPE(sv) != 't') /* unless tie: unrefcnted fake SV** */ | |
5550 | SvREFCNT_dec(LvTARG(sv)); | |
02270b4e | 5551 | goto freescalar; |
a0d0e21e | 5552 | case SVt_PVGV: |
1edc1566 | 5553 | gp_free((GV*)sv); |
a0d0e21e | 5554 | Safefree(GvNAME(sv)); |
ec12f114 JPC |
5555 | /* cannot decrease stash refcount yet, as we might recursively delete |
5556 | ourselves when the refcnt drops to zero. Delay SvREFCNT_dec | |
5557 | of stash until current sv is completely gone. | |
5558 | -- JohnPC, 27 Mar 1998 */ | |
5559 | stash = GvSTASH(sv); | |
a0d0e21e | 5560 | /* FALL THROUGH */ |
79072805 | 5561 | case SVt_PVMG: |
79072805 LW |
5562 | case SVt_PVNV: |
5563 | case SVt_PVIV: | |
a0d0e21e | 5564 | freescalar: |
5228ca4e NC |
5565 | /* Don't bother with SvOOK_off(sv); as we're only going to free it. */ |
5566 | if (SvOOK(sv)) { | |
93524f2b | 5567 | SvPV_set(sv, SvPVX_mutable(sv) - SvIVX(sv)); |
5228ca4e NC |
5568 | /* Don't even bother with turning off the OOK flag. */ |
5569 | } | |
79072805 LW |
5570 | /* FALL THROUGH */ |
5571 | case SVt_PV: | |
a0d0e21e | 5572 | case SVt_RV: |
810b8aa5 GS |
5573 | if (SvROK(sv)) { |
5574 | if (SvWEAKREF(sv)) | |
5575 | sv_del_backref(sv); | |
5576 | else | |
5577 | SvREFCNT_dec(SvRV(sv)); | |
5578 | } | |
f8c7b90f | 5579 | #ifdef PERL_OLD_COPY_ON_WRITE |
3f7c398e | 5580 | else if (SvPVX_const(sv)) { |
765f542d NC |
5581 | if (SvIsCOW(sv)) { |
5582 | /* I believe I need to grab the global SV mutex here and | |
5583 | then recheck the COW status. */ | |
46187eeb NC |
5584 | if (DEBUG_C_TEST) { |
5585 | PerlIO_printf(Perl_debug_log, "Copy on write: clear\n"); | |
e419cbc5 | 5586 | sv_dump(sv); |
46187eeb | 5587 | } |
bdd68bc3 NC |
5588 | sv_release_COW(sv, SvPVX_const(sv), SvLEN(sv), |
5589 | SV_COW_NEXT_SV(sv)); | |
765f542d NC |
5590 | /* And drop it here. */ |
5591 | SvFAKE_off(sv); | |
5592 | } else if (SvLEN(sv)) { | |
3f7c398e | 5593 | Safefree(SvPVX_const(sv)); |
765f542d NC |
5594 | } |
5595 | } | |
5596 | #else | |
3f7c398e SP |
5597 | else if (SvPVX_const(sv) && SvLEN(sv)) |
5598 | Safefree(SvPVX_const(sv)); | |
5599 | else if (SvPVX_const(sv) && SvREADONLY(sv) && SvFAKE(sv)) { | |
bdd68bc3 | 5600 | unshare_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sv))); |
1c846c1f NIS |
5601 | SvFAKE_off(sv); |
5602 | } | |
765f542d | 5603 | #endif |
79072805 | 5604 | break; |
a0d0e21e | 5605 | /* |
79072805 | 5606 | case SVt_NV: |
79072805 | 5607 | case SVt_IV: |
79072805 LW |
5608 | case SVt_NULL: |
5609 | break; | |
a0d0e21e | 5610 | */ |
79072805 LW |
5611 | } |
5612 | ||
5613 | switch (SvTYPE(sv)) { | |
5614 | case SVt_NULL: | |
5615 | break; | |
79072805 | 5616 | case SVt_IV: |
79072805 LW |
5617 | break; |
5618 | case SVt_NV: | |
5619 | del_XNV(SvANY(sv)); | |
5620 | break; | |
ed6116ce | 5621 | case SVt_RV: |
ed6116ce | 5622 | break; |
79072805 LW |
5623 | case SVt_PV: |
5624 | del_XPV(SvANY(sv)); | |
5625 | break; | |
5626 | case SVt_PVIV: | |
5627 | del_XPVIV(SvANY(sv)); | |
5628 | break; | |
5629 | case SVt_PVNV: | |
5630 | del_XPVNV(SvANY(sv)); | |
5631 | break; | |
5632 | case SVt_PVMG: | |
5633 | del_XPVMG(SvANY(sv)); | |
5634 | break; | |
5635 | case SVt_PVLV: | |
5636 | del_XPVLV(SvANY(sv)); | |
5637 | break; | |
5638 | case SVt_PVAV: | |
5639 | del_XPVAV(SvANY(sv)); | |
5640 | break; | |
5641 | case SVt_PVHV: | |
5642 | del_XPVHV(SvANY(sv)); | |
5643 | break; | |
5644 | case SVt_PVCV: | |
5645 | del_XPVCV(SvANY(sv)); | |
5646 | break; | |
5647 | case SVt_PVGV: | |
5648 | del_XPVGV(SvANY(sv)); | |
ec12f114 JPC |
5649 | /* code duplication for increased performance. */ |
5650 | SvFLAGS(sv) &= SVf_BREAK; | |
5651 | SvFLAGS(sv) |= SVTYPEMASK; | |
5652 | /* decrease refcount of the stash that owns this GV, if any */ | |
5653 | if (stash) | |
5654 | SvREFCNT_dec(stash); | |
5655 | return; /* not break, SvFLAGS reset already happened */ | |
79072805 LW |
5656 | case SVt_PVBM: |
5657 | del_XPVBM(SvANY(sv)); | |
5658 | break; | |
5659 | case SVt_PVFM: | |
5660 | del_XPVFM(SvANY(sv)); | |
5661 | break; | |
8990e307 LW |
5662 | case SVt_PVIO: |
5663 | del_XPVIO(SvANY(sv)); | |
5664 | break; | |
79072805 | 5665 | } |
a0d0e21e | 5666 | SvFLAGS(sv) &= SVf_BREAK; |
8990e307 | 5667 | SvFLAGS(sv) |= SVTYPEMASK; |
79072805 LW |
5668 | } |
5669 | ||
645c22ef DM |
5670 | /* |
5671 | =for apidoc sv_newref | |
5672 | ||
5673 | Increment an SV's reference count. Use the C<SvREFCNT_inc()> wrapper | |
5674 | instead. | |
5675 | ||
5676 | =cut | |
5677 | */ | |
5678 | ||
79072805 | 5679 | SV * |
864dbfa3 | 5680 | Perl_sv_newref(pTHX_ SV *sv) |
79072805 | 5681 | { |
463ee0b2 | 5682 | if (sv) |
4db098f4 | 5683 | (SvREFCNT(sv))++; |
79072805 LW |
5684 | return sv; |
5685 | } | |
5686 | ||
c461cf8f JH |
5687 | /* |
5688 | =for apidoc sv_free | |
5689 | ||
645c22ef DM |
5690 | Decrement an SV's reference count, and if it drops to zero, call |
5691 | C<sv_clear> to invoke destructors and free up any memory used by | |
5692 | the body; finally, deallocate the SV's head itself. | |
5693 | Normally called via a wrapper macro C<SvREFCNT_dec>. | |
c461cf8f JH |
5694 | |
5695 | =cut | |
5696 | */ | |
5697 | ||
79072805 | 5698 | void |
864dbfa3 | 5699 | Perl_sv_free(pTHX_ SV *sv) |
79072805 | 5700 | { |
27da23d5 | 5701 | dVAR; |
79072805 LW |
5702 | if (!sv) |
5703 | return; | |
a0d0e21e LW |
5704 | if (SvREFCNT(sv) == 0) { |
5705 | if (SvFLAGS(sv) & SVf_BREAK) | |
645c22ef DM |
5706 | /* this SV's refcnt has been artificially decremented to |
5707 | * trigger cleanup */ | |
a0d0e21e | 5708 | return; |
3280af22 | 5709 | if (PL_in_clean_all) /* All is fair */ |
1edc1566 | 5710 | return; |
d689ffdd JP |
5711 | if (SvREADONLY(sv) && SvIMMORTAL(sv)) { |
5712 | /* make sure SvREFCNT(sv)==0 happens very seldom */ | |
5713 | SvREFCNT(sv) = (~(U32)0)/2; | |
5714 | return; | |
5715 | } | |
0453d815 | 5716 | if (ckWARN_d(WARN_INTERNAL)) |
d5dede04 | 5717 | Perl_warner(aTHX_ packWARN(WARN_INTERNAL), |
472d47bc SB |
5718 | "Attempt to free unreferenced scalar: SV 0x%"UVxf |
5719 | pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE); | |
79072805 LW |
5720 | return; |
5721 | } | |
4db098f4 | 5722 | if (--(SvREFCNT(sv)) > 0) |
8990e307 | 5723 | return; |
8c4d3c90 NC |
5724 | Perl_sv_free2(aTHX_ sv); |
5725 | } | |
5726 | ||
5727 | void | |
5728 | Perl_sv_free2(pTHX_ SV *sv) | |
5729 | { | |
27da23d5 | 5730 | dVAR; |
463ee0b2 LW |
5731 | #ifdef DEBUGGING |
5732 | if (SvTEMP(sv)) { | |
0453d815 | 5733 | if (ckWARN_d(WARN_DEBUGGING)) |
9014280d | 5734 | Perl_warner(aTHX_ packWARN(WARN_DEBUGGING), |
472d47bc SB |
5735 | "Attempt to free temp prematurely: SV 0x%"UVxf |
5736 | pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE); | |
79072805 | 5737 | return; |
79072805 | 5738 | } |
463ee0b2 | 5739 | #endif |
d689ffdd JP |
5740 | if (SvREADONLY(sv) && SvIMMORTAL(sv)) { |
5741 | /* make sure SvREFCNT(sv)==0 happens very seldom */ | |
5742 | SvREFCNT(sv) = (~(U32)0)/2; | |
5743 | return; | |
5744 | } | |
79072805 | 5745 | sv_clear(sv); |
477f5d66 CS |
5746 | if (! SvREFCNT(sv)) |
5747 | del_SV(sv); | |
79072805 LW |
5748 | } |
5749 | ||
954c1994 GS |
5750 | /* |
5751 | =for apidoc sv_len | |
5752 | ||
645c22ef DM |
5753 | Returns the length of the string in the SV. Handles magic and type |
5754 | coercion. See also C<SvCUR>, which gives raw access to the xpv_cur slot. | |
954c1994 GS |
5755 | |
5756 | =cut | |
5757 | */ | |
5758 | ||
79072805 | 5759 | STRLEN |
864dbfa3 | 5760 | Perl_sv_len(pTHX_ register SV *sv) |
79072805 | 5761 | { |
463ee0b2 | 5762 | STRLEN len; |
79072805 LW |
5763 | |
5764 | if (!sv) | |
5765 | return 0; | |
5766 | ||
8990e307 | 5767 | if (SvGMAGICAL(sv)) |
565764a8 | 5768 | len = mg_length(sv); |
8990e307 | 5769 | else |
4d84ee25 | 5770 | (void)SvPV_const(sv, len); |
463ee0b2 | 5771 | return len; |
79072805 LW |
5772 | } |
5773 | ||
c461cf8f JH |
5774 | /* |
5775 | =for apidoc sv_len_utf8 | |
5776 | ||
5777 | Returns the number of characters in the string in an SV, counting wide | |
1e54db1a | 5778 | UTF-8 bytes as a single character. Handles magic and type coercion. |
c461cf8f JH |
5779 | |
5780 | =cut | |
5781 | */ | |
5782 | ||
7e8c5dac HS |
5783 | /* |
5784 | * The length is cached in PERL_UTF8_magic, in the mg_len field. Also the | |
5785 | * mg_ptr is used, by sv_pos_u2b(), see the comments of S_utf8_mg_pos_init(). | |
5786 | * (Note that the mg_len is not the length of the mg_ptr field.) | |
7a5fa8a2 | 5787 | * |
7e8c5dac HS |
5788 | */ |
5789 | ||
a0ed51b3 | 5790 | STRLEN |
864dbfa3 | 5791 | Perl_sv_len_utf8(pTHX_ register SV *sv) |
a0ed51b3 | 5792 | { |
a0ed51b3 LW |
5793 | if (!sv) |
5794 | return 0; | |
5795 | ||
a0ed51b3 | 5796 | if (SvGMAGICAL(sv)) |
b76347f2 | 5797 | return mg_length(sv); |
a0ed51b3 | 5798 | else |
b76347f2 | 5799 | { |
7e8c5dac | 5800 | STRLEN len, ulen; |
e62f0680 | 5801 | const U8 *s = (U8*)SvPV_const(sv, len); |
7e8c5dac HS |
5802 | MAGIC *mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_utf8) : 0; |
5803 | ||
e23c8137 | 5804 | if (mg && mg->mg_len != -1 && (mg->mg_len > 0 || len == 0)) { |
7e8c5dac | 5805 | ulen = mg->mg_len; |
e23c8137 JH |
5806 | #ifdef PERL_UTF8_CACHE_ASSERT |
5807 | assert(ulen == Perl_utf8_length(aTHX_ s, s + len)); | |
5808 | #endif | |
5809 | } | |
7e8c5dac HS |
5810 | else { |
5811 | ulen = Perl_utf8_length(aTHX_ s, s + len); | |
5812 | if (!mg && !SvREADONLY(sv)) { | |
5813 | sv_magic(sv, 0, PERL_MAGIC_utf8, 0, 0); | |
5814 | mg = mg_find(sv, PERL_MAGIC_utf8); | |
5815 | assert(mg); | |
5816 | } | |
5817 | if (mg) | |
5818 | mg->mg_len = ulen; | |
5819 | } | |
5820 | return ulen; | |
5821 | } | |
5822 | } | |
5823 | ||
5824 | /* S_utf8_mg_pos_init() is used to initialize the mg_ptr field of | |
5825 | * a PERL_UTF8_magic. The mg_ptr is used to store the mapping | |
5826 | * between UTF-8 and byte offsets. There are two (substr offset and substr | |
5827 | * length, the i offset, PERL_MAGIC_UTF8_CACHESIZE) times two (UTF-8 offset | |
5828 | * and byte offset) cache positions. | |
5829 | * | |
5830 | * The mg_len field is used by sv_len_utf8(), see its comments. | |
5831 | * Note that the mg_len is not the length of the mg_ptr field. | |
5832 | * | |
5833 | */ | |
5834 | STATIC bool | |
245d4a47 NC |
5835 | S_utf8_mg_pos_init(pTHX_ SV *sv, MAGIC **mgp, STRLEN **cachep, I32 i, |
5836 | I32 offsetp, const U8 *s, const U8 *start) | |
7e8c5dac | 5837 | { |
7a5fa8a2 | 5838 | bool found = FALSE; |
7e8c5dac HS |
5839 | |
5840 | if (SvMAGICAL(sv) && !SvREADONLY(sv)) { | |
8f78557a | 5841 | if (!*mgp) |
27da23d5 | 5842 | *mgp = sv_magicext(sv, 0, PERL_MAGIC_utf8, (MGVTBL*)&PL_vtbl_utf8, 0, 0); |
7e8c5dac | 5843 | assert(*mgp); |
b76347f2 | 5844 | |
7e8c5dac HS |
5845 | if ((*mgp)->mg_ptr) |
5846 | *cachep = (STRLEN *) (*mgp)->mg_ptr; | |
5847 | else { | |
5848 | Newz(0, *cachep, PERL_MAGIC_UTF8_CACHESIZE * 2, STRLEN); | |
5849 | (*mgp)->mg_ptr = (char *) *cachep; | |
5850 | } | |
5851 | assert(*cachep); | |
5852 | ||
a3b680e6 | 5853 | (*cachep)[i] = offsetp; |
7e8c5dac HS |
5854 | (*cachep)[i+1] = s - start; |
5855 | found = TRUE; | |
a0ed51b3 | 5856 | } |
7e8c5dac HS |
5857 | |
5858 | return found; | |
a0ed51b3 LW |
5859 | } |
5860 | ||
645c22ef | 5861 | /* |
7e8c5dac HS |
5862 | * S_utf8_mg_pos() is used to query and update mg_ptr field of |
5863 | * a PERL_UTF8_magic. The mg_ptr is used to store the mapping | |
5864 | * between UTF-8 and byte offsets. See also the comments of | |
5865 | * S_utf8_mg_pos_init(). | |
5866 | * | |
5867 | */ | |
5868 | STATIC bool | |
245d4a47 | 5869 | S_utf8_mg_pos(pTHX_ SV *sv, MAGIC **mgp, STRLEN **cachep, I32 i, I32 *offsetp, I32 uoff, const U8 **sp, const U8 *start, const U8 *send) |
7e8c5dac HS |
5870 | { |
5871 | bool found = FALSE; | |
5872 | ||
5873 | if (SvMAGICAL(sv) && !SvREADONLY(sv)) { | |
5874 | if (!*mgp) | |
5875 | *mgp = mg_find(sv, PERL_MAGIC_utf8); | |
5876 | if (*mgp && (*mgp)->mg_ptr) { | |
5877 | *cachep = (STRLEN *) (*mgp)->mg_ptr; | |
e23c8137 | 5878 | ASSERT_UTF8_CACHE(*cachep); |
667208dd | 5879 | if ((*cachep)[i] == (STRLEN)uoff) /* An exact match. */ |
7a5fa8a2 | 5880 | found = TRUE; |
7e8c5dac HS |
5881 | else { /* We will skip to the right spot. */ |
5882 | STRLEN forw = 0; | |
5883 | STRLEN backw = 0; | |
a3b680e6 | 5884 | const U8* p = NULL; |
7e8c5dac HS |
5885 | |
5886 | /* The assumption is that going backward is half | |
5887 | * the speed of going forward (that's where the | |
5888 | * 2 * backw in the below comes from). (The real | |
5889 | * figure of course depends on the UTF-8 data.) */ | |
5890 | ||
667208dd | 5891 | if ((*cachep)[i] > (STRLEN)uoff) { |
7e8c5dac | 5892 | forw = uoff; |
667208dd | 5893 | backw = (*cachep)[i] - (STRLEN)uoff; |
7e8c5dac HS |
5894 | |
5895 | if (forw < 2 * backw) | |
5896 | p = start; | |
5897 | else | |
5898 | p = start + (*cachep)[i+1]; | |
5899 | } | |
5900 | /* Try this only for the substr offset (i == 0), | |
5901 | * not for the substr length (i == 2). */ | |
5902 | else if (i == 0) { /* (*cachep)[i] < uoff */ | |
a3b680e6 | 5903 | const STRLEN ulen = sv_len_utf8(sv); |
7e8c5dac | 5904 | |
667208dd JH |
5905 | if ((STRLEN)uoff < ulen) { |
5906 | forw = (STRLEN)uoff - (*cachep)[i]; | |
5907 | backw = ulen - (STRLEN)uoff; | |
7e8c5dac HS |
5908 | |
5909 | if (forw < 2 * backw) | |
5910 | p = start + (*cachep)[i+1]; | |
5911 | else | |
5912 | p = send; | |
5913 | } | |
5914 | ||
5915 | /* If the string is not long enough for uoff, | |
5916 | * we could extend it, but not at this low a level. */ | |
5917 | } | |
5918 | ||
5919 | if (p) { | |
5920 | if (forw < 2 * backw) { | |
5921 | while (forw--) | |
5922 | p += UTF8SKIP(p); | |
5923 | } | |
5924 | else { | |
5925 | while (backw--) { | |
5926 | p--; | |
5927 | while (UTF8_IS_CONTINUATION(*p)) | |
5928 | p--; | |
5929 | } | |
5930 | } | |
5931 | ||
5932 | /* Update the cache. */ | |
667208dd | 5933 | (*cachep)[i] = (STRLEN)uoff; |
7e8c5dac | 5934 | (*cachep)[i+1] = p - start; |
8f78557a AE |
5935 | |
5936 | /* Drop the stale "length" cache */ | |
5937 | if (i == 0) { | |
5938 | (*cachep)[2] = 0; | |
5939 | (*cachep)[3] = 0; | |
5940 | } | |
7a5fa8a2 | 5941 | |
7e8c5dac HS |
5942 | found = TRUE; |
5943 | } | |
5944 | } | |
5945 | if (found) { /* Setup the return values. */ | |
5946 | *offsetp = (*cachep)[i+1]; | |
5947 | *sp = start + *offsetp; | |
5948 | if (*sp >= send) { | |
5949 | *sp = send; | |
5950 | *offsetp = send - start; | |
5951 | } | |
5952 | else if (*sp < start) { | |
5953 | *sp = start; | |
5954 | *offsetp = 0; | |
5955 | } | |
5956 | } | |
5957 | } | |
e23c8137 JH |
5958 | #ifdef PERL_UTF8_CACHE_ASSERT |
5959 | if (found) { | |
5960 | U8 *s = start; | |
5961 | I32 n = uoff; | |
5962 | ||
5963 | while (n-- && s < send) | |
5964 | s += UTF8SKIP(s); | |
5965 | ||
5966 | if (i == 0) { | |
5967 | assert(*offsetp == s - start); | |
5968 | assert((*cachep)[0] == (STRLEN)uoff); | |
5969 | assert((*cachep)[1] == *offsetp); | |
5970 | } | |
5971 | ASSERT_UTF8_CACHE(*cachep); | |
5972 | } | |
5973 | #endif | |
7e8c5dac | 5974 | } |
e23c8137 | 5975 | |
7e8c5dac HS |
5976 | return found; |
5977 | } | |
7a5fa8a2 | 5978 | |
7e8c5dac | 5979 | /* |
645c22ef DM |
5980 | =for apidoc sv_pos_u2b |
5981 | ||
1e54db1a | 5982 | Converts the value pointed to by offsetp from a count of UTF-8 chars from |
645c22ef DM |
5983 | the start of the string, to a count of the equivalent number of bytes; if |
5984 | lenp is non-zero, it does the same to lenp, but this time starting from | |
5985 | the offset, rather than from the start of the string. Handles magic and | |
5986 | type coercion. | |
5987 | ||
5988 | =cut | |
5989 | */ | |
5990 | ||
7e8c5dac HS |
5991 | /* |
5992 | * sv_pos_u2b() uses, like sv_pos_b2u(), the mg_ptr of the potential | |
5993 | * PERL_UTF8_magic of the sv to store the mapping between UTF-8 and | |
5994 | * byte offsets. See also the comments of S_utf8_mg_pos(). | |
5995 | * | |
5996 | */ | |
5997 | ||
a0ed51b3 | 5998 | void |
864dbfa3 | 5999 | Perl_sv_pos_u2b(pTHX_ register SV *sv, I32* offsetp, I32* lenp) |
a0ed51b3 | 6000 | { |
245d4a47 | 6001 | const U8 *start; |
a0ed51b3 LW |
6002 | STRLEN len; |
6003 | ||
6004 | if (!sv) | |
6005 | return; | |
6006 | ||
245d4a47 | 6007 | start = (U8*)SvPV_const(sv, len); |
7e8c5dac | 6008 | if (len) { |
b464bac0 AL |
6009 | STRLEN boffset = 0; |
6010 | STRLEN *cache = 0; | |
245d4a47 NC |
6011 | const U8 *s = start; |
6012 | I32 uoffset = *offsetp; | |
6013 | const U8 *send = s + len; | |
6014 | MAGIC *mg = 0; | |
6015 | bool found = FALSE; | |
7e8c5dac | 6016 | |
bdf77a2a | 6017 | if (utf8_mg_pos(sv, &mg, &cache, 0, offsetp, *offsetp, &s, start, send)) |
7e8c5dac HS |
6018 | found = TRUE; |
6019 | if (!found && uoffset > 0) { | |
6020 | while (s < send && uoffset--) | |
6021 | s += UTF8SKIP(s); | |
6022 | if (s >= send) | |
6023 | s = send; | |
a3b680e6 | 6024 | if (utf8_mg_pos_init(sv, &mg, &cache, 0, *offsetp, s, start)) |
7e8c5dac HS |
6025 | boffset = cache[1]; |
6026 | *offsetp = s - start; | |
6027 | } | |
6028 | if (lenp) { | |
6029 | found = FALSE; | |
6030 | start = s; | |
ec062429 | 6031 | if (utf8_mg_pos(sv, &mg, &cache, 2, lenp, *lenp, &s, start, send)) { |
7e8c5dac HS |
6032 | *lenp -= boffset; |
6033 | found = TRUE; | |
6034 | } | |
6035 | if (!found && *lenp > 0) { | |
6036 | I32 ulen = *lenp; | |
6037 | if (ulen > 0) | |
6038 | while (s < send && ulen--) | |
6039 | s += UTF8SKIP(s); | |
6040 | if (s >= send) | |
6041 | s = send; | |
a3b680e6 | 6042 | utf8_mg_pos_init(sv, &mg, &cache, 2, *lenp, s, start); |
7e8c5dac HS |
6043 | } |
6044 | *lenp = s - start; | |
6045 | } | |
e23c8137 | 6046 | ASSERT_UTF8_CACHE(cache); |
7e8c5dac HS |
6047 | } |
6048 | else { | |
6049 | *offsetp = 0; | |
6050 | if (lenp) | |
6051 | *lenp = 0; | |
a0ed51b3 | 6052 | } |
e23c8137 | 6053 | |
a0ed51b3 LW |
6054 | return; |
6055 | } | |
6056 | ||
645c22ef DM |
6057 | /* |
6058 | =for apidoc sv_pos_b2u | |
6059 | ||
6060 | Converts the value pointed to by offsetp from a count of bytes from the | |
1e54db1a | 6061 | start of the string, to a count of the equivalent number of UTF-8 chars. |
645c22ef DM |
6062 | Handles magic and type coercion. |
6063 | ||
6064 | =cut | |
6065 | */ | |
6066 | ||
7e8c5dac HS |
6067 | /* |
6068 | * sv_pos_b2u() uses, like sv_pos_u2b(), the mg_ptr of the potential | |
6069 | * PERL_UTF8_magic of the sv to store the mapping between UTF-8 and | |
6070 | * byte offsets. See also the comments of S_utf8_mg_pos(). | |
6071 | * | |
6072 | */ | |
6073 | ||
a0ed51b3 | 6074 | void |
7e8c5dac | 6075 | Perl_sv_pos_b2u(pTHX_ register SV* sv, I32* offsetp) |
a0ed51b3 | 6076 | { |
83003860 | 6077 | const U8* s; |
a0ed51b3 LW |
6078 | STRLEN len; |
6079 | ||
6080 | if (!sv) | |
6081 | return; | |
6082 | ||
83003860 | 6083 | s = (const U8*)SvPV_const(sv, len); |
eb160463 | 6084 | if ((I32)len < *offsetp) |
a0dbb045 | 6085 | Perl_croak(aTHX_ "panic: sv_pos_b2u: bad byte offset"); |
7e8c5dac | 6086 | else { |
83003860 | 6087 | const U8* send = s + *offsetp; |
7e8c5dac HS |
6088 | MAGIC* mg = NULL; |
6089 | STRLEN *cache = NULL; | |
6090 | ||
6091 | len = 0; | |
6092 | ||
6093 | if (SvMAGICAL(sv) && !SvREADONLY(sv)) { | |
6094 | mg = mg_find(sv, PERL_MAGIC_utf8); | |
6095 | if (mg && mg->mg_ptr) { | |
6096 | cache = (STRLEN *) mg->mg_ptr; | |
c5661c80 | 6097 | if (cache[1] == (STRLEN)*offsetp) { |
7e8c5dac HS |
6098 | /* An exact match. */ |
6099 | *offsetp = cache[0]; | |
6100 | ||
6101 | return; | |
6102 | } | |
c5661c80 | 6103 | else if (cache[1] < (STRLEN)*offsetp) { |
7e8c5dac HS |
6104 | /* We already know part of the way. */ |
6105 | len = cache[0]; | |
6106 | s += cache[1]; | |
7a5fa8a2 | 6107 | /* Let the below loop do the rest. */ |
7e8c5dac HS |
6108 | } |
6109 | else { /* cache[1] > *offsetp */ | |
6110 | /* We already know all of the way, now we may | |
6111 | * be able to walk back. The same assumption | |
6112 | * is made as in S_utf8_mg_pos(), namely that | |
6113 | * walking backward is twice slower than | |
6114 | * walking forward. */ | |
6115 | STRLEN forw = *offsetp; | |
6116 | STRLEN backw = cache[1] - *offsetp; | |
6117 | ||
6118 | if (!(forw < 2 * backw)) { | |
83003860 | 6119 | const U8 *p = s + cache[1]; |
7e8c5dac | 6120 | STRLEN ubackw = 0; |
7a5fa8a2 | 6121 | |
a5b510f2 AE |
6122 | cache[1] -= backw; |
6123 | ||
7e8c5dac HS |
6124 | while (backw--) { |
6125 | p--; | |
0aeb64d0 | 6126 | while (UTF8_IS_CONTINUATION(*p)) { |
7e8c5dac | 6127 | p--; |
0aeb64d0 JH |
6128 | backw--; |
6129 | } | |
7e8c5dac HS |
6130 | ubackw++; |
6131 | } | |
6132 | ||
6133 | cache[0] -= ubackw; | |
0aeb64d0 | 6134 | *offsetp = cache[0]; |
a67d7df9 TS |
6135 | |
6136 | /* Drop the stale "length" cache */ | |
6137 | cache[2] = 0; | |
6138 | cache[3] = 0; | |
6139 | ||
0aeb64d0 | 6140 | return; |
7e8c5dac HS |
6141 | } |
6142 | } | |
6143 | } | |
e23c8137 | 6144 | ASSERT_UTF8_CACHE(cache); |
a0dbb045 | 6145 | } |
7e8c5dac HS |
6146 | |
6147 | while (s < send) { | |
6148 | STRLEN n = 1; | |
6149 | ||
6150 | /* Call utf8n_to_uvchr() to validate the sequence | |
6151 | * (unless a simple non-UTF character) */ | |
6152 | if (!UTF8_IS_INVARIANT(*s)) | |
6153 | utf8n_to_uvchr(s, UTF8SKIP(s), &n, 0); | |
6154 | if (n > 0) { | |
6155 | s += n; | |
6156 | len++; | |
6157 | } | |
6158 | else | |
6159 | break; | |
6160 | } | |
6161 | ||
6162 | if (!SvREADONLY(sv)) { | |
6163 | if (!mg) { | |
6164 | sv_magic(sv, 0, PERL_MAGIC_utf8, 0, 0); | |
6165 | mg = mg_find(sv, PERL_MAGIC_utf8); | |
6166 | } | |
6167 | assert(mg); | |
6168 | ||
6169 | if (!mg->mg_ptr) { | |
979acdb5 | 6170 | Newz(0, cache, PERL_MAGIC_UTF8_CACHESIZE * 2, STRLEN); |
7e8c5dac HS |
6171 | mg->mg_ptr = (char *) cache; |
6172 | } | |
6173 | assert(cache); | |
6174 | ||
6175 | cache[0] = len; | |
6176 | cache[1] = *offsetp; | |
a67d7df9 TS |
6177 | /* Drop the stale "length" cache */ |
6178 | cache[2] = 0; | |
6179 | cache[3] = 0; | |
7e8c5dac HS |
6180 | } |
6181 | ||
6182 | *offsetp = len; | |
a0ed51b3 | 6183 | } |
a0ed51b3 LW |
6184 | return; |
6185 | } | |
6186 | ||
954c1994 GS |
6187 | /* |
6188 | =for apidoc sv_eq | |
6189 | ||
6190 | Returns a boolean indicating whether the strings in the two SVs are | |
645c22ef DM |
6191 | identical. Is UTF-8 and 'use bytes' aware, handles get magic, and will |
6192 | coerce its args to strings if necessary. | |
954c1994 GS |
6193 | |
6194 | =cut | |
6195 | */ | |
6196 | ||
79072805 | 6197 | I32 |
e01b9e88 | 6198 | Perl_sv_eq(pTHX_ register SV *sv1, register SV *sv2) |
79072805 | 6199 | { |
e1ec3a88 | 6200 | const char *pv1; |
463ee0b2 | 6201 | STRLEN cur1; |
e1ec3a88 | 6202 | const char *pv2; |
463ee0b2 | 6203 | STRLEN cur2; |
e01b9e88 | 6204 | I32 eq = 0; |
553e1bcc AT |
6205 | char *tpv = Nullch; |
6206 | SV* svrecode = Nullsv; | |
79072805 | 6207 | |
e01b9e88 | 6208 | if (!sv1) { |
79072805 LW |
6209 | pv1 = ""; |
6210 | cur1 = 0; | |
6211 | } | |
463ee0b2 | 6212 | else |
4d84ee25 | 6213 | pv1 = SvPV_const(sv1, cur1); |
79072805 | 6214 | |
e01b9e88 SC |
6215 | if (!sv2){ |
6216 | pv2 = ""; | |
6217 | cur2 = 0; | |
92d29cee | 6218 | } |
e01b9e88 | 6219 | else |
4d84ee25 | 6220 | pv2 = SvPV_const(sv2, cur2); |
79072805 | 6221 | |
cf48d248 | 6222 | if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) { |
799ef3cb JH |
6223 | /* Differing utf8ness. |
6224 | * Do not UTF8size the comparands as a side-effect. */ | |
6225 | if (PL_encoding) { | |
6226 | if (SvUTF8(sv1)) { | |
553e1bcc AT |
6227 | svrecode = newSVpvn(pv2, cur2); |
6228 | sv_recode_to_utf8(svrecode, PL_encoding); | |
93524f2b | 6229 | pv2 = SvPV_const(svrecode, cur2); |
799ef3cb JH |
6230 | } |
6231 | else { | |
553e1bcc AT |
6232 | svrecode = newSVpvn(pv1, cur1); |
6233 | sv_recode_to_utf8(svrecode, PL_encoding); | |
93524f2b | 6234 | pv1 = SvPV_const(svrecode, cur1); |
799ef3cb JH |
6235 | } |
6236 | /* Now both are in UTF-8. */ | |
0a1bd7ac DM |
6237 | if (cur1 != cur2) { |
6238 | SvREFCNT_dec(svrecode); | |
799ef3cb | 6239 | return FALSE; |
0a1bd7ac | 6240 | } |
799ef3cb JH |
6241 | } |
6242 | else { | |
6243 | bool is_utf8 = TRUE; | |
6244 | ||
6245 | if (SvUTF8(sv1)) { | |
6246 | /* sv1 is the UTF-8 one, | |
6247 | * if is equal it must be downgrade-able */ | |
e1ec3a88 | 6248 | char *pv = (char*)bytes_from_utf8((const U8*)pv1, |
799ef3cb JH |
6249 | &cur1, &is_utf8); |
6250 | if (pv != pv1) | |
553e1bcc | 6251 | pv1 = tpv = pv; |
799ef3cb JH |
6252 | } |
6253 | else { | |
6254 | /* sv2 is the UTF-8 one, | |
6255 | * if is equal it must be downgrade-able */ | |
e1ec3a88 | 6256 | char *pv = (char *)bytes_from_utf8((const U8*)pv2, |
799ef3cb JH |
6257 | &cur2, &is_utf8); |
6258 | if (pv != pv2) | |
553e1bcc | 6259 | pv2 = tpv = pv; |
799ef3cb JH |
6260 | } |
6261 | if (is_utf8) { | |
6262 | /* Downgrade not possible - cannot be eq */ | |
bf694877 | 6263 | assert (tpv == 0); |
799ef3cb JH |
6264 | return FALSE; |
6265 | } | |
6266 | } | |
cf48d248 JH |
6267 | } |
6268 | ||
6269 | if (cur1 == cur2) | |
765f542d | 6270 | eq = (pv1 == pv2) || memEQ(pv1, pv2, cur1); |
e01b9e88 | 6271 | |
553e1bcc AT |
6272 | if (svrecode) |
6273 | SvREFCNT_dec(svrecode); | |
799ef3cb | 6274 | |
553e1bcc AT |
6275 | if (tpv) |
6276 | Safefree(tpv); | |
cf48d248 | 6277 | |
e01b9e88 | 6278 | return eq; |
79072805 LW |
6279 | } |
6280 | ||
954c1994 GS |
6281 | /* |
6282 | =for apidoc sv_cmp | |
6283 | ||
6284 | Compares the strings in two SVs. Returns -1, 0, or 1 indicating whether the | |
6285 | string in C<sv1> is less than, equal to, or greater than the string in | |
645c22ef DM |
6286 | C<sv2>. Is UTF-8 and 'use bytes' aware, handles get magic, and will |
6287 | coerce its args to strings if necessary. See also C<sv_cmp_locale>. | |
954c1994 GS |
6288 | |
6289 | =cut | |
6290 | */ | |
6291 | ||
79072805 | 6292 | I32 |
e01b9e88 | 6293 | Perl_sv_cmp(pTHX_ register SV *sv1, register SV *sv2) |
79072805 | 6294 | { |
560a288e | 6295 | STRLEN cur1, cur2; |
e1ec3a88 AL |
6296 | const char *pv1, *pv2; |
6297 | char *tpv = Nullch; | |
cf48d248 | 6298 | I32 cmp; |
553e1bcc | 6299 | SV *svrecode = Nullsv; |
560a288e | 6300 | |
e01b9e88 SC |
6301 | if (!sv1) { |
6302 | pv1 = ""; | |
560a288e GS |
6303 | cur1 = 0; |
6304 | } | |
e01b9e88 | 6305 | else |
4d84ee25 | 6306 | pv1 = SvPV_const(sv1, cur1); |
560a288e | 6307 | |
553e1bcc | 6308 | if (!sv2) { |
e01b9e88 | 6309 | pv2 = ""; |
560a288e GS |
6310 | cur2 = 0; |
6311 | } | |
e01b9e88 | 6312 | else |
4d84ee25 | 6313 | pv2 = SvPV_const(sv2, cur2); |
79072805 | 6314 | |
cf48d248 | 6315 | if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) { |
799ef3cb JH |
6316 | /* Differing utf8ness. |
6317 | * Do not UTF8size the comparands as a side-effect. */ | |
cf48d248 | 6318 | if (SvUTF8(sv1)) { |
799ef3cb | 6319 | if (PL_encoding) { |
553e1bcc AT |
6320 | svrecode = newSVpvn(pv2, cur2); |
6321 | sv_recode_to_utf8(svrecode, PL_encoding); | |
93524f2b | 6322 | pv2 = SvPV_const(svrecode, cur2); |
799ef3cb JH |
6323 | } |
6324 | else { | |
e1ec3a88 | 6325 | pv2 = tpv = (char*)bytes_to_utf8((const U8*)pv2, &cur2); |
799ef3cb | 6326 | } |
cf48d248 JH |
6327 | } |
6328 | else { | |
799ef3cb | 6329 | if (PL_encoding) { |
553e1bcc AT |
6330 | svrecode = newSVpvn(pv1, cur1); |
6331 | sv_recode_to_utf8(svrecode, PL_encoding); | |
93524f2b | 6332 | pv1 = SvPV_const(svrecode, cur1); |
799ef3cb JH |
6333 | } |
6334 | else { | |
e1ec3a88 | 6335 | pv1 = tpv = (char*)bytes_to_utf8((const U8*)pv1, &cur1); |
799ef3cb | 6336 | } |
cf48d248 JH |
6337 | } |
6338 | } | |
6339 | ||
e01b9e88 | 6340 | if (!cur1) { |
cf48d248 | 6341 | cmp = cur2 ? -1 : 0; |
e01b9e88 | 6342 | } else if (!cur2) { |
cf48d248 JH |
6343 | cmp = 1; |
6344 | } else { | |
e1ec3a88 | 6345 | const I32 retval = memcmp((const void*)pv1, (const void*)pv2, cur1 < cur2 ? cur1 : cur2); |
e01b9e88 SC |
6346 | |
6347 | if (retval) { | |
cf48d248 | 6348 | cmp = retval < 0 ? -1 : 1; |
e01b9e88 | 6349 | } else if (cur1 == cur2) { |
cf48d248 JH |
6350 | cmp = 0; |
6351 | } else { | |
6352 | cmp = cur1 < cur2 ? -1 : 1; | |
e01b9e88 | 6353 | } |
cf48d248 | 6354 | } |
16660edb | 6355 | |
553e1bcc AT |
6356 | if (svrecode) |
6357 | SvREFCNT_dec(svrecode); | |
799ef3cb | 6358 | |
553e1bcc AT |
6359 | if (tpv) |
6360 | Safefree(tpv); | |
cf48d248 JH |
6361 | |
6362 | return cmp; | |
bbce6d69 | 6363 | } |
16660edb | 6364 | |
c461cf8f JH |
6365 | /* |
6366 | =for apidoc sv_cmp_locale | |
6367 | ||
645c22ef DM |
6368 | Compares the strings in two SVs in a locale-aware manner. Is UTF-8 and |
6369 | 'use bytes' aware, handles get magic, and will coerce its args to strings | |
6370 | if necessary. See also C<sv_cmp_locale>. See also C<sv_cmp>. | |
c461cf8f JH |
6371 | |
6372 | =cut | |
6373 | */ | |
6374 | ||
bbce6d69 | 6375 | I32 |
864dbfa3 | 6376 | Perl_sv_cmp_locale(pTHX_ register SV *sv1, register SV *sv2) |
bbce6d69 | 6377 | { |
36477c24 | 6378 | #ifdef USE_LOCALE_COLLATE |
16660edb | 6379 | |
bbce6d69 | 6380 | char *pv1, *pv2; |
6381 | STRLEN len1, len2; | |
6382 | I32 retval; | |
16660edb | 6383 | |
3280af22 | 6384 | if (PL_collation_standard) |
bbce6d69 | 6385 | goto raw_compare; |
16660edb | 6386 | |
bbce6d69 | 6387 | len1 = 0; |
8ac85365 | 6388 | pv1 = sv1 ? sv_collxfrm(sv1, &len1) : (char *) NULL; |
bbce6d69 | 6389 | len2 = 0; |
8ac85365 | 6390 | pv2 = sv2 ? sv_collxfrm(sv2, &len2) : (char *) NULL; |
16660edb | 6391 | |
bbce6d69 | 6392 | if (!pv1 || !len1) { |
6393 | if (pv2 && len2) | |
6394 | return -1; | |
6395 | else | |
6396 | goto raw_compare; | |
6397 | } | |
6398 | else { | |
6399 | if (!pv2 || !len2) | |
6400 | return 1; | |
6401 | } | |
16660edb | 6402 | |
bbce6d69 | 6403 | retval = memcmp((void*)pv1, (void*)pv2, len1 < len2 ? len1 : len2); |
16660edb | 6404 | |
bbce6d69 | 6405 | if (retval) |
16660edb | 6406 | return retval < 0 ? -1 : 1; |
6407 | ||
bbce6d69 | 6408 | /* |
6409 | * When the result of collation is equality, that doesn't mean | |
6410 | * that there are no differences -- some locales exclude some | |
6411 | * characters from consideration. So to avoid false equalities, | |
6412 | * we use the raw string as a tiebreaker. | |
6413 | */ | |
16660edb | 6414 | |
bbce6d69 | 6415 | raw_compare: |
6416 | /* FALL THROUGH */ | |
16660edb | 6417 | |
36477c24 | 6418 | #endif /* USE_LOCALE_COLLATE */ |
16660edb | 6419 | |
bbce6d69 | 6420 | return sv_cmp(sv1, sv2); |
6421 | } | |
79072805 | 6422 | |
645c22ef | 6423 | |
36477c24 | 6424 | #ifdef USE_LOCALE_COLLATE |
645c22ef | 6425 | |
7a4c00b4 | 6426 | /* |
645c22ef DM |
6427 | =for apidoc sv_collxfrm |
6428 | ||
6429 | Add Collate Transform magic to an SV if it doesn't already have it. | |
6430 | ||
6431 | Any scalar variable may carry PERL_MAGIC_collxfrm magic that contains the | |
6432 | scalar data of the variable, but transformed to such a format that a normal | |
6433 | memory comparison can be used to compare the data according to the locale | |
6434 | settings. | |
6435 | ||
6436 | =cut | |
6437 | */ | |
6438 | ||
bbce6d69 | 6439 | char * |
864dbfa3 | 6440 | Perl_sv_collxfrm(pTHX_ SV *sv, STRLEN *nxp) |
bbce6d69 | 6441 | { |
7a4c00b4 | 6442 | MAGIC *mg; |
16660edb | 6443 | |
14befaf4 | 6444 | mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_collxfrm) : (MAGIC *) NULL; |
3280af22 | 6445 | if (!mg || !mg->mg_ptr || *(U32*)mg->mg_ptr != PL_collation_ix) { |
93524f2b NC |
6446 | const char *s; |
6447 | char *xf; | |
bbce6d69 | 6448 | STRLEN len, xlen; |
6449 | ||
7a4c00b4 | 6450 | if (mg) |
6451 | Safefree(mg->mg_ptr); | |
93524f2b | 6452 | s = SvPV_const(sv, len); |
bbce6d69 | 6453 | if ((xf = mem_collxfrm(s, len, &xlen))) { |
ff0cee69 | 6454 | if (SvREADONLY(sv)) { |
6455 | SAVEFREEPV(xf); | |
6456 | *nxp = xlen; | |
3280af22 | 6457 | return xf + sizeof(PL_collation_ix); |
ff0cee69 | 6458 | } |
7a4c00b4 | 6459 | if (! mg) { |
14befaf4 DM |
6460 | sv_magic(sv, 0, PERL_MAGIC_collxfrm, 0, 0); |
6461 | mg = mg_find(sv, PERL_MAGIC_collxfrm); | |
7a4c00b4 | 6462 | assert(mg); |
bbce6d69 | 6463 | } |
7a4c00b4 | 6464 | mg->mg_ptr = xf; |
565764a8 | 6465 | mg->mg_len = xlen; |
7a4c00b4 | 6466 | } |
6467 | else { | |
ff0cee69 | 6468 | if (mg) { |
6469 | mg->mg_ptr = NULL; | |
565764a8 | 6470 | mg->mg_len = -1; |
ff0cee69 | 6471 | } |
bbce6d69 | 6472 | } |
6473 | } | |
7a4c00b4 | 6474 | if (mg && mg->mg_ptr) { |
565764a8 | 6475 | *nxp = mg->mg_len; |
3280af22 | 6476 | return mg->mg_ptr + sizeof(PL_collation_ix); |
bbce6d69 | 6477 | } |
6478 | else { | |
6479 | *nxp = 0; | |
6480 | return NULL; | |
16660edb | 6481 | } |
79072805 LW |
6482 | } |
6483 | ||
36477c24 | 6484 | #endif /* USE_LOCALE_COLLATE */ |
bbce6d69 | 6485 | |
c461cf8f JH |
6486 | /* |
6487 | =for apidoc sv_gets | |
6488 | ||
6489 | Get a line from the filehandle and store it into the SV, optionally | |
6490 | appending to the currently-stored string. | |
6491 | ||
6492 | =cut | |
6493 | */ | |
6494 | ||
79072805 | 6495 | char * |
864dbfa3 | 6496 | Perl_sv_gets(pTHX_ register SV *sv, register PerlIO *fp, I32 append) |
79072805 | 6497 | { |
e1ec3a88 | 6498 | const char *rsptr; |
c07a80fd | 6499 | STRLEN rslen; |
6500 | register STDCHAR rslast; | |
6501 | register STDCHAR *bp; | |
6502 | register I32 cnt; | |
9c5ffd7c | 6503 | I32 i = 0; |
8bfdd7d9 | 6504 | I32 rspara = 0; |
e311fd51 | 6505 | I32 recsize; |
c07a80fd | 6506 | |
bc44a8a2 NC |
6507 | if (SvTHINKFIRST(sv)) |
6508 | sv_force_normal_flags(sv, append ? 0 : SV_COW_DROP_PV); | |
765f542d NC |
6509 | /* XXX. If you make this PVIV, then copy on write can copy scalars read |
6510 | from <>. | |
6511 | However, perlbench says it's slower, because the existing swipe code | |
6512 | is faster than copy on write. | |
6513 | Swings and roundabouts. */ | |
862a34c6 | 6514 | SvUPGRADE(sv, SVt_PV); |
99491443 | 6515 | |
ff68c719 | 6516 | SvSCREAM_off(sv); |
efd8b2ba AE |
6517 | |
6518 | if (append) { | |
6519 | if (PerlIO_isutf8(fp)) { | |
6520 | if (!SvUTF8(sv)) { | |
6521 | sv_utf8_upgrade_nomg(sv); | |
6522 | sv_pos_u2b(sv,&append,0); | |
6523 | } | |
6524 | } else if (SvUTF8(sv)) { | |
6525 | SV *tsv = NEWSV(0,0); | |
6526 | sv_gets(tsv, fp, 0); | |
6527 | sv_utf8_upgrade_nomg(tsv); | |
6528 | SvCUR_set(sv,append); | |
6529 | sv_catsv(sv,tsv); | |
6530 | sv_free(tsv); | |
6531 | goto return_string_or_null; | |
6532 | } | |
6533 | } | |
6534 | ||
6535 | SvPOK_only(sv); | |
6536 | if (PerlIO_isutf8(fp)) | |
6537 | SvUTF8_on(sv); | |
c07a80fd | 6538 | |
923e4eb5 | 6539 | if (IN_PERL_COMPILETIME) { |
8bfdd7d9 HS |
6540 | /* we always read code in line mode */ |
6541 | rsptr = "\n"; | |
6542 | rslen = 1; | |
6543 | } | |
6544 | else if (RsSNARF(PL_rs)) { | |
7a5fa8a2 NIS |
6545 | /* If it is a regular disk file use size from stat() as estimate |
6546 | of amount we are going to read - may result in malloc-ing | |
6547 | more memory than we realy need if layers bellow reduce | |
e468d35b NIS |
6548 | size we read (e.g. CRLF or a gzip layer) |
6549 | */ | |
e311fd51 | 6550 | Stat_t st; |
e468d35b | 6551 | if (!PerlLIO_fstat(PerlIO_fileno(fp), &st) && S_ISREG(st.st_mode)) { |
f54cb97a | 6552 | const Off_t offset = PerlIO_tell(fp); |
58f1856e | 6553 | if (offset != (Off_t) -1 && st.st_size + append > offset) { |
e468d35b NIS |
6554 | (void) SvGROW(sv, (STRLEN)((st.st_size - offset) + append + 1)); |
6555 | } | |
6556 | } | |
c07a80fd | 6557 | rsptr = NULL; |
6558 | rslen = 0; | |
6559 | } | |
3280af22 | 6560 | else if (RsRECORD(PL_rs)) { |
e311fd51 | 6561 | I32 bytesread; |
5b2b9c68 HM |
6562 | char *buffer; |
6563 | ||
6564 | /* Grab the size of the record we're getting */ | |
3280af22 | 6565 | recsize = SvIV(SvRV(PL_rs)); |
e311fd51 | 6566 | buffer = SvGROW(sv, (STRLEN)(recsize + append + 1)) + append; |
5b2b9c68 HM |
6567 | /* Go yank in */ |
6568 | #ifdef VMS | |
6569 | /* VMS wants read instead of fread, because fread doesn't respect */ | |
6570 | /* RMS record boundaries. This is not necessarily a good thing to be */ | |
e468d35b NIS |
6571 | /* doing, but we've got no other real choice - except avoid stdio |
6572 | as implementation - perhaps write a :vms layer ? | |
6573 | */ | |
5b2b9c68 HM |
6574 | bytesread = PerlLIO_read(PerlIO_fileno(fp), buffer, recsize); |
6575 | #else | |
6576 | bytesread = PerlIO_read(fp, buffer, recsize); | |
6577 | #endif | |
27e6ca2d AE |
6578 | if (bytesread < 0) |
6579 | bytesread = 0; | |
e311fd51 | 6580 | SvCUR_set(sv, bytesread += append); |
e670df4e | 6581 | buffer[bytesread] = '\0'; |
efd8b2ba | 6582 | goto return_string_or_null; |
5b2b9c68 | 6583 | } |
3280af22 | 6584 | else if (RsPARA(PL_rs)) { |
c07a80fd | 6585 | rsptr = "\n\n"; |
6586 | rslen = 2; | |
8bfdd7d9 | 6587 | rspara = 1; |
c07a80fd | 6588 | } |
7d59b7e4 NIS |
6589 | else { |
6590 | /* Get $/ i.e. PL_rs into same encoding as stream wants */ | |
6591 | if (PerlIO_isutf8(fp)) { | |
6592 | rsptr = SvPVutf8(PL_rs, rslen); | |
6593 | } | |
6594 | else { | |
6595 | if (SvUTF8(PL_rs)) { | |
6596 | if (!sv_utf8_downgrade(PL_rs, TRUE)) { | |
6597 | Perl_croak(aTHX_ "Wide character in $/"); | |
6598 | } | |
6599 | } | |
93524f2b | 6600 | rsptr = SvPV_const(PL_rs, rslen); |
7d59b7e4 NIS |
6601 | } |
6602 | } | |
6603 | ||
c07a80fd | 6604 | rslast = rslen ? rsptr[rslen - 1] : '\0'; |
6605 | ||
8bfdd7d9 | 6606 | if (rspara) { /* have to do this both before and after */ |
79072805 | 6607 | do { /* to make sure file boundaries work right */ |
760ac839 | 6608 | if (PerlIO_eof(fp)) |
a0d0e21e | 6609 | return 0; |
760ac839 | 6610 | i = PerlIO_getc(fp); |
79072805 | 6611 | if (i != '\n') { |
a0d0e21e LW |
6612 | if (i == -1) |
6613 | return 0; | |
760ac839 | 6614 | PerlIO_ungetc(fp,i); |
79072805 LW |
6615 | break; |
6616 | } | |
6617 | } while (i != EOF); | |
6618 | } | |
c07a80fd | 6619 | |
760ac839 LW |
6620 | /* See if we know enough about I/O mechanism to cheat it ! */ |
6621 | ||
6622 | /* This used to be #ifdef test - it is made run-time test for ease | |
1c846c1f | 6623 | of abstracting out stdio interface. One call should be cheap |
760ac839 LW |
6624 | enough here - and may even be a macro allowing compile |
6625 | time optimization. | |
6626 | */ | |
6627 | ||
6628 | if (PerlIO_fast_gets(fp)) { | |
6629 | ||
6630 | /* | |
6631 | * We're going to steal some values from the stdio struct | |
6632 | * and put EVERYTHING in the innermost loop into registers. | |
6633 | */ | |
6634 | register STDCHAR *ptr; | |
6635 | STRLEN bpx; | |
6636 | I32 shortbuffered; | |
6637 | ||
16660edb | 6638 | #if defined(VMS) && defined(PERLIO_IS_STDIO) |
6639 | /* An ungetc()d char is handled separately from the regular | |
6640 | * buffer, so we getc() it back out and stuff it in the buffer. | |
6641 | */ | |
6642 | i = PerlIO_getc(fp); | |
6643 | if (i == EOF) return 0; | |
6644 | *(--((*fp)->_ptr)) = (unsigned char) i; | |
6645 | (*fp)->_cnt++; | |
6646 | #endif | |
c07a80fd | 6647 | |
c2960299 | 6648 | /* Here is some breathtakingly efficient cheating */ |
c07a80fd | 6649 | |
a20bf0c3 | 6650 | cnt = PerlIO_get_cnt(fp); /* get count into register */ |
e468d35b | 6651 | /* make sure we have the room */ |
7a5fa8a2 | 6652 | if ((I32)(SvLEN(sv) - append) <= cnt + 1) { |
e468d35b | 6653 | /* Not room for all of it |
7a5fa8a2 | 6654 | if we are looking for a separator and room for some |
e468d35b NIS |
6655 | */ |
6656 | if (rslen && cnt > 80 && (I32)SvLEN(sv) > append) { | |
7a5fa8a2 | 6657 | /* just process what we have room for */ |
79072805 LW |
6658 | shortbuffered = cnt - SvLEN(sv) + append + 1; |
6659 | cnt -= shortbuffered; | |
6660 | } | |
6661 | else { | |
6662 | shortbuffered = 0; | |
bbce6d69 | 6663 | /* remember that cnt can be negative */ |
eb160463 | 6664 | SvGROW(sv, (STRLEN)(append + (cnt <= 0 ? 2 : (cnt + 1)))); |
79072805 LW |
6665 | } |
6666 | } | |
7a5fa8a2 | 6667 | else |
79072805 | 6668 | shortbuffered = 0; |
3f7c398e | 6669 | bp = (STDCHAR*)SvPVX_const(sv) + append; /* move these two too to registers */ |
a20bf0c3 | 6670 | ptr = (STDCHAR*)PerlIO_get_ptr(fp); |
16660edb | 6671 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 6672 | "Screamer: entering, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt)); |
16660edb | 6673 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
ba7abf9d | 6674 | "Screamer: entering: PerlIO * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n", |
1c846c1f | 6675 | PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp), |
1d7c1841 | 6676 | PTR2UV(PerlIO_has_base(fp) ? PerlIO_get_base(fp) : 0))); |
79072805 LW |
6677 | for (;;) { |
6678 | screamer: | |
93a17b20 | 6679 | if (cnt > 0) { |
c07a80fd | 6680 | if (rslen) { |
760ac839 LW |
6681 | while (cnt > 0) { /* this | eat */ |
6682 | cnt--; | |
c07a80fd | 6683 | if ((*bp++ = *ptr++) == rslast) /* really | dust */ |
6684 | goto thats_all_folks; /* screams | sed :-) */ | |
6685 | } | |
6686 | } | |
6687 | else { | |
1c846c1f NIS |
6688 | Copy(ptr, bp, cnt, char); /* this | eat */ |
6689 | bp += cnt; /* screams | dust */ | |
c07a80fd | 6690 | ptr += cnt; /* louder | sed :-) */ |
a5f75d66 | 6691 | cnt = 0; |
93a17b20 | 6692 | } |
79072805 LW |
6693 | } |
6694 | ||
748a9306 | 6695 | if (shortbuffered) { /* oh well, must extend */ |
79072805 LW |
6696 | cnt = shortbuffered; |
6697 | shortbuffered = 0; | |
3f7c398e | 6698 | bpx = bp - (STDCHAR*)SvPVX_const(sv); /* box up before relocation */ |
79072805 LW |
6699 | SvCUR_set(sv, bpx); |
6700 | SvGROW(sv, SvLEN(sv) + append + cnt + 2); | |
3f7c398e | 6701 | bp = (STDCHAR*)SvPVX_const(sv) + bpx; /* unbox after relocation */ |
79072805 LW |
6702 | continue; |
6703 | } | |
6704 | ||
16660edb | 6705 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 GS |
6706 | "Screamer: going to getc, ptr=%"UVuf", cnt=%ld\n", |
6707 | PTR2UV(ptr),(long)cnt)); | |
cc00df79 | 6708 | PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* deregisterize cnt and ptr */ |
ba7abf9d | 6709 | #if 0 |
16660edb | 6710 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 6711 | "Screamer: pre: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n", |
1c846c1f | 6712 | PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp), |
1d7c1841 | 6713 | PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0))); |
ba7abf9d | 6714 | #endif |
1c846c1f | 6715 | /* This used to call 'filbuf' in stdio form, but as that behaves like |
774d564b | 6716 | getc when cnt <= 0 we use PerlIO_getc here to avoid introducing |
6717 | another abstraction. */ | |
760ac839 | 6718 | i = PerlIO_getc(fp); /* get more characters */ |
ba7abf9d | 6719 | #if 0 |
16660edb | 6720 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 6721 | "Screamer: post: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n", |
1c846c1f | 6722 | PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp), |
1d7c1841 | 6723 | PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0))); |
ba7abf9d | 6724 | #endif |
a20bf0c3 JH |
6725 | cnt = PerlIO_get_cnt(fp); |
6726 | ptr = (STDCHAR*)PerlIO_get_ptr(fp); /* reregisterize cnt and ptr */ | |
16660edb | 6727 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 6728 | "Screamer: after getc, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt)); |
79072805 | 6729 | |
748a9306 LW |
6730 | if (i == EOF) /* all done for ever? */ |
6731 | goto thats_really_all_folks; | |
6732 | ||
3f7c398e | 6733 | bpx = bp - (STDCHAR*)SvPVX_const(sv); /* box up before relocation */ |
79072805 LW |
6734 | SvCUR_set(sv, bpx); |
6735 | SvGROW(sv, bpx + cnt + 2); | |
3f7c398e | 6736 | bp = (STDCHAR*)SvPVX_const(sv) + bpx; /* unbox after relocation */ |
c07a80fd | 6737 | |
eb160463 | 6738 | *bp++ = (STDCHAR)i; /* store character from PerlIO_getc */ |
79072805 | 6739 | |
c07a80fd | 6740 | if (rslen && (STDCHAR)i == rslast) /* all done for now? */ |
79072805 | 6741 | goto thats_all_folks; |
79072805 LW |
6742 | } |
6743 | ||
6744 | thats_all_folks: | |
3f7c398e | 6745 | if ((rslen > 1 && (STRLEN)(bp - (STDCHAR*)SvPVX_const(sv)) < rslen) || |
36477c24 | 6746 | memNE((char*)bp - rslen, rsptr, rslen)) |
760ac839 | 6747 | goto screamer; /* go back to the fray */ |
79072805 LW |
6748 | thats_really_all_folks: |
6749 | if (shortbuffered) | |
6750 | cnt += shortbuffered; | |
16660edb | 6751 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 6752 | "Screamer: quitting, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt)); |
cc00df79 | 6753 | PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* put these back or we're in trouble */ |
16660edb | 6754 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 6755 | "Screamer: end: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n", |
1c846c1f | 6756 | PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp), |
1d7c1841 | 6757 | PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0))); |
79072805 | 6758 | *bp = '\0'; |
3f7c398e | 6759 | SvCUR_set(sv, bp - (STDCHAR*)SvPVX_const(sv)); /* set length */ |
16660edb | 6760 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
fb73857a | 6761 | "Screamer: done, len=%ld, string=|%.*s|\n", |
3f7c398e | 6762 | (long)SvCUR(sv),(int)SvCUR(sv),SvPVX_const(sv))); |
760ac839 LW |
6763 | } |
6764 | else | |
79072805 | 6765 | { |
6edd2cd5 | 6766 | /*The big, slow, and stupid way. */ |
27da23d5 | 6767 | #ifdef USE_HEAP_INSTEAD_OF_STACK /* Even slower way. */ |
6edd2cd5 JH |
6768 | STDCHAR *buf = 0; |
6769 | New(0, buf, 8192, STDCHAR); | |
6770 | assert(buf); | |
4d2c4e07 | 6771 | #else |
6edd2cd5 | 6772 | STDCHAR buf[8192]; |
4d2c4e07 | 6773 | #endif |
79072805 | 6774 | |
760ac839 | 6775 | screamer2: |
c07a80fd | 6776 | if (rslen) { |
6867be6d | 6777 | const register STDCHAR *bpe = buf + sizeof(buf); |
760ac839 | 6778 | bp = buf; |
eb160463 | 6779 | while ((i = PerlIO_getc(fp)) != EOF && (*bp++ = (STDCHAR)i) != rslast && bp < bpe) |
760ac839 LW |
6780 | ; /* keep reading */ |
6781 | cnt = bp - buf; | |
c07a80fd | 6782 | } |
6783 | else { | |
760ac839 | 6784 | cnt = PerlIO_read(fp,(char*)buf, sizeof(buf)); |
16660edb | 6785 | /* Accomodate broken VAXC compiler, which applies U8 cast to |
6786 | * both args of ?: operator, causing EOF to change into 255 | |
6787 | */ | |
37be0adf | 6788 | if (cnt > 0) |
cbe9e203 JH |
6789 | i = (U8)buf[cnt - 1]; |
6790 | else | |
37be0adf | 6791 | i = EOF; |
c07a80fd | 6792 | } |
79072805 | 6793 | |
cbe9e203 JH |
6794 | if (cnt < 0) |
6795 | cnt = 0; /* we do need to re-set the sv even when cnt <= 0 */ | |
6796 | if (append) | |
6797 | sv_catpvn(sv, (char *) buf, cnt); | |
6798 | else | |
6799 | sv_setpvn(sv, (char *) buf, cnt); | |
c07a80fd | 6800 | |
6801 | if (i != EOF && /* joy */ | |
6802 | (!rslen || | |
6803 | SvCUR(sv) < rslen || | |
3f7c398e | 6804 | memNE(SvPVX_const(sv) + SvCUR(sv) - rslen, rsptr, rslen))) |
79072805 LW |
6805 | { |
6806 | append = -1; | |
63e4d877 CS |
6807 | /* |
6808 | * If we're reading from a TTY and we get a short read, | |
6809 | * indicating that the user hit his EOF character, we need | |
6810 | * to notice it now, because if we try to read from the TTY | |
6811 | * again, the EOF condition will disappear. | |
6812 | * | |
6813 | * The comparison of cnt to sizeof(buf) is an optimization | |
6814 | * that prevents unnecessary calls to feof(). | |
6815 | * | |
6816 | * - jik 9/25/96 | |
6817 | */ | |
6818 | if (!(cnt < sizeof(buf) && PerlIO_eof(fp))) | |
6819 | goto screamer2; | |
79072805 | 6820 | } |
6edd2cd5 | 6821 | |
27da23d5 | 6822 | #ifdef USE_HEAP_INSTEAD_OF_STACK |
6edd2cd5 JH |
6823 | Safefree(buf); |
6824 | #endif | |
79072805 LW |
6825 | } |
6826 | ||
8bfdd7d9 | 6827 | if (rspara) { /* have to do this both before and after */ |
c07a80fd | 6828 | while (i != EOF) { /* to make sure file boundaries work right */ |
760ac839 | 6829 | i = PerlIO_getc(fp); |
79072805 | 6830 | if (i != '\n') { |
760ac839 | 6831 | PerlIO_ungetc(fp,i); |
79072805 LW |
6832 | break; |
6833 | } | |
6834 | } | |
6835 | } | |
c07a80fd | 6836 | |
efd8b2ba | 6837 | return_string_or_null: |
c07a80fd | 6838 | return (SvCUR(sv) - append) ? SvPVX(sv) : Nullch; |
79072805 LW |
6839 | } |
6840 | ||
954c1994 GS |
6841 | /* |
6842 | =for apidoc sv_inc | |
6843 | ||
645c22ef DM |
6844 | Auto-increment of the value in the SV, doing string to numeric conversion |
6845 | if necessary. Handles 'get' magic. | |
954c1994 GS |
6846 | |
6847 | =cut | |
6848 | */ | |
6849 | ||
79072805 | 6850 | void |
864dbfa3 | 6851 | Perl_sv_inc(pTHX_ register SV *sv) |
79072805 LW |
6852 | { |
6853 | register char *d; | |
463ee0b2 | 6854 | int flags; |
79072805 LW |
6855 | |
6856 | if (!sv) | |
6857 | return; | |
b23a5f78 GB |
6858 | if (SvGMAGICAL(sv)) |
6859 | mg_get(sv); | |
ed6116ce | 6860 | if (SvTHINKFIRST(sv)) { |
765f542d NC |
6861 | if (SvIsCOW(sv)) |
6862 | sv_force_normal_flags(sv, 0); | |
0f15f207 | 6863 | if (SvREADONLY(sv)) { |
923e4eb5 | 6864 | if (IN_PERL_RUNTIME) |
cea2e8a9 | 6865 | Perl_croak(aTHX_ PL_no_modify); |
0f15f207 | 6866 | } |
a0d0e21e | 6867 | if (SvROK(sv)) { |
b5be31e9 | 6868 | IV i; |
9e7bc3e8 JD |
6869 | if (SvAMAGIC(sv) && AMG_CALLun(sv,inc)) |
6870 | return; | |
56431972 | 6871 | i = PTR2IV(SvRV(sv)); |
b5be31e9 SM |
6872 | sv_unref(sv); |
6873 | sv_setiv(sv, i); | |
a0d0e21e | 6874 | } |
ed6116ce | 6875 | } |
8990e307 | 6876 | flags = SvFLAGS(sv); |
28e5dec8 JH |
6877 | if ((flags & (SVp_NOK|SVp_IOK)) == SVp_NOK) { |
6878 | /* It's (privately or publicly) a float, but not tested as an | |
6879 | integer, so test it to see. */ | |
d460ef45 | 6880 | (void) SvIV(sv); |
28e5dec8 JH |
6881 | flags = SvFLAGS(sv); |
6882 | } | |
6883 | if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) { | |
6884 | /* It's publicly an integer, or privately an integer-not-float */ | |
59d8ce62 | 6885 | #ifdef PERL_PRESERVE_IVUV |
28e5dec8 | 6886 | oops_its_int: |
59d8ce62 | 6887 | #endif |
25da4f38 IZ |
6888 | if (SvIsUV(sv)) { |
6889 | if (SvUVX(sv) == UV_MAX) | |
a1e868e7 | 6890 | sv_setnv(sv, UV_MAX_P1); |
25da4f38 IZ |
6891 | else |
6892 | (void)SvIOK_only_UV(sv); | |
607fa7f2 | 6893 | SvUV_set(sv, SvUVX(sv) + 1); |
25da4f38 IZ |
6894 | } else { |
6895 | if (SvIVX(sv) == IV_MAX) | |
28e5dec8 | 6896 | sv_setuv(sv, (UV)IV_MAX + 1); |
25da4f38 IZ |
6897 | else { |
6898 | (void)SvIOK_only(sv); | |
45977657 | 6899 | SvIV_set(sv, SvIVX(sv) + 1); |
1c846c1f | 6900 | } |
55497cff | 6901 | } |
79072805 LW |
6902 | return; |
6903 | } | |
28e5dec8 JH |
6904 | if (flags & SVp_NOK) { |
6905 | (void)SvNOK_only(sv); | |
9d6ce603 | 6906 | SvNV_set(sv, SvNVX(sv) + 1.0); |
28e5dec8 JH |
6907 | return; |
6908 | } | |
6909 | ||
3f7c398e | 6910 | if (!(flags & SVp_POK) || !*SvPVX_const(sv)) { |
28e5dec8 | 6911 | if ((flags & SVTYPEMASK) < SVt_PVIV) |
f5282e15 | 6912 | sv_upgrade(sv, ((flags & SVTYPEMASK) > SVt_IV ? SVt_PVIV : SVt_IV)); |
28e5dec8 | 6913 | (void)SvIOK_only(sv); |
45977657 | 6914 | SvIV_set(sv, 1); |
79072805 LW |
6915 | return; |
6916 | } | |
463ee0b2 | 6917 | d = SvPVX(sv); |
79072805 LW |
6918 | while (isALPHA(*d)) d++; |
6919 | while (isDIGIT(*d)) d++; | |
6920 | if (*d) { | |
28e5dec8 | 6921 | #ifdef PERL_PRESERVE_IVUV |
d1be9408 | 6922 | /* Got to punt this as an integer if needs be, but we don't issue |
28e5dec8 JH |
6923 | warnings. Probably ought to make the sv_iv_please() that does |
6924 | the conversion if possible, and silently. */ | |
504618e9 | 6925 | const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), NULL); |
28e5dec8 JH |
6926 | if (numtype && !(numtype & IS_NUMBER_INFINITY)) { |
6927 | /* Need to try really hard to see if it's an integer. | |
6928 | 9.22337203685478e+18 is an integer. | |
6929 | but "9.22337203685478e+18" + 0 is UV=9223372036854779904 | |
6930 | so $a="9.22337203685478e+18"; $a+0; $a++ | |
6931 | needs to be the same as $a="9.22337203685478e+18"; $a++ | |
6932 | or we go insane. */ | |
d460ef45 | 6933 | |
28e5dec8 JH |
6934 | (void) sv_2iv(sv); |
6935 | if (SvIOK(sv)) | |
6936 | goto oops_its_int; | |
6937 | ||
6938 | /* sv_2iv *should* have made this an NV */ | |
6939 | if (flags & SVp_NOK) { | |
6940 | (void)SvNOK_only(sv); | |
9d6ce603 | 6941 | SvNV_set(sv, SvNVX(sv) + 1.0); |
28e5dec8 JH |
6942 | return; |
6943 | } | |
6944 | /* I don't think we can get here. Maybe I should assert this | |
6945 | And if we do get here I suspect that sv_setnv will croak. NWC | |
6946 | Fall through. */ | |
6947 | #if defined(USE_LONG_DOUBLE) | |
6948 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"PERL_PRIgldbl"\n", | |
3f7c398e | 6949 | SvPVX_const(sv), SvIVX(sv), SvNVX(sv))); |
28e5dec8 | 6950 | #else |
1779d84d | 6951 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"NVgf"\n", |
3f7c398e | 6952 | SvPVX_const(sv), SvIVX(sv), SvNVX(sv))); |
28e5dec8 JH |
6953 | #endif |
6954 | } | |
6955 | #endif /* PERL_PRESERVE_IVUV */ | |
3f7c398e | 6956 | sv_setnv(sv,Atof(SvPVX_const(sv)) + 1.0); |
79072805 LW |
6957 | return; |
6958 | } | |
6959 | d--; | |
3f7c398e | 6960 | while (d >= SvPVX_const(sv)) { |
79072805 LW |
6961 | if (isDIGIT(*d)) { |
6962 | if (++*d <= '9') | |
6963 | return; | |
6964 | *(d--) = '0'; | |
6965 | } | |
6966 | else { | |
9d116dd7 JH |
6967 | #ifdef EBCDIC |
6968 | /* MKS: The original code here died if letters weren't consecutive. | |
6969 | * at least it didn't have to worry about non-C locales. The | |
6970 | * new code assumes that ('z'-'a')==('Z'-'A'), letters are | |
1c846c1f | 6971 | * arranged in order (although not consecutively) and that only |
9d116dd7 JH |
6972 | * [A-Za-z] are accepted by isALPHA in the C locale. |
6973 | */ | |
6974 | if (*d != 'z' && *d != 'Z') { | |
6975 | do { ++*d; } while (!isALPHA(*d)); | |
6976 | return; | |
6977 | } | |
6978 | *(d--) -= 'z' - 'a'; | |
6979 | #else | |
79072805 LW |
6980 | ++*d; |
6981 | if (isALPHA(*d)) | |
6982 | return; | |
6983 | *(d--) -= 'z' - 'a' + 1; | |
9d116dd7 | 6984 | #endif |
79072805 LW |
6985 | } |
6986 | } | |
6987 | /* oh,oh, the number grew */ | |
6988 | SvGROW(sv, SvCUR(sv) + 2); | |
b162af07 | 6989 | SvCUR_set(sv, SvCUR(sv) + 1); |
3f7c398e | 6990 | for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX_const(sv); d--) |
79072805 LW |
6991 | *d = d[-1]; |
6992 | if (isDIGIT(d[1])) | |
6993 | *d = '1'; | |
6994 | else | |
6995 | *d = d[1]; | |
6996 | } | |
6997 | ||
954c1994 GS |
6998 | /* |
6999 | =for apidoc sv_dec | |
7000 | ||
645c22ef DM |
7001 | Auto-decrement of the value in the SV, doing string to numeric conversion |
7002 | if necessary. Handles 'get' magic. | |
954c1994 GS |
7003 | |
7004 | =cut | |
7005 | */ | |
7006 | ||
79072805 | 7007 | void |
864dbfa3 | 7008 | Perl_sv_dec(pTHX_ register SV *sv) |
79072805 | 7009 | { |
463ee0b2 LW |
7010 | int flags; |
7011 | ||
79072805 LW |
7012 | if (!sv) |
7013 | return; | |
b23a5f78 GB |
7014 | if (SvGMAGICAL(sv)) |
7015 | mg_get(sv); | |
ed6116ce | 7016 | if (SvTHINKFIRST(sv)) { |
765f542d NC |
7017 | if (SvIsCOW(sv)) |
7018 | sv_force_normal_flags(sv, 0); | |
0f15f207 | 7019 | if (SvREADONLY(sv)) { |
923e4eb5 | 7020 | if (IN_PERL_RUNTIME) |
cea2e8a9 | 7021 | Perl_croak(aTHX_ PL_no_modify); |
0f15f207 | 7022 | } |
a0d0e21e | 7023 | if (SvROK(sv)) { |
b5be31e9 | 7024 | IV i; |
9e7bc3e8 JD |
7025 | if (SvAMAGIC(sv) && AMG_CALLun(sv,dec)) |
7026 | return; | |
56431972 | 7027 | i = PTR2IV(SvRV(sv)); |
b5be31e9 SM |
7028 | sv_unref(sv); |
7029 | sv_setiv(sv, i); | |
a0d0e21e | 7030 | } |
ed6116ce | 7031 | } |
28e5dec8 JH |
7032 | /* Unlike sv_inc we don't have to worry about string-never-numbers |
7033 | and keeping them magic. But we mustn't warn on punting */ | |
8990e307 | 7034 | flags = SvFLAGS(sv); |
28e5dec8 JH |
7035 | if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) { |
7036 | /* It's publicly an integer, or privately an integer-not-float */ | |
59d8ce62 | 7037 | #ifdef PERL_PRESERVE_IVUV |
28e5dec8 | 7038 | oops_its_int: |
59d8ce62 | 7039 | #endif |
25da4f38 IZ |
7040 | if (SvIsUV(sv)) { |
7041 | if (SvUVX(sv) == 0) { | |
7042 | (void)SvIOK_only(sv); | |
45977657 | 7043 | SvIV_set(sv, -1); |
25da4f38 IZ |
7044 | } |
7045 | else { | |
7046 | (void)SvIOK_only_UV(sv); | |
607fa7f2 | 7047 | SvUV_set(sv, SvUVX(sv) + 1); |
1c846c1f | 7048 | } |
25da4f38 IZ |
7049 | } else { |
7050 | if (SvIVX(sv) == IV_MIN) | |
65202027 | 7051 | sv_setnv(sv, (NV)IV_MIN - 1.0); |
25da4f38 IZ |
7052 | else { |
7053 | (void)SvIOK_only(sv); | |
45977657 | 7054 | SvIV_set(sv, SvIVX(sv) - 1); |
1c846c1f | 7055 | } |
55497cff | 7056 | } |
7057 | return; | |
7058 | } | |
28e5dec8 | 7059 | if (flags & SVp_NOK) { |
9d6ce603 | 7060 | SvNV_set(sv, SvNVX(sv) - 1.0); |
28e5dec8 JH |
7061 | (void)SvNOK_only(sv); |
7062 | return; | |
7063 | } | |
8990e307 | 7064 | if (!(flags & SVp_POK)) { |
4633a7c4 LW |
7065 | if ((flags & SVTYPEMASK) < SVt_PVNV) |
7066 | sv_upgrade(sv, SVt_NV); | |
f599b64b | 7067 | SvNV_set(sv, 1.0); |
a0d0e21e | 7068 | (void)SvNOK_only(sv); |
79072805 LW |
7069 | return; |
7070 | } | |
28e5dec8 JH |
7071 | #ifdef PERL_PRESERVE_IVUV |
7072 | { | |
504618e9 | 7073 | const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), NULL); |
28e5dec8 JH |
7074 | if (numtype && !(numtype & IS_NUMBER_INFINITY)) { |
7075 | /* Need to try really hard to see if it's an integer. | |
7076 | 9.22337203685478e+18 is an integer. | |
7077 | but "9.22337203685478e+18" + 0 is UV=9223372036854779904 | |
7078 | so $a="9.22337203685478e+18"; $a+0; $a-- | |
7079 | needs to be the same as $a="9.22337203685478e+18"; $a-- | |
7080 | or we go insane. */ | |
d460ef45 | 7081 | |
28e5dec8 JH |
7082 | (void) sv_2iv(sv); |
7083 | if (SvIOK(sv)) | |
7084 | goto oops_its_int; | |
7085 | ||
7086 | /* sv_2iv *should* have made this an NV */ | |
7087 | if (flags & SVp_NOK) { | |
7088 | (void)SvNOK_only(sv); | |
9d6ce603 | 7089 | SvNV_set(sv, SvNVX(sv) - 1.0); |
28e5dec8 JH |
7090 | return; |
7091 | } | |
7092 | /* I don't think we can get here. Maybe I should assert this | |
7093 | And if we do get here I suspect that sv_setnv will croak. NWC | |
7094 | Fall through. */ | |
7095 | #if defined(USE_LONG_DOUBLE) | |
7096 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"PERL_PRIgldbl"\n", | |
3f7c398e | 7097 | SvPVX_const(sv), SvIVX(sv), SvNVX(sv))); |
28e5dec8 | 7098 | #else |
1779d84d | 7099 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"NVgf"\n", |
3f7c398e | 7100 | SvPVX_const(sv), SvIVX(sv), SvNVX(sv))); |
28e5dec8 JH |
7101 | #endif |
7102 | } | |
7103 | } | |
7104 | #endif /* PERL_PRESERVE_IVUV */ | |
3f7c398e | 7105 | sv_setnv(sv,Atof(SvPVX_const(sv)) - 1.0); /* punt */ |
79072805 LW |
7106 | } |
7107 | ||
954c1994 GS |
7108 | /* |
7109 | =for apidoc sv_mortalcopy | |
7110 | ||
645c22ef | 7111 | Creates a new SV which is a copy of the original SV (using C<sv_setsv>). |
d4236ebc DM |
7112 | The new SV is marked as mortal. It will be destroyed "soon", either by an |
7113 | explicit call to FREETMPS, or by an implicit call at places such as | |
7114 | statement boundaries. See also C<sv_newmortal> and C<sv_2mortal>. | |
954c1994 GS |
7115 | |
7116 | =cut | |
7117 | */ | |
7118 | ||
79072805 LW |
7119 | /* Make a string that will exist for the duration of the expression |
7120 | * evaluation. Actually, it may have to last longer than that, but | |
7121 | * hopefully we won't free it until it has been assigned to a | |
7122 | * permanent location. */ | |
7123 | ||
7124 | SV * | |
864dbfa3 | 7125 | Perl_sv_mortalcopy(pTHX_ SV *oldstr) |
79072805 | 7126 | { |
463ee0b2 | 7127 | register SV *sv; |
b881518d | 7128 | |
4561caa4 | 7129 | new_SV(sv); |
79072805 | 7130 | sv_setsv(sv,oldstr); |
677b06e3 GS |
7131 | EXTEND_MORTAL(1); |
7132 | PL_tmps_stack[++PL_tmps_ix] = sv; | |
8990e307 LW |
7133 | SvTEMP_on(sv); |
7134 | return sv; | |
7135 | } | |
7136 | ||
954c1994 GS |
7137 | /* |
7138 | =for apidoc sv_newmortal | |
7139 | ||
645c22ef | 7140 | Creates a new null SV which is mortal. The reference count of the SV is |
d4236ebc DM |
7141 | set to 1. It will be destroyed "soon", either by an explicit call to |
7142 | FREETMPS, or by an implicit call at places such as statement boundaries. | |
7143 | See also C<sv_mortalcopy> and C<sv_2mortal>. | |
954c1994 GS |
7144 | |
7145 | =cut | |
7146 | */ | |
7147 | ||
8990e307 | 7148 | SV * |
864dbfa3 | 7149 | Perl_sv_newmortal(pTHX) |
8990e307 LW |
7150 | { |
7151 | register SV *sv; | |
7152 | ||
4561caa4 | 7153 | new_SV(sv); |
8990e307 | 7154 | SvFLAGS(sv) = SVs_TEMP; |
677b06e3 GS |
7155 | EXTEND_MORTAL(1); |
7156 | PL_tmps_stack[++PL_tmps_ix] = sv; | |
79072805 LW |
7157 | return sv; |
7158 | } | |
7159 | ||
954c1994 GS |
7160 | /* |
7161 | =for apidoc sv_2mortal | |
7162 | ||
d4236ebc DM |
7163 | Marks an existing SV as mortal. The SV will be destroyed "soon", either |
7164 | by an explicit call to FREETMPS, or by an implicit call at places such as | |
37d2ac18 NC |
7165 | statement boundaries. SvTEMP() is turned on which means that the SV's |
7166 | string buffer can be "stolen" if this SV is copied. See also C<sv_newmortal> | |
7167 | and C<sv_mortalcopy>. | |
954c1994 GS |
7168 | |
7169 | =cut | |
7170 | */ | |
7171 | ||
79072805 | 7172 | SV * |
864dbfa3 | 7173 | Perl_sv_2mortal(pTHX_ register SV *sv) |
79072805 | 7174 | { |
27da23d5 | 7175 | dVAR; |
79072805 LW |
7176 | if (!sv) |
7177 | return sv; | |
d689ffdd | 7178 | if (SvREADONLY(sv) && SvIMMORTAL(sv)) |
11162842 | 7179 | return sv; |
677b06e3 GS |
7180 | EXTEND_MORTAL(1); |
7181 | PL_tmps_stack[++PL_tmps_ix] = sv; | |
8990e307 | 7182 | SvTEMP_on(sv); |
79072805 LW |
7183 | return sv; |
7184 | } | |
7185 | ||
954c1994 GS |
7186 | /* |
7187 | =for apidoc newSVpv | |
7188 | ||
7189 | Creates a new SV and copies a string into it. The reference count for the | |
7190 | SV is set to 1. If C<len> is zero, Perl will compute the length using | |
7191 | strlen(). For efficiency, consider using C<newSVpvn> instead. | |
7192 | ||
7193 | =cut | |
7194 | */ | |
7195 | ||
79072805 | 7196 | SV * |
864dbfa3 | 7197 | Perl_newSVpv(pTHX_ const char *s, STRLEN len) |
79072805 | 7198 | { |
463ee0b2 | 7199 | register SV *sv; |
79072805 | 7200 | |
4561caa4 | 7201 | new_SV(sv); |
616d8c9c | 7202 | sv_setpvn(sv,s,len ? len : strlen(s)); |
79072805 LW |
7203 | return sv; |
7204 | } | |
7205 | ||
954c1994 GS |
7206 | /* |
7207 | =for apidoc newSVpvn | |
7208 | ||
7209 | Creates a new SV and copies a string into it. The reference count for the | |
1c846c1f | 7210 | SV is set to 1. Note that if C<len> is zero, Perl will create a zero length |
954c1994 | 7211 | string. You are responsible for ensuring that the source string is at least |
9e09f5f2 | 7212 | C<len> bytes long. If the C<s> argument is NULL the new SV will be undefined. |
954c1994 GS |
7213 | |
7214 | =cut | |
7215 | */ | |
7216 | ||
9da1e3b5 | 7217 | SV * |
864dbfa3 | 7218 | Perl_newSVpvn(pTHX_ const char *s, STRLEN len) |
9da1e3b5 MUN |
7219 | { |
7220 | register SV *sv; | |
7221 | ||
7222 | new_SV(sv); | |
9da1e3b5 MUN |
7223 | sv_setpvn(sv,s,len); |
7224 | return sv; | |
7225 | } | |
7226 | ||
bd08039b NC |
7227 | |
7228 | /* | |
926f8064 | 7229 | =for apidoc newSVhek |
bd08039b NC |
7230 | |
7231 | Creates a new SV from the hash key structure. It will generate scalars that | |
5aaec2b4 NC |
7232 | point to the shared string table where possible. Returns a new (undefined) |
7233 | SV if the hek is NULL. | |
bd08039b NC |
7234 | |
7235 | =cut | |
7236 | */ | |
7237 | ||
7238 | SV * | |
c1b02ed8 | 7239 | Perl_newSVhek(pTHX_ const HEK *hek) |
bd08039b | 7240 | { |
5aaec2b4 NC |
7241 | if (!hek) { |
7242 | SV *sv; | |
7243 | ||
7244 | new_SV(sv); | |
7245 | return sv; | |
7246 | } | |
7247 | ||
bd08039b NC |
7248 | if (HEK_LEN(hek) == HEf_SVKEY) { |
7249 | return newSVsv(*(SV**)HEK_KEY(hek)); | |
7250 | } else { | |
7251 | const int flags = HEK_FLAGS(hek); | |
7252 | if (flags & HVhek_WASUTF8) { | |
7253 | /* Trouble :-) | |
7254 | Andreas would like keys he put in as utf8 to come back as utf8 | |
7255 | */ | |
7256 | STRLEN utf8_len = HEK_LEN(hek); | |
7257 | U8 *as_utf8 = bytes_to_utf8 ((U8*)HEK_KEY(hek), &utf8_len); | |
7258 | SV *sv = newSVpvn ((char*)as_utf8, utf8_len); | |
7259 | ||
7260 | SvUTF8_on (sv); | |
7261 | Safefree (as_utf8); /* bytes_to_utf8() allocates a new string */ | |
7262 | return sv; | |
7263 | } else if (flags & HVhek_REHASH) { | |
7264 | /* We don't have a pointer to the hv, so we have to replicate the | |
7265 | flag into every HEK. This hv is using custom a hasing | |
7266 | algorithm. Hence we can't return a shared string scalar, as | |
7267 | that would contain the (wrong) hash value, and might get passed | |
7268 | into an hv routine with a regular hash */ | |
7269 | ||
7270 | SV *sv = newSVpvn (HEK_KEY(hek), HEK_LEN(hek)); | |
7271 | if (HEK_UTF8(hek)) | |
7272 | SvUTF8_on (sv); | |
7273 | return sv; | |
7274 | } | |
7275 | /* This will be overwhelminly the most common case. */ | |
7276 | return newSVpvn_share(HEK_KEY(hek), | |
7277 | (HEK_UTF8(hek) ? -HEK_LEN(hek) : HEK_LEN(hek)), | |
7278 | HEK_HASH(hek)); | |
7279 | } | |
7280 | } | |
7281 | ||
1c846c1f NIS |
7282 | /* |
7283 | =for apidoc newSVpvn_share | |
7284 | ||
3f7c398e | 7285 | Creates a new SV with its SvPVX_const pointing to a shared string in the string |
645c22ef DM |
7286 | table. If the string does not already exist in the table, it is created |
7287 | first. Turns on READONLY and FAKE. The string's hash is stored in the UV | |
7288 | slot of the SV; if the C<hash> parameter is non-zero, that value is used; | |
7289 | otherwise the hash is computed. The idea here is that as the string table | |
3f7c398e | 7290 | is used for shared hash keys these strings will have SvPVX_const == HeKEY and |
645c22ef | 7291 | hash lookup will avoid string compare. |
1c846c1f NIS |
7292 | |
7293 | =cut | |
7294 | */ | |
7295 | ||
7296 | SV * | |
c3654f1a | 7297 | Perl_newSVpvn_share(pTHX_ const char *src, I32 len, U32 hash) |
1c846c1f NIS |
7298 | { |
7299 | register SV *sv; | |
c3654f1a IH |
7300 | bool is_utf8 = FALSE; |
7301 | if (len < 0) { | |
77caf834 | 7302 | STRLEN tmplen = -len; |
c3654f1a | 7303 | is_utf8 = TRUE; |
75a54232 | 7304 | /* See the note in hv.c:hv_fetch() --jhi */ |
e1ec3a88 | 7305 | src = (char*)bytes_from_utf8((const U8*)src, &tmplen, &is_utf8); |
75a54232 JH |
7306 | len = tmplen; |
7307 | } | |
1c846c1f | 7308 | if (!hash) |
5afd6d42 | 7309 | PERL_HASH(hash, src, len); |
1c846c1f | 7310 | new_SV(sv); |
bdd68bc3 | 7311 | sv_upgrade(sv, SVt_PV); |
f880fe2f | 7312 | SvPV_set(sv, sharepvn(src, is_utf8?-len:len, hash)); |
b162af07 | 7313 | SvCUR_set(sv, len); |
b162af07 | 7314 | SvLEN_set(sv, 0); |
1c846c1f NIS |
7315 | SvREADONLY_on(sv); |
7316 | SvFAKE_on(sv); | |
7317 | SvPOK_on(sv); | |
c3654f1a IH |
7318 | if (is_utf8) |
7319 | SvUTF8_on(sv); | |
1c846c1f NIS |
7320 | return sv; |
7321 | } | |
7322 | ||
645c22ef | 7323 | |
cea2e8a9 | 7324 | #if defined(PERL_IMPLICIT_CONTEXT) |
645c22ef DM |
7325 | |
7326 | /* pTHX_ magic can't cope with varargs, so this is a no-context | |
7327 | * version of the main function, (which may itself be aliased to us). | |
7328 | * Don't access this version directly. | |
7329 | */ | |
7330 | ||
46fc3d4c | 7331 | SV * |
cea2e8a9 | 7332 | Perl_newSVpvf_nocontext(const char* pat, ...) |
46fc3d4c | 7333 | { |
cea2e8a9 | 7334 | dTHX; |
46fc3d4c | 7335 | register SV *sv; |
7336 | va_list args; | |
46fc3d4c | 7337 | va_start(args, pat); |
c5be433b | 7338 | sv = vnewSVpvf(pat, &args); |
46fc3d4c | 7339 | va_end(args); |
7340 | return sv; | |
7341 | } | |
cea2e8a9 | 7342 | #endif |
46fc3d4c | 7343 | |
954c1994 GS |
7344 | /* |
7345 | =for apidoc newSVpvf | |
7346 | ||
645c22ef | 7347 | Creates a new SV and initializes it with the string formatted like |
954c1994 GS |
7348 | C<sprintf>. |
7349 | ||
7350 | =cut | |
7351 | */ | |
7352 | ||
cea2e8a9 GS |
7353 | SV * |
7354 | Perl_newSVpvf(pTHX_ const char* pat, ...) | |
7355 | { | |
7356 | register SV *sv; | |
7357 | va_list args; | |
cea2e8a9 | 7358 | va_start(args, pat); |
c5be433b | 7359 | sv = vnewSVpvf(pat, &args); |
cea2e8a9 GS |
7360 | va_end(args); |
7361 | return sv; | |
7362 | } | |
46fc3d4c | 7363 | |
645c22ef DM |
7364 | /* backend for newSVpvf() and newSVpvf_nocontext() */ |
7365 | ||
79072805 | 7366 | SV * |
c5be433b GS |
7367 | Perl_vnewSVpvf(pTHX_ const char* pat, va_list* args) |
7368 | { | |
7369 | register SV *sv; | |
7370 | new_SV(sv); | |
7371 | sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
7372 | return sv; | |
7373 | } | |
7374 | ||
954c1994 GS |
7375 | /* |
7376 | =for apidoc newSVnv | |
7377 | ||
7378 | Creates a new SV and copies a floating point value into it. | |
7379 | The reference count for the SV is set to 1. | |
7380 | ||
7381 | =cut | |
7382 | */ | |
7383 | ||
c5be433b | 7384 | SV * |
65202027 | 7385 | Perl_newSVnv(pTHX_ NV n) |
79072805 | 7386 | { |
463ee0b2 | 7387 | register SV *sv; |
79072805 | 7388 | |
4561caa4 | 7389 | new_SV(sv); |
79072805 LW |
7390 | sv_setnv(sv,n); |
7391 | return sv; | |
7392 | } | |
7393 | ||
954c1994 GS |
7394 | /* |
7395 | =for apidoc newSViv | |
7396 | ||
7397 | Creates a new SV and copies an integer into it. The reference count for the | |
7398 | SV is set to 1. | |
7399 | ||
7400 | =cut | |
7401 | */ | |
7402 | ||
79072805 | 7403 | SV * |
864dbfa3 | 7404 | Perl_newSViv(pTHX_ IV i) |
79072805 | 7405 | { |
463ee0b2 | 7406 | register SV *sv; |
79072805 | 7407 | |
4561caa4 | 7408 | new_SV(sv); |
79072805 LW |
7409 | sv_setiv(sv,i); |
7410 | return sv; | |
7411 | } | |
7412 | ||
954c1994 | 7413 | /* |
1a3327fb JH |
7414 | =for apidoc newSVuv |
7415 | ||
7416 | Creates a new SV and copies an unsigned integer into it. | |
7417 | The reference count for the SV is set to 1. | |
7418 | ||
7419 | =cut | |
7420 | */ | |
7421 | ||
7422 | SV * | |
7423 | Perl_newSVuv(pTHX_ UV u) | |
7424 | { | |
7425 | register SV *sv; | |
7426 | ||
7427 | new_SV(sv); | |
7428 | sv_setuv(sv,u); | |
7429 | return sv; | |
7430 | } | |
7431 | ||
7432 | /* | |
954c1994 GS |
7433 | =for apidoc newRV_noinc |
7434 | ||
7435 | Creates an RV wrapper for an SV. The reference count for the original | |
7436 | SV is B<not> incremented. | |
7437 | ||
7438 | =cut | |
7439 | */ | |
7440 | ||
2304df62 | 7441 | SV * |
864dbfa3 | 7442 | Perl_newRV_noinc(pTHX_ SV *tmpRef) |
2304df62 AD |
7443 | { |
7444 | register SV *sv; | |
7445 | ||
4561caa4 | 7446 | new_SV(sv); |
2304df62 | 7447 | sv_upgrade(sv, SVt_RV); |
76e3520e | 7448 | SvTEMP_off(tmpRef); |
b162af07 | 7449 | SvRV_set(sv, tmpRef); |
2304df62 | 7450 | SvROK_on(sv); |
2304df62 AD |
7451 | return sv; |
7452 | } | |
7453 | ||
ff276b08 | 7454 | /* newRV_inc is the official function name to use now. |
645c22ef DM |
7455 | * newRV_inc is in fact #defined to newRV in sv.h |
7456 | */ | |
7457 | ||
5f05dabc | 7458 | SV * |
864dbfa3 | 7459 | Perl_newRV(pTHX_ SV *tmpRef) |
5f05dabc | 7460 | { |
5f6447b6 | 7461 | return newRV_noinc(SvREFCNT_inc(tmpRef)); |
5f05dabc | 7462 | } |
5f05dabc | 7463 | |
954c1994 GS |
7464 | /* |
7465 | =for apidoc newSVsv | |
7466 | ||
7467 | Creates a new SV which is an exact duplicate of the original SV. | |
645c22ef | 7468 | (Uses C<sv_setsv>). |
954c1994 GS |
7469 | |
7470 | =cut | |
7471 | */ | |
7472 | ||
79072805 | 7473 | SV * |
864dbfa3 | 7474 | Perl_newSVsv(pTHX_ register SV *old) |
79072805 | 7475 | { |
463ee0b2 | 7476 | register SV *sv; |
79072805 LW |
7477 | |
7478 | if (!old) | |
7479 | return Nullsv; | |
8990e307 | 7480 | if (SvTYPE(old) == SVTYPEMASK) { |
0453d815 | 7481 | if (ckWARN_d(WARN_INTERNAL)) |
9014280d | 7482 | Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "semi-panic: attempt to dup freed string"); |
79072805 LW |
7483 | return Nullsv; |
7484 | } | |
4561caa4 | 7485 | new_SV(sv); |
e90aabeb NC |
7486 | /* SV_GMAGIC is the default for sv_setv() |
7487 | SV_NOSTEAL prevents TEMP buffers being, well, stolen, and saves games | |
7488 | with SvTEMP_off and SvTEMP_on round a call to sv_setsv. */ | |
7489 | sv_setsv_flags(sv, old, SV_GMAGIC | SV_NOSTEAL); | |
463ee0b2 | 7490 | return sv; |
79072805 LW |
7491 | } |
7492 | ||
645c22ef DM |
7493 | /* |
7494 | =for apidoc sv_reset | |
7495 | ||
7496 | Underlying implementation for the C<reset> Perl function. | |
7497 | Note that the perl-level function is vaguely deprecated. | |
7498 | ||
7499 | =cut | |
7500 | */ | |
7501 | ||
79072805 | 7502 | void |
e1ec3a88 | 7503 | Perl_sv_reset(pTHX_ register const char *s, HV *stash) |
79072805 | 7504 | { |
27da23d5 | 7505 | dVAR; |
4802d5d7 | 7506 | char todo[PERL_UCHAR_MAX+1]; |
79072805 | 7507 | |
49d8d3a1 MB |
7508 | if (!stash) |
7509 | return; | |
7510 | ||
79072805 | 7511 | if (!*s) { /* reset ?? searches */ |
8d2f4536 NC |
7512 | MAGIC *mg = mg_find((SV *)stash, PERL_MAGIC_symtab); |
7513 | if (mg) { | |
7514 | PMOP *pm = (PMOP *) mg->mg_obj; | |
7515 | while (pm) { | |
7516 | pm->op_pmdynflags &= ~PMdf_USED; | |
7517 | pm = pm->op_pmnext; | |
7518 | } | |
79072805 LW |
7519 | } |
7520 | return; | |
7521 | } | |
7522 | ||
7523 | /* reset variables */ | |
7524 | ||
7525 | if (!HvARRAY(stash)) | |
7526 | return; | |
463ee0b2 LW |
7527 | |
7528 | Zero(todo, 256, char); | |
79072805 | 7529 | while (*s) { |
b464bac0 AL |
7530 | I32 max; |
7531 | I32 i = (unsigned char)*s; | |
79072805 LW |
7532 | if (s[1] == '-') { |
7533 | s += 2; | |
7534 | } | |
4802d5d7 | 7535 | max = (unsigned char)*s++; |
79072805 | 7536 | for ( ; i <= max; i++) { |
463ee0b2 LW |
7537 | todo[i] = 1; |
7538 | } | |
a0d0e21e | 7539 | for (i = 0; i <= (I32) HvMAX(stash); i++) { |
b464bac0 | 7540 | HE *entry; |
79072805 | 7541 | for (entry = HvARRAY(stash)[i]; |
9e35f4b3 GS |
7542 | entry; |
7543 | entry = HeNEXT(entry)) | |
7544 | { | |
b464bac0 AL |
7545 | register GV *gv; |
7546 | register SV *sv; | |
7547 | ||
1edc1566 | 7548 | if (!todo[(U8)*HeKEY(entry)]) |
463ee0b2 | 7549 | continue; |
1edc1566 | 7550 | gv = (GV*)HeVAL(entry); |
79072805 | 7551 | sv = GvSV(gv); |
9e35f4b3 GS |
7552 | if (SvTHINKFIRST(sv)) { |
7553 | if (!SvREADONLY(sv) && SvROK(sv)) | |
7554 | sv_unref(sv); | |
7555 | continue; | |
7556 | } | |
0c34ef67 | 7557 | SvOK_off(sv); |
79072805 LW |
7558 | if (SvTYPE(sv) >= SVt_PV) { |
7559 | SvCUR_set(sv, 0); | |
3f7c398e | 7560 | if (SvPVX_const(sv) != Nullch) |
463ee0b2 | 7561 | *SvPVX(sv) = '\0'; |
44a8e56a | 7562 | SvTAINT(sv); |
79072805 LW |
7563 | } |
7564 | if (GvAV(gv)) { | |
7565 | av_clear(GvAV(gv)); | |
7566 | } | |
bfcb3514 | 7567 | if (GvHV(gv) && !HvNAME_get(GvHV(gv))) { |
463ee0b2 | 7568 | hv_clear(GvHV(gv)); |
2f42fcb0 | 7569 | #ifndef PERL_MICRO |
fa6a1c44 | 7570 | #ifdef USE_ENVIRON_ARRAY |
4efc5df6 GS |
7571 | if (gv == PL_envgv |
7572 | # ifdef USE_ITHREADS | |
7573 | && PL_curinterp == aTHX | |
7574 | # endif | |
7575 | ) | |
7576 | { | |
79072805 | 7577 | environ[0] = Nullch; |
4efc5df6 | 7578 | } |
a0d0e21e | 7579 | #endif |
2f42fcb0 | 7580 | #endif /* !PERL_MICRO */ |
79072805 LW |
7581 | } |
7582 | } | |
7583 | } | |
7584 | } | |
7585 | } | |
7586 | ||
645c22ef DM |
7587 | /* |
7588 | =for apidoc sv_2io | |
7589 | ||
7590 | Using various gambits, try to get an IO from an SV: the IO slot if its a | |
7591 | GV; or the recursive result if we're an RV; or the IO slot of the symbol | |
7592 | named after the PV if we're a string. | |
7593 | ||
7594 | =cut | |
7595 | */ | |
7596 | ||
46fc3d4c | 7597 | IO* |
864dbfa3 | 7598 | Perl_sv_2io(pTHX_ SV *sv) |
46fc3d4c | 7599 | { |
7600 | IO* io; | |
7601 | GV* gv; | |
7602 | ||
7603 | switch (SvTYPE(sv)) { | |
7604 | case SVt_PVIO: | |
7605 | io = (IO*)sv; | |
7606 | break; | |
7607 | case SVt_PVGV: | |
7608 | gv = (GV*)sv; | |
7609 | io = GvIO(gv); | |
7610 | if (!io) | |
cea2e8a9 | 7611 | Perl_croak(aTHX_ "Bad filehandle: %s", GvNAME(gv)); |
46fc3d4c | 7612 | break; |
7613 | default: | |
7614 | if (!SvOK(sv)) | |
cea2e8a9 | 7615 | Perl_croak(aTHX_ PL_no_usym, "filehandle"); |
46fc3d4c | 7616 | if (SvROK(sv)) |
7617 | return sv_2io(SvRV(sv)); | |
7a5fd60d | 7618 | gv = gv_fetchsv(sv, FALSE, SVt_PVIO); |
46fc3d4c | 7619 | if (gv) |
7620 | io = GvIO(gv); | |
7621 | else | |
7622 | io = 0; | |
7623 | if (!io) | |
35c1215d | 7624 | Perl_croak(aTHX_ "Bad filehandle: %"SVf, sv); |
46fc3d4c | 7625 | break; |
7626 | } | |
7627 | return io; | |
7628 | } | |
7629 | ||
645c22ef DM |
7630 | /* |
7631 | =for apidoc sv_2cv | |
7632 | ||
7633 | Using various gambits, try to get a CV from an SV; in addition, try if | |
7634 | possible to set C<*st> and C<*gvp> to the stash and GV associated with it. | |
7635 | ||
7636 | =cut | |
7637 | */ | |
7638 | ||
79072805 | 7639 | CV * |
864dbfa3 | 7640 | Perl_sv_2cv(pTHX_ SV *sv, HV **st, GV **gvp, I32 lref) |
79072805 | 7641 | { |
27da23d5 | 7642 | dVAR; |
c04a4dfe JH |
7643 | GV *gv = Nullgv; |
7644 | CV *cv = Nullcv; | |
79072805 LW |
7645 | |
7646 | if (!sv) | |
93a17b20 | 7647 | return *gvp = Nullgv, Nullcv; |
79072805 | 7648 | switch (SvTYPE(sv)) { |
79072805 LW |
7649 | case SVt_PVCV: |
7650 | *st = CvSTASH(sv); | |
7651 | *gvp = Nullgv; | |
7652 | return (CV*)sv; | |
7653 | case SVt_PVHV: | |
7654 | case SVt_PVAV: | |
7655 | *gvp = Nullgv; | |
7656 | return Nullcv; | |
8990e307 LW |
7657 | case SVt_PVGV: |
7658 | gv = (GV*)sv; | |
a0d0e21e | 7659 | *gvp = gv; |
8990e307 LW |
7660 | *st = GvESTASH(gv); |
7661 | goto fix_gv; | |
7662 | ||
79072805 | 7663 | default: |
a0d0e21e LW |
7664 | if (SvGMAGICAL(sv)) |
7665 | mg_get(sv); | |
7666 | if (SvROK(sv)) { | |
f5284f61 IZ |
7667 | SV **sp = &sv; /* Used in tryAMAGICunDEREF macro. */ |
7668 | tryAMAGICunDEREF(to_cv); | |
7669 | ||
62f274bf GS |
7670 | sv = SvRV(sv); |
7671 | if (SvTYPE(sv) == SVt_PVCV) { | |
7672 | cv = (CV*)sv; | |
7673 | *gvp = Nullgv; | |
7674 | *st = CvSTASH(cv); | |
7675 | return cv; | |
7676 | } | |
7677 | else if(isGV(sv)) | |
7678 | gv = (GV*)sv; | |
7679 | else | |
cea2e8a9 | 7680 | Perl_croak(aTHX_ "Not a subroutine reference"); |
a0d0e21e | 7681 | } |
62f274bf | 7682 | else if (isGV(sv)) |
79072805 LW |
7683 | gv = (GV*)sv; |
7684 | else | |
7a5fd60d | 7685 | gv = gv_fetchsv(sv, lref, SVt_PVCV); |
79072805 LW |
7686 | *gvp = gv; |
7687 | if (!gv) | |
7688 | return Nullcv; | |
7689 | *st = GvESTASH(gv); | |
8990e307 | 7690 | fix_gv: |
8ebc5c01 | 7691 | if (lref && !GvCVu(gv)) { |
4633a7c4 | 7692 | SV *tmpsv; |
748a9306 | 7693 | ENTER; |
4633a7c4 | 7694 | tmpsv = NEWSV(704,0); |
16660edb | 7695 | gv_efullname3(tmpsv, gv, Nullch); |
f6ec51f7 GS |
7696 | /* XXX this is probably not what they think they're getting. |
7697 | * It has the same effect as "sub name;", i.e. just a forward | |
7698 | * declaration! */ | |
774d564b | 7699 | newSUB(start_subparse(FALSE, 0), |
4633a7c4 LW |
7700 | newSVOP(OP_CONST, 0, tmpsv), |
7701 | Nullop, | |
8990e307 | 7702 | Nullop); |
748a9306 | 7703 | LEAVE; |
8ebc5c01 | 7704 | if (!GvCVu(gv)) |
35c1215d NC |
7705 | Perl_croak(aTHX_ "Unable to create sub named \"%"SVf"\"", |
7706 | sv); | |
8990e307 | 7707 | } |
8ebc5c01 | 7708 | return GvCVu(gv); |
79072805 LW |
7709 | } |
7710 | } | |
7711 | ||
c461cf8f JH |
7712 | /* |
7713 | =for apidoc sv_true | |
7714 | ||
7715 | Returns true if the SV has a true value by Perl's rules. | |
645c22ef DM |
7716 | Use the C<SvTRUE> macro instead, which may call C<sv_true()> or may |
7717 | instead use an in-line version. | |
c461cf8f JH |
7718 | |
7719 | =cut | |
7720 | */ | |
7721 | ||
79072805 | 7722 | I32 |
864dbfa3 | 7723 | Perl_sv_true(pTHX_ register SV *sv) |
79072805 | 7724 | { |
8990e307 LW |
7725 | if (!sv) |
7726 | return 0; | |
79072805 | 7727 | if (SvPOK(sv)) { |
e1ec3a88 | 7728 | const register XPV* tXpv; |
4e35701f | 7729 | if ((tXpv = (XPV*)SvANY(sv)) && |
c2f1de04 | 7730 | (tXpv->xpv_cur > 1 || |
339049b0 | 7731 | (tXpv->xpv_cur && *sv->sv_u.svu_pv != '0'))) |
79072805 LW |
7732 | return 1; |
7733 | else | |
7734 | return 0; | |
7735 | } | |
7736 | else { | |
7737 | if (SvIOK(sv)) | |
463ee0b2 | 7738 | return SvIVX(sv) != 0; |
79072805 LW |
7739 | else { |
7740 | if (SvNOK(sv)) | |
463ee0b2 | 7741 | return SvNVX(sv) != 0.0; |
79072805 | 7742 | else |
463ee0b2 | 7743 | return sv_2bool(sv); |
79072805 LW |
7744 | } |
7745 | } | |
7746 | } | |
79072805 | 7747 | |
645c22ef DM |
7748 | /* |
7749 | =for apidoc sv_iv | |
7750 | ||
7751 | A private implementation of the C<SvIVx> macro for compilers which can't | |
7752 | cope with complex macro expressions. Always use the macro instead. | |
7753 | ||
7754 | =cut | |
7755 | */ | |
7756 | ||
ff68c719 | 7757 | IV |
864dbfa3 | 7758 | Perl_sv_iv(pTHX_ register SV *sv) |
85e6fe83 | 7759 | { |
25da4f38 IZ |
7760 | if (SvIOK(sv)) { |
7761 | if (SvIsUV(sv)) | |
7762 | return (IV)SvUVX(sv); | |
ff68c719 | 7763 | return SvIVX(sv); |
25da4f38 | 7764 | } |
ff68c719 | 7765 | return sv_2iv(sv); |
85e6fe83 | 7766 | } |
85e6fe83 | 7767 | |
645c22ef DM |
7768 | /* |
7769 | =for apidoc sv_uv | |
7770 | ||
7771 | A private implementation of the C<SvUVx> macro for compilers which can't | |
7772 | cope with complex macro expressions. Always use the macro instead. | |
7773 | ||
7774 | =cut | |
7775 | */ | |
7776 | ||
ff68c719 | 7777 | UV |
864dbfa3 | 7778 | Perl_sv_uv(pTHX_ register SV *sv) |
ff68c719 | 7779 | { |
25da4f38 IZ |
7780 | if (SvIOK(sv)) { |
7781 | if (SvIsUV(sv)) | |
7782 | return SvUVX(sv); | |
7783 | return (UV)SvIVX(sv); | |
7784 | } | |
ff68c719 | 7785 | return sv_2uv(sv); |
7786 | } | |
85e6fe83 | 7787 | |
645c22ef DM |
7788 | /* |
7789 | =for apidoc sv_nv | |
7790 | ||
7791 | A private implementation of the C<SvNVx> macro for compilers which can't | |
7792 | cope with complex macro expressions. Always use the macro instead. | |
7793 | ||
7794 | =cut | |
7795 | */ | |
7796 | ||
65202027 | 7797 | NV |
864dbfa3 | 7798 | Perl_sv_nv(pTHX_ register SV *sv) |
79072805 | 7799 | { |
ff68c719 | 7800 | if (SvNOK(sv)) |
7801 | return SvNVX(sv); | |
7802 | return sv_2nv(sv); | |
79072805 | 7803 | } |
79072805 | 7804 | |
09540bc3 JH |
7805 | /* sv_pv() is now a macro using SvPV_nolen(); |
7806 | * this function provided for binary compatibility only | |
7807 | */ | |
7808 | ||
7809 | char * | |
7810 | Perl_sv_pv(pTHX_ SV *sv) | |
7811 | { | |
09540bc3 JH |
7812 | if (SvPOK(sv)) |
7813 | return SvPVX(sv); | |
7814 | ||
93524f2b | 7815 | return sv_2pv(sv, 0); |
09540bc3 JH |
7816 | } |
7817 | ||
645c22ef DM |
7818 | /* |
7819 | =for apidoc sv_pv | |
7820 | ||
baca2b92 | 7821 | Use the C<SvPV_nolen> macro instead |
645c22ef | 7822 | |
645c22ef DM |
7823 | =for apidoc sv_pvn |
7824 | ||
7825 | A private implementation of the C<SvPV> macro for compilers which can't | |
7826 | cope with complex macro expressions. Always use the macro instead. | |
7827 | ||
7828 | =cut | |
7829 | */ | |
7830 | ||
1fa8b10d | 7831 | char * |
864dbfa3 | 7832 | Perl_sv_pvn(pTHX_ SV *sv, STRLEN *lp) |
79072805 | 7833 | { |
85e6fe83 LW |
7834 | if (SvPOK(sv)) { |
7835 | *lp = SvCUR(sv); | |
a0d0e21e | 7836 | return SvPVX(sv); |
85e6fe83 | 7837 | } |
463ee0b2 | 7838 | return sv_2pv(sv, lp); |
79072805 | 7839 | } |
79072805 | 7840 | |
6e9d1081 NC |
7841 | |
7842 | char * | |
7843 | Perl_sv_pvn_nomg(pTHX_ register SV *sv, STRLEN *lp) | |
7844 | { | |
7845 | if (SvPOK(sv)) { | |
7846 | *lp = SvCUR(sv); | |
7847 | return SvPVX(sv); | |
7848 | } | |
7849 | return sv_2pv_flags(sv, lp, 0); | |
7850 | } | |
7851 | ||
09540bc3 JH |
7852 | /* sv_pvn_force() is now a macro using Perl_sv_pvn_force_flags(); |
7853 | * this function provided for binary compatibility only | |
7854 | */ | |
7855 | ||
7856 | char * | |
7857 | Perl_sv_pvn_force(pTHX_ SV *sv, STRLEN *lp) | |
7858 | { | |
7859 | return sv_pvn_force_flags(sv, lp, SV_GMAGIC); | |
7860 | } | |
7861 | ||
c461cf8f JH |
7862 | /* |
7863 | =for apidoc sv_pvn_force | |
7864 | ||
7865 | Get a sensible string out of the SV somehow. | |
645c22ef DM |
7866 | A private implementation of the C<SvPV_force> macro for compilers which |
7867 | can't cope with complex macro expressions. Always use the macro instead. | |
c461cf8f | 7868 | |
8d6d96c1 HS |
7869 | =for apidoc sv_pvn_force_flags |
7870 | ||
7871 | Get a sensible string out of the SV somehow. | |
7872 | If C<flags> has C<SV_GMAGIC> bit set, will C<mg_get> on C<sv> if | |
7873 | appropriate, else not. C<sv_pvn_force> and C<sv_pvn_force_nomg> are | |
7874 | implemented in terms of this function. | |
645c22ef DM |
7875 | You normally want to use the various wrapper macros instead: see |
7876 | C<SvPV_force> and C<SvPV_force_nomg> | |
8d6d96c1 HS |
7877 | |
7878 | =cut | |
7879 | */ | |
7880 | ||
7881 | char * | |
7882 | Perl_sv_pvn_force_flags(pTHX_ SV *sv, STRLEN *lp, I32 flags) | |
7883 | { | |
a0d0e21e | 7884 | |
6fc92669 | 7885 | if (SvTHINKFIRST(sv) && !SvROK(sv)) |
765f542d | 7886 | sv_force_normal_flags(sv, 0); |
1c846c1f | 7887 | |
a0d0e21e | 7888 | if (SvPOK(sv)) { |
13c5b33c NC |
7889 | if (lp) |
7890 | *lp = SvCUR(sv); | |
a0d0e21e LW |
7891 | } |
7892 | else { | |
a3b680e6 | 7893 | char *s; |
13c5b33c NC |
7894 | STRLEN len; |
7895 | ||
4d84ee25 NC |
7896 | if (SvREADONLY(sv) && !(flags & SV_MUTABLE_RETURN)) { |
7897 | if (PL_op) | |
7898 | Perl_croak(aTHX_ "Can't coerce readonly %s to string in %s", | |
7899 | sv_reftype(sv,0), OP_NAME(PL_op)); | |
7900 | else | |
7901 | Perl_croak(aTHX_ "Can't coerce readonly %s to string", | |
7902 | sv_reftype(sv,0)); | |
7903 | } | |
748a9306 | 7904 | if (SvTYPE(sv) > SVt_PVLV && SvTYPE(sv) != SVt_PVFM) { |
cea2e8a9 | 7905 | Perl_croak(aTHX_ "Can't coerce %s to string in %s", sv_reftype(sv,0), |
53e06cf0 | 7906 | OP_NAME(PL_op)); |
a0d0e21e | 7907 | } |
4633a7c4 | 7908 | else |
13c5b33c NC |
7909 | s = sv_2pv_flags(sv, &len, flags); |
7910 | if (lp) | |
7911 | *lp = len; | |
7912 | ||
3f7c398e | 7913 | if (s != SvPVX_const(sv)) { /* Almost, but not quite, sv_setpvn() */ |
a0d0e21e LW |
7914 | if (SvROK(sv)) |
7915 | sv_unref(sv); | |
862a34c6 | 7916 | SvUPGRADE(sv, SVt_PV); /* Never FALSE */ |
a0d0e21e | 7917 | SvGROW(sv, len + 1); |
3f7c398e | 7918 | Move(s,SvPVX_const(sv),len,char); |
a0d0e21e LW |
7919 | SvCUR_set(sv, len); |
7920 | *SvEND(sv) = '\0'; | |
7921 | } | |
7922 | if (!SvPOK(sv)) { | |
7923 | SvPOK_on(sv); /* validate pointer */ | |
7924 | SvTAINT(sv); | |
1d7c1841 | 7925 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n", |
3f7c398e | 7926 | PTR2UV(sv),SvPVX_const(sv))); |
a0d0e21e LW |
7927 | } |
7928 | } | |
4d84ee25 | 7929 | return SvPVX_mutable(sv); |
a0d0e21e LW |
7930 | } |
7931 | ||
09540bc3 JH |
7932 | /* sv_pvbyte () is now a macro using Perl_sv_2pv_flags(); |
7933 | * this function provided for binary compatibility only | |
7934 | */ | |
7935 | ||
7936 | char * | |
7937 | Perl_sv_pvbyte(pTHX_ SV *sv) | |
7938 | { | |
7939 | sv_utf8_downgrade(sv,0); | |
7940 | return sv_pv(sv); | |
7941 | } | |
7942 | ||
645c22ef DM |
7943 | /* |
7944 | =for apidoc sv_pvbyte | |
7945 | ||
baca2b92 | 7946 | Use C<SvPVbyte_nolen> instead. |
645c22ef | 7947 | |
645c22ef DM |
7948 | =for apidoc sv_pvbyten |
7949 | ||
7950 | A private implementation of the C<SvPVbyte> macro for compilers | |
7951 | which can't cope with complex macro expressions. Always use the macro | |
7952 | instead. | |
7953 | ||
7954 | =cut | |
7955 | */ | |
7956 | ||
7340a771 GS |
7957 | char * |
7958 | Perl_sv_pvbyten(pTHX_ SV *sv, STRLEN *lp) | |
7959 | { | |
ffebcc3e | 7960 | sv_utf8_downgrade(sv,0); |
7340a771 GS |
7961 | return sv_pvn(sv,lp); |
7962 | } | |
7963 | ||
645c22ef DM |
7964 | /* |
7965 | =for apidoc sv_pvbyten_force | |
7966 | ||
7967 | A private implementation of the C<SvPVbytex_force> macro for compilers | |
7968 | which can't cope with complex macro expressions. Always use the macro | |
7969 | instead. | |
7970 | ||
7971 | =cut | |
7972 | */ | |
7973 | ||
7340a771 GS |
7974 | char * |
7975 | Perl_sv_pvbyten_force(pTHX_ SV *sv, STRLEN *lp) | |
7976 | { | |
46ec2f14 | 7977 | sv_pvn_force(sv,lp); |
ffebcc3e | 7978 | sv_utf8_downgrade(sv,0); |
46ec2f14 TS |
7979 | *lp = SvCUR(sv); |
7980 | return SvPVX(sv); | |
7340a771 GS |
7981 | } |
7982 | ||
09540bc3 JH |
7983 | /* sv_pvutf8 () is now a macro using Perl_sv_2pv_flags(); |
7984 | * this function provided for binary compatibility only | |
7985 | */ | |
7986 | ||
7987 | char * | |
7988 | Perl_sv_pvutf8(pTHX_ SV *sv) | |
7989 | { | |
7990 | sv_utf8_upgrade(sv); | |
7991 | return sv_pv(sv); | |
7992 | } | |
7993 | ||
645c22ef DM |
7994 | /* |
7995 | =for apidoc sv_pvutf8 | |
7996 | ||
baca2b92 | 7997 | Use the C<SvPVutf8_nolen> macro instead |
645c22ef | 7998 | |
645c22ef DM |
7999 | =for apidoc sv_pvutf8n |
8000 | ||
8001 | A private implementation of the C<SvPVutf8> macro for compilers | |
8002 | which can't cope with complex macro expressions. Always use the macro | |
8003 | instead. | |
8004 | ||
8005 | =cut | |
8006 | */ | |
8007 | ||
7340a771 GS |
8008 | char * |
8009 | Perl_sv_pvutf8n(pTHX_ SV *sv, STRLEN *lp) | |
8010 | { | |
560a288e | 8011 | sv_utf8_upgrade(sv); |
7340a771 GS |
8012 | return sv_pvn(sv,lp); |
8013 | } | |
8014 | ||
c461cf8f JH |
8015 | /* |
8016 | =for apidoc sv_pvutf8n_force | |
8017 | ||
645c22ef DM |
8018 | A private implementation of the C<SvPVutf8_force> macro for compilers |
8019 | which can't cope with complex macro expressions. Always use the macro | |
8020 | instead. | |
c461cf8f JH |
8021 | |
8022 | =cut | |
8023 | */ | |
8024 | ||
7340a771 GS |
8025 | char * |
8026 | Perl_sv_pvutf8n_force(pTHX_ SV *sv, STRLEN *lp) | |
8027 | { | |
46ec2f14 | 8028 | sv_pvn_force(sv,lp); |
560a288e | 8029 | sv_utf8_upgrade(sv); |
46ec2f14 TS |
8030 | *lp = SvCUR(sv); |
8031 | return SvPVX(sv); | |
7340a771 GS |
8032 | } |
8033 | ||
c461cf8f JH |
8034 | /* |
8035 | =for apidoc sv_reftype | |
8036 | ||
8037 | Returns a string describing what the SV is a reference to. | |
8038 | ||
8039 | =cut | |
8040 | */ | |
8041 | ||
1cb0ed9b | 8042 | char * |
bfed75c6 | 8043 | Perl_sv_reftype(pTHX_ const SV *sv, int ob) |
a0d0e21e | 8044 | { |
07409e01 NC |
8045 | /* The fact that I don't need to downcast to char * everywhere, only in ?: |
8046 | inside return suggests a const propagation bug in g++. */ | |
c86bf373 | 8047 | if (ob && SvOBJECT(sv)) { |
bfcb3514 | 8048 | char *name = HvNAME_get(SvSTASH(sv)); |
07409e01 | 8049 | return name ? name : (char *) "__ANON__"; |
c86bf373 | 8050 | } |
a0d0e21e LW |
8051 | else { |
8052 | switch (SvTYPE(sv)) { | |
8053 | case SVt_NULL: | |
8054 | case SVt_IV: | |
8055 | case SVt_NV: | |
8056 | case SVt_RV: | |
8057 | case SVt_PV: | |
8058 | case SVt_PVIV: | |
8059 | case SVt_PVNV: | |
8060 | case SVt_PVMG: | |
8061 | case SVt_PVBM: | |
1cb0ed9b | 8062 | if (SvVOK(sv)) |
439cb1c4 | 8063 | return "VSTRING"; |
a0d0e21e LW |
8064 | if (SvROK(sv)) |
8065 | return "REF"; | |
8066 | else | |
8067 | return "SCALAR"; | |
1cb0ed9b | 8068 | |
07409e01 | 8069 | case SVt_PVLV: return (char *) (SvROK(sv) ? "REF" |
be65207d DM |
8070 | /* tied lvalues should appear to be |
8071 | * scalars for backwards compatitbility */ | |
8072 | : (LvTYPE(sv) == 't' || LvTYPE(sv) == 'T') | |
07409e01 | 8073 | ? "SCALAR" : "LVALUE"); |
a0d0e21e LW |
8074 | case SVt_PVAV: return "ARRAY"; |
8075 | case SVt_PVHV: return "HASH"; | |
8076 | case SVt_PVCV: return "CODE"; | |
8077 | case SVt_PVGV: return "GLOB"; | |
1d2dff63 | 8078 | case SVt_PVFM: return "FORMAT"; |
27f9d8f3 | 8079 | case SVt_PVIO: return "IO"; |
a0d0e21e LW |
8080 | default: return "UNKNOWN"; |
8081 | } | |
8082 | } | |
8083 | } | |
8084 | ||
954c1994 GS |
8085 | /* |
8086 | =for apidoc sv_isobject | |
8087 | ||
8088 | Returns a boolean indicating whether the SV is an RV pointing to a blessed | |
8089 | object. If the SV is not an RV, or if the object is not blessed, then this | |
8090 | will return false. | |
8091 | ||
8092 | =cut | |
8093 | */ | |
8094 | ||
463ee0b2 | 8095 | int |
864dbfa3 | 8096 | Perl_sv_isobject(pTHX_ SV *sv) |
85e6fe83 | 8097 | { |
68dc0745 | 8098 | if (!sv) |
8099 | return 0; | |
8100 | if (SvGMAGICAL(sv)) | |
8101 | mg_get(sv); | |
85e6fe83 LW |
8102 | if (!SvROK(sv)) |
8103 | return 0; | |
8104 | sv = (SV*)SvRV(sv); | |
8105 | if (!SvOBJECT(sv)) | |
8106 | return 0; | |
8107 | return 1; | |
8108 | } | |
8109 | ||
954c1994 GS |
8110 | /* |
8111 | =for apidoc sv_isa | |
8112 | ||
8113 | Returns a boolean indicating whether the SV is blessed into the specified | |
8114 | class. This does not check for subtypes; use C<sv_derived_from> to verify | |
8115 | an inheritance relationship. | |
8116 | ||
8117 | =cut | |
8118 | */ | |
8119 | ||
85e6fe83 | 8120 | int |
864dbfa3 | 8121 | Perl_sv_isa(pTHX_ SV *sv, const char *name) |
463ee0b2 | 8122 | { |
bfcb3514 | 8123 | const char *hvname; |
68dc0745 | 8124 | if (!sv) |
8125 | return 0; | |
8126 | if (SvGMAGICAL(sv)) | |
8127 | mg_get(sv); | |
ed6116ce | 8128 | if (!SvROK(sv)) |
463ee0b2 | 8129 | return 0; |
ed6116ce LW |
8130 | sv = (SV*)SvRV(sv); |
8131 | if (!SvOBJECT(sv)) | |
463ee0b2 | 8132 | return 0; |
bfcb3514 NC |
8133 | hvname = HvNAME_get(SvSTASH(sv)); |
8134 | if (!hvname) | |
e27ad1f2 | 8135 | return 0; |
463ee0b2 | 8136 | |
bfcb3514 | 8137 | return strEQ(hvname, name); |
463ee0b2 LW |
8138 | } |
8139 | ||
954c1994 GS |
8140 | /* |
8141 | =for apidoc newSVrv | |
8142 | ||
8143 | Creates a new SV for the RV, C<rv>, to point to. If C<rv> is not an RV then | |
8144 | it will be upgraded to one. If C<classname> is non-null then the new SV will | |
8145 | be blessed in the specified package. The new SV is returned and its | |
8146 | reference count is 1. | |
8147 | ||
8148 | =cut | |
8149 | */ | |
8150 | ||
463ee0b2 | 8151 | SV* |
864dbfa3 | 8152 | Perl_newSVrv(pTHX_ SV *rv, const char *classname) |
463ee0b2 | 8153 | { |
463ee0b2 LW |
8154 | SV *sv; |
8155 | ||
4561caa4 | 8156 | new_SV(sv); |
51cf62d8 | 8157 | |
765f542d | 8158 | SV_CHECK_THINKFIRST_COW_DROP(rv); |
51cf62d8 | 8159 | SvAMAGIC_off(rv); |
51cf62d8 | 8160 | |
0199fce9 | 8161 | if (SvTYPE(rv) >= SVt_PVMG) { |
a3b680e6 | 8162 | const U32 refcnt = SvREFCNT(rv); |
0199fce9 JD |
8163 | SvREFCNT(rv) = 0; |
8164 | sv_clear(rv); | |
8165 | SvFLAGS(rv) = 0; | |
8166 | SvREFCNT(rv) = refcnt; | |
8167 | } | |
8168 | ||
51cf62d8 | 8169 | if (SvTYPE(rv) < SVt_RV) |
0199fce9 JD |
8170 | sv_upgrade(rv, SVt_RV); |
8171 | else if (SvTYPE(rv) > SVt_RV) { | |
8bd4d4c5 | 8172 | SvPV_free(rv); |
0199fce9 JD |
8173 | SvCUR_set(rv, 0); |
8174 | SvLEN_set(rv, 0); | |
8175 | } | |
51cf62d8 | 8176 | |
0c34ef67 | 8177 | SvOK_off(rv); |
b162af07 | 8178 | SvRV_set(rv, sv); |
ed6116ce | 8179 | SvROK_on(rv); |
463ee0b2 | 8180 | |
a0d0e21e LW |
8181 | if (classname) { |
8182 | HV* stash = gv_stashpv(classname, TRUE); | |
8183 | (void)sv_bless(rv, stash); | |
8184 | } | |
8185 | return sv; | |
8186 | } | |
8187 | ||
954c1994 GS |
8188 | /* |
8189 | =for apidoc sv_setref_pv | |
8190 | ||
8191 | Copies a pointer into a new SV, optionally blessing the SV. The C<rv> | |
8192 | argument will be upgraded to an RV. That RV will be modified to point to | |
8193 | the new SV. If the C<pv> argument is NULL then C<PL_sv_undef> will be placed | |
8194 | into the SV. The C<classname> argument indicates the package for the | |
8195 | blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV | |
d34c2299 | 8196 | will have a reference count of 1, and the RV will be returned. |
954c1994 GS |
8197 | |
8198 | Do not use with other Perl types such as HV, AV, SV, CV, because those | |
8199 | objects will become corrupted by the pointer copy process. | |
8200 | ||
8201 | Note that C<sv_setref_pvn> copies the string while this copies the pointer. | |
8202 | ||
8203 | =cut | |
8204 | */ | |
8205 | ||
a0d0e21e | 8206 | SV* |
864dbfa3 | 8207 | Perl_sv_setref_pv(pTHX_ SV *rv, const char *classname, void *pv) |
a0d0e21e | 8208 | { |
189b2af5 | 8209 | if (!pv) { |
3280af22 | 8210 | sv_setsv(rv, &PL_sv_undef); |
189b2af5 GS |
8211 | SvSETMAGIC(rv); |
8212 | } | |
a0d0e21e | 8213 | else |
56431972 | 8214 | sv_setiv(newSVrv(rv,classname), PTR2IV(pv)); |
a0d0e21e LW |
8215 | return rv; |
8216 | } | |
8217 | ||
954c1994 GS |
8218 | /* |
8219 | =for apidoc sv_setref_iv | |
8220 | ||
8221 | Copies an integer into a new SV, optionally blessing the SV. The C<rv> | |
8222 | argument will be upgraded to an RV. That RV will be modified to point to | |
8223 | the new SV. The C<classname> argument indicates the package for the | |
8224 | blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV | |
d34c2299 | 8225 | will have a reference count of 1, and the RV will be returned. |
954c1994 GS |
8226 | |
8227 | =cut | |
8228 | */ | |
8229 | ||
a0d0e21e | 8230 | SV* |
864dbfa3 | 8231 | Perl_sv_setref_iv(pTHX_ SV *rv, const char *classname, IV iv) |
a0d0e21e LW |
8232 | { |
8233 | sv_setiv(newSVrv(rv,classname), iv); | |
8234 | return rv; | |
8235 | } | |
8236 | ||
954c1994 | 8237 | /* |
e1c57cef JH |
8238 | =for apidoc sv_setref_uv |
8239 | ||
8240 | Copies an unsigned integer into a new SV, optionally blessing the SV. The C<rv> | |
8241 | argument will be upgraded to an RV. That RV will be modified to point to | |
8242 | the new SV. The C<classname> argument indicates the package for the | |
8243 | blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV | |
d34c2299 | 8244 | will have a reference count of 1, and the RV will be returned. |
e1c57cef JH |
8245 | |
8246 | =cut | |
8247 | */ | |
8248 | ||
8249 | SV* | |
8250 | Perl_sv_setref_uv(pTHX_ SV *rv, const char *classname, UV uv) | |
8251 | { | |
8252 | sv_setuv(newSVrv(rv,classname), uv); | |
8253 | return rv; | |
8254 | } | |
8255 | ||
8256 | /* | |
954c1994 GS |
8257 | =for apidoc sv_setref_nv |
8258 | ||
8259 | Copies a double into a new SV, optionally blessing the SV. The C<rv> | |
8260 | argument will be upgraded to an RV. That RV will be modified to point to | |
8261 | the new SV. The C<classname> argument indicates the package for the | |
8262 | blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV | |
d34c2299 | 8263 | will have a reference count of 1, and the RV will be returned. |
954c1994 GS |
8264 | |
8265 | =cut | |
8266 | */ | |
8267 | ||
a0d0e21e | 8268 | SV* |
65202027 | 8269 | Perl_sv_setref_nv(pTHX_ SV *rv, const char *classname, NV nv) |
a0d0e21e LW |
8270 | { |
8271 | sv_setnv(newSVrv(rv,classname), nv); | |
8272 | return rv; | |
8273 | } | |
463ee0b2 | 8274 | |
954c1994 GS |
8275 | /* |
8276 | =for apidoc sv_setref_pvn | |
8277 | ||
8278 | Copies a string into a new SV, optionally blessing the SV. The length of the | |
8279 | string must be specified with C<n>. The C<rv> argument will be upgraded to | |
8280 | an RV. That RV will be modified to point to the new SV. The C<classname> | |
8281 | argument indicates the package for the blessing. Set C<classname> to | |
7a5fa8a2 | 8282 | C<Nullch> to avoid the blessing. The new SV will have a reference count |
d34c2299 | 8283 | of 1, and the RV will be returned. |
954c1994 GS |
8284 | |
8285 | Note that C<sv_setref_pv> copies the pointer while this copies the string. | |
8286 | ||
8287 | =cut | |
8288 | */ | |
8289 | ||
a0d0e21e | 8290 | SV* |
864dbfa3 | 8291 | Perl_sv_setref_pvn(pTHX_ SV *rv, const char *classname, char *pv, STRLEN n) |
a0d0e21e LW |
8292 | { |
8293 | sv_setpvn(newSVrv(rv,classname), pv, n); | |
463ee0b2 LW |
8294 | return rv; |
8295 | } | |
8296 | ||
954c1994 GS |
8297 | /* |
8298 | =for apidoc sv_bless | |
8299 | ||
8300 | Blesses an SV into a specified package. The SV must be an RV. The package | |
8301 | must be designated by its stash (see C<gv_stashpv()>). The reference count | |
8302 | of the SV is unaffected. | |
8303 | ||
8304 | =cut | |
8305 | */ | |
8306 | ||
a0d0e21e | 8307 | SV* |
864dbfa3 | 8308 | Perl_sv_bless(pTHX_ SV *sv, HV *stash) |
a0d0e21e | 8309 | { |
76e3520e | 8310 | SV *tmpRef; |
a0d0e21e | 8311 | if (!SvROK(sv)) |
cea2e8a9 | 8312 | Perl_croak(aTHX_ "Can't bless non-reference value"); |
76e3520e GS |
8313 | tmpRef = SvRV(sv); |
8314 | if (SvFLAGS(tmpRef) & (SVs_OBJECT|SVf_READONLY)) { | |
8315 | if (SvREADONLY(tmpRef)) | |
cea2e8a9 | 8316 | Perl_croak(aTHX_ PL_no_modify); |
76e3520e GS |
8317 | if (SvOBJECT(tmpRef)) { |
8318 | if (SvTYPE(tmpRef) != SVt_PVIO) | |
3280af22 | 8319 | --PL_sv_objcount; |
76e3520e | 8320 | SvREFCNT_dec(SvSTASH(tmpRef)); |
2e3febc6 | 8321 | } |
a0d0e21e | 8322 | } |
76e3520e GS |
8323 | SvOBJECT_on(tmpRef); |
8324 | if (SvTYPE(tmpRef) != SVt_PVIO) | |
3280af22 | 8325 | ++PL_sv_objcount; |
862a34c6 | 8326 | SvUPGRADE(tmpRef, SVt_PVMG); |
b162af07 | 8327 | SvSTASH_set(tmpRef, (HV*)SvREFCNT_inc(stash)); |
a0d0e21e | 8328 | |
2e3febc6 CS |
8329 | if (Gv_AMG(stash)) |
8330 | SvAMAGIC_on(sv); | |
8331 | else | |
8332 | SvAMAGIC_off(sv); | |
a0d0e21e | 8333 | |
1edbfb88 AB |
8334 | if(SvSMAGICAL(tmpRef)) |
8335 | if(mg_find(tmpRef, PERL_MAGIC_ext) || mg_find(tmpRef, PERL_MAGIC_uvar)) | |
8336 | mg_set(tmpRef); | |
8337 | ||
8338 | ||
ecdeb87c | 8339 | |
a0d0e21e LW |
8340 | return sv; |
8341 | } | |
8342 | ||
645c22ef | 8343 | /* Downgrades a PVGV to a PVMG. |
645c22ef DM |
8344 | */ |
8345 | ||
76e3520e | 8346 | STATIC void |
cea2e8a9 | 8347 | S_sv_unglob(pTHX_ SV *sv) |
a0d0e21e | 8348 | { |
850fabdf GS |
8349 | void *xpvmg; |
8350 | ||
a0d0e21e LW |
8351 | assert(SvTYPE(sv) == SVt_PVGV); |
8352 | SvFAKE_off(sv); | |
8353 | if (GvGP(sv)) | |
1edc1566 | 8354 | gp_free((GV*)sv); |
e826b3c7 GS |
8355 | if (GvSTASH(sv)) { |
8356 | SvREFCNT_dec(GvSTASH(sv)); | |
8357 | GvSTASH(sv) = Nullhv; | |
8358 | } | |
14befaf4 | 8359 | sv_unmagic(sv, PERL_MAGIC_glob); |
a0d0e21e | 8360 | Safefree(GvNAME(sv)); |
a5f75d66 | 8361 | GvMULTI_off(sv); |
850fabdf GS |
8362 | |
8363 | /* need to keep SvANY(sv) in the right arena */ | |
8364 | xpvmg = new_XPVMG(); | |
8365 | StructCopy(SvANY(sv), xpvmg, XPVMG); | |
8366 | del_XPVGV(SvANY(sv)); | |
8367 | SvANY(sv) = xpvmg; | |
8368 | ||
a0d0e21e LW |
8369 | SvFLAGS(sv) &= ~SVTYPEMASK; |
8370 | SvFLAGS(sv) |= SVt_PVMG; | |
8371 | } | |
8372 | ||
954c1994 | 8373 | /* |
840a7b70 | 8374 | =for apidoc sv_unref_flags |
954c1994 GS |
8375 | |
8376 | Unsets the RV status of the SV, and decrements the reference count of | |
8377 | whatever was being referenced by the RV. This can almost be thought of | |
840a7b70 IZ |
8378 | as a reversal of C<newSVrv>. The C<cflags> argument can contain |
8379 | C<SV_IMMEDIATE_UNREF> to force the reference count to be decremented | |
8380 | (otherwise the decrementing is conditional on the reference count being | |
8381 | different from one or the reference being a readonly SV). | |
7889fe52 | 8382 | See C<SvROK_off>. |
954c1994 GS |
8383 | |
8384 | =cut | |
8385 | */ | |
8386 | ||
ed6116ce | 8387 | void |
840a7b70 | 8388 | Perl_sv_unref_flags(pTHX_ SV *sv, U32 flags) |
ed6116ce | 8389 | { |
a0d0e21e | 8390 | SV* rv = SvRV(sv); |
810b8aa5 GS |
8391 | |
8392 | if (SvWEAKREF(sv)) { | |
8393 | sv_del_backref(sv); | |
8394 | SvWEAKREF_off(sv); | |
b162af07 | 8395 | SvRV_set(sv, NULL); |
810b8aa5 GS |
8396 | return; |
8397 | } | |
b162af07 | 8398 | SvRV_set(sv, NULL); |
ed6116ce | 8399 | SvROK_off(sv); |
04ca4930 NC |
8400 | /* You can't have a || SvREADONLY(rv) here, as $a = $$a, where $a was |
8401 | assigned to as BEGIN {$a = \"Foo"} will fail. */ | |
8402 | if (SvREFCNT(rv) != 1 || (flags & SV_IMMEDIATE_UNREF)) | |
4633a7c4 | 8403 | SvREFCNT_dec(rv); |
840a7b70 | 8404 | else /* XXX Hack, but hard to make $a=$a->[1] work otherwise */ |
4633a7c4 | 8405 | sv_2mortal(rv); /* Schedule for freeing later */ |
ed6116ce | 8406 | } |
8990e307 | 8407 | |
840a7b70 IZ |
8408 | /* |
8409 | =for apidoc sv_unref | |
8410 | ||
8411 | Unsets the RV status of the SV, and decrements the reference count of | |
8412 | whatever was being referenced by the RV. This can almost be thought of | |
8413 | as a reversal of C<newSVrv>. This is C<sv_unref_flags> with the C<flag> | |
7889fe52 | 8414 | being zero. See C<SvROK_off>. |
840a7b70 IZ |
8415 | |
8416 | =cut | |
8417 | */ | |
8418 | ||
8419 | void | |
8420 | Perl_sv_unref(pTHX_ SV *sv) | |
8421 | { | |
8422 | sv_unref_flags(sv, 0); | |
8423 | } | |
8424 | ||
645c22ef DM |
8425 | /* |
8426 | =for apidoc sv_taint | |
8427 | ||
8428 | Taint an SV. Use C<SvTAINTED_on> instead. | |
8429 | =cut | |
8430 | */ | |
8431 | ||
bbce6d69 | 8432 | void |
864dbfa3 | 8433 | Perl_sv_taint(pTHX_ SV *sv) |
bbce6d69 | 8434 | { |
14befaf4 | 8435 | sv_magic((sv), Nullsv, PERL_MAGIC_taint, Nullch, 0); |
bbce6d69 | 8436 | } |
8437 | ||
645c22ef DM |
8438 | /* |
8439 | =for apidoc sv_untaint | |
8440 | ||
8441 | Untaint an SV. Use C<SvTAINTED_off> instead. | |
8442 | =cut | |
8443 | */ | |
8444 | ||
bbce6d69 | 8445 | void |
864dbfa3 | 8446 | Perl_sv_untaint(pTHX_ SV *sv) |
bbce6d69 | 8447 | { |
13f57bf8 | 8448 | if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) { |
14befaf4 | 8449 | MAGIC *mg = mg_find(sv, PERL_MAGIC_taint); |
36477c24 | 8450 | if (mg) |
565764a8 | 8451 | mg->mg_len &= ~1; |
36477c24 | 8452 | } |
bbce6d69 | 8453 | } |
8454 | ||
645c22ef DM |
8455 | /* |
8456 | =for apidoc sv_tainted | |
8457 | ||
8458 | Test an SV for taintedness. Use C<SvTAINTED> instead. | |
8459 | =cut | |
8460 | */ | |
8461 | ||
bbce6d69 | 8462 | bool |
864dbfa3 | 8463 | Perl_sv_tainted(pTHX_ SV *sv) |
bbce6d69 | 8464 | { |
13f57bf8 | 8465 | if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) { |
a28509cc | 8466 | MAGIC * const mg = mg_find(sv, PERL_MAGIC_taint); |
155aba94 | 8467 | if (mg && ((mg->mg_len & 1) || ((mg->mg_len & 2) && mg->mg_obj == sv))) |
36477c24 | 8468 | return TRUE; |
8469 | } | |
8470 | return FALSE; | |
bbce6d69 | 8471 | } |
8472 | ||
09540bc3 JH |
8473 | /* |
8474 | =for apidoc sv_setpviv | |
8475 | ||
8476 | Copies an integer into the given SV, also updating its string value. | |
8477 | Does not handle 'set' magic. See C<sv_setpviv_mg>. | |
8478 | ||
8479 | =cut | |
8480 | */ | |
8481 | ||
8482 | void | |
8483 | Perl_sv_setpviv(pTHX_ SV *sv, IV iv) | |
8484 | { | |
8485 | char buf[TYPE_CHARS(UV)]; | |
8486 | char *ebuf; | |
8487 | char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf); | |
8488 | ||
8489 | sv_setpvn(sv, ptr, ebuf - ptr); | |
8490 | } | |
8491 | ||
8492 | /* | |
8493 | =for apidoc sv_setpviv_mg | |
8494 | ||
8495 | Like C<sv_setpviv>, but also handles 'set' magic. | |
8496 | ||
8497 | =cut | |
8498 | */ | |
8499 | ||
8500 | void | |
8501 | Perl_sv_setpviv_mg(pTHX_ SV *sv, IV iv) | |
8502 | { | |
8503 | char buf[TYPE_CHARS(UV)]; | |
8504 | char *ebuf; | |
8505 | char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf); | |
8506 | ||
8507 | sv_setpvn(sv, ptr, ebuf - ptr); | |
8508 | SvSETMAGIC(sv); | |
8509 | } | |
8510 | ||
cea2e8a9 | 8511 | #if defined(PERL_IMPLICIT_CONTEXT) |
645c22ef DM |
8512 | |
8513 | /* pTHX_ magic can't cope with varargs, so this is a no-context | |
8514 | * version of the main function, (which may itself be aliased to us). | |
8515 | * Don't access this version directly. | |
8516 | */ | |
8517 | ||
cea2e8a9 GS |
8518 | void |
8519 | Perl_sv_setpvf_nocontext(SV *sv, const char* pat, ...) | |
8520 | { | |
8521 | dTHX; | |
8522 | va_list args; | |
8523 | va_start(args, pat); | |
c5be433b | 8524 | sv_vsetpvf(sv, pat, &args); |
cea2e8a9 GS |
8525 | va_end(args); |
8526 | } | |
8527 | ||
645c22ef DM |
8528 | /* pTHX_ magic can't cope with varargs, so this is a no-context |
8529 | * version of the main function, (which may itself be aliased to us). | |
8530 | * Don't access this version directly. | |
8531 | */ | |
cea2e8a9 GS |
8532 | |
8533 | void | |
8534 | Perl_sv_setpvf_mg_nocontext(SV *sv, const char* pat, ...) | |
8535 | { | |
8536 | dTHX; | |
8537 | va_list args; | |
8538 | va_start(args, pat); | |
c5be433b | 8539 | sv_vsetpvf_mg(sv, pat, &args); |
cea2e8a9 | 8540 | va_end(args); |
cea2e8a9 GS |
8541 | } |
8542 | #endif | |
8543 | ||
954c1994 GS |
8544 | /* |
8545 | =for apidoc sv_setpvf | |
8546 | ||
bffc3d17 SH |
8547 | Works like C<sv_catpvf> but copies the text into the SV instead of |
8548 | appending it. Does not handle 'set' magic. See C<sv_setpvf_mg>. | |
954c1994 GS |
8549 | |
8550 | =cut | |
8551 | */ | |
8552 | ||
46fc3d4c | 8553 | void |
864dbfa3 | 8554 | Perl_sv_setpvf(pTHX_ SV *sv, const char* pat, ...) |
46fc3d4c | 8555 | { |
8556 | va_list args; | |
46fc3d4c | 8557 | va_start(args, pat); |
c5be433b | 8558 | sv_vsetpvf(sv, pat, &args); |
46fc3d4c | 8559 | va_end(args); |
8560 | } | |
8561 | ||
bffc3d17 SH |
8562 | /* |
8563 | =for apidoc sv_vsetpvf | |
8564 | ||
8565 | Works like C<sv_vcatpvf> but copies the text into the SV instead of | |
8566 | appending it. Does not handle 'set' magic. See C<sv_vsetpvf_mg>. | |
8567 | ||
8568 | Usually used via its frontend C<sv_setpvf>. | |
8569 | ||
8570 | =cut | |
8571 | */ | |
645c22ef | 8572 | |
c5be433b GS |
8573 | void |
8574 | Perl_sv_vsetpvf(pTHX_ SV *sv, const char* pat, va_list* args) | |
8575 | { | |
8576 | sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
8577 | } | |
ef50df4b | 8578 | |
954c1994 GS |
8579 | /* |
8580 | =for apidoc sv_setpvf_mg | |
8581 | ||
8582 | Like C<sv_setpvf>, but also handles 'set' magic. | |
8583 | ||
8584 | =cut | |
8585 | */ | |
8586 | ||
ef50df4b | 8587 | void |
864dbfa3 | 8588 | Perl_sv_setpvf_mg(pTHX_ SV *sv, const char* pat, ...) |
ef50df4b GS |
8589 | { |
8590 | va_list args; | |
ef50df4b | 8591 | va_start(args, pat); |
c5be433b | 8592 | sv_vsetpvf_mg(sv, pat, &args); |
ef50df4b | 8593 | va_end(args); |
c5be433b GS |
8594 | } |
8595 | ||
bffc3d17 SH |
8596 | /* |
8597 | =for apidoc sv_vsetpvf_mg | |
8598 | ||
8599 | Like C<sv_vsetpvf>, but also handles 'set' magic. | |
8600 | ||
8601 | Usually used via its frontend C<sv_setpvf_mg>. | |
8602 | ||
8603 | =cut | |
8604 | */ | |
645c22ef | 8605 | |
c5be433b GS |
8606 | void |
8607 | Perl_sv_vsetpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args) | |
8608 | { | |
8609 | sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
ef50df4b GS |
8610 | SvSETMAGIC(sv); |
8611 | } | |
8612 | ||
cea2e8a9 | 8613 | #if defined(PERL_IMPLICIT_CONTEXT) |
645c22ef DM |
8614 | |
8615 | /* pTHX_ magic can't cope with varargs, so this is a no-context | |
8616 | * version of the main function, (which may itself be aliased to us). | |
8617 | * Don't access this version directly. | |
8618 | */ | |
8619 | ||
cea2e8a9 GS |
8620 | void |
8621 | Perl_sv_catpvf_nocontext(SV *sv, const char* pat, ...) | |
8622 | { | |
8623 | dTHX; | |
8624 | va_list args; | |
8625 | va_start(args, pat); | |
c5be433b | 8626 | sv_vcatpvf(sv, pat, &args); |
cea2e8a9 GS |
8627 | va_end(args); |
8628 | } | |
8629 | ||
645c22ef DM |
8630 | /* pTHX_ magic can't cope with varargs, so this is a no-context |
8631 | * version of the main function, (which may itself be aliased to us). | |
8632 | * Don't access this version directly. | |
8633 | */ | |
8634 | ||
cea2e8a9 GS |
8635 | void |
8636 | Perl_sv_catpvf_mg_nocontext(SV *sv, const char* pat, ...) | |
8637 | { | |
8638 | dTHX; | |
8639 | va_list args; | |
8640 | va_start(args, pat); | |
c5be433b | 8641 | sv_vcatpvf_mg(sv, pat, &args); |
cea2e8a9 | 8642 | va_end(args); |
cea2e8a9 GS |
8643 | } |
8644 | #endif | |
8645 | ||
954c1994 GS |
8646 | /* |
8647 | =for apidoc sv_catpvf | |
8648 | ||
d5ce4a7c GA |
8649 | Processes its arguments like C<sprintf> and appends the formatted |
8650 | output to an SV. If the appended data contains "wide" characters | |
8651 | (including, but not limited to, SVs with a UTF-8 PV formatted with %s, | |
8652 | and characters >255 formatted with %c), the original SV might get | |
bffc3d17 | 8653 | upgraded to UTF-8. Handles 'get' magic, but not 'set' magic. See |
cdd94ca7 NC |
8654 | C<sv_catpvf_mg>. If the original SV was UTF-8, the pattern should be |
8655 | valid UTF-8; if the original SV was bytes, the pattern should be too. | |
954c1994 | 8656 | |
d5ce4a7c | 8657 | =cut */ |
954c1994 | 8658 | |
46fc3d4c | 8659 | void |
864dbfa3 | 8660 | Perl_sv_catpvf(pTHX_ SV *sv, const char* pat, ...) |
46fc3d4c | 8661 | { |
8662 | va_list args; | |
46fc3d4c | 8663 | va_start(args, pat); |
c5be433b | 8664 | sv_vcatpvf(sv, pat, &args); |
46fc3d4c | 8665 | va_end(args); |
8666 | } | |
8667 | ||
bffc3d17 SH |
8668 | /* |
8669 | =for apidoc sv_vcatpvf | |
8670 | ||
8671 | Processes its arguments like C<vsprintf> and appends the formatted output | |
8672 | to an SV. Does not handle 'set' magic. See C<sv_vcatpvf_mg>. | |
8673 | ||
8674 | Usually used via its frontend C<sv_catpvf>. | |
8675 | ||
8676 | =cut | |
8677 | */ | |
645c22ef | 8678 | |
ef50df4b | 8679 | void |
c5be433b GS |
8680 | Perl_sv_vcatpvf(pTHX_ SV *sv, const char* pat, va_list* args) |
8681 | { | |
8682 | sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
8683 | } | |
8684 | ||
954c1994 GS |
8685 | /* |
8686 | =for apidoc sv_catpvf_mg | |
8687 | ||
8688 | Like C<sv_catpvf>, but also handles 'set' magic. | |
8689 | ||
8690 | =cut | |
8691 | */ | |
8692 | ||
c5be433b | 8693 | void |
864dbfa3 | 8694 | Perl_sv_catpvf_mg(pTHX_ SV *sv, const char* pat, ...) |
ef50df4b GS |
8695 | { |
8696 | va_list args; | |
ef50df4b | 8697 | va_start(args, pat); |
c5be433b | 8698 | sv_vcatpvf_mg(sv, pat, &args); |
ef50df4b | 8699 | va_end(args); |
c5be433b GS |
8700 | } |
8701 | ||
bffc3d17 SH |
8702 | /* |
8703 | =for apidoc sv_vcatpvf_mg | |
8704 | ||
8705 | Like C<sv_vcatpvf>, but also handles 'set' magic. | |
8706 | ||
8707 | Usually used via its frontend C<sv_catpvf_mg>. | |
8708 | ||
8709 | =cut | |
8710 | */ | |
645c22ef | 8711 | |
c5be433b GS |
8712 | void |
8713 | Perl_sv_vcatpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args) | |
8714 | { | |
8715 | sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
ef50df4b GS |
8716 | SvSETMAGIC(sv); |
8717 | } | |
8718 | ||
954c1994 GS |
8719 | /* |
8720 | =for apidoc sv_vsetpvfn | |
8721 | ||
bffc3d17 | 8722 | Works like C<sv_vcatpvfn> but copies the text into the SV instead of |
954c1994 GS |
8723 | appending it. |
8724 | ||
bffc3d17 | 8725 | Usually used via one of its frontends C<sv_vsetpvf> and C<sv_vsetpvf_mg>. |
645c22ef | 8726 | |
954c1994 GS |
8727 | =cut |
8728 | */ | |
8729 | ||
46fc3d4c | 8730 | void |
7d5ea4e7 | 8731 | Perl_sv_vsetpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted) |
46fc3d4c | 8732 | { |
8733 | sv_setpvn(sv, "", 0); | |
7d5ea4e7 | 8734 | sv_vcatpvfn(sv, pat, patlen, args, svargs, svmax, maybe_tainted); |
46fc3d4c | 8735 | } |
8736 | ||
645c22ef DM |
8737 | /* private function for use in sv_vcatpvfn via the EXPECT_NUMBER macro */ |
8738 | ||
2d00ba3b | 8739 | STATIC I32 |
9dd79c3f | 8740 | S_expect_number(pTHX_ char** pattern) |
211dfcf1 HS |
8741 | { |
8742 | I32 var = 0; | |
8743 | switch (**pattern) { | |
8744 | case '1': case '2': case '3': | |
8745 | case '4': case '5': case '6': | |
8746 | case '7': case '8': case '9': | |
8747 | while (isDIGIT(**pattern)) | |
8748 | var = var * 10 + (*(*pattern)++ - '0'); | |
8749 | } | |
8750 | return var; | |
8751 | } | |
9dd79c3f | 8752 | #define EXPECT_NUMBER(pattern, var) (var = S_expect_number(aTHX_ &pattern)) |
211dfcf1 | 8753 | |
4151a5fe IZ |
8754 | static char * |
8755 | F0convert(NV nv, char *endbuf, STRLEN *len) | |
8756 | { | |
a3b680e6 | 8757 | const int neg = nv < 0; |
4151a5fe | 8758 | UV uv; |
4151a5fe IZ |
8759 | |
8760 | if (neg) | |
8761 | nv = -nv; | |
8762 | if (nv < UV_MAX) { | |
b464bac0 | 8763 | char *p = endbuf; |
4151a5fe | 8764 | nv += 0.5; |
028f8eaa | 8765 | uv = (UV)nv; |
4151a5fe IZ |
8766 | if (uv & 1 && uv == nv) |
8767 | uv--; /* Round to even */ | |
8768 | do { | |
a3b680e6 | 8769 | const unsigned dig = uv % 10; |
4151a5fe IZ |
8770 | *--p = '0' + dig; |
8771 | } while (uv /= 10); | |
8772 | if (neg) | |
8773 | *--p = '-'; | |
8774 | *len = endbuf - p; | |
8775 | return p; | |
8776 | } | |
8777 | return Nullch; | |
8778 | } | |
8779 | ||
8780 | ||
954c1994 GS |
8781 | /* |
8782 | =for apidoc sv_vcatpvfn | |
8783 | ||
8784 | Processes its arguments like C<vsprintf> and appends the formatted output | |
8785 | to an SV. Uses an array of SVs if the C style variable argument list is | |
8786 | missing (NULL). When running with taint checks enabled, indicates via | |
8787 | C<maybe_tainted> if results are untrustworthy (often due to the use of | |
8788 | locales). | |
8789 | ||
bffc3d17 | 8790 | Usually used via one of its frontends C<sv_vcatpvf> and C<sv_vcatpvf_mg>. |
645c22ef | 8791 | |
954c1994 GS |
8792 | =cut |
8793 | */ | |
8794 | ||
1ef29b0e RGS |
8795 | /* XXX maybe_tainted is never assigned to, so the doc above is lying. */ |
8796 | ||
46fc3d4c | 8797 | void |
7d5ea4e7 | 8798 | Perl_sv_vcatpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted) |
46fc3d4c | 8799 | { |
8800 | char *p; | |
8801 | char *q; | |
a3b680e6 | 8802 | const char *patend; |
fc36a67e | 8803 | STRLEN origlen; |
46fc3d4c | 8804 | I32 svix = 0; |
27da23d5 | 8805 | static const char nullstr[] = "(null)"; |
9c5ffd7c | 8806 | SV *argsv = Nullsv; |
b464bac0 AL |
8807 | bool has_utf8 = DO_UTF8(sv); /* has the result utf8? */ |
8808 | const bool pat_utf8 = has_utf8; /* the pattern is in utf8? */ | |
db79b45b | 8809 | SV *nsv = Nullsv; |
4151a5fe IZ |
8810 | /* Times 4: a decimal digit takes more than 3 binary digits. |
8811 | * NV_DIG: mantissa takes than many decimal digits. | |
8812 | * Plus 32: Playing safe. */ | |
8813 | char ebuf[IV_DIG * 4 + NV_DIG + 32]; | |
8814 | /* large enough for "%#.#f" --chip */ | |
8815 | /* what about long double NVs? --jhi */ | |
db79b45b | 8816 | |
53c1dcc0 AL |
8817 | PERL_UNUSED_ARG(maybe_tainted); |
8818 | ||
46fc3d4c | 8819 | /* no matter what, this is a string now */ |
fc36a67e | 8820 | (void)SvPV_force(sv, origlen); |
46fc3d4c | 8821 | |
0dbb1585 | 8822 | /* special-case "", "%s", and "%-p" (SVf) */ |
46fc3d4c | 8823 | if (patlen == 0) |
8824 | return; | |
0dbb1585 | 8825 | if (patlen == 2 && pat[0] == '%' && pat[1] == 's') { |
c635e13b | 8826 | if (args) { |
53c1dcc0 | 8827 | const char * const s = va_arg(*args, char*); |
c635e13b | 8828 | sv_catpv(sv, s ? s : nullstr); |
8829 | } | |
7e2040f0 | 8830 | else if (svix < svmax) { |
fc36a67e | 8831 | sv_catsv(sv, *svargs); |
7e2040f0 GS |
8832 | if (DO_UTF8(*svargs)) |
8833 | SvUTF8_on(sv); | |
8834 | } | |
fc36a67e | 8835 | return; |
0dbb1585 AL |
8836 | } |
8837 | if (patlen == 3 && pat[0] == '%' && | |
8838 | pat[1] == '-' && pat[2] == 'p') { | |
fc36a67e | 8839 | if (args) { |
7e2040f0 GS |
8840 | argsv = va_arg(*args, SV*); |
8841 | sv_catsv(sv, argsv); | |
8842 | if (DO_UTF8(argsv)) | |
8843 | SvUTF8_on(sv); | |
fc36a67e | 8844 | return; |
8845 | } | |
46fc3d4c | 8846 | } |
8847 | ||
1d917b39 | 8848 | #ifndef USE_LONG_DOUBLE |
4151a5fe | 8849 | /* special-case "%.<number>[gf]" */ |
7af36d83 | 8850 | if ( !args && patlen <= 5 && pat[0] == '%' && pat[1] == '.' |
4151a5fe IZ |
8851 | && (pat[patlen-1] == 'g' || pat[patlen-1] == 'f') ) { |
8852 | unsigned digits = 0; | |
8853 | const char *pp; | |
8854 | ||
8855 | pp = pat + 2; | |
8856 | while (*pp >= '0' && *pp <= '9') | |
8857 | digits = 10 * digits + (*pp++ - '0'); | |
028f8eaa | 8858 | if (pp - pat == (int)patlen - 1) { |
4151a5fe IZ |
8859 | NV nv; |
8860 | ||
7af36d83 | 8861 | if (svix < svmax) |
4151a5fe IZ |
8862 | nv = SvNV(*svargs); |
8863 | else | |
8864 | return; | |
8865 | if (*pp == 'g') { | |
2873255c NC |
8866 | /* Add check for digits != 0 because it seems that some |
8867 | gconverts are buggy in this case, and we don't yet have | |
8868 | a Configure test for this. */ | |
8869 | if (digits && digits < sizeof(ebuf) - NV_DIG - 10) { | |
8870 | /* 0, point, slack */ | |
2e59c212 | 8871 | Gconvert(nv, (int)digits, 0, ebuf); |
4151a5fe IZ |
8872 | sv_catpv(sv, ebuf); |
8873 | if (*ebuf) /* May return an empty string for digits==0 */ | |
8874 | return; | |
8875 | } | |
8876 | } else if (!digits) { | |
8877 | STRLEN l; | |
8878 | ||
8879 | if ((p = F0convert(nv, ebuf + sizeof ebuf, &l))) { | |
8880 | sv_catpvn(sv, p, l); | |
8881 | return; | |
8882 | } | |
8883 | } | |
8884 | } | |
8885 | } | |
1d917b39 | 8886 | #endif /* !USE_LONG_DOUBLE */ |
4151a5fe | 8887 | |
2cf2cfc6 | 8888 | if (!args && svix < svmax && DO_UTF8(*svargs)) |
205f51d8 | 8889 | has_utf8 = TRUE; |
2cf2cfc6 | 8890 | |
46fc3d4c | 8891 | patend = (char*)pat + patlen; |
8892 | for (p = (char*)pat; p < patend; p = q) { | |
8893 | bool alt = FALSE; | |
8894 | bool left = FALSE; | |
b22c7a20 | 8895 | bool vectorize = FALSE; |
211dfcf1 | 8896 | bool vectorarg = FALSE; |
2cf2cfc6 | 8897 | bool vec_utf8 = FALSE; |
46fc3d4c | 8898 | char fill = ' '; |
8899 | char plus = 0; | |
8900 | char intsize = 0; | |
8901 | STRLEN width = 0; | |
fc36a67e | 8902 | STRLEN zeros = 0; |
46fc3d4c | 8903 | bool has_precis = FALSE; |
8904 | STRLEN precis = 0; | |
58e33a90 | 8905 | I32 osvix = svix; |
2cf2cfc6 | 8906 | bool is_utf8 = FALSE; /* is this item utf8? */ |
20f6aaab AS |
8907 | #ifdef HAS_LDBL_SPRINTF_BUG |
8908 | /* This is to try to fix a bug with irix/nonstop-ux/powerux and | |
205f51d8 | 8909 | with sfio - Allen <allens@cpan.org> */ |
20f6aaab AS |
8910 | bool fix_ldbl_sprintf_bug = FALSE; |
8911 | #endif | |
205f51d8 | 8912 | |
46fc3d4c | 8913 | char esignbuf[4]; |
89ebb4a3 | 8914 | U8 utf8buf[UTF8_MAXBYTES+1]; |
46fc3d4c | 8915 | STRLEN esignlen = 0; |
8916 | ||
4d84ee25 | 8917 | const char *eptr = Nullch; |
fc36a67e | 8918 | STRLEN elen = 0; |
81f715da | 8919 | SV *vecsv = Nullsv; |
245d4a47 | 8920 | const U8 *vecstr = Null(U8*); |
b22c7a20 | 8921 | STRLEN veclen = 0; |
934abaf1 | 8922 | char c = 0; |
46fc3d4c | 8923 | int i; |
9c5ffd7c | 8924 | unsigned base = 0; |
8c8eb53c RB |
8925 | IV iv = 0; |
8926 | UV uv = 0; | |
9e5b023a JH |
8927 | /* we need a long double target in case HAS_LONG_DOUBLE but |
8928 | not USE_LONG_DOUBLE | |
8929 | */ | |
35fff930 | 8930 | #if defined(HAS_LONG_DOUBLE) && LONG_DOUBLESIZE > DOUBLESIZE |
9e5b023a JH |
8931 | long double nv; |
8932 | #else | |
65202027 | 8933 | NV nv; |
9e5b023a | 8934 | #endif |
46fc3d4c | 8935 | STRLEN have; |
8936 | STRLEN need; | |
8937 | STRLEN gap; | |
7af36d83 | 8938 | const char *dotstr = "."; |
b22c7a20 | 8939 | STRLEN dotstrlen = 1; |
211dfcf1 | 8940 | I32 efix = 0; /* explicit format parameter index */ |
eb3fce90 | 8941 | I32 ewix = 0; /* explicit width index */ |
211dfcf1 HS |
8942 | I32 epix = 0; /* explicit precision index */ |
8943 | I32 evix = 0; /* explicit vector index */ | |
eb3fce90 | 8944 | bool asterisk = FALSE; |
46fc3d4c | 8945 | |
211dfcf1 | 8946 | /* echo everything up to the next format specification */ |
46fc3d4c | 8947 | for (q = p; q < patend && *q != '%'; ++q) ; |
8948 | if (q > p) { | |
db79b45b JH |
8949 | if (has_utf8 && !pat_utf8) |
8950 | sv_catpvn_utf8_upgrade(sv, p, q - p, nsv); | |
8951 | else | |
8952 | sv_catpvn(sv, p, q - p); | |
46fc3d4c | 8953 | p = q; |
8954 | } | |
8955 | if (q++ >= patend) | |
8956 | break; | |
8957 | ||
211dfcf1 HS |
8958 | /* |
8959 | We allow format specification elements in this order: | |
8960 | \d+\$ explicit format parameter index | |
8961 | [-+ 0#]+ flags | |
a472f209 | 8962 | v|\*(\d+\$)?v vector with optional (optionally specified) arg |
f3583277 | 8963 | 0 flag (as above): repeated to allow "v02" |
211dfcf1 HS |
8964 | \d+|\*(\d+\$)? width using optional (optionally specified) arg |
8965 | \.(\d*|\*(\d+\$)?) precision using optional (optionally specified) arg | |
8966 | [hlqLV] size | |
8967 | [%bcdefginopsux_DFOUX] format (mandatory) | |
8968 | */ | |
8969 | if (EXPECT_NUMBER(q, width)) { | |
8970 | if (*q == '$') { | |
8971 | ++q; | |
8972 | efix = width; | |
8973 | } else { | |
8974 | goto gotwidth; | |
8975 | } | |
8976 | } | |
8977 | ||
fc36a67e | 8978 | /* FLAGS */ |
8979 | ||
46fc3d4c | 8980 | while (*q) { |
8981 | switch (*q) { | |
8982 | case ' ': | |
8983 | case '+': | |
8984 | plus = *q++; | |
8985 | continue; | |
8986 | ||
8987 | case '-': | |
8988 | left = TRUE; | |
8989 | q++; | |
8990 | continue; | |
8991 | ||
8992 | case '0': | |
8993 | fill = *q++; | |
8994 | continue; | |
8995 | ||
8996 | case '#': | |
8997 | alt = TRUE; | |
8998 | q++; | |
8999 | continue; | |
9000 | ||
fc36a67e | 9001 | default: |
9002 | break; | |
9003 | } | |
9004 | break; | |
9005 | } | |
46fc3d4c | 9006 | |
211dfcf1 | 9007 | tryasterisk: |
eb3fce90 | 9008 | if (*q == '*') { |
211dfcf1 HS |
9009 | q++; |
9010 | if (EXPECT_NUMBER(q, ewix)) | |
9011 | if (*q++ != '$') | |
9012 | goto unknown; | |
eb3fce90 | 9013 | asterisk = TRUE; |
211dfcf1 HS |
9014 | } |
9015 | if (*q == 'v') { | |
eb3fce90 | 9016 | q++; |
211dfcf1 HS |
9017 | if (vectorize) |
9018 | goto unknown; | |
9cbac4c7 | 9019 | if ((vectorarg = asterisk)) { |
211dfcf1 HS |
9020 | evix = ewix; |
9021 | ewix = 0; | |
9022 | asterisk = FALSE; | |
9023 | } | |
9024 | vectorize = TRUE; | |
9025 | goto tryasterisk; | |
eb3fce90 JH |
9026 | } |
9027 | ||
211dfcf1 | 9028 | if (!asterisk) |
7a5fa8a2 | 9029 | if( *q == '0' ) |
f3583277 | 9030 | fill = *q++; |
211dfcf1 HS |
9031 | EXPECT_NUMBER(q, width); |
9032 | ||
9033 | if (vectorize) { | |
9034 | if (vectorarg) { | |
9035 | if (args) | |
9036 | vecsv = va_arg(*args, SV*); | |
9037 | else | |
9038 | vecsv = (evix ? evix <= svmax : svix < svmax) ? | |
3a7a539e | 9039 | svargs[evix ? evix-1 : svix++] : &PL_sv_undef; |
245d4a47 | 9040 | dotstr = SvPV_const(vecsv, dotstrlen); |
211dfcf1 | 9041 | if (DO_UTF8(vecsv)) |
2cf2cfc6 | 9042 | is_utf8 = TRUE; |
211dfcf1 HS |
9043 | } |
9044 | if (args) { | |
9045 | vecsv = va_arg(*args, SV*); | |
245d4a47 | 9046 | vecstr = (U8*)SvPV_const(vecsv,veclen); |
2cf2cfc6 | 9047 | vec_utf8 = DO_UTF8(vecsv); |
eb3fce90 | 9048 | } |
211dfcf1 HS |
9049 | else if (efix ? efix <= svmax : svix < svmax) { |
9050 | vecsv = svargs[efix ? efix-1 : svix++]; | |
245d4a47 | 9051 | vecstr = (U8*)SvPV_const(vecsv,veclen); |
2cf2cfc6 | 9052 | vec_utf8 = DO_UTF8(vecsv); |
d7aa5382 | 9053 | /* if this is a version object, we need to return the |
3f7c398e | 9054 | * stringified representation (which the SvPVX_const has |
d7aa5382 JP |
9055 | * already done for us), but not vectorize the args |
9056 | */ | |
9057 | if ( *q == 'd' && sv_derived_from(vecsv,"version") ) | |
9058 | { | |
9059 | q++; /* skip past the rest of the %vd format */ | |
245d4a47 | 9060 | eptr = (const char *) vecstr; |
d7aa5382 JP |
9061 | elen = strlen(eptr); |
9062 | vectorize=FALSE; | |
9063 | goto string; | |
9064 | } | |
211dfcf1 HS |
9065 | } |
9066 | else { | |
9067 | vecstr = (U8*)""; | |
9068 | veclen = 0; | |
9069 | } | |
eb3fce90 | 9070 | } |
fc36a67e | 9071 | |
eb3fce90 | 9072 | if (asterisk) { |
fc36a67e | 9073 | if (args) |
9074 | i = va_arg(*args, int); | |
9075 | else | |
eb3fce90 JH |
9076 | i = (ewix ? ewix <= svmax : svix < svmax) ? |
9077 | SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0; | |
fc36a67e | 9078 | left |= (i < 0); |
9079 | width = (i < 0) ? -i : i; | |
fc36a67e | 9080 | } |
211dfcf1 | 9081 | gotwidth: |
fc36a67e | 9082 | |
9083 | /* PRECISION */ | |
46fc3d4c | 9084 | |
fc36a67e | 9085 | if (*q == '.') { |
9086 | q++; | |
9087 | if (*q == '*') { | |
211dfcf1 | 9088 | q++; |
7b8dd722 HS |
9089 | if (EXPECT_NUMBER(q, epix) && *q++ != '$') |
9090 | goto unknown; | |
9091 | /* XXX: todo, support specified precision parameter */ | |
9092 | if (epix) | |
211dfcf1 | 9093 | goto unknown; |
46fc3d4c | 9094 | if (args) |
9095 | i = va_arg(*args, int); | |
9096 | else | |
eb3fce90 JH |
9097 | i = (ewix ? ewix <= svmax : svix < svmax) |
9098 | ? SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0; | |
fc36a67e | 9099 | precis = (i < 0) ? 0 : i; |
fc36a67e | 9100 | } |
9101 | else { | |
9102 | precis = 0; | |
9103 | while (isDIGIT(*q)) | |
9104 | precis = precis * 10 + (*q++ - '0'); | |
9105 | } | |
9106 | has_precis = TRUE; | |
9107 | } | |
46fc3d4c | 9108 | |
fc36a67e | 9109 | /* SIZE */ |
46fc3d4c | 9110 | |
fc36a67e | 9111 | switch (*q) { |
c623ac67 GS |
9112 | #ifdef WIN32 |
9113 | case 'I': /* Ix, I32x, and I64x */ | |
9114 | # ifdef WIN64 | |
9115 | if (q[1] == '6' && q[2] == '4') { | |
9116 | q += 3; | |
9117 | intsize = 'q'; | |
9118 | break; | |
9119 | } | |
9120 | # endif | |
9121 | if (q[1] == '3' && q[2] == '2') { | |
9122 | q += 3; | |
9123 | break; | |
9124 | } | |
9125 | # ifdef WIN64 | |
9126 | intsize = 'q'; | |
9127 | # endif | |
9128 | q++; | |
9129 | break; | |
9130 | #endif | |
9e5b023a | 9131 | #if defined(HAS_QUAD) || defined(HAS_LONG_DOUBLE) |
6f9bb7fd | 9132 | case 'L': /* Ld */ |
e5c81feb | 9133 | /* FALL THROUGH */ |
e5c81feb | 9134 | #ifdef HAS_QUAD |
6f9bb7fd | 9135 | case 'q': /* qd */ |
9e5b023a | 9136 | #endif |
6f9bb7fd GS |
9137 | intsize = 'q'; |
9138 | q++; | |
9139 | break; | |
9140 | #endif | |
fc36a67e | 9141 | case 'l': |
9e5b023a | 9142 | #if defined(HAS_QUAD) || defined(HAS_LONG_DOUBLE) |
205f51d8 | 9143 | if (*(q + 1) == 'l') { /* lld, llf */ |
fc36a67e | 9144 | intsize = 'q'; |
9145 | q += 2; | |
46fc3d4c | 9146 | break; |
cf2093f6 | 9147 | } |
fc36a67e | 9148 | #endif |
6f9bb7fd | 9149 | /* FALL THROUGH */ |
fc36a67e | 9150 | case 'h': |
cf2093f6 | 9151 | /* FALL THROUGH */ |
fc36a67e | 9152 | case 'V': |
9153 | intsize = *q++; | |
46fc3d4c | 9154 | break; |
9155 | } | |
9156 | ||
fc36a67e | 9157 | /* CONVERSION */ |
9158 | ||
211dfcf1 HS |
9159 | if (*q == '%') { |
9160 | eptr = q++; | |
9161 | elen = 1; | |
9162 | goto string; | |
9163 | } | |
9164 | ||
be75b157 HS |
9165 | if (vectorize) |
9166 | argsv = vecsv; | |
9167 | else if (!args) | |
211dfcf1 HS |
9168 | argsv = (efix ? efix <= svmax : svix < svmax) ? |
9169 | svargs[efix ? efix-1 : svix++] : &PL_sv_undef; | |
9170 | ||
46fc3d4c | 9171 | switch (c = *q++) { |
9172 | ||
9173 | /* STRINGS */ | |
9174 | ||
46fc3d4c | 9175 | case 'c': |
be75b157 | 9176 | uv = (args && !vectorize) ? va_arg(*args, int) : SvIVx(argsv); |
1bd104fb JH |
9177 | if ((uv > 255 || |
9178 | (!UNI_IS_INVARIANT(uv) && SvUTF8(sv))) | |
0064a8a9 | 9179 | && !IN_BYTES) { |
dfe13c55 | 9180 | eptr = (char*)utf8buf; |
9041c2e3 | 9181 | elen = uvchr_to_utf8((U8*)eptr, uv) - utf8buf; |
2cf2cfc6 | 9182 | is_utf8 = TRUE; |
7e2040f0 GS |
9183 | } |
9184 | else { | |
9185 | c = (char)uv; | |
9186 | eptr = &c; | |
9187 | elen = 1; | |
a0ed51b3 | 9188 | } |
46fc3d4c | 9189 | goto string; |
9190 | ||
46fc3d4c | 9191 | case 's': |
be75b157 | 9192 | if (args && !vectorize) { |
fc36a67e | 9193 | eptr = va_arg(*args, char*); |
c635e13b | 9194 | if (eptr) |
1d7c1841 GS |
9195 | #ifdef MACOS_TRADITIONAL |
9196 | /* On MacOS, %#s format is used for Pascal strings */ | |
9197 | if (alt) | |
9198 | elen = *eptr++; | |
9199 | else | |
9200 | #endif | |
c635e13b | 9201 | elen = strlen(eptr); |
9202 | else { | |
27da23d5 | 9203 | eptr = (char *)nullstr; |
c635e13b | 9204 | elen = sizeof nullstr - 1; |
9205 | } | |
46fc3d4c | 9206 | } |
211dfcf1 | 9207 | else { |
4d84ee25 | 9208 | eptr = SvPVx_const(argsv, elen); |
7e2040f0 | 9209 | if (DO_UTF8(argsv)) { |
a0ed51b3 LW |
9210 | if (has_precis && precis < elen) { |
9211 | I32 p = precis; | |
7e2040f0 | 9212 | sv_pos_u2b(argsv, &p, 0); /* sticks at end */ |
a0ed51b3 LW |
9213 | precis = p; |
9214 | } | |
9215 | if (width) { /* fudge width (can't fudge elen) */ | |
7e2040f0 | 9216 | width += elen - sv_len_utf8(argsv); |
a0ed51b3 | 9217 | } |
2cf2cfc6 | 9218 | is_utf8 = TRUE; |
a0ed51b3 LW |
9219 | } |
9220 | } | |
fc36a67e | 9221 | |
46fc3d4c | 9222 | string: |
b22c7a20 | 9223 | vectorize = FALSE; |
46fc3d4c | 9224 | if (has_precis && elen > precis) |
9225 | elen = precis; | |
9226 | break; | |
9227 | ||
9228 | /* INTEGERS */ | |
9229 | ||
fc36a67e | 9230 | case 'p': |
0dbb1585 | 9231 | if (left && args) { /* SVf */ |
5df617be | 9232 | left = FALSE; |
0dbb1585 AL |
9233 | if (width) { |
9234 | precis = width; | |
9235 | has_precis = TRUE; | |
9236 | width = 0; | |
9237 | } | |
9238 | if (vectorize) | |
9239 | goto unknown; | |
9240 | argsv = va_arg(*args, SV*); | |
4d84ee25 | 9241 | eptr = SvPVx_const(argsv, elen); |
0dbb1585 AL |
9242 | if (DO_UTF8(argsv)) |
9243 | is_utf8 = TRUE; | |
9244 | goto string; | |
5df617be | 9245 | } |
be75b157 | 9246 | if (alt || vectorize) |
c2e66d9e | 9247 | goto unknown; |
211dfcf1 | 9248 | uv = PTR2UV(args ? va_arg(*args, void*) : argsv); |
fc36a67e | 9249 | base = 16; |
9250 | goto integer; | |
9251 | ||
46fc3d4c | 9252 | case 'D': |
29fe7a80 | 9253 | #ifdef IV_IS_QUAD |
22f3ae8c | 9254 | intsize = 'q'; |
29fe7a80 | 9255 | #else |
46fc3d4c | 9256 | intsize = 'l'; |
29fe7a80 | 9257 | #endif |
46fc3d4c | 9258 | /* FALL THROUGH */ |
9259 | case 'd': | |
9260 | case 'i': | |
b22c7a20 | 9261 | if (vectorize) { |
ba210ebe | 9262 | STRLEN ulen; |
211dfcf1 HS |
9263 | if (!veclen) |
9264 | continue; | |
2cf2cfc6 A |
9265 | if (vec_utf8) |
9266 | uv = utf8n_to_uvchr(vecstr, veclen, &ulen, | |
9267 | UTF8_ALLOW_ANYUV); | |
b22c7a20 | 9268 | else { |
e83d50c9 | 9269 | uv = *vecstr; |
b22c7a20 GS |
9270 | ulen = 1; |
9271 | } | |
9272 | vecstr += ulen; | |
9273 | veclen -= ulen; | |
e83d50c9 JP |
9274 | if (plus) |
9275 | esignbuf[esignlen++] = plus; | |
b22c7a20 GS |
9276 | } |
9277 | else if (args) { | |
46fc3d4c | 9278 | switch (intsize) { |
9279 | case 'h': iv = (short)va_arg(*args, int); break; | |
46fc3d4c | 9280 | case 'l': iv = va_arg(*args, long); break; |
fc36a67e | 9281 | case 'V': iv = va_arg(*args, IV); break; |
b10c0dba | 9282 | default: iv = va_arg(*args, int); break; |
cf2093f6 JH |
9283 | #ifdef HAS_QUAD |
9284 | case 'q': iv = va_arg(*args, Quad_t); break; | |
9285 | #endif | |
46fc3d4c | 9286 | } |
9287 | } | |
9288 | else { | |
b10c0dba | 9289 | IV tiv = SvIVx(argsv); /* work around GCC bug #13488 */ |
46fc3d4c | 9290 | switch (intsize) { |
b10c0dba MHM |
9291 | case 'h': iv = (short)tiv; break; |
9292 | case 'l': iv = (long)tiv; break; | |
9293 | case 'V': | |
9294 | default: iv = tiv; break; | |
cf2093f6 | 9295 | #ifdef HAS_QUAD |
b10c0dba | 9296 | case 'q': iv = (Quad_t)tiv; break; |
cf2093f6 | 9297 | #endif |
46fc3d4c | 9298 | } |
9299 | } | |
e83d50c9 JP |
9300 | if ( !vectorize ) /* we already set uv above */ |
9301 | { | |
9302 | if (iv >= 0) { | |
9303 | uv = iv; | |
9304 | if (plus) | |
9305 | esignbuf[esignlen++] = plus; | |
9306 | } | |
9307 | else { | |
9308 | uv = -iv; | |
9309 | esignbuf[esignlen++] = '-'; | |
9310 | } | |
46fc3d4c | 9311 | } |
9312 | base = 10; | |
9313 | goto integer; | |
9314 | ||
fc36a67e | 9315 | case 'U': |
29fe7a80 | 9316 | #ifdef IV_IS_QUAD |
22f3ae8c | 9317 | intsize = 'q'; |
29fe7a80 | 9318 | #else |
fc36a67e | 9319 | intsize = 'l'; |
29fe7a80 | 9320 | #endif |
fc36a67e | 9321 | /* FALL THROUGH */ |
9322 | case 'u': | |
9323 | base = 10; | |
9324 | goto uns_integer; | |
9325 | ||
4f19785b WSI |
9326 | case 'b': |
9327 | base = 2; | |
9328 | goto uns_integer; | |
9329 | ||
46fc3d4c | 9330 | case 'O': |
29fe7a80 | 9331 | #ifdef IV_IS_QUAD |
22f3ae8c | 9332 | intsize = 'q'; |
29fe7a80 | 9333 | #else |
46fc3d4c | 9334 | intsize = 'l'; |
29fe7a80 | 9335 | #endif |
46fc3d4c | 9336 | /* FALL THROUGH */ |
9337 | case 'o': | |
9338 | base = 8; | |
9339 | goto uns_integer; | |
9340 | ||
9341 | case 'X': | |
46fc3d4c | 9342 | case 'x': |
9343 | base = 16; | |
46fc3d4c | 9344 | |
9345 | uns_integer: | |
b22c7a20 | 9346 | if (vectorize) { |
ba210ebe | 9347 | STRLEN ulen; |
b22c7a20 | 9348 | vector: |
211dfcf1 HS |
9349 | if (!veclen) |
9350 | continue; | |
2cf2cfc6 A |
9351 | if (vec_utf8) |
9352 | uv = utf8n_to_uvchr(vecstr, veclen, &ulen, | |
9353 | UTF8_ALLOW_ANYUV); | |
b22c7a20 | 9354 | else { |
a05b299f | 9355 | uv = *vecstr; |
b22c7a20 GS |
9356 | ulen = 1; |
9357 | } | |
9358 | vecstr += ulen; | |
9359 | veclen -= ulen; | |
9360 | } | |
9361 | else if (args) { | |
46fc3d4c | 9362 | switch (intsize) { |
9363 | case 'h': uv = (unsigned short)va_arg(*args, unsigned); break; | |
46fc3d4c | 9364 | case 'l': uv = va_arg(*args, unsigned long); break; |
fc36a67e | 9365 | case 'V': uv = va_arg(*args, UV); break; |
b10c0dba | 9366 | default: uv = va_arg(*args, unsigned); break; |
cf2093f6 | 9367 | #ifdef HAS_QUAD |
9e3321a5 | 9368 | case 'q': uv = va_arg(*args, Uquad_t); break; |
cf2093f6 | 9369 | #endif |
46fc3d4c | 9370 | } |
9371 | } | |
9372 | else { | |
b10c0dba | 9373 | UV tuv = SvUVx(argsv); /* work around GCC bug #13488 */ |
46fc3d4c | 9374 | switch (intsize) { |
b10c0dba MHM |
9375 | case 'h': uv = (unsigned short)tuv; break; |
9376 | case 'l': uv = (unsigned long)tuv; break; | |
9377 | case 'V': | |
9378 | default: uv = tuv; break; | |
cf2093f6 | 9379 | #ifdef HAS_QUAD |
b10c0dba | 9380 | case 'q': uv = (Uquad_t)tuv; break; |
cf2093f6 | 9381 | #endif |
46fc3d4c | 9382 | } |
9383 | } | |
9384 | ||
9385 | integer: | |
4d84ee25 NC |
9386 | { |
9387 | char *ptr = ebuf + sizeof ebuf; | |
9388 | switch (base) { | |
9389 | unsigned dig; | |
9390 | case 16: | |
9391 | if (!uv) | |
9392 | alt = FALSE; | |
9393 | p = (char*)((c == 'X') | |
9394 | ? "0123456789ABCDEF" : "0123456789abcdef"); | |
9395 | do { | |
9396 | dig = uv & 15; | |
9397 | *--ptr = p[dig]; | |
9398 | } while (uv >>= 4); | |
9399 | if (alt) { | |
9400 | esignbuf[esignlen++] = '0'; | |
9401 | esignbuf[esignlen++] = c; /* 'x' or 'X' */ | |
9402 | } | |
9403 | break; | |
9404 | case 8: | |
9405 | do { | |
9406 | dig = uv & 7; | |
9407 | *--ptr = '0' + dig; | |
9408 | } while (uv >>= 3); | |
9409 | if (alt && *ptr != '0') | |
9410 | *--ptr = '0'; | |
9411 | break; | |
9412 | case 2: | |
9413 | do { | |
9414 | dig = uv & 1; | |
9415 | *--ptr = '0' + dig; | |
9416 | } while (uv >>= 1); | |
9417 | if (alt) { | |
9418 | esignbuf[esignlen++] = '0'; | |
9419 | esignbuf[esignlen++] = 'b'; | |
9420 | } | |
9421 | break; | |
9422 | default: /* it had better be ten or less */ | |
9423 | do { | |
9424 | dig = uv % base; | |
9425 | *--ptr = '0' + dig; | |
9426 | } while (uv /= base); | |
9427 | break; | |
46fc3d4c | 9428 | } |
4d84ee25 NC |
9429 | elen = (ebuf + sizeof ebuf) - ptr; |
9430 | eptr = ptr; | |
9431 | if (has_precis) { | |
9432 | if (precis > elen) | |
9433 | zeros = precis - elen; | |
9434 | else if (precis == 0 && elen == 1 && *eptr == '0') | |
9435 | elen = 0; | |
eda88b6d | 9436 | } |
c10ed8b9 | 9437 | } |
46fc3d4c | 9438 | break; |
9439 | ||
9440 | /* FLOATING POINT */ | |
9441 | ||
fc36a67e | 9442 | case 'F': |
9443 | c = 'f'; /* maybe %F isn't supported here */ | |
9444 | /* FALL THROUGH */ | |
46fc3d4c | 9445 | case 'e': case 'E': |
fc36a67e | 9446 | case 'f': |
46fc3d4c | 9447 | case 'g': case 'G': |
9448 | ||
9449 | /* This is evil, but floating point is even more evil */ | |
9450 | ||
9e5b023a JH |
9451 | /* for SV-style calling, we can only get NV |
9452 | for C-style calling, we assume %f is double; | |
9453 | for simplicity we allow any of %Lf, %llf, %qf for long double | |
9454 | */ | |
9455 | switch (intsize) { | |
9456 | case 'V': | |
9457 | #if defined(USE_LONG_DOUBLE) | |
9458 | intsize = 'q'; | |
9459 | #endif | |
9460 | break; | |
8a2e3f14 | 9461 | /* [perl #20339] - we should accept and ignore %lf rather than die */ |
00e17364 HS |
9462 | case 'l': |
9463 | /* FALL THROUGH */ | |
9e5b023a JH |
9464 | default: |
9465 | #if defined(USE_LONG_DOUBLE) | |
9466 | intsize = args ? 0 : 'q'; | |
9467 | #endif | |
9468 | break; | |
9469 | case 'q': | |
9470 | #if defined(HAS_LONG_DOUBLE) | |
9471 | break; | |
9472 | #else | |
9473 | /* FALL THROUGH */ | |
9474 | #endif | |
9475 | case 'h': | |
9e5b023a JH |
9476 | goto unknown; |
9477 | } | |
9478 | ||
9479 | /* now we need (long double) if intsize == 'q', else (double) */ | |
be75b157 | 9480 | nv = (args && !vectorize) ? |
35fff930 JH |
9481 | #if LONG_DOUBLESIZE > DOUBLESIZE |
9482 | intsize == 'q' ? | |
205f51d8 AS |
9483 | va_arg(*args, long double) : |
9484 | va_arg(*args, double) | |
35fff930 | 9485 | #else |
205f51d8 | 9486 | va_arg(*args, double) |
35fff930 | 9487 | #endif |
9e5b023a | 9488 | : SvNVx(argsv); |
fc36a67e | 9489 | |
9490 | need = 0; | |
be75b157 | 9491 | vectorize = FALSE; |
fc36a67e | 9492 | if (c != 'e' && c != 'E') { |
9493 | i = PERL_INT_MIN; | |
9e5b023a JH |
9494 | /* FIXME: if HAS_LONG_DOUBLE but not USE_LONG_DOUBLE this |
9495 | will cast our (long double) to (double) */ | |
73b309ea | 9496 | (void)Perl_frexp(nv, &i); |
fc36a67e | 9497 | if (i == PERL_INT_MIN) |
cea2e8a9 | 9498 | Perl_die(aTHX_ "panic: frexp"); |
c635e13b | 9499 | if (i > 0) |
fc36a67e | 9500 | need = BIT_DIGITS(i); |
9501 | } | |
9502 | need += has_precis ? precis : 6; /* known default */ | |
20f6aaab | 9503 | |
fc36a67e | 9504 | if (need < width) |
9505 | need = width; | |
9506 | ||
20f6aaab AS |
9507 | #ifdef HAS_LDBL_SPRINTF_BUG |
9508 | /* This is to try to fix a bug with irix/nonstop-ux/powerux and | |
205f51d8 AS |
9509 | with sfio - Allen <allens@cpan.org> */ |
9510 | ||
9511 | # ifdef DBL_MAX | |
9512 | # define MY_DBL_MAX DBL_MAX | |
9513 | # else /* XXX guessing! HUGE_VAL may be defined as infinity, so not using */ | |
9514 | # if DOUBLESIZE >= 8 | |
9515 | # define MY_DBL_MAX 1.7976931348623157E+308L | |
9516 | # else | |
9517 | # define MY_DBL_MAX 3.40282347E+38L | |
9518 | # endif | |
9519 | # endif | |
9520 | ||
9521 | # ifdef HAS_LDBL_SPRINTF_BUG_LESS1 /* only between -1L & 1L - Allen */ | |
9522 | # define MY_DBL_MAX_BUG 1L | |
20f6aaab | 9523 | # else |
205f51d8 | 9524 | # define MY_DBL_MAX_BUG MY_DBL_MAX |
20f6aaab | 9525 | # endif |
20f6aaab | 9526 | |
205f51d8 AS |
9527 | # ifdef DBL_MIN |
9528 | # define MY_DBL_MIN DBL_MIN | |
9529 | # else /* XXX guessing! -Allen */ | |
9530 | # if DOUBLESIZE >= 8 | |
9531 | # define MY_DBL_MIN 2.2250738585072014E-308L | |
9532 | # else | |
9533 | # define MY_DBL_MIN 1.17549435E-38L | |
9534 | # endif | |
9535 | # endif | |
20f6aaab | 9536 | |
205f51d8 AS |
9537 | if ((intsize == 'q') && (c == 'f') && |
9538 | ((nv < MY_DBL_MAX_BUG) && (nv > -MY_DBL_MAX_BUG)) && | |
9539 | (need < DBL_DIG)) { | |
9540 | /* it's going to be short enough that | |
9541 | * long double precision is not needed */ | |
9542 | ||
9543 | if ((nv <= 0L) && (nv >= -0L)) | |
9544 | fix_ldbl_sprintf_bug = TRUE; /* 0 is 0 - easiest */ | |
9545 | else { | |
9546 | /* would use Perl_fp_class as a double-check but not | |
9547 | * functional on IRIX - see perl.h comments */ | |
9548 | ||
9549 | if ((nv >= MY_DBL_MIN) || (nv <= -MY_DBL_MIN)) { | |
9550 | /* It's within the range that a double can represent */ | |
9551 | #if defined(DBL_MAX) && !defined(DBL_MIN) | |
9552 | if ((nv >= ((long double)1/DBL_MAX)) || | |
9553 | (nv <= (-(long double)1/DBL_MAX))) | |
20f6aaab | 9554 | #endif |
205f51d8 | 9555 | fix_ldbl_sprintf_bug = TRUE; |
20f6aaab | 9556 | } |
205f51d8 AS |
9557 | } |
9558 | if (fix_ldbl_sprintf_bug == TRUE) { | |
9559 | double temp; | |
9560 | ||
9561 | intsize = 0; | |
9562 | temp = (double)nv; | |
9563 | nv = (NV)temp; | |
9564 | } | |
20f6aaab | 9565 | } |
205f51d8 AS |
9566 | |
9567 | # undef MY_DBL_MAX | |
9568 | # undef MY_DBL_MAX_BUG | |
9569 | # undef MY_DBL_MIN | |
9570 | ||
20f6aaab AS |
9571 | #endif /* HAS_LDBL_SPRINTF_BUG */ |
9572 | ||
46fc3d4c | 9573 | need += 20; /* fudge factor */ |
80252599 GS |
9574 | if (PL_efloatsize < need) { |
9575 | Safefree(PL_efloatbuf); | |
9576 | PL_efloatsize = need + 20; /* more fudge */ | |
9577 | New(906, PL_efloatbuf, PL_efloatsize, char); | |
7d5ea4e7 | 9578 | PL_efloatbuf[0] = '\0'; |
46fc3d4c | 9579 | } |
9580 | ||
4151a5fe IZ |
9581 | if ( !(width || left || plus || alt) && fill != '0' |
9582 | && has_precis && intsize != 'q' ) { /* Shortcuts */ | |
2873255c NC |
9583 | /* See earlier comment about buggy Gconvert when digits, |
9584 | aka precis is 0 */ | |
9585 | if ( c == 'g' && precis) { | |
2e59c212 | 9586 | Gconvert((NV)nv, (int)precis, 0, PL_efloatbuf); |
4151a5fe IZ |
9587 | if (*PL_efloatbuf) /* May return an empty string for digits==0 */ |
9588 | goto float_converted; | |
9589 | } else if ( c == 'f' && !precis) { | |
9590 | if ((eptr = F0convert(nv, ebuf + sizeof ebuf, &elen))) | |
9591 | break; | |
9592 | } | |
9593 | } | |
4d84ee25 NC |
9594 | { |
9595 | char *ptr = ebuf + sizeof ebuf; | |
9596 | *--ptr = '\0'; | |
9597 | *--ptr = c; | |
9598 | /* FIXME: what to do if HAS_LONG_DOUBLE but not PERL_PRIfldbl? */ | |
9e5b023a | 9599 | #if defined(HAS_LONG_DOUBLE) && defined(PERL_PRIfldbl) |
4d84ee25 NC |
9600 | if (intsize == 'q') { |
9601 | /* Copy the one or more characters in a long double | |
9602 | * format before the 'base' ([efgEFG]) character to | |
9603 | * the format string. */ | |
9604 | static char const prifldbl[] = PERL_PRIfldbl; | |
9605 | char const *p = prifldbl + sizeof(prifldbl) - 3; | |
9606 | while (p >= prifldbl) { *--ptr = *p--; } | |
9607 | } | |
65202027 | 9608 | #endif |
4d84ee25 NC |
9609 | if (has_precis) { |
9610 | base = precis; | |
9611 | do { *--ptr = '0' + (base % 10); } while (base /= 10); | |
9612 | *--ptr = '.'; | |
9613 | } | |
9614 | if (width) { | |
9615 | base = width; | |
9616 | do { *--ptr = '0' + (base % 10); } while (base /= 10); | |
9617 | } | |
9618 | if (fill == '0') | |
9619 | *--ptr = fill; | |
9620 | if (left) | |
9621 | *--ptr = '-'; | |
9622 | if (plus) | |
9623 | *--ptr = plus; | |
9624 | if (alt) | |
9625 | *--ptr = '#'; | |
9626 | *--ptr = '%'; | |
9627 | ||
9628 | /* No taint. Otherwise we are in the strange situation | |
9629 | * where printf() taints but print($float) doesn't. | |
9630 | * --jhi */ | |
9e5b023a | 9631 | #if defined(HAS_LONG_DOUBLE) |
4d84ee25 NC |
9632 | if (intsize == 'q') |
9633 | (void)sprintf(PL_efloatbuf, ptr, nv); | |
9634 | else | |
9635 | (void)sprintf(PL_efloatbuf, ptr, (double)nv); | |
9e5b023a | 9636 | #else |
4d84ee25 | 9637 | (void)sprintf(PL_efloatbuf, ptr, nv); |
9e5b023a | 9638 | #endif |
4d84ee25 | 9639 | } |
4151a5fe | 9640 | float_converted: |
80252599 GS |
9641 | eptr = PL_efloatbuf; |
9642 | elen = strlen(PL_efloatbuf); | |
46fc3d4c | 9643 | break; |
9644 | ||
fc36a67e | 9645 | /* SPECIAL */ |
9646 | ||
9647 | case 'n': | |
9648 | i = SvCUR(sv) - origlen; | |
be75b157 | 9649 | if (args && !vectorize) { |
c635e13b | 9650 | switch (intsize) { |
9651 | case 'h': *(va_arg(*args, short*)) = i; break; | |
9652 | default: *(va_arg(*args, int*)) = i; break; | |
9653 | case 'l': *(va_arg(*args, long*)) = i; break; | |
9654 | case 'V': *(va_arg(*args, IV*)) = i; break; | |
cf2093f6 JH |
9655 | #ifdef HAS_QUAD |
9656 | case 'q': *(va_arg(*args, Quad_t*)) = i; break; | |
9657 | #endif | |
c635e13b | 9658 | } |
fc36a67e | 9659 | } |
9dd79c3f | 9660 | else |
211dfcf1 | 9661 | sv_setuv_mg(argsv, (UV)i); |
be75b157 | 9662 | vectorize = FALSE; |
fc36a67e | 9663 | continue; /* not "break" */ |
9664 | ||
9665 | /* UNKNOWN */ | |
9666 | ||
46fc3d4c | 9667 | default: |
fc36a67e | 9668 | unknown: |
599cee73 | 9669 | if (!args && ckWARN(WARN_PRINTF) && |
533c011a | 9670 | (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF)) { |
c635e13b | 9671 | SV *msg = sv_newmortal(); |
35c1215d NC |
9672 | Perl_sv_setpvf(aTHX_ msg, "Invalid conversion in %sprintf: ", |
9673 | (PL_op->op_type == OP_PRTF) ? "" : "s"); | |
0f4b6630 | 9674 | if (c) { |
0f4b6630 | 9675 | if (isPRINT(c)) |
1c846c1f | 9676 | Perl_sv_catpvf(aTHX_ msg, |
0f4b6630 JH |
9677 | "\"%%%c\"", c & 0xFF); |
9678 | else | |
9679 | Perl_sv_catpvf(aTHX_ msg, | |
57def98f | 9680 | "\"%%\\%03"UVof"\"", |
0f4b6630 | 9681 | (UV)c & 0xFF); |
0f4b6630 | 9682 | } else |
c635e13b | 9683 | sv_catpv(msg, "end of string"); |
9014280d | 9684 | Perl_warner(aTHX_ packWARN(WARN_PRINTF), "%"SVf, msg); /* yes, this is reentrant */ |
c635e13b | 9685 | } |
fb73857a | 9686 | |
9687 | /* output mangled stuff ... */ | |
9688 | if (c == '\0') | |
9689 | --q; | |
46fc3d4c | 9690 | eptr = p; |
9691 | elen = q - p; | |
fb73857a | 9692 | |
9693 | /* ... right here, because formatting flags should not apply */ | |
9694 | SvGROW(sv, SvCUR(sv) + elen + 1); | |
9695 | p = SvEND(sv); | |
4459522c | 9696 | Copy(eptr, p, elen, char); |
fb73857a | 9697 | p += elen; |
9698 | *p = '\0'; | |
3f7c398e | 9699 | SvCUR_set(sv, p - SvPVX_const(sv)); |
58e33a90 | 9700 | svix = osvix; |
fb73857a | 9701 | continue; /* not "break" */ |
46fc3d4c | 9702 | } |
9703 | ||
6c94ec8b HS |
9704 | /* calculate width before utf8_upgrade changes it */ |
9705 | have = esignlen + zeros + elen; | |
9706 | ||
d2876be5 JH |
9707 | if (is_utf8 != has_utf8) { |
9708 | if (is_utf8) { | |
9709 | if (SvCUR(sv)) | |
9710 | sv_utf8_upgrade(sv); | |
9711 | } | |
9712 | else { | |
53c1dcc0 | 9713 | SV * const nsv = sv_2mortal(newSVpvn(eptr, elen)); |
d2876be5 | 9714 | sv_utf8_upgrade(nsv); |
93524f2b | 9715 | eptr = SvPVX_const(nsv); |
d2876be5 JH |
9716 | elen = SvCUR(nsv); |
9717 | } | |
9718 | SvGROW(sv, SvCUR(sv) + elen + 1); | |
9719 | p = SvEND(sv); | |
9720 | *p = '\0'; | |
9721 | } | |
6af65485 | 9722 | |
46fc3d4c | 9723 | need = (have > width ? have : width); |
9724 | gap = need - have; | |
9725 | ||
b22c7a20 | 9726 | SvGROW(sv, SvCUR(sv) + need + dotstrlen + 1); |
46fc3d4c | 9727 | p = SvEND(sv); |
9728 | if (esignlen && fill == '0') { | |
53c1dcc0 | 9729 | int i; |
eb160463 | 9730 | for (i = 0; i < (int)esignlen; i++) |
46fc3d4c | 9731 | *p++ = esignbuf[i]; |
9732 | } | |
9733 | if (gap && !left) { | |
9734 | memset(p, fill, gap); | |
9735 | p += gap; | |
9736 | } | |
9737 | if (esignlen && fill != '0') { | |
53c1dcc0 | 9738 | int i; |
eb160463 | 9739 | for (i = 0; i < (int)esignlen; i++) |
46fc3d4c | 9740 | *p++ = esignbuf[i]; |
9741 | } | |
fc36a67e | 9742 | if (zeros) { |
53c1dcc0 | 9743 | int i; |
fc36a67e | 9744 | for (i = zeros; i; i--) |
9745 | *p++ = '0'; | |
9746 | } | |
46fc3d4c | 9747 | if (elen) { |
4459522c | 9748 | Copy(eptr, p, elen, char); |
46fc3d4c | 9749 | p += elen; |
9750 | } | |
9751 | if (gap && left) { | |
9752 | memset(p, ' ', gap); | |
9753 | p += gap; | |
9754 | } | |
b22c7a20 GS |
9755 | if (vectorize) { |
9756 | if (veclen) { | |
4459522c | 9757 | Copy(dotstr, p, dotstrlen, char); |
b22c7a20 GS |
9758 | p += dotstrlen; |
9759 | } | |
9760 | else | |
9761 | vectorize = FALSE; /* done iterating over vecstr */ | |
9762 | } | |
2cf2cfc6 A |
9763 | if (is_utf8) |
9764 | has_utf8 = TRUE; | |
9765 | if (has_utf8) | |
7e2040f0 | 9766 | SvUTF8_on(sv); |
46fc3d4c | 9767 | *p = '\0'; |
3f7c398e | 9768 | SvCUR_set(sv, p - SvPVX_const(sv)); |
b22c7a20 GS |
9769 | if (vectorize) { |
9770 | esignlen = 0; | |
9771 | goto vector; | |
9772 | } | |
46fc3d4c | 9773 | } |
9774 | } | |
51371543 | 9775 | |
645c22ef DM |
9776 | /* ========================================================================= |
9777 | ||
9778 | =head1 Cloning an interpreter | |
9779 | ||
9780 | All the macros and functions in this section are for the private use of | |
9781 | the main function, perl_clone(). | |
9782 | ||
9783 | The foo_dup() functions make an exact copy of an existing foo thinngy. | |
9784 | During the course of a cloning, a hash table is used to map old addresses | |
9785 | to new addresses. The table is created and manipulated with the | |
9786 | ptr_table_* functions. | |
9787 | ||
9788 | =cut | |
9789 | ||
9790 | ============================================================================*/ | |
9791 | ||
9792 | ||
1d7c1841 GS |
9793 | #if defined(USE_ITHREADS) |
9794 | ||
1d7c1841 GS |
9795 | #ifndef GpREFCNT_inc |
9796 | # define GpREFCNT_inc(gp) ((gp) ? (++(gp)->gp_refcnt, (gp)) : (GP*)NULL) | |
9797 | #endif | |
9798 | ||
9799 | ||
d2d73c3e AB |
9800 | #define sv_dup_inc(s,t) SvREFCNT_inc(sv_dup(s,t)) |
9801 | #define av_dup(s,t) (AV*)sv_dup((SV*)s,t) | |
9802 | #define av_dup_inc(s,t) (AV*)SvREFCNT_inc(sv_dup((SV*)s,t)) | |
9803 | #define hv_dup(s,t) (HV*)sv_dup((SV*)s,t) | |
9804 | #define hv_dup_inc(s,t) (HV*)SvREFCNT_inc(sv_dup((SV*)s,t)) | |
9805 | #define cv_dup(s,t) (CV*)sv_dup((SV*)s,t) | |
9806 | #define cv_dup_inc(s,t) (CV*)SvREFCNT_inc(sv_dup((SV*)s,t)) | |
9807 | #define io_dup(s,t) (IO*)sv_dup((SV*)s,t) | |
9808 | #define io_dup_inc(s,t) (IO*)SvREFCNT_inc(sv_dup((SV*)s,t)) | |
9809 | #define gv_dup(s,t) (GV*)sv_dup((SV*)s,t) | |
9810 | #define gv_dup_inc(s,t) (GV*)SvREFCNT_inc(sv_dup((SV*)s,t)) | |
1d7c1841 GS |
9811 | #define SAVEPV(p) (p ? savepv(p) : Nullch) |
9812 | #define SAVEPVN(p,n) (p ? savepvn(p,n) : Nullch) | |
8cf8f3d1 | 9813 | |
d2d73c3e | 9814 | |
d2f185dc AMS |
9815 | /* Duplicate a regexp. Required reading: pregcomp() and pregfree() in |
9816 | regcomp.c. AMS 20010712 */ | |
645c22ef | 9817 | |
1d7c1841 | 9818 | REGEXP * |
53c1dcc0 | 9819 | Perl_re_dup(pTHX_ const REGEXP *r, CLONE_PARAMS *param) |
1d7c1841 | 9820 | { |
27da23d5 | 9821 | dVAR; |
d2f185dc AMS |
9822 | REGEXP *ret; |
9823 | int i, len, npar; | |
9824 | struct reg_substr_datum *s; | |
9825 | ||
9826 | if (!r) | |
9827 | return (REGEXP *)NULL; | |
9828 | ||
9829 | if ((ret = (REGEXP *)ptr_table_fetch(PL_ptr_table, r))) | |
9830 | return ret; | |
9831 | ||
9832 | len = r->offsets[0]; | |
9833 | npar = r->nparens+1; | |
9834 | ||
9835 | Newc(0, ret, sizeof(regexp) + (len+1)*sizeof(regnode), char, regexp); | |
9836 | Copy(r->program, ret->program, len+1, regnode); | |
9837 | ||
9838 | New(0, ret->startp, npar, I32); | |
9839 | Copy(r->startp, ret->startp, npar, I32); | |
9840 | New(0, ret->endp, npar, I32); | |
9841 | Copy(r->startp, ret->startp, npar, I32); | |
9842 | ||
d2f185dc AMS |
9843 | New(0, ret->substrs, 1, struct reg_substr_data); |
9844 | for (s = ret->substrs->data, i = 0; i < 3; i++, s++) { | |
9845 | s->min_offset = r->substrs->data[i].min_offset; | |
9846 | s->max_offset = r->substrs->data[i].max_offset; | |
9847 | s->substr = sv_dup_inc(r->substrs->data[i].substr, param); | |
33b8afdf | 9848 | s->utf8_substr = sv_dup_inc(r->substrs->data[i].utf8_substr, param); |
d2f185dc AMS |
9849 | } |
9850 | ||
70612e96 | 9851 | ret->regstclass = NULL; |
d2f185dc AMS |
9852 | if (r->data) { |
9853 | struct reg_data *d; | |
e1ec3a88 | 9854 | const int count = r->data->count; |
53c1dcc0 | 9855 | int i; |
d2f185dc AMS |
9856 | |
9857 | Newc(0, d, sizeof(struct reg_data) + count*sizeof(void *), | |
9858 | char, struct reg_data); | |
9859 | New(0, d->what, count, U8); | |
9860 | ||
9861 | d->count = count; | |
9862 | for (i = 0; i < count; i++) { | |
9863 | d->what[i] = r->data->what[i]; | |
9864 | switch (d->what[i]) { | |
a3621e74 YO |
9865 | /* legal options are one of: sfpont |
9866 | see also regcomp.h and pregfree() */ | |
d2f185dc AMS |
9867 | case 's': |
9868 | d->data[i] = sv_dup_inc((SV *)r->data->data[i], param); | |
9869 | break; | |
9870 | case 'p': | |
9871 | d->data[i] = av_dup_inc((AV *)r->data->data[i], param); | |
9872 | break; | |
9873 | case 'f': | |
9874 | /* This is cheating. */ | |
9875 | New(0, d->data[i], 1, struct regnode_charclass_class); | |
9876 | StructCopy(r->data->data[i], d->data[i], | |
9877 | struct regnode_charclass_class); | |
70612e96 | 9878 | ret->regstclass = (regnode*)d->data[i]; |
d2f185dc AMS |
9879 | break; |
9880 | case 'o': | |
33773810 AMS |
9881 | /* Compiled op trees are readonly, and can thus be |
9882 | shared without duplication. */ | |
b34c0dd4 | 9883 | OP_REFCNT_LOCK; |
9b978d73 | 9884 | d->data[i] = (void*)OpREFCNT_inc((OP*)r->data->data[i]); |
b34c0dd4 | 9885 | OP_REFCNT_UNLOCK; |
9b978d73 | 9886 | break; |
d2f185dc AMS |
9887 | case 'n': |
9888 | d->data[i] = r->data->data[i]; | |
9889 | break; | |
a3621e74 YO |
9890 | case 't': |
9891 | d->data[i] = r->data->data[i]; | |
9892 | OP_REFCNT_LOCK; | |
9893 | ((reg_trie_data*)d->data[i])->refcount++; | |
9894 | OP_REFCNT_UNLOCK; | |
9895 | break; | |
9896 | default: | |
9897 | Perl_croak(aTHX_ "panic: re_dup unknown data code '%c'", r->data->what[i]); | |
d2f185dc AMS |
9898 | } |
9899 | } | |
9900 | ||
9901 | ret->data = d; | |
9902 | } | |
9903 | else | |
9904 | ret->data = NULL; | |
9905 | ||
9906 | New(0, ret->offsets, 2*len+1, U32); | |
9907 | Copy(r->offsets, ret->offsets, 2*len+1, U32); | |
9908 | ||
e01c5899 | 9909 | ret->precomp = SAVEPVN(r->precomp, r->prelen); |
d2f185dc AMS |
9910 | ret->refcnt = r->refcnt; |
9911 | ret->minlen = r->minlen; | |
9912 | ret->prelen = r->prelen; | |
9913 | ret->nparens = r->nparens; | |
9914 | ret->lastparen = r->lastparen; | |
9915 | ret->lastcloseparen = r->lastcloseparen; | |
9916 | ret->reganch = r->reganch; | |
9917 | ||
70612e96 RG |
9918 | ret->sublen = r->sublen; |
9919 | ||
9920 | if (RX_MATCH_COPIED(ret)) | |
e01c5899 | 9921 | ret->subbeg = SAVEPVN(r->subbeg, r->sublen); |
70612e96 RG |
9922 | else |
9923 | ret->subbeg = Nullch; | |
f8c7b90f | 9924 | #ifdef PERL_OLD_COPY_ON_WRITE |
9a26048b NC |
9925 | ret->saved_copy = Nullsv; |
9926 | #endif | |
70612e96 | 9927 | |
d2f185dc AMS |
9928 | ptr_table_store(PL_ptr_table, r, ret); |
9929 | return ret; | |
1d7c1841 GS |
9930 | } |
9931 | ||
d2d73c3e | 9932 | /* duplicate a file handle */ |
645c22ef | 9933 | |
1d7c1841 | 9934 | PerlIO * |
a8fc9800 | 9935 | Perl_fp_dup(pTHX_ PerlIO *fp, char type, CLONE_PARAMS *param) |
1d7c1841 GS |
9936 | { |
9937 | PerlIO *ret; | |
53c1dcc0 AL |
9938 | |
9939 | PERL_UNUSED_ARG(type); | |
73d840c0 | 9940 | |
1d7c1841 GS |
9941 | if (!fp) |
9942 | return (PerlIO*)NULL; | |
9943 | ||
9944 | /* look for it in the table first */ | |
9945 | ret = (PerlIO*)ptr_table_fetch(PL_ptr_table, fp); | |
9946 | if (ret) | |
9947 | return ret; | |
9948 | ||
9949 | /* create anew and remember what it is */ | |
ecdeb87c | 9950 | ret = PerlIO_fdupopen(aTHX_ fp, param, PERLIO_DUP_CLONE); |
1d7c1841 GS |
9951 | ptr_table_store(PL_ptr_table, fp, ret); |
9952 | return ret; | |
9953 | } | |
9954 | ||
645c22ef DM |
9955 | /* duplicate a directory handle */ |
9956 | ||
1d7c1841 GS |
9957 | DIR * |
9958 | Perl_dirp_dup(pTHX_ DIR *dp) | |
9959 | { | |
9960 | if (!dp) | |
9961 | return (DIR*)NULL; | |
9962 | /* XXX TODO */ | |
9963 | return dp; | |
9964 | } | |
9965 | ||
ff276b08 | 9966 | /* duplicate a typeglob */ |
645c22ef | 9967 | |
1d7c1841 | 9968 | GP * |
a8fc9800 | 9969 | Perl_gp_dup(pTHX_ GP *gp, CLONE_PARAMS* param) |
1d7c1841 GS |
9970 | { |
9971 | GP *ret; | |
9972 | if (!gp) | |
9973 | return (GP*)NULL; | |
9974 | /* look for it in the table first */ | |
9975 | ret = (GP*)ptr_table_fetch(PL_ptr_table, gp); | |
9976 | if (ret) | |
9977 | return ret; | |
9978 | ||
9979 | /* create anew and remember what it is */ | |
9980 | Newz(0, ret, 1, GP); | |
9981 | ptr_table_store(PL_ptr_table, gp, ret); | |
9982 | ||
9983 | /* clone */ | |
9984 | ret->gp_refcnt = 0; /* must be before any other dups! */ | |
d2d73c3e AB |
9985 | ret->gp_sv = sv_dup_inc(gp->gp_sv, param); |
9986 | ret->gp_io = io_dup_inc(gp->gp_io, param); | |
9987 | ret->gp_form = cv_dup_inc(gp->gp_form, param); | |
9988 | ret->gp_av = av_dup_inc(gp->gp_av, param); | |
9989 | ret->gp_hv = hv_dup_inc(gp->gp_hv, param); | |
9990 | ret->gp_egv = gv_dup(gp->gp_egv, param);/* GvEGV is not refcounted */ | |
9991 | ret->gp_cv = cv_dup_inc(gp->gp_cv, param); | |
1d7c1841 GS |
9992 | ret->gp_cvgen = gp->gp_cvgen; |
9993 | ret->gp_flags = gp->gp_flags; | |
9994 | ret->gp_line = gp->gp_line; | |
9995 | ret->gp_file = gp->gp_file; /* points to COP.cop_file */ | |
9996 | return ret; | |
9997 | } | |
9998 | ||
645c22ef DM |
9999 | /* duplicate a chain of magic */ |
10000 | ||
1d7c1841 | 10001 | MAGIC * |
a8fc9800 | 10002 | Perl_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS* param) |
1d7c1841 | 10003 | { |
cb359b41 JH |
10004 | MAGIC *mgprev = (MAGIC*)NULL; |
10005 | MAGIC *mgret; | |
1d7c1841 GS |
10006 | if (!mg) |
10007 | return (MAGIC*)NULL; | |
10008 | /* look for it in the table first */ | |
10009 | mgret = (MAGIC*)ptr_table_fetch(PL_ptr_table, mg); | |
10010 | if (mgret) | |
10011 | return mgret; | |
10012 | ||
10013 | for (; mg; mg = mg->mg_moremagic) { | |
10014 | MAGIC *nmg; | |
10015 | Newz(0, nmg, 1, MAGIC); | |
cb359b41 | 10016 | if (mgprev) |
1d7c1841 | 10017 | mgprev->mg_moremagic = nmg; |
cb359b41 JH |
10018 | else |
10019 | mgret = nmg; | |
1d7c1841 GS |
10020 | nmg->mg_virtual = mg->mg_virtual; /* XXX copy dynamic vtable? */ |
10021 | nmg->mg_private = mg->mg_private; | |
10022 | nmg->mg_type = mg->mg_type; | |
10023 | nmg->mg_flags = mg->mg_flags; | |
14befaf4 | 10024 | if (mg->mg_type == PERL_MAGIC_qr) { |
d2f185dc | 10025 | nmg->mg_obj = (SV*)re_dup((REGEXP*)mg->mg_obj, param); |
1d7c1841 | 10026 | } |
05bd4103 | 10027 | else if(mg->mg_type == PERL_MAGIC_backref) { |
7fc63493 | 10028 | const AV * const av = (AV*) mg->mg_obj; |
fdc9a813 AE |
10029 | SV **svp; |
10030 | I32 i; | |
7fc63493 | 10031 | (void)SvREFCNT_inc(nmg->mg_obj = (SV*)newAV()); |
fdc9a813 AE |
10032 | svp = AvARRAY(av); |
10033 | for (i = AvFILLp(av); i >= 0; i--) { | |
3a81978b | 10034 | if (!svp[i]) continue; |
fdc9a813 AE |
10035 | av_push((AV*)nmg->mg_obj,sv_dup(svp[i],param)); |
10036 | } | |
05bd4103 | 10037 | } |
8d2f4536 NC |
10038 | else if (mg->mg_type == PERL_MAGIC_symtab) { |
10039 | nmg->mg_obj = mg->mg_obj; | |
10040 | } | |
1d7c1841 GS |
10041 | else { |
10042 | nmg->mg_obj = (mg->mg_flags & MGf_REFCOUNTED) | |
d2d73c3e AB |
10043 | ? sv_dup_inc(mg->mg_obj, param) |
10044 | : sv_dup(mg->mg_obj, param); | |
1d7c1841 GS |
10045 | } |
10046 | nmg->mg_len = mg->mg_len; | |
10047 | nmg->mg_ptr = mg->mg_ptr; /* XXX random ptr? */ | |
14befaf4 | 10048 | if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) { |
68795e93 | 10049 | if (mg->mg_len > 0) { |
1d7c1841 | 10050 | nmg->mg_ptr = SAVEPVN(mg->mg_ptr, mg->mg_len); |
14befaf4 DM |
10051 | if (mg->mg_type == PERL_MAGIC_overload_table && |
10052 | AMT_AMAGIC((AMT*)mg->mg_ptr)) | |
10053 | { | |
1d7c1841 GS |
10054 | AMT *amtp = (AMT*)mg->mg_ptr; |
10055 | AMT *namtp = (AMT*)nmg->mg_ptr; | |
10056 | I32 i; | |
10057 | for (i = 1; i < NofAMmeth; i++) { | |
d2d73c3e | 10058 | namtp->table[i] = cv_dup_inc(amtp->table[i], param); |
1d7c1841 GS |
10059 | } |
10060 | } | |
10061 | } | |
10062 | else if (mg->mg_len == HEf_SVKEY) | |
d2d73c3e | 10063 | nmg->mg_ptr = (char*)sv_dup_inc((SV*)mg->mg_ptr, param); |
1d7c1841 | 10064 | } |
68795e93 NIS |
10065 | if ((mg->mg_flags & MGf_DUP) && mg->mg_virtual && mg->mg_virtual->svt_dup) { |
10066 | CALL_FPTR(nmg->mg_virtual->svt_dup)(aTHX_ nmg, param); | |
10067 | } | |
1d7c1841 GS |
10068 | mgprev = nmg; |
10069 | } | |
10070 | return mgret; | |
10071 | } | |
10072 | ||
645c22ef DM |
10073 | /* create a new pointer-mapping table */ |
10074 | ||
1d7c1841 GS |
10075 | PTR_TBL_t * |
10076 | Perl_ptr_table_new(pTHX) | |
10077 | { | |
10078 | PTR_TBL_t *tbl; | |
10079 | Newz(0, tbl, 1, PTR_TBL_t); | |
10080 | tbl->tbl_max = 511; | |
10081 | tbl->tbl_items = 0; | |
10082 | Newz(0, tbl->tbl_ary, tbl->tbl_max + 1, PTR_TBL_ENT_t*); | |
10083 | return tbl; | |
10084 | } | |
10085 | ||
134ca3d6 DM |
10086 | #if (PTRSIZE == 8) |
10087 | # define PTR_TABLE_HASH(ptr) (PTR2UV(ptr) >> 3) | |
10088 | #else | |
10089 | # define PTR_TABLE_HASH(ptr) (PTR2UV(ptr) >> 2) | |
10090 | #endif | |
10091 | ||
437a6bf1 | 10092 | #define new_pte() new_body(struct ptr_tbl_ent, pte) |
cb4415b8 | 10093 | #define del_pte(p) del_body_type(p, struct ptr_tbl_ent, pte) |
32e691d0 | 10094 | |
645c22ef DM |
10095 | /* map an existing pointer using a table */ |
10096 | ||
1d7c1841 | 10097 | void * |
53c1dcc0 | 10098 | Perl_ptr_table_fetch(pTHX_ PTR_TBL_t *tbl, const void *sv) |
1d7c1841 GS |
10099 | { |
10100 | PTR_TBL_ENT_t *tblent; | |
4373e329 | 10101 | const UV hash = PTR_TABLE_HASH(sv); |
1d7c1841 GS |
10102 | assert(tbl); |
10103 | tblent = tbl->tbl_ary[hash & tbl->tbl_max]; | |
10104 | for (; tblent; tblent = tblent->next) { | |
10105 | if (tblent->oldval == sv) | |
10106 | return tblent->newval; | |
10107 | } | |
10108 | return (void*)NULL; | |
10109 | } | |
10110 | ||
645c22ef DM |
10111 | /* add a new entry to a pointer-mapping table */ |
10112 | ||
1d7c1841 | 10113 | void |
53c1dcc0 | 10114 | Perl_ptr_table_store(pTHX_ PTR_TBL_t *tbl, const void *oldv, void *newv) |
1d7c1841 GS |
10115 | { |
10116 | PTR_TBL_ENT_t *tblent, **otblent; | |
10117 | /* XXX this may be pessimal on platforms where pointers aren't good | |
10118 | * hash values e.g. if they grow faster in the most significant | |
10119 | * bits */ | |
4373e329 | 10120 | const UV hash = PTR_TABLE_HASH(oldv); |
14cade97 | 10121 | bool empty = 1; |
1d7c1841 GS |
10122 | |
10123 | assert(tbl); | |
10124 | otblent = &tbl->tbl_ary[hash & tbl->tbl_max]; | |
14cade97 | 10125 | for (tblent = *otblent; tblent; empty=0, tblent = tblent->next) { |
1d7c1841 GS |
10126 | if (tblent->oldval == oldv) { |
10127 | tblent->newval = newv; | |
1d7c1841 GS |
10128 | return; |
10129 | } | |
10130 | } | |
437a6bf1 | 10131 | tblent = new_pte(); |
1d7c1841 GS |
10132 | tblent->oldval = oldv; |
10133 | tblent->newval = newv; | |
10134 | tblent->next = *otblent; | |
10135 | *otblent = tblent; | |
10136 | tbl->tbl_items++; | |
14cade97 | 10137 | if (!empty && tbl->tbl_items > tbl->tbl_max) |
1d7c1841 GS |
10138 | ptr_table_split(tbl); |
10139 | } | |
10140 | ||
645c22ef DM |
10141 | /* double the hash bucket size of an existing ptr table */ |
10142 | ||
1d7c1841 GS |
10143 | void |
10144 | Perl_ptr_table_split(pTHX_ PTR_TBL_t *tbl) | |
10145 | { | |
10146 | PTR_TBL_ENT_t **ary = tbl->tbl_ary; | |
4373e329 | 10147 | const UV oldsize = tbl->tbl_max + 1; |
1d7c1841 GS |
10148 | UV newsize = oldsize * 2; |
10149 | UV i; | |
10150 | ||
10151 | Renew(ary, newsize, PTR_TBL_ENT_t*); | |
10152 | Zero(&ary[oldsize], newsize-oldsize, PTR_TBL_ENT_t*); | |
10153 | tbl->tbl_max = --newsize; | |
10154 | tbl->tbl_ary = ary; | |
10155 | for (i=0; i < oldsize; i++, ary++) { | |
10156 | PTR_TBL_ENT_t **curentp, **entp, *ent; | |
10157 | if (!*ary) | |
10158 | continue; | |
10159 | curentp = ary + oldsize; | |
10160 | for (entp = ary, ent = *ary; ent; ent = *entp) { | |
134ca3d6 | 10161 | if ((newsize & PTR_TABLE_HASH(ent->oldval)) != i) { |
1d7c1841 GS |
10162 | *entp = ent->next; |
10163 | ent->next = *curentp; | |
10164 | *curentp = ent; | |
10165 | continue; | |
10166 | } | |
10167 | else | |
10168 | entp = &ent->next; | |
10169 | } | |
10170 | } | |
10171 | } | |
10172 | ||
645c22ef DM |
10173 | /* remove all the entries from a ptr table */ |
10174 | ||
a0739874 DM |
10175 | void |
10176 | Perl_ptr_table_clear(pTHX_ PTR_TBL_t *tbl) | |
10177 | { | |
10178 | register PTR_TBL_ENT_t **array; | |
10179 | register PTR_TBL_ENT_t *entry; | |
a0739874 DM |
10180 | UV riter = 0; |
10181 | UV max; | |
10182 | ||
10183 | if (!tbl || !tbl->tbl_items) { | |
10184 | return; | |
10185 | } | |
10186 | ||
10187 | array = tbl->tbl_ary; | |
10188 | entry = array[0]; | |
10189 | max = tbl->tbl_max; | |
10190 | ||
10191 | for (;;) { | |
10192 | if (entry) { | |
4373e329 | 10193 | PTR_TBL_ENT_t *oentry = entry; |
a0739874 | 10194 | entry = entry->next; |
437a6bf1 | 10195 | del_pte(oentry); |
a0739874 DM |
10196 | } |
10197 | if (!entry) { | |
10198 | if (++riter > max) { | |
10199 | break; | |
10200 | } | |
10201 | entry = array[riter]; | |
10202 | } | |
10203 | } | |
10204 | ||
10205 | tbl->tbl_items = 0; | |
10206 | } | |
10207 | ||
645c22ef DM |
10208 | /* clear and free a ptr table */ |
10209 | ||
a0739874 DM |
10210 | void |
10211 | Perl_ptr_table_free(pTHX_ PTR_TBL_t *tbl) | |
10212 | { | |
10213 | if (!tbl) { | |
10214 | return; | |
10215 | } | |
10216 | ptr_table_clear(tbl); | |
10217 | Safefree(tbl->tbl_ary); | |
10218 | Safefree(tbl); | |
10219 | } | |
10220 | ||
645c22ef DM |
10221 | /* attempt to make everything in the typeglob readonly */ |
10222 | ||
5bd07a3d | 10223 | STATIC SV * |
59b40662 | 10224 | S_gv_share(pTHX_ SV *sstr, CLONE_PARAMS *param) |
5bd07a3d DM |
10225 | { |
10226 | GV *gv = (GV*)sstr; | |
59b40662 | 10227 | SV *sv = ¶m->proto_perl->Isv_no; /* just need SvREADONLY-ness */ |
5bd07a3d DM |
10228 | |
10229 | if (GvIO(gv) || GvFORM(gv)) { | |
7fb37951 | 10230 | GvUNIQUE_off(gv); /* GvIOs cannot be shared. nor can GvFORMs */ |
5bd07a3d DM |
10231 | } |
10232 | else if (!GvCV(gv)) { | |
10233 | GvCV(gv) = (CV*)sv; | |
10234 | } | |
10235 | else { | |
10236 | /* CvPADLISTs cannot be shared */ | |
37e20706 | 10237 | if (!SvREADONLY(GvCV(gv)) && !CvXSUB(GvCV(gv))) { |
7fb37951 | 10238 | GvUNIQUE_off(gv); |
5bd07a3d DM |
10239 | } |
10240 | } | |
10241 | ||
7fb37951 | 10242 | if (!GvUNIQUE(gv)) { |
5bd07a3d DM |
10243 | #if 0 |
10244 | PerlIO_printf(Perl_debug_log, "gv_share: unable to share %s::%s\n", | |
bfcb3514 | 10245 | HvNAME_get(GvSTASH(gv)), GvNAME(gv)); |
5bd07a3d DM |
10246 | #endif |
10247 | return Nullsv; | |
10248 | } | |
10249 | ||
4411f3b6 | 10250 | /* |
5bd07a3d DM |
10251 | * write attempts will die with |
10252 | * "Modification of a read-only value attempted" | |
10253 | */ | |
10254 | if (!GvSV(gv)) { | |
10255 | GvSV(gv) = sv; | |
10256 | } | |
10257 | else { | |
10258 | SvREADONLY_on(GvSV(gv)); | |
10259 | } | |
10260 | ||
10261 | if (!GvAV(gv)) { | |
10262 | GvAV(gv) = (AV*)sv; | |
10263 | } | |
10264 | else { | |
10265 | SvREADONLY_on(GvAV(gv)); | |
10266 | } | |
10267 | ||
10268 | if (!GvHV(gv)) { | |
10269 | GvHV(gv) = (HV*)sv; | |
10270 | } | |
10271 | else { | |
53c33732 | 10272 | SvREADONLY_on(GvHV(gv)); |
5bd07a3d DM |
10273 | } |
10274 | ||
10275 | return sstr; /* he_dup() will SvREFCNT_inc() */ | |
10276 | } | |
10277 | ||
83841fad NIS |
10278 | void |
10279 | Perl_rvpv_dup(pTHX_ SV *dstr, SV *sstr, CLONE_PARAMS* param) | |
10280 | { | |
10281 | if (SvROK(sstr)) { | |
b162af07 SP |
10282 | SvRV_set(dstr, SvWEAKREF(sstr) |
10283 | ? sv_dup(SvRV(sstr), param) | |
10284 | : sv_dup_inc(SvRV(sstr), param)); | |
f880fe2f | 10285 | |
83841fad | 10286 | } |
3f7c398e | 10287 | else if (SvPVX_const(sstr)) { |
83841fad NIS |
10288 | /* Has something there */ |
10289 | if (SvLEN(sstr)) { | |
68795e93 | 10290 | /* Normal PV - clone whole allocated space */ |
3f7c398e | 10291 | SvPV_set(dstr, SAVEPVN(SvPVX_const(sstr), SvLEN(sstr)-1)); |
d3d0e6f1 NC |
10292 | if (SvREADONLY(sstr) && SvFAKE(sstr)) { |
10293 | /* Not that normal - actually sstr is copy on write. | |
10294 | But we are a true, independant SV, so: */ | |
10295 | SvREADONLY_off(dstr); | |
10296 | SvFAKE_off(dstr); | |
10297 | } | |
68795e93 | 10298 | } |
83841fad NIS |
10299 | else { |
10300 | /* Special case - not normally malloced for some reason */ | |
ef10be65 NC |
10301 | if ((SvREADONLY(sstr) && SvFAKE(sstr))) { |
10302 | /* A "shared" PV - clone it as "shared" PV */ | |
10303 | SvPV_set(dstr, | |
10304 | HEK_KEY(hek_dup(SvSHARED_HEK_FROM_PV(SvPVX_const(sstr)), | |
10305 | param))); | |
83841fad NIS |
10306 | } |
10307 | else { | |
10308 | /* Some other special case - random pointer */ | |
f880fe2f | 10309 | SvPV_set(dstr, SvPVX(sstr)); |
d3d0e6f1 | 10310 | } |
83841fad NIS |
10311 | } |
10312 | } | |
10313 | else { | |
10314 | /* Copy the Null */ | |
f880fe2f | 10315 | if (SvTYPE(dstr) == SVt_RV) |
b162af07 | 10316 | SvRV_set(dstr, NULL); |
f880fe2f SP |
10317 | else |
10318 | SvPV_set(dstr, 0); | |
83841fad NIS |
10319 | } |
10320 | } | |
10321 | ||
662fb8b2 NC |
10322 | /* duplicate an SV of any type (including AV, HV etc) */ |
10323 | ||
1d7c1841 | 10324 | SV * |
a8fc9800 | 10325 | Perl_sv_dup(pTHX_ SV *sstr, CLONE_PARAMS* param) |
1d7c1841 | 10326 | { |
27da23d5 | 10327 | dVAR; |
1d7c1841 GS |
10328 | SV *dstr; |
10329 | ||
10330 | if (!sstr || SvTYPE(sstr) == SVTYPEMASK) | |
10331 | return Nullsv; | |
10332 | /* look for it in the table first */ | |
10333 | dstr = (SV*)ptr_table_fetch(PL_ptr_table, sstr); | |
10334 | if (dstr) | |
10335 | return dstr; | |
10336 | ||
0405e91e AB |
10337 | if(param->flags & CLONEf_JOIN_IN) { |
10338 | /** We are joining here so we don't want do clone | |
10339 | something that is bad **/ | |
bfcb3514 | 10340 | const char *hvname; |
0405e91e AB |
10341 | |
10342 | if(SvTYPE(sstr) == SVt_PVHV && | |
bfcb3514 | 10343 | (hvname = HvNAME_get(sstr))) { |
0405e91e | 10344 | /** don't clone stashes if they already exist **/ |
bfcb3514 | 10345 | HV* old_stash = gv_stashpv(hvname,0); |
0405e91e AB |
10346 | return (SV*) old_stash; |
10347 | } | |
10348 | } | |
10349 | ||
1d7c1841 GS |
10350 | /* create anew and remember what it is */ |
10351 | new_SV(dstr); | |
fd0854ff DM |
10352 | |
10353 | #ifdef DEBUG_LEAKING_SCALARS | |
10354 | dstr->sv_debug_optype = sstr->sv_debug_optype; | |
10355 | dstr->sv_debug_line = sstr->sv_debug_line; | |
10356 | dstr->sv_debug_inpad = sstr->sv_debug_inpad; | |
10357 | dstr->sv_debug_cloned = 1; | |
10358 | # ifdef NETWARE | |
10359 | dstr->sv_debug_file = savepv(sstr->sv_debug_file); | |
10360 | # else | |
10361 | dstr->sv_debug_file = savesharedpv(sstr->sv_debug_file); | |
10362 | # endif | |
10363 | #endif | |
10364 | ||
1d7c1841 GS |
10365 | ptr_table_store(PL_ptr_table, sstr, dstr); |
10366 | ||
10367 | /* clone */ | |
10368 | SvFLAGS(dstr) = SvFLAGS(sstr); | |
10369 | SvFLAGS(dstr) &= ~SVf_OOK; /* don't propagate OOK hack */ | |
10370 | SvREFCNT(dstr) = 0; /* must be before any other dups! */ | |
10371 | ||
10372 | #ifdef DEBUGGING | |
3f7c398e | 10373 | if (SvANY(sstr) && PL_watch_pvx && SvPVX_const(sstr) == PL_watch_pvx) |
1d7c1841 | 10374 | PerlIO_printf(Perl_debug_log, "watch at %p hit, found string \"%s\"\n", |
3f7c398e | 10375 | PL_watch_pvx, SvPVX_const(sstr)); |
1d7c1841 GS |
10376 | #endif |
10377 | ||
9660f481 DM |
10378 | /* don't clone objects whose class has asked us not to */ |
10379 | if (SvOBJECT(sstr) && ! (SvFLAGS(SvSTASH(sstr)) & SVphv_CLONEABLE)) { | |
10380 | SvFLAGS(dstr) &= ~SVTYPEMASK; | |
10381 | SvOBJECT_off(dstr); | |
10382 | return dstr; | |
10383 | } | |
10384 | ||
1d7c1841 GS |
10385 | switch (SvTYPE(sstr)) { |
10386 | case SVt_NULL: | |
10387 | SvANY(dstr) = NULL; | |
10388 | break; | |
10389 | case SVt_IV: | |
339049b0 | 10390 | SvANY(dstr) = (XPVIV*)((char*)&(dstr->sv_u.svu_iv) - STRUCT_OFFSET(XPVIV, xiv_iv)); |
45977657 | 10391 | SvIV_set(dstr, SvIVX(sstr)); |
1d7c1841 GS |
10392 | break; |
10393 | case SVt_NV: | |
10394 | SvANY(dstr) = new_XNV(); | |
9d6ce603 | 10395 | SvNV_set(dstr, SvNVX(sstr)); |
1d7c1841 GS |
10396 | break; |
10397 | case SVt_RV: | |
339049b0 | 10398 | SvANY(dstr) = &(dstr->sv_u.svu_rv); |
83841fad | 10399 | Perl_rvpv_dup(aTHX_ dstr, sstr, param); |
1d7c1841 | 10400 | break; |
662fb8b2 NC |
10401 | default: |
10402 | { | |
10403 | /* These are all the types that need complex bodies allocating. */ | |
10404 | size_t new_body_length; | |
10405 | size_t new_body_offset = 0; | |
10406 | void **new_body_arena; | |
10407 | void **new_body_arenaroot; | |
10408 | void *new_body; | |
10409 | ||
10410 | switch (SvTYPE(sstr)) { | |
10411 | default: | |
10412 | Perl_croak(aTHX_ "Bizarre SvTYPE [%" IVdf "]", | |
10413 | (IV)SvTYPE(sstr)); | |
10414 | break; | |
10415 | ||
10416 | case SVt_PVIO: | |
10417 | new_body = new_XPVIO(); | |
10418 | new_body_length = sizeof(XPVIO); | |
10419 | break; | |
10420 | case SVt_PVFM: | |
10421 | new_body = new_XPVFM(); | |
10422 | new_body_length = sizeof(XPVFM); | |
10423 | break; | |
10424 | ||
10425 | case SVt_PVHV: | |
10426 | new_body_arena = (void **) &PL_xpvhv_root; | |
10427 | new_body_arenaroot = (void **) &PL_xpvhv_arenaroot; | |
10428 | new_body_offset = STRUCT_OFFSET(XPVHV, xhv_fill) | |
10429 | - STRUCT_OFFSET(xpvhv_allocated, xhv_fill); | |
10430 | new_body_length = STRUCT_OFFSET(XPVHV, xmg_stash) | |
10431 | + sizeof (((XPVHV*)SvANY(sstr))->xmg_stash) | |
10432 | - new_body_offset; | |
10433 | goto new_body; | |
10434 | case SVt_PVAV: | |
10435 | new_body_arena = (void **) &PL_xpvav_root; | |
10436 | new_body_arenaroot = (void **) &PL_xpvav_arenaroot; | |
10437 | new_body_offset = STRUCT_OFFSET(XPVAV, xav_fill) | |
10438 | - STRUCT_OFFSET(xpvav_allocated, xav_fill); | |
10439 | new_body_length = STRUCT_OFFSET(XPVHV, xmg_stash) | |
10440 | + sizeof (((XPVHV*)SvANY(sstr))->xmg_stash) | |
10441 | - new_body_offset; | |
10442 | goto new_body; | |
10443 | case SVt_PVBM: | |
10444 | new_body_length = sizeof(XPVBM); | |
10445 | new_body_arena = (void **) &PL_xpvbm_root; | |
10446 | new_body_arenaroot = (void **) &PL_xpvbm_arenaroot; | |
10447 | goto new_body; | |
10448 | case SVt_PVGV: | |
10449 | if (GvUNIQUE((GV*)sstr)) { | |
10450 | SV *share; | |
10451 | if ((share = gv_share(sstr, param))) { | |
10452 | del_SV(dstr); | |
10453 | dstr = share; | |
10454 | ptr_table_store(PL_ptr_table, sstr, dstr); | |
5bd07a3d | 10455 | #if 0 |
662fb8b2 NC |
10456 | PerlIO_printf(Perl_debug_log, "sv_dup: sharing %s::%s\n", |
10457 | HvNAME_get(GvSTASH(share)), GvNAME(share)); | |
10458 | #endif | |
10459 | goto done_share; | |
10460 | } | |
10461 | } | |
10462 | new_body_length = sizeof(XPVGV); | |
10463 | new_body_arena = (void **) &PL_xpvgv_root; | |
10464 | new_body_arenaroot = (void **) &PL_xpvgv_arenaroot; | |
10465 | goto new_body; | |
10466 | case SVt_PVCV: | |
10467 | new_body_length = sizeof(XPVCV); | |
10468 | new_body_arena = (void **) &PL_xpvcv_root; | |
10469 | new_body_arenaroot = (void **) &PL_xpvcv_arenaroot; | |
10470 | goto new_body; | |
10471 | case SVt_PVLV: | |
10472 | new_body_length = sizeof(XPVLV); | |
10473 | new_body_arena = (void **) &PL_xpvlv_root; | |
10474 | new_body_arenaroot = (void **) &PL_xpvlv_arenaroot; | |
10475 | goto new_body; | |
10476 | case SVt_PVMG: | |
10477 | new_body_length = sizeof(XPVMG); | |
10478 | new_body_arena = (void **) &PL_xpvmg_root; | |
10479 | new_body_arenaroot = (void **) &PL_xpvmg_arenaroot; | |
10480 | goto new_body; | |
10481 | case SVt_PVNV: | |
10482 | new_body_length = sizeof(XPVNV); | |
10483 | new_body_arena = (void **) &PL_xpvnv_root; | |
10484 | new_body_arenaroot = (void **) &PL_xpvnv_arenaroot; | |
10485 | goto new_body; | |
10486 | case SVt_PVIV: | |
10487 | new_body_offset = STRUCT_OFFSET(XPVIV, xpv_cur) | |
10488 | - STRUCT_OFFSET(xpviv_allocated, xpv_cur); | |
10489 | new_body_length = sizeof(XPVIV) - new_body_offset; | |
10490 | new_body_arena = (void **) &PL_xpviv_root; | |
10491 | new_body_arenaroot = (void **) &PL_xpviv_arenaroot; | |
10492 | goto new_body; | |
10493 | case SVt_PV: | |
10494 | new_body_offset = STRUCT_OFFSET(XPV, xpv_cur) | |
10495 | - STRUCT_OFFSET(xpv_allocated, xpv_cur); | |
10496 | new_body_length = sizeof(XPV) - new_body_offset; | |
10497 | new_body_arena = (void **) &PL_xpv_root; | |
10498 | new_body_arenaroot = (void **) &PL_xpv_arenaroot; | |
10499 | new_body: | |
10500 | assert(new_body_length); | |
10501 | #ifndef PURIFY | |
dd690478 NC |
10502 | new_body = (void*)((char*)S_new_body(aTHX_ new_body_arenaroot, |
10503 | new_body_arena, | |
10504 | new_body_length) | |
10505 | - new_body_offset); | |
662fb8b2 NC |
10506 | #else |
10507 | /* We always allocated the full length item with PURIFY */ | |
10508 | new_body_length += new_body_offset; | |
10509 | new_body_offset = 0; | |
10510 | new_body = my_safemalloc(new_body_length); | |
5bd07a3d | 10511 | #endif |
1d7c1841 | 10512 | } |
662fb8b2 NC |
10513 | assert(new_body); |
10514 | SvANY(dstr) = new_body; | |
10515 | ||
10516 | Copy(((char*)SvANY(sstr)) + new_body_offset, | |
10517 | ((char*)SvANY(dstr)) + new_body_offset, | |
10518 | new_body_length, char); | |
10519 | ||
10520 | if (SvTYPE(sstr) != SVt_PVAV && SvTYPE(sstr) != SVt_PVHV) | |
10521 | Perl_rvpv_dup(aTHX_ dstr, sstr, param); | |
10522 | ||
10523 | /* The Copy above means that all the source (unduplicated) pointers | |
10524 | are now in the destination. We can check the flags and the | |
10525 | pointers in either, but it's possible that there's less cache | |
10526 | missing by always going for the destination. | |
10527 | FIXME - instrument and check that assumption */ | |
10528 | if (SvTYPE(sstr) >= SVt_PVMG) { | |
10529 | if (SvMAGIC(dstr)) | |
10530 | SvMAGIC_set(dstr, mg_dup(SvMAGIC(dstr), param)); | |
10531 | if (SvSTASH(dstr)) | |
10532 | SvSTASH_set(dstr, hv_dup_inc(SvSTASH(dstr), param)); | |
1d7c1841 | 10533 | } |
662fb8b2 NC |
10534 | |
10535 | switch (SvTYPE(sstr)) { | |
10536 | case SVt_PV: | |
10537 | break; | |
10538 | case SVt_PVIV: | |
10539 | break; | |
10540 | case SVt_PVNV: | |
10541 | break; | |
10542 | case SVt_PVMG: | |
10543 | break; | |
10544 | case SVt_PVBM: | |
10545 | break; | |
10546 | case SVt_PVLV: | |
10547 | /* XXX LvTARGOFF sometimes holds PMOP* when DEBUGGING */ | |
10548 | if (LvTYPE(dstr) == 't') /* for tie: unrefcnted fake (SV**) */ | |
10549 | LvTARG(dstr) = dstr; | |
10550 | else if (LvTYPE(dstr) == 'T') /* for tie: fake HE */ | |
10551 | LvTARG(dstr) = (SV*)he_dup((HE*)LvTARG(dstr), 0, param); | |
10552 | else | |
10553 | LvTARG(dstr) = sv_dup_inc(LvTARG(dstr), param); | |
10554 | break; | |
10555 | case SVt_PVGV: | |
10556 | GvNAME(dstr) = SAVEPVN(GvNAME(dstr), GvNAMELEN(dstr)); | |
10557 | GvSTASH(dstr) = hv_dup_inc(GvSTASH(dstr), param); | |
10558 | GvGP(dstr) = gp_dup(GvGP(dstr), param); | |
10559 | (void)GpREFCNT_inc(GvGP(dstr)); | |
10560 | break; | |
10561 | case SVt_PVIO: | |
10562 | IoIFP(dstr) = fp_dup(IoIFP(dstr), IoTYPE(dstr), param); | |
10563 | if (IoOFP(dstr) == IoIFP(sstr)) | |
10564 | IoOFP(dstr) = IoIFP(dstr); | |
10565 | else | |
10566 | IoOFP(dstr) = fp_dup(IoOFP(dstr), IoTYPE(dstr), param); | |
10567 | /* PL_rsfp_filters entries have fake IoDIRP() */ | |
10568 | if (IoDIRP(dstr) && !(IoFLAGS(dstr) & IOf_FAKE_DIRP)) | |
10569 | IoDIRP(dstr) = dirp_dup(IoDIRP(dstr)); | |
10570 | if(IoFLAGS(dstr) & IOf_FAKE_DIRP) { | |
10571 | /* I have no idea why fake dirp (rsfps) | |
10572 | should be treated differently but otherwise | |
10573 | we end up with leaks -- sky*/ | |
10574 | IoTOP_GV(dstr) = gv_dup_inc(IoTOP_GV(dstr), param); | |
10575 | IoFMT_GV(dstr) = gv_dup_inc(IoFMT_GV(dstr), param); | |
10576 | IoBOTTOM_GV(dstr) = gv_dup_inc(IoBOTTOM_GV(dstr), param); | |
10577 | } else { | |
10578 | IoTOP_GV(dstr) = gv_dup(IoTOP_GV(dstr), param); | |
10579 | IoFMT_GV(dstr) = gv_dup(IoFMT_GV(dstr), param); | |
10580 | IoBOTTOM_GV(dstr) = gv_dup(IoBOTTOM_GV(dstr), param); | |
10581 | } | |
10582 | IoTOP_NAME(dstr) = SAVEPV(IoTOP_NAME(dstr)); | |
10583 | IoFMT_NAME(dstr) = SAVEPV(IoFMT_NAME(dstr)); | |
10584 | IoBOTTOM_NAME(dstr) = SAVEPV(IoBOTTOM_NAME(dstr)); | |
10585 | break; | |
10586 | case SVt_PVAV: | |
10587 | if (AvARRAY((AV*)sstr)) { | |
10588 | SV **dst_ary, **src_ary; | |
10589 | SSize_t items = AvFILLp((AV*)sstr) + 1; | |
10590 | ||
10591 | src_ary = AvARRAY((AV*)sstr); | |
10592 | Newz(0, dst_ary, AvMAX((AV*)sstr)+1, SV*); | |
10593 | ptr_table_store(PL_ptr_table, src_ary, dst_ary); | |
10594 | SvPV_set(dstr, (char*)dst_ary); | |
10595 | AvALLOC((AV*)dstr) = dst_ary; | |
10596 | if (AvREAL((AV*)sstr)) { | |
10597 | while (items-- > 0) | |
10598 | *dst_ary++ = sv_dup_inc(*src_ary++, param); | |
10599 | } | |
10600 | else { | |
10601 | while (items-- > 0) | |
10602 | *dst_ary++ = sv_dup(*src_ary++, param); | |
10603 | } | |
10604 | items = AvMAX((AV*)sstr) - AvFILLp((AV*)sstr); | |
10605 | while (items-- > 0) { | |
10606 | *dst_ary++ = &PL_sv_undef; | |
10607 | } | |
bfcb3514 | 10608 | } |
662fb8b2 NC |
10609 | else { |
10610 | SvPV_set(dstr, Nullch); | |
10611 | AvALLOC((AV*)dstr) = (SV**)NULL; | |
b79f7545 | 10612 | } |
662fb8b2 NC |
10613 | break; |
10614 | case SVt_PVHV: | |
10615 | { | |
10616 | HEK *hvname = 0; | |
10617 | ||
10618 | if (HvARRAY((HV*)sstr)) { | |
10619 | STRLEN i = 0; | |
10620 | const bool sharekeys = !!HvSHAREKEYS(sstr); | |
10621 | XPVHV * const dxhv = (XPVHV*)SvANY(dstr); | |
10622 | XPVHV * const sxhv = (XPVHV*)SvANY(sstr); | |
10623 | char *darray; | |
10624 | New(0, darray, | |
10625 | PERL_HV_ARRAY_ALLOC_BYTES(dxhv->xhv_max+1) | |
10626 | + (SvOOK(sstr) ? sizeof(struct xpvhv_aux) : 0), | |
10627 | char); | |
10628 | HvARRAY(dstr) = (HE**)darray; | |
10629 | while (i <= sxhv->xhv_max) { | |
10630 | HE *source = HvARRAY(sstr)[i]; | |
10631 | HvARRAY(dstr)[i] = source | |
10632 | ? he_dup(source, sharekeys, param) : 0; | |
10633 | ++i; | |
10634 | } | |
10635 | if (SvOOK(sstr)) { | |
10636 | struct xpvhv_aux *saux = HvAUX(sstr); | |
10637 | struct xpvhv_aux *daux = HvAUX(dstr); | |
10638 | /* This flag isn't copied. */ | |
10639 | /* SvOOK_on(hv) attacks the IV flags. */ | |
10640 | SvFLAGS(dstr) |= SVf_OOK; | |
10641 | ||
10642 | hvname = saux->xhv_name; | |
dd690478 NC |
10643 | daux->xhv_name |
10644 | = hvname ? hek_dup(hvname, param) : hvname; | |
662fb8b2 NC |
10645 | |
10646 | daux->xhv_riter = saux->xhv_riter; | |
10647 | daux->xhv_eiter = saux->xhv_eiter | |
dd690478 NC |
10648 | ? he_dup(saux->xhv_eiter, |
10649 | (bool)!!HvSHAREKEYS(sstr), param) : 0; | |
662fb8b2 NC |
10650 | } |
10651 | } | |
10652 | else { | |
10653 | SvPV_set(dstr, Nullch); | |
10654 | } | |
10655 | /* Record stashes for possible cloning in Perl_clone(). */ | |
10656 | if(hvname) | |
10657 | av_push(param->stashes, dstr); | |
10658 | } | |
10659 | break; | |
10660 | case SVt_PVFM: | |
10661 | case SVt_PVCV: | |
10662 | /* NOTE: not refcounted */ | |
10663 | CvSTASH(dstr) = hv_dup(CvSTASH(dstr), param); | |
10664 | OP_REFCNT_LOCK; | |
10665 | CvROOT(dstr) = OpREFCNT_inc(CvROOT(dstr)); | |
10666 | OP_REFCNT_UNLOCK; | |
10667 | if (CvCONST(dstr)) { | |
10668 | CvXSUBANY(dstr).any_ptr = GvUNIQUE(CvGV(dstr)) ? | |
10669 | SvREFCNT_inc(CvXSUBANY(dstr).any_ptr) : | |
10670 | sv_dup_inc((SV *)CvXSUBANY(dstr).any_ptr, param); | |
10671 | } | |
10672 | /* don't dup if copying back - CvGV isn't refcounted, so the | |
10673 | * duped GV may never be freed. A bit of a hack! DAPM */ | |
10674 | CvGV(dstr) = (param->flags & CLONEf_JOIN_IN) ? | |
10675 | Nullgv : gv_dup(CvGV(dstr), param) ; | |
10676 | if (!(param->flags & CLONEf_COPY_STACKS)) { | |
10677 | CvDEPTH(dstr) = 0; | |
10678 | } | |
10679 | PAD_DUP(CvPADLIST(dstr), CvPADLIST(sstr), param); | |
10680 | CvOUTSIDE(dstr) = | |
10681 | CvWEAKOUTSIDE(sstr) | |
10682 | ? cv_dup( CvOUTSIDE(dstr), param) | |
10683 | : cv_dup_inc(CvOUTSIDE(dstr), param); | |
10684 | if (!CvXSUB(dstr)) | |
10685 | CvFILE(dstr) = SAVEPV(CvFILE(dstr)); | |
10686 | break; | |
bfcb3514 | 10687 | } |
1d7c1841 | 10688 | } |
1d7c1841 GS |
10689 | } |
10690 | ||
662fb8b2 | 10691 | done_share: |
1d7c1841 GS |
10692 | if (SvOBJECT(dstr) && SvTYPE(dstr) != SVt_PVIO) |
10693 | ++PL_sv_objcount; | |
10694 | ||
10695 | return dstr; | |
d2d73c3e | 10696 | } |
1d7c1841 | 10697 | |
645c22ef DM |
10698 | /* duplicate a context */ |
10699 | ||
1d7c1841 | 10700 | PERL_CONTEXT * |
a8fc9800 | 10701 | Perl_cx_dup(pTHX_ PERL_CONTEXT *cxs, I32 ix, I32 max, CLONE_PARAMS* param) |
1d7c1841 GS |
10702 | { |
10703 | PERL_CONTEXT *ncxs; | |
10704 | ||
10705 | if (!cxs) | |
10706 | return (PERL_CONTEXT*)NULL; | |
10707 | ||
10708 | /* look for it in the table first */ | |
10709 | ncxs = (PERL_CONTEXT*)ptr_table_fetch(PL_ptr_table, cxs); | |
10710 | if (ncxs) | |
10711 | return ncxs; | |
10712 | ||
10713 | /* create anew and remember what it is */ | |
10714 | Newz(56, ncxs, max + 1, PERL_CONTEXT); | |
10715 | ptr_table_store(PL_ptr_table, cxs, ncxs); | |
10716 | ||
10717 | while (ix >= 0) { | |
10718 | PERL_CONTEXT *cx = &cxs[ix]; | |
10719 | PERL_CONTEXT *ncx = &ncxs[ix]; | |
10720 | ncx->cx_type = cx->cx_type; | |
10721 | if (CxTYPE(cx) == CXt_SUBST) { | |
10722 | Perl_croak(aTHX_ "Cloning substitution context is unimplemented"); | |
10723 | } | |
10724 | else { | |
10725 | ncx->blk_oldsp = cx->blk_oldsp; | |
10726 | ncx->blk_oldcop = cx->blk_oldcop; | |
1d7c1841 GS |
10727 | ncx->blk_oldmarksp = cx->blk_oldmarksp; |
10728 | ncx->blk_oldscopesp = cx->blk_oldscopesp; | |
10729 | ncx->blk_oldpm = cx->blk_oldpm; | |
10730 | ncx->blk_gimme = cx->blk_gimme; | |
10731 | switch (CxTYPE(cx)) { | |
10732 | case CXt_SUB: | |
10733 | ncx->blk_sub.cv = (cx->blk_sub.olddepth == 0 | |
d2d73c3e AB |
10734 | ? cv_dup_inc(cx->blk_sub.cv, param) |
10735 | : cv_dup(cx->blk_sub.cv,param)); | |
1d7c1841 | 10736 | ncx->blk_sub.argarray = (cx->blk_sub.hasargs |
d2d73c3e | 10737 | ? av_dup_inc(cx->blk_sub.argarray, param) |
1d7c1841 | 10738 | : Nullav); |
d2d73c3e | 10739 | ncx->blk_sub.savearray = av_dup_inc(cx->blk_sub.savearray, param); |
1d7c1841 GS |
10740 | ncx->blk_sub.olddepth = cx->blk_sub.olddepth; |
10741 | ncx->blk_sub.hasargs = cx->blk_sub.hasargs; | |
10742 | ncx->blk_sub.lval = cx->blk_sub.lval; | |
f39bc417 | 10743 | ncx->blk_sub.retop = cx->blk_sub.retop; |
1d7c1841 GS |
10744 | break; |
10745 | case CXt_EVAL: | |
10746 | ncx->blk_eval.old_in_eval = cx->blk_eval.old_in_eval; | |
10747 | ncx->blk_eval.old_op_type = cx->blk_eval.old_op_type; | |
b47cad08 | 10748 | ncx->blk_eval.old_namesv = sv_dup_inc(cx->blk_eval.old_namesv, param); |
1d7c1841 | 10749 | ncx->blk_eval.old_eval_root = cx->blk_eval.old_eval_root; |
d2d73c3e | 10750 | ncx->blk_eval.cur_text = sv_dup(cx->blk_eval.cur_text, param); |
f39bc417 | 10751 | ncx->blk_eval.retop = cx->blk_eval.retop; |
1d7c1841 GS |
10752 | break; |
10753 | case CXt_LOOP: | |
10754 | ncx->blk_loop.label = cx->blk_loop.label; | |
10755 | ncx->blk_loop.resetsp = cx->blk_loop.resetsp; | |
10756 | ncx->blk_loop.redo_op = cx->blk_loop.redo_op; | |
10757 | ncx->blk_loop.next_op = cx->blk_loop.next_op; | |
10758 | ncx->blk_loop.last_op = cx->blk_loop.last_op; | |
10759 | ncx->blk_loop.iterdata = (CxPADLOOP(cx) | |
10760 | ? cx->blk_loop.iterdata | |
d2d73c3e | 10761 | : gv_dup((GV*)cx->blk_loop.iterdata, param)); |
f3548bdc DM |
10762 | ncx->blk_loop.oldcomppad |
10763 | = (PAD*)ptr_table_fetch(PL_ptr_table, | |
10764 | cx->blk_loop.oldcomppad); | |
d2d73c3e AB |
10765 | ncx->blk_loop.itersave = sv_dup_inc(cx->blk_loop.itersave, param); |
10766 | ncx->blk_loop.iterlval = sv_dup_inc(cx->blk_loop.iterlval, param); | |
10767 | ncx->blk_loop.iterary = av_dup_inc(cx->blk_loop.iterary, param); | |
1d7c1841 GS |
10768 | ncx->blk_loop.iterix = cx->blk_loop.iterix; |
10769 | ncx->blk_loop.itermax = cx->blk_loop.itermax; | |
10770 | break; | |
10771 | case CXt_FORMAT: | |
d2d73c3e AB |
10772 | ncx->blk_sub.cv = cv_dup(cx->blk_sub.cv, param); |
10773 | ncx->blk_sub.gv = gv_dup(cx->blk_sub.gv, param); | |
10774 | ncx->blk_sub.dfoutgv = gv_dup_inc(cx->blk_sub.dfoutgv, param); | |
1d7c1841 | 10775 | ncx->blk_sub.hasargs = cx->blk_sub.hasargs; |
f39bc417 | 10776 | ncx->blk_sub.retop = cx->blk_sub.retop; |
1d7c1841 GS |
10777 | break; |
10778 | case CXt_BLOCK: | |
10779 | case CXt_NULL: | |
10780 | break; | |
10781 | } | |
10782 | } | |
10783 | --ix; | |
10784 | } | |
10785 | return ncxs; | |
10786 | } | |
10787 | ||
645c22ef DM |
10788 | /* duplicate a stack info structure */ |
10789 | ||
1d7c1841 | 10790 | PERL_SI * |
a8fc9800 | 10791 | Perl_si_dup(pTHX_ PERL_SI *si, CLONE_PARAMS* param) |
1d7c1841 GS |
10792 | { |
10793 | PERL_SI *nsi; | |
10794 | ||
10795 | if (!si) | |
10796 | return (PERL_SI*)NULL; | |
10797 | ||
10798 | /* look for it in the table first */ | |
10799 | nsi = (PERL_SI*)ptr_table_fetch(PL_ptr_table, si); | |
10800 | if (nsi) | |
10801 | return nsi; | |
10802 | ||
10803 | /* create anew and remember what it is */ | |
10804 | Newz(56, nsi, 1, PERL_SI); | |
10805 | ptr_table_store(PL_ptr_table, si, nsi); | |
10806 | ||
d2d73c3e | 10807 | nsi->si_stack = av_dup_inc(si->si_stack, param); |
1d7c1841 GS |
10808 | nsi->si_cxix = si->si_cxix; |
10809 | nsi->si_cxmax = si->si_cxmax; | |
d2d73c3e | 10810 | nsi->si_cxstack = cx_dup(si->si_cxstack, si->si_cxix, si->si_cxmax, param); |
1d7c1841 | 10811 | nsi->si_type = si->si_type; |
d2d73c3e AB |
10812 | nsi->si_prev = si_dup(si->si_prev, param); |
10813 | nsi->si_next = si_dup(si->si_next, param); | |
1d7c1841 GS |
10814 | nsi->si_markoff = si->si_markoff; |
10815 | ||
10816 | return nsi; | |
10817 | } | |
10818 | ||
10819 | #define POPINT(ss,ix) ((ss)[--(ix)].any_i32) | |
10820 | #define TOPINT(ss,ix) ((ss)[ix].any_i32) | |
10821 | #define POPLONG(ss,ix) ((ss)[--(ix)].any_long) | |
10822 | #define TOPLONG(ss,ix) ((ss)[ix].any_long) | |
10823 | #define POPIV(ss,ix) ((ss)[--(ix)].any_iv) | |
10824 | #define TOPIV(ss,ix) ((ss)[ix].any_iv) | |
38d8b13e HS |
10825 | #define POPBOOL(ss,ix) ((ss)[--(ix)].any_bool) |
10826 | #define TOPBOOL(ss,ix) ((ss)[ix].any_bool) | |
1d7c1841 GS |
10827 | #define POPPTR(ss,ix) ((ss)[--(ix)].any_ptr) |
10828 | #define TOPPTR(ss,ix) ((ss)[ix].any_ptr) | |
10829 | #define POPDPTR(ss,ix) ((ss)[--(ix)].any_dptr) | |
10830 | #define TOPDPTR(ss,ix) ((ss)[ix].any_dptr) | |
10831 | #define POPDXPTR(ss,ix) ((ss)[--(ix)].any_dxptr) | |
10832 | #define TOPDXPTR(ss,ix) ((ss)[ix].any_dxptr) | |
10833 | ||
10834 | /* XXXXX todo */ | |
10835 | #define pv_dup_inc(p) SAVEPV(p) | |
10836 | #define pv_dup(p) SAVEPV(p) | |
10837 | #define svp_dup_inc(p,pp) any_dup(p,pp) | |
10838 | ||
645c22ef DM |
10839 | /* map any object to the new equivent - either something in the |
10840 | * ptr table, or something in the interpreter structure | |
10841 | */ | |
10842 | ||
1d7c1841 | 10843 | void * |
53c1dcc0 | 10844 | Perl_any_dup(pTHX_ void *v, const PerlInterpreter *proto_perl) |
1d7c1841 GS |
10845 | { |
10846 | void *ret; | |
10847 | ||
10848 | if (!v) | |
10849 | return (void*)NULL; | |
10850 | ||
10851 | /* look for it in the table first */ | |
10852 | ret = ptr_table_fetch(PL_ptr_table, v); | |
10853 | if (ret) | |
10854 | return ret; | |
10855 | ||
10856 | /* see if it is part of the interpreter structure */ | |
10857 | if (v >= (void*)proto_perl && v < (void*)(proto_perl+1)) | |
acfe0abc | 10858 | ret = (void*)(((char*)aTHX) + (((char*)v) - (char*)proto_perl)); |
05ec9bb3 | 10859 | else { |
1d7c1841 | 10860 | ret = v; |
05ec9bb3 | 10861 | } |
1d7c1841 GS |
10862 | |
10863 | return ret; | |
10864 | } | |
10865 | ||
645c22ef DM |
10866 | /* duplicate the save stack */ |
10867 | ||
1d7c1841 | 10868 | ANY * |
a8fc9800 | 10869 | Perl_ss_dup(pTHX_ PerlInterpreter *proto_perl, CLONE_PARAMS* param) |
1d7c1841 | 10870 | { |
53c1dcc0 AL |
10871 | ANY * const ss = proto_perl->Tsavestack; |
10872 | const I32 max = proto_perl->Tsavestack_max; | |
10873 | I32 ix = proto_perl->Tsavestack_ix; | |
1d7c1841 GS |
10874 | ANY *nss; |
10875 | SV *sv; | |
10876 | GV *gv; | |
10877 | AV *av; | |
10878 | HV *hv; | |
10879 | void* ptr; | |
10880 | int intval; | |
10881 | long longval; | |
10882 | GP *gp; | |
10883 | IV iv; | |
c4e33207 | 10884 | char *c = NULL; |
1d7c1841 | 10885 | void (*dptr) (void*); |
acfe0abc | 10886 | void (*dxptr) (pTHX_ void*); |
1d7c1841 GS |
10887 | |
10888 | Newz(54, nss, max, ANY); | |
10889 | ||
10890 | while (ix > 0) { | |
b464bac0 | 10891 | I32 i = POPINT(ss,ix); |
1d7c1841 GS |
10892 | TOPINT(nss,ix) = i; |
10893 | switch (i) { | |
10894 | case SAVEt_ITEM: /* normal string */ | |
10895 | sv = (SV*)POPPTR(ss,ix); | |
d2d73c3e | 10896 | TOPPTR(nss,ix) = sv_dup_inc(sv, param); |
1d7c1841 | 10897 | sv = (SV*)POPPTR(ss,ix); |
d2d73c3e | 10898 | TOPPTR(nss,ix) = sv_dup_inc(sv, param); |
1d7c1841 GS |
10899 | break; |
10900 | case SAVEt_SV: /* scalar reference */ | |
10901 | sv = (SV*)POPPTR(ss,ix); | |
d2d73c3e | 10902 | TOPPTR(nss,ix) = sv_dup_inc(sv, param); |
1d7c1841 | 10903 | gv = (GV*)POPPTR(ss,ix); |
d2d73c3e | 10904 | TOPPTR(nss,ix) = gv_dup_inc(gv, param); |
1d7c1841 | 10905 | break; |
f4dd75d9 GS |
10906 | case SAVEt_GENERIC_PVREF: /* generic char* */ |
10907 | c = (char*)POPPTR(ss,ix); | |
10908 | TOPPTR(nss,ix) = pv_dup(c); | |
10909 | ptr = POPPTR(ss,ix); | |
10910 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
10911 | break; | |
05ec9bb3 NIS |
10912 | case SAVEt_SHARED_PVREF: /* char* in shared space */ |
10913 | c = (char*)POPPTR(ss,ix); | |
10914 | TOPPTR(nss,ix) = savesharedpv(c); | |
10915 | ptr = POPPTR(ss,ix); | |
10916 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
10917 | break; | |
1d7c1841 GS |
10918 | case SAVEt_GENERIC_SVREF: /* generic sv */ |
10919 | case SAVEt_SVREF: /* scalar reference */ | |
10920 | sv = (SV*)POPPTR(ss,ix); | |
d2d73c3e | 10921 | TOPPTR(nss,ix) = sv_dup_inc(sv, param); |
1d7c1841 GS |
10922 | ptr = POPPTR(ss,ix); |
10923 | TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */ | |
10924 | break; | |
10925 | case SAVEt_AV: /* array reference */ | |
10926 | av = (AV*)POPPTR(ss,ix); | |
d2d73c3e | 10927 | TOPPTR(nss,ix) = av_dup_inc(av, param); |
1d7c1841 | 10928 | gv = (GV*)POPPTR(ss,ix); |
d2d73c3e | 10929 | TOPPTR(nss,ix) = gv_dup(gv, param); |
1d7c1841 GS |
10930 | break; |
10931 | case SAVEt_HV: /* hash reference */ | |
10932 | hv = (HV*)POPPTR(ss,ix); | |
d2d73c3e | 10933 | TOPPTR(nss,ix) = hv_dup_inc(hv, param); |
1d7c1841 | 10934 | gv = (GV*)POPPTR(ss,ix); |
d2d73c3e | 10935 | TOPPTR(nss,ix) = gv_dup(gv, param); |
1d7c1841 GS |
10936 | break; |
10937 | case SAVEt_INT: /* int reference */ | |
10938 | ptr = POPPTR(ss,ix); | |
10939 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
10940 | intval = (int)POPINT(ss,ix); | |
10941 | TOPINT(nss,ix) = intval; | |
10942 | break; | |
10943 | case SAVEt_LONG: /* long reference */ | |
10944 | ptr = POPPTR(ss,ix); | |
10945 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
10946 | longval = (long)POPLONG(ss,ix); | |
10947 | TOPLONG(nss,ix) = longval; | |
10948 | break; | |
10949 | case SAVEt_I32: /* I32 reference */ | |
10950 | case SAVEt_I16: /* I16 reference */ | |
10951 | case SAVEt_I8: /* I8 reference */ | |
10952 | ptr = POPPTR(ss,ix); | |
10953 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
10954 | i = POPINT(ss,ix); | |
10955 | TOPINT(nss,ix) = i; | |
10956 | break; | |
10957 | case SAVEt_IV: /* IV reference */ | |
10958 | ptr = POPPTR(ss,ix); | |
10959 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
10960 | iv = POPIV(ss,ix); | |
10961 | TOPIV(nss,ix) = iv; | |
10962 | break; | |
10963 | case SAVEt_SPTR: /* SV* reference */ | |
10964 | ptr = POPPTR(ss,ix); | |
10965 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
10966 | sv = (SV*)POPPTR(ss,ix); | |
d2d73c3e | 10967 | TOPPTR(nss,ix) = sv_dup(sv, param); |
1d7c1841 GS |
10968 | break; |
10969 | case SAVEt_VPTR: /* random* reference */ | |
10970 | ptr = POPPTR(ss,ix); | |
10971 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
10972 | ptr = POPPTR(ss,ix); | |
10973 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
10974 | break; | |
10975 | case SAVEt_PPTR: /* char* reference */ | |
10976 | ptr = POPPTR(ss,ix); | |
10977 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
10978 | c = (char*)POPPTR(ss,ix); | |
10979 | TOPPTR(nss,ix) = pv_dup(c); | |
10980 | break; | |
10981 | case SAVEt_HPTR: /* HV* reference */ | |
10982 | ptr = POPPTR(ss,ix); | |
10983 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
10984 | hv = (HV*)POPPTR(ss,ix); | |
d2d73c3e | 10985 | TOPPTR(nss,ix) = hv_dup(hv, param); |
1d7c1841 GS |
10986 | break; |
10987 | case SAVEt_APTR: /* AV* reference */ | |
10988 | ptr = POPPTR(ss,ix); | |
10989 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
10990 | av = (AV*)POPPTR(ss,ix); | |
d2d73c3e | 10991 | TOPPTR(nss,ix) = av_dup(av, param); |
1d7c1841 GS |
10992 | break; |
10993 | case SAVEt_NSTAB: | |
10994 | gv = (GV*)POPPTR(ss,ix); | |
d2d73c3e | 10995 | TOPPTR(nss,ix) = gv_dup(gv, param); |
1d7c1841 GS |
10996 | break; |
10997 | case SAVEt_GP: /* scalar reference */ | |
10998 | gp = (GP*)POPPTR(ss,ix); | |
d2d73c3e | 10999 | TOPPTR(nss,ix) = gp = gp_dup(gp, param); |
1d7c1841 GS |
11000 | (void)GpREFCNT_inc(gp); |
11001 | gv = (GV*)POPPTR(ss,ix); | |
2ed3c8fc | 11002 | TOPPTR(nss,ix) = gv_dup_inc(gv, param); |
1d7c1841 GS |
11003 | c = (char*)POPPTR(ss,ix); |
11004 | TOPPTR(nss,ix) = pv_dup(c); | |
11005 | iv = POPIV(ss,ix); | |
11006 | TOPIV(nss,ix) = iv; | |
11007 | iv = POPIV(ss,ix); | |
11008 | TOPIV(nss,ix) = iv; | |
11009 | break; | |
11010 | case SAVEt_FREESV: | |
26d9b02f | 11011 | case SAVEt_MORTALIZESV: |
1d7c1841 | 11012 | sv = (SV*)POPPTR(ss,ix); |
d2d73c3e | 11013 | TOPPTR(nss,ix) = sv_dup_inc(sv, param); |
1d7c1841 GS |
11014 | break; |
11015 | case SAVEt_FREEOP: | |
11016 | ptr = POPPTR(ss,ix); | |
11017 | if (ptr && (((OP*)ptr)->op_private & OPpREFCOUNTED)) { | |
11018 | /* these are assumed to be refcounted properly */ | |
53c1dcc0 | 11019 | OP *o; |
1d7c1841 GS |
11020 | switch (((OP*)ptr)->op_type) { |
11021 | case OP_LEAVESUB: | |
11022 | case OP_LEAVESUBLV: | |
11023 | case OP_LEAVEEVAL: | |
11024 | case OP_LEAVE: | |
11025 | case OP_SCOPE: | |
11026 | case OP_LEAVEWRITE: | |
e977893f GS |
11027 | TOPPTR(nss,ix) = ptr; |
11028 | o = (OP*)ptr; | |
11029 | OpREFCNT_inc(o); | |
1d7c1841 GS |
11030 | break; |
11031 | default: | |
11032 | TOPPTR(nss,ix) = Nullop; | |
11033 | break; | |
11034 | } | |
11035 | } | |
11036 | else | |
11037 | TOPPTR(nss,ix) = Nullop; | |
11038 | break; | |
11039 | case SAVEt_FREEPV: | |
11040 | c = (char*)POPPTR(ss,ix); | |
11041 | TOPPTR(nss,ix) = pv_dup_inc(c); | |
11042 | break; | |
11043 | case SAVEt_CLEARSV: | |
11044 | longval = POPLONG(ss,ix); | |
11045 | TOPLONG(nss,ix) = longval; | |
11046 | break; | |
11047 | case SAVEt_DELETE: | |
11048 | hv = (HV*)POPPTR(ss,ix); | |
d2d73c3e | 11049 | TOPPTR(nss,ix) = hv_dup_inc(hv, param); |
1d7c1841 GS |
11050 | c = (char*)POPPTR(ss,ix); |
11051 | TOPPTR(nss,ix) = pv_dup_inc(c); | |
11052 | i = POPINT(ss,ix); | |
11053 | TOPINT(nss,ix) = i; | |
11054 | break; | |
11055 | case SAVEt_DESTRUCTOR: | |
11056 | ptr = POPPTR(ss,ix); | |
11057 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */ | |
11058 | dptr = POPDPTR(ss,ix); | |
8141890a JH |
11059 | TOPDPTR(nss,ix) = DPTR2FPTR(void (*)(void*), |
11060 | any_dup(FPTR2DPTR(void *, dptr), | |
11061 | proto_perl)); | |
1d7c1841 GS |
11062 | break; |
11063 | case SAVEt_DESTRUCTOR_X: | |
11064 | ptr = POPPTR(ss,ix); | |
11065 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */ | |
11066 | dxptr = POPDXPTR(ss,ix); | |
8141890a JH |
11067 | TOPDXPTR(nss,ix) = DPTR2FPTR(void (*)(pTHX_ void*), |
11068 | any_dup(FPTR2DPTR(void *, dxptr), | |
11069 | proto_perl)); | |
1d7c1841 GS |
11070 | break; |
11071 | case SAVEt_REGCONTEXT: | |
11072 | case SAVEt_ALLOC: | |
11073 | i = POPINT(ss,ix); | |
11074 | TOPINT(nss,ix) = i; | |
11075 | ix -= i; | |
11076 | break; | |
11077 | case SAVEt_STACK_POS: /* Position on Perl stack */ | |
11078 | i = POPINT(ss,ix); | |
11079 | TOPINT(nss,ix) = i; | |
11080 | break; | |
11081 | case SAVEt_AELEM: /* array element */ | |
11082 | sv = (SV*)POPPTR(ss,ix); | |
d2d73c3e | 11083 | TOPPTR(nss,ix) = sv_dup_inc(sv, param); |
1d7c1841 GS |
11084 | i = POPINT(ss,ix); |
11085 | TOPINT(nss,ix) = i; | |
11086 | av = (AV*)POPPTR(ss,ix); | |
d2d73c3e | 11087 | TOPPTR(nss,ix) = av_dup_inc(av, param); |
1d7c1841 GS |
11088 | break; |
11089 | case SAVEt_HELEM: /* hash element */ | |
11090 | sv = (SV*)POPPTR(ss,ix); | |
d2d73c3e | 11091 | TOPPTR(nss,ix) = sv_dup_inc(sv, param); |
1d7c1841 | 11092 | sv = (SV*)POPPTR(ss,ix); |
d2d73c3e | 11093 | TOPPTR(nss,ix) = sv_dup_inc(sv, param); |
1d7c1841 | 11094 | hv = (HV*)POPPTR(ss,ix); |
d2d73c3e | 11095 | TOPPTR(nss,ix) = hv_dup_inc(hv, param); |
1d7c1841 GS |
11096 | break; |
11097 | case SAVEt_OP: | |
11098 | ptr = POPPTR(ss,ix); | |
11099 | TOPPTR(nss,ix) = ptr; | |
11100 | break; | |
11101 | case SAVEt_HINTS: | |
11102 | i = POPINT(ss,ix); | |
11103 | TOPINT(nss,ix) = i; | |
11104 | break; | |
c4410b1b GS |
11105 | case SAVEt_COMPPAD: |
11106 | av = (AV*)POPPTR(ss,ix); | |
58ed4fbe | 11107 | TOPPTR(nss,ix) = av_dup(av, param); |
c4410b1b | 11108 | break; |
c3564e5c GS |
11109 | case SAVEt_PADSV: |
11110 | longval = (long)POPLONG(ss,ix); | |
11111 | TOPLONG(nss,ix) = longval; | |
11112 | ptr = POPPTR(ss,ix); | |
11113 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
11114 | sv = (SV*)POPPTR(ss,ix); | |
d2d73c3e | 11115 | TOPPTR(nss,ix) = sv_dup(sv, param); |
c3564e5c | 11116 | break; |
a1bb4754 | 11117 | case SAVEt_BOOL: |
38d8b13e | 11118 | ptr = POPPTR(ss,ix); |
b9609c01 | 11119 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); |
38d8b13e | 11120 | longval = (long)POPBOOL(ss,ix); |
b9609c01 | 11121 | TOPBOOL(nss,ix) = (bool)longval; |
a1bb4754 | 11122 | break; |
8bd2680e MHM |
11123 | case SAVEt_SET_SVFLAGS: |
11124 | i = POPINT(ss,ix); | |
11125 | TOPINT(nss,ix) = i; | |
11126 | i = POPINT(ss,ix); | |
11127 | TOPINT(nss,ix) = i; | |
11128 | sv = (SV*)POPPTR(ss,ix); | |
11129 | TOPPTR(nss,ix) = sv_dup(sv, param); | |
11130 | break; | |
1d7c1841 GS |
11131 | default: |
11132 | Perl_croak(aTHX_ "panic: ss_dup inconsistency"); | |
11133 | } | |
11134 | } | |
11135 | ||
11136 | return nss; | |
11137 | } | |
11138 | ||
9660f481 DM |
11139 | |
11140 | /* if sv is a stash, call $class->CLONE_SKIP(), and set the SVphv_CLONEABLE | |
11141 | * flag to the result. This is done for each stash before cloning starts, | |
11142 | * so we know which stashes want their objects cloned */ | |
11143 | ||
11144 | static void | |
11145 | do_mark_cloneable_stash(pTHX_ SV *sv) | |
11146 | { | |
53c1dcc0 | 11147 | const HEK * const hvname = HvNAME_HEK((HV*)sv); |
bfcb3514 | 11148 | if (hvname) { |
53c1dcc0 | 11149 | GV* const cloner = gv_fetchmethod_autoload((HV*)sv, "CLONE_SKIP", 0); |
9660f481 DM |
11150 | SvFLAGS(sv) |= SVphv_CLONEABLE; /* clone objects by default */ |
11151 | if (cloner && GvCV(cloner)) { | |
11152 | dSP; | |
11153 | UV status; | |
11154 | ||
11155 | ENTER; | |
11156 | SAVETMPS; | |
11157 | PUSHMARK(SP); | |
84bda14a | 11158 | XPUSHs(sv_2mortal(newSVhek(hvname))); |
9660f481 DM |
11159 | PUTBACK; |
11160 | call_sv((SV*)GvCV(cloner), G_SCALAR); | |
11161 | SPAGAIN; | |
11162 | status = POPu; | |
11163 | PUTBACK; | |
11164 | FREETMPS; | |
11165 | LEAVE; | |
11166 | if (status) | |
11167 | SvFLAGS(sv) &= ~SVphv_CLONEABLE; | |
11168 | } | |
11169 | } | |
11170 | } | |
11171 | ||
11172 | ||
11173 | ||
645c22ef DM |
11174 | /* |
11175 | =for apidoc perl_clone | |
11176 | ||
11177 | Create and return a new interpreter by cloning the current one. | |
11178 | ||
4be49ee6 | 11179 | perl_clone takes these flags as parameters: |
6a78b4db | 11180 | |
7a5fa8a2 NIS |
11181 | CLONEf_COPY_STACKS - is used to, well, copy the stacks also, |
11182 | without it we only clone the data and zero the stacks, | |
11183 | with it we copy the stacks and the new perl interpreter is | |
11184 | ready to run at the exact same point as the previous one. | |
11185 | The pseudo-fork code uses COPY_STACKS while the | |
6a78b4db AB |
11186 | threads->new doesn't. |
11187 | ||
11188 | CLONEf_KEEP_PTR_TABLE | |
7a5fa8a2 NIS |
11189 | perl_clone keeps a ptr_table with the pointer of the old |
11190 | variable as a key and the new variable as a value, | |
11191 | this allows it to check if something has been cloned and not | |
11192 | clone it again but rather just use the value and increase the | |
11193 | refcount. If KEEP_PTR_TABLE is not set then perl_clone will kill | |
11194 | the ptr_table using the function | |
11195 | C<ptr_table_free(PL_ptr_table); PL_ptr_table = NULL;>, | |
11196 | reason to keep it around is if you want to dup some of your own | |
11197 | variable who are outside the graph perl scans, example of this | |
6a78b4db AB |
11198 | code is in threads.xs create |
11199 | ||
11200 | CLONEf_CLONE_HOST | |
7a5fa8a2 NIS |
11201 | This is a win32 thing, it is ignored on unix, it tells perls |
11202 | win32host code (which is c++) to clone itself, this is needed on | |
11203 | win32 if you want to run two threads at the same time, | |
11204 | if you just want to do some stuff in a separate perl interpreter | |
11205 | and then throw it away and return to the original one, | |
6a78b4db AB |
11206 | you don't need to do anything. |
11207 | ||
645c22ef DM |
11208 | =cut |
11209 | */ | |
11210 | ||
11211 | /* XXX the above needs expanding by someone who actually understands it ! */ | |
3fc56081 NK |
11212 | EXTERN_C PerlInterpreter * |
11213 | perl_clone_host(PerlInterpreter* proto_perl, UV flags); | |
645c22ef | 11214 | |
1d7c1841 GS |
11215 | PerlInterpreter * |
11216 | perl_clone(PerlInterpreter *proto_perl, UV flags) | |
11217 | { | |
27da23d5 | 11218 | dVAR; |
1d7c1841 | 11219 | #ifdef PERL_IMPLICIT_SYS |
c43294b8 AB |
11220 | |
11221 | /* perlhost.h so we need to call into it | |
11222 | to clone the host, CPerlHost should have a c interface, sky */ | |
11223 | ||
11224 | if (flags & CLONEf_CLONE_HOST) { | |
11225 | return perl_clone_host(proto_perl,flags); | |
11226 | } | |
11227 | return perl_clone_using(proto_perl, flags, | |
1d7c1841 GS |
11228 | proto_perl->IMem, |
11229 | proto_perl->IMemShared, | |
11230 | proto_perl->IMemParse, | |
11231 | proto_perl->IEnv, | |
11232 | proto_perl->IStdIO, | |
11233 | proto_perl->ILIO, | |
11234 | proto_perl->IDir, | |
11235 | proto_perl->ISock, | |
11236 | proto_perl->IProc); | |
11237 | } | |
11238 | ||
11239 | PerlInterpreter * | |
11240 | perl_clone_using(PerlInterpreter *proto_perl, UV flags, | |
11241 | struct IPerlMem* ipM, struct IPerlMem* ipMS, | |
11242 | struct IPerlMem* ipMP, struct IPerlEnv* ipE, | |
11243 | struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO, | |
11244 | struct IPerlDir* ipD, struct IPerlSock* ipS, | |
11245 | struct IPerlProc* ipP) | |
11246 | { | |
11247 | /* XXX many of the string copies here can be optimized if they're | |
11248 | * constants; they need to be allocated as common memory and just | |
11249 | * their pointers copied. */ | |
11250 | ||
8fc9efbd | 11251 | IV i; |
64aa0685 GS |
11252 | CLONE_PARAMS clone_params; |
11253 | CLONE_PARAMS* param = &clone_params; | |
d2d73c3e | 11254 | |
1d7c1841 | 11255 | PerlInterpreter *my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter)); |
9660f481 DM |
11256 | /* for each stash, determine whether its objects should be cloned */ |
11257 | S_visit(proto_perl, do_mark_cloneable_stash, SVt_PVHV, SVTYPEMASK); | |
ba869deb | 11258 | PERL_SET_THX(my_perl); |
1d7c1841 | 11259 | |
acfe0abc | 11260 | # ifdef DEBUGGING |
a4530404 | 11261 | Poison(my_perl, 1, PerlInterpreter); |
fd0854ff | 11262 | PL_op = Nullop; |
c008732b | 11263 | PL_curcop = (COP *)Nullop; |
1d7c1841 GS |
11264 | PL_markstack = 0; |
11265 | PL_scopestack = 0; | |
11266 | PL_savestack = 0; | |
22f7c9c9 JH |
11267 | PL_savestack_ix = 0; |
11268 | PL_savestack_max = -1; | |
66fe0623 | 11269 | PL_sig_pending = 0; |
25596c82 | 11270 | Zero(&PL_debug_pad, 1, struct perl_debug_pad); |
acfe0abc | 11271 | # else /* !DEBUGGING */ |
1d7c1841 | 11272 | Zero(my_perl, 1, PerlInterpreter); |
acfe0abc | 11273 | # endif /* DEBUGGING */ |
1d7c1841 GS |
11274 | |
11275 | /* host pointers */ | |
11276 | PL_Mem = ipM; | |
11277 | PL_MemShared = ipMS; | |
11278 | PL_MemParse = ipMP; | |
11279 | PL_Env = ipE; | |
11280 | PL_StdIO = ipStd; | |
11281 | PL_LIO = ipLIO; | |
11282 | PL_Dir = ipD; | |
11283 | PL_Sock = ipS; | |
11284 | PL_Proc = ipP; | |
1d7c1841 GS |
11285 | #else /* !PERL_IMPLICIT_SYS */ |
11286 | IV i; | |
64aa0685 GS |
11287 | CLONE_PARAMS clone_params; |
11288 | CLONE_PARAMS* param = &clone_params; | |
1d7c1841 | 11289 | PerlInterpreter *my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter)); |
9660f481 DM |
11290 | /* for each stash, determine whether its objects should be cloned */ |
11291 | S_visit(proto_perl, do_mark_cloneable_stash, SVt_PVHV, SVTYPEMASK); | |
ba869deb | 11292 | PERL_SET_THX(my_perl); |
1d7c1841 GS |
11293 | |
11294 | # ifdef DEBUGGING | |
a4530404 | 11295 | Poison(my_perl, 1, PerlInterpreter); |
fd0854ff | 11296 | PL_op = Nullop; |
c008732b | 11297 | PL_curcop = (COP *)Nullop; |
1d7c1841 GS |
11298 | PL_markstack = 0; |
11299 | PL_scopestack = 0; | |
11300 | PL_savestack = 0; | |
22f7c9c9 JH |
11301 | PL_savestack_ix = 0; |
11302 | PL_savestack_max = -1; | |
66fe0623 | 11303 | PL_sig_pending = 0; |
25596c82 | 11304 | Zero(&PL_debug_pad, 1, struct perl_debug_pad); |
1d7c1841 GS |
11305 | # else /* !DEBUGGING */ |
11306 | Zero(my_perl, 1, PerlInterpreter); | |
11307 | # endif /* DEBUGGING */ | |
11308 | #endif /* PERL_IMPLICIT_SYS */ | |
83236556 | 11309 | param->flags = flags; |
59b40662 | 11310 | param->proto_perl = proto_perl; |
1d7c1841 GS |
11311 | |
11312 | /* arena roots */ | |
612f20c3 | 11313 | PL_xnv_arenaroot = NULL; |
1d7c1841 | 11314 | PL_xnv_root = NULL; |
612f20c3 | 11315 | PL_xpv_arenaroot = NULL; |
1d7c1841 | 11316 | PL_xpv_root = NULL; |
612f20c3 | 11317 | PL_xpviv_arenaroot = NULL; |
1d7c1841 | 11318 | PL_xpviv_root = NULL; |
612f20c3 | 11319 | PL_xpvnv_arenaroot = NULL; |
1d7c1841 | 11320 | PL_xpvnv_root = NULL; |
612f20c3 | 11321 | PL_xpvcv_arenaroot = NULL; |
1d7c1841 | 11322 | PL_xpvcv_root = NULL; |
612f20c3 | 11323 | PL_xpvav_arenaroot = NULL; |
1d7c1841 | 11324 | PL_xpvav_root = NULL; |
612f20c3 | 11325 | PL_xpvhv_arenaroot = NULL; |
1d7c1841 | 11326 | PL_xpvhv_root = NULL; |
612f20c3 | 11327 | PL_xpvmg_arenaroot = NULL; |
1d7c1841 | 11328 | PL_xpvmg_root = NULL; |
7552b40b DM |
11329 | PL_xpvgv_arenaroot = NULL; |
11330 | PL_xpvgv_root = NULL; | |
612f20c3 | 11331 | PL_xpvlv_arenaroot = NULL; |
1d7c1841 | 11332 | PL_xpvlv_root = NULL; |
612f20c3 | 11333 | PL_xpvbm_arenaroot = NULL; |
1d7c1841 | 11334 | PL_xpvbm_root = NULL; |
612f20c3 | 11335 | PL_he_arenaroot = NULL; |
1d7c1841 | 11336 | PL_he_root = NULL; |
892b45be | 11337 | #if defined(USE_ITHREADS) |
32e691d0 NC |
11338 | PL_pte_arenaroot = NULL; |
11339 | PL_pte_root = NULL; | |
892b45be | 11340 | #endif |
1d7c1841 GS |
11341 | PL_nice_chunk = NULL; |
11342 | PL_nice_chunk_size = 0; | |
11343 | PL_sv_count = 0; | |
11344 | PL_sv_objcount = 0; | |
11345 | PL_sv_root = Nullsv; | |
11346 | PL_sv_arenaroot = Nullsv; | |
11347 | ||
11348 | PL_debug = proto_perl->Idebug; | |
11349 | ||
8df990a8 NC |
11350 | PL_hash_seed = proto_perl->Ihash_seed; |
11351 | PL_rehash_seed = proto_perl->Irehash_seed; | |
11352 | ||
e5dd39fc | 11353 | #ifdef USE_REENTRANT_API |
68853529 SB |
11354 | /* XXX: things like -Dm will segfault here in perlio, but doing |
11355 | * PERL_SET_CONTEXT(proto_perl); | |
11356 | * breaks too many other things | |
11357 | */ | |
59bd0823 | 11358 | Perl_reentrant_init(aTHX); |
e5dd39fc AB |
11359 | #endif |
11360 | ||
1d7c1841 GS |
11361 | /* create SV map for pointer relocation */ |
11362 | PL_ptr_table = ptr_table_new(); | |
11363 | ||
11364 | /* initialize these special pointers as early as possible */ | |
11365 | SvANY(&PL_sv_undef) = NULL; | |
11366 | SvREFCNT(&PL_sv_undef) = (~(U32)0)/2; | |
11367 | SvFLAGS(&PL_sv_undef) = SVf_READONLY|SVt_NULL; | |
11368 | ptr_table_store(PL_ptr_table, &proto_perl->Isv_undef, &PL_sv_undef); | |
11369 | ||
1d7c1841 | 11370 | SvANY(&PL_sv_no) = new_XPVNV(); |
1d7c1841 | 11371 | SvREFCNT(&PL_sv_no) = (~(U32)0)/2; |
0309f36e NC |
11372 | SvFLAGS(&PL_sv_no) = SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK |
11373 | |SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV; | |
f880fe2f | 11374 | SvPV_set(&PL_sv_no, SAVEPVN(PL_No, 0)); |
b162af07 SP |
11375 | SvCUR_set(&PL_sv_no, 0); |
11376 | SvLEN_set(&PL_sv_no, 1); | |
45977657 | 11377 | SvIV_set(&PL_sv_no, 0); |
9d6ce603 | 11378 | SvNV_set(&PL_sv_no, 0); |
1d7c1841 GS |
11379 | ptr_table_store(PL_ptr_table, &proto_perl->Isv_no, &PL_sv_no); |
11380 | ||
1d7c1841 | 11381 | SvANY(&PL_sv_yes) = new_XPVNV(); |
1d7c1841 | 11382 | SvREFCNT(&PL_sv_yes) = (~(U32)0)/2; |
0309f36e NC |
11383 | SvFLAGS(&PL_sv_yes) = SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK |
11384 | |SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV; | |
f880fe2f | 11385 | SvPV_set(&PL_sv_yes, SAVEPVN(PL_Yes, 1)); |
b162af07 SP |
11386 | SvCUR_set(&PL_sv_yes, 1); |
11387 | SvLEN_set(&PL_sv_yes, 2); | |
45977657 | 11388 | SvIV_set(&PL_sv_yes, 1); |
9d6ce603 | 11389 | SvNV_set(&PL_sv_yes, 1); |
1d7c1841 GS |
11390 | ptr_table_store(PL_ptr_table, &proto_perl->Isv_yes, &PL_sv_yes); |
11391 | ||
05ec9bb3 | 11392 | /* create (a non-shared!) shared string table */ |
1d7c1841 GS |
11393 | PL_strtab = newHV(); |
11394 | HvSHAREKEYS_off(PL_strtab); | |
c4a9c09d | 11395 | hv_ksplit(PL_strtab, HvTOTALKEYS(proto_perl->Istrtab)); |
1d7c1841 GS |
11396 | ptr_table_store(PL_ptr_table, proto_perl->Istrtab, PL_strtab); |
11397 | ||
05ec9bb3 NIS |
11398 | PL_compiling = proto_perl->Icompiling; |
11399 | ||
11400 | /* These two PVs will be free'd special way so must set them same way op.c does */ | |
11401 | PL_compiling.cop_stashpv = savesharedpv(PL_compiling.cop_stashpv); | |
11402 | ptr_table_store(PL_ptr_table, proto_perl->Icompiling.cop_stashpv, PL_compiling.cop_stashpv); | |
11403 | ||
11404 | PL_compiling.cop_file = savesharedpv(PL_compiling.cop_file); | |
11405 | ptr_table_store(PL_ptr_table, proto_perl->Icompiling.cop_file, PL_compiling.cop_file); | |
11406 | ||
1d7c1841 GS |
11407 | ptr_table_store(PL_ptr_table, &proto_perl->Icompiling, &PL_compiling); |
11408 | if (!specialWARN(PL_compiling.cop_warnings)) | |
d2d73c3e | 11409 | PL_compiling.cop_warnings = sv_dup_inc(PL_compiling.cop_warnings, param); |
ac27b0f5 | 11410 | if (!specialCopIO(PL_compiling.cop_io)) |
d2d73c3e | 11411 | PL_compiling.cop_io = sv_dup_inc(PL_compiling.cop_io, param); |
1d7c1841 GS |
11412 | PL_curcop = (COP*)any_dup(proto_perl->Tcurcop, proto_perl); |
11413 | ||
11414 | /* pseudo environmental stuff */ | |
11415 | PL_origargc = proto_perl->Iorigargc; | |
e2975953 | 11416 | PL_origargv = proto_perl->Iorigargv; |
d2d73c3e | 11417 | |
d2d73c3e AB |
11418 | param->stashes = newAV(); /* Setup array of objects to call clone on */ |
11419 | ||
a1ea730d | 11420 | #ifdef PERLIO_LAYERS |
3a1ee7e8 NIS |
11421 | /* Clone PerlIO tables as soon as we can handle general xx_dup() */ |
11422 | PerlIO_clone(aTHX_ proto_perl, param); | |
a1ea730d | 11423 | #endif |
d2d73c3e AB |
11424 | |
11425 | PL_envgv = gv_dup(proto_perl->Ienvgv, param); | |
11426 | PL_incgv = gv_dup(proto_perl->Iincgv, param); | |
11427 | PL_hintgv = gv_dup(proto_perl->Ihintgv, param); | |
1d7c1841 | 11428 | PL_origfilename = SAVEPV(proto_perl->Iorigfilename); |
d2d73c3e AB |
11429 | PL_diehook = sv_dup_inc(proto_perl->Idiehook, param); |
11430 | PL_warnhook = sv_dup_inc(proto_perl->Iwarnhook, param); | |
1d7c1841 GS |
11431 | |
11432 | /* switches */ | |
11433 | PL_minus_c = proto_perl->Iminus_c; | |
d2d73c3e | 11434 | PL_patchlevel = sv_dup_inc(proto_perl->Ipatchlevel, param); |
1d7c1841 GS |
11435 | PL_localpatches = proto_perl->Ilocalpatches; |
11436 | PL_splitstr = proto_perl->Isplitstr; | |
11437 | PL_preprocess = proto_perl->Ipreprocess; | |
11438 | PL_minus_n = proto_perl->Iminus_n; | |
11439 | PL_minus_p = proto_perl->Iminus_p; | |
11440 | PL_minus_l = proto_perl->Iminus_l; | |
11441 | PL_minus_a = proto_perl->Iminus_a; | |
11442 | PL_minus_F = proto_perl->Iminus_F; | |
11443 | PL_doswitches = proto_perl->Idoswitches; | |
11444 | PL_dowarn = proto_perl->Idowarn; | |
11445 | PL_doextract = proto_perl->Idoextract; | |
11446 | PL_sawampersand = proto_perl->Isawampersand; | |
11447 | PL_unsafe = proto_perl->Iunsafe; | |
11448 | PL_inplace = SAVEPV(proto_perl->Iinplace); | |
d2d73c3e | 11449 | PL_e_script = sv_dup_inc(proto_perl->Ie_script, param); |
1d7c1841 GS |
11450 | PL_perldb = proto_perl->Iperldb; |
11451 | PL_perl_destruct_level = proto_perl->Iperl_destruct_level; | |
1cbb0781 | 11452 | PL_exit_flags = proto_perl->Iexit_flags; |
1d7c1841 GS |
11453 | |
11454 | /* magical thingies */ | |
11455 | /* XXX time(&PL_basetime) when asked for? */ | |
11456 | PL_basetime = proto_perl->Ibasetime; | |
d2d73c3e | 11457 | PL_formfeed = sv_dup(proto_perl->Iformfeed, param); |
1d7c1841 GS |
11458 | |
11459 | PL_maxsysfd = proto_perl->Imaxsysfd; | |
11460 | PL_multiline = proto_perl->Imultiline; | |
11461 | PL_statusvalue = proto_perl->Istatusvalue; | |
11462 | #ifdef VMS | |
11463 | PL_statusvalue_vms = proto_perl->Istatusvalue_vms; | |
11464 | #endif | |
0a378802 | 11465 | PL_encoding = sv_dup(proto_perl->Iencoding, param); |
1d7c1841 | 11466 | |
4a4c6fe3 | 11467 | sv_setpvn(PERL_DEBUG_PAD(0), "", 0); /* For regex debugging. */ |
1f483ca1 JH |
11468 | sv_setpvn(PERL_DEBUG_PAD(1), "", 0); /* ext/re needs these */ |
11469 | sv_setpvn(PERL_DEBUG_PAD(2), "", 0); /* even without DEBUGGING. */ | |
4a4c6fe3 | 11470 | |
d2f185dc AMS |
11471 | /* Clone the regex array */ |
11472 | PL_regex_padav = newAV(); | |
11473 | { | |
a3b680e6 | 11474 | const I32 len = av_len((AV*)proto_perl->Iregex_padav); |
53c1dcc0 | 11475 | SV** const regexen = AvARRAY((AV*)proto_perl->Iregex_padav); |
b464bac0 | 11476 | IV i; |
0f95fc41 AB |
11477 | av_push(PL_regex_padav, |
11478 | sv_dup_inc(regexen[0],param)); | |
11479 | for(i = 1; i <= len; i++) { | |
11480 | if(SvREPADTMP(regexen[i])) { | |
11481 | av_push(PL_regex_padav, sv_dup_inc(regexen[i], param)); | |
8cf8f3d1 | 11482 | } else { |
0f95fc41 AB |
11483 | av_push(PL_regex_padav, |
11484 | SvREFCNT_inc( | |
8cf8f3d1 | 11485 | newSViv(PTR2IV(re_dup(INT2PTR(REGEXP *, |
cbfa9890 | 11486 | SvIVX(regexen[i])), param))) |
0f95fc41 AB |
11487 | )); |
11488 | } | |
d2f185dc AMS |
11489 | } |
11490 | } | |
11491 | PL_regex_pad = AvARRAY(PL_regex_padav); | |
1fcf4c12 | 11492 | |
1d7c1841 | 11493 | /* shortcuts to various I/O objects */ |
d2d73c3e AB |
11494 | PL_stdingv = gv_dup(proto_perl->Istdingv, param); |
11495 | PL_stderrgv = gv_dup(proto_perl->Istderrgv, param); | |
11496 | PL_defgv = gv_dup(proto_perl->Idefgv, param); | |
11497 | PL_argvgv = gv_dup(proto_perl->Iargvgv, param); | |
11498 | PL_argvoutgv = gv_dup(proto_perl->Iargvoutgv, param); | |
11499 | PL_argvout_stack = av_dup_inc(proto_perl->Iargvout_stack, param); | |
1d7c1841 GS |
11500 | |
11501 | /* shortcuts to regexp stuff */ | |
d2d73c3e | 11502 | PL_replgv = gv_dup(proto_perl->Ireplgv, param); |
1d7c1841 GS |
11503 | |
11504 | /* shortcuts to misc objects */ | |
d2d73c3e | 11505 | PL_errgv = gv_dup(proto_perl->Ierrgv, param); |
1d7c1841 GS |
11506 | |
11507 | /* shortcuts to debugging objects */ | |
d2d73c3e AB |
11508 | PL_DBgv = gv_dup(proto_perl->IDBgv, param); |
11509 | PL_DBline = gv_dup(proto_perl->IDBline, param); | |
11510 | PL_DBsub = gv_dup(proto_perl->IDBsub, param); | |
11511 | PL_DBsingle = sv_dup(proto_perl->IDBsingle, param); | |
11512 | PL_DBtrace = sv_dup(proto_perl->IDBtrace, param); | |
11513 | PL_DBsignal = sv_dup(proto_perl->IDBsignal, param); | |
06492da6 | 11514 | PL_DBassertion = sv_dup(proto_perl->IDBassertion, param); |
d2d73c3e AB |
11515 | PL_lineary = av_dup(proto_perl->Ilineary, param); |
11516 | PL_dbargs = av_dup(proto_perl->Idbargs, param); | |
1d7c1841 GS |
11517 | |
11518 | /* symbol tables */ | |
d2d73c3e AB |
11519 | PL_defstash = hv_dup_inc(proto_perl->Tdefstash, param); |
11520 | PL_curstash = hv_dup(proto_perl->Tcurstash, param); | |
d2d73c3e AB |
11521 | PL_debstash = hv_dup(proto_perl->Idebstash, param); |
11522 | PL_globalstash = hv_dup(proto_perl->Iglobalstash, param); | |
11523 | PL_curstname = sv_dup_inc(proto_perl->Icurstname, param); | |
11524 | ||
11525 | PL_beginav = av_dup_inc(proto_perl->Ibeginav, param); | |
ee1c5a4e | 11526 | PL_beginav_save = av_dup_inc(proto_perl->Ibeginav_save, param); |
ece599bd | 11527 | PL_checkav_save = av_dup_inc(proto_perl->Icheckav_save, param); |
d2d73c3e AB |
11528 | PL_endav = av_dup_inc(proto_perl->Iendav, param); |
11529 | PL_checkav = av_dup_inc(proto_perl->Icheckav, param); | |
11530 | PL_initav = av_dup_inc(proto_perl->Iinitav, param); | |
1d7c1841 GS |
11531 | |
11532 | PL_sub_generation = proto_perl->Isub_generation; | |
11533 | ||
11534 | /* funky return mechanisms */ | |
11535 | PL_forkprocess = proto_perl->Iforkprocess; | |
11536 | ||
11537 | /* subprocess state */ | |
d2d73c3e | 11538 | PL_fdpid = av_dup_inc(proto_perl->Ifdpid, param); |
1d7c1841 GS |
11539 | |
11540 | /* internal state */ | |
11541 | PL_tainting = proto_perl->Itainting; | |
7135f00b | 11542 | PL_taint_warn = proto_perl->Itaint_warn; |
1d7c1841 GS |
11543 | PL_maxo = proto_perl->Imaxo; |
11544 | if (proto_perl->Iop_mask) | |
11545 | PL_op_mask = SAVEPVN(proto_perl->Iop_mask, PL_maxo); | |
11546 | else | |
11547 | PL_op_mask = Nullch; | |
06492da6 | 11548 | /* PL_asserting = proto_perl->Iasserting; */ |
1d7c1841 GS |
11549 | |
11550 | /* current interpreter roots */ | |
d2d73c3e | 11551 | PL_main_cv = cv_dup_inc(proto_perl->Imain_cv, param); |
1d7c1841 GS |
11552 | PL_main_root = OpREFCNT_inc(proto_perl->Imain_root); |
11553 | PL_main_start = proto_perl->Imain_start; | |
e977893f | 11554 | PL_eval_root = proto_perl->Ieval_root; |
1d7c1841 GS |
11555 | PL_eval_start = proto_perl->Ieval_start; |
11556 | ||
11557 | /* runtime control stuff */ | |
11558 | PL_curcopdb = (COP*)any_dup(proto_perl->Icurcopdb, proto_perl); | |
11559 | PL_copline = proto_perl->Icopline; | |
11560 | ||
11561 | PL_filemode = proto_perl->Ifilemode; | |
11562 | PL_lastfd = proto_perl->Ilastfd; | |
11563 | PL_oldname = proto_perl->Ioldname; /* XXX not quite right */ | |
11564 | PL_Argv = NULL; | |
11565 | PL_Cmd = Nullch; | |
11566 | PL_gensym = proto_perl->Igensym; | |
11567 | PL_preambled = proto_perl->Ipreambled; | |
d2d73c3e | 11568 | PL_preambleav = av_dup_inc(proto_perl->Ipreambleav, param); |
1d7c1841 GS |
11569 | PL_laststatval = proto_perl->Ilaststatval; |
11570 | PL_laststype = proto_perl->Ilaststype; | |
11571 | PL_mess_sv = Nullsv; | |
11572 | ||
d2d73c3e | 11573 | PL_ors_sv = sv_dup_inc(proto_perl->Iors_sv, param); |
1d7c1841 GS |
11574 | |
11575 | /* interpreter atexit processing */ | |
11576 | PL_exitlistlen = proto_perl->Iexitlistlen; | |
11577 | if (PL_exitlistlen) { | |
11578 | New(0, PL_exitlist, PL_exitlistlen, PerlExitListEntry); | |
11579 | Copy(proto_perl->Iexitlist, PL_exitlist, PL_exitlistlen, PerlExitListEntry); | |
11580 | } | |
11581 | else | |
11582 | PL_exitlist = (PerlExitListEntry*)NULL; | |
d2d73c3e | 11583 | PL_modglobal = hv_dup_inc(proto_perl->Imodglobal, param); |
19e8ce8e AB |
11584 | PL_custom_op_names = hv_dup_inc(proto_perl->Icustom_op_names,param); |
11585 | PL_custom_op_descs = hv_dup_inc(proto_perl->Icustom_op_descs,param); | |
1d7c1841 GS |
11586 | |
11587 | PL_profiledata = NULL; | |
a8fc9800 | 11588 | PL_rsfp = fp_dup(proto_perl->Irsfp, '<', param); |
1d7c1841 | 11589 | /* PL_rsfp_filters entries have fake IoDIRP() */ |
d2d73c3e | 11590 | PL_rsfp_filters = av_dup_inc(proto_perl->Irsfp_filters, param); |
1d7c1841 | 11591 | |
d2d73c3e | 11592 | PL_compcv = cv_dup(proto_perl->Icompcv, param); |
dd2155a4 DM |
11593 | |
11594 | PAD_CLONE_VARS(proto_perl, param); | |
1d7c1841 GS |
11595 | |
11596 | #ifdef HAVE_INTERP_INTERN | |
11597 | sys_intern_dup(&proto_perl->Isys_intern, &PL_sys_intern); | |
11598 | #endif | |
11599 | ||
11600 | /* more statics moved here */ | |
11601 | PL_generation = proto_perl->Igeneration; | |
d2d73c3e | 11602 | PL_DBcv = cv_dup(proto_perl->IDBcv, param); |
1d7c1841 GS |
11603 | |
11604 | PL_in_clean_objs = proto_perl->Iin_clean_objs; | |
11605 | PL_in_clean_all = proto_perl->Iin_clean_all; | |
11606 | ||
11607 | PL_uid = proto_perl->Iuid; | |
11608 | PL_euid = proto_perl->Ieuid; | |
11609 | PL_gid = proto_perl->Igid; | |
11610 | PL_egid = proto_perl->Iegid; | |
11611 | PL_nomemok = proto_perl->Inomemok; | |
11612 | PL_an = proto_perl->Ian; | |
1d7c1841 GS |
11613 | PL_evalseq = proto_perl->Ievalseq; |
11614 | PL_origenviron = proto_perl->Iorigenviron; /* XXX not quite right */ | |
11615 | PL_origalen = proto_perl->Iorigalen; | |
11616 | PL_pidstatus = newHV(); /* XXX flag for cloning? */ | |
11617 | PL_osname = SAVEPV(proto_perl->Iosname); | |
5c728af0 | 11618 | PL_sh_path_compat = proto_perl->Ish_path_compat; /* XXX never deallocated */ |
1d7c1841 GS |
11619 | PL_sighandlerp = proto_perl->Isighandlerp; |
11620 | ||
11621 | ||
11622 | PL_runops = proto_perl->Irunops; | |
11623 | ||
11624 | Copy(proto_perl->Itokenbuf, PL_tokenbuf, 256, char); | |
11625 | ||
11626 | #ifdef CSH | |
11627 | PL_cshlen = proto_perl->Icshlen; | |
74f1b2b8 | 11628 | PL_cshname = proto_perl->Icshname; /* XXX never deallocated */ |
1d7c1841 GS |
11629 | #endif |
11630 | ||
11631 | PL_lex_state = proto_perl->Ilex_state; | |
11632 | PL_lex_defer = proto_perl->Ilex_defer; | |
11633 | PL_lex_expect = proto_perl->Ilex_expect; | |
11634 | PL_lex_formbrack = proto_perl->Ilex_formbrack; | |
11635 | PL_lex_dojoin = proto_perl->Ilex_dojoin; | |
11636 | PL_lex_starts = proto_perl->Ilex_starts; | |
d2d73c3e AB |
11637 | PL_lex_stuff = sv_dup_inc(proto_perl->Ilex_stuff, param); |
11638 | PL_lex_repl = sv_dup_inc(proto_perl->Ilex_repl, param); | |
1d7c1841 GS |
11639 | PL_lex_op = proto_perl->Ilex_op; |
11640 | PL_lex_inpat = proto_perl->Ilex_inpat; | |
11641 | PL_lex_inwhat = proto_perl->Ilex_inwhat; | |
11642 | PL_lex_brackets = proto_perl->Ilex_brackets; | |
11643 | i = (PL_lex_brackets < 120 ? 120 : PL_lex_brackets); | |
11644 | PL_lex_brackstack = SAVEPVN(proto_perl->Ilex_brackstack,i); | |
11645 | PL_lex_casemods = proto_perl->Ilex_casemods; | |
11646 | i = (PL_lex_casemods < 12 ? 12 : PL_lex_casemods); | |
11647 | PL_lex_casestack = SAVEPVN(proto_perl->Ilex_casestack,i); | |
11648 | ||
11649 | Copy(proto_perl->Inextval, PL_nextval, 5, YYSTYPE); | |
11650 | Copy(proto_perl->Inexttype, PL_nexttype, 5, I32); | |
11651 | PL_nexttoke = proto_perl->Inexttoke; | |
11652 | ||
1d773130 TB |
11653 | /* XXX This is probably masking the deeper issue of why |
11654 | * SvANY(proto_perl->Ilinestr) can be NULL at this point. For test case: | |
11655 | * http://archive.develooper.com/perl5-porters%40perl.org/msg83298.html | |
11656 | * (A little debugging with a watchpoint on it may help.) | |
11657 | */ | |
389edf32 TB |
11658 | if (SvANY(proto_perl->Ilinestr)) { |
11659 | PL_linestr = sv_dup_inc(proto_perl->Ilinestr, param); | |
3f7c398e | 11660 | i = proto_perl->Ibufptr - SvPVX_const(proto_perl->Ilinestr); |
389edf32 | 11661 | PL_bufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i); |
3f7c398e | 11662 | i = proto_perl->Ioldbufptr - SvPVX_const(proto_perl->Ilinestr); |
389edf32 | 11663 | PL_oldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i); |
3f7c398e | 11664 | i = proto_perl->Ioldoldbufptr - SvPVX_const(proto_perl->Ilinestr); |
389edf32 | 11665 | PL_oldoldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i); |
3f7c398e | 11666 | i = proto_perl->Ilinestart - SvPVX_const(proto_perl->Ilinestr); |
389edf32 TB |
11667 | PL_linestart = SvPVX(PL_linestr) + (i < 0 ? 0 : i); |
11668 | } | |
11669 | else { | |
11670 | PL_linestr = NEWSV(65,79); | |
11671 | sv_upgrade(PL_linestr,SVt_PVIV); | |
11672 | sv_setpvn(PL_linestr,"",0); | |
11673 | PL_bufptr = PL_oldbufptr = PL_oldoldbufptr = PL_linestart = SvPVX(PL_linestr); | |
11674 | } | |
1d7c1841 | 11675 | PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr); |
1d7c1841 GS |
11676 | PL_pending_ident = proto_perl->Ipending_ident; |
11677 | PL_sublex_info = proto_perl->Isublex_info; /* XXX not quite right */ | |
11678 | ||
11679 | PL_expect = proto_perl->Iexpect; | |
11680 | ||
11681 | PL_multi_start = proto_perl->Imulti_start; | |
11682 | PL_multi_end = proto_perl->Imulti_end; | |
11683 | PL_multi_open = proto_perl->Imulti_open; | |
11684 | PL_multi_close = proto_perl->Imulti_close; | |
11685 | ||
11686 | PL_error_count = proto_perl->Ierror_count; | |
11687 | PL_subline = proto_perl->Isubline; | |
d2d73c3e | 11688 | PL_subname = sv_dup_inc(proto_perl->Isubname, param); |
1d7c1841 | 11689 | |
1d773130 | 11690 | /* XXX See comment on SvANY(proto_perl->Ilinestr) above */ |
389edf32 | 11691 | if (SvANY(proto_perl->Ilinestr)) { |
3f7c398e | 11692 | i = proto_perl->Ilast_uni - SvPVX_const(proto_perl->Ilinestr); |
389edf32 | 11693 | PL_last_uni = SvPVX(PL_linestr) + (i < 0 ? 0 : i); |
3f7c398e | 11694 | i = proto_perl->Ilast_lop - SvPVX_const(proto_perl->Ilinestr); |
389edf32 TB |
11695 | PL_last_lop = SvPVX(PL_linestr) + (i < 0 ? 0 : i); |
11696 | PL_last_lop_op = proto_perl->Ilast_lop_op; | |
11697 | } | |
11698 | else { | |
11699 | PL_last_uni = SvPVX(PL_linestr); | |
11700 | PL_last_lop = SvPVX(PL_linestr); | |
11701 | PL_last_lop_op = 0; | |
11702 | } | |
1d7c1841 | 11703 | PL_in_my = proto_perl->Iin_my; |
d2d73c3e | 11704 | PL_in_my_stash = hv_dup(proto_perl->Iin_my_stash, param); |
1d7c1841 GS |
11705 | #ifdef FCRYPT |
11706 | PL_cryptseen = proto_perl->Icryptseen; | |
11707 | #endif | |
11708 | ||
11709 | PL_hints = proto_perl->Ihints; | |
11710 | ||
11711 | PL_amagic_generation = proto_perl->Iamagic_generation; | |
11712 | ||
11713 | #ifdef USE_LOCALE_COLLATE | |
11714 | PL_collation_ix = proto_perl->Icollation_ix; | |
11715 | PL_collation_name = SAVEPV(proto_perl->Icollation_name); | |
11716 | PL_collation_standard = proto_perl->Icollation_standard; | |
11717 | PL_collxfrm_base = proto_perl->Icollxfrm_base; | |
11718 | PL_collxfrm_mult = proto_perl->Icollxfrm_mult; | |
11719 | #endif /* USE_LOCALE_COLLATE */ | |
11720 | ||
11721 | #ifdef USE_LOCALE_NUMERIC | |
11722 | PL_numeric_name = SAVEPV(proto_perl->Inumeric_name); | |
11723 | PL_numeric_standard = proto_perl->Inumeric_standard; | |
11724 | PL_numeric_local = proto_perl->Inumeric_local; | |
d2d73c3e | 11725 | PL_numeric_radix_sv = sv_dup_inc(proto_perl->Inumeric_radix_sv, param); |
1d7c1841 GS |
11726 | #endif /* !USE_LOCALE_NUMERIC */ |
11727 | ||
11728 | /* utf8 character classes */ | |
d2d73c3e AB |
11729 | PL_utf8_alnum = sv_dup_inc(proto_perl->Iutf8_alnum, param); |
11730 | PL_utf8_alnumc = sv_dup_inc(proto_perl->Iutf8_alnumc, param); | |
11731 | PL_utf8_ascii = sv_dup_inc(proto_perl->Iutf8_ascii, param); | |
11732 | PL_utf8_alpha = sv_dup_inc(proto_perl->Iutf8_alpha, param); | |
11733 | PL_utf8_space = sv_dup_inc(proto_perl->Iutf8_space, param); | |
11734 | PL_utf8_cntrl = sv_dup_inc(proto_perl->Iutf8_cntrl, param); | |
11735 | PL_utf8_graph = sv_dup_inc(proto_perl->Iutf8_graph, param); | |
11736 | PL_utf8_digit = sv_dup_inc(proto_perl->Iutf8_digit, param); | |
11737 | PL_utf8_upper = sv_dup_inc(proto_perl->Iutf8_upper, param); | |
11738 | PL_utf8_lower = sv_dup_inc(proto_perl->Iutf8_lower, param); | |
11739 | PL_utf8_print = sv_dup_inc(proto_perl->Iutf8_print, param); | |
11740 | PL_utf8_punct = sv_dup_inc(proto_perl->Iutf8_punct, param); | |
11741 | PL_utf8_xdigit = sv_dup_inc(proto_perl->Iutf8_xdigit, param); | |
11742 | PL_utf8_mark = sv_dup_inc(proto_perl->Iutf8_mark, param); | |
11743 | PL_utf8_toupper = sv_dup_inc(proto_perl->Iutf8_toupper, param); | |
11744 | PL_utf8_totitle = sv_dup_inc(proto_perl->Iutf8_totitle, param); | |
11745 | PL_utf8_tolower = sv_dup_inc(proto_perl->Iutf8_tolower, param); | |
b4e400f9 | 11746 | PL_utf8_tofold = sv_dup_inc(proto_perl->Iutf8_tofold, param); |
82686b01 JH |
11747 | PL_utf8_idstart = sv_dup_inc(proto_perl->Iutf8_idstart, param); |
11748 | PL_utf8_idcont = sv_dup_inc(proto_perl->Iutf8_idcont, param); | |
1d7c1841 | 11749 | |
6c3182a5 | 11750 | /* Did the locale setup indicate UTF-8? */ |
9769094f | 11751 | PL_utf8locale = proto_perl->Iutf8locale; |
6c3182a5 JH |
11752 | /* Unicode features (see perlrun/-C) */ |
11753 | PL_unicode = proto_perl->Iunicode; | |
11754 | ||
11755 | /* Pre-5.8 signals control */ | |
11756 | PL_signals = proto_perl->Isignals; | |
11757 | ||
11758 | /* times() ticks per second */ | |
11759 | PL_clocktick = proto_perl->Iclocktick; | |
11760 | ||
11761 | /* Recursion stopper for PerlIO_find_layer */ | |
11762 | PL_in_load_module = proto_perl->Iin_load_module; | |
11763 | ||
11764 | /* sort() routine */ | |
11765 | PL_sort_RealCmp = proto_perl->Isort_RealCmp; | |
11766 | ||
57c6e6d2 JH |
11767 | /* Not really needed/useful since the reenrant_retint is "volatile", |
11768 | * but do it for consistency's sake. */ | |
11769 | PL_reentrant_retint = proto_perl->Ireentrant_retint; | |
11770 | ||
15a5279a JH |
11771 | /* Hooks to shared SVs and locks. */ |
11772 | PL_sharehook = proto_perl->Isharehook; | |
11773 | PL_lockhook = proto_perl->Ilockhook; | |
11774 | PL_unlockhook = proto_perl->Iunlockhook; | |
11775 | PL_threadhook = proto_perl->Ithreadhook; | |
11776 | ||
bce260cd JH |
11777 | PL_runops_std = proto_perl->Irunops_std; |
11778 | PL_runops_dbg = proto_perl->Irunops_dbg; | |
11779 | ||
11780 | #ifdef THREADS_HAVE_PIDS | |
11781 | PL_ppid = proto_perl->Ippid; | |
11782 | #endif | |
11783 | ||
1d7c1841 GS |
11784 | /* swatch cache */ |
11785 | PL_last_swash_hv = Nullhv; /* reinits on demand */ | |
11786 | PL_last_swash_klen = 0; | |
11787 | PL_last_swash_key[0]= '\0'; | |
11788 | PL_last_swash_tmps = (U8*)NULL; | |
11789 | PL_last_swash_slen = 0; | |
11790 | ||
1d7c1841 GS |
11791 | PL_glob_index = proto_perl->Iglob_index; |
11792 | PL_srand_called = proto_perl->Isrand_called; | |
11793 | PL_uudmap['M'] = 0; /* reinits on demand */ | |
11794 | PL_bitcount = Nullch; /* reinits on demand */ | |
11795 | ||
66fe0623 NIS |
11796 | if (proto_perl->Ipsig_pend) { |
11797 | Newz(0, PL_psig_pend, SIG_SIZE, int); | |
9dd79c3f | 11798 | } |
66fe0623 NIS |
11799 | else { |
11800 | PL_psig_pend = (int*)NULL; | |
11801 | } | |
11802 | ||
1d7c1841 | 11803 | if (proto_perl->Ipsig_ptr) { |
76d3c696 JH |
11804 | Newz(0, PL_psig_ptr, SIG_SIZE, SV*); |
11805 | Newz(0, PL_psig_name, SIG_SIZE, SV*); | |
76d3c696 | 11806 | for (i = 1; i < SIG_SIZE; i++) { |
d2d73c3e AB |
11807 | PL_psig_ptr[i] = sv_dup_inc(proto_perl->Ipsig_ptr[i], param); |
11808 | PL_psig_name[i] = sv_dup_inc(proto_perl->Ipsig_name[i], param); | |
1d7c1841 GS |
11809 | } |
11810 | } | |
11811 | else { | |
11812 | PL_psig_ptr = (SV**)NULL; | |
11813 | PL_psig_name = (SV**)NULL; | |
11814 | } | |
11815 | ||
11816 | /* thrdvar.h stuff */ | |
11817 | ||
a0739874 | 11818 | if (flags & CLONEf_COPY_STACKS) { |
1d7c1841 GS |
11819 | /* next allocation will be PL_tmps_stack[PL_tmps_ix+1] */ |
11820 | PL_tmps_ix = proto_perl->Ttmps_ix; | |
11821 | PL_tmps_max = proto_perl->Ttmps_max; | |
11822 | PL_tmps_floor = proto_perl->Ttmps_floor; | |
11823 | Newz(50, PL_tmps_stack, PL_tmps_max, SV*); | |
11824 | i = 0; | |
11825 | while (i <= PL_tmps_ix) { | |
d2d73c3e | 11826 | PL_tmps_stack[i] = sv_dup_inc(proto_perl->Ttmps_stack[i], param); |
1d7c1841 GS |
11827 | ++i; |
11828 | } | |
11829 | ||
11830 | /* next PUSHMARK() sets *(PL_markstack_ptr+1) */ | |
11831 | i = proto_perl->Tmarkstack_max - proto_perl->Tmarkstack; | |
11832 | Newz(54, PL_markstack, i, I32); | |
11833 | PL_markstack_max = PL_markstack + (proto_perl->Tmarkstack_max | |
11834 | - proto_perl->Tmarkstack); | |
11835 | PL_markstack_ptr = PL_markstack + (proto_perl->Tmarkstack_ptr | |
11836 | - proto_perl->Tmarkstack); | |
11837 | Copy(proto_perl->Tmarkstack, PL_markstack, | |
11838 | PL_markstack_ptr - PL_markstack + 1, I32); | |
11839 | ||
11840 | /* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix] | |
11841 | * NOTE: unlike the others! */ | |
11842 | PL_scopestack_ix = proto_perl->Tscopestack_ix; | |
11843 | PL_scopestack_max = proto_perl->Tscopestack_max; | |
11844 | Newz(54, PL_scopestack, PL_scopestack_max, I32); | |
11845 | Copy(proto_perl->Tscopestack, PL_scopestack, PL_scopestack_ix, I32); | |
11846 | ||
1d7c1841 | 11847 | /* NOTE: si_dup() looks at PL_markstack */ |
d2d73c3e | 11848 | PL_curstackinfo = si_dup(proto_perl->Tcurstackinfo, param); |
1d7c1841 GS |
11849 | |
11850 | /* PL_curstack = PL_curstackinfo->si_stack; */ | |
d2d73c3e AB |
11851 | PL_curstack = av_dup(proto_perl->Tcurstack, param); |
11852 | PL_mainstack = av_dup(proto_perl->Tmainstack, param); | |
1d7c1841 GS |
11853 | |
11854 | /* next PUSHs() etc. set *(PL_stack_sp+1) */ | |
11855 | PL_stack_base = AvARRAY(PL_curstack); | |
11856 | PL_stack_sp = PL_stack_base + (proto_perl->Tstack_sp | |
11857 | - proto_perl->Tstack_base); | |
11858 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); | |
11859 | ||
11860 | /* next SSPUSHFOO() sets PL_savestack[PL_savestack_ix] | |
11861 | * NOTE: unlike the others! */ | |
11862 | PL_savestack_ix = proto_perl->Tsavestack_ix; | |
11863 | PL_savestack_max = proto_perl->Tsavestack_max; | |
11864 | /*Newz(54, PL_savestack, PL_savestack_max, ANY);*/ | |
d2d73c3e | 11865 | PL_savestack = ss_dup(proto_perl, param); |
1d7c1841 GS |
11866 | } |
11867 | else { | |
11868 | init_stacks(); | |
985e7056 | 11869 | ENTER; /* perl_destruct() wants to LEAVE; */ |
1d7c1841 GS |
11870 | } |
11871 | ||
11872 | PL_start_env = proto_perl->Tstart_env; /* XXXXXX */ | |
11873 | PL_top_env = &PL_start_env; | |
11874 | ||
11875 | PL_op = proto_perl->Top; | |
11876 | ||
11877 | PL_Sv = Nullsv; | |
11878 | PL_Xpv = (XPV*)NULL; | |
11879 | PL_na = proto_perl->Tna; | |
11880 | ||
11881 | PL_statbuf = proto_perl->Tstatbuf; | |
11882 | PL_statcache = proto_perl->Tstatcache; | |
d2d73c3e AB |
11883 | PL_statgv = gv_dup(proto_perl->Tstatgv, param); |
11884 | PL_statname = sv_dup_inc(proto_perl->Tstatname, param); | |
1d7c1841 GS |
11885 | #ifdef HAS_TIMES |
11886 | PL_timesbuf = proto_perl->Ttimesbuf; | |
11887 | #endif | |
11888 | ||
11889 | PL_tainted = proto_perl->Ttainted; | |
11890 | PL_curpm = proto_perl->Tcurpm; /* XXX No PMOP ref count */ | |
d2d73c3e AB |
11891 | PL_rs = sv_dup_inc(proto_perl->Trs, param); |
11892 | PL_last_in_gv = gv_dup(proto_perl->Tlast_in_gv, param); | |
11893 | PL_ofs_sv = sv_dup_inc(proto_perl->Tofs_sv, param); | |
11894 | PL_defoutgv = gv_dup_inc(proto_perl->Tdefoutgv, param); | |
1d7c1841 | 11895 | PL_chopset = proto_perl->Tchopset; /* XXX never deallocated */ |
d2d73c3e AB |
11896 | PL_toptarget = sv_dup_inc(proto_perl->Ttoptarget, param); |
11897 | PL_bodytarget = sv_dup_inc(proto_perl->Tbodytarget, param); | |
11898 | PL_formtarget = sv_dup(proto_perl->Tformtarget, param); | |
1d7c1841 GS |
11899 | |
11900 | PL_restartop = proto_perl->Trestartop; | |
11901 | PL_in_eval = proto_perl->Tin_eval; | |
11902 | PL_delaymagic = proto_perl->Tdelaymagic; | |
11903 | PL_dirty = proto_perl->Tdirty; | |
11904 | PL_localizing = proto_perl->Tlocalizing; | |
11905 | ||
d2d73c3e | 11906 | PL_errors = sv_dup_inc(proto_perl->Terrors, param); |
dd28f7bb | 11907 | PL_hv_fetch_ent_mh = Nullhe; |
1d7c1841 GS |
11908 | PL_modcount = proto_perl->Tmodcount; |
11909 | PL_lastgotoprobe = Nullop; | |
11910 | PL_dumpindent = proto_perl->Tdumpindent; | |
11911 | ||
11912 | PL_sortcop = (OP*)any_dup(proto_perl->Tsortcop, proto_perl); | |
d2d73c3e AB |
11913 | PL_sortstash = hv_dup(proto_perl->Tsortstash, param); |
11914 | PL_firstgv = gv_dup(proto_perl->Tfirstgv, param); | |
11915 | PL_secondgv = gv_dup(proto_perl->Tsecondgv, param); | |
1d7c1841 GS |
11916 | PL_sortcxix = proto_perl->Tsortcxix; |
11917 | PL_efloatbuf = Nullch; /* reinits on demand */ | |
11918 | PL_efloatsize = 0; /* reinits on demand */ | |
11919 | ||
11920 | /* regex stuff */ | |
11921 | ||
11922 | PL_screamfirst = NULL; | |
11923 | PL_screamnext = NULL; | |
11924 | PL_maxscream = -1; /* reinits on demand */ | |
11925 | PL_lastscream = Nullsv; | |
11926 | ||
11927 | PL_watchaddr = NULL; | |
11928 | PL_watchok = Nullch; | |
11929 | ||
11930 | PL_regdummy = proto_perl->Tregdummy; | |
1d7c1841 GS |
11931 | PL_regprecomp = Nullch; |
11932 | PL_regnpar = 0; | |
11933 | PL_regsize = 0; | |
1d7c1841 GS |
11934 | PL_colorset = 0; /* reinits PL_colors[] */ |
11935 | /*PL_colors[6] = {0,0,0,0,0,0};*/ | |
1d7c1841 GS |
11936 | PL_reginput = Nullch; |
11937 | PL_regbol = Nullch; | |
11938 | PL_regeol = Nullch; | |
11939 | PL_regstartp = (I32*)NULL; | |
11940 | PL_regendp = (I32*)NULL; | |
11941 | PL_reglastparen = (U32*)NULL; | |
2d862feb | 11942 | PL_reglastcloseparen = (U32*)NULL; |
1d7c1841 | 11943 | PL_regtill = Nullch; |
1d7c1841 GS |
11944 | PL_reg_start_tmp = (char**)NULL; |
11945 | PL_reg_start_tmpl = 0; | |
11946 | PL_regdata = (struct reg_data*)NULL; | |
11947 | PL_bostr = Nullch; | |
11948 | PL_reg_flags = 0; | |
11949 | PL_reg_eval_set = 0; | |
11950 | PL_regnarrate = 0; | |
11951 | PL_regprogram = (regnode*)NULL; | |
11952 | PL_regindent = 0; | |
11953 | PL_regcc = (CURCUR*)NULL; | |
11954 | PL_reg_call_cc = (struct re_cc_state*)NULL; | |
11955 | PL_reg_re = (regexp*)NULL; | |
11956 | PL_reg_ganch = Nullch; | |
11957 | PL_reg_sv = Nullsv; | |
53c4c00c | 11958 | PL_reg_match_utf8 = FALSE; |
1d7c1841 GS |
11959 | PL_reg_magic = (MAGIC*)NULL; |
11960 | PL_reg_oldpos = 0; | |
11961 | PL_reg_oldcurpm = (PMOP*)NULL; | |
11962 | PL_reg_curpm = (PMOP*)NULL; | |
11963 | PL_reg_oldsaved = Nullch; | |
11964 | PL_reg_oldsavedlen = 0; | |
f8c7b90f | 11965 | #ifdef PERL_OLD_COPY_ON_WRITE |
504cff3b | 11966 | PL_nrs = Nullsv; |
ed252734 | 11967 | #endif |
1d7c1841 GS |
11968 | PL_reg_maxiter = 0; |
11969 | PL_reg_leftiter = 0; | |
11970 | PL_reg_poscache = Nullch; | |
11971 | PL_reg_poscache_size= 0; | |
11972 | ||
11973 | /* RE engine - function pointers */ | |
11974 | PL_regcompp = proto_perl->Tregcompp; | |
11975 | PL_regexecp = proto_perl->Tregexecp; | |
11976 | PL_regint_start = proto_perl->Tregint_start; | |
11977 | PL_regint_string = proto_perl->Tregint_string; | |
11978 | PL_regfree = proto_perl->Tregfree; | |
11979 | ||
11980 | PL_reginterp_cnt = 0; | |
11981 | PL_reg_starttry = 0; | |
11982 | ||
a2efc822 SC |
11983 | /* Pluggable optimizer */ |
11984 | PL_peepp = proto_perl->Tpeepp; | |
11985 | ||
081fc587 AB |
11986 | PL_stashcache = newHV(); |
11987 | ||
a0739874 DM |
11988 | if (!(flags & CLONEf_KEEP_PTR_TABLE)) { |
11989 | ptr_table_free(PL_ptr_table); | |
11990 | PL_ptr_table = NULL; | |
11991 | } | |
8cf8f3d1 | 11992 | |
f284b03f AMS |
11993 | /* Call the ->CLONE method, if it exists, for each of the stashes |
11994 | identified by sv_dup() above. | |
11995 | */ | |
d2d73c3e | 11996 | while(av_len(param->stashes) != -1) { |
53c1dcc0 AL |
11997 | HV* const stash = (HV*) av_shift(param->stashes); |
11998 | GV* const cloner = gv_fetchmethod_autoload(stash, "CLONE", 0); | |
f284b03f AMS |
11999 | if (cloner && GvCV(cloner)) { |
12000 | dSP; | |
12001 | ENTER; | |
12002 | SAVETMPS; | |
12003 | PUSHMARK(SP); | |
84bda14a | 12004 | XPUSHs(sv_2mortal(newSVhek(HvNAME_HEK(stash)))); |
f284b03f AMS |
12005 | PUTBACK; |
12006 | call_sv((SV*)GvCV(cloner), G_DISCARD); | |
12007 | FREETMPS; | |
12008 | LEAVE; | |
12009 | } | |
4a09accc | 12010 | } |
a0739874 | 12011 | |
dc507217 | 12012 | SvREFCNT_dec(param->stashes); |
dc507217 | 12013 | |
6d26897e DM |
12014 | /* orphaned? eg threads->new inside BEGIN or use */ |
12015 | if (PL_compcv && ! SvREFCNT(PL_compcv)) { | |
a3b680e6 | 12016 | (void)SvREFCNT_inc(PL_compcv); |
6d26897e DM |
12017 | SAVEFREESV(PL_compcv); |
12018 | } | |
12019 | ||
1d7c1841 | 12020 | return my_perl; |
1d7c1841 GS |
12021 | } |
12022 | ||
1d7c1841 | 12023 | #endif /* USE_ITHREADS */ |
a0ae6670 | 12024 | |
9f4817db | 12025 | /* |
ccfc67b7 JH |
12026 | =head1 Unicode Support |
12027 | ||
9f4817db JH |
12028 | =for apidoc sv_recode_to_utf8 |
12029 | ||
5d170f3a JH |
12030 | The encoding is assumed to be an Encode object, on entry the PV |
12031 | of the sv is assumed to be octets in that encoding, and the sv | |
12032 | will be converted into Unicode (and UTF-8). | |
9f4817db | 12033 | |
5d170f3a JH |
12034 | If the sv already is UTF-8 (or if it is not POK), or if the encoding |
12035 | is not a reference, nothing is done to the sv. If the encoding is not | |
1768d7eb JH |
12036 | an C<Encode::XS> Encoding object, bad things will happen. |
12037 | (See F<lib/encoding.pm> and L<Encode>). | |
9f4817db | 12038 | |
5d170f3a | 12039 | The PV of the sv is returned. |
9f4817db | 12040 | |
5d170f3a JH |
12041 | =cut */ |
12042 | ||
12043 | char * | |
12044 | Perl_sv_recode_to_utf8(pTHX_ SV *sv, SV *encoding) | |
12045 | { | |
27da23d5 | 12046 | dVAR; |
220e2d4e | 12047 | if (SvPOK(sv) && !SvUTF8(sv) && !IN_BYTES && SvROK(encoding)) { |
d0063567 DK |
12048 | SV *uni; |
12049 | STRLEN len; | |
93524f2b | 12050 | const char *s; |
d0063567 DK |
12051 | dSP; |
12052 | ENTER; | |
12053 | SAVETMPS; | |
220e2d4e | 12054 | save_re_context(); |
d0063567 DK |
12055 | PUSHMARK(sp); |
12056 | EXTEND(SP, 3); | |
12057 | XPUSHs(encoding); | |
12058 | XPUSHs(sv); | |
7a5fa8a2 | 12059 | /* |
f9893866 NIS |
12060 | NI-S 2002/07/09 |
12061 | Passing sv_yes is wrong - it needs to be or'ed set of constants | |
7a5fa8a2 | 12062 | for Encode::XS, while UTf-8 decode (currently) assumes a true value means |
f9893866 NIS |
12063 | remove converted chars from source. |
12064 | ||
12065 | Both will default the value - let them. | |
7a5fa8a2 | 12066 | |
d0063567 | 12067 | XPUSHs(&PL_sv_yes); |
f9893866 | 12068 | */ |
d0063567 DK |
12069 | PUTBACK; |
12070 | call_method("decode", G_SCALAR); | |
12071 | SPAGAIN; | |
12072 | uni = POPs; | |
12073 | PUTBACK; | |
93524f2b | 12074 | s = SvPV_const(uni, len); |
3f7c398e | 12075 | if (s != SvPVX_const(sv)) { |
d0063567 | 12076 | SvGROW(sv, len + 1); |
93524f2b | 12077 | Move(s, SvPVX(sv), len + 1, char); |
d0063567 | 12078 | SvCUR_set(sv, len); |
d0063567 DK |
12079 | } |
12080 | FREETMPS; | |
12081 | LEAVE; | |
d0063567 | 12082 | SvUTF8_on(sv); |
95899a2a | 12083 | return SvPVX(sv); |
f9893866 | 12084 | } |
95899a2a | 12085 | return SvPOKp(sv) ? SvPVX(sv) : NULL; |
9f4817db JH |
12086 | } |
12087 | ||
220e2d4e IH |
12088 | /* |
12089 | =for apidoc sv_cat_decode | |
12090 | ||
12091 | The encoding is assumed to be an Encode object, the PV of the ssv is | |
12092 | assumed to be octets in that encoding and decoding the input starts | |
12093 | from the position which (PV + *offset) pointed to. The dsv will be | |
12094 | concatenated the decoded UTF-8 string from ssv. Decoding will terminate | |
12095 | when the string tstr appears in decoding output or the input ends on | |
12096 | the PV of the ssv. The value which the offset points will be modified | |
12097 | to the last input position on the ssv. | |
68795e93 | 12098 | |
220e2d4e IH |
12099 | Returns TRUE if the terminator was found, else returns FALSE. |
12100 | ||
12101 | =cut */ | |
12102 | ||
12103 | bool | |
12104 | Perl_sv_cat_decode(pTHX_ SV *dsv, SV *encoding, | |
12105 | SV *ssv, int *offset, char *tstr, int tlen) | |
12106 | { | |
27da23d5 | 12107 | dVAR; |
a73e8557 | 12108 | bool ret = FALSE; |
220e2d4e | 12109 | if (SvPOK(ssv) && SvPOK(dsv) && SvROK(encoding) && offset) { |
220e2d4e IH |
12110 | SV *offsv; |
12111 | dSP; | |
12112 | ENTER; | |
12113 | SAVETMPS; | |
12114 | save_re_context(); | |
12115 | PUSHMARK(sp); | |
12116 | EXTEND(SP, 6); | |
12117 | XPUSHs(encoding); | |
12118 | XPUSHs(dsv); | |
12119 | XPUSHs(ssv); | |
12120 | XPUSHs(offsv = sv_2mortal(newSViv(*offset))); | |
12121 | XPUSHs(sv_2mortal(newSVpvn(tstr, tlen))); | |
12122 | PUTBACK; | |
12123 | call_method("cat_decode", G_SCALAR); | |
12124 | SPAGAIN; | |
12125 | ret = SvTRUE(TOPs); | |
12126 | *offset = SvIV(offsv); | |
12127 | PUTBACK; | |
12128 | FREETMPS; | |
12129 | LEAVE; | |
220e2d4e | 12130 | } |
a73e8557 JH |
12131 | else |
12132 | Perl_croak(aTHX_ "Invalid argument to sv_cat_decode"); | |
12133 | return ret; | |
220e2d4e | 12134 | } |
f9893866 | 12135 | |
241d1a3b NC |
12136 | /* |
12137 | * Local variables: | |
12138 | * c-indentation-style: bsd | |
12139 | * c-basic-offset: 4 | |
12140 | * indent-tabs-mode: t | |
12141 | * End: | |
12142 | * | |
37442d52 RGS |
12143 | * ex: set ts=8 sts=4 sw=4 noet: |
12144 | */ |