Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | /* sv.c |
79072805 | 2 | * |
bc89e66f | 3 | * Copyright (c) 1991-2001, Larry Wall |
79072805 LW |
4 | * |
5 | * You may distribute under the terms of either the GNU General Public | |
6 | * License or the Artistic License, as specified in the README file. | |
7 | * | |
a0d0e21e LW |
8 | */ |
9 | ||
10 | /* | |
11 | * "I wonder what the Entish is for 'yes' and 'no'," he thought. | |
79072805 LW |
12 | */ |
13 | ||
14 | #include "EXTERN.h" | |
864dbfa3 | 15 | #define PERL_IN_SV_C |
79072805 | 16 | #include "perl.h" |
79072805 | 17 | |
51371543 | 18 | #define FCALL *f |
6fc92669 | 19 | #define SV_CHECK_THINKFIRST(sv) if (SvTHINKFIRST(sv)) sv_force_normal(sv) |
2c5424a7 | 20 | |
51371543 GS |
21 | static void do_report_used(pTHXo_ SV *sv); |
22 | static void do_clean_objs(pTHXo_ SV *sv); | |
23 | #ifndef DISABLE_DESTRUCTOR_KLUDGE | |
24 | static void do_clean_named_objs(pTHXo_ SV *sv); | |
25 | #endif | |
26 | static void do_clean_all(pTHXo_ SV *sv); | |
27 | ||
4561caa4 CS |
28 | /* |
29 | * "A time to plant, and a time to uproot what was planted..." | |
30 | */ | |
31 | ||
053fc874 GS |
32 | #define plant_SV(p) \ |
33 | STMT_START { \ | |
34 | SvANY(p) = (void *)PL_sv_root; \ | |
35 | SvFLAGS(p) = SVTYPEMASK; \ | |
36 | PL_sv_root = (p); \ | |
37 | --PL_sv_count; \ | |
38 | } STMT_END | |
a0d0e21e | 39 | |
fba3b22e | 40 | /* sv_mutex must be held while calling uproot_SV() */ |
053fc874 GS |
41 | #define uproot_SV(p) \ |
42 | STMT_START { \ | |
43 | (p) = PL_sv_root; \ | |
44 | PL_sv_root = (SV*)SvANY(p); \ | |
45 | ++PL_sv_count; \ | |
46 | } STMT_END | |
47 | ||
48 | #define new_SV(p) \ | |
49 | STMT_START { \ | |
50 | LOCK_SV_MUTEX; \ | |
51 | if (PL_sv_root) \ | |
52 | uproot_SV(p); \ | |
53 | else \ | |
54 | (p) = more_sv(); \ | |
55 | UNLOCK_SV_MUTEX; \ | |
56 | SvANY(p) = 0; \ | |
57 | SvREFCNT(p) = 1; \ | |
58 | SvFLAGS(p) = 0; \ | |
59 | } STMT_END | |
463ee0b2 | 60 | |
a0d0e21e | 61 | #ifdef DEBUGGING |
4561caa4 | 62 | |
053fc874 GS |
63 | #define del_SV(p) \ |
64 | STMT_START { \ | |
65 | LOCK_SV_MUTEX; \ | |
66 | if (PL_debug & 32768) \ | |
67 | del_sv(p); \ | |
68 | else \ | |
69 | plant_SV(p); \ | |
70 | UNLOCK_SV_MUTEX; \ | |
71 | } STMT_END | |
a0d0e21e | 72 | |
76e3520e | 73 | STATIC void |
cea2e8a9 | 74 | S_del_sv(pTHX_ SV *p) |
463ee0b2 | 75 | { |
3280af22 | 76 | if (PL_debug & 32768) { |
4633a7c4 | 77 | SV* sva; |
a0d0e21e LW |
78 | SV* sv; |
79 | SV* svend; | |
80 | int ok = 0; | |
3280af22 | 81 | for (sva = PL_sv_arenaroot; sva; sva = (SV *) SvANY(sva)) { |
4633a7c4 LW |
82 | sv = sva + 1; |
83 | svend = &sva[SvREFCNT(sva)]; | |
a0d0e21e LW |
84 | if (p >= sv && p < svend) |
85 | ok = 1; | |
86 | } | |
87 | if (!ok) { | |
0453d815 PM |
88 | if (ckWARN_d(WARN_INTERNAL)) |
89 | Perl_warner(aTHX_ WARN_INTERNAL, | |
1d7c1841 GS |
90 | "Attempt to free non-arena SV: 0x%"UVxf, |
91 | PTR2UV(p)); | |
a0d0e21e LW |
92 | return; |
93 | } | |
94 | } | |
4561caa4 | 95 | plant_SV(p); |
463ee0b2 | 96 | } |
a0d0e21e | 97 | |
4561caa4 CS |
98 | #else /* ! DEBUGGING */ |
99 | ||
100 | #define del_SV(p) plant_SV(p) | |
101 | ||
102 | #endif /* DEBUGGING */ | |
463ee0b2 | 103 | |
4633a7c4 | 104 | void |
864dbfa3 | 105 | Perl_sv_add_arena(pTHX_ char *ptr, U32 size, U32 flags) |
463ee0b2 | 106 | { |
4633a7c4 | 107 | SV* sva = (SV*)ptr; |
463ee0b2 LW |
108 | register SV* sv; |
109 | register SV* svend; | |
14dd3ad8 | 110 | Zero(ptr, size, char); |
4633a7c4 LW |
111 | |
112 | /* The first SV in an arena isn't an SV. */ | |
3280af22 | 113 | SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */ |
4633a7c4 LW |
114 | SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */ |
115 | SvFLAGS(sva) = flags; /* FAKE if not to be freed */ | |
116 | ||
3280af22 NIS |
117 | PL_sv_arenaroot = sva; |
118 | PL_sv_root = sva + 1; | |
4633a7c4 LW |
119 | |
120 | svend = &sva[SvREFCNT(sva) - 1]; | |
121 | sv = sva + 1; | |
463ee0b2 | 122 | while (sv < svend) { |
a0d0e21e | 123 | SvANY(sv) = (void *)(SV*)(sv + 1); |
8990e307 | 124 | SvFLAGS(sv) = SVTYPEMASK; |
463ee0b2 LW |
125 | sv++; |
126 | } | |
127 | SvANY(sv) = 0; | |
4633a7c4 LW |
128 | SvFLAGS(sv) = SVTYPEMASK; |
129 | } | |
130 | ||
fba3b22e | 131 | /* sv_mutex must be held while calling more_sv() */ |
76e3520e | 132 | STATIC SV* |
cea2e8a9 | 133 | S_more_sv(pTHX) |
4633a7c4 | 134 | { |
4561caa4 CS |
135 | register SV* sv; |
136 | ||
3280af22 NIS |
137 | if (PL_nice_chunk) { |
138 | sv_add_arena(PL_nice_chunk, PL_nice_chunk_size, 0); | |
139 | PL_nice_chunk = Nullch; | |
c07a80fd | 140 | } |
1edc1566 | 141 | else { |
142 | char *chunk; /* must use New here to match call to */ | |
143 | New(704,chunk,1008,char); /* Safefree() in sv_free_arenas() */ | |
144 | sv_add_arena(chunk, 1008, 0); | |
145 | } | |
4561caa4 CS |
146 | uproot_SV(sv); |
147 | return sv; | |
463ee0b2 LW |
148 | } |
149 | ||
76e3520e | 150 | STATIC void |
cea2e8a9 | 151 | S_visit(pTHX_ SVFUNC_t f) |
8990e307 | 152 | { |
4633a7c4 | 153 | SV* sva; |
8990e307 LW |
154 | SV* sv; |
155 | register SV* svend; | |
156 | ||
3280af22 | 157 | for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) { |
4633a7c4 | 158 | svend = &sva[SvREFCNT(sva)]; |
4561caa4 CS |
159 | for (sv = sva + 1; sv < svend; ++sv) { |
160 | if (SvTYPE(sv) != SVTYPEMASK) | |
51371543 | 161 | (FCALL)(aTHXo_ sv); |
8990e307 LW |
162 | } |
163 | } | |
164 | } | |
165 | ||
166 | void | |
864dbfa3 | 167 | Perl_sv_report_used(pTHX) |
4561caa4 | 168 | { |
0b94c7bb | 169 | visit(do_report_used); |
4561caa4 CS |
170 | } |
171 | ||
4561caa4 | 172 | void |
864dbfa3 | 173 | Perl_sv_clean_objs(pTHX) |
4561caa4 | 174 | { |
3280af22 | 175 | PL_in_clean_objs = TRUE; |
0b94c7bb | 176 | visit(do_clean_objs); |
4561caa4 | 177 | #ifndef DISABLE_DESTRUCTOR_KLUDGE |
2d0f3c12 | 178 | /* some barnacles may yet remain, clinging to typeglobs */ |
0b94c7bb | 179 | visit(do_clean_named_objs); |
4561caa4 | 180 | #endif |
3280af22 | 181 | PL_in_clean_objs = FALSE; |
4561caa4 CS |
182 | } |
183 | ||
8990e307 | 184 | void |
864dbfa3 | 185 | Perl_sv_clean_all(pTHX) |
8990e307 | 186 | { |
3280af22 | 187 | PL_in_clean_all = TRUE; |
0b94c7bb | 188 | visit(do_clean_all); |
3280af22 | 189 | PL_in_clean_all = FALSE; |
8990e307 | 190 | } |
463ee0b2 | 191 | |
4633a7c4 | 192 | void |
864dbfa3 | 193 | Perl_sv_free_arenas(pTHX) |
4633a7c4 LW |
194 | { |
195 | SV* sva; | |
196 | SV* svanext; | |
612f20c3 | 197 | XPV *arena, *arenanext; |
4633a7c4 LW |
198 | |
199 | /* Free arenas here, but be careful about fake ones. (We assume | |
200 | contiguity of the fake ones with the corresponding real ones.) */ | |
201 | ||
3280af22 | 202 | for (sva = PL_sv_arenaroot; sva; sva = svanext) { |
4633a7c4 LW |
203 | svanext = (SV*) SvANY(sva); |
204 | while (svanext && SvFAKE(svanext)) | |
205 | svanext = (SV*) SvANY(svanext); | |
206 | ||
207 | if (!SvFAKE(sva)) | |
1edc1566 | 208 | Safefree((void *)sva); |
4633a7c4 | 209 | } |
5f05dabc | 210 | |
612f20c3 GS |
211 | for (arena = PL_xiv_arenaroot; arena; arena = arenanext) { |
212 | arenanext = (XPV*)arena->xpv_pv; | |
213 | Safefree(arena); | |
214 | } | |
215 | PL_xiv_arenaroot = 0; | |
216 | ||
217 | for (arena = PL_xnv_arenaroot; arena; arena = arenanext) { | |
218 | arenanext = (XPV*)arena->xpv_pv; | |
219 | Safefree(arena); | |
220 | } | |
221 | PL_xnv_arenaroot = 0; | |
222 | ||
223 | for (arena = PL_xrv_arenaroot; arena; arena = arenanext) { | |
224 | arenanext = (XPV*)arena->xpv_pv; | |
225 | Safefree(arena); | |
226 | } | |
227 | PL_xrv_arenaroot = 0; | |
228 | ||
229 | for (arena = PL_xpv_arenaroot; arena; arena = arenanext) { | |
230 | arenanext = (XPV*)arena->xpv_pv; | |
231 | Safefree(arena); | |
232 | } | |
233 | PL_xpv_arenaroot = 0; | |
234 | ||
235 | for (arena = (XPV*)PL_xpviv_arenaroot; arena; arena = arenanext) { | |
236 | arenanext = (XPV*)arena->xpv_pv; | |
237 | Safefree(arena); | |
238 | } | |
239 | PL_xpviv_arenaroot = 0; | |
240 | ||
241 | for (arena = (XPV*)PL_xpvnv_arenaroot; arena; arena = arenanext) { | |
242 | arenanext = (XPV*)arena->xpv_pv; | |
243 | Safefree(arena); | |
244 | } | |
245 | PL_xpvnv_arenaroot = 0; | |
246 | ||
247 | for (arena = (XPV*)PL_xpvcv_arenaroot; arena; arena = arenanext) { | |
248 | arenanext = (XPV*)arena->xpv_pv; | |
249 | Safefree(arena); | |
250 | } | |
251 | PL_xpvcv_arenaroot = 0; | |
252 | ||
253 | for (arena = (XPV*)PL_xpvav_arenaroot; arena; arena = arenanext) { | |
254 | arenanext = (XPV*)arena->xpv_pv; | |
255 | Safefree(arena); | |
256 | } | |
257 | PL_xpvav_arenaroot = 0; | |
258 | ||
259 | for (arena = (XPV*)PL_xpvhv_arenaroot; arena; arena = arenanext) { | |
260 | arenanext = (XPV*)arena->xpv_pv; | |
261 | Safefree(arena); | |
262 | } | |
263 | PL_xpvhv_arenaroot = 0; | |
264 | ||
265 | for (arena = (XPV*)PL_xpvmg_arenaroot; arena; arena = arenanext) { | |
266 | arenanext = (XPV*)arena->xpv_pv; | |
267 | Safefree(arena); | |
268 | } | |
269 | PL_xpvmg_arenaroot = 0; | |
270 | ||
271 | for (arena = (XPV*)PL_xpvlv_arenaroot; arena; arena = arenanext) { | |
272 | arenanext = (XPV*)arena->xpv_pv; | |
273 | Safefree(arena); | |
274 | } | |
275 | PL_xpvlv_arenaroot = 0; | |
276 | ||
277 | for (arena = (XPV*)PL_xpvbm_arenaroot; arena; arena = arenanext) { | |
278 | arenanext = (XPV*)arena->xpv_pv; | |
279 | Safefree(arena); | |
280 | } | |
281 | PL_xpvbm_arenaroot = 0; | |
282 | ||
283 | for (arena = (XPV*)PL_he_arenaroot; arena; arena = arenanext) { | |
284 | arenanext = (XPV*)arena->xpv_pv; | |
285 | Safefree(arena); | |
286 | } | |
287 | PL_he_arenaroot = 0; | |
288 | ||
3280af22 NIS |
289 | if (PL_nice_chunk) |
290 | Safefree(PL_nice_chunk); | |
291 | PL_nice_chunk = Nullch; | |
292 | PL_nice_chunk_size = 0; | |
293 | PL_sv_arenaroot = 0; | |
294 | PL_sv_root = 0; | |
4633a7c4 LW |
295 | } |
296 | ||
1d7c1841 GS |
297 | void |
298 | Perl_report_uninit(pTHX) | |
299 | { | |
300 | if (PL_op) | |
301 | Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit, | |
302 | " in ", PL_op_desc[PL_op->op_type]); | |
303 | else | |
304 | Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit, "", ""); | |
305 | } | |
306 | ||
76e3520e | 307 | STATIC XPVIV* |
cea2e8a9 | 308 | S_new_xiv(pTHX) |
463ee0b2 | 309 | { |
ea7c11a3 | 310 | IV* xiv; |
cbe51380 GS |
311 | LOCK_SV_MUTEX; |
312 | if (!PL_xiv_root) | |
313 | more_xiv(); | |
314 | xiv = PL_xiv_root; | |
315 | /* | |
316 | * See comment in more_xiv() -- RAM. | |
317 | */ | |
318 | PL_xiv_root = *(IV**)xiv; | |
319 | UNLOCK_SV_MUTEX; | |
320 | return (XPVIV*)((char*)xiv - STRUCT_OFFSET(XPVIV, xiv_iv)); | |
463ee0b2 LW |
321 | } |
322 | ||
76e3520e | 323 | STATIC void |
cea2e8a9 | 324 | S_del_xiv(pTHX_ XPVIV *p) |
463ee0b2 | 325 | { |
23e6a22f | 326 | IV* xiv = (IV*)((char*)(p) + STRUCT_OFFSET(XPVIV, xiv_iv)); |
cbe51380 | 327 | LOCK_SV_MUTEX; |
3280af22 NIS |
328 | *(IV**)xiv = PL_xiv_root; |
329 | PL_xiv_root = xiv; | |
cbe51380 | 330 | UNLOCK_SV_MUTEX; |
463ee0b2 LW |
331 | } |
332 | ||
cbe51380 | 333 | STATIC void |
cea2e8a9 | 334 | S_more_xiv(pTHX) |
463ee0b2 | 335 | { |
ea7c11a3 SM |
336 | register IV* xiv; |
337 | register IV* xivend; | |
8c52afec IZ |
338 | XPV* ptr; |
339 | New(705, ptr, 1008/sizeof(XPV), XPV); | |
3280af22 NIS |
340 | ptr->xpv_pv = (char*)PL_xiv_arenaroot; /* linked list of xiv arenas */ |
341 | PL_xiv_arenaroot = ptr; /* to keep Purify happy */ | |
a0d0e21e | 342 | |
ea7c11a3 SM |
343 | xiv = (IV*) ptr; |
344 | xivend = &xiv[1008 / sizeof(IV) - 1]; | |
345 | xiv += (sizeof(XPV) - 1) / sizeof(IV) + 1; /* fudge by size of XPV */ | |
3280af22 | 346 | PL_xiv_root = xiv; |
463ee0b2 | 347 | while (xiv < xivend) { |
ea7c11a3 | 348 | *(IV**)xiv = (IV *)(xiv + 1); |
463ee0b2 LW |
349 | xiv++; |
350 | } | |
ea7c11a3 | 351 | *(IV**)xiv = 0; |
463ee0b2 LW |
352 | } |
353 | ||
76e3520e | 354 | STATIC XPVNV* |
cea2e8a9 | 355 | S_new_xnv(pTHX) |
463ee0b2 | 356 | { |
65202027 | 357 | NV* xnv; |
cbe51380 GS |
358 | LOCK_SV_MUTEX; |
359 | if (!PL_xnv_root) | |
360 | more_xnv(); | |
361 | xnv = PL_xnv_root; | |
65202027 | 362 | PL_xnv_root = *(NV**)xnv; |
cbe51380 GS |
363 | UNLOCK_SV_MUTEX; |
364 | return (XPVNV*)((char*)xnv - STRUCT_OFFSET(XPVNV, xnv_nv)); | |
463ee0b2 LW |
365 | } |
366 | ||
76e3520e | 367 | STATIC void |
cea2e8a9 | 368 | S_del_xnv(pTHX_ XPVNV *p) |
463ee0b2 | 369 | { |
65202027 | 370 | NV* xnv = (NV*)((char*)(p) + STRUCT_OFFSET(XPVNV, xnv_nv)); |
cbe51380 | 371 | LOCK_SV_MUTEX; |
65202027 | 372 | *(NV**)xnv = PL_xnv_root; |
3280af22 | 373 | PL_xnv_root = xnv; |
cbe51380 | 374 | UNLOCK_SV_MUTEX; |
463ee0b2 LW |
375 | } |
376 | ||
cbe51380 | 377 | STATIC void |
cea2e8a9 | 378 | S_more_xnv(pTHX) |
463ee0b2 | 379 | { |
65202027 DS |
380 | register NV* xnv; |
381 | register NV* xnvend; | |
612f20c3 GS |
382 | XPV *ptr; |
383 | New(711, ptr, 1008/sizeof(XPV), XPV); | |
384 | ptr->xpv_pv = (char*)PL_xnv_arenaroot; | |
385 | PL_xnv_arenaroot = ptr; | |
386 | ||
387 | xnv = (NV*) ptr; | |
65202027 DS |
388 | xnvend = &xnv[1008 / sizeof(NV) - 1]; |
389 | xnv += (sizeof(XPVIV) - 1) / sizeof(NV) + 1; /* fudge by sizeof XPVIV */ | |
3280af22 | 390 | PL_xnv_root = xnv; |
463ee0b2 | 391 | while (xnv < xnvend) { |
65202027 | 392 | *(NV**)xnv = (NV*)(xnv + 1); |
463ee0b2 LW |
393 | xnv++; |
394 | } | |
65202027 | 395 | *(NV**)xnv = 0; |
463ee0b2 LW |
396 | } |
397 | ||
76e3520e | 398 | STATIC XRV* |
cea2e8a9 | 399 | S_new_xrv(pTHX) |
ed6116ce LW |
400 | { |
401 | XRV* xrv; | |
cbe51380 GS |
402 | LOCK_SV_MUTEX; |
403 | if (!PL_xrv_root) | |
404 | more_xrv(); | |
405 | xrv = PL_xrv_root; | |
406 | PL_xrv_root = (XRV*)xrv->xrv_rv; | |
407 | UNLOCK_SV_MUTEX; | |
408 | return xrv; | |
ed6116ce LW |
409 | } |
410 | ||
76e3520e | 411 | STATIC void |
cea2e8a9 | 412 | S_del_xrv(pTHX_ XRV *p) |
ed6116ce | 413 | { |
cbe51380 | 414 | LOCK_SV_MUTEX; |
3280af22 NIS |
415 | p->xrv_rv = (SV*)PL_xrv_root; |
416 | PL_xrv_root = p; | |
cbe51380 | 417 | UNLOCK_SV_MUTEX; |
ed6116ce LW |
418 | } |
419 | ||
cbe51380 | 420 | STATIC void |
cea2e8a9 | 421 | S_more_xrv(pTHX) |
ed6116ce | 422 | { |
ed6116ce LW |
423 | register XRV* xrv; |
424 | register XRV* xrvend; | |
612f20c3 GS |
425 | XPV *ptr; |
426 | New(712, ptr, 1008/sizeof(XPV), XPV); | |
427 | ptr->xpv_pv = (char*)PL_xrv_arenaroot; | |
428 | PL_xrv_arenaroot = ptr; | |
429 | ||
430 | xrv = (XRV*) ptr; | |
ed6116ce | 431 | xrvend = &xrv[1008 / sizeof(XRV) - 1]; |
612f20c3 GS |
432 | xrv += (sizeof(XPV) - 1) / sizeof(XRV) + 1; |
433 | PL_xrv_root = xrv; | |
ed6116ce LW |
434 | while (xrv < xrvend) { |
435 | xrv->xrv_rv = (SV*)(xrv + 1); | |
436 | xrv++; | |
437 | } | |
438 | xrv->xrv_rv = 0; | |
ed6116ce LW |
439 | } |
440 | ||
76e3520e | 441 | STATIC XPV* |
cea2e8a9 | 442 | S_new_xpv(pTHX) |
463ee0b2 LW |
443 | { |
444 | XPV* xpv; | |
cbe51380 GS |
445 | LOCK_SV_MUTEX; |
446 | if (!PL_xpv_root) | |
447 | more_xpv(); | |
448 | xpv = PL_xpv_root; | |
449 | PL_xpv_root = (XPV*)xpv->xpv_pv; | |
450 | UNLOCK_SV_MUTEX; | |
451 | return xpv; | |
463ee0b2 LW |
452 | } |
453 | ||
76e3520e | 454 | STATIC void |
cea2e8a9 | 455 | S_del_xpv(pTHX_ XPV *p) |
463ee0b2 | 456 | { |
cbe51380 | 457 | LOCK_SV_MUTEX; |
3280af22 NIS |
458 | p->xpv_pv = (char*)PL_xpv_root; |
459 | PL_xpv_root = p; | |
cbe51380 | 460 | UNLOCK_SV_MUTEX; |
463ee0b2 LW |
461 | } |
462 | ||
cbe51380 | 463 | STATIC void |
cea2e8a9 | 464 | S_more_xpv(pTHX) |
463ee0b2 | 465 | { |
463ee0b2 LW |
466 | register XPV* xpv; |
467 | register XPV* xpvend; | |
612f20c3 GS |
468 | New(713, xpv, 1008/sizeof(XPV), XPV); |
469 | xpv->xpv_pv = (char*)PL_xpv_arenaroot; | |
470 | PL_xpv_arenaroot = xpv; | |
471 | ||
463ee0b2 | 472 | xpvend = &xpv[1008 / sizeof(XPV) - 1]; |
612f20c3 | 473 | PL_xpv_root = ++xpv; |
463ee0b2 LW |
474 | while (xpv < xpvend) { |
475 | xpv->xpv_pv = (char*)(xpv + 1); | |
476 | xpv++; | |
477 | } | |
478 | xpv->xpv_pv = 0; | |
463ee0b2 LW |
479 | } |
480 | ||
932e9ff9 VB |
481 | STATIC XPVIV* |
482 | S_new_xpviv(pTHX) | |
483 | { | |
484 | XPVIV* xpviv; | |
485 | LOCK_SV_MUTEX; | |
486 | if (!PL_xpviv_root) | |
487 | more_xpviv(); | |
488 | xpviv = PL_xpviv_root; | |
489 | PL_xpviv_root = (XPVIV*)xpviv->xpv_pv; | |
490 | UNLOCK_SV_MUTEX; | |
491 | return xpviv; | |
492 | } | |
493 | ||
494 | STATIC void | |
495 | S_del_xpviv(pTHX_ XPVIV *p) | |
496 | { | |
497 | LOCK_SV_MUTEX; | |
498 | p->xpv_pv = (char*)PL_xpviv_root; | |
499 | PL_xpviv_root = p; | |
500 | UNLOCK_SV_MUTEX; | |
501 | } | |
502 | ||
932e9ff9 VB |
503 | STATIC void |
504 | S_more_xpviv(pTHX) | |
505 | { | |
506 | register XPVIV* xpviv; | |
507 | register XPVIV* xpvivend; | |
612f20c3 GS |
508 | New(714, xpviv, 1008/sizeof(XPVIV), XPVIV); |
509 | xpviv->xpv_pv = (char*)PL_xpviv_arenaroot; | |
510 | PL_xpviv_arenaroot = xpviv; | |
511 | ||
932e9ff9 | 512 | xpvivend = &xpviv[1008 / sizeof(XPVIV) - 1]; |
612f20c3 | 513 | PL_xpviv_root = ++xpviv; |
932e9ff9 VB |
514 | while (xpviv < xpvivend) { |
515 | xpviv->xpv_pv = (char*)(xpviv + 1); | |
516 | xpviv++; | |
517 | } | |
518 | xpviv->xpv_pv = 0; | |
519 | } | |
520 | ||
932e9ff9 VB |
521 | STATIC XPVNV* |
522 | S_new_xpvnv(pTHX) | |
523 | { | |
524 | XPVNV* xpvnv; | |
525 | LOCK_SV_MUTEX; | |
526 | if (!PL_xpvnv_root) | |
527 | more_xpvnv(); | |
528 | xpvnv = PL_xpvnv_root; | |
529 | PL_xpvnv_root = (XPVNV*)xpvnv->xpv_pv; | |
530 | UNLOCK_SV_MUTEX; | |
531 | return xpvnv; | |
532 | } | |
533 | ||
534 | STATIC void | |
535 | S_del_xpvnv(pTHX_ XPVNV *p) | |
536 | { | |
537 | LOCK_SV_MUTEX; | |
538 | p->xpv_pv = (char*)PL_xpvnv_root; | |
539 | PL_xpvnv_root = p; | |
540 | UNLOCK_SV_MUTEX; | |
541 | } | |
542 | ||
932e9ff9 VB |
543 | STATIC void |
544 | S_more_xpvnv(pTHX) | |
545 | { | |
546 | register XPVNV* xpvnv; | |
547 | register XPVNV* xpvnvend; | |
612f20c3 GS |
548 | New(715, xpvnv, 1008/sizeof(XPVNV), XPVNV); |
549 | xpvnv->xpv_pv = (char*)PL_xpvnv_arenaroot; | |
550 | PL_xpvnv_arenaroot = xpvnv; | |
551 | ||
932e9ff9 | 552 | xpvnvend = &xpvnv[1008 / sizeof(XPVNV) - 1]; |
612f20c3 | 553 | PL_xpvnv_root = ++xpvnv; |
932e9ff9 VB |
554 | while (xpvnv < xpvnvend) { |
555 | xpvnv->xpv_pv = (char*)(xpvnv + 1); | |
556 | xpvnv++; | |
557 | } | |
558 | xpvnv->xpv_pv = 0; | |
559 | } | |
560 | ||
932e9ff9 VB |
561 | STATIC XPVCV* |
562 | S_new_xpvcv(pTHX) | |
563 | { | |
564 | XPVCV* xpvcv; | |
565 | LOCK_SV_MUTEX; | |
566 | if (!PL_xpvcv_root) | |
567 | more_xpvcv(); | |
568 | xpvcv = PL_xpvcv_root; | |
569 | PL_xpvcv_root = (XPVCV*)xpvcv->xpv_pv; | |
570 | UNLOCK_SV_MUTEX; | |
571 | return xpvcv; | |
572 | } | |
573 | ||
574 | STATIC void | |
575 | S_del_xpvcv(pTHX_ XPVCV *p) | |
576 | { | |
577 | LOCK_SV_MUTEX; | |
578 | p->xpv_pv = (char*)PL_xpvcv_root; | |
579 | PL_xpvcv_root = p; | |
580 | UNLOCK_SV_MUTEX; | |
581 | } | |
582 | ||
932e9ff9 VB |
583 | STATIC void |
584 | S_more_xpvcv(pTHX) | |
585 | { | |
586 | register XPVCV* xpvcv; | |
587 | register XPVCV* xpvcvend; | |
612f20c3 GS |
588 | New(716, xpvcv, 1008/sizeof(XPVCV), XPVCV); |
589 | xpvcv->xpv_pv = (char*)PL_xpvcv_arenaroot; | |
590 | PL_xpvcv_arenaroot = xpvcv; | |
591 | ||
932e9ff9 | 592 | xpvcvend = &xpvcv[1008 / sizeof(XPVCV) - 1]; |
612f20c3 | 593 | PL_xpvcv_root = ++xpvcv; |
932e9ff9 VB |
594 | while (xpvcv < xpvcvend) { |
595 | xpvcv->xpv_pv = (char*)(xpvcv + 1); | |
596 | xpvcv++; | |
597 | } | |
598 | xpvcv->xpv_pv = 0; | |
599 | } | |
600 | ||
932e9ff9 VB |
601 | STATIC XPVAV* |
602 | S_new_xpvav(pTHX) | |
603 | { | |
604 | XPVAV* xpvav; | |
605 | LOCK_SV_MUTEX; | |
606 | if (!PL_xpvav_root) | |
607 | more_xpvav(); | |
608 | xpvav = PL_xpvav_root; | |
609 | PL_xpvav_root = (XPVAV*)xpvav->xav_array; | |
610 | UNLOCK_SV_MUTEX; | |
611 | return xpvav; | |
612 | } | |
613 | ||
614 | STATIC void | |
615 | S_del_xpvav(pTHX_ XPVAV *p) | |
616 | { | |
617 | LOCK_SV_MUTEX; | |
618 | p->xav_array = (char*)PL_xpvav_root; | |
619 | PL_xpvav_root = p; | |
620 | UNLOCK_SV_MUTEX; | |
621 | } | |
622 | ||
932e9ff9 VB |
623 | STATIC void |
624 | S_more_xpvav(pTHX) | |
625 | { | |
626 | register XPVAV* xpvav; | |
627 | register XPVAV* xpvavend; | |
612f20c3 GS |
628 | New(717, xpvav, 1008/sizeof(XPVAV), XPVAV); |
629 | xpvav->xav_array = (char*)PL_xpvav_arenaroot; | |
630 | PL_xpvav_arenaroot = xpvav; | |
631 | ||
932e9ff9 | 632 | xpvavend = &xpvav[1008 / sizeof(XPVAV) - 1]; |
612f20c3 | 633 | PL_xpvav_root = ++xpvav; |
932e9ff9 VB |
634 | while (xpvav < xpvavend) { |
635 | xpvav->xav_array = (char*)(xpvav + 1); | |
636 | xpvav++; | |
637 | } | |
638 | xpvav->xav_array = 0; | |
639 | } | |
640 | ||
932e9ff9 VB |
641 | STATIC XPVHV* |
642 | S_new_xpvhv(pTHX) | |
643 | { | |
644 | XPVHV* xpvhv; | |
645 | LOCK_SV_MUTEX; | |
646 | if (!PL_xpvhv_root) | |
647 | more_xpvhv(); | |
648 | xpvhv = PL_xpvhv_root; | |
649 | PL_xpvhv_root = (XPVHV*)xpvhv->xhv_array; | |
650 | UNLOCK_SV_MUTEX; | |
651 | return xpvhv; | |
652 | } | |
653 | ||
654 | STATIC void | |
655 | S_del_xpvhv(pTHX_ XPVHV *p) | |
656 | { | |
657 | LOCK_SV_MUTEX; | |
658 | p->xhv_array = (char*)PL_xpvhv_root; | |
659 | PL_xpvhv_root = p; | |
660 | UNLOCK_SV_MUTEX; | |
661 | } | |
662 | ||
932e9ff9 VB |
663 | STATIC void |
664 | S_more_xpvhv(pTHX) | |
665 | { | |
666 | register XPVHV* xpvhv; | |
667 | register XPVHV* xpvhvend; | |
612f20c3 GS |
668 | New(718, xpvhv, 1008/sizeof(XPVHV), XPVHV); |
669 | xpvhv->xhv_array = (char*)PL_xpvhv_arenaroot; | |
670 | PL_xpvhv_arenaroot = xpvhv; | |
671 | ||
932e9ff9 | 672 | xpvhvend = &xpvhv[1008 / sizeof(XPVHV) - 1]; |
612f20c3 | 673 | PL_xpvhv_root = ++xpvhv; |
932e9ff9 VB |
674 | while (xpvhv < xpvhvend) { |
675 | xpvhv->xhv_array = (char*)(xpvhv + 1); | |
676 | xpvhv++; | |
677 | } | |
678 | xpvhv->xhv_array = 0; | |
679 | } | |
680 | ||
932e9ff9 VB |
681 | STATIC XPVMG* |
682 | S_new_xpvmg(pTHX) | |
683 | { | |
684 | XPVMG* xpvmg; | |
685 | LOCK_SV_MUTEX; | |
686 | if (!PL_xpvmg_root) | |
687 | more_xpvmg(); | |
688 | xpvmg = PL_xpvmg_root; | |
689 | PL_xpvmg_root = (XPVMG*)xpvmg->xpv_pv; | |
690 | UNLOCK_SV_MUTEX; | |
691 | return xpvmg; | |
692 | } | |
693 | ||
694 | STATIC void | |
695 | S_del_xpvmg(pTHX_ XPVMG *p) | |
696 | { | |
697 | LOCK_SV_MUTEX; | |
698 | p->xpv_pv = (char*)PL_xpvmg_root; | |
699 | PL_xpvmg_root = p; | |
700 | UNLOCK_SV_MUTEX; | |
701 | } | |
702 | ||
932e9ff9 VB |
703 | STATIC void |
704 | S_more_xpvmg(pTHX) | |
705 | { | |
706 | register XPVMG* xpvmg; | |
707 | register XPVMG* xpvmgend; | |
612f20c3 GS |
708 | New(719, xpvmg, 1008/sizeof(XPVMG), XPVMG); |
709 | xpvmg->xpv_pv = (char*)PL_xpvmg_arenaroot; | |
710 | PL_xpvmg_arenaroot = xpvmg; | |
711 | ||
932e9ff9 | 712 | xpvmgend = &xpvmg[1008 / sizeof(XPVMG) - 1]; |
612f20c3 | 713 | PL_xpvmg_root = ++xpvmg; |
932e9ff9 VB |
714 | while (xpvmg < xpvmgend) { |
715 | xpvmg->xpv_pv = (char*)(xpvmg + 1); | |
716 | xpvmg++; | |
717 | } | |
718 | xpvmg->xpv_pv = 0; | |
719 | } | |
720 | ||
932e9ff9 VB |
721 | STATIC XPVLV* |
722 | S_new_xpvlv(pTHX) | |
723 | { | |
724 | XPVLV* xpvlv; | |
725 | LOCK_SV_MUTEX; | |
726 | if (!PL_xpvlv_root) | |
727 | more_xpvlv(); | |
728 | xpvlv = PL_xpvlv_root; | |
729 | PL_xpvlv_root = (XPVLV*)xpvlv->xpv_pv; | |
730 | UNLOCK_SV_MUTEX; | |
731 | return xpvlv; | |
732 | } | |
733 | ||
734 | STATIC void | |
735 | S_del_xpvlv(pTHX_ XPVLV *p) | |
736 | { | |
737 | LOCK_SV_MUTEX; | |
738 | p->xpv_pv = (char*)PL_xpvlv_root; | |
739 | PL_xpvlv_root = p; | |
740 | UNLOCK_SV_MUTEX; | |
741 | } | |
742 | ||
932e9ff9 VB |
743 | STATIC void |
744 | S_more_xpvlv(pTHX) | |
745 | { | |
746 | register XPVLV* xpvlv; | |
747 | register XPVLV* xpvlvend; | |
612f20c3 GS |
748 | New(720, xpvlv, 1008/sizeof(XPVLV), XPVLV); |
749 | xpvlv->xpv_pv = (char*)PL_xpvlv_arenaroot; | |
750 | PL_xpvlv_arenaroot = xpvlv; | |
751 | ||
932e9ff9 | 752 | xpvlvend = &xpvlv[1008 / sizeof(XPVLV) - 1]; |
612f20c3 | 753 | PL_xpvlv_root = ++xpvlv; |
932e9ff9 VB |
754 | while (xpvlv < xpvlvend) { |
755 | xpvlv->xpv_pv = (char*)(xpvlv + 1); | |
756 | xpvlv++; | |
757 | } | |
758 | xpvlv->xpv_pv = 0; | |
759 | } | |
760 | ||
932e9ff9 VB |
761 | STATIC XPVBM* |
762 | S_new_xpvbm(pTHX) | |
763 | { | |
764 | XPVBM* xpvbm; | |
765 | LOCK_SV_MUTEX; | |
766 | if (!PL_xpvbm_root) | |
767 | more_xpvbm(); | |
768 | xpvbm = PL_xpvbm_root; | |
769 | PL_xpvbm_root = (XPVBM*)xpvbm->xpv_pv; | |
770 | UNLOCK_SV_MUTEX; | |
771 | return xpvbm; | |
772 | } | |
773 | ||
774 | STATIC void | |
775 | S_del_xpvbm(pTHX_ XPVBM *p) | |
776 | { | |
777 | LOCK_SV_MUTEX; | |
778 | p->xpv_pv = (char*)PL_xpvbm_root; | |
779 | PL_xpvbm_root = p; | |
780 | UNLOCK_SV_MUTEX; | |
781 | } | |
782 | ||
932e9ff9 VB |
783 | STATIC void |
784 | S_more_xpvbm(pTHX) | |
785 | { | |
786 | register XPVBM* xpvbm; | |
787 | register XPVBM* xpvbmend; | |
612f20c3 GS |
788 | New(721, xpvbm, 1008/sizeof(XPVBM), XPVBM); |
789 | xpvbm->xpv_pv = (char*)PL_xpvbm_arenaroot; | |
790 | PL_xpvbm_arenaroot = xpvbm; | |
791 | ||
932e9ff9 | 792 | xpvbmend = &xpvbm[1008 / sizeof(XPVBM) - 1]; |
612f20c3 | 793 | PL_xpvbm_root = ++xpvbm; |
932e9ff9 VB |
794 | while (xpvbm < xpvbmend) { |
795 | xpvbm->xpv_pv = (char*)(xpvbm + 1); | |
796 | xpvbm++; | |
797 | } | |
798 | xpvbm->xpv_pv = 0; | |
799 | } | |
800 | ||
d33b2eba GS |
801 | #ifdef LEAKTEST |
802 | # define my_safemalloc(s) (void*)safexmalloc(717,s) | |
803 | # define my_safefree(p) safexfree((char*)p) | |
804 | #else | |
805 | # define my_safemalloc(s) (void*)safemalloc(s) | |
806 | # define my_safefree(p) safefree((char*)p) | |
807 | #endif | |
463ee0b2 | 808 | |
d33b2eba | 809 | #ifdef PURIFY |
463ee0b2 | 810 | |
d33b2eba GS |
811 | #define new_XIV() my_safemalloc(sizeof(XPVIV)) |
812 | #define del_XIV(p) my_safefree(p) | |
ed6116ce | 813 | |
d33b2eba GS |
814 | #define new_XNV() my_safemalloc(sizeof(XPVNV)) |
815 | #define del_XNV(p) my_safefree(p) | |
463ee0b2 | 816 | |
d33b2eba GS |
817 | #define new_XRV() my_safemalloc(sizeof(XRV)) |
818 | #define del_XRV(p) my_safefree(p) | |
8c52afec | 819 | |
d33b2eba GS |
820 | #define new_XPV() my_safemalloc(sizeof(XPV)) |
821 | #define del_XPV(p) my_safefree(p) | |
9b94d1dd | 822 | |
d33b2eba GS |
823 | #define new_XPVIV() my_safemalloc(sizeof(XPVIV)) |
824 | #define del_XPVIV(p) my_safefree(p) | |
932e9ff9 | 825 | |
d33b2eba GS |
826 | #define new_XPVNV() my_safemalloc(sizeof(XPVNV)) |
827 | #define del_XPVNV(p) my_safefree(p) | |
932e9ff9 | 828 | |
d33b2eba GS |
829 | #define new_XPVCV() my_safemalloc(sizeof(XPVCV)) |
830 | #define del_XPVCV(p) my_safefree(p) | |
932e9ff9 | 831 | |
d33b2eba GS |
832 | #define new_XPVAV() my_safemalloc(sizeof(XPVAV)) |
833 | #define del_XPVAV(p) my_safefree(p) | |
834 | ||
835 | #define new_XPVHV() my_safemalloc(sizeof(XPVHV)) | |
836 | #define del_XPVHV(p) my_safefree(p) | |
1c846c1f | 837 | |
d33b2eba GS |
838 | #define new_XPVMG() my_safemalloc(sizeof(XPVMG)) |
839 | #define del_XPVMG(p) my_safefree(p) | |
840 | ||
841 | #define new_XPVLV() my_safemalloc(sizeof(XPVLV)) | |
842 | #define del_XPVLV(p) my_safefree(p) | |
843 | ||
844 | #define new_XPVBM() my_safemalloc(sizeof(XPVBM)) | |
845 | #define del_XPVBM(p) my_safefree(p) | |
846 | ||
847 | #else /* !PURIFY */ | |
848 | ||
849 | #define new_XIV() (void*)new_xiv() | |
850 | #define del_XIV(p) del_xiv((XPVIV*) p) | |
851 | ||
852 | #define new_XNV() (void*)new_xnv() | |
853 | #define del_XNV(p) del_xnv((XPVNV*) p) | |
9b94d1dd | 854 | |
d33b2eba GS |
855 | #define new_XRV() (void*)new_xrv() |
856 | #define del_XRV(p) del_xrv((XRV*) p) | |
9b94d1dd | 857 | |
d33b2eba GS |
858 | #define new_XPV() (void*)new_xpv() |
859 | #define del_XPV(p) del_xpv((XPV *)p) | |
860 | ||
861 | #define new_XPVIV() (void*)new_xpviv() | |
862 | #define del_XPVIV(p) del_xpviv((XPVIV *)p) | |
863 | ||
864 | #define new_XPVNV() (void*)new_xpvnv() | |
865 | #define del_XPVNV(p) del_xpvnv((XPVNV *)p) | |
866 | ||
867 | #define new_XPVCV() (void*)new_xpvcv() | |
868 | #define del_XPVCV(p) del_xpvcv((XPVCV *)p) | |
869 | ||
870 | #define new_XPVAV() (void*)new_xpvav() | |
871 | #define del_XPVAV(p) del_xpvav((XPVAV *)p) | |
872 | ||
873 | #define new_XPVHV() (void*)new_xpvhv() | |
874 | #define del_XPVHV(p) del_xpvhv((XPVHV *)p) | |
1c846c1f | 875 | |
d33b2eba GS |
876 | #define new_XPVMG() (void*)new_xpvmg() |
877 | #define del_XPVMG(p) del_xpvmg((XPVMG *)p) | |
878 | ||
879 | #define new_XPVLV() (void*)new_xpvlv() | |
880 | #define del_XPVLV(p) del_xpvlv((XPVLV *)p) | |
881 | ||
882 | #define new_XPVBM() (void*)new_xpvbm() | |
883 | #define del_XPVBM(p) del_xpvbm((XPVBM *)p) | |
884 | ||
885 | #endif /* PURIFY */ | |
9b94d1dd | 886 | |
d33b2eba GS |
887 | #define new_XPVGV() my_safemalloc(sizeof(XPVGV)) |
888 | #define del_XPVGV(p) my_safefree(p) | |
1c846c1f | 889 | |
d33b2eba GS |
890 | #define new_XPVFM() my_safemalloc(sizeof(XPVFM)) |
891 | #define del_XPVFM(p) my_safefree(p) | |
1c846c1f | 892 | |
d33b2eba GS |
893 | #define new_XPVIO() my_safemalloc(sizeof(XPVIO)) |
894 | #define del_XPVIO(p) my_safefree(p) | |
8990e307 | 895 | |
954c1994 GS |
896 | /* |
897 | =for apidoc sv_upgrade | |
898 | ||
899 | Upgrade an SV to a more complex form. Use C<SvUPGRADE>. See | |
900 | C<svtype>. | |
901 | ||
902 | =cut | |
903 | */ | |
904 | ||
79072805 | 905 | bool |
864dbfa3 | 906 | Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt) |
79072805 LW |
907 | { |
908 | char* pv; | |
909 | U32 cur; | |
910 | U32 len; | |
a0d0e21e | 911 | IV iv; |
65202027 | 912 | NV nv; |
79072805 LW |
913 | MAGIC* magic; |
914 | HV* stash; | |
915 | ||
f130fd45 NIS |
916 | if (mt != SVt_PV && SvREADONLY(sv) && SvFAKE(sv)) { |
917 | sv_force_normal(sv); | |
918 | } | |
919 | ||
79072805 LW |
920 | if (SvTYPE(sv) == mt) |
921 | return TRUE; | |
922 | ||
a5f75d66 AD |
923 | if (mt < SVt_PVIV) |
924 | (void)SvOOK_off(sv); | |
925 | ||
79072805 LW |
926 | switch (SvTYPE(sv)) { |
927 | case SVt_NULL: | |
928 | pv = 0; | |
929 | cur = 0; | |
930 | len = 0; | |
931 | iv = 0; | |
932 | nv = 0.0; | |
933 | magic = 0; | |
934 | stash = 0; | |
935 | break; | |
79072805 LW |
936 | case SVt_IV: |
937 | pv = 0; | |
938 | cur = 0; | |
939 | len = 0; | |
463ee0b2 | 940 | iv = SvIVX(sv); |
65202027 | 941 | nv = (NV)SvIVX(sv); |
79072805 LW |
942 | del_XIV(SvANY(sv)); |
943 | magic = 0; | |
944 | stash = 0; | |
ed6116ce | 945 | if (mt == SVt_NV) |
463ee0b2 | 946 | mt = SVt_PVNV; |
ed6116ce LW |
947 | else if (mt < SVt_PVIV) |
948 | mt = SVt_PVIV; | |
79072805 LW |
949 | break; |
950 | case SVt_NV: | |
951 | pv = 0; | |
952 | cur = 0; | |
953 | len = 0; | |
463ee0b2 | 954 | nv = SvNVX(sv); |
1bd302c3 | 955 | iv = I_V(nv); |
79072805 LW |
956 | magic = 0; |
957 | stash = 0; | |
958 | del_XNV(SvANY(sv)); | |
959 | SvANY(sv) = 0; | |
ed6116ce | 960 | if (mt < SVt_PVNV) |
79072805 LW |
961 | mt = SVt_PVNV; |
962 | break; | |
ed6116ce LW |
963 | case SVt_RV: |
964 | pv = (char*)SvRV(sv); | |
965 | cur = 0; | |
966 | len = 0; | |
56431972 RB |
967 | iv = PTR2IV(pv); |
968 | nv = PTR2NV(pv); | |
ed6116ce LW |
969 | del_XRV(SvANY(sv)); |
970 | magic = 0; | |
971 | stash = 0; | |
972 | break; | |
79072805 | 973 | case SVt_PV: |
463ee0b2 | 974 | pv = SvPVX(sv); |
79072805 LW |
975 | cur = SvCUR(sv); |
976 | len = SvLEN(sv); | |
977 | iv = 0; | |
978 | nv = 0.0; | |
979 | magic = 0; | |
980 | stash = 0; | |
981 | del_XPV(SvANY(sv)); | |
748a9306 LW |
982 | if (mt <= SVt_IV) |
983 | mt = SVt_PVIV; | |
984 | else if (mt == SVt_NV) | |
985 | mt = SVt_PVNV; | |
79072805 LW |
986 | break; |
987 | case SVt_PVIV: | |
463ee0b2 | 988 | pv = SvPVX(sv); |
79072805 LW |
989 | cur = SvCUR(sv); |
990 | len = SvLEN(sv); | |
463ee0b2 | 991 | iv = SvIVX(sv); |
79072805 LW |
992 | nv = 0.0; |
993 | magic = 0; | |
994 | stash = 0; | |
995 | del_XPVIV(SvANY(sv)); | |
996 | break; | |
997 | case SVt_PVNV: | |
463ee0b2 | 998 | pv = SvPVX(sv); |
79072805 LW |
999 | cur = SvCUR(sv); |
1000 | len = SvLEN(sv); | |
463ee0b2 LW |
1001 | iv = SvIVX(sv); |
1002 | nv = SvNVX(sv); | |
79072805 LW |
1003 | magic = 0; |
1004 | stash = 0; | |
1005 | del_XPVNV(SvANY(sv)); | |
1006 | break; | |
1007 | case SVt_PVMG: | |
463ee0b2 | 1008 | pv = SvPVX(sv); |
79072805 LW |
1009 | cur = SvCUR(sv); |
1010 | len = SvLEN(sv); | |
463ee0b2 LW |
1011 | iv = SvIVX(sv); |
1012 | nv = SvNVX(sv); | |
79072805 LW |
1013 | magic = SvMAGIC(sv); |
1014 | stash = SvSTASH(sv); | |
1015 | del_XPVMG(SvANY(sv)); | |
1016 | break; | |
1017 | default: | |
cea2e8a9 | 1018 | Perl_croak(aTHX_ "Can't upgrade that kind of scalar"); |
79072805 LW |
1019 | } |
1020 | ||
1021 | switch (mt) { | |
1022 | case SVt_NULL: | |
cea2e8a9 | 1023 | Perl_croak(aTHX_ "Can't upgrade to undef"); |
79072805 LW |
1024 | case SVt_IV: |
1025 | SvANY(sv) = new_XIV(); | |
463ee0b2 | 1026 | SvIVX(sv) = iv; |
79072805 LW |
1027 | break; |
1028 | case SVt_NV: | |
1029 | SvANY(sv) = new_XNV(); | |
463ee0b2 | 1030 | SvNVX(sv) = nv; |
79072805 | 1031 | break; |
ed6116ce LW |
1032 | case SVt_RV: |
1033 | SvANY(sv) = new_XRV(); | |
1034 | SvRV(sv) = (SV*)pv; | |
ed6116ce | 1035 | break; |
79072805 LW |
1036 | case SVt_PV: |
1037 | SvANY(sv) = new_XPV(); | |
463ee0b2 | 1038 | SvPVX(sv) = pv; |
79072805 LW |
1039 | SvCUR(sv) = cur; |
1040 | SvLEN(sv) = len; | |
1041 | break; | |
1042 | case SVt_PVIV: | |
1043 | SvANY(sv) = new_XPVIV(); | |
463ee0b2 | 1044 | SvPVX(sv) = pv; |
79072805 LW |
1045 | SvCUR(sv) = cur; |
1046 | SvLEN(sv) = len; | |
463ee0b2 | 1047 | SvIVX(sv) = iv; |
79072805 | 1048 | if (SvNIOK(sv)) |
a0d0e21e | 1049 | (void)SvIOK_on(sv); |
79072805 LW |
1050 | SvNOK_off(sv); |
1051 | break; | |
1052 | case SVt_PVNV: | |
1053 | SvANY(sv) = new_XPVNV(); | |
463ee0b2 | 1054 | SvPVX(sv) = pv; |
79072805 LW |
1055 | SvCUR(sv) = cur; |
1056 | SvLEN(sv) = len; | |
463ee0b2 LW |
1057 | SvIVX(sv) = iv; |
1058 | SvNVX(sv) = nv; | |
79072805 LW |
1059 | break; |
1060 | case SVt_PVMG: | |
1061 | SvANY(sv) = new_XPVMG(); | |
463ee0b2 | 1062 | SvPVX(sv) = pv; |
79072805 LW |
1063 | SvCUR(sv) = cur; |
1064 | SvLEN(sv) = len; | |
463ee0b2 LW |
1065 | SvIVX(sv) = iv; |
1066 | SvNVX(sv) = nv; | |
79072805 LW |
1067 | SvMAGIC(sv) = magic; |
1068 | SvSTASH(sv) = stash; | |
1069 | break; | |
1070 | case SVt_PVLV: | |
1071 | SvANY(sv) = new_XPVLV(); | |
463ee0b2 | 1072 | SvPVX(sv) = pv; |
79072805 LW |
1073 | SvCUR(sv) = cur; |
1074 | SvLEN(sv) = len; | |
463ee0b2 LW |
1075 | SvIVX(sv) = iv; |
1076 | SvNVX(sv) = nv; | |
79072805 LW |
1077 | SvMAGIC(sv) = magic; |
1078 | SvSTASH(sv) = stash; | |
1079 | LvTARGOFF(sv) = 0; | |
1080 | LvTARGLEN(sv) = 0; | |
1081 | LvTARG(sv) = 0; | |
1082 | LvTYPE(sv) = 0; | |
1083 | break; | |
1084 | case SVt_PVAV: | |
1085 | SvANY(sv) = new_XPVAV(); | |
463ee0b2 LW |
1086 | if (pv) |
1087 | Safefree(pv); | |
2304df62 | 1088 | SvPVX(sv) = 0; |
d1bf51dd | 1089 | AvMAX(sv) = -1; |
93965878 | 1090 | AvFILLp(sv) = -1; |
463ee0b2 LW |
1091 | SvIVX(sv) = 0; |
1092 | SvNVX(sv) = 0.0; | |
1093 | SvMAGIC(sv) = magic; | |
1094 | SvSTASH(sv) = stash; | |
1095 | AvALLOC(sv) = 0; | |
79072805 LW |
1096 | AvARYLEN(sv) = 0; |
1097 | AvFLAGS(sv) = 0; | |
1098 | break; | |
1099 | case SVt_PVHV: | |
1100 | SvANY(sv) = new_XPVHV(); | |
463ee0b2 LW |
1101 | if (pv) |
1102 | Safefree(pv); | |
1103 | SvPVX(sv) = 0; | |
1104 | HvFILL(sv) = 0; | |
1105 | HvMAX(sv) = 0; | |
1106 | HvKEYS(sv) = 0; | |
1107 | SvNVX(sv) = 0.0; | |
79072805 LW |
1108 | SvMAGIC(sv) = magic; |
1109 | SvSTASH(sv) = stash; | |
79072805 LW |
1110 | HvRITER(sv) = 0; |
1111 | HvEITER(sv) = 0; | |
1112 | HvPMROOT(sv) = 0; | |
1113 | HvNAME(sv) = 0; | |
79072805 LW |
1114 | break; |
1115 | case SVt_PVCV: | |
1116 | SvANY(sv) = new_XPVCV(); | |
748a9306 | 1117 | Zero(SvANY(sv), 1, XPVCV); |
463ee0b2 | 1118 | SvPVX(sv) = pv; |
79072805 LW |
1119 | SvCUR(sv) = cur; |
1120 | SvLEN(sv) = len; | |
463ee0b2 LW |
1121 | SvIVX(sv) = iv; |
1122 | SvNVX(sv) = nv; | |
79072805 LW |
1123 | SvMAGIC(sv) = magic; |
1124 | SvSTASH(sv) = stash; | |
79072805 LW |
1125 | break; |
1126 | case SVt_PVGV: | |
1127 | SvANY(sv) = new_XPVGV(); | |
463ee0b2 | 1128 | SvPVX(sv) = pv; |
79072805 LW |
1129 | SvCUR(sv) = cur; |
1130 | SvLEN(sv) = len; | |
463ee0b2 LW |
1131 | SvIVX(sv) = iv; |
1132 | SvNVX(sv) = nv; | |
79072805 LW |
1133 | SvMAGIC(sv) = magic; |
1134 | SvSTASH(sv) = stash; | |
93a17b20 | 1135 | GvGP(sv) = 0; |
79072805 LW |
1136 | GvNAME(sv) = 0; |
1137 | GvNAMELEN(sv) = 0; | |
1138 | GvSTASH(sv) = 0; | |
a5f75d66 | 1139 | GvFLAGS(sv) = 0; |
79072805 LW |
1140 | break; |
1141 | case SVt_PVBM: | |
1142 | SvANY(sv) = new_XPVBM(); | |
463ee0b2 | 1143 | SvPVX(sv) = pv; |
79072805 LW |
1144 | SvCUR(sv) = cur; |
1145 | SvLEN(sv) = len; | |
463ee0b2 LW |
1146 | SvIVX(sv) = iv; |
1147 | SvNVX(sv) = nv; | |
79072805 LW |
1148 | SvMAGIC(sv) = magic; |
1149 | SvSTASH(sv) = stash; | |
1150 | BmRARE(sv) = 0; | |
1151 | BmUSEFUL(sv) = 0; | |
1152 | BmPREVIOUS(sv) = 0; | |
1153 | break; | |
1154 | case SVt_PVFM: | |
1155 | SvANY(sv) = new_XPVFM(); | |
748a9306 | 1156 | Zero(SvANY(sv), 1, XPVFM); |
463ee0b2 | 1157 | SvPVX(sv) = pv; |
79072805 LW |
1158 | SvCUR(sv) = cur; |
1159 | SvLEN(sv) = len; | |
463ee0b2 LW |
1160 | SvIVX(sv) = iv; |
1161 | SvNVX(sv) = nv; | |
79072805 LW |
1162 | SvMAGIC(sv) = magic; |
1163 | SvSTASH(sv) = stash; | |
79072805 | 1164 | break; |
8990e307 LW |
1165 | case SVt_PVIO: |
1166 | SvANY(sv) = new_XPVIO(); | |
748a9306 | 1167 | Zero(SvANY(sv), 1, XPVIO); |
8990e307 LW |
1168 | SvPVX(sv) = pv; |
1169 | SvCUR(sv) = cur; | |
1170 | SvLEN(sv) = len; | |
1171 | SvIVX(sv) = iv; | |
1172 | SvNVX(sv) = nv; | |
1173 | SvMAGIC(sv) = magic; | |
1174 | SvSTASH(sv) = stash; | |
85e6fe83 | 1175 | IoPAGE_LEN(sv) = 60; |
8990e307 LW |
1176 | break; |
1177 | } | |
1178 | SvFLAGS(sv) &= ~SVTYPEMASK; | |
1179 | SvFLAGS(sv) |= mt; | |
79072805 LW |
1180 | return TRUE; |
1181 | } | |
1182 | ||
79072805 | 1183 | int |
864dbfa3 | 1184 | Perl_sv_backoff(pTHX_ register SV *sv) |
79072805 LW |
1185 | { |
1186 | assert(SvOOK(sv)); | |
463ee0b2 LW |
1187 | if (SvIVX(sv)) { |
1188 | char *s = SvPVX(sv); | |
1189 | SvLEN(sv) += SvIVX(sv); | |
1190 | SvPVX(sv) -= SvIVX(sv); | |
79072805 | 1191 | SvIV_set(sv, 0); |
463ee0b2 | 1192 | Move(s, SvPVX(sv), SvCUR(sv)+1, char); |
79072805 LW |
1193 | } |
1194 | SvFLAGS(sv) &= ~SVf_OOK; | |
a0d0e21e | 1195 | return 0; |
79072805 LW |
1196 | } |
1197 | ||
954c1994 GS |
1198 | /* |
1199 | =for apidoc sv_grow | |
1200 | ||
1201 | Expands the character buffer in the SV. This will use C<sv_unref> and will | |
1202 | upgrade the SV to C<SVt_PV>. Returns a pointer to the character buffer. | |
1203 | Use C<SvGROW>. | |
1204 | ||
1205 | =cut | |
1206 | */ | |
1207 | ||
79072805 | 1208 | char * |
864dbfa3 | 1209 | Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen) |
79072805 LW |
1210 | { |
1211 | register char *s; | |
1212 | ||
55497cff | 1213 | #ifdef HAS_64K_LIMIT |
79072805 | 1214 | if (newlen >= 0x10000) { |
1d7c1841 GS |
1215 | PerlIO_printf(Perl_debug_log, |
1216 | "Allocation too large: %"UVxf"\n", (UV)newlen); | |
79072805 LW |
1217 | my_exit(1); |
1218 | } | |
55497cff | 1219 | #endif /* HAS_64K_LIMIT */ |
a0d0e21e LW |
1220 | if (SvROK(sv)) |
1221 | sv_unref(sv); | |
79072805 LW |
1222 | if (SvTYPE(sv) < SVt_PV) { |
1223 | sv_upgrade(sv, SVt_PV); | |
463ee0b2 | 1224 | s = SvPVX(sv); |
79072805 LW |
1225 | } |
1226 | else if (SvOOK(sv)) { /* pv is offset? */ | |
1227 | sv_backoff(sv); | |
463ee0b2 | 1228 | s = SvPVX(sv); |
79072805 LW |
1229 | if (newlen > SvLEN(sv)) |
1230 | newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */ | |
c6f8c383 GA |
1231 | #ifdef HAS_64K_LIMIT |
1232 | if (newlen >= 0x10000) | |
1233 | newlen = 0xFFFF; | |
1234 | #endif | |
79072805 LW |
1235 | } |
1236 | else | |
463ee0b2 | 1237 | s = SvPVX(sv); |
79072805 | 1238 | if (newlen > SvLEN(sv)) { /* need more room? */ |
8d6dde3e | 1239 | if (SvLEN(sv) && s) { |
f5a32c7f | 1240 | #if defined(MYMALLOC) && !defined(LEAKTEST) |
8d6dde3e IZ |
1241 | STRLEN l = malloced_size((void*)SvPVX(sv)); |
1242 | if (newlen <= l) { | |
1243 | SvLEN_set(sv, l); | |
1244 | return s; | |
1245 | } else | |
c70c8a0a | 1246 | #endif |
79072805 | 1247 | Renew(s,newlen,char); |
8d6dde3e | 1248 | } |
79072805 LW |
1249 | else |
1250 | New(703,s,newlen,char); | |
1251 | SvPV_set(sv, s); | |
1252 | SvLEN_set(sv, newlen); | |
1253 | } | |
1254 | return s; | |
1255 | } | |
1256 | ||
954c1994 GS |
1257 | /* |
1258 | =for apidoc sv_setiv | |
1259 | ||
1260 | Copies an integer into the given SV. Does not handle 'set' magic. See | |
1261 | C<sv_setiv_mg>. | |
1262 | ||
1263 | =cut | |
1264 | */ | |
1265 | ||
79072805 | 1266 | void |
864dbfa3 | 1267 | Perl_sv_setiv(pTHX_ register SV *sv, IV i) |
79072805 | 1268 | { |
2213622d | 1269 | SV_CHECK_THINKFIRST(sv); |
463ee0b2 LW |
1270 | switch (SvTYPE(sv)) { |
1271 | case SVt_NULL: | |
79072805 | 1272 | sv_upgrade(sv, SVt_IV); |
463ee0b2 LW |
1273 | break; |
1274 | case SVt_NV: | |
1275 | sv_upgrade(sv, SVt_PVNV); | |
1276 | break; | |
ed6116ce | 1277 | case SVt_RV: |
463ee0b2 | 1278 | case SVt_PV: |
79072805 | 1279 | sv_upgrade(sv, SVt_PVIV); |
463ee0b2 | 1280 | break; |
a0d0e21e LW |
1281 | |
1282 | case SVt_PVGV: | |
a0d0e21e LW |
1283 | case SVt_PVAV: |
1284 | case SVt_PVHV: | |
1285 | case SVt_PVCV: | |
1286 | case SVt_PVFM: | |
1287 | case SVt_PVIO: | |
411caa50 JH |
1288 | Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0), |
1289 | PL_op_desc[PL_op->op_type]); | |
463ee0b2 | 1290 | } |
a0d0e21e | 1291 | (void)SvIOK_only(sv); /* validate number */ |
a5f75d66 | 1292 | SvIVX(sv) = i; |
463ee0b2 | 1293 | SvTAINT(sv); |
79072805 LW |
1294 | } |
1295 | ||
954c1994 GS |
1296 | /* |
1297 | =for apidoc sv_setiv_mg | |
1298 | ||
1299 | Like C<sv_setiv>, but also handles 'set' magic. | |
1300 | ||
1301 | =cut | |
1302 | */ | |
1303 | ||
79072805 | 1304 | void |
864dbfa3 | 1305 | Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i) |
ef50df4b GS |
1306 | { |
1307 | sv_setiv(sv,i); | |
1308 | SvSETMAGIC(sv); | |
1309 | } | |
1310 | ||
954c1994 GS |
1311 | /* |
1312 | =for apidoc sv_setuv | |
1313 | ||
1314 | Copies an unsigned integer into the given SV. Does not handle 'set' magic. | |
1315 | See C<sv_setuv_mg>. | |
1316 | ||
1317 | =cut | |
1318 | */ | |
1319 | ||
ef50df4b | 1320 | void |
864dbfa3 | 1321 | Perl_sv_setuv(pTHX_ register SV *sv, UV u) |
55497cff | 1322 | { |
55ada374 NC |
1323 | /* With these two if statements: |
1324 | u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865 | |
1325 | ||
1326 | without | |
1327 | u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865 | |
1328 | ||
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 | |
1353 | ||
1354 | without | |
1355 | u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865 | |
1356 | ||
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; | |
1530 | ||
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. */ | |
1570 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_2inuv_non '%s', IV=0x%"UVxf" NV=%g inttype=%X\n", SvPVX(sv), SvIVX(sv), nv, numtype)); | |
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; | |
1628 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_2niuv_non UV? '%s', UV=0x%"UVxf" NV=%g U_V(NV)=0x%"UVxf" inttype=%X\n", SvPVX(sv), SvIVX(sv), nv, nv_as_uv, numtype)); | |
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 | { | |
1651 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_2iuv_non '%s', IV=0x%"UVxf" NV=%g inttype=%X\n", SvPVX(sv), SvIVX(sv), SvNVX(sv), numtype)); | |
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)) { | |
1681 | SvIOK_on(sv); /* Integer is precise. NOK, UOK */ | |
1682 | } else { | |
1683 | /* Integer is imprecise. NOK, IOKp */ | |
1684 | } | |
1685 | return IS_NUMBER_OVERFLOW_IV; | |
1686 | } | |
e57fe1aa | 1687 | return S_sv_2inuv_non_preserve(aTHX_ sv, numtype); |
28e5dec8 JH |
1688 | } |
1689 | #endif /* NV_PRESERVES_UV*/ | |
1690 | ||
a0d0e21e | 1691 | IV |
864dbfa3 | 1692 | Perl_sv_2iv(pTHX_ register SV *sv) |
79072805 LW |
1693 | { |
1694 | if (!sv) | |
1695 | return 0; | |
8990e307 | 1696 | if (SvGMAGICAL(sv)) { |
463ee0b2 LW |
1697 | mg_get(sv); |
1698 | if (SvIOKp(sv)) | |
1699 | return SvIVX(sv); | |
748a9306 | 1700 | if (SvNOKp(sv)) { |
25da4f38 | 1701 | return I_V(SvNVX(sv)); |
748a9306 | 1702 | } |
36477c24 | 1703 | if (SvPOKp(sv) && SvLEN(sv)) |
1704 | return asIV(sv); | |
3fe9a6f1 | 1705 | if (!SvROK(sv)) { |
d008e5eb | 1706 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 1707 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
1d7c1841 | 1708 | report_uninit(); |
c6ee37c5 | 1709 | } |
36477c24 | 1710 | return 0; |
3fe9a6f1 | 1711 | } |
463ee0b2 | 1712 | } |
ed6116ce | 1713 | if (SvTHINKFIRST(sv)) { |
a0d0e21e | 1714 | if (SvROK(sv)) { |
a0d0e21e | 1715 | SV* tmpstr; |
1554e226 DC |
1716 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) && |
1717 | (SvRV(tmpstr) != SvRV(sv))) | |
9e7bc3e8 | 1718 | return SvIV(tmpstr); |
56431972 | 1719 | return PTR2IV(SvRV(sv)); |
a0d0e21e | 1720 | } |
47deb5e7 NIS |
1721 | if (SvREADONLY(sv) && SvFAKE(sv)) { |
1722 | sv_force_normal(sv); | |
1723 | } | |
0336b60e | 1724 | if (SvREADONLY(sv) && !SvOK(sv)) { |
0336b60e | 1725 | if (ckWARN(WARN_UNINITIALIZED)) |
1d7c1841 | 1726 | report_uninit(); |
ed6116ce LW |
1727 | return 0; |
1728 | } | |
79072805 | 1729 | } |
25da4f38 IZ |
1730 | if (SvIOKp(sv)) { |
1731 | if (SvIsUV(sv)) { | |
1732 | return (IV)(SvUVX(sv)); | |
1733 | } | |
1734 | else { | |
1735 | return SvIVX(sv); | |
1736 | } | |
463ee0b2 | 1737 | } |
748a9306 | 1738 | if (SvNOKp(sv)) { |
28e5dec8 JH |
1739 | /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv |
1740 | * without also getting a cached IV/UV from it at the same time | |
1741 | * (ie PV->NV conversion should detect loss of accuracy and cache | |
1742 | * IV or UV at same time to avoid this. NWC */ | |
25da4f38 IZ |
1743 | |
1744 | if (SvTYPE(sv) == SVt_NV) | |
1745 | sv_upgrade(sv, SVt_PVNV); | |
1746 | ||
28e5dec8 JH |
1747 | (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */ |
1748 | /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost | |
1749 | certainly cast into the IV range at IV_MAX, whereas the correct | |
1750 | answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary | |
1751 | cases go to UV */ | |
1752 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
748a9306 | 1753 | SvIVX(sv) = I_V(SvNVX(sv)); |
28e5dec8 JH |
1754 | if (SvNVX(sv) == (NV) SvIVX(sv) |
1755 | #ifndef NV_PRESERVES_UV | |
1756 | && (((UV)1 << NV_PRESERVES_UV_BITS) > | |
1757 | (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv))) | |
1758 | /* Don't flag it as "accurately an integer" if the number | |
1759 | came from a (by definition imprecise) NV operation, and | |
1760 | we're outside the range of NV integer precision */ | |
1761 | #endif | |
1762 | ) { | |
1763 | SvIOK_on(sv); /* Can this go wrong with rounding? NWC */ | |
1764 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
1765 | "0x%"UVxf" iv(%g => %"IVdf") (precise)\n", | |
1766 | PTR2UV(sv), | |
1767 | SvNVX(sv), | |
1768 | SvIVX(sv))); | |
1769 | ||
1770 | } else { | |
1771 | /* IV not precise. No need to convert from PV, as NV | |
1772 | conversion would already have cached IV if it detected | |
1773 | that PV->IV would be better than PV->NV->IV | |
1774 | flags already correct - don't set public IOK. */ | |
1775 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
1776 | "0x%"UVxf" iv(%g => %"IVdf") (imprecise)\n", | |
1777 | PTR2UV(sv), | |
1778 | SvNVX(sv), | |
1779 | SvIVX(sv))); | |
1780 | } | |
1781 | /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN, | |
1782 | but the cast (NV)IV_MIN rounds to a the value less (more | |
1783 | negative) than IV_MIN which happens to be equal to SvNVX ?? | |
1784 | Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and | |
1785 | NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and | |
1786 | (NV)UVX == NVX are both true, but the values differ. :-( | |
1787 | Hopefully for 2s complement IV_MIN is something like | |
1788 | 0x8000000000000000 which will be exact. NWC */ | |
1789 | } | |
25da4f38 | 1790 | else { |
ff68c719 | 1791 | SvUVX(sv) = U_V(SvNVX(sv)); |
28e5dec8 JH |
1792 | if ( |
1793 | (SvNVX(sv) == (NV) SvUVX(sv)) | |
1794 | #ifndef NV_PRESERVES_UV | |
1795 | /* Make sure it's not 0xFFFFFFFFFFFFFFFF */ | |
1796 | /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */ | |
1797 | && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv)) | |
1798 | /* Don't flag it as "accurately an integer" if the number | |
1799 | came from a (by definition imprecise) NV operation, and | |
1800 | we're outside the range of NV integer precision */ | |
1801 | #endif | |
1802 | ) | |
1803 | SvIOK_on(sv); | |
25da4f38 IZ |
1804 | SvIsUV_on(sv); |
1805 | ret_iv_max: | |
1c846c1f | 1806 | DEBUG_c(PerlIO_printf(Perl_debug_log, |
57def98f | 1807 | "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n", |
56431972 | 1808 | PTR2UV(sv), |
57def98f JH |
1809 | SvUVX(sv), |
1810 | SvUVX(sv))); | |
25da4f38 IZ |
1811 | return (IV)SvUVX(sv); |
1812 | } | |
748a9306 LW |
1813 | } |
1814 | else if (SvPOKp(sv) && SvLEN(sv)) { | |
25da4f38 IZ |
1815 | I32 numtype = looks_like_number(sv); |
1816 | ||
1817 | /* We want to avoid a possible problem when we cache an IV which | |
1818 | may be later translated to an NV, and the resulting NV is not | |
1819 | the translation of the initial data. | |
1c846c1f | 1820 | |
25da4f38 IZ |
1821 | This means that if we cache such an IV, we need to cache the |
1822 | NV as well. Moreover, we trade speed for space, and do not | |
28e5dec8 | 1823 | cache the NV if we are sure it's not needed. |
25da4f38 | 1824 | */ |
16b7a9a4 | 1825 | |
28e5dec8 JH |
1826 | if ((numtype & ~IS_NUMBER_NEG) == IS_NUMBER_TO_INT_BY_ATOL) { |
1827 | /* The NV may be reconstructed from IV - safe to cache IV, | |
1828 | which may be calculated by atol(). */ | |
1829 | if (SvTYPE(sv) < SVt_PVIV) | |
1830 | sv_upgrade(sv, SVt_PVIV); | |
f7bbb42a | 1831 | (void)SvIOK_on(sv); |
28e5dec8 JH |
1832 | SvIVX(sv) = Atol(SvPVX(sv)); |
1833 | } else { | |
1834 | #ifdef HAS_STRTOL | |
1835 | IV i; | |
1836 | int save_errno = errno; | |
1837 | /* Is it an integer that we could convert with strtol? | |
1838 | So try it, and if it doesn't set errno then it's pukka. | |
1839 | This should be faster than going atof and then thinking. */ | |
1840 | if (((numtype & (IS_NUMBER_TO_INT_BY_STRTOL | IS_NUMBER_NOT_INT)) | |
1841 | == IS_NUMBER_TO_INT_BY_STRTOL) | |
1842 | /* && is a sequence point. Without it not sure if I'm trying | |
1843 | to do too much between sequence points and hence going | |
1844 | undefined */ | |
1845 | && ((errno = 0), 1) /* , 1 so always true */ | |
1846 | && ((i = Strtol(SvPVX(sv), Null(char**), 10)), 1) | |
1847 | && (errno == 0)) { | |
1848 | if (SvTYPE(sv) < SVt_PVIV) | |
1849 | sv_upgrade(sv, SVt_PVIV); | |
1850 | (void)SvIOK_on(sv); | |
1851 | SvIVX(sv) = i; | |
1852 | errno = save_errno; | |
1853 | } else | |
1854 | #endif | |
1855 | { | |
1856 | NV d; | |
1857 | #ifdef HAS_STRTOL | |
1858 | /* Hopefully trace flow will optimise this away where possible | |
1859 | */ | |
1860 | errno = save_errno; | |
1861 | #endif | |
1862 | /* It wasn't an integer, or it overflowed, or we don't have | |
1863 | strtol. Do things the slow way - check if it's a UV etc. */ | |
1864 | d = Atof(SvPVX(sv)); | |
1865 | ||
1866 | if (SvTYPE(sv) < SVt_PVNV) | |
1867 | sv_upgrade(sv, SVt_PVNV); | |
1868 | SvNVX(sv) = d; | |
1869 | ||
1870 | if (! numtype && ckWARN(WARN_NUMERIC)) | |
1871 | not_a_number(sv); | |
1872 | ||
65202027 | 1873 | #if defined(USE_LONG_DOUBLE) |
28e5dec8 JH |
1874 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n", |
1875 | PTR2UV(sv), SvNVX(sv))); | |
65202027 | 1876 | #else |
28e5dec8 JH |
1877 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%g)\n", |
1878 | PTR2UV(sv), SvNVX(sv))); | |
65202027 | 1879 | #endif |
28e5dec8 JH |
1880 | |
1881 | ||
1882 | #ifdef NV_PRESERVES_UV | |
1883 | (void)SvIOKp_on(sv); | |
1884 | (void)SvNOK_on(sv); | |
1885 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
1886 | SvIVX(sv) = I_V(SvNVX(sv)); | |
1887 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) { | |
1888 | SvIOK_on(sv); | |
1889 | } else { | |
1890 | /* Integer is imprecise. NOK, IOKp */ | |
1891 | } | |
1892 | /* UV will not work better than IV */ | |
1893 | } else { | |
1894 | if (SvNVX(sv) > (NV)UV_MAX) { | |
1895 | SvIsUV_on(sv); | |
1896 | /* Integer is inaccurate. NOK, IOKp, is UV */ | |
1897 | SvUVX(sv) = UV_MAX; | |
1898 | SvIsUV_on(sv); | |
1899 | } else { | |
1900 | SvUVX(sv) = U_V(SvNVX(sv)); | |
1901 | /* 0xFFFFFFFFFFFFFFFF not an issue in here */ | |
1902 | if ((NV)(SvUVX(sv)) == SvNVX(sv)) { | |
1903 | SvIOK_on(sv); | |
1904 | SvIsUV_on(sv); | |
1905 | } else { | |
1906 | /* Integer is imprecise. NOK, IOKp, is UV */ | |
1907 | SvIsUV_on(sv); | |
1908 | } | |
1909 | } | |
1910 | goto ret_iv_max; | |
1911 | } | |
1912 | #else /* NV_PRESERVES_UV */ | |
1913 | if (((UV)1 << NV_PRESERVES_UV_BITS) > | |
1914 | U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) { | |
1915 | /* Small enough to preserve all bits. */ | |
1916 | (void)SvIOKp_on(sv); | |
1917 | SvNOK_on(sv); | |
1918 | SvIVX(sv) = I_V(SvNVX(sv)); | |
1919 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) | |
1920 | SvIOK_on(sv); | |
1921 | /* Assumption: first non-preserved integer is < IV_MAX, | |
1922 | this NV is in the preserved range, therefore: */ | |
1923 | if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv)) | |
1924 | < (UV)IV_MAX)) { | |
1925 | 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); | |
1926 | } | |
1927 | } else if (sv_2iuv_non_preserve (sv, numtype) | |
1928 | >= IS_NUMBER_OVERFLOW_IV) | |
1929 | goto ret_iv_max; | |
1930 | #endif /* NV_PRESERVES_UV */ | |
25da4f38 IZ |
1931 | } |
1932 | } | |
28e5dec8 | 1933 | } else { |
599cee73 | 1934 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP)) |
1d7c1841 | 1935 | report_uninit(); |
25da4f38 IZ |
1936 | if (SvTYPE(sv) < SVt_IV) |
1937 | /* Typically the caller expects that sv_any is not NULL now. */ | |
1938 | sv_upgrade(sv, SVt_IV); | |
a0d0e21e | 1939 | return 0; |
79072805 | 1940 | } |
1d7c1841 GS |
1941 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n", |
1942 | PTR2UV(sv),SvIVX(sv))); | |
25da4f38 | 1943 | return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv); |
79072805 LW |
1944 | } |
1945 | ||
ff68c719 | 1946 | UV |
864dbfa3 | 1947 | Perl_sv_2uv(pTHX_ register SV *sv) |
ff68c719 | 1948 | { |
1949 | if (!sv) | |
1950 | return 0; | |
1951 | if (SvGMAGICAL(sv)) { | |
1952 | mg_get(sv); | |
1953 | if (SvIOKp(sv)) | |
1954 | return SvUVX(sv); | |
1955 | if (SvNOKp(sv)) | |
1956 | return U_V(SvNVX(sv)); | |
36477c24 | 1957 | if (SvPOKp(sv) && SvLEN(sv)) |
1958 | return asUV(sv); | |
3fe9a6f1 | 1959 | if (!SvROK(sv)) { |
d008e5eb | 1960 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 1961 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
1d7c1841 | 1962 | report_uninit(); |
c6ee37c5 | 1963 | } |
36477c24 | 1964 | return 0; |
3fe9a6f1 | 1965 | } |
ff68c719 | 1966 | } |
1967 | if (SvTHINKFIRST(sv)) { | |
1968 | if (SvROK(sv)) { | |
ff68c719 | 1969 | SV* tmpstr; |
1554e226 DC |
1970 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) && |
1971 | (SvRV(tmpstr) != SvRV(sv))) | |
9e7bc3e8 | 1972 | return SvUV(tmpstr); |
56431972 | 1973 | return PTR2UV(SvRV(sv)); |
ff68c719 | 1974 | } |
8a818333 NIS |
1975 | if (SvREADONLY(sv) && SvFAKE(sv)) { |
1976 | sv_force_normal(sv); | |
1977 | } | |
0336b60e | 1978 | if (SvREADONLY(sv) && !SvOK(sv)) { |
0336b60e | 1979 | if (ckWARN(WARN_UNINITIALIZED)) |
1d7c1841 | 1980 | report_uninit(); |
ff68c719 | 1981 | return 0; |
1982 | } | |
1983 | } | |
25da4f38 IZ |
1984 | if (SvIOKp(sv)) { |
1985 | if (SvIsUV(sv)) { | |
1986 | return SvUVX(sv); | |
1987 | } | |
1988 | else { | |
1989 | return (UV)SvIVX(sv); | |
1990 | } | |
ff68c719 | 1991 | } |
1992 | if (SvNOKp(sv)) { | |
28e5dec8 JH |
1993 | /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv |
1994 | * without also getting a cached IV/UV from it at the same time | |
1995 | * (ie PV->NV conversion should detect loss of accuracy and cache | |
1996 | * IV or UV at same time to avoid this. */ | |
1997 | /* IV-over-UV optimisation - choose to cache IV if possible */ | |
1998 | ||
25da4f38 IZ |
1999 | if (SvTYPE(sv) == SVt_NV) |
2000 | sv_upgrade(sv, SVt_PVNV); | |
28e5dec8 JH |
2001 | |
2002 | (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */ | |
2003 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
f7bbb42a | 2004 | SvIVX(sv) = I_V(SvNVX(sv)); |
28e5dec8 JH |
2005 | if (SvNVX(sv) == (NV) SvIVX(sv) |
2006 | #ifndef NV_PRESERVES_UV | |
2007 | && (((UV)1 << NV_PRESERVES_UV_BITS) > | |
2008 | (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv))) | |
2009 | /* Don't flag it as "accurately an integer" if the number | |
2010 | came from a (by definition imprecise) NV operation, and | |
2011 | we're outside the range of NV integer precision */ | |
2012 | #endif | |
2013 | ) { | |
2014 | SvIOK_on(sv); /* Can this go wrong with rounding? NWC */ | |
2015 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
2016 | "0x%"UVxf" uv(%g => %"IVdf") (precise)\n", | |
2017 | PTR2UV(sv), | |
2018 | SvNVX(sv), | |
2019 | SvIVX(sv))); | |
2020 | ||
2021 | } else { | |
2022 | /* IV not precise. No need to convert from PV, as NV | |
2023 | conversion would already have cached IV if it detected | |
2024 | that PV->IV would be better than PV->NV->IV | |
2025 | flags already correct - don't set public IOK. */ | |
2026 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
2027 | "0x%"UVxf" uv(%g => %"IVdf") (imprecise)\n", | |
2028 | PTR2UV(sv), | |
2029 | SvNVX(sv), | |
2030 | SvIVX(sv))); | |
2031 | } | |
2032 | /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN, | |
2033 | but the cast (NV)IV_MIN rounds to a the value less (more | |
2034 | negative) than IV_MIN which happens to be equal to SvNVX ?? | |
2035 | Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and | |
2036 | NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and | |
2037 | (NV)UVX == NVX are both true, but the values differ. :-( | |
2038 | Hopefully for 2s complement IV_MIN is something like | |
2039 | 0x8000000000000000 which will be exact. NWC */ | |
2040 | } | |
2041 | else { | |
2042 | SvUVX(sv) = U_V(SvNVX(sv)); | |
2043 | if ( | |
2044 | (SvNVX(sv) == (NV) SvUVX(sv)) | |
2045 | #ifndef NV_PRESERVES_UV | |
2046 | /* Make sure it's not 0xFFFFFFFFFFFFFFFF */ | |
2047 | /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */ | |
2048 | && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv)) | |
2049 | /* Don't flag it as "accurately an integer" if the number | |
2050 | came from a (by definition imprecise) NV operation, and | |
2051 | we're outside the range of NV integer precision */ | |
2052 | #endif | |
2053 | ) | |
2054 | SvIOK_on(sv); | |
2055 | SvIsUV_on(sv); | |
1c846c1f | 2056 | DEBUG_c(PerlIO_printf(Perl_debug_log, |
28e5dec8 | 2057 | "0x%"UVxf" 2uv(%"UVuf" => %"IVdf") (as unsigned)\n", |
57def98f | 2058 | PTR2UV(sv), |
28e5dec8 JH |
2059 | SvUVX(sv), |
2060 | SvUVX(sv))); | |
25da4f38 | 2061 | } |
ff68c719 | 2062 | } |
2063 | else if (SvPOKp(sv) && SvLEN(sv)) { | |
25da4f38 IZ |
2064 | I32 numtype = looks_like_number(sv); |
2065 | ||
2066 | /* We want to avoid a possible problem when we cache a UV which | |
2067 | may be later translated to an NV, and the resulting NV is not | |
2068 | the translation of the initial data. | |
1c846c1f | 2069 | |
25da4f38 IZ |
2070 | This means that if we cache such a UV, we need to cache the |
2071 | NV as well. Moreover, we trade speed for space, and do not | |
2072 | cache the NV if not needed. | |
2073 | */ | |
16b7a9a4 | 2074 | |
28e5dec8 | 2075 | if ((numtype & ~IS_NUMBER_NEG) == IS_NUMBER_TO_INT_BY_ATOL) { |
f7bbb42a | 2076 | /* The NV may be reconstructed from IV - safe to cache IV, |
28e5dec8 JH |
2077 | which may be calculated by atol(). */ |
2078 | if (SvTYPE(sv) < SVt_PVIV) | |
f7bbb42a JH |
2079 | sv_upgrade(sv, SVt_PVIV); |
2080 | (void)SvIOK_on(sv); | |
28e5dec8 JH |
2081 | SvIVX(sv) = Atol(SvPVX(sv)); |
2082 | } else { | |
f7bbb42a | 2083 | #ifdef HAS_STRTOUL |
28e5dec8 | 2084 | UV u; |
f9172815 | 2085 | char *num_begin = SvPVX(sv); |
28e5dec8 | 2086 | int save_errno = errno; |
f9172815 JH |
2087 | |
2088 | /* seems that strtoul taking numbers that start with - is | |
2089 | implementation dependant, and can't be relied upon. */ | |
2090 | if (numtype & IS_NUMBER_NEG) { | |
2091 | /* Not totally defensive. assumine that looks_like_num | |
2092 | didn't lie about a - sign */ | |
2093 | while (isSPACE(*num_begin)) | |
2094 | num_begin++; | |
2095 | if (*num_begin == '-') | |
2096 | num_begin++; | |
2097 | } | |
2098 | ||
28e5dec8 JH |
2099 | /* Is it an integer that we could convert with strtoul? |
2100 | So try it, and if it doesn't set errno then it's pukka. | |
2101 | This should be faster than going atof and then thinking. */ | |
2102 | if (((numtype & (IS_NUMBER_TO_INT_BY_STRTOL | IS_NUMBER_NOT_INT)) | |
2103 | == IS_NUMBER_TO_INT_BY_STRTOL) | |
2104 | && ((errno = 0), 1) /* always true */ | |
f9172815 | 2105 | && ((u = Strtoul(num_begin, Null(char**), 10)), 1) /* ditto */ |
28e5dec8 | 2106 | && (errno == 0) |
f9172815 JH |
2107 | /* If known to be negative, check it didn't undeflow IV |
2108 | XXX possibly we should put more negative values as NVs | |
2109 | direct rather than go via atof below */ | |
2110 | && ((numtype & IS_NUMBER_NEG) ? (u <= (UV)IV_MIN) : 1)) { | |
28e5dec8 JH |
2111 | errno = save_errno; |
2112 | ||
2113 | if (SvTYPE(sv) < SVt_PVIV) | |
2114 | sv_upgrade(sv, SVt_PVIV); | |
2115 | (void)SvIOK_on(sv); | |
2116 | ||
2117 | /* If it's negative must use IV. | |
2118 | IV-over-UV optimisation */ | |
f9172815 JH |
2119 | if (numtype & IS_NUMBER_NEG) { |
2120 | SvIVX(sv) = -(IV)u; | |
2121 | } else if (u <= (UV) IV_MAX) { | |
28e5dec8 JH |
2122 | SvIVX(sv) = (IV)u; |
2123 | } else { | |
2124 | /* it didn't overflow, and it was positive. */ | |
2125 | SvUVX(sv) = u; | |
2126 | SvIsUV_on(sv); | |
2127 | } | |
2128 | } else | |
f7bbb42a | 2129 | #endif |
28e5dec8 JH |
2130 | { |
2131 | NV d; | |
2132 | #ifdef HAS_STRTOUL | |
2133 | /* Hopefully trace flow will optimise this away where possible | |
2134 | */ | |
2135 | errno = save_errno; | |
2136 | #endif | |
2137 | /* It wasn't an integer, or it overflowed, or we don't have | |
2138 | strtol. Do things the slow way - check if it's a IV etc. */ | |
2139 | d = Atof(SvPVX(sv)); | |
2140 | ||
2141 | if (SvTYPE(sv) < SVt_PVNV) | |
2142 | sv_upgrade(sv, SVt_PVNV); | |
2143 | SvNVX(sv) = d; | |
2144 | ||
2145 | if (! numtype && ckWARN(WARN_NUMERIC)) | |
2146 | not_a_number(sv); | |
2147 | ||
2148 | #if defined(USE_LONG_DOUBLE) | |
2149 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%" PERL_PRIgldbl ")\n", | |
2150 | PTR2UV(sv), SvNVX(sv))); | |
2151 | #else | |
2152 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%g)\n", | |
2153 | PTR2UV(sv), SvNVX(sv))); | |
2154 | #endif | |
2155 | ||
2156 | #ifdef NV_PRESERVES_UV | |
2157 | (void)SvIOKp_on(sv); | |
2158 | (void)SvNOK_on(sv); | |
2159 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
2160 | SvIVX(sv) = I_V(SvNVX(sv)); | |
2161 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) { | |
2162 | SvIOK_on(sv); | |
2163 | } else { | |
2164 | /* Integer is imprecise. NOK, IOKp */ | |
2165 | } | |
2166 | /* UV will not work better than IV */ | |
2167 | } else { | |
2168 | if (SvNVX(sv) > (NV)UV_MAX) { | |
2169 | SvIsUV_on(sv); | |
2170 | /* Integer is inaccurate. NOK, IOKp, is UV */ | |
2171 | SvUVX(sv) = UV_MAX; | |
2172 | SvIsUV_on(sv); | |
2173 | } else { | |
2174 | SvUVX(sv) = U_V(SvNVX(sv)); | |
2175 | /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs | |
2176 | NV preservse UV so can do correct comparison. */ | |
2177 | if ((NV)(SvUVX(sv)) == SvNVX(sv)) { | |
2178 | SvIOK_on(sv); | |
2179 | SvIsUV_on(sv); | |
2180 | } else { | |
2181 | /* Integer is imprecise. NOK, IOKp, is UV */ | |
2182 | SvIsUV_on(sv); | |
2183 | } | |
2184 | } | |
2185 | } | |
2186 | #else /* NV_PRESERVES_UV */ | |
2187 | if (((UV)1 << NV_PRESERVES_UV_BITS) > | |
2188 | U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) { | |
2189 | /* Small enough to preserve all bits. */ | |
2190 | (void)SvIOKp_on(sv); | |
2191 | SvNOK_on(sv); | |
2192 | SvIVX(sv) = I_V(SvNVX(sv)); | |
2193 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) | |
2194 | SvIOK_on(sv); | |
2195 | /* Assumption: first non-preserved integer is < IV_MAX, | |
2196 | this NV is in the preserved range, therefore: */ | |
2197 | if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv)) | |
2198 | < (UV)IV_MAX)) { | |
2199 | 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); | |
2200 | } | |
2201 | } else | |
2202 | sv_2iuv_non_preserve (sv, numtype); | |
2203 | #endif /* NV_PRESERVES_UV */ | |
2204 | } | |
f7bbb42a | 2205 | } |
ff68c719 | 2206 | } |
2207 | else { | |
d008e5eb | 2208 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 2209 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
1d7c1841 | 2210 | report_uninit(); |
c6ee37c5 | 2211 | } |
25da4f38 IZ |
2212 | if (SvTYPE(sv) < SVt_IV) |
2213 | /* Typically the caller expects that sv_any is not NULL now. */ | |
2214 | sv_upgrade(sv, SVt_IV); | |
ff68c719 | 2215 | return 0; |
2216 | } | |
25da4f38 | 2217 | |
1d7c1841 GS |
2218 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n", |
2219 | PTR2UV(sv),SvUVX(sv))); | |
25da4f38 | 2220 | return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv); |
ff68c719 | 2221 | } |
2222 | ||
65202027 | 2223 | NV |
864dbfa3 | 2224 | Perl_sv_2nv(pTHX_ register SV *sv) |
79072805 LW |
2225 | { |
2226 | if (!sv) | |
2227 | return 0.0; | |
8990e307 | 2228 | if (SvGMAGICAL(sv)) { |
463ee0b2 LW |
2229 | mg_get(sv); |
2230 | if (SvNOKp(sv)) | |
2231 | return SvNVX(sv); | |
a0d0e21e | 2232 | if (SvPOKp(sv) && SvLEN(sv)) { |
599cee73 | 2233 | if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !looks_like_number(sv)) |
a0d0e21e | 2234 | not_a_number(sv); |
097ee67d | 2235 | return Atof(SvPVX(sv)); |
a0d0e21e | 2236 | } |
25da4f38 | 2237 | if (SvIOKp(sv)) { |
1c846c1f | 2238 | if (SvIsUV(sv)) |
65202027 | 2239 | return (NV)SvUVX(sv); |
25da4f38 | 2240 | else |
65202027 | 2241 | return (NV)SvIVX(sv); |
25da4f38 | 2242 | } |
16d20bd9 | 2243 | if (!SvROK(sv)) { |
d008e5eb | 2244 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 2245 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
1d7c1841 | 2246 | report_uninit(); |
c6ee37c5 | 2247 | } |
16d20bd9 AD |
2248 | return 0; |
2249 | } | |
463ee0b2 | 2250 | } |
ed6116ce | 2251 | if (SvTHINKFIRST(sv)) { |
a0d0e21e | 2252 | if (SvROK(sv)) { |
a0d0e21e | 2253 | SV* tmpstr; |
1554e226 DC |
2254 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) && |
2255 | (SvRV(tmpstr) != SvRV(sv))) | |
9e7bc3e8 | 2256 | return SvNV(tmpstr); |
56431972 | 2257 | return PTR2NV(SvRV(sv)); |
a0d0e21e | 2258 | } |
8a818333 NIS |
2259 | if (SvREADONLY(sv) && SvFAKE(sv)) { |
2260 | sv_force_normal(sv); | |
2261 | } | |
0336b60e | 2262 | if (SvREADONLY(sv) && !SvOK(sv)) { |
599cee73 | 2263 | if (ckWARN(WARN_UNINITIALIZED)) |
1d7c1841 | 2264 | report_uninit(); |
ed6116ce LW |
2265 | return 0.0; |
2266 | } | |
79072805 LW |
2267 | } |
2268 | if (SvTYPE(sv) < SVt_NV) { | |
463ee0b2 LW |
2269 | if (SvTYPE(sv) == SVt_IV) |
2270 | sv_upgrade(sv, SVt_PVNV); | |
2271 | else | |
2272 | sv_upgrade(sv, SVt_NV); | |
572bbb43 | 2273 | #if defined(USE_LONG_DOUBLE) |
097ee67d | 2274 | DEBUG_c({ |
f93f4e46 | 2275 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
2276 | PerlIO_printf(Perl_debug_log, |
2277 | "0x%"UVxf" num(%" PERL_PRIgldbl ")\n", | |
2278 | PTR2UV(sv), SvNVX(sv)); | |
572bbb43 GS |
2279 | RESTORE_NUMERIC_LOCAL(); |
2280 | }); | |
65202027 | 2281 | #else |
572bbb43 | 2282 | DEBUG_c({ |
f93f4e46 | 2283 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
2284 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%g)\n", |
2285 | PTR2UV(sv), SvNVX(sv)); | |
097ee67d JH |
2286 | RESTORE_NUMERIC_LOCAL(); |
2287 | }); | |
572bbb43 | 2288 | #endif |
79072805 LW |
2289 | } |
2290 | else if (SvTYPE(sv) < SVt_PVNV) | |
2291 | sv_upgrade(sv, SVt_PVNV); | |
748a9306 LW |
2292 | if (SvIOKp(sv) && |
2293 | (!SvPOKp(sv) || !strchr(SvPVX(sv),'.') || !looks_like_number(sv))) | |
93a17b20 | 2294 | { |
65202027 | 2295 | SvNVX(sv) = SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv); |
28e5dec8 JH |
2296 | #ifdef NV_PRESERVES_UV |
2297 | SvNOK_on(sv); | |
2298 | #else | |
2299 | /* Only set the public NV OK flag if this NV preserves the IV */ | |
2300 | /* Check it's not 0xFFFFFFFFFFFFFFFF */ | |
2301 | if (SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv)))) | |
2302 | : (SvIVX(sv) == I_V(SvNVX(sv)))) | |
2303 | SvNOK_on(sv); | |
2304 | else | |
2305 | SvNOKp_on(sv); | |
2306 | #endif | |
93a17b20 | 2307 | } |
748a9306 | 2308 | else if (SvPOKp(sv) && SvLEN(sv)) { |
599cee73 | 2309 | if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !looks_like_number(sv)) |
a0d0e21e | 2310 | not_a_number(sv); |
097ee67d | 2311 | SvNVX(sv) = Atof(SvPVX(sv)); |
28e5dec8 JH |
2312 | #ifdef NV_PRESERVES_UV |
2313 | SvNOK_on(sv); | |
2314 | #else | |
2315 | /* Only set the public NV OK flag if this NV preserves the value in | |
2316 | the PV at least as well as an IV/UV would. | |
2317 | Not sure how to do this 100% reliably. */ | |
2318 | /* if that shift count is out of range then Configure's test is | |
2319 | wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS == | |
2320 | UV_BITS */ | |
2321 | if (((UV)1 << NV_PRESERVES_UV_BITS) > | |
2322 | U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) | |
2323 | SvNOK_on(sv); /* Definitely small enough to preserve all bits */ | |
2324 | else if (SvNVX(sv) < (NV)IV_MIN || SvNVX(sv) > (NV)UV_MAX) { | |
2325 | /* Definitely too large/small to fit in an integer, so no loss | |
2326 | of precision going to integer in the future via NV */ | |
2327 | SvNOK_on(sv); | |
2328 | } else { | |
2329 | /* Is it something we can run through strtol etc (ie no | |
2330 | trailing exponent part)? */ | |
2331 | int numtype = looks_like_number(sv); | |
2332 | /* XXX probably should cache this if called above */ | |
2333 | ||
2334 | if (!(numtype & | |
2335 | (IS_NUMBER_TO_INT_BY_ATOL | IS_NUMBER_TO_INT_BY_STRTOL))) { | |
2336 | /* Can't use strtol etc to convert this string, so don't try */ | |
2337 | SvNOK_on(sv); | |
2338 | } else | |
2339 | sv_2inuv_non_preserve (sv, numtype); | |
2340 | } | |
2341 | #endif /* NV_PRESERVES_UV */ | |
93a17b20 | 2342 | } |
79072805 | 2343 | else { |
599cee73 | 2344 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP)) |
1d7c1841 | 2345 | report_uninit(); |
25da4f38 IZ |
2346 | if (SvTYPE(sv) < SVt_NV) |
2347 | /* Typically the caller expects that sv_any is not NULL now. */ | |
28e5dec8 JH |
2348 | /* XXX Ilya implies that this is a bug in callers that assume this |
2349 | and ideally should be fixed. */ | |
25da4f38 | 2350 | sv_upgrade(sv, SVt_NV); |
a0d0e21e | 2351 | return 0.0; |
79072805 | 2352 | } |
572bbb43 | 2353 | #if defined(USE_LONG_DOUBLE) |
097ee67d | 2354 | DEBUG_c({ |
f93f4e46 | 2355 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
2356 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n", |
2357 | PTR2UV(sv), SvNVX(sv)); | |
572bbb43 GS |
2358 | RESTORE_NUMERIC_LOCAL(); |
2359 | }); | |
65202027 | 2360 | #else |
572bbb43 | 2361 | DEBUG_c({ |
f93f4e46 | 2362 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
2363 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%g)\n", |
2364 | PTR2UV(sv), SvNVX(sv)); | |
097ee67d JH |
2365 | RESTORE_NUMERIC_LOCAL(); |
2366 | }); | |
572bbb43 | 2367 | #endif |
463ee0b2 | 2368 | return SvNVX(sv); |
79072805 LW |
2369 | } |
2370 | ||
76e3520e | 2371 | STATIC IV |
cea2e8a9 | 2372 | S_asIV(pTHX_ SV *sv) |
36477c24 | 2373 | { |
2374 | I32 numtype = looks_like_number(sv); | |
65202027 | 2375 | NV d; |
36477c24 | 2376 | |
25da4f38 | 2377 | if (numtype & IS_NUMBER_TO_INT_BY_ATOL) |
cf2093f6 | 2378 | return Atol(SvPVX(sv)); |
d008e5eb | 2379 | if (!numtype) { |
d008e5eb GS |
2380 | if (ckWARN(WARN_NUMERIC)) |
2381 | not_a_number(sv); | |
2382 | } | |
097ee67d | 2383 | d = Atof(SvPVX(sv)); |
25da4f38 | 2384 | return I_V(d); |
36477c24 | 2385 | } |
2386 | ||
76e3520e | 2387 | STATIC UV |
cea2e8a9 | 2388 | S_asUV(pTHX_ SV *sv) |
36477c24 | 2389 | { |
2390 | I32 numtype = looks_like_number(sv); | |
2391 | ||
84902520 | 2392 | #ifdef HAS_STRTOUL |
25da4f38 | 2393 | if (numtype & IS_NUMBER_TO_INT_BY_ATOL) |
cf2093f6 | 2394 | return Strtoul(SvPVX(sv), Null(char**), 10); |
84902520 | 2395 | #endif |
d008e5eb | 2396 | if (!numtype) { |
d008e5eb GS |
2397 | if (ckWARN(WARN_NUMERIC)) |
2398 | not_a_number(sv); | |
2399 | } | |
097ee67d | 2400 | return U_V(Atof(SvPVX(sv))); |
36477c24 | 2401 | } |
2402 | ||
25da4f38 IZ |
2403 | /* |
2404 | * Returns a combination of (advisory only - can get false negatives) | |
28e5dec8 JH |
2405 | * IS_NUMBER_TO_INT_BY_ATOL, IS_NUMBER_TO_INT_BY_ATOF |
2406 | * IS_NUMBER_LONGER_THAN_IV_MAX, IS_NUMBER_AS_LONG_AS_IV_MAX | |
2407 | * IS_NUMBER_NOT_INT, IS_NUMBER_NEG, IS_NUMBER_INFINITY | |
25da4f38 IZ |
2408 | * 0 if does not look like number. |
2409 | * | |
28e5dec8 JH |
2410 | * (atol and strtol stop when they hit a decimal point. strtol will return |
2411 | * LONG_MAX and LONG_MIN when given out of range values. ANSI says they should | |
2412 | * do this, and vendors have had 11 years to get it right. | |
2413 | * However, will try to make it still work with only atol | |
2414 | * | |
2415 | * IS_NUMBER_TO_INT_BY_ATOL 123456789 or 123456789.3 definitely < IV_MAX | |
2416 | * IS_NUMBER_TO_INT_BY_STRTOL 123456789 or 123456789.3 if digits = IV_MAX | |
2417 | * IS_NUMBER_TO_INT_BY_ATOF 123456789e0 or >> IV_MAX | |
2418 | * IS_NUMBER_LONGER_THAN_IV_MAX lots of digits, don't bother with atol | |
2419 | * IS_NUMBER_AS_LONG_AS_IV_MAX atol might hit LONG_MAX, might not. | |
2420 | * IS_NUMBER_NOT_INT saw "." or "e" | |
2421 | * IS_NUMBER_NEG | |
300aed98 | 2422 | * IS_NUMBER_INFINITY |
25da4f38 IZ |
2423 | */ |
2424 | ||
954c1994 GS |
2425 | /* |
2426 | =for apidoc looks_like_number | |
2427 | ||
2428 | Test if an the content of an SV looks like a number (or is a | |
28e5dec8 JH |
2429 | number). C<Inf> and C<Infinity> are treated as numbers (so will not |
2430 | issue a non-numeric warning), even if your atof() doesn't grok them. | |
954c1994 GS |
2431 | |
2432 | =cut | |
2433 | */ | |
2434 | ||
36477c24 | 2435 | I32 |
864dbfa3 | 2436 | Perl_looks_like_number(pTHX_ SV *sv) |
36477c24 | 2437 | { |
2438 | register char *s; | |
2439 | register char *send; | |
2440 | register char *sbegin; | |
25da4f38 IZ |
2441 | register char *nbegin; |
2442 | I32 numtype = 0; | |
300aed98 | 2443 | I32 sawinf = 0; |
36477c24 | 2444 | STRLEN len; |
2445 | ||
2446 | if (SvPOK(sv)) { | |
1c846c1f | 2447 | sbegin = SvPVX(sv); |
36477c24 | 2448 | len = SvCUR(sv); |
2449 | } | |
2450 | else if (SvPOKp(sv)) | |
2451 | sbegin = SvPV(sv, len); | |
2452 | else | |
2453 | return 1; | |
2454 | send = sbegin + len; | |
2455 | ||
2456 | s = sbegin; | |
2457 | while (isSPACE(*s)) | |
2458 | s++; | |
25da4f38 IZ |
2459 | if (*s == '-') { |
2460 | s++; | |
2461 | numtype = IS_NUMBER_NEG; | |
2462 | } | |
2463 | else if (*s == '+') | |
36477c24 | 2464 | s++; |
ff0cee69 | 2465 | |
25da4f38 IZ |
2466 | nbegin = s; |
2467 | /* | |
28e5dec8 JH |
2468 | * we return IS_NUMBER_TO_INT_BY_ATOL if the number can converted to |
2469 | * integer with atol() without overflow, IS_NUMBER_TO_INT_BY_STRTOL if | |
2470 | * possibly slightly larger than max int, IS_NUMBER_TO_INT_BY_ATOF if you | |
2471 | * will need (int)atof(). | |
25da4f38 IZ |
2472 | */ |
2473 | ||
300aed98 | 2474 | /* next must be digit or the radix separator or beginning of infinity */ |
ff0cee69 | 2475 | if (isDIGIT(*s)) { |
2476 | do { | |
2477 | s++; | |
2478 | } while (isDIGIT(*s)); | |
25da4f38 | 2479 | |
28e5dec8 JH |
2480 | /* Aaargh. long long really is irritating. |
2481 | In the gospel according to ANSI 1989, it is an axiom that "long" | |
2482 | is the longest integer type, and that if you don't know how long | |
2483 | something is you can cast it to long, and nothing will be lost | |
2484 | (except possibly speed of execution if long is slower than the | |
2485 | type is was). | |
2486 | Now, one can't be sure if the old rules apply, or long long | |
2487 | (or some other newfangled thing) is actually longer than the | |
2488 | (formerly) longest thing. | |
2489 | */ | |
2490 | /* This lot will work for 64 bit *as long as* either | |
2491 | either long is 64 bit | |
2492 | or we can find both strtol/strtoq and strtoul/strtouq | |
2493 | If not, we really should refuse to let the user use 64 bit IVs | |
2494 | By "64 bit" I really mean IVs that don't get preserved by NVs | |
2495 | It also should work for 128 bit IVs. Can any lend me a machine to | |
2496 | test this? | |
2497 | */ | |
2498 | if (s - nbegin > TYPE_DIGITS(UV)) /* Cannot cache ato[ul]() */ | |
2499 | numtype |= IS_NUMBER_TO_INT_BY_ATOF | IS_NUMBER_LONGER_THAN_IV_MAX; | |
2500 | else if (s - nbegin < BIT_DIGITS(((sizeof (IV)>sizeof (long)) | |
2501 | ? sizeof(long) : sizeof (IV))*8-1)) | |
f7bbb42a | 2502 | numtype |= IS_NUMBER_TO_INT_BY_ATOL; |
28e5dec8 JH |
2503 | else |
2504 | /* Can't be sure either way. (For 64 bit UV, 63 bit IV is 1 decimal | |
2505 | digit less (IV_MAX= 9223372036854775807, | |
2506 | UV_MAX= 18446744073709551615) so be cautious */ | |
2507 | numtype |= IS_NUMBER_TO_INT_BY_STRTOL | IS_NUMBER_AS_LONG_AS_IV_MAX; | |
25da4f38 | 2508 | |
097ee67d | 2509 | if (*s == '.' |
1c846c1f | 2510 | #ifdef USE_LOCALE_NUMERIC |
097ee67d JH |
2511 | || IS_NUMERIC_RADIX(*s) |
2512 | #endif | |
2513 | ) { | |
ff0cee69 | 2514 | s++; |
28e5dec8 | 2515 | numtype |= IS_NUMBER_NOT_INT; |
097ee67d | 2516 | while (isDIGIT(*s)) /* optional digits after the radix */ |
ff0cee69 | 2517 | s++; |
2518 | } | |
36477c24 | 2519 | } |
097ee67d | 2520 | else if (*s == '.' |
1c846c1f | 2521 | #ifdef USE_LOCALE_NUMERIC |
097ee67d JH |
2522 | || IS_NUMERIC_RADIX(*s) |
2523 | #endif | |
2524 | ) { | |
ff0cee69 | 2525 | s++; |
28e5dec8 | 2526 | numtype |= IS_NUMBER_TO_INT_BY_ATOL | IS_NUMBER_NOT_INT; |
097ee67d | 2527 | /* no digits before the radix means we need digits after it */ |
ff0cee69 | 2528 | if (isDIGIT(*s)) { |
2529 | do { | |
2530 | s++; | |
2531 | } while (isDIGIT(*s)); | |
2532 | } | |
2533 | else | |
2534 | return 0; | |
2535 | } | |
300aed98 JH |
2536 | else if (*s == 'I' || *s == 'i') { |
2537 | s++; if (*s != 'N' && *s != 'n') return 0; | |
2538 | s++; if (*s != 'F' && *s != 'f') return 0; | |
2539 | s++; if (*s == 'I' || *s == 'i') { | |
2540 | s++; if (*s != 'N' && *s != 'n') return 0; | |
2541 | s++; if (*s != 'I' && *s != 'i') return 0; | |
2542 | s++; if (*s != 'T' && *s != 't') return 0; | |
2543 | s++; if (*s != 'Y' && *s != 'y') return 0; | |
99938567 | 2544 | s++; |
300aed98 JH |
2545 | } |
2546 | sawinf = 1; | |
2547 | } | |
ff0cee69 | 2548 | else |
2549 | return 0; | |
2550 | ||
300aed98 | 2551 | if (sawinf) |
28e5dec8 JH |
2552 | numtype = (numtype & IS_NUMBER_NEG) /* Keep track of sign */ |
2553 | | IS_NUMBER_INFINITY | IS_NUMBER_NOT_INT; | |
300aed98 JH |
2554 | else { |
2555 | /* we can have an optional exponent part */ | |
2556 | if (*s == 'e' || *s == 'E') { | |
28e5dec8 JH |
2557 | numtype &= IS_NUMBER_NEG; |
2558 | numtype |= IS_NUMBER_TO_INT_BY_ATOF | IS_NUMBER_NOT_INT; | |
36477c24 | 2559 | s++; |
300aed98 JH |
2560 | if (*s == '+' || *s == '-') |
2561 | s++; | |
2562 | if (isDIGIT(*s)) { | |
2563 | do { | |
2564 | s++; | |
2565 | } while (isDIGIT(*s)); | |
2566 | } | |
2567 | else | |
2568 | return 0; | |
2569 | } | |
36477c24 | 2570 | } |
2571 | while (isSPACE(*s)) | |
2572 | s++; | |
80f3f388 | 2573 | if (s >= send) |
36477c24 | 2574 | return numtype; |
2575 | if (len == 10 && memEQ(sbegin, "0 but true", 10)) | |
25da4f38 | 2576 | return IS_NUMBER_TO_INT_BY_ATOL; |
36477c24 | 2577 | return 0; |
2578 | } | |
2579 | ||
79072805 | 2580 | char * |
864dbfa3 | 2581 | Perl_sv_2pv_nolen(pTHX_ register SV *sv) |
1fa8b10d JD |
2582 | { |
2583 | STRLEN n_a; | |
2584 | return sv_2pv(sv, &n_a); | |
2585 | } | |
2586 | ||
25da4f38 | 2587 | /* We assume that buf is at least TYPE_CHARS(UV) long. */ |
864dbfa3 | 2588 | static char * |
25da4f38 IZ |
2589 | uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob) |
2590 | { | |
25da4f38 IZ |
2591 | char *ptr = buf + TYPE_CHARS(UV); |
2592 | char *ebuf = ptr; | |
2593 | int sign; | |
25da4f38 IZ |
2594 | |
2595 | if (is_uv) | |
2596 | sign = 0; | |
2597 | else if (iv >= 0) { | |
2598 | uv = iv; | |
2599 | sign = 0; | |
2600 | } else { | |
2601 | uv = -iv; | |
2602 | sign = 1; | |
2603 | } | |
2604 | do { | |
2605 | *--ptr = '0' + (uv % 10); | |
2606 | } while (uv /= 10); | |
2607 | if (sign) | |
2608 | *--ptr = '-'; | |
2609 | *peob = ebuf; | |
2610 | return ptr; | |
2611 | } | |
2612 | ||
1fa8b10d | 2613 | char * |
864dbfa3 | 2614 | Perl_sv_2pv(pTHX_ register SV *sv, STRLEN *lp) |
79072805 LW |
2615 | { |
2616 | register char *s; | |
2617 | int olderrno; | |
46fc3d4c | 2618 | SV *tsv; |
25da4f38 IZ |
2619 | char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */ |
2620 | char *tmpbuf = tbuf; | |
79072805 | 2621 | |
463ee0b2 LW |
2622 | if (!sv) { |
2623 | *lp = 0; | |
2624 | return ""; | |
2625 | } | |
8990e307 | 2626 | if (SvGMAGICAL(sv)) { |
463ee0b2 LW |
2627 | mg_get(sv); |
2628 | if (SvPOKp(sv)) { | |
2629 | *lp = SvCUR(sv); | |
2630 | return SvPVX(sv); | |
2631 | } | |
cf2093f6 | 2632 | if (SvIOKp(sv)) { |
1c846c1f | 2633 | if (SvIsUV(sv)) |
57def98f | 2634 | (void)sprintf(tmpbuf,"%"UVuf, (UV)SvUVX(sv)); |
cf2093f6 | 2635 | else |
57def98f | 2636 | (void)sprintf(tmpbuf,"%"IVdf, (IV)SvIVX(sv)); |
46fc3d4c | 2637 | tsv = Nullsv; |
a0d0e21e | 2638 | goto tokensave; |
463ee0b2 LW |
2639 | } |
2640 | if (SvNOKp(sv)) { | |
2d4389e4 | 2641 | Gconvert(SvNVX(sv), NV_DIG, 0, tmpbuf); |
46fc3d4c | 2642 | tsv = Nullsv; |
a0d0e21e | 2643 | goto tokensave; |
463ee0b2 | 2644 | } |
16d20bd9 | 2645 | if (!SvROK(sv)) { |
d008e5eb | 2646 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 2647 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
1d7c1841 | 2648 | report_uninit(); |
c6ee37c5 | 2649 | } |
16d20bd9 AD |
2650 | *lp = 0; |
2651 | return ""; | |
2652 | } | |
463ee0b2 | 2653 | } |
ed6116ce LW |
2654 | if (SvTHINKFIRST(sv)) { |
2655 | if (SvROK(sv)) { | |
a0d0e21e | 2656 | SV* tmpstr; |
1554e226 DC |
2657 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)) && |
2658 | (SvRV(tmpstr) != SvRV(sv))) | |
9e7bc3e8 | 2659 | return SvPV(tmpstr,*lp); |
ed6116ce LW |
2660 | sv = (SV*)SvRV(sv); |
2661 | if (!sv) | |
2662 | s = "NULLREF"; | |
2663 | else { | |
f9277f47 IZ |
2664 | MAGIC *mg; |
2665 | ||
ed6116ce | 2666 | switch (SvTYPE(sv)) { |
f9277f47 IZ |
2667 | case SVt_PVMG: |
2668 | if ( ((SvFLAGS(sv) & | |
1c846c1f | 2669 | (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG)) |
3149a8e4 | 2670 | == (SVs_OBJECT|SVs_RMG)) |
57668c4d | 2671 | && strEQ(s=HvNAME(SvSTASH(sv)), "Regexp") |
f9277f47 | 2672 | && (mg = mg_find(sv, 'r'))) { |
2cd61cdb | 2673 | regexp *re = (regexp *)mg->mg_obj; |
1bd3ad17 | 2674 | |
2cd61cdb | 2675 | if (!mg->mg_ptr) { |
8782bef2 GB |
2676 | char *fptr = "msix"; |
2677 | char reflags[6]; | |
2678 | char ch; | |
2679 | int left = 0; | |
2680 | int right = 4; | |
2681 | U16 reganch = (re->reganch & PMf_COMPILETIME) >> 12; | |
2682 | ||
155aba94 | 2683 | while((ch = *fptr++)) { |
8782bef2 GB |
2684 | if(reganch & 1) { |
2685 | reflags[left++] = ch; | |
2686 | } | |
2687 | else { | |
2688 | reflags[right--] = ch; | |
2689 | } | |
2690 | reganch >>= 1; | |
2691 | } | |
2692 | if(left != 4) { | |
2693 | reflags[left] = '-'; | |
2694 | left = 5; | |
2695 | } | |
2696 | ||
2697 | mg->mg_len = re->prelen + 4 + left; | |
2698 | New(616, mg->mg_ptr, mg->mg_len + 1 + left, char); | |
2699 | Copy("(?", mg->mg_ptr, 2, char); | |
2700 | Copy(reflags, mg->mg_ptr+2, left, char); | |
2701 | Copy(":", mg->mg_ptr+left+2, 1, char); | |
2702 | Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char); | |
1bd3ad17 IZ |
2703 | mg->mg_ptr[mg->mg_len - 1] = ')'; |
2704 | mg->mg_ptr[mg->mg_len] = 0; | |
2705 | } | |
3280af22 | 2706 | PL_reginterp_cnt += re->program[0].next_off; |
1bd3ad17 IZ |
2707 | *lp = mg->mg_len; |
2708 | return mg->mg_ptr; | |
f9277f47 IZ |
2709 | } |
2710 | /* Fall through */ | |
ed6116ce LW |
2711 | case SVt_NULL: |
2712 | case SVt_IV: | |
2713 | case SVt_NV: | |
2714 | case SVt_RV: | |
2715 | case SVt_PV: | |
2716 | case SVt_PVIV: | |
2717 | case SVt_PVNV: | |
81689caa HS |
2718 | case SVt_PVBM: if (SvROK(sv)) |
2719 | s = "REF"; | |
2720 | else | |
2721 | s = "SCALAR"; break; | |
ed6116ce LW |
2722 | case SVt_PVLV: s = "LVALUE"; break; |
2723 | case SVt_PVAV: s = "ARRAY"; break; | |
2724 | case SVt_PVHV: s = "HASH"; break; | |
2725 | case SVt_PVCV: s = "CODE"; break; | |
2726 | case SVt_PVGV: s = "GLOB"; break; | |
1d2dff63 | 2727 | case SVt_PVFM: s = "FORMAT"; break; |
36477c24 | 2728 | case SVt_PVIO: s = "IO"; break; |
ed6116ce LW |
2729 | default: s = "UNKNOWN"; break; |
2730 | } | |
46fc3d4c | 2731 | tsv = NEWSV(0,0); |
ed6116ce | 2732 | if (SvOBJECT(sv)) |
cea2e8a9 | 2733 | Perl_sv_setpvf(aTHX_ tsv, "%s=%s", HvNAME(SvSTASH(sv)), s); |
ed6116ce | 2734 | else |
46fc3d4c | 2735 | sv_setpv(tsv, s); |
57def98f | 2736 | Perl_sv_catpvf(aTHX_ tsv, "(0x%"UVxf")", PTR2UV(sv)); |
a0d0e21e | 2737 | goto tokensaveref; |
463ee0b2 | 2738 | } |
ed6116ce LW |
2739 | *lp = strlen(s); |
2740 | return s; | |
79072805 | 2741 | } |
0336b60e | 2742 | if (SvREADONLY(sv) && !SvOK(sv)) { |
0336b60e | 2743 | if (ckWARN(WARN_UNINITIALIZED)) |
1d7c1841 | 2744 | report_uninit(); |
ed6116ce LW |
2745 | *lp = 0; |
2746 | return ""; | |
79072805 | 2747 | } |
79072805 | 2748 | } |
28e5dec8 JH |
2749 | if (SvIOK(sv) || ((SvIOKp(sv) && !SvNOKp(sv)))) { |
2750 | /* I'm assuming that if both IV and NV are equally valid then | |
2751 | converting the IV is going to be more efficient */ | |
2752 | U32 isIOK = SvIOK(sv); | |
2753 | U32 isUIOK = SvIsUV(sv); | |
2754 | char buf[TYPE_CHARS(UV)]; | |
2755 | char *ebuf, *ptr; | |
2756 | ||
2757 | if (SvTYPE(sv) < SVt_PVIV) | |
2758 | sv_upgrade(sv, SVt_PVIV); | |
2759 | if (isUIOK) | |
2760 | ptr = uiv_2buf(buf, 0, SvUVX(sv), 1, &ebuf); | |
2761 | else | |
2762 | ptr = uiv_2buf(buf, SvIVX(sv), 0, 0, &ebuf); | |
2763 | SvGROW(sv, ebuf - ptr + 1); /* inlined from sv_setpvn */ | |
2764 | Move(ptr,SvPVX(sv),ebuf - ptr,char); | |
2765 | SvCUR_set(sv, ebuf - ptr); | |
2766 | s = SvEND(sv); | |
2767 | *s = '\0'; | |
2768 | if (isIOK) | |
2769 | SvIOK_on(sv); | |
2770 | else | |
2771 | SvIOKp_on(sv); | |
2772 | if (isUIOK) | |
2773 | SvIsUV_on(sv); | |
2774 | } | |
2775 | else if (SvNOKp(sv)) { | |
79072805 LW |
2776 | if (SvTYPE(sv) < SVt_PVNV) |
2777 | sv_upgrade(sv, SVt_PVNV); | |
1c846c1f | 2778 | /* The +20 is pure guesswork. Configure test needed. --jhi */ |
59155cc0 | 2779 | SvGROW(sv, NV_DIG + 20); |
463ee0b2 | 2780 | s = SvPVX(sv); |
79072805 | 2781 | olderrno = errno; /* some Xenix systems wipe out errno here */ |
79072805 | 2782 | #ifdef apollo |
463ee0b2 | 2783 | if (SvNVX(sv) == 0.0) |
79072805 LW |
2784 | (void)strcpy(s,"0"); |
2785 | else | |
2786 | #endif /*apollo*/ | |
bbce6d69 | 2787 | { |
2d4389e4 | 2788 | Gconvert(SvNVX(sv), NV_DIG, 0, s); |
bbce6d69 | 2789 | } |
79072805 | 2790 | errno = olderrno; |
a0d0e21e LW |
2791 | #ifdef FIXNEGATIVEZERO |
2792 | if (*s == '-' && s[1] == '0' && !s[2]) | |
2793 | strcpy(s,"0"); | |
2794 | #endif | |
79072805 LW |
2795 | while (*s) s++; |
2796 | #ifdef hcx | |
2797 | if (s[-1] == '.') | |
46fc3d4c | 2798 | *--s = '\0'; |
79072805 LW |
2799 | #endif |
2800 | } | |
79072805 | 2801 | else { |
0336b60e IZ |
2802 | if (ckWARN(WARN_UNINITIALIZED) |
2803 | && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP)) | |
1d7c1841 | 2804 | report_uninit(); |
a0d0e21e | 2805 | *lp = 0; |
25da4f38 IZ |
2806 | if (SvTYPE(sv) < SVt_PV) |
2807 | /* Typically the caller expects that sv_any is not NULL now. */ | |
2808 | sv_upgrade(sv, SVt_PV); | |
a0d0e21e | 2809 | return ""; |
79072805 | 2810 | } |
463ee0b2 LW |
2811 | *lp = s - SvPVX(sv); |
2812 | SvCUR_set(sv, *lp); | |
79072805 | 2813 | SvPOK_on(sv); |
1d7c1841 GS |
2814 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n", |
2815 | PTR2UV(sv),SvPVX(sv))); | |
463ee0b2 | 2816 | return SvPVX(sv); |
a0d0e21e LW |
2817 | |
2818 | tokensave: | |
2819 | if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */ | |
2820 | /* Sneaky stuff here */ | |
2821 | ||
2822 | tokensaveref: | |
46fc3d4c | 2823 | if (!tsv) |
96827780 | 2824 | tsv = newSVpv(tmpbuf, 0); |
46fc3d4c | 2825 | sv_2mortal(tsv); |
2826 | *lp = SvCUR(tsv); | |
2827 | return SvPVX(tsv); | |
a0d0e21e LW |
2828 | } |
2829 | else { | |
2830 | STRLEN len; | |
46fc3d4c | 2831 | char *t; |
2832 | ||
2833 | if (tsv) { | |
2834 | sv_2mortal(tsv); | |
2835 | t = SvPVX(tsv); | |
2836 | len = SvCUR(tsv); | |
2837 | } | |
2838 | else { | |
96827780 MB |
2839 | t = tmpbuf; |
2840 | len = strlen(tmpbuf); | |
46fc3d4c | 2841 | } |
a0d0e21e | 2842 | #ifdef FIXNEGATIVEZERO |
46fc3d4c | 2843 | if (len == 2 && t[0] == '-' && t[1] == '0') { |
2844 | t = "0"; | |
2845 | len = 1; | |
2846 | } | |
a0d0e21e LW |
2847 | #endif |
2848 | (void)SvUPGRADE(sv, SVt_PV); | |
46fc3d4c | 2849 | *lp = len; |
a0d0e21e LW |
2850 | s = SvGROW(sv, len + 1); |
2851 | SvCUR_set(sv, len); | |
46fc3d4c | 2852 | (void)strcpy(s, t); |
6bf554b4 | 2853 | SvPOKp_on(sv); |
a0d0e21e LW |
2854 | return s; |
2855 | } | |
463ee0b2 LW |
2856 | } |
2857 | ||
7340a771 GS |
2858 | char * |
2859 | Perl_sv_2pvbyte_nolen(pTHX_ register SV *sv) | |
2860 | { | |
560a288e GS |
2861 | STRLEN n_a; |
2862 | return sv_2pvbyte(sv, &n_a); | |
7340a771 GS |
2863 | } |
2864 | ||
2865 | char * | |
2866 | Perl_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp) | |
2867 | { | |
2868 | return sv_2pv(sv,lp); | |
2869 | } | |
2870 | ||
2871 | char * | |
2872 | Perl_sv_2pvutf8_nolen(pTHX_ register SV *sv) | |
2873 | { | |
560a288e GS |
2874 | STRLEN n_a; |
2875 | return sv_2pvutf8(sv, &n_a); | |
7340a771 GS |
2876 | } |
2877 | ||
2878 | char * | |
2879 | Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp) | |
2880 | { | |
560a288e | 2881 | sv_utf8_upgrade(sv); |
7d59b7e4 | 2882 | return SvPV(sv,*lp); |
7340a771 | 2883 | } |
1c846c1f | 2884 | |
463ee0b2 LW |
2885 | /* This function is only called on magical items */ |
2886 | bool | |
864dbfa3 | 2887 | Perl_sv_2bool(pTHX_ register SV *sv) |
463ee0b2 | 2888 | { |
8990e307 | 2889 | if (SvGMAGICAL(sv)) |
463ee0b2 LW |
2890 | mg_get(sv); |
2891 | ||
a0d0e21e LW |
2892 | if (!SvOK(sv)) |
2893 | return 0; | |
2894 | if (SvROK(sv)) { | |
a0d0e21e | 2895 | SV* tmpsv; |
1554e226 DC |
2896 | if (SvAMAGIC(sv) && (tmpsv=AMG_CALLun(sv,bool_)) && |
2897 | (SvRV(tmpsv) != SvRV(sv))) | |
9e7bc3e8 | 2898 | return SvTRUE(tmpsv); |
a0d0e21e LW |
2899 | return SvRV(sv) != 0; |
2900 | } | |
463ee0b2 | 2901 | if (SvPOKp(sv)) { |
11343788 MB |
2902 | register XPV* Xpvtmp; |
2903 | if ((Xpvtmp = (XPV*)SvANY(sv)) && | |
2904 | (*Xpvtmp->xpv_pv > '0' || | |
2905 | Xpvtmp->xpv_cur > 1 || | |
2906 | (Xpvtmp->xpv_cur && *Xpvtmp->xpv_pv != '0'))) | |
463ee0b2 LW |
2907 | return 1; |
2908 | else | |
2909 | return 0; | |
2910 | } | |
2911 | else { | |
2912 | if (SvIOKp(sv)) | |
2913 | return SvIVX(sv) != 0; | |
2914 | else { | |
2915 | if (SvNOKp(sv)) | |
2916 | return SvNVX(sv) != 0.0; | |
2917 | else | |
2918 | return FALSE; | |
2919 | } | |
2920 | } | |
79072805 LW |
2921 | } |
2922 | ||
c461cf8f JH |
2923 | /* |
2924 | =for apidoc sv_utf8_upgrade | |
2925 | ||
2926 | Convert the PV of an SV to its UTF8-encoded form. | |
2927 | ||
2928 | =cut | |
2929 | */ | |
2930 | ||
560a288e GS |
2931 | void |
2932 | Perl_sv_utf8_upgrade(pTHX_ register SV *sv) | |
2933 | { | |
511c2ff0 NIS |
2934 | char *s, *t, *e; |
2935 | int hibit = 0; | |
560a288e | 2936 | |
0c981600 | 2937 | if (!sv || !SvPOK(sv) || SvUTF8(sv)) |
560a288e GS |
2938 | return; |
2939 | ||
40826f67 JH |
2940 | /* This function could be much more efficient if we had a FLAG in SVs |
2941 | * to signal if there are any hibit chars in the PV. | |
511c2ff0 | 2942 | * Given that there isn't make loop fast as possible |
560a288e | 2943 | */ |
511c2ff0 NIS |
2944 | s = SvPVX(sv); |
2945 | e = SvEND(sv); | |
2946 | t = s; | |
2947 | while (t < e) { | |
2948 | if ((hibit = *t++ & 0x80)) | |
8a818333 | 2949 | break; |
8a818333 | 2950 | } |
560a288e | 2951 | |
40826f67 | 2952 | if (hibit) { |
8a818333 NIS |
2953 | STRLEN len; |
2954 | if (SvREADONLY(sv) && SvFAKE(sv)) { | |
8a818333 NIS |
2955 | sv_force_normal(sv); |
2956 | s = SvPVX(sv); | |
2957 | } | |
2958 | len = SvCUR(sv) + 1; /* Plus the \0 */ | |
00df9076 | 2959 | SvPVX(sv) = (char*)bytes_to_utf8((U8*)s, &len); |
841d7a39 | 2960 | SvCUR(sv) = len - 1; |
511c2ff0 NIS |
2961 | if (SvLEN(sv) != 0) |
2962 | Safefree(s); /* No longer using what was there before. */ | |
841d7a39 | 2963 | SvLEN(sv) = len; /* No longer know the real size. */ |
560a288e GS |
2964 | SvUTF8_on(sv); |
2965 | } | |
2966 | } | |
2967 | ||
c461cf8f JH |
2968 | /* |
2969 | =for apidoc sv_utf8_downgrade | |
2970 | ||
2971 | Attempt to convert the PV of an SV from UTF8-encoded to byte encoding. | |
2972 | This may not be possible if the PV contains non-byte encoding characters; | |
2973 | if this is the case, either returns false or, if C<fail_ok> is not | |
2974 | true, croaks. | |
2975 | ||
2976 | =cut | |
2977 | */ | |
2978 | ||
560a288e GS |
2979 | bool |
2980 | Perl_sv_utf8_downgrade(pTHX_ register SV* sv, bool fail_ok) | |
2981 | { | |
2982 | if (SvPOK(sv) && SvUTF8(sv)) { | |
fa301091 JH |
2983 | if (SvCUR(sv)) { |
2984 | char *c = SvPVX(sv); | |
2985 | STRLEN len = SvCUR(sv); | |
2986 | ||
2987 | if (!utf8_to_bytes((U8*)c, &len)) { | |
2988 | if (fail_ok) | |
2989 | return FALSE; | |
2990 | else { | |
2991 | if (PL_op) | |
2992 | Perl_croak(aTHX_ "Wide character in %s", | |
2993 | PL_op_desc[PL_op->op_type]); | |
2994 | else | |
2995 | Perl_croak(aTHX_ "Wide character"); | |
2996 | } | |
4b3603a4 | 2997 | } |
fa301091 | 2998 | SvCUR(sv) = len; |
67e989fb | 2999 | } |
9f9ab905 | 3000 | SvUTF8_off(sv); |
560a288e | 3001 | } |
fa301091 | 3002 | |
560a288e GS |
3003 | return TRUE; |
3004 | } | |
3005 | ||
c461cf8f JH |
3006 | /* |
3007 | =for apidoc sv_utf8_encode | |
3008 | ||
3009 | Convert the PV of an SV to UTF8-encoded, but then turn off the C<SvUTF8> | |
1c846c1f | 3010 | flag so that it looks like bytes again. Nothing calls this. |
c461cf8f JH |
3011 | |
3012 | =cut | |
3013 | */ | |
3014 | ||
560a288e GS |
3015 | void |
3016 | Perl_sv_utf8_encode(pTHX_ register SV *sv) | |
3017 | { | |
3018 | sv_utf8_upgrade(sv); | |
3019 | SvUTF8_off(sv); | |
3020 | } | |
3021 | ||
3022 | bool | |
3023 | Perl_sv_utf8_decode(pTHX_ register SV *sv) | |
3024 | { | |
3025 | if (SvPOK(sv)) { | |
3026 | char *c; | |
511c2ff0 | 3027 | char *e; |
560a288e GS |
3028 | bool has_utf = FALSE; |
3029 | if (!sv_utf8_downgrade(sv, TRUE)) | |
3030 | return FALSE; | |
3031 | ||
3032 | /* it is actually just a matter of turning the utf8 flag on, but | |
3033 | * we want to make sure everything inside is valid utf8 first. | |
3034 | */ | |
3035 | c = SvPVX(sv); | |
00df9076 | 3036 | if (!is_utf8_string((U8*)c, SvCUR(sv)+1)) |
67e989fb | 3037 | return FALSE; |
511c2ff0 NIS |
3038 | e = SvEND(sv); |
3039 | while (c < e) { | |
67e989fb JH |
3040 | if (*c++ & 0x80) { |
3041 | SvUTF8_on(sv); | |
3042 | break; | |
3043 | } | |
560a288e | 3044 | } |
560a288e GS |
3045 | } |
3046 | return TRUE; | |
3047 | } | |
3048 | ||
3049 | ||
79072805 | 3050 | /* Note: sv_setsv() should not be called with a source string that needs |
463ee0b2 | 3051 | * to be reused, since it may destroy the source string if it is marked |
79072805 LW |
3052 | * as temporary. |
3053 | */ | |
3054 | ||
954c1994 GS |
3055 | /* |
3056 | =for apidoc sv_setsv | |
3057 | ||
3058 | Copies the contents of the source SV C<ssv> into the destination SV C<dsv>. | |
3059 | The source SV may be destroyed if it is mortal. Does not handle 'set' | |
3060 | magic. See the macro forms C<SvSetSV>, C<SvSetSV_nosteal> and | |
3061 | C<sv_setsv_mg>. | |
3062 | ||
3063 | =cut | |
3064 | */ | |
3065 | ||
79072805 | 3066 | void |
864dbfa3 | 3067 | Perl_sv_setsv(pTHX_ SV *dstr, register SV *sstr) |
79072805 | 3068 | { |
8990e307 LW |
3069 | register U32 sflags; |
3070 | register int dtype; | |
3071 | register int stype; | |
463ee0b2 | 3072 | |
79072805 LW |
3073 | if (sstr == dstr) |
3074 | return; | |
2213622d | 3075 | SV_CHECK_THINKFIRST(dstr); |
79072805 | 3076 | if (!sstr) |
3280af22 | 3077 | sstr = &PL_sv_undef; |
8990e307 LW |
3078 | stype = SvTYPE(sstr); |
3079 | dtype = SvTYPE(dstr); | |
79072805 | 3080 | |
a0d0e21e | 3081 | SvAMAGIC_off(dstr); |
9e7bc3e8 | 3082 | |
463ee0b2 | 3083 | /* There's a lot of redundancy below but we're going for speed here */ |
79072805 | 3084 | |
8990e307 | 3085 | switch (stype) { |
79072805 | 3086 | case SVt_NULL: |
aece5585 | 3087 | undef_sstr: |
20408e3c GS |
3088 | if (dtype != SVt_PVGV) { |
3089 | (void)SvOK_off(dstr); | |
3090 | return; | |
3091 | } | |
3092 | break; | |
463ee0b2 | 3093 | case SVt_IV: |
aece5585 GA |
3094 | if (SvIOK(sstr)) { |
3095 | switch (dtype) { | |
3096 | case SVt_NULL: | |
8990e307 | 3097 | sv_upgrade(dstr, SVt_IV); |
aece5585 GA |
3098 | break; |
3099 | case SVt_NV: | |
8990e307 | 3100 | sv_upgrade(dstr, SVt_PVNV); |
aece5585 GA |
3101 | break; |
3102 | case SVt_RV: | |
3103 | case SVt_PV: | |
a0d0e21e | 3104 | sv_upgrade(dstr, SVt_PVIV); |
aece5585 GA |
3105 | break; |
3106 | } | |
3107 | (void)SvIOK_only(dstr); | |
3108 | SvIVX(dstr) = SvIVX(sstr); | |
25da4f38 IZ |
3109 | if (SvIsUV(sstr)) |
3110 | SvIsUV_on(dstr); | |
27c9684d AP |
3111 | if (SvTAINTED(sstr)) |
3112 | SvTAINT(dstr); | |
aece5585 | 3113 | return; |
8990e307 | 3114 | } |
aece5585 GA |
3115 | goto undef_sstr; |
3116 | ||
463ee0b2 | 3117 | case SVt_NV: |
aece5585 GA |
3118 | if (SvNOK(sstr)) { |
3119 | switch (dtype) { | |
3120 | case SVt_NULL: | |
3121 | case SVt_IV: | |
8990e307 | 3122 | sv_upgrade(dstr, SVt_NV); |
aece5585 GA |
3123 | break; |
3124 | case SVt_RV: | |
3125 | case SVt_PV: | |
3126 | case SVt_PVIV: | |
a0d0e21e | 3127 | sv_upgrade(dstr, SVt_PVNV); |
aece5585 GA |
3128 | break; |
3129 | } | |
3130 | SvNVX(dstr) = SvNVX(sstr); | |
3131 | (void)SvNOK_only(dstr); | |
27c9684d AP |
3132 | if (SvTAINTED(sstr)) |
3133 | SvTAINT(dstr); | |
aece5585 | 3134 | return; |
8990e307 | 3135 | } |
aece5585 GA |
3136 | goto undef_sstr; |
3137 | ||
ed6116ce | 3138 | case SVt_RV: |
8990e307 | 3139 | if (dtype < SVt_RV) |
ed6116ce | 3140 | sv_upgrade(dstr, SVt_RV); |
c07a80fd | 3141 | else if (dtype == SVt_PVGV && |
3142 | SvTYPE(SvRV(sstr)) == SVt_PVGV) { | |
3143 | sstr = SvRV(sstr); | |
a5f75d66 | 3144 | if (sstr == dstr) { |
1d7c1841 GS |
3145 | if (GvIMPORTED(dstr) != GVf_IMPORTED |
3146 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) | |
3147 | { | |
a5f75d66 | 3148 | GvIMPORTED_on(dstr); |
1d7c1841 | 3149 | } |
a5f75d66 AD |
3150 | GvMULTI_on(dstr); |
3151 | return; | |
3152 | } | |
c07a80fd | 3153 | goto glob_assign; |
3154 | } | |
ed6116ce | 3155 | break; |
463ee0b2 | 3156 | case SVt_PV: |
fc36a67e | 3157 | case SVt_PVFM: |
8990e307 | 3158 | if (dtype < SVt_PV) |
463ee0b2 | 3159 | sv_upgrade(dstr, SVt_PV); |
463ee0b2 LW |
3160 | break; |
3161 | case SVt_PVIV: | |
8990e307 | 3162 | if (dtype < SVt_PVIV) |
463ee0b2 | 3163 | sv_upgrade(dstr, SVt_PVIV); |
463ee0b2 LW |
3164 | break; |
3165 | case SVt_PVNV: | |
8990e307 | 3166 | if (dtype < SVt_PVNV) |
463ee0b2 | 3167 | sv_upgrade(dstr, SVt_PVNV); |
463ee0b2 | 3168 | break; |
4633a7c4 LW |
3169 | case SVt_PVAV: |
3170 | case SVt_PVHV: | |
3171 | case SVt_PVCV: | |
4633a7c4 | 3172 | case SVt_PVIO: |
533c011a | 3173 | if (PL_op) |
cea2e8a9 | 3174 | Perl_croak(aTHX_ "Bizarre copy of %s in %s", sv_reftype(sstr, 0), |
22c35a8c | 3175 | PL_op_name[PL_op->op_type]); |
4633a7c4 | 3176 | else |
cea2e8a9 | 3177 | Perl_croak(aTHX_ "Bizarre copy of %s", sv_reftype(sstr, 0)); |
4633a7c4 LW |
3178 | break; |
3179 | ||
79072805 | 3180 | case SVt_PVGV: |
8990e307 | 3181 | if (dtype <= SVt_PVGV) { |
c07a80fd | 3182 | glob_assign: |
a5f75d66 | 3183 | if (dtype != SVt_PVGV) { |
a0d0e21e LW |
3184 | char *name = GvNAME(sstr); |
3185 | STRLEN len = GvNAMELEN(sstr); | |
463ee0b2 | 3186 | sv_upgrade(dstr, SVt_PVGV); |
6662521e | 3187 | sv_magic(dstr, dstr, '*', Nullch, 0); |
85aff577 | 3188 | GvSTASH(dstr) = (HV*)SvREFCNT_inc(GvSTASH(sstr)); |
a0d0e21e LW |
3189 | GvNAME(dstr) = savepvn(name, len); |
3190 | GvNAMELEN(dstr) = len; | |
3191 | SvFAKE_on(dstr); /* can coerce to non-glob */ | |
3192 | } | |
7bac28a0 | 3193 | /* ahem, death to those who redefine active sort subs */ |
3280af22 NIS |
3194 | else if (PL_curstackinfo->si_type == PERLSI_SORT |
3195 | && GvCV(dstr) && PL_sortcop == CvSTART(GvCV(dstr))) | |
cea2e8a9 | 3196 | Perl_croak(aTHX_ "Can't redefine active sort subroutine %s", |
7bac28a0 | 3197 | GvNAME(dstr)); |
a0d0e21e | 3198 | (void)SvOK_off(dstr); |
a5f75d66 | 3199 | GvINTRO_off(dstr); /* one-shot flag */ |
1edc1566 | 3200 | gp_free((GV*)dstr); |
79072805 | 3201 | GvGP(dstr) = gp_ref(GvGP(sstr)); |
27c9684d AP |
3202 | if (SvTAINTED(sstr)) |
3203 | SvTAINT(dstr); | |
1d7c1841 GS |
3204 | if (GvIMPORTED(dstr) != GVf_IMPORTED |
3205 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) | |
3206 | { | |
a5f75d66 | 3207 | GvIMPORTED_on(dstr); |
1d7c1841 | 3208 | } |
a5f75d66 | 3209 | GvMULTI_on(dstr); |
79072805 LW |
3210 | return; |
3211 | } | |
3212 | /* FALL THROUGH */ | |
3213 | ||
3214 | default: | |
973f89ab CS |
3215 | if (SvGMAGICAL(sstr)) { |
3216 | mg_get(sstr); | |
3217 | if (SvTYPE(sstr) != stype) { | |
3218 | stype = SvTYPE(sstr); | |
3219 | if (stype == SVt_PVGV && dtype <= SVt_PVGV) | |
3220 | goto glob_assign; | |
3221 | } | |
3222 | } | |
ded42b9f | 3223 | if (stype == SVt_PVLV) |
6fc92669 | 3224 | (void)SvUPGRADE(dstr, SVt_PVNV); |
ded42b9f | 3225 | else |
6fc92669 | 3226 | (void)SvUPGRADE(dstr, stype); |
79072805 LW |
3227 | } |
3228 | ||
8990e307 LW |
3229 | sflags = SvFLAGS(sstr); |
3230 | ||
3231 | if (sflags & SVf_ROK) { | |
3232 | if (dtype >= SVt_PV) { | |
3233 | if (dtype == SVt_PVGV) { | |
3234 | SV *sref = SvREFCNT_inc(SvRV(sstr)); | |
3235 | SV *dref = 0; | |
a5f75d66 | 3236 | int intro = GvINTRO(dstr); |
a0d0e21e LW |
3237 | |
3238 | if (intro) { | |
3239 | GP *gp; | |
1d7c1841 | 3240 | gp_free((GV*)dstr); |
a5f75d66 | 3241 | GvINTRO_off(dstr); /* one-shot flag */ |
a0d0e21e | 3242 | Newz(602,gp, 1, GP); |
44a8e56a | 3243 | GvGP(dstr) = gp_ref(gp); |
a0d0e21e | 3244 | GvSV(dstr) = NEWSV(72,0); |
1d7c1841 | 3245 | GvLINE(dstr) = CopLINE(PL_curcop); |
1edc1566 | 3246 | GvEGV(dstr) = (GV*)dstr; |
a0d0e21e | 3247 | } |
a5f75d66 | 3248 | GvMULTI_on(dstr); |
8990e307 LW |
3249 | switch (SvTYPE(sref)) { |
3250 | case SVt_PVAV: | |
a0d0e21e LW |
3251 | if (intro) |
3252 | SAVESPTR(GvAV(dstr)); | |
3253 | else | |
3254 | dref = (SV*)GvAV(dstr); | |
8990e307 | 3255 | GvAV(dstr) = (AV*)sref; |
39bac7f7 | 3256 | if (!GvIMPORTED_AV(dstr) |
1d7c1841 GS |
3257 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
3258 | { | |
a5f75d66 | 3259 | GvIMPORTED_AV_on(dstr); |
1d7c1841 | 3260 | } |
8990e307 LW |
3261 | break; |
3262 | case SVt_PVHV: | |
a0d0e21e LW |
3263 | if (intro) |
3264 | SAVESPTR(GvHV(dstr)); | |
3265 | else | |
3266 | dref = (SV*)GvHV(dstr); | |
8990e307 | 3267 | GvHV(dstr) = (HV*)sref; |
39bac7f7 | 3268 | if (!GvIMPORTED_HV(dstr) |
1d7c1841 GS |
3269 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
3270 | { | |
a5f75d66 | 3271 | GvIMPORTED_HV_on(dstr); |
1d7c1841 | 3272 | } |
8990e307 LW |
3273 | break; |
3274 | case SVt_PVCV: | |
8ebc5c01 | 3275 | if (intro) { |
3276 | if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) { | |
3277 | SvREFCNT_dec(GvCV(dstr)); | |
3278 | GvCV(dstr) = Nullcv; | |
68dc0745 | 3279 | GvCVGEN(dstr) = 0; /* Switch off cacheness. */ |
3280af22 | 3280 | PL_sub_generation++; |
8ebc5c01 | 3281 | } |
a0d0e21e | 3282 | SAVESPTR(GvCV(dstr)); |
8ebc5c01 | 3283 | } |
68dc0745 | 3284 | else |
3285 | dref = (SV*)GvCV(dstr); | |
3286 | if (GvCV(dstr) != (CV*)sref) { | |
748a9306 | 3287 | CV* cv = GvCV(dstr); |
4633a7c4 | 3288 | if (cv) { |
68dc0745 | 3289 | if (!GvCVGEN((GV*)dstr) && |
3290 | (CvROOT(cv) || CvXSUB(cv))) | |
3291 | { | |
beab0874 | 3292 | SV *const_sv; |
7bac28a0 | 3293 | /* ahem, death to those who redefine |
3294 | * active sort subs */ | |
3280af22 NIS |
3295 | if (PL_curstackinfo->si_type == PERLSI_SORT && |
3296 | PL_sortcop == CvSTART(cv)) | |
1c846c1f | 3297 | Perl_croak(aTHX_ |
7bac28a0 | 3298 | "Can't redefine active sort subroutine %s", |
3299 | GvENAME((GV*)dstr)); | |
beab0874 JT |
3300 | /* Redefining a sub - warning is mandatory if |
3301 | it was a const and its value changed. */ | |
3302 | if (ckWARN(WARN_REDEFINE) | |
3303 | || (CvCONST(cv) | |
3304 | && (!CvCONST((CV*)sref) | |
3305 | || sv_cmp(cv_const_sv(cv), | |
3306 | cv_const_sv((CV*)sref))))) | |
3307 | { | |
3308 | Perl_warner(aTHX_ WARN_REDEFINE, | |
3309 | CvCONST(cv) | |
3310 | ? "Constant subroutine %s redefined" | |
47deb5e7 | 3311 | : "Subroutine %s redefined", |
beab0874 JT |
3312 | GvENAME((GV*)dstr)); |
3313 | } | |
9607fc9c | 3314 | } |
3fe9a6f1 | 3315 | cv_ckproto(cv, (GV*)dstr, |
3316 | SvPOK(sref) ? SvPVX(sref) : Nullch); | |
4633a7c4 | 3317 | } |
a5f75d66 | 3318 | GvCV(dstr) = (CV*)sref; |
7a4c00b4 | 3319 | GvCVGEN(dstr) = 0; /* Switch off cacheness. */ |
a5f75d66 | 3320 | GvASSUMECV_on(dstr); |
3280af22 | 3321 | PL_sub_generation++; |
a5f75d66 | 3322 | } |
39bac7f7 | 3323 | if (!GvIMPORTED_CV(dstr) |
1d7c1841 GS |
3324 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
3325 | { | |
a5f75d66 | 3326 | GvIMPORTED_CV_on(dstr); |
1d7c1841 | 3327 | } |
8990e307 | 3328 | break; |
91bba347 LW |
3329 | case SVt_PVIO: |
3330 | if (intro) | |
3331 | SAVESPTR(GvIOp(dstr)); | |
3332 | else | |
3333 | dref = (SV*)GvIOp(dstr); | |
3334 | GvIOp(dstr) = (IO*)sref; | |
3335 | break; | |
f4d13ee9 JH |
3336 | case SVt_PVFM: |
3337 | if (intro) | |
3338 | SAVESPTR(GvFORM(dstr)); | |
3339 | else | |
3340 | dref = (SV*)GvFORM(dstr); | |
3341 | GvFORM(dstr) = (CV*)sref; | |
3342 | break; | |
8990e307 | 3343 | default: |
a0d0e21e LW |
3344 | if (intro) |
3345 | SAVESPTR(GvSV(dstr)); | |
3346 | else | |
3347 | dref = (SV*)GvSV(dstr); | |
8990e307 | 3348 | GvSV(dstr) = sref; |
39bac7f7 | 3349 | if (!GvIMPORTED_SV(dstr) |
1d7c1841 GS |
3350 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
3351 | { | |
a5f75d66 | 3352 | GvIMPORTED_SV_on(dstr); |
1d7c1841 | 3353 | } |
8990e307 LW |
3354 | break; |
3355 | } | |
3356 | if (dref) | |
3357 | SvREFCNT_dec(dref); | |
a0d0e21e LW |
3358 | if (intro) |
3359 | SAVEFREESV(sref); | |
27c9684d AP |
3360 | if (SvTAINTED(sstr)) |
3361 | SvTAINT(dstr); | |
8990e307 LW |
3362 | return; |
3363 | } | |
a0d0e21e | 3364 | if (SvPVX(dstr)) { |
760ac839 | 3365 | (void)SvOOK_off(dstr); /* backoff */ |
50483b2c JD |
3366 | if (SvLEN(dstr)) |
3367 | Safefree(SvPVX(dstr)); | |
a0d0e21e LW |
3368 | SvLEN(dstr)=SvCUR(dstr)=0; |
3369 | } | |
8990e307 | 3370 | } |
a0d0e21e | 3371 | (void)SvOK_off(dstr); |
8990e307 | 3372 | SvRV(dstr) = SvREFCNT_inc(SvRV(sstr)); |
ed6116ce | 3373 | SvROK_on(dstr); |
8990e307 | 3374 | if (sflags & SVp_NOK) { |
ed6116ce LW |
3375 | SvNOK_on(dstr); |
3376 | SvNVX(dstr) = SvNVX(sstr); | |
3377 | } | |
8990e307 | 3378 | if (sflags & SVp_IOK) { |
a0d0e21e | 3379 | (void)SvIOK_on(dstr); |
ed6116ce | 3380 | SvIVX(dstr) = SvIVX(sstr); |
2b1c7e3e | 3381 | if (sflags & SVf_IVisUV) |
25da4f38 | 3382 | SvIsUV_on(dstr); |
ed6116ce | 3383 | } |
a0d0e21e LW |
3384 | if (SvAMAGIC(sstr)) { |
3385 | SvAMAGIC_on(dstr); | |
3386 | } | |
ed6116ce | 3387 | } |
8990e307 | 3388 | else if (sflags & SVp_POK) { |
79072805 LW |
3389 | |
3390 | /* | |
3391 | * Check to see if we can just swipe the string. If so, it's a | |
3392 | * possible small lose on short strings, but a big win on long ones. | |
463ee0b2 LW |
3393 | * It might even be a win on short strings if SvPVX(dstr) |
3394 | * has to be allocated and SvPVX(sstr) has to be freed. | |
79072805 LW |
3395 | */ |
3396 | ||
ff68c719 | 3397 | if (SvTEMP(sstr) && /* slated for free anyway? */ |
01b73108 | 3398 | SvREFCNT(sstr) == 1 && /* and no other references to it? */ |
1c846c1f | 3399 | !(sflags & SVf_OOK) && /* and not involved in OOK hack? */ |
4c8f17b9 BH |
3400 | SvLEN(sstr) && /* and really is a string */ |
3401 | !(PL_op && PL_op->op_type == OP_AASSIGN)) /* and won't be needed again, potentially */ | |
a5f75d66 | 3402 | { |
adbc6bb1 | 3403 | if (SvPVX(dstr)) { /* we know that dtype >= SVt_PV */ |
a5f75d66 AD |
3404 | if (SvOOK(dstr)) { |
3405 | SvFLAGS(dstr) &= ~SVf_OOK; | |
3406 | Safefree(SvPVX(dstr) - SvIVX(dstr)); | |
3407 | } | |
50483b2c | 3408 | else if (SvLEN(dstr)) |
a5f75d66 | 3409 | Safefree(SvPVX(dstr)); |
79072805 | 3410 | } |
a5f75d66 | 3411 | (void)SvPOK_only(dstr); |
463ee0b2 | 3412 | SvPV_set(dstr, SvPVX(sstr)); |
79072805 LW |
3413 | SvLEN_set(dstr, SvLEN(sstr)); |
3414 | SvCUR_set(dstr, SvCUR(sstr)); | |
f4e86e0f | 3415 | |
79072805 | 3416 | SvTEMP_off(dstr); |
2b1c7e3e | 3417 | (void)SvOK_off(sstr); /* NOTE: nukes most SvFLAGS on sstr */ |
79072805 LW |
3418 | SvPV_set(sstr, Nullch); |
3419 | SvLEN_set(sstr, 0); | |
a5f75d66 AD |
3420 | SvCUR_set(sstr, 0); |
3421 | SvTEMP_off(sstr); | |
79072805 LW |
3422 | } |
3423 | else { /* have to copy actual string */ | |
8990e307 LW |
3424 | STRLEN len = SvCUR(sstr); |
3425 | ||
3426 | SvGROW(dstr, len + 1); /* inlined from sv_setpvn */ | |
3427 | Move(SvPVX(sstr),SvPVX(dstr),len,char); | |
3428 | SvCUR_set(dstr, len); | |
3429 | *SvEND(dstr) = '\0'; | |
a0d0e21e | 3430 | (void)SvPOK_only(dstr); |
79072805 | 3431 | } |
3aa33fe5 | 3432 | if ((sflags & SVf_UTF8) && !IN_BYTE) |
a7cb1f99 | 3433 | SvUTF8_on(dstr); |
79072805 | 3434 | /*SUPPRESS 560*/ |
8990e307 | 3435 | if (sflags & SVp_NOK) { |
79072805 | 3436 | SvNOK_on(dstr); |
463ee0b2 | 3437 | SvNVX(dstr) = SvNVX(sstr); |
79072805 | 3438 | } |
8990e307 | 3439 | if (sflags & SVp_IOK) { |
a0d0e21e | 3440 | (void)SvIOK_on(dstr); |
463ee0b2 | 3441 | SvIVX(dstr) = SvIVX(sstr); |
2b1c7e3e | 3442 | if (sflags & SVf_IVisUV) |
25da4f38 | 3443 | SvIsUV_on(dstr); |
79072805 LW |
3444 | } |
3445 | } | |
8990e307 | 3446 | else if (sflags & SVp_NOK) { |
463ee0b2 | 3447 | SvNVX(dstr) = SvNVX(sstr); |
a0d0e21e | 3448 | (void)SvNOK_only(dstr); |
2b1c7e3e | 3449 | if (sflags & SVf_IOK) { |
a0d0e21e | 3450 | (void)SvIOK_on(dstr); |
463ee0b2 | 3451 | SvIVX(dstr) = SvIVX(sstr); |
25da4f38 | 3452 | /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */ |
2b1c7e3e | 3453 | if (sflags & SVf_IVisUV) |
25da4f38 | 3454 | SvIsUV_on(dstr); |
79072805 LW |
3455 | } |
3456 | } | |
8990e307 | 3457 | else if (sflags & SVp_IOK) { |
a0d0e21e | 3458 | (void)SvIOK_only(dstr); |
463ee0b2 | 3459 | SvIVX(dstr) = SvIVX(sstr); |
2b1c7e3e | 3460 | if (sflags & SVf_IVisUV) |
25da4f38 | 3461 | SvIsUV_on(dstr); |
79072805 LW |
3462 | } |
3463 | else { | |
20408e3c | 3464 | if (dtype == SVt_PVGV) { |
e476b1b5 GS |
3465 | if (ckWARN(WARN_MISC)) |
3466 | Perl_warner(aTHX_ WARN_MISC, "Undefined value assigned to typeglob"); | |
20408e3c GS |
3467 | } |
3468 | else | |
3469 | (void)SvOK_off(dstr); | |
a0d0e21e | 3470 | } |
27c9684d AP |
3471 | if (SvTAINTED(sstr)) |
3472 | SvTAINT(dstr); | |
79072805 LW |
3473 | } |
3474 | ||
954c1994 GS |
3475 | /* |
3476 | =for apidoc sv_setsv_mg | |
3477 | ||
3478 | Like C<sv_setsv>, but also handles 'set' magic. | |
3479 | ||
3480 | =cut | |
3481 | */ | |
3482 | ||
79072805 | 3483 | void |
864dbfa3 | 3484 | Perl_sv_setsv_mg(pTHX_ SV *dstr, register SV *sstr) |
ef50df4b GS |
3485 | { |
3486 | sv_setsv(dstr,sstr); | |
3487 | SvSETMAGIC(dstr); | |
3488 | } | |
3489 | ||
954c1994 GS |
3490 | /* |
3491 | =for apidoc sv_setpvn | |
3492 | ||
3493 | Copies a string into an SV. The C<len> parameter indicates the number of | |
3494 | bytes to be copied. Does not handle 'set' magic. See C<sv_setpvn_mg>. | |
3495 | ||
3496 | =cut | |
3497 | */ | |
3498 | ||
ef50df4b | 3499 | void |
864dbfa3 | 3500 | Perl_sv_setpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len) |
79072805 | 3501 | { |
c6f8c383 | 3502 | register char *dptr; |
793db0cb JH |
3503 | { |
3504 | /* len is STRLEN which is unsigned, need to copy to signed */ | |
3505 | IV iv = len; | |
3506 | assert(iv >= 0); | |
3507 | } | |
2213622d | 3508 | SV_CHECK_THINKFIRST(sv); |
463ee0b2 | 3509 | if (!ptr) { |
a0d0e21e | 3510 | (void)SvOK_off(sv); |
463ee0b2 LW |
3511 | return; |
3512 | } | |
6fc92669 | 3513 | (void)SvUPGRADE(sv, SVt_PV); |
c6f8c383 | 3514 | |
79072805 | 3515 | SvGROW(sv, len + 1); |
c6f8c383 GA |
3516 | dptr = SvPVX(sv); |
3517 | Move(ptr,dptr,len,char); | |
3518 | dptr[len] = '\0'; | |
79072805 | 3519 | SvCUR_set(sv, len); |
1aa99e6b | 3520 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 | 3521 | SvTAINT(sv); |
79072805 LW |
3522 | } |
3523 | ||
954c1994 GS |
3524 | /* |
3525 | =for apidoc sv_setpvn_mg | |
3526 | ||
3527 | Like C<sv_setpvn>, but also handles 'set' magic. | |
3528 | ||
3529 | =cut | |
3530 | */ | |
3531 | ||
79072805 | 3532 | void |
864dbfa3 | 3533 | Perl_sv_setpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len) |
ef50df4b GS |
3534 | { |
3535 | sv_setpvn(sv,ptr,len); | |
3536 | SvSETMAGIC(sv); | |
3537 | } | |
3538 | ||
954c1994 GS |
3539 | /* |
3540 | =for apidoc sv_setpv | |
3541 | ||
3542 | Copies a string into an SV. The string must be null-terminated. Does not | |
3543 | handle 'set' magic. See C<sv_setpv_mg>. | |
3544 | ||
3545 | =cut | |
3546 | */ | |
3547 | ||
ef50df4b | 3548 | void |
864dbfa3 | 3549 | Perl_sv_setpv(pTHX_ register SV *sv, register const char *ptr) |
79072805 LW |
3550 | { |
3551 | register STRLEN len; | |
3552 | ||
2213622d | 3553 | SV_CHECK_THINKFIRST(sv); |
463ee0b2 | 3554 | if (!ptr) { |
a0d0e21e | 3555 | (void)SvOK_off(sv); |
463ee0b2 LW |
3556 | return; |
3557 | } | |
79072805 | 3558 | len = strlen(ptr); |
6fc92669 | 3559 | (void)SvUPGRADE(sv, SVt_PV); |
c6f8c383 | 3560 | |
79072805 | 3561 | SvGROW(sv, len + 1); |
463ee0b2 | 3562 | Move(ptr,SvPVX(sv),len+1,char); |
79072805 | 3563 | SvCUR_set(sv, len); |
1aa99e6b | 3564 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 LW |
3565 | SvTAINT(sv); |
3566 | } | |
3567 | ||
954c1994 GS |
3568 | /* |
3569 | =for apidoc sv_setpv_mg | |
3570 | ||
3571 | Like C<sv_setpv>, but also handles 'set' magic. | |
3572 | ||
3573 | =cut | |
3574 | */ | |
3575 | ||
463ee0b2 | 3576 | void |
864dbfa3 | 3577 | Perl_sv_setpv_mg(pTHX_ register SV *sv, register const char *ptr) |
ef50df4b GS |
3578 | { |
3579 | sv_setpv(sv,ptr); | |
3580 | SvSETMAGIC(sv); | |
3581 | } | |
3582 | ||
954c1994 GS |
3583 | /* |
3584 | =for apidoc sv_usepvn | |
3585 | ||
3586 | Tells an SV to use C<ptr> to find its string value. Normally the string is | |
1c846c1f | 3587 | stored inside the SV but sv_usepvn allows the SV to use an outside string. |
954c1994 GS |
3588 | The C<ptr> should point to memory that was allocated by C<malloc>. The |
3589 | string length, C<len>, must be supplied. This function will realloc the | |
3590 | memory pointed to by C<ptr>, so that pointer should not be freed or used by | |
3591 | the programmer after giving it to sv_usepvn. Does not handle 'set' magic. | |
3592 | See C<sv_usepvn_mg>. | |
3593 | ||
3594 | =cut | |
3595 | */ | |
3596 | ||
ef50df4b | 3597 | void |
864dbfa3 | 3598 | Perl_sv_usepvn(pTHX_ register SV *sv, register char *ptr, register STRLEN len) |
463ee0b2 | 3599 | { |
2213622d | 3600 | SV_CHECK_THINKFIRST(sv); |
c6f8c383 | 3601 | (void)SvUPGRADE(sv, SVt_PV); |
463ee0b2 | 3602 | if (!ptr) { |
a0d0e21e | 3603 | (void)SvOK_off(sv); |
463ee0b2 LW |
3604 | return; |
3605 | } | |
a0ed51b3 | 3606 | (void)SvOOK_off(sv); |
50483b2c | 3607 | if (SvPVX(sv) && SvLEN(sv)) |
463ee0b2 LW |
3608 | Safefree(SvPVX(sv)); |
3609 | Renew(ptr, len+1, char); | |
3610 | SvPVX(sv) = ptr; | |
3611 | SvCUR_set(sv, len); | |
3612 | SvLEN_set(sv, len+1); | |
3613 | *SvEND(sv) = '\0'; | |
1aa99e6b | 3614 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 | 3615 | SvTAINT(sv); |
79072805 LW |
3616 | } |
3617 | ||
954c1994 GS |
3618 | /* |
3619 | =for apidoc sv_usepvn_mg | |
3620 | ||
3621 | Like C<sv_usepvn>, but also handles 'set' magic. | |
3622 | ||
3623 | =cut | |
3624 | */ | |
3625 | ||
ef50df4b | 3626 | void |
864dbfa3 | 3627 | Perl_sv_usepvn_mg(pTHX_ register SV *sv, register char *ptr, register STRLEN len) |
ef50df4b | 3628 | { |
51c1089b | 3629 | sv_usepvn(sv,ptr,len); |
ef50df4b GS |
3630 | SvSETMAGIC(sv); |
3631 | } | |
3632 | ||
6fc92669 | 3633 | void |
840a7b70 | 3634 | Perl_sv_force_normal_flags(pTHX_ register SV *sv, U32 flags) |
0f15f207 | 3635 | { |
2213622d | 3636 | if (SvREADONLY(sv)) { |
1c846c1f NIS |
3637 | if (SvFAKE(sv)) { |
3638 | char *pvx = SvPVX(sv); | |
3639 | STRLEN len = SvCUR(sv); | |
3640 | U32 hash = SvUVX(sv); | |
3641 | SvGROW(sv, len + 1); | |
3642 | Move(pvx,SvPVX(sv),len,char); | |
3643 | *SvEND(sv) = '\0'; | |
3644 | SvFAKE_off(sv); | |
3645 | SvREADONLY_off(sv); | |
c3654f1a | 3646 | unsharepvn(pvx,SvUTF8(sv)?-len:len,hash); |
1c846c1f NIS |
3647 | } |
3648 | else if (PL_curcop != &PL_compiling) | |
cea2e8a9 | 3649 | Perl_croak(aTHX_ PL_no_modify); |
0f15f207 | 3650 | } |
2213622d | 3651 | if (SvROK(sv)) |
840a7b70 | 3652 | sv_unref_flags(sv, flags); |
6fc92669 GS |
3653 | else if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV) |
3654 | sv_unglob(sv); | |
0f15f207 | 3655 | } |
1c846c1f | 3656 | |
840a7b70 IZ |
3657 | void |
3658 | Perl_sv_force_normal(pTHX_ register SV *sv) | |
3659 | { | |
3660 | sv_force_normal_flags(sv, 0); | |
3661 | } | |
3662 | ||
954c1994 GS |
3663 | /* |
3664 | =for apidoc sv_chop | |
3665 | ||
1c846c1f | 3666 | Efficient removal of characters from the beginning of the string buffer. |
954c1994 GS |
3667 | SvPOK(sv) must be true and the C<ptr> must be a pointer to somewhere inside |
3668 | the string buffer. The C<ptr> becomes the first character of the adjusted | |
3669 | string. | |
3670 | ||
3671 | =cut | |
3672 | */ | |
3673 | ||
79072805 | 3674 | void |
864dbfa3 | 3675 | Perl_sv_chop(pTHX_ register SV *sv, register char *ptr) /* like set but assuming ptr is in sv */ |
1c846c1f NIS |
3676 | |
3677 | ||
79072805 LW |
3678 | { |
3679 | register STRLEN delta; | |
3680 | ||
a0d0e21e | 3681 | if (!ptr || !SvPOKp(sv)) |
79072805 | 3682 | return; |
2213622d | 3683 | SV_CHECK_THINKFIRST(sv); |
79072805 LW |
3684 | if (SvTYPE(sv) < SVt_PVIV) |
3685 | sv_upgrade(sv,SVt_PVIV); | |
3686 | ||
3687 | if (!SvOOK(sv)) { | |
50483b2c JD |
3688 | if (!SvLEN(sv)) { /* make copy of shared string */ |
3689 | char *pvx = SvPVX(sv); | |
3690 | STRLEN len = SvCUR(sv); | |
3691 | SvGROW(sv, len + 1); | |
3692 | Move(pvx,SvPVX(sv),len,char); | |
3693 | *SvEND(sv) = '\0'; | |
3694 | } | |
463ee0b2 | 3695 | SvIVX(sv) = 0; |
79072805 LW |
3696 | SvFLAGS(sv) |= SVf_OOK; |
3697 | } | |
25da4f38 | 3698 | SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVp_IOK|SVp_NOK|SVf_IVisUV); |
463ee0b2 | 3699 | delta = ptr - SvPVX(sv); |
79072805 LW |
3700 | SvLEN(sv) -= delta; |
3701 | SvCUR(sv) -= delta; | |
463ee0b2 LW |
3702 | SvPVX(sv) += delta; |
3703 | SvIVX(sv) += delta; | |
79072805 LW |
3704 | } |
3705 | ||
954c1994 GS |
3706 | /* |
3707 | =for apidoc sv_catpvn | |
3708 | ||
3709 | Concatenates the string onto the end of the string which is in the SV. The | |
3710 | C<len> indicates number of bytes to copy. Handles 'get' magic, but not | |
3711 | 'set' magic. See C<sv_catpvn_mg>. | |
3712 | ||
3713 | =cut | |
3714 | */ | |
3715 | ||
79072805 | 3716 | void |
864dbfa3 | 3717 | Perl_sv_catpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len) |
79072805 | 3718 | { |
463ee0b2 | 3719 | STRLEN tlen; |
748a9306 | 3720 | char *junk; |
a0d0e21e | 3721 | |
748a9306 | 3722 | junk = SvPV_force(sv, tlen); |
463ee0b2 | 3723 | SvGROW(sv, tlen + len + 1); |
4633a7c4 LW |
3724 | if (ptr == junk) |
3725 | ptr = SvPVX(sv); | |
463ee0b2 | 3726 | Move(ptr,SvPVX(sv)+tlen,len,char); |
79072805 LW |
3727 | SvCUR(sv) += len; |
3728 | *SvEND(sv) = '\0'; | |
d41ff1b8 | 3729 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 | 3730 | SvTAINT(sv); |
79072805 LW |
3731 | } |
3732 | ||
954c1994 GS |
3733 | /* |
3734 | =for apidoc sv_catpvn_mg | |
3735 | ||
3736 | Like C<sv_catpvn>, but also handles 'set' magic. | |
3737 | ||
3738 | =cut | |
3739 | */ | |
3740 | ||
79072805 | 3741 | void |
864dbfa3 | 3742 | Perl_sv_catpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len) |
ef50df4b GS |
3743 | { |
3744 | sv_catpvn(sv,ptr,len); | |
3745 | SvSETMAGIC(sv); | |
3746 | } | |
3747 | ||
954c1994 GS |
3748 | /* |
3749 | =for apidoc sv_catsv | |
3750 | ||
13e8c8e3 JH |
3751 | Concatenates the string from SV C<ssv> onto the end of the string in |
3752 | SV C<dsv>. Modifies C<dsv> but not C<ssv>. Handles 'get' magic, but | |
3753 | not 'set' magic. See C<sv_catsv_mg>. | |
954c1994 | 3754 | |
13e8c8e3 | 3755 | =cut */ |
954c1994 | 3756 | |
ef50df4b | 3757 | void |
46199a12 | 3758 | Perl_sv_catsv(pTHX_ SV *dsv, register SV *ssv) |
79072805 | 3759 | { |
13e8c8e3 JH |
3760 | char *spv; |
3761 | STRLEN slen; | |
46199a12 | 3762 | if (!ssv) |
79072805 | 3763 | return; |
46199a12 JH |
3764 | if ((spv = SvPV(ssv, slen))) { |
3765 | bool dutf8 = DO_UTF8(dsv); | |
3766 | bool sutf8 = DO_UTF8(ssv); | |
13e8c8e3 JH |
3767 | |
3768 | if (dutf8 == sutf8) | |
46199a12 | 3769 | sv_catpvn(dsv,spv,slen); |
13e8c8e3 JH |
3770 | else { |
3771 | if (dutf8) { | |
46199a12 JH |
3772 | /* Not modifying source SV, so taking a temporary copy. */ |
3773 | SV* csv = sv_2mortal(newSVsv(ssv)); | |
13e8c8e3 JH |
3774 | char *cpv; |
3775 | STRLEN clen; | |
3776 | ||
46199a12 JH |
3777 | sv_utf8_upgrade(csv); |
3778 | cpv = SvPV(csv,clen); | |
3779 | sv_catpvn(dsv,cpv,clen); | |
13e8c8e3 JH |
3780 | } |
3781 | else { | |
46199a12 JH |
3782 | sv_utf8_upgrade(dsv); |
3783 | sv_catpvn(dsv,spv,slen); | |
3784 | SvUTF8_on(dsv); /* If dsv has no wide characters. */ | |
13e8c8e3 | 3785 | } |
e84ff256 | 3786 | } |
560a288e | 3787 | } |
79072805 LW |
3788 | } |
3789 | ||
954c1994 GS |
3790 | /* |
3791 | =for apidoc sv_catsv_mg | |
3792 | ||
3793 | Like C<sv_catsv>, but also handles 'set' magic. | |
3794 | ||
3795 | =cut | |
3796 | */ | |
3797 | ||
79072805 | 3798 | void |
46199a12 | 3799 | Perl_sv_catsv_mg(pTHX_ SV *dsv, register SV *ssv) |
ef50df4b | 3800 | { |
46199a12 JH |
3801 | sv_catsv(dsv,ssv); |
3802 | SvSETMAGIC(dsv); | |
ef50df4b GS |
3803 | } |
3804 | ||
954c1994 GS |
3805 | /* |
3806 | =for apidoc sv_catpv | |
3807 | ||
3808 | Concatenates the string onto the end of the string which is in the SV. | |
3809 | Handles 'get' magic, but not 'set' magic. See C<sv_catpv_mg>. | |
3810 | ||
3811 | =cut | |
3812 | */ | |
3813 | ||
ef50df4b | 3814 | void |
0c981600 | 3815 | Perl_sv_catpv(pTHX_ register SV *sv, register const char *ptr) |
79072805 LW |
3816 | { |
3817 | register STRLEN len; | |
463ee0b2 | 3818 | STRLEN tlen; |
748a9306 | 3819 | char *junk; |
79072805 | 3820 | |
0c981600 | 3821 | if (!ptr) |
79072805 | 3822 | return; |
748a9306 | 3823 | junk = SvPV_force(sv, tlen); |
0c981600 | 3824 | len = strlen(ptr); |
463ee0b2 | 3825 | SvGROW(sv, tlen + len + 1); |
0c981600 JH |
3826 | if (ptr == junk) |
3827 | ptr = SvPVX(sv); | |
3828 | Move(ptr,SvPVX(sv)+tlen,len+1,char); | |
79072805 | 3829 | SvCUR(sv) += len; |
d41ff1b8 | 3830 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 | 3831 | SvTAINT(sv); |
79072805 LW |
3832 | } |
3833 | ||
954c1994 GS |
3834 | /* |
3835 | =for apidoc sv_catpv_mg | |
3836 | ||
3837 | Like C<sv_catpv>, but also handles 'set' magic. | |
3838 | ||
3839 | =cut | |
3840 | */ | |
3841 | ||
ef50df4b | 3842 | void |
0c981600 | 3843 | Perl_sv_catpv_mg(pTHX_ register SV *sv, register const char *ptr) |
ef50df4b | 3844 | { |
0c981600 | 3845 | sv_catpv(sv,ptr); |
ef50df4b GS |
3846 | SvSETMAGIC(sv); |
3847 | } | |
3848 | ||
79072805 | 3849 | SV * |
864dbfa3 | 3850 | Perl_newSV(pTHX_ STRLEN len) |
79072805 LW |
3851 | { |
3852 | register SV *sv; | |
1c846c1f | 3853 | |
4561caa4 | 3854 | new_SV(sv); |
79072805 LW |
3855 | if (len) { |
3856 | sv_upgrade(sv, SVt_PV); | |
3857 | SvGROW(sv, len + 1); | |
3858 | } | |
3859 | return sv; | |
3860 | } | |
3861 | ||
1edc1566 | 3862 | /* name is assumed to contain an SV* if (name && namelen == HEf_SVKEY) */ |
3863 | ||
954c1994 GS |
3864 | /* |
3865 | =for apidoc sv_magic | |
3866 | ||
3867 | Adds magic to an SV. | |
3868 | ||
3869 | =cut | |
3870 | */ | |
3871 | ||
79072805 | 3872 | void |
864dbfa3 | 3873 | Perl_sv_magic(pTHX_ register SV *sv, SV *obj, int how, const char *name, I32 namlen) |
79072805 LW |
3874 | { |
3875 | MAGIC* mg; | |
1c846c1f | 3876 | |
0f15f207 | 3877 | if (SvREADONLY(sv)) { |
3280af22 | 3878 | if (PL_curcop != &PL_compiling && !strchr("gBf", how)) |
cea2e8a9 | 3879 | Perl_croak(aTHX_ PL_no_modify); |
0f15f207 | 3880 | } |
4633a7c4 | 3881 | if (SvMAGICAL(sv) || (how == 't' && SvTYPE(sv) >= SVt_PVMG)) { |
748a9306 LW |
3882 | if (SvMAGIC(sv) && (mg = mg_find(sv, how))) { |
3883 | if (how == 't') | |
565764a8 | 3884 | mg->mg_len |= 1; |
463ee0b2 | 3885 | return; |
748a9306 | 3886 | } |
463ee0b2 LW |
3887 | } |
3888 | else { | |
c6f8c383 | 3889 | (void)SvUPGRADE(sv, SVt_PVMG); |
463ee0b2 | 3890 | } |
79072805 LW |
3891 | Newz(702,mg, 1, MAGIC); |
3892 | mg->mg_moremagic = SvMAGIC(sv); | |
463ee0b2 | 3893 | |
79072805 | 3894 | SvMAGIC(sv) = mg; |
c277df42 | 3895 | if (!obj || obj == sv || how == '#' || how == 'r') |
8990e307 | 3896 | mg->mg_obj = obj; |
85e6fe83 | 3897 | else { |
8990e307 | 3898 | mg->mg_obj = SvREFCNT_inc(obj); |
85e6fe83 LW |
3899 | mg->mg_flags |= MGf_REFCOUNTED; |
3900 | } | |
79072805 | 3901 | mg->mg_type = how; |
565764a8 | 3902 | mg->mg_len = namlen; |
1edc1566 | 3903 | if (name) |
3904 | if (namlen >= 0) | |
3905 | mg->mg_ptr = savepvn(name, namlen); | |
c6ee37c5 | 3906 | else if (namlen == HEf_SVKEY) |
1edc1566 | 3907 | mg->mg_ptr = (char*)SvREFCNT_inc((SV*)name); |
1c846c1f | 3908 | |
79072805 LW |
3909 | switch (how) { |
3910 | case 0: | |
22c35a8c | 3911 | mg->mg_virtual = &PL_vtbl_sv; |
79072805 | 3912 | break; |
a0d0e21e | 3913 | case 'A': |
22c35a8c | 3914 | mg->mg_virtual = &PL_vtbl_amagic; |
a0d0e21e LW |
3915 | break; |
3916 | case 'a': | |
22c35a8c | 3917 | mg->mg_virtual = &PL_vtbl_amagicelem; |
a0d0e21e LW |
3918 | break; |
3919 | case 'c': | |
3920 | mg->mg_virtual = 0; | |
3921 | break; | |
79072805 | 3922 | case 'B': |
22c35a8c | 3923 | mg->mg_virtual = &PL_vtbl_bm; |
79072805 | 3924 | break; |
6cef1e77 | 3925 | case 'D': |
22c35a8c | 3926 | mg->mg_virtual = &PL_vtbl_regdata; |
6cef1e77 IZ |
3927 | break; |
3928 | case 'd': | |
22c35a8c | 3929 | mg->mg_virtual = &PL_vtbl_regdatum; |
6cef1e77 | 3930 | break; |
79072805 | 3931 | case 'E': |
22c35a8c | 3932 | mg->mg_virtual = &PL_vtbl_env; |
79072805 | 3933 | break; |
55497cff | 3934 | case 'f': |
22c35a8c | 3935 | mg->mg_virtual = &PL_vtbl_fm; |
55497cff | 3936 | break; |
79072805 | 3937 | case 'e': |
22c35a8c | 3938 | mg->mg_virtual = &PL_vtbl_envelem; |
79072805 | 3939 | break; |
93a17b20 | 3940 | case 'g': |
22c35a8c | 3941 | mg->mg_virtual = &PL_vtbl_mglob; |
93a17b20 | 3942 | break; |
463ee0b2 | 3943 | case 'I': |
22c35a8c | 3944 | mg->mg_virtual = &PL_vtbl_isa; |
463ee0b2 LW |
3945 | break; |
3946 | case 'i': | |
22c35a8c | 3947 | mg->mg_virtual = &PL_vtbl_isaelem; |
463ee0b2 | 3948 | break; |
16660edb | 3949 | case 'k': |
22c35a8c | 3950 | mg->mg_virtual = &PL_vtbl_nkeys; |
16660edb | 3951 | break; |
79072805 | 3952 | case 'L': |
a0d0e21e | 3953 | SvRMAGICAL_on(sv); |
93a17b20 LW |
3954 | mg->mg_virtual = 0; |
3955 | break; | |
3956 | case 'l': | |
22c35a8c | 3957 | mg->mg_virtual = &PL_vtbl_dbline; |
79072805 | 3958 | break; |
f93b4edd MB |
3959 | #ifdef USE_THREADS |
3960 | case 'm': | |
22c35a8c | 3961 | mg->mg_virtual = &PL_vtbl_mutex; |
f93b4edd MB |
3962 | break; |
3963 | #endif /* USE_THREADS */ | |
36477c24 | 3964 | #ifdef USE_LOCALE_COLLATE |
bbce6d69 | 3965 | case 'o': |
22c35a8c | 3966 | mg->mg_virtual = &PL_vtbl_collxfrm; |
bbce6d69 | 3967 | break; |
36477c24 | 3968 | #endif /* USE_LOCALE_COLLATE */ |
463ee0b2 | 3969 | case 'P': |
22c35a8c | 3970 | mg->mg_virtual = &PL_vtbl_pack; |
463ee0b2 LW |
3971 | break; |
3972 | case 'p': | |
a0d0e21e | 3973 | case 'q': |
22c35a8c | 3974 | mg->mg_virtual = &PL_vtbl_packelem; |
463ee0b2 | 3975 | break; |
c277df42 | 3976 | case 'r': |
22c35a8c | 3977 | mg->mg_virtual = &PL_vtbl_regexp; |
c277df42 | 3978 | break; |
79072805 | 3979 | case 'S': |
22c35a8c | 3980 | mg->mg_virtual = &PL_vtbl_sig; |
79072805 LW |
3981 | break; |
3982 | case 's': | |
22c35a8c | 3983 | mg->mg_virtual = &PL_vtbl_sigelem; |
79072805 | 3984 | break; |
463ee0b2 | 3985 | case 't': |
22c35a8c | 3986 | mg->mg_virtual = &PL_vtbl_taint; |
565764a8 | 3987 | mg->mg_len = 1; |
463ee0b2 | 3988 | break; |
79072805 | 3989 | case 'U': |
22c35a8c | 3990 | mg->mg_virtual = &PL_vtbl_uvar; |
79072805 LW |
3991 | break; |
3992 | case 'v': | |
22c35a8c | 3993 | mg->mg_virtual = &PL_vtbl_vec; |
79072805 LW |
3994 | break; |
3995 | case 'x': | |
22c35a8c | 3996 | mg->mg_virtual = &PL_vtbl_substr; |
79072805 | 3997 | break; |
5f05dabc | 3998 | case 'y': |
22c35a8c | 3999 | mg->mg_virtual = &PL_vtbl_defelem; |
5f05dabc | 4000 | break; |
79072805 | 4001 | case '*': |
22c35a8c | 4002 | mg->mg_virtual = &PL_vtbl_glob; |
79072805 LW |
4003 | break; |
4004 | case '#': | |
22c35a8c | 4005 | mg->mg_virtual = &PL_vtbl_arylen; |
79072805 | 4006 | break; |
a0d0e21e | 4007 | case '.': |
22c35a8c | 4008 | mg->mg_virtual = &PL_vtbl_pos; |
a0d0e21e | 4009 | break; |
810b8aa5 GS |
4010 | case '<': |
4011 | mg->mg_virtual = &PL_vtbl_backref; | |
4012 | break; | |
4633a7c4 LW |
4013 | case '~': /* Reserved for use by extensions not perl internals. */ |
4014 | /* Useful for attaching extension internal data to perl vars. */ | |
4015 | /* Note that multiple extensions may clash if magical scalars */ | |
4016 | /* etc holding private data from one are passed to another. */ | |
4017 | SvRMAGICAL_on(sv); | |
a0d0e21e | 4018 | break; |
79072805 | 4019 | default: |
cea2e8a9 | 4020 | Perl_croak(aTHX_ "Don't know how to handle magic of type '%c'", how); |
463ee0b2 | 4021 | } |
8990e307 LW |
4022 | mg_magical(sv); |
4023 | if (SvGMAGICAL(sv)) | |
4024 | SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK); | |
463ee0b2 LW |
4025 | } |
4026 | ||
c461cf8f JH |
4027 | /* |
4028 | =for apidoc sv_unmagic | |
4029 | ||
4030 | Removes magic from an SV. | |
4031 | ||
4032 | =cut | |
4033 | */ | |
4034 | ||
463ee0b2 | 4035 | int |
864dbfa3 | 4036 | Perl_sv_unmagic(pTHX_ SV *sv, int type) |
463ee0b2 LW |
4037 | { |
4038 | MAGIC* mg; | |
4039 | MAGIC** mgp; | |
91bba347 | 4040 | if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv)) |
463ee0b2 LW |
4041 | return 0; |
4042 | mgp = &SvMAGIC(sv); | |
4043 | for (mg = *mgp; mg; mg = *mgp) { | |
4044 | if (mg->mg_type == type) { | |
4045 | MGVTBL* vtbl = mg->mg_virtual; | |
4046 | *mgp = mg->mg_moremagic; | |
1d7c1841 | 4047 | if (vtbl && vtbl->svt_free) |
fc0dc3b3 | 4048 | CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg); |
463ee0b2 | 4049 | if (mg->mg_ptr && mg->mg_type != 'g') |
565764a8 | 4050 | if (mg->mg_len >= 0) |
1edc1566 | 4051 | Safefree(mg->mg_ptr); |
565764a8 | 4052 | else if (mg->mg_len == HEf_SVKEY) |
1edc1566 | 4053 | SvREFCNT_dec((SV*)mg->mg_ptr); |
a0d0e21e LW |
4054 | if (mg->mg_flags & MGf_REFCOUNTED) |
4055 | SvREFCNT_dec(mg->mg_obj); | |
463ee0b2 LW |
4056 | Safefree(mg); |
4057 | } | |
4058 | else | |
4059 | mgp = &mg->mg_moremagic; | |
79072805 | 4060 | } |
91bba347 | 4061 | if (!SvMAGIC(sv)) { |
463ee0b2 | 4062 | SvMAGICAL_off(sv); |
8990e307 | 4063 | SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT; |
463ee0b2 LW |
4064 | } |
4065 | ||
4066 | return 0; | |
79072805 LW |
4067 | } |
4068 | ||
c461cf8f JH |
4069 | /* |
4070 | =for apidoc sv_rvweaken | |
4071 | ||
4072 | Weaken a reference. | |
4073 | ||
4074 | =cut | |
4075 | */ | |
4076 | ||
810b8aa5 | 4077 | SV * |
864dbfa3 | 4078 | Perl_sv_rvweaken(pTHX_ SV *sv) |
810b8aa5 GS |
4079 | { |
4080 | SV *tsv; | |
4081 | if (!SvOK(sv)) /* let undefs pass */ | |
4082 | return sv; | |
4083 | if (!SvROK(sv)) | |
cea2e8a9 | 4084 | Perl_croak(aTHX_ "Can't weaken a nonreference"); |
810b8aa5 | 4085 | else if (SvWEAKREF(sv)) { |
810b8aa5 | 4086 | if (ckWARN(WARN_MISC)) |
cea2e8a9 | 4087 | Perl_warner(aTHX_ WARN_MISC, "Reference is already weak"); |
810b8aa5 GS |
4088 | return sv; |
4089 | } | |
4090 | tsv = SvRV(sv); | |
4091 | sv_add_backref(tsv, sv); | |
4092 | SvWEAKREF_on(sv); | |
1c846c1f | 4093 | SvREFCNT_dec(tsv); |
810b8aa5 GS |
4094 | return sv; |
4095 | } | |
4096 | ||
4097 | STATIC void | |
cea2e8a9 | 4098 | S_sv_add_backref(pTHX_ SV *tsv, SV *sv) |
810b8aa5 GS |
4099 | { |
4100 | AV *av; | |
4101 | MAGIC *mg; | |
4102 | if (SvMAGICAL(tsv) && (mg = mg_find(tsv, '<'))) | |
4103 | av = (AV*)mg->mg_obj; | |
4104 | else { | |
4105 | av = newAV(); | |
4106 | sv_magic(tsv, (SV*)av, '<', NULL, 0); | |
4107 | SvREFCNT_dec(av); /* for sv_magic */ | |
4108 | } | |
4109 | av_push(av,sv); | |
4110 | } | |
4111 | ||
1c846c1f | 4112 | STATIC void |
cea2e8a9 | 4113 | S_sv_del_backref(pTHX_ SV *sv) |
810b8aa5 GS |
4114 | { |
4115 | AV *av; | |
4116 | SV **svp; | |
4117 | I32 i; | |
4118 | SV *tsv = SvRV(sv); | |
4119 | MAGIC *mg; | |
4120 | if (!SvMAGICAL(tsv) || !(mg = mg_find(tsv, '<'))) | |
cea2e8a9 | 4121 | Perl_croak(aTHX_ "panic: del_backref"); |
810b8aa5 GS |
4122 | av = (AV *)mg->mg_obj; |
4123 | svp = AvARRAY(av); | |
4124 | i = AvFILLp(av); | |
4125 | while (i >= 0) { | |
4126 | if (svp[i] == sv) { | |
4127 | svp[i] = &PL_sv_undef; /* XXX */ | |
4128 | } | |
4129 | i--; | |
4130 | } | |
4131 | } | |
4132 | ||
954c1994 GS |
4133 | /* |
4134 | =for apidoc sv_insert | |
4135 | ||
4136 | Inserts a string at the specified offset/length within the SV. Similar to | |
4137 | the Perl substr() function. | |
4138 | ||
4139 | =cut | |
4140 | */ | |
4141 | ||
79072805 | 4142 | void |
864dbfa3 | 4143 | Perl_sv_insert(pTHX_ SV *bigstr, STRLEN offset, STRLEN len, char *little, STRLEN littlelen) |
79072805 LW |
4144 | { |
4145 | register char *big; | |
4146 | register char *mid; | |
4147 | register char *midend; | |
4148 | register char *bigend; | |
4149 | register I32 i; | |
6ff81951 | 4150 | STRLEN curlen; |
1c846c1f | 4151 | |
79072805 | 4152 | |
8990e307 | 4153 | if (!bigstr) |
cea2e8a9 | 4154 | Perl_croak(aTHX_ "Can't modify non-existent substring"); |
6ff81951 | 4155 | SvPV_force(bigstr, curlen); |
60fa28ff | 4156 | (void)SvPOK_only_UTF8(bigstr); |
6ff81951 GS |
4157 | if (offset + len > curlen) { |
4158 | SvGROW(bigstr, offset+len+1); | |
4159 | Zero(SvPVX(bigstr)+curlen, offset+len-curlen, char); | |
4160 | SvCUR_set(bigstr, offset+len); | |
4161 | } | |
79072805 | 4162 | |
69b47968 | 4163 | SvTAINT(bigstr); |
79072805 LW |
4164 | i = littlelen - len; |
4165 | if (i > 0) { /* string might grow */ | |
a0d0e21e | 4166 | big = SvGROW(bigstr, SvCUR(bigstr) + i + 1); |
79072805 LW |
4167 | mid = big + offset + len; |
4168 | midend = bigend = big + SvCUR(bigstr); | |
4169 | bigend += i; | |
4170 | *bigend = '\0'; | |
4171 | while (midend > mid) /* shove everything down */ | |
4172 | *--bigend = *--midend; | |
4173 | Move(little,big+offset,littlelen,char); | |
4174 | SvCUR(bigstr) += i; | |
4175 | SvSETMAGIC(bigstr); | |
4176 | return; | |
4177 | } | |
4178 | else if (i == 0) { | |
463ee0b2 | 4179 | Move(little,SvPVX(bigstr)+offset,len,char); |
79072805 LW |
4180 | SvSETMAGIC(bigstr); |
4181 | return; | |
4182 | } | |
4183 | ||
463ee0b2 | 4184 | big = SvPVX(bigstr); |
79072805 LW |
4185 | mid = big + offset; |
4186 | midend = mid + len; | |
4187 | bigend = big + SvCUR(bigstr); | |
4188 | ||
4189 | if (midend > bigend) | |
cea2e8a9 | 4190 | Perl_croak(aTHX_ "panic: sv_insert"); |
79072805 LW |
4191 | |
4192 | if (mid - big > bigend - midend) { /* faster to shorten from end */ | |
4193 | if (littlelen) { | |
4194 | Move(little, mid, littlelen,char); | |
4195 | mid += littlelen; | |
4196 | } | |
4197 | i = bigend - midend; | |
4198 | if (i > 0) { | |
4199 | Move(midend, mid, i,char); | |
4200 | mid += i; | |
4201 | } | |
4202 | *mid = '\0'; | |
4203 | SvCUR_set(bigstr, mid - big); | |
4204 | } | |
4205 | /*SUPPRESS 560*/ | |
155aba94 | 4206 | else if ((i = mid - big)) { /* faster from front */ |
79072805 LW |
4207 | midend -= littlelen; |
4208 | mid = midend; | |
4209 | sv_chop(bigstr,midend-i); | |
4210 | big += i; | |
4211 | while (i--) | |
4212 | *--midend = *--big; | |
4213 | if (littlelen) | |
4214 | Move(little, mid, littlelen,char); | |
4215 | } | |
4216 | else if (littlelen) { | |
4217 | midend -= littlelen; | |
4218 | sv_chop(bigstr,midend); | |
4219 | Move(little,midend,littlelen,char); | |
4220 | } | |
4221 | else { | |
4222 | sv_chop(bigstr,midend); | |
4223 | } | |
4224 | SvSETMAGIC(bigstr); | |
4225 | } | |
4226 | ||
c461cf8f JH |
4227 | /* |
4228 | =for apidoc sv_replace | |
4229 | ||
4230 | Make the first argument a copy of the second, then delete the original. | |
4231 | ||
4232 | =cut | |
4233 | */ | |
79072805 LW |
4234 | |
4235 | void | |
864dbfa3 | 4236 | Perl_sv_replace(pTHX_ register SV *sv, register SV *nsv) |
79072805 LW |
4237 | { |
4238 | U32 refcnt = SvREFCNT(sv); | |
2213622d | 4239 | SV_CHECK_THINKFIRST(sv); |
0453d815 PM |
4240 | if (SvREFCNT(nsv) != 1 && ckWARN_d(WARN_INTERNAL)) |
4241 | Perl_warner(aTHX_ WARN_INTERNAL, "Reference miscount in sv_replace()"); | |
93a17b20 | 4242 | if (SvMAGICAL(sv)) { |
a0d0e21e LW |
4243 | if (SvMAGICAL(nsv)) |
4244 | mg_free(nsv); | |
4245 | else | |
4246 | sv_upgrade(nsv, SVt_PVMG); | |
93a17b20 | 4247 | SvMAGIC(nsv) = SvMAGIC(sv); |
a0d0e21e | 4248 | SvFLAGS(nsv) |= SvMAGICAL(sv); |
93a17b20 LW |
4249 | SvMAGICAL_off(sv); |
4250 | SvMAGIC(sv) = 0; | |
4251 | } | |
79072805 LW |
4252 | SvREFCNT(sv) = 0; |
4253 | sv_clear(sv); | |
477f5d66 | 4254 | assert(!SvREFCNT(sv)); |
79072805 LW |
4255 | StructCopy(nsv,sv,SV); |
4256 | SvREFCNT(sv) = refcnt; | |
1edc1566 | 4257 | SvFLAGS(nsv) |= SVTYPEMASK; /* Mark as freed */ |
463ee0b2 | 4258 | del_SV(nsv); |
79072805 LW |
4259 | } |
4260 | ||
c461cf8f JH |
4261 | /* |
4262 | =for apidoc sv_clear | |
4263 | ||
4264 | Clear an SV, making it empty. Does not free the memory used by the SV | |
4265 | itself. | |
4266 | ||
4267 | =cut | |
4268 | */ | |
4269 | ||
79072805 | 4270 | void |
864dbfa3 | 4271 | Perl_sv_clear(pTHX_ register SV *sv) |
79072805 | 4272 | { |
ec12f114 | 4273 | HV* stash; |
79072805 LW |
4274 | assert(sv); |
4275 | assert(SvREFCNT(sv) == 0); | |
4276 | ||
ed6116ce | 4277 | if (SvOBJECT(sv)) { |
3280af22 | 4278 | if (PL_defstash) { /* Still have a symbol table? */ |
4e35701f | 4279 | djSP; |
32251b26 | 4280 | CV* destructor; |
837485b6 | 4281 | SV tmpref; |
a0d0e21e | 4282 | |
837485b6 GS |
4283 | Zero(&tmpref, 1, SV); |
4284 | sv_upgrade(&tmpref, SVt_RV); | |
4285 | SvROK_on(&tmpref); | |
4286 | SvREADONLY_on(&tmpref); /* DESTROY() could be naughty */ | |
4287 | SvREFCNT(&tmpref) = 1; | |
8ebc5c01 | 4288 | |
32251b26 | 4289 | do { |
4e8e7886 | 4290 | stash = SvSTASH(sv); |
32251b26 | 4291 | destructor = StashHANDLER(stash,DESTROY); |
4e8e7886 GS |
4292 | if (destructor) { |
4293 | ENTER; | |
e788e7d3 | 4294 | PUSHSTACKi(PERLSI_DESTROY); |
837485b6 | 4295 | SvRV(&tmpref) = SvREFCNT_inc(sv); |
4e8e7886 GS |
4296 | EXTEND(SP, 2); |
4297 | PUSHMARK(SP); | |
837485b6 | 4298 | PUSHs(&tmpref); |
4e8e7886 | 4299 | PUTBACK; |
32251b26 | 4300 | call_sv((SV*)destructor, G_DISCARD|G_EVAL|G_KEEPERR); |
4e8e7886 | 4301 | SvREFCNT(sv)--; |
d3acc0f7 | 4302 | POPSTACK; |
3095d977 | 4303 | SPAGAIN; |
4e8e7886 GS |
4304 | LEAVE; |
4305 | } | |
4306 | } while (SvOBJECT(sv) && SvSTASH(sv) != stash); | |
8ebc5c01 | 4307 | |
837485b6 | 4308 | del_XRV(SvANY(&tmpref)); |
6f44e0a4 JP |
4309 | |
4310 | if (SvREFCNT(sv)) { | |
4311 | if (PL_in_clean_objs) | |
cea2e8a9 | 4312 | Perl_croak(aTHX_ "DESTROY created new reference to dead object '%s'", |
6f44e0a4 JP |
4313 | HvNAME(stash)); |
4314 | /* DESTROY gave object new lease on life */ | |
4315 | return; | |
4316 | } | |
a0d0e21e | 4317 | } |
4e8e7886 | 4318 | |
a0d0e21e | 4319 | if (SvOBJECT(sv)) { |
4e8e7886 | 4320 | SvREFCNT_dec(SvSTASH(sv)); /* possibly of changed persuasion */ |
a0d0e21e LW |
4321 | SvOBJECT_off(sv); /* Curse the object. */ |
4322 | if (SvTYPE(sv) != SVt_PVIO) | |
3280af22 | 4323 | --PL_sv_objcount; /* XXX Might want something more general */ |
a0d0e21e | 4324 | } |
463ee0b2 | 4325 | } |
c07a80fd | 4326 | if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) |
a0d0e21e | 4327 | mg_free(sv); |
ec12f114 | 4328 | stash = NULL; |
79072805 | 4329 | switch (SvTYPE(sv)) { |
8990e307 | 4330 | case SVt_PVIO: |
df0bd2f4 GS |
4331 | if (IoIFP(sv) && |
4332 | IoIFP(sv) != PerlIO_stdin() && | |
5f05dabc | 4333 | IoIFP(sv) != PerlIO_stdout() && |
4334 | IoIFP(sv) != PerlIO_stderr()) | |
93578b34 | 4335 | { |
f2b5be74 | 4336 | io_close((IO*)sv, FALSE); |
93578b34 | 4337 | } |
1d7c1841 | 4338 | if (IoDIRP(sv) && !(IoFLAGS(sv) & IOf_FAKE_DIRP)) |
1236053a | 4339 | PerlDir_close(IoDIRP(sv)); |
1d7c1841 | 4340 | IoDIRP(sv) = (DIR*)NULL; |
8990e307 LW |
4341 | Safefree(IoTOP_NAME(sv)); |
4342 | Safefree(IoFMT_NAME(sv)); | |
4343 | Safefree(IoBOTTOM_NAME(sv)); | |
a0d0e21e | 4344 | /* FALL THROUGH */ |
79072805 | 4345 | case SVt_PVBM: |
a0d0e21e | 4346 | goto freescalar; |
79072805 | 4347 | case SVt_PVCV: |
748a9306 | 4348 | case SVt_PVFM: |
85e6fe83 | 4349 | cv_undef((CV*)sv); |
a0d0e21e | 4350 | goto freescalar; |
79072805 | 4351 | case SVt_PVHV: |
85e6fe83 | 4352 | hv_undef((HV*)sv); |
a0d0e21e | 4353 | break; |
79072805 | 4354 | case SVt_PVAV: |
85e6fe83 | 4355 | av_undef((AV*)sv); |
a0d0e21e | 4356 | break; |
02270b4e GS |
4357 | case SVt_PVLV: |
4358 | SvREFCNT_dec(LvTARG(sv)); | |
4359 | goto freescalar; | |
a0d0e21e | 4360 | case SVt_PVGV: |
1edc1566 | 4361 | gp_free((GV*)sv); |
a0d0e21e | 4362 | Safefree(GvNAME(sv)); |
ec12f114 JPC |
4363 | /* cannot decrease stash refcount yet, as we might recursively delete |
4364 | ourselves when the refcnt drops to zero. Delay SvREFCNT_dec | |
4365 | of stash until current sv is completely gone. | |
4366 | -- JohnPC, 27 Mar 1998 */ | |
4367 | stash = GvSTASH(sv); | |
a0d0e21e | 4368 | /* FALL THROUGH */ |
79072805 | 4369 | case SVt_PVMG: |
79072805 LW |
4370 | case SVt_PVNV: |
4371 | case SVt_PVIV: | |
a0d0e21e LW |
4372 | freescalar: |
4373 | (void)SvOOK_off(sv); | |
79072805 LW |
4374 | /* FALL THROUGH */ |
4375 | case SVt_PV: | |
a0d0e21e | 4376 | case SVt_RV: |
810b8aa5 GS |
4377 | if (SvROK(sv)) { |
4378 | if (SvWEAKREF(sv)) | |
4379 | sv_del_backref(sv); | |
4380 | else | |
4381 | SvREFCNT_dec(SvRV(sv)); | |
4382 | } | |
1edc1566 | 4383 | else if (SvPVX(sv) && SvLEN(sv)) |
463ee0b2 | 4384 | Safefree(SvPVX(sv)); |
1c846c1f | 4385 | else if (SvPVX(sv) && SvREADONLY(sv) && SvFAKE(sv)) { |
c3654f1a | 4386 | unsharepvn(SvPVX(sv),SvUTF8(sv)?-SvCUR(sv):SvCUR(sv),SvUVX(sv)); |
1c846c1f NIS |
4387 | SvFAKE_off(sv); |
4388 | } | |
79072805 | 4389 | break; |
a0d0e21e | 4390 | /* |
79072805 | 4391 | case SVt_NV: |
79072805 | 4392 | case SVt_IV: |
79072805 LW |
4393 | case SVt_NULL: |
4394 | break; | |
a0d0e21e | 4395 | */ |
79072805 LW |
4396 | } |
4397 | ||
4398 | switch (SvTYPE(sv)) { | |
4399 | case SVt_NULL: | |
4400 | break; | |
79072805 LW |
4401 | case SVt_IV: |
4402 | del_XIV(SvANY(sv)); | |
4403 | break; | |
4404 | case SVt_NV: | |
4405 | del_XNV(SvANY(sv)); | |
4406 | break; | |
ed6116ce LW |
4407 | case SVt_RV: |
4408 | del_XRV(SvANY(sv)); | |
4409 | break; | |
79072805 LW |
4410 | case SVt_PV: |
4411 | del_XPV(SvANY(sv)); | |
4412 | break; | |
4413 | case SVt_PVIV: | |
4414 | del_XPVIV(SvANY(sv)); | |
4415 | break; | |
4416 | case SVt_PVNV: | |
4417 | del_XPVNV(SvANY(sv)); | |
4418 | break; | |
4419 | case SVt_PVMG: | |
4420 | del_XPVMG(SvANY(sv)); | |
4421 | break; | |
4422 | case SVt_PVLV: | |
4423 | del_XPVLV(SvANY(sv)); | |
4424 | break; | |
4425 | case SVt_PVAV: | |
4426 | del_XPVAV(SvANY(sv)); | |
4427 | break; | |
4428 | case SVt_PVHV: | |
4429 | del_XPVHV(SvANY(sv)); | |
4430 | break; | |
4431 | case SVt_PVCV: | |
4432 | del_XPVCV(SvANY(sv)); | |
4433 | break; | |
4434 | case SVt_PVGV: | |
4435 | del_XPVGV(SvANY(sv)); | |
ec12f114 JPC |
4436 | /* code duplication for increased performance. */ |
4437 | SvFLAGS(sv) &= SVf_BREAK; | |
4438 | SvFLAGS(sv) |= SVTYPEMASK; | |
4439 | /* decrease refcount of the stash that owns this GV, if any */ | |
4440 | if (stash) | |
4441 | SvREFCNT_dec(stash); | |
4442 | return; /* not break, SvFLAGS reset already happened */ | |
79072805 LW |
4443 | case SVt_PVBM: |
4444 | del_XPVBM(SvANY(sv)); | |
4445 | break; | |
4446 | case SVt_PVFM: | |
4447 | del_XPVFM(SvANY(sv)); | |
4448 | break; | |
8990e307 LW |
4449 | case SVt_PVIO: |
4450 | del_XPVIO(SvANY(sv)); | |
4451 | break; | |
79072805 | 4452 | } |
a0d0e21e | 4453 | SvFLAGS(sv) &= SVf_BREAK; |
8990e307 | 4454 | SvFLAGS(sv) |= SVTYPEMASK; |
79072805 LW |
4455 | } |
4456 | ||
4457 | SV * | |
864dbfa3 | 4458 | Perl_sv_newref(pTHX_ SV *sv) |
79072805 | 4459 | { |
463ee0b2 | 4460 | if (sv) |
dce16143 | 4461 | ATOMIC_INC(SvREFCNT(sv)); |
79072805 LW |
4462 | return sv; |
4463 | } | |
4464 | ||
c461cf8f JH |
4465 | /* |
4466 | =for apidoc sv_free | |
4467 | ||
4468 | Free the memory used by an SV. | |
4469 | ||
4470 | =cut | |
4471 | */ | |
4472 | ||
79072805 | 4473 | void |
864dbfa3 | 4474 | Perl_sv_free(pTHX_ SV *sv) |
79072805 | 4475 | { |
dce16143 MB |
4476 | int refcount_is_zero; |
4477 | ||
79072805 LW |
4478 | if (!sv) |
4479 | return; | |
a0d0e21e LW |
4480 | if (SvREFCNT(sv) == 0) { |
4481 | if (SvFLAGS(sv) & SVf_BREAK) | |
4482 | return; | |
3280af22 | 4483 | if (PL_in_clean_all) /* All is fair */ |
1edc1566 | 4484 | return; |
d689ffdd JP |
4485 | if (SvREADONLY(sv) && SvIMMORTAL(sv)) { |
4486 | /* make sure SvREFCNT(sv)==0 happens very seldom */ | |
4487 | SvREFCNT(sv) = (~(U32)0)/2; | |
4488 | return; | |
4489 | } | |
0453d815 PM |
4490 | if (ckWARN_d(WARN_INTERNAL)) |
4491 | Perl_warner(aTHX_ WARN_INTERNAL, "Attempt to free unreferenced scalar"); | |
79072805 LW |
4492 | return; |
4493 | } | |
dce16143 MB |
4494 | ATOMIC_DEC_AND_TEST(refcount_is_zero, SvREFCNT(sv)); |
4495 | if (!refcount_is_zero) | |
8990e307 | 4496 | return; |
463ee0b2 LW |
4497 | #ifdef DEBUGGING |
4498 | if (SvTEMP(sv)) { | |
0453d815 | 4499 | if (ckWARN_d(WARN_DEBUGGING)) |
f248d071 | 4500 | Perl_warner(aTHX_ WARN_DEBUGGING, |
1d7c1841 GS |
4501 | "Attempt to free temp prematurely: SV 0x%"UVxf, |
4502 | PTR2UV(sv)); | |
79072805 | 4503 | return; |
79072805 | 4504 | } |
463ee0b2 | 4505 | #endif |
d689ffdd JP |
4506 | if (SvREADONLY(sv) && SvIMMORTAL(sv)) { |
4507 | /* make sure SvREFCNT(sv)==0 happens very seldom */ | |
4508 | SvREFCNT(sv) = (~(U32)0)/2; | |
4509 | return; | |
4510 | } | |
79072805 | 4511 | sv_clear(sv); |
477f5d66 CS |
4512 | if (! SvREFCNT(sv)) |
4513 | del_SV(sv); | |
79072805 LW |
4514 | } |
4515 | ||
954c1994 GS |
4516 | /* |
4517 | =for apidoc sv_len | |
4518 | ||
4519 | Returns the length of the string in the SV. See also C<SvCUR>. | |
4520 | ||
4521 | =cut | |
4522 | */ | |
4523 | ||
79072805 | 4524 | STRLEN |
864dbfa3 | 4525 | Perl_sv_len(pTHX_ register SV *sv) |
79072805 | 4526 | { |
748a9306 | 4527 | char *junk; |
463ee0b2 | 4528 | STRLEN len; |
79072805 LW |
4529 | |
4530 | if (!sv) | |
4531 | return 0; | |
4532 | ||
8990e307 | 4533 | if (SvGMAGICAL(sv)) |
565764a8 | 4534 | len = mg_length(sv); |
8990e307 | 4535 | else |
748a9306 | 4536 | junk = SvPV(sv, len); |
463ee0b2 | 4537 | return len; |
79072805 LW |
4538 | } |
4539 | ||
c461cf8f JH |
4540 | /* |
4541 | =for apidoc sv_len_utf8 | |
4542 | ||
4543 | Returns the number of characters in the string in an SV, counting wide | |
4544 | UTF8 bytes as a single character. | |
4545 | ||
4546 | =cut | |
4547 | */ | |
4548 | ||
a0ed51b3 | 4549 | STRLEN |
864dbfa3 | 4550 | Perl_sv_len_utf8(pTHX_ register SV *sv) |
a0ed51b3 | 4551 | { |
a0ed51b3 LW |
4552 | if (!sv) |
4553 | return 0; | |
4554 | ||
a0ed51b3 | 4555 | if (SvGMAGICAL(sv)) |
b76347f2 | 4556 | return mg_length(sv); |
a0ed51b3 | 4557 | else |
b76347f2 JH |
4558 | { |
4559 | STRLEN len; | |
4560 | U8 *s = (U8*)SvPV(sv, len); | |
4561 | ||
d6efbbad | 4562 | return Perl_utf8_length(aTHX_ s, s + len); |
a0ed51b3 | 4563 | } |
a0ed51b3 LW |
4564 | } |
4565 | ||
4566 | void | |
864dbfa3 | 4567 | Perl_sv_pos_u2b(pTHX_ register SV *sv, I32* offsetp, I32* lenp) |
a0ed51b3 | 4568 | { |
dfe13c55 GS |
4569 | U8 *start; |
4570 | U8 *s; | |
4571 | U8 *send; | |
a0ed51b3 LW |
4572 | I32 uoffset = *offsetp; |
4573 | STRLEN len; | |
4574 | ||
4575 | if (!sv) | |
4576 | return; | |
4577 | ||
dfe13c55 | 4578 | start = s = (U8*)SvPV(sv, len); |
a0ed51b3 LW |
4579 | send = s + len; |
4580 | while (s < send && uoffset--) | |
4581 | s += UTF8SKIP(s); | |
bb40f870 GA |
4582 | if (s >= send) |
4583 | s = send; | |
a0ed51b3 LW |
4584 | *offsetp = s - start; |
4585 | if (lenp) { | |
4586 | I32 ulen = *lenp; | |
4587 | start = s; | |
4588 | while (s < send && ulen--) | |
4589 | s += UTF8SKIP(s); | |
bb40f870 GA |
4590 | if (s >= send) |
4591 | s = send; | |
a0ed51b3 LW |
4592 | *lenp = s - start; |
4593 | } | |
4594 | return; | |
4595 | } | |
4596 | ||
4597 | void | |
864dbfa3 | 4598 | Perl_sv_pos_b2u(pTHX_ register SV *sv, I32* offsetp) |
a0ed51b3 | 4599 | { |
dfe13c55 GS |
4600 | U8 *s; |
4601 | U8 *send; | |
a0ed51b3 LW |
4602 | STRLEN len; |
4603 | ||
4604 | if (!sv) | |
4605 | return; | |
4606 | ||
dfe13c55 | 4607 | s = (U8*)SvPV(sv, len); |
a0ed51b3 | 4608 | if (len < *offsetp) |
cea2e8a9 | 4609 | Perl_croak(aTHX_ "panic: bad byte offset"); |
a0ed51b3 LW |
4610 | send = s + *offsetp; |
4611 | len = 0; | |
4612 | while (s < send) { | |
4613 | s += UTF8SKIP(s); | |
4614 | ++len; | |
4615 | } | |
4616 | if (s != send) { | |
1c846c1f | 4617 | if (ckWARN_d(WARN_UTF8)) |
0453d815 | 4618 | Perl_warner(aTHX_ WARN_UTF8, "Malformed UTF-8 character"); |
a0ed51b3 LW |
4619 | --len; |
4620 | } | |
4621 | *offsetp = len; | |
4622 | return; | |
4623 | } | |
4624 | ||
954c1994 GS |
4625 | /* |
4626 | =for apidoc sv_eq | |
4627 | ||
4628 | Returns a boolean indicating whether the strings in the two SVs are | |
4629 | identical. | |
4630 | ||
4631 | =cut | |
4632 | */ | |
4633 | ||
79072805 | 4634 | I32 |
e01b9e88 | 4635 | Perl_sv_eq(pTHX_ register SV *sv1, register SV *sv2) |
79072805 LW |
4636 | { |
4637 | char *pv1; | |
463ee0b2 | 4638 | STRLEN cur1; |
79072805 | 4639 | char *pv2; |
463ee0b2 | 4640 | STRLEN cur2; |
e01b9e88 SC |
4641 | I32 eq = 0; |
4642 | bool pv1tmp = FALSE; | |
4643 | bool pv2tmp = FALSE; | |
79072805 | 4644 | |
e01b9e88 | 4645 | if (!sv1) { |
79072805 LW |
4646 | pv1 = ""; |
4647 | cur1 = 0; | |
4648 | } | |
463ee0b2 | 4649 | else |
e01b9e88 | 4650 | pv1 = SvPV(sv1, cur1); |
79072805 | 4651 | |
e01b9e88 SC |
4652 | if (!sv2){ |
4653 | pv2 = ""; | |
4654 | cur2 = 0; | |
92d29cee | 4655 | } |
e01b9e88 SC |
4656 | else |
4657 | pv2 = SvPV(sv2, cur2); | |
79072805 | 4658 | |
e01b9e88 | 4659 | /* do not utf8ize the comparands as a side-effect */ |
7bbb0251 | 4660 | if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTE) { |
1aa99e6b IH |
4661 | if (PL_hints & HINT_UTF8_DISTINCT) |
4662 | return FALSE; | |
4663 | ||
e01b9e88 | 4664 | if (SvUTF8(sv1)) { |
1aa99e6b | 4665 | (void)utf8_to_bytes((U8*)(pv1 = savepvn(pv1, cur1)), &cur1); |
a9df0fa1 CB |
4666 | { |
4667 | IV scur1 = cur1; | |
4668 | if (scur1 < 0) { | |
4669 | Safefree(pv1); | |
4670 | return 0; | |
4671 | } | |
1aa99e6b IH |
4672 | } |
4673 | pv1tmp = TRUE; | |
e01b9e88 SC |
4674 | } |
4675 | else { | |
1aa99e6b | 4676 | (void)utf8_to_bytes((U8*)(pv2 = savepvn(pv2, cur2)), &cur2); |
a9df0fa1 CB |
4677 | { |
4678 | IV scur2 = cur2; | |
4679 | if (scur2 < 0) { | |
4680 | Safefree(pv2); | |
4681 | return 0; | |
4682 | } | |
1aa99e6b IH |
4683 | } |
4684 | pv2tmp = TRUE; | |
e01b9e88 SC |
4685 | } |
4686 | } | |
79072805 | 4687 | |
e01b9e88 SC |
4688 | if (cur1 == cur2) |
4689 | eq = memEQ(pv1, pv2, cur1); | |
4690 | ||
4691 | if (pv1tmp) | |
4692 | Safefree(pv1); | |
4693 | if (pv2tmp) | |
4694 | Safefree(pv2); | |
4695 | ||
4696 | return eq; | |
79072805 LW |
4697 | } |
4698 | ||
954c1994 GS |
4699 | /* |
4700 | =for apidoc sv_cmp | |
4701 | ||
4702 | Compares the strings in two SVs. Returns -1, 0, or 1 indicating whether the | |
4703 | string in C<sv1> is less than, equal to, or greater than the string in | |
4704 | C<sv2>. | |
4705 | ||
4706 | =cut | |
4707 | */ | |
4708 | ||
79072805 | 4709 | I32 |
e01b9e88 | 4710 | Perl_sv_cmp(pTHX_ register SV *sv1, register SV *sv2) |
79072805 | 4711 | { |
560a288e GS |
4712 | STRLEN cur1, cur2; |
4713 | char *pv1, *pv2; | |
1c846c1f | 4714 | I32 cmp; |
e01b9e88 SC |
4715 | bool pv1tmp = FALSE; |
4716 | bool pv2tmp = FALSE; | |
560a288e | 4717 | |
e01b9e88 SC |
4718 | if (!sv1) { |
4719 | pv1 = ""; | |
560a288e GS |
4720 | cur1 = 0; |
4721 | } | |
e01b9e88 SC |
4722 | else |
4723 | pv1 = SvPV(sv1, cur1); | |
560a288e | 4724 | |
e01b9e88 SC |
4725 | if (!sv2){ |
4726 | pv2 = ""; | |
560a288e GS |
4727 | cur2 = 0; |
4728 | } | |
e01b9e88 SC |
4729 | else |
4730 | pv2 = SvPV(sv2, cur2); | |
79072805 | 4731 | |
e01b9e88 SC |
4732 | /* do not utf8ize the comparands as a side-effect */ |
4733 | if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTE) { | |
1aa99e6b IH |
4734 | if (PL_hints & HINT_UTF8_DISTINCT) |
4735 | return SvUTF8(sv1) ? 1 : -1; | |
4736 | ||
e01b9e88 SC |
4737 | if (SvUTF8(sv1)) { |
4738 | pv2 = (char*)bytes_to_utf8((U8*)pv2, &cur2); | |
4739 | pv2tmp = TRUE; | |
4740 | } | |
4741 | else { | |
4742 | pv1 = (char*)bytes_to_utf8((U8*)pv1, &cur1); | |
4743 | pv1tmp = TRUE; | |
4744 | } | |
4745 | } | |
79072805 | 4746 | |
e01b9e88 SC |
4747 | if (!cur1) { |
4748 | cmp = cur2 ? -1 : 0; | |
4749 | } else if (!cur2) { | |
4750 | cmp = 1; | |
4751 | } else { | |
4752 | I32 retval = memcmp((void*)pv1, (void*)pv2, cur1 < cur2 ? cur1 : cur2); | |
4753 | ||
4754 | if (retval) { | |
4755 | cmp = retval < 0 ? -1 : 1; | |
4756 | } else if (cur1 == cur2) { | |
4757 | cmp = 0; | |
4758 | } else { | |
4759 | cmp = cur1 < cur2 ? -1 : 1; | |
4760 | } | |
4761 | } | |
16660edb | 4762 | |
e01b9e88 SC |
4763 | if (pv1tmp) |
4764 | Safefree(pv1); | |
4765 | if (pv2tmp) | |
4766 | Safefree(pv2); | |
16660edb | 4767 | |
e01b9e88 | 4768 | return cmp; |
bbce6d69 | 4769 | } |
16660edb | 4770 | |
c461cf8f JH |
4771 | /* |
4772 | =for apidoc sv_cmp_locale | |
4773 | ||
4774 | Compares the strings in two SVs in a locale-aware manner. See | |
4775 | L</sv_cmp_locale> | |
4776 | ||
4777 | =cut | |
4778 | */ | |
4779 | ||
bbce6d69 | 4780 | I32 |
864dbfa3 | 4781 | Perl_sv_cmp_locale(pTHX_ register SV *sv1, register SV *sv2) |
bbce6d69 | 4782 | { |
36477c24 | 4783 | #ifdef USE_LOCALE_COLLATE |
16660edb | 4784 | |
bbce6d69 | 4785 | char *pv1, *pv2; |
4786 | STRLEN len1, len2; | |
4787 | I32 retval; | |
16660edb | 4788 | |
3280af22 | 4789 | if (PL_collation_standard) |
bbce6d69 | 4790 | goto raw_compare; |
16660edb | 4791 | |
bbce6d69 | 4792 | len1 = 0; |
8ac85365 | 4793 | pv1 = sv1 ? sv_collxfrm(sv1, &len1) : (char *) NULL; |
bbce6d69 | 4794 | len2 = 0; |
8ac85365 | 4795 | pv2 = sv2 ? sv_collxfrm(sv2, &len2) : (char *) NULL; |
16660edb | 4796 | |
bbce6d69 | 4797 | if (!pv1 || !len1) { |
4798 | if (pv2 && len2) | |
4799 | return -1; | |
4800 | else | |
4801 | goto raw_compare; | |
4802 | } | |
4803 | else { | |
4804 | if (!pv2 || !len2) | |
4805 | return 1; | |
4806 | } | |
16660edb | 4807 | |
bbce6d69 | 4808 | retval = memcmp((void*)pv1, (void*)pv2, len1 < len2 ? len1 : len2); |
16660edb | 4809 | |
bbce6d69 | 4810 | if (retval) |
16660edb | 4811 | return retval < 0 ? -1 : 1; |
4812 | ||
bbce6d69 | 4813 | /* |
4814 | * When the result of collation is equality, that doesn't mean | |
4815 | * that there are no differences -- some locales exclude some | |
4816 | * characters from consideration. So to avoid false equalities, | |
4817 | * we use the raw string as a tiebreaker. | |
4818 | */ | |
16660edb | 4819 | |
bbce6d69 | 4820 | raw_compare: |
4821 | /* FALL THROUGH */ | |
16660edb | 4822 | |
36477c24 | 4823 | #endif /* USE_LOCALE_COLLATE */ |
16660edb | 4824 | |
bbce6d69 | 4825 | return sv_cmp(sv1, sv2); |
4826 | } | |
79072805 | 4827 | |
36477c24 | 4828 | #ifdef USE_LOCALE_COLLATE |
7a4c00b4 | 4829 | /* |
4830 | * Any scalar variable may carry an 'o' magic that contains the | |
4831 | * scalar data of the variable transformed to such a format that | |
4832 | * a normal memory comparison can be used to compare the data | |
4833 | * according to the locale settings. | |
4834 | */ | |
bbce6d69 | 4835 | char * |
864dbfa3 | 4836 | Perl_sv_collxfrm(pTHX_ SV *sv, STRLEN *nxp) |
bbce6d69 | 4837 | { |
7a4c00b4 | 4838 | MAGIC *mg; |
16660edb | 4839 | |
8ac85365 | 4840 | mg = SvMAGICAL(sv) ? mg_find(sv, 'o') : (MAGIC *) NULL; |
3280af22 | 4841 | if (!mg || !mg->mg_ptr || *(U32*)mg->mg_ptr != PL_collation_ix) { |
bbce6d69 | 4842 | char *s, *xf; |
4843 | STRLEN len, xlen; | |
4844 | ||
7a4c00b4 | 4845 | if (mg) |
4846 | Safefree(mg->mg_ptr); | |
bbce6d69 | 4847 | s = SvPV(sv, len); |
4848 | if ((xf = mem_collxfrm(s, len, &xlen))) { | |
ff0cee69 | 4849 | if (SvREADONLY(sv)) { |
4850 | SAVEFREEPV(xf); | |
4851 | *nxp = xlen; | |
3280af22 | 4852 | return xf + sizeof(PL_collation_ix); |
ff0cee69 | 4853 | } |
7a4c00b4 | 4854 | if (! mg) { |
4855 | sv_magic(sv, 0, 'o', 0, 0); | |
4856 | mg = mg_find(sv, 'o'); | |
4857 | assert(mg); | |
bbce6d69 | 4858 | } |
7a4c00b4 | 4859 | mg->mg_ptr = xf; |
565764a8 | 4860 | mg->mg_len = xlen; |
7a4c00b4 | 4861 | } |
4862 | else { | |
ff0cee69 | 4863 | if (mg) { |
4864 | mg->mg_ptr = NULL; | |
565764a8 | 4865 | mg->mg_len = -1; |
ff0cee69 | 4866 | } |
bbce6d69 | 4867 | } |
4868 | } | |
7a4c00b4 | 4869 | if (mg && mg->mg_ptr) { |
565764a8 | 4870 | *nxp = mg->mg_len; |
3280af22 | 4871 | return mg->mg_ptr + sizeof(PL_collation_ix); |
bbce6d69 | 4872 | } |
4873 | else { | |
4874 | *nxp = 0; | |
4875 | return NULL; | |
16660edb | 4876 | } |
79072805 LW |
4877 | } |
4878 | ||
36477c24 | 4879 | #endif /* USE_LOCALE_COLLATE */ |
bbce6d69 | 4880 | |
c461cf8f JH |
4881 | /* |
4882 | =for apidoc sv_gets | |
4883 | ||
4884 | Get a line from the filehandle and store it into the SV, optionally | |
4885 | appending to the currently-stored string. | |
4886 | ||
4887 | =cut | |
4888 | */ | |
4889 | ||
79072805 | 4890 | char * |
864dbfa3 | 4891 | Perl_sv_gets(pTHX_ register SV *sv, register PerlIO *fp, I32 append) |
79072805 | 4892 | { |
c07a80fd | 4893 | char *rsptr; |
4894 | STRLEN rslen; | |
4895 | register STDCHAR rslast; | |
4896 | register STDCHAR *bp; | |
4897 | register I32 cnt; | |
4898 | I32 i; | |
4899 | ||
2213622d | 4900 | SV_CHECK_THINKFIRST(sv); |
6fc92669 | 4901 | (void)SvUPGRADE(sv, SVt_PV); |
99491443 | 4902 | |
ff68c719 | 4903 | SvSCREAM_off(sv); |
c07a80fd | 4904 | |
3280af22 | 4905 | if (RsSNARF(PL_rs)) { |
c07a80fd | 4906 | rsptr = NULL; |
4907 | rslen = 0; | |
4908 | } | |
3280af22 | 4909 | else if (RsRECORD(PL_rs)) { |
5b2b9c68 HM |
4910 | I32 recsize, bytesread; |
4911 | char *buffer; | |
4912 | ||
4913 | /* Grab the size of the record we're getting */ | |
3280af22 | 4914 | recsize = SvIV(SvRV(PL_rs)); |
5b2b9c68 | 4915 | (void)SvPOK_only(sv); /* Validate pointer */ |
e670df4e | 4916 | buffer = SvGROW(sv, recsize + 1); |
5b2b9c68 HM |
4917 | /* Go yank in */ |
4918 | #ifdef VMS | |
4919 | /* VMS wants read instead of fread, because fread doesn't respect */ | |
4920 | /* RMS record boundaries. This is not necessarily a good thing to be */ | |
4921 | /* doing, but we've got no other real choice */ | |
4922 | bytesread = PerlLIO_read(PerlIO_fileno(fp), buffer, recsize); | |
4923 | #else | |
4924 | bytesread = PerlIO_read(fp, buffer, recsize); | |
4925 | #endif | |
4926 | SvCUR_set(sv, bytesread); | |
e670df4e | 4927 | buffer[bytesread] = '\0'; |
7d59b7e4 NIS |
4928 | if (PerlIO_isutf8(fp)) |
4929 | SvUTF8_on(sv); | |
4930 | else | |
4931 | SvUTF8_off(sv); | |
5b2b9c68 HM |
4932 | return(SvCUR(sv) ? SvPVX(sv) : Nullch); |
4933 | } | |
3280af22 | 4934 | else if (RsPARA(PL_rs)) { |
c07a80fd | 4935 | rsptr = "\n\n"; |
4936 | rslen = 2; | |
4937 | } | |
7d59b7e4 NIS |
4938 | else { |
4939 | /* Get $/ i.e. PL_rs into same encoding as stream wants */ | |
4940 | if (PerlIO_isutf8(fp)) { | |
4941 | rsptr = SvPVutf8(PL_rs, rslen); | |
4942 | } | |
4943 | else { | |
4944 | if (SvUTF8(PL_rs)) { | |
4945 | if (!sv_utf8_downgrade(PL_rs, TRUE)) { | |
4946 | Perl_croak(aTHX_ "Wide character in $/"); | |
4947 | } | |
4948 | } | |
4949 | rsptr = SvPV(PL_rs, rslen); | |
4950 | } | |
4951 | } | |
4952 | ||
c07a80fd | 4953 | rslast = rslen ? rsptr[rslen - 1] : '\0'; |
4954 | ||
3280af22 | 4955 | if (RsPARA(PL_rs)) { /* have to do this both before and after */ |
79072805 | 4956 | do { /* to make sure file boundaries work right */ |
760ac839 | 4957 | if (PerlIO_eof(fp)) |
a0d0e21e | 4958 | return 0; |
760ac839 | 4959 | i = PerlIO_getc(fp); |
79072805 | 4960 | if (i != '\n') { |
a0d0e21e LW |
4961 | if (i == -1) |
4962 | return 0; | |
760ac839 | 4963 | PerlIO_ungetc(fp,i); |
79072805 LW |
4964 | break; |
4965 | } | |
4966 | } while (i != EOF); | |
4967 | } | |
c07a80fd | 4968 | |
760ac839 LW |
4969 | /* See if we know enough about I/O mechanism to cheat it ! */ |
4970 | ||
4971 | /* This used to be #ifdef test - it is made run-time test for ease | |
1c846c1f | 4972 | of abstracting out stdio interface. One call should be cheap |
760ac839 LW |
4973 | enough here - and may even be a macro allowing compile |
4974 | time optimization. | |
4975 | */ | |
4976 | ||
4977 | if (PerlIO_fast_gets(fp)) { | |
4978 | ||
4979 | /* | |
4980 | * We're going to steal some values from the stdio struct | |
4981 | * and put EVERYTHING in the innermost loop into registers. | |
4982 | */ | |
4983 | register STDCHAR *ptr; | |
4984 | STRLEN bpx; | |
4985 | I32 shortbuffered; | |
4986 | ||
16660edb | 4987 | #if defined(VMS) && defined(PERLIO_IS_STDIO) |
4988 | /* An ungetc()d char is handled separately from the regular | |
4989 | * buffer, so we getc() it back out and stuff it in the buffer. | |
4990 | */ | |
4991 | i = PerlIO_getc(fp); | |
4992 | if (i == EOF) return 0; | |
4993 | *(--((*fp)->_ptr)) = (unsigned char) i; | |
4994 | (*fp)->_cnt++; | |
4995 | #endif | |
c07a80fd | 4996 | |
c2960299 | 4997 | /* Here is some breathtakingly efficient cheating */ |
c07a80fd | 4998 | |
a20bf0c3 | 4999 | cnt = PerlIO_get_cnt(fp); /* get count into register */ |
a0d0e21e | 5000 | (void)SvPOK_only(sv); /* validate pointer */ |
79072805 LW |
5001 | if (SvLEN(sv) - append <= cnt + 1) { /* make sure we have the room */ |
5002 | if (cnt > 80 && SvLEN(sv) > append) { | |
5003 | shortbuffered = cnt - SvLEN(sv) + append + 1; | |
5004 | cnt -= shortbuffered; | |
5005 | } | |
5006 | else { | |
5007 | shortbuffered = 0; | |
bbce6d69 | 5008 | /* remember that cnt can be negative */ |
5009 | SvGROW(sv, append + (cnt <= 0 ? 2 : (cnt + 1))); | |
79072805 LW |
5010 | } |
5011 | } | |
5012 | else | |
5013 | shortbuffered = 0; | |
c07a80fd | 5014 | bp = (STDCHAR*)SvPVX(sv) + append; /* move these two too to registers */ |
a20bf0c3 | 5015 | ptr = (STDCHAR*)PerlIO_get_ptr(fp); |
16660edb | 5016 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5017 | "Screamer: entering, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt)); |
16660edb | 5018 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5019 | "Screamer: entering: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n", |
1c846c1f | 5020 | PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp), |
1d7c1841 | 5021 | PTR2UV(PerlIO_has_base(fp) ? PerlIO_get_base(fp) : 0))); |
79072805 LW |
5022 | for (;;) { |
5023 | screamer: | |
93a17b20 | 5024 | if (cnt > 0) { |
c07a80fd | 5025 | if (rslen) { |
760ac839 LW |
5026 | while (cnt > 0) { /* this | eat */ |
5027 | cnt--; | |
c07a80fd | 5028 | if ((*bp++ = *ptr++) == rslast) /* really | dust */ |
5029 | goto thats_all_folks; /* screams | sed :-) */ | |
5030 | } | |
5031 | } | |
5032 | else { | |
1c846c1f NIS |
5033 | Copy(ptr, bp, cnt, char); /* this | eat */ |
5034 | bp += cnt; /* screams | dust */ | |
c07a80fd | 5035 | ptr += cnt; /* louder | sed :-) */ |
a5f75d66 | 5036 | cnt = 0; |
93a17b20 | 5037 | } |
79072805 LW |
5038 | } |
5039 | ||
748a9306 | 5040 | if (shortbuffered) { /* oh well, must extend */ |
79072805 LW |
5041 | cnt = shortbuffered; |
5042 | shortbuffered = 0; | |
c07a80fd | 5043 | bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */ |
79072805 LW |
5044 | SvCUR_set(sv, bpx); |
5045 | SvGROW(sv, SvLEN(sv) + append + cnt + 2); | |
c07a80fd | 5046 | bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */ |
79072805 LW |
5047 | continue; |
5048 | } | |
5049 | ||
16660edb | 5050 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 GS |
5051 | "Screamer: going to getc, ptr=%"UVuf", cnt=%ld\n", |
5052 | PTR2UV(ptr),(long)cnt)); | |
a20bf0c3 | 5053 | PerlIO_set_ptrcnt(fp, ptr, cnt); /* deregisterize cnt and ptr */ |
16660edb | 5054 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5055 | "Screamer: pre: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n", |
1c846c1f | 5056 | PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp), |
1d7c1841 | 5057 | PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0))); |
1c846c1f | 5058 | /* This used to call 'filbuf' in stdio form, but as that behaves like |
774d564b | 5059 | getc when cnt <= 0 we use PerlIO_getc here to avoid introducing |
5060 | another abstraction. */ | |
760ac839 | 5061 | i = PerlIO_getc(fp); /* get more characters */ |
16660edb | 5062 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5063 | "Screamer: post: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n", |
1c846c1f | 5064 | PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp), |
1d7c1841 | 5065 | PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0))); |
a20bf0c3 JH |
5066 | cnt = PerlIO_get_cnt(fp); |
5067 | ptr = (STDCHAR*)PerlIO_get_ptr(fp); /* reregisterize cnt and ptr */ | |
16660edb | 5068 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5069 | "Screamer: after getc, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt)); |
79072805 | 5070 | |
748a9306 LW |
5071 | if (i == EOF) /* all done for ever? */ |
5072 | goto thats_really_all_folks; | |
5073 | ||
c07a80fd | 5074 | bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */ |
79072805 LW |
5075 | SvCUR_set(sv, bpx); |
5076 | SvGROW(sv, bpx + cnt + 2); | |
c07a80fd | 5077 | bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */ |
5078 | ||
760ac839 | 5079 | *bp++ = i; /* store character from PerlIO_getc */ |
79072805 | 5080 | |
c07a80fd | 5081 | if (rslen && (STDCHAR)i == rslast) /* all done for now? */ |
79072805 | 5082 | goto thats_all_folks; |
79072805 LW |
5083 | } |
5084 | ||
5085 | thats_all_folks: | |
c07a80fd | 5086 | if ((rslen > 1 && (bp - (STDCHAR*)SvPVX(sv) < rslen)) || |
36477c24 | 5087 | memNE((char*)bp - rslen, rsptr, rslen)) |
760ac839 | 5088 | goto screamer; /* go back to the fray */ |
79072805 LW |
5089 | thats_really_all_folks: |
5090 | if (shortbuffered) | |
5091 | cnt += shortbuffered; | |
16660edb | 5092 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5093 | "Screamer: quitting, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt)); |
a20bf0c3 | 5094 | PerlIO_set_ptrcnt(fp, ptr, cnt); /* put these back or we're in trouble */ |
16660edb | 5095 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5096 | "Screamer: end: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n", |
1c846c1f | 5097 | PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp), |
1d7c1841 | 5098 | PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0))); |
79072805 | 5099 | *bp = '\0'; |
760ac839 | 5100 | SvCUR_set(sv, bp - (STDCHAR*)SvPVX(sv)); /* set length */ |
16660edb | 5101 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
fb73857a | 5102 | "Screamer: done, len=%ld, string=|%.*s|\n", |
5103 | (long)SvCUR(sv),(int)SvCUR(sv),SvPVX(sv))); | |
760ac839 LW |
5104 | } |
5105 | else | |
79072805 | 5106 | { |
4d2c4e07 | 5107 | #ifndef EPOC |
760ac839 | 5108 | /*The big, slow, and stupid way */ |
c07a80fd | 5109 | STDCHAR buf[8192]; |
4d2c4e07 OF |
5110 | #else |
5111 | /* Need to work around EPOC SDK features */ | |
5112 | /* On WINS: MS VC5 generates calls to _chkstk, */ | |
5113 | /* if a `large' stack frame is allocated */ | |
5114 | /* gcc on MARM does not generate calls like these */ | |
5115 | STDCHAR buf[1024]; | |
5116 | #endif | |
79072805 | 5117 | |
760ac839 | 5118 | screamer2: |
c07a80fd | 5119 | if (rslen) { |
760ac839 LW |
5120 | register STDCHAR *bpe = buf + sizeof(buf); |
5121 | bp = buf; | |
5122 | while ((i = PerlIO_getc(fp)) != EOF && (*bp++ = i) != rslast && bp < bpe) | |
5123 | ; /* keep reading */ | |
5124 | cnt = bp - buf; | |
c07a80fd | 5125 | } |
5126 | else { | |
760ac839 | 5127 | cnt = PerlIO_read(fp,(char*)buf, sizeof(buf)); |
16660edb | 5128 | /* Accomodate broken VAXC compiler, which applies U8 cast to |
5129 | * both args of ?: operator, causing EOF to change into 255 | |
5130 | */ | |
5131 | if (cnt) { i = (U8)buf[cnt - 1]; } else { i = EOF; } | |
c07a80fd | 5132 | } |
79072805 LW |
5133 | |
5134 | if (append) | |
760ac839 | 5135 | sv_catpvn(sv, (char *) buf, cnt); |
79072805 | 5136 | else |
760ac839 | 5137 | sv_setpvn(sv, (char *) buf, cnt); |
c07a80fd | 5138 | |
5139 | if (i != EOF && /* joy */ | |
5140 | (!rslen || | |
5141 | SvCUR(sv) < rslen || | |
36477c24 | 5142 | memNE(SvPVX(sv) + SvCUR(sv) - rslen, rsptr, rslen))) |
79072805 LW |
5143 | { |
5144 | append = -1; | |
63e4d877 CS |
5145 | /* |
5146 | * If we're reading from a TTY and we get a short read, | |
5147 | * indicating that the user hit his EOF character, we need | |
5148 | * to notice it now, because if we try to read from the TTY | |
5149 | * again, the EOF condition will disappear. | |
5150 | * | |
5151 | * The comparison of cnt to sizeof(buf) is an optimization | |
5152 | * that prevents unnecessary calls to feof(). | |
5153 | * | |
5154 | * - jik 9/25/96 | |
5155 | */ | |
5156 | if (!(cnt < sizeof(buf) && PerlIO_eof(fp))) | |
5157 | goto screamer2; | |
79072805 LW |
5158 | } |
5159 | } | |
5160 | ||
1c846c1f | 5161 | if (RsPARA(PL_rs)) { /* have to do this both before and after */ |
c07a80fd | 5162 | while (i != EOF) { /* to make sure file boundaries work right */ |
760ac839 | 5163 | i = PerlIO_getc(fp); |
79072805 | 5164 | if (i != '\n') { |
760ac839 | 5165 | PerlIO_ungetc(fp,i); |
79072805 LW |
5166 | break; |
5167 | } | |
5168 | } | |
5169 | } | |
c07a80fd | 5170 | |
7d59b7e4 NIS |
5171 | if (PerlIO_isutf8(fp)) |
5172 | SvUTF8_on(sv); | |
5173 | else | |
5174 | SvUTF8_off(sv); | |
5175 | ||
c07a80fd | 5176 | return (SvCUR(sv) - append) ? SvPVX(sv) : Nullch; |
79072805 LW |
5177 | } |
5178 | ||
760ac839 | 5179 | |
954c1994 GS |
5180 | /* |
5181 | =for apidoc sv_inc | |
5182 | ||
5183 | Auto-increment of the value in the SV. | |
5184 | ||
5185 | =cut | |
5186 | */ | |
5187 | ||
79072805 | 5188 | void |
864dbfa3 | 5189 | Perl_sv_inc(pTHX_ register SV *sv) |
79072805 LW |
5190 | { |
5191 | register char *d; | |
463ee0b2 | 5192 | int flags; |
79072805 LW |
5193 | |
5194 | if (!sv) | |
5195 | return; | |
b23a5f78 GB |
5196 | if (SvGMAGICAL(sv)) |
5197 | mg_get(sv); | |
ed6116ce | 5198 | if (SvTHINKFIRST(sv)) { |
0f15f207 | 5199 | if (SvREADONLY(sv)) { |
3280af22 | 5200 | if (PL_curcop != &PL_compiling) |
cea2e8a9 | 5201 | Perl_croak(aTHX_ PL_no_modify); |
0f15f207 | 5202 | } |
a0d0e21e | 5203 | if (SvROK(sv)) { |
b5be31e9 | 5204 | IV i; |
9e7bc3e8 JD |
5205 | if (SvAMAGIC(sv) && AMG_CALLun(sv,inc)) |
5206 | return; | |
56431972 | 5207 | i = PTR2IV(SvRV(sv)); |
b5be31e9 SM |
5208 | sv_unref(sv); |
5209 | sv_setiv(sv, i); | |
a0d0e21e | 5210 | } |
ed6116ce | 5211 | } |
8990e307 | 5212 | flags = SvFLAGS(sv); |
28e5dec8 JH |
5213 | if ((flags & (SVp_NOK|SVp_IOK)) == SVp_NOK) { |
5214 | /* It's (privately or publicly) a float, but not tested as an | |
5215 | integer, so test it to see. */ | |
5216 | (void) SvIV(sv); | |
5217 | flags = SvFLAGS(sv); | |
5218 | } | |
5219 | if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) { | |
5220 | /* It's publicly an integer, or privately an integer-not-float */ | |
5221 | oops_its_int: | |
25da4f38 IZ |
5222 | if (SvIsUV(sv)) { |
5223 | if (SvUVX(sv) == UV_MAX) | |
65202027 | 5224 | sv_setnv(sv, (NV)UV_MAX + 1.0); |
25da4f38 IZ |
5225 | else |
5226 | (void)SvIOK_only_UV(sv); | |
5227 | ++SvUVX(sv); | |
5228 | } else { | |
5229 | if (SvIVX(sv) == IV_MAX) | |
28e5dec8 | 5230 | sv_setuv(sv, (UV)IV_MAX + 1); |
25da4f38 IZ |
5231 | else { |
5232 | (void)SvIOK_only(sv); | |
5233 | ++SvIVX(sv); | |
1c846c1f | 5234 | } |
55497cff | 5235 | } |
79072805 LW |
5236 | return; |
5237 | } | |
28e5dec8 JH |
5238 | if (flags & SVp_NOK) { |
5239 | (void)SvNOK_only(sv); | |
5240 | SvNVX(sv) += 1.0; | |
5241 | return; | |
5242 | } | |
5243 | ||
8990e307 | 5244 | if (!(flags & SVp_POK) || !*SvPVX(sv)) { |
28e5dec8 JH |
5245 | if ((flags & SVTYPEMASK) < SVt_PVIV) |
5246 | sv_upgrade(sv, SVt_IV); | |
5247 | (void)SvIOK_only(sv); | |
5248 | SvIVX(sv) = 1; | |
79072805 LW |
5249 | return; |
5250 | } | |
463ee0b2 | 5251 | d = SvPVX(sv); |
79072805 LW |
5252 | while (isALPHA(*d)) d++; |
5253 | while (isDIGIT(*d)) d++; | |
5254 | if (*d) { | |
28e5dec8 JH |
5255 | #ifdef PERL_PRESERVE_IVUV |
5256 | /* Got to punt this an an integer if needs be, but we don't issue | |
5257 | warnings. Probably ought to make the sv_iv_please() that does | |
5258 | the conversion if possible, and silently. */ | |
5259 | I32 numtype = looks_like_number(sv); | |
5260 | if (numtype && !(numtype & IS_NUMBER_INFINITY)) { | |
5261 | /* Need to try really hard to see if it's an integer. | |
5262 | 9.22337203685478e+18 is an integer. | |
5263 | but "9.22337203685478e+18" + 0 is UV=9223372036854779904 | |
5264 | so $a="9.22337203685478e+18"; $a+0; $a++ | |
5265 | needs to be the same as $a="9.22337203685478e+18"; $a++ | |
5266 | or we go insane. */ | |
5267 | ||
5268 | (void) sv_2iv(sv); | |
5269 | if (SvIOK(sv)) | |
5270 | goto oops_its_int; | |
5271 | ||
5272 | /* sv_2iv *should* have made this an NV */ | |
5273 | if (flags & SVp_NOK) { | |
5274 | (void)SvNOK_only(sv); | |
5275 | SvNVX(sv) += 1.0; | |
5276 | return; | |
5277 | } | |
5278 | /* I don't think we can get here. Maybe I should assert this | |
5279 | And if we do get here I suspect that sv_setnv will croak. NWC | |
5280 | Fall through. */ | |
5281 | #if defined(USE_LONG_DOUBLE) | |
5282 | 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", | |
5283 | SvPVX(sv), SvIVX(sv), SvNVX(sv))); | |
5284 | #else | |
5285 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%g\n", | |
5286 | SvPVX(sv), SvIVX(sv), SvNVX(sv))); | |
5287 | #endif | |
5288 | } | |
5289 | #endif /* PERL_PRESERVE_IVUV */ | |
5290 | sv_setnv(sv,Atof(SvPVX(sv)) + 1.0); | |
79072805 LW |
5291 | return; |
5292 | } | |
5293 | d--; | |
463ee0b2 | 5294 | while (d >= SvPVX(sv)) { |
79072805 LW |
5295 | if (isDIGIT(*d)) { |
5296 | if (++*d <= '9') | |
5297 | return; | |
5298 | *(d--) = '0'; | |
5299 | } | |
5300 | else { | |
9d116dd7 JH |
5301 | #ifdef EBCDIC |
5302 | /* MKS: The original code here died if letters weren't consecutive. | |
5303 | * at least it didn't have to worry about non-C locales. The | |
5304 | * new code assumes that ('z'-'a')==('Z'-'A'), letters are | |
1c846c1f | 5305 | * arranged in order (although not consecutively) and that only |
9d116dd7 JH |
5306 | * [A-Za-z] are accepted by isALPHA in the C locale. |
5307 | */ | |
5308 | if (*d != 'z' && *d != 'Z') { | |
5309 | do { ++*d; } while (!isALPHA(*d)); | |
5310 | return; | |
5311 | } | |
5312 | *(d--) -= 'z' - 'a'; | |
5313 | #else | |
79072805 LW |
5314 | ++*d; |
5315 | if (isALPHA(*d)) | |
5316 | return; | |
5317 | *(d--) -= 'z' - 'a' + 1; | |
9d116dd7 | 5318 | #endif |
79072805 LW |
5319 | } |
5320 | } | |
5321 | /* oh,oh, the number grew */ | |
5322 | SvGROW(sv, SvCUR(sv) + 2); | |
5323 | SvCUR(sv)++; | |
463ee0b2 | 5324 | for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX(sv); d--) |
79072805 LW |
5325 | *d = d[-1]; |
5326 | if (isDIGIT(d[1])) | |
5327 | *d = '1'; | |
5328 | else | |
5329 | *d = d[1]; | |
5330 | } | |
5331 | ||
954c1994 GS |
5332 | /* |
5333 | =for apidoc sv_dec | |
5334 | ||
5335 | Auto-decrement of the value in the SV. | |
5336 | ||
5337 | =cut | |
5338 | */ | |
5339 | ||
79072805 | 5340 | void |
864dbfa3 | 5341 | Perl_sv_dec(pTHX_ register SV *sv) |
79072805 | 5342 | { |
463ee0b2 LW |
5343 | int flags; |
5344 | ||
79072805 LW |
5345 | if (!sv) |
5346 | return; | |
b23a5f78 GB |
5347 | if (SvGMAGICAL(sv)) |
5348 | mg_get(sv); | |
ed6116ce | 5349 | if (SvTHINKFIRST(sv)) { |
0f15f207 | 5350 | if (SvREADONLY(sv)) { |
3280af22 | 5351 | if (PL_curcop != &PL_compiling) |
cea2e8a9 | 5352 | Perl_croak(aTHX_ PL_no_modify); |
0f15f207 | 5353 | } |
a0d0e21e | 5354 | if (SvROK(sv)) { |
b5be31e9 | 5355 | IV i; |
9e7bc3e8 JD |
5356 | if (SvAMAGIC(sv) && AMG_CALLun(sv,dec)) |
5357 | return; | |
56431972 | 5358 | i = PTR2IV(SvRV(sv)); |
b5be31e9 SM |
5359 | sv_unref(sv); |
5360 | sv_setiv(sv, i); | |
a0d0e21e | 5361 | } |
ed6116ce | 5362 | } |
28e5dec8 JH |
5363 | /* Unlike sv_inc we don't have to worry about string-never-numbers |
5364 | and keeping them magic. But we mustn't warn on punting */ | |
8990e307 | 5365 | flags = SvFLAGS(sv); |
28e5dec8 JH |
5366 | if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) { |
5367 | /* It's publicly an integer, or privately an integer-not-float */ | |
5368 | oops_its_int: | |
25da4f38 IZ |
5369 | if (SvIsUV(sv)) { |
5370 | if (SvUVX(sv) == 0) { | |
5371 | (void)SvIOK_only(sv); | |
5372 | SvIVX(sv) = -1; | |
5373 | } | |
5374 | else { | |
5375 | (void)SvIOK_only_UV(sv); | |
5376 | --SvUVX(sv); | |
1c846c1f | 5377 | } |
25da4f38 IZ |
5378 | } else { |
5379 | if (SvIVX(sv) == IV_MIN) | |
65202027 | 5380 | sv_setnv(sv, (NV)IV_MIN - 1.0); |
25da4f38 IZ |
5381 | else { |
5382 | (void)SvIOK_only(sv); | |
5383 | --SvIVX(sv); | |
1c846c1f | 5384 | } |
55497cff | 5385 | } |
5386 | return; | |
5387 | } | |
28e5dec8 JH |
5388 | if (flags & SVp_NOK) { |
5389 | SvNVX(sv) -= 1.0; | |
5390 | (void)SvNOK_only(sv); | |
5391 | return; | |
5392 | } | |
8990e307 | 5393 | if (!(flags & SVp_POK)) { |
4633a7c4 LW |
5394 | if ((flags & SVTYPEMASK) < SVt_PVNV) |
5395 | sv_upgrade(sv, SVt_NV); | |
463ee0b2 | 5396 | SvNVX(sv) = -1.0; |
a0d0e21e | 5397 | (void)SvNOK_only(sv); |
79072805 LW |
5398 | return; |
5399 | } | |
28e5dec8 JH |
5400 | #ifdef PERL_PRESERVE_IVUV |
5401 | { | |
5402 | I32 numtype = looks_like_number(sv); | |
5403 | if (numtype && !(numtype & IS_NUMBER_INFINITY)) { | |
5404 | /* Need to try really hard to see if it's an integer. | |
5405 | 9.22337203685478e+18 is an integer. | |
5406 | but "9.22337203685478e+18" + 0 is UV=9223372036854779904 | |
5407 | so $a="9.22337203685478e+18"; $a+0; $a-- | |
5408 | needs to be the same as $a="9.22337203685478e+18"; $a-- | |
5409 | or we go insane. */ | |
5410 | ||
5411 | (void) sv_2iv(sv); | |
5412 | if (SvIOK(sv)) | |
5413 | goto oops_its_int; | |
5414 | ||
5415 | /* sv_2iv *should* have made this an NV */ | |
5416 | if (flags & SVp_NOK) { | |
5417 | (void)SvNOK_only(sv); | |
5418 | SvNVX(sv) -= 1.0; | |
5419 | return; | |
5420 | } | |
5421 | /* I don't think we can get here. Maybe I should assert this | |
5422 | And if we do get here I suspect that sv_setnv will croak. NWC | |
5423 | Fall through. */ | |
5424 | #if defined(USE_LONG_DOUBLE) | |
5425 | 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", | |
5426 | SvPVX(sv), SvIVX(sv), SvNVX(sv))); | |
5427 | #else | |
5428 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%g\n", | |
5429 | SvPVX(sv), SvIVX(sv), SvNVX(sv))); | |
5430 | #endif | |
5431 | } | |
5432 | } | |
5433 | #endif /* PERL_PRESERVE_IVUV */ | |
097ee67d | 5434 | sv_setnv(sv,Atof(SvPVX(sv)) - 1.0); /* punt */ |
79072805 LW |
5435 | } |
5436 | ||
954c1994 GS |
5437 | /* |
5438 | =for apidoc sv_mortalcopy | |
5439 | ||
5440 | Creates a new SV which is a copy of the original SV. The new SV is marked | |
5441 | as mortal. | |
5442 | ||
5443 | =cut | |
5444 | */ | |
5445 | ||
79072805 LW |
5446 | /* Make a string that will exist for the duration of the expression |
5447 | * evaluation. Actually, it may have to last longer than that, but | |
5448 | * hopefully we won't free it until it has been assigned to a | |
5449 | * permanent location. */ | |
5450 | ||
5451 | SV * | |
864dbfa3 | 5452 | Perl_sv_mortalcopy(pTHX_ SV *oldstr) |
79072805 | 5453 | { |
463ee0b2 | 5454 | register SV *sv; |
79072805 | 5455 | |
4561caa4 | 5456 | new_SV(sv); |
79072805 | 5457 | sv_setsv(sv,oldstr); |
677b06e3 GS |
5458 | EXTEND_MORTAL(1); |
5459 | PL_tmps_stack[++PL_tmps_ix] = sv; | |
8990e307 LW |
5460 | SvTEMP_on(sv); |
5461 | return sv; | |
5462 | } | |
5463 | ||
954c1994 GS |
5464 | /* |
5465 | =for apidoc sv_newmortal | |
5466 | ||
5467 | Creates a new SV which is mortal. The reference count of the SV is set to 1. | |
5468 | ||
5469 | =cut | |
5470 | */ | |
5471 | ||
8990e307 | 5472 | SV * |
864dbfa3 | 5473 | Perl_sv_newmortal(pTHX) |
8990e307 LW |
5474 | { |
5475 | register SV *sv; | |
5476 | ||
4561caa4 | 5477 | new_SV(sv); |
8990e307 | 5478 | SvFLAGS(sv) = SVs_TEMP; |
677b06e3 GS |
5479 | EXTEND_MORTAL(1); |
5480 | PL_tmps_stack[++PL_tmps_ix] = sv; | |
79072805 LW |
5481 | return sv; |
5482 | } | |
5483 | ||
954c1994 GS |
5484 | /* |
5485 | =for apidoc sv_2mortal | |
5486 | ||
5487 | Marks an SV as mortal. The SV will be destroyed when the current context | |
5488 | ends. | |
5489 | ||
5490 | =cut | |
5491 | */ | |
5492 | ||
79072805 LW |
5493 | /* same thing without the copying */ |
5494 | ||
5495 | SV * | |
864dbfa3 | 5496 | Perl_sv_2mortal(pTHX_ register SV *sv) |
79072805 LW |
5497 | { |
5498 | if (!sv) | |
5499 | return sv; | |
d689ffdd | 5500 | if (SvREADONLY(sv) && SvIMMORTAL(sv)) |
11162842 | 5501 | return sv; |
677b06e3 GS |
5502 | EXTEND_MORTAL(1); |
5503 | PL_tmps_stack[++PL_tmps_ix] = sv; | |
8990e307 | 5504 | SvTEMP_on(sv); |
79072805 LW |
5505 | return sv; |
5506 | } | |
5507 | ||
954c1994 GS |
5508 | /* |
5509 | =for apidoc newSVpv | |
5510 | ||
5511 | Creates a new SV and copies a string into it. The reference count for the | |
5512 | SV is set to 1. If C<len> is zero, Perl will compute the length using | |
5513 | strlen(). For efficiency, consider using C<newSVpvn> instead. | |
5514 | ||
5515 | =cut | |
5516 | */ | |
5517 | ||
79072805 | 5518 | SV * |
864dbfa3 | 5519 | Perl_newSVpv(pTHX_ const char *s, STRLEN len) |
79072805 | 5520 | { |
463ee0b2 | 5521 | register SV *sv; |
79072805 | 5522 | |
4561caa4 | 5523 | new_SV(sv); |
79072805 LW |
5524 | if (!len) |
5525 | len = strlen(s); | |
5526 | sv_setpvn(sv,s,len); | |
5527 | return sv; | |
5528 | } | |
5529 | ||
954c1994 GS |
5530 | /* |
5531 | =for apidoc newSVpvn | |
5532 | ||
5533 | Creates a new SV and copies a string into it. The reference count for the | |
1c846c1f | 5534 | SV is set to 1. Note that if C<len> is zero, Perl will create a zero length |
954c1994 GS |
5535 | string. You are responsible for ensuring that the source string is at least |
5536 | C<len> bytes long. | |
5537 | ||
5538 | =cut | |
5539 | */ | |
5540 | ||
9da1e3b5 | 5541 | SV * |
864dbfa3 | 5542 | Perl_newSVpvn(pTHX_ const char *s, STRLEN len) |
9da1e3b5 MUN |
5543 | { |
5544 | register SV *sv; | |
5545 | ||
5546 | new_SV(sv); | |
9da1e3b5 MUN |
5547 | sv_setpvn(sv,s,len); |
5548 | return sv; | |
5549 | } | |
5550 | ||
1c846c1f NIS |
5551 | /* |
5552 | =for apidoc newSVpvn_share | |
5553 | ||
5554 | Creates a new SV and populates it with a string from | |
5555 | the string table. Turns on READONLY and FAKE. | |
5556 | The idea here is that as string table is used for shared hash | |
5557 | keys these strings will have SvPVX == HeKEY and hash lookup | |
5558 | will avoid string compare. | |
5559 | ||
5560 | =cut | |
5561 | */ | |
5562 | ||
5563 | SV * | |
c3654f1a | 5564 | Perl_newSVpvn_share(pTHX_ const char *src, I32 len, U32 hash) |
1c846c1f NIS |
5565 | { |
5566 | register SV *sv; | |
c3654f1a IH |
5567 | bool is_utf8 = FALSE; |
5568 | if (len < 0) { | |
5569 | len = -len; | |
5570 | is_utf8 = TRUE; | |
5571 | } | |
1c846c1f NIS |
5572 | if (!hash) |
5573 | PERL_HASH(hash, src, len); | |
5574 | new_SV(sv); | |
5575 | sv_upgrade(sv, SVt_PVIV); | |
c3654f1a | 5576 | SvPVX(sv) = sharepvn(src, is_utf8?-len:len, hash); |
1c846c1f NIS |
5577 | SvCUR(sv) = len; |
5578 | SvUVX(sv) = hash; | |
5579 | SvLEN(sv) = 0; | |
5580 | SvREADONLY_on(sv); | |
5581 | SvFAKE_on(sv); | |
5582 | SvPOK_on(sv); | |
c3654f1a IH |
5583 | if (is_utf8) |
5584 | SvUTF8_on(sv); | |
1c846c1f NIS |
5585 | return sv; |
5586 | } | |
5587 | ||
cea2e8a9 | 5588 | #if defined(PERL_IMPLICIT_CONTEXT) |
46fc3d4c | 5589 | SV * |
cea2e8a9 | 5590 | Perl_newSVpvf_nocontext(const char* pat, ...) |
46fc3d4c | 5591 | { |
cea2e8a9 | 5592 | dTHX; |
46fc3d4c | 5593 | register SV *sv; |
5594 | va_list args; | |
46fc3d4c | 5595 | va_start(args, pat); |
c5be433b | 5596 | sv = vnewSVpvf(pat, &args); |
46fc3d4c | 5597 | va_end(args); |
5598 | return sv; | |
5599 | } | |
cea2e8a9 | 5600 | #endif |
46fc3d4c | 5601 | |
954c1994 GS |
5602 | /* |
5603 | =for apidoc newSVpvf | |
5604 | ||
5605 | Creates a new SV an initialize it with the string formatted like | |
5606 | C<sprintf>. | |
5607 | ||
5608 | =cut | |
5609 | */ | |
5610 | ||
cea2e8a9 GS |
5611 | SV * |
5612 | Perl_newSVpvf(pTHX_ const char* pat, ...) | |
5613 | { | |
5614 | register SV *sv; | |
5615 | va_list args; | |
cea2e8a9 | 5616 | va_start(args, pat); |
c5be433b | 5617 | sv = vnewSVpvf(pat, &args); |
cea2e8a9 GS |
5618 | va_end(args); |
5619 | return sv; | |
5620 | } | |
46fc3d4c | 5621 | |
79072805 | 5622 | SV * |
c5be433b GS |
5623 | Perl_vnewSVpvf(pTHX_ const char* pat, va_list* args) |
5624 | { | |
5625 | register SV *sv; | |
5626 | new_SV(sv); | |
5627 | sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
5628 | return sv; | |
5629 | } | |
5630 | ||
954c1994 GS |
5631 | /* |
5632 | =for apidoc newSVnv | |
5633 | ||
5634 | Creates a new SV and copies a floating point value into it. | |
5635 | The reference count for the SV is set to 1. | |
5636 | ||
5637 | =cut | |
5638 | */ | |
5639 | ||
c5be433b | 5640 | SV * |
65202027 | 5641 | Perl_newSVnv(pTHX_ NV n) |
79072805 | 5642 | { |
463ee0b2 | 5643 | register SV *sv; |
79072805 | 5644 | |
4561caa4 | 5645 | new_SV(sv); |
79072805 LW |
5646 | sv_setnv(sv,n); |
5647 | return sv; | |
5648 | } | |
5649 | ||
954c1994 GS |
5650 | /* |
5651 | =for apidoc newSViv | |
5652 | ||
5653 | Creates a new SV and copies an integer into it. The reference count for the | |
5654 | SV is set to 1. | |
5655 | ||
5656 | =cut | |
5657 | */ | |
5658 | ||
79072805 | 5659 | SV * |
864dbfa3 | 5660 | Perl_newSViv(pTHX_ IV i) |
79072805 | 5661 | { |
463ee0b2 | 5662 | register SV *sv; |
79072805 | 5663 | |
4561caa4 | 5664 | new_SV(sv); |
79072805 LW |
5665 | sv_setiv(sv,i); |
5666 | return sv; | |
5667 | } | |
5668 | ||
954c1994 | 5669 | /* |
1a3327fb JH |
5670 | =for apidoc newSVuv |
5671 | ||
5672 | Creates a new SV and copies an unsigned integer into it. | |
5673 | The reference count for the SV is set to 1. | |
5674 | ||
5675 | =cut | |
5676 | */ | |
5677 | ||
5678 | SV * | |
5679 | Perl_newSVuv(pTHX_ UV u) | |
5680 | { | |
5681 | register SV *sv; | |
5682 | ||
5683 | new_SV(sv); | |
5684 | sv_setuv(sv,u); | |
5685 | return sv; | |
5686 | } | |
5687 | ||
5688 | /* | |
954c1994 GS |
5689 | =for apidoc newRV_noinc |
5690 | ||
5691 | Creates an RV wrapper for an SV. The reference count for the original | |
5692 | SV is B<not> incremented. | |
5693 | ||
5694 | =cut | |
5695 | */ | |
5696 | ||
2304df62 | 5697 | SV * |
864dbfa3 | 5698 | Perl_newRV_noinc(pTHX_ SV *tmpRef) |
2304df62 AD |
5699 | { |
5700 | register SV *sv; | |
5701 | ||
4561caa4 | 5702 | new_SV(sv); |
2304df62 | 5703 | sv_upgrade(sv, SVt_RV); |
76e3520e | 5704 | SvTEMP_off(tmpRef); |
d689ffdd | 5705 | SvRV(sv) = tmpRef; |
2304df62 | 5706 | SvROK_on(sv); |
2304df62 AD |
5707 | return sv; |
5708 | } | |
5709 | ||
954c1994 | 5710 | /* newRV_inc is #defined to newRV in sv.h */ |
5f05dabc | 5711 | SV * |
864dbfa3 | 5712 | Perl_newRV(pTHX_ SV *tmpRef) |
5f05dabc | 5713 | { |
5f6447b6 | 5714 | return newRV_noinc(SvREFCNT_inc(tmpRef)); |
5f05dabc | 5715 | } |
5f05dabc | 5716 | |
954c1994 GS |
5717 | /* |
5718 | =for apidoc newSVsv | |
5719 | ||
5720 | Creates a new SV which is an exact duplicate of the original SV. | |
5721 | ||
5722 | =cut | |
5723 | */ | |
5724 | ||
79072805 LW |
5725 | /* make an exact duplicate of old */ |
5726 | ||
5727 | SV * | |
864dbfa3 | 5728 | Perl_newSVsv(pTHX_ register SV *old) |
79072805 | 5729 | { |
463ee0b2 | 5730 | register SV *sv; |
79072805 LW |
5731 | |
5732 | if (!old) | |
5733 | return Nullsv; | |
8990e307 | 5734 | if (SvTYPE(old) == SVTYPEMASK) { |
0453d815 PM |
5735 | if (ckWARN_d(WARN_INTERNAL)) |
5736 | Perl_warner(aTHX_ WARN_INTERNAL, "semi-panic: attempt to dup freed string"); | |
79072805 LW |
5737 | return Nullsv; |
5738 | } | |
4561caa4 | 5739 | new_SV(sv); |
ff68c719 | 5740 | if (SvTEMP(old)) { |
5741 | SvTEMP_off(old); | |
463ee0b2 | 5742 | sv_setsv(sv,old); |
ff68c719 | 5743 | SvTEMP_on(old); |
79072805 LW |
5744 | } |
5745 | else | |
463ee0b2 LW |
5746 | sv_setsv(sv,old); |
5747 | return sv; | |
79072805 LW |
5748 | } |
5749 | ||
5750 | void | |
864dbfa3 | 5751 | Perl_sv_reset(pTHX_ register char *s, HV *stash) |
79072805 LW |
5752 | { |
5753 | register HE *entry; | |
5754 | register GV *gv; | |
5755 | register SV *sv; | |
5756 | register I32 i; | |
5757 | register PMOP *pm; | |
5758 | register I32 max; | |
4802d5d7 | 5759 | char todo[PERL_UCHAR_MAX+1]; |
79072805 | 5760 | |
49d8d3a1 MB |
5761 | if (!stash) |
5762 | return; | |
5763 | ||
79072805 LW |
5764 | if (!*s) { /* reset ?? searches */ |
5765 | for (pm = HvPMROOT(stash); pm; pm = pm->op_pmnext) { | |
48c036b1 | 5766 | pm->op_pmdynflags &= ~PMdf_USED; |
79072805 LW |
5767 | } |
5768 | return; | |
5769 | } | |
5770 | ||
5771 | /* reset variables */ | |
5772 | ||
5773 | if (!HvARRAY(stash)) | |
5774 | return; | |
463ee0b2 LW |
5775 | |
5776 | Zero(todo, 256, char); | |
79072805 | 5777 | while (*s) { |
4802d5d7 | 5778 | i = (unsigned char)*s; |
79072805 LW |
5779 | if (s[1] == '-') { |
5780 | s += 2; | |
5781 | } | |
4802d5d7 | 5782 | max = (unsigned char)*s++; |
79072805 | 5783 | for ( ; i <= max; i++) { |
463ee0b2 LW |
5784 | todo[i] = 1; |
5785 | } | |
a0d0e21e | 5786 | for (i = 0; i <= (I32) HvMAX(stash); i++) { |
79072805 | 5787 | for (entry = HvARRAY(stash)[i]; |
9e35f4b3 GS |
5788 | entry; |
5789 | entry = HeNEXT(entry)) | |
5790 | { | |
1edc1566 | 5791 | if (!todo[(U8)*HeKEY(entry)]) |
463ee0b2 | 5792 | continue; |
1edc1566 | 5793 | gv = (GV*)HeVAL(entry); |
79072805 | 5794 | sv = GvSV(gv); |
9e35f4b3 GS |
5795 | if (SvTHINKFIRST(sv)) { |
5796 | if (!SvREADONLY(sv) && SvROK(sv)) | |
5797 | sv_unref(sv); | |
5798 | continue; | |
5799 | } | |
a0d0e21e | 5800 | (void)SvOK_off(sv); |
79072805 LW |
5801 | if (SvTYPE(sv) >= SVt_PV) { |
5802 | SvCUR_set(sv, 0); | |
463ee0b2 LW |
5803 | if (SvPVX(sv) != Nullch) |
5804 | *SvPVX(sv) = '\0'; | |
44a8e56a | 5805 | SvTAINT(sv); |
79072805 LW |
5806 | } |
5807 | if (GvAV(gv)) { | |
5808 | av_clear(GvAV(gv)); | |
5809 | } | |
44a8e56a | 5810 | if (GvHV(gv) && !HvNAME(GvHV(gv))) { |
463ee0b2 | 5811 | hv_clear(GvHV(gv)); |
fa6a1c44 | 5812 | #ifdef USE_ENVIRON_ARRAY |
3280af22 | 5813 | if (gv == PL_envgv) |
79072805 | 5814 | environ[0] = Nullch; |
a0d0e21e | 5815 | #endif |
79072805 LW |
5816 | } |
5817 | } | |
5818 | } | |
5819 | } | |
5820 | } | |
5821 | ||
46fc3d4c | 5822 | IO* |
864dbfa3 | 5823 | Perl_sv_2io(pTHX_ SV *sv) |
46fc3d4c | 5824 | { |
5825 | IO* io; | |
5826 | GV* gv; | |
2d8e6c8d | 5827 | STRLEN n_a; |
46fc3d4c | 5828 | |
5829 | switch (SvTYPE(sv)) { | |
5830 | case SVt_PVIO: | |
5831 | io = (IO*)sv; | |
5832 | break; | |
5833 | case SVt_PVGV: | |
5834 | gv = (GV*)sv; | |
5835 | io = GvIO(gv); | |
5836 | if (!io) | |
cea2e8a9 | 5837 | Perl_croak(aTHX_ "Bad filehandle: %s", GvNAME(gv)); |
46fc3d4c | 5838 | break; |
5839 | default: | |
5840 | if (!SvOK(sv)) | |
cea2e8a9 | 5841 | Perl_croak(aTHX_ PL_no_usym, "filehandle"); |
46fc3d4c | 5842 | if (SvROK(sv)) |
5843 | return sv_2io(SvRV(sv)); | |
2d8e6c8d | 5844 | gv = gv_fetchpv(SvPV(sv,n_a), FALSE, SVt_PVIO); |
46fc3d4c | 5845 | if (gv) |
5846 | io = GvIO(gv); | |
5847 | else | |
5848 | io = 0; | |
5849 | if (!io) | |
cea2e8a9 | 5850 | Perl_croak(aTHX_ "Bad filehandle: %s", SvPV(sv,n_a)); |
46fc3d4c | 5851 | break; |
5852 | } | |
5853 | return io; | |
5854 | } | |
5855 | ||
79072805 | 5856 | CV * |
864dbfa3 | 5857 | Perl_sv_2cv(pTHX_ SV *sv, HV **st, GV **gvp, I32 lref) |
79072805 LW |
5858 | { |
5859 | GV *gv; | |
5860 | CV *cv; | |
2d8e6c8d | 5861 | STRLEN n_a; |
79072805 LW |
5862 | |
5863 | if (!sv) | |
93a17b20 | 5864 | return *gvp = Nullgv, Nullcv; |
79072805 | 5865 | switch (SvTYPE(sv)) { |
79072805 LW |
5866 | case SVt_PVCV: |
5867 | *st = CvSTASH(sv); | |
5868 | *gvp = Nullgv; | |
5869 | return (CV*)sv; | |
5870 | case SVt_PVHV: | |
5871 | case SVt_PVAV: | |
5872 | *gvp = Nullgv; | |
5873 | return Nullcv; | |
8990e307 LW |
5874 | case SVt_PVGV: |
5875 | gv = (GV*)sv; | |
a0d0e21e | 5876 | *gvp = gv; |
8990e307 LW |
5877 | *st = GvESTASH(gv); |
5878 | goto fix_gv; | |
5879 | ||
79072805 | 5880 | default: |
a0d0e21e LW |
5881 | if (SvGMAGICAL(sv)) |
5882 | mg_get(sv); | |
5883 | if (SvROK(sv)) { | |
f5284f61 IZ |
5884 | SV **sp = &sv; /* Used in tryAMAGICunDEREF macro. */ |
5885 | tryAMAGICunDEREF(to_cv); | |
5886 | ||
62f274bf GS |
5887 | sv = SvRV(sv); |
5888 | if (SvTYPE(sv) == SVt_PVCV) { | |
5889 | cv = (CV*)sv; | |
5890 | *gvp = Nullgv; | |
5891 | *st = CvSTASH(cv); | |
5892 | return cv; | |
5893 | } | |
5894 | else if(isGV(sv)) | |
5895 | gv = (GV*)sv; | |
5896 | else | |
cea2e8a9 | 5897 | Perl_croak(aTHX_ "Not a subroutine reference"); |
a0d0e21e | 5898 | } |
62f274bf | 5899 | else if (isGV(sv)) |
79072805 LW |
5900 | gv = (GV*)sv; |
5901 | else | |
2d8e6c8d | 5902 | gv = gv_fetchpv(SvPV(sv, n_a), lref, SVt_PVCV); |
79072805 LW |
5903 | *gvp = gv; |
5904 | if (!gv) | |
5905 | return Nullcv; | |
5906 | *st = GvESTASH(gv); | |
8990e307 | 5907 | fix_gv: |
8ebc5c01 | 5908 | if (lref && !GvCVu(gv)) { |
4633a7c4 | 5909 | SV *tmpsv; |
748a9306 | 5910 | ENTER; |
4633a7c4 | 5911 | tmpsv = NEWSV(704,0); |
16660edb | 5912 | gv_efullname3(tmpsv, gv, Nullch); |
f6ec51f7 GS |
5913 | /* XXX this is probably not what they think they're getting. |
5914 | * It has the same effect as "sub name;", i.e. just a forward | |
5915 | * declaration! */ | |
774d564b | 5916 | newSUB(start_subparse(FALSE, 0), |
4633a7c4 LW |
5917 | newSVOP(OP_CONST, 0, tmpsv), |
5918 | Nullop, | |
8990e307 | 5919 | Nullop); |
748a9306 | 5920 | LEAVE; |
8ebc5c01 | 5921 | if (!GvCVu(gv)) |
cea2e8a9 | 5922 | Perl_croak(aTHX_ "Unable to create sub named \"%s\"", SvPV(sv,n_a)); |
8990e307 | 5923 | } |
8ebc5c01 | 5924 | return GvCVu(gv); |
79072805 LW |
5925 | } |
5926 | } | |
5927 | ||
c461cf8f JH |
5928 | /* |
5929 | =for apidoc sv_true | |
5930 | ||
5931 | Returns true if the SV has a true value by Perl's rules. | |
5932 | ||
5933 | =cut | |
5934 | */ | |
5935 | ||
79072805 | 5936 | I32 |
864dbfa3 | 5937 | Perl_sv_true(pTHX_ register SV *sv) |
79072805 | 5938 | { |
8990e307 LW |
5939 | if (!sv) |
5940 | return 0; | |
79072805 | 5941 | if (SvPOK(sv)) { |
4e35701f NIS |
5942 | register XPV* tXpv; |
5943 | if ((tXpv = (XPV*)SvANY(sv)) && | |
c2f1de04 | 5944 | (tXpv->xpv_cur > 1 || |
4e35701f | 5945 | (tXpv->xpv_cur && *tXpv->xpv_pv != '0'))) |
79072805 LW |
5946 | return 1; |
5947 | else | |
5948 | return 0; | |
5949 | } | |
5950 | else { | |
5951 | if (SvIOK(sv)) | |
463ee0b2 | 5952 | return SvIVX(sv) != 0; |
79072805 LW |
5953 | else { |
5954 | if (SvNOK(sv)) | |
463ee0b2 | 5955 | return SvNVX(sv) != 0.0; |
79072805 | 5956 | else |
463ee0b2 | 5957 | return sv_2bool(sv); |
79072805 LW |
5958 | } |
5959 | } | |
5960 | } | |
79072805 | 5961 | |
ff68c719 | 5962 | IV |
864dbfa3 | 5963 | Perl_sv_iv(pTHX_ register SV *sv) |
85e6fe83 | 5964 | { |
25da4f38 IZ |
5965 | if (SvIOK(sv)) { |
5966 | if (SvIsUV(sv)) | |
5967 | return (IV)SvUVX(sv); | |
ff68c719 | 5968 | return SvIVX(sv); |
25da4f38 | 5969 | } |
ff68c719 | 5970 | return sv_2iv(sv); |
85e6fe83 | 5971 | } |
85e6fe83 | 5972 | |
ff68c719 | 5973 | UV |
864dbfa3 | 5974 | Perl_sv_uv(pTHX_ register SV *sv) |
ff68c719 | 5975 | { |
25da4f38 IZ |
5976 | if (SvIOK(sv)) { |
5977 | if (SvIsUV(sv)) | |
5978 | return SvUVX(sv); | |
5979 | return (UV)SvIVX(sv); | |
5980 | } | |
ff68c719 | 5981 | return sv_2uv(sv); |
5982 | } | |
85e6fe83 | 5983 | |
65202027 | 5984 | NV |
864dbfa3 | 5985 | Perl_sv_nv(pTHX_ register SV *sv) |
79072805 | 5986 | { |
ff68c719 | 5987 | if (SvNOK(sv)) |
5988 | return SvNVX(sv); | |
5989 | return sv_2nv(sv); | |
79072805 | 5990 | } |
79072805 | 5991 | |
79072805 | 5992 | char * |
864dbfa3 | 5993 | Perl_sv_pv(pTHX_ SV *sv) |
1fa8b10d JD |
5994 | { |
5995 | STRLEN n_a; | |
5996 | ||
5997 | if (SvPOK(sv)) | |
5998 | return SvPVX(sv); | |
5999 | ||
6000 | return sv_2pv(sv, &n_a); | |
6001 | } | |
6002 | ||
6003 | char * | |
864dbfa3 | 6004 | Perl_sv_pvn(pTHX_ SV *sv, STRLEN *lp) |
79072805 | 6005 | { |
85e6fe83 LW |
6006 | if (SvPOK(sv)) { |
6007 | *lp = SvCUR(sv); | |
a0d0e21e | 6008 | return SvPVX(sv); |
85e6fe83 | 6009 | } |
463ee0b2 | 6010 | return sv_2pv(sv, lp); |
79072805 | 6011 | } |
79072805 | 6012 | |
c461cf8f JH |
6013 | /* |
6014 | =for apidoc sv_pvn_force | |
6015 | ||
6016 | Get a sensible string out of the SV somehow. | |
6017 | ||
6018 | =cut | |
6019 | */ | |
6020 | ||
a0d0e21e | 6021 | char * |
864dbfa3 | 6022 | Perl_sv_pvn_force(pTHX_ SV *sv, STRLEN *lp) |
a0d0e21e LW |
6023 | { |
6024 | char *s; | |
6025 | ||
6fc92669 GS |
6026 | if (SvTHINKFIRST(sv) && !SvROK(sv)) |
6027 | sv_force_normal(sv); | |
1c846c1f | 6028 | |
a0d0e21e LW |
6029 | if (SvPOK(sv)) { |
6030 | *lp = SvCUR(sv); | |
6031 | } | |
6032 | else { | |
748a9306 | 6033 | if (SvTYPE(sv) > SVt_PVLV && SvTYPE(sv) != SVt_PVFM) { |
cea2e8a9 | 6034 | Perl_croak(aTHX_ "Can't coerce %s to string in %s", sv_reftype(sv,0), |
6fc92669 | 6035 | PL_op_name[PL_op->op_type]); |
a0d0e21e | 6036 | } |
4633a7c4 LW |
6037 | else |
6038 | s = sv_2pv(sv, lp); | |
a0d0e21e LW |
6039 | if (s != SvPVX(sv)) { /* Almost, but not quite, sv_setpvn() */ |
6040 | STRLEN len = *lp; | |
1c846c1f | 6041 | |
a0d0e21e LW |
6042 | if (SvROK(sv)) |
6043 | sv_unref(sv); | |
6044 | (void)SvUPGRADE(sv, SVt_PV); /* Never FALSE */ | |
6045 | SvGROW(sv, len + 1); | |
6046 | Move(s,SvPVX(sv),len,char); | |
6047 | SvCUR_set(sv, len); | |
6048 | *SvEND(sv) = '\0'; | |
6049 | } | |
6050 | if (!SvPOK(sv)) { | |
6051 | SvPOK_on(sv); /* validate pointer */ | |
6052 | SvTAINT(sv); | |
1d7c1841 GS |
6053 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n", |
6054 | PTR2UV(sv),SvPVX(sv))); | |
a0d0e21e LW |
6055 | } |
6056 | } | |
6057 | return SvPVX(sv); | |
6058 | } | |
6059 | ||
6060 | char * | |
7340a771 GS |
6061 | Perl_sv_pvbyte(pTHX_ SV *sv) |
6062 | { | |
6063 | return sv_pv(sv); | |
6064 | } | |
6065 | ||
6066 | char * | |
6067 | Perl_sv_pvbyten(pTHX_ SV *sv, STRLEN *lp) | |
6068 | { | |
6069 | return sv_pvn(sv,lp); | |
6070 | } | |
6071 | ||
6072 | char * | |
6073 | Perl_sv_pvbyten_force(pTHX_ SV *sv, STRLEN *lp) | |
6074 | { | |
6075 | return sv_pvn_force(sv,lp); | |
6076 | } | |
6077 | ||
6078 | char * | |
6079 | Perl_sv_pvutf8(pTHX_ SV *sv) | |
6080 | { | |
560a288e | 6081 | sv_utf8_upgrade(sv); |
7340a771 GS |
6082 | return sv_pv(sv); |
6083 | } | |
6084 | ||
6085 | char * | |
6086 | Perl_sv_pvutf8n(pTHX_ SV *sv, STRLEN *lp) | |
6087 | { | |
560a288e | 6088 | sv_utf8_upgrade(sv); |
7340a771 GS |
6089 | return sv_pvn(sv,lp); |
6090 | } | |
6091 | ||
c461cf8f JH |
6092 | /* |
6093 | =for apidoc sv_pvutf8n_force | |
6094 | ||
6095 | Get a sensible UTF8-encoded string out of the SV somehow. See | |
6096 | L</sv_pvn_force>. | |
6097 | ||
6098 | =cut | |
6099 | */ | |
6100 | ||
7340a771 GS |
6101 | char * |
6102 | Perl_sv_pvutf8n_force(pTHX_ SV *sv, STRLEN *lp) | |
6103 | { | |
560a288e | 6104 | sv_utf8_upgrade(sv); |
7340a771 GS |
6105 | return sv_pvn_force(sv,lp); |
6106 | } | |
6107 | ||
c461cf8f JH |
6108 | /* |
6109 | =for apidoc sv_reftype | |
6110 | ||
6111 | Returns a string describing what the SV is a reference to. | |
6112 | ||
6113 | =cut | |
6114 | */ | |
6115 | ||
7340a771 | 6116 | char * |
864dbfa3 | 6117 | Perl_sv_reftype(pTHX_ SV *sv, int ob) |
a0d0e21e LW |
6118 | { |
6119 | if (ob && SvOBJECT(sv)) | |
6120 | return HvNAME(SvSTASH(sv)); | |
6121 | else { | |
6122 | switch (SvTYPE(sv)) { | |
6123 | case SVt_NULL: | |
6124 | case SVt_IV: | |
6125 | case SVt_NV: | |
6126 | case SVt_RV: | |
6127 | case SVt_PV: | |
6128 | case SVt_PVIV: | |
6129 | case SVt_PVNV: | |
6130 | case SVt_PVMG: | |
6131 | case SVt_PVBM: | |
6132 | if (SvROK(sv)) | |
6133 | return "REF"; | |
6134 | else | |
6135 | return "SCALAR"; | |
6136 | case SVt_PVLV: return "LVALUE"; | |
6137 | case SVt_PVAV: return "ARRAY"; | |
6138 | case SVt_PVHV: return "HASH"; | |
6139 | case SVt_PVCV: return "CODE"; | |
6140 | case SVt_PVGV: return "GLOB"; | |
1d2dff63 | 6141 | case SVt_PVFM: return "FORMAT"; |
27f9d8f3 | 6142 | case SVt_PVIO: return "IO"; |
a0d0e21e LW |
6143 | default: return "UNKNOWN"; |
6144 | } | |
6145 | } | |
6146 | } | |
6147 | ||
954c1994 GS |
6148 | /* |
6149 | =for apidoc sv_isobject | |
6150 | ||
6151 | Returns a boolean indicating whether the SV is an RV pointing to a blessed | |
6152 | object. If the SV is not an RV, or if the object is not blessed, then this | |
6153 | will return false. | |
6154 | ||
6155 | =cut | |
6156 | */ | |
6157 | ||
463ee0b2 | 6158 | int |
864dbfa3 | 6159 | Perl_sv_isobject(pTHX_ SV *sv) |
85e6fe83 | 6160 | { |
68dc0745 | 6161 | if (!sv) |
6162 | return 0; | |
6163 | if (SvGMAGICAL(sv)) | |
6164 | mg_get(sv); | |
85e6fe83 LW |
6165 | if (!SvROK(sv)) |
6166 | return 0; | |
6167 | sv = (SV*)SvRV(sv); | |
6168 | if (!SvOBJECT(sv)) | |
6169 | return 0; | |
6170 | return 1; | |
6171 | } | |
6172 | ||
954c1994 GS |
6173 | /* |
6174 | =for apidoc sv_isa | |
6175 | ||
6176 | Returns a boolean indicating whether the SV is blessed into the specified | |
6177 | class. This does not check for subtypes; use C<sv_derived_from> to verify | |
6178 | an inheritance relationship. | |
6179 | ||
6180 | =cut | |
6181 | */ | |
6182 | ||
85e6fe83 | 6183 | int |
864dbfa3 | 6184 | Perl_sv_isa(pTHX_ SV *sv, const char *name) |
463ee0b2 | 6185 | { |
68dc0745 | 6186 | if (!sv) |
6187 | return 0; | |
6188 | if (SvGMAGICAL(sv)) | |
6189 | mg_get(sv); | |
ed6116ce | 6190 | if (!SvROK(sv)) |
463ee0b2 | 6191 | return 0; |
ed6116ce LW |
6192 | sv = (SV*)SvRV(sv); |
6193 | if (!SvOBJECT(sv)) | |
463ee0b2 LW |
6194 | return 0; |
6195 | ||
6196 | return strEQ(HvNAME(SvSTASH(sv)), name); | |
6197 | } | |
6198 | ||
954c1994 GS |
6199 | /* |
6200 | =for apidoc newSVrv | |
6201 | ||
6202 | Creates a new SV for the RV, C<rv>, to point to. If C<rv> is not an RV then | |
6203 | it will be upgraded to one. If C<classname> is non-null then the new SV will | |
6204 | be blessed in the specified package. The new SV is returned and its | |
6205 | reference count is 1. | |
6206 | ||
6207 | =cut | |
6208 | */ | |
6209 | ||
463ee0b2 | 6210 | SV* |
864dbfa3 | 6211 | Perl_newSVrv(pTHX_ SV *rv, const char *classname) |
463ee0b2 | 6212 | { |
463ee0b2 LW |
6213 | SV *sv; |
6214 | ||
4561caa4 | 6215 | new_SV(sv); |
51cf62d8 | 6216 | |
2213622d | 6217 | SV_CHECK_THINKFIRST(rv); |
51cf62d8 | 6218 | SvAMAGIC_off(rv); |
51cf62d8 | 6219 | |
0199fce9 JD |
6220 | if (SvTYPE(rv) >= SVt_PVMG) { |
6221 | U32 refcnt = SvREFCNT(rv); | |
6222 | SvREFCNT(rv) = 0; | |
6223 | sv_clear(rv); | |
6224 | SvFLAGS(rv) = 0; | |
6225 | SvREFCNT(rv) = refcnt; | |
6226 | } | |
6227 | ||
51cf62d8 | 6228 | if (SvTYPE(rv) < SVt_RV) |
0199fce9 JD |
6229 | sv_upgrade(rv, SVt_RV); |
6230 | else if (SvTYPE(rv) > SVt_RV) { | |
6231 | (void)SvOOK_off(rv); | |
6232 | if (SvPVX(rv) && SvLEN(rv)) | |
6233 | Safefree(SvPVX(rv)); | |
6234 | SvCUR_set(rv, 0); | |
6235 | SvLEN_set(rv, 0); | |
6236 | } | |
51cf62d8 OT |
6237 | |
6238 | (void)SvOK_off(rv); | |
053fc874 | 6239 | SvRV(rv) = sv; |
ed6116ce | 6240 | SvROK_on(rv); |
463ee0b2 | 6241 | |
a0d0e21e LW |
6242 | if (classname) { |
6243 | HV* stash = gv_stashpv(classname, TRUE); | |
6244 | (void)sv_bless(rv, stash); | |
6245 | } | |
6246 | return sv; | |
6247 | } | |
6248 | ||
954c1994 GS |
6249 | /* |
6250 | =for apidoc sv_setref_pv | |
6251 | ||
6252 | Copies a pointer into a new SV, optionally blessing the SV. The C<rv> | |
6253 | argument will be upgraded to an RV. That RV will be modified to point to | |
6254 | the new SV. If the C<pv> argument is NULL then C<PL_sv_undef> will be placed | |
6255 | into the SV. The C<classname> argument indicates the package for the | |
6256 | blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV | |
6257 | will be returned and will have a reference count of 1. | |
6258 | ||
6259 | Do not use with other Perl types such as HV, AV, SV, CV, because those | |
6260 | objects will become corrupted by the pointer copy process. | |
6261 | ||
6262 | Note that C<sv_setref_pvn> copies the string while this copies the pointer. | |
6263 | ||
6264 | =cut | |
6265 | */ | |
6266 | ||
a0d0e21e | 6267 | SV* |
864dbfa3 | 6268 | Perl_sv_setref_pv(pTHX_ SV *rv, const char *classname, void *pv) |
a0d0e21e | 6269 | { |
189b2af5 | 6270 | if (!pv) { |
3280af22 | 6271 | sv_setsv(rv, &PL_sv_undef); |
189b2af5 GS |
6272 | SvSETMAGIC(rv); |
6273 | } | |
a0d0e21e | 6274 | else |
56431972 | 6275 | sv_setiv(newSVrv(rv,classname), PTR2IV(pv)); |
a0d0e21e LW |
6276 | return rv; |
6277 | } | |
6278 | ||
954c1994 GS |
6279 | /* |
6280 | =for apidoc sv_setref_iv | |
6281 | ||
6282 | Copies an integer into a new SV, optionally blessing the SV. The C<rv> | |
6283 | argument will be upgraded to an RV. That RV will be modified to point to | |
6284 | the new SV. The C<classname> argument indicates the package for the | |
6285 | blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV | |
6286 | will be returned and will have a reference count of 1. | |
6287 | ||
6288 | =cut | |
6289 | */ | |
6290 | ||
a0d0e21e | 6291 | SV* |
864dbfa3 | 6292 | Perl_sv_setref_iv(pTHX_ SV *rv, const char *classname, IV iv) |
a0d0e21e LW |
6293 | { |
6294 | sv_setiv(newSVrv(rv,classname), iv); | |
6295 | return rv; | |
6296 | } | |
6297 | ||
954c1994 GS |
6298 | /* |
6299 | =for apidoc sv_setref_nv | |
6300 | ||
6301 | Copies a double into a new SV, optionally blessing the SV. The C<rv> | |
6302 | argument will be upgraded to an RV. That RV will be modified to point to | |
6303 | the new SV. The C<classname> argument indicates the package for the | |
6304 | blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV | |
6305 | will be returned and will have a reference count of 1. | |
6306 | ||
6307 | =cut | |
6308 | */ | |
6309 | ||
a0d0e21e | 6310 | SV* |
65202027 | 6311 | Perl_sv_setref_nv(pTHX_ SV *rv, const char *classname, NV nv) |
a0d0e21e LW |
6312 | { |
6313 | sv_setnv(newSVrv(rv,classname), nv); | |
6314 | return rv; | |
6315 | } | |
463ee0b2 | 6316 | |
954c1994 GS |
6317 | /* |
6318 | =for apidoc sv_setref_pvn | |
6319 | ||
6320 | Copies a string into a new SV, optionally blessing the SV. The length of the | |
6321 | string must be specified with C<n>. The C<rv> argument will be upgraded to | |
6322 | an RV. That RV will be modified to point to the new SV. The C<classname> | |
6323 | argument indicates the package for the blessing. Set C<classname> to | |
6324 | C<Nullch> to avoid the blessing. The new SV will be returned and will have | |
6325 | a reference count of 1. | |
6326 | ||
6327 | Note that C<sv_setref_pv> copies the pointer while this copies the string. | |
6328 | ||
6329 | =cut | |
6330 | */ | |
6331 | ||
a0d0e21e | 6332 | SV* |
864dbfa3 | 6333 | Perl_sv_setref_pvn(pTHX_ SV *rv, const char *classname, char *pv, STRLEN n) |
a0d0e21e LW |
6334 | { |
6335 | sv_setpvn(newSVrv(rv,classname), pv, n); | |
463ee0b2 LW |
6336 | return rv; |
6337 | } | |
6338 | ||
954c1994 GS |
6339 | /* |
6340 | =for apidoc sv_bless | |
6341 | ||
6342 | Blesses an SV into a specified package. The SV must be an RV. The package | |
6343 | must be designated by its stash (see C<gv_stashpv()>). The reference count | |
6344 | of the SV is unaffected. | |
6345 | ||
6346 | =cut | |
6347 | */ | |
6348 | ||
a0d0e21e | 6349 | SV* |
864dbfa3 | 6350 | Perl_sv_bless(pTHX_ SV *sv, HV *stash) |
a0d0e21e | 6351 | { |
76e3520e | 6352 | SV *tmpRef; |
a0d0e21e | 6353 | if (!SvROK(sv)) |
cea2e8a9 | 6354 | Perl_croak(aTHX_ "Can't bless non-reference value"); |
76e3520e GS |
6355 | tmpRef = SvRV(sv); |
6356 | if (SvFLAGS(tmpRef) & (SVs_OBJECT|SVf_READONLY)) { | |
6357 | if (SvREADONLY(tmpRef)) | |
cea2e8a9 | 6358 | Perl_croak(aTHX_ PL_no_modify); |
76e3520e GS |
6359 | if (SvOBJECT(tmpRef)) { |
6360 | if (SvTYPE(tmpRef) != SVt_PVIO) | |
3280af22 | 6361 | --PL_sv_objcount; |
76e3520e | 6362 | SvREFCNT_dec(SvSTASH(tmpRef)); |
2e3febc6 | 6363 | } |
a0d0e21e | 6364 | } |
76e3520e GS |
6365 | SvOBJECT_on(tmpRef); |
6366 | if (SvTYPE(tmpRef) != SVt_PVIO) | |
3280af22 | 6367 | ++PL_sv_objcount; |
76e3520e GS |
6368 | (void)SvUPGRADE(tmpRef, SVt_PVMG); |
6369 | SvSTASH(tmpRef) = (HV*)SvREFCNT_inc(stash); | |
a0d0e21e | 6370 | |
2e3febc6 CS |
6371 | if (Gv_AMG(stash)) |
6372 | SvAMAGIC_on(sv); | |
6373 | else | |
6374 | SvAMAGIC_off(sv); | |
a0d0e21e LW |
6375 | |
6376 | return sv; | |
6377 | } | |
6378 | ||
76e3520e | 6379 | STATIC void |
cea2e8a9 | 6380 | S_sv_unglob(pTHX_ SV *sv) |
a0d0e21e | 6381 | { |
850fabdf GS |
6382 | void *xpvmg; |
6383 | ||
a0d0e21e LW |
6384 | assert(SvTYPE(sv) == SVt_PVGV); |
6385 | SvFAKE_off(sv); | |
6386 | if (GvGP(sv)) | |
1edc1566 | 6387 | gp_free((GV*)sv); |
e826b3c7 GS |
6388 | if (GvSTASH(sv)) { |
6389 | SvREFCNT_dec(GvSTASH(sv)); | |
6390 | GvSTASH(sv) = Nullhv; | |
6391 | } | |
a0d0e21e LW |
6392 | sv_unmagic(sv, '*'); |
6393 | Safefree(GvNAME(sv)); | |
a5f75d66 | 6394 | GvMULTI_off(sv); |
850fabdf GS |
6395 | |
6396 | /* need to keep SvANY(sv) in the right arena */ | |
6397 | xpvmg = new_XPVMG(); | |
6398 | StructCopy(SvANY(sv), xpvmg, XPVMG); | |
6399 | del_XPVGV(SvANY(sv)); | |
6400 | SvANY(sv) = xpvmg; | |
6401 | ||
a0d0e21e LW |
6402 | SvFLAGS(sv) &= ~SVTYPEMASK; |
6403 | SvFLAGS(sv) |= SVt_PVMG; | |
6404 | } | |
6405 | ||
954c1994 | 6406 | /* |
840a7b70 | 6407 | =for apidoc sv_unref_flags |
954c1994 GS |
6408 | |
6409 | Unsets the RV status of the SV, and decrements the reference count of | |
6410 | whatever was being referenced by the RV. This can almost be thought of | |
840a7b70 IZ |
6411 | as a reversal of C<newSVrv>. The C<cflags> argument can contain |
6412 | C<SV_IMMEDIATE_UNREF> to force the reference count to be decremented | |
6413 | (otherwise the decrementing is conditional on the reference count being | |
6414 | different from one or the reference being a readonly SV). | |
7889fe52 | 6415 | See C<SvROK_off>. |
954c1994 GS |
6416 | |
6417 | =cut | |
6418 | */ | |
6419 | ||
ed6116ce | 6420 | void |
840a7b70 | 6421 | Perl_sv_unref_flags(pTHX_ SV *sv, U32 flags) |
ed6116ce | 6422 | { |
a0d0e21e | 6423 | SV* rv = SvRV(sv); |
810b8aa5 GS |
6424 | |
6425 | if (SvWEAKREF(sv)) { | |
6426 | sv_del_backref(sv); | |
6427 | SvWEAKREF_off(sv); | |
6428 | SvRV(sv) = 0; | |
6429 | return; | |
6430 | } | |
ed6116ce LW |
6431 | SvRV(sv) = 0; |
6432 | SvROK_off(sv); | |
840a7b70 | 6433 | if (SvREFCNT(rv) != 1 || SvREADONLY(rv) || flags) /* SV_IMMEDIATE_UNREF */ |
4633a7c4 | 6434 | SvREFCNT_dec(rv); |
840a7b70 | 6435 | else /* XXX Hack, but hard to make $a=$a->[1] work otherwise */ |
4633a7c4 | 6436 | sv_2mortal(rv); /* Schedule for freeing later */ |
ed6116ce | 6437 | } |
8990e307 | 6438 | |
840a7b70 IZ |
6439 | /* |
6440 | =for apidoc sv_unref | |
6441 | ||
6442 | Unsets the RV status of the SV, and decrements the reference count of | |
6443 | whatever was being referenced by the RV. This can almost be thought of | |
6444 | as a reversal of C<newSVrv>. This is C<sv_unref_flags> with the C<flag> | |
7889fe52 | 6445 | being zero. See C<SvROK_off>. |
840a7b70 IZ |
6446 | |
6447 | =cut | |
6448 | */ | |
6449 | ||
6450 | void | |
6451 | Perl_sv_unref(pTHX_ SV *sv) | |
6452 | { | |
6453 | sv_unref_flags(sv, 0); | |
6454 | } | |
6455 | ||
bbce6d69 | 6456 | void |
864dbfa3 | 6457 | Perl_sv_taint(pTHX_ SV *sv) |
bbce6d69 | 6458 | { |
6459 | sv_magic((sv), Nullsv, 't', Nullch, 0); | |
6460 | } | |
6461 | ||
6462 | void | |
864dbfa3 | 6463 | Perl_sv_untaint(pTHX_ SV *sv) |
bbce6d69 | 6464 | { |
13f57bf8 | 6465 | if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) { |
36477c24 | 6466 | MAGIC *mg = mg_find(sv, 't'); |
6467 | if (mg) | |
565764a8 | 6468 | mg->mg_len &= ~1; |
36477c24 | 6469 | } |
bbce6d69 | 6470 | } |
6471 | ||
6472 | bool | |
864dbfa3 | 6473 | Perl_sv_tainted(pTHX_ SV *sv) |
bbce6d69 | 6474 | { |
13f57bf8 | 6475 | if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) { |
36477c24 | 6476 | MAGIC *mg = mg_find(sv, 't'); |
155aba94 | 6477 | if (mg && ((mg->mg_len & 1) || ((mg->mg_len & 2) && mg->mg_obj == sv))) |
36477c24 | 6478 | return TRUE; |
6479 | } | |
6480 | return FALSE; | |
bbce6d69 | 6481 | } |
6482 | ||
954c1994 GS |
6483 | /* |
6484 | =for apidoc sv_setpviv | |
6485 | ||
6486 | Copies an integer into the given SV, also updating its string value. | |
6487 | Does not handle 'set' magic. See C<sv_setpviv_mg>. | |
6488 | ||
6489 | =cut | |
6490 | */ | |
6491 | ||
84902520 | 6492 | void |
864dbfa3 | 6493 | Perl_sv_setpviv(pTHX_ SV *sv, IV iv) |
84902520 | 6494 | { |
25da4f38 IZ |
6495 | char buf[TYPE_CHARS(UV)]; |
6496 | char *ebuf; | |
6497 | char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf); | |
84902520 | 6498 | |
25da4f38 | 6499 | sv_setpvn(sv, ptr, ebuf - ptr); |
84902520 TB |
6500 | } |
6501 | ||
ef50df4b | 6502 | |
954c1994 GS |
6503 | /* |
6504 | =for apidoc sv_setpviv_mg | |
6505 | ||
6506 | Like C<sv_setpviv>, but also handles 'set' magic. | |
6507 | ||
6508 | =cut | |
6509 | */ | |
6510 | ||
ef50df4b | 6511 | void |
864dbfa3 | 6512 | Perl_sv_setpviv_mg(pTHX_ SV *sv, IV iv) |
ef50df4b | 6513 | { |
25da4f38 IZ |
6514 | char buf[TYPE_CHARS(UV)]; |
6515 | char *ebuf; | |
6516 | char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf); | |
6517 | ||
6518 | sv_setpvn(sv, ptr, ebuf - ptr); | |
ef50df4b GS |
6519 | SvSETMAGIC(sv); |
6520 | } | |
6521 | ||
cea2e8a9 GS |
6522 | #if defined(PERL_IMPLICIT_CONTEXT) |
6523 | void | |
6524 | Perl_sv_setpvf_nocontext(SV *sv, const char* pat, ...) | |
6525 | { | |
6526 | dTHX; | |
6527 | va_list args; | |
6528 | va_start(args, pat); | |
c5be433b | 6529 | sv_vsetpvf(sv, pat, &args); |
cea2e8a9 GS |
6530 | va_end(args); |
6531 | } | |
6532 | ||
6533 | ||
6534 | void | |
6535 | Perl_sv_setpvf_mg_nocontext(SV *sv, const char* pat, ...) | |
6536 | { | |
6537 | dTHX; | |
6538 | va_list args; | |
6539 | va_start(args, pat); | |
c5be433b | 6540 | sv_vsetpvf_mg(sv, pat, &args); |
cea2e8a9 | 6541 | va_end(args); |
cea2e8a9 GS |
6542 | } |
6543 | #endif | |
6544 | ||
954c1994 GS |
6545 | /* |
6546 | =for apidoc sv_setpvf | |
6547 | ||
6548 | Processes its arguments like C<sprintf> and sets an SV to the formatted | |
6549 | output. Does not handle 'set' magic. See C<sv_setpvf_mg>. | |
6550 | ||
6551 | =cut | |
6552 | */ | |
6553 | ||
46fc3d4c | 6554 | void |
864dbfa3 | 6555 | Perl_sv_setpvf(pTHX_ SV *sv, const char* pat, ...) |
46fc3d4c | 6556 | { |
6557 | va_list args; | |
46fc3d4c | 6558 | va_start(args, pat); |
c5be433b | 6559 | sv_vsetpvf(sv, pat, &args); |
46fc3d4c | 6560 | va_end(args); |
6561 | } | |
6562 | ||
c5be433b GS |
6563 | void |
6564 | Perl_sv_vsetpvf(pTHX_ SV *sv, const char* pat, va_list* args) | |
6565 | { | |
6566 | sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
6567 | } | |
ef50df4b | 6568 | |
954c1994 GS |
6569 | /* |
6570 | =for apidoc sv_setpvf_mg | |
6571 | ||
6572 | Like C<sv_setpvf>, but also handles 'set' magic. | |
6573 | ||
6574 | =cut | |
6575 | */ | |
6576 | ||
ef50df4b | 6577 | void |
864dbfa3 | 6578 | Perl_sv_setpvf_mg(pTHX_ SV *sv, const char* pat, ...) |
ef50df4b GS |
6579 | { |
6580 | va_list args; | |
ef50df4b | 6581 | va_start(args, pat); |
c5be433b | 6582 | sv_vsetpvf_mg(sv, pat, &args); |
ef50df4b | 6583 | va_end(args); |
c5be433b GS |
6584 | } |
6585 | ||
6586 | void | |
6587 | Perl_sv_vsetpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args) | |
6588 | { | |
6589 | sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
ef50df4b GS |
6590 | SvSETMAGIC(sv); |
6591 | } | |
6592 | ||
cea2e8a9 GS |
6593 | #if defined(PERL_IMPLICIT_CONTEXT) |
6594 | void | |
6595 | Perl_sv_catpvf_nocontext(SV *sv, const char* pat, ...) | |
6596 | { | |
6597 | dTHX; | |
6598 | va_list args; | |
6599 | va_start(args, pat); | |
c5be433b | 6600 | sv_vcatpvf(sv, pat, &args); |
cea2e8a9 GS |
6601 | va_end(args); |
6602 | } | |
6603 | ||
6604 | void | |
6605 | Perl_sv_catpvf_mg_nocontext(SV *sv, const char* pat, ...) | |
6606 | { | |
6607 | dTHX; | |
6608 | va_list args; | |
6609 | va_start(args, pat); | |
c5be433b | 6610 | sv_vcatpvf_mg(sv, pat, &args); |
cea2e8a9 | 6611 | va_end(args); |
cea2e8a9 GS |
6612 | } |
6613 | #endif | |
6614 | ||
954c1994 GS |
6615 | /* |
6616 | =for apidoc sv_catpvf | |
6617 | ||
6618 | Processes its arguments like C<sprintf> and appends the formatted output | |
6619 | to an SV. Handles 'get' magic, but not 'set' magic. C<SvSETMAGIC()> must | |
6620 | typically be called after calling this function to handle 'set' magic. | |
6621 | ||
6622 | =cut | |
6623 | */ | |
6624 | ||
46fc3d4c | 6625 | void |
864dbfa3 | 6626 | Perl_sv_catpvf(pTHX_ SV *sv, const char* pat, ...) |
46fc3d4c | 6627 | { |
6628 | va_list args; | |
46fc3d4c | 6629 | va_start(args, pat); |
c5be433b | 6630 | sv_vcatpvf(sv, pat, &args); |
46fc3d4c | 6631 | va_end(args); |
6632 | } | |
6633 | ||
ef50df4b | 6634 | void |
c5be433b GS |
6635 | Perl_sv_vcatpvf(pTHX_ SV *sv, const char* pat, va_list* args) |
6636 | { | |
6637 | sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
6638 | } | |
6639 | ||
954c1994 GS |
6640 | /* |
6641 | =for apidoc sv_catpvf_mg | |
6642 | ||
6643 | Like C<sv_catpvf>, but also handles 'set' magic. | |
6644 | ||
6645 | =cut | |
6646 | */ | |
6647 | ||
c5be433b | 6648 | void |
864dbfa3 | 6649 | Perl_sv_catpvf_mg(pTHX_ SV *sv, const char* pat, ...) |
ef50df4b GS |
6650 | { |
6651 | va_list args; | |
ef50df4b | 6652 | va_start(args, pat); |
c5be433b | 6653 | sv_vcatpvf_mg(sv, pat, &args); |
ef50df4b | 6654 | va_end(args); |
c5be433b GS |
6655 | } |
6656 | ||
6657 | void | |
6658 | Perl_sv_vcatpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args) | |
6659 | { | |
6660 | sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
ef50df4b GS |
6661 | SvSETMAGIC(sv); |
6662 | } | |
6663 | ||
954c1994 GS |
6664 | /* |
6665 | =for apidoc sv_vsetpvfn | |
6666 | ||
6667 | Works like C<vcatpvfn> but copies the text into the SV instead of | |
6668 | appending it. | |
6669 | ||
6670 | =cut | |
6671 | */ | |
6672 | ||
46fc3d4c | 6673 | void |
7d5ea4e7 | 6674 | Perl_sv_vsetpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted) |
46fc3d4c | 6675 | { |
6676 | sv_setpvn(sv, "", 0); | |
7d5ea4e7 | 6677 | sv_vcatpvfn(sv, pat, patlen, args, svargs, svmax, maybe_tainted); |
46fc3d4c | 6678 | } |
6679 | ||
954c1994 GS |
6680 | /* |
6681 | =for apidoc sv_vcatpvfn | |
6682 | ||
6683 | Processes its arguments like C<vsprintf> and appends the formatted output | |
6684 | to an SV. Uses an array of SVs if the C style variable argument list is | |
6685 | missing (NULL). When running with taint checks enabled, indicates via | |
6686 | C<maybe_tainted> if results are untrustworthy (often due to the use of | |
6687 | locales). | |
6688 | ||
6689 | =cut | |
6690 | */ | |
6691 | ||
46fc3d4c | 6692 | void |
7d5ea4e7 | 6693 | Perl_sv_vcatpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted) |
46fc3d4c | 6694 | { |
6695 | char *p; | |
6696 | char *q; | |
6697 | char *patend; | |
fc36a67e | 6698 | STRLEN origlen; |
46fc3d4c | 6699 | I32 svix = 0; |
c635e13b | 6700 | static char nullstr[] = "(null)"; |
7e2040f0 | 6701 | SV *argsv; |
46fc3d4c | 6702 | |
6703 | /* no matter what, this is a string now */ | |
fc36a67e | 6704 | (void)SvPV_force(sv, origlen); |
46fc3d4c | 6705 | |
fc36a67e | 6706 | /* special-case "", "%s", and "%_" */ |
46fc3d4c | 6707 | if (patlen == 0) |
6708 | return; | |
fc36a67e | 6709 | if (patlen == 2 && pat[0] == '%') { |
6710 | switch (pat[1]) { | |
6711 | case 's': | |
c635e13b | 6712 | if (args) { |
6713 | char *s = va_arg(*args, char*); | |
6714 | sv_catpv(sv, s ? s : nullstr); | |
6715 | } | |
7e2040f0 | 6716 | else if (svix < svmax) { |
fc36a67e | 6717 | sv_catsv(sv, *svargs); |
7e2040f0 GS |
6718 | if (DO_UTF8(*svargs)) |
6719 | SvUTF8_on(sv); | |
6720 | } | |
fc36a67e | 6721 | return; |
6722 | case '_': | |
6723 | if (args) { | |
7e2040f0 GS |
6724 | argsv = va_arg(*args, SV*); |
6725 | sv_catsv(sv, argsv); | |
6726 | if (DO_UTF8(argsv)) | |
6727 | SvUTF8_on(sv); | |
fc36a67e | 6728 | return; |
6729 | } | |
6730 | /* See comment on '_' below */ | |
6731 | break; | |
6732 | } | |
46fc3d4c | 6733 | } |
6734 | ||
6735 | patend = (char*)pat + patlen; | |
6736 | for (p = (char*)pat; p < patend; p = q) { | |
6737 | bool alt = FALSE; | |
6738 | bool left = FALSE; | |
b22c7a20 GS |
6739 | bool vectorize = FALSE; |
6740 | bool utf = FALSE; | |
46fc3d4c | 6741 | char fill = ' '; |
6742 | char plus = 0; | |
6743 | char intsize = 0; | |
6744 | STRLEN width = 0; | |
fc36a67e | 6745 | STRLEN zeros = 0; |
46fc3d4c | 6746 | bool has_precis = FALSE; |
6747 | STRLEN precis = 0; | |
7e2040f0 | 6748 | bool is_utf = FALSE; |
eb3fce90 | 6749 | |
46fc3d4c | 6750 | char esignbuf[4]; |
ad391ad9 | 6751 | U8 utf8buf[UTF8_MAXLEN+1]; |
46fc3d4c | 6752 | STRLEN esignlen = 0; |
6753 | ||
6754 | char *eptr = Nullch; | |
fc36a67e | 6755 | STRLEN elen = 0; |
089c015b JH |
6756 | /* Times 4: a decimal digit takes more than 3 binary digits. |
6757 | * NV_DIG: mantissa takes than many decimal digits. | |
6758 | * Plus 32: Playing safe. */ | |
6759 | char ebuf[IV_DIG * 4 + NV_DIG + 32]; | |
2d4389e4 JH |
6760 | /* large enough for "%#.#f" --chip */ |
6761 | /* what about long double NVs? --jhi */ | |
b22c7a20 GS |
6762 | |
6763 | SV *vecsv; | |
a05b299f | 6764 | U8 *vecstr = Null(U8*); |
b22c7a20 | 6765 | STRLEN veclen = 0; |
46fc3d4c | 6766 | char c; |
6767 | int i; | |
6768 | unsigned base; | |
6769 | IV iv; | |
6770 | UV uv; | |
65202027 | 6771 | NV nv; |
46fc3d4c | 6772 | STRLEN have; |
6773 | STRLEN need; | |
6774 | STRLEN gap; | |
b22c7a20 GS |
6775 | char *dotstr = "."; |
6776 | STRLEN dotstrlen = 1; | |
eb3fce90 JH |
6777 | I32 epix = 0; /* explicit parameter index */ |
6778 | I32 ewix = 0; /* explicit width index */ | |
6779 | bool asterisk = FALSE; | |
46fc3d4c | 6780 | |
6781 | for (q = p; q < patend && *q != '%'; ++q) ; | |
6782 | if (q > p) { | |
6783 | sv_catpvn(sv, p, q - p); | |
6784 | p = q; | |
6785 | } | |
6786 | if (q++ >= patend) | |
6787 | break; | |
6788 | ||
fc36a67e | 6789 | /* FLAGS */ |
6790 | ||
46fc3d4c | 6791 | while (*q) { |
6792 | switch (*q) { | |
6793 | case ' ': | |
6794 | case '+': | |
6795 | plus = *q++; | |
6796 | continue; | |
6797 | ||
6798 | case '-': | |
6799 | left = TRUE; | |
6800 | q++; | |
6801 | continue; | |
6802 | ||
6803 | case '0': | |
6804 | fill = *q++; | |
6805 | continue; | |
6806 | ||
6807 | case '#': | |
6808 | alt = TRUE; | |
6809 | q++; | |
6810 | continue; | |
6811 | ||
b22c7a20 GS |
6812 | case '*': /* printf("%*vX",":",$ipv6addr) */ |
6813 | if (q[1] != 'v') | |
6814 | break; | |
6815 | q++; | |
6816 | if (args) | |
6817 | vecsv = va_arg(*args, SV*); | |
6818 | else if (svix < svmax) | |
6819 | vecsv = svargs[svix++]; | |
9c3dd3fe GS |
6820 | else |
6821 | continue; | |
b22c7a20 GS |
6822 | dotstr = SvPVx(vecsv,dotstrlen); |
6823 | if (DO_UTF8(vecsv)) | |
6824 | is_utf = TRUE; | |
6825 | /* FALL THROUGH */ | |
6826 | ||
6827 | case 'v': | |
6828 | vectorize = TRUE; | |
6829 | q++; | |
b22c7a20 GS |
6830 | continue; |
6831 | ||
fc36a67e | 6832 | default: |
6833 | break; | |
6834 | } | |
6835 | break; | |
6836 | } | |
46fc3d4c | 6837 | |
fc36a67e | 6838 | /* WIDTH */ |
6839 | ||
eb3fce90 JH |
6840 | scanwidth: |
6841 | ||
6842 | if (*q == '*') { | |
6843 | if (asterisk) | |
6844 | goto unknown; | |
6845 | asterisk = TRUE; | |
6846 | q++; | |
6847 | } | |
6848 | ||
fc36a67e | 6849 | switch (*q) { |
6850 | case '1': case '2': case '3': | |
6851 | case '4': case '5': case '6': | |
6852 | case '7': case '8': case '9': | |
6853 | width = 0; | |
6854 | while (isDIGIT(*q)) | |
6855 | width = width * 10 + (*q++ - '0'); | |
eb3fce90 JH |
6856 | if (*q == '$') { |
6857 | if (asterisk && ewix == 0) { | |
6858 | ewix = width; | |
6859 | width = 0; | |
6860 | q++; | |
6861 | goto scanwidth; | |
6862 | } else if (epix == 0) { | |
6863 | epix = width; | |
6864 | width = 0; | |
6865 | q++; | |
6866 | goto scanwidth; | |
6867 | } else | |
6868 | goto unknown; | |
6869 | } | |
6870 | } | |
fc36a67e | 6871 | |
eb3fce90 | 6872 | if (asterisk) { |
fc36a67e | 6873 | if (args) |
6874 | i = va_arg(*args, int); | |
6875 | else | |
eb3fce90 JH |
6876 | i = (ewix ? ewix <= svmax : svix < svmax) ? |
6877 | SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0; | |
fc36a67e | 6878 | left |= (i < 0); |
6879 | width = (i < 0) ? -i : i; | |
fc36a67e | 6880 | } |
6881 | ||
6882 | /* PRECISION */ | |
46fc3d4c | 6883 | |
fc36a67e | 6884 | if (*q == '.') { |
6885 | q++; | |
6886 | if (*q == '*') { | |
46fc3d4c | 6887 | if (args) |
6888 | i = va_arg(*args, int); | |
6889 | else | |
eb3fce90 JH |
6890 | i = (ewix ? ewix <= svmax : svix < svmax) |
6891 | ? SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0; | |
fc36a67e | 6892 | precis = (i < 0) ? 0 : i; |
46fc3d4c | 6893 | q++; |
fc36a67e | 6894 | } |
6895 | else { | |
6896 | precis = 0; | |
6897 | while (isDIGIT(*q)) | |
6898 | precis = precis * 10 + (*q++ - '0'); | |
6899 | } | |
6900 | has_precis = TRUE; | |
6901 | } | |
46fc3d4c | 6902 | |
bb2899ba GS |
6903 | if (vectorize) { |
6904 | if (args) { | |
6905 | vecsv = va_arg(*args, SV*); | |
6906 | vecstr = (U8*)SvPVx(vecsv,veclen); | |
6907 | utf = DO_UTF8(vecsv); | |
6908 | } | |
eb3fce90 JH |
6909 | else if (epix ? epix <= svmax : svix < svmax) { |
6910 | vecsv = svargs[epix ? epix-1 : svix++]; | |
bb2899ba GS |
6911 | vecstr = (U8*)SvPVx(vecsv,veclen); |
6912 | utf = DO_UTF8(vecsv); | |
6913 | } | |
6914 | else { | |
6915 | vecstr = (U8*)""; | |
6916 | veclen = 0; | |
6917 | } | |
6918 | } | |
6919 | ||
fc36a67e | 6920 | /* SIZE */ |
46fc3d4c | 6921 | |
fc36a67e | 6922 | switch (*q) { |
e5c81feb | 6923 | #if defined(HAS_QUAD) || (defined(HAS_LONG_DOUBLE) && defined(USE_LONG_DOUBLE)) |
6f9bb7fd | 6924 | case 'L': /* Ld */ |
e5c81feb JH |
6925 | /* FALL THROUGH */ |
6926 | #endif | |
6927 | #ifdef HAS_QUAD | |
6f9bb7fd GS |
6928 | case 'q': /* qd */ |
6929 | intsize = 'q'; | |
6930 | q++; | |
6931 | break; | |
6932 | #endif | |
fc36a67e | 6933 | case 'l': |
e5c81feb JH |
6934 | #if defined(HAS_QUAD) || (defined(HAS_LONG_DOUBLE) && defined(USE_LONG_DOUBLE)) |
6935 | if (*(q + 1) == 'l') { /* lld, llf */ | |
fc36a67e | 6936 | intsize = 'q'; |
6937 | q += 2; | |
46fc3d4c | 6938 | break; |
cf2093f6 | 6939 | } |
fc36a67e | 6940 | #endif |
6f9bb7fd | 6941 | /* FALL THROUGH */ |
fc36a67e | 6942 | case 'h': |
cf2093f6 | 6943 | /* FALL THROUGH */ |
fc36a67e | 6944 | case 'V': |
6945 | intsize = *q++; | |
46fc3d4c | 6946 | break; |
6947 | } | |
6948 | ||
fc36a67e | 6949 | /* CONVERSION */ |
6950 | ||
46fc3d4c | 6951 | switch (c = *q++) { |
6952 | ||
6953 | /* STRINGS */ | |
6954 | ||
6955 | case '%': | |
6956 | eptr = q - 1; | |
6957 | elen = 1; | |
6958 | goto string; | |
6959 | ||
6960 | case 'c': | |
7e2040f0 GS |
6961 | if (args) |
6962 | uv = va_arg(*args, int); | |
6963 | else | |
eb3fce90 JH |
6964 | uv = (epix ? epix <= svmax : svix < svmax) ? |
6965 | SvIVx(svargs[epix ? epix-1 : svix++]) : 0; | |
3969a896 | 6966 | if ((uv > 255 || (uv > 127 && SvUTF8(sv))) && !IN_BYTE) { |
dfe13c55 GS |
6967 | eptr = (char*)utf8buf; |
6968 | elen = uv_to_utf8((U8*)eptr, uv) - utf8buf; | |
7e2040f0 GS |
6969 | is_utf = TRUE; |
6970 | } | |
6971 | else { | |
6972 | c = (char)uv; | |
6973 | eptr = &c; | |
6974 | elen = 1; | |
a0ed51b3 | 6975 | } |
46fc3d4c | 6976 | goto string; |
6977 | ||
46fc3d4c | 6978 | case 's': |
6979 | if (args) { | |
fc36a67e | 6980 | eptr = va_arg(*args, char*); |
c635e13b | 6981 | if (eptr) |
1d7c1841 GS |
6982 | #ifdef MACOS_TRADITIONAL |
6983 | /* On MacOS, %#s format is used for Pascal strings */ | |
6984 | if (alt) | |
6985 | elen = *eptr++; | |
6986 | else | |
6987 | #endif | |
c635e13b | 6988 | elen = strlen(eptr); |
6989 | else { | |
6990 | eptr = nullstr; | |
6991 | elen = sizeof nullstr - 1; | |
6992 | } | |
46fc3d4c | 6993 | } |
eb3fce90 JH |
6994 | else if (epix ? epix <= svmax : svix < svmax) { |
6995 | argsv = svargs[epix ? epix-1 : svix++]; | |
7e2040f0 GS |
6996 | eptr = SvPVx(argsv, elen); |
6997 | if (DO_UTF8(argsv)) { | |
a0ed51b3 LW |
6998 | if (has_precis && precis < elen) { |
6999 | I32 p = precis; | |
7e2040f0 | 7000 | sv_pos_u2b(argsv, &p, 0); /* sticks at end */ |
a0ed51b3 LW |
7001 | precis = p; |
7002 | } | |
7003 | if (width) { /* fudge width (can't fudge elen) */ | |
7e2040f0 | 7004 | width += elen - sv_len_utf8(argsv); |
a0ed51b3 | 7005 | } |
7e2040f0 | 7006 | is_utf = TRUE; |
a0ed51b3 LW |
7007 | } |
7008 | } | |
46fc3d4c | 7009 | goto string; |
7010 | ||
fc36a67e | 7011 | case '_': |
7012 | /* | |
7013 | * The "%_" hack might have to be changed someday, | |
7014 | * if ISO or ANSI decide to use '_' for something. | |
7015 | * So we keep it hidden from users' code. | |
7016 | */ | |
7017 | if (!args) | |
7018 | goto unknown; | |
7e2040f0 GS |
7019 | argsv = va_arg(*args,SV*); |
7020 | eptr = SvPVx(argsv, elen); | |
7021 | if (DO_UTF8(argsv)) | |
7022 | is_utf = TRUE; | |
fc36a67e | 7023 | |
46fc3d4c | 7024 | string: |
b22c7a20 | 7025 | vectorize = FALSE; |
46fc3d4c | 7026 | if (has_precis && elen > precis) |
7027 | elen = precis; | |
7028 | break; | |
7029 | ||
7030 | /* INTEGERS */ | |
7031 | ||
fc36a67e | 7032 | case 'p': |
c2e66d9e GS |
7033 | if (alt) |
7034 | goto unknown; | |
fc36a67e | 7035 | if (args) |
56431972 | 7036 | uv = PTR2UV(va_arg(*args, void*)); |
fc36a67e | 7037 | else |
eb3fce90 JH |
7038 | uv = (epix ? epix <= svmax : svix < svmax) ? |
7039 | PTR2UV(svargs[epix ? epix-1 : svix++]) : 0; | |
fc36a67e | 7040 | base = 16; |
7041 | goto integer; | |
7042 | ||
46fc3d4c | 7043 | case 'D': |
29fe7a80 | 7044 | #ifdef IV_IS_QUAD |
22f3ae8c | 7045 | intsize = 'q'; |
29fe7a80 | 7046 | #else |
46fc3d4c | 7047 | intsize = 'l'; |
29fe7a80 | 7048 | #endif |
46fc3d4c | 7049 | /* FALL THROUGH */ |
7050 | case 'd': | |
7051 | case 'i': | |
b22c7a20 | 7052 | if (vectorize) { |
ba210ebe | 7053 | STRLEN ulen; |
b22c7a20 GS |
7054 | if (!veclen) { |
7055 | vectorize = FALSE; | |
7056 | break; | |
7057 | } | |
7058 | if (utf) | |
dcad2880 | 7059 | iv = (IV)utf8_to_uv(vecstr, veclen, &ulen, 0); |
b22c7a20 | 7060 | else { |
a05b299f | 7061 | iv = *vecstr; |
b22c7a20 GS |
7062 | ulen = 1; |
7063 | } | |
7064 | vecstr += ulen; | |
7065 | veclen -= ulen; | |
7066 | } | |
7067 | else if (args) { | |
46fc3d4c | 7068 | switch (intsize) { |
7069 | case 'h': iv = (short)va_arg(*args, int); break; | |
7070 | default: iv = va_arg(*args, int); break; | |
7071 | case 'l': iv = va_arg(*args, long); break; | |
fc36a67e | 7072 | case 'V': iv = va_arg(*args, IV); break; |
cf2093f6 JH |
7073 | #ifdef HAS_QUAD |
7074 | case 'q': iv = va_arg(*args, Quad_t); break; | |
7075 | #endif | |
46fc3d4c | 7076 | } |
7077 | } | |
7078 | else { | |
eb3fce90 JH |
7079 | iv = (epix ? epix <= svmax : svix < svmax) ? |
7080 | SvIVx(svargs[epix ? epix-1 : svix++]) : 0; | |
46fc3d4c | 7081 | switch (intsize) { |
7082 | case 'h': iv = (short)iv; break; | |
be28567c | 7083 | default: break; |
46fc3d4c | 7084 | case 'l': iv = (long)iv; break; |
fc36a67e | 7085 | case 'V': break; |
cf2093f6 JH |
7086 | #ifdef HAS_QUAD |
7087 | case 'q': iv = (Quad_t)iv; break; | |
7088 | #endif | |
46fc3d4c | 7089 | } |
7090 | } | |
7091 | if (iv >= 0) { | |
7092 | uv = iv; | |
7093 | if (plus) | |
7094 | esignbuf[esignlen++] = plus; | |
7095 | } | |
7096 | else { | |
7097 | uv = -iv; | |
7098 | esignbuf[esignlen++] = '-'; | |
7099 | } | |
7100 | base = 10; | |
7101 | goto integer; | |
7102 | ||
fc36a67e | 7103 | case 'U': |
29fe7a80 | 7104 | #ifdef IV_IS_QUAD |
22f3ae8c | 7105 | intsize = 'q'; |
29fe7a80 | 7106 | #else |
fc36a67e | 7107 | intsize = 'l'; |
29fe7a80 | 7108 | #endif |
fc36a67e | 7109 | /* FALL THROUGH */ |
7110 | case 'u': | |
7111 | base = 10; | |
7112 | goto uns_integer; | |
7113 | ||
4f19785b WSI |
7114 | case 'b': |
7115 | base = 2; | |
7116 | goto uns_integer; | |
7117 | ||
46fc3d4c | 7118 | case 'O': |
29fe7a80 | 7119 | #ifdef IV_IS_QUAD |
22f3ae8c | 7120 | intsize = 'q'; |
29fe7a80 | 7121 | #else |
46fc3d4c | 7122 | intsize = 'l'; |
29fe7a80 | 7123 | #endif |
46fc3d4c | 7124 | /* FALL THROUGH */ |
7125 | case 'o': | |
7126 | base = 8; | |
7127 | goto uns_integer; | |
7128 | ||
7129 | case 'X': | |
46fc3d4c | 7130 | case 'x': |
7131 | base = 16; | |
46fc3d4c | 7132 | |
7133 | uns_integer: | |
b22c7a20 | 7134 | if (vectorize) { |
ba210ebe | 7135 | STRLEN ulen; |
b22c7a20 GS |
7136 | vector: |
7137 | if (!veclen) { | |
7138 | vectorize = FALSE; | |
7139 | break; | |
7140 | } | |
7141 | if (utf) | |
dcad2880 | 7142 | uv = utf8_to_uv(vecstr, veclen, &ulen, 0); |
b22c7a20 | 7143 | else { |
a05b299f | 7144 | uv = *vecstr; |
b22c7a20 GS |
7145 | ulen = 1; |
7146 | } | |
7147 | vecstr += ulen; | |
7148 | veclen -= ulen; | |
7149 | } | |
7150 | else if (args) { | |
46fc3d4c | 7151 | switch (intsize) { |
7152 | case 'h': uv = (unsigned short)va_arg(*args, unsigned); break; | |
7153 | default: uv = va_arg(*args, unsigned); break; | |
7154 | case 'l': uv = va_arg(*args, unsigned long); break; | |
fc36a67e | 7155 | case 'V': uv = va_arg(*args, UV); break; |
cf2093f6 JH |
7156 | #ifdef HAS_QUAD |
7157 | case 'q': uv = va_arg(*args, Quad_t); break; | |
7158 | #endif | |
46fc3d4c | 7159 | } |
7160 | } | |
7161 | else { | |
eb3fce90 JH |
7162 | uv = (epix ? epix <= svmax : svix < svmax) ? |
7163 | SvUVx(svargs[epix ? epix-1 : svix++]) : 0; | |
46fc3d4c | 7164 | switch (intsize) { |
7165 | case 'h': uv = (unsigned short)uv; break; | |
be28567c | 7166 | default: break; |
46fc3d4c | 7167 | case 'l': uv = (unsigned long)uv; break; |
fc36a67e | 7168 | case 'V': break; |
cf2093f6 JH |
7169 | #ifdef HAS_QUAD |
7170 | case 'q': uv = (Quad_t)uv; break; | |
7171 | #endif | |
46fc3d4c | 7172 | } |
7173 | } | |
7174 | ||
7175 | integer: | |
46fc3d4c | 7176 | eptr = ebuf + sizeof ebuf; |
fc36a67e | 7177 | switch (base) { |
7178 | unsigned dig; | |
7179 | case 16: | |
c10ed8b9 HS |
7180 | if (!uv) |
7181 | alt = FALSE; | |
1d7c1841 GS |
7182 | p = (char*)((c == 'X') |
7183 | ? "0123456789ABCDEF" : "0123456789abcdef"); | |
fc36a67e | 7184 | do { |
7185 | dig = uv & 15; | |
7186 | *--eptr = p[dig]; | |
7187 | } while (uv >>= 4); | |
7188 | if (alt) { | |
46fc3d4c | 7189 | esignbuf[esignlen++] = '0'; |
fc36a67e | 7190 | esignbuf[esignlen++] = c; /* 'x' or 'X' */ |
46fc3d4c | 7191 | } |
fc36a67e | 7192 | break; |
7193 | case 8: | |
7194 | do { | |
7195 | dig = uv & 7; | |
7196 | *--eptr = '0' + dig; | |
7197 | } while (uv >>= 3); | |
7198 | if (alt && *eptr != '0') | |
7199 | *--eptr = '0'; | |
7200 | break; | |
4f19785b WSI |
7201 | case 2: |
7202 | do { | |
7203 | dig = uv & 1; | |
7204 | *--eptr = '0' + dig; | |
7205 | } while (uv >>= 1); | |
eda88b6d JH |
7206 | if (alt) { |
7207 | esignbuf[esignlen++] = '0'; | |
7481bb52 | 7208 | esignbuf[esignlen++] = 'b'; |
eda88b6d | 7209 | } |
4f19785b | 7210 | break; |
fc36a67e | 7211 | default: /* it had better be ten or less */ |
6bc102ca | 7212 | #if defined(PERL_Y2KWARN) |
e476b1b5 | 7213 | if (ckWARN(WARN_Y2K)) { |
6bc102ca GS |
7214 | STRLEN n; |
7215 | char *s = SvPV(sv,n); | |
7216 | if (n >= 2 && s[n-2] == '1' && s[n-1] == '9' | |
7217 | && (n == 2 || !isDIGIT(s[n-3]))) | |
7218 | { | |
e476b1b5 | 7219 | Perl_warner(aTHX_ WARN_Y2K, |
6bc102ca GS |
7220 | "Possible Y2K bug: %%%c %s", |
7221 | c, "format string following '19'"); | |
7222 | } | |
7223 | } | |
7224 | #endif | |
fc36a67e | 7225 | do { |
7226 | dig = uv % base; | |
7227 | *--eptr = '0' + dig; | |
7228 | } while (uv /= base); | |
7229 | break; | |
46fc3d4c | 7230 | } |
7231 | elen = (ebuf + sizeof ebuf) - eptr; | |
c10ed8b9 HS |
7232 | if (has_precis) { |
7233 | if (precis > elen) | |
7234 | zeros = precis - elen; | |
7235 | else if (precis == 0 && elen == 1 && *eptr == '0') | |
7236 | elen = 0; | |
7237 | } | |
46fc3d4c | 7238 | break; |
7239 | ||
7240 | /* FLOATING POINT */ | |
7241 | ||
fc36a67e | 7242 | case 'F': |
7243 | c = 'f'; /* maybe %F isn't supported here */ | |
7244 | /* FALL THROUGH */ | |
46fc3d4c | 7245 | case 'e': case 'E': |
fc36a67e | 7246 | case 'f': |
46fc3d4c | 7247 | case 'g': case 'G': |
7248 | ||
7249 | /* This is evil, but floating point is even more evil */ | |
7250 | ||
b22c7a20 | 7251 | vectorize = FALSE; |
fc36a67e | 7252 | if (args) |
65202027 | 7253 | nv = va_arg(*args, NV); |
fc36a67e | 7254 | else |
eb3fce90 JH |
7255 | nv = (epix ? epix <= svmax : svix < svmax) ? |
7256 | SvNVx(svargs[epix ? epix-1 : svix++]) : 0.0; | |
fc36a67e | 7257 | |
7258 | need = 0; | |
7259 | if (c != 'e' && c != 'E') { | |
7260 | i = PERL_INT_MIN; | |
73b309ea | 7261 | (void)Perl_frexp(nv, &i); |
fc36a67e | 7262 | if (i == PERL_INT_MIN) |
cea2e8a9 | 7263 | Perl_die(aTHX_ "panic: frexp"); |
c635e13b | 7264 | if (i > 0) |
fc36a67e | 7265 | need = BIT_DIGITS(i); |
7266 | } | |
7267 | need += has_precis ? precis : 6; /* known default */ | |
7268 | if (need < width) | |
7269 | need = width; | |
7270 | ||
46fc3d4c | 7271 | need += 20; /* fudge factor */ |
80252599 GS |
7272 | if (PL_efloatsize < need) { |
7273 | Safefree(PL_efloatbuf); | |
7274 | PL_efloatsize = need + 20; /* more fudge */ | |
7275 | New(906, PL_efloatbuf, PL_efloatsize, char); | |
7d5ea4e7 | 7276 | PL_efloatbuf[0] = '\0'; |
46fc3d4c | 7277 | } |
7278 | ||
7279 | eptr = ebuf + sizeof ebuf; | |
7280 | *--eptr = '\0'; | |
7281 | *--eptr = c; | |
e5c81feb | 7282 | #if defined(USE_LONG_DOUBLE) && defined(PERL_PRIfldbl) |
cf2093f6 | 7283 | { |
e5c81feb JH |
7284 | /* Copy the one or more characters in a long double |
7285 | * format before the 'base' ([efgEFG]) character to | |
7286 | * the format string. */ | |
7287 | static char const prifldbl[] = PERL_PRIfldbl; | |
7288 | char const *p = prifldbl + sizeof(prifldbl) - 3; | |
7289 | while (p >= prifldbl) { *--eptr = *p--; } | |
cf2093f6 | 7290 | } |
65202027 | 7291 | #endif |
46fc3d4c | 7292 | if (has_precis) { |
7293 | base = precis; | |
7294 | do { *--eptr = '0' + (base % 10); } while (base /= 10); | |
7295 | *--eptr = '.'; | |
7296 | } | |
7297 | if (width) { | |
7298 | base = width; | |
7299 | do { *--eptr = '0' + (base % 10); } while (base /= 10); | |
7300 | } | |
7301 | if (fill == '0') | |
7302 | *--eptr = fill; | |
84902520 TB |
7303 | if (left) |
7304 | *--eptr = '-'; | |
46fc3d4c | 7305 | if (plus) |
7306 | *--eptr = plus; | |
7307 | if (alt) | |
7308 | *--eptr = '#'; | |
7309 | *--eptr = '%'; | |
7310 | ||
ff9121f8 JH |
7311 | /* No taint. Otherwise we are in the strange situation |
7312 | * where printf() taints but print($float) doesn't. | |
bda0f7a5 | 7313 | * --jhi */ |
dd8482fc | 7314 | (void)sprintf(PL_efloatbuf, eptr, nv); |
8af02333 | 7315 | |
80252599 GS |
7316 | eptr = PL_efloatbuf; |
7317 | elen = strlen(PL_efloatbuf); | |
46fc3d4c | 7318 | break; |
7319 | ||
fc36a67e | 7320 | /* SPECIAL */ |
7321 | ||
7322 | case 'n': | |
b22c7a20 | 7323 | vectorize = FALSE; |
fc36a67e | 7324 | i = SvCUR(sv) - origlen; |
7325 | if (args) { | |
c635e13b | 7326 | switch (intsize) { |
7327 | case 'h': *(va_arg(*args, short*)) = i; break; | |
7328 | default: *(va_arg(*args, int*)) = i; break; | |
7329 | case 'l': *(va_arg(*args, long*)) = i; break; | |
7330 | case 'V': *(va_arg(*args, IV*)) = i; break; | |
cf2093f6 JH |
7331 | #ifdef HAS_QUAD |
7332 | case 'q': *(va_arg(*args, Quad_t*)) = i; break; | |
7333 | #endif | |
c635e13b | 7334 | } |
fc36a67e | 7335 | } |
eb3fce90 JH |
7336 | else if (epix ? epix <= svmax : svix < svmax) |
7337 | sv_setuv_mg(svargs[epix ? epix-1 : svix++], (UV)i); | |
fc36a67e | 7338 | continue; /* not "break" */ |
7339 | ||
7340 | /* UNKNOWN */ | |
7341 | ||
46fc3d4c | 7342 | default: |
fc36a67e | 7343 | unknown: |
b22c7a20 | 7344 | vectorize = FALSE; |
599cee73 | 7345 | if (!args && ckWARN(WARN_PRINTF) && |
533c011a | 7346 | (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF)) { |
c635e13b | 7347 | SV *msg = sv_newmortal(); |
cea2e8a9 | 7348 | Perl_sv_setpvf(aTHX_ msg, "Invalid conversion in %s: ", |
533c011a | 7349 | (PL_op->op_type == OP_PRTF) ? "printf" : "sprintf"); |
0f4b6630 | 7350 | if (c) { |
0f4b6630 | 7351 | if (isPRINT(c)) |
1c846c1f | 7352 | Perl_sv_catpvf(aTHX_ msg, |
0f4b6630 JH |
7353 | "\"%%%c\"", c & 0xFF); |
7354 | else | |
7355 | Perl_sv_catpvf(aTHX_ msg, | |
57def98f | 7356 | "\"%%\\%03"UVof"\"", |
0f4b6630 | 7357 | (UV)c & 0xFF); |
0f4b6630 | 7358 | } else |
c635e13b | 7359 | sv_catpv(msg, "end of string"); |
894356b3 | 7360 | Perl_warner(aTHX_ WARN_PRINTF, "%"SVf, msg); /* yes, this is reentrant */ |
c635e13b | 7361 | } |
fb73857a | 7362 | |
7363 | /* output mangled stuff ... */ | |
7364 | if (c == '\0') | |
7365 | --q; | |
46fc3d4c | 7366 | eptr = p; |
7367 | elen = q - p; | |
fb73857a | 7368 | |
7369 | /* ... right here, because formatting flags should not apply */ | |
7370 | SvGROW(sv, SvCUR(sv) + elen + 1); | |
7371 | p = SvEND(sv); | |
7372 | memcpy(p, eptr, elen); | |
7373 | p += elen; | |
7374 | *p = '\0'; | |
7375 | SvCUR(sv) = p - SvPVX(sv); | |
7376 | continue; /* not "break" */ | |
46fc3d4c | 7377 | } |
7378 | ||
fc36a67e | 7379 | have = esignlen + zeros + elen; |
46fc3d4c | 7380 | need = (have > width ? have : width); |
7381 | gap = need - have; | |
7382 | ||
b22c7a20 | 7383 | SvGROW(sv, SvCUR(sv) + need + dotstrlen + 1); |
46fc3d4c | 7384 | p = SvEND(sv); |
7385 | if (esignlen && fill == '0') { | |
7386 | for (i = 0; i < esignlen; i++) | |
7387 | *p++ = esignbuf[i]; | |
7388 | } | |
7389 | if (gap && !left) { | |
7390 | memset(p, fill, gap); | |
7391 | p += gap; | |
7392 | } | |
7393 | if (esignlen && fill != '0') { | |
7394 | for (i = 0; i < esignlen; i++) | |
7395 | *p++ = esignbuf[i]; | |
7396 | } | |
fc36a67e | 7397 | if (zeros) { |
7398 | for (i = zeros; i; i--) | |
7399 | *p++ = '0'; | |
7400 | } | |
46fc3d4c | 7401 | if (elen) { |
7402 | memcpy(p, eptr, elen); | |
7403 | p += elen; | |
7404 | } | |
7405 | if (gap && left) { | |
7406 | memset(p, ' ', gap); | |
7407 | p += gap; | |
7408 | } | |
b22c7a20 GS |
7409 | if (vectorize) { |
7410 | if (veclen) { | |
7411 | memcpy(p, dotstr, dotstrlen); | |
7412 | p += dotstrlen; | |
7413 | } | |
7414 | else | |
7415 | vectorize = FALSE; /* done iterating over vecstr */ | |
7416 | } | |
7e2040f0 GS |
7417 | if (is_utf) |
7418 | SvUTF8_on(sv); | |
46fc3d4c | 7419 | *p = '\0'; |
7420 | SvCUR(sv) = p - SvPVX(sv); | |
b22c7a20 GS |
7421 | if (vectorize) { |
7422 | esignlen = 0; | |
7423 | goto vector; | |
7424 | } | |
46fc3d4c | 7425 | } |
7426 | } | |
51371543 | 7427 | |
1d7c1841 GS |
7428 | #if defined(USE_ITHREADS) |
7429 | ||
7430 | #if defined(USE_THREADS) | |
7431 | # include "error: USE_THREADS and USE_ITHREADS are incompatible" | |
7432 | #endif | |
7433 | ||
1d7c1841 GS |
7434 | #ifndef GpREFCNT_inc |
7435 | # define GpREFCNT_inc(gp) ((gp) ? (++(gp)->gp_refcnt, (gp)) : (GP*)NULL) | |
7436 | #endif | |
7437 | ||
7438 | ||
7439 | #define sv_dup_inc(s) SvREFCNT_inc(sv_dup(s)) | |
7440 | #define av_dup(s) (AV*)sv_dup((SV*)s) | |
7441 | #define av_dup_inc(s) (AV*)SvREFCNT_inc(sv_dup((SV*)s)) | |
7442 | #define hv_dup(s) (HV*)sv_dup((SV*)s) | |
7443 | #define hv_dup_inc(s) (HV*)SvREFCNT_inc(sv_dup((SV*)s)) | |
7444 | #define cv_dup(s) (CV*)sv_dup((SV*)s) | |
7445 | #define cv_dup_inc(s) (CV*)SvREFCNT_inc(sv_dup((SV*)s)) | |
7446 | #define io_dup(s) (IO*)sv_dup((SV*)s) | |
7447 | #define io_dup_inc(s) (IO*)SvREFCNT_inc(sv_dup((SV*)s)) | |
7448 | #define gv_dup(s) (GV*)sv_dup((SV*)s) | |
7449 | #define gv_dup_inc(s) (GV*)SvREFCNT_inc(sv_dup((SV*)s)) | |
7450 | #define SAVEPV(p) (p ? savepv(p) : Nullch) | |
7451 | #define SAVEPVN(p,n) (p ? savepvn(p,n) : Nullch) | |
7452 | ||
7453 | REGEXP * | |
7454 | Perl_re_dup(pTHX_ REGEXP *r) | |
7455 | { | |
7456 | /* XXX fix when pmop->op_pmregexp becomes shared */ | |
7457 | return ReREFCNT_inc(r); | |
7458 | } | |
7459 | ||
7460 | PerlIO * | |
7461 | Perl_fp_dup(pTHX_ PerlIO *fp, char type) | |
7462 | { | |
7463 | PerlIO *ret; | |
7464 | if (!fp) | |
7465 | return (PerlIO*)NULL; | |
7466 | ||
7467 | /* look for it in the table first */ | |
7468 | ret = (PerlIO*)ptr_table_fetch(PL_ptr_table, fp); | |
7469 | if (ret) | |
7470 | return ret; | |
7471 | ||
7472 | /* create anew and remember what it is */ | |
5f1a76d0 | 7473 | ret = PerlIO_fdupopen(aTHX_ fp); |
1d7c1841 GS |
7474 | ptr_table_store(PL_ptr_table, fp, ret); |
7475 | return ret; | |
7476 | } | |
7477 | ||
7478 | DIR * | |
7479 | Perl_dirp_dup(pTHX_ DIR *dp) | |
7480 | { | |
7481 | if (!dp) | |
7482 | return (DIR*)NULL; | |
7483 | /* XXX TODO */ | |
7484 | return dp; | |
7485 | } | |
7486 | ||
7487 | GP * | |
7488 | Perl_gp_dup(pTHX_ GP *gp) | |
7489 | { | |
7490 | GP *ret; | |
7491 | if (!gp) | |
7492 | return (GP*)NULL; | |
7493 | /* look for it in the table first */ | |
7494 | ret = (GP*)ptr_table_fetch(PL_ptr_table, gp); | |
7495 | if (ret) | |
7496 | return ret; | |
7497 | ||
7498 | /* create anew and remember what it is */ | |
7499 | Newz(0, ret, 1, GP); | |
7500 | ptr_table_store(PL_ptr_table, gp, ret); | |
7501 | ||
7502 | /* clone */ | |
7503 | ret->gp_refcnt = 0; /* must be before any other dups! */ | |
7504 | ret->gp_sv = sv_dup_inc(gp->gp_sv); | |
7505 | ret->gp_io = io_dup_inc(gp->gp_io); | |
7506 | ret->gp_form = cv_dup_inc(gp->gp_form); | |
7507 | ret->gp_av = av_dup_inc(gp->gp_av); | |
7508 | ret->gp_hv = hv_dup_inc(gp->gp_hv); | |
7509 | ret->gp_egv = gv_dup(gp->gp_egv); /* GvEGV is not refcounted */ | |
7510 | ret->gp_cv = cv_dup_inc(gp->gp_cv); | |
7511 | ret->gp_cvgen = gp->gp_cvgen; | |
7512 | ret->gp_flags = gp->gp_flags; | |
7513 | ret->gp_line = gp->gp_line; | |
7514 | ret->gp_file = gp->gp_file; /* points to COP.cop_file */ | |
7515 | return ret; | |
7516 | } | |
7517 | ||
7518 | MAGIC * | |
7519 | Perl_mg_dup(pTHX_ MAGIC *mg) | |
7520 | { | |
7521 | MAGIC *mgret = (MAGIC*)NULL; | |
7522 | MAGIC *mgprev; | |
7523 | if (!mg) | |
7524 | return (MAGIC*)NULL; | |
7525 | /* look for it in the table first */ | |
7526 | mgret = (MAGIC*)ptr_table_fetch(PL_ptr_table, mg); | |
7527 | if (mgret) | |
7528 | return mgret; | |
7529 | ||
7530 | for (; mg; mg = mg->mg_moremagic) { | |
7531 | MAGIC *nmg; | |
7532 | Newz(0, nmg, 1, MAGIC); | |
7533 | if (!mgret) | |
7534 | mgret = nmg; | |
7535 | else | |
7536 | mgprev->mg_moremagic = nmg; | |
7537 | nmg->mg_virtual = mg->mg_virtual; /* XXX copy dynamic vtable? */ | |
7538 | nmg->mg_private = mg->mg_private; | |
7539 | nmg->mg_type = mg->mg_type; | |
7540 | nmg->mg_flags = mg->mg_flags; | |
7541 | if (mg->mg_type == 'r') { | |
7542 | nmg->mg_obj = (SV*)re_dup((REGEXP*)mg->mg_obj); | |
7543 | } | |
7544 | else { | |
7545 | nmg->mg_obj = (mg->mg_flags & MGf_REFCOUNTED) | |
7546 | ? sv_dup_inc(mg->mg_obj) | |
7547 | : sv_dup(mg->mg_obj); | |
7548 | } | |
7549 | nmg->mg_len = mg->mg_len; | |
7550 | nmg->mg_ptr = mg->mg_ptr; /* XXX random ptr? */ | |
7551 | if (mg->mg_ptr && mg->mg_type != 'g') { | |
7552 | if (mg->mg_len >= 0) { | |
7553 | nmg->mg_ptr = SAVEPVN(mg->mg_ptr, mg->mg_len); | |
7554 | if (mg->mg_type == 'c' && AMT_AMAGIC((AMT*)mg->mg_ptr)) { | |
7555 | AMT *amtp = (AMT*)mg->mg_ptr; | |
7556 | AMT *namtp = (AMT*)nmg->mg_ptr; | |
7557 | I32 i; | |
7558 | for (i = 1; i < NofAMmeth; i++) { | |
7559 | namtp->table[i] = cv_dup_inc(amtp->table[i]); | |
7560 | } | |
7561 | } | |
7562 | } | |
7563 | else if (mg->mg_len == HEf_SVKEY) | |
7564 | nmg->mg_ptr = (char*)sv_dup_inc((SV*)mg->mg_ptr); | |
7565 | } | |
7566 | mgprev = nmg; | |
7567 | } | |
7568 | return mgret; | |
7569 | } | |
7570 | ||
7571 | PTR_TBL_t * | |
7572 | Perl_ptr_table_new(pTHX) | |
7573 | { | |
7574 | PTR_TBL_t *tbl; | |
7575 | Newz(0, tbl, 1, PTR_TBL_t); | |
7576 | tbl->tbl_max = 511; | |
7577 | tbl->tbl_items = 0; | |
7578 | Newz(0, tbl->tbl_ary, tbl->tbl_max + 1, PTR_TBL_ENT_t*); | |
7579 | return tbl; | |
7580 | } | |
7581 | ||
7582 | void * | |
7583 | Perl_ptr_table_fetch(pTHX_ PTR_TBL_t *tbl, void *sv) | |
7584 | { | |
7585 | PTR_TBL_ENT_t *tblent; | |
d2a79402 | 7586 | UV hash = PTR2UV(sv); |
1d7c1841 GS |
7587 | assert(tbl); |
7588 | tblent = tbl->tbl_ary[hash & tbl->tbl_max]; | |
7589 | for (; tblent; tblent = tblent->next) { | |
7590 | if (tblent->oldval == sv) | |
7591 | return tblent->newval; | |
7592 | } | |
7593 | return (void*)NULL; | |
7594 | } | |
7595 | ||
7596 | void | |
7597 | Perl_ptr_table_store(pTHX_ PTR_TBL_t *tbl, void *oldv, void *newv) | |
7598 | { | |
7599 | PTR_TBL_ENT_t *tblent, **otblent; | |
7600 | /* XXX this may be pessimal on platforms where pointers aren't good | |
7601 | * hash values e.g. if they grow faster in the most significant | |
7602 | * bits */ | |
d2a79402 | 7603 | UV hash = PTR2UV(oldv); |
1d7c1841 GS |
7604 | bool i = 1; |
7605 | ||
7606 | assert(tbl); | |
7607 | otblent = &tbl->tbl_ary[hash & tbl->tbl_max]; | |
7608 | for (tblent = *otblent; tblent; i=0, tblent = tblent->next) { | |
7609 | if (tblent->oldval == oldv) { | |
7610 | tblent->newval = newv; | |
7611 | tbl->tbl_items++; | |
7612 | return; | |
7613 | } | |
7614 | } | |
7615 | Newz(0, tblent, 1, PTR_TBL_ENT_t); | |
7616 | tblent->oldval = oldv; | |
7617 | tblent->newval = newv; | |
7618 | tblent->next = *otblent; | |
7619 | *otblent = tblent; | |
7620 | tbl->tbl_items++; | |
7621 | if (i && tbl->tbl_items > tbl->tbl_max) | |
7622 | ptr_table_split(tbl); | |
7623 | } | |
7624 | ||
7625 | void | |
7626 | Perl_ptr_table_split(pTHX_ PTR_TBL_t *tbl) | |
7627 | { | |
7628 | PTR_TBL_ENT_t **ary = tbl->tbl_ary; | |
7629 | UV oldsize = tbl->tbl_max + 1; | |
7630 | UV newsize = oldsize * 2; | |
7631 | UV i; | |
7632 | ||
7633 | Renew(ary, newsize, PTR_TBL_ENT_t*); | |
7634 | Zero(&ary[oldsize], newsize-oldsize, PTR_TBL_ENT_t*); | |
7635 | tbl->tbl_max = --newsize; | |
7636 | tbl->tbl_ary = ary; | |
7637 | for (i=0; i < oldsize; i++, ary++) { | |
7638 | PTR_TBL_ENT_t **curentp, **entp, *ent; | |
7639 | if (!*ary) | |
7640 | continue; | |
7641 | curentp = ary + oldsize; | |
7642 | for (entp = ary, ent = *ary; ent; ent = *entp) { | |
d2a79402 | 7643 | if ((newsize & PTR2UV(ent->oldval)) != i) { |
1d7c1841 GS |
7644 | *entp = ent->next; |
7645 | ent->next = *curentp; | |
7646 | *curentp = ent; | |
7647 | continue; | |
7648 | } | |
7649 | else | |
7650 | entp = &ent->next; | |
7651 | } | |
7652 | } | |
7653 | } | |
7654 | ||
7655 | #ifdef DEBUGGING | |
7656 | char *PL_watch_pvx; | |
7657 | #endif | |
7658 | ||
7659 | SV * | |
7660 | Perl_sv_dup(pTHX_ SV *sstr) | |
7661 | { | |
1d7c1841 GS |
7662 | SV *dstr; |
7663 | ||
7664 | if (!sstr || SvTYPE(sstr) == SVTYPEMASK) | |
7665 | return Nullsv; | |
7666 | /* look for it in the table first */ | |
7667 | dstr = (SV*)ptr_table_fetch(PL_ptr_table, sstr); | |
7668 | if (dstr) | |
7669 | return dstr; | |
7670 | ||
7671 | /* create anew and remember what it is */ | |
7672 | new_SV(dstr); | |
7673 | ptr_table_store(PL_ptr_table, sstr, dstr); | |
7674 | ||
7675 | /* clone */ | |
7676 | SvFLAGS(dstr) = SvFLAGS(sstr); | |
7677 | SvFLAGS(dstr) &= ~SVf_OOK; /* don't propagate OOK hack */ | |
7678 | SvREFCNT(dstr) = 0; /* must be before any other dups! */ | |
7679 | ||
7680 | #ifdef DEBUGGING | |
7681 | if (SvANY(sstr) && PL_watch_pvx && SvPVX(sstr) == PL_watch_pvx) | |
7682 | PerlIO_printf(Perl_debug_log, "watch at %p hit, found string \"%s\"\n", | |
7683 | PL_watch_pvx, SvPVX(sstr)); | |
7684 | #endif | |
7685 | ||
7686 | switch (SvTYPE(sstr)) { | |
7687 | case SVt_NULL: | |
7688 | SvANY(dstr) = NULL; | |
7689 | break; | |
7690 | case SVt_IV: | |
7691 | SvANY(dstr) = new_XIV(); | |
7692 | SvIVX(dstr) = SvIVX(sstr); | |
7693 | break; | |
7694 | case SVt_NV: | |
7695 | SvANY(dstr) = new_XNV(); | |
7696 | SvNVX(dstr) = SvNVX(sstr); | |
7697 | break; | |
7698 | case SVt_RV: | |
7699 | SvANY(dstr) = new_XRV(); | |
7700 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7701 | break; | |
7702 | case SVt_PV: | |
7703 | SvANY(dstr) = new_XPV(); | |
7704 | SvCUR(dstr) = SvCUR(sstr); | |
7705 | SvLEN(dstr) = SvLEN(sstr); | |
7706 | if (SvROK(sstr)) | |
7707 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7708 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
7709 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
7710 | else | |
7711 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
7712 | break; | |
7713 | case SVt_PVIV: | |
7714 | SvANY(dstr) = new_XPVIV(); | |
7715 | SvCUR(dstr) = SvCUR(sstr); | |
7716 | SvLEN(dstr) = SvLEN(sstr); | |
7717 | SvIVX(dstr) = SvIVX(sstr); | |
7718 | if (SvROK(sstr)) | |
7719 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7720 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
7721 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
7722 | else | |
7723 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
7724 | break; | |
7725 | case SVt_PVNV: | |
7726 | SvANY(dstr) = new_XPVNV(); | |
7727 | SvCUR(dstr) = SvCUR(sstr); | |
7728 | SvLEN(dstr) = SvLEN(sstr); | |
7729 | SvIVX(dstr) = SvIVX(sstr); | |
7730 | SvNVX(dstr) = SvNVX(sstr); | |
7731 | if (SvROK(sstr)) | |
7732 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7733 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
7734 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
7735 | else | |
7736 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
7737 | break; | |
7738 | case SVt_PVMG: | |
7739 | SvANY(dstr) = new_XPVMG(); | |
7740 | SvCUR(dstr) = SvCUR(sstr); | |
7741 | SvLEN(dstr) = SvLEN(sstr); | |
7742 | SvIVX(dstr) = SvIVX(sstr); | |
7743 | SvNVX(dstr) = SvNVX(sstr); | |
7744 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
7745 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
7746 | if (SvROK(sstr)) | |
7747 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7748 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
7749 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
7750 | else | |
7751 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
7752 | break; | |
7753 | case SVt_PVBM: | |
7754 | SvANY(dstr) = new_XPVBM(); | |
7755 | SvCUR(dstr) = SvCUR(sstr); | |
7756 | SvLEN(dstr) = SvLEN(sstr); | |
7757 | SvIVX(dstr) = SvIVX(sstr); | |
7758 | SvNVX(dstr) = SvNVX(sstr); | |
7759 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
7760 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
7761 | if (SvROK(sstr)) | |
7762 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7763 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
7764 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
7765 | else | |
7766 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
7767 | BmRARE(dstr) = BmRARE(sstr); | |
7768 | BmUSEFUL(dstr) = BmUSEFUL(sstr); | |
7769 | BmPREVIOUS(dstr)= BmPREVIOUS(sstr); | |
7770 | break; | |
7771 | case SVt_PVLV: | |
7772 | SvANY(dstr) = new_XPVLV(); | |
7773 | SvCUR(dstr) = SvCUR(sstr); | |
7774 | SvLEN(dstr) = SvLEN(sstr); | |
7775 | SvIVX(dstr) = SvIVX(sstr); | |
7776 | SvNVX(dstr) = SvNVX(sstr); | |
7777 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
7778 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
7779 | if (SvROK(sstr)) | |
7780 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7781 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
7782 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
7783 | else | |
7784 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
7785 | LvTARGOFF(dstr) = LvTARGOFF(sstr); /* XXX sometimes holds PMOP* when DEBUGGING */ | |
7786 | LvTARGLEN(dstr) = LvTARGLEN(sstr); | |
7787 | LvTARG(dstr) = sv_dup_inc(LvTARG(sstr)); | |
7788 | LvTYPE(dstr) = LvTYPE(sstr); | |
7789 | break; | |
7790 | case SVt_PVGV: | |
7791 | SvANY(dstr) = new_XPVGV(); | |
7792 | SvCUR(dstr) = SvCUR(sstr); | |
7793 | SvLEN(dstr) = SvLEN(sstr); | |
7794 | SvIVX(dstr) = SvIVX(sstr); | |
7795 | SvNVX(dstr) = SvNVX(sstr); | |
7796 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
7797 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
7798 | if (SvROK(sstr)) | |
7799 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7800 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
7801 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
7802 | else | |
7803 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
7804 | GvNAMELEN(dstr) = GvNAMELEN(sstr); | |
7805 | GvNAME(dstr) = SAVEPVN(GvNAME(sstr), GvNAMELEN(sstr)); | |
7806 | GvSTASH(dstr) = hv_dup_inc(GvSTASH(sstr)); | |
7807 | GvFLAGS(dstr) = GvFLAGS(sstr); | |
7808 | GvGP(dstr) = gp_dup(GvGP(sstr)); | |
7809 | (void)GpREFCNT_inc(GvGP(dstr)); | |
7810 | break; | |
7811 | case SVt_PVIO: | |
7812 | SvANY(dstr) = new_XPVIO(); | |
7813 | SvCUR(dstr) = SvCUR(sstr); | |
7814 | SvLEN(dstr) = SvLEN(sstr); | |
7815 | SvIVX(dstr) = SvIVX(sstr); | |
7816 | SvNVX(dstr) = SvNVX(sstr); | |
7817 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
7818 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
7819 | if (SvROK(sstr)) | |
7820 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7821 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
7822 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
7823 | else | |
7824 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
7825 | IoIFP(dstr) = fp_dup(IoIFP(sstr), IoTYPE(sstr)); | |
7826 | if (IoOFP(sstr) == IoIFP(sstr)) | |
7827 | IoOFP(dstr) = IoIFP(dstr); | |
7828 | else | |
7829 | IoOFP(dstr) = fp_dup(IoOFP(sstr), IoTYPE(sstr)); | |
7830 | /* PL_rsfp_filters entries have fake IoDIRP() */ | |
7831 | if (IoDIRP(sstr) && !(IoFLAGS(sstr) & IOf_FAKE_DIRP)) | |
7832 | IoDIRP(dstr) = dirp_dup(IoDIRP(sstr)); | |
7833 | else | |
7834 | IoDIRP(dstr) = IoDIRP(sstr); | |
7835 | IoLINES(dstr) = IoLINES(sstr); | |
7836 | IoPAGE(dstr) = IoPAGE(sstr); | |
7837 | IoPAGE_LEN(dstr) = IoPAGE_LEN(sstr); | |
7838 | IoLINES_LEFT(dstr) = IoLINES_LEFT(sstr); | |
7839 | IoTOP_NAME(dstr) = SAVEPV(IoTOP_NAME(sstr)); | |
7840 | IoTOP_GV(dstr) = gv_dup(IoTOP_GV(sstr)); | |
7841 | IoFMT_NAME(dstr) = SAVEPV(IoFMT_NAME(sstr)); | |
7842 | IoFMT_GV(dstr) = gv_dup(IoFMT_GV(sstr)); | |
7843 | IoBOTTOM_NAME(dstr) = SAVEPV(IoBOTTOM_NAME(sstr)); | |
7844 | IoBOTTOM_GV(dstr) = gv_dup(IoBOTTOM_GV(sstr)); | |
7845 | IoSUBPROCESS(dstr) = IoSUBPROCESS(sstr); | |
7846 | IoTYPE(dstr) = IoTYPE(sstr); | |
7847 | IoFLAGS(dstr) = IoFLAGS(sstr); | |
7848 | break; | |
7849 | case SVt_PVAV: | |
7850 | SvANY(dstr) = new_XPVAV(); | |
7851 | SvCUR(dstr) = SvCUR(sstr); | |
7852 | SvLEN(dstr) = SvLEN(sstr); | |
7853 | SvIVX(dstr) = SvIVX(sstr); | |
7854 | SvNVX(dstr) = SvNVX(sstr); | |
7855 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
7856 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
7857 | AvARYLEN((AV*)dstr) = sv_dup_inc(AvARYLEN((AV*)sstr)); | |
7858 | AvFLAGS((AV*)dstr) = AvFLAGS((AV*)sstr); | |
7859 | if (AvARRAY((AV*)sstr)) { | |
7860 | SV **dst_ary, **src_ary; | |
7861 | SSize_t items = AvFILLp((AV*)sstr) + 1; | |
7862 | ||
7863 | src_ary = AvARRAY((AV*)sstr); | |
7864 | Newz(0, dst_ary, AvMAX((AV*)sstr)+1, SV*); | |
7865 | ptr_table_store(PL_ptr_table, src_ary, dst_ary); | |
7866 | SvPVX(dstr) = (char*)dst_ary; | |
7867 | AvALLOC((AV*)dstr) = dst_ary; | |
7868 | if (AvREAL((AV*)sstr)) { | |
7869 | while (items-- > 0) | |
7870 | *dst_ary++ = sv_dup_inc(*src_ary++); | |
7871 | } | |
7872 | else { | |
7873 | while (items-- > 0) | |
7874 | *dst_ary++ = sv_dup(*src_ary++); | |
7875 | } | |
7876 | items = AvMAX((AV*)sstr) - AvFILLp((AV*)sstr); | |
7877 | while (items-- > 0) { | |
7878 | *dst_ary++ = &PL_sv_undef; | |
7879 | } | |
7880 | } | |
7881 | else { | |
7882 | SvPVX(dstr) = Nullch; | |
7883 | AvALLOC((AV*)dstr) = (SV**)NULL; | |
7884 | } | |
7885 | break; | |
7886 | case SVt_PVHV: | |
7887 | SvANY(dstr) = new_XPVHV(); | |
7888 | SvCUR(dstr) = SvCUR(sstr); | |
7889 | SvLEN(dstr) = SvLEN(sstr); | |
7890 | SvIVX(dstr) = SvIVX(sstr); | |
7891 | SvNVX(dstr) = SvNVX(sstr); | |
7892 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
7893 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
7894 | HvRITER((HV*)dstr) = HvRITER((HV*)sstr); | |
7895 | if (HvARRAY((HV*)sstr)) { | |
1d7c1841 GS |
7896 | STRLEN i = 0; |
7897 | XPVHV *dxhv = (XPVHV*)SvANY(dstr); | |
7898 | XPVHV *sxhv = (XPVHV*)SvANY(sstr); | |
7899 | Newz(0, dxhv->xhv_array, | |
7900 | PERL_HV_ARRAY_ALLOC_BYTES(dxhv->xhv_max+1), char); | |
7901 | while (i <= sxhv->xhv_max) { | |
7902 | ((HE**)dxhv->xhv_array)[i] = he_dup(((HE**)sxhv->xhv_array)[i], | |
7903 | !!HvSHAREKEYS(sstr)); | |
7904 | ++i; | |
7905 | } | |
7906 | dxhv->xhv_eiter = he_dup(sxhv->xhv_eiter, !!HvSHAREKEYS(sstr)); | |
7907 | } | |
7908 | else { | |
7909 | SvPVX(dstr) = Nullch; | |
7910 | HvEITER((HV*)dstr) = (HE*)NULL; | |
7911 | } | |
7912 | HvPMROOT((HV*)dstr) = HvPMROOT((HV*)sstr); /* XXX */ | |
7913 | HvNAME((HV*)dstr) = SAVEPV(HvNAME((HV*)sstr)); | |
7914 | break; | |
7915 | case SVt_PVFM: | |
7916 | SvANY(dstr) = new_XPVFM(); | |
7917 | FmLINES(dstr) = FmLINES(sstr); | |
7918 | goto dup_pvcv; | |
7919 | /* NOTREACHED */ | |
7920 | case SVt_PVCV: | |
7921 | SvANY(dstr) = new_XPVCV(); | |
7922 | dup_pvcv: | |
7923 | SvCUR(dstr) = SvCUR(sstr); | |
7924 | SvLEN(dstr) = SvLEN(sstr); | |
7925 | SvIVX(dstr) = SvIVX(sstr); | |
7926 | SvNVX(dstr) = SvNVX(sstr); | |
7927 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
7928 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
7929 | if (SvPVX(sstr) && SvLEN(sstr)) | |
7930 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
7931 | else | |
7932 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
7933 | CvSTASH(dstr) = hv_dup(CvSTASH(sstr));/* NOTE: not refcounted */ | |
7934 | CvSTART(dstr) = CvSTART(sstr); | |
7935 | CvROOT(dstr) = OpREFCNT_inc(CvROOT(sstr)); | |
7936 | CvXSUB(dstr) = CvXSUB(sstr); | |
7937 | CvXSUBANY(dstr) = CvXSUBANY(sstr); | |
7938 | CvGV(dstr) = gv_dup_inc(CvGV(sstr)); | |
7939 | CvDEPTH(dstr) = CvDEPTH(sstr); | |
7940 | if (CvPADLIST(sstr) && !AvREAL(CvPADLIST(sstr))) { | |
7941 | /* XXX padlists are real, but pretend to be not */ | |
7942 | AvREAL_on(CvPADLIST(sstr)); | |
7943 | CvPADLIST(dstr) = av_dup_inc(CvPADLIST(sstr)); | |
7944 | AvREAL_off(CvPADLIST(sstr)); | |
7945 | AvREAL_off(CvPADLIST(dstr)); | |
7946 | } | |
7947 | else | |
7948 | CvPADLIST(dstr) = av_dup_inc(CvPADLIST(sstr)); | |
7949 | CvOUTSIDE(dstr) = cv_dup_inc(CvOUTSIDE(sstr)); | |
7950 | CvFLAGS(dstr) = CvFLAGS(sstr); | |
7951 | break; | |
7952 | default: | |
7953 | Perl_croak(aTHX_ "Bizarre SvTYPE [%d]", SvTYPE(sstr)); | |
7954 | break; | |
7955 | } | |
7956 | ||
7957 | if (SvOBJECT(dstr) && SvTYPE(dstr) != SVt_PVIO) | |
7958 | ++PL_sv_objcount; | |
7959 | ||
7960 | return dstr; | |
7961 | } | |
7962 | ||
7963 | PERL_CONTEXT * | |
7964 | Perl_cx_dup(pTHX_ PERL_CONTEXT *cxs, I32 ix, I32 max) | |
7965 | { | |
7966 | PERL_CONTEXT *ncxs; | |
7967 | ||
7968 | if (!cxs) | |
7969 | return (PERL_CONTEXT*)NULL; | |
7970 | ||
7971 | /* look for it in the table first */ | |
7972 | ncxs = (PERL_CONTEXT*)ptr_table_fetch(PL_ptr_table, cxs); | |
7973 | if (ncxs) | |
7974 | return ncxs; | |
7975 | ||
7976 | /* create anew and remember what it is */ | |
7977 | Newz(56, ncxs, max + 1, PERL_CONTEXT); | |
7978 | ptr_table_store(PL_ptr_table, cxs, ncxs); | |
7979 | ||
7980 | while (ix >= 0) { | |
7981 | PERL_CONTEXT *cx = &cxs[ix]; | |
7982 | PERL_CONTEXT *ncx = &ncxs[ix]; | |
7983 | ncx->cx_type = cx->cx_type; | |
7984 | if (CxTYPE(cx) == CXt_SUBST) { | |
7985 | Perl_croak(aTHX_ "Cloning substitution context is unimplemented"); | |
7986 | } | |
7987 | else { | |
7988 | ncx->blk_oldsp = cx->blk_oldsp; | |
7989 | ncx->blk_oldcop = cx->blk_oldcop; | |
7990 | ncx->blk_oldretsp = cx->blk_oldretsp; | |
7991 | ncx->blk_oldmarksp = cx->blk_oldmarksp; | |
7992 | ncx->blk_oldscopesp = cx->blk_oldscopesp; | |
7993 | ncx->blk_oldpm = cx->blk_oldpm; | |
7994 | ncx->blk_gimme = cx->blk_gimme; | |
7995 | switch (CxTYPE(cx)) { | |
7996 | case CXt_SUB: | |
7997 | ncx->blk_sub.cv = (cx->blk_sub.olddepth == 0 | |
7998 | ? cv_dup_inc(cx->blk_sub.cv) | |
7999 | : cv_dup(cx->blk_sub.cv)); | |
8000 | ncx->blk_sub.argarray = (cx->blk_sub.hasargs | |
8001 | ? av_dup_inc(cx->blk_sub.argarray) | |
8002 | : Nullav); | |
8003 | ncx->blk_sub.savearray = av_dup(cx->blk_sub.savearray); | |
8004 | ncx->blk_sub.olddepth = cx->blk_sub.olddepth; | |
8005 | ncx->blk_sub.hasargs = cx->blk_sub.hasargs; | |
8006 | ncx->blk_sub.lval = cx->blk_sub.lval; | |
8007 | break; | |
8008 | case CXt_EVAL: | |
8009 | ncx->blk_eval.old_in_eval = cx->blk_eval.old_in_eval; | |
8010 | ncx->blk_eval.old_op_type = cx->blk_eval.old_op_type; | |
0f79a09d | 8011 | ncx->blk_eval.old_namesv = sv_dup_inc(cx->blk_eval.old_namesv); |
1d7c1841 GS |
8012 | ncx->blk_eval.old_eval_root = cx->blk_eval.old_eval_root; |
8013 | ncx->blk_eval.cur_text = sv_dup(cx->blk_eval.cur_text); | |
8014 | break; | |
8015 | case CXt_LOOP: | |
8016 | ncx->blk_loop.label = cx->blk_loop.label; | |
8017 | ncx->blk_loop.resetsp = cx->blk_loop.resetsp; | |
8018 | ncx->blk_loop.redo_op = cx->blk_loop.redo_op; | |
8019 | ncx->blk_loop.next_op = cx->blk_loop.next_op; | |
8020 | ncx->blk_loop.last_op = cx->blk_loop.last_op; | |
8021 | ncx->blk_loop.iterdata = (CxPADLOOP(cx) | |
8022 | ? cx->blk_loop.iterdata | |
8023 | : gv_dup((GV*)cx->blk_loop.iterdata)); | |
a4b82a6f GS |
8024 | ncx->blk_loop.oldcurpad |
8025 | = (SV**)ptr_table_fetch(PL_ptr_table, | |
8026 | cx->blk_loop.oldcurpad); | |
1d7c1841 GS |
8027 | ncx->blk_loop.itersave = sv_dup_inc(cx->blk_loop.itersave); |
8028 | ncx->blk_loop.iterlval = sv_dup_inc(cx->blk_loop.iterlval); | |
8029 | ncx->blk_loop.iterary = av_dup_inc(cx->blk_loop.iterary); | |
8030 | ncx->blk_loop.iterix = cx->blk_loop.iterix; | |
8031 | ncx->blk_loop.itermax = cx->blk_loop.itermax; | |
8032 | break; | |
8033 | case CXt_FORMAT: | |
8034 | ncx->blk_sub.cv = cv_dup(cx->blk_sub.cv); | |
8035 | ncx->blk_sub.gv = gv_dup(cx->blk_sub.gv); | |
8036 | ncx->blk_sub.dfoutgv = gv_dup_inc(cx->blk_sub.dfoutgv); | |
8037 | ncx->blk_sub.hasargs = cx->blk_sub.hasargs; | |
8038 | break; | |
8039 | case CXt_BLOCK: | |
8040 | case CXt_NULL: | |
8041 | break; | |
8042 | } | |
8043 | } | |
8044 | --ix; | |
8045 | } | |
8046 | return ncxs; | |
8047 | } | |
8048 | ||
8049 | PERL_SI * | |
8050 | Perl_si_dup(pTHX_ PERL_SI *si) | |
8051 | { | |
8052 | PERL_SI *nsi; | |
8053 | ||
8054 | if (!si) | |
8055 | return (PERL_SI*)NULL; | |
8056 | ||
8057 | /* look for it in the table first */ | |
8058 | nsi = (PERL_SI*)ptr_table_fetch(PL_ptr_table, si); | |
8059 | if (nsi) | |
8060 | return nsi; | |
8061 | ||
8062 | /* create anew and remember what it is */ | |
8063 | Newz(56, nsi, 1, PERL_SI); | |
8064 | ptr_table_store(PL_ptr_table, si, nsi); | |
8065 | ||
8066 | nsi->si_stack = av_dup_inc(si->si_stack); | |
8067 | nsi->si_cxix = si->si_cxix; | |
8068 | nsi->si_cxmax = si->si_cxmax; | |
8069 | nsi->si_cxstack = cx_dup(si->si_cxstack, si->si_cxix, si->si_cxmax); | |
8070 | nsi->si_type = si->si_type; | |
8071 | nsi->si_prev = si_dup(si->si_prev); | |
8072 | nsi->si_next = si_dup(si->si_next); | |
8073 | nsi->si_markoff = si->si_markoff; | |
8074 | ||
8075 | return nsi; | |
8076 | } | |
8077 | ||
8078 | #define POPINT(ss,ix) ((ss)[--(ix)].any_i32) | |
8079 | #define TOPINT(ss,ix) ((ss)[ix].any_i32) | |
8080 | #define POPLONG(ss,ix) ((ss)[--(ix)].any_long) | |
8081 | #define TOPLONG(ss,ix) ((ss)[ix].any_long) | |
8082 | #define POPIV(ss,ix) ((ss)[--(ix)].any_iv) | |
8083 | #define TOPIV(ss,ix) ((ss)[ix].any_iv) | |
8084 | #define POPPTR(ss,ix) ((ss)[--(ix)].any_ptr) | |
8085 | #define TOPPTR(ss,ix) ((ss)[ix].any_ptr) | |
8086 | #define POPDPTR(ss,ix) ((ss)[--(ix)].any_dptr) | |
8087 | #define TOPDPTR(ss,ix) ((ss)[ix].any_dptr) | |
8088 | #define POPDXPTR(ss,ix) ((ss)[--(ix)].any_dxptr) | |
8089 | #define TOPDXPTR(ss,ix) ((ss)[ix].any_dxptr) | |
8090 | ||
8091 | /* XXXXX todo */ | |
8092 | #define pv_dup_inc(p) SAVEPV(p) | |
8093 | #define pv_dup(p) SAVEPV(p) | |
8094 | #define svp_dup_inc(p,pp) any_dup(p,pp) | |
8095 | ||
8096 | void * | |
8097 | Perl_any_dup(pTHX_ void *v, PerlInterpreter *proto_perl) | |
8098 | { | |
8099 | void *ret; | |
8100 | ||
8101 | if (!v) | |
8102 | return (void*)NULL; | |
8103 | ||
8104 | /* look for it in the table first */ | |
8105 | ret = ptr_table_fetch(PL_ptr_table, v); | |
8106 | if (ret) | |
8107 | return ret; | |
8108 | ||
8109 | /* see if it is part of the interpreter structure */ | |
8110 | if (v >= (void*)proto_perl && v < (void*)(proto_perl+1)) | |
8111 | ret = (void*)(((char*)aTHXo) + (((char*)v) - (char*)proto_perl)); | |
8112 | else | |
8113 | ret = v; | |
8114 | ||
8115 | return ret; | |
8116 | } | |
8117 | ||
8118 | ANY * | |
8119 | Perl_ss_dup(pTHX_ PerlInterpreter *proto_perl) | |
8120 | { | |
8121 | ANY *ss = proto_perl->Tsavestack; | |
8122 | I32 ix = proto_perl->Tsavestack_ix; | |
8123 | I32 max = proto_perl->Tsavestack_max; | |
8124 | ANY *nss; | |
8125 | SV *sv; | |
8126 | GV *gv; | |
8127 | AV *av; | |
8128 | HV *hv; | |
8129 | void* ptr; | |
8130 | int intval; | |
8131 | long longval; | |
8132 | GP *gp; | |
8133 | IV iv; | |
8134 | I32 i; | |
8135 | char *c; | |
8136 | void (*dptr) (void*); | |
8137 | void (*dxptr) (pTHXo_ void*); | |
e977893f | 8138 | OP *o; |
1d7c1841 GS |
8139 | |
8140 | Newz(54, nss, max, ANY); | |
8141 | ||
8142 | while (ix > 0) { | |
8143 | i = POPINT(ss,ix); | |
8144 | TOPINT(nss,ix) = i; | |
8145 | switch (i) { | |
8146 | case SAVEt_ITEM: /* normal string */ | |
8147 | sv = (SV*)POPPTR(ss,ix); | |
8148 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8149 | sv = (SV*)POPPTR(ss,ix); | |
8150 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8151 | break; | |
8152 | case SAVEt_SV: /* scalar reference */ | |
8153 | sv = (SV*)POPPTR(ss,ix); | |
8154 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8155 | gv = (GV*)POPPTR(ss,ix); | |
8156 | TOPPTR(nss,ix) = gv_dup_inc(gv); | |
8157 | break; | |
f4dd75d9 GS |
8158 | case SAVEt_GENERIC_PVREF: /* generic char* */ |
8159 | c = (char*)POPPTR(ss,ix); | |
8160 | TOPPTR(nss,ix) = pv_dup(c); | |
8161 | ptr = POPPTR(ss,ix); | |
8162 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8163 | break; | |
1d7c1841 GS |
8164 | case SAVEt_GENERIC_SVREF: /* generic sv */ |
8165 | case SAVEt_SVREF: /* scalar reference */ | |
8166 | sv = (SV*)POPPTR(ss,ix); | |
8167 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8168 | ptr = POPPTR(ss,ix); | |
8169 | TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */ | |
8170 | break; | |
8171 | case SAVEt_AV: /* array reference */ | |
8172 | av = (AV*)POPPTR(ss,ix); | |
8173 | TOPPTR(nss,ix) = av_dup_inc(av); | |
8174 | gv = (GV*)POPPTR(ss,ix); | |
8175 | TOPPTR(nss,ix) = gv_dup(gv); | |
8176 | break; | |
8177 | case SAVEt_HV: /* hash reference */ | |
8178 | hv = (HV*)POPPTR(ss,ix); | |
8179 | TOPPTR(nss,ix) = hv_dup_inc(hv); | |
8180 | gv = (GV*)POPPTR(ss,ix); | |
8181 | TOPPTR(nss,ix) = gv_dup(gv); | |
8182 | break; | |
8183 | case SAVEt_INT: /* int reference */ | |
8184 | ptr = POPPTR(ss,ix); | |
8185 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8186 | intval = (int)POPINT(ss,ix); | |
8187 | TOPINT(nss,ix) = intval; | |
8188 | break; | |
8189 | case SAVEt_LONG: /* long reference */ | |
8190 | ptr = POPPTR(ss,ix); | |
8191 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8192 | longval = (long)POPLONG(ss,ix); | |
8193 | TOPLONG(nss,ix) = longval; | |
8194 | break; | |
8195 | case SAVEt_I32: /* I32 reference */ | |
8196 | case SAVEt_I16: /* I16 reference */ | |
8197 | case SAVEt_I8: /* I8 reference */ | |
8198 | ptr = POPPTR(ss,ix); | |
8199 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8200 | i = POPINT(ss,ix); | |
8201 | TOPINT(nss,ix) = i; | |
8202 | break; | |
8203 | case SAVEt_IV: /* IV reference */ | |
8204 | ptr = POPPTR(ss,ix); | |
8205 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8206 | iv = POPIV(ss,ix); | |
8207 | TOPIV(nss,ix) = iv; | |
8208 | break; | |
8209 | case SAVEt_SPTR: /* SV* reference */ | |
8210 | ptr = POPPTR(ss,ix); | |
8211 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8212 | sv = (SV*)POPPTR(ss,ix); | |
8213 | TOPPTR(nss,ix) = sv_dup(sv); | |
8214 | break; | |
8215 | case SAVEt_VPTR: /* random* reference */ | |
8216 | ptr = POPPTR(ss,ix); | |
8217 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8218 | ptr = POPPTR(ss,ix); | |
8219 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8220 | break; | |
8221 | case SAVEt_PPTR: /* char* reference */ | |
8222 | ptr = POPPTR(ss,ix); | |
8223 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8224 | c = (char*)POPPTR(ss,ix); | |
8225 | TOPPTR(nss,ix) = pv_dup(c); | |
8226 | break; | |
8227 | case SAVEt_HPTR: /* HV* reference */ | |
8228 | ptr = POPPTR(ss,ix); | |
8229 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8230 | hv = (HV*)POPPTR(ss,ix); | |
8231 | TOPPTR(nss,ix) = hv_dup(hv); | |
8232 | break; | |
8233 | case SAVEt_APTR: /* AV* reference */ | |
8234 | ptr = POPPTR(ss,ix); | |
8235 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8236 | av = (AV*)POPPTR(ss,ix); | |
8237 | TOPPTR(nss,ix) = av_dup(av); | |
8238 | break; | |
8239 | case SAVEt_NSTAB: | |
8240 | gv = (GV*)POPPTR(ss,ix); | |
8241 | TOPPTR(nss,ix) = gv_dup(gv); | |
8242 | break; | |
8243 | case SAVEt_GP: /* scalar reference */ | |
8244 | gp = (GP*)POPPTR(ss,ix); | |
8245 | TOPPTR(nss,ix) = gp = gp_dup(gp); | |
8246 | (void)GpREFCNT_inc(gp); | |
8247 | gv = (GV*)POPPTR(ss,ix); | |
8248 | TOPPTR(nss,ix) = gv_dup_inc(c); | |
8249 | c = (char*)POPPTR(ss,ix); | |
8250 | TOPPTR(nss,ix) = pv_dup(c); | |
8251 | iv = POPIV(ss,ix); | |
8252 | TOPIV(nss,ix) = iv; | |
8253 | iv = POPIV(ss,ix); | |
8254 | TOPIV(nss,ix) = iv; | |
8255 | break; | |
8256 | case SAVEt_FREESV: | |
8257 | sv = (SV*)POPPTR(ss,ix); | |
8258 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8259 | break; | |
8260 | case SAVEt_FREEOP: | |
8261 | ptr = POPPTR(ss,ix); | |
8262 | if (ptr && (((OP*)ptr)->op_private & OPpREFCOUNTED)) { | |
8263 | /* these are assumed to be refcounted properly */ | |
8264 | switch (((OP*)ptr)->op_type) { | |
8265 | case OP_LEAVESUB: | |
8266 | case OP_LEAVESUBLV: | |
8267 | case OP_LEAVEEVAL: | |
8268 | case OP_LEAVE: | |
8269 | case OP_SCOPE: | |
8270 | case OP_LEAVEWRITE: | |
e977893f GS |
8271 | TOPPTR(nss,ix) = ptr; |
8272 | o = (OP*)ptr; | |
8273 | OpREFCNT_inc(o); | |
1d7c1841 GS |
8274 | break; |
8275 | default: | |
8276 | TOPPTR(nss,ix) = Nullop; | |
8277 | break; | |
8278 | } | |
8279 | } | |
8280 | else | |
8281 | TOPPTR(nss,ix) = Nullop; | |
8282 | break; | |
8283 | case SAVEt_FREEPV: | |
8284 | c = (char*)POPPTR(ss,ix); | |
8285 | TOPPTR(nss,ix) = pv_dup_inc(c); | |
8286 | break; | |
8287 | case SAVEt_CLEARSV: | |
8288 | longval = POPLONG(ss,ix); | |
8289 | TOPLONG(nss,ix) = longval; | |
8290 | break; | |
8291 | case SAVEt_DELETE: | |
8292 | hv = (HV*)POPPTR(ss,ix); | |
8293 | TOPPTR(nss,ix) = hv_dup_inc(hv); | |
8294 | c = (char*)POPPTR(ss,ix); | |
8295 | TOPPTR(nss,ix) = pv_dup_inc(c); | |
8296 | i = POPINT(ss,ix); | |
8297 | TOPINT(nss,ix) = i; | |
8298 | break; | |
8299 | case SAVEt_DESTRUCTOR: | |
8300 | ptr = POPPTR(ss,ix); | |
8301 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */ | |
8302 | dptr = POPDPTR(ss,ix); | |
ef75a179 | 8303 | TOPDPTR(nss,ix) = (void (*)(void*))any_dup((void *)dptr, proto_perl); |
1d7c1841 GS |
8304 | break; |
8305 | case SAVEt_DESTRUCTOR_X: | |
8306 | ptr = POPPTR(ss,ix); | |
8307 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */ | |
8308 | dxptr = POPDXPTR(ss,ix); | |
ef75a179 | 8309 | TOPDXPTR(nss,ix) = (void (*)(pTHXo_ void*))any_dup((void *)dxptr, proto_perl); |
1d7c1841 GS |
8310 | break; |
8311 | case SAVEt_REGCONTEXT: | |
8312 | case SAVEt_ALLOC: | |
8313 | i = POPINT(ss,ix); | |
8314 | TOPINT(nss,ix) = i; | |
8315 | ix -= i; | |
8316 | break; | |
8317 | case SAVEt_STACK_POS: /* Position on Perl stack */ | |
8318 | i = POPINT(ss,ix); | |
8319 | TOPINT(nss,ix) = i; | |
8320 | break; | |
8321 | case SAVEt_AELEM: /* array element */ | |
8322 | sv = (SV*)POPPTR(ss,ix); | |
8323 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8324 | i = POPINT(ss,ix); | |
8325 | TOPINT(nss,ix) = i; | |
8326 | av = (AV*)POPPTR(ss,ix); | |
8327 | TOPPTR(nss,ix) = av_dup_inc(av); | |
8328 | break; | |
8329 | case SAVEt_HELEM: /* hash element */ | |
8330 | sv = (SV*)POPPTR(ss,ix); | |
8331 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8332 | sv = (SV*)POPPTR(ss,ix); | |
8333 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8334 | hv = (HV*)POPPTR(ss,ix); | |
8335 | TOPPTR(nss,ix) = hv_dup_inc(hv); | |
8336 | break; | |
8337 | case SAVEt_OP: | |
8338 | ptr = POPPTR(ss,ix); | |
8339 | TOPPTR(nss,ix) = ptr; | |
8340 | break; | |
8341 | case SAVEt_HINTS: | |
8342 | i = POPINT(ss,ix); | |
8343 | TOPINT(nss,ix) = i; | |
8344 | break; | |
c4410b1b GS |
8345 | case SAVEt_COMPPAD: |
8346 | av = (AV*)POPPTR(ss,ix); | |
8347 | TOPPTR(nss,ix) = av_dup(av); | |
8348 | break; | |
c3564e5c GS |
8349 | case SAVEt_PADSV: |
8350 | longval = (long)POPLONG(ss,ix); | |
8351 | TOPLONG(nss,ix) = longval; | |
8352 | ptr = POPPTR(ss,ix); | |
8353 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8354 | sv = (SV*)POPPTR(ss,ix); | |
8355 | TOPPTR(nss,ix) = sv_dup(sv); | |
8356 | break; | |
1d7c1841 GS |
8357 | default: |
8358 | Perl_croak(aTHX_ "panic: ss_dup inconsistency"); | |
8359 | } | |
8360 | } | |
8361 | ||
8362 | return nss; | |
8363 | } | |
8364 | ||
8365 | #ifdef PERL_OBJECT | |
8366 | #include "XSUB.h" | |
8367 | #endif | |
8368 | ||
8369 | PerlInterpreter * | |
8370 | perl_clone(PerlInterpreter *proto_perl, UV flags) | |
8371 | { | |
8372 | #ifdef PERL_OBJECT | |
8373 | CPerlObj *pPerl = (CPerlObj*)proto_perl; | |
8374 | #endif | |
8375 | ||
8376 | #ifdef PERL_IMPLICIT_SYS | |
8377 | return perl_clone_using(proto_perl, flags, | |
8378 | proto_perl->IMem, | |
8379 | proto_perl->IMemShared, | |
8380 | proto_perl->IMemParse, | |
8381 | proto_perl->IEnv, | |
8382 | proto_perl->IStdIO, | |
8383 | proto_perl->ILIO, | |
8384 | proto_perl->IDir, | |
8385 | proto_perl->ISock, | |
8386 | proto_perl->IProc); | |
8387 | } | |
8388 | ||
8389 | PerlInterpreter * | |
8390 | perl_clone_using(PerlInterpreter *proto_perl, UV flags, | |
8391 | struct IPerlMem* ipM, struct IPerlMem* ipMS, | |
8392 | struct IPerlMem* ipMP, struct IPerlEnv* ipE, | |
8393 | struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO, | |
8394 | struct IPerlDir* ipD, struct IPerlSock* ipS, | |
8395 | struct IPerlProc* ipP) | |
8396 | { | |
8397 | /* XXX many of the string copies here can be optimized if they're | |
8398 | * constants; they need to be allocated as common memory and just | |
8399 | * their pointers copied. */ | |
8400 | ||
8401 | IV i; | |
1d7c1841 GS |
8402 | # ifdef PERL_OBJECT |
8403 | CPerlObj *pPerl = new(ipM) CPerlObj(ipM, ipMS, ipMP, ipE, ipStd, ipLIO, | |
8404 | ipD, ipS, ipP); | |
ba869deb | 8405 | PERL_SET_THX(pPerl); |
1d7c1841 GS |
8406 | # else /* !PERL_OBJECT */ |
8407 | PerlInterpreter *my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter)); | |
ba869deb | 8408 | PERL_SET_THX(my_perl); |
1d7c1841 GS |
8409 | |
8410 | # ifdef DEBUGGING | |
8411 | memset(my_perl, 0xab, sizeof(PerlInterpreter)); | |
8412 | PL_markstack = 0; | |
8413 | PL_scopestack = 0; | |
8414 | PL_savestack = 0; | |
8415 | PL_retstack = 0; | |
8416 | # else /* !DEBUGGING */ | |
8417 | Zero(my_perl, 1, PerlInterpreter); | |
8418 | # endif /* DEBUGGING */ | |
8419 | ||
8420 | /* host pointers */ | |
8421 | PL_Mem = ipM; | |
8422 | PL_MemShared = ipMS; | |
8423 | PL_MemParse = ipMP; | |
8424 | PL_Env = ipE; | |
8425 | PL_StdIO = ipStd; | |
8426 | PL_LIO = ipLIO; | |
8427 | PL_Dir = ipD; | |
8428 | PL_Sock = ipS; | |
8429 | PL_Proc = ipP; | |
8430 | # endif /* PERL_OBJECT */ | |
8431 | #else /* !PERL_IMPLICIT_SYS */ | |
8432 | IV i; | |
1d7c1841 | 8433 | PerlInterpreter *my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter)); |
ba869deb | 8434 | PERL_SET_THX(my_perl); |
1d7c1841 GS |
8435 | |
8436 | # ifdef DEBUGGING | |
8437 | memset(my_perl, 0xab, sizeof(PerlInterpreter)); | |
8438 | PL_markstack = 0; | |
8439 | PL_scopestack = 0; | |
8440 | PL_savestack = 0; | |
8441 | PL_retstack = 0; | |
8442 | # else /* !DEBUGGING */ | |
8443 | Zero(my_perl, 1, PerlInterpreter); | |
8444 | # endif /* DEBUGGING */ | |
8445 | #endif /* PERL_IMPLICIT_SYS */ | |
8446 | ||
8447 | /* arena roots */ | |
8448 | PL_xiv_arenaroot = NULL; | |
8449 | PL_xiv_root = NULL; | |
612f20c3 | 8450 | PL_xnv_arenaroot = NULL; |
1d7c1841 | 8451 | PL_xnv_root = NULL; |
612f20c3 | 8452 | PL_xrv_arenaroot = NULL; |
1d7c1841 | 8453 | PL_xrv_root = NULL; |
612f20c3 | 8454 | PL_xpv_arenaroot = NULL; |
1d7c1841 | 8455 | PL_xpv_root = NULL; |
612f20c3 | 8456 | PL_xpviv_arenaroot = NULL; |
1d7c1841 | 8457 | PL_xpviv_root = NULL; |
612f20c3 | 8458 | PL_xpvnv_arenaroot = NULL; |
1d7c1841 | 8459 | PL_xpvnv_root = NULL; |
612f20c3 | 8460 | PL_xpvcv_arenaroot = NULL; |
1d7c1841 | 8461 | PL_xpvcv_root = NULL; |
612f20c3 | 8462 | PL_xpvav_arenaroot = NULL; |
1d7c1841 | 8463 | PL_xpvav_root = NULL; |
612f20c3 | 8464 | PL_xpvhv_arenaroot = NULL; |
1d7c1841 | 8465 | PL_xpvhv_root = NULL; |
612f20c3 | 8466 | PL_xpvmg_arenaroot = NULL; |
1d7c1841 | 8467 | PL_xpvmg_root = NULL; |
612f20c3 | 8468 | PL_xpvlv_arenaroot = NULL; |
1d7c1841 | 8469 | PL_xpvlv_root = NULL; |
612f20c3 | 8470 | PL_xpvbm_arenaroot = NULL; |
1d7c1841 | 8471 | PL_xpvbm_root = NULL; |
612f20c3 | 8472 | PL_he_arenaroot = NULL; |
1d7c1841 GS |
8473 | PL_he_root = NULL; |
8474 | PL_nice_chunk = NULL; | |
8475 | PL_nice_chunk_size = 0; | |
8476 | PL_sv_count = 0; | |
8477 | PL_sv_objcount = 0; | |
8478 | PL_sv_root = Nullsv; | |
8479 | PL_sv_arenaroot = Nullsv; | |
8480 | ||
8481 | PL_debug = proto_perl->Idebug; | |
8482 | ||
8483 | /* create SV map for pointer relocation */ | |
8484 | PL_ptr_table = ptr_table_new(); | |
8485 | ||
8486 | /* initialize these special pointers as early as possible */ | |
8487 | SvANY(&PL_sv_undef) = NULL; | |
8488 | SvREFCNT(&PL_sv_undef) = (~(U32)0)/2; | |
8489 | SvFLAGS(&PL_sv_undef) = SVf_READONLY|SVt_NULL; | |
8490 | ptr_table_store(PL_ptr_table, &proto_perl->Isv_undef, &PL_sv_undef); | |
8491 | ||
8492 | #ifdef PERL_OBJECT | |
8493 | SvUPGRADE(&PL_sv_no, SVt_PVNV); | |
8494 | #else | |
8495 | SvANY(&PL_sv_no) = new_XPVNV(); | |
8496 | #endif | |
8497 | SvREFCNT(&PL_sv_no) = (~(U32)0)/2; | |
8498 | SvFLAGS(&PL_sv_no) = SVp_NOK|SVf_NOK|SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV; | |
8499 | SvPVX(&PL_sv_no) = SAVEPVN(PL_No, 0); | |
8500 | SvCUR(&PL_sv_no) = 0; | |
8501 | SvLEN(&PL_sv_no) = 1; | |
8502 | SvNVX(&PL_sv_no) = 0; | |
8503 | ptr_table_store(PL_ptr_table, &proto_perl->Isv_no, &PL_sv_no); | |
8504 | ||
8505 | #ifdef PERL_OBJECT | |
8506 | SvUPGRADE(&PL_sv_yes, SVt_PVNV); | |
8507 | #else | |
8508 | SvANY(&PL_sv_yes) = new_XPVNV(); | |
8509 | #endif | |
8510 | SvREFCNT(&PL_sv_yes) = (~(U32)0)/2; | |
8511 | SvFLAGS(&PL_sv_yes) = SVp_NOK|SVf_NOK|SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV; | |
8512 | SvPVX(&PL_sv_yes) = SAVEPVN(PL_Yes, 1); | |
8513 | SvCUR(&PL_sv_yes) = 1; | |
8514 | SvLEN(&PL_sv_yes) = 2; | |
8515 | SvNVX(&PL_sv_yes) = 1; | |
8516 | ptr_table_store(PL_ptr_table, &proto_perl->Isv_yes, &PL_sv_yes); | |
8517 | ||
8518 | /* create shared string table */ | |
8519 | PL_strtab = newHV(); | |
8520 | HvSHAREKEYS_off(PL_strtab); | |
8521 | hv_ksplit(PL_strtab, 512); | |
8522 | ptr_table_store(PL_ptr_table, proto_perl->Istrtab, PL_strtab); | |
8523 | ||
8524 | PL_compiling = proto_perl->Icompiling; | |
8525 | PL_compiling.cop_stashpv = SAVEPV(PL_compiling.cop_stashpv); | |
8526 | PL_compiling.cop_file = SAVEPV(PL_compiling.cop_file); | |
8527 | ptr_table_store(PL_ptr_table, &proto_perl->Icompiling, &PL_compiling); | |
8528 | if (!specialWARN(PL_compiling.cop_warnings)) | |
8529 | PL_compiling.cop_warnings = sv_dup_inc(PL_compiling.cop_warnings); | |
ac27b0f5 NIS |
8530 | if (!specialCopIO(PL_compiling.cop_io)) |
8531 | PL_compiling.cop_io = sv_dup_inc(PL_compiling.cop_io); | |
1d7c1841 GS |
8532 | PL_curcop = (COP*)any_dup(proto_perl->Tcurcop, proto_perl); |
8533 | ||
8534 | /* pseudo environmental stuff */ | |
8535 | PL_origargc = proto_perl->Iorigargc; | |
8536 | i = PL_origargc; | |
8537 | New(0, PL_origargv, i+1, char*); | |
8538 | PL_origargv[i] = '\0'; | |
8539 | while (i-- > 0) { | |
8540 | PL_origargv[i] = SAVEPV(proto_perl->Iorigargv[i]); | |
8541 | } | |
8542 | PL_envgv = gv_dup(proto_perl->Ienvgv); | |
8543 | PL_incgv = gv_dup(proto_perl->Iincgv); | |
8544 | PL_hintgv = gv_dup(proto_perl->Ihintgv); | |
8545 | PL_origfilename = SAVEPV(proto_perl->Iorigfilename); | |
8546 | PL_diehook = sv_dup_inc(proto_perl->Idiehook); | |
8547 | PL_warnhook = sv_dup_inc(proto_perl->Iwarnhook); | |
8548 | ||
8549 | /* switches */ | |
8550 | PL_minus_c = proto_perl->Iminus_c; | |
a7cb1f99 | 8551 | PL_patchlevel = sv_dup_inc(proto_perl->Ipatchlevel); |
1d7c1841 GS |
8552 | PL_localpatches = proto_perl->Ilocalpatches; |
8553 | PL_splitstr = proto_perl->Isplitstr; | |
8554 | PL_preprocess = proto_perl->Ipreprocess; | |
8555 | PL_minus_n = proto_perl->Iminus_n; | |
8556 | PL_minus_p = proto_perl->Iminus_p; | |
8557 | PL_minus_l = proto_perl->Iminus_l; | |
8558 | PL_minus_a = proto_perl->Iminus_a; | |
8559 | PL_minus_F = proto_perl->Iminus_F; | |
8560 | PL_doswitches = proto_perl->Idoswitches; | |
8561 | PL_dowarn = proto_perl->Idowarn; | |
8562 | PL_doextract = proto_perl->Idoextract; | |
8563 | PL_sawampersand = proto_perl->Isawampersand; | |
8564 | PL_unsafe = proto_perl->Iunsafe; | |
8565 | PL_inplace = SAVEPV(proto_perl->Iinplace); | |
8566 | PL_e_script = sv_dup_inc(proto_perl->Ie_script); | |
8567 | PL_perldb = proto_perl->Iperldb; | |
8568 | PL_perl_destruct_level = proto_perl->Iperl_destruct_level; | |
8569 | ||
8570 | /* magical thingies */ | |
8571 | /* XXX time(&PL_basetime) when asked for? */ | |
8572 | PL_basetime = proto_perl->Ibasetime; | |
8573 | PL_formfeed = sv_dup(proto_perl->Iformfeed); | |
8574 | ||
8575 | PL_maxsysfd = proto_perl->Imaxsysfd; | |
8576 | PL_multiline = proto_perl->Imultiline; | |
8577 | PL_statusvalue = proto_perl->Istatusvalue; | |
8578 | #ifdef VMS | |
8579 | PL_statusvalue_vms = proto_perl->Istatusvalue_vms; | |
8580 | #endif | |
8581 | ||
8582 | /* shortcuts to various I/O objects */ | |
8583 | PL_stdingv = gv_dup(proto_perl->Istdingv); | |
8584 | PL_stderrgv = gv_dup(proto_perl->Istderrgv); | |
8585 | PL_defgv = gv_dup(proto_perl->Idefgv); | |
8586 | PL_argvgv = gv_dup(proto_perl->Iargvgv); | |
8587 | PL_argvoutgv = gv_dup(proto_perl->Iargvoutgv); | |
8588 | PL_argvout_stack = av_dup(proto_perl->Iargvout_stack); | |
8589 | ||
8590 | /* shortcuts to regexp stuff */ | |
8591 | PL_replgv = gv_dup(proto_perl->Ireplgv); | |
8592 | ||
8593 | /* shortcuts to misc objects */ | |
8594 | PL_errgv = gv_dup(proto_perl->Ierrgv); | |
8595 | ||
8596 | /* shortcuts to debugging objects */ | |
8597 | PL_DBgv = gv_dup(proto_perl->IDBgv); | |
8598 | PL_DBline = gv_dup(proto_perl->IDBline); | |
8599 | PL_DBsub = gv_dup(proto_perl->IDBsub); | |
8600 | PL_DBsingle = sv_dup(proto_perl->IDBsingle); | |
8601 | PL_DBtrace = sv_dup(proto_perl->IDBtrace); | |
8602 | PL_DBsignal = sv_dup(proto_perl->IDBsignal); | |
8603 | PL_lineary = av_dup(proto_perl->Ilineary); | |
8604 | PL_dbargs = av_dup(proto_perl->Idbargs); | |
8605 | ||
8606 | /* symbol tables */ | |
8607 | PL_defstash = hv_dup_inc(proto_perl->Tdefstash); | |
8608 | PL_curstash = hv_dup(proto_perl->Tcurstash); | |
8609 | PL_debstash = hv_dup(proto_perl->Idebstash); | |
8610 | PL_globalstash = hv_dup(proto_perl->Iglobalstash); | |
8611 | PL_curstname = sv_dup_inc(proto_perl->Icurstname); | |
8612 | ||
8613 | PL_beginav = av_dup_inc(proto_perl->Ibeginav); | |
8614 | PL_endav = av_dup_inc(proto_perl->Iendav); | |
7d30b5c4 | 8615 | PL_checkav = av_dup_inc(proto_perl->Icheckav); |
1d7c1841 GS |
8616 | PL_initav = av_dup_inc(proto_perl->Iinitav); |
8617 | ||
8618 | PL_sub_generation = proto_perl->Isub_generation; | |
8619 | ||
8620 | /* funky return mechanisms */ | |
8621 | PL_forkprocess = proto_perl->Iforkprocess; | |
8622 | ||
8623 | /* subprocess state */ | |
8624 | PL_fdpid = av_dup_inc(proto_perl->Ifdpid); | |
8625 | ||
8626 | /* internal state */ | |
8627 | PL_tainting = proto_perl->Itainting; | |
8628 | PL_maxo = proto_perl->Imaxo; | |
8629 | if (proto_perl->Iop_mask) | |
8630 | PL_op_mask = SAVEPVN(proto_perl->Iop_mask, PL_maxo); | |
8631 | else | |
8632 | PL_op_mask = Nullch; | |
8633 | ||
8634 | /* current interpreter roots */ | |
8635 | PL_main_cv = cv_dup_inc(proto_perl->Imain_cv); | |
8636 | PL_main_root = OpREFCNT_inc(proto_perl->Imain_root); | |
8637 | PL_main_start = proto_perl->Imain_start; | |
e977893f | 8638 | PL_eval_root = proto_perl->Ieval_root; |
1d7c1841 GS |
8639 | PL_eval_start = proto_perl->Ieval_start; |
8640 | ||
8641 | /* runtime control stuff */ | |
8642 | PL_curcopdb = (COP*)any_dup(proto_perl->Icurcopdb, proto_perl); | |
8643 | PL_copline = proto_perl->Icopline; | |
8644 | ||
8645 | PL_filemode = proto_perl->Ifilemode; | |
8646 | PL_lastfd = proto_perl->Ilastfd; | |
8647 | PL_oldname = proto_perl->Ioldname; /* XXX not quite right */ | |
8648 | PL_Argv = NULL; | |
8649 | PL_Cmd = Nullch; | |
8650 | PL_gensym = proto_perl->Igensym; | |
8651 | PL_preambled = proto_perl->Ipreambled; | |
8652 | PL_preambleav = av_dup_inc(proto_perl->Ipreambleav); | |
8653 | PL_laststatval = proto_perl->Ilaststatval; | |
8654 | PL_laststype = proto_perl->Ilaststype; | |
8655 | PL_mess_sv = Nullsv; | |
8656 | ||
7889fe52 | 8657 | PL_ors_sv = sv_dup_inc(proto_perl->Iors_sv); |
1d7c1841 GS |
8658 | PL_ofmt = SAVEPV(proto_perl->Iofmt); |
8659 | ||
8660 | /* interpreter atexit processing */ | |
8661 | PL_exitlistlen = proto_perl->Iexitlistlen; | |
8662 | if (PL_exitlistlen) { | |
8663 | New(0, PL_exitlist, PL_exitlistlen, PerlExitListEntry); | |
8664 | Copy(proto_perl->Iexitlist, PL_exitlist, PL_exitlistlen, PerlExitListEntry); | |
8665 | } | |
8666 | else | |
8667 | PL_exitlist = (PerlExitListEntry*)NULL; | |
8668 | PL_modglobal = hv_dup_inc(proto_perl->Imodglobal); | |
8669 | ||
8670 | PL_profiledata = NULL; | |
8671 | PL_rsfp = fp_dup(proto_perl->Irsfp, '<'); | |
8672 | /* PL_rsfp_filters entries have fake IoDIRP() */ | |
8673 | PL_rsfp_filters = av_dup_inc(proto_perl->Irsfp_filters); | |
8674 | ||
8675 | PL_compcv = cv_dup(proto_perl->Icompcv); | |
8676 | PL_comppad = av_dup(proto_perl->Icomppad); | |
8677 | PL_comppad_name = av_dup(proto_perl->Icomppad_name); | |
8678 | PL_comppad_name_fill = proto_perl->Icomppad_name_fill; | |
8679 | PL_comppad_name_floor = proto_perl->Icomppad_name_floor; | |
8680 | PL_curpad = (SV**)ptr_table_fetch(PL_ptr_table, | |
8681 | proto_perl->Tcurpad); | |
8682 | ||
8683 | #ifdef HAVE_INTERP_INTERN | |
8684 | sys_intern_dup(&proto_perl->Isys_intern, &PL_sys_intern); | |
8685 | #endif | |
8686 | ||
8687 | /* more statics moved here */ | |
8688 | PL_generation = proto_perl->Igeneration; | |
8689 | PL_DBcv = cv_dup(proto_perl->IDBcv); | |
1d7c1841 GS |
8690 | |
8691 | PL_in_clean_objs = proto_perl->Iin_clean_objs; | |
8692 | PL_in_clean_all = proto_perl->Iin_clean_all; | |
8693 | ||
8694 | PL_uid = proto_perl->Iuid; | |
8695 | PL_euid = proto_perl->Ieuid; | |
8696 | PL_gid = proto_perl->Igid; | |
8697 | PL_egid = proto_perl->Iegid; | |
8698 | PL_nomemok = proto_perl->Inomemok; | |
8699 | PL_an = proto_perl->Ian; | |
8700 | PL_cop_seqmax = proto_perl->Icop_seqmax; | |
8701 | PL_op_seqmax = proto_perl->Iop_seqmax; | |
8702 | PL_evalseq = proto_perl->Ievalseq; | |
8703 | PL_origenviron = proto_perl->Iorigenviron; /* XXX not quite right */ | |
8704 | PL_origalen = proto_perl->Iorigalen; | |
8705 | PL_pidstatus = newHV(); /* XXX flag for cloning? */ | |
8706 | PL_osname = SAVEPV(proto_perl->Iosname); | |
8707 | PL_sh_path = SAVEPV(proto_perl->Ish_path); | |
8708 | PL_sighandlerp = proto_perl->Isighandlerp; | |
8709 | ||
8710 | ||
8711 | PL_runops = proto_perl->Irunops; | |
8712 | ||
8713 | Copy(proto_perl->Itokenbuf, PL_tokenbuf, 256, char); | |
8714 | ||
8715 | #ifdef CSH | |
8716 | PL_cshlen = proto_perl->Icshlen; | |
8717 | PL_cshname = SAVEPVN(proto_perl->Icshname, PL_cshlen); | |
8718 | #endif | |
8719 | ||
8720 | PL_lex_state = proto_perl->Ilex_state; | |
8721 | PL_lex_defer = proto_perl->Ilex_defer; | |
8722 | PL_lex_expect = proto_perl->Ilex_expect; | |
8723 | PL_lex_formbrack = proto_perl->Ilex_formbrack; | |
8724 | PL_lex_dojoin = proto_perl->Ilex_dojoin; | |
8725 | PL_lex_starts = proto_perl->Ilex_starts; | |
8726 | PL_lex_stuff = sv_dup_inc(proto_perl->Ilex_stuff); | |
8727 | PL_lex_repl = sv_dup_inc(proto_perl->Ilex_repl); | |
8728 | PL_lex_op = proto_perl->Ilex_op; | |
8729 | PL_lex_inpat = proto_perl->Ilex_inpat; | |
8730 | PL_lex_inwhat = proto_perl->Ilex_inwhat; | |
8731 | PL_lex_brackets = proto_perl->Ilex_brackets; | |
8732 | i = (PL_lex_brackets < 120 ? 120 : PL_lex_brackets); | |
8733 | PL_lex_brackstack = SAVEPVN(proto_perl->Ilex_brackstack,i); | |
8734 | PL_lex_casemods = proto_perl->Ilex_casemods; | |
8735 | i = (PL_lex_casemods < 12 ? 12 : PL_lex_casemods); | |
8736 | PL_lex_casestack = SAVEPVN(proto_perl->Ilex_casestack,i); | |
8737 | ||
8738 | Copy(proto_perl->Inextval, PL_nextval, 5, YYSTYPE); | |
8739 | Copy(proto_perl->Inexttype, PL_nexttype, 5, I32); | |
8740 | PL_nexttoke = proto_perl->Inexttoke; | |
8741 | ||
8742 | PL_linestr = sv_dup_inc(proto_perl->Ilinestr); | |
8743 | i = proto_perl->Ibufptr - SvPVX(proto_perl->Ilinestr); | |
8744 | PL_bufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
8745 | i = proto_perl->Ioldbufptr - SvPVX(proto_perl->Ilinestr); | |
8746 | PL_oldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
8747 | i = proto_perl->Ioldoldbufptr - SvPVX(proto_perl->Ilinestr); | |
8748 | PL_oldoldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
8749 | PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr); | |
8750 | i = proto_perl->Ilinestart - SvPVX(proto_perl->Ilinestr); | |
8751 | PL_linestart = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
8752 | PL_pending_ident = proto_perl->Ipending_ident; | |
8753 | PL_sublex_info = proto_perl->Isublex_info; /* XXX not quite right */ | |
8754 | ||
8755 | PL_expect = proto_perl->Iexpect; | |
8756 | ||
8757 | PL_multi_start = proto_perl->Imulti_start; | |
8758 | PL_multi_end = proto_perl->Imulti_end; | |
8759 | PL_multi_open = proto_perl->Imulti_open; | |
8760 | PL_multi_close = proto_perl->Imulti_close; | |
8761 | ||
8762 | PL_error_count = proto_perl->Ierror_count; | |
8763 | PL_subline = proto_perl->Isubline; | |
8764 | PL_subname = sv_dup_inc(proto_perl->Isubname); | |
8765 | ||
8766 | PL_min_intro_pending = proto_perl->Imin_intro_pending; | |
8767 | PL_max_intro_pending = proto_perl->Imax_intro_pending; | |
8768 | PL_padix = proto_perl->Ipadix; | |
8769 | PL_padix_floor = proto_perl->Ipadix_floor; | |
8770 | PL_pad_reset_pending = proto_perl->Ipad_reset_pending; | |
8771 | ||
8772 | i = proto_perl->Ilast_uni - SvPVX(proto_perl->Ilinestr); | |
8773 | PL_last_uni = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
8774 | i = proto_perl->Ilast_lop - SvPVX(proto_perl->Ilinestr); | |
8775 | PL_last_lop = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
8776 | PL_last_lop_op = proto_perl->Ilast_lop_op; | |
8777 | PL_in_my = proto_perl->Iin_my; | |
8778 | PL_in_my_stash = hv_dup(proto_perl->Iin_my_stash); | |
8779 | #ifdef FCRYPT | |
8780 | PL_cryptseen = proto_perl->Icryptseen; | |
8781 | #endif | |
8782 | ||
8783 | PL_hints = proto_perl->Ihints; | |
8784 | ||
8785 | PL_amagic_generation = proto_perl->Iamagic_generation; | |
8786 | ||
8787 | #ifdef USE_LOCALE_COLLATE | |
8788 | PL_collation_ix = proto_perl->Icollation_ix; | |
8789 | PL_collation_name = SAVEPV(proto_perl->Icollation_name); | |
8790 | PL_collation_standard = proto_perl->Icollation_standard; | |
8791 | PL_collxfrm_base = proto_perl->Icollxfrm_base; | |
8792 | PL_collxfrm_mult = proto_perl->Icollxfrm_mult; | |
8793 | #endif /* USE_LOCALE_COLLATE */ | |
8794 | ||
8795 | #ifdef USE_LOCALE_NUMERIC | |
8796 | PL_numeric_name = SAVEPV(proto_perl->Inumeric_name); | |
8797 | PL_numeric_standard = proto_perl->Inumeric_standard; | |
8798 | PL_numeric_local = proto_perl->Inumeric_local; | |
8799 | PL_numeric_radix = proto_perl->Inumeric_radix; | |
8800 | #endif /* !USE_LOCALE_NUMERIC */ | |
8801 | ||
8802 | /* utf8 character classes */ | |
8803 | PL_utf8_alnum = sv_dup_inc(proto_perl->Iutf8_alnum); | |
8804 | PL_utf8_alnumc = sv_dup_inc(proto_perl->Iutf8_alnumc); | |
8805 | PL_utf8_ascii = sv_dup_inc(proto_perl->Iutf8_ascii); | |
8806 | PL_utf8_alpha = sv_dup_inc(proto_perl->Iutf8_alpha); | |
8807 | PL_utf8_space = sv_dup_inc(proto_perl->Iutf8_space); | |
8808 | PL_utf8_cntrl = sv_dup_inc(proto_perl->Iutf8_cntrl); | |
8809 | PL_utf8_graph = sv_dup_inc(proto_perl->Iutf8_graph); | |
8810 | PL_utf8_digit = sv_dup_inc(proto_perl->Iutf8_digit); | |
8811 | PL_utf8_upper = sv_dup_inc(proto_perl->Iutf8_upper); | |
8812 | PL_utf8_lower = sv_dup_inc(proto_perl->Iutf8_lower); | |
8813 | PL_utf8_print = sv_dup_inc(proto_perl->Iutf8_print); | |
8814 | PL_utf8_punct = sv_dup_inc(proto_perl->Iutf8_punct); | |
8815 | PL_utf8_xdigit = sv_dup_inc(proto_perl->Iutf8_xdigit); | |
8816 | PL_utf8_mark = sv_dup_inc(proto_perl->Iutf8_mark); | |
8817 | PL_utf8_toupper = sv_dup_inc(proto_perl->Iutf8_toupper); | |
8818 | PL_utf8_totitle = sv_dup_inc(proto_perl->Iutf8_totitle); | |
8819 | PL_utf8_tolower = sv_dup_inc(proto_perl->Iutf8_tolower); | |
8820 | ||
8821 | /* swatch cache */ | |
8822 | PL_last_swash_hv = Nullhv; /* reinits on demand */ | |
8823 | PL_last_swash_klen = 0; | |
8824 | PL_last_swash_key[0]= '\0'; | |
8825 | PL_last_swash_tmps = (U8*)NULL; | |
8826 | PL_last_swash_slen = 0; | |
8827 | ||
8828 | /* perly.c globals */ | |
8829 | PL_yydebug = proto_perl->Iyydebug; | |
8830 | PL_yynerrs = proto_perl->Iyynerrs; | |
8831 | PL_yyerrflag = proto_perl->Iyyerrflag; | |
8832 | PL_yychar = proto_perl->Iyychar; | |
8833 | PL_yyval = proto_perl->Iyyval; | |
8834 | PL_yylval = proto_perl->Iyylval; | |
8835 | ||
8836 | PL_glob_index = proto_perl->Iglob_index; | |
8837 | PL_srand_called = proto_perl->Isrand_called; | |
8838 | PL_uudmap['M'] = 0; /* reinits on demand */ | |
8839 | PL_bitcount = Nullch; /* reinits on demand */ | |
8840 | ||
8841 | if (proto_perl->Ipsig_ptr) { | |
8842 | int sig_num[] = { SIG_NUM }; | |
8843 | Newz(0, PL_psig_ptr, sizeof(sig_num)/sizeof(*sig_num), SV*); | |
8844 | Newz(0, PL_psig_name, sizeof(sig_num)/sizeof(*sig_num), SV*); | |
8845 | for (i = 1; PL_sig_name[i]; i++) { | |
8846 | PL_psig_ptr[i] = sv_dup_inc(proto_perl->Ipsig_ptr[i]); | |
8847 | PL_psig_name[i] = sv_dup_inc(proto_perl->Ipsig_name[i]); | |
8848 | } | |
8849 | } | |
8850 | else { | |
8851 | PL_psig_ptr = (SV**)NULL; | |
8852 | PL_psig_name = (SV**)NULL; | |
8853 | } | |
8854 | ||
8855 | /* thrdvar.h stuff */ | |
8856 | ||
8857 | if (flags & 1) { | |
8858 | /* next allocation will be PL_tmps_stack[PL_tmps_ix+1] */ | |
8859 | PL_tmps_ix = proto_perl->Ttmps_ix; | |
8860 | PL_tmps_max = proto_perl->Ttmps_max; | |
8861 | PL_tmps_floor = proto_perl->Ttmps_floor; | |
8862 | Newz(50, PL_tmps_stack, PL_tmps_max, SV*); | |
8863 | i = 0; | |
8864 | while (i <= PL_tmps_ix) { | |
8865 | PL_tmps_stack[i] = sv_dup_inc(proto_perl->Ttmps_stack[i]); | |
8866 | ++i; | |
8867 | } | |
8868 | ||
8869 | /* next PUSHMARK() sets *(PL_markstack_ptr+1) */ | |
8870 | i = proto_perl->Tmarkstack_max - proto_perl->Tmarkstack; | |
8871 | Newz(54, PL_markstack, i, I32); | |
8872 | PL_markstack_max = PL_markstack + (proto_perl->Tmarkstack_max | |
8873 | - proto_perl->Tmarkstack); | |
8874 | PL_markstack_ptr = PL_markstack + (proto_perl->Tmarkstack_ptr | |
8875 | - proto_perl->Tmarkstack); | |
8876 | Copy(proto_perl->Tmarkstack, PL_markstack, | |
8877 | PL_markstack_ptr - PL_markstack + 1, I32); | |
8878 | ||
8879 | /* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix] | |
8880 | * NOTE: unlike the others! */ | |
8881 | PL_scopestack_ix = proto_perl->Tscopestack_ix; | |
8882 | PL_scopestack_max = proto_perl->Tscopestack_max; | |
8883 | Newz(54, PL_scopestack, PL_scopestack_max, I32); | |
8884 | Copy(proto_perl->Tscopestack, PL_scopestack, PL_scopestack_ix, I32); | |
8885 | ||
8886 | /* next push_return() sets PL_retstack[PL_retstack_ix] | |
8887 | * NOTE: unlike the others! */ | |
8888 | PL_retstack_ix = proto_perl->Tretstack_ix; | |
8889 | PL_retstack_max = proto_perl->Tretstack_max; | |
8890 | Newz(54, PL_retstack, PL_retstack_max, OP*); | |
8891 | Copy(proto_perl->Tretstack, PL_retstack, PL_retstack_ix, I32); | |
8892 | ||
8893 | /* NOTE: si_dup() looks at PL_markstack */ | |
8894 | PL_curstackinfo = si_dup(proto_perl->Tcurstackinfo); | |
8895 | ||
8896 | /* PL_curstack = PL_curstackinfo->si_stack; */ | |
8897 | PL_curstack = av_dup(proto_perl->Tcurstack); | |
8898 | PL_mainstack = av_dup(proto_perl->Tmainstack); | |
8899 | ||
8900 | /* next PUSHs() etc. set *(PL_stack_sp+1) */ | |
8901 | PL_stack_base = AvARRAY(PL_curstack); | |
8902 | PL_stack_sp = PL_stack_base + (proto_perl->Tstack_sp | |
8903 | - proto_perl->Tstack_base); | |
8904 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); | |
8905 | ||
8906 | /* next SSPUSHFOO() sets PL_savestack[PL_savestack_ix] | |
8907 | * NOTE: unlike the others! */ | |
8908 | PL_savestack_ix = proto_perl->Tsavestack_ix; | |
8909 | PL_savestack_max = proto_perl->Tsavestack_max; | |
8910 | /*Newz(54, PL_savestack, PL_savestack_max, ANY);*/ | |
8911 | PL_savestack = ss_dup(proto_perl); | |
8912 | } | |
8913 | else { | |
8914 | init_stacks(); | |
985e7056 | 8915 | ENTER; /* perl_destruct() wants to LEAVE; */ |
1d7c1841 GS |
8916 | } |
8917 | ||
8918 | PL_start_env = proto_perl->Tstart_env; /* XXXXXX */ | |
8919 | PL_top_env = &PL_start_env; | |
8920 | ||
8921 | PL_op = proto_perl->Top; | |
8922 | ||
8923 | PL_Sv = Nullsv; | |
8924 | PL_Xpv = (XPV*)NULL; | |
8925 | PL_na = proto_perl->Tna; | |
8926 | ||
8927 | PL_statbuf = proto_perl->Tstatbuf; | |
8928 | PL_statcache = proto_perl->Tstatcache; | |
8929 | PL_statgv = gv_dup(proto_perl->Tstatgv); | |
8930 | PL_statname = sv_dup_inc(proto_perl->Tstatname); | |
8931 | #ifdef HAS_TIMES | |
8932 | PL_timesbuf = proto_perl->Ttimesbuf; | |
8933 | #endif | |
8934 | ||
8935 | PL_tainted = proto_perl->Ttainted; | |
8936 | PL_curpm = proto_perl->Tcurpm; /* XXX No PMOP ref count */ | |
8937 | PL_nrs = sv_dup_inc(proto_perl->Tnrs); | |
8938 | PL_rs = sv_dup_inc(proto_perl->Trs); | |
8939 | PL_last_in_gv = gv_dup(proto_perl->Tlast_in_gv); | |
7889fe52 | 8940 | PL_ofs_sv = sv_dup_inc(proto_perl->Tofs_sv); |
1d7c1841 GS |
8941 | PL_defoutgv = gv_dup_inc(proto_perl->Tdefoutgv); |
8942 | PL_chopset = proto_perl->Tchopset; /* XXX never deallocated */ | |
8943 | PL_toptarget = sv_dup_inc(proto_perl->Ttoptarget); | |
8944 | PL_bodytarget = sv_dup_inc(proto_perl->Tbodytarget); | |
8945 | PL_formtarget = sv_dup(proto_perl->Tformtarget); | |
8946 | ||
8947 | PL_restartop = proto_perl->Trestartop; | |
8948 | PL_in_eval = proto_perl->Tin_eval; | |
8949 | PL_delaymagic = proto_perl->Tdelaymagic; | |
8950 | PL_dirty = proto_perl->Tdirty; | |
8951 | PL_localizing = proto_perl->Tlocalizing; | |
8952 | ||
14dd3ad8 | 8953 | #ifdef PERL_FLEXIBLE_EXCEPTIONS |
1d7c1841 | 8954 | PL_protect = proto_perl->Tprotect; |
14dd3ad8 | 8955 | #endif |
1d7c1841 GS |
8956 | PL_errors = sv_dup_inc(proto_perl->Terrors); |
8957 | PL_av_fetch_sv = Nullsv; | |
8958 | PL_hv_fetch_sv = Nullsv; | |
8959 | Zero(&PL_hv_fetch_ent_mh, 1, HE); /* XXX */ | |
8960 | PL_modcount = proto_perl->Tmodcount; | |
8961 | PL_lastgotoprobe = Nullop; | |
8962 | PL_dumpindent = proto_perl->Tdumpindent; | |
8963 | ||
8964 | PL_sortcop = (OP*)any_dup(proto_perl->Tsortcop, proto_perl); | |
8965 | PL_sortstash = hv_dup(proto_perl->Tsortstash); | |
8966 | PL_firstgv = gv_dup(proto_perl->Tfirstgv); | |
8967 | PL_secondgv = gv_dup(proto_perl->Tsecondgv); | |
8968 | PL_sortcxix = proto_perl->Tsortcxix; | |
8969 | PL_efloatbuf = Nullch; /* reinits on demand */ | |
8970 | PL_efloatsize = 0; /* reinits on demand */ | |
8971 | ||
8972 | /* regex stuff */ | |
8973 | ||
8974 | PL_screamfirst = NULL; | |
8975 | PL_screamnext = NULL; | |
8976 | PL_maxscream = -1; /* reinits on demand */ | |
8977 | PL_lastscream = Nullsv; | |
8978 | ||
8979 | PL_watchaddr = NULL; | |
8980 | PL_watchok = Nullch; | |
8981 | ||
8982 | PL_regdummy = proto_perl->Tregdummy; | |
8983 | PL_regcomp_parse = Nullch; | |
8984 | PL_regxend = Nullch; | |
8985 | PL_regcode = (regnode*)NULL; | |
8986 | PL_regnaughty = 0; | |
8987 | PL_regsawback = 0; | |
8988 | PL_regprecomp = Nullch; | |
8989 | PL_regnpar = 0; | |
8990 | PL_regsize = 0; | |
8991 | PL_regflags = 0; | |
8992 | PL_regseen = 0; | |
8993 | PL_seen_zerolen = 0; | |
8994 | PL_seen_evals = 0; | |
8995 | PL_regcomp_rx = (regexp*)NULL; | |
8996 | PL_extralen = 0; | |
8997 | PL_colorset = 0; /* reinits PL_colors[] */ | |
8998 | /*PL_colors[6] = {0,0,0,0,0,0};*/ | |
8999 | PL_reg_whilem_seen = 0; | |
9000 | PL_reginput = Nullch; | |
9001 | PL_regbol = Nullch; | |
9002 | PL_regeol = Nullch; | |
9003 | PL_regstartp = (I32*)NULL; | |
9004 | PL_regendp = (I32*)NULL; | |
9005 | PL_reglastparen = (U32*)NULL; | |
9006 | PL_regtill = Nullch; | |
9007 | PL_regprev = '\n'; | |
9008 | PL_reg_start_tmp = (char**)NULL; | |
9009 | PL_reg_start_tmpl = 0; | |
9010 | PL_regdata = (struct reg_data*)NULL; | |
9011 | PL_bostr = Nullch; | |
9012 | PL_reg_flags = 0; | |
9013 | PL_reg_eval_set = 0; | |
9014 | PL_regnarrate = 0; | |
9015 | PL_regprogram = (regnode*)NULL; | |
9016 | PL_regindent = 0; | |
9017 | PL_regcc = (CURCUR*)NULL; | |
9018 | PL_reg_call_cc = (struct re_cc_state*)NULL; | |
9019 | PL_reg_re = (regexp*)NULL; | |
9020 | PL_reg_ganch = Nullch; | |
9021 | PL_reg_sv = Nullsv; | |
9022 | PL_reg_magic = (MAGIC*)NULL; | |
9023 | PL_reg_oldpos = 0; | |
9024 | PL_reg_oldcurpm = (PMOP*)NULL; | |
9025 | PL_reg_curpm = (PMOP*)NULL; | |
9026 | PL_reg_oldsaved = Nullch; | |
9027 | PL_reg_oldsavedlen = 0; | |
9028 | PL_reg_maxiter = 0; | |
9029 | PL_reg_leftiter = 0; | |
9030 | PL_reg_poscache = Nullch; | |
9031 | PL_reg_poscache_size= 0; | |
9032 | ||
9033 | /* RE engine - function pointers */ | |
9034 | PL_regcompp = proto_perl->Tregcompp; | |
9035 | PL_regexecp = proto_perl->Tregexecp; | |
9036 | PL_regint_start = proto_perl->Tregint_start; | |
9037 | PL_regint_string = proto_perl->Tregint_string; | |
9038 | PL_regfree = proto_perl->Tregfree; | |
9039 | ||
9040 | PL_reginterp_cnt = 0; | |
9041 | PL_reg_starttry = 0; | |
9042 | ||
9043 | #ifdef PERL_OBJECT | |
9044 | return (PerlInterpreter*)pPerl; | |
9045 | #else | |
9046 | return my_perl; | |
9047 | #endif | |
9048 | } | |
9049 | ||
9050 | #else /* !USE_ITHREADS */ | |
51371543 GS |
9051 | |
9052 | #ifdef PERL_OBJECT | |
51371543 GS |
9053 | #include "XSUB.h" |
9054 | #endif | |
9055 | ||
1d7c1841 GS |
9056 | #endif /* USE_ITHREADS */ |
9057 | ||
51371543 GS |
9058 | static void |
9059 | do_report_used(pTHXo_ SV *sv) | |
9060 | { | |
9061 | if (SvTYPE(sv) != SVTYPEMASK) { | |
bf49b057 | 9062 | PerlIO_printf(Perl_debug_log, "****\n"); |
51371543 GS |
9063 | sv_dump(sv); |
9064 | } | |
9065 | } | |
9066 | ||
9067 | static void | |
9068 | do_clean_objs(pTHXo_ SV *sv) | |
9069 | { | |
9070 | SV* rv; | |
9071 | ||
9072 | if (SvROK(sv) && SvOBJECT(rv = SvRV(sv))) { | |
9073 | DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(sv));) | |
8b6e653b HS |
9074 | if (SvWEAKREF(sv)) { |
9075 | sv_del_backref(sv); | |
9076 | SvWEAKREF_off(sv); | |
9077 | SvRV(sv) = 0; | |
9078 | } else { | |
9079 | SvROK_off(sv); | |
9080 | SvRV(sv) = 0; | |
9081 | SvREFCNT_dec(rv); | |
9082 | } | |
51371543 GS |
9083 | } |
9084 | ||
9085 | /* XXX Might want to check arrays, etc. */ | |
9086 | } | |
9087 | ||
9088 | #ifndef DISABLE_DESTRUCTOR_KLUDGE | |
9089 | static void | |
9090 | do_clean_named_objs(pTHXo_ SV *sv) | |
9091 | { | |
f472eb5c | 9092 | if (SvTYPE(sv) == SVt_PVGV && GvGP(sv)) { |
51371543 | 9093 | if ( SvOBJECT(GvSV(sv)) || |
155aba94 GS |
9094 | (GvAV(sv) && SvOBJECT(GvAV(sv))) || |
9095 | (GvHV(sv) && SvOBJECT(GvHV(sv))) || | |
9096 | (GvIO(sv) && SvOBJECT(GvIO(sv))) || | |
9097 | (GvCV(sv) && SvOBJECT(GvCV(sv))) ) | |
51371543 GS |
9098 | { |
9099 | DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning named glob object:\n "), sv_dump(sv));) | |
9100 | SvREFCNT_dec(sv); | |
9101 | } | |
9102 | } | |
9103 | } | |
9104 | #endif | |
9105 | ||
9106 | static void | |
9107 | do_clean_all(pTHXo_ SV *sv) | |
9108 | { | |
1d7c1841 | 9109 | DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) );) |
51371543 GS |
9110 | SvFLAGS(sv) |= SVf_BREAK; |
9111 | SvREFCNT_dec(sv); | |
9112 | } | |
8af02333 | 9113 |