Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | /* sv.c |
79072805 | 2 | * |
bc89e66f | 3 | * Copyright (c) 1991-2001, Larry Wall |
79072805 LW |
4 | * |
5 | * You may distribute under the terms of either the GNU General Public | |
6 | * License or the Artistic License, as specified in the README file. | |
7 | * | |
a0d0e21e LW |
8 | */ |
9 | ||
10 | /* | |
11 | * "I wonder what the Entish is for 'yes' and 'no'," he thought. | |
79072805 LW |
12 | */ |
13 | ||
14 | #include "EXTERN.h" | |
864dbfa3 | 15 | #define PERL_IN_SV_C |
79072805 | 16 | #include "perl.h" |
79072805 | 17 | |
51371543 | 18 | #define FCALL *f |
6fc92669 | 19 | #define SV_CHECK_THINKFIRST(sv) if (SvTHINKFIRST(sv)) sv_force_normal(sv) |
2c5424a7 | 20 | |
51371543 GS |
21 | static void do_report_used(pTHXo_ SV *sv); |
22 | static void do_clean_objs(pTHXo_ SV *sv); | |
23 | #ifndef DISABLE_DESTRUCTOR_KLUDGE | |
24 | static void do_clean_named_objs(pTHXo_ SV *sv); | |
25 | #endif | |
26 | static void do_clean_all(pTHXo_ SV *sv); | |
27 | ||
4561caa4 CS |
28 | /* |
29 | * "A time to plant, and a time to uproot what was planted..." | |
30 | */ | |
31 | ||
053fc874 GS |
32 | #define plant_SV(p) \ |
33 | STMT_START { \ | |
34 | SvANY(p) = (void *)PL_sv_root; \ | |
35 | SvFLAGS(p) = SVTYPEMASK; \ | |
36 | PL_sv_root = (p); \ | |
37 | --PL_sv_count; \ | |
38 | } STMT_END | |
a0d0e21e | 39 | |
fba3b22e | 40 | /* sv_mutex must be held while calling uproot_SV() */ |
053fc874 GS |
41 | #define uproot_SV(p) \ |
42 | STMT_START { \ | |
43 | (p) = PL_sv_root; \ | |
44 | PL_sv_root = (SV*)SvANY(p); \ | |
45 | ++PL_sv_count; \ | |
46 | } STMT_END | |
47 | ||
48 | #define new_SV(p) \ | |
49 | STMT_START { \ | |
50 | LOCK_SV_MUTEX; \ | |
51 | if (PL_sv_root) \ | |
52 | uproot_SV(p); \ | |
53 | else \ | |
54 | (p) = more_sv(); \ | |
55 | UNLOCK_SV_MUTEX; \ | |
56 | SvANY(p) = 0; \ | |
57 | SvREFCNT(p) = 1; \ | |
58 | SvFLAGS(p) = 0; \ | |
59 | } STMT_END | |
463ee0b2 | 60 | |
a0d0e21e | 61 | #ifdef DEBUGGING |
4561caa4 | 62 | |
053fc874 GS |
63 | #define del_SV(p) \ |
64 | STMT_START { \ | |
65 | LOCK_SV_MUTEX; \ | |
aea4f609 | 66 | if (DEBUG_D_TEST) \ |
053fc874 GS |
67 | del_sv(p); \ |
68 | else \ | |
69 | plant_SV(p); \ | |
70 | UNLOCK_SV_MUTEX; \ | |
71 | } STMT_END | |
a0d0e21e | 72 | |
76e3520e | 73 | STATIC void |
cea2e8a9 | 74 | S_del_sv(pTHX_ SV *p) |
463ee0b2 | 75 | { |
aea4f609 | 76 | if (DEBUG_D_TEST) { |
4633a7c4 | 77 | SV* sva; |
a0d0e21e LW |
78 | SV* sv; |
79 | SV* svend; | |
80 | int ok = 0; | |
3280af22 | 81 | for (sva = PL_sv_arenaroot; sva; sva = (SV *) SvANY(sva)) { |
4633a7c4 LW |
82 | sv = sva + 1; |
83 | svend = &sva[SvREFCNT(sva)]; | |
a0d0e21e LW |
84 | if (p >= sv && p < svend) |
85 | ok = 1; | |
86 | } | |
87 | if (!ok) { | |
0453d815 PM |
88 | if (ckWARN_d(WARN_INTERNAL)) |
89 | Perl_warner(aTHX_ WARN_INTERNAL, | |
1d7c1841 GS |
90 | "Attempt to free non-arena SV: 0x%"UVxf, |
91 | PTR2UV(p)); | |
a0d0e21e LW |
92 | return; |
93 | } | |
94 | } | |
4561caa4 | 95 | plant_SV(p); |
463ee0b2 | 96 | } |
a0d0e21e | 97 | |
4561caa4 CS |
98 | #else /* ! DEBUGGING */ |
99 | ||
100 | #define del_SV(p) plant_SV(p) | |
101 | ||
102 | #endif /* DEBUGGING */ | |
463ee0b2 | 103 | |
4633a7c4 | 104 | void |
864dbfa3 | 105 | Perl_sv_add_arena(pTHX_ char *ptr, U32 size, U32 flags) |
463ee0b2 | 106 | { |
4633a7c4 | 107 | SV* sva = (SV*)ptr; |
463ee0b2 LW |
108 | register SV* sv; |
109 | register SV* svend; | |
14dd3ad8 | 110 | Zero(ptr, size, char); |
4633a7c4 LW |
111 | |
112 | /* The first SV in an arena isn't an SV. */ | |
3280af22 | 113 | SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */ |
4633a7c4 LW |
114 | SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */ |
115 | SvFLAGS(sva) = flags; /* FAKE if not to be freed */ | |
116 | ||
3280af22 NIS |
117 | PL_sv_arenaroot = sva; |
118 | PL_sv_root = sva + 1; | |
4633a7c4 LW |
119 | |
120 | svend = &sva[SvREFCNT(sva) - 1]; | |
121 | sv = sva + 1; | |
463ee0b2 | 122 | while (sv < svend) { |
a0d0e21e | 123 | SvANY(sv) = (void *)(SV*)(sv + 1); |
8990e307 | 124 | SvFLAGS(sv) = SVTYPEMASK; |
463ee0b2 LW |
125 | sv++; |
126 | } | |
127 | SvANY(sv) = 0; | |
4633a7c4 LW |
128 | SvFLAGS(sv) = SVTYPEMASK; |
129 | } | |
130 | ||
fba3b22e | 131 | /* sv_mutex must be held while calling more_sv() */ |
76e3520e | 132 | STATIC SV* |
cea2e8a9 | 133 | S_more_sv(pTHX) |
4633a7c4 | 134 | { |
4561caa4 CS |
135 | register SV* sv; |
136 | ||
3280af22 NIS |
137 | if (PL_nice_chunk) { |
138 | sv_add_arena(PL_nice_chunk, PL_nice_chunk_size, 0); | |
139 | PL_nice_chunk = Nullch; | |
c07a80fd | 140 | } |
1edc1566 | 141 | else { |
142 | char *chunk; /* must use New here to match call to */ | |
143 | New(704,chunk,1008,char); /* Safefree() in sv_free_arenas() */ | |
144 | sv_add_arena(chunk, 1008, 0); | |
145 | } | |
4561caa4 CS |
146 | uproot_SV(sv); |
147 | return sv; | |
463ee0b2 LW |
148 | } |
149 | ||
5226ed68 | 150 | STATIC I32 |
cea2e8a9 | 151 | S_visit(pTHX_ SVFUNC_t f) |
8990e307 | 152 | { |
4633a7c4 | 153 | SV* sva; |
8990e307 LW |
154 | SV* sv; |
155 | register SV* svend; | |
5226ed68 | 156 | I32 visited = 0; |
8990e307 | 157 | |
3280af22 | 158 | for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) { |
4633a7c4 | 159 | svend = &sva[SvREFCNT(sva)]; |
4561caa4 | 160 | for (sv = sva + 1; sv < svend; ++sv) { |
f25c30a3 | 161 | if (SvTYPE(sv) != SVTYPEMASK && SvREFCNT(sv)) { |
51371543 | 162 | (FCALL)(aTHXo_ sv); |
5226ed68 JH |
163 | ++visited; |
164 | } | |
8990e307 LW |
165 | } |
166 | } | |
5226ed68 | 167 | return visited; |
8990e307 LW |
168 | } |
169 | ||
170 | void | |
864dbfa3 | 171 | Perl_sv_report_used(pTHX) |
4561caa4 | 172 | { |
0b94c7bb | 173 | visit(do_report_used); |
4561caa4 CS |
174 | } |
175 | ||
4561caa4 | 176 | void |
864dbfa3 | 177 | Perl_sv_clean_objs(pTHX) |
4561caa4 | 178 | { |
3280af22 | 179 | PL_in_clean_objs = TRUE; |
0b94c7bb | 180 | visit(do_clean_objs); |
4561caa4 | 181 | #ifndef DISABLE_DESTRUCTOR_KLUDGE |
2d0f3c12 | 182 | /* some barnacles may yet remain, clinging to typeglobs */ |
0b94c7bb | 183 | visit(do_clean_named_objs); |
4561caa4 | 184 | #endif |
3280af22 | 185 | PL_in_clean_objs = FALSE; |
4561caa4 CS |
186 | } |
187 | ||
5226ed68 | 188 | I32 |
864dbfa3 | 189 | Perl_sv_clean_all(pTHX) |
8990e307 | 190 | { |
5226ed68 | 191 | I32 cleaned; |
3280af22 | 192 | PL_in_clean_all = TRUE; |
5226ed68 | 193 | cleaned = visit(do_clean_all); |
3280af22 | 194 | PL_in_clean_all = FALSE; |
5226ed68 | 195 | return cleaned; |
8990e307 | 196 | } |
463ee0b2 | 197 | |
4633a7c4 | 198 | void |
864dbfa3 | 199 | Perl_sv_free_arenas(pTHX) |
4633a7c4 LW |
200 | { |
201 | SV* sva; | |
202 | SV* svanext; | |
612f20c3 | 203 | XPV *arena, *arenanext; |
4633a7c4 LW |
204 | |
205 | /* Free arenas here, but be careful about fake ones. (We assume | |
206 | contiguity of the fake ones with the corresponding real ones.) */ | |
207 | ||
3280af22 | 208 | for (sva = PL_sv_arenaroot; sva; sva = svanext) { |
4633a7c4 LW |
209 | svanext = (SV*) SvANY(sva); |
210 | while (svanext && SvFAKE(svanext)) | |
211 | svanext = (SV*) SvANY(svanext); | |
212 | ||
213 | if (!SvFAKE(sva)) | |
1edc1566 | 214 | Safefree((void *)sva); |
4633a7c4 | 215 | } |
5f05dabc | 216 | |
612f20c3 GS |
217 | for (arena = PL_xiv_arenaroot; arena; arena = arenanext) { |
218 | arenanext = (XPV*)arena->xpv_pv; | |
219 | Safefree(arena); | |
220 | } | |
221 | PL_xiv_arenaroot = 0; | |
222 | ||
223 | for (arena = PL_xnv_arenaroot; arena; arena = arenanext) { | |
224 | arenanext = (XPV*)arena->xpv_pv; | |
225 | Safefree(arena); | |
226 | } | |
227 | PL_xnv_arenaroot = 0; | |
228 | ||
229 | for (arena = PL_xrv_arenaroot; arena; arena = arenanext) { | |
230 | arenanext = (XPV*)arena->xpv_pv; | |
231 | Safefree(arena); | |
232 | } | |
233 | PL_xrv_arenaroot = 0; | |
234 | ||
235 | for (arena = PL_xpv_arenaroot; arena; arena = arenanext) { | |
236 | arenanext = (XPV*)arena->xpv_pv; | |
237 | Safefree(arena); | |
238 | } | |
239 | PL_xpv_arenaroot = 0; | |
240 | ||
241 | for (arena = (XPV*)PL_xpviv_arenaroot; arena; arena = arenanext) { | |
242 | arenanext = (XPV*)arena->xpv_pv; | |
243 | Safefree(arena); | |
244 | } | |
245 | PL_xpviv_arenaroot = 0; | |
246 | ||
247 | for (arena = (XPV*)PL_xpvnv_arenaroot; arena; arena = arenanext) { | |
248 | arenanext = (XPV*)arena->xpv_pv; | |
249 | Safefree(arena); | |
250 | } | |
251 | PL_xpvnv_arenaroot = 0; | |
252 | ||
253 | for (arena = (XPV*)PL_xpvcv_arenaroot; arena; arena = arenanext) { | |
254 | arenanext = (XPV*)arena->xpv_pv; | |
255 | Safefree(arena); | |
256 | } | |
257 | PL_xpvcv_arenaroot = 0; | |
258 | ||
259 | for (arena = (XPV*)PL_xpvav_arenaroot; arena; arena = arenanext) { | |
260 | arenanext = (XPV*)arena->xpv_pv; | |
261 | Safefree(arena); | |
262 | } | |
263 | PL_xpvav_arenaroot = 0; | |
264 | ||
265 | for (arena = (XPV*)PL_xpvhv_arenaroot; arena; arena = arenanext) { | |
266 | arenanext = (XPV*)arena->xpv_pv; | |
267 | Safefree(arena); | |
268 | } | |
269 | PL_xpvhv_arenaroot = 0; | |
270 | ||
271 | for (arena = (XPV*)PL_xpvmg_arenaroot; arena; arena = arenanext) { | |
272 | arenanext = (XPV*)arena->xpv_pv; | |
273 | Safefree(arena); | |
274 | } | |
275 | PL_xpvmg_arenaroot = 0; | |
276 | ||
277 | for (arena = (XPV*)PL_xpvlv_arenaroot; arena; arena = arenanext) { | |
278 | arenanext = (XPV*)arena->xpv_pv; | |
279 | Safefree(arena); | |
280 | } | |
281 | PL_xpvlv_arenaroot = 0; | |
282 | ||
283 | for (arena = (XPV*)PL_xpvbm_arenaroot; arena; arena = arenanext) { | |
284 | arenanext = (XPV*)arena->xpv_pv; | |
285 | Safefree(arena); | |
286 | } | |
287 | PL_xpvbm_arenaroot = 0; | |
288 | ||
289 | for (arena = (XPV*)PL_he_arenaroot; arena; arena = arenanext) { | |
290 | arenanext = (XPV*)arena->xpv_pv; | |
291 | Safefree(arena); | |
292 | } | |
293 | PL_he_arenaroot = 0; | |
294 | ||
3280af22 NIS |
295 | if (PL_nice_chunk) |
296 | Safefree(PL_nice_chunk); | |
297 | PL_nice_chunk = Nullch; | |
298 | PL_nice_chunk_size = 0; | |
299 | PL_sv_arenaroot = 0; | |
300 | PL_sv_root = 0; | |
4633a7c4 LW |
301 | } |
302 | ||
1d7c1841 GS |
303 | void |
304 | Perl_report_uninit(pTHX) | |
305 | { | |
306 | if (PL_op) | |
307 | Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit, | |
308 | " in ", PL_op_desc[PL_op->op_type]); | |
309 | else | |
310 | Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit, "", ""); | |
311 | } | |
312 | ||
76e3520e | 313 | STATIC XPVIV* |
cea2e8a9 | 314 | S_new_xiv(pTHX) |
463ee0b2 | 315 | { |
ea7c11a3 | 316 | IV* xiv; |
cbe51380 GS |
317 | LOCK_SV_MUTEX; |
318 | if (!PL_xiv_root) | |
319 | more_xiv(); | |
320 | xiv = PL_xiv_root; | |
321 | /* | |
322 | * See comment in more_xiv() -- RAM. | |
323 | */ | |
324 | PL_xiv_root = *(IV**)xiv; | |
325 | UNLOCK_SV_MUTEX; | |
326 | return (XPVIV*)((char*)xiv - STRUCT_OFFSET(XPVIV, xiv_iv)); | |
463ee0b2 LW |
327 | } |
328 | ||
76e3520e | 329 | STATIC void |
cea2e8a9 | 330 | S_del_xiv(pTHX_ XPVIV *p) |
463ee0b2 | 331 | { |
23e6a22f | 332 | IV* xiv = (IV*)((char*)(p) + STRUCT_OFFSET(XPVIV, xiv_iv)); |
cbe51380 | 333 | LOCK_SV_MUTEX; |
3280af22 NIS |
334 | *(IV**)xiv = PL_xiv_root; |
335 | PL_xiv_root = xiv; | |
cbe51380 | 336 | UNLOCK_SV_MUTEX; |
463ee0b2 LW |
337 | } |
338 | ||
cbe51380 | 339 | STATIC void |
cea2e8a9 | 340 | S_more_xiv(pTHX) |
463ee0b2 | 341 | { |
ea7c11a3 SM |
342 | register IV* xiv; |
343 | register IV* xivend; | |
8c52afec IZ |
344 | XPV* ptr; |
345 | New(705, ptr, 1008/sizeof(XPV), XPV); | |
3280af22 NIS |
346 | ptr->xpv_pv = (char*)PL_xiv_arenaroot; /* linked list of xiv arenas */ |
347 | PL_xiv_arenaroot = ptr; /* to keep Purify happy */ | |
a0d0e21e | 348 | |
ea7c11a3 SM |
349 | xiv = (IV*) ptr; |
350 | xivend = &xiv[1008 / sizeof(IV) - 1]; | |
351 | xiv += (sizeof(XPV) - 1) / sizeof(IV) + 1; /* fudge by size of XPV */ | |
3280af22 | 352 | PL_xiv_root = xiv; |
463ee0b2 | 353 | while (xiv < xivend) { |
ea7c11a3 | 354 | *(IV**)xiv = (IV *)(xiv + 1); |
463ee0b2 LW |
355 | xiv++; |
356 | } | |
ea7c11a3 | 357 | *(IV**)xiv = 0; |
463ee0b2 LW |
358 | } |
359 | ||
76e3520e | 360 | STATIC XPVNV* |
cea2e8a9 | 361 | S_new_xnv(pTHX) |
463ee0b2 | 362 | { |
65202027 | 363 | NV* xnv; |
cbe51380 GS |
364 | LOCK_SV_MUTEX; |
365 | if (!PL_xnv_root) | |
366 | more_xnv(); | |
367 | xnv = PL_xnv_root; | |
65202027 | 368 | PL_xnv_root = *(NV**)xnv; |
cbe51380 GS |
369 | UNLOCK_SV_MUTEX; |
370 | return (XPVNV*)((char*)xnv - STRUCT_OFFSET(XPVNV, xnv_nv)); | |
463ee0b2 LW |
371 | } |
372 | ||
76e3520e | 373 | STATIC void |
cea2e8a9 | 374 | S_del_xnv(pTHX_ XPVNV *p) |
463ee0b2 | 375 | { |
65202027 | 376 | NV* xnv = (NV*)((char*)(p) + STRUCT_OFFSET(XPVNV, xnv_nv)); |
cbe51380 | 377 | LOCK_SV_MUTEX; |
65202027 | 378 | *(NV**)xnv = PL_xnv_root; |
3280af22 | 379 | PL_xnv_root = xnv; |
cbe51380 | 380 | UNLOCK_SV_MUTEX; |
463ee0b2 LW |
381 | } |
382 | ||
cbe51380 | 383 | STATIC void |
cea2e8a9 | 384 | S_more_xnv(pTHX) |
463ee0b2 | 385 | { |
65202027 DS |
386 | register NV* xnv; |
387 | register NV* xnvend; | |
612f20c3 GS |
388 | XPV *ptr; |
389 | New(711, ptr, 1008/sizeof(XPV), XPV); | |
390 | ptr->xpv_pv = (char*)PL_xnv_arenaroot; | |
391 | PL_xnv_arenaroot = ptr; | |
392 | ||
393 | xnv = (NV*) ptr; | |
65202027 DS |
394 | xnvend = &xnv[1008 / sizeof(NV) - 1]; |
395 | xnv += (sizeof(XPVIV) - 1) / sizeof(NV) + 1; /* fudge by sizeof XPVIV */ | |
3280af22 | 396 | PL_xnv_root = xnv; |
463ee0b2 | 397 | while (xnv < xnvend) { |
65202027 | 398 | *(NV**)xnv = (NV*)(xnv + 1); |
463ee0b2 LW |
399 | xnv++; |
400 | } | |
65202027 | 401 | *(NV**)xnv = 0; |
463ee0b2 LW |
402 | } |
403 | ||
76e3520e | 404 | STATIC XRV* |
cea2e8a9 | 405 | S_new_xrv(pTHX) |
ed6116ce LW |
406 | { |
407 | XRV* xrv; | |
cbe51380 GS |
408 | LOCK_SV_MUTEX; |
409 | if (!PL_xrv_root) | |
410 | more_xrv(); | |
411 | xrv = PL_xrv_root; | |
412 | PL_xrv_root = (XRV*)xrv->xrv_rv; | |
413 | UNLOCK_SV_MUTEX; | |
414 | return xrv; | |
ed6116ce LW |
415 | } |
416 | ||
76e3520e | 417 | STATIC void |
cea2e8a9 | 418 | S_del_xrv(pTHX_ XRV *p) |
ed6116ce | 419 | { |
cbe51380 | 420 | LOCK_SV_MUTEX; |
3280af22 NIS |
421 | p->xrv_rv = (SV*)PL_xrv_root; |
422 | PL_xrv_root = p; | |
cbe51380 | 423 | UNLOCK_SV_MUTEX; |
ed6116ce LW |
424 | } |
425 | ||
cbe51380 | 426 | STATIC void |
cea2e8a9 | 427 | S_more_xrv(pTHX) |
ed6116ce | 428 | { |
ed6116ce LW |
429 | register XRV* xrv; |
430 | register XRV* xrvend; | |
612f20c3 GS |
431 | XPV *ptr; |
432 | New(712, ptr, 1008/sizeof(XPV), XPV); | |
433 | ptr->xpv_pv = (char*)PL_xrv_arenaroot; | |
434 | PL_xrv_arenaroot = ptr; | |
435 | ||
436 | xrv = (XRV*) ptr; | |
ed6116ce | 437 | xrvend = &xrv[1008 / sizeof(XRV) - 1]; |
612f20c3 GS |
438 | xrv += (sizeof(XPV) - 1) / sizeof(XRV) + 1; |
439 | PL_xrv_root = xrv; | |
ed6116ce LW |
440 | while (xrv < xrvend) { |
441 | xrv->xrv_rv = (SV*)(xrv + 1); | |
442 | xrv++; | |
443 | } | |
444 | xrv->xrv_rv = 0; | |
ed6116ce LW |
445 | } |
446 | ||
76e3520e | 447 | STATIC XPV* |
cea2e8a9 | 448 | S_new_xpv(pTHX) |
463ee0b2 LW |
449 | { |
450 | XPV* xpv; | |
cbe51380 GS |
451 | LOCK_SV_MUTEX; |
452 | if (!PL_xpv_root) | |
453 | more_xpv(); | |
454 | xpv = PL_xpv_root; | |
455 | PL_xpv_root = (XPV*)xpv->xpv_pv; | |
456 | UNLOCK_SV_MUTEX; | |
457 | return xpv; | |
463ee0b2 LW |
458 | } |
459 | ||
76e3520e | 460 | STATIC void |
cea2e8a9 | 461 | S_del_xpv(pTHX_ XPV *p) |
463ee0b2 | 462 | { |
cbe51380 | 463 | LOCK_SV_MUTEX; |
3280af22 NIS |
464 | p->xpv_pv = (char*)PL_xpv_root; |
465 | PL_xpv_root = p; | |
cbe51380 | 466 | UNLOCK_SV_MUTEX; |
463ee0b2 LW |
467 | } |
468 | ||
cbe51380 | 469 | STATIC void |
cea2e8a9 | 470 | S_more_xpv(pTHX) |
463ee0b2 | 471 | { |
463ee0b2 LW |
472 | register XPV* xpv; |
473 | register XPV* xpvend; | |
612f20c3 GS |
474 | New(713, xpv, 1008/sizeof(XPV), XPV); |
475 | xpv->xpv_pv = (char*)PL_xpv_arenaroot; | |
476 | PL_xpv_arenaroot = xpv; | |
477 | ||
463ee0b2 | 478 | xpvend = &xpv[1008 / sizeof(XPV) - 1]; |
612f20c3 | 479 | PL_xpv_root = ++xpv; |
463ee0b2 LW |
480 | while (xpv < xpvend) { |
481 | xpv->xpv_pv = (char*)(xpv + 1); | |
482 | xpv++; | |
483 | } | |
484 | xpv->xpv_pv = 0; | |
463ee0b2 LW |
485 | } |
486 | ||
932e9ff9 VB |
487 | STATIC XPVIV* |
488 | S_new_xpviv(pTHX) | |
489 | { | |
490 | XPVIV* xpviv; | |
491 | LOCK_SV_MUTEX; | |
492 | if (!PL_xpviv_root) | |
493 | more_xpviv(); | |
494 | xpviv = PL_xpviv_root; | |
495 | PL_xpviv_root = (XPVIV*)xpviv->xpv_pv; | |
496 | UNLOCK_SV_MUTEX; | |
497 | return xpviv; | |
498 | } | |
499 | ||
500 | STATIC void | |
501 | S_del_xpviv(pTHX_ XPVIV *p) | |
502 | { | |
503 | LOCK_SV_MUTEX; | |
504 | p->xpv_pv = (char*)PL_xpviv_root; | |
505 | PL_xpviv_root = p; | |
506 | UNLOCK_SV_MUTEX; | |
507 | } | |
508 | ||
932e9ff9 VB |
509 | STATIC void |
510 | S_more_xpviv(pTHX) | |
511 | { | |
512 | register XPVIV* xpviv; | |
513 | register XPVIV* xpvivend; | |
612f20c3 GS |
514 | New(714, xpviv, 1008/sizeof(XPVIV), XPVIV); |
515 | xpviv->xpv_pv = (char*)PL_xpviv_arenaroot; | |
516 | PL_xpviv_arenaroot = xpviv; | |
517 | ||
932e9ff9 | 518 | xpvivend = &xpviv[1008 / sizeof(XPVIV) - 1]; |
612f20c3 | 519 | PL_xpviv_root = ++xpviv; |
932e9ff9 VB |
520 | while (xpviv < xpvivend) { |
521 | xpviv->xpv_pv = (char*)(xpviv + 1); | |
522 | xpviv++; | |
523 | } | |
524 | xpviv->xpv_pv = 0; | |
525 | } | |
526 | ||
932e9ff9 VB |
527 | STATIC XPVNV* |
528 | S_new_xpvnv(pTHX) | |
529 | { | |
530 | XPVNV* xpvnv; | |
531 | LOCK_SV_MUTEX; | |
532 | if (!PL_xpvnv_root) | |
533 | more_xpvnv(); | |
534 | xpvnv = PL_xpvnv_root; | |
535 | PL_xpvnv_root = (XPVNV*)xpvnv->xpv_pv; | |
536 | UNLOCK_SV_MUTEX; | |
537 | return xpvnv; | |
538 | } | |
539 | ||
540 | STATIC void | |
541 | S_del_xpvnv(pTHX_ XPVNV *p) | |
542 | { | |
543 | LOCK_SV_MUTEX; | |
544 | p->xpv_pv = (char*)PL_xpvnv_root; | |
545 | PL_xpvnv_root = p; | |
546 | UNLOCK_SV_MUTEX; | |
547 | } | |
548 | ||
932e9ff9 VB |
549 | STATIC void |
550 | S_more_xpvnv(pTHX) | |
551 | { | |
552 | register XPVNV* xpvnv; | |
553 | register XPVNV* xpvnvend; | |
612f20c3 GS |
554 | New(715, xpvnv, 1008/sizeof(XPVNV), XPVNV); |
555 | xpvnv->xpv_pv = (char*)PL_xpvnv_arenaroot; | |
556 | PL_xpvnv_arenaroot = xpvnv; | |
557 | ||
932e9ff9 | 558 | xpvnvend = &xpvnv[1008 / sizeof(XPVNV) - 1]; |
612f20c3 | 559 | PL_xpvnv_root = ++xpvnv; |
932e9ff9 VB |
560 | while (xpvnv < xpvnvend) { |
561 | xpvnv->xpv_pv = (char*)(xpvnv + 1); | |
562 | xpvnv++; | |
563 | } | |
564 | xpvnv->xpv_pv = 0; | |
565 | } | |
566 | ||
932e9ff9 VB |
567 | STATIC XPVCV* |
568 | S_new_xpvcv(pTHX) | |
569 | { | |
570 | XPVCV* xpvcv; | |
571 | LOCK_SV_MUTEX; | |
572 | if (!PL_xpvcv_root) | |
573 | more_xpvcv(); | |
574 | xpvcv = PL_xpvcv_root; | |
575 | PL_xpvcv_root = (XPVCV*)xpvcv->xpv_pv; | |
576 | UNLOCK_SV_MUTEX; | |
577 | return xpvcv; | |
578 | } | |
579 | ||
580 | STATIC void | |
581 | S_del_xpvcv(pTHX_ XPVCV *p) | |
582 | { | |
583 | LOCK_SV_MUTEX; | |
584 | p->xpv_pv = (char*)PL_xpvcv_root; | |
585 | PL_xpvcv_root = p; | |
586 | UNLOCK_SV_MUTEX; | |
587 | } | |
588 | ||
932e9ff9 VB |
589 | STATIC void |
590 | S_more_xpvcv(pTHX) | |
591 | { | |
592 | register XPVCV* xpvcv; | |
593 | register XPVCV* xpvcvend; | |
612f20c3 GS |
594 | New(716, xpvcv, 1008/sizeof(XPVCV), XPVCV); |
595 | xpvcv->xpv_pv = (char*)PL_xpvcv_arenaroot; | |
596 | PL_xpvcv_arenaroot = xpvcv; | |
597 | ||
932e9ff9 | 598 | xpvcvend = &xpvcv[1008 / sizeof(XPVCV) - 1]; |
612f20c3 | 599 | PL_xpvcv_root = ++xpvcv; |
932e9ff9 VB |
600 | while (xpvcv < xpvcvend) { |
601 | xpvcv->xpv_pv = (char*)(xpvcv + 1); | |
602 | xpvcv++; | |
603 | } | |
604 | xpvcv->xpv_pv = 0; | |
605 | } | |
606 | ||
932e9ff9 VB |
607 | STATIC XPVAV* |
608 | S_new_xpvav(pTHX) | |
609 | { | |
610 | XPVAV* xpvav; | |
611 | LOCK_SV_MUTEX; | |
612 | if (!PL_xpvav_root) | |
613 | more_xpvav(); | |
614 | xpvav = PL_xpvav_root; | |
615 | PL_xpvav_root = (XPVAV*)xpvav->xav_array; | |
616 | UNLOCK_SV_MUTEX; | |
617 | return xpvav; | |
618 | } | |
619 | ||
620 | STATIC void | |
621 | S_del_xpvav(pTHX_ XPVAV *p) | |
622 | { | |
623 | LOCK_SV_MUTEX; | |
624 | p->xav_array = (char*)PL_xpvav_root; | |
625 | PL_xpvav_root = p; | |
626 | UNLOCK_SV_MUTEX; | |
627 | } | |
628 | ||
932e9ff9 VB |
629 | STATIC void |
630 | S_more_xpvav(pTHX) | |
631 | { | |
632 | register XPVAV* xpvav; | |
633 | register XPVAV* xpvavend; | |
612f20c3 GS |
634 | New(717, xpvav, 1008/sizeof(XPVAV), XPVAV); |
635 | xpvav->xav_array = (char*)PL_xpvav_arenaroot; | |
636 | PL_xpvav_arenaroot = xpvav; | |
637 | ||
932e9ff9 | 638 | xpvavend = &xpvav[1008 / sizeof(XPVAV) - 1]; |
612f20c3 | 639 | PL_xpvav_root = ++xpvav; |
932e9ff9 VB |
640 | while (xpvav < xpvavend) { |
641 | xpvav->xav_array = (char*)(xpvav + 1); | |
642 | xpvav++; | |
643 | } | |
644 | xpvav->xav_array = 0; | |
645 | } | |
646 | ||
932e9ff9 VB |
647 | STATIC XPVHV* |
648 | S_new_xpvhv(pTHX) | |
649 | { | |
650 | XPVHV* xpvhv; | |
651 | LOCK_SV_MUTEX; | |
652 | if (!PL_xpvhv_root) | |
653 | more_xpvhv(); | |
654 | xpvhv = PL_xpvhv_root; | |
655 | PL_xpvhv_root = (XPVHV*)xpvhv->xhv_array; | |
656 | UNLOCK_SV_MUTEX; | |
657 | return xpvhv; | |
658 | } | |
659 | ||
660 | STATIC void | |
661 | S_del_xpvhv(pTHX_ XPVHV *p) | |
662 | { | |
663 | LOCK_SV_MUTEX; | |
664 | p->xhv_array = (char*)PL_xpvhv_root; | |
665 | PL_xpvhv_root = p; | |
666 | UNLOCK_SV_MUTEX; | |
667 | } | |
668 | ||
932e9ff9 VB |
669 | STATIC void |
670 | S_more_xpvhv(pTHX) | |
671 | { | |
672 | register XPVHV* xpvhv; | |
673 | register XPVHV* xpvhvend; | |
612f20c3 GS |
674 | New(718, xpvhv, 1008/sizeof(XPVHV), XPVHV); |
675 | xpvhv->xhv_array = (char*)PL_xpvhv_arenaroot; | |
676 | PL_xpvhv_arenaroot = xpvhv; | |
677 | ||
932e9ff9 | 678 | xpvhvend = &xpvhv[1008 / sizeof(XPVHV) - 1]; |
612f20c3 | 679 | PL_xpvhv_root = ++xpvhv; |
932e9ff9 VB |
680 | while (xpvhv < xpvhvend) { |
681 | xpvhv->xhv_array = (char*)(xpvhv + 1); | |
682 | xpvhv++; | |
683 | } | |
684 | xpvhv->xhv_array = 0; | |
685 | } | |
686 | ||
932e9ff9 VB |
687 | STATIC XPVMG* |
688 | S_new_xpvmg(pTHX) | |
689 | { | |
690 | XPVMG* xpvmg; | |
691 | LOCK_SV_MUTEX; | |
692 | if (!PL_xpvmg_root) | |
693 | more_xpvmg(); | |
694 | xpvmg = PL_xpvmg_root; | |
695 | PL_xpvmg_root = (XPVMG*)xpvmg->xpv_pv; | |
696 | UNLOCK_SV_MUTEX; | |
697 | return xpvmg; | |
698 | } | |
699 | ||
700 | STATIC void | |
701 | S_del_xpvmg(pTHX_ XPVMG *p) | |
702 | { | |
703 | LOCK_SV_MUTEX; | |
704 | p->xpv_pv = (char*)PL_xpvmg_root; | |
705 | PL_xpvmg_root = p; | |
706 | UNLOCK_SV_MUTEX; | |
707 | } | |
708 | ||
932e9ff9 VB |
709 | STATIC void |
710 | S_more_xpvmg(pTHX) | |
711 | { | |
712 | register XPVMG* xpvmg; | |
713 | register XPVMG* xpvmgend; | |
612f20c3 GS |
714 | New(719, xpvmg, 1008/sizeof(XPVMG), XPVMG); |
715 | xpvmg->xpv_pv = (char*)PL_xpvmg_arenaroot; | |
716 | PL_xpvmg_arenaroot = xpvmg; | |
717 | ||
932e9ff9 | 718 | xpvmgend = &xpvmg[1008 / sizeof(XPVMG) - 1]; |
612f20c3 | 719 | PL_xpvmg_root = ++xpvmg; |
932e9ff9 VB |
720 | while (xpvmg < xpvmgend) { |
721 | xpvmg->xpv_pv = (char*)(xpvmg + 1); | |
722 | xpvmg++; | |
723 | } | |
724 | xpvmg->xpv_pv = 0; | |
725 | } | |
726 | ||
932e9ff9 VB |
727 | STATIC XPVLV* |
728 | S_new_xpvlv(pTHX) | |
729 | { | |
730 | XPVLV* xpvlv; | |
731 | LOCK_SV_MUTEX; | |
732 | if (!PL_xpvlv_root) | |
733 | more_xpvlv(); | |
734 | xpvlv = PL_xpvlv_root; | |
735 | PL_xpvlv_root = (XPVLV*)xpvlv->xpv_pv; | |
736 | UNLOCK_SV_MUTEX; | |
737 | return xpvlv; | |
738 | } | |
739 | ||
740 | STATIC void | |
741 | S_del_xpvlv(pTHX_ XPVLV *p) | |
742 | { | |
743 | LOCK_SV_MUTEX; | |
744 | p->xpv_pv = (char*)PL_xpvlv_root; | |
745 | PL_xpvlv_root = p; | |
746 | UNLOCK_SV_MUTEX; | |
747 | } | |
748 | ||
932e9ff9 VB |
749 | STATIC void |
750 | S_more_xpvlv(pTHX) | |
751 | { | |
752 | register XPVLV* xpvlv; | |
753 | register XPVLV* xpvlvend; | |
612f20c3 GS |
754 | New(720, xpvlv, 1008/sizeof(XPVLV), XPVLV); |
755 | xpvlv->xpv_pv = (char*)PL_xpvlv_arenaroot; | |
756 | PL_xpvlv_arenaroot = xpvlv; | |
757 | ||
932e9ff9 | 758 | xpvlvend = &xpvlv[1008 / sizeof(XPVLV) - 1]; |
612f20c3 | 759 | PL_xpvlv_root = ++xpvlv; |
932e9ff9 VB |
760 | while (xpvlv < xpvlvend) { |
761 | xpvlv->xpv_pv = (char*)(xpvlv + 1); | |
762 | xpvlv++; | |
763 | } | |
764 | xpvlv->xpv_pv = 0; | |
765 | } | |
766 | ||
932e9ff9 VB |
767 | STATIC XPVBM* |
768 | S_new_xpvbm(pTHX) | |
769 | { | |
770 | XPVBM* xpvbm; | |
771 | LOCK_SV_MUTEX; | |
772 | if (!PL_xpvbm_root) | |
773 | more_xpvbm(); | |
774 | xpvbm = PL_xpvbm_root; | |
775 | PL_xpvbm_root = (XPVBM*)xpvbm->xpv_pv; | |
776 | UNLOCK_SV_MUTEX; | |
777 | return xpvbm; | |
778 | } | |
779 | ||
780 | STATIC void | |
781 | S_del_xpvbm(pTHX_ XPVBM *p) | |
782 | { | |
783 | LOCK_SV_MUTEX; | |
784 | p->xpv_pv = (char*)PL_xpvbm_root; | |
785 | PL_xpvbm_root = p; | |
786 | UNLOCK_SV_MUTEX; | |
787 | } | |
788 | ||
932e9ff9 VB |
789 | STATIC void |
790 | S_more_xpvbm(pTHX) | |
791 | { | |
792 | register XPVBM* xpvbm; | |
793 | register XPVBM* xpvbmend; | |
612f20c3 GS |
794 | New(721, xpvbm, 1008/sizeof(XPVBM), XPVBM); |
795 | xpvbm->xpv_pv = (char*)PL_xpvbm_arenaroot; | |
796 | PL_xpvbm_arenaroot = xpvbm; | |
797 | ||
932e9ff9 | 798 | xpvbmend = &xpvbm[1008 / sizeof(XPVBM) - 1]; |
612f20c3 | 799 | PL_xpvbm_root = ++xpvbm; |
932e9ff9 VB |
800 | while (xpvbm < xpvbmend) { |
801 | xpvbm->xpv_pv = (char*)(xpvbm + 1); | |
802 | xpvbm++; | |
803 | } | |
804 | xpvbm->xpv_pv = 0; | |
805 | } | |
806 | ||
d33b2eba GS |
807 | #ifdef LEAKTEST |
808 | # define my_safemalloc(s) (void*)safexmalloc(717,s) | |
809 | # define my_safefree(p) safexfree((char*)p) | |
810 | #else | |
811 | # define my_safemalloc(s) (void*)safemalloc(s) | |
812 | # define my_safefree(p) safefree((char*)p) | |
813 | #endif | |
463ee0b2 | 814 | |
d33b2eba | 815 | #ifdef PURIFY |
463ee0b2 | 816 | |
d33b2eba GS |
817 | #define new_XIV() my_safemalloc(sizeof(XPVIV)) |
818 | #define del_XIV(p) my_safefree(p) | |
ed6116ce | 819 | |
d33b2eba GS |
820 | #define new_XNV() my_safemalloc(sizeof(XPVNV)) |
821 | #define del_XNV(p) my_safefree(p) | |
463ee0b2 | 822 | |
d33b2eba GS |
823 | #define new_XRV() my_safemalloc(sizeof(XRV)) |
824 | #define del_XRV(p) my_safefree(p) | |
8c52afec | 825 | |
d33b2eba GS |
826 | #define new_XPV() my_safemalloc(sizeof(XPV)) |
827 | #define del_XPV(p) my_safefree(p) | |
9b94d1dd | 828 | |
d33b2eba GS |
829 | #define new_XPVIV() my_safemalloc(sizeof(XPVIV)) |
830 | #define del_XPVIV(p) my_safefree(p) | |
932e9ff9 | 831 | |
d33b2eba GS |
832 | #define new_XPVNV() my_safemalloc(sizeof(XPVNV)) |
833 | #define del_XPVNV(p) my_safefree(p) | |
932e9ff9 | 834 | |
d33b2eba GS |
835 | #define new_XPVCV() my_safemalloc(sizeof(XPVCV)) |
836 | #define del_XPVCV(p) my_safefree(p) | |
932e9ff9 | 837 | |
d33b2eba GS |
838 | #define new_XPVAV() my_safemalloc(sizeof(XPVAV)) |
839 | #define del_XPVAV(p) my_safefree(p) | |
840 | ||
841 | #define new_XPVHV() my_safemalloc(sizeof(XPVHV)) | |
842 | #define del_XPVHV(p) my_safefree(p) | |
1c846c1f | 843 | |
d33b2eba GS |
844 | #define new_XPVMG() my_safemalloc(sizeof(XPVMG)) |
845 | #define del_XPVMG(p) my_safefree(p) | |
846 | ||
847 | #define new_XPVLV() my_safemalloc(sizeof(XPVLV)) | |
848 | #define del_XPVLV(p) my_safefree(p) | |
849 | ||
850 | #define new_XPVBM() my_safemalloc(sizeof(XPVBM)) | |
851 | #define del_XPVBM(p) my_safefree(p) | |
852 | ||
853 | #else /* !PURIFY */ | |
854 | ||
855 | #define new_XIV() (void*)new_xiv() | |
856 | #define del_XIV(p) del_xiv((XPVIV*) p) | |
857 | ||
858 | #define new_XNV() (void*)new_xnv() | |
859 | #define del_XNV(p) del_xnv((XPVNV*) p) | |
9b94d1dd | 860 | |
d33b2eba GS |
861 | #define new_XRV() (void*)new_xrv() |
862 | #define del_XRV(p) del_xrv((XRV*) p) | |
9b94d1dd | 863 | |
d33b2eba GS |
864 | #define new_XPV() (void*)new_xpv() |
865 | #define del_XPV(p) del_xpv((XPV *)p) | |
866 | ||
867 | #define new_XPVIV() (void*)new_xpviv() | |
868 | #define del_XPVIV(p) del_xpviv((XPVIV *)p) | |
869 | ||
870 | #define new_XPVNV() (void*)new_xpvnv() | |
871 | #define del_XPVNV(p) del_xpvnv((XPVNV *)p) | |
872 | ||
873 | #define new_XPVCV() (void*)new_xpvcv() | |
874 | #define del_XPVCV(p) del_xpvcv((XPVCV *)p) | |
875 | ||
876 | #define new_XPVAV() (void*)new_xpvav() | |
877 | #define del_XPVAV(p) del_xpvav((XPVAV *)p) | |
878 | ||
879 | #define new_XPVHV() (void*)new_xpvhv() | |
880 | #define del_XPVHV(p) del_xpvhv((XPVHV *)p) | |
1c846c1f | 881 | |
d33b2eba GS |
882 | #define new_XPVMG() (void*)new_xpvmg() |
883 | #define del_XPVMG(p) del_xpvmg((XPVMG *)p) | |
884 | ||
885 | #define new_XPVLV() (void*)new_xpvlv() | |
886 | #define del_XPVLV(p) del_xpvlv((XPVLV *)p) | |
887 | ||
888 | #define new_XPVBM() (void*)new_xpvbm() | |
889 | #define del_XPVBM(p) del_xpvbm((XPVBM *)p) | |
890 | ||
891 | #endif /* PURIFY */ | |
9b94d1dd | 892 | |
d33b2eba GS |
893 | #define new_XPVGV() my_safemalloc(sizeof(XPVGV)) |
894 | #define del_XPVGV(p) my_safefree(p) | |
1c846c1f | 895 | |
d33b2eba GS |
896 | #define new_XPVFM() my_safemalloc(sizeof(XPVFM)) |
897 | #define del_XPVFM(p) my_safefree(p) | |
1c846c1f | 898 | |
d33b2eba GS |
899 | #define new_XPVIO() my_safemalloc(sizeof(XPVIO)) |
900 | #define del_XPVIO(p) my_safefree(p) | |
8990e307 | 901 | |
954c1994 GS |
902 | /* |
903 | =for apidoc sv_upgrade | |
904 | ||
905 | Upgrade an SV to a more complex form. Use C<SvUPGRADE>. See | |
906 | C<svtype>. | |
907 | ||
908 | =cut | |
909 | */ | |
910 | ||
79072805 | 911 | bool |
864dbfa3 | 912 | Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt) |
79072805 LW |
913 | { |
914 | char* pv; | |
915 | U32 cur; | |
916 | U32 len; | |
a0d0e21e | 917 | IV iv; |
65202027 | 918 | NV nv; |
79072805 LW |
919 | MAGIC* magic; |
920 | HV* stash; | |
921 | ||
f130fd45 NIS |
922 | if (mt != SVt_PV && SvREADONLY(sv) && SvFAKE(sv)) { |
923 | sv_force_normal(sv); | |
924 | } | |
925 | ||
79072805 LW |
926 | if (SvTYPE(sv) == mt) |
927 | return TRUE; | |
928 | ||
a5f75d66 AD |
929 | if (mt < SVt_PVIV) |
930 | (void)SvOOK_off(sv); | |
931 | ||
79072805 LW |
932 | switch (SvTYPE(sv)) { |
933 | case SVt_NULL: | |
934 | pv = 0; | |
935 | cur = 0; | |
936 | len = 0; | |
937 | iv = 0; | |
938 | nv = 0.0; | |
939 | magic = 0; | |
940 | stash = 0; | |
941 | break; | |
79072805 LW |
942 | case SVt_IV: |
943 | pv = 0; | |
944 | cur = 0; | |
945 | len = 0; | |
463ee0b2 | 946 | iv = SvIVX(sv); |
65202027 | 947 | nv = (NV)SvIVX(sv); |
79072805 LW |
948 | del_XIV(SvANY(sv)); |
949 | magic = 0; | |
950 | stash = 0; | |
ed6116ce | 951 | if (mt == SVt_NV) |
463ee0b2 | 952 | mt = SVt_PVNV; |
ed6116ce LW |
953 | else if (mt < SVt_PVIV) |
954 | mt = SVt_PVIV; | |
79072805 LW |
955 | break; |
956 | case SVt_NV: | |
957 | pv = 0; | |
958 | cur = 0; | |
959 | len = 0; | |
463ee0b2 | 960 | nv = SvNVX(sv); |
1bd302c3 | 961 | iv = I_V(nv); |
79072805 LW |
962 | magic = 0; |
963 | stash = 0; | |
964 | del_XNV(SvANY(sv)); | |
965 | SvANY(sv) = 0; | |
ed6116ce | 966 | if (mt < SVt_PVNV) |
79072805 LW |
967 | mt = SVt_PVNV; |
968 | break; | |
ed6116ce LW |
969 | case SVt_RV: |
970 | pv = (char*)SvRV(sv); | |
971 | cur = 0; | |
972 | len = 0; | |
56431972 RB |
973 | iv = PTR2IV(pv); |
974 | nv = PTR2NV(pv); | |
ed6116ce LW |
975 | del_XRV(SvANY(sv)); |
976 | magic = 0; | |
977 | stash = 0; | |
978 | break; | |
79072805 | 979 | case SVt_PV: |
463ee0b2 | 980 | pv = SvPVX(sv); |
79072805 LW |
981 | cur = SvCUR(sv); |
982 | len = SvLEN(sv); | |
983 | iv = 0; | |
984 | nv = 0.0; | |
985 | magic = 0; | |
986 | stash = 0; | |
987 | del_XPV(SvANY(sv)); | |
748a9306 LW |
988 | if (mt <= SVt_IV) |
989 | mt = SVt_PVIV; | |
990 | else if (mt == SVt_NV) | |
991 | mt = SVt_PVNV; | |
79072805 LW |
992 | break; |
993 | case SVt_PVIV: | |
463ee0b2 | 994 | pv = SvPVX(sv); |
79072805 LW |
995 | cur = SvCUR(sv); |
996 | len = SvLEN(sv); | |
463ee0b2 | 997 | iv = SvIVX(sv); |
79072805 LW |
998 | nv = 0.0; |
999 | magic = 0; | |
1000 | stash = 0; | |
1001 | del_XPVIV(SvANY(sv)); | |
1002 | break; | |
1003 | case SVt_PVNV: | |
463ee0b2 | 1004 | pv = SvPVX(sv); |
79072805 LW |
1005 | cur = SvCUR(sv); |
1006 | len = SvLEN(sv); | |
463ee0b2 LW |
1007 | iv = SvIVX(sv); |
1008 | nv = SvNVX(sv); | |
79072805 LW |
1009 | magic = 0; |
1010 | stash = 0; | |
1011 | del_XPVNV(SvANY(sv)); | |
1012 | break; | |
1013 | case SVt_PVMG: | |
463ee0b2 | 1014 | pv = SvPVX(sv); |
79072805 LW |
1015 | cur = SvCUR(sv); |
1016 | len = SvLEN(sv); | |
463ee0b2 LW |
1017 | iv = SvIVX(sv); |
1018 | nv = SvNVX(sv); | |
79072805 LW |
1019 | magic = SvMAGIC(sv); |
1020 | stash = SvSTASH(sv); | |
1021 | del_XPVMG(SvANY(sv)); | |
1022 | break; | |
1023 | default: | |
cea2e8a9 | 1024 | Perl_croak(aTHX_ "Can't upgrade that kind of scalar"); |
79072805 LW |
1025 | } |
1026 | ||
1027 | switch (mt) { | |
1028 | case SVt_NULL: | |
cea2e8a9 | 1029 | Perl_croak(aTHX_ "Can't upgrade to undef"); |
79072805 LW |
1030 | case SVt_IV: |
1031 | SvANY(sv) = new_XIV(); | |
463ee0b2 | 1032 | SvIVX(sv) = iv; |
79072805 LW |
1033 | break; |
1034 | case SVt_NV: | |
1035 | SvANY(sv) = new_XNV(); | |
463ee0b2 | 1036 | SvNVX(sv) = nv; |
79072805 | 1037 | break; |
ed6116ce LW |
1038 | case SVt_RV: |
1039 | SvANY(sv) = new_XRV(); | |
1040 | SvRV(sv) = (SV*)pv; | |
ed6116ce | 1041 | break; |
79072805 LW |
1042 | case SVt_PV: |
1043 | SvANY(sv) = new_XPV(); | |
463ee0b2 | 1044 | SvPVX(sv) = pv; |
79072805 LW |
1045 | SvCUR(sv) = cur; |
1046 | SvLEN(sv) = len; | |
1047 | break; | |
1048 | case SVt_PVIV: | |
1049 | SvANY(sv) = new_XPVIV(); | |
463ee0b2 | 1050 | SvPVX(sv) = pv; |
79072805 LW |
1051 | SvCUR(sv) = cur; |
1052 | SvLEN(sv) = len; | |
463ee0b2 | 1053 | SvIVX(sv) = iv; |
79072805 | 1054 | if (SvNIOK(sv)) |
a0d0e21e | 1055 | (void)SvIOK_on(sv); |
79072805 LW |
1056 | SvNOK_off(sv); |
1057 | break; | |
1058 | case SVt_PVNV: | |
1059 | SvANY(sv) = new_XPVNV(); | |
463ee0b2 | 1060 | SvPVX(sv) = pv; |
79072805 LW |
1061 | SvCUR(sv) = cur; |
1062 | SvLEN(sv) = len; | |
463ee0b2 LW |
1063 | SvIVX(sv) = iv; |
1064 | SvNVX(sv) = nv; | |
79072805 LW |
1065 | break; |
1066 | case SVt_PVMG: | |
1067 | SvANY(sv) = new_XPVMG(); | |
463ee0b2 | 1068 | SvPVX(sv) = pv; |
79072805 LW |
1069 | SvCUR(sv) = cur; |
1070 | SvLEN(sv) = len; | |
463ee0b2 LW |
1071 | SvIVX(sv) = iv; |
1072 | SvNVX(sv) = nv; | |
79072805 LW |
1073 | SvMAGIC(sv) = magic; |
1074 | SvSTASH(sv) = stash; | |
1075 | break; | |
1076 | case SVt_PVLV: | |
1077 | SvANY(sv) = new_XPVLV(); | |
463ee0b2 | 1078 | SvPVX(sv) = pv; |
79072805 LW |
1079 | SvCUR(sv) = cur; |
1080 | SvLEN(sv) = len; | |
463ee0b2 LW |
1081 | SvIVX(sv) = iv; |
1082 | SvNVX(sv) = nv; | |
79072805 LW |
1083 | SvMAGIC(sv) = magic; |
1084 | SvSTASH(sv) = stash; | |
1085 | LvTARGOFF(sv) = 0; | |
1086 | LvTARGLEN(sv) = 0; | |
1087 | LvTARG(sv) = 0; | |
1088 | LvTYPE(sv) = 0; | |
1089 | break; | |
1090 | case SVt_PVAV: | |
1091 | SvANY(sv) = new_XPVAV(); | |
463ee0b2 LW |
1092 | if (pv) |
1093 | Safefree(pv); | |
2304df62 | 1094 | SvPVX(sv) = 0; |
d1bf51dd | 1095 | AvMAX(sv) = -1; |
93965878 | 1096 | AvFILLp(sv) = -1; |
463ee0b2 LW |
1097 | SvIVX(sv) = 0; |
1098 | SvNVX(sv) = 0.0; | |
1099 | SvMAGIC(sv) = magic; | |
1100 | SvSTASH(sv) = stash; | |
1101 | AvALLOC(sv) = 0; | |
79072805 LW |
1102 | AvARYLEN(sv) = 0; |
1103 | AvFLAGS(sv) = 0; | |
1104 | break; | |
1105 | case SVt_PVHV: | |
1106 | SvANY(sv) = new_XPVHV(); | |
463ee0b2 LW |
1107 | if (pv) |
1108 | Safefree(pv); | |
1109 | SvPVX(sv) = 0; | |
1110 | HvFILL(sv) = 0; | |
1111 | HvMAX(sv) = 0; | |
1112 | HvKEYS(sv) = 0; | |
1113 | SvNVX(sv) = 0.0; | |
79072805 LW |
1114 | SvMAGIC(sv) = magic; |
1115 | SvSTASH(sv) = stash; | |
79072805 LW |
1116 | HvRITER(sv) = 0; |
1117 | HvEITER(sv) = 0; | |
1118 | HvPMROOT(sv) = 0; | |
1119 | HvNAME(sv) = 0; | |
79072805 LW |
1120 | break; |
1121 | case SVt_PVCV: | |
1122 | SvANY(sv) = new_XPVCV(); | |
748a9306 | 1123 | Zero(SvANY(sv), 1, XPVCV); |
463ee0b2 | 1124 | SvPVX(sv) = pv; |
79072805 LW |
1125 | SvCUR(sv) = cur; |
1126 | SvLEN(sv) = len; | |
463ee0b2 LW |
1127 | SvIVX(sv) = iv; |
1128 | SvNVX(sv) = nv; | |
79072805 LW |
1129 | SvMAGIC(sv) = magic; |
1130 | SvSTASH(sv) = stash; | |
79072805 LW |
1131 | break; |
1132 | case SVt_PVGV: | |
1133 | SvANY(sv) = new_XPVGV(); | |
463ee0b2 | 1134 | SvPVX(sv) = pv; |
79072805 LW |
1135 | SvCUR(sv) = cur; |
1136 | SvLEN(sv) = len; | |
463ee0b2 LW |
1137 | SvIVX(sv) = iv; |
1138 | SvNVX(sv) = nv; | |
79072805 LW |
1139 | SvMAGIC(sv) = magic; |
1140 | SvSTASH(sv) = stash; | |
93a17b20 | 1141 | GvGP(sv) = 0; |
79072805 LW |
1142 | GvNAME(sv) = 0; |
1143 | GvNAMELEN(sv) = 0; | |
1144 | GvSTASH(sv) = 0; | |
a5f75d66 | 1145 | GvFLAGS(sv) = 0; |
79072805 LW |
1146 | break; |
1147 | case SVt_PVBM: | |
1148 | SvANY(sv) = new_XPVBM(); | |
463ee0b2 | 1149 | SvPVX(sv) = pv; |
79072805 LW |
1150 | SvCUR(sv) = cur; |
1151 | SvLEN(sv) = len; | |
463ee0b2 LW |
1152 | SvIVX(sv) = iv; |
1153 | SvNVX(sv) = nv; | |
79072805 LW |
1154 | SvMAGIC(sv) = magic; |
1155 | SvSTASH(sv) = stash; | |
1156 | BmRARE(sv) = 0; | |
1157 | BmUSEFUL(sv) = 0; | |
1158 | BmPREVIOUS(sv) = 0; | |
1159 | break; | |
1160 | case SVt_PVFM: | |
1161 | SvANY(sv) = new_XPVFM(); | |
748a9306 | 1162 | Zero(SvANY(sv), 1, XPVFM); |
463ee0b2 | 1163 | SvPVX(sv) = pv; |
79072805 LW |
1164 | SvCUR(sv) = cur; |
1165 | SvLEN(sv) = len; | |
463ee0b2 LW |
1166 | SvIVX(sv) = iv; |
1167 | SvNVX(sv) = nv; | |
79072805 LW |
1168 | SvMAGIC(sv) = magic; |
1169 | SvSTASH(sv) = stash; | |
79072805 | 1170 | break; |
8990e307 LW |
1171 | case SVt_PVIO: |
1172 | SvANY(sv) = new_XPVIO(); | |
748a9306 | 1173 | Zero(SvANY(sv), 1, XPVIO); |
8990e307 LW |
1174 | SvPVX(sv) = pv; |
1175 | SvCUR(sv) = cur; | |
1176 | SvLEN(sv) = len; | |
1177 | SvIVX(sv) = iv; | |
1178 | SvNVX(sv) = nv; | |
1179 | SvMAGIC(sv) = magic; | |
1180 | SvSTASH(sv) = stash; | |
85e6fe83 | 1181 | IoPAGE_LEN(sv) = 60; |
8990e307 LW |
1182 | break; |
1183 | } | |
1184 | SvFLAGS(sv) &= ~SVTYPEMASK; | |
1185 | SvFLAGS(sv) |= mt; | |
79072805 LW |
1186 | return TRUE; |
1187 | } | |
1188 | ||
79072805 | 1189 | int |
864dbfa3 | 1190 | Perl_sv_backoff(pTHX_ register SV *sv) |
79072805 LW |
1191 | { |
1192 | assert(SvOOK(sv)); | |
463ee0b2 LW |
1193 | if (SvIVX(sv)) { |
1194 | char *s = SvPVX(sv); | |
1195 | SvLEN(sv) += SvIVX(sv); | |
1196 | SvPVX(sv) -= SvIVX(sv); | |
79072805 | 1197 | SvIV_set(sv, 0); |
463ee0b2 | 1198 | Move(s, SvPVX(sv), SvCUR(sv)+1, char); |
79072805 LW |
1199 | } |
1200 | SvFLAGS(sv) &= ~SVf_OOK; | |
a0d0e21e | 1201 | return 0; |
79072805 LW |
1202 | } |
1203 | ||
954c1994 GS |
1204 | /* |
1205 | =for apidoc sv_grow | |
1206 | ||
1207 | Expands the character buffer in the SV. This will use C<sv_unref> and will | |
1208 | upgrade the SV to C<SVt_PV>. Returns a pointer to the character buffer. | |
1209 | Use C<SvGROW>. | |
1210 | ||
1211 | =cut | |
1212 | */ | |
1213 | ||
79072805 | 1214 | char * |
864dbfa3 | 1215 | Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen) |
79072805 LW |
1216 | { |
1217 | register char *s; | |
1218 | ||
55497cff | 1219 | #ifdef HAS_64K_LIMIT |
79072805 | 1220 | if (newlen >= 0x10000) { |
1d7c1841 GS |
1221 | PerlIO_printf(Perl_debug_log, |
1222 | "Allocation too large: %"UVxf"\n", (UV)newlen); | |
79072805 LW |
1223 | my_exit(1); |
1224 | } | |
55497cff | 1225 | #endif /* HAS_64K_LIMIT */ |
a0d0e21e LW |
1226 | if (SvROK(sv)) |
1227 | sv_unref(sv); | |
79072805 LW |
1228 | if (SvTYPE(sv) < SVt_PV) { |
1229 | sv_upgrade(sv, SVt_PV); | |
463ee0b2 | 1230 | s = SvPVX(sv); |
79072805 LW |
1231 | } |
1232 | else if (SvOOK(sv)) { /* pv is offset? */ | |
1233 | sv_backoff(sv); | |
463ee0b2 | 1234 | s = SvPVX(sv); |
79072805 LW |
1235 | if (newlen > SvLEN(sv)) |
1236 | newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */ | |
c6f8c383 GA |
1237 | #ifdef HAS_64K_LIMIT |
1238 | if (newlen >= 0x10000) | |
1239 | newlen = 0xFFFF; | |
1240 | #endif | |
79072805 LW |
1241 | } |
1242 | else | |
463ee0b2 | 1243 | s = SvPVX(sv); |
79072805 | 1244 | if (newlen > SvLEN(sv)) { /* need more room? */ |
8d6dde3e | 1245 | if (SvLEN(sv) && s) { |
f5a32c7f | 1246 | #if defined(MYMALLOC) && !defined(LEAKTEST) |
8d6dde3e IZ |
1247 | STRLEN l = malloced_size((void*)SvPVX(sv)); |
1248 | if (newlen <= l) { | |
1249 | SvLEN_set(sv, l); | |
1250 | return s; | |
1251 | } else | |
c70c8a0a | 1252 | #endif |
79072805 | 1253 | Renew(s,newlen,char); |
8d6dde3e | 1254 | } |
79072805 LW |
1255 | else |
1256 | New(703,s,newlen,char); | |
1257 | SvPV_set(sv, s); | |
1258 | SvLEN_set(sv, newlen); | |
1259 | } | |
1260 | return s; | |
1261 | } | |
1262 | ||
954c1994 GS |
1263 | /* |
1264 | =for apidoc sv_setiv | |
1265 | ||
1266 | Copies an integer into the given SV. Does not handle 'set' magic. See | |
1267 | C<sv_setiv_mg>. | |
1268 | ||
1269 | =cut | |
1270 | */ | |
1271 | ||
79072805 | 1272 | void |
864dbfa3 | 1273 | Perl_sv_setiv(pTHX_ register SV *sv, IV i) |
79072805 | 1274 | { |
2213622d | 1275 | SV_CHECK_THINKFIRST(sv); |
463ee0b2 LW |
1276 | switch (SvTYPE(sv)) { |
1277 | case SVt_NULL: | |
79072805 | 1278 | sv_upgrade(sv, SVt_IV); |
463ee0b2 LW |
1279 | break; |
1280 | case SVt_NV: | |
1281 | sv_upgrade(sv, SVt_PVNV); | |
1282 | break; | |
ed6116ce | 1283 | case SVt_RV: |
463ee0b2 | 1284 | case SVt_PV: |
79072805 | 1285 | sv_upgrade(sv, SVt_PVIV); |
463ee0b2 | 1286 | break; |
a0d0e21e LW |
1287 | |
1288 | case SVt_PVGV: | |
a0d0e21e LW |
1289 | case SVt_PVAV: |
1290 | case SVt_PVHV: | |
1291 | case SVt_PVCV: | |
1292 | case SVt_PVFM: | |
1293 | case SVt_PVIO: | |
411caa50 JH |
1294 | Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0), |
1295 | PL_op_desc[PL_op->op_type]); | |
463ee0b2 | 1296 | } |
a0d0e21e | 1297 | (void)SvIOK_only(sv); /* validate number */ |
a5f75d66 | 1298 | SvIVX(sv) = i; |
463ee0b2 | 1299 | SvTAINT(sv); |
79072805 LW |
1300 | } |
1301 | ||
954c1994 GS |
1302 | /* |
1303 | =for apidoc sv_setiv_mg | |
1304 | ||
1305 | Like C<sv_setiv>, but also handles 'set' magic. | |
1306 | ||
1307 | =cut | |
1308 | */ | |
1309 | ||
79072805 | 1310 | void |
864dbfa3 | 1311 | Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i) |
ef50df4b GS |
1312 | { |
1313 | sv_setiv(sv,i); | |
1314 | SvSETMAGIC(sv); | |
1315 | } | |
1316 | ||
954c1994 GS |
1317 | /* |
1318 | =for apidoc sv_setuv | |
1319 | ||
1320 | Copies an unsigned integer into the given SV. Does not handle 'set' magic. | |
1321 | See C<sv_setuv_mg>. | |
1322 | ||
1323 | =cut | |
1324 | */ | |
1325 | ||
ef50df4b | 1326 | void |
864dbfa3 | 1327 | Perl_sv_setuv(pTHX_ register SV *sv, UV u) |
55497cff | 1328 | { |
55ada374 NC |
1329 | /* With these two if statements: |
1330 | u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865 | |
d460ef45 | 1331 | |
55ada374 NC |
1332 | without |
1333 | u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865 | |
d460ef45 | 1334 | |
55ada374 NC |
1335 | If you wish to remove them, please benchmark to see what the effect is |
1336 | */ | |
28e5dec8 JH |
1337 | if (u <= (UV)IV_MAX) { |
1338 | sv_setiv(sv, (IV)u); | |
1339 | return; | |
1340 | } | |
25da4f38 IZ |
1341 | sv_setiv(sv, 0); |
1342 | SvIsUV_on(sv); | |
1343 | SvUVX(sv) = u; | |
55497cff | 1344 | } |
1345 | ||
954c1994 GS |
1346 | /* |
1347 | =for apidoc sv_setuv_mg | |
1348 | ||
1349 | Like C<sv_setuv>, but also handles 'set' magic. | |
1350 | ||
1351 | =cut | |
1352 | */ | |
1353 | ||
55497cff | 1354 | void |
864dbfa3 | 1355 | Perl_sv_setuv_mg(pTHX_ register SV *sv, UV u) |
ef50df4b | 1356 | { |
55ada374 NC |
1357 | /* With these two if statements: |
1358 | u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865 | |
d460ef45 | 1359 | |
55ada374 NC |
1360 | without |
1361 | u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865 | |
d460ef45 | 1362 | |
55ada374 NC |
1363 | If you wish to remove them, please benchmark to see what the effect is |
1364 | */ | |
28e5dec8 JH |
1365 | if (u <= (UV)IV_MAX) { |
1366 | sv_setiv(sv, (IV)u); | |
1367 | } else { | |
1368 | sv_setiv(sv, 0); | |
1369 | SvIsUV_on(sv); | |
1370 | sv_setuv(sv,u); | |
1371 | } | |
ef50df4b GS |
1372 | SvSETMAGIC(sv); |
1373 | } | |
1374 | ||
954c1994 GS |
1375 | /* |
1376 | =for apidoc sv_setnv | |
1377 | ||
1378 | Copies a double into the given SV. Does not handle 'set' magic. See | |
1379 | C<sv_setnv_mg>. | |
1380 | ||
1381 | =cut | |
1382 | */ | |
1383 | ||
ef50df4b | 1384 | void |
65202027 | 1385 | Perl_sv_setnv(pTHX_ register SV *sv, NV num) |
79072805 | 1386 | { |
2213622d | 1387 | SV_CHECK_THINKFIRST(sv); |
a0d0e21e LW |
1388 | switch (SvTYPE(sv)) { |
1389 | case SVt_NULL: | |
1390 | case SVt_IV: | |
79072805 | 1391 | sv_upgrade(sv, SVt_NV); |
a0d0e21e | 1392 | break; |
a0d0e21e LW |
1393 | case SVt_RV: |
1394 | case SVt_PV: | |
1395 | case SVt_PVIV: | |
79072805 | 1396 | sv_upgrade(sv, SVt_PVNV); |
a0d0e21e | 1397 | break; |
827b7e14 | 1398 | |
a0d0e21e | 1399 | case SVt_PVGV: |
a0d0e21e LW |
1400 | case SVt_PVAV: |
1401 | case SVt_PVHV: | |
1402 | case SVt_PVCV: | |
1403 | case SVt_PVFM: | |
1404 | case SVt_PVIO: | |
411caa50 JH |
1405 | Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0), |
1406 | PL_op_name[PL_op->op_type]); | |
79072805 | 1407 | } |
463ee0b2 | 1408 | SvNVX(sv) = num; |
a0d0e21e | 1409 | (void)SvNOK_only(sv); /* validate number */ |
463ee0b2 | 1410 | SvTAINT(sv); |
79072805 LW |
1411 | } |
1412 | ||
954c1994 GS |
1413 | /* |
1414 | =for apidoc sv_setnv_mg | |
1415 | ||
1416 | Like C<sv_setnv>, but also handles 'set' magic. | |
1417 | ||
1418 | =cut | |
1419 | */ | |
1420 | ||
ef50df4b | 1421 | void |
65202027 | 1422 | Perl_sv_setnv_mg(pTHX_ register SV *sv, NV num) |
ef50df4b GS |
1423 | { |
1424 | sv_setnv(sv,num); | |
1425 | SvSETMAGIC(sv); | |
1426 | } | |
1427 | ||
76e3520e | 1428 | STATIC void |
cea2e8a9 | 1429 | S_not_a_number(pTHX_ SV *sv) |
a0d0e21e LW |
1430 | { |
1431 | char tmpbuf[64]; | |
1432 | char *d = tmpbuf; | |
1433 | char *s; | |
dc28f22b GA |
1434 | char *limit = tmpbuf + sizeof(tmpbuf) - 8; |
1435 | /* each *s can expand to 4 chars + "...\0", | |
1436 | i.e. need room for 8 chars */ | |
a0d0e21e | 1437 | |
dc28f22b | 1438 | for (s = SvPVX(sv); *s && d < limit; s++) { |
bbce6d69 | 1439 | int ch = *s & 0xFF; |
1440 | if (ch & 128 && !isPRINT_LC(ch)) { | |
a0d0e21e LW |
1441 | *d++ = 'M'; |
1442 | *d++ = '-'; | |
1443 | ch &= 127; | |
1444 | } | |
bbce6d69 | 1445 | if (ch == '\n') { |
1446 | *d++ = '\\'; | |
1447 | *d++ = 'n'; | |
1448 | } | |
1449 | else if (ch == '\r') { | |
1450 | *d++ = '\\'; | |
1451 | *d++ = 'r'; | |
1452 | } | |
1453 | else if (ch == '\f') { | |
1454 | *d++ = '\\'; | |
1455 | *d++ = 'f'; | |
1456 | } | |
1457 | else if (ch == '\\') { | |
1458 | *d++ = '\\'; | |
1459 | *d++ = '\\'; | |
1460 | } | |
1461 | else if (isPRINT_LC(ch)) | |
a0d0e21e LW |
1462 | *d++ = ch; |
1463 | else { | |
1464 | *d++ = '^'; | |
bbce6d69 | 1465 | *d++ = toCTRL(ch); |
a0d0e21e LW |
1466 | } |
1467 | } | |
1468 | if (*s) { | |
1469 | *d++ = '.'; | |
1470 | *d++ = '.'; | |
1471 | *d++ = '.'; | |
1472 | } | |
1473 | *d = '\0'; | |
1474 | ||
533c011a | 1475 | if (PL_op) |
42d38218 MS |
1476 | Perl_warner(aTHX_ WARN_NUMERIC, |
1477 | "Argument \"%s\" isn't numeric in %s", tmpbuf, | |
1478 | PL_op_desc[PL_op->op_type]); | |
a0d0e21e | 1479 | else |
42d38218 MS |
1480 | Perl_warner(aTHX_ WARN_NUMERIC, |
1481 | "Argument \"%s\" isn't numeric", tmpbuf); | |
a0d0e21e LW |
1482 | } |
1483 | ||
28e5dec8 JH |
1484 | /* the number can be converted to integer with atol() or atoll() although */ |
1485 | #define IS_NUMBER_TO_INT_BY_ATOL 0x01 /* integer (may have decimals) */ | |
1486 | #define IS_NUMBER_TO_INT_BY_STRTOL 0x02 /* it may exceed IV_MAX */ | |
1487 | #define IS_NUMBER_TO_INT_BY_ATOF 0x04 /* seen something like 123e4 */ | |
1488 | #define IS_NUMBER_LONGER_THAN_IV_MAX 0x08 /* more digits than IV_MAX */ | |
1489 | #define IS_NUMBER_AS_LONG_AS_IV_MAX 0x10 /* may(be not) larger than IV_MAX */ | |
1490 | #define IS_NUMBER_NOT_INT 0x20 /* seen a decimal point or e */ | |
1491 | #define IS_NUMBER_NEG 0x40 /* seen a leading - */ | |
1492 | #define IS_NUMBER_INFINITY 0x80 /* /^\s*-?Infinity\s*$/i */ | |
25da4f38 IZ |
1493 | |
1494 | /* Actually, ISO C leaves conversion of UV to IV undefined, but | |
1495 | until proven guilty, assume that things are not that bad... */ | |
1496 | ||
28e5dec8 JH |
1497 | /* As 64 bit platforms often have an NV that doesn't preserve all bits of |
1498 | an IV (an assumption perl has been based on to date) it becomes necessary | |
1499 | to remove the assumption that the NV always carries enough precision to | |
1500 | recreate the IV whenever needed, and that the NV is the canonical form. | |
1501 | Instead, IV/UV and NV need to be given equal rights. So as to not lose | |
1502 | precision as an side effect of conversion (which would lead to insanity | |
1503 | and the dragon(s) in t/op/numconvert.t getting very angry) the intent is | |
1504 | 1) to distinguish between IV/UV/NV slots that have cached a valid | |
1505 | conversion where precision was lost and IV/UV/NV slots that have a | |
1506 | valid conversion which has lost no precision | |
1507 | 2) to ensure that if a numeric conversion to one form is request that | |
1508 | would lose precision, the precise conversion (or differently | |
1509 | imprecise conversion) is also performed and cached, to prevent | |
1510 | requests for different numeric formats on the same SV causing | |
1511 | lossy conversion chains. (lossless conversion chains are perfectly | |
1512 | acceptable (still)) | |
1513 | ||
1514 | ||
1515 | flags are used: | |
1516 | SvIOKp is true if the IV slot contains a valid value | |
1517 | SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true) | |
1518 | SvNOKp is true if the NV slot contains a valid value | |
1519 | SvNOK is true only if the NV value is accurate | |
1520 | ||
1521 | so | |
1522 | while converting from PV to NV check to see if converting that NV to an | |
1523 | IV(or UV) would lose accuracy over a direct conversion from PV to | |
1524 | IV(or UV). If it would, cache both conversions, return NV, but mark | |
1525 | SV as IOK NOKp (ie not NOK). | |
1526 | ||
1527 | while converting from PV to IV check to see if converting that IV to an | |
1528 | NV would lose accuracy over a direct conversion from PV to NV. If it | |
1529 | would, cache both conversions, flag similarly. | |
1530 | ||
1531 | Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite | |
1532 | correctly because if IV & NV were set NV *always* overruled. | |
1533 | Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flags meaning | |
1534 | changes - now IV and NV together means that the two are interchangeable | |
1535 | SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX; | |
d460ef45 | 1536 | |
28e5dec8 JH |
1537 | The benefit of this is operations such as pp_add know that if SvIOK is |
1538 | true for both left and right operands, then integer addition can be | |
1539 | used instead of floating point. (for cases where the result won't | |
1540 | overflow) Before, floating point was always used, which could lead to | |
1541 | loss of precision compared with integer addition. | |
1542 | ||
1543 | * making IV and NV equal status should make maths accurate on 64 bit | |
1544 | platforms | |
1545 | * may speed up maths somewhat if pp_add and friends start to use | |
1546 | integers when possible instead of fp. (hopefully the overhead in | |
1547 | looking for SvIOK and checking for overflow will not outweigh the | |
1548 | fp to integer speedup) | |
1549 | * will slow down integer operations (callers of SvIV) on "inaccurate" | |
1550 | values, as the change from SvIOK to SvIOKp will cause a call into | |
1551 | sv_2iv each time rather than a macro access direct to the IV slot | |
1552 | * should speed up number->string conversion on integers as IV is | |
1553 | favoured when IV and NV equally accurate | |
1554 | ||
1555 | #################################################################### | |
1556 | You had better be using SvIOK_notUV if you want an IV for arithmetic | |
1557 | SvIOK is true if (IV or UV), so you might be getting (IV)SvUV | |
1558 | SvUOK is true iff UV. | |
1559 | #################################################################### | |
1560 | ||
1561 | Your mileage will vary depending your CPUs relative fp to integer | |
1562 | performance ratio. | |
1563 | */ | |
1564 | ||
1565 | #ifndef NV_PRESERVES_UV | |
1566 | #define IS_NUMBER_UNDERFLOW_IV 1 | |
1567 | #define IS_NUMBER_UNDERFLOW_UV 2 | |
1568 | #define IS_NUMBER_IV_AND_UV 2 | |
1569 | #define IS_NUMBER_OVERFLOW_IV 4 | |
1570 | #define IS_NUMBER_OVERFLOW_UV 5 | |
1571 | /* Hopefully your optimiser will consider inlining these two functions. */ | |
1572 | STATIC int | |
1573 | S_sv_2inuv_non_preserve (pTHX_ register SV *sv, I32 numtype) { | |
1574 | NV nv = SvNVX(sv); /* Code simpler and had compiler problems if */ | |
1575 | UV nv_as_uv = U_V(nv); /* these are not in simple variables. */ | |
159fae86 | 1576 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_2inuv_non '%s', IV=0x%"UVxf" NV=%g inttype=%"UVXf"\n", SvPVX(sv), SvIVX(sv), nv, (UV)numtype)); |
28e5dec8 JH |
1577 | if (nv_as_uv <= (UV)IV_MAX) { |
1578 | (void)SvIOKp_on(sv); | |
1579 | (void)SvNOKp_on(sv); | |
1580 | /* Within suitable range to fit in an IV, atol won't overflow */ | |
1581 | /* XXX quite sure? Is that your final answer? not really, I'm | |
1582 | trusting that nv_as_uv to round down if NV is (IV_MAX + 1) */ | |
1583 | SvIVX(sv) = (IV)Atol(SvPVX(sv)); | |
1584 | if (numtype & IS_NUMBER_NOT_INT) { | |
1585 | /* I believe that even if the original PV had decimals, they | |
1586 | are lost beyond the limit of the FP precision. | |
1587 | However, neither is canonical, so both only get p flags. | |
1588 | NWC, 2000/11/25 */ | |
1589 | /* Both already have p flags, so do nothing */ | |
1590 | } else if (SvIVX(sv) == I_V(nv)) { | |
1591 | SvNOK_on(sv); | |
1592 | SvIOK_on(sv); | |
1593 | } else { | |
1594 | SvIOK_on(sv); | |
1595 | /* It had no "." so it must be integer. assert (get in here from | |
1596 | sv_2iv and sv_2uv only for ndef HAS_STRTOL and | |
1597 | IS_NUMBER_AS_LONG_AS_IV_MAX) or my logic is faulty and all | |
1598 | conversion routines need audit. */ | |
1599 | } | |
1600 | return nv < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV; | |
1601 | } | |
1602 | /* between IV_MAX and NV(UV_MAX). Could be slightly> UV_MAX */ | |
1603 | (void)SvIOKp_on(sv); | |
1604 | (void)SvNOKp_on(sv); | |
1605 | #ifdef HAS_STRTOUL | |
1606 | { | |
1607 | int save_errno = errno; | |
1608 | errno = 0; | |
1609 | SvUVX(sv) = Strtoul(SvPVX(sv), Null(char**), 10); | |
1610 | if (errno == 0) { | |
1611 | if (numtype & IS_NUMBER_NOT_INT) { | |
1612 | /* UV and NV both imprecise. */ | |
1613 | SvIsUV_on(sv); | |
1614 | } else if (SvUVX(sv) == nv_as_uv && SvUVX(sv) != UV_MAX) { | |
1615 | SvNOK_on(sv); | |
1616 | SvIOK_on(sv); | |
1617 | SvIsUV_on(sv); | |
1618 | } else { | |
1619 | SvIOK_on(sv); | |
1620 | SvIsUV_on(sv); | |
1621 | } | |
1622 | errno = save_errno; | |
1623 | return IS_NUMBER_OVERFLOW_IV; | |
1624 | } | |
1625 | errno = save_errno; | |
1626 | SvNOK_on(sv); | |
1627 | /* Must have just overflowed UV, but not enough that an NV could spot | |
1628 | this.. */ | |
1629 | return IS_NUMBER_OVERFLOW_UV; | |
1630 | } | |
1631 | #else | |
1632 | /* We've just lost integer precision, nothing we could do. */ | |
1633 | SvUVX(sv) = nv_as_uv; | |
159fae86 | 1634 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_2niuv_non UV? '%s', UV=0x%"UVxf" NV=%g U_V(NV)=0x%"UVxf" inttype=%"UVXf"\n", SvPVX(sv), SvIVX(sv), nv, nv_as_uv, (UV)numtype)); |
28e5dec8 JH |
1635 | /* UV and NV slots equally valid only if we have casting symmetry. */ |
1636 | if (numtype & IS_NUMBER_NOT_INT) { | |
1637 | SvIsUV_on(sv); | |
1638 | } else if (SvUVX(sv) == nv_as_uv && SvUVX(sv) != UV_MAX) { | |
1639 | /* UV_MAX can cast up to NV (UV_MAX+1), that NV casts down to UV_MAX | |
1640 | UV_MAX ought to be 0xFF...FFF which won't preserve (We only | |
1641 | get to this point if NVs don't preserve UVs) */ | |
1642 | SvNOK_on(sv); | |
1643 | SvIOK_on(sv); | |
1644 | SvIsUV_on(sv); | |
1645 | } else { | |
1646 | /* As above, I believe UV at least as good as NV */ | |
1647 | SvIsUV_on(sv); | |
1648 | } | |
1649 | #endif /* HAS_STRTOUL */ | |
1650 | return IS_NUMBER_OVERFLOW_IV; | |
1651 | } | |
1652 | ||
1653 | /* For sv_2nv these three cases are "SvNOK and don't bother casting" */ | |
1654 | STATIC int | |
1655 | S_sv_2iuv_non_preserve (pTHX_ register SV *sv, I32 numtype) | |
1656 | { | |
159fae86 | 1657 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_2iuv_non '%s', IV=0x%"UVxf" NV=%g inttype=%"UVXf"\n", SvPVX(sv), SvIVX(sv), SvNVX(sv), (UV)numtype)); |
28e5dec8 JH |
1658 | if (SvNVX(sv) < (NV)IV_MIN) { |
1659 | (void)SvIOKp_on(sv); | |
1660 | (void)SvNOK_on(sv); | |
1661 | SvIVX(sv) = IV_MIN; | |
1662 | return IS_NUMBER_UNDERFLOW_IV; | |
1663 | } | |
1664 | if (SvNVX(sv) > (NV)UV_MAX) { | |
1665 | (void)SvIOKp_on(sv); | |
1666 | (void)SvNOK_on(sv); | |
1667 | SvIsUV_on(sv); | |
1668 | SvUVX(sv) = UV_MAX; | |
1669 | return IS_NUMBER_OVERFLOW_UV; | |
1670 | } | |
1671 | if (!(numtype & (IS_NUMBER_TO_INT_BY_ATOL | IS_NUMBER_TO_INT_BY_STRTOL))) { | |
1672 | (void)SvIOKp_on(sv); | |
1673 | (void)SvNOK_on(sv); | |
1674 | /* Can't use strtol etc to convert this string */ | |
1675 | if (SvNVX(sv) <= (UV)IV_MAX) { | |
1676 | SvIVX(sv) = I_V(SvNVX(sv)); | |
1677 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) { | |
1678 | SvIOK_on(sv); /* Integer is precise. NOK, IOK */ | |
1679 | } else { | |
1680 | /* Integer is imprecise. NOK, IOKp */ | |
1681 | } | |
1682 | return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV; | |
1683 | } | |
1684 | SvIsUV_on(sv); | |
1685 | SvUVX(sv) = U_V(SvNVX(sv)); | |
1686 | if ((NV)(SvUVX(sv)) == SvNVX(sv)) { | |
09bb3e27 NC |
1687 | if (SvUVX(sv) == UV_MAX) { |
1688 | /* As we know that NVs don't preserve UVs, UV_MAX cannot | |
1689 | possibly be preserved by NV. Hence, it must be overflow. | |
1690 | NOK, IOKp */ | |
1691 | return IS_NUMBER_OVERFLOW_UV; | |
1692 | } | |
28e5dec8 JH |
1693 | SvIOK_on(sv); /* Integer is precise. NOK, UOK */ |
1694 | } else { | |
1695 | /* Integer is imprecise. NOK, IOKp */ | |
1696 | } | |
1697 | return IS_NUMBER_OVERFLOW_IV; | |
1698 | } | |
e57fe1aa | 1699 | return S_sv_2inuv_non_preserve(aTHX_ sv, numtype); |
28e5dec8 JH |
1700 | } |
1701 | #endif /* NV_PRESERVES_UV*/ | |
1702 | ||
a0d0e21e | 1703 | IV |
864dbfa3 | 1704 | Perl_sv_2iv(pTHX_ register SV *sv) |
79072805 LW |
1705 | { |
1706 | if (!sv) | |
1707 | return 0; | |
8990e307 | 1708 | if (SvGMAGICAL(sv)) { |
463ee0b2 LW |
1709 | mg_get(sv); |
1710 | if (SvIOKp(sv)) | |
1711 | return SvIVX(sv); | |
748a9306 | 1712 | if (SvNOKp(sv)) { |
25da4f38 | 1713 | return I_V(SvNVX(sv)); |
748a9306 | 1714 | } |
36477c24 | 1715 | if (SvPOKp(sv) && SvLEN(sv)) |
1716 | return asIV(sv); | |
3fe9a6f1 | 1717 | if (!SvROK(sv)) { |
d008e5eb | 1718 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 1719 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
1d7c1841 | 1720 | report_uninit(); |
c6ee37c5 | 1721 | } |
36477c24 | 1722 | return 0; |
3fe9a6f1 | 1723 | } |
463ee0b2 | 1724 | } |
ed6116ce | 1725 | if (SvTHINKFIRST(sv)) { |
a0d0e21e | 1726 | if (SvROK(sv)) { |
a0d0e21e | 1727 | SV* tmpstr; |
1554e226 DC |
1728 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) && |
1729 | (SvRV(tmpstr) != SvRV(sv))) | |
9e7bc3e8 | 1730 | return SvIV(tmpstr); |
56431972 | 1731 | return PTR2IV(SvRV(sv)); |
a0d0e21e | 1732 | } |
47deb5e7 NIS |
1733 | if (SvREADONLY(sv) && SvFAKE(sv)) { |
1734 | sv_force_normal(sv); | |
1735 | } | |
0336b60e | 1736 | if (SvREADONLY(sv) && !SvOK(sv)) { |
0336b60e | 1737 | if (ckWARN(WARN_UNINITIALIZED)) |
1d7c1841 | 1738 | report_uninit(); |
ed6116ce LW |
1739 | return 0; |
1740 | } | |
79072805 | 1741 | } |
25da4f38 IZ |
1742 | if (SvIOKp(sv)) { |
1743 | if (SvIsUV(sv)) { | |
1744 | return (IV)(SvUVX(sv)); | |
1745 | } | |
1746 | else { | |
1747 | return SvIVX(sv); | |
1748 | } | |
463ee0b2 | 1749 | } |
748a9306 | 1750 | if (SvNOKp(sv)) { |
28e5dec8 JH |
1751 | /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv |
1752 | * without also getting a cached IV/UV from it at the same time | |
1753 | * (ie PV->NV conversion should detect loss of accuracy and cache | |
1754 | * IV or UV at same time to avoid this. NWC */ | |
25da4f38 IZ |
1755 | |
1756 | if (SvTYPE(sv) == SVt_NV) | |
1757 | sv_upgrade(sv, SVt_PVNV); | |
1758 | ||
28e5dec8 JH |
1759 | (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */ |
1760 | /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost | |
1761 | certainly cast into the IV range at IV_MAX, whereas the correct | |
1762 | answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary | |
1763 | cases go to UV */ | |
1764 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
748a9306 | 1765 | SvIVX(sv) = I_V(SvNVX(sv)); |
28e5dec8 JH |
1766 | if (SvNVX(sv) == (NV) SvIVX(sv) |
1767 | #ifndef NV_PRESERVES_UV | |
1768 | && (((UV)1 << NV_PRESERVES_UV_BITS) > | |
1769 | (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv))) | |
1770 | /* Don't flag it as "accurately an integer" if the number | |
1771 | came from a (by definition imprecise) NV operation, and | |
1772 | we're outside the range of NV integer precision */ | |
1773 | #endif | |
1774 | ) { | |
1775 | SvIOK_on(sv); /* Can this go wrong with rounding? NWC */ | |
1776 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
1777 | "0x%"UVxf" iv(%g => %"IVdf") (precise)\n", | |
1778 | PTR2UV(sv), | |
1779 | SvNVX(sv), | |
1780 | SvIVX(sv))); | |
1781 | ||
1782 | } else { | |
1783 | /* IV not precise. No need to convert from PV, as NV | |
1784 | conversion would already have cached IV if it detected | |
1785 | that PV->IV would be better than PV->NV->IV | |
1786 | flags already correct - don't set public IOK. */ | |
1787 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
1788 | "0x%"UVxf" iv(%g => %"IVdf") (imprecise)\n", | |
1789 | PTR2UV(sv), | |
1790 | SvNVX(sv), | |
1791 | SvIVX(sv))); | |
1792 | } | |
1793 | /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN, | |
1794 | but the cast (NV)IV_MIN rounds to a the value less (more | |
1795 | negative) than IV_MIN which happens to be equal to SvNVX ?? | |
1796 | Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and | |
1797 | NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and | |
1798 | (NV)UVX == NVX are both true, but the values differ. :-( | |
1799 | Hopefully for 2s complement IV_MIN is something like | |
1800 | 0x8000000000000000 which will be exact. NWC */ | |
d460ef45 | 1801 | } |
25da4f38 | 1802 | else { |
ff68c719 | 1803 | SvUVX(sv) = U_V(SvNVX(sv)); |
28e5dec8 JH |
1804 | if ( |
1805 | (SvNVX(sv) == (NV) SvUVX(sv)) | |
1806 | #ifndef NV_PRESERVES_UV | |
1807 | /* Make sure it's not 0xFFFFFFFFFFFFFFFF */ | |
1808 | /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */ | |
1809 | && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv)) | |
1810 | /* Don't flag it as "accurately an integer" if the number | |
1811 | came from a (by definition imprecise) NV operation, and | |
1812 | we're outside the range of NV integer precision */ | |
1813 | #endif | |
1814 | ) | |
1815 | SvIOK_on(sv); | |
25da4f38 IZ |
1816 | SvIsUV_on(sv); |
1817 | ret_iv_max: | |
1c846c1f | 1818 | DEBUG_c(PerlIO_printf(Perl_debug_log, |
57def98f | 1819 | "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n", |
56431972 | 1820 | PTR2UV(sv), |
57def98f JH |
1821 | SvUVX(sv), |
1822 | SvUVX(sv))); | |
25da4f38 IZ |
1823 | return (IV)SvUVX(sv); |
1824 | } | |
748a9306 LW |
1825 | } |
1826 | else if (SvPOKp(sv) && SvLEN(sv)) { | |
25da4f38 IZ |
1827 | I32 numtype = looks_like_number(sv); |
1828 | ||
1829 | /* We want to avoid a possible problem when we cache an IV which | |
1830 | may be later translated to an NV, and the resulting NV is not | |
1831 | the translation of the initial data. | |
1c846c1f | 1832 | |
25da4f38 IZ |
1833 | This means that if we cache such an IV, we need to cache the |
1834 | NV as well. Moreover, we trade speed for space, and do not | |
28e5dec8 | 1835 | cache the NV if we are sure it's not needed. |
25da4f38 | 1836 | */ |
16b7a9a4 | 1837 | |
28e5dec8 JH |
1838 | if ((numtype & ~IS_NUMBER_NEG) == IS_NUMBER_TO_INT_BY_ATOL) { |
1839 | /* The NV may be reconstructed from IV - safe to cache IV, | |
1840 | which may be calculated by atol(). */ | |
1841 | if (SvTYPE(sv) < SVt_PVIV) | |
1842 | sv_upgrade(sv, SVt_PVIV); | |
f7bbb42a | 1843 | (void)SvIOK_on(sv); |
28e5dec8 JH |
1844 | SvIVX(sv) = Atol(SvPVX(sv)); |
1845 | } else { | |
1846 | #ifdef HAS_STRTOL | |
1847 | IV i; | |
1848 | int save_errno = errno; | |
1849 | /* Is it an integer that we could convert with strtol? | |
1850 | So try it, and if it doesn't set errno then it's pukka. | |
1851 | This should be faster than going atof and then thinking. */ | |
1852 | if (((numtype & (IS_NUMBER_TO_INT_BY_STRTOL | IS_NUMBER_NOT_INT)) | |
1853 | == IS_NUMBER_TO_INT_BY_STRTOL) | |
1854 | /* && is a sequence point. Without it not sure if I'm trying | |
1855 | to do too much between sequence points and hence going | |
1856 | undefined */ | |
1857 | && ((errno = 0), 1) /* , 1 so always true */ | |
1858 | && ((i = Strtol(SvPVX(sv), Null(char**), 10)), 1) | |
1859 | && (errno == 0)) { | |
1860 | if (SvTYPE(sv) < SVt_PVIV) | |
1861 | sv_upgrade(sv, SVt_PVIV); | |
1862 | (void)SvIOK_on(sv); | |
1863 | SvIVX(sv) = i; | |
1864 | errno = save_errno; | |
1865 | } else | |
1866 | #endif | |
1867 | { | |
1868 | NV d; | |
1869 | #ifdef HAS_STRTOL | |
1870 | /* Hopefully trace flow will optimise this away where possible | |
1871 | */ | |
1872 | errno = save_errno; | |
1873 | #endif | |
1874 | /* It wasn't an integer, or it overflowed, or we don't have | |
1875 | strtol. Do things the slow way - check if it's a UV etc. */ | |
1876 | d = Atof(SvPVX(sv)); | |
1877 | ||
1878 | if (SvTYPE(sv) < SVt_PVNV) | |
1879 | sv_upgrade(sv, SVt_PVNV); | |
1880 | SvNVX(sv) = d; | |
1881 | ||
1882 | if (! numtype && ckWARN(WARN_NUMERIC)) | |
1883 | not_a_number(sv); | |
1884 | ||
65202027 | 1885 | #if defined(USE_LONG_DOUBLE) |
28e5dec8 JH |
1886 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n", |
1887 | PTR2UV(sv), SvNVX(sv))); | |
65202027 | 1888 | #else |
28e5dec8 JH |
1889 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%g)\n", |
1890 | PTR2UV(sv), SvNVX(sv))); | |
65202027 | 1891 | #endif |
28e5dec8 JH |
1892 | |
1893 | ||
1894 | #ifdef NV_PRESERVES_UV | |
1895 | (void)SvIOKp_on(sv); | |
1896 | (void)SvNOK_on(sv); | |
1897 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
1898 | SvIVX(sv) = I_V(SvNVX(sv)); | |
1899 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) { | |
1900 | SvIOK_on(sv); | |
1901 | } else { | |
1902 | /* Integer is imprecise. NOK, IOKp */ | |
1903 | } | |
1904 | /* UV will not work better than IV */ | |
1905 | } else { | |
1906 | if (SvNVX(sv) > (NV)UV_MAX) { | |
1907 | SvIsUV_on(sv); | |
1908 | /* Integer is inaccurate. NOK, IOKp, is UV */ | |
1909 | SvUVX(sv) = UV_MAX; | |
1910 | SvIsUV_on(sv); | |
1911 | } else { | |
1912 | SvUVX(sv) = U_V(SvNVX(sv)); | |
1913 | /* 0xFFFFFFFFFFFFFFFF not an issue in here */ | |
1914 | if ((NV)(SvUVX(sv)) == SvNVX(sv)) { | |
1915 | SvIOK_on(sv); | |
1916 | SvIsUV_on(sv); | |
1917 | } else { | |
1918 | /* Integer is imprecise. NOK, IOKp, is UV */ | |
1919 | SvIsUV_on(sv); | |
1920 | } | |
1921 | } | |
1922 | goto ret_iv_max; | |
1923 | } | |
1924 | #else /* NV_PRESERVES_UV */ | |
1925 | if (((UV)1 << NV_PRESERVES_UV_BITS) > | |
1926 | U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) { | |
1927 | /* Small enough to preserve all bits. */ | |
1928 | (void)SvIOKp_on(sv); | |
1929 | SvNOK_on(sv); | |
1930 | SvIVX(sv) = I_V(SvNVX(sv)); | |
1931 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) | |
1932 | SvIOK_on(sv); | |
1933 | /* Assumption: first non-preserved integer is < IV_MAX, | |
1934 | this NV is in the preserved range, therefore: */ | |
1935 | if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv)) | |
1936 | < (UV)IV_MAX)) { | |
1937 | 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); | |
1938 | } | |
1939 | } else if (sv_2iuv_non_preserve (sv, numtype) | |
1940 | >= IS_NUMBER_OVERFLOW_IV) | |
1941 | goto ret_iv_max; | |
1942 | #endif /* NV_PRESERVES_UV */ | |
25da4f38 IZ |
1943 | } |
1944 | } | |
28e5dec8 | 1945 | } else { |
599cee73 | 1946 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP)) |
1d7c1841 | 1947 | report_uninit(); |
25da4f38 IZ |
1948 | if (SvTYPE(sv) < SVt_IV) |
1949 | /* Typically the caller expects that sv_any is not NULL now. */ | |
1950 | sv_upgrade(sv, SVt_IV); | |
a0d0e21e | 1951 | return 0; |
79072805 | 1952 | } |
1d7c1841 GS |
1953 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n", |
1954 | PTR2UV(sv),SvIVX(sv))); | |
25da4f38 | 1955 | return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv); |
79072805 LW |
1956 | } |
1957 | ||
ff68c719 | 1958 | UV |
864dbfa3 | 1959 | Perl_sv_2uv(pTHX_ register SV *sv) |
ff68c719 | 1960 | { |
1961 | if (!sv) | |
1962 | return 0; | |
1963 | if (SvGMAGICAL(sv)) { | |
1964 | mg_get(sv); | |
1965 | if (SvIOKp(sv)) | |
1966 | return SvUVX(sv); | |
1967 | if (SvNOKp(sv)) | |
1968 | return U_V(SvNVX(sv)); | |
36477c24 | 1969 | if (SvPOKp(sv) && SvLEN(sv)) |
1970 | return asUV(sv); | |
3fe9a6f1 | 1971 | if (!SvROK(sv)) { |
d008e5eb | 1972 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 1973 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
1d7c1841 | 1974 | report_uninit(); |
c6ee37c5 | 1975 | } |
36477c24 | 1976 | return 0; |
3fe9a6f1 | 1977 | } |
ff68c719 | 1978 | } |
1979 | if (SvTHINKFIRST(sv)) { | |
1980 | if (SvROK(sv)) { | |
ff68c719 | 1981 | SV* tmpstr; |
1554e226 DC |
1982 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) && |
1983 | (SvRV(tmpstr) != SvRV(sv))) | |
9e7bc3e8 | 1984 | return SvUV(tmpstr); |
56431972 | 1985 | return PTR2UV(SvRV(sv)); |
ff68c719 | 1986 | } |
8a818333 NIS |
1987 | if (SvREADONLY(sv) && SvFAKE(sv)) { |
1988 | sv_force_normal(sv); | |
1989 | } | |
0336b60e | 1990 | if (SvREADONLY(sv) && !SvOK(sv)) { |
0336b60e | 1991 | if (ckWARN(WARN_UNINITIALIZED)) |
1d7c1841 | 1992 | report_uninit(); |
ff68c719 | 1993 | return 0; |
1994 | } | |
1995 | } | |
25da4f38 IZ |
1996 | if (SvIOKp(sv)) { |
1997 | if (SvIsUV(sv)) { | |
1998 | return SvUVX(sv); | |
1999 | } | |
2000 | else { | |
2001 | return (UV)SvIVX(sv); | |
2002 | } | |
ff68c719 | 2003 | } |
2004 | if (SvNOKp(sv)) { | |
28e5dec8 JH |
2005 | /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv |
2006 | * without also getting a cached IV/UV from it at the same time | |
2007 | * (ie PV->NV conversion should detect loss of accuracy and cache | |
2008 | * IV or UV at same time to avoid this. */ | |
2009 | /* IV-over-UV optimisation - choose to cache IV if possible */ | |
2010 | ||
25da4f38 IZ |
2011 | if (SvTYPE(sv) == SVt_NV) |
2012 | sv_upgrade(sv, SVt_PVNV); | |
28e5dec8 JH |
2013 | |
2014 | (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */ | |
2015 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
f7bbb42a | 2016 | SvIVX(sv) = I_V(SvNVX(sv)); |
28e5dec8 JH |
2017 | if (SvNVX(sv) == (NV) SvIVX(sv) |
2018 | #ifndef NV_PRESERVES_UV | |
2019 | && (((UV)1 << NV_PRESERVES_UV_BITS) > | |
2020 | (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv))) | |
2021 | /* Don't flag it as "accurately an integer" if the number | |
2022 | came from a (by definition imprecise) NV operation, and | |
2023 | we're outside the range of NV integer precision */ | |
2024 | #endif | |
2025 | ) { | |
2026 | SvIOK_on(sv); /* Can this go wrong with rounding? NWC */ | |
2027 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
2028 | "0x%"UVxf" uv(%g => %"IVdf") (precise)\n", | |
2029 | PTR2UV(sv), | |
2030 | SvNVX(sv), | |
2031 | SvIVX(sv))); | |
2032 | ||
2033 | } else { | |
2034 | /* IV not precise. No need to convert from PV, as NV | |
2035 | conversion would already have cached IV if it detected | |
2036 | that PV->IV would be better than PV->NV->IV | |
2037 | flags already correct - don't set public IOK. */ | |
2038 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
2039 | "0x%"UVxf" uv(%g => %"IVdf") (imprecise)\n", | |
2040 | PTR2UV(sv), | |
2041 | SvNVX(sv), | |
2042 | SvIVX(sv))); | |
2043 | } | |
2044 | /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN, | |
2045 | but the cast (NV)IV_MIN rounds to a the value less (more | |
2046 | negative) than IV_MIN which happens to be equal to SvNVX ?? | |
2047 | Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and | |
2048 | NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and | |
2049 | (NV)UVX == NVX are both true, but the values differ. :-( | |
2050 | Hopefully for 2s complement IV_MIN is something like | |
2051 | 0x8000000000000000 which will be exact. NWC */ | |
d460ef45 | 2052 | } |
28e5dec8 JH |
2053 | else { |
2054 | SvUVX(sv) = U_V(SvNVX(sv)); | |
2055 | if ( | |
2056 | (SvNVX(sv) == (NV) SvUVX(sv)) | |
2057 | #ifndef NV_PRESERVES_UV | |
2058 | /* Make sure it's not 0xFFFFFFFFFFFFFFFF */ | |
2059 | /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */ | |
2060 | && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv)) | |
2061 | /* Don't flag it as "accurately an integer" if the number | |
2062 | came from a (by definition imprecise) NV operation, and | |
2063 | we're outside the range of NV integer precision */ | |
2064 | #endif | |
2065 | ) | |
2066 | SvIOK_on(sv); | |
2067 | SvIsUV_on(sv); | |
1c846c1f | 2068 | DEBUG_c(PerlIO_printf(Perl_debug_log, |
28e5dec8 | 2069 | "0x%"UVxf" 2uv(%"UVuf" => %"IVdf") (as unsigned)\n", |
57def98f | 2070 | PTR2UV(sv), |
28e5dec8 JH |
2071 | SvUVX(sv), |
2072 | SvUVX(sv))); | |
25da4f38 | 2073 | } |
ff68c719 | 2074 | } |
2075 | else if (SvPOKp(sv) && SvLEN(sv)) { | |
25da4f38 IZ |
2076 | I32 numtype = looks_like_number(sv); |
2077 | ||
2078 | /* We want to avoid a possible problem when we cache a UV which | |
2079 | may be later translated to an NV, and the resulting NV is not | |
2080 | the translation of the initial data. | |
1c846c1f | 2081 | |
25da4f38 IZ |
2082 | This means that if we cache such a UV, we need to cache the |
2083 | NV as well. Moreover, we trade speed for space, and do not | |
2084 | cache the NV if not needed. | |
2085 | */ | |
16b7a9a4 | 2086 | |
28e5dec8 | 2087 | if ((numtype & ~IS_NUMBER_NEG) == IS_NUMBER_TO_INT_BY_ATOL) { |
f7bbb42a | 2088 | /* The NV may be reconstructed from IV - safe to cache IV, |
28e5dec8 JH |
2089 | which may be calculated by atol(). */ |
2090 | if (SvTYPE(sv) < SVt_PVIV) | |
f7bbb42a JH |
2091 | sv_upgrade(sv, SVt_PVIV); |
2092 | (void)SvIOK_on(sv); | |
28e5dec8 JH |
2093 | SvIVX(sv) = Atol(SvPVX(sv)); |
2094 | } else { | |
f7bbb42a | 2095 | #ifdef HAS_STRTOUL |
28e5dec8 | 2096 | UV u; |
f9172815 | 2097 | char *num_begin = SvPVX(sv); |
28e5dec8 | 2098 | int save_errno = errno; |
d460ef45 | 2099 | |
f9172815 JH |
2100 | /* seems that strtoul taking numbers that start with - is |
2101 | implementation dependant, and can't be relied upon. */ | |
2102 | if (numtype & IS_NUMBER_NEG) { | |
2103 | /* Not totally defensive. assumine that looks_like_num | |
2104 | didn't lie about a - sign */ | |
2105 | while (isSPACE(*num_begin)) | |
2106 | num_begin++; | |
2107 | if (*num_begin == '-') | |
2108 | num_begin++; | |
2109 | } | |
d460ef45 | 2110 | |
28e5dec8 JH |
2111 | /* Is it an integer that we could convert with strtoul? |
2112 | So try it, and if it doesn't set errno then it's pukka. | |
2113 | This should be faster than going atof and then thinking. */ | |
2114 | if (((numtype & (IS_NUMBER_TO_INT_BY_STRTOL | IS_NUMBER_NOT_INT)) | |
2115 | == IS_NUMBER_TO_INT_BY_STRTOL) | |
2116 | && ((errno = 0), 1) /* always true */ | |
f9172815 | 2117 | && ((u = Strtoul(num_begin, Null(char**), 10)), 1) /* ditto */ |
28e5dec8 | 2118 | && (errno == 0) |
d460ef45 | 2119 | /* If known to be negative, check it didn't undeflow IV |
f9172815 JH |
2120 | XXX possibly we should put more negative values as NVs |
2121 | direct rather than go via atof below */ | |
2122 | && ((numtype & IS_NUMBER_NEG) ? (u <= (UV)IV_MIN) : 1)) { | |
28e5dec8 JH |
2123 | errno = save_errno; |
2124 | ||
2125 | if (SvTYPE(sv) < SVt_PVIV) | |
2126 | sv_upgrade(sv, SVt_PVIV); | |
2127 | (void)SvIOK_on(sv); | |
2128 | ||
2129 | /* If it's negative must use IV. | |
2130 | IV-over-UV optimisation */ | |
f9172815 JH |
2131 | if (numtype & IS_NUMBER_NEG) { |
2132 | SvIVX(sv) = -(IV)u; | |
2133 | } else if (u <= (UV) IV_MAX) { | |
28e5dec8 JH |
2134 | SvIVX(sv) = (IV)u; |
2135 | } else { | |
2136 | /* it didn't overflow, and it was positive. */ | |
2137 | SvUVX(sv) = u; | |
2138 | SvIsUV_on(sv); | |
2139 | } | |
2140 | } else | |
f7bbb42a | 2141 | #endif |
28e5dec8 JH |
2142 | { |
2143 | NV d; | |
2144 | #ifdef HAS_STRTOUL | |
2145 | /* Hopefully trace flow will optimise this away where possible | |
2146 | */ | |
2147 | errno = save_errno; | |
2148 | #endif | |
2149 | /* It wasn't an integer, or it overflowed, or we don't have | |
2150 | strtol. Do things the slow way - check if it's a IV etc. */ | |
2151 | d = Atof(SvPVX(sv)); | |
2152 | ||
2153 | if (SvTYPE(sv) < SVt_PVNV) | |
2154 | sv_upgrade(sv, SVt_PVNV); | |
2155 | SvNVX(sv) = d; | |
2156 | ||
2157 | if (! numtype && ckWARN(WARN_NUMERIC)) | |
2158 | not_a_number(sv); | |
2159 | ||
2160 | #if defined(USE_LONG_DOUBLE) | |
2161 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%" PERL_PRIgldbl ")\n", | |
2162 | PTR2UV(sv), SvNVX(sv))); | |
2163 | #else | |
2164 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%g)\n", | |
2165 | PTR2UV(sv), SvNVX(sv))); | |
2166 | #endif | |
2167 | ||
2168 | #ifdef NV_PRESERVES_UV | |
2169 | (void)SvIOKp_on(sv); | |
2170 | (void)SvNOK_on(sv); | |
2171 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
2172 | SvIVX(sv) = I_V(SvNVX(sv)); | |
2173 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) { | |
2174 | SvIOK_on(sv); | |
2175 | } else { | |
2176 | /* Integer is imprecise. NOK, IOKp */ | |
2177 | } | |
2178 | /* UV will not work better than IV */ | |
2179 | } else { | |
2180 | if (SvNVX(sv) > (NV)UV_MAX) { | |
2181 | SvIsUV_on(sv); | |
2182 | /* Integer is inaccurate. NOK, IOKp, is UV */ | |
2183 | SvUVX(sv) = UV_MAX; | |
2184 | SvIsUV_on(sv); | |
2185 | } else { | |
2186 | SvUVX(sv) = U_V(SvNVX(sv)); | |
2187 | /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs | |
2188 | NV preservse UV so can do correct comparison. */ | |
2189 | if ((NV)(SvUVX(sv)) == SvNVX(sv)) { | |
2190 | SvIOK_on(sv); | |
2191 | SvIsUV_on(sv); | |
2192 | } else { | |
2193 | /* Integer is imprecise. NOK, IOKp, is UV */ | |
2194 | SvIsUV_on(sv); | |
2195 | } | |
2196 | } | |
2197 | } | |
2198 | #else /* NV_PRESERVES_UV */ | |
2199 | if (((UV)1 << NV_PRESERVES_UV_BITS) > | |
2200 | U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) { | |
2201 | /* Small enough to preserve all bits. */ | |
2202 | (void)SvIOKp_on(sv); | |
2203 | SvNOK_on(sv); | |
2204 | SvIVX(sv) = I_V(SvNVX(sv)); | |
2205 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) | |
2206 | SvIOK_on(sv); | |
2207 | /* Assumption: first non-preserved integer is < IV_MAX, | |
2208 | this NV is in the preserved range, therefore: */ | |
2209 | if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv)) | |
2210 | < (UV)IV_MAX)) { | |
2211 | 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); | |
2212 | } | |
2213 | } else | |
2214 | sv_2iuv_non_preserve (sv, numtype); | |
2215 | #endif /* NV_PRESERVES_UV */ | |
2216 | } | |
f7bbb42a | 2217 | } |
ff68c719 | 2218 | } |
2219 | else { | |
d008e5eb | 2220 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 2221 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
1d7c1841 | 2222 | report_uninit(); |
c6ee37c5 | 2223 | } |
25da4f38 IZ |
2224 | if (SvTYPE(sv) < SVt_IV) |
2225 | /* Typically the caller expects that sv_any is not NULL now. */ | |
2226 | sv_upgrade(sv, SVt_IV); | |
ff68c719 | 2227 | return 0; |
2228 | } | |
25da4f38 | 2229 | |
1d7c1841 GS |
2230 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n", |
2231 | PTR2UV(sv),SvUVX(sv))); | |
25da4f38 | 2232 | return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv); |
ff68c719 | 2233 | } |
2234 | ||
65202027 | 2235 | NV |
864dbfa3 | 2236 | Perl_sv_2nv(pTHX_ register SV *sv) |
79072805 LW |
2237 | { |
2238 | if (!sv) | |
2239 | return 0.0; | |
8990e307 | 2240 | if (SvGMAGICAL(sv)) { |
463ee0b2 LW |
2241 | mg_get(sv); |
2242 | if (SvNOKp(sv)) | |
2243 | return SvNVX(sv); | |
a0d0e21e | 2244 | if (SvPOKp(sv) && SvLEN(sv)) { |
599cee73 | 2245 | if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !looks_like_number(sv)) |
a0d0e21e | 2246 | not_a_number(sv); |
097ee67d | 2247 | return Atof(SvPVX(sv)); |
a0d0e21e | 2248 | } |
25da4f38 | 2249 | if (SvIOKp(sv)) { |
1c846c1f | 2250 | if (SvIsUV(sv)) |
65202027 | 2251 | return (NV)SvUVX(sv); |
25da4f38 | 2252 | else |
65202027 | 2253 | return (NV)SvIVX(sv); |
25da4f38 | 2254 | } |
16d20bd9 | 2255 | if (!SvROK(sv)) { |
d008e5eb | 2256 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 2257 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
1d7c1841 | 2258 | report_uninit(); |
c6ee37c5 | 2259 | } |
16d20bd9 AD |
2260 | return 0; |
2261 | } | |
463ee0b2 | 2262 | } |
ed6116ce | 2263 | if (SvTHINKFIRST(sv)) { |
a0d0e21e | 2264 | if (SvROK(sv)) { |
a0d0e21e | 2265 | SV* tmpstr; |
1554e226 DC |
2266 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) && |
2267 | (SvRV(tmpstr) != SvRV(sv))) | |
9e7bc3e8 | 2268 | return SvNV(tmpstr); |
56431972 | 2269 | return PTR2NV(SvRV(sv)); |
a0d0e21e | 2270 | } |
8a818333 NIS |
2271 | if (SvREADONLY(sv) && SvFAKE(sv)) { |
2272 | sv_force_normal(sv); | |
2273 | } | |
0336b60e | 2274 | if (SvREADONLY(sv) && !SvOK(sv)) { |
599cee73 | 2275 | if (ckWARN(WARN_UNINITIALIZED)) |
1d7c1841 | 2276 | report_uninit(); |
ed6116ce LW |
2277 | return 0.0; |
2278 | } | |
79072805 LW |
2279 | } |
2280 | if (SvTYPE(sv) < SVt_NV) { | |
463ee0b2 LW |
2281 | if (SvTYPE(sv) == SVt_IV) |
2282 | sv_upgrade(sv, SVt_PVNV); | |
2283 | else | |
2284 | sv_upgrade(sv, SVt_NV); | |
572bbb43 | 2285 | #if defined(USE_LONG_DOUBLE) |
097ee67d | 2286 | DEBUG_c({ |
f93f4e46 | 2287 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
2288 | PerlIO_printf(Perl_debug_log, |
2289 | "0x%"UVxf" num(%" PERL_PRIgldbl ")\n", | |
2290 | PTR2UV(sv), SvNVX(sv)); | |
572bbb43 GS |
2291 | RESTORE_NUMERIC_LOCAL(); |
2292 | }); | |
65202027 | 2293 | #else |
572bbb43 | 2294 | DEBUG_c({ |
f93f4e46 | 2295 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
2296 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%g)\n", |
2297 | PTR2UV(sv), SvNVX(sv)); | |
097ee67d JH |
2298 | RESTORE_NUMERIC_LOCAL(); |
2299 | }); | |
572bbb43 | 2300 | #endif |
79072805 LW |
2301 | } |
2302 | else if (SvTYPE(sv) < SVt_PVNV) | |
2303 | sv_upgrade(sv, SVt_PVNV); | |
748a9306 LW |
2304 | if (SvIOKp(sv) && |
2305 | (!SvPOKp(sv) || !strchr(SvPVX(sv),'.') || !looks_like_number(sv))) | |
93a17b20 | 2306 | { |
65202027 | 2307 | SvNVX(sv) = SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv); |
28e5dec8 JH |
2308 | #ifdef NV_PRESERVES_UV |
2309 | SvNOK_on(sv); | |
2310 | #else | |
2311 | /* Only set the public NV OK flag if this NV preserves the IV */ | |
2312 | /* Check it's not 0xFFFFFFFFFFFFFFFF */ | |
2313 | if (SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv)))) | |
2314 | : (SvIVX(sv) == I_V(SvNVX(sv)))) | |
2315 | SvNOK_on(sv); | |
2316 | else | |
2317 | SvNOKp_on(sv); | |
2318 | #endif | |
93a17b20 | 2319 | } |
748a9306 | 2320 | else if (SvPOKp(sv) && SvLEN(sv)) { |
599cee73 | 2321 | if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !looks_like_number(sv)) |
a0d0e21e | 2322 | not_a_number(sv); |
097ee67d | 2323 | SvNVX(sv) = Atof(SvPVX(sv)); |
28e5dec8 JH |
2324 | #ifdef NV_PRESERVES_UV |
2325 | SvNOK_on(sv); | |
2326 | #else | |
2327 | /* Only set the public NV OK flag if this NV preserves the value in | |
2328 | the PV at least as well as an IV/UV would. | |
2329 | Not sure how to do this 100% reliably. */ | |
2330 | /* if that shift count is out of range then Configure's test is | |
2331 | wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS == | |
2332 | UV_BITS */ | |
2333 | if (((UV)1 << NV_PRESERVES_UV_BITS) > | |
2334 | U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) | |
2335 | SvNOK_on(sv); /* Definitely small enough to preserve all bits */ | |
2336 | else if (SvNVX(sv) < (NV)IV_MIN || SvNVX(sv) > (NV)UV_MAX) { | |
2337 | /* Definitely too large/small to fit in an integer, so no loss | |
2338 | of precision going to integer in the future via NV */ | |
2339 | SvNOK_on(sv); | |
2340 | } else { | |
2341 | /* Is it something we can run through strtol etc (ie no | |
2342 | trailing exponent part)? */ | |
2343 | int numtype = looks_like_number(sv); | |
2344 | /* XXX probably should cache this if called above */ | |
2345 | ||
2346 | if (!(numtype & | |
2347 | (IS_NUMBER_TO_INT_BY_ATOL | IS_NUMBER_TO_INT_BY_STRTOL))) { | |
2348 | /* Can't use strtol etc to convert this string, so don't try */ | |
2349 | SvNOK_on(sv); | |
2350 | } else | |
2351 | sv_2inuv_non_preserve (sv, numtype); | |
2352 | } | |
2353 | #endif /* NV_PRESERVES_UV */ | |
93a17b20 | 2354 | } |
79072805 | 2355 | else { |
599cee73 | 2356 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP)) |
1d7c1841 | 2357 | report_uninit(); |
25da4f38 IZ |
2358 | if (SvTYPE(sv) < SVt_NV) |
2359 | /* Typically the caller expects that sv_any is not NULL now. */ | |
28e5dec8 JH |
2360 | /* XXX Ilya implies that this is a bug in callers that assume this |
2361 | and ideally should be fixed. */ | |
25da4f38 | 2362 | sv_upgrade(sv, SVt_NV); |
a0d0e21e | 2363 | return 0.0; |
79072805 | 2364 | } |
572bbb43 | 2365 | #if defined(USE_LONG_DOUBLE) |
097ee67d | 2366 | DEBUG_c({ |
f93f4e46 | 2367 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
2368 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n", |
2369 | PTR2UV(sv), SvNVX(sv)); | |
572bbb43 GS |
2370 | RESTORE_NUMERIC_LOCAL(); |
2371 | }); | |
65202027 | 2372 | #else |
572bbb43 | 2373 | DEBUG_c({ |
f93f4e46 | 2374 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
2375 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%g)\n", |
2376 | PTR2UV(sv), SvNVX(sv)); | |
097ee67d JH |
2377 | RESTORE_NUMERIC_LOCAL(); |
2378 | }); | |
572bbb43 | 2379 | #endif |
463ee0b2 | 2380 | return SvNVX(sv); |
79072805 LW |
2381 | } |
2382 | ||
76e3520e | 2383 | STATIC IV |
cea2e8a9 | 2384 | S_asIV(pTHX_ SV *sv) |
36477c24 | 2385 | { |
2386 | I32 numtype = looks_like_number(sv); | |
65202027 | 2387 | NV d; |
36477c24 | 2388 | |
25da4f38 | 2389 | if (numtype & IS_NUMBER_TO_INT_BY_ATOL) |
cf2093f6 | 2390 | return Atol(SvPVX(sv)); |
d008e5eb | 2391 | if (!numtype) { |
d008e5eb GS |
2392 | if (ckWARN(WARN_NUMERIC)) |
2393 | not_a_number(sv); | |
2394 | } | |
097ee67d | 2395 | d = Atof(SvPVX(sv)); |
25da4f38 | 2396 | return I_V(d); |
36477c24 | 2397 | } |
2398 | ||
76e3520e | 2399 | STATIC UV |
cea2e8a9 | 2400 | S_asUV(pTHX_ SV *sv) |
36477c24 | 2401 | { |
2402 | I32 numtype = looks_like_number(sv); | |
2403 | ||
84902520 | 2404 | #ifdef HAS_STRTOUL |
25da4f38 | 2405 | if (numtype & IS_NUMBER_TO_INT_BY_ATOL) |
cf2093f6 | 2406 | return Strtoul(SvPVX(sv), Null(char**), 10); |
84902520 | 2407 | #endif |
d008e5eb | 2408 | if (!numtype) { |
d008e5eb GS |
2409 | if (ckWARN(WARN_NUMERIC)) |
2410 | not_a_number(sv); | |
2411 | } | |
097ee67d | 2412 | return U_V(Atof(SvPVX(sv))); |
36477c24 | 2413 | } |
2414 | ||
25da4f38 IZ |
2415 | /* |
2416 | * Returns a combination of (advisory only - can get false negatives) | |
28e5dec8 JH |
2417 | * IS_NUMBER_TO_INT_BY_ATOL, IS_NUMBER_TO_INT_BY_ATOF |
2418 | * IS_NUMBER_LONGER_THAN_IV_MAX, IS_NUMBER_AS_LONG_AS_IV_MAX | |
2419 | * IS_NUMBER_NOT_INT, IS_NUMBER_NEG, IS_NUMBER_INFINITY | |
25da4f38 IZ |
2420 | * 0 if does not look like number. |
2421 | * | |
28e5dec8 JH |
2422 | * (atol and strtol stop when they hit a decimal point. strtol will return |
2423 | * LONG_MAX and LONG_MIN when given out of range values. ANSI says they should | |
2424 | * do this, and vendors have had 11 years to get it right. | |
2425 | * However, will try to make it still work with only atol | |
d460ef45 | 2426 | * |
28e5dec8 JH |
2427 | * IS_NUMBER_TO_INT_BY_ATOL 123456789 or 123456789.3 definitely < IV_MAX |
2428 | * IS_NUMBER_TO_INT_BY_STRTOL 123456789 or 123456789.3 if digits = IV_MAX | |
2429 | * IS_NUMBER_TO_INT_BY_ATOF 123456789e0 or >> IV_MAX | |
2430 | * IS_NUMBER_LONGER_THAN_IV_MAX lots of digits, don't bother with atol | |
2431 | * IS_NUMBER_AS_LONG_AS_IV_MAX atol might hit LONG_MAX, might not. | |
2432 | * IS_NUMBER_NOT_INT saw "." or "e" | |
2433 | * IS_NUMBER_NEG | |
300aed98 | 2434 | * IS_NUMBER_INFINITY |
25da4f38 IZ |
2435 | */ |
2436 | ||
954c1994 GS |
2437 | /* |
2438 | =for apidoc looks_like_number | |
2439 | ||
2440 | Test if an the content of an SV looks like a number (or is a | |
28e5dec8 JH |
2441 | number). C<Inf> and C<Infinity> are treated as numbers (so will not |
2442 | issue a non-numeric warning), even if your atof() doesn't grok them. | |
954c1994 GS |
2443 | |
2444 | =cut | |
2445 | */ | |
2446 | ||
36477c24 | 2447 | I32 |
864dbfa3 | 2448 | Perl_looks_like_number(pTHX_ SV *sv) |
36477c24 | 2449 | { |
2450 | register char *s; | |
2451 | register char *send; | |
2452 | register char *sbegin; | |
25da4f38 IZ |
2453 | register char *nbegin; |
2454 | I32 numtype = 0; | |
300aed98 | 2455 | I32 sawinf = 0; |
36477c24 | 2456 | STRLEN len; |
9c7192ba | 2457 | #ifdef USE_LOCALE_NUMERIC |
eff180cd | 2458 | bool specialradix = FALSE; |
9c7192ba | 2459 | #endif |
36477c24 | 2460 | |
2461 | if (SvPOK(sv)) { | |
1c846c1f | 2462 | sbegin = SvPVX(sv); |
36477c24 | 2463 | len = SvCUR(sv); |
2464 | } | |
2465 | else if (SvPOKp(sv)) | |
2466 | sbegin = SvPV(sv, len); | |
2467 | else | |
2468 | return 1; | |
2469 | send = sbegin + len; | |
2470 | ||
2471 | s = sbegin; | |
2472 | while (isSPACE(*s)) | |
2473 | s++; | |
25da4f38 IZ |
2474 | if (*s == '-') { |
2475 | s++; | |
2476 | numtype = IS_NUMBER_NEG; | |
2477 | } | |
2478 | else if (*s == '+') | |
36477c24 | 2479 | s++; |
ff0cee69 | 2480 | |
25da4f38 IZ |
2481 | nbegin = s; |
2482 | /* | |
d460ef45 | 2483 | * we return IS_NUMBER_TO_INT_BY_ATOL if the number can converted to |
28e5dec8 JH |
2484 | * integer with atol() without overflow, IS_NUMBER_TO_INT_BY_STRTOL if |
2485 | * possibly slightly larger than max int, IS_NUMBER_TO_INT_BY_ATOF if you | |
2486 | * will need (int)atof(). | |
25da4f38 IZ |
2487 | */ |
2488 | ||
300aed98 | 2489 | /* next must be digit or the radix separator or beginning of infinity */ |
ff0cee69 | 2490 | if (isDIGIT(*s)) { |
2491 | do { | |
2492 | s++; | |
2493 | } while (isDIGIT(*s)); | |
25da4f38 | 2494 | |
28e5dec8 JH |
2495 | /* Aaargh. long long really is irritating. |
2496 | In the gospel according to ANSI 1989, it is an axiom that "long" | |
2497 | is the longest integer type, and that if you don't know how long | |
2498 | something is you can cast it to long, and nothing will be lost | |
2499 | (except possibly speed of execution if long is slower than the | |
2500 | type is was). | |
2501 | Now, one can't be sure if the old rules apply, or long long | |
2502 | (or some other newfangled thing) is actually longer than the | |
2503 | (formerly) longest thing. | |
2504 | */ | |
2505 | /* This lot will work for 64 bit *as long as* either | |
2506 | either long is 64 bit | |
2507 | or we can find both strtol/strtoq and strtoul/strtouq | |
2508 | If not, we really should refuse to let the user use 64 bit IVs | |
2509 | By "64 bit" I really mean IVs that don't get preserved by NVs | |
2510 | It also should work for 128 bit IVs. Can any lend me a machine to | |
2511 | test this? | |
2512 | */ | |
2513 | if (s - nbegin > TYPE_DIGITS(UV)) /* Cannot cache ato[ul]() */ | |
2514 | numtype |= IS_NUMBER_TO_INT_BY_ATOF | IS_NUMBER_LONGER_THAN_IV_MAX; | |
2515 | else if (s - nbegin < BIT_DIGITS(((sizeof (IV)>sizeof (long)) | |
2516 | ? sizeof(long) : sizeof (IV))*8-1)) | |
f7bbb42a | 2517 | numtype |= IS_NUMBER_TO_INT_BY_ATOL; |
28e5dec8 JH |
2518 | else |
2519 | /* Can't be sure either way. (For 64 bit UV, 63 bit IV is 1 decimal | |
2520 | digit less (IV_MAX= 9223372036854775807, | |
2521 | UV_MAX= 18446744073709551615) so be cautious */ | |
2522 | numtype |= IS_NUMBER_TO_INT_BY_STRTOL | IS_NUMBER_AS_LONG_AS_IV_MAX; | |
25da4f38 | 2523 | |
097ee67d | 2524 | if (*s == '.' |
1c846c1f | 2525 | #ifdef USE_LOCALE_NUMERIC |
eff180cd | 2526 | || (specialradix = IS_NUMERIC_RADIX(s)) |
097ee67d JH |
2527 | #endif |
2528 | ) { | |
9c7192ba | 2529 | #ifdef USE_LOCALE_NUMERIC |
eff180cd JH |
2530 | if (specialradix) |
2531 | s += SvCUR(PL_numeric_radix); | |
2532 | else | |
9c7192ba | 2533 | #endif |
eff180cd | 2534 | s++; |
28e5dec8 | 2535 | numtype |= IS_NUMBER_NOT_INT; |
097ee67d | 2536 | while (isDIGIT(*s)) /* optional digits after the radix */ |
ff0cee69 | 2537 | s++; |
2538 | } | |
36477c24 | 2539 | } |
097ee67d | 2540 | else if (*s == '.' |
1c846c1f | 2541 | #ifdef USE_LOCALE_NUMERIC |
eff180cd | 2542 | || (specialradix = IS_NUMERIC_RADIX(s)) |
097ee67d JH |
2543 | #endif |
2544 | ) { | |
9c7192ba | 2545 | #ifdef USE_LOCALE_NUMERIC |
eff180cd JH |
2546 | if (specialradix) |
2547 | s += SvCUR(PL_numeric_radix); | |
2548 | else | |
9c7192ba | 2549 | #endif |
eff180cd | 2550 | s++; |
28e5dec8 | 2551 | numtype |= IS_NUMBER_TO_INT_BY_ATOL | IS_NUMBER_NOT_INT; |
097ee67d | 2552 | /* no digits before the radix means we need digits after it */ |
ff0cee69 | 2553 | if (isDIGIT(*s)) { |
2554 | do { | |
2555 | s++; | |
2556 | } while (isDIGIT(*s)); | |
2557 | } | |
2558 | else | |
2559 | return 0; | |
2560 | } | |
300aed98 JH |
2561 | else if (*s == 'I' || *s == 'i') { |
2562 | s++; if (*s != 'N' && *s != 'n') return 0; | |
2563 | s++; if (*s != 'F' && *s != 'f') return 0; | |
2564 | s++; if (*s == 'I' || *s == 'i') { | |
2565 | s++; if (*s != 'N' && *s != 'n') return 0; | |
2566 | s++; if (*s != 'I' && *s != 'i') return 0; | |
2567 | s++; if (*s != 'T' && *s != 't') return 0; | |
2568 | s++; if (*s != 'Y' && *s != 'y') return 0; | |
99938567 | 2569 | s++; |
300aed98 JH |
2570 | } |
2571 | sawinf = 1; | |
2572 | } | |
ff0cee69 | 2573 | else |
2574 | return 0; | |
2575 | ||
300aed98 | 2576 | if (sawinf) |
28e5dec8 JH |
2577 | numtype = (numtype & IS_NUMBER_NEG) /* Keep track of sign */ |
2578 | | IS_NUMBER_INFINITY | IS_NUMBER_NOT_INT; | |
300aed98 JH |
2579 | else { |
2580 | /* we can have an optional exponent part */ | |
2581 | if (*s == 'e' || *s == 'E') { | |
28e5dec8 JH |
2582 | numtype &= IS_NUMBER_NEG; |
2583 | numtype |= IS_NUMBER_TO_INT_BY_ATOF | IS_NUMBER_NOT_INT; | |
36477c24 | 2584 | s++; |
300aed98 JH |
2585 | if (*s == '+' || *s == '-') |
2586 | s++; | |
2587 | if (isDIGIT(*s)) { | |
2588 | do { | |
2589 | s++; | |
2590 | } while (isDIGIT(*s)); | |
2591 | } | |
2592 | else | |
2593 | return 0; | |
2594 | } | |
36477c24 | 2595 | } |
2596 | while (isSPACE(*s)) | |
2597 | s++; | |
80f3f388 | 2598 | if (s >= send) |
36477c24 | 2599 | return numtype; |
2600 | if (len == 10 && memEQ(sbegin, "0 but true", 10)) | |
25da4f38 | 2601 | return IS_NUMBER_TO_INT_BY_ATOL; |
36477c24 | 2602 | return 0; |
2603 | } | |
2604 | ||
79072805 | 2605 | char * |
864dbfa3 | 2606 | Perl_sv_2pv_nolen(pTHX_ register SV *sv) |
1fa8b10d JD |
2607 | { |
2608 | STRLEN n_a; | |
2609 | return sv_2pv(sv, &n_a); | |
2610 | } | |
2611 | ||
25da4f38 | 2612 | /* We assume that buf is at least TYPE_CHARS(UV) long. */ |
864dbfa3 | 2613 | static char * |
25da4f38 IZ |
2614 | uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob) |
2615 | { | |
25da4f38 IZ |
2616 | char *ptr = buf + TYPE_CHARS(UV); |
2617 | char *ebuf = ptr; | |
2618 | int sign; | |
25da4f38 IZ |
2619 | |
2620 | if (is_uv) | |
2621 | sign = 0; | |
2622 | else if (iv >= 0) { | |
2623 | uv = iv; | |
2624 | sign = 0; | |
2625 | } else { | |
2626 | uv = -iv; | |
2627 | sign = 1; | |
2628 | } | |
2629 | do { | |
2630 | *--ptr = '0' + (uv % 10); | |
2631 | } while (uv /= 10); | |
2632 | if (sign) | |
2633 | *--ptr = '-'; | |
2634 | *peob = ebuf; | |
2635 | return ptr; | |
2636 | } | |
2637 | ||
1fa8b10d | 2638 | char * |
864dbfa3 | 2639 | Perl_sv_2pv(pTHX_ register SV *sv, STRLEN *lp) |
79072805 LW |
2640 | { |
2641 | register char *s; | |
2642 | int olderrno; | |
46fc3d4c | 2643 | SV *tsv; |
25da4f38 IZ |
2644 | char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */ |
2645 | char *tmpbuf = tbuf; | |
79072805 | 2646 | |
463ee0b2 LW |
2647 | if (!sv) { |
2648 | *lp = 0; | |
2649 | return ""; | |
2650 | } | |
8990e307 | 2651 | if (SvGMAGICAL(sv)) { |
463ee0b2 LW |
2652 | mg_get(sv); |
2653 | if (SvPOKp(sv)) { | |
2654 | *lp = SvCUR(sv); | |
2655 | return SvPVX(sv); | |
2656 | } | |
cf2093f6 | 2657 | if (SvIOKp(sv)) { |
1c846c1f | 2658 | if (SvIsUV(sv)) |
57def98f | 2659 | (void)sprintf(tmpbuf,"%"UVuf, (UV)SvUVX(sv)); |
cf2093f6 | 2660 | else |
57def98f | 2661 | (void)sprintf(tmpbuf,"%"IVdf, (IV)SvIVX(sv)); |
46fc3d4c | 2662 | tsv = Nullsv; |
a0d0e21e | 2663 | goto tokensave; |
463ee0b2 LW |
2664 | } |
2665 | if (SvNOKp(sv)) { | |
2d4389e4 | 2666 | Gconvert(SvNVX(sv), NV_DIG, 0, tmpbuf); |
46fc3d4c | 2667 | tsv = Nullsv; |
a0d0e21e | 2668 | goto tokensave; |
463ee0b2 | 2669 | } |
16d20bd9 | 2670 | if (!SvROK(sv)) { |
d008e5eb | 2671 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 2672 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
1d7c1841 | 2673 | report_uninit(); |
c6ee37c5 | 2674 | } |
16d20bd9 AD |
2675 | *lp = 0; |
2676 | return ""; | |
2677 | } | |
463ee0b2 | 2678 | } |
ed6116ce LW |
2679 | if (SvTHINKFIRST(sv)) { |
2680 | if (SvROK(sv)) { | |
a0d0e21e | 2681 | SV* tmpstr; |
1554e226 DC |
2682 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)) && |
2683 | (SvRV(tmpstr) != SvRV(sv))) | |
9e7bc3e8 | 2684 | return SvPV(tmpstr,*lp); |
ed6116ce LW |
2685 | sv = (SV*)SvRV(sv); |
2686 | if (!sv) | |
2687 | s = "NULLREF"; | |
2688 | else { | |
f9277f47 IZ |
2689 | MAGIC *mg; |
2690 | ||
ed6116ce | 2691 | switch (SvTYPE(sv)) { |
f9277f47 IZ |
2692 | case SVt_PVMG: |
2693 | if ( ((SvFLAGS(sv) & | |
1c846c1f | 2694 | (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG)) |
3149a8e4 | 2695 | == (SVs_OBJECT|SVs_RMG)) |
57668c4d | 2696 | && strEQ(s=HvNAME(SvSTASH(sv)), "Regexp") |
f9277f47 | 2697 | && (mg = mg_find(sv, 'r'))) { |
2cd61cdb | 2698 | regexp *re = (regexp *)mg->mg_obj; |
1bd3ad17 | 2699 | |
2cd61cdb | 2700 | if (!mg->mg_ptr) { |
8782bef2 GB |
2701 | char *fptr = "msix"; |
2702 | char reflags[6]; | |
2703 | char ch; | |
2704 | int left = 0; | |
2705 | int right = 4; | |
2706 | U16 reganch = (re->reganch & PMf_COMPILETIME) >> 12; | |
2707 | ||
155aba94 | 2708 | while((ch = *fptr++)) { |
8782bef2 GB |
2709 | if(reganch & 1) { |
2710 | reflags[left++] = ch; | |
2711 | } | |
2712 | else { | |
2713 | reflags[right--] = ch; | |
2714 | } | |
2715 | reganch >>= 1; | |
2716 | } | |
2717 | if(left != 4) { | |
2718 | reflags[left] = '-'; | |
2719 | left = 5; | |
2720 | } | |
2721 | ||
2722 | mg->mg_len = re->prelen + 4 + left; | |
2723 | New(616, mg->mg_ptr, mg->mg_len + 1 + left, char); | |
2724 | Copy("(?", mg->mg_ptr, 2, char); | |
2725 | Copy(reflags, mg->mg_ptr+2, left, char); | |
2726 | Copy(":", mg->mg_ptr+left+2, 1, char); | |
2727 | Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char); | |
1bd3ad17 IZ |
2728 | mg->mg_ptr[mg->mg_len - 1] = ')'; |
2729 | mg->mg_ptr[mg->mg_len] = 0; | |
2730 | } | |
3280af22 | 2731 | PL_reginterp_cnt += re->program[0].next_off; |
1bd3ad17 IZ |
2732 | *lp = mg->mg_len; |
2733 | return mg->mg_ptr; | |
f9277f47 IZ |
2734 | } |
2735 | /* Fall through */ | |
ed6116ce LW |
2736 | case SVt_NULL: |
2737 | case SVt_IV: | |
2738 | case SVt_NV: | |
2739 | case SVt_RV: | |
2740 | case SVt_PV: | |
2741 | case SVt_PVIV: | |
2742 | case SVt_PVNV: | |
81689caa HS |
2743 | case SVt_PVBM: if (SvROK(sv)) |
2744 | s = "REF"; | |
2745 | else | |
2746 | s = "SCALAR"; break; | |
ed6116ce LW |
2747 | case SVt_PVLV: s = "LVALUE"; break; |
2748 | case SVt_PVAV: s = "ARRAY"; break; | |
2749 | case SVt_PVHV: s = "HASH"; break; | |
2750 | case SVt_PVCV: s = "CODE"; break; | |
2751 | case SVt_PVGV: s = "GLOB"; break; | |
1d2dff63 | 2752 | case SVt_PVFM: s = "FORMAT"; break; |
36477c24 | 2753 | case SVt_PVIO: s = "IO"; break; |
ed6116ce LW |
2754 | default: s = "UNKNOWN"; break; |
2755 | } | |
46fc3d4c | 2756 | tsv = NEWSV(0,0); |
ed6116ce | 2757 | if (SvOBJECT(sv)) |
cea2e8a9 | 2758 | Perl_sv_setpvf(aTHX_ tsv, "%s=%s", HvNAME(SvSTASH(sv)), s); |
ed6116ce | 2759 | else |
46fc3d4c | 2760 | sv_setpv(tsv, s); |
57def98f | 2761 | Perl_sv_catpvf(aTHX_ tsv, "(0x%"UVxf")", PTR2UV(sv)); |
a0d0e21e | 2762 | goto tokensaveref; |
463ee0b2 | 2763 | } |
ed6116ce LW |
2764 | *lp = strlen(s); |
2765 | return s; | |
79072805 | 2766 | } |
0336b60e | 2767 | if (SvREADONLY(sv) && !SvOK(sv)) { |
0336b60e | 2768 | if (ckWARN(WARN_UNINITIALIZED)) |
1d7c1841 | 2769 | report_uninit(); |
ed6116ce LW |
2770 | *lp = 0; |
2771 | return ""; | |
79072805 | 2772 | } |
79072805 | 2773 | } |
28e5dec8 JH |
2774 | if (SvIOK(sv) || ((SvIOKp(sv) && !SvNOKp(sv)))) { |
2775 | /* I'm assuming that if both IV and NV are equally valid then | |
2776 | converting the IV is going to be more efficient */ | |
2777 | U32 isIOK = SvIOK(sv); | |
2778 | U32 isUIOK = SvIsUV(sv); | |
2779 | char buf[TYPE_CHARS(UV)]; | |
2780 | char *ebuf, *ptr; | |
2781 | ||
2782 | if (SvTYPE(sv) < SVt_PVIV) | |
2783 | sv_upgrade(sv, SVt_PVIV); | |
2784 | if (isUIOK) | |
2785 | ptr = uiv_2buf(buf, 0, SvUVX(sv), 1, &ebuf); | |
2786 | else | |
2787 | ptr = uiv_2buf(buf, SvIVX(sv), 0, 0, &ebuf); | |
2788 | SvGROW(sv, ebuf - ptr + 1); /* inlined from sv_setpvn */ | |
2789 | Move(ptr,SvPVX(sv),ebuf - ptr,char); | |
2790 | SvCUR_set(sv, ebuf - ptr); | |
2791 | s = SvEND(sv); | |
2792 | *s = '\0'; | |
2793 | if (isIOK) | |
2794 | SvIOK_on(sv); | |
2795 | else | |
2796 | SvIOKp_on(sv); | |
2797 | if (isUIOK) | |
2798 | SvIsUV_on(sv); | |
2799 | } | |
2800 | else if (SvNOKp(sv)) { | |
79072805 LW |
2801 | if (SvTYPE(sv) < SVt_PVNV) |
2802 | sv_upgrade(sv, SVt_PVNV); | |
1c846c1f | 2803 | /* The +20 is pure guesswork. Configure test needed. --jhi */ |
59155cc0 | 2804 | SvGROW(sv, NV_DIG + 20); |
463ee0b2 | 2805 | s = SvPVX(sv); |
79072805 | 2806 | olderrno = errno; /* some Xenix systems wipe out errno here */ |
79072805 | 2807 | #ifdef apollo |
463ee0b2 | 2808 | if (SvNVX(sv) == 0.0) |
79072805 LW |
2809 | (void)strcpy(s,"0"); |
2810 | else | |
2811 | #endif /*apollo*/ | |
bbce6d69 | 2812 | { |
2d4389e4 | 2813 | Gconvert(SvNVX(sv), NV_DIG, 0, s); |
bbce6d69 | 2814 | } |
79072805 | 2815 | errno = olderrno; |
a0d0e21e LW |
2816 | #ifdef FIXNEGATIVEZERO |
2817 | if (*s == '-' && s[1] == '0' && !s[2]) | |
2818 | strcpy(s,"0"); | |
2819 | #endif | |
79072805 LW |
2820 | while (*s) s++; |
2821 | #ifdef hcx | |
2822 | if (s[-1] == '.') | |
46fc3d4c | 2823 | *--s = '\0'; |
79072805 LW |
2824 | #endif |
2825 | } | |
79072805 | 2826 | else { |
0336b60e IZ |
2827 | if (ckWARN(WARN_UNINITIALIZED) |
2828 | && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP)) | |
1d7c1841 | 2829 | report_uninit(); |
a0d0e21e | 2830 | *lp = 0; |
25da4f38 IZ |
2831 | if (SvTYPE(sv) < SVt_PV) |
2832 | /* Typically the caller expects that sv_any is not NULL now. */ | |
2833 | sv_upgrade(sv, SVt_PV); | |
a0d0e21e | 2834 | return ""; |
79072805 | 2835 | } |
463ee0b2 LW |
2836 | *lp = s - SvPVX(sv); |
2837 | SvCUR_set(sv, *lp); | |
79072805 | 2838 | SvPOK_on(sv); |
1d7c1841 GS |
2839 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n", |
2840 | PTR2UV(sv),SvPVX(sv))); | |
463ee0b2 | 2841 | return SvPVX(sv); |
a0d0e21e LW |
2842 | |
2843 | tokensave: | |
2844 | if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */ | |
2845 | /* Sneaky stuff here */ | |
2846 | ||
2847 | tokensaveref: | |
46fc3d4c | 2848 | if (!tsv) |
96827780 | 2849 | tsv = newSVpv(tmpbuf, 0); |
46fc3d4c | 2850 | sv_2mortal(tsv); |
2851 | *lp = SvCUR(tsv); | |
2852 | return SvPVX(tsv); | |
a0d0e21e LW |
2853 | } |
2854 | else { | |
2855 | STRLEN len; | |
46fc3d4c | 2856 | char *t; |
2857 | ||
2858 | if (tsv) { | |
2859 | sv_2mortal(tsv); | |
2860 | t = SvPVX(tsv); | |
2861 | len = SvCUR(tsv); | |
2862 | } | |
2863 | else { | |
96827780 MB |
2864 | t = tmpbuf; |
2865 | len = strlen(tmpbuf); | |
46fc3d4c | 2866 | } |
a0d0e21e | 2867 | #ifdef FIXNEGATIVEZERO |
46fc3d4c | 2868 | if (len == 2 && t[0] == '-' && t[1] == '0') { |
2869 | t = "0"; | |
2870 | len = 1; | |
2871 | } | |
a0d0e21e LW |
2872 | #endif |
2873 | (void)SvUPGRADE(sv, SVt_PV); | |
46fc3d4c | 2874 | *lp = len; |
a0d0e21e LW |
2875 | s = SvGROW(sv, len + 1); |
2876 | SvCUR_set(sv, len); | |
46fc3d4c | 2877 | (void)strcpy(s, t); |
6bf554b4 | 2878 | SvPOKp_on(sv); |
a0d0e21e LW |
2879 | return s; |
2880 | } | |
463ee0b2 LW |
2881 | } |
2882 | ||
7340a771 GS |
2883 | char * |
2884 | Perl_sv_2pvbyte_nolen(pTHX_ register SV *sv) | |
2885 | { | |
560a288e GS |
2886 | STRLEN n_a; |
2887 | return sv_2pvbyte(sv, &n_a); | |
7340a771 GS |
2888 | } |
2889 | ||
2890 | char * | |
2891 | Perl_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp) | |
2892 | { | |
0875d2fe NIS |
2893 | sv_utf8_downgrade(sv,0); |
2894 | return SvPV(sv,*lp); | |
7340a771 GS |
2895 | } |
2896 | ||
2897 | char * | |
2898 | Perl_sv_2pvutf8_nolen(pTHX_ register SV *sv) | |
2899 | { | |
560a288e GS |
2900 | STRLEN n_a; |
2901 | return sv_2pvutf8(sv, &n_a); | |
7340a771 GS |
2902 | } |
2903 | ||
2904 | char * | |
2905 | Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp) | |
2906 | { | |
560a288e | 2907 | sv_utf8_upgrade(sv); |
7d59b7e4 | 2908 | return SvPV(sv,*lp); |
7340a771 | 2909 | } |
1c846c1f | 2910 | |
463ee0b2 LW |
2911 | /* This function is only called on magical items */ |
2912 | bool | |
864dbfa3 | 2913 | Perl_sv_2bool(pTHX_ register SV *sv) |
463ee0b2 | 2914 | { |
8990e307 | 2915 | if (SvGMAGICAL(sv)) |
463ee0b2 LW |
2916 | mg_get(sv); |
2917 | ||
a0d0e21e LW |
2918 | if (!SvOK(sv)) |
2919 | return 0; | |
2920 | if (SvROK(sv)) { | |
a0d0e21e | 2921 | SV* tmpsv; |
1554e226 DC |
2922 | if (SvAMAGIC(sv) && (tmpsv=AMG_CALLun(sv,bool_)) && |
2923 | (SvRV(tmpsv) != SvRV(sv))) | |
9e7bc3e8 | 2924 | return SvTRUE(tmpsv); |
a0d0e21e LW |
2925 | return SvRV(sv) != 0; |
2926 | } | |
463ee0b2 | 2927 | if (SvPOKp(sv)) { |
11343788 MB |
2928 | register XPV* Xpvtmp; |
2929 | if ((Xpvtmp = (XPV*)SvANY(sv)) && | |
2930 | (*Xpvtmp->xpv_pv > '0' || | |
2931 | Xpvtmp->xpv_cur > 1 || | |
2932 | (Xpvtmp->xpv_cur && *Xpvtmp->xpv_pv != '0'))) | |
463ee0b2 LW |
2933 | return 1; |
2934 | else | |
2935 | return 0; | |
2936 | } | |
2937 | else { | |
2938 | if (SvIOKp(sv)) | |
2939 | return SvIVX(sv) != 0; | |
2940 | else { | |
2941 | if (SvNOKp(sv)) | |
2942 | return SvNVX(sv) != 0.0; | |
2943 | else | |
2944 | return FALSE; | |
2945 | } | |
2946 | } | |
79072805 LW |
2947 | } |
2948 | ||
c461cf8f JH |
2949 | /* |
2950 | =for apidoc sv_utf8_upgrade | |
2951 | ||
2952 | Convert the PV of an SV to its UTF8-encoded form. | |
4411f3b6 NIS |
2953 | Forces the SV to string form it it is not already. |
2954 | Always sets the SvUTF8 flag to avoid future validity checks even | |
2955 | if all the bytes have hibit clear. | |
c461cf8f JH |
2956 | |
2957 | =cut | |
2958 | */ | |
2959 | ||
4411f3b6 | 2960 | STRLEN |
560a288e GS |
2961 | Perl_sv_utf8_upgrade(pTHX_ register SV *sv) |
2962 | { | |
db42d148 | 2963 | U8 *s, *t, *e; |
511c2ff0 | 2964 | int hibit = 0; |
560a288e | 2965 | |
4411f3b6 NIS |
2966 | if (!sv) |
2967 | return 0; | |
2968 | ||
2969 | if (!SvPOK(sv)) | |
2970 | (void) SvPV_nolen(sv); | |
2971 | ||
2972 | if (SvUTF8(sv)) | |
2973 | return SvCUR(sv); | |
560a288e | 2974 | |
db42d148 NIS |
2975 | if (SvREADONLY(sv) && SvFAKE(sv)) { |
2976 | sv_force_normal(sv); | |
2977 | } | |
2978 | ||
40826f67 JH |
2979 | /* This function could be much more efficient if we had a FLAG in SVs |
2980 | * to signal if there are any hibit chars in the PV. | |
511c2ff0 | 2981 | * Given that there isn't make loop fast as possible |
560a288e | 2982 | */ |
db42d148 NIS |
2983 | s = (U8 *) SvPVX(sv); |
2984 | e = (U8 *) SvEND(sv); | |
511c2ff0 NIS |
2985 | t = s; |
2986 | while (t < e) { | |
c4d5f83a NIS |
2987 | U8 ch = *t++; |
2988 | if ((hibit = !NATIVE_IS_INVARIANT(ch))) | |
8a818333 | 2989 | break; |
8a818333 | 2990 | } |
40826f67 | 2991 | if (hibit) { |
8a818333 | 2992 | STRLEN len; |
652088fc | 2993 | |
8a818333 | 2994 | len = SvCUR(sv) + 1; /* Plus the \0 */ |
00df9076 | 2995 | SvPVX(sv) = (char*)bytes_to_utf8((U8*)s, &len); |
841d7a39 | 2996 | SvCUR(sv) = len - 1; |
511c2ff0 NIS |
2997 | if (SvLEN(sv) != 0) |
2998 | Safefree(s); /* No longer using what was there before. */ | |
841d7a39 | 2999 | SvLEN(sv) = len; /* No longer know the real size. */ |
560a288e | 3000 | } |
4411f3b6 NIS |
3001 | /* Mark as UTF-8 even if no hibit - saves scanning loop */ |
3002 | SvUTF8_on(sv); | |
3003 | return SvCUR(sv); | |
560a288e GS |
3004 | } |
3005 | ||
c461cf8f JH |
3006 | /* |
3007 | =for apidoc sv_utf8_downgrade | |
3008 | ||
3009 | Attempt to convert the PV of an SV from UTF8-encoded to byte encoding. | |
3010 | This may not be possible if the PV contains non-byte encoding characters; | |
3011 | if this is the case, either returns false or, if C<fail_ok> is not | |
3012 | true, croaks. | |
3013 | ||
3014 | =cut | |
3015 | */ | |
3016 | ||
560a288e GS |
3017 | bool |
3018 | Perl_sv_utf8_downgrade(pTHX_ register SV* sv, bool fail_ok) | |
3019 | { | |
3020 | if (SvPOK(sv) && SvUTF8(sv)) { | |
fa301091 | 3021 | if (SvCUR(sv)) { |
03cfe0ae | 3022 | U8 *s; |
652088fc | 3023 | STRLEN len; |
fa301091 | 3024 | |
652088fc JH |
3025 | if (SvREADONLY(sv) && SvFAKE(sv)) |
3026 | sv_force_normal(sv); | |
03cfe0ae NIS |
3027 | s = (U8 *) SvPV(sv, len); |
3028 | if (!utf8_to_bytes(s, &len)) { | |
fa301091 JH |
3029 | if (fail_ok) |
3030 | return FALSE; | |
03cfe0ae NIS |
3031 | #ifdef USE_BYTES_DOWNGRADES |
3032 | else if (IN_BYTE) { | |
3033 | U8 *d = s; | |
3034 | U8 *e = (U8 *) SvEND(sv); | |
3035 | int first = 1; | |
3036 | while (s < e) { | |
3037 | UV ch = utf8n_to_uvchr(s,(e-s),&len,0); | |
3038 | if (first && ch > 255) { | |
3039 | if (PL_op) | |
3040 | Perl_warner(aTHX_ WARN_UTF8, "Wide character in byte %s", | |
3041 | PL_op_desc[PL_op->op_type]); | |
3042 | else | |
3043 | Perl_warner(aTHX_ WARN_UTF8, "Wide character in byte"); | |
3044 | first = 0; | |
3045 | } | |
3046 | *d++ = ch; | |
3047 | s += len; | |
3048 | } | |
3049 | *d = '\0'; | |
3050 | len = (d - (U8 *) SvPVX(sv)); | |
3051 | } | |
3052 | #endif | |
fa301091 JH |
3053 | else { |
3054 | if (PL_op) | |
3055 | Perl_croak(aTHX_ "Wide character in %s", | |
3056 | PL_op_desc[PL_op->op_type]); | |
3057 | else | |
3058 | Perl_croak(aTHX_ "Wide character"); | |
3059 | } | |
4b3603a4 | 3060 | } |
fa301091 | 3061 | SvCUR(sv) = len; |
67e989fb | 3062 | } |
560a288e | 3063 | } |
ffebcc3e | 3064 | SvUTF8_off(sv); |
560a288e GS |
3065 | return TRUE; |
3066 | } | |
3067 | ||
c461cf8f JH |
3068 | /* |
3069 | =for apidoc sv_utf8_encode | |
3070 | ||
3071 | Convert the PV of an SV to UTF8-encoded, but then turn off the C<SvUTF8> | |
4411f3b6 NIS |
3072 | flag so that it looks like octets again. Used as a building block |
3073 | for encode_utf8 in Encode.xs | |
c461cf8f JH |
3074 | |
3075 | =cut | |
3076 | */ | |
3077 | ||
560a288e GS |
3078 | void |
3079 | Perl_sv_utf8_encode(pTHX_ register SV *sv) | |
3080 | { | |
4411f3b6 | 3081 | (void) sv_utf8_upgrade(sv); |
560a288e GS |
3082 | SvUTF8_off(sv); |
3083 | } | |
3084 | ||
4411f3b6 NIS |
3085 | /* |
3086 | =for apidoc sv_utf8_decode | |
3087 | ||
3088 | Convert the octets in the PV from UTF-8 to chars. Scan for validity and then | |
3089 | turn of SvUTF8 if needed so that we see characters. Used as a building block | |
3090 | for decode_utf8 in Encode.xs | |
3091 | ||
3092 | =cut | |
3093 | */ | |
3094 | ||
3095 | ||
3096 | ||
560a288e GS |
3097 | bool |
3098 | Perl_sv_utf8_decode(pTHX_ register SV *sv) | |
3099 | { | |
3100 | if (SvPOK(sv)) { | |
63cd0674 NIS |
3101 | U8 *c; |
3102 | U8 *e; | |
9cbac4c7 | 3103 | |
4411f3b6 | 3104 | /* The octets may have got themselves encoded - get them back as bytes */ |
560a288e GS |
3105 | if (!sv_utf8_downgrade(sv, TRUE)) |
3106 | return FALSE; | |
3107 | ||
3108 | /* it is actually just a matter of turning the utf8 flag on, but | |
3109 | * we want to make sure everything inside is valid utf8 first. | |
3110 | */ | |
63cd0674 NIS |
3111 | c = (U8 *) SvPVX(sv); |
3112 | if (!is_utf8_string(c, SvCUR(sv)+1)) | |
67e989fb | 3113 | return FALSE; |
63cd0674 | 3114 | e = (U8 *) SvEND(sv); |
511c2ff0 | 3115 | while (c < e) { |
c4d5f83a NIS |
3116 | U8 ch = *c++; |
3117 | if (!UTF8_IS_INVARIANT(ch)) { | |
67e989fb JH |
3118 | SvUTF8_on(sv); |
3119 | break; | |
3120 | } | |
560a288e | 3121 | } |
560a288e GS |
3122 | } |
3123 | return TRUE; | |
3124 | } | |
3125 | ||
3126 | ||
79072805 | 3127 | /* Note: sv_setsv() should not be called with a source string that needs |
463ee0b2 | 3128 | * to be reused, since it may destroy the source string if it is marked |
79072805 LW |
3129 | * as temporary. |
3130 | */ | |
3131 | ||
954c1994 GS |
3132 | /* |
3133 | =for apidoc sv_setsv | |
3134 | ||
3135 | Copies the contents of the source SV C<ssv> into the destination SV C<dsv>. | |
3136 | The source SV may be destroyed if it is mortal. Does not handle 'set' | |
3137 | magic. See the macro forms C<SvSetSV>, C<SvSetSV_nosteal> and | |
3138 | C<sv_setsv_mg>. | |
3139 | ||
3140 | =cut | |
3141 | */ | |
3142 | ||
79072805 | 3143 | void |
864dbfa3 | 3144 | Perl_sv_setsv(pTHX_ SV *dstr, register SV *sstr) |
79072805 | 3145 | { |
8990e307 LW |
3146 | register U32 sflags; |
3147 | register int dtype; | |
3148 | register int stype; | |
463ee0b2 | 3149 | |
79072805 LW |
3150 | if (sstr == dstr) |
3151 | return; | |
2213622d | 3152 | SV_CHECK_THINKFIRST(dstr); |
79072805 | 3153 | if (!sstr) |
3280af22 | 3154 | sstr = &PL_sv_undef; |
8990e307 LW |
3155 | stype = SvTYPE(sstr); |
3156 | dtype = SvTYPE(dstr); | |
79072805 | 3157 | |
a0d0e21e | 3158 | SvAMAGIC_off(dstr); |
9e7bc3e8 | 3159 | |
463ee0b2 | 3160 | /* There's a lot of redundancy below but we're going for speed here */ |
79072805 | 3161 | |
8990e307 | 3162 | switch (stype) { |
79072805 | 3163 | case SVt_NULL: |
aece5585 | 3164 | undef_sstr: |
20408e3c GS |
3165 | if (dtype != SVt_PVGV) { |
3166 | (void)SvOK_off(dstr); | |
3167 | return; | |
3168 | } | |
3169 | break; | |
463ee0b2 | 3170 | case SVt_IV: |
aece5585 GA |
3171 | if (SvIOK(sstr)) { |
3172 | switch (dtype) { | |
3173 | case SVt_NULL: | |
8990e307 | 3174 | sv_upgrade(dstr, SVt_IV); |
aece5585 GA |
3175 | break; |
3176 | case SVt_NV: | |
8990e307 | 3177 | sv_upgrade(dstr, SVt_PVNV); |
aece5585 GA |
3178 | break; |
3179 | case SVt_RV: | |
3180 | case SVt_PV: | |
a0d0e21e | 3181 | sv_upgrade(dstr, SVt_PVIV); |
aece5585 GA |
3182 | break; |
3183 | } | |
3184 | (void)SvIOK_only(dstr); | |
3185 | SvIVX(dstr) = SvIVX(sstr); | |
25da4f38 IZ |
3186 | if (SvIsUV(sstr)) |
3187 | SvIsUV_on(dstr); | |
27c9684d AP |
3188 | if (SvTAINTED(sstr)) |
3189 | SvTAINT(dstr); | |
aece5585 | 3190 | return; |
8990e307 | 3191 | } |
aece5585 GA |
3192 | goto undef_sstr; |
3193 | ||
463ee0b2 | 3194 | case SVt_NV: |
aece5585 GA |
3195 | if (SvNOK(sstr)) { |
3196 | switch (dtype) { | |
3197 | case SVt_NULL: | |
3198 | case SVt_IV: | |
8990e307 | 3199 | sv_upgrade(dstr, SVt_NV); |
aece5585 GA |
3200 | break; |
3201 | case SVt_RV: | |
3202 | case SVt_PV: | |
3203 | case SVt_PVIV: | |
a0d0e21e | 3204 | sv_upgrade(dstr, SVt_PVNV); |
aece5585 GA |
3205 | break; |
3206 | } | |
3207 | SvNVX(dstr) = SvNVX(sstr); | |
3208 | (void)SvNOK_only(dstr); | |
27c9684d AP |
3209 | if (SvTAINTED(sstr)) |
3210 | SvTAINT(dstr); | |
aece5585 | 3211 | return; |
8990e307 | 3212 | } |
aece5585 GA |
3213 | goto undef_sstr; |
3214 | ||
ed6116ce | 3215 | case SVt_RV: |
8990e307 | 3216 | if (dtype < SVt_RV) |
ed6116ce | 3217 | sv_upgrade(dstr, SVt_RV); |
c07a80fd | 3218 | else if (dtype == SVt_PVGV && |
3219 | SvTYPE(SvRV(sstr)) == SVt_PVGV) { | |
3220 | sstr = SvRV(sstr); | |
a5f75d66 | 3221 | if (sstr == dstr) { |
1d7c1841 GS |
3222 | if (GvIMPORTED(dstr) != GVf_IMPORTED |
3223 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) | |
3224 | { | |
a5f75d66 | 3225 | GvIMPORTED_on(dstr); |
1d7c1841 | 3226 | } |
a5f75d66 AD |
3227 | GvMULTI_on(dstr); |
3228 | return; | |
3229 | } | |
c07a80fd | 3230 | goto glob_assign; |
3231 | } | |
ed6116ce | 3232 | break; |
463ee0b2 | 3233 | case SVt_PV: |
fc36a67e | 3234 | case SVt_PVFM: |
8990e307 | 3235 | if (dtype < SVt_PV) |
463ee0b2 | 3236 | sv_upgrade(dstr, SVt_PV); |
463ee0b2 LW |
3237 | break; |
3238 | case SVt_PVIV: | |
8990e307 | 3239 | if (dtype < SVt_PVIV) |
463ee0b2 | 3240 | sv_upgrade(dstr, SVt_PVIV); |
463ee0b2 LW |
3241 | break; |
3242 | case SVt_PVNV: | |
8990e307 | 3243 | if (dtype < SVt_PVNV) |
463ee0b2 | 3244 | sv_upgrade(dstr, SVt_PVNV); |
463ee0b2 | 3245 | break; |
4633a7c4 LW |
3246 | case SVt_PVAV: |
3247 | case SVt_PVHV: | |
3248 | case SVt_PVCV: | |
4633a7c4 | 3249 | case SVt_PVIO: |
533c011a | 3250 | if (PL_op) |
cea2e8a9 | 3251 | Perl_croak(aTHX_ "Bizarre copy of %s in %s", sv_reftype(sstr, 0), |
22c35a8c | 3252 | PL_op_name[PL_op->op_type]); |
4633a7c4 | 3253 | else |
cea2e8a9 | 3254 | Perl_croak(aTHX_ "Bizarre copy of %s", sv_reftype(sstr, 0)); |
4633a7c4 LW |
3255 | break; |
3256 | ||
79072805 | 3257 | case SVt_PVGV: |
8990e307 | 3258 | if (dtype <= SVt_PVGV) { |
c07a80fd | 3259 | glob_assign: |
a5f75d66 | 3260 | if (dtype != SVt_PVGV) { |
a0d0e21e LW |
3261 | char *name = GvNAME(sstr); |
3262 | STRLEN len = GvNAMELEN(sstr); | |
463ee0b2 | 3263 | sv_upgrade(dstr, SVt_PVGV); |
6662521e | 3264 | sv_magic(dstr, dstr, '*', Nullch, 0); |
85aff577 | 3265 | GvSTASH(dstr) = (HV*)SvREFCNT_inc(GvSTASH(sstr)); |
a0d0e21e LW |
3266 | GvNAME(dstr) = savepvn(name, len); |
3267 | GvNAMELEN(dstr) = len; | |
3268 | SvFAKE_on(dstr); /* can coerce to non-glob */ | |
3269 | } | |
7bac28a0 | 3270 | /* ahem, death to those who redefine active sort subs */ |
3280af22 NIS |
3271 | else if (PL_curstackinfo->si_type == PERLSI_SORT |
3272 | && GvCV(dstr) && PL_sortcop == CvSTART(GvCV(dstr))) | |
cea2e8a9 | 3273 | Perl_croak(aTHX_ "Can't redefine active sort subroutine %s", |
7bac28a0 | 3274 | GvNAME(dstr)); |
5bd07a3d DM |
3275 | |
3276 | #ifdef GV_SHARED_CHECK | |
3277 | if (GvSHARED((GV*)dstr)) { | |
3278 | Perl_croak(aTHX_ PL_no_modify); | |
3279 | } | |
3280 | #endif | |
3281 | ||
a0d0e21e | 3282 | (void)SvOK_off(dstr); |
a5f75d66 | 3283 | GvINTRO_off(dstr); /* one-shot flag */ |
1edc1566 | 3284 | gp_free((GV*)dstr); |
79072805 | 3285 | GvGP(dstr) = gp_ref(GvGP(sstr)); |
27c9684d AP |
3286 | if (SvTAINTED(sstr)) |
3287 | SvTAINT(dstr); | |
1d7c1841 GS |
3288 | if (GvIMPORTED(dstr) != GVf_IMPORTED |
3289 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) | |
3290 | { | |
a5f75d66 | 3291 | GvIMPORTED_on(dstr); |
1d7c1841 | 3292 | } |
a5f75d66 | 3293 | GvMULTI_on(dstr); |
79072805 LW |
3294 | return; |
3295 | } | |
3296 | /* FALL THROUGH */ | |
3297 | ||
3298 | default: | |
973f89ab CS |
3299 | if (SvGMAGICAL(sstr)) { |
3300 | mg_get(sstr); | |
3301 | if (SvTYPE(sstr) != stype) { | |
3302 | stype = SvTYPE(sstr); | |
3303 | if (stype == SVt_PVGV && dtype <= SVt_PVGV) | |
3304 | goto glob_assign; | |
3305 | } | |
3306 | } | |
ded42b9f | 3307 | if (stype == SVt_PVLV) |
6fc92669 | 3308 | (void)SvUPGRADE(dstr, SVt_PVNV); |
ded42b9f | 3309 | else |
6fc92669 | 3310 | (void)SvUPGRADE(dstr, stype); |
79072805 LW |
3311 | } |
3312 | ||
8990e307 LW |
3313 | sflags = SvFLAGS(sstr); |
3314 | ||
3315 | if (sflags & SVf_ROK) { | |
3316 | if (dtype >= SVt_PV) { | |
3317 | if (dtype == SVt_PVGV) { | |
3318 | SV *sref = SvREFCNT_inc(SvRV(sstr)); | |
3319 | SV *dref = 0; | |
a5f75d66 | 3320 | int intro = GvINTRO(dstr); |
a0d0e21e | 3321 | |
5bd07a3d DM |
3322 | #ifdef GV_SHARED_CHECK |
3323 | if (GvSHARED((GV*)dstr)) { | |
3324 | Perl_croak(aTHX_ PL_no_modify); | |
3325 | } | |
3326 | #endif | |
3327 | ||
a0d0e21e LW |
3328 | if (intro) { |
3329 | GP *gp; | |
1d7c1841 | 3330 | gp_free((GV*)dstr); |
a5f75d66 | 3331 | GvINTRO_off(dstr); /* one-shot flag */ |
a0d0e21e | 3332 | Newz(602,gp, 1, GP); |
44a8e56a | 3333 | GvGP(dstr) = gp_ref(gp); |
a0d0e21e | 3334 | GvSV(dstr) = NEWSV(72,0); |
1d7c1841 | 3335 | GvLINE(dstr) = CopLINE(PL_curcop); |
1edc1566 | 3336 | GvEGV(dstr) = (GV*)dstr; |
a0d0e21e | 3337 | } |
a5f75d66 | 3338 | GvMULTI_on(dstr); |
8990e307 LW |
3339 | switch (SvTYPE(sref)) { |
3340 | case SVt_PVAV: | |
a0d0e21e LW |
3341 | if (intro) |
3342 | SAVESPTR(GvAV(dstr)); | |
3343 | else | |
3344 | dref = (SV*)GvAV(dstr); | |
8990e307 | 3345 | GvAV(dstr) = (AV*)sref; |
39bac7f7 | 3346 | if (!GvIMPORTED_AV(dstr) |
1d7c1841 GS |
3347 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
3348 | { | |
a5f75d66 | 3349 | GvIMPORTED_AV_on(dstr); |
1d7c1841 | 3350 | } |
8990e307 LW |
3351 | break; |
3352 | case SVt_PVHV: | |
a0d0e21e LW |
3353 | if (intro) |
3354 | SAVESPTR(GvHV(dstr)); | |
3355 | else | |
3356 | dref = (SV*)GvHV(dstr); | |
8990e307 | 3357 | GvHV(dstr) = (HV*)sref; |
39bac7f7 | 3358 | if (!GvIMPORTED_HV(dstr) |
1d7c1841 GS |
3359 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
3360 | { | |
a5f75d66 | 3361 | GvIMPORTED_HV_on(dstr); |
1d7c1841 | 3362 | } |
8990e307 LW |
3363 | break; |
3364 | case SVt_PVCV: | |
8ebc5c01 | 3365 | if (intro) { |
3366 | if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) { | |
3367 | SvREFCNT_dec(GvCV(dstr)); | |
3368 | GvCV(dstr) = Nullcv; | |
68dc0745 | 3369 | GvCVGEN(dstr) = 0; /* Switch off cacheness. */ |
3280af22 | 3370 | PL_sub_generation++; |
8ebc5c01 | 3371 | } |
a0d0e21e | 3372 | SAVESPTR(GvCV(dstr)); |
8ebc5c01 | 3373 | } |
68dc0745 | 3374 | else |
3375 | dref = (SV*)GvCV(dstr); | |
3376 | if (GvCV(dstr) != (CV*)sref) { | |
748a9306 | 3377 | CV* cv = GvCV(dstr); |
4633a7c4 | 3378 | if (cv) { |
68dc0745 | 3379 | if (!GvCVGEN((GV*)dstr) && |
3380 | (CvROOT(cv) || CvXSUB(cv))) | |
3381 | { | |
7bac28a0 | 3382 | /* ahem, death to those who redefine |
3383 | * active sort subs */ | |
3280af22 NIS |
3384 | if (PL_curstackinfo->si_type == PERLSI_SORT && |
3385 | PL_sortcop == CvSTART(cv)) | |
1c846c1f | 3386 | Perl_croak(aTHX_ |
7bac28a0 | 3387 | "Can't redefine active sort subroutine %s", |
3388 | GvENAME((GV*)dstr)); | |
beab0874 JT |
3389 | /* Redefining a sub - warning is mandatory if |
3390 | it was a const and its value changed. */ | |
3391 | if (ckWARN(WARN_REDEFINE) | |
3392 | || (CvCONST(cv) | |
3393 | && (!CvCONST((CV*)sref) | |
3394 | || sv_cmp(cv_const_sv(cv), | |
3395 | cv_const_sv((CV*)sref))))) | |
3396 | { | |
3397 | Perl_warner(aTHX_ WARN_REDEFINE, | |
3398 | CvCONST(cv) | |
3399 | ? "Constant subroutine %s redefined" | |
47deb5e7 | 3400 | : "Subroutine %s redefined", |
beab0874 JT |
3401 | GvENAME((GV*)dstr)); |
3402 | } | |
9607fc9c | 3403 | } |
3fe9a6f1 | 3404 | cv_ckproto(cv, (GV*)dstr, |
3405 | SvPOK(sref) ? SvPVX(sref) : Nullch); | |
4633a7c4 | 3406 | } |
a5f75d66 | 3407 | GvCV(dstr) = (CV*)sref; |
7a4c00b4 | 3408 | GvCVGEN(dstr) = 0; /* Switch off cacheness. */ |
a5f75d66 | 3409 | GvASSUMECV_on(dstr); |
3280af22 | 3410 | PL_sub_generation++; |
a5f75d66 | 3411 | } |
39bac7f7 | 3412 | if (!GvIMPORTED_CV(dstr) |
1d7c1841 GS |
3413 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
3414 | { | |
a5f75d66 | 3415 | GvIMPORTED_CV_on(dstr); |
1d7c1841 | 3416 | } |
8990e307 | 3417 | break; |
91bba347 LW |
3418 | case SVt_PVIO: |
3419 | if (intro) | |
3420 | SAVESPTR(GvIOp(dstr)); | |
3421 | else | |
3422 | dref = (SV*)GvIOp(dstr); | |
3423 | GvIOp(dstr) = (IO*)sref; | |
3424 | break; | |
f4d13ee9 JH |
3425 | case SVt_PVFM: |
3426 | if (intro) | |
3427 | SAVESPTR(GvFORM(dstr)); | |
3428 | else | |
3429 | dref = (SV*)GvFORM(dstr); | |
3430 | GvFORM(dstr) = (CV*)sref; | |
3431 | break; | |
8990e307 | 3432 | default: |
a0d0e21e LW |
3433 | if (intro) |
3434 | SAVESPTR(GvSV(dstr)); | |
3435 | else | |
3436 | dref = (SV*)GvSV(dstr); | |
8990e307 | 3437 | GvSV(dstr) = sref; |
39bac7f7 | 3438 | if (!GvIMPORTED_SV(dstr) |
1d7c1841 GS |
3439 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
3440 | { | |
a5f75d66 | 3441 | GvIMPORTED_SV_on(dstr); |
1d7c1841 | 3442 | } |
8990e307 LW |
3443 | break; |
3444 | } | |
3445 | if (dref) | |
3446 | SvREFCNT_dec(dref); | |
a0d0e21e LW |
3447 | if (intro) |
3448 | SAVEFREESV(sref); | |
27c9684d AP |
3449 | if (SvTAINTED(sstr)) |
3450 | SvTAINT(dstr); | |
8990e307 LW |
3451 | return; |
3452 | } | |
a0d0e21e | 3453 | if (SvPVX(dstr)) { |
760ac839 | 3454 | (void)SvOOK_off(dstr); /* backoff */ |
50483b2c JD |
3455 | if (SvLEN(dstr)) |
3456 | Safefree(SvPVX(dstr)); | |
a0d0e21e LW |
3457 | SvLEN(dstr)=SvCUR(dstr)=0; |
3458 | } | |
8990e307 | 3459 | } |
a0d0e21e | 3460 | (void)SvOK_off(dstr); |
8990e307 | 3461 | SvRV(dstr) = SvREFCNT_inc(SvRV(sstr)); |
ed6116ce | 3462 | SvROK_on(dstr); |
8990e307 | 3463 | if (sflags & SVp_NOK) { |
3332b3c1 JH |
3464 | SvNOKp_on(dstr); |
3465 | /* Only set the public OK flag if the source has public OK. */ | |
3466 | if (sflags & SVf_NOK) | |
3467 | SvFLAGS(dstr) |= SVf_NOK; | |
ed6116ce LW |
3468 | SvNVX(dstr) = SvNVX(sstr); |
3469 | } | |
8990e307 | 3470 | if (sflags & SVp_IOK) { |
3332b3c1 JH |
3471 | (void)SvIOKp_on(dstr); |
3472 | if (sflags & SVf_IOK) | |
3473 | SvFLAGS(dstr) |= SVf_IOK; | |
2b1c7e3e | 3474 | if (sflags & SVf_IVisUV) |
25da4f38 | 3475 | SvIsUV_on(dstr); |
3332b3c1 | 3476 | SvIVX(dstr) = SvIVX(sstr); |
ed6116ce | 3477 | } |
a0d0e21e LW |
3478 | if (SvAMAGIC(sstr)) { |
3479 | SvAMAGIC_on(dstr); | |
3480 | } | |
ed6116ce | 3481 | } |
8990e307 | 3482 | else if (sflags & SVp_POK) { |
79072805 LW |
3483 | |
3484 | /* | |
3485 | * Check to see if we can just swipe the string. If so, it's a | |
3486 | * possible small lose on short strings, but a big win on long ones. | |
463ee0b2 LW |
3487 | * It might even be a win on short strings if SvPVX(dstr) |
3488 | * has to be allocated and SvPVX(sstr) has to be freed. | |
79072805 LW |
3489 | */ |
3490 | ||
ff68c719 | 3491 | if (SvTEMP(sstr) && /* slated for free anyway? */ |
01b73108 | 3492 | SvREFCNT(sstr) == 1 && /* and no other references to it? */ |
1c846c1f | 3493 | !(sflags & SVf_OOK) && /* and not involved in OOK hack? */ |
4c8f17b9 BH |
3494 | SvLEN(sstr) && /* and really is a string */ |
3495 | !(PL_op && PL_op->op_type == OP_AASSIGN)) /* and won't be needed again, potentially */ | |
a5f75d66 | 3496 | { |
adbc6bb1 | 3497 | if (SvPVX(dstr)) { /* we know that dtype >= SVt_PV */ |
a5f75d66 AD |
3498 | if (SvOOK(dstr)) { |
3499 | SvFLAGS(dstr) &= ~SVf_OOK; | |
3500 | Safefree(SvPVX(dstr) - SvIVX(dstr)); | |
3501 | } | |
50483b2c | 3502 | else if (SvLEN(dstr)) |
a5f75d66 | 3503 | Safefree(SvPVX(dstr)); |
79072805 | 3504 | } |
a5f75d66 | 3505 | (void)SvPOK_only(dstr); |
463ee0b2 | 3506 | SvPV_set(dstr, SvPVX(sstr)); |
79072805 LW |
3507 | SvLEN_set(dstr, SvLEN(sstr)); |
3508 | SvCUR_set(dstr, SvCUR(sstr)); | |
f4e86e0f | 3509 | |
79072805 | 3510 | SvTEMP_off(dstr); |
2b1c7e3e | 3511 | (void)SvOK_off(sstr); /* NOTE: nukes most SvFLAGS on sstr */ |
79072805 LW |
3512 | SvPV_set(sstr, Nullch); |
3513 | SvLEN_set(sstr, 0); | |
a5f75d66 AD |
3514 | SvCUR_set(sstr, 0); |
3515 | SvTEMP_off(sstr); | |
79072805 LW |
3516 | } |
3517 | else { /* have to copy actual string */ | |
8990e307 LW |
3518 | STRLEN len = SvCUR(sstr); |
3519 | ||
3520 | SvGROW(dstr, len + 1); /* inlined from sv_setpvn */ | |
3521 | Move(SvPVX(sstr),SvPVX(dstr),len,char); | |
3522 | SvCUR_set(dstr, len); | |
3523 | *SvEND(dstr) = '\0'; | |
a0d0e21e | 3524 | (void)SvPOK_only(dstr); |
79072805 | 3525 | } |
9aa983d2 | 3526 | if (sflags & SVf_UTF8) |
a7cb1f99 | 3527 | SvUTF8_on(dstr); |
79072805 | 3528 | /*SUPPRESS 560*/ |
8990e307 | 3529 | if (sflags & SVp_NOK) { |
3332b3c1 JH |
3530 | SvNOKp_on(dstr); |
3531 | if (sflags & SVf_NOK) | |
3532 | SvFLAGS(dstr) |= SVf_NOK; | |
463ee0b2 | 3533 | SvNVX(dstr) = SvNVX(sstr); |
79072805 | 3534 | } |
8990e307 | 3535 | if (sflags & SVp_IOK) { |
3332b3c1 JH |
3536 | (void)SvIOKp_on(dstr); |
3537 | if (sflags & SVf_IOK) | |
3538 | SvFLAGS(dstr) |= SVf_IOK; | |
2b1c7e3e | 3539 | if (sflags & SVf_IVisUV) |
25da4f38 | 3540 | SvIsUV_on(dstr); |
463ee0b2 | 3541 | SvIVX(dstr) = SvIVX(sstr); |
79072805 LW |
3542 | } |
3543 | } | |
8990e307 | 3544 | else if (sflags & SVp_IOK) { |
3332b3c1 JH |
3545 | if (sflags & SVf_IOK) |
3546 | (void)SvIOK_only(dstr); | |
3547 | else { | |
9cbac4c7 DM |
3548 | (void)SvOK_off(dstr); |
3549 | (void)SvIOKp_on(dstr); | |
3332b3c1 JH |
3550 | } |
3551 | /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */ | |
2b1c7e3e | 3552 | if (sflags & SVf_IVisUV) |
25da4f38 | 3553 | SvIsUV_on(dstr); |
3332b3c1 JH |
3554 | SvIVX(dstr) = SvIVX(sstr); |
3555 | if (sflags & SVp_NOK) { | |
3556 | if (sflags & SVf_NOK) | |
3557 | (void)SvNOK_on(dstr); | |
3558 | else | |
3559 | (void)SvNOKp_on(dstr); | |
3560 | SvNVX(dstr) = SvNVX(sstr); | |
3561 | } | |
3562 | } | |
3563 | else if (sflags & SVp_NOK) { | |
3564 | if (sflags & SVf_NOK) | |
3565 | (void)SvNOK_only(dstr); | |
3566 | else { | |
9cbac4c7 | 3567 | (void)SvOK_off(dstr); |
3332b3c1 JH |
3568 | SvNOKp_on(dstr); |
3569 | } | |
3570 | SvNVX(dstr) = SvNVX(sstr); | |
79072805 LW |
3571 | } |
3572 | else { | |
20408e3c | 3573 | if (dtype == SVt_PVGV) { |
e476b1b5 GS |
3574 | if (ckWARN(WARN_MISC)) |
3575 | Perl_warner(aTHX_ WARN_MISC, "Undefined value assigned to typeglob"); | |
20408e3c GS |
3576 | } |
3577 | else | |
3578 | (void)SvOK_off(dstr); | |
a0d0e21e | 3579 | } |
27c9684d AP |
3580 | if (SvTAINTED(sstr)) |
3581 | SvTAINT(dstr); | |
79072805 LW |
3582 | } |
3583 | ||
954c1994 GS |
3584 | /* |
3585 | =for apidoc sv_setsv_mg | |
3586 | ||
3587 | Like C<sv_setsv>, but also handles 'set' magic. | |
3588 | ||
3589 | =cut | |
3590 | */ | |
3591 | ||
79072805 | 3592 | void |
864dbfa3 | 3593 | Perl_sv_setsv_mg(pTHX_ SV *dstr, register SV *sstr) |
ef50df4b GS |
3594 | { |
3595 | sv_setsv(dstr,sstr); | |
3596 | SvSETMAGIC(dstr); | |
3597 | } | |
3598 | ||
954c1994 GS |
3599 | /* |
3600 | =for apidoc sv_setpvn | |
3601 | ||
3602 | Copies a string into an SV. The C<len> parameter indicates the number of | |
3603 | bytes to be copied. Does not handle 'set' magic. See C<sv_setpvn_mg>. | |
3604 | ||
3605 | =cut | |
3606 | */ | |
3607 | ||
ef50df4b | 3608 | void |
864dbfa3 | 3609 | Perl_sv_setpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len) |
79072805 | 3610 | { |
c6f8c383 | 3611 | register char *dptr; |
22c522df | 3612 | |
2213622d | 3613 | SV_CHECK_THINKFIRST(sv); |
463ee0b2 | 3614 | if (!ptr) { |
a0d0e21e | 3615 | (void)SvOK_off(sv); |
463ee0b2 LW |
3616 | return; |
3617 | } | |
22c522df JH |
3618 | else { |
3619 | /* len is STRLEN which is unsigned, need to copy to signed */ | |
3620 | IV iv = len; | |
3621 | assert(iv >= 0); | |
3622 | } | |
6fc92669 | 3623 | (void)SvUPGRADE(sv, SVt_PV); |
c6f8c383 | 3624 | |
79072805 | 3625 | SvGROW(sv, len + 1); |
c6f8c383 GA |
3626 | dptr = SvPVX(sv); |
3627 | Move(ptr,dptr,len,char); | |
3628 | dptr[len] = '\0'; | |
79072805 | 3629 | SvCUR_set(sv, len); |
1aa99e6b | 3630 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 | 3631 | SvTAINT(sv); |
79072805 LW |
3632 | } |
3633 | ||
954c1994 GS |
3634 | /* |
3635 | =for apidoc sv_setpvn_mg | |
3636 | ||
3637 | Like C<sv_setpvn>, but also handles 'set' magic. | |
3638 | ||
3639 | =cut | |
3640 | */ | |
3641 | ||
79072805 | 3642 | void |
864dbfa3 | 3643 | Perl_sv_setpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len) |
ef50df4b GS |
3644 | { |
3645 | sv_setpvn(sv,ptr,len); | |
3646 | SvSETMAGIC(sv); | |
3647 | } | |
3648 | ||
954c1994 GS |
3649 | /* |
3650 | =for apidoc sv_setpv | |
3651 | ||
3652 | Copies a string into an SV. The string must be null-terminated. Does not | |
3653 | handle 'set' magic. See C<sv_setpv_mg>. | |
3654 | ||
3655 | =cut | |
3656 | */ | |
3657 | ||
ef50df4b | 3658 | void |
864dbfa3 | 3659 | Perl_sv_setpv(pTHX_ register SV *sv, register const char *ptr) |
79072805 LW |
3660 | { |
3661 | register STRLEN len; | |
3662 | ||
2213622d | 3663 | SV_CHECK_THINKFIRST(sv); |
463ee0b2 | 3664 | if (!ptr) { |
a0d0e21e | 3665 | (void)SvOK_off(sv); |
463ee0b2 LW |
3666 | return; |
3667 | } | |
79072805 | 3668 | len = strlen(ptr); |
6fc92669 | 3669 | (void)SvUPGRADE(sv, SVt_PV); |
c6f8c383 | 3670 | |
79072805 | 3671 | SvGROW(sv, len + 1); |
463ee0b2 | 3672 | Move(ptr,SvPVX(sv),len+1,char); |
79072805 | 3673 | SvCUR_set(sv, len); |
1aa99e6b | 3674 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 LW |
3675 | SvTAINT(sv); |
3676 | } | |
3677 | ||
954c1994 GS |
3678 | /* |
3679 | =for apidoc sv_setpv_mg | |
3680 | ||
3681 | Like C<sv_setpv>, but also handles 'set' magic. | |
3682 | ||
3683 | =cut | |
3684 | */ | |
3685 | ||
463ee0b2 | 3686 | void |
864dbfa3 | 3687 | Perl_sv_setpv_mg(pTHX_ register SV *sv, register const char *ptr) |
ef50df4b GS |
3688 | { |
3689 | sv_setpv(sv,ptr); | |
3690 | SvSETMAGIC(sv); | |
3691 | } | |
3692 | ||
954c1994 GS |
3693 | /* |
3694 | =for apidoc sv_usepvn | |
3695 | ||
3696 | Tells an SV to use C<ptr> to find its string value. Normally the string is | |
1c846c1f | 3697 | stored inside the SV but sv_usepvn allows the SV to use an outside string. |
954c1994 GS |
3698 | The C<ptr> should point to memory that was allocated by C<malloc>. The |
3699 | string length, C<len>, must be supplied. This function will realloc the | |
3700 | memory pointed to by C<ptr>, so that pointer should not be freed or used by | |
3701 | the programmer after giving it to sv_usepvn. Does not handle 'set' magic. | |
3702 | See C<sv_usepvn_mg>. | |
3703 | ||
3704 | =cut | |
3705 | */ | |
3706 | ||
ef50df4b | 3707 | void |
864dbfa3 | 3708 | Perl_sv_usepvn(pTHX_ register SV *sv, register char *ptr, register STRLEN len) |
463ee0b2 | 3709 | { |
2213622d | 3710 | SV_CHECK_THINKFIRST(sv); |
c6f8c383 | 3711 | (void)SvUPGRADE(sv, SVt_PV); |
463ee0b2 | 3712 | if (!ptr) { |
a0d0e21e | 3713 | (void)SvOK_off(sv); |
463ee0b2 LW |
3714 | return; |
3715 | } | |
a0ed51b3 | 3716 | (void)SvOOK_off(sv); |
50483b2c | 3717 | if (SvPVX(sv) && SvLEN(sv)) |
463ee0b2 LW |
3718 | Safefree(SvPVX(sv)); |
3719 | Renew(ptr, len+1, char); | |
3720 | SvPVX(sv) = ptr; | |
3721 | SvCUR_set(sv, len); | |
3722 | SvLEN_set(sv, len+1); | |
3723 | *SvEND(sv) = '\0'; | |
1aa99e6b | 3724 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 | 3725 | SvTAINT(sv); |
79072805 LW |
3726 | } |
3727 | ||
954c1994 GS |
3728 | /* |
3729 | =for apidoc sv_usepvn_mg | |
3730 | ||
3731 | Like C<sv_usepvn>, but also handles 'set' magic. | |
3732 | ||
3733 | =cut | |
3734 | */ | |
3735 | ||
ef50df4b | 3736 | void |
864dbfa3 | 3737 | Perl_sv_usepvn_mg(pTHX_ register SV *sv, register char *ptr, register STRLEN len) |
ef50df4b | 3738 | { |
51c1089b | 3739 | sv_usepvn(sv,ptr,len); |
ef50df4b GS |
3740 | SvSETMAGIC(sv); |
3741 | } | |
3742 | ||
6fc92669 | 3743 | void |
840a7b70 | 3744 | Perl_sv_force_normal_flags(pTHX_ register SV *sv, U32 flags) |
0f15f207 | 3745 | { |
2213622d | 3746 | if (SvREADONLY(sv)) { |
1c846c1f NIS |
3747 | if (SvFAKE(sv)) { |
3748 | char *pvx = SvPVX(sv); | |
3749 | STRLEN len = SvCUR(sv); | |
3750 | U32 hash = SvUVX(sv); | |
3751 | SvGROW(sv, len + 1); | |
3752 | Move(pvx,SvPVX(sv),len,char); | |
3753 | *SvEND(sv) = '\0'; | |
3754 | SvFAKE_off(sv); | |
3755 | SvREADONLY_off(sv); | |
c3654f1a | 3756 | unsharepvn(pvx,SvUTF8(sv)?-len:len,hash); |
1c846c1f NIS |
3757 | } |
3758 | else if (PL_curcop != &PL_compiling) | |
cea2e8a9 | 3759 | Perl_croak(aTHX_ PL_no_modify); |
0f15f207 | 3760 | } |
2213622d | 3761 | if (SvROK(sv)) |
840a7b70 | 3762 | sv_unref_flags(sv, flags); |
6fc92669 GS |
3763 | else if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV) |
3764 | sv_unglob(sv); | |
0f15f207 | 3765 | } |
1c846c1f | 3766 | |
840a7b70 IZ |
3767 | void |
3768 | Perl_sv_force_normal(pTHX_ register SV *sv) | |
3769 | { | |
3770 | sv_force_normal_flags(sv, 0); | |
3771 | } | |
3772 | ||
954c1994 GS |
3773 | /* |
3774 | =for apidoc sv_chop | |
3775 | ||
1c846c1f | 3776 | Efficient removal of characters from the beginning of the string buffer. |
954c1994 GS |
3777 | SvPOK(sv) must be true and the C<ptr> must be a pointer to somewhere inside |
3778 | the string buffer. The C<ptr> becomes the first character of the adjusted | |
3779 | string. | |
3780 | ||
3781 | =cut | |
3782 | */ | |
3783 | ||
79072805 | 3784 | void |
864dbfa3 | 3785 | Perl_sv_chop(pTHX_ register SV *sv, register char *ptr) /* like set but assuming ptr is in sv */ |
1c846c1f NIS |
3786 | |
3787 | ||
79072805 LW |
3788 | { |
3789 | register STRLEN delta; | |
3790 | ||
a0d0e21e | 3791 | if (!ptr || !SvPOKp(sv)) |
79072805 | 3792 | return; |
2213622d | 3793 | SV_CHECK_THINKFIRST(sv); |
79072805 LW |
3794 | if (SvTYPE(sv) < SVt_PVIV) |
3795 | sv_upgrade(sv,SVt_PVIV); | |
3796 | ||
3797 | if (!SvOOK(sv)) { | |
50483b2c JD |
3798 | if (!SvLEN(sv)) { /* make copy of shared string */ |
3799 | char *pvx = SvPVX(sv); | |
3800 | STRLEN len = SvCUR(sv); | |
3801 | SvGROW(sv, len + 1); | |
3802 | Move(pvx,SvPVX(sv),len,char); | |
3803 | *SvEND(sv) = '\0'; | |
3804 | } | |
463ee0b2 | 3805 | SvIVX(sv) = 0; |
79072805 LW |
3806 | SvFLAGS(sv) |= SVf_OOK; |
3807 | } | |
25da4f38 | 3808 | SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVp_IOK|SVp_NOK|SVf_IVisUV); |
463ee0b2 | 3809 | delta = ptr - SvPVX(sv); |
79072805 LW |
3810 | SvLEN(sv) -= delta; |
3811 | SvCUR(sv) -= delta; | |
463ee0b2 LW |
3812 | SvPVX(sv) += delta; |
3813 | SvIVX(sv) += delta; | |
79072805 LW |
3814 | } |
3815 | ||
954c1994 GS |
3816 | /* |
3817 | =for apidoc sv_catpvn | |
3818 | ||
3819 | Concatenates the string onto the end of the string which is in the SV. The | |
3820 | C<len> indicates number of bytes to copy. Handles 'get' magic, but not | |
3821 | 'set' magic. See C<sv_catpvn_mg>. | |
3822 | ||
3823 | =cut | |
3824 | */ | |
3825 | ||
79072805 | 3826 | void |
864dbfa3 | 3827 | Perl_sv_catpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len) |
79072805 | 3828 | { |
463ee0b2 | 3829 | STRLEN tlen; |
748a9306 | 3830 | char *junk; |
a0d0e21e | 3831 | |
748a9306 | 3832 | junk = SvPV_force(sv, tlen); |
463ee0b2 | 3833 | SvGROW(sv, tlen + len + 1); |
4633a7c4 LW |
3834 | if (ptr == junk) |
3835 | ptr = SvPVX(sv); | |
463ee0b2 | 3836 | Move(ptr,SvPVX(sv)+tlen,len,char); |
79072805 LW |
3837 | SvCUR(sv) += len; |
3838 | *SvEND(sv) = '\0'; | |
d41ff1b8 | 3839 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 | 3840 | SvTAINT(sv); |
79072805 LW |
3841 | } |
3842 | ||
954c1994 GS |
3843 | /* |
3844 | =for apidoc sv_catpvn_mg | |
3845 | ||
3846 | Like C<sv_catpvn>, but also handles 'set' magic. | |
3847 | ||
3848 | =cut | |
3849 | */ | |
3850 | ||
79072805 | 3851 | void |
864dbfa3 | 3852 | Perl_sv_catpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len) |
ef50df4b GS |
3853 | { |
3854 | sv_catpvn(sv,ptr,len); | |
3855 | SvSETMAGIC(sv); | |
3856 | } | |
3857 | ||
954c1994 GS |
3858 | /* |
3859 | =for apidoc sv_catsv | |
3860 | ||
13e8c8e3 JH |
3861 | Concatenates the string from SV C<ssv> onto the end of the string in |
3862 | SV C<dsv>. Modifies C<dsv> but not C<ssv>. Handles 'get' magic, but | |
3863 | not 'set' magic. See C<sv_catsv_mg>. | |
954c1994 | 3864 | |
13e8c8e3 | 3865 | =cut */ |
954c1994 | 3866 | |
ef50df4b | 3867 | void |
46199a12 | 3868 | Perl_sv_catsv(pTHX_ SV *dsv, register SV *ssv) |
79072805 | 3869 | { |
13e8c8e3 JH |
3870 | char *spv; |
3871 | STRLEN slen; | |
46199a12 | 3872 | if (!ssv) |
79072805 | 3873 | return; |
46199a12 JH |
3874 | if ((spv = SvPV(ssv, slen))) { |
3875 | bool dutf8 = DO_UTF8(dsv); | |
3876 | bool sutf8 = DO_UTF8(ssv); | |
13e8c8e3 JH |
3877 | |
3878 | if (dutf8 == sutf8) | |
46199a12 | 3879 | sv_catpvn(dsv,spv,slen); |
13e8c8e3 JH |
3880 | else { |
3881 | if (dutf8) { | |
46199a12 JH |
3882 | /* Not modifying source SV, so taking a temporary copy. */ |
3883 | SV* csv = sv_2mortal(newSVsv(ssv)); | |
13e8c8e3 JH |
3884 | char *cpv; |
3885 | STRLEN clen; | |
3886 | ||
46199a12 JH |
3887 | sv_utf8_upgrade(csv); |
3888 | cpv = SvPV(csv,clen); | |
3889 | sv_catpvn(dsv,cpv,clen); | |
13e8c8e3 JH |
3890 | } |
3891 | else { | |
46199a12 JH |
3892 | sv_utf8_upgrade(dsv); |
3893 | sv_catpvn(dsv,spv,slen); | |
3894 | SvUTF8_on(dsv); /* If dsv has no wide characters. */ | |
13e8c8e3 | 3895 | } |
e84ff256 | 3896 | } |
560a288e | 3897 | } |
79072805 LW |
3898 | } |
3899 | ||
954c1994 GS |
3900 | /* |
3901 | =for apidoc sv_catsv_mg | |
3902 | ||
3903 | Like C<sv_catsv>, but also handles 'set' magic. | |
3904 | ||
3905 | =cut | |
3906 | */ | |
3907 | ||
79072805 | 3908 | void |
46199a12 | 3909 | Perl_sv_catsv_mg(pTHX_ SV *dsv, register SV *ssv) |
ef50df4b | 3910 | { |
46199a12 JH |
3911 | sv_catsv(dsv,ssv); |
3912 | SvSETMAGIC(dsv); | |
ef50df4b GS |
3913 | } |
3914 | ||
954c1994 GS |
3915 | /* |
3916 | =for apidoc sv_catpv | |
3917 | ||
3918 | Concatenates the string onto the end of the string which is in the SV. | |
3919 | Handles 'get' magic, but not 'set' magic. See C<sv_catpv_mg>. | |
3920 | ||
3921 | =cut | |
3922 | */ | |
3923 | ||
ef50df4b | 3924 | void |
0c981600 | 3925 | Perl_sv_catpv(pTHX_ register SV *sv, register const char *ptr) |
79072805 LW |
3926 | { |
3927 | register STRLEN len; | |
463ee0b2 | 3928 | STRLEN tlen; |
748a9306 | 3929 | char *junk; |
79072805 | 3930 | |
0c981600 | 3931 | if (!ptr) |
79072805 | 3932 | return; |
748a9306 | 3933 | junk = SvPV_force(sv, tlen); |
0c981600 | 3934 | len = strlen(ptr); |
463ee0b2 | 3935 | SvGROW(sv, tlen + len + 1); |
0c981600 JH |
3936 | if (ptr == junk) |
3937 | ptr = SvPVX(sv); | |
3938 | Move(ptr,SvPVX(sv)+tlen,len+1,char); | |
79072805 | 3939 | SvCUR(sv) += len; |
d41ff1b8 | 3940 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 | 3941 | SvTAINT(sv); |
79072805 LW |
3942 | } |
3943 | ||
954c1994 GS |
3944 | /* |
3945 | =for apidoc sv_catpv_mg | |
3946 | ||
3947 | Like C<sv_catpv>, but also handles 'set' magic. | |
3948 | ||
3949 | =cut | |
3950 | */ | |
3951 | ||
ef50df4b | 3952 | void |
0c981600 | 3953 | Perl_sv_catpv_mg(pTHX_ register SV *sv, register const char *ptr) |
ef50df4b | 3954 | { |
0c981600 | 3955 | sv_catpv(sv,ptr); |
ef50df4b GS |
3956 | SvSETMAGIC(sv); |
3957 | } | |
3958 | ||
79072805 | 3959 | SV * |
864dbfa3 | 3960 | Perl_newSV(pTHX_ STRLEN len) |
79072805 LW |
3961 | { |
3962 | register SV *sv; | |
1c846c1f | 3963 | |
4561caa4 | 3964 | new_SV(sv); |
79072805 LW |
3965 | if (len) { |
3966 | sv_upgrade(sv, SVt_PV); | |
3967 | SvGROW(sv, len + 1); | |
3968 | } | |
3969 | return sv; | |
3970 | } | |
3971 | ||
1edc1566 | 3972 | /* name is assumed to contain an SV* if (name && namelen == HEf_SVKEY) */ |
3973 | ||
954c1994 GS |
3974 | /* |
3975 | =for apidoc sv_magic | |
3976 | ||
3977 | Adds magic to an SV. | |
3978 | ||
3979 | =cut | |
3980 | */ | |
3981 | ||
79072805 | 3982 | void |
864dbfa3 | 3983 | Perl_sv_magic(pTHX_ register SV *sv, SV *obj, int how, const char *name, I32 namlen) |
79072805 LW |
3984 | { |
3985 | MAGIC* mg; | |
1c846c1f | 3986 | |
0f15f207 | 3987 | if (SvREADONLY(sv)) { |
3280af22 | 3988 | if (PL_curcop != &PL_compiling && !strchr("gBf", how)) |
cea2e8a9 | 3989 | Perl_croak(aTHX_ PL_no_modify); |
0f15f207 | 3990 | } |
4633a7c4 | 3991 | if (SvMAGICAL(sv) || (how == 't' && SvTYPE(sv) >= SVt_PVMG)) { |
748a9306 LW |
3992 | if (SvMAGIC(sv) && (mg = mg_find(sv, how))) { |
3993 | if (how == 't') | |
565764a8 | 3994 | mg->mg_len |= 1; |
463ee0b2 | 3995 | return; |
748a9306 | 3996 | } |
463ee0b2 LW |
3997 | } |
3998 | else { | |
c6f8c383 | 3999 | (void)SvUPGRADE(sv, SVt_PVMG); |
463ee0b2 | 4000 | } |
79072805 LW |
4001 | Newz(702,mg, 1, MAGIC); |
4002 | mg->mg_moremagic = SvMAGIC(sv); | |
79072805 | 4003 | SvMAGIC(sv) = mg; |
75f9d97a JH |
4004 | |
4005 | /* Some magic sontains a reference loop, where the sv and object refer to | |
4006 | each other. To prevent a avoid a reference loop that would prevent such | |
4007 | objects being freed, we look for such loops and if we find one we avoid | |
4008 | incrementing the object refcount. */ | |
4009 | if (!obj || obj == sv || how == '#' || how == 'r' || | |
4010 | (SvTYPE(obj) == SVt_PVGV && | |
4011 | (GvSV(obj) == sv || GvHV(obj) == (HV*)sv || GvAV(obj) == (AV*)sv || | |
4012 | GvCV(obj) == (CV*)sv || GvIOp(obj) == (IO*)sv || | |
4013 | GvFORM(obj) == (CV*)sv))) | |
4014 | { | |
8990e307 | 4015 | mg->mg_obj = obj; |
75f9d97a | 4016 | } |
85e6fe83 | 4017 | else { |
8990e307 | 4018 | mg->mg_obj = SvREFCNT_inc(obj); |
85e6fe83 LW |
4019 | mg->mg_flags |= MGf_REFCOUNTED; |
4020 | } | |
79072805 | 4021 | mg->mg_type = how; |
565764a8 | 4022 | mg->mg_len = namlen; |
9cbac4c7 | 4023 | if (name) { |
1edc1566 | 4024 | if (namlen >= 0) |
4025 | mg->mg_ptr = savepvn(name, namlen); | |
c6ee37c5 | 4026 | else if (namlen == HEf_SVKEY) |
1edc1566 | 4027 | mg->mg_ptr = (char*)SvREFCNT_inc((SV*)name); |
9cbac4c7 | 4028 | } |
1c846c1f | 4029 | |
79072805 LW |
4030 | switch (how) { |
4031 | case 0: | |
22c35a8c | 4032 | mg->mg_virtual = &PL_vtbl_sv; |
79072805 | 4033 | break; |
a0d0e21e | 4034 | case 'A': |
22c35a8c | 4035 | mg->mg_virtual = &PL_vtbl_amagic; |
a0d0e21e LW |
4036 | break; |
4037 | case 'a': | |
22c35a8c | 4038 | mg->mg_virtual = &PL_vtbl_amagicelem; |
a0d0e21e LW |
4039 | break; |
4040 | case 'c': | |
d460ef45 | 4041 | mg->mg_virtual = &PL_vtbl_ovrld; |
a0d0e21e | 4042 | break; |
79072805 | 4043 | case 'B': |
22c35a8c | 4044 | mg->mg_virtual = &PL_vtbl_bm; |
79072805 | 4045 | break; |
6cef1e77 | 4046 | case 'D': |
22c35a8c | 4047 | mg->mg_virtual = &PL_vtbl_regdata; |
6cef1e77 IZ |
4048 | break; |
4049 | case 'd': | |
22c35a8c | 4050 | mg->mg_virtual = &PL_vtbl_regdatum; |
6cef1e77 | 4051 | break; |
79072805 | 4052 | case 'E': |
22c35a8c | 4053 | mg->mg_virtual = &PL_vtbl_env; |
79072805 | 4054 | break; |
55497cff | 4055 | case 'f': |
22c35a8c | 4056 | mg->mg_virtual = &PL_vtbl_fm; |
55497cff | 4057 | break; |
79072805 | 4058 | case 'e': |
22c35a8c | 4059 | mg->mg_virtual = &PL_vtbl_envelem; |
79072805 | 4060 | break; |
93a17b20 | 4061 | case 'g': |
22c35a8c | 4062 | mg->mg_virtual = &PL_vtbl_mglob; |
93a17b20 | 4063 | break; |
463ee0b2 | 4064 | case 'I': |
22c35a8c | 4065 | mg->mg_virtual = &PL_vtbl_isa; |
463ee0b2 LW |
4066 | break; |
4067 | case 'i': | |
22c35a8c | 4068 | mg->mg_virtual = &PL_vtbl_isaelem; |
463ee0b2 | 4069 | break; |
16660edb | 4070 | case 'k': |
22c35a8c | 4071 | mg->mg_virtual = &PL_vtbl_nkeys; |
16660edb | 4072 | break; |
79072805 | 4073 | case 'L': |
a0d0e21e | 4074 | SvRMAGICAL_on(sv); |
93a17b20 LW |
4075 | mg->mg_virtual = 0; |
4076 | break; | |
4077 | case 'l': | |
22c35a8c | 4078 | mg->mg_virtual = &PL_vtbl_dbline; |
79072805 | 4079 | break; |
f93b4edd MB |
4080 | #ifdef USE_THREADS |
4081 | case 'm': | |
22c35a8c | 4082 | mg->mg_virtual = &PL_vtbl_mutex; |
f93b4edd MB |
4083 | break; |
4084 | #endif /* USE_THREADS */ | |
36477c24 | 4085 | #ifdef USE_LOCALE_COLLATE |
bbce6d69 | 4086 | case 'o': |
22c35a8c | 4087 | mg->mg_virtual = &PL_vtbl_collxfrm; |
bbce6d69 | 4088 | break; |
36477c24 | 4089 | #endif /* USE_LOCALE_COLLATE */ |
463ee0b2 | 4090 | case 'P': |
22c35a8c | 4091 | mg->mg_virtual = &PL_vtbl_pack; |
463ee0b2 LW |
4092 | break; |
4093 | case 'p': | |
a0d0e21e | 4094 | case 'q': |
22c35a8c | 4095 | mg->mg_virtual = &PL_vtbl_packelem; |
463ee0b2 | 4096 | break; |
c277df42 | 4097 | case 'r': |
22c35a8c | 4098 | mg->mg_virtual = &PL_vtbl_regexp; |
c277df42 | 4099 | break; |
79072805 | 4100 | case 'S': |
22c35a8c | 4101 | mg->mg_virtual = &PL_vtbl_sig; |
79072805 LW |
4102 | break; |
4103 | case 's': | |
22c35a8c | 4104 | mg->mg_virtual = &PL_vtbl_sigelem; |
79072805 | 4105 | break; |
463ee0b2 | 4106 | case 't': |
22c35a8c | 4107 | mg->mg_virtual = &PL_vtbl_taint; |
565764a8 | 4108 | mg->mg_len = 1; |
463ee0b2 | 4109 | break; |
79072805 | 4110 | case 'U': |
22c35a8c | 4111 | mg->mg_virtual = &PL_vtbl_uvar; |
79072805 LW |
4112 | break; |
4113 | case 'v': | |
22c35a8c | 4114 | mg->mg_virtual = &PL_vtbl_vec; |
79072805 LW |
4115 | break; |
4116 | case 'x': | |
22c35a8c | 4117 | mg->mg_virtual = &PL_vtbl_substr; |
79072805 | 4118 | break; |
5f05dabc | 4119 | case 'y': |
22c35a8c | 4120 | mg->mg_virtual = &PL_vtbl_defelem; |
5f05dabc | 4121 | break; |
79072805 | 4122 | case '*': |
22c35a8c | 4123 | mg->mg_virtual = &PL_vtbl_glob; |
79072805 LW |
4124 | break; |
4125 | case '#': | |
22c35a8c | 4126 | mg->mg_virtual = &PL_vtbl_arylen; |
79072805 | 4127 | break; |
a0d0e21e | 4128 | case '.': |
22c35a8c | 4129 | mg->mg_virtual = &PL_vtbl_pos; |
a0d0e21e | 4130 | break; |
810b8aa5 GS |
4131 | case '<': |
4132 | mg->mg_virtual = &PL_vtbl_backref; | |
4133 | break; | |
4633a7c4 LW |
4134 | case '~': /* Reserved for use by extensions not perl internals. */ |
4135 | /* Useful for attaching extension internal data to perl vars. */ | |
4136 | /* Note that multiple extensions may clash if magical scalars */ | |
4137 | /* etc holding private data from one are passed to another. */ | |
4138 | SvRMAGICAL_on(sv); | |
a0d0e21e | 4139 | break; |
79072805 | 4140 | default: |
cea2e8a9 | 4141 | Perl_croak(aTHX_ "Don't know how to handle magic of type '%c'", how); |
463ee0b2 | 4142 | } |
8990e307 LW |
4143 | mg_magical(sv); |
4144 | if (SvGMAGICAL(sv)) | |
4145 | SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK); | |
463ee0b2 LW |
4146 | } |
4147 | ||
c461cf8f JH |
4148 | /* |
4149 | =for apidoc sv_unmagic | |
4150 | ||
4151 | Removes magic from an SV. | |
4152 | ||
4153 | =cut | |
4154 | */ | |
4155 | ||
463ee0b2 | 4156 | int |
864dbfa3 | 4157 | Perl_sv_unmagic(pTHX_ SV *sv, int type) |
463ee0b2 LW |
4158 | { |
4159 | MAGIC* mg; | |
4160 | MAGIC** mgp; | |
91bba347 | 4161 | if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv)) |
463ee0b2 LW |
4162 | return 0; |
4163 | mgp = &SvMAGIC(sv); | |
4164 | for (mg = *mgp; mg; mg = *mgp) { | |
4165 | if (mg->mg_type == type) { | |
4166 | MGVTBL* vtbl = mg->mg_virtual; | |
4167 | *mgp = mg->mg_moremagic; | |
1d7c1841 | 4168 | if (vtbl && vtbl->svt_free) |
fc0dc3b3 | 4169 | CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg); |
9cbac4c7 | 4170 | if (mg->mg_ptr && mg->mg_type != 'g') { |
565764a8 | 4171 | if (mg->mg_len >= 0) |
1edc1566 | 4172 | Safefree(mg->mg_ptr); |
565764a8 | 4173 | else if (mg->mg_len == HEf_SVKEY) |
1edc1566 | 4174 | SvREFCNT_dec((SV*)mg->mg_ptr); |
9cbac4c7 | 4175 | } |
a0d0e21e LW |
4176 | if (mg->mg_flags & MGf_REFCOUNTED) |
4177 | SvREFCNT_dec(mg->mg_obj); | |
463ee0b2 LW |
4178 | Safefree(mg); |
4179 | } | |
4180 | else | |
4181 | mgp = &mg->mg_moremagic; | |
79072805 | 4182 | } |
91bba347 | 4183 | if (!SvMAGIC(sv)) { |
463ee0b2 | 4184 | SvMAGICAL_off(sv); |
8990e307 | 4185 | SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT; |
463ee0b2 LW |
4186 | } |
4187 | ||
4188 | return 0; | |
79072805 LW |
4189 | } |
4190 | ||
c461cf8f JH |
4191 | /* |
4192 | =for apidoc sv_rvweaken | |
4193 | ||
4194 | Weaken a reference. | |
4195 | ||
4196 | =cut | |
4197 | */ | |
4198 | ||
810b8aa5 | 4199 | SV * |
864dbfa3 | 4200 | Perl_sv_rvweaken(pTHX_ SV *sv) |
810b8aa5 GS |
4201 | { |
4202 | SV *tsv; | |
4203 | if (!SvOK(sv)) /* let undefs pass */ | |
4204 | return sv; | |
4205 | if (!SvROK(sv)) | |
cea2e8a9 | 4206 | Perl_croak(aTHX_ "Can't weaken a nonreference"); |
810b8aa5 | 4207 | else if (SvWEAKREF(sv)) { |
810b8aa5 | 4208 | if (ckWARN(WARN_MISC)) |
cea2e8a9 | 4209 | Perl_warner(aTHX_ WARN_MISC, "Reference is already weak"); |
810b8aa5 GS |
4210 | return sv; |
4211 | } | |
4212 | tsv = SvRV(sv); | |
4213 | sv_add_backref(tsv, sv); | |
4214 | SvWEAKREF_on(sv); | |
1c846c1f | 4215 | SvREFCNT_dec(tsv); |
810b8aa5 GS |
4216 | return sv; |
4217 | } | |
4218 | ||
4219 | STATIC void | |
cea2e8a9 | 4220 | S_sv_add_backref(pTHX_ SV *tsv, SV *sv) |
810b8aa5 GS |
4221 | { |
4222 | AV *av; | |
4223 | MAGIC *mg; | |
4224 | if (SvMAGICAL(tsv) && (mg = mg_find(tsv, '<'))) | |
4225 | av = (AV*)mg->mg_obj; | |
4226 | else { | |
4227 | av = newAV(); | |
4228 | sv_magic(tsv, (SV*)av, '<', NULL, 0); | |
4229 | SvREFCNT_dec(av); /* for sv_magic */ | |
4230 | } | |
4231 | av_push(av,sv); | |
4232 | } | |
4233 | ||
1c846c1f | 4234 | STATIC void |
cea2e8a9 | 4235 | S_sv_del_backref(pTHX_ SV *sv) |
810b8aa5 GS |
4236 | { |
4237 | AV *av; | |
4238 | SV **svp; | |
4239 | I32 i; | |
4240 | SV *tsv = SvRV(sv); | |
4241 | MAGIC *mg; | |
4242 | if (!SvMAGICAL(tsv) || !(mg = mg_find(tsv, '<'))) | |
cea2e8a9 | 4243 | Perl_croak(aTHX_ "panic: del_backref"); |
810b8aa5 GS |
4244 | av = (AV *)mg->mg_obj; |
4245 | svp = AvARRAY(av); | |
4246 | i = AvFILLp(av); | |
4247 | while (i >= 0) { | |
4248 | if (svp[i] == sv) { | |
4249 | svp[i] = &PL_sv_undef; /* XXX */ | |
4250 | } | |
4251 | i--; | |
4252 | } | |
4253 | } | |
4254 | ||
954c1994 GS |
4255 | /* |
4256 | =for apidoc sv_insert | |
4257 | ||
4258 | Inserts a string at the specified offset/length within the SV. Similar to | |
4259 | the Perl substr() function. | |
4260 | ||
4261 | =cut | |
4262 | */ | |
4263 | ||
79072805 | 4264 | void |
864dbfa3 | 4265 | Perl_sv_insert(pTHX_ SV *bigstr, STRLEN offset, STRLEN len, char *little, STRLEN littlelen) |
79072805 LW |
4266 | { |
4267 | register char *big; | |
4268 | register char *mid; | |
4269 | register char *midend; | |
4270 | register char *bigend; | |
4271 | register I32 i; | |
6ff81951 | 4272 | STRLEN curlen; |
1c846c1f | 4273 | |
79072805 | 4274 | |
8990e307 | 4275 | if (!bigstr) |
cea2e8a9 | 4276 | Perl_croak(aTHX_ "Can't modify non-existent substring"); |
6ff81951 | 4277 | SvPV_force(bigstr, curlen); |
60fa28ff | 4278 | (void)SvPOK_only_UTF8(bigstr); |
6ff81951 GS |
4279 | if (offset + len > curlen) { |
4280 | SvGROW(bigstr, offset+len+1); | |
4281 | Zero(SvPVX(bigstr)+curlen, offset+len-curlen, char); | |
4282 | SvCUR_set(bigstr, offset+len); | |
4283 | } | |
79072805 | 4284 | |
69b47968 | 4285 | SvTAINT(bigstr); |
79072805 LW |
4286 | i = littlelen - len; |
4287 | if (i > 0) { /* string might grow */ | |
a0d0e21e | 4288 | big = SvGROW(bigstr, SvCUR(bigstr) + i + 1); |
79072805 LW |
4289 | mid = big + offset + len; |
4290 | midend = bigend = big + SvCUR(bigstr); | |
4291 | bigend += i; | |
4292 | *bigend = '\0'; | |
4293 | while (midend > mid) /* shove everything down */ | |
4294 | *--bigend = *--midend; | |
4295 | Move(little,big+offset,littlelen,char); | |
4296 | SvCUR(bigstr) += i; | |
4297 | SvSETMAGIC(bigstr); | |
4298 | return; | |
4299 | } | |
4300 | else if (i == 0) { | |
463ee0b2 | 4301 | Move(little,SvPVX(bigstr)+offset,len,char); |
79072805 LW |
4302 | SvSETMAGIC(bigstr); |
4303 | return; | |
4304 | } | |
4305 | ||
463ee0b2 | 4306 | big = SvPVX(bigstr); |
79072805 LW |
4307 | mid = big + offset; |
4308 | midend = mid + len; | |
4309 | bigend = big + SvCUR(bigstr); | |
4310 | ||
4311 | if (midend > bigend) | |
cea2e8a9 | 4312 | Perl_croak(aTHX_ "panic: sv_insert"); |
79072805 LW |
4313 | |
4314 | if (mid - big > bigend - midend) { /* faster to shorten from end */ | |
4315 | if (littlelen) { | |
4316 | Move(little, mid, littlelen,char); | |
4317 | mid += littlelen; | |
4318 | } | |
4319 | i = bigend - midend; | |
4320 | if (i > 0) { | |
4321 | Move(midend, mid, i,char); | |
4322 | mid += i; | |
4323 | } | |
4324 | *mid = '\0'; | |
4325 | SvCUR_set(bigstr, mid - big); | |
4326 | } | |
4327 | /*SUPPRESS 560*/ | |
155aba94 | 4328 | else if ((i = mid - big)) { /* faster from front */ |
79072805 LW |
4329 | midend -= littlelen; |
4330 | mid = midend; | |
4331 | sv_chop(bigstr,midend-i); | |
4332 | big += i; | |
4333 | while (i--) | |
4334 | *--midend = *--big; | |
4335 | if (littlelen) | |
4336 | Move(little, mid, littlelen,char); | |
4337 | } | |
4338 | else if (littlelen) { | |
4339 | midend -= littlelen; | |
4340 | sv_chop(bigstr,midend); | |
4341 | Move(little,midend,littlelen,char); | |
4342 | } | |
4343 | else { | |
4344 | sv_chop(bigstr,midend); | |
4345 | } | |
4346 | SvSETMAGIC(bigstr); | |
4347 | } | |
4348 | ||
c461cf8f JH |
4349 | /* |
4350 | =for apidoc sv_replace | |
4351 | ||
4352 | Make the first argument a copy of the second, then delete the original. | |
4353 | ||
4354 | =cut | |
4355 | */ | |
79072805 LW |
4356 | |
4357 | void | |
864dbfa3 | 4358 | Perl_sv_replace(pTHX_ register SV *sv, register SV *nsv) |
79072805 LW |
4359 | { |
4360 | U32 refcnt = SvREFCNT(sv); | |
2213622d | 4361 | SV_CHECK_THINKFIRST(sv); |
0453d815 PM |
4362 | if (SvREFCNT(nsv) != 1 && ckWARN_d(WARN_INTERNAL)) |
4363 | Perl_warner(aTHX_ WARN_INTERNAL, "Reference miscount in sv_replace()"); | |
93a17b20 | 4364 | if (SvMAGICAL(sv)) { |
a0d0e21e LW |
4365 | if (SvMAGICAL(nsv)) |
4366 | mg_free(nsv); | |
4367 | else | |
4368 | sv_upgrade(nsv, SVt_PVMG); | |
93a17b20 | 4369 | SvMAGIC(nsv) = SvMAGIC(sv); |
a0d0e21e | 4370 | SvFLAGS(nsv) |= SvMAGICAL(sv); |
93a17b20 LW |
4371 | SvMAGICAL_off(sv); |
4372 | SvMAGIC(sv) = 0; | |
4373 | } | |
79072805 LW |
4374 | SvREFCNT(sv) = 0; |
4375 | sv_clear(sv); | |
477f5d66 | 4376 | assert(!SvREFCNT(sv)); |
79072805 LW |
4377 | StructCopy(nsv,sv,SV); |
4378 | SvREFCNT(sv) = refcnt; | |
1edc1566 | 4379 | SvFLAGS(nsv) |= SVTYPEMASK; /* Mark as freed */ |
463ee0b2 | 4380 | del_SV(nsv); |
79072805 LW |
4381 | } |
4382 | ||
c461cf8f JH |
4383 | /* |
4384 | =for apidoc sv_clear | |
4385 | ||
4386 | Clear an SV, making it empty. Does not free the memory used by the SV | |
4387 | itself. | |
4388 | ||
4389 | =cut | |
4390 | */ | |
4391 | ||
79072805 | 4392 | void |
864dbfa3 | 4393 | Perl_sv_clear(pTHX_ register SV *sv) |
79072805 | 4394 | { |
ec12f114 | 4395 | HV* stash; |
79072805 LW |
4396 | assert(sv); |
4397 | assert(SvREFCNT(sv) == 0); | |
4398 | ||
ed6116ce | 4399 | if (SvOBJECT(sv)) { |
3280af22 | 4400 | if (PL_defstash) { /* Still have a symbol table? */ |
39644a26 | 4401 | dSP; |
32251b26 | 4402 | CV* destructor; |
837485b6 | 4403 | SV tmpref; |
a0d0e21e | 4404 | |
837485b6 GS |
4405 | Zero(&tmpref, 1, SV); |
4406 | sv_upgrade(&tmpref, SVt_RV); | |
4407 | SvROK_on(&tmpref); | |
4408 | SvREADONLY_on(&tmpref); /* DESTROY() could be naughty */ | |
4409 | SvREFCNT(&tmpref) = 1; | |
8ebc5c01 | 4410 | |
d460ef45 | 4411 | do { |
4e8e7886 | 4412 | stash = SvSTASH(sv); |
32251b26 | 4413 | destructor = StashHANDLER(stash,DESTROY); |
4e8e7886 GS |
4414 | if (destructor) { |
4415 | ENTER; | |
e788e7d3 | 4416 | PUSHSTACKi(PERLSI_DESTROY); |
837485b6 | 4417 | SvRV(&tmpref) = SvREFCNT_inc(sv); |
4e8e7886 GS |
4418 | EXTEND(SP, 2); |
4419 | PUSHMARK(SP); | |
837485b6 | 4420 | PUSHs(&tmpref); |
4e8e7886 | 4421 | PUTBACK; |
32251b26 | 4422 | call_sv((SV*)destructor, G_DISCARD|G_EVAL|G_KEEPERR); |
4e8e7886 | 4423 | SvREFCNT(sv)--; |
d3acc0f7 | 4424 | POPSTACK; |
3095d977 | 4425 | SPAGAIN; |
4e8e7886 GS |
4426 | LEAVE; |
4427 | } | |
4428 | } while (SvOBJECT(sv) && SvSTASH(sv) != stash); | |
8ebc5c01 | 4429 | |
837485b6 | 4430 | del_XRV(SvANY(&tmpref)); |
6f44e0a4 JP |
4431 | |
4432 | if (SvREFCNT(sv)) { | |
4433 | if (PL_in_clean_objs) | |
cea2e8a9 | 4434 | Perl_croak(aTHX_ "DESTROY created new reference to dead object '%s'", |
6f44e0a4 JP |
4435 | HvNAME(stash)); |
4436 | /* DESTROY gave object new lease on life */ | |
4437 | return; | |
4438 | } | |
a0d0e21e | 4439 | } |
4e8e7886 | 4440 | |
a0d0e21e | 4441 | if (SvOBJECT(sv)) { |
4e8e7886 | 4442 | SvREFCNT_dec(SvSTASH(sv)); /* possibly of changed persuasion */ |
a0d0e21e LW |
4443 | SvOBJECT_off(sv); /* Curse the object. */ |
4444 | if (SvTYPE(sv) != SVt_PVIO) | |
3280af22 | 4445 | --PL_sv_objcount; /* XXX Might want something more general */ |
a0d0e21e | 4446 | } |
463ee0b2 | 4447 | } |
c07a80fd | 4448 | if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) |
a0d0e21e | 4449 | mg_free(sv); |
ec12f114 | 4450 | stash = NULL; |
79072805 | 4451 | switch (SvTYPE(sv)) { |
8990e307 | 4452 | case SVt_PVIO: |
df0bd2f4 GS |
4453 | if (IoIFP(sv) && |
4454 | IoIFP(sv) != PerlIO_stdin() && | |
5f05dabc | 4455 | IoIFP(sv) != PerlIO_stdout() && |
4456 | IoIFP(sv) != PerlIO_stderr()) | |
93578b34 | 4457 | { |
f2b5be74 | 4458 | io_close((IO*)sv, FALSE); |
93578b34 | 4459 | } |
1d7c1841 | 4460 | if (IoDIRP(sv) && !(IoFLAGS(sv) & IOf_FAKE_DIRP)) |
1236053a | 4461 | PerlDir_close(IoDIRP(sv)); |
1d7c1841 | 4462 | IoDIRP(sv) = (DIR*)NULL; |
8990e307 LW |
4463 | Safefree(IoTOP_NAME(sv)); |
4464 | Safefree(IoFMT_NAME(sv)); | |
4465 | Safefree(IoBOTTOM_NAME(sv)); | |
a0d0e21e | 4466 | /* FALL THROUGH */ |
79072805 | 4467 | case SVt_PVBM: |
a0d0e21e | 4468 | goto freescalar; |
79072805 | 4469 | case SVt_PVCV: |
748a9306 | 4470 | case SVt_PVFM: |
85e6fe83 | 4471 | cv_undef((CV*)sv); |
a0d0e21e | 4472 | goto freescalar; |
79072805 | 4473 | case SVt_PVHV: |
85e6fe83 | 4474 | hv_undef((HV*)sv); |
a0d0e21e | 4475 | break; |
79072805 | 4476 | case SVt_PVAV: |
85e6fe83 | 4477 | av_undef((AV*)sv); |
a0d0e21e | 4478 | break; |
02270b4e GS |
4479 | case SVt_PVLV: |
4480 | SvREFCNT_dec(LvTARG(sv)); | |
4481 | goto freescalar; | |
a0d0e21e | 4482 | case SVt_PVGV: |
1edc1566 | 4483 | gp_free((GV*)sv); |
a0d0e21e | 4484 | Safefree(GvNAME(sv)); |
ec12f114 JPC |
4485 | /* cannot decrease stash refcount yet, as we might recursively delete |
4486 | ourselves when the refcnt drops to zero. Delay SvREFCNT_dec | |
4487 | of stash until current sv is completely gone. | |
4488 | -- JohnPC, 27 Mar 1998 */ | |
4489 | stash = GvSTASH(sv); | |
a0d0e21e | 4490 | /* FALL THROUGH */ |
79072805 | 4491 | case SVt_PVMG: |
79072805 LW |
4492 | case SVt_PVNV: |
4493 | case SVt_PVIV: | |
a0d0e21e LW |
4494 | freescalar: |
4495 | (void)SvOOK_off(sv); | |
79072805 LW |
4496 | /* FALL THROUGH */ |
4497 | case SVt_PV: | |
a0d0e21e | 4498 | case SVt_RV: |
810b8aa5 GS |
4499 | if (SvROK(sv)) { |
4500 | if (SvWEAKREF(sv)) | |
4501 | sv_del_backref(sv); | |
4502 | else | |
4503 | SvREFCNT_dec(SvRV(sv)); | |
4504 | } | |
1edc1566 | 4505 | else if (SvPVX(sv) && SvLEN(sv)) |
463ee0b2 | 4506 | Safefree(SvPVX(sv)); |
1c846c1f | 4507 | else if (SvPVX(sv) && SvREADONLY(sv) && SvFAKE(sv)) { |
c3654f1a | 4508 | unsharepvn(SvPVX(sv),SvUTF8(sv)?-SvCUR(sv):SvCUR(sv),SvUVX(sv)); |
1c846c1f NIS |
4509 | SvFAKE_off(sv); |
4510 | } | |
79072805 | 4511 | break; |
a0d0e21e | 4512 | /* |
79072805 | 4513 | case SVt_NV: |
79072805 | 4514 | case SVt_IV: |
79072805 LW |
4515 | case SVt_NULL: |
4516 | break; | |
a0d0e21e | 4517 | */ |
79072805 LW |
4518 | } |
4519 | ||
4520 | switch (SvTYPE(sv)) { | |
4521 | case SVt_NULL: | |
4522 | break; | |
79072805 LW |
4523 | case SVt_IV: |
4524 | del_XIV(SvANY(sv)); | |
4525 | break; | |
4526 | case SVt_NV: | |
4527 | del_XNV(SvANY(sv)); | |
4528 | break; | |
ed6116ce LW |
4529 | case SVt_RV: |
4530 | del_XRV(SvANY(sv)); | |
4531 | break; | |
79072805 LW |
4532 | case SVt_PV: |
4533 | del_XPV(SvANY(sv)); | |
4534 | break; | |
4535 | case SVt_PVIV: | |
4536 | del_XPVIV(SvANY(sv)); | |
4537 | break; | |
4538 | case SVt_PVNV: | |
4539 | del_XPVNV(SvANY(sv)); | |
4540 | break; | |
4541 | case SVt_PVMG: | |
4542 | del_XPVMG(SvANY(sv)); | |
4543 | break; | |
4544 | case SVt_PVLV: | |
4545 | del_XPVLV(SvANY(sv)); | |
4546 | break; | |
4547 | case SVt_PVAV: | |
4548 | del_XPVAV(SvANY(sv)); | |
4549 | break; | |
4550 | case SVt_PVHV: | |
4551 | del_XPVHV(SvANY(sv)); | |
4552 | break; | |
4553 | case SVt_PVCV: | |
4554 | del_XPVCV(SvANY(sv)); | |
4555 | break; | |
4556 | case SVt_PVGV: | |
4557 | del_XPVGV(SvANY(sv)); | |
ec12f114 JPC |
4558 | /* code duplication for increased performance. */ |
4559 | SvFLAGS(sv) &= SVf_BREAK; | |
4560 | SvFLAGS(sv) |= SVTYPEMASK; | |
4561 | /* decrease refcount of the stash that owns this GV, if any */ | |
4562 | if (stash) | |
4563 | SvREFCNT_dec(stash); | |
4564 | return; /* not break, SvFLAGS reset already happened */ | |
79072805 LW |
4565 | case SVt_PVBM: |
4566 | del_XPVBM(SvANY(sv)); | |
4567 | break; | |
4568 | case SVt_PVFM: | |
4569 | del_XPVFM(SvANY(sv)); | |
4570 | break; | |
8990e307 LW |
4571 | case SVt_PVIO: |
4572 | del_XPVIO(SvANY(sv)); | |
4573 | break; | |
79072805 | 4574 | } |
a0d0e21e | 4575 | SvFLAGS(sv) &= SVf_BREAK; |
8990e307 | 4576 | SvFLAGS(sv) |= SVTYPEMASK; |
79072805 LW |
4577 | } |
4578 | ||
4579 | SV * | |
864dbfa3 | 4580 | Perl_sv_newref(pTHX_ SV *sv) |
79072805 | 4581 | { |
463ee0b2 | 4582 | if (sv) |
dce16143 | 4583 | ATOMIC_INC(SvREFCNT(sv)); |
79072805 LW |
4584 | return sv; |
4585 | } | |
4586 | ||
c461cf8f JH |
4587 | /* |
4588 | =for apidoc sv_free | |
4589 | ||
4590 | Free the memory used by an SV. | |
4591 | ||
4592 | =cut | |
4593 | */ | |
4594 | ||
79072805 | 4595 | void |
864dbfa3 | 4596 | Perl_sv_free(pTHX_ SV *sv) |
79072805 | 4597 | { |
dce16143 MB |
4598 | int refcount_is_zero; |
4599 | ||
79072805 LW |
4600 | if (!sv) |
4601 | return; | |
a0d0e21e LW |
4602 | if (SvREFCNT(sv) == 0) { |
4603 | if (SvFLAGS(sv) & SVf_BREAK) | |
4604 | return; | |
3280af22 | 4605 | if (PL_in_clean_all) /* All is fair */ |
1edc1566 | 4606 | return; |
d689ffdd JP |
4607 | if (SvREADONLY(sv) && SvIMMORTAL(sv)) { |
4608 | /* make sure SvREFCNT(sv)==0 happens very seldom */ | |
4609 | SvREFCNT(sv) = (~(U32)0)/2; | |
4610 | return; | |
4611 | } | |
0453d815 PM |
4612 | if (ckWARN_d(WARN_INTERNAL)) |
4613 | Perl_warner(aTHX_ WARN_INTERNAL, "Attempt to free unreferenced scalar"); | |
79072805 LW |
4614 | return; |
4615 | } | |
dce16143 MB |
4616 | ATOMIC_DEC_AND_TEST(refcount_is_zero, SvREFCNT(sv)); |
4617 | if (!refcount_is_zero) | |
8990e307 | 4618 | return; |
463ee0b2 LW |
4619 | #ifdef DEBUGGING |
4620 | if (SvTEMP(sv)) { | |
0453d815 | 4621 | if (ckWARN_d(WARN_DEBUGGING)) |
f248d071 | 4622 | Perl_warner(aTHX_ WARN_DEBUGGING, |
1d7c1841 GS |
4623 | "Attempt to free temp prematurely: SV 0x%"UVxf, |
4624 | PTR2UV(sv)); | |
79072805 | 4625 | return; |
79072805 | 4626 | } |
463ee0b2 | 4627 | #endif |
d689ffdd JP |
4628 | if (SvREADONLY(sv) && SvIMMORTAL(sv)) { |
4629 | /* make sure SvREFCNT(sv)==0 happens very seldom */ | |
4630 | SvREFCNT(sv) = (~(U32)0)/2; | |
4631 | return; | |
4632 | } | |
79072805 | 4633 | sv_clear(sv); |
477f5d66 CS |
4634 | if (! SvREFCNT(sv)) |
4635 | del_SV(sv); | |
79072805 LW |
4636 | } |
4637 | ||
954c1994 GS |
4638 | /* |
4639 | =for apidoc sv_len | |
4640 | ||
4641 | Returns the length of the string in the SV. See also C<SvCUR>. | |
4642 | ||
4643 | =cut | |
4644 | */ | |
4645 | ||
79072805 | 4646 | STRLEN |
864dbfa3 | 4647 | Perl_sv_len(pTHX_ register SV *sv) |
79072805 | 4648 | { |
748a9306 | 4649 | char *junk; |
463ee0b2 | 4650 | STRLEN len; |
79072805 LW |
4651 | |
4652 | if (!sv) | |
4653 | return 0; | |
4654 | ||
8990e307 | 4655 | if (SvGMAGICAL(sv)) |
565764a8 | 4656 | len = mg_length(sv); |
8990e307 | 4657 | else |
748a9306 | 4658 | junk = SvPV(sv, len); |
463ee0b2 | 4659 | return len; |
79072805 LW |
4660 | } |
4661 | ||
c461cf8f JH |
4662 | /* |
4663 | =for apidoc sv_len_utf8 | |
4664 | ||
4665 | Returns the number of characters in the string in an SV, counting wide | |
4666 | UTF8 bytes as a single character. | |
4667 | ||
4668 | =cut | |
4669 | */ | |
4670 | ||
a0ed51b3 | 4671 | STRLEN |
864dbfa3 | 4672 | Perl_sv_len_utf8(pTHX_ register SV *sv) |
a0ed51b3 | 4673 | { |
a0ed51b3 LW |
4674 | if (!sv) |
4675 | return 0; | |
4676 | ||
a0ed51b3 | 4677 | if (SvGMAGICAL(sv)) |
b76347f2 | 4678 | return mg_length(sv); |
a0ed51b3 | 4679 | else |
b76347f2 JH |
4680 | { |
4681 | STRLEN len; | |
4682 | U8 *s = (U8*)SvPV(sv, len); | |
4683 | ||
d6efbbad | 4684 | return Perl_utf8_length(aTHX_ s, s + len); |
a0ed51b3 | 4685 | } |
a0ed51b3 LW |
4686 | } |
4687 | ||
4688 | void | |
864dbfa3 | 4689 | Perl_sv_pos_u2b(pTHX_ register SV *sv, I32* offsetp, I32* lenp) |
a0ed51b3 | 4690 | { |
dfe13c55 GS |
4691 | U8 *start; |
4692 | U8 *s; | |
4693 | U8 *send; | |
a0ed51b3 LW |
4694 | I32 uoffset = *offsetp; |
4695 | STRLEN len; | |
4696 | ||
4697 | if (!sv) | |
4698 | return; | |
4699 | ||
dfe13c55 | 4700 | start = s = (U8*)SvPV(sv, len); |
a0ed51b3 LW |
4701 | send = s + len; |
4702 | while (s < send && uoffset--) | |
4703 | s += UTF8SKIP(s); | |
bb40f870 GA |
4704 | if (s >= send) |
4705 | s = send; | |
a0ed51b3 LW |
4706 | *offsetp = s - start; |
4707 | if (lenp) { | |
4708 | I32 ulen = *lenp; | |
4709 | start = s; | |
4710 | while (s < send && ulen--) | |
4711 | s += UTF8SKIP(s); | |
bb40f870 GA |
4712 | if (s >= send) |
4713 | s = send; | |
a0ed51b3 LW |
4714 | *lenp = s - start; |
4715 | } | |
4716 | return; | |
4717 | } | |
4718 | ||
4719 | void | |
864dbfa3 | 4720 | Perl_sv_pos_b2u(pTHX_ register SV *sv, I32* offsetp) |
a0ed51b3 | 4721 | { |
dfe13c55 GS |
4722 | U8 *s; |
4723 | U8 *send; | |
a0ed51b3 LW |
4724 | STRLEN len; |
4725 | ||
4726 | if (!sv) | |
4727 | return; | |
4728 | ||
dfe13c55 | 4729 | s = (U8*)SvPV(sv, len); |
a0ed51b3 | 4730 | if (len < *offsetp) |
a0dbb045 | 4731 | Perl_croak(aTHX_ "panic: sv_pos_b2u: bad byte offset"); |
a0ed51b3 LW |
4732 | send = s + *offsetp; |
4733 | len = 0; | |
4734 | while (s < send) { | |
a0dbb045 | 4735 | STRLEN n; |
9041c2e3 NIS |
4736 | /* We can use low level directly here as we are not looking at the values */ |
4737 | if (utf8n_to_uvuni(s, UTF8SKIP(s), &n, 0)) { | |
a0dbb045 JH |
4738 | s += n; |
4739 | len++; | |
4740 | } | |
4741 | else | |
4742 | break; | |
a0ed51b3 LW |
4743 | } |
4744 | *offsetp = len; | |
4745 | return; | |
4746 | } | |
4747 | ||
954c1994 GS |
4748 | /* |
4749 | =for apidoc sv_eq | |
4750 | ||
4751 | Returns a boolean indicating whether the strings in the two SVs are | |
4752 | identical. | |
4753 | ||
4754 | =cut | |
4755 | */ | |
4756 | ||
79072805 | 4757 | I32 |
e01b9e88 | 4758 | Perl_sv_eq(pTHX_ register SV *sv1, register SV *sv2) |
79072805 LW |
4759 | { |
4760 | char *pv1; | |
463ee0b2 | 4761 | STRLEN cur1; |
79072805 | 4762 | char *pv2; |
463ee0b2 | 4763 | STRLEN cur2; |
e01b9e88 | 4764 | I32 eq = 0; |
db42d148 | 4765 | char *tpv = Nullch; |
79072805 | 4766 | |
e01b9e88 | 4767 | if (!sv1) { |
79072805 LW |
4768 | pv1 = ""; |
4769 | cur1 = 0; | |
4770 | } | |
463ee0b2 | 4771 | else |
e01b9e88 | 4772 | pv1 = SvPV(sv1, cur1); |
79072805 | 4773 | |
e01b9e88 SC |
4774 | if (!sv2){ |
4775 | pv2 = ""; | |
4776 | cur2 = 0; | |
92d29cee | 4777 | } |
e01b9e88 SC |
4778 | else |
4779 | pv2 = SvPV(sv2, cur2); | |
79072805 | 4780 | |
e01b9e88 | 4781 | /* do not utf8ize the comparands as a side-effect */ |
7bbb0251 | 4782 | if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTE) { |
f9a63242 | 4783 | bool is_utf8 = TRUE; |
db42d148 | 4784 | /* UTF-8ness differs */ |
1aa99e6b IH |
4785 | if (PL_hints & HINT_UTF8_DISTINCT) |
4786 | return FALSE; | |
4787 | ||
e01b9e88 | 4788 | if (SvUTF8(sv1)) { |
db42d148 | 4789 | /* sv1 is the UTF-8 one , If is equal it must be downgrade-able */ |
f34ff0a8 | 4790 | char *pv = (char*)bytes_from_utf8((U8*)pv1, &cur1, &is_utf8); |
db42d148 NIS |
4791 | if (pv != pv1) |
4792 | pv1 = tpv = pv; | |
e01b9e88 SC |
4793 | } |
4794 | else { | |
db42d148 | 4795 | /* sv2 is the UTF-8 one , If is equal it must be downgrade-able */ |
f34ff0a8 | 4796 | char *pv = (char *)bytes_from_utf8((U8*)pv2, &cur2, &is_utf8); |
db42d148 NIS |
4797 | if (pv != pv2) |
4798 | pv2 = tpv = pv; | |
4799 | } | |
4800 | if (is_utf8) { | |
4801 | /* Downgrade not possible - cannot be eq */ | |
4802 | return FALSE; | |
e01b9e88 SC |
4803 | } |
4804 | } | |
79072805 | 4805 | |
e01b9e88 SC |
4806 | if (cur1 == cur2) |
4807 | eq = memEQ(pv1, pv2, cur1); | |
4808 | ||
db42d148 NIS |
4809 | if (tpv != Nullch) |
4810 | Safefree(tpv); | |
e01b9e88 SC |
4811 | |
4812 | return eq; | |
79072805 LW |
4813 | } |
4814 | ||
954c1994 GS |
4815 | /* |
4816 | =for apidoc sv_cmp | |
4817 | ||
4818 | Compares the strings in two SVs. Returns -1, 0, or 1 indicating whether the | |
4819 | string in C<sv1> is less than, equal to, or greater than the string in | |
4820 | C<sv2>. | |
4821 | ||
4822 | =cut | |
4823 | */ | |
4824 | ||
79072805 | 4825 | I32 |
e01b9e88 | 4826 | Perl_sv_cmp(pTHX_ register SV *sv1, register SV *sv2) |
79072805 | 4827 | { |
560a288e GS |
4828 | STRLEN cur1, cur2; |
4829 | char *pv1, *pv2; | |
1c846c1f | 4830 | I32 cmp; |
e01b9e88 SC |
4831 | bool pv1tmp = FALSE; |
4832 | bool pv2tmp = FALSE; | |
560a288e | 4833 | |
e01b9e88 SC |
4834 | if (!sv1) { |
4835 | pv1 = ""; | |
560a288e GS |
4836 | cur1 = 0; |
4837 | } | |
e01b9e88 SC |
4838 | else |
4839 | pv1 = SvPV(sv1, cur1); | |
560a288e | 4840 | |
e01b9e88 SC |
4841 | if (!sv2){ |
4842 | pv2 = ""; | |
560a288e GS |
4843 | cur2 = 0; |
4844 | } | |
e01b9e88 SC |
4845 | else |
4846 | pv2 = SvPV(sv2, cur2); | |
79072805 | 4847 | |
e01b9e88 SC |
4848 | /* do not utf8ize the comparands as a side-effect */ |
4849 | if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTE) { | |
1aa99e6b IH |
4850 | if (PL_hints & HINT_UTF8_DISTINCT) |
4851 | return SvUTF8(sv1) ? 1 : -1; | |
4852 | ||
e01b9e88 SC |
4853 | if (SvUTF8(sv1)) { |
4854 | pv2 = (char*)bytes_to_utf8((U8*)pv2, &cur2); | |
4855 | pv2tmp = TRUE; | |
4856 | } | |
4857 | else { | |
4858 | pv1 = (char*)bytes_to_utf8((U8*)pv1, &cur1); | |
4859 | pv1tmp = TRUE; | |
4860 | } | |
4861 | } | |
79072805 | 4862 | |
e01b9e88 SC |
4863 | if (!cur1) { |
4864 | cmp = cur2 ? -1 : 0; | |
4865 | } else if (!cur2) { | |
4866 | cmp = 1; | |
4867 | } else { | |
4868 | I32 retval = memcmp((void*)pv1, (void*)pv2, cur1 < cur2 ? cur1 : cur2); | |
4869 | ||
4870 | if (retval) { | |
4871 | cmp = retval < 0 ? -1 : 1; | |
4872 | } else if (cur1 == cur2) { | |
4873 | cmp = 0; | |
4874 | } else { | |
4875 | cmp = cur1 < cur2 ? -1 : 1; | |
4876 | } | |
4877 | } | |
16660edb | 4878 | |
e01b9e88 SC |
4879 | if (pv1tmp) |
4880 | Safefree(pv1); | |
4881 | if (pv2tmp) | |
4882 | Safefree(pv2); | |
16660edb | 4883 | |
e01b9e88 | 4884 | return cmp; |
bbce6d69 | 4885 | } |
16660edb | 4886 | |
c461cf8f JH |
4887 | /* |
4888 | =for apidoc sv_cmp_locale | |
4889 | ||
4890 | Compares the strings in two SVs in a locale-aware manner. See | |
4891 | L</sv_cmp_locale> | |
4892 | ||
4893 | =cut | |
4894 | */ | |
4895 | ||
bbce6d69 | 4896 | I32 |
864dbfa3 | 4897 | Perl_sv_cmp_locale(pTHX_ register SV *sv1, register SV *sv2) |
bbce6d69 | 4898 | { |
36477c24 | 4899 | #ifdef USE_LOCALE_COLLATE |
16660edb | 4900 | |
bbce6d69 | 4901 | char *pv1, *pv2; |
4902 | STRLEN len1, len2; | |
4903 | I32 retval; | |
16660edb | 4904 | |
3280af22 | 4905 | if (PL_collation_standard) |
bbce6d69 | 4906 | goto raw_compare; |
16660edb | 4907 | |
bbce6d69 | 4908 | len1 = 0; |
8ac85365 | 4909 | pv1 = sv1 ? sv_collxfrm(sv1, &len1) : (char *) NULL; |
bbce6d69 | 4910 | len2 = 0; |
8ac85365 | 4911 | pv2 = sv2 ? sv_collxfrm(sv2, &len2) : (char *) NULL; |
16660edb | 4912 | |
bbce6d69 | 4913 | if (!pv1 || !len1) { |
4914 | if (pv2 && len2) | |
4915 | return -1; | |
4916 | else | |
4917 | goto raw_compare; | |
4918 | } | |
4919 | else { | |
4920 | if (!pv2 || !len2) | |
4921 | return 1; | |
4922 | } | |
16660edb | 4923 | |
bbce6d69 | 4924 | retval = memcmp((void*)pv1, (void*)pv2, len1 < len2 ? len1 : len2); |
16660edb | 4925 | |
bbce6d69 | 4926 | if (retval) |
16660edb | 4927 | return retval < 0 ? -1 : 1; |
4928 | ||
bbce6d69 | 4929 | /* |
4930 | * When the result of collation is equality, that doesn't mean | |
4931 | * that there are no differences -- some locales exclude some | |
4932 | * characters from consideration. So to avoid false equalities, | |
4933 | * we use the raw string as a tiebreaker. | |
4934 | */ | |
16660edb | 4935 | |
bbce6d69 | 4936 | raw_compare: |
4937 | /* FALL THROUGH */ | |
16660edb | 4938 | |
36477c24 | 4939 | #endif /* USE_LOCALE_COLLATE */ |
16660edb | 4940 | |
bbce6d69 | 4941 | return sv_cmp(sv1, sv2); |
4942 | } | |
79072805 | 4943 | |
36477c24 | 4944 | #ifdef USE_LOCALE_COLLATE |
7a4c00b4 | 4945 | /* |
4946 | * Any scalar variable may carry an 'o' magic that contains the | |
4947 | * scalar data of the variable transformed to such a format that | |
4948 | * a normal memory comparison can be used to compare the data | |
4949 | * according to the locale settings. | |
4950 | */ | |
bbce6d69 | 4951 | char * |
864dbfa3 | 4952 | Perl_sv_collxfrm(pTHX_ SV *sv, STRLEN *nxp) |
bbce6d69 | 4953 | { |
7a4c00b4 | 4954 | MAGIC *mg; |
16660edb | 4955 | |
8ac85365 | 4956 | mg = SvMAGICAL(sv) ? mg_find(sv, 'o') : (MAGIC *) NULL; |
3280af22 | 4957 | if (!mg || !mg->mg_ptr || *(U32*)mg->mg_ptr != PL_collation_ix) { |
bbce6d69 | 4958 | char *s, *xf; |
4959 | STRLEN len, xlen; | |
4960 | ||
7a4c00b4 | 4961 | if (mg) |
4962 | Safefree(mg->mg_ptr); | |
bbce6d69 | 4963 | s = SvPV(sv, len); |
4964 | if ((xf = mem_collxfrm(s, len, &xlen))) { | |
ff0cee69 | 4965 | if (SvREADONLY(sv)) { |
4966 | SAVEFREEPV(xf); | |
4967 | *nxp = xlen; | |
3280af22 | 4968 | return xf + sizeof(PL_collation_ix); |
ff0cee69 | 4969 | } |
7a4c00b4 | 4970 | if (! mg) { |
4971 | sv_magic(sv, 0, 'o', 0, 0); | |
4972 | mg = mg_find(sv, 'o'); | |
4973 | assert(mg); | |
bbce6d69 | 4974 | } |
7a4c00b4 | 4975 | mg->mg_ptr = xf; |
565764a8 | 4976 | mg->mg_len = xlen; |
7a4c00b4 | 4977 | } |
4978 | else { | |
ff0cee69 | 4979 | if (mg) { |
4980 | mg->mg_ptr = NULL; | |
565764a8 | 4981 | mg->mg_len = -1; |
ff0cee69 | 4982 | } |
bbce6d69 | 4983 | } |
4984 | } | |
7a4c00b4 | 4985 | if (mg && mg->mg_ptr) { |
565764a8 | 4986 | *nxp = mg->mg_len; |
3280af22 | 4987 | return mg->mg_ptr + sizeof(PL_collation_ix); |
bbce6d69 | 4988 | } |
4989 | else { | |
4990 | *nxp = 0; | |
4991 | return NULL; | |
16660edb | 4992 | } |
79072805 LW |
4993 | } |
4994 | ||
36477c24 | 4995 | #endif /* USE_LOCALE_COLLATE */ |
bbce6d69 | 4996 | |
c461cf8f JH |
4997 | /* |
4998 | =for apidoc sv_gets | |
4999 | ||
5000 | Get a line from the filehandle and store it into the SV, optionally | |
5001 | appending to the currently-stored string. | |
5002 | ||
5003 | =cut | |
5004 | */ | |
5005 | ||
79072805 | 5006 | char * |
864dbfa3 | 5007 | Perl_sv_gets(pTHX_ register SV *sv, register PerlIO *fp, I32 append) |
79072805 | 5008 | { |
c07a80fd | 5009 | char *rsptr; |
5010 | STRLEN rslen; | |
5011 | register STDCHAR rslast; | |
5012 | register STDCHAR *bp; | |
5013 | register I32 cnt; | |
5014 | I32 i; | |
5015 | ||
2213622d | 5016 | SV_CHECK_THINKFIRST(sv); |
6fc92669 | 5017 | (void)SvUPGRADE(sv, SVt_PV); |
99491443 | 5018 | |
ff68c719 | 5019 | SvSCREAM_off(sv); |
c07a80fd | 5020 | |
3280af22 | 5021 | if (RsSNARF(PL_rs)) { |
c07a80fd | 5022 | rsptr = NULL; |
5023 | rslen = 0; | |
5024 | } | |
3280af22 | 5025 | else if (RsRECORD(PL_rs)) { |
5b2b9c68 HM |
5026 | I32 recsize, bytesread; |
5027 | char *buffer; | |
5028 | ||
5029 | /* Grab the size of the record we're getting */ | |
3280af22 | 5030 | recsize = SvIV(SvRV(PL_rs)); |
5b2b9c68 | 5031 | (void)SvPOK_only(sv); /* Validate pointer */ |
e670df4e | 5032 | buffer = SvGROW(sv, recsize + 1); |
5b2b9c68 HM |
5033 | /* Go yank in */ |
5034 | #ifdef VMS | |
5035 | /* VMS wants read instead of fread, because fread doesn't respect */ | |
5036 | /* RMS record boundaries. This is not necessarily a good thing to be */ | |
5037 | /* doing, but we've got no other real choice */ | |
5038 | bytesread = PerlLIO_read(PerlIO_fileno(fp), buffer, recsize); | |
5039 | #else | |
5040 | bytesread = PerlIO_read(fp, buffer, recsize); | |
5041 | #endif | |
5042 | SvCUR_set(sv, bytesread); | |
e670df4e | 5043 | buffer[bytesread] = '\0'; |
7d59b7e4 NIS |
5044 | if (PerlIO_isutf8(fp)) |
5045 | SvUTF8_on(sv); | |
5046 | else | |
5047 | SvUTF8_off(sv); | |
5b2b9c68 HM |
5048 | return(SvCUR(sv) ? SvPVX(sv) : Nullch); |
5049 | } | |
3280af22 | 5050 | else if (RsPARA(PL_rs)) { |
c07a80fd | 5051 | rsptr = "\n\n"; |
5052 | rslen = 2; | |
5053 | } | |
7d59b7e4 NIS |
5054 | else { |
5055 | /* Get $/ i.e. PL_rs into same encoding as stream wants */ | |
5056 | if (PerlIO_isutf8(fp)) { | |
5057 | rsptr = SvPVutf8(PL_rs, rslen); | |
5058 | } | |
5059 | else { | |
5060 | if (SvUTF8(PL_rs)) { | |
5061 | if (!sv_utf8_downgrade(PL_rs, TRUE)) { | |
5062 | Perl_croak(aTHX_ "Wide character in $/"); | |
5063 | } | |
5064 | } | |
5065 | rsptr = SvPV(PL_rs, rslen); | |
5066 | } | |
5067 | } | |
5068 | ||
c07a80fd | 5069 | rslast = rslen ? rsptr[rslen - 1] : '\0'; |
5070 | ||
3280af22 | 5071 | if (RsPARA(PL_rs)) { /* have to do this both before and after */ |
79072805 | 5072 | do { /* to make sure file boundaries work right */ |
760ac839 | 5073 | if (PerlIO_eof(fp)) |
a0d0e21e | 5074 | return 0; |
760ac839 | 5075 | i = PerlIO_getc(fp); |
79072805 | 5076 | if (i != '\n') { |
a0d0e21e LW |
5077 | if (i == -1) |
5078 | return 0; | |
760ac839 | 5079 | PerlIO_ungetc(fp,i); |
79072805 LW |
5080 | break; |
5081 | } | |
5082 | } while (i != EOF); | |
5083 | } | |
c07a80fd | 5084 | |
760ac839 LW |
5085 | /* See if we know enough about I/O mechanism to cheat it ! */ |
5086 | ||
5087 | /* This used to be #ifdef test - it is made run-time test for ease | |
1c846c1f | 5088 | of abstracting out stdio interface. One call should be cheap |
760ac839 LW |
5089 | enough here - and may even be a macro allowing compile |
5090 | time optimization. | |
5091 | */ | |
5092 | ||
5093 | if (PerlIO_fast_gets(fp)) { | |
5094 | ||
5095 | /* | |
5096 | * We're going to steal some values from the stdio struct | |
5097 | * and put EVERYTHING in the innermost loop into registers. | |
5098 | */ | |
5099 | register STDCHAR *ptr; | |
5100 | STRLEN bpx; | |
5101 | I32 shortbuffered; | |
5102 | ||
16660edb | 5103 | #if defined(VMS) && defined(PERLIO_IS_STDIO) |
5104 | /* An ungetc()d char is handled separately from the regular | |
5105 | * buffer, so we getc() it back out and stuff it in the buffer. | |
5106 | */ | |
5107 | i = PerlIO_getc(fp); | |
5108 | if (i == EOF) return 0; | |
5109 | *(--((*fp)->_ptr)) = (unsigned char) i; | |
5110 | (*fp)->_cnt++; | |
5111 | #endif | |
c07a80fd | 5112 | |
c2960299 | 5113 | /* Here is some breathtakingly efficient cheating */ |
c07a80fd | 5114 | |
a20bf0c3 | 5115 | cnt = PerlIO_get_cnt(fp); /* get count into register */ |
a0d0e21e | 5116 | (void)SvPOK_only(sv); /* validate pointer */ |
79072805 LW |
5117 | if (SvLEN(sv) - append <= cnt + 1) { /* make sure we have the room */ |
5118 | if (cnt > 80 && SvLEN(sv) > append) { | |
5119 | shortbuffered = cnt - SvLEN(sv) + append + 1; | |
5120 | cnt -= shortbuffered; | |
5121 | } | |
5122 | else { | |
5123 | shortbuffered = 0; | |
bbce6d69 | 5124 | /* remember that cnt can be negative */ |
5125 | SvGROW(sv, append + (cnt <= 0 ? 2 : (cnt + 1))); | |
79072805 LW |
5126 | } |
5127 | } | |
5128 | else | |
5129 | shortbuffered = 0; | |
c07a80fd | 5130 | bp = (STDCHAR*)SvPVX(sv) + append; /* move these two too to registers */ |
a20bf0c3 | 5131 | ptr = (STDCHAR*)PerlIO_get_ptr(fp); |
16660edb | 5132 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5133 | "Screamer: entering, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt)); |
16660edb | 5134 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5135 | "Screamer: entering: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n", |
1c846c1f | 5136 | PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp), |
1d7c1841 | 5137 | PTR2UV(PerlIO_has_base(fp) ? PerlIO_get_base(fp) : 0))); |
79072805 LW |
5138 | for (;;) { |
5139 | screamer: | |
93a17b20 | 5140 | if (cnt > 0) { |
c07a80fd | 5141 | if (rslen) { |
760ac839 LW |
5142 | while (cnt > 0) { /* this | eat */ |
5143 | cnt--; | |
c07a80fd | 5144 | if ((*bp++ = *ptr++) == rslast) /* really | dust */ |
5145 | goto thats_all_folks; /* screams | sed :-) */ | |
5146 | } | |
5147 | } | |
5148 | else { | |
1c846c1f NIS |
5149 | Copy(ptr, bp, cnt, char); /* this | eat */ |
5150 | bp += cnt; /* screams | dust */ | |
c07a80fd | 5151 | ptr += cnt; /* louder | sed :-) */ |
a5f75d66 | 5152 | cnt = 0; |
93a17b20 | 5153 | } |
79072805 LW |
5154 | } |
5155 | ||
748a9306 | 5156 | if (shortbuffered) { /* oh well, must extend */ |
79072805 LW |
5157 | cnt = shortbuffered; |
5158 | shortbuffered = 0; | |
c07a80fd | 5159 | bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */ |
79072805 LW |
5160 | SvCUR_set(sv, bpx); |
5161 | SvGROW(sv, SvLEN(sv) + append + cnt + 2); | |
c07a80fd | 5162 | bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */ |
79072805 LW |
5163 | continue; |
5164 | } | |
5165 | ||
16660edb | 5166 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 GS |
5167 | "Screamer: going to getc, ptr=%"UVuf", cnt=%ld\n", |
5168 | PTR2UV(ptr),(long)cnt)); | |
a20bf0c3 | 5169 | PerlIO_set_ptrcnt(fp, ptr, cnt); /* deregisterize cnt and ptr */ |
16660edb | 5170 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5171 | "Screamer: pre: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n", |
1c846c1f | 5172 | PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp), |
1d7c1841 | 5173 | PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0))); |
1c846c1f | 5174 | /* This used to call 'filbuf' in stdio form, but as that behaves like |
774d564b | 5175 | getc when cnt <= 0 we use PerlIO_getc here to avoid introducing |
5176 | another abstraction. */ | |
760ac839 | 5177 | i = PerlIO_getc(fp); /* get more characters */ |
16660edb | 5178 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5179 | "Screamer: post: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n", |
1c846c1f | 5180 | PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp), |
1d7c1841 | 5181 | PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0))); |
a20bf0c3 JH |
5182 | cnt = PerlIO_get_cnt(fp); |
5183 | ptr = (STDCHAR*)PerlIO_get_ptr(fp); /* reregisterize cnt and ptr */ | |
16660edb | 5184 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5185 | "Screamer: after getc, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt)); |
79072805 | 5186 | |
748a9306 LW |
5187 | if (i == EOF) /* all done for ever? */ |
5188 | goto thats_really_all_folks; | |
5189 | ||
c07a80fd | 5190 | bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */ |
79072805 LW |
5191 | SvCUR_set(sv, bpx); |
5192 | SvGROW(sv, bpx + cnt + 2); | |
c07a80fd | 5193 | bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */ |
5194 | ||
760ac839 | 5195 | *bp++ = i; /* store character from PerlIO_getc */ |
79072805 | 5196 | |
c07a80fd | 5197 | if (rslen && (STDCHAR)i == rslast) /* all done for now? */ |
79072805 | 5198 | goto thats_all_folks; |
79072805 LW |
5199 | } |
5200 | ||
5201 | thats_all_folks: | |
c07a80fd | 5202 | if ((rslen > 1 && (bp - (STDCHAR*)SvPVX(sv) < rslen)) || |
36477c24 | 5203 | memNE((char*)bp - rslen, rsptr, rslen)) |
760ac839 | 5204 | goto screamer; /* go back to the fray */ |
79072805 LW |
5205 | thats_really_all_folks: |
5206 | if (shortbuffered) | |
5207 | cnt += shortbuffered; | |
16660edb | 5208 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5209 | "Screamer: quitting, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt)); |
a20bf0c3 | 5210 | PerlIO_set_ptrcnt(fp, ptr, cnt); /* put these back or we're in trouble */ |
16660edb | 5211 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5212 | "Screamer: end: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n", |
1c846c1f | 5213 | PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp), |
1d7c1841 | 5214 | PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0))); |
79072805 | 5215 | *bp = '\0'; |
760ac839 | 5216 | SvCUR_set(sv, bp - (STDCHAR*)SvPVX(sv)); /* set length */ |
16660edb | 5217 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
fb73857a | 5218 | "Screamer: done, len=%ld, string=|%.*s|\n", |
5219 | (long)SvCUR(sv),(int)SvCUR(sv),SvPVX(sv))); | |
760ac839 LW |
5220 | } |
5221 | else | |
79072805 | 5222 | { |
4d2c4e07 | 5223 | #ifndef EPOC |
760ac839 | 5224 | /*The big, slow, and stupid way */ |
c07a80fd | 5225 | STDCHAR buf[8192]; |
4d2c4e07 OF |
5226 | #else |
5227 | /* Need to work around EPOC SDK features */ | |
5228 | /* On WINS: MS VC5 generates calls to _chkstk, */ | |
5229 | /* if a `large' stack frame is allocated */ | |
5230 | /* gcc on MARM does not generate calls like these */ | |
5231 | STDCHAR buf[1024]; | |
5232 | #endif | |
79072805 | 5233 | |
760ac839 | 5234 | screamer2: |
c07a80fd | 5235 | if (rslen) { |
760ac839 LW |
5236 | register STDCHAR *bpe = buf + sizeof(buf); |
5237 | bp = buf; | |
5238 | while ((i = PerlIO_getc(fp)) != EOF && (*bp++ = i) != rslast && bp < bpe) | |
5239 | ; /* keep reading */ | |
5240 | cnt = bp - buf; | |
c07a80fd | 5241 | } |
5242 | else { | |
760ac839 | 5243 | cnt = PerlIO_read(fp,(char*)buf, sizeof(buf)); |
16660edb | 5244 | /* Accomodate broken VAXC compiler, which applies U8 cast to |
5245 | * both args of ?: operator, causing EOF to change into 255 | |
5246 | */ | |
5247 | if (cnt) { i = (U8)buf[cnt - 1]; } else { i = EOF; } | |
c07a80fd | 5248 | } |
79072805 LW |
5249 | |
5250 | if (append) | |
760ac839 | 5251 | sv_catpvn(sv, (char *) buf, cnt); |
79072805 | 5252 | else |
760ac839 | 5253 | sv_setpvn(sv, (char *) buf, cnt); |
c07a80fd | 5254 | |
5255 | if (i != EOF && /* joy */ | |
5256 | (!rslen || | |
5257 | SvCUR(sv) < rslen || | |
36477c24 | 5258 | memNE(SvPVX(sv) + SvCUR(sv) - rslen, rsptr, rslen))) |
79072805 LW |
5259 | { |
5260 | append = -1; | |
63e4d877 CS |
5261 | /* |
5262 | * If we're reading from a TTY and we get a short read, | |
5263 | * indicating that the user hit his EOF character, we need | |
5264 | * to notice it now, because if we try to read from the TTY | |
5265 | * again, the EOF condition will disappear. | |
5266 | * | |
5267 | * The comparison of cnt to sizeof(buf) is an optimization | |
5268 | * that prevents unnecessary calls to feof(). | |
5269 | * | |
5270 | * - jik 9/25/96 | |
5271 | */ | |
5272 | if (!(cnt < sizeof(buf) && PerlIO_eof(fp))) | |
5273 | goto screamer2; | |
79072805 LW |
5274 | } |
5275 | } | |
5276 | ||
1c846c1f | 5277 | if (RsPARA(PL_rs)) { /* have to do this both before and after */ |
c07a80fd | 5278 | while (i != EOF) { /* to make sure file boundaries work right */ |
760ac839 | 5279 | i = PerlIO_getc(fp); |
79072805 | 5280 | if (i != '\n') { |
760ac839 | 5281 | PerlIO_ungetc(fp,i); |
79072805 LW |
5282 | break; |
5283 | } | |
5284 | } | |
5285 | } | |
c07a80fd | 5286 | |
7d59b7e4 NIS |
5287 | if (PerlIO_isutf8(fp)) |
5288 | SvUTF8_on(sv); | |
5289 | else | |
5290 | SvUTF8_off(sv); | |
5291 | ||
c07a80fd | 5292 | return (SvCUR(sv) - append) ? SvPVX(sv) : Nullch; |
79072805 LW |
5293 | } |
5294 | ||
760ac839 | 5295 | |
954c1994 GS |
5296 | /* |
5297 | =for apidoc sv_inc | |
5298 | ||
5299 | Auto-increment of the value in the SV. | |
5300 | ||
5301 | =cut | |
5302 | */ | |
5303 | ||
79072805 | 5304 | void |
864dbfa3 | 5305 | Perl_sv_inc(pTHX_ register SV *sv) |
79072805 LW |
5306 | { |
5307 | register char *d; | |
463ee0b2 | 5308 | int flags; |
79072805 LW |
5309 | |
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,inc)) |
5322 | return; | |
56431972 | 5323 | i = PTR2IV(SvRV(sv)); |
b5be31e9 SM |
5324 | sv_unref(sv); |
5325 | sv_setiv(sv, i); | |
a0d0e21e | 5326 | } |
ed6116ce | 5327 | } |
8990e307 | 5328 | flags = SvFLAGS(sv); |
28e5dec8 JH |
5329 | if ((flags & (SVp_NOK|SVp_IOK)) == SVp_NOK) { |
5330 | /* It's (privately or publicly) a float, but not tested as an | |
5331 | integer, so test it to see. */ | |
d460ef45 | 5332 | (void) SvIV(sv); |
28e5dec8 JH |
5333 | flags = SvFLAGS(sv); |
5334 | } | |
5335 | if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) { | |
5336 | /* It's publicly an integer, or privately an integer-not-float */ | |
5337 | oops_its_int: | |
25da4f38 IZ |
5338 | if (SvIsUV(sv)) { |
5339 | if (SvUVX(sv) == UV_MAX) | |
65202027 | 5340 | sv_setnv(sv, (NV)UV_MAX + 1.0); |
25da4f38 IZ |
5341 | else |
5342 | (void)SvIOK_only_UV(sv); | |
5343 | ++SvUVX(sv); | |
5344 | } else { | |
5345 | if (SvIVX(sv) == IV_MAX) | |
28e5dec8 | 5346 | sv_setuv(sv, (UV)IV_MAX + 1); |
25da4f38 IZ |
5347 | else { |
5348 | (void)SvIOK_only(sv); | |
5349 | ++SvIVX(sv); | |
1c846c1f | 5350 | } |
55497cff | 5351 | } |
79072805 LW |
5352 | return; |
5353 | } | |
28e5dec8 JH |
5354 | if (flags & SVp_NOK) { |
5355 | (void)SvNOK_only(sv); | |
5356 | SvNVX(sv) += 1.0; | |
5357 | return; | |
5358 | } | |
5359 | ||
8990e307 | 5360 | if (!(flags & SVp_POK) || !*SvPVX(sv)) { |
28e5dec8 JH |
5361 | if ((flags & SVTYPEMASK) < SVt_PVIV) |
5362 | sv_upgrade(sv, SVt_IV); | |
5363 | (void)SvIOK_only(sv); | |
5364 | SvIVX(sv) = 1; | |
79072805 LW |
5365 | return; |
5366 | } | |
463ee0b2 | 5367 | d = SvPVX(sv); |
79072805 LW |
5368 | while (isALPHA(*d)) d++; |
5369 | while (isDIGIT(*d)) d++; | |
5370 | if (*d) { | |
28e5dec8 JH |
5371 | #ifdef PERL_PRESERVE_IVUV |
5372 | /* Got to punt this an an integer if needs be, but we don't issue | |
5373 | warnings. Probably ought to make the sv_iv_please() that does | |
5374 | the conversion if possible, and silently. */ | |
5375 | I32 numtype = looks_like_number(sv); | |
5376 | if (numtype && !(numtype & IS_NUMBER_INFINITY)) { | |
5377 | /* Need to try really hard to see if it's an integer. | |
5378 | 9.22337203685478e+18 is an integer. | |
5379 | but "9.22337203685478e+18" + 0 is UV=9223372036854779904 | |
5380 | so $a="9.22337203685478e+18"; $a+0; $a++ | |
5381 | needs to be the same as $a="9.22337203685478e+18"; $a++ | |
5382 | or we go insane. */ | |
d460ef45 | 5383 | |
28e5dec8 JH |
5384 | (void) sv_2iv(sv); |
5385 | if (SvIOK(sv)) | |
5386 | goto oops_its_int; | |
5387 | ||
5388 | /* sv_2iv *should* have made this an NV */ | |
5389 | if (flags & SVp_NOK) { | |
5390 | (void)SvNOK_only(sv); | |
5391 | SvNVX(sv) += 1.0; | |
5392 | return; | |
5393 | } | |
5394 | /* I don't think we can get here. Maybe I should assert this | |
5395 | And if we do get here I suspect that sv_setnv will croak. NWC | |
5396 | Fall through. */ | |
5397 | #if defined(USE_LONG_DOUBLE) | |
5398 | 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", | |
5399 | SvPVX(sv), SvIVX(sv), SvNVX(sv))); | |
5400 | #else | |
5401 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%g\n", | |
5402 | SvPVX(sv), SvIVX(sv), SvNVX(sv))); | |
5403 | #endif | |
5404 | } | |
5405 | #endif /* PERL_PRESERVE_IVUV */ | |
5406 | sv_setnv(sv,Atof(SvPVX(sv)) + 1.0); | |
79072805 LW |
5407 | return; |
5408 | } | |
5409 | d--; | |
463ee0b2 | 5410 | while (d >= SvPVX(sv)) { |
79072805 LW |
5411 | if (isDIGIT(*d)) { |
5412 | if (++*d <= '9') | |
5413 | return; | |
5414 | *(d--) = '0'; | |
5415 | } | |
5416 | else { | |
9d116dd7 JH |
5417 | #ifdef EBCDIC |
5418 | /* MKS: The original code here died if letters weren't consecutive. | |
5419 | * at least it didn't have to worry about non-C locales. The | |
5420 | * new code assumes that ('z'-'a')==('Z'-'A'), letters are | |
1c846c1f | 5421 | * arranged in order (although not consecutively) and that only |
9d116dd7 JH |
5422 | * [A-Za-z] are accepted by isALPHA in the C locale. |
5423 | */ | |
5424 | if (*d != 'z' && *d != 'Z') { | |
5425 | do { ++*d; } while (!isALPHA(*d)); | |
5426 | return; | |
5427 | } | |
5428 | *(d--) -= 'z' - 'a'; | |
5429 | #else | |
79072805 LW |
5430 | ++*d; |
5431 | if (isALPHA(*d)) | |
5432 | return; | |
5433 | *(d--) -= 'z' - 'a' + 1; | |
9d116dd7 | 5434 | #endif |
79072805 LW |
5435 | } |
5436 | } | |
5437 | /* oh,oh, the number grew */ | |
5438 | SvGROW(sv, SvCUR(sv) + 2); | |
5439 | SvCUR(sv)++; | |
463ee0b2 | 5440 | for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX(sv); d--) |
79072805 LW |
5441 | *d = d[-1]; |
5442 | if (isDIGIT(d[1])) | |
5443 | *d = '1'; | |
5444 | else | |
5445 | *d = d[1]; | |
5446 | } | |
5447 | ||
954c1994 GS |
5448 | /* |
5449 | =for apidoc sv_dec | |
5450 | ||
5451 | Auto-decrement of the value in the SV. | |
5452 | ||
5453 | =cut | |
5454 | */ | |
5455 | ||
79072805 | 5456 | void |
864dbfa3 | 5457 | Perl_sv_dec(pTHX_ register SV *sv) |
79072805 | 5458 | { |
463ee0b2 LW |
5459 | int flags; |
5460 | ||
79072805 LW |
5461 | if (!sv) |
5462 | return; | |
b23a5f78 GB |
5463 | if (SvGMAGICAL(sv)) |
5464 | mg_get(sv); | |
ed6116ce | 5465 | if (SvTHINKFIRST(sv)) { |
0f15f207 | 5466 | if (SvREADONLY(sv)) { |
3280af22 | 5467 | if (PL_curcop != &PL_compiling) |
cea2e8a9 | 5468 | Perl_croak(aTHX_ PL_no_modify); |
0f15f207 | 5469 | } |
a0d0e21e | 5470 | if (SvROK(sv)) { |
b5be31e9 | 5471 | IV i; |
9e7bc3e8 JD |
5472 | if (SvAMAGIC(sv) && AMG_CALLun(sv,dec)) |
5473 | return; | |
56431972 | 5474 | i = PTR2IV(SvRV(sv)); |
b5be31e9 SM |
5475 | sv_unref(sv); |
5476 | sv_setiv(sv, i); | |
a0d0e21e | 5477 | } |
ed6116ce | 5478 | } |
28e5dec8 JH |
5479 | /* Unlike sv_inc we don't have to worry about string-never-numbers |
5480 | and keeping them magic. But we mustn't warn on punting */ | |
8990e307 | 5481 | flags = SvFLAGS(sv); |
28e5dec8 JH |
5482 | if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) { |
5483 | /* It's publicly an integer, or privately an integer-not-float */ | |
5484 | oops_its_int: | |
25da4f38 IZ |
5485 | if (SvIsUV(sv)) { |
5486 | if (SvUVX(sv) == 0) { | |
5487 | (void)SvIOK_only(sv); | |
5488 | SvIVX(sv) = -1; | |
5489 | } | |
5490 | else { | |
5491 | (void)SvIOK_only_UV(sv); | |
5492 | --SvUVX(sv); | |
1c846c1f | 5493 | } |
25da4f38 IZ |
5494 | } else { |
5495 | if (SvIVX(sv) == IV_MIN) | |
65202027 | 5496 | sv_setnv(sv, (NV)IV_MIN - 1.0); |
25da4f38 IZ |
5497 | else { |
5498 | (void)SvIOK_only(sv); | |
5499 | --SvIVX(sv); | |
1c846c1f | 5500 | } |
55497cff | 5501 | } |
5502 | return; | |
5503 | } | |
28e5dec8 JH |
5504 | if (flags & SVp_NOK) { |
5505 | SvNVX(sv) -= 1.0; | |
5506 | (void)SvNOK_only(sv); | |
5507 | return; | |
5508 | } | |
8990e307 | 5509 | if (!(flags & SVp_POK)) { |
4633a7c4 LW |
5510 | if ((flags & SVTYPEMASK) < SVt_PVNV) |
5511 | sv_upgrade(sv, SVt_NV); | |
463ee0b2 | 5512 | SvNVX(sv) = -1.0; |
a0d0e21e | 5513 | (void)SvNOK_only(sv); |
79072805 LW |
5514 | return; |
5515 | } | |
28e5dec8 JH |
5516 | #ifdef PERL_PRESERVE_IVUV |
5517 | { | |
5518 | I32 numtype = looks_like_number(sv); | |
5519 | if (numtype && !(numtype & IS_NUMBER_INFINITY)) { | |
5520 | /* Need to try really hard to see if it's an integer. | |
5521 | 9.22337203685478e+18 is an integer. | |
5522 | but "9.22337203685478e+18" + 0 is UV=9223372036854779904 | |
5523 | so $a="9.22337203685478e+18"; $a+0; $a-- | |
5524 | needs to be the same as $a="9.22337203685478e+18"; $a-- | |
5525 | or we go insane. */ | |
d460ef45 | 5526 | |
28e5dec8 JH |
5527 | (void) sv_2iv(sv); |
5528 | if (SvIOK(sv)) | |
5529 | goto oops_its_int; | |
5530 | ||
5531 | /* sv_2iv *should* have made this an NV */ | |
5532 | if (flags & SVp_NOK) { | |
5533 | (void)SvNOK_only(sv); | |
5534 | SvNVX(sv) -= 1.0; | |
5535 | return; | |
5536 | } | |
5537 | /* I don't think we can get here. Maybe I should assert this | |
5538 | And if we do get here I suspect that sv_setnv will croak. NWC | |
5539 | Fall through. */ | |
5540 | #if defined(USE_LONG_DOUBLE) | |
5541 | 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", | |
5542 | SvPVX(sv), SvIVX(sv), SvNVX(sv))); | |
5543 | #else | |
5544 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%g\n", | |
5545 | SvPVX(sv), SvIVX(sv), SvNVX(sv))); | |
5546 | #endif | |
5547 | } | |
5548 | } | |
5549 | #endif /* PERL_PRESERVE_IVUV */ | |
097ee67d | 5550 | sv_setnv(sv,Atof(SvPVX(sv)) - 1.0); /* punt */ |
79072805 LW |
5551 | } |
5552 | ||
954c1994 GS |
5553 | /* |
5554 | =for apidoc sv_mortalcopy | |
5555 | ||
5556 | Creates a new SV which is a copy of the original SV. The new SV is marked | |
5557 | as mortal. | |
5558 | ||
5559 | =cut | |
5560 | */ | |
5561 | ||
79072805 LW |
5562 | /* Make a string that will exist for the duration of the expression |
5563 | * evaluation. Actually, it may have to last longer than that, but | |
5564 | * hopefully we won't free it until it has been assigned to a | |
5565 | * permanent location. */ | |
5566 | ||
5567 | SV * | |
864dbfa3 | 5568 | Perl_sv_mortalcopy(pTHX_ SV *oldstr) |
79072805 | 5569 | { |
463ee0b2 | 5570 | register SV *sv; |
79072805 | 5571 | |
4561caa4 | 5572 | new_SV(sv); |
79072805 | 5573 | sv_setsv(sv,oldstr); |
677b06e3 GS |
5574 | EXTEND_MORTAL(1); |
5575 | PL_tmps_stack[++PL_tmps_ix] = sv; | |
8990e307 LW |
5576 | SvTEMP_on(sv); |
5577 | return sv; | |
5578 | } | |
5579 | ||
954c1994 GS |
5580 | /* |
5581 | =for apidoc sv_newmortal | |
5582 | ||
5583 | Creates a new SV which is mortal. The reference count of the SV is set to 1. | |
5584 | ||
5585 | =cut | |
5586 | */ | |
5587 | ||
8990e307 | 5588 | SV * |
864dbfa3 | 5589 | Perl_sv_newmortal(pTHX) |
8990e307 LW |
5590 | { |
5591 | register SV *sv; | |
5592 | ||
4561caa4 | 5593 | new_SV(sv); |
8990e307 | 5594 | SvFLAGS(sv) = SVs_TEMP; |
677b06e3 GS |
5595 | EXTEND_MORTAL(1); |
5596 | PL_tmps_stack[++PL_tmps_ix] = sv; | |
79072805 LW |
5597 | return sv; |
5598 | } | |
5599 | ||
954c1994 GS |
5600 | /* |
5601 | =for apidoc sv_2mortal | |
5602 | ||
5603 | Marks an SV as mortal. The SV will be destroyed when the current context | |
5604 | ends. | |
5605 | ||
5606 | =cut | |
5607 | */ | |
5608 | ||
79072805 LW |
5609 | /* same thing without the copying */ |
5610 | ||
5611 | SV * | |
864dbfa3 | 5612 | Perl_sv_2mortal(pTHX_ register SV *sv) |
79072805 LW |
5613 | { |
5614 | if (!sv) | |
5615 | return sv; | |
d689ffdd | 5616 | if (SvREADONLY(sv) && SvIMMORTAL(sv)) |
11162842 | 5617 | return sv; |
677b06e3 GS |
5618 | EXTEND_MORTAL(1); |
5619 | PL_tmps_stack[++PL_tmps_ix] = sv; | |
8990e307 | 5620 | SvTEMP_on(sv); |
79072805 LW |
5621 | return sv; |
5622 | } | |
5623 | ||
954c1994 GS |
5624 | /* |
5625 | =for apidoc newSVpv | |
5626 | ||
5627 | Creates a new SV and copies a string into it. The reference count for the | |
5628 | SV is set to 1. If C<len> is zero, Perl will compute the length using | |
5629 | strlen(). For efficiency, consider using C<newSVpvn> instead. | |
5630 | ||
5631 | =cut | |
5632 | */ | |
5633 | ||
79072805 | 5634 | SV * |
864dbfa3 | 5635 | Perl_newSVpv(pTHX_ const char *s, STRLEN len) |
79072805 | 5636 | { |
463ee0b2 | 5637 | register SV *sv; |
79072805 | 5638 | |
4561caa4 | 5639 | new_SV(sv); |
79072805 LW |
5640 | if (!len) |
5641 | len = strlen(s); | |
5642 | sv_setpvn(sv,s,len); | |
5643 | return sv; | |
5644 | } | |
5645 | ||
954c1994 GS |
5646 | /* |
5647 | =for apidoc newSVpvn | |
5648 | ||
5649 | Creates a new SV and copies a string into it. The reference count for the | |
1c846c1f | 5650 | SV is set to 1. Note that if C<len> is zero, Perl will create a zero length |
954c1994 GS |
5651 | string. You are responsible for ensuring that the source string is at least |
5652 | C<len> bytes long. | |
5653 | ||
5654 | =cut | |
5655 | */ | |
5656 | ||
9da1e3b5 | 5657 | SV * |
864dbfa3 | 5658 | Perl_newSVpvn(pTHX_ const char *s, STRLEN len) |
9da1e3b5 MUN |
5659 | { |
5660 | register SV *sv; | |
5661 | ||
5662 | new_SV(sv); | |
9da1e3b5 MUN |
5663 | sv_setpvn(sv,s,len); |
5664 | return sv; | |
5665 | } | |
5666 | ||
1c846c1f NIS |
5667 | /* |
5668 | =for apidoc newSVpvn_share | |
5669 | ||
5670 | Creates a new SV and populates it with a string from | |
5671 | the string table. Turns on READONLY and FAKE. | |
5672 | The idea here is that as string table is used for shared hash | |
5673 | keys these strings will have SvPVX == HeKEY and hash lookup | |
5674 | will avoid string compare. | |
5675 | ||
5676 | =cut | |
5677 | */ | |
5678 | ||
5679 | SV * | |
c3654f1a | 5680 | Perl_newSVpvn_share(pTHX_ const char *src, I32 len, U32 hash) |
1c846c1f NIS |
5681 | { |
5682 | register SV *sv; | |
c3654f1a IH |
5683 | bool is_utf8 = FALSE; |
5684 | if (len < 0) { | |
5685 | len = -len; | |
5686 | is_utf8 = TRUE; | |
5687 | } | |
75a54232 JH |
5688 | if (is_utf8 && !(PL_hints & HINT_UTF8_DISTINCT)) { |
5689 | STRLEN tmplen = len; | |
5690 | /* See the note in hv.c:hv_fetch() --jhi */ | |
5691 | src = (char*)bytes_from_utf8((U8*)src, &tmplen, &is_utf8); | |
5692 | len = tmplen; | |
5693 | } | |
1c846c1f NIS |
5694 | if (!hash) |
5695 | PERL_HASH(hash, src, len); | |
5696 | new_SV(sv); | |
5697 | sv_upgrade(sv, SVt_PVIV); | |
c3654f1a | 5698 | SvPVX(sv) = sharepvn(src, is_utf8?-len:len, hash); |
1c846c1f NIS |
5699 | SvCUR(sv) = len; |
5700 | SvUVX(sv) = hash; | |
5701 | SvLEN(sv) = 0; | |
5702 | SvREADONLY_on(sv); | |
5703 | SvFAKE_on(sv); | |
5704 | SvPOK_on(sv); | |
c3654f1a IH |
5705 | if (is_utf8) |
5706 | SvUTF8_on(sv); | |
1c846c1f NIS |
5707 | return sv; |
5708 | } | |
5709 | ||
cea2e8a9 | 5710 | #if defined(PERL_IMPLICIT_CONTEXT) |
46fc3d4c | 5711 | SV * |
cea2e8a9 | 5712 | Perl_newSVpvf_nocontext(const char* pat, ...) |
46fc3d4c | 5713 | { |
cea2e8a9 | 5714 | dTHX; |
46fc3d4c | 5715 | register SV *sv; |
5716 | va_list args; | |
46fc3d4c | 5717 | va_start(args, pat); |
c5be433b | 5718 | sv = vnewSVpvf(pat, &args); |
46fc3d4c | 5719 | va_end(args); |
5720 | return sv; | |
5721 | } | |
cea2e8a9 | 5722 | #endif |
46fc3d4c | 5723 | |
954c1994 GS |
5724 | /* |
5725 | =for apidoc newSVpvf | |
5726 | ||
5727 | Creates a new SV an initialize it with the string formatted like | |
5728 | C<sprintf>. | |
5729 | ||
5730 | =cut | |
5731 | */ | |
5732 | ||
cea2e8a9 GS |
5733 | SV * |
5734 | Perl_newSVpvf(pTHX_ const char* pat, ...) | |
5735 | { | |
5736 | register SV *sv; | |
5737 | va_list args; | |
cea2e8a9 | 5738 | va_start(args, pat); |
c5be433b | 5739 | sv = vnewSVpvf(pat, &args); |
cea2e8a9 GS |
5740 | va_end(args); |
5741 | return sv; | |
5742 | } | |
46fc3d4c | 5743 | |
79072805 | 5744 | SV * |
c5be433b GS |
5745 | Perl_vnewSVpvf(pTHX_ const char* pat, va_list* args) |
5746 | { | |
5747 | register SV *sv; | |
5748 | new_SV(sv); | |
5749 | sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
5750 | return sv; | |
5751 | } | |
5752 | ||
954c1994 GS |
5753 | /* |
5754 | =for apidoc newSVnv | |
5755 | ||
5756 | Creates a new SV and copies a floating point value into it. | |
5757 | The reference count for the SV is set to 1. | |
5758 | ||
5759 | =cut | |
5760 | */ | |
5761 | ||
c5be433b | 5762 | SV * |
65202027 | 5763 | Perl_newSVnv(pTHX_ NV n) |
79072805 | 5764 | { |
463ee0b2 | 5765 | register SV *sv; |
79072805 | 5766 | |
4561caa4 | 5767 | new_SV(sv); |
79072805 LW |
5768 | sv_setnv(sv,n); |
5769 | return sv; | |
5770 | } | |
5771 | ||
954c1994 GS |
5772 | /* |
5773 | =for apidoc newSViv | |
5774 | ||
5775 | Creates a new SV and copies an integer into it. The reference count for the | |
5776 | SV is set to 1. | |
5777 | ||
5778 | =cut | |
5779 | */ | |
5780 | ||
79072805 | 5781 | SV * |
864dbfa3 | 5782 | Perl_newSViv(pTHX_ IV i) |
79072805 | 5783 | { |
463ee0b2 | 5784 | register SV *sv; |
79072805 | 5785 | |
4561caa4 | 5786 | new_SV(sv); |
79072805 LW |
5787 | sv_setiv(sv,i); |
5788 | return sv; | |
5789 | } | |
5790 | ||
954c1994 | 5791 | /* |
1a3327fb JH |
5792 | =for apidoc newSVuv |
5793 | ||
5794 | Creates a new SV and copies an unsigned integer into it. | |
5795 | The reference count for the SV is set to 1. | |
5796 | ||
5797 | =cut | |
5798 | */ | |
5799 | ||
5800 | SV * | |
5801 | Perl_newSVuv(pTHX_ UV u) | |
5802 | { | |
5803 | register SV *sv; | |
5804 | ||
5805 | new_SV(sv); | |
5806 | sv_setuv(sv,u); | |
5807 | return sv; | |
5808 | } | |
5809 | ||
5810 | /* | |
954c1994 GS |
5811 | =for apidoc newRV_noinc |
5812 | ||
5813 | Creates an RV wrapper for an SV. The reference count for the original | |
5814 | SV is B<not> incremented. | |
5815 | ||
5816 | =cut | |
5817 | */ | |
5818 | ||
2304df62 | 5819 | SV * |
864dbfa3 | 5820 | Perl_newRV_noinc(pTHX_ SV *tmpRef) |
2304df62 AD |
5821 | { |
5822 | register SV *sv; | |
5823 | ||
4561caa4 | 5824 | new_SV(sv); |
2304df62 | 5825 | sv_upgrade(sv, SVt_RV); |
76e3520e | 5826 | SvTEMP_off(tmpRef); |
d689ffdd | 5827 | SvRV(sv) = tmpRef; |
2304df62 | 5828 | SvROK_on(sv); |
2304df62 AD |
5829 | return sv; |
5830 | } | |
5831 | ||
954c1994 | 5832 | /* newRV_inc is #defined to newRV in sv.h */ |
5f05dabc | 5833 | SV * |
864dbfa3 | 5834 | Perl_newRV(pTHX_ SV *tmpRef) |
5f05dabc | 5835 | { |
5f6447b6 | 5836 | return newRV_noinc(SvREFCNT_inc(tmpRef)); |
5f05dabc | 5837 | } |
5f05dabc | 5838 | |
954c1994 GS |
5839 | /* |
5840 | =for apidoc newSVsv | |
5841 | ||
5842 | Creates a new SV which is an exact duplicate of the original SV. | |
5843 | ||
5844 | =cut | |
5845 | */ | |
5846 | ||
79072805 LW |
5847 | /* make an exact duplicate of old */ |
5848 | ||
5849 | SV * | |
864dbfa3 | 5850 | Perl_newSVsv(pTHX_ register SV *old) |
79072805 | 5851 | { |
463ee0b2 | 5852 | register SV *sv; |
79072805 LW |
5853 | |
5854 | if (!old) | |
5855 | return Nullsv; | |
8990e307 | 5856 | if (SvTYPE(old) == SVTYPEMASK) { |
0453d815 PM |
5857 | if (ckWARN_d(WARN_INTERNAL)) |
5858 | Perl_warner(aTHX_ WARN_INTERNAL, "semi-panic: attempt to dup freed string"); | |
79072805 LW |
5859 | return Nullsv; |
5860 | } | |
4561caa4 | 5861 | new_SV(sv); |
ff68c719 | 5862 | if (SvTEMP(old)) { |
5863 | SvTEMP_off(old); | |
463ee0b2 | 5864 | sv_setsv(sv,old); |
ff68c719 | 5865 | SvTEMP_on(old); |
79072805 LW |
5866 | } |
5867 | else | |
463ee0b2 LW |
5868 | sv_setsv(sv,old); |
5869 | return sv; | |
79072805 LW |
5870 | } |
5871 | ||
5872 | void | |
864dbfa3 | 5873 | Perl_sv_reset(pTHX_ register char *s, HV *stash) |
79072805 LW |
5874 | { |
5875 | register HE *entry; | |
5876 | register GV *gv; | |
5877 | register SV *sv; | |
5878 | register I32 i; | |
5879 | register PMOP *pm; | |
5880 | register I32 max; | |
4802d5d7 | 5881 | char todo[PERL_UCHAR_MAX+1]; |
79072805 | 5882 | |
49d8d3a1 MB |
5883 | if (!stash) |
5884 | return; | |
5885 | ||
79072805 LW |
5886 | if (!*s) { /* reset ?? searches */ |
5887 | for (pm = HvPMROOT(stash); pm; pm = pm->op_pmnext) { | |
48c036b1 | 5888 | pm->op_pmdynflags &= ~PMdf_USED; |
79072805 LW |
5889 | } |
5890 | return; | |
5891 | } | |
5892 | ||
5893 | /* reset variables */ | |
5894 | ||
5895 | if (!HvARRAY(stash)) | |
5896 | return; | |
463ee0b2 LW |
5897 | |
5898 | Zero(todo, 256, char); | |
79072805 | 5899 | while (*s) { |
4802d5d7 | 5900 | i = (unsigned char)*s; |
79072805 LW |
5901 | if (s[1] == '-') { |
5902 | s += 2; | |
5903 | } | |
4802d5d7 | 5904 | max = (unsigned char)*s++; |
79072805 | 5905 | for ( ; i <= max; i++) { |
463ee0b2 LW |
5906 | todo[i] = 1; |
5907 | } | |
a0d0e21e | 5908 | for (i = 0; i <= (I32) HvMAX(stash); i++) { |
79072805 | 5909 | for (entry = HvARRAY(stash)[i]; |
9e35f4b3 GS |
5910 | entry; |
5911 | entry = HeNEXT(entry)) | |
5912 | { | |
1edc1566 | 5913 | if (!todo[(U8)*HeKEY(entry)]) |
463ee0b2 | 5914 | continue; |
1edc1566 | 5915 | gv = (GV*)HeVAL(entry); |
79072805 | 5916 | sv = GvSV(gv); |
9e35f4b3 GS |
5917 | if (SvTHINKFIRST(sv)) { |
5918 | if (!SvREADONLY(sv) && SvROK(sv)) | |
5919 | sv_unref(sv); | |
5920 | continue; | |
5921 | } | |
a0d0e21e | 5922 | (void)SvOK_off(sv); |
79072805 LW |
5923 | if (SvTYPE(sv) >= SVt_PV) { |
5924 | SvCUR_set(sv, 0); | |
463ee0b2 LW |
5925 | if (SvPVX(sv) != Nullch) |
5926 | *SvPVX(sv) = '\0'; | |
44a8e56a | 5927 | SvTAINT(sv); |
79072805 LW |
5928 | } |
5929 | if (GvAV(gv)) { | |
5930 | av_clear(GvAV(gv)); | |
5931 | } | |
44a8e56a | 5932 | if (GvHV(gv) && !HvNAME(GvHV(gv))) { |
463ee0b2 | 5933 | hv_clear(GvHV(gv)); |
fa6a1c44 | 5934 | #ifdef USE_ENVIRON_ARRAY |
3280af22 | 5935 | if (gv == PL_envgv) |
79072805 | 5936 | environ[0] = Nullch; |
a0d0e21e | 5937 | #endif |
79072805 LW |
5938 | } |
5939 | } | |
5940 | } | |
5941 | } | |
5942 | } | |
5943 | ||
46fc3d4c | 5944 | IO* |
864dbfa3 | 5945 | Perl_sv_2io(pTHX_ SV *sv) |
46fc3d4c | 5946 | { |
5947 | IO* io; | |
5948 | GV* gv; | |
2d8e6c8d | 5949 | STRLEN n_a; |
46fc3d4c | 5950 | |
5951 | switch (SvTYPE(sv)) { | |
5952 | case SVt_PVIO: | |
5953 | io = (IO*)sv; | |
5954 | break; | |
5955 | case SVt_PVGV: | |
5956 | gv = (GV*)sv; | |
5957 | io = GvIO(gv); | |
5958 | if (!io) | |
cea2e8a9 | 5959 | Perl_croak(aTHX_ "Bad filehandle: %s", GvNAME(gv)); |
46fc3d4c | 5960 | break; |
5961 | default: | |
5962 | if (!SvOK(sv)) | |
cea2e8a9 | 5963 | Perl_croak(aTHX_ PL_no_usym, "filehandle"); |
46fc3d4c | 5964 | if (SvROK(sv)) |
5965 | return sv_2io(SvRV(sv)); | |
2d8e6c8d | 5966 | gv = gv_fetchpv(SvPV(sv,n_a), FALSE, SVt_PVIO); |
46fc3d4c | 5967 | if (gv) |
5968 | io = GvIO(gv); | |
5969 | else | |
5970 | io = 0; | |
5971 | if (!io) | |
cea2e8a9 | 5972 | Perl_croak(aTHX_ "Bad filehandle: %s", SvPV(sv,n_a)); |
46fc3d4c | 5973 | break; |
5974 | } | |
5975 | return io; | |
5976 | } | |
5977 | ||
79072805 | 5978 | CV * |
864dbfa3 | 5979 | Perl_sv_2cv(pTHX_ SV *sv, HV **st, GV **gvp, I32 lref) |
79072805 LW |
5980 | { |
5981 | GV *gv; | |
5982 | CV *cv; | |
2d8e6c8d | 5983 | STRLEN n_a; |
79072805 LW |
5984 | |
5985 | if (!sv) | |
93a17b20 | 5986 | return *gvp = Nullgv, Nullcv; |
79072805 | 5987 | switch (SvTYPE(sv)) { |
79072805 LW |
5988 | case SVt_PVCV: |
5989 | *st = CvSTASH(sv); | |
5990 | *gvp = Nullgv; | |
5991 | return (CV*)sv; | |
5992 | case SVt_PVHV: | |
5993 | case SVt_PVAV: | |
5994 | *gvp = Nullgv; | |
5995 | return Nullcv; | |
8990e307 LW |
5996 | case SVt_PVGV: |
5997 | gv = (GV*)sv; | |
a0d0e21e | 5998 | *gvp = gv; |
8990e307 LW |
5999 | *st = GvESTASH(gv); |
6000 | goto fix_gv; | |
6001 | ||
79072805 | 6002 | default: |
a0d0e21e LW |
6003 | if (SvGMAGICAL(sv)) |
6004 | mg_get(sv); | |
6005 | if (SvROK(sv)) { | |
f5284f61 IZ |
6006 | SV **sp = &sv; /* Used in tryAMAGICunDEREF macro. */ |
6007 | tryAMAGICunDEREF(to_cv); | |
6008 | ||
62f274bf GS |
6009 | sv = SvRV(sv); |
6010 | if (SvTYPE(sv) == SVt_PVCV) { | |
6011 | cv = (CV*)sv; | |
6012 | *gvp = Nullgv; | |
6013 | *st = CvSTASH(cv); | |
6014 | return cv; | |
6015 | } | |
6016 | else if(isGV(sv)) | |
6017 | gv = (GV*)sv; | |
6018 | else | |
cea2e8a9 | 6019 | Perl_croak(aTHX_ "Not a subroutine reference"); |
a0d0e21e | 6020 | } |
62f274bf | 6021 | else if (isGV(sv)) |
79072805 LW |
6022 | gv = (GV*)sv; |
6023 | else | |
2d8e6c8d | 6024 | gv = gv_fetchpv(SvPV(sv, n_a), lref, SVt_PVCV); |
79072805 LW |
6025 | *gvp = gv; |
6026 | if (!gv) | |
6027 | return Nullcv; | |
6028 | *st = GvESTASH(gv); | |
8990e307 | 6029 | fix_gv: |
8ebc5c01 | 6030 | if (lref && !GvCVu(gv)) { |
4633a7c4 | 6031 | SV *tmpsv; |
748a9306 | 6032 | ENTER; |
4633a7c4 | 6033 | tmpsv = NEWSV(704,0); |
16660edb | 6034 | gv_efullname3(tmpsv, gv, Nullch); |
f6ec51f7 GS |
6035 | /* XXX this is probably not what they think they're getting. |
6036 | * It has the same effect as "sub name;", i.e. just a forward | |
6037 | * declaration! */ | |
774d564b | 6038 | newSUB(start_subparse(FALSE, 0), |
4633a7c4 LW |
6039 | newSVOP(OP_CONST, 0, tmpsv), |
6040 | Nullop, | |
8990e307 | 6041 | Nullop); |
748a9306 | 6042 | LEAVE; |
8ebc5c01 | 6043 | if (!GvCVu(gv)) |
cea2e8a9 | 6044 | Perl_croak(aTHX_ "Unable to create sub named \"%s\"", SvPV(sv,n_a)); |
8990e307 | 6045 | } |
8ebc5c01 | 6046 | return GvCVu(gv); |
79072805 LW |
6047 | } |
6048 | } | |
6049 | ||
c461cf8f JH |
6050 | /* |
6051 | =for apidoc sv_true | |
6052 | ||
6053 | Returns true if the SV has a true value by Perl's rules. | |
6054 | ||
6055 | =cut | |
6056 | */ | |
6057 | ||
79072805 | 6058 | I32 |
864dbfa3 | 6059 | Perl_sv_true(pTHX_ register SV *sv) |
79072805 | 6060 | { |
8990e307 LW |
6061 | if (!sv) |
6062 | return 0; | |
79072805 | 6063 | if (SvPOK(sv)) { |
4e35701f NIS |
6064 | register XPV* tXpv; |
6065 | if ((tXpv = (XPV*)SvANY(sv)) && | |
c2f1de04 | 6066 | (tXpv->xpv_cur > 1 || |
4e35701f | 6067 | (tXpv->xpv_cur && *tXpv->xpv_pv != '0'))) |
79072805 LW |
6068 | return 1; |
6069 | else | |
6070 | return 0; | |
6071 | } | |
6072 | else { | |
6073 | if (SvIOK(sv)) | |
463ee0b2 | 6074 | return SvIVX(sv) != 0; |
79072805 LW |
6075 | else { |
6076 | if (SvNOK(sv)) | |
463ee0b2 | 6077 | return SvNVX(sv) != 0.0; |
79072805 | 6078 | else |
463ee0b2 | 6079 | return sv_2bool(sv); |
79072805 LW |
6080 | } |
6081 | } | |
6082 | } | |
79072805 | 6083 | |
ff68c719 | 6084 | IV |
864dbfa3 | 6085 | Perl_sv_iv(pTHX_ register SV *sv) |
85e6fe83 | 6086 | { |
25da4f38 IZ |
6087 | if (SvIOK(sv)) { |
6088 | if (SvIsUV(sv)) | |
6089 | return (IV)SvUVX(sv); | |
ff68c719 | 6090 | return SvIVX(sv); |
25da4f38 | 6091 | } |
ff68c719 | 6092 | return sv_2iv(sv); |
85e6fe83 | 6093 | } |
85e6fe83 | 6094 | |
ff68c719 | 6095 | UV |
864dbfa3 | 6096 | Perl_sv_uv(pTHX_ register SV *sv) |
ff68c719 | 6097 | { |
25da4f38 IZ |
6098 | if (SvIOK(sv)) { |
6099 | if (SvIsUV(sv)) | |
6100 | return SvUVX(sv); | |
6101 | return (UV)SvIVX(sv); | |
6102 | } | |
ff68c719 | 6103 | return sv_2uv(sv); |
6104 | } | |
85e6fe83 | 6105 | |
65202027 | 6106 | NV |
864dbfa3 | 6107 | Perl_sv_nv(pTHX_ register SV *sv) |
79072805 | 6108 | { |
ff68c719 | 6109 | if (SvNOK(sv)) |
6110 | return SvNVX(sv); | |
6111 | return sv_2nv(sv); | |
79072805 | 6112 | } |
79072805 | 6113 | |
79072805 | 6114 | char * |
864dbfa3 | 6115 | Perl_sv_pv(pTHX_ SV *sv) |
1fa8b10d JD |
6116 | { |
6117 | STRLEN n_a; | |
6118 | ||
6119 | if (SvPOK(sv)) | |
6120 | return SvPVX(sv); | |
6121 | ||
6122 | return sv_2pv(sv, &n_a); | |
6123 | } | |
6124 | ||
6125 | char * | |
864dbfa3 | 6126 | Perl_sv_pvn(pTHX_ SV *sv, STRLEN *lp) |
79072805 | 6127 | { |
85e6fe83 LW |
6128 | if (SvPOK(sv)) { |
6129 | *lp = SvCUR(sv); | |
a0d0e21e | 6130 | return SvPVX(sv); |
85e6fe83 | 6131 | } |
463ee0b2 | 6132 | return sv_2pv(sv, lp); |
79072805 | 6133 | } |
79072805 | 6134 | |
c461cf8f JH |
6135 | /* |
6136 | =for apidoc sv_pvn_force | |
6137 | ||
6138 | Get a sensible string out of the SV somehow. | |
6139 | ||
6140 | =cut | |
6141 | */ | |
6142 | ||
a0d0e21e | 6143 | char * |
864dbfa3 | 6144 | Perl_sv_pvn_force(pTHX_ SV *sv, STRLEN *lp) |
a0d0e21e LW |
6145 | { |
6146 | char *s; | |
6147 | ||
6fc92669 GS |
6148 | if (SvTHINKFIRST(sv) && !SvROK(sv)) |
6149 | sv_force_normal(sv); | |
1c846c1f | 6150 | |
a0d0e21e LW |
6151 | if (SvPOK(sv)) { |
6152 | *lp = SvCUR(sv); | |
6153 | } | |
6154 | else { | |
748a9306 | 6155 | if (SvTYPE(sv) > SVt_PVLV && SvTYPE(sv) != SVt_PVFM) { |
cea2e8a9 | 6156 | Perl_croak(aTHX_ "Can't coerce %s to string in %s", sv_reftype(sv,0), |
6fc92669 | 6157 | PL_op_name[PL_op->op_type]); |
a0d0e21e | 6158 | } |
4633a7c4 LW |
6159 | else |
6160 | s = sv_2pv(sv, lp); | |
a0d0e21e LW |
6161 | if (s != SvPVX(sv)) { /* Almost, but not quite, sv_setpvn() */ |
6162 | STRLEN len = *lp; | |
1c846c1f | 6163 | |
a0d0e21e LW |
6164 | if (SvROK(sv)) |
6165 | sv_unref(sv); | |
6166 | (void)SvUPGRADE(sv, SVt_PV); /* Never FALSE */ | |
6167 | SvGROW(sv, len + 1); | |
6168 | Move(s,SvPVX(sv),len,char); | |
6169 | SvCUR_set(sv, len); | |
6170 | *SvEND(sv) = '\0'; | |
6171 | } | |
6172 | if (!SvPOK(sv)) { | |
6173 | SvPOK_on(sv); /* validate pointer */ | |
6174 | SvTAINT(sv); | |
1d7c1841 GS |
6175 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n", |
6176 | PTR2UV(sv),SvPVX(sv))); | |
a0d0e21e LW |
6177 | } |
6178 | } | |
6179 | return SvPVX(sv); | |
6180 | } | |
6181 | ||
6182 | char * | |
7340a771 GS |
6183 | Perl_sv_pvbyte(pTHX_ SV *sv) |
6184 | { | |
ffebcc3e | 6185 | sv_utf8_downgrade(sv,0); |
7340a771 GS |
6186 | return sv_pv(sv); |
6187 | } | |
6188 | ||
6189 | char * | |
6190 | Perl_sv_pvbyten(pTHX_ SV *sv, STRLEN *lp) | |
6191 | { | |
ffebcc3e | 6192 | sv_utf8_downgrade(sv,0); |
7340a771 GS |
6193 | return sv_pvn(sv,lp); |
6194 | } | |
6195 | ||
6196 | char * | |
6197 | Perl_sv_pvbyten_force(pTHX_ SV *sv, STRLEN *lp) | |
6198 | { | |
ffebcc3e | 6199 | sv_utf8_downgrade(sv,0); |
7340a771 GS |
6200 | return sv_pvn_force(sv,lp); |
6201 | } | |
6202 | ||
6203 | char * | |
6204 | Perl_sv_pvutf8(pTHX_ SV *sv) | |
6205 | { | |
560a288e | 6206 | sv_utf8_upgrade(sv); |
7340a771 GS |
6207 | return sv_pv(sv); |
6208 | } | |
6209 | ||
6210 | char * | |
6211 | Perl_sv_pvutf8n(pTHX_ SV *sv, STRLEN *lp) | |
6212 | { | |
560a288e | 6213 | sv_utf8_upgrade(sv); |
7340a771 GS |
6214 | return sv_pvn(sv,lp); |
6215 | } | |
6216 | ||
c461cf8f JH |
6217 | /* |
6218 | =for apidoc sv_pvutf8n_force | |
6219 | ||
6220 | Get a sensible UTF8-encoded string out of the SV somehow. See | |
6221 | L</sv_pvn_force>. | |
6222 | ||
6223 | =cut | |
6224 | */ | |
6225 | ||
7340a771 GS |
6226 | char * |
6227 | Perl_sv_pvutf8n_force(pTHX_ SV *sv, STRLEN *lp) | |
6228 | { | |
560a288e | 6229 | sv_utf8_upgrade(sv); |
7340a771 GS |
6230 | return sv_pvn_force(sv,lp); |
6231 | } | |
6232 | ||
c461cf8f JH |
6233 | /* |
6234 | =for apidoc sv_reftype | |
6235 | ||
6236 | Returns a string describing what the SV is a reference to. | |
6237 | ||
6238 | =cut | |
6239 | */ | |
6240 | ||
7340a771 | 6241 | char * |
864dbfa3 | 6242 | Perl_sv_reftype(pTHX_ SV *sv, int ob) |
a0d0e21e LW |
6243 | { |
6244 | if (ob && SvOBJECT(sv)) | |
6245 | return HvNAME(SvSTASH(sv)); | |
6246 | else { | |
6247 | switch (SvTYPE(sv)) { | |
6248 | case SVt_NULL: | |
6249 | case SVt_IV: | |
6250 | case SVt_NV: | |
6251 | case SVt_RV: | |
6252 | case SVt_PV: | |
6253 | case SVt_PVIV: | |
6254 | case SVt_PVNV: | |
6255 | case SVt_PVMG: | |
6256 | case SVt_PVBM: | |
6257 | if (SvROK(sv)) | |
6258 | return "REF"; | |
6259 | else | |
6260 | return "SCALAR"; | |
6261 | case SVt_PVLV: return "LVALUE"; | |
6262 | case SVt_PVAV: return "ARRAY"; | |
6263 | case SVt_PVHV: return "HASH"; | |
6264 | case SVt_PVCV: return "CODE"; | |
6265 | case SVt_PVGV: return "GLOB"; | |
1d2dff63 | 6266 | case SVt_PVFM: return "FORMAT"; |
27f9d8f3 | 6267 | case SVt_PVIO: return "IO"; |
a0d0e21e LW |
6268 | default: return "UNKNOWN"; |
6269 | } | |
6270 | } | |
6271 | } | |
6272 | ||
954c1994 GS |
6273 | /* |
6274 | =for apidoc sv_isobject | |
6275 | ||
6276 | Returns a boolean indicating whether the SV is an RV pointing to a blessed | |
6277 | object. If the SV is not an RV, or if the object is not blessed, then this | |
6278 | will return false. | |
6279 | ||
6280 | =cut | |
6281 | */ | |
6282 | ||
463ee0b2 | 6283 | int |
864dbfa3 | 6284 | Perl_sv_isobject(pTHX_ SV *sv) |
85e6fe83 | 6285 | { |
68dc0745 | 6286 | if (!sv) |
6287 | return 0; | |
6288 | if (SvGMAGICAL(sv)) | |
6289 | mg_get(sv); | |
85e6fe83 LW |
6290 | if (!SvROK(sv)) |
6291 | return 0; | |
6292 | sv = (SV*)SvRV(sv); | |
6293 | if (!SvOBJECT(sv)) | |
6294 | return 0; | |
6295 | return 1; | |
6296 | } | |
6297 | ||
954c1994 GS |
6298 | /* |
6299 | =for apidoc sv_isa | |
6300 | ||
6301 | Returns a boolean indicating whether the SV is blessed into the specified | |
6302 | class. This does not check for subtypes; use C<sv_derived_from> to verify | |
6303 | an inheritance relationship. | |
6304 | ||
6305 | =cut | |
6306 | */ | |
6307 | ||
85e6fe83 | 6308 | int |
864dbfa3 | 6309 | Perl_sv_isa(pTHX_ SV *sv, const char *name) |
463ee0b2 | 6310 | { |
68dc0745 | 6311 | if (!sv) |
6312 | return 0; | |
6313 | if (SvGMAGICAL(sv)) | |
6314 | mg_get(sv); | |
ed6116ce | 6315 | if (!SvROK(sv)) |
463ee0b2 | 6316 | return 0; |
ed6116ce LW |
6317 | sv = (SV*)SvRV(sv); |
6318 | if (!SvOBJECT(sv)) | |
463ee0b2 LW |
6319 | return 0; |
6320 | ||
6321 | return strEQ(HvNAME(SvSTASH(sv)), name); | |
6322 | } | |
6323 | ||
954c1994 GS |
6324 | /* |
6325 | =for apidoc newSVrv | |
6326 | ||
6327 | Creates a new SV for the RV, C<rv>, to point to. If C<rv> is not an RV then | |
6328 | it will be upgraded to one. If C<classname> is non-null then the new SV will | |
6329 | be blessed in the specified package. The new SV is returned and its | |
6330 | reference count is 1. | |
6331 | ||
6332 | =cut | |
6333 | */ | |
6334 | ||
463ee0b2 | 6335 | SV* |
864dbfa3 | 6336 | Perl_newSVrv(pTHX_ SV *rv, const char *classname) |
463ee0b2 | 6337 | { |
463ee0b2 LW |
6338 | SV *sv; |
6339 | ||
4561caa4 | 6340 | new_SV(sv); |
51cf62d8 | 6341 | |
2213622d | 6342 | SV_CHECK_THINKFIRST(rv); |
51cf62d8 | 6343 | SvAMAGIC_off(rv); |
51cf62d8 | 6344 | |
0199fce9 JD |
6345 | if (SvTYPE(rv) >= SVt_PVMG) { |
6346 | U32 refcnt = SvREFCNT(rv); | |
6347 | SvREFCNT(rv) = 0; | |
6348 | sv_clear(rv); | |
6349 | SvFLAGS(rv) = 0; | |
6350 | SvREFCNT(rv) = refcnt; | |
6351 | } | |
6352 | ||
51cf62d8 | 6353 | if (SvTYPE(rv) < SVt_RV) |
0199fce9 JD |
6354 | sv_upgrade(rv, SVt_RV); |
6355 | else if (SvTYPE(rv) > SVt_RV) { | |
6356 | (void)SvOOK_off(rv); | |
6357 | if (SvPVX(rv) && SvLEN(rv)) | |
6358 | Safefree(SvPVX(rv)); | |
6359 | SvCUR_set(rv, 0); | |
6360 | SvLEN_set(rv, 0); | |
6361 | } | |
51cf62d8 OT |
6362 | |
6363 | (void)SvOK_off(rv); | |
053fc874 | 6364 | SvRV(rv) = sv; |
ed6116ce | 6365 | SvROK_on(rv); |
463ee0b2 | 6366 | |
a0d0e21e LW |
6367 | if (classname) { |
6368 | HV* stash = gv_stashpv(classname, TRUE); | |
6369 | (void)sv_bless(rv, stash); | |
6370 | } | |
6371 | return sv; | |
6372 | } | |
6373 | ||
954c1994 GS |
6374 | /* |
6375 | =for apidoc sv_setref_pv | |
6376 | ||
6377 | Copies a pointer into a new SV, optionally blessing the SV. The C<rv> | |
6378 | argument will be upgraded to an RV. That RV will be modified to point to | |
6379 | the new SV. If the C<pv> argument is NULL then C<PL_sv_undef> will be placed | |
6380 | into the SV. The C<classname> argument indicates the package for the | |
6381 | blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV | |
6382 | will be returned and will have a reference count of 1. | |
6383 | ||
6384 | Do not use with other Perl types such as HV, AV, SV, CV, because those | |
6385 | objects will become corrupted by the pointer copy process. | |
6386 | ||
6387 | Note that C<sv_setref_pvn> copies the string while this copies the pointer. | |
6388 | ||
6389 | =cut | |
6390 | */ | |
6391 | ||
a0d0e21e | 6392 | SV* |
864dbfa3 | 6393 | Perl_sv_setref_pv(pTHX_ SV *rv, const char *classname, void *pv) |
a0d0e21e | 6394 | { |
189b2af5 | 6395 | if (!pv) { |
3280af22 | 6396 | sv_setsv(rv, &PL_sv_undef); |
189b2af5 GS |
6397 | SvSETMAGIC(rv); |
6398 | } | |
a0d0e21e | 6399 | else |
56431972 | 6400 | sv_setiv(newSVrv(rv,classname), PTR2IV(pv)); |
a0d0e21e LW |
6401 | return rv; |
6402 | } | |
6403 | ||
954c1994 GS |
6404 | /* |
6405 | =for apidoc sv_setref_iv | |
6406 | ||
6407 | Copies an integer into a new SV, optionally blessing the SV. The C<rv> | |
6408 | argument will be upgraded to an RV. That RV will be modified to point to | |
6409 | the new SV. The C<classname> argument indicates the package for the | |
6410 | blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV | |
6411 | will be returned and will have a reference count of 1. | |
6412 | ||
6413 | =cut | |
6414 | */ | |
6415 | ||
a0d0e21e | 6416 | SV* |
864dbfa3 | 6417 | Perl_sv_setref_iv(pTHX_ SV *rv, const char *classname, IV iv) |
a0d0e21e LW |
6418 | { |
6419 | sv_setiv(newSVrv(rv,classname), iv); | |
6420 | return rv; | |
6421 | } | |
6422 | ||
954c1994 | 6423 | /* |
e1c57cef JH |
6424 | =for apidoc sv_setref_uv |
6425 | ||
6426 | Copies an unsigned integer into a new SV, optionally blessing the SV. The C<rv> | |
6427 | argument will be upgraded to an RV. That RV will be modified to point to | |
6428 | the new SV. The C<classname> argument indicates the package for the | |
6429 | blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV | |
6430 | will be returned and will have a reference count of 1. | |
6431 | ||
6432 | =cut | |
6433 | */ | |
6434 | ||
6435 | SV* | |
6436 | Perl_sv_setref_uv(pTHX_ SV *rv, const char *classname, UV uv) | |
6437 | { | |
6438 | sv_setuv(newSVrv(rv,classname), uv); | |
6439 | return rv; | |
6440 | } | |
6441 | ||
6442 | /* | |
954c1994 GS |
6443 | =for apidoc sv_setref_nv |
6444 | ||
6445 | Copies a double into a new SV, optionally blessing the SV. The C<rv> | |
6446 | argument will be upgraded to an RV. That RV will be modified to point to | |
6447 | the new SV. The C<classname> argument indicates the package for the | |
6448 | blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV | |
6449 | will be returned and will have a reference count of 1. | |
6450 | ||
6451 | =cut | |
6452 | */ | |
6453 | ||
a0d0e21e | 6454 | SV* |
65202027 | 6455 | Perl_sv_setref_nv(pTHX_ SV *rv, const char *classname, NV nv) |
a0d0e21e LW |
6456 | { |
6457 | sv_setnv(newSVrv(rv,classname), nv); | |
6458 | return rv; | |
6459 | } | |
463ee0b2 | 6460 | |
954c1994 GS |
6461 | /* |
6462 | =for apidoc sv_setref_pvn | |
6463 | ||
6464 | Copies a string into a new SV, optionally blessing the SV. The length of the | |
6465 | string must be specified with C<n>. The C<rv> argument will be upgraded to | |
6466 | an RV. That RV will be modified to point to the new SV. The C<classname> | |
6467 | argument indicates the package for the blessing. Set C<classname> to | |
6468 | C<Nullch> to avoid the blessing. The new SV will be returned and will have | |
6469 | a reference count of 1. | |
6470 | ||
6471 | Note that C<sv_setref_pv> copies the pointer while this copies the string. | |
6472 | ||
6473 | =cut | |
6474 | */ | |
6475 | ||
a0d0e21e | 6476 | SV* |
864dbfa3 | 6477 | Perl_sv_setref_pvn(pTHX_ SV *rv, const char *classname, char *pv, STRLEN n) |
a0d0e21e LW |
6478 | { |
6479 | sv_setpvn(newSVrv(rv,classname), pv, n); | |
463ee0b2 LW |
6480 | return rv; |
6481 | } | |
6482 | ||
954c1994 GS |
6483 | /* |
6484 | =for apidoc sv_bless | |
6485 | ||
6486 | Blesses an SV into a specified package. The SV must be an RV. The package | |
6487 | must be designated by its stash (see C<gv_stashpv()>). The reference count | |
6488 | of the SV is unaffected. | |
6489 | ||
6490 | =cut | |
6491 | */ | |
6492 | ||
a0d0e21e | 6493 | SV* |
864dbfa3 | 6494 | Perl_sv_bless(pTHX_ SV *sv, HV *stash) |
a0d0e21e | 6495 | { |
76e3520e | 6496 | SV *tmpRef; |
a0d0e21e | 6497 | if (!SvROK(sv)) |
cea2e8a9 | 6498 | Perl_croak(aTHX_ "Can't bless non-reference value"); |
76e3520e GS |
6499 | tmpRef = SvRV(sv); |
6500 | if (SvFLAGS(tmpRef) & (SVs_OBJECT|SVf_READONLY)) { | |
6501 | if (SvREADONLY(tmpRef)) | |
cea2e8a9 | 6502 | Perl_croak(aTHX_ PL_no_modify); |
76e3520e GS |
6503 | if (SvOBJECT(tmpRef)) { |
6504 | if (SvTYPE(tmpRef) != SVt_PVIO) | |
3280af22 | 6505 | --PL_sv_objcount; |
76e3520e | 6506 | SvREFCNT_dec(SvSTASH(tmpRef)); |
2e3febc6 | 6507 | } |
a0d0e21e | 6508 | } |
76e3520e GS |
6509 | SvOBJECT_on(tmpRef); |
6510 | if (SvTYPE(tmpRef) != SVt_PVIO) | |
3280af22 | 6511 | ++PL_sv_objcount; |
76e3520e GS |
6512 | (void)SvUPGRADE(tmpRef, SVt_PVMG); |
6513 | SvSTASH(tmpRef) = (HV*)SvREFCNT_inc(stash); | |
a0d0e21e | 6514 | |
2e3febc6 CS |
6515 | if (Gv_AMG(stash)) |
6516 | SvAMAGIC_on(sv); | |
6517 | else | |
6518 | SvAMAGIC_off(sv); | |
a0d0e21e LW |
6519 | |
6520 | return sv; | |
6521 | } | |
6522 | ||
76e3520e | 6523 | STATIC void |
cea2e8a9 | 6524 | S_sv_unglob(pTHX_ SV *sv) |
a0d0e21e | 6525 | { |
850fabdf GS |
6526 | void *xpvmg; |
6527 | ||
a0d0e21e LW |
6528 | assert(SvTYPE(sv) == SVt_PVGV); |
6529 | SvFAKE_off(sv); | |
6530 | if (GvGP(sv)) | |
1edc1566 | 6531 | gp_free((GV*)sv); |
e826b3c7 GS |
6532 | if (GvSTASH(sv)) { |
6533 | SvREFCNT_dec(GvSTASH(sv)); | |
6534 | GvSTASH(sv) = Nullhv; | |
6535 | } | |
a0d0e21e LW |
6536 | sv_unmagic(sv, '*'); |
6537 | Safefree(GvNAME(sv)); | |
a5f75d66 | 6538 | GvMULTI_off(sv); |
850fabdf GS |
6539 | |
6540 | /* need to keep SvANY(sv) in the right arena */ | |
6541 | xpvmg = new_XPVMG(); | |
6542 | StructCopy(SvANY(sv), xpvmg, XPVMG); | |
6543 | del_XPVGV(SvANY(sv)); | |
6544 | SvANY(sv) = xpvmg; | |
6545 | ||
a0d0e21e LW |
6546 | SvFLAGS(sv) &= ~SVTYPEMASK; |
6547 | SvFLAGS(sv) |= SVt_PVMG; | |
6548 | } | |
6549 | ||
954c1994 | 6550 | /* |
840a7b70 | 6551 | =for apidoc sv_unref_flags |
954c1994 GS |
6552 | |
6553 | Unsets the RV status of the SV, and decrements the reference count of | |
6554 | whatever was being referenced by the RV. This can almost be thought of | |
840a7b70 IZ |
6555 | as a reversal of C<newSVrv>. The C<cflags> argument can contain |
6556 | C<SV_IMMEDIATE_UNREF> to force the reference count to be decremented | |
6557 | (otherwise the decrementing is conditional on the reference count being | |
6558 | different from one or the reference being a readonly SV). | |
7889fe52 | 6559 | See C<SvROK_off>. |
954c1994 GS |
6560 | |
6561 | =cut | |
6562 | */ | |
6563 | ||
ed6116ce | 6564 | void |
840a7b70 | 6565 | Perl_sv_unref_flags(pTHX_ SV *sv, U32 flags) |
ed6116ce | 6566 | { |
a0d0e21e | 6567 | SV* rv = SvRV(sv); |
810b8aa5 GS |
6568 | |
6569 | if (SvWEAKREF(sv)) { | |
6570 | sv_del_backref(sv); | |
6571 | SvWEAKREF_off(sv); | |
6572 | SvRV(sv) = 0; | |
6573 | return; | |
6574 | } | |
ed6116ce LW |
6575 | SvRV(sv) = 0; |
6576 | SvROK_off(sv); | |
840a7b70 | 6577 | if (SvREFCNT(rv) != 1 || SvREADONLY(rv) || flags) /* SV_IMMEDIATE_UNREF */ |
4633a7c4 | 6578 | SvREFCNT_dec(rv); |
840a7b70 | 6579 | else /* XXX Hack, but hard to make $a=$a->[1] work otherwise */ |
4633a7c4 | 6580 | sv_2mortal(rv); /* Schedule for freeing later */ |
ed6116ce | 6581 | } |
8990e307 | 6582 | |
840a7b70 IZ |
6583 | /* |
6584 | =for apidoc sv_unref | |
6585 | ||
6586 | Unsets the RV status of the SV, and decrements the reference count of | |
6587 | whatever was being referenced by the RV. This can almost be thought of | |
6588 | as a reversal of C<newSVrv>. This is C<sv_unref_flags> with the C<flag> | |
7889fe52 | 6589 | being zero. See C<SvROK_off>. |
840a7b70 IZ |
6590 | |
6591 | =cut | |
6592 | */ | |
6593 | ||
6594 | void | |
6595 | Perl_sv_unref(pTHX_ SV *sv) | |
6596 | { | |
6597 | sv_unref_flags(sv, 0); | |
6598 | } | |
6599 | ||
bbce6d69 | 6600 | void |
864dbfa3 | 6601 | Perl_sv_taint(pTHX_ SV *sv) |
bbce6d69 | 6602 | { |
6603 | sv_magic((sv), Nullsv, 't', Nullch, 0); | |
6604 | } | |
6605 | ||
6606 | void | |
864dbfa3 | 6607 | Perl_sv_untaint(pTHX_ SV *sv) |
bbce6d69 | 6608 | { |
13f57bf8 | 6609 | if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) { |
36477c24 | 6610 | MAGIC *mg = mg_find(sv, 't'); |
6611 | if (mg) | |
565764a8 | 6612 | mg->mg_len &= ~1; |
36477c24 | 6613 | } |
bbce6d69 | 6614 | } |
6615 | ||
6616 | bool | |
864dbfa3 | 6617 | Perl_sv_tainted(pTHX_ SV *sv) |
bbce6d69 | 6618 | { |
13f57bf8 | 6619 | if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) { |
36477c24 | 6620 | MAGIC *mg = mg_find(sv, 't'); |
155aba94 | 6621 | if (mg && ((mg->mg_len & 1) || ((mg->mg_len & 2) && mg->mg_obj == sv))) |
36477c24 | 6622 | return TRUE; |
6623 | } | |
6624 | return FALSE; | |
bbce6d69 | 6625 | } |
6626 | ||
954c1994 GS |
6627 | /* |
6628 | =for apidoc sv_setpviv | |
6629 | ||
6630 | Copies an integer into the given SV, also updating its string value. | |
6631 | Does not handle 'set' magic. See C<sv_setpviv_mg>. | |
6632 | ||
6633 | =cut | |
6634 | */ | |
6635 | ||
84902520 | 6636 | void |
864dbfa3 | 6637 | Perl_sv_setpviv(pTHX_ SV *sv, IV iv) |
84902520 | 6638 | { |
25da4f38 IZ |
6639 | char buf[TYPE_CHARS(UV)]; |
6640 | char *ebuf; | |
6641 | char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf); | |
84902520 | 6642 | |
25da4f38 | 6643 | sv_setpvn(sv, ptr, ebuf - ptr); |
84902520 TB |
6644 | } |
6645 | ||
ef50df4b | 6646 | |
954c1994 GS |
6647 | /* |
6648 | =for apidoc sv_setpviv_mg | |
6649 | ||
6650 | Like C<sv_setpviv>, but also handles 'set' magic. | |
6651 | ||
6652 | =cut | |
6653 | */ | |
6654 | ||
ef50df4b | 6655 | void |
864dbfa3 | 6656 | Perl_sv_setpviv_mg(pTHX_ SV *sv, IV iv) |
ef50df4b | 6657 | { |
25da4f38 IZ |
6658 | char buf[TYPE_CHARS(UV)]; |
6659 | char *ebuf; | |
6660 | char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf); | |
6661 | ||
6662 | sv_setpvn(sv, ptr, ebuf - ptr); | |
ef50df4b GS |
6663 | SvSETMAGIC(sv); |
6664 | } | |
6665 | ||
cea2e8a9 GS |
6666 | #if defined(PERL_IMPLICIT_CONTEXT) |
6667 | void | |
6668 | Perl_sv_setpvf_nocontext(SV *sv, const char* pat, ...) | |
6669 | { | |
6670 | dTHX; | |
6671 | va_list args; | |
6672 | va_start(args, pat); | |
c5be433b | 6673 | sv_vsetpvf(sv, pat, &args); |
cea2e8a9 GS |
6674 | va_end(args); |
6675 | } | |
6676 | ||
6677 | ||
6678 | void | |
6679 | Perl_sv_setpvf_mg_nocontext(SV *sv, const char* pat, ...) | |
6680 | { | |
6681 | dTHX; | |
6682 | va_list args; | |
6683 | va_start(args, pat); | |
c5be433b | 6684 | sv_vsetpvf_mg(sv, pat, &args); |
cea2e8a9 | 6685 | va_end(args); |
cea2e8a9 GS |
6686 | } |
6687 | #endif | |
6688 | ||
954c1994 GS |
6689 | /* |
6690 | =for apidoc sv_setpvf | |
6691 | ||
6692 | Processes its arguments like C<sprintf> and sets an SV to the formatted | |
6693 | output. Does not handle 'set' magic. See C<sv_setpvf_mg>. | |
6694 | ||
6695 | =cut | |
6696 | */ | |
6697 | ||
46fc3d4c | 6698 | void |
864dbfa3 | 6699 | Perl_sv_setpvf(pTHX_ SV *sv, const char* pat, ...) |
46fc3d4c | 6700 | { |
6701 | va_list args; | |
46fc3d4c | 6702 | va_start(args, pat); |
c5be433b | 6703 | sv_vsetpvf(sv, pat, &args); |
46fc3d4c | 6704 | va_end(args); |
6705 | } | |
6706 | ||
c5be433b GS |
6707 | void |
6708 | Perl_sv_vsetpvf(pTHX_ SV *sv, const char* pat, va_list* args) | |
6709 | { | |
6710 | sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
6711 | } | |
ef50df4b | 6712 | |
954c1994 GS |
6713 | /* |
6714 | =for apidoc sv_setpvf_mg | |
6715 | ||
6716 | Like C<sv_setpvf>, but also handles 'set' magic. | |
6717 | ||
6718 | =cut | |
6719 | */ | |
6720 | ||
ef50df4b | 6721 | void |
864dbfa3 | 6722 | Perl_sv_setpvf_mg(pTHX_ SV *sv, const char* pat, ...) |
ef50df4b GS |
6723 | { |
6724 | va_list args; | |
ef50df4b | 6725 | va_start(args, pat); |
c5be433b | 6726 | sv_vsetpvf_mg(sv, pat, &args); |
ef50df4b | 6727 | va_end(args); |
c5be433b GS |
6728 | } |
6729 | ||
6730 | void | |
6731 | Perl_sv_vsetpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args) | |
6732 | { | |
6733 | sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
ef50df4b GS |
6734 | SvSETMAGIC(sv); |
6735 | } | |
6736 | ||
cea2e8a9 GS |
6737 | #if defined(PERL_IMPLICIT_CONTEXT) |
6738 | void | |
6739 | Perl_sv_catpvf_nocontext(SV *sv, const char* pat, ...) | |
6740 | { | |
6741 | dTHX; | |
6742 | va_list args; | |
6743 | va_start(args, pat); | |
c5be433b | 6744 | sv_vcatpvf(sv, pat, &args); |
cea2e8a9 GS |
6745 | va_end(args); |
6746 | } | |
6747 | ||
6748 | void | |
6749 | Perl_sv_catpvf_mg_nocontext(SV *sv, const char* pat, ...) | |
6750 | { | |
6751 | dTHX; | |
6752 | va_list args; | |
6753 | va_start(args, pat); | |
c5be433b | 6754 | sv_vcatpvf_mg(sv, pat, &args); |
cea2e8a9 | 6755 | va_end(args); |
cea2e8a9 GS |
6756 | } |
6757 | #endif | |
6758 | ||
954c1994 GS |
6759 | /* |
6760 | =for apidoc sv_catpvf | |
6761 | ||
6762 | Processes its arguments like C<sprintf> and appends the formatted output | |
6763 | to an SV. Handles 'get' magic, but not 'set' magic. C<SvSETMAGIC()> must | |
6764 | typically be called after calling this function to handle 'set' magic. | |
6765 | ||
6766 | =cut | |
6767 | */ | |
6768 | ||
46fc3d4c | 6769 | void |
864dbfa3 | 6770 | Perl_sv_catpvf(pTHX_ SV *sv, const char* pat, ...) |
46fc3d4c | 6771 | { |
6772 | va_list args; | |
46fc3d4c | 6773 | va_start(args, pat); |
c5be433b | 6774 | sv_vcatpvf(sv, pat, &args); |
46fc3d4c | 6775 | va_end(args); |
6776 | } | |
6777 | ||
ef50df4b | 6778 | void |
c5be433b GS |
6779 | Perl_sv_vcatpvf(pTHX_ SV *sv, const char* pat, va_list* args) |
6780 | { | |
6781 | sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
6782 | } | |
6783 | ||
954c1994 GS |
6784 | /* |
6785 | =for apidoc sv_catpvf_mg | |
6786 | ||
6787 | Like C<sv_catpvf>, but also handles 'set' magic. | |
6788 | ||
6789 | =cut | |
6790 | */ | |
6791 | ||
c5be433b | 6792 | void |
864dbfa3 | 6793 | Perl_sv_catpvf_mg(pTHX_ SV *sv, const char* pat, ...) |
ef50df4b GS |
6794 | { |
6795 | va_list args; | |
ef50df4b | 6796 | va_start(args, pat); |
c5be433b | 6797 | sv_vcatpvf_mg(sv, pat, &args); |
ef50df4b | 6798 | va_end(args); |
c5be433b GS |
6799 | } |
6800 | ||
6801 | void | |
6802 | Perl_sv_vcatpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args) | |
6803 | { | |
6804 | sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
ef50df4b GS |
6805 | SvSETMAGIC(sv); |
6806 | } | |
6807 | ||
954c1994 GS |
6808 | /* |
6809 | =for apidoc sv_vsetpvfn | |
6810 | ||
6811 | Works like C<vcatpvfn> but copies the text into the SV instead of | |
6812 | appending it. | |
6813 | ||
6814 | =cut | |
6815 | */ | |
6816 | ||
46fc3d4c | 6817 | void |
7d5ea4e7 | 6818 | Perl_sv_vsetpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted) |
46fc3d4c | 6819 | { |
6820 | sv_setpvn(sv, "", 0); | |
7d5ea4e7 | 6821 | sv_vcatpvfn(sv, pat, patlen, args, svargs, svmax, maybe_tainted); |
46fc3d4c | 6822 | } |
6823 | ||
2d00ba3b | 6824 | STATIC I32 |
9dd79c3f | 6825 | S_expect_number(pTHX_ char** pattern) |
211dfcf1 HS |
6826 | { |
6827 | I32 var = 0; | |
6828 | switch (**pattern) { | |
6829 | case '1': case '2': case '3': | |
6830 | case '4': case '5': case '6': | |
6831 | case '7': case '8': case '9': | |
6832 | while (isDIGIT(**pattern)) | |
6833 | var = var * 10 + (*(*pattern)++ - '0'); | |
6834 | } | |
6835 | return var; | |
6836 | } | |
9dd79c3f | 6837 | #define EXPECT_NUMBER(pattern, var) (var = S_expect_number(aTHX_ &pattern)) |
211dfcf1 | 6838 | |
954c1994 GS |
6839 | /* |
6840 | =for apidoc sv_vcatpvfn | |
6841 | ||
6842 | Processes its arguments like C<vsprintf> and appends the formatted output | |
6843 | to an SV. Uses an array of SVs if the C style variable argument list is | |
6844 | missing (NULL). When running with taint checks enabled, indicates via | |
6845 | C<maybe_tainted> if results are untrustworthy (often due to the use of | |
6846 | locales). | |
6847 | ||
6848 | =cut | |
6849 | */ | |
6850 | ||
46fc3d4c | 6851 | void |
7d5ea4e7 | 6852 | Perl_sv_vcatpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted) |
46fc3d4c | 6853 | { |
6854 | char *p; | |
6855 | char *q; | |
6856 | char *patend; | |
fc36a67e | 6857 | STRLEN origlen; |
46fc3d4c | 6858 | I32 svix = 0; |
c635e13b | 6859 | static char nullstr[] = "(null)"; |
7e2040f0 | 6860 | SV *argsv; |
46fc3d4c | 6861 | |
6862 | /* no matter what, this is a string now */ | |
fc36a67e | 6863 | (void)SvPV_force(sv, origlen); |
46fc3d4c | 6864 | |
fc36a67e | 6865 | /* special-case "", "%s", and "%_" */ |
46fc3d4c | 6866 | if (patlen == 0) |
6867 | return; | |
fc36a67e | 6868 | if (patlen == 2 && pat[0] == '%') { |
6869 | switch (pat[1]) { | |
6870 | case 's': | |
c635e13b | 6871 | if (args) { |
6872 | char *s = va_arg(*args, char*); | |
6873 | sv_catpv(sv, s ? s : nullstr); | |
6874 | } | |
7e2040f0 | 6875 | else if (svix < svmax) { |
fc36a67e | 6876 | sv_catsv(sv, *svargs); |
7e2040f0 GS |
6877 | if (DO_UTF8(*svargs)) |
6878 | SvUTF8_on(sv); | |
6879 | } | |
fc36a67e | 6880 | return; |
6881 | case '_': | |
6882 | if (args) { | |
7e2040f0 GS |
6883 | argsv = va_arg(*args, SV*); |
6884 | sv_catsv(sv, argsv); | |
6885 | if (DO_UTF8(argsv)) | |
6886 | SvUTF8_on(sv); | |
fc36a67e | 6887 | return; |
6888 | } | |
6889 | /* See comment on '_' below */ | |
6890 | break; | |
6891 | } | |
46fc3d4c | 6892 | } |
6893 | ||
6894 | patend = (char*)pat + patlen; | |
6895 | for (p = (char*)pat; p < patend; p = q) { | |
6896 | bool alt = FALSE; | |
6897 | bool left = FALSE; | |
b22c7a20 | 6898 | bool vectorize = FALSE; |
211dfcf1 | 6899 | bool vectorarg = FALSE; |
b2e23cf9 | 6900 | bool vec_utf = FALSE; |
46fc3d4c | 6901 | char fill = ' '; |
6902 | char plus = 0; | |
6903 | char intsize = 0; | |
6904 | STRLEN width = 0; | |
fc36a67e | 6905 | STRLEN zeros = 0; |
46fc3d4c | 6906 | bool has_precis = FALSE; |
6907 | STRLEN precis = 0; | |
7e2040f0 | 6908 | bool is_utf = FALSE; |
eb3fce90 | 6909 | |
46fc3d4c | 6910 | char esignbuf[4]; |
ad391ad9 | 6911 | U8 utf8buf[UTF8_MAXLEN+1]; |
46fc3d4c | 6912 | STRLEN esignlen = 0; |
6913 | ||
6914 | char *eptr = Nullch; | |
fc36a67e | 6915 | STRLEN elen = 0; |
089c015b JH |
6916 | /* Times 4: a decimal digit takes more than 3 binary digits. |
6917 | * NV_DIG: mantissa takes than many decimal digits. | |
6918 | * Plus 32: Playing safe. */ | |
6919 | char ebuf[IV_DIG * 4 + NV_DIG + 32]; | |
2d4389e4 JH |
6920 | /* large enough for "%#.#f" --chip */ |
6921 | /* what about long double NVs? --jhi */ | |
b22c7a20 GS |
6922 | |
6923 | SV *vecsv; | |
a05b299f | 6924 | U8 *vecstr = Null(U8*); |
b22c7a20 | 6925 | STRLEN veclen = 0; |
46fc3d4c | 6926 | char c; |
6927 | int i; | |
6928 | unsigned base; | |
6929 | IV iv; | |
6930 | UV uv; | |
65202027 | 6931 | NV nv; |
46fc3d4c | 6932 | STRLEN have; |
6933 | STRLEN need; | |
6934 | STRLEN gap; | |
b22c7a20 GS |
6935 | char *dotstr = "."; |
6936 | STRLEN dotstrlen = 1; | |
211dfcf1 | 6937 | I32 efix = 0; /* explicit format parameter index */ |
eb3fce90 | 6938 | I32 ewix = 0; /* explicit width index */ |
211dfcf1 HS |
6939 | I32 epix = 0; /* explicit precision index */ |
6940 | I32 evix = 0; /* explicit vector index */ | |
eb3fce90 | 6941 | bool asterisk = FALSE; |
46fc3d4c | 6942 | |
211dfcf1 | 6943 | /* echo everything up to the next format specification */ |
46fc3d4c | 6944 | for (q = p; q < patend && *q != '%'; ++q) ; |
6945 | if (q > p) { | |
6946 | sv_catpvn(sv, p, q - p); | |
6947 | p = q; | |
6948 | } | |
6949 | if (q++ >= patend) | |
6950 | break; | |
6951 | ||
211dfcf1 HS |
6952 | /* |
6953 | We allow format specification elements in this order: | |
6954 | \d+\$ explicit format parameter index | |
6955 | [-+ 0#]+ flags | |
6956 | \*?(\d+\$)?v vector with optional (optionally specified) arg | |
6957 | \d+|\*(\d+\$)? width using optional (optionally specified) arg | |
6958 | \.(\d*|\*(\d+\$)?) precision using optional (optionally specified) arg | |
6959 | [hlqLV] size | |
6960 | [%bcdefginopsux_DFOUX] format (mandatory) | |
6961 | */ | |
6962 | if (EXPECT_NUMBER(q, width)) { | |
6963 | if (*q == '$') { | |
6964 | ++q; | |
6965 | efix = width; | |
6966 | } else { | |
6967 | goto gotwidth; | |
6968 | } | |
6969 | } | |
6970 | ||
fc36a67e | 6971 | /* FLAGS */ |
6972 | ||
46fc3d4c | 6973 | while (*q) { |
6974 | switch (*q) { | |
6975 | case ' ': | |
6976 | case '+': | |
6977 | plus = *q++; | |
6978 | continue; | |
6979 | ||
6980 | case '-': | |
6981 | left = TRUE; | |
6982 | q++; | |
6983 | continue; | |
6984 | ||
6985 | case '0': | |
6986 | fill = *q++; | |
6987 | continue; | |
6988 | ||
6989 | case '#': | |
6990 | alt = TRUE; | |
6991 | q++; | |
6992 | continue; | |
6993 | ||
fc36a67e | 6994 | default: |
6995 | break; | |
6996 | } | |
6997 | break; | |
6998 | } | |
46fc3d4c | 6999 | |
211dfcf1 | 7000 | tryasterisk: |
eb3fce90 | 7001 | if (*q == '*') { |
211dfcf1 HS |
7002 | q++; |
7003 | if (EXPECT_NUMBER(q, ewix)) | |
7004 | if (*q++ != '$') | |
7005 | goto unknown; | |
eb3fce90 | 7006 | asterisk = TRUE; |
211dfcf1 HS |
7007 | } |
7008 | if (*q == 'v') { | |
eb3fce90 | 7009 | q++; |
211dfcf1 HS |
7010 | if (vectorize) |
7011 | goto unknown; | |
9cbac4c7 | 7012 | if ((vectorarg = asterisk)) { |
211dfcf1 HS |
7013 | evix = ewix; |
7014 | ewix = 0; | |
7015 | asterisk = FALSE; | |
7016 | } | |
7017 | vectorize = TRUE; | |
7018 | goto tryasterisk; | |
eb3fce90 JH |
7019 | } |
7020 | ||
211dfcf1 HS |
7021 | if (!asterisk) |
7022 | EXPECT_NUMBER(q, width); | |
7023 | ||
7024 | if (vectorize) { | |
7025 | if (vectorarg) { | |
7026 | if (args) | |
7027 | vecsv = va_arg(*args, SV*); | |
7028 | else | |
7029 | vecsv = (evix ? evix <= svmax : svix < svmax) ? | |
7030 | svargs[ewix ? ewix-1 : svix++] : &PL_sv_undef; | |
4459522c | 7031 | dotstr = SvPVx(vecsv, dotstrlen); |
211dfcf1 HS |
7032 | if (DO_UTF8(vecsv)) |
7033 | is_utf = TRUE; | |
7034 | } | |
7035 | if (args) { | |
7036 | vecsv = va_arg(*args, SV*); | |
7037 | vecstr = (U8*)SvPVx(vecsv,veclen); | |
b2e23cf9 | 7038 | vec_utf = DO_UTF8(vecsv); |
eb3fce90 | 7039 | } |
211dfcf1 HS |
7040 | else if (efix ? efix <= svmax : svix < svmax) { |
7041 | vecsv = svargs[efix ? efix-1 : svix++]; | |
7042 | vecstr = (U8*)SvPVx(vecsv,veclen); | |
b2e23cf9 | 7043 | vec_utf = DO_UTF8(vecsv); |
211dfcf1 HS |
7044 | } |
7045 | else { | |
7046 | vecstr = (U8*)""; | |
7047 | veclen = 0; | |
7048 | } | |
eb3fce90 | 7049 | } |
fc36a67e | 7050 | |
eb3fce90 | 7051 | if (asterisk) { |
fc36a67e | 7052 | if (args) |
7053 | i = va_arg(*args, int); | |
7054 | else | |
eb3fce90 JH |
7055 | i = (ewix ? ewix <= svmax : svix < svmax) ? |
7056 | SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0; | |
fc36a67e | 7057 | left |= (i < 0); |
7058 | width = (i < 0) ? -i : i; | |
fc36a67e | 7059 | } |
211dfcf1 | 7060 | gotwidth: |
fc36a67e | 7061 | |
7062 | /* PRECISION */ | |
46fc3d4c | 7063 | |
fc36a67e | 7064 | if (*q == '.') { |
7065 | q++; | |
7066 | if (*q == '*') { | |
211dfcf1 HS |
7067 | q++; |
7068 | if (EXPECT_NUMBER(q, epix) && *q++ != '$') | |
7069 | goto unknown; | |
46fc3d4c | 7070 | if (args) |
7071 | i = va_arg(*args, int); | |
7072 | else | |
eb3fce90 JH |
7073 | i = (ewix ? ewix <= svmax : svix < svmax) |
7074 | ? SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0; | |
fc36a67e | 7075 | precis = (i < 0) ? 0 : i; |
fc36a67e | 7076 | } |
7077 | else { | |
7078 | precis = 0; | |
7079 | while (isDIGIT(*q)) | |
7080 | precis = precis * 10 + (*q++ - '0'); | |
7081 | } | |
7082 | has_precis = TRUE; | |
7083 | } | |
46fc3d4c | 7084 | |
fc36a67e | 7085 | /* SIZE */ |
46fc3d4c | 7086 | |
fc36a67e | 7087 | switch (*q) { |
e5c81feb | 7088 | #if defined(HAS_QUAD) || (defined(HAS_LONG_DOUBLE) && defined(USE_LONG_DOUBLE)) |
6f9bb7fd | 7089 | case 'L': /* Ld */ |
e5c81feb JH |
7090 | /* FALL THROUGH */ |
7091 | #endif | |
7092 | #ifdef HAS_QUAD | |
6f9bb7fd GS |
7093 | case 'q': /* qd */ |
7094 | intsize = 'q'; | |
7095 | q++; | |
7096 | break; | |
7097 | #endif | |
fc36a67e | 7098 | case 'l': |
e5c81feb JH |
7099 | #if defined(HAS_QUAD) || (defined(HAS_LONG_DOUBLE) && defined(USE_LONG_DOUBLE)) |
7100 | if (*(q + 1) == 'l') { /* lld, llf */ | |
fc36a67e | 7101 | intsize = 'q'; |
7102 | q += 2; | |
46fc3d4c | 7103 | break; |
cf2093f6 | 7104 | } |
fc36a67e | 7105 | #endif |
6f9bb7fd | 7106 | /* FALL THROUGH */ |
fc36a67e | 7107 | case 'h': |
cf2093f6 | 7108 | /* FALL THROUGH */ |
fc36a67e | 7109 | case 'V': |
7110 | intsize = *q++; | |
46fc3d4c | 7111 | break; |
7112 | } | |
7113 | ||
fc36a67e | 7114 | /* CONVERSION */ |
7115 | ||
211dfcf1 HS |
7116 | if (*q == '%') { |
7117 | eptr = q++; | |
7118 | elen = 1; | |
7119 | goto string; | |
7120 | } | |
7121 | ||
7122 | if (!args) | |
7123 | argsv = (efix ? efix <= svmax : svix < svmax) ? | |
7124 | svargs[efix ? efix-1 : svix++] : &PL_sv_undef; | |
7125 | ||
46fc3d4c | 7126 | switch (c = *q++) { |
7127 | ||
7128 | /* STRINGS */ | |
7129 | ||
46fc3d4c | 7130 | case 'c': |
211dfcf1 | 7131 | uv = args ? va_arg(*args, int) : SvIVx(argsv); |
c4d5f83a | 7132 | if ((uv > 255 || (!UNI_IS_INVARIANT(uv) || SvUTF8(sv))) && !IN_BYTE) { |
dfe13c55 | 7133 | eptr = (char*)utf8buf; |
9041c2e3 | 7134 | elen = uvchr_to_utf8((U8*)eptr, uv) - utf8buf; |
7e2040f0 GS |
7135 | is_utf = TRUE; |
7136 | } | |
7137 | else { | |
7138 | c = (char)uv; | |
7139 | eptr = &c; | |
7140 | elen = 1; | |
a0ed51b3 | 7141 | } |
46fc3d4c | 7142 | goto string; |
7143 | ||
46fc3d4c | 7144 | case 's': |
7145 | if (args) { | |
fc36a67e | 7146 | eptr = va_arg(*args, char*); |
c635e13b | 7147 | if (eptr) |
1d7c1841 GS |
7148 | #ifdef MACOS_TRADITIONAL |
7149 | /* On MacOS, %#s format is used for Pascal strings */ | |
7150 | if (alt) | |
7151 | elen = *eptr++; | |
7152 | else | |
7153 | #endif | |
c635e13b | 7154 | elen = strlen(eptr); |
7155 | else { | |
7156 | eptr = nullstr; | |
7157 | elen = sizeof nullstr - 1; | |
7158 | } | |
46fc3d4c | 7159 | } |
211dfcf1 | 7160 | else { |
7e2040f0 GS |
7161 | eptr = SvPVx(argsv, elen); |
7162 | if (DO_UTF8(argsv)) { | |
a0ed51b3 LW |
7163 | if (has_precis && precis < elen) { |
7164 | I32 p = precis; | |
7e2040f0 | 7165 | sv_pos_u2b(argsv, &p, 0); /* sticks at end */ |
a0ed51b3 LW |
7166 | precis = p; |
7167 | } | |
7168 | if (width) { /* fudge width (can't fudge elen) */ | |
7e2040f0 | 7169 | width += elen - sv_len_utf8(argsv); |
a0ed51b3 | 7170 | } |
7e2040f0 | 7171 | is_utf = TRUE; |
a0ed51b3 LW |
7172 | } |
7173 | } | |
46fc3d4c | 7174 | goto string; |
7175 | ||
fc36a67e | 7176 | case '_': |
7177 | /* | |
7178 | * The "%_" hack might have to be changed someday, | |
7179 | * if ISO or ANSI decide to use '_' for something. | |
7180 | * So we keep it hidden from users' code. | |
7181 | */ | |
7182 | if (!args) | |
7183 | goto unknown; | |
211dfcf1 | 7184 | argsv = va_arg(*args, SV*); |
7e2040f0 GS |
7185 | eptr = SvPVx(argsv, elen); |
7186 | if (DO_UTF8(argsv)) | |
7187 | is_utf = TRUE; | |
fc36a67e | 7188 | |
46fc3d4c | 7189 | string: |
b22c7a20 | 7190 | vectorize = FALSE; |
46fc3d4c | 7191 | if (has_precis && elen > precis) |
7192 | elen = precis; | |
7193 | break; | |
7194 | ||
7195 | /* INTEGERS */ | |
7196 | ||
fc36a67e | 7197 | case 'p': |
c2e66d9e GS |
7198 | if (alt) |
7199 | goto unknown; | |
211dfcf1 | 7200 | uv = PTR2UV(args ? va_arg(*args, void*) : argsv); |
fc36a67e | 7201 | base = 16; |
7202 | goto integer; | |
7203 | ||
46fc3d4c | 7204 | case 'D': |
29fe7a80 | 7205 | #ifdef IV_IS_QUAD |
22f3ae8c | 7206 | intsize = 'q'; |
29fe7a80 | 7207 | #else |
46fc3d4c | 7208 | intsize = 'l'; |
29fe7a80 | 7209 | #endif |
46fc3d4c | 7210 | /* FALL THROUGH */ |
7211 | case 'd': | |
7212 | case 'i': | |
b22c7a20 | 7213 | if (vectorize) { |
ba210ebe | 7214 | STRLEN ulen; |
211dfcf1 HS |
7215 | if (!veclen) |
7216 | continue; | |
b2e23cf9 | 7217 | if (vec_utf) |
9041c2e3 | 7218 | iv = (IV)utf8n_to_uvchr(vecstr, veclen, &ulen, 0); |
b22c7a20 | 7219 | else { |
a05b299f | 7220 | iv = *vecstr; |
b22c7a20 GS |
7221 | ulen = 1; |
7222 | } | |
7223 | vecstr += ulen; | |
7224 | veclen -= ulen; | |
7225 | } | |
7226 | else if (args) { | |
46fc3d4c | 7227 | switch (intsize) { |
7228 | case 'h': iv = (short)va_arg(*args, int); break; | |
7229 | default: iv = va_arg(*args, int); break; | |
7230 | case 'l': iv = va_arg(*args, long); break; | |
fc36a67e | 7231 | case 'V': iv = va_arg(*args, IV); break; |
cf2093f6 JH |
7232 | #ifdef HAS_QUAD |
7233 | case 'q': iv = va_arg(*args, Quad_t); break; | |
7234 | #endif | |
46fc3d4c | 7235 | } |
7236 | } | |
7237 | else { | |
211dfcf1 | 7238 | iv = SvIVx(argsv); |
46fc3d4c | 7239 | switch (intsize) { |
7240 | case 'h': iv = (short)iv; break; | |
be28567c | 7241 | default: break; |
46fc3d4c | 7242 | case 'l': iv = (long)iv; break; |
fc36a67e | 7243 | case 'V': break; |
cf2093f6 JH |
7244 | #ifdef HAS_QUAD |
7245 | case 'q': iv = (Quad_t)iv; break; | |
7246 | #endif | |
46fc3d4c | 7247 | } |
7248 | } | |
7249 | if (iv >= 0) { | |
7250 | uv = iv; | |
7251 | if (plus) | |
7252 | esignbuf[esignlen++] = plus; | |
7253 | } | |
7254 | else { | |
7255 | uv = -iv; | |
7256 | esignbuf[esignlen++] = '-'; | |
7257 | } | |
7258 | base = 10; | |
7259 | goto integer; | |
7260 | ||
fc36a67e | 7261 | case 'U': |
29fe7a80 | 7262 | #ifdef IV_IS_QUAD |
22f3ae8c | 7263 | intsize = 'q'; |
29fe7a80 | 7264 | #else |
fc36a67e | 7265 | intsize = 'l'; |
29fe7a80 | 7266 | #endif |
fc36a67e | 7267 | /* FALL THROUGH */ |
7268 | case 'u': | |
7269 | base = 10; | |
7270 | goto uns_integer; | |
7271 | ||
4f19785b WSI |
7272 | case 'b': |
7273 | base = 2; | |
7274 | goto uns_integer; | |
7275 | ||
46fc3d4c | 7276 | case 'O': |
29fe7a80 | 7277 | #ifdef IV_IS_QUAD |
22f3ae8c | 7278 | intsize = 'q'; |
29fe7a80 | 7279 | #else |
46fc3d4c | 7280 | intsize = 'l'; |
29fe7a80 | 7281 | #endif |
46fc3d4c | 7282 | /* FALL THROUGH */ |
7283 | case 'o': | |
7284 | base = 8; | |
7285 | goto uns_integer; | |
7286 | ||
7287 | case 'X': | |
46fc3d4c | 7288 | case 'x': |
7289 | base = 16; | |
46fc3d4c | 7290 | |
7291 | uns_integer: | |
b22c7a20 | 7292 | if (vectorize) { |
ba210ebe | 7293 | STRLEN ulen; |
b22c7a20 | 7294 | vector: |
211dfcf1 HS |
7295 | if (!veclen) |
7296 | continue; | |
b2e23cf9 | 7297 | if (vec_utf) |
9041c2e3 | 7298 | uv = utf8n_to_uvchr(vecstr, veclen, &ulen, 0); |
b22c7a20 | 7299 | else { |
a05b299f | 7300 | uv = *vecstr; |
b22c7a20 GS |
7301 | ulen = 1; |
7302 | } | |
7303 | vecstr += ulen; | |
7304 | veclen -= ulen; | |
7305 | } | |
7306 | else if (args) { | |
46fc3d4c | 7307 | switch (intsize) { |
7308 | case 'h': uv = (unsigned short)va_arg(*args, unsigned); break; | |
7309 | default: uv = va_arg(*args, unsigned); break; | |
7310 | case 'l': uv = va_arg(*args, unsigned long); break; | |
fc36a67e | 7311 | case 'V': uv = va_arg(*args, UV); break; |
cf2093f6 JH |
7312 | #ifdef HAS_QUAD |
7313 | case 'q': uv = va_arg(*args, Quad_t); break; | |
7314 | #endif | |
46fc3d4c | 7315 | } |
7316 | } | |
7317 | else { | |
211dfcf1 | 7318 | uv = SvUVx(argsv); |
46fc3d4c | 7319 | switch (intsize) { |
7320 | case 'h': uv = (unsigned short)uv; break; | |
be28567c | 7321 | default: break; |
46fc3d4c | 7322 | case 'l': uv = (unsigned long)uv; break; |
fc36a67e | 7323 | case 'V': break; |
cf2093f6 JH |
7324 | #ifdef HAS_QUAD |
7325 | case 'q': uv = (Quad_t)uv; break; | |
7326 | #endif | |
46fc3d4c | 7327 | } |
7328 | } | |
7329 | ||
7330 | integer: | |
46fc3d4c | 7331 | eptr = ebuf + sizeof ebuf; |
fc36a67e | 7332 | switch (base) { |
7333 | unsigned dig; | |
7334 | case 16: | |
c10ed8b9 HS |
7335 | if (!uv) |
7336 | alt = FALSE; | |
1d7c1841 GS |
7337 | p = (char*)((c == 'X') |
7338 | ? "0123456789ABCDEF" : "0123456789abcdef"); | |
fc36a67e | 7339 | do { |
7340 | dig = uv & 15; | |
7341 | *--eptr = p[dig]; | |
7342 | } while (uv >>= 4); | |
7343 | if (alt) { | |
46fc3d4c | 7344 | esignbuf[esignlen++] = '0'; |
fc36a67e | 7345 | esignbuf[esignlen++] = c; /* 'x' or 'X' */ |
46fc3d4c | 7346 | } |
fc36a67e | 7347 | break; |
7348 | case 8: | |
7349 | do { | |
7350 | dig = uv & 7; | |
7351 | *--eptr = '0' + dig; | |
7352 | } while (uv >>= 3); | |
7353 | if (alt && *eptr != '0') | |
7354 | *--eptr = '0'; | |
7355 | break; | |
4f19785b WSI |
7356 | case 2: |
7357 | do { | |
7358 | dig = uv & 1; | |
7359 | *--eptr = '0' + dig; | |
7360 | } while (uv >>= 1); | |
eda88b6d JH |
7361 | if (alt) { |
7362 | esignbuf[esignlen++] = '0'; | |
7481bb52 | 7363 | esignbuf[esignlen++] = 'b'; |
eda88b6d | 7364 | } |
4f19785b | 7365 | break; |
fc36a67e | 7366 | default: /* it had better be ten or less */ |
6bc102ca | 7367 | #if defined(PERL_Y2KWARN) |
e476b1b5 | 7368 | if (ckWARN(WARN_Y2K)) { |
6bc102ca GS |
7369 | STRLEN n; |
7370 | char *s = SvPV(sv,n); | |
7371 | if (n >= 2 && s[n-2] == '1' && s[n-1] == '9' | |
7372 | && (n == 2 || !isDIGIT(s[n-3]))) | |
7373 | { | |
e476b1b5 | 7374 | Perl_warner(aTHX_ WARN_Y2K, |
6bc102ca GS |
7375 | "Possible Y2K bug: %%%c %s", |
7376 | c, "format string following '19'"); | |
7377 | } | |
7378 | } | |
7379 | #endif | |
fc36a67e | 7380 | do { |
7381 | dig = uv % base; | |
7382 | *--eptr = '0' + dig; | |
7383 | } while (uv /= base); | |
7384 | break; | |
46fc3d4c | 7385 | } |
7386 | elen = (ebuf + sizeof ebuf) - eptr; | |
c10ed8b9 HS |
7387 | if (has_precis) { |
7388 | if (precis > elen) | |
7389 | zeros = precis - elen; | |
7390 | else if (precis == 0 && elen == 1 && *eptr == '0') | |
7391 | elen = 0; | |
7392 | } | |
46fc3d4c | 7393 | break; |
7394 | ||
7395 | /* FLOATING POINT */ | |
7396 | ||
fc36a67e | 7397 | case 'F': |
7398 | c = 'f'; /* maybe %F isn't supported here */ | |
7399 | /* FALL THROUGH */ | |
46fc3d4c | 7400 | case 'e': case 'E': |
fc36a67e | 7401 | case 'f': |
46fc3d4c | 7402 | case 'g': case 'G': |
7403 | ||
7404 | /* This is evil, but floating point is even more evil */ | |
7405 | ||
b22c7a20 | 7406 | vectorize = FALSE; |
211dfcf1 | 7407 | nv = args ? va_arg(*args, NV) : SvNVx(argsv); |
fc36a67e | 7408 | |
7409 | need = 0; | |
7410 | if (c != 'e' && c != 'E') { | |
7411 | i = PERL_INT_MIN; | |
73b309ea | 7412 | (void)Perl_frexp(nv, &i); |
fc36a67e | 7413 | if (i == PERL_INT_MIN) |
cea2e8a9 | 7414 | Perl_die(aTHX_ "panic: frexp"); |
c635e13b | 7415 | if (i > 0) |
fc36a67e | 7416 | need = BIT_DIGITS(i); |
7417 | } | |
7418 | need += has_precis ? precis : 6; /* known default */ | |
7419 | if (need < width) | |
7420 | need = width; | |
7421 | ||
46fc3d4c | 7422 | need += 20; /* fudge factor */ |
80252599 GS |
7423 | if (PL_efloatsize < need) { |
7424 | Safefree(PL_efloatbuf); | |
7425 | PL_efloatsize = need + 20; /* more fudge */ | |
7426 | New(906, PL_efloatbuf, PL_efloatsize, char); | |
7d5ea4e7 | 7427 | PL_efloatbuf[0] = '\0'; |
46fc3d4c | 7428 | } |
7429 | ||
7430 | eptr = ebuf + sizeof ebuf; | |
7431 | *--eptr = '\0'; | |
7432 | *--eptr = c; | |
e5c81feb | 7433 | #if defined(USE_LONG_DOUBLE) && defined(PERL_PRIfldbl) |
cf2093f6 | 7434 | { |
e5c81feb JH |
7435 | /* Copy the one or more characters in a long double |
7436 | * format before the 'base' ([efgEFG]) character to | |
7437 | * the format string. */ | |
7438 | static char const prifldbl[] = PERL_PRIfldbl; | |
7439 | char const *p = prifldbl + sizeof(prifldbl) - 3; | |
7440 | while (p >= prifldbl) { *--eptr = *p--; } | |
cf2093f6 | 7441 | } |
65202027 | 7442 | #endif |
46fc3d4c | 7443 | if (has_precis) { |
7444 | base = precis; | |
7445 | do { *--eptr = '0' + (base % 10); } while (base /= 10); | |
7446 | *--eptr = '.'; | |
7447 | } | |
7448 | if (width) { | |
7449 | base = width; | |
7450 | do { *--eptr = '0' + (base % 10); } while (base /= 10); | |
7451 | } | |
7452 | if (fill == '0') | |
7453 | *--eptr = fill; | |
84902520 TB |
7454 | if (left) |
7455 | *--eptr = '-'; | |
46fc3d4c | 7456 | if (plus) |
7457 | *--eptr = plus; | |
7458 | if (alt) | |
7459 | *--eptr = '#'; | |
7460 | *--eptr = '%'; | |
7461 | ||
ff9121f8 JH |
7462 | /* No taint. Otherwise we are in the strange situation |
7463 | * where printf() taints but print($float) doesn't. | |
bda0f7a5 | 7464 | * --jhi */ |
dd8482fc | 7465 | (void)sprintf(PL_efloatbuf, eptr, nv); |
8af02333 | 7466 | |
80252599 GS |
7467 | eptr = PL_efloatbuf; |
7468 | elen = strlen(PL_efloatbuf); | |
46fc3d4c | 7469 | break; |
7470 | ||
fc36a67e | 7471 | /* SPECIAL */ |
7472 | ||
7473 | case 'n': | |
b22c7a20 | 7474 | vectorize = FALSE; |
fc36a67e | 7475 | i = SvCUR(sv) - origlen; |
7476 | if (args) { | |
c635e13b | 7477 | switch (intsize) { |
7478 | case 'h': *(va_arg(*args, short*)) = i; break; | |
7479 | default: *(va_arg(*args, int*)) = i; break; | |
7480 | case 'l': *(va_arg(*args, long*)) = i; break; | |
7481 | case 'V': *(va_arg(*args, IV*)) = i; break; | |
cf2093f6 JH |
7482 | #ifdef HAS_QUAD |
7483 | case 'q': *(va_arg(*args, Quad_t*)) = i; break; | |
7484 | #endif | |
c635e13b | 7485 | } |
fc36a67e | 7486 | } |
9dd79c3f | 7487 | else |
211dfcf1 | 7488 | sv_setuv_mg(argsv, (UV)i); |
fc36a67e | 7489 | continue; /* not "break" */ |
7490 | ||
7491 | /* UNKNOWN */ | |
7492 | ||
46fc3d4c | 7493 | default: |
fc36a67e | 7494 | unknown: |
b22c7a20 | 7495 | vectorize = FALSE; |
599cee73 | 7496 | if (!args && ckWARN(WARN_PRINTF) && |
533c011a | 7497 | (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF)) { |
c635e13b | 7498 | SV *msg = sv_newmortal(); |
cea2e8a9 | 7499 | Perl_sv_setpvf(aTHX_ msg, "Invalid conversion in %s: ", |
533c011a | 7500 | (PL_op->op_type == OP_PRTF) ? "printf" : "sprintf"); |
0f4b6630 | 7501 | if (c) { |
0f4b6630 | 7502 | if (isPRINT(c)) |
1c846c1f | 7503 | Perl_sv_catpvf(aTHX_ msg, |
0f4b6630 JH |
7504 | "\"%%%c\"", c & 0xFF); |
7505 | else | |
7506 | Perl_sv_catpvf(aTHX_ msg, | |
57def98f | 7507 | "\"%%\\%03"UVof"\"", |
0f4b6630 | 7508 | (UV)c & 0xFF); |
0f4b6630 | 7509 | } else |
c635e13b | 7510 | sv_catpv(msg, "end of string"); |
894356b3 | 7511 | Perl_warner(aTHX_ WARN_PRINTF, "%"SVf, msg); /* yes, this is reentrant */ |
c635e13b | 7512 | } |
fb73857a | 7513 | |
7514 | /* output mangled stuff ... */ | |
7515 | if (c == '\0') | |
7516 | --q; | |
46fc3d4c | 7517 | eptr = p; |
7518 | elen = q - p; | |
fb73857a | 7519 | |
7520 | /* ... right here, because formatting flags should not apply */ | |
7521 | SvGROW(sv, SvCUR(sv) + elen + 1); | |
7522 | p = SvEND(sv); | |
4459522c | 7523 | Copy(eptr, p, elen, char); |
fb73857a | 7524 | p += elen; |
7525 | *p = '\0'; | |
7526 | SvCUR(sv) = p - SvPVX(sv); | |
7527 | continue; /* not "break" */ | |
46fc3d4c | 7528 | } |
7529 | ||
fc36a67e | 7530 | have = esignlen + zeros + elen; |
46fc3d4c | 7531 | need = (have > width ? have : width); |
7532 | gap = need - have; | |
7533 | ||
b22c7a20 | 7534 | SvGROW(sv, SvCUR(sv) + need + dotstrlen + 1); |
46fc3d4c | 7535 | p = SvEND(sv); |
7536 | if (esignlen && fill == '0') { | |
7537 | for (i = 0; i < esignlen; i++) | |
7538 | *p++ = esignbuf[i]; | |
7539 | } | |
7540 | if (gap && !left) { | |
7541 | memset(p, fill, gap); | |
7542 | p += gap; | |
7543 | } | |
7544 | if (esignlen && fill != '0') { | |
7545 | for (i = 0; i < esignlen; i++) | |
7546 | *p++ = esignbuf[i]; | |
7547 | } | |
fc36a67e | 7548 | if (zeros) { |
7549 | for (i = zeros; i; i--) | |
7550 | *p++ = '0'; | |
7551 | } | |
46fc3d4c | 7552 | if (elen) { |
4459522c | 7553 | Copy(eptr, p, elen, char); |
46fc3d4c | 7554 | p += elen; |
7555 | } | |
7556 | if (gap && left) { | |
7557 | memset(p, ' ', gap); | |
7558 | p += gap; | |
7559 | } | |
b22c7a20 GS |
7560 | if (vectorize) { |
7561 | if (veclen) { | |
4459522c | 7562 | Copy(dotstr, p, dotstrlen, char); |
b22c7a20 GS |
7563 | p += dotstrlen; |
7564 | } | |
7565 | else | |
7566 | vectorize = FALSE; /* done iterating over vecstr */ | |
7567 | } | |
7e2040f0 GS |
7568 | if (is_utf) |
7569 | SvUTF8_on(sv); | |
46fc3d4c | 7570 | *p = '\0'; |
7571 | SvCUR(sv) = p - SvPVX(sv); | |
b22c7a20 GS |
7572 | if (vectorize) { |
7573 | esignlen = 0; | |
7574 | goto vector; | |
7575 | } | |
46fc3d4c | 7576 | } |
7577 | } | |
51371543 | 7578 | |
1d7c1841 GS |
7579 | #if defined(USE_ITHREADS) |
7580 | ||
7581 | #if defined(USE_THREADS) | |
7582 | # include "error: USE_THREADS and USE_ITHREADS are incompatible" | |
7583 | #endif | |
7584 | ||
1d7c1841 GS |
7585 | #ifndef GpREFCNT_inc |
7586 | # define GpREFCNT_inc(gp) ((gp) ? (++(gp)->gp_refcnt, (gp)) : (GP*)NULL) | |
7587 | #endif | |
7588 | ||
7589 | ||
7590 | #define sv_dup_inc(s) SvREFCNT_inc(sv_dup(s)) | |
7591 | #define av_dup(s) (AV*)sv_dup((SV*)s) | |
7592 | #define av_dup_inc(s) (AV*)SvREFCNT_inc(sv_dup((SV*)s)) | |
7593 | #define hv_dup(s) (HV*)sv_dup((SV*)s) | |
7594 | #define hv_dup_inc(s) (HV*)SvREFCNT_inc(sv_dup((SV*)s)) | |
7595 | #define cv_dup(s) (CV*)sv_dup((SV*)s) | |
7596 | #define cv_dup_inc(s) (CV*)SvREFCNT_inc(sv_dup((SV*)s)) | |
7597 | #define io_dup(s) (IO*)sv_dup((SV*)s) | |
7598 | #define io_dup_inc(s) (IO*)SvREFCNT_inc(sv_dup((SV*)s)) | |
7599 | #define gv_dup(s) (GV*)sv_dup((SV*)s) | |
7600 | #define gv_dup_inc(s) (GV*)SvREFCNT_inc(sv_dup((SV*)s)) | |
7601 | #define SAVEPV(p) (p ? savepv(p) : Nullch) | |
7602 | #define SAVEPVN(p,n) (p ? savepvn(p,n) : Nullch) | |
7603 | ||
7604 | REGEXP * | |
7605 | Perl_re_dup(pTHX_ REGEXP *r) | |
7606 | { | |
7607 | /* XXX fix when pmop->op_pmregexp becomes shared */ | |
7608 | return ReREFCNT_inc(r); | |
7609 | } | |
7610 | ||
7611 | PerlIO * | |
7612 | Perl_fp_dup(pTHX_ PerlIO *fp, char type) | |
7613 | { | |
7614 | PerlIO *ret; | |
7615 | if (!fp) | |
7616 | return (PerlIO*)NULL; | |
7617 | ||
7618 | /* look for it in the table first */ | |
7619 | ret = (PerlIO*)ptr_table_fetch(PL_ptr_table, fp); | |
7620 | if (ret) | |
7621 | return ret; | |
7622 | ||
7623 | /* create anew and remember what it is */ | |
5f1a76d0 | 7624 | ret = PerlIO_fdupopen(aTHX_ fp); |
1d7c1841 GS |
7625 | ptr_table_store(PL_ptr_table, fp, ret); |
7626 | return ret; | |
7627 | } | |
7628 | ||
7629 | DIR * | |
7630 | Perl_dirp_dup(pTHX_ DIR *dp) | |
7631 | { | |
7632 | if (!dp) | |
7633 | return (DIR*)NULL; | |
7634 | /* XXX TODO */ | |
7635 | return dp; | |
7636 | } | |
7637 | ||
7638 | GP * | |
7639 | Perl_gp_dup(pTHX_ GP *gp) | |
7640 | { | |
7641 | GP *ret; | |
7642 | if (!gp) | |
7643 | return (GP*)NULL; | |
7644 | /* look for it in the table first */ | |
7645 | ret = (GP*)ptr_table_fetch(PL_ptr_table, gp); | |
7646 | if (ret) | |
7647 | return ret; | |
7648 | ||
7649 | /* create anew and remember what it is */ | |
7650 | Newz(0, ret, 1, GP); | |
7651 | ptr_table_store(PL_ptr_table, gp, ret); | |
7652 | ||
7653 | /* clone */ | |
7654 | ret->gp_refcnt = 0; /* must be before any other dups! */ | |
7655 | ret->gp_sv = sv_dup_inc(gp->gp_sv); | |
7656 | ret->gp_io = io_dup_inc(gp->gp_io); | |
7657 | ret->gp_form = cv_dup_inc(gp->gp_form); | |
7658 | ret->gp_av = av_dup_inc(gp->gp_av); | |
7659 | ret->gp_hv = hv_dup_inc(gp->gp_hv); | |
7660 | ret->gp_egv = gv_dup(gp->gp_egv); /* GvEGV is not refcounted */ | |
7661 | ret->gp_cv = cv_dup_inc(gp->gp_cv); | |
7662 | ret->gp_cvgen = gp->gp_cvgen; | |
7663 | ret->gp_flags = gp->gp_flags; | |
7664 | ret->gp_line = gp->gp_line; | |
7665 | ret->gp_file = gp->gp_file; /* points to COP.cop_file */ | |
7666 | return ret; | |
7667 | } | |
7668 | ||
7669 | MAGIC * | |
7670 | Perl_mg_dup(pTHX_ MAGIC *mg) | |
7671 | { | |
7672 | MAGIC *mgret = (MAGIC*)NULL; | |
7673 | MAGIC *mgprev; | |
7674 | if (!mg) | |
7675 | return (MAGIC*)NULL; | |
7676 | /* look for it in the table first */ | |
7677 | mgret = (MAGIC*)ptr_table_fetch(PL_ptr_table, mg); | |
7678 | if (mgret) | |
7679 | return mgret; | |
7680 | ||
7681 | for (; mg; mg = mg->mg_moremagic) { | |
7682 | MAGIC *nmg; | |
7683 | Newz(0, nmg, 1, MAGIC); | |
7684 | if (!mgret) | |
7685 | mgret = nmg; | |
7686 | else | |
7687 | mgprev->mg_moremagic = nmg; | |
7688 | nmg->mg_virtual = mg->mg_virtual; /* XXX copy dynamic vtable? */ | |
7689 | nmg->mg_private = mg->mg_private; | |
7690 | nmg->mg_type = mg->mg_type; | |
7691 | nmg->mg_flags = mg->mg_flags; | |
7692 | if (mg->mg_type == 'r') { | |
7693 | nmg->mg_obj = (SV*)re_dup((REGEXP*)mg->mg_obj); | |
7694 | } | |
7695 | else { | |
7696 | nmg->mg_obj = (mg->mg_flags & MGf_REFCOUNTED) | |
7697 | ? sv_dup_inc(mg->mg_obj) | |
7698 | : sv_dup(mg->mg_obj); | |
7699 | } | |
7700 | nmg->mg_len = mg->mg_len; | |
7701 | nmg->mg_ptr = mg->mg_ptr; /* XXX random ptr? */ | |
7702 | if (mg->mg_ptr && mg->mg_type != 'g') { | |
7703 | if (mg->mg_len >= 0) { | |
7704 | nmg->mg_ptr = SAVEPVN(mg->mg_ptr, mg->mg_len); | |
7705 | if (mg->mg_type == 'c' && AMT_AMAGIC((AMT*)mg->mg_ptr)) { | |
7706 | AMT *amtp = (AMT*)mg->mg_ptr; | |
7707 | AMT *namtp = (AMT*)nmg->mg_ptr; | |
7708 | I32 i; | |
7709 | for (i = 1; i < NofAMmeth; i++) { | |
7710 | namtp->table[i] = cv_dup_inc(amtp->table[i]); | |
7711 | } | |
7712 | } | |
7713 | } | |
7714 | else if (mg->mg_len == HEf_SVKEY) | |
7715 | nmg->mg_ptr = (char*)sv_dup_inc((SV*)mg->mg_ptr); | |
7716 | } | |
7717 | mgprev = nmg; | |
7718 | } | |
7719 | return mgret; | |
7720 | } | |
7721 | ||
7722 | PTR_TBL_t * | |
7723 | Perl_ptr_table_new(pTHX) | |
7724 | { | |
7725 | PTR_TBL_t *tbl; | |
7726 | Newz(0, tbl, 1, PTR_TBL_t); | |
7727 | tbl->tbl_max = 511; | |
7728 | tbl->tbl_items = 0; | |
7729 | Newz(0, tbl->tbl_ary, tbl->tbl_max + 1, PTR_TBL_ENT_t*); | |
7730 | return tbl; | |
7731 | } | |
7732 | ||
7733 | void * | |
7734 | Perl_ptr_table_fetch(pTHX_ PTR_TBL_t *tbl, void *sv) | |
7735 | { | |
7736 | PTR_TBL_ENT_t *tblent; | |
d2a79402 | 7737 | UV hash = PTR2UV(sv); |
1d7c1841 GS |
7738 | assert(tbl); |
7739 | tblent = tbl->tbl_ary[hash & tbl->tbl_max]; | |
7740 | for (; tblent; tblent = tblent->next) { | |
7741 | if (tblent->oldval == sv) | |
7742 | return tblent->newval; | |
7743 | } | |
7744 | return (void*)NULL; | |
7745 | } | |
7746 | ||
7747 | void | |
7748 | Perl_ptr_table_store(pTHX_ PTR_TBL_t *tbl, void *oldv, void *newv) | |
7749 | { | |
7750 | PTR_TBL_ENT_t *tblent, **otblent; | |
7751 | /* XXX this may be pessimal on platforms where pointers aren't good | |
7752 | * hash values e.g. if they grow faster in the most significant | |
7753 | * bits */ | |
d2a79402 | 7754 | UV hash = PTR2UV(oldv); |
1d7c1841 GS |
7755 | bool i = 1; |
7756 | ||
7757 | assert(tbl); | |
7758 | otblent = &tbl->tbl_ary[hash & tbl->tbl_max]; | |
7759 | for (tblent = *otblent; tblent; i=0, tblent = tblent->next) { | |
7760 | if (tblent->oldval == oldv) { | |
7761 | tblent->newval = newv; | |
7762 | tbl->tbl_items++; | |
7763 | return; | |
7764 | } | |
7765 | } | |
7766 | Newz(0, tblent, 1, PTR_TBL_ENT_t); | |
7767 | tblent->oldval = oldv; | |
7768 | tblent->newval = newv; | |
7769 | tblent->next = *otblent; | |
7770 | *otblent = tblent; | |
7771 | tbl->tbl_items++; | |
7772 | if (i && tbl->tbl_items > tbl->tbl_max) | |
7773 | ptr_table_split(tbl); | |
7774 | } | |
7775 | ||
7776 | void | |
7777 | Perl_ptr_table_split(pTHX_ PTR_TBL_t *tbl) | |
7778 | { | |
7779 | PTR_TBL_ENT_t **ary = tbl->tbl_ary; | |
7780 | UV oldsize = tbl->tbl_max + 1; | |
7781 | UV newsize = oldsize * 2; | |
7782 | UV i; | |
7783 | ||
7784 | Renew(ary, newsize, PTR_TBL_ENT_t*); | |
7785 | Zero(&ary[oldsize], newsize-oldsize, PTR_TBL_ENT_t*); | |
7786 | tbl->tbl_max = --newsize; | |
7787 | tbl->tbl_ary = ary; | |
7788 | for (i=0; i < oldsize; i++, ary++) { | |
7789 | PTR_TBL_ENT_t **curentp, **entp, *ent; | |
7790 | if (!*ary) | |
7791 | continue; | |
7792 | curentp = ary + oldsize; | |
7793 | for (entp = ary, ent = *ary; ent; ent = *entp) { | |
d2a79402 | 7794 | if ((newsize & PTR2UV(ent->oldval)) != i) { |
1d7c1841 GS |
7795 | *entp = ent->next; |
7796 | ent->next = *curentp; | |
7797 | *curentp = ent; | |
7798 | continue; | |
7799 | } | |
7800 | else | |
7801 | entp = &ent->next; | |
7802 | } | |
7803 | } | |
7804 | } | |
7805 | ||
a0739874 DM |
7806 | void |
7807 | Perl_ptr_table_clear(pTHX_ PTR_TBL_t *tbl) | |
7808 | { | |
7809 | register PTR_TBL_ENT_t **array; | |
7810 | register PTR_TBL_ENT_t *entry; | |
7811 | register PTR_TBL_ENT_t *oentry = Null(PTR_TBL_ENT_t*); | |
7812 | UV riter = 0; | |
7813 | UV max; | |
7814 | ||
7815 | if (!tbl || !tbl->tbl_items) { | |
7816 | return; | |
7817 | } | |
7818 | ||
7819 | array = tbl->tbl_ary; | |
7820 | entry = array[0]; | |
7821 | max = tbl->tbl_max; | |
7822 | ||
7823 | for (;;) { | |
7824 | if (entry) { | |
7825 | oentry = entry; | |
7826 | entry = entry->next; | |
7827 | Safefree(oentry); | |
7828 | } | |
7829 | if (!entry) { | |
7830 | if (++riter > max) { | |
7831 | break; | |
7832 | } | |
7833 | entry = array[riter]; | |
7834 | } | |
7835 | } | |
7836 | ||
7837 | tbl->tbl_items = 0; | |
7838 | } | |
7839 | ||
7840 | void | |
7841 | Perl_ptr_table_free(pTHX_ PTR_TBL_t *tbl) | |
7842 | { | |
7843 | if (!tbl) { | |
7844 | return; | |
7845 | } | |
7846 | ptr_table_clear(tbl); | |
7847 | Safefree(tbl->tbl_ary); | |
7848 | Safefree(tbl); | |
7849 | } | |
7850 | ||
1d7c1841 GS |
7851 | #ifdef DEBUGGING |
7852 | char *PL_watch_pvx; | |
7853 | #endif | |
7854 | ||
5bd07a3d DM |
7855 | STATIC SV * |
7856 | S_gv_share(pTHX_ SV *sstr) | |
7857 | { | |
7858 | GV *gv = (GV*)sstr; | |
7859 | SV *sv = &PL_sv_no; /* just need SvREADONLY-ness */ | |
7860 | ||
7861 | if (GvIO(gv) || GvFORM(gv)) { | |
7862 | GvSHARED_off(gv); /* GvIOs cannot be shared. nor can GvFORMs */ | |
7863 | } | |
7864 | else if (!GvCV(gv)) { | |
7865 | GvCV(gv) = (CV*)sv; | |
7866 | } | |
7867 | else { | |
7868 | /* CvPADLISTs cannot be shared */ | |
7869 | if (!CvXSUB(GvCV(gv))) { | |
7870 | GvSHARED_off(gv); | |
7871 | } | |
7872 | } | |
7873 | ||
7874 | if (!GvSHARED(gv)) { | |
7875 | #if 0 | |
7876 | PerlIO_printf(Perl_debug_log, "gv_share: unable to share %s::%s\n", | |
7877 | HvNAME(GvSTASH(gv)), GvNAME(gv)); | |
7878 | #endif | |
7879 | return Nullsv; | |
7880 | } | |
7881 | ||
4411f3b6 | 7882 | /* |
5bd07a3d DM |
7883 | * write attempts will die with |
7884 | * "Modification of a read-only value attempted" | |
7885 | */ | |
7886 | if (!GvSV(gv)) { | |
7887 | GvSV(gv) = sv; | |
7888 | } | |
7889 | else { | |
7890 | SvREADONLY_on(GvSV(gv)); | |
7891 | } | |
7892 | ||
7893 | if (!GvAV(gv)) { | |
7894 | GvAV(gv) = (AV*)sv; | |
7895 | } | |
7896 | else { | |
7897 | SvREADONLY_on(GvAV(gv)); | |
7898 | } | |
7899 | ||
7900 | if (!GvHV(gv)) { | |
7901 | GvHV(gv) = (HV*)sv; | |
7902 | } | |
7903 | else { | |
7904 | SvREADONLY_on(GvAV(gv)); | |
7905 | } | |
7906 | ||
7907 | return sstr; /* he_dup() will SvREFCNT_inc() */ | |
7908 | } | |
7909 | ||
1d7c1841 GS |
7910 | SV * |
7911 | Perl_sv_dup(pTHX_ SV *sstr) | |
7912 | { | |
1d7c1841 GS |
7913 | SV *dstr; |
7914 | ||
7915 | if (!sstr || SvTYPE(sstr) == SVTYPEMASK) | |
7916 | return Nullsv; | |
7917 | /* look for it in the table first */ | |
7918 | dstr = (SV*)ptr_table_fetch(PL_ptr_table, sstr); | |
7919 | if (dstr) | |
7920 | return dstr; | |
7921 | ||
7922 | /* create anew and remember what it is */ | |
7923 | new_SV(dstr); | |
7924 | ptr_table_store(PL_ptr_table, sstr, dstr); | |
7925 | ||
7926 | /* clone */ | |
7927 | SvFLAGS(dstr) = SvFLAGS(sstr); | |
7928 | SvFLAGS(dstr) &= ~SVf_OOK; /* don't propagate OOK hack */ | |
7929 | SvREFCNT(dstr) = 0; /* must be before any other dups! */ | |
7930 | ||
7931 | #ifdef DEBUGGING | |
7932 | if (SvANY(sstr) && PL_watch_pvx && SvPVX(sstr) == PL_watch_pvx) | |
7933 | PerlIO_printf(Perl_debug_log, "watch at %p hit, found string \"%s\"\n", | |
7934 | PL_watch_pvx, SvPVX(sstr)); | |
7935 | #endif | |
7936 | ||
7937 | switch (SvTYPE(sstr)) { | |
7938 | case SVt_NULL: | |
7939 | SvANY(dstr) = NULL; | |
7940 | break; | |
7941 | case SVt_IV: | |
7942 | SvANY(dstr) = new_XIV(); | |
7943 | SvIVX(dstr) = SvIVX(sstr); | |
7944 | break; | |
7945 | case SVt_NV: | |
7946 | SvANY(dstr) = new_XNV(); | |
7947 | SvNVX(dstr) = SvNVX(sstr); | |
7948 | break; | |
7949 | case SVt_RV: | |
7950 | SvANY(dstr) = new_XRV(); | |
7951 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7952 | break; | |
7953 | case SVt_PV: | |
7954 | SvANY(dstr) = new_XPV(); | |
7955 | SvCUR(dstr) = SvCUR(sstr); | |
7956 | SvLEN(dstr) = SvLEN(sstr); | |
7957 | if (SvROK(sstr)) | |
7958 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7959 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
7960 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
7961 | else | |
7962 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
7963 | break; | |
7964 | case SVt_PVIV: | |
7965 | SvANY(dstr) = new_XPVIV(); | |
7966 | SvCUR(dstr) = SvCUR(sstr); | |
7967 | SvLEN(dstr) = SvLEN(sstr); | |
7968 | SvIVX(dstr) = SvIVX(sstr); | |
7969 | if (SvROK(sstr)) | |
7970 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7971 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
7972 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
7973 | else | |
7974 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
7975 | break; | |
7976 | case SVt_PVNV: | |
7977 | SvANY(dstr) = new_XPVNV(); | |
7978 | SvCUR(dstr) = SvCUR(sstr); | |
7979 | SvLEN(dstr) = SvLEN(sstr); | |
7980 | SvIVX(dstr) = SvIVX(sstr); | |
7981 | SvNVX(dstr) = SvNVX(sstr); | |
7982 | if (SvROK(sstr)) | |
7983 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7984 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
7985 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
7986 | else | |
7987 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
7988 | break; | |
7989 | case SVt_PVMG: | |
7990 | SvANY(dstr) = new_XPVMG(); | |
7991 | SvCUR(dstr) = SvCUR(sstr); | |
7992 | SvLEN(dstr) = SvLEN(sstr); | |
7993 | SvIVX(dstr) = SvIVX(sstr); | |
7994 | SvNVX(dstr) = SvNVX(sstr); | |
7995 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
7996 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
7997 | if (SvROK(sstr)) | |
7998 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7999 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
8000 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
8001 | else | |
8002 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
8003 | break; | |
8004 | case SVt_PVBM: | |
8005 | SvANY(dstr) = new_XPVBM(); | |
8006 | SvCUR(dstr) = SvCUR(sstr); | |
8007 | SvLEN(dstr) = SvLEN(sstr); | |
8008 | SvIVX(dstr) = SvIVX(sstr); | |
8009 | SvNVX(dstr) = SvNVX(sstr); | |
8010 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
8011 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
8012 | if (SvROK(sstr)) | |
8013 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
8014 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
8015 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
8016 | else | |
8017 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
8018 | BmRARE(dstr) = BmRARE(sstr); | |
8019 | BmUSEFUL(dstr) = BmUSEFUL(sstr); | |
8020 | BmPREVIOUS(dstr)= BmPREVIOUS(sstr); | |
8021 | break; | |
8022 | case SVt_PVLV: | |
8023 | SvANY(dstr) = new_XPVLV(); | |
8024 | SvCUR(dstr) = SvCUR(sstr); | |
8025 | SvLEN(dstr) = SvLEN(sstr); | |
8026 | SvIVX(dstr) = SvIVX(sstr); | |
8027 | SvNVX(dstr) = SvNVX(sstr); | |
8028 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
8029 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
8030 | if (SvROK(sstr)) | |
8031 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
8032 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
8033 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
8034 | else | |
8035 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
8036 | LvTARGOFF(dstr) = LvTARGOFF(sstr); /* XXX sometimes holds PMOP* when DEBUGGING */ | |
8037 | LvTARGLEN(dstr) = LvTARGLEN(sstr); | |
8038 | LvTARG(dstr) = sv_dup_inc(LvTARG(sstr)); | |
8039 | LvTYPE(dstr) = LvTYPE(sstr); | |
8040 | break; | |
8041 | case SVt_PVGV: | |
5bd07a3d DM |
8042 | if (GvSHARED((GV*)sstr)) { |
8043 | SV *share; | |
8044 | if ((share = gv_share(sstr))) { | |
8045 | del_SV(dstr); | |
8046 | dstr = share; | |
8047 | #if 0 | |
8048 | PerlIO_printf(Perl_debug_log, "sv_dup: sharing %s::%s\n", | |
8049 | HvNAME(GvSTASH(share)), GvNAME(share)); | |
8050 | #endif | |
8051 | break; | |
8052 | } | |
8053 | } | |
1d7c1841 GS |
8054 | SvANY(dstr) = new_XPVGV(); |
8055 | SvCUR(dstr) = SvCUR(sstr); | |
8056 | SvLEN(dstr) = SvLEN(sstr); | |
8057 | SvIVX(dstr) = SvIVX(sstr); | |
8058 | SvNVX(dstr) = SvNVX(sstr); | |
8059 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
8060 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
8061 | if (SvROK(sstr)) | |
8062 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
8063 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
8064 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
8065 | else | |
8066 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
8067 | GvNAMELEN(dstr) = GvNAMELEN(sstr); | |
8068 | GvNAME(dstr) = SAVEPVN(GvNAME(sstr), GvNAMELEN(sstr)); | |
8069 | GvSTASH(dstr) = hv_dup_inc(GvSTASH(sstr)); | |
8070 | GvFLAGS(dstr) = GvFLAGS(sstr); | |
8071 | GvGP(dstr) = gp_dup(GvGP(sstr)); | |
8072 | (void)GpREFCNT_inc(GvGP(dstr)); | |
8073 | break; | |
8074 | case SVt_PVIO: | |
8075 | SvANY(dstr) = new_XPVIO(); | |
8076 | SvCUR(dstr) = SvCUR(sstr); | |
8077 | SvLEN(dstr) = SvLEN(sstr); | |
8078 | SvIVX(dstr) = SvIVX(sstr); | |
8079 | SvNVX(dstr) = SvNVX(sstr); | |
8080 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
8081 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
8082 | if (SvROK(sstr)) | |
8083 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
8084 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
8085 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
8086 | else | |
8087 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
8088 | IoIFP(dstr) = fp_dup(IoIFP(sstr), IoTYPE(sstr)); | |
8089 | if (IoOFP(sstr) == IoIFP(sstr)) | |
8090 | IoOFP(dstr) = IoIFP(dstr); | |
8091 | else | |
8092 | IoOFP(dstr) = fp_dup(IoOFP(sstr), IoTYPE(sstr)); | |
8093 | /* PL_rsfp_filters entries have fake IoDIRP() */ | |
8094 | if (IoDIRP(sstr) && !(IoFLAGS(sstr) & IOf_FAKE_DIRP)) | |
8095 | IoDIRP(dstr) = dirp_dup(IoDIRP(sstr)); | |
8096 | else | |
8097 | IoDIRP(dstr) = IoDIRP(sstr); | |
8098 | IoLINES(dstr) = IoLINES(sstr); | |
8099 | IoPAGE(dstr) = IoPAGE(sstr); | |
8100 | IoPAGE_LEN(dstr) = IoPAGE_LEN(sstr); | |
8101 | IoLINES_LEFT(dstr) = IoLINES_LEFT(sstr); | |
8102 | IoTOP_NAME(dstr) = SAVEPV(IoTOP_NAME(sstr)); | |
8103 | IoTOP_GV(dstr) = gv_dup(IoTOP_GV(sstr)); | |
8104 | IoFMT_NAME(dstr) = SAVEPV(IoFMT_NAME(sstr)); | |
8105 | IoFMT_GV(dstr) = gv_dup(IoFMT_GV(sstr)); | |
8106 | IoBOTTOM_NAME(dstr) = SAVEPV(IoBOTTOM_NAME(sstr)); | |
8107 | IoBOTTOM_GV(dstr) = gv_dup(IoBOTTOM_GV(sstr)); | |
8108 | IoSUBPROCESS(dstr) = IoSUBPROCESS(sstr); | |
8109 | IoTYPE(dstr) = IoTYPE(sstr); | |
8110 | IoFLAGS(dstr) = IoFLAGS(sstr); | |
8111 | break; | |
8112 | case SVt_PVAV: | |
8113 | SvANY(dstr) = new_XPVAV(); | |
8114 | SvCUR(dstr) = SvCUR(sstr); | |
8115 | SvLEN(dstr) = SvLEN(sstr); | |
8116 | SvIVX(dstr) = SvIVX(sstr); | |
8117 | SvNVX(dstr) = SvNVX(sstr); | |
8118 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
8119 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
8120 | AvARYLEN((AV*)dstr) = sv_dup_inc(AvARYLEN((AV*)sstr)); | |
8121 | AvFLAGS((AV*)dstr) = AvFLAGS((AV*)sstr); | |
8122 | if (AvARRAY((AV*)sstr)) { | |
8123 | SV **dst_ary, **src_ary; | |
8124 | SSize_t items = AvFILLp((AV*)sstr) + 1; | |
8125 | ||
8126 | src_ary = AvARRAY((AV*)sstr); | |
8127 | Newz(0, dst_ary, AvMAX((AV*)sstr)+1, SV*); | |
8128 | ptr_table_store(PL_ptr_table, src_ary, dst_ary); | |
8129 | SvPVX(dstr) = (char*)dst_ary; | |
8130 | AvALLOC((AV*)dstr) = dst_ary; | |
8131 | if (AvREAL((AV*)sstr)) { | |
8132 | while (items-- > 0) | |
8133 | *dst_ary++ = sv_dup_inc(*src_ary++); | |
8134 | } | |
8135 | else { | |
8136 | while (items-- > 0) | |
8137 | *dst_ary++ = sv_dup(*src_ary++); | |
8138 | } | |
8139 | items = AvMAX((AV*)sstr) - AvFILLp((AV*)sstr); | |
8140 | while (items-- > 0) { | |
8141 | *dst_ary++ = &PL_sv_undef; | |
8142 | } | |
8143 | } | |
8144 | else { | |
8145 | SvPVX(dstr) = Nullch; | |
8146 | AvALLOC((AV*)dstr) = (SV**)NULL; | |
8147 | } | |
8148 | break; | |
8149 | case SVt_PVHV: | |
8150 | SvANY(dstr) = new_XPVHV(); | |
8151 | SvCUR(dstr) = SvCUR(sstr); | |
8152 | SvLEN(dstr) = SvLEN(sstr); | |
8153 | SvIVX(dstr) = SvIVX(sstr); | |
8154 | SvNVX(dstr) = SvNVX(sstr); | |
8155 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
8156 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
8157 | HvRITER((HV*)dstr) = HvRITER((HV*)sstr); | |
8158 | if (HvARRAY((HV*)sstr)) { | |
1d7c1841 GS |
8159 | STRLEN i = 0; |
8160 | XPVHV *dxhv = (XPVHV*)SvANY(dstr); | |
8161 | XPVHV *sxhv = (XPVHV*)SvANY(sstr); | |
8162 | Newz(0, dxhv->xhv_array, | |
8163 | PERL_HV_ARRAY_ALLOC_BYTES(dxhv->xhv_max+1), char); | |
8164 | while (i <= sxhv->xhv_max) { | |
8165 | ((HE**)dxhv->xhv_array)[i] = he_dup(((HE**)sxhv->xhv_array)[i], | |
8166 | !!HvSHAREKEYS(sstr)); | |
8167 | ++i; | |
8168 | } | |
8169 | dxhv->xhv_eiter = he_dup(sxhv->xhv_eiter, !!HvSHAREKEYS(sstr)); | |
8170 | } | |
8171 | else { | |
8172 | SvPVX(dstr) = Nullch; | |
8173 | HvEITER((HV*)dstr) = (HE*)NULL; | |
8174 | } | |
8175 | HvPMROOT((HV*)dstr) = HvPMROOT((HV*)sstr); /* XXX */ | |
8176 | HvNAME((HV*)dstr) = SAVEPV(HvNAME((HV*)sstr)); | |
8177 | break; | |
8178 | case SVt_PVFM: | |
8179 | SvANY(dstr) = new_XPVFM(); | |
8180 | FmLINES(dstr) = FmLINES(sstr); | |
8181 | goto dup_pvcv; | |
8182 | /* NOTREACHED */ | |
8183 | case SVt_PVCV: | |
8184 | SvANY(dstr) = new_XPVCV(); | |
8185 | dup_pvcv: | |
8186 | SvCUR(dstr) = SvCUR(sstr); | |
8187 | SvLEN(dstr) = SvLEN(sstr); | |
8188 | SvIVX(dstr) = SvIVX(sstr); | |
8189 | SvNVX(dstr) = SvNVX(sstr); | |
8190 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
8191 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
8192 | if (SvPVX(sstr) && SvLEN(sstr)) | |
8193 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
8194 | else | |
8195 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
8196 | CvSTASH(dstr) = hv_dup(CvSTASH(sstr));/* NOTE: not refcounted */ | |
8197 | CvSTART(dstr) = CvSTART(sstr); | |
8198 | CvROOT(dstr) = OpREFCNT_inc(CvROOT(sstr)); | |
8199 | CvXSUB(dstr) = CvXSUB(sstr); | |
8200 | CvXSUBANY(dstr) = CvXSUBANY(sstr); | |
f25c30a3 | 8201 | CvGV(dstr) = gv_dup(CvGV(sstr)); |
1d7c1841 GS |
8202 | CvDEPTH(dstr) = CvDEPTH(sstr); |
8203 | if (CvPADLIST(sstr) && !AvREAL(CvPADLIST(sstr))) { | |
8204 | /* XXX padlists are real, but pretend to be not */ | |
8205 | AvREAL_on(CvPADLIST(sstr)); | |
8206 | CvPADLIST(dstr) = av_dup_inc(CvPADLIST(sstr)); | |
8207 | AvREAL_off(CvPADLIST(sstr)); | |
8208 | AvREAL_off(CvPADLIST(dstr)); | |
8209 | } | |
8210 | else | |
8211 | CvPADLIST(dstr) = av_dup_inc(CvPADLIST(sstr)); | |
282f25c9 JH |
8212 | if (!CvANON(sstr) || CvCLONED(sstr)) |
8213 | CvOUTSIDE(dstr) = cv_dup_inc(CvOUTSIDE(sstr)); | |
8214 | else | |
8215 | CvOUTSIDE(dstr) = cv_dup(CvOUTSIDE(sstr)); | |
1d7c1841 GS |
8216 | CvFLAGS(dstr) = CvFLAGS(sstr); |
8217 | break; | |
8218 | default: | |
8219 | Perl_croak(aTHX_ "Bizarre SvTYPE [%d]", SvTYPE(sstr)); | |
8220 | break; | |
8221 | } | |
8222 | ||
8223 | if (SvOBJECT(dstr) && SvTYPE(dstr) != SVt_PVIO) | |
8224 | ++PL_sv_objcount; | |
8225 | ||
8226 | return dstr; | |
8227 | } | |
8228 | ||
8229 | PERL_CONTEXT * | |
8230 | Perl_cx_dup(pTHX_ PERL_CONTEXT *cxs, I32 ix, I32 max) | |
8231 | { | |
8232 | PERL_CONTEXT *ncxs; | |
8233 | ||
8234 | if (!cxs) | |
8235 | return (PERL_CONTEXT*)NULL; | |
8236 | ||
8237 | /* look for it in the table first */ | |
8238 | ncxs = (PERL_CONTEXT*)ptr_table_fetch(PL_ptr_table, cxs); | |
8239 | if (ncxs) | |
8240 | return ncxs; | |
8241 | ||
8242 | /* create anew and remember what it is */ | |
8243 | Newz(56, ncxs, max + 1, PERL_CONTEXT); | |
8244 | ptr_table_store(PL_ptr_table, cxs, ncxs); | |
8245 | ||
8246 | while (ix >= 0) { | |
8247 | PERL_CONTEXT *cx = &cxs[ix]; | |
8248 | PERL_CONTEXT *ncx = &ncxs[ix]; | |
8249 | ncx->cx_type = cx->cx_type; | |
8250 | if (CxTYPE(cx) == CXt_SUBST) { | |
8251 | Perl_croak(aTHX_ "Cloning substitution context is unimplemented"); | |
8252 | } | |
8253 | else { | |
8254 | ncx->blk_oldsp = cx->blk_oldsp; | |
8255 | ncx->blk_oldcop = cx->blk_oldcop; | |
8256 | ncx->blk_oldretsp = cx->blk_oldretsp; | |
8257 | ncx->blk_oldmarksp = cx->blk_oldmarksp; | |
8258 | ncx->blk_oldscopesp = cx->blk_oldscopesp; | |
8259 | ncx->blk_oldpm = cx->blk_oldpm; | |
8260 | ncx->blk_gimme = cx->blk_gimme; | |
8261 | switch (CxTYPE(cx)) { | |
8262 | case CXt_SUB: | |
8263 | ncx->blk_sub.cv = (cx->blk_sub.olddepth == 0 | |
8264 | ? cv_dup_inc(cx->blk_sub.cv) | |
8265 | : cv_dup(cx->blk_sub.cv)); | |
8266 | ncx->blk_sub.argarray = (cx->blk_sub.hasargs | |
8267 | ? av_dup_inc(cx->blk_sub.argarray) | |
8268 | : Nullav); | |
f25c30a3 | 8269 | ncx->blk_sub.savearray = av_dup_inc(cx->blk_sub.savearray); |
1d7c1841 GS |
8270 | ncx->blk_sub.olddepth = cx->blk_sub.olddepth; |
8271 | ncx->blk_sub.hasargs = cx->blk_sub.hasargs; | |
8272 | ncx->blk_sub.lval = cx->blk_sub.lval; | |
8273 | break; | |
8274 | case CXt_EVAL: | |
8275 | ncx->blk_eval.old_in_eval = cx->blk_eval.old_in_eval; | |
8276 | ncx->blk_eval.old_op_type = cx->blk_eval.old_op_type; | |
0f79a09d | 8277 | ncx->blk_eval.old_namesv = sv_dup_inc(cx->blk_eval.old_namesv); |
1d7c1841 GS |
8278 | ncx->blk_eval.old_eval_root = cx->blk_eval.old_eval_root; |
8279 | ncx->blk_eval.cur_text = sv_dup(cx->blk_eval.cur_text); | |
8280 | break; | |
8281 | case CXt_LOOP: | |
8282 | ncx->blk_loop.label = cx->blk_loop.label; | |
8283 | ncx->blk_loop.resetsp = cx->blk_loop.resetsp; | |
8284 | ncx->blk_loop.redo_op = cx->blk_loop.redo_op; | |
8285 | ncx->blk_loop.next_op = cx->blk_loop.next_op; | |
8286 | ncx->blk_loop.last_op = cx->blk_loop.last_op; | |
8287 | ncx->blk_loop.iterdata = (CxPADLOOP(cx) | |
8288 | ? cx->blk_loop.iterdata | |
8289 | : gv_dup((GV*)cx->blk_loop.iterdata)); | |
a4b82a6f GS |
8290 | ncx->blk_loop.oldcurpad |
8291 | = (SV**)ptr_table_fetch(PL_ptr_table, | |
8292 | cx->blk_loop.oldcurpad); | |
1d7c1841 GS |
8293 | ncx->blk_loop.itersave = sv_dup_inc(cx->blk_loop.itersave); |
8294 | ncx->blk_loop.iterlval = sv_dup_inc(cx->blk_loop.iterlval); | |
8295 | ncx->blk_loop.iterary = av_dup_inc(cx->blk_loop.iterary); | |
8296 | ncx->blk_loop.iterix = cx->blk_loop.iterix; | |
8297 | ncx->blk_loop.itermax = cx->blk_loop.itermax; | |
8298 | break; | |
8299 | case CXt_FORMAT: | |
8300 | ncx->blk_sub.cv = cv_dup(cx->blk_sub.cv); | |
8301 | ncx->blk_sub.gv = gv_dup(cx->blk_sub.gv); | |
8302 | ncx->blk_sub.dfoutgv = gv_dup_inc(cx->blk_sub.dfoutgv); | |
8303 | ncx->blk_sub.hasargs = cx->blk_sub.hasargs; | |
8304 | break; | |
8305 | case CXt_BLOCK: | |
8306 | case CXt_NULL: | |
8307 | break; | |
8308 | } | |
8309 | } | |
8310 | --ix; | |
8311 | } | |
8312 | return ncxs; | |
8313 | } | |
8314 | ||
8315 | PERL_SI * | |
8316 | Perl_si_dup(pTHX_ PERL_SI *si) | |
8317 | { | |
8318 | PERL_SI *nsi; | |
8319 | ||
8320 | if (!si) | |
8321 | return (PERL_SI*)NULL; | |
8322 | ||
8323 | /* look for it in the table first */ | |
8324 | nsi = (PERL_SI*)ptr_table_fetch(PL_ptr_table, si); | |
8325 | if (nsi) | |
8326 | return nsi; | |
8327 | ||
8328 | /* create anew and remember what it is */ | |
8329 | Newz(56, nsi, 1, PERL_SI); | |
8330 | ptr_table_store(PL_ptr_table, si, nsi); | |
8331 | ||
8332 | nsi->si_stack = av_dup_inc(si->si_stack); | |
8333 | nsi->si_cxix = si->si_cxix; | |
8334 | nsi->si_cxmax = si->si_cxmax; | |
8335 | nsi->si_cxstack = cx_dup(si->si_cxstack, si->si_cxix, si->si_cxmax); | |
8336 | nsi->si_type = si->si_type; | |
8337 | nsi->si_prev = si_dup(si->si_prev); | |
8338 | nsi->si_next = si_dup(si->si_next); | |
8339 | nsi->si_markoff = si->si_markoff; | |
8340 | ||
8341 | return nsi; | |
8342 | } | |
8343 | ||
8344 | #define POPINT(ss,ix) ((ss)[--(ix)].any_i32) | |
8345 | #define TOPINT(ss,ix) ((ss)[ix].any_i32) | |
8346 | #define POPLONG(ss,ix) ((ss)[--(ix)].any_long) | |
8347 | #define TOPLONG(ss,ix) ((ss)[ix].any_long) | |
8348 | #define POPIV(ss,ix) ((ss)[--(ix)].any_iv) | |
8349 | #define TOPIV(ss,ix) ((ss)[ix].any_iv) | |
8350 | #define POPPTR(ss,ix) ((ss)[--(ix)].any_ptr) | |
8351 | #define TOPPTR(ss,ix) ((ss)[ix].any_ptr) | |
8352 | #define POPDPTR(ss,ix) ((ss)[--(ix)].any_dptr) | |
8353 | #define TOPDPTR(ss,ix) ((ss)[ix].any_dptr) | |
8354 | #define POPDXPTR(ss,ix) ((ss)[--(ix)].any_dxptr) | |
8355 | #define TOPDXPTR(ss,ix) ((ss)[ix].any_dxptr) | |
8356 | ||
8357 | /* XXXXX todo */ | |
8358 | #define pv_dup_inc(p) SAVEPV(p) | |
8359 | #define pv_dup(p) SAVEPV(p) | |
8360 | #define svp_dup_inc(p,pp) any_dup(p,pp) | |
8361 | ||
8362 | void * | |
8363 | Perl_any_dup(pTHX_ void *v, PerlInterpreter *proto_perl) | |
8364 | { | |
8365 | void *ret; | |
8366 | ||
8367 | if (!v) | |
8368 | return (void*)NULL; | |
8369 | ||
8370 | /* look for it in the table first */ | |
8371 | ret = ptr_table_fetch(PL_ptr_table, v); | |
8372 | if (ret) | |
8373 | return ret; | |
8374 | ||
8375 | /* see if it is part of the interpreter structure */ | |
8376 | if (v >= (void*)proto_perl && v < (void*)(proto_perl+1)) | |
8377 | ret = (void*)(((char*)aTHXo) + (((char*)v) - (char*)proto_perl)); | |
8378 | else | |
8379 | ret = v; | |
8380 | ||
8381 | return ret; | |
8382 | } | |
8383 | ||
8384 | ANY * | |
8385 | Perl_ss_dup(pTHX_ PerlInterpreter *proto_perl) | |
8386 | { | |
8387 | ANY *ss = proto_perl->Tsavestack; | |
8388 | I32 ix = proto_perl->Tsavestack_ix; | |
8389 | I32 max = proto_perl->Tsavestack_max; | |
8390 | ANY *nss; | |
8391 | SV *sv; | |
8392 | GV *gv; | |
8393 | AV *av; | |
8394 | HV *hv; | |
8395 | void* ptr; | |
8396 | int intval; | |
8397 | long longval; | |
8398 | GP *gp; | |
8399 | IV iv; | |
8400 | I32 i; | |
8401 | char *c; | |
8402 | void (*dptr) (void*); | |
8403 | void (*dxptr) (pTHXo_ void*); | |
e977893f | 8404 | OP *o; |
1d7c1841 GS |
8405 | |
8406 | Newz(54, nss, max, ANY); | |
8407 | ||
8408 | while (ix > 0) { | |
8409 | i = POPINT(ss,ix); | |
8410 | TOPINT(nss,ix) = i; | |
8411 | switch (i) { | |
8412 | case SAVEt_ITEM: /* normal string */ | |
8413 | sv = (SV*)POPPTR(ss,ix); | |
8414 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8415 | sv = (SV*)POPPTR(ss,ix); | |
8416 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8417 | break; | |
8418 | case SAVEt_SV: /* scalar reference */ | |
8419 | sv = (SV*)POPPTR(ss,ix); | |
8420 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8421 | gv = (GV*)POPPTR(ss,ix); | |
8422 | TOPPTR(nss,ix) = gv_dup_inc(gv); | |
8423 | break; | |
f4dd75d9 GS |
8424 | case SAVEt_GENERIC_PVREF: /* generic char* */ |
8425 | c = (char*)POPPTR(ss,ix); | |
8426 | TOPPTR(nss,ix) = pv_dup(c); | |
8427 | ptr = POPPTR(ss,ix); | |
8428 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8429 | break; | |
1d7c1841 GS |
8430 | case SAVEt_GENERIC_SVREF: /* generic sv */ |
8431 | case SAVEt_SVREF: /* scalar reference */ | |
8432 | sv = (SV*)POPPTR(ss,ix); | |
8433 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8434 | ptr = POPPTR(ss,ix); | |
8435 | TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */ | |
8436 | break; | |
8437 | case SAVEt_AV: /* array reference */ | |
8438 | av = (AV*)POPPTR(ss,ix); | |
8439 | TOPPTR(nss,ix) = av_dup_inc(av); | |
8440 | gv = (GV*)POPPTR(ss,ix); | |
8441 | TOPPTR(nss,ix) = gv_dup(gv); | |
8442 | break; | |
8443 | case SAVEt_HV: /* hash reference */ | |
8444 | hv = (HV*)POPPTR(ss,ix); | |
8445 | TOPPTR(nss,ix) = hv_dup_inc(hv); | |
8446 | gv = (GV*)POPPTR(ss,ix); | |
8447 | TOPPTR(nss,ix) = gv_dup(gv); | |
8448 | break; | |
8449 | case SAVEt_INT: /* int reference */ | |
8450 | ptr = POPPTR(ss,ix); | |
8451 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8452 | intval = (int)POPINT(ss,ix); | |
8453 | TOPINT(nss,ix) = intval; | |
8454 | break; | |
8455 | case SAVEt_LONG: /* long reference */ | |
8456 | ptr = POPPTR(ss,ix); | |
8457 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8458 | longval = (long)POPLONG(ss,ix); | |
8459 | TOPLONG(nss,ix) = longval; | |
8460 | break; | |
8461 | case SAVEt_I32: /* I32 reference */ | |
8462 | case SAVEt_I16: /* I16 reference */ | |
8463 | case SAVEt_I8: /* I8 reference */ | |
8464 | ptr = POPPTR(ss,ix); | |
8465 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8466 | i = POPINT(ss,ix); | |
8467 | TOPINT(nss,ix) = i; | |
8468 | break; | |
8469 | case SAVEt_IV: /* IV reference */ | |
8470 | ptr = POPPTR(ss,ix); | |
8471 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8472 | iv = POPIV(ss,ix); | |
8473 | TOPIV(nss,ix) = iv; | |
8474 | break; | |
8475 | case SAVEt_SPTR: /* SV* reference */ | |
8476 | ptr = POPPTR(ss,ix); | |
8477 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8478 | sv = (SV*)POPPTR(ss,ix); | |
8479 | TOPPTR(nss,ix) = sv_dup(sv); | |
8480 | break; | |
8481 | case SAVEt_VPTR: /* random* reference */ | |
8482 | ptr = POPPTR(ss,ix); | |
8483 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8484 | ptr = POPPTR(ss,ix); | |
8485 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8486 | break; | |
8487 | case SAVEt_PPTR: /* char* reference */ | |
8488 | ptr = POPPTR(ss,ix); | |
8489 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8490 | c = (char*)POPPTR(ss,ix); | |
8491 | TOPPTR(nss,ix) = pv_dup(c); | |
8492 | break; | |
8493 | case SAVEt_HPTR: /* HV* reference */ | |
8494 | ptr = POPPTR(ss,ix); | |
8495 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8496 | hv = (HV*)POPPTR(ss,ix); | |
8497 | TOPPTR(nss,ix) = hv_dup(hv); | |
8498 | break; | |
8499 | case SAVEt_APTR: /* AV* reference */ | |
8500 | ptr = POPPTR(ss,ix); | |
8501 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8502 | av = (AV*)POPPTR(ss,ix); | |
8503 | TOPPTR(nss,ix) = av_dup(av); | |
8504 | break; | |
8505 | case SAVEt_NSTAB: | |
8506 | gv = (GV*)POPPTR(ss,ix); | |
8507 | TOPPTR(nss,ix) = gv_dup(gv); | |
8508 | break; | |
8509 | case SAVEt_GP: /* scalar reference */ | |
8510 | gp = (GP*)POPPTR(ss,ix); | |
8511 | TOPPTR(nss,ix) = gp = gp_dup(gp); | |
8512 | (void)GpREFCNT_inc(gp); | |
8513 | gv = (GV*)POPPTR(ss,ix); | |
8514 | TOPPTR(nss,ix) = gv_dup_inc(c); | |
8515 | c = (char*)POPPTR(ss,ix); | |
8516 | TOPPTR(nss,ix) = pv_dup(c); | |
8517 | iv = POPIV(ss,ix); | |
8518 | TOPIV(nss,ix) = iv; | |
8519 | iv = POPIV(ss,ix); | |
8520 | TOPIV(nss,ix) = iv; | |
8521 | break; | |
8522 | case SAVEt_FREESV: | |
8523 | sv = (SV*)POPPTR(ss,ix); | |
8524 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8525 | break; | |
8526 | case SAVEt_FREEOP: | |
8527 | ptr = POPPTR(ss,ix); | |
8528 | if (ptr && (((OP*)ptr)->op_private & OPpREFCOUNTED)) { | |
8529 | /* these are assumed to be refcounted properly */ | |
8530 | switch (((OP*)ptr)->op_type) { | |
8531 | case OP_LEAVESUB: | |
8532 | case OP_LEAVESUBLV: | |
8533 | case OP_LEAVEEVAL: | |
8534 | case OP_LEAVE: | |
8535 | case OP_SCOPE: | |
8536 | case OP_LEAVEWRITE: | |
e977893f GS |
8537 | TOPPTR(nss,ix) = ptr; |
8538 | o = (OP*)ptr; | |
8539 | OpREFCNT_inc(o); | |
1d7c1841 GS |
8540 | break; |
8541 | default: | |
8542 | TOPPTR(nss,ix) = Nullop; | |
8543 | break; | |
8544 | } | |
8545 | } | |
8546 | else | |
8547 | TOPPTR(nss,ix) = Nullop; | |
8548 | break; | |
8549 | case SAVEt_FREEPV: | |
8550 | c = (char*)POPPTR(ss,ix); | |
8551 | TOPPTR(nss,ix) = pv_dup_inc(c); | |
8552 | break; | |
8553 | case SAVEt_CLEARSV: | |
8554 | longval = POPLONG(ss,ix); | |
8555 | TOPLONG(nss,ix) = longval; | |
8556 | break; | |
8557 | case SAVEt_DELETE: | |
8558 | hv = (HV*)POPPTR(ss,ix); | |
8559 | TOPPTR(nss,ix) = hv_dup_inc(hv); | |
8560 | c = (char*)POPPTR(ss,ix); | |
8561 | TOPPTR(nss,ix) = pv_dup_inc(c); | |
8562 | i = POPINT(ss,ix); | |
8563 | TOPINT(nss,ix) = i; | |
8564 | break; | |
8565 | case SAVEt_DESTRUCTOR: | |
8566 | ptr = POPPTR(ss,ix); | |
8567 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */ | |
8568 | dptr = POPDPTR(ss,ix); | |
ef75a179 | 8569 | TOPDPTR(nss,ix) = (void (*)(void*))any_dup((void *)dptr, proto_perl); |
1d7c1841 GS |
8570 | break; |
8571 | case SAVEt_DESTRUCTOR_X: | |
8572 | ptr = POPPTR(ss,ix); | |
8573 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */ | |
8574 | dxptr = POPDXPTR(ss,ix); | |
ef75a179 | 8575 | TOPDXPTR(nss,ix) = (void (*)(pTHXo_ void*))any_dup((void *)dxptr, proto_perl); |
1d7c1841 GS |
8576 | break; |
8577 | case SAVEt_REGCONTEXT: | |
8578 | case SAVEt_ALLOC: | |
8579 | i = POPINT(ss,ix); | |
8580 | TOPINT(nss,ix) = i; | |
8581 | ix -= i; | |
8582 | break; | |
8583 | case SAVEt_STACK_POS: /* Position on Perl stack */ | |
8584 | i = POPINT(ss,ix); | |
8585 | TOPINT(nss,ix) = i; | |
8586 | break; | |
8587 | case SAVEt_AELEM: /* array element */ | |
8588 | sv = (SV*)POPPTR(ss,ix); | |
8589 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8590 | i = POPINT(ss,ix); | |
8591 | TOPINT(nss,ix) = i; | |
8592 | av = (AV*)POPPTR(ss,ix); | |
8593 | TOPPTR(nss,ix) = av_dup_inc(av); | |
8594 | break; | |
8595 | case SAVEt_HELEM: /* hash element */ | |
8596 | sv = (SV*)POPPTR(ss,ix); | |
8597 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8598 | sv = (SV*)POPPTR(ss,ix); | |
8599 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8600 | hv = (HV*)POPPTR(ss,ix); | |
8601 | TOPPTR(nss,ix) = hv_dup_inc(hv); | |
8602 | break; | |
8603 | case SAVEt_OP: | |
8604 | ptr = POPPTR(ss,ix); | |
8605 | TOPPTR(nss,ix) = ptr; | |
8606 | break; | |
8607 | case SAVEt_HINTS: | |
8608 | i = POPINT(ss,ix); | |
8609 | TOPINT(nss,ix) = i; | |
8610 | break; | |
c4410b1b GS |
8611 | case SAVEt_COMPPAD: |
8612 | av = (AV*)POPPTR(ss,ix); | |
8613 | TOPPTR(nss,ix) = av_dup(av); | |
8614 | break; | |
c3564e5c GS |
8615 | case SAVEt_PADSV: |
8616 | longval = (long)POPLONG(ss,ix); | |
8617 | TOPLONG(nss,ix) = longval; | |
8618 | ptr = POPPTR(ss,ix); | |
8619 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8620 | sv = (SV*)POPPTR(ss,ix); | |
8621 | TOPPTR(nss,ix) = sv_dup(sv); | |
8622 | break; | |
1d7c1841 GS |
8623 | default: |
8624 | Perl_croak(aTHX_ "panic: ss_dup inconsistency"); | |
8625 | } | |
8626 | } | |
8627 | ||
8628 | return nss; | |
8629 | } | |
8630 | ||
8631 | #ifdef PERL_OBJECT | |
8632 | #include "XSUB.h" | |
8633 | #endif | |
8634 | ||
8635 | PerlInterpreter * | |
8636 | perl_clone(PerlInterpreter *proto_perl, UV flags) | |
8637 | { | |
8638 | #ifdef PERL_OBJECT | |
8639 | CPerlObj *pPerl = (CPerlObj*)proto_perl; | |
8640 | #endif | |
8641 | ||
8642 | #ifdef PERL_IMPLICIT_SYS | |
8643 | return perl_clone_using(proto_perl, flags, | |
8644 | proto_perl->IMem, | |
8645 | proto_perl->IMemShared, | |
8646 | proto_perl->IMemParse, | |
8647 | proto_perl->IEnv, | |
8648 | proto_perl->IStdIO, | |
8649 | proto_perl->ILIO, | |
8650 | proto_perl->IDir, | |
8651 | proto_perl->ISock, | |
8652 | proto_perl->IProc); | |
8653 | } | |
8654 | ||
8655 | PerlInterpreter * | |
8656 | perl_clone_using(PerlInterpreter *proto_perl, UV flags, | |
8657 | struct IPerlMem* ipM, struct IPerlMem* ipMS, | |
8658 | struct IPerlMem* ipMP, struct IPerlEnv* ipE, | |
8659 | struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO, | |
8660 | struct IPerlDir* ipD, struct IPerlSock* ipS, | |
8661 | struct IPerlProc* ipP) | |
8662 | { | |
8663 | /* XXX many of the string copies here can be optimized if they're | |
8664 | * constants; they need to be allocated as common memory and just | |
8665 | * their pointers copied. */ | |
8666 | ||
8667 | IV i; | |
1d7c1841 GS |
8668 | # ifdef PERL_OBJECT |
8669 | CPerlObj *pPerl = new(ipM) CPerlObj(ipM, ipMS, ipMP, ipE, ipStd, ipLIO, | |
8670 | ipD, ipS, ipP); | |
ba869deb | 8671 | PERL_SET_THX(pPerl); |
1d7c1841 GS |
8672 | # else /* !PERL_OBJECT */ |
8673 | PerlInterpreter *my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter)); | |
ba869deb | 8674 | PERL_SET_THX(my_perl); |
1d7c1841 GS |
8675 | |
8676 | # ifdef DEBUGGING | |
8677 | memset(my_perl, 0xab, sizeof(PerlInterpreter)); | |
8678 | PL_markstack = 0; | |
8679 | PL_scopestack = 0; | |
8680 | PL_savestack = 0; | |
8681 | PL_retstack = 0; | |
66fe0623 | 8682 | PL_sig_pending = 0; |
1d7c1841 GS |
8683 | # else /* !DEBUGGING */ |
8684 | Zero(my_perl, 1, PerlInterpreter); | |
8685 | # endif /* DEBUGGING */ | |
8686 | ||
8687 | /* host pointers */ | |
8688 | PL_Mem = ipM; | |
8689 | PL_MemShared = ipMS; | |
8690 | PL_MemParse = ipMP; | |
8691 | PL_Env = ipE; | |
8692 | PL_StdIO = ipStd; | |
8693 | PL_LIO = ipLIO; | |
8694 | PL_Dir = ipD; | |
8695 | PL_Sock = ipS; | |
8696 | PL_Proc = ipP; | |
8697 | # endif /* PERL_OBJECT */ | |
8698 | #else /* !PERL_IMPLICIT_SYS */ | |
8699 | IV i; | |
1d7c1841 | 8700 | PerlInterpreter *my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter)); |
ba869deb | 8701 | PERL_SET_THX(my_perl); |
1d7c1841 GS |
8702 | |
8703 | # ifdef DEBUGGING | |
8704 | memset(my_perl, 0xab, sizeof(PerlInterpreter)); | |
8705 | PL_markstack = 0; | |
8706 | PL_scopestack = 0; | |
8707 | PL_savestack = 0; | |
8708 | PL_retstack = 0; | |
66fe0623 | 8709 | PL_sig_pending = 0; |
1d7c1841 GS |
8710 | # else /* !DEBUGGING */ |
8711 | Zero(my_perl, 1, PerlInterpreter); | |
8712 | # endif /* DEBUGGING */ | |
8713 | #endif /* PERL_IMPLICIT_SYS */ | |
8714 | ||
8715 | /* arena roots */ | |
8716 | PL_xiv_arenaroot = NULL; | |
8717 | PL_xiv_root = NULL; | |
612f20c3 | 8718 | PL_xnv_arenaroot = NULL; |
1d7c1841 | 8719 | PL_xnv_root = NULL; |
612f20c3 | 8720 | PL_xrv_arenaroot = NULL; |
1d7c1841 | 8721 | PL_xrv_root = NULL; |
612f20c3 | 8722 | PL_xpv_arenaroot = NULL; |
1d7c1841 | 8723 | PL_xpv_root = NULL; |
612f20c3 | 8724 | PL_xpviv_arenaroot = NULL; |
1d7c1841 | 8725 | PL_xpviv_root = NULL; |
612f20c3 | 8726 | PL_xpvnv_arenaroot = NULL; |
1d7c1841 | 8727 | PL_xpvnv_root = NULL; |
612f20c3 | 8728 | PL_xpvcv_arenaroot = NULL; |
1d7c1841 | 8729 | PL_xpvcv_root = NULL; |
612f20c3 | 8730 | PL_xpvav_arenaroot = NULL; |
1d7c1841 | 8731 | PL_xpvav_root = NULL; |
612f20c3 | 8732 | PL_xpvhv_arenaroot = NULL; |
1d7c1841 | 8733 | PL_xpvhv_root = NULL; |
612f20c3 | 8734 | PL_xpvmg_arenaroot = NULL; |
1d7c1841 | 8735 | PL_xpvmg_root = NULL; |
612f20c3 | 8736 | PL_xpvlv_arenaroot = NULL; |
1d7c1841 | 8737 | PL_xpvlv_root = NULL; |
612f20c3 | 8738 | PL_xpvbm_arenaroot = NULL; |
1d7c1841 | 8739 | PL_xpvbm_root = NULL; |
612f20c3 | 8740 | PL_he_arenaroot = NULL; |
1d7c1841 GS |
8741 | PL_he_root = NULL; |
8742 | PL_nice_chunk = NULL; | |
8743 | PL_nice_chunk_size = 0; | |
8744 | PL_sv_count = 0; | |
8745 | PL_sv_objcount = 0; | |
8746 | PL_sv_root = Nullsv; | |
8747 | PL_sv_arenaroot = Nullsv; | |
8748 | ||
8749 | PL_debug = proto_perl->Idebug; | |
8750 | ||
8751 | /* create SV map for pointer relocation */ | |
8752 | PL_ptr_table = ptr_table_new(); | |
8753 | ||
8754 | /* initialize these special pointers as early as possible */ | |
8755 | SvANY(&PL_sv_undef) = NULL; | |
8756 | SvREFCNT(&PL_sv_undef) = (~(U32)0)/2; | |
8757 | SvFLAGS(&PL_sv_undef) = SVf_READONLY|SVt_NULL; | |
8758 | ptr_table_store(PL_ptr_table, &proto_perl->Isv_undef, &PL_sv_undef); | |
8759 | ||
8760 | #ifdef PERL_OBJECT | |
8761 | SvUPGRADE(&PL_sv_no, SVt_PVNV); | |
8762 | #else | |
8763 | SvANY(&PL_sv_no) = new_XPVNV(); | |
8764 | #endif | |
8765 | SvREFCNT(&PL_sv_no) = (~(U32)0)/2; | |
8766 | SvFLAGS(&PL_sv_no) = SVp_NOK|SVf_NOK|SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV; | |
8767 | SvPVX(&PL_sv_no) = SAVEPVN(PL_No, 0); | |
8768 | SvCUR(&PL_sv_no) = 0; | |
8769 | SvLEN(&PL_sv_no) = 1; | |
8770 | SvNVX(&PL_sv_no) = 0; | |
8771 | ptr_table_store(PL_ptr_table, &proto_perl->Isv_no, &PL_sv_no); | |
8772 | ||
8773 | #ifdef PERL_OBJECT | |
8774 | SvUPGRADE(&PL_sv_yes, SVt_PVNV); | |
8775 | #else | |
8776 | SvANY(&PL_sv_yes) = new_XPVNV(); | |
8777 | #endif | |
8778 | SvREFCNT(&PL_sv_yes) = (~(U32)0)/2; | |
8779 | SvFLAGS(&PL_sv_yes) = SVp_NOK|SVf_NOK|SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV; | |
8780 | SvPVX(&PL_sv_yes) = SAVEPVN(PL_Yes, 1); | |
8781 | SvCUR(&PL_sv_yes) = 1; | |
8782 | SvLEN(&PL_sv_yes) = 2; | |
8783 | SvNVX(&PL_sv_yes) = 1; | |
8784 | ptr_table_store(PL_ptr_table, &proto_perl->Isv_yes, &PL_sv_yes); | |
8785 | ||
8786 | /* create shared string table */ | |
8787 | PL_strtab = newHV(); | |
8788 | HvSHAREKEYS_off(PL_strtab); | |
8789 | hv_ksplit(PL_strtab, 512); | |
8790 | ptr_table_store(PL_ptr_table, proto_perl->Istrtab, PL_strtab); | |
8791 | ||
8792 | PL_compiling = proto_perl->Icompiling; | |
8793 | PL_compiling.cop_stashpv = SAVEPV(PL_compiling.cop_stashpv); | |
8794 | PL_compiling.cop_file = SAVEPV(PL_compiling.cop_file); | |
8795 | ptr_table_store(PL_ptr_table, &proto_perl->Icompiling, &PL_compiling); | |
8796 | if (!specialWARN(PL_compiling.cop_warnings)) | |
8797 | PL_compiling.cop_warnings = sv_dup_inc(PL_compiling.cop_warnings); | |
ac27b0f5 NIS |
8798 | if (!specialCopIO(PL_compiling.cop_io)) |
8799 | PL_compiling.cop_io = sv_dup_inc(PL_compiling.cop_io); | |
1d7c1841 GS |
8800 | PL_curcop = (COP*)any_dup(proto_perl->Tcurcop, proto_perl); |
8801 | ||
8802 | /* pseudo environmental stuff */ | |
8803 | PL_origargc = proto_perl->Iorigargc; | |
8804 | i = PL_origargc; | |
8805 | New(0, PL_origargv, i+1, char*); | |
8806 | PL_origargv[i] = '\0'; | |
8807 | while (i-- > 0) { | |
8808 | PL_origargv[i] = SAVEPV(proto_perl->Iorigargv[i]); | |
8809 | } | |
8810 | PL_envgv = gv_dup(proto_perl->Ienvgv); | |
8811 | PL_incgv = gv_dup(proto_perl->Iincgv); | |
8812 | PL_hintgv = gv_dup(proto_perl->Ihintgv); | |
8813 | PL_origfilename = SAVEPV(proto_perl->Iorigfilename); | |
8814 | PL_diehook = sv_dup_inc(proto_perl->Idiehook); | |
8815 | PL_warnhook = sv_dup_inc(proto_perl->Iwarnhook); | |
8816 | ||
8817 | /* switches */ | |
8818 | PL_minus_c = proto_perl->Iminus_c; | |
a7cb1f99 | 8819 | PL_patchlevel = sv_dup_inc(proto_perl->Ipatchlevel); |
1d7c1841 GS |
8820 | PL_localpatches = proto_perl->Ilocalpatches; |
8821 | PL_splitstr = proto_perl->Isplitstr; | |
8822 | PL_preprocess = proto_perl->Ipreprocess; | |
8823 | PL_minus_n = proto_perl->Iminus_n; | |
8824 | PL_minus_p = proto_perl->Iminus_p; | |
8825 | PL_minus_l = proto_perl->Iminus_l; | |
8826 | PL_minus_a = proto_perl->Iminus_a; | |
8827 | PL_minus_F = proto_perl->Iminus_F; | |
8828 | PL_doswitches = proto_perl->Idoswitches; | |
8829 | PL_dowarn = proto_perl->Idowarn; | |
8830 | PL_doextract = proto_perl->Idoextract; | |
8831 | PL_sawampersand = proto_perl->Isawampersand; | |
8832 | PL_unsafe = proto_perl->Iunsafe; | |
8833 | PL_inplace = SAVEPV(proto_perl->Iinplace); | |
8834 | PL_e_script = sv_dup_inc(proto_perl->Ie_script); | |
8835 | PL_perldb = proto_perl->Iperldb; | |
8836 | PL_perl_destruct_level = proto_perl->Iperl_destruct_level; | |
8837 | ||
8838 | /* magical thingies */ | |
8839 | /* XXX time(&PL_basetime) when asked for? */ | |
8840 | PL_basetime = proto_perl->Ibasetime; | |
8841 | PL_formfeed = sv_dup(proto_perl->Iformfeed); | |
8842 | ||
8843 | PL_maxsysfd = proto_perl->Imaxsysfd; | |
8844 | PL_multiline = proto_perl->Imultiline; | |
8845 | PL_statusvalue = proto_perl->Istatusvalue; | |
8846 | #ifdef VMS | |
8847 | PL_statusvalue_vms = proto_perl->Istatusvalue_vms; | |
8848 | #endif | |
8849 | ||
8850 | /* shortcuts to various I/O objects */ | |
8851 | PL_stdingv = gv_dup(proto_perl->Istdingv); | |
8852 | PL_stderrgv = gv_dup(proto_perl->Istderrgv); | |
8853 | PL_defgv = gv_dup(proto_perl->Idefgv); | |
8854 | PL_argvgv = gv_dup(proto_perl->Iargvgv); | |
8855 | PL_argvoutgv = gv_dup(proto_perl->Iargvoutgv); | |
f25c30a3 | 8856 | PL_argvout_stack = av_dup_inc(proto_perl->Iargvout_stack); |
1d7c1841 GS |
8857 | |
8858 | /* shortcuts to regexp stuff */ | |
8859 | PL_replgv = gv_dup(proto_perl->Ireplgv); | |
8860 | ||
8861 | /* shortcuts to misc objects */ | |
8862 | PL_errgv = gv_dup(proto_perl->Ierrgv); | |
8863 | ||
8864 | /* shortcuts to debugging objects */ | |
8865 | PL_DBgv = gv_dup(proto_perl->IDBgv); | |
8866 | PL_DBline = gv_dup(proto_perl->IDBline); | |
8867 | PL_DBsub = gv_dup(proto_perl->IDBsub); | |
8868 | PL_DBsingle = sv_dup(proto_perl->IDBsingle); | |
8869 | PL_DBtrace = sv_dup(proto_perl->IDBtrace); | |
8870 | PL_DBsignal = sv_dup(proto_perl->IDBsignal); | |
8871 | PL_lineary = av_dup(proto_perl->Ilineary); | |
8872 | PL_dbargs = av_dup(proto_perl->Idbargs); | |
8873 | ||
8874 | /* symbol tables */ | |
8875 | PL_defstash = hv_dup_inc(proto_perl->Tdefstash); | |
8876 | PL_curstash = hv_dup(proto_perl->Tcurstash); | |
8877 | PL_debstash = hv_dup(proto_perl->Idebstash); | |
8878 | PL_globalstash = hv_dup(proto_perl->Iglobalstash); | |
8879 | PL_curstname = sv_dup_inc(proto_perl->Icurstname); | |
8880 | ||
8881 | PL_beginav = av_dup_inc(proto_perl->Ibeginav); | |
8882 | PL_endav = av_dup_inc(proto_perl->Iendav); | |
7d30b5c4 | 8883 | PL_checkav = av_dup_inc(proto_perl->Icheckav); |
1d7c1841 GS |
8884 | PL_initav = av_dup_inc(proto_perl->Iinitav); |
8885 | ||
8886 | PL_sub_generation = proto_perl->Isub_generation; | |
8887 | ||
8888 | /* funky return mechanisms */ | |
8889 | PL_forkprocess = proto_perl->Iforkprocess; | |
8890 | ||
8891 | /* subprocess state */ | |
8892 | PL_fdpid = av_dup_inc(proto_perl->Ifdpid); | |
8893 | ||
8894 | /* internal state */ | |
8895 | PL_tainting = proto_perl->Itainting; | |
8896 | PL_maxo = proto_perl->Imaxo; | |
8897 | if (proto_perl->Iop_mask) | |
8898 | PL_op_mask = SAVEPVN(proto_perl->Iop_mask, PL_maxo); | |
8899 | else | |
8900 | PL_op_mask = Nullch; | |
8901 | ||
8902 | /* current interpreter roots */ | |
8903 | PL_main_cv = cv_dup_inc(proto_perl->Imain_cv); | |
8904 | PL_main_root = OpREFCNT_inc(proto_perl->Imain_root); | |
8905 | PL_main_start = proto_perl->Imain_start; | |
e977893f | 8906 | PL_eval_root = proto_perl->Ieval_root; |
1d7c1841 GS |
8907 | PL_eval_start = proto_perl->Ieval_start; |
8908 | ||
8909 | /* runtime control stuff */ | |
8910 | PL_curcopdb = (COP*)any_dup(proto_perl->Icurcopdb, proto_perl); | |
8911 | PL_copline = proto_perl->Icopline; | |
8912 | ||
8913 | PL_filemode = proto_perl->Ifilemode; | |
8914 | PL_lastfd = proto_perl->Ilastfd; | |
8915 | PL_oldname = proto_perl->Ioldname; /* XXX not quite right */ | |
8916 | PL_Argv = NULL; | |
8917 | PL_Cmd = Nullch; | |
8918 | PL_gensym = proto_perl->Igensym; | |
8919 | PL_preambled = proto_perl->Ipreambled; | |
8920 | PL_preambleav = av_dup_inc(proto_perl->Ipreambleav); | |
8921 | PL_laststatval = proto_perl->Ilaststatval; | |
8922 | PL_laststype = proto_perl->Ilaststype; | |
8923 | PL_mess_sv = Nullsv; | |
8924 | ||
7889fe52 | 8925 | PL_ors_sv = sv_dup_inc(proto_perl->Iors_sv); |
1d7c1841 GS |
8926 | PL_ofmt = SAVEPV(proto_perl->Iofmt); |
8927 | ||
8928 | /* interpreter atexit processing */ | |
8929 | PL_exitlistlen = proto_perl->Iexitlistlen; | |
8930 | if (PL_exitlistlen) { | |
8931 | New(0, PL_exitlist, PL_exitlistlen, PerlExitListEntry); | |
8932 | Copy(proto_perl->Iexitlist, PL_exitlist, PL_exitlistlen, PerlExitListEntry); | |
8933 | } | |
8934 | else | |
8935 | PL_exitlist = (PerlExitListEntry*)NULL; | |
8936 | PL_modglobal = hv_dup_inc(proto_perl->Imodglobal); | |
8937 | ||
8938 | PL_profiledata = NULL; | |
8939 | PL_rsfp = fp_dup(proto_perl->Irsfp, '<'); | |
8940 | /* PL_rsfp_filters entries have fake IoDIRP() */ | |
8941 | PL_rsfp_filters = av_dup_inc(proto_perl->Irsfp_filters); | |
8942 | ||
8943 | PL_compcv = cv_dup(proto_perl->Icompcv); | |
8944 | PL_comppad = av_dup(proto_perl->Icomppad); | |
8945 | PL_comppad_name = av_dup(proto_perl->Icomppad_name); | |
8946 | PL_comppad_name_fill = proto_perl->Icomppad_name_fill; | |
8947 | PL_comppad_name_floor = proto_perl->Icomppad_name_floor; | |
8948 | PL_curpad = (SV**)ptr_table_fetch(PL_ptr_table, | |
8949 | proto_perl->Tcurpad); | |
8950 | ||
8951 | #ifdef HAVE_INTERP_INTERN | |
8952 | sys_intern_dup(&proto_perl->Isys_intern, &PL_sys_intern); | |
8953 | #endif | |
8954 | ||
8955 | /* more statics moved here */ | |
8956 | PL_generation = proto_perl->Igeneration; | |
8957 | PL_DBcv = cv_dup(proto_perl->IDBcv); | |
1d7c1841 GS |
8958 | |
8959 | PL_in_clean_objs = proto_perl->Iin_clean_objs; | |
8960 | PL_in_clean_all = proto_perl->Iin_clean_all; | |
8961 | ||
8962 | PL_uid = proto_perl->Iuid; | |
8963 | PL_euid = proto_perl->Ieuid; | |
8964 | PL_gid = proto_perl->Igid; | |
8965 | PL_egid = proto_perl->Iegid; | |
8966 | PL_nomemok = proto_perl->Inomemok; | |
8967 | PL_an = proto_perl->Ian; | |
8968 | PL_cop_seqmax = proto_perl->Icop_seqmax; | |
8969 | PL_op_seqmax = proto_perl->Iop_seqmax; | |
8970 | PL_evalseq = proto_perl->Ievalseq; | |
8971 | PL_origenviron = proto_perl->Iorigenviron; /* XXX not quite right */ | |
8972 | PL_origalen = proto_perl->Iorigalen; | |
8973 | PL_pidstatus = newHV(); /* XXX flag for cloning? */ | |
8974 | PL_osname = SAVEPV(proto_perl->Iosname); | |
8975 | PL_sh_path = SAVEPV(proto_perl->Ish_path); | |
8976 | PL_sighandlerp = proto_perl->Isighandlerp; | |
8977 | ||
8978 | ||
8979 | PL_runops = proto_perl->Irunops; | |
8980 | ||
8981 | Copy(proto_perl->Itokenbuf, PL_tokenbuf, 256, char); | |
8982 | ||
8983 | #ifdef CSH | |
8984 | PL_cshlen = proto_perl->Icshlen; | |
8985 | PL_cshname = SAVEPVN(proto_perl->Icshname, PL_cshlen); | |
8986 | #endif | |
8987 | ||
8988 | PL_lex_state = proto_perl->Ilex_state; | |
8989 | PL_lex_defer = proto_perl->Ilex_defer; | |
8990 | PL_lex_expect = proto_perl->Ilex_expect; | |
8991 | PL_lex_formbrack = proto_perl->Ilex_formbrack; | |
8992 | PL_lex_dojoin = proto_perl->Ilex_dojoin; | |
8993 | PL_lex_starts = proto_perl->Ilex_starts; | |
8994 | PL_lex_stuff = sv_dup_inc(proto_perl->Ilex_stuff); | |
8995 | PL_lex_repl = sv_dup_inc(proto_perl->Ilex_repl); | |
8996 | PL_lex_op = proto_perl->Ilex_op; | |
8997 | PL_lex_inpat = proto_perl->Ilex_inpat; | |
8998 | PL_lex_inwhat = proto_perl->Ilex_inwhat; | |
8999 | PL_lex_brackets = proto_perl->Ilex_brackets; | |
9000 | i = (PL_lex_brackets < 120 ? 120 : PL_lex_brackets); | |
9001 | PL_lex_brackstack = SAVEPVN(proto_perl->Ilex_brackstack,i); | |
9002 | PL_lex_casemods = proto_perl->Ilex_casemods; | |
9003 | i = (PL_lex_casemods < 12 ? 12 : PL_lex_casemods); | |
9004 | PL_lex_casestack = SAVEPVN(proto_perl->Ilex_casestack,i); | |
9005 | ||
9006 | Copy(proto_perl->Inextval, PL_nextval, 5, YYSTYPE); | |
9007 | Copy(proto_perl->Inexttype, PL_nexttype, 5, I32); | |
9008 | PL_nexttoke = proto_perl->Inexttoke; | |
9009 | ||
9010 | PL_linestr = sv_dup_inc(proto_perl->Ilinestr); | |
9011 | i = proto_perl->Ibufptr - SvPVX(proto_perl->Ilinestr); | |
9012 | PL_bufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
9013 | i = proto_perl->Ioldbufptr - SvPVX(proto_perl->Ilinestr); | |
9014 | PL_oldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
9015 | i = proto_perl->Ioldoldbufptr - SvPVX(proto_perl->Ilinestr); | |
9016 | PL_oldoldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
9017 | PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr); | |
9018 | i = proto_perl->Ilinestart - SvPVX(proto_perl->Ilinestr); | |
9019 | PL_linestart = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
9020 | PL_pending_ident = proto_perl->Ipending_ident; | |
9021 | PL_sublex_info = proto_perl->Isublex_info; /* XXX not quite right */ | |
9022 | ||
9023 | PL_expect = proto_perl->Iexpect; | |
9024 | ||
9025 | PL_multi_start = proto_perl->Imulti_start; | |
9026 | PL_multi_end = proto_perl->Imulti_end; | |
9027 | PL_multi_open = proto_perl->Imulti_open; | |
9028 | PL_multi_close = proto_perl->Imulti_close; | |
9029 | ||
9030 | PL_error_count = proto_perl->Ierror_count; | |
9031 | PL_subline = proto_perl->Isubline; | |
9032 | PL_subname = sv_dup_inc(proto_perl->Isubname); | |
9033 | ||
9034 | PL_min_intro_pending = proto_perl->Imin_intro_pending; | |
9035 | PL_max_intro_pending = proto_perl->Imax_intro_pending; | |
9036 | PL_padix = proto_perl->Ipadix; | |
9037 | PL_padix_floor = proto_perl->Ipadix_floor; | |
9038 | PL_pad_reset_pending = proto_perl->Ipad_reset_pending; | |
9039 | ||
9040 | i = proto_perl->Ilast_uni - SvPVX(proto_perl->Ilinestr); | |
9041 | PL_last_uni = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
9042 | i = proto_perl->Ilast_lop - SvPVX(proto_perl->Ilinestr); | |
9043 | PL_last_lop = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
9044 | PL_last_lop_op = proto_perl->Ilast_lop_op; | |
9045 | PL_in_my = proto_perl->Iin_my; | |
9046 | PL_in_my_stash = hv_dup(proto_perl->Iin_my_stash); | |
9047 | #ifdef FCRYPT | |
9048 | PL_cryptseen = proto_perl->Icryptseen; | |
9049 | #endif | |
9050 | ||
9051 | PL_hints = proto_perl->Ihints; | |
9052 | ||
9053 | PL_amagic_generation = proto_perl->Iamagic_generation; | |
9054 | ||
9055 | #ifdef USE_LOCALE_COLLATE | |
9056 | PL_collation_ix = proto_perl->Icollation_ix; | |
9057 | PL_collation_name = SAVEPV(proto_perl->Icollation_name); | |
9058 | PL_collation_standard = proto_perl->Icollation_standard; | |
9059 | PL_collxfrm_base = proto_perl->Icollxfrm_base; | |
9060 | PL_collxfrm_mult = proto_perl->Icollxfrm_mult; | |
9061 | #endif /* USE_LOCALE_COLLATE */ | |
9062 | ||
9063 | #ifdef USE_LOCALE_NUMERIC | |
9064 | PL_numeric_name = SAVEPV(proto_perl->Inumeric_name); | |
9065 | PL_numeric_standard = proto_perl->Inumeric_standard; | |
9066 | PL_numeric_local = proto_perl->Inumeric_local; | |
ac634a9a | 9067 | PL_numeric_radix = sv_dup_inc(proto_perl->Inumeric_radix); |
1d7c1841 GS |
9068 | #endif /* !USE_LOCALE_NUMERIC */ |
9069 | ||
9070 | /* utf8 character classes */ | |
9071 | PL_utf8_alnum = sv_dup_inc(proto_perl->Iutf8_alnum); | |
9072 | PL_utf8_alnumc = sv_dup_inc(proto_perl->Iutf8_alnumc); | |
9073 | PL_utf8_ascii = sv_dup_inc(proto_perl->Iutf8_ascii); | |
9074 | PL_utf8_alpha = sv_dup_inc(proto_perl->Iutf8_alpha); | |
9075 | PL_utf8_space = sv_dup_inc(proto_perl->Iutf8_space); | |
9076 | PL_utf8_cntrl = sv_dup_inc(proto_perl->Iutf8_cntrl); | |
9077 | PL_utf8_graph = sv_dup_inc(proto_perl->Iutf8_graph); | |
9078 | PL_utf8_digit = sv_dup_inc(proto_perl->Iutf8_digit); | |
9079 | PL_utf8_upper = sv_dup_inc(proto_perl->Iutf8_upper); | |
9080 | PL_utf8_lower = sv_dup_inc(proto_perl->Iutf8_lower); | |
9081 | PL_utf8_print = sv_dup_inc(proto_perl->Iutf8_print); | |
9082 | PL_utf8_punct = sv_dup_inc(proto_perl->Iutf8_punct); | |
9083 | PL_utf8_xdigit = sv_dup_inc(proto_perl->Iutf8_xdigit); | |
9084 | PL_utf8_mark = sv_dup_inc(proto_perl->Iutf8_mark); | |
9085 | PL_utf8_toupper = sv_dup_inc(proto_perl->Iutf8_toupper); | |
9086 | PL_utf8_totitle = sv_dup_inc(proto_perl->Iutf8_totitle); | |
9087 | PL_utf8_tolower = sv_dup_inc(proto_perl->Iutf8_tolower); | |
9088 | ||
9089 | /* swatch cache */ | |
9090 | PL_last_swash_hv = Nullhv; /* reinits on demand */ | |
9091 | PL_last_swash_klen = 0; | |
9092 | PL_last_swash_key[0]= '\0'; | |
9093 | PL_last_swash_tmps = (U8*)NULL; | |
9094 | PL_last_swash_slen = 0; | |
9095 | ||
9096 | /* perly.c globals */ | |
9097 | PL_yydebug = proto_perl->Iyydebug; | |
9098 | PL_yynerrs = proto_perl->Iyynerrs; | |
9099 | PL_yyerrflag = proto_perl->Iyyerrflag; | |
9100 | PL_yychar = proto_perl->Iyychar; | |
9101 | PL_yyval = proto_perl->Iyyval; | |
9102 | PL_yylval = proto_perl->Iyylval; | |
9103 | ||
9104 | PL_glob_index = proto_perl->Iglob_index; | |
9105 | PL_srand_called = proto_perl->Isrand_called; | |
9106 | PL_uudmap['M'] = 0; /* reinits on demand */ | |
9107 | PL_bitcount = Nullch; /* reinits on demand */ | |
9108 | ||
66fe0623 NIS |
9109 | if (proto_perl->Ipsig_pend) { |
9110 | Newz(0, PL_psig_pend, SIG_SIZE, int); | |
9dd79c3f | 9111 | } |
66fe0623 NIS |
9112 | else { |
9113 | PL_psig_pend = (int*)NULL; | |
9114 | } | |
9115 | ||
1d7c1841 | 9116 | if (proto_perl->Ipsig_ptr) { |
76d3c696 JH |
9117 | Newz(0, PL_psig_ptr, SIG_SIZE, SV*); |
9118 | Newz(0, PL_psig_name, SIG_SIZE, SV*); | |
76d3c696 JH |
9119 | for (i = 1; i < SIG_SIZE; i++) { |
9120 | PL_psig_ptr[i] = sv_dup_inc(proto_perl->Ipsig_ptr[i]); | |
1d7c1841 GS |
9121 | PL_psig_name[i] = sv_dup_inc(proto_perl->Ipsig_name[i]); |
9122 | } | |
9123 | } | |
9124 | else { | |
9125 | PL_psig_ptr = (SV**)NULL; | |
9126 | PL_psig_name = (SV**)NULL; | |
9127 | } | |
9128 | ||
9129 | /* thrdvar.h stuff */ | |
9130 | ||
a0739874 | 9131 | if (flags & CLONEf_COPY_STACKS) { |
1d7c1841 GS |
9132 | /* next allocation will be PL_tmps_stack[PL_tmps_ix+1] */ |
9133 | PL_tmps_ix = proto_perl->Ttmps_ix; | |
9134 | PL_tmps_max = proto_perl->Ttmps_max; | |
9135 | PL_tmps_floor = proto_perl->Ttmps_floor; | |
9136 | Newz(50, PL_tmps_stack, PL_tmps_max, SV*); | |
9137 | i = 0; | |
9138 | while (i <= PL_tmps_ix) { | |
9139 | PL_tmps_stack[i] = sv_dup_inc(proto_perl->Ttmps_stack[i]); | |
9140 | ++i; | |
9141 | } | |
9142 | ||
9143 | /* next PUSHMARK() sets *(PL_markstack_ptr+1) */ | |
9144 | i = proto_perl->Tmarkstack_max - proto_perl->Tmarkstack; | |
9145 | Newz(54, PL_markstack, i, I32); | |
9146 | PL_markstack_max = PL_markstack + (proto_perl->Tmarkstack_max | |
9147 | - proto_perl->Tmarkstack); | |
9148 | PL_markstack_ptr = PL_markstack + (proto_perl->Tmarkstack_ptr | |
9149 | - proto_perl->Tmarkstack); | |
9150 | Copy(proto_perl->Tmarkstack, PL_markstack, | |
9151 | PL_markstack_ptr - PL_markstack + 1, I32); | |
9152 | ||
9153 | /* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix] | |
9154 | * NOTE: unlike the others! */ | |
9155 | PL_scopestack_ix = proto_perl->Tscopestack_ix; | |
9156 | PL_scopestack_max = proto_perl->Tscopestack_max; | |
9157 | Newz(54, PL_scopestack, PL_scopestack_max, I32); | |
9158 | Copy(proto_perl->Tscopestack, PL_scopestack, PL_scopestack_ix, I32); | |
9159 | ||
9160 | /* next push_return() sets PL_retstack[PL_retstack_ix] | |
9161 | * NOTE: unlike the others! */ | |
9162 | PL_retstack_ix = proto_perl->Tretstack_ix; | |
9163 | PL_retstack_max = proto_perl->Tretstack_max; | |
9164 | Newz(54, PL_retstack, PL_retstack_max, OP*); | |
9165 | Copy(proto_perl->Tretstack, PL_retstack, PL_retstack_ix, I32); | |
9166 | ||
9167 | /* NOTE: si_dup() looks at PL_markstack */ | |
9168 | PL_curstackinfo = si_dup(proto_perl->Tcurstackinfo); | |
9169 | ||
9170 | /* PL_curstack = PL_curstackinfo->si_stack; */ | |
9171 | PL_curstack = av_dup(proto_perl->Tcurstack); | |
9172 | PL_mainstack = av_dup(proto_perl->Tmainstack); | |
9173 | ||
9174 | /* next PUSHs() etc. set *(PL_stack_sp+1) */ | |
9175 | PL_stack_base = AvARRAY(PL_curstack); | |
9176 | PL_stack_sp = PL_stack_base + (proto_perl->Tstack_sp | |
9177 | - proto_perl->Tstack_base); | |
9178 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); | |
9179 | ||
9180 | /* next SSPUSHFOO() sets PL_savestack[PL_savestack_ix] | |
9181 | * NOTE: unlike the others! */ | |
9182 | PL_savestack_ix = proto_perl->Tsavestack_ix; | |
9183 | PL_savestack_max = proto_perl->Tsavestack_max; | |
9184 | /*Newz(54, PL_savestack, PL_savestack_max, ANY);*/ | |
9185 | PL_savestack = ss_dup(proto_perl); | |
9186 | } | |
9187 | else { | |
9188 | init_stacks(); | |
985e7056 | 9189 | ENTER; /* perl_destruct() wants to LEAVE; */ |
1d7c1841 GS |
9190 | } |
9191 | ||
9192 | PL_start_env = proto_perl->Tstart_env; /* XXXXXX */ | |
9193 | PL_top_env = &PL_start_env; | |
9194 | ||
9195 | PL_op = proto_perl->Top; | |
9196 | ||
9197 | PL_Sv = Nullsv; | |
9198 | PL_Xpv = (XPV*)NULL; | |
9199 | PL_na = proto_perl->Tna; | |
9200 | ||
9201 | PL_statbuf = proto_perl->Tstatbuf; | |
9202 | PL_statcache = proto_perl->Tstatcache; | |
9203 | PL_statgv = gv_dup(proto_perl->Tstatgv); | |
9204 | PL_statname = sv_dup_inc(proto_perl->Tstatname); | |
9205 | #ifdef HAS_TIMES | |
9206 | PL_timesbuf = proto_perl->Ttimesbuf; | |
9207 | #endif | |
9208 | ||
9209 | PL_tainted = proto_perl->Ttainted; | |
9210 | PL_curpm = proto_perl->Tcurpm; /* XXX No PMOP ref count */ | |
9211 | PL_nrs = sv_dup_inc(proto_perl->Tnrs); | |
9212 | PL_rs = sv_dup_inc(proto_perl->Trs); | |
9213 | PL_last_in_gv = gv_dup(proto_perl->Tlast_in_gv); | |
7889fe52 | 9214 | PL_ofs_sv = sv_dup_inc(proto_perl->Tofs_sv); |
1d7c1841 GS |
9215 | PL_defoutgv = gv_dup_inc(proto_perl->Tdefoutgv); |
9216 | PL_chopset = proto_perl->Tchopset; /* XXX never deallocated */ | |
9217 | PL_toptarget = sv_dup_inc(proto_perl->Ttoptarget); | |
9218 | PL_bodytarget = sv_dup_inc(proto_perl->Tbodytarget); | |
9219 | PL_formtarget = sv_dup(proto_perl->Tformtarget); | |
9220 | ||
9221 | PL_restartop = proto_perl->Trestartop; | |
9222 | PL_in_eval = proto_perl->Tin_eval; | |
9223 | PL_delaymagic = proto_perl->Tdelaymagic; | |
9224 | PL_dirty = proto_perl->Tdirty; | |
9225 | PL_localizing = proto_perl->Tlocalizing; | |
9226 | ||
14dd3ad8 | 9227 | #ifdef PERL_FLEXIBLE_EXCEPTIONS |
1d7c1841 | 9228 | PL_protect = proto_perl->Tprotect; |
14dd3ad8 | 9229 | #endif |
1d7c1841 GS |
9230 | PL_errors = sv_dup_inc(proto_perl->Terrors); |
9231 | PL_av_fetch_sv = Nullsv; | |
9232 | PL_hv_fetch_sv = Nullsv; | |
9233 | Zero(&PL_hv_fetch_ent_mh, 1, HE); /* XXX */ | |
9234 | PL_modcount = proto_perl->Tmodcount; | |
9235 | PL_lastgotoprobe = Nullop; | |
9236 | PL_dumpindent = proto_perl->Tdumpindent; | |
9237 | ||
9238 | PL_sortcop = (OP*)any_dup(proto_perl->Tsortcop, proto_perl); | |
9239 | PL_sortstash = hv_dup(proto_perl->Tsortstash); | |
9240 | PL_firstgv = gv_dup(proto_perl->Tfirstgv); | |
9241 | PL_secondgv = gv_dup(proto_perl->Tsecondgv); | |
9242 | PL_sortcxix = proto_perl->Tsortcxix; | |
9243 | PL_efloatbuf = Nullch; /* reinits on demand */ | |
9244 | PL_efloatsize = 0; /* reinits on demand */ | |
9245 | ||
9246 | /* regex stuff */ | |
9247 | ||
9248 | PL_screamfirst = NULL; | |
9249 | PL_screamnext = NULL; | |
9250 | PL_maxscream = -1; /* reinits on demand */ | |
9251 | PL_lastscream = Nullsv; | |
9252 | ||
9253 | PL_watchaddr = NULL; | |
9254 | PL_watchok = Nullch; | |
9255 | ||
9256 | PL_regdummy = proto_perl->Tregdummy; | |
9257 | PL_regcomp_parse = Nullch; | |
9258 | PL_regxend = Nullch; | |
9259 | PL_regcode = (regnode*)NULL; | |
9260 | PL_regnaughty = 0; | |
9261 | PL_regsawback = 0; | |
9262 | PL_regprecomp = Nullch; | |
9263 | PL_regnpar = 0; | |
9264 | PL_regsize = 0; | |
9265 | PL_regflags = 0; | |
9266 | PL_regseen = 0; | |
9267 | PL_seen_zerolen = 0; | |
9268 | PL_seen_evals = 0; | |
9269 | PL_regcomp_rx = (regexp*)NULL; | |
9270 | PL_extralen = 0; | |
9271 | PL_colorset = 0; /* reinits PL_colors[] */ | |
9272 | /*PL_colors[6] = {0,0,0,0,0,0};*/ | |
9273 | PL_reg_whilem_seen = 0; | |
9274 | PL_reginput = Nullch; | |
9275 | PL_regbol = Nullch; | |
9276 | PL_regeol = Nullch; | |
9277 | PL_regstartp = (I32*)NULL; | |
9278 | PL_regendp = (I32*)NULL; | |
9279 | PL_reglastparen = (U32*)NULL; | |
9280 | PL_regtill = Nullch; | |
9281 | PL_regprev = '\n'; | |
9282 | PL_reg_start_tmp = (char**)NULL; | |
9283 | PL_reg_start_tmpl = 0; | |
9284 | PL_regdata = (struct reg_data*)NULL; | |
9285 | PL_bostr = Nullch; | |
9286 | PL_reg_flags = 0; | |
9287 | PL_reg_eval_set = 0; | |
9288 | PL_regnarrate = 0; | |
9289 | PL_regprogram = (regnode*)NULL; | |
9290 | PL_regindent = 0; | |
9291 | PL_regcc = (CURCUR*)NULL; | |
9292 | PL_reg_call_cc = (struct re_cc_state*)NULL; | |
9293 | PL_reg_re = (regexp*)NULL; | |
9294 | PL_reg_ganch = Nullch; | |
9295 | PL_reg_sv = Nullsv; | |
9296 | PL_reg_magic = (MAGIC*)NULL; | |
9297 | PL_reg_oldpos = 0; | |
9298 | PL_reg_oldcurpm = (PMOP*)NULL; | |
9299 | PL_reg_curpm = (PMOP*)NULL; | |
9300 | PL_reg_oldsaved = Nullch; | |
9301 | PL_reg_oldsavedlen = 0; | |
9302 | PL_reg_maxiter = 0; | |
9303 | PL_reg_leftiter = 0; | |
9304 | PL_reg_poscache = Nullch; | |
9305 | PL_reg_poscache_size= 0; | |
9306 | ||
9307 | /* RE engine - function pointers */ | |
9308 | PL_regcompp = proto_perl->Tregcompp; | |
9309 | PL_regexecp = proto_perl->Tregexecp; | |
9310 | PL_regint_start = proto_perl->Tregint_start; | |
9311 | PL_regint_string = proto_perl->Tregint_string; | |
9312 | PL_regfree = proto_perl->Tregfree; | |
9313 | ||
9314 | PL_reginterp_cnt = 0; | |
9315 | PL_reg_starttry = 0; | |
9316 | ||
a0739874 DM |
9317 | if (!(flags & CLONEf_KEEP_PTR_TABLE)) { |
9318 | ptr_table_free(PL_ptr_table); | |
9319 | PL_ptr_table = NULL; | |
9320 | } | |
9321 | ||
1d7c1841 GS |
9322 | #ifdef PERL_OBJECT |
9323 | return (PerlInterpreter*)pPerl; | |
9324 | #else | |
9325 | return my_perl; | |
9326 | #endif | |
9327 | } | |
9328 | ||
9329 | #else /* !USE_ITHREADS */ | |
51371543 GS |
9330 | |
9331 | #ifdef PERL_OBJECT | |
51371543 GS |
9332 | #include "XSUB.h" |
9333 | #endif | |
9334 | ||
1d7c1841 GS |
9335 | #endif /* USE_ITHREADS */ |
9336 | ||
51371543 GS |
9337 | static void |
9338 | do_report_used(pTHXo_ SV *sv) | |
9339 | { | |
9340 | if (SvTYPE(sv) != SVTYPEMASK) { | |
bf49b057 | 9341 | PerlIO_printf(Perl_debug_log, "****\n"); |
51371543 GS |
9342 | sv_dump(sv); |
9343 | } | |
9344 | } | |
9345 | ||
9346 | static void | |
9347 | do_clean_objs(pTHXo_ SV *sv) | |
9348 | { | |
9349 | SV* rv; | |
9350 | ||
9351 | if (SvROK(sv) && SvOBJECT(rv = SvRV(sv))) { | |
9352 | DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(sv));) | |
8b6e653b HS |
9353 | if (SvWEAKREF(sv)) { |
9354 | sv_del_backref(sv); | |
9355 | SvWEAKREF_off(sv); | |
9356 | SvRV(sv) = 0; | |
9357 | } else { | |
9358 | SvROK_off(sv); | |
9359 | SvRV(sv) = 0; | |
9360 | SvREFCNT_dec(rv); | |
9361 | } | |
51371543 GS |
9362 | } |
9363 | ||
9364 | /* XXX Might want to check arrays, etc. */ | |
9365 | } | |
9366 | ||
9367 | #ifndef DISABLE_DESTRUCTOR_KLUDGE | |
9368 | static void | |
9369 | do_clean_named_objs(pTHXo_ SV *sv) | |
9370 | { | |
f472eb5c | 9371 | if (SvTYPE(sv) == SVt_PVGV && GvGP(sv)) { |
51371543 | 9372 | if ( SvOBJECT(GvSV(sv)) || |
155aba94 GS |
9373 | (GvAV(sv) && SvOBJECT(GvAV(sv))) || |
9374 | (GvHV(sv) && SvOBJECT(GvHV(sv))) || | |
9375 | (GvIO(sv) && SvOBJECT(GvIO(sv))) || | |
9376 | (GvCV(sv) && SvOBJECT(GvCV(sv))) ) | |
51371543 GS |
9377 | { |
9378 | DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning named glob object:\n "), sv_dump(sv));) | |
9379 | SvREFCNT_dec(sv); | |
9380 | } | |
9381 | } | |
9382 | } | |
9383 | #endif | |
9384 | ||
9385 | static void | |
9386 | do_clean_all(pTHXo_ SV *sv) | |
9387 | { | |
1d7c1841 | 9388 | DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) );) |
51371543 GS |
9389 | SvFLAGS(sv) |= SVf_BREAK; |
9390 | SvREFCNT_dec(sv); | |
9391 | } | |
8af02333 | 9392 |