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