Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | /* sv.c |
79072805 | 2 | * |
bc89e66f | 3 | * Copyright (c) 1991-2001, Larry Wall |
79072805 LW |
4 | * |
5 | * You may distribute under the terms of either the GNU General Public | |
6 | * License or the Artistic License, as specified in the README file. | |
7 | * | |
a0d0e21e LW |
8 | */ |
9 | ||
10 | /* | |
11 | * "I wonder what the Entish is for 'yes' and 'no'," he thought. | |
79072805 LW |
12 | */ |
13 | ||
14 | #include "EXTERN.h" | |
864dbfa3 | 15 | #define PERL_IN_SV_C |
79072805 | 16 | #include "perl.h" |
79072805 | 17 | |
51371543 | 18 | #define FCALL *f |
6fc92669 | 19 | #define SV_CHECK_THINKFIRST(sv) if (SvTHINKFIRST(sv)) sv_force_normal(sv) |
2c5424a7 | 20 | |
51371543 GS |
21 | static void do_report_used(pTHXo_ SV *sv); |
22 | static void do_clean_objs(pTHXo_ SV *sv); | |
23 | #ifndef DISABLE_DESTRUCTOR_KLUDGE | |
24 | static void do_clean_named_objs(pTHXo_ SV *sv); | |
25 | #endif | |
26 | static void do_clean_all(pTHXo_ SV *sv); | |
27 | ||
4561caa4 CS |
28 | /* |
29 | * "A time to plant, and a time to uproot what was planted..." | |
30 | */ | |
31 | ||
053fc874 GS |
32 | #define plant_SV(p) \ |
33 | STMT_START { \ | |
34 | SvANY(p) = (void *)PL_sv_root; \ | |
35 | SvFLAGS(p) = SVTYPEMASK; \ | |
36 | PL_sv_root = (p); \ | |
37 | --PL_sv_count; \ | |
38 | } STMT_END | |
a0d0e21e | 39 | |
fba3b22e | 40 | /* sv_mutex must be held while calling uproot_SV() */ |
053fc874 GS |
41 | #define uproot_SV(p) \ |
42 | STMT_START { \ | |
43 | (p) = PL_sv_root; \ | |
44 | PL_sv_root = (SV*)SvANY(p); \ | |
45 | ++PL_sv_count; \ | |
46 | } STMT_END | |
47 | ||
48 | #define new_SV(p) \ | |
49 | STMT_START { \ | |
50 | LOCK_SV_MUTEX; \ | |
51 | if (PL_sv_root) \ | |
52 | uproot_SV(p); \ | |
53 | else \ | |
54 | (p) = more_sv(); \ | |
55 | UNLOCK_SV_MUTEX; \ | |
56 | SvANY(p) = 0; \ | |
57 | SvREFCNT(p) = 1; \ | |
58 | SvFLAGS(p) = 0; \ | |
59 | } STMT_END | |
463ee0b2 | 60 | |
a0d0e21e | 61 | #ifdef DEBUGGING |
4561caa4 | 62 | |
053fc874 GS |
63 | #define del_SV(p) \ |
64 | STMT_START { \ | |
65 | LOCK_SV_MUTEX; \ | |
aea4f609 | 66 | if (DEBUG_D_TEST) \ |
053fc874 GS |
67 | del_sv(p); \ |
68 | else \ | |
69 | plant_SV(p); \ | |
70 | UNLOCK_SV_MUTEX; \ | |
71 | } STMT_END | |
a0d0e21e | 72 | |
76e3520e | 73 | STATIC void |
cea2e8a9 | 74 | S_del_sv(pTHX_ SV *p) |
463ee0b2 | 75 | { |
aea4f609 | 76 | if (DEBUG_D_TEST) { |
4633a7c4 | 77 | SV* sva; |
a0d0e21e LW |
78 | SV* sv; |
79 | SV* svend; | |
80 | int ok = 0; | |
3280af22 | 81 | for (sva = PL_sv_arenaroot; sva; sva = (SV *) SvANY(sva)) { |
4633a7c4 LW |
82 | sv = sva + 1; |
83 | svend = &sva[SvREFCNT(sva)]; | |
a0d0e21e LW |
84 | if (p >= sv && p < svend) |
85 | ok = 1; | |
86 | } | |
87 | if (!ok) { | |
0453d815 PM |
88 | if (ckWARN_d(WARN_INTERNAL)) |
89 | Perl_warner(aTHX_ WARN_INTERNAL, | |
1d7c1841 GS |
90 | "Attempt to free non-arena SV: 0x%"UVxf, |
91 | PTR2UV(p)); | |
a0d0e21e LW |
92 | return; |
93 | } | |
94 | } | |
4561caa4 | 95 | plant_SV(p); |
463ee0b2 | 96 | } |
a0d0e21e | 97 | |
4561caa4 CS |
98 | #else /* ! DEBUGGING */ |
99 | ||
100 | #define del_SV(p) plant_SV(p) | |
101 | ||
102 | #endif /* DEBUGGING */ | |
463ee0b2 | 103 | |
4633a7c4 | 104 | void |
864dbfa3 | 105 | Perl_sv_add_arena(pTHX_ char *ptr, U32 size, U32 flags) |
463ee0b2 | 106 | { |
4633a7c4 | 107 | SV* sva = (SV*)ptr; |
463ee0b2 LW |
108 | register SV* sv; |
109 | register SV* svend; | |
14dd3ad8 | 110 | Zero(ptr, size, char); |
4633a7c4 LW |
111 | |
112 | /* The first SV in an arena isn't an SV. */ | |
3280af22 | 113 | SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */ |
4633a7c4 LW |
114 | SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */ |
115 | SvFLAGS(sva) = flags; /* FAKE if not to be freed */ | |
116 | ||
3280af22 NIS |
117 | PL_sv_arenaroot = sva; |
118 | PL_sv_root = sva + 1; | |
4633a7c4 LW |
119 | |
120 | svend = &sva[SvREFCNT(sva) - 1]; | |
121 | sv = sva + 1; | |
463ee0b2 | 122 | while (sv < svend) { |
a0d0e21e | 123 | SvANY(sv) = (void *)(SV*)(sv + 1); |
8990e307 | 124 | SvFLAGS(sv) = SVTYPEMASK; |
463ee0b2 LW |
125 | sv++; |
126 | } | |
127 | SvANY(sv) = 0; | |
4633a7c4 LW |
128 | SvFLAGS(sv) = SVTYPEMASK; |
129 | } | |
130 | ||
fba3b22e | 131 | /* sv_mutex must be held while calling more_sv() */ |
76e3520e | 132 | STATIC SV* |
cea2e8a9 | 133 | S_more_sv(pTHX) |
4633a7c4 | 134 | { |
4561caa4 CS |
135 | register SV* sv; |
136 | ||
3280af22 NIS |
137 | if (PL_nice_chunk) { |
138 | sv_add_arena(PL_nice_chunk, PL_nice_chunk_size, 0); | |
139 | PL_nice_chunk = Nullch; | |
30ad99e7 | 140 | PL_nice_chunk_size = 0; |
c07a80fd | 141 | } |
1edc1566 | 142 | else { |
143 | char *chunk; /* must use New here to match call to */ | |
144 | New(704,chunk,1008,char); /* Safefree() in sv_free_arenas() */ | |
145 | sv_add_arena(chunk, 1008, 0); | |
146 | } | |
4561caa4 CS |
147 | uproot_SV(sv); |
148 | return sv; | |
463ee0b2 LW |
149 | } |
150 | ||
5226ed68 | 151 | STATIC I32 |
cea2e8a9 | 152 | S_visit(pTHX_ SVFUNC_t f) |
8990e307 | 153 | { |
4633a7c4 | 154 | SV* sva; |
8990e307 LW |
155 | SV* sv; |
156 | register SV* svend; | |
5226ed68 | 157 | I32 visited = 0; |
8990e307 | 158 | |
3280af22 | 159 | for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) { |
4633a7c4 | 160 | svend = &sva[SvREFCNT(sva)]; |
4561caa4 | 161 | for (sv = sva + 1; sv < svend; ++sv) { |
f25c30a3 | 162 | if (SvTYPE(sv) != SVTYPEMASK && SvREFCNT(sv)) { |
51371543 | 163 | (FCALL)(aTHXo_ sv); |
5226ed68 JH |
164 | ++visited; |
165 | } | |
8990e307 LW |
166 | } |
167 | } | |
5226ed68 | 168 | return visited; |
8990e307 LW |
169 | } |
170 | ||
171 | void | |
864dbfa3 | 172 | Perl_sv_report_used(pTHX) |
4561caa4 | 173 | { |
0b94c7bb | 174 | visit(do_report_used); |
4561caa4 CS |
175 | } |
176 | ||
4561caa4 | 177 | void |
864dbfa3 | 178 | Perl_sv_clean_objs(pTHX) |
4561caa4 | 179 | { |
3280af22 | 180 | PL_in_clean_objs = TRUE; |
0b94c7bb | 181 | visit(do_clean_objs); |
4561caa4 | 182 | #ifndef DISABLE_DESTRUCTOR_KLUDGE |
2d0f3c12 | 183 | /* some barnacles may yet remain, clinging to typeglobs */ |
0b94c7bb | 184 | visit(do_clean_named_objs); |
4561caa4 | 185 | #endif |
3280af22 | 186 | PL_in_clean_objs = FALSE; |
4561caa4 CS |
187 | } |
188 | ||
5226ed68 | 189 | I32 |
864dbfa3 | 190 | Perl_sv_clean_all(pTHX) |
8990e307 | 191 | { |
5226ed68 | 192 | I32 cleaned; |
3280af22 | 193 | PL_in_clean_all = TRUE; |
5226ed68 | 194 | cleaned = visit(do_clean_all); |
3280af22 | 195 | PL_in_clean_all = FALSE; |
5226ed68 | 196 | return cleaned; |
8990e307 | 197 | } |
463ee0b2 | 198 | |
4633a7c4 | 199 | void |
864dbfa3 | 200 | Perl_sv_free_arenas(pTHX) |
4633a7c4 LW |
201 | { |
202 | SV* sva; | |
203 | SV* svanext; | |
612f20c3 | 204 | XPV *arena, *arenanext; |
4633a7c4 LW |
205 | |
206 | /* Free arenas here, but be careful about fake ones. (We assume | |
207 | contiguity of the fake ones with the corresponding real ones.) */ | |
208 | ||
3280af22 | 209 | for (sva = PL_sv_arenaroot; sva; sva = svanext) { |
4633a7c4 LW |
210 | svanext = (SV*) SvANY(sva); |
211 | while (svanext && SvFAKE(svanext)) | |
212 | svanext = (SV*) SvANY(svanext); | |
213 | ||
214 | if (!SvFAKE(sva)) | |
1edc1566 | 215 | Safefree((void *)sva); |
4633a7c4 | 216 | } |
5f05dabc | 217 | |
612f20c3 GS |
218 | for (arena = PL_xiv_arenaroot; arena; arena = arenanext) { |
219 | arenanext = (XPV*)arena->xpv_pv; | |
220 | Safefree(arena); | |
221 | } | |
222 | PL_xiv_arenaroot = 0; | |
223 | ||
224 | for (arena = PL_xnv_arenaroot; arena; arena = arenanext) { | |
225 | arenanext = (XPV*)arena->xpv_pv; | |
226 | Safefree(arena); | |
227 | } | |
228 | PL_xnv_arenaroot = 0; | |
229 | ||
230 | for (arena = PL_xrv_arenaroot; arena; arena = arenanext) { | |
231 | arenanext = (XPV*)arena->xpv_pv; | |
232 | Safefree(arena); | |
233 | } | |
234 | PL_xrv_arenaroot = 0; | |
235 | ||
236 | for (arena = PL_xpv_arenaroot; arena; arena = arenanext) { | |
237 | arenanext = (XPV*)arena->xpv_pv; | |
238 | Safefree(arena); | |
239 | } | |
240 | PL_xpv_arenaroot = 0; | |
241 | ||
242 | for (arena = (XPV*)PL_xpviv_arenaroot; arena; arena = arenanext) { | |
243 | arenanext = (XPV*)arena->xpv_pv; | |
244 | Safefree(arena); | |
245 | } | |
246 | PL_xpviv_arenaroot = 0; | |
247 | ||
248 | for (arena = (XPV*)PL_xpvnv_arenaroot; arena; arena = arenanext) { | |
249 | arenanext = (XPV*)arena->xpv_pv; | |
250 | Safefree(arena); | |
251 | } | |
252 | PL_xpvnv_arenaroot = 0; | |
253 | ||
254 | for (arena = (XPV*)PL_xpvcv_arenaroot; arena; arena = arenanext) { | |
255 | arenanext = (XPV*)arena->xpv_pv; | |
256 | Safefree(arena); | |
257 | } | |
258 | PL_xpvcv_arenaroot = 0; | |
259 | ||
260 | for (arena = (XPV*)PL_xpvav_arenaroot; arena; arena = arenanext) { | |
261 | arenanext = (XPV*)arena->xpv_pv; | |
262 | Safefree(arena); | |
263 | } | |
264 | PL_xpvav_arenaroot = 0; | |
265 | ||
266 | for (arena = (XPV*)PL_xpvhv_arenaroot; arena; arena = arenanext) { | |
267 | arenanext = (XPV*)arena->xpv_pv; | |
268 | Safefree(arena); | |
269 | } | |
270 | PL_xpvhv_arenaroot = 0; | |
271 | ||
272 | for (arena = (XPV*)PL_xpvmg_arenaroot; arena; arena = arenanext) { | |
273 | arenanext = (XPV*)arena->xpv_pv; | |
274 | Safefree(arena); | |
275 | } | |
276 | PL_xpvmg_arenaroot = 0; | |
277 | ||
278 | for (arena = (XPV*)PL_xpvlv_arenaroot; arena; arena = arenanext) { | |
279 | arenanext = (XPV*)arena->xpv_pv; | |
280 | Safefree(arena); | |
281 | } | |
282 | PL_xpvlv_arenaroot = 0; | |
283 | ||
284 | for (arena = (XPV*)PL_xpvbm_arenaroot; arena; arena = arenanext) { | |
285 | arenanext = (XPV*)arena->xpv_pv; | |
286 | Safefree(arena); | |
287 | } | |
288 | PL_xpvbm_arenaroot = 0; | |
289 | ||
290 | for (arena = (XPV*)PL_he_arenaroot; arena; arena = arenanext) { | |
291 | arenanext = (XPV*)arena->xpv_pv; | |
292 | Safefree(arena); | |
293 | } | |
294 | PL_he_arenaroot = 0; | |
295 | ||
3280af22 NIS |
296 | if (PL_nice_chunk) |
297 | Safefree(PL_nice_chunk); | |
298 | PL_nice_chunk = Nullch; | |
299 | PL_nice_chunk_size = 0; | |
300 | PL_sv_arenaroot = 0; | |
301 | PL_sv_root = 0; | |
4633a7c4 LW |
302 | } |
303 | ||
1d7c1841 GS |
304 | void |
305 | Perl_report_uninit(pTHX) | |
306 | { | |
307 | if (PL_op) | |
308 | Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit, | |
309 | " in ", PL_op_desc[PL_op->op_type]); | |
310 | else | |
311 | Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit, "", ""); | |
312 | } | |
313 | ||
76e3520e | 314 | STATIC XPVIV* |
cea2e8a9 | 315 | S_new_xiv(pTHX) |
463ee0b2 | 316 | { |
ea7c11a3 | 317 | IV* xiv; |
cbe51380 GS |
318 | LOCK_SV_MUTEX; |
319 | if (!PL_xiv_root) | |
320 | more_xiv(); | |
321 | xiv = PL_xiv_root; | |
322 | /* | |
323 | * See comment in more_xiv() -- RAM. | |
324 | */ | |
325 | PL_xiv_root = *(IV**)xiv; | |
326 | UNLOCK_SV_MUTEX; | |
327 | return (XPVIV*)((char*)xiv - STRUCT_OFFSET(XPVIV, xiv_iv)); | |
463ee0b2 LW |
328 | } |
329 | ||
76e3520e | 330 | STATIC void |
cea2e8a9 | 331 | S_del_xiv(pTHX_ XPVIV *p) |
463ee0b2 | 332 | { |
23e6a22f | 333 | IV* xiv = (IV*)((char*)(p) + STRUCT_OFFSET(XPVIV, xiv_iv)); |
cbe51380 | 334 | LOCK_SV_MUTEX; |
3280af22 NIS |
335 | *(IV**)xiv = PL_xiv_root; |
336 | PL_xiv_root = xiv; | |
cbe51380 | 337 | UNLOCK_SV_MUTEX; |
463ee0b2 LW |
338 | } |
339 | ||
cbe51380 | 340 | STATIC void |
cea2e8a9 | 341 | S_more_xiv(pTHX) |
463ee0b2 | 342 | { |
ea7c11a3 SM |
343 | register IV* xiv; |
344 | register IV* xivend; | |
8c52afec IZ |
345 | XPV* ptr; |
346 | New(705, ptr, 1008/sizeof(XPV), XPV); | |
3280af22 NIS |
347 | ptr->xpv_pv = (char*)PL_xiv_arenaroot; /* linked list of xiv arenas */ |
348 | PL_xiv_arenaroot = ptr; /* to keep Purify happy */ | |
a0d0e21e | 349 | |
ea7c11a3 SM |
350 | xiv = (IV*) ptr; |
351 | xivend = &xiv[1008 / sizeof(IV) - 1]; | |
352 | xiv += (sizeof(XPV) - 1) / sizeof(IV) + 1; /* fudge by size of XPV */ | |
3280af22 | 353 | PL_xiv_root = xiv; |
463ee0b2 | 354 | while (xiv < xivend) { |
ea7c11a3 | 355 | *(IV**)xiv = (IV *)(xiv + 1); |
463ee0b2 LW |
356 | xiv++; |
357 | } | |
ea7c11a3 | 358 | *(IV**)xiv = 0; |
463ee0b2 LW |
359 | } |
360 | ||
76e3520e | 361 | STATIC XPVNV* |
cea2e8a9 | 362 | S_new_xnv(pTHX) |
463ee0b2 | 363 | { |
65202027 | 364 | NV* xnv; |
cbe51380 GS |
365 | LOCK_SV_MUTEX; |
366 | if (!PL_xnv_root) | |
367 | more_xnv(); | |
368 | xnv = PL_xnv_root; | |
65202027 | 369 | PL_xnv_root = *(NV**)xnv; |
cbe51380 GS |
370 | UNLOCK_SV_MUTEX; |
371 | return (XPVNV*)((char*)xnv - STRUCT_OFFSET(XPVNV, xnv_nv)); | |
463ee0b2 LW |
372 | } |
373 | ||
76e3520e | 374 | STATIC void |
cea2e8a9 | 375 | S_del_xnv(pTHX_ XPVNV *p) |
463ee0b2 | 376 | { |
65202027 | 377 | NV* xnv = (NV*)((char*)(p) + STRUCT_OFFSET(XPVNV, xnv_nv)); |
cbe51380 | 378 | LOCK_SV_MUTEX; |
65202027 | 379 | *(NV**)xnv = PL_xnv_root; |
3280af22 | 380 | PL_xnv_root = xnv; |
cbe51380 | 381 | UNLOCK_SV_MUTEX; |
463ee0b2 LW |
382 | } |
383 | ||
cbe51380 | 384 | STATIC void |
cea2e8a9 | 385 | S_more_xnv(pTHX) |
463ee0b2 | 386 | { |
65202027 DS |
387 | register NV* xnv; |
388 | register NV* xnvend; | |
612f20c3 GS |
389 | XPV *ptr; |
390 | New(711, ptr, 1008/sizeof(XPV), XPV); | |
391 | ptr->xpv_pv = (char*)PL_xnv_arenaroot; | |
392 | PL_xnv_arenaroot = ptr; | |
393 | ||
394 | xnv = (NV*) ptr; | |
65202027 DS |
395 | xnvend = &xnv[1008 / sizeof(NV) - 1]; |
396 | xnv += (sizeof(XPVIV) - 1) / sizeof(NV) + 1; /* fudge by sizeof XPVIV */ | |
3280af22 | 397 | PL_xnv_root = xnv; |
463ee0b2 | 398 | while (xnv < xnvend) { |
65202027 | 399 | *(NV**)xnv = (NV*)(xnv + 1); |
463ee0b2 LW |
400 | xnv++; |
401 | } | |
65202027 | 402 | *(NV**)xnv = 0; |
463ee0b2 LW |
403 | } |
404 | ||
76e3520e | 405 | STATIC XRV* |
cea2e8a9 | 406 | S_new_xrv(pTHX) |
ed6116ce LW |
407 | { |
408 | XRV* xrv; | |
cbe51380 GS |
409 | LOCK_SV_MUTEX; |
410 | if (!PL_xrv_root) | |
411 | more_xrv(); | |
412 | xrv = PL_xrv_root; | |
413 | PL_xrv_root = (XRV*)xrv->xrv_rv; | |
414 | UNLOCK_SV_MUTEX; | |
415 | return xrv; | |
ed6116ce LW |
416 | } |
417 | ||
76e3520e | 418 | STATIC void |
cea2e8a9 | 419 | S_del_xrv(pTHX_ XRV *p) |
ed6116ce | 420 | { |
cbe51380 | 421 | LOCK_SV_MUTEX; |
3280af22 NIS |
422 | p->xrv_rv = (SV*)PL_xrv_root; |
423 | PL_xrv_root = p; | |
cbe51380 | 424 | UNLOCK_SV_MUTEX; |
ed6116ce LW |
425 | } |
426 | ||
cbe51380 | 427 | STATIC void |
cea2e8a9 | 428 | S_more_xrv(pTHX) |
ed6116ce | 429 | { |
ed6116ce LW |
430 | register XRV* xrv; |
431 | register XRV* xrvend; | |
612f20c3 GS |
432 | XPV *ptr; |
433 | New(712, ptr, 1008/sizeof(XPV), XPV); | |
434 | ptr->xpv_pv = (char*)PL_xrv_arenaroot; | |
435 | PL_xrv_arenaroot = ptr; | |
436 | ||
437 | xrv = (XRV*) ptr; | |
ed6116ce | 438 | xrvend = &xrv[1008 / sizeof(XRV) - 1]; |
612f20c3 GS |
439 | xrv += (sizeof(XPV) - 1) / sizeof(XRV) + 1; |
440 | PL_xrv_root = xrv; | |
ed6116ce LW |
441 | while (xrv < xrvend) { |
442 | xrv->xrv_rv = (SV*)(xrv + 1); | |
443 | xrv++; | |
444 | } | |
445 | xrv->xrv_rv = 0; | |
ed6116ce LW |
446 | } |
447 | ||
76e3520e | 448 | STATIC XPV* |
cea2e8a9 | 449 | S_new_xpv(pTHX) |
463ee0b2 LW |
450 | { |
451 | XPV* xpv; | |
cbe51380 GS |
452 | LOCK_SV_MUTEX; |
453 | if (!PL_xpv_root) | |
454 | more_xpv(); | |
455 | xpv = PL_xpv_root; | |
456 | PL_xpv_root = (XPV*)xpv->xpv_pv; | |
457 | UNLOCK_SV_MUTEX; | |
458 | return xpv; | |
463ee0b2 LW |
459 | } |
460 | ||
76e3520e | 461 | STATIC void |
cea2e8a9 | 462 | S_del_xpv(pTHX_ XPV *p) |
463ee0b2 | 463 | { |
cbe51380 | 464 | LOCK_SV_MUTEX; |
3280af22 NIS |
465 | p->xpv_pv = (char*)PL_xpv_root; |
466 | PL_xpv_root = p; | |
cbe51380 | 467 | UNLOCK_SV_MUTEX; |
463ee0b2 LW |
468 | } |
469 | ||
cbe51380 | 470 | STATIC void |
cea2e8a9 | 471 | S_more_xpv(pTHX) |
463ee0b2 | 472 | { |
463ee0b2 LW |
473 | register XPV* xpv; |
474 | register XPV* xpvend; | |
612f20c3 GS |
475 | New(713, xpv, 1008/sizeof(XPV), XPV); |
476 | xpv->xpv_pv = (char*)PL_xpv_arenaroot; | |
477 | PL_xpv_arenaroot = xpv; | |
478 | ||
463ee0b2 | 479 | xpvend = &xpv[1008 / sizeof(XPV) - 1]; |
612f20c3 | 480 | PL_xpv_root = ++xpv; |
463ee0b2 LW |
481 | while (xpv < xpvend) { |
482 | xpv->xpv_pv = (char*)(xpv + 1); | |
483 | xpv++; | |
484 | } | |
485 | xpv->xpv_pv = 0; | |
463ee0b2 LW |
486 | } |
487 | ||
932e9ff9 VB |
488 | STATIC XPVIV* |
489 | S_new_xpviv(pTHX) | |
490 | { | |
491 | XPVIV* xpviv; | |
492 | LOCK_SV_MUTEX; | |
493 | if (!PL_xpviv_root) | |
494 | more_xpviv(); | |
495 | xpviv = PL_xpviv_root; | |
496 | PL_xpviv_root = (XPVIV*)xpviv->xpv_pv; | |
497 | UNLOCK_SV_MUTEX; | |
498 | return xpviv; | |
499 | } | |
500 | ||
501 | STATIC void | |
502 | S_del_xpviv(pTHX_ XPVIV *p) | |
503 | { | |
504 | LOCK_SV_MUTEX; | |
505 | p->xpv_pv = (char*)PL_xpviv_root; | |
506 | PL_xpviv_root = p; | |
507 | UNLOCK_SV_MUTEX; | |
508 | } | |
509 | ||
932e9ff9 VB |
510 | STATIC void |
511 | S_more_xpviv(pTHX) | |
512 | { | |
513 | register XPVIV* xpviv; | |
514 | register XPVIV* xpvivend; | |
612f20c3 GS |
515 | New(714, xpviv, 1008/sizeof(XPVIV), XPVIV); |
516 | xpviv->xpv_pv = (char*)PL_xpviv_arenaroot; | |
517 | PL_xpviv_arenaroot = xpviv; | |
518 | ||
932e9ff9 | 519 | xpvivend = &xpviv[1008 / sizeof(XPVIV) - 1]; |
612f20c3 | 520 | PL_xpviv_root = ++xpviv; |
932e9ff9 VB |
521 | while (xpviv < xpvivend) { |
522 | xpviv->xpv_pv = (char*)(xpviv + 1); | |
523 | xpviv++; | |
524 | } | |
525 | xpviv->xpv_pv = 0; | |
526 | } | |
527 | ||
932e9ff9 VB |
528 | STATIC XPVNV* |
529 | S_new_xpvnv(pTHX) | |
530 | { | |
531 | XPVNV* xpvnv; | |
532 | LOCK_SV_MUTEX; | |
533 | if (!PL_xpvnv_root) | |
534 | more_xpvnv(); | |
535 | xpvnv = PL_xpvnv_root; | |
536 | PL_xpvnv_root = (XPVNV*)xpvnv->xpv_pv; | |
537 | UNLOCK_SV_MUTEX; | |
538 | return xpvnv; | |
539 | } | |
540 | ||
541 | STATIC void | |
542 | S_del_xpvnv(pTHX_ XPVNV *p) | |
543 | { | |
544 | LOCK_SV_MUTEX; | |
545 | p->xpv_pv = (char*)PL_xpvnv_root; | |
546 | PL_xpvnv_root = p; | |
547 | UNLOCK_SV_MUTEX; | |
548 | } | |
549 | ||
932e9ff9 VB |
550 | STATIC void |
551 | S_more_xpvnv(pTHX) | |
552 | { | |
553 | register XPVNV* xpvnv; | |
554 | register XPVNV* xpvnvend; | |
612f20c3 GS |
555 | New(715, xpvnv, 1008/sizeof(XPVNV), XPVNV); |
556 | xpvnv->xpv_pv = (char*)PL_xpvnv_arenaroot; | |
557 | PL_xpvnv_arenaroot = xpvnv; | |
558 | ||
932e9ff9 | 559 | xpvnvend = &xpvnv[1008 / sizeof(XPVNV) - 1]; |
612f20c3 | 560 | PL_xpvnv_root = ++xpvnv; |
932e9ff9 VB |
561 | while (xpvnv < xpvnvend) { |
562 | xpvnv->xpv_pv = (char*)(xpvnv + 1); | |
563 | xpvnv++; | |
564 | } | |
565 | xpvnv->xpv_pv = 0; | |
566 | } | |
567 | ||
932e9ff9 VB |
568 | STATIC XPVCV* |
569 | S_new_xpvcv(pTHX) | |
570 | { | |
571 | XPVCV* xpvcv; | |
572 | LOCK_SV_MUTEX; | |
573 | if (!PL_xpvcv_root) | |
574 | more_xpvcv(); | |
575 | xpvcv = PL_xpvcv_root; | |
576 | PL_xpvcv_root = (XPVCV*)xpvcv->xpv_pv; | |
577 | UNLOCK_SV_MUTEX; | |
578 | return xpvcv; | |
579 | } | |
580 | ||
581 | STATIC void | |
582 | S_del_xpvcv(pTHX_ XPVCV *p) | |
583 | { | |
584 | LOCK_SV_MUTEX; | |
585 | p->xpv_pv = (char*)PL_xpvcv_root; | |
586 | PL_xpvcv_root = p; | |
587 | UNLOCK_SV_MUTEX; | |
588 | } | |
589 | ||
932e9ff9 VB |
590 | STATIC void |
591 | S_more_xpvcv(pTHX) | |
592 | { | |
593 | register XPVCV* xpvcv; | |
594 | register XPVCV* xpvcvend; | |
612f20c3 GS |
595 | New(716, xpvcv, 1008/sizeof(XPVCV), XPVCV); |
596 | xpvcv->xpv_pv = (char*)PL_xpvcv_arenaroot; | |
597 | PL_xpvcv_arenaroot = xpvcv; | |
598 | ||
932e9ff9 | 599 | xpvcvend = &xpvcv[1008 / sizeof(XPVCV) - 1]; |
612f20c3 | 600 | PL_xpvcv_root = ++xpvcv; |
932e9ff9 VB |
601 | while (xpvcv < xpvcvend) { |
602 | xpvcv->xpv_pv = (char*)(xpvcv + 1); | |
603 | xpvcv++; | |
604 | } | |
605 | xpvcv->xpv_pv = 0; | |
606 | } | |
607 | ||
932e9ff9 VB |
608 | STATIC XPVAV* |
609 | S_new_xpvav(pTHX) | |
610 | { | |
611 | XPVAV* xpvav; | |
612 | LOCK_SV_MUTEX; | |
613 | if (!PL_xpvav_root) | |
614 | more_xpvav(); | |
615 | xpvav = PL_xpvav_root; | |
616 | PL_xpvav_root = (XPVAV*)xpvav->xav_array; | |
617 | UNLOCK_SV_MUTEX; | |
618 | return xpvav; | |
619 | } | |
620 | ||
621 | STATIC void | |
622 | S_del_xpvav(pTHX_ XPVAV *p) | |
623 | { | |
624 | LOCK_SV_MUTEX; | |
625 | p->xav_array = (char*)PL_xpvav_root; | |
626 | PL_xpvav_root = p; | |
627 | UNLOCK_SV_MUTEX; | |
628 | } | |
629 | ||
932e9ff9 VB |
630 | STATIC void |
631 | S_more_xpvav(pTHX) | |
632 | { | |
633 | register XPVAV* xpvav; | |
634 | register XPVAV* xpvavend; | |
612f20c3 GS |
635 | New(717, xpvav, 1008/sizeof(XPVAV), XPVAV); |
636 | xpvav->xav_array = (char*)PL_xpvav_arenaroot; | |
637 | PL_xpvav_arenaroot = xpvav; | |
638 | ||
932e9ff9 | 639 | xpvavend = &xpvav[1008 / sizeof(XPVAV) - 1]; |
612f20c3 | 640 | PL_xpvav_root = ++xpvav; |
932e9ff9 VB |
641 | while (xpvav < xpvavend) { |
642 | xpvav->xav_array = (char*)(xpvav + 1); | |
643 | xpvav++; | |
644 | } | |
645 | xpvav->xav_array = 0; | |
646 | } | |
647 | ||
932e9ff9 VB |
648 | STATIC XPVHV* |
649 | S_new_xpvhv(pTHX) | |
650 | { | |
651 | XPVHV* xpvhv; | |
652 | LOCK_SV_MUTEX; | |
653 | if (!PL_xpvhv_root) | |
654 | more_xpvhv(); | |
655 | xpvhv = PL_xpvhv_root; | |
656 | PL_xpvhv_root = (XPVHV*)xpvhv->xhv_array; | |
657 | UNLOCK_SV_MUTEX; | |
658 | return xpvhv; | |
659 | } | |
660 | ||
661 | STATIC void | |
662 | S_del_xpvhv(pTHX_ XPVHV *p) | |
663 | { | |
664 | LOCK_SV_MUTEX; | |
665 | p->xhv_array = (char*)PL_xpvhv_root; | |
666 | PL_xpvhv_root = p; | |
667 | UNLOCK_SV_MUTEX; | |
668 | } | |
669 | ||
932e9ff9 VB |
670 | STATIC void |
671 | S_more_xpvhv(pTHX) | |
672 | { | |
673 | register XPVHV* xpvhv; | |
674 | register XPVHV* xpvhvend; | |
612f20c3 GS |
675 | New(718, xpvhv, 1008/sizeof(XPVHV), XPVHV); |
676 | xpvhv->xhv_array = (char*)PL_xpvhv_arenaroot; | |
677 | PL_xpvhv_arenaroot = xpvhv; | |
678 | ||
932e9ff9 | 679 | xpvhvend = &xpvhv[1008 / sizeof(XPVHV) - 1]; |
612f20c3 | 680 | PL_xpvhv_root = ++xpvhv; |
932e9ff9 VB |
681 | while (xpvhv < xpvhvend) { |
682 | xpvhv->xhv_array = (char*)(xpvhv + 1); | |
683 | xpvhv++; | |
684 | } | |
685 | xpvhv->xhv_array = 0; | |
686 | } | |
687 | ||
932e9ff9 VB |
688 | STATIC XPVMG* |
689 | S_new_xpvmg(pTHX) | |
690 | { | |
691 | XPVMG* xpvmg; | |
692 | LOCK_SV_MUTEX; | |
693 | if (!PL_xpvmg_root) | |
694 | more_xpvmg(); | |
695 | xpvmg = PL_xpvmg_root; | |
696 | PL_xpvmg_root = (XPVMG*)xpvmg->xpv_pv; | |
697 | UNLOCK_SV_MUTEX; | |
698 | return xpvmg; | |
699 | } | |
700 | ||
701 | STATIC void | |
702 | S_del_xpvmg(pTHX_ XPVMG *p) | |
703 | { | |
704 | LOCK_SV_MUTEX; | |
705 | p->xpv_pv = (char*)PL_xpvmg_root; | |
706 | PL_xpvmg_root = p; | |
707 | UNLOCK_SV_MUTEX; | |
708 | } | |
709 | ||
932e9ff9 VB |
710 | STATIC void |
711 | S_more_xpvmg(pTHX) | |
712 | { | |
713 | register XPVMG* xpvmg; | |
714 | register XPVMG* xpvmgend; | |
612f20c3 GS |
715 | New(719, xpvmg, 1008/sizeof(XPVMG), XPVMG); |
716 | xpvmg->xpv_pv = (char*)PL_xpvmg_arenaroot; | |
717 | PL_xpvmg_arenaroot = xpvmg; | |
718 | ||
932e9ff9 | 719 | xpvmgend = &xpvmg[1008 / sizeof(XPVMG) - 1]; |
612f20c3 | 720 | PL_xpvmg_root = ++xpvmg; |
932e9ff9 VB |
721 | while (xpvmg < xpvmgend) { |
722 | xpvmg->xpv_pv = (char*)(xpvmg + 1); | |
723 | xpvmg++; | |
724 | } | |
725 | xpvmg->xpv_pv = 0; | |
726 | } | |
727 | ||
932e9ff9 VB |
728 | STATIC XPVLV* |
729 | S_new_xpvlv(pTHX) | |
730 | { | |
731 | XPVLV* xpvlv; | |
732 | LOCK_SV_MUTEX; | |
733 | if (!PL_xpvlv_root) | |
734 | more_xpvlv(); | |
735 | xpvlv = PL_xpvlv_root; | |
736 | PL_xpvlv_root = (XPVLV*)xpvlv->xpv_pv; | |
737 | UNLOCK_SV_MUTEX; | |
738 | return xpvlv; | |
739 | } | |
740 | ||
741 | STATIC void | |
742 | S_del_xpvlv(pTHX_ XPVLV *p) | |
743 | { | |
744 | LOCK_SV_MUTEX; | |
745 | p->xpv_pv = (char*)PL_xpvlv_root; | |
746 | PL_xpvlv_root = p; | |
747 | UNLOCK_SV_MUTEX; | |
748 | } | |
749 | ||
932e9ff9 VB |
750 | STATIC void |
751 | S_more_xpvlv(pTHX) | |
752 | { | |
753 | register XPVLV* xpvlv; | |
754 | register XPVLV* xpvlvend; | |
612f20c3 GS |
755 | New(720, xpvlv, 1008/sizeof(XPVLV), XPVLV); |
756 | xpvlv->xpv_pv = (char*)PL_xpvlv_arenaroot; | |
757 | PL_xpvlv_arenaroot = xpvlv; | |
758 | ||
932e9ff9 | 759 | xpvlvend = &xpvlv[1008 / sizeof(XPVLV) - 1]; |
612f20c3 | 760 | PL_xpvlv_root = ++xpvlv; |
932e9ff9 VB |
761 | while (xpvlv < xpvlvend) { |
762 | xpvlv->xpv_pv = (char*)(xpvlv + 1); | |
763 | xpvlv++; | |
764 | } | |
765 | xpvlv->xpv_pv = 0; | |
766 | } | |
767 | ||
932e9ff9 VB |
768 | STATIC XPVBM* |
769 | S_new_xpvbm(pTHX) | |
770 | { | |
771 | XPVBM* xpvbm; | |
772 | LOCK_SV_MUTEX; | |
773 | if (!PL_xpvbm_root) | |
774 | more_xpvbm(); | |
775 | xpvbm = PL_xpvbm_root; | |
776 | PL_xpvbm_root = (XPVBM*)xpvbm->xpv_pv; | |
777 | UNLOCK_SV_MUTEX; | |
778 | return xpvbm; | |
779 | } | |
780 | ||
781 | STATIC void | |
782 | S_del_xpvbm(pTHX_ XPVBM *p) | |
783 | { | |
784 | LOCK_SV_MUTEX; | |
785 | p->xpv_pv = (char*)PL_xpvbm_root; | |
786 | PL_xpvbm_root = p; | |
787 | UNLOCK_SV_MUTEX; | |
788 | } | |
789 | ||
932e9ff9 VB |
790 | STATIC void |
791 | S_more_xpvbm(pTHX) | |
792 | { | |
793 | register XPVBM* xpvbm; | |
794 | register XPVBM* xpvbmend; | |
612f20c3 GS |
795 | New(721, xpvbm, 1008/sizeof(XPVBM), XPVBM); |
796 | xpvbm->xpv_pv = (char*)PL_xpvbm_arenaroot; | |
797 | PL_xpvbm_arenaroot = xpvbm; | |
798 | ||
932e9ff9 | 799 | xpvbmend = &xpvbm[1008 / sizeof(XPVBM) - 1]; |
612f20c3 | 800 | PL_xpvbm_root = ++xpvbm; |
932e9ff9 VB |
801 | while (xpvbm < xpvbmend) { |
802 | xpvbm->xpv_pv = (char*)(xpvbm + 1); | |
803 | xpvbm++; | |
804 | } | |
805 | xpvbm->xpv_pv = 0; | |
806 | } | |
807 | ||
d33b2eba GS |
808 | #ifdef LEAKTEST |
809 | # define my_safemalloc(s) (void*)safexmalloc(717,s) | |
810 | # define my_safefree(p) safexfree((char*)p) | |
811 | #else | |
812 | # define my_safemalloc(s) (void*)safemalloc(s) | |
813 | # define my_safefree(p) safefree((char*)p) | |
814 | #endif | |
463ee0b2 | 815 | |
d33b2eba | 816 | #ifdef PURIFY |
463ee0b2 | 817 | |
d33b2eba GS |
818 | #define new_XIV() my_safemalloc(sizeof(XPVIV)) |
819 | #define del_XIV(p) my_safefree(p) | |
ed6116ce | 820 | |
d33b2eba GS |
821 | #define new_XNV() my_safemalloc(sizeof(XPVNV)) |
822 | #define del_XNV(p) my_safefree(p) | |
463ee0b2 | 823 | |
d33b2eba GS |
824 | #define new_XRV() my_safemalloc(sizeof(XRV)) |
825 | #define del_XRV(p) my_safefree(p) | |
8c52afec | 826 | |
d33b2eba GS |
827 | #define new_XPV() my_safemalloc(sizeof(XPV)) |
828 | #define del_XPV(p) my_safefree(p) | |
9b94d1dd | 829 | |
d33b2eba GS |
830 | #define new_XPVIV() my_safemalloc(sizeof(XPVIV)) |
831 | #define del_XPVIV(p) my_safefree(p) | |
932e9ff9 | 832 | |
d33b2eba GS |
833 | #define new_XPVNV() my_safemalloc(sizeof(XPVNV)) |
834 | #define del_XPVNV(p) my_safefree(p) | |
932e9ff9 | 835 | |
d33b2eba GS |
836 | #define new_XPVCV() my_safemalloc(sizeof(XPVCV)) |
837 | #define del_XPVCV(p) my_safefree(p) | |
932e9ff9 | 838 | |
d33b2eba GS |
839 | #define new_XPVAV() my_safemalloc(sizeof(XPVAV)) |
840 | #define del_XPVAV(p) my_safefree(p) | |
841 | ||
842 | #define new_XPVHV() my_safemalloc(sizeof(XPVHV)) | |
843 | #define del_XPVHV(p) my_safefree(p) | |
1c846c1f | 844 | |
d33b2eba GS |
845 | #define new_XPVMG() my_safemalloc(sizeof(XPVMG)) |
846 | #define del_XPVMG(p) my_safefree(p) | |
847 | ||
848 | #define new_XPVLV() my_safemalloc(sizeof(XPVLV)) | |
849 | #define del_XPVLV(p) my_safefree(p) | |
850 | ||
851 | #define new_XPVBM() my_safemalloc(sizeof(XPVBM)) | |
852 | #define del_XPVBM(p) my_safefree(p) | |
853 | ||
854 | #else /* !PURIFY */ | |
855 | ||
856 | #define new_XIV() (void*)new_xiv() | |
857 | #define del_XIV(p) del_xiv((XPVIV*) p) | |
858 | ||
859 | #define new_XNV() (void*)new_xnv() | |
860 | #define del_XNV(p) del_xnv((XPVNV*) p) | |
9b94d1dd | 861 | |
d33b2eba GS |
862 | #define new_XRV() (void*)new_xrv() |
863 | #define del_XRV(p) del_xrv((XRV*) p) | |
9b94d1dd | 864 | |
d33b2eba GS |
865 | #define new_XPV() (void*)new_xpv() |
866 | #define del_XPV(p) del_xpv((XPV *)p) | |
867 | ||
868 | #define new_XPVIV() (void*)new_xpviv() | |
869 | #define del_XPVIV(p) del_xpviv((XPVIV *)p) | |
870 | ||
871 | #define new_XPVNV() (void*)new_xpvnv() | |
872 | #define del_XPVNV(p) del_xpvnv((XPVNV *)p) | |
873 | ||
874 | #define new_XPVCV() (void*)new_xpvcv() | |
875 | #define del_XPVCV(p) del_xpvcv((XPVCV *)p) | |
876 | ||
877 | #define new_XPVAV() (void*)new_xpvav() | |
878 | #define del_XPVAV(p) del_xpvav((XPVAV *)p) | |
879 | ||
880 | #define new_XPVHV() (void*)new_xpvhv() | |
881 | #define del_XPVHV(p) del_xpvhv((XPVHV *)p) | |
1c846c1f | 882 | |
d33b2eba GS |
883 | #define new_XPVMG() (void*)new_xpvmg() |
884 | #define del_XPVMG(p) del_xpvmg((XPVMG *)p) | |
885 | ||
886 | #define new_XPVLV() (void*)new_xpvlv() | |
887 | #define del_XPVLV(p) del_xpvlv((XPVLV *)p) | |
888 | ||
889 | #define new_XPVBM() (void*)new_xpvbm() | |
890 | #define del_XPVBM(p) del_xpvbm((XPVBM *)p) | |
891 | ||
892 | #endif /* PURIFY */ | |
9b94d1dd | 893 | |
d33b2eba GS |
894 | #define new_XPVGV() my_safemalloc(sizeof(XPVGV)) |
895 | #define del_XPVGV(p) my_safefree(p) | |
1c846c1f | 896 | |
d33b2eba GS |
897 | #define new_XPVFM() my_safemalloc(sizeof(XPVFM)) |
898 | #define del_XPVFM(p) my_safefree(p) | |
1c846c1f | 899 | |
d33b2eba GS |
900 | #define new_XPVIO() my_safemalloc(sizeof(XPVIO)) |
901 | #define del_XPVIO(p) my_safefree(p) | |
8990e307 | 902 | |
954c1994 GS |
903 | /* |
904 | =for apidoc sv_upgrade | |
905 | ||
906 | Upgrade an SV to a more complex form. Use C<SvUPGRADE>. See | |
907 | C<svtype>. | |
908 | ||
909 | =cut | |
910 | */ | |
911 | ||
79072805 | 912 | bool |
864dbfa3 | 913 | Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt) |
79072805 LW |
914 | { |
915 | char* pv; | |
916 | U32 cur; | |
917 | U32 len; | |
a0d0e21e | 918 | IV iv; |
65202027 | 919 | NV nv; |
79072805 LW |
920 | MAGIC* magic; |
921 | HV* stash; | |
922 | ||
f130fd45 NIS |
923 | if (mt != SVt_PV && SvREADONLY(sv) && SvFAKE(sv)) { |
924 | sv_force_normal(sv); | |
925 | } | |
926 | ||
79072805 LW |
927 | if (SvTYPE(sv) == mt) |
928 | return TRUE; | |
929 | ||
a5f75d66 AD |
930 | if (mt < SVt_PVIV) |
931 | (void)SvOOK_off(sv); | |
932 | ||
79072805 LW |
933 | switch (SvTYPE(sv)) { |
934 | case SVt_NULL: | |
935 | pv = 0; | |
936 | cur = 0; | |
937 | len = 0; | |
938 | iv = 0; | |
939 | nv = 0.0; | |
940 | magic = 0; | |
941 | stash = 0; | |
942 | break; | |
79072805 LW |
943 | case SVt_IV: |
944 | pv = 0; | |
945 | cur = 0; | |
946 | len = 0; | |
463ee0b2 | 947 | iv = SvIVX(sv); |
65202027 | 948 | nv = (NV)SvIVX(sv); |
79072805 LW |
949 | del_XIV(SvANY(sv)); |
950 | magic = 0; | |
951 | stash = 0; | |
ed6116ce | 952 | if (mt == SVt_NV) |
463ee0b2 | 953 | mt = SVt_PVNV; |
ed6116ce LW |
954 | else if (mt < SVt_PVIV) |
955 | mt = SVt_PVIV; | |
79072805 LW |
956 | break; |
957 | case SVt_NV: | |
958 | pv = 0; | |
959 | cur = 0; | |
960 | len = 0; | |
463ee0b2 | 961 | nv = SvNVX(sv); |
1bd302c3 | 962 | iv = I_V(nv); |
79072805 LW |
963 | magic = 0; |
964 | stash = 0; | |
965 | del_XNV(SvANY(sv)); | |
966 | SvANY(sv) = 0; | |
ed6116ce | 967 | if (mt < SVt_PVNV) |
79072805 LW |
968 | mt = SVt_PVNV; |
969 | break; | |
ed6116ce LW |
970 | case SVt_RV: |
971 | pv = (char*)SvRV(sv); | |
972 | cur = 0; | |
973 | len = 0; | |
56431972 RB |
974 | iv = PTR2IV(pv); |
975 | nv = PTR2NV(pv); | |
ed6116ce LW |
976 | del_XRV(SvANY(sv)); |
977 | magic = 0; | |
978 | stash = 0; | |
979 | break; | |
79072805 | 980 | case SVt_PV: |
463ee0b2 | 981 | pv = SvPVX(sv); |
79072805 LW |
982 | cur = SvCUR(sv); |
983 | len = SvLEN(sv); | |
984 | iv = 0; | |
985 | nv = 0.0; | |
986 | magic = 0; | |
987 | stash = 0; | |
988 | del_XPV(SvANY(sv)); | |
748a9306 LW |
989 | if (mt <= SVt_IV) |
990 | mt = SVt_PVIV; | |
991 | else if (mt == SVt_NV) | |
992 | mt = SVt_PVNV; | |
79072805 LW |
993 | break; |
994 | case SVt_PVIV: | |
463ee0b2 | 995 | pv = SvPVX(sv); |
79072805 LW |
996 | cur = SvCUR(sv); |
997 | len = SvLEN(sv); | |
463ee0b2 | 998 | iv = SvIVX(sv); |
79072805 LW |
999 | nv = 0.0; |
1000 | magic = 0; | |
1001 | stash = 0; | |
1002 | del_XPVIV(SvANY(sv)); | |
1003 | break; | |
1004 | case SVt_PVNV: | |
463ee0b2 | 1005 | pv = SvPVX(sv); |
79072805 LW |
1006 | cur = SvCUR(sv); |
1007 | len = SvLEN(sv); | |
463ee0b2 LW |
1008 | iv = SvIVX(sv); |
1009 | nv = SvNVX(sv); | |
79072805 LW |
1010 | magic = 0; |
1011 | stash = 0; | |
1012 | del_XPVNV(SvANY(sv)); | |
1013 | break; | |
1014 | case SVt_PVMG: | |
463ee0b2 | 1015 | pv = SvPVX(sv); |
79072805 LW |
1016 | cur = SvCUR(sv); |
1017 | len = SvLEN(sv); | |
463ee0b2 LW |
1018 | iv = SvIVX(sv); |
1019 | nv = SvNVX(sv); | |
79072805 LW |
1020 | magic = SvMAGIC(sv); |
1021 | stash = SvSTASH(sv); | |
1022 | del_XPVMG(SvANY(sv)); | |
1023 | break; | |
1024 | default: | |
cea2e8a9 | 1025 | Perl_croak(aTHX_ "Can't upgrade that kind of scalar"); |
79072805 LW |
1026 | } |
1027 | ||
1028 | switch (mt) { | |
1029 | case SVt_NULL: | |
cea2e8a9 | 1030 | Perl_croak(aTHX_ "Can't upgrade to undef"); |
79072805 LW |
1031 | case SVt_IV: |
1032 | SvANY(sv) = new_XIV(); | |
463ee0b2 | 1033 | SvIVX(sv) = iv; |
79072805 LW |
1034 | break; |
1035 | case SVt_NV: | |
1036 | SvANY(sv) = new_XNV(); | |
463ee0b2 | 1037 | SvNVX(sv) = nv; |
79072805 | 1038 | break; |
ed6116ce LW |
1039 | case SVt_RV: |
1040 | SvANY(sv) = new_XRV(); | |
1041 | SvRV(sv) = (SV*)pv; | |
ed6116ce | 1042 | break; |
79072805 LW |
1043 | case SVt_PV: |
1044 | SvANY(sv) = new_XPV(); | |
463ee0b2 | 1045 | SvPVX(sv) = pv; |
79072805 LW |
1046 | SvCUR(sv) = cur; |
1047 | SvLEN(sv) = len; | |
1048 | break; | |
1049 | case SVt_PVIV: | |
1050 | SvANY(sv) = new_XPVIV(); | |
463ee0b2 | 1051 | SvPVX(sv) = pv; |
79072805 LW |
1052 | SvCUR(sv) = cur; |
1053 | SvLEN(sv) = len; | |
463ee0b2 | 1054 | SvIVX(sv) = iv; |
79072805 | 1055 | if (SvNIOK(sv)) |
a0d0e21e | 1056 | (void)SvIOK_on(sv); |
79072805 LW |
1057 | SvNOK_off(sv); |
1058 | break; | |
1059 | case SVt_PVNV: | |
1060 | SvANY(sv) = new_XPVNV(); | |
463ee0b2 | 1061 | SvPVX(sv) = pv; |
79072805 LW |
1062 | SvCUR(sv) = cur; |
1063 | SvLEN(sv) = len; | |
463ee0b2 LW |
1064 | SvIVX(sv) = iv; |
1065 | SvNVX(sv) = nv; | |
79072805 LW |
1066 | break; |
1067 | case SVt_PVMG: | |
1068 | SvANY(sv) = new_XPVMG(); | |
463ee0b2 | 1069 | SvPVX(sv) = pv; |
79072805 LW |
1070 | SvCUR(sv) = cur; |
1071 | SvLEN(sv) = len; | |
463ee0b2 LW |
1072 | SvIVX(sv) = iv; |
1073 | SvNVX(sv) = nv; | |
79072805 LW |
1074 | SvMAGIC(sv) = magic; |
1075 | SvSTASH(sv) = stash; | |
1076 | break; | |
1077 | case SVt_PVLV: | |
1078 | SvANY(sv) = new_XPVLV(); | |
463ee0b2 | 1079 | SvPVX(sv) = pv; |
79072805 LW |
1080 | SvCUR(sv) = cur; |
1081 | SvLEN(sv) = len; | |
463ee0b2 LW |
1082 | SvIVX(sv) = iv; |
1083 | SvNVX(sv) = nv; | |
79072805 LW |
1084 | SvMAGIC(sv) = magic; |
1085 | SvSTASH(sv) = stash; | |
1086 | LvTARGOFF(sv) = 0; | |
1087 | LvTARGLEN(sv) = 0; | |
1088 | LvTARG(sv) = 0; | |
1089 | LvTYPE(sv) = 0; | |
1090 | break; | |
1091 | case SVt_PVAV: | |
1092 | SvANY(sv) = new_XPVAV(); | |
463ee0b2 LW |
1093 | if (pv) |
1094 | Safefree(pv); | |
2304df62 | 1095 | SvPVX(sv) = 0; |
d1bf51dd | 1096 | AvMAX(sv) = -1; |
93965878 | 1097 | AvFILLp(sv) = -1; |
463ee0b2 LW |
1098 | SvIVX(sv) = 0; |
1099 | SvNVX(sv) = 0.0; | |
1100 | SvMAGIC(sv) = magic; | |
1101 | SvSTASH(sv) = stash; | |
1102 | AvALLOC(sv) = 0; | |
79072805 LW |
1103 | AvARYLEN(sv) = 0; |
1104 | AvFLAGS(sv) = 0; | |
1105 | break; | |
1106 | case SVt_PVHV: | |
1107 | SvANY(sv) = new_XPVHV(); | |
463ee0b2 LW |
1108 | if (pv) |
1109 | Safefree(pv); | |
1110 | SvPVX(sv) = 0; | |
1111 | HvFILL(sv) = 0; | |
1112 | HvMAX(sv) = 0; | |
1113 | HvKEYS(sv) = 0; | |
1114 | SvNVX(sv) = 0.0; | |
79072805 LW |
1115 | SvMAGIC(sv) = magic; |
1116 | SvSTASH(sv) = stash; | |
79072805 LW |
1117 | HvRITER(sv) = 0; |
1118 | HvEITER(sv) = 0; | |
1119 | HvPMROOT(sv) = 0; | |
1120 | HvNAME(sv) = 0; | |
79072805 LW |
1121 | break; |
1122 | case SVt_PVCV: | |
1123 | SvANY(sv) = new_XPVCV(); | |
748a9306 | 1124 | Zero(SvANY(sv), 1, XPVCV); |
463ee0b2 | 1125 | SvPVX(sv) = pv; |
79072805 LW |
1126 | SvCUR(sv) = cur; |
1127 | SvLEN(sv) = len; | |
463ee0b2 LW |
1128 | SvIVX(sv) = iv; |
1129 | SvNVX(sv) = nv; | |
79072805 LW |
1130 | SvMAGIC(sv) = magic; |
1131 | SvSTASH(sv) = stash; | |
79072805 LW |
1132 | break; |
1133 | case SVt_PVGV: | |
1134 | SvANY(sv) = new_XPVGV(); | |
463ee0b2 | 1135 | SvPVX(sv) = pv; |
79072805 LW |
1136 | SvCUR(sv) = cur; |
1137 | SvLEN(sv) = len; | |
463ee0b2 LW |
1138 | SvIVX(sv) = iv; |
1139 | SvNVX(sv) = nv; | |
79072805 LW |
1140 | SvMAGIC(sv) = magic; |
1141 | SvSTASH(sv) = stash; | |
93a17b20 | 1142 | GvGP(sv) = 0; |
79072805 LW |
1143 | GvNAME(sv) = 0; |
1144 | GvNAMELEN(sv) = 0; | |
1145 | GvSTASH(sv) = 0; | |
a5f75d66 | 1146 | GvFLAGS(sv) = 0; |
79072805 LW |
1147 | break; |
1148 | case SVt_PVBM: | |
1149 | SvANY(sv) = new_XPVBM(); | |
463ee0b2 | 1150 | SvPVX(sv) = pv; |
79072805 LW |
1151 | SvCUR(sv) = cur; |
1152 | SvLEN(sv) = len; | |
463ee0b2 LW |
1153 | SvIVX(sv) = iv; |
1154 | SvNVX(sv) = nv; | |
79072805 LW |
1155 | SvMAGIC(sv) = magic; |
1156 | SvSTASH(sv) = stash; | |
1157 | BmRARE(sv) = 0; | |
1158 | BmUSEFUL(sv) = 0; | |
1159 | BmPREVIOUS(sv) = 0; | |
1160 | break; | |
1161 | case SVt_PVFM: | |
1162 | SvANY(sv) = new_XPVFM(); | |
748a9306 | 1163 | Zero(SvANY(sv), 1, XPVFM); |
463ee0b2 | 1164 | SvPVX(sv) = pv; |
79072805 LW |
1165 | SvCUR(sv) = cur; |
1166 | SvLEN(sv) = len; | |
463ee0b2 LW |
1167 | SvIVX(sv) = iv; |
1168 | SvNVX(sv) = nv; | |
79072805 LW |
1169 | SvMAGIC(sv) = magic; |
1170 | SvSTASH(sv) = stash; | |
79072805 | 1171 | break; |
8990e307 LW |
1172 | case SVt_PVIO: |
1173 | SvANY(sv) = new_XPVIO(); | |
748a9306 | 1174 | Zero(SvANY(sv), 1, XPVIO); |
8990e307 LW |
1175 | SvPVX(sv) = pv; |
1176 | SvCUR(sv) = cur; | |
1177 | SvLEN(sv) = len; | |
1178 | SvIVX(sv) = iv; | |
1179 | SvNVX(sv) = nv; | |
1180 | SvMAGIC(sv) = magic; | |
1181 | SvSTASH(sv) = stash; | |
85e6fe83 | 1182 | IoPAGE_LEN(sv) = 60; |
8990e307 LW |
1183 | break; |
1184 | } | |
1185 | SvFLAGS(sv) &= ~SVTYPEMASK; | |
1186 | SvFLAGS(sv) |= mt; | |
79072805 LW |
1187 | return TRUE; |
1188 | } | |
1189 | ||
79072805 | 1190 | int |
864dbfa3 | 1191 | Perl_sv_backoff(pTHX_ register SV *sv) |
79072805 LW |
1192 | { |
1193 | assert(SvOOK(sv)); | |
463ee0b2 LW |
1194 | if (SvIVX(sv)) { |
1195 | char *s = SvPVX(sv); | |
1196 | SvLEN(sv) += SvIVX(sv); | |
1197 | SvPVX(sv) -= SvIVX(sv); | |
79072805 | 1198 | SvIV_set(sv, 0); |
463ee0b2 | 1199 | Move(s, SvPVX(sv), SvCUR(sv)+1, char); |
79072805 LW |
1200 | } |
1201 | SvFLAGS(sv) &= ~SVf_OOK; | |
a0d0e21e | 1202 | return 0; |
79072805 LW |
1203 | } |
1204 | ||
954c1994 GS |
1205 | /* |
1206 | =for apidoc sv_grow | |
1207 | ||
1208 | Expands the character buffer in the SV. This will use C<sv_unref> and will | |
1209 | upgrade the SV to C<SVt_PV>. Returns a pointer to the character buffer. | |
1210 | Use C<SvGROW>. | |
1211 | ||
1212 | =cut | |
1213 | */ | |
1214 | ||
79072805 | 1215 | char * |
864dbfa3 | 1216 | Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen) |
79072805 LW |
1217 | { |
1218 | register char *s; | |
1219 | ||
55497cff | 1220 | #ifdef HAS_64K_LIMIT |
79072805 | 1221 | if (newlen >= 0x10000) { |
1d7c1841 GS |
1222 | PerlIO_printf(Perl_debug_log, |
1223 | "Allocation too large: %"UVxf"\n", (UV)newlen); | |
79072805 LW |
1224 | my_exit(1); |
1225 | } | |
55497cff | 1226 | #endif /* HAS_64K_LIMIT */ |
a0d0e21e LW |
1227 | if (SvROK(sv)) |
1228 | sv_unref(sv); | |
79072805 LW |
1229 | if (SvTYPE(sv) < SVt_PV) { |
1230 | sv_upgrade(sv, SVt_PV); | |
463ee0b2 | 1231 | s = SvPVX(sv); |
79072805 LW |
1232 | } |
1233 | else if (SvOOK(sv)) { /* pv is offset? */ | |
1234 | sv_backoff(sv); | |
463ee0b2 | 1235 | s = SvPVX(sv); |
79072805 LW |
1236 | if (newlen > SvLEN(sv)) |
1237 | newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */ | |
c6f8c383 GA |
1238 | #ifdef HAS_64K_LIMIT |
1239 | if (newlen >= 0x10000) | |
1240 | newlen = 0xFFFF; | |
1241 | #endif | |
79072805 LW |
1242 | } |
1243 | else | |
463ee0b2 | 1244 | s = SvPVX(sv); |
79072805 | 1245 | if (newlen > SvLEN(sv)) { /* need more room? */ |
8d6dde3e | 1246 | if (SvLEN(sv) && s) { |
f5a32c7f | 1247 | #if defined(MYMALLOC) && !defined(LEAKTEST) |
8d6dde3e IZ |
1248 | STRLEN l = malloced_size((void*)SvPVX(sv)); |
1249 | if (newlen <= l) { | |
1250 | SvLEN_set(sv, l); | |
1251 | return s; | |
1252 | } else | |
c70c8a0a | 1253 | #endif |
79072805 | 1254 | Renew(s,newlen,char); |
8d6dde3e | 1255 | } |
79072805 LW |
1256 | else |
1257 | New(703,s,newlen,char); | |
1258 | SvPV_set(sv, s); | |
1259 | SvLEN_set(sv, newlen); | |
1260 | } | |
1261 | return s; | |
1262 | } | |
1263 | ||
954c1994 GS |
1264 | /* |
1265 | =for apidoc sv_setiv | |
1266 | ||
1267 | Copies an integer into the given SV. Does not handle 'set' magic. See | |
1268 | C<sv_setiv_mg>. | |
1269 | ||
1270 | =cut | |
1271 | */ | |
1272 | ||
79072805 | 1273 | void |
864dbfa3 | 1274 | Perl_sv_setiv(pTHX_ register SV *sv, IV i) |
79072805 | 1275 | { |
2213622d | 1276 | SV_CHECK_THINKFIRST(sv); |
463ee0b2 LW |
1277 | switch (SvTYPE(sv)) { |
1278 | case SVt_NULL: | |
79072805 | 1279 | sv_upgrade(sv, SVt_IV); |
463ee0b2 LW |
1280 | break; |
1281 | case SVt_NV: | |
1282 | sv_upgrade(sv, SVt_PVNV); | |
1283 | break; | |
ed6116ce | 1284 | case SVt_RV: |
463ee0b2 | 1285 | case SVt_PV: |
79072805 | 1286 | sv_upgrade(sv, SVt_PVIV); |
463ee0b2 | 1287 | break; |
a0d0e21e LW |
1288 | |
1289 | case SVt_PVGV: | |
a0d0e21e LW |
1290 | case SVt_PVAV: |
1291 | case SVt_PVHV: | |
1292 | case SVt_PVCV: | |
1293 | case SVt_PVFM: | |
1294 | case SVt_PVIO: | |
411caa50 JH |
1295 | Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0), |
1296 | PL_op_desc[PL_op->op_type]); | |
463ee0b2 | 1297 | } |
a0d0e21e | 1298 | (void)SvIOK_only(sv); /* validate number */ |
a5f75d66 | 1299 | SvIVX(sv) = i; |
463ee0b2 | 1300 | SvTAINT(sv); |
79072805 LW |
1301 | } |
1302 | ||
954c1994 GS |
1303 | /* |
1304 | =for apidoc sv_setiv_mg | |
1305 | ||
1306 | Like C<sv_setiv>, but also handles 'set' magic. | |
1307 | ||
1308 | =cut | |
1309 | */ | |
1310 | ||
79072805 | 1311 | void |
864dbfa3 | 1312 | Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i) |
ef50df4b GS |
1313 | { |
1314 | sv_setiv(sv,i); | |
1315 | SvSETMAGIC(sv); | |
1316 | } | |
1317 | ||
954c1994 GS |
1318 | /* |
1319 | =for apidoc sv_setuv | |
1320 | ||
1321 | Copies an unsigned integer into the given SV. Does not handle 'set' magic. | |
1322 | See C<sv_setuv_mg>. | |
1323 | ||
1324 | =cut | |
1325 | */ | |
1326 | ||
ef50df4b | 1327 | void |
864dbfa3 | 1328 | Perl_sv_setuv(pTHX_ register SV *sv, UV u) |
55497cff | 1329 | { |
55ada374 NC |
1330 | /* With these two if statements: |
1331 | u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865 | |
d460ef45 | 1332 | |
55ada374 NC |
1333 | without |
1334 | u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865 | |
d460ef45 | 1335 | |
55ada374 NC |
1336 | If you wish to remove them, please benchmark to see what the effect is |
1337 | */ | |
28e5dec8 JH |
1338 | if (u <= (UV)IV_MAX) { |
1339 | sv_setiv(sv, (IV)u); | |
1340 | return; | |
1341 | } | |
25da4f38 IZ |
1342 | sv_setiv(sv, 0); |
1343 | SvIsUV_on(sv); | |
1344 | SvUVX(sv) = u; | |
55497cff | 1345 | } |
1346 | ||
954c1994 GS |
1347 | /* |
1348 | =for apidoc sv_setuv_mg | |
1349 | ||
1350 | Like C<sv_setuv>, but also handles 'set' magic. | |
1351 | ||
1352 | =cut | |
1353 | */ | |
1354 | ||
55497cff | 1355 | void |
864dbfa3 | 1356 | Perl_sv_setuv_mg(pTHX_ register SV *sv, UV u) |
ef50df4b | 1357 | { |
55ada374 NC |
1358 | /* With these two if statements: |
1359 | u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865 | |
d460ef45 | 1360 | |
55ada374 NC |
1361 | without |
1362 | u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865 | |
d460ef45 | 1363 | |
55ada374 NC |
1364 | If you wish to remove them, please benchmark to see what the effect is |
1365 | */ | |
28e5dec8 JH |
1366 | if (u <= (UV)IV_MAX) { |
1367 | sv_setiv(sv, (IV)u); | |
1368 | } else { | |
1369 | sv_setiv(sv, 0); | |
1370 | SvIsUV_on(sv); | |
1371 | sv_setuv(sv,u); | |
1372 | } | |
ef50df4b GS |
1373 | SvSETMAGIC(sv); |
1374 | } | |
1375 | ||
954c1994 GS |
1376 | /* |
1377 | =for apidoc sv_setnv | |
1378 | ||
1379 | Copies a double into the given SV. Does not handle 'set' magic. See | |
1380 | C<sv_setnv_mg>. | |
1381 | ||
1382 | =cut | |
1383 | */ | |
1384 | ||
ef50df4b | 1385 | void |
65202027 | 1386 | Perl_sv_setnv(pTHX_ register SV *sv, NV num) |
79072805 | 1387 | { |
2213622d | 1388 | SV_CHECK_THINKFIRST(sv); |
a0d0e21e LW |
1389 | switch (SvTYPE(sv)) { |
1390 | case SVt_NULL: | |
1391 | case SVt_IV: | |
79072805 | 1392 | sv_upgrade(sv, SVt_NV); |
a0d0e21e | 1393 | break; |
a0d0e21e LW |
1394 | case SVt_RV: |
1395 | case SVt_PV: | |
1396 | case SVt_PVIV: | |
79072805 | 1397 | sv_upgrade(sv, SVt_PVNV); |
a0d0e21e | 1398 | break; |
827b7e14 | 1399 | |
a0d0e21e | 1400 | case SVt_PVGV: |
a0d0e21e LW |
1401 | case SVt_PVAV: |
1402 | case SVt_PVHV: | |
1403 | case SVt_PVCV: | |
1404 | case SVt_PVFM: | |
1405 | case SVt_PVIO: | |
411caa50 JH |
1406 | Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0), |
1407 | PL_op_name[PL_op->op_type]); | |
79072805 | 1408 | } |
463ee0b2 | 1409 | SvNVX(sv) = num; |
a0d0e21e | 1410 | (void)SvNOK_only(sv); /* validate number */ |
463ee0b2 | 1411 | SvTAINT(sv); |
79072805 LW |
1412 | } |
1413 | ||
954c1994 GS |
1414 | /* |
1415 | =for apidoc sv_setnv_mg | |
1416 | ||
1417 | Like C<sv_setnv>, but also handles 'set' magic. | |
1418 | ||
1419 | =cut | |
1420 | */ | |
1421 | ||
ef50df4b | 1422 | void |
65202027 | 1423 | Perl_sv_setnv_mg(pTHX_ register SV *sv, NV num) |
ef50df4b GS |
1424 | { |
1425 | sv_setnv(sv,num); | |
1426 | SvSETMAGIC(sv); | |
1427 | } | |
1428 | ||
76e3520e | 1429 | STATIC void |
cea2e8a9 | 1430 | S_not_a_number(pTHX_ SV *sv) |
a0d0e21e LW |
1431 | { |
1432 | char tmpbuf[64]; | |
1433 | char *d = tmpbuf; | |
dc28f22b GA |
1434 | char *limit = tmpbuf + sizeof(tmpbuf) - 8; |
1435 | /* each *s can expand to 4 chars + "...\0", | |
1436 | i.e. need room for 8 chars */ | |
a0d0e21e | 1437 | |
59bb5845 RB |
1438 | char *s, *end; |
1439 | for (s = SvPVX(sv), end = s + SvCUR(sv); s < end && d < limit; s++) { | |
bbce6d69 | 1440 | int ch = *s & 0xFF; |
1441 | if (ch & 128 && !isPRINT_LC(ch)) { | |
a0d0e21e LW |
1442 | *d++ = 'M'; |
1443 | *d++ = '-'; | |
1444 | ch &= 127; | |
1445 | } | |
bbce6d69 | 1446 | if (ch == '\n') { |
1447 | *d++ = '\\'; | |
1448 | *d++ = 'n'; | |
1449 | } | |
1450 | else if (ch == '\r') { | |
1451 | *d++ = '\\'; | |
1452 | *d++ = 'r'; | |
1453 | } | |
1454 | else if (ch == '\f') { | |
1455 | *d++ = '\\'; | |
1456 | *d++ = 'f'; | |
1457 | } | |
1458 | else if (ch == '\\') { | |
1459 | *d++ = '\\'; | |
1460 | *d++ = '\\'; | |
1461 | } | |
59bb5845 RB |
1462 | else if (ch == '\0') { |
1463 | *d++ = '\\'; | |
1464 | *d++ = '0'; | |
1465 | } | |
bbce6d69 | 1466 | else if (isPRINT_LC(ch)) |
a0d0e21e LW |
1467 | *d++ = ch; |
1468 | else { | |
1469 | *d++ = '^'; | |
bbce6d69 | 1470 | *d++ = toCTRL(ch); |
a0d0e21e LW |
1471 | } |
1472 | } | |
e71c6625 | 1473 | if (s < end) { |
a0d0e21e LW |
1474 | *d++ = '.'; |
1475 | *d++ = '.'; | |
1476 | *d++ = '.'; | |
1477 | } | |
1478 | *d = '\0'; | |
1479 | ||
533c011a | 1480 | if (PL_op) |
42d38218 MS |
1481 | Perl_warner(aTHX_ WARN_NUMERIC, |
1482 | "Argument \"%s\" isn't numeric in %s", tmpbuf, | |
1483 | PL_op_desc[PL_op->op_type]); | |
a0d0e21e | 1484 | else |
42d38218 MS |
1485 | Perl_warner(aTHX_ WARN_NUMERIC, |
1486 | "Argument \"%s\" isn't numeric", tmpbuf); | |
a0d0e21e LW |
1487 | } |
1488 | ||
28e5dec8 JH |
1489 | /* the number can be converted to integer with atol() or atoll() although */ |
1490 | #define IS_NUMBER_TO_INT_BY_ATOL 0x01 /* integer (may have decimals) */ | |
1491 | #define IS_NUMBER_TO_INT_BY_STRTOL 0x02 /* it may exceed IV_MAX */ | |
1492 | #define IS_NUMBER_TO_INT_BY_ATOF 0x04 /* seen something like 123e4 */ | |
1493 | #define IS_NUMBER_LONGER_THAN_IV_MAX 0x08 /* more digits than IV_MAX */ | |
1494 | #define IS_NUMBER_AS_LONG_AS_IV_MAX 0x10 /* may(be not) larger than IV_MAX */ | |
1495 | #define IS_NUMBER_NOT_INT 0x20 /* seen a decimal point or e */ | |
1496 | #define IS_NUMBER_NEG 0x40 /* seen a leading - */ | |
1497 | #define IS_NUMBER_INFINITY 0x80 /* /^\s*-?Infinity\s*$/i */ | |
25da4f38 IZ |
1498 | |
1499 | /* Actually, ISO C leaves conversion of UV to IV undefined, but | |
1500 | until proven guilty, assume that things are not that bad... */ | |
1501 | ||
28e5dec8 JH |
1502 | /* As 64 bit platforms often have an NV that doesn't preserve all bits of |
1503 | an IV (an assumption perl has been based on to date) it becomes necessary | |
1504 | to remove the assumption that the NV always carries enough precision to | |
1505 | recreate the IV whenever needed, and that the NV is the canonical form. | |
1506 | Instead, IV/UV and NV need to be given equal rights. So as to not lose | |
1507 | precision as an side effect of conversion (which would lead to insanity | |
1508 | and the dragon(s) in t/op/numconvert.t getting very angry) the intent is | |
1509 | 1) to distinguish between IV/UV/NV slots that have cached a valid | |
1510 | conversion where precision was lost and IV/UV/NV slots that have a | |
1511 | valid conversion which has lost no precision | |
1512 | 2) to ensure that if a numeric conversion to one form is request that | |
1513 | would lose precision, the precise conversion (or differently | |
1514 | imprecise conversion) is also performed and cached, to prevent | |
1515 | requests for different numeric formats on the same SV causing | |
1516 | lossy conversion chains. (lossless conversion chains are perfectly | |
1517 | acceptable (still)) | |
1518 | ||
1519 | ||
1520 | flags are used: | |
1521 | SvIOKp is true if the IV slot contains a valid value | |
1522 | SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true) | |
1523 | SvNOKp is true if the NV slot contains a valid value | |
1524 | SvNOK is true only if the NV value is accurate | |
1525 | ||
1526 | so | |
1527 | while converting from PV to NV check to see if converting that NV to an | |
1528 | IV(or UV) would lose accuracy over a direct conversion from PV to | |
1529 | IV(or UV). If it would, cache both conversions, return NV, but mark | |
1530 | SV as IOK NOKp (ie not NOK). | |
1531 | ||
1532 | while converting from PV to IV check to see if converting that IV to an | |
1533 | NV would lose accuracy over a direct conversion from PV to NV. If it | |
1534 | would, cache both conversions, flag similarly. | |
1535 | ||
1536 | Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite | |
1537 | correctly because if IV & NV were set NV *always* overruled. | |
1538 | Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flags meaning | |
1539 | changes - now IV and NV together means that the two are interchangeable | |
1540 | SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX; | |
d460ef45 | 1541 | |
28e5dec8 JH |
1542 | The benefit of this is operations such as pp_add know that if SvIOK is |
1543 | true for both left and right operands, then integer addition can be | |
1544 | used instead of floating point. (for cases where the result won't | |
1545 | overflow) Before, floating point was always used, which could lead to | |
1546 | loss of precision compared with integer addition. | |
1547 | ||
1548 | * making IV and NV equal status should make maths accurate on 64 bit | |
1549 | platforms | |
1550 | * may speed up maths somewhat if pp_add and friends start to use | |
1551 | integers when possible instead of fp. (hopefully the overhead in | |
1552 | looking for SvIOK and checking for overflow will not outweigh the | |
1553 | fp to integer speedup) | |
1554 | * will slow down integer operations (callers of SvIV) on "inaccurate" | |
1555 | values, as the change from SvIOK to SvIOKp will cause a call into | |
1556 | sv_2iv each time rather than a macro access direct to the IV slot | |
1557 | * should speed up number->string conversion on integers as IV is | |
1558 | favoured when IV and NV equally accurate | |
1559 | ||
1560 | #################################################################### | |
1561 | You had better be using SvIOK_notUV if you want an IV for arithmetic | |
1562 | SvIOK is true if (IV or UV), so you might be getting (IV)SvUV | |
1563 | SvUOK is true iff UV. | |
1564 | #################################################################### | |
1565 | ||
1566 | Your mileage will vary depending your CPUs relative fp to integer | |
1567 | performance ratio. | |
1568 | */ | |
1569 | ||
1570 | #ifndef NV_PRESERVES_UV | |
1571 | #define IS_NUMBER_UNDERFLOW_IV 1 | |
1572 | #define IS_NUMBER_UNDERFLOW_UV 2 | |
1573 | #define IS_NUMBER_IV_AND_UV 2 | |
1574 | #define IS_NUMBER_OVERFLOW_IV 4 | |
1575 | #define IS_NUMBER_OVERFLOW_UV 5 | |
1576 | /* Hopefully your optimiser will consider inlining these two functions. */ | |
1577 | STATIC int | |
1578 | S_sv_2inuv_non_preserve (pTHX_ register SV *sv, I32 numtype) { | |
1579 | NV nv = SvNVX(sv); /* Code simpler and had compiler problems if */ | |
1580 | UV nv_as_uv = U_V(nv); /* these are not in simple variables. */ | |
159fae86 | 1581 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_2inuv_non '%s', IV=0x%"UVxf" NV=%g inttype=%"UVXf"\n", SvPVX(sv), SvIVX(sv), nv, (UV)numtype)); |
28e5dec8 JH |
1582 | if (nv_as_uv <= (UV)IV_MAX) { |
1583 | (void)SvIOKp_on(sv); | |
1584 | (void)SvNOKp_on(sv); | |
1585 | /* Within suitable range to fit in an IV, atol won't overflow */ | |
1586 | /* XXX quite sure? Is that your final answer? not really, I'm | |
1587 | trusting that nv_as_uv to round down if NV is (IV_MAX + 1) */ | |
1588 | SvIVX(sv) = (IV)Atol(SvPVX(sv)); | |
1589 | if (numtype & IS_NUMBER_NOT_INT) { | |
1590 | /* I believe that even if the original PV had decimals, they | |
1591 | are lost beyond the limit of the FP precision. | |
1592 | However, neither is canonical, so both only get p flags. | |
1593 | NWC, 2000/11/25 */ | |
1594 | /* Both already have p flags, so do nothing */ | |
1595 | } else if (SvIVX(sv) == I_V(nv)) { | |
1596 | SvNOK_on(sv); | |
1597 | SvIOK_on(sv); | |
1598 | } else { | |
1599 | SvIOK_on(sv); | |
1600 | /* It had no "." so it must be integer. assert (get in here from | |
1601 | sv_2iv and sv_2uv only for ndef HAS_STRTOL and | |
1602 | IS_NUMBER_AS_LONG_AS_IV_MAX) or my logic is faulty and all | |
1603 | conversion routines need audit. */ | |
1604 | } | |
1605 | return nv < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV; | |
1606 | } | |
1607 | /* between IV_MAX and NV(UV_MAX). Could be slightly> UV_MAX */ | |
1608 | (void)SvIOKp_on(sv); | |
1609 | (void)SvNOKp_on(sv); | |
1610 | #ifdef HAS_STRTOUL | |
1611 | { | |
1612 | int save_errno = errno; | |
1613 | errno = 0; | |
1614 | SvUVX(sv) = Strtoul(SvPVX(sv), Null(char**), 10); | |
1615 | if (errno == 0) { | |
1616 | if (numtype & IS_NUMBER_NOT_INT) { | |
1617 | /* UV and NV both imprecise. */ | |
1618 | SvIsUV_on(sv); | |
1619 | } else if (SvUVX(sv) == nv_as_uv && SvUVX(sv) != UV_MAX) { | |
1620 | SvNOK_on(sv); | |
1621 | SvIOK_on(sv); | |
1622 | SvIsUV_on(sv); | |
1623 | } else { | |
1624 | SvIOK_on(sv); | |
1625 | SvIsUV_on(sv); | |
1626 | } | |
1627 | errno = save_errno; | |
1628 | return IS_NUMBER_OVERFLOW_IV; | |
1629 | } | |
1630 | errno = save_errno; | |
1631 | SvNOK_on(sv); | |
1632 | /* Must have just overflowed UV, but not enough that an NV could spot | |
1633 | this.. */ | |
1634 | return IS_NUMBER_OVERFLOW_UV; | |
1635 | } | |
1636 | #else | |
1637 | /* We've just lost integer precision, nothing we could do. */ | |
1638 | SvUVX(sv) = nv_as_uv; | |
159fae86 | 1639 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_2niuv_non UV? '%s', UV=0x%"UVxf" NV=%g U_V(NV)=0x%"UVxf" inttype=%"UVXf"\n", SvPVX(sv), SvIVX(sv), nv, nv_as_uv, (UV)numtype)); |
28e5dec8 JH |
1640 | /* UV and NV slots equally valid only if we have casting symmetry. */ |
1641 | if (numtype & IS_NUMBER_NOT_INT) { | |
1642 | SvIsUV_on(sv); | |
1643 | } else if (SvUVX(sv) == nv_as_uv && SvUVX(sv) != UV_MAX) { | |
1644 | /* UV_MAX can cast up to NV (UV_MAX+1), that NV casts down to UV_MAX | |
1645 | UV_MAX ought to be 0xFF...FFF which won't preserve (We only | |
1646 | get to this point if NVs don't preserve UVs) */ | |
1647 | SvNOK_on(sv); | |
1648 | SvIOK_on(sv); | |
1649 | SvIsUV_on(sv); | |
1650 | } else { | |
1651 | /* As above, I believe UV at least as good as NV */ | |
1652 | SvIsUV_on(sv); | |
1653 | } | |
1654 | #endif /* HAS_STRTOUL */ | |
1655 | return IS_NUMBER_OVERFLOW_IV; | |
1656 | } | |
1657 | ||
1658 | /* For sv_2nv these three cases are "SvNOK and don't bother casting" */ | |
1659 | STATIC int | |
1660 | S_sv_2iuv_non_preserve (pTHX_ register SV *sv, I32 numtype) | |
1661 | { | |
159fae86 | 1662 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_2iuv_non '%s', IV=0x%"UVxf" NV=%g inttype=%"UVXf"\n", SvPVX(sv), SvIVX(sv), SvNVX(sv), (UV)numtype)); |
28e5dec8 JH |
1663 | if (SvNVX(sv) < (NV)IV_MIN) { |
1664 | (void)SvIOKp_on(sv); | |
1665 | (void)SvNOK_on(sv); | |
1666 | SvIVX(sv) = IV_MIN; | |
1667 | return IS_NUMBER_UNDERFLOW_IV; | |
1668 | } | |
1669 | if (SvNVX(sv) > (NV)UV_MAX) { | |
1670 | (void)SvIOKp_on(sv); | |
1671 | (void)SvNOK_on(sv); | |
1672 | SvIsUV_on(sv); | |
1673 | SvUVX(sv) = UV_MAX; | |
1674 | return IS_NUMBER_OVERFLOW_UV; | |
1675 | } | |
1676 | if (!(numtype & (IS_NUMBER_TO_INT_BY_ATOL | IS_NUMBER_TO_INT_BY_STRTOL))) { | |
1677 | (void)SvIOKp_on(sv); | |
1678 | (void)SvNOK_on(sv); | |
1679 | /* Can't use strtol etc to convert this string */ | |
1680 | if (SvNVX(sv) <= (UV)IV_MAX) { | |
1681 | SvIVX(sv) = I_V(SvNVX(sv)); | |
1682 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) { | |
1683 | SvIOK_on(sv); /* Integer is precise. NOK, IOK */ | |
1684 | } else { | |
1685 | /* Integer is imprecise. NOK, IOKp */ | |
1686 | } | |
1687 | return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV; | |
1688 | } | |
1689 | SvIsUV_on(sv); | |
1690 | SvUVX(sv) = U_V(SvNVX(sv)); | |
1691 | if ((NV)(SvUVX(sv)) == SvNVX(sv)) { | |
09bb3e27 NC |
1692 | if (SvUVX(sv) == UV_MAX) { |
1693 | /* As we know that NVs don't preserve UVs, UV_MAX cannot | |
1694 | possibly be preserved by NV. Hence, it must be overflow. | |
1695 | NOK, IOKp */ | |
1696 | return IS_NUMBER_OVERFLOW_UV; | |
1697 | } | |
28e5dec8 JH |
1698 | SvIOK_on(sv); /* Integer is precise. NOK, UOK */ |
1699 | } else { | |
1700 | /* Integer is imprecise. NOK, IOKp */ | |
1701 | } | |
1702 | return IS_NUMBER_OVERFLOW_IV; | |
1703 | } | |
e57fe1aa | 1704 | return S_sv_2inuv_non_preserve(aTHX_ sv, numtype); |
28e5dec8 JH |
1705 | } |
1706 | #endif /* NV_PRESERVES_UV*/ | |
1707 | ||
a0d0e21e | 1708 | IV |
864dbfa3 | 1709 | Perl_sv_2iv(pTHX_ register SV *sv) |
79072805 LW |
1710 | { |
1711 | if (!sv) | |
1712 | return 0; | |
8990e307 | 1713 | if (SvGMAGICAL(sv)) { |
463ee0b2 LW |
1714 | mg_get(sv); |
1715 | if (SvIOKp(sv)) | |
1716 | return SvIVX(sv); | |
748a9306 | 1717 | if (SvNOKp(sv)) { |
25da4f38 | 1718 | return I_V(SvNVX(sv)); |
748a9306 | 1719 | } |
36477c24 | 1720 | if (SvPOKp(sv) && SvLEN(sv)) |
1721 | return asIV(sv); | |
3fe9a6f1 | 1722 | if (!SvROK(sv)) { |
d008e5eb | 1723 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 1724 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
1d7c1841 | 1725 | report_uninit(); |
c6ee37c5 | 1726 | } |
36477c24 | 1727 | return 0; |
3fe9a6f1 | 1728 | } |
463ee0b2 | 1729 | } |
ed6116ce | 1730 | if (SvTHINKFIRST(sv)) { |
a0d0e21e | 1731 | if (SvROK(sv)) { |
a0d0e21e | 1732 | SV* tmpstr; |
1554e226 | 1733 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) && |
1dc13c17 | 1734 | (SvTYPE(tmpstr) != SVt_RV || (SvRV(tmpstr) != SvRV(sv)))) |
9e7bc3e8 | 1735 | return SvIV(tmpstr); |
56431972 | 1736 | return PTR2IV(SvRV(sv)); |
a0d0e21e | 1737 | } |
47deb5e7 NIS |
1738 | if (SvREADONLY(sv) && SvFAKE(sv)) { |
1739 | sv_force_normal(sv); | |
1740 | } | |
0336b60e | 1741 | if (SvREADONLY(sv) && !SvOK(sv)) { |
0336b60e | 1742 | if (ckWARN(WARN_UNINITIALIZED)) |
1d7c1841 | 1743 | report_uninit(); |
ed6116ce LW |
1744 | return 0; |
1745 | } | |
79072805 | 1746 | } |
25da4f38 IZ |
1747 | if (SvIOKp(sv)) { |
1748 | if (SvIsUV(sv)) { | |
1749 | return (IV)(SvUVX(sv)); | |
1750 | } | |
1751 | else { | |
1752 | return SvIVX(sv); | |
1753 | } | |
463ee0b2 | 1754 | } |
748a9306 | 1755 | if (SvNOKp(sv)) { |
28e5dec8 JH |
1756 | /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv |
1757 | * without also getting a cached IV/UV from it at the same time | |
1758 | * (ie PV->NV conversion should detect loss of accuracy and cache | |
1759 | * IV or UV at same time to avoid this. NWC */ | |
25da4f38 IZ |
1760 | |
1761 | if (SvTYPE(sv) == SVt_NV) | |
1762 | sv_upgrade(sv, SVt_PVNV); | |
1763 | ||
28e5dec8 JH |
1764 | (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */ |
1765 | /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost | |
1766 | certainly cast into the IV range at IV_MAX, whereas the correct | |
1767 | answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary | |
1768 | cases go to UV */ | |
1769 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
748a9306 | 1770 | SvIVX(sv) = I_V(SvNVX(sv)); |
28e5dec8 JH |
1771 | if (SvNVX(sv) == (NV) SvIVX(sv) |
1772 | #ifndef NV_PRESERVES_UV | |
1773 | && (((UV)1 << NV_PRESERVES_UV_BITS) > | |
1774 | (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv))) | |
1775 | /* Don't flag it as "accurately an integer" if the number | |
1776 | came from a (by definition imprecise) NV operation, and | |
1777 | we're outside the range of NV integer precision */ | |
1778 | #endif | |
1779 | ) { | |
1780 | SvIOK_on(sv); /* Can this go wrong with rounding? NWC */ | |
1781 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
1782 | "0x%"UVxf" iv(%g => %"IVdf") (precise)\n", | |
1783 | PTR2UV(sv), | |
1784 | SvNVX(sv), | |
1785 | SvIVX(sv))); | |
1786 | ||
1787 | } else { | |
1788 | /* IV not precise. No need to convert from PV, as NV | |
1789 | conversion would already have cached IV if it detected | |
1790 | that PV->IV would be better than PV->NV->IV | |
1791 | flags already correct - don't set public IOK. */ | |
1792 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
1793 | "0x%"UVxf" iv(%g => %"IVdf") (imprecise)\n", | |
1794 | PTR2UV(sv), | |
1795 | SvNVX(sv), | |
1796 | SvIVX(sv))); | |
1797 | } | |
1798 | /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN, | |
1799 | but the cast (NV)IV_MIN rounds to a the value less (more | |
1800 | negative) than IV_MIN which happens to be equal to SvNVX ?? | |
1801 | Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and | |
1802 | NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and | |
1803 | (NV)UVX == NVX are both true, but the values differ. :-( | |
1804 | Hopefully for 2s complement IV_MIN is something like | |
1805 | 0x8000000000000000 which will be exact. NWC */ | |
d460ef45 | 1806 | } |
25da4f38 | 1807 | else { |
ff68c719 | 1808 | SvUVX(sv) = U_V(SvNVX(sv)); |
28e5dec8 JH |
1809 | if ( |
1810 | (SvNVX(sv) == (NV) SvUVX(sv)) | |
1811 | #ifndef NV_PRESERVES_UV | |
1812 | /* Make sure it's not 0xFFFFFFFFFFFFFFFF */ | |
1813 | /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */ | |
1814 | && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv)) | |
1815 | /* Don't flag it as "accurately an integer" if the number | |
1816 | came from a (by definition imprecise) NV operation, and | |
1817 | we're outside the range of NV integer precision */ | |
1818 | #endif | |
1819 | ) | |
1820 | SvIOK_on(sv); | |
25da4f38 IZ |
1821 | SvIsUV_on(sv); |
1822 | ret_iv_max: | |
1c846c1f | 1823 | DEBUG_c(PerlIO_printf(Perl_debug_log, |
57def98f | 1824 | "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n", |
56431972 | 1825 | PTR2UV(sv), |
57def98f JH |
1826 | SvUVX(sv), |
1827 | SvUVX(sv))); | |
25da4f38 IZ |
1828 | return (IV)SvUVX(sv); |
1829 | } | |
748a9306 LW |
1830 | } |
1831 | else if (SvPOKp(sv) && SvLEN(sv)) { | |
25da4f38 IZ |
1832 | I32 numtype = looks_like_number(sv); |
1833 | ||
1834 | /* We want to avoid a possible problem when we cache an IV which | |
1835 | may be later translated to an NV, and the resulting NV is not | |
1836 | the translation of the initial data. | |
1c846c1f | 1837 | |
25da4f38 IZ |
1838 | This means that if we cache such an IV, we need to cache the |
1839 | NV as well. Moreover, we trade speed for space, and do not | |
28e5dec8 | 1840 | cache the NV if we are sure it's not needed. |
25da4f38 | 1841 | */ |
16b7a9a4 | 1842 | |
28e5dec8 JH |
1843 | if ((numtype & ~IS_NUMBER_NEG) == IS_NUMBER_TO_INT_BY_ATOL) { |
1844 | /* The NV may be reconstructed from IV - safe to cache IV, | |
1845 | which may be calculated by atol(). */ | |
1846 | if (SvTYPE(sv) < SVt_PVIV) | |
1847 | sv_upgrade(sv, SVt_PVIV); | |
f7bbb42a | 1848 | (void)SvIOK_on(sv); |
28e5dec8 JH |
1849 | SvIVX(sv) = Atol(SvPVX(sv)); |
1850 | } else { | |
1851 | #ifdef HAS_STRTOL | |
1852 | IV i; | |
1853 | int save_errno = errno; | |
1854 | /* Is it an integer that we could convert with strtol? | |
1855 | So try it, and if it doesn't set errno then it's pukka. | |
1856 | This should be faster than going atof and then thinking. */ | |
1857 | if (((numtype & (IS_NUMBER_TO_INT_BY_STRTOL | IS_NUMBER_NOT_INT)) | |
1858 | == IS_NUMBER_TO_INT_BY_STRTOL) | |
1859 | /* && is a sequence point. Without it not sure if I'm trying | |
1860 | to do too much between sequence points and hence going | |
1861 | undefined */ | |
1862 | && ((errno = 0), 1) /* , 1 so always true */ | |
1863 | && ((i = Strtol(SvPVX(sv), Null(char**), 10)), 1) | |
1864 | && (errno == 0)) { | |
1865 | if (SvTYPE(sv) < SVt_PVIV) | |
1866 | sv_upgrade(sv, SVt_PVIV); | |
1867 | (void)SvIOK_on(sv); | |
1868 | SvIVX(sv) = i; | |
1869 | errno = save_errno; | |
1870 | } else | |
1871 | #endif | |
1872 | { | |
1873 | NV d; | |
1874 | #ifdef HAS_STRTOL | |
1875 | /* Hopefully trace flow will optimise this away where possible | |
1876 | */ | |
1877 | errno = save_errno; | |
1878 | #endif | |
1879 | /* It wasn't an integer, or it overflowed, or we don't have | |
1880 | strtol. Do things the slow way - check if it's a UV etc. */ | |
1881 | d = Atof(SvPVX(sv)); | |
1882 | ||
1883 | if (SvTYPE(sv) < SVt_PVNV) | |
1884 | sv_upgrade(sv, SVt_PVNV); | |
1885 | SvNVX(sv) = d; | |
1886 | ||
1887 | if (! numtype && ckWARN(WARN_NUMERIC)) | |
1888 | not_a_number(sv); | |
1889 | ||
65202027 | 1890 | #if defined(USE_LONG_DOUBLE) |
28e5dec8 JH |
1891 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n", |
1892 | PTR2UV(sv), SvNVX(sv))); | |
65202027 | 1893 | #else |
28e5dec8 JH |
1894 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%g)\n", |
1895 | PTR2UV(sv), SvNVX(sv))); | |
65202027 | 1896 | #endif |
28e5dec8 JH |
1897 | |
1898 | ||
1899 | #ifdef NV_PRESERVES_UV | |
1900 | (void)SvIOKp_on(sv); | |
1901 | (void)SvNOK_on(sv); | |
1902 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
1903 | SvIVX(sv) = I_V(SvNVX(sv)); | |
1904 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) { | |
1905 | SvIOK_on(sv); | |
1906 | } else { | |
1907 | /* Integer is imprecise. NOK, IOKp */ | |
1908 | } | |
1909 | /* UV will not work better than IV */ | |
1910 | } else { | |
1911 | if (SvNVX(sv) > (NV)UV_MAX) { | |
1912 | SvIsUV_on(sv); | |
1913 | /* Integer is inaccurate. NOK, IOKp, is UV */ | |
1914 | SvUVX(sv) = UV_MAX; | |
1915 | SvIsUV_on(sv); | |
1916 | } else { | |
1917 | SvUVX(sv) = U_V(SvNVX(sv)); | |
1918 | /* 0xFFFFFFFFFFFFFFFF not an issue in here */ | |
1919 | if ((NV)(SvUVX(sv)) == SvNVX(sv)) { | |
1920 | SvIOK_on(sv); | |
1921 | SvIsUV_on(sv); | |
1922 | } else { | |
1923 | /* Integer is imprecise. NOK, IOKp, is UV */ | |
1924 | SvIsUV_on(sv); | |
1925 | } | |
1926 | } | |
1927 | goto ret_iv_max; | |
1928 | } | |
1929 | #else /* NV_PRESERVES_UV */ | |
1930 | if (((UV)1 << NV_PRESERVES_UV_BITS) > | |
1931 | U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) { | |
1932 | /* Small enough to preserve all bits. */ | |
1933 | (void)SvIOKp_on(sv); | |
1934 | SvNOK_on(sv); | |
1935 | SvIVX(sv) = I_V(SvNVX(sv)); | |
1936 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) | |
1937 | SvIOK_on(sv); | |
1938 | /* Assumption: first non-preserved integer is < IV_MAX, | |
1939 | this NV is in the preserved range, therefore: */ | |
1940 | if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv)) | |
1941 | < (UV)IV_MAX)) { | |
1942 | Perl_croak(aTHX_ "sv_2iv assumed (U_V(fabs(SvNVX(sv))) < (UV)IV_MAX) but SvNVX(sv)=%g U_V is 0x%"UVxf", IV_MAX is 0x%"UVxf"\n", SvNVX(sv), U_V(SvNVX(sv)), (UV)IV_MAX); | |
1943 | } | |
1944 | } else if (sv_2iuv_non_preserve (sv, numtype) | |
1945 | >= IS_NUMBER_OVERFLOW_IV) | |
1946 | goto ret_iv_max; | |
1947 | #endif /* NV_PRESERVES_UV */ | |
25da4f38 IZ |
1948 | } |
1949 | } | |
28e5dec8 | 1950 | } else { |
599cee73 | 1951 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP)) |
1d7c1841 | 1952 | report_uninit(); |
25da4f38 IZ |
1953 | if (SvTYPE(sv) < SVt_IV) |
1954 | /* Typically the caller expects that sv_any is not NULL now. */ | |
1955 | sv_upgrade(sv, SVt_IV); | |
a0d0e21e | 1956 | return 0; |
79072805 | 1957 | } |
1d7c1841 GS |
1958 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n", |
1959 | PTR2UV(sv),SvIVX(sv))); | |
25da4f38 | 1960 | return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv); |
79072805 LW |
1961 | } |
1962 | ||
ff68c719 | 1963 | UV |
864dbfa3 | 1964 | Perl_sv_2uv(pTHX_ register SV *sv) |
ff68c719 | 1965 | { |
1966 | if (!sv) | |
1967 | return 0; | |
1968 | if (SvGMAGICAL(sv)) { | |
1969 | mg_get(sv); | |
1970 | if (SvIOKp(sv)) | |
1971 | return SvUVX(sv); | |
1972 | if (SvNOKp(sv)) | |
1973 | return U_V(SvNVX(sv)); | |
36477c24 | 1974 | if (SvPOKp(sv) && SvLEN(sv)) |
1975 | return asUV(sv); | |
3fe9a6f1 | 1976 | if (!SvROK(sv)) { |
d008e5eb | 1977 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 1978 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
1d7c1841 | 1979 | report_uninit(); |
c6ee37c5 | 1980 | } |
36477c24 | 1981 | return 0; |
3fe9a6f1 | 1982 | } |
ff68c719 | 1983 | } |
1984 | if (SvTHINKFIRST(sv)) { | |
1985 | if (SvROK(sv)) { | |
ff68c719 | 1986 | SV* tmpstr; |
1554e226 | 1987 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) && |
1dc13c17 | 1988 | (SvTYPE(tmpstr) != SVt_RV || (SvRV(tmpstr) != SvRV(sv)))) |
9e7bc3e8 | 1989 | return SvUV(tmpstr); |
56431972 | 1990 | return PTR2UV(SvRV(sv)); |
ff68c719 | 1991 | } |
8a818333 NIS |
1992 | if (SvREADONLY(sv) && SvFAKE(sv)) { |
1993 | sv_force_normal(sv); | |
1994 | } | |
0336b60e | 1995 | if (SvREADONLY(sv) && !SvOK(sv)) { |
0336b60e | 1996 | if (ckWARN(WARN_UNINITIALIZED)) |
1d7c1841 | 1997 | report_uninit(); |
ff68c719 | 1998 | return 0; |
1999 | } | |
2000 | } | |
25da4f38 IZ |
2001 | if (SvIOKp(sv)) { |
2002 | if (SvIsUV(sv)) { | |
2003 | return SvUVX(sv); | |
2004 | } | |
2005 | else { | |
2006 | return (UV)SvIVX(sv); | |
2007 | } | |
ff68c719 | 2008 | } |
2009 | if (SvNOKp(sv)) { | |
28e5dec8 JH |
2010 | /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv |
2011 | * without also getting a cached IV/UV from it at the same time | |
2012 | * (ie PV->NV conversion should detect loss of accuracy and cache | |
2013 | * IV or UV at same time to avoid this. */ | |
2014 | /* IV-over-UV optimisation - choose to cache IV if possible */ | |
2015 | ||
25da4f38 IZ |
2016 | if (SvTYPE(sv) == SVt_NV) |
2017 | sv_upgrade(sv, SVt_PVNV); | |
28e5dec8 JH |
2018 | |
2019 | (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */ | |
2020 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
f7bbb42a | 2021 | SvIVX(sv) = I_V(SvNVX(sv)); |
28e5dec8 JH |
2022 | if (SvNVX(sv) == (NV) SvIVX(sv) |
2023 | #ifndef NV_PRESERVES_UV | |
2024 | && (((UV)1 << NV_PRESERVES_UV_BITS) > | |
2025 | (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv))) | |
2026 | /* Don't flag it as "accurately an integer" if the number | |
2027 | came from a (by definition imprecise) NV operation, and | |
2028 | we're outside the range of NV integer precision */ | |
2029 | #endif | |
2030 | ) { | |
2031 | SvIOK_on(sv); /* Can this go wrong with rounding? NWC */ | |
2032 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
2033 | "0x%"UVxf" uv(%g => %"IVdf") (precise)\n", | |
2034 | PTR2UV(sv), | |
2035 | SvNVX(sv), | |
2036 | SvIVX(sv))); | |
2037 | ||
2038 | } else { | |
2039 | /* IV not precise. No need to convert from PV, as NV | |
2040 | conversion would already have cached IV if it detected | |
2041 | that PV->IV would be better than PV->NV->IV | |
2042 | flags already correct - don't set public IOK. */ | |
2043 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
2044 | "0x%"UVxf" uv(%g => %"IVdf") (imprecise)\n", | |
2045 | PTR2UV(sv), | |
2046 | SvNVX(sv), | |
2047 | SvIVX(sv))); | |
2048 | } | |
2049 | /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN, | |
2050 | but the cast (NV)IV_MIN rounds to a the value less (more | |
2051 | negative) than IV_MIN which happens to be equal to SvNVX ?? | |
2052 | Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and | |
2053 | NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and | |
2054 | (NV)UVX == NVX are both true, but the values differ. :-( | |
2055 | Hopefully for 2s complement IV_MIN is something like | |
2056 | 0x8000000000000000 which will be exact. NWC */ | |
d460ef45 | 2057 | } |
28e5dec8 JH |
2058 | else { |
2059 | SvUVX(sv) = U_V(SvNVX(sv)); | |
2060 | if ( | |
2061 | (SvNVX(sv) == (NV) SvUVX(sv)) | |
2062 | #ifndef NV_PRESERVES_UV | |
2063 | /* Make sure it's not 0xFFFFFFFFFFFFFFFF */ | |
2064 | /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */ | |
2065 | && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv)) | |
2066 | /* Don't flag it as "accurately an integer" if the number | |
2067 | came from a (by definition imprecise) NV operation, and | |
2068 | we're outside the range of NV integer precision */ | |
2069 | #endif | |
2070 | ) | |
2071 | SvIOK_on(sv); | |
2072 | SvIsUV_on(sv); | |
1c846c1f | 2073 | DEBUG_c(PerlIO_printf(Perl_debug_log, |
28e5dec8 | 2074 | "0x%"UVxf" 2uv(%"UVuf" => %"IVdf") (as unsigned)\n", |
57def98f | 2075 | PTR2UV(sv), |
28e5dec8 JH |
2076 | SvUVX(sv), |
2077 | SvUVX(sv))); | |
25da4f38 | 2078 | } |
ff68c719 | 2079 | } |
2080 | else if (SvPOKp(sv) && SvLEN(sv)) { | |
25da4f38 IZ |
2081 | I32 numtype = looks_like_number(sv); |
2082 | ||
2083 | /* We want to avoid a possible problem when we cache a UV which | |
2084 | may be later translated to an NV, and the resulting NV is not | |
2085 | the translation of the initial data. | |
1c846c1f | 2086 | |
25da4f38 IZ |
2087 | This means that if we cache such a UV, we need to cache the |
2088 | NV as well. Moreover, we trade speed for space, and do not | |
2089 | cache the NV if not needed. | |
2090 | */ | |
16b7a9a4 | 2091 | |
28e5dec8 | 2092 | if ((numtype & ~IS_NUMBER_NEG) == IS_NUMBER_TO_INT_BY_ATOL) { |
f7bbb42a | 2093 | /* The NV may be reconstructed from IV - safe to cache IV, |
28e5dec8 JH |
2094 | which may be calculated by atol(). */ |
2095 | if (SvTYPE(sv) < SVt_PVIV) | |
f7bbb42a JH |
2096 | sv_upgrade(sv, SVt_PVIV); |
2097 | (void)SvIOK_on(sv); | |
28e5dec8 JH |
2098 | SvIVX(sv) = Atol(SvPVX(sv)); |
2099 | } else { | |
f7bbb42a | 2100 | #ifdef HAS_STRTOUL |
28e5dec8 | 2101 | UV u; |
f9172815 | 2102 | char *num_begin = SvPVX(sv); |
28e5dec8 | 2103 | int save_errno = errno; |
d460ef45 | 2104 | |
f9172815 JH |
2105 | /* seems that strtoul taking numbers that start with - is |
2106 | implementation dependant, and can't be relied upon. */ | |
2107 | if (numtype & IS_NUMBER_NEG) { | |
2108 | /* Not totally defensive. assumine that looks_like_num | |
2109 | didn't lie about a - sign */ | |
2110 | while (isSPACE(*num_begin)) | |
2111 | num_begin++; | |
2112 | if (*num_begin == '-') | |
2113 | num_begin++; | |
2114 | } | |
d460ef45 | 2115 | |
28e5dec8 JH |
2116 | /* Is it an integer that we could convert with strtoul? |
2117 | So try it, and if it doesn't set errno then it's pukka. | |
2118 | This should be faster than going atof and then thinking. */ | |
2119 | if (((numtype & (IS_NUMBER_TO_INT_BY_STRTOL | IS_NUMBER_NOT_INT)) | |
2120 | == IS_NUMBER_TO_INT_BY_STRTOL) | |
2121 | && ((errno = 0), 1) /* always true */ | |
f9172815 | 2122 | && ((u = Strtoul(num_begin, Null(char**), 10)), 1) /* ditto */ |
28e5dec8 | 2123 | && (errno == 0) |
d460ef45 | 2124 | /* If known to be negative, check it didn't undeflow IV |
f9172815 JH |
2125 | XXX possibly we should put more negative values as NVs |
2126 | direct rather than go via atof below */ | |
2127 | && ((numtype & IS_NUMBER_NEG) ? (u <= (UV)IV_MIN) : 1)) { | |
28e5dec8 JH |
2128 | errno = save_errno; |
2129 | ||
2130 | if (SvTYPE(sv) < SVt_PVIV) | |
2131 | sv_upgrade(sv, SVt_PVIV); | |
2132 | (void)SvIOK_on(sv); | |
2133 | ||
2134 | /* If it's negative must use IV. | |
2135 | IV-over-UV optimisation */ | |
f9172815 JH |
2136 | if (numtype & IS_NUMBER_NEG) { |
2137 | SvIVX(sv) = -(IV)u; | |
2138 | } else if (u <= (UV) IV_MAX) { | |
28e5dec8 JH |
2139 | SvIVX(sv) = (IV)u; |
2140 | } else { | |
2141 | /* it didn't overflow, and it was positive. */ | |
2142 | SvUVX(sv) = u; | |
2143 | SvIsUV_on(sv); | |
2144 | } | |
2145 | } else | |
f7bbb42a | 2146 | #endif |
28e5dec8 JH |
2147 | { |
2148 | NV d; | |
2149 | #ifdef HAS_STRTOUL | |
2150 | /* Hopefully trace flow will optimise this away where possible | |
2151 | */ | |
2152 | errno = save_errno; | |
2153 | #endif | |
2154 | /* It wasn't an integer, or it overflowed, or we don't have | |
2155 | strtol. Do things the slow way - check if it's a IV etc. */ | |
2156 | d = Atof(SvPVX(sv)); | |
2157 | ||
2158 | if (SvTYPE(sv) < SVt_PVNV) | |
2159 | sv_upgrade(sv, SVt_PVNV); | |
2160 | SvNVX(sv) = d; | |
2161 | ||
2162 | if (! numtype && ckWARN(WARN_NUMERIC)) | |
2163 | not_a_number(sv); | |
2164 | ||
2165 | #if defined(USE_LONG_DOUBLE) | |
2166 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%" PERL_PRIgldbl ")\n", | |
2167 | PTR2UV(sv), SvNVX(sv))); | |
2168 | #else | |
2169 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%g)\n", | |
2170 | PTR2UV(sv), SvNVX(sv))); | |
2171 | #endif | |
2172 | ||
2173 | #ifdef NV_PRESERVES_UV | |
2174 | (void)SvIOKp_on(sv); | |
2175 | (void)SvNOK_on(sv); | |
2176 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
2177 | SvIVX(sv) = I_V(SvNVX(sv)); | |
2178 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) { | |
2179 | SvIOK_on(sv); | |
2180 | } else { | |
2181 | /* Integer is imprecise. NOK, IOKp */ | |
2182 | } | |
2183 | /* UV will not work better than IV */ | |
2184 | } else { | |
2185 | if (SvNVX(sv) > (NV)UV_MAX) { | |
2186 | SvIsUV_on(sv); | |
2187 | /* Integer is inaccurate. NOK, IOKp, is UV */ | |
2188 | SvUVX(sv) = UV_MAX; | |
2189 | SvIsUV_on(sv); | |
2190 | } else { | |
2191 | SvUVX(sv) = U_V(SvNVX(sv)); | |
2192 | /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs | |
2193 | NV preservse UV so can do correct comparison. */ | |
2194 | if ((NV)(SvUVX(sv)) == SvNVX(sv)) { | |
2195 | SvIOK_on(sv); | |
2196 | SvIsUV_on(sv); | |
2197 | } else { | |
2198 | /* Integer is imprecise. NOK, IOKp, is UV */ | |
2199 | SvIsUV_on(sv); | |
2200 | } | |
2201 | } | |
2202 | } | |
2203 | #else /* NV_PRESERVES_UV */ | |
2204 | if (((UV)1 << NV_PRESERVES_UV_BITS) > | |
2205 | U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) { | |
2206 | /* Small enough to preserve all bits. */ | |
2207 | (void)SvIOKp_on(sv); | |
2208 | SvNOK_on(sv); | |
2209 | SvIVX(sv) = I_V(SvNVX(sv)); | |
2210 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) | |
2211 | SvIOK_on(sv); | |
2212 | /* Assumption: first non-preserved integer is < IV_MAX, | |
2213 | this NV is in the preserved range, therefore: */ | |
2214 | if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv)) | |
2215 | < (UV)IV_MAX)) { | |
2216 | Perl_croak(aTHX_ "sv_2uv assumed (U_V(fabs(SvNVX(sv))) < (UV)IV_MAX) but SvNVX(sv)=%g U_V is 0x%"UVxf", IV_MAX is 0x%"UVxf"\n", SvNVX(sv), U_V(SvNVX(sv)), (UV)IV_MAX); | |
2217 | } | |
2218 | } else | |
2219 | sv_2iuv_non_preserve (sv, numtype); | |
2220 | #endif /* NV_PRESERVES_UV */ | |
2221 | } | |
f7bbb42a | 2222 | } |
ff68c719 | 2223 | } |
2224 | else { | |
d008e5eb | 2225 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 2226 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
1d7c1841 | 2227 | report_uninit(); |
c6ee37c5 | 2228 | } |
25da4f38 IZ |
2229 | if (SvTYPE(sv) < SVt_IV) |
2230 | /* Typically the caller expects that sv_any is not NULL now. */ | |
2231 | sv_upgrade(sv, SVt_IV); | |
ff68c719 | 2232 | return 0; |
2233 | } | |
25da4f38 | 2234 | |
1d7c1841 GS |
2235 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n", |
2236 | PTR2UV(sv),SvUVX(sv))); | |
25da4f38 | 2237 | return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv); |
ff68c719 | 2238 | } |
2239 | ||
65202027 | 2240 | NV |
864dbfa3 | 2241 | Perl_sv_2nv(pTHX_ register SV *sv) |
79072805 LW |
2242 | { |
2243 | if (!sv) | |
2244 | return 0.0; | |
8990e307 | 2245 | if (SvGMAGICAL(sv)) { |
463ee0b2 LW |
2246 | mg_get(sv); |
2247 | if (SvNOKp(sv)) | |
2248 | return SvNVX(sv); | |
a0d0e21e | 2249 | if (SvPOKp(sv) && SvLEN(sv)) { |
599cee73 | 2250 | if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !looks_like_number(sv)) |
a0d0e21e | 2251 | not_a_number(sv); |
097ee67d | 2252 | return Atof(SvPVX(sv)); |
a0d0e21e | 2253 | } |
25da4f38 | 2254 | if (SvIOKp(sv)) { |
1c846c1f | 2255 | if (SvIsUV(sv)) |
65202027 | 2256 | return (NV)SvUVX(sv); |
25da4f38 | 2257 | else |
65202027 | 2258 | return (NV)SvIVX(sv); |
25da4f38 | 2259 | } |
16d20bd9 | 2260 | if (!SvROK(sv)) { |
d008e5eb | 2261 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 2262 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
1d7c1841 | 2263 | report_uninit(); |
c6ee37c5 | 2264 | } |
16d20bd9 AD |
2265 | return 0; |
2266 | } | |
463ee0b2 | 2267 | } |
ed6116ce | 2268 | if (SvTHINKFIRST(sv)) { |
a0d0e21e | 2269 | if (SvROK(sv)) { |
a0d0e21e | 2270 | SV* tmpstr; |
1554e226 | 2271 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) && |
1dc13c17 | 2272 | (SvTYPE(tmpstr) != SVt_RV || (SvRV(tmpstr) != SvRV(sv)))) |
9e7bc3e8 | 2273 | return SvNV(tmpstr); |
56431972 | 2274 | return PTR2NV(SvRV(sv)); |
a0d0e21e | 2275 | } |
8a818333 NIS |
2276 | if (SvREADONLY(sv) && SvFAKE(sv)) { |
2277 | sv_force_normal(sv); | |
2278 | } | |
0336b60e | 2279 | if (SvREADONLY(sv) && !SvOK(sv)) { |
599cee73 | 2280 | if (ckWARN(WARN_UNINITIALIZED)) |
1d7c1841 | 2281 | report_uninit(); |
ed6116ce LW |
2282 | return 0.0; |
2283 | } | |
79072805 LW |
2284 | } |
2285 | if (SvTYPE(sv) < SVt_NV) { | |
463ee0b2 LW |
2286 | if (SvTYPE(sv) == SVt_IV) |
2287 | sv_upgrade(sv, SVt_PVNV); | |
2288 | else | |
2289 | sv_upgrade(sv, SVt_NV); | |
572bbb43 | 2290 | #if defined(USE_LONG_DOUBLE) |
097ee67d | 2291 | DEBUG_c({ |
f93f4e46 | 2292 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
2293 | PerlIO_printf(Perl_debug_log, |
2294 | "0x%"UVxf" num(%" PERL_PRIgldbl ")\n", | |
2295 | PTR2UV(sv), SvNVX(sv)); | |
572bbb43 GS |
2296 | RESTORE_NUMERIC_LOCAL(); |
2297 | }); | |
65202027 | 2298 | #else |
572bbb43 | 2299 | DEBUG_c({ |
f93f4e46 | 2300 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
2301 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%g)\n", |
2302 | PTR2UV(sv), SvNVX(sv)); | |
097ee67d JH |
2303 | RESTORE_NUMERIC_LOCAL(); |
2304 | }); | |
572bbb43 | 2305 | #endif |
79072805 LW |
2306 | } |
2307 | else if (SvTYPE(sv) < SVt_PVNV) | |
2308 | sv_upgrade(sv, SVt_PVNV); | |
748a9306 LW |
2309 | if (SvIOKp(sv) && |
2310 | (!SvPOKp(sv) || !strchr(SvPVX(sv),'.') || !looks_like_number(sv))) | |
93a17b20 | 2311 | { |
65202027 | 2312 | SvNVX(sv) = SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv); |
28e5dec8 JH |
2313 | #ifdef NV_PRESERVES_UV |
2314 | SvNOK_on(sv); | |
2315 | #else | |
2316 | /* Only set the public NV OK flag if this NV preserves the IV */ | |
2317 | /* Check it's not 0xFFFFFFFFFFFFFFFF */ | |
2318 | if (SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv)))) | |
2319 | : (SvIVX(sv) == I_V(SvNVX(sv)))) | |
2320 | SvNOK_on(sv); | |
2321 | else | |
2322 | SvNOKp_on(sv); | |
2323 | #endif | |
93a17b20 | 2324 | } |
748a9306 | 2325 | else if (SvPOKp(sv) && SvLEN(sv)) { |
599cee73 | 2326 | if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !looks_like_number(sv)) |
a0d0e21e | 2327 | not_a_number(sv); |
097ee67d | 2328 | SvNVX(sv) = Atof(SvPVX(sv)); |
28e5dec8 JH |
2329 | #ifdef NV_PRESERVES_UV |
2330 | SvNOK_on(sv); | |
2331 | #else | |
2332 | /* Only set the public NV OK flag if this NV preserves the value in | |
2333 | the PV at least as well as an IV/UV would. | |
2334 | Not sure how to do this 100% reliably. */ | |
2335 | /* if that shift count is out of range then Configure's test is | |
2336 | wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS == | |
2337 | UV_BITS */ | |
2338 | if (((UV)1 << NV_PRESERVES_UV_BITS) > | |
2339 | U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) | |
2340 | SvNOK_on(sv); /* Definitely small enough to preserve all bits */ | |
2341 | else if (SvNVX(sv) < (NV)IV_MIN || SvNVX(sv) > (NV)UV_MAX) { | |
2342 | /* Definitely too large/small to fit in an integer, so no loss | |
2343 | of precision going to integer in the future via NV */ | |
2344 | SvNOK_on(sv); | |
2345 | } else { | |
2346 | /* Is it something we can run through strtol etc (ie no | |
2347 | trailing exponent part)? */ | |
2348 | int numtype = looks_like_number(sv); | |
2349 | /* XXX probably should cache this if called above */ | |
2350 | ||
2351 | if (!(numtype & | |
2352 | (IS_NUMBER_TO_INT_BY_ATOL | IS_NUMBER_TO_INT_BY_STRTOL))) { | |
2353 | /* Can't use strtol etc to convert this string, so don't try */ | |
2354 | SvNOK_on(sv); | |
2355 | } else | |
2356 | sv_2inuv_non_preserve (sv, numtype); | |
2357 | } | |
2358 | #endif /* NV_PRESERVES_UV */ | |
93a17b20 | 2359 | } |
79072805 | 2360 | else { |
599cee73 | 2361 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP)) |
1d7c1841 | 2362 | report_uninit(); |
25da4f38 IZ |
2363 | if (SvTYPE(sv) < SVt_NV) |
2364 | /* Typically the caller expects that sv_any is not NULL now. */ | |
28e5dec8 JH |
2365 | /* XXX Ilya implies that this is a bug in callers that assume this |
2366 | and ideally should be fixed. */ | |
25da4f38 | 2367 | sv_upgrade(sv, SVt_NV); |
a0d0e21e | 2368 | return 0.0; |
79072805 | 2369 | } |
572bbb43 | 2370 | #if defined(USE_LONG_DOUBLE) |
097ee67d | 2371 | DEBUG_c({ |
f93f4e46 | 2372 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
2373 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n", |
2374 | PTR2UV(sv), SvNVX(sv)); | |
572bbb43 GS |
2375 | RESTORE_NUMERIC_LOCAL(); |
2376 | }); | |
65202027 | 2377 | #else |
572bbb43 | 2378 | DEBUG_c({ |
f93f4e46 | 2379 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
2380 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%g)\n", |
2381 | PTR2UV(sv), SvNVX(sv)); | |
097ee67d JH |
2382 | RESTORE_NUMERIC_LOCAL(); |
2383 | }); | |
572bbb43 | 2384 | #endif |
463ee0b2 | 2385 | return SvNVX(sv); |
79072805 LW |
2386 | } |
2387 | ||
76e3520e | 2388 | STATIC IV |
cea2e8a9 | 2389 | S_asIV(pTHX_ SV *sv) |
36477c24 | 2390 | { |
2391 | I32 numtype = looks_like_number(sv); | |
65202027 | 2392 | NV d; |
36477c24 | 2393 | |
25da4f38 | 2394 | if (numtype & IS_NUMBER_TO_INT_BY_ATOL) |
cf2093f6 | 2395 | return Atol(SvPVX(sv)); |
d008e5eb | 2396 | if (!numtype) { |
d008e5eb GS |
2397 | if (ckWARN(WARN_NUMERIC)) |
2398 | not_a_number(sv); | |
2399 | } | |
097ee67d | 2400 | d = Atof(SvPVX(sv)); |
25da4f38 | 2401 | return I_V(d); |
36477c24 | 2402 | } |
2403 | ||
76e3520e | 2404 | STATIC UV |
cea2e8a9 | 2405 | S_asUV(pTHX_ SV *sv) |
36477c24 | 2406 | { |
2407 | I32 numtype = looks_like_number(sv); | |
2408 | ||
84902520 | 2409 | #ifdef HAS_STRTOUL |
25da4f38 | 2410 | if (numtype & IS_NUMBER_TO_INT_BY_ATOL) |
cf2093f6 | 2411 | return Strtoul(SvPVX(sv), Null(char**), 10); |
84902520 | 2412 | #endif |
d008e5eb | 2413 | if (!numtype) { |
d008e5eb GS |
2414 | if (ckWARN(WARN_NUMERIC)) |
2415 | not_a_number(sv); | |
2416 | } | |
097ee67d | 2417 | return U_V(Atof(SvPVX(sv))); |
36477c24 | 2418 | } |
2419 | ||
25da4f38 IZ |
2420 | /* |
2421 | * Returns a combination of (advisory only - can get false negatives) | |
28e5dec8 JH |
2422 | * IS_NUMBER_TO_INT_BY_ATOL, IS_NUMBER_TO_INT_BY_ATOF |
2423 | * IS_NUMBER_LONGER_THAN_IV_MAX, IS_NUMBER_AS_LONG_AS_IV_MAX | |
2424 | * IS_NUMBER_NOT_INT, IS_NUMBER_NEG, IS_NUMBER_INFINITY | |
25da4f38 IZ |
2425 | * 0 if does not look like number. |
2426 | * | |
28e5dec8 JH |
2427 | * (atol and strtol stop when they hit a decimal point. strtol will return |
2428 | * LONG_MAX and LONG_MIN when given out of range values. ANSI says they should | |
2429 | * do this, and vendors have had 11 years to get it right. | |
2430 | * However, will try to make it still work with only atol | |
d460ef45 | 2431 | * |
28e5dec8 JH |
2432 | * IS_NUMBER_TO_INT_BY_ATOL 123456789 or 123456789.3 definitely < IV_MAX |
2433 | * IS_NUMBER_TO_INT_BY_STRTOL 123456789 or 123456789.3 if digits = IV_MAX | |
2434 | * IS_NUMBER_TO_INT_BY_ATOF 123456789e0 or >> IV_MAX | |
2435 | * IS_NUMBER_LONGER_THAN_IV_MAX lots of digits, don't bother with atol | |
2436 | * IS_NUMBER_AS_LONG_AS_IV_MAX atol might hit LONG_MAX, might not. | |
2437 | * IS_NUMBER_NOT_INT saw "." or "e" | |
2438 | * IS_NUMBER_NEG | |
300aed98 | 2439 | * IS_NUMBER_INFINITY |
25da4f38 IZ |
2440 | */ |
2441 | ||
954c1994 GS |
2442 | /* |
2443 | =for apidoc looks_like_number | |
2444 | ||
2445 | Test if an the content of an SV looks like a number (or is a | |
28e5dec8 JH |
2446 | number). C<Inf> and C<Infinity> are treated as numbers (so will not |
2447 | issue a non-numeric warning), even if your atof() doesn't grok them. | |
954c1994 GS |
2448 | |
2449 | =cut | |
2450 | */ | |
2451 | ||
36477c24 | 2452 | I32 |
864dbfa3 | 2453 | Perl_looks_like_number(pTHX_ SV *sv) |
36477c24 | 2454 | { |
2455 | register char *s; | |
2456 | register char *send; | |
2457 | register char *sbegin; | |
25da4f38 IZ |
2458 | register char *nbegin; |
2459 | I32 numtype = 0; | |
300aed98 | 2460 | I32 sawinf = 0; |
36477c24 | 2461 | STRLEN len; |
9c7192ba | 2462 | #ifdef USE_LOCALE_NUMERIC |
eff180cd | 2463 | bool specialradix = FALSE; |
9c7192ba | 2464 | #endif |
36477c24 | 2465 | |
2466 | if (SvPOK(sv)) { | |
1c846c1f | 2467 | sbegin = SvPVX(sv); |
36477c24 | 2468 | len = SvCUR(sv); |
2469 | } | |
2470 | else if (SvPOKp(sv)) | |
2471 | sbegin = SvPV(sv, len); | |
2472 | else | |
2473 | return 1; | |
2474 | send = sbegin + len; | |
2475 | ||
2476 | s = sbegin; | |
2477 | while (isSPACE(*s)) | |
2478 | s++; | |
25da4f38 IZ |
2479 | if (*s == '-') { |
2480 | s++; | |
2481 | numtype = IS_NUMBER_NEG; | |
2482 | } | |
2483 | else if (*s == '+') | |
36477c24 | 2484 | s++; |
ff0cee69 | 2485 | |
25da4f38 IZ |
2486 | nbegin = s; |
2487 | /* | |
d460ef45 | 2488 | * we return IS_NUMBER_TO_INT_BY_ATOL if the number can converted to |
28e5dec8 JH |
2489 | * integer with atol() without overflow, IS_NUMBER_TO_INT_BY_STRTOL if |
2490 | * possibly slightly larger than max int, IS_NUMBER_TO_INT_BY_ATOF if you | |
2491 | * will need (int)atof(). | |
25da4f38 IZ |
2492 | */ |
2493 | ||
300aed98 | 2494 | /* next must be digit or the radix separator or beginning of infinity */ |
ff0cee69 | 2495 | if (isDIGIT(*s)) { |
2496 | do { | |
2497 | s++; | |
2498 | } while (isDIGIT(*s)); | |
25da4f38 | 2499 | |
28e5dec8 JH |
2500 | /* Aaargh. long long really is irritating. |
2501 | In the gospel according to ANSI 1989, it is an axiom that "long" | |
2502 | is the longest integer type, and that if you don't know how long | |
2503 | something is you can cast it to long, and nothing will be lost | |
2504 | (except possibly speed of execution if long is slower than the | |
2505 | type is was). | |
2506 | Now, one can't be sure if the old rules apply, or long long | |
2507 | (or some other newfangled thing) is actually longer than the | |
2508 | (formerly) longest thing. | |
2509 | */ | |
2510 | /* This lot will work for 64 bit *as long as* either | |
2511 | either long is 64 bit | |
2512 | or we can find both strtol/strtoq and strtoul/strtouq | |
2513 | If not, we really should refuse to let the user use 64 bit IVs | |
2514 | By "64 bit" I really mean IVs that don't get preserved by NVs | |
2515 | It also should work for 128 bit IVs. Can any lend me a machine to | |
2516 | test this? | |
2517 | */ | |
2518 | if (s - nbegin > TYPE_DIGITS(UV)) /* Cannot cache ato[ul]() */ | |
2519 | numtype |= IS_NUMBER_TO_INT_BY_ATOF | IS_NUMBER_LONGER_THAN_IV_MAX; | |
2520 | else if (s - nbegin < BIT_DIGITS(((sizeof (IV)>sizeof (long)) | |
2521 | ? sizeof(long) : sizeof (IV))*8-1)) | |
f7bbb42a | 2522 | numtype |= IS_NUMBER_TO_INT_BY_ATOL; |
28e5dec8 JH |
2523 | else |
2524 | /* Can't be sure either way. (For 64 bit UV, 63 bit IV is 1 decimal | |
2525 | digit less (IV_MAX= 9223372036854775807, | |
2526 | UV_MAX= 18446744073709551615) so be cautious */ | |
2527 | numtype |= IS_NUMBER_TO_INT_BY_STRTOL | IS_NUMBER_AS_LONG_AS_IV_MAX; | |
25da4f38 | 2528 | |
097ee67d | 2529 | if (*s == '.' |
1c846c1f | 2530 | #ifdef USE_LOCALE_NUMERIC |
eff180cd | 2531 | || (specialradix = IS_NUMERIC_RADIX(s)) |
097ee67d JH |
2532 | #endif |
2533 | ) { | |
9c7192ba | 2534 | #ifdef USE_LOCALE_NUMERIC |
eff180cd | 2535 | if (specialradix) |
a453c169 | 2536 | s += SvCUR(PL_numeric_radix_sv); |
eff180cd | 2537 | else |
9c7192ba | 2538 | #endif |
eff180cd | 2539 | s++; |
28e5dec8 | 2540 | numtype |= IS_NUMBER_NOT_INT; |
097ee67d | 2541 | while (isDIGIT(*s)) /* optional digits after the radix */ |
ff0cee69 | 2542 | s++; |
2543 | } | |
36477c24 | 2544 | } |
097ee67d | 2545 | else if (*s == '.' |
1c846c1f | 2546 | #ifdef USE_LOCALE_NUMERIC |
eff180cd | 2547 | || (specialradix = IS_NUMERIC_RADIX(s)) |
097ee67d JH |
2548 | #endif |
2549 | ) { | |
9c7192ba | 2550 | #ifdef USE_LOCALE_NUMERIC |
eff180cd | 2551 | if (specialradix) |
a453c169 | 2552 | s += SvCUR(PL_numeric_radix_sv); |
eff180cd | 2553 | else |
9c7192ba | 2554 | #endif |
eff180cd | 2555 | s++; |
28e5dec8 | 2556 | numtype |= IS_NUMBER_TO_INT_BY_ATOL | IS_NUMBER_NOT_INT; |
097ee67d | 2557 | /* no digits before the radix means we need digits after it */ |
ff0cee69 | 2558 | if (isDIGIT(*s)) { |
2559 | do { | |
2560 | s++; | |
2561 | } while (isDIGIT(*s)); | |
2562 | } | |
2563 | else | |
2564 | return 0; | |
2565 | } | |
300aed98 JH |
2566 | else if (*s == 'I' || *s == 'i') { |
2567 | s++; if (*s != 'N' && *s != 'n') return 0; | |
2568 | s++; if (*s != 'F' && *s != 'f') return 0; | |
2569 | s++; if (*s == 'I' || *s == 'i') { | |
2570 | s++; if (*s != 'N' && *s != 'n') return 0; | |
2571 | s++; if (*s != 'I' && *s != 'i') return 0; | |
2572 | s++; if (*s != 'T' && *s != 't') return 0; | |
2573 | s++; if (*s != 'Y' && *s != 'y') return 0; | |
99938567 | 2574 | s++; |
300aed98 JH |
2575 | } |
2576 | sawinf = 1; | |
2577 | } | |
ff0cee69 | 2578 | else |
2579 | return 0; | |
2580 | ||
300aed98 | 2581 | if (sawinf) |
28e5dec8 JH |
2582 | numtype = (numtype & IS_NUMBER_NEG) /* Keep track of sign */ |
2583 | | IS_NUMBER_INFINITY | IS_NUMBER_NOT_INT; | |
300aed98 JH |
2584 | else { |
2585 | /* we can have an optional exponent part */ | |
2586 | if (*s == 'e' || *s == 'E') { | |
28e5dec8 JH |
2587 | numtype &= IS_NUMBER_NEG; |
2588 | numtype |= IS_NUMBER_TO_INT_BY_ATOF | IS_NUMBER_NOT_INT; | |
36477c24 | 2589 | s++; |
300aed98 JH |
2590 | if (*s == '+' || *s == '-') |
2591 | s++; | |
2592 | if (isDIGIT(*s)) { | |
2593 | do { | |
2594 | s++; | |
2595 | } while (isDIGIT(*s)); | |
2596 | } | |
2597 | else | |
2598 | return 0; | |
2599 | } | |
36477c24 | 2600 | } |
2601 | while (isSPACE(*s)) | |
2602 | s++; | |
80f3f388 | 2603 | if (s >= send) |
36477c24 | 2604 | return numtype; |
2605 | if (len == 10 && memEQ(sbegin, "0 but true", 10)) | |
25da4f38 | 2606 | return IS_NUMBER_TO_INT_BY_ATOL; |
36477c24 | 2607 | return 0; |
2608 | } | |
2609 | ||
79072805 | 2610 | char * |
864dbfa3 | 2611 | Perl_sv_2pv_nolen(pTHX_ register SV *sv) |
1fa8b10d JD |
2612 | { |
2613 | STRLEN n_a; | |
2614 | return sv_2pv(sv, &n_a); | |
2615 | } | |
2616 | ||
25da4f38 | 2617 | /* We assume that buf is at least TYPE_CHARS(UV) long. */ |
864dbfa3 | 2618 | static char * |
25da4f38 IZ |
2619 | uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob) |
2620 | { | |
25da4f38 IZ |
2621 | char *ptr = buf + TYPE_CHARS(UV); |
2622 | char *ebuf = ptr; | |
2623 | int sign; | |
25da4f38 IZ |
2624 | |
2625 | if (is_uv) | |
2626 | sign = 0; | |
2627 | else if (iv >= 0) { | |
2628 | uv = iv; | |
2629 | sign = 0; | |
2630 | } else { | |
2631 | uv = -iv; | |
2632 | sign = 1; | |
2633 | } | |
2634 | do { | |
2635 | *--ptr = '0' + (uv % 10); | |
2636 | } while (uv /= 10); | |
2637 | if (sign) | |
2638 | *--ptr = '-'; | |
2639 | *peob = ebuf; | |
2640 | return ptr; | |
2641 | } | |
2642 | ||
1fa8b10d | 2643 | char * |
864dbfa3 | 2644 | Perl_sv_2pv(pTHX_ register SV *sv, STRLEN *lp) |
79072805 LW |
2645 | { |
2646 | register char *s; | |
2647 | int olderrno; | |
46fc3d4c | 2648 | SV *tsv; |
25da4f38 IZ |
2649 | char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */ |
2650 | char *tmpbuf = tbuf; | |
79072805 | 2651 | |
463ee0b2 LW |
2652 | if (!sv) { |
2653 | *lp = 0; | |
2654 | return ""; | |
2655 | } | |
8990e307 | 2656 | if (SvGMAGICAL(sv)) { |
463ee0b2 LW |
2657 | mg_get(sv); |
2658 | if (SvPOKp(sv)) { | |
2659 | *lp = SvCUR(sv); | |
2660 | return SvPVX(sv); | |
2661 | } | |
cf2093f6 | 2662 | if (SvIOKp(sv)) { |
1c846c1f | 2663 | if (SvIsUV(sv)) |
57def98f | 2664 | (void)sprintf(tmpbuf,"%"UVuf, (UV)SvUVX(sv)); |
cf2093f6 | 2665 | else |
57def98f | 2666 | (void)sprintf(tmpbuf,"%"IVdf, (IV)SvIVX(sv)); |
46fc3d4c | 2667 | tsv = Nullsv; |
a0d0e21e | 2668 | goto tokensave; |
463ee0b2 LW |
2669 | } |
2670 | if (SvNOKp(sv)) { | |
2d4389e4 | 2671 | Gconvert(SvNVX(sv), NV_DIG, 0, tmpbuf); |
46fc3d4c | 2672 | tsv = Nullsv; |
a0d0e21e | 2673 | goto tokensave; |
463ee0b2 | 2674 | } |
16d20bd9 | 2675 | if (!SvROK(sv)) { |
d008e5eb | 2676 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 2677 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
1d7c1841 | 2678 | report_uninit(); |
c6ee37c5 | 2679 | } |
16d20bd9 AD |
2680 | *lp = 0; |
2681 | return ""; | |
2682 | } | |
463ee0b2 | 2683 | } |
ed6116ce LW |
2684 | if (SvTHINKFIRST(sv)) { |
2685 | if (SvROK(sv)) { | |
a0d0e21e | 2686 | SV* tmpstr; |
1554e226 | 2687 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)) && |
1dc13c17 | 2688 | (SvTYPE(tmpstr) != SVt_RV || (SvRV(tmpstr) != SvRV(sv)))) |
9e7bc3e8 | 2689 | return SvPV(tmpstr,*lp); |
ed6116ce LW |
2690 | sv = (SV*)SvRV(sv); |
2691 | if (!sv) | |
2692 | s = "NULLREF"; | |
2693 | else { | |
f9277f47 IZ |
2694 | MAGIC *mg; |
2695 | ||
ed6116ce | 2696 | switch (SvTYPE(sv)) { |
f9277f47 IZ |
2697 | case SVt_PVMG: |
2698 | if ( ((SvFLAGS(sv) & | |
1c846c1f | 2699 | (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG)) |
3149a8e4 | 2700 | == (SVs_OBJECT|SVs_RMG)) |
57668c4d | 2701 | && strEQ(s=HvNAME(SvSTASH(sv)), "Regexp") |
14befaf4 | 2702 | && (mg = mg_find(sv, PERL_MAGIC_qr))) { |
2cd61cdb | 2703 | regexp *re = (regexp *)mg->mg_obj; |
1bd3ad17 | 2704 | |
2cd61cdb | 2705 | if (!mg->mg_ptr) { |
8782bef2 GB |
2706 | char *fptr = "msix"; |
2707 | char reflags[6]; | |
2708 | char ch; | |
2709 | int left = 0; | |
2710 | int right = 4; | |
2711 | U16 reganch = (re->reganch & PMf_COMPILETIME) >> 12; | |
2712 | ||
155aba94 | 2713 | while((ch = *fptr++)) { |
8782bef2 GB |
2714 | if(reganch & 1) { |
2715 | reflags[left++] = ch; | |
2716 | } | |
2717 | else { | |
2718 | reflags[right--] = ch; | |
2719 | } | |
2720 | reganch >>= 1; | |
2721 | } | |
2722 | if(left != 4) { | |
2723 | reflags[left] = '-'; | |
2724 | left = 5; | |
2725 | } | |
2726 | ||
2727 | mg->mg_len = re->prelen + 4 + left; | |
2728 | New(616, mg->mg_ptr, mg->mg_len + 1 + left, char); | |
2729 | Copy("(?", mg->mg_ptr, 2, char); | |
2730 | Copy(reflags, mg->mg_ptr+2, left, char); | |
2731 | Copy(":", mg->mg_ptr+left+2, 1, char); | |
2732 | Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char); | |
1bd3ad17 IZ |
2733 | mg->mg_ptr[mg->mg_len - 1] = ')'; |
2734 | mg->mg_ptr[mg->mg_len] = 0; | |
2735 | } | |
3280af22 | 2736 | PL_reginterp_cnt += re->program[0].next_off; |
1bd3ad17 IZ |
2737 | *lp = mg->mg_len; |
2738 | return mg->mg_ptr; | |
f9277f47 IZ |
2739 | } |
2740 | /* Fall through */ | |
ed6116ce LW |
2741 | case SVt_NULL: |
2742 | case SVt_IV: | |
2743 | case SVt_NV: | |
2744 | case SVt_RV: | |
2745 | case SVt_PV: | |
2746 | case SVt_PVIV: | |
2747 | case SVt_PVNV: | |
81689caa HS |
2748 | case SVt_PVBM: if (SvROK(sv)) |
2749 | s = "REF"; | |
2750 | else | |
2751 | s = "SCALAR"; break; | |
ed6116ce LW |
2752 | case SVt_PVLV: s = "LVALUE"; break; |
2753 | case SVt_PVAV: s = "ARRAY"; break; | |
2754 | case SVt_PVHV: s = "HASH"; break; | |
2755 | case SVt_PVCV: s = "CODE"; break; | |
2756 | case SVt_PVGV: s = "GLOB"; break; | |
1d2dff63 | 2757 | case SVt_PVFM: s = "FORMAT"; break; |
36477c24 | 2758 | case SVt_PVIO: s = "IO"; break; |
ed6116ce LW |
2759 | default: s = "UNKNOWN"; break; |
2760 | } | |
46fc3d4c | 2761 | tsv = NEWSV(0,0); |
ed6116ce | 2762 | if (SvOBJECT(sv)) |
cea2e8a9 | 2763 | Perl_sv_setpvf(aTHX_ tsv, "%s=%s", HvNAME(SvSTASH(sv)), s); |
ed6116ce | 2764 | else |
46fc3d4c | 2765 | sv_setpv(tsv, s); |
57def98f | 2766 | Perl_sv_catpvf(aTHX_ tsv, "(0x%"UVxf")", PTR2UV(sv)); |
a0d0e21e | 2767 | goto tokensaveref; |
463ee0b2 | 2768 | } |
ed6116ce LW |
2769 | *lp = strlen(s); |
2770 | return s; | |
79072805 | 2771 | } |
0336b60e | 2772 | if (SvREADONLY(sv) && !SvOK(sv)) { |
0336b60e | 2773 | if (ckWARN(WARN_UNINITIALIZED)) |
1d7c1841 | 2774 | report_uninit(); |
ed6116ce LW |
2775 | *lp = 0; |
2776 | return ""; | |
79072805 | 2777 | } |
79072805 | 2778 | } |
28e5dec8 JH |
2779 | if (SvIOK(sv) || ((SvIOKp(sv) && !SvNOKp(sv)))) { |
2780 | /* I'm assuming that if both IV and NV are equally valid then | |
2781 | converting the IV is going to be more efficient */ | |
2782 | U32 isIOK = SvIOK(sv); | |
2783 | U32 isUIOK = SvIsUV(sv); | |
2784 | char buf[TYPE_CHARS(UV)]; | |
2785 | char *ebuf, *ptr; | |
2786 | ||
2787 | if (SvTYPE(sv) < SVt_PVIV) | |
2788 | sv_upgrade(sv, SVt_PVIV); | |
2789 | if (isUIOK) | |
2790 | ptr = uiv_2buf(buf, 0, SvUVX(sv), 1, &ebuf); | |
2791 | else | |
2792 | ptr = uiv_2buf(buf, SvIVX(sv), 0, 0, &ebuf); | |
2793 | SvGROW(sv, ebuf - ptr + 1); /* inlined from sv_setpvn */ | |
2794 | Move(ptr,SvPVX(sv),ebuf - ptr,char); | |
2795 | SvCUR_set(sv, ebuf - ptr); | |
2796 | s = SvEND(sv); | |
2797 | *s = '\0'; | |
2798 | if (isIOK) | |
2799 | SvIOK_on(sv); | |
2800 | else | |
2801 | SvIOKp_on(sv); | |
2802 | if (isUIOK) | |
2803 | SvIsUV_on(sv); | |
2804 | } | |
2805 | else if (SvNOKp(sv)) { | |
79072805 LW |
2806 | if (SvTYPE(sv) < SVt_PVNV) |
2807 | sv_upgrade(sv, SVt_PVNV); | |
1c846c1f | 2808 | /* The +20 is pure guesswork. Configure test needed. --jhi */ |
59155cc0 | 2809 | SvGROW(sv, NV_DIG + 20); |
463ee0b2 | 2810 | s = SvPVX(sv); |
79072805 | 2811 | olderrno = errno; /* some Xenix systems wipe out errno here */ |
79072805 | 2812 | #ifdef apollo |
463ee0b2 | 2813 | if (SvNVX(sv) == 0.0) |
79072805 LW |
2814 | (void)strcpy(s,"0"); |
2815 | else | |
2816 | #endif /*apollo*/ | |
bbce6d69 | 2817 | { |
2d4389e4 | 2818 | Gconvert(SvNVX(sv), NV_DIG, 0, s); |
bbce6d69 | 2819 | } |
79072805 | 2820 | errno = olderrno; |
a0d0e21e LW |
2821 | #ifdef FIXNEGATIVEZERO |
2822 | if (*s == '-' && s[1] == '0' && !s[2]) | |
2823 | strcpy(s,"0"); | |
2824 | #endif | |
79072805 LW |
2825 | while (*s) s++; |
2826 | #ifdef hcx | |
2827 | if (s[-1] == '.') | |
46fc3d4c | 2828 | *--s = '\0'; |
79072805 LW |
2829 | #endif |
2830 | } | |
79072805 | 2831 | else { |
0336b60e IZ |
2832 | if (ckWARN(WARN_UNINITIALIZED) |
2833 | && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP)) | |
1d7c1841 | 2834 | report_uninit(); |
a0d0e21e | 2835 | *lp = 0; |
25da4f38 IZ |
2836 | if (SvTYPE(sv) < SVt_PV) |
2837 | /* Typically the caller expects that sv_any is not NULL now. */ | |
2838 | sv_upgrade(sv, SVt_PV); | |
a0d0e21e | 2839 | return ""; |
79072805 | 2840 | } |
463ee0b2 LW |
2841 | *lp = s - SvPVX(sv); |
2842 | SvCUR_set(sv, *lp); | |
79072805 | 2843 | SvPOK_on(sv); |
1d7c1841 GS |
2844 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n", |
2845 | PTR2UV(sv),SvPVX(sv))); | |
463ee0b2 | 2846 | return SvPVX(sv); |
a0d0e21e LW |
2847 | |
2848 | tokensave: | |
2849 | if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */ | |
2850 | /* Sneaky stuff here */ | |
2851 | ||
2852 | tokensaveref: | |
46fc3d4c | 2853 | if (!tsv) |
96827780 | 2854 | tsv = newSVpv(tmpbuf, 0); |
46fc3d4c | 2855 | sv_2mortal(tsv); |
2856 | *lp = SvCUR(tsv); | |
2857 | return SvPVX(tsv); | |
a0d0e21e LW |
2858 | } |
2859 | else { | |
2860 | STRLEN len; | |
46fc3d4c | 2861 | char *t; |
2862 | ||
2863 | if (tsv) { | |
2864 | sv_2mortal(tsv); | |
2865 | t = SvPVX(tsv); | |
2866 | len = SvCUR(tsv); | |
2867 | } | |
2868 | else { | |
96827780 MB |
2869 | t = tmpbuf; |
2870 | len = strlen(tmpbuf); | |
46fc3d4c | 2871 | } |
a0d0e21e | 2872 | #ifdef FIXNEGATIVEZERO |
46fc3d4c | 2873 | if (len == 2 && t[0] == '-' && t[1] == '0') { |
2874 | t = "0"; | |
2875 | len = 1; | |
2876 | } | |
a0d0e21e LW |
2877 | #endif |
2878 | (void)SvUPGRADE(sv, SVt_PV); | |
46fc3d4c | 2879 | *lp = len; |
a0d0e21e LW |
2880 | s = SvGROW(sv, len + 1); |
2881 | SvCUR_set(sv, len); | |
46fc3d4c | 2882 | (void)strcpy(s, t); |
6bf554b4 | 2883 | SvPOKp_on(sv); |
a0d0e21e LW |
2884 | return s; |
2885 | } | |
463ee0b2 LW |
2886 | } |
2887 | ||
7340a771 GS |
2888 | char * |
2889 | Perl_sv_2pvbyte_nolen(pTHX_ register SV *sv) | |
2890 | { | |
560a288e GS |
2891 | STRLEN n_a; |
2892 | return sv_2pvbyte(sv, &n_a); | |
7340a771 GS |
2893 | } |
2894 | ||
2895 | char * | |
2896 | Perl_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp) | |
2897 | { | |
0875d2fe NIS |
2898 | sv_utf8_downgrade(sv,0); |
2899 | return SvPV(sv,*lp); | |
7340a771 GS |
2900 | } |
2901 | ||
2902 | char * | |
2903 | Perl_sv_2pvutf8_nolen(pTHX_ register SV *sv) | |
2904 | { | |
560a288e GS |
2905 | STRLEN n_a; |
2906 | return sv_2pvutf8(sv, &n_a); | |
7340a771 GS |
2907 | } |
2908 | ||
2909 | char * | |
2910 | Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp) | |
2911 | { | |
560a288e | 2912 | sv_utf8_upgrade(sv); |
7d59b7e4 | 2913 | return SvPV(sv,*lp); |
7340a771 | 2914 | } |
1c846c1f | 2915 | |
463ee0b2 LW |
2916 | /* This function is only called on magical items */ |
2917 | bool | |
864dbfa3 | 2918 | Perl_sv_2bool(pTHX_ register SV *sv) |
463ee0b2 | 2919 | { |
8990e307 | 2920 | if (SvGMAGICAL(sv)) |
463ee0b2 LW |
2921 | mg_get(sv); |
2922 | ||
a0d0e21e LW |
2923 | if (!SvOK(sv)) |
2924 | return 0; | |
2925 | if (SvROK(sv)) { | |
a0d0e21e | 2926 | SV* tmpsv; |
1554e226 | 2927 | if (SvAMAGIC(sv) && (tmpsv=AMG_CALLun(sv,bool_)) && |
1dc13c17 | 2928 | (SvTYPE(tmpsv) != SVt_RV || (SvRV(tmpsv) != SvRV(sv)))) |
9e7bc3e8 | 2929 | return SvTRUE(tmpsv); |
a0d0e21e LW |
2930 | return SvRV(sv) != 0; |
2931 | } | |
463ee0b2 | 2932 | if (SvPOKp(sv)) { |
11343788 MB |
2933 | register XPV* Xpvtmp; |
2934 | if ((Xpvtmp = (XPV*)SvANY(sv)) && | |
2935 | (*Xpvtmp->xpv_pv > '0' || | |
2936 | Xpvtmp->xpv_cur > 1 || | |
2937 | (Xpvtmp->xpv_cur && *Xpvtmp->xpv_pv != '0'))) | |
463ee0b2 LW |
2938 | return 1; |
2939 | else | |
2940 | return 0; | |
2941 | } | |
2942 | else { | |
2943 | if (SvIOKp(sv)) | |
2944 | return SvIVX(sv) != 0; | |
2945 | else { | |
2946 | if (SvNOKp(sv)) | |
2947 | return SvNVX(sv) != 0.0; | |
2948 | else | |
2949 | return FALSE; | |
2950 | } | |
2951 | } | |
79072805 LW |
2952 | } |
2953 | ||
c461cf8f JH |
2954 | /* |
2955 | =for apidoc sv_utf8_upgrade | |
2956 | ||
2957 | Convert the PV of an SV to its UTF8-encoded form. | |
4411f3b6 NIS |
2958 | Forces the SV to string form it it is not already. |
2959 | Always sets the SvUTF8 flag to avoid future validity checks even | |
2960 | if all the bytes have hibit clear. | |
c461cf8f JH |
2961 | |
2962 | =cut | |
2963 | */ | |
2964 | ||
4411f3b6 | 2965 | STRLEN |
560a288e GS |
2966 | Perl_sv_utf8_upgrade(pTHX_ register SV *sv) |
2967 | { | |
db42d148 | 2968 | U8 *s, *t, *e; |
511c2ff0 | 2969 | int hibit = 0; |
560a288e | 2970 | |
4411f3b6 NIS |
2971 | if (!sv) |
2972 | return 0; | |
2973 | ||
e0e62c2a NIS |
2974 | if (!SvPOK(sv)) { |
2975 | STRLEN len = 0; | |
2976 | (void) sv_2pv(sv,&len); | |
2977 | if (!SvPOK(sv)) | |
2978 | return len; | |
2979 | } | |
4411f3b6 NIS |
2980 | |
2981 | if (SvUTF8(sv)) | |
2982 | return SvCUR(sv); | |
560a288e | 2983 | |
db42d148 NIS |
2984 | if (SvREADONLY(sv) && SvFAKE(sv)) { |
2985 | sv_force_normal(sv); | |
2986 | } | |
2987 | ||
40826f67 JH |
2988 | /* This function could be much more efficient if we had a FLAG in SVs |
2989 | * to signal if there are any hibit chars in the PV. | |
511c2ff0 | 2990 | * Given that there isn't make loop fast as possible |
560a288e | 2991 | */ |
db42d148 NIS |
2992 | s = (U8 *) SvPVX(sv); |
2993 | e = (U8 *) SvEND(sv); | |
511c2ff0 NIS |
2994 | t = s; |
2995 | while (t < e) { | |
c4d5f83a NIS |
2996 | U8 ch = *t++; |
2997 | if ((hibit = !NATIVE_IS_INVARIANT(ch))) | |
8a818333 | 2998 | break; |
8a818333 | 2999 | } |
40826f67 | 3000 | if (hibit) { |
8a818333 | 3001 | STRLEN len; |
652088fc | 3002 | |
8a818333 | 3003 | len = SvCUR(sv) + 1; /* Plus the \0 */ |
00df9076 | 3004 | SvPVX(sv) = (char*)bytes_to_utf8((U8*)s, &len); |
841d7a39 | 3005 | SvCUR(sv) = len - 1; |
511c2ff0 NIS |
3006 | if (SvLEN(sv) != 0) |
3007 | Safefree(s); /* No longer using what was there before. */ | |
841d7a39 | 3008 | SvLEN(sv) = len; /* No longer know the real size. */ |
560a288e | 3009 | } |
4411f3b6 NIS |
3010 | /* Mark as UTF-8 even if no hibit - saves scanning loop */ |
3011 | SvUTF8_on(sv); | |
3012 | return SvCUR(sv); | |
560a288e GS |
3013 | } |
3014 | ||
c461cf8f JH |
3015 | /* |
3016 | =for apidoc sv_utf8_downgrade | |
3017 | ||
3018 | Attempt to convert the PV of an SV from UTF8-encoded to byte encoding. | |
3019 | This may not be possible if the PV contains non-byte encoding characters; | |
3020 | if this is the case, either returns false or, if C<fail_ok> is not | |
3021 | true, croaks. | |
3022 | ||
3023 | =cut | |
3024 | */ | |
3025 | ||
560a288e GS |
3026 | bool |
3027 | Perl_sv_utf8_downgrade(pTHX_ register SV* sv, bool fail_ok) | |
3028 | { | |
3029 | if (SvPOK(sv) && SvUTF8(sv)) { | |
fa301091 | 3030 | if (SvCUR(sv)) { |
03cfe0ae | 3031 | U8 *s; |
652088fc | 3032 | STRLEN len; |
fa301091 | 3033 | |
652088fc JH |
3034 | if (SvREADONLY(sv) && SvFAKE(sv)) |
3035 | sv_force_normal(sv); | |
03cfe0ae NIS |
3036 | s = (U8 *) SvPV(sv, len); |
3037 | if (!utf8_to_bytes(s, &len)) { | |
fa301091 JH |
3038 | if (fail_ok) |
3039 | return FALSE; | |
03cfe0ae NIS |
3040 | #ifdef USE_BYTES_DOWNGRADES |
3041 | else if (IN_BYTE) { | |
3042 | U8 *d = s; | |
3043 | U8 *e = (U8 *) SvEND(sv); | |
3044 | int first = 1; | |
3045 | while (s < e) { | |
3046 | UV ch = utf8n_to_uvchr(s,(e-s),&len,0); | |
3047 | if (first && ch > 255) { | |
3048 | if (PL_op) | |
3049 | Perl_warner(aTHX_ WARN_UTF8, "Wide character in byte %s", | |
3050 | PL_op_desc[PL_op->op_type]); | |
3051 | else | |
3052 | Perl_warner(aTHX_ WARN_UTF8, "Wide character in byte"); | |
3053 | first = 0; | |
3054 | } | |
3055 | *d++ = ch; | |
3056 | s += len; | |
3057 | } | |
3058 | *d = '\0'; | |
3059 | len = (d - (U8 *) SvPVX(sv)); | |
3060 | } | |
3061 | #endif | |
fa301091 JH |
3062 | else { |
3063 | if (PL_op) | |
3064 | Perl_croak(aTHX_ "Wide character in %s", | |
3065 | PL_op_desc[PL_op->op_type]); | |
3066 | else | |
3067 | Perl_croak(aTHX_ "Wide character"); | |
3068 | } | |
4b3603a4 | 3069 | } |
fa301091 | 3070 | SvCUR(sv) = len; |
67e989fb | 3071 | } |
560a288e | 3072 | } |
ffebcc3e | 3073 | SvUTF8_off(sv); |
560a288e GS |
3074 | return TRUE; |
3075 | } | |
3076 | ||
c461cf8f JH |
3077 | /* |
3078 | =for apidoc sv_utf8_encode | |
3079 | ||
3080 | Convert the PV of an SV to UTF8-encoded, but then turn off the C<SvUTF8> | |
4411f3b6 NIS |
3081 | flag so that it looks like octets again. Used as a building block |
3082 | for encode_utf8 in Encode.xs | |
c461cf8f JH |
3083 | |
3084 | =cut | |
3085 | */ | |
3086 | ||
560a288e GS |
3087 | void |
3088 | Perl_sv_utf8_encode(pTHX_ register SV *sv) | |
3089 | { | |
4411f3b6 | 3090 | (void) sv_utf8_upgrade(sv); |
560a288e GS |
3091 | SvUTF8_off(sv); |
3092 | } | |
3093 | ||
4411f3b6 NIS |
3094 | /* |
3095 | =for apidoc sv_utf8_decode | |
3096 | ||
3097 | Convert the octets in the PV from UTF-8 to chars. Scan for validity and then | |
3098 | turn of SvUTF8 if needed so that we see characters. Used as a building block | |
3099 | for decode_utf8 in Encode.xs | |
3100 | ||
3101 | =cut | |
3102 | */ | |
3103 | ||
3104 | ||
3105 | ||
560a288e GS |
3106 | bool |
3107 | Perl_sv_utf8_decode(pTHX_ register SV *sv) | |
3108 | { | |
3109 | if (SvPOK(sv)) { | |
63cd0674 NIS |
3110 | U8 *c; |
3111 | U8 *e; | |
9cbac4c7 | 3112 | |
4411f3b6 | 3113 | /* The octets may have got themselves encoded - get them back as bytes */ |
560a288e GS |
3114 | if (!sv_utf8_downgrade(sv, TRUE)) |
3115 | return FALSE; | |
3116 | ||
3117 | /* it is actually just a matter of turning the utf8 flag on, but | |
3118 | * we want to make sure everything inside is valid utf8 first. | |
3119 | */ | |
63cd0674 NIS |
3120 | c = (U8 *) SvPVX(sv); |
3121 | if (!is_utf8_string(c, SvCUR(sv)+1)) | |
67e989fb | 3122 | return FALSE; |
63cd0674 | 3123 | e = (U8 *) SvEND(sv); |
511c2ff0 | 3124 | while (c < e) { |
c4d5f83a NIS |
3125 | U8 ch = *c++; |
3126 | if (!UTF8_IS_INVARIANT(ch)) { | |
67e989fb JH |
3127 | SvUTF8_on(sv); |
3128 | break; | |
3129 | } | |
560a288e | 3130 | } |
560a288e GS |
3131 | } |
3132 | return TRUE; | |
3133 | } | |
3134 | ||
3135 | ||
79072805 | 3136 | /* Note: sv_setsv() should not be called with a source string that needs |
463ee0b2 | 3137 | * to be reused, since it may destroy the source string if it is marked |
79072805 LW |
3138 | * as temporary. |
3139 | */ | |
3140 | ||
954c1994 GS |
3141 | /* |
3142 | =for apidoc sv_setsv | |
3143 | ||
3144 | Copies the contents of the source SV C<ssv> into the destination SV C<dsv>. | |
3145 | The source SV may be destroyed if it is mortal. Does not handle 'set' | |
3146 | magic. See the macro forms C<SvSetSV>, C<SvSetSV_nosteal> and | |
3147 | C<sv_setsv_mg>. | |
3148 | ||
3149 | =cut | |
3150 | */ | |
3151 | ||
79072805 | 3152 | void |
864dbfa3 | 3153 | Perl_sv_setsv(pTHX_ SV *dstr, register SV *sstr) |
79072805 | 3154 | { |
8990e307 LW |
3155 | register U32 sflags; |
3156 | register int dtype; | |
3157 | register int stype; | |
463ee0b2 | 3158 | |
79072805 LW |
3159 | if (sstr == dstr) |
3160 | return; | |
2213622d | 3161 | SV_CHECK_THINKFIRST(dstr); |
79072805 | 3162 | if (!sstr) |
3280af22 | 3163 | sstr = &PL_sv_undef; |
8990e307 LW |
3164 | stype = SvTYPE(sstr); |
3165 | dtype = SvTYPE(dstr); | |
79072805 | 3166 | |
a0d0e21e | 3167 | SvAMAGIC_off(dstr); |
9e7bc3e8 | 3168 | |
463ee0b2 | 3169 | /* There's a lot of redundancy below but we're going for speed here */ |
79072805 | 3170 | |
8990e307 | 3171 | switch (stype) { |
79072805 | 3172 | case SVt_NULL: |
aece5585 | 3173 | undef_sstr: |
20408e3c GS |
3174 | if (dtype != SVt_PVGV) { |
3175 | (void)SvOK_off(dstr); | |
3176 | return; | |
3177 | } | |
3178 | break; | |
463ee0b2 | 3179 | case SVt_IV: |
aece5585 GA |
3180 | if (SvIOK(sstr)) { |
3181 | switch (dtype) { | |
3182 | case SVt_NULL: | |
8990e307 | 3183 | sv_upgrade(dstr, SVt_IV); |
aece5585 GA |
3184 | break; |
3185 | case SVt_NV: | |
8990e307 | 3186 | sv_upgrade(dstr, SVt_PVNV); |
aece5585 GA |
3187 | break; |
3188 | case SVt_RV: | |
3189 | case SVt_PV: | |
a0d0e21e | 3190 | sv_upgrade(dstr, SVt_PVIV); |
aece5585 GA |
3191 | break; |
3192 | } | |
3193 | (void)SvIOK_only(dstr); | |
3194 | SvIVX(dstr) = SvIVX(sstr); | |
25da4f38 IZ |
3195 | if (SvIsUV(sstr)) |
3196 | SvIsUV_on(dstr); | |
27c9684d AP |
3197 | if (SvTAINTED(sstr)) |
3198 | SvTAINT(dstr); | |
aece5585 | 3199 | return; |
8990e307 | 3200 | } |
aece5585 GA |
3201 | goto undef_sstr; |
3202 | ||
463ee0b2 | 3203 | case SVt_NV: |
aece5585 GA |
3204 | if (SvNOK(sstr)) { |
3205 | switch (dtype) { | |
3206 | case SVt_NULL: | |
3207 | case SVt_IV: | |
8990e307 | 3208 | sv_upgrade(dstr, SVt_NV); |
aece5585 GA |
3209 | break; |
3210 | case SVt_RV: | |
3211 | case SVt_PV: | |
3212 | case SVt_PVIV: | |
a0d0e21e | 3213 | sv_upgrade(dstr, SVt_PVNV); |
aece5585 GA |
3214 | break; |
3215 | } | |
3216 | SvNVX(dstr) = SvNVX(sstr); | |
3217 | (void)SvNOK_only(dstr); | |
27c9684d AP |
3218 | if (SvTAINTED(sstr)) |
3219 | SvTAINT(dstr); | |
aece5585 | 3220 | return; |
8990e307 | 3221 | } |
aece5585 GA |
3222 | goto undef_sstr; |
3223 | ||
ed6116ce | 3224 | case SVt_RV: |
8990e307 | 3225 | if (dtype < SVt_RV) |
ed6116ce | 3226 | sv_upgrade(dstr, SVt_RV); |
c07a80fd | 3227 | else if (dtype == SVt_PVGV && |
3228 | SvTYPE(SvRV(sstr)) == SVt_PVGV) { | |
3229 | sstr = SvRV(sstr); | |
a5f75d66 | 3230 | if (sstr == dstr) { |
1d7c1841 GS |
3231 | if (GvIMPORTED(dstr) != GVf_IMPORTED |
3232 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) | |
3233 | { | |
a5f75d66 | 3234 | GvIMPORTED_on(dstr); |
1d7c1841 | 3235 | } |
a5f75d66 AD |
3236 | GvMULTI_on(dstr); |
3237 | return; | |
3238 | } | |
c07a80fd | 3239 | goto glob_assign; |
3240 | } | |
ed6116ce | 3241 | break; |
463ee0b2 | 3242 | case SVt_PV: |
fc36a67e | 3243 | case SVt_PVFM: |
8990e307 | 3244 | if (dtype < SVt_PV) |
463ee0b2 | 3245 | sv_upgrade(dstr, SVt_PV); |
463ee0b2 LW |
3246 | break; |
3247 | case SVt_PVIV: | |
8990e307 | 3248 | if (dtype < SVt_PVIV) |
463ee0b2 | 3249 | sv_upgrade(dstr, SVt_PVIV); |
463ee0b2 LW |
3250 | break; |
3251 | case SVt_PVNV: | |
8990e307 | 3252 | if (dtype < SVt_PVNV) |
463ee0b2 | 3253 | sv_upgrade(dstr, SVt_PVNV); |
463ee0b2 | 3254 | break; |
4633a7c4 LW |
3255 | case SVt_PVAV: |
3256 | case SVt_PVHV: | |
3257 | case SVt_PVCV: | |
4633a7c4 | 3258 | case SVt_PVIO: |
533c011a | 3259 | if (PL_op) |
cea2e8a9 | 3260 | Perl_croak(aTHX_ "Bizarre copy of %s in %s", sv_reftype(sstr, 0), |
22c35a8c | 3261 | PL_op_name[PL_op->op_type]); |
4633a7c4 | 3262 | else |
cea2e8a9 | 3263 | Perl_croak(aTHX_ "Bizarre copy of %s", sv_reftype(sstr, 0)); |
4633a7c4 LW |
3264 | break; |
3265 | ||
79072805 | 3266 | case SVt_PVGV: |
8990e307 | 3267 | if (dtype <= SVt_PVGV) { |
c07a80fd | 3268 | glob_assign: |
a5f75d66 | 3269 | if (dtype != SVt_PVGV) { |
a0d0e21e LW |
3270 | char *name = GvNAME(sstr); |
3271 | STRLEN len = GvNAMELEN(sstr); | |
463ee0b2 | 3272 | sv_upgrade(dstr, SVt_PVGV); |
14befaf4 | 3273 | sv_magic(dstr, dstr, PERL_MAGIC_glob, Nullch, 0); |
85aff577 | 3274 | GvSTASH(dstr) = (HV*)SvREFCNT_inc(GvSTASH(sstr)); |
a0d0e21e LW |
3275 | GvNAME(dstr) = savepvn(name, len); |
3276 | GvNAMELEN(dstr) = len; | |
3277 | SvFAKE_on(dstr); /* can coerce to non-glob */ | |
3278 | } | |
7bac28a0 | 3279 | /* ahem, death to those who redefine active sort subs */ |
3280af22 NIS |
3280 | else if (PL_curstackinfo->si_type == PERLSI_SORT |
3281 | && GvCV(dstr) && PL_sortcop == CvSTART(GvCV(dstr))) | |
cea2e8a9 | 3282 | Perl_croak(aTHX_ "Can't redefine active sort subroutine %s", |
7bac28a0 | 3283 | GvNAME(dstr)); |
5bd07a3d DM |
3284 | |
3285 | #ifdef GV_SHARED_CHECK | |
3286 | if (GvSHARED((GV*)dstr)) { | |
3287 | Perl_croak(aTHX_ PL_no_modify); | |
3288 | } | |
3289 | #endif | |
3290 | ||
a0d0e21e | 3291 | (void)SvOK_off(dstr); |
a5f75d66 | 3292 | GvINTRO_off(dstr); /* one-shot flag */ |
1edc1566 | 3293 | gp_free((GV*)dstr); |
79072805 | 3294 | GvGP(dstr) = gp_ref(GvGP(sstr)); |
27c9684d AP |
3295 | if (SvTAINTED(sstr)) |
3296 | SvTAINT(dstr); | |
1d7c1841 GS |
3297 | if (GvIMPORTED(dstr) != GVf_IMPORTED |
3298 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) | |
3299 | { | |
a5f75d66 | 3300 | GvIMPORTED_on(dstr); |
1d7c1841 | 3301 | } |
a5f75d66 | 3302 | GvMULTI_on(dstr); |
79072805 LW |
3303 | return; |
3304 | } | |
3305 | /* FALL THROUGH */ | |
3306 | ||
3307 | default: | |
973f89ab CS |
3308 | if (SvGMAGICAL(sstr)) { |
3309 | mg_get(sstr); | |
3310 | if (SvTYPE(sstr) != stype) { | |
3311 | stype = SvTYPE(sstr); | |
3312 | if (stype == SVt_PVGV && dtype <= SVt_PVGV) | |
3313 | goto glob_assign; | |
3314 | } | |
3315 | } | |
ded42b9f | 3316 | if (stype == SVt_PVLV) |
6fc92669 | 3317 | (void)SvUPGRADE(dstr, SVt_PVNV); |
ded42b9f | 3318 | else |
6fc92669 | 3319 | (void)SvUPGRADE(dstr, stype); |
79072805 LW |
3320 | } |
3321 | ||
8990e307 LW |
3322 | sflags = SvFLAGS(sstr); |
3323 | ||
3324 | if (sflags & SVf_ROK) { | |
3325 | if (dtype >= SVt_PV) { | |
3326 | if (dtype == SVt_PVGV) { | |
3327 | SV *sref = SvREFCNT_inc(SvRV(sstr)); | |
3328 | SV *dref = 0; | |
a5f75d66 | 3329 | int intro = GvINTRO(dstr); |
a0d0e21e | 3330 | |
5bd07a3d DM |
3331 | #ifdef GV_SHARED_CHECK |
3332 | if (GvSHARED((GV*)dstr)) { | |
3333 | Perl_croak(aTHX_ PL_no_modify); | |
3334 | } | |
3335 | #endif | |
3336 | ||
a0d0e21e LW |
3337 | if (intro) { |
3338 | GP *gp; | |
1d7c1841 | 3339 | gp_free((GV*)dstr); |
a5f75d66 | 3340 | GvINTRO_off(dstr); /* one-shot flag */ |
a0d0e21e | 3341 | Newz(602,gp, 1, GP); |
44a8e56a | 3342 | GvGP(dstr) = gp_ref(gp); |
a0d0e21e | 3343 | GvSV(dstr) = NEWSV(72,0); |
1d7c1841 | 3344 | GvLINE(dstr) = CopLINE(PL_curcop); |
1edc1566 | 3345 | GvEGV(dstr) = (GV*)dstr; |
a0d0e21e | 3346 | } |
a5f75d66 | 3347 | GvMULTI_on(dstr); |
8990e307 LW |
3348 | switch (SvTYPE(sref)) { |
3349 | case SVt_PVAV: | |
a0d0e21e LW |
3350 | if (intro) |
3351 | SAVESPTR(GvAV(dstr)); | |
3352 | else | |
3353 | dref = (SV*)GvAV(dstr); | |
8990e307 | 3354 | GvAV(dstr) = (AV*)sref; |
39bac7f7 | 3355 | if (!GvIMPORTED_AV(dstr) |
1d7c1841 GS |
3356 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
3357 | { | |
a5f75d66 | 3358 | GvIMPORTED_AV_on(dstr); |
1d7c1841 | 3359 | } |
8990e307 LW |
3360 | break; |
3361 | case SVt_PVHV: | |
a0d0e21e LW |
3362 | if (intro) |
3363 | SAVESPTR(GvHV(dstr)); | |
3364 | else | |
3365 | dref = (SV*)GvHV(dstr); | |
8990e307 | 3366 | GvHV(dstr) = (HV*)sref; |
39bac7f7 | 3367 | if (!GvIMPORTED_HV(dstr) |
1d7c1841 GS |
3368 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
3369 | { | |
a5f75d66 | 3370 | GvIMPORTED_HV_on(dstr); |
1d7c1841 | 3371 | } |
8990e307 LW |
3372 | break; |
3373 | case SVt_PVCV: | |
8ebc5c01 | 3374 | if (intro) { |
3375 | if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) { | |
3376 | SvREFCNT_dec(GvCV(dstr)); | |
3377 | GvCV(dstr) = Nullcv; | |
68dc0745 | 3378 | GvCVGEN(dstr) = 0; /* Switch off cacheness. */ |
3280af22 | 3379 | PL_sub_generation++; |
8ebc5c01 | 3380 | } |
a0d0e21e | 3381 | SAVESPTR(GvCV(dstr)); |
8ebc5c01 | 3382 | } |
68dc0745 | 3383 | else |
3384 | dref = (SV*)GvCV(dstr); | |
3385 | if (GvCV(dstr) != (CV*)sref) { | |
748a9306 | 3386 | CV* cv = GvCV(dstr); |
4633a7c4 | 3387 | if (cv) { |
68dc0745 | 3388 | if (!GvCVGEN((GV*)dstr) && |
3389 | (CvROOT(cv) || CvXSUB(cv))) | |
3390 | { | |
7bac28a0 | 3391 | /* ahem, death to those who redefine |
3392 | * active sort subs */ | |
3280af22 NIS |
3393 | if (PL_curstackinfo->si_type == PERLSI_SORT && |
3394 | PL_sortcop == CvSTART(cv)) | |
1c846c1f | 3395 | Perl_croak(aTHX_ |
7bac28a0 | 3396 | "Can't redefine active sort subroutine %s", |
3397 | GvENAME((GV*)dstr)); | |
beab0874 JT |
3398 | /* Redefining a sub - warning is mandatory if |
3399 | it was a const and its value changed. */ | |
3400 | if (ckWARN(WARN_REDEFINE) | |
3401 | || (CvCONST(cv) | |
3402 | && (!CvCONST((CV*)sref) | |
3403 | || sv_cmp(cv_const_sv(cv), | |
3404 | cv_const_sv((CV*)sref))))) | |
3405 | { | |
3406 | Perl_warner(aTHX_ WARN_REDEFINE, | |
3407 | CvCONST(cv) | |
3408 | ? "Constant subroutine %s redefined" | |
47deb5e7 | 3409 | : "Subroutine %s redefined", |
beab0874 JT |
3410 | GvENAME((GV*)dstr)); |
3411 | } | |
9607fc9c | 3412 | } |
3fe9a6f1 | 3413 | cv_ckproto(cv, (GV*)dstr, |
3414 | SvPOK(sref) ? SvPVX(sref) : Nullch); | |
4633a7c4 | 3415 | } |
a5f75d66 | 3416 | GvCV(dstr) = (CV*)sref; |
7a4c00b4 | 3417 | GvCVGEN(dstr) = 0; /* Switch off cacheness. */ |
a5f75d66 | 3418 | GvASSUMECV_on(dstr); |
3280af22 | 3419 | PL_sub_generation++; |
a5f75d66 | 3420 | } |
39bac7f7 | 3421 | if (!GvIMPORTED_CV(dstr) |
1d7c1841 GS |
3422 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
3423 | { | |
a5f75d66 | 3424 | GvIMPORTED_CV_on(dstr); |
1d7c1841 | 3425 | } |
8990e307 | 3426 | break; |
91bba347 LW |
3427 | case SVt_PVIO: |
3428 | if (intro) | |
3429 | SAVESPTR(GvIOp(dstr)); | |
3430 | else | |
3431 | dref = (SV*)GvIOp(dstr); | |
3432 | GvIOp(dstr) = (IO*)sref; | |
3433 | break; | |
f4d13ee9 JH |
3434 | case SVt_PVFM: |
3435 | if (intro) | |
3436 | SAVESPTR(GvFORM(dstr)); | |
3437 | else | |
3438 | dref = (SV*)GvFORM(dstr); | |
3439 | GvFORM(dstr) = (CV*)sref; | |
3440 | break; | |
8990e307 | 3441 | default: |
a0d0e21e LW |
3442 | if (intro) |
3443 | SAVESPTR(GvSV(dstr)); | |
3444 | else | |
3445 | dref = (SV*)GvSV(dstr); | |
8990e307 | 3446 | GvSV(dstr) = sref; |
39bac7f7 | 3447 | if (!GvIMPORTED_SV(dstr) |
1d7c1841 GS |
3448 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
3449 | { | |
a5f75d66 | 3450 | GvIMPORTED_SV_on(dstr); |
1d7c1841 | 3451 | } |
8990e307 LW |
3452 | break; |
3453 | } | |
3454 | if (dref) | |
3455 | SvREFCNT_dec(dref); | |
a0d0e21e LW |
3456 | if (intro) |
3457 | SAVEFREESV(sref); | |
27c9684d AP |
3458 | if (SvTAINTED(sstr)) |
3459 | SvTAINT(dstr); | |
8990e307 LW |
3460 | return; |
3461 | } | |
a0d0e21e | 3462 | if (SvPVX(dstr)) { |
760ac839 | 3463 | (void)SvOOK_off(dstr); /* backoff */ |
50483b2c JD |
3464 | if (SvLEN(dstr)) |
3465 | Safefree(SvPVX(dstr)); | |
a0d0e21e LW |
3466 | SvLEN(dstr)=SvCUR(dstr)=0; |
3467 | } | |
8990e307 | 3468 | } |
a0d0e21e | 3469 | (void)SvOK_off(dstr); |
8990e307 | 3470 | SvRV(dstr) = SvREFCNT_inc(SvRV(sstr)); |
ed6116ce | 3471 | SvROK_on(dstr); |
8990e307 | 3472 | if (sflags & SVp_NOK) { |
3332b3c1 JH |
3473 | SvNOKp_on(dstr); |
3474 | /* Only set the public OK flag if the source has public OK. */ | |
3475 | if (sflags & SVf_NOK) | |
3476 | SvFLAGS(dstr) |= SVf_NOK; | |
ed6116ce LW |
3477 | SvNVX(dstr) = SvNVX(sstr); |
3478 | } | |
8990e307 | 3479 | if (sflags & SVp_IOK) { |
3332b3c1 JH |
3480 | (void)SvIOKp_on(dstr); |
3481 | if (sflags & SVf_IOK) | |
3482 | SvFLAGS(dstr) |= SVf_IOK; | |
2b1c7e3e | 3483 | if (sflags & SVf_IVisUV) |
25da4f38 | 3484 | SvIsUV_on(dstr); |
3332b3c1 | 3485 | SvIVX(dstr) = SvIVX(sstr); |
ed6116ce | 3486 | } |
a0d0e21e LW |
3487 | if (SvAMAGIC(sstr)) { |
3488 | SvAMAGIC_on(dstr); | |
3489 | } | |
ed6116ce | 3490 | } |
8990e307 | 3491 | else if (sflags & SVp_POK) { |
79072805 LW |
3492 | |
3493 | /* | |
3494 | * Check to see if we can just swipe the string. If so, it's a | |
3495 | * possible small lose on short strings, but a big win on long ones. | |
463ee0b2 LW |
3496 | * It might even be a win on short strings if SvPVX(dstr) |
3497 | * has to be allocated and SvPVX(sstr) has to be freed. | |
79072805 LW |
3498 | */ |
3499 | ||
ff68c719 | 3500 | if (SvTEMP(sstr) && /* slated for free anyway? */ |
01b73108 | 3501 | SvREFCNT(sstr) == 1 && /* and no other references to it? */ |
1c846c1f | 3502 | !(sflags & SVf_OOK) && /* and not involved in OOK hack? */ |
4c8f17b9 BH |
3503 | SvLEN(sstr) && /* and really is a string */ |
3504 | !(PL_op && PL_op->op_type == OP_AASSIGN)) /* and won't be needed again, potentially */ | |
a5f75d66 | 3505 | { |
adbc6bb1 | 3506 | if (SvPVX(dstr)) { /* we know that dtype >= SVt_PV */ |
a5f75d66 AD |
3507 | if (SvOOK(dstr)) { |
3508 | SvFLAGS(dstr) &= ~SVf_OOK; | |
3509 | Safefree(SvPVX(dstr) - SvIVX(dstr)); | |
3510 | } | |
50483b2c | 3511 | else if (SvLEN(dstr)) |
a5f75d66 | 3512 | Safefree(SvPVX(dstr)); |
79072805 | 3513 | } |
a5f75d66 | 3514 | (void)SvPOK_only(dstr); |
463ee0b2 | 3515 | SvPV_set(dstr, SvPVX(sstr)); |
79072805 LW |
3516 | SvLEN_set(dstr, SvLEN(sstr)); |
3517 | SvCUR_set(dstr, SvCUR(sstr)); | |
f4e86e0f | 3518 | |
79072805 | 3519 | SvTEMP_off(dstr); |
2b1c7e3e | 3520 | (void)SvOK_off(sstr); /* NOTE: nukes most SvFLAGS on sstr */ |
79072805 LW |
3521 | SvPV_set(sstr, Nullch); |
3522 | SvLEN_set(sstr, 0); | |
a5f75d66 AD |
3523 | SvCUR_set(sstr, 0); |
3524 | SvTEMP_off(sstr); | |
79072805 LW |
3525 | } |
3526 | else { /* have to copy actual string */ | |
8990e307 LW |
3527 | STRLEN len = SvCUR(sstr); |
3528 | ||
3529 | SvGROW(dstr, len + 1); /* inlined from sv_setpvn */ | |
3530 | Move(SvPVX(sstr),SvPVX(dstr),len,char); | |
3531 | SvCUR_set(dstr, len); | |
3532 | *SvEND(dstr) = '\0'; | |
a0d0e21e | 3533 | (void)SvPOK_only(dstr); |
79072805 | 3534 | } |
9aa983d2 | 3535 | if (sflags & SVf_UTF8) |
a7cb1f99 | 3536 | SvUTF8_on(dstr); |
79072805 | 3537 | /*SUPPRESS 560*/ |
8990e307 | 3538 | if (sflags & SVp_NOK) { |
3332b3c1 JH |
3539 | SvNOKp_on(dstr); |
3540 | if (sflags & SVf_NOK) | |
3541 | SvFLAGS(dstr) |= SVf_NOK; | |
463ee0b2 | 3542 | SvNVX(dstr) = SvNVX(sstr); |
79072805 | 3543 | } |
8990e307 | 3544 | if (sflags & SVp_IOK) { |
3332b3c1 JH |
3545 | (void)SvIOKp_on(dstr); |
3546 | if (sflags & SVf_IOK) | |
3547 | SvFLAGS(dstr) |= SVf_IOK; | |
2b1c7e3e | 3548 | if (sflags & SVf_IVisUV) |
25da4f38 | 3549 | SvIsUV_on(dstr); |
463ee0b2 | 3550 | SvIVX(dstr) = SvIVX(sstr); |
79072805 LW |
3551 | } |
3552 | } | |
8990e307 | 3553 | else if (sflags & SVp_IOK) { |
3332b3c1 JH |
3554 | if (sflags & SVf_IOK) |
3555 | (void)SvIOK_only(dstr); | |
3556 | else { | |
9cbac4c7 DM |
3557 | (void)SvOK_off(dstr); |
3558 | (void)SvIOKp_on(dstr); | |
3332b3c1 JH |
3559 | } |
3560 | /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */ | |
2b1c7e3e | 3561 | if (sflags & SVf_IVisUV) |
25da4f38 | 3562 | SvIsUV_on(dstr); |
3332b3c1 JH |
3563 | SvIVX(dstr) = SvIVX(sstr); |
3564 | if (sflags & SVp_NOK) { | |
3565 | if (sflags & SVf_NOK) | |
3566 | (void)SvNOK_on(dstr); | |
3567 | else | |
3568 | (void)SvNOKp_on(dstr); | |
3569 | SvNVX(dstr) = SvNVX(sstr); | |
3570 | } | |
3571 | } | |
3572 | else if (sflags & SVp_NOK) { | |
3573 | if (sflags & SVf_NOK) | |
3574 | (void)SvNOK_only(dstr); | |
3575 | else { | |
9cbac4c7 | 3576 | (void)SvOK_off(dstr); |
3332b3c1 JH |
3577 | SvNOKp_on(dstr); |
3578 | } | |
3579 | SvNVX(dstr) = SvNVX(sstr); | |
79072805 LW |
3580 | } |
3581 | else { | |
20408e3c | 3582 | if (dtype == SVt_PVGV) { |
e476b1b5 GS |
3583 | if (ckWARN(WARN_MISC)) |
3584 | Perl_warner(aTHX_ WARN_MISC, "Undefined value assigned to typeglob"); | |
20408e3c GS |
3585 | } |
3586 | else | |
3587 | (void)SvOK_off(dstr); | |
a0d0e21e | 3588 | } |
27c9684d AP |
3589 | if (SvTAINTED(sstr)) |
3590 | SvTAINT(dstr); | |
79072805 LW |
3591 | } |
3592 | ||
954c1994 GS |
3593 | /* |
3594 | =for apidoc sv_setsv_mg | |
3595 | ||
3596 | Like C<sv_setsv>, but also handles 'set' magic. | |
3597 | ||
3598 | =cut | |
3599 | */ | |
3600 | ||
79072805 | 3601 | void |
864dbfa3 | 3602 | Perl_sv_setsv_mg(pTHX_ SV *dstr, register SV *sstr) |
ef50df4b GS |
3603 | { |
3604 | sv_setsv(dstr,sstr); | |
3605 | SvSETMAGIC(dstr); | |
3606 | } | |
3607 | ||
954c1994 GS |
3608 | /* |
3609 | =for apidoc sv_setpvn | |
3610 | ||
3611 | Copies a string into an SV. The C<len> parameter indicates the number of | |
3612 | bytes to be copied. Does not handle 'set' magic. See C<sv_setpvn_mg>. | |
3613 | ||
3614 | =cut | |
3615 | */ | |
3616 | ||
ef50df4b | 3617 | void |
864dbfa3 | 3618 | Perl_sv_setpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len) |
79072805 | 3619 | { |
c6f8c383 | 3620 | register char *dptr; |
22c522df | 3621 | |
2213622d | 3622 | SV_CHECK_THINKFIRST(sv); |
463ee0b2 | 3623 | if (!ptr) { |
a0d0e21e | 3624 | (void)SvOK_off(sv); |
463ee0b2 LW |
3625 | return; |
3626 | } | |
22c522df JH |
3627 | else { |
3628 | /* len is STRLEN which is unsigned, need to copy to signed */ | |
3629 | IV iv = len; | |
3630 | assert(iv >= 0); | |
3631 | } | |
6fc92669 | 3632 | (void)SvUPGRADE(sv, SVt_PV); |
c6f8c383 | 3633 | |
79072805 | 3634 | SvGROW(sv, len + 1); |
c6f8c383 GA |
3635 | dptr = SvPVX(sv); |
3636 | Move(ptr,dptr,len,char); | |
3637 | dptr[len] = '\0'; | |
79072805 | 3638 | SvCUR_set(sv, len); |
1aa99e6b | 3639 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 | 3640 | SvTAINT(sv); |
79072805 LW |
3641 | } |
3642 | ||
954c1994 GS |
3643 | /* |
3644 | =for apidoc sv_setpvn_mg | |
3645 | ||
3646 | Like C<sv_setpvn>, but also handles 'set' magic. | |
3647 | ||
3648 | =cut | |
3649 | */ | |
3650 | ||
79072805 | 3651 | void |
864dbfa3 | 3652 | Perl_sv_setpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len) |
ef50df4b GS |
3653 | { |
3654 | sv_setpvn(sv,ptr,len); | |
3655 | SvSETMAGIC(sv); | |
3656 | } | |
3657 | ||
954c1994 GS |
3658 | /* |
3659 | =for apidoc sv_setpv | |
3660 | ||
3661 | Copies a string into an SV. The string must be null-terminated. Does not | |
3662 | handle 'set' magic. See C<sv_setpv_mg>. | |
3663 | ||
3664 | =cut | |
3665 | */ | |
3666 | ||
ef50df4b | 3667 | void |
864dbfa3 | 3668 | Perl_sv_setpv(pTHX_ register SV *sv, register const char *ptr) |
79072805 LW |
3669 | { |
3670 | register STRLEN len; | |
3671 | ||
2213622d | 3672 | SV_CHECK_THINKFIRST(sv); |
463ee0b2 | 3673 | if (!ptr) { |
a0d0e21e | 3674 | (void)SvOK_off(sv); |
463ee0b2 LW |
3675 | return; |
3676 | } | |
79072805 | 3677 | len = strlen(ptr); |
6fc92669 | 3678 | (void)SvUPGRADE(sv, SVt_PV); |
c6f8c383 | 3679 | |
79072805 | 3680 | SvGROW(sv, len + 1); |
463ee0b2 | 3681 | Move(ptr,SvPVX(sv),len+1,char); |
79072805 | 3682 | SvCUR_set(sv, len); |
1aa99e6b | 3683 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 LW |
3684 | SvTAINT(sv); |
3685 | } | |
3686 | ||
954c1994 GS |
3687 | /* |
3688 | =for apidoc sv_setpv_mg | |
3689 | ||
3690 | Like C<sv_setpv>, but also handles 'set' magic. | |
3691 | ||
3692 | =cut | |
3693 | */ | |
3694 | ||
463ee0b2 | 3695 | void |
864dbfa3 | 3696 | Perl_sv_setpv_mg(pTHX_ register SV *sv, register const char *ptr) |
ef50df4b GS |
3697 | { |
3698 | sv_setpv(sv,ptr); | |
3699 | SvSETMAGIC(sv); | |
3700 | } | |
3701 | ||
954c1994 GS |
3702 | /* |
3703 | =for apidoc sv_usepvn | |
3704 | ||
3705 | Tells an SV to use C<ptr> to find its string value. Normally the string is | |
1c846c1f | 3706 | stored inside the SV but sv_usepvn allows the SV to use an outside string. |
954c1994 GS |
3707 | The C<ptr> should point to memory that was allocated by C<malloc>. The |
3708 | string length, C<len>, must be supplied. This function will realloc the | |
3709 | memory pointed to by C<ptr>, so that pointer should not be freed or used by | |
3710 | the programmer after giving it to sv_usepvn. Does not handle 'set' magic. | |
3711 | See C<sv_usepvn_mg>. | |
3712 | ||
3713 | =cut | |
3714 | */ | |
3715 | ||
ef50df4b | 3716 | void |
864dbfa3 | 3717 | Perl_sv_usepvn(pTHX_ register SV *sv, register char *ptr, register STRLEN len) |
463ee0b2 | 3718 | { |
2213622d | 3719 | SV_CHECK_THINKFIRST(sv); |
c6f8c383 | 3720 | (void)SvUPGRADE(sv, SVt_PV); |
463ee0b2 | 3721 | if (!ptr) { |
a0d0e21e | 3722 | (void)SvOK_off(sv); |
463ee0b2 LW |
3723 | return; |
3724 | } | |
a0ed51b3 | 3725 | (void)SvOOK_off(sv); |
50483b2c | 3726 | if (SvPVX(sv) && SvLEN(sv)) |
463ee0b2 LW |
3727 | Safefree(SvPVX(sv)); |
3728 | Renew(ptr, len+1, char); | |
3729 | SvPVX(sv) = ptr; | |
3730 | SvCUR_set(sv, len); | |
3731 | SvLEN_set(sv, len+1); | |
3732 | *SvEND(sv) = '\0'; | |
1aa99e6b | 3733 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 | 3734 | SvTAINT(sv); |
79072805 LW |
3735 | } |
3736 | ||
954c1994 GS |
3737 | /* |
3738 | =for apidoc sv_usepvn_mg | |
3739 | ||
3740 | Like C<sv_usepvn>, but also handles 'set' magic. | |
3741 | ||
3742 | =cut | |
3743 | */ | |
3744 | ||
ef50df4b | 3745 | void |
864dbfa3 | 3746 | Perl_sv_usepvn_mg(pTHX_ register SV *sv, register char *ptr, register STRLEN len) |
ef50df4b | 3747 | { |
51c1089b | 3748 | sv_usepvn(sv,ptr,len); |
ef50df4b GS |
3749 | SvSETMAGIC(sv); |
3750 | } | |
3751 | ||
6fc92669 | 3752 | void |
840a7b70 | 3753 | Perl_sv_force_normal_flags(pTHX_ register SV *sv, U32 flags) |
0f15f207 | 3754 | { |
2213622d | 3755 | if (SvREADONLY(sv)) { |
1c846c1f NIS |
3756 | if (SvFAKE(sv)) { |
3757 | char *pvx = SvPVX(sv); | |
3758 | STRLEN len = SvCUR(sv); | |
3759 | U32 hash = SvUVX(sv); | |
3760 | SvGROW(sv, len + 1); | |
3761 | Move(pvx,SvPVX(sv),len,char); | |
3762 | *SvEND(sv) = '\0'; | |
3763 | SvFAKE_off(sv); | |
3764 | SvREADONLY_off(sv); | |
c3654f1a | 3765 | unsharepvn(pvx,SvUTF8(sv)?-len:len,hash); |
1c846c1f NIS |
3766 | } |
3767 | else if (PL_curcop != &PL_compiling) | |
cea2e8a9 | 3768 | Perl_croak(aTHX_ PL_no_modify); |
0f15f207 | 3769 | } |
2213622d | 3770 | if (SvROK(sv)) |
840a7b70 | 3771 | sv_unref_flags(sv, flags); |
6fc92669 GS |
3772 | else if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV) |
3773 | sv_unglob(sv); | |
0f15f207 | 3774 | } |
1c846c1f | 3775 | |
840a7b70 IZ |
3776 | void |
3777 | Perl_sv_force_normal(pTHX_ register SV *sv) | |
3778 | { | |
3779 | sv_force_normal_flags(sv, 0); | |
3780 | } | |
3781 | ||
954c1994 GS |
3782 | /* |
3783 | =for apidoc sv_chop | |
3784 | ||
1c846c1f | 3785 | Efficient removal of characters from the beginning of the string buffer. |
954c1994 GS |
3786 | SvPOK(sv) must be true and the C<ptr> must be a pointer to somewhere inside |
3787 | the string buffer. The C<ptr> becomes the first character of the adjusted | |
3788 | string. | |
3789 | ||
3790 | =cut | |
3791 | */ | |
3792 | ||
79072805 | 3793 | void |
864dbfa3 | 3794 | Perl_sv_chop(pTHX_ register SV *sv, register char *ptr) /* like set but assuming ptr is in sv */ |
1c846c1f NIS |
3795 | |
3796 | ||
79072805 LW |
3797 | { |
3798 | register STRLEN delta; | |
3799 | ||
a0d0e21e | 3800 | if (!ptr || !SvPOKp(sv)) |
79072805 | 3801 | return; |
2213622d | 3802 | SV_CHECK_THINKFIRST(sv); |
79072805 LW |
3803 | if (SvTYPE(sv) < SVt_PVIV) |
3804 | sv_upgrade(sv,SVt_PVIV); | |
3805 | ||
3806 | if (!SvOOK(sv)) { | |
50483b2c JD |
3807 | if (!SvLEN(sv)) { /* make copy of shared string */ |
3808 | char *pvx = SvPVX(sv); | |
3809 | STRLEN len = SvCUR(sv); | |
3810 | SvGROW(sv, len + 1); | |
3811 | Move(pvx,SvPVX(sv),len,char); | |
3812 | *SvEND(sv) = '\0'; | |
3813 | } | |
463ee0b2 | 3814 | SvIVX(sv) = 0; |
79072805 LW |
3815 | SvFLAGS(sv) |= SVf_OOK; |
3816 | } | |
25da4f38 | 3817 | SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVp_IOK|SVp_NOK|SVf_IVisUV); |
463ee0b2 | 3818 | delta = ptr - SvPVX(sv); |
79072805 LW |
3819 | SvLEN(sv) -= delta; |
3820 | SvCUR(sv) -= delta; | |
463ee0b2 LW |
3821 | SvPVX(sv) += delta; |
3822 | SvIVX(sv) += delta; | |
79072805 LW |
3823 | } |
3824 | ||
954c1994 GS |
3825 | /* |
3826 | =for apidoc sv_catpvn | |
3827 | ||
3828 | Concatenates the string onto the end of the string which is in the SV. The | |
d5ce4a7c GA |
3829 | C<len> indicates number of bytes to copy. If the SV has the UTF8 |
3830 | status set, then the bytes appended should be valid UTF8. | |
3831 | Handles 'get' magic, but not 'set' magic. See C<sv_catpvn_mg>. | |
954c1994 GS |
3832 | |
3833 | =cut | |
3834 | */ | |
3835 | ||
79072805 | 3836 | void |
864dbfa3 | 3837 | Perl_sv_catpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len) |
79072805 | 3838 | { |
463ee0b2 | 3839 | STRLEN tlen; |
748a9306 | 3840 | char *junk; |
a0d0e21e | 3841 | |
748a9306 | 3842 | junk = SvPV_force(sv, tlen); |
463ee0b2 | 3843 | SvGROW(sv, tlen + len + 1); |
4633a7c4 LW |
3844 | if (ptr == junk) |
3845 | ptr = SvPVX(sv); | |
463ee0b2 | 3846 | Move(ptr,SvPVX(sv)+tlen,len,char); |
79072805 LW |
3847 | SvCUR(sv) += len; |
3848 | *SvEND(sv) = '\0'; | |
d41ff1b8 | 3849 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 | 3850 | SvTAINT(sv); |
79072805 LW |
3851 | } |
3852 | ||
954c1994 GS |
3853 | /* |
3854 | =for apidoc sv_catpvn_mg | |
3855 | ||
3856 | Like C<sv_catpvn>, but also handles 'set' magic. | |
3857 | ||
3858 | =cut | |
3859 | */ | |
3860 | ||
79072805 | 3861 | void |
864dbfa3 | 3862 | Perl_sv_catpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len) |
ef50df4b GS |
3863 | { |
3864 | sv_catpvn(sv,ptr,len); | |
3865 | SvSETMAGIC(sv); | |
3866 | } | |
3867 | ||
954c1994 GS |
3868 | /* |
3869 | =for apidoc sv_catsv | |
3870 | ||
13e8c8e3 JH |
3871 | Concatenates the string from SV C<ssv> onto the end of the string in |
3872 | SV C<dsv>. Modifies C<dsv> but not C<ssv>. Handles 'get' magic, but | |
3873 | not 'set' magic. See C<sv_catsv_mg>. | |
954c1994 | 3874 | |
13e8c8e3 | 3875 | =cut */ |
954c1994 | 3876 | |
ef50df4b | 3877 | void |
46199a12 | 3878 | Perl_sv_catsv(pTHX_ SV *dsv, register SV *ssv) |
79072805 | 3879 | { |
13e8c8e3 JH |
3880 | char *spv; |
3881 | STRLEN slen; | |
46199a12 | 3882 | if (!ssv) |
79072805 | 3883 | return; |
46199a12 JH |
3884 | if ((spv = SvPV(ssv, slen))) { |
3885 | bool dutf8 = DO_UTF8(dsv); | |
3886 | bool sutf8 = DO_UTF8(ssv); | |
13e8c8e3 JH |
3887 | |
3888 | if (dutf8 == sutf8) | |
46199a12 | 3889 | sv_catpvn(dsv,spv,slen); |
13e8c8e3 JH |
3890 | else { |
3891 | if (dutf8) { | |
46199a12 JH |
3892 | /* Not modifying source SV, so taking a temporary copy. */ |
3893 | SV* csv = sv_2mortal(newSVsv(ssv)); | |
13e8c8e3 JH |
3894 | char *cpv; |
3895 | STRLEN clen; | |
3896 | ||
46199a12 JH |
3897 | sv_utf8_upgrade(csv); |
3898 | cpv = SvPV(csv,clen); | |
3899 | sv_catpvn(dsv,cpv,clen); | |
13e8c8e3 JH |
3900 | } |
3901 | else { | |
46199a12 JH |
3902 | sv_utf8_upgrade(dsv); |
3903 | sv_catpvn(dsv,spv,slen); | |
3904 | SvUTF8_on(dsv); /* If dsv has no wide characters. */ | |
13e8c8e3 | 3905 | } |
e84ff256 | 3906 | } |
560a288e | 3907 | } |
79072805 LW |
3908 | } |
3909 | ||
954c1994 GS |
3910 | /* |
3911 | =for apidoc sv_catsv_mg | |
3912 | ||
3913 | Like C<sv_catsv>, but also handles 'set' magic. | |
3914 | ||
3915 | =cut | |
3916 | */ | |
3917 | ||
79072805 | 3918 | void |
46199a12 | 3919 | Perl_sv_catsv_mg(pTHX_ SV *dsv, register SV *ssv) |
ef50df4b | 3920 | { |
46199a12 JH |
3921 | sv_catsv(dsv,ssv); |
3922 | SvSETMAGIC(dsv); | |
ef50df4b GS |
3923 | } |
3924 | ||
954c1994 GS |
3925 | /* |
3926 | =for apidoc sv_catpv | |
3927 | ||
3928 | Concatenates the string onto the end of the string which is in the SV. | |
d5ce4a7c GA |
3929 | If the SV has the UTF8 status set, then the bytes appended should be |
3930 | valid UTF8. Handles 'get' magic, but not 'set' magic. See C<sv_catpv_mg>. | |
954c1994 | 3931 | |
d5ce4a7c | 3932 | =cut */ |
954c1994 | 3933 | |
ef50df4b | 3934 | void |
0c981600 | 3935 | Perl_sv_catpv(pTHX_ register SV *sv, register const char *ptr) |
79072805 LW |
3936 | { |
3937 | register STRLEN len; | |
463ee0b2 | 3938 | STRLEN tlen; |
748a9306 | 3939 | char *junk; |
79072805 | 3940 | |
0c981600 | 3941 | if (!ptr) |
79072805 | 3942 | return; |
748a9306 | 3943 | junk = SvPV_force(sv, tlen); |
0c981600 | 3944 | len = strlen(ptr); |
463ee0b2 | 3945 | SvGROW(sv, tlen + len + 1); |
0c981600 JH |
3946 | if (ptr == junk) |
3947 | ptr = SvPVX(sv); | |
3948 | Move(ptr,SvPVX(sv)+tlen,len+1,char); | |
79072805 | 3949 | SvCUR(sv) += len; |
d41ff1b8 | 3950 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 | 3951 | SvTAINT(sv); |
79072805 LW |
3952 | } |
3953 | ||
954c1994 GS |
3954 | /* |
3955 | =for apidoc sv_catpv_mg | |
3956 | ||
3957 | Like C<sv_catpv>, but also handles 'set' magic. | |
3958 | ||
3959 | =cut | |
3960 | */ | |
3961 | ||
ef50df4b | 3962 | void |
0c981600 | 3963 | Perl_sv_catpv_mg(pTHX_ register SV *sv, register const char *ptr) |
ef50df4b | 3964 | { |
0c981600 | 3965 | sv_catpv(sv,ptr); |
ef50df4b GS |
3966 | SvSETMAGIC(sv); |
3967 | } | |
3968 | ||
79072805 | 3969 | SV * |
864dbfa3 | 3970 | Perl_newSV(pTHX_ STRLEN len) |
79072805 LW |
3971 | { |
3972 | register SV *sv; | |
1c846c1f | 3973 | |
4561caa4 | 3974 | new_SV(sv); |
79072805 LW |
3975 | if (len) { |
3976 | sv_upgrade(sv, SVt_PV); | |
3977 | SvGROW(sv, len + 1); | |
3978 | } | |
3979 | return sv; | |
3980 | } | |
3981 | ||
1edc1566 | 3982 | /* name is assumed to contain an SV* if (name && namelen == HEf_SVKEY) */ |
3983 | ||
954c1994 GS |
3984 | /* |
3985 | =for apidoc sv_magic | |
3986 | ||
3987 | Adds magic to an SV. | |
3988 | ||
3989 | =cut | |
3990 | */ | |
3991 | ||
79072805 | 3992 | void |
864dbfa3 | 3993 | Perl_sv_magic(pTHX_ register SV *sv, SV *obj, int how, const char *name, I32 namlen) |
79072805 LW |
3994 | { |
3995 | MAGIC* mg; | |
1c846c1f | 3996 | |
0f15f207 | 3997 | if (SvREADONLY(sv)) { |
14befaf4 DM |
3998 | if (PL_curcop != &PL_compiling |
3999 | /* XXX this used to be !strchr("gBf", how), which seems to | |
4000 | * implicity be equal to !strchr("gBf\0", how), ie \0 matches | |
4001 | * too. I find this suprising, but have hadded PERL_MAGIC_sv | |
4002 | * to the list of things to check - DAPM 19-May-01 */ | |
4003 | && how != PERL_MAGIC_regex_global | |
4004 | && how != PERL_MAGIC_bm | |
4005 | && how != PERL_MAGIC_fm | |
4006 | && how != PERL_MAGIC_sv | |
4007 | ) | |
4008 | { | |
cea2e8a9 | 4009 | Perl_croak(aTHX_ PL_no_modify); |
14befaf4 | 4010 | } |
0f15f207 | 4011 | } |
14befaf4 | 4012 | if (SvMAGICAL(sv) || (how == PERL_MAGIC_taint && SvTYPE(sv) >= SVt_PVMG)) { |
748a9306 | 4013 | if (SvMAGIC(sv) && (mg = mg_find(sv, how))) { |
14befaf4 | 4014 | if (how == PERL_MAGIC_taint) |
565764a8 | 4015 | mg->mg_len |= 1; |
463ee0b2 | 4016 | return; |
748a9306 | 4017 | } |
463ee0b2 LW |
4018 | } |
4019 | else { | |
c6f8c383 | 4020 | (void)SvUPGRADE(sv, SVt_PVMG); |
463ee0b2 | 4021 | } |
79072805 LW |
4022 | Newz(702,mg, 1, MAGIC); |
4023 | mg->mg_moremagic = SvMAGIC(sv); | |
79072805 | 4024 | SvMAGIC(sv) = mg; |
75f9d97a JH |
4025 | |
4026 | /* Some magic sontains a reference loop, where the sv and object refer to | |
4027 | each other. To prevent a avoid a reference loop that would prevent such | |
4028 | objects being freed, we look for such loops and if we find one we avoid | |
4029 | incrementing the object refcount. */ | |
14befaf4 DM |
4030 | if (!obj || obj == sv || |
4031 | how == PERL_MAGIC_arylen || | |
4032 | how == PERL_MAGIC_qr || | |
75f9d97a JH |
4033 | (SvTYPE(obj) == SVt_PVGV && |
4034 | (GvSV(obj) == sv || GvHV(obj) == (HV*)sv || GvAV(obj) == (AV*)sv || | |
4035 | GvCV(obj) == (CV*)sv || GvIOp(obj) == (IO*)sv || | |
4036 | GvFORM(obj) == (CV*)sv))) | |
4037 | { | |
8990e307 | 4038 | mg->mg_obj = obj; |
75f9d97a | 4039 | } |
85e6fe83 | 4040 | else { |
8990e307 | 4041 | mg->mg_obj = SvREFCNT_inc(obj); |
85e6fe83 LW |
4042 | mg->mg_flags |= MGf_REFCOUNTED; |
4043 | } | |
79072805 | 4044 | mg->mg_type = how; |
565764a8 | 4045 | mg->mg_len = namlen; |
9cbac4c7 | 4046 | if (name) { |
1edc1566 | 4047 | if (namlen >= 0) |
4048 | mg->mg_ptr = savepvn(name, namlen); | |
c6ee37c5 | 4049 | else if (namlen == HEf_SVKEY) |
1edc1566 | 4050 | mg->mg_ptr = (char*)SvREFCNT_inc((SV*)name); |
9cbac4c7 | 4051 | } |
1c846c1f | 4052 | |
79072805 | 4053 | switch (how) { |
14befaf4 | 4054 | case PERL_MAGIC_sv: |
22c35a8c | 4055 | mg->mg_virtual = &PL_vtbl_sv; |
79072805 | 4056 | break; |
14befaf4 | 4057 | case PERL_MAGIC_overload: |
22c35a8c | 4058 | mg->mg_virtual = &PL_vtbl_amagic; |
a0d0e21e | 4059 | break; |
14befaf4 | 4060 | case PERL_MAGIC_overload_elem: |
22c35a8c | 4061 | mg->mg_virtual = &PL_vtbl_amagicelem; |
a0d0e21e | 4062 | break; |
14befaf4 | 4063 | case PERL_MAGIC_overload_table: |
d460ef45 | 4064 | mg->mg_virtual = &PL_vtbl_ovrld; |
a0d0e21e | 4065 | break; |
14befaf4 | 4066 | case PERL_MAGIC_bm: |
22c35a8c | 4067 | mg->mg_virtual = &PL_vtbl_bm; |
79072805 | 4068 | break; |
14befaf4 | 4069 | case PERL_MAGIC_regdata: |
22c35a8c | 4070 | mg->mg_virtual = &PL_vtbl_regdata; |
6cef1e77 | 4071 | break; |
14befaf4 | 4072 | case PERL_MAGIC_regdatum: |
22c35a8c | 4073 | mg->mg_virtual = &PL_vtbl_regdatum; |
6cef1e77 | 4074 | break; |
14befaf4 | 4075 | case PERL_MAGIC_env: |
22c35a8c | 4076 | mg->mg_virtual = &PL_vtbl_env; |
79072805 | 4077 | break; |
14befaf4 | 4078 | case PERL_MAGIC_fm: |
22c35a8c | 4079 | mg->mg_virtual = &PL_vtbl_fm; |
55497cff | 4080 | break; |
14befaf4 | 4081 | case PERL_MAGIC_envelem: |
22c35a8c | 4082 | mg->mg_virtual = &PL_vtbl_envelem; |
79072805 | 4083 | break; |
14befaf4 | 4084 | case PERL_MAGIC_regex_global: |
22c35a8c | 4085 | mg->mg_virtual = &PL_vtbl_mglob; |
93a17b20 | 4086 | break; |
14befaf4 | 4087 | case PERL_MAGIC_isa: |
22c35a8c | 4088 | mg->mg_virtual = &PL_vtbl_isa; |
463ee0b2 | 4089 | break; |
14befaf4 | 4090 | case PERL_MAGIC_isaelem: |
22c35a8c | 4091 | mg->mg_virtual = &PL_vtbl_isaelem; |
463ee0b2 | 4092 | break; |
14befaf4 | 4093 | case PERL_MAGIC_nkeys: |
22c35a8c | 4094 | mg->mg_virtual = &PL_vtbl_nkeys; |
16660edb | 4095 | break; |
14befaf4 | 4096 | case PERL_MAGIC_dbfile: |
a0d0e21e | 4097 | SvRMAGICAL_on(sv); |
93a17b20 LW |
4098 | mg->mg_virtual = 0; |
4099 | break; | |
14befaf4 | 4100 | case PERL_MAGIC_dbline: |
22c35a8c | 4101 | mg->mg_virtual = &PL_vtbl_dbline; |
79072805 | 4102 | break; |
f93b4edd | 4103 | #ifdef USE_THREADS |
14befaf4 | 4104 | case PERL_MAGIC_mutex: |
22c35a8c | 4105 | mg->mg_virtual = &PL_vtbl_mutex; |
f93b4edd MB |
4106 | break; |
4107 | #endif /* USE_THREADS */ | |
36477c24 | 4108 | #ifdef USE_LOCALE_COLLATE |
14befaf4 | 4109 | case PERL_MAGIC_collxfrm: |
22c35a8c | 4110 | mg->mg_virtual = &PL_vtbl_collxfrm; |
bbce6d69 | 4111 | break; |
36477c24 | 4112 | #endif /* USE_LOCALE_COLLATE */ |
14befaf4 | 4113 | case PERL_MAGIC_tied: |
22c35a8c | 4114 | mg->mg_virtual = &PL_vtbl_pack; |
463ee0b2 | 4115 | break; |
14befaf4 DM |
4116 | case PERL_MAGIC_tiedelem: |
4117 | case PERL_MAGIC_tiedscalar: | |
22c35a8c | 4118 | mg->mg_virtual = &PL_vtbl_packelem; |
463ee0b2 | 4119 | break; |
14befaf4 | 4120 | case PERL_MAGIC_qr: |
22c35a8c | 4121 | mg->mg_virtual = &PL_vtbl_regexp; |
c277df42 | 4122 | break; |
14befaf4 | 4123 | case PERL_MAGIC_sig: |
22c35a8c | 4124 | mg->mg_virtual = &PL_vtbl_sig; |
79072805 | 4125 | break; |
14befaf4 | 4126 | case PERL_MAGIC_sigelem: |
22c35a8c | 4127 | mg->mg_virtual = &PL_vtbl_sigelem; |
79072805 | 4128 | break; |
14befaf4 | 4129 | case PERL_MAGIC_taint: |
22c35a8c | 4130 | mg->mg_virtual = &PL_vtbl_taint; |
565764a8 | 4131 | mg->mg_len = 1; |
463ee0b2 | 4132 | break; |
14befaf4 | 4133 | case PERL_MAGIC_uvar: |
22c35a8c | 4134 | mg->mg_virtual = &PL_vtbl_uvar; |
79072805 | 4135 | break; |
14befaf4 | 4136 | case PERL_MAGIC_vec: |
22c35a8c | 4137 | mg->mg_virtual = &PL_vtbl_vec; |
79072805 | 4138 | break; |
14befaf4 | 4139 | case PERL_MAGIC_substr: |
22c35a8c | 4140 | mg->mg_virtual = &PL_vtbl_substr; |
79072805 | 4141 | break; |
14befaf4 | 4142 | case PERL_MAGIC_defelem: |
22c35a8c | 4143 | mg->mg_virtual = &PL_vtbl_defelem; |
5f05dabc | 4144 | break; |
14befaf4 | 4145 | case PERL_MAGIC_glob: |
22c35a8c | 4146 | mg->mg_virtual = &PL_vtbl_glob; |
79072805 | 4147 | break; |
14befaf4 | 4148 | case PERL_MAGIC_arylen: |
22c35a8c | 4149 | mg->mg_virtual = &PL_vtbl_arylen; |
79072805 | 4150 | break; |
14befaf4 | 4151 | case PERL_MAGIC_pos: |
22c35a8c | 4152 | mg->mg_virtual = &PL_vtbl_pos; |
a0d0e21e | 4153 | break; |
14befaf4 | 4154 | case PERL_MAGIC_backref: |
810b8aa5 GS |
4155 | mg->mg_virtual = &PL_vtbl_backref; |
4156 | break; | |
14befaf4 DM |
4157 | case PERL_MAGIC_ext: |
4158 | /* Reserved for use by extensions not perl internals. */ | |
4633a7c4 LW |
4159 | /* Useful for attaching extension internal data to perl vars. */ |
4160 | /* Note that multiple extensions may clash if magical scalars */ | |
4161 | /* etc holding private data from one are passed to another. */ | |
4162 | SvRMAGICAL_on(sv); | |
a0d0e21e | 4163 | break; |
79072805 | 4164 | default: |
14befaf4 | 4165 | Perl_croak(aTHX_ "Don't know how to handle magic of type \\%o", how); |
463ee0b2 | 4166 | } |
8990e307 LW |
4167 | mg_magical(sv); |
4168 | if (SvGMAGICAL(sv)) | |
4169 | SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK); | |
463ee0b2 LW |
4170 | } |
4171 | ||
c461cf8f JH |
4172 | /* |
4173 | =for apidoc sv_unmagic | |
4174 | ||
4175 | Removes magic from an SV. | |
4176 | ||
4177 | =cut | |
4178 | */ | |
4179 | ||
463ee0b2 | 4180 | int |
864dbfa3 | 4181 | Perl_sv_unmagic(pTHX_ SV *sv, int type) |
463ee0b2 LW |
4182 | { |
4183 | MAGIC* mg; | |
4184 | MAGIC** mgp; | |
91bba347 | 4185 | if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv)) |
463ee0b2 LW |
4186 | return 0; |
4187 | mgp = &SvMAGIC(sv); | |
4188 | for (mg = *mgp; mg; mg = *mgp) { | |
4189 | if (mg->mg_type == type) { | |
4190 | MGVTBL* vtbl = mg->mg_virtual; | |
4191 | *mgp = mg->mg_moremagic; | |
1d7c1841 | 4192 | if (vtbl && vtbl->svt_free) |
fc0dc3b3 | 4193 | CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg); |
14befaf4 | 4194 | if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) { |
565764a8 | 4195 | if (mg->mg_len >= 0) |
1edc1566 | 4196 | Safefree(mg->mg_ptr); |
565764a8 | 4197 | else if (mg->mg_len == HEf_SVKEY) |
1edc1566 | 4198 | SvREFCNT_dec((SV*)mg->mg_ptr); |
9cbac4c7 | 4199 | } |
a0d0e21e LW |
4200 | if (mg->mg_flags & MGf_REFCOUNTED) |
4201 | SvREFCNT_dec(mg->mg_obj); | |
463ee0b2 LW |
4202 | Safefree(mg); |
4203 | } | |
4204 | else | |
4205 | mgp = &mg->mg_moremagic; | |
79072805 | 4206 | } |
91bba347 | 4207 | if (!SvMAGIC(sv)) { |
463ee0b2 | 4208 | SvMAGICAL_off(sv); |
8990e307 | 4209 | SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT; |
463ee0b2 LW |
4210 | } |
4211 | ||
4212 | return 0; | |
79072805 LW |
4213 | } |
4214 | ||
c461cf8f JH |
4215 | /* |
4216 | =for apidoc sv_rvweaken | |
4217 | ||
4218 | Weaken a reference. | |
4219 | ||
4220 | =cut | |
4221 | */ | |
4222 | ||
810b8aa5 | 4223 | SV * |
864dbfa3 | 4224 | Perl_sv_rvweaken(pTHX_ SV *sv) |
810b8aa5 GS |
4225 | { |
4226 | SV *tsv; | |
4227 | if (!SvOK(sv)) /* let undefs pass */ | |
4228 | return sv; | |
4229 | if (!SvROK(sv)) | |
cea2e8a9 | 4230 | Perl_croak(aTHX_ "Can't weaken a nonreference"); |
810b8aa5 | 4231 | else if (SvWEAKREF(sv)) { |
810b8aa5 | 4232 | if (ckWARN(WARN_MISC)) |
cea2e8a9 | 4233 | Perl_warner(aTHX_ WARN_MISC, "Reference is already weak"); |
810b8aa5 GS |
4234 | return sv; |
4235 | } | |
4236 | tsv = SvRV(sv); | |
4237 | sv_add_backref(tsv, sv); | |
4238 | SvWEAKREF_on(sv); | |
1c846c1f | 4239 | SvREFCNT_dec(tsv); |
810b8aa5 GS |
4240 | return sv; |
4241 | } | |
4242 | ||
4243 | STATIC void | |
cea2e8a9 | 4244 | S_sv_add_backref(pTHX_ SV *tsv, SV *sv) |
810b8aa5 GS |
4245 | { |
4246 | AV *av; | |
4247 | MAGIC *mg; | |
14befaf4 | 4248 | if (SvMAGICAL(tsv) && (mg = mg_find(tsv, PERL_MAGIC_backref))) |
810b8aa5 GS |
4249 | av = (AV*)mg->mg_obj; |
4250 | else { | |
4251 | av = newAV(); | |
14befaf4 | 4252 | sv_magic(tsv, (SV*)av, PERL_MAGIC_backref, NULL, 0); |
810b8aa5 GS |
4253 | SvREFCNT_dec(av); /* for sv_magic */ |
4254 | } | |
4255 | av_push(av,sv); | |
4256 | } | |
4257 | ||
1c846c1f | 4258 | STATIC void |
cea2e8a9 | 4259 | S_sv_del_backref(pTHX_ SV *sv) |
810b8aa5 GS |
4260 | { |
4261 | AV *av; | |
4262 | SV **svp; | |
4263 | I32 i; | |
4264 | SV *tsv = SvRV(sv); | |
4265 | MAGIC *mg; | |
14befaf4 | 4266 | if (!SvMAGICAL(tsv) || !(mg = mg_find(tsv, PERL_MAGIC_backref))) |
cea2e8a9 | 4267 | Perl_croak(aTHX_ "panic: del_backref"); |
810b8aa5 GS |
4268 | av = (AV *)mg->mg_obj; |
4269 | svp = AvARRAY(av); | |
4270 | i = AvFILLp(av); | |
4271 | while (i >= 0) { | |
4272 | if (svp[i] == sv) { | |
4273 | svp[i] = &PL_sv_undef; /* XXX */ | |
4274 | } | |
4275 | i--; | |
4276 | } | |
4277 | } | |
4278 | ||
954c1994 GS |
4279 | /* |
4280 | =for apidoc sv_insert | |
4281 | ||
4282 | Inserts a string at the specified offset/length within the SV. Similar to | |
4283 | the Perl substr() function. | |
4284 | ||
4285 | =cut | |
4286 | */ | |
4287 | ||
79072805 | 4288 | void |
864dbfa3 | 4289 | Perl_sv_insert(pTHX_ SV *bigstr, STRLEN offset, STRLEN len, char *little, STRLEN littlelen) |
79072805 LW |
4290 | { |
4291 | register char *big; | |
4292 | register char *mid; | |
4293 | register char *midend; | |
4294 | register char *bigend; | |
4295 | register I32 i; | |
6ff81951 | 4296 | STRLEN curlen; |
1c846c1f | 4297 | |
79072805 | 4298 | |
8990e307 | 4299 | if (!bigstr) |
cea2e8a9 | 4300 | Perl_croak(aTHX_ "Can't modify non-existent substring"); |
6ff81951 | 4301 | SvPV_force(bigstr, curlen); |
60fa28ff | 4302 | (void)SvPOK_only_UTF8(bigstr); |
6ff81951 GS |
4303 | if (offset + len > curlen) { |
4304 | SvGROW(bigstr, offset+len+1); | |
4305 | Zero(SvPVX(bigstr)+curlen, offset+len-curlen, char); | |
4306 | SvCUR_set(bigstr, offset+len); | |
4307 | } | |
79072805 | 4308 | |
69b47968 | 4309 | SvTAINT(bigstr); |
79072805 LW |
4310 | i = littlelen - len; |
4311 | if (i > 0) { /* string might grow */ | |
a0d0e21e | 4312 | big = SvGROW(bigstr, SvCUR(bigstr) + i + 1); |
79072805 LW |
4313 | mid = big + offset + len; |
4314 | midend = bigend = big + SvCUR(bigstr); | |
4315 | bigend += i; | |
4316 | *bigend = '\0'; | |
4317 | while (midend > mid) /* shove everything down */ | |
4318 | *--bigend = *--midend; | |
4319 | Move(little,big+offset,littlelen,char); | |
4320 | SvCUR(bigstr) += i; | |
4321 | SvSETMAGIC(bigstr); | |
4322 | return; | |
4323 | } | |
4324 | else if (i == 0) { | |
463ee0b2 | 4325 | Move(little,SvPVX(bigstr)+offset,len,char); |
79072805 LW |
4326 | SvSETMAGIC(bigstr); |
4327 | return; | |
4328 | } | |
4329 | ||
463ee0b2 | 4330 | big = SvPVX(bigstr); |
79072805 LW |
4331 | mid = big + offset; |
4332 | midend = mid + len; | |
4333 | bigend = big + SvCUR(bigstr); | |
4334 | ||
4335 | if (midend > bigend) | |
cea2e8a9 | 4336 | Perl_croak(aTHX_ "panic: sv_insert"); |
79072805 LW |
4337 | |
4338 | if (mid - big > bigend - midend) { /* faster to shorten from end */ | |
4339 | if (littlelen) { | |
4340 | Move(little, mid, littlelen,char); | |
4341 | mid += littlelen; | |
4342 | } | |
4343 | i = bigend - midend; | |
4344 | if (i > 0) { | |
4345 | Move(midend, mid, i,char); | |
4346 | mid += i; | |
4347 | } | |
4348 | *mid = '\0'; | |
4349 | SvCUR_set(bigstr, mid - big); | |
4350 | } | |
4351 | /*SUPPRESS 560*/ | |
155aba94 | 4352 | else if ((i = mid - big)) { /* faster from front */ |
79072805 LW |
4353 | midend -= littlelen; |
4354 | mid = midend; | |
4355 | sv_chop(bigstr,midend-i); | |
4356 | big += i; | |
4357 | while (i--) | |
4358 | *--midend = *--big; | |
4359 | if (littlelen) | |
4360 | Move(little, mid, littlelen,char); | |
4361 | } | |
4362 | else if (littlelen) { | |
4363 | midend -= littlelen; | |
4364 | sv_chop(bigstr,midend); | |
4365 | Move(little,midend,littlelen,char); | |
4366 | } | |
4367 | else { | |
4368 | sv_chop(bigstr,midend); | |
4369 | } | |
4370 | SvSETMAGIC(bigstr); | |
4371 | } | |
4372 | ||
c461cf8f JH |
4373 | /* |
4374 | =for apidoc sv_replace | |
4375 | ||
4376 | Make the first argument a copy of the second, then delete the original. | |
4377 | ||
4378 | =cut | |
4379 | */ | |
79072805 LW |
4380 | |
4381 | void | |
864dbfa3 | 4382 | Perl_sv_replace(pTHX_ register SV *sv, register SV *nsv) |
79072805 LW |
4383 | { |
4384 | U32 refcnt = SvREFCNT(sv); | |
2213622d | 4385 | SV_CHECK_THINKFIRST(sv); |
0453d815 PM |
4386 | if (SvREFCNT(nsv) != 1 && ckWARN_d(WARN_INTERNAL)) |
4387 | Perl_warner(aTHX_ WARN_INTERNAL, "Reference miscount in sv_replace()"); | |
93a17b20 | 4388 | if (SvMAGICAL(sv)) { |
a0d0e21e LW |
4389 | if (SvMAGICAL(nsv)) |
4390 | mg_free(nsv); | |
4391 | else | |
4392 | sv_upgrade(nsv, SVt_PVMG); | |
93a17b20 | 4393 | SvMAGIC(nsv) = SvMAGIC(sv); |
a0d0e21e | 4394 | SvFLAGS(nsv) |= SvMAGICAL(sv); |
93a17b20 LW |
4395 | SvMAGICAL_off(sv); |
4396 | SvMAGIC(sv) = 0; | |
4397 | } | |
79072805 LW |
4398 | SvREFCNT(sv) = 0; |
4399 | sv_clear(sv); | |
477f5d66 | 4400 | assert(!SvREFCNT(sv)); |
79072805 LW |
4401 | StructCopy(nsv,sv,SV); |
4402 | SvREFCNT(sv) = refcnt; | |
1edc1566 | 4403 | SvFLAGS(nsv) |= SVTYPEMASK; /* Mark as freed */ |
463ee0b2 | 4404 | del_SV(nsv); |
79072805 LW |
4405 | } |
4406 | ||
c461cf8f JH |
4407 | /* |
4408 | =for apidoc sv_clear | |
4409 | ||
4410 | Clear an SV, making it empty. Does not free the memory used by the SV | |
4411 | itself. | |
4412 | ||
4413 | =cut | |
4414 | */ | |
4415 | ||
79072805 | 4416 | void |
864dbfa3 | 4417 | Perl_sv_clear(pTHX_ register SV *sv) |
79072805 | 4418 | { |
ec12f114 | 4419 | HV* stash; |
79072805 LW |
4420 | assert(sv); |
4421 | assert(SvREFCNT(sv) == 0); | |
4422 | ||
ed6116ce | 4423 | if (SvOBJECT(sv)) { |
3280af22 | 4424 | if (PL_defstash) { /* Still have a symbol table? */ |
39644a26 | 4425 | dSP; |
32251b26 | 4426 | CV* destructor; |
837485b6 | 4427 | SV tmpref; |
a0d0e21e | 4428 | |
837485b6 GS |
4429 | Zero(&tmpref, 1, SV); |
4430 | sv_upgrade(&tmpref, SVt_RV); | |
4431 | SvROK_on(&tmpref); | |
4432 | SvREADONLY_on(&tmpref); /* DESTROY() could be naughty */ | |
4433 | SvREFCNT(&tmpref) = 1; | |
8ebc5c01 | 4434 | |
d460ef45 | 4435 | do { |
4e8e7886 | 4436 | stash = SvSTASH(sv); |
32251b26 | 4437 | destructor = StashHANDLER(stash,DESTROY); |
4e8e7886 GS |
4438 | if (destructor) { |
4439 | ENTER; | |
e788e7d3 | 4440 | PUSHSTACKi(PERLSI_DESTROY); |
837485b6 | 4441 | SvRV(&tmpref) = SvREFCNT_inc(sv); |
4e8e7886 GS |
4442 | EXTEND(SP, 2); |
4443 | PUSHMARK(SP); | |
837485b6 | 4444 | PUSHs(&tmpref); |
4e8e7886 | 4445 | PUTBACK; |
32251b26 | 4446 | call_sv((SV*)destructor, G_DISCARD|G_EVAL|G_KEEPERR); |
4e8e7886 | 4447 | SvREFCNT(sv)--; |
d3acc0f7 | 4448 | POPSTACK; |
3095d977 | 4449 | SPAGAIN; |
4e8e7886 GS |
4450 | LEAVE; |
4451 | } | |
4452 | } while (SvOBJECT(sv) && SvSTASH(sv) != stash); | |
8ebc5c01 | 4453 | |
837485b6 | 4454 | del_XRV(SvANY(&tmpref)); |
6f44e0a4 JP |
4455 | |
4456 | if (SvREFCNT(sv)) { | |
4457 | if (PL_in_clean_objs) | |
cea2e8a9 | 4458 | Perl_croak(aTHX_ "DESTROY created new reference to dead object '%s'", |
6f44e0a4 JP |
4459 | HvNAME(stash)); |
4460 | /* DESTROY gave object new lease on life */ | |
4461 | return; | |
4462 | } | |
a0d0e21e | 4463 | } |
4e8e7886 | 4464 | |
a0d0e21e | 4465 | if (SvOBJECT(sv)) { |
4e8e7886 | 4466 | SvREFCNT_dec(SvSTASH(sv)); /* possibly of changed persuasion */ |
a0d0e21e LW |
4467 | SvOBJECT_off(sv); /* Curse the object. */ |
4468 | if (SvTYPE(sv) != SVt_PVIO) | |
3280af22 | 4469 | --PL_sv_objcount; /* XXX Might want something more general */ |
a0d0e21e | 4470 | } |
463ee0b2 | 4471 | } |
c07a80fd | 4472 | if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) |
a0d0e21e | 4473 | mg_free(sv); |
ec12f114 | 4474 | stash = NULL; |
79072805 | 4475 | switch (SvTYPE(sv)) { |
8990e307 | 4476 | case SVt_PVIO: |
df0bd2f4 GS |
4477 | if (IoIFP(sv) && |
4478 | IoIFP(sv) != PerlIO_stdin() && | |
5f05dabc | 4479 | IoIFP(sv) != PerlIO_stdout() && |
4480 | IoIFP(sv) != PerlIO_stderr()) | |
93578b34 | 4481 | { |
f2b5be74 | 4482 | io_close((IO*)sv, FALSE); |
93578b34 | 4483 | } |
1d7c1841 | 4484 | if (IoDIRP(sv) && !(IoFLAGS(sv) & IOf_FAKE_DIRP)) |
1236053a | 4485 | PerlDir_close(IoDIRP(sv)); |
1d7c1841 | 4486 | IoDIRP(sv) = (DIR*)NULL; |
8990e307 LW |
4487 | Safefree(IoTOP_NAME(sv)); |
4488 | Safefree(IoFMT_NAME(sv)); | |
4489 | Safefree(IoBOTTOM_NAME(sv)); | |
a0d0e21e | 4490 | /* FALL THROUGH */ |
79072805 | 4491 | case SVt_PVBM: |
a0d0e21e | 4492 | goto freescalar; |
79072805 | 4493 | case SVt_PVCV: |
748a9306 | 4494 | case SVt_PVFM: |
85e6fe83 | 4495 | cv_undef((CV*)sv); |
a0d0e21e | 4496 | goto freescalar; |
79072805 | 4497 | case SVt_PVHV: |
85e6fe83 | 4498 | hv_undef((HV*)sv); |
a0d0e21e | 4499 | break; |
79072805 | 4500 | case SVt_PVAV: |
85e6fe83 | 4501 | av_undef((AV*)sv); |
a0d0e21e | 4502 | break; |
02270b4e GS |
4503 | case SVt_PVLV: |
4504 | SvREFCNT_dec(LvTARG(sv)); | |
4505 | goto freescalar; | |
a0d0e21e | 4506 | case SVt_PVGV: |
1edc1566 | 4507 | gp_free((GV*)sv); |
a0d0e21e | 4508 | Safefree(GvNAME(sv)); |
ec12f114 JPC |
4509 | /* cannot decrease stash refcount yet, as we might recursively delete |
4510 | ourselves when the refcnt drops to zero. Delay SvREFCNT_dec | |
4511 | of stash until current sv is completely gone. | |
4512 | -- JohnPC, 27 Mar 1998 */ | |
4513 | stash = GvSTASH(sv); | |
a0d0e21e | 4514 | /* FALL THROUGH */ |
79072805 | 4515 | case SVt_PVMG: |
79072805 LW |
4516 | case SVt_PVNV: |
4517 | case SVt_PVIV: | |
a0d0e21e LW |
4518 | freescalar: |
4519 | (void)SvOOK_off(sv); | |
79072805 LW |
4520 | /* FALL THROUGH */ |
4521 | case SVt_PV: | |
a0d0e21e | 4522 | case SVt_RV: |
810b8aa5 GS |
4523 | if (SvROK(sv)) { |
4524 | if (SvWEAKREF(sv)) | |
4525 | sv_del_backref(sv); | |
4526 | else | |
4527 | SvREFCNT_dec(SvRV(sv)); | |
4528 | } | |
1edc1566 | 4529 | else if (SvPVX(sv) && SvLEN(sv)) |
463ee0b2 | 4530 | Safefree(SvPVX(sv)); |
1c846c1f | 4531 | else if (SvPVX(sv) && SvREADONLY(sv) && SvFAKE(sv)) { |
c3654f1a | 4532 | unsharepvn(SvPVX(sv),SvUTF8(sv)?-SvCUR(sv):SvCUR(sv),SvUVX(sv)); |
1c846c1f NIS |
4533 | SvFAKE_off(sv); |
4534 | } | |
79072805 | 4535 | break; |
a0d0e21e | 4536 | /* |
79072805 | 4537 | case SVt_NV: |
79072805 | 4538 | case SVt_IV: |
79072805 LW |
4539 | case SVt_NULL: |
4540 | break; | |
a0d0e21e | 4541 | */ |
79072805 LW |
4542 | } |
4543 | ||
4544 | switch (SvTYPE(sv)) { | |
4545 | case SVt_NULL: | |
4546 | break; | |
79072805 LW |
4547 | case SVt_IV: |
4548 | del_XIV(SvANY(sv)); | |
4549 | break; | |
4550 | case SVt_NV: | |
4551 | del_XNV(SvANY(sv)); | |
4552 | break; | |
ed6116ce LW |
4553 | case SVt_RV: |
4554 | del_XRV(SvANY(sv)); | |
4555 | break; | |
79072805 LW |
4556 | case SVt_PV: |
4557 | del_XPV(SvANY(sv)); | |
4558 | break; | |
4559 | case SVt_PVIV: | |
4560 | del_XPVIV(SvANY(sv)); | |
4561 | break; | |
4562 | case SVt_PVNV: | |
4563 | del_XPVNV(SvANY(sv)); | |
4564 | break; | |
4565 | case SVt_PVMG: | |
4566 | del_XPVMG(SvANY(sv)); | |
4567 | break; | |
4568 | case SVt_PVLV: | |
4569 | del_XPVLV(SvANY(sv)); | |
4570 | break; | |
4571 | case SVt_PVAV: | |
4572 | del_XPVAV(SvANY(sv)); | |
4573 | break; | |
4574 | case SVt_PVHV: | |
4575 | del_XPVHV(SvANY(sv)); | |
4576 | break; | |
4577 | case SVt_PVCV: | |
4578 | del_XPVCV(SvANY(sv)); | |
4579 | break; | |
4580 | case SVt_PVGV: | |
4581 | del_XPVGV(SvANY(sv)); | |
ec12f114 JPC |
4582 | /* code duplication for increased performance. */ |
4583 | SvFLAGS(sv) &= SVf_BREAK; | |
4584 | SvFLAGS(sv) |= SVTYPEMASK; | |
4585 | /* decrease refcount of the stash that owns this GV, if any */ | |
4586 | if (stash) | |
4587 | SvREFCNT_dec(stash); | |
4588 | return; /* not break, SvFLAGS reset already happened */ | |
79072805 LW |
4589 | case SVt_PVBM: |
4590 | del_XPVBM(SvANY(sv)); | |
4591 | break; | |
4592 | case SVt_PVFM: | |
4593 | del_XPVFM(SvANY(sv)); | |
4594 | break; | |
8990e307 LW |
4595 | case SVt_PVIO: |
4596 | del_XPVIO(SvANY(sv)); | |
4597 | break; | |
79072805 | 4598 | } |
a0d0e21e | 4599 | SvFLAGS(sv) &= SVf_BREAK; |
8990e307 | 4600 | SvFLAGS(sv) |= SVTYPEMASK; |
79072805 LW |
4601 | } |
4602 | ||
4603 | SV * | |
864dbfa3 | 4604 | Perl_sv_newref(pTHX_ SV *sv) |
79072805 | 4605 | { |
463ee0b2 | 4606 | if (sv) |
dce16143 | 4607 | ATOMIC_INC(SvREFCNT(sv)); |
79072805 LW |
4608 | return sv; |
4609 | } | |
4610 | ||
c461cf8f JH |
4611 | /* |
4612 | =for apidoc sv_free | |
4613 | ||
4614 | Free the memory used by an SV. | |
4615 | ||
4616 | =cut | |
4617 | */ | |
4618 | ||
79072805 | 4619 | void |
864dbfa3 | 4620 | Perl_sv_free(pTHX_ SV *sv) |
79072805 | 4621 | { |
dce16143 MB |
4622 | int refcount_is_zero; |
4623 | ||
79072805 LW |
4624 | if (!sv) |
4625 | return; | |
a0d0e21e LW |
4626 | if (SvREFCNT(sv) == 0) { |
4627 | if (SvFLAGS(sv) & SVf_BREAK) | |
4628 | return; | |
3280af22 | 4629 | if (PL_in_clean_all) /* All is fair */ |
1edc1566 | 4630 | return; |
d689ffdd JP |
4631 | if (SvREADONLY(sv) && SvIMMORTAL(sv)) { |
4632 | /* make sure SvREFCNT(sv)==0 happens very seldom */ | |
4633 | SvREFCNT(sv) = (~(U32)0)/2; | |
4634 | return; | |
4635 | } | |
0453d815 PM |
4636 | if (ckWARN_d(WARN_INTERNAL)) |
4637 | Perl_warner(aTHX_ WARN_INTERNAL, "Attempt to free unreferenced scalar"); | |
79072805 LW |
4638 | return; |
4639 | } | |
dce16143 MB |
4640 | ATOMIC_DEC_AND_TEST(refcount_is_zero, SvREFCNT(sv)); |
4641 | if (!refcount_is_zero) | |
8990e307 | 4642 | return; |
463ee0b2 LW |
4643 | #ifdef DEBUGGING |
4644 | if (SvTEMP(sv)) { | |
0453d815 | 4645 | if (ckWARN_d(WARN_DEBUGGING)) |
f248d071 | 4646 | Perl_warner(aTHX_ WARN_DEBUGGING, |
1d7c1841 GS |
4647 | "Attempt to free temp prematurely: SV 0x%"UVxf, |
4648 | PTR2UV(sv)); | |
79072805 | 4649 | return; |
79072805 | 4650 | } |
463ee0b2 | 4651 | #endif |
d689ffdd JP |
4652 | if (SvREADONLY(sv) && SvIMMORTAL(sv)) { |
4653 | /* make sure SvREFCNT(sv)==0 happens very seldom */ | |
4654 | SvREFCNT(sv) = (~(U32)0)/2; | |
4655 | return; | |
4656 | } | |
79072805 | 4657 | sv_clear(sv); |
477f5d66 CS |
4658 | if (! SvREFCNT(sv)) |
4659 | del_SV(sv); | |
79072805 LW |
4660 | } |
4661 | ||
954c1994 GS |
4662 | /* |
4663 | =for apidoc sv_len | |
4664 | ||
4665 | Returns the length of the string in the SV. See also C<SvCUR>. | |
4666 | ||
4667 | =cut | |
4668 | */ | |
4669 | ||
79072805 | 4670 | STRLEN |
864dbfa3 | 4671 | Perl_sv_len(pTHX_ register SV *sv) |
79072805 | 4672 | { |
748a9306 | 4673 | char *junk; |
463ee0b2 | 4674 | STRLEN len; |
79072805 LW |
4675 | |
4676 | if (!sv) | |
4677 | return 0; | |
4678 | ||
8990e307 | 4679 | if (SvGMAGICAL(sv)) |
565764a8 | 4680 | len = mg_length(sv); |
8990e307 | 4681 | else |
748a9306 | 4682 | junk = SvPV(sv, len); |
463ee0b2 | 4683 | return len; |
79072805 LW |
4684 | } |
4685 | ||
c461cf8f JH |
4686 | /* |
4687 | =for apidoc sv_len_utf8 | |
4688 | ||
4689 | Returns the number of characters in the string in an SV, counting wide | |
4690 | UTF8 bytes as a single character. | |
4691 | ||
4692 | =cut | |
4693 | */ | |
4694 | ||
a0ed51b3 | 4695 | STRLEN |
864dbfa3 | 4696 | Perl_sv_len_utf8(pTHX_ register SV *sv) |
a0ed51b3 | 4697 | { |
a0ed51b3 LW |
4698 | if (!sv) |
4699 | return 0; | |
4700 | ||
a0ed51b3 | 4701 | if (SvGMAGICAL(sv)) |
b76347f2 | 4702 | return mg_length(sv); |
a0ed51b3 | 4703 | else |
b76347f2 JH |
4704 | { |
4705 | STRLEN len; | |
4706 | U8 *s = (U8*)SvPV(sv, len); | |
4707 | ||
d6efbbad | 4708 | return Perl_utf8_length(aTHX_ s, s + len); |
a0ed51b3 | 4709 | } |
a0ed51b3 LW |
4710 | } |
4711 | ||
4712 | void | |
864dbfa3 | 4713 | Perl_sv_pos_u2b(pTHX_ register SV *sv, I32* offsetp, I32* lenp) |
a0ed51b3 | 4714 | { |
dfe13c55 GS |
4715 | U8 *start; |
4716 | U8 *s; | |
4717 | U8 *send; | |
a0ed51b3 LW |
4718 | I32 uoffset = *offsetp; |
4719 | STRLEN len; | |
4720 | ||
4721 | if (!sv) | |
4722 | return; | |
4723 | ||
dfe13c55 | 4724 | start = s = (U8*)SvPV(sv, len); |
a0ed51b3 LW |
4725 | send = s + len; |
4726 | while (s < send && uoffset--) | |
4727 | s += UTF8SKIP(s); | |
bb40f870 GA |
4728 | if (s >= send) |
4729 | s = send; | |
a0ed51b3 LW |
4730 | *offsetp = s - start; |
4731 | if (lenp) { | |
4732 | I32 ulen = *lenp; | |
4733 | start = s; | |
4734 | while (s < send && ulen--) | |
4735 | s += UTF8SKIP(s); | |
bb40f870 GA |
4736 | if (s >= send) |
4737 | s = send; | |
a0ed51b3 LW |
4738 | *lenp = s - start; |
4739 | } | |
4740 | return; | |
4741 | } | |
4742 | ||
4743 | void | |
864dbfa3 | 4744 | Perl_sv_pos_b2u(pTHX_ register SV *sv, I32* offsetp) |
a0ed51b3 | 4745 | { |
dfe13c55 GS |
4746 | U8 *s; |
4747 | U8 *send; | |
a0ed51b3 LW |
4748 | STRLEN len; |
4749 | ||
4750 | if (!sv) | |
4751 | return; | |
4752 | ||
dfe13c55 | 4753 | s = (U8*)SvPV(sv, len); |
a0ed51b3 | 4754 | if (len < *offsetp) |
a0dbb045 | 4755 | Perl_croak(aTHX_ "panic: sv_pos_b2u: bad byte offset"); |
a0ed51b3 LW |
4756 | send = s + *offsetp; |
4757 | len = 0; | |
4758 | while (s < send) { | |
a0dbb045 | 4759 | STRLEN n; |
2b9d42f0 NIS |
4760 | /* Call utf8n_to_uvchr() to validate the sequence */ |
4761 | utf8n_to_uvchr(s, UTF8SKIP(s), &n, 0); | |
4762 | if (n > 0) { | |
a0dbb045 JH |
4763 | s += n; |
4764 | len++; | |
4765 | } | |
4766 | else | |
4767 | break; | |
a0ed51b3 LW |
4768 | } |
4769 | *offsetp = len; | |
4770 | return; | |
4771 | } | |
4772 | ||
954c1994 GS |
4773 | /* |
4774 | =for apidoc sv_eq | |
4775 | ||
4776 | Returns a boolean indicating whether the strings in the two SVs are | |
4777 | identical. | |
4778 | ||
4779 | =cut | |
4780 | */ | |
4781 | ||
79072805 | 4782 | I32 |
e01b9e88 | 4783 | Perl_sv_eq(pTHX_ register SV *sv1, register SV *sv2) |
79072805 LW |
4784 | { |
4785 | char *pv1; | |
463ee0b2 | 4786 | STRLEN cur1; |
79072805 | 4787 | char *pv2; |
463ee0b2 | 4788 | STRLEN cur2; |
e01b9e88 | 4789 | I32 eq = 0; |
db42d148 | 4790 | char *tpv = Nullch; |
79072805 | 4791 | |
e01b9e88 | 4792 | if (!sv1) { |
79072805 LW |
4793 | pv1 = ""; |
4794 | cur1 = 0; | |
4795 | } | |
463ee0b2 | 4796 | else |
e01b9e88 | 4797 | pv1 = SvPV(sv1, cur1); |
79072805 | 4798 | |
e01b9e88 SC |
4799 | if (!sv2){ |
4800 | pv2 = ""; | |
4801 | cur2 = 0; | |
92d29cee | 4802 | } |
e01b9e88 SC |
4803 | else |
4804 | pv2 = SvPV(sv2, cur2); | |
79072805 | 4805 | |
e01b9e88 | 4806 | /* do not utf8ize the comparands as a side-effect */ |
7bbb0251 | 4807 | if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTE) { |
f9a63242 | 4808 | bool is_utf8 = TRUE; |
db42d148 | 4809 | /* UTF-8ness differs */ |
1aa99e6b IH |
4810 | if (PL_hints & HINT_UTF8_DISTINCT) |
4811 | return FALSE; | |
4812 | ||
e01b9e88 | 4813 | if (SvUTF8(sv1)) { |
db42d148 | 4814 | /* sv1 is the UTF-8 one , If is equal it must be downgrade-able */ |
f34ff0a8 | 4815 | char *pv = (char*)bytes_from_utf8((U8*)pv1, &cur1, &is_utf8); |
db42d148 NIS |
4816 | if (pv != pv1) |
4817 | pv1 = tpv = pv; | |
e01b9e88 SC |
4818 | } |
4819 | else { | |
db42d148 | 4820 | /* sv2 is the UTF-8 one , If is equal it must be downgrade-able */ |
f34ff0a8 | 4821 | char *pv = (char *)bytes_from_utf8((U8*)pv2, &cur2, &is_utf8); |
db42d148 NIS |
4822 | if (pv != pv2) |
4823 | pv2 = tpv = pv; | |
4824 | } | |
4825 | if (is_utf8) { | |
4826 | /* Downgrade not possible - cannot be eq */ | |
4827 | return FALSE; | |
e01b9e88 SC |
4828 | } |
4829 | } | |
79072805 | 4830 | |
e01b9e88 SC |
4831 | if (cur1 == cur2) |
4832 | eq = memEQ(pv1, pv2, cur1); | |
4833 | ||
db42d148 NIS |
4834 | if (tpv != Nullch) |
4835 | Safefree(tpv); | |
e01b9e88 SC |
4836 | |
4837 | return eq; | |
79072805 LW |
4838 | } |
4839 | ||
954c1994 GS |
4840 | /* |
4841 | =for apidoc sv_cmp | |
4842 | ||
4843 | Compares the strings in two SVs. Returns -1, 0, or 1 indicating whether the | |
4844 | string in C<sv1> is less than, equal to, or greater than the string in | |
4845 | C<sv2>. | |
4846 | ||
4847 | =cut | |
4848 | */ | |
4849 | ||
79072805 | 4850 | I32 |
e01b9e88 | 4851 | Perl_sv_cmp(pTHX_ register SV *sv1, register SV *sv2) |
79072805 | 4852 | { |
560a288e GS |
4853 | STRLEN cur1, cur2; |
4854 | char *pv1, *pv2; | |
1c846c1f | 4855 | I32 cmp; |
e01b9e88 SC |
4856 | bool pv1tmp = FALSE; |
4857 | bool pv2tmp = FALSE; | |
560a288e | 4858 | |
e01b9e88 SC |
4859 | if (!sv1) { |
4860 | pv1 = ""; | |
560a288e GS |
4861 | cur1 = 0; |
4862 | } | |
e01b9e88 SC |
4863 | else |
4864 | pv1 = SvPV(sv1, cur1); | |
560a288e | 4865 | |
e01b9e88 SC |
4866 | if (!sv2){ |
4867 | pv2 = ""; | |
560a288e GS |
4868 | cur2 = 0; |
4869 | } | |
e01b9e88 SC |
4870 | else |
4871 | pv2 = SvPV(sv2, cur2); | |
79072805 | 4872 | |
e01b9e88 SC |
4873 | /* do not utf8ize the comparands as a side-effect */ |
4874 | if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTE) { | |
1aa99e6b IH |
4875 | if (PL_hints & HINT_UTF8_DISTINCT) |
4876 | return SvUTF8(sv1) ? 1 : -1; | |
4877 | ||
e01b9e88 SC |
4878 | if (SvUTF8(sv1)) { |
4879 | pv2 = (char*)bytes_to_utf8((U8*)pv2, &cur2); | |
4880 | pv2tmp = TRUE; | |
4881 | } | |
4882 | else { | |
4883 | pv1 = (char*)bytes_to_utf8((U8*)pv1, &cur1); | |
4884 | pv1tmp = TRUE; | |
4885 | } | |
4886 | } | |
79072805 | 4887 | |
e01b9e88 SC |
4888 | if (!cur1) { |
4889 | cmp = cur2 ? -1 : 0; | |
4890 | } else if (!cur2) { | |
4891 | cmp = 1; | |
4892 | } else { | |
4893 | I32 retval = memcmp((void*)pv1, (void*)pv2, cur1 < cur2 ? cur1 : cur2); | |
4894 | ||
4895 | if (retval) { | |
4896 | cmp = retval < 0 ? -1 : 1; | |
4897 | } else if (cur1 == cur2) { | |
4898 | cmp = 0; | |
4899 | } else { | |
4900 | cmp = cur1 < cur2 ? -1 : 1; | |
4901 | } | |
4902 | } | |
16660edb | 4903 | |
e01b9e88 SC |
4904 | if (pv1tmp) |
4905 | Safefree(pv1); | |
4906 | if (pv2tmp) | |
4907 | Safefree(pv2); | |
16660edb | 4908 | |
e01b9e88 | 4909 | return cmp; |
bbce6d69 | 4910 | } |
16660edb | 4911 | |
c461cf8f JH |
4912 | /* |
4913 | =for apidoc sv_cmp_locale | |
4914 | ||
4915 | Compares the strings in two SVs in a locale-aware manner. See | |
4916 | L</sv_cmp_locale> | |
4917 | ||
4918 | =cut | |
4919 | */ | |
4920 | ||
bbce6d69 | 4921 | I32 |
864dbfa3 | 4922 | Perl_sv_cmp_locale(pTHX_ register SV *sv1, register SV *sv2) |
bbce6d69 | 4923 | { |
36477c24 | 4924 | #ifdef USE_LOCALE_COLLATE |
16660edb | 4925 | |
bbce6d69 | 4926 | char *pv1, *pv2; |
4927 | STRLEN len1, len2; | |
4928 | I32 retval; | |
16660edb | 4929 | |
3280af22 | 4930 | if (PL_collation_standard) |
bbce6d69 | 4931 | goto raw_compare; |
16660edb | 4932 | |
bbce6d69 | 4933 | len1 = 0; |
8ac85365 | 4934 | pv1 = sv1 ? sv_collxfrm(sv1, &len1) : (char *) NULL; |
bbce6d69 | 4935 | len2 = 0; |
8ac85365 | 4936 | pv2 = sv2 ? sv_collxfrm(sv2, &len2) : (char *) NULL; |
16660edb | 4937 | |
bbce6d69 | 4938 | if (!pv1 || !len1) { |
4939 | if (pv2 && len2) | |
4940 | return -1; | |
4941 | else | |
4942 | goto raw_compare; | |
4943 | } | |
4944 | else { | |
4945 | if (!pv2 || !len2) | |
4946 | return 1; | |
4947 | } | |
16660edb | 4948 | |
bbce6d69 | 4949 | retval = memcmp((void*)pv1, (void*)pv2, len1 < len2 ? len1 : len2); |
16660edb | 4950 | |
bbce6d69 | 4951 | if (retval) |
16660edb | 4952 | return retval < 0 ? -1 : 1; |
4953 | ||
bbce6d69 | 4954 | /* |
4955 | * When the result of collation is equality, that doesn't mean | |
4956 | * that there are no differences -- some locales exclude some | |
4957 | * characters from consideration. So to avoid false equalities, | |
4958 | * we use the raw string as a tiebreaker. | |
4959 | */ | |
16660edb | 4960 | |
bbce6d69 | 4961 | raw_compare: |
4962 | /* FALL THROUGH */ | |
16660edb | 4963 | |
36477c24 | 4964 | #endif /* USE_LOCALE_COLLATE */ |
16660edb | 4965 | |
bbce6d69 | 4966 | return sv_cmp(sv1, sv2); |
4967 | } | |
79072805 | 4968 | |
36477c24 | 4969 | #ifdef USE_LOCALE_COLLATE |
7a4c00b4 | 4970 | /* |
14befaf4 | 4971 | * Any scalar variable may carry PERL_MAGIC_collxfrm magic that contains the |
7a4c00b4 | 4972 | * scalar data of the variable transformed to such a format that |
4973 | * a normal memory comparison can be used to compare the data | |
4974 | * according to the locale settings. | |
4975 | */ | |
bbce6d69 | 4976 | char * |
864dbfa3 | 4977 | Perl_sv_collxfrm(pTHX_ SV *sv, STRLEN *nxp) |
bbce6d69 | 4978 | { |
7a4c00b4 | 4979 | MAGIC *mg; |
16660edb | 4980 | |
14befaf4 | 4981 | mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_collxfrm) : (MAGIC *) NULL; |
3280af22 | 4982 | if (!mg || !mg->mg_ptr || *(U32*)mg->mg_ptr != PL_collation_ix) { |
bbce6d69 | 4983 | char *s, *xf; |
4984 | STRLEN len, xlen; | |
4985 | ||
7a4c00b4 | 4986 | if (mg) |
4987 | Safefree(mg->mg_ptr); | |
bbce6d69 | 4988 | s = SvPV(sv, len); |
4989 | if ((xf = mem_collxfrm(s, len, &xlen))) { | |
ff0cee69 | 4990 | if (SvREADONLY(sv)) { |
4991 | SAVEFREEPV(xf); | |
4992 | *nxp = xlen; | |
3280af22 | 4993 | return xf + sizeof(PL_collation_ix); |
ff0cee69 | 4994 | } |
7a4c00b4 | 4995 | if (! mg) { |
14befaf4 DM |
4996 | sv_magic(sv, 0, PERL_MAGIC_collxfrm, 0, 0); |
4997 | mg = mg_find(sv, PERL_MAGIC_collxfrm); | |
7a4c00b4 | 4998 | assert(mg); |
bbce6d69 | 4999 | } |
7a4c00b4 | 5000 | mg->mg_ptr = xf; |
565764a8 | 5001 | mg->mg_len = xlen; |
7a4c00b4 | 5002 | } |
5003 | else { | |
ff0cee69 | 5004 | if (mg) { |
5005 | mg->mg_ptr = NULL; | |
565764a8 | 5006 | mg->mg_len = -1; |
ff0cee69 | 5007 | } |
bbce6d69 | 5008 | } |
5009 | } | |
7a4c00b4 | 5010 | if (mg && mg->mg_ptr) { |
565764a8 | 5011 | *nxp = mg->mg_len; |
3280af22 | 5012 | return mg->mg_ptr + sizeof(PL_collation_ix); |
bbce6d69 | 5013 | } |
5014 | else { | |
5015 | *nxp = 0; | |
5016 | return NULL; | |
16660edb | 5017 | } |
79072805 LW |
5018 | } |
5019 | ||
36477c24 | 5020 | #endif /* USE_LOCALE_COLLATE */ |
bbce6d69 | 5021 | |
c461cf8f JH |
5022 | /* |
5023 | =for apidoc sv_gets | |
5024 | ||
5025 | Get a line from the filehandle and store it into the SV, optionally | |
5026 | appending to the currently-stored string. | |
5027 | ||
5028 | =cut | |
5029 | */ | |
5030 | ||
79072805 | 5031 | char * |
864dbfa3 | 5032 | Perl_sv_gets(pTHX_ register SV *sv, register PerlIO *fp, I32 append) |
79072805 | 5033 | { |
c07a80fd | 5034 | char *rsptr; |
5035 | STRLEN rslen; | |
5036 | register STDCHAR rslast; | |
5037 | register STDCHAR *bp; | |
5038 | register I32 cnt; | |
5039 | I32 i; | |
5040 | ||
2213622d | 5041 | SV_CHECK_THINKFIRST(sv); |
6fc92669 | 5042 | (void)SvUPGRADE(sv, SVt_PV); |
99491443 | 5043 | |
ff68c719 | 5044 | SvSCREAM_off(sv); |
c07a80fd | 5045 | |
3280af22 | 5046 | if (RsSNARF(PL_rs)) { |
c07a80fd | 5047 | rsptr = NULL; |
5048 | rslen = 0; | |
5049 | } | |
3280af22 | 5050 | else if (RsRECORD(PL_rs)) { |
5b2b9c68 HM |
5051 | I32 recsize, bytesread; |
5052 | char *buffer; | |
5053 | ||
5054 | /* Grab the size of the record we're getting */ | |
3280af22 | 5055 | recsize = SvIV(SvRV(PL_rs)); |
5b2b9c68 | 5056 | (void)SvPOK_only(sv); /* Validate pointer */ |
e670df4e | 5057 | buffer = SvGROW(sv, recsize + 1); |
5b2b9c68 HM |
5058 | /* Go yank in */ |
5059 | #ifdef VMS | |
5060 | /* VMS wants read instead of fread, because fread doesn't respect */ | |
5061 | /* RMS record boundaries. This is not necessarily a good thing to be */ | |
5062 | /* doing, but we've got no other real choice */ | |
5063 | bytesread = PerlLIO_read(PerlIO_fileno(fp), buffer, recsize); | |
5064 | #else | |
5065 | bytesread = PerlIO_read(fp, buffer, recsize); | |
5066 | #endif | |
5067 | SvCUR_set(sv, bytesread); | |
e670df4e | 5068 | buffer[bytesread] = '\0'; |
7d59b7e4 NIS |
5069 | if (PerlIO_isutf8(fp)) |
5070 | SvUTF8_on(sv); | |
5071 | else | |
5072 | SvUTF8_off(sv); | |
5b2b9c68 HM |
5073 | return(SvCUR(sv) ? SvPVX(sv) : Nullch); |
5074 | } | |
3280af22 | 5075 | else if (RsPARA(PL_rs)) { |
c07a80fd | 5076 | rsptr = "\n\n"; |
5077 | rslen = 2; | |
5078 | } | |
7d59b7e4 NIS |
5079 | else { |
5080 | /* Get $/ i.e. PL_rs into same encoding as stream wants */ | |
5081 | if (PerlIO_isutf8(fp)) { | |
5082 | rsptr = SvPVutf8(PL_rs, rslen); | |
5083 | } | |
5084 | else { | |
5085 | if (SvUTF8(PL_rs)) { | |
5086 | if (!sv_utf8_downgrade(PL_rs, TRUE)) { | |
5087 | Perl_croak(aTHX_ "Wide character in $/"); | |
5088 | } | |
5089 | } | |
5090 | rsptr = SvPV(PL_rs, rslen); | |
5091 | } | |
5092 | } | |
5093 | ||
c07a80fd | 5094 | rslast = rslen ? rsptr[rslen - 1] : '\0'; |
5095 | ||
3280af22 | 5096 | if (RsPARA(PL_rs)) { /* have to do this both before and after */ |
79072805 | 5097 | do { /* to make sure file boundaries work right */ |
760ac839 | 5098 | if (PerlIO_eof(fp)) |
a0d0e21e | 5099 | return 0; |
760ac839 | 5100 | i = PerlIO_getc(fp); |
79072805 | 5101 | if (i != '\n') { |
a0d0e21e LW |
5102 | if (i == -1) |
5103 | return 0; | |
760ac839 | 5104 | PerlIO_ungetc(fp,i); |
79072805 LW |
5105 | break; |
5106 | } | |
5107 | } while (i != EOF); | |
5108 | } | |
c07a80fd | 5109 | |
760ac839 LW |
5110 | /* See if we know enough about I/O mechanism to cheat it ! */ |
5111 | ||
5112 | /* This used to be #ifdef test - it is made run-time test for ease | |
1c846c1f | 5113 | of abstracting out stdio interface. One call should be cheap |
760ac839 LW |
5114 | enough here - and may even be a macro allowing compile |
5115 | time optimization. | |
5116 | */ | |
5117 | ||
5118 | if (PerlIO_fast_gets(fp)) { | |
5119 | ||
5120 | /* | |
5121 | * We're going to steal some values from the stdio struct | |
5122 | * and put EVERYTHING in the innermost loop into registers. | |
5123 | */ | |
5124 | register STDCHAR *ptr; | |
5125 | STRLEN bpx; | |
5126 | I32 shortbuffered; | |
5127 | ||
16660edb | 5128 | #if defined(VMS) && defined(PERLIO_IS_STDIO) |
5129 | /* An ungetc()d char is handled separately from the regular | |
5130 | * buffer, so we getc() it back out and stuff it in the buffer. | |
5131 | */ | |
5132 | i = PerlIO_getc(fp); | |
5133 | if (i == EOF) return 0; | |
5134 | *(--((*fp)->_ptr)) = (unsigned char) i; | |
5135 | (*fp)->_cnt++; | |
5136 | #endif | |
c07a80fd | 5137 | |
c2960299 | 5138 | /* Here is some breathtakingly efficient cheating */ |
c07a80fd | 5139 | |
a20bf0c3 | 5140 | cnt = PerlIO_get_cnt(fp); /* get count into register */ |
a0d0e21e | 5141 | (void)SvPOK_only(sv); /* validate pointer */ |
79072805 LW |
5142 | if (SvLEN(sv) - append <= cnt + 1) { /* make sure we have the room */ |
5143 | if (cnt > 80 && SvLEN(sv) > append) { | |
5144 | shortbuffered = cnt - SvLEN(sv) + append + 1; | |
5145 | cnt -= shortbuffered; | |
5146 | } | |
5147 | else { | |
5148 | shortbuffered = 0; | |
bbce6d69 | 5149 | /* remember that cnt can be negative */ |
5150 | SvGROW(sv, append + (cnt <= 0 ? 2 : (cnt + 1))); | |
79072805 LW |
5151 | } |
5152 | } | |
5153 | else | |
5154 | shortbuffered = 0; | |
c07a80fd | 5155 | bp = (STDCHAR*)SvPVX(sv) + append; /* move these two too to registers */ |
a20bf0c3 | 5156 | ptr = (STDCHAR*)PerlIO_get_ptr(fp); |
16660edb | 5157 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5158 | "Screamer: entering, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt)); |
16660edb | 5159 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5160 | "Screamer: entering: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n", |
1c846c1f | 5161 | PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp), |
1d7c1841 | 5162 | PTR2UV(PerlIO_has_base(fp) ? PerlIO_get_base(fp) : 0))); |
79072805 LW |
5163 | for (;;) { |
5164 | screamer: | |
93a17b20 | 5165 | if (cnt > 0) { |
c07a80fd | 5166 | if (rslen) { |
760ac839 LW |
5167 | while (cnt > 0) { /* this | eat */ |
5168 | cnt--; | |
c07a80fd | 5169 | if ((*bp++ = *ptr++) == rslast) /* really | dust */ |
5170 | goto thats_all_folks; /* screams | sed :-) */ | |
5171 | } | |
5172 | } | |
5173 | else { | |
1c846c1f NIS |
5174 | Copy(ptr, bp, cnt, char); /* this | eat */ |
5175 | bp += cnt; /* screams | dust */ | |
c07a80fd | 5176 | ptr += cnt; /* louder | sed :-) */ |
a5f75d66 | 5177 | cnt = 0; |
93a17b20 | 5178 | } |
79072805 LW |
5179 | } |
5180 | ||
748a9306 | 5181 | if (shortbuffered) { /* oh well, must extend */ |
79072805 LW |
5182 | cnt = shortbuffered; |
5183 | shortbuffered = 0; | |
c07a80fd | 5184 | bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */ |
79072805 LW |
5185 | SvCUR_set(sv, bpx); |
5186 | SvGROW(sv, SvLEN(sv) + append + cnt + 2); | |
c07a80fd | 5187 | bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */ |
79072805 LW |
5188 | continue; |
5189 | } | |
5190 | ||
16660edb | 5191 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 GS |
5192 | "Screamer: going to getc, ptr=%"UVuf", cnt=%ld\n", |
5193 | PTR2UV(ptr),(long)cnt)); | |
a20bf0c3 | 5194 | PerlIO_set_ptrcnt(fp, ptr, cnt); /* deregisterize cnt and ptr */ |
16660edb | 5195 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5196 | "Screamer: pre: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n", |
1c846c1f | 5197 | PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp), |
1d7c1841 | 5198 | PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0))); |
1c846c1f | 5199 | /* This used to call 'filbuf' in stdio form, but as that behaves like |
774d564b | 5200 | getc when cnt <= 0 we use PerlIO_getc here to avoid introducing |
5201 | another abstraction. */ | |
760ac839 | 5202 | i = PerlIO_getc(fp); /* get more characters */ |
16660edb | 5203 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5204 | "Screamer: post: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n", |
1c846c1f | 5205 | PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp), |
1d7c1841 | 5206 | PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0))); |
a20bf0c3 JH |
5207 | cnt = PerlIO_get_cnt(fp); |
5208 | ptr = (STDCHAR*)PerlIO_get_ptr(fp); /* reregisterize cnt and ptr */ | |
16660edb | 5209 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5210 | "Screamer: after getc, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt)); |
79072805 | 5211 | |
748a9306 LW |
5212 | if (i == EOF) /* all done for ever? */ |
5213 | goto thats_really_all_folks; | |
5214 | ||
c07a80fd | 5215 | bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */ |
79072805 LW |
5216 | SvCUR_set(sv, bpx); |
5217 | SvGROW(sv, bpx + cnt + 2); | |
c07a80fd | 5218 | bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */ |
5219 | ||
760ac839 | 5220 | *bp++ = i; /* store character from PerlIO_getc */ |
79072805 | 5221 | |
c07a80fd | 5222 | if (rslen && (STDCHAR)i == rslast) /* all done for now? */ |
79072805 | 5223 | goto thats_all_folks; |
79072805 LW |
5224 | } |
5225 | ||
5226 | thats_all_folks: | |
c07a80fd | 5227 | if ((rslen > 1 && (bp - (STDCHAR*)SvPVX(sv) < rslen)) || |
36477c24 | 5228 | memNE((char*)bp - rslen, rsptr, rslen)) |
760ac839 | 5229 | goto screamer; /* go back to the fray */ |
79072805 LW |
5230 | thats_really_all_folks: |
5231 | if (shortbuffered) | |
5232 | cnt += shortbuffered; | |
16660edb | 5233 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5234 | "Screamer: quitting, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt)); |
a20bf0c3 | 5235 | PerlIO_set_ptrcnt(fp, ptr, cnt); /* put these back or we're in trouble */ |
16660edb | 5236 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5237 | "Screamer: end: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n", |
1c846c1f | 5238 | PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp), |
1d7c1841 | 5239 | PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0))); |
79072805 | 5240 | *bp = '\0'; |
760ac839 | 5241 | SvCUR_set(sv, bp - (STDCHAR*)SvPVX(sv)); /* set length */ |
16660edb | 5242 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
fb73857a | 5243 | "Screamer: done, len=%ld, string=|%.*s|\n", |
5244 | (long)SvCUR(sv),(int)SvCUR(sv),SvPVX(sv))); | |
760ac839 LW |
5245 | } |
5246 | else | |
79072805 | 5247 | { |
4d2c4e07 | 5248 | #ifndef EPOC |
760ac839 | 5249 | /*The big, slow, and stupid way */ |
c07a80fd | 5250 | STDCHAR buf[8192]; |
4d2c4e07 OF |
5251 | #else |
5252 | /* Need to work around EPOC SDK features */ | |
5253 | /* On WINS: MS VC5 generates calls to _chkstk, */ | |
5254 | /* if a `large' stack frame is allocated */ | |
5255 | /* gcc on MARM does not generate calls like these */ | |
5256 | STDCHAR buf[1024]; | |
5257 | #endif | |
79072805 | 5258 | |
760ac839 | 5259 | screamer2: |
c07a80fd | 5260 | if (rslen) { |
760ac839 LW |
5261 | register STDCHAR *bpe = buf + sizeof(buf); |
5262 | bp = buf; | |
5263 | while ((i = PerlIO_getc(fp)) != EOF && (*bp++ = i) != rslast && bp < bpe) | |
5264 | ; /* keep reading */ | |
5265 | cnt = bp - buf; | |
c07a80fd | 5266 | } |
5267 | else { | |
760ac839 | 5268 | cnt = PerlIO_read(fp,(char*)buf, sizeof(buf)); |
16660edb | 5269 | /* Accomodate broken VAXC compiler, which applies U8 cast to |
5270 | * both args of ?: operator, causing EOF to change into 255 | |
5271 | */ | |
5272 | if (cnt) { i = (U8)buf[cnt - 1]; } else { i = EOF; } | |
c07a80fd | 5273 | } |
79072805 LW |
5274 | |
5275 | if (append) | |
760ac839 | 5276 | sv_catpvn(sv, (char *) buf, cnt); |
79072805 | 5277 | else |
760ac839 | 5278 | sv_setpvn(sv, (char *) buf, cnt); |
c07a80fd | 5279 | |
5280 | if (i != EOF && /* joy */ | |
5281 | (!rslen || | |
5282 | SvCUR(sv) < rslen || | |
36477c24 | 5283 | memNE(SvPVX(sv) + SvCUR(sv) - rslen, rsptr, rslen))) |
79072805 LW |
5284 | { |
5285 | append = -1; | |
63e4d877 CS |
5286 | /* |
5287 | * If we're reading from a TTY and we get a short read, | |
5288 | * indicating that the user hit his EOF character, we need | |
5289 | * to notice it now, because if we try to read from the TTY | |
5290 | * again, the EOF condition will disappear. | |
5291 | * | |
5292 | * The comparison of cnt to sizeof(buf) is an optimization | |
5293 | * that prevents unnecessary calls to feof(). | |
5294 | * | |
5295 | * - jik 9/25/96 | |
5296 | */ | |
5297 | if (!(cnt < sizeof(buf) && PerlIO_eof(fp))) | |
5298 | goto screamer2; | |
79072805 LW |
5299 | } |
5300 | } | |
5301 | ||
1c846c1f | 5302 | if (RsPARA(PL_rs)) { /* have to do this both before and after */ |
c07a80fd | 5303 | while (i != EOF) { /* to make sure file boundaries work right */ |
760ac839 | 5304 | i = PerlIO_getc(fp); |
79072805 | 5305 | if (i != '\n') { |
760ac839 | 5306 | PerlIO_ungetc(fp,i); |
79072805 LW |
5307 | break; |
5308 | } | |
5309 | } | |
5310 | } | |
c07a80fd | 5311 | |
7d59b7e4 NIS |
5312 | if (PerlIO_isutf8(fp)) |
5313 | SvUTF8_on(sv); | |
5314 | else | |
5315 | SvUTF8_off(sv); | |
5316 | ||
c07a80fd | 5317 | return (SvCUR(sv) - append) ? SvPVX(sv) : Nullch; |
79072805 LW |
5318 | } |
5319 | ||
760ac839 | 5320 | |
954c1994 GS |
5321 | /* |
5322 | =for apidoc sv_inc | |
5323 | ||
5324 | Auto-increment of the value in the SV. | |
5325 | ||
5326 | =cut | |
5327 | */ | |
5328 | ||
79072805 | 5329 | void |
864dbfa3 | 5330 | Perl_sv_inc(pTHX_ register SV *sv) |
79072805 LW |
5331 | { |
5332 | register char *d; | |
463ee0b2 | 5333 | int flags; |
79072805 LW |
5334 | |
5335 | if (!sv) | |
5336 | return; | |
b23a5f78 GB |
5337 | if (SvGMAGICAL(sv)) |
5338 | mg_get(sv); | |
ed6116ce | 5339 | if (SvTHINKFIRST(sv)) { |
0f15f207 | 5340 | if (SvREADONLY(sv)) { |
3280af22 | 5341 | if (PL_curcop != &PL_compiling) |
cea2e8a9 | 5342 | Perl_croak(aTHX_ PL_no_modify); |
0f15f207 | 5343 | } |
a0d0e21e | 5344 | if (SvROK(sv)) { |
b5be31e9 | 5345 | IV i; |
9e7bc3e8 JD |
5346 | if (SvAMAGIC(sv) && AMG_CALLun(sv,inc)) |
5347 | return; | |
56431972 | 5348 | i = PTR2IV(SvRV(sv)); |
b5be31e9 SM |
5349 | sv_unref(sv); |
5350 | sv_setiv(sv, i); | |
a0d0e21e | 5351 | } |
ed6116ce | 5352 | } |
8990e307 | 5353 | flags = SvFLAGS(sv); |
28e5dec8 JH |
5354 | if ((flags & (SVp_NOK|SVp_IOK)) == SVp_NOK) { |
5355 | /* It's (privately or publicly) a float, but not tested as an | |
5356 | integer, so test it to see. */ | |
d460ef45 | 5357 | (void) SvIV(sv); |
28e5dec8 JH |
5358 | flags = SvFLAGS(sv); |
5359 | } | |
5360 | if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) { | |
5361 | /* It's publicly an integer, or privately an integer-not-float */ | |
5362 | oops_its_int: | |
25da4f38 IZ |
5363 | if (SvIsUV(sv)) { |
5364 | if (SvUVX(sv) == UV_MAX) | |
65202027 | 5365 | sv_setnv(sv, (NV)UV_MAX + 1.0); |
25da4f38 IZ |
5366 | else |
5367 | (void)SvIOK_only_UV(sv); | |
5368 | ++SvUVX(sv); | |
5369 | } else { | |
5370 | if (SvIVX(sv) == IV_MAX) | |
28e5dec8 | 5371 | sv_setuv(sv, (UV)IV_MAX + 1); |
25da4f38 IZ |
5372 | else { |
5373 | (void)SvIOK_only(sv); | |
5374 | ++SvIVX(sv); | |
1c846c1f | 5375 | } |
55497cff | 5376 | } |
79072805 LW |
5377 | return; |
5378 | } | |
28e5dec8 JH |
5379 | if (flags & SVp_NOK) { |
5380 | (void)SvNOK_only(sv); | |
5381 | SvNVX(sv) += 1.0; | |
5382 | return; | |
5383 | } | |
5384 | ||
8990e307 | 5385 | if (!(flags & SVp_POK) || !*SvPVX(sv)) { |
28e5dec8 JH |
5386 | if ((flags & SVTYPEMASK) < SVt_PVIV) |
5387 | sv_upgrade(sv, SVt_IV); | |
5388 | (void)SvIOK_only(sv); | |
5389 | SvIVX(sv) = 1; | |
79072805 LW |
5390 | return; |
5391 | } | |
463ee0b2 | 5392 | d = SvPVX(sv); |
79072805 LW |
5393 | while (isALPHA(*d)) d++; |
5394 | while (isDIGIT(*d)) d++; | |
5395 | if (*d) { | |
28e5dec8 JH |
5396 | #ifdef PERL_PRESERVE_IVUV |
5397 | /* Got to punt this an an integer if needs be, but we don't issue | |
5398 | warnings. Probably ought to make the sv_iv_please() that does | |
5399 | the conversion if possible, and silently. */ | |
5400 | I32 numtype = looks_like_number(sv); | |
5401 | if (numtype && !(numtype & IS_NUMBER_INFINITY)) { | |
5402 | /* Need to try really hard to see if it's an integer. | |
5403 | 9.22337203685478e+18 is an integer. | |
5404 | but "9.22337203685478e+18" + 0 is UV=9223372036854779904 | |
5405 | so $a="9.22337203685478e+18"; $a+0; $a++ | |
5406 | needs to be the same as $a="9.22337203685478e+18"; $a++ | |
5407 | or we go insane. */ | |
d460ef45 | 5408 | |
28e5dec8 JH |
5409 | (void) sv_2iv(sv); |
5410 | if (SvIOK(sv)) | |
5411 | goto oops_its_int; | |
5412 | ||
5413 | /* sv_2iv *should* have made this an NV */ | |
5414 | if (flags & SVp_NOK) { | |
5415 | (void)SvNOK_only(sv); | |
5416 | SvNVX(sv) += 1.0; | |
5417 | return; | |
5418 | } | |
5419 | /* I don't think we can get here. Maybe I should assert this | |
5420 | And if we do get here I suspect that sv_setnv will croak. NWC | |
5421 | Fall through. */ | |
5422 | #if defined(USE_LONG_DOUBLE) | |
5423 | 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", | |
5424 | SvPVX(sv), SvIVX(sv), SvNVX(sv))); | |
5425 | #else | |
5426 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%g\n", | |
5427 | SvPVX(sv), SvIVX(sv), SvNVX(sv))); | |
5428 | #endif | |
5429 | } | |
5430 | #endif /* PERL_PRESERVE_IVUV */ | |
5431 | sv_setnv(sv,Atof(SvPVX(sv)) + 1.0); | |
79072805 LW |
5432 | return; |
5433 | } | |
5434 | d--; | |
463ee0b2 | 5435 | while (d >= SvPVX(sv)) { |
79072805 LW |
5436 | if (isDIGIT(*d)) { |
5437 | if (++*d <= '9') | |
5438 | return; | |
5439 | *(d--) = '0'; | |
5440 | } | |
5441 | else { | |
9d116dd7 JH |
5442 | #ifdef EBCDIC |
5443 | /* MKS: The original code here died if letters weren't consecutive. | |
5444 | * at least it didn't have to worry about non-C locales. The | |
5445 | * new code assumes that ('z'-'a')==('Z'-'A'), letters are | |
1c846c1f | 5446 | * arranged in order (although not consecutively) and that only |
9d116dd7 JH |
5447 | * [A-Za-z] are accepted by isALPHA in the C locale. |
5448 | */ | |
5449 | if (*d != 'z' && *d != 'Z') { | |
5450 | do { ++*d; } while (!isALPHA(*d)); | |
5451 | return; | |
5452 | } | |
5453 | *(d--) -= 'z' - 'a'; | |
5454 | #else | |
79072805 LW |
5455 | ++*d; |
5456 | if (isALPHA(*d)) | |
5457 | return; | |
5458 | *(d--) -= 'z' - 'a' + 1; | |
9d116dd7 | 5459 | #endif |
79072805 LW |
5460 | } |
5461 | } | |
5462 | /* oh,oh, the number grew */ | |
5463 | SvGROW(sv, SvCUR(sv) + 2); | |
5464 | SvCUR(sv)++; | |
463ee0b2 | 5465 | for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX(sv); d--) |
79072805 LW |
5466 | *d = d[-1]; |
5467 | if (isDIGIT(d[1])) | |
5468 | *d = '1'; | |
5469 | else | |
5470 | *d = d[1]; | |
5471 | } | |
5472 | ||
954c1994 GS |
5473 | /* |
5474 | =for apidoc sv_dec | |
5475 | ||
5476 | Auto-decrement of the value in the SV. | |
5477 | ||
5478 | =cut | |
5479 | */ | |
5480 | ||
79072805 | 5481 | void |
864dbfa3 | 5482 | Perl_sv_dec(pTHX_ register SV *sv) |
79072805 | 5483 | { |
463ee0b2 LW |
5484 | int flags; |
5485 | ||
79072805 LW |
5486 | if (!sv) |
5487 | return; | |
b23a5f78 GB |
5488 | if (SvGMAGICAL(sv)) |
5489 | mg_get(sv); | |
ed6116ce | 5490 | if (SvTHINKFIRST(sv)) { |
0f15f207 | 5491 | if (SvREADONLY(sv)) { |
3280af22 | 5492 | if (PL_curcop != &PL_compiling) |
cea2e8a9 | 5493 | Perl_croak(aTHX_ PL_no_modify); |
0f15f207 | 5494 | } |
a0d0e21e | 5495 | if (SvROK(sv)) { |
b5be31e9 | 5496 | IV i; |
9e7bc3e8 JD |
5497 | if (SvAMAGIC(sv) && AMG_CALLun(sv,dec)) |
5498 | return; | |
56431972 | 5499 | i = PTR2IV(SvRV(sv)); |
b5be31e9 SM |
5500 | sv_unref(sv); |
5501 | sv_setiv(sv, i); | |
a0d0e21e | 5502 | } |
ed6116ce | 5503 | } |
28e5dec8 JH |
5504 | /* Unlike sv_inc we don't have to worry about string-never-numbers |
5505 | and keeping them magic. But we mustn't warn on punting */ | |
8990e307 | 5506 | flags = SvFLAGS(sv); |
28e5dec8 JH |
5507 | if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) { |
5508 | /* It's publicly an integer, or privately an integer-not-float */ | |
5509 | oops_its_int: | |
25da4f38 IZ |
5510 | if (SvIsUV(sv)) { |
5511 | if (SvUVX(sv) == 0) { | |
5512 | (void)SvIOK_only(sv); | |
5513 | SvIVX(sv) = -1; | |
5514 | } | |
5515 | else { | |
5516 | (void)SvIOK_only_UV(sv); | |
5517 | --SvUVX(sv); | |
1c846c1f | 5518 | } |
25da4f38 IZ |
5519 | } else { |
5520 | if (SvIVX(sv) == IV_MIN) | |
65202027 | 5521 | sv_setnv(sv, (NV)IV_MIN - 1.0); |
25da4f38 IZ |
5522 | else { |
5523 | (void)SvIOK_only(sv); | |
5524 | --SvIVX(sv); | |
1c846c1f | 5525 | } |
55497cff | 5526 | } |
5527 | return; | |
5528 | } | |
28e5dec8 JH |
5529 | if (flags & SVp_NOK) { |
5530 | SvNVX(sv) -= 1.0; | |
5531 | (void)SvNOK_only(sv); | |
5532 | return; | |
5533 | } | |
8990e307 | 5534 | if (!(flags & SVp_POK)) { |
4633a7c4 LW |
5535 | if ((flags & SVTYPEMASK) < SVt_PVNV) |
5536 | sv_upgrade(sv, SVt_NV); | |
463ee0b2 | 5537 | SvNVX(sv) = -1.0; |
a0d0e21e | 5538 | (void)SvNOK_only(sv); |
79072805 LW |
5539 | return; |
5540 | } | |
28e5dec8 JH |
5541 | #ifdef PERL_PRESERVE_IVUV |
5542 | { | |
5543 | I32 numtype = looks_like_number(sv); | |
5544 | if (numtype && !(numtype & IS_NUMBER_INFINITY)) { | |
5545 | /* Need to try really hard to see if it's an integer. | |
5546 | 9.22337203685478e+18 is an integer. | |
5547 | but "9.22337203685478e+18" + 0 is UV=9223372036854779904 | |
5548 | so $a="9.22337203685478e+18"; $a+0; $a-- | |
5549 | needs to be the same as $a="9.22337203685478e+18"; $a-- | |
5550 | or we go insane. */ | |
d460ef45 | 5551 | |
28e5dec8 JH |
5552 | (void) sv_2iv(sv); |
5553 | if (SvIOK(sv)) | |
5554 | goto oops_its_int; | |
5555 | ||
5556 | /* sv_2iv *should* have made this an NV */ | |
5557 | if (flags & SVp_NOK) { | |
5558 | (void)SvNOK_only(sv); | |
5559 | SvNVX(sv) -= 1.0; | |
5560 | return; | |
5561 | } | |
5562 | /* I don't think we can get here. Maybe I should assert this | |
5563 | And if we do get here I suspect that sv_setnv will croak. NWC | |
5564 | Fall through. */ | |
5565 | #if defined(USE_LONG_DOUBLE) | |
5566 | 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", | |
5567 | SvPVX(sv), SvIVX(sv), SvNVX(sv))); | |
5568 | #else | |
5569 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%g\n", | |
5570 | SvPVX(sv), SvIVX(sv), SvNVX(sv))); | |
5571 | #endif | |
5572 | } | |
5573 | } | |
5574 | #endif /* PERL_PRESERVE_IVUV */ | |
097ee67d | 5575 | sv_setnv(sv,Atof(SvPVX(sv)) - 1.0); /* punt */ |
79072805 LW |
5576 | } |
5577 | ||
954c1994 GS |
5578 | /* |
5579 | =for apidoc sv_mortalcopy | |
5580 | ||
5581 | Creates a new SV which is a copy of the original SV. The new SV is marked | |
5582 | as mortal. | |
5583 | ||
5584 | =cut | |
5585 | */ | |
5586 | ||
79072805 LW |
5587 | /* Make a string that will exist for the duration of the expression |
5588 | * evaluation. Actually, it may have to last longer than that, but | |
5589 | * hopefully we won't free it until it has been assigned to a | |
5590 | * permanent location. */ | |
5591 | ||
5592 | SV * | |
864dbfa3 | 5593 | Perl_sv_mortalcopy(pTHX_ SV *oldstr) |
79072805 | 5594 | { |
463ee0b2 | 5595 | register SV *sv; |
79072805 | 5596 | |
4561caa4 | 5597 | new_SV(sv); |
79072805 | 5598 | sv_setsv(sv,oldstr); |
677b06e3 GS |
5599 | EXTEND_MORTAL(1); |
5600 | PL_tmps_stack[++PL_tmps_ix] = sv; | |
8990e307 LW |
5601 | SvTEMP_on(sv); |
5602 | return sv; | |
5603 | } | |
5604 | ||
954c1994 GS |
5605 | /* |
5606 | =for apidoc sv_newmortal | |
5607 | ||
5608 | Creates a new SV which is mortal. The reference count of the SV is set to 1. | |
5609 | ||
5610 | =cut | |
5611 | */ | |
5612 | ||
8990e307 | 5613 | SV * |
864dbfa3 | 5614 | Perl_sv_newmortal(pTHX) |
8990e307 LW |
5615 | { |
5616 | register SV *sv; | |
5617 | ||
4561caa4 | 5618 | new_SV(sv); |
8990e307 | 5619 | SvFLAGS(sv) = SVs_TEMP; |
677b06e3 GS |
5620 | EXTEND_MORTAL(1); |
5621 | PL_tmps_stack[++PL_tmps_ix] = sv; | |
79072805 LW |
5622 | return sv; |
5623 | } | |
5624 | ||
954c1994 GS |
5625 | /* |
5626 | =for apidoc sv_2mortal | |
5627 | ||
5628 | Marks an SV as mortal. The SV will be destroyed when the current context | |
5629 | ends. | |
5630 | ||
5631 | =cut | |
5632 | */ | |
5633 | ||
79072805 LW |
5634 | /* same thing without the copying */ |
5635 | ||
5636 | SV * | |
864dbfa3 | 5637 | Perl_sv_2mortal(pTHX_ register SV *sv) |
79072805 LW |
5638 | { |
5639 | if (!sv) | |
5640 | return sv; | |
d689ffdd | 5641 | if (SvREADONLY(sv) && SvIMMORTAL(sv)) |
11162842 | 5642 | return sv; |
677b06e3 GS |
5643 | EXTEND_MORTAL(1); |
5644 | PL_tmps_stack[++PL_tmps_ix] = sv; | |
8990e307 | 5645 | SvTEMP_on(sv); |
79072805 LW |
5646 | return sv; |
5647 | } | |
5648 | ||
954c1994 GS |
5649 | /* |
5650 | =for apidoc newSVpv | |
5651 | ||
5652 | Creates a new SV and copies a string into it. The reference count for the | |
5653 | SV is set to 1. If C<len> is zero, Perl will compute the length using | |
5654 | strlen(). For efficiency, consider using C<newSVpvn> instead. | |
5655 | ||
5656 | =cut | |
5657 | */ | |
5658 | ||
79072805 | 5659 | SV * |
864dbfa3 | 5660 | Perl_newSVpv(pTHX_ const char *s, STRLEN len) |
79072805 | 5661 | { |
463ee0b2 | 5662 | register SV *sv; |
79072805 | 5663 | |
4561caa4 | 5664 | new_SV(sv); |
79072805 LW |
5665 | if (!len) |
5666 | len = strlen(s); | |
5667 | sv_setpvn(sv,s,len); | |
5668 | return sv; | |
5669 | } | |
5670 | ||
954c1994 GS |
5671 | /* |
5672 | =for apidoc newSVpvn | |
5673 | ||
5674 | Creates a new SV and copies a string into it. The reference count for the | |
1c846c1f | 5675 | SV is set to 1. Note that if C<len> is zero, Perl will create a zero length |
954c1994 GS |
5676 | string. You are responsible for ensuring that the source string is at least |
5677 | C<len> bytes long. | |
5678 | ||
5679 | =cut | |
5680 | */ | |
5681 | ||
9da1e3b5 | 5682 | SV * |
864dbfa3 | 5683 | Perl_newSVpvn(pTHX_ const char *s, STRLEN len) |
9da1e3b5 MUN |
5684 | { |
5685 | register SV *sv; | |
5686 | ||
5687 | new_SV(sv); | |
9da1e3b5 MUN |
5688 | sv_setpvn(sv,s,len); |
5689 | return sv; | |
5690 | } | |
5691 | ||
1c846c1f NIS |
5692 | /* |
5693 | =for apidoc newSVpvn_share | |
5694 | ||
5695 | Creates a new SV and populates it with a string from | |
5696 | the string table. Turns on READONLY and FAKE. | |
5697 | The idea here is that as string table is used for shared hash | |
5698 | keys these strings will have SvPVX == HeKEY and hash lookup | |
5699 | will avoid string compare. | |
5700 | ||
5701 | =cut | |
5702 | */ | |
5703 | ||
5704 | SV * | |
c3654f1a | 5705 | Perl_newSVpvn_share(pTHX_ const char *src, I32 len, U32 hash) |
1c846c1f NIS |
5706 | { |
5707 | register SV *sv; | |
c3654f1a IH |
5708 | bool is_utf8 = FALSE; |
5709 | if (len < 0) { | |
5710 | len = -len; | |
5711 | is_utf8 = TRUE; | |
5712 | } | |
75a54232 JH |
5713 | if (is_utf8 && !(PL_hints & HINT_UTF8_DISTINCT)) { |
5714 | STRLEN tmplen = len; | |
5715 | /* See the note in hv.c:hv_fetch() --jhi */ | |
5716 | src = (char*)bytes_from_utf8((U8*)src, &tmplen, &is_utf8); | |
5717 | len = tmplen; | |
5718 | } | |
1c846c1f NIS |
5719 | if (!hash) |
5720 | PERL_HASH(hash, src, len); | |
5721 | new_SV(sv); | |
5722 | sv_upgrade(sv, SVt_PVIV); | |
c3654f1a | 5723 | SvPVX(sv) = sharepvn(src, is_utf8?-len:len, hash); |
1c846c1f NIS |
5724 | SvCUR(sv) = len; |
5725 | SvUVX(sv) = hash; | |
5726 | SvLEN(sv) = 0; | |
5727 | SvREADONLY_on(sv); | |
5728 | SvFAKE_on(sv); | |
5729 | SvPOK_on(sv); | |
c3654f1a IH |
5730 | if (is_utf8) |
5731 | SvUTF8_on(sv); | |
1c846c1f NIS |
5732 | return sv; |
5733 | } | |
5734 | ||
cea2e8a9 | 5735 | #if defined(PERL_IMPLICIT_CONTEXT) |
46fc3d4c | 5736 | SV * |
cea2e8a9 | 5737 | Perl_newSVpvf_nocontext(const char* pat, ...) |
46fc3d4c | 5738 | { |
cea2e8a9 | 5739 | dTHX; |
46fc3d4c | 5740 | register SV *sv; |
5741 | va_list args; | |
46fc3d4c | 5742 | va_start(args, pat); |
c5be433b | 5743 | sv = vnewSVpvf(pat, &args); |
46fc3d4c | 5744 | va_end(args); |
5745 | return sv; | |
5746 | } | |
cea2e8a9 | 5747 | #endif |
46fc3d4c | 5748 | |
954c1994 GS |
5749 | /* |
5750 | =for apidoc newSVpvf | |
5751 | ||
5752 | Creates a new SV an initialize it with the string formatted like | |
5753 | C<sprintf>. | |
5754 | ||
5755 | =cut | |
5756 | */ | |
5757 | ||
cea2e8a9 GS |
5758 | SV * |
5759 | Perl_newSVpvf(pTHX_ const char* pat, ...) | |
5760 | { | |
5761 | register SV *sv; | |
5762 | va_list args; | |
cea2e8a9 | 5763 | va_start(args, pat); |
c5be433b | 5764 | sv = vnewSVpvf(pat, &args); |
cea2e8a9 GS |
5765 | va_end(args); |
5766 | return sv; | |
5767 | } | |
46fc3d4c | 5768 | |
79072805 | 5769 | SV * |
c5be433b GS |
5770 | Perl_vnewSVpvf(pTHX_ const char* pat, va_list* args) |
5771 | { | |
5772 | register SV *sv; | |
5773 | new_SV(sv); | |
5774 | sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
5775 | return sv; | |
5776 | } | |
5777 | ||
954c1994 GS |
5778 | /* |
5779 | =for apidoc newSVnv | |
5780 | ||
5781 | Creates a new SV and copies a floating point value into it. | |
5782 | The reference count for the SV is set to 1. | |
5783 | ||
5784 | =cut | |
5785 | */ | |
5786 | ||
c5be433b | 5787 | SV * |
65202027 | 5788 | Perl_newSVnv(pTHX_ NV n) |
79072805 | 5789 | { |
463ee0b2 | 5790 | register SV *sv; |
79072805 | 5791 | |
4561caa4 | 5792 | new_SV(sv); |
79072805 LW |
5793 | sv_setnv(sv,n); |
5794 | return sv; | |
5795 | } | |
5796 | ||
954c1994 GS |
5797 | /* |
5798 | =for apidoc newSViv | |
5799 | ||
5800 | Creates a new SV and copies an integer into it. The reference count for the | |
5801 | SV is set to 1. | |
5802 | ||
5803 | =cut | |
5804 | */ | |
5805 | ||
79072805 | 5806 | SV * |
864dbfa3 | 5807 | Perl_newSViv(pTHX_ IV i) |
79072805 | 5808 | { |
463ee0b2 | 5809 | register SV *sv; |
79072805 | 5810 | |
4561caa4 | 5811 | new_SV(sv); |
79072805 LW |
5812 | sv_setiv(sv,i); |
5813 | return sv; | |
5814 | } | |
5815 | ||
954c1994 | 5816 | /* |
1a3327fb JH |
5817 | =for apidoc newSVuv |
5818 | ||
5819 | Creates a new SV and copies an unsigned integer into it. | |
5820 | The reference count for the SV is set to 1. | |
5821 | ||
5822 | =cut | |
5823 | */ | |
5824 | ||
5825 | SV * | |
5826 | Perl_newSVuv(pTHX_ UV u) | |
5827 | { | |
5828 | register SV *sv; | |
5829 | ||
5830 | new_SV(sv); | |
5831 | sv_setuv(sv,u); | |
5832 | return sv; | |
5833 | } | |
5834 | ||
5835 | /* | |
954c1994 GS |
5836 | =for apidoc newRV_noinc |
5837 | ||
5838 | Creates an RV wrapper for an SV. The reference count for the original | |
5839 | SV is B<not> incremented. | |
5840 | ||
5841 | =cut | |
5842 | */ | |
5843 | ||
2304df62 | 5844 | SV * |
864dbfa3 | 5845 | Perl_newRV_noinc(pTHX_ SV *tmpRef) |
2304df62 AD |
5846 | { |
5847 | register SV *sv; | |
5848 | ||
4561caa4 | 5849 | new_SV(sv); |
2304df62 | 5850 | sv_upgrade(sv, SVt_RV); |
76e3520e | 5851 | SvTEMP_off(tmpRef); |
d689ffdd | 5852 | SvRV(sv) = tmpRef; |
2304df62 | 5853 | SvROK_on(sv); |
2304df62 AD |
5854 | return sv; |
5855 | } | |
5856 | ||
954c1994 | 5857 | /* newRV_inc is #defined to newRV in sv.h */ |
5f05dabc | 5858 | SV * |
864dbfa3 | 5859 | Perl_newRV(pTHX_ SV *tmpRef) |
5f05dabc | 5860 | { |
5f6447b6 | 5861 | return newRV_noinc(SvREFCNT_inc(tmpRef)); |
5f05dabc | 5862 | } |
5f05dabc | 5863 | |
954c1994 GS |
5864 | /* |
5865 | =for apidoc newSVsv | |
5866 | ||
5867 | Creates a new SV which is an exact duplicate of the original SV. | |
5868 | ||
5869 | =cut | |
5870 | */ | |
5871 | ||
79072805 LW |
5872 | /* make an exact duplicate of old */ |
5873 | ||
5874 | SV * | |
864dbfa3 | 5875 | Perl_newSVsv(pTHX_ register SV *old) |
79072805 | 5876 | { |
463ee0b2 | 5877 | register SV *sv; |
79072805 LW |
5878 | |
5879 | if (!old) | |
5880 | return Nullsv; | |
8990e307 | 5881 | if (SvTYPE(old) == SVTYPEMASK) { |
0453d815 PM |
5882 | if (ckWARN_d(WARN_INTERNAL)) |
5883 | Perl_warner(aTHX_ WARN_INTERNAL, "semi-panic: attempt to dup freed string"); | |
79072805 LW |
5884 | return Nullsv; |
5885 | } | |
4561caa4 | 5886 | new_SV(sv); |
ff68c719 | 5887 | if (SvTEMP(old)) { |
5888 | SvTEMP_off(old); | |
463ee0b2 | 5889 | sv_setsv(sv,old); |
ff68c719 | 5890 | SvTEMP_on(old); |
79072805 LW |
5891 | } |
5892 | else | |
463ee0b2 LW |
5893 | sv_setsv(sv,old); |
5894 | return sv; | |
79072805 LW |
5895 | } |
5896 | ||
5897 | void | |
864dbfa3 | 5898 | Perl_sv_reset(pTHX_ register char *s, HV *stash) |
79072805 LW |
5899 | { |
5900 | register HE *entry; | |
5901 | register GV *gv; | |
5902 | register SV *sv; | |
5903 | register I32 i; | |
5904 | register PMOP *pm; | |
5905 | register I32 max; | |
4802d5d7 | 5906 | char todo[PERL_UCHAR_MAX+1]; |
79072805 | 5907 | |
49d8d3a1 MB |
5908 | if (!stash) |
5909 | return; | |
5910 | ||
79072805 LW |
5911 | if (!*s) { /* reset ?? searches */ |
5912 | for (pm = HvPMROOT(stash); pm; pm = pm->op_pmnext) { | |
48c036b1 | 5913 | pm->op_pmdynflags &= ~PMdf_USED; |
79072805 LW |
5914 | } |
5915 | return; | |
5916 | } | |
5917 | ||
5918 | /* reset variables */ | |
5919 | ||
5920 | if (!HvARRAY(stash)) | |
5921 | return; | |
463ee0b2 LW |
5922 | |
5923 | Zero(todo, 256, char); | |
79072805 | 5924 | while (*s) { |
4802d5d7 | 5925 | i = (unsigned char)*s; |
79072805 LW |
5926 | if (s[1] == '-') { |
5927 | s += 2; | |
5928 | } | |
4802d5d7 | 5929 | max = (unsigned char)*s++; |
79072805 | 5930 | for ( ; i <= max; i++) { |
463ee0b2 LW |
5931 | todo[i] = 1; |
5932 | } | |
a0d0e21e | 5933 | for (i = 0; i <= (I32) HvMAX(stash); i++) { |
79072805 | 5934 | for (entry = HvARRAY(stash)[i]; |
9e35f4b3 GS |
5935 | entry; |
5936 | entry = HeNEXT(entry)) | |
5937 | { | |
1edc1566 | 5938 | if (!todo[(U8)*HeKEY(entry)]) |
463ee0b2 | 5939 | continue; |
1edc1566 | 5940 | gv = (GV*)HeVAL(entry); |
79072805 | 5941 | sv = GvSV(gv); |
9e35f4b3 GS |
5942 | if (SvTHINKFIRST(sv)) { |
5943 | if (!SvREADONLY(sv) && SvROK(sv)) | |
5944 | sv_unref(sv); | |
5945 | continue; | |
5946 | } | |
a0d0e21e | 5947 | (void)SvOK_off(sv); |
79072805 LW |
5948 | if (SvTYPE(sv) >= SVt_PV) { |
5949 | SvCUR_set(sv, 0); | |
463ee0b2 LW |
5950 | if (SvPVX(sv) != Nullch) |
5951 | *SvPVX(sv) = '\0'; | |
44a8e56a | 5952 | SvTAINT(sv); |
79072805 LW |
5953 | } |
5954 | if (GvAV(gv)) { | |
5955 | av_clear(GvAV(gv)); | |
5956 | } | |
44a8e56a | 5957 | if (GvHV(gv) && !HvNAME(GvHV(gv))) { |
463ee0b2 | 5958 | hv_clear(GvHV(gv)); |
fa6a1c44 | 5959 | #ifdef USE_ENVIRON_ARRAY |
3280af22 | 5960 | if (gv == PL_envgv) |
79072805 | 5961 | environ[0] = Nullch; |
a0d0e21e | 5962 | #endif |
79072805 LW |
5963 | } |
5964 | } | |
5965 | } | |
5966 | } | |
5967 | } | |
5968 | ||
46fc3d4c | 5969 | IO* |
864dbfa3 | 5970 | Perl_sv_2io(pTHX_ SV *sv) |
46fc3d4c | 5971 | { |
5972 | IO* io; | |
5973 | GV* gv; | |
2d8e6c8d | 5974 | STRLEN n_a; |
46fc3d4c | 5975 | |
5976 | switch (SvTYPE(sv)) { | |
5977 | case SVt_PVIO: | |
5978 | io = (IO*)sv; | |
5979 | break; | |
5980 | case SVt_PVGV: | |
5981 | gv = (GV*)sv; | |
5982 | io = GvIO(gv); | |
5983 | if (!io) | |
cea2e8a9 | 5984 | Perl_croak(aTHX_ "Bad filehandle: %s", GvNAME(gv)); |
46fc3d4c | 5985 | break; |
5986 | default: | |
5987 | if (!SvOK(sv)) | |
cea2e8a9 | 5988 | Perl_croak(aTHX_ PL_no_usym, "filehandle"); |
46fc3d4c | 5989 | if (SvROK(sv)) |
5990 | return sv_2io(SvRV(sv)); | |
2d8e6c8d | 5991 | gv = gv_fetchpv(SvPV(sv,n_a), FALSE, SVt_PVIO); |
46fc3d4c | 5992 | if (gv) |
5993 | io = GvIO(gv); | |
5994 | else | |
5995 | io = 0; | |
5996 | if (!io) | |
cea2e8a9 | 5997 | Perl_croak(aTHX_ "Bad filehandle: %s", SvPV(sv,n_a)); |
46fc3d4c | 5998 | break; |
5999 | } | |
6000 | return io; | |
6001 | } | |
6002 | ||
79072805 | 6003 | CV * |
864dbfa3 | 6004 | Perl_sv_2cv(pTHX_ SV *sv, HV **st, GV **gvp, I32 lref) |
79072805 LW |
6005 | { |
6006 | GV *gv; | |
6007 | CV *cv; | |
2d8e6c8d | 6008 | STRLEN n_a; |
79072805 LW |
6009 | |
6010 | if (!sv) | |
93a17b20 | 6011 | return *gvp = Nullgv, Nullcv; |
79072805 | 6012 | switch (SvTYPE(sv)) { |
79072805 LW |
6013 | case SVt_PVCV: |
6014 | *st = CvSTASH(sv); | |
6015 | *gvp = Nullgv; | |
6016 | return (CV*)sv; | |
6017 | case SVt_PVHV: | |
6018 | case SVt_PVAV: | |
6019 | *gvp = Nullgv; | |
6020 | return Nullcv; | |
8990e307 LW |
6021 | case SVt_PVGV: |
6022 | gv = (GV*)sv; | |
a0d0e21e | 6023 | *gvp = gv; |
8990e307 LW |
6024 | *st = GvESTASH(gv); |
6025 | goto fix_gv; | |
6026 | ||
79072805 | 6027 | default: |
a0d0e21e LW |
6028 | if (SvGMAGICAL(sv)) |
6029 | mg_get(sv); | |
6030 | if (SvROK(sv)) { | |
f5284f61 IZ |
6031 | SV **sp = &sv; /* Used in tryAMAGICunDEREF macro. */ |
6032 | tryAMAGICunDEREF(to_cv); | |
6033 | ||
62f274bf GS |
6034 | sv = SvRV(sv); |
6035 | if (SvTYPE(sv) == SVt_PVCV) { | |
6036 | cv = (CV*)sv; | |
6037 | *gvp = Nullgv; | |
6038 | *st = CvSTASH(cv); | |
6039 | return cv; | |
6040 | } | |
6041 | else if(isGV(sv)) | |
6042 | gv = (GV*)sv; | |
6043 | else | |
cea2e8a9 | 6044 | Perl_croak(aTHX_ "Not a subroutine reference"); |
a0d0e21e | 6045 | } |
62f274bf | 6046 | else if (isGV(sv)) |
79072805 LW |
6047 | gv = (GV*)sv; |
6048 | else | |
2d8e6c8d | 6049 | gv = gv_fetchpv(SvPV(sv, n_a), lref, SVt_PVCV); |
79072805 LW |
6050 | *gvp = gv; |
6051 | if (!gv) | |
6052 | return Nullcv; | |
6053 | *st = GvESTASH(gv); | |
8990e307 | 6054 | fix_gv: |
8ebc5c01 | 6055 | if (lref && !GvCVu(gv)) { |
4633a7c4 | 6056 | SV *tmpsv; |
748a9306 | 6057 | ENTER; |
4633a7c4 | 6058 | tmpsv = NEWSV(704,0); |
16660edb | 6059 | gv_efullname3(tmpsv, gv, Nullch); |
f6ec51f7 GS |
6060 | /* XXX this is probably not what they think they're getting. |
6061 | * It has the same effect as "sub name;", i.e. just a forward | |
6062 | * declaration! */ | |
774d564b | 6063 | newSUB(start_subparse(FALSE, 0), |
4633a7c4 LW |
6064 | newSVOP(OP_CONST, 0, tmpsv), |
6065 | Nullop, | |
8990e307 | 6066 | Nullop); |
748a9306 | 6067 | LEAVE; |
8ebc5c01 | 6068 | if (!GvCVu(gv)) |
cea2e8a9 | 6069 | Perl_croak(aTHX_ "Unable to create sub named \"%s\"", SvPV(sv,n_a)); |
8990e307 | 6070 | } |
8ebc5c01 | 6071 | return GvCVu(gv); |
79072805 LW |
6072 | } |
6073 | } | |
6074 | ||
c461cf8f JH |
6075 | /* |
6076 | =for apidoc sv_true | |
6077 | ||
6078 | Returns true if the SV has a true value by Perl's rules. | |
6079 | ||
6080 | =cut | |
6081 | */ | |
6082 | ||
79072805 | 6083 | I32 |
864dbfa3 | 6084 | Perl_sv_true(pTHX_ register SV *sv) |
79072805 | 6085 | { |
8990e307 LW |
6086 | if (!sv) |
6087 | return 0; | |
79072805 | 6088 | if (SvPOK(sv)) { |
4e35701f NIS |
6089 | register XPV* tXpv; |
6090 | if ((tXpv = (XPV*)SvANY(sv)) && | |
c2f1de04 | 6091 | (tXpv->xpv_cur > 1 || |
4e35701f | 6092 | (tXpv->xpv_cur && *tXpv->xpv_pv != '0'))) |
79072805 LW |
6093 | return 1; |
6094 | else | |
6095 | return 0; | |
6096 | } | |
6097 | else { | |
6098 | if (SvIOK(sv)) | |
463ee0b2 | 6099 | return SvIVX(sv) != 0; |
79072805 LW |
6100 | else { |
6101 | if (SvNOK(sv)) | |
463ee0b2 | 6102 | return SvNVX(sv) != 0.0; |
79072805 | 6103 | else |
463ee0b2 | 6104 | return sv_2bool(sv); |
79072805 LW |
6105 | } |
6106 | } | |
6107 | } | |
79072805 | 6108 | |
ff68c719 | 6109 | IV |
864dbfa3 | 6110 | Perl_sv_iv(pTHX_ register SV *sv) |
85e6fe83 | 6111 | { |
25da4f38 IZ |
6112 | if (SvIOK(sv)) { |
6113 | if (SvIsUV(sv)) | |
6114 | return (IV)SvUVX(sv); | |
ff68c719 | 6115 | return SvIVX(sv); |
25da4f38 | 6116 | } |
ff68c719 | 6117 | return sv_2iv(sv); |
85e6fe83 | 6118 | } |
85e6fe83 | 6119 | |
ff68c719 | 6120 | UV |
864dbfa3 | 6121 | Perl_sv_uv(pTHX_ register SV *sv) |
ff68c719 | 6122 | { |
25da4f38 IZ |
6123 | if (SvIOK(sv)) { |
6124 | if (SvIsUV(sv)) | |
6125 | return SvUVX(sv); | |
6126 | return (UV)SvIVX(sv); | |
6127 | } | |
ff68c719 | 6128 | return sv_2uv(sv); |
6129 | } | |
85e6fe83 | 6130 | |
65202027 | 6131 | NV |
864dbfa3 | 6132 | Perl_sv_nv(pTHX_ register SV *sv) |
79072805 | 6133 | { |
ff68c719 | 6134 | if (SvNOK(sv)) |
6135 | return SvNVX(sv); | |
6136 | return sv_2nv(sv); | |
79072805 | 6137 | } |
79072805 | 6138 | |
79072805 | 6139 | char * |
864dbfa3 | 6140 | Perl_sv_pv(pTHX_ SV *sv) |
1fa8b10d JD |
6141 | { |
6142 | STRLEN n_a; | |
6143 | ||
6144 | if (SvPOK(sv)) | |
6145 | return SvPVX(sv); | |
6146 | ||
6147 | return sv_2pv(sv, &n_a); | |
6148 | } | |
6149 | ||
6150 | char * | |
864dbfa3 | 6151 | Perl_sv_pvn(pTHX_ SV *sv, STRLEN *lp) |
79072805 | 6152 | { |
85e6fe83 LW |
6153 | if (SvPOK(sv)) { |
6154 | *lp = SvCUR(sv); | |
a0d0e21e | 6155 | return SvPVX(sv); |
85e6fe83 | 6156 | } |
463ee0b2 | 6157 | return sv_2pv(sv, lp); |
79072805 | 6158 | } |
79072805 | 6159 | |
c461cf8f JH |
6160 | /* |
6161 | =for apidoc sv_pvn_force | |
6162 | ||
6163 | Get a sensible string out of the SV somehow. | |
6164 | ||
6165 | =cut | |
6166 | */ | |
6167 | ||
a0d0e21e | 6168 | char * |
864dbfa3 | 6169 | Perl_sv_pvn_force(pTHX_ SV *sv, STRLEN *lp) |
a0d0e21e LW |
6170 | { |
6171 | char *s; | |
6172 | ||
6fc92669 GS |
6173 | if (SvTHINKFIRST(sv) && !SvROK(sv)) |
6174 | sv_force_normal(sv); | |
1c846c1f | 6175 | |
a0d0e21e LW |
6176 | if (SvPOK(sv)) { |
6177 | *lp = SvCUR(sv); | |
6178 | } | |
6179 | else { | |
748a9306 | 6180 | if (SvTYPE(sv) > SVt_PVLV && SvTYPE(sv) != SVt_PVFM) { |
cea2e8a9 | 6181 | Perl_croak(aTHX_ "Can't coerce %s to string in %s", sv_reftype(sv,0), |
6fc92669 | 6182 | PL_op_name[PL_op->op_type]); |
a0d0e21e | 6183 | } |
4633a7c4 LW |
6184 | else |
6185 | s = sv_2pv(sv, lp); | |
a0d0e21e LW |
6186 | if (s != SvPVX(sv)) { /* Almost, but not quite, sv_setpvn() */ |
6187 | STRLEN len = *lp; | |
1c846c1f | 6188 | |
a0d0e21e LW |
6189 | if (SvROK(sv)) |
6190 | sv_unref(sv); | |
6191 | (void)SvUPGRADE(sv, SVt_PV); /* Never FALSE */ | |
6192 | SvGROW(sv, len + 1); | |
6193 | Move(s,SvPVX(sv),len,char); | |
6194 | SvCUR_set(sv, len); | |
6195 | *SvEND(sv) = '\0'; | |
6196 | } | |
6197 | if (!SvPOK(sv)) { | |
6198 | SvPOK_on(sv); /* validate pointer */ | |
6199 | SvTAINT(sv); | |
1d7c1841 GS |
6200 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n", |
6201 | PTR2UV(sv),SvPVX(sv))); | |
a0d0e21e LW |
6202 | } |
6203 | } | |
6204 | return SvPVX(sv); | |
6205 | } | |
6206 | ||
6207 | char * | |
7340a771 GS |
6208 | Perl_sv_pvbyte(pTHX_ SV *sv) |
6209 | { | |
ffebcc3e | 6210 | sv_utf8_downgrade(sv,0); |
7340a771 GS |
6211 | return sv_pv(sv); |
6212 | } | |
6213 | ||
6214 | char * | |
6215 | Perl_sv_pvbyten(pTHX_ SV *sv, STRLEN *lp) | |
6216 | { | |
ffebcc3e | 6217 | sv_utf8_downgrade(sv,0); |
7340a771 GS |
6218 | return sv_pvn(sv,lp); |
6219 | } | |
6220 | ||
6221 | char * | |
6222 | Perl_sv_pvbyten_force(pTHX_ SV *sv, STRLEN *lp) | |
6223 | { | |
ffebcc3e | 6224 | sv_utf8_downgrade(sv,0); |
7340a771 GS |
6225 | return sv_pvn_force(sv,lp); |
6226 | } | |
6227 | ||
6228 | char * | |
6229 | Perl_sv_pvutf8(pTHX_ SV *sv) | |
6230 | { | |
560a288e | 6231 | sv_utf8_upgrade(sv); |
7340a771 GS |
6232 | return sv_pv(sv); |
6233 | } | |
6234 | ||
6235 | char * | |
6236 | Perl_sv_pvutf8n(pTHX_ SV *sv, STRLEN *lp) | |
6237 | { | |
560a288e | 6238 | sv_utf8_upgrade(sv); |
7340a771 GS |
6239 | return sv_pvn(sv,lp); |
6240 | } | |
6241 | ||
c461cf8f JH |
6242 | /* |
6243 | =for apidoc sv_pvutf8n_force | |
6244 | ||
6245 | Get a sensible UTF8-encoded string out of the SV somehow. See | |
6246 | L</sv_pvn_force>. | |
6247 | ||
6248 | =cut | |
6249 | */ | |
6250 | ||
7340a771 GS |
6251 | char * |
6252 | Perl_sv_pvutf8n_force(pTHX_ SV *sv, STRLEN *lp) | |
6253 | { | |
560a288e | 6254 | sv_utf8_upgrade(sv); |
7340a771 GS |
6255 | return sv_pvn_force(sv,lp); |
6256 | } | |
6257 | ||
c461cf8f JH |
6258 | /* |
6259 | =for apidoc sv_reftype | |
6260 | ||
6261 | Returns a string describing what the SV is a reference to. | |
6262 | ||
6263 | =cut | |
6264 | */ | |
6265 | ||
7340a771 | 6266 | char * |
864dbfa3 | 6267 | Perl_sv_reftype(pTHX_ SV *sv, int ob) |
a0d0e21e LW |
6268 | { |
6269 | if (ob && SvOBJECT(sv)) | |
6270 | return HvNAME(SvSTASH(sv)); | |
6271 | else { | |
6272 | switch (SvTYPE(sv)) { | |
6273 | case SVt_NULL: | |
6274 | case SVt_IV: | |
6275 | case SVt_NV: | |
6276 | case SVt_RV: | |
6277 | case SVt_PV: | |
6278 | case SVt_PVIV: | |
6279 | case SVt_PVNV: | |
6280 | case SVt_PVMG: | |
6281 | case SVt_PVBM: | |
6282 | if (SvROK(sv)) | |
6283 | return "REF"; | |
6284 | else | |
6285 | return "SCALAR"; | |
6286 | case SVt_PVLV: return "LVALUE"; | |
6287 | case SVt_PVAV: return "ARRAY"; | |
6288 | case SVt_PVHV: return "HASH"; | |
6289 | case SVt_PVCV: return "CODE"; | |
6290 | case SVt_PVGV: return "GLOB"; | |
1d2dff63 | 6291 | case SVt_PVFM: return "FORMAT"; |
27f9d8f3 | 6292 | case SVt_PVIO: return "IO"; |
a0d0e21e LW |
6293 | default: return "UNKNOWN"; |
6294 | } | |
6295 | } | |
6296 | } | |
6297 | ||
954c1994 GS |
6298 | /* |
6299 | =for apidoc sv_isobject | |
6300 | ||
6301 | Returns a boolean indicating whether the SV is an RV pointing to a blessed | |
6302 | object. If the SV is not an RV, or if the object is not blessed, then this | |
6303 | will return false. | |
6304 | ||
6305 | =cut | |
6306 | */ | |
6307 | ||
463ee0b2 | 6308 | int |
864dbfa3 | 6309 | Perl_sv_isobject(pTHX_ SV *sv) |
85e6fe83 | 6310 | { |
68dc0745 | 6311 | if (!sv) |
6312 | return 0; | |
6313 | if (SvGMAGICAL(sv)) | |
6314 | mg_get(sv); | |
85e6fe83 LW |
6315 | if (!SvROK(sv)) |
6316 | return 0; | |
6317 | sv = (SV*)SvRV(sv); | |
6318 | if (!SvOBJECT(sv)) | |
6319 | return 0; | |
6320 | return 1; | |
6321 | } | |
6322 | ||
954c1994 GS |
6323 | /* |
6324 | =for apidoc sv_isa | |
6325 | ||
6326 | Returns a boolean indicating whether the SV is blessed into the specified | |
6327 | class. This does not check for subtypes; use C<sv_derived_from> to verify | |
6328 | an inheritance relationship. | |
6329 | ||
6330 | =cut | |
6331 | */ | |
6332 | ||
85e6fe83 | 6333 | int |
864dbfa3 | 6334 | Perl_sv_isa(pTHX_ SV *sv, const char *name) |
463ee0b2 | 6335 | { |
68dc0745 | 6336 | if (!sv) |
6337 | return 0; | |
6338 | if (SvGMAGICAL(sv)) | |
6339 | mg_get(sv); | |
ed6116ce | 6340 | if (!SvROK(sv)) |
463ee0b2 | 6341 | return 0; |
ed6116ce LW |
6342 | sv = (SV*)SvRV(sv); |
6343 | if (!SvOBJECT(sv)) | |
463ee0b2 LW |
6344 | return 0; |
6345 | ||
6346 | return strEQ(HvNAME(SvSTASH(sv)), name); | |
6347 | } | |
6348 | ||
954c1994 GS |
6349 | /* |
6350 | =for apidoc newSVrv | |
6351 | ||
6352 | Creates a new SV for the RV, C<rv>, to point to. If C<rv> is not an RV then | |
6353 | it will be upgraded to one. If C<classname> is non-null then the new SV will | |
6354 | be blessed in the specified package. The new SV is returned and its | |
6355 | reference count is 1. | |
6356 | ||
6357 | =cut | |
6358 | */ | |
6359 | ||
463ee0b2 | 6360 | SV* |
864dbfa3 | 6361 | Perl_newSVrv(pTHX_ SV *rv, const char *classname) |
463ee0b2 | 6362 | { |
463ee0b2 LW |
6363 | SV *sv; |
6364 | ||
4561caa4 | 6365 | new_SV(sv); |
51cf62d8 | 6366 | |
2213622d | 6367 | SV_CHECK_THINKFIRST(rv); |
51cf62d8 | 6368 | SvAMAGIC_off(rv); |
51cf62d8 | 6369 | |
0199fce9 JD |
6370 | if (SvTYPE(rv) >= SVt_PVMG) { |
6371 | U32 refcnt = SvREFCNT(rv); | |
6372 | SvREFCNT(rv) = 0; | |
6373 | sv_clear(rv); | |
6374 | SvFLAGS(rv) = 0; | |
6375 | SvREFCNT(rv) = refcnt; | |
6376 | } | |
6377 | ||
51cf62d8 | 6378 | if (SvTYPE(rv) < SVt_RV) |
0199fce9 JD |
6379 | sv_upgrade(rv, SVt_RV); |
6380 | else if (SvTYPE(rv) > SVt_RV) { | |
6381 | (void)SvOOK_off(rv); | |
6382 | if (SvPVX(rv) && SvLEN(rv)) | |
6383 | Safefree(SvPVX(rv)); | |
6384 | SvCUR_set(rv, 0); | |
6385 | SvLEN_set(rv, 0); | |
6386 | } | |
51cf62d8 OT |
6387 | |
6388 | (void)SvOK_off(rv); | |
053fc874 | 6389 | SvRV(rv) = sv; |
ed6116ce | 6390 | SvROK_on(rv); |
463ee0b2 | 6391 | |
a0d0e21e LW |
6392 | if (classname) { |
6393 | HV* stash = gv_stashpv(classname, TRUE); | |
6394 | (void)sv_bless(rv, stash); | |
6395 | } | |
6396 | return sv; | |
6397 | } | |
6398 | ||
954c1994 GS |
6399 | /* |
6400 | =for apidoc sv_setref_pv | |
6401 | ||
6402 | Copies a pointer into a new SV, optionally blessing the SV. The C<rv> | |
6403 | argument will be upgraded to an RV. That RV will be modified to point to | |
6404 | the new SV. If the C<pv> argument is NULL then C<PL_sv_undef> will be placed | |
6405 | into the SV. The C<classname> argument indicates the package for the | |
6406 | blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV | |
6407 | will be returned and will have a reference count of 1. | |
6408 | ||
6409 | Do not use with other Perl types such as HV, AV, SV, CV, because those | |
6410 | objects will become corrupted by the pointer copy process. | |
6411 | ||
6412 | Note that C<sv_setref_pvn> copies the string while this copies the pointer. | |
6413 | ||
6414 | =cut | |
6415 | */ | |
6416 | ||
a0d0e21e | 6417 | SV* |
864dbfa3 | 6418 | Perl_sv_setref_pv(pTHX_ SV *rv, const char *classname, void *pv) |
a0d0e21e | 6419 | { |
189b2af5 | 6420 | if (!pv) { |
3280af22 | 6421 | sv_setsv(rv, &PL_sv_undef); |
189b2af5 GS |
6422 | SvSETMAGIC(rv); |
6423 | } | |
a0d0e21e | 6424 | else |
56431972 | 6425 | sv_setiv(newSVrv(rv,classname), PTR2IV(pv)); |
a0d0e21e LW |
6426 | return rv; |
6427 | } | |
6428 | ||
954c1994 GS |
6429 | /* |
6430 | =for apidoc sv_setref_iv | |
6431 | ||
6432 | Copies an integer into a new SV, optionally blessing the SV. The C<rv> | |
6433 | argument will be upgraded to an RV. That RV will be modified to point to | |
6434 | the new SV. The C<classname> argument indicates the package for the | |
6435 | blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV | |
6436 | will be returned and will have a reference count of 1. | |
6437 | ||
6438 | =cut | |
6439 | */ | |
6440 | ||
a0d0e21e | 6441 | SV* |
864dbfa3 | 6442 | Perl_sv_setref_iv(pTHX_ SV *rv, const char *classname, IV iv) |
a0d0e21e LW |
6443 | { |
6444 | sv_setiv(newSVrv(rv,classname), iv); | |
6445 | return rv; | |
6446 | } | |
6447 | ||
954c1994 | 6448 | /* |
e1c57cef JH |
6449 | =for apidoc sv_setref_uv |
6450 | ||
6451 | Copies an unsigned integer into a new SV, optionally blessing the SV. The C<rv> | |
6452 | argument will be upgraded to an RV. That RV will be modified to point to | |
6453 | the new SV. The C<classname> argument indicates the package for the | |
6454 | blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV | |
6455 | will be returned and will have a reference count of 1. | |
6456 | ||
6457 | =cut | |
6458 | */ | |
6459 | ||
6460 | SV* | |
6461 | Perl_sv_setref_uv(pTHX_ SV *rv, const char *classname, UV uv) | |
6462 | { | |
6463 | sv_setuv(newSVrv(rv,classname), uv); | |
6464 | return rv; | |
6465 | } | |
6466 | ||
6467 | /* | |
954c1994 GS |
6468 | =for apidoc sv_setref_nv |
6469 | ||
6470 | Copies a double into a new SV, optionally blessing the SV. The C<rv> | |
6471 | argument will be upgraded to an RV. That RV will be modified to point to | |
6472 | the new SV. The C<classname> argument indicates the package for the | |
6473 | blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV | |
6474 | will be returned and will have a reference count of 1. | |
6475 | ||
6476 | =cut | |
6477 | */ | |
6478 | ||
a0d0e21e | 6479 | SV* |
65202027 | 6480 | Perl_sv_setref_nv(pTHX_ SV *rv, const char *classname, NV nv) |
a0d0e21e LW |
6481 | { |
6482 | sv_setnv(newSVrv(rv,classname), nv); | |
6483 | return rv; | |
6484 | } | |
463ee0b2 | 6485 | |
954c1994 GS |
6486 | /* |
6487 | =for apidoc sv_setref_pvn | |
6488 | ||
6489 | Copies a string into a new SV, optionally blessing the SV. The length of the | |
6490 | string must be specified with C<n>. The C<rv> argument will be upgraded to | |
6491 | an RV. That RV will be modified to point to the new SV. The C<classname> | |
6492 | argument indicates the package for the blessing. Set C<classname> to | |
6493 | C<Nullch> to avoid the blessing. The new SV will be returned and will have | |
6494 | a reference count of 1. | |
6495 | ||
6496 | Note that C<sv_setref_pv> copies the pointer while this copies the string. | |
6497 | ||
6498 | =cut | |
6499 | */ | |
6500 | ||
a0d0e21e | 6501 | SV* |
864dbfa3 | 6502 | Perl_sv_setref_pvn(pTHX_ SV *rv, const char *classname, char *pv, STRLEN n) |
a0d0e21e LW |
6503 | { |
6504 | sv_setpvn(newSVrv(rv,classname), pv, n); | |
463ee0b2 LW |
6505 | return rv; |
6506 | } | |
6507 | ||
954c1994 GS |
6508 | /* |
6509 | =for apidoc sv_bless | |
6510 | ||
6511 | Blesses an SV into a specified package. The SV must be an RV. The package | |
6512 | must be designated by its stash (see C<gv_stashpv()>). The reference count | |
6513 | of the SV is unaffected. | |
6514 | ||
6515 | =cut | |
6516 | */ | |
6517 | ||
a0d0e21e | 6518 | SV* |
864dbfa3 | 6519 | Perl_sv_bless(pTHX_ SV *sv, HV *stash) |
a0d0e21e | 6520 | { |
76e3520e | 6521 | SV *tmpRef; |
a0d0e21e | 6522 | if (!SvROK(sv)) |
cea2e8a9 | 6523 | Perl_croak(aTHX_ "Can't bless non-reference value"); |
76e3520e GS |
6524 | tmpRef = SvRV(sv); |
6525 | if (SvFLAGS(tmpRef) & (SVs_OBJECT|SVf_READONLY)) { | |
6526 | if (SvREADONLY(tmpRef)) | |
cea2e8a9 | 6527 | Perl_croak(aTHX_ PL_no_modify); |
76e3520e GS |
6528 | if (SvOBJECT(tmpRef)) { |
6529 | if (SvTYPE(tmpRef) != SVt_PVIO) | |
3280af22 | 6530 | --PL_sv_objcount; |
76e3520e | 6531 | SvREFCNT_dec(SvSTASH(tmpRef)); |
2e3febc6 | 6532 | } |
a0d0e21e | 6533 | } |
76e3520e GS |
6534 | SvOBJECT_on(tmpRef); |
6535 | if (SvTYPE(tmpRef) != SVt_PVIO) | |
3280af22 | 6536 | ++PL_sv_objcount; |
76e3520e GS |
6537 | (void)SvUPGRADE(tmpRef, SVt_PVMG); |
6538 | SvSTASH(tmpRef) = (HV*)SvREFCNT_inc(stash); | |
a0d0e21e | 6539 | |
2e3febc6 CS |
6540 | if (Gv_AMG(stash)) |
6541 | SvAMAGIC_on(sv); | |
6542 | else | |
6543 | SvAMAGIC_off(sv); | |
a0d0e21e LW |
6544 | |
6545 | return sv; | |
6546 | } | |
6547 | ||
76e3520e | 6548 | STATIC void |
cea2e8a9 | 6549 | S_sv_unglob(pTHX_ SV *sv) |
a0d0e21e | 6550 | { |
850fabdf GS |
6551 | void *xpvmg; |
6552 | ||
a0d0e21e LW |
6553 | assert(SvTYPE(sv) == SVt_PVGV); |
6554 | SvFAKE_off(sv); | |
6555 | if (GvGP(sv)) | |
1edc1566 | 6556 | gp_free((GV*)sv); |
e826b3c7 GS |
6557 | if (GvSTASH(sv)) { |
6558 | SvREFCNT_dec(GvSTASH(sv)); | |
6559 | GvSTASH(sv) = Nullhv; | |
6560 | } | |
14befaf4 | 6561 | sv_unmagic(sv, PERL_MAGIC_glob); |
a0d0e21e | 6562 | Safefree(GvNAME(sv)); |
a5f75d66 | 6563 | GvMULTI_off(sv); |
850fabdf GS |
6564 | |
6565 | /* need to keep SvANY(sv) in the right arena */ | |
6566 | xpvmg = new_XPVMG(); | |
6567 | StructCopy(SvANY(sv), xpvmg, XPVMG); | |
6568 | del_XPVGV(SvANY(sv)); | |
6569 | SvANY(sv) = xpvmg; | |
6570 | ||
a0d0e21e LW |
6571 | SvFLAGS(sv) &= ~SVTYPEMASK; |
6572 | SvFLAGS(sv) |= SVt_PVMG; | |
6573 | } | |
6574 | ||
954c1994 | 6575 | /* |
840a7b70 | 6576 | =for apidoc sv_unref_flags |
954c1994 GS |
6577 | |
6578 | Unsets the RV status of the SV, and decrements the reference count of | |
6579 | whatever was being referenced by the RV. This can almost be thought of | |
840a7b70 IZ |
6580 | as a reversal of C<newSVrv>. The C<cflags> argument can contain |
6581 | C<SV_IMMEDIATE_UNREF> to force the reference count to be decremented | |
6582 | (otherwise the decrementing is conditional on the reference count being | |
6583 | different from one or the reference being a readonly SV). | |
7889fe52 | 6584 | See C<SvROK_off>. |
954c1994 GS |
6585 | |
6586 | =cut | |
6587 | */ | |
6588 | ||
ed6116ce | 6589 | void |
840a7b70 | 6590 | Perl_sv_unref_flags(pTHX_ SV *sv, U32 flags) |
ed6116ce | 6591 | { |
a0d0e21e | 6592 | SV* rv = SvRV(sv); |
810b8aa5 GS |
6593 | |
6594 | if (SvWEAKREF(sv)) { | |
6595 | sv_del_backref(sv); | |
6596 | SvWEAKREF_off(sv); | |
6597 | SvRV(sv) = 0; | |
6598 | return; | |
6599 | } | |
ed6116ce LW |
6600 | SvRV(sv) = 0; |
6601 | SvROK_off(sv); | |
840a7b70 | 6602 | if (SvREFCNT(rv) != 1 || SvREADONLY(rv) || flags) /* SV_IMMEDIATE_UNREF */ |
4633a7c4 | 6603 | SvREFCNT_dec(rv); |
840a7b70 | 6604 | else /* XXX Hack, but hard to make $a=$a->[1] work otherwise */ |
4633a7c4 | 6605 | sv_2mortal(rv); /* Schedule for freeing later */ |
ed6116ce | 6606 | } |
8990e307 | 6607 | |
840a7b70 IZ |
6608 | /* |
6609 | =for apidoc sv_unref | |
6610 | ||
6611 | Unsets the RV status of the SV, and decrements the reference count of | |
6612 | whatever was being referenced by the RV. This can almost be thought of | |
6613 | as a reversal of C<newSVrv>. This is C<sv_unref_flags> with the C<flag> | |
7889fe52 | 6614 | being zero. See C<SvROK_off>. |
840a7b70 IZ |
6615 | |
6616 | =cut | |
6617 | */ | |
6618 | ||
6619 | void | |
6620 | Perl_sv_unref(pTHX_ SV *sv) | |
6621 | { | |
6622 | sv_unref_flags(sv, 0); | |
6623 | } | |
6624 | ||
bbce6d69 | 6625 | void |
864dbfa3 | 6626 | Perl_sv_taint(pTHX_ SV *sv) |
bbce6d69 | 6627 | { |
14befaf4 | 6628 | sv_magic((sv), Nullsv, PERL_MAGIC_taint, Nullch, 0); |
bbce6d69 | 6629 | } |
6630 | ||
6631 | void | |
864dbfa3 | 6632 | Perl_sv_untaint(pTHX_ SV *sv) |
bbce6d69 | 6633 | { |
13f57bf8 | 6634 | if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) { |
14befaf4 | 6635 | MAGIC *mg = mg_find(sv, PERL_MAGIC_taint); |
36477c24 | 6636 | if (mg) |
565764a8 | 6637 | mg->mg_len &= ~1; |
36477c24 | 6638 | } |
bbce6d69 | 6639 | } |
6640 | ||
6641 | bool | |
864dbfa3 | 6642 | Perl_sv_tainted(pTHX_ SV *sv) |
bbce6d69 | 6643 | { |
13f57bf8 | 6644 | if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) { |
14befaf4 | 6645 | MAGIC *mg = mg_find(sv, PERL_MAGIC_taint); |
155aba94 | 6646 | if (mg && ((mg->mg_len & 1) || ((mg->mg_len & 2) && mg->mg_obj == sv))) |
36477c24 | 6647 | return TRUE; |
6648 | } | |
6649 | return FALSE; | |
bbce6d69 | 6650 | } |
6651 | ||
954c1994 GS |
6652 | /* |
6653 | =for apidoc sv_setpviv | |
6654 | ||
6655 | Copies an integer into the given SV, also updating its string value. | |
6656 | Does not handle 'set' magic. See C<sv_setpviv_mg>. | |
6657 | ||
6658 | =cut | |
6659 | */ | |
6660 | ||
84902520 | 6661 | void |
864dbfa3 | 6662 | Perl_sv_setpviv(pTHX_ SV *sv, IV iv) |
84902520 | 6663 | { |
25da4f38 IZ |
6664 | char buf[TYPE_CHARS(UV)]; |
6665 | char *ebuf; | |
6666 | char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf); | |
84902520 | 6667 | |
25da4f38 | 6668 | sv_setpvn(sv, ptr, ebuf - ptr); |
84902520 TB |
6669 | } |
6670 | ||
ef50df4b | 6671 | |
954c1994 GS |
6672 | /* |
6673 | =for apidoc sv_setpviv_mg | |
6674 | ||
6675 | Like C<sv_setpviv>, but also handles 'set' magic. | |
6676 | ||
6677 | =cut | |
6678 | */ | |
6679 | ||
ef50df4b | 6680 | void |
864dbfa3 | 6681 | Perl_sv_setpviv_mg(pTHX_ SV *sv, IV iv) |
ef50df4b | 6682 | { |
25da4f38 IZ |
6683 | char buf[TYPE_CHARS(UV)]; |
6684 | char *ebuf; | |
6685 | char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf); | |
6686 | ||
6687 | sv_setpvn(sv, ptr, ebuf - ptr); | |
ef50df4b GS |
6688 | SvSETMAGIC(sv); |
6689 | } | |
6690 | ||
cea2e8a9 GS |
6691 | #if defined(PERL_IMPLICIT_CONTEXT) |
6692 | void | |
6693 | Perl_sv_setpvf_nocontext(SV *sv, const char* pat, ...) | |
6694 | { | |
6695 | dTHX; | |
6696 | va_list args; | |
6697 | va_start(args, pat); | |
c5be433b | 6698 | sv_vsetpvf(sv, pat, &args); |
cea2e8a9 GS |
6699 | va_end(args); |
6700 | } | |
6701 | ||
6702 | ||
6703 | void | |
6704 | Perl_sv_setpvf_mg_nocontext(SV *sv, const char* pat, ...) | |
6705 | { | |
6706 | dTHX; | |
6707 | va_list args; | |
6708 | va_start(args, pat); | |
c5be433b | 6709 | sv_vsetpvf_mg(sv, pat, &args); |
cea2e8a9 | 6710 | va_end(args); |
cea2e8a9 GS |
6711 | } |
6712 | #endif | |
6713 | ||
954c1994 GS |
6714 | /* |
6715 | =for apidoc sv_setpvf | |
6716 | ||
6717 | Processes its arguments like C<sprintf> and sets an SV to the formatted | |
6718 | output. Does not handle 'set' magic. See C<sv_setpvf_mg>. | |
6719 | ||
6720 | =cut | |
6721 | */ | |
6722 | ||
46fc3d4c | 6723 | void |
864dbfa3 | 6724 | Perl_sv_setpvf(pTHX_ SV *sv, const char* pat, ...) |
46fc3d4c | 6725 | { |
6726 | va_list args; | |
46fc3d4c | 6727 | va_start(args, pat); |
c5be433b | 6728 | sv_vsetpvf(sv, pat, &args); |
46fc3d4c | 6729 | va_end(args); |
6730 | } | |
6731 | ||
c5be433b GS |
6732 | void |
6733 | Perl_sv_vsetpvf(pTHX_ SV *sv, const char* pat, va_list* args) | |
6734 | { | |
6735 | sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
6736 | } | |
ef50df4b | 6737 | |
954c1994 GS |
6738 | /* |
6739 | =for apidoc sv_setpvf_mg | |
6740 | ||
6741 | Like C<sv_setpvf>, but also handles 'set' magic. | |
6742 | ||
6743 | =cut | |
6744 | */ | |
6745 | ||
ef50df4b | 6746 | void |
864dbfa3 | 6747 | Perl_sv_setpvf_mg(pTHX_ SV *sv, const char* pat, ...) |
ef50df4b GS |
6748 | { |
6749 | va_list args; | |
ef50df4b | 6750 | va_start(args, pat); |
c5be433b | 6751 | sv_vsetpvf_mg(sv, pat, &args); |
ef50df4b | 6752 | va_end(args); |
c5be433b GS |
6753 | } |
6754 | ||
6755 | void | |
6756 | Perl_sv_vsetpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args) | |
6757 | { | |
6758 | sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
ef50df4b GS |
6759 | SvSETMAGIC(sv); |
6760 | } | |
6761 | ||
cea2e8a9 GS |
6762 | #if defined(PERL_IMPLICIT_CONTEXT) |
6763 | void | |
6764 | Perl_sv_catpvf_nocontext(SV *sv, const char* pat, ...) | |
6765 | { | |
6766 | dTHX; | |
6767 | va_list args; | |
6768 | va_start(args, pat); | |
c5be433b | 6769 | sv_vcatpvf(sv, pat, &args); |
cea2e8a9 GS |
6770 | va_end(args); |
6771 | } | |
6772 | ||
6773 | void | |
6774 | Perl_sv_catpvf_mg_nocontext(SV *sv, const char* pat, ...) | |
6775 | { | |
6776 | dTHX; | |
6777 | va_list args; | |
6778 | va_start(args, pat); | |
c5be433b | 6779 | sv_vcatpvf_mg(sv, pat, &args); |
cea2e8a9 | 6780 | va_end(args); |
cea2e8a9 GS |
6781 | } |
6782 | #endif | |
6783 | ||
954c1994 GS |
6784 | /* |
6785 | =for apidoc sv_catpvf | |
6786 | ||
d5ce4a7c GA |
6787 | Processes its arguments like C<sprintf> and appends the formatted |
6788 | output to an SV. If the appended data contains "wide" characters | |
6789 | (including, but not limited to, SVs with a UTF-8 PV formatted with %s, | |
6790 | and characters >255 formatted with %c), the original SV might get | |
6791 | upgraded to UTF-8. Handles 'get' magic, but not 'set' magic. | |
6792 | C<SvSETMAGIC()> must typically be called after calling this function | |
6793 | to handle 'set' magic. | |
954c1994 | 6794 | |
d5ce4a7c | 6795 | =cut */ |
954c1994 | 6796 | |
46fc3d4c | 6797 | void |
864dbfa3 | 6798 | Perl_sv_catpvf(pTHX_ SV *sv, const char* pat, ...) |
46fc3d4c | 6799 | { |
6800 | va_list args; | |
46fc3d4c | 6801 | va_start(args, pat); |
c5be433b | 6802 | sv_vcatpvf(sv, pat, &args); |
46fc3d4c | 6803 | va_end(args); |
6804 | } | |
6805 | ||
ef50df4b | 6806 | void |
c5be433b GS |
6807 | Perl_sv_vcatpvf(pTHX_ SV *sv, const char* pat, va_list* args) |
6808 | { | |
6809 | sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
6810 | } | |
6811 | ||
954c1994 GS |
6812 | /* |
6813 | =for apidoc sv_catpvf_mg | |
6814 | ||
6815 | Like C<sv_catpvf>, but also handles 'set' magic. | |
6816 | ||
6817 | =cut | |
6818 | */ | |
6819 | ||
c5be433b | 6820 | void |
864dbfa3 | 6821 | Perl_sv_catpvf_mg(pTHX_ SV *sv, const char* pat, ...) |
ef50df4b GS |
6822 | { |
6823 | va_list args; | |
ef50df4b | 6824 | va_start(args, pat); |
c5be433b | 6825 | sv_vcatpvf_mg(sv, pat, &args); |
ef50df4b | 6826 | va_end(args); |
c5be433b GS |
6827 | } |
6828 | ||
6829 | void | |
6830 | Perl_sv_vcatpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args) | |
6831 | { | |
6832 | sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
ef50df4b GS |
6833 | SvSETMAGIC(sv); |
6834 | } | |
6835 | ||
954c1994 GS |
6836 | /* |
6837 | =for apidoc sv_vsetpvfn | |
6838 | ||
6839 | Works like C<vcatpvfn> but copies the text into the SV instead of | |
6840 | appending it. | |
6841 | ||
6842 | =cut | |
6843 | */ | |
6844 | ||
46fc3d4c | 6845 | void |
7d5ea4e7 | 6846 | Perl_sv_vsetpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted) |
46fc3d4c | 6847 | { |
6848 | sv_setpvn(sv, "", 0); | |
7d5ea4e7 | 6849 | sv_vcatpvfn(sv, pat, patlen, args, svargs, svmax, maybe_tainted); |
46fc3d4c | 6850 | } |
6851 | ||
2d00ba3b | 6852 | STATIC I32 |
9dd79c3f | 6853 | S_expect_number(pTHX_ char** pattern) |
211dfcf1 HS |
6854 | { |
6855 | I32 var = 0; | |
6856 | switch (**pattern) { | |
6857 | case '1': case '2': case '3': | |
6858 | case '4': case '5': case '6': | |
6859 | case '7': case '8': case '9': | |
6860 | while (isDIGIT(**pattern)) | |
6861 | var = var * 10 + (*(*pattern)++ - '0'); | |
6862 | } | |
6863 | return var; | |
6864 | } | |
9dd79c3f | 6865 | #define EXPECT_NUMBER(pattern, var) (var = S_expect_number(aTHX_ &pattern)) |
211dfcf1 | 6866 | |
954c1994 GS |
6867 | /* |
6868 | =for apidoc sv_vcatpvfn | |
6869 | ||
6870 | Processes its arguments like C<vsprintf> and appends the formatted output | |
6871 | to an SV. Uses an array of SVs if the C style variable argument list is | |
6872 | missing (NULL). When running with taint checks enabled, indicates via | |
6873 | C<maybe_tainted> if results are untrustworthy (often due to the use of | |
6874 | locales). | |
6875 | ||
6876 | =cut | |
6877 | */ | |
6878 | ||
46fc3d4c | 6879 | void |
7d5ea4e7 | 6880 | Perl_sv_vcatpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted) |
46fc3d4c | 6881 | { |
6882 | char *p; | |
6883 | char *q; | |
6884 | char *patend; | |
fc36a67e | 6885 | STRLEN origlen; |
46fc3d4c | 6886 | I32 svix = 0; |
c635e13b | 6887 | static char nullstr[] = "(null)"; |
7e2040f0 | 6888 | SV *argsv; |
46fc3d4c | 6889 | |
6890 | /* no matter what, this is a string now */ | |
fc36a67e | 6891 | (void)SvPV_force(sv, origlen); |
46fc3d4c | 6892 | |
fc36a67e | 6893 | /* special-case "", "%s", and "%_" */ |
46fc3d4c | 6894 | if (patlen == 0) |
6895 | return; | |
fc36a67e | 6896 | if (patlen == 2 && pat[0] == '%') { |
6897 | switch (pat[1]) { | |
6898 | case 's': | |
c635e13b | 6899 | if (args) { |
6900 | char *s = va_arg(*args, char*); | |
6901 | sv_catpv(sv, s ? s : nullstr); | |
6902 | } | |
7e2040f0 | 6903 | else if (svix < svmax) { |
fc36a67e | 6904 | sv_catsv(sv, *svargs); |
7e2040f0 GS |
6905 | if (DO_UTF8(*svargs)) |
6906 | SvUTF8_on(sv); | |
6907 | } | |
fc36a67e | 6908 | return; |
6909 | case '_': | |
6910 | if (args) { | |
7e2040f0 GS |
6911 | argsv = va_arg(*args, SV*); |
6912 | sv_catsv(sv, argsv); | |
6913 | if (DO_UTF8(argsv)) | |
6914 | SvUTF8_on(sv); | |
fc36a67e | 6915 | return; |
6916 | } | |
6917 | /* See comment on '_' below */ | |
6918 | break; | |
6919 | } | |
46fc3d4c | 6920 | } |
6921 | ||
6922 | patend = (char*)pat + patlen; | |
6923 | for (p = (char*)pat; p < patend; p = q) { | |
6924 | bool alt = FALSE; | |
6925 | bool left = FALSE; | |
b22c7a20 | 6926 | bool vectorize = FALSE; |
211dfcf1 | 6927 | bool vectorarg = FALSE; |
b2e23cf9 | 6928 | bool vec_utf = FALSE; |
46fc3d4c | 6929 | char fill = ' '; |
6930 | char plus = 0; | |
6931 | char intsize = 0; | |
6932 | STRLEN width = 0; | |
fc36a67e | 6933 | STRLEN zeros = 0; |
46fc3d4c | 6934 | bool has_precis = FALSE; |
6935 | STRLEN precis = 0; | |
7e2040f0 | 6936 | bool is_utf = FALSE; |
eb3fce90 | 6937 | |
46fc3d4c | 6938 | char esignbuf[4]; |
ad391ad9 | 6939 | U8 utf8buf[UTF8_MAXLEN+1]; |
46fc3d4c | 6940 | STRLEN esignlen = 0; |
6941 | ||
6942 | char *eptr = Nullch; | |
fc36a67e | 6943 | STRLEN elen = 0; |
089c015b JH |
6944 | /* Times 4: a decimal digit takes more than 3 binary digits. |
6945 | * NV_DIG: mantissa takes than many decimal digits. | |
6946 | * Plus 32: Playing safe. */ | |
6947 | char ebuf[IV_DIG * 4 + NV_DIG + 32]; | |
2d4389e4 JH |
6948 | /* large enough for "%#.#f" --chip */ |
6949 | /* what about long double NVs? --jhi */ | |
b22c7a20 GS |
6950 | |
6951 | SV *vecsv; | |
a05b299f | 6952 | U8 *vecstr = Null(U8*); |
b22c7a20 | 6953 | STRLEN veclen = 0; |
46fc3d4c | 6954 | char c; |
6955 | int i; | |
6956 | unsigned base; | |
6957 | IV iv; | |
6958 | UV uv; | |
65202027 | 6959 | NV nv; |
46fc3d4c | 6960 | STRLEN have; |
6961 | STRLEN need; | |
6962 | STRLEN gap; | |
b22c7a20 GS |
6963 | char *dotstr = "."; |
6964 | STRLEN dotstrlen = 1; | |
211dfcf1 | 6965 | I32 efix = 0; /* explicit format parameter index */ |
eb3fce90 | 6966 | I32 ewix = 0; /* explicit width index */ |
211dfcf1 HS |
6967 | I32 epix = 0; /* explicit precision index */ |
6968 | I32 evix = 0; /* explicit vector index */ | |
eb3fce90 | 6969 | bool asterisk = FALSE; |
46fc3d4c | 6970 | |
211dfcf1 | 6971 | /* echo everything up to the next format specification */ |
46fc3d4c | 6972 | for (q = p; q < patend && *q != '%'; ++q) ; |
6973 | if (q > p) { | |
6974 | sv_catpvn(sv, p, q - p); | |
6975 | p = q; | |
6976 | } | |
6977 | if (q++ >= patend) | |
6978 | break; | |
6979 | ||
211dfcf1 HS |
6980 | /* |
6981 | We allow format specification elements in this order: | |
6982 | \d+\$ explicit format parameter index | |
6983 | [-+ 0#]+ flags | |
6984 | \*?(\d+\$)?v vector with optional (optionally specified) arg | |
6985 | \d+|\*(\d+\$)? width using optional (optionally specified) arg | |
6986 | \.(\d*|\*(\d+\$)?) precision using optional (optionally specified) arg | |
6987 | [hlqLV] size | |
6988 | [%bcdefginopsux_DFOUX] format (mandatory) | |
6989 | */ | |
6990 | if (EXPECT_NUMBER(q, width)) { | |
6991 | if (*q == '$') { | |
6992 | ++q; | |
6993 | efix = width; | |
6994 | } else { | |
6995 | goto gotwidth; | |
6996 | } | |
6997 | } | |
6998 | ||
fc36a67e | 6999 | /* FLAGS */ |
7000 | ||
46fc3d4c | 7001 | while (*q) { |
7002 | switch (*q) { | |
7003 | case ' ': | |
7004 | case '+': | |
7005 | plus = *q++; | |
7006 | continue; | |
7007 | ||
7008 | case '-': | |
7009 | left = TRUE; | |
7010 | q++; | |
7011 | continue; | |
7012 | ||
7013 | case '0': | |
7014 | fill = *q++; | |
7015 | continue; | |
7016 | ||
7017 | case '#': | |
7018 | alt = TRUE; | |
7019 | q++; | |
7020 | continue; | |
7021 | ||
fc36a67e | 7022 | default: |
7023 | break; | |
7024 | } | |
7025 | break; | |
7026 | } | |
46fc3d4c | 7027 | |
211dfcf1 | 7028 | tryasterisk: |
eb3fce90 | 7029 | if (*q == '*') { |
211dfcf1 HS |
7030 | q++; |
7031 | if (EXPECT_NUMBER(q, ewix)) | |
7032 | if (*q++ != '$') | |
7033 | goto unknown; | |
eb3fce90 | 7034 | asterisk = TRUE; |
211dfcf1 HS |
7035 | } |
7036 | if (*q == 'v') { | |
eb3fce90 | 7037 | q++; |
211dfcf1 HS |
7038 | if (vectorize) |
7039 | goto unknown; | |
9cbac4c7 | 7040 | if ((vectorarg = asterisk)) { |
211dfcf1 HS |
7041 | evix = ewix; |
7042 | ewix = 0; | |
7043 | asterisk = FALSE; | |
7044 | } | |
7045 | vectorize = TRUE; | |
7046 | goto tryasterisk; | |
eb3fce90 JH |
7047 | } |
7048 | ||
211dfcf1 HS |
7049 | if (!asterisk) |
7050 | EXPECT_NUMBER(q, width); | |
7051 | ||
7052 | if (vectorize) { | |
7053 | if (vectorarg) { | |
7054 | if (args) | |
7055 | vecsv = va_arg(*args, SV*); | |
7056 | else | |
7057 | vecsv = (evix ? evix <= svmax : svix < svmax) ? | |
7058 | svargs[ewix ? ewix-1 : svix++] : &PL_sv_undef; | |
4459522c | 7059 | dotstr = SvPVx(vecsv, dotstrlen); |
211dfcf1 HS |
7060 | if (DO_UTF8(vecsv)) |
7061 | is_utf = TRUE; | |
7062 | } | |
7063 | if (args) { | |
7064 | vecsv = va_arg(*args, SV*); | |
7065 | vecstr = (U8*)SvPVx(vecsv,veclen); | |
b2e23cf9 | 7066 | vec_utf = DO_UTF8(vecsv); |
eb3fce90 | 7067 | } |
211dfcf1 HS |
7068 | else if (efix ? efix <= svmax : svix < svmax) { |
7069 | vecsv = svargs[efix ? efix-1 : svix++]; | |
7070 | vecstr = (U8*)SvPVx(vecsv,veclen); | |
b2e23cf9 | 7071 | vec_utf = DO_UTF8(vecsv); |
211dfcf1 HS |
7072 | } |
7073 | else { | |
7074 | vecstr = (U8*)""; | |
7075 | veclen = 0; | |
7076 | } | |
eb3fce90 | 7077 | } |
fc36a67e | 7078 | |
eb3fce90 | 7079 | if (asterisk) { |
fc36a67e | 7080 | if (args) |
7081 | i = va_arg(*args, int); | |
7082 | else | |
eb3fce90 JH |
7083 | i = (ewix ? ewix <= svmax : svix < svmax) ? |
7084 | SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0; | |
fc36a67e | 7085 | left |= (i < 0); |
7086 | width = (i < 0) ? -i : i; | |
fc36a67e | 7087 | } |
211dfcf1 | 7088 | gotwidth: |
fc36a67e | 7089 | |
7090 | /* PRECISION */ | |
46fc3d4c | 7091 | |
fc36a67e | 7092 | if (*q == '.') { |
7093 | q++; | |
7094 | if (*q == '*') { | |
211dfcf1 HS |
7095 | q++; |
7096 | if (EXPECT_NUMBER(q, epix) && *q++ != '$') | |
7097 | goto unknown; | |
46fc3d4c | 7098 | if (args) |
7099 | i = va_arg(*args, int); | |
7100 | else | |
eb3fce90 JH |
7101 | i = (ewix ? ewix <= svmax : svix < svmax) |
7102 | ? SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0; | |
fc36a67e | 7103 | precis = (i < 0) ? 0 : i; |
fc36a67e | 7104 | } |
7105 | else { | |
7106 | precis = 0; | |
7107 | while (isDIGIT(*q)) | |
7108 | precis = precis * 10 + (*q++ - '0'); | |
7109 | } | |
7110 | has_precis = TRUE; | |
7111 | } | |
46fc3d4c | 7112 | |
fc36a67e | 7113 | /* SIZE */ |
46fc3d4c | 7114 | |
fc36a67e | 7115 | switch (*q) { |
e5c81feb | 7116 | #if defined(HAS_QUAD) || (defined(HAS_LONG_DOUBLE) && defined(USE_LONG_DOUBLE)) |
6f9bb7fd | 7117 | case 'L': /* Ld */ |
e5c81feb JH |
7118 | /* FALL THROUGH */ |
7119 | #endif | |
7120 | #ifdef HAS_QUAD | |
6f9bb7fd GS |
7121 | case 'q': /* qd */ |
7122 | intsize = 'q'; | |
7123 | q++; | |
7124 | break; | |
7125 | #endif | |
fc36a67e | 7126 | case 'l': |
e5c81feb JH |
7127 | #if defined(HAS_QUAD) || (defined(HAS_LONG_DOUBLE) && defined(USE_LONG_DOUBLE)) |
7128 | if (*(q + 1) == 'l') { /* lld, llf */ | |
fc36a67e | 7129 | intsize = 'q'; |
7130 | q += 2; | |
46fc3d4c | 7131 | break; |
cf2093f6 | 7132 | } |
fc36a67e | 7133 | #endif |
6f9bb7fd | 7134 | /* FALL THROUGH */ |
fc36a67e | 7135 | case 'h': |
cf2093f6 | 7136 | /* FALL THROUGH */ |
fc36a67e | 7137 | case 'V': |
7138 | intsize = *q++; | |
46fc3d4c | 7139 | break; |
7140 | } | |
7141 | ||
fc36a67e | 7142 | /* CONVERSION */ |
7143 | ||
211dfcf1 HS |
7144 | if (*q == '%') { |
7145 | eptr = q++; | |
7146 | elen = 1; | |
7147 | goto string; | |
7148 | } | |
7149 | ||
7150 | if (!args) | |
7151 | argsv = (efix ? efix <= svmax : svix < svmax) ? | |
7152 | svargs[efix ? efix-1 : svix++] : &PL_sv_undef; | |
7153 | ||
46fc3d4c | 7154 | switch (c = *q++) { |
7155 | ||
7156 | /* STRINGS */ | |
7157 | ||
46fc3d4c | 7158 | case 'c': |
211dfcf1 | 7159 | uv = args ? va_arg(*args, int) : SvIVx(argsv); |
1bd104fb JH |
7160 | if ((uv > 255 || |
7161 | (!UNI_IS_INVARIANT(uv) && SvUTF8(sv))) | |
7162 | && !IN_BYTE) { | |
dfe13c55 | 7163 | eptr = (char*)utf8buf; |
9041c2e3 | 7164 | elen = uvchr_to_utf8((U8*)eptr, uv) - utf8buf; |
7e2040f0 GS |
7165 | is_utf = TRUE; |
7166 | } | |
7167 | else { | |
7168 | c = (char)uv; | |
7169 | eptr = &c; | |
7170 | elen = 1; | |
a0ed51b3 | 7171 | } |
46fc3d4c | 7172 | goto string; |
7173 | ||
46fc3d4c | 7174 | case 's': |
7175 | if (args) { | |
fc36a67e | 7176 | eptr = va_arg(*args, char*); |
c635e13b | 7177 | if (eptr) |
1d7c1841 GS |
7178 | #ifdef MACOS_TRADITIONAL |
7179 | /* On MacOS, %#s format is used for Pascal strings */ | |
7180 | if (alt) | |
7181 | elen = *eptr++; | |
7182 | else | |
7183 | #endif | |
c635e13b | 7184 | elen = strlen(eptr); |
7185 | else { | |
7186 | eptr = nullstr; | |
7187 | elen = sizeof nullstr - 1; | |
7188 | } | |
46fc3d4c | 7189 | } |
211dfcf1 | 7190 | else { |
7e2040f0 GS |
7191 | eptr = SvPVx(argsv, elen); |
7192 | if (DO_UTF8(argsv)) { | |
a0ed51b3 LW |
7193 | if (has_precis && precis < elen) { |
7194 | I32 p = precis; | |
7e2040f0 | 7195 | sv_pos_u2b(argsv, &p, 0); /* sticks at end */ |
a0ed51b3 LW |
7196 | precis = p; |
7197 | } | |
7198 | if (width) { /* fudge width (can't fudge elen) */ | |
7e2040f0 | 7199 | width += elen - sv_len_utf8(argsv); |
a0ed51b3 | 7200 | } |
7e2040f0 | 7201 | is_utf = TRUE; |
a0ed51b3 LW |
7202 | } |
7203 | } | |
46fc3d4c | 7204 | goto string; |
7205 | ||
fc36a67e | 7206 | case '_': |
7207 | /* | |
7208 | * The "%_" hack might have to be changed someday, | |
7209 | * if ISO or ANSI decide to use '_' for something. | |
7210 | * So we keep it hidden from users' code. | |
7211 | */ | |
7212 | if (!args) | |
7213 | goto unknown; | |
211dfcf1 | 7214 | argsv = va_arg(*args, SV*); |
7e2040f0 GS |
7215 | eptr = SvPVx(argsv, elen); |
7216 | if (DO_UTF8(argsv)) | |
7217 | is_utf = TRUE; | |
fc36a67e | 7218 | |
46fc3d4c | 7219 | string: |
b22c7a20 | 7220 | vectorize = FALSE; |
46fc3d4c | 7221 | if (has_precis && elen > precis) |
7222 | elen = precis; | |
7223 | break; | |
7224 | ||
7225 | /* INTEGERS */ | |
7226 | ||
fc36a67e | 7227 | case 'p': |
c2e66d9e GS |
7228 | if (alt) |
7229 | goto unknown; | |
211dfcf1 | 7230 | uv = PTR2UV(args ? va_arg(*args, void*) : argsv); |
fc36a67e | 7231 | base = 16; |
7232 | goto integer; | |
7233 | ||
46fc3d4c | 7234 | case 'D': |
29fe7a80 | 7235 | #ifdef IV_IS_QUAD |
22f3ae8c | 7236 | intsize = 'q'; |
29fe7a80 | 7237 | #else |
46fc3d4c | 7238 | intsize = 'l'; |
29fe7a80 | 7239 | #endif |
46fc3d4c | 7240 | /* FALL THROUGH */ |
7241 | case 'd': | |
7242 | case 'i': | |
b22c7a20 | 7243 | if (vectorize) { |
ba210ebe | 7244 | STRLEN ulen; |
211dfcf1 HS |
7245 | if (!veclen) |
7246 | continue; | |
b2e23cf9 | 7247 | if (vec_utf) |
9041c2e3 | 7248 | iv = (IV)utf8n_to_uvchr(vecstr, veclen, &ulen, 0); |
b22c7a20 | 7249 | else { |
a05b299f | 7250 | iv = *vecstr; |
b22c7a20 GS |
7251 | ulen = 1; |
7252 | } | |
7253 | vecstr += ulen; | |
7254 | veclen -= ulen; | |
7255 | } | |
7256 | else if (args) { | |
46fc3d4c | 7257 | switch (intsize) { |
7258 | case 'h': iv = (short)va_arg(*args, int); break; | |
7259 | default: iv = va_arg(*args, int); break; | |
7260 | case 'l': iv = va_arg(*args, long); break; | |
fc36a67e | 7261 | case 'V': iv = va_arg(*args, IV); break; |
cf2093f6 JH |
7262 | #ifdef HAS_QUAD |
7263 | case 'q': iv = va_arg(*args, Quad_t); break; | |
7264 | #endif | |
46fc3d4c | 7265 | } |
7266 | } | |
7267 | else { | |
211dfcf1 | 7268 | iv = SvIVx(argsv); |
46fc3d4c | 7269 | switch (intsize) { |
7270 | case 'h': iv = (short)iv; break; | |
be28567c | 7271 | default: break; |
46fc3d4c | 7272 | case 'l': iv = (long)iv; break; |
fc36a67e | 7273 | case 'V': break; |
cf2093f6 JH |
7274 | #ifdef HAS_QUAD |
7275 | case 'q': iv = (Quad_t)iv; break; | |
7276 | #endif | |
46fc3d4c | 7277 | } |
7278 | } | |
7279 | if (iv >= 0) { | |
7280 | uv = iv; | |
7281 | if (plus) | |
7282 | esignbuf[esignlen++] = plus; | |
7283 | } | |
7284 | else { | |
7285 | uv = -iv; | |
7286 | esignbuf[esignlen++] = '-'; | |
7287 | } | |
7288 | base = 10; | |
7289 | goto integer; | |
7290 | ||
fc36a67e | 7291 | case 'U': |
29fe7a80 | 7292 | #ifdef IV_IS_QUAD |
22f3ae8c | 7293 | intsize = 'q'; |
29fe7a80 | 7294 | #else |
fc36a67e | 7295 | intsize = 'l'; |
29fe7a80 | 7296 | #endif |
fc36a67e | 7297 | /* FALL THROUGH */ |
7298 | case 'u': | |
7299 | base = 10; | |
7300 | goto uns_integer; | |
7301 | ||
4f19785b WSI |
7302 | case 'b': |
7303 | base = 2; | |
7304 | goto uns_integer; | |
7305 | ||
46fc3d4c | 7306 | case 'O': |
29fe7a80 | 7307 | #ifdef IV_IS_QUAD |
22f3ae8c | 7308 | intsize = 'q'; |
29fe7a80 | 7309 | #else |
46fc3d4c | 7310 | intsize = 'l'; |
29fe7a80 | 7311 | #endif |
46fc3d4c | 7312 | /* FALL THROUGH */ |
7313 | case 'o': | |
7314 | base = 8; | |
7315 | goto uns_integer; | |
7316 | ||
7317 | case 'X': | |
46fc3d4c | 7318 | case 'x': |
7319 | base = 16; | |
46fc3d4c | 7320 | |
7321 | uns_integer: | |
b22c7a20 | 7322 | if (vectorize) { |
ba210ebe | 7323 | STRLEN ulen; |
b22c7a20 | 7324 | vector: |
211dfcf1 HS |
7325 | if (!veclen) |
7326 | continue; | |
b2e23cf9 | 7327 | if (vec_utf) |
9041c2e3 | 7328 | uv = utf8n_to_uvchr(vecstr, veclen, &ulen, 0); |
b22c7a20 | 7329 | else { |
a05b299f | 7330 | uv = *vecstr; |
b22c7a20 GS |
7331 | ulen = 1; |
7332 | } | |
7333 | vecstr += ulen; | |
7334 | veclen -= ulen; | |
7335 | } | |
7336 | else if (args) { | |
46fc3d4c | 7337 | switch (intsize) { |
7338 | case 'h': uv = (unsigned short)va_arg(*args, unsigned); break; | |
7339 | default: uv = va_arg(*args, unsigned); break; | |
7340 | case 'l': uv = va_arg(*args, unsigned long); break; | |
fc36a67e | 7341 | case 'V': uv = va_arg(*args, UV); break; |
cf2093f6 JH |
7342 | #ifdef HAS_QUAD |
7343 | case 'q': uv = va_arg(*args, Quad_t); break; | |
7344 | #endif | |
46fc3d4c | 7345 | } |
7346 | } | |
7347 | else { | |
211dfcf1 | 7348 | uv = SvUVx(argsv); |
46fc3d4c | 7349 | switch (intsize) { |
7350 | case 'h': uv = (unsigned short)uv; break; | |
be28567c | 7351 | default: break; |
46fc3d4c | 7352 | case 'l': uv = (unsigned long)uv; break; |
fc36a67e | 7353 | case 'V': break; |
cf2093f6 JH |
7354 | #ifdef HAS_QUAD |
7355 | case 'q': uv = (Quad_t)uv; break; | |
7356 | #endif | |
46fc3d4c | 7357 | } |
7358 | } | |
7359 | ||
7360 | integer: | |
46fc3d4c | 7361 | eptr = ebuf + sizeof ebuf; |
fc36a67e | 7362 | switch (base) { |
7363 | unsigned dig; | |
7364 | case 16: | |
c10ed8b9 HS |
7365 | if (!uv) |
7366 | alt = FALSE; | |
1d7c1841 GS |
7367 | p = (char*)((c == 'X') |
7368 | ? "0123456789ABCDEF" : "0123456789abcdef"); | |
fc36a67e | 7369 | do { |
7370 | dig = uv & 15; | |
7371 | *--eptr = p[dig]; | |
7372 | } while (uv >>= 4); | |
7373 | if (alt) { | |
46fc3d4c | 7374 | esignbuf[esignlen++] = '0'; |
fc36a67e | 7375 | esignbuf[esignlen++] = c; /* 'x' or 'X' */ |
46fc3d4c | 7376 | } |
fc36a67e | 7377 | break; |
7378 | case 8: | |
7379 | do { | |
7380 | dig = uv & 7; | |
7381 | *--eptr = '0' + dig; | |
7382 | } while (uv >>= 3); | |
7383 | if (alt && *eptr != '0') | |
7384 | *--eptr = '0'; | |
7385 | break; | |
4f19785b WSI |
7386 | case 2: |
7387 | do { | |
7388 | dig = uv & 1; | |
7389 | *--eptr = '0' + dig; | |
7390 | } while (uv >>= 1); | |
eda88b6d JH |
7391 | if (alt) { |
7392 | esignbuf[esignlen++] = '0'; | |
7481bb52 | 7393 | esignbuf[esignlen++] = 'b'; |
eda88b6d | 7394 | } |
4f19785b | 7395 | break; |
fc36a67e | 7396 | default: /* it had better be ten or less */ |
6bc102ca | 7397 | #if defined(PERL_Y2KWARN) |
e476b1b5 | 7398 | if (ckWARN(WARN_Y2K)) { |
6bc102ca GS |
7399 | STRLEN n; |
7400 | char *s = SvPV(sv,n); | |
7401 | if (n >= 2 && s[n-2] == '1' && s[n-1] == '9' | |
7402 | && (n == 2 || !isDIGIT(s[n-3]))) | |
7403 | { | |
e476b1b5 | 7404 | Perl_warner(aTHX_ WARN_Y2K, |
6bc102ca GS |
7405 | "Possible Y2K bug: %%%c %s", |
7406 | c, "format string following '19'"); | |
7407 | } | |
7408 | } | |
7409 | #endif | |
fc36a67e | 7410 | do { |
7411 | dig = uv % base; | |
7412 | *--eptr = '0' + dig; | |
7413 | } while (uv /= base); | |
7414 | break; | |
46fc3d4c | 7415 | } |
7416 | elen = (ebuf + sizeof ebuf) - eptr; | |
c10ed8b9 HS |
7417 | if (has_precis) { |
7418 | if (precis > elen) | |
7419 | zeros = precis - elen; | |
7420 | else if (precis == 0 && elen == 1 && *eptr == '0') | |
7421 | elen = 0; | |
7422 | } | |
46fc3d4c | 7423 | break; |
7424 | ||
7425 | /* FLOATING POINT */ | |
7426 | ||
fc36a67e | 7427 | case 'F': |
7428 | c = 'f'; /* maybe %F isn't supported here */ | |
7429 | /* FALL THROUGH */ | |
46fc3d4c | 7430 | case 'e': case 'E': |
fc36a67e | 7431 | case 'f': |
46fc3d4c | 7432 | case 'g': case 'G': |
7433 | ||
7434 | /* This is evil, but floating point is even more evil */ | |
7435 | ||
b22c7a20 | 7436 | vectorize = FALSE; |
211dfcf1 | 7437 | nv = args ? va_arg(*args, NV) : SvNVx(argsv); |
fc36a67e | 7438 | |
7439 | need = 0; | |
7440 | if (c != 'e' && c != 'E') { | |
7441 | i = PERL_INT_MIN; | |
73b309ea | 7442 | (void)Perl_frexp(nv, &i); |
fc36a67e | 7443 | if (i == PERL_INT_MIN) |
cea2e8a9 | 7444 | Perl_die(aTHX_ "panic: frexp"); |
c635e13b | 7445 | if (i > 0) |
fc36a67e | 7446 | need = BIT_DIGITS(i); |
7447 | } | |
7448 | need += has_precis ? precis : 6; /* known default */ | |
7449 | if (need < width) | |
7450 | need = width; | |
7451 | ||
46fc3d4c | 7452 | need += 20; /* fudge factor */ |
80252599 GS |
7453 | if (PL_efloatsize < need) { |
7454 | Safefree(PL_efloatbuf); | |
7455 | PL_efloatsize = need + 20; /* more fudge */ | |
7456 | New(906, PL_efloatbuf, PL_efloatsize, char); | |
7d5ea4e7 | 7457 | PL_efloatbuf[0] = '\0'; |
46fc3d4c | 7458 | } |
7459 | ||
7460 | eptr = ebuf + sizeof ebuf; | |
7461 | *--eptr = '\0'; | |
7462 | *--eptr = c; | |
e5c81feb | 7463 | #if defined(USE_LONG_DOUBLE) && defined(PERL_PRIfldbl) |
cf2093f6 | 7464 | { |
e5c81feb JH |
7465 | /* Copy the one or more characters in a long double |
7466 | * format before the 'base' ([efgEFG]) character to | |
7467 | * the format string. */ | |
7468 | static char const prifldbl[] = PERL_PRIfldbl; | |
7469 | char const *p = prifldbl + sizeof(prifldbl) - 3; | |
7470 | while (p >= prifldbl) { *--eptr = *p--; } | |
cf2093f6 | 7471 | } |
65202027 | 7472 | #endif |
46fc3d4c | 7473 | if (has_precis) { |
7474 | base = precis; | |
7475 | do { *--eptr = '0' + (base % 10); } while (base /= 10); | |
7476 | *--eptr = '.'; | |
7477 | } | |
7478 | if (width) { | |
7479 | base = width; | |
7480 | do { *--eptr = '0' + (base % 10); } while (base /= 10); | |
7481 | } | |
7482 | if (fill == '0') | |
7483 | *--eptr = fill; | |
84902520 TB |
7484 | if (left) |
7485 | *--eptr = '-'; | |
46fc3d4c | 7486 | if (plus) |
7487 | *--eptr = plus; | |
7488 | if (alt) | |
7489 | *--eptr = '#'; | |
7490 | *--eptr = '%'; | |
7491 | ||
ff9121f8 JH |
7492 | /* No taint. Otherwise we are in the strange situation |
7493 | * where printf() taints but print($float) doesn't. | |
bda0f7a5 | 7494 | * --jhi */ |
dd8482fc | 7495 | (void)sprintf(PL_efloatbuf, eptr, nv); |
8af02333 | 7496 | |
80252599 GS |
7497 | eptr = PL_efloatbuf; |
7498 | elen = strlen(PL_efloatbuf); | |
46fc3d4c | 7499 | break; |
7500 | ||
fc36a67e | 7501 | /* SPECIAL */ |
7502 | ||
7503 | case 'n': | |
b22c7a20 | 7504 | vectorize = FALSE; |
fc36a67e | 7505 | i = SvCUR(sv) - origlen; |
7506 | if (args) { | |
c635e13b | 7507 | switch (intsize) { |
7508 | case 'h': *(va_arg(*args, short*)) = i; break; | |
7509 | default: *(va_arg(*args, int*)) = i; break; | |
7510 | case 'l': *(va_arg(*args, long*)) = i; break; | |
7511 | case 'V': *(va_arg(*args, IV*)) = i; break; | |
cf2093f6 JH |
7512 | #ifdef HAS_QUAD |
7513 | case 'q': *(va_arg(*args, Quad_t*)) = i; break; | |
7514 | #endif | |
c635e13b | 7515 | } |
fc36a67e | 7516 | } |
9dd79c3f | 7517 | else |
211dfcf1 | 7518 | sv_setuv_mg(argsv, (UV)i); |
fc36a67e | 7519 | continue; /* not "break" */ |
7520 | ||
7521 | /* UNKNOWN */ | |
7522 | ||
46fc3d4c | 7523 | default: |
fc36a67e | 7524 | unknown: |
b22c7a20 | 7525 | vectorize = FALSE; |
599cee73 | 7526 | if (!args && ckWARN(WARN_PRINTF) && |
533c011a | 7527 | (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF)) { |
c635e13b | 7528 | SV *msg = sv_newmortal(); |
cea2e8a9 | 7529 | Perl_sv_setpvf(aTHX_ msg, "Invalid conversion in %s: ", |
533c011a | 7530 | (PL_op->op_type == OP_PRTF) ? "printf" : "sprintf"); |
0f4b6630 | 7531 | if (c) { |
0f4b6630 | 7532 | if (isPRINT(c)) |
1c846c1f | 7533 | Perl_sv_catpvf(aTHX_ msg, |
0f4b6630 JH |
7534 | "\"%%%c\"", c & 0xFF); |
7535 | else | |
7536 | Perl_sv_catpvf(aTHX_ msg, | |
57def98f | 7537 | "\"%%\\%03"UVof"\"", |
0f4b6630 | 7538 | (UV)c & 0xFF); |
0f4b6630 | 7539 | } else |
c635e13b | 7540 | sv_catpv(msg, "end of string"); |
894356b3 | 7541 | Perl_warner(aTHX_ WARN_PRINTF, "%"SVf, msg); /* yes, this is reentrant */ |
c635e13b | 7542 | } |
fb73857a | 7543 | |
7544 | /* output mangled stuff ... */ | |
7545 | if (c == '\0') | |
7546 | --q; | |
46fc3d4c | 7547 | eptr = p; |
7548 | elen = q - p; | |
fb73857a | 7549 | |
7550 | /* ... right here, because formatting flags should not apply */ | |
7551 | SvGROW(sv, SvCUR(sv) + elen + 1); | |
7552 | p = SvEND(sv); | |
4459522c | 7553 | Copy(eptr, p, elen, char); |
fb73857a | 7554 | p += elen; |
7555 | *p = '\0'; | |
7556 | SvCUR(sv) = p - SvPVX(sv); | |
7557 | continue; /* not "break" */ | |
46fc3d4c | 7558 | } |
7559 | ||
fc36a67e | 7560 | have = esignlen + zeros + elen; |
46fc3d4c | 7561 | need = (have > width ? have : width); |
7562 | gap = need - have; | |
7563 | ||
b22c7a20 | 7564 | SvGROW(sv, SvCUR(sv) + need + dotstrlen + 1); |
46fc3d4c | 7565 | p = SvEND(sv); |
7566 | if (esignlen && fill == '0') { | |
7567 | for (i = 0; i < esignlen; i++) | |
7568 | *p++ = esignbuf[i]; | |
7569 | } | |
7570 | if (gap && !left) { | |
7571 | memset(p, fill, gap); | |
7572 | p += gap; | |
7573 | } | |
7574 | if (esignlen && fill != '0') { | |
7575 | for (i = 0; i < esignlen; i++) | |
7576 | *p++ = esignbuf[i]; | |
7577 | } | |
fc36a67e | 7578 | if (zeros) { |
7579 | for (i = zeros; i; i--) | |
7580 | *p++ = '0'; | |
7581 | } | |
46fc3d4c | 7582 | if (elen) { |
4459522c | 7583 | Copy(eptr, p, elen, char); |
46fc3d4c | 7584 | p += elen; |
7585 | } | |
7586 | if (gap && left) { | |
7587 | memset(p, ' ', gap); | |
7588 | p += gap; | |
7589 | } | |
b22c7a20 GS |
7590 | if (vectorize) { |
7591 | if (veclen) { | |
4459522c | 7592 | Copy(dotstr, p, dotstrlen, char); |
b22c7a20 GS |
7593 | p += dotstrlen; |
7594 | } | |
7595 | else | |
7596 | vectorize = FALSE; /* done iterating over vecstr */ | |
7597 | } | |
7e2040f0 GS |
7598 | if (is_utf) |
7599 | SvUTF8_on(sv); | |
46fc3d4c | 7600 | *p = '\0'; |
7601 | SvCUR(sv) = p - SvPVX(sv); | |
b22c7a20 GS |
7602 | if (vectorize) { |
7603 | esignlen = 0; | |
7604 | goto vector; | |
7605 | } | |
46fc3d4c | 7606 | } |
7607 | } | |
51371543 | 7608 | |
1d7c1841 GS |
7609 | #if defined(USE_ITHREADS) |
7610 | ||
7611 | #if defined(USE_THREADS) | |
7612 | # include "error: USE_THREADS and USE_ITHREADS are incompatible" | |
7613 | #endif | |
7614 | ||
1d7c1841 GS |
7615 | #ifndef GpREFCNT_inc |
7616 | # define GpREFCNT_inc(gp) ((gp) ? (++(gp)->gp_refcnt, (gp)) : (GP*)NULL) | |
7617 | #endif | |
7618 | ||
7619 | ||
7620 | #define sv_dup_inc(s) SvREFCNT_inc(sv_dup(s)) | |
7621 | #define av_dup(s) (AV*)sv_dup((SV*)s) | |
7622 | #define av_dup_inc(s) (AV*)SvREFCNT_inc(sv_dup((SV*)s)) | |
7623 | #define hv_dup(s) (HV*)sv_dup((SV*)s) | |
7624 | #define hv_dup_inc(s) (HV*)SvREFCNT_inc(sv_dup((SV*)s)) | |
7625 | #define cv_dup(s) (CV*)sv_dup((SV*)s) | |
7626 | #define cv_dup_inc(s) (CV*)SvREFCNT_inc(sv_dup((SV*)s)) | |
7627 | #define io_dup(s) (IO*)sv_dup((SV*)s) | |
7628 | #define io_dup_inc(s) (IO*)SvREFCNT_inc(sv_dup((SV*)s)) | |
7629 | #define gv_dup(s) (GV*)sv_dup((SV*)s) | |
7630 | #define gv_dup_inc(s) (GV*)SvREFCNT_inc(sv_dup((SV*)s)) | |
7631 | #define SAVEPV(p) (p ? savepv(p) : Nullch) | |
7632 | #define SAVEPVN(p,n) (p ? savepvn(p,n) : Nullch) | |
7633 | ||
7634 | REGEXP * | |
7635 | Perl_re_dup(pTHX_ REGEXP *r) | |
7636 | { | |
7637 | /* XXX fix when pmop->op_pmregexp becomes shared */ | |
7638 | return ReREFCNT_inc(r); | |
7639 | } | |
7640 | ||
7641 | PerlIO * | |
7642 | Perl_fp_dup(pTHX_ PerlIO *fp, char type) | |
7643 | { | |
7644 | PerlIO *ret; | |
7645 | if (!fp) | |
7646 | return (PerlIO*)NULL; | |
7647 | ||
7648 | /* look for it in the table first */ | |
7649 | ret = (PerlIO*)ptr_table_fetch(PL_ptr_table, fp); | |
7650 | if (ret) | |
7651 | return ret; | |
7652 | ||
7653 | /* create anew and remember what it is */ | |
5f1a76d0 | 7654 | ret = PerlIO_fdupopen(aTHX_ fp); |
1d7c1841 GS |
7655 | ptr_table_store(PL_ptr_table, fp, ret); |
7656 | return ret; | |
7657 | } | |
7658 | ||
7659 | DIR * | |
7660 | Perl_dirp_dup(pTHX_ DIR *dp) | |
7661 | { | |
7662 | if (!dp) | |
7663 | return (DIR*)NULL; | |
7664 | /* XXX TODO */ | |
7665 | return dp; | |
7666 | } | |
7667 | ||
7668 | GP * | |
7669 | Perl_gp_dup(pTHX_ GP *gp) | |
7670 | { | |
7671 | GP *ret; | |
7672 | if (!gp) | |
7673 | return (GP*)NULL; | |
7674 | /* look for it in the table first */ | |
7675 | ret = (GP*)ptr_table_fetch(PL_ptr_table, gp); | |
7676 | if (ret) | |
7677 | return ret; | |
7678 | ||
7679 | /* create anew and remember what it is */ | |
7680 | Newz(0, ret, 1, GP); | |
7681 | ptr_table_store(PL_ptr_table, gp, ret); | |
7682 | ||
7683 | /* clone */ | |
7684 | ret->gp_refcnt = 0; /* must be before any other dups! */ | |
7685 | ret->gp_sv = sv_dup_inc(gp->gp_sv); | |
7686 | ret->gp_io = io_dup_inc(gp->gp_io); | |
7687 | ret->gp_form = cv_dup_inc(gp->gp_form); | |
7688 | ret->gp_av = av_dup_inc(gp->gp_av); | |
7689 | ret->gp_hv = hv_dup_inc(gp->gp_hv); | |
7690 | ret->gp_egv = gv_dup(gp->gp_egv); /* GvEGV is not refcounted */ | |
7691 | ret->gp_cv = cv_dup_inc(gp->gp_cv); | |
7692 | ret->gp_cvgen = gp->gp_cvgen; | |
7693 | ret->gp_flags = gp->gp_flags; | |
7694 | ret->gp_line = gp->gp_line; | |
7695 | ret->gp_file = gp->gp_file; /* points to COP.cop_file */ | |
7696 | return ret; | |
7697 | } | |
7698 | ||
7699 | MAGIC * | |
7700 | Perl_mg_dup(pTHX_ MAGIC *mg) | |
7701 | { | |
cb359b41 JH |
7702 | MAGIC *mgprev = (MAGIC*)NULL; |
7703 | MAGIC *mgret; | |
1d7c1841 GS |
7704 | if (!mg) |
7705 | return (MAGIC*)NULL; | |
7706 | /* look for it in the table first */ | |
7707 | mgret = (MAGIC*)ptr_table_fetch(PL_ptr_table, mg); | |
7708 | if (mgret) | |
7709 | return mgret; | |
7710 | ||
7711 | for (; mg; mg = mg->mg_moremagic) { | |
7712 | MAGIC *nmg; | |
7713 | Newz(0, nmg, 1, MAGIC); | |
cb359b41 | 7714 | if (mgprev) |
1d7c1841 | 7715 | mgprev->mg_moremagic = nmg; |
cb359b41 JH |
7716 | else |
7717 | mgret = nmg; | |
1d7c1841 GS |
7718 | nmg->mg_virtual = mg->mg_virtual; /* XXX copy dynamic vtable? */ |
7719 | nmg->mg_private = mg->mg_private; | |
7720 | nmg->mg_type = mg->mg_type; | |
7721 | nmg->mg_flags = mg->mg_flags; | |
14befaf4 | 7722 | if (mg->mg_type == PERL_MAGIC_qr) { |
1d7c1841 GS |
7723 | nmg->mg_obj = (SV*)re_dup((REGEXP*)mg->mg_obj); |
7724 | } | |
7725 | else { | |
7726 | nmg->mg_obj = (mg->mg_flags & MGf_REFCOUNTED) | |
7727 | ? sv_dup_inc(mg->mg_obj) | |
7728 | : sv_dup(mg->mg_obj); | |
7729 | } | |
7730 | nmg->mg_len = mg->mg_len; | |
7731 | nmg->mg_ptr = mg->mg_ptr; /* XXX random ptr? */ | |
14befaf4 | 7732 | if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) { |
1d7c1841 GS |
7733 | if (mg->mg_len >= 0) { |
7734 | nmg->mg_ptr = SAVEPVN(mg->mg_ptr, mg->mg_len); | |
14befaf4 DM |
7735 | if (mg->mg_type == PERL_MAGIC_overload_table && |
7736 | AMT_AMAGIC((AMT*)mg->mg_ptr)) | |
7737 | { | |
1d7c1841 GS |
7738 | AMT *amtp = (AMT*)mg->mg_ptr; |
7739 | AMT *namtp = (AMT*)nmg->mg_ptr; | |
7740 | I32 i; | |
7741 | for (i = 1; i < NofAMmeth; i++) { | |
7742 | namtp->table[i] = cv_dup_inc(amtp->table[i]); | |
7743 | } | |
7744 | } | |
7745 | } | |
7746 | else if (mg->mg_len == HEf_SVKEY) | |
7747 | nmg->mg_ptr = (char*)sv_dup_inc((SV*)mg->mg_ptr); | |
7748 | } | |
7749 | mgprev = nmg; | |
7750 | } | |
7751 | return mgret; | |
7752 | } | |
7753 | ||
7754 | PTR_TBL_t * | |
7755 | Perl_ptr_table_new(pTHX) | |
7756 | { | |
7757 | PTR_TBL_t *tbl; | |
7758 | Newz(0, tbl, 1, PTR_TBL_t); | |
7759 | tbl->tbl_max = 511; | |
7760 | tbl->tbl_items = 0; | |
7761 | Newz(0, tbl->tbl_ary, tbl->tbl_max + 1, PTR_TBL_ENT_t*); | |
7762 | return tbl; | |
7763 | } | |
7764 | ||
7765 | void * | |
7766 | Perl_ptr_table_fetch(pTHX_ PTR_TBL_t *tbl, void *sv) | |
7767 | { | |
7768 | PTR_TBL_ENT_t *tblent; | |
d2a79402 | 7769 | UV hash = PTR2UV(sv); |
1d7c1841 GS |
7770 | assert(tbl); |
7771 | tblent = tbl->tbl_ary[hash & tbl->tbl_max]; | |
7772 | for (; tblent; tblent = tblent->next) { | |
7773 | if (tblent->oldval == sv) | |
7774 | return tblent->newval; | |
7775 | } | |
7776 | return (void*)NULL; | |
7777 | } | |
7778 | ||
7779 | void | |
7780 | Perl_ptr_table_store(pTHX_ PTR_TBL_t *tbl, void *oldv, void *newv) | |
7781 | { | |
7782 | PTR_TBL_ENT_t *tblent, **otblent; | |
7783 | /* XXX this may be pessimal on platforms where pointers aren't good | |
7784 | * hash values e.g. if they grow faster in the most significant | |
7785 | * bits */ | |
d2a79402 | 7786 | UV hash = PTR2UV(oldv); |
1d7c1841 GS |
7787 | bool i = 1; |
7788 | ||
7789 | assert(tbl); | |
7790 | otblent = &tbl->tbl_ary[hash & tbl->tbl_max]; | |
7791 | for (tblent = *otblent; tblent; i=0, tblent = tblent->next) { | |
7792 | if (tblent->oldval == oldv) { | |
7793 | tblent->newval = newv; | |
7794 | tbl->tbl_items++; | |
7795 | return; | |
7796 | } | |
7797 | } | |
7798 | Newz(0, tblent, 1, PTR_TBL_ENT_t); | |
7799 | tblent->oldval = oldv; | |
7800 | tblent->newval = newv; | |
7801 | tblent->next = *otblent; | |
7802 | *otblent = tblent; | |
7803 | tbl->tbl_items++; | |
7804 | if (i && tbl->tbl_items > tbl->tbl_max) | |
7805 | ptr_table_split(tbl); | |
7806 | } | |
7807 | ||
7808 | void | |
7809 | Perl_ptr_table_split(pTHX_ PTR_TBL_t *tbl) | |
7810 | { | |
7811 | PTR_TBL_ENT_t **ary = tbl->tbl_ary; | |
7812 | UV oldsize = tbl->tbl_max + 1; | |
7813 | UV newsize = oldsize * 2; | |
7814 | UV i; | |
7815 | ||
7816 | Renew(ary, newsize, PTR_TBL_ENT_t*); | |
7817 | Zero(&ary[oldsize], newsize-oldsize, PTR_TBL_ENT_t*); | |
7818 | tbl->tbl_max = --newsize; | |
7819 | tbl->tbl_ary = ary; | |
7820 | for (i=0; i < oldsize; i++, ary++) { | |
7821 | PTR_TBL_ENT_t **curentp, **entp, *ent; | |
7822 | if (!*ary) | |
7823 | continue; | |
7824 | curentp = ary + oldsize; | |
7825 | for (entp = ary, ent = *ary; ent; ent = *entp) { | |
d2a79402 | 7826 | if ((newsize & PTR2UV(ent->oldval)) != i) { |
1d7c1841 GS |
7827 | *entp = ent->next; |
7828 | ent->next = *curentp; | |
7829 | *curentp = ent; | |
7830 | continue; | |
7831 | } | |
7832 | else | |
7833 | entp = &ent->next; | |
7834 | } | |
7835 | } | |
7836 | } | |
7837 | ||
a0739874 DM |
7838 | void |
7839 | Perl_ptr_table_clear(pTHX_ PTR_TBL_t *tbl) | |
7840 | { | |
7841 | register PTR_TBL_ENT_t **array; | |
7842 | register PTR_TBL_ENT_t *entry; | |
7843 | register PTR_TBL_ENT_t *oentry = Null(PTR_TBL_ENT_t*); | |
7844 | UV riter = 0; | |
7845 | UV max; | |
7846 | ||
7847 | if (!tbl || !tbl->tbl_items) { | |
7848 | return; | |
7849 | } | |
7850 | ||
7851 | array = tbl->tbl_ary; | |
7852 | entry = array[0]; | |
7853 | max = tbl->tbl_max; | |
7854 | ||
7855 | for (;;) { | |
7856 | if (entry) { | |
7857 | oentry = entry; | |
7858 | entry = entry->next; | |
7859 | Safefree(oentry); | |
7860 | } | |
7861 | if (!entry) { | |
7862 | if (++riter > max) { | |
7863 | break; | |
7864 | } | |
7865 | entry = array[riter]; | |
7866 | } | |
7867 | } | |
7868 | ||
7869 | tbl->tbl_items = 0; | |
7870 | } | |
7871 | ||
7872 | void | |
7873 | Perl_ptr_table_free(pTHX_ PTR_TBL_t *tbl) | |
7874 | { | |
7875 | if (!tbl) { | |
7876 | return; | |
7877 | } | |
7878 | ptr_table_clear(tbl); | |
7879 | Safefree(tbl->tbl_ary); | |
7880 | Safefree(tbl); | |
7881 | } | |
7882 | ||
1d7c1841 GS |
7883 | #ifdef DEBUGGING |
7884 | char *PL_watch_pvx; | |
7885 | #endif | |
7886 | ||
5bd07a3d DM |
7887 | STATIC SV * |
7888 | S_gv_share(pTHX_ SV *sstr) | |
7889 | { | |
7890 | GV *gv = (GV*)sstr; | |
7891 | SV *sv = &PL_sv_no; /* just need SvREADONLY-ness */ | |
7892 | ||
7893 | if (GvIO(gv) || GvFORM(gv)) { | |
7894 | GvSHARED_off(gv); /* GvIOs cannot be shared. nor can GvFORMs */ | |
7895 | } | |
7896 | else if (!GvCV(gv)) { | |
7897 | GvCV(gv) = (CV*)sv; | |
7898 | } | |
7899 | else { | |
7900 | /* CvPADLISTs cannot be shared */ | |
7901 | if (!CvXSUB(GvCV(gv))) { | |
7902 | GvSHARED_off(gv); | |
7903 | } | |
7904 | } | |
7905 | ||
7906 | if (!GvSHARED(gv)) { | |
7907 | #if 0 | |
7908 | PerlIO_printf(Perl_debug_log, "gv_share: unable to share %s::%s\n", | |
7909 | HvNAME(GvSTASH(gv)), GvNAME(gv)); | |
7910 | #endif | |
7911 | return Nullsv; | |
7912 | } | |
7913 | ||
4411f3b6 | 7914 | /* |
5bd07a3d DM |
7915 | * write attempts will die with |
7916 | * "Modification of a read-only value attempted" | |
7917 | */ | |
7918 | if (!GvSV(gv)) { | |
7919 | GvSV(gv) = sv; | |
7920 | } | |
7921 | else { | |
7922 | SvREADONLY_on(GvSV(gv)); | |
7923 | } | |
7924 | ||
7925 | if (!GvAV(gv)) { | |
7926 | GvAV(gv) = (AV*)sv; | |
7927 | } | |
7928 | else { | |
7929 | SvREADONLY_on(GvAV(gv)); | |
7930 | } | |
7931 | ||
7932 | if (!GvHV(gv)) { | |
7933 | GvHV(gv) = (HV*)sv; | |
7934 | } | |
7935 | else { | |
7936 | SvREADONLY_on(GvAV(gv)); | |
7937 | } | |
7938 | ||
7939 | return sstr; /* he_dup() will SvREFCNT_inc() */ | |
7940 | } | |
7941 | ||
1d7c1841 GS |
7942 | SV * |
7943 | Perl_sv_dup(pTHX_ SV *sstr) | |
7944 | { | |
1d7c1841 GS |
7945 | SV *dstr; |
7946 | ||
7947 | if (!sstr || SvTYPE(sstr) == SVTYPEMASK) | |
7948 | return Nullsv; | |
7949 | /* look for it in the table first */ | |
7950 | dstr = (SV*)ptr_table_fetch(PL_ptr_table, sstr); | |
7951 | if (dstr) | |
7952 | return dstr; | |
7953 | ||
7954 | /* create anew and remember what it is */ | |
7955 | new_SV(dstr); | |
7956 | ptr_table_store(PL_ptr_table, sstr, dstr); | |
7957 | ||
7958 | /* clone */ | |
7959 | SvFLAGS(dstr) = SvFLAGS(sstr); | |
7960 | SvFLAGS(dstr) &= ~SVf_OOK; /* don't propagate OOK hack */ | |
7961 | SvREFCNT(dstr) = 0; /* must be before any other dups! */ | |
7962 | ||
7963 | #ifdef DEBUGGING | |
7964 | if (SvANY(sstr) && PL_watch_pvx && SvPVX(sstr) == PL_watch_pvx) | |
7965 | PerlIO_printf(Perl_debug_log, "watch at %p hit, found string \"%s\"\n", | |
7966 | PL_watch_pvx, SvPVX(sstr)); | |
7967 | #endif | |
7968 | ||
7969 | switch (SvTYPE(sstr)) { | |
7970 | case SVt_NULL: | |
7971 | SvANY(dstr) = NULL; | |
7972 | break; | |
7973 | case SVt_IV: | |
7974 | SvANY(dstr) = new_XIV(); | |
7975 | SvIVX(dstr) = SvIVX(sstr); | |
7976 | break; | |
7977 | case SVt_NV: | |
7978 | SvANY(dstr) = new_XNV(); | |
7979 | SvNVX(dstr) = SvNVX(sstr); | |
7980 | break; | |
7981 | case SVt_RV: | |
7982 | SvANY(dstr) = new_XRV(); | |
ce4ad881 RS |
7983 | SvRV(dstr) = SvWEAKREF(SvRV(sstr)) |
7984 | ? sv_dup_inc(SvRV(sstr)) | |
7985 | : sv_dup(SvRV(sstr)); | |
1d7c1841 GS |
7986 | break; |
7987 | case SVt_PV: | |
7988 | SvANY(dstr) = new_XPV(); | |
7989 | SvCUR(dstr) = SvCUR(sstr); | |
7990 | SvLEN(dstr) = SvLEN(sstr); | |
7991 | if (SvROK(sstr)) | |
ce4ad881 RS |
7992 | SvRV(dstr) = SvWEAKREF(SvRV(sstr)) |
7993 | ? sv_dup_inc(SvRV(sstr)) | |
7994 | : sv_dup(SvRV(sstr)); | |
1d7c1841 GS |
7995 | else if (SvPVX(sstr) && SvLEN(sstr)) |
7996 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
7997 | else | |
7998 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
7999 | break; | |
8000 | case SVt_PVIV: | |
8001 | SvANY(dstr) = new_XPVIV(); | |
8002 | SvCUR(dstr) = SvCUR(sstr); | |
8003 | SvLEN(dstr) = SvLEN(sstr); | |
8004 | SvIVX(dstr) = SvIVX(sstr); | |
8005 | if (SvROK(sstr)) | |
ce4ad881 RS |
8006 | SvRV(dstr) = SvWEAKREF(SvRV(sstr)) |
8007 | ? sv_dup_inc(SvRV(sstr)) | |
8008 | : sv_dup(SvRV(sstr)); | |
1d7c1841 GS |
8009 | else if (SvPVX(sstr) && SvLEN(sstr)) |
8010 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
8011 | else | |
8012 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
8013 | break; | |
8014 | case SVt_PVNV: | |
8015 | SvANY(dstr) = new_XPVNV(); | |
8016 | SvCUR(dstr) = SvCUR(sstr); | |
8017 | SvLEN(dstr) = SvLEN(sstr); | |
8018 | SvIVX(dstr) = SvIVX(sstr); | |
8019 | SvNVX(dstr) = SvNVX(sstr); | |
8020 | if (SvROK(sstr)) | |
ce4ad881 RS |
8021 | SvRV(dstr) = SvWEAKREF(SvRV(sstr)) |
8022 | ? sv_dup_inc(SvRV(sstr)) | |
8023 | : sv_dup(SvRV(sstr)); | |
1d7c1841 GS |
8024 | else if (SvPVX(sstr) && SvLEN(sstr)) |
8025 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
8026 | else | |
8027 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
8028 | break; | |
8029 | case SVt_PVMG: | |
8030 | SvANY(dstr) = new_XPVMG(); | |
8031 | SvCUR(dstr) = SvCUR(sstr); | |
8032 | SvLEN(dstr) = SvLEN(sstr); | |
8033 | SvIVX(dstr) = SvIVX(sstr); | |
8034 | SvNVX(dstr) = SvNVX(sstr); | |
8035 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
8036 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
8037 | if (SvROK(sstr)) | |
ce4ad881 RS |
8038 | SvRV(dstr) = SvWEAKREF(SvRV(sstr)) |
8039 | ? sv_dup_inc(SvRV(sstr)) | |
8040 | : sv_dup(SvRV(sstr)); | |
1d7c1841 GS |
8041 | else if (SvPVX(sstr) && SvLEN(sstr)) |
8042 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
8043 | else | |
8044 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
8045 | break; | |
8046 | case SVt_PVBM: | |
8047 | SvANY(dstr) = new_XPVBM(); | |
8048 | SvCUR(dstr) = SvCUR(sstr); | |
8049 | SvLEN(dstr) = SvLEN(sstr); | |
8050 | SvIVX(dstr) = SvIVX(sstr); | |
8051 | SvNVX(dstr) = SvNVX(sstr); | |
8052 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
8053 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
8054 | if (SvROK(sstr)) | |
ce4ad881 RS |
8055 | SvRV(dstr) = SvWEAKREF(SvRV(sstr)) |
8056 | ? sv_dup_inc(SvRV(sstr)) | |
8057 | : sv_dup(SvRV(sstr)); | |
1d7c1841 GS |
8058 | else if (SvPVX(sstr) && SvLEN(sstr)) |
8059 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
8060 | else | |
8061 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
8062 | BmRARE(dstr) = BmRARE(sstr); | |
8063 | BmUSEFUL(dstr) = BmUSEFUL(sstr); | |
8064 | BmPREVIOUS(dstr)= BmPREVIOUS(sstr); | |
8065 | break; | |
8066 | case SVt_PVLV: | |
8067 | SvANY(dstr) = new_XPVLV(); | |
8068 | SvCUR(dstr) = SvCUR(sstr); | |
8069 | SvLEN(dstr) = SvLEN(sstr); | |
8070 | SvIVX(dstr) = SvIVX(sstr); | |
8071 | SvNVX(dstr) = SvNVX(sstr); | |
8072 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
8073 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
8074 | if (SvROK(sstr)) | |
ce4ad881 RS |
8075 | SvRV(dstr) = SvWEAKREF(SvRV(sstr)) |
8076 | ? sv_dup_inc(SvRV(sstr)) | |
8077 | : sv_dup(SvRV(sstr)); | |
1d7c1841 GS |
8078 | else if (SvPVX(sstr) && SvLEN(sstr)) |
8079 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
8080 | else | |
8081 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
8082 | LvTARGOFF(dstr) = LvTARGOFF(sstr); /* XXX sometimes holds PMOP* when DEBUGGING */ | |
8083 | LvTARGLEN(dstr) = LvTARGLEN(sstr); | |
8084 | LvTARG(dstr) = sv_dup_inc(LvTARG(sstr)); | |
8085 | LvTYPE(dstr) = LvTYPE(sstr); | |
8086 | break; | |
8087 | case SVt_PVGV: | |
5bd07a3d DM |
8088 | if (GvSHARED((GV*)sstr)) { |
8089 | SV *share; | |
8090 | if ((share = gv_share(sstr))) { | |
8091 | del_SV(dstr); | |
8092 | dstr = share; | |
8093 | #if 0 | |
8094 | PerlIO_printf(Perl_debug_log, "sv_dup: sharing %s::%s\n", | |
8095 | HvNAME(GvSTASH(share)), GvNAME(share)); | |
8096 | #endif | |
8097 | break; | |
8098 | } | |
8099 | } | |
1d7c1841 GS |
8100 | SvANY(dstr) = new_XPVGV(); |
8101 | SvCUR(dstr) = SvCUR(sstr); | |
8102 | SvLEN(dstr) = SvLEN(sstr); | |
8103 | SvIVX(dstr) = SvIVX(sstr); | |
8104 | SvNVX(dstr) = SvNVX(sstr); | |
8105 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
8106 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
8107 | if (SvROK(sstr)) | |
ce4ad881 RS |
8108 | SvRV(dstr) = SvWEAKREF(SvRV(sstr)) |
8109 | ? sv_dup_inc(SvRV(sstr)) | |
8110 | : sv_dup(SvRV(sstr)); | |
1d7c1841 GS |
8111 | else if (SvPVX(sstr) && SvLEN(sstr)) |
8112 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
8113 | else | |
8114 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
8115 | GvNAMELEN(dstr) = GvNAMELEN(sstr); | |
8116 | GvNAME(dstr) = SAVEPVN(GvNAME(sstr), GvNAMELEN(sstr)); | |
8117 | GvSTASH(dstr) = hv_dup_inc(GvSTASH(sstr)); | |
8118 | GvFLAGS(dstr) = GvFLAGS(sstr); | |
8119 | GvGP(dstr) = gp_dup(GvGP(sstr)); | |
8120 | (void)GpREFCNT_inc(GvGP(dstr)); | |
8121 | break; | |
8122 | case SVt_PVIO: | |
8123 | SvANY(dstr) = new_XPVIO(); | |
8124 | SvCUR(dstr) = SvCUR(sstr); | |
8125 | SvLEN(dstr) = SvLEN(sstr); | |
8126 | SvIVX(dstr) = SvIVX(sstr); | |
8127 | SvNVX(dstr) = SvNVX(sstr); | |
8128 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
8129 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
8130 | if (SvROK(sstr)) | |
ce4ad881 RS |
8131 | SvRV(dstr) = SvWEAKREF(SvRV(sstr)) |
8132 | ? sv_dup_inc(SvRV(sstr)) | |
8133 | : sv_dup(SvRV(sstr)); | |
1d7c1841 GS |
8134 | else if (SvPVX(sstr) && SvLEN(sstr)) |
8135 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
8136 | else | |
8137 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
8138 | IoIFP(dstr) = fp_dup(IoIFP(sstr), IoTYPE(sstr)); | |
8139 | if (IoOFP(sstr) == IoIFP(sstr)) | |
8140 | IoOFP(dstr) = IoIFP(dstr); | |
8141 | else | |
8142 | IoOFP(dstr) = fp_dup(IoOFP(sstr), IoTYPE(sstr)); | |
8143 | /* PL_rsfp_filters entries have fake IoDIRP() */ | |
8144 | if (IoDIRP(sstr) && !(IoFLAGS(sstr) & IOf_FAKE_DIRP)) | |
8145 | IoDIRP(dstr) = dirp_dup(IoDIRP(sstr)); | |
8146 | else | |
8147 | IoDIRP(dstr) = IoDIRP(sstr); | |
8148 | IoLINES(dstr) = IoLINES(sstr); | |
8149 | IoPAGE(dstr) = IoPAGE(sstr); | |
8150 | IoPAGE_LEN(dstr) = IoPAGE_LEN(sstr); | |
8151 | IoLINES_LEFT(dstr) = IoLINES_LEFT(sstr); | |
8152 | IoTOP_NAME(dstr) = SAVEPV(IoTOP_NAME(sstr)); | |
8153 | IoTOP_GV(dstr) = gv_dup(IoTOP_GV(sstr)); | |
8154 | IoFMT_NAME(dstr) = SAVEPV(IoFMT_NAME(sstr)); | |
8155 | IoFMT_GV(dstr) = gv_dup(IoFMT_GV(sstr)); | |
8156 | IoBOTTOM_NAME(dstr) = SAVEPV(IoBOTTOM_NAME(sstr)); | |
8157 | IoBOTTOM_GV(dstr) = gv_dup(IoBOTTOM_GV(sstr)); | |
8158 | IoSUBPROCESS(dstr) = IoSUBPROCESS(sstr); | |
8159 | IoTYPE(dstr) = IoTYPE(sstr); | |
8160 | IoFLAGS(dstr) = IoFLAGS(sstr); | |
8161 | break; | |
8162 | case SVt_PVAV: | |
8163 | SvANY(dstr) = new_XPVAV(); | |
8164 | SvCUR(dstr) = SvCUR(sstr); | |
8165 | SvLEN(dstr) = SvLEN(sstr); | |
8166 | SvIVX(dstr) = SvIVX(sstr); | |
8167 | SvNVX(dstr) = SvNVX(sstr); | |
8168 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
8169 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
8170 | AvARYLEN((AV*)dstr) = sv_dup_inc(AvARYLEN((AV*)sstr)); | |
8171 | AvFLAGS((AV*)dstr) = AvFLAGS((AV*)sstr); | |
8172 | if (AvARRAY((AV*)sstr)) { | |
8173 | SV **dst_ary, **src_ary; | |
8174 | SSize_t items = AvFILLp((AV*)sstr) + 1; | |
8175 | ||
8176 | src_ary = AvARRAY((AV*)sstr); | |
8177 | Newz(0, dst_ary, AvMAX((AV*)sstr)+1, SV*); | |
8178 | ptr_table_store(PL_ptr_table, src_ary, dst_ary); | |
8179 | SvPVX(dstr) = (char*)dst_ary; | |
8180 | AvALLOC((AV*)dstr) = dst_ary; | |
8181 | if (AvREAL((AV*)sstr)) { | |
8182 | while (items-- > 0) | |
8183 | *dst_ary++ = sv_dup_inc(*src_ary++); | |
8184 | } | |
8185 | else { | |
8186 | while (items-- > 0) | |
8187 | *dst_ary++ = sv_dup(*src_ary++); | |
8188 | } | |
8189 | items = AvMAX((AV*)sstr) - AvFILLp((AV*)sstr); | |
8190 | while (items-- > 0) { | |
8191 | *dst_ary++ = &PL_sv_undef; | |
8192 | } | |
8193 | } | |
8194 | else { | |
8195 | SvPVX(dstr) = Nullch; | |
8196 | AvALLOC((AV*)dstr) = (SV**)NULL; | |
8197 | } | |
8198 | break; | |
8199 | case SVt_PVHV: | |
8200 | SvANY(dstr) = new_XPVHV(); | |
8201 | SvCUR(dstr) = SvCUR(sstr); | |
8202 | SvLEN(dstr) = SvLEN(sstr); | |
8203 | SvIVX(dstr) = SvIVX(sstr); | |
8204 | SvNVX(dstr) = SvNVX(sstr); | |
8205 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
8206 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
8207 | HvRITER((HV*)dstr) = HvRITER((HV*)sstr); | |
8208 | if (HvARRAY((HV*)sstr)) { | |
1d7c1841 GS |
8209 | STRLEN i = 0; |
8210 | XPVHV *dxhv = (XPVHV*)SvANY(dstr); | |
8211 | XPVHV *sxhv = (XPVHV*)SvANY(sstr); | |
8212 | Newz(0, dxhv->xhv_array, | |
8213 | PERL_HV_ARRAY_ALLOC_BYTES(dxhv->xhv_max+1), char); | |
8214 | while (i <= sxhv->xhv_max) { | |
8215 | ((HE**)dxhv->xhv_array)[i] = he_dup(((HE**)sxhv->xhv_array)[i], | |
8216 | !!HvSHAREKEYS(sstr)); | |
8217 | ++i; | |
8218 | } | |
8219 | dxhv->xhv_eiter = he_dup(sxhv->xhv_eiter, !!HvSHAREKEYS(sstr)); | |
8220 | } | |
8221 | else { | |
8222 | SvPVX(dstr) = Nullch; | |
8223 | HvEITER((HV*)dstr) = (HE*)NULL; | |
8224 | } | |
8225 | HvPMROOT((HV*)dstr) = HvPMROOT((HV*)sstr); /* XXX */ | |
8226 | HvNAME((HV*)dstr) = SAVEPV(HvNAME((HV*)sstr)); | |
8227 | break; | |
8228 | case SVt_PVFM: | |
8229 | SvANY(dstr) = new_XPVFM(); | |
8230 | FmLINES(dstr) = FmLINES(sstr); | |
8231 | goto dup_pvcv; | |
8232 | /* NOTREACHED */ | |
8233 | case SVt_PVCV: | |
8234 | SvANY(dstr) = new_XPVCV(); | |
8235 | dup_pvcv: | |
8236 | SvCUR(dstr) = SvCUR(sstr); | |
8237 | SvLEN(dstr) = SvLEN(sstr); | |
8238 | SvIVX(dstr) = SvIVX(sstr); | |
8239 | SvNVX(dstr) = SvNVX(sstr); | |
8240 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
8241 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
8242 | if (SvPVX(sstr) && SvLEN(sstr)) | |
8243 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
8244 | else | |
8245 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
8246 | CvSTASH(dstr) = hv_dup(CvSTASH(sstr));/* NOTE: not refcounted */ | |
8247 | CvSTART(dstr) = CvSTART(sstr); | |
8248 | CvROOT(dstr) = OpREFCNT_inc(CvROOT(sstr)); | |
8249 | CvXSUB(dstr) = CvXSUB(sstr); | |
8250 | CvXSUBANY(dstr) = CvXSUBANY(sstr); | |
f25c30a3 | 8251 | CvGV(dstr) = gv_dup(CvGV(sstr)); |
1d7c1841 GS |
8252 | CvDEPTH(dstr) = CvDEPTH(sstr); |
8253 | if (CvPADLIST(sstr) && !AvREAL(CvPADLIST(sstr))) { | |
8254 | /* XXX padlists are real, but pretend to be not */ | |
8255 | AvREAL_on(CvPADLIST(sstr)); | |
8256 | CvPADLIST(dstr) = av_dup_inc(CvPADLIST(sstr)); | |
8257 | AvREAL_off(CvPADLIST(sstr)); | |
8258 | AvREAL_off(CvPADLIST(dstr)); | |
8259 | } | |
8260 | else | |
8261 | CvPADLIST(dstr) = av_dup_inc(CvPADLIST(sstr)); | |
282f25c9 JH |
8262 | if (!CvANON(sstr) || CvCLONED(sstr)) |
8263 | CvOUTSIDE(dstr) = cv_dup_inc(CvOUTSIDE(sstr)); | |
8264 | else | |
8265 | CvOUTSIDE(dstr) = cv_dup(CvOUTSIDE(sstr)); | |
1d7c1841 GS |
8266 | CvFLAGS(dstr) = CvFLAGS(sstr); |
8267 | break; | |
8268 | default: | |
8269 | Perl_croak(aTHX_ "Bizarre SvTYPE [%d]", SvTYPE(sstr)); | |
8270 | break; | |
8271 | } | |
8272 | ||
8273 | if (SvOBJECT(dstr) && SvTYPE(dstr) != SVt_PVIO) | |
8274 | ++PL_sv_objcount; | |
8275 | ||
8276 | return dstr; | |
8277 | } | |
8278 | ||
8279 | PERL_CONTEXT * | |
8280 | Perl_cx_dup(pTHX_ PERL_CONTEXT *cxs, I32 ix, I32 max) | |
8281 | { | |
8282 | PERL_CONTEXT *ncxs; | |
8283 | ||
8284 | if (!cxs) | |
8285 | return (PERL_CONTEXT*)NULL; | |
8286 | ||
8287 | /* look for it in the table first */ | |
8288 | ncxs = (PERL_CONTEXT*)ptr_table_fetch(PL_ptr_table, cxs); | |
8289 | if (ncxs) | |
8290 | return ncxs; | |
8291 | ||
8292 | /* create anew and remember what it is */ | |
8293 | Newz(56, ncxs, max + 1, PERL_CONTEXT); | |
8294 | ptr_table_store(PL_ptr_table, cxs, ncxs); | |
8295 | ||
8296 | while (ix >= 0) { | |
8297 | PERL_CONTEXT *cx = &cxs[ix]; | |
8298 | PERL_CONTEXT *ncx = &ncxs[ix]; | |
8299 | ncx->cx_type = cx->cx_type; | |
8300 | if (CxTYPE(cx) == CXt_SUBST) { | |
8301 | Perl_croak(aTHX_ "Cloning substitution context is unimplemented"); | |
8302 | } | |
8303 | else { | |
8304 | ncx->blk_oldsp = cx->blk_oldsp; | |
8305 | ncx->blk_oldcop = cx->blk_oldcop; | |
8306 | ncx->blk_oldretsp = cx->blk_oldretsp; | |
8307 | ncx->blk_oldmarksp = cx->blk_oldmarksp; | |
8308 | ncx->blk_oldscopesp = cx->blk_oldscopesp; | |
8309 | ncx->blk_oldpm = cx->blk_oldpm; | |
8310 | ncx->blk_gimme = cx->blk_gimme; | |
8311 | switch (CxTYPE(cx)) { | |
8312 | case CXt_SUB: | |
8313 | ncx->blk_sub.cv = (cx->blk_sub.olddepth == 0 | |
8314 | ? cv_dup_inc(cx->blk_sub.cv) | |
8315 | : cv_dup(cx->blk_sub.cv)); | |
8316 | ncx->blk_sub.argarray = (cx->blk_sub.hasargs | |
8317 | ? av_dup_inc(cx->blk_sub.argarray) | |
8318 | : Nullav); | |
f25c30a3 | 8319 | ncx->blk_sub.savearray = av_dup_inc(cx->blk_sub.savearray); |
1d7c1841 GS |
8320 | ncx->blk_sub.olddepth = cx->blk_sub.olddepth; |
8321 | ncx->blk_sub.hasargs = cx->blk_sub.hasargs; | |
8322 | ncx->blk_sub.lval = cx->blk_sub.lval; | |
8323 | break; | |
8324 | case CXt_EVAL: | |
8325 | ncx->blk_eval.old_in_eval = cx->blk_eval.old_in_eval; | |
8326 | ncx->blk_eval.old_op_type = cx->blk_eval.old_op_type; | |
0f79a09d | 8327 | ncx->blk_eval.old_namesv = sv_dup_inc(cx->blk_eval.old_namesv); |
1d7c1841 GS |
8328 | ncx->blk_eval.old_eval_root = cx->blk_eval.old_eval_root; |
8329 | ncx->blk_eval.cur_text = sv_dup(cx->blk_eval.cur_text); | |
8330 | break; | |
8331 | case CXt_LOOP: | |
8332 | ncx->blk_loop.label = cx->blk_loop.label; | |
8333 | ncx->blk_loop.resetsp = cx->blk_loop.resetsp; | |
8334 | ncx->blk_loop.redo_op = cx->blk_loop.redo_op; | |
8335 | ncx->blk_loop.next_op = cx->blk_loop.next_op; | |
8336 | ncx->blk_loop.last_op = cx->blk_loop.last_op; | |
8337 | ncx->blk_loop.iterdata = (CxPADLOOP(cx) | |
8338 | ? cx->blk_loop.iterdata | |
8339 | : gv_dup((GV*)cx->blk_loop.iterdata)); | |
a4b82a6f GS |
8340 | ncx->blk_loop.oldcurpad |
8341 | = (SV**)ptr_table_fetch(PL_ptr_table, | |
8342 | cx->blk_loop.oldcurpad); | |
1d7c1841 GS |
8343 | ncx->blk_loop.itersave = sv_dup_inc(cx->blk_loop.itersave); |
8344 | ncx->blk_loop.iterlval = sv_dup_inc(cx->blk_loop.iterlval); | |
8345 | ncx->blk_loop.iterary = av_dup_inc(cx->blk_loop.iterary); | |
8346 | ncx->blk_loop.iterix = cx->blk_loop.iterix; | |
8347 | ncx->blk_loop.itermax = cx->blk_loop.itermax; | |
8348 | break; | |
8349 | case CXt_FORMAT: | |
8350 | ncx->blk_sub.cv = cv_dup(cx->blk_sub.cv); | |
8351 | ncx->blk_sub.gv = gv_dup(cx->blk_sub.gv); | |
8352 | ncx->blk_sub.dfoutgv = gv_dup_inc(cx->blk_sub.dfoutgv); | |
8353 | ncx->blk_sub.hasargs = cx->blk_sub.hasargs; | |
8354 | break; | |
8355 | case CXt_BLOCK: | |
8356 | case CXt_NULL: | |
8357 | break; | |
8358 | } | |
8359 | } | |
8360 | --ix; | |
8361 | } | |
8362 | return ncxs; | |
8363 | } | |
8364 | ||
8365 | PERL_SI * | |
8366 | Perl_si_dup(pTHX_ PERL_SI *si) | |
8367 | { | |
8368 | PERL_SI *nsi; | |
8369 | ||
8370 | if (!si) | |
8371 | return (PERL_SI*)NULL; | |
8372 | ||
8373 | /* look for it in the table first */ | |
8374 | nsi = (PERL_SI*)ptr_table_fetch(PL_ptr_table, si); | |
8375 | if (nsi) | |
8376 | return nsi; | |
8377 | ||
8378 | /* create anew and remember what it is */ | |
8379 | Newz(56, nsi, 1, PERL_SI); | |
8380 | ptr_table_store(PL_ptr_table, si, nsi); | |
8381 | ||
8382 | nsi->si_stack = av_dup_inc(si->si_stack); | |
8383 | nsi->si_cxix = si->si_cxix; | |
8384 | nsi->si_cxmax = si->si_cxmax; | |
8385 | nsi->si_cxstack = cx_dup(si->si_cxstack, si->si_cxix, si->si_cxmax); | |
8386 | nsi->si_type = si->si_type; | |
8387 | nsi->si_prev = si_dup(si->si_prev); | |
8388 | nsi->si_next = si_dup(si->si_next); | |
8389 | nsi->si_markoff = si->si_markoff; | |
8390 | ||
8391 | return nsi; | |
8392 | } | |
8393 | ||
8394 | #define POPINT(ss,ix) ((ss)[--(ix)].any_i32) | |
8395 | #define TOPINT(ss,ix) ((ss)[ix].any_i32) | |
8396 | #define POPLONG(ss,ix) ((ss)[--(ix)].any_long) | |
8397 | #define TOPLONG(ss,ix) ((ss)[ix].any_long) | |
8398 | #define POPIV(ss,ix) ((ss)[--(ix)].any_iv) | |
8399 | #define TOPIV(ss,ix) ((ss)[ix].any_iv) | |
8400 | #define POPPTR(ss,ix) ((ss)[--(ix)].any_ptr) | |
8401 | #define TOPPTR(ss,ix) ((ss)[ix].any_ptr) | |
8402 | #define POPDPTR(ss,ix) ((ss)[--(ix)].any_dptr) | |
8403 | #define TOPDPTR(ss,ix) ((ss)[ix].any_dptr) | |
8404 | #define POPDXPTR(ss,ix) ((ss)[--(ix)].any_dxptr) | |
8405 | #define TOPDXPTR(ss,ix) ((ss)[ix].any_dxptr) | |
8406 | ||
8407 | /* XXXXX todo */ | |
8408 | #define pv_dup_inc(p) SAVEPV(p) | |
8409 | #define pv_dup(p) SAVEPV(p) | |
8410 | #define svp_dup_inc(p,pp) any_dup(p,pp) | |
8411 | ||
8412 | void * | |
8413 | Perl_any_dup(pTHX_ void *v, PerlInterpreter *proto_perl) | |
8414 | { | |
8415 | void *ret; | |
8416 | ||
8417 | if (!v) | |
8418 | return (void*)NULL; | |
8419 | ||
8420 | /* look for it in the table first */ | |
8421 | ret = ptr_table_fetch(PL_ptr_table, v); | |
8422 | if (ret) | |
8423 | return ret; | |
8424 | ||
8425 | /* see if it is part of the interpreter structure */ | |
8426 | if (v >= (void*)proto_perl && v < (void*)(proto_perl+1)) | |
8427 | ret = (void*)(((char*)aTHXo) + (((char*)v) - (char*)proto_perl)); | |
8428 | else | |
8429 | ret = v; | |
8430 | ||
8431 | return ret; | |
8432 | } | |
8433 | ||
8434 | ANY * | |
8435 | Perl_ss_dup(pTHX_ PerlInterpreter *proto_perl) | |
8436 | { | |
8437 | ANY *ss = proto_perl->Tsavestack; | |
8438 | I32 ix = proto_perl->Tsavestack_ix; | |
8439 | I32 max = proto_perl->Tsavestack_max; | |
8440 | ANY *nss; | |
8441 | SV *sv; | |
8442 | GV *gv; | |
8443 | AV *av; | |
8444 | HV *hv; | |
8445 | void* ptr; | |
8446 | int intval; | |
8447 | long longval; | |
8448 | GP *gp; | |
8449 | IV iv; | |
8450 | I32 i; | |
8451 | char *c; | |
8452 | void (*dptr) (void*); | |
8453 | void (*dxptr) (pTHXo_ void*); | |
e977893f | 8454 | OP *o; |
1d7c1841 GS |
8455 | |
8456 | Newz(54, nss, max, ANY); | |
8457 | ||
8458 | while (ix > 0) { | |
8459 | i = POPINT(ss,ix); | |
8460 | TOPINT(nss,ix) = i; | |
8461 | switch (i) { | |
8462 | case SAVEt_ITEM: /* normal string */ | |
8463 | sv = (SV*)POPPTR(ss,ix); | |
8464 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8465 | sv = (SV*)POPPTR(ss,ix); | |
8466 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8467 | break; | |
8468 | case SAVEt_SV: /* scalar reference */ | |
8469 | sv = (SV*)POPPTR(ss,ix); | |
8470 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8471 | gv = (GV*)POPPTR(ss,ix); | |
8472 | TOPPTR(nss,ix) = gv_dup_inc(gv); | |
8473 | break; | |
f4dd75d9 GS |
8474 | case SAVEt_GENERIC_PVREF: /* generic char* */ |
8475 | c = (char*)POPPTR(ss,ix); | |
8476 | TOPPTR(nss,ix) = pv_dup(c); | |
8477 | ptr = POPPTR(ss,ix); | |
8478 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8479 | break; | |
1d7c1841 GS |
8480 | case SAVEt_GENERIC_SVREF: /* generic sv */ |
8481 | case SAVEt_SVREF: /* scalar reference */ | |
8482 | sv = (SV*)POPPTR(ss,ix); | |
8483 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8484 | ptr = POPPTR(ss,ix); | |
8485 | TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */ | |
8486 | break; | |
8487 | case SAVEt_AV: /* array reference */ | |
8488 | av = (AV*)POPPTR(ss,ix); | |
8489 | TOPPTR(nss,ix) = av_dup_inc(av); | |
8490 | gv = (GV*)POPPTR(ss,ix); | |
8491 | TOPPTR(nss,ix) = gv_dup(gv); | |
8492 | break; | |
8493 | case SAVEt_HV: /* hash reference */ | |
8494 | hv = (HV*)POPPTR(ss,ix); | |
8495 | TOPPTR(nss,ix) = hv_dup_inc(hv); | |
8496 | gv = (GV*)POPPTR(ss,ix); | |
8497 | TOPPTR(nss,ix) = gv_dup(gv); | |
8498 | break; | |
8499 | case SAVEt_INT: /* int reference */ | |
8500 | ptr = POPPTR(ss,ix); | |
8501 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8502 | intval = (int)POPINT(ss,ix); | |
8503 | TOPINT(nss,ix) = intval; | |
8504 | break; | |
8505 | case SAVEt_LONG: /* long reference */ | |
8506 | ptr = POPPTR(ss,ix); | |
8507 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8508 | longval = (long)POPLONG(ss,ix); | |
8509 | TOPLONG(nss,ix) = longval; | |
8510 | break; | |
8511 | case SAVEt_I32: /* I32 reference */ | |
8512 | case SAVEt_I16: /* I16 reference */ | |
8513 | case SAVEt_I8: /* I8 reference */ | |
8514 | ptr = POPPTR(ss,ix); | |
8515 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8516 | i = POPINT(ss,ix); | |
8517 | TOPINT(nss,ix) = i; | |
8518 | break; | |
8519 | case SAVEt_IV: /* IV reference */ | |
8520 | ptr = POPPTR(ss,ix); | |
8521 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8522 | iv = POPIV(ss,ix); | |
8523 | TOPIV(nss,ix) = iv; | |
8524 | break; | |
8525 | case SAVEt_SPTR: /* SV* reference */ | |
8526 | ptr = POPPTR(ss,ix); | |
8527 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8528 | sv = (SV*)POPPTR(ss,ix); | |
8529 | TOPPTR(nss,ix) = sv_dup(sv); | |
8530 | break; | |
8531 | case SAVEt_VPTR: /* random* reference */ | |
8532 | ptr = POPPTR(ss,ix); | |
8533 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8534 | ptr = POPPTR(ss,ix); | |
8535 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8536 | break; | |
8537 | case SAVEt_PPTR: /* char* reference */ | |
8538 | ptr = POPPTR(ss,ix); | |
8539 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8540 | c = (char*)POPPTR(ss,ix); | |
8541 | TOPPTR(nss,ix) = pv_dup(c); | |
8542 | break; | |
8543 | case SAVEt_HPTR: /* HV* reference */ | |
8544 | ptr = POPPTR(ss,ix); | |
8545 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8546 | hv = (HV*)POPPTR(ss,ix); | |
8547 | TOPPTR(nss,ix) = hv_dup(hv); | |
8548 | break; | |
8549 | case SAVEt_APTR: /* AV* reference */ | |
8550 | ptr = POPPTR(ss,ix); | |
8551 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8552 | av = (AV*)POPPTR(ss,ix); | |
8553 | TOPPTR(nss,ix) = av_dup(av); | |
8554 | break; | |
8555 | case SAVEt_NSTAB: | |
8556 | gv = (GV*)POPPTR(ss,ix); | |
8557 | TOPPTR(nss,ix) = gv_dup(gv); | |
8558 | break; | |
8559 | case SAVEt_GP: /* scalar reference */ | |
8560 | gp = (GP*)POPPTR(ss,ix); | |
8561 | TOPPTR(nss,ix) = gp = gp_dup(gp); | |
8562 | (void)GpREFCNT_inc(gp); | |
8563 | gv = (GV*)POPPTR(ss,ix); | |
8564 | TOPPTR(nss,ix) = gv_dup_inc(c); | |
8565 | c = (char*)POPPTR(ss,ix); | |
8566 | TOPPTR(nss,ix) = pv_dup(c); | |
8567 | iv = POPIV(ss,ix); | |
8568 | TOPIV(nss,ix) = iv; | |
8569 | iv = POPIV(ss,ix); | |
8570 | TOPIV(nss,ix) = iv; | |
8571 | break; | |
8572 | case SAVEt_FREESV: | |
26d9b02f | 8573 | case SAVEt_MORTALIZESV: |
1d7c1841 GS |
8574 | sv = (SV*)POPPTR(ss,ix); |
8575 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8576 | break; | |
8577 | case SAVEt_FREEOP: | |
8578 | ptr = POPPTR(ss,ix); | |
8579 | if (ptr && (((OP*)ptr)->op_private & OPpREFCOUNTED)) { | |
8580 | /* these are assumed to be refcounted properly */ | |
8581 | switch (((OP*)ptr)->op_type) { | |
8582 | case OP_LEAVESUB: | |
8583 | case OP_LEAVESUBLV: | |
8584 | case OP_LEAVEEVAL: | |
8585 | case OP_LEAVE: | |
8586 | case OP_SCOPE: | |
8587 | case OP_LEAVEWRITE: | |
e977893f GS |
8588 | TOPPTR(nss,ix) = ptr; |
8589 | o = (OP*)ptr; | |
8590 | OpREFCNT_inc(o); | |
1d7c1841 GS |
8591 | break; |
8592 | default: | |
8593 | TOPPTR(nss,ix) = Nullop; | |
8594 | break; | |
8595 | } | |
8596 | } | |
8597 | else | |
8598 | TOPPTR(nss,ix) = Nullop; | |
8599 | break; | |
8600 | case SAVEt_FREEPV: | |
8601 | c = (char*)POPPTR(ss,ix); | |
8602 | TOPPTR(nss,ix) = pv_dup_inc(c); | |
8603 | break; | |
8604 | case SAVEt_CLEARSV: | |
8605 | longval = POPLONG(ss,ix); | |
8606 | TOPLONG(nss,ix) = longval; | |
8607 | break; | |
8608 | case SAVEt_DELETE: | |
8609 | hv = (HV*)POPPTR(ss,ix); | |
8610 | TOPPTR(nss,ix) = hv_dup_inc(hv); | |
8611 | c = (char*)POPPTR(ss,ix); | |
8612 | TOPPTR(nss,ix) = pv_dup_inc(c); | |
8613 | i = POPINT(ss,ix); | |
8614 | TOPINT(nss,ix) = i; | |
8615 | break; | |
8616 | case SAVEt_DESTRUCTOR: | |
8617 | ptr = POPPTR(ss,ix); | |
8618 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */ | |
8619 | dptr = POPDPTR(ss,ix); | |
ef75a179 | 8620 | TOPDPTR(nss,ix) = (void (*)(void*))any_dup((void *)dptr, proto_perl); |
1d7c1841 GS |
8621 | break; |
8622 | case SAVEt_DESTRUCTOR_X: | |
8623 | ptr = POPPTR(ss,ix); | |
8624 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */ | |
8625 | dxptr = POPDXPTR(ss,ix); | |
ef75a179 | 8626 | TOPDXPTR(nss,ix) = (void (*)(pTHXo_ void*))any_dup((void *)dxptr, proto_perl); |
1d7c1841 GS |
8627 | break; |
8628 | case SAVEt_REGCONTEXT: | |
8629 | case SAVEt_ALLOC: | |
8630 | i = POPINT(ss,ix); | |
8631 | TOPINT(nss,ix) = i; | |
8632 | ix -= i; | |
8633 | break; | |
8634 | case SAVEt_STACK_POS: /* Position on Perl stack */ | |
8635 | i = POPINT(ss,ix); | |
8636 | TOPINT(nss,ix) = i; | |
8637 | break; | |
8638 | case SAVEt_AELEM: /* array element */ | |
8639 | sv = (SV*)POPPTR(ss,ix); | |
8640 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8641 | i = POPINT(ss,ix); | |
8642 | TOPINT(nss,ix) = i; | |
8643 | av = (AV*)POPPTR(ss,ix); | |
8644 | TOPPTR(nss,ix) = av_dup_inc(av); | |
8645 | break; | |
8646 | case SAVEt_HELEM: /* hash element */ | |
8647 | sv = (SV*)POPPTR(ss,ix); | |
8648 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8649 | sv = (SV*)POPPTR(ss,ix); | |
8650 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8651 | hv = (HV*)POPPTR(ss,ix); | |
8652 | TOPPTR(nss,ix) = hv_dup_inc(hv); | |
8653 | break; | |
8654 | case SAVEt_OP: | |
8655 | ptr = POPPTR(ss,ix); | |
8656 | TOPPTR(nss,ix) = ptr; | |
8657 | break; | |
8658 | case SAVEt_HINTS: | |
8659 | i = POPINT(ss,ix); | |
8660 | TOPINT(nss,ix) = i; | |
8661 | break; | |
c4410b1b GS |
8662 | case SAVEt_COMPPAD: |
8663 | av = (AV*)POPPTR(ss,ix); | |
8664 | TOPPTR(nss,ix) = av_dup(av); | |
8665 | break; | |
c3564e5c GS |
8666 | case SAVEt_PADSV: |
8667 | longval = (long)POPLONG(ss,ix); | |
8668 | TOPLONG(nss,ix) = longval; | |
8669 | ptr = POPPTR(ss,ix); | |
8670 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8671 | sv = (SV*)POPPTR(ss,ix); | |
8672 | TOPPTR(nss,ix) = sv_dup(sv); | |
8673 | break; | |
1d7c1841 GS |
8674 | default: |
8675 | Perl_croak(aTHX_ "panic: ss_dup inconsistency"); | |
8676 | } | |
8677 | } | |
8678 | ||
8679 | return nss; | |
8680 | } | |
8681 | ||
8682 | #ifdef PERL_OBJECT | |
8683 | #include "XSUB.h" | |
8684 | #endif | |
8685 | ||
8686 | PerlInterpreter * | |
8687 | perl_clone(PerlInterpreter *proto_perl, UV flags) | |
8688 | { | |
8689 | #ifdef PERL_OBJECT | |
8690 | CPerlObj *pPerl = (CPerlObj*)proto_perl; | |
8691 | #endif | |
8692 | ||
8693 | #ifdef PERL_IMPLICIT_SYS | |
8694 | return perl_clone_using(proto_perl, flags, | |
8695 | proto_perl->IMem, | |
8696 | proto_perl->IMemShared, | |
8697 | proto_perl->IMemParse, | |
8698 | proto_perl->IEnv, | |
8699 | proto_perl->IStdIO, | |
8700 | proto_perl->ILIO, | |
8701 | proto_perl->IDir, | |
8702 | proto_perl->ISock, | |
8703 | proto_perl->IProc); | |
8704 | } | |
8705 | ||
8706 | PerlInterpreter * | |
8707 | perl_clone_using(PerlInterpreter *proto_perl, UV flags, | |
8708 | struct IPerlMem* ipM, struct IPerlMem* ipMS, | |
8709 | struct IPerlMem* ipMP, struct IPerlEnv* ipE, | |
8710 | struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO, | |
8711 | struct IPerlDir* ipD, struct IPerlSock* ipS, | |
8712 | struct IPerlProc* ipP) | |
8713 | { | |
8714 | /* XXX many of the string copies here can be optimized if they're | |
8715 | * constants; they need to be allocated as common memory and just | |
8716 | * their pointers copied. */ | |
8717 | ||
8718 | IV i; | |
1d7c1841 GS |
8719 | # ifdef PERL_OBJECT |
8720 | CPerlObj *pPerl = new(ipM) CPerlObj(ipM, ipMS, ipMP, ipE, ipStd, ipLIO, | |
8721 | ipD, ipS, ipP); | |
ba869deb | 8722 | PERL_SET_THX(pPerl); |
1d7c1841 GS |
8723 | # else /* !PERL_OBJECT */ |
8724 | PerlInterpreter *my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter)); | |
ba869deb | 8725 | PERL_SET_THX(my_perl); |
1d7c1841 GS |
8726 | |
8727 | # ifdef DEBUGGING | |
8728 | memset(my_perl, 0xab, sizeof(PerlInterpreter)); | |
8729 | PL_markstack = 0; | |
8730 | PL_scopestack = 0; | |
8731 | PL_savestack = 0; | |
8732 | PL_retstack = 0; | |
66fe0623 | 8733 | PL_sig_pending = 0; |
1d7c1841 GS |
8734 | # else /* !DEBUGGING */ |
8735 | Zero(my_perl, 1, PerlInterpreter); | |
8736 | # endif /* DEBUGGING */ | |
8737 | ||
8738 | /* host pointers */ | |
8739 | PL_Mem = ipM; | |
8740 | PL_MemShared = ipMS; | |
8741 | PL_MemParse = ipMP; | |
8742 | PL_Env = ipE; | |
8743 | PL_StdIO = ipStd; | |
8744 | PL_LIO = ipLIO; | |
8745 | PL_Dir = ipD; | |
8746 | PL_Sock = ipS; | |
8747 | PL_Proc = ipP; | |
8748 | # endif /* PERL_OBJECT */ | |
8749 | #else /* !PERL_IMPLICIT_SYS */ | |
8750 | IV i; | |
1d7c1841 | 8751 | PerlInterpreter *my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter)); |
ba869deb | 8752 | PERL_SET_THX(my_perl); |
1d7c1841 GS |
8753 | |
8754 | # ifdef DEBUGGING | |
8755 | memset(my_perl, 0xab, sizeof(PerlInterpreter)); | |
8756 | PL_markstack = 0; | |
8757 | PL_scopestack = 0; | |
8758 | PL_savestack = 0; | |
8759 | PL_retstack = 0; | |
66fe0623 | 8760 | PL_sig_pending = 0; |
1d7c1841 GS |
8761 | # else /* !DEBUGGING */ |
8762 | Zero(my_perl, 1, PerlInterpreter); | |
8763 | # endif /* DEBUGGING */ | |
8764 | #endif /* PERL_IMPLICIT_SYS */ | |
8765 | ||
8766 | /* arena roots */ | |
8767 | PL_xiv_arenaroot = NULL; | |
8768 | PL_xiv_root = NULL; | |
612f20c3 | 8769 | PL_xnv_arenaroot = NULL; |
1d7c1841 | 8770 | PL_xnv_root = NULL; |
612f20c3 | 8771 | PL_xrv_arenaroot = NULL; |
1d7c1841 | 8772 | PL_xrv_root = NULL; |
612f20c3 | 8773 | PL_xpv_arenaroot = NULL; |
1d7c1841 | 8774 | PL_xpv_root = NULL; |
612f20c3 | 8775 | PL_xpviv_arenaroot = NULL; |
1d7c1841 | 8776 | PL_xpviv_root = NULL; |
612f20c3 | 8777 | PL_xpvnv_arenaroot = NULL; |
1d7c1841 | 8778 | PL_xpvnv_root = NULL; |
612f20c3 | 8779 | PL_xpvcv_arenaroot = NULL; |
1d7c1841 | 8780 | PL_xpvcv_root = NULL; |
612f20c3 | 8781 | PL_xpvav_arenaroot = NULL; |
1d7c1841 | 8782 | PL_xpvav_root = NULL; |
612f20c3 | 8783 | PL_xpvhv_arenaroot = NULL; |
1d7c1841 | 8784 | PL_xpvhv_root = NULL; |
612f20c3 | 8785 | PL_xpvmg_arenaroot = NULL; |
1d7c1841 | 8786 | PL_xpvmg_root = NULL; |
612f20c3 | 8787 | PL_xpvlv_arenaroot = NULL; |
1d7c1841 | 8788 | PL_xpvlv_root = NULL; |
612f20c3 | 8789 | PL_xpvbm_arenaroot = NULL; |
1d7c1841 | 8790 | PL_xpvbm_root = NULL; |
612f20c3 | 8791 | PL_he_arenaroot = NULL; |
1d7c1841 GS |
8792 | PL_he_root = NULL; |
8793 | PL_nice_chunk = NULL; | |
8794 | PL_nice_chunk_size = 0; | |
8795 | PL_sv_count = 0; | |
8796 | PL_sv_objcount = 0; | |
8797 | PL_sv_root = Nullsv; | |
8798 | PL_sv_arenaroot = Nullsv; | |
8799 | ||
8800 | PL_debug = proto_perl->Idebug; | |
8801 | ||
8802 | /* create SV map for pointer relocation */ | |
8803 | PL_ptr_table = ptr_table_new(); | |
8804 | ||
8805 | /* initialize these special pointers as early as possible */ | |
8806 | SvANY(&PL_sv_undef) = NULL; | |
8807 | SvREFCNT(&PL_sv_undef) = (~(U32)0)/2; | |
8808 | SvFLAGS(&PL_sv_undef) = SVf_READONLY|SVt_NULL; | |
8809 | ptr_table_store(PL_ptr_table, &proto_perl->Isv_undef, &PL_sv_undef); | |
8810 | ||
8811 | #ifdef PERL_OBJECT | |
8812 | SvUPGRADE(&PL_sv_no, SVt_PVNV); | |
8813 | #else | |
8814 | SvANY(&PL_sv_no) = new_XPVNV(); | |
8815 | #endif | |
8816 | SvREFCNT(&PL_sv_no) = (~(U32)0)/2; | |
8817 | SvFLAGS(&PL_sv_no) = SVp_NOK|SVf_NOK|SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV; | |
8818 | SvPVX(&PL_sv_no) = SAVEPVN(PL_No, 0); | |
8819 | SvCUR(&PL_sv_no) = 0; | |
8820 | SvLEN(&PL_sv_no) = 1; | |
8821 | SvNVX(&PL_sv_no) = 0; | |
8822 | ptr_table_store(PL_ptr_table, &proto_perl->Isv_no, &PL_sv_no); | |
8823 | ||
8824 | #ifdef PERL_OBJECT | |
8825 | SvUPGRADE(&PL_sv_yes, SVt_PVNV); | |
8826 | #else | |
8827 | SvANY(&PL_sv_yes) = new_XPVNV(); | |
8828 | #endif | |
8829 | SvREFCNT(&PL_sv_yes) = (~(U32)0)/2; | |
8830 | SvFLAGS(&PL_sv_yes) = SVp_NOK|SVf_NOK|SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV; | |
8831 | SvPVX(&PL_sv_yes) = SAVEPVN(PL_Yes, 1); | |
8832 | SvCUR(&PL_sv_yes) = 1; | |
8833 | SvLEN(&PL_sv_yes) = 2; | |
8834 | SvNVX(&PL_sv_yes) = 1; | |
8835 | ptr_table_store(PL_ptr_table, &proto_perl->Isv_yes, &PL_sv_yes); | |
8836 | ||
8837 | /* create shared string table */ | |
8838 | PL_strtab = newHV(); | |
8839 | HvSHAREKEYS_off(PL_strtab); | |
8840 | hv_ksplit(PL_strtab, 512); | |
8841 | ptr_table_store(PL_ptr_table, proto_perl->Istrtab, PL_strtab); | |
8842 | ||
8843 | PL_compiling = proto_perl->Icompiling; | |
8844 | PL_compiling.cop_stashpv = SAVEPV(PL_compiling.cop_stashpv); | |
8845 | PL_compiling.cop_file = SAVEPV(PL_compiling.cop_file); | |
8846 | ptr_table_store(PL_ptr_table, &proto_perl->Icompiling, &PL_compiling); | |
8847 | if (!specialWARN(PL_compiling.cop_warnings)) | |
8848 | PL_compiling.cop_warnings = sv_dup_inc(PL_compiling.cop_warnings); | |
ac27b0f5 NIS |
8849 | if (!specialCopIO(PL_compiling.cop_io)) |
8850 | PL_compiling.cop_io = sv_dup_inc(PL_compiling.cop_io); | |
1d7c1841 GS |
8851 | PL_curcop = (COP*)any_dup(proto_perl->Tcurcop, proto_perl); |
8852 | ||
8853 | /* pseudo environmental stuff */ | |
8854 | PL_origargc = proto_perl->Iorigargc; | |
8855 | i = PL_origargc; | |
8856 | New(0, PL_origargv, i+1, char*); | |
8857 | PL_origargv[i] = '\0'; | |
8858 | while (i-- > 0) { | |
8859 | PL_origargv[i] = SAVEPV(proto_perl->Iorigargv[i]); | |
8860 | } | |
8861 | PL_envgv = gv_dup(proto_perl->Ienvgv); | |
8862 | PL_incgv = gv_dup(proto_perl->Iincgv); | |
8863 | PL_hintgv = gv_dup(proto_perl->Ihintgv); | |
8864 | PL_origfilename = SAVEPV(proto_perl->Iorigfilename); | |
8865 | PL_diehook = sv_dup_inc(proto_perl->Idiehook); | |
8866 | PL_warnhook = sv_dup_inc(proto_perl->Iwarnhook); | |
8867 | ||
8868 | /* switches */ | |
8869 | PL_minus_c = proto_perl->Iminus_c; | |
a7cb1f99 | 8870 | PL_patchlevel = sv_dup_inc(proto_perl->Ipatchlevel); |
1d7c1841 GS |
8871 | PL_localpatches = proto_perl->Ilocalpatches; |
8872 | PL_splitstr = proto_perl->Isplitstr; | |
8873 | PL_preprocess = proto_perl->Ipreprocess; | |
8874 | PL_minus_n = proto_perl->Iminus_n; | |
8875 | PL_minus_p = proto_perl->Iminus_p; | |
8876 | PL_minus_l = proto_perl->Iminus_l; | |
8877 | PL_minus_a = proto_perl->Iminus_a; | |
8878 | PL_minus_F = proto_perl->Iminus_F; | |
8879 | PL_doswitches = proto_perl->Idoswitches; | |
8880 | PL_dowarn = proto_perl->Idowarn; | |
8881 | PL_doextract = proto_perl->Idoextract; | |
8882 | PL_sawampersand = proto_perl->Isawampersand; | |
8883 | PL_unsafe = proto_perl->Iunsafe; | |
8884 | PL_inplace = SAVEPV(proto_perl->Iinplace); | |
8885 | PL_e_script = sv_dup_inc(proto_perl->Ie_script); | |
8886 | PL_perldb = proto_perl->Iperldb; | |
8887 | PL_perl_destruct_level = proto_perl->Iperl_destruct_level; | |
8888 | ||
8889 | /* magical thingies */ | |
8890 | /* XXX time(&PL_basetime) when asked for? */ | |
8891 | PL_basetime = proto_perl->Ibasetime; | |
8892 | PL_formfeed = sv_dup(proto_perl->Iformfeed); | |
8893 | ||
8894 | PL_maxsysfd = proto_perl->Imaxsysfd; | |
8895 | PL_multiline = proto_perl->Imultiline; | |
8896 | PL_statusvalue = proto_perl->Istatusvalue; | |
8897 | #ifdef VMS | |
8898 | PL_statusvalue_vms = proto_perl->Istatusvalue_vms; | |
8899 | #endif | |
8900 | ||
8901 | /* shortcuts to various I/O objects */ | |
8902 | PL_stdingv = gv_dup(proto_perl->Istdingv); | |
8903 | PL_stderrgv = gv_dup(proto_perl->Istderrgv); | |
8904 | PL_defgv = gv_dup(proto_perl->Idefgv); | |
8905 | PL_argvgv = gv_dup(proto_perl->Iargvgv); | |
8906 | PL_argvoutgv = gv_dup(proto_perl->Iargvoutgv); | |
f25c30a3 | 8907 | PL_argvout_stack = av_dup_inc(proto_perl->Iargvout_stack); |
1d7c1841 GS |
8908 | |
8909 | /* shortcuts to regexp stuff */ | |
8910 | PL_replgv = gv_dup(proto_perl->Ireplgv); | |
8911 | ||
8912 | /* shortcuts to misc objects */ | |
8913 | PL_errgv = gv_dup(proto_perl->Ierrgv); | |
8914 | ||
8915 | /* shortcuts to debugging objects */ | |
8916 | PL_DBgv = gv_dup(proto_perl->IDBgv); | |
8917 | PL_DBline = gv_dup(proto_perl->IDBline); | |
8918 | PL_DBsub = gv_dup(proto_perl->IDBsub); | |
8919 | PL_DBsingle = sv_dup(proto_perl->IDBsingle); | |
8920 | PL_DBtrace = sv_dup(proto_perl->IDBtrace); | |
8921 | PL_DBsignal = sv_dup(proto_perl->IDBsignal); | |
8922 | PL_lineary = av_dup(proto_perl->Ilineary); | |
8923 | PL_dbargs = av_dup(proto_perl->Idbargs); | |
8924 | ||
8925 | /* symbol tables */ | |
8926 | PL_defstash = hv_dup_inc(proto_perl->Tdefstash); | |
8927 | PL_curstash = hv_dup(proto_perl->Tcurstash); | |
8928 | PL_debstash = hv_dup(proto_perl->Idebstash); | |
8929 | PL_globalstash = hv_dup(proto_perl->Iglobalstash); | |
8930 | PL_curstname = sv_dup_inc(proto_perl->Icurstname); | |
8931 | ||
8932 | PL_beginav = av_dup_inc(proto_perl->Ibeginav); | |
8933 | PL_endav = av_dup_inc(proto_perl->Iendav); | |
7d30b5c4 | 8934 | PL_checkav = av_dup_inc(proto_perl->Icheckav); |
1d7c1841 GS |
8935 | PL_initav = av_dup_inc(proto_perl->Iinitav); |
8936 | ||
8937 | PL_sub_generation = proto_perl->Isub_generation; | |
8938 | ||
8939 | /* funky return mechanisms */ | |
8940 | PL_forkprocess = proto_perl->Iforkprocess; | |
8941 | ||
8942 | /* subprocess state */ | |
8943 | PL_fdpid = av_dup_inc(proto_perl->Ifdpid); | |
8944 | ||
8945 | /* internal state */ | |
8946 | PL_tainting = proto_perl->Itainting; | |
8947 | PL_maxo = proto_perl->Imaxo; | |
8948 | if (proto_perl->Iop_mask) | |
8949 | PL_op_mask = SAVEPVN(proto_perl->Iop_mask, PL_maxo); | |
8950 | else | |
8951 | PL_op_mask = Nullch; | |
8952 | ||
8953 | /* current interpreter roots */ | |
8954 | PL_main_cv = cv_dup_inc(proto_perl->Imain_cv); | |
8955 | PL_main_root = OpREFCNT_inc(proto_perl->Imain_root); | |
8956 | PL_main_start = proto_perl->Imain_start; | |
e977893f | 8957 | PL_eval_root = proto_perl->Ieval_root; |
1d7c1841 GS |
8958 | PL_eval_start = proto_perl->Ieval_start; |
8959 | ||
8960 | /* runtime control stuff */ | |
8961 | PL_curcopdb = (COP*)any_dup(proto_perl->Icurcopdb, proto_perl); | |
8962 | PL_copline = proto_perl->Icopline; | |
8963 | ||
8964 | PL_filemode = proto_perl->Ifilemode; | |
8965 | PL_lastfd = proto_perl->Ilastfd; | |
8966 | PL_oldname = proto_perl->Ioldname; /* XXX not quite right */ | |
8967 | PL_Argv = NULL; | |
8968 | PL_Cmd = Nullch; | |
8969 | PL_gensym = proto_perl->Igensym; | |
8970 | PL_preambled = proto_perl->Ipreambled; | |
8971 | PL_preambleav = av_dup_inc(proto_perl->Ipreambleav); | |
8972 | PL_laststatval = proto_perl->Ilaststatval; | |
8973 | PL_laststype = proto_perl->Ilaststype; | |
8974 | PL_mess_sv = Nullsv; | |
8975 | ||
7889fe52 | 8976 | PL_ors_sv = sv_dup_inc(proto_perl->Iors_sv); |
1d7c1841 GS |
8977 | PL_ofmt = SAVEPV(proto_perl->Iofmt); |
8978 | ||
8979 | /* interpreter atexit processing */ | |
8980 | PL_exitlistlen = proto_perl->Iexitlistlen; | |
8981 | if (PL_exitlistlen) { | |
8982 | New(0, PL_exitlist, PL_exitlistlen, PerlExitListEntry); | |
8983 | Copy(proto_perl->Iexitlist, PL_exitlist, PL_exitlistlen, PerlExitListEntry); | |
8984 | } | |
8985 | else | |
8986 | PL_exitlist = (PerlExitListEntry*)NULL; | |
8987 | PL_modglobal = hv_dup_inc(proto_perl->Imodglobal); | |
8988 | ||
8989 | PL_profiledata = NULL; | |
8990 | PL_rsfp = fp_dup(proto_perl->Irsfp, '<'); | |
8991 | /* PL_rsfp_filters entries have fake IoDIRP() */ | |
8992 | PL_rsfp_filters = av_dup_inc(proto_perl->Irsfp_filters); | |
8993 | ||
8994 | PL_compcv = cv_dup(proto_perl->Icompcv); | |
8995 | PL_comppad = av_dup(proto_perl->Icomppad); | |
8996 | PL_comppad_name = av_dup(proto_perl->Icomppad_name); | |
8997 | PL_comppad_name_fill = proto_perl->Icomppad_name_fill; | |
8998 | PL_comppad_name_floor = proto_perl->Icomppad_name_floor; | |
8999 | PL_curpad = (SV**)ptr_table_fetch(PL_ptr_table, | |
9000 | proto_perl->Tcurpad); | |
9001 | ||
9002 | #ifdef HAVE_INTERP_INTERN | |
9003 | sys_intern_dup(&proto_perl->Isys_intern, &PL_sys_intern); | |
9004 | #endif | |
9005 | ||
9006 | /* more statics moved here */ | |
9007 | PL_generation = proto_perl->Igeneration; | |
9008 | PL_DBcv = cv_dup(proto_perl->IDBcv); | |
1d7c1841 GS |
9009 | |
9010 | PL_in_clean_objs = proto_perl->Iin_clean_objs; | |
9011 | PL_in_clean_all = proto_perl->Iin_clean_all; | |
9012 | ||
9013 | PL_uid = proto_perl->Iuid; | |
9014 | PL_euid = proto_perl->Ieuid; | |
9015 | PL_gid = proto_perl->Igid; | |
9016 | PL_egid = proto_perl->Iegid; | |
9017 | PL_nomemok = proto_perl->Inomemok; | |
9018 | PL_an = proto_perl->Ian; | |
9019 | PL_cop_seqmax = proto_perl->Icop_seqmax; | |
9020 | PL_op_seqmax = proto_perl->Iop_seqmax; | |
9021 | PL_evalseq = proto_perl->Ievalseq; | |
9022 | PL_origenviron = proto_perl->Iorigenviron; /* XXX not quite right */ | |
9023 | PL_origalen = proto_perl->Iorigalen; | |
9024 | PL_pidstatus = newHV(); /* XXX flag for cloning? */ | |
9025 | PL_osname = SAVEPV(proto_perl->Iosname); | |
9026 | PL_sh_path = SAVEPV(proto_perl->Ish_path); | |
9027 | PL_sighandlerp = proto_perl->Isighandlerp; | |
9028 | ||
9029 | ||
9030 | PL_runops = proto_perl->Irunops; | |
9031 | ||
9032 | Copy(proto_perl->Itokenbuf, PL_tokenbuf, 256, char); | |
9033 | ||
9034 | #ifdef CSH | |
9035 | PL_cshlen = proto_perl->Icshlen; | |
9036 | PL_cshname = SAVEPVN(proto_perl->Icshname, PL_cshlen); | |
9037 | #endif | |
9038 | ||
9039 | PL_lex_state = proto_perl->Ilex_state; | |
9040 | PL_lex_defer = proto_perl->Ilex_defer; | |
9041 | PL_lex_expect = proto_perl->Ilex_expect; | |
9042 | PL_lex_formbrack = proto_perl->Ilex_formbrack; | |
9043 | PL_lex_dojoin = proto_perl->Ilex_dojoin; | |
9044 | PL_lex_starts = proto_perl->Ilex_starts; | |
9045 | PL_lex_stuff = sv_dup_inc(proto_perl->Ilex_stuff); | |
9046 | PL_lex_repl = sv_dup_inc(proto_perl->Ilex_repl); | |
9047 | PL_lex_op = proto_perl->Ilex_op; | |
9048 | PL_lex_inpat = proto_perl->Ilex_inpat; | |
9049 | PL_lex_inwhat = proto_perl->Ilex_inwhat; | |
9050 | PL_lex_brackets = proto_perl->Ilex_brackets; | |
9051 | i = (PL_lex_brackets < 120 ? 120 : PL_lex_brackets); | |
9052 | PL_lex_brackstack = SAVEPVN(proto_perl->Ilex_brackstack,i); | |
9053 | PL_lex_casemods = proto_perl->Ilex_casemods; | |
9054 | i = (PL_lex_casemods < 12 ? 12 : PL_lex_casemods); | |
9055 | PL_lex_casestack = SAVEPVN(proto_perl->Ilex_casestack,i); | |
9056 | ||
9057 | Copy(proto_perl->Inextval, PL_nextval, 5, YYSTYPE); | |
9058 | Copy(proto_perl->Inexttype, PL_nexttype, 5, I32); | |
9059 | PL_nexttoke = proto_perl->Inexttoke; | |
9060 | ||
9061 | PL_linestr = sv_dup_inc(proto_perl->Ilinestr); | |
9062 | i = proto_perl->Ibufptr - SvPVX(proto_perl->Ilinestr); | |
9063 | PL_bufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
9064 | i = proto_perl->Ioldbufptr - SvPVX(proto_perl->Ilinestr); | |
9065 | PL_oldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
9066 | i = proto_perl->Ioldoldbufptr - SvPVX(proto_perl->Ilinestr); | |
9067 | PL_oldoldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
9068 | PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr); | |
9069 | i = proto_perl->Ilinestart - SvPVX(proto_perl->Ilinestr); | |
9070 | PL_linestart = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
9071 | PL_pending_ident = proto_perl->Ipending_ident; | |
9072 | PL_sublex_info = proto_perl->Isublex_info; /* XXX not quite right */ | |
9073 | ||
9074 | PL_expect = proto_perl->Iexpect; | |
9075 | ||
9076 | PL_multi_start = proto_perl->Imulti_start; | |
9077 | PL_multi_end = proto_perl->Imulti_end; | |
9078 | PL_multi_open = proto_perl->Imulti_open; | |
9079 | PL_multi_close = proto_perl->Imulti_close; | |
9080 | ||
9081 | PL_error_count = proto_perl->Ierror_count; | |
9082 | PL_subline = proto_perl->Isubline; | |
9083 | PL_subname = sv_dup_inc(proto_perl->Isubname); | |
9084 | ||
9085 | PL_min_intro_pending = proto_perl->Imin_intro_pending; | |
9086 | PL_max_intro_pending = proto_perl->Imax_intro_pending; | |
9087 | PL_padix = proto_perl->Ipadix; | |
9088 | PL_padix_floor = proto_perl->Ipadix_floor; | |
9089 | PL_pad_reset_pending = proto_perl->Ipad_reset_pending; | |
9090 | ||
9091 | i = proto_perl->Ilast_uni - SvPVX(proto_perl->Ilinestr); | |
9092 | PL_last_uni = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
9093 | i = proto_perl->Ilast_lop - SvPVX(proto_perl->Ilinestr); | |
9094 | PL_last_lop = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
9095 | PL_last_lop_op = proto_perl->Ilast_lop_op; | |
9096 | PL_in_my = proto_perl->Iin_my; | |
9097 | PL_in_my_stash = hv_dup(proto_perl->Iin_my_stash); | |
9098 | #ifdef FCRYPT | |
9099 | PL_cryptseen = proto_perl->Icryptseen; | |
9100 | #endif | |
9101 | ||
9102 | PL_hints = proto_perl->Ihints; | |
9103 | ||
9104 | PL_amagic_generation = proto_perl->Iamagic_generation; | |
9105 | ||
9106 | #ifdef USE_LOCALE_COLLATE | |
9107 | PL_collation_ix = proto_perl->Icollation_ix; | |
9108 | PL_collation_name = SAVEPV(proto_perl->Icollation_name); | |
9109 | PL_collation_standard = proto_perl->Icollation_standard; | |
9110 | PL_collxfrm_base = proto_perl->Icollxfrm_base; | |
9111 | PL_collxfrm_mult = proto_perl->Icollxfrm_mult; | |
9112 | #endif /* USE_LOCALE_COLLATE */ | |
9113 | ||
9114 | #ifdef USE_LOCALE_NUMERIC | |
9115 | PL_numeric_name = SAVEPV(proto_perl->Inumeric_name); | |
9116 | PL_numeric_standard = proto_perl->Inumeric_standard; | |
9117 | PL_numeric_local = proto_perl->Inumeric_local; | |
a453c169 | 9118 | PL_numeric_radix_sv = sv_dup_inc(proto_perl->Inumeric_radix_sv); |
1d7c1841 GS |
9119 | #endif /* !USE_LOCALE_NUMERIC */ |
9120 | ||
9121 | /* utf8 character classes */ | |
9122 | PL_utf8_alnum = sv_dup_inc(proto_perl->Iutf8_alnum); | |
9123 | PL_utf8_alnumc = sv_dup_inc(proto_perl->Iutf8_alnumc); | |
9124 | PL_utf8_ascii = sv_dup_inc(proto_perl->Iutf8_ascii); | |
9125 | PL_utf8_alpha = sv_dup_inc(proto_perl->Iutf8_alpha); | |
9126 | PL_utf8_space = sv_dup_inc(proto_perl->Iutf8_space); | |
9127 | PL_utf8_cntrl = sv_dup_inc(proto_perl->Iutf8_cntrl); | |
9128 | PL_utf8_graph = sv_dup_inc(proto_perl->Iutf8_graph); | |
9129 | PL_utf8_digit = sv_dup_inc(proto_perl->Iutf8_digit); | |
9130 | PL_utf8_upper = sv_dup_inc(proto_perl->Iutf8_upper); | |
9131 | PL_utf8_lower = sv_dup_inc(proto_perl->Iutf8_lower); | |
9132 | PL_utf8_print = sv_dup_inc(proto_perl->Iutf8_print); | |
9133 | PL_utf8_punct = sv_dup_inc(proto_perl->Iutf8_punct); | |
9134 | PL_utf8_xdigit = sv_dup_inc(proto_perl->Iutf8_xdigit); | |
9135 | PL_utf8_mark = sv_dup_inc(proto_perl->Iutf8_mark); | |
9136 | PL_utf8_toupper = sv_dup_inc(proto_perl->Iutf8_toupper); | |
9137 | PL_utf8_totitle = sv_dup_inc(proto_perl->Iutf8_totitle); | |
9138 | PL_utf8_tolower = sv_dup_inc(proto_perl->Iutf8_tolower); | |
9139 | ||
9140 | /* swatch cache */ | |
9141 | PL_last_swash_hv = Nullhv; /* reinits on demand */ | |
9142 | PL_last_swash_klen = 0; | |
9143 | PL_last_swash_key[0]= '\0'; | |
9144 | PL_last_swash_tmps = (U8*)NULL; | |
9145 | PL_last_swash_slen = 0; | |
9146 | ||
9147 | /* perly.c globals */ | |
9148 | PL_yydebug = proto_perl->Iyydebug; | |
9149 | PL_yynerrs = proto_perl->Iyynerrs; | |
9150 | PL_yyerrflag = proto_perl->Iyyerrflag; | |
9151 | PL_yychar = proto_perl->Iyychar; | |
9152 | PL_yyval = proto_perl->Iyyval; | |
9153 | PL_yylval = proto_perl->Iyylval; | |
9154 | ||
9155 | PL_glob_index = proto_perl->Iglob_index; | |
9156 | PL_srand_called = proto_perl->Isrand_called; | |
9157 | PL_uudmap['M'] = 0; /* reinits on demand */ | |
9158 | PL_bitcount = Nullch; /* reinits on demand */ | |
9159 | ||
66fe0623 NIS |
9160 | if (proto_perl->Ipsig_pend) { |
9161 | Newz(0, PL_psig_pend, SIG_SIZE, int); | |
9dd79c3f | 9162 | } |
66fe0623 NIS |
9163 | else { |
9164 | PL_psig_pend = (int*)NULL; | |
9165 | } | |
9166 | ||
1d7c1841 | 9167 | if (proto_perl->Ipsig_ptr) { |
76d3c696 JH |
9168 | Newz(0, PL_psig_ptr, SIG_SIZE, SV*); |
9169 | Newz(0, PL_psig_name, SIG_SIZE, SV*); | |
76d3c696 JH |
9170 | for (i = 1; i < SIG_SIZE; i++) { |
9171 | PL_psig_ptr[i] = sv_dup_inc(proto_perl->Ipsig_ptr[i]); | |
1d7c1841 GS |
9172 | PL_psig_name[i] = sv_dup_inc(proto_perl->Ipsig_name[i]); |
9173 | } | |
9174 | } | |
9175 | else { | |
9176 | PL_psig_ptr = (SV**)NULL; | |
9177 | PL_psig_name = (SV**)NULL; | |
9178 | } | |
9179 | ||
9180 | /* thrdvar.h stuff */ | |
9181 | ||
a0739874 | 9182 | if (flags & CLONEf_COPY_STACKS) { |
1d7c1841 GS |
9183 | /* next allocation will be PL_tmps_stack[PL_tmps_ix+1] */ |
9184 | PL_tmps_ix = proto_perl->Ttmps_ix; | |
9185 | PL_tmps_max = proto_perl->Ttmps_max; | |
9186 | PL_tmps_floor = proto_perl->Ttmps_floor; | |
9187 | Newz(50, PL_tmps_stack, PL_tmps_max, SV*); | |
9188 | i = 0; | |
9189 | while (i <= PL_tmps_ix) { | |
9190 | PL_tmps_stack[i] = sv_dup_inc(proto_perl->Ttmps_stack[i]); | |
9191 | ++i; | |
9192 | } | |
9193 | ||
9194 | /* next PUSHMARK() sets *(PL_markstack_ptr+1) */ | |
9195 | i = proto_perl->Tmarkstack_max - proto_perl->Tmarkstack; | |
9196 | Newz(54, PL_markstack, i, I32); | |
9197 | PL_markstack_max = PL_markstack + (proto_perl->Tmarkstack_max | |
9198 | - proto_perl->Tmarkstack); | |
9199 | PL_markstack_ptr = PL_markstack + (proto_perl->Tmarkstack_ptr | |
9200 | - proto_perl->Tmarkstack); | |
9201 | Copy(proto_perl->Tmarkstack, PL_markstack, | |
9202 | PL_markstack_ptr - PL_markstack + 1, I32); | |
9203 | ||
9204 | /* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix] | |
9205 | * NOTE: unlike the others! */ | |
9206 | PL_scopestack_ix = proto_perl->Tscopestack_ix; | |
9207 | PL_scopestack_max = proto_perl->Tscopestack_max; | |
9208 | Newz(54, PL_scopestack, PL_scopestack_max, I32); | |
9209 | Copy(proto_perl->Tscopestack, PL_scopestack, PL_scopestack_ix, I32); | |
9210 | ||
9211 | /* next push_return() sets PL_retstack[PL_retstack_ix] | |
9212 | * NOTE: unlike the others! */ | |
9213 | PL_retstack_ix = proto_perl->Tretstack_ix; | |
9214 | PL_retstack_max = proto_perl->Tretstack_max; | |
9215 | Newz(54, PL_retstack, PL_retstack_max, OP*); | |
9216 | Copy(proto_perl->Tretstack, PL_retstack, PL_retstack_ix, I32); | |
9217 | ||
9218 | /* NOTE: si_dup() looks at PL_markstack */ | |
9219 | PL_curstackinfo = si_dup(proto_perl->Tcurstackinfo); | |
9220 | ||
9221 | /* PL_curstack = PL_curstackinfo->si_stack; */ | |
9222 | PL_curstack = av_dup(proto_perl->Tcurstack); | |
9223 | PL_mainstack = av_dup(proto_perl->Tmainstack); | |
9224 | ||
9225 | /* next PUSHs() etc. set *(PL_stack_sp+1) */ | |
9226 | PL_stack_base = AvARRAY(PL_curstack); | |
9227 | PL_stack_sp = PL_stack_base + (proto_perl->Tstack_sp | |
9228 | - proto_perl->Tstack_base); | |
9229 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); | |
9230 | ||
9231 | /* next SSPUSHFOO() sets PL_savestack[PL_savestack_ix] | |
9232 | * NOTE: unlike the others! */ | |
9233 | PL_savestack_ix = proto_perl->Tsavestack_ix; | |
9234 | PL_savestack_max = proto_perl->Tsavestack_max; | |
9235 | /*Newz(54, PL_savestack, PL_savestack_max, ANY);*/ | |
9236 | PL_savestack = ss_dup(proto_perl); | |
9237 | } | |
9238 | else { | |
9239 | init_stacks(); | |
985e7056 | 9240 | ENTER; /* perl_destruct() wants to LEAVE; */ |
1d7c1841 GS |
9241 | } |
9242 | ||
9243 | PL_start_env = proto_perl->Tstart_env; /* XXXXXX */ | |
9244 | PL_top_env = &PL_start_env; | |
9245 | ||
9246 | PL_op = proto_perl->Top; | |
9247 | ||
9248 | PL_Sv = Nullsv; | |
9249 | PL_Xpv = (XPV*)NULL; | |
9250 | PL_na = proto_perl->Tna; | |
9251 | ||
9252 | PL_statbuf = proto_perl->Tstatbuf; | |
9253 | PL_statcache = proto_perl->Tstatcache; | |
9254 | PL_statgv = gv_dup(proto_perl->Tstatgv); | |
9255 | PL_statname = sv_dup_inc(proto_perl->Tstatname); | |
9256 | #ifdef HAS_TIMES | |
9257 | PL_timesbuf = proto_perl->Ttimesbuf; | |
9258 | #endif | |
9259 | ||
9260 | PL_tainted = proto_perl->Ttainted; | |
9261 | PL_curpm = proto_perl->Tcurpm; /* XXX No PMOP ref count */ | |
9262 | PL_nrs = sv_dup_inc(proto_perl->Tnrs); | |
9263 | PL_rs = sv_dup_inc(proto_perl->Trs); | |
9264 | PL_last_in_gv = gv_dup(proto_perl->Tlast_in_gv); | |
7889fe52 | 9265 | PL_ofs_sv = sv_dup_inc(proto_perl->Tofs_sv); |
1d7c1841 GS |
9266 | PL_defoutgv = gv_dup_inc(proto_perl->Tdefoutgv); |
9267 | PL_chopset = proto_perl->Tchopset; /* XXX never deallocated */ | |
9268 | PL_toptarget = sv_dup_inc(proto_perl->Ttoptarget); | |
9269 | PL_bodytarget = sv_dup_inc(proto_perl->Tbodytarget); | |
9270 | PL_formtarget = sv_dup(proto_perl->Tformtarget); | |
9271 | ||
9272 | PL_restartop = proto_perl->Trestartop; | |
9273 | PL_in_eval = proto_perl->Tin_eval; | |
9274 | PL_delaymagic = proto_perl->Tdelaymagic; | |
9275 | PL_dirty = proto_perl->Tdirty; | |
9276 | PL_localizing = proto_perl->Tlocalizing; | |
9277 | ||
14dd3ad8 | 9278 | #ifdef PERL_FLEXIBLE_EXCEPTIONS |
1d7c1841 | 9279 | PL_protect = proto_perl->Tprotect; |
14dd3ad8 | 9280 | #endif |
1d7c1841 GS |
9281 | PL_errors = sv_dup_inc(proto_perl->Terrors); |
9282 | PL_av_fetch_sv = Nullsv; | |
9283 | PL_hv_fetch_sv = Nullsv; | |
9284 | Zero(&PL_hv_fetch_ent_mh, 1, HE); /* XXX */ | |
9285 | PL_modcount = proto_perl->Tmodcount; | |
9286 | PL_lastgotoprobe = Nullop; | |
9287 | PL_dumpindent = proto_perl->Tdumpindent; | |
9288 | ||
9289 | PL_sortcop = (OP*)any_dup(proto_perl->Tsortcop, proto_perl); | |
9290 | PL_sortstash = hv_dup(proto_perl->Tsortstash); | |
9291 | PL_firstgv = gv_dup(proto_perl->Tfirstgv); | |
9292 | PL_secondgv = gv_dup(proto_perl->Tsecondgv); | |
9293 | PL_sortcxix = proto_perl->Tsortcxix; | |
9294 | PL_efloatbuf = Nullch; /* reinits on demand */ | |
9295 | PL_efloatsize = 0; /* reinits on demand */ | |
9296 | ||
9297 | /* regex stuff */ | |
9298 | ||
9299 | PL_screamfirst = NULL; | |
9300 | PL_screamnext = NULL; | |
9301 | PL_maxscream = -1; /* reinits on demand */ | |
9302 | PL_lastscream = Nullsv; | |
9303 | ||
9304 | PL_watchaddr = NULL; | |
9305 | PL_watchok = Nullch; | |
9306 | ||
9307 | PL_regdummy = proto_perl->Tregdummy; | |
9308 | PL_regcomp_parse = Nullch; | |
9309 | PL_regxend = Nullch; | |
9310 | PL_regcode = (regnode*)NULL; | |
9311 | PL_regnaughty = 0; | |
9312 | PL_regsawback = 0; | |
9313 | PL_regprecomp = Nullch; | |
9314 | PL_regnpar = 0; | |
9315 | PL_regsize = 0; | |
9316 | PL_regflags = 0; | |
9317 | PL_regseen = 0; | |
9318 | PL_seen_zerolen = 0; | |
9319 | PL_seen_evals = 0; | |
9320 | PL_regcomp_rx = (regexp*)NULL; | |
9321 | PL_extralen = 0; | |
9322 | PL_colorset = 0; /* reinits PL_colors[] */ | |
9323 | /*PL_colors[6] = {0,0,0,0,0,0};*/ | |
9324 | PL_reg_whilem_seen = 0; | |
9325 | PL_reginput = Nullch; | |
9326 | PL_regbol = Nullch; | |
9327 | PL_regeol = Nullch; | |
9328 | PL_regstartp = (I32*)NULL; | |
9329 | PL_regendp = (I32*)NULL; | |
9330 | PL_reglastparen = (U32*)NULL; | |
9331 | PL_regtill = Nullch; | |
1d7c1841 GS |
9332 | PL_reg_start_tmp = (char**)NULL; |
9333 | PL_reg_start_tmpl = 0; | |
9334 | PL_regdata = (struct reg_data*)NULL; | |
9335 | PL_bostr = Nullch; | |
9336 | PL_reg_flags = 0; | |
9337 | PL_reg_eval_set = 0; | |
9338 | PL_regnarrate = 0; | |
9339 | PL_regprogram = (regnode*)NULL; | |
9340 | PL_regindent = 0; | |
9341 | PL_regcc = (CURCUR*)NULL; | |
9342 | PL_reg_call_cc = (struct re_cc_state*)NULL; | |
9343 | PL_reg_re = (regexp*)NULL; | |
9344 | PL_reg_ganch = Nullch; | |
9345 | PL_reg_sv = Nullsv; | |
9346 | PL_reg_magic = (MAGIC*)NULL; | |
9347 | PL_reg_oldpos = 0; | |
9348 | PL_reg_oldcurpm = (PMOP*)NULL; | |
9349 | PL_reg_curpm = (PMOP*)NULL; | |
9350 | PL_reg_oldsaved = Nullch; | |
9351 | PL_reg_oldsavedlen = 0; | |
9352 | PL_reg_maxiter = 0; | |
9353 | PL_reg_leftiter = 0; | |
9354 | PL_reg_poscache = Nullch; | |
9355 | PL_reg_poscache_size= 0; | |
9356 | ||
9357 | /* RE engine - function pointers */ | |
9358 | PL_regcompp = proto_perl->Tregcompp; | |
9359 | PL_regexecp = proto_perl->Tregexecp; | |
9360 | PL_regint_start = proto_perl->Tregint_start; | |
9361 | PL_regint_string = proto_perl->Tregint_string; | |
9362 | PL_regfree = proto_perl->Tregfree; | |
9363 | ||
9364 | PL_reginterp_cnt = 0; | |
9365 | PL_reg_starttry = 0; | |
9366 | ||
a0739874 DM |
9367 | if (!(flags & CLONEf_KEEP_PTR_TABLE)) { |
9368 | ptr_table_free(PL_ptr_table); | |
9369 | PL_ptr_table = NULL; | |
9370 | } | |
9371 | ||
1d7c1841 GS |
9372 | #ifdef PERL_OBJECT |
9373 | return (PerlInterpreter*)pPerl; | |
9374 | #else | |
9375 | return my_perl; | |
9376 | #endif | |
9377 | } | |
9378 | ||
9379 | #else /* !USE_ITHREADS */ | |
51371543 GS |
9380 | |
9381 | #ifdef PERL_OBJECT | |
51371543 GS |
9382 | #include "XSUB.h" |
9383 | #endif | |
9384 | ||
1d7c1841 GS |
9385 | #endif /* USE_ITHREADS */ |
9386 | ||
51371543 GS |
9387 | static void |
9388 | do_report_used(pTHXo_ SV *sv) | |
9389 | { | |
9390 | if (SvTYPE(sv) != SVTYPEMASK) { | |
bf49b057 | 9391 | PerlIO_printf(Perl_debug_log, "****\n"); |
51371543 GS |
9392 | sv_dump(sv); |
9393 | } | |
9394 | } | |
9395 | ||
9396 | static void | |
9397 | do_clean_objs(pTHXo_ SV *sv) | |
9398 | { | |
9399 | SV* rv; | |
9400 | ||
9401 | if (SvROK(sv) && SvOBJECT(rv = SvRV(sv))) { | |
9402 | DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(sv));) | |
8b6e653b HS |
9403 | if (SvWEAKREF(sv)) { |
9404 | sv_del_backref(sv); | |
9405 | SvWEAKREF_off(sv); | |
9406 | SvRV(sv) = 0; | |
9407 | } else { | |
9408 | SvROK_off(sv); | |
9409 | SvRV(sv) = 0; | |
9410 | SvREFCNT_dec(rv); | |
9411 | } | |
51371543 GS |
9412 | } |
9413 | ||
9414 | /* XXX Might want to check arrays, etc. */ | |
9415 | } | |
9416 | ||
9417 | #ifndef DISABLE_DESTRUCTOR_KLUDGE | |
9418 | static void | |
9419 | do_clean_named_objs(pTHXo_ SV *sv) | |
9420 | { | |
f472eb5c | 9421 | if (SvTYPE(sv) == SVt_PVGV && GvGP(sv)) { |
51371543 | 9422 | if ( SvOBJECT(GvSV(sv)) || |
155aba94 GS |
9423 | (GvAV(sv) && SvOBJECT(GvAV(sv))) || |
9424 | (GvHV(sv) && SvOBJECT(GvHV(sv))) || | |
9425 | (GvIO(sv) && SvOBJECT(GvIO(sv))) || | |
9426 | (GvCV(sv) && SvOBJECT(GvCV(sv))) ) | |
51371543 GS |
9427 | { |
9428 | DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning named glob object:\n "), sv_dump(sv));) | |
9429 | SvREFCNT_dec(sv); | |
9430 | } | |
9431 | } | |
9432 | } | |
9433 | #endif | |
9434 | ||
9435 | static void | |
9436 | do_clean_all(pTHXo_ SV *sv) | |
9437 | { | |
1d7c1841 | 9438 | DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) );) |
51371543 GS |
9439 | SvFLAGS(sv) |= SVf_BREAK; |
9440 | SvREFCNT_dec(sv); | |
9441 | } | |
8af02333 | 9442 |