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; \ | |
66 | if (PL_debug & 32768) \ | |
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 | { |
3280af22 | 76 | if (PL_debug & 32768) { |
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; | |
c07a80fd | 140 | } |
1edc1566 | 141 | else { |
142 | char *chunk; /* must use New here to match call to */ | |
143 | New(704,chunk,1008,char); /* Safefree() in sv_free_arenas() */ | |
144 | sv_add_arena(chunk, 1008, 0); | |
145 | } | |
4561caa4 CS |
146 | uproot_SV(sv); |
147 | return sv; | |
463ee0b2 LW |
148 | } |
149 | ||
76e3520e | 150 | STATIC void |
cea2e8a9 | 151 | S_visit(pTHX_ SVFUNC_t f) |
8990e307 | 152 | { |
4633a7c4 | 153 | SV* sva; |
8990e307 LW |
154 | SV* sv; |
155 | register SV* svend; | |
156 | ||
3280af22 | 157 | for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) { |
4633a7c4 | 158 | svend = &sva[SvREFCNT(sva)]; |
4561caa4 CS |
159 | for (sv = sva + 1; sv < svend; ++sv) { |
160 | if (SvTYPE(sv) != SVTYPEMASK) | |
51371543 | 161 | (FCALL)(aTHXo_ sv); |
8990e307 LW |
162 | } |
163 | } | |
164 | } | |
165 | ||
166 | void | |
864dbfa3 | 167 | Perl_sv_report_used(pTHX) |
4561caa4 | 168 | { |
0b94c7bb | 169 | visit(do_report_used); |
4561caa4 CS |
170 | } |
171 | ||
4561caa4 | 172 | void |
864dbfa3 | 173 | Perl_sv_clean_objs(pTHX) |
4561caa4 | 174 | { |
3280af22 | 175 | PL_in_clean_objs = TRUE; |
0b94c7bb | 176 | visit(do_clean_objs); |
4561caa4 | 177 | #ifndef DISABLE_DESTRUCTOR_KLUDGE |
2d0f3c12 | 178 | /* some barnacles may yet remain, clinging to typeglobs */ |
0b94c7bb | 179 | visit(do_clean_named_objs); |
4561caa4 | 180 | #endif |
3280af22 | 181 | PL_in_clean_objs = FALSE; |
4561caa4 CS |
182 | } |
183 | ||
8990e307 | 184 | void |
864dbfa3 | 185 | Perl_sv_clean_all(pTHX) |
8990e307 | 186 | { |
3280af22 | 187 | PL_in_clean_all = TRUE; |
0b94c7bb | 188 | visit(do_clean_all); |
3280af22 | 189 | PL_in_clean_all = FALSE; |
8990e307 | 190 | } |
463ee0b2 | 191 | |
4633a7c4 | 192 | void |
864dbfa3 | 193 | Perl_sv_free_arenas(pTHX) |
4633a7c4 LW |
194 | { |
195 | SV* sva; | |
196 | SV* svanext; | |
612f20c3 | 197 | XPV *arena, *arenanext; |
4633a7c4 LW |
198 | |
199 | /* Free arenas here, but be careful about fake ones. (We assume | |
200 | contiguity of the fake ones with the corresponding real ones.) */ | |
201 | ||
3280af22 | 202 | for (sva = PL_sv_arenaroot; sva; sva = svanext) { |
4633a7c4 LW |
203 | svanext = (SV*) SvANY(sva); |
204 | while (svanext && SvFAKE(svanext)) | |
205 | svanext = (SV*) SvANY(svanext); | |
206 | ||
207 | if (!SvFAKE(sva)) | |
1edc1566 | 208 | Safefree((void *)sva); |
4633a7c4 | 209 | } |
5f05dabc | 210 | |
612f20c3 GS |
211 | for (arena = PL_xiv_arenaroot; arena; arena = arenanext) { |
212 | arenanext = (XPV*)arena->xpv_pv; | |
213 | Safefree(arena); | |
214 | } | |
215 | PL_xiv_arenaroot = 0; | |
216 | ||
217 | for (arena = PL_xnv_arenaroot; arena; arena = arenanext) { | |
218 | arenanext = (XPV*)arena->xpv_pv; | |
219 | Safefree(arena); | |
220 | } | |
221 | PL_xnv_arenaroot = 0; | |
222 | ||
223 | for (arena = PL_xrv_arenaroot; arena; arena = arenanext) { | |
224 | arenanext = (XPV*)arena->xpv_pv; | |
225 | Safefree(arena); | |
226 | } | |
227 | PL_xrv_arenaroot = 0; | |
228 | ||
229 | for (arena = PL_xpv_arenaroot; arena; arena = arenanext) { | |
230 | arenanext = (XPV*)arena->xpv_pv; | |
231 | Safefree(arena); | |
232 | } | |
233 | PL_xpv_arenaroot = 0; | |
234 | ||
235 | for (arena = (XPV*)PL_xpviv_arenaroot; arena; arena = arenanext) { | |
236 | arenanext = (XPV*)arena->xpv_pv; | |
237 | Safefree(arena); | |
238 | } | |
239 | PL_xpviv_arenaroot = 0; | |
240 | ||
241 | for (arena = (XPV*)PL_xpvnv_arenaroot; arena; arena = arenanext) { | |
242 | arenanext = (XPV*)arena->xpv_pv; | |
243 | Safefree(arena); | |
244 | } | |
245 | PL_xpvnv_arenaroot = 0; | |
246 | ||
247 | for (arena = (XPV*)PL_xpvcv_arenaroot; arena; arena = arenanext) { | |
248 | arenanext = (XPV*)arena->xpv_pv; | |
249 | Safefree(arena); | |
250 | } | |
251 | PL_xpvcv_arenaroot = 0; | |
252 | ||
253 | for (arena = (XPV*)PL_xpvav_arenaroot; arena; arena = arenanext) { | |
254 | arenanext = (XPV*)arena->xpv_pv; | |
255 | Safefree(arena); | |
256 | } | |
257 | PL_xpvav_arenaroot = 0; | |
258 | ||
259 | for (arena = (XPV*)PL_xpvhv_arenaroot; arena; arena = arenanext) { | |
260 | arenanext = (XPV*)arena->xpv_pv; | |
261 | Safefree(arena); | |
262 | } | |
263 | PL_xpvhv_arenaroot = 0; | |
264 | ||
265 | for (arena = (XPV*)PL_xpvmg_arenaroot; arena; arena = arenanext) { | |
266 | arenanext = (XPV*)arena->xpv_pv; | |
267 | Safefree(arena); | |
268 | } | |
269 | PL_xpvmg_arenaroot = 0; | |
270 | ||
271 | for (arena = (XPV*)PL_xpvlv_arenaroot; arena; arena = arenanext) { | |
272 | arenanext = (XPV*)arena->xpv_pv; | |
273 | Safefree(arena); | |
274 | } | |
275 | PL_xpvlv_arenaroot = 0; | |
276 | ||
277 | for (arena = (XPV*)PL_xpvbm_arenaroot; arena; arena = arenanext) { | |
278 | arenanext = (XPV*)arena->xpv_pv; | |
279 | Safefree(arena); | |
280 | } | |
281 | PL_xpvbm_arenaroot = 0; | |
282 | ||
283 | for (arena = (XPV*)PL_he_arenaroot; arena; arena = arenanext) { | |
284 | arenanext = (XPV*)arena->xpv_pv; | |
285 | Safefree(arena); | |
286 | } | |
287 | PL_he_arenaroot = 0; | |
288 | ||
3280af22 NIS |
289 | if (PL_nice_chunk) |
290 | Safefree(PL_nice_chunk); | |
291 | PL_nice_chunk = Nullch; | |
292 | PL_nice_chunk_size = 0; | |
293 | PL_sv_arenaroot = 0; | |
294 | PL_sv_root = 0; | |
4633a7c4 LW |
295 | } |
296 | ||
1d7c1841 GS |
297 | void |
298 | Perl_report_uninit(pTHX) | |
299 | { | |
300 | if (PL_op) | |
301 | Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit, | |
302 | " in ", PL_op_desc[PL_op->op_type]); | |
303 | else | |
304 | Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit, "", ""); | |
305 | } | |
306 | ||
76e3520e | 307 | STATIC XPVIV* |
cea2e8a9 | 308 | S_new_xiv(pTHX) |
463ee0b2 | 309 | { |
ea7c11a3 | 310 | IV* xiv; |
cbe51380 GS |
311 | LOCK_SV_MUTEX; |
312 | if (!PL_xiv_root) | |
313 | more_xiv(); | |
314 | xiv = PL_xiv_root; | |
315 | /* | |
316 | * See comment in more_xiv() -- RAM. | |
317 | */ | |
318 | PL_xiv_root = *(IV**)xiv; | |
319 | UNLOCK_SV_MUTEX; | |
320 | return (XPVIV*)((char*)xiv - STRUCT_OFFSET(XPVIV, xiv_iv)); | |
463ee0b2 LW |
321 | } |
322 | ||
76e3520e | 323 | STATIC void |
cea2e8a9 | 324 | S_del_xiv(pTHX_ XPVIV *p) |
463ee0b2 | 325 | { |
23e6a22f | 326 | IV* xiv = (IV*)((char*)(p) + STRUCT_OFFSET(XPVIV, xiv_iv)); |
cbe51380 | 327 | LOCK_SV_MUTEX; |
3280af22 NIS |
328 | *(IV**)xiv = PL_xiv_root; |
329 | PL_xiv_root = xiv; | |
cbe51380 | 330 | UNLOCK_SV_MUTEX; |
463ee0b2 LW |
331 | } |
332 | ||
cbe51380 | 333 | STATIC void |
cea2e8a9 | 334 | S_more_xiv(pTHX) |
463ee0b2 | 335 | { |
ea7c11a3 SM |
336 | register IV* xiv; |
337 | register IV* xivend; | |
8c52afec IZ |
338 | XPV* ptr; |
339 | New(705, ptr, 1008/sizeof(XPV), XPV); | |
3280af22 NIS |
340 | ptr->xpv_pv = (char*)PL_xiv_arenaroot; /* linked list of xiv arenas */ |
341 | PL_xiv_arenaroot = ptr; /* to keep Purify happy */ | |
a0d0e21e | 342 | |
ea7c11a3 SM |
343 | xiv = (IV*) ptr; |
344 | xivend = &xiv[1008 / sizeof(IV) - 1]; | |
345 | xiv += (sizeof(XPV) - 1) / sizeof(IV) + 1; /* fudge by size of XPV */ | |
3280af22 | 346 | PL_xiv_root = xiv; |
463ee0b2 | 347 | while (xiv < xivend) { |
ea7c11a3 | 348 | *(IV**)xiv = (IV *)(xiv + 1); |
463ee0b2 LW |
349 | xiv++; |
350 | } | |
ea7c11a3 | 351 | *(IV**)xiv = 0; |
463ee0b2 LW |
352 | } |
353 | ||
76e3520e | 354 | STATIC XPVNV* |
cea2e8a9 | 355 | S_new_xnv(pTHX) |
463ee0b2 | 356 | { |
65202027 | 357 | NV* xnv; |
cbe51380 GS |
358 | LOCK_SV_MUTEX; |
359 | if (!PL_xnv_root) | |
360 | more_xnv(); | |
361 | xnv = PL_xnv_root; | |
65202027 | 362 | PL_xnv_root = *(NV**)xnv; |
cbe51380 GS |
363 | UNLOCK_SV_MUTEX; |
364 | return (XPVNV*)((char*)xnv - STRUCT_OFFSET(XPVNV, xnv_nv)); | |
463ee0b2 LW |
365 | } |
366 | ||
76e3520e | 367 | STATIC void |
cea2e8a9 | 368 | S_del_xnv(pTHX_ XPVNV *p) |
463ee0b2 | 369 | { |
65202027 | 370 | NV* xnv = (NV*)((char*)(p) + STRUCT_OFFSET(XPVNV, xnv_nv)); |
cbe51380 | 371 | LOCK_SV_MUTEX; |
65202027 | 372 | *(NV**)xnv = PL_xnv_root; |
3280af22 | 373 | PL_xnv_root = xnv; |
cbe51380 | 374 | UNLOCK_SV_MUTEX; |
463ee0b2 LW |
375 | } |
376 | ||
cbe51380 | 377 | STATIC void |
cea2e8a9 | 378 | S_more_xnv(pTHX) |
463ee0b2 | 379 | { |
65202027 DS |
380 | register NV* xnv; |
381 | register NV* xnvend; | |
612f20c3 GS |
382 | XPV *ptr; |
383 | New(711, ptr, 1008/sizeof(XPV), XPV); | |
384 | ptr->xpv_pv = (char*)PL_xnv_arenaroot; | |
385 | PL_xnv_arenaroot = ptr; | |
386 | ||
387 | xnv = (NV*) ptr; | |
65202027 DS |
388 | xnvend = &xnv[1008 / sizeof(NV) - 1]; |
389 | xnv += (sizeof(XPVIV) - 1) / sizeof(NV) + 1; /* fudge by sizeof XPVIV */ | |
3280af22 | 390 | PL_xnv_root = xnv; |
463ee0b2 | 391 | while (xnv < xnvend) { |
65202027 | 392 | *(NV**)xnv = (NV*)(xnv + 1); |
463ee0b2 LW |
393 | xnv++; |
394 | } | |
65202027 | 395 | *(NV**)xnv = 0; |
463ee0b2 LW |
396 | } |
397 | ||
76e3520e | 398 | STATIC XRV* |
cea2e8a9 | 399 | S_new_xrv(pTHX) |
ed6116ce LW |
400 | { |
401 | XRV* xrv; | |
cbe51380 GS |
402 | LOCK_SV_MUTEX; |
403 | if (!PL_xrv_root) | |
404 | more_xrv(); | |
405 | xrv = PL_xrv_root; | |
406 | PL_xrv_root = (XRV*)xrv->xrv_rv; | |
407 | UNLOCK_SV_MUTEX; | |
408 | return xrv; | |
ed6116ce LW |
409 | } |
410 | ||
76e3520e | 411 | STATIC void |
cea2e8a9 | 412 | S_del_xrv(pTHX_ XRV *p) |
ed6116ce | 413 | { |
cbe51380 | 414 | LOCK_SV_MUTEX; |
3280af22 NIS |
415 | p->xrv_rv = (SV*)PL_xrv_root; |
416 | PL_xrv_root = p; | |
cbe51380 | 417 | UNLOCK_SV_MUTEX; |
ed6116ce LW |
418 | } |
419 | ||
cbe51380 | 420 | STATIC void |
cea2e8a9 | 421 | S_more_xrv(pTHX) |
ed6116ce | 422 | { |
ed6116ce LW |
423 | register XRV* xrv; |
424 | register XRV* xrvend; | |
612f20c3 GS |
425 | XPV *ptr; |
426 | New(712, ptr, 1008/sizeof(XPV), XPV); | |
427 | ptr->xpv_pv = (char*)PL_xrv_arenaroot; | |
428 | PL_xrv_arenaroot = ptr; | |
429 | ||
430 | xrv = (XRV*) ptr; | |
ed6116ce | 431 | xrvend = &xrv[1008 / sizeof(XRV) - 1]; |
612f20c3 GS |
432 | xrv += (sizeof(XPV) - 1) / sizeof(XRV) + 1; |
433 | PL_xrv_root = xrv; | |
ed6116ce LW |
434 | while (xrv < xrvend) { |
435 | xrv->xrv_rv = (SV*)(xrv + 1); | |
436 | xrv++; | |
437 | } | |
438 | xrv->xrv_rv = 0; | |
ed6116ce LW |
439 | } |
440 | ||
76e3520e | 441 | STATIC XPV* |
cea2e8a9 | 442 | S_new_xpv(pTHX) |
463ee0b2 LW |
443 | { |
444 | XPV* xpv; | |
cbe51380 GS |
445 | LOCK_SV_MUTEX; |
446 | if (!PL_xpv_root) | |
447 | more_xpv(); | |
448 | xpv = PL_xpv_root; | |
449 | PL_xpv_root = (XPV*)xpv->xpv_pv; | |
450 | UNLOCK_SV_MUTEX; | |
451 | return xpv; | |
463ee0b2 LW |
452 | } |
453 | ||
76e3520e | 454 | STATIC void |
cea2e8a9 | 455 | S_del_xpv(pTHX_ XPV *p) |
463ee0b2 | 456 | { |
cbe51380 | 457 | LOCK_SV_MUTEX; |
3280af22 NIS |
458 | p->xpv_pv = (char*)PL_xpv_root; |
459 | PL_xpv_root = p; | |
cbe51380 | 460 | UNLOCK_SV_MUTEX; |
463ee0b2 LW |
461 | } |
462 | ||
cbe51380 | 463 | STATIC void |
cea2e8a9 | 464 | S_more_xpv(pTHX) |
463ee0b2 | 465 | { |
463ee0b2 LW |
466 | register XPV* xpv; |
467 | register XPV* xpvend; | |
612f20c3 GS |
468 | New(713, xpv, 1008/sizeof(XPV), XPV); |
469 | xpv->xpv_pv = (char*)PL_xpv_arenaroot; | |
470 | PL_xpv_arenaroot = xpv; | |
471 | ||
463ee0b2 | 472 | xpvend = &xpv[1008 / sizeof(XPV) - 1]; |
612f20c3 | 473 | PL_xpv_root = ++xpv; |
463ee0b2 LW |
474 | while (xpv < xpvend) { |
475 | xpv->xpv_pv = (char*)(xpv + 1); | |
476 | xpv++; | |
477 | } | |
478 | xpv->xpv_pv = 0; | |
463ee0b2 LW |
479 | } |
480 | ||
932e9ff9 VB |
481 | STATIC XPVIV* |
482 | S_new_xpviv(pTHX) | |
483 | { | |
484 | XPVIV* xpviv; | |
485 | LOCK_SV_MUTEX; | |
486 | if (!PL_xpviv_root) | |
487 | more_xpviv(); | |
488 | xpviv = PL_xpviv_root; | |
489 | PL_xpviv_root = (XPVIV*)xpviv->xpv_pv; | |
490 | UNLOCK_SV_MUTEX; | |
491 | return xpviv; | |
492 | } | |
493 | ||
494 | STATIC void | |
495 | S_del_xpviv(pTHX_ XPVIV *p) | |
496 | { | |
497 | LOCK_SV_MUTEX; | |
498 | p->xpv_pv = (char*)PL_xpviv_root; | |
499 | PL_xpviv_root = p; | |
500 | UNLOCK_SV_MUTEX; | |
501 | } | |
502 | ||
932e9ff9 VB |
503 | STATIC void |
504 | S_more_xpviv(pTHX) | |
505 | { | |
506 | register XPVIV* xpviv; | |
507 | register XPVIV* xpvivend; | |
612f20c3 GS |
508 | New(714, xpviv, 1008/sizeof(XPVIV), XPVIV); |
509 | xpviv->xpv_pv = (char*)PL_xpviv_arenaroot; | |
510 | PL_xpviv_arenaroot = xpviv; | |
511 | ||
932e9ff9 | 512 | xpvivend = &xpviv[1008 / sizeof(XPVIV) - 1]; |
612f20c3 | 513 | PL_xpviv_root = ++xpviv; |
932e9ff9 VB |
514 | while (xpviv < xpvivend) { |
515 | xpviv->xpv_pv = (char*)(xpviv + 1); | |
516 | xpviv++; | |
517 | } | |
518 | xpviv->xpv_pv = 0; | |
519 | } | |
520 | ||
932e9ff9 VB |
521 | STATIC XPVNV* |
522 | S_new_xpvnv(pTHX) | |
523 | { | |
524 | XPVNV* xpvnv; | |
525 | LOCK_SV_MUTEX; | |
526 | if (!PL_xpvnv_root) | |
527 | more_xpvnv(); | |
528 | xpvnv = PL_xpvnv_root; | |
529 | PL_xpvnv_root = (XPVNV*)xpvnv->xpv_pv; | |
530 | UNLOCK_SV_MUTEX; | |
531 | return xpvnv; | |
532 | } | |
533 | ||
534 | STATIC void | |
535 | S_del_xpvnv(pTHX_ XPVNV *p) | |
536 | { | |
537 | LOCK_SV_MUTEX; | |
538 | p->xpv_pv = (char*)PL_xpvnv_root; | |
539 | PL_xpvnv_root = p; | |
540 | UNLOCK_SV_MUTEX; | |
541 | } | |
542 | ||
932e9ff9 VB |
543 | STATIC void |
544 | S_more_xpvnv(pTHX) | |
545 | { | |
546 | register XPVNV* xpvnv; | |
547 | register XPVNV* xpvnvend; | |
612f20c3 GS |
548 | New(715, xpvnv, 1008/sizeof(XPVNV), XPVNV); |
549 | xpvnv->xpv_pv = (char*)PL_xpvnv_arenaroot; | |
550 | PL_xpvnv_arenaroot = xpvnv; | |
551 | ||
932e9ff9 | 552 | xpvnvend = &xpvnv[1008 / sizeof(XPVNV) - 1]; |
612f20c3 | 553 | PL_xpvnv_root = ++xpvnv; |
932e9ff9 VB |
554 | while (xpvnv < xpvnvend) { |
555 | xpvnv->xpv_pv = (char*)(xpvnv + 1); | |
556 | xpvnv++; | |
557 | } | |
558 | xpvnv->xpv_pv = 0; | |
559 | } | |
560 | ||
932e9ff9 VB |
561 | STATIC XPVCV* |
562 | S_new_xpvcv(pTHX) | |
563 | { | |
564 | XPVCV* xpvcv; | |
565 | LOCK_SV_MUTEX; | |
566 | if (!PL_xpvcv_root) | |
567 | more_xpvcv(); | |
568 | xpvcv = PL_xpvcv_root; | |
569 | PL_xpvcv_root = (XPVCV*)xpvcv->xpv_pv; | |
570 | UNLOCK_SV_MUTEX; | |
571 | return xpvcv; | |
572 | } | |
573 | ||
574 | STATIC void | |
575 | S_del_xpvcv(pTHX_ XPVCV *p) | |
576 | { | |
577 | LOCK_SV_MUTEX; | |
578 | p->xpv_pv = (char*)PL_xpvcv_root; | |
579 | PL_xpvcv_root = p; | |
580 | UNLOCK_SV_MUTEX; | |
581 | } | |
582 | ||
932e9ff9 VB |
583 | STATIC void |
584 | S_more_xpvcv(pTHX) | |
585 | { | |
586 | register XPVCV* xpvcv; | |
587 | register XPVCV* xpvcvend; | |
612f20c3 GS |
588 | New(716, xpvcv, 1008/sizeof(XPVCV), XPVCV); |
589 | xpvcv->xpv_pv = (char*)PL_xpvcv_arenaroot; | |
590 | PL_xpvcv_arenaroot = xpvcv; | |
591 | ||
932e9ff9 | 592 | xpvcvend = &xpvcv[1008 / sizeof(XPVCV) - 1]; |
612f20c3 | 593 | PL_xpvcv_root = ++xpvcv; |
932e9ff9 VB |
594 | while (xpvcv < xpvcvend) { |
595 | xpvcv->xpv_pv = (char*)(xpvcv + 1); | |
596 | xpvcv++; | |
597 | } | |
598 | xpvcv->xpv_pv = 0; | |
599 | } | |
600 | ||
932e9ff9 VB |
601 | STATIC XPVAV* |
602 | S_new_xpvav(pTHX) | |
603 | { | |
604 | XPVAV* xpvav; | |
605 | LOCK_SV_MUTEX; | |
606 | if (!PL_xpvav_root) | |
607 | more_xpvav(); | |
608 | xpvav = PL_xpvav_root; | |
609 | PL_xpvav_root = (XPVAV*)xpvav->xav_array; | |
610 | UNLOCK_SV_MUTEX; | |
611 | return xpvav; | |
612 | } | |
613 | ||
614 | STATIC void | |
615 | S_del_xpvav(pTHX_ XPVAV *p) | |
616 | { | |
617 | LOCK_SV_MUTEX; | |
618 | p->xav_array = (char*)PL_xpvav_root; | |
619 | PL_xpvav_root = p; | |
620 | UNLOCK_SV_MUTEX; | |
621 | } | |
622 | ||
932e9ff9 VB |
623 | STATIC void |
624 | S_more_xpvav(pTHX) | |
625 | { | |
626 | register XPVAV* xpvav; | |
627 | register XPVAV* xpvavend; | |
612f20c3 GS |
628 | New(717, xpvav, 1008/sizeof(XPVAV), XPVAV); |
629 | xpvav->xav_array = (char*)PL_xpvav_arenaroot; | |
630 | PL_xpvav_arenaroot = xpvav; | |
631 | ||
932e9ff9 | 632 | xpvavend = &xpvav[1008 / sizeof(XPVAV) - 1]; |
612f20c3 | 633 | PL_xpvav_root = ++xpvav; |
932e9ff9 VB |
634 | while (xpvav < xpvavend) { |
635 | xpvav->xav_array = (char*)(xpvav + 1); | |
636 | xpvav++; | |
637 | } | |
638 | xpvav->xav_array = 0; | |
639 | } | |
640 | ||
932e9ff9 VB |
641 | STATIC XPVHV* |
642 | S_new_xpvhv(pTHX) | |
643 | { | |
644 | XPVHV* xpvhv; | |
645 | LOCK_SV_MUTEX; | |
646 | if (!PL_xpvhv_root) | |
647 | more_xpvhv(); | |
648 | xpvhv = PL_xpvhv_root; | |
649 | PL_xpvhv_root = (XPVHV*)xpvhv->xhv_array; | |
650 | UNLOCK_SV_MUTEX; | |
651 | return xpvhv; | |
652 | } | |
653 | ||
654 | STATIC void | |
655 | S_del_xpvhv(pTHX_ XPVHV *p) | |
656 | { | |
657 | LOCK_SV_MUTEX; | |
658 | p->xhv_array = (char*)PL_xpvhv_root; | |
659 | PL_xpvhv_root = p; | |
660 | UNLOCK_SV_MUTEX; | |
661 | } | |
662 | ||
932e9ff9 VB |
663 | STATIC void |
664 | S_more_xpvhv(pTHX) | |
665 | { | |
666 | register XPVHV* xpvhv; | |
667 | register XPVHV* xpvhvend; | |
612f20c3 GS |
668 | New(718, xpvhv, 1008/sizeof(XPVHV), XPVHV); |
669 | xpvhv->xhv_array = (char*)PL_xpvhv_arenaroot; | |
670 | PL_xpvhv_arenaroot = xpvhv; | |
671 | ||
932e9ff9 | 672 | xpvhvend = &xpvhv[1008 / sizeof(XPVHV) - 1]; |
612f20c3 | 673 | PL_xpvhv_root = ++xpvhv; |
932e9ff9 VB |
674 | while (xpvhv < xpvhvend) { |
675 | xpvhv->xhv_array = (char*)(xpvhv + 1); | |
676 | xpvhv++; | |
677 | } | |
678 | xpvhv->xhv_array = 0; | |
679 | } | |
680 | ||
932e9ff9 VB |
681 | STATIC XPVMG* |
682 | S_new_xpvmg(pTHX) | |
683 | { | |
684 | XPVMG* xpvmg; | |
685 | LOCK_SV_MUTEX; | |
686 | if (!PL_xpvmg_root) | |
687 | more_xpvmg(); | |
688 | xpvmg = PL_xpvmg_root; | |
689 | PL_xpvmg_root = (XPVMG*)xpvmg->xpv_pv; | |
690 | UNLOCK_SV_MUTEX; | |
691 | return xpvmg; | |
692 | } | |
693 | ||
694 | STATIC void | |
695 | S_del_xpvmg(pTHX_ XPVMG *p) | |
696 | { | |
697 | LOCK_SV_MUTEX; | |
698 | p->xpv_pv = (char*)PL_xpvmg_root; | |
699 | PL_xpvmg_root = p; | |
700 | UNLOCK_SV_MUTEX; | |
701 | } | |
702 | ||
932e9ff9 VB |
703 | STATIC void |
704 | S_more_xpvmg(pTHX) | |
705 | { | |
706 | register XPVMG* xpvmg; | |
707 | register XPVMG* xpvmgend; | |
612f20c3 GS |
708 | New(719, xpvmg, 1008/sizeof(XPVMG), XPVMG); |
709 | xpvmg->xpv_pv = (char*)PL_xpvmg_arenaroot; | |
710 | PL_xpvmg_arenaroot = xpvmg; | |
711 | ||
932e9ff9 | 712 | xpvmgend = &xpvmg[1008 / sizeof(XPVMG) - 1]; |
612f20c3 | 713 | PL_xpvmg_root = ++xpvmg; |
932e9ff9 VB |
714 | while (xpvmg < xpvmgend) { |
715 | xpvmg->xpv_pv = (char*)(xpvmg + 1); | |
716 | xpvmg++; | |
717 | } | |
718 | xpvmg->xpv_pv = 0; | |
719 | } | |
720 | ||
932e9ff9 VB |
721 | STATIC XPVLV* |
722 | S_new_xpvlv(pTHX) | |
723 | { | |
724 | XPVLV* xpvlv; | |
725 | LOCK_SV_MUTEX; | |
726 | if (!PL_xpvlv_root) | |
727 | more_xpvlv(); | |
728 | xpvlv = PL_xpvlv_root; | |
729 | PL_xpvlv_root = (XPVLV*)xpvlv->xpv_pv; | |
730 | UNLOCK_SV_MUTEX; | |
731 | return xpvlv; | |
732 | } | |
733 | ||
734 | STATIC void | |
735 | S_del_xpvlv(pTHX_ XPVLV *p) | |
736 | { | |
737 | LOCK_SV_MUTEX; | |
738 | p->xpv_pv = (char*)PL_xpvlv_root; | |
739 | PL_xpvlv_root = p; | |
740 | UNLOCK_SV_MUTEX; | |
741 | } | |
742 | ||
932e9ff9 VB |
743 | STATIC void |
744 | S_more_xpvlv(pTHX) | |
745 | { | |
746 | register XPVLV* xpvlv; | |
747 | register XPVLV* xpvlvend; | |
612f20c3 GS |
748 | New(720, xpvlv, 1008/sizeof(XPVLV), XPVLV); |
749 | xpvlv->xpv_pv = (char*)PL_xpvlv_arenaroot; | |
750 | PL_xpvlv_arenaroot = xpvlv; | |
751 | ||
932e9ff9 | 752 | xpvlvend = &xpvlv[1008 / sizeof(XPVLV) - 1]; |
612f20c3 | 753 | PL_xpvlv_root = ++xpvlv; |
932e9ff9 VB |
754 | while (xpvlv < xpvlvend) { |
755 | xpvlv->xpv_pv = (char*)(xpvlv + 1); | |
756 | xpvlv++; | |
757 | } | |
758 | xpvlv->xpv_pv = 0; | |
759 | } | |
760 | ||
932e9ff9 VB |
761 | STATIC XPVBM* |
762 | S_new_xpvbm(pTHX) | |
763 | { | |
764 | XPVBM* xpvbm; | |
765 | LOCK_SV_MUTEX; | |
766 | if (!PL_xpvbm_root) | |
767 | more_xpvbm(); | |
768 | xpvbm = PL_xpvbm_root; | |
769 | PL_xpvbm_root = (XPVBM*)xpvbm->xpv_pv; | |
770 | UNLOCK_SV_MUTEX; | |
771 | return xpvbm; | |
772 | } | |
773 | ||
774 | STATIC void | |
775 | S_del_xpvbm(pTHX_ XPVBM *p) | |
776 | { | |
777 | LOCK_SV_MUTEX; | |
778 | p->xpv_pv = (char*)PL_xpvbm_root; | |
779 | PL_xpvbm_root = p; | |
780 | UNLOCK_SV_MUTEX; | |
781 | } | |
782 | ||
932e9ff9 VB |
783 | STATIC void |
784 | S_more_xpvbm(pTHX) | |
785 | { | |
786 | register XPVBM* xpvbm; | |
787 | register XPVBM* xpvbmend; | |
612f20c3 GS |
788 | New(721, xpvbm, 1008/sizeof(XPVBM), XPVBM); |
789 | xpvbm->xpv_pv = (char*)PL_xpvbm_arenaroot; | |
790 | PL_xpvbm_arenaroot = xpvbm; | |
791 | ||
932e9ff9 | 792 | xpvbmend = &xpvbm[1008 / sizeof(XPVBM) - 1]; |
612f20c3 | 793 | PL_xpvbm_root = ++xpvbm; |
932e9ff9 VB |
794 | while (xpvbm < xpvbmend) { |
795 | xpvbm->xpv_pv = (char*)(xpvbm + 1); | |
796 | xpvbm++; | |
797 | } | |
798 | xpvbm->xpv_pv = 0; | |
799 | } | |
800 | ||
d33b2eba GS |
801 | #ifdef LEAKTEST |
802 | # define my_safemalloc(s) (void*)safexmalloc(717,s) | |
803 | # define my_safefree(p) safexfree((char*)p) | |
804 | #else | |
805 | # define my_safemalloc(s) (void*)safemalloc(s) | |
806 | # define my_safefree(p) safefree((char*)p) | |
807 | #endif | |
463ee0b2 | 808 | |
d33b2eba | 809 | #ifdef PURIFY |
463ee0b2 | 810 | |
d33b2eba GS |
811 | #define new_XIV() my_safemalloc(sizeof(XPVIV)) |
812 | #define del_XIV(p) my_safefree(p) | |
ed6116ce | 813 | |
d33b2eba GS |
814 | #define new_XNV() my_safemalloc(sizeof(XPVNV)) |
815 | #define del_XNV(p) my_safefree(p) | |
463ee0b2 | 816 | |
d33b2eba GS |
817 | #define new_XRV() my_safemalloc(sizeof(XRV)) |
818 | #define del_XRV(p) my_safefree(p) | |
8c52afec | 819 | |
d33b2eba GS |
820 | #define new_XPV() my_safemalloc(sizeof(XPV)) |
821 | #define del_XPV(p) my_safefree(p) | |
9b94d1dd | 822 | |
d33b2eba GS |
823 | #define new_XPVIV() my_safemalloc(sizeof(XPVIV)) |
824 | #define del_XPVIV(p) my_safefree(p) | |
932e9ff9 | 825 | |
d33b2eba GS |
826 | #define new_XPVNV() my_safemalloc(sizeof(XPVNV)) |
827 | #define del_XPVNV(p) my_safefree(p) | |
932e9ff9 | 828 | |
d33b2eba GS |
829 | #define new_XPVCV() my_safemalloc(sizeof(XPVCV)) |
830 | #define del_XPVCV(p) my_safefree(p) | |
932e9ff9 | 831 | |
d33b2eba GS |
832 | #define new_XPVAV() my_safemalloc(sizeof(XPVAV)) |
833 | #define del_XPVAV(p) my_safefree(p) | |
834 | ||
835 | #define new_XPVHV() my_safemalloc(sizeof(XPVHV)) | |
836 | #define del_XPVHV(p) my_safefree(p) | |
1c846c1f | 837 | |
d33b2eba GS |
838 | #define new_XPVMG() my_safemalloc(sizeof(XPVMG)) |
839 | #define del_XPVMG(p) my_safefree(p) | |
840 | ||
841 | #define new_XPVLV() my_safemalloc(sizeof(XPVLV)) | |
842 | #define del_XPVLV(p) my_safefree(p) | |
843 | ||
844 | #define new_XPVBM() my_safemalloc(sizeof(XPVBM)) | |
845 | #define del_XPVBM(p) my_safefree(p) | |
846 | ||
847 | #else /* !PURIFY */ | |
848 | ||
849 | #define new_XIV() (void*)new_xiv() | |
850 | #define del_XIV(p) del_xiv((XPVIV*) p) | |
851 | ||
852 | #define new_XNV() (void*)new_xnv() | |
853 | #define del_XNV(p) del_xnv((XPVNV*) p) | |
9b94d1dd | 854 | |
d33b2eba GS |
855 | #define new_XRV() (void*)new_xrv() |
856 | #define del_XRV(p) del_xrv((XRV*) p) | |
9b94d1dd | 857 | |
d33b2eba GS |
858 | #define new_XPV() (void*)new_xpv() |
859 | #define del_XPV(p) del_xpv((XPV *)p) | |
860 | ||
861 | #define new_XPVIV() (void*)new_xpviv() | |
862 | #define del_XPVIV(p) del_xpviv((XPVIV *)p) | |
863 | ||
864 | #define new_XPVNV() (void*)new_xpvnv() | |
865 | #define del_XPVNV(p) del_xpvnv((XPVNV *)p) | |
866 | ||
867 | #define new_XPVCV() (void*)new_xpvcv() | |
868 | #define del_XPVCV(p) del_xpvcv((XPVCV *)p) | |
869 | ||
870 | #define new_XPVAV() (void*)new_xpvav() | |
871 | #define del_XPVAV(p) del_xpvav((XPVAV *)p) | |
872 | ||
873 | #define new_XPVHV() (void*)new_xpvhv() | |
874 | #define del_XPVHV(p) del_xpvhv((XPVHV *)p) | |
1c846c1f | 875 | |
d33b2eba GS |
876 | #define new_XPVMG() (void*)new_xpvmg() |
877 | #define del_XPVMG(p) del_xpvmg((XPVMG *)p) | |
878 | ||
879 | #define new_XPVLV() (void*)new_xpvlv() | |
880 | #define del_XPVLV(p) del_xpvlv((XPVLV *)p) | |
881 | ||
882 | #define new_XPVBM() (void*)new_xpvbm() | |
883 | #define del_XPVBM(p) del_xpvbm((XPVBM *)p) | |
884 | ||
885 | #endif /* PURIFY */ | |
9b94d1dd | 886 | |
d33b2eba GS |
887 | #define new_XPVGV() my_safemalloc(sizeof(XPVGV)) |
888 | #define del_XPVGV(p) my_safefree(p) | |
1c846c1f | 889 | |
d33b2eba GS |
890 | #define new_XPVFM() my_safemalloc(sizeof(XPVFM)) |
891 | #define del_XPVFM(p) my_safefree(p) | |
1c846c1f | 892 | |
d33b2eba GS |
893 | #define new_XPVIO() my_safemalloc(sizeof(XPVIO)) |
894 | #define del_XPVIO(p) my_safefree(p) | |
8990e307 | 895 | |
954c1994 GS |
896 | /* |
897 | =for apidoc sv_upgrade | |
898 | ||
899 | Upgrade an SV to a more complex form. Use C<SvUPGRADE>. See | |
900 | C<svtype>. | |
901 | ||
902 | =cut | |
903 | */ | |
904 | ||
79072805 | 905 | bool |
864dbfa3 | 906 | Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt) |
79072805 LW |
907 | { |
908 | char* pv; | |
909 | U32 cur; | |
910 | U32 len; | |
a0d0e21e | 911 | IV iv; |
65202027 | 912 | NV nv; |
79072805 LW |
913 | MAGIC* magic; |
914 | HV* stash; | |
915 | ||
f130fd45 NIS |
916 | if (mt != SVt_PV && SvREADONLY(sv) && SvFAKE(sv)) { |
917 | sv_force_normal(sv); | |
918 | } | |
919 | ||
79072805 LW |
920 | if (SvTYPE(sv) == mt) |
921 | return TRUE; | |
922 | ||
a5f75d66 AD |
923 | if (mt < SVt_PVIV) |
924 | (void)SvOOK_off(sv); | |
925 | ||
79072805 LW |
926 | switch (SvTYPE(sv)) { |
927 | case SVt_NULL: | |
928 | pv = 0; | |
929 | cur = 0; | |
930 | len = 0; | |
931 | iv = 0; | |
932 | nv = 0.0; | |
933 | magic = 0; | |
934 | stash = 0; | |
935 | break; | |
79072805 LW |
936 | case SVt_IV: |
937 | pv = 0; | |
938 | cur = 0; | |
939 | len = 0; | |
463ee0b2 | 940 | iv = SvIVX(sv); |
65202027 | 941 | nv = (NV)SvIVX(sv); |
79072805 LW |
942 | del_XIV(SvANY(sv)); |
943 | magic = 0; | |
944 | stash = 0; | |
ed6116ce | 945 | if (mt == SVt_NV) |
463ee0b2 | 946 | mt = SVt_PVNV; |
ed6116ce LW |
947 | else if (mt < SVt_PVIV) |
948 | mt = SVt_PVIV; | |
79072805 LW |
949 | break; |
950 | case SVt_NV: | |
951 | pv = 0; | |
952 | cur = 0; | |
953 | len = 0; | |
463ee0b2 | 954 | nv = SvNVX(sv); |
1bd302c3 | 955 | iv = I_V(nv); |
79072805 LW |
956 | magic = 0; |
957 | stash = 0; | |
958 | del_XNV(SvANY(sv)); | |
959 | SvANY(sv) = 0; | |
ed6116ce | 960 | if (mt < SVt_PVNV) |
79072805 LW |
961 | mt = SVt_PVNV; |
962 | break; | |
ed6116ce LW |
963 | case SVt_RV: |
964 | pv = (char*)SvRV(sv); | |
965 | cur = 0; | |
966 | len = 0; | |
56431972 RB |
967 | iv = PTR2IV(pv); |
968 | nv = PTR2NV(pv); | |
ed6116ce LW |
969 | del_XRV(SvANY(sv)); |
970 | magic = 0; | |
971 | stash = 0; | |
972 | break; | |
79072805 | 973 | case SVt_PV: |
463ee0b2 | 974 | pv = SvPVX(sv); |
79072805 LW |
975 | cur = SvCUR(sv); |
976 | len = SvLEN(sv); | |
977 | iv = 0; | |
978 | nv = 0.0; | |
979 | magic = 0; | |
980 | stash = 0; | |
981 | del_XPV(SvANY(sv)); | |
748a9306 LW |
982 | if (mt <= SVt_IV) |
983 | mt = SVt_PVIV; | |
984 | else if (mt == SVt_NV) | |
985 | mt = SVt_PVNV; | |
79072805 LW |
986 | break; |
987 | case SVt_PVIV: | |
463ee0b2 | 988 | pv = SvPVX(sv); |
79072805 LW |
989 | cur = SvCUR(sv); |
990 | len = SvLEN(sv); | |
463ee0b2 | 991 | iv = SvIVX(sv); |
79072805 LW |
992 | nv = 0.0; |
993 | magic = 0; | |
994 | stash = 0; | |
995 | del_XPVIV(SvANY(sv)); | |
996 | break; | |
997 | case SVt_PVNV: | |
463ee0b2 | 998 | pv = SvPVX(sv); |
79072805 LW |
999 | cur = SvCUR(sv); |
1000 | len = SvLEN(sv); | |
463ee0b2 LW |
1001 | iv = SvIVX(sv); |
1002 | nv = SvNVX(sv); | |
79072805 LW |
1003 | magic = 0; |
1004 | stash = 0; | |
1005 | del_XPVNV(SvANY(sv)); | |
1006 | break; | |
1007 | case SVt_PVMG: | |
463ee0b2 | 1008 | pv = SvPVX(sv); |
79072805 LW |
1009 | cur = SvCUR(sv); |
1010 | len = SvLEN(sv); | |
463ee0b2 LW |
1011 | iv = SvIVX(sv); |
1012 | nv = SvNVX(sv); | |
79072805 LW |
1013 | magic = SvMAGIC(sv); |
1014 | stash = SvSTASH(sv); | |
1015 | del_XPVMG(SvANY(sv)); | |
1016 | break; | |
1017 | default: | |
cea2e8a9 | 1018 | Perl_croak(aTHX_ "Can't upgrade that kind of scalar"); |
79072805 LW |
1019 | } |
1020 | ||
1021 | switch (mt) { | |
1022 | case SVt_NULL: | |
cea2e8a9 | 1023 | Perl_croak(aTHX_ "Can't upgrade to undef"); |
79072805 LW |
1024 | case SVt_IV: |
1025 | SvANY(sv) = new_XIV(); | |
463ee0b2 | 1026 | SvIVX(sv) = iv; |
79072805 LW |
1027 | break; |
1028 | case SVt_NV: | |
1029 | SvANY(sv) = new_XNV(); | |
463ee0b2 | 1030 | SvNVX(sv) = nv; |
79072805 | 1031 | break; |
ed6116ce LW |
1032 | case SVt_RV: |
1033 | SvANY(sv) = new_XRV(); | |
1034 | SvRV(sv) = (SV*)pv; | |
ed6116ce | 1035 | break; |
79072805 LW |
1036 | case SVt_PV: |
1037 | SvANY(sv) = new_XPV(); | |
463ee0b2 | 1038 | SvPVX(sv) = pv; |
79072805 LW |
1039 | SvCUR(sv) = cur; |
1040 | SvLEN(sv) = len; | |
1041 | break; | |
1042 | case SVt_PVIV: | |
1043 | SvANY(sv) = new_XPVIV(); | |
463ee0b2 | 1044 | SvPVX(sv) = pv; |
79072805 LW |
1045 | SvCUR(sv) = cur; |
1046 | SvLEN(sv) = len; | |
463ee0b2 | 1047 | SvIVX(sv) = iv; |
79072805 | 1048 | if (SvNIOK(sv)) |
a0d0e21e | 1049 | (void)SvIOK_on(sv); |
79072805 LW |
1050 | SvNOK_off(sv); |
1051 | break; | |
1052 | case SVt_PVNV: | |
1053 | SvANY(sv) = new_XPVNV(); | |
463ee0b2 | 1054 | SvPVX(sv) = pv; |
79072805 LW |
1055 | SvCUR(sv) = cur; |
1056 | SvLEN(sv) = len; | |
463ee0b2 LW |
1057 | SvIVX(sv) = iv; |
1058 | SvNVX(sv) = nv; | |
79072805 LW |
1059 | break; |
1060 | case SVt_PVMG: | |
1061 | SvANY(sv) = new_XPVMG(); | |
463ee0b2 | 1062 | SvPVX(sv) = pv; |
79072805 LW |
1063 | SvCUR(sv) = cur; |
1064 | SvLEN(sv) = len; | |
463ee0b2 LW |
1065 | SvIVX(sv) = iv; |
1066 | SvNVX(sv) = nv; | |
79072805 LW |
1067 | SvMAGIC(sv) = magic; |
1068 | SvSTASH(sv) = stash; | |
1069 | break; | |
1070 | case SVt_PVLV: | |
1071 | SvANY(sv) = new_XPVLV(); | |
463ee0b2 | 1072 | SvPVX(sv) = pv; |
79072805 LW |
1073 | SvCUR(sv) = cur; |
1074 | SvLEN(sv) = len; | |
463ee0b2 LW |
1075 | SvIVX(sv) = iv; |
1076 | SvNVX(sv) = nv; | |
79072805 LW |
1077 | SvMAGIC(sv) = magic; |
1078 | SvSTASH(sv) = stash; | |
1079 | LvTARGOFF(sv) = 0; | |
1080 | LvTARGLEN(sv) = 0; | |
1081 | LvTARG(sv) = 0; | |
1082 | LvTYPE(sv) = 0; | |
1083 | break; | |
1084 | case SVt_PVAV: | |
1085 | SvANY(sv) = new_XPVAV(); | |
463ee0b2 LW |
1086 | if (pv) |
1087 | Safefree(pv); | |
2304df62 | 1088 | SvPVX(sv) = 0; |
d1bf51dd | 1089 | AvMAX(sv) = -1; |
93965878 | 1090 | AvFILLp(sv) = -1; |
463ee0b2 LW |
1091 | SvIVX(sv) = 0; |
1092 | SvNVX(sv) = 0.0; | |
1093 | SvMAGIC(sv) = magic; | |
1094 | SvSTASH(sv) = stash; | |
1095 | AvALLOC(sv) = 0; | |
79072805 LW |
1096 | AvARYLEN(sv) = 0; |
1097 | AvFLAGS(sv) = 0; | |
1098 | break; | |
1099 | case SVt_PVHV: | |
1100 | SvANY(sv) = new_XPVHV(); | |
463ee0b2 LW |
1101 | if (pv) |
1102 | Safefree(pv); | |
1103 | SvPVX(sv) = 0; | |
1104 | HvFILL(sv) = 0; | |
1105 | HvMAX(sv) = 0; | |
1106 | HvKEYS(sv) = 0; | |
1107 | SvNVX(sv) = 0.0; | |
79072805 LW |
1108 | SvMAGIC(sv) = magic; |
1109 | SvSTASH(sv) = stash; | |
79072805 LW |
1110 | HvRITER(sv) = 0; |
1111 | HvEITER(sv) = 0; | |
1112 | HvPMROOT(sv) = 0; | |
1113 | HvNAME(sv) = 0; | |
79072805 LW |
1114 | break; |
1115 | case SVt_PVCV: | |
1116 | SvANY(sv) = new_XPVCV(); | |
748a9306 | 1117 | Zero(SvANY(sv), 1, XPVCV); |
463ee0b2 | 1118 | SvPVX(sv) = pv; |
79072805 LW |
1119 | SvCUR(sv) = cur; |
1120 | SvLEN(sv) = len; | |
463ee0b2 LW |
1121 | SvIVX(sv) = iv; |
1122 | SvNVX(sv) = nv; | |
79072805 LW |
1123 | SvMAGIC(sv) = magic; |
1124 | SvSTASH(sv) = stash; | |
79072805 LW |
1125 | break; |
1126 | case SVt_PVGV: | |
1127 | SvANY(sv) = new_XPVGV(); | |
463ee0b2 | 1128 | SvPVX(sv) = pv; |
79072805 LW |
1129 | SvCUR(sv) = cur; |
1130 | SvLEN(sv) = len; | |
463ee0b2 LW |
1131 | SvIVX(sv) = iv; |
1132 | SvNVX(sv) = nv; | |
79072805 LW |
1133 | SvMAGIC(sv) = magic; |
1134 | SvSTASH(sv) = stash; | |
93a17b20 | 1135 | GvGP(sv) = 0; |
79072805 LW |
1136 | GvNAME(sv) = 0; |
1137 | GvNAMELEN(sv) = 0; | |
1138 | GvSTASH(sv) = 0; | |
a5f75d66 | 1139 | GvFLAGS(sv) = 0; |
79072805 LW |
1140 | break; |
1141 | case SVt_PVBM: | |
1142 | SvANY(sv) = new_XPVBM(); | |
463ee0b2 | 1143 | SvPVX(sv) = pv; |
79072805 LW |
1144 | SvCUR(sv) = cur; |
1145 | SvLEN(sv) = len; | |
463ee0b2 LW |
1146 | SvIVX(sv) = iv; |
1147 | SvNVX(sv) = nv; | |
79072805 LW |
1148 | SvMAGIC(sv) = magic; |
1149 | SvSTASH(sv) = stash; | |
1150 | BmRARE(sv) = 0; | |
1151 | BmUSEFUL(sv) = 0; | |
1152 | BmPREVIOUS(sv) = 0; | |
1153 | break; | |
1154 | case SVt_PVFM: | |
1155 | SvANY(sv) = new_XPVFM(); | |
748a9306 | 1156 | Zero(SvANY(sv), 1, XPVFM); |
463ee0b2 | 1157 | SvPVX(sv) = pv; |
79072805 LW |
1158 | SvCUR(sv) = cur; |
1159 | SvLEN(sv) = len; | |
463ee0b2 LW |
1160 | SvIVX(sv) = iv; |
1161 | SvNVX(sv) = nv; | |
79072805 LW |
1162 | SvMAGIC(sv) = magic; |
1163 | SvSTASH(sv) = stash; | |
79072805 | 1164 | break; |
8990e307 LW |
1165 | case SVt_PVIO: |
1166 | SvANY(sv) = new_XPVIO(); | |
748a9306 | 1167 | Zero(SvANY(sv), 1, XPVIO); |
8990e307 LW |
1168 | SvPVX(sv) = pv; |
1169 | SvCUR(sv) = cur; | |
1170 | SvLEN(sv) = len; | |
1171 | SvIVX(sv) = iv; | |
1172 | SvNVX(sv) = nv; | |
1173 | SvMAGIC(sv) = magic; | |
1174 | SvSTASH(sv) = stash; | |
85e6fe83 | 1175 | IoPAGE_LEN(sv) = 60; |
8990e307 LW |
1176 | break; |
1177 | } | |
1178 | SvFLAGS(sv) &= ~SVTYPEMASK; | |
1179 | SvFLAGS(sv) |= mt; | |
79072805 LW |
1180 | return TRUE; |
1181 | } | |
1182 | ||
79072805 | 1183 | int |
864dbfa3 | 1184 | Perl_sv_backoff(pTHX_ register SV *sv) |
79072805 LW |
1185 | { |
1186 | assert(SvOOK(sv)); | |
463ee0b2 LW |
1187 | if (SvIVX(sv)) { |
1188 | char *s = SvPVX(sv); | |
1189 | SvLEN(sv) += SvIVX(sv); | |
1190 | SvPVX(sv) -= SvIVX(sv); | |
79072805 | 1191 | SvIV_set(sv, 0); |
463ee0b2 | 1192 | Move(s, SvPVX(sv), SvCUR(sv)+1, char); |
79072805 LW |
1193 | } |
1194 | SvFLAGS(sv) &= ~SVf_OOK; | |
a0d0e21e | 1195 | return 0; |
79072805 LW |
1196 | } |
1197 | ||
954c1994 GS |
1198 | /* |
1199 | =for apidoc sv_grow | |
1200 | ||
1201 | Expands the character buffer in the SV. This will use C<sv_unref> and will | |
1202 | upgrade the SV to C<SVt_PV>. Returns a pointer to the character buffer. | |
1203 | Use C<SvGROW>. | |
1204 | ||
1205 | =cut | |
1206 | */ | |
1207 | ||
79072805 | 1208 | char * |
864dbfa3 | 1209 | Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen) |
79072805 LW |
1210 | { |
1211 | register char *s; | |
1212 | ||
55497cff | 1213 | #ifdef HAS_64K_LIMIT |
79072805 | 1214 | if (newlen >= 0x10000) { |
1d7c1841 GS |
1215 | PerlIO_printf(Perl_debug_log, |
1216 | "Allocation too large: %"UVxf"\n", (UV)newlen); | |
79072805 LW |
1217 | my_exit(1); |
1218 | } | |
55497cff | 1219 | #endif /* HAS_64K_LIMIT */ |
a0d0e21e LW |
1220 | if (SvROK(sv)) |
1221 | sv_unref(sv); | |
79072805 LW |
1222 | if (SvTYPE(sv) < SVt_PV) { |
1223 | sv_upgrade(sv, SVt_PV); | |
463ee0b2 | 1224 | s = SvPVX(sv); |
79072805 LW |
1225 | } |
1226 | else if (SvOOK(sv)) { /* pv is offset? */ | |
1227 | sv_backoff(sv); | |
463ee0b2 | 1228 | s = SvPVX(sv); |
79072805 LW |
1229 | if (newlen > SvLEN(sv)) |
1230 | newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */ | |
c6f8c383 GA |
1231 | #ifdef HAS_64K_LIMIT |
1232 | if (newlen >= 0x10000) | |
1233 | newlen = 0xFFFF; | |
1234 | #endif | |
79072805 LW |
1235 | } |
1236 | else | |
463ee0b2 | 1237 | s = SvPVX(sv); |
79072805 | 1238 | if (newlen > SvLEN(sv)) { /* need more room? */ |
8d6dde3e | 1239 | if (SvLEN(sv) && s) { |
f5a32c7f | 1240 | #if defined(MYMALLOC) && !defined(LEAKTEST) |
8d6dde3e IZ |
1241 | STRLEN l = malloced_size((void*)SvPVX(sv)); |
1242 | if (newlen <= l) { | |
1243 | SvLEN_set(sv, l); | |
1244 | return s; | |
1245 | } else | |
c70c8a0a | 1246 | #endif |
79072805 | 1247 | Renew(s,newlen,char); |
8d6dde3e | 1248 | } |
79072805 LW |
1249 | else |
1250 | New(703,s,newlen,char); | |
1251 | SvPV_set(sv, s); | |
1252 | SvLEN_set(sv, newlen); | |
1253 | } | |
1254 | return s; | |
1255 | } | |
1256 | ||
954c1994 GS |
1257 | /* |
1258 | =for apidoc sv_setiv | |
1259 | ||
1260 | Copies an integer into the given SV. Does not handle 'set' magic. See | |
1261 | C<sv_setiv_mg>. | |
1262 | ||
1263 | =cut | |
1264 | */ | |
1265 | ||
79072805 | 1266 | void |
864dbfa3 | 1267 | Perl_sv_setiv(pTHX_ register SV *sv, IV i) |
79072805 | 1268 | { |
2213622d | 1269 | SV_CHECK_THINKFIRST(sv); |
463ee0b2 LW |
1270 | switch (SvTYPE(sv)) { |
1271 | case SVt_NULL: | |
79072805 | 1272 | sv_upgrade(sv, SVt_IV); |
463ee0b2 LW |
1273 | break; |
1274 | case SVt_NV: | |
1275 | sv_upgrade(sv, SVt_PVNV); | |
1276 | break; | |
ed6116ce | 1277 | case SVt_RV: |
463ee0b2 | 1278 | case SVt_PV: |
79072805 | 1279 | sv_upgrade(sv, SVt_PVIV); |
463ee0b2 | 1280 | break; |
a0d0e21e LW |
1281 | |
1282 | case SVt_PVGV: | |
a0d0e21e LW |
1283 | case SVt_PVAV: |
1284 | case SVt_PVHV: | |
1285 | case SVt_PVCV: | |
1286 | case SVt_PVFM: | |
1287 | case SVt_PVIO: | |
411caa50 JH |
1288 | Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0), |
1289 | PL_op_desc[PL_op->op_type]); | |
463ee0b2 | 1290 | } |
a0d0e21e | 1291 | (void)SvIOK_only(sv); /* validate number */ |
a5f75d66 | 1292 | SvIVX(sv) = i; |
463ee0b2 | 1293 | SvTAINT(sv); |
79072805 LW |
1294 | } |
1295 | ||
954c1994 GS |
1296 | /* |
1297 | =for apidoc sv_setiv_mg | |
1298 | ||
1299 | Like C<sv_setiv>, but also handles 'set' magic. | |
1300 | ||
1301 | =cut | |
1302 | */ | |
1303 | ||
79072805 | 1304 | void |
864dbfa3 | 1305 | Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i) |
ef50df4b GS |
1306 | { |
1307 | sv_setiv(sv,i); | |
1308 | SvSETMAGIC(sv); | |
1309 | } | |
1310 | ||
954c1994 GS |
1311 | /* |
1312 | =for apidoc sv_setuv | |
1313 | ||
1314 | Copies an unsigned integer into the given SV. Does not handle 'set' magic. | |
1315 | See C<sv_setuv_mg>. | |
1316 | ||
1317 | =cut | |
1318 | */ | |
1319 | ||
ef50df4b | 1320 | void |
864dbfa3 | 1321 | Perl_sv_setuv(pTHX_ register SV *sv, UV u) |
55497cff | 1322 | { |
55ada374 NC |
1323 | /* With these two if statements: |
1324 | u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865 | |
d460ef45 | 1325 | |
55ada374 NC |
1326 | without |
1327 | u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865 | |
d460ef45 | 1328 | |
55ada374 NC |
1329 | If you wish to remove them, please benchmark to see what the effect is |
1330 | */ | |
28e5dec8 JH |
1331 | if (u <= (UV)IV_MAX) { |
1332 | sv_setiv(sv, (IV)u); | |
1333 | return; | |
1334 | } | |
25da4f38 IZ |
1335 | sv_setiv(sv, 0); |
1336 | SvIsUV_on(sv); | |
1337 | SvUVX(sv) = u; | |
55497cff | 1338 | } |
1339 | ||
954c1994 GS |
1340 | /* |
1341 | =for apidoc sv_setuv_mg | |
1342 | ||
1343 | Like C<sv_setuv>, but also handles 'set' magic. | |
1344 | ||
1345 | =cut | |
1346 | */ | |
1347 | ||
55497cff | 1348 | void |
864dbfa3 | 1349 | Perl_sv_setuv_mg(pTHX_ register SV *sv, UV u) |
ef50df4b | 1350 | { |
55ada374 NC |
1351 | /* With these two if statements: |
1352 | u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865 | |
d460ef45 | 1353 | |
55ada374 NC |
1354 | without |
1355 | u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865 | |
d460ef45 | 1356 | |
55ada374 NC |
1357 | If you wish to remove them, please benchmark to see what the effect is |
1358 | */ | |
28e5dec8 JH |
1359 | if (u <= (UV)IV_MAX) { |
1360 | sv_setiv(sv, (IV)u); | |
1361 | } else { | |
1362 | sv_setiv(sv, 0); | |
1363 | SvIsUV_on(sv); | |
1364 | sv_setuv(sv,u); | |
1365 | } | |
ef50df4b GS |
1366 | SvSETMAGIC(sv); |
1367 | } | |
1368 | ||
954c1994 GS |
1369 | /* |
1370 | =for apidoc sv_setnv | |
1371 | ||
1372 | Copies a double into the given SV. Does not handle 'set' magic. See | |
1373 | C<sv_setnv_mg>. | |
1374 | ||
1375 | =cut | |
1376 | */ | |
1377 | ||
ef50df4b | 1378 | void |
65202027 | 1379 | Perl_sv_setnv(pTHX_ register SV *sv, NV num) |
79072805 | 1380 | { |
2213622d | 1381 | SV_CHECK_THINKFIRST(sv); |
a0d0e21e LW |
1382 | switch (SvTYPE(sv)) { |
1383 | case SVt_NULL: | |
1384 | case SVt_IV: | |
79072805 | 1385 | sv_upgrade(sv, SVt_NV); |
a0d0e21e | 1386 | break; |
a0d0e21e LW |
1387 | case SVt_RV: |
1388 | case SVt_PV: | |
1389 | case SVt_PVIV: | |
79072805 | 1390 | sv_upgrade(sv, SVt_PVNV); |
a0d0e21e | 1391 | break; |
827b7e14 | 1392 | |
a0d0e21e | 1393 | case SVt_PVGV: |
a0d0e21e LW |
1394 | case SVt_PVAV: |
1395 | case SVt_PVHV: | |
1396 | case SVt_PVCV: | |
1397 | case SVt_PVFM: | |
1398 | case SVt_PVIO: | |
411caa50 JH |
1399 | Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0), |
1400 | PL_op_name[PL_op->op_type]); | |
79072805 | 1401 | } |
463ee0b2 | 1402 | SvNVX(sv) = num; |
a0d0e21e | 1403 | (void)SvNOK_only(sv); /* validate number */ |
463ee0b2 | 1404 | SvTAINT(sv); |
79072805 LW |
1405 | } |
1406 | ||
954c1994 GS |
1407 | /* |
1408 | =for apidoc sv_setnv_mg | |
1409 | ||
1410 | Like C<sv_setnv>, but also handles 'set' magic. | |
1411 | ||
1412 | =cut | |
1413 | */ | |
1414 | ||
ef50df4b | 1415 | void |
65202027 | 1416 | Perl_sv_setnv_mg(pTHX_ register SV *sv, NV num) |
ef50df4b GS |
1417 | { |
1418 | sv_setnv(sv,num); | |
1419 | SvSETMAGIC(sv); | |
1420 | } | |
1421 | ||
76e3520e | 1422 | STATIC void |
cea2e8a9 | 1423 | S_not_a_number(pTHX_ SV *sv) |
a0d0e21e LW |
1424 | { |
1425 | char tmpbuf[64]; | |
1426 | char *d = tmpbuf; | |
1427 | char *s; | |
dc28f22b GA |
1428 | char *limit = tmpbuf + sizeof(tmpbuf) - 8; |
1429 | /* each *s can expand to 4 chars + "...\0", | |
1430 | i.e. need room for 8 chars */ | |
a0d0e21e | 1431 | |
dc28f22b | 1432 | for (s = SvPVX(sv); *s && d < limit; s++) { |
bbce6d69 | 1433 | int ch = *s & 0xFF; |
1434 | if (ch & 128 && !isPRINT_LC(ch)) { | |
a0d0e21e LW |
1435 | *d++ = 'M'; |
1436 | *d++ = '-'; | |
1437 | ch &= 127; | |
1438 | } | |
bbce6d69 | 1439 | if (ch == '\n') { |
1440 | *d++ = '\\'; | |
1441 | *d++ = 'n'; | |
1442 | } | |
1443 | else if (ch == '\r') { | |
1444 | *d++ = '\\'; | |
1445 | *d++ = 'r'; | |
1446 | } | |
1447 | else if (ch == '\f') { | |
1448 | *d++ = '\\'; | |
1449 | *d++ = 'f'; | |
1450 | } | |
1451 | else if (ch == '\\') { | |
1452 | *d++ = '\\'; | |
1453 | *d++ = '\\'; | |
1454 | } | |
1455 | else if (isPRINT_LC(ch)) | |
a0d0e21e LW |
1456 | *d++ = ch; |
1457 | else { | |
1458 | *d++ = '^'; | |
bbce6d69 | 1459 | *d++ = toCTRL(ch); |
a0d0e21e LW |
1460 | } |
1461 | } | |
1462 | if (*s) { | |
1463 | *d++ = '.'; | |
1464 | *d++ = '.'; | |
1465 | *d++ = '.'; | |
1466 | } | |
1467 | *d = '\0'; | |
1468 | ||
533c011a | 1469 | if (PL_op) |
42d38218 MS |
1470 | Perl_warner(aTHX_ WARN_NUMERIC, |
1471 | "Argument \"%s\" isn't numeric in %s", tmpbuf, | |
1472 | PL_op_desc[PL_op->op_type]); | |
a0d0e21e | 1473 | else |
42d38218 MS |
1474 | Perl_warner(aTHX_ WARN_NUMERIC, |
1475 | "Argument \"%s\" isn't numeric", tmpbuf); | |
a0d0e21e LW |
1476 | } |
1477 | ||
28e5dec8 JH |
1478 | /* the number can be converted to integer with atol() or atoll() although */ |
1479 | #define IS_NUMBER_TO_INT_BY_ATOL 0x01 /* integer (may have decimals) */ | |
1480 | #define IS_NUMBER_TO_INT_BY_STRTOL 0x02 /* it may exceed IV_MAX */ | |
1481 | #define IS_NUMBER_TO_INT_BY_ATOF 0x04 /* seen something like 123e4 */ | |
1482 | #define IS_NUMBER_LONGER_THAN_IV_MAX 0x08 /* more digits than IV_MAX */ | |
1483 | #define IS_NUMBER_AS_LONG_AS_IV_MAX 0x10 /* may(be not) larger than IV_MAX */ | |
1484 | #define IS_NUMBER_NOT_INT 0x20 /* seen a decimal point or e */ | |
1485 | #define IS_NUMBER_NEG 0x40 /* seen a leading - */ | |
1486 | #define IS_NUMBER_INFINITY 0x80 /* /^\s*-?Infinity\s*$/i */ | |
25da4f38 IZ |
1487 | |
1488 | /* Actually, ISO C leaves conversion of UV to IV undefined, but | |
1489 | until proven guilty, assume that things are not that bad... */ | |
1490 | ||
28e5dec8 JH |
1491 | /* As 64 bit platforms often have an NV that doesn't preserve all bits of |
1492 | an IV (an assumption perl has been based on to date) it becomes necessary | |
1493 | to remove the assumption that the NV always carries enough precision to | |
1494 | recreate the IV whenever needed, and that the NV is the canonical form. | |
1495 | Instead, IV/UV and NV need to be given equal rights. So as to not lose | |
1496 | precision as an side effect of conversion (which would lead to insanity | |
1497 | and the dragon(s) in t/op/numconvert.t getting very angry) the intent is | |
1498 | 1) to distinguish between IV/UV/NV slots that have cached a valid | |
1499 | conversion where precision was lost and IV/UV/NV slots that have a | |
1500 | valid conversion which has lost no precision | |
1501 | 2) to ensure that if a numeric conversion to one form is request that | |
1502 | would lose precision, the precise conversion (or differently | |
1503 | imprecise conversion) is also performed and cached, to prevent | |
1504 | requests for different numeric formats on the same SV causing | |
1505 | lossy conversion chains. (lossless conversion chains are perfectly | |
1506 | acceptable (still)) | |
1507 | ||
1508 | ||
1509 | flags are used: | |
1510 | SvIOKp is true if the IV slot contains a valid value | |
1511 | SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true) | |
1512 | SvNOKp is true if the NV slot contains a valid value | |
1513 | SvNOK is true only if the NV value is accurate | |
1514 | ||
1515 | so | |
1516 | while converting from PV to NV check to see if converting that NV to an | |
1517 | IV(or UV) would lose accuracy over a direct conversion from PV to | |
1518 | IV(or UV). If it would, cache both conversions, return NV, but mark | |
1519 | SV as IOK NOKp (ie not NOK). | |
1520 | ||
1521 | while converting from PV to IV check to see if converting that IV to an | |
1522 | NV would lose accuracy over a direct conversion from PV to NV. If it | |
1523 | would, cache both conversions, flag similarly. | |
1524 | ||
1525 | Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite | |
1526 | correctly because if IV & NV were set NV *always* overruled. | |
1527 | Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flags meaning | |
1528 | changes - now IV and NV together means that the two are interchangeable | |
1529 | SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX; | |
d460ef45 | 1530 | |
28e5dec8 JH |
1531 | The benefit of this is operations such as pp_add know that if SvIOK is |
1532 | true for both left and right operands, then integer addition can be | |
1533 | used instead of floating point. (for cases where the result won't | |
1534 | overflow) Before, floating point was always used, which could lead to | |
1535 | loss of precision compared with integer addition. | |
1536 | ||
1537 | * making IV and NV equal status should make maths accurate on 64 bit | |
1538 | platforms | |
1539 | * may speed up maths somewhat if pp_add and friends start to use | |
1540 | integers when possible instead of fp. (hopefully the overhead in | |
1541 | looking for SvIOK and checking for overflow will not outweigh the | |
1542 | fp to integer speedup) | |
1543 | * will slow down integer operations (callers of SvIV) on "inaccurate" | |
1544 | values, as the change from SvIOK to SvIOKp will cause a call into | |
1545 | sv_2iv each time rather than a macro access direct to the IV slot | |
1546 | * should speed up number->string conversion on integers as IV is | |
1547 | favoured when IV and NV equally accurate | |
1548 | ||
1549 | #################################################################### | |
1550 | You had better be using SvIOK_notUV if you want an IV for arithmetic | |
1551 | SvIOK is true if (IV or UV), so you might be getting (IV)SvUV | |
1552 | SvUOK is true iff UV. | |
1553 | #################################################################### | |
1554 | ||
1555 | Your mileage will vary depending your CPUs relative fp to integer | |
1556 | performance ratio. | |
1557 | */ | |
1558 | ||
1559 | #ifndef NV_PRESERVES_UV | |
1560 | #define IS_NUMBER_UNDERFLOW_IV 1 | |
1561 | #define IS_NUMBER_UNDERFLOW_UV 2 | |
1562 | #define IS_NUMBER_IV_AND_UV 2 | |
1563 | #define IS_NUMBER_OVERFLOW_IV 4 | |
1564 | #define IS_NUMBER_OVERFLOW_UV 5 | |
1565 | /* Hopefully your optimiser will consider inlining these two functions. */ | |
1566 | STATIC int | |
1567 | S_sv_2inuv_non_preserve (pTHX_ register SV *sv, I32 numtype) { | |
1568 | NV nv = SvNVX(sv); /* Code simpler and had compiler problems if */ | |
1569 | UV nv_as_uv = U_V(nv); /* these are not in simple variables. */ | |
159fae86 | 1570 | 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 |
1571 | if (nv_as_uv <= (UV)IV_MAX) { |
1572 | (void)SvIOKp_on(sv); | |
1573 | (void)SvNOKp_on(sv); | |
1574 | /* Within suitable range to fit in an IV, atol won't overflow */ | |
1575 | /* XXX quite sure? Is that your final answer? not really, I'm | |
1576 | trusting that nv_as_uv to round down if NV is (IV_MAX + 1) */ | |
1577 | SvIVX(sv) = (IV)Atol(SvPVX(sv)); | |
1578 | if (numtype & IS_NUMBER_NOT_INT) { | |
1579 | /* I believe that even if the original PV had decimals, they | |
1580 | are lost beyond the limit of the FP precision. | |
1581 | However, neither is canonical, so both only get p flags. | |
1582 | NWC, 2000/11/25 */ | |
1583 | /* Both already have p flags, so do nothing */ | |
1584 | } else if (SvIVX(sv) == I_V(nv)) { | |
1585 | SvNOK_on(sv); | |
1586 | SvIOK_on(sv); | |
1587 | } else { | |
1588 | SvIOK_on(sv); | |
1589 | /* It had no "." so it must be integer. assert (get in here from | |
1590 | sv_2iv and sv_2uv only for ndef HAS_STRTOL and | |
1591 | IS_NUMBER_AS_LONG_AS_IV_MAX) or my logic is faulty and all | |
1592 | conversion routines need audit. */ | |
1593 | } | |
1594 | return nv < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV; | |
1595 | } | |
1596 | /* between IV_MAX and NV(UV_MAX). Could be slightly> UV_MAX */ | |
1597 | (void)SvIOKp_on(sv); | |
1598 | (void)SvNOKp_on(sv); | |
1599 | #ifdef HAS_STRTOUL | |
1600 | { | |
1601 | int save_errno = errno; | |
1602 | errno = 0; | |
1603 | SvUVX(sv) = Strtoul(SvPVX(sv), Null(char**), 10); | |
1604 | if (errno == 0) { | |
1605 | if (numtype & IS_NUMBER_NOT_INT) { | |
1606 | /* UV and NV both imprecise. */ | |
1607 | SvIsUV_on(sv); | |
1608 | } else if (SvUVX(sv) == nv_as_uv && SvUVX(sv) != UV_MAX) { | |
1609 | SvNOK_on(sv); | |
1610 | SvIOK_on(sv); | |
1611 | SvIsUV_on(sv); | |
1612 | } else { | |
1613 | SvIOK_on(sv); | |
1614 | SvIsUV_on(sv); | |
1615 | } | |
1616 | errno = save_errno; | |
1617 | return IS_NUMBER_OVERFLOW_IV; | |
1618 | } | |
1619 | errno = save_errno; | |
1620 | SvNOK_on(sv); | |
1621 | /* Must have just overflowed UV, but not enough that an NV could spot | |
1622 | this.. */ | |
1623 | return IS_NUMBER_OVERFLOW_UV; | |
1624 | } | |
1625 | #else | |
1626 | /* We've just lost integer precision, nothing we could do. */ | |
1627 | SvUVX(sv) = nv_as_uv; | |
159fae86 | 1628 | 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 |
1629 | /* UV and NV slots equally valid only if we have casting symmetry. */ |
1630 | if (numtype & IS_NUMBER_NOT_INT) { | |
1631 | SvIsUV_on(sv); | |
1632 | } else if (SvUVX(sv) == nv_as_uv && SvUVX(sv) != UV_MAX) { | |
1633 | /* UV_MAX can cast up to NV (UV_MAX+1), that NV casts down to UV_MAX | |
1634 | UV_MAX ought to be 0xFF...FFF which won't preserve (We only | |
1635 | get to this point if NVs don't preserve UVs) */ | |
1636 | SvNOK_on(sv); | |
1637 | SvIOK_on(sv); | |
1638 | SvIsUV_on(sv); | |
1639 | } else { | |
1640 | /* As above, I believe UV at least as good as NV */ | |
1641 | SvIsUV_on(sv); | |
1642 | } | |
1643 | #endif /* HAS_STRTOUL */ | |
1644 | return IS_NUMBER_OVERFLOW_IV; | |
1645 | } | |
1646 | ||
1647 | /* For sv_2nv these three cases are "SvNOK and don't bother casting" */ | |
1648 | STATIC int | |
1649 | S_sv_2iuv_non_preserve (pTHX_ register SV *sv, I32 numtype) | |
1650 | { | |
159fae86 | 1651 | 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 |
1652 | if (SvNVX(sv) < (NV)IV_MIN) { |
1653 | (void)SvIOKp_on(sv); | |
1654 | (void)SvNOK_on(sv); | |
1655 | SvIVX(sv) = IV_MIN; | |
1656 | return IS_NUMBER_UNDERFLOW_IV; | |
1657 | } | |
1658 | if (SvNVX(sv) > (NV)UV_MAX) { | |
1659 | (void)SvIOKp_on(sv); | |
1660 | (void)SvNOK_on(sv); | |
1661 | SvIsUV_on(sv); | |
1662 | SvUVX(sv) = UV_MAX; | |
1663 | return IS_NUMBER_OVERFLOW_UV; | |
1664 | } | |
1665 | if (!(numtype & (IS_NUMBER_TO_INT_BY_ATOL | IS_NUMBER_TO_INT_BY_STRTOL))) { | |
1666 | (void)SvIOKp_on(sv); | |
1667 | (void)SvNOK_on(sv); | |
1668 | /* Can't use strtol etc to convert this string */ | |
1669 | if (SvNVX(sv) <= (UV)IV_MAX) { | |
1670 | SvIVX(sv) = I_V(SvNVX(sv)); | |
1671 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) { | |
1672 | SvIOK_on(sv); /* Integer is precise. NOK, IOK */ | |
1673 | } else { | |
1674 | /* Integer is imprecise. NOK, IOKp */ | |
1675 | } | |
1676 | return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV; | |
1677 | } | |
1678 | SvIsUV_on(sv); | |
1679 | SvUVX(sv) = U_V(SvNVX(sv)); | |
1680 | if ((NV)(SvUVX(sv)) == SvNVX(sv)) { | |
09bb3e27 NC |
1681 | if (SvUVX(sv) == UV_MAX) { |
1682 | /* As we know that NVs don't preserve UVs, UV_MAX cannot | |
1683 | possibly be preserved by NV. Hence, it must be overflow. | |
1684 | NOK, IOKp */ | |
1685 | return IS_NUMBER_OVERFLOW_UV; | |
1686 | } | |
28e5dec8 JH |
1687 | SvIOK_on(sv); /* Integer is precise. NOK, UOK */ |
1688 | } else { | |
1689 | /* Integer is imprecise. NOK, IOKp */ | |
1690 | } | |
1691 | return IS_NUMBER_OVERFLOW_IV; | |
1692 | } | |
e57fe1aa | 1693 | return S_sv_2inuv_non_preserve(aTHX_ sv, numtype); |
28e5dec8 JH |
1694 | } |
1695 | #endif /* NV_PRESERVES_UV*/ | |
1696 | ||
a0d0e21e | 1697 | IV |
864dbfa3 | 1698 | Perl_sv_2iv(pTHX_ register SV *sv) |
79072805 LW |
1699 | { |
1700 | if (!sv) | |
1701 | return 0; | |
8990e307 | 1702 | if (SvGMAGICAL(sv)) { |
463ee0b2 LW |
1703 | mg_get(sv); |
1704 | if (SvIOKp(sv)) | |
1705 | return SvIVX(sv); | |
748a9306 | 1706 | if (SvNOKp(sv)) { |
25da4f38 | 1707 | return I_V(SvNVX(sv)); |
748a9306 | 1708 | } |
36477c24 | 1709 | if (SvPOKp(sv) && SvLEN(sv)) |
1710 | return asIV(sv); | |
3fe9a6f1 | 1711 | if (!SvROK(sv)) { |
d008e5eb | 1712 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 1713 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
1d7c1841 | 1714 | report_uninit(); |
c6ee37c5 | 1715 | } |
36477c24 | 1716 | return 0; |
3fe9a6f1 | 1717 | } |
463ee0b2 | 1718 | } |
ed6116ce | 1719 | if (SvTHINKFIRST(sv)) { |
a0d0e21e | 1720 | if (SvROK(sv)) { |
a0d0e21e | 1721 | SV* tmpstr; |
1554e226 DC |
1722 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) && |
1723 | (SvRV(tmpstr) != SvRV(sv))) | |
9e7bc3e8 | 1724 | return SvIV(tmpstr); |
56431972 | 1725 | return PTR2IV(SvRV(sv)); |
a0d0e21e | 1726 | } |
47deb5e7 NIS |
1727 | if (SvREADONLY(sv) && SvFAKE(sv)) { |
1728 | sv_force_normal(sv); | |
1729 | } | |
0336b60e | 1730 | if (SvREADONLY(sv) && !SvOK(sv)) { |
0336b60e | 1731 | if (ckWARN(WARN_UNINITIALIZED)) |
1d7c1841 | 1732 | report_uninit(); |
ed6116ce LW |
1733 | return 0; |
1734 | } | |
79072805 | 1735 | } |
25da4f38 IZ |
1736 | if (SvIOKp(sv)) { |
1737 | if (SvIsUV(sv)) { | |
1738 | return (IV)(SvUVX(sv)); | |
1739 | } | |
1740 | else { | |
1741 | return SvIVX(sv); | |
1742 | } | |
463ee0b2 | 1743 | } |
748a9306 | 1744 | if (SvNOKp(sv)) { |
28e5dec8 JH |
1745 | /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv |
1746 | * without also getting a cached IV/UV from it at the same time | |
1747 | * (ie PV->NV conversion should detect loss of accuracy and cache | |
1748 | * IV or UV at same time to avoid this. NWC */ | |
25da4f38 IZ |
1749 | |
1750 | if (SvTYPE(sv) == SVt_NV) | |
1751 | sv_upgrade(sv, SVt_PVNV); | |
1752 | ||
28e5dec8 JH |
1753 | (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */ |
1754 | /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost | |
1755 | certainly cast into the IV range at IV_MAX, whereas the correct | |
1756 | answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary | |
1757 | cases go to UV */ | |
1758 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
748a9306 | 1759 | SvIVX(sv) = I_V(SvNVX(sv)); |
28e5dec8 JH |
1760 | if (SvNVX(sv) == (NV) SvIVX(sv) |
1761 | #ifndef NV_PRESERVES_UV | |
1762 | && (((UV)1 << NV_PRESERVES_UV_BITS) > | |
1763 | (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv))) | |
1764 | /* Don't flag it as "accurately an integer" if the number | |
1765 | came from a (by definition imprecise) NV operation, and | |
1766 | we're outside the range of NV integer precision */ | |
1767 | #endif | |
1768 | ) { | |
1769 | SvIOK_on(sv); /* Can this go wrong with rounding? NWC */ | |
1770 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
1771 | "0x%"UVxf" iv(%g => %"IVdf") (precise)\n", | |
1772 | PTR2UV(sv), | |
1773 | SvNVX(sv), | |
1774 | SvIVX(sv))); | |
1775 | ||
1776 | } else { | |
1777 | /* IV not precise. No need to convert from PV, as NV | |
1778 | conversion would already have cached IV if it detected | |
1779 | that PV->IV would be better than PV->NV->IV | |
1780 | flags already correct - don't set public IOK. */ | |
1781 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
1782 | "0x%"UVxf" iv(%g => %"IVdf") (imprecise)\n", | |
1783 | PTR2UV(sv), | |
1784 | SvNVX(sv), | |
1785 | SvIVX(sv))); | |
1786 | } | |
1787 | /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN, | |
1788 | but the cast (NV)IV_MIN rounds to a the value less (more | |
1789 | negative) than IV_MIN which happens to be equal to SvNVX ?? | |
1790 | Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and | |
1791 | NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and | |
1792 | (NV)UVX == NVX are both true, but the values differ. :-( | |
1793 | Hopefully for 2s complement IV_MIN is something like | |
1794 | 0x8000000000000000 which will be exact. NWC */ | |
d460ef45 | 1795 | } |
25da4f38 | 1796 | else { |
ff68c719 | 1797 | SvUVX(sv) = U_V(SvNVX(sv)); |
28e5dec8 JH |
1798 | if ( |
1799 | (SvNVX(sv) == (NV) SvUVX(sv)) | |
1800 | #ifndef NV_PRESERVES_UV | |
1801 | /* Make sure it's not 0xFFFFFFFFFFFFFFFF */ | |
1802 | /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */ | |
1803 | && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv)) | |
1804 | /* Don't flag it as "accurately an integer" if the number | |
1805 | came from a (by definition imprecise) NV operation, and | |
1806 | we're outside the range of NV integer precision */ | |
1807 | #endif | |
1808 | ) | |
1809 | SvIOK_on(sv); | |
25da4f38 IZ |
1810 | SvIsUV_on(sv); |
1811 | ret_iv_max: | |
1c846c1f | 1812 | DEBUG_c(PerlIO_printf(Perl_debug_log, |
57def98f | 1813 | "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n", |
56431972 | 1814 | PTR2UV(sv), |
57def98f JH |
1815 | SvUVX(sv), |
1816 | SvUVX(sv))); | |
25da4f38 IZ |
1817 | return (IV)SvUVX(sv); |
1818 | } | |
748a9306 LW |
1819 | } |
1820 | else if (SvPOKp(sv) && SvLEN(sv)) { | |
25da4f38 IZ |
1821 | I32 numtype = looks_like_number(sv); |
1822 | ||
1823 | /* We want to avoid a possible problem when we cache an IV which | |
1824 | may be later translated to an NV, and the resulting NV is not | |
1825 | the translation of the initial data. | |
1c846c1f | 1826 | |
25da4f38 IZ |
1827 | This means that if we cache such an IV, we need to cache the |
1828 | NV as well. Moreover, we trade speed for space, and do not | |
28e5dec8 | 1829 | cache the NV if we are sure it's not needed. |
25da4f38 | 1830 | */ |
16b7a9a4 | 1831 | |
28e5dec8 JH |
1832 | if ((numtype & ~IS_NUMBER_NEG) == IS_NUMBER_TO_INT_BY_ATOL) { |
1833 | /* The NV may be reconstructed from IV - safe to cache IV, | |
1834 | which may be calculated by atol(). */ | |
1835 | if (SvTYPE(sv) < SVt_PVIV) | |
1836 | sv_upgrade(sv, SVt_PVIV); | |
f7bbb42a | 1837 | (void)SvIOK_on(sv); |
28e5dec8 JH |
1838 | SvIVX(sv) = Atol(SvPVX(sv)); |
1839 | } else { | |
1840 | #ifdef HAS_STRTOL | |
1841 | IV i; | |
1842 | int save_errno = errno; | |
1843 | /* Is it an integer that we could convert with strtol? | |
1844 | So try it, and if it doesn't set errno then it's pukka. | |
1845 | This should be faster than going atof and then thinking. */ | |
1846 | if (((numtype & (IS_NUMBER_TO_INT_BY_STRTOL | IS_NUMBER_NOT_INT)) | |
1847 | == IS_NUMBER_TO_INT_BY_STRTOL) | |
1848 | /* && is a sequence point. Without it not sure if I'm trying | |
1849 | to do too much between sequence points and hence going | |
1850 | undefined */ | |
1851 | && ((errno = 0), 1) /* , 1 so always true */ | |
1852 | && ((i = Strtol(SvPVX(sv), Null(char**), 10)), 1) | |
1853 | && (errno == 0)) { | |
1854 | if (SvTYPE(sv) < SVt_PVIV) | |
1855 | sv_upgrade(sv, SVt_PVIV); | |
1856 | (void)SvIOK_on(sv); | |
1857 | SvIVX(sv) = i; | |
1858 | errno = save_errno; | |
1859 | } else | |
1860 | #endif | |
1861 | { | |
1862 | NV d; | |
1863 | #ifdef HAS_STRTOL | |
1864 | /* Hopefully trace flow will optimise this away where possible | |
1865 | */ | |
1866 | errno = save_errno; | |
1867 | #endif | |
1868 | /* It wasn't an integer, or it overflowed, or we don't have | |
1869 | strtol. Do things the slow way - check if it's a UV etc. */ | |
1870 | d = Atof(SvPVX(sv)); | |
1871 | ||
1872 | if (SvTYPE(sv) < SVt_PVNV) | |
1873 | sv_upgrade(sv, SVt_PVNV); | |
1874 | SvNVX(sv) = d; | |
1875 | ||
1876 | if (! numtype && ckWARN(WARN_NUMERIC)) | |
1877 | not_a_number(sv); | |
1878 | ||
65202027 | 1879 | #if defined(USE_LONG_DOUBLE) |
28e5dec8 JH |
1880 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n", |
1881 | PTR2UV(sv), SvNVX(sv))); | |
65202027 | 1882 | #else |
28e5dec8 JH |
1883 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%g)\n", |
1884 | PTR2UV(sv), SvNVX(sv))); | |
65202027 | 1885 | #endif |
28e5dec8 JH |
1886 | |
1887 | ||
1888 | #ifdef NV_PRESERVES_UV | |
1889 | (void)SvIOKp_on(sv); | |
1890 | (void)SvNOK_on(sv); | |
1891 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
1892 | SvIVX(sv) = I_V(SvNVX(sv)); | |
1893 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) { | |
1894 | SvIOK_on(sv); | |
1895 | } else { | |
1896 | /* Integer is imprecise. NOK, IOKp */ | |
1897 | } | |
1898 | /* UV will not work better than IV */ | |
1899 | } else { | |
1900 | if (SvNVX(sv) > (NV)UV_MAX) { | |
1901 | SvIsUV_on(sv); | |
1902 | /* Integer is inaccurate. NOK, IOKp, is UV */ | |
1903 | SvUVX(sv) = UV_MAX; | |
1904 | SvIsUV_on(sv); | |
1905 | } else { | |
1906 | SvUVX(sv) = U_V(SvNVX(sv)); | |
1907 | /* 0xFFFFFFFFFFFFFFFF not an issue in here */ | |
1908 | if ((NV)(SvUVX(sv)) == SvNVX(sv)) { | |
1909 | SvIOK_on(sv); | |
1910 | SvIsUV_on(sv); | |
1911 | } else { | |
1912 | /* Integer is imprecise. NOK, IOKp, is UV */ | |
1913 | SvIsUV_on(sv); | |
1914 | } | |
1915 | } | |
1916 | goto ret_iv_max; | |
1917 | } | |
1918 | #else /* NV_PRESERVES_UV */ | |
1919 | if (((UV)1 << NV_PRESERVES_UV_BITS) > | |
1920 | U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) { | |
1921 | /* Small enough to preserve all bits. */ | |
1922 | (void)SvIOKp_on(sv); | |
1923 | SvNOK_on(sv); | |
1924 | SvIVX(sv) = I_V(SvNVX(sv)); | |
1925 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) | |
1926 | SvIOK_on(sv); | |
1927 | /* Assumption: first non-preserved integer is < IV_MAX, | |
1928 | this NV is in the preserved range, therefore: */ | |
1929 | if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv)) | |
1930 | < (UV)IV_MAX)) { | |
1931 | 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); | |
1932 | } | |
1933 | } else if (sv_2iuv_non_preserve (sv, numtype) | |
1934 | >= IS_NUMBER_OVERFLOW_IV) | |
1935 | goto ret_iv_max; | |
1936 | #endif /* NV_PRESERVES_UV */ | |
25da4f38 IZ |
1937 | } |
1938 | } | |
28e5dec8 | 1939 | } else { |
599cee73 | 1940 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP)) |
1d7c1841 | 1941 | report_uninit(); |
25da4f38 IZ |
1942 | if (SvTYPE(sv) < SVt_IV) |
1943 | /* Typically the caller expects that sv_any is not NULL now. */ | |
1944 | sv_upgrade(sv, SVt_IV); | |
a0d0e21e | 1945 | return 0; |
79072805 | 1946 | } |
1d7c1841 GS |
1947 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n", |
1948 | PTR2UV(sv),SvIVX(sv))); | |
25da4f38 | 1949 | return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv); |
79072805 LW |
1950 | } |
1951 | ||
ff68c719 | 1952 | UV |
864dbfa3 | 1953 | Perl_sv_2uv(pTHX_ register SV *sv) |
ff68c719 | 1954 | { |
1955 | if (!sv) | |
1956 | return 0; | |
1957 | if (SvGMAGICAL(sv)) { | |
1958 | mg_get(sv); | |
1959 | if (SvIOKp(sv)) | |
1960 | return SvUVX(sv); | |
1961 | if (SvNOKp(sv)) | |
1962 | return U_V(SvNVX(sv)); | |
36477c24 | 1963 | if (SvPOKp(sv) && SvLEN(sv)) |
1964 | return asUV(sv); | |
3fe9a6f1 | 1965 | if (!SvROK(sv)) { |
d008e5eb | 1966 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 1967 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
1d7c1841 | 1968 | report_uninit(); |
c6ee37c5 | 1969 | } |
36477c24 | 1970 | return 0; |
3fe9a6f1 | 1971 | } |
ff68c719 | 1972 | } |
1973 | if (SvTHINKFIRST(sv)) { | |
1974 | if (SvROK(sv)) { | |
ff68c719 | 1975 | SV* tmpstr; |
1554e226 DC |
1976 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) && |
1977 | (SvRV(tmpstr) != SvRV(sv))) | |
9e7bc3e8 | 1978 | return SvUV(tmpstr); |
56431972 | 1979 | return PTR2UV(SvRV(sv)); |
ff68c719 | 1980 | } |
8a818333 NIS |
1981 | if (SvREADONLY(sv) && SvFAKE(sv)) { |
1982 | sv_force_normal(sv); | |
1983 | } | |
0336b60e | 1984 | if (SvREADONLY(sv) && !SvOK(sv)) { |
0336b60e | 1985 | if (ckWARN(WARN_UNINITIALIZED)) |
1d7c1841 | 1986 | report_uninit(); |
ff68c719 | 1987 | return 0; |
1988 | } | |
1989 | } | |
25da4f38 IZ |
1990 | if (SvIOKp(sv)) { |
1991 | if (SvIsUV(sv)) { | |
1992 | return SvUVX(sv); | |
1993 | } | |
1994 | else { | |
1995 | return (UV)SvIVX(sv); | |
1996 | } | |
ff68c719 | 1997 | } |
1998 | if (SvNOKp(sv)) { | |
28e5dec8 JH |
1999 | /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv |
2000 | * without also getting a cached IV/UV from it at the same time | |
2001 | * (ie PV->NV conversion should detect loss of accuracy and cache | |
2002 | * IV or UV at same time to avoid this. */ | |
2003 | /* IV-over-UV optimisation - choose to cache IV if possible */ | |
2004 | ||
25da4f38 IZ |
2005 | if (SvTYPE(sv) == SVt_NV) |
2006 | sv_upgrade(sv, SVt_PVNV); | |
28e5dec8 JH |
2007 | |
2008 | (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */ | |
2009 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
f7bbb42a | 2010 | SvIVX(sv) = I_V(SvNVX(sv)); |
28e5dec8 JH |
2011 | if (SvNVX(sv) == (NV) SvIVX(sv) |
2012 | #ifndef NV_PRESERVES_UV | |
2013 | && (((UV)1 << NV_PRESERVES_UV_BITS) > | |
2014 | (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv))) | |
2015 | /* Don't flag it as "accurately an integer" if the number | |
2016 | came from a (by definition imprecise) NV operation, and | |
2017 | we're outside the range of NV integer precision */ | |
2018 | #endif | |
2019 | ) { | |
2020 | SvIOK_on(sv); /* Can this go wrong with rounding? NWC */ | |
2021 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
2022 | "0x%"UVxf" uv(%g => %"IVdf") (precise)\n", | |
2023 | PTR2UV(sv), | |
2024 | SvNVX(sv), | |
2025 | SvIVX(sv))); | |
2026 | ||
2027 | } else { | |
2028 | /* IV not precise. No need to convert from PV, as NV | |
2029 | conversion would already have cached IV if it detected | |
2030 | that PV->IV would be better than PV->NV->IV | |
2031 | flags already correct - don't set public IOK. */ | |
2032 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
2033 | "0x%"UVxf" uv(%g => %"IVdf") (imprecise)\n", | |
2034 | PTR2UV(sv), | |
2035 | SvNVX(sv), | |
2036 | SvIVX(sv))); | |
2037 | } | |
2038 | /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN, | |
2039 | but the cast (NV)IV_MIN rounds to a the value less (more | |
2040 | negative) than IV_MIN which happens to be equal to SvNVX ?? | |
2041 | Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and | |
2042 | NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and | |
2043 | (NV)UVX == NVX are both true, but the values differ. :-( | |
2044 | Hopefully for 2s complement IV_MIN is something like | |
2045 | 0x8000000000000000 which will be exact. NWC */ | |
d460ef45 | 2046 | } |
28e5dec8 JH |
2047 | else { |
2048 | SvUVX(sv) = U_V(SvNVX(sv)); | |
2049 | if ( | |
2050 | (SvNVX(sv) == (NV) SvUVX(sv)) | |
2051 | #ifndef NV_PRESERVES_UV | |
2052 | /* Make sure it's not 0xFFFFFFFFFFFFFFFF */ | |
2053 | /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */ | |
2054 | && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv)) | |
2055 | /* Don't flag it as "accurately an integer" if the number | |
2056 | came from a (by definition imprecise) NV operation, and | |
2057 | we're outside the range of NV integer precision */ | |
2058 | #endif | |
2059 | ) | |
2060 | SvIOK_on(sv); | |
2061 | SvIsUV_on(sv); | |
1c846c1f | 2062 | DEBUG_c(PerlIO_printf(Perl_debug_log, |
28e5dec8 | 2063 | "0x%"UVxf" 2uv(%"UVuf" => %"IVdf") (as unsigned)\n", |
57def98f | 2064 | PTR2UV(sv), |
28e5dec8 JH |
2065 | SvUVX(sv), |
2066 | SvUVX(sv))); | |
25da4f38 | 2067 | } |
ff68c719 | 2068 | } |
2069 | else if (SvPOKp(sv) && SvLEN(sv)) { | |
25da4f38 IZ |
2070 | I32 numtype = looks_like_number(sv); |
2071 | ||
2072 | /* We want to avoid a possible problem when we cache a UV which | |
2073 | may be later translated to an NV, and the resulting NV is not | |
2074 | the translation of the initial data. | |
1c846c1f | 2075 | |
25da4f38 IZ |
2076 | This means that if we cache such a UV, we need to cache the |
2077 | NV as well. Moreover, we trade speed for space, and do not | |
2078 | cache the NV if not needed. | |
2079 | */ | |
16b7a9a4 | 2080 | |
28e5dec8 | 2081 | if ((numtype & ~IS_NUMBER_NEG) == IS_NUMBER_TO_INT_BY_ATOL) { |
f7bbb42a | 2082 | /* The NV may be reconstructed from IV - safe to cache IV, |
28e5dec8 JH |
2083 | which may be calculated by atol(). */ |
2084 | if (SvTYPE(sv) < SVt_PVIV) | |
f7bbb42a JH |
2085 | sv_upgrade(sv, SVt_PVIV); |
2086 | (void)SvIOK_on(sv); | |
28e5dec8 JH |
2087 | SvIVX(sv) = Atol(SvPVX(sv)); |
2088 | } else { | |
f7bbb42a | 2089 | #ifdef HAS_STRTOUL |
28e5dec8 | 2090 | UV u; |
f9172815 | 2091 | char *num_begin = SvPVX(sv); |
28e5dec8 | 2092 | int save_errno = errno; |
d460ef45 | 2093 | |
f9172815 JH |
2094 | /* seems that strtoul taking numbers that start with - is |
2095 | implementation dependant, and can't be relied upon. */ | |
2096 | if (numtype & IS_NUMBER_NEG) { | |
2097 | /* Not totally defensive. assumine that looks_like_num | |
2098 | didn't lie about a - sign */ | |
2099 | while (isSPACE(*num_begin)) | |
2100 | num_begin++; | |
2101 | if (*num_begin == '-') | |
2102 | num_begin++; | |
2103 | } | |
d460ef45 | 2104 | |
28e5dec8 JH |
2105 | /* Is it an integer that we could convert with strtoul? |
2106 | So try it, and if it doesn't set errno then it's pukka. | |
2107 | This should be faster than going atof and then thinking. */ | |
2108 | if (((numtype & (IS_NUMBER_TO_INT_BY_STRTOL | IS_NUMBER_NOT_INT)) | |
2109 | == IS_NUMBER_TO_INT_BY_STRTOL) | |
2110 | && ((errno = 0), 1) /* always true */ | |
f9172815 | 2111 | && ((u = Strtoul(num_begin, Null(char**), 10)), 1) /* ditto */ |
28e5dec8 | 2112 | && (errno == 0) |
d460ef45 | 2113 | /* If known to be negative, check it didn't undeflow IV |
f9172815 JH |
2114 | XXX possibly we should put more negative values as NVs |
2115 | direct rather than go via atof below */ | |
2116 | && ((numtype & IS_NUMBER_NEG) ? (u <= (UV)IV_MIN) : 1)) { | |
28e5dec8 JH |
2117 | errno = save_errno; |
2118 | ||
2119 | if (SvTYPE(sv) < SVt_PVIV) | |
2120 | sv_upgrade(sv, SVt_PVIV); | |
2121 | (void)SvIOK_on(sv); | |
2122 | ||
2123 | /* If it's negative must use IV. | |
2124 | IV-over-UV optimisation */ | |
f9172815 JH |
2125 | if (numtype & IS_NUMBER_NEG) { |
2126 | SvIVX(sv) = -(IV)u; | |
2127 | } else if (u <= (UV) IV_MAX) { | |
28e5dec8 JH |
2128 | SvIVX(sv) = (IV)u; |
2129 | } else { | |
2130 | /* it didn't overflow, and it was positive. */ | |
2131 | SvUVX(sv) = u; | |
2132 | SvIsUV_on(sv); | |
2133 | } | |
2134 | } else | |
f7bbb42a | 2135 | #endif |
28e5dec8 JH |
2136 | { |
2137 | NV d; | |
2138 | #ifdef HAS_STRTOUL | |
2139 | /* Hopefully trace flow will optimise this away where possible | |
2140 | */ | |
2141 | errno = save_errno; | |
2142 | #endif | |
2143 | /* It wasn't an integer, or it overflowed, or we don't have | |
2144 | strtol. Do things the slow way - check if it's a IV etc. */ | |
2145 | d = Atof(SvPVX(sv)); | |
2146 | ||
2147 | if (SvTYPE(sv) < SVt_PVNV) | |
2148 | sv_upgrade(sv, SVt_PVNV); | |
2149 | SvNVX(sv) = d; | |
2150 | ||
2151 | if (! numtype && ckWARN(WARN_NUMERIC)) | |
2152 | not_a_number(sv); | |
2153 | ||
2154 | #if defined(USE_LONG_DOUBLE) | |
2155 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%" PERL_PRIgldbl ")\n", | |
2156 | PTR2UV(sv), SvNVX(sv))); | |
2157 | #else | |
2158 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%g)\n", | |
2159 | PTR2UV(sv), SvNVX(sv))); | |
2160 | #endif | |
2161 | ||
2162 | #ifdef NV_PRESERVES_UV | |
2163 | (void)SvIOKp_on(sv); | |
2164 | (void)SvNOK_on(sv); | |
2165 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
2166 | SvIVX(sv) = I_V(SvNVX(sv)); | |
2167 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) { | |
2168 | SvIOK_on(sv); | |
2169 | } else { | |
2170 | /* Integer is imprecise. NOK, IOKp */ | |
2171 | } | |
2172 | /* UV will not work better than IV */ | |
2173 | } else { | |
2174 | if (SvNVX(sv) > (NV)UV_MAX) { | |
2175 | SvIsUV_on(sv); | |
2176 | /* Integer is inaccurate. NOK, IOKp, is UV */ | |
2177 | SvUVX(sv) = UV_MAX; | |
2178 | SvIsUV_on(sv); | |
2179 | } else { | |
2180 | SvUVX(sv) = U_V(SvNVX(sv)); | |
2181 | /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs | |
2182 | NV preservse UV so can do correct comparison. */ | |
2183 | if ((NV)(SvUVX(sv)) == SvNVX(sv)) { | |
2184 | SvIOK_on(sv); | |
2185 | SvIsUV_on(sv); | |
2186 | } else { | |
2187 | /* Integer is imprecise. NOK, IOKp, is UV */ | |
2188 | SvIsUV_on(sv); | |
2189 | } | |
2190 | } | |
2191 | } | |
2192 | #else /* NV_PRESERVES_UV */ | |
2193 | if (((UV)1 << NV_PRESERVES_UV_BITS) > | |
2194 | U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) { | |
2195 | /* Small enough to preserve all bits. */ | |
2196 | (void)SvIOKp_on(sv); | |
2197 | SvNOK_on(sv); | |
2198 | SvIVX(sv) = I_V(SvNVX(sv)); | |
2199 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) | |
2200 | SvIOK_on(sv); | |
2201 | /* Assumption: first non-preserved integer is < IV_MAX, | |
2202 | this NV is in the preserved range, therefore: */ | |
2203 | if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv)) | |
2204 | < (UV)IV_MAX)) { | |
2205 | 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); | |
2206 | } | |
2207 | } else | |
2208 | sv_2iuv_non_preserve (sv, numtype); | |
2209 | #endif /* NV_PRESERVES_UV */ | |
2210 | } | |
f7bbb42a | 2211 | } |
ff68c719 | 2212 | } |
2213 | else { | |
d008e5eb | 2214 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 2215 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
1d7c1841 | 2216 | report_uninit(); |
c6ee37c5 | 2217 | } |
25da4f38 IZ |
2218 | if (SvTYPE(sv) < SVt_IV) |
2219 | /* Typically the caller expects that sv_any is not NULL now. */ | |
2220 | sv_upgrade(sv, SVt_IV); | |
ff68c719 | 2221 | return 0; |
2222 | } | |
25da4f38 | 2223 | |
1d7c1841 GS |
2224 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n", |
2225 | PTR2UV(sv),SvUVX(sv))); | |
25da4f38 | 2226 | return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv); |
ff68c719 | 2227 | } |
2228 | ||
65202027 | 2229 | NV |
864dbfa3 | 2230 | Perl_sv_2nv(pTHX_ register SV *sv) |
79072805 LW |
2231 | { |
2232 | if (!sv) | |
2233 | return 0.0; | |
8990e307 | 2234 | if (SvGMAGICAL(sv)) { |
463ee0b2 LW |
2235 | mg_get(sv); |
2236 | if (SvNOKp(sv)) | |
2237 | return SvNVX(sv); | |
a0d0e21e | 2238 | if (SvPOKp(sv) && SvLEN(sv)) { |
599cee73 | 2239 | if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !looks_like_number(sv)) |
a0d0e21e | 2240 | not_a_number(sv); |
097ee67d | 2241 | return Atof(SvPVX(sv)); |
a0d0e21e | 2242 | } |
25da4f38 | 2243 | if (SvIOKp(sv)) { |
1c846c1f | 2244 | if (SvIsUV(sv)) |
65202027 | 2245 | return (NV)SvUVX(sv); |
25da4f38 | 2246 | else |
65202027 | 2247 | return (NV)SvIVX(sv); |
25da4f38 | 2248 | } |
16d20bd9 | 2249 | if (!SvROK(sv)) { |
d008e5eb | 2250 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 2251 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
1d7c1841 | 2252 | report_uninit(); |
c6ee37c5 | 2253 | } |
16d20bd9 AD |
2254 | return 0; |
2255 | } | |
463ee0b2 | 2256 | } |
ed6116ce | 2257 | if (SvTHINKFIRST(sv)) { |
a0d0e21e | 2258 | if (SvROK(sv)) { |
a0d0e21e | 2259 | SV* tmpstr; |
1554e226 DC |
2260 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) && |
2261 | (SvRV(tmpstr) != SvRV(sv))) | |
9e7bc3e8 | 2262 | return SvNV(tmpstr); |
56431972 | 2263 | return PTR2NV(SvRV(sv)); |
a0d0e21e | 2264 | } |
8a818333 NIS |
2265 | if (SvREADONLY(sv) && SvFAKE(sv)) { |
2266 | sv_force_normal(sv); | |
2267 | } | |
0336b60e | 2268 | if (SvREADONLY(sv) && !SvOK(sv)) { |
599cee73 | 2269 | if (ckWARN(WARN_UNINITIALIZED)) |
1d7c1841 | 2270 | report_uninit(); |
ed6116ce LW |
2271 | return 0.0; |
2272 | } | |
79072805 LW |
2273 | } |
2274 | if (SvTYPE(sv) < SVt_NV) { | |
463ee0b2 LW |
2275 | if (SvTYPE(sv) == SVt_IV) |
2276 | sv_upgrade(sv, SVt_PVNV); | |
2277 | else | |
2278 | sv_upgrade(sv, SVt_NV); | |
572bbb43 | 2279 | #if defined(USE_LONG_DOUBLE) |
097ee67d | 2280 | DEBUG_c({ |
f93f4e46 | 2281 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
2282 | PerlIO_printf(Perl_debug_log, |
2283 | "0x%"UVxf" num(%" PERL_PRIgldbl ")\n", | |
2284 | PTR2UV(sv), SvNVX(sv)); | |
572bbb43 GS |
2285 | RESTORE_NUMERIC_LOCAL(); |
2286 | }); | |
65202027 | 2287 | #else |
572bbb43 | 2288 | DEBUG_c({ |
f93f4e46 | 2289 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
2290 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%g)\n", |
2291 | PTR2UV(sv), SvNVX(sv)); | |
097ee67d JH |
2292 | RESTORE_NUMERIC_LOCAL(); |
2293 | }); | |
572bbb43 | 2294 | #endif |
79072805 LW |
2295 | } |
2296 | else if (SvTYPE(sv) < SVt_PVNV) | |
2297 | sv_upgrade(sv, SVt_PVNV); | |
748a9306 LW |
2298 | if (SvIOKp(sv) && |
2299 | (!SvPOKp(sv) || !strchr(SvPVX(sv),'.') || !looks_like_number(sv))) | |
93a17b20 | 2300 | { |
65202027 | 2301 | SvNVX(sv) = SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv); |
28e5dec8 JH |
2302 | #ifdef NV_PRESERVES_UV |
2303 | SvNOK_on(sv); | |
2304 | #else | |
2305 | /* Only set the public NV OK flag if this NV preserves the IV */ | |
2306 | /* Check it's not 0xFFFFFFFFFFFFFFFF */ | |
2307 | if (SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv)))) | |
2308 | : (SvIVX(sv) == I_V(SvNVX(sv)))) | |
2309 | SvNOK_on(sv); | |
2310 | else | |
2311 | SvNOKp_on(sv); | |
2312 | #endif | |
93a17b20 | 2313 | } |
748a9306 | 2314 | else if (SvPOKp(sv) && SvLEN(sv)) { |
599cee73 | 2315 | if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !looks_like_number(sv)) |
a0d0e21e | 2316 | not_a_number(sv); |
097ee67d | 2317 | SvNVX(sv) = Atof(SvPVX(sv)); |
28e5dec8 JH |
2318 | #ifdef NV_PRESERVES_UV |
2319 | SvNOK_on(sv); | |
2320 | #else | |
2321 | /* Only set the public NV OK flag if this NV preserves the value in | |
2322 | the PV at least as well as an IV/UV would. | |
2323 | Not sure how to do this 100% reliably. */ | |
2324 | /* if that shift count is out of range then Configure's test is | |
2325 | wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS == | |
2326 | UV_BITS */ | |
2327 | if (((UV)1 << NV_PRESERVES_UV_BITS) > | |
2328 | U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) | |
2329 | SvNOK_on(sv); /* Definitely small enough to preserve all bits */ | |
2330 | else if (SvNVX(sv) < (NV)IV_MIN || SvNVX(sv) > (NV)UV_MAX) { | |
2331 | /* Definitely too large/small to fit in an integer, so no loss | |
2332 | of precision going to integer in the future via NV */ | |
2333 | SvNOK_on(sv); | |
2334 | } else { | |
2335 | /* Is it something we can run through strtol etc (ie no | |
2336 | trailing exponent part)? */ | |
2337 | int numtype = looks_like_number(sv); | |
2338 | /* XXX probably should cache this if called above */ | |
2339 | ||
2340 | if (!(numtype & | |
2341 | (IS_NUMBER_TO_INT_BY_ATOL | IS_NUMBER_TO_INT_BY_STRTOL))) { | |
2342 | /* Can't use strtol etc to convert this string, so don't try */ | |
2343 | SvNOK_on(sv); | |
2344 | } else | |
2345 | sv_2inuv_non_preserve (sv, numtype); | |
2346 | } | |
2347 | #endif /* NV_PRESERVES_UV */ | |
93a17b20 | 2348 | } |
79072805 | 2349 | else { |
599cee73 | 2350 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP)) |
1d7c1841 | 2351 | report_uninit(); |
25da4f38 IZ |
2352 | if (SvTYPE(sv) < SVt_NV) |
2353 | /* Typically the caller expects that sv_any is not NULL now. */ | |
28e5dec8 JH |
2354 | /* XXX Ilya implies that this is a bug in callers that assume this |
2355 | and ideally should be fixed. */ | |
25da4f38 | 2356 | sv_upgrade(sv, SVt_NV); |
a0d0e21e | 2357 | return 0.0; |
79072805 | 2358 | } |
572bbb43 | 2359 | #if defined(USE_LONG_DOUBLE) |
097ee67d | 2360 | DEBUG_c({ |
f93f4e46 | 2361 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
2362 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n", |
2363 | PTR2UV(sv), SvNVX(sv)); | |
572bbb43 GS |
2364 | RESTORE_NUMERIC_LOCAL(); |
2365 | }); | |
65202027 | 2366 | #else |
572bbb43 | 2367 | DEBUG_c({ |
f93f4e46 | 2368 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
2369 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%g)\n", |
2370 | PTR2UV(sv), SvNVX(sv)); | |
097ee67d JH |
2371 | RESTORE_NUMERIC_LOCAL(); |
2372 | }); | |
572bbb43 | 2373 | #endif |
463ee0b2 | 2374 | return SvNVX(sv); |
79072805 LW |
2375 | } |
2376 | ||
76e3520e | 2377 | STATIC IV |
cea2e8a9 | 2378 | S_asIV(pTHX_ SV *sv) |
36477c24 | 2379 | { |
2380 | I32 numtype = looks_like_number(sv); | |
65202027 | 2381 | NV d; |
36477c24 | 2382 | |
25da4f38 | 2383 | if (numtype & IS_NUMBER_TO_INT_BY_ATOL) |
cf2093f6 | 2384 | return Atol(SvPVX(sv)); |
d008e5eb | 2385 | if (!numtype) { |
d008e5eb GS |
2386 | if (ckWARN(WARN_NUMERIC)) |
2387 | not_a_number(sv); | |
2388 | } | |
097ee67d | 2389 | d = Atof(SvPVX(sv)); |
25da4f38 | 2390 | return I_V(d); |
36477c24 | 2391 | } |
2392 | ||
76e3520e | 2393 | STATIC UV |
cea2e8a9 | 2394 | S_asUV(pTHX_ SV *sv) |
36477c24 | 2395 | { |
2396 | I32 numtype = looks_like_number(sv); | |
2397 | ||
84902520 | 2398 | #ifdef HAS_STRTOUL |
25da4f38 | 2399 | if (numtype & IS_NUMBER_TO_INT_BY_ATOL) |
cf2093f6 | 2400 | return Strtoul(SvPVX(sv), Null(char**), 10); |
84902520 | 2401 | #endif |
d008e5eb | 2402 | if (!numtype) { |
d008e5eb GS |
2403 | if (ckWARN(WARN_NUMERIC)) |
2404 | not_a_number(sv); | |
2405 | } | |
097ee67d | 2406 | return U_V(Atof(SvPVX(sv))); |
36477c24 | 2407 | } |
2408 | ||
25da4f38 IZ |
2409 | /* |
2410 | * Returns a combination of (advisory only - can get false negatives) | |
28e5dec8 JH |
2411 | * IS_NUMBER_TO_INT_BY_ATOL, IS_NUMBER_TO_INT_BY_ATOF |
2412 | * IS_NUMBER_LONGER_THAN_IV_MAX, IS_NUMBER_AS_LONG_AS_IV_MAX | |
2413 | * IS_NUMBER_NOT_INT, IS_NUMBER_NEG, IS_NUMBER_INFINITY | |
25da4f38 IZ |
2414 | * 0 if does not look like number. |
2415 | * | |
28e5dec8 JH |
2416 | * (atol and strtol stop when they hit a decimal point. strtol will return |
2417 | * LONG_MAX and LONG_MIN when given out of range values. ANSI says they should | |
2418 | * do this, and vendors have had 11 years to get it right. | |
2419 | * However, will try to make it still work with only atol | |
d460ef45 | 2420 | * |
28e5dec8 JH |
2421 | * IS_NUMBER_TO_INT_BY_ATOL 123456789 or 123456789.3 definitely < IV_MAX |
2422 | * IS_NUMBER_TO_INT_BY_STRTOL 123456789 or 123456789.3 if digits = IV_MAX | |
2423 | * IS_NUMBER_TO_INT_BY_ATOF 123456789e0 or >> IV_MAX | |
2424 | * IS_NUMBER_LONGER_THAN_IV_MAX lots of digits, don't bother with atol | |
2425 | * IS_NUMBER_AS_LONG_AS_IV_MAX atol might hit LONG_MAX, might not. | |
2426 | * IS_NUMBER_NOT_INT saw "." or "e" | |
2427 | * IS_NUMBER_NEG | |
300aed98 | 2428 | * IS_NUMBER_INFINITY |
25da4f38 IZ |
2429 | */ |
2430 | ||
954c1994 GS |
2431 | /* |
2432 | =for apidoc looks_like_number | |
2433 | ||
2434 | Test if an the content of an SV looks like a number (or is a | |
28e5dec8 JH |
2435 | number). C<Inf> and C<Infinity> are treated as numbers (so will not |
2436 | issue a non-numeric warning), even if your atof() doesn't grok them. | |
954c1994 GS |
2437 | |
2438 | =cut | |
2439 | */ | |
2440 | ||
36477c24 | 2441 | I32 |
864dbfa3 | 2442 | Perl_looks_like_number(pTHX_ SV *sv) |
36477c24 | 2443 | { |
2444 | register char *s; | |
2445 | register char *send; | |
2446 | register char *sbegin; | |
25da4f38 IZ |
2447 | register char *nbegin; |
2448 | I32 numtype = 0; | |
300aed98 | 2449 | I32 sawinf = 0; |
36477c24 | 2450 | STRLEN len; |
9c7192ba | 2451 | #ifdef USE_LOCALE_NUMERIC |
eff180cd | 2452 | bool specialradix = FALSE; |
9c7192ba | 2453 | #endif |
36477c24 | 2454 | |
2455 | if (SvPOK(sv)) { | |
1c846c1f | 2456 | sbegin = SvPVX(sv); |
36477c24 | 2457 | len = SvCUR(sv); |
2458 | } | |
2459 | else if (SvPOKp(sv)) | |
2460 | sbegin = SvPV(sv, len); | |
2461 | else | |
2462 | return 1; | |
2463 | send = sbegin + len; | |
2464 | ||
2465 | s = sbegin; | |
2466 | while (isSPACE(*s)) | |
2467 | s++; | |
25da4f38 IZ |
2468 | if (*s == '-') { |
2469 | s++; | |
2470 | numtype = IS_NUMBER_NEG; | |
2471 | } | |
2472 | else if (*s == '+') | |
36477c24 | 2473 | s++; |
ff0cee69 | 2474 | |
25da4f38 IZ |
2475 | nbegin = s; |
2476 | /* | |
d460ef45 | 2477 | * we return IS_NUMBER_TO_INT_BY_ATOL if the number can converted to |
28e5dec8 JH |
2478 | * integer with atol() without overflow, IS_NUMBER_TO_INT_BY_STRTOL if |
2479 | * possibly slightly larger than max int, IS_NUMBER_TO_INT_BY_ATOF if you | |
2480 | * will need (int)atof(). | |
25da4f38 IZ |
2481 | */ |
2482 | ||
300aed98 | 2483 | /* next must be digit or the radix separator or beginning of infinity */ |
ff0cee69 | 2484 | if (isDIGIT(*s)) { |
2485 | do { | |
2486 | s++; | |
2487 | } while (isDIGIT(*s)); | |
25da4f38 | 2488 | |
28e5dec8 JH |
2489 | /* Aaargh. long long really is irritating. |
2490 | In the gospel according to ANSI 1989, it is an axiom that "long" | |
2491 | is the longest integer type, and that if you don't know how long | |
2492 | something is you can cast it to long, and nothing will be lost | |
2493 | (except possibly speed of execution if long is slower than the | |
2494 | type is was). | |
2495 | Now, one can't be sure if the old rules apply, or long long | |
2496 | (or some other newfangled thing) is actually longer than the | |
2497 | (formerly) longest thing. | |
2498 | */ | |
2499 | /* This lot will work for 64 bit *as long as* either | |
2500 | either long is 64 bit | |
2501 | or we can find both strtol/strtoq and strtoul/strtouq | |
2502 | If not, we really should refuse to let the user use 64 bit IVs | |
2503 | By "64 bit" I really mean IVs that don't get preserved by NVs | |
2504 | It also should work for 128 bit IVs. Can any lend me a machine to | |
2505 | test this? | |
2506 | */ | |
2507 | if (s - nbegin > TYPE_DIGITS(UV)) /* Cannot cache ato[ul]() */ | |
2508 | numtype |= IS_NUMBER_TO_INT_BY_ATOF | IS_NUMBER_LONGER_THAN_IV_MAX; | |
2509 | else if (s - nbegin < BIT_DIGITS(((sizeof (IV)>sizeof (long)) | |
2510 | ? sizeof(long) : sizeof (IV))*8-1)) | |
f7bbb42a | 2511 | numtype |= IS_NUMBER_TO_INT_BY_ATOL; |
28e5dec8 JH |
2512 | else |
2513 | /* Can't be sure either way. (For 64 bit UV, 63 bit IV is 1 decimal | |
2514 | digit less (IV_MAX= 9223372036854775807, | |
2515 | UV_MAX= 18446744073709551615) so be cautious */ | |
2516 | numtype |= IS_NUMBER_TO_INT_BY_STRTOL | IS_NUMBER_AS_LONG_AS_IV_MAX; | |
25da4f38 | 2517 | |
097ee67d | 2518 | if (*s == '.' |
1c846c1f | 2519 | #ifdef USE_LOCALE_NUMERIC |
eff180cd | 2520 | || (specialradix = IS_NUMERIC_RADIX(s)) |
097ee67d JH |
2521 | #endif |
2522 | ) { | |
9c7192ba | 2523 | #ifdef USE_LOCALE_NUMERIC |
eff180cd JH |
2524 | if (specialradix) |
2525 | s += SvCUR(PL_numeric_radix); | |
2526 | else | |
9c7192ba | 2527 | #endif |
eff180cd | 2528 | s++; |
28e5dec8 | 2529 | numtype |= IS_NUMBER_NOT_INT; |
097ee67d | 2530 | while (isDIGIT(*s)) /* optional digits after the radix */ |
ff0cee69 | 2531 | s++; |
2532 | } | |
36477c24 | 2533 | } |
097ee67d | 2534 | else if (*s == '.' |
1c846c1f | 2535 | #ifdef USE_LOCALE_NUMERIC |
eff180cd | 2536 | || (specialradix = IS_NUMERIC_RADIX(s)) |
097ee67d JH |
2537 | #endif |
2538 | ) { | |
9c7192ba | 2539 | #ifdef USE_LOCALE_NUMERIC |
eff180cd JH |
2540 | if (specialradix) |
2541 | s += SvCUR(PL_numeric_radix); | |
2542 | else | |
9c7192ba | 2543 | #endif |
eff180cd | 2544 | s++; |
28e5dec8 | 2545 | numtype |= IS_NUMBER_TO_INT_BY_ATOL | IS_NUMBER_NOT_INT; |
097ee67d | 2546 | /* no digits before the radix means we need digits after it */ |
ff0cee69 | 2547 | if (isDIGIT(*s)) { |
2548 | do { | |
2549 | s++; | |
2550 | } while (isDIGIT(*s)); | |
2551 | } | |
2552 | else | |
2553 | return 0; | |
2554 | } | |
300aed98 JH |
2555 | else if (*s == 'I' || *s == 'i') { |
2556 | s++; if (*s != 'N' && *s != 'n') return 0; | |
2557 | s++; if (*s != 'F' && *s != 'f') return 0; | |
2558 | s++; if (*s == 'I' || *s == 'i') { | |
2559 | s++; if (*s != 'N' && *s != 'n') return 0; | |
2560 | s++; if (*s != 'I' && *s != 'i') return 0; | |
2561 | s++; if (*s != 'T' && *s != 't') return 0; | |
2562 | s++; if (*s != 'Y' && *s != 'y') return 0; | |
99938567 | 2563 | s++; |
300aed98 JH |
2564 | } |
2565 | sawinf = 1; | |
2566 | } | |
ff0cee69 | 2567 | else |
2568 | return 0; | |
2569 | ||
300aed98 | 2570 | if (sawinf) |
28e5dec8 JH |
2571 | numtype = (numtype & IS_NUMBER_NEG) /* Keep track of sign */ |
2572 | | IS_NUMBER_INFINITY | IS_NUMBER_NOT_INT; | |
300aed98 JH |
2573 | else { |
2574 | /* we can have an optional exponent part */ | |
2575 | if (*s == 'e' || *s == 'E') { | |
28e5dec8 JH |
2576 | numtype &= IS_NUMBER_NEG; |
2577 | numtype |= IS_NUMBER_TO_INT_BY_ATOF | IS_NUMBER_NOT_INT; | |
36477c24 | 2578 | s++; |
300aed98 JH |
2579 | if (*s == '+' || *s == '-') |
2580 | s++; | |
2581 | if (isDIGIT(*s)) { | |
2582 | do { | |
2583 | s++; | |
2584 | } while (isDIGIT(*s)); | |
2585 | } | |
2586 | else | |
2587 | return 0; | |
2588 | } | |
36477c24 | 2589 | } |
2590 | while (isSPACE(*s)) | |
2591 | s++; | |
80f3f388 | 2592 | if (s >= send) |
36477c24 | 2593 | return numtype; |
2594 | if (len == 10 && memEQ(sbegin, "0 but true", 10)) | |
25da4f38 | 2595 | return IS_NUMBER_TO_INT_BY_ATOL; |
36477c24 | 2596 | return 0; |
2597 | } | |
2598 | ||
79072805 | 2599 | char * |
864dbfa3 | 2600 | Perl_sv_2pv_nolen(pTHX_ register SV *sv) |
1fa8b10d JD |
2601 | { |
2602 | STRLEN n_a; | |
2603 | return sv_2pv(sv, &n_a); | |
2604 | } | |
2605 | ||
25da4f38 | 2606 | /* We assume that buf is at least TYPE_CHARS(UV) long. */ |
864dbfa3 | 2607 | static char * |
25da4f38 IZ |
2608 | uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob) |
2609 | { | |
25da4f38 IZ |
2610 | char *ptr = buf + TYPE_CHARS(UV); |
2611 | char *ebuf = ptr; | |
2612 | int sign; | |
25da4f38 IZ |
2613 | |
2614 | if (is_uv) | |
2615 | sign = 0; | |
2616 | else if (iv >= 0) { | |
2617 | uv = iv; | |
2618 | sign = 0; | |
2619 | } else { | |
2620 | uv = -iv; | |
2621 | sign = 1; | |
2622 | } | |
2623 | do { | |
2624 | *--ptr = '0' + (uv % 10); | |
2625 | } while (uv /= 10); | |
2626 | if (sign) | |
2627 | *--ptr = '-'; | |
2628 | *peob = ebuf; | |
2629 | return ptr; | |
2630 | } | |
2631 | ||
1fa8b10d | 2632 | char * |
864dbfa3 | 2633 | Perl_sv_2pv(pTHX_ register SV *sv, STRLEN *lp) |
79072805 LW |
2634 | { |
2635 | register char *s; | |
2636 | int olderrno; | |
46fc3d4c | 2637 | SV *tsv; |
25da4f38 IZ |
2638 | char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */ |
2639 | char *tmpbuf = tbuf; | |
79072805 | 2640 | |
463ee0b2 LW |
2641 | if (!sv) { |
2642 | *lp = 0; | |
2643 | return ""; | |
2644 | } | |
8990e307 | 2645 | if (SvGMAGICAL(sv)) { |
463ee0b2 LW |
2646 | mg_get(sv); |
2647 | if (SvPOKp(sv)) { | |
2648 | *lp = SvCUR(sv); | |
2649 | return SvPVX(sv); | |
2650 | } | |
cf2093f6 | 2651 | if (SvIOKp(sv)) { |
1c846c1f | 2652 | if (SvIsUV(sv)) |
57def98f | 2653 | (void)sprintf(tmpbuf,"%"UVuf, (UV)SvUVX(sv)); |
cf2093f6 | 2654 | else |
57def98f | 2655 | (void)sprintf(tmpbuf,"%"IVdf, (IV)SvIVX(sv)); |
46fc3d4c | 2656 | tsv = Nullsv; |
a0d0e21e | 2657 | goto tokensave; |
463ee0b2 LW |
2658 | } |
2659 | if (SvNOKp(sv)) { | |
2d4389e4 | 2660 | Gconvert(SvNVX(sv), NV_DIG, 0, tmpbuf); |
46fc3d4c | 2661 | tsv = Nullsv; |
a0d0e21e | 2662 | goto tokensave; |
463ee0b2 | 2663 | } |
16d20bd9 | 2664 | if (!SvROK(sv)) { |
d008e5eb | 2665 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 2666 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
1d7c1841 | 2667 | report_uninit(); |
c6ee37c5 | 2668 | } |
16d20bd9 AD |
2669 | *lp = 0; |
2670 | return ""; | |
2671 | } | |
463ee0b2 | 2672 | } |
ed6116ce LW |
2673 | if (SvTHINKFIRST(sv)) { |
2674 | if (SvROK(sv)) { | |
a0d0e21e | 2675 | SV* tmpstr; |
1554e226 DC |
2676 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)) && |
2677 | (SvRV(tmpstr) != SvRV(sv))) | |
9e7bc3e8 | 2678 | return SvPV(tmpstr,*lp); |
ed6116ce LW |
2679 | sv = (SV*)SvRV(sv); |
2680 | if (!sv) | |
2681 | s = "NULLREF"; | |
2682 | else { | |
f9277f47 IZ |
2683 | MAGIC *mg; |
2684 | ||
ed6116ce | 2685 | switch (SvTYPE(sv)) { |
f9277f47 IZ |
2686 | case SVt_PVMG: |
2687 | if ( ((SvFLAGS(sv) & | |
1c846c1f | 2688 | (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG)) |
3149a8e4 | 2689 | == (SVs_OBJECT|SVs_RMG)) |
57668c4d | 2690 | && strEQ(s=HvNAME(SvSTASH(sv)), "Regexp") |
f9277f47 | 2691 | && (mg = mg_find(sv, 'r'))) { |
2cd61cdb | 2692 | regexp *re = (regexp *)mg->mg_obj; |
1bd3ad17 | 2693 | |
2cd61cdb | 2694 | if (!mg->mg_ptr) { |
8782bef2 GB |
2695 | char *fptr = "msix"; |
2696 | char reflags[6]; | |
2697 | char ch; | |
2698 | int left = 0; | |
2699 | int right = 4; | |
2700 | U16 reganch = (re->reganch & PMf_COMPILETIME) >> 12; | |
2701 | ||
155aba94 | 2702 | while((ch = *fptr++)) { |
8782bef2 GB |
2703 | if(reganch & 1) { |
2704 | reflags[left++] = ch; | |
2705 | } | |
2706 | else { | |
2707 | reflags[right--] = ch; | |
2708 | } | |
2709 | reganch >>= 1; | |
2710 | } | |
2711 | if(left != 4) { | |
2712 | reflags[left] = '-'; | |
2713 | left = 5; | |
2714 | } | |
2715 | ||
2716 | mg->mg_len = re->prelen + 4 + left; | |
2717 | New(616, mg->mg_ptr, mg->mg_len + 1 + left, char); | |
2718 | Copy("(?", mg->mg_ptr, 2, char); | |
2719 | Copy(reflags, mg->mg_ptr+2, left, char); | |
2720 | Copy(":", mg->mg_ptr+left+2, 1, char); | |
2721 | Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char); | |
1bd3ad17 IZ |
2722 | mg->mg_ptr[mg->mg_len - 1] = ')'; |
2723 | mg->mg_ptr[mg->mg_len] = 0; | |
2724 | } | |
3280af22 | 2725 | PL_reginterp_cnt += re->program[0].next_off; |
1bd3ad17 IZ |
2726 | *lp = mg->mg_len; |
2727 | return mg->mg_ptr; | |
f9277f47 IZ |
2728 | } |
2729 | /* Fall through */ | |
ed6116ce LW |
2730 | case SVt_NULL: |
2731 | case SVt_IV: | |
2732 | case SVt_NV: | |
2733 | case SVt_RV: | |
2734 | case SVt_PV: | |
2735 | case SVt_PVIV: | |
2736 | case SVt_PVNV: | |
81689caa HS |
2737 | case SVt_PVBM: if (SvROK(sv)) |
2738 | s = "REF"; | |
2739 | else | |
2740 | s = "SCALAR"; break; | |
ed6116ce LW |
2741 | case SVt_PVLV: s = "LVALUE"; break; |
2742 | case SVt_PVAV: s = "ARRAY"; break; | |
2743 | case SVt_PVHV: s = "HASH"; break; | |
2744 | case SVt_PVCV: s = "CODE"; break; | |
2745 | case SVt_PVGV: s = "GLOB"; break; | |
1d2dff63 | 2746 | case SVt_PVFM: s = "FORMAT"; break; |
36477c24 | 2747 | case SVt_PVIO: s = "IO"; break; |
ed6116ce LW |
2748 | default: s = "UNKNOWN"; break; |
2749 | } | |
46fc3d4c | 2750 | tsv = NEWSV(0,0); |
ed6116ce | 2751 | if (SvOBJECT(sv)) |
cea2e8a9 | 2752 | Perl_sv_setpvf(aTHX_ tsv, "%s=%s", HvNAME(SvSTASH(sv)), s); |
ed6116ce | 2753 | else |
46fc3d4c | 2754 | sv_setpv(tsv, s); |
57def98f | 2755 | Perl_sv_catpvf(aTHX_ tsv, "(0x%"UVxf")", PTR2UV(sv)); |
a0d0e21e | 2756 | goto tokensaveref; |
463ee0b2 | 2757 | } |
ed6116ce LW |
2758 | *lp = strlen(s); |
2759 | return s; | |
79072805 | 2760 | } |
0336b60e | 2761 | if (SvREADONLY(sv) && !SvOK(sv)) { |
0336b60e | 2762 | if (ckWARN(WARN_UNINITIALIZED)) |
1d7c1841 | 2763 | report_uninit(); |
ed6116ce LW |
2764 | *lp = 0; |
2765 | return ""; | |
79072805 | 2766 | } |
79072805 | 2767 | } |
28e5dec8 JH |
2768 | if (SvIOK(sv) || ((SvIOKp(sv) && !SvNOKp(sv)))) { |
2769 | /* I'm assuming that if both IV and NV are equally valid then | |
2770 | converting the IV is going to be more efficient */ | |
2771 | U32 isIOK = SvIOK(sv); | |
2772 | U32 isUIOK = SvIsUV(sv); | |
2773 | char buf[TYPE_CHARS(UV)]; | |
2774 | char *ebuf, *ptr; | |
2775 | ||
2776 | if (SvTYPE(sv) < SVt_PVIV) | |
2777 | sv_upgrade(sv, SVt_PVIV); | |
2778 | if (isUIOK) | |
2779 | ptr = uiv_2buf(buf, 0, SvUVX(sv), 1, &ebuf); | |
2780 | else | |
2781 | ptr = uiv_2buf(buf, SvIVX(sv), 0, 0, &ebuf); | |
2782 | SvGROW(sv, ebuf - ptr + 1); /* inlined from sv_setpvn */ | |
2783 | Move(ptr,SvPVX(sv),ebuf - ptr,char); | |
2784 | SvCUR_set(sv, ebuf - ptr); | |
2785 | s = SvEND(sv); | |
2786 | *s = '\0'; | |
2787 | if (isIOK) | |
2788 | SvIOK_on(sv); | |
2789 | else | |
2790 | SvIOKp_on(sv); | |
2791 | if (isUIOK) | |
2792 | SvIsUV_on(sv); | |
2793 | } | |
2794 | else if (SvNOKp(sv)) { | |
79072805 LW |
2795 | if (SvTYPE(sv) < SVt_PVNV) |
2796 | sv_upgrade(sv, SVt_PVNV); | |
1c846c1f | 2797 | /* The +20 is pure guesswork. Configure test needed. --jhi */ |
59155cc0 | 2798 | SvGROW(sv, NV_DIG + 20); |
463ee0b2 | 2799 | s = SvPVX(sv); |
79072805 | 2800 | olderrno = errno; /* some Xenix systems wipe out errno here */ |
79072805 | 2801 | #ifdef apollo |
463ee0b2 | 2802 | if (SvNVX(sv) == 0.0) |
79072805 LW |
2803 | (void)strcpy(s,"0"); |
2804 | else | |
2805 | #endif /*apollo*/ | |
bbce6d69 | 2806 | { |
2d4389e4 | 2807 | Gconvert(SvNVX(sv), NV_DIG, 0, s); |
bbce6d69 | 2808 | } |
79072805 | 2809 | errno = olderrno; |
a0d0e21e LW |
2810 | #ifdef FIXNEGATIVEZERO |
2811 | if (*s == '-' && s[1] == '0' && !s[2]) | |
2812 | strcpy(s,"0"); | |
2813 | #endif | |
79072805 LW |
2814 | while (*s) s++; |
2815 | #ifdef hcx | |
2816 | if (s[-1] == '.') | |
46fc3d4c | 2817 | *--s = '\0'; |
79072805 LW |
2818 | #endif |
2819 | } | |
79072805 | 2820 | else { |
0336b60e IZ |
2821 | if (ckWARN(WARN_UNINITIALIZED) |
2822 | && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP)) | |
1d7c1841 | 2823 | report_uninit(); |
a0d0e21e | 2824 | *lp = 0; |
25da4f38 IZ |
2825 | if (SvTYPE(sv) < SVt_PV) |
2826 | /* Typically the caller expects that sv_any is not NULL now. */ | |
2827 | sv_upgrade(sv, SVt_PV); | |
a0d0e21e | 2828 | return ""; |
79072805 | 2829 | } |
463ee0b2 LW |
2830 | *lp = s - SvPVX(sv); |
2831 | SvCUR_set(sv, *lp); | |
79072805 | 2832 | SvPOK_on(sv); |
1d7c1841 GS |
2833 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n", |
2834 | PTR2UV(sv),SvPVX(sv))); | |
463ee0b2 | 2835 | return SvPVX(sv); |
a0d0e21e LW |
2836 | |
2837 | tokensave: | |
2838 | if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */ | |
2839 | /* Sneaky stuff here */ | |
2840 | ||
2841 | tokensaveref: | |
46fc3d4c | 2842 | if (!tsv) |
96827780 | 2843 | tsv = newSVpv(tmpbuf, 0); |
46fc3d4c | 2844 | sv_2mortal(tsv); |
2845 | *lp = SvCUR(tsv); | |
2846 | return SvPVX(tsv); | |
a0d0e21e LW |
2847 | } |
2848 | else { | |
2849 | STRLEN len; | |
46fc3d4c | 2850 | char *t; |
2851 | ||
2852 | if (tsv) { | |
2853 | sv_2mortal(tsv); | |
2854 | t = SvPVX(tsv); | |
2855 | len = SvCUR(tsv); | |
2856 | } | |
2857 | else { | |
96827780 MB |
2858 | t = tmpbuf; |
2859 | len = strlen(tmpbuf); | |
46fc3d4c | 2860 | } |
a0d0e21e | 2861 | #ifdef FIXNEGATIVEZERO |
46fc3d4c | 2862 | if (len == 2 && t[0] == '-' && t[1] == '0') { |
2863 | t = "0"; | |
2864 | len = 1; | |
2865 | } | |
a0d0e21e LW |
2866 | #endif |
2867 | (void)SvUPGRADE(sv, SVt_PV); | |
46fc3d4c | 2868 | *lp = len; |
a0d0e21e LW |
2869 | s = SvGROW(sv, len + 1); |
2870 | SvCUR_set(sv, len); | |
46fc3d4c | 2871 | (void)strcpy(s, t); |
6bf554b4 | 2872 | SvPOKp_on(sv); |
a0d0e21e LW |
2873 | return s; |
2874 | } | |
463ee0b2 LW |
2875 | } |
2876 | ||
7340a771 GS |
2877 | char * |
2878 | Perl_sv_2pvbyte_nolen(pTHX_ register SV *sv) | |
2879 | { | |
560a288e GS |
2880 | STRLEN n_a; |
2881 | return sv_2pvbyte(sv, &n_a); | |
7340a771 GS |
2882 | } |
2883 | ||
2884 | char * | |
2885 | Perl_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp) | |
2886 | { | |
2887 | return sv_2pv(sv,lp); | |
2888 | } | |
2889 | ||
2890 | char * | |
2891 | Perl_sv_2pvutf8_nolen(pTHX_ register SV *sv) | |
2892 | { | |
560a288e GS |
2893 | STRLEN n_a; |
2894 | return sv_2pvutf8(sv, &n_a); | |
7340a771 GS |
2895 | } |
2896 | ||
2897 | char * | |
2898 | Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp) | |
2899 | { | |
560a288e | 2900 | sv_utf8_upgrade(sv); |
7d59b7e4 | 2901 | return SvPV(sv,*lp); |
7340a771 | 2902 | } |
1c846c1f | 2903 | |
463ee0b2 LW |
2904 | /* This function is only called on magical items */ |
2905 | bool | |
864dbfa3 | 2906 | Perl_sv_2bool(pTHX_ register SV *sv) |
463ee0b2 | 2907 | { |
8990e307 | 2908 | if (SvGMAGICAL(sv)) |
463ee0b2 LW |
2909 | mg_get(sv); |
2910 | ||
a0d0e21e LW |
2911 | if (!SvOK(sv)) |
2912 | return 0; | |
2913 | if (SvROK(sv)) { | |
a0d0e21e | 2914 | SV* tmpsv; |
1554e226 DC |
2915 | if (SvAMAGIC(sv) && (tmpsv=AMG_CALLun(sv,bool_)) && |
2916 | (SvRV(tmpsv) != SvRV(sv))) | |
9e7bc3e8 | 2917 | return SvTRUE(tmpsv); |
a0d0e21e LW |
2918 | return SvRV(sv) != 0; |
2919 | } | |
463ee0b2 | 2920 | if (SvPOKp(sv)) { |
11343788 MB |
2921 | register XPV* Xpvtmp; |
2922 | if ((Xpvtmp = (XPV*)SvANY(sv)) && | |
2923 | (*Xpvtmp->xpv_pv > '0' || | |
2924 | Xpvtmp->xpv_cur > 1 || | |
2925 | (Xpvtmp->xpv_cur && *Xpvtmp->xpv_pv != '0'))) | |
463ee0b2 LW |
2926 | return 1; |
2927 | else | |
2928 | return 0; | |
2929 | } | |
2930 | else { | |
2931 | if (SvIOKp(sv)) | |
2932 | return SvIVX(sv) != 0; | |
2933 | else { | |
2934 | if (SvNOKp(sv)) | |
2935 | return SvNVX(sv) != 0.0; | |
2936 | else | |
2937 | return FALSE; | |
2938 | } | |
2939 | } | |
79072805 LW |
2940 | } |
2941 | ||
c461cf8f JH |
2942 | /* |
2943 | =for apidoc sv_utf8_upgrade | |
2944 | ||
2945 | Convert the PV of an SV to its UTF8-encoded form. | |
4411f3b6 NIS |
2946 | Forces the SV to string form it it is not already. |
2947 | Always sets the SvUTF8 flag to avoid future validity checks even | |
2948 | if all the bytes have hibit clear. | |
c461cf8f JH |
2949 | |
2950 | =cut | |
2951 | */ | |
2952 | ||
4411f3b6 | 2953 | STRLEN |
560a288e GS |
2954 | Perl_sv_utf8_upgrade(pTHX_ register SV *sv) |
2955 | { | |
511c2ff0 NIS |
2956 | char *s, *t, *e; |
2957 | int hibit = 0; | |
560a288e | 2958 | |
4411f3b6 NIS |
2959 | if (!sv) |
2960 | return 0; | |
2961 | ||
2962 | if (!SvPOK(sv)) | |
2963 | (void) SvPV_nolen(sv); | |
2964 | ||
2965 | if (SvUTF8(sv)) | |
2966 | return SvCUR(sv); | |
560a288e | 2967 | |
40826f67 JH |
2968 | /* This function could be much more efficient if we had a FLAG in SVs |
2969 | * to signal if there are any hibit chars in the PV. | |
511c2ff0 | 2970 | * Given that there isn't make loop fast as possible |
560a288e | 2971 | */ |
511c2ff0 NIS |
2972 | s = SvPVX(sv); |
2973 | e = SvEND(sv); | |
2974 | t = s; | |
2975 | while (t < e) { | |
fd400ab9 | 2976 | if ((hibit = UTF8_IS_CONTINUED(*t++))) |
8a818333 | 2977 | break; |
8a818333 | 2978 | } |
560a288e | 2979 | |
40826f67 | 2980 | if (hibit) { |
8a818333 | 2981 | STRLEN len; |
652088fc | 2982 | |
8a818333 | 2983 | if (SvREADONLY(sv) && SvFAKE(sv)) { |
8a818333 NIS |
2984 | sv_force_normal(sv); |
2985 | s = SvPVX(sv); | |
2986 | } | |
2987 | len = SvCUR(sv) + 1; /* Plus the \0 */ | |
00df9076 | 2988 | SvPVX(sv) = (char*)bytes_to_utf8((U8*)s, &len); |
841d7a39 | 2989 | SvCUR(sv) = len - 1; |
511c2ff0 NIS |
2990 | if (SvLEN(sv) != 0) |
2991 | Safefree(s); /* No longer using what was there before. */ | |
841d7a39 | 2992 | SvLEN(sv) = len; /* No longer know the real size. */ |
560a288e | 2993 | } |
4411f3b6 NIS |
2994 | /* Mark as UTF-8 even if no hibit - saves scanning loop */ |
2995 | SvUTF8_on(sv); | |
2996 | return SvCUR(sv); | |
560a288e GS |
2997 | } |
2998 | ||
c461cf8f JH |
2999 | /* |
3000 | =for apidoc sv_utf8_downgrade | |
3001 | ||
3002 | Attempt to convert the PV of an SV from UTF8-encoded to byte encoding. | |
3003 | This may not be possible if the PV contains non-byte encoding characters; | |
3004 | if this is the case, either returns false or, if C<fail_ok> is not | |
3005 | true, croaks. | |
3006 | ||
3007 | =cut | |
3008 | */ | |
3009 | ||
560a288e GS |
3010 | bool |
3011 | Perl_sv_utf8_downgrade(pTHX_ register SV* sv, bool fail_ok) | |
3012 | { | |
3013 | if (SvPOK(sv) && SvUTF8(sv)) { | |
fa301091 | 3014 | if (SvCUR(sv)) { |
652088fc JH |
3015 | char *s; |
3016 | STRLEN len; | |
fa301091 | 3017 | |
652088fc JH |
3018 | if (SvREADONLY(sv) && SvFAKE(sv)) |
3019 | sv_force_normal(sv); | |
3020 | s = SvPV(sv, len); | |
3021 | if (!utf8_to_bytes((U8*)s, &len)) { | |
fa301091 JH |
3022 | if (fail_ok) |
3023 | return FALSE; | |
3024 | else { | |
3025 | if (PL_op) | |
3026 | Perl_croak(aTHX_ "Wide character in %s", | |
3027 | PL_op_desc[PL_op->op_type]); | |
3028 | else | |
3029 | Perl_croak(aTHX_ "Wide character"); | |
3030 | } | |
4b3603a4 | 3031 | } |
fa301091 | 3032 | SvCUR(sv) = len; |
67e989fb | 3033 | } |
9f9ab905 | 3034 | SvUTF8_off(sv); |
560a288e | 3035 | } |
fa301091 | 3036 | |
560a288e GS |
3037 | return TRUE; |
3038 | } | |
3039 | ||
c461cf8f JH |
3040 | /* |
3041 | =for apidoc sv_utf8_encode | |
3042 | ||
3043 | Convert the PV of an SV to UTF8-encoded, but then turn off the C<SvUTF8> | |
4411f3b6 NIS |
3044 | flag so that it looks like octets again. Used as a building block |
3045 | for encode_utf8 in Encode.xs | |
c461cf8f JH |
3046 | |
3047 | =cut | |
3048 | */ | |
3049 | ||
560a288e GS |
3050 | void |
3051 | Perl_sv_utf8_encode(pTHX_ register SV *sv) | |
3052 | { | |
4411f3b6 | 3053 | (void) sv_utf8_upgrade(sv); |
560a288e GS |
3054 | SvUTF8_off(sv); |
3055 | } | |
3056 | ||
4411f3b6 NIS |
3057 | /* |
3058 | =for apidoc sv_utf8_decode | |
3059 | ||
3060 | Convert the octets in the PV from UTF-8 to chars. Scan for validity and then | |
3061 | turn of SvUTF8 if needed so that we see characters. Used as a building block | |
3062 | for decode_utf8 in Encode.xs | |
3063 | ||
3064 | =cut | |
3065 | */ | |
3066 | ||
3067 | ||
3068 | ||
560a288e GS |
3069 | bool |
3070 | Perl_sv_utf8_decode(pTHX_ register SV *sv) | |
3071 | { | |
3072 | if (SvPOK(sv)) { | |
3073 | char *c; | |
511c2ff0 | 3074 | char *e; |
9cbac4c7 | 3075 | |
4411f3b6 | 3076 | /* The octets may have got themselves encoded - get them back as bytes */ |
560a288e GS |
3077 | if (!sv_utf8_downgrade(sv, TRUE)) |
3078 | return FALSE; | |
3079 | ||
3080 | /* it is actually just a matter of turning the utf8 flag on, but | |
3081 | * we want to make sure everything inside is valid utf8 first. | |
3082 | */ | |
3083 | c = SvPVX(sv); | |
00df9076 | 3084 | if (!is_utf8_string((U8*)c, SvCUR(sv)+1)) |
67e989fb | 3085 | return FALSE; |
511c2ff0 NIS |
3086 | e = SvEND(sv); |
3087 | while (c < e) { | |
fd400ab9 | 3088 | if (UTF8_IS_CONTINUED(*c++)) { |
67e989fb JH |
3089 | SvUTF8_on(sv); |
3090 | break; | |
3091 | } | |
560a288e | 3092 | } |
560a288e GS |
3093 | } |
3094 | return TRUE; | |
3095 | } | |
3096 | ||
3097 | ||
79072805 | 3098 | /* Note: sv_setsv() should not be called with a source string that needs |
463ee0b2 | 3099 | * to be reused, since it may destroy the source string if it is marked |
79072805 LW |
3100 | * as temporary. |
3101 | */ | |
3102 | ||
954c1994 GS |
3103 | /* |
3104 | =for apidoc sv_setsv | |
3105 | ||
3106 | Copies the contents of the source SV C<ssv> into the destination SV C<dsv>. | |
3107 | The source SV may be destroyed if it is mortal. Does not handle 'set' | |
3108 | magic. See the macro forms C<SvSetSV>, C<SvSetSV_nosteal> and | |
3109 | C<sv_setsv_mg>. | |
3110 | ||
3111 | =cut | |
3112 | */ | |
3113 | ||
79072805 | 3114 | void |
864dbfa3 | 3115 | Perl_sv_setsv(pTHX_ SV *dstr, register SV *sstr) |
79072805 | 3116 | { |
8990e307 LW |
3117 | register U32 sflags; |
3118 | register int dtype; | |
3119 | register int stype; | |
463ee0b2 | 3120 | |
79072805 LW |
3121 | if (sstr == dstr) |
3122 | return; | |
2213622d | 3123 | SV_CHECK_THINKFIRST(dstr); |
79072805 | 3124 | if (!sstr) |
3280af22 | 3125 | sstr = &PL_sv_undef; |
8990e307 LW |
3126 | stype = SvTYPE(sstr); |
3127 | dtype = SvTYPE(dstr); | |
79072805 | 3128 | |
a0d0e21e | 3129 | SvAMAGIC_off(dstr); |
9e7bc3e8 | 3130 | |
463ee0b2 | 3131 | /* There's a lot of redundancy below but we're going for speed here */ |
79072805 | 3132 | |
8990e307 | 3133 | switch (stype) { |
79072805 | 3134 | case SVt_NULL: |
aece5585 | 3135 | undef_sstr: |
20408e3c GS |
3136 | if (dtype != SVt_PVGV) { |
3137 | (void)SvOK_off(dstr); | |
3138 | return; | |
3139 | } | |
3140 | break; | |
463ee0b2 | 3141 | case SVt_IV: |
aece5585 GA |
3142 | if (SvIOK(sstr)) { |
3143 | switch (dtype) { | |
3144 | case SVt_NULL: | |
8990e307 | 3145 | sv_upgrade(dstr, SVt_IV); |
aece5585 GA |
3146 | break; |
3147 | case SVt_NV: | |
8990e307 | 3148 | sv_upgrade(dstr, SVt_PVNV); |
aece5585 GA |
3149 | break; |
3150 | case SVt_RV: | |
3151 | case SVt_PV: | |
a0d0e21e | 3152 | sv_upgrade(dstr, SVt_PVIV); |
aece5585 GA |
3153 | break; |
3154 | } | |
3155 | (void)SvIOK_only(dstr); | |
3156 | SvIVX(dstr) = SvIVX(sstr); | |
25da4f38 IZ |
3157 | if (SvIsUV(sstr)) |
3158 | SvIsUV_on(dstr); | |
27c9684d AP |
3159 | if (SvTAINTED(sstr)) |
3160 | SvTAINT(dstr); | |
aece5585 | 3161 | return; |
8990e307 | 3162 | } |
aece5585 GA |
3163 | goto undef_sstr; |
3164 | ||
463ee0b2 | 3165 | case SVt_NV: |
aece5585 GA |
3166 | if (SvNOK(sstr)) { |
3167 | switch (dtype) { | |
3168 | case SVt_NULL: | |
3169 | case SVt_IV: | |
8990e307 | 3170 | sv_upgrade(dstr, SVt_NV); |
aece5585 GA |
3171 | break; |
3172 | case SVt_RV: | |
3173 | case SVt_PV: | |
3174 | case SVt_PVIV: | |
a0d0e21e | 3175 | sv_upgrade(dstr, SVt_PVNV); |
aece5585 GA |
3176 | break; |
3177 | } | |
3178 | SvNVX(dstr) = SvNVX(sstr); | |
3179 | (void)SvNOK_only(dstr); | |
27c9684d AP |
3180 | if (SvTAINTED(sstr)) |
3181 | SvTAINT(dstr); | |
aece5585 | 3182 | return; |
8990e307 | 3183 | } |
aece5585 GA |
3184 | goto undef_sstr; |
3185 | ||
ed6116ce | 3186 | case SVt_RV: |
8990e307 | 3187 | if (dtype < SVt_RV) |
ed6116ce | 3188 | sv_upgrade(dstr, SVt_RV); |
c07a80fd | 3189 | else if (dtype == SVt_PVGV && |
3190 | SvTYPE(SvRV(sstr)) == SVt_PVGV) { | |
3191 | sstr = SvRV(sstr); | |
a5f75d66 | 3192 | if (sstr == dstr) { |
1d7c1841 GS |
3193 | if (GvIMPORTED(dstr) != GVf_IMPORTED |
3194 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) | |
3195 | { | |
a5f75d66 | 3196 | GvIMPORTED_on(dstr); |
1d7c1841 | 3197 | } |
a5f75d66 AD |
3198 | GvMULTI_on(dstr); |
3199 | return; | |
3200 | } | |
c07a80fd | 3201 | goto glob_assign; |
3202 | } | |
ed6116ce | 3203 | break; |
463ee0b2 | 3204 | case SVt_PV: |
fc36a67e | 3205 | case SVt_PVFM: |
8990e307 | 3206 | if (dtype < SVt_PV) |
463ee0b2 | 3207 | sv_upgrade(dstr, SVt_PV); |
463ee0b2 LW |
3208 | break; |
3209 | case SVt_PVIV: | |
8990e307 | 3210 | if (dtype < SVt_PVIV) |
463ee0b2 | 3211 | sv_upgrade(dstr, SVt_PVIV); |
463ee0b2 LW |
3212 | break; |
3213 | case SVt_PVNV: | |
8990e307 | 3214 | if (dtype < SVt_PVNV) |
463ee0b2 | 3215 | sv_upgrade(dstr, SVt_PVNV); |
463ee0b2 | 3216 | break; |
4633a7c4 LW |
3217 | case SVt_PVAV: |
3218 | case SVt_PVHV: | |
3219 | case SVt_PVCV: | |
4633a7c4 | 3220 | case SVt_PVIO: |
533c011a | 3221 | if (PL_op) |
cea2e8a9 | 3222 | Perl_croak(aTHX_ "Bizarre copy of %s in %s", sv_reftype(sstr, 0), |
22c35a8c | 3223 | PL_op_name[PL_op->op_type]); |
4633a7c4 | 3224 | else |
cea2e8a9 | 3225 | Perl_croak(aTHX_ "Bizarre copy of %s", sv_reftype(sstr, 0)); |
4633a7c4 LW |
3226 | break; |
3227 | ||
79072805 | 3228 | case SVt_PVGV: |
8990e307 | 3229 | if (dtype <= SVt_PVGV) { |
c07a80fd | 3230 | glob_assign: |
a5f75d66 | 3231 | if (dtype != SVt_PVGV) { |
a0d0e21e LW |
3232 | char *name = GvNAME(sstr); |
3233 | STRLEN len = GvNAMELEN(sstr); | |
463ee0b2 | 3234 | sv_upgrade(dstr, SVt_PVGV); |
6662521e | 3235 | sv_magic(dstr, dstr, '*', Nullch, 0); |
85aff577 | 3236 | GvSTASH(dstr) = (HV*)SvREFCNT_inc(GvSTASH(sstr)); |
a0d0e21e LW |
3237 | GvNAME(dstr) = savepvn(name, len); |
3238 | GvNAMELEN(dstr) = len; | |
3239 | SvFAKE_on(dstr); /* can coerce to non-glob */ | |
3240 | } | |
7bac28a0 | 3241 | /* ahem, death to those who redefine active sort subs */ |
3280af22 NIS |
3242 | else if (PL_curstackinfo->si_type == PERLSI_SORT |
3243 | && GvCV(dstr) && PL_sortcop == CvSTART(GvCV(dstr))) | |
cea2e8a9 | 3244 | Perl_croak(aTHX_ "Can't redefine active sort subroutine %s", |
7bac28a0 | 3245 | GvNAME(dstr)); |
5bd07a3d DM |
3246 | |
3247 | #ifdef GV_SHARED_CHECK | |
3248 | if (GvSHARED((GV*)dstr)) { | |
3249 | Perl_croak(aTHX_ PL_no_modify); | |
3250 | } | |
3251 | #endif | |
3252 | ||
a0d0e21e | 3253 | (void)SvOK_off(dstr); |
a5f75d66 | 3254 | GvINTRO_off(dstr); /* one-shot flag */ |
1edc1566 | 3255 | gp_free((GV*)dstr); |
79072805 | 3256 | GvGP(dstr) = gp_ref(GvGP(sstr)); |
27c9684d AP |
3257 | if (SvTAINTED(sstr)) |
3258 | SvTAINT(dstr); | |
1d7c1841 GS |
3259 | if (GvIMPORTED(dstr) != GVf_IMPORTED |
3260 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) | |
3261 | { | |
a5f75d66 | 3262 | GvIMPORTED_on(dstr); |
1d7c1841 | 3263 | } |
a5f75d66 | 3264 | GvMULTI_on(dstr); |
79072805 LW |
3265 | return; |
3266 | } | |
3267 | /* FALL THROUGH */ | |
3268 | ||
3269 | default: | |
973f89ab CS |
3270 | if (SvGMAGICAL(sstr)) { |
3271 | mg_get(sstr); | |
3272 | if (SvTYPE(sstr) != stype) { | |
3273 | stype = SvTYPE(sstr); | |
3274 | if (stype == SVt_PVGV && dtype <= SVt_PVGV) | |
3275 | goto glob_assign; | |
3276 | } | |
3277 | } | |
ded42b9f | 3278 | if (stype == SVt_PVLV) |
6fc92669 | 3279 | (void)SvUPGRADE(dstr, SVt_PVNV); |
ded42b9f | 3280 | else |
6fc92669 | 3281 | (void)SvUPGRADE(dstr, stype); |
79072805 LW |
3282 | } |
3283 | ||
8990e307 LW |
3284 | sflags = SvFLAGS(sstr); |
3285 | ||
3286 | if (sflags & SVf_ROK) { | |
3287 | if (dtype >= SVt_PV) { | |
3288 | if (dtype == SVt_PVGV) { | |
3289 | SV *sref = SvREFCNT_inc(SvRV(sstr)); | |
3290 | SV *dref = 0; | |
a5f75d66 | 3291 | int intro = GvINTRO(dstr); |
a0d0e21e | 3292 | |
5bd07a3d DM |
3293 | #ifdef GV_SHARED_CHECK |
3294 | if (GvSHARED((GV*)dstr)) { | |
3295 | Perl_croak(aTHX_ PL_no_modify); | |
3296 | } | |
3297 | #endif | |
3298 | ||
a0d0e21e LW |
3299 | if (intro) { |
3300 | GP *gp; | |
1d7c1841 | 3301 | gp_free((GV*)dstr); |
a5f75d66 | 3302 | GvINTRO_off(dstr); /* one-shot flag */ |
a0d0e21e | 3303 | Newz(602,gp, 1, GP); |
44a8e56a | 3304 | GvGP(dstr) = gp_ref(gp); |
a0d0e21e | 3305 | GvSV(dstr) = NEWSV(72,0); |
1d7c1841 | 3306 | GvLINE(dstr) = CopLINE(PL_curcop); |
1edc1566 | 3307 | GvEGV(dstr) = (GV*)dstr; |
a0d0e21e | 3308 | } |
a5f75d66 | 3309 | GvMULTI_on(dstr); |
8990e307 LW |
3310 | switch (SvTYPE(sref)) { |
3311 | case SVt_PVAV: | |
a0d0e21e LW |
3312 | if (intro) |
3313 | SAVESPTR(GvAV(dstr)); | |
3314 | else | |
3315 | dref = (SV*)GvAV(dstr); | |
8990e307 | 3316 | GvAV(dstr) = (AV*)sref; |
39bac7f7 | 3317 | if (!GvIMPORTED_AV(dstr) |
1d7c1841 GS |
3318 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
3319 | { | |
a5f75d66 | 3320 | GvIMPORTED_AV_on(dstr); |
1d7c1841 | 3321 | } |
8990e307 LW |
3322 | break; |
3323 | case SVt_PVHV: | |
a0d0e21e LW |
3324 | if (intro) |
3325 | SAVESPTR(GvHV(dstr)); | |
3326 | else | |
3327 | dref = (SV*)GvHV(dstr); | |
8990e307 | 3328 | GvHV(dstr) = (HV*)sref; |
39bac7f7 | 3329 | if (!GvIMPORTED_HV(dstr) |
1d7c1841 GS |
3330 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
3331 | { | |
a5f75d66 | 3332 | GvIMPORTED_HV_on(dstr); |
1d7c1841 | 3333 | } |
8990e307 LW |
3334 | break; |
3335 | case SVt_PVCV: | |
8ebc5c01 | 3336 | if (intro) { |
3337 | if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) { | |
3338 | SvREFCNT_dec(GvCV(dstr)); | |
3339 | GvCV(dstr) = Nullcv; | |
68dc0745 | 3340 | GvCVGEN(dstr) = 0; /* Switch off cacheness. */ |
3280af22 | 3341 | PL_sub_generation++; |
8ebc5c01 | 3342 | } |
a0d0e21e | 3343 | SAVESPTR(GvCV(dstr)); |
8ebc5c01 | 3344 | } |
68dc0745 | 3345 | else |
3346 | dref = (SV*)GvCV(dstr); | |
3347 | if (GvCV(dstr) != (CV*)sref) { | |
748a9306 | 3348 | CV* cv = GvCV(dstr); |
4633a7c4 | 3349 | if (cv) { |
68dc0745 | 3350 | if (!GvCVGEN((GV*)dstr) && |
3351 | (CvROOT(cv) || CvXSUB(cv))) | |
3352 | { | |
7bac28a0 | 3353 | /* ahem, death to those who redefine |
3354 | * active sort subs */ | |
3280af22 NIS |
3355 | if (PL_curstackinfo->si_type == PERLSI_SORT && |
3356 | PL_sortcop == CvSTART(cv)) | |
1c846c1f | 3357 | Perl_croak(aTHX_ |
7bac28a0 | 3358 | "Can't redefine active sort subroutine %s", |
3359 | GvENAME((GV*)dstr)); | |
beab0874 JT |
3360 | /* Redefining a sub - warning is mandatory if |
3361 | it was a const and its value changed. */ | |
3362 | if (ckWARN(WARN_REDEFINE) | |
3363 | || (CvCONST(cv) | |
3364 | && (!CvCONST((CV*)sref) | |
3365 | || sv_cmp(cv_const_sv(cv), | |
3366 | cv_const_sv((CV*)sref))))) | |
3367 | { | |
3368 | Perl_warner(aTHX_ WARN_REDEFINE, | |
3369 | CvCONST(cv) | |
3370 | ? "Constant subroutine %s redefined" | |
47deb5e7 | 3371 | : "Subroutine %s redefined", |
beab0874 JT |
3372 | GvENAME((GV*)dstr)); |
3373 | } | |
9607fc9c | 3374 | } |
3fe9a6f1 | 3375 | cv_ckproto(cv, (GV*)dstr, |
3376 | SvPOK(sref) ? SvPVX(sref) : Nullch); | |
4633a7c4 | 3377 | } |
a5f75d66 | 3378 | GvCV(dstr) = (CV*)sref; |
7a4c00b4 | 3379 | GvCVGEN(dstr) = 0; /* Switch off cacheness. */ |
a5f75d66 | 3380 | GvASSUMECV_on(dstr); |
3280af22 | 3381 | PL_sub_generation++; |
a5f75d66 | 3382 | } |
39bac7f7 | 3383 | if (!GvIMPORTED_CV(dstr) |
1d7c1841 GS |
3384 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
3385 | { | |
a5f75d66 | 3386 | GvIMPORTED_CV_on(dstr); |
1d7c1841 | 3387 | } |
8990e307 | 3388 | break; |
91bba347 LW |
3389 | case SVt_PVIO: |
3390 | if (intro) | |
3391 | SAVESPTR(GvIOp(dstr)); | |
3392 | else | |
3393 | dref = (SV*)GvIOp(dstr); | |
3394 | GvIOp(dstr) = (IO*)sref; | |
3395 | break; | |
f4d13ee9 JH |
3396 | case SVt_PVFM: |
3397 | if (intro) | |
3398 | SAVESPTR(GvFORM(dstr)); | |
3399 | else | |
3400 | dref = (SV*)GvFORM(dstr); | |
3401 | GvFORM(dstr) = (CV*)sref; | |
3402 | break; | |
8990e307 | 3403 | default: |
a0d0e21e LW |
3404 | if (intro) |
3405 | SAVESPTR(GvSV(dstr)); | |
3406 | else | |
3407 | dref = (SV*)GvSV(dstr); | |
8990e307 | 3408 | GvSV(dstr) = sref; |
39bac7f7 | 3409 | if (!GvIMPORTED_SV(dstr) |
1d7c1841 GS |
3410 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
3411 | { | |
a5f75d66 | 3412 | GvIMPORTED_SV_on(dstr); |
1d7c1841 | 3413 | } |
8990e307 LW |
3414 | break; |
3415 | } | |
3416 | if (dref) | |
3417 | SvREFCNT_dec(dref); | |
a0d0e21e LW |
3418 | if (intro) |
3419 | SAVEFREESV(sref); | |
27c9684d AP |
3420 | if (SvTAINTED(sstr)) |
3421 | SvTAINT(dstr); | |
8990e307 LW |
3422 | return; |
3423 | } | |
a0d0e21e | 3424 | if (SvPVX(dstr)) { |
760ac839 | 3425 | (void)SvOOK_off(dstr); /* backoff */ |
50483b2c JD |
3426 | if (SvLEN(dstr)) |
3427 | Safefree(SvPVX(dstr)); | |
a0d0e21e LW |
3428 | SvLEN(dstr)=SvCUR(dstr)=0; |
3429 | } | |
8990e307 | 3430 | } |
a0d0e21e | 3431 | (void)SvOK_off(dstr); |
8990e307 | 3432 | SvRV(dstr) = SvREFCNT_inc(SvRV(sstr)); |
ed6116ce | 3433 | SvROK_on(dstr); |
8990e307 | 3434 | if (sflags & SVp_NOK) { |
3332b3c1 JH |
3435 | SvNOKp_on(dstr); |
3436 | /* Only set the public OK flag if the source has public OK. */ | |
3437 | if (sflags & SVf_NOK) | |
3438 | SvFLAGS(dstr) |= SVf_NOK; | |
ed6116ce LW |
3439 | SvNVX(dstr) = SvNVX(sstr); |
3440 | } | |
8990e307 | 3441 | if (sflags & SVp_IOK) { |
3332b3c1 JH |
3442 | (void)SvIOKp_on(dstr); |
3443 | if (sflags & SVf_IOK) | |
3444 | SvFLAGS(dstr) |= SVf_IOK; | |
2b1c7e3e | 3445 | if (sflags & SVf_IVisUV) |
25da4f38 | 3446 | SvIsUV_on(dstr); |
3332b3c1 | 3447 | SvIVX(dstr) = SvIVX(sstr); |
ed6116ce | 3448 | } |
a0d0e21e LW |
3449 | if (SvAMAGIC(sstr)) { |
3450 | SvAMAGIC_on(dstr); | |
3451 | } | |
ed6116ce | 3452 | } |
8990e307 | 3453 | else if (sflags & SVp_POK) { |
79072805 LW |
3454 | |
3455 | /* | |
3456 | * Check to see if we can just swipe the string. If so, it's a | |
3457 | * possible small lose on short strings, but a big win on long ones. | |
463ee0b2 LW |
3458 | * It might even be a win on short strings if SvPVX(dstr) |
3459 | * has to be allocated and SvPVX(sstr) has to be freed. | |
79072805 LW |
3460 | */ |
3461 | ||
ff68c719 | 3462 | if (SvTEMP(sstr) && /* slated for free anyway? */ |
01b73108 | 3463 | SvREFCNT(sstr) == 1 && /* and no other references to it? */ |
1c846c1f | 3464 | !(sflags & SVf_OOK) && /* and not involved in OOK hack? */ |
4c8f17b9 BH |
3465 | SvLEN(sstr) && /* and really is a string */ |
3466 | !(PL_op && PL_op->op_type == OP_AASSIGN)) /* and won't be needed again, potentially */ | |
a5f75d66 | 3467 | { |
adbc6bb1 | 3468 | if (SvPVX(dstr)) { /* we know that dtype >= SVt_PV */ |
a5f75d66 AD |
3469 | if (SvOOK(dstr)) { |
3470 | SvFLAGS(dstr) &= ~SVf_OOK; | |
3471 | Safefree(SvPVX(dstr) - SvIVX(dstr)); | |
3472 | } | |
50483b2c | 3473 | else if (SvLEN(dstr)) |
a5f75d66 | 3474 | Safefree(SvPVX(dstr)); |
79072805 | 3475 | } |
a5f75d66 | 3476 | (void)SvPOK_only(dstr); |
463ee0b2 | 3477 | SvPV_set(dstr, SvPVX(sstr)); |
79072805 LW |
3478 | SvLEN_set(dstr, SvLEN(sstr)); |
3479 | SvCUR_set(dstr, SvCUR(sstr)); | |
f4e86e0f | 3480 | |
79072805 | 3481 | SvTEMP_off(dstr); |
2b1c7e3e | 3482 | (void)SvOK_off(sstr); /* NOTE: nukes most SvFLAGS on sstr */ |
79072805 LW |
3483 | SvPV_set(sstr, Nullch); |
3484 | SvLEN_set(sstr, 0); | |
a5f75d66 AD |
3485 | SvCUR_set(sstr, 0); |
3486 | SvTEMP_off(sstr); | |
79072805 LW |
3487 | } |
3488 | else { /* have to copy actual string */ | |
8990e307 LW |
3489 | STRLEN len = SvCUR(sstr); |
3490 | ||
3491 | SvGROW(dstr, len + 1); /* inlined from sv_setpvn */ | |
3492 | Move(SvPVX(sstr),SvPVX(dstr),len,char); | |
3493 | SvCUR_set(dstr, len); | |
3494 | *SvEND(dstr) = '\0'; | |
a0d0e21e | 3495 | (void)SvPOK_only(dstr); |
79072805 | 3496 | } |
9aa983d2 | 3497 | if (sflags & SVf_UTF8) |
a7cb1f99 | 3498 | SvUTF8_on(dstr); |
79072805 | 3499 | /*SUPPRESS 560*/ |
8990e307 | 3500 | if (sflags & SVp_NOK) { |
3332b3c1 JH |
3501 | SvNOKp_on(dstr); |
3502 | if (sflags & SVf_NOK) | |
3503 | SvFLAGS(dstr) |= SVf_NOK; | |
463ee0b2 | 3504 | SvNVX(dstr) = SvNVX(sstr); |
79072805 | 3505 | } |
8990e307 | 3506 | if (sflags & SVp_IOK) { |
3332b3c1 JH |
3507 | (void)SvIOKp_on(dstr); |
3508 | if (sflags & SVf_IOK) | |
3509 | SvFLAGS(dstr) |= SVf_IOK; | |
2b1c7e3e | 3510 | if (sflags & SVf_IVisUV) |
25da4f38 | 3511 | SvIsUV_on(dstr); |
463ee0b2 | 3512 | SvIVX(dstr) = SvIVX(sstr); |
79072805 LW |
3513 | } |
3514 | } | |
8990e307 | 3515 | else if (sflags & SVp_IOK) { |
3332b3c1 JH |
3516 | if (sflags & SVf_IOK) |
3517 | (void)SvIOK_only(dstr); | |
3518 | else { | |
9cbac4c7 DM |
3519 | (void)SvOK_off(dstr); |
3520 | (void)SvIOKp_on(dstr); | |
3332b3c1 JH |
3521 | } |
3522 | /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */ | |
2b1c7e3e | 3523 | if (sflags & SVf_IVisUV) |
25da4f38 | 3524 | SvIsUV_on(dstr); |
3332b3c1 JH |
3525 | SvIVX(dstr) = SvIVX(sstr); |
3526 | if (sflags & SVp_NOK) { | |
3527 | if (sflags & SVf_NOK) | |
3528 | (void)SvNOK_on(dstr); | |
3529 | else | |
3530 | (void)SvNOKp_on(dstr); | |
3531 | SvNVX(dstr) = SvNVX(sstr); | |
3532 | } | |
3533 | } | |
3534 | else if (sflags & SVp_NOK) { | |
3535 | if (sflags & SVf_NOK) | |
3536 | (void)SvNOK_only(dstr); | |
3537 | else { | |
9cbac4c7 | 3538 | (void)SvOK_off(dstr); |
3332b3c1 JH |
3539 | SvNOKp_on(dstr); |
3540 | } | |
3541 | SvNVX(dstr) = SvNVX(sstr); | |
79072805 LW |
3542 | } |
3543 | else { | |
20408e3c | 3544 | if (dtype == SVt_PVGV) { |
e476b1b5 GS |
3545 | if (ckWARN(WARN_MISC)) |
3546 | Perl_warner(aTHX_ WARN_MISC, "Undefined value assigned to typeglob"); | |
20408e3c GS |
3547 | } |
3548 | else | |
3549 | (void)SvOK_off(dstr); | |
a0d0e21e | 3550 | } |
27c9684d AP |
3551 | if (SvTAINTED(sstr)) |
3552 | SvTAINT(dstr); | |
79072805 LW |
3553 | } |
3554 | ||
954c1994 GS |
3555 | /* |
3556 | =for apidoc sv_setsv_mg | |
3557 | ||
3558 | Like C<sv_setsv>, but also handles 'set' magic. | |
3559 | ||
3560 | =cut | |
3561 | */ | |
3562 | ||
79072805 | 3563 | void |
864dbfa3 | 3564 | Perl_sv_setsv_mg(pTHX_ SV *dstr, register SV *sstr) |
ef50df4b GS |
3565 | { |
3566 | sv_setsv(dstr,sstr); | |
3567 | SvSETMAGIC(dstr); | |
3568 | } | |
3569 | ||
954c1994 GS |
3570 | /* |
3571 | =for apidoc sv_setpvn | |
3572 | ||
3573 | Copies a string into an SV. The C<len> parameter indicates the number of | |
3574 | bytes to be copied. Does not handle 'set' magic. See C<sv_setpvn_mg>. | |
3575 | ||
3576 | =cut | |
3577 | */ | |
3578 | ||
ef50df4b | 3579 | void |
864dbfa3 | 3580 | Perl_sv_setpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len) |
79072805 | 3581 | { |
c6f8c383 | 3582 | register char *dptr; |
22c522df | 3583 | |
2213622d | 3584 | SV_CHECK_THINKFIRST(sv); |
463ee0b2 | 3585 | if (!ptr) { |
a0d0e21e | 3586 | (void)SvOK_off(sv); |
463ee0b2 LW |
3587 | return; |
3588 | } | |
22c522df JH |
3589 | else { |
3590 | /* len is STRLEN which is unsigned, need to copy to signed */ | |
3591 | IV iv = len; | |
3592 | assert(iv >= 0); | |
3593 | } | |
6fc92669 | 3594 | (void)SvUPGRADE(sv, SVt_PV); |
c6f8c383 | 3595 | |
79072805 | 3596 | SvGROW(sv, len + 1); |
c6f8c383 GA |
3597 | dptr = SvPVX(sv); |
3598 | Move(ptr,dptr,len,char); | |
3599 | dptr[len] = '\0'; | |
79072805 | 3600 | SvCUR_set(sv, len); |
1aa99e6b | 3601 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 | 3602 | SvTAINT(sv); |
79072805 LW |
3603 | } |
3604 | ||
954c1994 GS |
3605 | /* |
3606 | =for apidoc sv_setpvn_mg | |
3607 | ||
3608 | Like C<sv_setpvn>, but also handles 'set' magic. | |
3609 | ||
3610 | =cut | |
3611 | */ | |
3612 | ||
79072805 | 3613 | void |
864dbfa3 | 3614 | Perl_sv_setpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len) |
ef50df4b GS |
3615 | { |
3616 | sv_setpvn(sv,ptr,len); | |
3617 | SvSETMAGIC(sv); | |
3618 | } | |
3619 | ||
954c1994 GS |
3620 | /* |
3621 | =for apidoc sv_setpv | |
3622 | ||
3623 | Copies a string into an SV. The string must be null-terminated. Does not | |
3624 | handle 'set' magic. See C<sv_setpv_mg>. | |
3625 | ||
3626 | =cut | |
3627 | */ | |
3628 | ||
ef50df4b | 3629 | void |
864dbfa3 | 3630 | Perl_sv_setpv(pTHX_ register SV *sv, register const char *ptr) |
79072805 LW |
3631 | { |
3632 | register STRLEN len; | |
3633 | ||
2213622d | 3634 | SV_CHECK_THINKFIRST(sv); |
463ee0b2 | 3635 | if (!ptr) { |
a0d0e21e | 3636 | (void)SvOK_off(sv); |
463ee0b2 LW |
3637 | return; |
3638 | } | |
79072805 | 3639 | len = strlen(ptr); |
6fc92669 | 3640 | (void)SvUPGRADE(sv, SVt_PV); |
c6f8c383 | 3641 | |
79072805 | 3642 | SvGROW(sv, len + 1); |
463ee0b2 | 3643 | Move(ptr,SvPVX(sv),len+1,char); |
79072805 | 3644 | SvCUR_set(sv, len); |
1aa99e6b | 3645 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 LW |
3646 | SvTAINT(sv); |
3647 | } | |
3648 | ||
954c1994 GS |
3649 | /* |
3650 | =for apidoc sv_setpv_mg | |
3651 | ||
3652 | Like C<sv_setpv>, but also handles 'set' magic. | |
3653 | ||
3654 | =cut | |
3655 | */ | |
3656 | ||
463ee0b2 | 3657 | void |
864dbfa3 | 3658 | Perl_sv_setpv_mg(pTHX_ register SV *sv, register const char *ptr) |
ef50df4b GS |
3659 | { |
3660 | sv_setpv(sv,ptr); | |
3661 | SvSETMAGIC(sv); | |
3662 | } | |
3663 | ||
954c1994 GS |
3664 | /* |
3665 | =for apidoc sv_usepvn | |
3666 | ||
3667 | Tells an SV to use C<ptr> to find its string value. Normally the string is | |
1c846c1f | 3668 | stored inside the SV but sv_usepvn allows the SV to use an outside string. |
954c1994 GS |
3669 | The C<ptr> should point to memory that was allocated by C<malloc>. The |
3670 | string length, C<len>, must be supplied. This function will realloc the | |
3671 | memory pointed to by C<ptr>, so that pointer should not be freed or used by | |
3672 | the programmer after giving it to sv_usepvn. Does not handle 'set' magic. | |
3673 | See C<sv_usepvn_mg>. | |
3674 | ||
3675 | =cut | |
3676 | */ | |
3677 | ||
ef50df4b | 3678 | void |
864dbfa3 | 3679 | Perl_sv_usepvn(pTHX_ register SV *sv, register char *ptr, register STRLEN len) |
463ee0b2 | 3680 | { |
2213622d | 3681 | SV_CHECK_THINKFIRST(sv); |
c6f8c383 | 3682 | (void)SvUPGRADE(sv, SVt_PV); |
463ee0b2 | 3683 | if (!ptr) { |
a0d0e21e | 3684 | (void)SvOK_off(sv); |
463ee0b2 LW |
3685 | return; |
3686 | } | |
a0ed51b3 | 3687 | (void)SvOOK_off(sv); |
50483b2c | 3688 | if (SvPVX(sv) && SvLEN(sv)) |
463ee0b2 LW |
3689 | Safefree(SvPVX(sv)); |
3690 | Renew(ptr, len+1, char); | |
3691 | SvPVX(sv) = ptr; | |
3692 | SvCUR_set(sv, len); | |
3693 | SvLEN_set(sv, len+1); | |
3694 | *SvEND(sv) = '\0'; | |
1aa99e6b | 3695 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 | 3696 | SvTAINT(sv); |
79072805 LW |
3697 | } |
3698 | ||
954c1994 GS |
3699 | /* |
3700 | =for apidoc sv_usepvn_mg | |
3701 | ||
3702 | Like C<sv_usepvn>, but also handles 'set' magic. | |
3703 | ||
3704 | =cut | |
3705 | */ | |
3706 | ||
ef50df4b | 3707 | void |
864dbfa3 | 3708 | Perl_sv_usepvn_mg(pTHX_ register SV *sv, register char *ptr, register STRLEN len) |
ef50df4b | 3709 | { |
51c1089b | 3710 | sv_usepvn(sv,ptr,len); |
ef50df4b GS |
3711 | SvSETMAGIC(sv); |
3712 | } | |
3713 | ||
6fc92669 | 3714 | void |
840a7b70 | 3715 | Perl_sv_force_normal_flags(pTHX_ register SV *sv, U32 flags) |
0f15f207 | 3716 | { |
2213622d | 3717 | if (SvREADONLY(sv)) { |
1c846c1f NIS |
3718 | if (SvFAKE(sv)) { |
3719 | char *pvx = SvPVX(sv); | |
3720 | STRLEN len = SvCUR(sv); | |
3721 | U32 hash = SvUVX(sv); | |
3722 | SvGROW(sv, len + 1); | |
3723 | Move(pvx,SvPVX(sv),len,char); | |
3724 | *SvEND(sv) = '\0'; | |
3725 | SvFAKE_off(sv); | |
3726 | SvREADONLY_off(sv); | |
c3654f1a | 3727 | unsharepvn(pvx,SvUTF8(sv)?-len:len,hash); |
1c846c1f NIS |
3728 | } |
3729 | else if (PL_curcop != &PL_compiling) | |
cea2e8a9 | 3730 | Perl_croak(aTHX_ PL_no_modify); |
0f15f207 | 3731 | } |
2213622d | 3732 | if (SvROK(sv)) |
840a7b70 | 3733 | sv_unref_flags(sv, flags); |
6fc92669 GS |
3734 | else if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV) |
3735 | sv_unglob(sv); | |
0f15f207 | 3736 | } |
1c846c1f | 3737 | |
840a7b70 IZ |
3738 | void |
3739 | Perl_sv_force_normal(pTHX_ register SV *sv) | |
3740 | { | |
3741 | sv_force_normal_flags(sv, 0); | |
3742 | } | |
3743 | ||
954c1994 GS |
3744 | /* |
3745 | =for apidoc sv_chop | |
3746 | ||
1c846c1f | 3747 | Efficient removal of characters from the beginning of the string buffer. |
954c1994 GS |
3748 | SvPOK(sv) must be true and the C<ptr> must be a pointer to somewhere inside |
3749 | the string buffer. The C<ptr> becomes the first character of the adjusted | |
3750 | string. | |
3751 | ||
3752 | =cut | |
3753 | */ | |
3754 | ||
79072805 | 3755 | void |
864dbfa3 | 3756 | Perl_sv_chop(pTHX_ register SV *sv, register char *ptr) /* like set but assuming ptr is in sv */ |
1c846c1f NIS |
3757 | |
3758 | ||
79072805 LW |
3759 | { |
3760 | register STRLEN delta; | |
3761 | ||
a0d0e21e | 3762 | if (!ptr || !SvPOKp(sv)) |
79072805 | 3763 | return; |
2213622d | 3764 | SV_CHECK_THINKFIRST(sv); |
79072805 LW |
3765 | if (SvTYPE(sv) < SVt_PVIV) |
3766 | sv_upgrade(sv,SVt_PVIV); | |
3767 | ||
3768 | if (!SvOOK(sv)) { | |
50483b2c JD |
3769 | if (!SvLEN(sv)) { /* make copy of shared string */ |
3770 | char *pvx = SvPVX(sv); | |
3771 | STRLEN len = SvCUR(sv); | |
3772 | SvGROW(sv, len + 1); | |
3773 | Move(pvx,SvPVX(sv),len,char); | |
3774 | *SvEND(sv) = '\0'; | |
3775 | } | |
463ee0b2 | 3776 | SvIVX(sv) = 0; |
79072805 LW |
3777 | SvFLAGS(sv) |= SVf_OOK; |
3778 | } | |
25da4f38 | 3779 | SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVp_IOK|SVp_NOK|SVf_IVisUV); |
463ee0b2 | 3780 | delta = ptr - SvPVX(sv); |
79072805 LW |
3781 | SvLEN(sv) -= delta; |
3782 | SvCUR(sv) -= delta; | |
463ee0b2 LW |
3783 | SvPVX(sv) += delta; |
3784 | SvIVX(sv) += delta; | |
79072805 LW |
3785 | } |
3786 | ||
954c1994 GS |
3787 | /* |
3788 | =for apidoc sv_catpvn | |
3789 | ||
3790 | Concatenates the string onto the end of the string which is in the SV. The | |
3791 | C<len> indicates number of bytes to copy. Handles 'get' magic, but not | |
3792 | 'set' magic. See C<sv_catpvn_mg>. | |
3793 | ||
3794 | =cut | |
3795 | */ | |
3796 | ||
79072805 | 3797 | void |
864dbfa3 | 3798 | Perl_sv_catpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len) |
79072805 | 3799 | { |
463ee0b2 | 3800 | STRLEN tlen; |
748a9306 | 3801 | char *junk; |
a0d0e21e | 3802 | |
748a9306 | 3803 | junk = SvPV_force(sv, tlen); |
463ee0b2 | 3804 | SvGROW(sv, tlen + len + 1); |
4633a7c4 LW |
3805 | if (ptr == junk) |
3806 | ptr = SvPVX(sv); | |
463ee0b2 | 3807 | Move(ptr,SvPVX(sv)+tlen,len,char); |
79072805 LW |
3808 | SvCUR(sv) += len; |
3809 | *SvEND(sv) = '\0'; | |
d41ff1b8 | 3810 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 | 3811 | SvTAINT(sv); |
79072805 LW |
3812 | } |
3813 | ||
954c1994 GS |
3814 | /* |
3815 | =for apidoc sv_catpvn_mg | |
3816 | ||
3817 | Like C<sv_catpvn>, but also handles 'set' magic. | |
3818 | ||
3819 | =cut | |
3820 | */ | |
3821 | ||
79072805 | 3822 | void |
864dbfa3 | 3823 | Perl_sv_catpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len) |
ef50df4b GS |
3824 | { |
3825 | sv_catpvn(sv,ptr,len); | |
3826 | SvSETMAGIC(sv); | |
3827 | } | |
3828 | ||
954c1994 GS |
3829 | /* |
3830 | =for apidoc sv_catsv | |
3831 | ||
13e8c8e3 JH |
3832 | Concatenates the string from SV C<ssv> onto the end of the string in |
3833 | SV C<dsv>. Modifies C<dsv> but not C<ssv>. Handles 'get' magic, but | |
3834 | not 'set' magic. See C<sv_catsv_mg>. | |
954c1994 | 3835 | |
13e8c8e3 | 3836 | =cut */ |
954c1994 | 3837 | |
ef50df4b | 3838 | void |
46199a12 | 3839 | Perl_sv_catsv(pTHX_ SV *dsv, register SV *ssv) |
79072805 | 3840 | { |
13e8c8e3 JH |
3841 | char *spv; |
3842 | STRLEN slen; | |
46199a12 | 3843 | if (!ssv) |
79072805 | 3844 | return; |
46199a12 JH |
3845 | if ((spv = SvPV(ssv, slen))) { |
3846 | bool dutf8 = DO_UTF8(dsv); | |
3847 | bool sutf8 = DO_UTF8(ssv); | |
13e8c8e3 JH |
3848 | |
3849 | if (dutf8 == sutf8) | |
46199a12 | 3850 | sv_catpvn(dsv,spv,slen); |
13e8c8e3 JH |
3851 | else { |
3852 | if (dutf8) { | |
46199a12 JH |
3853 | /* Not modifying source SV, so taking a temporary copy. */ |
3854 | SV* csv = sv_2mortal(newSVsv(ssv)); | |
13e8c8e3 JH |
3855 | char *cpv; |
3856 | STRLEN clen; | |
3857 | ||
46199a12 JH |
3858 | sv_utf8_upgrade(csv); |
3859 | cpv = SvPV(csv,clen); | |
3860 | sv_catpvn(dsv,cpv,clen); | |
13e8c8e3 JH |
3861 | } |
3862 | else { | |
46199a12 JH |
3863 | sv_utf8_upgrade(dsv); |
3864 | sv_catpvn(dsv,spv,slen); | |
3865 | SvUTF8_on(dsv); /* If dsv has no wide characters. */ | |
13e8c8e3 | 3866 | } |
e84ff256 | 3867 | } |
560a288e | 3868 | } |
79072805 LW |
3869 | } |
3870 | ||
954c1994 GS |
3871 | /* |
3872 | =for apidoc sv_catsv_mg | |
3873 | ||
3874 | Like C<sv_catsv>, but also handles 'set' magic. | |
3875 | ||
3876 | =cut | |
3877 | */ | |
3878 | ||
79072805 | 3879 | void |
46199a12 | 3880 | Perl_sv_catsv_mg(pTHX_ SV *dsv, register SV *ssv) |
ef50df4b | 3881 | { |
46199a12 JH |
3882 | sv_catsv(dsv,ssv); |
3883 | SvSETMAGIC(dsv); | |
ef50df4b GS |
3884 | } |
3885 | ||
954c1994 GS |
3886 | /* |
3887 | =for apidoc sv_catpv | |
3888 | ||
3889 | Concatenates the string onto the end of the string which is in the SV. | |
3890 | Handles 'get' magic, but not 'set' magic. See C<sv_catpv_mg>. | |
3891 | ||
3892 | =cut | |
3893 | */ | |
3894 | ||
ef50df4b | 3895 | void |
0c981600 | 3896 | Perl_sv_catpv(pTHX_ register SV *sv, register const char *ptr) |
79072805 LW |
3897 | { |
3898 | register STRLEN len; | |
463ee0b2 | 3899 | STRLEN tlen; |
748a9306 | 3900 | char *junk; |
79072805 | 3901 | |
0c981600 | 3902 | if (!ptr) |
79072805 | 3903 | return; |
748a9306 | 3904 | junk = SvPV_force(sv, tlen); |
0c981600 | 3905 | len = strlen(ptr); |
463ee0b2 | 3906 | SvGROW(sv, tlen + len + 1); |
0c981600 JH |
3907 | if (ptr == junk) |
3908 | ptr = SvPVX(sv); | |
3909 | Move(ptr,SvPVX(sv)+tlen,len+1,char); | |
79072805 | 3910 | SvCUR(sv) += len; |
d41ff1b8 | 3911 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 | 3912 | SvTAINT(sv); |
79072805 LW |
3913 | } |
3914 | ||
954c1994 GS |
3915 | /* |
3916 | =for apidoc sv_catpv_mg | |
3917 | ||
3918 | Like C<sv_catpv>, but also handles 'set' magic. | |
3919 | ||
3920 | =cut | |
3921 | */ | |
3922 | ||
ef50df4b | 3923 | void |
0c981600 | 3924 | Perl_sv_catpv_mg(pTHX_ register SV *sv, register const char *ptr) |
ef50df4b | 3925 | { |
0c981600 | 3926 | sv_catpv(sv,ptr); |
ef50df4b GS |
3927 | SvSETMAGIC(sv); |
3928 | } | |
3929 | ||
79072805 | 3930 | SV * |
864dbfa3 | 3931 | Perl_newSV(pTHX_ STRLEN len) |
79072805 LW |
3932 | { |
3933 | register SV *sv; | |
1c846c1f | 3934 | |
4561caa4 | 3935 | new_SV(sv); |
79072805 LW |
3936 | if (len) { |
3937 | sv_upgrade(sv, SVt_PV); | |
3938 | SvGROW(sv, len + 1); | |
3939 | } | |
3940 | return sv; | |
3941 | } | |
3942 | ||
1edc1566 | 3943 | /* name is assumed to contain an SV* if (name && namelen == HEf_SVKEY) */ |
3944 | ||
954c1994 GS |
3945 | /* |
3946 | =for apidoc sv_magic | |
3947 | ||
3948 | Adds magic to an SV. | |
3949 | ||
3950 | =cut | |
3951 | */ | |
3952 | ||
79072805 | 3953 | void |
864dbfa3 | 3954 | Perl_sv_magic(pTHX_ register SV *sv, SV *obj, int how, const char *name, I32 namlen) |
79072805 LW |
3955 | { |
3956 | MAGIC* mg; | |
1c846c1f | 3957 | |
0f15f207 | 3958 | if (SvREADONLY(sv)) { |
3280af22 | 3959 | if (PL_curcop != &PL_compiling && !strchr("gBf", how)) |
cea2e8a9 | 3960 | Perl_croak(aTHX_ PL_no_modify); |
0f15f207 | 3961 | } |
4633a7c4 | 3962 | if (SvMAGICAL(sv) || (how == 't' && SvTYPE(sv) >= SVt_PVMG)) { |
748a9306 LW |
3963 | if (SvMAGIC(sv) && (mg = mg_find(sv, how))) { |
3964 | if (how == 't') | |
565764a8 | 3965 | mg->mg_len |= 1; |
463ee0b2 | 3966 | return; |
748a9306 | 3967 | } |
463ee0b2 LW |
3968 | } |
3969 | else { | |
c6f8c383 | 3970 | (void)SvUPGRADE(sv, SVt_PVMG); |
463ee0b2 | 3971 | } |
79072805 LW |
3972 | Newz(702,mg, 1, MAGIC); |
3973 | mg->mg_moremagic = SvMAGIC(sv); | |
79072805 | 3974 | SvMAGIC(sv) = mg; |
75f9d97a JH |
3975 | |
3976 | /* Some magic sontains a reference loop, where the sv and object refer to | |
3977 | each other. To prevent a avoid a reference loop that would prevent such | |
3978 | objects being freed, we look for such loops and if we find one we avoid | |
3979 | incrementing the object refcount. */ | |
3980 | if (!obj || obj == sv || how == '#' || how == 'r' || | |
3981 | (SvTYPE(obj) == SVt_PVGV && | |
3982 | (GvSV(obj) == sv || GvHV(obj) == (HV*)sv || GvAV(obj) == (AV*)sv || | |
3983 | GvCV(obj) == (CV*)sv || GvIOp(obj) == (IO*)sv || | |
3984 | GvFORM(obj) == (CV*)sv))) | |
3985 | { | |
8990e307 | 3986 | mg->mg_obj = obj; |
75f9d97a | 3987 | } |
85e6fe83 | 3988 | else { |
8990e307 | 3989 | mg->mg_obj = SvREFCNT_inc(obj); |
85e6fe83 LW |
3990 | mg->mg_flags |= MGf_REFCOUNTED; |
3991 | } | |
79072805 | 3992 | mg->mg_type = how; |
565764a8 | 3993 | mg->mg_len = namlen; |
9cbac4c7 | 3994 | if (name) { |
1edc1566 | 3995 | if (namlen >= 0) |
3996 | mg->mg_ptr = savepvn(name, namlen); | |
c6ee37c5 | 3997 | else if (namlen == HEf_SVKEY) |
1edc1566 | 3998 | mg->mg_ptr = (char*)SvREFCNT_inc((SV*)name); |
9cbac4c7 | 3999 | } |
1c846c1f | 4000 | |
79072805 LW |
4001 | switch (how) { |
4002 | case 0: | |
22c35a8c | 4003 | mg->mg_virtual = &PL_vtbl_sv; |
79072805 | 4004 | break; |
a0d0e21e | 4005 | case 'A': |
22c35a8c | 4006 | mg->mg_virtual = &PL_vtbl_amagic; |
a0d0e21e LW |
4007 | break; |
4008 | case 'a': | |
22c35a8c | 4009 | mg->mg_virtual = &PL_vtbl_amagicelem; |
a0d0e21e LW |
4010 | break; |
4011 | case 'c': | |
d460ef45 | 4012 | mg->mg_virtual = &PL_vtbl_ovrld; |
a0d0e21e | 4013 | break; |
79072805 | 4014 | case 'B': |
22c35a8c | 4015 | mg->mg_virtual = &PL_vtbl_bm; |
79072805 | 4016 | break; |
6cef1e77 | 4017 | case 'D': |
22c35a8c | 4018 | mg->mg_virtual = &PL_vtbl_regdata; |
6cef1e77 IZ |
4019 | break; |
4020 | case 'd': | |
22c35a8c | 4021 | mg->mg_virtual = &PL_vtbl_regdatum; |
6cef1e77 | 4022 | break; |
79072805 | 4023 | case 'E': |
22c35a8c | 4024 | mg->mg_virtual = &PL_vtbl_env; |
79072805 | 4025 | break; |
55497cff | 4026 | case 'f': |
22c35a8c | 4027 | mg->mg_virtual = &PL_vtbl_fm; |
55497cff | 4028 | break; |
79072805 | 4029 | case 'e': |
22c35a8c | 4030 | mg->mg_virtual = &PL_vtbl_envelem; |
79072805 | 4031 | break; |
93a17b20 | 4032 | case 'g': |
22c35a8c | 4033 | mg->mg_virtual = &PL_vtbl_mglob; |
93a17b20 | 4034 | break; |
463ee0b2 | 4035 | case 'I': |
22c35a8c | 4036 | mg->mg_virtual = &PL_vtbl_isa; |
463ee0b2 LW |
4037 | break; |
4038 | case 'i': | |
22c35a8c | 4039 | mg->mg_virtual = &PL_vtbl_isaelem; |
463ee0b2 | 4040 | break; |
16660edb | 4041 | case 'k': |
22c35a8c | 4042 | mg->mg_virtual = &PL_vtbl_nkeys; |
16660edb | 4043 | break; |
79072805 | 4044 | case 'L': |
a0d0e21e | 4045 | SvRMAGICAL_on(sv); |
93a17b20 LW |
4046 | mg->mg_virtual = 0; |
4047 | break; | |
4048 | case 'l': | |
22c35a8c | 4049 | mg->mg_virtual = &PL_vtbl_dbline; |
79072805 | 4050 | break; |
f93b4edd MB |
4051 | #ifdef USE_THREADS |
4052 | case 'm': | |
22c35a8c | 4053 | mg->mg_virtual = &PL_vtbl_mutex; |
f93b4edd MB |
4054 | break; |
4055 | #endif /* USE_THREADS */ | |
36477c24 | 4056 | #ifdef USE_LOCALE_COLLATE |
bbce6d69 | 4057 | case 'o': |
22c35a8c | 4058 | mg->mg_virtual = &PL_vtbl_collxfrm; |
bbce6d69 | 4059 | break; |
36477c24 | 4060 | #endif /* USE_LOCALE_COLLATE */ |
463ee0b2 | 4061 | case 'P': |
22c35a8c | 4062 | mg->mg_virtual = &PL_vtbl_pack; |
463ee0b2 LW |
4063 | break; |
4064 | case 'p': | |
a0d0e21e | 4065 | case 'q': |
22c35a8c | 4066 | mg->mg_virtual = &PL_vtbl_packelem; |
463ee0b2 | 4067 | break; |
c277df42 | 4068 | case 'r': |
22c35a8c | 4069 | mg->mg_virtual = &PL_vtbl_regexp; |
c277df42 | 4070 | break; |
79072805 | 4071 | case 'S': |
22c35a8c | 4072 | mg->mg_virtual = &PL_vtbl_sig; |
79072805 LW |
4073 | break; |
4074 | case 's': | |
22c35a8c | 4075 | mg->mg_virtual = &PL_vtbl_sigelem; |
79072805 | 4076 | break; |
463ee0b2 | 4077 | case 't': |
22c35a8c | 4078 | mg->mg_virtual = &PL_vtbl_taint; |
565764a8 | 4079 | mg->mg_len = 1; |
463ee0b2 | 4080 | break; |
79072805 | 4081 | case 'U': |
22c35a8c | 4082 | mg->mg_virtual = &PL_vtbl_uvar; |
79072805 LW |
4083 | break; |
4084 | case 'v': | |
22c35a8c | 4085 | mg->mg_virtual = &PL_vtbl_vec; |
79072805 LW |
4086 | break; |
4087 | case 'x': | |
22c35a8c | 4088 | mg->mg_virtual = &PL_vtbl_substr; |
79072805 | 4089 | break; |
5f05dabc | 4090 | case 'y': |
22c35a8c | 4091 | mg->mg_virtual = &PL_vtbl_defelem; |
5f05dabc | 4092 | break; |
79072805 | 4093 | case '*': |
22c35a8c | 4094 | mg->mg_virtual = &PL_vtbl_glob; |
79072805 LW |
4095 | break; |
4096 | case '#': | |
22c35a8c | 4097 | mg->mg_virtual = &PL_vtbl_arylen; |
79072805 | 4098 | break; |
a0d0e21e | 4099 | case '.': |
22c35a8c | 4100 | mg->mg_virtual = &PL_vtbl_pos; |
a0d0e21e | 4101 | break; |
810b8aa5 GS |
4102 | case '<': |
4103 | mg->mg_virtual = &PL_vtbl_backref; | |
4104 | break; | |
4633a7c4 LW |
4105 | case '~': /* Reserved for use by extensions not perl internals. */ |
4106 | /* Useful for attaching extension internal data to perl vars. */ | |
4107 | /* Note that multiple extensions may clash if magical scalars */ | |
4108 | /* etc holding private data from one are passed to another. */ | |
4109 | SvRMAGICAL_on(sv); | |
a0d0e21e | 4110 | break; |
79072805 | 4111 | default: |
cea2e8a9 | 4112 | Perl_croak(aTHX_ "Don't know how to handle magic of type '%c'", how); |
463ee0b2 | 4113 | } |
8990e307 LW |
4114 | mg_magical(sv); |
4115 | if (SvGMAGICAL(sv)) | |
4116 | SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK); | |
463ee0b2 LW |
4117 | } |
4118 | ||
c461cf8f JH |
4119 | /* |
4120 | =for apidoc sv_unmagic | |
4121 | ||
4122 | Removes magic from an SV. | |
4123 | ||
4124 | =cut | |
4125 | */ | |
4126 | ||
463ee0b2 | 4127 | int |
864dbfa3 | 4128 | Perl_sv_unmagic(pTHX_ SV *sv, int type) |
463ee0b2 LW |
4129 | { |
4130 | MAGIC* mg; | |
4131 | MAGIC** mgp; | |
91bba347 | 4132 | if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv)) |
463ee0b2 LW |
4133 | return 0; |
4134 | mgp = &SvMAGIC(sv); | |
4135 | for (mg = *mgp; mg; mg = *mgp) { | |
4136 | if (mg->mg_type == type) { | |
4137 | MGVTBL* vtbl = mg->mg_virtual; | |
4138 | *mgp = mg->mg_moremagic; | |
1d7c1841 | 4139 | if (vtbl && vtbl->svt_free) |
fc0dc3b3 | 4140 | CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg); |
9cbac4c7 | 4141 | if (mg->mg_ptr && mg->mg_type != 'g') { |
565764a8 | 4142 | if (mg->mg_len >= 0) |
1edc1566 | 4143 | Safefree(mg->mg_ptr); |
565764a8 | 4144 | else if (mg->mg_len == HEf_SVKEY) |
1edc1566 | 4145 | SvREFCNT_dec((SV*)mg->mg_ptr); |
9cbac4c7 | 4146 | } |
a0d0e21e LW |
4147 | if (mg->mg_flags & MGf_REFCOUNTED) |
4148 | SvREFCNT_dec(mg->mg_obj); | |
463ee0b2 LW |
4149 | Safefree(mg); |
4150 | } | |
4151 | else | |
4152 | mgp = &mg->mg_moremagic; | |
79072805 | 4153 | } |
91bba347 | 4154 | if (!SvMAGIC(sv)) { |
463ee0b2 | 4155 | SvMAGICAL_off(sv); |
8990e307 | 4156 | SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT; |
463ee0b2 LW |
4157 | } |
4158 | ||
4159 | return 0; | |
79072805 LW |
4160 | } |
4161 | ||
c461cf8f JH |
4162 | /* |
4163 | =for apidoc sv_rvweaken | |
4164 | ||
4165 | Weaken a reference. | |
4166 | ||
4167 | =cut | |
4168 | */ | |
4169 | ||
810b8aa5 | 4170 | SV * |
864dbfa3 | 4171 | Perl_sv_rvweaken(pTHX_ SV *sv) |
810b8aa5 GS |
4172 | { |
4173 | SV *tsv; | |
4174 | if (!SvOK(sv)) /* let undefs pass */ | |
4175 | return sv; | |
4176 | if (!SvROK(sv)) | |
cea2e8a9 | 4177 | Perl_croak(aTHX_ "Can't weaken a nonreference"); |
810b8aa5 | 4178 | else if (SvWEAKREF(sv)) { |
810b8aa5 | 4179 | if (ckWARN(WARN_MISC)) |
cea2e8a9 | 4180 | Perl_warner(aTHX_ WARN_MISC, "Reference is already weak"); |
810b8aa5 GS |
4181 | return sv; |
4182 | } | |
4183 | tsv = SvRV(sv); | |
4184 | sv_add_backref(tsv, sv); | |
4185 | SvWEAKREF_on(sv); | |
1c846c1f | 4186 | SvREFCNT_dec(tsv); |
810b8aa5 GS |
4187 | return sv; |
4188 | } | |
4189 | ||
4190 | STATIC void | |
cea2e8a9 | 4191 | S_sv_add_backref(pTHX_ SV *tsv, SV *sv) |
810b8aa5 GS |
4192 | { |
4193 | AV *av; | |
4194 | MAGIC *mg; | |
4195 | if (SvMAGICAL(tsv) && (mg = mg_find(tsv, '<'))) | |
4196 | av = (AV*)mg->mg_obj; | |
4197 | else { | |
4198 | av = newAV(); | |
4199 | sv_magic(tsv, (SV*)av, '<', NULL, 0); | |
4200 | SvREFCNT_dec(av); /* for sv_magic */ | |
4201 | } | |
4202 | av_push(av,sv); | |
4203 | } | |
4204 | ||
1c846c1f | 4205 | STATIC void |
cea2e8a9 | 4206 | S_sv_del_backref(pTHX_ SV *sv) |
810b8aa5 GS |
4207 | { |
4208 | AV *av; | |
4209 | SV **svp; | |
4210 | I32 i; | |
4211 | SV *tsv = SvRV(sv); | |
4212 | MAGIC *mg; | |
4213 | if (!SvMAGICAL(tsv) || !(mg = mg_find(tsv, '<'))) | |
cea2e8a9 | 4214 | Perl_croak(aTHX_ "panic: del_backref"); |
810b8aa5 GS |
4215 | av = (AV *)mg->mg_obj; |
4216 | svp = AvARRAY(av); | |
4217 | i = AvFILLp(av); | |
4218 | while (i >= 0) { | |
4219 | if (svp[i] == sv) { | |
4220 | svp[i] = &PL_sv_undef; /* XXX */ | |
4221 | } | |
4222 | i--; | |
4223 | } | |
4224 | } | |
4225 | ||
954c1994 GS |
4226 | /* |
4227 | =for apidoc sv_insert | |
4228 | ||
4229 | Inserts a string at the specified offset/length within the SV. Similar to | |
4230 | the Perl substr() function. | |
4231 | ||
4232 | =cut | |
4233 | */ | |
4234 | ||
79072805 | 4235 | void |
864dbfa3 | 4236 | Perl_sv_insert(pTHX_ SV *bigstr, STRLEN offset, STRLEN len, char *little, STRLEN littlelen) |
79072805 LW |
4237 | { |
4238 | register char *big; | |
4239 | register char *mid; | |
4240 | register char *midend; | |
4241 | register char *bigend; | |
4242 | register I32 i; | |
6ff81951 | 4243 | STRLEN curlen; |
1c846c1f | 4244 | |
79072805 | 4245 | |
8990e307 | 4246 | if (!bigstr) |
cea2e8a9 | 4247 | Perl_croak(aTHX_ "Can't modify non-existent substring"); |
6ff81951 | 4248 | SvPV_force(bigstr, curlen); |
60fa28ff | 4249 | (void)SvPOK_only_UTF8(bigstr); |
6ff81951 GS |
4250 | if (offset + len > curlen) { |
4251 | SvGROW(bigstr, offset+len+1); | |
4252 | Zero(SvPVX(bigstr)+curlen, offset+len-curlen, char); | |
4253 | SvCUR_set(bigstr, offset+len); | |
4254 | } | |
79072805 | 4255 | |
69b47968 | 4256 | SvTAINT(bigstr); |
79072805 LW |
4257 | i = littlelen - len; |
4258 | if (i > 0) { /* string might grow */ | |
a0d0e21e | 4259 | big = SvGROW(bigstr, SvCUR(bigstr) + i + 1); |
79072805 LW |
4260 | mid = big + offset + len; |
4261 | midend = bigend = big + SvCUR(bigstr); | |
4262 | bigend += i; | |
4263 | *bigend = '\0'; | |
4264 | while (midend > mid) /* shove everything down */ | |
4265 | *--bigend = *--midend; | |
4266 | Move(little,big+offset,littlelen,char); | |
4267 | SvCUR(bigstr) += i; | |
4268 | SvSETMAGIC(bigstr); | |
4269 | return; | |
4270 | } | |
4271 | else if (i == 0) { | |
463ee0b2 | 4272 | Move(little,SvPVX(bigstr)+offset,len,char); |
79072805 LW |
4273 | SvSETMAGIC(bigstr); |
4274 | return; | |
4275 | } | |
4276 | ||
463ee0b2 | 4277 | big = SvPVX(bigstr); |
79072805 LW |
4278 | mid = big + offset; |
4279 | midend = mid + len; | |
4280 | bigend = big + SvCUR(bigstr); | |
4281 | ||
4282 | if (midend > bigend) | |
cea2e8a9 | 4283 | Perl_croak(aTHX_ "panic: sv_insert"); |
79072805 LW |
4284 | |
4285 | if (mid - big > bigend - midend) { /* faster to shorten from end */ | |
4286 | if (littlelen) { | |
4287 | Move(little, mid, littlelen,char); | |
4288 | mid += littlelen; | |
4289 | } | |
4290 | i = bigend - midend; | |
4291 | if (i > 0) { | |
4292 | Move(midend, mid, i,char); | |
4293 | mid += i; | |
4294 | } | |
4295 | *mid = '\0'; | |
4296 | SvCUR_set(bigstr, mid - big); | |
4297 | } | |
4298 | /*SUPPRESS 560*/ | |
155aba94 | 4299 | else if ((i = mid - big)) { /* faster from front */ |
79072805 LW |
4300 | midend -= littlelen; |
4301 | mid = midend; | |
4302 | sv_chop(bigstr,midend-i); | |
4303 | big += i; | |
4304 | while (i--) | |
4305 | *--midend = *--big; | |
4306 | if (littlelen) | |
4307 | Move(little, mid, littlelen,char); | |
4308 | } | |
4309 | else if (littlelen) { | |
4310 | midend -= littlelen; | |
4311 | sv_chop(bigstr,midend); | |
4312 | Move(little,midend,littlelen,char); | |
4313 | } | |
4314 | else { | |
4315 | sv_chop(bigstr,midend); | |
4316 | } | |
4317 | SvSETMAGIC(bigstr); | |
4318 | } | |
4319 | ||
c461cf8f JH |
4320 | /* |
4321 | =for apidoc sv_replace | |
4322 | ||
4323 | Make the first argument a copy of the second, then delete the original. | |
4324 | ||
4325 | =cut | |
4326 | */ | |
79072805 LW |
4327 | |
4328 | void | |
864dbfa3 | 4329 | Perl_sv_replace(pTHX_ register SV *sv, register SV *nsv) |
79072805 LW |
4330 | { |
4331 | U32 refcnt = SvREFCNT(sv); | |
2213622d | 4332 | SV_CHECK_THINKFIRST(sv); |
0453d815 PM |
4333 | if (SvREFCNT(nsv) != 1 && ckWARN_d(WARN_INTERNAL)) |
4334 | Perl_warner(aTHX_ WARN_INTERNAL, "Reference miscount in sv_replace()"); | |
93a17b20 | 4335 | if (SvMAGICAL(sv)) { |
a0d0e21e LW |
4336 | if (SvMAGICAL(nsv)) |
4337 | mg_free(nsv); | |
4338 | else | |
4339 | sv_upgrade(nsv, SVt_PVMG); | |
93a17b20 | 4340 | SvMAGIC(nsv) = SvMAGIC(sv); |
a0d0e21e | 4341 | SvFLAGS(nsv) |= SvMAGICAL(sv); |
93a17b20 LW |
4342 | SvMAGICAL_off(sv); |
4343 | SvMAGIC(sv) = 0; | |
4344 | } | |
79072805 LW |
4345 | SvREFCNT(sv) = 0; |
4346 | sv_clear(sv); | |
477f5d66 | 4347 | assert(!SvREFCNT(sv)); |
79072805 LW |
4348 | StructCopy(nsv,sv,SV); |
4349 | SvREFCNT(sv) = refcnt; | |
1edc1566 | 4350 | SvFLAGS(nsv) |= SVTYPEMASK; /* Mark as freed */ |
463ee0b2 | 4351 | del_SV(nsv); |
79072805 LW |
4352 | } |
4353 | ||
c461cf8f JH |
4354 | /* |
4355 | =for apidoc sv_clear | |
4356 | ||
4357 | Clear an SV, making it empty. Does not free the memory used by the SV | |
4358 | itself. | |
4359 | ||
4360 | =cut | |
4361 | */ | |
4362 | ||
79072805 | 4363 | void |
864dbfa3 | 4364 | Perl_sv_clear(pTHX_ register SV *sv) |
79072805 | 4365 | { |
ec12f114 | 4366 | HV* stash; |
79072805 LW |
4367 | assert(sv); |
4368 | assert(SvREFCNT(sv) == 0); | |
4369 | ||
ed6116ce | 4370 | if (SvOBJECT(sv)) { |
3280af22 | 4371 | if (PL_defstash) { /* Still have a symbol table? */ |
4e35701f | 4372 | djSP; |
32251b26 | 4373 | CV* destructor; |
837485b6 | 4374 | SV tmpref; |
a0d0e21e | 4375 | |
837485b6 GS |
4376 | Zero(&tmpref, 1, SV); |
4377 | sv_upgrade(&tmpref, SVt_RV); | |
4378 | SvROK_on(&tmpref); | |
4379 | SvREADONLY_on(&tmpref); /* DESTROY() could be naughty */ | |
4380 | SvREFCNT(&tmpref) = 1; | |
8ebc5c01 | 4381 | |
d460ef45 | 4382 | do { |
4e8e7886 | 4383 | stash = SvSTASH(sv); |
32251b26 | 4384 | destructor = StashHANDLER(stash,DESTROY); |
4e8e7886 GS |
4385 | if (destructor) { |
4386 | ENTER; | |
e788e7d3 | 4387 | PUSHSTACKi(PERLSI_DESTROY); |
837485b6 | 4388 | SvRV(&tmpref) = SvREFCNT_inc(sv); |
4e8e7886 GS |
4389 | EXTEND(SP, 2); |
4390 | PUSHMARK(SP); | |
837485b6 | 4391 | PUSHs(&tmpref); |
4e8e7886 | 4392 | PUTBACK; |
32251b26 | 4393 | call_sv((SV*)destructor, G_DISCARD|G_EVAL|G_KEEPERR); |
4e8e7886 | 4394 | SvREFCNT(sv)--; |
d3acc0f7 | 4395 | POPSTACK; |
3095d977 | 4396 | SPAGAIN; |
4e8e7886 GS |
4397 | LEAVE; |
4398 | } | |
4399 | } while (SvOBJECT(sv) && SvSTASH(sv) != stash); | |
8ebc5c01 | 4400 | |
837485b6 | 4401 | del_XRV(SvANY(&tmpref)); |
6f44e0a4 JP |
4402 | |
4403 | if (SvREFCNT(sv)) { | |
4404 | if (PL_in_clean_objs) | |
cea2e8a9 | 4405 | Perl_croak(aTHX_ "DESTROY created new reference to dead object '%s'", |
6f44e0a4 JP |
4406 | HvNAME(stash)); |
4407 | /* DESTROY gave object new lease on life */ | |
4408 | return; | |
4409 | } | |
a0d0e21e | 4410 | } |
4e8e7886 | 4411 | |
a0d0e21e | 4412 | if (SvOBJECT(sv)) { |
4e8e7886 | 4413 | SvREFCNT_dec(SvSTASH(sv)); /* possibly of changed persuasion */ |
a0d0e21e LW |
4414 | SvOBJECT_off(sv); /* Curse the object. */ |
4415 | if (SvTYPE(sv) != SVt_PVIO) | |
3280af22 | 4416 | --PL_sv_objcount; /* XXX Might want something more general */ |
a0d0e21e | 4417 | } |
463ee0b2 | 4418 | } |
c07a80fd | 4419 | if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) |
a0d0e21e | 4420 | mg_free(sv); |
ec12f114 | 4421 | stash = NULL; |
79072805 | 4422 | switch (SvTYPE(sv)) { |
8990e307 | 4423 | case SVt_PVIO: |
df0bd2f4 GS |
4424 | if (IoIFP(sv) && |
4425 | IoIFP(sv) != PerlIO_stdin() && | |
5f05dabc | 4426 | IoIFP(sv) != PerlIO_stdout() && |
4427 | IoIFP(sv) != PerlIO_stderr()) | |
93578b34 | 4428 | { |
f2b5be74 | 4429 | io_close((IO*)sv, FALSE); |
93578b34 | 4430 | } |
1d7c1841 | 4431 | if (IoDIRP(sv) && !(IoFLAGS(sv) & IOf_FAKE_DIRP)) |
1236053a | 4432 | PerlDir_close(IoDIRP(sv)); |
1d7c1841 | 4433 | IoDIRP(sv) = (DIR*)NULL; |
8990e307 LW |
4434 | Safefree(IoTOP_NAME(sv)); |
4435 | Safefree(IoFMT_NAME(sv)); | |
4436 | Safefree(IoBOTTOM_NAME(sv)); | |
a0d0e21e | 4437 | /* FALL THROUGH */ |
79072805 | 4438 | case SVt_PVBM: |
a0d0e21e | 4439 | goto freescalar; |
79072805 | 4440 | case SVt_PVCV: |
748a9306 | 4441 | case SVt_PVFM: |
85e6fe83 | 4442 | cv_undef((CV*)sv); |
a0d0e21e | 4443 | goto freescalar; |
79072805 | 4444 | case SVt_PVHV: |
85e6fe83 | 4445 | hv_undef((HV*)sv); |
a0d0e21e | 4446 | break; |
79072805 | 4447 | case SVt_PVAV: |
85e6fe83 | 4448 | av_undef((AV*)sv); |
a0d0e21e | 4449 | break; |
02270b4e GS |
4450 | case SVt_PVLV: |
4451 | SvREFCNT_dec(LvTARG(sv)); | |
4452 | goto freescalar; | |
a0d0e21e | 4453 | case SVt_PVGV: |
1edc1566 | 4454 | gp_free((GV*)sv); |
a0d0e21e | 4455 | Safefree(GvNAME(sv)); |
ec12f114 JPC |
4456 | /* cannot decrease stash refcount yet, as we might recursively delete |
4457 | ourselves when the refcnt drops to zero. Delay SvREFCNT_dec | |
4458 | of stash until current sv is completely gone. | |
4459 | -- JohnPC, 27 Mar 1998 */ | |
4460 | stash = GvSTASH(sv); | |
a0d0e21e | 4461 | /* FALL THROUGH */ |
79072805 | 4462 | case SVt_PVMG: |
79072805 LW |
4463 | case SVt_PVNV: |
4464 | case SVt_PVIV: | |
a0d0e21e LW |
4465 | freescalar: |
4466 | (void)SvOOK_off(sv); | |
79072805 LW |
4467 | /* FALL THROUGH */ |
4468 | case SVt_PV: | |
a0d0e21e | 4469 | case SVt_RV: |
810b8aa5 GS |
4470 | if (SvROK(sv)) { |
4471 | if (SvWEAKREF(sv)) | |
4472 | sv_del_backref(sv); | |
4473 | else | |
4474 | SvREFCNT_dec(SvRV(sv)); | |
4475 | } | |
1edc1566 | 4476 | else if (SvPVX(sv) && SvLEN(sv)) |
463ee0b2 | 4477 | Safefree(SvPVX(sv)); |
1c846c1f | 4478 | else if (SvPVX(sv) && SvREADONLY(sv) && SvFAKE(sv)) { |
c3654f1a | 4479 | unsharepvn(SvPVX(sv),SvUTF8(sv)?-SvCUR(sv):SvCUR(sv),SvUVX(sv)); |
1c846c1f NIS |
4480 | SvFAKE_off(sv); |
4481 | } | |
79072805 | 4482 | break; |
a0d0e21e | 4483 | /* |
79072805 | 4484 | case SVt_NV: |
79072805 | 4485 | case SVt_IV: |
79072805 LW |
4486 | case SVt_NULL: |
4487 | break; | |
a0d0e21e | 4488 | */ |
79072805 LW |
4489 | } |
4490 | ||
4491 | switch (SvTYPE(sv)) { | |
4492 | case SVt_NULL: | |
4493 | break; | |
79072805 LW |
4494 | case SVt_IV: |
4495 | del_XIV(SvANY(sv)); | |
4496 | break; | |
4497 | case SVt_NV: | |
4498 | del_XNV(SvANY(sv)); | |
4499 | break; | |
ed6116ce LW |
4500 | case SVt_RV: |
4501 | del_XRV(SvANY(sv)); | |
4502 | break; | |
79072805 LW |
4503 | case SVt_PV: |
4504 | del_XPV(SvANY(sv)); | |
4505 | break; | |
4506 | case SVt_PVIV: | |
4507 | del_XPVIV(SvANY(sv)); | |
4508 | break; | |
4509 | case SVt_PVNV: | |
4510 | del_XPVNV(SvANY(sv)); | |
4511 | break; | |
4512 | case SVt_PVMG: | |
4513 | del_XPVMG(SvANY(sv)); | |
4514 | break; | |
4515 | case SVt_PVLV: | |
4516 | del_XPVLV(SvANY(sv)); | |
4517 | break; | |
4518 | case SVt_PVAV: | |
4519 | del_XPVAV(SvANY(sv)); | |
4520 | break; | |
4521 | case SVt_PVHV: | |
4522 | del_XPVHV(SvANY(sv)); | |
4523 | break; | |
4524 | case SVt_PVCV: | |
4525 | del_XPVCV(SvANY(sv)); | |
4526 | break; | |
4527 | case SVt_PVGV: | |
4528 | del_XPVGV(SvANY(sv)); | |
ec12f114 JPC |
4529 | /* code duplication for increased performance. */ |
4530 | SvFLAGS(sv) &= SVf_BREAK; | |
4531 | SvFLAGS(sv) |= SVTYPEMASK; | |
4532 | /* decrease refcount of the stash that owns this GV, if any */ | |
4533 | if (stash) | |
4534 | SvREFCNT_dec(stash); | |
4535 | return; /* not break, SvFLAGS reset already happened */ | |
79072805 LW |
4536 | case SVt_PVBM: |
4537 | del_XPVBM(SvANY(sv)); | |
4538 | break; | |
4539 | case SVt_PVFM: | |
4540 | del_XPVFM(SvANY(sv)); | |
4541 | break; | |
8990e307 LW |
4542 | case SVt_PVIO: |
4543 | del_XPVIO(SvANY(sv)); | |
4544 | break; | |
79072805 | 4545 | } |
a0d0e21e | 4546 | SvFLAGS(sv) &= SVf_BREAK; |
8990e307 | 4547 | SvFLAGS(sv) |= SVTYPEMASK; |
79072805 LW |
4548 | } |
4549 | ||
4550 | SV * | |
864dbfa3 | 4551 | Perl_sv_newref(pTHX_ SV *sv) |
79072805 | 4552 | { |
463ee0b2 | 4553 | if (sv) |
dce16143 | 4554 | ATOMIC_INC(SvREFCNT(sv)); |
79072805 LW |
4555 | return sv; |
4556 | } | |
4557 | ||
c461cf8f JH |
4558 | /* |
4559 | =for apidoc sv_free | |
4560 | ||
4561 | Free the memory used by an SV. | |
4562 | ||
4563 | =cut | |
4564 | */ | |
4565 | ||
79072805 | 4566 | void |
864dbfa3 | 4567 | Perl_sv_free(pTHX_ SV *sv) |
79072805 | 4568 | { |
dce16143 MB |
4569 | int refcount_is_zero; |
4570 | ||
79072805 LW |
4571 | if (!sv) |
4572 | return; | |
a0d0e21e LW |
4573 | if (SvREFCNT(sv) == 0) { |
4574 | if (SvFLAGS(sv) & SVf_BREAK) | |
4575 | return; | |
3280af22 | 4576 | if (PL_in_clean_all) /* All is fair */ |
1edc1566 | 4577 | return; |
d689ffdd JP |
4578 | if (SvREADONLY(sv) && SvIMMORTAL(sv)) { |
4579 | /* make sure SvREFCNT(sv)==0 happens very seldom */ | |
4580 | SvREFCNT(sv) = (~(U32)0)/2; | |
4581 | return; | |
4582 | } | |
0453d815 PM |
4583 | if (ckWARN_d(WARN_INTERNAL)) |
4584 | Perl_warner(aTHX_ WARN_INTERNAL, "Attempt to free unreferenced scalar"); | |
79072805 LW |
4585 | return; |
4586 | } | |
dce16143 MB |
4587 | ATOMIC_DEC_AND_TEST(refcount_is_zero, SvREFCNT(sv)); |
4588 | if (!refcount_is_zero) | |
8990e307 | 4589 | return; |
463ee0b2 LW |
4590 | #ifdef DEBUGGING |
4591 | if (SvTEMP(sv)) { | |
0453d815 | 4592 | if (ckWARN_d(WARN_DEBUGGING)) |
f248d071 | 4593 | Perl_warner(aTHX_ WARN_DEBUGGING, |
1d7c1841 GS |
4594 | "Attempt to free temp prematurely: SV 0x%"UVxf, |
4595 | PTR2UV(sv)); | |
79072805 | 4596 | return; |
79072805 | 4597 | } |
463ee0b2 | 4598 | #endif |
d689ffdd JP |
4599 | if (SvREADONLY(sv) && SvIMMORTAL(sv)) { |
4600 | /* make sure SvREFCNT(sv)==0 happens very seldom */ | |
4601 | SvREFCNT(sv) = (~(U32)0)/2; | |
4602 | return; | |
4603 | } | |
79072805 | 4604 | sv_clear(sv); |
477f5d66 CS |
4605 | if (! SvREFCNT(sv)) |
4606 | del_SV(sv); | |
79072805 LW |
4607 | } |
4608 | ||
954c1994 GS |
4609 | /* |
4610 | =for apidoc sv_len | |
4611 | ||
4612 | Returns the length of the string in the SV. See also C<SvCUR>. | |
4613 | ||
4614 | =cut | |
4615 | */ | |
4616 | ||
79072805 | 4617 | STRLEN |
864dbfa3 | 4618 | Perl_sv_len(pTHX_ register SV *sv) |
79072805 | 4619 | { |
748a9306 | 4620 | char *junk; |
463ee0b2 | 4621 | STRLEN len; |
79072805 LW |
4622 | |
4623 | if (!sv) | |
4624 | return 0; | |
4625 | ||
8990e307 | 4626 | if (SvGMAGICAL(sv)) |
565764a8 | 4627 | len = mg_length(sv); |
8990e307 | 4628 | else |
748a9306 | 4629 | junk = SvPV(sv, len); |
463ee0b2 | 4630 | return len; |
79072805 LW |
4631 | } |
4632 | ||
c461cf8f JH |
4633 | /* |
4634 | =for apidoc sv_len_utf8 | |
4635 | ||
4636 | Returns the number of characters in the string in an SV, counting wide | |
4637 | UTF8 bytes as a single character. | |
4638 | ||
4639 | =cut | |
4640 | */ | |
4641 | ||
a0ed51b3 | 4642 | STRLEN |
864dbfa3 | 4643 | Perl_sv_len_utf8(pTHX_ register SV *sv) |
a0ed51b3 | 4644 | { |
a0ed51b3 LW |
4645 | if (!sv) |
4646 | return 0; | |
4647 | ||
a0ed51b3 | 4648 | if (SvGMAGICAL(sv)) |
b76347f2 | 4649 | return mg_length(sv); |
a0ed51b3 | 4650 | else |
b76347f2 JH |
4651 | { |
4652 | STRLEN len; | |
4653 | U8 *s = (U8*)SvPV(sv, len); | |
4654 | ||
d6efbbad | 4655 | return Perl_utf8_length(aTHX_ s, s + len); |
a0ed51b3 | 4656 | } |
a0ed51b3 LW |
4657 | } |
4658 | ||
4659 | void | |
864dbfa3 | 4660 | Perl_sv_pos_u2b(pTHX_ register SV *sv, I32* offsetp, I32* lenp) |
a0ed51b3 | 4661 | { |
dfe13c55 GS |
4662 | U8 *start; |
4663 | U8 *s; | |
4664 | U8 *send; | |
a0ed51b3 LW |
4665 | I32 uoffset = *offsetp; |
4666 | STRLEN len; | |
4667 | ||
4668 | if (!sv) | |
4669 | return; | |
4670 | ||
dfe13c55 | 4671 | start = s = (U8*)SvPV(sv, len); |
a0ed51b3 LW |
4672 | send = s + len; |
4673 | while (s < send && uoffset--) | |
4674 | s += UTF8SKIP(s); | |
bb40f870 GA |
4675 | if (s >= send) |
4676 | s = send; | |
a0ed51b3 LW |
4677 | *offsetp = s - start; |
4678 | if (lenp) { | |
4679 | I32 ulen = *lenp; | |
4680 | start = s; | |
4681 | while (s < send && ulen--) | |
4682 | s += UTF8SKIP(s); | |
bb40f870 GA |
4683 | if (s >= send) |
4684 | s = send; | |
a0ed51b3 LW |
4685 | *lenp = s - start; |
4686 | } | |
4687 | return; | |
4688 | } | |
4689 | ||
4690 | void | |
864dbfa3 | 4691 | Perl_sv_pos_b2u(pTHX_ register SV *sv, I32* offsetp) |
a0ed51b3 | 4692 | { |
dfe13c55 GS |
4693 | U8 *s; |
4694 | U8 *send; | |
a0ed51b3 LW |
4695 | STRLEN len; |
4696 | ||
4697 | if (!sv) | |
4698 | return; | |
4699 | ||
dfe13c55 | 4700 | s = (U8*)SvPV(sv, len); |
a0ed51b3 | 4701 | if (len < *offsetp) |
a0dbb045 | 4702 | Perl_croak(aTHX_ "panic: sv_pos_b2u: bad byte offset"); |
a0ed51b3 LW |
4703 | send = s + *offsetp; |
4704 | len = 0; | |
4705 | while (s < send) { | |
a0dbb045 JH |
4706 | STRLEN n; |
4707 | ||
4708 | if (utf8_to_uv(s, UTF8SKIP(s), &n, 0)) { | |
4709 | s += n; | |
4710 | len++; | |
4711 | } | |
4712 | else | |
4713 | break; | |
a0ed51b3 LW |
4714 | } |
4715 | *offsetp = len; | |
4716 | return; | |
4717 | } | |
4718 | ||
954c1994 GS |
4719 | /* |
4720 | =for apidoc sv_eq | |
4721 | ||
4722 | Returns a boolean indicating whether the strings in the two SVs are | |
4723 | identical. | |
4724 | ||
4725 | =cut | |
4726 | */ | |
4727 | ||
79072805 | 4728 | I32 |
e01b9e88 | 4729 | Perl_sv_eq(pTHX_ register SV *sv1, register SV *sv2) |
79072805 LW |
4730 | { |
4731 | char *pv1; | |
463ee0b2 | 4732 | STRLEN cur1; |
79072805 | 4733 | char *pv2; |
463ee0b2 | 4734 | STRLEN cur2; |
e01b9e88 SC |
4735 | I32 eq = 0; |
4736 | bool pv1tmp = FALSE; | |
4737 | bool pv2tmp = FALSE; | |
79072805 | 4738 | |
e01b9e88 | 4739 | if (!sv1) { |
79072805 LW |
4740 | pv1 = ""; |
4741 | cur1 = 0; | |
4742 | } | |
463ee0b2 | 4743 | else |
e01b9e88 | 4744 | pv1 = SvPV(sv1, cur1); |
79072805 | 4745 | |
e01b9e88 SC |
4746 | if (!sv2){ |
4747 | pv2 = ""; | |
4748 | cur2 = 0; | |
92d29cee | 4749 | } |
e01b9e88 SC |
4750 | else |
4751 | pv2 = SvPV(sv2, cur2); | |
79072805 | 4752 | |
e01b9e88 | 4753 | /* do not utf8ize the comparands as a side-effect */ |
7bbb0251 | 4754 | if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTE) { |
f9a63242 JH |
4755 | bool is_utf8 = TRUE; |
4756 | ||
1aa99e6b IH |
4757 | if (PL_hints & HINT_UTF8_DISTINCT) |
4758 | return FALSE; | |
4759 | ||
e01b9e88 | 4760 | if (SvUTF8(sv1)) { |
f34ff0a8 | 4761 | char *pv = (char*)bytes_from_utf8((U8*)pv1, &cur1, &is_utf8); |
90f44359 JH |
4762 | |
4763 | if ((pv1tmp = (pv != pv1))) | |
4764 | pv1 = pv; | |
e01b9e88 SC |
4765 | } |
4766 | else { | |
f34ff0a8 | 4767 | char *pv = (char *)bytes_from_utf8((U8*)pv2, &cur2, &is_utf8); |
90f44359 JH |
4768 | |
4769 | if ((pv2tmp = (pv != pv2))) | |
4770 | pv2 = pv; | |
e01b9e88 SC |
4771 | } |
4772 | } | |
79072805 | 4773 | |
e01b9e88 SC |
4774 | if (cur1 == cur2) |
4775 | eq = memEQ(pv1, pv2, cur1); | |
4776 | ||
4777 | if (pv1tmp) | |
4778 | Safefree(pv1); | |
4779 | if (pv2tmp) | |
4780 | Safefree(pv2); | |
4781 | ||
4782 | return eq; | |
79072805 LW |
4783 | } |
4784 | ||
954c1994 GS |
4785 | /* |
4786 | =for apidoc sv_cmp | |
4787 | ||
4788 | Compares the strings in two SVs. Returns -1, 0, or 1 indicating whether the | |
4789 | string in C<sv1> is less than, equal to, or greater than the string in | |
4790 | C<sv2>. | |
4791 | ||
4792 | =cut | |
4793 | */ | |
4794 | ||
79072805 | 4795 | I32 |
e01b9e88 | 4796 | Perl_sv_cmp(pTHX_ register SV *sv1, register SV *sv2) |
79072805 | 4797 | { |
560a288e GS |
4798 | STRLEN cur1, cur2; |
4799 | char *pv1, *pv2; | |
1c846c1f | 4800 | I32 cmp; |
e01b9e88 SC |
4801 | bool pv1tmp = FALSE; |
4802 | bool pv2tmp = FALSE; | |
560a288e | 4803 | |
e01b9e88 SC |
4804 | if (!sv1) { |
4805 | pv1 = ""; | |
560a288e GS |
4806 | cur1 = 0; |
4807 | } | |
e01b9e88 SC |
4808 | else |
4809 | pv1 = SvPV(sv1, cur1); | |
560a288e | 4810 | |
e01b9e88 SC |
4811 | if (!sv2){ |
4812 | pv2 = ""; | |
560a288e GS |
4813 | cur2 = 0; |
4814 | } | |
e01b9e88 SC |
4815 | else |
4816 | pv2 = SvPV(sv2, cur2); | |
79072805 | 4817 | |
e01b9e88 SC |
4818 | /* do not utf8ize the comparands as a side-effect */ |
4819 | if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTE) { | |
1aa99e6b IH |
4820 | if (PL_hints & HINT_UTF8_DISTINCT) |
4821 | return SvUTF8(sv1) ? 1 : -1; | |
4822 | ||
e01b9e88 SC |
4823 | if (SvUTF8(sv1)) { |
4824 | pv2 = (char*)bytes_to_utf8((U8*)pv2, &cur2); | |
4825 | pv2tmp = TRUE; | |
4826 | } | |
4827 | else { | |
4828 | pv1 = (char*)bytes_to_utf8((U8*)pv1, &cur1); | |
4829 | pv1tmp = TRUE; | |
4830 | } | |
4831 | } | |
79072805 | 4832 | |
e01b9e88 SC |
4833 | if (!cur1) { |
4834 | cmp = cur2 ? -1 : 0; | |
4835 | } else if (!cur2) { | |
4836 | cmp = 1; | |
4837 | } else { | |
4838 | I32 retval = memcmp((void*)pv1, (void*)pv2, cur1 < cur2 ? cur1 : cur2); | |
4839 | ||
4840 | if (retval) { | |
4841 | cmp = retval < 0 ? -1 : 1; | |
4842 | } else if (cur1 == cur2) { | |
4843 | cmp = 0; | |
4844 | } else { | |
4845 | cmp = cur1 < cur2 ? -1 : 1; | |
4846 | } | |
4847 | } | |
16660edb | 4848 | |
e01b9e88 SC |
4849 | if (pv1tmp) |
4850 | Safefree(pv1); | |
4851 | if (pv2tmp) | |
4852 | Safefree(pv2); | |
16660edb | 4853 | |
e01b9e88 | 4854 | return cmp; |
bbce6d69 | 4855 | } |
16660edb | 4856 | |
c461cf8f JH |
4857 | /* |
4858 | =for apidoc sv_cmp_locale | |
4859 | ||
4860 | Compares the strings in two SVs in a locale-aware manner. See | |
4861 | L</sv_cmp_locale> | |
4862 | ||
4863 | =cut | |
4864 | */ | |
4865 | ||
bbce6d69 | 4866 | I32 |
864dbfa3 | 4867 | Perl_sv_cmp_locale(pTHX_ register SV *sv1, register SV *sv2) |
bbce6d69 | 4868 | { |
36477c24 | 4869 | #ifdef USE_LOCALE_COLLATE |
16660edb | 4870 | |
bbce6d69 | 4871 | char *pv1, *pv2; |
4872 | STRLEN len1, len2; | |
4873 | I32 retval; | |
16660edb | 4874 | |
3280af22 | 4875 | if (PL_collation_standard) |
bbce6d69 | 4876 | goto raw_compare; |
16660edb | 4877 | |
bbce6d69 | 4878 | len1 = 0; |
8ac85365 | 4879 | pv1 = sv1 ? sv_collxfrm(sv1, &len1) : (char *) NULL; |
bbce6d69 | 4880 | len2 = 0; |
8ac85365 | 4881 | pv2 = sv2 ? sv_collxfrm(sv2, &len2) : (char *) NULL; |
16660edb | 4882 | |
bbce6d69 | 4883 | if (!pv1 || !len1) { |
4884 | if (pv2 && len2) | |
4885 | return -1; | |
4886 | else | |
4887 | goto raw_compare; | |
4888 | } | |
4889 | else { | |
4890 | if (!pv2 || !len2) | |
4891 | return 1; | |
4892 | } | |
16660edb | 4893 | |
bbce6d69 | 4894 | retval = memcmp((void*)pv1, (void*)pv2, len1 < len2 ? len1 : len2); |
16660edb | 4895 | |
bbce6d69 | 4896 | if (retval) |
16660edb | 4897 | return retval < 0 ? -1 : 1; |
4898 | ||
bbce6d69 | 4899 | /* |
4900 | * When the result of collation is equality, that doesn't mean | |
4901 | * that there are no differences -- some locales exclude some | |
4902 | * characters from consideration. So to avoid false equalities, | |
4903 | * we use the raw string as a tiebreaker. | |
4904 | */ | |
16660edb | 4905 | |
bbce6d69 | 4906 | raw_compare: |
4907 | /* FALL THROUGH */ | |
16660edb | 4908 | |
36477c24 | 4909 | #endif /* USE_LOCALE_COLLATE */ |
16660edb | 4910 | |
bbce6d69 | 4911 | return sv_cmp(sv1, sv2); |
4912 | } | |
79072805 | 4913 | |
36477c24 | 4914 | #ifdef USE_LOCALE_COLLATE |
7a4c00b4 | 4915 | /* |
4916 | * Any scalar variable may carry an 'o' magic that contains the | |
4917 | * scalar data of the variable transformed to such a format that | |
4918 | * a normal memory comparison can be used to compare the data | |
4919 | * according to the locale settings. | |
4920 | */ | |
bbce6d69 | 4921 | char * |
864dbfa3 | 4922 | Perl_sv_collxfrm(pTHX_ SV *sv, STRLEN *nxp) |
bbce6d69 | 4923 | { |
7a4c00b4 | 4924 | MAGIC *mg; |
16660edb | 4925 | |
8ac85365 | 4926 | mg = SvMAGICAL(sv) ? mg_find(sv, 'o') : (MAGIC *) NULL; |
3280af22 | 4927 | if (!mg || !mg->mg_ptr || *(U32*)mg->mg_ptr != PL_collation_ix) { |
bbce6d69 | 4928 | char *s, *xf; |
4929 | STRLEN len, xlen; | |
4930 | ||
7a4c00b4 | 4931 | if (mg) |
4932 | Safefree(mg->mg_ptr); | |
bbce6d69 | 4933 | s = SvPV(sv, len); |
4934 | if ((xf = mem_collxfrm(s, len, &xlen))) { | |
ff0cee69 | 4935 | if (SvREADONLY(sv)) { |
4936 | SAVEFREEPV(xf); | |
4937 | *nxp = xlen; | |
3280af22 | 4938 | return xf + sizeof(PL_collation_ix); |
ff0cee69 | 4939 | } |
7a4c00b4 | 4940 | if (! mg) { |
4941 | sv_magic(sv, 0, 'o', 0, 0); | |
4942 | mg = mg_find(sv, 'o'); | |
4943 | assert(mg); | |
bbce6d69 | 4944 | } |
7a4c00b4 | 4945 | mg->mg_ptr = xf; |
565764a8 | 4946 | mg->mg_len = xlen; |
7a4c00b4 | 4947 | } |
4948 | else { | |
ff0cee69 | 4949 | if (mg) { |
4950 | mg->mg_ptr = NULL; | |
565764a8 | 4951 | mg->mg_len = -1; |
ff0cee69 | 4952 | } |
bbce6d69 | 4953 | } |
4954 | } | |
7a4c00b4 | 4955 | if (mg && mg->mg_ptr) { |
565764a8 | 4956 | *nxp = mg->mg_len; |
3280af22 | 4957 | return mg->mg_ptr + sizeof(PL_collation_ix); |
bbce6d69 | 4958 | } |
4959 | else { | |
4960 | *nxp = 0; | |
4961 | return NULL; | |
16660edb | 4962 | } |
79072805 LW |
4963 | } |
4964 | ||
36477c24 | 4965 | #endif /* USE_LOCALE_COLLATE */ |
bbce6d69 | 4966 | |
c461cf8f JH |
4967 | /* |
4968 | =for apidoc sv_gets | |
4969 | ||
4970 | Get a line from the filehandle and store it into the SV, optionally | |
4971 | appending to the currently-stored string. | |
4972 | ||
4973 | =cut | |
4974 | */ | |
4975 | ||
79072805 | 4976 | char * |
864dbfa3 | 4977 | Perl_sv_gets(pTHX_ register SV *sv, register PerlIO *fp, I32 append) |
79072805 | 4978 | { |
c07a80fd | 4979 | char *rsptr; |
4980 | STRLEN rslen; | |
4981 | register STDCHAR rslast; | |
4982 | register STDCHAR *bp; | |
4983 | register I32 cnt; | |
4984 | I32 i; | |
4985 | ||
2213622d | 4986 | SV_CHECK_THINKFIRST(sv); |
6fc92669 | 4987 | (void)SvUPGRADE(sv, SVt_PV); |
99491443 | 4988 | |
ff68c719 | 4989 | SvSCREAM_off(sv); |
c07a80fd | 4990 | |
3280af22 | 4991 | if (RsSNARF(PL_rs)) { |
c07a80fd | 4992 | rsptr = NULL; |
4993 | rslen = 0; | |
4994 | } | |
3280af22 | 4995 | else if (RsRECORD(PL_rs)) { |
5b2b9c68 HM |
4996 | I32 recsize, bytesread; |
4997 | char *buffer; | |
4998 | ||
4999 | /* Grab the size of the record we're getting */ | |
3280af22 | 5000 | recsize = SvIV(SvRV(PL_rs)); |
5b2b9c68 | 5001 | (void)SvPOK_only(sv); /* Validate pointer */ |
e670df4e | 5002 | buffer = SvGROW(sv, recsize + 1); |
5b2b9c68 HM |
5003 | /* Go yank in */ |
5004 | #ifdef VMS | |
5005 | /* VMS wants read instead of fread, because fread doesn't respect */ | |
5006 | /* RMS record boundaries. This is not necessarily a good thing to be */ | |
5007 | /* doing, but we've got no other real choice */ | |
5008 | bytesread = PerlLIO_read(PerlIO_fileno(fp), buffer, recsize); | |
5009 | #else | |
5010 | bytesread = PerlIO_read(fp, buffer, recsize); | |
5011 | #endif | |
5012 | SvCUR_set(sv, bytesread); | |
e670df4e | 5013 | buffer[bytesread] = '\0'; |
7d59b7e4 NIS |
5014 | if (PerlIO_isutf8(fp)) |
5015 | SvUTF8_on(sv); | |
5016 | else | |
5017 | SvUTF8_off(sv); | |
5b2b9c68 HM |
5018 | return(SvCUR(sv) ? SvPVX(sv) : Nullch); |
5019 | } | |
3280af22 | 5020 | else if (RsPARA(PL_rs)) { |
c07a80fd | 5021 | rsptr = "\n\n"; |
5022 | rslen = 2; | |
5023 | } | |
7d59b7e4 NIS |
5024 | else { |
5025 | /* Get $/ i.e. PL_rs into same encoding as stream wants */ | |
5026 | if (PerlIO_isutf8(fp)) { | |
5027 | rsptr = SvPVutf8(PL_rs, rslen); | |
5028 | } | |
5029 | else { | |
5030 | if (SvUTF8(PL_rs)) { | |
5031 | if (!sv_utf8_downgrade(PL_rs, TRUE)) { | |
5032 | Perl_croak(aTHX_ "Wide character in $/"); | |
5033 | } | |
5034 | } | |
5035 | rsptr = SvPV(PL_rs, rslen); | |
5036 | } | |
5037 | } | |
5038 | ||
c07a80fd | 5039 | rslast = rslen ? rsptr[rslen - 1] : '\0'; |
5040 | ||
3280af22 | 5041 | if (RsPARA(PL_rs)) { /* have to do this both before and after */ |
79072805 | 5042 | do { /* to make sure file boundaries work right */ |
760ac839 | 5043 | if (PerlIO_eof(fp)) |
a0d0e21e | 5044 | return 0; |
760ac839 | 5045 | i = PerlIO_getc(fp); |
79072805 | 5046 | if (i != '\n') { |
a0d0e21e LW |
5047 | if (i == -1) |
5048 | return 0; | |
760ac839 | 5049 | PerlIO_ungetc(fp,i); |
79072805 LW |
5050 | break; |
5051 | } | |
5052 | } while (i != EOF); | |
5053 | } | |
c07a80fd | 5054 | |
760ac839 LW |
5055 | /* See if we know enough about I/O mechanism to cheat it ! */ |
5056 | ||
5057 | /* This used to be #ifdef test - it is made run-time test for ease | |
1c846c1f | 5058 | of abstracting out stdio interface. One call should be cheap |
760ac839 LW |
5059 | enough here - and may even be a macro allowing compile |
5060 | time optimization. | |
5061 | */ | |
5062 | ||
5063 | if (PerlIO_fast_gets(fp)) { | |
5064 | ||
5065 | /* | |
5066 | * We're going to steal some values from the stdio struct | |
5067 | * and put EVERYTHING in the innermost loop into registers. | |
5068 | */ | |
5069 | register STDCHAR *ptr; | |
5070 | STRLEN bpx; | |
5071 | I32 shortbuffered; | |
5072 | ||
16660edb | 5073 | #if defined(VMS) && defined(PERLIO_IS_STDIO) |
5074 | /* An ungetc()d char is handled separately from the regular | |
5075 | * buffer, so we getc() it back out and stuff it in the buffer. | |
5076 | */ | |
5077 | i = PerlIO_getc(fp); | |
5078 | if (i == EOF) return 0; | |
5079 | *(--((*fp)->_ptr)) = (unsigned char) i; | |
5080 | (*fp)->_cnt++; | |
5081 | #endif | |
c07a80fd | 5082 | |
c2960299 | 5083 | /* Here is some breathtakingly efficient cheating */ |
c07a80fd | 5084 | |
a20bf0c3 | 5085 | cnt = PerlIO_get_cnt(fp); /* get count into register */ |
a0d0e21e | 5086 | (void)SvPOK_only(sv); /* validate pointer */ |
79072805 LW |
5087 | if (SvLEN(sv) - append <= cnt + 1) { /* make sure we have the room */ |
5088 | if (cnt > 80 && SvLEN(sv) > append) { | |
5089 | shortbuffered = cnt - SvLEN(sv) + append + 1; | |
5090 | cnt -= shortbuffered; | |
5091 | } | |
5092 | else { | |
5093 | shortbuffered = 0; | |
bbce6d69 | 5094 | /* remember that cnt can be negative */ |
5095 | SvGROW(sv, append + (cnt <= 0 ? 2 : (cnt + 1))); | |
79072805 LW |
5096 | } |
5097 | } | |
5098 | else | |
5099 | shortbuffered = 0; | |
c07a80fd | 5100 | bp = (STDCHAR*)SvPVX(sv) + append; /* move these two too to registers */ |
a20bf0c3 | 5101 | ptr = (STDCHAR*)PerlIO_get_ptr(fp); |
16660edb | 5102 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5103 | "Screamer: entering, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt)); |
16660edb | 5104 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5105 | "Screamer: entering: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n", |
1c846c1f | 5106 | PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp), |
1d7c1841 | 5107 | PTR2UV(PerlIO_has_base(fp) ? PerlIO_get_base(fp) : 0))); |
79072805 LW |
5108 | for (;;) { |
5109 | screamer: | |
93a17b20 | 5110 | if (cnt > 0) { |
c07a80fd | 5111 | if (rslen) { |
760ac839 LW |
5112 | while (cnt > 0) { /* this | eat */ |
5113 | cnt--; | |
c07a80fd | 5114 | if ((*bp++ = *ptr++) == rslast) /* really | dust */ |
5115 | goto thats_all_folks; /* screams | sed :-) */ | |
5116 | } | |
5117 | } | |
5118 | else { | |
1c846c1f NIS |
5119 | Copy(ptr, bp, cnt, char); /* this | eat */ |
5120 | bp += cnt; /* screams | dust */ | |
c07a80fd | 5121 | ptr += cnt; /* louder | sed :-) */ |
a5f75d66 | 5122 | cnt = 0; |
93a17b20 | 5123 | } |
79072805 LW |
5124 | } |
5125 | ||
748a9306 | 5126 | if (shortbuffered) { /* oh well, must extend */ |
79072805 LW |
5127 | cnt = shortbuffered; |
5128 | shortbuffered = 0; | |
c07a80fd | 5129 | bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */ |
79072805 LW |
5130 | SvCUR_set(sv, bpx); |
5131 | SvGROW(sv, SvLEN(sv) + append + cnt + 2); | |
c07a80fd | 5132 | bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */ |
79072805 LW |
5133 | continue; |
5134 | } | |
5135 | ||
16660edb | 5136 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 GS |
5137 | "Screamer: going to getc, ptr=%"UVuf", cnt=%ld\n", |
5138 | PTR2UV(ptr),(long)cnt)); | |
a20bf0c3 | 5139 | PerlIO_set_ptrcnt(fp, ptr, cnt); /* deregisterize cnt and ptr */ |
16660edb | 5140 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5141 | "Screamer: pre: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n", |
1c846c1f | 5142 | PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp), |
1d7c1841 | 5143 | PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0))); |
1c846c1f | 5144 | /* This used to call 'filbuf' in stdio form, but as that behaves like |
774d564b | 5145 | getc when cnt <= 0 we use PerlIO_getc here to avoid introducing |
5146 | another abstraction. */ | |
760ac839 | 5147 | i = PerlIO_getc(fp); /* get more characters */ |
16660edb | 5148 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5149 | "Screamer: post: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n", |
1c846c1f | 5150 | PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp), |
1d7c1841 | 5151 | PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0))); |
a20bf0c3 JH |
5152 | cnt = PerlIO_get_cnt(fp); |
5153 | ptr = (STDCHAR*)PerlIO_get_ptr(fp); /* reregisterize cnt and ptr */ | |
16660edb | 5154 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5155 | "Screamer: after getc, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt)); |
79072805 | 5156 | |
748a9306 LW |
5157 | if (i == EOF) /* all done for ever? */ |
5158 | goto thats_really_all_folks; | |
5159 | ||
c07a80fd | 5160 | bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */ |
79072805 LW |
5161 | SvCUR_set(sv, bpx); |
5162 | SvGROW(sv, bpx + cnt + 2); | |
c07a80fd | 5163 | bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */ |
5164 | ||
760ac839 | 5165 | *bp++ = i; /* store character from PerlIO_getc */ |
79072805 | 5166 | |
c07a80fd | 5167 | if (rslen && (STDCHAR)i == rslast) /* all done for now? */ |
79072805 | 5168 | goto thats_all_folks; |
79072805 LW |
5169 | } |
5170 | ||
5171 | thats_all_folks: | |
c07a80fd | 5172 | if ((rslen > 1 && (bp - (STDCHAR*)SvPVX(sv) < rslen)) || |
36477c24 | 5173 | memNE((char*)bp - rslen, rsptr, rslen)) |
760ac839 | 5174 | goto screamer; /* go back to the fray */ |
79072805 LW |
5175 | thats_really_all_folks: |
5176 | if (shortbuffered) | |
5177 | cnt += shortbuffered; | |
16660edb | 5178 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5179 | "Screamer: quitting, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt)); |
a20bf0c3 | 5180 | PerlIO_set_ptrcnt(fp, ptr, cnt); /* put these back or we're in trouble */ |
16660edb | 5181 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5182 | "Screamer: end: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n", |
1c846c1f | 5183 | PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp), |
1d7c1841 | 5184 | PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0))); |
79072805 | 5185 | *bp = '\0'; |
760ac839 | 5186 | SvCUR_set(sv, bp - (STDCHAR*)SvPVX(sv)); /* set length */ |
16660edb | 5187 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
fb73857a | 5188 | "Screamer: done, len=%ld, string=|%.*s|\n", |
5189 | (long)SvCUR(sv),(int)SvCUR(sv),SvPVX(sv))); | |
760ac839 LW |
5190 | } |
5191 | else | |
79072805 | 5192 | { |
4d2c4e07 | 5193 | #ifndef EPOC |
760ac839 | 5194 | /*The big, slow, and stupid way */ |
c07a80fd | 5195 | STDCHAR buf[8192]; |
4d2c4e07 OF |
5196 | #else |
5197 | /* Need to work around EPOC SDK features */ | |
5198 | /* On WINS: MS VC5 generates calls to _chkstk, */ | |
5199 | /* if a `large' stack frame is allocated */ | |
5200 | /* gcc on MARM does not generate calls like these */ | |
5201 | STDCHAR buf[1024]; | |
5202 | #endif | |
79072805 | 5203 | |
760ac839 | 5204 | screamer2: |
c07a80fd | 5205 | if (rslen) { |
760ac839 LW |
5206 | register STDCHAR *bpe = buf + sizeof(buf); |
5207 | bp = buf; | |
5208 | while ((i = PerlIO_getc(fp)) != EOF && (*bp++ = i) != rslast && bp < bpe) | |
5209 | ; /* keep reading */ | |
5210 | cnt = bp - buf; | |
c07a80fd | 5211 | } |
5212 | else { | |
760ac839 | 5213 | cnt = PerlIO_read(fp,(char*)buf, sizeof(buf)); |
16660edb | 5214 | /* Accomodate broken VAXC compiler, which applies U8 cast to |
5215 | * both args of ?: operator, causing EOF to change into 255 | |
5216 | */ | |
5217 | if (cnt) { i = (U8)buf[cnt - 1]; } else { i = EOF; } | |
c07a80fd | 5218 | } |
79072805 LW |
5219 | |
5220 | if (append) | |
760ac839 | 5221 | sv_catpvn(sv, (char *) buf, cnt); |
79072805 | 5222 | else |
760ac839 | 5223 | sv_setpvn(sv, (char *) buf, cnt); |
c07a80fd | 5224 | |
5225 | if (i != EOF && /* joy */ | |
5226 | (!rslen || | |
5227 | SvCUR(sv) < rslen || | |
36477c24 | 5228 | memNE(SvPVX(sv) + SvCUR(sv) - rslen, rsptr, rslen))) |
79072805 LW |
5229 | { |
5230 | append = -1; | |
63e4d877 CS |
5231 | /* |
5232 | * If we're reading from a TTY and we get a short read, | |
5233 | * indicating that the user hit his EOF character, we need | |
5234 | * to notice it now, because if we try to read from the TTY | |
5235 | * again, the EOF condition will disappear. | |
5236 | * | |
5237 | * The comparison of cnt to sizeof(buf) is an optimization | |
5238 | * that prevents unnecessary calls to feof(). | |
5239 | * | |
5240 | * - jik 9/25/96 | |
5241 | */ | |
5242 | if (!(cnt < sizeof(buf) && PerlIO_eof(fp))) | |
5243 | goto screamer2; | |
79072805 LW |
5244 | } |
5245 | } | |
5246 | ||
1c846c1f | 5247 | if (RsPARA(PL_rs)) { /* have to do this both before and after */ |
c07a80fd | 5248 | while (i != EOF) { /* to make sure file boundaries work right */ |
760ac839 | 5249 | i = PerlIO_getc(fp); |
79072805 | 5250 | if (i != '\n') { |
760ac839 | 5251 | PerlIO_ungetc(fp,i); |
79072805 LW |
5252 | break; |
5253 | } | |
5254 | } | |
5255 | } | |
c07a80fd | 5256 | |
7d59b7e4 NIS |
5257 | if (PerlIO_isutf8(fp)) |
5258 | SvUTF8_on(sv); | |
5259 | else | |
5260 | SvUTF8_off(sv); | |
5261 | ||
c07a80fd | 5262 | return (SvCUR(sv) - append) ? SvPVX(sv) : Nullch; |
79072805 LW |
5263 | } |
5264 | ||
760ac839 | 5265 | |
954c1994 GS |
5266 | /* |
5267 | =for apidoc sv_inc | |
5268 | ||
5269 | Auto-increment of the value in the SV. | |
5270 | ||
5271 | =cut | |
5272 | */ | |
5273 | ||
79072805 | 5274 | void |
864dbfa3 | 5275 | Perl_sv_inc(pTHX_ register SV *sv) |
79072805 LW |
5276 | { |
5277 | register char *d; | |
463ee0b2 | 5278 | int flags; |
79072805 LW |
5279 | |
5280 | if (!sv) | |
5281 | return; | |
b23a5f78 GB |
5282 | if (SvGMAGICAL(sv)) |
5283 | mg_get(sv); | |
ed6116ce | 5284 | if (SvTHINKFIRST(sv)) { |
0f15f207 | 5285 | if (SvREADONLY(sv)) { |
3280af22 | 5286 | if (PL_curcop != &PL_compiling) |
cea2e8a9 | 5287 | Perl_croak(aTHX_ PL_no_modify); |
0f15f207 | 5288 | } |
a0d0e21e | 5289 | if (SvROK(sv)) { |
b5be31e9 | 5290 | IV i; |
9e7bc3e8 JD |
5291 | if (SvAMAGIC(sv) && AMG_CALLun(sv,inc)) |
5292 | return; | |
56431972 | 5293 | i = PTR2IV(SvRV(sv)); |
b5be31e9 SM |
5294 | sv_unref(sv); |
5295 | sv_setiv(sv, i); | |
a0d0e21e | 5296 | } |
ed6116ce | 5297 | } |
8990e307 | 5298 | flags = SvFLAGS(sv); |
28e5dec8 JH |
5299 | if ((flags & (SVp_NOK|SVp_IOK)) == SVp_NOK) { |
5300 | /* It's (privately or publicly) a float, but not tested as an | |
5301 | integer, so test it to see. */ | |
d460ef45 | 5302 | (void) SvIV(sv); |
28e5dec8 JH |
5303 | flags = SvFLAGS(sv); |
5304 | } | |
5305 | if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) { | |
5306 | /* It's publicly an integer, or privately an integer-not-float */ | |
5307 | oops_its_int: | |
25da4f38 IZ |
5308 | if (SvIsUV(sv)) { |
5309 | if (SvUVX(sv) == UV_MAX) | |
65202027 | 5310 | sv_setnv(sv, (NV)UV_MAX + 1.0); |
25da4f38 IZ |
5311 | else |
5312 | (void)SvIOK_only_UV(sv); | |
5313 | ++SvUVX(sv); | |
5314 | } else { | |
5315 | if (SvIVX(sv) == IV_MAX) | |
28e5dec8 | 5316 | sv_setuv(sv, (UV)IV_MAX + 1); |
25da4f38 IZ |
5317 | else { |
5318 | (void)SvIOK_only(sv); | |
5319 | ++SvIVX(sv); | |
1c846c1f | 5320 | } |
55497cff | 5321 | } |
79072805 LW |
5322 | return; |
5323 | } | |
28e5dec8 JH |
5324 | if (flags & SVp_NOK) { |
5325 | (void)SvNOK_only(sv); | |
5326 | SvNVX(sv) += 1.0; | |
5327 | return; | |
5328 | } | |
5329 | ||
8990e307 | 5330 | if (!(flags & SVp_POK) || !*SvPVX(sv)) { |
28e5dec8 JH |
5331 | if ((flags & SVTYPEMASK) < SVt_PVIV) |
5332 | sv_upgrade(sv, SVt_IV); | |
5333 | (void)SvIOK_only(sv); | |
5334 | SvIVX(sv) = 1; | |
79072805 LW |
5335 | return; |
5336 | } | |
463ee0b2 | 5337 | d = SvPVX(sv); |
79072805 LW |
5338 | while (isALPHA(*d)) d++; |
5339 | while (isDIGIT(*d)) d++; | |
5340 | if (*d) { | |
28e5dec8 JH |
5341 | #ifdef PERL_PRESERVE_IVUV |
5342 | /* Got to punt this an an integer if needs be, but we don't issue | |
5343 | warnings. Probably ought to make the sv_iv_please() that does | |
5344 | the conversion if possible, and silently. */ | |
5345 | I32 numtype = looks_like_number(sv); | |
5346 | if (numtype && !(numtype & IS_NUMBER_INFINITY)) { | |
5347 | /* Need to try really hard to see if it's an integer. | |
5348 | 9.22337203685478e+18 is an integer. | |
5349 | but "9.22337203685478e+18" + 0 is UV=9223372036854779904 | |
5350 | so $a="9.22337203685478e+18"; $a+0; $a++ | |
5351 | needs to be the same as $a="9.22337203685478e+18"; $a++ | |
5352 | or we go insane. */ | |
d460ef45 | 5353 | |
28e5dec8 JH |
5354 | (void) sv_2iv(sv); |
5355 | if (SvIOK(sv)) | |
5356 | goto oops_its_int; | |
5357 | ||
5358 | /* sv_2iv *should* have made this an NV */ | |
5359 | if (flags & SVp_NOK) { | |
5360 | (void)SvNOK_only(sv); | |
5361 | SvNVX(sv) += 1.0; | |
5362 | return; | |
5363 | } | |
5364 | /* I don't think we can get here. Maybe I should assert this | |
5365 | And if we do get here I suspect that sv_setnv will croak. NWC | |
5366 | Fall through. */ | |
5367 | #if defined(USE_LONG_DOUBLE) | |
5368 | 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", | |
5369 | SvPVX(sv), SvIVX(sv), SvNVX(sv))); | |
5370 | #else | |
5371 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%g\n", | |
5372 | SvPVX(sv), SvIVX(sv), SvNVX(sv))); | |
5373 | #endif | |
5374 | } | |
5375 | #endif /* PERL_PRESERVE_IVUV */ | |
5376 | sv_setnv(sv,Atof(SvPVX(sv)) + 1.0); | |
79072805 LW |
5377 | return; |
5378 | } | |
5379 | d--; | |
463ee0b2 | 5380 | while (d >= SvPVX(sv)) { |
79072805 LW |
5381 | if (isDIGIT(*d)) { |
5382 | if (++*d <= '9') | |
5383 | return; | |
5384 | *(d--) = '0'; | |
5385 | } | |
5386 | else { | |
9d116dd7 JH |
5387 | #ifdef EBCDIC |
5388 | /* MKS: The original code here died if letters weren't consecutive. | |
5389 | * at least it didn't have to worry about non-C locales. The | |
5390 | * new code assumes that ('z'-'a')==('Z'-'A'), letters are | |
1c846c1f | 5391 | * arranged in order (although not consecutively) and that only |
9d116dd7 JH |
5392 | * [A-Za-z] are accepted by isALPHA in the C locale. |
5393 | */ | |
5394 | if (*d != 'z' && *d != 'Z') { | |
5395 | do { ++*d; } while (!isALPHA(*d)); | |
5396 | return; | |
5397 | } | |
5398 | *(d--) -= 'z' - 'a'; | |
5399 | #else | |
79072805 LW |
5400 | ++*d; |
5401 | if (isALPHA(*d)) | |
5402 | return; | |
5403 | *(d--) -= 'z' - 'a' + 1; | |
9d116dd7 | 5404 | #endif |
79072805 LW |
5405 | } |
5406 | } | |
5407 | /* oh,oh, the number grew */ | |
5408 | SvGROW(sv, SvCUR(sv) + 2); | |
5409 | SvCUR(sv)++; | |
463ee0b2 | 5410 | for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX(sv); d--) |
79072805 LW |
5411 | *d = d[-1]; |
5412 | if (isDIGIT(d[1])) | |
5413 | *d = '1'; | |
5414 | else | |
5415 | *d = d[1]; | |
5416 | } | |
5417 | ||
954c1994 GS |
5418 | /* |
5419 | =for apidoc sv_dec | |
5420 | ||
5421 | Auto-decrement of the value in the SV. | |
5422 | ||
5423 | =cut | |
5424 | */ | |
5425 | ||
79072805 | 5426 | void |
864dbfa3 | 5427 | Perl_sv_dec(pTHX_ register SV *sv) |
79072805 | 5428 | { |
463ee0b2 LW |
5429 | int flags; |
5430 | ||
79072805 LW |
5431 | if (!sv) |
5432 | return; | |
b23a5f78 GB |
5433 | if (SvGMAGICAL(sv)) |
5434 | mg_get(sv); | |
ed6116ce | 5435 | if (SvTHINKFIRST(sv)) { |
0f15f207 | 5436 | if (SvREADONLY(sv)) { |
3280af22 | 5437 | if (PL_curcop != &PL_compiling) |
cea2e8a9 | 5438 | Perl_croak(aTHX_ PL_no_modify); |
0f15f207 | 5439 | } |
a0d0e21e | 5440 | if (SvROK(sv)) { |
b5be31e9 | 5441 | IV i; |
9e7bc3e8 JD |
5442 | if (SvAMAGIC(sv) && AMG_CALLun(sv,dec)) |
5443 | return; | |
56431972 | 5444 | i = PTR2IV(SvRV(sv)); |
b5be31e9 SM |
5445 | sv_unref(sv); |
5446 | sv_setiv(sv, i); | |
a0d0e21e | 5447 | } |
ed6116ce | 5448 | } |
28e5dec8 JH |
5449 | /* Unlike sv_inc we don't have to worry about string-never-numbers |
5450 | and keeping them magic. But we mustn't warn on punting */ | |
8990e307 | 5451 | flags = SvFLAGS(sv); |
28e5dec8 JH |
5452 | if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) { |
5453 | /* It's publicly an integer, or privately an integer-not-float */ | |
5454 | oops_its_int: | |
25da4f38 IZ |
5455 | if (SvIsUV(sv)) { |
5456 | if (SvUVX(sv) == 0) { | |
5457 | (void)SvIOK_only(sv); | |
5458 | SvIVX(sv) = -1; | |
5459 | } | |
5460 | else { | |
5461 | (void)SvIOK_only_UV(sv); | |
5462 | --SvUVX(sv); | |
1c846c1f | 5463 | } |
25da4f38 IZ |
5464 | } else { |
5465 | if (SvIVX(sv) == IV_MIN) | |
65202027 | 5466 | sv_setnv(sv, (NV)IV_MIN - 1.0); |
25da4f38 IZ |
5467 | else { |
5468 | (void)SvIOK_only(sv); | |
5469 | --SvIVX(sv); | |
1c846c1f | 5470 | } |
55497cff | 5471 | } |
5472 | return; | |
5473 | } | |
28e5dec8 JH |
5474 | if (flags & SVp_NOK) { |
5475 | SvNVX(sv) -= 1.0; | |
5476 | (void)SvNOK_only(sv); | |
5477 | return; | |
5478 | } | |
8990e307 | 5479 | if (!(flags & SVp_POK)) { |
4633a7c4 LW |
5480 | if ((flags & SVTYPEMASK) < SVt_PVNV) |
5481 | sv_upgrade(sv, SVt_NV); | |
463ee0b2 | 5482 | SvNVX(sv) = -1.0; |
a0d0e21e | 5483 | (void)SvNOK_only(sv); |
79072805 LW |
5484 | return; |
5485 | } | |
28e5dec8 JH |
5486 | #ifdef PERL_PRESERVE_IVUV |
5487 | { | |
5488 | I32 numtype = looks_like_number(sv); | |
5489 | if (numtype && !(numtype & IS_NUMBER_INFINITY)) { | |
5490 | /* Need to try really hard to see if it's an integer. | |
5491 | 9.22337203685478e+18 is an integer. | |
5492 | but "9.22337203685478e+18" + 0 is UV=9223372036854779904 | |
5493 | so $a="9.22337203685478e+18"; $a+0; $a-- | |
5494 | needs to be the same as $a="9.22337203685478e+18"; $a-- | |
5495 | or we go insane. */ | |
d460ef45 | 5496 | |
28e5dec8 JH |
5497 | (void) sv_2iv(sv); |
5498 | if (SvIOK(sv)) | |
5499 | goto oops_its_int; | |
5500 | ||
5501 | /* sv_2iv *should* have made this an NV */ | |
5502 | if (flags & SVp_NOK) { | |
5503 | (void)SvNOK_only(sv); | |
5504 | SvNVX(sv) -= 1.0; | |
5505 | return; | |
5506 | } | |
5507 | /* I don't think we can get here. Maybe I should assert this | |
5508 | And if we do get here I suspect that sv_setnv will croak. NWC | |
5509 | Fall through. */ | |
5510 | #if defined(USE_LONG_DOUBLE) | |
5511 | 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", | |
5512 | SvPVX(sv), SvIVX(sv), SvNVX(sv))); | |
5513 | #else | |
5514 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%g\n", | |
5515 | SvPVX(sv), SvIVX(sv), SvNVX(sv))); | |
5516 | #endif | |
5517 | } | |
5518 | } | |
5519 | #endif /* PERL_PRESERVE_IVUV */ | |
097ee67d | 5520 | sv_setnv(sv,Atof(SvPVX(sv)) - 1.0); /* punt */ |
79072805 LW |
5521 | } |
5522 | ||
954c1994 GS |
5523 | /* |
5524 | =for apidoc sv_mortalcopy | |
5525 | ||
5526 | Creates a new SV which is a copy of the original SV. The new SV is marked | |
5527 | as mortal. | |
5528 | ||
5529 | =cut | |
5530 | */ | |
5531 | ||
79072805 LW |
5532 | /* Make a string that will exist for the duration of the expression |
5533 | * evaluation. Actually, it may have to last longer than that, but | |
5534 | * hopefully we won't free it until it has been assigned to a | |
5535 | * permanent location. */ | |
5536 | ||
5537 | SV * | |
864dbfa3 | 5538 | Perl_sv_mortalcopy(pTHX_ SV *oldstr) |
79072805 | 5539 | { |
463ee0b2 | 5540 | register SV *sv; |
79072805 | 5541 | |
4561caa4 | 5542 | new_SV(sv); |
79072805 | 5543 | sv_setsv(sv,oldstr); |
677b06e3 GS |
5544 | EXTEND_MORTAL(1); |
5545 | PL_tmps_stack[++PL_tmps_ix] = sv; | |
8990e307 LW |
5546 | SvTEMP_on(sv); |
5547 | return sv; | |
5548 | } | |
5549 | ||
954c1994 GS |
5550 | /* |
5551 | =for apidoc sv_newmortal | |
5552 | ||
5553 | Creates a new SV which is mortal. The reference count of the SV is set to 1. | |
5554 | ||
5555 | =cut | |
5556 | */ | |
5557 | ||
8990e307 | 5558 | SV * |
864dbfa3 | 5559 | Perl_sv_newmortal(pTHX) |
8990e307 LW |
5560 | { |
5561 | register SV *sv; | |
5562 | ||
4561caa4 | 5563 | new_SV(sv); |
8990e307 | 5564 | SvFLAGS(sv) = SVs_TEMP; |
677b06e3 GS |
5565 | EXTEND_MORTAL(1); |
5566 | PL_tmps_stack[++PL_tmps_ix] = sv; | |
79072805 LW |
5567 | return sv; |
5568 | } | |
5569 | ||
954c1994 GS |
5570 | /* |
5571 | =for apidoc sv_2mortal | |
5572 | ||
5573 | Marks an SV as mortal. The SV will be destroyed when the current context | |
5574 | ends. | |
5575 | ||
5576 | =cut | |
5577 | */ | |
5578 | ||
79072805 LW |
5579 | /* same thing without the copying */ |
5580 | ||
5581 | SV * | |
864dbfa3 | 5582 | Perl_sv_2mortal(pTHX_ register SV *sv) |
79072805 LW |
5583 | { |
5584 | if (!sv) | |
5585 | return sv; | |
d689ffdd | 5586 | if (SvREADONLY(sv) && SvIMMORTAL(sv)) |
11162842 | 5587 | return sv; |
677b06e3 GS |
5588 | EXTEND_MORTAL(1); |
5589 | PL_tmps_stack[++PL_tmps_ix] = sv; | |
8990e307 | 5590 | SvTEMP_on(sv); |
79072805 LW |
5591 | return sv; |
5592 | } | |
5593 | ||
954c1994 GS |
5594 | /* |
5595 | =for apidoc newSVpv | |
5596 | ||
5597 | Creates a new SV and copies a string into it. The reference count for the | |
5598 | SV is set to 1. If C<len> is zero, Perl will compute the length using | |
5599 | strlen(). For efficiency, consider using C<newSVpvn> instead. | |
5600 | ||
5601 | =cut | |
5602 | */ | |
5603 | ||
79072805 | 5604 | SV * |
864dbfa3 | 5605 | Perl_newSVpv(pTHX_ const char *s, STRLEN len) |
79072805 | 5606 | { |
463ee0b2 | 5607 | register SV *sv; |
79072805 | 5608 | |
4561caa4 | 5609 | new_SV(sv); |
79072805 LW |
5610 | if (!len) |
5611 | len = strlen(s); | |
5612 | sv_setpvn(sv,s,len); | |
5613 | return sv; | |
5614 | } | |
5615 | ||
954c1994 GS |
5616 | /* |
5617 | =for apidoc newSVpvn | |
5618 | ||
5619 | Creates a new SV and copies a string into it. The reference count for the | |
1c846c1f | 5620 | SV is set to 1. Note that if C<len> is zero, Perl will create a zero length |
954c1994 GS |
5621 | string. You are responsible for ensuring that the source string is at least |
5622 | C<len> bytes long. | |
5623 | ||
5624 | =cut | |
5625 | */ | |
5626 | ||
9da1e3b5 | 5627 | SV * |
864dbfa3 | 5628 | Perl_newSVpvn(pTHX_ const char *s, STRLEN len) |
9da1e3b5 MUN |
5629 | { |
5630 | register SV *sv; | |
5631 | ||
5632 | new_SV(sv); | |
9da1e3b5 MUN |
5633 | sv_setpvn(sv,s,len); |
5634 | return sv; | |
5635 | } | |
5636 | ||
1c846c1f NIS |
5637 | /* |
5638 | =for apidoc newSVpvn_share | |
5639 | ||
5640 | Creates a new SV and populates it with a string from | |
5641 | the string table. Turns on READONLY and FAKE. | |
5642 | The idea here is that as string table is used for shared hash | |
5643 | keys these strings will have SvPVX == HeKEY and hash lookup | |
5644 | will avoid string compare. | |
5645 | ||
5646 | =cut | |
5647 | */ | |
5648 | ||
5649 | SV * | |
c3654f1a | 5650 | Perl_newSVpvn_share(pTHX_ const char *src, I32 len, U32 hash) |
1c846c1f NIS |
5651 | { |
5652 | register SV *sv; | |
c3654f1a IH |
5653 | bool is_utf8 = FALSE; |
5654 | if (len < 0) { | |
5655 | len = -len; | |
5656 | is_utf8 = TRUE; | |
5657 | } | |
75a54232 JH |
5658 | if (is_utf8 && !(PL_hints & HINT_UTF8_DISTINCT)) { |
5659 | STRLEN tmplen = len; | |
5660 | /* See the note in hv.c:hv_fetch() --jhi */ | |
5661 | src = (char*)bytes_from_utf8((U8*)src, &tmplen, &is_utf8); | |
5662 | len = tmplen; | |
5663 | } | |
1c846c1f NIS |
5664 | if (!hash) |
5665 | PERL_HASH(hash, src, len); | |
5666 | new_SV(sv); | |
5667 | sv_upgrade(sv, SVt_PVIV); | |
c3654f1a | 5668 | SvPVX(sv) = sharepvn(src, is_utf8?-len:len, hash); |
1c846c1f NIS |
5669 | SvCUR(sv) = len; |
5670 | SvUVX(sv) = hash; | |
5671 | SvLEN(sv) = 0; | |
5672 | SvREADONLY_on(sv); | |
5673 | SvFAKE_on(sv); | |
5674 | SvPOK_on(sv); | |
c3654f1a IH |
5675 | if (is_utf8) |
5676 | SvUTF8_on(sv); | |
1c846c1f NIS |
5677 | return sv; |
5678 | } | |
5679 | ||
cea2e8a9 | 5680 | #if defined(PERL_IMPLICIT_CONTEXT) |
46fc3d4c | 5681 | SV * |
cea2e8a9 | 5682 | Perl_newSVpvf_nocontext(const char* pat, ...) |
46fc3d4c | 5683 | { |
cea2e8a9 | 5684 | dTHX; |
46fc3d4c | 5685 | register SV *sv; |
5686 | va_list args; | |
46fc3d4c | 5687 | va_start(args, pat); |
c5be433b | 5688 | sv = vnewSVpvf(pat, &args); |
46fc3d4c | 5689 | va_end(args); |
5690 | return sv; | |
5691 | } | |
cea2e8a9 | 5692 | #endif |
46fc3d4c | 5693 | |
954c1994 GS |
5694 | /* |
5695 | =for apidoc newSVpvf | |
5696 | ||
5697 | Creates a new SV an initialize it with the string formatted like | |
5698 | C<sprintf>. | |
5699 | ||
5700 | =cut | |
5701 | */ | |
5702 | ||
cea2e8a9 GS |
5703 | SV * |
5704 | Perl_newSVpvf(pTHX_ const char* pat, ...) | |
5705 | { | |
5706 | register SV *sv; | |
5707 | va_list args; | |
cea2e8a9 | 5708 | va_start(args, pat); |
c5be433b | 5709 | sv = vnewSVpvf(pat, &args); |
cea2e8a9 GS |
5710 | va_end(args); |
5711 | return sv; | |
5712 | } | |
46fc3d4c | 5713 | |
79072805 | 5714 | SV * |
c5be433b GS |
5715 | Perl_vnewSVpvf(pTHX_ const char* pat, va_list* args) |
5716 | { | |
5717 | register SV *sv; | |
5718 | new_SV(sv); | |
5719 | sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
5720 | return sv; | |
5721 | } | |
5722 | ||
954c1994 GS |
5723 | /* |
5724 | =for apidoc newSVnv | |
5725 | ||
5726 | Creates a new SV and copies a floating point value into it. | |
5727 | The reference count for the SV is set to 1. | |
5728 | ||
5729 | =cut | |
5730 | */ | |
5731 | ||
c5be433b | 5732 | SV * |
65202027 | 5733 | Perl_newSVnv(pTHX_ NV n) |
79072805 | 5734 | { |
463ee0b2 | 5735 | register SV *sv; |
79072805 | 5736 | |
4561caa4 | 5737 | new_SV(sv); |
79072805 LW |
5738 | sv_setnv(sv,n); |
5739 | return sv; | |
5740 | } | |
5741 | ||
954c1994 GS |
5742 | /* |
5743 | =for apidoc newSViv | |
5744 | ||
5745 | Creates a new SV and copies an integer into it. The reference count for the | |
5746 | SV is set to 1. | |
5747 | ||
5748 | =cut | |
5749 | */ | |
5750 | ||
79072805 | 5751 | SV * |
864dbfa3 | 5752 | Perl_newSViv(pTHX_ IV i) |
79072805 | 5753 | { |
463ee0b2 | 5754 | register SV *sv; |
79072805 | 5755 | |
4561caa4 | 5756 | new_SV(sv); |
79072805 LW |
5757 | sv_setiv(sv,i); |
5758 | return sv; | |
5759 | } | |
5760 | ||
954c1994 | 5761 | /* |
1a3327fb JH |
5762 | =for apidoc newSVuv |
5763 | ||
5764 | Creates a new SV and copies an unsigned integer into it. | |
5765 | The reference count for the SV is set to 1. | |
5766 | ||
5767 | =cut | |
5768 | */ | |
5769 | ||
5770 | SV * | |
5771 | Perl_newSVuv(pTHX_ UV u) | |
5772 | { | |
5773 | register SV *sv; | |
5774 | ||
5775 | new_SV(sv); | |
5776 | sv_setuv(sv,u); | |
5777 | return sv; | |
5778 | } | |
5779 | ||
5780 | /* | |
954c1994 GS |
5781 | =for apidoc newRV_noinc |
5782 | ||
5783 | Creates an RV wrapper for an SV. The reference count for the original | |
5784 | SV is B<not> incremented. | |
5785 | ||
5786 | =cut | |
5787 | */ | |
5788 | ||
2304df62 | 5789 | SV * |
864dbfa3 | 5790 | Perl_newRV_noinc(pTHX_ SV *tmpRef) |
2304df62 AD |
5791 | { |
5792 | register SV *sv; | |
5793 | ||
4561caa4 | 5794 | new_SV(sv); |
2304df62 | 5795 | sv_upgrade(sv, SVt_RV); |
76e3520e | 5796 | SvTEMP_off(tmpRef); |
d689ffdd | 5797 | SvRV(sv) = tmpRef; |
2304df62 | 5798 | SvROK_on(sv); |
2304df62 AD |
5799 | return sv; |
5800 | } | |
5801 | ||
954c1994 | 5802 | /* newRV_inc is #defined to newRV in sv.h */ |
5f05dabc | 5803 | SV * |
864dbfa3 | 5804 | Perl_newRV(pTHX_ SV *tmpRef) |
5f05dabc | 5805 | { |
5f6447b6 | 5806 | return newRV_noinc(SvREFCNT_inc(tmpRef)); |
5f05dabc | 5807 | } |
5f05dabc | 5808 | |
954c1994 GS |
5809 | /* |
5810 | =for apidoc newSVsv | |
5811 | ||
5812 | Creates a new SV which is an exact duplicate of the original SV. | |
5813 | ||
5814 | =cut | |
5815 | */ | |
5816 | ||
79072805 LW |
5817 | /* make an exact duplicate of old */ |
5818 | ||
5819 | SV * | |
864dbfa3 | 5820 | Perl_newSVsv(pTHX_ register SV *old) |
79072805 | 5821 | { |
463ee0b2 | 5822 | register SV *sv; |
79072805 LW |
5823 | |
5824 | if (!old) | |
5825 | return Nullsv; | |
8990e307 | 5826 | if (SvTYPE(old) == SVTYPEMASK) { |
0453d815 PM |
5827 | if (ckWARN_d(WARN_INTERNAL)) |
5828 | Perl_warner(aTHX_ WARN_INTERNAL, "semi-panic: attempt to dup freed string"); | |
79072805 LW |
5829 | return Nullsv; |
5830 | } | |
4561caa4 | 5831 | new_SV(sv); |
ff68c719 | 5832 | if (SvTEMP(old)) { |
5833 | SvTEMP_off(old); | |
463ee0b2 | 5834 | sv_setsv(sv,old); |
ff68c719 | 5835 | SvTEMP_on(old); |
79072805 LW |
5836 | } |
5837 | else | |
463ee0b2 LW |
5838 | sv_setsv(sv,old); |
5839 | return sv; | |
79072805 LW |
5840 | } |
5841 | ||
5842 | void | |
864dbfa3 | 5843 | Perl_sv_reset(pTHX_ register char *s, HV *stash) |
79072805 LW |
5844 | { |
5845 | register HE *entry; | |
5846 | register GV *gv; | |
5847 | register SV *sv; | |
5848 | register I32 i; | |
5849 | register PMOP *pm; | |
5850 | register I32 max; | |
4802d5d7 | 5851 | char todo[PERL_UCHAR_MAX+1]; |
79072805 | 5852 | |
49d8d3a1 MB |
5853 | if (!stash) |
5854 | return; | |
5855 | ||
79072805 LW |
5856 | if (!*s) { /* reset ?? searches */ |
5857 | for (pm = HvPMROOT(stash); pm; pm = pm->op_pmnext) { | |
48c036b1 | 5858 | pm->op_pmdynflags &= ~PMdf_USED; |
79072805 LW |
5859 | } |
5860 | return; | |
5861 | } | |
5862 | ||
5863 | /* reset variables */ | |
5864 | ||
5865 | if (!HvARRAY(stash)) | |
5866 | return; | |
463ee0b2 LW |
5867 | |
5868 | Zero(todo, 256, char); | |
79072805 | 5869 | while (*s) { |
4802d5d7 | 5870 | i = (unsigned char)*s; |
79072805 LW |
5871 | if (s[1] == '-') { |
5872 | s += 2; | |
5873 | } | |
4802d5d7 | 5874 | max = (unsigned char)*s++; |
79072805 | 5875 | for ( ; i <= max; i++) { |
463ee0b2 LW |
5876 | todo[i] = 1; |
5877 | } | |
a0d0e21e | 5878 | for (i = 0; i <= (I32) HvMAX(stash); i++) { |
79072805 | 5879 | for (entry = HvARRAY(stash)[i]; |
9e35f4b3 GS |
5880 | entry; |
5881 | entry = HeNEXT(entry)) | |
5882 | { | |
1edc1566 | 5883 | if (!todo[(U8)*HeKEY(entry)]) |
463ee0b2 | 5884 | continue; |
1edc1566 | 5885 | gv = (GV*)HeVAL(entry); |
79072805 | 5886 | sv = GvSV(gv); |
9e35f4b3 GS |
5887 | if (SvTHINKFIRST(sv)) { |
5888 | if (!SvREADONLY(sv) && SvROK(sv)) | |
5889 | sv_unref(sv); | |
5890 | continue; | |
5891 | } | |
a0d0e21e | 5892 | (void)SvOK_off(sv); |
79072805 LW |
5893 | if (SvTYPE(sv) >= SVt_PV) { |
5894 | SvCUR_set(sv, 0); | |
463ee0b2 LW |
5895 | if (SvPVX(sv) != Nullch) |
5896 | *SvPVX(sv) = '\0'; | |
44a8e56a | 5897 | SvTAINT(sv); |
79072805 LW |
5898 | } |
5899 | if (GvAV(gv)) { | |
5900 | av_clear(GvAV(gv)); | |
5901 | } | |
44a8e56a | 5902 | if (GvHV(gv) && !HvNAME(GvHV(gv))) { |
463ee0b2 | 5903 | hv_clear(GvHV(gv)); |
fa6a1c44 | 5904 | #ifdef USE_ENVIRON_ARRAY |
3280af22 | 5905 | if (gv == PL_envgv) |
79072805 | 5906 | environ[0] = Nullch; |
a0d0e21e | 5907 | #endif |
79072805 LW |
5908 | } |
5909 | } | |
5910 | } | |
5911 | } | |
5912 | } | |
5913 | ||
46fc3d4c | 5914 | IO* |
864dbfa3 | 5915 | Perl_sv_2io(pTHX_ SV *sv) |
46fc3d4c | 5916 | { |
5917 | IO* io; | |
5918 | GV* gv; | |
2d8e6c8d | 5919 | STRLEN n_a; |
46fc3d4c | 5920 | |
5921 | switch (SvTYPE(sv)) { | |
5922 | case SVt_PVIO: | |
5923 | io = (IO*)sv; | |
5924 | break; | |
5925 | case SVt_PVGV: | |
5926 | gv = (GV*)sv; | |
5927 | io = GvIO(gv); | |
5928 | if (!io) | |
cea2e8a9 | 5929 | Perl_croak(aTHX_ "Bad filehandle: %s", GvNAME(gv)); |
46fc3d4c | 5930 | break; |
5931 | default: | |
5932 | if (!SvOK(sv)) | |
cea2e8a9 | 5933 | Perl_croak(aTHX_ PL_no_usym, "filehandle"); |
46fc3d4c | 5934 | if (SvROK(sv)) |
5935 | return sv_2io(SvRV(sv)); | |
2d8e6c8d | 5936 | gv = gv_fetchpv(SvPV(sv,n_a), FALSE, SVt_PVIO); |
46fc3d4c | 5937 | if (gv) |
5938 | io = GvIO(gv); | |
5939 | else | |
5940 | io = 0; | |
5941 | if (!io) | |
cea2e8a9 | 5942 | Perl_croak(aTHX_ "Bad filehandle: %s", SvPV(sv,n_a)); |
46fc3d4c | 5943 | break; |
5944 | } | |
5945 | return io; | |
5946 | } | |
5947 | ||
79072805 | 5948 | CV * |
864dbfa3 | 5949 | Perl_sv_2cv(pTHX_ SV *sv, HV **st, GV **gvp, I32 lref) |
79072805 LW |
5950 | { |
5951 | GV *gv; | |
5952 | CV *cv; | |
2d8e6c8d | 5953 | STRLEN n_a; |
79072805 LW |
5954 | |
5955 | if (!sv) | |
93a17b20 | 5956 | return *gvp = Nullgv, Nullcv; |
79072805 | 5957 | switch (SvTYPE(sv)) { |
79072805 LW |
5958 | case SVt_PVCV: |
5959 | *st = CvSTASH(sv); | |
5960 | *gvp = Nullgv; | |
5961 | return (CV*)sv; | |
5962 | case SVt_PVHV: | |
5963 | case SVt_PVAV: | |
5964 | *gvp = Nullgv; | |
5965 | return Nullcv; | |
8990e307 LW |
5966 | case SVt_PVGV: |
5967 | gv = (GV*)sv; | |
a0d0e21e | 5968 | *gvp = gv; |
8990e307 LW |
5969 | *st = GvESTASH(gv); |
5970 | goto fix_gv; | |
5971 | ||
79072805 | 5972 | default: |
a0d0e21e LW |
5973 | if (SvGMAGICAL(sv)) |
5974 | mg_get(sv); | |
5975 | if (SvROK(sv)) { | |
f5284f61 IZ |
5976 | SV **sp = &sv; /* Used in tryAMAGICunDEREF macro. */ |
5977 | tryAMAGICunDEREF(to_cv); | |
5978 | ||
62f274bf GS |
5979 | sv = SvRV(sv); |
5980 | if (SvTYPE(sv) == SVt_PVCV) { | |
5981 | cv = (CV*)sv; | |
5982 | *gvp = Nullgv; | |
5983 | *st = CvSTASH(cv); | |
5984 | return cv; | |
5985 | } | |
5986 | else if(isGV(sv)) | |
5987 | gv = (GV*)sv; | |
5988 | else | |
cea2e8a9 | 5989 | Perl_croak(aTHX_ "Not a subroutine reference"); |
a0d0e21e | 5990 | } |
62f274bf | 5991 | else if (isGV(sv)) |
79072805 LW |
5992 | gv = (GV*)sv; |
5993 | else | |
2d8e6c8d | 5994 | gv = gv_fetchpv(SvPV(sv, n_a), lref, SVt_PVCV); |
79072805 LW |
5995 | *gvp = gv; |
5996 | if (!gv) | |
5997 | return Nullcv; | |
5998 | *st = GvESTASH(gv); | |
8990e307 | 5999 | fix_gv: |
8ebc5c01 | 6000 | if (lref && !GvCVu(gv)) { |
4633a7c4 | 6001 | SV *tmpsv; |
748a9306 | 6002 | ENTER; |
4633a7c4 | 6003 | tmpsv = NEWSV(704,0); |
16660edb | 6004 | gv_efullname3(tmpsv, gv, Nullch); |
f6ec51f7 GS |
6005 | /* XXX this is probably not what they think they're getting. |
6006 | * It has the same effect as "sub name;", i.e. just a forward | |
6007 | * declaration! */ | |
774d564b | 6008 | newSUB(start_subparse(FALSE, 0), |
4633a7c4 LW |
6009 | newSVOP(OP_CONST, 0, tmpsv), |
6010 | Nullop, | |
8990e307 | 6011 | Nullop); |
748a9306 | 6012 | LEAVE; |
8ebc5c01 | 6013 | if (!GvCVu(gv)) |
cea2e8a9 | 6014 | Perl_croak(aTHX_ "Unable to create sub named \"%s\"", SvPV(sv,n_a)); |
8990e307 | 6015 | } |
8ebc5c01 | 6016 | return GvCVu(gv); |
79072805 LW |
6017 | } |
6018 | } | |
6019 | ||
c461cf8f JH |
6020 | /* |
6021 | =for apidoc sv_true | |
6022 | ||
6023 | Returns true if the SV has a true value by Perl's rules. | |
6024 | ||
6025 | =cut | |
6026 | */ | |
6027 | ||
79072805 | 6028 | I32 |
864dbfa3 | 6029 | Perl_sv_true(pTHX_ register SV *sv) |
79072805 | 6030 | { |
8990e307 LW |
6031 | if (!sv) |
6032 | return 0; | |
79072805 | 6033 | if (SvPOK(sv)) { |
4e35701f NIS |
6034 | register XPV* tXpv; |
6035 | if ((tXpv = (XPV*)SvANY(sv)) && | |
c2f1de04 | 6036 | (tXpv->xpv_cur > 1 || |
4e35701f | 6037 | (tXpv->xpv_cur && *tXpv->xpv_pv != '0'))) |
79072805 LW |
6038 | return 1; |
6039 | else | |
6040 | return 0; | |
6041 | } | |
6042 | else { | |
6043 | if (SvIOK(sv)) | |
463ee0b2 | 6044 | return SvIVX(sv) != 0; |
79072805 LW |
6045 | else { |
6046 | if (SvNOK(sv)) | |
463ee0b2 | 6047 | return SvNVX(sv) != 0.0; |
79072805 | 6048 | else |
463ee0b2 | 6049 | return sv_2bool(sv); |
79072805 LW |
6050 | } |
6051 | } | |
6052 | } | |
79072805 | 6053 | |
ff68c719 | 6054 | IV |
864dbfa3 | 6055 | Perl_sv_iv(pTHX_ register SV *sv) |
85e6fe83 | 6056 | { |
25da4f38 IZ |
6057 | if (SvIOK(sv)) { |
6058 | if (SvIsUV(sv)) | |
6059 | return (IV)SvUVX(sv); | |
ff68c719 | 6060 | return SvIVX(sv); |
25da4f38 | 6061 | } |
ff68c719 | 6062 | return sv_2iv(sv); |
85e6fe83 | 6063 | } |
85e6fe83 | 6064 | |
ff68c719 | 6065 | UV |
864dbfa3 | 6066 | Perl_sv_uv(pTHX_ register SV *sv) |
ff68c719 | 6067 | { |
25da4f38 IZ |
6068 | if (SvIOK(sv)) { |
6069 | if (SvIsUV(sv)) | |
6070 | return SvUVX(sv); | |
6071 | return (UV)SvIVX(sv); | |
6072 | } | |
ff68c719 | 6073 | return sv_2uv(sv); |
6074 | } | |
85e6fe83 | 6075 | |
65202027 | 6076 | NV |
864dbfa3 | 6077 | Perl_sv_nv(pTHX_ register SV *sv) |
79072805 | 6078 | { |
ff68c719 | 6079 | if (SvNOK(sv)) |
6080 | return SvNVX(sv); | |
6081 | return sv_2nv(sv); | |
79072805 | 6082 | } |
79072805 | 6083 | |
79072805 | 6084 | char * |
864dbfa3 | 6085 | Perl_sv_pv(pTHX_ SV *sv) |
1fa8b10d JD |
6086 | { |
6087 | STRLEN n_a; | |
6088 | ||
6089 | if (SvPOK(sv)) | |
6090 | return SvPVX(sv); | |
6091 | ||
6092 | return sv_2pv(sv, &n_a); | |
6093 | } | |
6094 | ||
6095 | char * | |
864dbfa3 | 6096 | Perl_sv_pvn(pTHX_ SV *sv, STRLEN *lp) |
79072805 | 6097 | { |
85e6fe83 LW |
6098 | if (SvPOK(sv)) { |
6099 | *lp = SvCUR(sv); | |
a0d0e21e | 6100 | return SvPVX(sv); |
85e6fe83 | 6101 | } |
463ee0b2 | 6102 | return sv_2pv(sv, lp); |
79072805 | 6103 | } |
79072805 | 6104 | |
c461cf8f JH |
6105 | /* |
6106 | =for apidoc sv_pvn_force | |
6107 | ||
6108 | Get a sensible string out of the SV somehow. | |
6109 | ||
6110 | =cut | |
6111 | */ | |
6112 | ||
a0d0e21e | 6113 | char * |
864dbfa3 | 6114 | Perl_sv_pvn_force(pTHX_ SV *sv, STRLEN *lp) |
a0d0e21e LW |
6115 | { |
6116 | char *s; | |
6117 | ||
6fc92669 GS |
6118 | if (SvTHINKFIRST(sv) && !SvROK(sv)) |
6119 | sv_force_normal(sv); | |
1c846c1f | 6120 | |
a0d0e21e LW |
6121 | if (SvPOK(sv)) { |
6122 | *lp = SvCUR(sv); | |
6123 | } | |
6124 | else { | |
748a9306 | 6125 | if (SvTYPE(sv) > SVt_PVLV && SvTYPE(sv) != SVt_PVFM) { |
cea2e8a9 | 6126 | Perl_croak(aTHX_ "Can't coerce %s to string in %s", sv_reftype(sv,0), |
6fc92669 | 6127 | PL_op_name[PL_op->op_type]); |
a0d0e21e | 6128 | } |
4633a7c4 LW |
6129 | else |
6130 | s = sv_2pv(sv, lp); | |
a0d0e21e LW |
6131 | if (s != SvPVX(sv)) { /* Almost, but not quite, sv_setpvn() */ |
6132 | STRLEN len = *lp; | |
1c846c1f | 6133 | |
a0d0e21e LW |
6134 | if (SvROK(sv)) |
6135 | sv_unref(sv); | |
6136 | (void)SvUPGRADE(sv, SVt_PV); /* Never FALSE */ | |
6137 | SvGROW(sv, len + 1); | |
6138 | Move(s,SvPVX(sv),len,char); | |
6139 | SvCUR_set(sv, len); | |
6140 | *SvEND(sv) = '\0'; | |
6141 | } | |
6142 | if (!SvPOK(sv)) { | |
6143 | SvPOK_on(sv); /* validate pointer */ | |
6144 | SvTAINT(sv); | |
1d7c1841 GS |
6145 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n", |
6146 | PTR2UV(sv),SvPVX(sv))); | |
a0d0e21e LW |
6147 | } |
6148 | } | |
6149 | return SvPVX(sv); | |
6150 | } | |
6151 | ||
6152 | char * | |
7340a771 GS |
6153 | Perl_sv_pvbyte(pTHX_ SV *sv) |
6154 | { | |
6155 | return sv_pv(sv); | |
6156 | } | |
6157 | ||
6158 | char * | |
6159 | Perl_sv_pvbyten(pTHX_ SV *sv, STRLEN *lp) | |
6160 | { | |
6161 | return sv_pvn(sv,lp); | |
6162 | } | |
6163 | ||
6164 | char * | |
6165 | Perl_sv_pvbyten_force(pTHX_ SV *sv, STRLEN *lp) | |
6166 | { | |
6167 | return sv_pvn_force(sv,lp); | |
6168 | } | |
6169 | ||
6170 | char * | |
6171 | Perl_sv_pvutf8(pTHX_ SV *sv) | |
6172 | { | |
560a288e | 6173 | sv_utf8_upgrade(sv); |
7340a771 GS |
6174 | return sv_pv(sv); |
6175 | } | |
6176 | ||
6177 | char * | |
6178 | Perl_sv_pvutf8n(pTHX_ SV *sv, STRLEN *lp) | |
6179 | { | |
560a288e | 6180 | sv_utf8_upgrade(sv); |
7340a771 GS |
6181 | return sv_pvn(sv,lp); |
6182 | } | |
6183 | ||
c461cf8f JH |
6184 | /* |
6185 | =for apidoc sv_pvutf8n_force | |
6186 | ||
6187 | Get a sensible UTF8-encoded string out of the SV somehow. See | |
6188 | L</sv_pvn_force>. | |
6189 | ||
6190 | =cut | |
6191 | */ | |
6192 | ||
7340a771 GS |
6193 | char * |
6194 | Perl_sv_pvutf8n_force(pTHX_ SV *sv, STRLEN *lp) | |
6195 | { | |
560a288e | 6196 | sv_utf8_upgrade(sv); |
7340a771 GS |
6197 | return sv_pvn_force(sv,lp); |
6198 | } | |
6199 | ||
c461cf8f JH |
6200 | /* |
6201 | =for apidoc sv_reftype | |
6202 | ||
6203 | Returns a string describing what the SV is a reference to. | |
6204 | ||
6205 | =cut | |
6206 | */ | |
6207 | ||
7340a771 | 6208 | char * |
864dbfa3 | 6209 | Perl_sv_reftype(pTHX_ SV *sv, int ob) |
a0d0e21e LW |
6210 | { |
6211 | if (ob && SvOBJECT(sv)) | |
6212 | return HvNAME(SvSTASH(sv)); | |
6213 | else { | |
6214 | switch (SvTYPE(sv)) { | |
6215 | case SVt_NULL: | |
6216 | case SVt_IV: | |
6217 | case SVt_NV: | |
6218 | case SVt_RV: | |
6219 | case SVt_PV: | |
6220 | case SVt_PVIV: | |
6221 | case SVt_PVNV: | |
6222 | case SVt_PVMG: | |
6223 | case SVt_PVBM: | |
6224 | if (SvROK(sv)) | |
6225 | return "REF"; | |
6226 | else | |
6227 | return "SCALAR"; | |
6228 | case SVt_PVLV: return "LVALUE"; | |
6229 | case SVt_PVAV: return "ARRAY"; | |
6230 | case SVt_PVHV: return "HASH"; | |
6231 | case SVt_PVCV: return "CODE"; | |
6232 | case SVt_PVGV: return "GLOB"; | |
1d2dff63 | 6233 | case SVt_PVFM: return "FORMAT"; |
27f9d8f3 | 6234 | case SVt_PVIO: return "IO"; |
a0d0e21e LW |
6235 | default: return "UNKNOWN"; |
6236 | } | |
6237 | } | |
6238 | } | |
6239 | ||
954c1994 GS |
6240 | /* |
6241 | =for apidoc sv_isobject | |
6242 | ||
6243 | Returns a boolean indicating whether the SV is an RV pointing to a blessed | |
6244 | object. If the SV is not an RV, or if the object is not blessed, then this | |
6245 | will return false. | |
6246 | ||
6247 | =cut | |
6248 | */ | |
6249 | ||
463ee0b2 | 6250 | int |
864dbfa3 | 6251 | Perl_sv_isobject(pTHX_ SV *sv) |
85e6fe83 | 6252 | { |
68dc0745 | 6253 | if (!sv) |
6254 | return 0; | |
6255 | if (SvGMAGICAL(sv)) | |
6256 | mg_get(sv); | |
85e6fe83 LW |
6257 | if (!SvROK(sv)) |
6258 | return 0; | |
6259 | sv = (SV*)SvRV(sv); | |
6260 | if (!SvOBJECT(sv)) | |
6261 | return 0; | |
6262 | return 1; | |
6263 | } | |
6264 | ||
954c1994 GS |
6265 | /* |
6266 | =for apidoc sv_isa | |
6267 | ||
6268 | Returns a boolean indicating whether the SV is blessed into the specified | |
6269 | class. This does not check for subtypes; use C<sv_derived_from> to verify | |
6270 | an inheritance relationship. | |
6271 | ||
6272 | =cut | |
6273 | */ | |
6274 | ||
85e6fe83 | 6275 | int |
864dbfa3 | 6276 | Perl_sv_isa(pTHX_ SV *sv, const char *name) |
463ee0b2 | 6277 | { |
68dc0745 | 6278 | if (!sv) |
6279 | return 0; | |
6280 | if (SvGMAGICAL(sv)) | |
6281 | mg_get(sv); | |
ed6116ce | 6282 | if (!SvROK(sv)) |
463ee0b2 | 6283 | return 0; |
ed6116ce LW |
6284 | sv = (SV*)SvRV(sv); |
6285 | if (!SvOBJECT(sv)) | |
463ee0b2 LW |
6286 | return 0; |
6287 | ||
6288 | return strEQ(HvNAME(SvSTASH(sv)), name); | |
6289 | } | |
6290 | ||
954c1994 GS |
6291 | /* |
6292 | =for apidoc newSVrv | |
6293 | ||
6294 | Creates a new SV for the RV, C<rv>, to point to. If C<rv> is not an RV then | |
6295 | it will be upgraded to one. If C<classname> is non-null then the new SV will | |
6296 | be blessed in the specified package. The new SV is returned and its | |
6297 | reference count is 1. | |
6298 | ||
6299 | =cut | |
6300 | */ | |
6301 | ||
463ee0b2 | 6302 | SV* |
864dbfa3 | 6303 | Perl_newSVrv(pTHX_ SV *rv, const char *classname) |
463ee0b2 | 6304 | { |
463ee0b2 LW |
6305 | SV *sv; |
6306 | ||
4561caa4 | 6307 | new_SV(sv); |
51cf62d8 | 6308 | |
2213622d | 6309 | SV_CHECK_THINKFIRST(rv); |
51cf62d8 | 6310 | SvAMAGIC_off(rv); |
51cf62d8 | 6311 | |
0199fce9 JD |
6312 | if (SvTYPE(rv) >= SVt_PVMG) { |
6313 | U32 refcnt = SvREFCNT(rv); | |
6314 | SvREFCNT(rv) = 0; | |
6315 | sv_clear(rv); | |
6316 | SvFLAGS(rv) = 0; | |
6317 | SvREFCNT(rv) = refcnt; | |
6318 | } | |
6319 | ||
51cf62d8 | 6320 | if (SvTYPE(rv) < SVt_RV) |
0199fce9 JD |
6321 | sv_upgrade(rv, SVt_RV); |
6322 | else if (SvTYPE(rv) > SVt_RV) { | |
6323 | (void)SvOOK_off(rv); | |
6324 | if (SvPVX(rv) && SvLEN(rv)) | |
6325 | Safefree(SvPVX(rv)); | |
6326 | SvCUR_set(rv, 0); | |
6327 | SvLEN_set(rv, 0); | |
6328 | } | |
51cf62d8 OT |
6329 | |
6330 | (void)SvOK_off(rv); | |
053fc874 | 6331 | SvRV(rv) = sv; |
ed6116ce | 6332 | SvROK_on(rv); |
463ee0b2 | 6333 | |
a0d0e21e LW |
6334 | if (classname) { |
6335 | HV* stash = gv_stashpv(classname, TRUE); | |
6336 | (void)sv_bless(rv, stash); | |
6337 | } | |
6338 | return sv; | |
6339 | } | |
6340 | ||
954c1994 GS |
6341 | /* |
6342 | =for apidoc sv_setref_pv | |
6343 | ||
6344 | Copies a pointer into a new SV, optionally blessing the SV. The C<rv> | |
6345 | argument will be upgraded to an RV. That RV will be modified to point to | |
6346 | the new SV. If the C<pv> argument is NULL then C<PL_sv_undef> will be placed | |
6347 | into the SV. The C<classname> argument indicates the package for the | |
6348 | blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV | |
6349 | will be returned and will have a reference count of 1. | |
6350 | ||
6351 | Do not use with other Perl types such as HV, AV, SV, CV, because those | |
6352 | objects will become corrupted by the pointer copy process. | |
6353 | ||
6354 | Note that C<sv_setref_pvn> copies the string while this copies the pointer. | |
6355 | ||
6356 | =cut | |
6357 | */ | |
6358 | ||
a0d0e21e | 6359 | SV* |
864dbfa3 | 6360 | Perl_sv_setref_pv(pTHX_ SV *rv, const char *classname, void *pv) |
a0d0e21e | 6361 | { |
189b2af5 | 6362 | if (!pv) { |
3280af22 | 6363 | sv_setsv(rv, &PL_sv_undef); |
189b2af5 GS |
6364 | SvSETMAGIC(rv); |
6365 | } | |
a0d0e21e | 6366 | else |
56431972 | 6367 | sv_setiv(newSVrv(rv,classname), PTR2IV(pv)); |
a0d0e21e LW |
6368 | return rv; |
6369 | } | |
6370 | ||
954c1994 GS |
6371 | /* |
6372 | =for apidoc sv_setref_iv | |
6373 | ||
6374 | Copies an integer into a new SV, optionally blessing the SV. The C<rv> | |
6375 | argument will be upgraded to an RV. That RV will be modified to point to | |
6376 | the new SV. The C<classname> argument indicates the package for the | |
6377 | blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV | |
6378 | will be returned and will have a reference count of 1. | |
6379 | ||
6380 | =cut | |
6381 | */ | |
6382 | ||
a0d0e21e | 6383 | SV* |
864dbfa3 | 6384 | Perl_sv_setref_iv(pTHX_ SV *rv, const char *classname, IV iv) |
a0d0e21e LW |
6385 | { |
6386 | sv_setiv(newSVrv(rv,classname), iv); | |
6387 | return rv; | |
6388 | } | |
6389 | ||
954c1994 | 6390 | /* |
e1c57cef JH |
6391 | =for apidoc sv_setref_uv |
6392 | ||
6393 | Copies an unsigned integer into a new SV, optionally blessing the SV. The C<rv> | |
6394 | argument will be upgraded to an RV. That RV will be modified to point to | |
6395 | the new SV. The C<classname> argument indicates the package for the | |
6396 | blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV | |
6397 | will be returned and will have a reference count of 1. | |
6398 | ||
6399 | =cut | |
6400 | */ | |
6401 | ||
6402 | SV* | |
6403 | Perl_sv_setref_uv(pTHX_ SV *rv, const char *classname, UV uv) | |
6404 | { | |
6405 | sv_setuv(newSVrv(rv,classname), uv); | |
6406 | return rv; | |
6407 | } | |
6408 | ||
6409 | /* | |
954c1994 GS |
6410 | =for apidoc sv_setref_nv |
6411 | ||
6412 | Copies a double into a new SV, optionally blessing the SV. The C<rv> | |
6413 | argument will be upgraded to an RV. That RV will be modified to point to | |
6414 | the new SV. The C<classname> argument indicates the package for the | |
6415 | blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV | |
6416 | will be returned and will have a reference count of 1. | |
6417 | ||
6418 | =cut | |
6419 | */ | |
6420 | ||
a0d0e21e | 6421 | SV* |
65202027 | 6422 | Perl_sv_setref_nv(pTHX_ SV *rv, const char *classname, NV nv) |
a0d0e21e LW |
6423 | { |
6424 | sv_setnv(newSVrv(rv,classname), nv); | |
6425 | return rv; | |
6426 | } | |
463ee0b2 | 6427 | |
954c1994 GS |
6428 | /* |
6429 | =for apidoc sv_setref_pvn | |
6430 | ||
6431 | Copies a string into a new SV, optionally blessing the SV. The length of the | |
6432 | string must be specified with C<n>. The C<rv> argument will be upgraded to | |
6433 | an RV. That RV will be modified to point to the new SV. The C<classname> | |
6434 | argument indicates the package for the blessing. Set C<classname> to | |
6435 | C<Nullch> to avoid the blessing. The new SV will be returned and will have | |
6436 | a reference count of 1. | |
6437 | ||
6438 | Note that C<sv_setref_pv> copies the pointer while this copies the string. | |
6439 | ||
6440 | =cut | |
6441 | */ | |
6442 | ||
a0d0e21e | 6443 | SV* |
864dbfa3 | 6444 | Perl_sv_setref_pvn(pTHX_ SV *rv, const char *classname, char *pv, STRLEN n) |
a0d0e21e LW |
6445 | { |
6446 | sv_setpvn(newSVrv(rv,classname), pv, n); | |
463ee0b2 LW |
6447 | return rv; |
6448 | } | |
6449 | ||
954c1994 GS |
6450 | /* |
6451 | =for apidoc sv_bless | |
6452 | ||
6453 | Blesses an SV into a specified package. The SV must be an RV. The package | |
6454 | must be designated by its stash (see C<gv_stashpv()>). The reference count | |
6455 | of the SV is unaffected. | |
6456 | ||
6457 | =cut | |
6458 | */ | |
6459 | ||
a0d0e21e | 6460 | SV* |
864dbfa3 | 6461 | Perl_sv_bless(pTHX_ SV *sv, HV *stash) |
a0d0e21e | 6462 | { |
76e3520e | 6463 | SV *tmpRef; |
a0d0e21e | 6464 | if (!SvROK(sv)) |
cea2e8a9 | 6465 | Perl_croak(aTHX_ "Can't bless non-reference value"); |
76e3520e GS |
6466 | tmpRef = SvRV(sv); |
6467 | if (SvFLAGS(tmpRef) & (SVs_OBJECT|SVf_READONLY)) { | |
6468 | if (SvREADONLY(tmpRef)) | |
cea2e8a9 | 6469 | Perl_croak(aTHX_ PL_no_modify); |
76e3520e GS |
6470 | if (SvOBJECT(tmpRef)) { |
6471 | if (SvTYPE(tmpRef) != SVt_PVIO) | |
3280af22 | 6472 | --PL_sv_objcount; |
76e3520e | 6473 | SvREFCNT_dec(SvSTASH(tmpRef)); |
2e3febc6 | 6474 | } |
a0d0e21e | 6475 | } |
76e3520e GS |
6476 | SvOBJECT_on(tmpRef); |
6477 | if (SvTYPE(tmpRef) != SVt_PVIO) | |
3280af22 | 6478 | ++PL_sv_objcount; |
76e3520e GS |
6479 | (void)SvUPGRADE(tmpRef, SVt_PVMG); |
6480 | SvSTASH(tmpRef) = (HV*)SvREFCNT_inc(stash); | |
a0d0e21e | 6481 | |
2e3febc6 CS |
6482 | if (Gv_AMG(stash)) |
6483 | SvAMAGIC_on(sv); | |
6484 | else | |
6485 | SvAMAGIC_off(sv); | |
a0d0e21e LW |
6486 | |
6487 | return sv; | |
6488 | } | |
6489 | ||
76e3520e | 6490 | STATIC void |
cea2e8a9 | 6491 | S_sv_unglob(pTHX_ SV *sv) |
a0d0e21e | 6492 | { |
850fabdf GS |
6493 | void *xpvmg; |
6494 | ||
a0d0e21e LW |
6495 | assert(SvTYPE(sv) == SVt_PVGV); |
6496 | SvFAKE_off(sv); | |
6497 | if (GvGP(sv)) | |
1edc1566 | 6498 | gp_free((GV*)sv); |
e826b3c7 GS |
6499 | if (GvSTASH(sv)) { |
6500 | SvREFCNT_dec(GvSTASH(sv)); | |
6501 | GvSTASH(sv) = Nullhv; | |
6502 | } | |
a0d0e21e LW |
6503 | sv_unmagic(sv, '*'); |
6504 | Safefree(GvNAME(sv)); | |
a5f75d66 | 6505 | GvMULTI_off(sv); |
850fabdf GS |
6506 | |
6507 | /* need to keep SvANY(sv) in the right arena */ | |
6508 | xpvmg = new_XPVMG(); | |
6509 | StructCopy(SvANY(sv), xpvmg, XPVMG); | |
6510 | del_XPVGV(SvANY(sv)); | |
6511 | SvANY(sv) = xpvmg; | |
6512 | ||
a0d0e21e LW |
6513 | SvFLAGS(sv) &= ~SVTYPEMASK; |
6514 | SvFLAGS(sv) |= SVt_PVMG; | |
6515 | } | |
6516 | ||
954c1994 | 6517 | /* |
840a7b70 | 6518 | =for apidoc sv_unref_flags |
954c1994 GS |
6519 | |
6520 | Unsets the RV status of the SV, and decrements the reference count of | |
6521 | whatever was being referenced by the RV. This can almost be thought of | |
840a7b70 IZ |
6522 | as a reversal of C<newSVrv>. The C<cflags> argument can contain |
6523 | C<SV_IMMEDIATE_UNREF> to force the reference count to be decremented | |
6524 | (otherwise the decrementing is conditional on the reference count being | |
6525 | different from one or the reference being a readonly SV). | |
7889fe52 | 6526 | See C<SvROK_off>. |
954c1994 GS |
6527 | |
6528 | =cut | |
6529 | */ | |
6530 | ||
ed6116ce | 6531 | void |
840a7b70 | 6532 | Perl_sv_unref_flags(pTHX_ SV *sv, U32 flags) |
ed6116ce | 6533 | { |
a0d0e21e | 6534 | SV* rv = SvRV(sv); |
810b8aa5 GS |
6535 | |
6536 | if (SvWEAKREF(sv)) { | |
6537 | sv_del_backref(sv); | |
6538 | SvWEAKREF_off(sv); | |
6539 | SvRV(sv) = 0; | |
6540 | return; | |
6541 | } | |
ed6116ce LW |
6542 | SvRV(sv) = 0; |
6543 | SvROK_off(sv); | |
840a7b70 | 6544 | if (SvREFCNT(rv) != 1 || SvREADONLY(rv) || flags) /* SV_IMMEDIATE_UNREF */ |
4633a7c4 | 6545 | SvREFCNT_dec(rv); |
840a7b70 | 6546 | else /* XXX Hack, but hard to make $a=$a->[1] work otherwise */ |
4633a7c4 | 6547 | sv_2mortal(rv); /* Schedule for freeing later */ |
ed6116ce | 6548 | } |
8990e307 | 6549 | |
840a7b70 IZ |
6550 | /* |
6551 | =for apidoc sv_unref | |
6552 | ||
6553 | Unsets the RV status of the SV, and decrements the reference count of | |
6554 | whatever was being referenced by the RV. This can almost be thought of | |
6555 | as a reversal of C<newSVrv>. This is C<sv_unref_flags> with the C<flag> | |
7889fe52 | 6556 | being zero. See C<SvROK_off>. |
840a7b70 IZ |
6557 | |
6558 | =cut | |
6559 | */ | |
6560 | ||
6561 | void | |
6562 | Perl_sv_unref(pTHX_ SV *sv) | |
6563 | { | |
6564 | sv_unref_flags(sv, 0); | |
6565 | } | |
6566 | ||
bbce6d69 | 6567 | void |
864dbfa3 | 6568 | Perl_sv_taint(pTHX_ SV *sv) |
bbce6d69 | 6569 | { |
6570 | sv_magic((sv), Nullsv, 't', Nullch, 0); | |
6571 | } | |
6572 | ||
6573 | void | |
864dbfa3 | 6574 | Perl_sv_untaint(pTHX_ SV *sv) |
bbce6d69 | 6575 | { |
13f57bf8 | 6576 | if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) { |
36477c24 | 6577 | MAGIC *mg = mg_find(sv, 't'); |
6578 | if (mg) | |
565764a8 | 6579 | mg->mg_len &= ~1; |
36477c24 | 6580 | } |
bbce6d69 | 6581 | } |
6582 | ||
6583 | bool | |
864dbfa3 | 6584 | Perl_sv_tainted(pTHX_ SV *sv) |
bbce6d69 | 6585 | { |
13f57bf8 | 6586 | if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) { |
36477c24 | 6587 | MAGIC *mg = mg_find(sv, 't'); |
155aba94 | 6588 | if (mg && ((mg->mg_len & 1) || ((mg->mg_len & 2) && mg->mg_obj == sv))) |
36477c24 | 6589 | return TRUE; |
6590 | } | |
6591 | return FALSE; | |
bbce6d69 | 6592 | } |
6593 | ||
954c1994 GS |
6594 | /* |
6595 | =for apidoc sv_setpviv | |
6596 | ||
6597 | Copies an integer into the given SV, also updating its string value. | |
6598 | Does not handle 'set' magic. See C<sv_setpviv_mg>. | |
6599 | ||
6600 | =cut | |
6601 | */ | |
6602 | ||
84902520 | 6603 | void |
864dbfa3 | 6604 | Perl_sv_setpviv(pTHX_ SV *sv, IV iv) |
84902520 | 6605 | { |
25da4f38 IZ |
6606 | char buf[TYPE_CHARS(UV)]; |
6607 | char *ebuf; | |
6608 | char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf); | |
84902520 | 6609 | |
25da4f38 | 6610 | sv_setpvn(sv, ptr, ebuf - ptr); |
84902520 TB |
6611 | } |
6612 | ||
ef50df4b | 6613 | |
954c1994 GS |
6614 | /* |
6615 | =for apidoc sv_setpviv_mg | |
6616 | ||
6617 | Like C<sv_setpviv>, but also handles 'set' magic. | |
6618 | ||
6619 | =cut | |
6620 | */ | |
6621 | ||
ef50df4b | 6622 | void |
864dbfa3 | 6623 | Perl_sv_setpviv_mg(pTHX_ SV *sv, IV iv) |
ef50df4b | 6624 | { |
25da4f38 IZ |
6625 | char buf[TYPE_CHARS(UV)]; |
6626 | char *ebuf; | |
6627 | char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf); | |
6628 | ||
6629 | sv_setpvn(sv, ptr, ebuf - ptr); | |
ef50df4b GS |
6630 | SvSETMAGIC(sv); |
6631 | } | |
6632 | ||
cea2e8a9 GS |
6633 | #if defined(PERL_IMPLICIT_CONTEXT) |
6634 | void | |
6635 | Perl_sv_setpvf_nocontext(SV *sv, const char* pat, ...) | |
6636 | { | |
6637 | dTHX; | |
6638 | va_list args; | |
6639 | va_start(args, pat); | |
c5be433b | 6640 | sv_vsetpvf(sv, pat, &args); |
cea2e8a9 GS |
6641 | va_end(args); |
6642 | } | |
6643 | ||
6644 | ||
6645 | void | |
6646 | Perl_sv_setpvf_mg_nocontext(SV *sv, const char* pat, ...) | |
6647 | { | |
6648 | dTHX; | |
6649 | va_list args; | |
6650 | va_start(args, pat); | |
c5be433b | 6651 | sv_vsetpvf_mg(sv, pat, &args); |
cea2e8a9 | 6652 | va_end(args); |
cea2e8a9 GS |
6653 | } |
6654 | #endif | |
6655 | ||
954c1994 GS |
6656 | /* |
6657 | =for apidoc sv_setpvf | |
6658 | ||
6659 | Processes its arguments like C<sprintf> and sets an SV to the formatted | |
6660 | output. Does not handle 'set' magic. See C<sv_setpvf_mg>. | |
6661 | ||
6662 | =cut | |
6663 | */ | |
6664 | ||
46fc3d4c | 6665 | void |
864dbfa3 | 6666 | Perl_sv_setpvf(pTHX_ SV *sv, const char* pat, ...) |
46fc3d4c | 6667 | { |
6668 | va_list args; | |
46fc3d4c | 6669 | va_start(args, pat); |
c5be433b | 6670 | sv_vsetpvf(sv, pat, &args); |
46fc3d4c | 6671 | va_end(args); |
6672 | } | |
6673 | ||
c5be433b GS |
6674 | void |
6675 | Perl_sv_vsetpvf(pTHX_ SV *sv, const char* pat, va_list* args) | |
6676 | { | |
6677 | sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
6678 | } | |
ef50df4b | 6679 | |
954c1994 GS |
6680 | /* |
6681 | =for apidoc sv_setpvf_mg | |
6682 | ||
6683 | Like C<sv_setpvf>, but also handles 'set' magic. | |
6684 | ||
6685 | =cut | |
6686 | */ | |
6687 | ||
ef50df4b | 6688 | void |
864dbfa3 | 6689 | Perl_sv_setpvf_mg(pTHX_ SV *sv, const char* pat, ...) |
ef50df4b GS |
6690 | { |
6691 | va_list args; | |
ef50df4b | 6692 | va_start(args, pat); |
c5be433b | 6693 | sv_vsetpvf_mg(sv, pat, &args); |
ef50df4b | 6694 | va_end(args); |
c5be433b GS |
6695 | } |
6696 | ||
6697 | void | |
6698 | Perl_sv_vsetpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args) | |
6699 | { | |
6700 | sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
ef50df4b GS |
6701 | SvSETMAGIC(sv); |
6702 | } | |
6703 | ||
cea2e8a9 GS |
6704 | #if defined(PERL_IMPLICIT_CONTEXT) |
6705 | void | |
6706 | Perl_sv_catpvf_nocontext(SV *sv, const char* pat, ...) | |
6707 | { | |
6708 | dTHX; | |
6709 | va_list args; | |
6710 | va_start(args, pat); | |
c5be433b | 6711 | sv_vcatpvf(sv, pat, &args); |
cea2e8a9 GS |
6712 | va_end(args); |
6713 | } | |
6714 | ||
6715 | void | |
6716 | Perl_sv_catpvf_mg_nocontext(SV *sv, const char* pat, ...) | |
6717 | { | |
6718 | dTHX; | |
6719 | va_list args; | |
6720 | va_start(args, pat); | |
c5be433b | 6721 | sv_vcatpvf_mg(sv, pat, &args); |
cea2e8a9 | 6722 | va_end(args); |
cea2e8a9 GS |
6723 | } |
6724 | #endif | |
6725 | ||
954c1994 GS |
6726 | /* |
6727 | =for apidoc sv_catpvf | |
6728 | ||
6729 | Processes its arguments like C<sprintf> and appends the formatted output | |
6730 | to an SV. Handles 'get' magic, but not 'set' magic. C<SvSETMAGIC()> must | |
6731 | typically be called after calling this function to handle 'set' magic. | |
6732 | ||
6733 | =cut | |
6734 | */ | |
6735 | ||
46fc3d4c | 6736 | void |
864dbfa3 | 6737 | Perl_sv_catpvf(pTHX_ SV *sv, const char* pat, ...) |
46fc3d4c | 6738 | { |
6739 | va_list args; | |
46fc3d4c | 6740 | va_start(args, pat); |
c5be433b | 6741 | sv_vcatpvf(sv, pat, &args); |
46fc3d4c | 6742 | va_end(args); |
6743 | } | |
6744 | ||
ef50df4b | 6745 | void |
c5be433b GS |
6746 | Perl_sv_vcatpvf(pTHX_ SV *sv, const char* pat, va_list* args) |
6747 | { | |
6748 | sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
6749 | } | |
6750 | ||
954c1994 GS |
6751 | /* |
6752 | =for apidoc sv_catpvf_mg | |
6753 | ||
6754 | Like C<sv_catpvf>, but also handles 'set' magic. | |
6755 | ||
6756 | =cut | |
6757 | */ | |
6758 | ||
c5be433b | 6759 | void |
864dbfa3 | 6760 | Perl_sv_catpvf_mg(pTHX_ SV *sv, const char* pat, ...) |
ef50df4b GS |
6761 | { |
6762 | va_list args; | |
ef50df4b | 6763 | va_start(args, pat); |
c5be433b | 6764 | sv_vcatpvf_mg(sv, pat, &args); |
ef50df4b | 6765 | va_end(args); |
c5be433b GS |
6766 | } |
6767 | ||
6768 | void | |
6769 | Perl_sv_vcatpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args) | |
6770 | { | |
6771 | sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
ef50df4b GS |
6772 | SvSETMAGIC(sv); |
6773 | } | |
6774 | ||
954c1994 GS |
6775 | /* |
6776 | =for apidoc sv_vsetpvfn | |
6777 | ||
6778 | Works like C<vcatpvfn> but copies the text into the SV instead of | |
6779 | appending it. | |
6780 | ||
6781 | =cut | |
6782 | */ | |
6783 | ||
46fc3d4c | 6784 | void |
7d5ea4e7 | 6785 | Perl_sv_vsetpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted) |
46fc3d4c | 6786 | { |
6787 | sv_setpvn(sv, "", 0); | |
7d5ea4e7 | 6788 | sv_vcatpvfn(sv, pat, patlen, args, svargs, svmax, maybe_tainted); |
46fc3d4c | 6789 | } |
6790 | ||
2d00ba3b | 6791 | STATIC I32 |
9dd79c3f | 6792 | S_expect_number(pTHX_ char** pattern) |
211dfcf1 HS |
6793 | { |
6794 | I32 var = 0; | |
6795 | switch (**pattern) { | |
6796 | case '1': case '2': case '3': | |
6797 | case '4': case '5': case '6': | |
6798 | case '7': case '8': case '9': | |
6799 | while (isDIGIT(**pattern)) | |
6800 | var = var * 10 + (*(*pattern)++ - '0'); | |
6801 | } | |
6802 | return var; | |
6803 | } | |
9dd79c3f | 6804 | #define EXPECT_NUMBER(pattern, var) (var = S_expect_number(aTHX_ &pattern)) |
211dfcf1 | 6805 | |
954c1994 GS |
6806 | /* |
6807 | =for apidoc sv_vcatpvfn | |
6808 | ||
6809 | Processes its arguments like C<vsprintf> and appends the formatted output | |
6810 | to an SV. Uses an array of SVs if the C style variable argument list is | |
6811 | missing (NULL). When running with taint checks enabled, indicates via | |
6812 | C<maybe_tainted> if results are untrustworthy (often due to the use of | |
6813 | locales). | |
6814 | ||
6815 | =cut | |
6816 | */ | |
6817 | ||
46fc3d4c | 6818 | void |
7d5ea4e7 | 6819 | Perl_sv_vcatpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted) |
46fc3d4c | 6820 | { |
6821 | char *p; | |
6822 | char *q; | |
6823 | char *patend; | |
fc36a67e | 6824 | STRLEN origlen; |
46fc3d4c | 6825 | I32 svix = 0; |
c635e13b | 6826 | static char nullstr[] = "(null)"; |
7e2040f0 | 6827 | SV *argsv; |
46fc3d4c | 6828 | |
6829 | /* no matter what, this is a string now */ | |
fc36a67e | 6830 | (void)SvPV_force(sv, origlen); |
46fc3d4c | 6831 | |
fc36a67e | 6832 | /* special-case "", "%s", and "%_" */ |
46fc3d4c | 6833 | if (patlen == 0) |
6834 | return; | |
fc36a67e | 6835 | if (patlen == 2 && pat[0] == '%') { |
6836 | switch (pat[1]) { | |
6837 | case 's': | |
c635e13b | 6838 | if (args) { |
6839 | char *s = va_arg(*args, char*); | |
6840 | sv_catpv(sv, s ? s : nullstr); | |
6841 | } | |
7e2040f0 | 6842 | else if (svix < svmax) { |
fc36a67e | 6843 | sv_catsv(sv, *svargs); |
7e2040f0 GS |
6844 | if (DO_UTF8(*svargs)) |
6845 | SvUTF8_on(sv); | |
6846 | } | |
fc36a67e | 6847 | return; |
6848 | case '_': | |
6849 | if (args) { | |
7e2040f0 GS |
6850 | argsv = va_arg(*args, SV*); |
6851 | sv_catsv(sv, argsv); | |
6852 | if (DO_UTF8(argsv)) | |
6853 | SvUTF8_on(sv); | |
fc36a67e | 6854 | return; |
6855 | } | |
6856 | /* See comment on '_' below */ | |
6857 | break; | |
6858 | } | |
46fc3d4c | 6859 | } |
6860 | ||
6861 | patend = (char*)pat + patlen; | |
6862 | for (p = (char*)pat; p < patend; p = q) { | |
6863 | bool alt = FALSE; | |
6864 | bool left = FALSE; | |
b22c7a20 | 6865 | bool vectorize = FALSE; |
211dfcf1 | 6866 | bool vectorarg = FALSE; |
b2e23cf9 | 6867 | bool vec_utf = FALSE; |
46fc3d4c | 6868 | char fill = ' '; |
6869 | char plus = 0; | |
6870 | char intsize = 0; | |
6871 | STRLEN width = 0; | |
fc36a67e | 6872 | STRLEN zeros = 0; |
46fc3d4c | 6873 | bool has_precis = FALSE; |
6874 | STRLEN precis = 0; | |
7e2040f0 | 6875 | bool is_utf = FALSE; |
eb3fce90 | 6876 | |
46fc3d4c | 6877 | char esignbuf[4]; |
ad391ad9 | 6878 | U8 utf8buf[UTF8_MAXLEN+1]; |
46fc3d4c | 6879 | STRLEN esignlen = 0; |
6880 | ||
6881 | char *eptr = Nullch; | |
fc36a67e | 6882 | STRLEN elen = 0; |
089c015b JH |
6883 | /* Times 4: a decimal digit takes more than 3 binary digits. |
6884 | * NV_DIG: mantissa takes than many decimal digits. | |
6885 | * Plus 32: Playing safe. */ | |
6886 | char ebuf[IV_DIG * 4 + NV_DIG + 32]; | |
2d4389e4 JH |
6887 | /* large enough for "%#.#f" --chip */ |
6888 | /* what about long double NVs? --jhi */ | |
b22c7a20 GS |
6889 | |
6890 | SV *vecsv; | |
a05b299f | 6891 | U8 *vecstr = Null(U8*); |
b22c7a20 | 6892 | STRLEN veclen = 0; |
46fc3d4c | 6893 | char c; |
6894 | int i; | |
6895 | unsigned base; | |
6896 | IV iv; | |
6897 | UV uv; | |
65202027 | 6898 | NV nv; |
46fc3d4c | 6899 | STRLEN have; |
6900 | STRLEN need; | |
6901 | STRLEN gap; | |
b22c7a20 GS |
6902 | char *dotstr = "."; |
6903 | STRLEN dotstrlen = 1; | |
211dfcf1 | 6904 | I32 efix = 0; /* explicit format parameter index */ |
eb3fce90 | 6905 | I32 ewix = 0; /* explicit width index */ |
211dfcf1 HS |
6906 | I32 epix = 0; /* explicit precision index */ |
6907 | I32 evix = 0; /* explicit vector index */ | |
eb3fce90 | 6908 | bool asterisk = FALSE; |
46fc3d4c | 6909 | |
211dfcf1 | 6910 | /* echo everything up to the next format specification */ |
46fc3d4c | 6911 | for (q = p; q < patend && *q != '%'; ++q) ; |
6912 | if (q > p) { | |
6913 | sv_catpvn(sv, p, q - p); | |
6914 | p = q; | |
6915 | } | |
6916 | if (q++ >= patend) | |
6917 | break; | |
6918 | ||
211dfcf1 HS |
6919 | /* |
6920 | We allow format specification elements in this order: | |
6921 | \d+\$ explicit format parameter index | |
6922 | [-+ 0#]+ flags | |
6923 | \*?(\d+\$)?v vector with optional (optionally specified) arg | |
6924 | \d+|\*(\d+\$)? width using optional (optionally specified) arg | |
6925 | \.(\d*|\*(\d+\$)?) precision using optional (optionally specified) arg | |
6926 | [hlqLV] size | |
6927 | [%bcdefginopsux_DFOUX] format (mandatory) | |
6928 | */ | |
6929 | if (EXPECT_NUMBER(q, width)) { | |
6930 | if (*q == '$') { | |
6931 | ++q; | |
6932 | efix = width; | |
6933 | } else { | |
6934 | goto gotwidth; | |
6935 | } | |
6936 | } | |
6937 | ||
fc36a67e | 6938 | /* FLAGS */ |
6939 | ||
46fc3d4c | 6940 | while (*q) { |
6941 | switch (*q) { | |
6942 | case ' ': | |
6943 | case '+': | |
6944 | plus = *q++; | |
6945 | continue; | |
6946 | ||
6947 | case '-': | |
6948 | left = TRUE; | |
6949 | q++; | |
6950 | continue; | |
6951 | ||
6952 | case '0': | |
6953 | fill = *q++; | |
6954 | continue; | |
6955 | ||
6956 | case '#': | |
6957 | alt = TRUE; | |
6958 | q++; | |
6959 | continue; | |
6960 | ||
fc36a67e | 6961 | default: |
6962 | break; | |
6963 | } | |
6964 | break; | |
6965 | } | |
46fc3d4c | 6966 | |
211dfcf1 | 6967 | tryasterisk: |
eb3fce90 | 6968 | if (*q == '*') { |
211dfcf1 HS |
6969 | q++; |
6970 | if (EXPECT_NUMBER(q, ewix)) | |
6971 | if (*q++ != '$') | |
6972 | goto unknown; | |
eb3fce90 | 6973 | asterisk = TRUE; |
211dfcf1 HS |
6974 | } |
6975 | if (*q == 'v') { | |
eb3fce90 | 6976 | q++; |
211dfcf1 HS |
6977 | if (vectorize) |
6978 | goto unknown; | |
9cbac4c7 | 6979 | if ((vectorarg = asterisk)) { |
211dfcf1 HS |
6980 | evix = ewix; |
6981 | ewix = 0; | |
6982 | asterisk = FALSE; | |
6983 | } | |
6984 | vectorize = TRUE; | |
6985 | goto tryasterisk; | |
eb3fce90 JH |
6986 | } |
6987 | ||
211dfcf1 HS |
6988 | if (!asterisk) |
6989 | EXPECT_NUMBER(q, width); | |
6990 | ||
6991 | if (vectorize) { | |
6992 | if (vectorarg) { | |
6993 | if (args) | |
6994 | vecsv = va_arg(*args, SV*); | |
6995 | else | |
6996 | vecsv = (evix ? evix <= svmax : svix < svmax) ? | |
6997 | svargs[ewix ? ewix-1 : svix++] : &PL_sv_undef; | |
4459522c | 6998 | dotstr = SvPVx(vecsv, dotstrlen); |
211dfcf1 HS |
6999 | if (DO_UTF8(vecsv)) |
7000 | is_utf = TRUE; | |
7001 | } | |
7002 | if (args) { | |
7003 | vecsv = va_arg(*args, SV*); | |
7004 | vecstr = (U8*)SvPVx(vecsv,veclen); | |
b2e23cf9 | 7005 | vec_utf = DO_UTF8(vecsv); |
eb3fce90 | 7006 | } |
211dfcf1 HS |
7007 | else if (efix ? efix <= svmax : svix < svmax) { |
7008 | vecsv = svargs[efix ? efix-1 : svix++]; | |
7009 | vecstr = (U8*)SvPVx(vecsv,veclen); | |
b2e23cf9 | 7010 | vec_utf = DO_UTF8(vecsv); |
211dfcf1 HS |
7011 | } |
7012 | else { | |
7013 | vecstr = (U8*)""; | |
7014 | veclen = 0; | |
7015 | } | |
eb3fce90 | 7016 | } |
fc36a67e | 7017 | |
eb3fce90 | 7018 | if (asterisk) { |
fc36a67e | 7019 | if (args) |
7020 | i = va_arg(*args, int); | |
7021 | else | |
eb3fce90 JH |
7022 | i = (ewix ? ewix <= svmax : svix < svmax) ? |
7023 | SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0; | |
fc36a67e | 7024 | left |= (i < 0); |
7025 | width = (i < 0) ? -i : i; | |
fc36a67e | 7026 | } |
211dfcf1 | 7027 | gotwidth: |
fc36a67e | 7028 | |
7029 | /* PRECISION */ | |
46fc3d4c | 7030 | |
fc36a67e | 7031 | if (*q == '.') { |
7032 | q++; | |
7033 | if (*q == '*') { | |
211dfcf1 HS |
7034 | q++; |
7035 | if (EXPECT_NUMBER(q, epix) && *q++ != '$') | |
7036 | goto unknown; | |
46fc3d4c | 7037 | if (args) |
7038 | i = va_arg(*args, int); | |
7039 | else | |
eb3fce90 JH |
7040 | i = (ewix ? ewix <= svmax : svix < svmax) |
7041 | ? SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0; | |
fc36a67e | 7042 | precis = (i < 0) ? 0 : i; |
fc36a67e | 7043 | } |
7044 | else { | |
7045 | precis = 0; | |
7046 | while (isDIGIT(*q)) | |
7047 | precis = precis * 10 + (*q++ - '0'); | |
7048 | } | |
7049 | has_precis = TRUE; | |
7050 | } | |
46fc3d4c | 7051 | |
fc36a67e | 7052 | /* SIZE */ |
46fc3d4c | 7053 | |
fc36a67e | 7054 | switch (*q) { |
e5c81feb | 7055 | #if defined(HAS_QUAD) || (defined(HAS_LONG_DOUBLE) && defined(USE_LONG_DOUBLE)) |
6f9bb7fd | 7056 | case 'L': /* Ld */ |
e5c81feb JH |
7057 | /* FALL THROUGH */ |
7058 | #endif | |
7059 | #ifdef HAS_QUAD | |
6f9bb7fd GS |
7060 | case 'q': /* qd */ |
7061 | intsize = 'q'; | |
7062 | q++; | |
7063 | break; | |
7064 | #endif | |
fc36a67e | 7065 | case 'l': |
e5c81feb JH |
7066 | #if defined(HAS_QUAD) || (defined(HAS_LONG_DOUBLE) && defined(USE_LONG_DOUBLE)) |
7067 | if (*(q + 1) == 'l') { /* lld, llf */ | |
fc36a67e | 7068 | intsize = 'q'; |
7069 | q += 2; | |
46fc3d4c | 7070 | break; |
cf2093f6 | 7071 | } |
fc36a67e | 7072 | #endif |
6f9bb7fd | 7073 | /* FALL THROUGH */ |
fc36a67e | 7074 | case 'h': |
cf2093f6 | 7075 | /* FALL THROUGH */ |
fc36a67e | 7076 | case 'V': |
7077 | intsize = *q++; | |
46fc3d4c | 7078 | break; |
7079 | } | |
7080 | ||
fc36a67e | 7081 | /* CONVERSION */ |
7082 | ||
211dfcf1 HS |
7083 | if (*q == '%') { |
7084 | eptr = q++; | |
7085 | elen = 1; | |
7086 | goto string; | |
7087 | } | |
7088 | ||
7089 | if (!args) | |
7090 | argsv = (efix ? efix <= svmax : svix < svmax) ? | |
7091 | svargs[efix ? efix-1 : svix++] : &PL_sv_undef; | |
7092 | ||
46fc3d4c | 7093 | switch (c = *q++) { |
7094 | ||
7095 | /* STRINGS */ | |
7096 | ||
46fc3d4c | 7097 | case 'c': |
211dfcf1 | 7098 | uv = args ? va_arg(*args, int) : SvIVx(argsv); |
3969a896 | 7099 | if ((uv > 255 || (uv > 127 && SvUTF8(sv))) && !IN_BYTE) { |
dfe13c55 GS |
7100 | eptr = (char*)utf8buf; |
7101 | elen = uv_to_utf8((U8*)eptr, uv) - utf8buf; | |
7e2040f0 GS |
7102 | is_utf = TRUE; |
7103 | } | |
7104 | else { | |
7105 | c = (char)uv; | |
7106 | eptr = &c; | |
7107 | elen = 1; | |
a0ed51b3 | 7108 | } |
46fc3d4c | 7109 | goto string; |
7110 | ||
46fc3d4c | 7111 | case 's': |
7112 | if (args) { | |
fc36a67e | 7113 | eptr = va_arg(*args, char*); |
c635e13b | 7114 | if (eptr) |
1d7c1841 GS |
7115 | #ifdef MACOS_TRADITIONAL |
7116 | /* On MacOS, %#s format is used for Pascal strings */ | |
7117 | if (alt) | |
7118 | elen = *eptr++; | |
7119 | else | |
7120 | #endif | |
c635e13b | 7121 | elen = strlen(eptr); |
7122 | else { | |
7123 | eptr = nullstr; | |
7124 | elen = sizeof nullstr - 1; | |
7125 | } | |
46fc3d4c | 7126 | } |
211dfcf1 | 7127 | else { |
7e2040f0 GS |
7128 | eptr = SvPVx(argsv, elen); |
7129 | if (DO_UTF8(argsv)) { | |
a0ed51b3 LW |
7130 | if (has_precis && precis < elen) { |
7131 | I32 p = precis; | |
7e2040f0 | 7132 | sv_pos_u2b(argsv, &p, 0); /* sticks at end */ |
a0ed51b3 LW |
7133 | precis = p; |
7134 | } | |
7135 | if (width) { /* fudge width (can't fudge elen) */ | |
7e2040f0 | 7136 | width += elen - sv_len_utf8(argsv); |
a0ed51b3 | 7137 | } |
7e2040f0 | 7138 | is_utf = TRUE; |
a0ed51b3 LW |
7139 | } |
7140 | } | |
46fc3d4c | 7141 | goto string; |
7142 | ||
fc36a67e | 7143 | case '_': |
7144 | /* | |
7145 | * The "%_" hack might have to be changed someday, | |
7146 | * if ISO or ANSI decide to use '_' for something. | |
7147 | * So we keep it hidden from users' code. | |
7148 | */ | |
7149 | if (!args) | |
7150 | goto unknown; | |
211dfcf1 | 7151 | argsv = va_arg(*args, SV*); |
7e2040f0 GS |
7152 | eptr = SvPVx(argsv, elen); |
7153 | if (DO_UTF8(argsv)) | |
7154 | is_utf = TRUE; | |
fc36a67e | 7155 | |
46fc3d4c | 7156 | string: |
b22c7a20 | 7157 | vectorize = FALSE; |
46fc3d4c | 7158 | if (has_precis && elen > precis) |
7159 | elen = precis; | |
7160 | break; | |
7161 | ||
7162 | /* INTEGERS */ | |
7163 | ||
fc36a67e | 7164 | case 'p': |
c2e66d9e GS |
7165 | if (alt) |
7166 | goto unknown; | |
211dfcf1 | 7167 | uv = PTR2UV(args ? va_arg(*args, void*) : argsv); |
fc36a67e | 7168 | base = 16; |
7169 | goto integer; | |
7170 | ||
46fc3d4c | 7171 | case 'D': |
29fe7a80 | 7172 | #ifdef IV_IS_QUAD |
22f3ae8c | 7173 | intsize = 'q'; |
29fe7a80 | 7174 | #else |
46fc3d4c | 7175 | intsize = 'l'; |
29fe7a80 | 7176 | #endif |
46fc3d4c | 7177 | /* FALL THROUGH */ |
7178 | case 'd': | |
7179 | case 'i': | |
b22c7a20 | 7180 | if (vectorize) { |
ba210ebe | 7181 | STRLEN ulen; |
211dfcf1 HS |
7182 | if (!veclen) |
7183 | continue; | |
b2e23cf9 | 7184 | if (vec_utf) |
dcad2880 | 7185 | iv = (IV)utf8_to_uv(vecstr, veclen, &ulen, 0); |
b22c7a20 | 7186 | else { |
a05b299f | 7187 | iv = *vecstr; |
b22c7a20 GS |
7188 | ulen = 1; |
7189 | } | |
7190 | vecstr += ulen; | |
7191 | veclen -= ulen; | |
7192 | } | |
7193 | else if (args) { | |
46fc3d4c | 7194 | switch (intsize) { |
7195 | case 'h': iv = (short)va_arg(*args, int); break; | |
7196 | default: iv = va_arg(*args, int); break; | |
7197 | case 'l': iv = va_arg(*args, long); break; | |
fc36a67e | 7198 | case 'V': iv = va_arg(*args, IV); break; |
cf2093f6 JH |
7199 | #ifdef HAS_QUAD |
7200 | case 'q': iv = va_arg(*args, Quad_t); break; | |
7201 | #endif | |
46fc3d4c | 7202 | } |
7203 | } | |
7204 | else { | |
211dfcf1 | 7205 | iv = SvIVx(argsv); |
46fc3d4c | 7206 | switch (intsize) { |
7207 | case 'h': iv = (short)iv; break; | |
be28567c | 7208 | default: break; |
46fc3d4c | 7209 | case 'l': iv = (long)iv; break; |
fc36a67e | 7210 | case 'V': break; |
cf2093f6 JH |
7211 | #ifdef HAS_QUAD |
7212 | case 'q': iv = (Quad_t)iv; break; | |
7213 | #endif | |
46fc3d4c | 7214 | } |
7215 | } | |
7216 | if (iv >= 0) { | |
7217 | uv = iv; | |
7218 | if (plus) | |
7219 | esignbuf[esignlen++] = plus; | |
7220 | } | |
7221 | else { | |
7222 | uv = -iv; | |
7223 | esignbuf[esignlen++] = '-'; | |
7224 | } | |
7225 | base = 10; | |
7226 | goto integer; | |
7227 | ||
fc36a67e | 7228 | case 'U': |
29fe7a80 | 7229 | #ifdef IV_IS_QUAD |
22f3ae8c | 7230 | intsize = 'q'; |
29fe7a80 | 7231 | #else |
fc36a67e | 7232 | intsize = 'l'; |
29fe7a80 | 7233 | #endif |
fc36a67e | 7234 | /* FALL THROUGH */ |
7235 | case 'u': | |
7236 | base = 10; | |
7237 | goto uns_integer; | |
7238 | ||
4f19785b WSI |
7239 | case 'b': |
7240 | base = 2; | |
7241 | goto uns_integer; | |
7242 | ||
46fc3d4c | 7243 | case 'O': |
29fe7a80 | 7244 | #ifdef IV_IS_QUAD |
22f3ae8c | 7245 | intsize = 'q'; |
29fe7a80 | 7246 | #else |
46fc3d4c | 7247 | intsize = 'l'; |
29fe7a80 | 7248 | #endif |
46fc3d4c | 7249 | /* FALL THROUGH */ |
7250 | case 'o': | |
7251 | base = 8; | |
7252 | goto uns_integer; | |
7253 | ||
7254 | case 'X': | |
46fc3d4c | 7255 | case 'x': |
7256 | base = 16; | |
46fc3d4c | 7257 | |
7258 | uns_integer: | |
b22c7a20 | 7259 | if (vectorize) { |
ba210ebe | 7260 | STRLEN ulen; |
b22c7a20 | 7261 | vector: |
211dfcf1 HS |
7262 | if (!veclen) |
7263 | continue; | |
b2e23cf9 | 7264 | if (vec_utf) |
dcad2880 | 7265 | uv = utf8_to_uv(vecstr, veclen, &ulen, 0); |
b22c7a20 | 7266 | else { |
a05b299f | 7267 | uv = *vecstr; |
b22c7a20 GS |
7268 | ulen = 1; |
7269 | } | |
7270 | vecstr += ulen; | |
7271 | veclen -= ulen; | |
7272 | } | |
7273 | else if (args) { | |
46fc3d4c | 7274 | switch (intsize) { |
7275 | case 'h': uv = (unsigned short)va_arg(*args, unsigned); break; | |
7276 | default: uv = va_arg(*args, unsigned); break; | |
7277 | case 'l': uv = va_arg(*args, unsigned long); break; | |
fc36a67e | 7278 | case 'V': uv = va_arg(*args, UV); break; |
cf2093f6 JH |
7279 | #ifdef HAS_QUAD |
7280 | case 'q': uv = va_arg(*args, Quad_t); break; | |
7281 | #endif | |
46fc3d4c | 7282 | } |
7283 | } | |
7284 | else { | |
211dfcf1 | 7285 | uv = SvUVx(argsv); |
46fc3d4c | 7286 | switch (intsize) { |
7287 | case 'h': uv = (unsigned short)uv; break; | |
be28567c | 7288 | default: break; |
46fc3d4c | 7289 | case 'l': uv = (unsigned long)uv; break; |
fc36a67e | 7290 | case 'V': break; |
cf2093f6 JH |
7291 | #ifdef HAS_QUAD |
7292 | case 'q': uv = (Quad_t)uv; break; | |
7293 | #endif | |
46fc3d4c | 7294 | } |
7295 | } | |
7296 | ||
7297 | integer: | |
46fc3d4c | 7298 | eptr = ebuf + sizeof ebuf; |
fc36a67e | 7299 | switch (base) { |
7300 | unsigned dig; | |
7301 | case 16: | |
c10ed8b9 HS |
7302 | if (!uv) |
7303 | alt = FALSE; | |
1d7c1841 GS |
7304 | p = (char*)((c == 'X') |
7305 | ? "0123456789ABCDEF" : "0123456789abcdef"); | |
fc36a67e | 7306 | do { |
7307 | dig = uv & 15; | |
7308 | *--eptr = p[dig]; | |
7309 | } while (uv >>= 4); | |
7310 | if (alt) { | |
46fc3d4c | 7311 | esignbuf[esignlen++] = '0'; |
fc36a67e | 7312 | esignbuf[esignlen++] = c; /* 'x' or 'X' */ |
46fc3d4c | 7313 | } |
fc36a67e | 7314 | break; |
7315 | case 8: | |
7316 | do { | |
7317 | dig = uv & 7; | |
7318 | *--eptr = '0' + dig; | |
7319 | } while (uv >>= 3); | |
7320 | if (alt && *eptr != '0') | |
7321 | *--eptr = '0'; | |
7322 | break; | |
4f19785b WSI |
7323 | case 2: |
7324 | do { | |
7325 | dig = uv & 1; | |
7326 | *--eptr = '0' + dig; | |
7327 | } while (uv >>= 1); | |
eda88b6d JH |
7328 | if (alt) { |
7329 | esignbuf[esignlen++] = '0'; | |
7481bb52 | 7330 | esignbuf[esignlen++] = 'b'; |
eda88b6d | 7331 | } |
4f19785b | 7332 | break; |
fc36a67e | 7333 | default: /* it had better be ten or less */ |
6bc102ca | 7334 | #if defined(PERL_Y2KWARN) |
e476b1b5 | 7335 | if (ckWARN(WARN_Y2K)) { |
6bc102ca GS |
7336 | STRLEN n; |
7337 | char *s = SvPV(sv,n); | |
7338 | if (n >= 2 && s[n-2] == '1' && s[n-1] == '9' | |
7339 | && (n == 2 || !isDIGIT(s[n-3]))) | |
7340 | { | |
e476b1b5 | 7341 | Perl_warner(aTHX_ WARN_Y2K, |
6bc102ca GS |
7342 | "Possible Y2K bug: %%%c %s", |
7343 | c, "format string following '19'"); | |
7344 | } | |
7345 | } | |
7346 | #endif | |
fc36a67e | 7347 | do { |
7348 | dig = uv % base; | |
7349 | *--eptr = '0' + dig; | |
7350 | } while (uv /= base); | |
7351 | break; | |
46fc3d4c | 7352 | } |
7353 | elen = (ebuf + sizeof ebuf) - eptr; | |
c10ed8b9 HS |
7354 | if (has_precis) { |
7355 | if (precis > elen) | |
7356 | zeros = precis - elen; | |
7357 | else if (precis == 0 && elen == 1 && *eptr == '0') | |
7358 | elen = 0; | |
7359 | } | |
46fc3d4c | 7360 | break; |
7361 | ||
7362 | /* FLOATING POINT */ | |
7363 | ||
fc36a67e | 7364 | case 'F': |
7365 | c = 'f'; /* maybe %F isn't supported here */ | |
7366 | /* FALL THROUGH */ | |
46fc3d4c | 7367 | case 'e': case 'E': |
fc36a67e | 7368 | case 'f': |
46fc3d4c | 7369 | case 'g': case 'G': |
7370 | ||
7371 | /* This is evil, but floating point is even more evil */ | |
7372 | ||
b22c7a20 | 7373 | vectorize = FALSE; |
211dfcf1 | 7374 | nv = args ? va_arg(*args, NV) : SvNVx(argsv); |
fc36a67e | 7375 | |
7376 | need = 0; | |
7377 | if (c != 'e' && c != 'E') { | |
7378 | i = PERL_INT_MIN; | |
73b309ea | 7379 | (void)Perl_frexp(nv, &i); |
fc36a67e | 7380 | if (i == PERL_INT_MIN) |
cea2e8a9 | 7381 | Perl_die(aTHX_ "panic: frexp"); |
c635e13b | 7382 | if (i > 0) |
fc36a67e | 7383 | need = BIT_DIGITS(i); |
7384 | } | |
7385 | need += has_precis ? precis : 6; /* known default */ | |
7386 | if (need < width) | |
7387 | need = width; | |
7388 | ||
46fc3d4c | 7389 | need += 20; /* fudge factor */ |
80252599 GS |
7390 | if (PL_efloatsize < need) { |
7391 | Safefree(PL_efloatbuf); | |
7392 | PL_efloatsize = need + 20; /* more fudge */ | |
7393 | New(906, PL_efloatbuf, PL_efloatsize, char); | |
7d5ea4e7 | 7394 | PL_efloatbuf[0] = '\0'; |
46fc3d4c | 7395 | } |
7396 | ||
7397 | eptr = ebuf + sizeof ebuf; | |
7398 | *--eptr = '\0'; | |
7399 | *--eptr = c; | |
e5c81feb | 7400 | #if defined(USE_LONG_DOUBLE) && defined(PERL_PRIfldbl) |
cf2093f6 | 7401 | { |
e5c81feb JH |
7402 | /* Copy the one or more characters in a long double |
7403 | * format before the 'base' ([efgEFG]) character to | |
7404 | * the format string. */ | |
7405 | static char const prifldbl[] = PERL_PRIfldbl; | |
7406 | char const *p = prifldbl + sizeof(prifldbl) - 3; | |
7407 | while (p >= prifldbl) { *--eptr = *p--; } | |
cf2093f6 | 7408 | } |
65202027 | 7409 | #endif |
46fc3d4c | 7410 | if (has_precis) { |
7411 | base = precis; | |
7412 | do { *--eptr = '0' + (base % 10); } while (base /= 10); | |
7413 | *--eptr = '.'; | |
7414 | } | |
7415 | if (width) { | |
7416 | base = width; | |
7417 | do { *--eptr = '0' + (base % 10); } while (base /= 10); | |
7418 | } | |
7419 | if (fill == '0') | |
7420 | *--eptr = fill; | |
84902520 TB |
7421 | if (left) |
7422 | *--eptr = '-'; | |
46fc3d4c | 7423 | if (plus) |
7424 | *--eptr = plus; | |
7425 | if (alt) | |
7426 | *--eptr = '#'; | |
7427 | *--eptr = '%'; | |
7428 | ||
ff9121f8 JH |
7429 | /* No taint. Otherwise we are in the strange situation |
7430 | * where printf() taints but print($float) doesn't. | |
bda0f7a5 | 7431 | * --jhi */ |
dd8482fc | 7432 | (void)sprintf(PL_efloatbuf, eptr, nv); |
8af02333 | 7433 | |
80252599 GS |
7434 | eptr = PL_efloatbuf; |
7435 | elen = strlen(PL_efloatbuf); | |
46fc3d4c | 7436 | break; |
7437 | ||
fc36a67e | 7438 | /* SPECIAL */ |
7439 | ||
7440 | case 'n': | |
b22c7a20 | 7441 | vectorize = FALSE; |
fc36a67e | 7442 | i = SvCUR(sv) - origlen; |
7443 | if (args) { | |
c635e13b | 7444 | switch (intsize) { |
7445 | case 'h': *(va_arg(*args, short*)) = i; break; | |
7446 | default: *(va_arg(*args, int*)) = i; break; | |
7447 | case 'l': *(va_arg(*args, long*)) = i; break; | |
7448 | case 'V': *(va_arg(*args, IV*)) = i; break; | |
cf2093f6 JH |
7449 | #ifdef HAS_QUAD |
7450 | case 'q': *(va_arg(*args, Quad_t*)) = i; break; | |
7451 | #endif | |
c635e13b | 7452 | } |
fc36a67e | 7453 | } |
9dd79c3f | 7454 | else |
211dfcf1 | 7455 | sv_setuv_mg(argsv, (UV)i); |
fc36a67e | 7456 | continue; /* not "break" */ |
7457 | ||
7458 | /* UNKNOWN */ | |
7459 | ||
46fc3d4c | 7460 | default: |
fc36a67e | 7461 | unknown: |
b22c7a20 | 7462 | vectorize = FALSE; |
599cee73 | 7463 | if (!args && ckWARN(WARN_PRINTF) && |
533c011a | 7464 | (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF)) { |
c635e13b | 7465 | SV *msg = sv_newmortal(); |
cea2e8a9 | 7466 | Perl_sv_setpvf(aTHX_ msg, "Invalid conversion in %s: ", |
533c011a | 7467 | (PL_op->op_type == OP_PRTF) ? "printf" : "sprintf"); |
0f4b6630 | 7468 | if (c) { |
0f4b6630 | 7469 | if (isPRINT(c)) |
1c846c1f | 7470 | Perl_sv_catpvf(aTHX_ msg, |
0f4b6630 JH |
7471 | "\"%%%c\"", c & 0xFF); |
7472 | else | |
7473 | Perl_sv_catpvf(aTHX_ msg, | |
57def98f | 7474 | "\"%%\\%03"UVof"\"", |
0f4b6630 | 7475 | (UV)c & 0xFF); |
0f4b6630 | 7476 | } else |
c635e13b | 7477 | sv_catpv(msg, "end of string"); |
894356b3 | 7478 | Perl_warner(aTHX_ WARN_PRINTF, "%"SVf, msg); /* yes, this is reentrant */ |
c635e13b | 7479 | } |
fb73857a | 7480 | |
7481 | /* output mangled stuff ... */ | |
7482 | if (c == '\0') | |
7483 | --q; | |
46fc3d4c | 7484 | eptr = p; |
7485 | elen = q - p; | |
fb73857a | 7486 | |
7487 | /* ... right here, because formatting flags should not apply */ | |
7488 | SvGROW(sv, SvCUR(sv) + elen + 1); | |
7489 | p = SvEND(sv); | |
4459522c | 7490 | Copy(eptr, p, elen, char); |
fb73857a | 7491 | p += elen; |
7492 | *p = '\0'; | |
7493 | SvCUR(sv) = p - SvPVX(sv); | |
7494 | continue; /* not "break" */ | |
46fc3d4c | 7495 | } |
7496 | ||
fc36a67e | 7497 | have = esignlen + zeros + elen; |
46fc3d4c | 7498 | need = (have > width ? have : width); |
7499 | gap = need - have; | |
7500 | ||
b22c7a20 | 7501 | SvGROW(sv, SvCUR(sv) + need + dotstrlen + 1); |
46fc3d4c | 7502 | p = SvEND(sv); |
7503 | if (esignlen && fill == '0') { | |
7504 | for (i = 0; i < esignlen; i++) | |
7505 | *p++ = esignbuf[i]; | |
7506 | } | |
7507 | if (gap && !left) { | |
7508 | memset(p, fill, gap); | |
7509 | p += gap; | |
7510 | } | |
7511 | if (esignlen && fill != '0') { | |
7512 | for (i = 0; i < esignlen; i++) | |
7513 | *p++ = esignbuf[i]; | |
7514 | } | |
fc36a67e | 7515 | if (zeros) { |
7516 | for (i = zeros; i; i--) | |
7517 | *p++ = '0'; | |
7518 | } | |
46fc3d4c | 7519 | if (elen) { |
4459522c | 7520 | Copy(eptr, p, elen, char); |
46fc3d4c | 7521 | p += elen; |
7522 | } | |
7523 | if (gap && left) { | |
7524 | memset(p, ' ', gap); | |
7525 | p += gap; | |
7526 | } | |
b22c7a20 GS |
7527 | if (vectorize) { |
7528 | if (veclen) { | |
4459522c | 7529 | Copy(dotstr, p, dotstrlen, char); |
b22c7a20 GS |
7530 | p += dotstrlen; |
7531 | } | |
7532 | else | |
7533 | vectorize = FALSE; /* done iterating over vecstr */ | |
7534 | } | |
7e2040f0 GS |
7535 | if (is_utf) |
7536 | SvUTF8_on(sv); | |
46fc3d4c | 7537 | *p = '\0'; |
7538 | SvCUR(sv) = p - SvPVX(sv); | |
b22c7a20 GS |
7539 | if (vectorize) { |
7540 | esignlen = 0; | |
7541 | goto vector; | |
7542 | } | |
46fc3d4c | 7543 | } |
7544 | } | |
51371543 | 7545 | |
1d7c1841 GS |
7546 | #if defined(USE_ITHREADS) |
7547 | ||
7548 | #if defined(USE_THREADS) | |
7549 | # include "error: USE_THREADS and USE_ITHREADS are incompatible" | |
7550 | #endif | |
7551 | ||
1d7c1841 GS |
7552 | #ifndef GpREFCNT_inc |
7553 | # define GpREFCNT_inc(gp) ((gp) ? (++(gp)->gp_refcnt, (gp)) : (GP*)NULL) | |
7554 | #endif | |
7555 | ||
7556 | ||
7557 | #define sv_dup_inc(s) SvREFCNT_inc(sv_dup(s)) | |
7558 | #define av_dup(s) (AV*)sv_dup((SV*)s) | |
7559 | #define av_dup_inc(s) (AV*)SvREFCNT_inc(sv_dup((SV*)s)) | |
7560 | #define hv_dup(s) (HV*)sv_dup((SV*)s) | |
7561 | #define hv_dup_inc(s) (HV*)SvREFCNT_inc(sv_dup((SV*)s)) | |
7562 | #define cv_dup(s) (CV*)sv_dup((SV*)s) | |
7563 | #define cv_dup_inc(s) (CV*)SvREFCNT_inc(sv_dup((SV*)s)) | |
7564 | #define io_dup(s) (IO*)sv_dup((SV*)s) | |
7565 | #define io_dup_inc(s) (IO*)SvREFCNT_inc(sv_dup((SV*)s)) | |
7566 | #define gv_dup(s) (GV*)sv_dup((SV*)s) | |
7567 | #define gv_dup_inc(s) (GV*)SvREFCNT_inc(sv_dup((SV*)s)) | |
7568 | #define SAVEPV(p) (p ? savepv(p) : Nullch) | |
7569 | #define SAVEPVN(p,n) (p ? savepvn(p,n) : Nullch) | |
7570 | ||
7571 | REGEXP * | |
7572 | Perl_re_dup(pTHX_ REGEXP *r) | |
7573 | { | |
7574 | /* XXX fix when pmop->op_pmregexp becomes shared */ | |
7575 | return ReREFCNT_inc(r); | |
7576 | } | |
7577 | ||
7578 | PerlIO * | |
7579 | Perl_fp_dup(pTHX_ PerlIO *fp, char type) | |
7580 | { | |
7581 | PerlIO *ret; | |
7582 | if (!fp) | |
7583 | return (PerlIO*)NULL; | |
7584 | ||
7585 | /* look for it in the table first */ | |
7586 | ret = (PerlIO*)ptr_table_fetch(PL_ptr_table, fp); | |
7587 | if (ret) | |
7588 | return ret; | |
7589 | ||
7590 | /* create anew and remember what it is */ | |
5f1a76d0 | 7591 | ret = PerlIO_fdupopen(aTHX_ fp); |
1d7c1841 GS |
7592 | ptr_table_store(PL_ptr_table, fp, ret); |
7593 | return ret; | |
7594 | } | |
7595 | ||
7596 | DIR * | |
7597 | Perl_dirp_dup(pTHX_ DIR *dp) | |
7598 | { | |
7599 | if (!dp) | |
7600 | return (DIR*)NULL; | |
7601 | /* XXX TODO */ | |
7602 | return dp; | |
7603 | } | |
7604 | ||
7605 | GP * | |
7606 | Perl_gp_dup(pTHX_ GP *gp) | |
7607 | { | |
7608 | GP *ret; | |
7609 | if (!gp) | |
7610 | return (GP*)NULL; | |
7611 | /* look for it in the table first */ | |
7612 | ret = (GP*)ptr_table_fetch(PL_ptr_table, gp); | |
7613 | if (ret) | |
7614 | return ret; | |
7615 | ||
7616 | /* create anew and remember what it is */ | |
7617 | Newz(0, ret, 1, GP); | |
7618 | ptr_table_store(PL_ptr_table, gp, ret); | |
7619 | ||
7620 | /* clone */ | |
7621 | ret->gp_refcnt = 0; /* must be before any other dups! */ | |
7622 | ret->gp_sv = sv_dup_inc(gp->gp_sv); | |
7623 | ret->gp_io = io_dup_inc(gp->gp_io); | |
7624 | ret->gp_form = cv_dup_inc(gp->gp_form); | |
7625 | ret->gp_av = av_dup_inc(gp->gp_av); | |
7626 | ret->gp_hv = hv_dup_inc(gp->gp_hv); | |
7627 | ret->gp_egv = gv_dup(gp->gp_egv); /* GvEGV is not refcounted */ | |
7628 | ret->gp_cv = cv_dup_inc(gp->gp_cv); | |
7629 | ret->gp_cvgen = gp->gp_cvgen; | |
7630 | ret->gp_flags = gp->gp_flags; | |
7631 | ret->gp_line = gp->gp_line; | |
7632 | ret->gp_file = gp->gp_file; /* points to COP.cop_file */ | |
7633 | return ret; | |
7634 | } | |
7635 | ||
7636 | MAGIC * | |
7637 | Perl_mg_dup(pTHX_ MAGIC *mg) | |
7638 | { | |
7639 | MAGIC *mgret = (MAGIC*)NULL; | |
7640 | MAGIC *mgprev; | |
7641 | if (!mg) | |
7642 | return (MAGIC*)NULL; | |
7643 | /* look for it in the table first */ | |
7644 | mgret = (MAGIC*)ptr_table_fetch(PL_ptr_table, mg); | |
7645 | if (mgret) | |
7646 | return mgret; | |
7647 | ||
7648 | for (; mg; mg = mg->mg_moremagic) { | |
7649 | MAGIC *nmg; | |
7650 | Newz(0, nmg, 1, MAGIC); | |
7651 | if (!mgret) | |
7652 | mgret = nmg; | |
7653 | else | |
7654 | mgprev->mg_moremagic = nmg; | |
7655 | nmg->mg_virtual = mg->mg_virtual; /* XXX copy dynamic vtable? */ | |
7656 | nmg->mg_private = mg->mg_private; | |
7657 | nmg->mg_type = mg->mg_type; | |
7658 | nmg->mg_flags = mg->mg_flags; | |
7659 | if (mg->mg_type == 'r') { | |
7660 | nmg->mg_obj = (SV*)re_dup((REGEXP*)mg->mg_obj); | |
7661 | } | |
7662 | else { | |
7663 | nmg->mg_obj = (mg->mg_flags & MGf_REFCOUNTED) | |
7664 | ? sv_dup_inc(mg->mg_obj) | |
7665 | : sv_dup(mg->mg_obj); | |
7666 | } | |
7667 | nmg->mg_len = mg->mg_len; | |
7668 | nmg->mg_ptr = mg->mg_ptr; /* XXX random ptr? */ | |
7669 | if (mg->mg_ptr && mg->mg_type != 'g') { | |
7670 | if (mg->mg_len >= 0) { | |
7671 | nmg->mg_ptr = SAVEPVN(mg->mg_ptr, mg->mg_len); | |
7672 | if (mg->mg_type == 'c' && AMT_AMAGIC((AMT*)mg->mg_ptr)) { | |
7673 | AMT *amtp = (AMT*)mg->mg_ptr; | |
7674 | AMT *namtp = (AMT*)nmg->mg_ptr; | |
7675 | I32 i; | |
7676 | for (i = 1; i < NofAMmeth; i++) { | |
7677 | namtp->table[i] = cv_dup_inc(amtp->table[i]); | |
7678 | } | |
7679 | } | |
7680 | } | |
7681 | else if (mg->mg_len == HEf_SVKEY) | |
7682 | nmg->mg_ptr = (char*)sv_dup_inc((SV*)mg->mg_ptr); | |
7683 | } | |
7684 | mgprev = nmg; | |
7685 | } | |
7686 | return mgret; | |
7687 | } | |
7688 | ||
7689 | PTR_TBL_t * | |
7690 | Perl_ptr_table_new(pTHX) | |
7691 | { | |
7692 | PTR_TBL_t *tbl; | |
7693 | Newz(0, tbl, 1, PTR_TBL_t); | |
7694 | tbl->tbl_max = 511; | |
7695 | tbl->tbl_items = 0; | |
7696 | Newz(0, tbl->tbl_ary, tbl->tbl_max + 1, PTR_TBL_ENT_t*); | |
7697 | return tbl; | |
7698 | } | |
7699 | ||
7700 | void * | |
7701 | Perl_ptr_table_fetch(pTHX_ PTR_TBL_t *tbl, void *sv) | |
7702 | { | |
7703 | PTR_TBL_ENT_t *tblent; | |
d2a79402 | 7704 | UV hash = PTR2UV(sv); |
1d7c1841 GS |
7705 | assert(tbl); |
7706 | tblent = tbl->tbl_ary[hash & tbl->tbl_max]; | |
7707 | for (; tblent; tblent = tblent->next) { | |
7708 | if (tblent->oldval == sv) | |
7709 | return tblent->newval; | |
7710 | } | |
7711 | return (void*)NULL; | |
7712 | } | |
7713 | ||
7714 | void | |
7715 | Perl_ptr_table_store(pTHX_ PTR_TBL_t *tbl, void *oldv, void *newv) | |
7716 | { | |
7717 | PTR_TBL_ENT_t *tblent, **otblent; | |
7718 | /* XXX this may be pessimal on platforms where pointers aren't good | |
7719 | * hash values e.g. if they grow faster in the most significant | |
7720 | * bits */ | |
d2a79402 | 7721 | UV hash = PTR2UV(oldv); |
1d7c1841 GS |
7722 | bool i = 1; |
7723 | ||
7724 | assert(tbl); | |
7725 | otblent = &tbl->tbl_ary[hash & tbl->tbl_max]; | |
7726 | for (tblent = *otblent; tblent; i=0, tblent = tblent->next) { | |
7727 | if (tblent->oldval == oldv) { | |
7728 | tblent->newval = newv; | |
7729 | tbl->tbl_items++; | |
7730 | return; | |
7731 | } | |
7732 | } | |
7733 | Newz(0, tblent, 1, PTR_TBL_ENT_t); | |
7734 | tblent->oldval = oldv; | |
7735 | tblent->newval = newv; | |
7736 | tblent->next = *otblent; | |
7737 | *otblent = tblent; | |
7738 | tbl->tbl_items++; | |
7739 | if (i && tbl->tbl_items > tbl->tbl_max) | |
7740 | ptr_table_split(tbl); | |
7741 | } | |
7742 | ||
7743 | void | |
7744 | Perl_ptr_table_split(pTHX_ PTR_TBL_t *tbl) | |
7745 | { | |
7746 | PTR_TBL_ENT_t **ary = tbl->tbl_ary; | |
7747 | UV oldsize = tbl->tbl_max + 1; | |
7748 | UV newsize = oldsize * 2; | |
7749 | UV i; | |
7750 | ||
7751 | Renew(ary, newsize, PTR_TBL_ENT_t*); | |
7752 | Zero(&ary[oldsize], newsize-oldsize, PTR_TBL_ENT_t*); | |
7753 | tbl->tbl_max = --newsize; | |
7754 | tbl->tbl_ary = ary; | |
7755 | for (i=0; i < oldsize; i++, ary++) { | |
7756 | PTR_TBL_ENT_t **curentp, **entp, *ent; | |
7757 | if (!*ary) | |
7758 | continue; | |
7759 | curentp = ary + oldsize; | |
7760 | for (entp = ary, ent = *ary; ent; ent = *entp) { | |
d2a79402 | 7761 | if ((newsize & PTR2UV(ent->oldval)) != i) { |
1d7c1841 GS |
7762 | *entp = ent->next; |
7763 | ent->next = *curentp; | |
7764 | *curentp = ent; | |
7765 | continue; | |
7766 | } | |
7767 | else | |
7768 | entp = &ent->next; | |
7769 | } | |
7770 | } | |
7771 | } | |
7772 | ||
a0739874 DM |
7773 | void |
7774 | Perl_ptr_table_clear(pTHX_ PTR_TBL_t *tbl) | |
7775 | { | |
7776 | register PTR_TBL_ENT_t **array; | |
7777 | register PTR_TBL_ENT_t *entry; | |
7778 | register PTR_TBL_ENT_t *oentry = Null(PTR_TBL_ENT_t*); | |
7779 | UV riter = 0; | |
7780 | UV max; | |
7781 | ||
7782 | if (!tbl || !tbl->tbl_items) { | |
7783 | return; | |
7784 | } | |
7785 | ||
7786 | array = tbl->tbl_ary; | |
7787 | entry = array[0]; | |
7788 | max = tbl->tbl_max; | |
7789 | ||
7790 | for (;;) { | |
7791 | if (entry) { | |
7792 | oentry = entry; | |
7793 | entry = entry->next; | |
7794 | Safefree(oentry); | |
7795 | } | |
7796 | if (!entry) { | |
7797 | if (++riter > max) { | |
7798 | break; | |
7799 | } | |
7800 | entry = array[riter]; | |
7801 | } | |
7802 | } | |
7803 | ||
7804 | tbl->tbl_items = 0; | |
7805 | } | |
7806 | ||
7807 | void | |
7808 | Perl_ptr_table_free(pTHX_ PTR_TBL_t *tbl) | |
7809 | { | |
7810 | if (!tbl) { | |
7811 | return; | |
7812 | } | |
7813 | ptr_table_clear(tbl); | |
7814 | Safefree(tbl->tbl_ary); | |
7815 | Safefree(tbl); | |
7816 | } | |
7817 | ||
1d7c1841 GS |
7818 | #ifdef DEBUGGING |
7819 | char *PL_watch_pvx; | |
7820 | #endif | |
7821 | ||
5bd07a3d DM |
7822 | STATIC SV * |
7823 | S_gv_share(pTHX_ SV *sstr) | |
7824 | { | |
7825 | GV *gv = (GV*)sstr; | |
7826 | SV *sv = &PL_sv_no; /* just need SvREADONLY-ness */ | |
7827 | ||
7828 | if (GvIO(gv) || GvFORM(gv)) { | |
7829 | GvSHARED_off(gv); /* GvIOs cannot be shared. nor can GvFORMs */ | |
7830 | } | |
7831 | else if (!GvCV(gv)) { | |
7832 | GvCV(gv) = (CV*)sv; | |
7833 | } | |
7834 | else { | |
7835 | /* CvPADLISTs cannot be shared */ | |
7836 | if (!CvXSUB(GvCV(gv))) { | |
7837 | GvSHARED_off(gv); | |
7838 | } | |
7839 | } | |
7840 | ||
7841 | if (!GvSHARED(gv)) { | |
7842 | #if 0 | |
7843 | PerlIO_printf(Perl_debug_log, "gv_share: unable to share %s::%s\n", | |
7844 | HvNAME(GvSTASH(gv)), GvNAME(gv)); | |
7845 | #endif | |
7846 | return Nullsv; | |
7847 | } | |
7848 | ||
4411f3b6 | 7849 | /* |
5bd07a3d DM |
7850 | * write attempts will die with |
7851 | * "Modification of a read-only value attempted" | |
7852 | */ | |
7853 | if (!GvSV(gv)) { | |
7854 | GvSV(gv) = sv; | |
7855 | } | |
7856 | else { | |
7857 | SvREADONLY_on(GvSV(gv)); | |
7858 | } | |
7859 | ||
7860 | if (!GvAV(gv)) { | |
7861 | GvAV(gv) = (AV*)sv; | |
7862 | } | |
7863 | else { | |
7864 | SvREADONLY_on(GvAV(gv)); | |
7865 | } | |
7866 | ||
7867 | if (!GvHV(gv)) { | |
7868 | GvHV(gv) = (HV*)sv; | |
7869 | } | |
7870 | else { | |
7871 | SvREADONLY_on(GvAV(gv)); | |
7872 | } | |
7873 | ||
7874 | return sstr; /* he_dup() will SvREFCNT_inc() */ | |
7875 | } | |
7876 | ||
1d7c1841 GS |
7877 | SV * |
7878 | Perl_sv_dup(pTHX_ SV *sstr) | |
7879 | { | |
1d7c1841 GS |
7880 | SV *dstr; |
7881 | ||
7882 | if (!sstr || SvTYPE(sstr) == SVTYPEMASK) | |
7883 | return Nullsv; | |
7884 | /* look for it in the table first */ | |
7885 | dstr = (SV*)ptr_table_fetch(PL_ptr_table, sstr); | |
7886 | if (dstr) | |
7887 | return dstr; | |
7888 | ||
7889 | /* create anew and remember what it is */ | |
7890 | new_SV(dstr); | |
7891 | ptr_table_store(PL_ptr_table, sstr, dstr); | |
7892 | ||
7893 | /* clone */ | |
7894 | SvFLAGS(dstr) = SvFLAGS(sstr); | |
7895 | SvFLAGS(dstr) &= ~SVf_OOK; /* don't propagate OOK hack */ | |
7896 | SvREFCNT(dstr) = 0; /* must be before any other dups! */ | |
7897 | ||
7898 | #ifdef DEBUGGING | |
7899 | if (SvANY(sstr) && PL_watch_pvx && SvPVX(sstr) == PL_watch_pvx) | |
7900 | PerlIO_printf(Perl_debug_log, "watch at %p hit, found string \"%s\"\n", | |
7901 | PL_watch_pvx, SvPVX(sstr)); | |
7902 | #endif | |
7903 | ||
7904 | switch (SvTYPE(sstr)) { | |
7905 | case SVt_NULL: | |
7906 | SvANY(dstr) = NULL; | |
7907 | break; | |
7908 | case SVt_IV: | |
7909 | SvANY(dstr) = new_XIV(); | |
7910 | SvIVX(dstr) = SvIVX(sstr); | |
7911 | break; | |
7912 | case SVt_NV: | |
7913 | SvANY(dstr) = new_XNV(); | |
7914 | SvNVX(dstr) = SvNVX(sstr); | |
7915 | break; | |
7916 | case SVt_RV: | |
7917 | SvANY(dstr) = new_XRV(); | |
7918 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7919 | break; | |
7920 | case SVt_PV: | |
7921 | SvANY(dstr) = new_XPV(); | |
7922 | SvCUR(dstr) = SvCUR(sstr); | |
7923 | SvLEN(dstr) = SvLEN(sstr); | |
7924 | if (SvROK(sstr)) | |
7925 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7926 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
7927 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
7928 | else | |
7929 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
7930 | break; | |
7931 | case SVt_PVIV: | |
7932 | SvANY(dstr) = new_XPVIV(); | |
7933 | SvCUR(dstr) = SvCUR(sstr); | |
7934 | SvLEN(dstr) = SvLEN(sstr); | |
7935 | SvIVX(dstr) = SvIVX(sstr); | |
7936 | if (SvROK(sstr)) | |
7937 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7938 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
7939 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
7940 | else | |
7941 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
7942 | break; | |
7943 | case SVt_PVNV: | |
7944 | SvANY(dstr) = new_XPVNV(); | |
7945 | SvCUR(dstr) = SvCUR(sstr); | |
7946 | SvLEN(dstr) = SvLEN(sstr); | |
7947 | SvIVX(dstr) = SvIVX(sstr); | |
7948 | SvNVX(dstr) = SvNVX(sstr); | |
7949 | if (SvROK(sstr)) | |
7950 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7951 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
7952 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
7953 | else | |
7954 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
7955 | break; | |
7956 | case SVt_PVMG: | |
7957 | SvANY(dstr) = new_XPVMG(); | |
7958 | SvCUR(dstr) = SvCUR(sstr); | |
7959 | SvLEN(dstr) = SvLEN(sstr); | |
7960 | SvIVX(dstr) = SvIVX(sstr); | |
7961 | SvNVX(dstr) = SvNVX(sstr); | |
7962 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
7963 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
7964 | if (SvROK(sstr)) | |
7965 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7966 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
7967 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
7968 | else | |
7969 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
7970 | break; | |
7971 | case SVt_PVBM: | |
7972 | SvANY(dstr) = new_XPVBM(); | |
7973 | SvCUR(dstr) = SvCUR(sstr); | |
7974 | SvLEN(dstr) = SvLEN(sstr); | |
7975 | SvIVX(dstr) = SvIVX(sstr); | |
7976 | SvNVX(dstr) = SvNVX(sstr); | |
7977 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
7978 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
7979 | if (SvROK(sstr)) | |
7980 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7981 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
7982 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
7983 | else | |
7984 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
7985 | BmRARE(dstr) = BmRARE(sstr); | |
7986 | BmUSEFUL(dstr) = BmUSEFUL(sstr); | |
7987 | BmPREVIOUS(dstr)= BmPREVIOUS(sstr); | |
7988 | break; | |
7989 | case SVt_PVLV: | |
7990 | SvANY(dstr) = new_XPVLV(); | |
7991 | SvCUR(dstr) = SvCUR(sstr); | |
7992 | SvLEN(dstr) = SvLEN(sstr); | |
7993 | SvIVX(dstr) = SvIVX(sstr); | |
7994 | SvNVX(dstr) = SvNVX(sstr); | |
7995 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
7996 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
7997 | if (SvROK(sstr)) | |
7998 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7999 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
8000 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
8001 | else | |
8002 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
8003 | LvTARGOFF(dstr) = LvTARGOFF(sstr); /* XXX sometimes holds PMOP* when DEBUGGING */ | |
8004 | LvTARGLEN(dstr) = LvTARGLEN(sstr); | |
8005 | LvTARG(dstr) = sv_dup_inc(LvTARG(sstr)); | |
8006 | LvTYPE(dstr) = LvTYPE(sstr); | |
8007 | break; | |
8008 | case SVt_PVGV: | |
5bd07a3d DM |
8009 | if (GvSHARED((GV*)sstr)) { |
8010 | SV *share; | |
8011 | if ((share = gv_share(sstr))) { | |
8012 | del_SV(dstr); | |
8013 | dstr = share; | |
8014 | #if 0 | |
8015 | PerlIO_printf(Perl_debug_log, "sv_dup: sharing %s::%s\n", | |
8016 | HvNAME(GvSTASH(share)), GvNAME(share)); | |
8017 | #endif | |
8018 | break; | |
8019 | } | |
8020 | } | |
1d7c1841 GS |
8021 | SvANY(dstr) = new_XPVGV(); |
8022 | SvCUR(dstr) = SvCUR(sstr); | |
8023 | SvLEN(dstr) = SvLEN(sstr); | |
8024 | SvIVX(dstr) = SvIVX(sstr); | |
8025 | SvNVX(dstr) = SvNVX(sstr); | |
8026 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
8027 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
8028 | if (SvROK(sstr)) | |
8029 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
8030 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
8031 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
8032 | else | |
8033 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
8034 | GvNAMELEN(dstr) = GvNAMELEN(sstr); | |
8035 | GvNAME(dstr) = SAVEPVN(GvNAME(sstr), GvNAMELEN(sstr)); | |
8036 | GvSTASH(dstr) = hv_dup_inc(GvSTASH(sstr)); | |
8037 | GvFLAGS(dstr) = GvFLAGS(sstr); | |
8038 | GvGP(dstr) = gp_dup(GvGP(sstr)); | |
8039 | (void)GpREFCNT_inc(GvGP(dstr)); | |
8040 | break; | |
8041 | case SVt_PVIO: | |
8042 | SvANY(dstr) = new_XPVIO(); | |
8043 | SvCUR(dstr) = SvCUR(sstr); | |
8044 | SvLEN(dstr) = SvLEN(sstr); | |
8045 | SvIVX(dstr) = SvIVX(sstr); | |
8046 | SvNVX(dstr) = SvNVX(sstr); | |
8047 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
8048 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
8049 | if (SvROK(sstr)) | |
8050 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
8051 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
8052 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
8053 | else | |
8054 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
8055 | IoIFP(dstr) = fp_dup(IoIFP(sstr), IoTYPE(sstr)); | |
8056 | if (IoOFP(sstr) == IoIFP(sstr)) | |
8057 | IoOFP(dstr) = IoIFP(dstr); | |
8058 | else | |
8059 | IoOFP(dstr) = fp_dup(IoOFP(sstr), IoTYPE(sstr)); | |
8060 | /* PL_rsfp_filters entries have fake IoDIRP() */ | |
8061 | if (IoDIRP(sstr) && !(IoFLAGS(sstr) & IOf_FAKE_DIRP)) | |
8062 | IoDIRP(dstr) = dirp_dup(IoDIRP(sstr)); | |
8063 | else | |
8064 | IoDIRP(dstr) = IoDIRP(sstr); | |
8065 | IoLINES(dstr) = IoLINES(sstr); | |
8066 | IoPAGE(dstr) = IoPAGE(sstr); | |
8067 | IoPAGE_LEN(dstr) = IoPAGE_LEN(sstr); | |
8068 | IoLINES_LEFT(dstr) = IoLINES_LEFT(sstr); | |
8069 | IoTOP_NAME(dstr) = SAVEPV(IoTOP_NAME(sstr)); | |
8070 | IoTOP_GV(dstr) = gv_dup(IoTOP_GV(sstr)); | |
8071 | IoFMT_NAME(dstr) = SAVEPV(IoFMT_NAME(sstr)); | |
8072 | IoFMT_GV(dstr) = gv_dup(IoFMT_GV(sstr)); | |
8073 | IoBOTTOM_NAME(dstr) = SAVEPV(IoBOTTOM_NAME(sstr)); | |
8074 | IoBOTTOM_GV(dstr) = gv_dup(IoBOTTOM_GV(sstr)); | |
8075 | IoSUBPROCESS(dstr) = IoSUBPROCESS(sstr); | |
8076 | IoTYPE(dstr) = IoTYPE(sstr); | |
8077 | IoFLAGS(dstr) = IoFLAGS(sstr); | |
8078 | break; | |
8079 | case SVt_PVAV: | |
8080 | SvANY(dstr) = new_XPVAV(); | |
8081 | SvCUR(dstr) = SvCUR(sstr); | |
8082 | SvLEN(dstr) = SvLEN(sstr); | |
8083 | SvIVX(dstr) = SvIVX(sstr); | |
8084 | SvNVX(dstr) = SvNVX(sstr); | |
8085 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
8086 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
8087 | AvARYLEN((AV*)dstr) = sv_dup_inc(AvARYLEN((AV*)sstr)); | |
8088 | AvFLAGS((AV*)dstr) = AvFLAGS((AV*)sstr); | |
8089 | if (AvARRAY((AV*)sstr)) { | |
8090 | SV **dst_ary, **src_ary; | |
8091 | SSize_t items = AvFILLp((AV*)sstr) + 1; | |
8092 | ||
8093 | src_ary = AvARRAY((AV*)sstr); | |
8094 | Newz(0, dst_ary, AvMAX((AV*)sstr)+1, SV*); | |
8095 | ptr_table_store(PL_ptr_table, src_ary, dst_ary); | |
8096 | SvPVX(dstr) = (char*)dst_ary; | |
8097 | AvALLOC((AV*)dstr) = dst_ary; | |
8098 | if (AvREAL((AV*)sstr)) { | |
8099 | while (items-- > 0) | |
8100 | *dst_ary++ = sv_dup_inc(*src_ary++); | |
8101 | } | |
8102 | else { | |
8103 | while (items-- > 0) | |
8104 | *dst_ary++ = sv_dup(*src_ary++); | |
8105 | } | |
8106 | items = AvMAX((AV*)sstr) - AvFILLp((AV*)sstr); | |
8107 | while (items-- > 0) { | |
8108 | *dst_ary++ = &PL_sv_undef; | |
8109 | } | |
8110 | } | |
8111 | else { | |
8112 | SvPVX(dstr) = Nullch; | |
8113 | AvALLOC((AV*)dstr) = (SV**)NULL; | |
8114 | } | |
8115 | break; | |
8116 | case SVt_PVHV: | |
8117 | SvANY(dstr) = new_XPVHV(); | |
8118 | SvCUR(dstr) = SvCUR(sstr); | |
8119 | SvLEN(dstr) = SvLEN(sstr); | |
8120 | SvIVX(dstr) = SvIVX(sstr); | |
8121 | SvNVX(dstr) = SvNVX(sstr); | |
8122 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
8123 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
8124 | HvRITER((HV*)dstr) = HvRITER((HV*)sstr); | |
8125 | if (HvARRAY((HV*)sstr)) { | |
1d7c1841 GS |
8126 | STRLEN i = 0; |
8127 | XPVHV *dxhv = (XPVHV*)SvANY(dstr); | |
8128 | XPVHV *sxhv = (XPVHV*)SvANY(sstr); | |
8129 | Newz(0, dxhv->xhv_array, | |
8130 | PERL_HV_ARRAY_ALLOC_BYTES(dxhv->xhv_max+1), char); | |
8131 | while (i <= sxhv->xhv_max) { | |
8132 | ((HE**)dxhv->xhv_array)[i] = he_dup(((HE**)sxhv->xhv_array)[i], | |
8133 | !!HvSHAREKEYS(sstr)); | |
8134 | ++i; | |
8135 | } | |
8136 | dxhv->xhv_eiter = he_dup(sxhv->xhv_eiter, !!HvSHAREKEYS(sstr)); | |
8137 | } | |
8138 | else { | |
8139 | SvPVX(dstr) = Nullch; | |
8140 | HvEITER((HV*)dstr) = (HE*)NULL; | |
8141 | } | |
8142 | HvPMROOT((HV*)dstr) = HvPMROOT((HV*)sstr); /* XXX */ | |
8143 | HvNAME((HV*)dstr) = SAVEPV(HvNAME((HV*)sstr)); | |
8144 | break; | |
8145 | case SVt_PVFM: | |
8146 | SvANY(dstr) = new_XPVFM(); | |
8147 | FmLINES(dstr) = FmLINES(sstr); | |
8148 | goto dup_pvcv; | |
8149 | /* NOTREACHED */ | |
8150 | case SVt_PVCV: | |
8151 | SvANY(dstr) = new_XPVCV(); | |
8152 | dup_pvcv: | |
8153 | SvCUR(dstr) = SvCUR(sstr); | |
8154 | SvLEN(dstr) = SvLEN(sstr); | |
8155 | SvIVX(dstr) = SvIVX(sstr); | |
8156 | SvNVX(dstr) = SvNVX(sstr); | |
8157 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
8158 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
8159 | if (SvPVX(sstr) && SvLEN(sstr)) | |
8160 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
8161 | else | |
8162 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
8163 | CvSTASH(dstr) = hv_dup(CvSTASH(sstr));/* NOTE: not refcounted */ | |
8164 | CvSTART(dstr) = CvSTART(sstr); | |
8165 | CvROOT(dstr) = OpREFCNT_inc(CvROOT(sstr)); | |
8166 | CvXSUB(dstr) = CvXSUB(sstr); | |
8167 | CvXSUBANY(dstr) = CvXSUBANY(sstr); | |
8168 | CvGV(dstr) = gv_dup_inc(CvGV(sstr)); | |
8169 | CvDEPTH(dstr) = CvDEPTH(sstr); | |
8170 | if (CvPADLIST(sstr) && !AvREAL(CvPADLIST(sstr))) { | |
8171 | /* XXX padlists are real, but pretend to be not */ | |
8172 | AvREAL_on(CvPADLIST(sstr)); | |
8173 | CvPADLIST(dstr) = av_dup_inc(CvPADLIST(sstr)); | |
8174 | AvREAL_off(CvPADLIST(sstr)); | |
8175 | AvREAL_off(CvPADLIST(dstr)); | |
8176 | } | |
8177 | else | |
8178 | CvPADLIST(dstr) = av_dup_inc(CvPADLIST(sstr)); | |
8179 | CvOUTSIDE(dstr) = cv_dup_inc(CvOUTSIDE(sstr)); | |
8180 | CvFLAGS(dstr) = CvFLAGS(sstr); | |
8181 | break; | |
8182 | default: | |
8183 | Perl_croak(aTHX_ "Bizarre SvTYPE [%d]", SvTYPE(sstr)); | |
8184 | break; | |
8185 | } | |
8186 | ||
8187 | if (SvOBJECT(dstr) && SvTYPE(dstr) != SVt_PVIO) | |
8188 | ++PL_sv_objcount; | |
8189 | ||
8190 | return dstr; | |
8191 | } | |
8192 | ||
8193 | PERL_CONTEXT * | |
8194 | Perl_cx_dup(pTHX_ PERL_CONTEXT *cxs, I32 ix, I32 max) | |
8195 | { | |
8196 | PERL_CONTEXT *ncxs; | |
8197 | ||
8198 | if (!cxs) | |
8199 | return (PERL_CONTEXT*)NULL; | |
8200 | ||
8201 | /* look for it in the table first */ | |
8202 | ncxs = (PERL_CONTEXT*)ptr_table_fetch(PL_ptr_table, cxs); | |
8203 | if (ncxs) | |
8204 | return ncxs; | |
8205 | ||
8206 | /* create anew and remember what it is */ | |
8207 | Newz(56, ncxs, max + 1, PERL_CONTEXT); | |
8208 | ptr_table_store(PL_ptr_table, cxs, ncxs); | |
8209 | ||
8210 | while (ix >= 0) { | |
8211 | PERL_CONTEXT *cx = &cxs[ix]; | |
8212 | PERL_CONTEXT *ncx = &ncxs[ix]; | |
8213 | ncx->cx_type = cx->cx_type; | |
8214 | if (CxTYPE(cx) == CXt_SUBST) { | |
8215 | Perl_croak(aTHX_ "Cloning substitution context is unimplemented"); | |
8216 | } | |
8217 | else { | |
8218 | ncx->blk_oldsp = cx->blk_oldsp; | |
8219 | ncx->blk_oldcop = cx->blk_oldcop; | |
8220 | ncx->blk_oldretsp = cx->blk_oldretsp; | |
8221 | ncx->blk_oldmarksp = cx->blk_oldmarksp; | |
8222 | ncx->blk_oldscopesp = cx->blk_oldscopesp; | |
8223 | ncx->blk_oldpm = cx->blk_oldpm; | |
8224 | ncx->blk_gimme = cx->blk_gimme; | |
8225 | switch (CxTYPE(cx)) { | |
8226 | case CXt_SUB: | |
8227 | ncx->blk_sub.cv = (cx->blk_sub.olddepth == 0 | |
8228 | ? cv_dup_inc(cx->blk_sub.cv) | |
8229 | : cv_dup(cx->blk_sub.cv)); | |
8230 | ncx->blk_sub.argarray = (cx->blk_sub.hasargs | |
8231 | ? av_dup_inc(cx->blk_sub.argarray) | |
8232 | : Nullav); | |
8233 | ncx->blk_sub.savearray = av_dup(cx->blk_sub.savearray); | |
8234 | ncx->blk_sub.olddepth = cx->blk_sub.olddepth; | |
8235 | ncx->blk_sub.hasargs = cx->blk_sub.hasargs; | |
8236 | ncx->blk_sub.lval = cx->blk_sub.lval; | |
8237 | break; | |
8238 | case CXt_EVAL: | |
8239 | ncx->blk_eval.old_in_eval = cx->blk_eval.old_in_eval; | |
8240 | ncx->blk_eval.old_op_type = cx->blk_eval.old_op_type; | |
0f79a09d | 8241 | ncx->blk_eval.old_namesv = sv_dup_inc(cx->blk_eval.old_namesv); |
1d7c1841 GS |
8242 | ncx->blk_eval.old_eval_root = cx->blk_eval.old_eval_root; |
8243 | ncx->blk_eval.cur_text = sv_dup(cx->blk_eval.cur_text); | |
8244 | break; | |
8245 | case CXt_LOOP: | |
8246 | ncx->blk_loop.label = cx->blk_loop.label; | |
8247 | ncx->blk_loop.resetsp = cx->blk_loop.resetsp; | |
8248 | ncx->blk_loop.redo_op = cx->blk_loop.redo_op; | |
8249 | ncx->blk_loop.next_op = cx->blk_loop.next_op; | |
8250 | ncx->blk_loop.last_op = cx->blk_loop.last_op; | |
8251 | ncx->blk_loop.iterdata = (CxPADLOOP(cx) | |
8252 | ? cx->blk_loop.iterdata | |
8253 | : gv_dup((GV*)cx->blk_loop.iterdata)); | |
a4b82a6f GS |
8254 | ncx->blk_loop.oldcurpad |
8255 | = (SV**)ptr_table_fetch(PL_ptr_table, | |
8256 | cx->blk_loop.oldcurpad); | |
1d7c1841 GS |
8257 | ncx->blk_loop.itersave = sv_dup_inc(cx->blk_loop.itersave); |
8258 | ncx->blk_loop.iterlval = sv_dup_inc(cx->blk_loop.iterlval); | |
8259 | ncx->blk_loop.iterary = av_dup_inc(cx->blk_loop.iterary); | |
8260 | ncx->blk_loop.iterix = cx->blk_loop.iterix; | |
8261 | ncx->blk_loop.itermax = cx->blk_loop.itermax; | |
8262 | break; | |
8263 | case CXt_FORMAT: | |
8264 | ncx->blk_sub.cv = cv_dup(cx->blk_sub.cv); | |
8265 | ncx->blk_sub.gv = gv_dup(cx->blk_sub.gv); | |
8266 | ncx->blk_sub.dfoutgv = gv_dup_inc(cx->blk_sub.dfoutgv); | |
8267 | ncx->blk_sub.hasargs = cx->blk_sub.hasargs; | |
8268 | break; | |
8269 | case CXt_BLOCK: | |
8270 | case CXt_NULL: | |
8271 | break; | |
8272 | } | |
8273 | } | |
8274 | --ix; | |
8275 | } | |
8276 | return ncxs; | |
8277 | } | |
8278 | ||
8279 | PERL_SI * | |
8280 | Perl_si_dup(pTHX_ PERL_SI *si) | |
8281 | { | |
8282 | PERL_SI *nsi; | |
8283 | ||
8284 | if (!si) | |
8285 | return (PERL_SI*)NULL; | |
8286 | ||
8287 | /* look for it in the table first */ | |
8288 | nsi = (PERL_SI*)ptr_table_fetch(PL_ptr_table, si); | |
8289 | if (nsi) | |
8290 | return nsi; | |
8291 | ||
8292 | /* create anew and remember what it is */ | |
8293 | Newz(56, nsi, 1, PERL_SI); | |
8294 | ptr_table_store(PL_ptr_table, si, nsi); | |
8295 | ||
8296 | nsi->si_stack = av_dup_inc(si->si_stack); | |
8297 | nsi->si_cxix = si->si_cxix; | |
8298 | nsi->si_cxmax = si->si_cxmax; | |
8299 | nsi->si_cxstack = cx_dup(si->si_cxstack, si->si_cxix, si->si_cxmax); | |
8300 | nsi->si_type = si->si_type; | |
8301 | nsi->si_prev = si_dup(si->si_prev); | |
8302 | nsi->si_next = si_dup(si->si_next); | |
8303 | nsi->si_markoff = si->si_markoff; | |
8304 | ||
8305 | return nsi; | |
8306 | } | |
8307 | ||
8308 | #define POPINT(ss,ix) ((ss)[--(ix)].any_i32) | |
8309 | #define TOPINT(ss,ix) ((ss)[ix].any_i32) | |
8310 | #define POPLONG(ss,ix) ((ss)[--(ix)].any_long) | |
8311 | #define TOPLONG(ss,ix) ((ss)[ix].any_long) | |
8312 | #define POPIV(ss,ix) ((ss)[--(ix)].any_iv) | |
8313 | #define TOPIV(ss,ix) ((ss)[ix].any_iv) | |
8314 | #define POPPTR(ss,ix) ((ss)[--(ix)].any_ptr) | |
8315 | #define TOPPTR(ss,ix) ((ss)[ix].any_ptr) | |
8316 | #define POPDPTR(ss,ix) ((ss)[--(ix)].any_dptr) | |
8317 | #define TOPDPTR(ss,ix) ((ss)[ix].any_dptr) | |
8318 | #define POPDXPTR(ss,ix) ((ss)[--(ix)].any_dxptr) | |
8319 | #define TOPDXPTR(ss,ix) ((ss)[ix].any_dxptr) | |
8320 | ||
8321 | /* XXXXX todo */ | |
8322 | #define pv_dup_inc(p) SAVEPV(p) | |
8323 | #define pv_dup(p) SAVEPV(p) | |
8324 | #define svp_dup_inc(p,pp) any_dup(p,pp) | |
8325 | ||
8326 | void * | |
8327 | Perl_any_dup(pTHX_ void *v, PerlInterpreter *proto_perl) | |
8328 | { | |
8329 | void *ret; | |
8330 | ||
8331 | if (!v) | |
8332 | return (void*)NULL; | |
8333 | ||
8334 | /* look for it in the table first */ | |
8335 | ret = ptr_table_fetch(PL_ptr_table, v); | |
8336 | if (ret) | |
8337 | return ret; | |
8338 | ||
8339 | /* see if it is part of the interpreter structure */ | |
8340 | if (v >= (void*)proto_perl && v < (void*)(proto_perl+1)) | |
8341 | ret = (void*)(((char*)aTHXo) + (((char*)v) - (char*)proto_perl)); | |
8342 | else | |
8343 | ret = v; | |
8344 | ||
8345 | return ret; | |
8346 | } | |
8347 | ||
8348 | ANY * | |
8349 | Perl_ss_dup(pTHX_ PerlInterpreter *proto_perl) | |
8350 | { | |
8351 | ANY *ss = proto_perl->Tsavestack; | |
8352 | I32 ix = proto_perl->Tsavestack_ix; | |
8353 | I32 max = proto_perl->Tsavestack_max; | |
8354 | ANY *nss; | |
8355 | SV *sv; | |
8356 | GV *gv; | |
8357 | AV *av; | |
8358 | HV *hv; | |
8359 | void* ptr; | |
8360 | int intval; | |
8361 | long longval; | |
8362 | GP *gp; | |
8363 | IV iv; | |
8364 | I32 i; | |
8365 | char *c; | |
8366 | void (*dptr) (void*); | |
8367 | void (*dxptr) (pTHXo_ void*); | |
e977893f | 8368 | OP *o; |
1d7c1841 GS |
8369 | |
8370 | Newz(54, nss, max, ANY); | |
8371 | ||
8372 | while (ix > 0) { | |
8373 | i = POPINT(ss,ix); | |
8374 | TOPINT(nss,ix) = i; | |
8375 | switch (i) { | |
8376 | case SAVEt_ITEM: /* normal string */ | |
8377 | sv = (SV*)POPPTR(ss,ix); | |
8378 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8379 | sv = (SV*)POPPTR(ss,ix); | |
8380 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8381 | break; | |
8382 | case SAVEt_SV: /* scalar reference */ | |
8383 | sv = (SV*)POPPTR(ss,ix); | |
8384 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8385 | gv = (GV*)POPPTR(ss,ix); | |
8386 | TOPPTR(nss,ix) = gv_dup_inc(gv); | |
8387 | break; | |
f4dd75d9 GS |
8388 | case SAVEt_GENERIC_PVREF: /* generic char* */ |
8389 | c = (char*)POPPTR(ss,ix); | |
8390 | TOPPTR(nss,ix) = pv_dup(c); | |
8391 | ptr = POPPTR(ss,ix); | |
8392 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8393 | break; | |
1d7c1841 GS |
8394 | case SAVEt_GENERIC_SVREF: /* generic sv */ |
8395 | case SAVEt_SVREF: /* scalar reference */ | |
8396 | sv = (SV*)POPPTR(ss,ix); | |
8397 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8398 | ptr = POPPTR(ss,ix); | |
8399 | TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */ | |
8400 | break; | |
8401 | case SAVEt_AV: /* array reference */ | |
8402 | av = (AV*)POPPTR(ss,ix); | |
8403 | TOPPTR(nss,ix) = av_dup_inc(av); | |
8404 | gv = (GV*)POPPTR(ss,ix); | |
8405 | TOPPTR(nss,ix) = gv_dup(gv); | |
8406 | break; | |
8407 | case SAVEt_HV: /* hash reference */ | |
8408 | hv = (HV*)POPPTR(ss,ix); | |
8409 | TOPPTR(nss,ix) = hv_dup_inc(hv); | |
8410 | gv = (GV*)POPPTR(ss,ix); | |
8411 | TOPPTR(nss,ix) = gv_dup(gv); | |
8412 | break; | |
8413 | case SAVEt_INT: /* int reference */ | |
8414 | ptr = POPPTR(ss,ix); | |
8415 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8416 | intval = (int)POPINT(ss,ix); | |
8417 | TOPINT(nss,ix) = intval; | |
8418 | break; | |
8419 | case SAVEt_LONG: /* long reference */ | |
8420 | ptr = POPPTR(ss,ix); | |
8421 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8422 | longval = (long)POPLONG(ss,ix); | |
8423 | TOPLONG(nss,ix) = longval; | |
8424 | break; | |
8425 | case SAVEt_I32: /* I32 reference */ | |
8426 | case SAVEt_I16: /* I16 reference */ | |
8427 | case SAVEt_I8: /* I8 reference */ | |
8428 | ptr = POPPTR(ss,ix); | |
8429 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8430 | i = POPINT(ss,ix); | |
8431 | TOPINT(nss,ix) = i; | |
8432 | break; | |
8433 | case SAVEt_IV: /* IV reference */ | |
8434 | ptr = POPPTR(ss,ix); | |
8435 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8436 | iv = POPIV(ss,ix); | |
8437 | TOPIV(nss,ix) = iv; | |
8438 | break; | |
8439 | case SAVEt_SPTR: /* SV* reference */ | |
8440 | ptr = POPPTR(ss,ix); | |
8441 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8442 | sv = (SV*)POPPTR(ss,ix); | |
8443 | TOPPTR(nss,ix) = sv_dup(sv); | |
8444 | break; | |
8445 | case SAVEt_VPTR: /* random* reference */ | |
8446 | ptr = POPPTR(ss,ix); | |
8447 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8448 | ptr = POPPTR(ss,ix); | |
8449 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8450 | break; | |
8451 | case SAVEt_PPTR: /* char* reference */ | |
8452 | ptr = POPPTR(ss,ix); | |
8453 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8454 | c = (char*)POPPTR(ss,ix); | |
8455 | TOPPTR(nss,ix) = pv_dup(c); | |
8456 | break; | |
8457 | case SAVEt_HPTR: /* HV* reference */ | |
8458 | ptr = POPPTR(ss,ix); | |
8459 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8460 | hv = (HV*)POPPTR(ss,ix); | |
8461 | TOPPTR(nss,ix) = hv_dup(hv); | |
8462 | break; | |
8463 | case SAVEt_APTR: /* AV* reference */ | |
8464 | ptr = POPPTR(ss,ix); | |
8465 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8466 | av = (AV*)POPPTR(ss,ix); | |
8467 | TOPPTR(nss,ix) = av_dup(av); | |
8468 | break; | |
8469 | case SAVEt_NSTAB: | |
8470 | gv = (GV*)POPPTR(ss,ix); | |
8471 | TOPPTR(nss,ix) = gv_dup(gv); | |
8472 | break; | |
8473 | case SAVEt_GP: /* scalar reference */ | |
8474 | gp = (GP*)POPPTR(ss,ix); | |
8475 | TOPPTR(nss,ix) = gp = gp_dup(gp); | |
8476 | (void)GpREFCNT_inc(gp); | |
8477 | gv = (GV*)POPPTR(ss,ix); | |
8478 | TOPPTR(nss,ix) = gv_dup_inc(c); | |
8479 | c = (char*)POPPTR(ss,ix); | |
8480 | TOPPTR(nss,ix) = pv_dup(c); | |
8481 | iv = POPIV(ss,ix); | |
8482 | TOPIV(nss,ix) = iv; | |
8483 | iv = POPIV(ss,ix); | |
8484 | TOPIV(nss,ix) = iv; | |
8485 | break; | |
8486 | case SAVEt_FREESV: | |
8487 | sv = (SV*)POPPTR(ss,ix); | |
8488 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8489 | break; | |
8490 | case SAVEt_FREEOP: | |
8491 | ptr = POPPTR(ss,ix); | |
8492 | if (ptr && (((OP*)ptr)->op_private & OPpREFCOUNTED)) { | |
8493 | /* these are assumed to be refcounted properly */ | |
8494 | switch (((OP*)ptr)->op_type) { | |
8495 | case OP_LEAVESUB: | |
8496 | case OP_LEAVESUBLV: | |
8497 | case OP_LEAVEEVAL: | |
8498 | case OP_LEAVE: | |
8499 | case OP_SCOPE: | |
8500 | case OP_LEAVEWRITE: | |
e977893f GS |
8501 | TOPPTR(nss,ix) = ptr; |
8502 | o = (OP*)ptr; | |
8503 | OpREFCNT_inc(o); | |
1d7c1841 GS |
8504 | break; |
8505 | default: | |
8506 | TOPPTR(nss,ix) = Nullop; | |
8507 | break; | |
8508 | } | |
8509 | } | |
8510 | else | |
8511 | TOPPTR(nss,ix) = Nullop; | |
8512 | break; | |
8513 | case SAVEt_FREEPV: | |
8514 | c = (char*)POPPTR(ss,ix); | |
8515 | TOPPTR(nss,ix) = pv_dup_inc(c); | |
8516 | break; | |
8517 | case SAVEt_CLEARSV: | |
8518 | longval = POPLONG(ss,ix); | |
8519 | TOPLONG(nss,ix) = longval; | |
8520 | break; | |
8521 | case SAVEt_DELETE: | |
8522 | hv = (HV*)POPPTR(ss,ix); | |
8523 | TOPPTR(nss,ix) = hv_dup_inc(hv); | |
8524 | c = (char*)POPPTR(ss,ix); | |
8525 | TOPPTR(nss,ix) = pv_dup_inc(c); | |
8526 | i = POPINT(ss,ix); | |
8527 | TOPINT(nss,ix) = i; | |
8528 | break; | |
8529 | case SAVEt_DESTRUCTOR: | |
8530 | ptr = POPPTR(ss,ix); | |
8531 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */ | |
8532 | dptr = POPDPTR(ss,ix); | |
ef75a179 | 8533 | TOPDPTR(nss,ix) = (void (*)(void*))any_dup((void *)dptr, proto_perl); |
1d7c1841 GS |
8534 | break; |
8535 | case SAVEt_DESTRUCTOR_X: | |
8536 | ptr = POPPTR(ss,ix); | |
8537 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */ | |
8538 | dxptr = POPDXPTR(ss,ix); | |
ef75a179 | 8539 | TOPDXPTR(nss,ix) = (void (*)(pTHXo_ void*))any_dup((void *)dxptr, proto_perl); |
1d7c1841 GS |
8540 | break; |
8541 | case SAVEt_REGCONTEXT: | |
8542 | case SAVEt_ALLOC: | |
8543 | i = POPINT(ss,ix); | |
8544 | TOPINT(nss,ix) = i; | |
8545 | ix -= i; | |
8546 | break; | |
8547 | case SAVEt_STACK_POS: /* Position on Perl stack */ | |
8548 | i = POPINT(ss,ix); | |
8549 | TOPINT(nss,ix) = i; | |
8550 | break; | |
8551 | case SAVEt_AELEM: /* array element */ | |
8552 | sv = (SV*)POPPTR(ss,ix); | |
8553 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8554 | i = POPINT(ss,ix); | |
8555 | TOPINT(nss,ix) = i; | |
8556 | av = (AV*)POPPTR(ss,ix); | |
8557 | TOPPTR(nss,ix) = av_dup_inc(av); | |
8558 | break; | |
8559 | case SAVEt_HELEM: /* hash element */ | |
8560 | sv = (SV*)POPPTR(ss,ix); | |
8561 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8562 | sv = (SV*)POPPTR(ss,ix); | |
8563 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8564 | hv = (HV*)POPPTR(ss,ix); | |
8565 | TOPPTR(nss,ix) = hv_dup_inc(hv); | |
8566 | break; | |
8567 | case SAVEt_OP: | |
8568 | ptr = POPPTR(ss,ix); | |
8569 | TOPPTR(nss,ix) = ptr; | |
8570 | break; | |
8571 | case SAVEt_HINTS: | |
8572 | i = POPINT(ss,ix); | |
8573 | TOPINT(nss,ix) = i; | |
8574 | break; | |
c4410b1b GS |
8575 | case SAVEt_COMPPAD: |
8576 | av = (AV*)POPPTR(ss,ix); | |
8577 | TOPPTR(nss,ix) = av_dup(av); | |
8578 | break; | |
c3564e5c GS |
8579 | case SAVEt_PADSV: |
8580 | longval = (long)POPLONG(ss,ix); | |
8581 | TOPLONG(nss,ix) = longval; | |
8582 | ptr = POPPTR(ss,ix); | |
8583 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8584 | sv = (SV*)POPPTR(ss,ix); | |
8585 | TOPPTR(nss,ix) = sv_dup(sv); | |
8586 | break; | |
1d7c1841 GS |
8587 | default: |
8588 | Perl_croak(aTHX_ "panic: ss_dup inconsistency"); | |
8589 | } | |
8590 | } | |
8591 | ||
8592 | return nss; | |
8593 | } | |
8594 | ||
8595 | #ifdef PERL_OBJECT | |
8596 | #include "XSUB.h" | |
8597 | #endif | |
8598 | ||
8599 | PerlInterpreter * | |
8600 | perl_clone(PerlInterpreter *proto_perl, UV flags) | |
8601 | { | |
8602 | #ifdef PERL_OBJECT | |
8603 | CPerlObj *pPerl = (CPerlObj*)proto_perl; | |
8604 | #endif | |
8605 | ||
8606 | #ifdef PERL_IMPLICIT_SYS | |
8607 | return perl_clone_using(proto_perl, flags, | |
8608 | proto_perl->IMem, | |
8609 | proto_perl->IMemShared, | |
8610 | proto_perl->IMemParse, | |
8611 | proto_perl->IEnv, | |
8612 | proto_perl->IStdIO, | |
8613 | proto_perl->ILIO, | |
8614 | proto_perl->IDir, | |
8615 | proto_perl->ISock, | |
8616 | proto_perl->IProc); | |
8617 | } | |
8618 | ||
8619 | PerlInterpreter * | |
8620 | perl_clone_using(PerlInterpreter *proto_perl, UV flags, | |
8621 | struct IPerlMem* ipM, struct IPerlMem* ipMS, | |
8622 | struct IPerlMem* ipMP, struct IPerlEnv* ipE, | |
8623 | struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO, | |
8624 | struct IPerlDir* ipD, struct IPerlSock* ipS, | |
8625 | struct IPerlProc* ipP) | |
8626 | { | |
8627 | /* XXX many of the string copies here can be optimized if they're | |
8628 | * constants; they need to be allocated as common memory and just | |
8629 | * their pointers copied. */ | |
8630 | ||
8631 | IV i; | |
1d7c1841 GS |
8632 | # ifdef PERL_OBJECT |
8633 | CPerlObj *pPerl = new(ipM) CPerlObj(ipM, ipMS, ipMP, ipE, ipStd, ipLIO, | |
8634 | ipD, ipS, ipP); | |
ba869deb | 8635 | PERL_SET_THX(pPerl); |
1d7c1841 GS |
8636 | # else /* !PERL_OBJECT */ |
8637 | PerlInterpreter *my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter)); | |
ba869deb | 8638 | PERL_SET_THX(my_perl); |
1d7c1841 GS |
8639 | |
8640 | # ifdef DEBUGGING | |
8641 | memset(my_perl, 0xab, sizeof(PerlInterpreter)); | |
8642 | PL_markstack = 0; | |
8643 | PL_scopestack = 0; | |
8644 | PL_savestack = 0; | |
8645 | PL_retstack = 0; | |
66fe0623 | 8646 | PL_sig_pending = 0; |
1d7c1841 GS |
8647 | # else /* !DEBUGGING */ |
8648 | Zero(my_perl, 1, PerlInterpreter); | |
8649 | # endif /* DEBUGGING */ | |
8650 | ||
8651 | /* host pointers */ | |
8652 | PL_Mem = ipM; | |
8653 | PL_MemShared = ipMS; | |
8654 | PL_MemParse = ipMP; | |
8655 | PL_Env = ipE; | |
8656 | PL_StdIO = ipStd; | |
8657 | PL_LIO = ipLIO; | |
8658 | PL_Dir = ipD; | |
8659 | PL_Sock = ipS; | |
8660 | PL_Proc = ipP; | |
8661 | # endif /* PERL_OBJECT */ | |
8662 | #else /* !PERL_IMPLICIT_SYS */ | |
8663 | IV i; | |
1d7c1841 | 8664 | PerlInterpreter *my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter)); |
ba869deb | 8665 | PERL_SET_THX(my_perl); |
1d7c1841 GS |
8666 | |
8667 | # ifdef DEBUGGING | |
8668 | memset(my_perl, 0xab, sizeof(PerlInterpreter)); | |
8669 | PL_markstack = 0; | |
8670 | PL_scopestack = 0; | |
8671 | PL_savestack = 0; | |
8672 | PL_retstack = 0; | |
66fe0623 | 8673 | PL_sig_pending = 0; |
1d7c1841 GS |
8674 | # else /* !DEBUGGING */ |
8675 | Zero(my_perl, 1, PerlInterpreter); | |
8676 | # endif /* DEBUGGING */ | |
8677 | #endif /* PERL_IMPLICIT_SYS */ | |
8678 | ||
8679 | /* arena roots */ | |
8680 | PL_xiv_arenaroot = NULL; | |
8681 | PL_xiv_root = NULL; | |
612f20c3 | 8682 | PL_xnv_arenaroot = NULL; |
1d7c1841 | 8683 | PL_xnv_root = NULL; |
612f20c3 | 8684 | PL_xrv_arenaroot = NULL; |
1d7c1841 | 8685 | PL_xrv_root = NULL; |
612f20c3 | 8686 | PL_xpv_arenaroot = NULL; |
1d7c1841 | 8687 | PL_xpv_root = NULL; |
612f20c3 | 8688 | PL_xpviv_arenaroot = NULL; |
1d7c1841 | 8689 | PL_xpviv_root = NULL; |
612f20c3 | 8690 | PL_xpvnv_arenaroot = NULL; |
1d7c1841 | 8691 | PL_xpvnv_root = NULL; |
612f20c3 | 8692 | PL_xpvcv_arenaroot = NULL; |
1d7c1841 | 8693 | PL_xpvcv_root = NULL; |
612f20c3 | 8694 | PL_xpvav_arenaroot = NULL; |
1d7c1841 | 8695 | PL_xpvav_root = NULL; |
612f20c3 | 8696 | PL_xpvhv_arenaroot = NULL; |
1d7c1841 | 8697 | PL_xpvhv_root = NULL; |
612f20c3 | 8698 | PL_xpvmg_arenaroot = NULL; |
1d7c1841 | 8699 | PL_xpvmg_root = NULL; |
612f20c3 | 8700 | PL_xpvlv_arenaroot = NULL; |
1d7c1841 | 8701 | PL_xpvlv_root = NULL; |
612f20c3 | 8702 | PL_xpvbm_arenaroot = NULL; |
1d7c1841 | 8703 | PL_xpvbm_root = NULL; |
612f20c3 | 8704 | PL_he_arenaroot = NULL; |
1d7c1841 GS |
8705 | PL_he_root = NULL; |
8706 | PL_nice_chunk = NULL; | |
8707 | PL_nice_chunk_size = 0; | |
8708 | PL_sv_count = 0; | |
8709 | PL_sv_objcount = 0; | |
8710 | PL_sv_root = Nullsv; | |
8711 | PL_sv_arenaroot = Nullsv; | |
8712 | ||
8713 | PL_debug = proto_perl->Idebug; | |
8714 | ||
8715 | /* create SV map for pointer relocation */ | |
8716 | PL_ptr_table = ptr_table_new(); | |
8717 | ||
8718 | /* initialize these special pointers as early as possible */ | |
8719 | SvANY(&PL_sv_undef) = NULL; | |
8720 | SvREFCNT(&PL_sv_undef) = (~(U32)0)/2; | |
8721 | SvFLAGS(&PL_sv_undef) = SVf_READONLY|SVt_NULL; | |
8722 | ptr_table_store(PL_ptr_table, &proto_perl->Isv_undef, &PL_sv_undef); | |
8723 | ||
8724 | #ifdef PERL_OBJECT | |
8725 | SvUPGRADE(&PL_sv_no, SVt_PVNV); | |
8726 | #else | |
8727 | SvANY(&PL_sv_no) = new_XPVNV(); | |
8728 | #endif | |
8729 | SvREFCNT(&PL_sv_no) = (~(U32)0)/2; | |
8730 | SvFLAGS(&PL_sv_no) = SVp_NOK|SVf_NOK|SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV; | |
8731 | SvPVX(&PL_sv_no) = SAVEPVN(PL_No, 0); | |
8732 | SvCUR(&PL_sv_no) = 0; | |
8733 | SvLEN(&PL_sv_no) = 1; | |
8734 | SvNVX(&PL_sv_no) = 0; | |
8735 | ptr_table_store(PL_ptr_table, &proto_perl->Isv_no, &PL_sv_no); | |
8736 | ||
8737 | #ifdef PERL_OBJECT | |
8738 | SvUPGRADE(&PL_sv_yes, SVt_PVNV); | |
8739 | #else | |
8740 | SvANY(&PL_sv_yes) = new_XPVNV(); | |
8741 | #endif | |
8742 | SvREFCNT(&PL_sv_yes) = (~(U32)0)/2; | |
8743 | SvFLAGS(&PL_sv_yes) = SVp_NOK|SVf_NOK|SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV; | |
8744 | SvPVX(&PL_sv_yes) = SAVEPVN(PL_Yes, 1); | |
8745 | SvCUR(&PL_sv_yes) = 1; | |
8746 | SvLEN(&PL_sv_yes) = 2; | |
8747 | SvNVX(&PL_sv_yes) = 1; | |
8748 | ptr_table_store(PL_ptr_table, &proto_perl->Isv_yes, &PL_sv_yes); | |
8749 | ||
8750 | /* create shared string table */ | |
8751 | PL_strtab = newHV(); | |
8752 | HvSHAREKEYS_off(PL_strtab); | |
8753 | hv_ksplit(PL_strtab, 512); | |
8754 | ptr_table_store(PL_ptr_table, proto_perl->Istrtab, PL_strtab); | |
8755 | ||
8756 | PL_compiling = proto_perl->Icompiling; | |
8757 | PL_compiling.cop_stashpv = SAVEPV(PL_compiling.cop_stashpv); | |
8758 | PL_compiling.cop_file = SAVEPV(PL_compiling.cop_file); | |
8759 | ptr_table_store(PL_ptr_table, &proto_perl->Icompiling, &PL_compiling); | |
8760 | if (!specialWARN(PL_compiling.cop_warnings)) | |
8761 | PL_compiling.cop_warnings = sv_dup_inc(PL_compiling.cop_warnings); | |
ac27b0f5 NIS |
8762 | if (!specialCopIO(PL_compiling.cop_io)) |
8763 | PL_compiling.cop_io = sv_dup_inc(PL_compiling.cop_io); | |
1d7c1841 GS |
8764 | PL_curcop = (COP*)any_dup(proto_perl->Tcurcop, proto_perl); |
8765 | ||
8766 | /* pseudo environmental stuff */ | |
8767 | PL_origargc = proto_perl->Iorigargc; | |
8768 | i = PL_origargc; | |
8769 | New(0, PL_origargv, i+1, char*); | |
8770 | PL_origargv[i] = '\0'; | |
8771 | while (i-- > 0) { | |
8772 | PL_origargv[i] = SAVEPV(proto_perl->Iorigargv[i]); | |
8773 | } | |
8774 | PL_envgv = gv_dup(proto_perl->Ienvgv); | |
8775 | PL_incgv = gv_dup(proto_perl->Iincgv); | |
8776 | PL_hintgv = gv_dup(proto_perl->Ihintgv); | |
8777 | PL_origfilename = SAVEPV(proto_perl->Iorigfilename); | |
8778 | PL_diehook = sv_dup_inc(proto_perl->Idiehook); | |
8779 | PL_warnhook = sv_dup_inc(proto_perl->Iwarnhook); | |
8780 | ||
8781 | /* switches */ | |
8782 | PL_minus_c = proto_perl->Iminus_c; | |
a7cb1f99 | 8783 | PL_patchlevel = sv_dup_inc(proto_perl->Ipatchlevel); |
1d7c1841 GS |
8784 | PL_localpatches = proto_perl->Ilocalpatches; |
8785 | PL_splitstr = proto_perl->Isplitstr; | |
8786 | PL_preprocess = proto_perl->Ipreprocess; | |
8787 | PL_minus_n = proto_perl->Iminus_n; | |
8788 | PL_minus_p = proto_perl->Iminus_p; | |
8789 | PL_minus_l = proto_perl->Iminus_l; | |
8790 | PL_minus_a = proto_perl->Iminus_a; | |
8791 | PL_minus_F = proto_perl->Iminus_F; | |
8792 | PL_doswitches = proto_perl->Idoswitches; | |
8793 | PL_dowarn = proto_perl->Idowarn; | |
8794 | PL_doextract = proto_perl->Idoextract; | |
8795 | PL_sawampersand = proto_perl->Isawampersand; | |
8796 | PL_unsafe = proto_perl->Iunsafe; | |
8797 | PL_inplace = SAVEPV(proto_perl->Iinplace); | |
8798 | PL_e_script = sv_dup_inc(proto_perl->Ie_script); | |
8799 | PL_perldb = proto_perl->Iperldb; | |
8800 | PL_perl_destruct_level = proto_perl->Iperl_destruct_level; | |
8801 | ||
8802 | /* magical thingies */ | |
8803 | /* XXX time(&PL_basetime) when asked for? */ | |
8804 | PL_basetime = proto_perl->Ibasetime; | |
8805 | PL_formfeed = sv_dup(proto_perl->Iformfeed); | |
8806 | ||
8807 | PL_maxsysfd = proto_perl->Imaxsysfd; | |
8808 | PL_multiline = proto_perl->Imultiline; | |
8809 | PL_statusvalue = proto_perl->Istatusvalue; | |
8810 | #ifdef VMS | |
8811 | PL_statusvalue_vms = proto_perl->Istatusvalue_vms; | |
8812 | #endif | |
8813 | ||
8814 | /* shortcuts to various I/O objects */ | |
8815 | PL_stdingv = gv_dup(proto_perl->Istdingv); | |
8816 | PL_stderrgv = gv_dup(proto_perl->Istderrgv); | |
8817 | PL_defgv = gv_dup(proto_perl->Idefgv); | |
8818 | PL_argvgv = gv_dup(proto_perl->Iargvgv); | |
8819 | PL_argvoutgv = gv_dup(proto_perl->Iargvoutgv); | |
8820 | PL_argvout_stack = av_dup(proto_perl->Iargvout_stack); | |
8821 | ||
8822 | /* shortcuts to regexp stuff */ | |
8823 | PL_replgv = gv_dup(proto_perl->Ireplgv); | |
8824 | ||
8825 | /* shortcuts to misc objects */ | |
8826 | PL_errgv = gv_dup(proto_perl->Ierrgv); | |
8827 | ||
8828 | /* shortcuts to debugging objects */ | |
8829 | PL_DBgv = gv_dup(proto_perl->IDBgv); | |
8830 | PL_DBline = gv_dup(proto_perl->IDBline); | |
8831 | PL_DBsub = gv_dup(proto_perl->IDBsub); | |
8832 | PL_DBsingle = sv_dup(proto_perl->IDBsingle); | |
8833 | PL_DBtrace = sv_dup(proto_perl->IDBtrace); | |
8834 | PL_DBsignal = sv_dup(proto_perl->IDBsignal); | |
8835 | PL_lineary = av_dup(proto_perl->Ilineary); | |
8836 | PL_dbargs = av_dup(proto_perl->Idbargs); | |
8837 | ||
8838 | /* symbol tables */ | |
8839 | PL_defstash = hv_dup_inc(proto_perl->Tdefstash); | |
8840 | PL_curstash = hv_dup(proto_perl->Tcurstash); | |
8841 | PL_debstash = hv_dup(proto_perl->Idebstash); | |
8842 | PL_globalstash = hv_dup(proto_perl->Iglobalstash); | |
8843 | PL_curstname = sv_dup_inc(proto_perl->Icurstname); | |
8844 | ||
8845 | PL_beginav = av_dup_inc(proto_perl->Ibeginav); | |
8846 | PL_endav = av_dup_inc(proto_perl->Iendav); | |
7d30b5c4 | 8847 | PL_checkav = av_dup_inc(proto_perl->Icheckav); |
1d7c1841 GS |
8848 | PL_initav = av_dup_inc(proto_perl->Iinitav); |
8849 | ||
8850 | PL_sub_generation = proto_perl->Isub_generation; | |
8851 | ||
8852 | /* funky return mechanisms */ | |
8853 | PL_forkprocess = proto_perl->Iforkprocess; | |
8854 | ||
8855 | /* subprocess state */ | |
8856 | PL_fdpid = av_dup_inc(proto_perl->Ifdpid); | |
8857 | ||
8858 | /* internal state */ | |
8859 | PL_tainting = proto_perl->Itainting; | |
8860 | PL_maxo = proto_perl->Imaxo; | |
8861 | if (proto_perl->Iop_mask) | |
8862 | PL_op_mask = SAVEPVN(proto_perl->Iop_mask, PL_maxo); | |
8863 | else | |
8864 | PL_op_mask = Nullch; | |
8865 | ||
8866 | /* current interpreter roots */ | |
8867 | PL_main_cv = cv_dup_inc(proto_perl->Imain_cv); | |
8868 | PL_main_root = OpREFCNT_inc(proto_perl->Imain_root); | |
8869 | PL_main_start = proto_perl->Imain_start; | |
e977893f | 8870 | PL_eval_root = proto_perl->Ieval_root; |
1d7c1841 GS |
8871 | PL_eval_start = proto_perl->Ieval_start; |
8872 | ||
8873 | /* runtime control stuff */ | |
8874 | PL_curcopdb = (COP*)any_dup(proto_perl->Icurcopdb, proto_perl); | |
8875 | PL_copline = proto_perl->Icopline; | |
8876 | ||
8877 | PL_filemode = proto_perl->Ifilemode; | |
8878 | PL_lastfd = proto_perl->Ilastfd; | |
8879 | PL_oldname = proto_perl->Ioldname; /* XXX not quite right */ | |
8880 | PL_Argv = NULL; | |
8881 | PL_Cmd = Nullch; | |
8882 | PL_gensym = proto_perl->Igensym; | |
8883 | PL_preambled = proto_perl->Ipreambled; | |
8884 | PL_preambleav = av_dup_inc(proto_perl->Ipreambleav); | |
8885 | PL_laststatval = proto_perl->Ilaststatval; | |
8886 | PL_laststype = proto_perl->Ilaststype; | |
8887 | PL_mess_sv = Nullsv; | |
8888 | ||
7889fe52 | 8889 | PL_ors_sv = sv_dup_inc(proto_perl->Iors_sv); |
1d7c1841 GS |
8890 | PL_ofmt = SAVEPV(proto_perl->Iofmt); |
8891 | ||
8892 | /* interpreter atexit processing */ | |
8893 | PL_exitlistlen = proto_perl->Iexitlistlen; | |
8894 | if (PL_exitlistlen) { | |
8895 | New(0, PL_exitlist, PL_exitlistlen, PerlExitListEntry); | |
8896 | Copy(proto_perl->Iexitlist, PL_exitlist, PL_exitlistlen, PerlExitListEntry); | |
8897 | } | |
8898 | else | |
8899 | PL_exitlist = (PerlExitListEntry*)NULL; | |
8900 | PL_modglobal = hv_dup_inc(proto_perl->Imodglobal); | |
8901 | ||
8902 | PL_profiledata = NULL; | |
8903 | PL_rsfp = fp_dup(proto_perl->Irsfp, '<'); | |
8904 | /* PL_rsfp_filters entries have fake IoDIRP() */ | |
8905 | PL_rsfp_filters = av_dup_inc(proto_perl->Irsfp_filters); | |
8906 | ||
8907 | PL_compcv = cv_dup(proto_perl->Icompcv); | |
8908 | PL_comppad = av_dup(proto_perl->Icomppad); | |
8909 | PL_comppad_name = av_dup(proto_perl->Icomppad_name); | |
8910 | PL_comppad_name_fill = proto_perl->Icomppad_name_fill; | |
8911 | PL_comppad_name_floor = proto_perl->Icomppad_name_floor; | |
8912 | PL_curpad = (SV**)ptr_table_fetch(PL_ptr_table, | |
8913 | proto_perl->Tcurpad); | |
8914 | ||
8915 | #ifdef HAVE_INTERP_INTERN | |
8916 | sys_intern_dup(&proto_perl->Isys_intern, &PL_sys_intern); | |
8917 | #endif | |
8918 | ||
8919 | /* more statics moved here */ | |
8920 | PL_generation = proto_perl->Igeneration; | |
8921 | PL_DBcv = cv_dup(proto_perl->IDBcv); | |
1d7c1841 GS |
8922 | |
8923 | PL_in_clean_objs = proto_perl->Iin_clean_objs; | |
8924 | PL_in_clean_all = proto_perl->Iin_clean_all; | |
8925 | ||
8926 | PL_uid = proto_perl->Iuid; | |
8927 | PL_euid = proto_perl->Ieuid; | |
8928 | PL_gid = proto_perl->Igid; | |
8929 | PL_egid = proto_perl->Iegid; | |
8930 | PL_nomemok = proto_perl->Inomemok; | |
8931 | PL_an = proto_perl->Ian; | |
8932 | PL_cop_seqmax = proto_perl->Icop_seqmax; | |
8933 | PL_op_seqmax = proto_perl->Iop_seqmax; | |
8934 | PL_evalseq = proto_perl->Ievalseq; | |
8935 | PL_origenviron = proto_perl->Iorigenviron; /* XXX not quite right */ | |
8936 | PL_origalen = proto_perl->Iorigalen; | |
8937 | PL_pidstatus = newHV(); /* XXX flag for cloning? */ | |
8938 | PL_osname = SAVEPV(proto_perl->Iosname); | |
8939 | PL_sh_path = SAVEPV(proto_perl->Ish_path); | |
8940 | PL_sighandlerp = proto_perl->Isighandlerp; | |
8941 | ||
8942 | ||
8943 | PL_runops = proto_perl->Irunops; | |
8944 | ||
8945 | Copy(proto_perl->Itokenbuf, PL_tokenbuf, 256, char); | |
8946 | ||
8947 | #ifdef CSH | |
8948 | PL_cshlen = proto_perl->Icshlen; | |
8949 | PL_cshname = SAVEPVN(proto_perl->Icshname, PL_cshlen); | |
8950 | #endif | |
8951 | ||
8952 | PL_lex_state = proto_perl->Ilex_state; | |
8953 | PL_lex_defer = proto_perl->Ilex_defer; | |
8954 | PL_lex_expect = proto_perl->Ilex_expect; | |
8955 | PL_lex_formbrack = proto_perl->Ilex_formbrack; | |
8956 | PL_lex_dojoin = proto_perl->Ilex_dojoin; | |
8957 | PL_lex_starts = proto_perl->Ilex_starts; | |
8958 | PL_lex_stuff = sv_dup_inc(proto_perl->Ilex_stuff); | |
8959 | PL_lex_repl = sv_dup_inc(proto_perl->Ilex_repl); | |
8960 | PL_lex_op = proto_perl->Ilex_op; | |
8961 | PL_lex_inpat = proto_perl->Ilex_inpat; | |
8962 | PL_lex_inwhat = proto_perl->Ilex_inwhat; | |
8963 | PL_lex_brackets = proto_perl->Ilex_brackets; | |
8964 | i = (PL_lex_brackets < 120 ? 120 : PL_lex_brackets); | |
8965 | PL_lex_brackstack = SAVEPVN(proto_perl->Ilex_brackstack,i); | |
8966 | PL_lex_casemods = proto_perl->Ilex_casemods; | |
8967 | i = (PL_lex_casemods < 12 ? 12 : PL_lex_casemods); | |
8968 | PL_lex_casestack = SAVEPVN(proto_perl->Ilex_casestack,i); | |
8969 | ||
8970 | Copy(proto_perl->Inextval, PL_nextval, 5, YYSTYPE); | |
8971 | Copy(proto_perl->Inexttype, PL_nexttype, 5, I32); | |
8972 | PL_nexttoke = proto_perl->Inexttoke; | |
8973 | ||
8974 | PL_linestr = sv_dup_inc(proto_perl->Ilinestr); | |
8975 | i = proto_perl->Ibufptr - SvPVX(proto_perl->Ilinestr); | |
8976 | PL_bufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
8977 | i = proto_perl->Ioldbufptr - SvPVX(proto_perl->Ilinestr); | |
8978 | PL_oldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
8979 | i = proto_perl->Ioldoldbufptr - SvPVX(proto_perl->Ilinestr); | |
8980 | PL_oldoldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
8981 | PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr); | |
8982 | i = proto_perl->Ilinestart - SvPVX(proto_perl->Ilinestr); | |
8983 | PL_linestart = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
8984 | PL_pending_ident = proto_perl->Ipending_ident; | |
8985 | PL_sublex_info = proto_perl->Isublex_info; /* XXX not quite right */ | |
8986 | ||
8987 | PL_expect = proto_perl->Iexpect; | |
8988 | ||
8989 | PL_multi_start = proto_perl->Imulti_start; | |
8990 | PL_multi_end = proto_perl->Imulti_end; | |
8991 | PL_multi_open = proto_perl->Imulti_open; | |
8992 | PL_multi_close = proto_perl->Imulti_close; | |
8993 | ||
8994 | PL_error_count = proto_perl->Ierror_count; | |
8995 | PL_subline = proto_perl->Isubline; | |
8996 | PL_subname = sv_dup_inc(proto_perl->Isubname); | |
8997 | ||
8998 | PL_min_intro_pending = proto_perl->Imin_intro_pending; | |
8999 | PL_max_intro_pending = proto_perl->Imax_intro_pending; | |
9000 | PL_padix = proto_perl->Ipadix; | |
9001 | PL_padix_floor = proto_perl->Ipadix_floor; | |
9002 | PL_pad_reset_pending = proto_perl->Ipad_reset_pending; | |
9003 | ||
9004 | i = proto_perl->Ilast_uni - SvPVX(proto_perl->Ilinestr); | |
9005 | PL_last_uni = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
9006 | i = proto_perl->Ilast_lop - SvPVX(proto_perl->Ilinestr); | |
9007 | PL_last_lop = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
9008 | PL_last_lop_op = proto_perl->Ilast_lop_op; | |
9009 | PL_in_my = proto_perl->Iin_my; | |
9010 | PL_in_my_stash = hv_dup(proto_perl->Iin_my_stash); | |
9011 | #ifdef FCRYPT | |
9012 | PL_cryptseen = proto_perl->Icryptseen; | |
9013 | #endif | |
9014 | ||
9015 | PL_hints = proto_perl->Ihints; | |
9016 | ||
9017 | PL_amagic_generation = proto_perl->Iamagic_generation; | |
9018 | ||
9019 | #ifdef USE_LOCALE_COLLATE | |
9020 | PL_collation_ix = proto_perl->Icollation_ix; | |
9021 | PL_collation_name = SAVEPV(proto_perl->Icollation_name); | |
9022 | PL_collation_standard = proto_perl->Icollation_standard; | |
9023 | PL_collxfrm_base = proto_perl->Icollxfrm_base; | |
9024 | PL_collxfrm_mult = proto_perl->Icollxfrm_mult; | |
9025 | #endif /* USE_LOCALE_COLLATE */ | |
9026 | ||
9027 | #ifdef USE_LOCALE_NUMERIC | |
9028 | PL_numeric_name = SAVEPV(proto_perl->Inumeric_name); | |
9029 | PL_numeric_standard = proto_perl->Inumeric_standard; | |
9030 | PL_numeric_local = proto_perl->Inumeric_local; | |
ac634a9a | 9031 | PL_numeric_radix = sv_dup_inc(proto_perl->Inumeric_radix); |
1d7c1841 GS |
9032 | #endif /* !USE_LOCALE_NUMERIC */ |
9033 | ||
9034 | /* utf8 character classes */ | |
9035 | PL_utf8_alnum = sv_dup_inc(proto_perl->Iutf8_alnum); | |
9036 | PL_utf8_alnumc = sv_dup_inc(proto_perl->Iutf8_alnumc); | |
9037 | PL_utf8_ascii = sv_dup_inc(proto_perl->Iutf8_ascii); | |
9038 | PL_utf8_alpha = sv_dup_inc(proto_perl->Iutf8_alpha); | |
9039 | PL_utf8_space = sv_dup_inc(proto_perl->Iutf8_space); | |
9040 | PL_utf8_cntrl = sv_dup_inc(proto_perl->Iutf8_cntrl); | |
9041 | PL_utf8_graph = sv_dup_inc(proto_perl->Iutf8_graph); | |
9042 | PL_utf8_digit = sv_dup_inc(proto_perl->Iutf8_digit); | |
9043 | PL_utf8_upper = sv_dup_inc(proto_perl->Iutf8_upper); | |
9044 | PL_utf8_lower = sv_dup_inc(proto_perl->Iutf8_lower); | |
9045 | PL_utf8_print = sv_dup_inc(proto_perl->Iutf8_print); | |
9046 | PL_utf8_punct = sv_dup_inc(proto_perl->Iutf8_punct); | |
9047 | PL_utf8_xdigit = sv_dup_inc(proto_perl->Iutf8_xdigit); | |
9048 | PL_utf8_mark = sv_dup_inc(proto_perl->Iutf8_mark); | |
9049 | PL_utf8_toupper = sv_dup_inc(proto_perl->Iutf8_toupper); | |
9050 | PL_utf8_totitle = sv_dup_inc(proto_perl->Iutf8_totitle); | |
9051 | PL_utf8_tolower = sv_dup_inc(proto_perl->Iutf8_tolower); | |
9052 | ||
9053 | /* swatch cache */ | |
9054 | PL_last_swash_hv = Nullhv; /* reinits on demand */ | |
9055 | PL_last_swash_klen = 0; | |
9056 | PL_last_swash_key[0]= '\0'; | |
9057 | PL_last_swash_tmps = (U8*)NULL; | |
9058 | PL_last_swash_slen = 0; | |
9059 | ||
9060 | /* perly.c globals */ | |
9061 | PL_yydebug = proto_perl->Iyydebug; | |
9062 | PL_yynerrs = proto_perl->Iyynerrs; | |
9063 | PL_yyerrflag = proto_perl->Iyyerrflag; | |
9064 | PL_yychar = proto_perl->Iyychar; | |
9065 | PL_yyval = proto_perl->Iyyval; | |
9066 | PL_yylval = proto_perl->Iyylval; | |
9067 | ||
9068 | PL_glob_index = proto_perl->Iglob_index; | |
9069 | PL_srand_called = proto_perl->Isrand_called; | |
9070 | PL_uudmap['M'] = 0; /* reinits on demand */ | |
9071 | PL_bitcount = Nullch; /* reinits on demand */ | |
9072 | ||
66fe0623 NIS |
9073 | if (proto_perl->Ipsig_pend) { |
9074 | Newz(0, PL_psig_pend, SIG_SIZE, int); | |
9dd79c3f | 9075 | } |
66fe0623 NIS |
9076 | else { |
9077 | PL_psig_pend = (int*)NULL; | |
9078 | } | |
9079 | ||
1d7c1841 | 9080 | if (proto_perl->Ipsig_ptr) { |
76d3c696 JH |
9081 | Newz(0, PL_psig_ptr, SIG_SIZE, SV*); |
9082 | Newz(0, PL_psig_name, SIG_SIZE, SV*); | |
76d3c696 JH |
9083 | for (i = 1; i < SIG_SIZE; i++) { |
9084 | PL_psig_ptr[i] = sv_dup_inc(proto_perl->Ipsig_ptr[i]); | |
1d7c1841 GS |
9085 | PL_psig_name[i] = sv_dup_inc(proto_perl->Ipsig_name[i]); |
9086 | } | |
9087 | } | |
9088 | else { | |
9089 | PL_psig_ptr = (SV**)NULL; | |
9090 | PL_psig_name = (SV**)NULL; | |
9091 | } | |
9092 | ||
9093 | /* thrdvar.h stuff */ | |
9094 | ||
a0739874 | 9095 | if (flags & CLONEf_COPY_STACKS) { |
1d7c1841 GS |
9096 | /* next allocation will be PL_tmps_stack[PL_tmps_ix+1] */ |
9097 | PL_tmps_ix = proto_perl->Ttmps_ix; | |
9098 | PL_tmps_max = proto_perl->Ttmps_max; | |
9099 | PL_tmps_floor = proto_perl->Ttmps_floor; | |
9100 | Newz(50, PL_tmps_stack, PL_tmps_max, SV*); | |
9101 | i = 0; | |
9102 | while (i <= PL_tmps_ix) { | |
9103 | PL_tmps_stack[i] = sv_dup_inc(proto_perl->Ttmps_stack[i]); | |
9104 | ++i; | |
9105 | } | |
9106 | ||
9107 | /* next PUSHMARK() sets *(PL_markstack_ptr+1) */ | |
9108 | i = proto_perl->Tmarkstack_max - proto_perl->Tmarkstack; | |
9109 | Newz(54, PL_markstack, i, I32); | |
9110 | PL_markstack_max = PL_markstack + (proto_perl->Tmarkstack_max | |
9111 | - proto_perl->Tmarkstack); | |
9112 | PL_markstack_ptr = PL_markstack + (proto_perl->Tmarkstack_ptr | |
9113 | - proto_perl->Tmarkstack); | |
9114 | Copy(proto_perl->Tmarkstack, PL_markstack, | |
9115 | PL_markstack_ptr - PL_markstack + 1, I32); | |
9116 | ||
9117 | /* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix] | |
9118 | * NOTE: unlike the others! */ | |
9119 | PL_scopestack_ix = proto_perl->Tscopestack_ix; | |
9120 | PL_scopestack_max = proto_perl->Tscopestack_max; | |
9121 | Newz(54, PL_scopestack, PL_scopestack_max, I32); | |
9122 | Copy(proto_perl->Tscopestack, PL_scopestack, PL_scopestack_ix, I32); | |
9123 | ||
9124 | /* next push_return() sets PL_retstack[PL_retstack_ix] | |
9125 | * NOTE: unlike the others! */ | |
9126 | PL_retstack_ix = proto_perl->Tretstack_ix; | |
9127 | PL_retstack_max = proto_perl->Tretstack_max; | |
9128 | Newz(54, PL_retstack, PL_retstack_max, OP*); | |
9129 | Copy(proto_perl->Tretstack, PL_retstack, PL_retstack_ix, I32); | |
9130 | ||
9131 | /* NOTE: si_dup() looks at PL_markstack */ | |
9132 | PL_curstackinfo = si_dup(proto_perl->Tcurstackinfo); | |
9133 | ||
9134 | /* PL_curstack = PL_curstackinfo->si_stack; */ | |
9135 | PL_curstack = av_dup(proto_perl->Tcurstack); | |
9136 | PL_mainstack = av_dup(proto_perl->Tmainstack); | |
9137 | ||
9138 | /* next PUSHs() etc. set *(PL_stack_sp+1) */ | |
9139 | PL_stack_base = AvARRAY(PL_curstack); | |
9140 | PL_stack_sp = PL_stack_base + (proto_perl->Tstack_sp | |
9141 | - proto_perl->Tstack_base); | |
9142 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); | |
9143 | ||
9144 | /* next SSPUSHFOO() sets PL_savestack[PL_savestack_ix] | |
9145 | * NOTE: unlike the others! */ | |
9146 | PL_savestack_ix = proto_perl->Tsavestack_ix; | |
9147 | PL_savestack_max = proto_perl->Tsavestack_max; | |
9148 | /*Newz(54, PL_savestack, PL_savestack_max, ANY);*/ | |
9149 | PL_savestack = ss_dup(proto_perl); | |
9150 | } | |
9151 | else { | |
9152 | init_stacks(); | |
985e7056 | 9153 | ENTER; /* perl_destruct() wants to LEAVE; */ |
1d7c1841 GS |
9154 | } |
9155 | ||
9156 | PL_start_env = proto_perl->Tstart_env; /* XXXXXX */ | |
9157 | PL_top_env = &PL_start_env; | |
9158 | ||
9159 | PL_op = proto_perl->Top; | |
9160 | ||
9161 | PL_Sv = Nullsv; | |
9162 | PL_Xpv = (XPV*)NULL; | |
9163 | PL_na = proto_perl->Tna; | |
9164 | ||
9165 | PL_statbuf = proto_perl->Tstatbuf; | |
9166 | PL_statcache = proto_perl->Tstatcache; | |
9167 | PL_statgv = gv_dup(proto_perl->Tstatgv); | |
9168 | PL_statname = sv_dup_inc(proto_perl->Tstatname); | |
9169 | #ifdef HAS_TIMES | |
9170 | PL_timesbuf = proto_perl->Ttimesbuf; | |
9171 | #endif | |
9172 | ||
9173 | PL_tainted = proto_perl->Ttainted; | |
9174 | PL_curpm = proto_perl->Tcurpm; /* XXX No PMOP ref count */ | |
9175 | PL_nrs = sv_dup_inc(proto_perl->Tnrs); | |
9176 | PL_rs = sv_dup_inc(proto_perl->Trs); | |
9177 | PL_last_in_gv = gv_dup(proto_perl->Tlast_in_gv); | |
7889fe52 | 9178 | PL_ofs_sv = sv_dup_inc(proto_perl->Tofs_sv); |
1d7c1841 GS |
9179 | PL_defoutgv = gv_dup_inc(proto_perl->Tdefoutgv); |
9180 | PL_chopset = proto_perl->Tchopset; /* XXX never deallocated */ | |
9181 | PL_toptarget = sv_dup_inc(proto_perl->Ttoptarget); | |
9182 | PL_bodytarget = sv_dup_inc(proto_perl->Tbodytarget); | |
9183 | PL_formtarget = sv_dup(proto_perl->Tformtarget); | |
9184 | ||
9185 | PL_restartop = proto_perl->Trestartop; | |
9186 | PL_in_eval = proto_perl->Tin_eval; | |
9187 | PL_delaymagic = proto_perl->Tdelaymagic; | |
9188 | PL_dirty = proto_perl->Tdirty; | |
9189 | PL_localizing = proto_perl->Tlocalizing; | |
9190 | ||
14dd3ad8 | 9191 | #ifdef PERL_FLEXIBLE_EXCEPTIONS |
1d7c1841 | 9192 | PL_protect = proto_perl->Tprotect; |
14dd3ad8 | 9193 | #endif |
1d7c1841 GS |
9194 | PL_errors = sv_dup_inc(proto_perl->Terrors); |
9195 | PL_av_fetch_sv = Nullsv; | |
9196 | PL_hv_fetch_sv = Nullsv; | |
9197 | Zero(&PL_hv_fetch_ent_mh, 1, HE); /* XXX */ | |
9198 | PL_modcount = proto_perl->Tmodcount; | |
9199 | PL_lastgotoprobe = Nullop; | |
9200 | PL_dumpindent = proto_perl->Tdumpindent; | |
9201 | ||
9202 | PL_sortcop = (OP*)any_dup(proto_perl->Tsortcop, proto_perl); | |
9203 | PL_sortstash = hv_dup(proto_perl->Tsortstash); | |
9204 | PL_firstgv = gv_dup(proto_perl->Tfirstgv); | |
9205 | PL_secondgv = gv_dup(proto_perl->Tsecondgv); | |
9206 | PL_sortcxix = proto_perl->Tsortcxix; | |
9207 | PL_efloatbuf = Nullch; /* reinits on demand */ | |
9208 | PL_efloatsize = 0; /* reinits on demand */ | |
9209 | ||
9210 | /* regex stuff */ | |
9211 | ||
9212 | PL_screamfirst = NULL; | |
9213 | PL_screamnext = NULL; | |
9214 | PL_maxscream = -1; /* reinits on demand */ | |
9215 | PL_lastscream = Nullsv; | |
9216 | ||
9217 | PL_watchaddr = NULL; | |
9218 | PL_watchok = Nullch; | |
9219 | ||
9220 | PL_regdummy = proto_perl->Tregdummy; | |
9221 | PL_regcomp_parse = Nullch; | |
9222 | PL_regxend = Nullch; | |
9223 | PL_regcode = (regnode*)NULL; | |
9224 | PL_regnaughty = 0; | |
9225 | PL_regsawback = 0; | |
9226 | PL_regprecomp = Nullch; | |
9227 | PL_regnpar = 0; | |
9228 | PL_regsize = 0; | |
9229 | PL_regflags = 0; | |
9230 | PL_regseen = 0; | |
9231 | PL_seen_zerolen = 0; | |
9232 | PL_seen_evals = 0; | |
9233 | PL_regcomp_rx = (regexp*)NULL; | |
9234 | PL_extralen = 0; | |
9235 | PL_colorset = 0; /* reinits PL_colors[] */ | |
9236 | /*PL_colors[6] = {0,0,0,0,0,0};*/ | |
9237 | PL_reg_whilem_seen = 0; | |
9238 | PL_reginput = Nullch; | |
9239 | PL_regbol = Nullch; | |
9240 | PL_regeol = Nullch; | |
9241 | PL_regstartp = (I32*)NULL; | |
9242 | PL_regendp = (I32*)NULL; | |
9243 | PL_reglastparen = (U32*)NULL; | |
9244 | PL_regtill = Nullch; | |
9245 | PL_regprev = '\n'; | |
9246 | PL_reg_start_tmp = (char**)NULL; | |
9247 | PL_reg_start_tmpl = 0; | |
9248 | PL_regdata = (struct reg_data*)NULL; | |
9249 | PL_bostr = Nullch; | |
9250 | PL_reg_flags = 0; | |
9251 | PL_reg_eval_set = 0; | |
9252 | PL_regnarrate = 0; | |
9253 | PL_regprogram = (regnode*)NULL; | |
9254 | PL_regindent = 0; | |
9255 | PL_regcc = (CURCUR*)NULL; | |
9256 | PL_reg_call_cc = (struct re_cc_state*)NULL; | |
9257 | PL_reg_re = (regexp*)NULL; | |
9258 | PL_reg_ganch = Nullch; | |
9259 | PL_reg_sv = Nullsv; | |
9260 | PL_reg_magic = (MAGIC*)NULL; | |
9261 | PL_reg_oldpos = 0; | |
9262 | PL_reg_oldcurpm = (PMOP*)NULL; | |
9263 | PL_reg_curpm = (PMOP*)NULL; | |
9264 | PL_reg_oldsaved = Nullch; | |
9265 | PL_reg_oldsavedlen = 0; | |
9266 | PL_reg_maxiter = 0; | |
9267 | PL_reg_leftiter = 0; | |
9268 | PL_reg_poscache = Nullch; | |
9269 | PL_reg_poscache_size= 0; | |
9270 | ||
9271 | /* RE engine - function pointers */ | |
9272 | PL_regcompp = proto_perl->Tregcompp; | |
9273 | PL_regexecp = proto_perl->Tregexecp; | |
9274 | PL_regint_start = proto_perl->Tregint_start; | |
9275 | PL_regint_string = proto_perl->Tregint_string; | |
9276 | PL_regfree = proto_perl->Tregfree; | |
9277 | ||
9278 | PL_reginterp_cnt = 0; | |
9279 | PL_reg_starttry = 0; | |
9280 | ||
a0739874 DM |
9281 | if (!(flags & CLONEf_KEEP_PTR_TABLE)) { |
9282 | ptr_table_free(PL_ptr_table); | |
9283 | PL_ptr_table = NULL; | |
9284 | } | |
9285 | ||
1d7c1841 GS |
9286 | #ifdef PERL_OBJECT |
9287 | return (PerlInterpreter*)pPerl; | |
9288 | #else | |
9289 | return my_perl; | |
9290 | #endif | |
9291 | } | |
9292 | ||
9293 | #else /* !USE_ITHREADS */ | |
51371543 GS |
9294 | |
9295 | #ifdef PERL_OBJECT | |
51371543 GS |
9296 | #include "XSUB.h" |
9297 | #endif | |
9298 | ||
1d7c1841 GS |
9299 | #endif /* USE_ITHREADS */ |
9300 | ||
51371543 GS |
9301 | static void |
9302 | do_report_used(pTHXo_ SV *sv) | |
9303 | { | |
9304 | if (SvTYPE(sv) != SVTYPEMASK) { | |
bf49b057 | 9305 | PerlIO_printf(Perl_debug_log, "****\n"); |
51371543 GS |
9306 | sv_dump(sv); |
9307 | } | |
9308 | } | |
9309 | ||
9310 | static void | |
9311 | do_clean_objs(pTHXo_ SV *sv) | |
9312 | { | |
9313 | SV* rv; | |
9314 | ||
9315 | if (SvROK(sv) && SvOBJECT(rv = SvRV(sv))) { | |
9316 | DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(sv));) | |
8b6e653b HS |
9317 | if (SvWEAKREF(sv)) { |
9318 | sv_del_backref(sv); | |
9319 | SvWEAKREF_off(sv); | |
9320 | SvRV(sv) = 0; | |
9321 | } else { | |
9322 | SvROK_off(sv); | |
9323 | SvRV(sv) = 0; | |
9324 | SvREFCNT_dec(rv); | |
9325 | } | |
51371543 GS |
9326 | } |
9327 | ||
9328 | /* XXX Might want to check arrays, etc. */ | |
9329 | } | |
9330 | ||
9331 | #ifndef DISABLE_DESTRUCTOR_KLUDGE | |
9332 | static void | |
9333 | do_clean_named_objs(pTHXo_ SV *sv) | |
9334 | { | |
f472eb5c | 9335 | if (SvTYPE(sv) == SVt_PVGV && GvGP(sv)) { |
51371543 | 9336 | if ( SvOBJECT(GvSV(sv)) || |
155aba94 GS |
9337 | (GvAV(sv) && SvOBJECT(GvAV(sv))) || |
9338 | (GvHV(sv) && SvOBJECT(GvHV(sv))) || | |
9339 | (GvIO(sv) && SvOBJECT(GvIO(sv))) || | |
9340 | (GvCV(sv) && SvOBJECT(GvCV(sv))) ) | |
51371543 GS |
9341 | { |
9342 | DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning named glob object:\n "), sv_dump(sv));) | |
9343 | SvREFCNT_dec(sv); | |
9344 | } | |
9345 | } | |
9346 | } | |
9347 | #endif | |
9348 | ||
9349 | static void | |
9350 | do_clean_all(pTHXo_ SV *sv) | |
9351 | { | |
1d7c1841 | 9352 | DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) );) |
51371543 GS |
9353 | SvFLAGS(sv) |= SVf_BREAK; |
9354 | SvREFCNT_dec(sv); | |
9355 | } | |
8af02333 | 9356 |