Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | /* sv.c |
79072805 | 2 | * |
bc89e66f | 3 | * Copyright (c) 1991-2001, Larry Wall |
79072805 LW |
4 | * |
5 | * You may distribute under the terms of either the GNU General Public | |
6 | * License or the Artistic License, as specified in the README file. | |
7 | * | |
a0d0e21e LW |
8 | */ |
9 | ||
10 | /* | |
11 | * "I wonder what the Entish is for 'yes' and 'no'," he thought. | |
79072805 LW |
12 | */ |
13 | ||
14 | #include "EXTERN.h" | |
864dbfa3 | 15 | #define PERL_IN_SV_C |
79072805 | 16 | #include "perl.h" |
79072805 | 17 | |
51371543 | 18 | #define FCALL *f |
6fc92669 | 19 | #define SV_CHECK_THINKFIRST(sv) if (SvTHINKFIRST(sv)) sv_force_normal(sv) |
2c5424a7 | 20 | |
51371543 GS |
21 | static void do_report_used(pTHXo_ SV *sv); |
22 | static void do_clean_objs(pTHXo_ SV *sv); | |
23 | #ifndef DISABLE_DESTRUCTOR_KLUDGE | |
24 | static void do_clean_named_objs(pTHXo_ SV *sv); | |
25 | #endif | |
26 | static void do_clean_all(pTHXo_ SV *sv); | |
27 | ||
4561caa4 CS |
28 | /* |
29 | * "A time to plant, and a time to uproot what was planted..." | |
30 | */ | |
31 | ||
053fc874 GS |
32 | #define plant_SV(p) \ |
33 | STMT_START { \ | |
34 | SvANY(p) = (void *)PL_sv_root; \ | |
35 | SvFLAGS(p) = SVTYPEMASK; \ | |
36 | PL_sv_root = (p); \ | |
37 | --PL_sv_count; \ | |
38 | } STMT_END | |
a0d0e21e | 39 | |
fba3b22e | 40 | /* sv_mutex must be held while calling uproot_SV() */ |
053fc874 GS |
41 | #define uproot_SV(p) \ |
42 | STMT_START { \ | |
43 | (p) = PL_sv_root; \ | |
44 | PL_sv_root = (SV*)SvANY(p); \ | |
45 | ++PL_sv_count; \ | |
46 | } STMT_END | |
47 | ||
48 | #define new_SV(p) \ | |
49 | STMT_START { \ | |
50 | LOCK_SV_MUTEX; \ | |
51 | if (PL_sv_root) \ | |
52 | uproot_SV(p); \ | |
53 | else \ | |
54 | (p) = more_sv(); \ | |
55 | UNLOCK_SV_MUTEX; \ | |
56 | SvANY(p) = 0; \ | |
57 | SvREFCNT(p) = 1; \ | |
58 | SvFLAGS(p) = 0; \ | |
59 | } STMT_END | |
463ee0b2 | 60 | |
a0d0e21e | 61 | #ifdef DEBUGGING |
4561caa4 | 62 | |
053fc874 GS |
63 | #define del_SV(p) \ |
64 | STMT_START { \ | |
65 | LOCK_SV_MUTEX; \ | |
aea4f609 | 66 | if (DEBUG_D_TEST) \ |
053fc874 GS |
67 | del_sv(p); \ |
68 | else \ | |
69 | plant_SV(p); \ | |
70 | UNLOCK_SV_MUTEX; \ | |
71 | } STMT_END | |
a0d0e21e | 72 | |
76e3520e | 73 | STATIC void |
cea2e8a9 | 74 | S_del_sv(pTHX_ SV *p) |
463ee0b2 | 75 | { |
aea4f609 | 76 | if (DEBUG_D_TEST) { |
4633a7c4 | 77 | SV* sva; |
a0d0e21e LW |
78 | SV* sv; |
79 | SV* svend; | |
80 | int ok = 0; | |
3280af22 | 81 | for (sva = PL_sv_arenaroot; sva; sva = (SV *) SvANY(sva)) { |
4633a7c4 LW |
82 | sv = sva + 1; |
83 | svend = &sva[SvREFCNT(sva)]; | |
a0d0e21e LW |
84 | if (p >= sv && p < svend) |
85 | ok = 1; | |
86 | } | |
87 | if (!ok) { | |
0453d815 PM |
88 | if (ckWARN_d(WARN_INTERNAL)) |
89 | Perl_warner(aTHX_ WARN_INTERNAL, | |
1d7c1841 GS |
90 | "Attempt to free non-arena SV: 0x%"UVxf, |
91 | PTR2UV(p)); | |
a0d0e21e LW |
92 | return; |
93 | } | |
94 | } | |
4561caa4 | 95 | plant_SV(p); |
463ee0b2 | 96 | } |
a0d0e21e | 97 | |
4561caa4 CS |
98 | #else /* ! DEBUGGING */ |
99 | ||
100 | #define del_SV(p) plant_SV(p) | |
101 | ||
102 | #endif /* DEBUGGING */ | |
463ee0b2 | 103 | |
4633a7c4 | 104 | void |
864dbfa3 | 105 | Perl_sv_add_arena(pTHX_ char *ptr, U32 size, U32 flags) |
463ee0b2 | 106 | { |
4633a7c4 | 107 | SV* sva = (SV*)ptr; |
463ee0b2 LW |
108 | register SV* sv; |
109 | register SV* svend; | |
14dd3ad8 | 110 | Zero(ptr, size, char); |
4633a7c4 LW |
111 | |
112 | /* The first SV in an arena isn't an SV. */ | |
3280af22 | 113 | SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */ |
4633a7c4 LW |
114 | SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */ |
115 | SvFLAGS(sva) = flags; /* FAKE if not to be freed */ | |
116 | ||
3280af22 NIS |
117 | PL_sv_arenaroot = sva; |
118 | PL_sv_root = sva + 1; | |
4633a7c4 LW |
119 | |
120 | svend = &sva[SvREFCNT(sva) - 1]; | |
121 | sv = sva + 1; | |
463ee0b2 | 122 | while (sv < svend) { |
a0d0e21e | 123 | SvANY(sv) = (void *)(SV*)(sv + 1); |
8990e307 | 124 | SvFLAGS(sv) = SVTYPEMASK; |
463ee0b2 LW |
125 | sv++; |
126 | } | |
127 | SvANY(sv) = 0; | |
4633a7c4 LW |
128 | SvFLAGS(sv) = SVTYPEMASK; |
129 | } | |
130 | ||
fba3b22e | 131 | /* sv_mutex must be held while calling more_sv() */ |
76e3520e | 132 | STATIC SV* |
cea2e8a9 | 133 | S_more_sv(pTHX) |
4633a7c4 | 134 | { |
4561caa4 CS |
135 | register SV* sv; |
136 | ||
3280af22 NIS |
137 | if (PL_nice_chunk) { |
138 | sv_add_arena(PL_nice_chunk, PL_nice_chunk_size, 0); | |
139 | PL_nice_chunk = Nullch; | |
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 | { | |
0875d2fe NIS |
2887 | sv_utf8_downgrade(sv,0); |
2888 | return SvPV(sv,*lp); | |
7340a771 GS |
2889 | } |
2890 | ||
2891 | char * | |
2892 | Perl_sv_2pvutf8_nolen(pTHX_ register SV *sv) | |
2893 | { | |
560a288e GS |
2894 | STRLEN n_a; |
2895 | return sv_2pvutf8(sv, &n_a); | |
7340a771 GS |
2896 | } |
2897 | ||
2898 | char * | |
2899 | Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp) | |
2900 | { | |
560a288e | 2901 | sv_utf8_upgrade(sv); |
7d59b7e4 | 2902 | return SvPV(sv,*lp); |
7340a771 | 2903 | } |
1c846c1f | 2904 | |
463ee0b2 LW |
2905 | /* This function is only called on magical items */ |
2906 | bool | |
864dbfa3 | 2907 | Perl_sv_2bool(pTHX_ register SV *sv) |
463ee0b2 | 2908 | { |
8990e307 | 2909 | if (SvGMAGICAL(sv)) |
463ee0b2 LW |
2910 | mg_get(sv); |
2911 | ||
a0d0e21e LW |
2912 | if (!SvOK(sv)) |
2913 | return 0; | |
2914 | if (SvROK(sv)) { | |
a0d0e21e | 2915 | SV* tmpsv; |
1554e226 DC |
2916 | if (SvAMAGIC(sv) && (tmpsv=AMG_CALLun(sv,bool_)) && |
2917 | (SvRV(tmpsv) != SvRV(sv))) | |
9e7bc3e8 | 2918 | return SvTRUE(tmpsv); |
a0d0e21e LW |
2919 | return SvRV(sv) != 0; |
2920 | } | |
463ee0b2 | 2921 | if (SvPOKp(sv)) { |
11343788 MB |
2922 | register XPV* Xpvtmp; |
2923 | if ((Xpvtmp = (XPV*)SvANY(sv)) && | |
2924 | (*Xpvtmp->xpv_pv > '0' || | |
2925 | Xpvtmp->xpv_cur > 1 || | |
2926 | (Xpvtmp->xpv_cur && *Xpvtmp->xpv_pv != '0'))) | |
463ee0b2 LW |
2927 | return 1; |
2928 | else | |
2929 | return 0; | |
2930 | } | |
2931 | else { | |
2932 | if (SvIOKp(sv)) | |
2933 | return SvIVX(sv) != 0; | |
2934 | else { | |
2935 | if (SvNOKp(sv)) | |
2936 | return SvNVX(sv) != 0.0; | |
2937 | else | |
2938 | return FALSE; | |
2939 | } | |
2940 | } | |
79072805 LW |
2941 | } |
2942 | ||
c461cf8f JH |
2943 | /* |
2944 | =for apidoc sv_utf8_upgrade | |
2945 | ||
2946 | Convert the PV of an SV to its UTF8-encoded form. | |
4411f3b6 NIS |
2947 | Forces the SV to string form it it is not already. |
2948 | Always sets the SvUTF8 flag to avoid future validity checks even | |
2949 | if all the bytes have hibit clear. | |
c461cf8f JH |
2950 | |
2951 | =cut | |
2952 | */ | |
2953 | ||
4411f3b6 | 2954 | STRLEN |
560a288e GS |
2955 | Perl_sv_utf8_upgrade(pTHX_ register SV *sv) |
2956 | { | |
511c2ff0 NIS |
2957 | char *s, *t, *e; |
2958 | int hibit = 0; | |
560a288e | 2959 | |
4411f3b6 NIS |
2960 | if (!sv) |
2961 | return 0; | |
2962 | ||
2963 | if (!SvPOK(sv)) | |
2964 | (void) SvPV_nolen(sv); | |
2965 | ||
2966 | if (SvUTF8(sv)) | |
2967 | return SvCUR(sv); | |
560a288e | 2968 | |
40826f67 JH |
2969 | /* This function could be much more efficient if we had a FLAG in SVs |
2970 | * to signal if there are any hibit chars in the PV. | |
511c2ff0 | 2971 | * Given that there isn't make loop fast as possible |
560a288e | 2972 | */ |
511c2ff0 NIS |
2973 | s = SvPVX(sv); |
2974 | e = SvEND(sv); | |
2975 | t = s; | |
2976 | while (t < e) { | |
fd400ab9 | 2977 | if ((hibit = UTF8_IS_CONTINUED(*t++))) |
8a818333 | 2978 | break; |
8a818333 | 2979 | } |
560a288e | 2980 | |
40826f67 | 2981 | if (hibit) { |
8a818333 | 2982 | STRLEN len; |
652088fc | 2983 | |
8a818333 | 2984 | if (SvREADONLY(sv) && SvFAKE(sv)) { |
8a818333 NIS |
2985 | sv_force_normal(sv); |
2986 | s = SvPVX(sv); | |
2987 | } | |
2988 | len = SvCUR(sv) + 1; /* Plus the \0 */ | |
00df9076 | 2989 | SvPVX(sv) = (char*)bytes_to_utf8((U8*)s, &len); |
841d7a39 | 2990 | SvCUR(sv) = len - 1; |
511c2ff0 NIS |
2991 | if (SvLEN(sv) != 0) |
2992 | Safefree(s); /* No longer using what was there before. */ | |
841d7a39 | 2993 | SvLEN(sv) = len; /* No longer know the real size. */ |
560a288e | 2994 | } |
4411f3b6 NIS |
2995 | /* Mark as UTF-8 even if no hibit - saves scanning loop */ |
2996 | SvUTF8_on(sv); | |
2997 | return SvCUR(sv); | |
560a288e GS |
2998 | } |
2999 | ||
c461cf8f JH |
3000 | /* |
3001 | =for apidoc sv_utf8_downgrade | |
3002 | ||
3003 | Attempt to convert the PV of an SV from UTF8-encoded to byte encoding. | |
3004 | This may not be possible if the PV contains non-byte encoding characters; | |
3005 | if this is the case, either returns false or, if C<fail_ok> is not | |
3006 | true, croaks. | |
3007 | ||
3008 | =cut | |
3009 | */ | |
3010 | ||
560a288e GS |
3011 | bool |
3012 | Perl_sv_utf8_downgrade(pTHX_ register SV* sv, bool fail_ok) | |
3013 | { | |
3014 | if (SvPOK(sv) && SvUTF8(sv)) { | |
fa301091 | 3015 | if (SvCUR(sv)) { |
652088fc JH |
3016 | char *s; |
3017 | STRLEN len; | |
fa301091 | 3018 | |
652088fc JH |
3019 | if (SvREADONLY(sv) && SvFAKE(sv)) |
3020 | sv_force_normal(sv); | |
3021 | s = SvPV(sv, len); | |
3022 | if (!utf8_to_bytes((U8*)s, &len)) { | |
fa301091 JH |
3023 | if (fail_ok) |
3024 | return FALSE; | |
3025 | else { | |
3026 | if (PL_op) | |
3027 | Perl_croak(aTHX_ "Wide character in %s", | |
3028 | PL_op_desc[PL_op->op_type]); | |
3029 | else | |
3030 | Perl_croak(aTHX_ "Wide character"); | |
3031 | } | |
4b3603a4 | 3032 | } |
fa301091 | 3033 | SvCUR(sv) = len; |
67e989fb | 3034 | } |
9f9ab905 | 3035 | SvUTF8_off(sv); |
560a288e | 3036 | } |
fa301091 | 3037 | |
560a288e GS |
3038 | return TRUE; |
3039 | } | |
3040 | ||
c461cf8f JH |
3041 | /* |
3042 | =for apidoc sv_utf8_encode | |
3043 | ||
3044 | Convert the PV of an SV to UTF8-encoded, but then turn off the C<SvUTF8> | |
4411f3b6 NIS |
3045 | flag so that it looks like octets again. Used as a building block |
3046 | for encode_utf8 in Encode.xs | |
c461cf8f JH |
3047 | |
3048 | =cut | |
3049 | */ | |
3050 | ||
560a288e GS |
3051 | void |
3052 | Perl_sv_utf8_encode(pTHX_ register SV *sv) | |
3053 | { | |
4411f3b6 | 3054 | (void) sv_utf8_upgrade(sv); |
560a288e GS |
3055 | SvUTF8_off(sv); |
3056 | } | |
3057 | ||
4411f3b6 NIS |
3058 | /* |
3059 | =for apidoc sv_utf8_decode | |
3060 | ||
3061 | Convert the octets in the PV from UTF-8 to chars. Scan for validity and then | |
3062 | turn of SvUTF8 if needed so that we see characters. Used as a building block | |
3063 | for decode_utf8 in Encode.xs | |
3064 | ||
3065 | =cut | |
3066 | */ | |
3067 | ||
3068 | ||
3069 | ||
560a288e GS |
3070 | bool |
3071 | Perl_sv_utf8_decode(pTHX_ register SV *sv) | |
3072 | { | |
3073 | if (SvPOK(sv)) { | |
3074 | char *c; | |
511c2ff0 | 3075 | char *e; |
9cbac4c7 | 3076 | |
4411f3b6 | 3077 | /* The octets may have got themselves encoded - get them back as bytes */ |
560a288e GS |
3078 | if (!sv_utf8_downgrade(sv, TRUE)) |
3079 | return FALSE; | |
3080 | ||
3081 | /* it is actually just a matter of turning the utf8 flag on, but | |
3082 | * we want to make sure everything inside is valid utf8 first. | |
3083 | */ | |
3084 | c = SvPVX(sv); | |
00df9076 | 3085 | if (!is_utf8_string((U8*)c, SvCUR(sv)+1)) |
67e989fb | 3086 | return FALSE; |
511c2ff0 NIS |
3087 | e = SvEND(sv); |
3088 | while (c < e) { | |
fd400ab9 | 3089 | if (UTF8_IS_CONTINUED(*c++)) { |
67e989fb JH |
3090 | SvUTF8_on(sv); |
3091 | break; | |
3092 | } | |
560a288e | 3093 | } |
560a288e GS |
3094 | } |
3095 | return TRUE; | |
3096 | } | |
3097 | ||
3098 | ||
79072805 | 3099 | /* Note: sv_setsv() should not be called with a source string that needs |
463ee0b2 | 3100 | * to be reused, since it may destroy the source string if it is marked |
79072805 LW |
3101 | * as temporary. |
3102 | */ | |
3103 | ||
954c1994 GS |
3104 | /* |
3105 | =for apidoc sv_setsv | |
3106 | ||
3107 | Copies the contents of the source SV C<ssv> into the destination SV C<dsv>. | |
3108 | The source SV may be destroyed if it is mortal. Does not handle 'set' | |
3109 | magic. See the macro forms C<SvSetSV>, C<SvSetSV_nosteal> and | |
3110 | C<sv_setsv_mg>. | |
3111 | ||
3112 | =cut | |
3113 | */ | |
3114 | ||
79072805 | 3115 | void |
864dbfa3 | 3116 | Perl_sv_setsv(pTHX_ SV *dstr, register SV *sstr) |
79072805 | 3117 | { |
8990e307 LW |
3118 | register U32 sflags; |
3119 | register int dtype; | |
3120 | register int stype; | |
463ee0b2 | 3121 | |
79072805 LW |
3122 | if (sstr == dstr) |
3123 | return; | |
2213622d | 3124 | SV_CHECK_THINKFIRST(dstr); |
79072805 | 3125 | if (!sstr) |
3280af22 | 3126 | sstr = &PL_sv_undef; |
8990e307 LW |
3127 | stype = SvTYPE(sstr); |
3128 | dtype = SvTYPE(dstr); | |
79072805 | 3129 | |
a0d0e21e | 3130 | SvAMAGIC_off(dstr); |
9e7bc3e8 | 3131 | |
463ee0b2 | 3132 | /* There's a lot of redundancy below but we're going for speed here */ |
79072805 | 3133 | |
8990e307 | 3134 | switch (stype) { |
79072805 | 3135 | case SVt_NULL: |
aece5585 | 3136 | undef_sstr: |
20408e3c GS |
3137 | if (dtype != SVt_PVGV) { |
3138 | (void)SvOK_off(dstr); | |
3139 | return; | |
3140 | } | |
3141 | break; | |
463ee0b2 | 3142 | case SVt_IV: |
aece5585 GA |
3143 | if (SvIOK(sstr)) { |
3144 | switch (dtype) { | |
3145 | case SVt_NULL: | |
8990e307 | 3146 | sv_upgrade(dstr, SVt_IV); |
aece5585 GA |
3147 | break; |
3148 | case SVt_NV: | |
8990e307 | 3149 | sv_upgrade(dstr, SVt_PVNV); |
aece5585 GA |
3150 | break; |
3151 | case SVt_RV: | |
3152 | case SVt_PV: | |
a0d0e21e | 3153 | sv_upgrade(dstr, SVt_PVIV); |
aece5585 GA |
3154 | break; |
3155 | } | |
3156 | (void)SvIOK_only(dstr); | |
3157 | SvIVX(dstr) = SvIVX(sstr); | |
25da4f38 IZ |
3158 | if (SvIsUV(sstr)) |
3159 | SvIsUV_on(dstr); | |
27c9684d AP |
3160 | if (SvTAINTED(sstr)) |
3161 | SvTAINT(dstr); | |
aece5585 | 3162 | return; |
8990e307 | 3163 | } |
aece5585 GA |
3164 | goto undef_sstr; |
3165 | ||
463ee0b2 | 3166 | case SVt_NV: |
aece5585 GA |
3167 | if (SvNOK(sstr)) { |
3168 | switch (dtype) { | |
3169 | case SVt_NULL: | |
3170 | case SVt_IV: | |
8990e307 | 3171 | sv_upgrade(dstr, SVt_NV); |
aece5585 GA |
3172 | break; |
3173 | case SVt_RV: | |
3174 | case SVt_PV: | |
3175 | case SVt_PVIV: | |
a0d0e21e | 3176 | sv_upgrade(dstr, SVt_PVNV); |
aece5585 GA |
3177 | break; |
3178 | } | |
3179 | SvNVX(dstr) = SvNVX(sstr); | |
3180 | (void)SvNOK_only(dstr); | |
27c9684d AP |
3181 | if (SvTAINTED(sstr)) |
3182 | SvTAINT(dstr); | |
aece5585 | 3183 | return; |
8990e307 | 3184 | } |
aece5585 GA |
3185 | goto undef_sstr; |
3186 | ||
ed6116ce | 3187 | case SVt_RV: |
8990e307 | 3188 | if (dtype < SVt_RV) |
ed6116ce | 3189 | sv_upgrade(dstr, SVt_RV); |
c07a80fd | 3190 | else if (dtype == SVt_PVGV && |
3191 | SvTYPE(SvRV(sstr)) == SVt_PVGV) { | |
3192 | sstr = SvRV(sstr); | |
a5f75d66 | 3193 | if (sstr == dstr) { |
1d7c1841 GS |
3194 | if (GvIMPORTED(dstr) != GVf_IMPORTED |
3195 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) | |
3196 | { | |
a5f75d66 | 3197 | GvIMPORTED_on(dstr); |
1d7c1841 | 3198 | } |
a5f75d66 AD |
3199 | GvMULTI_on(dstr); |
3200 | return; | |
3201 | } | |
c07a80fd | 3202 | goto glob_assign; |
3203 | } | |
ed6116ce | 3204 | break; |
463ee0b2 | 3205 | case SVt_PV: |
fc36a67e | 3206 | case SVt_PVFM: |
8990e307 | 3207 | if (dtype < SVt_PV) |
463ee0b2 | 3208 | sv_upgrade(dstr, SVt_PV); |
463ee0b2 LW |
3209 | break; |
3210 | case SVt_PVIV: | |
8990e307 | 3211 | if (dtype < SVt_PVIV) |
463ee0b2 | 3212 | sv_upgrade(dstr, SVt_PVIV); |
463ee0b2 LW |
3213 | break; |
3214 | case SVt_PVNV: | |
8990e307 | 3215 | if (dtype < SVt_PVNV) |
463ee0b2 | 3216 | sv_upgrade(dstr, SVt_PVNV); |
463ee0b2 | 3217 | break; |
4633a7c4 LW |
3218 | case SVt_PVAV: |
3219 | case SVt_PVHV: | |
3220 | case SVt_PVCV: | |
4633a7c4 | 3221 | case SVt_PVIO: |
533c011a | 3222 | if (PL_op) |
cea2e8a9 | 3223 | Perl_croak(aTHX_ "Bizarre copy of %s in %s", sv_reftype(sstr, 0), |
22c35a8c | 3224 | PL_op_name[PL_op->op_type]); |
4633a7c4 | 3225 | else |
cea2e8a9 | 3226 | Perl_croak(aTHX_ "Bizarre copy of %s", sv_reftype(sstr, 0)); |
4633a7c4 LW |
3227 | break; |
3228 | ||
79072805 | 3229 | case SVt_PVGV: |
8990e307 | 3230 | if (dtype <= SVt_PVGV) { |
c07a80fd | 3231 | glob_assign: |
a5f75d66 | 3232 | if (dtype != SVt_PVGV) { |
a0d0e21e LW |
3233 | char *name = GvNAME(sstr); |
3234 | STRLEN len = GvNAMELEN(sstr); | |
463ee0b2 | 3235 | sv_upgrade(dstr, SVt_PVGV); |
6662521e | 3236 | sv_magic(dstr, dstr, '*', Nullch, 0); |
85aff577 | 3237 | GvSTASH(dstr) = (HV*)SvREFCNT_inc(GvSTASH(sstr)); |
a0d0e21e LW |
3238 | GvNAME(dstr) = savepvn(name, len); |
3239 | GvNAMELEN(dstr) = len; | |
3240 | SvFAKE_on(dstr); /* can coerce to non-glob */ | |
3241 | } | |
7bac28a0 | 3242 | /* ahem, death to those who redefine active sort subs */ |
3280af22 NIS |
3243 | else if (PL_curstackinfo->si_type == PERLSI_SORT |
3244 | && GvCV(dstr) && PL_sortcop == CvSTART(GvCV(dstr))) | |
cea2e8a9 | 3245 | Perl_croak(aTHX_ "Can't redefine active sort subroutine %s", |
7bac28a0 | 3246 | GvNAME(dstr)); |
5bd07a3d DM |
3247 | |
3248 | #ifdef GV_SHARED_CHECK | |
3249 | if (GvSHARED((GV*)dstr)) { | |
3250 | Perl_croak(aTHX_ PL_no_modify); | |
3251 | } | |
3252 | #endif | |
3253 | ||
a0d0e21e | 3254 | (void)SvOK_off(dstr); |
a5f75d66 | 3255 | GvINTRO_off(dstr); /* one-shot flag */ |
1edc1566 | 3256 | gp_free((GV*)dstr); |
79072805 | 3257 | GvGP(dstr) = gp_ref(GvGP(sstr)); |
27c9684d AP |
3258 | if (SvTAINTED(sstr)) |
3259 | SvTAINT(dstr); | |
1d7c1841 GS |
3260 | if (GvIMPORTED(dstr) != GVf_IMPORTED |
3261 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) | |
3262 | { | |
a5f75d66 | 3263 | GvIMPORTED_on(dstr); |
1d7c1841 | 3264 | } |
a5f75d66 | 3265 | GvMULTI_on(dstr); |
79072805 LW |
3266 | return; |
3267 | } | |
3268 | /* FALL THROUGH */ | |
3269 | ||
3270 | default: | |
973f89ab CS |
3271 | if (SvGMAGICAL(sstr)) { |
3272 | mg_get(sstr); | |
3273 | if (SvTYPE(sstr) != stype) { | |
3274 | stype = SvTYPE(sstr); | |
3275 | if (stype == SVt_PVGV && dtype <= SVt_PVGV) | |
3276 | goto glob_assign; | |
3277 | } | |
3278 | } | |
ded42b9f | 3279 | if (stype == SVt_PVLV) |
6fc92669 | 3280 | (void)SvUPGRADE(dstr, SVt_PVNV); |
ded42b9f | 3281 | else |
6fc92669 | 3282 | (void)SvUPGRADE(dstr, stype); |
79072805 LW |
3283 | } |
3284 | ||
8990e307 LW |
3285 | sflags = SvFLAGS(sstr); |
3286 | ||
3287 | if (sflags & SVf_ROK) { | |
3288 | if (dtype >= SVt_PV) { | |
3289 | if (dtype == SVt_PVGV) { | |
3290 | SV *sref = SvREFCNT_inc(SvRV(sstr)); | |
3291 | SV *dref = 0; | |
a5f75d66 | 3292 | int intro = GvINTRO(dstr); |
a0d0e21e | 3293 | |
5bd07a3d DM |
3294 | #ifdef GV_SHARED_CHECK |
3295 | if (GvSHARED((GV*)dstr)) { | |
3296 | Perl_croak(aTHX_ PL_no_modify); | |
3297 | } | |
3298 | #endif | |
3299 | ||
a0d0e21e LW |
3300 | if (intro) { |
3301 | GP *gp; | |
1d7c1841 | 3302 | gp_free((GV*)dstr); |
a5f75d66 | 3303 | GvINTRO_off(dstr); /* one-shot flag */ |
a0d0e21e | 3304 | Newz(602,gp, 1, GP); |
44a8e56a | 3305 | GvGP(dstr) = gp_ref(gp); |
a0d0e21e | 3306 | GvSV(dstr) = NEWSV(72,0); |
1d7c1841 | 3307 | GvLINE(dstr) = CopLINE(PL_curcop); |
1edc1566 | 3308 | GvEGV(dstr) = (GV*)dstr; |
a0d0e21e | 3309 | } |
a5f75d66 | 3310 | GvMULTI_on(dstr); |
8990e307 LW |
3311 | switch (SvTYPE(sref)) { |
3312 | case SVt_PVAV: | |
a0d0e21e LW |
3313 | if (intro) |
3314 | SAVESPTR(GvAV(dstr)); | |
3315 | else | |
3316 | dref = (SV*)GvAV(dstr); | |
8990e307 | 3317 | GvAV(dstr) = (AV*)sref; |
39bac7f7 | 3318 | if (!GvIMPORTED_AV(dstr) |
1d7c1841 GS |
3319 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
3320 | { | |
a5f75d66 | 3321 | GvIMPORTED_AV_on(dstr); |
1d7c1841 | 3322 | } |
8990e307 LW |
3323 | break; |
3324 | case SVt_PVHV: | |
a0d0e21e LW |
3325 | if (intro) |
3326 | SAVESPTR(GvHV(dstr)); | |
3327 | else | |
3328 | dref = (SV*)GvHV(dstr); | |
8990e307 | 3329 | GvHV(dstr) = (HV*)sref; |
39bac7f7 | 3330 | if (!GvIMPORTED_HV(dstr) |
1d7c1841 GS |
3331 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
3332 | { | |
a5f75d66 | 3333 | GvIMPORTED_HV_on(dstr); |
1d7c1841 | 3334 | } |
8990e307 LW |
3335 | break; |
3336 | case SVt_PVCV: | |
8ebc5c01 | 3337 | if (intro) { |
3338 | if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) { | |
3339 | SvREFCNT_dec(GvCV(dstr)); | |
3340 | GvCV(dstr) = Nullcv; | |
68dc0745 | 3341 | GvCVGEN(dstr) = 0; /* Switch off cacheness. */ |
3280af22 | 3342 | PL_sub_generation++; |
8ebc5c01 | 3343 | } |
a0d0e21e | 3344 | SAVESPTR(GvCV(dstr)); |
8ebc5c01 | 3345 | } |
68dc0745 | 3346 | else |
3347 | dref = (SV*)GvCV(dstr); | |
3348 | if (GvCV(dstr) != (CV*)sref) { | |
748a9306 | 3349 | CV* cv = GvCV(dstr); |
4633a7c4 | 3350 | if (cv) { |
68dc0745 | 3351 | if (!GvCVGEN((GV*)dstr) && |
3352 | (CvROOT(cv) || CvXSUB(cv))) | |
3353 | { | |
7bac28a0 | 3354 | /* ahem, death to those who redefine |
3355 | * active sort subs */ | |
3280af22 NIS |
3356 | if (PL_curstackinfo->si_type == PERLSI_SORT && |
3357 | PL_sortcop == CvSTART(cv)) | |
1c846c1f | 3358 | Perl_croak(aTHX_ |
7bac28a0 | 3359 | "Can't redefine active sort subroutine %s", |
3360 | GvENAME((GV*)dstr)); | |
beab0874 JT |
3361 | /* Redefining a sub - warning is mandatory if |
3362 | it was a const and its value changed. */ | |
3363 | if (ckWARN(WARN_REDEFINE) | |
3364 | || (CvCONST(cv) | |
3365 | && (!CvCONST((CV*)sref) | |
3366 | || sv_cmp(cv_const_sv(cv), | |
3367 | cv_const_sv((CV*)sref))))) | |
3368 | { | |
3369 | Perl_warner(aTHX_ WARN_REDEFINE, | |
3370 | CvCONST(cv) | |
3371 | ? "Constant subroutine %s redefined" | |
47deb5e7 | 3372 | : "Subroutine %s redefined", |
beab0874 JT |
3373 | GvENAME((GV*)dstr)); |
3374 | } | |
9607fc9c | 3375 | } |
3fe9a6f1 | 3376 | cv_ckproto(cv, (GV*)dstr, |
3377 | SvPOK(sref) ? SvPVX(sref) : Nullch); | |
4633a7c4 | 3378 | } |
a5f75d66 | 3379 | GvCV(dstr) = (CV*)sref; |
7a4c00b4 | 3380 | GvCVGEN(dstr) = 0; /* Switch off cacheness. */ |
a5f75d66 | 3381 | GvASSUMECV_on(dstr); |
3280af22 | 3382 | PL_sub_generation++; |
a5f75d66 | 3383 | } |
39bac7f7 | 3384 | if (!GvIMPORTED_CV(dstr) |
1d7c1841 GS |
3385 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
3386 | { | |
a5f75d66 | 3387 | GvIMPORTED_CV_on(dstr); |
1d7c1841 | 3388 | } |
8990e307 | 3389 | break; |
91bba347 LW |
3390 | case SVt_PVIO: |
3391 | if (intro) | |
3392 | SAVESPTR(GvIOp(dstr)); | |
3393 | else | |
3394 | dref = (SV*)GvIOp(dstr); | |
3395 | GvIOp(dstr) = (IO*)sref; | |
3396 | break; | |
f4d13ee9 JH |
3397 | case SVt_PVFM: |
3398 | if (intro) | |
3399 | SAVESPTR(GvFORM(dstr)); | |
3400 | else | |
3401 | dref = (SV*)GvFORM(dstr); | |
3402 | GvFORM(dstr) = (CV*)sref; | |
3403 | break; | |
8990e307 | 3404 | default: |
a0d0e21e LW |
3405 | if (intro) |
3406 | SAVESPTR(GvSV(dstr)); | |
3407 | else | |
3408 | dref = (SV*)GvSV(dstr); | |
8990e307 | 3409 | GvSV(dstr) = sref; |
39bac7f7 | 3410 | if (!GvIMPORTED_SV(dstr) |
1d7c1841 GS |
3411 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
3412 | { | |
a5f75d66 | 3413 | GvIMPORTED_SV_on(dstr); |
1d7c1841 | 3414 | } |
8990e307 LW |
3415 | break; |
3416 | } | |
3417 | if (dref) | |
3418 | SvREFCNT_dec(dref); | |
a0d0e21e LW |
3419 | if (intro) |
3420 | SAVEFREESV(sref); | |
27c9684d AP |
3421 | if (SvTAINTED(sstr)) |
3422 | SvTAINT(dstr); | |
8990e307 LW |
3423 | return; |
3424 | } | |
a0d0e21e | 3425 | if (SvPVX(dstr)) { |
760ac839 | 3426 | (void)SvOOK_off(dstr); /* backoff */ |
50483b2c JD |
3427 | if (SvLEN(dstr)) |
3428 | Safefree(SvPVX(dstr)); | |
a0d0e21e LW |
3429 | SvLEN(dstr)=SvCUR(dstr)=0; |
3430 | } | |
8990e307 | 3431 | } |
a0d0e21e | 3432 | (void)SvOK_off(dstr); |
8990e307 | 3433 | SvRV(dstr) = SvREFCNT_inc(SvRV(sstr)); |
ed6116ce | 3434 | SvROK_on(dstr); |
8990e307 | 3435 | if (sflags & SVp_NOK) { |
3332b3c1 JH |
3436 | SvNOKp_on(dstr); |
3437 | /* Only set the public OK flag if the source has public OK. */ | |
3438 | if (sflags & SVf_NOK) | |
3439 | SvFLAGS(dstr) |= SVf_NOK; | |
ed6116ce LW |
3440 | SvNVX(dstr) = SvNVX(sstr); |
3441 | } | |
8990e307 | 3442 | if (sflags & SVp_IOK) { |
3332b3c1 JH |
3443 | (void)SvIOKp_on(dstr); |
3444 | if (sflags & SVf_IOK) | |
3445 | SvFLAGS(dstr) |= SVf_IOK; | |
2b1c7e3e | 3446 | if (sflags & SVf_IVisUV) |
25da4f38 | 3447 | SvIsUV_on(dstr); |
3332b3c1 | 3448 | SvIVX(dstr) = SvIVX(sstr); |
ed6116ce | 3449 | } |
a0d0e21e LW |
3450 | if (SvAMAGIC(sstr)) { |
3451 | SvAMAGIC_on(dstr); | |
3452 | } | |
ed6116ce | 3453 | } |
8990e307 | 3454 | else if (sflags & SVp_POK) { |
79072805 LW |
3455 | |
3456 | /* | |
3457 | * Check to see if we can just swipe the string. If so, it's a | |
3458 | * possible small lose on short strings, but a big win on long ones. | |
463ee0b2 LW |
3459 | * It might even be a win on short strings if SvPVX(dstr) |
3460 | * has to be allocated and SvPVX(sstr) has to be freed. | |
79072805 LW |
3461 | */ |
3462 | ||
ff68c719 | 3463 | if (SvTEMP(sstr) && /* slated for free anyway? */ |
01b73108 | 3464 | SvREFCNT(sstr) == 1 && /* and no other references to it? */ |
1c846c1f | 3465 | !(sflags & SVf_OOK) && /* and not involved in OOK hack? */ |
4c8f17b9 BH |
3466 | SvLEN(sstr) && /* and really is a string */ |
3467 | !(PL_op && PL_op->op_type == OP_AASSIGN)) /* and won't be needed again, potentially */ | |
a5f75d66 | 3468 | { |
adbc6bb1 | 3469 | if (SvPVX(dstr)) { /* we know that dtype >= SVt_PV */ |
a5f75d66 AD |
3470 | if (SvOOK(dstr)) { |
3471 | SvFLAGS(dstr) &= ~SVf_OOK; | |
3472 | Safefree(SvPVX(dstr) - SvIVX(dstr)); | |
3473 | } | |
50483b2c | 3474 | else if (SvLEN(dstr)) |
a5f75d66 | 3475 | Safefree(SvPVX(dstr)); |
79072805 | 3476 | } |
a5f75d66 | 3477 | (void)SvPOK_only(dstr); |
463ee0b2 | 3478 | SvPV_set(dstr, SvPVX(sstr)); |
79072805 LW |
3479 | SvLEN_set(dstr, SvLEN(sstr)); |
3480 | SvCUR_set(dstr, SvCUR(sstr)); | |
f4e86e0f | 3481 | |
79072805 | 3482 | SvTEMP_off(dstr); |
2b1c7e3e | 3483 | (void)SvOK_off(sstr); /* NOTE: nukes most SvFLAGS on sstr */ |
79072805 LW |
3484 | SvPV_set(sstr, Nullch); |
3485 | SvLEN_set(sstr, 0); | |
a5f75d66 AD |
3486 | SvCUR_set(sstr, 0); |
3487 | SvTEMP_off(sstr); | |
79072805 LW |
3488 | } |
3489 | else { /* have to copy actual string */ | |
8990e307 LW |
3490 | STRLEN len = SvCUR(sstr); |
3491 | ||
3492 | SvGROW(dstr, len + 1); /* inlined from sv_setpvn */ | |
3493 | Move(SvPVX(sstr),SvPVX(dstr),len,char); | |
3494 | SvCUR_set(dstr, len); | |
3495 | *SvEND(dstr) = '\0'; | |
a0d0e21e | 3496 | (void)SvPOK_only(dstr); |
79072805 | 3497 | } |
9aa983d2 | 3498 | if (sflags & SVf_UTF8) |
a7cb1f99 | 3499 | SvUTF8_on(dstr); |
79072805 | 3500 | /*SUPPRESS 560*/ |
8990e307 | 3501 | if (sflags & SVp_NOK) { |
3332b3c1 JH |
3502 | SvNOKp_on(dstr); |
3503 | if (sflags & SVf_NOK) | |
3504 | SvFLAGS(dstr) |= SVf_NOK; | |
463ee0b2 | 3505 | SvNVX(dstr) = SvNVX(sstr); |
79072805 | 3506 | } |
8990e307 | 3507 | if (sflags & SVp_IOK) { |
3332b3c1 JH |
3508 | (void)SvIOKp_on(dstr); |
3509 | if (sflags & SVf_IOK) | |
3510 | SvFLAGS(dstr) |= SVf_IOK; | |
2b1c7e3e | 3511 | if (sflags & SVf_IVisUV) |
25da4f38 | 3512 | SvIsUV_on(dstr); |
463ee0b2 | 3513 | SvIVX(dstr) = SvIVX(sstr); |
79072805 LW |
3514 | } |
3515 | } | |
8990e307 | 3516 | else if (sflags & SVp_IOK) { |
3332b3c1 JH |
3517 | if (sflags & SVf_IOK) |
3518 | (void)SvIOK_only(dstr); | |
3519 | else { | |
9cbac4c7 DM |
3520 | (void)SvOK_off(dstr); |
3521 | (void)SvIOKp_on(dstr); | |
3332b3c1 JH |
3522 | } |
3523 | /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */ | |
2b1c7e3e | 3524 | if (sflags & SVf_IVisUV) |
25da4f38 | 3525 | SvIsUV_on(dstr); |
3332b3c1 JH |
3526 | SvIVX(dstr) = SvIVX(sstr); |
3527 | if (sflags & SVp_NOK) { | |
3528 | if (sflags & SVf_NOK) | |
3529 | (void)SvNOK_on(dstr); | |
3530 | else | |
3531 | (void)SvNOKp_on(dstr); | |
3532 | SvNVX(dstr) = SvNVX(sstr); | |
3533 | } | |
3534 | } | |
3535 | else if (sflags & SVp_NOK) { | |
3536 | if (sflags & SVf_NOK) | |
3537 | (void)SvNOK_only(dstr); | |
3538 | else { | |
9cbac4c7 | 3539 | (void)SvOK_off(dstr); |
3332b3c1 JH |
3540 | SvNOKp_on(dstr); |
3541 | } | |
3542 | SvNVX(dstr) = SvNVX(sstr); | |
79072805 LW |
3543 | } |
3544 | else { | |
20408e3c | 3545 | if (dtype == SVt_PVGV) { |
e476b1b5 GS |
3546 | if (ckWARN(WARN_MISC)) |
3547 | Perl_warner(aTHX_ WARN_MISC, "Undefined value assigned to typeglob"); | |
20408e3c GS |
3548 | } |
3549 | else | |
3550 | (void)SvOK_off(dstr); | |
a0d0e21e | 3551 | } |
27c9684d AP |
3552 | if (SvTAINTED(sstr)) |
3553 | SvTAINT(dstr); | |
79072805 LW |
3554 | } |
3555 | ||
954c1994 GS |
3556 | /* |
3557 | =for apidoc sv_setsv_mg | |
3558 | ||
3559 | Like C<sv_setsv>, but also handles 'set' magic. | |
3560 | ||
3561 | =cut | |
3562 | */ | |
3563 | ||
79072805 | 3564 | void |
864dbfa3 | 3565 | Perl_sv_setsv_mg(pTHX_ SV *dstr, register SV *sstr) |
ef50df4b GS |
3566 | { |
3567 | sv_setsv(dstr,sstr); | |
3568 | SvSETMAGIC(dstr); | |
3569 | } | |
3570 | ||
954c1994 GS |
3571 | /* |
3572 | =for apidoc sv_setpvn | |
3573 | ||
3574 | Copies a string into an SV. The C<len> parameter indicates the number of | |
3575 | bytes to be copied. Does not handle 'set' magic. See C<sv_setpvn_mg>. | |
3576 | ||
3577 | =cut | |
3578 | */ | |
3579 | ||
ef50df4b | 3580 | void |
864dbfa3 | 3581 | Perl_sv_setpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len) |
79072805 | 3582 | { |
c6f8c383 | 3583 | register char *dptr; |
22c522df | 3584 | |
2213622d | 3585 | SV_CHECK_THINKFIRST(sv); |
463ee0b2 | 3586 | if (!ptr) { |
a0d0e21e | 3587 | (void)SvOK_off(sv); |
463ee0b2 LW |
3588 | return; |
3589 | } | |
22c522df JH |
3590 | else { |
3591 | /* len is STRLEN which is unsigned, need to copy to signed */ | |
3592 | IV iv = len; | |
3593 | assert(iv >= 0); | |
3594 | } | |
6fc92669 | 3595 | (void)SvUPGRADE(sv, SVt_PV); |
c6f8c383 | 3596 | |
79072805 | 3597 | SvGROW(sv, len + 1); |
c6f8c383 GA |
3598 | dptr = SvPVX(sv); |
3599 | Move(ptr,dptr,len,char); | |
3600 | dptr[len] = '\0'; | |
79072805 | 3601 | SvCUR_set(sv, len); |
1aa99e6b | 3602 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 | 3603 | SvTAINT(sv); |
79072805 LW |
3604 | } |
3605 | ||
954c1994 GS |
3606 | /* |
3607 | =for apidoc sv_setpvn_mg | |
3608 | ||
3609 | Like C<sv_setpvn>, but also handles 'set' magic. | |
3610 | ||
3611 | =cut | |
3612 | */ | |
3613 | ||
79072805 | 3614 | void |
864dbfa3 | 3615 | Perl_sv_setpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len) |
ef50df4b GS |
3616 | { |
3617 | sv_setpvn(sv,ptr,len); | |
3618 | SvSETMAGIC(sv); | |
3619 | } | |
3620 | ||
954c1994 GS |
3621 | /* |
3622 | =for apidoc sv_setpv | |
3623 | ||
3624 | Copies a string into an SV. The string must be null-terminated. Does not | |
3625 | handle 'set' magic. See C<sv_setpv_mg>. | |
3626 | ||
3627 | =cut | |
3628 | */ | |
3629 | ||
ef50df4b | 3630 | void |
864dbfa3 | 3631 | Perl_sv_setpv(pTHX_ register SV *sv, register const char *ptr) |
79072805 LW |
3632 | { |
3633 | register STRLEN len; | |
3634 | ||
2213622d | 3635 | SV_CHECK_THINKFIRST(sv); |
463ee0b2 | 3636 | if (!ptr) { |
a0d0e21e | 3637 | (void)SvOK_off(sv); |
463ee0b2 LW |
3638 | return; |
3639 | } | |
79072805 | 3640 | len = strlen(ptr); |
6fc92669 | 3641 | (void)SvUPGRADE(sv, SVt_PV); |
c6f8c383 | 3642 | |
79072805 | 3643 | SvGROW(sv, len + 1); |
463ee0b2 | 3644 | Move(ptr,SvPVX(sv),len+1,char); |
79072805 | 3645 | SvCUR_set(sv, len); |
1aa99e6b | 3646 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 LW |
3647 | SvTAINT(sv); |
3648 | } | |
3649 | ||
954c1994 GS |
3650 | /* |
3651 | =for apidoc sv_setpv_mg | |
3652 | ||
3653 | Like C<sv_setpv>, but also handles 'set' magic. | |
3654 | ||
3655 | =cut | |
3656 | */ | |
3657 | ||
463ee0b2 | 3658 | void |
864dbfa3 | 3659 | Perl_sv_setpv_mg(pTHX_ register SV *sv, register const char *ptr) |
ef50df4b GS |
3660 | { |
3661 | sv_setpv(sv,ptr); | |
3662 | SvSETMAGIC(sv); | |
3663 | } | |
3664 | ||
954c1994 GS |
3665 | /* |
3666 | =for apidoc sv_usepvn | |
3667 | ||
3668 | Tells an SV to use C<ptr> to find its string value. Normally the string is | |
1c846c1f | 3669 | stored inside the SV but sv_usepvn allows the SV to use an outside string. |
954c1994 GS |
3670 | The C<ptr> should point to memory that was allocated by C<malloc>. The |
3671 | string length, C<len>, must be supplied. This function will realloc the | |
3672 | memory pointed to by C<ptr>, so that pointer should not be freed or used by | |
3673 | the programmer after giving it to sv_usepvn. Does not handle 'set' magic. | |
3674 | See C<sv_usepvn_mg>. | |
3675 | ||
3676 | =cut | |
3677 | */ | |
3678 | ||
ef50df4b | 3679 | void |
864dbfa3 | 3680 | Perl_sv_usepvn(pTHX_ register SV *sv, register char *ptr, register STRLEN len) |
463ee0b2 | 3681 | { |
2213622d | 3682 | SV_CHECK_THINKFIRST(sv); |
c6f8c383 | 3683 | (void)SvUPGRADE(sv, SVt_PV); |
463ee0b2 | 3684 | if (!ptr) { |
a0d0e21e | 3685 | (void)SvOK_off(sv); |
463ee0b2 LW |
3686 | return; |
3687 | } | |
a0ed51b3 | 3688 | (void)SvOOK_off(sv); |
50483b2c | 3689 | if (SvPVX(sv) && SvLEN(sv)) |
463ee0b2 LW |
3690 | Safefree(SvPVX(sv)); |
3691 | Renew(ptr, len+1, char); | |
3692 | SvPVX(sv) = ptr; | |
3693 | SvCUR_set(sv, len); | |
3694 | SvLEN_set(sv, len+1); | |
3695 | *SvEND(sv) = '\0'; | |
1aa99e6b | 3696 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 | 3697 | SvTAINT(sv); |
79072805 LW |
3698 | } |
3699 | ||
954c1994 GS |
3700 | /* |
3701 | =for apidoc sv_usepvn_mg | |
3702 | ||
3703 | Like C<sv_usepvn>, but also handles 'set' magic. | |
3704 | ||
3705 | =cut | |
3706 | */ | |
3707 | ||
ef50df4b | 3708 | void |
864dbfa3 | 3709 | Perl_sv_usepvn_mg(pTHX_ register SV *sv, register char *ptr, register STRLEN len) |
ef50df4b | 3710 | { |
51c1089b | 3711 | sv_usepvn(sv,ptr,len); |
ef50df4b GS |
3712 | SvSETMAGIC(sv); |
3713 | } | |
3714 | ||
6fc92669 | 3715 | void |
840a7b70 | 3716 | Perl_sv_force_normal_flags(pTHX_ register SV *sv, U32 flags) |
0f15f207 | 3717 | { |
2213622d | 3718 | if (SvREADONLY(sv)) { |
1c846c1f NIS |
3719 | if (SvFAKE(sv)) { |
3720 | char *pvx = SvPVX(sv); | |
3721 | STRLEN len = SvCUR(sv); | |
3722 | U32 hash = SvUVX(sv); | |
3723 | SvGROW(sv, len + 1); | |
3724 | Move(pvx,SvPVX(sv),len,char); | |
3725 | *SvEND(sv) = '\0'; | |
3726 | SvFAKE_off(sv); | |
3727 | SvREADONLY_off(sv); | |
c3654f1a | 3728 | unsharepvn(pvx,SvUTF8(sv)?-len:len,hash); |
1c846c1f NIS |
3729 | } |
3730 | else if (PL_curcop != &PL_compiling) | |
cea2e8a9 | 3731 | Perl_croak(aTHX_ PL_no_modify); |
0f15f207 | 3732 | } |
2213622d | 3733 | if (SvROK(sv)) |
840a7b70 | 3734 | sv_unref_flags(sv, flags); |
6fc92669 GS |
3735 | else if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV) |
3736 | sv_unglob(sv); | |
0f15f207 | 3737 | } |
1c846c1f | 3738 | |
840a7b70 IZ |
3739 | void |
3740 | Perl_sv_force_normal(pTHX_ register SV *sv) | |
3741 | { | |
3742 | sv_force_normal_flags(sv, 0); | |
3743 | } | |
3744 | ||
954c1994 GS |
3745 | /* |
3746 | =for apidoc sv_chop | |
3747 | ||
1c846c1f | 3748 | Efficient removal of characters from the beginning of the string buffer. |
954c1994 GS |
3749 | SvPOK(sv) must be true and the C<ptr> must be a pointer to somewhere inside |
3750 | the string buffer. The C<ptr> becomes the first character of the adjusted | |
3751 | string. | |
3752 | ||
3753 | =cut | |
3754 | */ | |
3755 | ||
79072805 | 3756 | void |
864dbfa3 | 3757 | Perl_sv_chop(pTHX_ register SV *sv, register char *ptr) /* like set but assuming ptr is in sv */ |
1c846c1f NIS |
3758 | |
3759 | ||
79072805 LW |
3760 | { |
3761 | register STRLEN delta; | |
3762 | ||
a0d0e21e | 3763 | if (!ptr || !SvPOKp(sv)) |
79072805 | 3764 | return; |
2213622d | 3765 | SV_CHECK_THINKFIRST(sv); |
79072805 LW |
3766 | if (SvTYPE(sv) < SVt_PVIV) |
3767 | sv_upgrade(sv,SVt_PVIV); | |
3768 | ||
3769 | if (!SvOOK(sv)) { | |
50483b2c JD |
3770 | if (!SvLEN(sv)) { /* make copy of shared string */ |
3771 | char *pvx = SvPVX(sv); | |
3772 | STRLEN len = SvCUR(sv); | |
3773 | SvGROW(sv, len + 1); | |
3774 | Move(pvx,SvPVX(sv),len,char); | |
3775 | *SvEND(sv) = '\0'; | |
3776 | } | |
463ee0b2 | 3777 | SvIVX(sv) = 0; |
79072805 LW |
3778 | SvFLAGS(sv) |= SVf_OOK; |
3779 | } | |
25da4f38 | 3780 | SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVp_IOK|SVp_NOK|SVf_IVisUV); |
463ee0b2 | 3781 | delta = ptr - SvPVX(sv); |
79072805 LW |
3782 | SvLEN(sv) -= delta; |
3783 | SvCUR(sv) -= delta; | |
463ee0b2 LW |
3784 | SvPVX(sv) += delta; |
3785 | SvIVX(sv) += delta; | |
79072805 LW |
3786 | } |
3787 | ||
954c1994 GS |
3788 | /* |
3789 | =for apidoc sv_catpvn | |
3790 | ||
3791 | Concatenates the string onto the end of the string which is in the SV. The | |
3792 | C<len> indicates number of bytes to copy. Handles 'get' magic, but not | |
3793 | 'set' magic. See C<sv_catpvn_mg>. | |
3794 | ||
3795 | =cut | |
3796 | */ | |
3797 | ||
79072805 | 3798 | void |
864dbfa3 | 3799 | Perl_sv_catpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len) |
79072805 | 3800 | { |
463ee0b2 | 3801 | STRLEN tlen; |
748a9306 | 3802 | char *junk; |
a0d0e21e | 3803 | |
748a9306 | 3804 | junk = SvPV_force(sv, tlen); |
463ee0b2 | 3805 | SvGROW(sv, tlen + len + 1); |
4633a7c4 LW |
3806 | if (ptr == junk) |
3807 | ptr = SvPVX(sv); | |
463ee0b2 | 3808 | Move(ptr,SvPVX(sv)+tlen,len,char); |
79072805 LW |
3809 | SvCUR(sv) += len; |
3810 | *SvEND(sv) = '\0'; | |
d41ff1b8 | 3811 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 | 3812 | SvTAINT(sv); |
79072805 LW |
3813 | } |
3814 | ||
954c1994 GS |
3815 | /* |
3816 | =for apidoc sv_catpvn_mg | |
3817 | ||
3818 | Like C<sv_catpvn>, but also handles 'set' magic. | |
3819 | ||
3820 | =cut | |
3821 | */ | |
3822 | ||
79072805 | 3823 | void |
864dbfa3 | 3824 | Perl_sv_catpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len) |
ef50df4b GS |
3825 | { |
3826 | sv_catpvn(sv,ptr,len); | |
3827 | SvSETMAGIC(sv); | |
3828 | } | |
3829 | ||
954c1994 GS |
3830 | /* |
3831 | =for apidoc sv_catsv | |
3832 | ||
13e8c8e3 JH |
3833 | Concatenates the string from SV C<ssv> onto the end of the string in |
3834 | SV C<dsv>. Modifies C<dsv> but not C<ssv>. Handles 'get' magic, but | |
3835 | not 'set' magic. See C<sv_catsv_mg>. | |
954c1994 | 3836 | |
13e8c8e3 | 3837 | =cut */ |
954c1994 | 3838 | |
ef50df4b | 3839 | void |
46199a12 | 3840 | Perl_sv_catsv(pTHX_ SV *dsv, register SV *ssv) |
79072805 | 3841 | { |
13e8c8e3 JH |
3842 | char *spv; |
3843 | STRLEN slen; | |
46199a12 | 3844 | if (!ssv) |
79072805 | 3845 | return; |
46199a12 JH |
3846 | if ((spv = SvPV(ssv, slen))) { |
3847 | bool dutf8 = DO_UTF8(dsv); | |
3848 | bool sutf8 = DO_UTF8(ssv); | |
13e8c8e3 JH |
3849 | |
3850 | if (dutf8 == sutf8) | |
46199a12 | 3851 | sv_catpvn(dsv,spv,slen); |
13e8c8e3 JH |
3852 | else { |
3853 | if (dutf8) { | |
46199a12 JH |
3854 | /* Not modifying source SV, so taking a temporary copy. */ |
3855 | SV* csv = sv_2mortal(newSVsv(ssv)); | |
13e8c8e3 JH |
3856 | char *cpv; |
3857 | STRLEN clen; | |
3858 | ||
46199a12 JH |
3859 | sv_utf8_upgrade(csv); |
3860 | cpv = SvPV(csv,clen); | |
3861 | sv_catpvn(dsv,cpv,clen); | |
13e8c8e3 JH |
3862 | } |
3863 | else { | |
46199a12 JH |
3864 | sv_utf8_upgrade(dsv); |
3865 | sv_catpvn(dsv,spv,slen); | |
3866 | SvUTF8_on(dsv); /* If dsv has no wide characters. */ | |
13e8c8e3 | 3867 | } |
e84ff256 | 3868 | } |
560a288e | 3869 | } |
79072805 LW |
3870 | } |
3871 | ||
954c1994 GS |
3872 | /* |
3873 | =for apidoc sv_catsv_mg | |
3874 | ||
3875 | Like C<sv_catsv>, but also handles 'set' magic. | |
3876 | ||
3877 | =cut | |
3878 | */ | |
3879 | ||
79072805 | 3880 | void |
46199a12 | 3881 | Perl_sv_catsv_mg(pTHX_ SV *dsv, register SV *ssv) |
ef50df4b | 3882 | { |
46199a12 JH |
3883 | sv_catsv(dsv,ssv); |
3884 | SvSETMAGIC(dsv); | |
ef50df4b GS |
3885 | } |
3886 | ||
954c1994 GS |
3887 | /* |
3888 | =for apidoc sv_catpv | |
3889 | ||
3890 | Concatenates the string onto the end of the string which is in the SV. | |
3891 | Handles 'get' magic, but not 'set' magic. See C<sv_catpv_mg>. | |
3892 | ||
3893 | =cut | |
3894 | */ | |
3895 | ||
ef50df4b | 3896 | void |
0c981600 | 3897 | Perl_sv_catpv(pTHX_ register SV *sv, register const char *ptr) |
79072805 LW |
3898 | { |
3899 | register STRLEN len; | |
463ee0b2 | 3900 | STRLEN tlen; |
748a9306 | 3901 | char *junk; |
79072805 | 3902 | |
0c981600 | 3903 | if (!ptr) |
79072805 | 3904 | return; |
748a9306 | 3905 | junk = SvPV_force(sv, tlen); |
0c981600 | 3906 | len = strlen(ptr); |
463ee0b2 | 3907 | SvGROW(sv, tlen + len + 1); |
0c981600 JH |
3908 | if (ptr == junk) |
3909 | ptr = SvPVX(sv); | |
3910 | Move(ptr,SvPVX(sv)+tlen,len+1,char); | |
79072805 | 3911 | SvCUR(sv) += len; |
d41ff1b8 | 3912 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 | 3913 | SvTAINT(sv); |
79072805 LW |
3914 | } |
3915 | ||
954c1994 GS |
3916 | /* |
3917 | =for apidoc sv_catpv_mg | |
3918 | ||
3919 | Like C<sv_catpv>, but also handles 'set' magic. | |
3920 | ||
3921 | =cut | |
3922 | */ | |
3923 | ||
ef50df4b | 3924 | void |
0c981600 | 3925 | Perl_sv_catpv_mg(pTHX_ register SV *sv, register const char *ptr) |
ef50df4b | 3926 | { |
0c981600 | 3927 | sv_catpv(sv,ptr); |
ef50df4b GS |
3928 | SvSETMAGIC(sv); |
3929 | } | |
3930 | ||
79072805 | 3931 | SV * |
864dbfa3 | 3932 | Perl_newSV(pTHX_ STRLEN len) |
79072805 LW |
3933 | { |
3934 | register SV *sv; | |
1c846c1f | 3935 | |
4561caa4 | 3936 | new_SV(sv); |
79072805 LW |
3937 | if (len) { |
3938 | sv_upgrade(sv, SVt_PV); | |
3939 | SvGROW(sv, len + 1); | |
3940 | } | |
3941 | return sv; | |
3942 | } | |
3943 | ||
1edc1566 | 3944 | /* name is assumed to contain an SV* if (name && namelen == HEf_SVKEY) */ |
3945 | ||
954c1994 GS |
3946 | /* |
3947 | =for apidoc sv_magic | |
3948 | ||
3949 | Adds magic to an SV. | |
3950 | ||
3951 | =cut | |
3952 | */ | |
3953 | ||
79072805 | 3954 | void |
864dbfa3 | 3955 | Perl_sv_magic(pTHX_ register SV *sv, SV *obj, int how, const char *name, I32 namlen) |
79072805 LW |
3956 | { |
3957 | MAGIC* mg; | |
1c846c1f | 3958 | |
0f15f207 | 3959 | if (SvREADONLY(sv)) { |
3280af22 | 3960 | if (PL_curcop != &PL_compiling && !strchr("gBf", how)) |
cea2e8a9 | 3961 | Perl_croak(aTHX_ PL_no_modify); |
0f15f207 | 3962 | } |
4633a7c4 | 3963 | if (SvMAGICAL(sv) || (how == 't' && SvTYPE(sv) >= SVt_PVMG)) { |
748a9306 LW |
3964 | if (SvMAGIC(sv) && (mg = mg_find(sv, how))) { |
3965 | if (how == 't') | |
565764a8 | 3966 | mg->mg_len |= 1; |
463ee0b2 | 3967 | return; |
748a9306 | 3968 | } |
463ee0b2 LW |
3969 | } |
3970 | else { | |
c6f8c383 | 3971 | (void)SvUPGRADE(sv, SVt_PVMG); |
463ee0b2 | 3972 | } |
79072805 LW |
3973 | Newz(702,mg, 1, MAGIC); |
3974 | mg->mg_moremagic = SvMAGIC(sv); | |
79072805 | 3975 | SvMAGIC(sv) = mg; |
75f9d97a JH |
3976 | |
3977 | /* Some magic sontains a reference loop, where the sv and object refer to | |
3978 | each other. To prevent a avoid a reference loop that would prevent such | |
3979 | objects being freed, we look for such loops and if we find one we avoid | |
3980 | incrementing the object refcount. */ | |
3981 | if (!obj || obj == sv || how == '#' || how == 'r' || | |
3982 | (SvTYPE(obj) == SVt_PVGV && | |
3983 | (GvSV(obj) == sv || GvHV(obj) == (HV*)sv || GvAV(obj) == (AV*)sv || | |
3984 | GvCV(obj) == (CV*)sv || GvIOp(obj) == (IO*)sv || | |
3985 | GvFORM(obj) == (CV*)sv))) | |
3986 | { | |
8990e307 | 3987 | mg->mg_obj = obj; |
75f9d97a | 3988 | } |
85e6fe83 | 3989 | else { |
8990e307 | 3990 | mg->mg_obj = SvREFCNT_inc(obj); |
85e6fe83 LW |
3991 | mg->mg_flags |= MGf_REFCOUNTED; |
3992 | } | |
79072805 | 3993 | mg->mg_type = how; |
565764a8 | 3994 | mg->mg_len = namlen; |
9cbac4c7 | 3995 | if (name) { |
1edc1566 | 3996 | if (namlen >= 0) |
3997 | mg->mg_ptr = savepvn(name, namlen); | |
c6ee37c5 | 3998 | else if (namlen == HEf_SVKEY) |
1edc1566 | 3999 | mg->mg_ptr = (char*)SvREFCNT_inc((SV*)name); |
9cbac4c7 | 4000 | } |
1c846c1f | 4001 | |
79072805 LW |
4002 | switch (how) { |
4003 | case 0: | |
22c35a8c | 4004 | mg->mg_virtual = &PL_vtbl_sv; |
79072805 | 4005 | break; |
a0d0e21e | 4006 | case 'A': |
22c35a8c | 4007 | mg->mg_virtual = &PL_vtbl_amagic; |
a0d0e21e LW |
4008 | break; |
4009 | case 'a': | |
22c35a8c | 4010 | mg->mg_virtual = &PL_vtbl_amagicelem; |
a0d0e21e LW |
4011 | break; |
4012 | case 'c': | |
d460ef45 | 4013 | mg->mg_virtual = &PL_vtbl_ovrld; |
a0d0e21e | 4014 | break; |
79072805 | 4015 | case 'B': |
22c35a8c | 4016 | mg->mg_virtual = &PL_vtbl_bm; |
79072805 | 4017 | break; |
6cef1e77 | 4018 | case 'D': |
22c35a8c | 4019 | mg->mg_virtual = &PL_vtbl_regdata; |
6cef1e77 IZ |
4020 | break; |
4021 | case 'd': | |
22c35a8c | 4022 | mg->mg_virtual = &PL_vtbl_regdatum; |
6cef1e77 | 4023 | break; |
79072805 | 4024 | case 'E': |
22c35a8c | 4025 | mg->mg_virtual = &PL_vtbl_env; |
79072805 | 4026 | break; |
55497cff | 4027 | case 'f': |
22c35a8c | 4028 | mg->mg_virtual = &PL_vtbl_fm; |
55497cff | 4029 | break; |
79072805 | 4030 | case 'e': |
22c35a8c | 4031 | mg->mg_virtual = &PL_vtbl_envelem; |
79072805 | 4032 | break; |
93a17b20 | 4033 | case 'g': |
22c35a8c | 4034 | mg->mg_virtual = &PL_vtbl_mglob; |
93a17b20 | 4035 | break; |
463ee0b2 | 4036 | case 'I': |
22c35a8c | 4037 | mg->mg_virtual = &PL_vtbl_isa; |
463ee0b2 LW |
4038 | break; |
4039 | case 'i': | |
22c35a8c | 4040 | mg->mg_virtual = &PL_vtbl_isaelem; |
463ee0b2 | 4041 | break; |
16660edb | 4042 | case 'k': |
22c35a8c | 4043 | mg->mg_virtual = &PL_vtbl_nkeys; |
16660edb | 4044 | break; |
79072805 | 4045 | case 'L': |
a0d0e21e | 4046 | SvRMAGICAL_on(sv); |
93a17b20 LW |
4047 | mg->mg_virtual = 0; |
4048 | break; | |
4049 | case 'l': | |
22c35a8c | 4050 | mg->mg_virtual = &PL_vtbl_dbline; |
79072805 | 4051 | break; |
f93b4edd MB |
4052 | #ifdef USE_THREADS |
4053 | case 'm': | |
22c35a8c | 4054 | mg->mg_virtual = &PL_vtbl_mutex; |
f93b4edd MB |
4055 | break; |
4056 | #endif /* USE_THREADS */ | |
36477c24 | 4057 | #ifdef USE_LOCALE_COLLATE |
bbce6d69 | 4058 | case 'o': |
22c35a8c | 4059 | mg->mg_virtual = &PL_vtbl_collxfrm; |
bbce6d69 | 4060 | break; |
36477c24 | 4061 | #endif /* USE_LOCALE_COLLATE */ |
463ee0b2 | 4062 | case 'P': |
22c35a8c | 4063 | mg->mg_virtual = &PL_vtbl_pack; |
463ee0b2 LW |
4064 | break; |
4065 | case 'p': | |
a0d0e21e | 4066 | case 'q': |
22c35a8c | 4067 | mg->mg_virtual = &PL_vtbl_packelem; |
463ee0b2 | 4068 | break; |
c277df42 | 4069 | case 'r': |
22c35a8c | 4070 | mg->mg_virtual = &PL_vtbl_regexp; |
c277df42 | 4071 | break; |
79072805 | 4072 | case 'S': |
22c35a8c | 4073 | mg->mg_virtual = &PL_vtbl_sig; |
79072805 LW |
4074 | break; |
4075 | case 's': | |
22c35a8c | 4076 | mg->mg_virtual = &PL_vtbl_sigelem; |
79072805 | 4077 | break; |
463ee0b2 | 4078 | case 't': |
22c35a8c | 4079 | mg->mg_virtual = &PL_vtbl_taint; |
565764a8 | 4080 | mg->mg_len = 1; |
463ee0b2 | 4081 | break; |
79072805 | 4082 | case 'U': |
22c35a8c | 4083 | mg->mg_virtual = &PL_vtbl_uvar; |
79072805 LW |
4084 | break; |
4085 | case 'v': | |
22c35a8c | 4086 | mg->mg_virtual = &PL_vtbl_vec; |
79072805 LW |
4087 | break; |
4088 | case 'x': | |
22c35a8c | 4089 | mg->mg_virtual = &PL_vtbl_substr; |
79072805 | 4090 | break; |
5f05dabc | 4091 | case 'y': |
22c35a8c | 4092 | mg->mg_virtual = &PL_vtbl_defelem; |
5f05dabc | 4093 | break; |
79072805 | 4094 | case '*': |
22c35a8c | 4095 | mg->mg_virtual = &PL_vtbl_glob; |
79072805 LW |
4096 | break; |
4097 | case '#': | |
22c35a8c | 4098 | mg->mg_virtual = &PL_vtbl_arylen; |
79072805 | 4099 | break; |
a0d0e21e | 4100 | case '.': |
22c35a8c | 4101 | mg->mg_virtual = &PL_vtbl_pos; |
a0d0e21e | 4102 | break; |
810b8aa5 GS |
4103 | case '<': |
4104 | mg->mg_virtual = &PL_vtbl_backref; | |
4105 | break; | |
4633a7c4 LW |
4106 | case '~': /* Reserved for use by extensions not perl internals. */ |
4107 | /* Useful for attaching extension internal data to perl vars. */ | |
4108 | /* Note that multiple extensions may clash if magical scalars */ | |
4109 | /* etc holding private data from one are passed to another. */ | |
4110 | SvRMAGICAL_on(sv); | |
a0d0e21e | 4111 | break; |
79072805 | 4112 | default: |
cea2e8a9 | 4113 | Perl_croak(aTHX_ "Don't know how to handle magic of type '%c'", how); |
463ee0b2 | 4114 | } |
8990e307 LW |
4115 | mg_magical(sv); |
4116 | if (SvGMAGICAL(sv)) | |
4117 | SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK); | |
463ee0b2 LW |
4118 | } |
4119 | ||
c461cf8f JH |
4120 | /* |
4121 | =for apidoc sv_unmagic | |
4122 | ||
4123 | Removes magic from an SV. | |
4124 | ||
4125 | =cut | |
4126 | */ | |
4127 | ||
463ee0b2 | 4128 | int |
864dbfa3 | 4129 | Perl_sv_unmagic(pTHX_ SV *sv, int type) |
463ee0b2 LW |
4130 | { |
4131 | MAGIC* mg; | |
4132 | MAGIC** mgp; | |
91bba347 | 4133 | if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv)) |
463ee0b2 LW |
4134 | return 0; |
4135 | mgp = &SvMAGIC(sv); | |
4136 | for (mg = *mgp; mg; mg = *mgp) { | |
4137 | if (mg->mg_type == type) { | |
4138 | MGVTBL* vtbl = mg->mg_virtual; | |
4139 | *mgp = mg->mg_moremagic; | |
1d7c1841 | 4140 | if (vtbl && vtbl->svt_free) |
fc0dc3b3 | 4141 | CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg); |
9cbac4c7 | 4142 | if (mg->mg_ptr && mg->mg_type != 'g') { |
565764a8 | 4143 | if (mg->mg_len >= 0) |
1edc1566 | 4144 | Safefree(mg->mg_ptr); |
565764a8 | 4145 | else if (mg->mg_len == HEf_SVKEY) |
1edc1566 | 4146 | SvREFCNT_dec((SV*)mg->mg_ptr); |
9cbac4c7 | 4147 | } |
a0d0e21e LW |
4148 | if (mg->mg_flags & MGf_REFCOUNTED) |
4149 | SvREFCNT_dec(mg->mg_obj); | |
463ee0b2 LW |
4150 | Safefree(mg); |
4151 | } | |
4152 | else | |
4153 | mgp = &mg->mg_moremagic; | |
79072805 | 4154 | } |
91bba347 | 4155 | if (!SvMAGIC(sv)) { |
463ee0b2 | 4156 | SvMAGICAL_off(sv); |
8990e307 | 4157 | SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT; |
463ee0b2 LW |
4158 | } |
4159 | ||
4160 | return 0; | |
79072805 LW |
4161 | } |
4162 | ||
c461cf8f JH |
4163 | /* |
4164 | =for apidoc sv_rvweaken | |
4165 | ||
4166 | Weaken a reference. | |
4167 | ||
4168 | =cut | |
4169 | */ | |
4170 | ||
810b8aa5 | 4171 | SV * |
864dbfa3 | 4172 | Perl_sv_rvweaken(pTHX_ SV *sv) |
810b8aa5 GS |
4173 | { |
4174 | SV *tsv; | |
4175 | if (!SvOK(sv)) /* let undefs pass */ | |
4176 | return sv; | |
4177 | if (!SvROK(sv)) | |
cea2e8a9 | 4178 | Perl_croak(aTHX_ "Can't weaken a nonreference"); |
810b8aa5 | 4179 | else if (SvWEAKREF(sv)) { |
810b8aa5 | 4180 | if (ckWARN(WARN_MISC)) |
cea2e8a9 | 4181 | Perl_warner(aTHX_ WARN_MISC, "Reference is already weak"); |
810b8aa5 GS |
4182 | return sv; |
4183 | } | |
4184 | tsv = SvRV(sv); | |
4185 | sv_add_backref(tsv, sv); | |
4186 | SvWEAKREF_on(sv); | |
1c846c1f | 4187 | SvREFCNT_dec(tsv); |
810b8aa5 GS |
4188 | return sv; |
4189 | } | |
4190 | ||
4191 | STATIC void | |
cea2e8a9 | 4192 | S_sv_add_backref(pTHX_ SV *tsv, SV *sv) |
810b8aa5 GS |
4193 | { |
4194 | AV *av; | |
4195 | MAGIC *mg; | |
4196 | if (SvMAGICAL(tsv) && (mg = mg_find(tsv, '<'))) | |
4197 | av = (AV*)mg->mg_obj; | |
4198 | else { | |
4199 | av = newAV(); | |
4200 | sv_magic(tsv, (SV*)av, '<', NULL, 0); | |
4201 | SvREFCNT_dec(av); /* for sv_magic */ | |
4202 | } | |
4203 | av_push(av,sv); | |
4204 | } | |
4205 | ||
1c846c1f | 4206 | STATIC void |
cea2e8a9 | 4207 | S_sv_del_backref(pTHX_ SV *sv) |
810b8aa5 GS |
4208 | { |
4209 | AV *av; | |
4210 | SV **svp; | |
4211 | I32 i; | |
4212 | SV *tsv = SvRV(sv); | |
4213 | MAGIC *mg; | |
4214 | if (!SvMAGICAL(tsv) || !(mg = mg_find(tsv, '<'))) | |
cea2e8a9 | 4215 | Perl_croak(aTHX_ "panic: del_backref"); |
810b8aa5 GS |
4216 | av = (AV *)mg->mg_obj; |
4217 | svp = AvARRAY(av); | |
4218 | i = AvFILLp(av); | |
4219 | while (i >= 0) { | |
4220 | if (svp[i] == sv) { | |
4221 | svp[i] = &PL_sv_undef; /* XXX */ | |
4222 | } | |
4223 | i--; | |
4224 | } | |
4225 | } | |
4226 | ||
954c1994 GS |
4227 | /* |
4228 | =for apidoc sv_insert | |
4229 | ||
4230 | Inserts a string at the specified offset/length within the SV. Similar to | |
4231 | the Perl substr() function. | |
4232 | ||
4233 | =cut | |
4234 | */ | |
4235 | ||
79072805 | 4236 | void |
864dbfa3 | 4237 | Perl_sv_insert(pTHX_ SV *bigstr, STRLEN offset, STRLEN len, char *little, STRLEN littlelen) |
79072805 LW |
4238 | { |
4239 | register char *big; | |
4240 | register char *mid; | |
4241 | register char *midend; | |
4242 | register char *bigend; | |
4243 | register I32 i; | |
6ff81951 | 4244 | STRLEN curlen; |
1c846c1f | 4245 | |
79072805 | 4246 | |
8990e307 | 4247 | if (!bigstr) |
cea2e8a9 | 4248 | Perl_croak(aTHX_ "Can't modify non-existent substring"); |
6ff81951 | 4249 | SvPV_force(bigstr, curlen); |
60fa28ff | 4250 | (void)SvPOK_only_UTF8(bigstr); |
6ff81951 GS |
4251 | if (offset + len > curlen) { |
4252 | SvGROW(bigstr, offset+len+1); | |
4253 | Zero(SvPVX(bigstr)+curlen, offset+len-curlen, char); | |
4254 | SvCUR_set(bigstr, offset+len); | |
4255 | } | |
79072805 | 4256 | |
69b47968 | 4257 | SvTAINT(bigstr); |
79072805 LW |
4258 | i = littlelen - len; |
4259 | if (i > 0) { /* string might grow */ | |
a0d0e21e | 4260 | big = SvGROW(bigstr, SvCUR(bigstr) + i + 1); |
79072805 LW |
4261 | mid = big + offset + len; |
4262 | midend = bigend = big + SvCUR(bigstr); | |
4263 | bigend += i; | |
4264 | *bigend = '\0'; | |
4265 | while (midend > mid) /* shove everything down */ | |
4266 | *--bigend = *--midend; | |
4267 | Move(little,big+offset,littlelen,char); | |
4268 | SvCUR(bigstr) += i; | |
4269 | SvSETMAGIC(bigstr); | |
4270 | return; | |
4271 | } | |
4272 | else if (i == 0) { | |
463ee0b2 | 4273 | Move(little,SvPVX(bigstr)+offset,len,char); |
79072805 LW |
4274 | SvSETMAGIC(bigstr); |
4275 | return; | |
4276 | } | |
4277 | ||
463ee0b2 | 4278 | big = SvPVX(bigstr); |
79072805 LW |
4279 | mid = big + offset; |
4280 | midend = mid + len; | |
4281 | bigend = big + SvCUR(bigstr); | |
4282 | ||
4283 | if (midend > bigend) | |
cea2e8a9 | 4284 | Perl_croak(aTHX_ "panic: sv_insert"); |
79072805 LW |
4285 | |
4286 | if (mid - big > bigend - midend) { /* faster to shorten from end */ | |
4287 | if (littlelen) { | |
4288 | Move(little, mid, littlelen,char); | |
4289 | mid += littlelen; | |
4290 | } | |
4291 | i = bigend - midend; | |
4292 | if (i > 0) { | |
4293 | Move(midend, mid, i,char); | |
4294 | mid += i; | |
4295 | } | |
4296 | *mid = '\0'; | |
4297 | SvCUR_set(bigstr, mid - big); | |
4298 | } | |
4299 | /*SUPPRESS 560*/ | |
155aba94 | 4300 | else if ((i = mid - big)) { /* faster from front */ |
79072805 LW |
4301 | midend -= littlelen; |
4302 | mid = midend; | |
4303 | sv_chop(bigstr,midend-i); | |
4304 | big += i; | |
4305 | while (i--) | |
4306 | *--midend = *--big; | |
4307 | if (littlelen) | |
4308 | Move(little, mid, littlelen,char); | |
4309 | } | |
4310 | else if (littlelen) { | |
4311 | midend -= littlelen; | |
4312 | sv_chop(bigstr,midend); | |
4313 | Move(little,midend,littlelen,char); | |
4314 | } | |
4315 | else { | |
4316 | sv_chop(bigstr,midend); | |
4317 | } | |
4318 | SvSETMAGIC(bigstr); | |
4319 | } | |
4320 | ||
c461cf8f JH |
4321 | /* |
4322 | =for apidoc sv_replace | |
4323 | ||
4324 | Make the first argument a copy of the second, then delete the original. | |
4325 | ||
4326 | =cut | |
4327 | */ | |
79072805 LW |
4328 | |
4329 | void | |
864dbfa3 | 4330 | Perl_sv_replace(pTHX_ register SV *sv, register SV *nsv) |
79072805 LW |
4331 | { |
4332 | U32 refcnt = SvREFCNT(sv); | |
2213622d | 4333 | SV_CHECK_THINKFIRST(sv); |
0453d815 PM |
4334 | if (SvREFCNT(nsv) != 1 && ckWARN_d(WARN_INTERNAL)) |
4335 | Perl_warner(aTHX_ WARN_INTERNAL, "Reference miscount in sv_replace()"); | |
93a17b20 | 4336 | if (SvMAGICAL(sv)) { |
a0d0e21e LW |
4337 | if (SvMAGICAL(nsv)) |
4338 | mg_free(nsv); | |
4339 | else | |
4340 | sv_upgrade(nsv, SVt_PVMG); | |
93a17b20 | 4341 | SvMAGIC(nsv) = SvMAGIC(sv); |
a0d0e21e | 4342 | SvFLAGS(nsv) |= SvMAGICAL(sv); |
93a17b20 LW |
4343 | SvMAGICAL_off(sv); |
4344 | SvMAGIC(sv) = 0; | |
4345 | } | |
79072805 LW |
4346 | SvREFCNT(sv) = 0; |
4347 | sv_clear(sv); | |
477f5d66 | 4348 | assert(!SvREFCNT(sv)); |
79072805 LW |
4349 | StructCopy(nsv,sv,SV); |
4350 | SvREFCNT(sv) = refcnt; | |
1edc1566 | 4351 | SvFLAGS(nsv) |= SVTYPEMASK; /* Mark as freed */ |
463ee0b2 | 4352 | del_SV(nsv); |
79072805 LW |
4353 | } |
4354 | ||
c461cf8f JH |
4355 | /* |
4356 | =for apidoc sv_clear | |
4357 | ||
4358 | Clear an SV, making it empty. Does not free the memory used by the SV | |
4359 | itself. | |
4360 | ||
4361 | =cut | |
4362 | */ | |
4363 | ||
79072805 | 4364 | void |
864dbfa3 | 4365 | Perl_sv_clear(pTHX_ register SV *sv) |
79072805 | 4366 | { |
ec12f114 | 4367 | HV* stash; |
79072805 LW |
4368 | assert(sv); |
4369 | assert(SvREFCNT(sv) == 0); | |
4370 | ||
ed6116ce | 4371 | if (SvOBJECT(sv)) { |
3280af22 | 4372 | if (PL_defstash) { /* Still have a symbol table? */ |
39644a26 | 4373 | dSP; |
32251b26 | 4374 | CV* destructor; |
837485b6 | 4375 | SV tmpref; |
a0d0e21e | 4376 | |
837485b6 GS |
4377 | Zero(&tmpref, 1, SV); |
4378 | sv_upgrade(&tmpref, SVt_RV); | |
4379 | SvROK_on(&tmpref); | |
4380 | SvREADONLY_on(&tmpref); /* DESTROY() could be naughty */ | |
4381 | SvREFCNT(&tmpref) = 1; | |
8ebc5c01 | 4382 | |
d460ef45 | 4383 | do { |
4e8e7886 | 4384 | stash = SvSTASH(sv); |
32251b26 | 4385 | destructor = StashHANDLER(stash,DESTROY); |
4e8e7886 GS |
4386 | if (destructor) { |
4387 | ENTER; | |
e788e7d3 | 4388 | PUSHSTACKi(PERLSI_DESTROY); |
837485b6 | 4389 | SvRV(&tmpref) = SvREFCNT_inc(sv); |
4e8e7886 GS |
4390 | EXTEND(SP, 2); |
4391 | PUSHMARK(SP); | |
837485b6 | 4392 | PUSHs(&tmpref); |
4e8e7886 | 4393 | PUTBACK; |
32251b26 | 4394 | call_sv((SV*)destructor, G_DISCARD|G_EVAL|G_KEEPERR); |
4e8e7886 | 4395 | SvREFCNT(sv)--; |
d3acc0f7 | 4396 | POPSTACK; |
3095d977 | 4397 | SPAGAIN; |
4e8e7886 GS |
4398 | LEAVE; |
4399 | } | |
4400 | } while (SvOBJECT(sv) && SvSTASH(sv) != stash); | |
8ebc5c01 | 4401 | |
837485b6 | 4402 | del_XRV(SvANY(&tmpref)); |
6f44e0a4 JP |
4403 | |
4404 | if (SvREFCNT(sv)) { | |
4405 | if (PL_in_clean_objs) | |
cea2e8a9 | 4406 | Perl_croak(aTHX_ "DESTROY created new reference to dead object '%s'", |
6f44e0a4 JP |
4407 | HvNAME(stash)); |
4408 | /* DESTROY gave object new lease on life */ | |
4409 | return; | |
4410 | } | |
a0d0e21e | 4411 | } |
4e8e7886 | 4412 | |
a0d0e21e | 4413 | if (SvOBJECT(sv)) { |
4e8e7886 | 4414 | SvREFCNT_dec(SvSTASH(sv)); /* possibly of changed persuasion */ |
a0d0e21e LW |
4415 | SvOBJECT_off(sv); /* Curse the object. */ |
4416 | if (SvTYPE(sv) != SVt_PVIO) | |
3280af22 | 4417 | --PL_sv_objcount; /* XXX Might want something more general */ |
a0d0e21e | 4418 | } |
463ee0b2 | 4419 | } |
c07a80fd | 4420 | if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) |
a0d0e21e | 4421 | mg_free(sv); |
ec12f114 | 4422 | stash = NULL; |
79072805 | 4423 | switch (SvTYPE(sv)) { |
8990e307 | 4424 | case SVt_PVIO: |
df0bd2f4 GS |
4425 | if (IoIFP(sv) && |
4426 | IoIFP(sv) != PerlIO_stdin() && | |
5f05dabc | 4427 | IoIFP(sv) != PerlIO_stdout() && |
4428 | IoIFP(sv) != PerlIO_stderr()) | |
93578b34 | 4429 | { |
f2b5be74 | 4430 | io_close((IO*)sv, FALSE); |
93578b34 | 4431 | } |
1d7c1841 | 4432 | if (IoDIRP(sv) && !(IoFLAGS(sv) & IOf_FAKE_DIRP)) |
1236053a | 4433 | PerlDir_close(IoDIRP(sv)); |
1d7c1841 | 4434 | IoDIRP(sv) = (DIR*)NULL; |
8990e307 LW |
4435 | Safefree(IoTOP_NAME(sv)); |
4436 | Safefree(IoFMT_NAME(sv)); | |
4437 | Safefree(IoBOTTOM_NAME(sv)); | |
a0d0e21e | 4438 | /* FALL THROUGH */ |
79072805 | 4439 | case SVt_PVBM: |
a0d0e21e | 4440 | goto freescalar; |
79072805 | 4441 | case SVt_PVCV: |
748a9306 | 4442 | case SVt_PVFM: |
85e6fe83 | 4443 | cv_undef((CV*)sv); |
a0d0e21e | 4444 | goto freescalar; |
79072805 | 4445 | case SVt_PVHV: |
85e6fe83 | 4446 | hv_undef((HV*)sv); |
a0d0e21e | 4447 | break; |
79072805 | 4448 | case SVt_PVAV: |
85e6fe83 | 4449 | av_undef((AV*)sv); |
a0d0e21e | 4450 | break; |
02270b4e GS |
4451 | case SVt_PVLV: |
4452 | SvREFCNT_dec(LvTARG(sv)); | |
4453 | goto freescalar; | |
a0d0e21e | 4454 | case SVt_PVGV: |
1edc1566 | 4455 | gp_free((GV*)sv); |
a0d0e21e | 4456 | Safefree(GvNAME(sv)); |
ec12f114 JPC |
4457 | /* cannot decrease stash refcount yet, as we might recursively delete |
4458 | ourselves when the refcnt drops to zero. Delay SvREFCNT_dec | |
4459 | of stash until current sv is completely gone. | |
4460 | -- JohnPC, 27 Mar 1998 */ | |
4461 | stash = GvSTASH(sv); | |
a0d0e21e | 4462 | /* FALL THROUGH */ |
79072805 | 4463 | case SVt_PVMG: |
79072805 LW |
4464 | case SVt_PVNV: |
4465 | case SVt_PVIV: | |
a0d0e21e LW |
4466 | freescalar: |
4467 | (void)SvOOK_off(sv); | |
79072805 LW |
4468 | /* FALL THROUGH */ |
4469 | case SVt_PV: | |
a0d0e21e | 4470 | case SVt_RV: |
810b8aa5 GS |
4471 | if (SvROK(sv)) { |
4472 | if (SvWEAKREF(sv)) | |
4473 | sv_del_backref(sv); | |
4474 | else | |
4475 | SvREFCNT_dec(SvRV(sv)); | |
4476 | } | |
1edc1566 | 4477 | else if (SvPVX(sv) && SvLEN(sv)) |
463ee0b2 | 4478 | Safefree(SvPVX(sv)); |
1c846c1f | 4479 | else if (SvPVX(sv) && SvREADONLY(sv) && SvFAKE(sv)) { |
c3654f1a | 4480 | unsharepvn(SvPVX(sv),SvUTF8(sv)?-SvCUR(sv):SvCUR(sv),SvUVX(sv)); |
1c846c1f NIS |
4481 | SvFAKE_off(sv); |
4482 | } | |
79072805 | 4483 | break; |
a0d0e21e | 4484 | /* |
79072805 | 4485 | case SVt_NV: |
79072805 | 4486 | case SVt_IV: |
79072805 LW |
4487 | case SVt_NULL: |
4488 | break; | |
a0d0e21e | 4489 | */ |
79072805 LW |
4490 | } |
4491 | ||
4492 | switch (SvTYPE(sv)) { | |
4493 | case SVt_NULL: | |
4494 | break; | |
79072805 LW |
4495 | case SVt_IV: |
4496 | del_XIV(SvANY(sv)); | |
4497 | break; | |
4498 | case SVt_NV: | |
4499 | del_XNV(SvANY(sv)); | |
4500 | break; | |
ed6116ce LW |
4501 | case SVt_RV: |
4502 | del_XRV(SvANY(sv)); | |
4503 | break; | |
79072805 LW |
4504 | case SVt_PV: |
4505 | del_XPV(SvANY(sv)); | |
4506 | break; | |
4507 | case SVt_PVIV: | |
4508 | del_XPVIV(SvANY(sv)); | |
4509 | break; | |
4510 | case SVt_PVNV: | |
4511 | del_XPVNV(SvANY(sv)); | |
4512 | break; | |
4513 | case SVt_PVMG: | |
4514 | del_XPVMG(SvANY(sv)); | |
4515 | break; | |
4516 | case SVt_PVLV: | |
4517 | del_XPVLV(SvANY(sv)); | |
4518 | break; | |
4519 | case SVt_PVAV: | |
4520 | del_XPVAV(SvANY(sv)); | |
4521 | break; | |
4522 | case SVt_PVHV: | |
4523 | del_XPVHV(SvANY(sv)); | |
4524 | break; | |
4525 | case SVt_PVCV: | |
4526 | del_XPVCV(SvANY(sv)); | |
4527 | break; | |
4528 | case SVt_PVGV: | |
4529 | del_XPVGV(SvANY(sv)); | |
ec12f114 JPC |
4530 | /* code duplication for increased performance. */ |
4531 | SvFLAGS(sv) &= SVf_BREAK; | |
4532 | SvFLAGS(sv) |= SVTYPEMASK; | |
4533 | /* decrease refcount of the stash that owns this GV, if any */ | |
4534 | if (stash) | |
4535 | SvREFCNT_dec(stash); | |
4536 | return; /* not break, SvFLAGS reset already happened */ | |
79072805 LW |
4537 | case SVt_PVBM: |
4538 | del_XPVBM(SvANY(sv)); | |
4539 | break; | |
4540 | case SVt_PVFM: | |
4541 | del_XPVFM(SvANY(sv)); | |
4542 | break; | |
8990e307 LW |
4543 | case SVt_PVIO: |
4544 | del_XPVIO(SvANY(sv)); | |
4545 | break; | |
79072805 | 4546 | } |
a0d0e21e | 4547 | SvFLAGS(sv) &= SVf_BREAK; |
8990e307 | 4548 | SvFLAGS(sv) |= SVTYPEMASK; |
79072805 LW |
4549 | } |
4550 | ||
4551 | SV * | |
864dbfa3 | 4552 | Perl_sv_newref(pTHX_ SV *sv) |
79072805 | 4553 | { |
463ee0b2 | 4554 | if (sv) |
dce16143 | 4555 | ATOMIC_INC(SvREFCNT(sv)); |
79072805 LW |
4556 | return sv; |
4557 | } | |
4558 | ||
c461cf8f JH |
4559 | /* |
4560 | =for apidoc sv_free | |
4561 | ||
4562 | Free the memory used by an SV. | |
4563 | ||
4564 | =cut | |
4565 | */ | |
4566 | ||
79072805 | 4567 | void |
864dbfa3 | 4568 | Perl_sv_free(pTHX_ SV *sv) |
79072805 | 4569 | { |
dce16143 MB |
4570 | int refcount_is_zero; |
4571 | ||
79072805 LW |
4572 | if (!sv) |
4573 | return; | |
a0d0e21e LW |
4574 | if (SvREFCNT(sv) == 0) { |
4575 | if (SvFLAGS(sv) & SVf_BREAK) | |
4576 | return; | |
3280af22 | 4577 | if (PL_in_clean_all) /* All is fair */ |
1edc1566 | 4578 | return; |
d689ffdd JP |
4579 | if (SvREADONLY(sv) && SvIMMORTAL(sv)) { |
4580 | /* make sure SvREFCNT(sv)==0 happens very seldom */ | |
4581 | SvREFCNT(sv) = (~(U32)0)/2; | |
4582 | return; | |
4583 | } | |
0453d815 PM |
4584 | if (ckWARN_d(WARN_INTERNAL)) |
4585 | Perl_warner(aTHX_ WARN_INTERNAL, "Attempt to free unreferenced scalar"); | |
79072805 LW |
4586 | return; |
4587 | } | |
dce16143 MB |
4588 | ATOMIC_DEC_AND_TEST(refcount_is_zero, SvREFCNT(sv)); |
4589 | if (!refcount_is_zero) | |
8990e307 | 4590 | return; |
463ee0b2 LW |
4591 | #ifdef DEBUGGING |
4592 | if (SvTEMP(sv)) { | |
0453d815 | 4593 | if (ckWARN_d(WARN_DEBUGGING)) |
f248d071 | 4594 | Perl_warner(aTHX_ WARN_DEBUGGING, |
1d7c1841 GS |
4595 | "Attempt to free temp prematurely: SV 0x%"UVxf, |
4596 | PTR2UV(sv)); | |
79072805 | 4597 | return; |
79072805 | 4598 | } |
463ee0b2 | 4599 | #endif |
d689ffdd JP |
4600 | if (SvREADONLY(sv) && SvIMMORTAL(sv)) { |
4601 | /* make sure SvREFCNT(sv)==0 happens very seldom */ | |
4602 | SvREFCNT(sv) = (~(U32)0)/2; | |
4603 | return; | |
4604 | } | |
79072805 | 4605 | sv_clear(sv); |
477f5d66 CS |
4606 | if (! SvREFCNT(sv)) |
4607 | del_SV(sv); | |
79072805 LW |
4608 | } |
4609 | ||
954c1994 GS |
4610 | /* |
4611 | =for apidoc sv_len | |
4612 | ||
4613 | Returns the length of the string in the SV. See also C<SvCUR>. | |
4614 | ||
4615 | =cut | |
4616 | */ | |
4617 | ||
79072805 | 4618 | STRLEN |
864dbfa3 | 4619 | Perl_sv_len(pTHX_ register SV *sv) |
79072805 | 4620 | { |
748a9306 | 4621 | char *junk; |
463ee0b2 | 4622 | STRLEN len; |
79072805 LW |
4623 | |
4624 | if (!sv) | |
4625 | return 0; | |
4626 | ||
8990e307 | 4627 | if (SvGMAGICAL(sv)) |
565764a8 | 4628 | len = mg_length(sv); |
8990e307 | 4629 | else |
748a9306 | 4630 | junk = SvPV(sv, len); |
463ee0b2 | 4631 | return len; |
79072805 LW |
4632 | } |
4633 | ||
c461cf8f JH |
4634 | /* |
4635 | =for apidoc sv_len_utf8 | |
4636 | ||
4637 | Returns the number of characters in the string in an SV, counting wide | |
4638 | UTF8 bytes as a single character. | |
4639 | ||
4640 | =cut | |
4641 | */ | |
4642 | ||
a0ed51b3 | 4643 | STRLEN |
864dbfa3 | 4644 | Perl_sv_len_utf8(pTHX_ register SV *sv) |
a0ed51b3 | 4645 | { |
a0ed51b3 LW |
4646 | if (!sv) |
4647 | return 0; | |
4648 | ||
a0ed51b3 | 4649 | if (SvGMAGICAL(sv)) |
b76347f2 | 4650 | return mg_length(sv); |
a0ed51b3 | 4651 | else |
b76347f2 JH |
4652 | { |
4653 | STRLEN len; | |
4654 | U8 *s = (U8*)SvPV(sv, len); | |
4655 | ||
d6efbbad | 4656 | return Perl_utf8_length(aTHX_ s, s + len); |
a0ed51b3 | 4657 | } |
a0ed51b3 LW |
4658 | } |
4659 | ||
4660 | void | |
864dbfa3 | 4661 | Perl_sv_pos_u2b(pTHX_ register SV *sv, I32* offsetp, I32* lenp) |
a0ed51b3 | 4662 | { |
dfe13c55 GS |
4663 | U8 *start; |
4664 | U8 *s; | |
4665 | U8 *send; | |
a0ed51b3 LW |
4666 | I32 uoffset = *offsetp; |
4667 | STRLEN len; | |
4668 | ||
4669 | if (!sv) | |
4670 | return; | |
4671 | ||
dfe13c55 | 4672 | start = s = (U8*)SvPV(sv, len); |
a0ed51b3 LW |
4673 | send = s + len; |
4674 | while (s < send && uoffset--) | |
4675 | s += UTF8SKIP(s); | |
bb40f870 GA |
4676 | if (s >= send) |
4677 | s = send; | |
a0ed51b3 LW |
4678 | *offsetp = s - start; |
4679 | if (lenp) { | |
4680 | I32 ulen = *lenp; | |
4681 | start = s; | |
4682 | while (s < send && ulen--) | |
4683 | s += UTF8SKIP(s); | |
bb40f870 GA |
4684 | if (s >= send) |
4685 | s = send; | |
a0ed51b3 LW |
4686 | *lenp = s - start; |
4687 | } | |
4688 | return; | |
4689 | } | |
4690 | ||
4691 | void | |
864dbfa3 | 4692 | Perl_sv_pos_b2u(pTHX_ register SV *sv, I32* offsetp) |
a0ed51b3 | 4693 | { |
dfe13c55 GS |
4694 | U8 *s; |
4695 | U8 *send; | |
a0ed51b3 LW |
4696 | STRLEN len; |
4697 | ||
4698 | if (!sv) | |
4699 | return; | |
4700 | ||
dfe13c55 | 4701 | s = (U8*)SvPV(sv, len); |
a0ed51b3 | 4702 | if (len < *offsetp) |
a0dbb045 | 4703 | Perl_croak(aTHX_ "panic: sv_pos_b2u: bad byte offset"); |
a0ed51b3 LW |
4704 | send = s + *offsetp; |
4705 | len = 0; | |
4706 | while (s < send) { | |
a0dbb045 JH |
4707 | STRLEN n; |
4708 | ||
4709 | if (utf8_to_uv(s, UTF8SKIP(s), &n, 0)) { | |
4710 | s += n; | |
4711 | len++; | |
4712 | } | |
4713 | else | |
4714 | break; | |
a0ed51b3 LW |
4715 | } |
4716 | *offsetp = len; | |
4717 | return; | |
4718 | } | |
4719 | ||
954c1994 GS |
4720 | /* |
4721 | =for apidoc sv_eq | |
4722 | ||
4723 | Returns a boolean indicating whether the strings in the two SVs are | |
4724 | identical. | |
4725 | ||
4726 | =cut | |
4727 | */ | |
4728 | ||
79072805 | 4729 | I32 |
e01b9e88 | 4730 | Perl_sv_eq(pTHX_ register SV *sv1, register SV *sv2) |
79072805 LW |
4731 | { |
4732 | char *pv1; | |
463ee0b2 | 4733 | STRLEN cur1; |
79072805 | 4734 | char *pv2; |
463ee0b2 | 4735 | STRLEN cur2; |
e01b9e88 SC |
4736 | I32 eq = 0; |
4737 | bool pv1tmp = FALSE; | |
4738 | bool pv2tmp = FALSE; | |
79072805 | 4739 | |
e01b9e88 | 4740 | if (!sv1) { |
79072805 LW |
4741 | pv1 = ""; |
4742 | cur1 = 0; | |
4743 | } | |
463ee0b2 | 4744 | else |
e01b9e88 | 4745 | pv1 = SvPV(sv1, cur1); |
79072805 | 4746 | |
e01b9e88 SC |
4747 | if (!sv2){ |
4748 | pv2 = ""; | |
4749 | cur2 = 0; | |
92d29cee | 4750 | } |
e01b9e88 SC |
4751 | else |
4752 | pv2 = SvPV(sv2, cur2); | |
79072805 | 4753 | |
e01b9e88 | 4754 | /* do not utf8ize the comparands as a side-effect */ |
7bbb0251 | 4755 | if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTE) { |
f9a63242 JH |
4756 | bool is_utf8 = TRUE; |
4757 | ||
1aa99e6b IH |
4758 | if (PL_hints & HINT_UTF8_DISTINCT) |
4759 | return FALSE; | |
4760 | ||
e01b9e88 | 4761 | if (SvUTF8(sv1)) { |
f34ff0a8 | 4762 | char *pv = (char*)bytes_from_utf8((U8*)pv1, &cur1, &is_utf8); |
90f44359 JH |
4763 | |
4764 | if ((pv1tmp = (pv != pv1))) | |
4765 | pv1 = pv; | |
e01b9e88 SC |
4766 | } |
4767 | else { | |
f34ff0a8 | 4768 | char *pv = (char *)bytes_from_utf8((U8*)pv2, &cur2, &is_utf8); |
90f44359 JH |
4769 | |
4770 | if ((pv2tmp = (pv != pv2))) | |
4771 | pv2 = pv; | |
e01b9e88 SC |
4772 | } |
4773 | } | |
79072805 | 4774 | |
e01b9e88 SC |
4775 | if (cur1 == cur2) |
4776 | eq = memEQ(pv1, pv2, cur1); | |
4777 | ||
4778 | if (pv1tmp) | |
4779 | Safefree(pv1); | |
4780 | if (pv2tmp) | |
4781 | Safefree(pv2); | |
4782 | ||
4783 | return eq; | |
79072805 LW |
4784 | } |
4785 | ||
954c1994 GS |
4786 | /* |
4787 | =for apidoc sv_cmp | |
4788 | ||
4789 | Compares the strings in two SVs. Returns -1, 0, or 1 indicating whether the | |
4790 | string in C<sv1> is less than, equal to, or greater than the string in | |
4791 | C<sv2>. | |
4792 | ||
4793 | =cut | |
4794 | */ | |
4795 | ||
79072805 | 4796 | I32 |
e01b9e88 | 4797 | Perl_sv_cmp(pTHX_ register SV *sv1, register SV *sv2) |
79072805 | 4798 | { |
560a288e GS |
4799 | STRLEN cur1, cur2; |
4800 | char *pv1, *pv2; | |
1c846c1f | 4801 | I32 cmp; |
e01b9e88 SC |
4802 | bool pv1tmp = FALSE; |
4803 | bool pv2tmp = FALSE; | |
560a288e | 4804 | |
e01b9e88 SC |
4805 | if (!sv1) { |
4806 | pv1 = ""; | |
560a288e GS |
4807 | cur1 = 0; |
4808 | } | |
e01b9e88 SC |
4809 | else |
4810 | pv1 = SvPV(sv1, cur1); | |
560a288e | 4811 | |
e01b9e88 SC |
4812 | if (!sv2){ |
4813 | pv2 = ""; | |
560a288e GS |
4814 | cur2 = 0; |
4815 | } | |
e01b9e88 SC |
4816 | else |
4817 | pv2 = SvPV(sv2, cur2); | |
79072805 | 4818 | |
e01b9e88 SC |
4819 | /* do not utf8ize the comparands as a side-effect */ |
4820 | if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTE) { | |
1aa99e6b IH |
4821 | if (PL_hints & HINT_UTF8_DISTINCT) |
4822 | return SvUTF8(sv1) ? 1 : -1; | |
4823 | ||
e01b9e88 SC |
4824 | if (SvUTF8(sv1)) { |
4825 | pv2 = (char*)bytes_to_utf8((U8*)pv2, &cur2); | |
4826 | pv2tmp = TRUE; | |
4827 | } | |
4828 | else { | |
4829 | pv1 = (char*)bytes_to_utf8((U8*)pv1, &cur1); | |
4830 | pv1tmp = TRUE; | |
4831 | } | |
4832 | } | |
79072805 | 4833 | |
e01b9e88 SC |
4834 | if (!cur1) { |
4835 | cmp = cur2 ? -1 : 0; | |
4836 | } else if (!cur2) { | |
4837 | cmp = 1; | |
4838 | } else { | |
4839 | I32 retval = memcmp((void*)pv1, (void*)pv2, cur1 < cur2 ? cur1 : cur2); | |
4840 | ||
4841 | if (retval) { | |
4842 | cmp = retval < 0 ? -1 : 1; | |
4843 | } else if (cur1 == cur2) { | |
4844 | cmp = 0; | |
4845 | } else { | |
4846 | cmp = cur1 < cur2 ? -1 : 1; | |
4847 | } | |
4848 | } | |
16660edb | 4849 | |
e01b9e88 SC |
4850 | if (pv1tmp) |
4851 | Safefree(pv1); | |
4852 | if (pv2tmp) | |
4853 | Safefree(pv2); | |
16660edb | 4854 | |
e01b9e88 | 4855 | return cmp; |
bbce6d69 | 4856 | } |
16660edb | 4857 | |
c461cf8f JH |
4858 | /* |
4859 | =for apidoc sv_cmp_locale | |
4860 | ||
4861 | Compares the strings in two SVs in a locale-aware manner. See | |
4862 | L</sv_cmp_locale> | |
4863 | ||
4864 | =cut | |
4865 | */ | |
4866 | ||
bbce6d69 | 4867 | I32 |
864dbfa3 | 4868 | Perl_sv_cmp_locale(pTHX_ register SV *sv1, register SV *sv2) |
bbce6d69 | 4869 | { |
36477c24 | 4870 | #ifdef USE_LOCALE_COLLATE |
16660edb | 4871 | |
bbce6d69 | 4872 | char *pv1, *pv2; |
4873 | STRLEN len1, len2; | |
4874 | I32 retval; | |
16660edb | 4875 | |
3280af22 | 4876 | if (PL_collation_standard) |
bbce6d69 | 4877 | goto raw_compare; |
16660edb | 4878 | |
bbce6d69 | 4879 | len1 = 0; |
8ac85365 | 4880 | pv1 = sv1 ? sv_collxfrm(sv1, &len1) : (char *) NULL; |
bbce6d69 | 4881 | len2 = 0; |
8ac85365 | 4882 | pv2 = sv2 ? sv_collxfrm(sv2, &len2) : (char *) NULL; |
16660edb | 4883 | |
bbce6d69 | 4884 | if (!pv1 || !len1) { |
4885 | if (pv2 && len2) | |
4886 | return -1; | |
4887 | else | |
4888 | goto raw_compare; | |
4889 | } | |
4890 | else { | |
4891 | if (!pv2 || !len2) | |
4892 | return 1; | |
4893 | } | |
16660edb | 4894 | |
bbce6d69 | 4895 | retval = memcmp((void*)pv1, (void*)pv2, len1 < len2 ? len1 : len2); |
16660edb | 4896 | |
bbce6d69 | 4897 | if (retval) |
16660edb | 4898 | return retval < 0 ? -1 : 1; |
4899 | ||
bbce6d69 | 4900 | /* |
4901 | * When the result of collation is equality, that doesn't mean | |
4902 | * that there are no differences -- some locales exclude some | |
4903 | * characters from consideration. So to avoid false equalities, | |
4904 | * we use the raw string as a tiebreaker. | |
4905 | */ | |
16660edb | 4906 | |
bbce6d69 | 4907 | raw_compare: |
4908 | /* FALL THROUGH */ | |
16660edb | 4909 | |
36477c24 | 4910 | #endif /* USE_LOCALE_COLLATE */ |
16660edb | 4911 | |
bbce6d69 | 4912 | return sv_cmp(sv1, sv2); |
4913 | } | |
79072805 | 4914 | |
36477c24 | 4915 | #ifdef USE_LOCALE_COLLATE |
7a4c00b4 | 4916 | /* |
4917 | * Any scalar variable may carry an 'o' magic that contains the | |
4918 | * scalar data of the variable transformed to such a format that | |
4919 | * a normal memory comparison can be used to compare the data | |
4920 | * according to the locale settings. | |
4921 | */ | |
bbce6d69 | 4922 | char * |
864dbfa3 | 4923 | Perl_sv_collxfrm(pTHX_ SV *sv, STRLEN *nxp) |
bbce6d69 | 4924 | { |
7a4c00b4 | 4925 | MAGIC *mg; |
16660edb | 4926 | |
8ac85365 | 4927 | mg = SvMAGICAL(sv) ? mg_find(sv, 'o') : (MAGIC *) NULL; |
3280af22 | 4928 | if (!mg || !mg->mg_ptr || *(U32*)mg->mg_ptr != PL_collation_ix) { |
bbce6d69 | 4929 | char *s, *xf; |
4930 | STRLEN len, xlen; | |
4931 | ||
7a4c00b4 | 4932 | if (mg) |
4933 | Safefree(mg->mg_ptr); | |
bbce6d69 | 4934 | s = SvPV(sv, len); |
4935 | if ((xf = mem_collxfrm(s, len, &xlen))) { | |
ff0cee69 | 4936 | if (SvREADONLY(sv)) { |
4937 | SAVEFREEPV(xf); | |
4938 | *nxp = xlen; | |
3280af22 | 4939 | return xf + sizeof(PL_collation_ix); |
ff0cee69 | 4940 | } |
7a4c00b4 | 4941 | if (! mg) { |
4942 | sv_magic(sv, 0, 'o', 0, 0); | |
4943 | mg = mg_find(sv, 'o'); | |
4944 | assert(mg); | |
bbce6d69 | 4945 | } |
7a4c00b4 | 4946 | mg->mg_ptr = xf; |
565764a8 | 4947 | mg->mg_len = xlen; |
7a4c00b4 | 4948 | } |
4949 | else { | |
ff0cee69 | 4950 | if (mg) { |
4951 | mg->mg_ptr = NULL; | |
565764a8 | 4952 | mg->mg_len = -1; |
ff0cee69 | 4953 | } |
bbce6d69 | 4954 | } |
4955 | } | |
7a4c00b4 | 4956 | if (mg && mg->mg_ptr) { |
565764a8 | 4957 | *nxp = mg->mg_len; |
3280af22 | 4958 | return mg->mg_ptr + sizeof(PL_collation_ix); |
bbce6d69 | 4959 | } |
4960 | else { | |
4961 | *nxp = 0; | |
4962 | return NULL; | |
16660edb | 4963 | } |
79072805 LW |
4964 | } |
4965 | ||
36477c24 | 4966 | #endif /* USE_LOCALE_COLLATE */ |
bbce6d69 | 4967 | |
c461cf8f JH |
4968 | /* |
4969 | =for apidoc sv_gets | |
4970 | ||
4971 | Get a line from the filehandle and store it into the SV, optionally | |
4972 | appending to the currently-stored string. | |
4973 | ||
4974 | =cut | |
4975 | */ | |
4976 | ||
79072805 | 4977 | char * |
864dbfa3 | 4978 | Perl_sv_gets(pTHX_ register SV *sv, register PerlIO *fp, I32 append) |
79072805 | 4979 | { |
c07a80fd | 4980 | char *rsptr; |
4981 | STRLEN rslen; | |
4982 | register STDCHAR rslast; | |
4983 | register STDCHAR *bp; | |
4984 | register I32 cnt; | |
4985 | I32 i; | |
4986 | ||
2213622d | 4987 | SV_CHECK_THINKFIRST(sv); |
6fc92669 | 4988 | (void)SvUPGRADE(sv, SVt_PV); |
99491443 | 4989 | |
ff68c719 | 4990 | SvSCREAM_off(sv); |
c07a80fd | 4991 | |
3280af22 | 4992 | if (RsSNARF(PL_rs)) { |
c07a80fd | 4993 | rsptr = NULL; |
4994 | rslen = 0; | |
4995 | } | |
3280af22 | 4996 | else if (RsRECORD(PL_rs)) { |
5b2b9c68 HM |
4997 | I32 recsize, bytesread; |
4998 | char *buffer; | |
4999 | ||
5000 | /* Grab the size of the record we're getting */ | |
3280af22 | 5001 | recsize = SvIV(SvRV(PL_rs)); |
5b2b9c68 | 5002 | (void)SvPOK_only(sv); /* Validate pointer */ |
e670df4e | 5003 | buffer = SvGROW(sv, recsize + 1); |
5b2b9c68 HM |
5004 | /* Go yank in */ |
5005 | #ifdef VMS | |
5006 | /* VMS wants read instead of fread, because fread doesn't respect */ | |
5007 | /* RMS record boundaries. This is not necessarily a good thing to be */ | |
5008 | /* doing, but we've got no other real choice */ | |
5009 | bytesread = PerlLIO_read(PerlIO_fileno(fp), buffer, recsize); | |
5010 | #else | |
5011 | bytesread = PerlIO_read(fp, buffer, recsize); | |
5012 | #endif | |
5013 | SvCUR_set(sv, bytesread); | |
e670df4e | 5014 | buffer[bytesread] = '\0'; |
7d59b7e4 NIS |
5015 | if (PerlIO_isutf8(fp)) |
5016 | SvUTF8_on(sv); | |
5017 | else | |
5018 | SvUTF8_off(sv); | |
5b2b9c68 HM |
5019 | return(SvCUR(sv) ? SvPVX(sv) : Nullch); |
5020 | } | |
3280af22 | 5021 | else if (RsPARA(PL_rs)) { |
c07a80fd | 5022 | rsptr = "\n\n"; |
5023 | rslen = 2; | |
5024 | } | |
7d59b7e4 NIS |
5025 | else { |
5026 | /* Get $/ i.e. PL_rs into same encoding as stream wants */ | |
5027 | if (PerlIO_isutf8(fp)) { | |
5028 | rsptr = SvPVutf8(PL_rs, rslen); | |
5029 | } | |
5030 | else { | |
5031 | if (SvUTF8(PL_rs)) { | |
5032 | if (!sv_utf8_downgrade(PL_rs, TRUE)) { | |
5033 | Perl_croak(aTHX_ "Wide character in $/"); | |
5034 | } | |
5035 | } | |
5036 | rsptr = SvPV(PL_rs, rslen); | |
5037 | } | |
5038 | } | |
5039 | ||
c07a80fd | 5040 | rslast = rslen ? rsptr[rslen - 1] : '\0'; |
5041 | ||
3280af22 | 5042 | if (RsPARA(PL_rs)) { /* have to do this both before and after */ |
79072805 | 5043 | do { /* to make sure file boundaries work right */ |
760ac839 | 5044 | if (PerlIO_eof(fp)) |
a0d0e21e | 5045 | return 0; |
760ac839 | 5046 | i = PerlIO_getc(fp); |
79072805 | 5047 | if (i != '\n') { |
a0d0e21e LW |
5048 | if (i == -1) |
5049 | return 0; | |
760ac839 | 5050 | PerlIO_ungetc(fp,i); |
79072805 LW |
5051 | break; |
5052 | } | |
5053 | } while (i != EOF); | |
5054 | } | |
c07a80fd | 5055 | |
760ac839 LW |
5056 | /* See if we know enough about I/O mechanism to cheat it ! */ |
5057 | ||
5058 | /* This used to be #ifdef test - it is made run-time test for ease | |
1c846c1f | 5059 | of abstracting out stdio interface. One call should be cheap |
760ac839 LW |
5060 | enough here - and may even be a macro allowing compile |
5061 | time optimization. | |
5062 | */ | |
5063 | ||
5064 | if (PerlIO_fast_gets(fp)) { | |
5065 | ||
5066 | /* | |
5067 | * We're going to steal some values from the stdio struct | |
5068 | * and put EVERYTHING in the innermost loop into registers. | |
5069 | */ | |
5070 | register STDCHAR *ptr; | |
5071 | STRLEN bpx; | |
5072 | I32 shortbuffered; | |
5073 | ||
16660edb | 5074 | #if defined(VMS) && defined(PERLIO_IS_STDIO) |
5075 | /* An ungetc()d char is handled separately from the regular | |
5076 | * buffer, so we getc() it back out and stuff it in the buffer. | |
5077 | */ | |
5078 | i = PerlIO_getc(fp); | |
5079 | if (i == EOF) return 0; | |
5080 | *(--((*fp)->_ptr)) = (unsigned char) i; | |
5081 | (*fp)->_cnt++; | |
5082 | #endif | |
c07a80fd | 5083 | |
c2960299 | 5084 | /* Here is some breathtakingly efficient cheating */ |
c07a80fd | 5085 | |
a20bf0c3 | 5086 | cnt = PerlIO_get_cnt(fp); /* get count into register */ |
a0d0e21e | 5087 | (void)SvPOK_only(sv); /* validate pointer */ |
79072805 LW |
5088 | if (SvLEN(sv) - append <= cnt + 1) { /* make sure we have the room */ |
5089 | if (cnt > 80 && SvLEN(sv) > append) { | |
5090 | shortbuffered = cnt - SvLEN(sv) + append + 1; | |
5091 | cnt -= shortbuffered; | |
5092 | } | |
5093 | else { | |
5094 | shortbuffered = 0; | |
bbce6d69 | 5095 | /* remember that cnt can be negative */ |
5096 | SvGROW(sv, append + (cnt <= 0 ? 2 : (cnt + 1))); | |
79072805 LW |
5097 | } |
5098 | } | |
5099 | else | |
5100 | shortbuffered = 0; | |
c07a80fd | 5101 | bp = (STDCHAR*)SvPVX(sv) + append; /* move these two too to registers */ |
a20bf0c3 | 5102 | ptr = (STDCHAR*)PerlIO_get_ptr(fp); |
16660edb | 5103 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5104 | "Screamer: entering, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt)); |
16660edb | 5105 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5106 | "Screamer: entering: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n", |
1c846c1f | 5107 | PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp), |
1d7c1841 | 5108 | PTR2UV(PerlIO_has_base(fp) ? PerlIO_get_base(fp) : 0))); |
79072805 LW |
5109 | for (;;) { |
5110 | screamer: | |
93a17b20 | 5111 | if (cnt > 0) { |
c07a80fd | 5112 | if (rslen) { |
760ac839 LW |
5113 | while (cnt > 0) { /* this | eat */ |
5114 | cnt--; | |
c07a80fd | 5115 | if ((*bp++ = *ptr++) == rslast) /* really | dust */ |
5116 | goto thats_all_folks; /* screams | sed :-) */ | |
5117 | } | |
5118 | } | |
5119 | else { | |
1c846c1f NIS |
5120 | Copy(ptr, bp, cnt, char); /* this | eat */ |
5121 | bp += cnt; /* screams | dust */ | |
c07a80fd | 5122 | ptr += cnt; /* louder | sed :-) */ |
a5f75d66 | 5123 | cnt = 0; |
93a17b20 | 5124 | } |
79072805 LW |
5125 | } |
5126 | ||
748a9306 | 5127 | if (shortbuffered) { /* oh well, must extend */ |
79072805 LW |
5128 | cnt = shortbuffered; |
5129 | shortbuffered = 0; | |
c07a80fd | 5130 | bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */ |
79072805 LW |
5131 | SvCUR_set(sv, bpx); |
5132 | SvGROW(sv, SvLEN(sv) + append + cnt + 2); | |
c07a80fd | 5133 | bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */ |
79072805 LW |
5134 | continue; |
5135 | } | |
5136 | ||
16660edb | 5137 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 GS |
5138 | "Screamer: going to getc, ptr=%"UVuf", cnt=%ld\n", |
5139 | PTR2UV(ptr),(long)cnt)); | |
a20bf0c3 | 5140 | PerlIO_set_ptrcnt(fp, ptr, cnt); /* deregisterize cnt and ptr */ |
16660edb | 5141 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5142 | "Screamer: pre: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n", |
1c846c1f | 5143 | PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp), |
1d7c1841 | 5144 | PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0))); |
1c846c1f | 5145 | /* This used to call 'filbuf' in stdio form, but as that behaves like |
774d564b | 5146 | getc when cnt <= 0 we use PerlIO_getc here to avoid introducing |
5147 | another abstraction. */ | |
760ac839 | 5148 | i = PerlIO_getc(fp); /* get more characters */ |
16660edb | 5149 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5150 | "Screamer: post: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n", |
1c846c1f | 5151 | PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp), |
1d7c1841 | 5152 | PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0))); |
a20bf0c3 JH |
5153 | cnt = PerlIO_get_cnt(fp); |
5154 | ptr = (STDCHAR*)PerlIO_get_ptr(fp); /* reregisterize cnt and ptr */ | |
16660edb | 5155 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5156 | "Screamer: after getc, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt)); |
79072805 | 5157 | |
748a9306 LW |
5158 | if (i == EOF) /* all done for ever? */ |
5159 | goto thats_really_all_folks; | |
5160 | ||
c07a80fd | 5161 | bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */ |
79072805 LW |
5162 | SvCUR_set(sv, bpx); |
5163 | SvGROW(sv, bpx + cnt + 2); | |
c07a80fd | 5164 | bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */ |
5165 | ||
760ac839 | 5166 | *bp++ = i; /* store character from PerlIO_getc */ |
79072805 | 5167 | |
c07a80fd | 5168 | if (rslen && (STDCHAR)i == rslast) /* all done for now? */ |
79072805 | 5169 | goto thats_all_folks; |
79072805 LW |
5170 | } |
5171 | ||
5172 | thats_all_folks: | |
c07a80fd | 5173 | if ((rslen > 1 && (bp - (STDCHAR*)SvPVX(sv) < rslen)) || |
36477c24 | 5174 | memNE((char*)bp - rslen, rsptr, rslen)) |
760ac839 | 5175 | goto screamer; /* go back to the fray */ |
79072805 LW |
5176 | thats_really_all_folks: |
5177 | if (shortbuffered) | |
5178 | cnt += shortbuffered; | |
16660edb | 5179 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5180 | "Screamer: quitting, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt)); |
a20bf0c3 | 5181 | PerlIO_set_ptrcnt(fp, ptr, cnt); /* put these back or we're in trouble */ |
16660edb | 5182 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5183 | "Screamer: end: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n", |
1c846c1f | 5184 | PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp), |
1d7c1841 | 5185 | PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0))); |
79072805 | 5186 | *bp = '\0'; |
760ac839 | 5187 | SvCUR_set(sv, bp - (STDCHAR*)SvPVX(sv)); /* set length */ |
16660edb | 5188 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
fb73857a | 5189 | "Screamer: done, len=%ld, string=|%.*s|\n", |
5190 | (long)SvCUR(sv),(int)SvCUR(sv),SvPVX(sv))); | |
760ac839 LW |
5191 | } |
5192 | else | |
79072805 | 5193 | { |
4d2c4e07 | 5194 | #ifndef EPOC |
760ac839 | 5195 | /*The big, slow, and stupid way */ |
c07a80fd | 5196 | STDCHAR buf[8192]; |
4d2c4e07 OF |
5197 | #else |
5198 | /* Need to work around EPOC SDK features */ | |
5199 | /* On WINS: MS VC5 generates calls to _chkstk, */ | |
5200 | /* if a `large' stack frame is allocated */ | |
5201 | /* gcc on MARM does not generate calls like these */ | |
5202 | STDCHAR buf[1024]; | |
5203 | #endif | |
79072805 | 5204 | |
760ac839 | 5205 | screamer2: |
c07a80fd | 5206 | if (rslen) { |
760ac839 LW |
5207 | register STDCHAR *bpe = buf + sizeof(buf); |
5208 | bp = buf; | |
5209 | while ((i = PerlIO_getc(fp)) != EOF && (*bp++ = i) != rslast && bp < bpe) | |
5210 | ; /* keep reading */ | |
5211 | cnt = bp - buf; | |
c07a80fd | 5212 | } |
5213 | else { | |
760ac839 | 5214 | cnt = PerlIO_read(fp,(char*)buf, sizeof(buf)); |
16660edb | 5215 | /* Accomodate broken VAXC compiler, which applies U8 cast to |
5216 | * both args of ?: operator, causing EOF to change into 255 | |
5217 | */ | |
5218 | if (cnt) { i = (U8)buf[cnt - 1]; } else { i = EOF; } | |
c07a80fd | 5219 | } |
79072805 LW |
5220 | |
5221 | if (append) | |
760ac839 | 5222 | sv_catpvn(sv, (char *) buf, cnt); |
79072805 | 5223 | else |
760ac839 | 5224 | sv_setpvn(sv, (char *) buf, cnt); |
c07a80fd | 5225 | |
5226 | if (i != EOF && /* joy */ | |
5227 | (!rslen || | |
5228 | SvCUR(sv) < rslen || | |
36477c24 | 5229 | memNE(SvPVX(sv) + SvCUR(sv) - rslen, rsptr, rslen))) |
79072805 LW |
5230 | { |
5231 | append = -1; | |
63e4d877 CS |
5232 | /* |
5233 | * If we're reading from a TTY and we get a short read, | |
5234 | * indicating that the user hit his EOF character, we need | |
5235 | * to notice it now, because if we try to read from the TTY | |
5236 | * again, the EOF condition will disappear. | |
5237 | * | |
5238 | * The comparison of cnt to sizeof(buf) is an optimization | |
5239 | * that prevents unnecessary calls to feof(). | |
5240 | * | |
5241 | * - jik 9/25/96 | |
5242 | */ | |
5243 | if (!(cnt < sizeof(buf) && PerlIO_eof(fp))) | |
5244 | goto screamer2; | |
79072805 LW |
5245 | } |
5246 | } | |
5247 | ||
1c846c1f | 5248 | if (RsPARA(PL_rs)) { /* have to do this both before and after */ |
c07a80fd | 5249 | while (i != EOF) { /* to make sure file boundaries work right */ |
760ac839 | 5250 | i = PerlIO_getc(fp); |
79072805 | 5251 | if (i != '\n') { |
760ac839 | 5252 | PerlIO_ungetc(fp,i); |
79072805 LW |
5253 | break; |
5254 | } | |
5255 | } | |
5256 | } | |
c07a80fd | 5257 | |
7d59b7e4 NIS |
5258 | if (PerlIO_isutf8(fp)) |
5259 | SvUTF8_on(sv); | |
5260 | else | |
5261 | SvUTF8_off(sv); | |
5262 | ||
c07a80fd | 5263 | return (SvCUR(sv) - append) ? SvPVX(sv) : Nullch; |
79072805 LW |
5264 | } |
5265 | ||
760ac839 | 5266 | |
954c1994 GS |
5267 | /* |
5268 | =for apidoc sv_inc | |
5269 | ||
5270 | Auto-increment of the value in the SV. | |
5271 | ||
5272 | =cut | |
5273 | */ | |
5274 | ||
79072805 | 5275 | void |
864dbfa3 | 5276 | Perl_sv_inc(pTHX_ register SV *sv) |
79072805 LW |
5277 | { |
5278 | register char *d; | |
463ee0b2 | 5279 | int flags; |
79072805 LW |
5280 | |
5281 | if (!sv) | |
5282 | return; | |
b23a5f78 GB |
5283 | if (SvGMAGICAL(sv)) |
5284 | mg_get(sv); | |
ed6116ce | 5285 | if (SvTHINKFIRST(sv)) { |
0f15f207 | 5286 | if (SvREADONLY(sv)) { |
3280af22 | 5287 | if (PL_curcop != &PL_compiling) |
cea2e8a9 | 5288 | Perl_croak(aTHX_ PL_no_modify); |
0f15f207 | 5289 | } |
a0d0e21e | 5290 | if (SvROK(sv)) { |
b5be31e9 | 5291 | IV i; |
9e7bc3e8 JD |
5292 | if (SvAMAGIC(sv) && AMG_CALLun(sv,inc)) |
5293 | return; | |
56431972 | 5294 | i = PTR2IV(SvRV(sv)); |
b5be31e9 SM |
5295 | sv_unref(sv); |
5296 | sv_setiv(sv, i); | |
a0d0e21e | 5297 | } |
ed6116ce | 5298 | } |
8990e307 | 5299 | flags = SvFLAGS(sv); |
28e5dec8 JH |
5300 | if ((flags & (SVp_NOK|SVp_IOK)) == SVp_NOK) { |
5301 | /* It's (privately or publicly) a float, but not tested as an | |
5302 | integer, so test it to see. */ | |
d460ef45 | 5303 | (void) SvIV(sv); |
28e5dec8 JH |
5304 | flags = SvFLAGS(sv); |
5305 | } | |
5306 | if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) { | |
5307 | /* It's publicly an integer, or privately an integer-not-float */ | |
5308 | oops_its_int: | |
25da4f38 IZ |
5309 | if (SvIsUV(sv)) { |
5310 | if (SvUVX(sv) == UV_MAX) | |
65202027 | 5311 | sv_setnv(sv, (NV)UV_MAX + 1.0); |
25da4f38 IZ |
5312 | else |
5313 | (void)SvIOK_only_UV(sv); | |
5314 | ++SvUVX(sv); | |
5315 | } else { | |
5316 | if (SvIVX(sv) == IV_MAX) | |
28e5dec8 | 5317 | sv_setuv(sv, (UV)IV_MAX + 1); |
25da4f38 IZ |
5318 | else { |
5319 | (void)SvIOK_only(sv); | |
5320 | ++SvIVX(sv); | |
1c846c1f | 5321 | } |
55497cff | 5322 | } |
79072805 LW |
5323 | return; |
5324 | } | |
28e5dec8 JH |
5325 | if (flags & SVp_NOK) { |
5326 | (void)SvNOK_only(sv); | |
5327 | SvNVX(sv) += 1.0; | |
5328 | return; | |
5329 | } | |
5330 | ||
8990e307 | 5331 | if (!(flags & SVp_POK) || !*SvPVX(sv)) { |
28e5dec8 JH |
5332 | if ((flags & SVTYPEMASK) < SVt_PVIV) |
5333 | sv_upgrade(sv, SVt_IV); | |
5334 | (void)SvIOK_only(sv); | |
5335 | SvIVX(sv) = 1; | |
79072805 LW |
5336 | return; |
5337 | } | |
463ee0b2 | 5338 | d = SvPVX(sv); |
79072805 LW |
5339 | while (isALPHA(*d)) d++; |
5340 | while (isDIGIT(*d)) d++; | |
5341 | if (*d) { | |
28e5dec8 JH |
5342 | #ifdef PERL_PRESERVE_IVUV |
5343 | /* Got to punt this an an integer if needs be, but we don't issue | |
5344 | warnings. Probably ought to make the sv_iv_please() that does | |
5345 | the conversion if possible, and silently. */ | |
5346 | I32 numtype = looks_like_number(sv); | |
5347 | if (numtype && !(numtype & IS_NUMBER_INFINITY)) { | |
5348 | /* Need to try really hard to see if it's an integer. | |
5349 | 9.22337203685478e+18 is an integer. | |
5350 | but "9.22337203685478e+18" + 0 is UV=9223372036854779904 | |
5351 | so $a="9.22337203685478e+18"; $a+0; $a++ | |
5352 | needs to be the same as $a="9.22337203685478e+18"; $a++ | |
5353 | or we go insane. */ | |
d460ef45 | 5354 | |
28e5dec8 JH |
5355 | (void) sv_2iv(sv); |
5356 | if (SvIOK(sv)) | |
5357 | goto oops_its_int; | |
5358 | ||
5359 | /* sv_2iv *should* have made this an NV */ | |
5360 | if (flags & SVp_NOK) { | |
5361 | (void)SvNOK_only(sv); | |
5362 | SvNVX(sv) += 1.0; | |
5363 | return; | |
5364 | } | |
5365 | /* I don't think we can get here. Maybe I should assert this | |
5366 | And if we do get here I suspect that sv_setnv will croak. NWC | |
5367 | Fall through. */ | |
5368 | #if defined(USE_LONG_DOUBLE) | |
5369 | 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", | |
5370 | SvPVX(sv), SvIVX(sv), SvNVX(sv))); | |
5371 | #else | |
5372 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%g\n", | |
5373 | SvPVX(sv), SvIVX(sv), SvNVX(sv))); | |
5374 | #endif | |
5375 | } | |
5376 | #endif /* PERL_PRESERVE_IVUV */ | |
5377 | sv_setnv(sv,Atof(SvPVX(sv)) + 1.0); | |
79072805 LW |
5378 | return; |
5379 | } | |
5380 | d--; | |
463ee0b2 | 5381 | while (d >= SvPVX(sv)) { |
79072805 LW |
5382 | if (isDIGIT(*d)) { |
5383 | if (++*d <= '9') | |
5384 | return; | |
5385 | *(d--) = '0'; | |
5386 | } | |
5387 | else { | |
9d116dd7 JH |
5388 | #ifdef EBCDIC |
5389 | /* MKS: The original code here died if letters weren't consecutive. | |
5390 | * at least it didn't have to worry about non-C locales. The | |
5391 | * new code assumes that ('z'-'a')==('Z'-'A'), letters are | |
1c846c1f | 5392 | * arranged in order (although not consecutively) and that only |
9d116dd7 JH |
5393 | * [A-Za-z] are accepted by isALPHA in the C locale. |
5394 | */ | |
5395 | if (*d != 'z' && *d != 'Z') { | |
5396 | do { ++*d; } while (!isALPHA(*d)); | |
5397 | return; | |
5398 | } | |
5399 | *(d--) -= 'z' - 'a'; | |
5400 | #else | |
79072805 LW |
5401 | ++*d; |
5402 | if (isALPHA(*d)) | |
5403 | return; | |
5404 | *(d--) -= 'z' - 'a' + 1; | |
9d116dd7 | 5405 | #endif |
79072805 LW |
5406 | } |
5407 | } | |
5408 | /* oh,oh, the number grew */ | |
5409 | SvGROW(sv, SvCUR(sv) + 2); | |
5410 | SvCUR(sv)++; | |
463ee0b2 | 5411 | for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX(sv); d--) |
79072805 LW |
5412 | *d = d[-1]; |
5413 | if (isDIGIT(d[1])) | |
5414 | *d = '1'; | |
5415 | else | |
5416 | *d = d[1]; | |
5417 | } | |
5418 | ||
954c1994 GS |
5419 | /* |
5420 | =for apidoc sv_dec | |
5421 | ||
5422 | Auto-decrement of the value in the SV. | |
5423 | ||
5424 | =cut | |
5425 | */ | |
5426 | ||
79072805 | 5427 | void |
864dbfa3 | 5428 | Perl_sv_dec(pTHX_ register SV *sv) |
79072805 | 5429 | { |
463ee0b2 LW |
5430 | int flags; |
5431 | ||
79072805 LW |
5432 | if (!sv) |
5433 | return; | |
b23a5f78 GB |
5434 | if (SvGMAGICAL(sv)) |
5435 | mg_get(sv); | |
ed6116ce | 5436 | if (SvTHINKFIRST(sv)) { |
0f15f207 | 5437 | if (SvREADONLY(sv)) { |
3280af22 | 5438 | if (PL_curcop != &PL_compiling) |
cea2e8a9 | 5439 | Perl_croak(aTHX_ PL_no_modify); |
0f15f207 | 5440 | } |
a0d0e21e | 5441 | if (SvROK(sv)) { |
b5be31e9 | 5442 | IV i; |
9e7bc3e8 JD |
5443 | if (SvAMAGIC(sv) && AMG_CALLun(sv,dec)) |
5444 | return; | |
56431972 | 5445 | i = PTR2IV(SvRV(sv)); |
b5be31e9 SM |
5446 | sv_unref(sv); |
5447 | sv_setiv(sv, i); | |
a0d0e21e | 5448 | } |
ed6116ce | 5449 | } |
28e5dec8 JH |
5450 | /* Unlike sv_inc we don't have to worry about string-never-numbers |
5451 | and keeping them magic. But we mustn't warn on punting */ | |
8990e307 | 5452 | flags = SvFLAGS(sv); |
28e5dec8 JH |
5453 | if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) { |
5454 | /* It's publicly an integer, or privately an integer-not-float */ | |
5455 | oops_its_int: | |
25da4f38 IZ |
5456 | if (SvIsUV(sv)) { |
5457 | if (SvUVX(sv) == 0) { | |
5458 | (void)SvIOK_only(sv); | |
5459 | SvIVX(sv) = -1; | |
5460 | } | |
5461 | else { | |
5462 | (void)SvIOK_only_UV(sv); | |
5463 | --SvUVX(sv); | |
1c846c1f | 5464 | } |
25da4f38 IZ |
5465 | } else { |
5466 | if (SvIVX(sv) == IV_MIN) | |
65202027 | 5467 | sv_setnv(sv, (NV)IV_MIN - 1.0); |
25da4f38 IZ |
5468 | else { |
5469 | (void)SvIOK_only(sv); | |
5470 | --SvIVX(sv); | |
1c846c1f | 5471 | } |
55497cff | 5472 | } |
5473 | return; | |
5474 | } | |
28e5dec8 JH |
5475 | if (flags & SVp_NOK) { |
5476 | SvNVX(sv) -= 1.0; | |
5477 | (void)SvNOK_only(sv); | |
5478 | return; | |
5479 | } | |
8990e307 | 5480 | if (!(flags & SVp_POK)) { |
4633a7c4 LW |
5481 | if ((flags & SVTYPEMASK) < SVt_PVNV) |
5482 | sv_upgrade(sv, SVt_NV); | |
463ee0b2 | 5483 | SvNVX(sv) = -1.0; |
a0d0e21e | 5484 | (void)SvNOK_only(sv); |
79072805 LW |
5485 | return; |
5486 | } | |
28e5dec8 JH |
5487 | #ifdef PERL_PRESERVE_IVUV |
5488 | { | |
5489 | I32 numtype = looks_like_number(sv); | |
5490 | if (numtype && !(numtype & IS_NUMBER_INFINITY)) { | |
5491 | /* Need to try really hard to see if it's an integer. | |
5492 | 9.22337203685478e+18 is an integer. | |
5493 | but "9.22337203685478e+18" + 0 is UV=9223372036854779904 | |
5494 | so $a="9.22337203685478e+18"; $a+0; $a-- | |
5495 | needs to be the same as $a="9.22337203685478e+18"; $a-- | |
5496 | or we go insane. */ | |
d460ef45 | 5497 | |
28e5dec8 JH |
5498 | (void) sv_2iv(sv); |
5499 | if (SvIOK(sv)) | |
5500 | goto oops_its_int; | |
5501 | ||
5502 | /* sv_2iv *should* have made this an NV */ | |
5503 | if (flags & SVp_NOK) { | |
5504 | (void)SvNOK_only(sv); | |
5505 | SvNVX(sv) -= 1.0; | |
5506 | return; | |
5507 | } | |
5508 | /* I don't think we can get here. Maybe I should assert this | |
5509 | And if we do get here I suspect that sv_setnv will croak. NWC | |
5510 | Fall through. */ | |
5511 | #if defined(USE_LONG_DOUBLE) | |
5512 | 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", | |
5513 | SvPVX(sv), SvIVX(sv), SvNVX(sv))); | |
5514 | #else | |
5515 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%g\n", | |
5516 | SvPVX(sv), SvIVX(sv), SvNVX(sv))); | |
5517 | #endif | |
5518 | } | |
5519 | } | |
5520 | #endif /* PERL_PRESERVE_IVUV */ | |
097ee67d | 5521 | sv_setnv(sv,Atof(SvPVX(sv)) - 1.0); /* punt */ |
79072805 LW |
5522 | } |
5523 | ||
954c1994 GS |
5524 | /* |
5525 | =for apidoc sv_mortalcopy | |
5526 | ||
5527 | Creates a new SV which is a copy of the original SV. The new SV is marked | |
5528 | as mortal. | |
5529 | ||
5530 | =cut | |
5531 | */ | |
5532 | ||
79072805 LW |
5533 | /* Make a string that will exist for the duration of the expression |
5534 | * evaluation. Actually, it may have to last longer than that, but | |
5535 | * hopefully we won't free it until it has been assigned to a | |
5536 | * permanent location. */ | |
5537 | ||
5538 | SV * | |
864dbfa3 | 5539 | Perl_sv_mortalcopy(pTHX_ SV *oldstr) |
79072805 | 5540 | { |
463ee0b2 | 5541 | register SV *sv; |
79072805 | 5542 | |
4561caa4 | 5543 | new_SV(sv); |
79072805 | 5544 | sv_setsv(sv,oldstr); |
677b06e3 GS |
5545 | EXTEND_MORTAL(1); |
5546 | PL_tmps_stack[++PL_tmps_ix] = sv; | |
8990e307 LW |
5547 | SvTEMP_on(sv); |
5548 | return sv; | |
5549 | } | |
5550 | ||
954c1994 GS |
5551 | /* |
5552 | =for apidoc sv_newmortal | |
5553 | ||
5554 | Creates a new SV which is mortal. The reference count of the SV is set to 1. | |
5555 | ||
5556 | =cut | |
5557 | */ | |
5558 | ||
8990e307 | 5559 | SV * |
864dbfa3 | 5560 | Perl_sv_newmortal(pTHX) |
8990e307 LW |
5561 | { |
5562 | register SV *sv; | |
5563 | ||
4561caa4 | 5564 | new_SV(sv); |
8990e307 | 5565 | SvFLAGS(sv) = SVs_TEMP; |
677b06e3 GS |
5566 | EXTEND_MORTAL(1); |
5567 | PL_tmps_stack[++PL_tmps_ix] = sv; | |
79072805 LW |
5568 | return sv; |
5569 | } | |
5570 | ||
954c1994 GS |
5571 | /* |
5572 | =for apidoc sv_2mortal | |
5573 | ||
5574 | Marks an SV as mortal. The SV will be destroyed when the current context | |
5575 | ends. | |
5576 | ||
5577 | =cut | |
5578 | */ | |
5579 | ||
79072805 LW |
5580 | /* same thing without the copying */ |
5581 | ||
5582 | SV * | |
864dbfa3 | 5583 | Perl_sv_2mortal(pTHX_ register SV *sv) |
79072805 LW |
5584 | { |
5585 | if (!sv) | |
5586 | return sv; | |
d689ffdd | 5587 | if (SvREADONLY(sv) && SvIMMORTAL(sv)) |
11162842 | 5588 | return sv; |
677b06e3 GS |
5589 | EXTEND_MORTAL(1); |
5590 | PL_tmps_stack[++PL_tmps_ix] = sv; | |
8990e307 | 5591 | SvTEMP_on(sv); |
79072805 LW |
5592 | return sv; |
5593 | } | |
5594 | ||
954c1994 GS |
5595 | /* |
5596 | =for apidoc newSVpv | |
5597 | ||
5598 | Creates a new SV and copies a string into it. The reference count for the | |
5599 | SV is set to 1. If C<len> is zero, Perl will compute the length using | |
5600 | strlen(). For efficiency, consider using C<newSVpvn> instead. | |
5601 | ||
5602 | =cut | |
5603 | */ | |
5604 | ||
79072805 | 5605 | SV * |
864dbfa3 | 5606 | Perl_newSVpv(pTHX_ const char *s, STRLEN len) |
79072805 | 5607 | { |
463ee0b2 | 5608 | register SV *sv; |
79072805 | 5609 | |
4561caa4 | 5610 | new_SV(sv); |
79072805 LW |
5611 | if (!len) |
5612 | len = strlen(s); | |
5613 | sv_setpvn(sv,s,len); | |
5614 | return sv; | |
5615 | } | |
5616 | ||
954c1994 GS |
5617 | /* |
5618 | =for apidoc newSVpvn | |
5619 | ||
5620 | Creates a new SV and copies a string into it. The reference count for the | |
1c846c1f | 5621 | SV is set to 1. Note that if C<len> is zero, Perl will create a zero length |
954c1994 GS |
5622 | string. You are responsible for ensuring that the source string is at least |
5623 | C<len> bytes long. | |
5624 | ||
5625 | =cut | |
5626 | */ | |
5627 | ||
9da1e3b5 | 5628 | SV * |
864dbfa3 | 5629 | Perl_newSVpvn(pTHX_ const char *s, STRLEN len) |
9da1e3b5 MUN |
5630 | { |
5631 | register SV *sv; | |
5632 | ||
5633 | new_SV(sv); | |
9da1e3b5 MUN |
5634 | sv_setpvn(sv,s,len); |
5635 | return sv; | |
5636 | } | |
5637 | ||
1c846c1f NIS |
5638 | /* |
5639 | =for apidoc newSVpvn_share | |
5640 | ||
5641 | Creates a new SV and populates it with a string from | |
5642 | the string table. Turns on READONLY and FAKE. | |
5643 | The idea here is that as string table is used for shared hash | |
5644 | keys these strings will have SvPVX == HeKEY and hash lookup | |
5645 | will avoid string compare. | |
5646 | ||
5647 | =cut | |
5648 | */ | |
5649 | ||
5650 | SV * | |
c3654f1a | 5651 | Perl_newSVpvn_share(pTHX_ const char *src, I32 len, U32 hash) |
1c846c1f NIS |
5652 | { |
5653 | register SV *sv; | |
c3654f1a IH |
5654 | bool is_utf8 = FALSE; |
5655 | if (len < 0) { | |
5656 | len = -len; | |
5657 | is_utf8 = TRUE; | |
5658 | } | |
75a54232 JH |
5659 | if (is_utf8 && !(PL_hints & HINT_UTF8_DISTINCT)) { |
5660 | STRLEN tmplen = len; | |
5661 | /* See the note in hv.c:hv_fetch() --jhi */ | |
5662 | src = (char*)bytes_from_utf8((U8*)src, &tmplen, &is_utf8); | |
5663 | len = tmplen; | |
5664 | } | |
1c846c1f NIS |
5665 | if (!hash) |
5666 | PERL_HASH(hash, src, len); | |
5667 | new_SV(sv); | |
5668 | sv_upgrade(sv, SVt_PVIV); | |
c3654f1a | 5669 | SvPVX(sv) = sharepvn(src, is_utf8?-len:len, hash); |
1c846c1f NIS |
5670 | SvCUR(sv) = len; |
5671 | SvUVX(sv) = hash; | |
5672 | SvLEN(sv) = 0; | |
5673 | SvREADONLY_on(sv); | |
5674 | SvFAKE_on(sv); | |
5675 | SvPOK_on(sv); | |
c3654f1a IH |
5676 | if (is_utf8) |
5677 | SvUTF8_on(sv); | |
1c846c1f NIS |
5678 | return sv; |
5679 | } | |
5680 | ||
cea2e8a9 | 5681 | #if defined(PERL_IMPLICIT_CONTEXT) |
46fc3d4c | 5682 | SV * |
cea2e8a9 | 5683 | Perl_newSVpvf_nocontext(const char* pat, ...) |
46fc3d4c | 5684 | { |
cea2e8a9 | 5685 | dTHX; |
46fc3d4c | 5686 | register SV *sv; |
5687 | va_list args; | |
46fc3d4c | 5688 | va_start(args, pat); |
c5be433b | 5689 | sv = vnewSVpvf(pat, &args); |
46fc3d4c | 5690 | va_end(args); |
5691 | return sv; | |
5692 | } | |
cea2e8a9 | 5693 | #endif |
46fc3d4c | 5694 | |
954c1994 GS |
5695 | /* |
5696 | =for apidoc newSVpvf | |
5697 | ||
5698 | Creates a new SV an initialize it with the string formatted like | |
5699 | C<sprintf>. | |
5700 | ||
5701 | =cut | |
5702 | */ | |
5703 | ||
cea2e8a9 GS |
5704 | SV * |
5705 | Perl_newSVpvf(pTHX_ const char* pat, ...) | |
5706 | { | |
5707 | register SV *sv; | |
5708 | va_list args; | |
cea2e8a9 | 5709 | va_start(args, pat); |
c5be433b | 5710 | sv = vnewSVpvf(pat, &args); |
cea2e8a9 GS |
5711 | va_end(args); |
5712 | return sv; | |
5713 | } | |
46fc3d4c | 5714 | |
79072805 | 5715 | SV * |
c5be433b GS |
5716 | Perl_vnewSVpvf(pTHX_ const char* pat, va_list* args) |
5717 | { | |
5718 | register SV *sv; | |
5719 | new_SV(sv); | |
5720 | sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
5721 | return sv; | |
5722 | } | |
5723 | ||
954c1994 GS |
5724 | /* |
5725 | =for apidoc newSVnv | |
5726 | ||
5727 | Creates a new SV and copies a floating point value into it. | |
5728 | The reference count for the SV is set to 1. | |
5729 | ||
5730 | =cut | |
5731 | */ | |
5732 | ||
c5be433b | 5733 | SV * |
65202027 | 5734 | Perl_newSVnv(pTHX_ NV n) |
79072805 | 5735 | { |
463ee0b2 | 5736 | register SV *sv; |
79072805 | 5737 | |
4561caa4 | 5738 | new_SV(sv); |
79072805 LW |
5739 | sv_setnv(sv,n); |
5740 | return sv; | |
5741 | } | |
5742 | ||
954c1994 GS |
5743 | /* |
5744 | =for apidoc newSViv | |
5745 | ||
5746 | Creates a new SV and copies an integer into it. The reference count for the | |
5747 | SV is set to 1. | |
5748 | ||
5749 | =cut | |
5750 | */ | |
5751 | ||
79072805 | 5752 | SV * |
864dbfa3 | 5753 | Perl_newSViv(pTHX_ IV i) |
79072805 | 5754 | { |
463ee0b2 | 5755 | register SV *sv; |
79072805 | 5756 | |
4561caa4 | 5757 | new_SV(sv); |
79072805 LW |
5758 | sv_setiv(sv,i); |
5759 | return sv; | |
5760 | } | |
5761 | ||
954c1994 | 5762 | /* |
1a3327fb JH |
5763 | =for apidoc newSVuv |
5764 | ||
5765 | Creates a new SV and copies an unsigned integer into it. | |
5766 | The reference count for the SV is set to 1. | |
5767 | ||
5768 | =cut | |
5769 | */ | |
5770 | ||
5771 | SV * | |
5772 | Perl_newSVuv(pTHX_ UV u) | |
5773 | { | |
5774 | register SV *sv; | |
5775 | ||
5776 | new_SV(sv); | |
5777 | sv_setuv(sv,u); | |
5778 | return sv; | |
5779 | } | |
5780 | ||
5781 | /* | |
954c1994 GS |
5782 | =for apidoc newRV_noinc |
5783 | ||
5784 | Creates an RV wrapper for an SV. The reference count for the original | |
5785 | SV is B<not> incremented. | |
5786 | ||
5787 | =cut | |
5788 | */ | |
5789 | ||
2304df62 | 5790 | SV * |
864dbfa3 | 5791 | Perl_newRV_noinc(pTHX_ SV *tmpRef) |
2304df62 AD |
5792 | { |
5793 | register SV *sv; | |
5794 | ||
4561caa4 | 5795 | new_SV(sv); |
2304df62 | 5796 | sv_upgrade(sv, SVt_RV); |
76e3520e | 5797 | SvTEMP_off(tmpRef); |
d689ffdd | 5798 | SvRV(sv) = tmpRef; |
2304df62 | 5799 | SvROK_on(sv); |
2304df62 AD |
5800 | return sv; |
5801 | } | |
5802 | ||
954c1994 | 5803 | /* newRV_inc is #defined to newRV in sv.h */ |
5f05dabc | 5804 | SV * |
864dbfa3 | 5805 | Perl_newRV(pTHX_ SV *tmpRef) |
5f05dabc | 5806 | { |
5f6447b6 | 5807 | return newRV_noinc(SvREFCNT_inc(tmpRef)); |
5f05dabc | 5808 | } |
5f05dabc | 5809 | |
954c1994 GS |
5810 | /* |
5811 | =for apidoc newSVsv | |
5812 | ||
5813 | Creates a new SV which is an exact duplicate of the original SV. | |
5814 | ||
5815 | =cut | |
5816 | */ | |
5817 | ||
79072805 LW |
5818 | /* make an exact duplicate of old */ |
5819 | ||
5820 | SV * | |
864dbfa3 | 5821 | Perl_newSVsv(pTHX_ register SV *old) |
79072805 | 5822 | { |
463ee0b2 | 5823 | register SV *sv; |
79072805 LW |
5824 | |
5825 | if (!old) | |
5826 | return Nullsv; | |
8990e307 | 5827 | if (SvTYPE(old) == SVTYPEMASK) { |
0453d815 PM |
5828 | if (ckWARN_d(WARN_INTERNAL)) |
5829 | Perl_warner(aTHX_ WARN_INTERNAL, "semi-panic: attempt to dup freed string"); | |
79072805 LW |
5830 | return Nullsv; |
5831 | } | |
4561caa4 | 5832 | new_SV(sv); |
ff68c719 | 5833 | if (SvTEMP(old)) { |
5834 | SvTEMP_off(old); | |
463ee0b2 | 5835 | sv_setsv(sv,old); |
ff68c719 | 5836 | SvTEMP_on(old); |
79072805 LW |
5837 | } |
5838 | else | |
463ee0b2 LW |
5839 | sv_setsv(sv,old); |
5840 | return sv; | |
79072805 LW |
5841 | } |
5842 | ||
5843 | void | |
864dbfa3 | 5844 | Perl_sv_reset(pTHX_ register char *s, HV *stash) |
79072805 LW |
5845 | { |
5846 | register HE *entry; | |
5847 | register GV *gv; | |
5848 | register SV *sv; | |
5849 | register I32 i; | |
5850 | register PMOP *pm; | |
5851 | register I32 max; | |
4802d5d7 | 5852 | char todo[PERL_UCHAR_MAX+1]; |
79072805 | 5853 | |
49d8d3a1 MB |
5854 | if (!stash) |
5855 | return; | |
5856 | ||
79072805 LW |
5857 | if (!*s) { /* reset ?? searches */ |
5858 | for (pm = HvPMROOT(stash); pm; pm = pm->op_pmnext) { | |
48c036b1 | 5859 | pm->op_pmdynflags &= ~PMdf_USED; |
79072805 LW |
5860 | } |
5861 | return; | |
5862 | } | |
5863 | ||
5864 | /* reset variables */ | |
5865 | ||
5866 | if (!HvARRAY(stash)) | |
5867 | return; | |
463ee0b2 LW |
5868 | |
5869 | Zero(todo, 256, char); | |
79072805 | 5870 | while (*s) { |
4802d5d7 | 5871 | i = (unsigned char)*s; |
79072805 LW |
5872 | if (s[1] == '-') { |
5873 | s += 2; | |
5874 | } | |
4802d5d7 | 5875 | max = (unsigned char)*s++; |
79072805 | 5876 | for ( ; i <= max; i++) { |
463ee0b2 LW |
5877 | todo[i] = 1; |
5878 | } | |
a0d0e21e | 5879 | for (i = 0; i <= (I32) HvMAX(stash); i++) { |
79072805 | 5880 | for (entry = HvARRAY(stash)[i]; |
9e35f4b3 GS |
5881 | entry; |
5882 | entry = HeNEXT(entry)) | |
5883 | { | |
1edc1566 | 5884 | if (!todo[(U8)*HeKEY(entry)]) |
463ee0b2 | 5885 | continue; |
1edc1566 | 5886 | gv = (GV*)HeVAL(entry); |
79072805 | 5887 | sv = GvSV(gv); |
9e35f4b3 GS |
5888 | if (SvTHINKFIRST(sv)) { |
5889 | if (!SvREADONLY(sv) && SvROK(sv)) | |
5890 | sv_unref(sv); | |
5891 | continue; | |
5892 | } | |
a0d0e21e | 5893 | (void)SvOK_off(sv); |
79072805 LW |
5894 | if (SvTYPE(sv) >= SVt_PV) { |
5895 | SvCUR_set(sv, 0); | |
463ee0b2 LW |
5896 | if (SvPVX(sv) != Nullch) |
5897 | *SvPVX(sv) = '\0'; | |
44a8e56a | 5898 | SvTAINT(sv); |
79072805 LW |
5899 | } |
5900 | if (GvAV(gv)) { | |
5901 | av_clear(GvAV(gv)); | |
5902 | } | |
44a8e56a | 5903 | if (GvHV(gv) && !HvNAME(GvHV(gv))) { |
463ee0b2 | 5904 | hv_clear(GvHV(gv)); |
fa6a1c44 | 5905 | #ifdef USE_ENVIRON_ARRAY |
3280af22 | 5906 | if (gv == PL_envgv) |
79072805 | 5907 | environ[0] = Nullch; |
a0d0e21e | 5908 | #endif |
79072805 LW |
5909 | } |
5910 | } | |
5911 | } | |
5912 | } | |
5913 | } | |
5914 | ||
46fc3d4c | 5915 | IO* |
864dbfa3 | 5916 | Perl_sv_2io(pTHX_ SV *sv) |
46fc3d4c | 5917 | { |
5918 | IO* io; | |
5919 | GV* gv; | |
2d8e6c8d | 5920 | STRLEN n_a; |
46fc3d4c | 5921 | |
5922 | switch (SvTYPE(sv)) { | |
5923 | case SVt_PVIO: | |
5924 | io = (IO*)sv; | |
5925 | break; | |
5926 | case SVt_PVGV: | |
5927 | gv = (GV*)sv; | |
5928 | io = GvIO(gv); | |
5929 | if (!io) | |
cea2e8a9 | 5930 | Perl_croak(aTHX_ "Bad filehandle: %s", GvNAME(gv)); |
46fc3d4c | 5931 | break; |
5932 | default: | |
5933 | if (!SvOK(sv)) | |
cea2e8a9 | 5934 | Perl_croak(aTHX_ PL_no_usym, "filehandle"); |
46fc3d4c | 5935 | if (SvROK(sv)) |
5936 | return sv_2io(SvRV(sv)); | |
2d8e6c8d | 5937 | gv = gv_fetchpv(SvPV(sv,n_a), FALSE, SVt_PVIO); |
46fc3d4c | 5938 | if (gv) |
5939 | io = GvIO(gv); | |
5940 | else | |
5941 | io = 0; | |
5942 | if (!io) | |
cea2e8a9 | 5943 | Perl_croak(aTHX_ "Bad filehandle: %s", SvPV(sv,n_a)); |
46fc3d4c | 5944 | break; |
5945 | } | |
5946 | return io; | |
5947 | } | |
5948 | ||
79072805 | 5949 | CV * |
864dbfa3 | 5950 | Perl_sv_2cv(pTHX_ SV *sv, HV **st, GV **gvp, I32 lref) |
79072805 LW |
5951 | { |
5952 | GV *gv; | |
5953 | CV *cv; | |
2d8e6c8d | 5954 | STRLEN n_a; |
79072805 LW |
5955 | |
5956 | if (!sv) | |
93a17b20 | 5957 | return *gvp = Nullgv, Nullcv; |
79072805 | 5958 | switch (SvTYPE(sv)) { |
79072805 LW |
5959 | case SVt_PVCV: |
5960 | *st = CvSTASH(sv); | |
5961 | *gvp = Nullgv; | |
5962 | return (CV*)sv; | |
5963 | case SVt_PVHV: | |
5964 | case SVt_PVAV: | |
5965 | *gvp = Nullgv; | |
5966 | return Nullcv; | |
8990e307 LW |
5967 | case SVt_PVGV: |
5968 | gv = (GV*)sv; | |
a0d0e21e | 5969 | *gvp = gv; |
8990e307 LW |
5970 | *st = GvESTASH(gv); |
5971 | goto fix_gv; | |
5972 | ||
79072805 | 5973 | default: |
a0d0e21e LW |
5974 | if (SvGMAGICAL(sv)) |
5975 | mg_get(sv); | |
5976 | if (SvROK(sv)) { | |
f5284f61 IZ |
5977 | SV **sp = &sv; /* Used in tryAMAGICunDEREF macro. */ |
5978 | tryAMAGICunDEREF(to_cv); | |
5979 | ||
62f274bf GS |
5980 | sv = SvRV(sv); |
5981 | if (SvTYPE(sv) == SVt_PVCV) { | |
5982 | cv = (CV*)sv; | |
5983 | *gvp = Nullgv; | |
5984 | *st = CvSTASH(cv); | |
5985 | return cv; | |
5986 | } | |
5987 | else if(isGV(sv)) | |
5988 | gv = (GV*)sv; | |
5989 | else | |
cea2e8a9 | 5990 | Perl_croak(aTHX_ "Not a subroutine reference"); |
a0d0e21e | 5991 | } |
62f274bf | 5992 | else if (isGV(sv)) |
79072805 LW |
5993 | gv = (GV*)sv; |
5994 | else | |
2d8e6c8d | 5995 | gv = gv_fetchpv(SvPV(sv, n_a), lref, SVt_PVCV); |
79072805 LW |
5996 | *gvp = gv; |
5997 | if (!gv) | |
5998 | return Nullcv; | |
5999 | *st = GvESTASH(gv); | |
8990e307 | 6000 | fix_gv: |
8ebc5c01 | 6001 | if (lref && !GvCVu(gv)) { |
4633a7c4 | 6002 | SV *tmpsv; |
748a9306 | 6003 | ENTER; |
4633a7c4 | 6004 | tmpsv = NEWSV(704,0); |
16660edb | 6005 | gv_efullname3(tmpsv, gv, Nullch); |
f6ec51f7 GS |
6006 | /* XXX this is probably not what they think they're getting. |
6007 | * It has the same effect as "sub name;", i.e. just a forward | |
6008 | * declaration! */ | |
774d564b | 6009 | newSUB(start_subparse(FALSE, 0), |
4633a7c4 LW |
6010 | newSVOP(OP_CONST, 0, tmpsv), |
6011 | Nullop, | |
8990e307 | 6012 | Nullop); |
748a9306 | 6013 | LEAVE; |
8ebc5c01 | 6014 | if (!GvCVu(gv)) |
cea2e8a9 | 6015 | Perl_croak(aTHX_ "Unable to create sub named \"%s\"", SvPV(sv,n_a)); |
8990e307 | 6016 | } |
8ebc5c01 | 6017 | return GvCVu(gv); |
79072805 LW |
6018 | } |
6019 | } | |
6020 | ||
c461cf8f JH |
6021 | /* |
6022 | =for apidoc sv_true | |
6023 | ||
6024 | Returns true if the SV has a true value by Perl's rules. | |
6025 | ||
6026 | =cut | |
6027 | */ | |
6028 | ||
79072805 | 6029 | I32 |
864dbfa3 | 6030 | Perl_sv_true(pTHX_ register SV *sv) |
79072805 | 6031 | { |
8990e307 LW |
6032 | if (!sv) |
6033 | return 0; | |
79072805 | 6034 | if (SvPOK(sv)) { |
4e35701f NIS |
6035 | register XPV* tXpv; |
6036 | if ((tXpv = (XPV*)SvANY(sv)) && | |
c2f1de04 | 6037 | (tXpv->xpv_cur > 1 || |
4e35701f | 6038 | (tXpv->xpv_cur && *tXpv->xpv_pv != '0'))) |
79072805 LW |
6039 | return 1; |
6040 | else | |
6041 | return 0; | |
6042 | } | |
6043 | else { | |
6044 | if (SvIOK(sv)) | |
463ee0b2 | 6045 | return SvIVX(sv) != 0; |
79072805 LW |
6046 | else { |
6047 | if (SvNOK(sv)) | |
463ee0b2 | 6048 | return SvNVX(sv) != 0.0; |
79072805 | 6049 | else |
463ee0b2 | 6050 | return sv_2bool(sv); |
79072805 LW |
6051 | } |
6052 | } | |
6053 | } | |
79072805 | 6054 | |
ff68c719 | 6055 | IV |
864dbfa3 | 6056 | Perl_sv_iv(pTHX_ register SV *sv) |
85e6fe83 | 6057 | { |
25da4f38 IZ |
6058 | if (SvIOK(sv)) { |
6059 | if (SvIsUV(sv)) | |
6060 | return (IV)SvUVX(sv); | |
ff68c719 | 6061 | return SvIVX(sv); |
25da4f38 | 6062 | } |
ff68c719 | 6063 | return sv_2iv(sv); |
85e6fe83 | 6064 | } |
85e6fe83 | 6065 | |
ff68c719 | 6066 | UV |
864dbfa3 | 6067 | Perl_sv_uv(pTHX_ register SV *sv) |
ff68c719 | 6068 | { |
25da4f38 IZ |
6069 | if (SvIOK(sv)) { |
6070 | if (SvIsUV(sv)) | |
6071 | return SvUVX(sv); | |
6072 | return (UV)SvIVX(sv); | |
6073 | } | |
ff68c719 | 6074 | return sv_2uv(sv); |
6075 | } | |
85e6fe83 | 6076 | |
65202027 | 6077 | NV |
864dbfa3 | 6078 | Perl_sv_nv(pTHX_ register SV *sv) |
79072805 | 6079 | { |
ff68c719 | 6080 | if (SvNOK(sv)) |
6081 | return SvNVX(sv); | |
6082 | return sv_2nv(sv); | |
79072805 | 6083 | } |
79072805 | 6084 | |
79072805 | 6085 | char * |
864dbfa3 | 6086 | Perl_sv_pv(pTHX_ SV *sv) |
1fa8b10d JD |
6087 | { |
6088 | STRLEN n_a; | |
6089 | ||
6090 | if (SvPOK(sv)) | |
6091 | return SvPVX(sv); | |
6092 | ||
6093 | return sv_2pv(sv, &n_a); | |
6094 | } | |
6095 | ||
6096 | char * | |
864dbfa3 | 6097 | Perl_sv_pvn(pTHX_ SV *sv, STRLEN *lp) |
79072805 | 6098 | { |
85e6fe83 LW |
6099 | if (SvPOK(sv)) { |
6100 | *lp = SvCUR(sv); | |
a0d0e21e | 6101 | return SvPVX(sv); |
85e6fe83 | 6102 | } |
463ee0b2 | 6103 | return sv_2pv(sv, lp); |
79072805 | 6104 | } |
79072805 | 6105 | |
c461cf8f JH |
6106 | /* |
6107 | =for apidoc sv_pvn_force | |
6108 | ||
6109 | Get a sensible string out of the SV somehow. | |
6110 | ||
6111 | =cut | |
6112 | */ | |
6113 | ||
a0d0e21e | 6114 | char * |
864dbfa3 | 6115 | Perl_sv_pvn_force(pTHX_ SV *sv, STRLEN *lp) |
a0d0e21e LW |
6116 | { |
6117 | char *s; | |
6118 | ||
6fc92669 GS |
6119 | if (SvTHINKFIRST(sv) && !SvROK(sv)) |
6120 | sv_force_normal(sv); | |
1c846c1f | 6121 | |
a0d0e21e LW |
6122 | if (SvPOK(sv)) { |
6123 | *lp = SvCUR(sv); | |
6124 | } | |
6125 | else { | |
748a9306 | 6126 | if (SvTYPE(sv) > SVt_PVLV && SvTYPE(sv) != SVt_PVFM) { |
cea2e8a9 | 6127 | Perl_croak(aTHX_ "Can't coerce %s to string in %s", sv_reftype(sv,0), |
6fc92669 | 6128 | PL_op_name[PL_op->op_type]); |
a0d0e21e | 6129 | } |
4633a7c4 LW |
6130 | else |
6131 | s = sv_2pv(sv, lp); | |
a0d0e21e LW |
6132 | if (s != SvPVX(sv)) { /* Almost, but not quite, sv_setpvn() */ |
6133 | STRLEN len = *lp; | |
1c846c1f | 6134 | |
a0d0e21e LW |
6135 | if (SvROK(sv)) |
6136 | sv_unref(sv); | |
6137 | (void)SvUPGRADE(sv, SVt_PV); /* Never FALSE */ | |
6138 | SvGROW(sv, len + 1); | |
6139 | Move(s,SvPVX(sv),len,char); | |
6140 | SvCUR_set(sv, len); | |
6141 | *SvEND(sv) = '\0'; | |
6142 | } | |
6143 | if (!SvPOK(sv)) { | |
6144 | SvPOK_on(sv); /* validate pointer */ | |
6145 | SvTAINT(sv); | |
1d7c1841 GS |
6146 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n", |
6147 | PTR2UV(sv),SvPVX(sv))); | |
a0d0e21e LW |
6148 | } |
6149 | } | |
6150 | return SvPVX(sv); | |
6151 | } | |
6152 | ||
6153 | char * | |
7340a771 GS |
6154 | Perl_sv_pvbyte(pTHX_ SV *sv) |
6155 | { | |
6156 | return sv_pv(sv); | |
6157 | } | |
6158 | ||
6159 | char * | |
6160 | Perl_sv_pvbyten(pTHX_ SV *sv, STRLEN *lp) | |
6161 | { | |
6162 | return sv_pvn(sv,lp); | |
6163 | } | |
6164 | ||
6165 | char * | |
6166 | Perl_sv_pvbyten_force(pTHX_ SV *sv, STRLEN *lp) | |
6167 | { | |
6168 | return sv_pvn_force(sv,lp); | |
6169 | } | |
6170 | ||
6171 | char * | |
6172 | Perl_sv_pvutf8(pTHX_ SV *sv) | |
6173 | { | |
560a288e | 6174 | sv_utf8_upgrade(sv); |
7340a771 GS |
6175 | return sv_pv(sv); |
6176 | } | |
6177 | ||
6178 | char * | |
6179 | Perl_sv_pvutf8n(pTHX_ SV *sv, STRLEN *lp) | |
6180 | { | |
560a288e | 6181 | sv_utf8_upgrade(sv); |
7340a771 GS |
6182 | return sv_pvn(sv,lp); |
6183 | } | |
6184 | ||
c461cf8f JH |
6185 | /* |
6186 | =for apidoc sv_pvutf8n_force | |
6187 | ||
6188 | Get a sensible UTF8-encoded string out of the SV somehow. See | |
6189 | L</sv_pvn_force>. | |
6190 | ||
6191 | =cut | |
6192 | */ | |
6193 | ||
7340a771 GS |
6194 | char * |
6195 | Perl_sv_pvutf8n_force(pTHX_ SV *sv, STRLEN *lp) | |
6196 | { | |
560a288e | 6197 | sv_utf8_upgrade(sv); |
7340a771 GS |
6198 | return sv_pvn_force(sv,lp); |
6199 | } | |
6200 | ||
c461cf8f JH |
6201 | /* |
6202 | =for apidoc sv_reftype | |
6203 | ||
6204 | Returns a string describing what the SV is a reference to. | |
6205 | ||
6206 | =cut | |
6207 | */ | |
6208 | ||
7340a771 | 6209 | char * |
864dbfa3 | 6210 | Perl_sv_reftype(pTHX_ SV *sv, int ob) |
a0d0e21e LW |
6211 | { |
6212 | if (ob && SvOBJECT(sv)) | |
6213 | return HvNAME(SvSTASH(sv)); | |
6214 | else { | |
6215 | switch (SvTYPE(sv)) { | |
6216 | case SVt_NULL: | |
6217 | case SVt_IV: | |
6218 | case SVt_NV: | |
6219 | case SVt_RV: | |
6220 | case SVt_PV: | |
6221 | case SVt_PVIV: | |
6222 | case SVt_PVNV: | |
6223 | case SVt_PVMG: | |
6224 | case SVt_PVBM: | |
6225 | if (SvROK(sv)) | |
6226 | return "REF"; | |
6227 | else | |
6228 | return "SCALAR"; | |
6229 | case SVt_PVLV: return "LVALUE"; | |
6230 | case SVt_PVAV: return "ARRAY"; | |
6231 | case SVt_PVHV: return "HASH"; | |
6232 | case SVt_PVCV: return "CODE"; | |
6233 | case SVt_PVGV: return "GLOB"; | |
1d2dff63 | 6234 | case SVt_PVFM: return "FORMAT"; |
27f9d8f3 | 6235 | case SVt_PVIO: return "IO"; |
a0d0e21e LW |
6236 | default: return "UNKNOWN"; |
6237 | } | |
6238 | } | |
6239 | } | |
6240 | ||
954c1994 GS |
6241 | /* |
6242 | =for apidoc sv_isobject | |
6243 | ||
6244 | Returns a boolean indicating whether the SV is an RV pointing to a blessed | |
6245 | object. If the SV is not an RV, or if the object is not blessed, then this | |
6246 | will return false. | |
6247 | ||
6248 | =cut | |
6249 | */ | |
6250 | ||
463ee0b2 | 6251 | int |
864dbfa3 | 6252 | Perl_sv_isobject(pTHX_ SV *sv) |
85e6fe83 | 6253 | { |
68dc0745 | 6254 | if (!sv) |
6255 | return 0; | |
6256 | if (SvGMAGICAL(sv)) | |
6257 | mg_get(sv); | |
85e6fe83 LW |
6258 | if (!SvROK(sv)) |
6259 | return 0; | |
6260 | sv = (SV*)SvRV(sv); | |
6261 | if (!SvOBJECT(sv)) | |
6262 | return 0; | |
6263 | return 1; | |
6264 | } | |
6265 | ||
954c1994 GS |
6266 | /* |
6267 | =for apidoc sv_isa | |
6268 | ||
6269 | Returns a boolean indicating whether the SV is blessed into the specified | |
6270 | class. This does not check for subtypes; use C<sv_derived_from> to verify | |
6271 | an inheritance relationship. | |
6272 | ||
6273 | =cut | |
6274 | */ | |
6275 | ||
85e6fe83 | 6276 | int |
864dbfa3 | 6277 | Perl_sv_isa(pTHX_ SV *sv, const char *name) |
463ee0b2 | 6278 | { |
68dc0745 | 6279 | if (!sv) |
6280 | return 0; | |
6281 | if (SvGMAGICAL(sv)) | |
6282 | mg_get(sv); | |
ed6116ce | 6283 | if (!SvROK(sv)) |
463ee0b2 | 6284 | return 0; |
ed6116ce LW |
6285 | sv = (SV*)SvRV(sv); |
6286 | if (!SvOBJECT(sv)) | |
463ee0b2 LW |
6287 | return 0; |
6288 | ||
6289 | return strEQ(HvNAME(SvSTASH(sv)), name); | |
6290 | } | |
6291 | ||
954c1994 GS |
6292 | /* |
6293 | =for apidoc newSVrv | |
6294 | ||
6295 | Creates a new SV for the RV, C<rv>, to point to. If C<rv> is not an RV then | |
6296 | it will be upgraded to one. If C<classname> is non-null then the new SV will | |
6297 | be blessed in the specified package. The new SV is returned and its | |
6298 | reference count is 1. | |
6299 | ||
6300 | =cut | |
6301 | */ | |
6302 | ||
463ee0b2 | 6303 | SV* |
864dbfa3 | 6304 | Perl_newSVrv(pTHX_ SV *rv, const char *classname) |
463ee0b2 | 6305 | { |
463ee0b2 LW |
6306 | SV *sv; |
6307 | ||
4561caa4 | 6308 | new_SV(sv); |
51cf62d8 | 6309 | |
2213622d | 6310 | SV_CHECK_THINKFIRST(rv); |
51cf62d8 | 6311 | SvAMAGIC_off(rv); |
51cf62d8 | 6312 | |
0199fce9 JD |
6313 | if (SvTYPE(rv) >= SVt_PVMG) { |
6314 | U32 refcnt = SvREFCNT(rv); | |
6315 | SvREFCNT(rv) = 0; | |
6316 | sv_clear(rv); | |
6317 | SvFLAGS(rv) = 0; | |
6318 | SvREFCNT(rv) = refcnt; | |
6319 | } | |
6320 | ||
51cf62d8 | 6321 | if (SvTYPE(rv) < SVt_RV) |
0199fce9 JD |
6322 | sv_upgrade(rv, SVt_RV); |
6323 | else if (SvTYPE(rv) > SVt_RV) { | |
6324 | (void)SvOOK_off(rv); | |
6325 | if (SvPVX(rv) && SvLEN(rv)) | |
6326 | Safefree(SvPVX(rv)); | |
6327 | SvCUR_set(rv, 0); | |
6328 | SvLEN_set(rv, 0); | |
6329 | } | |
51cf62d8 OT |
6330 | |
6331 | (void)SvOK_off(rv); | |
053fc874 | 6332 | SvRV(rv) = sv; |
ed6116ce | 6333 | SvROK_on(rv); |
463ee0b2 | 6334 | |
a0d0e21e LW |
6335 | if (classname) { |
6336 | HV* stash = gv_stashpv(classname, TRUE); | |
6337 | (void)sv_bless(rv, stash); | |
6338 | } | |
6339 | return sv; | |
6340 | } | |
6341 | ||
954c1994 GS |
6342 | /* |
6343 | =for apidoc sv_setref_pv | |
6344 | ||
6345 | Copies a pointer into a new SV, optionally blessing the SV. The C<rv> | |
6346 | argument will be upgraded to an RV. That RV will be modified to point to | |
6347 | the new SV. If the C<pv> argument is NULL then C<PL_sv_undef> will be placed | |
6348 | into the SV. The C<classname> argument indicates the package for the | |
6349 | blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV | |
6350 | will be returned and will have a reference count of 1. | |
6351 | ||
6352 | Do not use with other Perl types such as HV, AV, SV, CV, because those | |
6353 | objects will become corrupted by the pointer copy process. | |
6354 | ||
6355 | Note that C<sv_setref_pvn> copies the string while this copies the pointer. | |
6356 | ||
6357 | =cut | |
6358 | */ | |
6359 | ||
a0d0e21e | 6360 | SV* |
864dbfa3 | 6361 | Perl_sv_setref_pv(pTHX_ SV *rv, const char *classname, void *pv) |
a0d0e21e | 6362 | { |
189b2af5 | 6363 | if (!pv) { |
3280af22 | 6364 | sv_setsv(rv, &PL_sv_undef); |
189b2af5 GS |
6365 | SvSETMAGIC(rv); |
6366 | } | |
a0d0e21e | 6367 | else |
56431972 | 6368 | sv_setiv(newSVrv(rv,classname), PTR2IV(pv)); |
a0d0e21e LW |
6369 | return rv; |
6370 | } | |
6371 | ||
954c1994 GS |
6372 | /* |
6373 | =for apidoc sv_setref_iv | |
6374 | ||
6375 | Copies an integer into a new SV, optionally blessing the SV. The C<rv> | |
6376 | argument will be upgraded to an RV. That RV will be modified to point to | |
6377 | the new SV. The C<classname> argument indicates the package for the | |
6378 | blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV | |
6379 | will be returned and will have a reference count of 1. | |
6380 | ||
6381 | =cut | |
6382 | */ | |
6383 | ||
a0d0e21e | 6384 | SV* |
864dbfa3 | 6385 | Perl_sv_setref_iv(pTHX_ SV *rv, const char *classname, IV iv) |
a0d0e21e LW |
6386 | { |
6387 | sv_setiv(newSVrv(rv,classname), iv); | |
6388 | return rv; | |
6389 | } | |
6390 | ||
954c1994 | 6391 | /* |
e1c57cef JH |
6392 | =for apidoc sv_setref_uv |
6393 | ||
6394 | Copies an unsigned integer into a new SV, optionally blessing the SV. The C<rv> | |
6395 | argument will be upgraded to an RV. That RV will be modified to point to | |
6396 | the new SV. The C<classname> argument indicates the package for the | |
6397 | blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV | |
6398 | will be returned and will have a reference count of 1. | |
6399 | ||
6400 | =cut | |
6401 | */ | |
6402 | ||
6403 | SV* | |
6404 | Perl_sv_setref_uv(pTHX_ SV *rv, const char *classname, UV uv) | |
6405 | { | |
6406 | sv_setuv(newSVrv(rv,classname), uv); | |
6407 | return rv; | |
6408 | } | |
6409 | ||
6410 | /* | |
954c1994 GS |
6411 | =for apidoc sv_setref_nv |
6412 | ||
6413 | Copies a double into a new SV, optionally blessing the SV. The C<rv> | |
6414 | argument will be upgraded to an RV. That RV will be modified to point to | |
6415 | the new SV. The C<classname> argument indicates the package for the | |
6416 | blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV | |
6417 | will be returned and will have a reference count of 1. | |
6418 | ||
6419 | =cut | |
6420 | */ | |
6421 | ||
a0d0e21e | 6422 | SV* |
65202027 | 6423 | Perl_sv_setref_nv(pTHX_ SV *rv, const char *classname, NV nv) |
a0d0e21e LW |
6424 | { |
6425 | sv_setnv(newSVrv(rv,classname), nv); | |
6426 | return rv; | |
6427 | } | |
463ee0b2 | 6428 | |
954c1994 GS |
6429 | /* |
6430 | =for apidoc sv_setref_pvn | |
6431 | ||
6432 | Copies a string into a new SV, optionally blessing the SV. The length of the | |
6433 | string must be specified with C<n>. The C<rv> argument will be upgraded to | |
6434 | an RV. That RV will be modified to point to the new SV. The C<classname> | |
6435 | argument indicates the package for the blessing. Set C<classname> to | |
6436 | C<Nullch> to avoid the blessing. The new SV will be returned and will have | |
6437 | a reference count of 1. | |
6438 | ||
6439 | Note that C<sv_setref_pv> copies the pointer while this copies the string. | |
6440 | ||
6441 | =cut | |
6442 | */ | |
6443 | ||
a0d0e21e | 6444 | SV* |
864dbfa3 | 6445 | Perl_sv_setref_pvn(pTHX_ SV *rv, const char *classname, char *pv, STRLEN n) |
a0d0e21e LW |
6446 | { |
6447 | sv_setpvn(newSVrv(rv,classname), pv, n); | |
463ee0b2 LW |
6448 | return rv; |
6449 | } | |
6450 | ||
954c1994 GS |
6451 | /* |
6452 | =for apidoc sv_bless | |
6453 | ||
6454 | Blesses an SV into a specified package. The SV must be an RV. The package | |
6455 | must be designated by its stash (see C<gv_stashpv()>). The reference count | |
6456 | of the SV is unaffected. | |
6457 | ||
6458 | =cut | |
6459 | */ | |
6460 | ||
a0d0e21e | 6461 | SV* |
864dbfa3 | 6462 | Perl_sv_bless(pTHX_ SV *sv, HV *stash) |
a0d0e21e | 6463 | { |
76e3520e | 6464 | SV *tmpRef; |
a0d0e21e | 6465 | if (!SvROK(sv)) |
cea2e8a9 | 6466 | Perl_croak(aTHX_ "Can't bless non-reference value"); |
76e3520e GS |
6467 | tmpRef = SvRV(sv); |
6468 | if (SvFLAGS(tmpRef) & (SVs_OBJECT|SVf_READONLY)) { | |
6469 | if (SvREADONLY(tmpRef)) | |
cea2e8a9 | 6470 | Perl_croak(aTHX_ PL_no_modify); |
76e3520e GS |
6471 | if (SvOBJECT(tmpRef)) { |
6472 | if (SvTYPE(tmpRef) != SVt_PVIO) | |
3280af22 | 6473 | --PL_sv_objcount; |
76e3520e | 6474 | SvREFCNT_dec(SvSTASH(tmpRef)); |
2e3febc6 | 6475 | } |
a0d0e21e | 6476 | } |
76e3520e GS |
6477 | SvOBJECT_on(tmpRef); |
6478 | if (SvTYPE(tmpRef) != SVt_PVIO) | |
3280af22 | 6479 | ++PL_sv_objcount; |
76e3520e GS |
6480 | (void)SvUPGRADE(tmpRef, SVt_PVMG); |
6481 | SvSTASH(tmpRef) = (HV*)SvREFCNT_inc(stash); | |
a0d0e21e | 6482 | |
2e3febc6 CS |
6483 | if (Gv_AMG(stash)) |
6484 | SvAMAGIC_on(sv); | |
6485 | else | |
6486 | SvAMAGIC_off(sv); | |
a0d0e21e LW |
6487 | |
6488 | return sv; | |
6489 | } | |
6490 | ||
76e3520e | 6491 | STATIC void |
cea2e8a9 | 6492 | S_sv_unglob(pTHX_ SV *sv) |
a0d0e21e | 6493 | { |
850fabdf GS |
6494 | void *xpvmg; |
6495 | ||
a0d0e21e LW |
6496 | assert(SvTYPE(sv) == SVt_PVGV); |
6497 | SvFAKE_off(sv); | |
6498 | if (GvGP(sv)) | |
1edc1566 | 6499 | gp_free((GV*)sv); |
e826b3c7 GS |
6500 | if (GvSTASH(sv)) { |
6501 | SvREFCNT_dec(GvSTASH(sv)); | |
6502 | GvSTASH(sv) = Nullhv; | |
6503 | } | |
a0d0e21e LW |
6504 | sv_unmagic(sv, '*'); |
6505 | Safefree(GvNAME(sv)); | |
a5f75d66 | 6506 | GvMULTI_off(sv); |
850fabdf GS |
6507 | |
6508 | /* need to keep SvANY(sv) in the right arena */ | |
6509 | xpvmg = new_XPVMG(); | |
6510 | StructCopy(SvANY(sv), xpvmg, XPVMG); | |
6511 | del_XPVGV(SvANY(sv)); | |
6512 | SvANY(sv) = xpvmg; | |
6513 | ||
a0d0e21e LW |
6514 | SvFLAGS(sv) &= ~SVTYPEMASK; |
6515 | SvFLAGS(sv) |= SVt_PVMG; | |
6516 | } | |
6517 | ||
954c1994 | 6518 | /* |
840a7b70 | 6519 | =for apidoc sv_unref_flags |
954c1994 GS |
6520 | |
6521 | Unsets the RV status of the SV, and decrements the reference count of | |
6522 | whatever was being referenced by the RV. This can almost be thought of | |
840a7b70 IZ |
6523 | as a reversal of C<newSVrv>. The C<cflags> argument can contain |
6524 | C<SV_IMMEDIATE_UNREF> to force the reference count to be decremented | |
6525 | (otherwise the decrementing is conditional on the reference count being | |
6526 | different from one or the reference being a readonly SV). | |
7889fe52 | 6527 | See C<SvROK_off>. |
954c1994 GS |
6528 | |
6529 | =cut | |
6530 | */ | |
6531 | ||
ed6116ce | 6532 | void |
840a7b70 | 6533 | Perl_sv_unref_flags(pTHX_ SV *sv, U32 flags) |
ed6116ce | 6534 | { |
a0d0e21e | 6535 | SV* rv = SvRV(sv); |
810b8aa5 GS |
6536 | |
6537 | if (SvWEAKREF(sv)) { | |
6538 | sv_del_backref(sv); | |
6539 | SvWEAKREF_off(sv); | |
6540 | SvRV(sv) = 0; | |
6541 | return; | |
6542 | } | |
ed6116ce LW |
6543 | SvRV(sv) = 0; |
6544 | SvROK_off(sv); | |
840a7b70 | 6545 | if (SvREFCNT(rv) != 1 || SvREADONLY(rv) || flags) /* SV_IMMEDIATE_UNREF */ |
4633a7c4 | 6546 | SvREFCNT_dec(rv); |
840a7b70 | 6547 | else /* XXX Hack, but hard to make $a=$a->[1] work otherwise */ |
4633a7c4 | 6548 | sv_2mortal(rv); /* Schedule for freeing later */ |
ed6116ce | 6549 | } |
8990e307 | 6550 | |
840a7b70 IZ |
6551 | /* |
6552 | =for apidoc sv_unref | |
6553 | ||
6554 | Unsets the RV status of the SV, and decrements the reference count of | |
6555 | whatever was being referenced by the RV. This can almost be thought of | |
6556 | as a reversal of C<newSVrv>. This is C<sv_unref_flags> with the C<flag> | |
7889fe52 | 6557 | being zero. See C<SvROK_off>. |
840a7b70 IZ |
6558 | |
6559 | =cut | |
6560 | */ | |
6561 | ||
6562 | void | |
6563 | Perl_sv_unref(pTHX_ SV *sv) | |
6564 | { | |
6565 | sv_unref_flags(sv, 0); | |
6566 | } | |
6567 | ||
bbce6d69 | 6568 | void |
864dbfa3 | 6569 | Perl_sv_taint(pTHX_ SV *sv) |
bbce6d69 | 6570 | { |
6571 | sv_magic((sv), Nullsv, 't', Nullch, 0); | |
6572 | } | |
6573 | ||
6574 | void | |
864dbfa3 | 6575 | Perl_sv_untaint(pTHX_ SV *sv) |
bbce6d69 | 6576 | { |
13f57bf8 | 6577 | if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) { |
36477c24 | 6578 | MAGIC *mg = mg_find(sv, 't'); |
6579 | if (mg) | |
565764a8 | 6580 | mg->mg_len &= ~1; |
36477c24 | 6581 | } |
bbce6d69 | 6582 | } |
6583 | ||
6584 | bool | |
864dbfa3 | 6585 | Perl_sv_tainted(pTHX_ SV *sv) |
bbce6d69 | 6586 | { |
13f57bf8 | 6587 | if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) { |
36477c24 | 6588 | MAGIC *mg = mg_find(sv, 't'); |
155aba94 | 6589 | if (mg && ((mg->mg_len & 1) || ((mg->mg_len & 2) && mg->mg_obj == sv))) |
36477c24 | 6590 | return TRUE; |
6591 | } | |
6592 | return FALSE; | |
bbce6d69 | 6593 | } |
6594 | ||
954c1994 GS |
6595 | /* |
6596 | =for apidoc sv_setpviv | |
6597 | ||
6598 | Copies an integer into the given SV, also updating its string value. | |
6599 | Does not handle 'set' magic. See C<sv_setpviv_mg>. | |
6600 | ||
6601 | =cut | |
6602 | */ | |
6603 | ||
84902520 | 6604 | void |
864dbfa3 | 6605 | Perl_sv_setpviv(pTHX_ SV *sv, IV iv) |
84902520 | 6606 | { |
25da4f38 IZ |
6607 | char buf[TYPE_CHARS(UV)]; |
6608 | char *ebuf; | |
6609 | char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf); | |
84902520 | 6610 | |
25da4f38 | 6611 | sv_setpvn(sv, ptr, ebuf - ptr); |
84902520 TB |
6612 | } |
6613 | ||
ef50df4b | 6614 | |
954c1994 GS |
6615 | /* |
6616 | =for apidoc sv_setpviv_mg | |
6617 | ||
6618 | Like C<sv_setpviv>, but also handles 'set' magic. | |
6619 | ||
6620 | =cut | |
6621 | */ | |
6622 | ||
ef50df4b | 6623 | void |
864dbfa3 | 6624 | Perl_sv_setpviv_mg(pTHX_ SV *sv, IV iv) |
ef50df4b | 6625 | { |
25da4f38 IZ |
6626 | char buf[TYPE_CHARS(UV)]; |
6627 | char *ebuf; | |
6628 | char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf); | |
6629 | ||
6630 | sv_setpvn(sv, ptr, ebuf - ptr); | |
ef50df4b GS |
6631 | SvSETMAGIC(sv); |
6632 | } | |
6633 | ||
cea2e8a9 GS |
6634 | #if defined(PERL_IMPLICIT_CONTEXT) |
6635 | void | |
6636 | Perl_sv_setpvf_nocontext(SV *sv, const char* pat, ...) | |
6637 | { | |
6638 | dTHX; | |
6639 | va_list args; | |
6640 | va_start(args, pat); | |
c5be433b | 6641 | sv_vsetpvf(sv, pat, &args); |
cea2e8a9 GS |
6642 | va_end(args); |
6643 | } | |
6644 | ||
6645 | ||
6646 | void | |
6647 | Perl_sv_setpvf_mg_nocontext(SV *sv, const char* pat, ...) | |
6648 | { | |
6649 | dTHX; | |
6650 | va_list args; | |
6651 | va_start(args, pat); | |
c5be433b | 6652 | sv_vsetpvf_mg(sv, pat, &args); |
cea2e8a9 | 6653 | va_end(args); |
cea2e8a9 GS |
6654 | } |
6655 | #endif | |
6656 | ||
954c1994 GS |
6657 | /* |
6658 | =for apidoc sv_setpvf | |
6659 | ||
6660 | Processes its arguments like C<sprintf> and sets an SV to the formatted | |
6661 | output. Does not handle 'set' magic. See C<sv_setpvf_mg>. | |
6662 | ||
6663 | =cut | |
6664 | */ | |
6665 | ||
46fc3d4c | 6666 | void |
864dbfa3 | 6667 | Perl_sv_setpvf(pTHX_ SV *sv, const char* pat, ...) |
46fc3d4c | 6668 | { |
6669 | va_list args; | |
46fc3d4c | 6670 | va_start(args, pat); |
c5be433b | 6671 | sv_vsetpvf(sv, pat, &args); |
46fc3d4c | 6672 | va_end(args); |
6673 | } | |
6674 | ||
c5be433b GS |
6675 | void |
6676 | Perl_sv_vsetpvf(pTHX_ SV *sv, const char* pat, va_list* args) | |
6677 | { | |
6678 | sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
6679 | } | |
ef50df4b | 6680 | |
954c1994 GS |
6681 | /* |
6682 | =for apidoc sv_setpvf_mg | |
6683 | ||
6684 | Like C<sv_setpvf>, but also handles 'set' magic. | |
6685 | ||
6686 | =cut | |
6687 | */ | |
6688 | ||
ef50df4b | 6689 | void |
864dbfa3 | 6690 | Perl_sv_setpvf_mg(pTHX_ SV *sv, const char* pat, ...) |
ef50df4b GS |
6691 | { |
6692 | va_list args; | |
ef50df4b | 6693 | va_start(args, pat); |
c5be433b | 6694 | sv_vsetpvf_mg(sv, pat, &args); |
ef50df4b | 6695 | va_end(args); |
c5be433b GS |
6696 | } |
6697 | ||
6698 | void | |
6699 | Perl_sv_vsetpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args) | |
6700 | { | |
6701 | sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
ef50df4b GS |
6702 | SvSETMAGIC(sv); |
6703 | } | |
6704 | ||
cea2e8a9 GS |
6705 | #if defined(PERL_IMPLICIT_CONTEXT) |
6706 | void | |
6707 | Perl_sv_catpvf_nocontext(SV *sv, const char* pat, ...) | |
6708 | { | |
6709 | dTHX; | |
6710 | va_list args; | |
6711 | va_start(args, pat); | |
c5be433b | 6712 | sv_vcatpvf(sv, pat, &args); |
cea2e8a9 GS |
6713 | va_end(args); |
6714 | } | |
6715 | ||
6716 | void | |
6717 | Perl_sv_catpvf_mg_nocontext(SV *sv, const char* pat, ...) | |
6718 | { | |
6719 | dTHX; | |
6720 | va_list args; | |
6721 | va_start(args, pat); | |
c5be433b | 6722 | sv_vcatpvf_mg(sv, pat, &args); |
cea2e8a9 | 6723 | va_end(args); |
cea2e8a9 GS |
6724 | } |
6725 | #endif | |
6726 | ||
954c1994 GS |
6727 | /* |
6728 | =for apidoc sv_catpvf | |
6729 | ||
6730 | Processes its arguments like C<sprintf> and appends the formatted output | |
6731 | to an SV. Handles 'get' magic, but not 'set' magic. C<SvSETMAGIC()> must | |
6732 | typically be called after calling this function to handle 'set' magic. | |
6733 | ||
6734 | =cut | |
6735 | */ | |
6736 | ||
46fc3d4c | 6737 | void |
864dbfa3 | 6738 | Perl_sv_catpvf(pTHX_ SV *sv, const char* pat, ...) |
46fc3d4c | 6739 | { |
6740 | va_list args; | |
46fc3d4c | 6741 | va_start(args, pat); |
c5be433b | 6742 | sv_vcatpvf(sv, pat, &args); |
46fc3d4c | 6743 | va_end(args); |
6744 | } | |
6745 | ||
ef50df4b | 6746 | void |
c5be433b GS |
6747 | Perl_sv_vcatpvf(pTHX_ SV *sv, const char* pat, va_list* args) |
6748 | { | |
6749 | sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
6750 | } | |
6751 | ||
954c1994 GS |
6752 | /* |
6753 | =for apidoc sv_catpvf_mg | |
6754 | ||
6755 | Like C<sv_catpvf>, but also handles 'set' magic. | |
6756 | ||
6757 | =cut | |
6758 | */ | |
6759 | ||
c5be433b | 6760 | void |
864dbfa3 | 6761 | Perl_sv_catpvf_mg(pTHX_ SV *sv, const char* pat, ...) |
ef50df4b GS |
6762 | { |
6763 | va_list args; | |
ef50df4b | 6764 | va_start(args, pat); |
c5be433b | 6765 | sv_vcatpvf_mg(sv, pat, &args); |
ef50df4b | 6766 | va_end(args); |
c5be433b GS |
6767 | } |
6768 | ||
6769 | void | |
6770 | Perl_sv_vcatpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args) | |
6771 | { | |
6772 | sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
ef50df4b GS |
6773 | SvSETMAGIC(sv); |
6774 | } | |
6775 | ||
954c1994 GS |
6776 | /* |
6777 | =for apidoc sv_vsetpvfn | |
6778 | ||
6779 | Works like C<vcatpvfn> but copies the text into the SV instead of | |
6780 | appending it. | |
6781 | ||
6782 | =cut | |
6783 | */ | |
6784 | ||
46fc3d4c | 6785 | void |
7d5ea4e7 | 6786 | Perl_sv_vsetpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted) |
46fc3d4c | 6787 | { |
6788 | sv_setpvn(sv, "", 0); | |
7d5ea4e7 | 6789 | sv_vcatpvfn(sv, pat, patlen, args, svargs, svmax, maybe_tainted); |
46fc3d4c | 6790 | } |
6791 | ||
2d00ba3b | 6792 | STATIC I32 |
9dd79c3f | 6793 | S_expect_number(pTHX_ char** pattern) |
211dfcf1 HS |
6794 | { |
6795 | I32 var = 0; | |
6796 | switch (**pattern) { | |
6797 | case '1': case '2': case '3': | |
6798 | case '4': case '5': case '6': | |
6799 | case '7': case '8': case '9': | |
6800 | while (isDIGIT(**pattern)) | |
6801 | var = var * 10 + (*(*pattern)++ - '0'); | |
6802 | } | |
6803 | return var; | |
6804 | } | |
9dd79c3f | 6805 | #define EXPECT_NUMBER(pattern, var) (var = S_expect_number(aTHX_ &pattern)) |
211dfcf1 | 6806 | |
954c1994 GS |
6807 | /* |
6808 | =for apidoc sv_vcatpvfn | |
6809 | ||
6810 | Processes its arguments like C<vsprintf> and appends the formatted output | |
6811 | to an SV. Uses an array of SVs if the C style variable argument list is | |
6812 | missing (NULL). When running with taint checks enabled, indicates via | |
6813 | C<maybe_tainted> if results are untrustworthy (often due to the use of | |
6814 | locales). | |
6815 | ||
6816 | =cut | |
6817 | */ | |
6818 | ||
46fc3d4c | 6819 | void |
7d5ea4e7 | 6820 | Perl_sv_vcatpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted) |
46fc3d4c | 6821 | { |
6822 | char *p; | |
6823 | char *q; | |
6824 | char *patend; | |
fc36a67e | 6825 | STRLEN origlen; |
46fc3d4c | 6826 | I32 svix = 0; |
c635e13b | 6827 | static char nullstr[] = "(null)"; |
7e2040f0 | 6828 | SV *argsv; |
46fc3d4c | 6829 | |
6830 | /* no matter what, this is a string now */ | |
fc36a67e | 6831 | (void)SvPV_force(sv, origlen); |
46fc3d4c | 6832 | |
fc36a67e | 6833 | /* special-case "", "%s", and "%_" */ |
46fc3d4c | 6834 | if (patlen == 0) |
6835 | return; | |
fc36a67e | 6836 | if (patlen == 2 && pat[0] == '%') { |
6837 | switch (pat[1]) { | |
6838 | case 's': | |
c635e13b | 6839 | if (args) { |
6840 | char *s = va_arg(*args, char*); | |
6841 | sv_catpv(sv, s ? s : nullstr); | |
6842 | } | |
7e2040f0 | 6843 | else if (svix < svmax) { |
fc36a67e | 6844 | sv_catsv(sv, *svargs); |
7e2040f0 GS |
6845 | if (DO_UTF8(*svargs)) |
6846 | SvUTF8_on(sv); | |
6847 | } | |
fc36a67e | 6848 | return; |
6849 | case '_': | |
6850 | if (args) { | |
7e2040f0 GS |
6851 | argsv = va_arg(*args, SV*); |
6852 | sv_catsv(sv, argsv); | |
6853 | if (DO_UTF8(argsv)) | |
6854 | SvUTF8_on(sv); | |
fc36a67e | 6855 | return; |
6856 | } | |
6857 | /* See comment on '_' below */ | |
6858 | break; | |
6859 | } | |
46fc3d4c | 6860 | } |
6861 | ||
6862 | patend = (char*)pat + patlen; | |
6863 | for (p = (char*)pat; p < patend; p = q) { | |
6864 | bool alt = FALSE; | |
6865 | bool left = FALSE; | |
b22c7a20 | 6866 | bool vectorize = FALSE; |
211dfcf1 | 6867 | bool vectorarg = FALSE; |
b2e23cf9 | 6868 | bool vec_utf = FALSE; |
46fc3d4c | 6869 | char fill = ' '; |
6870 | char plus = 0; | |
6871 | char intsize = 0; | |
6872 | STRLEN width = 0; | |
fc36a67e | 6873 | STRLEN zeros = 0; |
46fc3d4c | 6874 | bool has_precis = FALSE; |
6875 | STRLEN precis = 0; | |
7e2040f0 | 6876 | bool is_utf = FALSE; |
eb3fce90 | 6877 | |
46fc3d4c | 6878 | char esignbuf[4]; |
ad391ad9 | 6879 | U8 utf8buf[UTF8_MAXLEN+1]; |
46fc3d4c | 6880 | STRLEN esignlen = 0; |
6881 | ||
6882 | char *eptr = Nullch; | |
fc36a67e | 6883 | STRLEN elen = 0; |
089c015b JH |
6884 | /* Times 4: a decimal digit takes more than 3 binary digits. |
6885 | * NV_DIG: mantissa takes than many decimal digits. | |
6886 | * Plus 32: Playing safe. */ | |
6887 | char ebuf[IV_DIG * 4 + NV_DIG + 32]; | |
2d4389e4 JH |
6888 | /* large enough for "%#.#f" --chip */ |
6889 | /* what about long double NVs? --jhi */ | |
b22c7a20 GS |
6890 | |
6891 | SV *vecsv; | |
a05b299f | 6892 | U8 *vecstr = Null(U8*); |
b22c7a20 | 6893 | STRLEN veclen = 0; |
46fc3d4c | 6894 | char c; |
6895 | int i; | |
6896 | unsigned base; | |
6897 | IV iv; | |
6898 | UV uv; | |
65202027 | 6899 | NV nv; |
46fc3d4c | 6900 | STRLEN have; |
6901 | STRLEN need; | |
6902 | STRLEN gap; | |
b22c7a20 GS |
6903 | char *dotstr = "."; |
6904 | STRLEN dotstrlen = 1; | |
211dfcf1 | 6905 | I32 efix = 0; /* explicit format parameter index */ |
eb3fce90 | 6906 | I32 ewix = 0; /* explicit width index */ |
211dfcf1 HS |
6907 | I32 epix = 0; /* explicit precision index */ |
6908 | I32 evix = 0; /* explicit vector index */ | |
eb3fce90 | 6909 | bool asterisk = FALSE; |
46fc3d4c | 6910 | |
211dfcf1 | 6911 | /* echo everything up to the next format specification */ |
46fc3d4c | 6912 | for (q = p; q < patend && *q != '%'; ++q) ; |
6913 | if (q > p) { | |
6914 | sv_catpvn(sv, p, q - p); | |
6915 | p = q; | |
6916 | } | |
6917 | if (q++ >= patend) | |
6918 | break; | |
6919 | ||
211dfcf1 HS |
6920 | /* |
6921 | We allow format specification elements in this order: | |
6922 | \d+\$ explicit format parameter index | |
6923 | [-+ 0#]+ flags | |
6924 | \*?(\d+\$)?v vector with optional (optionally specified) arg | |
6925 | \d+|\*(\d+\$)? width using optional (optionally specified) arg | |
6926 | \.(\d*|\*(\d+\$)?) precision using optional (optionally specified) arg | |
6927 | [hlqLV] size | |
6928 | [%bcdefginopsux_DFOUX] format (mandatory) | |
6929 | */ | |
6930 | if (EXPECT_NUMBER(q, width)) { | |
6931 | if (*q == '$') { | |
6932 | ++q; | |
6933 | efix = width; | |
6934 | } else { | |
6935 | goto gotwidth; | |
6936 | } | |
6937 | } | |
6938 | ||
fc36a67e | 6939 | /* FLAGS */ |
6940 | ||
46fc3d4c | 6941 | while (*q) { |
6942 | switch (*q) { | |
6943 | case ' ': | |
6944 | case '+': | |
6945 | plus = *q++; | |
6946 | continue; | |
6947 | ||
6948 | case '-': | |
6949 | left = TRUE; | |
6950 | q++; | |
6951 | continue; | |
6952 | ||
6953 | case '0': | |
6954 | fill = *q++; | |
6955 | continue; | |
6956 | ||
6957 | case '#': | |
6958 | alt = TRUE; | |
6959 | q++; | |
6960 | continue; | |
6961 | ||
fc36a67e | 6962 | default: |
6963 | break; | |
6964 | } | |
6965 | break; | |
6966 | } | |
46fc3d4c | 6967 | |
211dfcf1 | 6968 | tryasterisk: |
eb3fce90 | 6969 | if (*q == '*') { |
211dfcf1 HS |
6970 | q++; |
6971 | if (EXPECT_NUMBER(q, ewix)) | |
6972 | if (*q++ != '$') | |
6973 | goto unknown; | |
eb3fce90 | 6974 | asterisk = TRUE; |
211dfcf1 HS |
6975 | } |
6976 | if (*q == 'v') { | |
eb3fce90 | 6977 | q++; |
211dfcf1 HS |
6978 | if (vectorize) |
6979 | goto unknown; | |
9cbac4c7 | 6980 | if ((vectorarg = asterisk)) { |
211dfcf1 HS |
6981 | evix = ewix; |
6982 | ewix = 0; | |
6983 | asterisk = FALSE; | |
6984 | } | |
6985 | vectorize = TRUE; | |
6986 | goto tryasterisk; | |
eb3fce90 JH |
6987 | } |
6988 | ||
211dfcf1 HS |
6989 | if (!asterisk) |
6990 | EXPECT_NUMBER(q, width); | |
6991 | ||
6992 | if (vectorize) { | |
6993 | if (vectorarg) { | |
6994 | if (args) | |
6995 | vecsv = va_arg(*args, SV*); | |
6996 | else | |
6997 | vecsv = (evix ? evix <= svmax : svix < svmax) ? | |
6998 | svargs[ewix ? ewix-1 : svix++] : &PL_sv_undef; | |
4459522c | 6999 | dotstr = SvPVx(vecsv, dotstrlen); |
211dfcf1 HS |
7000 | if (DO_UTF8(vecsv)) |
7001 | is_utf = TRUE; | |
7002 | } | |
7003 | if (args) { | |
7004 | vecsv = va_arg(*args, SV*); | |
7005 | vecstr = (U8*)SvPVx(vecsv,veclen); | |
b2e23cf9 | 7006 | vec_utf = DO_UTF8(vecsv); |
eb3fce90 | 7007 | } |
211dfcf1 HS |
7008 | else if (efix ? efix <= svmax : svix < svmax) { |
7009 | vecsv = svargs[efix ? efix-1 : svix++]; | |
7010 | vecstr = (U8*)SvPVx(vecsv,veclen); | |
b2e23cf9 | 7011 | vec_utf = DO_UTF8(vecsv); |
211dfcf1 HS |
7012 | } |
7013 | else { | |
7014 | vecstr = (U8*)""; | |
7015 | veclen = 0; | |
7016 | } | |
eb3fce90 | 7017 | } |
fc36a67e | 7018 | |
eb3fce90 | 7019 | if (asterisk) { |
fc36a67e | 7020 | if (args) |
7021 | i = va_arg(*args, int); | |
7022 | else | |
eb3fce90 JH |
7023 | i = (ewix ? ewix <= svmax : svix < svmax) ? |
7024 | SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0; | |
fc36a67e | 7025 | left |= (i < 0); |
7026 | width = (i < 0) ? -i : i; | |
fc36a67e | 7027 | } |
211dfcf1 | 7028 | gotwidth: |
fc36a67e | 7029 | |
7030 | /* PRECISION */ | |
46fc3d4c | 7031 | |
fc36a67e | 7032 | if (*q == '.') { |
7033 | q++; | |
7034 | if (*q == '*') { | |
211dfcf1 HS |
7035 | q++; |
7036 | if (EXPECT_NUMBER(q, epix) && *q++ != '$') | |
7037 | goto unknown; | |
46fc3d4c | 7038 | if (args) |
7039 | i = va_arg(*args, int); | |
7040 | else | |
eb3fce90 JH |
7041 | i = (ewix ? ewix <= svmax : svix < svmax) |
7042 | ? SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0; | |
fc36a67e | 7043 | precis = (i < 0) ? 0 : i; |
fc36a67e | 7044 | } |
7045 | else { | |
7046 | precis = 0; | |
7047 | while (isDIGIT(*q)) | |
7048 | precis = precis * 10 + (*q++ - '0'); | |
7049 | } | |
7050 | has_precis = TRUE; | |
7051 | } | |
46fc3d4c | 7052 | |
fc36a67e | 7053 | /* SIZE */ |
46fc3d4c | 7054 | |
fc36a67e | 7055 | switch (*q) { |
e5c81feb | 7056 | #if defined(HAS_QUAD) || (defined(HAS_LONG_DOUBLE) && defined(USE_LONG_DOUBLE)) |
6f9bb7fd | 7057 | case 'L': /* Ld */ |
e5c81feb JH |
7058 | /* FALL THROUGH */ |
7059 | #endif | |
7060 | #ifdef HAS_QUAD | |
6f9bb7fd GS |
7061 | case 'q': /* qd */ |
7062 | intsize = 'q'; | |
7063 | q++; | |
7064 | break; | |
7065 | #endif | |
fc36a67e | 7066 | case 'l': |
e5c81feb JH |
7067 | #if defined(HAS_QUAD) || (defined(HAS_LONG_DOUBLE) && defined(USE_LONG_DOUBLE)) |
7068 | if (*(q + 1) == 'l') { /* lld, llf */ | |
fc36a67e | 7069 | intsize = 'q'; |
7070 | q += 2; | |
46fc3d4c | 7071 | break; |
cf2093f6 | 7072 | } |
fc36a67e | 7073 | #endif |
6f9bb7fd | 7074 | /* FALL THROUGH */ |
fc36a67e | 7075 | case 'h': |
cf2093f6 | 7076 | /* FALL THROUGH */ |
fc36a67e | 7077 | case 'V': |
7078 | intsize = *q++; | |
46fc3d4c | 7079 | break; |
7080 | } | |
7081 | ||
fc36a67e | 7082 | /* CONVERSION */ |
7083 | ||
211dfcf1 HS |
7084 | if (*q == '%') { |
7085 | eptr = q++; | |
7086 | elen = 1; | |
7087 | goto string; | |
7088 | } | |
7089 | ||
7090 | if (!args) | |
7091 | argsv = (efix ? efix <= svmax : svix < svmax) ? | |
7092 | svargs[efix ? efix-1 : svix++] : &PL_sv_undef; | |
7093 | ||
46fc3d4c | 7094 | switch (c = *q++) { |
7095 | ||
7096 | /* STRINGS */ | |
7097 | ||
46fc3d4c | 7098 | case 'c': |
211dfcf1 | 7099 | uv = args ? va_arg(*args, int) : SvIVx(argsv); |
3969a896 | 7100 | if ((uv > 255 || (uv > 127 && SvUTF8(sv))) && !IN_BYTE) { |
dfe13c55 GS |
7101 | eptr = (char*)utf8buf; |
7102 | elen = uv_to_utf8((U8*)eptr, uv) - utf8buf; | |
7e2040f0 GS |
7103 | is_utf = TRUE; |
7104 | } | |
7105 | else { | |
7106 | c = (char)uv; | |
7107 | eptr = &c; | |
7108 | elen = 1; | |
a0ed51b3 | 7109 | } |
46fc3d4c | 7110 | goto string; |
7111 | ||
46fc3d4c | 7112 | case 's': |
7113 | if (args) { | |
fc36a67e | 7114 | eptr = va_arg(*args, char*); |
c635e13b | 7115 | if (eptr) |
1d7c1841 GS |
7116 | #ifdef MACOS_TRADITIONAL |
7117 | /* On MacOS, %#s format is used for Pascal strings */ | |
7118 | if (alt) | |
7119 | elen = *eptr++; | |
7120 | else | |
7121 | #endif | |
c635e13b | 7122 | elen = strlen(eptr); |
7123 | else { | |
7124 | eptr = nullstr; | |
7125 | elen = sizeof nullstr - 1; | |
7126 | } | |
46fc3d4c | 7127 | } |
211dfcf1 | 7128 | else { |
7e2040f0 GS |
7129 | eptr = SvPVx(argsv, elen); |
7130 | if (DO_UTF8(argsv)) { | |
a0ed51b3 LW |
7131 | if (has_precis && precis < elen) { |
7132 | I32 p = precis; | |
7e2040f0 | 7133 | sv_pos_u2b(argsv, &p, 0); /* sticks at end */ |
a0ed51b3 LW |
7134 | precis = p; |
7135 | } | |
7136 | if (width) { /* fudge width (can't fudge elen) */ | |
7e2040f0 | 7137 | width += elen - sv_len_utf8(argsv); |
a0ed51b3 | 7138 | } |
7e2040f0 | 7139 | is_utf = TRUE; |
a0ed51b3 LW |
7140 | } |
7141 | } | |
46fc3d4c | 7142 | goto string; |
7143 | ||
fc36a67e | 7144 | case '_': |
7145 | /* | |
7146 | * The "%_" hack might have to be changed someday, | |
7147 | * if ISO or ANSI decide to use '_' for something. | |
7148 | * So we keep it hidden from users' code. | |
7149 | */ | |
7150 | if (!args) | |
7151 | goto unknown; | |
211dfcf1 | 7152 | argsv = va_arg(*args, SV*); |
7e2040f0 GS |
7153 | eptr = SvPVx(argsv, elen); |
7154 | if (DO_UTF8(argsv)) | |
7155 | is_utf = TRUE; | |
fc36a67e | 7156 | |
46fc3d4c | 7157 | string: |
b22c7a20 | 7158 | vectorize = FALSE; |
46fc3d4c | 7159 | if (has_precis && elen > precis) |
7160 | elen = precis; | |
7161 | break; | |
7162 | ||
7163 | /* INTEGERS */ | |
7164 | ||
fc36a67e | 7165 | case 'p': |
c2e66d9e GS |
7166 | if (alt) |
7167 | goto unknown; | |
211dfcf1 | 7168 | uv = PTR2UV(args ? va_arg(*args, void*) : argsv); |
fc36a67e | 7169 | base = 16; |
7170 | goto integer; | |
7171 | ||
46fc3d4c | 7172 | case 'D': |
29fe7a80 | 7173 | #ifdef IV_IS_QUAD |
22f3ae8c | 7174 | intsize = 'q'; |
29fe7a80 | 7175 | #else |
46fc3d4c | 7176 | intsize = 'l'; |
29fe7a80 | 7177 | #endif |
46fc3d4c | 7178 | /* FALL THROUGH */ |
7179 | case 'd': | |
7180 | case 'i': | |
b22c7a20 | 7181 | if (vectorize) { |
ba210ebe | 7182 | STRLEN ulen; |
211dfcf1 HS |
7183 | if (!veclen) |
7184 | continue; | |
b2e23cf9 | 7185 | if (vec_utf) |
dcad2880 | 7186 | iv = (IV)utf8_to_uv(vecstr, veclen, &ulen, 0); |
b22c7a20 | 7187 | else { |
a05b299f | 7188 | iv = *vecstr; |
b22c7a20 GS |
7189 | ulen = 1; |
7190 | } | |
3bd709b1 PP |
7191 | if (iv <256) |
7192 | iv = NATIVE_TO_ASCII(iv); /* v-strings are codepoints */ | |
b22c7a20 GS |
7193 | vecstr += ulen; |
7194 | veclen -= ulen; | |
7195 | } | |
7196 | else if (args) { | |
46fc3d4c | 7197 | switch (intsize) { |
7198 | case 'h': iv = (short)va_arg(*args, int); break; | |
7199 | default: iv = va_arg(*args, int); break; | |
7200 | case 'l': iv = va_arg(*args, long); break; | |
fc36a67e | 7201 | case 'V': iv = va_arg(*args, IV); break; |
cf2093f6 JH |
7202 | #ifdef HAS_QUAD |
7203 | case 'q': iv = va_arg(*args, Quad_t); break; | |
7204 | #endif | |
46fc3d4c | 7205 | } |
7206 | } | |
7207 | else { | |
211dfcf1 | 7208 | iv = SvIVx(argsv); |
46fc3d4c | 7209 | switch (intsize) { |
7210 | case 'h': iv = (short)iv; break; | |
be28567c | 7211 | default: break; |
46fc3d4c | 7212 | case 'l': iv = (long)iv; break; |
fc36a67e | 7213 | case 'V': break; |
cf2093f6 JH |
7214 | #ifdef HAS_QUAD |
7215 | case 'q': iv = (Quad_t)iv; break; | |
7216 | #endif | |
46fc3d4c | 7217 | } |
7218 | } | |
7219 | if (iv >= 0) { | |
7220 | uv = iv; | |
7221 | if (plus) | |
7222 | esignbuf[esignlen++] = plus; | |
7223 | } | |
7224 | else { | |
7225 | uv = -iv; | |
7226 | esignbuf[esignlen++] = '-'; | |
7227 | } | |
7228 | base = 10; | |
7229 | goto integer; | |
7230 | ||
fc36a67e | 7231 | case 'U': |
29fe7a80 | 7232 | #ifdef IV_IS_QUAD |
22f3ae8c | 7233 | intsize = 'q'; |
29fe7a80 | 7234 | #else |
fc36a67e | 7235 | intsize = 'l'; |
29fe7a80 | 7236 | #endif |
fc36a67e | 7237 | /* FALL THROUGH */ |
7238 | case 'u': | |
7239 | base = 10; | |
7240 | goto uns_integer; | |
7241 | ||
4f19785b WSI |
7242 | case 'b': |
7243 | base = 2; | |
7244 | goto uns_integer; | |
7245 | ||
46fc3d4c | 7246 | case 'O': |
29fe7a80 | 7247 | #ifdef IV_IS_QUAD |
22f3ae8c | 7248 | intsize = 'q'; |
29fe7a80 | 7249 | #else |
46fc3d4c | 7250 | intsize = 'l'; |
29fe7a80 | 7251 | #endif |
46fc3d4c | 7252 | /* FALL THROUGH */ |
7253 | case 'o': | |
7254 | base = 8; | |
7255 | goto uns_integer; | |
7256 | ||
7257 | case 'X': | |
46fc3d4c | 7258 | case 'x': |
7259 | base = 16; | |
46fc3d4c | 7260 | |
7261 | uns_integer: | |
b22c7a20 | 7262 | if (vectorize) { |
ba210ebe | 7263 | STRLEN ulen; |
b22c7a20 | 7264 | vector: |
211dfcf1 HS |
7265 | if (!veclen) |
7266 | continue; | |
b2e23cf9 | 7267 | if (vec_utf) |
dcad2880 | 7268 | uv = utf8_to_uv(vecstr, veclen, &ulen, 0); |
b22c7a20 | 7269 | else { |
a05b299f | 7270 | uv = *vecstr; |
b22c7a20 GS |
7271 | ulen = 1; |
7272 | } | |
3bd709b1 PP |
7273 | if (uv <256) |
7274 | uv = NATIVE_TO_ASCII(uv); /* v-strings are codepoints */ | |
b22c7a20 GS |
7275 | vecstr += ulen; |
7276 | veclen -= ulen; | |
7277 | } | |
7278 | else if (args) { | |
46fc3d4c | 7279 | switch (intsize) { |
7280 | case 'h': uv = (unsigned short)va_arg(*args, unsigned); break; | |
7281 | default: uv = va_arg(*args, unsigned); break; | |
7282 | case 'l': uv = va_arg(*args, unsigned long); break; | |
fc36a67e | 7283 | case 'V': uv = va_arg(*args, UV); break; |
cf2093f6 JH |
7284 | #ifdef HAS_QUAD |
7285 | case 'q': uv = va_arg(*args, Quad_t); break; | |
7286 | #endif | |
46fc3d4c | 7287 | } |
7288 | } | |
7289 | else { | |
211dfcf1 | 7290 | uv = SvUVx(argsv); |
46fc3d4c | 7291 | switch (intsize) { |
7292 | case 'h': uv = (unsigned short)uv; break; | |
be28567c | 7293 | default: break; |
46fc3d4c | 7294 | case 'l': uv = (unsigned long)uv; break; |
fc36a67e | 7295 | case 'V': break; |
cf2093f6 JH |
7296 | #ifdef HAS_QUAD |
7297 | case 'q': uv = (Quad_t)uv; break; | |
7298 | #endif | |
46fc3d4c | 7299 | } |
7300 | } | |
7301 | ||
7302 | integer: | |
46fc3d4c | 7303 | eptr = ebuf + sizeof ebuf; |
fc36a67e | 7304 | switch (base) { |
7305 | unsigned dig; | |
7306 | case 16: | |
c10ed8b9 HS |
7307 | if (!uv) |
7308 | alt = FALSE; | |
1d7c1841 GS |
7309 | p = (char*)((c == 'X') |
7310 | ? "0123456789ABCDEF" : "0123456789abcdef"); | |
fc36a67e | 7311 | do { |
7312 | dig = uv & 15; | |
7313 | *--eptr = p[dig]; | |
7314 | } while (uv >>= 4); | |
7315 | if (alt) { | |
46fc3d4c | 7316 | esignbuf[esignlen++] = '0'; |
fc36a67e | 7317 | esignbuf[esignlen++] = c; /* 'x' or 'X' */ |
46fc3d4c | 7318 | } |
fc36a67e | 7319 | break; |
7320 | case 8: | |
7321 | do { | |
7322 | dig = uv & 7; | |
7323 | *--eptr = '0' + dig; | |
7324 | } while (uv >>= 3); | |
7325 | if (alt && *eptr != '0') | |
7326 | *--eptr = '0'; | |
7327 | break; | |
4f19785b WSI |
7328 | case 2: |
7329 | do { | |
7330 | dig = uv & 1; | |
7331 | *--eptr = '0' + dig; | |
7332 | } while (uv >>= 1); | |
eda88b6d JH |
7333 | if (alt) { |
7334 | esignbuf[esignlen++] = '0'; | |
7481bb52 | 7335 | esignbuf[esignlen++] = 'b'; |
eda88b6d | 7336 | } |
4f19785b | 7337 | break; |
fc36a67e | 7338 | default: /* it had better be ten or less */ |
6bc102ca | 7339 | #if defined(PERL_Y2KWARN) |
e476b1b5 | 7340 | if (ckWARN(WARN_Y2K)) { |
6bc102ca GS |
7341 | STRLEN n; |
7342 | char *s = SvPV(sv,n); | |
7343 | if (n >= 2 && s[n-2] == '1' && s[n-1] == '9' | |
7344 | && (n == 2 || !isDIGIT(s[n-3]))) | |
7345 | { | |
e476b1b5 | 7346 | Perl_warner(aTHX_ WARN_Y2K, |
6bc102ca GS |
7347 | "Possible Y2K bug: %%%c %s", |
7348 | c, "format string following '19'"); | |
7349 | } | |
7350 | } | |
7351 | #endif | |
fc36a67e | 7352 | do { |
7353 | dig = uv % base; | |
7354 | *--eptr = '0' + dig; | |
7355 | } while (uv /= base); | |
7356 | break; | |
46fc3d4c | 7357 | } |
7358 | elen = (ebuf + sizeof ebuf) - eptr; | |
c10ed8b9 HS |
7359 | if (has_precis) { |
7360 | if (precis > elen) | |
7361 | zeros = precis - elen; | |
7362 | else if (precis == 0 && elen == 1 && *eptr == '0') | |
7363 | elen = 0; | |
7364 | } | |
46fc3d4c | 7365 | break; |
7366 | ||
7367 | /* FLOATING POINT */ | |
7368 | ||
fc36a67e | 7369 | case 'F': |
7370 | c = 'f'; /* maybe %F isn't supported here */ | |
7371 | /* FALL THROUGH */ | |
46fc3d4c | 7372 | case 'e': case 'E': |
fc36a67e | 7373 | case 'f': |
46fc3d4c | 7374 | case 'g': case 'G': |
7375 | ||
7376 | /* This is evil, but floating point is even more evil */ | |
7377 | ||
b22c7a20 | 7378 | vectorize = FALSE; |
211dfcf1 | 7379 | nv = args ? va_arg(*args, NV) : SvNVx(argsv); |
fc36a67e | 7380 | |
7381 | need = 0; | |
7382 | if (c != 'e' && c != 'E') { | |
7383 | i = PERL_INT_MIN; | |
73b309ea | 7384 | (void)Perl_frexp(nv, &i); |
fc36a67e | 7385 | if (i == PERL_INT_MIN) |
cea2e8a9 | 7386 | Perl_die(aTHX_ "panic: frexp"); |
c635e13b | 7387 | if (i > 0) |
fc36a67e | 7388 | need = BIT_DIGITS(i); |
7389 | } | |
7390 | need += has_precis ? precis : 6; /* known default */ | |
7391 | if (need < width) | |
7392 | need = width; | |
7393 | ||
46fc3d4c | 7394 | need += 20; /* fudge factor */ |
80252599 GS |
7395 | if (PL_efloatsize < need) { |
7396 | Safefree(PL_efloatbuf); | |
7397 | PL_efloatsize = need + 20; /* more fudge */ | |
7398 | New(906, PL_efloatbuf, PL_efloatsize, char); | |
7d5ea4e7 | 7399 | PL_efloatbuf[0] = '\0'; |
46fc3d4c | 7400 | } |
7401 | ||
7402 | eptr = ebuf + sizeof ebuf; | |
7403 | *--eptr = '\0'; | |
7404 | *--eptr = c; | |
e5c81feb | 7405 | #if defined(USE_LONG_DOUBLE) && defined(PERL_PRIfldbl) |
cf2093f6 | 7406 | { |
e5c81feb JH |
7407 | /* Copy the one or more characters in a long double |
7408 | * format before the 'base' ([efgEFG]) character to | |
7409 | * the format string. */ | |
7410 | static char const prifldbl[] = PERL_PRIfldbl; | |
7411 | char const *p = prifldbl + sizeof(prifldbl) - 3; | |
7412 | while (p >= prifldbl) { *--eptr = *p--; } | |
cf2093f6 | 7413 | } |
65202027 | 7414 | #endif |
46fc3d4c | 7415 | if (has_precis) { |
7416 | base = precis; | |
7417 | do { *--eptr = '0' + (base % 10); } while (base /= 10); | |
7418 | *--eptr = '.'; | |
7419 | } | |
7420 | if (width) { | |
7421 | base = width; | |
7422 | do { *--eptr = '0' + (base % 10); } while (base /= 10); | |
7423 | } | |
7424 | if (fill == '0') | |
7425 | *--eptr = fill; | |
84902520 TB |
7426 | if (left) |
7427 | *--eptr = '-'; | |
46fc3d4c | 7428 | if (plus) |
7429 | *--eptr = plus; | |
7430 | if (alt) | |
7431 | *--eptr = '#'; | |
7432 | *--eptr = '%'; | |
7433 | ||
ff9121f8 JH |
7434 | /* No taint. Otherwise we are in the strange situation |
7435 | * where printf() taints but print($float) doesn't. | |
bda0f7a5 | 7436 | * --jhi */ |
dd8482fc | 7437 | (void)sprintf(PL_efloatbuf, eptr, nv); |
8af02333 | 7438 | |
80252599 GS |
7439 | eptr = PL_efloatbuf; |
7440 | elen = strlen(PL_efloatbuf); | |
46fc3d4c | 7441 | break; |
7442 | ||
fc36a67e | 7443 | /* SPECIAL */ |
7444 | ||
7445 | case 'n': | |
b22c7a20 | 7446 | vectorize = FALSE; |
fc36a67e | 7447 | i = SvCUR(sv) - origlen; |
7448 | if (args) { | |
c635e13b | 7449 | switch (intsize) { |
7450 | case 'h': *(va_arg(*args, short*)) = i; break; | |
7451 | default: *(va_arg(*args, int*)) = i; break; | |
7452 | case 'l': *(va_arg(*args, long*)) = i; break; | |
7453 | case 'V': *(va_arg(*args, IV*)) = i; break; | |
cf2093f6 JH |
7454 | #ifdef HAS_QUAD |
7455 | case 'q': *(va_arg(*args, Quad_t*)) = i; break; | |
7456 | #endif | |
c635e13b | 7457 | } |
fc36a67e | 7458 | } |
9dd79c3f | 7459 | else |
211dfcf1 | 7460 | sv_setuv_mg(argsv, (UV)i); |
fc36a67e | 7461 | continue; /* not "break" */ |
7462 | ||
7463 | /* UNKNOWN */ | |
7464 | ||
46fc3d4c | 7465 | default: |
fc36a67e | 7466 | unknown: |
b22c7a20 | 7467 | vectorize = FALSE; |
599cee73 | 7468 | if (!args && ckWARN(WARN_PRINTF) && |
533c011a | 7469 | (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF)) { |
c635e13b | 7470 | SV *msg = sv_newmortal(); |
cea2e8a9 | 7471 | Perl_sv_setpvf(aTHX_ msg, "Invalid conversion in %s: ", |
533c011a | 7472 | (PL_op->op_type == OP_PRTF) ? "printf" : "sprintf"); |
0f4b6630 | 7473 | if (c) { |
0f4b6630 | 7474 | if (isPRINT(c)) |
1c846c1f | 7475 | Perl_sv_catpvf(aTHX_ msg, |
0f4b6630 JH |
7476 | "\"%%%c\"", c & 0xFF); |
7477 | else | |
7478 | Perl_sv_catpvf(aTHX_ msg, | |
57def98f | 7479 | "\"%%\\%03"UVof"\"", |
0f4b6630 | 7480 | (UV)c & 0xFF); |
0f4b6630 | 7481 | } else |
c635e13b | 7482 | sv_catpv(msg, "end of string"); |
894356b3 | 7483 | Perl_warner(aTHX_ WARN_PRINTF, "%"SVf, msg); /* yes, this is reentrant */ |
c635e13b | 7484 | } |
fb73857a | 7485 | |
7486 | /* output mangled stuff ... */ | |
7487 | if (c == '\0') | |
7488 | --q; | |
46fc3d4c | 7489 | eptr = p; |
7490 | elen = q - p; | |
fb73857a | 7491 | |
7492 | /* ... right here, because formatting flags should not apply */ | |
7493 | SvGROW(sv, SvCUR(sv) + elen + 1); | |
7494 | p = SvEND(sv); | |
4459522c | 7495 | Copy(eptr, p, elen, char); |
fb73857a | 7496 | p += elen; |
7497 | *p = '\0'; | |
7498 | SvCUR(sv) = p - SvPVX(sv); | |
7499 | continue; /* not "break" */ | |
46fc3d4c | 7500 | } |
7501 | ||
fc36a67e | 7502 | have = esignlen + zeros + elen; |
46fc3d4c | 7503 | need = (have > width ? have : width); |
7504 | gap = need - have; | |
7505 | ||
b22c7a20 | 7506 | SvGROW(sv, SvCUR(sv) + need + dotstrlen + 1); |
46fc3d4c | 7507 | p = SvEND(sv); |
7508 | if (esignlen && fill == '0') { | |
7509 | for (i = 0; i < esignlen; i++) | |
7510 | *p++ = esignbuf[i]; | |
7511 | } | |
7512 | if (gap && !left) { | |
7513 | memset(p, fill, gap); | |
7514 | p += gap; | |
7515 | } | |
7516 | if (esignlen && fill != '0') { | |
7517 | for (i = 0; i < esignlen; i++) | |
7518 | *p++ = esignbuf[i]; | |
7519 | } | |
fc36a67e | 7520 | if (zeros) { |
7521 | for (i = zeros; i; i--) | |
7522 | *p++ = '0'; | |
7523 | } | |
46fc3d4c | 7524 | if (elen) { |
4459522c | 7525 | Copy(eptr, p, elen, char); |
46fc3d4c | 7526 | p += elen; |
7527 | } | |
7528 | if (gap && left) { | |
7529 | memset(p, ' ', gap); | |
7530 | p += gap; | |
7531 | } | |
b22c7a20 GS |
7532 | if (vectorize) { |
7533 | if (veclen) { | |
4459522c | 7534 | Copy(dotstr, p, dotstrlen, char); |
b22c7a20 GS |
7535 | p += dotstrlen; |
7536 | } | |
7537 | else | |
7538 | vectorize = FALSE; /* done iterating over vecstr */ | |
7539 | } | |
7e2040f0 GS |
7540 | if (is_utf) |
7541 | SvUTF8_on(sv); | |
46fc3d4c | 7542 | *p = '\0'; |
7543 | SvCUR(sv) = p - SvPVX(sv); | |
b22c7a20 GS |
7544 | if (vectorize) { |
7545 | esignlen = 0; | |
7546 | goto vector; | |
7547 | } | |
46fc3d4c | 7548 | } |
7549 | } | |
51371543 | 7550 | |
1d7c1841 GS |
7551 | #if defined(USE_ITHREADS) |
7552 | ||
7553 | #if defined(USE_THREADS) | |
7554 | # include "error: USE_THREADS and USE_ITHREADS are incompatible" | |
7555 | #endif | |
7556 | ||
1d7c1841 GS |
7557 | #ifndef GpREFCNT_inc |
7558 | # define GpREFCNT_inc(gp) ((gp) ? (++(gp)->gp_refcnt, (gp)) : (GP*)NULL) | |
7559 | #endif | |
7560 | ||
7561 | ||
7562 | #define sv_dup_inc(s) SvREFCNT_inc(sv_dup(s)) | |
7563 | #define av_dup(s) (AV*)sv_dup((SV*)s) | |
7564 | #define av_dup_inc(s) (AV*)SvREFCNT_inc(sv_dup((SV*)s)) | |
7565 | #define hv_dup(s) (HV*)sv_dup((SV*)s) | |
7566 | #define hv_dup_inc(s) (HV*)SvREFCNT_inc(sv_dup((SV*)s)) | |
7567 | #define cv_dup(s) (CV*)sv_dup((SV*)s) | |
7568 | #define cv_dup_inc(s) (CV*)SvREFCNT_inc(sv_dup((SV*)s)) | |
7569 | #define io_dup(s) (IO*)sv_dup((SV*)s) | |
7570 | #define io_dup_inc(s) (IO*)SvREFCNT_inc(sv_dup((SV*)s)) | |
7571 | #define gv_dup(s) (GV*)sv_dup((SV*)s) | |
7572 | #define gv_dup_inc(s) (GV*)SvREFCNT_inc(sv_dup((SV*)s)) | |
7573 | #define SAVEPV(p) (p ? savepv(p) : Nullch) | |
7574 | #define SAVEPVN(p,n) (p ? savepvn(p,n) : Nullch) | |
7575 | ||
7576 | REGEXP * | |
7577 | Perl_re_dup(pTHX_ REGEXP *r) | |
7578 | { | |
7579 | /* XXX fix when pmop->op_pmregexp becomes shared */ | |
7580 | return ReREFCNT_inc(r); | |
7581 | } | |
7582 | ||
7583 | PerlIO * | |
7584 | Perl_fp_dup(pTHX_ PerlIO *fp, char type) | |
7585 | { | |
7586 | PerlIO *ret; | |
7587 | if (!fp) | |
7588 | return (PerlIO*)NULL; | |
7589 | ||
7590 | /* look for it in the table first */ | |
7591 | ret = (PerlIO*)ptr_table_fetch(PL_ptr_table, fp); | |
7592 | if (ret) | |
7593 | return ret; | |
7594 | ||
7595 | /* create anew and remember what it is */ | |
5f1a76d0 | 7596 | ret = PerlIO_fdupopen(aTHX_ fp); |
1d7c1841 GS |
7597 | ptr_table_store(PL_ptr_table, fp, ret); |
7598 | return ret; | |
7599 | } | |
7600 | ||
7601 | DIR * | |
7602 | Perl_dirp_dup(pTHX_ DIR *dp) | |
7603 | { | |
7604 | if (!dp) | |
7605 | return (DIR*)NULL; | |
7606 | /* XXX TODO */ | |
7607 | return dp; | |
7608 | } | |
7609 | ||
7610 | GP * | |
7611 | Perl_gp_dup(pTHX_ GP *gp) | |
7612 | { | |
7613 | GP *ret; | |
7614 | if (!gp) | |
7615 | return (GP*)NULL; | |
7616 | /* look for it in the table first */ | |
7617 | ret = (GP*)ptr_table_fetch(PL_ptr_table, gp); | |
7618 | if (ret) | |
7619 | return ret; | |
7620 | ||
7621 | /* create anew and remember what it is */ | |
7622 | Newz(0, ret, 1, GP); | |
7623 | ptr_table_store(PL_ptr_table, gp, ret); | |
7624 | ||
7625 | /* clone */ | |
7626 | ret->gp_refcnt = 0; /* must be before any other dups! */ | |
7627 | ret->gp_sv = sv_dup_inc(gp->gp_sv); | |
7628 | ret->gp_io = io_dup_inc(gp->gp_io); | |
7629 | ret->gp_form = cv_dup_inc(gp->gp_form); | |
7630 | ret->gp_av = av_dup_inc(gp->gp_av); | |
7631 | ret->gp_hv = hv_dup_inc(gp->gp_hv); | |
7632 | ret->gp_egv = gv_dup(gp->gp_egv); /* GvEGV is not refcounted */ | |
7633 | ret->gp_cv = cv_dup_inc(gp->gp_cv); | |
7634 | ret->gp_cvgen = gp->gp_cvgen; | |
7635 | ret->gp_flags = gp->gp_flags; | |
7636 | ret->gp_line = gp->gp_line; | |
7637 | ret->gp_file = gp->gp_file; /* points to COP.cop_file */ | |
7638 | return ret; | |
7639 | } | |
7640 | ||
7641 | MAGIC * | |
7642 | Perl_mg_dup(pTHX_ MAGIC *mg) | |
7643 | { | |
7644 | MAGIC *mgret = (MAGIC*)NULL; | |
7645 | MAGIC *mgprev; | |
7646 | if (!mg) | |
7647 | return (MAGIC*)NULL; | |
7648 | /* look for it in the table first */ | |
7649 | mgret = (MAGIC*)ptr_table_fetch(PL_ptr_table, mg); | |
7650 | if (mgret) | |
7651 | return mgret; | |
7652 | ||
7653 | for (; mg; mg = mg->mg_moremagic) { | |
7654 | MAGIC *nmg; | |
7655 | Newz(0, nmg, 1, MAGIC); | |
7656 | if (!mgret) | |
7657 | mgret = nmg; | |
7658 | else | |
7659 | mgprev->mg_moremagic = nmg; | |
7660 | nmg->mg_virtual = mg->mg_virtual; /* XXX copy dynamic vtable? */ | |
7661 | nmg->mg_private = mg->mg_private; | |
7662 | nmg->mg_type = mg->mg_type; | |
7663 | nmg->mg_flags = mg->mg_flags; | |
7664 | if (mg->mg_type == 'r') { | |
7665 | nmg->mg_obj = (SV*)re_dup((REGEXP*)mg->mg_obj); | |
7666 | } | |
7667 | else { | |
7668 | nmg->mg_obj = (mg->mg_flags & MGf_REFCOUNTED) | |
7669 | ? sv_dup_inc(mg->mg_obj) | |
7670 | : sv_dup(mg->mg_obj); | |
7671 | } | |
7672 | nmg->mg_len = mg->mg_len; | |
7673 | nmg->mg_ptr = mg->mg_ptr; /* XXX random ptr? */ | |
7674 | if (mg->mg_ptr && mg->mg_type != 'g') { | |
7675 | if (mg->mg_len >= 0) { | |
7676 | nmg->mg_ptr = SAVEPVN(mg->mg_ptr, mg->mg_len); | |
7677 | if (mg->mg_type == 'c' && AMT_AMAGIC((AMT*)mg->mg_ptr)) { | |
7678 | AMT *amtp = (AMT*)mg->mg_ptr; | |
7679 | AMT *namtp = (AMT*)nmg->mg_ptr; | |
7680 | I32 i; | |
7681 | for (i = 1; i < NofAMmeth; i++) { | |
7682 | namtp->table[i] = cv_dup_inc(amtp->table[i]); | |
7683 | } | |
7684 | } | |
7685 | } | |
7686 | else if (mg->mg_len == HEf_SVKEY) | |
7687 | nmg->mg_ptr = (char*)sv_dup_inc((SV*)mg->mg_ptr); | |
7688 | } | |
7689 | mgprev = nmg; | |
7690 | } | |
7691 | return mgret; | |
7692 | } | |
7693 | ||
7694 | PTR_TBL_t * | |
7695 | Perl_ptr_table_new(pTHX) | |
7696 | { | |
7697 | PTR_TBL_t *tbl; | |
7698 | Newz(0, tbl, 1, PTR_TBL_t); | |
7699 | tbl->tbl_max = 511; | |
7700 | tbl->tbl_items = 0; | |
7701 | Newz(0, tbl->tbl_ary, tbl->tbl_max + 1, PTR_TBL_ENT_t*); | |
7702 | return tbl; | |
7703 | } | |
7704 | ||
7705 | void * | |
7706 | Perl_ptr_table_fetch(pTHX_ PTR_TBL_t *tbl, void *sv) | |
7707 | { | |
7708 | PTR_TBL_ENT_t *tblent; | |
d2a79402 | 7709 | UV hash = PTR2UV(sv); |
1d7c1841 GS |
7710 | assert(tbl); |
7711 | tblent = tbl->tbl_ary[hash & tbl->tbl_max]; | |
7712 | for (; tblent; tblent = tblent->next) { | |
7713 | if (tblent->oldval == sv) | |
7714 | return tblent->newval; | |
7715 | } | |
7716 | return (void*)NULL; | |
7717 | } | |
7718 | ||
7719 | void | |
7720 | Perl_ptr_table_store(pTHX_ PTR_TBL_t *tbl, void *oldv, void *newv) | |
7721 | { | |
7722 | PTR_TBL_ENT_t *tblent, **otblent; | |
7723 | /* XXX this may be pessimal on platforms where pointers aren't good | |
7724 | * hash values e.g. if they grow faster in the most significant | |
7725 | * bits */ | |
d2a79402 | 7726 | UV hash = PTR2UV(oldv); |
1d7c1841 GS |
7727 | bool i = 1; |
7728 | ||
7729 | assert(tbl); | |
7730 | otblent = &tbl->tbl_ary[hash & tbl->tbl_max]; | |
7731 | for (tblent = *otblent; tblent; i=0, tblent = tblent->next) { | |
7732 | if (tblent->oldval == oldv) { | |
7733 | tblent->newval = newv; | |
7734 | tbl->tbl_items++; | |
7735 | return; | |
7736 | } | |
7737 | } | |
7738 | Newz(0, tblent, 1, PTR_TBL_ENT_t); | |
7739 | tblent->oldval = oldv; | |
7740 | tblent->newval = newv; | |
7741 | tblent->next = *otblent; | |
7742 | *otblent = tblent; | |
7743 | tbl->tbl_items++; | |
7744 | if (i && tbl->tbl_items > tbl->tbl_max) | |
7745 | ptr_table_split(tbl); | |
7746 | } | |
7747 | ||
7748 | void | |
7749 | Perl_ptr_table_split(pTHX_ PTR_TBL_t *tbl) | |
7750 | { | |
7751 | PTR_TBL_ENT_t **ary = tbl->tbl_ary; | |
7752 | UV oldsize = tbl->tbl_max + 1; | |
7753 | UV newsize = oldsize * 2; | |
7754 | UV i; | |
7755 | ||
7756 | Renew(ary, newsize, PTR_TBL_ENT_t*); | |
7757 | Zero(&ary[oldsize], newsize-oldsize, PTR_TBL_ENT_t*); | |
7758 | tbl->tbl_max = --newsize; | |
7759 | tbl->tbl_ary = ary; | |
7760 | for (i=0; i < oldsize; i++, ary++) { | |
7761 | PTR_TBL_ENT_t **curentp, **entp, *ent; | |
7762 | if (!*ary) | |
7763 | continue; | |
7764 | curentp = ary + oldsize; | |
7765 | for (entp = ary, ent = *ary; ent; ent = *entp) { | |
d2a79402 | 7766 | if ((newsize & PTR2UV(ent->oldval)) != i) { |
1d7c1841 GS |
7767 | *entp = ent->next; |
7768 | ent->next = *curentp; | |
7769 | *curentp = ent; | |
7770 | continue; | |
7771 | } | |
7772 | else | |
7773 | entp = &ent->next; | |
7774 | } | |
7775 | } | |
7776 | } | |
7777 | ||
a0739874 DM |
7778 | void |
7779 | Perl_ptr_table_clear(pTHX_ PTR_TBL_t *tbl) | |
7780 | { | |
7781 | register PTR_TBL_ENT_t **array; | |
7782 | register PTR_TBL_ENT_t *entry; | |
7783 | register PTR_TBL_ENT_t *oentry = Null(PTR_TBL_ENT_t*); | |
7784 | UV riter = 0; | |
7785 | UV max; | |
7786 | ||
7787 | if (!tbl || !tbl->tbl_items) { | |
7788 | return; | |
7789 | } | |
7790 | ||
7791 | array = tbl->tbl_ary; | |
7792 | entry = array[0]; | |
7793 | max = tbl->tbl_max; | |
7794 | ||
7795 | for (;;) { | |
7796 | if (entry) { | |
7797 | oentry = entry; | |
7798 | entry = entry->next; | |
7799 | Safefree(oentry); | |
7800 | } | |
7801 | if (!entry) { | |
7802 | if (++riter > max) { | |
7803 | break; | |
7804 | } | |
7805 | entry = array[riter]; | |
7806 | } | |
7807 | } | |
7808 | ||
7809 | tbl->tbl_items = 0; | |
7810 | } | |
7811 | ||
7812 | void | |
7813 | Perl_ptr_table_free(pTHX_ PTR_TBL_t *tbl) | |
7814 | { | |
7815 | if (!tbl) { | |
7816 | return; | |
7817 | } | |
7818 | ptr_table_clear(tbl); | |
7819 | Safefree(tbl->tbl_ary); | |
7820 | Safefree(tbl); | |
7821 | } | |
7822 | ||
1d7c1841 GS |
7823 | #ifdef DEBUGGING |
7824 | char *PL_watch_pvx; | |
7825 | #endif | |
7826 | ||
5bd07a3d DM |
7827 | STATIC SV * |
7828 | S_gv_share(pTHX_ SV *sstr) | |
7829 | { | |
7830 | GV *gv = (GV*)sstr; | |
7831 | SV *sv = &PL_sv_no; /* just need SvREADONLY-ness */ | |
7832 | ||
7833 | if (GvIO(gv) || GvFORM(gv)) { | |
7834 | GvSHARED_off(gv); /* GvIOs cannot be shared. nor can GvFORMs */ | |
7835 | } | |
7836 | else if (!GvCV(gv)) { | |
7837 | GvCV(gv) = (CV*)sv; | |
7838 | } | |
7839 | else { | |
7840 | /* CvPADLISTs cannot be shared */ | |
7841 | if (!CvXSUB(GvCV(gv))) { | |
7842 | GvSHARED_off(gv); | |
7843 | } | |
7844 | } | |
7845 | ||
7846 | if (!GvSHARED(gv)) { | |
7847 | #if 0 | |
7848 | PerlIO_printf(Perl_debug_log, "gv_share: unable to share %s::%s\n", | |
7849 | HvNAME(GvSTASH(gv)), GvNAME(gv)); | |
7850 | #endif | |
7851 | return Nullsv; | |
7852 | } | |
7853 | ||
4411f3b6 | 7854 | /* |
5bd07a3d DM |
7855 | * write attempts will die with |
7856 | * "Modification of a read-only value attempted" | |
7857 | */ | |
7858 | if (!GvSV(gv)) { | |
7859 | GvSV(gv) = sv; | |
7860 | } | |
7861 | else { | |
7862 | SvREADONLY_on(GvSV(gv)); | |
7863 | } | |
7864 | ||
7865 | if (!GvAV(gv)) { | |
7866 | GvAV(gv) = (AV*)sv; | |
7867 | } | |
7868 | else { | |
7869 | SvREADONLY_on(GvAV(gv)); | |
7870 | } | |
7871 | ||
7872 | if (!GvHV(gv)) { | |
7873 | GvHV(gv) = (HV*)sv; | |
7874 | } | |
7875 | else { | |
7876 | SvREADONLY_on(GvAV(gv)); | |
7877 | } | |
7878 | ||
7879 | return sstr; /* he_dup() will SvREFCNT_inc() */ | |
7880 | } | |
7881 | ||
1d7c1841 GS |
7882 | SV * |
7883 | Perl_sv_dup(pTHX_ SV *sstr) | |
7884 | { | |
1d7c1841 GS |
7885 | SV *dstr; |
7886 | ||
7887 | if (!sstr || SvTYPE(sstr) == SVTYPEMASK) | |
7888 | return Nullsv; | |
7889 | /* look for it in the table first */ | |
7890 | dstr = (SV*)ptr_table_fetch(PL_ptr_table, sstr); | |
7891 | if (dstr) | |
7892 | return dstr; | |
7893 | ||
7894 | /* create anew and remember what it is */ | |
7895 | new_SV(dstr); | |
7896 | ptr_table_store(PL_ptr_table, sstr, dstr); | |
7897 | ||
7898 | /* clone */ | |
7899 | SvFLAGS(dstr) = SvFLAGS(sstr); | |
7900 | SvFLAGS(dstr) &= ~SVf_OOK; /* don't propagate OOK hack */ | |
7901 | SvREFCNT(dstr) = 0; /* must be before any other dups! */ | |
7902 | ||
7903 | #ifdef DEBUGGING | |
7904 | if (SvANY(sstr) && PL_watch_pvx && SvPVX(sstr) == PL_watch_pvx) | |
7905 | PerlIO_printf(Perl_debug_log, "watch at %p hit, found string \"%s\"\n", | |
7906 | PL_watch_pvx, SvPVX(sstr)); | |
7907 | #endif | |
7908 | ||
7909 | switch (SvTYPE(sstr)) { | |
7910 | case SVt_NULL: | |
7911 | SvANY(dstr) = NULL; | |
7912 | break; | |
7913 | case SVt_IV: | |
7914 | SvANY(dstr) = new_XIV(); | |
7915 | SvIVX(dstr) = SvIVX(sstr); | |
7916 | break; | |
7917 | case SVt_NV: | |
7918 | SvANY(dstr) = new_XNV(); | |
7919 | SvNVX(dstr) = SvNVX(sstr); | |
7920 | break; | |
7921 | case SVt_RV: | |
7922 | SvANY(dstr) = new_XRV(); | |
7923 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7924 | break; | |
7925 | case SVt_PV: | |
7926 | SvANY(dstr) = new_XPV(); | |
7927 | SvCUR(dstr) = SvCUR(sstr); | |
7928 | SvLEN(dstr) = SvLEN(sstr); | |
7929 | if (SvROK(sstr)) | |
7930 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7931 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
7932 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
7933 | else | |
7934 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
7935 | break; | |
7936 | case SVt_PVIV: | |
7937 | SvANY(dstr) = new_XPVIV(); | |
7938 | SvCUR(dstr) = SvCUR(sstr); | |
7939 | SvLEN(dstr) = SvLEN(sstr); | |
7940 | SvIVX(dstr) = SvIVX(sstr); | |
7941 | if (SvROK(sstr)) | |
7942 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7943 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
7944 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
7945 | else | |
7946 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
7947 | break; | |
7948 | case SVt_PVNV: | |
7949 | SvANY(dstr) = new_XPVNV(); | |
7950 | SvCUR(dstr) = SvCUR(sstr); | |
7951 | SvLEN(dstr) = SvLEN(sstr); | |
7952 | SvIVX(dstr) = SvIVX(sstr); | |
7953 | SvNVX(dstr) = SvNVX(sstr); | |
7954 | if (SvROK(sstr)) | |
7955 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7956 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
7957 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
7958 | else | |
7959 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
7960 | break; | |
7961 | case SVt_PVMG: | |
7962 | SvANY(dstr) = new_XPVMG(); | |
7963 | SvCUR(dstr) = SvCUR(sstr); | |
7964 | SvLEN(dstr) = SvLEN(sstr); | |
7965 | SvIVX(dstr) = SvIVX(sstr); | |
7966 | SvNVX(dstr) = SvNVX(sstr); | |
7967 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
7968 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
7969 | if (SvROK(sstr)) | |
7970 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7971 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
7972 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
7973 | else | |
7974 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
7975 | break; | |
7976 | case SVt_PVBM: | |
7977 | SvANY(dstr) = new_XPVBM(); | |
7978 | SvCUR(dstr) = SvCUR(sstr); | |
7979 | SvLEN(dstr) = SvLEN(sstr); | |
7980 | SvIVX(dstr) = SvIVX(sstr); | |
7981 | SvNVX(dstr) = SvNVX(sstr); | |
7982 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
7983 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
7984 | if (SvROK(sstr)) | |
7985 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7986 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
7987 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
7988 | else | |
7989 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
7990 | BmRARE(dstr) = BmRARE(sstr); | |
7991 | BmUSEFUL(dstr) = BmUSEFUL(sstr); | |
7992 | BmPREVIOUS(dstr)= BmPREVIOUS(sstr); | |
7993 | break; | |
7994 | case SVt_PVLV: | |
7995 | SvANY(dstr) = new_XPVLV(); | |
7996 | SvCUR(dstr) = SvCUR(sstr); | |
7997 | SvLEN(dstr) = SvLEN(sstr); | |
7998 | SvIVX(dstr) = SvIVX(sstr); | |
7999 | SvNVX(dstr) = SvNVX(sstr); | |
8000 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
8001 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
8002 | if (SvROK(sstr)) | |
8003 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
8004 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
8005 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
8006 | else | |
8007 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
8008 | LvTARGOFF(dstr) = LvTARGOFF(sstr); /* XXX sometimes holds PMOP* when DEBUGGING */ | |
8009 | LvTARGLEN(dstr) = LvTARGLEN(sstr); | |
8010 | LvTARG(dstr) = sv_dup_inc(LvTARG(sstr)); | |
8011 | LvTYPE(dstr) = LvTYPE(sstr); | |
8012 | break; | |
8013 | case SVt_PVGV: | |
5bd07a3d DM |
8014 | if (GvSHARED((GV*)sstr)) { |
8015 | SV *share; | |
8016 | if ((share = gv_share(sstr))) { | |
8017 | del_SV(dstr); | |
8018 | dstr = share; | |
8019 | #if 0 | |
8020 | PerlIO_printf(Perl_debug_log, "sv_dup: sharing %s::%s\n", | |
8021 | HvNAME(GvSTASH(share)), GvNAME(share)); | |
8022 | #endif | |
8023 | break; | |
8024 | } | |
8025 | } | |
1d7c1841 GS |
8026 | SvANY(dstr) = new_XPVGV(); |
8027 | SvCUR(dstr) = SvCUR(sstr); | |
8028 | SvLEN(dstr) = SvLEN(sstr); | |
8029 | SvIVX(dstr) = SvIVX(sstr); | |
8030 | SvNVX(dstr) = SvNVX(sstr); | |
8031 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
8032 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
8033 | if (SvROK(sstr)) | |
8034 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
8035 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
8036 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
8037 | else | |
8038 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
8039 | GvNAMELEN(dstr) = GvNAMELEN(sstr); | |
8040 | GvNAME(dstr) = SAVEPVN(GvNAME(sstr), GvNAMELEN(sstr)); | |
8041 | GvSTASH(dstr) = hv_dup_inc(GvSTASH(sstr)); | |
8042 | GvFLAGS(dstr) = GvFLAGS(sstr); | |
8043 | GvGP(dstr) = gp_dup(GvGP(sstr)); | |
8044 | (void)GpREFCNT_inc(GvGP(dstr)); | |
8045 | break; | |
8046 | case SVt_PVIO: | |
8047 | SvANY(dstr) = new_XPVIO(); | |
8048 | SvCUR(dstr) = SvCUR(sstr); | |
8049 | SvLEN(dstr) = SvLEN(sstr); | |
8050 | SvIVX(dstr) = SvIVX(sstr); | |
8051 | SvNVX(dstr) = SvNVX(sstr); | |
8052 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
8053 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
8054 | if (SvROK(sstr)) | |
8055 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
8056 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
8057 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
8058 | else | |
8059 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
8060 | IoIFP(dstr) = fp_dup(IoIFP(sstr), IoTYPE(sstr)); | |
8061 | if (IoOFP(sstr) == IoIFP(sstr)) | |
8062 | IoOFP(dstr) = IoIFP(dstr); | |
8063 | else | |
8064 | IoOFP(dstr) = fp_dup(IoOFP(sstr), IoTYPE(sstr)); | |
8065 | /* PL_rsfp_filters entries have fake IoDIRP() */ | |
8066 | if (IoDIRP(sstr) && !(IoFLAGS(sstr) & IOf_FAKE_DIRP)) | |
8067 | IoDIRP(dstr) = dirp_dup(IoDIRP(sstr)); | |
8068 | else | |
8069 | IoDIRP(dstr) = IoDIRP(sstr); | |
8070 | IoLINES(dstr) = IoLINES(sstr); | |
8071 | IoPAGE(dstr) = IoPAGE(sstr); | |
8072 | IoPAGE_LEN(dstr) = IoPAGE_LEN(sstr); | |
8073 | IoLINES_LEFT(dstr) = IoLINES_LEFT(sstr); | |
8074 | IoTOP_NAME(dstr) = SAVEPV(IoTOP_NAME(sstr)); | |
8075 | IoTOP_GV(dstr) = gv_dup(IoTOP_GV(sstr)); | |
8076 | IoFMT_NAME(dstr) = SAVEPV(IoFMT_NAME(sstr)); | |
8077 | IoFMT_GV(dstr) = gv_dup(IoFMT_GV(sstr)); | |
8078 | IoBOTTOM_NAME(dstr) = SAVEPV(IoBOTTOM_NAME(sstr)); | |
8079 | IoBOTTOM_GV(dstr) = gv_dup(IoBOTTOM_GV(sstr)); | |
8080 | IoSUBPROCESS(dstr) = IoSUBPROCESS(sstr); | |
8081 | IoTYPE(dstr) = IoTYPE(sstr); | |
8082 | IoFLAGS(dstr) = IoFLAGS(sstr); | |
8083 | break; | |
8084 | case SVt_PVAV: | |
8085 | SvANY(dstr) = new_XPVAV(); | |
8086 | SvCUR(dstr) = SvCUR(sstr); | |
8087 | SvLEN(dstr) = SvLEN(sstr); | |
8088 | SvIVX(dstr) = SvIVX(sstr); | |
8089 | SvNVX(dstr) = SvNVX(sstr); | |
8090 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
8091 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
8092 | AvARYLEN((AV*)dstr) = sv_dup_inc(AvARYLEN((AV*)sstr)); | |
8093 | AvFLAGS((AV*)dstr) = AvFLAGS((AV*)sstr); | |
8094 | if (AvARRAY((AV*)sstr)) { | |
8095 | SV **dst_ary, **src_ary; | |
8096 | SSize_t items = AvFILLp((AV*)sstr) + 1; | |
8097 | ||
8098 | src_ary = AvARRAY((AV*)sstr); | |
8099 | Newz(0, dst_ary, AvMAX((AV*)sstr)+1, SV*); | |
8100 | ptr_table_store(PL_ptr_table, src_ary, dst_ary); | |
8101 | SvPVX(dstr) = (char*)dst_ary; | |
8102 | AvALLOC((AV*)dstr) = dst_ary; | |
8103 | if (AvREAL((AV*)sstr)) { | |
8104 | while (items-- > 0) | |
8105 | *dst_ary++ = sv_dup_inc(*src_ary++); | |
8106 | } | |
8107 | else { | |
8108 | while (items-- > 0) | |
8109 | *dst_ary++ = sv_dup(*src_ary++); | |
8110 | } | |
8111 | items = AvMAX((AV*)sstr) - AvFILLp((AV*)sstr); | |
8112 | while (items-- > 0) { | |
8113 | *dst_ary++ = &PL_sv_undef; | |
8114 | } | |
8115 | } | |
8116 | else { | |
8117 | SvPVX(dstr) = Nullch; | |
8118 | AvALLOC((AV*)dstr) = (SV**)NULL; | |
8119 | } | |
8120 | break; | |
8121 | case SVt_PVHV: | |
8122 | SvANY(dstr) = new_XPVHV(); | |
8123 | SvCUR(dstr) = SvCUR(sstr); | |
8124 | SvLEN(dstr) = SvLEN(sstr); | |
8125 | SvIVX(dstr) = SvIVX(sstr); | |
8126 | SvNVX(dstr) = SvNVX(sstr); | |
8127 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
8128 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
8129 | HvRITER((HV*)dstr) = HvRITER((HV*)sstr); | |
8130 | if (HvARRAY((HV*)sstr)) { | |
1d7c1841 GS |
8131 | STRLEN i = 0; |
8132 | XPVHV *dxhv = (XPVHV*)SvANY(dstr); | |
8133 | XPVHV *sxhv = (XPVHV*)SvANY(sstr); | |
8134 | Newz(0, dxhv->xhv_array, | |
8135 | PERL_HV_ARRAY_ALLOC_BYTES(dxhv->xhv_max+1), char); | |
8136 | while (i <= sxhv->xhv_max) { | |
8137 | ((HE**)dxhv->xhv_array)[i] = he_dup(((HE**)sxhv->xhv_array)[i], | |
8138 | !!HvSHAREKEYS(sstr)); | |
8139 | ++i; | |
8140 | } | |
8141 | dxhv->xhv_eiter = he_dup(sxhv->xhv_eiter, !!HvSHAREKEYS(sstr)); | |
8142 | } | |
8143 | else { | |
8144 | SvPVX(dstr) = Nullch; | |
8145 | HvEITER((HV*)dstr) = (HE*)NULL; | |
8146 | } | |
8147 | HvPMROOT((HV*)dstr) = HvPMROOT((HV*)sstr); /* XXX */ | |
8148 | HvNAME((HV*)dstr) = SAVEPV(HvNAME((HV*)sstr)); | |
8149 | break; | |
8150 | case SVt_PVFM: | |
8151 | SvANY(dstr) = new_XPVFM(); | |
8152 | FmLINES(dstr) = FmLINES(sstr); | |
8153 | goto dup_pvcv; | |
8154 | /* NOTREACHED */ | |
8155 | case SVt_PVCV: | |
8156 | SvANY(dstr) = new_XPVCV(); | |
8157 | dup_pvcv: | |
8158 | SvCUR(dstr) = SvCUR(sstr); | |
8159 | SvLEN(dstr) = SvLEN(sstr); | |
8160 | SvIVX(dstr) = SvIVX(sstr); | |
8161 | SvNVX(dstr) = SvNVX(sstr); | |
8162 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
8163 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
8164 | if (SvPVX(sstr) && SvLEN(sstr)) | |
8165 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
8166 | else | |
8167 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
8168 | CvSTASH(dstr) = hv_dup(CvSTASH(sstr));/* NOTE: not refcounted */ | |
8169 | CvSTART(dstr) = CvSTART(sstr); | |
8170 | CvROOT(dstr) = OpREFCNT_inc(CvROOT(sstr)); | |
8171 | CvXSUB(dstr) = CvXSUB(sstr); | |
8172 | CvXSUBANY(dstr) = CvXSUBANY(sstr); | |
8173 | CvGV(dstr) = gv_dup_inc(CvGV(sstr)); | |
8174 | CvDEPTH(dstr) = CvDEPTH(sstr); | |
8175 | if (CvPADLIST(sstr) && !AvREAL(CvPADLIST(sstr))) { | |
8176 | /* XXX padlists are real, but pretend to be not */ | |
8177 | AvREAL_on(CvPADLIST(sstr)); | |
8178 | CvPADLIST(dstr) = av_dup_inc(CvPADLIST(sstr)); | |
8179 | AvREAL_off(CvPADLIST(sstr)); | |
8180 | AvREAL_off(CvPADLIST(dstr)); | |
8181 | } | |
8182 | else | |
8183 | CvPADLIST(dstr) = av_dup_inc(CvPADLIST(sstr)); | |
8184 | CvOUTSIDE(dstr) = cv_dup_inc(CvOUTSIDE(sstr)); | |
8185 | CvFLAGS(dstr) = CvFLAGS(sstr); | |
8186 | break; | |
8187 | default: | |
8188 | Perl_croak(aTHX_ "Bizarre SvTYPE [%d]", SvTYPE(sstr)); | |
8189 | break; | |
8190 | } | |
8191 | ||
8192 | if (SvOBJECT(dstr) && SvTYPE(dstr) != SVt_PVIO) | |
8193 | ++PL_sv_objcount; | |
8194 | ||
8195 | return dstr; | |
8196 | } | |
8197 | ||
8198 | PERL_CONTEXT * | |
8199 | Perl_cx_dup(pTHX_ PERL_CONTEXT *cxs, I32 ix, I32 max) | |
8200 | { | |
8201 | PERL_CONTEXT *ncxs; | |
8202 | ||
8203 | if (!cxs) | |
8204 | return (PERL_CONTEXT*)NULL; | |
8205 | ||
8206 | /* look for it in the table first */ | |
8207 | ncxs = (PERL_CONTEXT*)ptr_table_fetch(PL_ptr_table, cxs); | |
8208 | if (ncxs) | |
8209 | return ncxs; | |
8210 | ||
8211 | /* create anew and remember what it is */ | |
8212 | Newz(56, ncxs, max + 1, PERL_CONTEXT); | |
8213 | ptr_table_store(PL_ptr_table, cxs, ncxs); | |
8214 | ||
8215 | while (ix >= 0) { | |
8216 | PERL_CONTEXT *cx = &cxs[ix]; | |
8217 | PERL_CONTEXT *ncx = &ncxs[ix]; | |
8218 | ncx->cx_type = cx->cx_type; | |
8219 | if (CxTYPE(cx) == CXt_SUBST) { | |
8220 | Perl_croak(aTHX_ "Cloning substitution context is unimplemented"); | |
8221 | } | |
8222 | else { | |
8223 | ncx->blk_oldsp = cx->blk_oldsp; | |
8224 | ncx->blk_oldcop = cx->blk_oldcop; | |
8225 | ncx->blk_oldretsp = cx->blk_oldretsp; | |
8226 | ncx->blk_oldmarksp = cx->blk_oldmarksp; | |
8227 | ncx->blk_oldscopesp = cx->blk_oldscopesp; | |
8228 | ncx->blk_oldpm = cx->blk_oldpm; | |
8229 | ncx->blk_gimme = cx->blk_gimme; | |
8230 | switch (CxTYPE(cx)) { | |
8231 | case CXt_SUB: | |
8232 | ncx->blk_sub.cv = (cx->blk_sub.olddepth == 0 | |
8233 | ? cv_dup_inc(cx->blk_sub.cv) | |
8234 | : cv_dup(cx->blk_sub.cv)); | |
8235 | ncx->blk_sub.argarray = (cx->blk_sub.hasargs | |
8236 | ? av_dup_inc(cx->blk_sub.argarray) | |
8237 | : Nullav); | |
8238 | ncx->blk_sub.savearray = av_dup(cx->blk_sub.savearray); | |
8239 | ncx->blk_sub.olddepth = cx->blk_sub.olddepth; | |
8240 | ncx->blk_sub.hasargs = cx->blk_sub.hasargs; | |
8241 | ncx->blk_sub.lval = cx->blk_sub.lval; | |
8242 | break; | |
8243 | case CXt_EVAL: | |
8244 | ncx->blk_eval.old_in_eval = cx->blk_eval.old_in_eval; | |
8245 | ncx->blk_eval.old_op_type = cx->blk_eval.old_op_type; | |
0f79a09d | 8246 | ncx->blk_eval.old_namesv = sv_dup_inc(cx->blk_eval.old_namesv); |
1d7c1841 GS |
8247 | ncx->blk_eval.old_eval_root = cx->blk_eval.old_eval_root; |
8248 | ncx->blk_eval.cur_text = sv_dup(cx->blk_eval.cur_text); | |
8249 | break; | |
8250 | case CXt_LOOP: | |
8251 | ncx->blk_loop.label = cx->blk_loop.label; | |
8252 | ncx->blk_loop.resetsp = cx->blk_loop.resetsp; | |
8253 | ncx->blk_loop.redo_op = cx->blk_loop.redo_op; | |
8254 | ncx->blk_loop.next_op = cx->blk_loop.next_op; | |
8255 | ncx->blk_loop.last_op = cx->blk_loop.last_op; | |
8256 | ncx->blk_loop.iterdata = (CxPADLOOP(cx) | |
8257 | ? cx->blk_loop.iterdata | |
8258 | : gv_dup((GV*)cx->blk_loop.iterdata)); | |
a4b82a6f GS |
8259 | ncx->blk_loop.oldcurpad |
8260 | = (SV**)ptr_table_fetch(PL_ptr_table, | |
8261 | cx->blk_loop.oldcurpad); | |
1d7c1841 GS |
8262 | ncx->blk_loop.itersave = sv_dup_inc(cx->blk_loop.itersave); |
8263 | ncx->blk_loop.iterlval = sv_dup_inc(cx->blk_loop.iterlval); | |
8264 | ncx->blk_loop.iterary = av_dup_inc(cx->blk_loop.iterary); | |
8265 | ncx->blk_loop.iterix = cx->blk_loop.iterix; | |
8266 | ncx->blk_loop.itermax = cx->blk_loop.itermax; | |
8267 | break; | |
8268 | case CXt_FORMAT: | |
8269 | ncx->blk_sub.cv = cv_dup(cx->blk_sub.cv); | |
8270 | ncx->blk_sub.gv = gv_dup(cx->blk_sub.gv); | |
8271 | ncx->blk_sub.dfoutgv = gv_dup_inc(cx->blk_sub.dfoutgv); | |
8272 | ncx->blk_sub.hasargs = cx->blk_sub.hasargs; | |
8273 | break; | |
8274 | case CXt_BLOCK: | |
8275 | case CXt_NULL: | |
8276 | break; | |
8277 | } | |
8278 | } | |
8279 | --ix; | |
8280 | } | |
8281 | return ncxs; | |
8282 | } | |
8283 | ||
8284 | PERL_SI * | |
8285 | Perl_si_dup(pTHX_ PERL_SI *si) | |
8286 | { | |
8287 | PERL_SI *nsi; | |
8288 | ||
8289 | if (!si) | |
8290 | return (PERL_SI*)NULL; | |
8291 | ||
8292 | /* look for it in the table first */ | |
8293 | nsi = (PERL_SI*)ptr_table_fetch(PL_ptr_table, si); | |
8294 | if (nsi) | |
8295 | return nsi; | |
8296 | ||
8297 | /* create anew and remember what it is */ | |
8298 | Newz(56, nsi, 1, PERL_SI); | |
8299 | ptr_table_store(PL_ptr_table, si, nsi); | |
8300 | ||
8301 | nsi->si_stack = av_dup_inc(si->si_stack); | |
8302 | nsi->si_cxix = si->si_cxix; | |
8303 | nsi->si_cxmax = si->si_cxmax; | |
8304 | nsi->si_cxstack = cx_dup(si->si_cxstack, si->si_cxix, si->si_cxmax); | |
8305 | nsi->si_type = si->si_type; | |
8306 | nsi->si_prev = si_dup(si->si_prev); | |
8307 | nsi->si_next = si_dup(si->si_next); | |
8308 | nsi->si_markoff = si->si_markoff; | |
8309 | ||
8310 | return nsi; | |
8311 | } | |
8312 | ||
8313 | #define POPINT(ss,ix) ((ss)[--(ix)].any_i32) | |
8314 | #define TOPINT(ss,ix) ((ss)[ix].any_i32) | |
8315 | #define POPLONG(ss,ix) ((ss)[--(ix)].any_long) | |
8316 | #define TOPLONG(ss,ix) ((ss)[ix].any_long) | |
8317 | #define POPIV(ss,ix) ((ss)[--(ix)].any_iv) | |
8318 | #define TOPIV(ss,ix) ((ss)[ix].any_iv) | |
8319 | #define POPPTR(ss,ix) ((ss)[--(ix)].any_ptr) | |
8320 | #define TOPPTR(ss,ix) ((ss)[ix].any_ptr) | |
8321 | #define POPDPTR(ss,ix) ((ss)[--(ix)].any_dptr) | |
8322 | #define TOPDPTR(ss,ix) ((ss)[ix].any_dptr) | |
8323 | #define POPDXPTR(ss,ix) ((ss)[--(ix)].any_dxptr) | |
8324 | #define TOPDXPTR(ss,ix) ((ss)[ix].any_dxptr) | |
8325 | ||
8326 | /* XXXXX todo */ | |
8327 | #define pv_dup_inc(p) SAVEPV(p) | |
8328 | #define pv_dup(p) SAVEPV(p) | |
8329 | #define svp_dup_inc(p,pp) any_dup(p,pp) | |
8330 | ||
8331 | void * | |
8332 | Perl_any_dup(pTHX_ void *v, PerlInterpreter *proto_perl) | |
8333 | { | |
8334 | void *ret; | |
8335 | ||
8336 | if (!v) | |
8337 | return (void*)NULL; | |
8338 | ||
8339 | /* look for it in the table first */ | |
8340 | ret = ptr_table_fetch(PL_ptr_table, v); | |
8341 | if (ret) | |
8342 | return ret; | |
8343 | ||
8344 | /* see if it is part of the interpreter structure */ | |
8345 | if (v >= (void*)proto_perl && v < (void*)(proto_perl+1)) | |
8346 | ret = (void*)(((char*)aTHXo) + (((char*)v) - (char*)proto_perl)); | |
8347 | else | |
8348 | ret = v; | |
8349 | ||
8350 | return ret; | |
8351 | } | |
8352 | ||
8353 | ANY * | |
8354 | Perl_ss_dup(pTHX_ PerlInterpreter *proto_perl) | |
8355 | { | |
8356 | ANY *ss = proto_perl->Tsavestack; | |
8357 | I32 ix = proto_perl->Tsavestack_ix; | |
8358 | I32 max = proto_perl->Tsavestack_max; | |
8359 | ANY *nss; | |
8360 | SV *sv; | |
8361 | GV *gv; | |
8362 | AV *av; | |
8363 | HV *hv; | |
8364 | void* ptr; | |
8365 | int intval; | |
8366 | long longval; | |
8367 | GP *gp; | |
8368 | IV iv; | |
8369 | I32 i; | |
8370 | char *c; | |
8371 | void (*dptr) (void*); | |
8372 | void (*dxptr) (pTHXo_ void*); | |
e977893f | 8373 | OP *o; |
1d7c1841 GS |
8374 | |
8375 | Newz(54, nss, max, ANY); | |
8376 | ||
8377 | while (ix > 0) { | |
8378 | i = POPINT(ss,ix); | |
8379 | TOPINT(nss,ix) = i; | |
8380 | switch (i) { | |
8381 | case SAVEt_ITEM: /* normal string */ | |
8382 | sv = (SV*)POPPTR(ss,ix); | |
8383 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8384 | sv = (SV*)POPPTR(ss,ix); | |
8385 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8386 | break; | |
8387 | case SAVEt_SV: /* scalar reference */ | |
8388 | sv = (SV*)POPPTR(ss,ix); | |
8389 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8390 | gv = (GV*)POPPTR(ss,ix); | |
8391 | TOPPTR(nss,ix) = gv_dup_inc(gv); | |
8392 | break; | |
f4dd75d9 GS |
8393 | case SAVEt_GENERIC_PVREF: /* generic char* */ |
8394 | c = (char*)POPPTR(ss,ix); | |
8395 | TOPPTR(nss,ix) = pv_dup(c); | |
8396 | ptr = POPPTR(ss,ix); | |
8397 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8398 | break; | |
1d7c1841 GS |
8399 | case SAVEt_GENERIC_SVREF: /* generic sv */ |
8400 | case SAVEt_SVREF: /* scalar reference */ | |
8401 | sv = (SV*)POPPTR(ss,ix); | |
8402 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8403 | ptr = POPPTR(ss,ix); | |
8404 | TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */ | |
8405 | break; | |
8406 | case SAVEt_AV: /* array reference */ | |
8407 | av = (AV*)POPPTR(ss,ix); | |
8408 | TOPPTR(nss,ix) = av_dup_inc(av); | |
8409 | gv = (GV*)POPPTR(ss,ix); | |
8410 | TOPPTR(nss,ix) = gv_dup(gv); | |
8411 | break; | |
8412 | case SAVEt_HV: /* hash reference */ | |
8413 | hv = (HV*)POPPTR(ss,ix); | |
8414 | TOPPTR(nss,ix) = hv_dup_inc(hv); | |
8415 | gv = (GV*)POPPTR(ss,ix); | |
8416 | TOPPTR(nss,ix) = gv_dup(gv); | |
8417 | break; | |
8418 | case SAVEt_INT: /* int reference */ | |
8419 | ptr = POPPTR(ss,ix); | |
8420 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8421 | intval = (int)POPINT(ss,ix); | |
8422 | TOPINT(nss,ix) = intval; | |
8423 | break; | |
8424 | case SAVEt_LONG: /* long reference */ | |
8425 | ptr = POPPTR(ss,ix); | |
8426 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8427 | longval = (long)POPLONG(ss,ix); | |
8428 | TOPLONG(nss,ix) = longval; | |
8429 | break; | |
8430 | case SAVEt_I32: /* I32 reference */ | |
8431 | case SAVEt_I16: /* I16 reference */ | |
8432 | case SAVEt_I8: /* I8 reference */ | |
8433 | ptr = POPPTR(ss,ix); | |
8434 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8435 | i = POPINT(ss,ix); | |
8436 | TOPINT(nss,ix) = i; | |
8437 | break; | |
8438 | case SAVEt_IV: /* IV reference */ | |
8439 | ptr = POPPTR(ss,ix); | |
8440 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8441 | iv = POPIV(ss,ix); | |
8442 | TOPIV(nss,ix) = iv; | |
8443 | break; | |
8444 | case SAVEt_SPTR: /* SV* reference */ | |
8445 | ptr = POPPTR(ss,ix); | |
8446 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8447 | sv = (SV*)POPPTR(ss,ix); | |
8448 | TOPPTR(nss,ix) = sv_dup(sv); | |
8449 | break; | |
8450 | case SAVEt_VPTR: /* random* reference */ | |
8451 | ptr = POPPTR(ss,ix); | |
8452 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8453 | ptr = POPPTR(ss,ix); | |
8454 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8455 | break; | |
8456 | case SAVEt_PPTR: /* char* reference */ | |
8457 | ptr = POPPTR(ss,ix); | |
8458 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8459 | c = (char*)POPPTR(ss,ix); | |
8460 | TOPPTR(nss,ix) = pv_dup(c); | |
8461 | break; | |
8462 | case SAVEt_HPTR: /* HV* reference */ | |
8463 | ptr = POPPTR(ss,ix); | |
8464 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8465 | hv = (HV*)POPPTR(ss,ix); | |
8466 | TOPPTR(nss,ix) = hv_dup(hv); | |
8467 | break; | |
8468 | case SAVEt_APTR: /* AV* reference */ | |
8469 | ptr = POPPTR(ss,ix); | |
8470 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8471 | av = (AV*)POPPTR(ss,ix); | |
8472 | TOPPTR(nss,ix) = av_dup(av); | |
8473 | break; | |
8474 | case SAVEt_NSTAB: | |
8475 | gv = (GV*)POPPTR(ss,ix); | |
8476 | TOPPTR(nss,ix) = gv_dup(gv); | |
8477 | break; | |
8478 | case SAVEt_GP: /* scalar reference */ | |
8479 | gp = (GP*)POPPTR(ss,ix); | |
8480 | TOPPTR(nss,ix) = gp = gp_dup(gp); | |
8481 | (void)GpREFCNT_inc(gp); | |
8482 | gv = (GV*)POPPTR(ss,ix); | |
8483 | TOPPTR(nss,ix) = gv_dup_inc(c); | |
8484 | c = (char*)POPPTR(ss,ix); | |
8485 | TOPPTR(nss,ix) = pv_dup(c); | |
8486 | iv = POPIV(ss,ix); | |
8487 | TOPIV(nss,ix) = iv; | |
8488 | iv = POPIV(ss,ix); | |
8489 | TOPIV(nss,ix) = iv; | |
8490 | break; | |
8491 | case SAVEt_FREESV: | |
8492 | sv = (SV*)POPPTR(ss,ix); | |
8493 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8494 | break; | |
8495 | case SAVEt_FREEOP: | |
8496 | ptr = POPPTR(ss,ix); | |
8497 | if (ptr && (((OP*)ptr)->op_private & OPpREFCOUNTED)) { | |
8498 | /* these are assumed to be refcounted properly */ | |
8499 | switch (((OP*)ptr)->op_type) { | |
8500 | case OP_LEAVESUB: | |
8501 | case OP_LEAVESUBLV: | |
8502 | case OP_LEAVEEVAL: | |
8503 | case OP_LEAVE: | |
8504 | case OP_SCOPE: | |
8505 | case OP_LEAVEWRITE: | |
e977893f GS |
8506 | TOPPTR(nss,ix) = ptr; |
8507 | o = (OP*)ptr; | |
8508 | OpREFCNT_inc(o); | |
1d7c1841 GS |
8509 | break; |
8510 | default: | |
8511 | TOPPTR(nss,ix) = Nullop; | |
8512 | break; | |
8513 | } | |
8514 | } | |
8515 | else | |
8516 | TOPPTR(nss,ix) = Nullop; | |
8517 | break; | |
8518 | case SAVEt_FREEPV: | |
8519 | c = (char*)POPPTR(ss,ix); | |
8520 | TOPPTR(nss,ix) = pv_dup_inc(c); | |
8521 | break; | |
8522 | case SAVEt_CLEARSV: | |
8523 | longval = POPLONG(ss,ix); | |
8524 | TOPLONG(nss,ix) = longval; | |
8525 | break; | |
8526 | case SAVEt_DELETE: | |
8527 | hv = (HV*)POPPTR(ss,ix); | |
8528 | TOPPTR(nss,ix) = hv_dup_inc(hv); | |
8529 | c = (char*)POPPTR(ss,ix); | |
8530 | TOPPTR(nss,ix) = pv_dup_inc(c); | |
8531 | i = POPINT(ss,ix); | |
8532 | TOPINT(nss,ix) = i; | |
8533 | break; | |
8534 | case SAVEt_DESTRUCTOR: | |
8535 | ptr = POPPTR(ss,ix); | |
8536 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */ | |
8537 | dptr = POPDPTR(ss,ix); | |
ef75a179 | 8538 | TOPDPTR(nss,ix) = (void (*)(void*))any_dup((void *)dptr, proto_perl); |
1d7c1841 GS |
8539 | break; |
8540 | case SAVEt_DESTRUCTOR_X: | |
8541 | ptr = POPPTR(ss,ix); | |
8542 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */ | |
8543 | dxptr = POPDXPTR(ss,ix); | |
ef75a179 | 8544 | TOPDXPTR(nss,ix) = (void (*)(pTHXo_ void*))any_dup((void *)dxptr, proto_perl); |
1d7c1841 GS |
8545 | break; |
8546 | case SAVEt_REGCONTEXT: | |
8547 | case SAVEt_ALLOC: | |
8548 | i = POPINT(ss,ix); | |
8549 | TOPINT(nss,ix) = i; | |
8550 | ix -= i; | |
8551 | break; | |
8552 | case SAVEt_STACK_POS: /* Position on Perl stack */ | |
8553 | i = POPINT(ss,ix); | |
8554 | TOPINT(nss,ix) = i; | |
8555 | break; | |
8556 | case SAVEt_AELEM: /* array element */ | |
8557 | sv = (SV*)POPPTR(ss,ix); | |
8558 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8559 | i = POPINT(ss,ix); | |
8560 | TOPINT(nss,ix) = i; | |
8561 | av = (AV*)POPPTR(ss,ix); | |
8562 | TOPPTR(nss,ix) = av_dup_inc(av); | |
8563 | break; | |
8564 | case SAVEt_HELEM: /* hash element */ | |
8565 | sv = (SV*)POPPTR(ss,ix); | |
8566 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8567 | sv = (SV*)POPPTR(ss,ix); | |
8568 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8569 | hv = (HV*)POPPTR(ss,ix); | |
8570 | TOPPTR(nss,ix) = hv_dup_inc(hv); | |
8571 | break; | |
8572 | case SAVEt_OP: | |
8573 | ptr = POPPTR(ss,ix); | |
8574 | TOPPTR(nss,ix) = ptr; | |
8575 | break; | |
8576 | case SAVEt_HINTS: | |
8577 | i = POPINT(ss,ix); | |
8578 | TOPINT(nss,ix) = i; | |
8579 | break; | |
c4410b1b GS |
8580 | case SAVEt_COMPPAD: |
8581 | av = (AV*)POPPTR(ss,ix); | |
8582 | TOPPTR(nss,ix) = av_dup(av); | |
8583 | break; | |
c3564e5c GS |
8584 | case SAVEt_PADSV: |
8585 | longval = (long)POPLONG(ss,ix); | |
8586 | TOPLONG(nss,ix) = longval; | |
8587 | ptr = POPPTR(ss,ix); | |
8588 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8589 | sv = (SV*)POPPTR(ss,ix); | |
8590 | TOPPTR(nss,ix) = sv_dup(sv); | |
8591 | break; | |
1d7c1841 GS |
8592 | default: |
8593 | Perl_croak(aTHX_ "panic: ss_dup inconsistency"); | |
8594 | } | |
8595 | } | |
8596 | ||
8597 | return nss; | |
8598 | } | |
8599 | ||
8600 | #ifdef PERL_OBJECT | |
8601 | #include "XSUB.h" | |
8602 | #endif | |
8603 | ||
8604 | PerlInterpreter * | |
8605 | perl_clone(PerlInterpreter *proto_perl, UV flags) | |
8606 | { | |
8607 | #ifdef PERL_OBJECT | |
8608 | CPerlObj *pPerl = (CPerlObj*)proto_perl; | |
8609 | #endif | |
8610 | ||
8611 | #ifdef PERL_IMPLICIT_SYS | |
8612 | return perl_clone_using(proto_perl, flags, | |
8613 | proto_perl->IMem, | |
8614 | proto_perl->IMemShared, | |
8615 | proto_perl->IMemParse, | |
8616 | proto_perl->IEnv, | |
8617 | proto_perl->IStdIO, | |
8618 | proto_perl->ILIO, | |
8619 | proto_perl->IDir, | |
8620 | proto_perl->ISock, | |
8621 | proto_perl->IProc); | |
8622 | } | |
8623 | ||
8624 | PerlInterpreter * | |
8625 | perl_clone_using(PerlInterpreter *proto_perl, UV flags, | |
8626 | struct IPerlMem* ipM, struct IPerlMem* ipMS, | |
8627 | struct IPerlMem* ipMP, struct IPerlEnv* ipE, | |
8628 | struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO, | |
8629 | struct IPerlDir* ipD, struct IPerlSock* ipS, | |
8630 | struct IPerlProc* ipP) | |
8631 | { | |
8632 | /* XXX many of the string copies here can be optimized if they're | |
8633 | * constants; they need to be allocated as common memory and just | |
8634 | * their pointers copied. */ | |
8635 | ||
8636 | IV i; | |
1d7c1841 GS |
8637 | # ifdef PERL_OBJECT |
8638 | CPerlObj *pPerl = new(ipM) CPerlObj(ipM, ipMS, ipMP, ipE, ipStd, ipLIO, | |
8639 | ipD, ipS, ipP); | |
ba869deb | 8640 | PERL_SET_THX(pPerl); |
1d7c1841 GS |
8641 | # else /* !PERL_OBJECT */ |
8642 | PerlInterpreter *my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter)); | |
ba869deb | 8643 | PERL_SET_THX(my_perl); |
1d7c1841 GS |
8644 | |
8645 | # ifdef DEBUGGING | |
8646 | memset(my_perl, 0xab, sizeof(PerlInterpreter)); | |
8647 | PL_markstack = 0; | |
8648 | PL_scopestack = 0; | |
8649 | PL_savestack = 0; | |
8650 | PL_retstack = 0; | |
66fe0623 | 8651 | PL_sig_pending = 0; |
1d7c1841 GS |
8652 | # else /* !DEBUGGING */ |
8653 | Zero(my_perl, 1, PerlInterpreter); | |
8654 | # endif /* DEBUGGING */ | |
8655 | ||
8656 | /* host pointers */ | |
8657 | PL_Mem = ipM; | |
8658 | PL_MemShared = ipMS; | |
8659 | PL_MemParse = ipMP; | |
8660 | PL_Env = ipE; | |
8661 | PL_StdIO = ipStd; | |
8662 | PL_LIO = ipLIO; | |
8663 | PL_Dir = ipD; | |
8664 | PL_Sock = ipS; | |
8665 | PL_Proc = ipP; | |
8666 | # endif /* PERL_OBJECT */ | |
8667 | #else /* !PERL_IMPLICIT_SYS */ | |
8668 | IV i; | |
1d7c1841 | 8669 | PerlInterpreter *my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter)); |
ba869deb | 8670 | PERL_SET_THX(my_perl); |
1d7c1841 GS |
8671 | |
8672 | # ifdef DEBUGGING | |
8673 | memset(my_perl, 0xab, sizeof(PerlInterpreter)); | |
8674 | PL_markstack = 0; | |
8675 | PL_scopestack = 0; | |
8676 | PL_savestack = 0; | |
8677 | PL_retstack = 0; | |
66fe0623 | 8678 | PL_sig_pending = 0; |
1d7c1841 GS |
8679 | # else /* !DEBUGGING */ |
8680 | Zero(my_perl, 1, PerlInterpreter); | |
8681 | # endif /* DEBUGGING */ | |
8682 | #endif /* PERL_IMPLICIT_SYS */ | |
8683 | ||
8684 | /* arena roots */ | |
8685 | PL_xiv_arenaroot = NULL; | |
8686 | PL_xiv_root = NULL; | |
612f20c3 | 8687 | PL_xnv_arenaroot = NULL; |
1d7c1841 | 8688 | PL_xnv_root = NULL; |
612f20c3 | 8689 | PL_xrv_arenaroot = NULL; |
1d7c1841 | 8690 | PL_xrv_root = NULL; |
612f20c3 | 8691 | PL_xpv_arenaroot = NULL; |
1d7c1841 | 8692 | PL_xpv_root = NULL; |
612f20c3 | 8693 | PL_xpviv_arenaroot = NULL; |
1d7c1841 | 8694 | PL_xpviv_root = NULL; |
612f20c3 | 8695 | PL_xpvnv_arenaroot = NULL; |
1d7c1841 | 8696 | PL_xpvnv_root = NULL; |
612f20c3 | 8697 | PL_xpvcv_arenaroot = NULL; |
1d7c1841 | 8698 | PL_xpvcv_root = NULL; |
612f20c3 | 8699 | PL_xpvav_arenaroot = NULL; |
1d7c1841 | 8700 | PL_xpvav_root = NULL; |
612f20c3 | 8701 | PL_xpvhv_arenaroot = NULL; |
1d7c1841 | 8702 | PL_xpvhv_root = NULL; |
612f20c3 | 8703 | PL_xpvmg_arenaroot = NULL; |
1d7c1841 | 8704 | PL_xpvmg_root = NULL; |
612f20c3 | 8705 | PL_xpvlv_arenaroot = NULL; |
1d7c1841 | 8706 | PL_xpvlv_root = NULL; |
612f20c3 | 8707 | PL_xpvbm_arenaroot = NULL; |
1d7c1841 | 8708 | PL_xpvbm_root = NULL; |
612f20c3 | 8709 | PL_he_arenaroot = NULL; |
1d7c1841 GS |
8710 | PL_he_root = NULL; |
8711 | PL_nice_chunk = NULL; | |
8712 | PL_nice_chunk_size = 0; | |
8713 | PL_sv_count = 0; | |
8714 | PL_sv_objcount = 0; | |
8715 | PL_sv_root = Nullsv; | |
8716 | PL_sv_arenaroot = Nullsv; | |
8717 | ||
8718 | PL_debug = proto_perl->Idebug; | |
8719 | ||
8720 | /* create SV map for pointer relocation */ | |
8721 | PL_ptr_table = ptr_table_new(); | |
8722 | ||
8723 | /* initialize these special pointers as early as possible */ | |
8724 | SvANY(&PL_sv_undef) = NULL; | |
8725 | SvREFCNT(&PL_sv_undef) = (~(U32)0)/2; | |
8726 | SvFLAGS(&PL_sv_undef) = SVf_READONLY|SVt_NULL; | |
8727 | ptr_table_store(PL_ptr_table, &proto_perl->Isv_undef, &PL_sv_undef); | |
8728 | ||
8729 | #ifdef PERL_OBJECT | |
8730 | SvUPGRADE(&PL_sv_no, SVt_PVNV); | |
8731 | #else | |
8732 | SvANY(&PL_sv_no) = new_XPVNV(); | |
8733 | #endif | |
8734 | SvREFCNT(&PL_sv_no) = (~(U32)0)/2; | |
8735 | SvFLAGS(&PL_sv_no) = SVp_NOK|SVf_NOK|SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV; | |
8736 | SvPVX(&PL_sv_no) = SAVEPVN(PL_No, 0); | |
8737 | SvCUR(&PL_sv_no) = 0; | |
8738 | SvLEN(&PL_sv_no) = 1; | |
8739 | SvNVX(&PL_sv_no) = 0; | |
8740 | ptr_table_store(PL_ptr_table, &proto_perl->Isv_no, &PL_sv_no); | |
8741 | ||
8742 | #ifdef PERL_OBJECT | |
8743 | SvUPGRADE(&PL_sv_yes, SVt_PVNV); | |
8744 | #else | |
8745 | SvANY(&PL_sv_yes) = new_XPVNV(); | |
8746 | #endif | |
8747 | SvREFCNT(&PL_sv_yes) = (~(U32)0)/2; | |
8748 | SvFLAGS(&PL_sv_yes) = SVp_NOK|SVf_NOK|SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV; | |
8749 | SvPVX(&PL_sv_yes) = SAVEPVN(PL_Yes, 1); | |
8750 | SvCUR(&PL_sv_yes) = 1; | |
8751 | SvLEN(&PL_sv_yes) = 2; | |
8752 | SvNVX(&PL_sv_yes) = 1; | |
8753 | ptr_table_store(PL_ptr_table, &proto_perl->Isv_yes, &PL_sv_yes); | |
8754 | ||
8755 | /* create shared string table */ | |
8756 | PL_strtab = newHV(); | |
8757 | HvSHAREKEYS_off(PL_strtab); | |
8758 | hv_ksplit(PL_strtab, 512); | |
8759 | ptr_table_store(PL_ptr_table, proto_perl->Istrtab, PL_strtab); | |
8760 | ||
8761 | PL_compiling = proto_perl->Icompiling; | |
8762 | PL_compiling.cop_stashpv = SAVEPV(PL_compiling.cop_stashpv); | |
8763 | PL_compiling.cop_file = SAVEPV(PL_compiling.cop_file); | |
8764 | ptr_table_store(PL_ptr_table, &proto_perl->Icompiling, &PL_compiling); | |
8765 | if (!specialWARN(PL_compiling.cop_warnings)) | |
8766 | PL_compiling.cop_warnings = sv_dup_inc(PL_compiling.cop_warnings); | |
ac27b0f5 NIS |
8767 | if (!specialCopIO(PL_compiling.cop_io)) |
8768 | PL_compiling.cop_io = sv_dup_inc(PL_compiling.cop_io); | |
1d7c1841 GS |
8769 | PL_curcop = (COP*)any_dup(proto_perl->Tcurcop, proto_perl); |
8770 | ||
8771 | /* pseudo environmental stuff */ | |
8772 | PL_origargc = proto_perl->Iorigargc; | |
8773 | i = PL_origargc; | |
8774 | New(0, PL_origargv, i+1, char*); | |
8775 | PL_origargv[i] = '\0'; | |
8776 | while (i-- > 0) { | |
8777 | PL_origargv[i] = SAVEPV(proto_perl->Iorigargv[i]); | |
8778 | } | |
8779 | PL_envgv = gv_dup(proto_perl->Ienvgv); | |
8780 | PL_incgv = gv_dup(proto_perl->Iincgv); | |
8781 | PL_hintgv = gv_dup(proto_perl->Ihintgv); | |
8782 | PL_origfilename = SAVEPV(proto_perl->Iorigfilename); | |
8783 | PL_diehook = sv_dup_inc(proto_perl->Idiehook); | |
8784 | PL_warnhook = sv_dup_inc(proto_perl->Iwarnhook); | |
8785 | ||
8786 | /* switches */ | |
8787 | PL_minus_c = proto_perl->Iminus_c; | |
a7cb1f99 | 8788 | PL_patchlevel = sv_dup_inc(proto_perl->Ipatchlevel); |
1d7c1841 GS |
8789 | PL_localpatches = proto_perl->Ilocalpatches; |
8790 | PL_splitstr = proto_perl->Isplitstr; | |
8791 | PL_preprocess = proto_perl->Ipreprocess; | |
8792 | PL_minus_n = proto_perl->Iminus_n; | |
8793 | PL_minus_p = proto_perl->Iminus_p; | |
8794 | PL_minus_l = proto_perl->Iminus_l; | |
8795 | PL_minus_a = proto_perl->Iminus_a; | |
8796 | PL_minus_F = proto_perl->Iminus_F; | |
8797 | PL_doswitches = proto_perl->Idoswitches; | |
8798 | PL_dowarn = proto_perl->Idowarn; | |
8799 | PL_doextract = proto_perl->Idoextract; | |
8800 | PL_sawampersand = proto_perl->Isawampersand; | |
8801 | PL_unsafe = proto_perl->Iunsafe; | |
8802 | PL_inplace = SAVEPV(proto_perl->Iinplace); | |
8803 | PL_e_script = sv_dup_inc(proto_perl->Ie_script); | |
8804 | PL_perldb = proto_perl->Iperldb; | |
8805 | PL_perl_destruct_level = proto_perl->Iperl_destruct_level; | |
8806 | ||
8807 | /* magical thingies */ | |
8808 | /* XXX time(&PL_basetime) when asked for? */ | |
8809 | PL_basetime = proto_perl->Ibasetime; | |
8810 | PL_formfeed = sv_dup(proto_perl->Iformfeed); | |
8811 | ||
8812 | PL_maxsysfd = proto_perl->Imaxsysfd; | |
8813 | PL_multiline = proto_perl->Imultiline; | |
8814 | PL_statusvalue = proto_perl->Istatusvalue; | |
8815 | #ifdef VMS | |
8816 | PL_statusvalue_vms = proto_perl->Istatusvalue_vms; | |
8817 | #endif | |
8818 | ||
8819 | /* shortcuts to various I/O objects */ | |
8820 | PL_stdingv = gv_dup(proto_perl->Istdingv); | |
8821 | PL_stderrgv = gv_dup(proto_perl->Istderrgv); | |
8822 | PL_defgv = gv_dup(proto_perl->Idefgv); | |
8823 | PL_argvgv = gv_dup(proto_perl->Iargvgv); | |
8824 | PL_argvoutgv = gv_dup(proto_perl->Iargvoutgv); | |
8825 | PL_argvout_stack = av_dup(proto_perl->Iargvout_stack); | |
8826 | ||
8827 | /* shortcuts to regexp stuff */ | |
8828 | PL_replgv = gv_dup(proto_perl->Ireplgv); | |
8829 | ||
8830 | /* shortcuts to misc objects */ | |
8831 | PL_errgv = gv_dup(proto_perl->Ierrgv); | |
8832 | ||
8833 | /* shortcuts to debugging objects */ | |
8834 | PL_DBgv = gv_dup(proto_perl->IDBgv); | |
8835 | PL_DBline = gv_dup(proto_perl->IDBline); | |
8836 | PL_DBsub = gv_dup(proto_perl->IDBsub); | |
8837 | PL_DBsingle = sv_dup(proto_perl->IDBsingle); | |
8838 | PL_DBtrace = sv_dup(proto_perl->IDBtrace); | |
8839 | PL_DBsignal = sv_dup(proto_perl->IDBsignal); | |
8840 | PL_lineary = av_dup(proto_perl->Ilineary); | |
8841 | PL_dbargs = av_dup(proto_perl->Idbargs); | |
8842 | ||
8843 | /* symbol tables */ | |
8844 | PL_defstash = hv_dup_inc(proto_perl->Tdefstash); | |
8845 | PL_curstash = hv_dup(proto_perl->Tcurstash); | |
8846 | PL_debstash = hv_dup(proto_perl->Idebstash); | |
8847 | PL_globalstash = hv_dup(proto_perl->Iglobalstash); | |
8848 | PL_curstname = sv_dup_inc(proto_perl->Icurstname); | |
8849 | ||
8850 | PL_beginav = av_dup_inc(proto_perl->Ibeginav); | |
8851 | PL_endav = av_dup_inc(proto_perl->Iendav); | |
7d30b5c4 | 8852 | PL_checkav = av_dup_inc(proto_perl->Icheckav); |
1d7c1841 GS |
8853 | PL_initav = av_dup_inc(proto_perl->Iinitav); |
8854 | ||
8855 | PL_sub_generation = proto_perl->Isub_generation; | |
8856 | ||
8857 | /* funky return mechanisms */ | |
8858 | PL_forkprocess = proto_perl->Iforkprocess; | |
8859 | ||
8860 | /* subprocess state */ | |
8861 | PL_fdpid = av_dup_inc(proto_perl->Ifdpid); | |
8862 | ||
8863 | /* internal state */ | |
8864 | PL_tainting = proto_perl->Itainting; | |
8865 | PL_maxo = proto_perl->Imaxo; | |
8866 | if (proto_perl->Iop_mask) | |
8867 | PL_op_mask = SAVEPVN(proto_perl->Iop_mask, PL_maxo); | |
8868 | else | |
8869 | PL_op_mask = Nullch; | |
8870 | ||
8871 | /* current interpreter roots */ | |
8872 | PL_main_cv = cv_dup_inc(proto_perl->Imain_cv); | |
8873 | PL_main_root = OpREFCNT_inc(proto_perl->Imain_root); | |
8874 | PL_main_start = proto_perl->Imain_start; | |
e977893f | 8875 | PL_eval_root = proto_perl->Ieval_root; |
1d7c1841 GS |
8876 | PL_eval_start = proto_perl->Ieval_start; |
8877 | ||
8878 | /* runtime control stuff */ | |
8879 | PL_curcopdb = (COP*)any_dup(proto_perl->Icurcopdb, proto_perl); | |
8880 | PL_copline = proto_perl->Icopline; | |
8881 | ||
8882 | PL_filemode = proto_perl->Ifilemode; | |
8883 | PL_lastfd = proto_perl->Ilastfd; | |
8884 | PL_oldname = proto_perl->Ioldname; /* XXX not quite right */ | |
8885 | PL_Argv = NULL; | |
8886 | PL_Cmd = Nullch; | |
8887 | PL_gensym = proto_perl->Igensym; | |
8888 | PL_preambled = proto_perl->Ipreambled; | |
8889 | PL_preambleav = av_dup_inc(proto_perl->Ipreambleav); | |
8890 | PL_laststatval = proto_perl->Ilaststatval; | |
8891 | PL_laststype = proto_perl->Ilaststype; | |
8892 | PL_mess_sv = Nullsv; | |
8893 | ||
7889fe52 | 8894 | PL_ors_sv = sv_dup_inc(proto_perl->Iors_sv); |
1d7c1841 GS |
8895 | PL_ofmt = SAVEPV(proto_perl->Iofmt); |
8896 | ||
8897 | /* interpreter atexit processing */ | |
8898 | PL_exitlistlen = proto_perl->Iexitlistlen; | |
8899 | if (PL_exitlistlen) { | |
8900 | New(0, PL_exitlist, PL_exitlistlen, PerlExitListEntry); | |
8901 | Copy(proto_perl->Iexitlist, PL_exitlist, PL_exitlistlen, PerlExitListEntry); | |
8902 | } | |
8903 | else | |
8904 | PL_exitlist = (PerlExitListEntry*)NULL; | |
8905 | PL_modglobal = hv_dup_inc(proto_perl->Imodglobal); | |
8906 | ||
8907 | PL_profiledata = NULL; | |
8908 | PL_rsfp = fp_dup(proto_perl->Irsfp, '<'); | |
8909 | /* PL_rsfp_filters entries have fake IoDIRP() */ | |
8910 | PL_rsfp_filters = av_dup_inc(proto_perl->Irsfp_filters); | |
8911 | ||
8912 | PL_compcv = cv_dup(proto_perl->Icompcv); | |
8913 | PL_comppad = av_dup(proto_perl->Icomppad); | |
8914 | PL_comppad_name = av_dup(proto_perl->Icomppad_name); | |
8915 | PL_comppad_name_fill = proto_perl->Icomppad_name_fill; | |
8916 | PL_comppad_name_floor = proto_perl->Icomppad_name_floor; | |
8917 | PL_curpad = (SV**)ptr_table_fetch(PL_ptr_table, | |
8918 | proto_perl->Tcurpad); | |
8919 | ||
8920 | #ifdef HAVE_INTERP_INTERN | |
8921 | sys_intern_dup(&proto_perl->Isys_intern, &PL_sys_intern); | |
8922 | #endif | |
8923 | ||
8924 | /* more statics moved here */ | |
8925 | PL_generation = proto_perl->Igeneration; | |
8926 | PL_DBcv = cv_dup(proto_perl->IDBcv); | |
1d7c1841 GS |
8927 | |
8928 | PL_in_clean_objs = proto_perl->Iin_clean_objs; | |
8929 | PL_in_clean_all = proto_perl->Iin_clean_all; | |
8930 | ||
8931 | PL_uid = proto_perl->Iuid; | |
8932 | PL_euid = proto_perl->Ieuid; | |
8933 | PL_gid = proto_perl->Igid; | |
8934 | PL_egid = proto_perl->Iegid; | |
8935 | PL_nomemok = proto_perl->Inomemok; | |
8936 | PL_an = proto_perl->Ian; | |
8937 | PL_cop_seqmax = proto_perl->Icop_seqmax; | |
8938 | PL_op_seqmax = proto_perl->Iop_seqmax; | |
8939 | PL_evalseq = proto_perl->Ievalseq; | |
8940 | PL_origenviron = proto_perl->Iorigenviron; /* XXX not quite right */ | |
8941 | PL_origalen = proto_perl->Iorigalen; | |
8942 | PL_pidstatus = newHV(); /* XXX flag for cloning? */ | |
8943 | PL_osname = SAVEPV(proto_perl->Iosname); | |
8944 | PL_sh_path = SAVEPV(proto_perl->Ish_path); | |
8945 | PL_sighandlerp = proto_perl->Isighandlerp; | |
8946 | ||
8947 | ||
8948 | PL_runops = proto_perl->Irunops; | |
8949 | ||
8950 | Copy(proto_perl->Itokenbuf, PL_tokenbuf, 256, char); | |
8951 | ||
8952 | #ifdef CSH | |
8953 | PL_cshlen = proto_perl->Icshlen; | |
8954 | PL_cshname = SAVEPVN(proto_perl->Icshname, PL_cshlen); | |
8955 | #endif | |
8956 | ||
8957 | PL_lex_state = proto_perl->Ilex_state; | |
8958 | PL_lex_defer = proto_perl->Ilex_defer; | |
8959 | PL_lex_expect = proto_perl->Ilex_expect; | |
8960 | PL_lex_formbrack = proto_perl->Ilex_formbrack; | |
8961 | PL_lex_dojoin = proto_perl->Ilex_dojoin; | |
8962 | PL_lex_starts = proto_perl->Ilex_starts; | |
8963 | PL_lex_stuff = sv_dup_inc(proto_perl->Ilex_stuff); | |
8964 | PL_lex_repl = sv_dup_inc(proto_perl->Ilex_repl); | |
8965 | PL_lex_op = proto_perl->Ilex_op; | |
8966 | PL_lex_inpat = proto_perl->Ilex_inpat; | |
8967 | PL_lex_inwhat = proto_perl->Ilex_inwhat; | |
8968 | PL_lex_brackets = proto_perl->Ilex_brackets; | |
8969 | i = (PL_lex_brackets < 120 ? 120 : PL_lex_brackets); | |
8970 | PL_lex_brackstack = SAVEPVN(proto_perl->Ilex_brackstack,i); | |
8971 | PL_lex_casemods = proto_perl->Ilex_casemods; | |
8972 | i = (PL_lex_casemods < 12 ? 12 : PL_lex_casemods); | |
8973 | PL_lex_casestack = SAVEPVN(proto_perl->Ilex_casestack,i); | |
8974 | ||
8975 | Copy(proto_perl->Inextval, PL_nextval, 5, YYSTYPE); | |
8976 | Copy(proto_perl->Inexttype, PL_nexttype, 5, I32); | |
8977 | PL_nexttoke = proto_perl->Inexttoke; | |
8978 | ||
8979 | PL_linestr = sv_dup_inc(proto_perl->Ilinestr); | |
8980 | i = proto_perl->Ibufptr - SvPVX(proto_perl->Ilinestr); | |
8981 | PL_bufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
8982 | i = proto_perl->Ioldbufptr - SvPVX(proto_perl->Ilinestr); | |
8983 | PL_oldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
8984 | i = proto_perl->Ioldoldbufptr - SvPVX(proto_perl->Ilinestr); | |
8985 | PL_oldoldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
8986 | PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr); | |
8987 | i = proto_perl->Ilinestart - SvPVX(proto_perl->Ilinestr); | |
8988 | PL_linestart = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
8989 | PL_pending_ident = proto_perl->Ipending_ident; | |
8990 | PL_sublex_info = proto_perl->Isublex_info; /* XXX not quite right */ | |
8991 | ||
8992 | PL_expect = proto_perl->Iexpect; | |
8993 | ||
8994 | PL_multi_start = proto_perl->Imulti_start; | |
8995 | PL_multi_end = proto_perl->Imulti_end; | |
8996 | PL_multi_open = proto_perl->Imulti_open; | |
8997 | PL_multi_close = proto_perl->Imulti_close; | |
8998 | ||
8999 | PL_error_count = proto_perl->Ierror_count; | |
9000 | PL_subline = proto_perl->Isubline; | |
9001 | PL_subname = sv_dup_inc(proto_perl->Isubname); | |
9002 | ||
9003 | PL_min_intro_pending = proto_perl->Imin_intro_pending; | |
9004 | PL_max_intro_pending = proto_perl->Imax_intro_pending; | |
9005 | PL_padix = proto_perl->Ipadix; | |
9006 | PL_padix_floor = proto_perl->Ipadix_floor; | |
9007 | PL_pad_reset_pending = proto_perl->Ipad_reset_pending; | |
9008 | ||
9009 | i = proto_perl->Ilast_uni - SvPVX(proto_perl->Ilinestr); | |
9010 | PL_last_uni = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
9011 | i = proto_perl->Ilast_lop - SvPVX(proto_perl->Ilinestr); | |
9012 | PL_last_lop = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
9013 | PL_last_lop_op = proto_perl->Ilast_lop_op; | |
9014 | PL_in_my = proto_perl->Iin_my; | |
9015 | PL_in_my_stash = hv_dup(proto_perl->Iin_my_stash); | |
9016 | #ifdef FCRYPT | |
9017 | PL_cryptseen = proto_perl->Icryptseen; | |
9018 | #endif | |
9019 | ||
9020 | PL_hints = proto_perl->Ihints; | |
9021 | ||
9022 | PL_amagic_generation = proto_perl->Iamagic_generation; | |
9023 | ||
9024 | #ifdef USE_LOCALE_COLLATE | |
9025 | PL_collation_ix = proto_perl->Icollation_ix; | |
9026 | PL_collation_name = SAVEPV(proto_perl->Icollation_name); | |
9027 | PL_collation_standard = proto_perl->Icollation_standard; | |
9028 | PL_collxfrm_base = proto_perl->Icollxfrm_base; | |
9029 | PL_collxfrm_mult = proto_perl->Icollxfrm_mult; | |
9030 | #endif /* USE_LOCALE_COLLATE */ | |
9031 | ||
9032 | #ifdef USE_LOCALE_NUMERIC | |
9033 | PL_numeric_name = SAVEPV(proto_perl->Inumeric_name); | |
9034 | PL_numeric_standard = proto_perl->Inumeric_standard; | |
9035 | PL_numeric_local = proto_perl->Inumeric_local; | |
ac634a9a | 9036 | PL_numeric_radix = sv_dup_inc(proto_perl->Inumeric_radix); |
1d7c1841 GS |
9037 | #endif /* !USE_LOCALE_NUMERIC */ |
9038 | ||
9039 | /* utf8 character classes */ | |
9040 | PL_utf8_alnum = sv_dup_inc(proto_perl->Iutf8_alnum); | |
9041 | PL_utf8_alnumc = sv_dup_inc(proto_perl->Iutf8_alnumc); | |
9042 | PL_utf8_ascii = sv_dup_inc(proto_perl->Iutf8_ascii); | |
9043 | PL_utf8_alpha = sv_dup_inc(proto_perl->Iutf8_alpha); | |
9044 | PL_utf8_space = sv_dup_inc(proto_perl->Iutf8_space); | |
9045 | PL_utf8_cntrl = sv_dup_inc(proto_perl->Iutf8_cntrl); | |
9046 | PL_utf8_graph = sv_dup_inc(proto_perl->Iutf8_graph); | |
9047 | PL_utf8_digit = sv_dup_inc(proto_perl->Iutf8_digit); | |
9048 | PL_utf8_upper = sv_dup_inc(proto_perl->Iutf8_upper); | |
9049 | PL_utf8_lower = sv_dup_inc(proto_perl->Iutf8_lower); | |
9050 | PL_utf8_print = sv_dup_inc(proto_perl->Iutf8_print); | |
9051 | PL_utf8_punct = sv_dup_inc(proto_perl->Iutf8_punct); | |
9052 | PL_utf8_xdigit = sv_dup_inc(proto_perl->Iutf8_xdigit); | |
9053 | PL_utf8_mark = sv_dup_inc(proto_perl->Iutf8_mark); | |
9054 | PL_utf8_toupper = sv_dup_inc(proto_perl->Iutf8_toupper); | |
9055 | PL_utf8_totitle = sv_dup_inc(proto_perl->Iutf8_totitle); | |
9056 | PL_utf8_tolower = sv_dup_inc(proto_perl->Iutf8_tolower); | |
9057 | ||
9058 | /* swatch cache */ | |
9059 | PL_last_swash_hv = Nullhv; /* reinits on demand */ | |
9060 | PL_last_swash_klen = 0; | |
9061 | PL_last_swash_key[0]= '\0'; | |
9062 | PL_last_swash_tmps = (U8*)NULL; | |
9063 | PL_last_swash_slen = 0; | |
9064 | ||
9065 | /* perly.c globals */ | |
9066 | PL_yydebug = proto_perl->Iyydebug; | |
9067 | PL_yynerrs = proto_perl->Iyynerrs; | |
9068 | PL_yyerrflag = proto_perl->Iyyerrflag; | |
9069 | PL_yychar = proto_perl->Iyychar; | |
9070 | PL_yyval = proto_perl->Iyyval; | |
9071 | PL_yylval = proto_perl->Iyylval; | |
9072 | ||
9073 | PL_glob_index = proto_perl->Iglob_index; | |
9074 | PL_srand_called = proto_perl->Isrand_called; | |
9075 | PL_uudmap['M'] = 0; /* reinits on demand */ | |
9076 | PL_bitcount = Nullch; /* reinits on demand */ | |
9077 | ||
66fe0623 NIS |
9078 | if (proto_perl->Ipsig_pend) { |
9079 | Newz(0, PL_psig_pend, SIG_SIZE, int); | |
9dd79c3f | 9080 | } |
66fe0623 NIS |
9081 | else { |
9082 | PL_psig_pend = (int*)NULL; | |
9083 | } | |
9084 | ||
1d7c1841 | 9085 | if (proto_perl->Ipsig_ptr) { |
76d3c696 JH |
9086 | Newz(0, PL_psig_ptr, SIG_SIZE, SV*); |
9087 | Newz(0, PL_psig_name, SIG_SIZE, SV*); | |
76d3c696 JH |
9088 | for (i = 1; i < SIG_SIZE; i++) { |
9089 | PL_psig_ptr[i] = sv_dup_inc(proto_perl->Ipsig_ptr[i]); | |
1d7c1841 GS |
9090 | PL_psig_name[i] = sv_dup_inc(proto_perl->Ipsig_name[i]); |
9091 | } | |
9092 | } | |
9093 | else { | |
9094 | PL_psig_ptr = (SV**)NULL; | |
9095 | PL_psig_name = (SV**)NULL; | |
9096 | } | |
9097 | ||
9098 | /* thrdvar.h stuff */ | |
9099 | ||
a0739874 | 9100 | if (flags & CLONEf_COPY_STACKS) { |
1d7c1841 GS |
9101 | /* next allocation will be PL_tmps_stack[PL_tmps_ix+1] */ |
9102 | PL_tmps_ix = proto_perl->Ttmps_ix; | |
9103 | PL_tmps_max = proto_perl->Ttmps_max; | |
9104 | PL_tmps_floor = proto_perl->Ttmps_floor; | |
9105 | Newz(50, PL_tmps_stack, PL_tmps_max, SV*); | |
9106 | i = 0; | |
9107 | while (i <= PL_tmps_ix) { | |
9108 | PL_tmps_stack[i] = sv_dup_inc(proto_perl->Ttmps_stack[i]); | |
9109 | ++i; | |
9110 | } | |
9111 | ||
9112 | /* next PUSHMARK() sets *(PL_markstack_ptr+1) */ | |
9113 | i = proto_perl->Tmarkstack_max - proto_perl->Tmarkstack; | |
9114 | Newz(54, PL_markstack, i, I32); | |
9115 | PL_markstack_max = PL_markstack + (proto_perl->Tmarkstack_max | |
9116 | - proto_perl->Tmarkstack); | |
9117 | PL_markstack_ptr = PL_markstack + (proto_perl->Tmarkstack_ptr | |
9118 | - proto_perl->Tmarkstack); | |
9119 | Copy(proto_perl->Tmarkstack, PL_markstack, | |
9120 | PL_markstack_ptr - PL_markstack + 1, I32); | |
9121 | ||
9122 | /* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix] | |
9123 | * NOTE: unlike the others! */ | |
9124 | PL_scopestack_ix = proto_perl->Tscopestack_ix; | |
9125 | PL_scopestack_max = proto_perl->Tscopestack_max; | |
9126 | Newz(54, PL_scopestack, PL_scopestack_max, I32); | |
9127 | Copy(proto_perl->Tscopestack, PL_scopestack, PL_scopestack_ix, I32); | |
9128 | ||
9129 | /* next push_return() sets PL_retstack[PL_retstack_ix] | |
9130 | * NOTE: unlike the others! */ | |
9131 | PL_retstack_ix = proto_perl->Tretstack_ix; | |
9132 | PL_retstack_max = proto_perl->Tretstack_max; | |
9133 | Newz(54, PL_retstack, PL_retstack_max, OP*); | |
9134 | Copy(proto_perl->Tretstack, PL_retstack, PL_retstack_ix, I32); | |
9135 | ||
9136 | /* NOTE: si_dup() looks at PL_markstack */ | |
9137 | PL_curstackinfo = si_dup(proto_perl->Tcurstackinfo); | |
9138 | ||
9139 | /* PL_curstack = PL_curstackinfo->si_stack; */ | |
9140 | PL_curstack = av_dup(proto_perl->Tcurstack); | |
9141 | PL_mainstack = av_dup(proto_perl->Tmainstack); | |
9142 | ||
9143 | /* next PUSHs() etc. set *(PL_stack_sp+1) */ | |
9144 | PL_stack_base = AvARRAY(PL_curstack); | |
9145 | PL_stack_sp = PL_stack_base + (proto_perl->Tstack_sp | |
9146 | - proto_perl->Tstack_base); | |
9147 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); | |
9148 | ||
9149 | /* next SSPUSHFOO() sets PL_savestack[PL_savestack_ix] | |
9150 | * NOTE: unlike the others! */ | |
9151 | PL_savestack_ix = proto_perl->Tsavestack_ix; | |
9152 | PL_savestack_max = proto_perl->Tsavestack_max; | |
9153 | /*Newz(54, PL_savestack, PL_savestack_max, ANY);*/ | |
9154 | PL_savestack = ss_dup(proto_perl); | |
9155 | } | |
9156 | else { | |
9157 | init_stacks(); | |
985e7056 | 9158 | ENTER; /* perl_destruct() wants to LEAVE; */ |
1d7c1841 GS |
9159 | } |
9160 | ||
9161 | PL_start_env = proto_perl->Tstart_env; /* XXXXXX */ | |
9162 | PL_top_env = &PL_start_env; | |
9163 | ||
9164 | PL_op = proto_perl->Top; | |
9165 | ||
9166 | PL_Sv = Nullsv; | |
9167 | PL_Xpv = (XPV*)NULL; | |
9168 | PL_na = proto_perl->Tna; | |
9169 | ||
9170 | PL_statbuf = proto_perl->Tstatbuf; | |
9171 | PL_statcache = proto_perl->Tstatcache; | |
9172 | PL_statgv = gv_dup(proto_perl->Tstatgv); | |
9173 | PL_statname = sv_dup_inc(proto_perl->Tstatname); | |
9174 | #ifdef HAS_TIMES | |
9175 | PL_timesbuf = proto_perl->Ttimesbuf; | |
9176 | #endif | |
9177 | ||
9178 | PL_tainted = proto_perl->Ttainted; | |
9179 | PL_curpm = proto_perl->Tcurpm; /* XXX No PMOP ref count */ | |
9180 | PL_nrs = sv_dup_inc(proto_perl->Tnrs); | |
9181 | PL_rs = sv_dup_inc(proto_perl->Trs); | |
9182 | PL_last_in_gv = gv_dup(proto_perl->Tlast_in_gv); | |
7889fe52 | 9183 | PL_ofs_sv = sv_dup_inc(proto_perl->Tofs_sv); |
1d7c1841 GS |
9184 | PL_defoutgv = gv_dup_inc(proto_perl->Tdefoutgv); |
9185 | PL_chopset = proto_perl->Tchopset; /* XXX never deallocated */ | |
9186 | PL_toptarget = sv_dup_inc(proto_perl->Ttoptarget); | |
9187 | PL_bodytarget = sv_dup_inc(proto_perl->Tbodytarget); | |
9188 | PL_formtarget = sv_dup(proto_perl->Tformtarget); | |
9189 | ||
9190 | PL_restartop = proto_perl->Trestartop; | |
9191 | PL_in_eval = proto_perl->Tin_eval; | |
9192 | PL_delaymagic = proto_perl->Tdelaymagic; | |
9193 | PL_dirty = proto_perl->Tdirty; | |
9194 | PL_localizing = proto_perl->Tlocalizing; | |
9195 | ||
14dd3ad8 | 9196 | #ifdef PERL_FLEXIBLE_EXCEPTIONS |
1d7c1841 | 9197 | PL_protect = proto_perl->Tprotect; |
14dd3ad8 | 9198 | #endif |
1d7c1841 GS |
9199 | PL_errors = sv_dup_inc(proto_perl->Terrors); |
9200 | PL_av_fetch_sv = Nullsv; | |
9201 | PL_hv_fetch_sv = Nullsv; | |
9202 | Zero(&PL_hv_fetch_ent_mh, 1, HE); /* XXX */ | |
9203 | PL_modcount = proto_perl->Tmodcount; | |
9204 | PL_lastgotoprobe = Nullop; | |
9205 | PL_dumpindent = proto_perl->Tdumpindent; | |
9206 | ||
9207 | PL_sortcop = (OP*)any_dup(proto_perl->Tsortcop, proto_perl); | |
9208 | PL_sortstash = hv_dup(proto_perl->Tsortstash); | |
9209 | PL_firstgv = gv_dup(proto_perl->Tfirstgv); | |
9210 | PL_secondgv = gv_dup(proto_perl->Tsecondgv); | |
9211 | PL_sortcxix = proto_perl->Tsortcxix; | |
9212 | PL_efloatbuf = Nullch; /* reinits on demand */ | |
9213 | PL_efloatsize = 0; /* reinits on demand */ | |
9214 | ||
9215 | /* regex stuff */ | |
9216 | ||
9217 | PL_screamfirst = NULL; | |
9218 | PL_screamnext = NULL; | |
9219 | PL_maxscream = -1; /* reinits on demand */ | |
9220 | PL_lastscream = Nullsv; | |
9221 | ||
9222 | PL_watchaddr = NULL; | |
9223 | PL_watchok = Nullch; | |
9224 | ||
9225 | PL_regdummy = proto_perl->Tregdummy; | |
9226 | PL_regcomp_parse = Nullch; | |
9227 | PL_regxend = Nullch; | |
9228 | PL_regcode = (regnode*)NULL; | |
9229 | PL_regnaughty = 0; | |
9230 | PL_regsawback = 0; | |
9231 | PL_regprecomp = Nullch; | |
9232 | PL_regnpar = 0; | |
9233 | PL_regsize = 0; | |
9234 | PL_regflags = 0; | |
9235 | PL_regseen = 0; | |
9236 | PL_seen_zerolen = 0; | |
9237 | PL_seen_evals = 0; | |
9238 | PL_regcomp_rx = (regexp*)NULL; | |
9239 | PL_extralen = 0; | |
9240 | PL_colorset = 0; /* reinits PL_colors[] */ | |
9241 | /*PL_colors[6] = {0,0,0,0,0,0};*/ | |
9242 | PL_reg_whilem_seen = 0; | |
9243 | PL_reginput = Nullch; | |
9244 | PL_regbol = Nullch; | |
9245 | PL_regeol = Nullch; | |
9246 | PL_regstartp = (I32*)NULL; | |
9247 | PL_regendp = (I32*)NULL; | |
9248 | PL_reglastparen = (U32*)NULL; | |
9249 | PL_regtill = Nullch; | |
9250 | PL_regprev = '\n'; | |
9251 | PL_reg_start_tmp = (char**)NULL; | |
9252 | PL_reg_start_tmpl = 0; | |
9253 | PL_regdata = (struct reg_data*)NULL; | |
9254 | PL_bostr = Nullch; | |
9255 | PL_reg_flags = 0; | |
9256 | PL_reg_eval_set = 0; | |
9257 | PL_regnarrate = 0; | |
9258 | PL_regprogram = (regnode*)NULL; | |
9259 | PL_regindent = 0; | |
9260 | PL_regcc = (CURCUR*)NULL; | |
9261 | PL_reg_call_cc = (struct re_cc_state*)NULL; | |
9262 | PL_reg_re = (regexp*)NULL; | |
9263 | PL_reg_ganch = Nullch; | |
9264 | PL_reg_sv = Nullsv; | |
9265 | PL_reg_magic = (MAGIC*)NULL; | |
9266 | PL_reg_oldpos = 0; | |
9267 | PL_reg_oldcurpm = (PMOP*)NULL; | |
9268 | PL_reg_curpm = (PMOP*)NULL; | |
9269 | PL_reg_oldsaved = Nullch; | |
9270 | PL_reg_oldsavedlen = 0; | |
9271 | PL_reg_maxiter = 0; | |
9272 | PL_reg_leftiter = 0; | |
9273 | PL_reg_poscache = Nullch; | |
9274 | PL_reg_poscache_size= 0; | |
9275 | ||
9276 | /* RE engine - function pointers */ | |
9277 | PL_regcompp = proto_perl->Tregcompp; | |
9278 | PL_regexecp = proto_perl->Tregexecp; | |
9279 | PL_regint_start = proto_perl->Tregint_start; | |
9280 | PL_regint_string = proto_perl->Tregint_string; | |
9281 | PL_regfree = proto_perl->Tregfree; | |
9282 | ||
9283 | PL_reginterp_cnt = 0; | |
9284 | PL_reg_starttry = 0; | |
9285 | ||
a0739874 DM |
9286 | if (!(flags & CLONEf_KEEP_PTR_TABLE)) { |
9287 | ptr_table_free(PL_ptr_table); | |
9288 | PL_ptr_table = NULL; | |
9289 | } | |
9290 | ||
1d7c1841 GS |
9291 | #ifdef PERL_OBJECT |
9292 | return (PerlInterpreter*)pPerl; | |
9293 | #else | |
9294 | return my_perl; | |
9295 | #endif | |
9296 | } | |
9297 | ||
9298 | #else /* !USE_ITHREADS */ | |
51371543 GS |
9299 | |
9300 | #ifdef PERL_OBJECT | |
51371543 GS |
9301 | #include "XSUB.h" |
9302 | #endif | |
9303 | ||
1d7c1841 GS |
9304 | #endif /* USE_ITHREADS */ |
9305 | ||
51371543 GS |
9306 | static void |
9307 | do_report_used(pTHXo_ SV *sv) | |
9308 | { | |
9309 | if (SvTYPE(sv) != SVTYPEMASK) { | |
bf49b057 | 9310 | PerlIO_printf(Perl_debug_log, "****\n"); |
51371543 GS |
9311 | sv_dump(sv); |
9312 | } | |
9313 | } | |
9314 | ||
9315 | static void | |
9316 | do_clean_objs(pTHXo_ SV *sv) | |
9317 | { | |
9318 | SV* rv; | |
9319 | ||
9320 | if (SvROK(sv) && SvOBJECT(rv = SvRV(sv))) { | |
9321 | DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(sv));) | |
8b6e653b HS |
9322 | if (SvWEAKREF(sv)) { |
9323 | sv_del_backref(sv); | |
9324 | SvWEAKREF_off(sv); | |
9325 | SvRV(sv) = 0; | |
9326 | } else { | |
9327 | SvROK_off(sv); | |
9328 | SvRV(sv) = 0; | |
9329 | SvREFCNT_dec(rv); | |
9330 | } | |
51371543 GS |
9331 | } |
9332 | ||
9333 | /* XXX Might want to check arrays, etc. */ | |
9334 | } | |
9335 | ||
9336 | #ifndef DISABLE_DESTRUCTOR_KLUDGE | |
9337 | static void | |
9338 | do_clean_named_objs(pTHXo_ SV *sv) | |
9339 | { | |
f472eb5c | 9340 | if (SvTYPE(sv) == SVt_PVGV && GvGP(sv)) { |
51371543 | 9341 | if ( SvOBJECT(GvSV(sv)) || |
155aba94 GS |
9342 | (GvAV(sv) && SvOBJECT(GvAV(sv))) || |
9343 | (GvHV(sv) && SvOBJECT(GvHV(sv))) || | |
9344 | (GvIO(sv) && SvOBJECT(GvIO(sv))) || | |
9345 | (GvCV(sv) && SvOBJECT(GvCV(sv))) ) | |
51371543 GS |
9346 | { |
9347 | DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning named glob object:\n "), sv_dump(sv));) | |
9348 | SvREFCNT_dec(sv); | |
9349 | } | |
9350 | } | |
9351 | } | |
9352 | #endif | |
9353 | ||
9354 | static void | |
9355 | do_clean_all(pTHXo_ SV *sv) | |
9356 | { | |
1d7c1841 | 9357 | DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) );) |
51371543 GS |
9358 | SvFLAGS(sv) |= SVf_BREAK; |
9359 | SvREFCNT_dec(sv); | |
9360 | } | |
8af02333 | 9361 |