Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | /* sv.c |
79072805 | 2 | * |
bc89e66f | 3 | * Copyright (c) 1991-2001, Larry Wall |
79072805 LW |
4 | * |
5 | * You may distribute under the terms of either the GNU General Public | |
6 | * License or the Artistic License, as specified in the README file. | |
7 | * | |
a0d0e21e LW |
8 | */ |
9 | ||
10 | /* | |
11 | * "I wonder what the Entish is for 'yes' and 'no'," he thought. | |
79072805 LW |
12 | */ |
13 | ||
14 | #include "EXTERN.h" | |
864dbfa3 | 15 | #define PERL_IN_SV_C |
79072805 | 16 | #include "perl.h" |
79072805 | 17 | |
51371543 | 18 | #define FCALL *f |
6fc92669 | 19 | #define SV_CHECK_THINKFIRST(sv) if (SvTHINKFIRST(sv)) sv_force_normal(sv) |
2c5424a7 | 20 | |
51371543 GS |
21 | static void do_report_used(pTHXo_ SV *sv); |
22 | static void do_clean_objs(pTHXo_ SV *sv); | |
23 | #ifndef DISABLE_DESTRUCTOR_KLUDGE | |
24 | static void do_clean_named_objs(pTHXo_ SV *sv); | |
25 | #endif | |
26 | static void do_clean_all(pTHXo_ SV *sv); | |
27 | ||
4561caa4 CS |
28 | /* |
29 | * "A time to plant, and a time to uproot what was planted..." | |
30 | */ | |
31 | ||
053fc874 GS |
32 | #define plant_SV(p) \ |
33 | STMT_START { \ | |
34 | SvANY(p) = (void *)PL_sv_root; \ | |
35 | SvFLAGS(p) = SVTYPEMASK; \ | |
36 | PL_sv_root = (p); \ | |
37 | --PL_sv_count; \ | |
38 | } STMT_END | |
a0d0e21e | 39 | |
fba3b22e | 40 | /* sv_mutex must be held while calling uproot_SV() */ |
053fc874 GS |
41 | #define uproot_SV(p) \ |
42 | STMT_START { \ | |
43 | (p) = PL_sv_root; \ | |
44 | PL_sv_root = (SV*)SvANY(p); \ | |
45 | ++PL_sv_count; \ | |
46 | } STMT_END | |
47 | ||
48 | #define new_SV(p) \ | |
49 | STMT_START { \ | |
50 | LOCK_SV_MUTEX; \ | |
51 | if (PL_sv_root) \ | |
52 | uproot_SV(p); \ | |
53 | else \ | |
54 | (p) = more_sv(); \ | |
55 | UNLOCK_SV_MUTEX; \ | |
56 | SvANY(p) = 0; \ | |
57 | SvREFCNT(p) = 1; \ | |
58 | SvFLAGS(p) = 0; \ | |
59 | } STMT_END | |
463ee0b2 | 60 | |
a0d0e21e | 61 | #ifdef DEBUGGING |
4561caa4 | 62 | |
053fc874 GS |
63 | #define del_SV(p) \ |
64 | STMT_START { \ | |
65 | LOCK_SV_MUTEX; \ | |
66 | if (PL_debug & 32768) \ | |
67 | del_sv(p); \ | |
68 | else \ | |
69 | plant_SV(p); \ | |
70 | UNLOCK_SV_MUTEX; \ | |
71 | } STMT_END | |
a0d0e21e | 72 | |
76e3520e | 73 | STATIC void |
cea2e8a9 | 74 | S_del_sv(pTHX_ SV *p) |
463ee0b2 | 75 | { |
3280af22 | 76 | if (PL_debug & 32768) { |
4633a7c4 | 77 | SV* sva; |
a0d0e21e LW |
78 | SV* sv; |
79 | SV* svend; | |
80 | int ok = 0; | |
3280af22 | 81 | for (sva = PL_sv_arenaroot; sva; sva = (SV *) SvANY(sva)) { |
4633a7c4 LW |
82 | sv = sva + 1; |
83 | svend = &sva[SvREFCNT(sva)]; | |
a0d0e21e LW |
84 | if (p >= sv && p < svend) |
85 | ok = 1; | |
86 | } | |
87 | if (!ok) { | |
0453d815 PM |
88 | if (ckWARN_d(WARN_INTERNAL)) |
89 | Perl_warner(aTHX_ WARN_INTERNAL, | |
1d7c1841 GS |
90 | "Attempt to free non-arena SV: 0x%"UVxf, |
91 | PTR2UV(p)); | |
a0d0e21e LW |
92 | return; |
93 | } | |
94 | } | |
4561caa4 | 95 | plant_SV(p); |
463ee0b2 | 96 | } |
a0d0e21e | 97 | |
4561caa4 CS |
98 | #else /* ! DEBUGGING */ |
99 | ||
100 | #define del_SV(p) plant_SV(p) | |
101 | ||
102 | #endif /* DEBUGGING */ | |
463ee0b2 | 103 | |
4633a7c4 | 104 | void |
864dbfa3 | 105 | Perl_sv_add_arena(pTHX_ char *ptr, U32 size, U32 flags) |
463ee0b2 | 106 | { |
4633a7c4 | 107 | SV* sva = (SV*)ptr; |
463ee0b2 LW |
108 | register SV* sv; |
109 | register SV* svend; | |
14dd3ad8 | 110 | Zero(ptr, size, char); |
4633a7c4 LW |
111 | |
112 | /* The first SV in an arena isn't an SV. */ | |
3280af22 | 113 | SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */ |
4633a7c4 LW |
114 | SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */ |
115 | SvFLAGS(sva) = flags; /* FAKE if not to be freed */ | |
116 | ||
3280af22 NIS |
117 | PL_sv_arenaroot = sva; |
118 | PL_sv_root = sva + 1; | |
4633a7c4 LW |
119 | |
120 | svend = &sva[SvREFCNT(sva) - 1]; | |
121 | sv = sva + 1; | |
463ee0b2 | 122 | while (sv < svend) { |
a0d0e21e | 123 | SvANY(sv) = (void *)(SV*)(sv + 1); |
8990e307 | 124 | SvFLAGS(sv) = SVTYPEMASK; |
463ee0b2 LW |
125 | sv++; |
126 | } | |
127 | SvANY(sv) = 0; | |
4633a7c4 LW |
128 | SvFLAGS(sv) = SVTYPEMASK; |
129 | } | |
130 | ||
fba3b22e | 131 | /* sv_mutex must be held while calling more_sv() */ |
76e3520e | 132 | STATIC SV* |
cea2e8a9 | 133 | S_more_sv(pTHX) |
4633a7c4 | 134 | { |
4561caa4 CS |
135 | register SV* sv; |
136 | ||
3280af22 NIS |
137 | if (PL_nice_chunk) { |
138 | sv_add_arena(PL_nice_chunk, PL_nice_chunk_size, 0); | |
139 | PL_nice_chunk = Nullch; | |
c07a80fd | 140 | } |
1edc1566 | 141 | else { |
142 | char *chunk; /* must use New here to match call to */ | |
143 | New(704,chunk,1008,char); /* Safefree() in sv_free_arenas() */ | |
144 | sv_add_arena(chunk, 1008, 0); | |
145 | } | |
4561caa4 CS |
146 | uproot_SV(sv); |
147 | return sv; | |
463ee0b2 LW |
148 | } |
149 | ||
76e3520e | 150 | STATIC void |
cea2e8a9 | 151 | S_visit(pTHX_ SVFUNC_t f) |
8990e307 | 152 | { |
4633a7c4 | 153 | SV* sva; |
8990e307 LW |
154 | SV* sv; |
155 | register SV* svend; | |
156 | ||
3280af22 | 157 | for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) { |
4633a7c4 | 158 | svend = &sva[SvREFCNT(sva)]; |
4561caa4 CS |
159 | for (sv = sva + 1; sv < svend; ++sv) { |
160 | if (SvTYPE(sv) != SVTYPEMASK) | |
51371543 | 161 | (FCALL)(aTHXo_ sv); |
8990e307 LW |
162 | } |
163 | } | |
164 | } | |
165 | ||
166 | void | |
864dbfa3 | 167 | Perl_sv_report_used(pTHX) |
4561caa4 | 168 | { |
0b94c7bb | 169 | visit(do_report_used); |
4561caa4 CS |
170 | } |
171 | ||
4561caa4 | 172 | void |
864dbfa3 | 173 | Perl_sv_clean_objs(pTHX) |
4561caa4 | 174 | { |
3280af22 | 175 | PL_in_clean_objs = TRUE; |
0b94c7bb | 176 | visit(do_clean_objs); |
4561caa4 | 177 | #ifndef DISABLE_DESTRUCTOR_KLUDGE |
2d0f3c12 | 178 | /* some barnacles may yet remain, clinging to typeglobs */ |
0b94c7bb | 179 | visit(do_clean_named_objs); |
4561caa4 | 180 | #endif |
3280af22 | 181 | PL_in_clean_objs = FALSE; |
4561caa4 CS |
182 | } |
183 | ||
8990e307 | 184 | void |
864dbfa3 | 185 | Perl_sv_clean_all(pTHX) |
8990e307 | 186 | { |
3280af22 | 187 | PL_in_clean_all = TRUE; |
0b94c7bb | 188 | visit(do_clean_all); |
3280af22 | 189 | PL_in_clean_all = FALSE; |
8990e307 | 190 | } |
463ee0b2 | 191 | |
4633a7c4 | 192 | void |
864dbfa3 | 193 | Perl_sv_free_arenas(pTHX) |
4633a7c4 LW |
194 | { |
195 | SV* sva; | |
196 | SV* svanext; | |
612f20c3 | 197 | XPV *arena, *arenanext; |
4633a7c4 LW |
198 | |
199 | /* Free arenas here, but be careful about fake ones. (We assume | |
200 | contiguity of the fake ones with the corresponding real ones.) */ | |
201 | ||
3280af22 | 202 | for (sva = PL_sv_arenaroot; sva; sva = svanext) { |
4633a7c4 LW |
203 | svanext = (SV*) SvANY(sva); |
204 | while (svanext && SvFAKE(svanext)) | |
205 | svanext = (SV*) SvANY(svanext); | |
206 | ||
207 | if (!SvFAKE(sva)) | |
1edc1566 | 208 | Safefree((void *)sva); |
4633a7c4 | 209 | } |
5f05dabc | 210 | |
612f20c3 GS |
211 | for (arena = PL_xiv_arenaroot; arena; arena = arenanext) { |
212 | arenanext = (XPV*)arena->xpv_pv; | |
213 | Safefree(arena); | |
214 | } | |
215 | PL_xiv_arenaroot = 0; | |
216 | ||
217 | for (arena = PL_xnv_arenaroot; arena; arena = arenanext) { | |
218 | arenanext = (XPV*)arena->xpv_pv; | |
219 | Safefree(arena); | |
220 | } | |
221 | PL_xnv_arenaroot = 0; | |
222 | ||
223 | for (arena = PL_xrv_arenaroot; arena; arena = arenanext) { | |
224 | arenanext = (XPV*)arena->xpv_pv; | |
225 | Safefree(arena); | |
226 | } | |
227 | PL_xrv_arenaroot = 0; | |
228 | ||
229 | for (arena = PL_xpv_arenaroot; arena; arena = arenanext) { | |
230 | arenanext = (XPV*)arena->xpv_pv; | |
231 | Safefree(arena); | |
232 | } | |
233 | PL_xpv_arenaroot = 0; | |
234 | ||
235 | for (arena = (XPV*)PL_xpviv_arenaroot; arena; arena = arenanext) { | |
236 | arenanext = (XPV*)arena->xpv_pv; | |
237 | Safefree(arena); | |
238 | } | |
239 | PL_xpviv_arenaroot = 0; | |
240 | ||
241 | for (arena = (XPV*)PL_xpvnv_arenaroot; arena; arena = arenanext) { | |
242 | arenanext = (XPV*)arena->xpv_pv; | |
243 | Safefree(arena); | |
244 | } | |
245 | PL_xpvnv_arenaroot = 0; | |
246 | ||
247 | for (arena = (XPV*)PL_xpvcv_arenaroot; arena; arena = arenanext) { | |
248 | arenanext = (XPV*)arena->xpv_pv; | |
249 | Safefree(arena); | |
250 | } | |
251 | PL_xpvcv_arenaroot = 0; | |
252 | ||
253 | for (arena = (XPV*)PL_xpvav_arenaroot; arena; arena = arenanext) { | |
254 | arenanext = (XPV*)arena->xpv_pv; | |
255 | Safefree(arena); | |
256 | } | |
257 | PL_xpvav_arenaroot = 0; | |
258 | ||
259 | for (arena = (XPV*)PL_xpvhv_arenaroot; arena; arena = arenanext) { | |
260 | arenanext = (XPV*)arena->xpv_pv; | |
261 | Safefree(arena); | |
262 | } | |
263 | PL_xpvhv_arenaroot = 0; | |
264 | ||
265 | for (arena = (XPV*)PL_xpvmg_arenaroot; arena; arena = arenanext) { | |
266 | arenanext = (XPV*)arena->xpv_pv; | |
267 | Safefree(arena); | |
268 | } | |
269 | PL_xpvmg_arenaroot = 0; | |
270 | ||
271 | for (arena = (XPV*)PL_xpvlv_arenaroot; arena; arena = arenanext) { | |
272 | arenanext = (XPV*)arena->xpv_pv; | |
273 | Safefree(arena); | |
274 | } | |
275 | PL_xpvlv_arenaroot = 0; | |
276 | ||
277 | for (arena = (XPV*)PL_xpvbm_arenaroot; arena; arena = arenanext) { | |
278 | arenanext = (XPV*)arena->xpv_pv; | |
279 | Safefree(arena); | |
280 | } | |
281 | PL_xpvbm_arenaroot = 0; | |
282 | ||
283 | for (arena = (XPV*)PL_he_arenaroot; arena; arena = arenanext) { | |
284 | arenanext = (XPV*)arena->xpv_pv; | |
285 | Safefree(arena); | |
286 | } | |
287 | PL_he_arenaroot = 0; | |
288 | ||
3280af22 NIS |
289 | if (PL_nice_chunk) |
290 | Safefree(PL_nice_chunk); | |
291 | PL_nice_chunk = Nullch; | |
292 | PL_nice_chunk_size = 0; | |
293 | PL_sv_arenaroot = 0; | |
294 | PL_sv_root = 0; | |
4633a7c4 LW |
295 | } |
296 | ||
1d7c1841 GS |
297 | void |
298 | Perl_report_uninit(pTHX) | |
299 | { | |
300 | if (PL_op) | |
301 | Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit, | |
302 | " in ", PL_op_desc[PL_op->op_type]); | |
303 | else | |
304 | Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit, "", ""); | |
305 | } | |
306 | ||
76e3520e | 307 | STATIC XPVIV* |
cea2e8a9 | 308 | S_new_xiv(pTHX) |
463ee0b2 | 309 | { |
ea7c11a3 | 310 | IV* xiv; |
cbe51380 GS |
311 | LOCK_SV_MUTEX; |
312 | if (!PL_xiv_root) | |
313 | more_xiv(); | |
314 | xiv = PL_xiv_root; | |
315 | /* | |
316 | * See comment in more_xiv() -- RAM. | |
317 | */ | |
318 | PL_xiv_root = *(IV**)xiv; | |
319 | UNLOCK_SV_MUTEX; | |
320 | return (XPVIV*)((char*)xiv - STRUCT_OFFSET(XPVIV, xiv_iv)); | |
463ee0b2 LW |
321 | } |
322 | ||
76e3520e | 323 | STATIC void |
cea2e8a9 | 324 | S_del_xiv(pTHX_ XPVIV *p) |
463ee0b2 | 325 | { |
23e6a22f | 326 | IV* xiv = (IV*)((char*)(p) + STRUCT_OFFSET(XPVIV, xiv_iv)); |
cbe51380 | 327 | LOCK_SV_MUTEX; |
3280af22 NIS |
328 | *(IV**)xiv = PL_xiv_root; |
329 | PL_xiv_root = xiv; | |
cbe51380 | 330 | UNLOCK_SV_MUTEX; |
463ee0b2 LW |
331 | } |
332 | ||
cbe51380 | 333 | STATIC void |
cea2e8a9 | 334 | S_more_xiv(pTHX) |
463ee0b2 | 335 | { |
ea7c11a3 SM |
336 | register IV* xiv; |
337 | register IV* xivend; | |
8c52afec IZ |
338 | XPV* ptr; |
339 | New(705, ptr, 1008/sizeof(XPV), XPV); | |
3280af22 NIS |
340 | ptr->xpv_pv = (char*)PL_xiv_arenaroot; /* linked list of xiv arenas */ |
341 | PL_xiv_arenaroot = ptr; /* to keep Purify happy */ | |
a0d0e21e | 342 | |
ea7c11a3 SM |
343 | xiv = (IV*) ptr; |
344 | xivend = &xiv[1008 / sizeof(IV) - 1]; | |
345 | xiv += (sizeof(XPV) - 1) / sizeof(IV) + 1; /* fudge by size of XPV */ | |
3280af22 | 346 | PL_xiv_root = xiv; |
463ee0b2 | 347 | while (xiv < xivend) { |
ea7c11a3 | 348 | *(IV**)xiv = (IV *)(xiv + 1); |
463ee0b2 LW |
349 | xiv++; |
350 | } | |
ea7c11a3 | 351 | *(IV**)xiv = 0; |
463ee0b2 LW |
352 | } |
353 | ||
76e3520e | 354 | STATIC XPVNV* |
cea2e8a9 | 355 | S_new_xnv(pTHX) |
463ee0b2 | 356 | { |
65202027 | 357 | NV* xnv; |
cbe51380 GS |
358 | LOCK_SV_MUTEX; |
359 | if (!PL_xnv_root) | |
360 | more_xnv(); | |
361 | xnv = PL_xnv_root; | |
65202027 | 362 | PL_xnv_root = *(NV**)xnv; |
cbe51380 GS |
363 | UNLOCK_SV_MUTEX; |
364 | return (XPVNV*)((char*)xnv - STRUCT_OFFSET(XPVNV, xnv_nv)); | |
463ee0b2 LW |
365 | } |
366 | ||
76e3520e | 367 | STATIC void |
cea2e8a9 | 368 | S_del_xnv(pTHX_ XPVNV *p) |
463ee0b2 | 369 | { |
65202027 | 370 | NV* xnv = (NV*)((char*)(p) + STRUCT_OFFSET(XPVNV, xnv_nv)); |
cbe51380 | 371 | LOCK_SV_MUTEX; |
65202027 | 372 | *(NV**)xnv = PL_xnv_root; |
3280af22 | 373 | PL_xnv_root = xnv; |
cbe51380 | 374 | UNLOCK_SV_MUTEX; |
463ee0b2 LW |
375 | } |
376 | ||
cbe51380 | 377 | STATIC void |
cea2e8a9 | 378 | S_more_xnv(pTHX) |
463ee0b2 | 379 | { |
65202027 DS |
380 | register NV* xnv; |
381 | register NV* xnvend; | |
612f20c3 GS |
382 | XPV *ptr; |
383 | New(711, ptr, 1008/sizeof(XPV), XPV); | |
384 | ptr->xpv_pv = (char*)PL_xnv_arenaroot; | |
385 | PL_xnv_arenaroot = ptr; | |
386 | ||
387 | xnv = (NV*) ptr; | |
65202027 DS |
388 | xnvend = &xnv[1008 / sizeof(NV) - 1]; |
389 | xnv += (sizeof(XPVIV) - 1) / sizeof(NV) + 1; /* fudge by sizeof XPVIV */ | |
3280af22 | 390 | PL_xnv_root = xnv; |
463ee0b2 | 391 | while (xnv < xnvend) { |
65202027 | 392 | *(NV**)xnv = (NV*)(xnv + 1); |
463ee0b2 LW |
393 | xnv++; |
394 | } | |
65202027 | 395 | *(NV**)xnv = 0; |
463ee0b2 LW |
396 | } |
397 | ||
76e3520e | 398 | STATIC XRV* |
cea2e8a9 | 399 | S_new_xrv(pTHX) |
ed6116ce LW |
400 | { |
401 | XRV* xrv; | |
cbe51380 GS |
402 | LOCK_SV_MUTEX; |
403 | if (!PL_xrv_root) | |
404 | more_xrv(); | |
405 | xrv = PL_xrv_root; | |
406 | PL_xrv_root = (XRV*)xrv->xrv_rv; | |
407 | UNLOCK_SV_MUTEX; | |
408 | return xrv; | |
ed6116ce LW |
409 | } |
410 | ||
76e3520e | 411 | STATIC void |
cea2e8a9 | 412 | S_del_xrv(pTHX_ XRV *p) |
ed6116ce | 413 | { |
cbe51380 | 414 | LOCK_SV_MUTEX; |
3280af22 NIS |
415 | p->xrv_rv = (SV*)PL_xrv_root; |
416 | PL_xrv_root = p; | |
cbe51380 | 417 | UNLOCK_SV_MUTEX; |
ed6116ce LW |
418 | } |
419 | ||
cbe51380 | 420 | STATIC void |
cea2e8a9 | 421 | S_more_xrv(pTHX) |
ed6116ce | 422 | { |
ed6116ce LW |
423 | register XRV* xrv; |
424 | register XRV* xrvend; | |
612f20c3 GS |
425 | XPV *ptr; |
426 | New(712, ptr, 1008/sizeof(XPV), XPV); | |
427 | ptr->xpv_pv = (char*)PL_xrv_arenaroot; | |
428 | PL_xrv_arenaroot = ptr; | |
429 | ||
430 | xrv = (XRV*) ptr; | |
ed6116ce | 431 | xrvend = &xrv[1008 / sizeof(XRV) - 1]; |
612f20c3 GS |
432 | xrv += (sizeof(XPV) - 1) / sizeof(XRV) + 1; |
433 | PL_xrv_root = xrv; | |
ed6116ce LW |
434 | while (xrv < xrvend) { |
435 | xrv->xrv_rv = (SV*)(xrv + 1); | |
436 | xrv++; | |
437 | } | |
438 | xrv->xrv_rv = 0; | |
ed6116ce LW |
439 | } |
440 | ||
76e3520e | 441 | STATIC XPV* |
cea2e8a9 | 442 | S_new_xpv(pTHX) |
463ee0b2 LW |
443 | { |
444 | XPV* xpv; | |
cbe51380 GS |
445 | LOCK_SV_MUTEX; |
446 | if (!PL_xpv_root) | |
447 | more_xpv(); | |
448 | xpv = PL_xpv_root; | |
449 | PL_xpv_root = (XPV*)xpv->xpv_pv; | |
450 | UNLOCK_SV_MUTEX; | |
451 | return xpv; | |
463ee0b2 LW |
452 | } |
453 | ||
76e3520e | 454 | STATIC void |
cea2e8a9 | 455 | S_del_xpv(pTHX_ XPV *p) |
463ee0b2 | 456 | { |
cbe51380 | 457 | LOCK_SV_MUTEX; |
3280af22 NIS |
458 | p->xpv_pv = (char*)PL_xpv_root; |
459 | PL_xpv_root = p; | |
cbe51380 | 460 | UNLOCK_SV_MUTEX; |
463ee0b2 LW |
461 | } |
462 | ||
cbe51380 | 463 | STATIC void |
cea2e8a9 | 464 | S_more_xpv(pTHX) |
463ee0b2 | 465 | { |
463ee0b2 LW |
466 | register XPV* xpv; |
467 | register XPV* xpvend; | |
612f20c3 GS |
468 | New(713, xpv, 1008/sizeof(XPV), XPV); |
469 | xpv->xpv_pv = (char*)PL_xpv_arenaroot; | |
470 | PL_xpv_arenaroot = xpv; | |
471 | ||
463ee0b2 | 472 | xpvend = &xpv[1008 / sizeof(XPV) - 1]; |
612f20c3 | 473 | PL_xpv_root = ++xpv; |
463ee0b2 LW |
474 | while (xpv < xpvend) { |
475 | xpv->xpv_pv = (char*)(xpv + 1); | |
476 | xpv++; | |
477 | } | |
478 | xpv->xpv_pv = 0; | |
463ee0b2 LW |
479 | } |
480 | ||
932e9ff9 VB |
481 | STATIC XPVIV* |
482 | S_new_xpviv(pTHX) | |
483 | { | |
484 | XPVIV* xpviv; | |
485 | LOCK_SV_MUTEX; | |
486 | if (!PL_xpviv_root) | |
487 | more_xpviv(); | |
488 | xpviv = PL_xpviv_root; | |
489 | PL_xpviv_root = (XPVIV*)xpviv->xpv_pv; | |
490 | UNLOCK_SV_MUTEX; | |
491 | return xpviv; | |
492 | } | |
493 | ||
494 | STATIC void | |
495 | S_del_xpviv(pTHX_ XPVIV *p) | |
496 | { | |
497 | LOCK_SV_MUTEX; | |
498 | p->xpv_pv = (char*)PL_xpviv_root; | |
499 | PL_xpviv_root = p; | |
500 | UNLOCK_SV_MUTEX; | |
501 | } | |
502 | ||
932e9ff9 VB |
503 | STATIC void |
504 | S_more_xpviv(pTHX) | |
505 | { | |
506 | register XPVIV* xpviv; | |
507 | register XPVIV* xpvivend; | |
612f20c3 GS |
508 | New(714, xpviv, 1008/sizeof(XPVIV), XPVIV); |
509 | xpviv->xpv_pv = (char*)PL_xpviv_arenaroot; | |
510 | PL_xpviv_arenaroot = xpviv; | |
511 | ||
932e9ff9 | 512 | xpvivend = &xpviv[1008 / sizeof(XPVIV) - 1]; |
612f20c3 | 513 | PL_xpviv_root = ++xpviv; |
932e9ff9 VB |
514 | while (xpviv < xpvivend) { |
515 | xpviv->xpv_pv = (char*)(xpviv + 1); | |
516 | xpviv++; | |
517 | } | |
518 | xpviv->xpv_pv = 0; | |
519 | } | |
520 | ||
932e9ff9 VB |
521 | STATIC XPVNV* |
522 | S_new_xpvnv(pTHX) | |
523 | { | |
524 | XPVNV* xpvnv; | |
525 | LOCK_SV_MUTEX; | |
526 | if (!PL_xpvnv_root) | |
527 | more_xpvnv(); | |
528 | xpvnv = PL_xpvnv_root; | |
529 | PL_xpvnv_root = (XPVNV*)xpvnv->xpv_pv; | |
530 | UNLOCK_SV_MUTEX; | |
531 | return xpvnv; | |
532 | } | |
533 | ||
534 | STATIC void | |
535 | S_del_xpvnv(pTHX_ XPVNV *p) | |
536 | { | |
537 | LOCK_SV_MUTEX; | |
538 | p->xpv_pv = (char*)PL_xpvnv_root; | |
539 | PL_xpvnv_root = p; | |
540 | UNLOCK_SV_MUTEX; | |
541 | } | |
542 | ||
932e9ff9 VB |
543 | STATIC void |
544 | S_more_xpvnv(pTHX) | |
545 | { | |
546 | register XPVNV* xpvnv; | |
547 | register XPVNV* xpvnvend; | |
612f20c3 GS |
548 | New(715, xpvnv, 1008/sizeof(XPVNV), XPVNV); |
549 | xpvnv->xpv_pv = (char*)PL_xpvnv_arenaroot; | |
550 | PL_xpvnv_arenaroot = xpvnv; | |
551 | ||
932e9ff9 | 552 | xpvnvend = &xpvnv[1008 / sizeof(XPVNV) - 1]; |
612f20c3 | 553 | PL_xpvnv_root = ++xpvnv; |
932e9ff9 VB |
554 | while (xpvnv < xpvnvend) { |
555 | xpvnv->xpv_pv = (char*)(xpvnv + 1); | |
556 | xpvnv++; | |
557 | } | |
558 | xpvnv->xpv_pv = 0; | |
559 | } | |
560 | ||
932e9ff9 VB |
561 | STATIC XPVCV* |
562 | S_new_xpvcv(pTHX) | |
563 | { | |
564 | XPVCV* xpvcv; | |
565 | LOCK_SV_MUTEX; | |
566 | if (!PL_xpvcv_root) | |
567 | more_xpvcv(); | |
568 | xpvcv = PL_xpvcv_root; | |
569 | PL_xpvcv_root = (XPVCV*)xpvcv->xpv_pv; | |
570 | UNLOCK_SV_MUTEX; | |
571 | return xpvcv; | |
572 | } | |
573 | ||
574 | STATIC void | |
575 | S_del_xpvcv(pTHX_ XPVCV *p) | |
576 | { | |
577 | LOCK_SV_MUTEX; | |
578 | p->xpv_pv = (char*)PL_xpvcv_root; | |
579 | PL_xpvcv_root = p; | |
580 | UNLOCK_SV_MUTEX; | |
581 | } | |
582 | ||
932e9ff9 VB |
583 | STATIC void |
584 | S_more_xpvcv(pTHX) | |
585 | { | |
586 | register XPVCV* xpvcv; | |
587 | register XPVCV* xpvcvend; | |
612f20c3 GS |
588 | New(716, xpvcv, 1008/sizeof(XPVCV), XPVCV); |
589 | xpvcv->xpv_pv = (char*)PL_xpvcv_arenaroot; | |
590 | PL_xpvcv_arenaroot = xpvcv; | |
591 | ||
932e9ff9 | 592 | xpvcvend = &xpvcv[1008 / sizeof(XPVCV) - 1]; |
612f20c3 | 593 | PL_xpvcv_root = ++xpvcv; |
932e9ff9 VB |
594 | while (xpvcv < xpvcvend) { |
595 | xpvcv->xpv_pv = (char*)(xpvcv + 1); | |
596 | xpvcv++; | |
597 | } | |
598 | xpvcv->xpv_pv = 0; | |
599 | } | |
600 | ||
932e9ff9 VB |
601 | STATIC XPVAV* |
602 | S_new_xpvav(pTHX) | |
603 | { | |
604 | XPVAV* xpvav; | |
605 | LOCK_SV_MUTEX; | |
606 | if (!PL_xpvav_root) | |
607 | more_xpvav(); | |
608 | xpvav = PL_xpvav_root; | |
609 | PL_xpvav_root = (XPVAV*)xpvav->xav_array; | |
610 | UNLOCK_SV_MUTEX; | |
611 | return xpvav; | |
612 | } | |
613 | ||
614 | STATIC void | |
615 | S_del_xpvav(pTHX_ XPVAV *p) | |
616 | { | |
617 | LOCK_SV_MUTEX; | |
618 | p->xav_array = (char*)PL_xpvav_root; | |
619 | PL_xpvav_root = p; | |
620 | UNLOCK_SV_MUTEX; | |
621 | } | |
622 | ||
932e9ff9 VB |
623 | STATIC void |
624 | S_more_xpvav(pTHX) | |
625 | { | |
626 | register XPVAV* xpvav; | |
627 | register XPVAV* xpvavend; | |
612f20c3 GS |
628 | New(717, xpvav, 1008/sizeof(XPVAV), XPVAV); |
629 | xpvav->xav_array = (char*)PL_xpvav_arenaroot; | |
630 | PL_xpvav_arenaroot = xpvav; | |
631 | ||
932e9ff9 | 632 | xpvavend = &xpvav[1008 / sizeof(XPVAV) - 1]; |
612f20c3 | 633 | PL_xpvav_root = ++xpvav; |
932e9ff9 VB |
634 | while (xpvav < xpvavend) { |
635 | xpvav->xav_array = (char*)(xpvav + 1); | |
636 | xpvav++; | |
637 | } | |
638 | xpvav->xav_array = 0; | |
639 | } | |
640 | ||
932e9ff9 VB |
641 | STATIC XPVHV* |
642 | S_new_xpvhv(pTHX) | |
643 | { | |
644 | XPVHV* xpvhv; | |
645 | LOCK_SV_MUTEX; | |
646 | if (!PL_xpvhv_root) | |
647 | more_xpvhv(); | |
648 | xpvhv = PL_xpvhv_root; | |
649 | PL_xpvhv_root = (XPVHV*)xpvhv->xhv_array; | |
650 | UNLOCK_SV_MUTEX; | |
651 | return xpvhv; | |
652 | } | |
653 | ||
654 | STATIC void | |
655 | S_del_xpvhv(pTHX_ XPVHV *p) | |
656 | { | |
657 | LOCK_SV_MUTEX; | |
658 | p->xhv_array = (char*)PL_xpvhv_root; | |
659 | PL_xpvhv_root = p; | |
660 | UNLOCK_SV_MUTEX; | |
661 | } | |
662 | ||
932e9ff9 VB |
663 | STATIC void |
664 | S_more_xpvhv(pTHX) | |
665 | { | |
666 | register XPVHV* xpvhv; | |
667 | register XPVHV* xpvhvend; | |
612f20c3 GS |
668 | New(718, xpvhv, 1008/sizeof(XPVHV), XPVHV); |
669 | xpvhv->xhv_array = (char*)PL_xpvhv_arenaroot; | |
670 | PL_xpvhv_arenaroot = xpvhv; | |
671 | ||
932e9ff9 | 672 | xpvhvend = &xpvhv[1008 / sizeof(XPVHV) - 1]; |
612f20c3 | 673 | PL_xpvhv_root = ++xpvhv; |
932e9ff9 VB |
674 | while (xpvhv < xpvhvend) { |
675 | xpvhv->xhv_array = (char*)(xpvhv + 1); | |
676 | xpvhv++; | |
677 | } | |
678 | xpvhv->xhv_array = 0; | |
679 | } | |
680 | ||
932e9ff9 VB |
681 | STATIC XPVMG* |
682 | S_new_xpvmg(pTHX) | |
683 | { | |
684 | XPVMG* xpvmg; | |
685 | LOCK_SV_MUTEX; | |
686 | if (!PL_xpvmg_root) | |
687 | more_xpvmg(); | |
688 | xpvmg = PL_xpvmg_root; | |
689 | PL_xpvmg_root = (XPVMG*)xpvmg->xpv_pv; | |
690 | UNLOCK_SV_MUTEX; | |
691 | return xpvmg; | |
692 | } | |
693 | ||
694 | STATIC void | |
695 | S_del_xpvmg(pTHX_ XPVMG *p) | |
696 | { | |
697 | LOCK_SV_MUTEX; | |
698 | p->xpv_pv = (char*)PL_xpvmg_root; | |
699 | PL_xpvmg_root = p; | |
700 | UNLOCK_SV_MUTEX; | |
701 | } | |
702 | ||
932e9ff9 VB |
703 | STATIC void |
704 | S_more_xpvmg(pTHX) | |
705 | { | |
706 | register XPVMG* xpvmg; | |
707 | register XPVMG* xpvmgend; | |
612f20c3 GS |
708 | New(719, xpvmg, 1008/sizeof(XPVMG), XPVMG); |
709 | xpvmg->xpv_pv = (char*)PL_xpvmg_arenaroot; | |
710 | PL_xpvmg_arenaroot = xpvmg; | |
711 | ||
932e9ff9 | 712 | xpvmgend = &xpvmg[1008 / sizeof(XPVMG) - 1]; |
612f20c3 | 713 | PL_xpvmg_root = ++xpvmg; |
932e9ff9 VB |
714 | while (xpvmg < xpvmgend) { |
715 | xpvmg->xpv_pv = (char*)(xpvmg + 1); | |
716 | xpvmg++; | |
717 | } | |
718 | xpvmg->xpv_pv = 0; | |
719 | } | |
720 | ||
932e9ff9 VB |
721 | STATIC XPVLV* |
722 | S_new_xpvlv(pTHX) | |
723 | { | |
724 | XPVLV* xpvlv; | |
725 | LOCK_SV_MUTEX; | |
726 | if (!PL_xpvlv_root) | |
727 | more_xpvlv(); | |
728 | xpvlv = PL_xpvlv_root; | |
729 | PL_xpvlv_root = (XPVLV*)xpvlv->xpv_pv; | |
730 | UNLOCK_SV_MUTEX; | |
731 | return xpvlv; | |
732 | } | |
733 | ||
734 | STATIC void | |
735 | S_del_xpvlv(pTHX_ XPVLV *p) | |
736 | { | |
737 | LOCK_SV_MUTEX; | |
738 | p->xpv_pv = (char*)PL_xpvlv_root; | |
739 | PL_xpvlv_root = p; | |
740 | UNLOCK_SV_MUTEX; | |
741 | } | |
742 | ||
932e9ff9 VB |
743 | STATIC void |
744 | S_more_xpvlv(pTHX) | |
745 | { | |
746 | register XPVLV* xpvlv; | |
747 | register XPVLV* xpvlvend; | |
612f20c3 GS |
748 | New(720, xpvlv, 1008/sizeof(XPVLV), XPVLV); |
749 | xpvlv->xpv_pv = (char*)PL_xpvlv_arenaroot; | |
750 | PL_xpvlv_arenaroot = xpvlv; | |
751 | ||
932e9ff9 | 752 | xpvlvend = &xpvlv[1008 / sizeof(XPVLV) - 1]; |
612f20c3 | 753 | PL_xpvlv_root = ++xpvlv; |
932e9ff9 VB |
754 | while (xpvlv < xpvlvend) { |
755 | xpvlv->xpv_pv = (char*)(xpvlv + 1); | |
756 | xpvlv++; | |
757 | } | |
758 | xpvlv->xpv_pv = 0; | |
759 | } | |
760 | ||
932e9ff9 VB |
761 | STATIC XPVBM* |
762 | S_new_xpvbm(pTHX) | |
763 | { | |
764 | XPVBM* xpvbm; | |
765 | LOCK_SV_MUTEX; | |
766 | if (!PL_xpvbm_root) | |
767 | more_xpvbm(); | |
768 | xpvbm = PL_xpvbm_root; | |
769 | PL_xpvbm_root = (XPVBM*)xpvbm->xpv_pv; | |
770 | UNLOCK_SV_MUTEX; | |
771 | return xpvbm; | |
772 | } | |
773 | ||
774 | STATIC void | |
775 | S_del_xpvbm(pTHX_ XPVBM *p) | |
776 | { | |
777 | LOCK_SV_MUTEX; | |
778 | p->xpv_pv = (char*)PL_xpvbm_root; | |
779 | PL_xpvbm_root = p; | |
780 | UNLOCK_SV_MUTEX; | |
781 | } | |
782 | ||
932e9ff9 VB |
783 | STATIC void |
784 | S_more_xpvbm(pTHX) | |
785 | { | |
786 | register XPVBM* xpvbm; | |
787 | register XPVBM* xpvbmend; | |
612f20c3 GS |
788 | New(721, xpvbm, 1008/sizeof(XPVBM), XPVBM); |
789 | xpvbm->xpv_pv = (char*)PL_xpvbm_arenaroot; | |
790 | PL_xpvbm_arenaroot = xpvbm; | |
791 | ||
932e9ff9 | 792 | xpvbmend = &xpvbm[1008 / sizeof(XPVBM) - 1]; |
612f20c3 | 793 | PL_xpvbm_root = ++xpvbm; |
932e9ff9 VB |
794 | while (xpvbm < xpvbmend) { |
795 | xpvbm->xpv_pv = (char*)(xpvbm + 1); | |
796 | xpvbm++; | |
797 | } | |
798 | xpvbm->xpv_pv = 0; | |
799 | } | |
800 | ||
d33b2eba GS |
801 | #ifdef LEAKTEST |
802 | # define my_safemalloc(s) (void*)safexmalloc(717,s) | |
803 | # define my_safefree(p) safexfree((char*)p) | |
804 | #else | |
805 | # define my_safemalloc(s) (void*)safemalloc(s) | |
806 | # define my_safefree(p) safefree((char*)p) | |
807 | #endif | |
463ee0b2 | 808 | |
d33b2eba | 809 | #ifdef PURIFY |
463ee0b2 | 810 | |
d33b2eba GS |
811 | #define new_XIV() my_safemalloc(sizeof(XPVIV)) |
812 | #define del_XIV(p) my_safefree(p) | |
ed6116ce | 813 | |
d33b2eba GS |
814 | #define new_XNV() my_safemalloc(sizeof(XPVNV)) |
815 | #define del_XNV(p) my_safefree(p) | |
463ee0b2 | 816 | |
d33b2eba GS |
817 | #define new_XRV() my_safemalloc(sizeof(XRV)) |
818 | #define del_XRV(p) my_safefree(p) | |
8c52afec | 819 | |
d33b2eba GS |
820 | #define new_XPV() my_safemalloc(sizeof(XPV)) |
821 | #define del_XPV(p) my_safefree(p) | |
9b94d1dd | 822 | |
d33b2eba GS |
823 | #define new_XPVIV() my_safemalloc(sizeof(XPVIV)) |
824 | #define del_XPVIV(p) my_safefree(p) | |
932e9ff9 | 825 | |
d33b2eba GS |
826 | #define new_XPVNV() my_safemalloc(sizeof(XPVNV)) |
827 | #define del_XPVNV(p) my_safefree(p) | |
932e9ff9 | 828 | |
d33b2eba GS |
829 | #define new_XPVCV() my_safemalloc(sizeof(XPVCV)) |
830 | #define del_XPVCV(p) my_safefree(p) | |
932e9ff9 | 831 | |
d33b2eba GS |
832 | #define new_XPVAV() my_safemalloc(sizeof(XPVAV)) |
833 | #define del_XPVAV(p) my_safefree(p) | |
834 | ||
835 | #define new_XPVHV() my_safemalloc(sizeof(XPVHV)) | |
836 | #define del_XPVHV(p) my_safefree(p) | |
1c846c1f | 837 | |
d33b2eba GS |
838 | #define new_XPVMG() my_safemalloc(sizeof(XPVMG)) |
839 | #define del_XPVMG(p) my_safefree(p) | |
840 | ||
841 | #define new_XPVLV() my_safemalloc(sizeof(XPVLV)) | |
842 | #define del_XPVLV(p) my_safefree(p) | |
843 | ||
844 | #define new_XPVBM() my_safemalloc(sizeof(XPVBM)) | |
845 | #define del_XPVBM(p) my_safefree(p) | |
846 | ||
847 | #else /* !PURIFY */ | |
848 | ||
849 | #define new_XIV() (void*)new_xiv() | |
850 | #define del_XIV(p) del_xiv((XPVIV*) p) | |
851 | ||
852 | #define new_XNV() (void*)new_xnv() | |
853 | #define del_XNV(p) del_xnv((XPVNV*) p) | |
9b94d1dd | 854 | |
d33b2eba GS |
855 | #define new_XRV() (void*)new_xrv() |
856 | #define del_XRV(p) del_xrv((XRV*) p) | |
9b94d1dd | 857 | |
d33b2eba GS |
858 | #define new_XPV() (void*)new_xpv() |
859 | #define del_XPV(p) del_xpv((XPV *)p) | |
860 | ||
861 | #define new_XPVIV() (void*)new_xpviv() | |
862 | #define del_XPVIV(p) del_xpviv((XPVIV *)p) | |
863 | ||
864 | #define new_XPVNV() (void*)new_xpvnv() | |
865 | #define del_XPVNV(p) del_xpvnv((XPVNV *)p) | |
866 | ||
867 | #define new_XPVCV() (void*)new_xpvcv() | |
868 | #define del_XPVCV(p) del_xpvcv((XPVCV *)p) | |
869 | ||
870 | #define new_XPVAV() (void*)new_xpvav() | |
871 | #define del_XPVAV(p) del_xpvav((XPVAV *)p) | |
872 | ||
873 | #define new_XPVHV() (void*)new_xpvhv() | |
874 | #define del_XPVHV(p) del_xpvhv((XPVHV *)p) | |
1c846c1f | 875 | |
d33b2eba GS |
876 | #define new_XPVMG() (void*)new_xpvmg() |
877 | #define del_XPVMG(p) del_xpvmg((XPVMG *)p) | |
878 | ||
879 | #define new_XPVLV() (void*)new_xpvlv() | |
880 | #define del_XPVLV(p) del_xpvlv((XPVLV *)p) | |
881 | ||
882 | #define new_XPVBM() (void*)new_xpvbm() | |
883 | #define del_XPVBM(p) del_xpvbm((XPVBM *)p) | |
884 | ||
885 | #endif /* PURIFY */ | |
9b94d1dd | 886 | |
d33b2eba GS |
887 | #define new_XPVGV() my_safemalloc(sizeof(XPVGV)) |
888 | #define del_XPVGV(p) my_safefree(p) | |
1c846c1f | 889 | |
d33b2eba GS |
890 | #define new_XPVFM() my_safemalloc(sizeof(XPVFM)) |
891 | #define del_XPVFM(p) my_safefree(p) | |
1c846c1f | 892 | |
d33b2eba GS |
893 | #define new_XPVIO() my_safemalloc(sizeof(XPVIO)) |
894 | #define del_XPVIO(p) my_safefree(p) | |
8990e307 | 895 | |
954c1994 GS |
896 | /* |
897 | =for apidoc sv_upgrade | |
898 | ||
899 | Upgrade an SV to a more complex form. Use C<SvUPGRADE>. See | |
900 | C<svtype>. | |
901 | ||
902 | =cut | |
903 | */ | |
904 | ||
79072805 | 905 | bool |
864dbfa3 | 906 | Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt) |
79072805 LW |
907 | { |
908 | char* pv; | |
909 | U32 cur; | |
910 | U32 len; | |
a0d0e21e | 911 | IV iv; |
65202027 | 912 | NV nv; |
79072805 LW |
913 | MAGIC* magic; |
914 | HV* stash; | |
915 | ||
f130fd45 NIS |
916 | if (mt != SVt_PV && SvREADONLY(sv) && SvFAKE(sv)) { |
917 | sv_force_normal(sv); | |
918 | } | |
919 | ||
79072805 LW |
920 | if (SvTYPE(sv) == mt) |
921 | return TRUE; | |
922 | ||
a5f75d66 AD |
923 | if (mt < SVt_PVIV) |
924 | (void)SvOOK_off(sv); | |
925 | ||
79072805 LW |
926 | switch (SvTYPE(sv)) { |
927 | case SVt_NULL: | |
928 | pv = 0; | |
929 | cur = 0; | |
930 | len = 0; | |
931 | iv = 0; | |
932 | nv = 0.0; | |
933 | magic = 0; | |
934 | stash = 0; | |
935 | break; | |
79072805 LW |
936 | case SVt_IV: |
937 | pv = 0; | |
938 | cur = 0; | |
939 | len = 0; | |
463ee0b2 | 940 | iv = SvIVX(sv); |
65202027 | 941 | nv = (NV)SvIVX(sv); |
79072805 LW |
942 | del_XIV(SvANY(sv)); |
943 | magic = 0; | |
944 | stash = 0; | |
ed6116ce | 945 | if (mt == SVt_NV) |
463ee0b2 | 946 | mt = SVt_PVNV; |
ed6116ce LW |
947 | else if (mt < SVt_PVIV) |
948 | mt = SVt_PVIV; | |
79072805 LW |
949 | break; |
950 | case SVt_NV: | |
951 | pv = 0; | |
952 | cur = 0; | |
953 | len = 0; | |
463ee0b2 | 954 | nv = SvNVX(sv); |
1bd302c3 | 955 | iv = I_V(nv); |
79072805 LW |
956 | magic = 0; |
957 | stash = 0; | |
958 | del_XNV(SvANY(sv)); | |
959 | SvANY(sv) = 0; | |
ed6116ce | 960 | if (mt < SVt_PVNV) |
79072805 LW |
961 | mt = SVt_PVNV; |
962 | break; | |
ed6116ce LW |
963 | case SVt_RV: |
964 | pv = (char*)SvRV(sv); | |
965 | cur = 0; | |
966 | len = 0; | |
56431972 RB |
967 | iv = PTR2IV(pv); |
968 | nv = PTR2NV(pv); | |
ed6116ce LW |
969 | del_XRV(SvANY(sv)); |
970 | magic = 0; | |
971 | stash = 0; | |
972 | break; | |
79072805 | 973 | case SVt_PV: |
463ee0b2 | 974 | pv = SvPVX(sv); |
79072805 LW |
975 | cur = SvCUR(sv); |
976 | len = SvLEN(sv); | |
977 | iv = 0; | |
978 | nv = 0.0; | |
979 | magic = 0; | |
980 | stash = 0; | |
981 | del_XPV(SvANY(sv)); | |
748a9306 LW |
982 | if (mt <= SVt_IV) |
983 | mt = SVt_PVIV; | |
984 | else if (mt == SVt_NV) | |
985 | mt = SVt_PVNV; | |
79072805 LW |
986 | break; |
987 | case SVt_PVIV: | |
463ee0b2 | 988 | pv = SvPVX(sv); |
79072805 LW |
989 | cur = SvCUR(sv); |
990 | len = SvLEN(sv); | |
463ee0b2 | 991 | iv = SvIVX(sv); |
79072805 LW |
992 | nv = 0.0; |
993 | magic = 0; | |
994 | stash = 0; | |
995 | del_XPVIV(SvANY(sv)); | |
996 | break; | |
997 | case SVt_PVNV: | |
463ee0b2 | 998 | pv = SvPVX(sv); |
79072805 LW |
999 | cur = SvCUR(sv); |
1000 | len = SvLEN(sv); | |
463ee0b2 LW |
1001 | iv = SvIVX(sv); |
1002 | nv = SvNVX(sv); | |
79072805 LW |
1003 | magic = 0; |
1004 | stash = 0; | |
1005 | del_XPVNV(SvANY(sv)); | |
1006 | break; | |
1007 | case SVt_PVMG: | |
463ee0b2 | 1008 | pv = SvPVX(sv); |
79072805 LW |
1009 | cur = SvCUR(sv); |
1010 | len = SvLEN(sv); | |
463ee0b2 LW |
1011 | iv = SvIVX(sv); |
1012 | nv = SvNVX(sv); | |
79072805 LW |
1013 | magic = SvMAGIC(sv); |
1014 | stash = SvSTASH(sv); | |
1015 | del_XPVMG(SvANY(sv)); | |
1016 | break; | |
1017 | default: | |
cea2e8a9 | 1018 | Perl_croak(aTHX_ "Can't upgrade that kind of scalar"); |
79072805 LW |
1019 | } |
1020 | ||
1021 | switch (mt) { | |
1022 | case SVt_NULL: | |
cea2e8a9 | 1023 | Perl_croak(aTHX_ "Can't upgrade to undef"); |
79072805 LW |
1024 | case SVt_IV: |
1025 | SvANY(sv) = new_XIV(); | |
463ee0b2 | 1026 | SvIVX(sv) = iv; |
79072805 LW |
1027 | break; |
1028 | case SVt_NV: | |
1029 | SvANY(sv) = new_XNV(); | |
463ee0b2 | 1030 | SvNVX(sv) = nv; |
79072805 | 1031 | break; |
ed6116ce LW |
1032 | case SVt_RV: |
1033 | SvANY(sv) = new_XRV(); | |
1034 | SvRV(sv) = (SV*)pv; | |
ed6116ce | 1035 | break; |
79072805 LW |
1036 | case SVt_PV: |
1037 | SvANY(sv) = new_XPV(); | |
463ee0b2 | 1038 | SvPVX(sv) = pv; |
79072805 LW |
1039 | SvCUR(sv) = cur; |
1040 | SvLEN(sv) = len; | |
1041 | break; | |
1042 | case SVt_PVIV: | |
1043 | SvANY(sv) = new_XPVIV(); | |
463ee0b2 | 1044 | SvPVX(sv) = pv; |
79072805 LW |
1045 | SvCUR(sv) = cur; |
1046 | SvLEN(sv) = len; | |
463ee0b2 | 1047 | SvIVX(sv) = iv; |
79072805 | 1048 | if (SvNIOK(sv)) |
a0d0e21e | 1049 | (void)SvIOK_on(sv); |
79072805 LW |
1050 | SvNOK_off(sv); |
1051 | break; | |
1052 | case SVt_PVNV: | |
1053 | SvANY(sv) = new_XPVNV(); | |
463ee0b2 | 1054 | SvPVX(sv) = pv; |
79072805 LW |
1055 | SvCUR(sv) = cur; |
1056 | SvLEN(sv) = len; | |
463ee0b2 LW |
1057 | SvIVX(sv) = iv; |
1058 | SvNVX(sv) = nv; | |
79072805 LW |
1059 | break; |
1060 | case SVt_PVMG: | |
1061 | SvANY(sv) = new_XPVMG(); | |
463ee0b2 | 1062 | SvPVX(sv) = pv; |
79072805 LW |
1063 | SvCUR(sv) = cur; |
1064 | SvLEN(sv) = len; | |
463ee0b2 LW |
1065 | SvIVX(sv) = iv; |
1066 | SvNVX(sv) = nv; | |
79072805 LW |
1067 | SvMAGIC(sv) = magic; |
1068 | SvSTASH(sv) = stash; | |
1069 | break; | |
1070 | case SVt_PVLV: | |
1071 | SvANY(sv) = new_XPVLV(); | |
463ee0b2 | 1072 | SvPVX(sv) = pv; |
79072805 LW |
1073 | SvCUR(sv) = cur; |
1074 | SvLEN(sv) = len; | |
463ee0b2 LW |
1075 | SvIVX(sv) = iv; |
1076 | SvNVX(sv) = nv; | |
79072805 LW |
1077 | SvMAGIC(sv) = magic; |
1078 | SvSTASH(sv) = stash; | |
1079 | LvTARGOFF(sv) = 0; | |
1080 | LvTARGLEN(sv) = 0; | |
1081 | LvTARG(sv) = 0; | |
1082 | LvTYPE(sv) = 0; | |
1083 | break; | |
1084 | case SVt_PVAV: | |
1085 | SvANY(sv) = new_XPVAV(); | |
463ee0b2 LW |
1086 | if (pv) |
1087 | Safefree(pv); | |
2304df62 | 1088 | SvPVX(sv) = 0; |
d1bf51dd | 1089 | AvMAX(sv) = -1; |
93965878 | 1090 | AvFILLp(sv) = -1; |
463ee0b2 LW |
1091 | SvIVX(sv) = 0; |
1092 | SvNVX(sv) = 0.0; | |
1093 | SvMAGIC(sv) = magic; | |
1094 | SvSTASH(sv) = stash; | |
1095 | AvALLOC(sv) = 0; | |
79072805 LW |
1096 | AvARYLEN(sv) = 0; |
1097 | AvFLAGS(sv) = 0; | |
1098 | break; | |
1099 | case SVt_PVHV: | |
1100 | SvANY(sv) = new_XPVHV(); | |
463ee0b2 LW |
1101 | if (pv) |
1102 | Safefree(pv); | |
1103 | SvPVX(sv) = 0; | |
1104 | HvFILL(sv) = 0; | |
1105 | HvMAX(sv) = 0; | |
1106 | HvKEYS(sv) = 0; | |
1107 | SvNVX(sv) = 0.0; | |
79072805 LW |
1108 | SvMAGIC(sv) = magic; |
1109 | SvSTASH(sv) = stash; | |
79072805 LW |
1110 | HvRITER(sv) = 0; |
1111 | HvEITER(sv) = 0; | |
1112 | HvPMROOT(sv) = 0; | |
1113 | HvNAME(sv) = 0; | |
79072805 LW |
1114 | break; |
1115 | case SVt_PVCV: | |
1116 | SvANY(sv) = new_XPVCV(); | |
748a9306 | 1117 | Zero(SvANY(sv), 1, XPVCV); |
463ee0b2 | 1118 | SvPVX(sv) = pv; |
79072805 LW |
1119 | SvCUR(sv) = cur; |
1120 | SvLEN(sv) = len; | |
463ee0b2 LW |
1121 | SvIVX(sv) = iv; |
1122 | SvNVX(sv) = nv; | |
79072805 LW |
1123 | SvMAGIC(sv) = magic; |
1124 | SvSTASH(sv) = stash; | |
79072805 LW |
1125 | break; |
1126 | case SVt_PVGV: | |
1127 | SvANY(sv) = new_XPVGV(); | |
463ee0b2 | 1128 | SvPVX(sv) = pv; |
79072805 LW |
1129 | SvCUR(sv) = cur; |
1130 | SvLEN(sv) = len; | |
463ee0b2 LW |
1131 | SvIVX(sv) = iv; |
1132 | SvNVX(sv) = nv; | |
79072805 LW |
1133 | SvMAGIC(sv) = magic; |
1134 | SvSTASH(sv) = stash; | |
93a17b20 | 1135 | GvGP(sv) = 0; |
79072805 LW |
1136 | GvNAME(sv) = 0; |
1137 | GvNAMELEN(sv) = 0; | |
1138 | GvSTASH(sv) = 0; | |
a5f75d66 | 1139 | GvFLAGS(sv) = 0; |
79072805 LW |
1140 | break; |
1141 | case SVt_PVBM: | |
1142 | SvANY(sv) = new_XPVBM(); | |
463ee0b2 | 1143 | SvPVX(sv) = pv; |
79072805 LW |
1144 | SvCUR(sv) = cur; |
1145 | SvLEN(sv) = len; | |
463ee0b2 LW |
1146 | SvIVX(sv) = iv; |
1147 | SvNVX(sv) = nv; | |
79072805 LW |
1148 | SvMAGIC(sv) = magic; |
1149 | SvSTASH(sv) = stash; | |
1150 | BmRARE(sv) = 0; | |
1151 | BmUSEFUL(sv) = 0; | |
1152 | BmPREVIOUS(sv) = 0; | |
1153 | break; | |
1154 | case SVt_PVFM: | |
1155 | SvANY(sv) = new_XPVFM(); | |
748a9306 | 1156 | Zero(SvANY(sv), 1, XPVFM); |
463ee0b2 | 1157 | SvPVX(sv) = pv; |
79072805 LW |
1158 | SvCUR(sv) = cur; |
1159 | SvLEN(sv) = len; | |
463ee0b2 LW |
1160 | SvIVX(sv) = iv; |
1161 | SvNVX(sv) = nv; | |
79072805 LW |
1162 | SvMAGIC(sv) = magic; |
1163 | SvSTASH(sv) = stash; | |
79072805 | 1164 | break; |
8990e307 LW |
1165 | case SVt_PVIO: |
1166 | SvANY(sv) = new_XPVIO(); | |
748a9306 | 1167 | Zero(SvANY(sv), 1, XPVIO); |
8990e307 LW |
1168 | SvPVX(sv) = pv; |
1169 | SvCUR(sv) = cur; | |
1170 | SvLEN(sv) = len; | |
1171 | SvIVX(sv) = iv; | |
1172 | SvNVX(sv) = nv; | |
1173 | SvMAGIC(sv) = magic; | |
1174 | SvSTASH(sv) = stash; | |
85e6fe83 | 1175 | IoPAGE_LEN(sv) = 60; |
8990e307 LW |
1176 | break; |
1177 | } | |
1178 | SvFLAGS(sv) &= ~SVTYPEMASK; | |
1179 | SvFLAGS(sv) |= mt; | |
79072805 LW |
1180 | return TRUE; |
1181 | } | |
1182 | ||
79072805 | 1183 | int |
864dbfa3 | 1184 | Perl_sv_backoff(pTHX_ register SV *sv) |
79072805 LW |
1185 | { |
1186 | assert(SvOOK(sv)); | |
463ee0b2 LW |
1187 | if (SvIVX(sv)) { |
1188 | char *s = SvPVX(sv); | |
1189 | SvLEN(sv) += SvIVX(sv); | |
1190 | SvPVX(sv) -= SvIVX(sv); | |
79072805 | 1191 | SvIV_set(sv, 0); |
463ee0b2 | 1192 | Move(s, SvPVX(sv), SvCUR(sv)+1, char); |
79072805 LW |
1193 | } |
1194 | SvFLAGS(sv) &= ~SVf_OOK; | |
a0d0e21e | 1195 | return 0; |
79072805 LW |
1196 | } |
1197 | ||
954c1994 GS |
1198 | /* |
1199 | =for apidoc sv_grow | |
1200 | ||
1201 | Expands the character buffer in the SV. This will use C<sv_unref> and will | |
1202 | upgrade the SV to C<SVt_PV>. Returns a pointer to the character buffer. | |
1203 | Use C<SvGROW>. | |
1204 | ||
1205 | =cut | |
1206 | */ | |
1207 | ||
79072805 | 1208 | char * |
864dbfa3 | 1209 | Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen) |
79072805 LW |
1210 | { |
1211 | register char *s; | |
1212 | ||
55497cff | 1213 | #ifdef HAS_64K_LIMIT |
79072805 | 1214 | if (newlen >= 0x10000) { |
1d7c1841 GS |
1215 | PerlIO_printf(Perl_debug_log, |
1216 | "Allocation too large: %"UVxf"\n", (UV)newlen); | |
79072805 LW |
1217 | my_exit(1); |
1218 | } | |
55497cff | 1219 | #endif /* HAS_64K_LIMIT */ |
a0d0e21e LW |
1220 | if (SvROK(sv)) |
1221 | sv_unref(sv); | |
79072805 LW |
1222 | if (SvTYPE(sv) < SVt_PV) { |
1223 | sv_upgrade(sv, SVt_PV); | |
463ee0b2 | 1224 | s = SvPVX(sv); |
79072805 LW |
1225 | } |
1226 | else if (SvOOK(sv)) { /* pv is offset? */ | |
1227 | sv_backoff(sv); | |
463ee0b2 | 1228 | s = SvPVX(sv); |
79072805 LW |
1229 | if (newlen > SvLEN(sv)) |
1230 | newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */ | |
c6f8c383 GA |
1231 | #ifdef HAS_64K_LIMIT |
1232 | if (newlen >= 0x10000) | |
1233 | newlen = 0xFFFF; | |
1234 | #endif | |
79072805 LW |
1235 | } |
1236 | else | |
463ee0b2 | 1237 | s = SvPVX(sv); |
79072805 | 1238 | if (newlen > SvLEN(sv)) { /* need more room? */ |
8d6dde3e | 1239 | if (SvLEN(sv) && s) { |
f5a32c7f | 1240 | #if defined(MYMALLOC) && !defined(LEAKTEST) |
8d6dde3e IZ |
1241 | STRLEN l = malloced_size((void*)SvPVX(sv)); |
1242 | if (newlen <= l) { | |
1243 | SvLEN_set(sv, l); | |
1244 | return s; | |
1245 | } else | |
c70c8a0a | 1246 | #endif |
79072805 | 1247 | Renew(s,newlen,char); |
8d6dde3e | 1248 | } |
79072805 LW |
1249 | else |
1250 | New(703,s,newlen,char); | |
1251 | SvPV_set(sv, s); | |
1252 | SvLEN_set(sv, newlen); | |
1253 | } | |
1254 | return s; | |
1255 | } | |
1256 | ||
954c1994 GS |
1257 | /* |
1258 | =for apidoc sv_setiv | |
1259 | ||
1260 | Copies an integer into the given SV. Does not handle 'set' magic. See | |
1261 | C<sv_setiv_mg>. | |
1262 | ||
1263 | =cut | |
1264 | */ | |
1265 | ||
79072805 | 1266 | void |
864dbfa3 | 1267 | Perl_sv_setiv(pTHX_ register SV *sv, IV i) |
79072805 | 1268 | { |
2213622d | 1269 | SV_CHECK_THINKFIRST(sv); |
463ee0b2 LW |
1270 | switch (SvTYPE(sv)) { |
1271 | case SVt_NULL: | |
79072805 | 1272 | sv_upgrade(sv, SVt_IV); |
463ee0b2 LW |
1273 | break; |
1274 | case SVt_NV: | |
1275 | sv_upgrade(sv, SVt_PVNV); | |
1276 | break; | |
ed6116ce | 1277 | case SVt_RV: |
463ee0b2 | 1278 | case SVt_PV: |
79072805 | 1279 | sv_upgrade(sv, SVt_PVIV); |
463ee0b2 | 1280 | break; |
a0d0e21e LW |
1281 | |
1282 | case SVt_PVGV: | |
a0d0e21e LW |
1283 | case SVt_PVAV: |
1284 | case SVt_PVHV: | |
1285 | case SVt_PVCV: | |
1286 | case SVt_PVFM: | |
1287 | case SVt_PVIO: | |
411caa50 JH |
1288 | Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0), |
1289 | PL_op_desc[PL_op->op_type]); | |
463ee0b2 | 1290 | } |
a0d0e21e | 1291 | (void)SvIOK_only(sv); /* validate number */ |
a5f75d66 | 1292 | SvIVX(sv) = i; |
463ee0b2 | 1293 | SvTAINT(sv); |
79072805 LW |
1294 | } |
1295 | ||
954c1994 GS |
1296 | /* |
1297 | =for apidoc sv_setiv_mg | |
1298 | ||
1299 | Like C<sv_setiv>, but also handles 'set' magic. | |
1300 | ||
1301 | =cut | |
1302 | */ | |
1303 | ||
79072805 | 1304 | void |
864dbfa3 | 1305 | Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i) |
ef50df4b GS |
1306 | { |
1307 | sv_setiv(sv,i); | |
1308 | SvSETMAGIC(sv); | |
1309 | } | |
1310 | ||
954c1994 GS |
1311 | /* |
1312 | =for apidoc sv_setuv | |
1313 | ||
1314 | Copies an unsigned integer into the given SV. Does not handle 'set' magic. | |
1315 | See C<sv_setuv_mg>. | |
1316 | ||
1317 | =cut | |
1318 | */ | |
1319 | ||
ef50df4b | 1320 | void |
864dbfa3 | 1321 | Perl_sv_setuv(pTHX_ register SV *sv, UV u) |
55497cff | 1322 | { |
55ada374 NC |
1323 | /* With these two if statements: |
1324 | u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865 | |
d460ef45 | 1325 | |
55ada374 NC |
1326 | without |
1327 | u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865 | |
d460ef45 | 1328 | |
55ada374 NC |
1329 | If you wish to remove them, please benchmark to see what the effect is |
1330 | */ | |
28e5dec8 JH |
1331 | if (u <= (UV)IV_MAX) { |
1332 | sv_setiv(sv, (IV)u); | |
1333 | return; | |
1334 | } | |
25da4f38 IZ |
1335 | sv_setiv(sv, 0); |
1336 | SvIsUV_on(sv); | |
1337 | SvUVX(sv) = u; | |
55497cff | 1338 | } |
1339 | ||
954c1994 GS |
1340 | /* |
1341 | =for apidoc sv_setuv_mg | |
1342 | ||
1343 | Like C<sv_setuv>, but also handles 'set' magic. | |
1344 | ||
1345 | =cut | |
1346 | */ | |
1347 | ||
55497cff | 1348 | void |
864dbfa3 | 1349 | Perl_sv_setuv_mg(pTHX_ register SV *sv, UV u) |
ef50df4b | 1350 | { |
55ada374 NC |
1351 | /* With these two if statements: |
1352 | u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865 | |
d460ef45 | 1353 | |
55ada374 NC |
1354 | without |
1355 | u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865 | |
d460ef45 | 1356 | |
55ada374 NC |
1357 | If you wish to remove them, please benchmark to see what the effect is |
1358 | */ | |
28e5dec8 JH |
1359 | if (u <= (UV)IV_MAX) { |
1360 | sv_setiv(sv, (IV)u); | |
1361 | } else { | |
1362 | sv_setiv(sv, 0); | |
1363 | SvIsUV_on(sv); | |
1364 | sv_setuv(sv,u); | |
1365 | } | |
ef50df4b GS |
1366 | SvSETMAGIC(sv); |
1367 | } | |
1368 | ||
954c1994 GS |
1369 | /* |
1370 | =for apidoc sv_setnv | |
1371 | ||
1372 | Copies a double into the given SV. Does not handle 'set' magic. See | |
1373 | C<sv_setnv_mg>. | |
1374 | ||
1375 | =cut | |
1376 | */ | |
1377 | ||
ef50df4b | 1378 | void |
65202027 | 1379 | Perl_sv_setnv(pTHX_ register SV *sv, NV num) |
79072805 | 1380 | { |
2213622d | 1381 | SV_CHECK_THINKFIRST(sv); |
a0d0e21e LW |
1382 | switch (SvTYPE(sv)) { |
1383 | case SVt_NULL: | |
1384 | case SVt_IV: | |
79072805 | 1385 | sv_upgrade(sv, SVt_NV); |
a0d0e21e | 1386 | break; |
a0d0e21e LW |
1387 | case SVt_RV: |
1388 | case SVt_PV: | |
1389 | case SVt_PVIV: | |
79072805 | 1390 | sv_upgrade(sv, SVt_PVNV); |
a0d0e21e | 1391 | break; |
827b7e14 | 1392 | |
a0d0e21e | 1393 | case SVt_PVGV: |
a0d0e21e LW |
1394 | case SVt_PVAV: |
1395 | case SVt_PVHV: | |
1396 | case SVt_PVCV: | |
1397 | case SVt_PVFM: | |
1398 | case SVt_PVIO: | |
411caa50 JH |
1399 | Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0), |
1400 | PL_op_name[PL_op->op_type]); | |
79072805 | 1401 | } |
463ee0b2 | 1402 | SvNVX(sv) = num; |
a0d0e21e | 1403 | (void)SvNOK_only(sv); /* validate number */ |
463ee0b2 | 1404 | SvTAINT(sv); |
79072805 LW |
1405 | } |
1406 | ||
954c1994 GS |
1407 | /* |
1408 | =for apidoc sv_setnv_mg | |
1409 | ||
1410 | Like C<sv_setnv>, but also handles 'set' magic. | |
1411 | ||
1412 | =cut | |
1413 | */ | |
1414 | ||
ef50df4b | 1415 | void |
65202027 | 1416 | Perl_sv_setnv_mg(pTHX_ register SV *sv, NV num) |
ef50df4b GS |
1417 | { |
1418 | sv_setnv(sv,num); | |
1419 | SvSETMAGIC(sv); | |
1420 | } | |
1421 | ||
76e3520e | 1422 | STATIC void |
cea2e8a9 | 1423 | S_not_a_number(pTHX_ SV *sv) |
a0d0e21e LW |
1424 | { |
1425 | char tmpbuf[64]; | |
1426 | char *d = tmpbuf; | |
1427 | char *s; | |
dc28f22b GA |
1428 | char *limit = tmpbuf + sizeof(tmpbuf) - 8; |
1429 | /* each *s can expand to 4 chars + "...\0", | |
1430 | i.e. need room for 8 chars */ | |
a0d0e21e | 1431 | |
dc28f22b | 1432 | for (s = SvPVX(sv); *s && d < limit; s++) { |
bbce6d69 | 1433 | int ch = *s & 0xFF; |
1434 | if (ch & 128 && !isPRINT_LC(ch)) { | |
a0d0e21e LW |
1435 | *d++ = 'M'; |
1436 | *d++ = '-'; | |
1437 | ch &= 127; | |
1438 | } | |
bbce6d69 | 1439 | if (ch == '\n') { |
1440 | *d++ = '\\'; | |
1441 | *d++ = 'n'; | |
1442 | } | |
1443 | else if (ch == '\r') { | |
1444 | *d++ = '\\'; | |
1445 | *d++ = 'r'; | |
1446 | } | |
1447 | else if (ch == '\f') { | |
1448 | *d++ = '\\'; | |
1449 | *d++ = 'f'; | |
1450 | } | |
1451 | else if (ch == '\\') { | |
1452 | *d++ = '\\'; | |
1453 | *d++ = '\\'; | |
1454 | } | |
1455 | else if (isPRINT_LC(ch)) | |
a0d0e21e LW |
1456 | *d++ = ch; |
1457 | else { | |
1458 | *d++ = '^'; | |
bbce6d69 | 1459 | *d++ = toCTRL(ch); |
a0d0e21e LW |
1460 | } |
1461 | } | |
1462 | if (*s) { | |
1463 | *d++ = '.'; | |
1464 | *d++ = '.'; | |
1465 | *d++ = '.'; | |
1466 | } | |
1467 | *d = '\0'; | |
1468 | ||
533c011a | 1469 | if (PL_op) |
42d38218 MS |
1470 | Perl_warner(aTHX_ WARN_NUMERIC, |
1471 | "Argument \"%s\" isn't numeric in %s", tmpbuf, | |
1472 | PL_op_desc[PL_op->op_type]); | |
a0d0e21e | 1473 | else |
42d38218 MS |
1474 | Perl_warner(aTHX_ WARN_NUMERIC, |
1475 | "Argument \"%s\" isn't numeric", tmpbuf); | |
a0d0e21e LW |
1476 | } |
1477 | ||
28e5dec8 JH |
1478 | /* the number can be converted to integer with atol() or atoll() although */ |
1479 | #define IS_NUMBER_TO_INT_BY_ATOL 0x01 /* integer (may have decimals) */ | |
1480 | #define IS_NUMBER_TO_INT_BY_STRTOL 0x02 /* it may exceed IV_MAX */ | |
1481 | #define IS_NUMBER_TO_INT_BY_ATOF 0x04 /* seen something like 123e4 */ | |
1482 | #define IS_NUMBER_LONGER_THAN_IV_MAX 0x08 /* more digits than IV_MAX */ | |
1483 | #define IS_NUMBER_AS_LONG_AS_IV_MAX 0x10 /* may(be not) larger than IV_MAX */ | |
1484 | #define IS_NUMBER_NOT_INT 0x20 /* seen a decimal point or e */ | |
1485 | #define IS_NUMBER_NEG 0x40 /* seen a leading - */ | |
1486 | #define IS_NUMBER_INFINITY 0x80 /* /^\s*-?Infinity\s*$/i */ | |
25da4f38 IZ |
1487 | |
1488 | /* Actually, ISO C leaves conversion of UV to IV undefined, but | |
1489 | until proven guilty, assume that things are not that bad... */ | |
1490 | ||
28e5dec8 JH |
1491 | /* As 64 bit platforms often have an NV that doesn't preserve all bits of |
1492 | an IV (an assumption perl has been based on to date) it becomes necessary | |
1493 | to remove the assumption that the NV always carries enough precision to | |
1494 | recreate the IV whenever needed, and that the NV is the canonical form. | |
1495 | Instead, IV/UV and NV need to be given equal rights. So as to not lose | |
1496 | precision as an side effect of conversion (which would lead to insanity | |
1497 | and the dragon(s) in t/op/numconvert.t getting very angry) the intent is | |
1498 | 1) to distinguish between IV/UV/NV slots that have cached a valid | |
1499 | conversion where precision was lost and IV/UV/NV slots that have a | |
1500 | valid conversion which has lost no precision | |
1501 | 2) to ensure that if a numeric conversion to one form is request that | |
1502 | would lose precision, the precise conversion (or differently | |
1503 | imprecise conversion) is also performed and cached, to prevent | |
1504 | requests for different numeric formats on the same SV causing | |
1505 | lossy conversion chains. (lossless conversion chains are perfectly | |
1506 | acceptable (still)) | |
1507 | ||
1508 | ||
1509 | flags are used: | |
1510 | SvIOKp is true if the IV slot contains a valid value | |
1511 | SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true) | |
1512 | SvNOKp is true if the NV slot contains a valid value | |
1513 | SvNOK is true only if the NV value is accurate | |
1514 | ||
1515 | so | |
1516 | while converting from PV to NV check to see if converting that NV to an | |
1517 | IV(or UV) would lose accuracy over a direct conversion from PV to | |
1518 | IV(or UV). If it would, cache both conversions, return NV, but mark | |
1519 | SV as IOK NOKp (ie not NOK). | |
1520 | ||
1521 | while converting from PV to IV check to see if converting that IV to an | |
1522 | NV would lose accuracy over a direct conversion from PV to NV. If it | |
1523 | would, cache both conversions, flag similarly. | |
1524 | ||
1525 | Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite | |
1526 | correctly because if IV & NV were set NV *always* overruled. | |
1527 | Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flags meaning | |
1528 | changes - now IV and NV together means that the two are interchangeable | |
1529 | SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX; | |
d460ef45 | 1530 | |
28e5dec8 JH |
1531 | The benefit of this is operations such as pp_add know that if SvIOK is |
1532 | true for both left and right operands, then integer addition can be | |
1533 | used instead of floating point. (for cases where the result won't | |
1534 | overflow) Before, floating point was always used, which could lead to | |
1535 | loss of precision compared with integer addition. | |
1536 | ||
1537 | * making IV and NV equal status should make maths accurate on 64 bit | |
1538 | platforms | |
1539 | * may speed up maths somewhat if pp_add and friends start to use | |
1540 | integers when possible instead of fp. (hopefully the overhead in | |
1541 | looking for SvIOK and checking for overflow will not outweigh the | |
1542 | fp to integer speedup) | |
1543 | * will slow down integer operations (callers of SvIV) on "inaccurate" | |
1544 | values, as the change from SvIOK to SvIOKp will cause a call into | |
1545 | sv_2iv each time rather than a macro access direct to the IV slot | |
1546 | * should speed up number->string conversion on integers as IV is | |
1547 | favoured when IV and NV equally accurate | |
1548 | ||
1549 | #################################################################### | |
1550 | You had better be using SvIOK_notUV if you want an IV for arithmetic | |
1551 | SvIOK is true if (IV or UV), so you might be getting (IV)SvUV | |
1552 | SvUOK is true iff UV. | |
1553 | #################################################################### | |
1554 | ||
1555 | Your mileage will vary depending your CPUs relative fp to integer | |
1556 | performance ratio. | |
1557 | */ | |
1558 | ||
1559 | #ifndef NV_PRESERVES_UV | |
1560 | #define IS_NUMBER_UNDERFLOW_IV 1 | |
1561 | #define IS_NUMBER_UNDERFLOW_UV 2 | |
1562 | #define IS_NUMBER_IV_AND_UV 2 | |
1563 | #define IS_NUMBER_OVERFLOW_IV 4 | |
1564 | #define IS_NUMBER_OVERFLOW_UV 5 | |
1565 | /* Hopefully your optimiser will consider inlining these two functions. */ | |
1566 | STATIC int | |
1567 | S_sv_2inuv_non_preserve (pTHX_ register SV *sv, I32 numtype) { | |
1568 | NV nv = SvNVX(sv); /* Code simpler and had compiler problems if */ | |
1569 | UV nv_as_uv = U_V(nv); /* these are not in simple variables. */ | |
159fae86 | 1570 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_2inuv_non '%s', IV=0x%"UVxf" NV=%g inttype=%"UVXf"\n", SvPVX(sv), SvIVX(sv), nv, (UV)numtype)); |
28e5dec8 JH |
1571 | if (nv_as_uv <= (UV)IV_MAX) { |
1572 | (void)SvIOKp_on(sv); | |
1573 | (void)SvNOKp_on(sv); | |
1574 | /* Within suitable range to fit in an IV, atol won't overflow */ | |
1575 | /* XXX quite sure? Is that your final answer? not really, I'm | |
1576 | trusting that nv_as_uv to round down if NV is (IV_MAX + 1) */ | |
1577 | SvIVX(sv) = (IV)Atol(SvPVX(sv)); | |
1578 | if (numtype & IS_NUMBER_NOT_INT) { | |
1579 | /* I believe that even if the original PV had decimals, they | |
1580 | are lost beyond the limit of the FP precision. | |
1581 | However, neither is canonical, so both only get p flags. | |
1582 | NWC, 2000/11/25 */ | |
1583 | /* Both already have p flags, so do nothing */ | |
1584 | } else if (SvIVX(sv) == I_V(nv)) { | |
1585 | SvNOK_on(sv); | |
1586 | SvIOK_on(sv); | |
1587 | } else { | |
1588 | SvIOK_on(sv); | |
1589 | /* It had no "." so it must be integer. assert (get in here from | |
1590 | sv_2iv and sv_2uv only for ndef HAS_STRTOL and | |
1591 | IS_NUMBER_AS_LONG_AS_IV_MAX) or my logic is faulty and all | |
1592 | conversion routines need audit. */ | |
1593 | } | |
1594 | return nv < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV; | |
1595 | } | |
1596 | /* between IV_MAX and NV(UV_MAX). Could be slightly> UV_MAX */ | |
1597 | (void)SvIOKp_on(sv); | |
1598 | (void)SvNOKp_on(sv); | |
1599 | #ifdef HAS_STRTOUL | |
1600 | { | |
1601 | int save_errno = errno; | |
1602 | errno = 0; | |
1603 | SvUVX(sv) = Strtoul(SvPVX(sv), Null(char**), 10); | |
1604 | if (errno == 0) { | |
1605 | if (numtype & IS_NUMBER_NOT_INT) { | |
1606 | /* UV and NV both imprecise. */ | |
1607 | SvIsUV_on(sv); | |
1608 | } else if (SvUVX(sv) == nv_as_uv && SvUVX(sv) != UV_MAX) { | |
1609 | SvNOK_on(sv); | |
1610 | SvIOK_on(sv); | |
1611 | SvIsUV_on(sv); | |
1612 | } else { | |
1613 | SvIOK_on(sv); | |
1614 | SvIsUV_on(sv); | |
1615 | } | |
1616 | errno = save_errno; | |
1617 | return IS_NUMBER_OVERFLOW_IV; | |
1618 | } | |
1619 | errno = save_errno; | |
1620 | SvNOK_on(sv); | |
1621 | /* Must have just overflowed UV, but not enough that an NV could spot | |
1622 | this.. */ | |
1623 | return IS_NUMBER_OVERFLOW_UV; | |
1624 | } | |
1625 | #else | |
1626 | /* We've just lost integer precision, nothing we could do. */ | |
1627 | SvUVX(sv) = nv_as_uv; | |
159fae86 | 1628 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_2niuv_non UV? '%s', UV=0x%"UVxf" NV=%g U_V(NV)=0x%"UVxf" inttype=%"UVXf"\n", SvPVX(sv), SvIVX(sv), nv, nv_as_uv, (UV)numtype)); |
28e5dec8 JH |
1629 | /* UV and NV slots equally valid only if we have casting symmetry. */ |
1630 | if (numtype & IS_NUMBER_NOT_INT) { | |
1631 | SvIsUV_on(sv); | |
1632 | } else if (SvUVX(sv) == nv_as_uv && SvUVX(sv) != UV_MAX) { | |
1633 | /* UV_MAX can cast up to NV (UV_MAX+1), that NV casts down to UV_MAX | |
1634 | UV_MAX ought to be 0xFF...FFF which won't preserve (We only | |
1635 | get to this point if NVs don't preserve UVs) */ | |
1636 | SvNOK_on(sv); | |
1637 | SvIOK_on(sv); | |
1638 | SvIsUV_on(sv); | |
1639 | } else { | |
1640 | /* As above, I believe UV at least as good as NV */ | |
1641 | SvIsUV_on(sv); | |
1642 | } | |
1643 | #endif /* HAS_STRTOUL */ | |
1644 | return IS_NUMBER_OVERFLOW_IV; | |
1645 | } | |
1646 | ||
1647 | /* For sv_2nv these three cases are "SvNOK and don't bother casting" */ | |
1648 | STATIC int | |
1649 | S_sv_2iuv_non_preserve (pTHX_ register SV *sv, I32 numtype) | |
1650 | { | |
159fae86 | 1651 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_2iuv_non '%s', IV=0x%"UVxf" NV=%g inttype=%"UVXf"\n", SvPVX(sv), SvIVX(sv), SvNVX(sv), (UV)numtype)); |
28e5dec8 JH |
1652 | if (SvNVX(sv) < (NV)IV_MIN) { |
1653 | (void)SvIOKp_on(sv); | |
1654 | (void)SvNOK_on(sv); | |
1655 | SvIVX(sv) = IV_MIN; | |
1656 | return IS_NUMBER_UNDERFLOW_IV; | |
1657 | } | |
1658 | if (SvNVX(sv) > (NV)UV_MAX) { | |
1659 | (void)SvIOKp_on(sv); | |
1660 | (void)SvNOK_on(sv); | |
1661 | SvIsUV_on(sv); | |
1662 | SvUVX(sv) = UV_MAX; | |
1663 | return IS_NUMBER_OVERFLOW_UV; | |
1664 | } | |
1665 | if (!(numtype & (IS_NUMBER_TO_INT_BY_ATOL | IS_NUMBER_TO_INT_BY_STRTOL))) { | |
1666 | (void)SvIOKp_on(sv); | |
1667 | (void)SvNOK_on(sv); | |
1668 | /* Can't use strtol etc to convert this string */ | |
1669 | if (SvNVX(sv) <= (UV)IV_MAX) { | |
1670 | SvIVX(sv) = I_V(SvNVX(sv)); | |
1671 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) { | |
1672 | SvIOK_on(sv); /* Integer is precise. NOK, IOK */ | |
1673 | } else { | |
1674 | /* Integer is imprecise. NOK, IOKp */ | |
1675 | } | |
1676 | return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV; | |
1677 | } | |
1678 | SvIsUV_on(sv); | |
1679 | SvUVX(sv) = U_V(SvNVX(sv)); | |
1680 | if ((NV)(SvUVX(sv)) == SvNVX(sv)) { | |
09bb3e27 NC |
1681 | if (SvUVX(sv) == UV_MAX) { |
1682 | /* As we know that NVs don't preserve UVs, UV_MAX cannot | |
1683 | possibly be preserved by NV. Hence, it must be overflow. | |
1684 | NOK, IOKp */ | |
1685 | return IS_NUMBER_OVERFLOW_UV; | |
1686 | } | |
28e5dec8 JH |
1687 | SvIOK_on(sv); /* Integer is precise. NOK, UOK */ |
1688 | } else { | |
1689 | /* Integer is imprecise. NOK, IOKp */ | |
1690 | } | |
1691 | return IS_NUMBER_OVERFLOW_IV; | |
1692 | } | |
e57fe1aa | 1693 | return S_sv_2inuv_non_preserve(aTHX_ sv, numtype); |
28e5dec8 JH |
1694 | } |
1695 | #endif /* NV_PRESERVES_UV*/ | |
1696 | ||
a0d0e21e | 1697 | IV |
864dbfa3 | 1698 | Perl_sv_2iv(pTHX_ register SV *sv) |
79072805 LW |
1699 | { |
1700 | if (!sv) | |
1701 | return 0; | |
8990e307 | 1702 | if (SvGMAGICAL(sv)) { |
463ee0b2 LW |
1703 | mg_get(sv); |
1704 | if (SvIOKp(sv)) | |
1705 | return SvIVX(sv); | |
748a9306 | 1706 | if (SvNOKp(sv)) { |
25da4f38 | 1707 | return I_V(SvNVX(sv)); |
748a9306 | 1708 | } |
36477c24 | 1709 | if (SvPOKp(sv) && SvLEN(sv)) |
1710 | return asIV(sv); | |
3fe9a6f1 | 1711 | if (!SvROK(sv)) { |
d008e5eb | 1712 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 1713 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
1d7c1841 | 1714 | report_uninit(); |
c6ee37c5 | 1715 | } |
36477c24 | 1716 | return 0; |
3fe9a6f1 | 1717 | } |
463ee0b2 | 1718 | } |
ed6116ce | 1719 | if (SvTHINKFIRST(sv)) { |
a0d0e21e | 1720 | if (SvROK(sv)) { |
a0d0e21e | 1721 | SV* tmpstr; |
1554e226 DC |
1722 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) && |
1723 | (SvRV(tmpstr) != SvRV(sv))) | |
9e7bc3e8 | 1724 | return SvIV(tmpstr); |
56431972 | 1725 | return PTR2IV(SvRV(sv)); |
a0d0e21e | 1726 | } |
47deb5e7 NIS |
1727 | if (SvREADONLY(sv) && SvFAKE(sv)) { |
1728 | sv_force_normal(sv); | |
1729 | } | |
0336b60e | 1730 | if (SvREADONLY(sv) && !SvOK(sv)) { |
0336b60e | 1731 | if (ckWARN(WARN_UNINITIALIZED)) |
1d7c1841 | 1732 | report_uninit(); |
ed6116ce LW |
1733 | return 0; |
1734 | } | |
79072805 | 1735 | } |
25da4f38 IZ |
1736 | if (SvIOKp(sv)) { |
1737 | if (SvIsUV(sv)) { | |
1738 | return (IV)(SvUVX(sv)); | |
1739 | } | |
1740 | else { | |
1741 | return SvIVX(sv); | |
1742 | } | |
463ee0b2 | 1743 | } |
748a9306 | 1744 | if (SvNOKp(sv)) { |
28e5dec8 JH |
1745 | /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv |
1746 | * without also getting a cached IV/UV from it at the same time | |
1747 | * (ie PV->NV conversion should detect loss of accuracy and cache | |
1748 | * IV or UV at same time to avoid this. NWC */ | |
25da4f38 IZ |
1749 | |
1750 | if (SvTYPE(sv) == SVt_NV) | |
1751 | sv_upgrade(sv, SVt_PVNV); | |
1752 | ||
28e5dec8 JH |
1753 | (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */ |
1754 | /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost | |
1755 | certainly cast into the IV range at IV_MAX, whereas the correct | |
1756 | answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary | |
1757 | cases go to UV */ | |
1758 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
748a9306 | 1759 | SvIVX(sv) = I_V(SvNVX(sv)); |
28e5dec8 JH |
1760 | if (SvNVX(sv) == (NV) SvIVX(sv) |
1761 | #ifndef NV_PRESERVES_UV | |
1762 | && (((UV)1 << NV_PRESERVES_UV_BITS) > | |
1763 | (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv))) | |
1764 | /* Don't flag it as "accurately an integer" if the number | |
1765 | came from a (by definition imprecise) NV operation, and | |
1766 | we're outside the range of NV integer precision */ | |
1767 | #endif | |
1768 | ) { | |
1769 | SvIOK_on(sv); /* Can this go wrong with rounding? NWC */ | |
1770 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
1771 | "0x%"UVxf" iv(%g => %"IVdf") (precise)\n", | |
1772 | PTR2UV(sv), | |
1773 | SvNVX(sv), | |
1774 | SvIVX(sv))); | |
1775 | ||
1776 | } else { | |
1777 | /* IV not precise. No need to convert from PV, as NV | |
1778 | conversion would already have cached IV if it detected | |
1779 | that PV->IV would be better than PV->NV->IV | |
1780 | flags already correct - don't set public IOK. */ | |
1781 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
1782 | "0x%"UVxf" iv(%g => %"IVdf") (imprecise)\n", | |
1783 | PTR2UV(sv), | |
1784 | SvNVX(sv), | |
1785 | SvIVX(sv))); | |
1786 | } | |
1787 | /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN, | |
1788 | but the cast (NV)IV_MIN rounds to a the value less (more | |
1789 | negative) than IV_MIN which happens to be equal to SvNVX ?? | |
1790 | Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and | |
1791 | NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and | |
1792 | (NV)UVX == NVX are both true, but the values differ. :-( | |
1793 | Hopefully for 2s complement IV_MIN is something like | |
1794 | 0x8000000000000000 which will be exact. NWC */ | |
d460ef45 | 1795 | } |
25da4f38 | 1796 | else { |
ff68c719 | 1797 | SvUVX(sv) = U_V(SvNVX(sv)); |
28e5dec8 JH |
1798 | if ( |
1799 | (SvNVX(sv) == (NV) SvUVX(sv)) | |
1800 | #ifndef NV_PRESERVES_UV | |
1801 | /* Make sure it's not 0xFFFFFFFFFFFFFFFF */ | |
1802 | /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */ | |
1803 | && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv)) | |
1804 | /* Don't flag it as "accurately an integer" if the number | |
1805 | came from a (by definition imprecise) NV operation, and | |
1806 | we're outside the range of NV integer precision */ | |
1807 | #endif | |
1808 | ) | |
1809 | SvIOK_on(sv); | |
25da4f38 IZ |
1810 | SvIsUV_on(sv); |
1811 | ret_iv_max: | |
1c846c1f | 1812 | DEBUG_c(PerlIO_printf(Perl_debug_log, |
57def98f | 1813 | "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n", |
56431972 | 1814 | PTR2UV(sv), |
57def98f JH |
1815 | SvUVX(sv), |
1816 | SvUVX(sv))); | |
25da4f38 IZ |
1817 | return (IV)SvUVX(sv); |
1818 | } | |
748a9306 LW |
1819 | } |
1820 | else if (SvPOKp(sv) && SvLEN(sv)) { | |
25da4f38 IZ |
1821 | I32 numtype = looks_like_number(sv); |
1822 | ||
1823 | /* We want to avoid a possible problem when we cache an IV which | |
1824 | may be later translated to an NV, and the resulting NV is not | |
1825 | the translation of the initial data. | |
1c846c1f | 1826 | |
25da4f38 IZ |
1827 | This means that if we cache such an IV, we need to cache the |
1828 | NV as well. Moreover, we trade speed for space, and do not | |
28e5dec8 | 1829 | cache the NV if we are sure it's not needed. |
25da4f38 | 1830 | */ |
16b7a9a4 | 1831 | |
28e5dec8 JH |
1832 | if ((numtype & ~IS_NUMBER_NEG) == IS_NUMBER_TO_INT_BY_ATOL) { |
1833 | /* The NV may be reconstructed from IV - safe to cache IV, | |
1834 | which may be calculated by atol(). */ | |
1835 | if (SvTYPE(sv) < SVt_PVIV) | |
1836 | sv_upgrade(sv, SVt_PVIV); | |
f7bbb42a | 1837 | (void)SvIOK_on(sv); |
28e5dec8 JH |
1838 | SvIVX(sv) = Atol(SvPVX(sv)); |
1839 | } else { | |
1840 | #ifdef HAS_STRTOL | |
1841 | IV i; | |
1842 | int save_errno = errno; | |
1843 | /* Is it an integer that we could convert with strtol? | |
1844 | So try it, and if it doesn't set errno then it's pukka. | |
1845 | This should be faster than going atof and then thinking. */ | |
1846 | if (((numtype & (IS_NUMBER_TO_INT_BY_STRTOL | IS_NUMBER_NOT_INT)) | |
1847 | == IS_NUMBER_TO_INT_BY_STRTOL) | |
1848 | /* && is a sequence point. Without it not sure if I'm trying | |
1849 | to do too much between sequence points and hence going | |
1850 | undefined */ | |
1851 | && ((errno = 0), 1) /* , 1 so always true */ | |
1852 | && ((i = Strtol(SvPVX(sv), Null(char**), 10)), 1) | |
1853 | && (errno == 0)) { | |
1854 | if (SvTYPE(sv) < SVt_PVIV) | |
1855 | sv_upgrade(sv, SVt_PVIV); | |
1856 | (void)SvIOK_on(sv); | |
1857 | SvIVX(sv) = i; | |
1858 | errno = save_errno; | |
1859 | } else | |
1860 | #endif | |
1861 | { | |
1862 | NV d; | |
1863 | #ifdef HAS_STRTOL | |
1864 | /* Hopefully trace flow will optimise this away where possible | |
1865 | */ | |
1866 | errno = save_errno; | |
1867 | #endif | |
1868 | /* It wasn't an integer, or it overflowed, or we don't have | |
1869 | strtol. Do things the slow way - check if it's a UV etc. */ | |
1870 | d = Atof(SvPVX(sv)); | |
1871 | ||
1872 | if (SvTYPE(sv) < SVt_PVNV) | |
1873 | sv_upgrade(sv, SVt_PVNV); | |
1874 | SvNVX(sv) = d; | |
1875 | ||
1876 | if (! numtype && ckWARN(WARN_NUMERIC)) | |
1877 | not_a_number(sv); | |
1878 | ||
65202027 | 1879 | #if defined(USE_LONG_DOUBLE) |
28e5dec8 JH |
1880 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n", |
1881 | PTR2UV(sv), SvNVX(sv))); | |
65202027 | 1882 | #else |
28e5dec8 JH |
1883 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%g)\n", |
1884 | PTR2UV(sv), SvNVX(sv))); | |
65202027 | 1885 | #endif |
28e5dec8 JH |
1886 | |
1887 | ||
1888 | #ifdef NV_PRESERVES_UV | |
1889 | (void)SvIOKp_on(sv); | |
1890 | (void)SvNOK_on(sv); | |
1891 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
1892 | SvIVX(sv) = I_V(SvNVX(sv)); | |
1893 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) { | |
1894 | SvIOK_on(sv); | |
1895 | } else { | |
1896 | /* Integer is imprecise. NOK, IOKp */ | |
1897 | } | |
1898 | /* UV will not work better than IV */ | |
1899 | } else { | |
1900 | if (SvNVX(sv) > (NV)UV_MAX) { | |
1901 | SvIsUV_on(sv); | |
1902 | /* Integer is inaccurate. NOK, IOKp, is UV */ | |
1903 | SvUVX(sv) = UV_MAX; | |
1904 | SvIsUV_on(sv); | |
1905 | } else { | |
1906 | SvUVX(sv) = U_V(SvNVX(sv)); | |
1907 | /* 0xFFFFFFFFFFFFFFFF not an issue in here */ | |
1908 | if ((NV)(SvUVX(sv)) == SvNVX(sv)) { | |
1909 | SvIOK_on(sv); | |
1910 | SvIsUV_on(sv); | |
1911 | } else { | |
1912 | /* Integer is imprecise. NOK, IOKp, is UV */ | |
1913 | SvIsUV_on(sv); | |
1914 | } | |
1915 | } | |
1916 | goto ret_iv_max; | |
1917 | } | |
1918 | #else /* NV_PRESERVES_UV */ | |
1919 | if (((UV)1 << NV_PRESERVES_UV_BITS) > | |
1920 | U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) { | |
1921 | /* Small enough to preserve all bits. */ | |
1922 | (void)SvIOKp_on(sv); | |
1923 | SvNOK_on(sv); | |
1924 | SvIVX(sv) = I_V(SvNVX(sv)); | |
1925 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) | |
1926 | SvIOK_on(sv); | |
1927 | /* Assumption: first non-preserved integer is < IV_MAX, | |
1928 | this NV is in the preserved range, therefore: */ | |
1929 | if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv)) | |
1930 | < (UV)IV_MAX)) { | |
1931 | Perl_croak(aTHX_ "sv_2iv assumed (U_V(fabs(SvNVX(sv))) < (UV)IV_MAX) but SvNVX(sv)=%g U_V is 0x%"UVxf", IV_MAX is 0x%"UVxf"\n", SvNVX(sv), U_V(SvNVX(sv)), (UV)IV_MAX); | |
1932 | } | |
1933 | } else if (sv_2iuv_non_preserve (sv, numtype) | |
1934 | >= IS_NUMBER_OVERFLOW_IV) | |
1935 | goto ret_iv_max; | |
1936 | #endif /* NV_PRESERVES_UV */ | |
25da4f38 IZ |
1937 | } |
1938 | } | |
28e5dec8 | 1939 | } else { |
599cee73 | 1940 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP)) |
1d7c1841 | 1941 | report_uninit(); |
25da4f38 IZ |
1942 | if (SvTYPE(sv) < SVt_IV) |
1943 | /* Typically the caller expects that sv_any is not NULL now. */ | |
1944 | sv_upgrade(sv, SVt_IV); | |
a0d0e21e | 1945 | return 0; |
79072805 | 1946 | } |
1d7c1841 GS |
1947 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n", |
1948 | PTR2UV(sv),SvIVX(sv))); | |
25da4f38 | 1949 | return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv); |
79072805 LW |
1950 | } |
1951 | ||
ff68c719 | 1952 | UV |
864dbfa3 | 1953 | Perl_sv_2uv(pTHX_ register SV *sv) |
ff68c719 | 1954 | { |
1955 | if (!sv) | |
1956 | return 0; | |
1957 | if (SvGMAGICAL(sv)) { | |
1958 | mg_get(sv); | |
1959 | if (SvIOKp(sv)) | |
1960 | return SvUVX(sv); | |
1961 | if (SvNOKp(sv)) | |
1962 | return U_V(SvNVX(sv)); | |
36477c24 | 1963 | if (SvPOKp(sv) && SvLEN(sv)) |
1964 | return asUV(sv); | |
3fe9a6f1 | 1965 | if (!SvROK(sv)) { |
d008e5eb | 1966 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 1967 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
1d7c1841 | 1968 | report_uninit(); |
c6ee37c5 | 1969 | } |
36477c24 | 1970 | return 0; |
3fe9a6f1 | 1971 | } |
ff68c719 | 1972 | } |
1973 | if (SvTHINKFIRST(sv)) { | |
1974 | if (SvROK(sv)) { | |
ff68c719 | 1975 | SV* tmpstr; |
1554e226 DC |
1976 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) && |
1977 | (SvRV(tmpstr) != SvRV(sv))) | |
9e7bc3e8 | 1978 | return SvUV(tmpstr); |
56431972 | 1979 | return PTR2UV(SvRV(sv)); |
ff68c719 | 1980 | } |
8a818333 NIS |
1981 | if (SvREADONLY(sv) && SvFAKE(sv)) { |
1982 | sv_force_normal(sv); | |
1983 | } | |
0336b60e | 1984 | if (SvREADONLY(sv) && !SvOK(sv)) { |
0336b60e | 1985 | if (ckWARN(WARN_UNINITIALIZED)) |
1d7c1841 | 1986 | report_uninit(); |
ff68c719 | 1987 | return 0; |
1988 | } | |
1989 | } | |
25da4f38 IZ |
1990 | if (SvIOKp(sv)) { |
1991 | if (SvIsUV(sv)) { | |
1992 | return SvUVX(sv); | |
1993 | } | |
1994 | else { | |
1995 | return (UV)SvIVX(sv); | |
1996 | } | |
ff68c719 | 1997 | } |
1998 | if (SvNOKp(sv)) { | |
28e5dec8 JH |
1999 | /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv |
2000 | * without also getting a cached IV/UV from it at the same time | |
2001 | * (ie PV->NV conversion should detect loss of accuracy and cache | |
2002 | * IV or UV at same time to avoid this. */ | |
2003 | /* IV-over-UV optimisation - choose to cache IV if possible */ | |
2004 | ||
25da4f38 IZ |
2005 | if (SvTYPE(sv) == SVt_NV) |
2006 | sv_upgrade(sv, SVt_PVNV); | |
28e5dec8 JH |
2007 | |
2008 | (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */ | |
2009 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
f7bbb42a | 2010 | SvIVX(sv) = I_V(SvNVX(sv)); |
28e5dec8 JH |
2011 | if (SvNVX(sv) == (NV) SvIVX(sv) |
2012 | #ifndef NV_PRESERVES_UV | |
2013 | && (((UV)1 << NV_PRESERVES_UV_BITS) > | |
2014 | (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv))) | |
2015 | /* Don't flag it as "accurately an integer" if the number | |
2016 | came from a (by definition imprecise) NV operation, and | |
2017 | we're outside the range of NV integer precision */ | |
2018 | #endif | |
2019 | ) { | |
2020 | SvIOK_on(sv); /* Can this go wrong with rounding? NWC */ | |
2021 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
2022 | "0x%"UVxf" uv(%g => %"IVdf") (precise)\n", | |
2023 | PTR2UV(sv), | |
2024 | SvNVX(sv), | |
2025 | SvIVX(sv))); | |
2026 | ||
2027 | } else { | |
2028 | /* IV not precise. No need to convert from PV, as NV | |
2029 | conversion would already have cached IV if it detected | |
2030 | that PV->IV would be better than PV->NV->IV | |
2031 | flags already correct - don't set public IOK. */ | |
2032 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
2033 | "0x%"UVxf" uv(%g => %"IVdf") (imprecise)\n", | |
2034 | PTR2UV(sv), | |
2035 | SvNVX(sv), | |
2036 | SvIVX(sv))); | |
2037 | } | |
2038 | /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN, | |
2039 | but the cast (NV)IV_MIN rounds to a the value less (more | |
2040 | negative) than IV_MIN which happens to be equal to SvNVX ?? | |
2041 | Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and | |
2042 | NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and | |
2043 | (NV)UVX == NVX are both true, but the values differ. :-( | |
2044 | Hopefully for 2s complement IV_MIN is something like | |
2045 | 0x8000000000000000 which will be exact. NWC */ | |
d460ef45 | 2046 | } |
28e5dec8 JH |
2047 | else { |
2048 | SvUVX(sv) = U_V(SvNVX(sv)); | |
2049 | if ( | |
2050 | (SvNVX(sv) == (NV) SvUVX(sv)) | |
2051 | #ifndef NV_PRESERVES_UV | |
2052 | /* Make sure it's not 0xFFFFFFFFFFFFFFFF */ | |
2053 | /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */ | |
2054 | && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv)) | |
2055 | /* Don't flag it as "accurately an integer" if the number | |
2056 | came from a (by definition imprecise) NV operation, and | |
2057 | we're outside the range of NV integer precision */ | |
2058 | #endif | |
2059 | ) | |
2060 | SvIOK_on(sv); | |
2061 | SvIsUV_on(sv); | |
1c846c1f | 2062 | DEBUG_c(PerlIO_printf(Perl_debug_log, |
28e5dec8 | 2063 | "0x%"UVxf" 2uv(%"UVuf" => %"IVdf") (as unsigned)\n", |
57def98f | 2064 | PTR2UV(sv), |
28e5dec8 JH |
2065 | SvUVX(sv), |
2066 | SvUVX(sv))); | |
25da4f38 | 2067 | } |
ff68c719 | 2068 | } |
2069 | else if (SvPOKp(sv) && SvLEN(sv)) { | |
25da4f38 IZ |
2070 | I32 numtype = looks_like_number(sv); |
2071 | ||
2072 | /* We want to avoid a possible problem when we cache a UV which | |
2073 | may be later translated to an NV, and the resulting NV is not | |
2074 | the translation of the initial data. | |
1c846c1f | 2075 | |
25da4f38 IZ |
2076 | This means that if we cache such a UV, we need to cache the |
2077 | NV as well. Moreover, we trade speed for space, and do not | |
2078 | cache the NV if not needed. | |
2079 | */ | |
16b7a9a4 | 2080 | |
28e5dec8 | 2081 | if ((numtype & ~IS_NUMBER_NEG) == IS_NUMBER_TO_INT_BY_ATOL) { |
f7bbb42a | 2082 | /* The NV may be reconstructed from IV - safe to cache IV, |
28e5dec8 JH |
2083 | which may be calculated by atol(). */ |
2084 | if (SvTYPE(sv) < SVt_PVIV) | |
f7bbb42a JH |
2085 | sv_upgrade(sv, SVt_PVIV); |
2086 | (void)SvIOK_on(sv); | |
28e5dec8 JH |
2087 | SvIVX(sv) = Atol(SvPVX(sv)); |
2088 | } else { | |
f7bbb42a | 2089 | #ifdef HAS_STRTOUL |
28e5dec8 | 2090 | UV u; |
f9172815 | 2091 | char *num_begin = SvPVX(sv); |
28e5dec8 | 2092 | int save_errno = errno; |
d460ef45 | 2093 | |
f9172815 JH |
2094 | /* seems that strtoul taking numbers that start with - is |
2095 | implementation dependant, and can't be relied upon. */ | |
2096 | if (numtype & IS_NUMBER_NEG) { | |
2097 | /* Not totally defensive. assumine that looks_like_num | |
2098 | didn't lie about a - sign */ | |
2099 | while (isSPACE(*num_begin)) | |
2100 | num_begin++; | |
2101 | if (*num_begin == '-') | |
2102 | num_begin++; | |
2103 | } | |
d460ef45 | 2104 | |
28e5dec8 JH |
2105 | /* Is it an integer that we could convert with strtoul? |
2106 | So try it, and if it doesn't set errno then it's pukka. | |
2107 | This should be faster than going atof and then thinking. */ | |
2108 | if (((numtype & (IS_NUMBER_TO_INT_BY_STRTOL | IS_NUMBER_NOT_INT)) | |
2109 | == IS_NUMBER_TO_INT_BY_STRTOL) | |
2110 | && ((errno = 0), 1) /* always true */ | |
f9172815 | 2111 | && ((u = Strtoul(num_begin, Null(char**), 10)), 1) /* ditto */ |
28e5dec8 | 2112 | && (errno == 0) |
d460ef45 | 2113 | /* If known to be negative, check it didn't undeflow IV |
f9172815 JH |
2114 | XXX possibly we should put more negative values as NVs |
2115 | direct rather than go via atof below */ | |
2116 | && ((numtype & IS_NUMBER_NEG) ? (u <= (UV)IV_MIN) : 1)) { | |
28e5dec8 JH |
2117 | errno = save_errno; |
2118 | ||
2119 | if (SvTYPE(sv) < SVt_PVIV) | |
2120 | sv_upgrade(sv, SVt_PVIV); | |
2121 | (void)SvIOK_on(sv); | |
2122 | ||
2123 | /* If it's negative must use IV. | |
2124 | IV-over-UV optimisation */ | |
f9172815 JH |
2125 | if (numtype & IS_NUMBER_NEG) { |
2126 | SvIVX(sv) = -(IV)u; | |
2127 | } else if (u <= (UV) IV_MAX) { | |
28e5dec8 JH |
2128 | SvIVX(sv) = (IV)u; |
2129 | } else { | |
2130 | /* it didn't overflow, and it was positive. */ | |
2131 | SvUVX(sv) = u; | |
2132 | SvIsUV_on(sv); | |
2133 | } | |
2134 | } else | |
f7bbb42a | 2135 | #endif |
28e5dec8 JH |
2136 | { |
2137 | NV d; | |
2138 | #ifdef HAS_STRTOUL | |
2139 | /* Hopefully trace flow will optimise this away where possible | |
2140 | */ | |
2141 | errno = save_errno; | |
2142 | #endif | |
2143 | /* It wasn't an integer, or it overflowed, or we don't have | |
2144 | strtol. Do things the slow way - check if it's a IV etc. */ | |
2145 | d = Atof(SvPVX(sv)); | |
2146 | ||
2147 | if (SvTYPE(sv) < SVt_PVNV) | |
2148 | sv_upgrade(sv, SVt_PVNV); | |
2149 | SvNVX(sv) = d; | |
2150 | ||
2151 | if (! numtype && ckWARN(WARN_NUMERIC)) | |
2152 | not_a_number(sv); | |
2153 | ||
2154 | #if defined(USE_LONG_DOUBLE) | |
2155 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%" PERL_PRIgldbl ")\n", | |
2156 | PTR2UV(sv), SvNVX(sv))); | |
2157 | #else | |
2158 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%g)\n", | |
2159 | PTR2UV(sv), SvNVX(sv))); | |
2160 | #endif | |
2161 | ||
2162 | #ifdef NV_PRESERVES_UV | |
2163 | (void)SvIOKp_on(sv); | |
2164 | (void)SvNOK_on(sv); | |
2165 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
2166 | SvIVX(sv) = I_V(SvNVX(sv)); | |
2167 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) { | |
2168 | SvIOK_on(sv); | |
2169 | } else { | |
2170 | /* Integer is imprecise. NOK, IOKp */ | |
2171 | } | |
2172 | /* UV will not work better than IV */ | |
2173 | } else { | |
2174 | if (SvNVX(sv) > (NV)UV_MAX) { | |
2175 | SvIsUV_on(sv); | |
2176 | /* Integer is inaccurate. NOK, IOKp, is UV */ | |
2177 | SvUVX(sv) = UV_MAX; | |
2178 | SvIsUV_on(sv); | |
2179 | } else { | |
2180 | SvUVX(sv) = U_V(SvNVX(sv)); | |
2181 | /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs | |
2182 | NV preservse UV so can do correct comparison. */ | |
2183 | if ((NV)(SvUVX(sv)) == SvNVX(sv)) { | |
2184 | SvIOK_on(sv); | |
2185 | SvIsUV_on(sv); | |
2186 | } else { | |
2187 | /* Integer is imprecise. NOK, IOKp, is UV */ | |
2188 | SvIsUV_on(sv); | |
2189 | } | |
2190 | } | |
2191 | } | |
2192 | #else /* NV_PRESERVES_UV */ | |
2193 | if (((UV)1 << NV_PRESERVES_UV_BITS) > | |
2194 | U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) { | |
2195 | /* Small enough to preserve all bits. */ | |
2196 | (void)SvIOKp_on(sv); | |
2197 | SvNOK_on(sv); | |
2198 | SvIVX(sv) = I_V(SvNVX(sv)); | |
2199 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) | |
2200 | SvIOK_on(sv); | |
2201 | /* Assumption: first non-preserved integer is < IV_MAX, | |
2202 | this NV is in the preserved range, therefore: */ | |
2203 | if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv)) | |
2204 | < (UV)IV_MAX)) { | |
2205 | Perl_croak(aTHX_ "sv_2uv assumed (U_V(fabs(SvNVX(sv))) < (UV)IV_MAX) but SvNVX(sv)=%g U_V is 0x%"UVxf", IV_MAX is 0x%"UVxf"\n", SvNVX(sv), U_V(SvNVX(sv)), (UV)IV_MAX); | |
2206 | } | |
2207 | } else | |
2208 | sv_2iuv_non_preserve (sv, numtype); | |
2209 | #endif /* NV_PRESERVES_UV */ | |
2210 | } | |
f7bbb42a | 2211 | } |
ff68c719 | 2212 | } |
2213 | else { | |
d008e5eb | 2214 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 2215 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
1d7c1841 | 2216 | report_uninit(); |
c6ee37c5 | 2217 | } |
25da4f38 IZ |
2218 | if (SvTYPE(sv) < SVt_IV) |
2219 | /* Typically the caller expects that sv_any is not NULL now. */ | |
2220 | sv_upgrade(sv, SVt_IV); | |
ff68c719 | 2221 | return 0; |
2222 | } | |
25da4f38 | 2223 | |
1d7c1841 GS |
2224 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n", |
2225 | PTR2UV(sv),SvUVX(sv))); | |
25da4f38 | 2226 | return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv); |
ff68c719 | 2227 | } |
2228 | ||
65202027 | 2229 | NV |
864dbfa3 | 2230 | Perl_sv_2nv(pTHX_ register SV *sv) |
79072805 LW |
2231 | { |
2232 | if (!sv) | |
2233 | return 0.0; | |
8990e307 | 2234 | if (SvGMAGICAL(sv)) { |
463ee0b2 LW |
2235 | mg_get(sv); |
2236 | if (SvNOKp(sv)) | |
2237 | return SvNVX(sv); | |
a0d0e21e | 2238 | if (SvPOKp(sv) && SvLEN(sv)) { |
599cee73 | 2239 | if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !looks_like_number(sv)) |
a0d0e21e | 2240 | not_a_number(sv); |
097ee67d | 2241 | return Atof(SvPVX(sv)); |
a0d0e21e | 2242 | } |
25da4f38 | 2243 | if (SvIOKp(sv)) { |
1c846c1f | 2244 | if (SvIsUV(sv)) |
65202027 | 2245 | return (NV)SvUVX(sv); |
25da4f38 | 2246 | else |
65202027 | 2247 | return (NV)SvIVX(sv); |
25da4f38 | 2248 | } |
16d20bd9 | 2249 | if (!SvROK(sv)) { |
d008e5eb | 2250 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 2251 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
1d7c1841 | 2252 | report_uninit(); |
c6ee37c5 | 2253 | } |
16d20bd9 AD |
2254 | return 0; |
2255 | } | |
463ee0b2 | 2256 | } |
ed6116ce | 2257 | if (SvTHINKFIRST(sv)) { |
a0d0e21e | 2258 | if (SvROK(sv)) { |
a0d0e21e | 2259 | SV* tmpstr; |
1554e226 DC |
2260 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) && |
2261 | (SvRV(tmpstr) != SvRV(sv))) | |
9e7bc3e8 | 2262 | return SvNV(tmpstr); |
56431972 | 2263 | return PTR2NV(SvRV(sv)); |
a0d0e21e | 2264 | } |
8a818333 NIS |
2265 | if (SvREADONLY(sv) && SvFAKE(sv)) { |
2266 | sv_force_normal(sv); | |
2267 | } | |
0336b60e | 2268 | if (SvREADONLY(sv) && !SvOK(sv)) { |
599cee73 | 2269 | if (ckWARN(WARN_UNINITIALIZED)) |
1d7c1841 | 2270 | report_uninit(); |
ed6116ce LW |
2271 | return 0.0; |
2272 | } | |
79072805 LW |
2273 | } |
2274 | if (SvTYPE(sv) < SVt_NV) { | |
463ee0b2 LW |
2275 | if (SvTYPE(sv) == SVt_IV) |
2276 | sv_upgrade(sv, SVt_PVNV); | |
2277 | else | |
2278 | sv_upgrade(sv, SVt_NV); | |
572bbb43 | 2279 | #if defined(USE_LONG_DOUBLE) |
097ee67d | 2280 | DEBUG_c({ |
f93f4e46 | 2281 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
2282 | PerlIO_printf(Perl_debug_log, |
2283 | "0x%"UVxf" num(%" PERL_PRIgldbl ")\n", | |
2284 | PTR2UV(sv), SvNVX(sv)); | |
572bbb43 GS |
2285 | RESTORE_NUMERIC_LOCAL(); |
2286 | }); | |
65202027 | 2287 | #else |
572bbb43 | 2288 | DEBUG_c({ |
f93f4e46 | 2289 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
2290 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%g)\n", |
2291 | PTR2UV(sv), SvNVX(sv)); | |
097ee67d JH |
2292 | RESTORE_NUMERIC_LOCAL(); |
2293 | }); | |
572bbb43 | 2294 | #endif |
79072805 LW |
2295 | } |
2296 | else if (SvTYPE(sv) < SVt_PVNV) | |
2297 | sv_upgrade(sv, SVt_PVNV); | |
748a9306 LW |
2298 | if (SvIOKp(sv) && |
2299 | (!SvPOKp(sv) || !strchr(SvPVX(sv),'.') || !looks_like_number(sv))) | |
93a17b20 | 2300 | { |
65202027 | 2301 | SvNVX(sv) = SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv); |
28e5dec8 JH |
2302 | #ifdef NV_PRESERVES_UV |
2303 | SvNOK_on(sv); | |
2304 | #else | |
2305 | /* Only set the public NV OK flag if this NV preserves the IV */ | |
2306 | /* Check it's not 0xFFFFFFFFFFFFFFFF */ | |
2307 | if (SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv)))) | |
2308 | : (SvIVX(sv) == I_V(SvNVX(sv)))) | |
2309 | SvNOK_on(sv); | |
2310 | else | |
2311 | SvNOKp_on(sv); | |
2312 | #endif | |
93a17b20 | 2313 | } |
748a9306 | 2314 | else if (SvPOKp(sv) && SvLEN(sv)) { |
599cee73 | 2315 | if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !looks_like_number(sv)) |
a0d0e21e | 2316 | not_a_number(sv); |
097ee67d | 2317 | SvNVX(sv) = Atof(SvPVX(sv)); |
28e5dec8 JH |
2318 | #ifdef NV_PRESERVES_UV |
2319 | SvNOK_on(sv); | |
2320 | #else | |
2321 | /* Only set the public NV OK flag if this NV preserves the value in | |
2322 | the PV at least as well as an IV/UV would. | |
2323 | Not sure how to do this 100% reliably. */ | |
2324 | /* if that shift count is out of range then Configure's test is | |
2325 | wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS == | |
2326 | UV_BITS */ | |
2327 | if (((UV)1 << NV_PRESERVES_UV_BITS) > | |
2328 | U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) | |
2329 | SvNOK_on(sv); /* Definitely small enough to preserve all bits */ | |
2330 | else if (SvNVX(sv) < (NV)IV_MIN || SvNVX(sv) > (NV)UV_MAX) { | |
2331 | /* Definitely too large/small to fit in an integer, so no loss | |
2332 | of precision going to integer in the future via NV */ | |
2333 | SvNOK_on(sv); | |
2334 | } else { | |
2335 | /* Is it something we can run through strtol etc (ie no | |
2336 | trailing exponent part)? */ | |
2337 | int numtype = looks_like_number(sv); | |
2338 | /* XXX probably should cache this if called above */ | |
2339 | ||
2340 | if (!(numtype & | |
2341 | (IS_NUMBER_TO_INT_BY_ATOL | IS_NUMBER_TO_INT_BY_STRTOL))) { | |
2342 | /* Can't use strtol etc to convert this string, so don't try */ | |
2343 | SvNOK_on(sv); | |
2344 | } else | |
2345 | sv_2inuv_non_preserve (sv, numtype); | |
2346 | } | |
2347 | #endif /* NV_PRESERVES_UV */ | |
93a17b20 | 2348 | } |
79072805 | 2349 | else { |
599cee73 | 2350 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP)) |
1d7c1841 | 2351 | report_uninit(); |
25da4f38 IZ |
2352 | if (SvTYPE(sv) < SVt_NV) |
2353 | /* Typically the caller expects that sv_any is not NULL now. */ | |
28e5dec8 JH |
2354 | /* XXX Ilya implies that this is a bug in callers that assume this |
2355 | and ideally should be fixed. */ | |
25da4f38 | 2356 | sv_upgrade(sv, SVt_NV); |
a0d0e21e | 2357 | return 0.0; |
79072805 | 2358 | } |
572bbb43 | 2359 | #if defined(USE_LONG_DOUBLE) |
097ee67d | 2360 | DEBUG_c({ |
f93f4e46 | 2361 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
2362 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n", |
2363 | PTR2UV(sv), SvNVX(sv)); | |
572bbb43 GS |
2364 | RESTORE_NUMERIC_LOCAL(); |
2365 | }); | |
65202027 | 2366 | #else |
572bbb43 | 2367 | DEBUG_c({ |
f93f4e46 | 2368 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
2369 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%g)\n", |
2370 | PTR2UV(sv), SvNVX(sv)); | |
097ee67d JH |
2371 | RESTORE_NUMERIC_LOCAL(); |
2372 | }); | |
572bbb43 | 2373 | #endif |
463ee0b2 | 2374 | return SvNVX(sv); |
79072805 LW |
2375 | } |
2376 | ||
76e3520e | 2377 | STATIC IV |
cea2e8a9 | 2378 | S_asIV(pTHX_ SV *sv) |
36477c24 | 2379 | { |
2380 | I32 numtype = looks_like_number(sv); | |
65202027 | 2381 | NV d; |
36477c24 | 2382 | |
25da4f38 | 2383 | if (numtype & IS_NUMBER_TO_INT_BY_ATOL) |
cf2093f6 | 2384 | return Atol(SvPVX(sv)); |
d008e5eb | 2385 | if (!numtype) { |
d008e5eb GS |
2386 | if (ckWARN(WARN_NUMERIC)) |
2387 | not_a_number(sv); | |
2388 | } | |
097ee67d | 2389 | d = Atof(SvPVX(sv)); |
25da4f38 | 2390 | return I_V(d); |
36477c24 | 2391 | } |
2392 | ||
76e3520e | 2393 | STATIC UV |
cea2e8a9 | 2394 | S_asUV(pTHX_ SV *sv) |
36477c24 | 2395 | { |
2396 | I32 numtype = looks_like_number(sv); | |
2397 | ||
84902520 | 2398 | #ifdef HAS_STRTOUL |
25da4f38 | 2399 | if (numtype & IS_NUMBER_TO_INT_BY_ATOL) |
cf2093f6 | 2400 | return Strtoul(SvPVX(sv), Null(char**), 10); |
84902520 | 2401 | #endif |
d008e5eb | 2402 | if (!numtype) { |
d008e5eb GS |
2403 | if (ckWARN(WARN_NUMERIC)) |
2404 | not_a_number(sv); | |
2405 | } | |
097ee67d | 2406 | return U_V(Atof(SvPVX(sv))); |
36477c24 | 2407 | } |
2408 | ||
25da4f38 IZ |
2409 | /* |
2410 | * Returns a combination of (advisory only - can get false negatives) | |
28e5dec8 JH |
2411 | * IS_NUMBER_TO_INT_BY_ATOL, IS_NUMBER_TO_INT_BY_ATOF |
2412 | * IS_NUMBER_LONGER_THAN_IV_MAX, IS_NUMBER_AS_LONG_AS_IV_MAX | |
2413 | * IS_NUMBER_NOT_INT, IS_NUMBER_NEG, IS_NUMBER_INFINITY | |
25da4f38 IZ |
2414 | * 0 if does not look like number. |
2415 | * | |
28e5dec8 JH |
2416 | * (atol and strtol stop when they hit a decimal point. strtol will return |
2417 | * LONG_MAX and LONG_MIN when given out of range values. ANSI says they should | |
2418 | * do this, and vendors have had 11 years to get it right. | |
2419 | * However, will try to make it still work with only atol | |
d460ef45 | 2420 | * |
28e5dec8 JH |
2421 | * IS_NUMBER_TO_INT_BY_ATOL 123456789 or 123456789.3 definitely < IV_MAX |
2422 | * IS_NUMBER_TO_INT_BY_STRTOL 123456789 or 123456789.3 if digits = IV_MAX | |
2423 | * IS_NUMBER_TO_INT_BY_ATOF 123456789e0 or >> IV_MAX | |
2424 | * IS_NUMBER_LONGER_THAN_IV_MAX lots of digits, don't bother with atol | |
2425 | * IS_NUMBER_AS_LONG_AS_IV_MAX atol might hit LONG_MAX, might not. | |
2426 | * IS_NUMBER_NOT_INT saw "." or "e" | |
2427 | * IS_NUMBER_NEG | |
300aed98 | 2428 | * IS_NUMBER_INFINITY |
25da4f38 IZ |
2429 | */ |
2430 | ||
954c1994 GS |
2431 | /* |
2432 | =for apidoc looks_like_number | |
2433 | ||
2434 | Test if an the content of an SV looks like a number (or is a | |
28e5dec8 JH |
2435 | number). C<Inf> and C<Infinity> are treated as numbers (so will not |
2436 | issue a non-numeric warning), even if your atof() doesn't grok them. | |
954c1994 GS |
2437 | |
2438 | =cut | |
2439 | */ | |
2440 | ||
36477c24 | 2441 | I32 |
864dbfa3 | 2442 | Perl_looks_like_number(pTHX_ SV *sv) |
36477c24 | 2443 | { |
2444 | register char *s; | |
2445 | register char *send; | |
2446 | register char *sbegin; | |
25da4f38 IZ |
2447 | register char *nbegin; |
2448 | I32 numtype = 0; | |
300aed98 | 2449 | I32 sawinf = 0; |
36477c24 | 2450 | STRLEN len; |
9c7192ba | 2451 | #ifdef USE_LOCALE_NUMERIC |
eff180cd | 2452 | bool specialradix = FALSE; |
9c7192ba | 2453 | #endif |
36477c24 | 2454 | |
2455 | if (SvPOK(sv)) { | |
1c846c1f | 2456 | sbegin = SvPVX(sv); |
36477c24 | 2457 | len = SvCUR(sv); |
2458 | } | |
2459 | else if (SvPOKp(sv)) | |
2460 | sbegin = SvPV(sv, len); | |
2461 | else | |
2462 | return 1; | |
2463 | send = sbegin + len; | |
2464 | ||
2465 | s = sbegin; | |
2466 | while (isSPACE(*s)) | |
2467 | s++; | |
25da4f38 IZ |
2468 | if (*s == '-') { |
2469 | s++; | |
2470 | numtype = IS_NUMBER_NEG; | |
2471 | } | |
2472 | else if (*s == '+') | |
36477c24 | 2473 | s++; |
ff0cee69 | 2474 | |
25da4f38 IZ |
2475 | nbegin = s; |
2476 | /* | |
d460ef45 | 2477 | * we return IS_NUMBER_TO_INT_BY_ATOL if the number can converted to |
28e5dec8 JH |
2478 | * integer with atol() without overflow, IS_NUMBER_TO_INT_BY_STRTOL if |
2479 | * possibly slightly larger than max int, IS_NUMBER_TO_INT_BY_ATOF if you | |
2480 | * will need (int)atof(). | |
25da4f38 IZ |
2481 | */ |
2482 | ||
300aed98 | 2483 | /* next must be digit or the radix separator or beginning of infinity */ |
ff0cee69 | 2484 | if (isDIGIT(*s)) { |
2485 | do { | |
2486 | s++; | |
2487 | } while (isDIGIT(*s)); | |
25da4f38 | 2488 | |
28e5dec8 JH |
2489 | /* Aaargh. long long really is irritating. |
2490 | In the gospel according to ANSI 1989, it is an axiom that "long" | |
2491 | is the longest integer type, and that if you don't know how long | |
2492 | something is you can cast it to long, and nothing will be lost | |
2493 | (except possibly speed of execution if long is slower than the | |
2494 | type is was). | |
2495 | Now, one can't be sure if the old rules apply, or long long | |
2496 | (or some other newfangled thing) is actually longer than the | |
2497 | (formerly) longest thing. | |
2498 | */ | |
2499 | /* This lot will work for 64 bit *as long as* either | |
2500 | either long is 64 bit | |
2501 | or we can find both strtol/strtoq and strtoul/strtouq | |
2502 | If not, we really should refuse to let the user use 64 bit IVs | |
2503 | By "64 bit" I really mean IVs that don't get preserved by NVs | |
2504 | It also should work for 128 bit IVs. Can any lend me a machine to | |
2505 | test this? | |
2506 | */ | |
2507 | if (s - nbegin > TYPE_DIGITS(UV)) /* Cannot cache ato[ul]() */ | |
2508 | numtype |= IS_NUMBER_TO_INT_BY_ATOF | IS_NUMBER_LONGER_THAN_IV_MAX; | |
2509 | else if (s - nbegin < BIT_DIGITS(((sizeof (IV)>sizeof (long)) | |
2510 | ? sizeof(long) : sizeof (IV))*8-1)) | |
f7bbb42a | 2511 | numtype |= IS_NUMBER_TO_INT_BY_ATOL; |
28e5dec8 JH |
2512 | else |
2513 | /* Can't be sure either way. (For 64 bit UV, 63 bit IV is 1 decimal | |
2514 | digit less (IV_MAX= 9223372036854775807, | |
2515 | UV_MAX= 18446744073709551615) so be cautious */ | |
2516 | numtype |= IS_NUMBER_TO_INT_BY_STRTOL | IS_NUMBER_AS_LONG_AS_IV_MAX; | |
25da4f38 | 2517 | |
097ee67d | 2518 | if (*s == '.' |
1c846c1f | 2519 | #ifdef USE_LOCALE_NUMERIC |
eff180cd | 2520 | || (specialradix = IS_NUMERIC_RADIX(s)) |
097ee67d JH |
2521 | #endif |
2522 | ) { | |
9c7192ba | 2523 | #ifdef USE_LOCALE_NUMERIC |
eff180cd JH |
2524 | if (specialradix) |
2525 | s += SvCUR(PL_numeric_radix); | |
2526 | else | |
9c7192ba | 2527 | #endif |
eff180cd | 2528 | s++; |
28e5dec8 | 2529 | numtype |= IS_NUMBER_NOT_INT; |
097ee67d | 2530 | while (isDIGIT(*s)) /* optional digits after the radix */ |
ff0cee69 | 2531 | s++; |
2532 | } | |
36477c24 | 2533 | } |
097ee67d | 2534 | else if (*s == '.' |
1c846c1f | 2535 | #ifdef USE_LOCALE_NUMERIC |
eff180cd | 2536 | || (specialradix = IS_NUMERIC_RADIX(s)) |
097ee67d JH |
2537 | #endif |
2538 | ) { | |
9c7192ba | 2539 | #ifdef USE_LOCALE_NUMERIC |
eff180cd JH |
2540 | if (specialradix) |
2541 | s += SvCUR(PL_numeric_radix); | |
2542 | else | |
9c7192ba | 2543 | #endif |
eff180cd | 2544 | s++; |
28e5dec8 | 2545 | numtype |= IS_NUMBER_TO_INT_BY_ATOL | IS_NUMBER_NOT_INT; |
097ee67d | 2546 | /* no digits before the radix means we need digits after it */ |
ff0cee69 | 2547 | if (isDIGIT(*s)) { |
2548 | do { | |
2549 | s++; | |
2550 | } while (isDIGIT(*s)); | |
2551 | } | |
2552 | else | |
2553 | return 0; | |
2554 | } | |
300aed98 JH |
2555 | else if (*s == 'I' || *s == 'i') { |
2556 | s++; if (*s != 'N' && *s != 'n') return 0; | |
2557 | s++; if (*s != 'F' && *s != 'f') return 0; | |
2558 | s++; if (*s == 'I' || *s == 'i') { | |
2559 | s++; if (*s != 'N' && *s != 'n') return 0; | |
2560 | s++; if (*s != 'I' && *s != 'i') return 0; | |
2561 | s++; if (*s != 'T' && *s != 't') return 0; | |
2562 | s++; if (*s != 'Y' && *s != 'y') return 0; | |
99938567 | 2563 | s++; |
300aed98 JH |
2564 | } |
2565 | sawinf = 1; | |
2566 | } | |
ff0cee69 | 2567 | else |
2568 | return 0; | |
2569 | ||
300aed98 | 2570 | if (sawinf) |
28e5dec8 JH |
2571 | numtype = (numtype & IS_NUMBER_NEG) /* Keep track of sign */ |
2572 | | IS_NUMBER_INFINITY | IS_NUMBER_NOT_INT; | |
300aed98 JH |
2573 | else { |
2574 | /* we can have an optional exponent part */ | |
2575 | if (*s == 'e' || *s == 'E') { | |
28e5dec8 JH |
2576 | numtype &= IS_NUMBER_NEG; |
2577 | numtype |= IS_NUMBER_TO_INT_BY_ATOF | IS_NUMBER_NOT_INT; | |
36477c24 | 2578 | s++; |
300aed98 JH |
2579 | if (*s == '+' || *s == '-') |
2580 | s++; | |
2581 | if (isDIGIT(*s)) { | |
2582 | do { | |
2583 | s++; | |
2584 | } while (isDIGIT(*s)); | |
2585 | } | |
2586 | else | |
2587 | return 0; | |
2588 | } | |
36477c24 | 2589 | } |
2590 | while (isSPACE(*s)) | |
2591 | s++; | |
80f3f388 | 2592 | if (s >= send) |
36477c24 | 2593 | return numtype; |
2594 | if (len == 10 && memEQ(sbegin, "0 but true", 10)) | |
25da4f38 | 2595 | return IS_NUMBER_TO_INT_BY_ATOL; |
36477c24 | 2596 | return 0; |
2597 | } | |
2598 | ||
79072805 | 2599 | char * |
864dbfa3 | 2600 | Perl_sv_2pv_nolen(pTHX_ register SV *sv) |
1fa8b10d JD |
2601 | { |
2602 | STRLEN n_a; | |
2603 | return sv_2pv(sv, &n_a); | |
2604 | } | |
2605 | ||
25da4f38 | 2606 | /* We assume that buf is at least TYPE_CHARS(UV) long. */ |
864dbfa3 | 2607 | static char * |
25da4f38 IZ |
2608 | uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob) |
2609 | { | |
25da4f38 IZ |
2610 | char *ptr = buf + TYPE_CHARS(UV); |
2611 | char *ebuf = ptr; | |
2612 | int sign; | |
25da4f38 IZ |
2613 | |
2614 | if (is_uv) | |
2615 | sign = 0; | |
2616 | else if (iv >= 0) { | |
2617 | uv = iv; | |
2618 | sign = 0; | |
2619 | } else { | |
2620 | uv = -iv; | |
2621 | sign = 1; | |
2622 | } | |
2623 | do { | |
2624 | *--ptr = '0' + (uv % 10); | |
2625 | } while (uv /= 10); | |
2626 | if (sign) | |
2627 | *--ptr = '-'; | |
2628 | *peob = ebuf; | |
2629 | return ptr; | |
2630 | } | |
2631 | ||
1fa8b10d | 2632 | char * |
864dbfa3 | 2633 | Perl_sv_2pv(pTHX_ register SV *sv, STRLEN *lp) |
79072805 LW |
2634 | { |
2635 | register char *s; | |
2636 | int olderrno; | |
46fc3d4c | 2637 | SV *tsv; |
25da4f38 IZ |
2638 | char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */ |
2639 | char *tmpbuf = tbuf; | |
79072805 | 2640 | |
463ee0b2 LW |
2641 | if (!sv) { |
2642 | *lp = 0; | |
2643 | return ""; | |
2644 | } | |
8990e307 | 2645 | if (SvGMAGICAL(sv)) { |
463ee0b2 LW |
2646 | mg_get(sv); |
2647 | if (SvPOKp(sv)) { | |
2648 | *lp = SvCUR(sv); | |
2649 | return SvPVX(sv); | |
2650 | } | |
cf2093f6 | 2651 | if (SvIOKp(sv)) { |
1c846c1f | 2652 | if (SvIsUV(sv)) |
57def98f | 2653 | (void)sprintf(tmpbuf,"%"UVuf, (UV)SvUVX(sv)); |
cf2093f6 | 2654 | else |
57def98f | 2655 | (void)sprintf(tmpbuf,"%"IVdf, (IV)SvIVX(sv)); |
46fc3d4c | 2656 | tsv = Nullsv; |
a0d0e21e | 2657 | goto tokensave; |
463ee0b2 LW |
2658 | } |
2659 | if (SvNOKp(sv)) { | |
2d4389e4 | 2660 | Gconvert(SvNVX(sv), NV_DIG, 0, tmpbuf); |
46fc3d4c | 2661 | tsv = Nullsv; |
a0d0e21e | 2662 | goto tokensave; |
463ee0b2 | 2663 | } |
16d20bd9 | 2664 | if (!SvROK(sv)) { |
d008e5eb | 2665 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 2666 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
1d7c1841 | 2667 | report_uninit(); |
c6ee37c5 | 2668 | } |
16d20bd9 AD |
2669 | *lp = 0; |
2670 | return ""; | |
2671 | } | |
463ee0b2 | 2672 | } |
ed6116ce LW |
2673 | if (SvTHINKFIRST(sv)) { |
2674 | if (SvROK(sv)) { | |
a0d0e21e | 2675 | SV* tmpstr; |
1554e226 DC |
2676 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)) && |
2677 | (SvRV(tmpstr) != SvRV(sv))) | |
9e7bc3e8 | 2678 | return SvPV(tmpstr,*lp); |
ed6116ce LW |
2679 | sv = (SV*)SvRV(sv); |
2680 | if (!sv) | |
2681 | s = "NULLREF"; | |
2682 | else { | |
f9277f47 IZ |
2683 | MAGIC *mg; |
2684 | ||
ed6116ce | 2685 | switch (SvTYPE(sv)) { |
f9277f47 IZ |
2686 | case SVt_PVMG: |
2687 | if ( ((SvFLAGS(sv) & | |
1c846c1f | 2688 | (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG)) |
3149a8e4 | 2689 | == (SVs_OBJECT|SVs_RMG)) |
57668c4d | 2690 | && strEQ(s=HvNAME(SvSTASH(sv)), "Regexp") |
f9277f47 | 2691 | && (mg = mg_find(sv, 'r'))) { |
2cd61cdb | 2692 | regexp *re = (regexp *)mg->mg_obj; |
1bd3ad17 | 2693 | |
2cd61cdb | 2694 | if (!mg->mg_ptr) { |
8782bef2 GB |
2695 | char *fptr = "msix"; |
2696 | char reflags[6]; | |
2697 | char ch; | |
2698 | int left = 0; | |
2699 | int right = 4; | |
2700 | U16 reganch = (re->reganch & PMf_COMPILETIME) >> 12; | |
2701 | ||
155aba94 | 2702 | while((ch = *fptr++)) { |
8782bef2 GB |
2703 | if(reganch & 1) { |
2704 | reflags[left++] = ch; | |
2705 | } | |
2706 | else { | |
2707 | reflags[right--] = ch; | |
2708 | } | |
2709 | reganch >>= 1; | |
2710 | } | |
2711 | if(left != 4) { | |
2712 | reflags[left] = '-'; | |
2713 | left = 5; | |
2714 | } | |
2715 | ||
2716 | mg->mg_len = re->prelen + 4 + left; | |
2717 | New(616, mg->mg_ptr, mg->mg_len + 1 + left, char); | |
2718 | Copy("(?", mg->mg_ptr, 2, char); | |
2719 | Copy(reflags, mg->mg_ptr+2, left, char); | |
2720 | Copy(":", mg->mg_ptr+left+2, 1, char); | |
2721 | Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char); | |
1bd3ad17 IZ |
2722 | mg->mg_ptr[mg->mg_len - 1] = ')'; |
2723 | mg->mg_ptr[mg->mg_len] = 0; | |
2724 | } | |
3280af22 | 2725 | PL_reginterp_cnt += re->program[0].next_off; |
1bd3ad17 IZ |
2726 | *lp = mg->mg_len; |
2727 | return mg->mg_ptr; | |
f9277f47 IZ |
2728 | } |
2729 | /* Fall through */ | |
ed6116ce LW |
2730 | case SVt_NULL: |
2731 | case SVt_IV: | |
2732 | case SVt_NV: | |
2733 | case SVt_RV: | |
2734 | case SVt_PV: | |
2735 | case SVt_PVIV: | |
2736 | case SVt_PVNV: | |
81689caa HS |
2737 | case SVt_PVBM: if (SvROK(sv)) |
2738 | s = "REF"; | |
2739 | else | |
2740 | s = "SCALAR"; break; | |
ed6116ce LW |
2741 | case SVt_PVLV: s = "LVALUE"; break; |
2742 | case SVt_PVAV: s = "ARRAY"; break; | |
2743 | case SVt_PVHV: s = "HASH"; break; | |
2744 | case SVt_PVCV: s = "CODE"; break; | |
2745 | case SVt_PVGV: s = "GLOB"; break; | |
1d2dff63 | 2746 | case SVt_PVFM: s = "FORMAT"; break; |
36477c24 | 2747 | case SVt_PVIO: s = "IO"; break; |
ed6116ce LW |
2748 | default: s = "UNKNOWN"; break; |
2749 | } | |
46fc3d4c | 2750 | tsv = NEWSV(0,0); |
ed6116ce | 2751 | if (SvOBJECT(sv)) |
cea2e8a9 | 2752 | Perl_sv_setpvf(aTHX_ tsv, "%s=%s", HvNAME(SvSTASH(sv)), s); |
ed6116ce | 2753 | else |
46fc3d4c | 2754 | sv_setpv(tsv, s); |
57def98f | 2755 | Perl_sv_catpvf(aTHX_ tsv, "(0x%"UVxf")", PTR2UV(sv)); |
a0d0e21e | 2756 | goto tokensaveref; |
463ee0b2 | 2757 | } |
ed6116ce LW |
2758 | *lp = strlen(s); |
2759 | return s; | |
79072805 | 2760 | } |
0336b60e | 2761 | if (SvREADONLY(sv) && !SvOK(sv)) { |
0336b60e | 2762 | if (ckWARN(WARN_UNINITIALIZED)) |
1d7c1841 | 2763 | report_uninit(); |
ed6116ce LW |
2764 | *lp = 0; |
2765 | return ""; | |
79072805 | 2766 | } |
79072805 | 2767 | } |
28e5dec8 JH |
2768 | if (SvIOK(sv) || ((SvIOKp(sv) && !SvNOKp(sv)))) { |
2769 | /* I'm assuming that if both IV and NV are equally valid then | |
2770 | converting the IV is going to be more efficient */ | |
2771 | U32 isIOK = SvIOK(sv); | |
2772 | U32 isUIOK = SvIsUV(sv); | |
2773 | char buf[TYPE_CHARS(UV)]; | |
2774 | char *ebuf, *ptr; | |
2775 | ||
2776 | if (SvTYPE(sv) < SVt_PVIV) | |
2777 | sv_upgrade(sv, SVt_PVIV); | |
2778 | if (isUIOK) | |
2779 | ptr = uiv_2buf(buf, 0, SvUVX(sv), 1, &ebuf); | |
2780 | else | |
2781 | ptr = uiv_2buf(buf, SvIVX(sv), 0, 0, &ebuf); | |
2782 | SvGROW(sv, ebuf - ptr + 1); /* inlined from sv_setpvn */ | |
2783 | Move(ptr,SvPVX(sv),ebuf - ptr,char); | |
2784 | SvCUR_set(sv, ebuf - ptr); | |
2785 | s = SvEND(sv); | |
2786 | *s = '\0'; | |
2787 | if (isIOK) | |
2788 | SvIOK_on(sv); | |
2789 | else | |
2790 | SvIOKp_on(sv); | |
2791 | if (isUIOK) | |
2792 | SvIsUV_on(sv); | |
2793 | } | |
2794 | else if (SvNOKp(sv)) { | |
79072805 LW |
2795 | if (SvTYPE(sv) < SVt_PVNV) |
2796 | sv_upgrade(sv, SVt_PVNV); | |
1c846c1f | 2797 | /* The +20 is pure guesswork. Configure test needed. --jhi */ |
59155cc0 | 2798 | SvGROW(sv, NV_DIG + 20); |
463ee0b2 | 2799 | s = SvPVX(sv); |
79072805 | 2800 | olderrno = errno; /* some Xenix systems wipe out errno here */ |
79072805 | 2801 | #ifdef apollo |
463ee0b2 | 2802 | if (SvNVX(sv) == 0.0) |
79072805 LW |
2803 | (void)strcpy(s,"0"); |
2804 | else | |
2805 | #endif /*apollo*/ | |
bbce6d69 | 2806 | { |
2d4389e4 | 2807 | Gconvert(SvNVX(sv), NV_DIG, 0, s); |
bbce6d69 | 2808 | } |
79072805 | 2809 | errno = olderrno; |
a0d0e21e LW |
2810 | #ifdef FIXNEGATIVEZERO |
2811 | if (*s == '-' && s[1] == '0' && !s[2]) | |
2812 | strcpy(s,"0"); | |
2813 | #endif | |
79072805 LW |
2814 | while (*s) s++; |
2815 | #ifdef hcx | |
2816 | if (s[-1] == '.') | |
46fc3d4c | 2817 | *--s = '\0'; |
79072805 LW |
2818 | #endif |
2819 | } | |
79072805 | 2820 | else { |
0336b60e IZ |
2821 | if (ckWARN(WARN_UNINITIALIZED) |
2822 | && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP)) | |
1d7c1841 | 2823 | report_uninit(); |
a0d0e21e | 2824 | *lp = 0; |
25da4f38 IZ |
2825 | if (SvTYPE(sv) < SVt_PV) |
2826 | /* Typically the caller expects that sv_any is not NULL now. */ | |
2827 | sv_upgrade(sv, SVt_PV); | |
a0d0e21e | 2828 | return ""; |
79072805 | 2829 | } |
463ee0b2 LW |
2830 | *lp = s - SvPVX(sv); |
2831 | SvCUR_set(sv, *lp); | |
79072805 | 2832 | SvPOK_on(sv); |
1d7c1841 GS |
2833 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n", |
2834 | PTR2UV(sv),SvPVX(sv))); | |
463ee0b2 | 2835 | return SvPVX(sv); |
a0d0e21e LW |
2836 | |
2837 | tokensave: | |
2838 | if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */ | |
2839 | /* Sneaky stuff here */ | |
2840 | ||
2841 | tokensaveref: | |
46fc3d4c | 2842 | if (!tsv) |
96827780 | 2843 | tsv = newSVpv(tmpbuf, 0); |
46fc3d4c | 2844 | sv_2mortal(tsv); |
2845 | *lp = SvCUR(tsv); | |
2846 | return SvPVX(tsv); | |
a0d0e21e LW |
2847 | } |
2848 | else { | |
2849 | STRLEN len; | |
46fc3d4c | 2850 | char *t; |
2851 | ||
2852 | if (tsv) { | |
2853 | sv_2mortal(tsv); | |
2854 | t = SvPVX(tsv); | |
2855 | len = SvCUR(tsv); | |
2856 | } | |
2857 | else { | |
96827780 MB |
2858 | t = tmpbuf; |
2859 | len = strlen(tmpbuf); | |
46fc3d4c | 2860 | } |
a0d0e21e | 2861 | #ifdef FIXNEGATIVEZERO |
46fc3d4c | 2862 | if (len == 2 && t[0] == '-' && t[1] == '0') { |
2863 | t = "0"; | |
2864 | len = 1; | |
2865 | } | |
a0d0e21e LW |
2866 | #endif |
2867 | (void)SvUPGRADE(sv, SVt_PV); | |
46fc3d4c | 2868 | *lp = len; |
a0d0e21e LW |
2869 | s = SvGROW(sv, len + 1); |
2870 | SvCUR_set(sv, len); | |
46fc3d4c | 2871 | (void)strcpy(s, t); |
6bf554b4 | 2872 | SvPOKp_on(sv); |
a0d0e21e LW |
2873 | return s; |
2874 | } | |
463ee0b2 LW |
2875 | } |
2876 | ||
7340a771 GS |
2877 | char * |
2878 | Perl_sv_2pvbyte_nolen(pTHX_ register SV *sv) | |
2879 | { | |
560a288e GS |
2880 | STRLEN n_a; |
2881 | return sv_2pvbyte(sv, &n_a); | |
7340a771 GS |
2882 | } |
2883 | ||
2884 | char * | |
2885 | Perl_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp) | |
2886 | { | |
2887 | return sv_2pv(sv,lp); | |
2888 | } | |
2889 | ||
2890 | char * | |
2891 | Perl_sv_2pvutf8_nolen(pTHX_ register SV *sv) | |
2892 | { | |
560a288e GS |
2893 | STRLEN n_a; |
2894 | return sv_2pvutf8(sv, &n_a); | |
7340a771 GS |
2895 | } |
2896 | ||
2897 | char * | |
2898 | Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp) | |
2899 | { | |
560a288e | 2900 | sv_utf8_upgrade(sv); |
7d59b7e4 | 2901 | return SvPV(sv,*lp); |
7340a771 | 2902 | } |
1c846c1f | 2903 | |
463ee0b2 LW |
2904 | /* This function is only called on magical items */ |
2905 | bool | |
864dbfa3 | 2906 | Perl_sv_2bool(pTHX_ register SV *sv) |
463ee0b2 | 2907 | { |
8990e307 | 2908 | if (SvGMAGICAL(sv)) |
463ee0b2 LW |
2909 | mg_get(sv); |
2910 | ||
a0d0e21e LW |
2911 | if (!SvOK(sv)) |
2912 | return 0; | |
2913 | if (SvROK(sv)) { | |
a0d0e21e | 2914 | SV* tmpsv; |
1554e226 DC |
2915 | if (SvAMAGIC(sv) && (tmpsv=AMG_CALLun(sv,bool_)) && |
2916 | (SvRV(tmpsv) != SvRV(sv))) | |
9e7bc3e8 | 2917 | return SvTRUE(tmpsv); |
a0d0e21e LW |
2918 | return SvRV(sv) != 0; |
2919 | } | |
463ee0b2 | 2920 | if (SvPOKp(sv)) { |
11343788 MB |
2921 | register XPV* Xpvtmp; |
2922 | if ((Xpvtmp = (XPV*)SvANY(sv)) && | |
2923 | (*Xpvtmp->xpv_pv > '0' || | |
2924 | Xpvtmp->xpv_cur > 1 || | |
2925 | (Xpvtmp->xpv_cur && *Xpvtmp->xpv_pv != '0'))) | |
463ee0b2 LW |
2926 | return 1; |
2927 | else | |
2928 | return 0; | |
2929 | } | |
2930 | else { | |
2931 | if (SvIOKp(sv)) | |
2932 | return SvIVX(sv) != 0; | |
2933 | else { | |
2934 | if (SvNOKp(sv)) | |
2935 | return SvNVX(sv) != 0.0; | |
2936 | else | |
2937 | return FALSE; | |
2938 | } | |
2939 | } | |
79072805 LW |
2940 | } |
2941 | ||
c461cf8f JH |
2942 | /* |
2943 | =for apidoc sv_utf8_upgrade | |
2944 | ||
2945 | Convert the PV of an SV to its UTF8-encoded form. | |
2946 | ||
2947 | =cut | |
2948 | */ | |
2949 | ||
560a288e GS |
2950 | void |
2951 | Perl_sv_utf8_upgrade(pTHX_ register SV *sv) | |
2952 | { | |
511c2ff0 NIS |
2953 | char *s, *t, *e; |
2954 | int hibit = 0; | |
560a288e | 2955 | |
0c981600 | 2956 | if (!sv || !SvPOK(sv) || SvUTF8(sv)) |
560a288e GS |
2957 | return; |
2958 | ||
40826f67 JH |
2959 | /* This function could be much more efficient if we had a FLAG in SVs |
2960 | * to signal if there are any hibit chars in the PV. | |
511c2ff0 | 2961 | * Given that there isn't make loop fast as possible |
560a288e | 2962 | */ |
511c2ff0 NIS |
2963 | s = SvPVX(sv); |
2964 | e = SvEND(sv); | |
2965 | t = s; | |
2966 | while (t < e) { | |
fd400ab9 | 2967 | if ((hibit = UTF8_IS_CONTINUED(*t++))) |
8a818333 | 2968 | break; |
8a818333 | 2969 | } |
560a288e | 2970 | |
40826f67 | 2971 | if (hibit) { |
8a818333 | 2972 | STRLEN len; |
652088fc | 2973 | |
8a818333 | 2974 | if (SvREADONLY(sv) && SvFAKE(sv)) { |
8a818333 NIS |
2975 | sv_force_normal(sv); |
2976 | s = SvPVX(sv); | |
2977 | } | |
2978 | len = SvCUR(sv) + 1; /* Plus the \0 */ | |
00df9076 | 2979 | SvPVX(sv) = (char*)bytes_to_utf8((U8*)s, &len); |
841d7a39 | 2980 | SvCUR(sv) = len - 1; |
511c2ff0 NIS |
2981 | if (SvLEN(sv) != 0) |
2982 | Safefree(s); /* No longer using what was there before. */ | |
841d7a39 | 2983 | SvLEN(sv) = len; /* No longer know the real size. */ |
560a288e GS |
2984 | SvUTF8_on(sv); |
2985 | } | |
2986 | } | |
2987 | ||
c461cf8f JH |
2988 | /* |
2989 | =for apidoc sv_utf8_downgrade | |
2990 | ||
2991 | Attempt to convert the PV of an SV from UTF8-encoded to byte encoding. | |
2992 | This may not be possible if the PV contains non-byte encoding characters; | |
2993 | if this is the case, either returns false or, if C<fail_ok> is not | |
2994 | true, croaks. | |
2995 | ||
2996 | =cut | |
2997 | */ | |
2998 | ||
560a288e GS |
2999 | bool |
3000 | Perl_sv_utf8_downgrade(pTHX_ register SV* sv, bool fail_ok) | |
3001 | { | |
3002 | if (SvPOK(sv) && SvUTF8(sv)) { | |
fa301091 | 3003 | if (SvCUR(sv)) { |
652088fc JH |
3004 | char *s; |
3005 | STRLEN len; | |
fa301091 | 3006 | |
652088fc JH |
3007 | if (SvREADONLY(sv) && SvFAKE(sv)) |
3008 | sv_force_normal(sv); | |
3009 | s = SvPV(sv, len); | |
3010 | if (!utf8_to_bytes((U8*)s, &len)) { | |
fa301091 JH |
3011 | if (fail_ok) |
3012 | return FALSE; | |
3013 | else { | |
3014 | if (PL_op) | |
3015 | Perl_croak(aTHX_ "Wide character in %s", | |
3016 | PL_op_desc[PL_op->op_type]); | |
3017 | else | |
3018 | Perl_croak(aTHX_ "Wide character"); | |
3019 | } | |
4b3603a4 | 3020 | } |
fa301091 | 3021 | SvCUR(sv) = len; |
67e989fb | 3022 | } |
9f9ab905 | 3023 | SvUTF8_off(sv); |
560a288e | 3024 | } |
fa301091 | 3025 | |
560a288e GS |
3026 | return TRUE; |
3027 | } | |
3028 | ||
c461cf8f JH |
3029 | /* |
3030 | =for apidoc sv_utf8_encode | |
3031 | ||
3032 | Convert the PV of an SV to UTF8-encoded, but then turn off the C<SvUTF8> | |
1c846c1f | 3033 | flag so that it looks like bytes again. Nothing calls this. |
c461cf8f JH |
3034 | |
3035 | =cut | |
3036 | */ | |
3037 | ||
560a288e GS |
3038 | void |
3039 | Perl_sv_utf8_encode(pTHX_ register SV *sv) | |
3040 | { | |
3041 | sv_utf8_upgrade(sv); | |
3042 | SvUTF8_off(sv); | |
3043 | } | |
3044 | ||
3045 | bool | |
3046 | Perl_sv_utf8_decode(pTHX_ register SV *sv) | |
3047 | { | |
3048 | if (SvPOK(sv)) { | |
3049 | char *c; | |
511c2ff0 | 3050 | char *e; |
560a288e GS |
3051 | bool has_utf = FALSE; |
3052 | if (!sv_utf8_downgrade(sv, TRUE)) | |
3053 | return FALSE; | |
3054 | ||
3055 | /* it is actually just a matter of turning the utf8 flag on, but | |
3056 | * we want to make sure everything inside is valid utf8 first. | |
3057 | */ | |
3058 | c = SvPVX(sv); | |
00df9076 | 3059 | if (!is_utf8_string((U8*)c, SvCUR(sv)+1)) |
67e989fb | 3060 | return FALSE; |
511c2ff0 NIS |
3061 | e = SvEND(sv); |
3062 | while (c < e) { | |
fd400ab9 | 3063 | if (UTF8_IS_CONTINUED(*c++)) { |
67e989fb JH |
3064 | SvUTF8_on(sv); |
3065 | break; | |
3066 | } | |
560a288e | 3067 | } |
560a288e GS |
3068 | } |
3069 | return TRUE; | |
3070 | } | |
3071 | ||
3072 | ||
79072805 | 3073 | /* Note: sv_setsv() should not be called with a source string that needs |
463ee0b2 | 3074 | * to be reused, since it may destroy the source string if it is marked |
79072805 LW |
3075 | * as temporary. |
3076 | */ | |
3077 | ||
954c1994 GS |
3078 | /* |
3079 | =for apidoc sv_setsv | |
3080 | ||
3081 | Copies the contents of the source SV C<ssv> into the destination SV C<dsv>. | |
3082 | The source SV may be destroyed if it is mortal. Does not handle 'set' | |
3083 | magic. See the macro forms C<SvSetSV>, C<SvSetSV_nosteal> and | |
3084 | C<sv_setsv_mg>. | |
3085 | ||
3086 | =cut | |
3087 | */ | |
3088 | ||
79072805 | 3089 | void |
864dbfa3 | 3090 | Perl_sv_setsv(pTHX_ SV *dstr, register SV *sstr) |
79072805 | 3091 | { |
8990e307 LW |
3092 | register U32 sflags; |
3093 | register int dtype; | |
3094 | register int stype; | |
463ee0b2 | 3095 | |
79072805 LW |
3096 | if (sstr == dstr) |
3097 | return; | |
2213622d | 3098 | SV_CHECK_THINKFIRST(dstr); |
79072805 | 3099 | if (!sstr) |
3280af22 | 3100 | sstr = &PL_sv_undef; |
8990e307 LW |
3101 | stype = SvTYPE(sstr); |
3102 | dtype = SvTYPE(dstr); | |
79072805 | 3103 | |
a0d0e21e | 3104 | SvAMAGIC_off(dstr); |
9e7bc3e8 | 3105 | |
463ee0b2 | 3106 | /* There's a lot of redundancy below but we're going for speed here */ |
79072805 | 3107 | |
8990e307 | 3108 | switch (stype) { |
79072805 | 3109 | case SVt_NULL: |
aece5585 | 3110 | undef_sstr: |
20408e3c GS |
3111 | if (dtype != SVt_PVGV) { |
3112 | (void)SvOK_off(dstr); | |
3113 | return; | |
3114 | } | |
3115 | break; | |
463ee0b2 | 3116 | case SVt_IV: |
aece5585 GA |
3117 | if (SvIOK(sstr)) { |
3118 | switch (dtype) { | |
3119 | case SVt_NULL: | |
8990e307 | 3120 | sv_upgrade(dstr, SVt_IV); |
aece5585 GA |
3121 | break; |
3122 | case SVt_NV: | |
8990e307 | 3123 | sv_upgrade(dstr, SVt_PVNV); |
aece5585 GA |
3124 | break; |
3125 | case SVt_RV: | |
3126 | case SVt_PV: | |
a0d0e21e | 3127 | sv_upgrade(dstr, SVt_PVIV); |
aece5585 GA |
3128 | break; |
3129 | } | |
3130 | (void)SvIOK_only(dstr); | |
3131 | SvIVX(dstr) = SvIVX(sstr); | |
25da4f38 IZ |
3132 | if (SvIsUV(sstr)) |
3133 | SvIsUV_on(dstr); | |
27c9684d AP |
3134 | if (SvTAINTED(sstr)) |
3135 | SvTAINT(dstr); | |
aece5585 | 3136 | return; |
8990e307 | 3137 | } |
aece5585 GA |
3138 | goto undef_sstr; |
3139 | ||
463ee0b2 | 3140 | case SVt_NV: |
aece5585 GA |
3141 | if (SvNOK(sstr)) { |
3142 | switch (dtype) { | |
3143 | case SVt_NULL: | |
3144 | case SVt_IV: | |
8990e307 | 3145 | sv_upgrade(dstr, SVt_NV); |
aece5585 GA |
3146 | break; |
3147 | case SVt_RV: | |
3148 | case SVt_PV: | |
3149 | case SVt_PVIV: | |
a0d0e21e | 3150 | sv_upgrade(dstr, SVt_PVNV); |
aece5585 GA |
3151 | break; |
3152 | } | |
3153 | SvNVX(dstr) = SvNVX(sstr); | |
3154 | (void)SvNOK_only(dstr); | |
27c9684d AP |
3155 | if (SvTAINTED(sstr)) |
3156 | SvTAINT(dstr); | |
aece5585 | 3157 | return; |
8990e307 | 3158 | } |
aece5585 GA |
3159 | goto undef_sstr; |
3160 | ||
ed6116ce | 3161 | case SVt_RV: |
8990e307 | 3162 | if (dtype < SVt_RV) |
ed6116ce | 3163 | sv_upgrade(dstr, SVt_RV); |
c07a80fd | 3164 | else if (dtype == SVt_PVGV && |
3165 | SvTYPE(SvRV(sstr)) == SVt_PVGV) { | |
3166 | sstr = SvRV(sstr); | |
a5f75d66 | 3167 | if (sstr == dstr) { |
1d7c1841 GS |
3168 | if (GvIMPORTED(dstr) != GVf_IMPORTED |
3169 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) | |
3170 | { | |
a5f75d66 | 3171 | GvIMPORTED_on(dstr); |
1d7c1841 | 3172 | } |
a5f75d66 AD |
3173 | GvMULTI_on(dstr); |
3174 | return; | |
3175 | } | |
c07a80fd | 3176 | goto glob_assign; |
3177 | } | |
ed6116ce | 3178 | break; |
463ee0b2 | 3179 | case SVt_PV: |
fc36a67e | 3180 | case SVt_PVFM: |
8990e307 | 3181 | if (dtype < SVt_PV) |
463ee0b2 | 3182 | sv_upgrade(dstr, SVt_PV); |
463ee0b2 LW |
3183 | break; |
3184 | case SVt_PVIV: | |
8990e307 | 3185 | if (dtype < SVt_PVIV) |
463ee0b2 | 3186 | sv_upgrade(dstr, SVt_PVIV); |
463ee0b2 LW |
3187 | break; |
3188 | case SVt_PVNV: | |
8990e307 | 3189 | if (dtype < SVt_PVNV) |
463ee0b2 | 3190 | sv_upgrade(dstr, SVt_PVNV); |
463ee0b2 | 3191 | break; |
4633a7c4 LW |
3192 | case SVt_PVAV: |
3193 | case SVt_PVHV: | |
3194 | case SVt_PVCV: | |
4633a7c4 | 3195 | case SVt_PVIO: |
533c011a | 3196 | if (PL_op) |
cea2e8a9 | 3197 | Perl_croak(aTHX_ "Bizarre copy of %s in %s", sv_reftype(sstr, 0), |
22c35a8c | 3198 | PL_op_name[PL_op->op_type]); |
4633a7c4 | 3199 | else |
cea2e8a9 | 3200 | Perl_croak(aTHX_ "Bizarre copy of %s", sv_reftype(sstr, 0)); |
4633a7c4 LW |
3201 | break; |
3202 | ||
79072805 | 3203 | case SVt_PVGV: |
8990e307 | 3204 | if (dtype <= SVt_PVGV) { |
c07a80fd | 3205 | glob_assign: |
a5f75d66 | 3206 | if (dtype != SVt_PVGV) { |
a0d0e21e LW |
3207 | char *name = GvNAME(sstr); |
3208 | STRLEN len = GvNAMELEN(sstr); | |
463ee0b2 | 3209 | sv_upgrade(dstr, SVt_PVGV); |
6662521e | 3210 | sv_magic(dstr, dstr, '*', Nullch, 0); |
85aff577 | 3211 | GvSTASH(dstr) = (HV*)SvREFCNT_inc(GvSTASH(sstr)); |
a0d0e21e LW |
3212 | GvNAME(dstr) = savepvn(name, len); |
3213 | GvNAMELEN(dstr) = len; | |
3214 | SvFAKE_on(dstr); /* can coerce to non-glob */ | |
3215 | } | |
7bac28a0 | 3216 | /* ahem, death to those who redefine active sort subs */ |
3280af22 NIS |
3217 | else if (PL_curstackinfo->si_type == PERLSI_SORT |
3218 | && GvCV(dstr) && PL_sortcop == CvSTART(GvCV(dstr))) | |
cea2e8a9 | 3219 | Perl_croak(aTHX_ "Can't redefine active sort subroutine %s", |
7bac28a0 | 3220 | GvNAME(dstr)); |
a0d0e21e | 3221 | (void)SvOK_off(dstr); |
a5f75d66 | 3222 | GvINTRO_off(dstr); /* one-shot flag */ |
1edc1566 | 3223 | gp_free((GV*)dstr); |
79072805 | 3224 | GvGP(dstr) = gp_ref(GvGP(sstr)); |
27c9684d AP |
3225 | if (SvTAINTED(sstr)) |
3226 | SvTAINT(dstr); | |
1d7c1841 GS |
3227 | if (GvIMPORTED(dstr) != GVf_IMPORTED |
3228 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) | |
3229 | { | |
a5f75d66 | 3230 | GvIMPORTED_on(dstr); |
1d7c1841 | 3231 | } |
a5f75d66 | 3232 | GvMULTI_on(dstr); |
79072805 LW |
3233 | return; |
3234 | } | |
3235 | /* FALL THROUGH */ | |
3236 | ||
3237 | default: | |
973f89ab CS |
3238 | if (SvGMAGICAL(sstr)) { |
3239 | mg_get(sstr); | |
3240 | if (SvTYPE(sstr) != stype) { | |
3241 | stype = SvTYPE(sstr); | |
3242 | if (stype == SVt_PVGV && dtype <= SVt_PVGV) | |
3243 | goto glob_assign; | |
3244 | } | |
3245 | } | |
ded42b9f | 3246 | if (stype == SVt_PVLV) |
6fc92669 | 3247 | (void)SvUPGRADE(dstr, SVt_PVNV); |
ded42b9f | 3248 | else |
6fc92669 | 3249 | (void)SvUPGRADE(dstr, stype); |
79072805 LW |
3250 | } |
3251 | ||
8990e307 LW |
3252 | sflags = SvFLAGS(sstr); |
3253 | ||
3254 | if (sflags & SVf_ROK) { | |
3255 | if (dtype >= SVt_PV) { | |
3256 | if (dtype == SVt_PVGV) { | |
3257 | SV *sref = SvREFCNT_inc(SvRV(sstr)); | |
3258 | SV *dref = 0; | |
a5f75d66 | 3259 | int intro = GvINTRO(dstr); |
a0d0e21e LW |
3260 | |
3261 | if (intro) { | |
3262 | GP *gp; | |
1d7c1841 | 3263 | gp_free((GV*)dstr); |
a5f75d66 | 3264 | GvINTRO_off(dstr); /* one-shot flag */ |
a0d0e21e | 3265 | Newz(602,gp, 1, GP); |
44a8e56a | 3266 | GvGP(dstr) = gp_ref(gp); |
a0d0e21e | 3267 | GvSV(dstr) = NEWSV(72,0); |
1d7c1841 | 3268 | GvLINE(dstr) = CopLINE(PL_curcop); |
1edc1566 | 3269 | GvEGV(dstr) = (GV*)dstr; |
a0d0e21e | 3270 | } |
a5f75d66 | 3271 | GvMULTI_on(dstr); |
8990e307 LW |
3272 | switch (SvTYPE(sref)) { |
3273 | case SVt_PVAV: | |
a0d0e21e LW |
3274 | if (intro) |
3275 | SAVESPTR(GvAV(dstr)); | |
3276 | else | |
3277 | dref = (SV*)GvAV(dstr); | |
8990e307 | 3278 | GvAV(dstr) = (AV*)sref; |
39bac7f7 | 3279 | if (!GvIMPORTED_AV(dstr) |
1d7c1841 GS |
3280 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
3281 | { | |
a5f75d66 | 3282 | GvIMPORTED_AV_on(dstr); |
1d7c1841 | 3283 | } |
8990e307 LW |
3284 | break; |
3285 | case SVt_PVHV: | |
a0d0e21e LW |
3286 | if (intro) |
3287 | SAVESPTR(GvHV(dstr)); | |
3288 | else | |
3289 | dref = (SV*)GvHV(dstr); | |
8990e307 | 3290 | GvHV(dstr) = (HV*)sref; |
39bac7f7 | 3291 | if (!GvIMPORTED_HV(dstr) |
1d7c1841 GS |
3292 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
3293 | { | |
a5f75d66 | 3294 | GvIMPORTED_HV_on(dstr); |
1d7c1841 | 3295 | } |
8990e307 LW |
3296 | break; |
3297 | case SVt_PVCV: | |
8ebc5c01 | 3298 | if (intro) { |
3299 | if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) { | |
3300 | SvREFCNT_dec(GvCV(dstr)); | |
3301 | GvCV(dstr) = Nullcv; | |
68dc0745 | 3302 | GvCVGEN(dstr) = 0; /* Switch off cacheness. */ |
3280af22 | 3303 | PL_sub_generation++; |
8ebc5c01 | 3304 | } |
a0d0e21e | 3305 | SAVESPTR(GvCV(dstr)); |
8ebc5c01 | 3306 | } |
68dc0745 | 3307 | else |
3308 | dref = (SV*)GvCV(dstr); | |
3309 | if (GvCV(dstr) != (CV*)sref) { | |
748a9306 | 3310 | CV* cv = GvCV(dstr); |
4633a7c4 | 3311 | if (cv) { |
68dc0745 | 3312 | if (!GvCVGEN((GV*)dstr) && |
3313 | (CvROOT(cv) || CvXSUB(cv))) | |
3314 | { | |
beab0874 | 3315 | SV *const_sv; |
7bac28a0 | 3316 | /* ahem, death to those who redefine |
3317 | * active sort subs */ | |
3280af22 NIS |
3318 | if (PL_curstackinfo->si_type == PERLSI_SORT && |
3319 | PL_sortcop == CvSTART(cv)) | |
1c846c1f | 3320 | Perl_croak(aTHX_ |
7bac28a0 | 3321 | "Can't redefine active sort subroutine %s", |
3322 | GvENAME((GV*)dstr)); | |
beab0874 JT |
3323 | /* Redefining a sub - warning is mandatory if |
3324 | it was a const and its value changed. */ | |
3325 | if (ckWARN(WARN_REDEFINE) | |
3326 | || (CvCONST(cv) | |
3327 | && (!CvCONST((CV*)sref) | |
3328 | || sv_cmp(cv_const_sv(cv), | |
3329 | cv_const_sv((CV*)sref))))) | |
3330 | { | |
3331 | Perl_warner(aTHX_ WARN_REDEFINE, | |
3332 | CvCONST(cv) | |
3333 | ? "Constant subroutine %s redefined" | |
47deb5e7 | 3334 | : "Subroutine %s redefined", |
beab0874 JT |
3335 | GvENAME((GV*)dstr)); |
3336 | } | |
9607fc9c | 3337 | } |
3fe9a6f1 | 3338 | cv_ckproto(cv, (GV*)dstr, |
3339 | SvPOK(sref) ? SvPVX(sref) : Nullch); | |
4633a7c4 | 3340 | } |
a5f75d66 | 3341 | GvCV(dstr) = (CV*)sref; |
7a4c00b4 | 3342 | GvCVGEN(dstr) = 0; /* Switch off cacheness. */ |
a5f75d66 | 3343 | GvASSUMECV_on(dstr); |
3280af22 | 3344 | PL_sub_generation++; |
a5f75d66 | 3345 | } |
39bac7f7 | 3346 | if (!GvIMPORTED_CV(dstr) |
1d7c1841 GS |
3347 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
3348 | { | |
a5f75d66 | 3349 | GvIMPORTED_CV_on(dstr); |
1d7c1841 | 3350 | } |
8990e307 | 3351 | break; |
91bba347 LW |
3352 | case SVt_PVIO: |
3353 | if (intro) | |
3354 | SAVESPTR(GvIOp(dstr)); | |
3355 | else | |
3356 | dref = (SV*)GvIOp(dstr); | |
3357 | GvIOp(dstr) = (IO*)sref; | |
3358 | break; | |
f4d13ee9 JH |
3359 | case SVt_PVFM: |
3360 | if (intro) | |
3361 | SAVESPTR(GvFORM(dstr)); | |
3362 | else | |
3363 | dref = (SV*)GvFORM(dstr); | |
3364 | GvFORM(dstr) = (CV*)sref; | |
3365 | break; | |
8990e307 | 3366 | default: |
a0d0e21e LW |
3367 | if (intro) |
3368 | SAVESPTR(GvSV(dstr)); | |
3369 | else | |
3370 | dref = (SV*)GvSV(dstr); | |
8990e307 | 3371 | GvSV(dstr) = sref; |
39bac7f7 | 3372 | if (!GvIMPORTED_SV(dstr) |
1d7c1841 GS |
3373 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
3374 | { | |
a5f75d66 | 3375 | GvIMPORTED_SV_on(dstr); |
1d7c1841 | 3376 | } |
8990e307 LW |
3377 | break; |
3378 | } | |
3379 | if (dref) | |
3380 | SvREFCNT_dec(dref); | |
a0d0e21e LW |
3381 | if (intro) |
3382 | SAVEFREESV(sref); | |
27c9684d AP |
3383 | if (SvTAINTED(sstr)) |
3384 | SvTAINT(dstr); | |
8990e307 LW |
3385 | return; |
3386 | } | |
a0d0e21e | 3387 | if (SvPVX(dstr)) { |
760ac839 | 3388 | (void)SvOOK_off(dstr); /* backoff */ |
50483b2c JD |
3389 | if (SvLEN(dstr)) |
3390 | Safefree(SvPVX(dstr)); | |
a0d0e21e LW |
3391 | SvLEN(dstr)=SvCUR(dstr)=0; |
3392 | } | |
8990e307 | 3393 | } |
a0d0e21e | 3394 | (void)SvOK_off(dstr); |
8990e307 | 3395 | SvRV(dstr) = SvREFCNT_inc(SvRV(sstr)); |
ed6116ce | 3396 | SvROK_on(dstr); |
8990e307 | 3397 | if (sflags & SVp_NOK) { |
3332b3c1 JH |
3398 | SvNOKp_on(dstr); |
3399 | /* Only set the public OK flag if the source has public OK. */ | |
3400 | if (sflags & SVf_NOK) | |
3401 | SvFLAGS(dstr) |= SVf_NOK; | |
ed6116ce LW |
3402 | SvNVX(dstr) = SvNVX(sstr); |
3403 | } | |
8990e307 | 3404 | if (sflags & SVp_IOK) { |
3332b3c1 JH |
3405 | (void)SvIOKp_on(dstr); |
3406 | if (sflags & SVf_IOK) | |
3407 | SvFLAGS(dstr) |= SVf_IOK; | |
2b1c7e3e | 3408 | if (sflags & SVf_IVisUV) |
25da4f38 | 3409 | SvIsUV_on(dstr); |
3332b3c1 | 3410 | SvIVX(dstr) = SvIVX(sstr); |
ed6116ce | 3411 | } |
a0d0e21e LW |
3412 | if (SvAMAGIC(sstr)) { |
3413 | SvAMAGIC_on(dstr); | |
3414 | } | |
ed6116ce | 3415 | } |
8990e307 | 3416 | else if (sflags & SVp_POK) { |
79072805 LW |
3417 | |
3418 | /* | |
3419 | * Check to see if we can just swipe the string. If so, it's a | |
3420 | * possible small lose on short strings, but a big win on long ones. | |
463ee0b2 LW |
3421 | * It might even be a win on short strings if SvPVX(dstr) |
3422 | * has to be allocated and SvPVX(sstr) has to be freed. | |
79072805 LW |
3423 | */ |
3424 | ||
ff68c719 | 3425 | if (SvTEMP(sstr) && /* slated for free anyway? */ |
01b73108 | 3426 | SvREFCNT(sstr) == 1 && /* and no other references to it? */ |
1c846c1f | 3427 | !(sflags & SVf_OOK) && /* and not involved in OOK hack? */ |
4c8f17b9 BH |
3428 | SvLEN(sstr) && /* and really is a string */ |
3429 | !(PL_op && PL_op->op_type == OP_AASSIGN)) /* and won't be needed again, potentially */ | |
a5f75d66 | 3430 | { |
adbc6bb1 | 3431 | if (SvPVX(dstr)) { /* we know that dtype >= SVt_PV */ |
a5f75d66 AD |
3432 | if (SvOOK(dstr)) { |
3433 | SvFLAGS(dstr) &= ~SVf_OOK; | |
3434 | Safefree(SvPVX(dstr) - SvIVX(dstr)); | |
3435 | } | |
50483b2c | 3436 | else if (SvLEN(dstr)) |
a5f75d66 | 3437 | Safefree(SvPVX(dstr)); |
79072805 | 3438 | } |
a5f75d66 | 3439 | (void)SvPOK_only(dstr); |
463ee0b2 | 3440 | SvPV_set(dstr, SvPVX(sstr)); |
79072805 LW |
3441 | SvLEN_set(dstr, SvLEN(sstr)); |
3442 | SvCUR_set(dstr, SvCUR(sstr)); | |
f4e86e0f | 3443 | |
79072805 | 3444 | SvTEMP_off(dstr); |
2b1c7e3e | 3445 | (void)SvOK_off(sstr); /* NOTE: nukes most SvFLAGS on sstr */ |
79072805 LW |
3446 | SvPV_set(sstr, Nullch); |
3447 | SvLEN_set(sstr, 0); | |
a5f75d66 AD |
3448 | SvCUR_set(sstr, 0); |
3449 | SvTEMP_off(sstr); | |
79072805 LW |
3450 | } |
3451 | else { /* have to copy actual string */ | |
8990e307 LW |
3452 | STRLEN len = SvCUR(sstr); |
3453 | ||
3454 | SvGROW(dstr, len + 1); /* inlined from sv_setpvn */ | |
3455 | Move(SvPVX(sstr),SvPVX(dstr),len,char); | |
3456 | SvCUR_set(dstr, len); | |
3457 | *SvEND(dstr) = '\0'; | |
a0d0e21e | 3458 | (void)SvPOK_only(dstr); |
79072805 | 3459 | } |
9aa983d2 | 3460 | if (sflags & SVf_UTF8) |
a7cb1f99 | 3461 | SvUTF8_on(dstr); |
79072805 | 3462 | /*SUPPRESS 560*/ |
8990e307 | 3463 | if (sflags & SVp_NOK) { |
3332b3c1 JH |
3464 | SvNOKp_on(dstr); |
3465 | if (sflags & SVf_NOK) | |
3466 | SvFLAGS(dstr) |= SVf_NOK; | |
463ee0b2 | 3467 | SvNVX(dstr) = SvNVX(sstr); |
79072805 | 3468 | } |
8990e307 | 3469 | if (sflags & SVp_IOK) { |
3332b3c1 JH |
3470 | (void)SvIOKp_on(dstr); |
3471 | if (sflags & SVf_IOK) | |
3472 | SvFLAGS(dstr) |= SVf_IOK; | |
2b1c7e3e | 3473 | if (sflags & SVf_IVisUV) |
25da4f38 | 3474 | SvIsUV_on(dstr); |
463ee0b2 | 3475 | SvIVX(dstr) = SvIVX(sstr); |
79072805 LW |
3476 | } |
3477 | } | |
8990e307 | 3478 | else if (sflags & SVp_IOK) { |
3332b3c1 JH |
3479 | if (sflags & SVf_IOK) |
3480 | (void)SvIOK_only(dstr); | |
3481 | else { | |
3482 | SvOK_off(dstr); | |
3483 | SvIOKp_on(dstr); | |
3484 | } | |
3485 | /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */ | |
2b1c7e3e | 3486 | if (sflags & SVf_IVisUV) |
25da4f38 | 3487 | SvIsUV_on(dstr); |
3332b3c1 JH |
3488 | SvIVX(dstr) = SvIVX(sstr); |
3489 | if (sflags & SVp_NOK) { | |
3490 | if (sflags & SVf_NOK) | |
3491 | (void)SvNOK_on(dstr); | |
3492 | else | |
3493 | (void)SvNOKp_on(dstr); | |
3494 | SvNVX(dstr) = SvNVX(sstr); | |
3495 | } | |
3496 | } | |
3497 | else if (sflags & SVp_NOK) { | |
3498 | if (sflags & SVf_NOK) | |
3499 | (void)SvNOK_only(dstr); | |
3500 | else { | |
3501 | SvOK_off(dstr); | |
3502 | SvNOKp_on(dstr); | |
3503 | } | |
3504 | SvNVX(dstr) = SvNVX(sstr); | |
79072805 LW |
3505 | } |
3506 | else { | |
20408e3c | 3507 | if (dtype == SVt_PVGV) { |
e476b1b5 GS |
3508 | if (ckWARN(WARN_MISC)) |
3509 | Perl_warner(aTHX_ WARN_MISC, "Undefined value assigned to typeglob"); | |
20408e3c GS |
3510 | } |
3511 | else | |
3512 | (void)SvOK_off(dstr); | |
a0d0e21e | 3513 | } |
27c9684d AP |
3514 | if (SvTAINTED(sstr)) |
3515 | SvTAINT(dstr); | |
79072805 LW |
3516 | } |
3517 | ||
954c1994 GS |
3518 | /* |
3519 | =for apidoc sv_setsv_mg | |
3520 | ||
3521 | Like C<sv_setsv>, but also handles 'set' magic. | |
3522 | ||
3523 | =cut | |
3524 | */ | |
3525 | ||
79072805 | 3526 | void |
864dbfa3 | 3527 | Perl_sv_setsv_mg(pTHX_ SV *dstr, register SV *sstr) |
ef50df4b GS |
3528 | { |
3529 | sv_setsv(dstr,sstr); | |
3530 | SvSETMAGIC(dstr); | |
3531 | } | |
3532 | ||
954c1994 GS |
3533 | /* |
3534 | =for apidoc sv_setpvn | |
3535 | ||
3536 | Copies a string into an SV. The C<len> parameter indicates the number of | |
3537 | bytes to be copied. Does not handle 'set' magic. See C<sv_setpvn_mg>. | |
3538 | ||
3539 | =cut | |
3540 | */ | |
3541 | ||
ef50df4b | 3542 | void |
864dbfa3 | 3543 | Perl_sv_setpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len) |
79072805 | 3544 | { |
c6f8c383 | 3545 | register char *dptr; |
22c522df | 3546 | |
2213622d | 3547 | SV_CHECK_THINKFIRST(sv); |
463ee0b2 | 3548 | if (!ptr) { |
a0d0e21e | 3549 | (void)SvOK_off(sv); |
463ee0b2 LW |
3550 | return; |
3551 | } | |
22c522df JH |
3552 | else { |
3553 | /* len is STRLEN which is unsigned, need to copy to signed */ | |
3554 | IV iv = len; | |
3555 | assert(iv >= 0); | |
3556 | } | |
6fc92669 | 3557 | (void)SvUPGRADE(sv, SVt_PV); |
c6f8c383 | 3558 | |
79072805 | 3559 | SvGROW(sv, len + 1); |
c6f8c383 GA |
3560 | dptr = SvPVX(sv); |
3561 | Move(ptr,dptr,len,char); | |
3562 | dptr[len] = '\0'; | |
79072805 | 3563 | SvCUR_set(sv, len); |
1aa99e6b | 3564 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 | 3565 | SvTAINT(sv); |
79072805 LW |
3566 | } |
3567 | ||
954c1994 GS |
3568 | /* |
3569 | =for apidoc sv_setpvn_mg | |
3570 | ||
3571 | Like C<sv_setpvn>, but also handles 'set' magic. | |
3572 | ||
3573 | =cut | |
3574 | */ | |
3575 | ||
79072805 | 3576 | void |
864dbfa3 | 3577 | Perl_sv_setpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len) |
ef50df4b GS |
3578 | { |
3579 | sv_setpvn(sv,ptr,len); | |
3580 | SvSETMAGIC(sv); | |
3581 | } | |
3582 | ||
954c1994 GS |
3583 | /* |
3584 | =for apidoc sv_setpv | |
3585 | ||
3586 | Copies a string into an SV. The string must be null-terminated. Does not | |
3587 | handle 'set' magic. See C<sv_setpv_mg>. | |
3588 | ||
3589 | =cut | |
3590 | */ | |
3591 | ||
ef50df4b | 3592 | void |
864dbfa3 | 3593 | Perl_sv_setpv(pTHX_ register SV *sv, register const char *ptr) |
79072805 LW |
3594 | { |
3595 | register STRLEN len; | |
3596 | ||
2213622d | 3597 | SV_CHECK_THINKFIRST(sv); |
463ee0b2 | 3598 | if (!ptr) { |
a0d0e21e | 3599 | (void)SvOK_off(sv); |
463ee0b2 LW |
3600 | return; |
3601 | } | |
79072805 | 3602 | len = strlen(ptr); |
6fc92669 | 3603 | (void)SvUPGRADE(sv, SVt_PV); |
c6f8c383 | 3604 | |
79072805 | 3605 | SvGROW(sv, len + 1); |
463ee0b2 | 3606 | Move(ptr,SvPVX(sv),len+1,char); |
79072805 | 3607 | SvCUR_set(sv, len); |
1aa99e6b | 3608 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 LW |
3609 | SvTAINT(sv); |
3610 | } | |
3611 | ||
954c1994 GS |
3612 | /* |
3613 | =for apidoc sv_setpv_mg | |
3614 | ||
3615 | Like C<sv_setpv>, but also handles 'set' magic. | |
3616 | ||
3617 | =cut | |
3618 | */ | |
3619 | ||
463ee0b2 | 3620 | void |
864dbfa3 | 3621 | Perl_sv_setpv_mg(pTHX_ register SV *sv, register const char *ptr) |
ef50df4b GS |
3622 | { |
3623 | sv_setpv(sv,ptr); | |
3624 | SvSETMAGIC(sv); | |
3625 | } | |
3626 | ||
954c1994 GS |
3627 | /* |
3628 | =for apidoc sv_usepvn | |
3629 | ||
3630 | Tells an SV to use C<ptr> to find its string value. Normally the string is | |
1c846c1f | 3631 | stored inside the SV but sv_usepvn allows the SV to use an outside string. |
954c1994 GS |
3632 | The C<ptr> should point to memory that was allocated by C<malloc>. The |
3633 | string length, C<len>, must be supplied. This function will realloc the | |
3634 | memory pointed to by C<ptr>, so that pointer should not be freed or used by | |
3635 | the programmer after giving it to sv_usepvn. Does not handle 'set' magic. | |
3636 | See C<sv_usepvn_mg>. | |
3637 | ||
3638 | =cut | |
3639 | */ | |
3640 | ||
ef50df4b | 3641 | void |
864dbfa3 | 3642 | Perl_sv_usepvn(pTHX_ register SV *sv, register char *ptr, register STRLEN len) |
463ee0b2 | 3643 | { |
2213622d | 3644 | SV_CHECK_THINKFIRST(sv); |
c6f8c383 | 3645 | (void)SvUPGRADE(sv, SVt_PV); |
463ee0b2 | 3646 | if (!ptr) { |
a0d0e21e | 3647 | (void)SvOK_off(sv); |
463ee0b2 LW |
3648 | return; |
3649 | } | |
a0ed51b3 | 3650 | (void)SvOOK_off(sv); |
50483b2c | 3651 | if (SvPVX(sv) && SvLEN(sv)) |
463ee0b2 LW |
3652 | Safefree(SvPVX(sv)); |
3653 | Renew(ptr, len+1, char); | |
3654 | SvPVX(sv) = ptr; | |
3655 | SvCUR_set(sv, len); | |
3656 | SvLEN_set(sv, len+1); | |
3657 | *SvEND(sv) = '\0'; | |
1aa99e6b | 3658 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 | 3659 | SvTAINT(sv); |
79072805 LW |
3660 | } |
3661 | ||
954c1994 GS |
3662 | /* |
3663 | =for apidoc sv_usepvn_mg | |
3664 | ||
3665 | Like C<sv_usepvn>, but also handles 'set' magic. | |
3666 | ||
3667 | =cut | |
3668 | */ | |
3669 | ||
ef50df4b | 3670 | void |
864dbfa3 | 3671 | Perl_sv_usepvn_mg(pTHX_ register SV *sv, register char *ptr, register STRLEN len) |
ef50df4b | 3672 | { |
51c1089b | 3673 | sv_usepvn(sv,ptr,len); |
ef50df4b GS |
3674 | SvSETMAGIC(sv); |
3675 | } | |
3676 | ||
6fc92669 | 3677 | void |
840a7b70 | 3678 | Perl_sv_force_normal_flags(pTHX_ register SV *sv, U32 flags) |
0f15f207 | 3679 | { |
2213622d | 3680 | if (SvREADONLY(sv)) { |
1c846c1f NIS |
3681 | if (SvFAKE(sv)) { |
3682 | char *pvx = SvPVX(sv); | |
3683 | STRLEN len = SvCUR(sv); | |
3684 | U32 hash = SvUVX(sv); | |
3685 | SvGROW(sv, len + 1); | |
3686 | Move(pvx,SvPVX(sv),len,char); | |
3687 | *SvEND(sv) = '\0'; | |
3688 | SvFAKE_off(sv); | |
3689 | SvREADONLY_off(sv); | |
c3654f1a | 3690 | unsharepvn(pvx,SvUTF8(sv)?-len:len,hash); |
1c846c1f NIS |
3691 | } |
3692 | else if (PL_curcop != &PL_compiling) | |
cea2e8a9 | 3693 | Perl_croak(aTHX_ PL_no_modify); |
0f15f207 | 3694 | } |
2213622d | 3695 | if (SvROK(sv)) |
840a7b70 | 3696 | sv_unref_flags(sv, flags); |
6fc92669 GS |
3697 | else if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV) |
3698 | sv_unglob(sv); | |
0f15f207 | 3699 | } |
1c846c1f | 3700 | |
840a7b70 IZ |
3701 | void |
3702 | Perl_sv_force_normal(pTHX_ register SV *sv) | |
3703 | { | |
3704 | sv_force_normal_flags(sv, 0); | |
3705 | } | |
3706 | ||
954c1994 GS |
3707 | /* |
3708 | =for apidoc sv_chop | |
3709 | ||
1c846c1f | 3710 | Efficient removal of characters from the beginning of the string buffer. |
954c1994 GS |
3711 | SvPOK(sv) must be true and the C<ptr> must be a pointer to somewhere inside |
3712 | the string buffer. The C<ptr> becomes the first character of the adjusted | |
3713 | string. | |
3714 | ||
3715 | =cut | |
3716 | */ | |
3717 | ||
79072805 | 3718 | void |
864dbfa3 | 3719 | Perl_sv_chop(pTHX_ register SV *sv, register char *ptr) /* like set but assuming ptr is in sv */ |
1c846c1f NIS |
3720 | |
3721 | ||
79072805 LW |
3722 | { |
3723 | register STRLEN delta; | |
3724 | ||
a0d0e21e | 3725 | if (!ptr || !SvPOKp(sv)) |
79072805 | 3726 | return; |
2213622d | 3727 | SV_CHECK_THINKFIRST(sv); |
79072805 LW |
3728 | if (SvTYPE(sv) < SVt_PVIV) |
3729 | sv_upgrade(sv,SVt_PVIV); | |
3730 | ||
3731 | if (!SvOOK(sv)) { | |
50483b2c JD |
3732 | if (!SvLEN(sv)) { /* make copy of shared string */ |
3733 | char *pvx = SvPVX(sv); | |
3734 | STRLEN len = SvCUR(sv); | |
3735 | SvGROW(sv, len + 1); | |
3736 | Move(pvx,SvPVX(sv),len,char); | |
3737 | *SvEND(sv) = '\0'; | |
3738 | } | |
463ee0b2 | 3739 | SvIVX(sv) = 0; |
79072805 LW |
3740 | SvFLAGS(sv) |= SVf_OOK; |
3741 | } | |
25da4f38 | 3742 | SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVp_IOK|SVp_NOK|SVf_IVisUV); |
463ee0b2 | 3743 | delta = ptr - SvPVX(sv); |
79072805 LW |
3744 | SvLEN(sv) -= delta; |
3745 | SvCUR(sv) -= delta; | |
463ee0b2 LW |
3746 | SvPVX(sv) += delta; |
3747 | SvIVX(sv) += delta; | |
79072805 LW |
3748 | } |
3749 | ||
954c1994 GS |
3750 | /* |
3751 | =for apidoc sv_catpvn | |
3752 | ||
3753 | Concatenates the string onto the end of the string which is in the SV. The | |
3754 | C<len> indicates number of bytes to copy. Handles 'get' magic, but not | |
3755 | 'set' magic. See C<sv_catpvn_mg>. | |
3756 | ||
3757 | =cut | |
3758 | */ | |
3759 | ||
79072805 | 3760 | void |
864dbfa3 | 3761 | Perl_sv_catpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len) |
79072805 | 3762 | { |
463ee0b2 | 3763 | STRLEN tlen; |
748a9306 | 3764 | char *junk; |
a0d0e21e | 3765 | |
748a9306 | 3766 | junk = SvPV_force(sv, tlen); |
463ee0b2 | 3767 | SvGROW(sv, tlen + len + 1); |
4633a7c4 LW |
3768 | if (ptr == junk) |
3769 | ptr = SvPVX(sv); | |
463ee0b2 | 3770 | Move(ptr,SvPVX(sv)+tlen,len,char); |
79072805 LW |
3771 | SvCUR(sv) += len; |
3772 | *SvEND(sv) = '\0'; | |
d41ff1b8 | 3773 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 | 3774 | SvTAINT(sv); |
79072805 LW |
3775 | } |
3776 | ||
954c1994 GS |
3777 | /* |
3778 | =for apidoc sv_catpvn_mg | |
3779 | ||
3780 | Like C<sv_catpvn>, but also handles 'set' magic. | |
3781 | ||
3782 | =cut | |
3783 | */ | |
3784 | ||
79072805 | 3785 | void |
864dbfa3 | 3786 | Perl_sv_catpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len) |
ef50df4b GS |
3787 | { |
3788 | sv_catpvn(sv,ptr,len); | |
3789 | SvSETMAGIC(sv); | |
3790 | } | |
3791 | ||
954c1994 GS |
3792 | /* |
3793 | =for apidoc sv_catsv | |
3794 | ||
13e8c8e3 JH |
3795 | Concatenates the string from SV C<ssv> onto the end of the string in |
3796 | SV C<dsv>. Modifies C<dsv> but not C<ssv>. Handles 'get' magic, but | |
3797 | not 'set' magic. See C<sv_catsv_mg>. | |
954c1994 | 3798 | |
13e8c8e3 | 3799 | =cut */ |
954c1994 | 3800 | |
ef50df4b | 3801 | void |
46199a12 | 3802 | Perl_sv_catsv(pTHX_ SV *dsv, register SV *ssv) |
79072805 | 3803 | { |
13e8c8e3 JH |
3804 | char *spv; |
3805 | STRLEN slen; | |
46199a12 | 3806 | if (!ssv) |
79072805 | 3807 | return; |
46199a12 JH |
3808 | if ((spv = SvPV(ssv, slen))) { |
3809 | bool dutf8 = DO_UTF8(dsv); | |
3810 | bool sutf8 = DO_UTF8(ssv); | |
13e8c8e3 JH |
3811 | |
3812 | if (dutf8 == sutf8) | |
46199a12 | 3813 | sv_catpvn(dsv,spv,slen); |
13e8c8e3 JH |
3814 | else { |
3815 | if (dutf8) { | |
46199a12 JH |
3816 | /* Not modifying source SV, so taking a temporary copy. */ |
3817 | SV* csv = sv_2mortal(newSVsv(ssv)); | |
13e8c8e3 JH |
3818 | char *cpv; |
3819 | STRLEN clen; | |
3820 | ||
46199a12 JH |
3821 | sv_utf8_upgrade(csv); |
3822 | cpv = SvPV(csv,clen); | |
3823 | sv_catpvn(dsv,cpv,clen); | |
13e8c8e3 JH |
3824 | } |
3825 | else { | |
46199a12 JH |
3826 | sv_utf8_upgrade(dsv); |
3827 | sv_catpvn(dsv,spv,slen); | |
3828 | SvUTF8_on(dsv); /* If dsv has no wide characters. */ | |
13e8c8e3 | 3829 | } |
e84ff256 | 3830 | } |
560a288e | 3831 | } |
79072805 LW |
3832 | } |
3833 | ||
954c1994 GS |
3834 | /* |
3835 | =for apidoc sv_catsv_mg | |
3836 | ||
3837 | Like C<sv_catsv>, but also handles 'set' magic. | |
3838 | ||
3839 | =cut | |
3840 | */ | |
3841 | ||
79072805 | 3842 | void |
46199a12 | 3843 | Perl_sv_catsv_mg(pTHX_ SV *dsv, register SV *ssv) |
ef50df4b | 3844 | { |
46199a12 JH |
3845 | sv_catsv(dsv,ssv); |
3846 | SvSETMAGIC(dsv); | |
ef50df4b GS |
3847 | } |
3848 | ||
954c1994 GS |
3849 | /* |
3850 | =for apidoc sv_catpv | |
3851 | ||
3852 | Concatenates the string onto the end of the string which is in the SV. | |
3853 | Handles 'get' magic, but not 'set' magic. See C<sv_catpv_mg>. | |
3854 | ||
3855 | =cut | |
3856 | */ | |
3857 | ||
ef50df4b | 3858 | void |
0c981600 | 3859 | Perl_sv_catpv(pTHX_ register SV *sv, register const char *ptr) |
79072805 LW |
3860 | { |
3861 | register STRLEN len; | |
463ee0b2 | 3862 | STRLEN tlen; |
748a9306 | 3863 | char *junk; |
79072805 | 3864 | |
0c981600 | 3865 | if (!ptr) |
79072805 | 3866 | return; |
748a9306 | 3867 | junk = SvPV_force(sv, tlen); |
0c981600 | 3868 | len = strlen(ptr); |
463ee0b2 | 3869 | SvGROW(sv, tlen + len + 1); |
0c981600 JH |
3870 | if (ptr == junk) |
3871 | ptr = SvPVX(sv); | |
3872 | Move(ptr,SvPVX(sv)+tlen,len+1,char); | |
79072805 | 3873 | SvCUR(sv) += len; |
d41ff1b8 | 3874 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 | 3875 | SvTAINT(sv); |
79072805 LW |
3876 | } |
3877 | ||
954c1994 GS |
3878 | /* |
3879 | =for apidoc sv_catpv_mg | |
3880 | ||
3881 | Like C<sv_catpv>, but also handles 'set' magic. | |
3882 | ||
3883 | =cut | |
3884 | */ | |
3885 | ||
ef50df4b | 3886 | void |
0c981600 | 3887 | Perl_sv_catpv_mg(pTHX_ register SV *sv, register const char *ptr) |
ef50df4b | 3888 | { |
0c981600 | 3889 | sv_catpv(sv,ptr); |
ef50df4b GS |
3890 | SvSETMAGIC(sv); |
3891 | } | |
3892 | ||
79072805 | 3893 | SV * |
864dbfa3 | 3894 | Perl_newSV(pTHX_ STRLEN len) |
79072805 LW |
3895 | { |
3896 | register SV *sv; | |
1c846c1f | 3897 | |
4561caa4 | 3898 | new_SV(sv); |
79072805 LW |
3899 | if (len) { |
3900 | sv_upgrade(sv, SVt_PV); | |
3901 | SvGROW(sv, len + 1); | |
3902 | } | |
3903 | return sv; | |
3904 | } | |
3905 | ||
1edc1566 | 3906 | /* name is assumed to contain an SV* if (name && namelen == HEf_SVKEY) */ |
3907 | ||
954c1994 GS |
3908 | /* |
3909 | =for apidoc sv_magic | |
3910 | ||
3911 | Adds magic to an SV. | |
3912 | ||
3913 | =cut | |
3914 | */ | |
3915 | ||
79072805 | 3916 | void |
864dbfa3 | 3917 | Perl_sv_magic(pTHX_ register SV *sv, SV *obj, int how, const char *name, I32 namlen) |
79072805 LW |
3918 | { |
3919 | MAGIC* mg; | |
1c846c1f | 3920 | |
0f15f207 | 3921 | if (SvREADONLY(sv)) { |
3280af22 | 3922 | if (PL_curcop != &PL_compiling && !strchr("gBf", how)) |
cea2e8a9 | 3923 | Perl_croak(aTHX_ PL_no_modify); |
0f15f207 | 3924 | } |
4633a7c4 | 3925 | if (SvMAGICAL(sv) || (how == 't' && SvTYPE(sv) >= SVt_PVMG)) { |
748a9306 LW |
3926 | if (SvMAGIC(sv) && (mg = mg_find(sv, how))) { |
3927 | if (how == 't') | |
565764a8 | 3928 | mg->mg_len |= 1; |
463ee0b2 | 3929 | return; |
748a9306 | 3930 | } |
463ee0b2 LW |
3931 | } |
3932 | else { | |
c6f8c383 | 3933 | (void)SvUPGRADE(sv, SVt_PVMG); |
463ee0b2 | 3934 | } |
79072805 LW |
3935 | Newz(702,mg, 1, MAGIC); |
3936 | mg->mg_moremagic = SvMAGIC(sv); | |
463ee0b2 | 3937 | |
79072805 | 3938 | SvMAGIC(sv) = mg; |
c277df42 | 3939 | if (!obj || obj == sv || how == '#' || how == 'r') |
8990e307 | 3940 | mg->mg_obj = obj; |
85e6fe83 | 3941 | else { |
8990e307 | 3942 | mg->mg_obj = SvREFCNT_inc(obj); |
85e6fe83 LW |
3943 | mg->mg_flags |= MGf_REFCOUNTED; |
3944 | } | |
79072805 | 3945 | mg->mg_type = how; |
565764a8 | 3946 | mg->mg_len = namlen; |
1edc1566 | 3947 | if (name) |
3948 | if (namlen >= 0) | |
3949 | mg->mg_ptr = savepvn(name, namlen); | |
c6ee37c5 | 3950 | else if (namlen == HEf_SVKEY) |
1edc1566 | 3951 | mg->mg_ptr = (char*)SvREFCNT_inc((SV*)name); |
1c846c1f | 3952 | |
79072805 LW |
3953 | switch (how) { |
3954 | case 0: | |
22c35a8c | 3955 | mg->mg_virtual = &PL_vtbl_sv; |
79072805 | 3956 | break; |
a0d0e21e | 3957 | case 'A': |
22c35a8c | 3958 | mg->mg_virtual = &PL_vtbl_amagic; |
a0d0e21e LW |
3959 | break; |
3960 | case 'a': | |
22c35a8c | 3961 | mg->mg_virtual = &PL_vtbl_amagicelem; |
a0d0e21e LW |
3962 | break; |
3963 | case 'c': | |
d460ef45 | 3964 | mg->mg_virtual = &PL_vtbl_ovrld; |
a0d0e21e | 3965 | break; |
79072805 | 3966 | case 'B': |
22c35a8c | 3967 | mg->mg_virtual = &PL_vtbl_bm; |
79072805 | 3968 | break; |
6cef1e77 | 3969 | case 'D': |
22c35a8c | 3970 | mg->mg_virtual = &PL_vtbl_regdata; |
6cef1e77 IZ |
3971 | break; |
3972 | case 'd': | |
22c35a8c | 3973 | mg->mg_virtual = &PL_vtbl_regdatum; |
6cef1e77 | 3974 | break; |
79072805 | 3975 | case 'E': |
22c35a8c | 3976 | mg->mg_virtual = &PL_vtbl_env; |
79072805 | 3977 | break; |
55497cff | 3978 | case 'f': |
22c35a8c | 3979 | mg->mg_virtual = &PL_vtbl_fm; |
55497cff | 3980 | break; |
79072805 | 3981 | case 'e': |
22c35a8c | 3982 | mg->mg_virtual = &PL_vtbl_envelem; |
79072805 | 3983 | break; |
93a17b20 | 3984 | case 'g': |
22c35a8c | 3985 | mg->mg_virtual = &PL_vtbl_mglob; |
93a17b20 | 3986 | break; |
463ee0b2 | 3987 | case 'I': |
22c35a8c | 3988 | mg->mg_virtual = &PL_vtbl_isa; |
463ee0b2 LW |
3989 | break; |
3990 | case 'i': | |
22c35a8c | 3991 | mg->mg_virtual = &PL_vtbl_isaelem; |
463ee0b2 | 3992 | break; |
16660edb | 3993 | case 'k': |
22c35a8c | 3994 | mg->mg_virtual = &PL_vtbl_nkeys; |
16660edb | 3995 | break; |
79072805 | 3996 | case 'L': |
a0d0e21e | 3997 | SvRMAGICAL_on(sv); |
93a17b20 LW |
3998 | mg->mg_virtual = 0; |
3999 | break; | |
4000 | case 'l': | |
22c35a8c | 4001 | mg->mg_virtual = &PL_vtbl_dbline; |
79072805 | 4002 | break; |
f93b4edd MB |
4003 | #ifdef USE_THREADS |
4004 | case 'm': | |
22c35a8c | 4005 | mg->mg_virtual = &PL_vtbl_mutex; |
f93b4edd MB |
4006 | break; |
4007 | #endif /* USE_THREADS */ | |
36477c24 | 4008 | #ifdef USE_LOCALE_COLLATE |
bbce6d69 | 4009 | case 'o': |
22c35a8c | 4010 | mg->mg_virtual = &PL_vtbl_collxfrm; |
bbce6d69 | 4011 | break; |
36477c24 | 4012 | #endif /* USE_LOCALE_COLLATE */ |
463ee0b2 | 4013 | case 'P': |
22c35a8c | 4014 | mg->mg_virtual = &PL_vtbl_pack; |
463ee0b2 LW |
4015 | break; |
4016 | case 'p': | |
a0d0e21e | 4017 | case 'q': |
22c35a8c | 4018 | mg->mg_virtual = &PL_vtbl_packelem; |
463ee0b2 | 4019 | break; |
c277df42 | 4020 | case 'r': |
22c35a8c | 4021 | mg->mg_virtual = &PL_vtbl_regexp; |
c277df42 | 4022 | break; |
79072805 | 4023 | case 'S': |
22c35a8c | 4024 | mg->mg_virtual = &PL_vtbl_sig; |
79072805 LW |
4025 | break; |
4026 | case 's': | |
22c35a8c | 4027 | mg->mg_virtual = &PL_vtbl_sigelem; |
79072805 | 4028 | break; |
463ee0b2 | 4029 | case 't': |
22c35a8c | 4030 | mg->mg_virtual = &PL_vtbl_taint; |
565764a8 | 4031 | mg->mg_len = 1; |
463ee0b2 | 4032 | break; |
79072805 | 4033 | case 'U': |
22c35a8c | 4034 | mg->mg_virtual = &PL_vtbl_uvar; |
79072805 LW |
4035 | break; |
4036 | case 'v': | |
22c35a8c | 4037 | mg->mg_virtual = &PL_vtbl_vec; |
79072805 LW |
4038 | break; |
4039 | case 'x': | |
22c35a8c | 4040 | mg->mg_virtual = &PL_vtbl_substr; |
79072805 | 4041 | break; |
5f05dabc | 4042 | case 'y': |
22c35a8c | 4043 | mg->mg_virtual = &PL_vtbl_defelem; |
5f05dabc | 4044 | break; |
79072805 | 4045 | case '*': |
22c35a8c | 4046 | mg->mg_virtual = &PL_vtbl_glob; |
79072805 LW |
4047 | break; |
4048 | case '#': | |
22c35a8c | 4049 | mg->mg_virtual = &PL_vtbl_arylen; |
79072805 | 4050 | break; |
a0d0e21e | 4051 | case '.': |
22c35a8c | 4052 | mg->mg_virtual = &PL_vtbl_pos; |
a0d0e21e | 4053 | break; |
810b8aa5 GS |
4054 | case '<': |
4055 | mg->mg_virtual = &PL_vtbl_backref; | |
4056 | break; | |
4633a7c4 LW |
4057 | case '~': /* Reserved for use by extensions not perl internals. */ |
4058 | /* Useful for attaching extension internal data to perl vars. */ | |
4059 | /* Note that multiple extensions may clash if magical scalars */ | |
4060 | /* etc holding private data from one are passed to another. */ | |
4061 | SvRMAGICAL_on(sv); | |
a0d0e21e | 4062 | break; |
79072805 | 4063 | default: |
cea2e8a9 | 4064 | Perl_croak(aTHX_ "Don't know how to handle magic of type '%c'", how); |
463ee0b2 | 4065 | } |
8990e307 LW |
4066 | mg_magical(sv); |
4067 | if (SvGMAGICAL(sv)) | |
4068 | SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK); | |
463ee0b2 LW |
4069 | } |
4070 | ||
c461cf8f JH |
4071 | /* |
4072 | =for apidoc sv_unmagic | |
4073 | ||
4074 | Removes magic from an SV. | |
4075 | ||
4076 | =cut | |
4077 | */ | |
4078 | ||
463ee0b2 | 4079 | int |
864dbfa3 | 4080 | Perl_sv_unmagic(pTHX_ SV *sv, int type) |
463ee0b2 LW |
4081 | { |
4082 | MAGIC* mg; | |
4083 | MAGIC** mgp; | |
91bba347 | 4084 | if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv)) |
463ee0b2 LW |
4085 | return 0; |
4086 | mgp = &SvMAGIC(sv); | |
4087 | for (mg = *mgp; mg; mg = *mgp) { | |
4088 | if (mg->mg_type == type) { | |
4089 | MGVTBL* vtbl = mg->mg_virtual; | |
4090 | *mgp = mg->mg_moremagic; | |
1d7c1841 | 4091 | if (vtbl && vtbl->svt_free) |
fc0dc3b3 | 4092 | CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg); |
463ee0b2 | 4093 | if (mg->mg_ptr && mg->mg_type != 'g') |
565764a8 | 4094 | if (mg->mg_len >= 0) |
1edc1566 | 4095 | Safefree(mg->mg_ptr); |
565764a8 | 4096 | else if (mg->mg_len == HEf_SVKEY) |
1edc1566 | 4097 | SvREFCNT_dec((SV*)mg->mg_ptr); |
a0d0e21e LW |
4098 | if (mg->mg_flags & MGf_REFCOUNTED) |
4099 | SvREFCNT_dec(mg->mg_obj); | |
463ee0b2 LW |
4100 | Safefree(mg); |
4101 | } | |
4102 | else | |
4103 | mgp = &mg->mg_moremagic; | |
79072805 | 4104 | } |
91bba347 | 4105 | if (!SvMAGIC(sv)) { |
463ee0b2 | 4106 | SvMAGICAL_off(sv); |
8990e307 | 4107 | SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT; |
463ee0b2 LW |
4108 | } |
4109 | ||
4110 | return 0; | |
79072805 LW |
4111 | } |
4112 | ||
c461cf8f JH |
4113 | /* |
4114 | =for apidoc sv_rvweaken | |
4115 | ||
4116 | Weaken a reference. | |
4117 | ||
4118 | =cut | |
4119 | */ | |
4120 | ||
810b8aa5 | 4121 | SV * |
864dbfa3 | 4122 | Perl_sv_rvweaken(pTHX_ SV *sv) |
810b8aa5 GS |
4123 | { |
4124 | SV *tsv; | |
4125 | if (!SvOK(sv)) /* let undefs pass */ | |
4126 | return sv; | |
4127 | if (!SvROK(sv)) | |
cea2e8a9 | 4128 | Perl_croak(aTHX_ "Can't weaken a nonreference"); |
810b8aa5 | 4129 | else if (SvWEAKREF(sv)) { |
810b8aa5 | 4130 | if (ckWARN(WARN_MISC)) |
cea2e8a9 | 4131 | Perl_warner(aTHX_ WARN_MISC, "Reference is already weak"); |
810b8aa5 GS |
4132 | return sv; |
4133 | } | |
4134 | tsv = SvRV(sv); | |
4135 | sv_add_backref(tsv, sv); | |
4136 | SvWEAKREF_on(sv); | |
1c846c1f | 4137 | SvREFCNT_dec(tsv); |
810b8aa5 GS |
4138 | return sv; |
4139 | } | |
4140 | ||
4141 | STATIC void | |
cea2e8a9 | 4142 | S_sv_add_backref(pTHX_ SV *tsv, SV *sv) |
810b8aa5 GS |
4143 | { |
4144 | AV *av; | |
4145 | MAGIC *mg; | |
4146 | if (SvMAGICAL(tsv) && (mg = mg_find(tsv, '<'))) | |
4147 | av = (AV*)mg->mg_obj; | |
4148 | else { | |
4149 | av = newAV(); | |
4150 | sv_magic(tsv, (SV*)av, '<', NULL, 0); | |
4151 | SvREFCNT_dec(av); /* for sv_magic */ | |
4152 | } | |
4153 | av_push(av,sv); | |
4154 | } | |
4155 | ||
1c846c1f | 4156 | STATIC void |
cea2e8a9 | 4157 | S_sv_del_backref(pTHX_ SV *sv) |
810b8aa5 GS |
4158 | { |
4159 | AV *av; | |
4160 | SV **svp; | |
4161 | I32 i; | |
4162 | SV *tsv = SvRV(sv); | |
4163 | MAGIC *mg; | |
4164 | if (!SvMAGICAL(tsv) || !(mg = mg_find(tsv, '<'))) | |
cea2e8a9 | 4165 | Perl_croak(aTHX_ "panic: del_backref"); |
810b8aa5 GS |
4166 | av = (AV *)mg->mg_obj; |
4167 | svp = AvARRAY(av); | |
4168 | i = AvFILLp(av); | |
4169 | while (i >= 0) { | |
4170 | if (svp[i] == sv) { | |
4171 | svp[i] = &PL_sv_undef; /* XXX */ | |
4172 | } | |
4173 | i--; | |
4174 | } | |
4175 | } | |
4176 | ||
954c1994 GS |
4177 | /* |
4178 | =for apidoc sv_insert | |
4179 | ||
4180 | Inserts a string at the specified offset/length within the SV. Similar to | |
4181 | the Perl substr() function. | |
4182 | ||
4183 | =cut | |
4184 | */ | |
4185 | ||
79072805 | 4186 | void |
864dbfa3 | 4187 | Perl_sv_insert(pTHX_ SV *bigstr, STRLEN offset, STRLEN len, char *little, STRLEN littlelen) |
79072805 LW |
4188 | { |
4189 | register char *big; | |
4190 | register char *mid; | |
4191 | register char *midend; | |
4192 | register char *bigend; | |
4193 | register I32 i; | |
6ff81951 | 4194 | STRLEN curlen; |
1c846c1f | 4195 | |
79072805 | 4196 | |
8990e307 | 4197 | if (!bigstr) |
cea2e8a9 | 4198 | Perl_croak(aTHX_ "Can't modify non-existent substring"); |
6ff81951 | 4199 | SvPV_force(bigstr, curlen); |
60fa28ff | 4200 | (void)SvPOK_only_UTF8(bigstr); |
6ff81951 GS |
4201 | if (offset + len > curlen) { |
4202 | SvGROW(bigstr, offset+len+1); | |
4203 | Zero(SvPVX(bigstr)+curlen, offset+len-curlen, char); | |
4204 | SvCUR_set(bigstr, offset+len); | |
4205 | } | |
79072805 | 4206 | |
69b47968 | 4207 | SvTAINT(bigstr); |
79072805 LW |
4208 | i = littlelen - len; |
4209 | if (i > 0) { /* string might grow */ | |
a0d0e21e | 4210 | big = SvGROW(bigstr, SvCUR(bigstr) + i + 1); |
79072805 LW |
4211 | mid = big + offset + len; |
4212 | midend = bigend = big + SvCUR(bigstr); | |
4213 | bigend += i; | |
4214 | *bigend = '\0'; | |
4215 | while (midend > mid) /* shove everything down */ | |
4216 | *--bigend = *--midend; | |
4217 | Move(little,big+offset,littlelen,char); | |
4218 | SvCUR(bigstr) += i; | |
4219 | SvSETMAGIC(bigstr); | |
4220 | return; | |
4221 | } | |
4222 | else if (i == 0) { | |
463ee0b2 | 4223 | Move(little,SvPVX(bigstr)+offset,len,char); |
79072805 LW |
4224 | SvSETMAGIC(bigstr); |
4225 | return; | |
4226 | } | |
4227 | ||
463ee0b2 | 4228 | big = SvPVX(bigstr); |
79072805 LW |
4229 | mid = big + offset; |
4230 | midend = mid + len; | |
4231 | bigend = big + SvCUR(bigstr); | |
4232 | ||
4233 | if (midend > bigend) | |
cea2e8a9 | 4234 | Perl_croak(aTHX_ "panic: sv_insert"); |
79072805 LW |
4235 | |
4236 | if (mid - big > bigend - midend) { /* faster to shorten from end */ | |
4237 | if (littlelen) { | |
4238 | Move(little, mid, littlelen,char); | |
4239 | mid += littlelen; | |
4240 | } | |
4241 | i = bigend - midend; | |
4242 | if (i > 0) { | |
4243 | Move(midend, mid, i,char); | |
4244 | mid += i; | |
4245 | } | |
4246 | *mid = '\0'; | |
4247 | SvCUR_set(bigstr, mid - big); | |
4248 | } | |
4249 | /*SUPPRESS 560*/ | |
155aba94 | 4250 | else if ((i = mid - big)) { /* faster from front */ |
79072805 LW |
4251 | midend -= littlelen; |
4252 | mid = midend; | |
4253 | sv_chop(bigstr,midend-i); | |
4254 | big += i; | |
4255 | while (i--) | |
4256 | *--midend = *--big; | |
4257 | if (littlelen) | |
4258 | Move(little, mid, littlelen,char); | |
4259 | } | |
4260 | else if (littlelen) { | |
4261 | midend -= littlelen; | |
4262 | sv_chop(bigstr,midend); | |
4263 | Move(little,midend,littlelen,char); | |
4264 | } | |
4265 | else { | |
4266 | sv_chop(bigstr,midend); | |
4267 | } | |
4268 | SvSETMAGIC(bigstr); | |
4269 | } | |
4270 | ||
c461cf8f JH |
4271 | /* |
4272 | =for apidoc sv_replace | |
4273 | ||
4274 | Make the first argument a copy of the second, then delete the original. | |
4275 | ||
4276 | =cut | |
4277 | */ | |
79072805 LW |
4278 | |
4279 | void | |
864dbfa3 | 4280 | Perl_sv_replace(pTHX_ register SV *sv, register SV *nsv) |
79072805 LW |
4281 | { |
4282 | U32 refcnt = SvREFCNT(sv); | |
2213622d | 4283 | SV_CHECK_THINKFIRST(sv); |
0453d815 PM |
4284 | if (SvREFCNT(nsv) != 1 && ckWARN_d(WARN_INTERNAL)) |
4285 | Perl_warner(aTHX_ WARN_INTERNAL, "Reference miscount in sv_replace()"); | |
93a17b20 | 4286 | if (SvMAGICAL(sv)) { |
a0d0e21e LW |
4287 | if (SvMAGICAL(nsv)) |
4288 | mg_free(nsv); | |
4289 | else | |
4290 | sv_upgrade(nsv, SVt_PVMG); | |
93a17b20 | 4291 | SvMAGIC(nsv) = SvMAGIC(sv); |
a0d0e21e | 4292 | SvFLAGS(nsv) |= SvMAGICAL(sv); |
93a17b20 LW |
4293 | SvMAGICAL_off(sv); |
4294 | SvMAGIC(sv) = 0; | |
4295 | } | |
79072805 LW |
4296 | SvREFCNT(sv) = 0; |
4297 | sv_clear(sv); | |
477f5d66 | 4298 | assert(!SvREFCNT(sv)); |
79072805 LW |
4299 | StructCopy(nsv,sv,SV); |
4300 | SvREFCNT(sv) = refcnt; | |
1edc1566 | 4301 | SvFLAGS(nsv) |= SVTYPEMASK; /* Mark as freed */ |
463ee0b2 | 4302 | del_SV(nsv); |
79072805 LW |
4303 | } |
4304 | ||
c461cf8f JH |
4305 | /* |
4306 | =for apidoc sv_clear | |
4307 | ||
4308 | Clear an SV, making it empty. Does not free the memory used by the SV | |
4309 | itself. | |
4310 | ||
4311 | =cut | |
4312 | */ | |
4313 | ||
79072805 | 4314 | void |
864dbfa3 | 4315 | Perl_sv_clear(pTHX_ register SV *sv) |
79072805 | 4316 | { |
ec12f114 | 4317 | HV* stash; |
79072805 LW |
4318 | assert(sv); |
4319 | assert(SvREFCNT(sv) == 0); | |
4320 | ||
ed6116ce | 4321 | if (SvOBJECT(sv)) { |
3280af22 | 4322 | if (PL_defstash) { /* Still have a symbol table? */ |
4e35701f | 4323 | djSP; |
32251b26 | 4324 | CV* destructor; |
837485b6 | 4325 | SV tmpref; |
a0d0e21e | 4326 | |
837485b6 GS |
4327 | Zero(&tmpref, 1, SV); |
4328 | sv_upgrade(&tmpref, SVt_RV); | |
4329 | SvROK_on(&tmpref); | |
4330 | SvREADONLY_on(&tmpref); /* DESTROY() could be naughty */ | |
4331 | SvREFCNT(&tmpref) = 1; | |
8ebc5c01 | 4332 | |
d460ef45 | 4333 | do { |
4e8e7886 | 4334 | stash = SvSTASH(sv); |
32251b26 | 4335 | destructor = StashHANDLER(stash,DESTROY); |
4e8e7886 GS |
4336 | if (destructor) { |
4337 | ENTER; | |
e788e7d3 | 4338 | PUSHSTACKi(PERLSI_DESTROY); |
837485b6 | 4339 | SvRV(&tmpref) = SvREFCNT_inc(sv); |
4e8e7886 GS |
4340 | EXTEND(SP, 2); |
4341 | PUSHMARK(SP); | |
837485b6 | 4342 | PUSHs(&tmpref); |
4e8e7886 | 4343 | PUTBACK; |
32251b26 | 4344 | call_sv((SV*)destructor, G_DISCARD|G_EVAL|G_KEEPERR); |
4e8e7886 | 4345 | SvREFCNT(sv)--; |
d3acc0f7 | 4346 | POPSTACK; |
3095d977 | 4347 | SPAGAIN; |
4e8e7886 GS |
4348 | LEAVE; |
4349 | } | |
4350 | } while (SvOBJECT(sv) && SvSTASH(sv) != stash); | |
8ebc5c01 | 4351 | |
837485b6 | 4352 | del_XRV(SvANY(&tmpref)); |
6f44e0a4 JP |
4353 | |
4354 | if (SvREFCNT(sv)) { | |
4355 | if (PL_in_clean_objs) | |
cea2e8a9 | 4356 | Perl_croak(aTHX_ "DESTROY created new reference to dead object '%s'", |
6f44e0a4 JP |
4357 | HvNAME(stash)); |
4358 | /* DESTROY gave object new lease on life */ | |
4359 | return; | |
4360 | } | |
a0d0e21e | 4361 | } |
4e8e7886 | 4362 | |
a0d0e21e | 4363 | if (SvOBJECT(sv)) { |
4e8e7886 | 4364 | SvREFCNT_dec(SvSTASH(sv)); /* possibly of changed persuasion */ |
a0d0e21e LW |
4365 | SvOBJECT_off(sv); /* Curse the object. */ |
4366 | if (SvTYPE(sv) != SVt_PVIO) | |
3280af22 | 4367 | --PL_sv_objcount; /* XXX Might want something more general */ |
a0d0e21e | 4368 | } |
463ee0b2 | 4369 | } |
c07a80fd | 4370 | if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) |
a0d0e21e | 4371 | mg_free(sv); |
ec12f114 | 4372 | stash = NULL; |
79072805 | 4373 | switch (SvTYPE(sv)) { |
8990e307 | 4374 | case SVt_PVIO: |
df0bd2f4 GS |
4375 | if (IoIFP(sv) && |
4376 | IoIFP(sv) != PerlIO_stdin() && | |
5f05dabc | 4377 | IoIFP(sv) != PerlIO_stdout() && |
4378 | IoIFP(sv) != PerlIO_stderr()) | |
93578b34 | 4379 | { |
f2b5be74 | 4380 | io_close((IO*)sv, FALSE); |
93578b34 | 4381 | } |
1d7c1841 | 4382 | if (IoDIRP(sv) && !(IoFLAGS(sv) & IOf_FAKE_DIRP)) |
1236053a | 4383 | PerlDir_close(IoDIRP(sv)); |
1d7c1841 | 4384 | IoDIRP(sv) = (DIR*)NULL; |
8990e307 LW |
4385 | Safefree(IoTOP_NAME(sv)); |
4386 | Safefree(IoFMT_NAME(sv)); | |
4387 | Safefree(IoBOTTOM_NAME(sv)); | |
a0d0e21e | 4388 | /* FALL THROUGH */ |
79072805 | 4389 | case SVt_PVBM: |
a0d0e21e | 4390 | goto freescalar; |
79072805 | 4391 | case SVt_PVCV: |
748a9306 | 4392 | case SVt_PVFM: |
85e6fe83 | 4393 | cv_undef((CV*)sv); |
a0d0e21e | 4394 | goto freescalar; |
79072805 | 4395 | case SVt_PVHV: |
85e6fe83 | 4396 | hv_undef((HV*)sv); |
a0d0e21e | 4397 | break; |
79072805 | 4398 | case SVt_PVAV: |
85e6fe83 | 4399 | av_undef((AV*)sv); |
a0d0e21e | 4400 | break; |
02270b4e GS |
4401 | case SVt_PVLV: |
4402 | SvREFCNT_dec(LvTARG(sv)); | |
4403 | goto freescalar; | |
a0d0e21e | 4404 | case SVt_PVGV: |
1edc1566 | 4405 | gp_free((GV*)sv); |
a0d0e21e | 4406 | Safefree(GvNAME(sv)); |
ec12f114 JPC |
4407 | /* cannot decrease stash refcount yet, as we might recursively delete |
4408 | ourselves when the refcnt drops to zero. Delay SvREFCNT_dec | |
4409 | of stash until current sv is completely gone. | |
4410 | -- JohnPC, 27 Mar 1998 */ | |
4411 | stash = GvSTASH(sv); | |
a0d0e21e | 4412 | /* FALL THROUGH */ |
79072805 | 4413 | case SVt_PVMG: |
79072805 LW |
4414 | case SVt_PVNV: |
4415 | case SVt_PVIV: | |
a0d0e21e LW |
4416 | freescalar: |
4417 | (void)SvOOK_off(sv); | |
79072805 LW |
4418 | /* FALL THROUGH */ |
4419 | case SVt_PV: | |
a0d0e21e | 4420 | case SVt_RV: |
810b8aa5 GS |
4421 | if (SvROK(sv)) { |
4422 | if (SvWEAKREF(sv)) | |
4423 | sv_del_backref(sv); | |
4424 | else | |
4425 | SvREFCNT_dec(SvRV(sv)); | |
4426 | } | |
1edc1566 | 4427 | else if (SvPVX(sv) && SvLEN(sv)) |
463ee0b2 | 4428 | Safefree(SvPVX(sv)); |
1c846c1f | 4429 | else if (SvPVX(sv) && SvREADONLY(sv) && SvFAKE(sv)) { |
c3654f1a | 4430 | unsharepvn(SvPVX(sv),SvUTF8(sv)?-SvCUR(sv):SvCUR(sv),SvUVX(sv)); |
1c846c1f NIS |
4431 | SvFAKE_off(sv); |
4432 | } | |
79072805 | 4433 | break; |
a0d0e21e | 4434 | /* |
79072805 | 4435 | case SVt_NV: |
79072805 | 4436 | case SVt_IV: |
79072805 LW |
4437 | case SVt_NULL: |
4438 | break; | |
a0d0e21e | 4439 | */ |
79072805 LW |
4440 | } |
4441 | ||
4442 | switch (SvTYPE(sv)) { | |
4443 | case SVt_NULL: | |
4444 | break; | |
79072805 LW |
4445 | case SVt_IV: |
4446 | del_XIV(SvANY(sv)); | |
4447 | break; | |
4448 | case SVt_NV: | |
4449 | del_XNV(SvANY(sv)); | |
4450 | break; | |
ed6116ce LW |
4451 | case SVt_RV: |
4452 | del_XRV(SvANY(sv)); | |
4453 | break; | |
79072805 LW |
4454 | case SVt_PV: |
4455 | del_XPV(SvANY(sv)); | |
4456 | break; | |
4457 | case SVt_PVIV: | |
4458 | del_XPVIV(SvANY(sv)); | |
4459 | break; | |
4460 | case SVt_PVNV: | |
4461 | del_XPVNV(SvANY(sv)); | |
4462 | break; | |
4463 | case SVt_PVMG: | |
4464 | del_XPVMG(SvANY(sv)); | |
4465 | break; | |
4466 | case SVt_PVLV: | |
4467 | del_XPVLV(SvANY(sv)); | |
4468 | break; | |
4469 | case SVt_PVAV: | |
4470 | del_XPVAV(SvANY(sv)); | |
4471 | break; | |
4472 | case SVt_PVHV: | |
4473 | del_XPVHV(SvANY(sv)); | |
4474 | break; | |
4475 | case SVt_PVCV: | |
4476 | del_XPVCV(SvANY(sv)); | |
4477 | break; | |
4478 | case SVt_PVGV: | |
4479 | del_XPVGV(SvANY(sv)); | |
ec12f114 JPC |
4480 | /* code duplication for increased performance. */ |
4481 | SvFLAGS(sv) &= SVf_BREAK; | |
4482 | SvFLAGS(sv) |= SVTYPEMASK; | |
4483 | /* decrease refcount of the stash that owns this GV, if any */ | |
4484 | if (stash) | |
4485 | SvREFCNT_dec(stash); | |
4486 | return; /* not break, SvFLAGS reset already happened */ | |
79072805 LW |
4487 | case SVt_PVBM: |
4488 | del_XPVBM(SvANY(sv)); | |
4489 | break; | |
4490 | case SVt_PVFM: | |
4491 | del_XPVFM(SvANY(sv)); | |
4492 | break; | |
8990e307 LW |
4493 | case SVt_PVIO: |
4494 | del_XPVIO(SvANY(sv)); | |
4495 | break; | |
79072805 | 4496 | } |
a0d0e21e | 4497 | SvFLAGS(sv) &= SVf_BREAK; |
8990e307 | 4498 | SvFLAGS(sv) |= SVTYPEMASK; |
79072805 LW |
4499 | } |
4500 | ||
4501 | SV * | |
864dbfa3 | 4502 | Perl_sv_newref(pTHX_ SV *sv) |
79072805 | 4503 | { |
463ee0b2 | 4504 | if (sv) |
dce16143 | 4505 | ATOMIC_INC(SvREFCNT(sv)); |
79072805 LW |
4506 | return sv; |
4507 | } | |
4508 | ||
c461cf8f JH |
4509 | /* |
4510 | =for apidoc sv_free | |
4511 | ||
4512 | Free the memory used by an SV. | |
4513 | ||
4514 | =cut | |
4515 | */ | |
4516 | ||
79072805 | 4517 | void |
864dbfa3 | 4518 | Perl_sv_free(pTHX_ SV *sv) |
79072805 | 4519 | { |
dce16143 MB |
4520 | int refcount_is_zero; |
4521 | ||
79072805 LW |
4522 | if (!sv) |
4523 | return; | |
a0d0e21e LW |
4524 | if (SvREFCNT(sv) == 0) { |
4525 | if (SvFLAGS(sv) & SVf_BREAK) | |
4526 | return; | |
3280af22 | 4527 | if (PL_in_clean_all) /* All is fair */ |
1edc1566 | 4528 | return; |
d689ffdd JP |
4529 | if (SvREADONLY(sv) && SvIMMORTAL(sv)) { |
4530 | /* make sure SvREFCNT(sv)==0 happens very seldom */ | |
4531 | SvREFCNT(sv) = (~(U32)0)/2; | |
4532 | return; | |
4533 | } | |
0453d815 PM |
4534 | if (ckWARN_d(WARN_INTERNAL)) |
4535 | Perl_warner(aTHX_ WARN_INTERNAL, "Attempt to free unreferenced scalar"); | |
79072805 LW |
4536 | return; |
4537 | } | |
dce16143 MB |
4538 | ATOMIC_DEC_AND_TEST(refcount_is_zero, SvREFCNT(sv)); |
4539 | if (!refcount_is_zero) | |
8990e307 | 4540 | return; |
463ee0b2 LW |
4541 | #ifdef DEBUGGING |
4542 | if (SvTEMP(sv)) { | |
0453d815 | 4543 | if (ckWARN_d(WARN_DEBUGGING)) |
f248d071 | 4544 | Perl_warner(aTHX_ WARN_DEBUGGING, |
1d7c1841 GS |
4545 | "Attempt to free temp prematurely: SV 0x%"UVxf, |
4546 | PTR2UV(sv)); | |
79072805 | 4547 | return; |
79072805 | 4548 | } |
463ee0b2 | 4549 | #endif |
d689ffdd JP |
4550 | if (SvREADONLY(sv) && SvIMMORTAL(sv)) { |
4551 | /* make sure SvREFCNT(sv)==0 happens very seldom */ | |
4552 | SvREFCNT(sv) = (~(U32)0)/2; | |
4553 | return; | |
4554 | } | |
79072805 | 4555 | sv_clear(sv); |
477f5d66 CS |
4556 | if (! SvREFCNT(sv)) |
4557 | del_SV(sv); | |
79072805 LW |
4558 | } |
4559 | ||
954c1994 GS |
4560 | /* |
4561 | =for apidoc sv_len | |
4562 | ||
4563 | Returns the length of the string in the SV. See also C<SvCUR>. | |
4564 | ||
4565 | =cut | |
4566 | */ | |
4567 | ||
79072805 | 4568 | STRLEN |
864dbfa3 | 4569 | Perl_sv_len(pTHX_ register SV *sv) |
79072805 | 4570 | { |
748a9306 | 4571 | char *junk; |
463ee0b2 | 4572 | STRLEN len; |
79072805 LW |
4573 | |
4574 | if (!sv) | |
4575 | return 0; | |
4576 | ||
8990e307 | 4577 | if (SvGMAGICAL(sv)) |
565764a8 | 4578 | len = mg_length(sv); |
8990e307 | 4579 | else |
748a9306 | 4580 | junk = SvPV(sv, len); |
463ee0b2 | 4581 | return len; |
79072805 LW |
4582 | } |
4583 | ||
c461cf8f JH |
4584 | /* |
4585 | =for apidoc sv_len_utf8 | |
4586 | ||
4587 | Returns the number of characters in the string in an SV, counting wide | |
4588 | UTF8 bytes as a single character. | |
4589 | ||
4590 | =cut | |
4591 | */ | |
4592 | ||
a0ed51b3 | 4593 | STRLEN |
864dbfa3 | 4594 | Perl_sv_len_utf8(pTHX_ register SV *sv) |
a0ed51b3 | 4595 | { |
a0ed51b3 LW |
4596 | if (!sv) |
4597 | return 0; | |
4598 | ||
a0ed51b3 | 4599 | if (SvGMAGICAL(sv)) |
b76347f2 | 4600 | return mg_length(sv); |
a0ed51b3 | 4601 | else |
b76347f2 JH |
4602 | { |
4603 | STRLEN len; | |
4604 | U8 *s = (U8*)SvPV(sv, len); | |
4605 | ||
d6efbbad | 4606 | return Perl_utf8_length(aTHX_ s, s + len); |
a0ed51b3 | 4607 | } |
a0ed51b3 LW |
4608 | } |
4609 | ||
4610 | void | |
864dbfa3 | 4611 | Perl_sv_pos_u2b(pTHX_ register SV *sv, I32* offsetp, I32* lenp) |
a0ed51b3 | 4612 | { |
dfe13c55 GS |
4613 | U8 *start; |
4614 | U8 *s; | |
4615 | U8 *send; | |
a0ed51b3 LW |
4616 | I32 uoffset = *offsetp; |
4617 | STRLEN len; | |
4618 | ||
4619 | if (!sv) | |
4620 | return; | |
4621 | ||
dfe13c55 | 4622 | start = s = (U8*)SvPV(sv, len); |
a0ed51b3 LW |
4623 | send = s + len; |
4624 | while (s < send && uoffset--) | |
4625 | s += UTF8SKIP(s); | |
bb40f870 GA |
4626 | if (s >= send) |
4627 | s = send; | |
a0ed51b3 LW |
4628 | *offsetp = s - start; |
4629 | if (lenp) { | |
4630 | I32 ulen = *lenp; | |
4631 | start = s; | |
4632 | while (s < send && ulen--) | |
4633 | s += UTF8SKIP(s); | |
bb40f870 GA |
4634 | if (s >= send) |
4635 | s = send; | |
a0ed51b3 LW |
4636 | *lenp = s - start; |
4637 | } | |
4638 | return; | |
4639 | } | |
4640 | ||
4641 | void | |
864dbfa3 | 4642 | Perl_sv_pos_b2u(pTHX_ register SV *sv, I32* offsetp) |
a0ed51b3 | 4643 | { |
dfe13c55 GS |
4644 | U8 *s; |
4645 | U8 *send; | |
a0ed51b3 LW |
4646 | STRLEN len; |
4647 | ||
4648 | if (!sv) | |
4649 | return; | |
4650 | ||
dfe13c55 | 4651 | s = (U8*)SvPV(sv, len); |
a0ed51b3 | 4652 | if (len < *offsetp) |
a0dbb045 | 4653 | Perl_croak(aTHX_ "panic: sv_pos_b2u: bad byte offset"); |
a0ed51b3 LW |
4654 | send = s + *offsetp; |
4655 | len = 0; | |
4656 | while (s < send) { | |
a0dbb045 JH |
4657 | STRLEN n; |
4658 | ||
4659 | if (utf8_to_uv(s, UTF8SKIP(s), &n, 0)) { | |
4660 | s += n; | |
4661 | len++; | |
4662 | } | |
4663 | else | |
4664 | break; | |
a0ed51b3 LW |
4665 | } |
4666 | *offsetp = len; | |
4667 | return; | |
4668 | } | |
4669 | ||
954c1994 GS |
4670 | /* |
4671 | =for apidoc sv_eq | |
4672 | ||
4673 | Returns a boolean indicating whether the strings in the two SVs are | |
4674 | identical. | |
4675 | ||
4676 | =cut | |
4677 | */ | |
4678 | ||
79072805 | 4679 | I32 |
e01b9e88 | 4680 | Perl_sv_eq(pTHX_ register SV *sv1, register SV *sv2) |
79072805 LW |
4681 | { |
4682 | char *pv1; | |
463ee0b2 | 4683 | STRLEN cur1; |
79072805 | 4684 | char *pv2; |
463ee0b2 | 4685 | STRLEN cur2; |
e01b9e88 SC |
4686 | I32 eq = 0; |
4687 | bool pv1tmp = FALSE; | |
4688 | bool pv2tmp = FALSE; | |
79072805 | 4689 | |
e01b9e88 | 4690 | if (!sv1) { |
79072805 LW |
4691 | pv1 = ""; |
4692 | cur1 = 0; | |
4693 | } | |
463ee0b2 | 4694 | else |
e01b9e88 | 4695 | pv1 = SvPV(sv1, cur1); |
79072805 | 4696 | |
e01b9e88 SC |
4697 | if (!sv2){ |
4698 | pv2 = ""; | |
4699 | cur2 = 0; | |
92d29cee | 4700 | } |
e01b9e88 SC |
4701 | else |
4702 | pv2 = SvPV(sv2, cur2); | |
79072805 | 4703 | |
e01b9e88 | 4704 | /* do not utf8ize the comparands as a side-effect */ |
7bbb0251 | 4705 | if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTE) { |
f9a63242 JH |
4706 | bool is_utf8 = TRUE; |
4707 | ||
1aa99e6b IH |
4708 | if (PL_hints & HINT_UTF8_DISTINCT) |
4709 | return FALSE; | |
4710 | ||
e01b9e88 | 4711 | if (SvUTF8(sv1)) { |
f34ff0a8 | 4712 | char *pv = (char*)bytes_from_utf8((U8*)pv1, &cur1, &is_utf8); |
f9a63242 JH |
4713 | if (is_utf8) |
4714 | return 0; | |
4715 | pv1tmp = (pv != pv1); | |
4716 | pv1 = pv; | |
e01b9e88 SC |
4717 | } |
4718 | else { | |
f34ff0a8 | 4719 | char *pv = (char *)bytes_from_utf8((U8*)pv2, &cur2, &is_utf8); |
f9a63242 JH |
4720 | if (is_utf8) |
4721 | return 0; | |
4722 | pv2tmp = (pv != pv2); | |
4723 | pv2 = pv; | |
e01b9e88 SC |
4724 | } |
4725 | } | |
79072805 | 4726 | |
e01b9e88 SC |
4727 | if (cur1 == cur2) |
4728 | eq = memEQ(pv1, pv2, cur1); | |
4729 | ||
4730 | if (pv1tmp) | |
4731 | Safefree(pv1); | |
4732 | if (pv2tmp) | |
4733 | Safefree(pv2); | |
4734 | ||
4735 | return eq; | |
79072805 LW |
4736 | } |
4737 | ||
954c1994 GS |
4738 | /* |
4739 | =for apidoc sv_cmp | |
4740 | ||
4741 | Compares the strings in two SVs. Returns -1, 0, or 1 indicating whether the | |
4742 | string in C<sv1> is less than, equal to, or greater than the string in | |
4743 | C<sv2>. | |
4744 | ||
4745 | =cut | |
4746 | */ | |
4747 | ||
79072805 | 4748 | I32 |
e01b9e88 | 4749 | Perl_sv_cmp(pTHX_ register SV *sv1, register SV *sv2) |
79072805 | 4750 | { |
560a288e GS |
4751 | STRLEN cur1, cur2; |
4752 | char *pv1, *pv2; | |
1c846c1f | 4753 | I32 cmp; |
e01b9e88 SC |
4754 | bool pv1tmp = FALSE; |
4755 | bool pv2tmp = FALSE; | |
560a288e | 4756 | |
e01b9e88 SC |
4757 | if (!sv1) { |
4758 | pv1 = ""; | |
560a288e GS |
4759 | cur1 = 0; |
4760 | } | |
e01b9e88 SC |
4761 | else |
4762 | pv1 = SvPV(sv1, cur1); | |
560a288e | 4763 | |
e01b9e88 SC |
4764 | if (!sv2){ |
4765 | pv2 = ""; | |
560a288e GS |
4766 | cur2 = 0; |
4767 | } | |
e01b9e88 SC |
4768 | else |
4769 | pv2 = SvPV(sv2, cur2); | |
79072805 | 4770 | |
e01b9e88 SC |
4771 | /* do not utf8ize the comparands as a side-effect */ |
4772 | if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTE) { | |
1aa99e6b IH |
4773 | if (PL_hints & HINT_UTF8_DISTINCT) |
4774 | return SvUTF8(sv1) ? 1 : -1; | |
4775 | ||
e01b9e88 SC |
4776 | if (SvUTF8(sv1)) { |
4777 | pv2 = (char*)bytes_to_utf8((U8*)pv2, &cur2); | |
4778 | pv2tmp = TRUE; | |
4779 | } | |
4780 | else { | |
4781 | pv1 = (char*)bytes_to_utf8((U8*)pv1, &cur1); | |
4782 | pv1tmp = TRUE; | |
4783 | } | |
4784 | } | |
79072805 | 4785 | |
e01b9e88 SC |
4786 | if (!cur1) { |
4787 | cmp = cur2 ? -1 : 0; | |
4788 | } else if (!cur2) { | |
4789 | cmp = 1; | |
4790 | } else { | |
4791 | I32 retval = memcmp((void*)pv1, (void*)pv2, cur1 < cur2 ? cur1 : cur2); | |
4792 | ||
4793 | if (retval) { | |
4794 | cmp = retval < 0 ? -1 : 1; | |
4795 | } else if (cur1 == cur2) { | |
4796 | cmp = 0; | |
4797 | } else { | |
4798 | cmp = cur1 < cur2 ? -1 : 1; | |
4799 | } | |
4800 | } | |
16660edb | 4801 | |
e01b9e88 SC |
4802 | if (pv1tmp) |
4803 | Safefree(pv1); | |
4804 | if (pv2tmp) | |
4805 | Safefree(pv2); | |
16660edb | 4806 | |
e01b9e88 | 4807 | return cmp; |
bbce6d69 | 4808 | } |
16660edb | 4809 | |
c461cf8f JH |
4810 | /* |
4811 | =for apidoc sv_cmp_locale | |
4812 | ||
4813 | Compares the strings in two SVs in a locale-aware manner. See | |
4814 | L</sv_cmp_locale> | |
4815 | ||
4816 | =cut | |
4817 | */ | |
4818 | ||
bbce6d69 | 4819 | I32 |
864dbfa3 | 4820 | Perl_sv_cmp_locale(pTHX_ register SV *sv1, register SV *sv2) |
bbce6d69 | 4821 | { |
36477c24 | 4822 | #ifdef USE_LOCALE_COLLATE |
16660edb | 4823 | |
bbce6d69 | 4824 | char *pv1, *pv2; |
4825 | STRLEN len1, len2; | |
4826 | I32 retval; | |
16660edb | 4827 | |
3280af22 | 4828 | if (PL_collation_standard) |
bbce6d69 | 4829 | goto raw_compare; |
16660edb | 4830 | |
bbce6d69 | 4831 | len1 = 0; |
8ac85365 | 4832 | pv1 = sv1 ? sv_collxfrm(sv1, &len1) : (char *) NULL; |
bbce6d69 | 4833 | len2 = 0; |
8ac85365 | 4834 | pv2 = sv2 ? sv_collxfrm(sv2, &len2) : (char *) NULL; |
16660edb | 4835 | |
bbce6d69 | 4836 | if (!pv1 || !len1) { |
4837 | if (pv2 && len2) | |
4838 | return -1; | |
4839 | else | |
4840 | goto raw_compare; | |
4841 | } | |
4842 | else { | |
4843 | if (!pv2 || !len2) | |
4844 | return 1; | |
4845 | } | |
16660edb | 4846 | |
bbce6d69 | 4847 | retval = memcmp((void*)pv1, (void*)pv2, len1 < len2 ? len1 : len2); |
16660edb | 4848 | |
bbce6d69 | 4849 | if (retval) |
16660edb | 4850 | return retval < 0 ? -1 : 1; |
4851 | ||
bbce6d69 | 4852 | /* |
4853 | * When the result of collation is equality, that doesn't mean | |
4854 | * that there are no differences -- some locales exclude some | |
4855 | * characters from consideration. So to avoid false equalities, | |
4856 | * we use the raw string as a tiebreaker. | |
4857 | */ | |
16660edb | 4858 | |
bbce6d69 | 4859 | raw_compare: |
4860 | /* FALL THROUGH */ | |
16660edb | 4861 | |
36477c24 | 4862 | #endif /* USE_LOCALE_COLLATE */ |
16660edb | 4863 | |
bbce6d69 | 4864 | return sv_cmp(sv1, sv2); |
4865 | } | |
79072805 | 4866 | |
36477c24 | 4867 | #ifdef USE_LOCALE_COLLATE |
7a4c00b4 | 4868 | /* |
4869 | * Any scalar variable may carry an 'o' magic that contains the | |
4870 | * scalar data of the variable transformed to such a format that | |
4871 | * a normal memory comparison can be used to compare the data | |
4872 | * according to the locale settings. | |
4873 | */ | |
bbce6d69 | 4874 | char * |
864dbfa3 | 4875 | Perl_sv_collxfrm(pTHX_ SV *sv, STRLEN *nxp) |
bbce6d69 | 4876 | { |
7a4c00b4 | 4877 | MAGIC *mg; |
16660edb | 4878 | |
8ac85365 | 4879 | mg = SvMAGICAL(sv) ? mg_find(sv, 'o') : (MAGIC *) NULL; |
3280af22 | 4880 | if (!mg || !mg->mg_ptr || *(U32*)mg->mg_ptr != PL_collation_ix) { |
bbce6d69 | 4881 | char *s, *xf; |
4882 | STRLEN len, xlen; | |
4883 | ||
7a4c00b4 | 4884 | if (mg) |
4885 | Safefree(mg->mg_ptr); | |
bbce6d69 | 4886 | s = SvPV(sv, len); |
4887 | if ((xf = mem_collxfrm(s, len, &xlen))) { | |
ff0cee69 | 4888 | if (SvREADONLY(sv)) { |
4889 | SAVEFREEPV(xf); | |
4890 | *nxp = xlen; | |
3280af22 | 4891 | return xf + sizeof(PL_collation_ix); |
ff0cee69 | 4892 | } |
7a4c00b4 | 4893 | if (! mg) { |
4894 | sv_magic(sv, 0, 'o', 0, 0); | |
4895 | mg = mg_find(sv, 'o'); | |
4896 | assert(mg); | |
bbce6d69 | 4897 | } |
7a4c00b4 | 4898 | mg->mg_ptr = xf; |
565764a8 | 4899 | mg->mg_len = xlen; |
7a4c00b4 | 4900 | } |
4901 | else { | |
ff0cee69 | 4902 | if (mg) { |
4903 | mg->mg_ptr = NULL; | |
565764a8 | 4904 | mg->mg_len = -1; |
ff0cee69 | 4905 | } |
bbce6d69 | 4906 | } |
4907 | } | |
7a4c00b4 | 4908 | if (mg && mg->mg_ptr) { |
565764a8 | 4909 | *nxp = mg->mg_len; |
3280af22 | 4910 | return mg->mg_ptr + sizeof(PL_collation_ix); |
bbce6d69 | 4911 | } |
4912 | else { | |
4913 | *nxp = 0; | |
4914 | return NULL; | |
16660edb | 4915 | } |
79072805 LW |
4916 | } |
4917 | ||
36477c24 | 4918 | #endif /* USE_LOCALE_COLLATE */ |
bbce6d69 | 4919 | |
c461cf8f JH |
4920 | /* |
4921 | =for apidoc sv_gets | |
4922 | ||
4923 | Get a line from the filehandle and store it into the SV, optionally | |
4924 | appending to the currently-stored string. | |
4925 | ||
4926 | =cut | |
4927 | */ | |
4928 | ||
79072805 | 4929 | char * |
864dbfa3 | 4930 | Perl_sv_gets(pTHX_ register SV *sv, register PerlIO *fp, I32 append) |
79072805 | 4931 | { |
c07a80fd | 4932 | char *rsptr; |
4933 | STRLEN rslen; | |
4934 | register STDCHAR rslast; | |
4935 | register STDCHAR *bp; | |
4936 | register I32 cnt; | |
4937 | I32 i; | |
4938 | ||
2213622d | 4939 | SV_CHECK_THINKFIRST(sv); |
6fc92669 | 4940 | (void)SvUPGRADE(sv, SVt_PV); |
99491443 | 4941 | |
ff68c719 | 4942 | SvSCREAM_off(sv); |
c07a80fd | 4943 | |
3280af22 | 4944 | if (RsSNARF(PL_rs)) { |
c07a80fd | 4945 | rsptr = NULL; |
4946 | rslen = 0; | |
4947 | } | |
3280af22 | 4948 | else if (RsRECORD(PL_rs)) { |
5b2b9c68 HM |
4949 | I32 recsize, bytesread; |
4950 | char *buffer; | |
4951 | ||
4952 | /* Grab the size of the record we're getting */ | |
3280af22 | 4953 | recsize = SvIV(SvRV(PL_rs)); |
5b2b9c68 | 4954 | (void)SvPOK_only(sv); /* Validate pointer */ |
e670df4e | 4955 | buffer = SvGROW(sv, recsize + 1); |
5b2b9c68 HM |
4956 | /* Go yank in */ |
4957 | #ifdef VMS | |
4958 | /* VMS wants read instead of fread, because fread doesn't respect */ | |
4959 | /* RMS record boundaries. This is not necessarily a good thing to be */ | |
4960 | /* doing, but we've got no other real choice */ | |
4961 | bytesread = PerlLIO_read(PerlIO_fileno(fp), buffer, recsize); | |
4962 | #else | |
4963 | bytesread = PerlIO_read(fp, buffer, recsize); | |
4964 | #endif | |
4965 | SvCUR_set(sv, bytesread); | |
e670df4e | 4966 | buffer[bytesread] = '\0'; |
7d59b7e4 NIS |
4967 | if (PerlIO_isutf8(fp)) |
4968 | SvUTF8_on(sv); | |
4969 | else | |
4970 | SvUTF8_off(sv); | |
5b2b9c68 HM |
4971 | return(SvCUR(sv) ? SvPVX(sv) : Nullch); |
4972 | } | |
3280af22 | 4973 | else if (RsPARA(PL_rs)) { |
c07a80fd | 4974 | rsptr = "\n\n"; |
4975 | rslen = 2; | |
4976 | } | |
7d59b7e4 NIS |
4977 | else { |
4978 | /* Get $/ i.e. PL_rs into same encoding as stream wants */ | |
4979 | if (PerlIO_isutf8(fp)) { | |
4980 | rsptr = SvPVutf8(PL_rs, rslen); | |
4981 | } | |
4982 | else { | |
4983 | if (SvUTF8(PL_rs)) { | |
4984 | if (!sv_utf8_downgrade(PL_rs, TRUE)) { | |
4985 | Perl_croak(aTHX_ "Wide character in $/"); | |
4986 | } | |
4987 | } | |
4988 | rsptr = SvPV(PL_rs, rslen); | |
4989 | } | |
4990 | } | |
4991 | ||
c07a80fd | 4992 | rslast = rslen ? rsptr[rslen - 1] : '\0'; |
4993 | ||
3280af22 | 4994 | if (RsPARA(PL_rs)) { /* have to do this both before and after */ |
79072805 | 4995 | do { /* to make sure file boundaries work right */ |
760ac839 | 4996 | if (PerlIO_eof(fp)) |
a0d0e21e | 4997 | return 0; |
760ac839 | 4998 | i = PerlIO_getc(fp); |
79072805 | 4999 | if (i != '\n') { |
a0d0e21e LW |
5000 | if (i == -1) |
5001 | return 0; | |
760ac839 | 5002 | PerlIO_ungetc(fp,i); |
79072805 LW |
5003 | break; |
5004 | } | |
5005 | } while (i != EOF); | |
5006 | } | |
c07a80fd | 5007 | |
760ac839 LW |
5008 | /* See if we know enough about I/O mechanism to cheat it ! */ |
5009 | ||
5010 | /* This used to be #ifdef test - it is made run-time test for ease | |
1c846c1f | 5011 | of abstracting out stdio interface. One call should be cheap |
760ac839 LW |
5012 | enough here - and may even be a macro allowing compile |
5013 | time optimization. | |
5014 | */ | |
5015 | ||
5016 | if (PerlIO_fast_gets(fp)) { | |
5017 | ||
5018 | /* | |
5019 | * We're going to steal some values from the stdio struct | |
5020 | * and put EVERYTHING in the innermost loop into registers. | |
5021 | */ | |
5022 | register STDCHAR *ptr; | |
5023 | STRLEN bpx; | |
5024 | I32 shortbuffered; | |
5025 | ||
16660edb | 5026 | #if defined(VMS) && defined(PERLIO_IS_STDIO) |
5027 | /* An ungetc()d char is handled separately from the regular | |
5028 | * buffer, so we getc() it back out and stuff it in the buffer. | |
5029 | */ | |
5030 | i = PerlIO_getc(fp); | |
5031 | if (i == EOF) return 0; | |
5032 | *(--((*fp)->_ptr)) = (unsigned char) i; | |
5033 | (*fp)->_cnt++; | |
5034 | #endif | |
c07a80fd | 5035 | |
c2960299 | 5036 | /* Here is some breathtakingly efficient cheating */ |
c07a80fd | 5037 | |
a20bf0c3 | 5038 | cnt = PerlIO_get_cnt(fp); /* get count into register */ |
a0d0e21e | 5039 | (void)SvPOK_only(sv); /* validate pointer */ |
79072805 LW |
5040 | if (SvLEN(sv) - append <= cnt + 1) { /* make sure we have the room */ |
5041 | if (cnt > 80 && SvLEN(sv) > append) { | |
5042 | shortbuffered = cnt - SvLEN(sv) + append + 1; | |
5043 | cnt -= shortbuffered; | |
5044 | } | |
5045 | else { | |
5046 | shortbuffered = 0; | |
bbce6d69 | 5047 | /* remember that cnt can be negative */ |
5048 | SvGROW(sv, append + (cnt <= 0 ? 2 : (cnt + 1))); | |
79072805 LW |
5049 | } |
5050 | } | |
5051 | else | |
5052 | shortbuffered = 0; | |
c07a80fd | 5053 | bp = (STDCHAR*)SvPVX(sv) + append; /* move these two too to registers */ |
a20bf0c3 | 5054 | ptr = (STDCHAR*)PerlIO_get_ptr(fp); |
16660edb | 5055 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5056 | "Screamer: entering, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt)); |
16660edb | 5057 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5058 | "Screamer: entering: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n", |
1c846c1f | 5059 | PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp), |
1d7c1841 | 5060 | PTR2UV(PerlIO_has_base(fp) ? PerlIO_get_base(fp) : 0))); |
79072805 LW |
5061 | for (;;) { |
5062 | screamer: | |
93a17b20 | 5063 | if (cnt > 0) { |
c07a80fd | 5064 | if (rslen) { |
760ac839 LW |
5065 | while (cnt > 0) { /* this | eat */ |
5066 | cnt--; | |
c07a80fd | 5067 | if ((*bp++ = *ptr++) == rslast) /* really | dust */ |
5068 | goto thats_all_folks; /* screams | sed :-) */ | |
5069 | } | |
5070 | } | |
5071 | else { | |
1c846c1f NIS |
5072 | Copy(ptr, bp, cnt, char); /* this | eat */ |
5073 | bp += cnt; /* screams | dust */ | |
c07a80fd | 5074 | ptr += cnt; /* louder | sed :-) */ |
a5f75d66 | 5075 | cnt = 0; |
93a17b20 | 5076 | } |
79072805 LW |
5077 | } |
5078 | ||
748a9306 | 5079 | if (shortbuffered) { /* oh well, must extend */ |
79072805 LW |
5080 | cnt = shortbuffered; |
5081 | shortbuffered = 0; | |
c07a80fd | 5082 | bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */ |
79072805 LW |
5083 | SvCUR_set(sv, bpx); |
5084 | SvGROW(sv, SvLEN(sv) + append + cnt + 2); | |
c07a80fd | 5085 | bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */ |
79072805 LW |
5086 | continue; |
5087 | } | |
5088 | ||
16660edb | 5089 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 GS |
5090 | "Screamer: going to getc, ptr=%"UVuf", cnt=%ld\n", |
5091 | PTR2UV(ptr),(long)cnt)); | |
a20bf0c3 | 5092 | PerlIO_set_ptrcnt(fp, ptr, cnt); /* deregisterize cnt and ptr */ |
16660edb | 5093 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5094 | "Screamer: pre: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n", |
1c846c1f | 5095 | PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp), |
1d7c1841 | 5096 | PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0))); |
1c846c1f | 5097 | /* This used to call 'filbuf' in stdio form, but as that behaves like |
774d564b | 5098 | getc when cnt <= 0 we use PerlIO_getc here to avoid introducing |
5099 | another abstraction. */ | |
760ac839 | 5100 | i = PerlIO_getc(fp); /* get more characters */ |
16660edb | 5101 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5102 | "Screamer: post: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n", |
1c846c1f | 5103 | PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp), |
1d7c1841 | 5104 | PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0))); |
a20bf0c3 JH |
5105 | cnt = PerlIO_get_cnt(fp); |
5106 | ptr = (STDCHAR*)PerlIO_get_ptr(fp); /* reregisterize cnt and ptr */ | |
16660edb | 5107 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5108 | "Screamer: after getc, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt)); |
79072805 | 5109 | |
748a9306 LW |
5110 | if (i == EOF) /* all done for ever? */ |
5111 | goto thats_really_all_folks; | |
5112 | ||
c07a80fd | 5113 | bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */ |
79072805 LW |
5114 | SvCUR_set(sv, bpx); |
5115 | SvGROW(sv, bpx + cnt + 2); | |
c07a80fd | 5116 | bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */ |
5117 | ||
760ac839 | 5118 | *bp++ = i; /* store character from PerlIO_getc */ |
79072805 | 5119 | |
c07a80fd | 5120 | if (rslen && (STDCHAR)i == rslast) /* all done for now? */ |
79072805 | 5121 | goto thats_all_folks; |
79072805 LW |
5122 | } |
5123 | ||
5124 | thats_all_folks: | |
c07a80fd | 5125 | if ((rslen > 1 && (bp - (STDCHAR*)SvPVX(sv) < rslen)) || |
36477c24 | 5126 | memNE((char*)bp - rslen, rsptr, rslen)) |
760ac839 | 5127 | goto screamer; /* go back to the fray */ |
79072805 LW |
5128 | thats_really_all_folks: |
5129 | if (shortbuffered) | |
5130 | cnt += shortbuffered; | |
16660edb | 5131 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5132 | "Screamer: quitting, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt)); |
a20bf0c3 | 5133 | PerlIO_set_ptrcnt(fp, ptr, cnt); /* put these back or we're in trouble */ |
16660edb | 5134 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
1d7c1841 | 5135 | "Screamer: end: 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 | 5138 | *bp = '\0'; |
760ac839 | 5139 | SvCUR_set(sv, bp - (STDCHAR*)SvPVX(sv)); /* set length */ |
16660edb | 5140 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
fb73857a | 5141 | "Screamer: done, len=%ld, string=|%.*s|\n", |
5142 | (long)SvCUR(sv),(int)SvCUR(sv),SvPVX(sv))); | |
760ac839 LW |
5143 | } |
5144 | else | |
79072805 | 5145 | { |
4d2c4e07 | 5146 | #ifndef EPOC |
760ac839 | 5147 | /*The big, slow, and stupid way */ |
c07a80fd | 5148 | STDCHAR buf[8192]; |
4d2c4e07 OF |
5149 | #else |
5150 | /* Need to work around EPOC SDK features */ | |
5151 | /* On WINS: MS VC5 generates calls to _chkstk, */ | |
5152 | /* if a `large' stack frame is allocated */ | |
5153 | /* gcc on MARM does not generate calls like these */ | |
5154 | STDCHAR buf[1024]; | |
5155 | #endif | |
79072805 | 5156 | |
760ac839 | 5157 | screamer2: |
c07a80fd | 5158 | if (rslen) { |
760ac839 LW |
5159 | register STDCHAR *bpe = buf + sizeof(buf); |
5160 | bp = buf; | |
5161 | while ((i = PerlIO_getc(fp)) != EOF && (*bp++ = i) != rslast && bp < bpe) | |
5162 | ; /* keep reading */ | |
5163 | cnt = bp - buf; | |
c07a80fd | 5164 | } |
5165 | else { | |
760ac839 | 5166 | cnt = PerlIO_read(fp,(char*)buf, sizeof(buf)); |
16660edb | 5167 | /* Accomodate broken VAXC compiler, which applies U8 cast to |
5168 | * both args of ?: operator, causing EOF to change into 255 | |
5169 | */ | |
5170 | if (cnt) { i = (U8)buf[cnt - 1]; } else { i = EOF; } | |
c07a80fd | 5171 | } |
79072805 LW |
5172 | |
5173 | if (append) | |
760ac839 | 5174 | sv_catpvn(sv, (char *) buf, cnt); |
79072805 | 5175 | else |
760ac839 | 5176 | sv_setpvn(sv, (char *) buf, cnt); |
c07a80fd | 5177 | |
5178 | if (i != EOF && /* joy */ | |
5179 | (!rslen || | |
5180 | SvCUR(sv) < rslen || | |
36477c24 | 5181 | memNE(SvPVX(sv) + SvCUR(sv) - rslen, rsptr, rslen))) |
79072805 LW |
5182 | { |
5183 | append = -1; | |
63e4d877 CS |
5184 | /* |
5185 | * If we're reading from a TTY and we get a short read, | |
5186 | * indicating that the user hit his EOF character, we need | |
5187 | * to notice it now, because if we try to read from the TTY | |
5188 | * again, the EOF condition will disappear. | |
5189 | * | |
5190 | * The comparison of cnt to sizeof(buf) is an optimization | |
5191 | * that prevents unnecessary calls to feof(). | |
5192 | * | |
5193 | * - jik 9/25/96 | |
5194 | */ | |
5195 | if (!(cnt < sizeof(buf) && PerlIO_eof(fp))) | |
5196 | goto screamer2; | |
79072805 LW |
5197 | } |
5198 | } | |
5199 | ||
1c846c1f | 5200 | if (RsPARA(PL_rs)) { /* have to do this both before and after */ |
c07a80fd | 5201 | while (i != EOF) { /* to make sure file boundaries work right */ |
760ac839 | 5202 | i = PerlIO_getc(fp); |
79072805 | 5203 | if (i != '\n') { |
760ac839 | 5204 | PerlIO_ungetc(fp,i); |
79072805 LW |
5205 | break; |
5206 | } | |
5207 | } | |
5208 | } | |
c07a80fd | 5209 | |
7d59b7e4 NIS |
5210 | if (PerlIO_isutf8(fp)) |
5211 | SvUTF8_on(sv); | |
5212 | else | |
5213 | SvUTF8_off(sv); | |
5214 | ||
c07a80fd | 5215 | return (SvCUR(sv) - append) ? SvPVX(sv) : Nullch; |
79072805 LW |
5216 | } |
5217 | ||
760ac839 | 5218 | |
954c1994 GS |
5219 | /* |
5220 | =for apidoc sv_inc | |
5221 | ||
5222 | Auto-increment of the value in the SV. | |
5223 | ||
5224 | =cut | |
5225 | */ | |
5226 | ||
79072805 | 5227 | void |
864dbfa3 | 5228 | Perl_sv_inc(pTHX_ register SV *sv) |
79072805 LW |
5229 | { |
5230 | register char *d; | |
463ee0b2 | 5231 | int flags; |
79072805 LW |
5232 | |
5233 | if (!sv) | |
5234 | return; | |
b23a5f78 GB |
5235 | if (SvGMAGICAL(sv)) |
5236 | mg_get(sv); | |
ed6116ce | 5237 | if (SvTHINKFIRST(sv)) { |
0f15f207 | 5238 | if (SvREADONLY(sv)) { |
3280af22 | 5239 | if (PL_curcop != &PL_compiling) |
cea2e8a9 | 5240 | Perl_croak(aTHX_ PL_no_modify); |
0f15f207 | 5241 | } |
a0d0e21e | 5242 | if (SvROK(sv)) { |
b5be31e9 | 5243 | IV i; |
9e7bc3e8 JD |
5244 | if (SvAMAGIC(sv) && AMG_CALLun(sv,inc)) |
5245 | return; | |
56431972 | 5246 | i = PTR2IV(SvRV(sv)); |
b5be31e9 SM |
5247 | sv_unref(sv); |
5248 | sv_setiv(sv, i); | |
a0d0e21e | 5249 | } |
ed6116ce | 5250 | } |
8990e307 | 5251 | flags = SvFLAGS(sv); |
28e5dec8 JH |
5252 | if ((flags & (SVp_NOK|SVp_IOK)) == SVp_NOK) { |
5253 | /* It's (privately or publicly) a float, but not tested as an | |
5254 | integer, so test it to see. */ | |
d460ef45 | 5255 | (void) SvIV(sv); |
28e5dec8 JH |
5256 | flags = SvFLAGS(sv); |
5257 | } | |
5258 | if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) { | |
5259 | /* It's publicly an integer, or privately an integer-not-float */ | |
5260 | oops_its_int: | |
25da4f38 IZ |
5261 | if (SvIsUV(sv)) { |
5262 | if (SvUVX(sv) == UV_MAX) | |
65202027 | 5263 | sv_setnv(sv, (NV)UV_MAX + 1.0); |
25da4f38 IZ |
5264 | else |
5265 | (void)SvIOK_only_UV(sv); | |
5266 | ++SvUVX(sv); | |
5267 | } else { | |
5268 | if (SvIVX(sv) == IV_MAX) | |
28e5dec8 | 5269 | sv_setuv(sv, (UV)IV_MAX + 1); |
25da4f38 IZ |
5270 | else { |
5271 | (void)SvIOK_only(sv); | |
5272 | ++SvIVX(sv); | |
1c846c1f | 5273 | } |
55497cff | 5274 | } |
79072805 LW |
5275 | return; |
5276 | } | |
28e5dec8 JH |
5277 | if (flags & SVp_NOK) { |
5278 | (void)SvNOK_only(sv); | |
5279 | SvNVX(sv) += 1.0; | |
5280 | return; | |
5281 | } | |
5282 | ||
8990e307 | 5283 | if (!(flags & SVp_POK) || !*SvPVX(sv)) { |
28e5dec8 JH |
5284 | if ((flags & SVTYPEMASK) < SVt_PVIV) |
5285 | sv_upgrade(sv, SVt_IV); | |
5286 | (void)SvIOK_only(sv); | |
5287 | SvIVX(sv) = 1; | |
79072805 LW |
5288 | return; |
5289 | } | |
463ee0b2 | 5290 | d = SvPVX(sv); |
79072805 LW |
5291 | while (isALPHA(*d)) d++; |
5292 | while (isDIGIT(*d)) d++; | |
5293 | if (*d) { | |
28e5dec8 JH |
5294 | #ifdef PERL_PRESERVE_IVUV |
5295 | /* Got to punt this an an integer if needs be, but we don't issue | |
5296 | warnings. Probably ought to make the sv_iv_please() that does | |
5297 | the conversion if possible, and silently. */ | |
5298 | I32 numtype = looks_like_number(sv); | |
5299 | if (numtype && !(numtype & IS_NUMBER_INFINITY)) { | |
5300 | /* Need to try really hard to see if it's an integer. | |
5301 | 9.22337203685478e+18 is an integer. | |
5302 | but "9.22337203685478e+18" + 0 is UV=9223372036854779904 | |
5303 | so $a="9.22337203685478e+18"; $a+0; $a++ | |
5304 | needs to be the same as $a="9.22337203685478e+18"; $a++ | |
5305 | or we go insane. */ | |
d460ef45 | 5306 | |
28e5dec8 JH |
5307 | (void) sv_2iv(sv); |
5308 | if (SvIOK(sv)) | |
5309 | goto oops_its_int; | |
5310 | ||
5311 | /* sv_2iv *should* have made this an NV */ | |
5312 | if (flags & SVp_NOK) { | |
5313 | (void)SvNOK_only(sv); | |
5314 | SvNVX(sv) += 1.0; | |
5315 | return; | |
5316 | } | |
5317 | /* I don't think we can get here. Maybe I should assert this | |
5318 | And if we do get here I suspect that sv_setnv will croak. NWC | |
5319 | Fall through. */ | |
5320 | #if defined(USE_LONG_DOUBLE) | |
5321 | 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", | |
5322 | SvPVX(sv), SvIVX(sv), SvNVX(sv))); | |
5323 | #else | |
5324 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%g\n", | |
5325 | SvPVX(sv), SvIVX(sv), SvNVX(sv))); | |
5326 | #endif | |
5327 | } | |
5328 | #endif /* PERL_PRESERVE_IVUV */ | |
5329 | sv_setnv(sv,Atof(SvPVX(sv)) + 1.0); | |
79072805 LW |
5330 | return; |
5331 | } | |
5332 | d--; | |
463ee0b2 | 5333 | while (d >= SvPVX(sv)) { |
79072805 LW |
5334 | if (isDIGIT(*d)) { |
5335 | if (++*d <= '9') | |
5336 | return; | |
5337 | *(d--) = '0'; | |
5338 | } | |
5339 | else { | |
9d116dd7 JH |
5340 | #ifdef EBCDIC |
5341 | /* MKS: The original code here died if letters weren't consecutive. | |
5342 | * at least it didn't have to worry about non-C locales. The | |
5343 | * new code assumes that ('z'-'a')==('Z'-'A'), letters are | |
1c846c1f | 5344 | * arranged in order (although not consecutively) and that only |
9d116dd7 JH |
5345 | * [A-Za-z] are accepted by isALPHA in the C locale. |
5346 | */ | |
5347 | if (*d != 'z' && *d != 'Z') { | |
5348 | do { ++*d; } while (!isALPHA(*d)); | |
5349 | return; | |
5350 | } | |
5351 | *(d--) -= 'z' - 'a'; | |
5352 | #else | |
79072805 LW |
5353 | ++*d; |
5354 | if (isALPHA(*d)) | |
5355 | return; | |
5356 | *(d--) -= 'z' - 'a' + 1; | |
9d116dd7 | 5357 | #endif |
79072805 LW |
5358 | } |
5359 | } | |
5360 | /* oh,oh, the number grew */ | |
5361 | SvGROW(sv, SvCUR(sv) + 2); | |
5362 | SvCUR(sv)++; | |
463ee0b2 | 5363 | for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX(sv); d--) |
79072805 LW |
5364 | *d = d[-1]; |
5365 | if (isDIGIT(d[1])) | |
5366 | *d = '1'; | |
5367 | else | |
5368 | *d = d[1]; | |
5369 | } | |
5370 | ||
954c1994 GS |
5371 | /* |
5372 | =for apidoc sv_dec | |
5373 | ||
5374 | Auto-decrement of the value in the SV. | |
5375 | ||
5376 | =cut | |
5377 | */ | |
5378 | ||
79072805 | 5379 | void |
864dbfa3 | 5380 | Perl_sv_dec(pTHX_ register SV *sv) |
79072805 | 5381 | { |
463ee0b2 LW |
5382 | int flags; |
5383 | ||
79072805 LW |
5384 | if (!sv) |
5385 | return; | |
b23a5f78 GB |
5386 | if (SvGMAGICAL(sv)) |
5387 | mg_get(sv); | |
ed6116ce | 5388 | if (SvTHINKFIRST(sv)) { |
0f15f207 | 5389 | if (SvREADONLY(sv)) { |
3280af22 | 5390 | if (PL_curcop != &PL_compiling) |
cea2e8a9 | 5391 | Perl_croak(aTHX_ PL_no_modify); |
0f15f207 | 5392 | } |
a0d0e21e | 5393 | if (SvROK(sv)) { |
b5be31e9 | 5394 | IV i; |
9e7bc3e8 JD |
5395 | if (SvAMAGIC(sv) && AMG_CALLun(sv,dec)) |
5396 | return; | |
56431972 | 5397 | i = PTR2IV(SvRV(sv)); |
b5be31e9 SM |
5398 | sv_unref(sv); |
5399 | sv_setiv(sv, i); | |
a0d0e21e | 5400 | } |
ed6116ce | 5401 | } |
28e5dec8 JH |
5402 | /* Unlike sv_inc we don't have to worry about string-never-numbers |
5403 | and keeping them magic. But we mustn't warn on punting */ | |
8990e307 | 5404 | flags = SvFLAGS(sv); |
28e5dec8 JH |
5405 | if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) { |
5406 | /* It's publicly an integer, or privately an integer-not-float */ | |
5407 | oops_its_int: | |
25da4f38 IZ |
5408 | if (SvIsUV(sv)) { |
5409 | if (SvUVX(sv) == 0) { | |
5410 | (void)SvIOK_only(sv); | |
5411 | SvIVX(sv) = -1; | |
5412 | } | |
5413 | else { | |
5414 | (void)SvIOK_only_UV(sv); | |
5415 | --SvUVX(sv); | |
1c846c1f | 5416 | } |
25da4f38 IZ |
5417 | } else { |
5418 | if (SvIVX(sv) == IV_MIN) | |
65202027 | 5419 | sv_setnv(sv, (NV)IV_MIN - 1.0); |
25da4f38 IZ |
5420 | else { |
5421 | (void)SvIOK_only(sv); | |
5422 | --SvIVX(sv); | |
1c846c1f | 5423 | } |
55497cff | 5424 | } |
5425 | return; | |
5426 | } | |
28e5dec8 JH |
5427 | if (flags & SVp_NOK) { |
5428 | SvNVX(sv) -= 1.0; | |
5429 | (void)SvNOK_only(sv); | |
5430 | return; | |
5431 | } | |
8990e307 | 5432 | if (!(flags & SVp_POK)) { |
4633a7c4 LW |
5433 | if ((flags & SVTYPEMASK) < SVt_PVNV) |
5434 | sv_upgrade(sv, SVt_NV); | |
463ee0b2 | 5435 | SvNVX(sv) = -1.0; |
a0d0e21e | 5436 | (void)SvNOK_only(sv); |
79072805 LW |
5437 | return; |
5438 | } | |
28e5dec8 JH |
5439 | #ifdef PERL_PRESERVE_IVUV |
5440 | { | |
5441 | I32 numtype = looks_like_number(sv); | |
5442 | if (numtype && !(numtype & IS_NUMBER_INFINITY)) { | |
5443 | /* Need to try really hard to see if it's an integer. | |
5444 | 9.22337203685478e+18 is an integer. | |
5445 | but "9.22337203685478e+18" + 0 is UV=9223372036854779904 | |
5446 | so $a="9.22337203685478e+18"; $a+0; $a-- | |
5447 | needs to be the same as $a="9.22337203685478e+18"; $a-- | |
5448 | or we go insane. */ | |
d460ef45 | 5449 | |
28e5dec8 JH |
5450 | (void) sv_2iv(sv); |
5451 | if (SvIOK(sv)) | |
5452 | goto oops_its_int; | |
5453 | ||
5454 | /* sv_2iv *should* have made this an NV */ | |
5455 | if (flags & SVp_NOK) { | |
5456 | (void)SvNOK_only(sv); | |
5457 | SvNVX(sv) -= 1.0; | |
5458 | return; | |
5459 | } | |
5460 | /* I don't think we can get here. Maybe I should assert this | |
5461 | And if we do get here I suspect that sv_setnv will croak. NWC | |
5462 | Fall through. */ | |
5463 | #if defined(USE_LONG_DOUBLE) | |
5464 | 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", | |
5465 | SvPVX(sv), SvIVX(sv), SvNVX(sv))); | |
5466 | #else | |
5467 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%g\n", | |
5468 | SvPVX(sv), SvIVX(sv), SvNVX(sv))); | |
5469 | #endif | |
5470 | } | |
5471 | } | |
5472 | #endif /* PERL_PRESERVE_IVUV */ | |
097ee67d | 5473 | sv_setnv(sv,Atof(SvPVX(sv)) - 1.0); /* punt */ |
79072805 LW |
5474 | } |
5475 | ||
954c1994 GS |
5476 | /* |
5477 | =for apidoc sv_mortalcopy | |
5478 | ||
5479 | Creates a new SV which is a copy of the original SV. The new SV is marked | |
5480 | as mortal. | |
5481 | ||
5482 | =cut | |
5483 | */ | |
5484 | ||
79072805 LW |
5485 | /* Make a string that will exist for the duration of the expression |
5486 | * evaluation. Actually, it may have to last longer than that, but | |
5487 | * hopefully we won't free it until it has been assigned to a | |
5488 | * permanent location. */ | |
5489 | ||
5490 | SV * | |
864dbfa3 | 5491 | Perl_sv_mortalcopy(pTHX_ SV *oldstr) |
79072805 | 5492 | { |
463ee0b2 | 5493 | register SV *sv; |
79072805 | 5494 | |
4561caa4 | 5495 | new_SV(sv); |
79072805 | 5496 | sv_setsv(sv,oldstr); |
677b06e3 GS |
5497 | EXTEND_MORTAL(1); |
5498 | PL_tmps_stack[++PL_tmps_ix] = sv; | |
8990e307 LW |
5499 | SvTEMP_on(sv); |
5500 | return sv; | |
5501 | } | |
5502 | ||
954c1994 GS |
5503 | /* |
5504 | =for apidoc sv_newmortal | |
5505 | ||
5506 | Creates a new SV which is mortal. The reference count of the SV is set to 1. | |
5507 | ||
5508 | =cut | |
5509 | */ | |
5510 | ||
8990e307 | 5511 | SV * |
864dbfa3 | 5512 | Perl_sv_newmortal(pTHX) |
8990e307 LW |
5513 | { |
5514 | register SV *sv; | |
5515 | ||
4561caa4 | 5516 | new_SV(sv); |
8990e307 | 5517 | SvFLAGS(sv) = SVs_TEMP; |
677b06e3 GS |
5518 | EXTEND_MORTAL(1); |
5519 | PL_tmps_stack[++PL_tmps_ix] = sv; | |
79072805 LW |
5520 | return sv; |
5521 | } | |
5522 | ||
954c1994 GS |
5523 | /* |
5524 | =for apidoc sv_2mortal | |
5525 | ||
5526 | Marks an SV as mortal. The SV will be destroyed when the current context | |
5527 | ends. | |
5528 | ||
5529 | =cut | |
5530 | */ | |
5531 | ||
79072805 LW |
5532 | /* same thing without the copying */ |
5533 | ||
5534 | SV * | |
864dbfa3 | 5535 | Perl_sv_2mortal(pTHX_ register SV *sv) |
79072805 LW |
5536 | { |
5537 | if (!sv) | |
5538 | return sv; | |
d689ffdd | 5539 | if (SvREADONLY(sv) && SvIMMORTAL(sv)) |
11162842 | 5540 | return sv; |
677b06e3 GS |
5541 | EXTEND_MORTAL(1); |
5542 | PL_tmps_stack[++PL_tmps_ix] = sv; | |
8990e307 | 5543 | SvTEMP_on(sv); |
79072805 LW |
5544 | return sv; |
5545 | } | |
5546 | ||
954c1994 GS |
5547 | /* |
5548 | =for apidoc newSVpv | |
5549 | ||
5550 | Creates a new SV and copies a string into it. The reference count for the | |
5551 | SV is set to 1. If C<len> is zero, Perl will compute the length using | |
5552 | strlen(). For efficiency, consider using C<newSVpvn> instead. | |
5553 | ||
5554 | =cut | |
5555 | */ | |
5556 | ||
79072805 | 5557 | SV * |
864dbfa3 | 5558 | Perl_newSVpv(pTHX_ const char *s, STRLEN len) |
79072805 | 5559 | { |
463ee0b2 | 5560 | register SV *sv; |
79072805 | 5561 | |
4561caa4 | 5562 | new_SV(sv); |
79072805 LW |
5563 | if (!len) |
5564 | len = strlen(s); | |
5565 | sv_setpvn(sv,s,len); | |
5566 | return sv; | |
5567 | } | |
5568 | ||
954c1994 GS |
5569 | /* |
5570 | =for apidoc newSVpvn | |
5571 | ||
5572 | Creates a new SV and copies a string into it. The reference count for the | |
1c846c1f | 5573 | SV is set to 1. Note that if C<len> is zero, Perl will create a zero length |
954c1994 GS |
5574 | string. You are responsible for ensuring that the source string is at least |
5575 | C<len> bytes long. | |
5576 | ||
5577 | =cut | |
5578 | */ | |
5579 | ||
9da1e3b5 | 5580 | SV * |
864dbfa3 | 5581 | Perl_newSVpvn(pTHX_ const char *s, STRLEN len) |
9da1e3b5 MUN |
5582 | { |
5583 | register SV *sv; | |
5584 | ||
5585 | new_SV(sv); | |
9da1e3b5 MUN |
5586 | sv_setpvn(sv,s,len); |
5587 | return sv; | |
5588 | } | |
5589 | ||
1c846c1f NIS |
5590 | /* |
5591 | =for apidoc newSVpvn_share | |
5592 | ||
5593 | Creates a new SV and populates it with a string from | |
5594 | the string table. Turns on READONLY and FAKE. | |
5595 | The idea here is that as string table is used for shared hash | |
5596 | keys these strings will have SvPVX == HeKEY and hash lookup | |
5597 | will avoid string compare. | |
5598 | ||
5599 | =cut | |
5600 | */ | |
5601 | ||
5602 | SV * | |
c3654f1a | 5603 | Perl_newSVpvn_share(pTHX_ const char *src, I32 len, U32 hash) |
1c846c1f NIS |
5604 | { |
5605 | register SV *sv; | |
c3654f1a IH |
5606 | bool is_utf8 = FALSE; |
5607 | if (len < 0) { | |
5608 | len = -len; | |
5609 | is_utf8 = TRUE; | |
5610 | } | |
f9a63242 JH |
5611 | if (is_utf8 && !(PL_hints & HINT_UTF8_DISTINCT)) |
5612 | src = (char*)bytes_from_utf8((U8*)src, (STRLEN*)&len, &is_utf8); | |
1c846c1f NIS |
5613 | if (!hash) |
5614 | PERL_HASH(hash, src, len); | |
5615 | new_SV(sv); | |
5616 | sv_upgrade(sv, SVt_PVIV); | |
c3654f1a | 5617 | SvPVX(sv) = sharepvn(src, is_utf8?-len:len, hash); |
1c846c1f NIS |
5618 | SvCUR(sv) = len; |
5619 | SvUVX(sv) = hash; | |
5620 | SvLEN(sv) = 0; | |
5621 | SvREADONLY_on(sv); | |
5622 | SvFAKE_on(sv); | |
5623 | SvPOK_on(sv); | |
c3654f1a IH |
5624 | if (is_utf8) |
5625 | SvUTF8_on(sv); | |
1c846c1f NIS |
5626 | return sv; |
5627 | } | |
5628 | ||
cea2e8a9 | 5629 | #if defined(PERL_IMPLICIT_CONTEXT) |
46fc3d4c | 5630 | SV * |
cea2e8a9 | 5631 | Perl_newSVpvf_nocontext(const char* pat, ...) |
46fc3d4c | 5632 | { |
cea2e8a9 | 5633 | dTHX; |
46fc3d4c | 5634 | register SV *sv; |
5635 | va_list args; | |
46fc3d4c | 5636 | va_start(args, pat); |
c5be433b | 5637 | sv = vnewSVpvf(pat, &args); |
46fc3d4c | 5638 | va_end(args); |
5639 | return sv; | |
5640 | } | |
cea2e8a9 | 5641 | #endif |
46fc3d4c | 5642 | |
954c1994 GS |
5643 | /* |
5644 | =for apidoc newSVpvf | |
5645 | ||
5646 | Creates a new SV an initialize it with the string formatted like | |
5647 | C<sprintf>. | |
5648 | ||
5649 | =cut | |
5650 | */ | |
5651 | ||
cea2e8a9 GS |
5652 | SV * |
5653 | Perl_newSVpvf(pTHX_ const char* pat, ...) | |
5654 | { | |
5655 | register SV *sv; | |
5656 | va_list args; | |
cea2e8a9 | 5657 | va_start(args, pat); |
c5be433b | 5658 | sv = vnewSVpvf(pat, &args); |
cea2e8a9 GS |
5659 | va_end(args); |
5660 | return sv; | |
5661 | } | |
46fc3d4c | 5662 | |
79072805 | 5663 | SV * |
c5be433b GS |
5664 | Perl_vnewSVpvf(pTHX_ const char* pat, va_list* args) |
5665 | { | |
5666 | register SV *sv; | |
5667 | new_SV(sv); | |
5668 | sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
5669 | return sv; | |
5670 | } | |
5671 | ||
954c1994 GS |
5672 | /* |
5673 | =for apidoc newSVnv | |
5674 | ||
5675 | Creates a new SV and copies a floating point value into it. | |
5676 | The reference count for the SV is set to 1. | |
5677 | ||
5678 | =cut | |
5679 | */ | |
5680 | ||
c5be433b | 5681 | SV * |
65202027 | 5682 | Perl_newSVnv(pTHX_ NV n) |
79072805 | 5683 | { |
463ee0b2 | 5684 | register SV *sv; |
79072805 | 5685 | |
4561caa4 | 5686 | new_SV(sv); |
79072805 LW |
5687 | sv_setnv(sv,n); |
5688 | return sv; | |
5689 | } | |
5690 | ||
954c1994 GS |
5691 | /* |
5692 | =for apidoc newSViv | |
5693 | ||
5694 | Creates a new SV and copies an integer into it. The reference count for the | |
5695 | SV is set to 1. | |
5696 | ||
5697 | =cut | |
5698 | */ | |
5699 | ||
79072805 | 5700 | SV * |
864dbfa3 | 5701 | Perl_newSViv(pTHX_ IV i) |
79072805 | 5702 | { |
463ee0b2 | 5703 | register SV *sv; |
79072805 | 5704 | |
4561caa4 | 5705 | new_SV(sv); |
79072805 LW |
5706 | sv_setiv(sv,i); |
5707 | return sv; | |
5708 | } | |
5709 | ||
954c1994 | 5710 | /* |
1a3327fb JH |
5711 | =for apidoc newSVuv |
5712 | ||
5713 | Creates a new SV and copies an unsigned integer into it. | |
5714 | The reference count for the SV is set to 1. | |
5715 | ||
5716 | =cut | |
5717 | */ | |
5718 | ||
5719 | SV * | |
5720 | Perl_newSVuv(pTHX_ UV u) | |
5721 | { | |
5722 | register SV *sv; | |
5723 | ||
5724 | new_SV(sv); | |
5725 | sv_setuv(sv,u); | |
5726 | return sv; | |
5727 | } | |
5728 | ||
5729 | /* | |
954c1994 GS |
5730 | =for apidoc newRV_noinc |
5731 | ||
5732 | Creates an RV wrapper for an SV. The reference count for the original | |
5733 | SV is B<not> incremented. | |
5734 | ||
5735 | =cut | |
5736 | */ | |
5737 | ||
2304df62 | 5738 | SV * |
864dbfa3 | 5739 | Perl_newRV_noinc(pTHX_ SV *tmpRef) |
2304df62 AD |
5740 | { |
5741 | register SV *sv; | |
5742 | ||
4561caa4 | 5743 | new_SV(sv); |
2304df62 | 5744 | sv_upgrade(sv, SVt_RV); |
76e3520e | 5745 | SvTEMP_off(tmpRef); |
d689ffdd | 5746 | SvRV(sv) = tmpRef; |
2304df62 | 5747 | SvROK_on(sv); |
2304df62 AD |
5748 | return sv; |
5749 | } | |
5750 | ||
954c1994 | 5751 | /* newRV_inc is #defined to newRV in sv.h */ |
5f05dabc | 5752 | SV * |
864dbfa3 | 5753 | Perl_newRV(pTHX_ SV *tmpRef) |
5f05dabc | 5754 | { |
5f6447b6 | 5755 | return newRV_noinc(SvREFCNT_inc(tmpRef)); |
5f05dabc | 5756 | } |
5f05dabc | 5757 | |
954c1994 GS |
5758 | /* |
5759 | =for apidoc newSVsv | |
5760 | ||
5761 | Creates a new SV which is an exact duplicate of the original SV. | |
5762 | ||
5763 | =cut | |
5764 | */ | |
5765 | ||
79072805 LW |
5766 | /* make an exact duplicate of old */ |
5767 | ||
5768 | SV * | |
864dbfa3 | 5769 | Perl_newSVsv(pTHX_ register SV *old) |
79072805 | 5770 | { |
463ee0b2 | 5771 | register SV *sv; |
79072805 LW |
5772 | |
5773 | if (!old) | |
5774 | return Nullsv; | |
8990e307 | 5775 | if (SvTYPE(old) == SVTYPEMASK) { |
0453d815 PM |
5776 | if (ckWARN_d(WARN_INTERNAL)) |
5777 | Perl_warner(aTHX_ WARN_INTERNAL, "semi-panic: attempt to dup freed string"); | |
79072805 LW |
5778 | return Nullsv; |
5779 | } | |
4561caa4 | 5780 | new_SV(sv); |
ff68c719 | 5781 | if (SvTEMP(old)) { |
5782 | SvTEMP_off(old); | |
463ee0b2 | 5783 | sv_setsv(sv,old); |
ff68c719 | 5784 | SvTEMP_on(old); |
79072805 LW |
5785 | } |
5786 | else | |
463ee0b2 LW |
5787 | sv_setsv(sv,old); |
5788 | return sv; | |
79072805 LW |
5789 | } |
5790 | ||
5791 | void | |
864dbfa3 | 5792 | Perl_sv_reset(pTHX_ register char *s, HV *stash) |
79072805 LW |
5793 | { |
5794 | register HE *entry; | |
5795 | register GV *gv; | |
5796 | register SV *sv; | |
5797 | register I32 i; | |
5798 | register PMOP *pm; | |
5799 | register I32 max; | |
4802d5d7 | 5800 | char todo[PERL_UCHAR_MAX+1]; |
79072805 | 5801 | |
49d8d3a1 MB |
5802 | if (!stash) |
5803 | return; | |
5804 | ||
79072805 LW |
5805 | if (!*s) { /* reset ?? searches */ |
5806 | for (pm = HvPMROOT(stash); pm; pm = pm->op_pmnext) { | |
48c036b1 | 5807 | pm->op_pmdynflags &= ~PMdf_USED; |
79072805 LW |
5808 | } |
5809 | return; | |
5810 | } | |
5811 | ||
5812 | /* reset variables */ | |
5813 | ||
5814 | if (!HvARRAY(stash)) | |
5815 | return; | |
463ee0b2 LW |
5816 | |
5817 | Zero(todo, 256, char); | |
79072805 | 5818 | while (*s) { |
4802d5d7 | 5819 | i = (unsigned char)*s; |
79072805 LW |
5820 | if (s[1] == '-') { |
5821 | s += 2; | |
5822 | } | |
4802d5d7 | 5823 | max = (unsigned char)*s++; |
79072805 | 5824 | for ( ; i <= max; i++) { |
463ee0b2 LW |
5825 | todo[i] = 1; |
5826 | } | |
a0d0e21e | 5827 | for (i = 0; i <= (I32) HvMAX(stash); i++) { |
79072805 | 5828 | for (entry = HvARRAY(stash)[i]; |
9e35f4b3 GS |
5829 | entry; |
5830 | entry = HeNEXT(entry)) | |
5831 | { | |
1edc1566 | 5832 | if (!todo[(U8)*HeKEY(entry)]) |
463ee0b2 | 5833 | continue; |
1edc1566 | 5834 | gv = (GV*)HeVAL(entry); |
79072805 | 5835 | sv = GvSV(gv); |
9e35f4b3 GS |
5836 | if (SvTHINKFIRST(sv)) { |
5837 | if (!SvREADONLY(sv) && SvROK(sv)) | |
5838 | sv_unref(sv); | |
5839 | continue; | |
5840 | } | |
a0d0e21e | 5841 | (void)SvOK_off(sv); |
79072805 LW |
5842 | if (SvTYPE(sv) >= SVt_PV) { |
5843 | SvCUR_set(sv, 0); | |
463ee0b2 LW |
5844 | if (SvPVX(sv) != Nullch) |
5845 | *SvPVX(sv) = '\0'; | |
44a8e56a | 5846 | SvTAINT(sv); |
79072805 LW |
5847 | } |
5848 | if (GvAV(gv)) { | |
5849 | av_clear(GvAV(gv)); | |
5850 | } | |
44a8e56a | 5851 | if (GvHV(gv) && !HvNAME(GvHV(gv))) { |
463ee0b2 | 5852 | hv_clear(GvHV(gv)); |
fa6a1c44 | 5853 | #ifdef USE_ENVIRON_ARRAY |
3280af22 | 5854 | if (gv == PL_envgv) |
79072805 | 5855 | environ[0] = Nullch; |
a0d0e21e | 5856 | #endif |
79072805 LW |
5857 | } |
5858 | } | |
5859 | } | |
5860 | } | |
5861 | } | |
5862 | ||
46fc3d4c | 5863 | IO* |
864dbfa3 | 5864 | Perl_sv_2io(pTHX_ SV *sv) |
46fc3d4c | 5865 | { |
5866 | IO* io; | |
5867 | GV* gv; | |
2d8e6c8d | 5868 | STRLEN n_a; |
46fc3d4c | 5869 | |
5870 | switch (SvTYPE(sv)) { | |
5871 | case SVt_PVIO: | |
5872 | io = (IO*)sv; | |
5873 | break; | |
5874 | case SVt_PVGV: | |
5875 | gv = (GV*)sv; | |
5876 | io = GvIO(gv); | |
5877 | if (!io) | |
cea2e8a9 | 5878 | Perl_croak(aTHX_ "Bad filehandle: %s", GvNAME(gv)); |
46fc3d4c | 5879 | break; |
5880 | default: | |
5881 | if (!SvOK(sv)) | |
cea2e8a9 | 5882 | Perl_croak(aTHX_ PL_no_usym, "filehandle"); |
46fc3d4c | 5883 | if (SvROK(sv)) |
5884 | return sv_2io(SvRV(sv)); | |
2d8e6c8d | 5885 | gv = gv_fetchpv(SvPV(sv,n_a), FALSE, SVt_PVIO); |
46fc3d4c | 5886 | if (gv) |
5887 | io = GvIO(gv); | |
5888 | else | |
5889 | io = 0; | |
5890 | if (!io) | |
cea2e8a9 | 5891 | Perl_croak(aTHX_ "Bad filehandle: %s", SvPV(sv,n_a)); |
46fc3d4c | 5892 | break; |
5893 | } | |
5894 | return io; | |
5895 | } | |
5896 | ||
79072805 | 5897 | CV * |
864dbfa3 | 5898 | Perl_sv_2cv(pTHX_ SV *sv, HV **st, GV **gvp, I32 lref) |
79072805 LW |
5899 | { |
5900 | GV *gv; | |
5901 | CV *cv; | |
2d8e6c8d | 5902 | STRLEN n_a; |
79072805 LW |
5903 | |
5904 | if (!sv) | |
93a17b20 | 5905 | return *gvp = Nullgv, Nullcv; |
79072805 | 5906 | switch (SvTYPE(sv)) { |
79072805 LW |
5907 | case SVt_PVCV: |
5908 | *st = CvSTASH(sv); | |
5909 | *gvp = Nullgv; | |
5910 | return (CV*)sv; | |
5911 | case SVt_PVHV: | |
5912 | case SVt_PVAV: | |
5913 | *gvp = Nullgv; | |
5914 | return Nullcv; | |
8990e307 LW |
5915 | case SVt_PVGV: |
5916 | gv = (GV*)sv; | |
a0d0e21e | 5917 | *gvp = gv; |
8990e307 LW |
5918 | *st = GvESTASH(gv); |
5919 | goto fix_gv; | |
5920 | ||
79072805 | 5921 | default: |
a0d0e21e LW |
5922 | if (SvGMAGICAL(sv)) |
5923 | mg_get(sv); | |
5924 | if (SvROK(sv)) { | |
f5284f61 IZ |
5925 | SV **sp = &sv; /* Used in tryAMAGICunDEREF macro. */ |
5926 | tryAMAGICunDEREF(to_cv); | |
5927 | ||
62f274bf GS |
5928 | sv = SvRV(sv); |
5929 | if (SvTYPE(sv) == SVt_PVCV) { | |
5930 | cv = (CV*)sv; | |
5931 | *gvp = Nullgv; | |
5932 | *st = CvSTASH(cv); | |
5933 | return cv; | |
5934 | } | |
5935 | else if(isGV(sv)) | |
5936 | gv = (GV*)sv; | |
5937 | else | |
cea2e8a9 | 5938 | Perl_croak(aTHX_ "Not a subroutine reference"); |
a0d0e21e | 5939 | } |
62f274bf | 5940 | else if (isGV(sv)) |
79072805 LW |
5941 | gv = (GV*)sv; |
5942 | else | |
2d8e6c8d | 5943 | gv = gv_fetchpv(SvPV(sv, n_a), lref, SVt_PVCV); |
79072805 LW |
5944 | *gvp = gv; |
5945 | if (!gv) | |
5946 | return Nullcv; | |
5947 | *st = GvESTASH(gv); | |
8990e307 | 5948 | fix_gv: |
8ebc5c01 | 5949 | if (lref && !GvCVu(gv)) { |
4633a7c4 | 5950 | SV *tmpsv; |
748a9306 | 5951 | ENTER; |
4633a7c4 | 5952 | tmpsv = NEWSV(704,0); |
16660edb | 5953 | gv_efullname3(tmpsv, gv, Nullch); |
f6ec51f7 GS |
5954 | /* XXX this is probably not what they think they're getting. |
5955 | * It has the same effect as "sub name;", i.e. just a forward | |
5956 | * declaration! */ | |
774d564b | 5957 | newSUB(start_subparse(FALSE, 0), |
4633a7c4 LW |
5958 | newSVOP(OP_CONST, 0, tmpsv), |
5959 | Nullop, | |
8990e307 | 5960 | Nullop); |
748a9306 | 5961 | LEAVE; |
8ebc5c01 | 5962 | if (!GvCVu(gv)) |
cea2e8a9 | 5963 | Perl_croak(aTHX_ "Unable to create sub named \"%s\"", SvPV(sv,n_a)); |
8990e307 | 5964 | } |
8ebc5c01 | 5965 | return GvCVu(gv); |
79072805 LW |
5966 | } |
5967 | } | |
5968 | ||
c461cf8f JH |
5969 | /* |
5970 | =for apidoc sv_true | |
5971 | ||
5972 | Returns true if the SV has a true value by Perl's rules. | |
5973 | ||
5974 | =cut | |
5975 | */ | |
5976 | ||
79072805 | 5977 | I32 |
864dbfa3 | 5978 | Perl_sv_true(pTHX_ register SV *sv) |
79072805 | 5979 | { |
8990e307 LW |
5980 | if (!sv) |
5981 | return 0; | |
79072805 | 5982 | if (SvPOK(sv)) { |
4e35701f NIS |
5983 | register XPV* tXpv; |
5984 | if ((tXpv = (XPV*)SvANY(sv)) && | |
c2f1de04 | 5985 | (tXpv->xpv_cur > 1 || |
4e35701f | 5986 | (tXpv->xpv_cur && *tXpv->xpv_pv != '0'))) |
79072805 LW |
5987 | return 1; |
5988 | else | |
5989 | return 0; | |
5990 | } | |
5991 | else { | |
5992 | if (SvIOK(sv)) | |
463ee0b2 | 5993 | return SvIVX(sv) != 0; |
79072805 LW |
5994 | else { |
5995 | if (SvNOK(sv)) | |
463ee0b2 | 5996 | return SvNVX(sv) != 0.0; |
79072805 | 5997 | else |
463ee0b2 | 5998 | return sv_2bool(sv); |
79072805 LW |
5999 | } |
6000 | } | |
6001 | } | |
79072805 | 6002 | |
ff68c719 | 6003 | IV |
864dbfa3 | 6004 | Perl_sv_iv(pTHX_ register SV *sv) |
85e6fe83 | 6005 | { |
25da4f38 IZ |
6006 | if (SvIOK(sv)) { |
6007 | if (SvIsUV(sv)) | |
6008 | return (IV)SvUVX(sv); | |
ff68c719 | 6009 | return SvIVX(sv); |
25da4f38 | 6010 | } |
ff68c719 | 6011 | return sv_2iv(sv); |
85e6fe83 | 6012 | } |
85e6fe83 | 6013 | |
ff68c719 | 6014 | UV |
864dbfa3 | 6015 | Perl_sv_uv(pTHX_ register SV *sv) |
ff68c719 | 6016 | { |
25da4f38 IZ |
6017 | if (SvIOK(sv)) { |
6018 | if (SvIsUV(sv)) | |
6019 | return SvUVX(sv); | |
6020 | return (UV)SvIVX(sv); | |
6021 | } | |
ff68c719 | 6022 | return sv_2uv(sv); |
6023 | } | |
85e6fe83 | 6024 | |
65202027 | 6025 | NV |
864dbfa3 | 6026 | Perl_sv_nv(pTHX_ register SV *sv) |
79072805 | 6027 | { |
ff68c719 | 6028 | if (SvNOK(sv)) |
6029 | return SvNVX(sv); | |
6030 | return sv_2nv(sv); | |
79072805 | 6031 | } |
79072805 | 6032 | |
79072805 | 6033 | char * |
864dbfa3 | 6034 | Perl_sv_pv(pTHX_ SV *sv) |
1fa8b10d JD |
6035 | { |
6036 | STRLEN n_a; | |
6037 | ||
6038 | if (SvPOK(sv)) | |
6039 | return SvPVX(sv); | |
6040 | ||
6041 | return sv_2pv(sv, &n_a); | |
6042 | } | |
6043 | ||
6044 | char * | |
864dbfa3 | 6045 | Perl_sv_pvn(pTHX_ SV *sv, STRLEN *lp) |
79072805 | 6046 | { |
85e6fe83 LW |
6047 | if (SvPOK(sv)) { |
6048 | *lp = SvCUR(sv); | |
a0d0e21e | 6049 | return SvPVX(sv); |
85e6fe83 | 6050 | } |
463ee0b2 | 6051 | return sv_2pv(sv, lp); |
79072805 | 6052 | } |
79072805 | 6053 | |
c461cf8f JH |
6054 | /* |
6055 | =for apidoc sv_pvn_force | |
6056 | ||
6057 | Get a sensible string out of the SV somehow. | |
6058 | ||
6059 | =cut | |
6060 | */ | |
6061 | ||
a0d0e21e | 6062 | char * |
864dbfa3 | 6063 | Perl_sv_pvn_force(pTHX_ SV *sv, STRLEN *lp) |
a0d0e21e LW |
6064 | { |
6065 | char *s; | |
6066 | ||
6fc92669 GS |
6067 | if (SvTHINKFIRST(sv) && !SvROK(sv)) |
6068 | sv_force_normal(sv); | |
1c846c1f | 6069 | |
a0d0e21e LW |
6070 | if (SvPOK(sv)) { |
6071 | *lp = SvCUR(sv); | |
6072 | } | |
6073 | else { | |
748a9306 | 6074 | if (SvTYPE(sv) > SVt_PVLV && SvTYPE(sv) != SVt_PVFM) { |
cea2e8a9 | 6075 | Perl_croak(aTHX_ "Can't coerce %s to string in %s", sv_reftype(sv,0), |
6fc92669 | 6076 | PL_op_name[PL_op->op_type]); |
a0d0e21e | 6077 | } |
4633a7c4 LW |
6078 | else |
6079 | s = sv_2pv(sv, lp); | |
a0d0e21e LW |
6080 | if (s != SvPVX(sv)) { /* Almost, but not quite, sv_setpvn() */ |
6081 | STRLEN len = *lp; | |
1c846c1f | 6082 | |
a0d0e21e LW |
6083 | if (SvROK(sv)) |
6084 | sv_unref(sv); | |
6085 | (void)SvUPGRADE(sv, SVt_PV); /* Never FALSE */ | |
6086 | SvGROW(sv, len + 1); | |
6087 | Move(s,SvPVX(sv),len,char); | |
6088 | SvCUR_set(sv, len); | |
6089 | *SvEND(sv) = '\0'; | |
6090 | } | |
6091 | if (!SvPOK(sv)) { | |
6092 | SvPOK_on(sv); /* validate pointer */ | |
6093 | SvTAINT(sv); | |
1d7c1841 GS |
6094 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n", |
6095 | PTR2UV(sv),SvPVX(sv))); | |
a0d0e21e LW |
6096 | } |
6097 | } | |
6098 | return SvPVX(sv); | |
6099 | } | |
6100 | ||
6101 | char * | |
7340a771 GS |
6102 | Perl_sv_pvbyte(pTHX_ SV *sv) |
6103 | { | |
6104 | return sv_pv(sv); | |
6105 | } | |
6106 | ||
6107 | char * | |
6108 | Perl_sv_pvbyten(pTHX_ SV *sv, STRLEN *lp) | |
6109 | { | |
6110 | return sv_pvn(sv,lp); | |
6111 | } | |
6112 | ||
6113 | char * | |
6114 | Perl_sv_pvbyten_force(pTHX_ SV *sv, STRLEN *lp) | |
6115 | { | |
6116 | return sv_pvn_force(sv,lp); | |
6117 | } | |
6118 | ||
6119 | char * | |
6120 | Perl_sv_pvutf8(pTHX_ SV *sv) | |
6121 | { | |
560a288e | 6122 | sv_utf8_upgrade(sv); |
7340a771 GS |
6123 | return sv_pv(sv); |
6124 | } | |
6125 | ||
6126 | char * | |
6127 | Perl_sv_pvutf8n(pTHX_ SV *sv, STRLEN *lp) | |
6128 | { | |
560a288e | 6129 | sv_utf8_upgrade(sv); |
7340a771 GS |
6130 | return sv_pvn(sv,lp); |
6131 | } | |
6132 | ||
c461cf8f JH |
6133 | /* |
6134 | =for apidoc sv_pvutf8n_force | |
6135 | ||
6136 | Get a sensible UTF8-encoded string out of the SV somehow. See | |
6137 | L</sv_pvn_force>. | |
6138 | ||
6139 | =cut | |
6140 | */ | |
6141 | ||
7340a771 GS |
6142 | char * |
6143 | Perl_sv_pvutf8n_force(pTHX_ SV *sv, STRLEN *lp) | |
6144 | { | |
560a288e | 6145 | sv_utf8_upgrade(sv); |
7340a771 GS |
6146 | return sv_pvn_force(sv,lp); |
6147 | } | |
6148 | ||
c461cf8f JH |
6149 | /* |
6150 | =for apidoc sv_reftype | |
6151 | ||
6152 | Returns a string describing what the SV is a reference to. | |
6153 | ||
6154 | =cut | |
6155 | */ | |
6156 | ||
7340a771 | 6157 | char * |
864dbfa3 | 6158 | Perl_sv_reftype(pTHX_ SV *sv, int ob) |
a0d0e21e LW |
6159 | { |
6160 | if (ob && SvOBJECT(sv)) | |
6161 | return HvNAME(SvSTASH(sv)); | |
6162 | else { | |
6163 | switch (SvTYPE(sv)) { | |
6164 | case SVt_NULL: | |
6165 | case SVt_IV: | |
6166 | case SVt_NV: | |
6167 | case SVt_RV: | |
6168 | case SVt_PV: | |
6169 | case SVt_PVIV: | |
6170 | case SVt_PVNV: | |
6171 | case SVt_PVMG: | |
6172 | case SVt_PVBM: | |
6173 | if (SvROK(sv)) | |
6174 | return "REF"; | |
6175 | else | |
6176 | return "SCALAR"; | |
6177 | case SVt_PVLV: return "LVALUE"; | |
6178 | case SVt_PVAV: return "ARRAY"; | |
6179 | case SVt_PVHV: return "HASH"; | |
6180 | case SVt_PVCV: return "CODE"; | |
6181 | case SVt_PVGV: return "GLOB"; | |
1d2dff63 | 6182 | case SVt_PVFM: return "FORMAT"; |
27f9d8f3 | 6183 | case SVt_PVIO: return "IO"; |
a0d0e21e LW |
6184 | default: return "UNKNOWN"; |
6185 | } | |
6186 | } | |
6187 | } | |
6188 | ||
954c1994 GS |
6189 | /* |
6190 | =for apidoc sv_isobject | |
6191 | ||
6192 | Returns a boolean indicating whether the SV is an RV pointing to a blessed | |
6193 | object. If the SV is not an RV, or if the object is not blessed, then this | |
6194 | will return false. | |
6195 | ||
6196 | =cut | |
6197 | */ | |
6198 | ||
463ee0b2 | 6199 | int |
864dbfa3 | 6200 | Perl_sv_isobject(pTHX_ SV *sv) |
85e6fe83 | 6201 | { |
68dc0745 | 6202 | if (!sv) |
6203 | return 0; | |
6204 | if (SvGMAGICAL(sv)) | |
6205 | mg_get(sv); | |
85e6fe83 LW |
6206 | if (!SvROK(sv)) |
6207 | return 0; | |
6208 | sv = (SV*)SvRV(sv); | |
6209 | if (!SvOBJECT(sv)) | |
6210 | return 0; | |
6211 | return 1; | |
6212 | } | |
6213 | ||
954c1994 GS |
6214 | /* |
6215 | =for apidoc sv_isa | |
6216 | ||
6217 | Returns a boolean indicating whether the SV is blessed into the specified | |
6218 | class. This does not check for subtypes; use C<sv_derived_from> to verify | |
6219 | an inheritance relationship. | |
6220 | ||
6221 | =cut | |
6222 | */ | |
6223 | ||
85e6fe83 | 6224 | int |
864dbfa3 | 6225 | Perl_sv_isa(pTHX_ SV *sv, const char *name) |
463ee0b2 | 6226 | { |
68dc0745 | 6227 | if (!sv) |
6228 | return 0; | |
6229 | if (SvGMAGICAL(sv)) | |
6230 | mg_get(sv); | |
ed6116ce | 6231 | if (!SvROK(sv)) |
463ee0b2 | 6232 | return 0; |
ed6116ce LW |
6233 | sv = (SV*)SvRV(sv); |
6234 | if (!SvOBJECT(sv)) | |
463ee0b2 LW |
6235 | return 0; |
6236 | ||
6237 | return strEQ(HvNAME(SvSTASH(sv)), name); | |
6238 | } | |
6239 | ||
954c1994 GS |
6240 | /* |
6241 | =for apidoc newSVrv | |
6242 | ||
6243 | Creates a new SV for the RV, C<rv>, to point to. If C<rv> is not an RV then | |
6244 | it will be upgraded to one. If C<classname> is non-null then the new SV will | |
6245 | be blessed in the specified package. The new SV is returned and its | |
6246 | reference count is 1. | |
6247 | ||
6248 | =cut | |
6249 | */ | |
6250 | ||
463ee0b2 | 6251 | SV* |
864dbfa3 | 6252 | Perl_newSVrv(pTHX_ SV *rv, const char *classname) |
463ee0b2 | 6253 | { |
463ee0b2 LW |
6254 | SV *sv; |
6255 | ||
4561caa4 | 6256 | new_SV(sv); |
51cf62d8 | 6257 | |
2213622d | 6258 | SV_CHECK_THINKFIRST(rv); |
51cf62d8 | 6259 | SvAMAGIC_off(rv); |
51cf62d8 | 6260 | |
0199fce9 JD |
6261 | if (SvTYPE(rv) >= SVt_PVMG) { |
6262 | U32 refcnt = SvREFCNT(rv); | |
6263 | SvREFCNT(rv) = 0; | |
6264 | sv_clear(rv); | |
6265 | SvFLAGS(rv) = 0; | |
6266 | SvREFCNT(rv) = refcnt; | |
6267 | } | |
6268 | ||
51cf62d8 | 6269 | if (SvTYPE(rv) < SVt_RV) |
0199fce9 JD |
6270 | sv_upgrade(rv, SVt_RV); |
6271 | else if (SvTYPE(rv) > SVt_RV) { | |
6272 | (void)SvOOK_off(rv); | |
6273 | if (SvPVX(rv) && SvLEN(rv)) | |
6274 | Safefree(SvPVX(rv)); | |
6275 | SvCUR_set(rv, 0); | |
6276 | SvLEN_set(rv, 0); | |
6277 | } | |
51cf62d8 OT |
6278 | |
6279 | (void)SvOK_off(rv); | |
053fc874 | 6280 | SvRV(rv) = sv; |
ed6116ce | 6281 | SvROK_on(rv); |
463ee0b2 | 6282 | |
a0d0e21e LW |
6283 | if (classname) { |
6284 | HV* stash = gv_stashpv(classname, TRUE); | |
6285 | (void)sv_bless(rv, stash); | |
6286 | } | |
6287 | return sv; | |
6288 | } | |
6289 | ||
954c1994 GS |
6290 | /* |
6291 | =for apidoc sv_setref_pv | |
6292 | ||
6293 | Copies a pointer into a new SV, optionally blessing the SV. The C<rv> | |
6294 | argument will be upgraded to an RV. That RV will be modified to point to | |
6295 | the new SV. If the C<pv> argument is NULL then C<PL_sv_undef> will be placed | |
6296 | into the SV. The C<classname> argument indicates the package for the | |
6297 | blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV | |
6298 | will be returned and will have a reference count of 1. | |
6299 | ||
6300 | Do not use with other Perl types such as HV, AV, SV, CV, because those | |
6301 | objects will become corrupted by the pointer copy process. | |
6302 | ||
6303 | Note that C<sv_setref_pvn> copies the string while this copies the pointer. | |
6304 | ||
6305 | =cut | |
6306 | */ | |
6307 | ||
a0d0e21e | 6308 | SV* |
864dbfa3 | 6309 | Perl_sv_setref_pv(pTHX_ SV *rv, const char *classname, void *pv) |
a0d0e21e | 6310 | { |
189b2af5 | 6311 | if (!pv) { |
3280af22 | 6312 | sv_setsv(rv, &PL_sv_undef); |
189b2af5 GS |
6313 | SvSETMAGIC(rv); |
6314 | } | |
a0d0e21e | 6315 | else |
56431972 | 6316 | sv_setiv(newSVrv(rv,classname), PTR2IV(pv)); |
a0d0e21e LW |
6317 | return rv; |
6318 | } | |
6319 | ||
954c1994 GS |
6320 | /* |
6321 | =for apidoc sv_setref_iv | |
6322 | ||
6323 | Copies an integer into a new SV, optionally blessing the SV. The C<rv> | |
6324 | argument will be upgraded to an RV. That RV will be modified to point to | |
6325 | the new SV. The C<classname> argument indicates the package for the | |
6326 | blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV | |
6327 | will be returned and will have a reference count of 1. | |
6328 | ||
6329 | =cut | |
6330 | */ | |
6331 | ||
a0d0e21e | 6332 | SV* |
864dbfa3 | 6333 | Perl_sv_setref_iv(pTHX_ SV *rv, const char *classname, IV iv) |
a0d0e21e LW |
6334 | { |
6335 | sv_setiv(newSVrv(rv,classname), iv); | |
6336 | return rv; | |
6337 | } | |
6338 | ||
954c1994 GS |
6339 | /* |
6340 | =for apidoc sv_setref_nv | |
6341 | ||
6342 | Copies a double into a new SV, optionally blessing the SV. The C<rv> | |
6343 | argument will be upgraded to an RV. That RV will be modified to point to | |
6344 | the new SV. The C<classname> argument indicates the package for the | |
6345 | blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV | |
6346 | will be returned and will have a reference count of 1. | |
6347 | ||
6348 | =cut | |
6349 | */ | |
6350 | ||
a0d0e21e | 6351 | SV* |
65202027 | 6352 | Perl_sv_setref_nv(pTHX_ SV *rv, const char *classname, NV nv) |
a0d0e21e LW |
6353 | { |
6354 | sv_setnv(newSVrv(rv,classname), nv); | |
6355 | return rv; | |
6356 | } | |
463ee0b2 | 6357 | |
954c1994 GS |
6358 | /* |
6359 | =for apidoc sv_setref_pvn | |
6360 | ||
6361 | Copies a string into a new SV, optionally blessing the SV. The length of the | |
6362 | string must be specified with C<n>. The C<rv> argument will be upgraded to | |
6363 | an RV. That RV will be modified to point to the new SV. The C<classname> | |
6364 | argument indicates the package for the blessing. Set C<classname> to | |
6365 | C<Nullch> to avoid the blessing. The new SV will be returned and will have | |
6366 | a reference count of 1. | |
6367 | ||
6368 | Note that C<sv_setref_pv> copies the pointer while this copies the string. | |
6369 | ||
6370 | =cut | |
6371 | */ | |
6372 | ||
a0d0e21e | 6373 | SV* |
864dbfa3 | 6374 | Perl_sv_setref_pvn(pTHX_ SV *rv, const char *classname, char *pv, STRLEN n) |
a0d0e21e LW |
6375 | { |
6376 | sv_setpvn(newSVrv(rv,classname), pv, n); | |
463ee0b2 LW |
6377 | return rv; |
6378 | } | |
6379 | ||
954c1994 GS |
6380 | /* |
6381 | =for apidoc sv_bless | |
6382 | ||
6383 | Blesses an SV into a specified package. The SV must be an RV. The package | |
6384 | must be designated by its stash (see C<gv_stashpv()>). The reference count | |
6385 | of the SV is unaffected. | |
6386 | ||
6387 | =cut | |
6388 | */ | |
6389 | ||
a0d0e21e | 6390 | SV* |
864dbfa3 | 6391 | Perl_sv_bless(pTHX_ SV *sv, HV *stash) |
a0d0e21e | 6392 | { |
76e3520e | 6393 | SV *tmpRef; |
a0d0e21e | 6394 | if (!SvROK(sv)) |
cea2e8a9 | 6395 | Perl_croak(aTHX_ "Can't bless non-reference value"); |
76e3520e GS |
6396 | tmpRef = SvRV(sv); |
6397 | if (SvFLAGS(tmpRef) & (SVs_OBJECT|SVf_READONLY)) { | |
6398 | if (SvREADONLY(tmpRef)) | |
cea2e8a9 | 6399 | Perl_croak(aTHX_ PL_no_modify); |
76e3520e GS |
6400 | if (SvOBJECT(tmpRef)) { |
6401 | if (SvTYPE(tmpRef) != SVt_PVIO) | |
3280af22 | 6402 | --PL_sv_objcount; |
76e3520e | 6403 | SvREFCNT_dec(SvSTASH(tmpRef)); |
2e3febc6 | 6404 | } |
a0d0e21e | 6405 | } |
76e3520e GS |
6406 | SvOBJECT_on(tmpRef); |
6407 | if (SvTYPE(tmpRef) != SVt_PVIO) | |
3280af22 | 6408 | ++PL_sv_objcount; |
76e3520e GS |
6409 | (void)SvUPGRADE(tmpRef, SVt_PVMG); |
6410 | SvSTASH(tmpRef) = (HV*)SvREFCNT_inc(stash); | |
a0d0e21e | 6411 | |
2e3febc6 CS |
6412 | if (Gv_AMG(stash)) |
6413 | SvAMAGIC_on(sv); | |
6414 | else | |
6415 | SvAMAGIC_off(sv); | |
a0d0e21e LW |
6416 | |
6417 | return sv; | |
6418 | } | |
6419 | ||
76e3520e | 6420 | STATIC void |
cea2e8a9 | 6421 | S_sv_unglob(pTHX_ SV *sv) |
a0d0e21e | 6422 | { |
850fabdf GS |
6423 | void *xpvmg; |
6424 | ||
a0d0e21e LW |
6425 | assert(SvTYPE(sv) == SVt_PVGV); |
6426 | SvFAKE_off(sv); | |
6427 | if (GvGP(sv)) | |
1edc1566 | 6428 | gp_free((GV*)sv); |
e826b3c7 GS |
6429 | if (GvSTASH(sv)) { |
6430 | SvREFCNT_dec(GvSTASH(sv)); | |
6431 | GvSTASH(sv) = Nullhv; | |
6432 | } | |
a0d0e21e LW |
6433 | sv_unmagic(sv, '*'); |
6434 | Safefree(GvNAME(sv)); | |
a5f75d66 | 6435 | GvMULTI_off(sv); |
850fabdf GS |
6436 | |
6437 | /* need to keep SvANY(sv) in the right arena */ | |
6438 | xpvmg = new_XPVMG(); | |
6439 | StructCopy(SvANY(sv), xpvmg, XPVMG); | |
6440 | del_XPVGV(SvANY(sv)); | |
6441 | SvANY(sv) = xpvmg; | |
6442 | ||
a0d0e21e LW |
6443 | SvFLAGS(sv) &= ~SVTYPEMASK; |
6444 | SvFLAGS(sv) |= SVt_PVMG; | |
6445 | } | |
6446 | ||
954c1994 | 6447 | /* |
840a7b70 | 6448 | =for apidoc sv_unref_flags |
954c1994 GS |
6449 | |
6450 | Unsets the RV status of the SV, and decrements the reference count of | |
6451 | whatever was being referenced by the RV. This can almost be thought of | |
840a7b70 IZ |
6452 | as a reversal of C<newSVrv>. The C<cflags> argument can contain |
6453 | C<SV_IMMEDIATE_UNREF> to force the reference count to be decremented | |
6454 | (otherwise the decrementing is conditional on the reference count being | |
6455 | different from one or the reference being a readonly SV). | |
7889fe52 | 6456 | See C<SvROK_off>. |
954c1994 GS |
6457 | |
6458 | =cut | |
6459 | */ | |
6460 | ||
ed6116ce | 6461 | void |
840a7b70 | 6462 | Perl_sv_unref_flags(pTHX_ SV *sv, U32 flags) |
ed6116ce | 6463 | { |
a0d0e21e | 6464 | SV* rv = SvRV(sv); |
810b8aa5 GS |
6465 | |
6466 | if (SvWEAKREF(sv)) { | |
6467 | sv_del_backref(sv); | |
6468 | SvWEAKREF_off(sv); | |
6469 | SvRV(sv) = 0; | |
6470 | return; | |
6471 | } | |
ed6116ce LW |
6472 | SvRV(sv) = 0; |
6473 | SvROK_off(sv); | |
840a7b70 | 6474 | if (SvREFCNT(rv) != 1 || SvREADONLY(rv) || flags) /* SV_IMMEDIATE_UNREF */ |
4633a7c4 | 6475 | SvREFCNT_dec(rv); |
840a7b70 | 6476 | else /* XXX Hack, but hard to make $a=$a->[1] work otherwise */ |
4633a7c4 | 6477 | sv_2mortal(rv); /* Schedule for freeing later */ |
ed6116ce | 6478 | } |
8990e307 | 6479 | |
840a7b70 IZ |
6480 | /* |
6481 | =for apidoc sv_unref | |
6482 | ||
6483 | Unsets the RV status of the SV, and decrements the reference count of | |
6484 | whatever was being referenced by the RV. This can almost be thought of | |
6485 | as a reversal of C<newSVrv>. This is C<sv_unref_flags> with the C<flag> | |
7889fe52 | 6486 | being zero. See C<SvROK_off>. |
840a7b70 IZ |
6487 | |
6488 | =cut | |
6489 | */ | |
6490 | ||
6491 | void | |
6492 | Perl_sv_unref(pTHX_ SV *sv) | |
6493 | { | |
6494 | sv_unref_flags(sv, 0); | |
6495 | } | |
6496 | ||
bbce6d69 | 6497 | void |
864dbfa3 | 6498 | Perl_sv_taint(pTHX_ SV *sv) |
bbce6d69 | 6499 | { |
6500 | sv_magic((sv), Nullsv, 't', Nullch, 0); | |
6501 | } | |
6502 | ||
6503 | void | |
864dbfa3 | 6504 | Perl_sv_untaint(pTHX_ SV *sv) |
bbce6d69 | 6505 | { |
13f57bf8 | 6506 | if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) { |
36477c24 | 6507 | MAGIC *mg = mg_find(sv, 't'); |
6508 | if (mg) | |
565764a8 | 6509 | mg->mg_len &= ~1; |
36477c24 | 6510 | } |
bbce6d69 | 6511 | } |
6512 | ||
6513 | bool | |
864dbfa3 | 6514 | Perl_sv_tainted(pTHX_ SV *sv) |
bbce6d69 | 6515 | { |
13f57bf8 | 6516 | if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) { |
36477c24 | 6517 | MAGIC *mg = mg_find(sv, 't'); |
155aba94 | 6518 | if (mg && ((mg->mg_len & 1) || ((mg->mg_len & 2) && mg->mg_obj == sv))) |
36477c24 | 6519 | return TRUE; |
6520 | } | |
6521 | return FALSE; | |
bbce6d69 | 6522 | } |
6523 | ||
954c1994 GS |
6524 | /* |
6525 | =for apidoc sv_setpviv | |
6526 | ||
6527 | Copies an integer into the given SV, also updating its string value. | |
6528 | Does not handle 'set' magic. See C<sv_setpviv_mg>. | |
6529 | ||
6530 | =cut | |
6531 | */ | |
6532 | ||
84902520 | 6533 | void |
864dbfa3 | 6534 | Perl_sv_setpviv(pTHX_ SV *sv, IV iv) |
84902520 | 6535 | { |
25da4f38 IZ |
6536 | char buf[TYPE_CHARS(UV)]; |
6537 | char *ebuf; | |
6538 | char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf); | |
84902520 | 6539 | |
25da4f38 | 6540 | sv_setpvn(sv, ptr, ebuf - ptr); |
84902520 TB |
6541 | } |
6542 | ||
ef50df4b | 6543 | |
954c1994 GS |
6544 | /* |
6545 | =for apidoc sv_setpviv_mg | |
6546 | ||
6547 | Like C<sv_setpviv>, but also handles 'set' magic. | |
6548 | ||
6549 | =cut | |
6550 | */ | |
6551 | ||
ef50df4b | 6552 | void |
864dbfa3 | 6553 | Perl_sv_setpviv_mg(pTHX_ SV *sv, IV iv) |
ef50df4b | 6554 | { |
25da4f38 IZ |
6555 | char buf[TYPE_CHARS(UV)]; |
6556 | char *ebuf; | |
6557 | char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf); | |
6558 | ||
6559 | sv_setpvn(sv, ptr, ebuf - ptr); | |
ef50df4b GS |
6560 | SvSETMAGIC(sv); |
6561 | } | |
6562 | ||
cea2e8a9 GS |
6563 | #if defined(PERL_IMPLICIT_CONTEXT) |
6564 | void | |
6565 | Perl_sv_setpvf_nocontext(SV *sv, const char* pat, ...) | |
6566 | { | |
6567 | dTHX; | |
6568 | va_list args; | |
6569 | va_start(args, pat); | |
c5be433b | 6570 | sv_vsetpvf(sv, pat, &args); |
cea2e8a9 GS |
6571 | va_end(args); |
6572 | } | |
6573 | ||
6574 | ||
6575 | void | |
6576 | Perl_sv_setpvf_mg_nocontext(SV *sv, const char* pat, ...) | |
6577 | { | |
6578 | dTHX; | |
6579 | va_list args; | |
6580 | va_start(args, pat); | |
c5be433b | 6581 | sv_vsetpvf_mg(sv, pat, &args); |
cea2e8a9 | 6582 | va_end(args); |
cea2e8a9 GS |
6583 | } |
6584 | #endif | |
6585 | ||
954c1994 GS |
6586 | /* |
6587 | =for apidoc sv_setpvf | |
6588 | ||
6589 | Processes its arguments like C<sprintf> and sets an SV to the formatted | |
6590 | output. Does not handle 'set' magic. See C<sv_setpvf_mg>. | |
6591 | ||
6592 | =cut | |
6593 | */ | |
6594 | ||
46fc3d4c | 6595 | void |
864dbfa3 | 6596 | Perl_sv_setpvf(pTHX_ SV *sv, const char* pat, ...) |
46fc3d4c | 6597 | { |
6598 | va_list args; | |
46fc3d4c | 6599 | va_start(args, pat); |
c5be433b | 6600 | sv_vsetpvf(sv, pat, &args); |
46fc3d4c | 6601 | va_end(args); |
6602 | } | |
6603 | ||
c5be433b GS |
6604 | void |
6605 | Perl_sv_vsetpvf(pTHX_ SV *sv, const char* pat, va_list* args) | |
6606 | { | |
6607 | sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
6608 | } | |
ef50df4b | 6609 | |
954c1994 GS |
6610 | /* |
6611 | =for apidoc sv_setpvf_mg | |
6612 | ||
6613 | Like C<sv_setpvf>, but also handles 'set' magic. | |
6614 | ||
6615 | =cut | |
6616 | */ | |
6617 | ||
ef50df4b | 6618 | void |
864dbfa3 | 6619 | Perl_sv_setpvf_mg(pTHX_ SV *sv, const char* pat, ...) |
ef50df4b GS |
6620 | { |
6621 | va_list args; | |
ef50df4b | 6622 | va_start(args, pat); |
c5be433b | 6623 | sv_vsetpvf_mg(sv, pat, &args); |
ef50df4b | 6624 | va_end(args); |
c5be433b GS |
6625 | } |
6626 | ||
6627 | void | |
6628 | Perl_sv_vsetpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args) | |
6629 | { | |
6630 | sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
ef50df4b GS |
6631 | SvSETMAGIC(sv); |
6632 | } | |
6633 | ||
cea2e8a9 GS |
6634 | #if defined(PERL_IMPLICIT_CONTEXT) |
6635 | void | |
6636 | Perl_sv_catpvf_nocontext(SV *sv, const char* pat, ...) | |
6637 | { | |
6638 | dTHX; | |
6639 | va_list args; | |
6640 | va_start(args, pat); | |
c5be433b | 6641 | sv_vcatpvf(sv, pat, &args); |
cea2e8a9 GS |
6642 | va_end(args); |
6643 | } | |
6644 | ||
6645 | void | |
6646 | Perl_sv_catpvf_mg_nocontext(SV *sv, const char* pat, ...) | |
6647 | { | |
6648 | dTHX; | |
6649 | va_list args; | |
6650 | va_start(args, pat); | |
c5be433b | 6651 | sv_vcatpvf_mg(sv, pat, &args); |
cea2e8a9 | 6652 | va_end(args); |
cea2e8a9 GS |
6653 | } |
6654 | #endif | |
6655 | ||
954c1994 GS |
6656 | /* |
6657 | =for apidoc sv_catpvf | |
6658 | ||
6659 | Processes its arguments like C<sprintf> and appends the formatted output | |
6660 | to an SV. Handles 'get' magic, but not 'set' magic. C<SvSETMAGIC()> must | |
6661 | typically be called after calling this function to handle 'set' magic. | |
6662 | ||
6663 | =cut | |
6664 | */ | |
6665 | ||
46fc3d4c | 6666 | void |
864dbfa3 | 6667 | Perl_sv_catpvf(pTHX_ SV *sv, const char* pat, ...) |
46fc3d4c | 6668 | { |
6669 | va_list args; | |
46fc3d4c | 6670 | va_start(args, pat); |
c5be433b | 6671 | sv_vcatpvf(sv, pat, &args); |
46fc3d4c | 6672 | va_end(args); |
6673 | } | |
6674 | ||
ef50df4b | 6675 | void |
c5be433b GS |
6676 | Perl_sv_vcatpvf(pTHX_ SV *sv, const char* pat, va_list* args) |
6677 | { | |
6678 | sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
6679 | } | |
6680 | ||
954c1994 GS |
6681 | /* |
6682 | =for apidoc sv_catpvf_mg | |
6683 | ||
6684 | Like C<sv_catpvf>, but also handles 'set' magic. | |
6685 | ||
6686 | =cut | |
6687 | */ | |
6688 | ||
c5be433b | 6689 | void |
864dbfa3 | 6690 | Perl_sv_catpvf_mg(pTHX_ SV *sv, const char* pat, ...) |
ef50df4b GS |
6691 | { |
6692 | va_list args; | |
ef50df4b | 6693 | va_start(args, pat); |
c5be433b | 6694 | sv_vcatpvf_mg(sv, pat, &args); |
ef50df4b | 6695 | va_end(args); |
c5be433b GS |
6696 | } |
6697 | ||
6698 | void | |
6699 | Perl_sv_vcatpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args) | |
6700 | { | |
6701 | sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
ef50df4b GS |
6702 | SvSETMAGIC(sv); |
6703 | } | |
6704 | ||
954c1994 GS |
6705 | /* |
6706 | =for apidoc sv_vsetpvfn | |
6707 | ||
6708 | Works like C<vcatpvfn> but copies the text into the SV instead of | |
6709 | appending it. | |
6710 | ||
6711 | =cut | |
6712 | */ | |
6713 | ||
46fc3d4c | 6714 | void |
7d5ea4e7 | 6715 | Perl_sv_vsetpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted) |
46fc3d4c | 6716 | { |
6717 | sv_setpvn(sv, "", 0); | |
7d5ea4e7 | 6718 | sv_vcatpvfn(sv, pat, patlen, args, svargs, svmax, maybe_tainted); |
46fc3d4c | 6719 | } |
6720 | ||
211dfcf1 | 6721 | I32 |
9dd79c3f | 6722 | S_expect_number(pTHX_ char** pattern) |
211dfcf1 HS |
6723 | { |
6724 | I32 var = 0; | |
6725 | switch (**pattern) { | |
6726 | case '1': case '2': case '3': | |
6727 | case '4': case '5': case '6': | |
6728 | case '7': case '8': case '9': | |
6729 | while (isDIGIT(**pattern)) | |
6730 | var = var * 10 + (*(*pattern)++ - '0'); | |
6731 | } | |
6732 | return var; | |
6733 | } | |
9dd79c3f | 6734 | #define EXPECT_NUMBER(pattern, var) (var = S_expect_number(aTHX_ &pattern)) |
211dfcf1 | 6735 | |
954c1994 GS |
6736 | /* |
6737 | =for apidoc sv_vcatpvfn | |
6738 | ||
6739 | Processes its arguments like C<vsprintf> and appends the formatted output | |
6740 | to an SV. Uses an array of SVs if the C style variable argument list is | |
6741 | missing (NULL). When running with taint checks enabled, indicates via | |
6742 | C<maybe_tainted> if results are untrustworthy (often due to the use of | |
6743 | locales). | |
6744 | ||
6745 | =cut | |
6746 | */ | |
6747 | ||
46fc3d4c | 6748 | void |
7d5ea4e7 | 6749 | Perl_sv_vcatpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted) |
46fc3d4c | 6750 | { |
6751 | char *p; | |
6752 | char *q; | |
6753 | char *patend; | |
fc36a67e | 6754 | STRLEN origlen; |
46fc3d4c | 6755 | I32 svix = 0; |
c635e13b | 6756 | static char nullstr[] = "(null)"; |
7e2040f0 | 6757 | SV *argsv; |
46fc3d4c | 6758 | |
6759 | /* no matter what, this is a string now */ | |
fc36a67e | 6760 | (void)SvPV_force(sv, origlen); |
46fc3d4c | 6761 | |
fc36a67e | 6762 | /* special-case "", "%s", and "%_" */ |
46fc3d4c | 6763 | if (patlen == 0) |
6764 | return; | |
fc36a67e | 6765 | if (patlen == 2 && pat[0] == '%') { |
6766 | switch (pat[1]) { | |
6767 | case 's': | |
c635e13b | 6768 | if (args) { |
6769 | char *s = va_arg(*args, char*); | |
6770 | sv_catpv(sv, s ? s : nullstr); | |
6771 | } | |
7e2040f0 | 6772 | else if (svix < svmax) { |
fc36a67e | 6773 | sv_catsv(sv, *svargs); |
7e2040f0 GS |
6774 | if (DO_UTF8(*svargs)) |
6775 | SvUTF8_on(sv); | |
6776 | } | |
fc36a67e | 6777 | return; |
6778 | case '_': | |
6779 | if (args) { | |
7e2040f0 GS |
6780 | argsv = va_arg(*args, SV*); |
6781 | sv_catsv(sv, argsv); | |
6782 | if (DO_UTF8(argsv)) | |
6783 | SvUTF8_on(sv); | |
fc36a67e | 6784 | return; |
6785 | } | |
6786 | /* See comment on '_' below */ | |
6787 | break; | |
6788 | } | |
46fc3d4c | 6789 | } |
6790 | ||
6791 | patend = (char*)pat + patlen; | |
6792 | for (p = (char*)pat; p < patend; p = q) { | |
6793 | bool alt = FALSE; | |
6794 | bool left = FALSE; | |
b22c7a20 | 6795 | bool vectorize = FALSE; |
211dfcf1 | 6796 | bool vectorarg = FALSE; |
b2e23cf9 | 6797 | bool vec_utf = FALSE; |
46fc3d4c | 6798 | char fill = ' '; |
6799 | char plus = 0; | |
6800 | char intsize = 0; | |
6801 | STRLEN width = 0; | |
fc36a67e | 6802 | STRLEN zeros = 0; |
46fc3d4c | 6803 | bool has_precis = FALSE; |
6804 | STRLEN precis = 0; | |
7e2040f0 | 6805 | bool is_utf = FALSE; |
eb3fce90 | 6806 | |
46fc3d4c | 6807 | char esignbuf[4]; |
ad391ad9 | 6808 | U8 utf8buf[UTF8_MAXLEN+1]; |
46fc3d4c | 6809 | STRLEN esignlen = 0; |
6810 | ||
6811 | char *eptr = Nullch; | |
fc36a67e | 6812 | STRLEN elen = 0; |
089c015b JH |
6813 | /* Times 4: a decimal digit takes more than 3 binary digits. |
6814 | * NV_DIG: mantissa takes than many decimal digits. | |
6815 | * Plus 32: Playing safe. */ | |
6816 | char ebuf[IV_DIG * 4 + NV_DIG + 32]; | |
2d4389e4 JH |
6817 | /* large enough for "%#.#f" --chip */ |
6818 | /* what about long double NVs? --jhi */ | |
b22c7a20 GS |
6819 | |
6820 | SV *vecsv; | |
a05b299f | 6821 | U8 *vecstr = Null(U8*); |
b22c7a20 | 6822 | STRLEN veclen = 0; |
46fc3d4c | 6823 | char c; |
6824 | int i; | |
6825 | unsigned base; | |
6826 | IV iv; | |
6827 | UV uv; | |
65202027 | 6828 | NV nv; |
46fc3d4c | 6829 | STRLEN have; |
6830 | STRLEN need; | |
6831 | STRLEN gap; | |
b22c7a20 GS |
6832 | char *dotstr = "."; |
6833 | STRLEN dotstrlen = 1; | |
211dfcf1 | 6834 | I32 efix = 0; /* explicit format parameter index */ |
eb3fce90 | 6835 | I32 ewix = 0; /* explicit width index */ |
211dfcf1 HS |
6836 | I32 epix = 0; /* explicit precision index */ |
6837 | I32 evix = 0; /* explicit vector index */ | |
eb3fce90 | 6838 | bool asterisk = FALSE; |
46fc3d4c | 6839 | |
211dfcf1 | 6840 | /* echo everything up to the next format specification */ |
46fc3d4c | 6841 | for (q = p; q < patend && *q != '%'; ++q) ; |
6842 | if (q > p) { | |
6843 | sv_catpvn(sv, p, q - p); | |
6844 | p = q; | |
6845 | } | |
6846 | if (q++ >= patend) | |
6847 | break; | |
6848 | ||
211dfcf1 HS |
6849 | /* |
6850 | We allow format specification elements in this order: | |
6851 | \d+\$ explicit format parameter index | |
6852 | [-+ 0#]+ flags | |
6853 | \*?(\d+\$)?v vector with optional (optionally specified) arg | |
6854 | \d+|\*(\d+\$)? width using optional (optionally specified) arg | |
6855 | \.(\d*|\*(\d+\$)?) precision using optional (optionally specified) arg | |
6856 | [hlqLV] size | |
6857 | [%bcdefginopsux_DFOUX] format (mandatory) | |
6858 | */ | |
6859 | if (EXPECT_NUMBER(q, width)) { | |
6860 | if (*q == '$') { | |
6861 | ++q; | |
6862 | efix = width; | |
6863 | } else { | |
6864 | goto gotwidth; | |
6865 | } | |
6866 | } | |
6867 | ||
fc36a67e | 6868 | /* FLAGS */ |
6869 | ||
46fc3d4c | 6870 | while (*q) { |
6871 | switch (*q) { | |
6872 | case ' ': | |
6873 | case '+': | |
6874 | plus = *q++; | |
6875 | continue; | |
6876 | ||
6877 | case '-': | |
6878 | left = TRUE; | |
6879 | q++; | |
6880 | continue; | |
6881 | ||
6882 | case '0': | |
6883 | fill = *q++; | |
6884 | continue; | |
6885 | ||
6886 | case '#': | |
6887 | alt = TRUE; | |
6888 | q++; | |
6889 | continue; | |
6890 | ||
fc36a67e | 6891 | default: |
6892 | break; | |
6893 | } | |
6894 | break; | |
6895 | } | |
46fc3d4c | 6896 | |
211dfcf1 | 6897 | tryasterisk: |
eb3fce90 | 6898 | if (*q == '*') { |
211dfcf1 HS |
6899 | q++; |
6900 | if (EXPECT_NUMBER(q, ewix)) | |
6901 | if (*q++ != '$') | |
6902 | goto unknown; | |
eb3fce90 | 6903 | asterisk = TRUE; |
211dfcf1 HS |
6904 | } |
6905 | if (*q == 'v') { | |
eb3fce90 | 6906 | q++; |
211dfcf1 HS |
6907 | if (vectorize) |
6908 | goto unknown; | |
6909 | if (vectorarg = asterisk) { | |
6910 | evix = ewix; | |
6911 | ewix = 0; | |
6912 | asterisk = FALSE; | |
6913 | } | |
6914 | vectorize = TRUE; | |
6915 | goto tryasterisk; | |
eb3fce90 JH |
6916 | } |
6917 | ||
211dfcf1 HS |
6918 | if (!asterisk) |
6919 | EXPECT_NUMBER(q, width); | |
6920 | ||
6921 | if (vectorize) { | |
6922 | if (vectorarg) { | |
6923 | if (args) | |
6924 | vecsv = va_arg(*args, SV*); | |
6925 | else | |
6926 | vecsv = (evix ? evix <= svmax : svix < svmax) ? | |
6927 | svargs[ewix ? ewix-1 : svix++] : &PL_sv_undef; | |
4459522c | 6928 | dotstr = SvPVx(vecsv, dotstrlen); |
211dfcf1 HS |
6929 | if (DO_UTF8(vecsv)) |
6930 | is_utf = TRUE; | |
6931 | } | |
6932 | if (args) { | |
6933 | vecsv = va_arg(*args, SV*); | |
6934 | vecstr = (U8*)SvPVx(vecsv,veclen); | |
b2e23cf9 | 6935 | vec_utf = DO_UTF8(vecsv); |
eb3fce90 | 6936 | } |
211dfcf1 HS |
6937 | else if (efix ? efix <= svmax : svix < svmax) { |
6938 | vecsv = svargs[efix ? efix-1 : svix++]; | |
6939 | vecstr = (U8*)SvPVx(vecsv,veclen); | |
b2e23cf9 | 6940 | vec_utf = DO_UTF8(vecsv); |
211dfcf1 HS |
6941 | } |
6942 | else { | |
6943 | vecstr = (U8*)""; | |
6944 | veclen = 0; | |
6945 | } | |
eb3fce90 | 6946 | } |
fc36a67e | 6947 | |
eb3fce90 | 6948 | if (asterisk) { |
fc36a67e | 6949 | if (args) |
6950 | i = va_arg(*args, int); | |
6951 | else | |
eb3fce90 JH |
6952 | i = (ewix ? ewix <= svmax : svix < svmax) ? |
6953 | SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0; | |
fc36a67e | 6954 | left |= (i < 0); |
6955 | width = (i < 0) ? -i : i; | |
fc36a67e | 6956 | } |
211dfcf1 | 6957 | gotwidth: |
fc36a67e | 6958 | |
6959 | /* PRECISION */ | |
46fc3d4c | 6960 | |
fc36a67e | 6961 | if (*q == '.') { |
6962 | q++; | |
6963 | if (*q == '*') { | |
211dfcf1 HS |
6964 | q++; |
6965 | if (EXPECT_NUMBER(q, epix) && *q++ != '$') | |
6966 | goto unknown; | |
46fc3d4c | 6967 | if (args) |
6968 | i = va_arg(*args, int); | |
6969 | else | |
eb3fce90 JH |
6970 | i = (ewix ? ewix <= svmax : svix < svmax) |
6971 | ? SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0; | |
fc36a67e | 6972 | precis = (i < 0) ? 0 : i; |
fc36a67e | 6973 | } |
6974 | else { | |
6975 | precis = 0; | |
6976 | while (isDIGIT(*q)) | |
6977 | precis = precis * 10 + (*q++ - '0'); | |
6978 | } | |
6979 | has_precis = TRUE; | |
6980 | } | |
46fc3d4c | 6981 | |
fc36a67e | 6982 | /* SIZE */ |
46fc3d4c | 6983 | |
fc36a67e | 6984 | switch (*q) { |
e5c81feb | 6985 | #if defined(HAS_QUAD) || (defined(HAS_LONG_DOUBLE) && defined(USE_LONG_DOUBLE)) |
6f9bb7fd | 6986 | case 'L': /* Ld */ |
e5c81feb JH |
6987 | /* FALL THROUGH */ |
6988 | #endif | |
6989 | #ifdef HAS_QUAD | |
6f9bb7fd GS |
6990 | case 'q': /* qd */ |
6991 | intsize = 'q'; | |
6992 | q++; | |
6993 | break; | |
6994 | #endif | |
fc36a67e | 6995 | case 'l': |
e5c81feb JH |
6996 | #if defined(HAS_QUAD) || (defined(HAS_LONG_DOUBLE) && defined(USE_LONG_DOUBLE)) |
6997 | if (*(q + 1) == 'l') { /* lld, llf */ | |
fc36a67e | 6998 | intsize = 'q'; |
6999 | q += 2; | |
46fc3d4c | 7000 | break; |
cf2093f6 | 7001 | } |
fc36a67e | 7002 | #endif |
6f9bb7fd | 7003 | /* FALL THROUGH */ |
fc36a67e | 7004 | case 'h': |
cf2093f6 | 7005 | /* FALL THROUGH */ |
fc36a67e | 7006 | case 'V': |
7007 | intsize = *q++; | |
46fc3d4c | 7008 | break; |
7009 | } | |
7010 | ||
fc36a67e | 7011 | /* CONVERSION */ |
7012 | ||
211dfcf1 HS |
7013 | if (*q == '%') { |
7014 | eptr = q++; | |
7015 | elen = 1; | |
7016 | goto string; | |
7017 | } | |
7018 | ||
7019 | if (!args) | |
7020 | argsv = (efix ? efix <= svmax : svix < svmax) ? | |
7021 | svargs[efix ? efix-1 : svix++] : &PL_sv_undef; | |
7022 | ||
46fc3d4c | 7023 | switch (c = *q++) { |
7024 | ||
7025 | /* STRINGS */ | |
7026 | ||
46fc3d4c | 7027 | case 'c': |
211dfcf1 | 7028 | uv = args ? va_arg(*args, int) : SvIVx(argsv); |
3969a896 | 7029 | if ((uv > 255 || (uv > 127 && SvUTF8(sv))) && !IN_BYTE) { |
dfe13c55 GS |
7030 | eptr = (char*)utf8buf; |
7031 | elen = uv_to_utf8((U8*)eptr, uv) - utf8buf; | |
7e2040f0 GS |
7032 | is_utf = TRUE; |
7033 | } | |
7034 | else { | |
7035 | c = (char)uv; | |
7036 | eptr = &c; | |
7037 | elen = 1; | |
a0ed51b3 | 7038 | } |
46fc3d4c | 7039 | goto string; |
7040 | ||
46fc3d4c | 7041 | case 's': |
7042 | if (args) { | |
fc36a67e | 7043 | eptr = va_arg(*args, char*); |
c635e13b | 7044 | if (eptr) |
1d7c1841 GS |
7045 | #ifdef MACOS_TRADITIONAL |
7046 | /* On MacOS, %#s format is used for Pascal strings */ | |
7047 | if (alt) | |
7048 | elen = *eptr++; | |
7049 | else | |
7050 | #endif | |
c635e13b | 7051 | elen = strlen(eptr); |
7052 | else { | |
7053 | eptr = nullstr; | |
7054 | elen = sizeof nullstr - 1; | |
7055 | } | |
46fc3d4c | 7056 | } |
211dfcf1 | 7057 | else { |
7e2040f0 GS |
7058 | eptr = SvPVx(argsv, elen); |
7059 | if (DO_UTF8(argsv)) { | |
a0ed51b3 LW |
7060 | if (has_precis && precis < elen) { |
7061 | I32 p = precis; | |
7e2040f0 | 7062 | sv_pos_u2b(argsv, &p, 0); /* sticks at end */ |
a0ed51b3 LW |
7063 | precis = p; |
7064 | } | |
7065 | if (width) { /* fudge width (can't fudge elen) */ | |
7e2040f0 | 7066 | width += elen - sv_len_utf8(argsv); |
a0ed51b3 | 7067 | } |
7e2040f0 | 7068 | is_utf = TRUE; |
a0ed51b3 LW |
7069 | } |
7070 | } | |
46fc3d4c | 7071 | goto string; |
7072 | ||
fc36a67e | 7073 | case '_': |
7074 | /* | |
7075 | * The "%_" hack might have to be changed someday, | |
7076 | * if ISO or ANSI decide to use '_' for something. | |
7077 | * So we keep it hidden from users' code. | |
7078 | */ | |
7079 | if (!args) | |
7080 | goto unknown; | |
211dfcf1 | 7081 | argsv = va_arg(*args, SV*); |
7e2040f0 GS |
7082 | eptr = SvPVx(argsv, elen); |
7083 | if (DO_UTF8(argsv)) | |
7084 | is_utf = TRUE; | |
fc36a67e | 7085 | |
46fc3d4c | 7086 | string: |
b22c7a20 | 7087 | vectorize = FALSE; |
46fc3d4c | 7088 | if (has_precis && elen > precis) |
7089 | elen = precis; | |
7090 | break; | |
7091 | ||
7092 | /* INTEGERS */ | |
7093 | ||
fc36a67e | 7094 | case 'p': |
c2e66d9e GS |
7095 | if (alt) |
7096 | goto unknown; | |
211dfcf1 | 7097 | uv = PTR2UV(args ? va_arg(*args, void*) : argsv); |
fc36a67e | 7098 | base = 16; |
7099 | goto integer; | |
7100 | ||
46fc3d4c | 7101 | case 'D': |
29fe7a80 | 7102 | #ifdef IV_IS_QUAD |
22f3ae8c | 7103 | intsize = 'q'; |
29fe7a80 | 7104 | #else |
46fc3d4c | 7105 | intsize = 'l'; |
29fe7a80 | 7106 | #endif |
46fc3d4c | 7107 | /* FALL THROUGH */ |
7108 | case 'd': | |
7109 | case 'i': | |
b22c7a20 | 7110 | if (vectorize) { |
ba210ebe | 7111 | STRLEN ulen; |
211dfcf1 HS |
7112 | if (!veclen) |
7113 | continue; | |
b2e23cf9 | 7114 | if (vec_utf) |
dcad2880 | 7115 | iv = (IV)utf8_to_uv(vecstr, veclen, &ulen, 0); |
b22c7a20 | 7116 | else { |
a05b299f | 7117 | iv = *vecstr; |
b22c7a20 GS |
7118 | ulen = 1; |
7119 | } | |
7120 | vecstr += ulen; | |
7121 | veclen -= ulen; | |
7122 | } | |
7123 | else if (args) { | |
46fc3d4c | 7124 | switch (intsize) { |
7125 | case 'h': iv = (short)va_arg(*args, int); break; | |
7126 | default: iv = va_arg(*args, int); break; | |
7127 | case 'l': iv = va_arg(*args, long); break; | |
fc36a67e | 7128 | case 'V': iv = va_arg(*args, IV); break; |
cf2093f6 JH |
7129 | #ifdef HAS_QUAD |
7130 | case 'q': iv = va_arg(*args, Quad_t); break; | |
7131 | #endif | |
46fc3d4c | 7132 | } |
7133 | } | |
7134 | else { | |
211dfcf1 | 7135 | iv = SvIVx(argsv); |
46fc3d4c | 7136 | switch (intsize) { |
7137 | case 'h': iv = (short)iv; break; | |
be28567c | 7138 | default: break; |
46fc3d4c | 7139 | case 'l': iv = (long)iv; break; |
fc36a67e | 7140 | case 'V': break; |
cf2093f6 JH |
7141 | #ifdef HAS_QUAD |
7142 | case 'q': iv = (Quad_t)iv; break; | |
7143 | #endif | |
46fc3d4c | 7144 | } |
7145 | } | |
7146 | if (iv >= 0) { | |
7147 | uv = iv; | |
7148 | if (plus) | |
7149 | esignbuf[esignlen++] = plus; | |
7150 | } | |
7151 | else { | |
7152 | uv = -iv; | |
7153 | esignbuf[esignlen++] = '-'; | |
7154 | } | |
7155 | base = 10; | |
7156 | goto integer; | |
7157 | ||
fc36a67e | 7158 | case 'U': |
29fe7a80 | 7159 | #ifdef IV_IS_QUAD |
22f3ae8c | 7160 | intsize = 'q'; |
29fe7a80 | 7161 | #else |
fc36a67e | 7162 | intsize = 'l'; |
29fe7a80 | 7163 | #endif |
fc36a67e | 7164 | /* FALL THROUGH */ |
7165 | case 'u': | |
7166 | base = 10; | |
7167 | goto uns_integer; | |
7168 | ||
4f19785b WSI |
7169 | case 'b': |
7170 | base = 2; | |
7171 | goto uns_integer; | |
7172 | ||
46fc3d4c | 7173 | case 'O': |
29fe7a80 | 7174 | #ifdef IV_IS_QUAD |
22f3ae8c | 7175 | intsize = 'q'; |
29fe7a80 | 7176 | #else |
46fc3d4c | 7177 | intsize = 'l'; |
29fe7a80 | 7178 | #endif |
46fc3d4c | 7179 | /* FALL THROUGH */ |
7180 | case 'o': | |
7181 | base = 8; | |
7182 | goto uns_integer; | |
7183 | ||
7184 | case 'X': | |
46fc3d4c | 7185 | case 'x': |
7186 | base = 16; | |
46fc3d4c | 7187 | |
7188 | uns_integer: | |
b22c7a20 | 7189 | if (vectorize) { |
ba210ebe | 7190 | STRLEN ulen; |
b22c7a20 | 7191 | vector: |
211dfcf1 HS |
7192 | if (!veclen) |
7193 | continue; | |
b2e23cf9 | 7194 | if (vec_utf) |
dcad2880 | 7195 | uv = utf8_to_uv(vecstr, veclen, &ulen, 0); |
b22c7a20 | 7196 | else { |
a05b299f | 7197 | uv = *vecstr; |
b22c7a20 GS |
7198 | ulen = 1; |
7199 | } | |
7200 | vecstr += ulen; | |
7201 | veclen -= ulen; | |
7202 | } | |
7203 | else if (args) { | |
46fc3d4c | 7204 | switch (intsize) { |
7205 | case 'h': uv = (unsigned short)va_arg(*args, unsigned); break; | |
7206 | default: uv = va_arg(*args, unsigned); break; | |
7207 | case 'l': uv = va_arg(*args, unsigned long); break; | |
fc36a67e | 7208 | case 'V': uv = va_arg(*args, UV); break; |
cf2093f6 JH |
7209 | #ifdef HAS_QUAD |
7210 | case 'q': uv = va_arg(*args, Quad_t); break; | |
7211 | #endif | |
46fc3d4c | 7212 | } |
7213 | } | |
7214 | else { | |
211dfcf1 | 7215 | uv = SvUVx(argsv); |
46fc3d4c | 7216 | switch (intsize) { |
7217 | case 'h': uv = (unsigned short)uv; break; | |
be28567c | 7218 | default: break; |
46fc3d4c | 7219 | case 'l': uv = (unsigned long)uv; break; |
fc36a67e | 7220 | case 'V': break; |
cf2093f6 JH |
7221 | #ifdef HAS_QUAD |
7222 | case 'q': uv = (Quad_t)uv; break; | |
7223 | #endif | |
46fc3d4c | 7224 | } |
7225 | } | |
7226 | ||
7227 | integer: | |
46fc3d4c | 7228 | eptr = ebuf + sizeof ebuf; |
fc36a67e | 7229 | switch (base) { |
7230 | unsigned dig; | |
7231 | case 16: | |
c10ed8b9 HS |
7232 | if (!uv) |
7233 | alt = FALSE; | |
1d7c1841 GS |
7234 | p = (char*)((c == 'X') |
7235 | ? "0123456789ABCDEF" : "0123456789abcdef"); | |
fc36a67e | 7236 | do { |
7237 | dig = uv & 15; | |
7238 | *--eptr = p[dig]; | |
7239 | } while (uv >>= 4); | |
7240 | if (alt) { | |
46fc3d4c | 7241 | esignbuf[esignlen++] = '0'; |
fc36a67e | 7242 | esignbuf[esignlen++] = c; /* 'x' or 'X' */ |
46fc3d4c | 7243 | } |
fc36a67e | 7244 | break; |
7245 | case 8: | |
7246 | do { | |
7247 | dig = uv & 7; | |
7248 | *--eptr = '0' + dig; | |
7249 | } while (uv >>= 3); | |
7250 | if (alt && *eptr != '0') | |
7251 | *--eptr = '0'; | |
7252 | break; | |
4f19785b WSI |
7253 | case 2: |
7254 | do { | |
7255 | dig = uv & 1; | |
7256 | *--eptr = '0' + dig; | |
7257 | } while (uv >>= 1); | |
eda88b6d JH |
7258 | if (alt) { |
7259 | esignbuf[esignlen++] = '0'; | |
7481bb52 | 7260 | esignbuf[esignlen++] = 'b'; |
eda88b6d | 7261 | } |
4f19785b | 7262 | break; |
fc36a67e | 7263 | default: /* it had better be ten or less */ |
6bc102ca | 7264 | #if defined(PERL_Y2KWARN) |
e476b1b5 | 7265 | if (ckWARN(WARN_Y2K)) { |
6bc102ca GS |
7266 | STRLEN n; |
7267 | char *s = SvPV(sv,n); | |
7268 | if (n >= 2 && s[n-2] == '1' && s[n-1] == '9' | |
7269 | && (n == 2 || !isDIGIT(s[n-3]))) | |
7270 | { | |
e476b1b5 | 7271 | Perl_warner(aTHX_ WARN_Y2K, |
6bc102ca GS |
7272 | "Possible Y2K bug: %%%c %s", |
7273 | c, "format string following '19'"); | |
7274 | } | |
7275 | } | |
7276 | #endif | |
fc36a67e | 7277 | do { |
7278 | dig = uv % base; | |
7279 | *--eptr = '0' + dig; | |
7280 | } while (uv /= base); | |
7281 | break; | |
46fc3d4c | 7282 | } |
7283 | elen = (ebuf + sizeof ebuf) - eptr; | |
c10ed8b9 HS |
7284 | if (has_precis) { |
7285 | if (precis > elen) | |
7286 | zeros = precis - elen; | |
7287 | else if (precis == 0 && elen == 1 && *eptr == '0') | |
7288 | elen = 0; | |
7289 | } | |
46fc3d4c | 7290 | break; |
7291 | ||
7292 | /* FLOATING POINT */ | |
7293 | ||
fc36a67e | 7294 | case 'F': |
7295 | c = 'f'; /* maybe %F isn't supported here */ | |
7296 | /* FALL THROUGH */ | |
46fc3d4c | 7297 | case 'e': case 'E': |
fc36a67e | 7298 | case 'f': |
46fc3d4c | 7299 | case 'g': case 'G': |
7300 | ||
7301 | /* This is evil, but floating point is even more evil */ | |
7302 | ||
b22c7a20 | 7303 | vectorize = FALSE; |
211dfcf1 | 7304 | nv = args ? va_arg(*args, NV) : SvNVx(argsv); |
fc36a67e | 7305 | |
7306 | need = 0; | |
7307 | if (c != 'e' && c != 'E') { | |
7308 | i = PERL_INT_MIN; | |
73b309ea | 7309 | (void)Perl_frexp(nv, &i); |
fc36a67e | 7310 | if (i == PERL_INT_MIN) |
cea2e8a9 | 7311 | Perl_die(aTHX_ "panic: frexp"); |
c635e13b | 7312 | if (i > 0) |
fc36a67e | 7313 | need = BIT_DIGITS(i); |
7314 | } | |
7315 | need += has_precis ? precis : 6; /* known default */ | |
7316 | if (need < width) | |
7317 | need = width; | |
7318 | ||
46fc3d4c | 7319 | need += 20; /* fudge factor */ |
80252599 GS |
7320 | if (PL_efloatsize < need) { |
7321 | Safefree(PL_efloatbuf); | |
7322 | PL_efloatsize = need + 20; /* more fudge */ | |
7323 | New(906, PL_efloatbuf, PL_efloatsize, char); | |
7d5ea4e7 | 7324 | PL_efloatbuf[0] = '\0'; |
46fc3d4c | 7325 | } |
7326 | ||
7327 | eptr = ebuf + sizeof ebuf; | |
7328 | *--eptr = '\0'; | |
7329 | *--eptr = c; | |
e5c81feb | 7330 | #if defined(USE_LONG_DOUBLE) && defined(PERL_PRIfldbl) |
cf2093f6 | 7331 | { |
e5c81feb JH |
7332 | /* Copy the one or more characters in a long double |
7333 | * format before the 'base' ([efgEFG]) character to | |
7334 | * the format string. */ | |
7335 | static char const prifldbl[] = PERL_PRIfldbl; | |
7336 | char const *p = prifldbl + sizeof(prifldbl) - 3; | |
7337 | while (p >= prifldbl) { *--eptr = *p--; } | |
cf2093f6 | 7338 | } |
65202027 | 7339 | #endif |
46fc3d4c | 7340 | if (has_precis) { |
7341 | base = precis; | |
7342 | do { *--eptr = '0' + (base % 10); } while (base /= 10); | |
7343 | *--eptr = '.'; | |
7344 | } | |
7345 | if (width) { | |
7346 | base = width; | |
7347 | do { *--eptr = '0' + (base % 10); } while (base /= 10); | |
7348 | } | |
7349 | if (fill == '0') | |
7350 | *--eptr = fill; | |
84902520 TB |
7351 | if (left) |
7352 | *--eptr = '-'; | |
46fc3d4c | 7353 | if (plus) |
7354 | *--eptr = plus; | |
7355 | if (alt) | |
7356 | *--eptr = '#'; | |
7357 | *--eptr = '%'; | |
7358 | ||
ff9121f8 JH |
7359 | /* No taint. Otherwise we are in the strange situation |
7360 | * where printf() taints but print($float) doesn't. | |
bda0f7a5 | 7361 | * --jhi */ |
dd8482fc | 7362 | (void)sprintf(PL_efloatbuf, eptr, nv); |
8af02333 | 7363 | |
80252599 GS |
7364 | eptr = PL_efloatbuf; |
7365 | elen = strlen(PL_efloatbuf); | |
46fc3d4c | 7366 | break; |
7367 | ||
fc36a67e | 7368 | /* SPECIAL */ |
7369 | ||
7370 | case 'n': | |
b22c7a20 | 7371 | vectorize = FALSE; |
fc36a67e | 7372 | i = SvCUR(sv) - origlen; |
7373 | if (args) { | |
c635e13b | 7374 | switch (intsize) { |
7375 | case 'h': *(va_arg(*args, short*)) = i; break; | |
7376 | default: *(va_arg(*args, int*)) = i; break; | |
7377 | case 'l': *(va_arg(*args, long*)) = i; break; | |
7378 | case 'V': *(va_arg(*args, IV*)) = i; break; | |
cf2093f6 JH |
7379 | #ifdef HAS_QUAD |
7380 | case 'q': *(va_arg(*args, Quad_t*)) = i; break; | |
7381 | #endif | |
c635e13b | 7382 | } |
fc36a67e | 7383 | } |
9dd79c3f | 7384 | else |
211dfcf1 | 7385 | sv_setuv_mg(argsv, (UV)i); |
fc36a67e | 7386 | continue; /* not "break" */ |
7387 | ||
7388 | /* UNKNOWN */ | |
7389 | ||
46fc3d4c | 7390 | default: |
fc36a67e | 7391 | unknown: |
b22c7a20 | 7392 | vectorize = FALSE; |
599cee73 | 7393 | if (!args && ckWARN(WARN_PRINTF) && |
533c011a | 7394 | (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF)) { |
c635e13b | 7395 | SV *msg = sv_newmortal(); |
cea2e8a9 | 7396 | Perl_sv_setpvf(aTHX_ msg, "Invalid conversion in %s: ", |
533c011a | 7397 | (PL_op->op_type == OP_PRTF) ? "printf" : "sprintf"); |
0f4b6630 | 7398 | if (c) { |
0f4b6630 | 7399 | if (isPRINT(c)) |
1c846c1f | 7400 | Perl_sv_catpvf(aTHX_ msg, |
0f4b6630 JH |
7401 | "\"%%%c\"", c & 0xFF); |
7402 | else | |
7403 | Perl_sv_catpvf(aTHX_ msg, | |
57def98f | 7404 | "\"%%\\%03"UVof"\"", |
0f4b6630 | 7405 | (UV)c & 0xFF); |
0f4b6630 | 7406 | } else |
c635e13b | 7407 | sv_catpv(msg, "end of string"); |
894356b3 | 7408 | Perl_warner(aTHX_ WARN_PRINTF, "%"SVf, msg); /* yes, this is reentrant */ |
c635e13b | 7409 | } |
fb73857a | 7410 | |
7411 | /* output mangled stuff ... */ | |
7412 | if (c == '\0') | |
7413 | --q; | |
46fc3d4c | 7414 | eptr = p; |
7415 | elen = q - p; | |
fb73857a | 7416 | |
7417 | /* ... right here, because formatting flags should not apply */ | |
7418 | SvGROW(sv, SvCUR(sv) + elen + 1); | |
7419 | p = SvEND(sv); | |
4459522c | 7420 | Copy(eptr, p, elen, char); |
fb73857a | 7421 | p += elen; |
7422 | *p = '\0'; | |
7423 | SvCUR(sv) = p - SvPVX(sv); | |
7424 | continue; /* not "break" */ | |
46fc3d4c | 7425 | } |
7426 | ||
fc36a67e | 7427 | have = esignlen + zeros + elen; |
46fc3d4c | 7428 | need = (have > width ? have : width); |
7429 | gap = need - have; | |
7430 | ||
b22c7a20 | 7431 | SvGROW(sv, SvCUR(sv) + need + dotstrlen + 1); |
46fc3d4c | 7432 | p = SvEND(sv); |
7433 | if (esignlen && fill == '0') { | |
7434 | for (i = 0; i < esignlen; i++) | |
7435 | *p++ = esignbuf[i]; | |
7436 | } | |
7437 | if (gap && !left) { | |
7438 | memset(p, fill, gap); | |
7439 | p += gap; | |
7440 | } | |
7441 | if (esignlen && fill != '0') { | |
7442 | for (i = 0; i < esignlen; i++) | |
7443 | *p++ = esignbuf[i]; | |
7444 | } | |
fc36a67e | 7445 | if (zeros) { |
7446 | for (i = zeros; i; i--) | |
7447 | *p++ = '0'; | |
7448 | } | |
46fc3d4c | 7449 | if (elen) { |
4459522c | 7450 | Copy(eptr, p, elen, char); |
46fc3d4c | 7451 | p += elen; |
7452 | } | |
7453 | if (gap && left) { | |
7454 | memset(p, ' ', gap); | |
7455 | p += gap; | |
7456 | } | |
b22c7a20 GS |
7457 | if (vectorize) { |
7458 | if (veclen) { | |
4459522c | 7459 | Copy(dotstr, p, dotstrlen, char); |
b22c7a20 GS |
7460 | p += dotstrlen; |
7461 | } | |
7462 | else | |
7463 | vectorize = FALSE; /* done iterating over vecstr */ | |
7464 | } | |
7e2040f0 GS |
7465 | if (is_utf) |
7466 | SvUTF8_on(sv); | |
46fc3d4c | 7467 | *p = '\0'; |
7468 | SvCUR(sv) = p - SvPVX(sv); | |
b22c7a20 GS |
7469 | if (vectorize) { |
7470 | esignlen = 0; | |
7471 | goto vector; | |
7472 | } | |
46fc3d4c | 7473 | } |
7474 | } | |
51371543 | 7475 | |
1d7c1841 GS |
7476 | #if defined(USE_ITHREADS) |
7477 | ||
7478 | #if defined(USE_THREADS) | |
7479 | # include "error: USE_THREADS and USE_ITHREADS are incompatible" | |
7480 | #endif | |
7481 | ||
1d7c1841 GS |
7482 | #ifndef GpREFCNT_inc |
7483 | # define GpREFCNT_inc(gp) ((gp) ? (++(gp)->gp_refcnt, (gp)) : (GP*)NULL) | |
7484 | #endif | |
7485 | ||
7486 | ||
7487 | #define sv_dup_inc(s) SvREFCNT_inc(sv_dup(s)) | |
7488 | #define av_dup(s) (AV*)sv_dup((SV*)s) | |
7489 | #define av_dup_inc(s) (AV*)SvREFCNT_inc(sv_dup((SV*)s)) | |
7490 | #define hv_dup(s) (HV*)sv_dup((SV*)s) | |
7491 | #define hv_dup_inc(s) (HV*)SvREFCNT_inc(sv_dup((SV*)s)) | |
7492 | #define cv_dup(s) (CV*)sv_dup((SV*)s) | |
7493 | #define cv_dup_inc(s) (CV*)SvREFCNT_inc(sv_dup((SV*)s)) | |
7494 | #define io_dup(s) (IO*)sv_dup((SV*)s) | |
7495 | #define io_dup_inc(s) (IO*)SvREFCNT_inc(sv_dup((SV*)s)) | |
7496 | #define gv_dup(s) (GV*)sv_dup((SV*)s) | |
7497 | #define gv_dup_inc(s) (GV*)SvREFCNT_inc(sv_dup((SV*)s)) | |
7498 | #define SAVEPV(p) (p ? savepv(p) : Nullch) | |
7499 | #define SAVEPVN(p,n) (p ? savepvn(p,n) : Nullch) | |
7500 | ||
7501 | REGEXP * | |
7502 | Perl_re_dup(pTHX_ REGEXP *r) | |
7503 | { | |
7504 | /* XXX fix when pmop->op_pmregexp becomes shared */ | |
7505 | return ReREFCNT_inc(r); | |
7506 | } | |
7507 | ||
7508 | PerlIO * | |
7509 | Perl_fp_dup(pTHX_ PerlIO *fp, char type) | |
7510 | { | |
7511 | PerlIO *ret; | |
7512 | if (!fp) | |
7513 | return (PerlIO*)NULL; | |
7514 | ||
7515 | /* look for it in the table first */ | |
7516 | ret = (PerlIO*)ptr_table_fetch(PL_ptr_table, fp); | |
7517 | if (ret) | |
7518 | return ret; | |
7519 | ||
7520 | /* create anew and remember what it is */ | |
5f1a76d0 | 7521 | ret = PerlIO_fdupopen(aTHX_ fp); |
1d7c1841 GS |
7522 | ptr_table_store(PL_ptr_table, fp, ret); |
7523 | return ret; | |
7524 | } | |
7525 | ||
7526 | DIR * | |
7527 | Perl_dirp_dup(pTHX_ DIR *dp) | |
7528 | { | |
7529 | if (!dp) | |
7530 | return (DIR*)NULL; | |
7531 | /* XXX TODO */ | |
7532 | return dp; | |
7533 | } | |
7534 | ||
7535 | GP * | |
7536 | Perl_gp_dup(pTHX_ GP *gp) | |
7537 | { | |
7538 | GP *ret; | |
7539 | if (!gp) | |
7540 | return (GP*)NULL; | |
7541 | /* look for it in the table first */ | |
7542 | ret = (GP*)ptr_table_fetch(PL_ptr_table, gp); | |
7543 | if (ret) | |
7544 | return ret; | |
7545 | ||
7546 | /* create anew and remember what it is */ | |
7547 | Newz(0, ret, 1, GP); | |
7548 | ptr_table_store(PL_ptr_table, gp, ret); | |
7549 | ||
7550 | /* clone */ | |
7551 | ret->gp_refcnt = 0; /* must be before any other dups! */ | |
7552 | ret->gp_sv = sv_dup_inc(gp->gp_sv); | |
7553 | ret->gp_io = io_dup_inc(gp->gp_io); | |
7554 | ret->gp_form = cv_dup_inc(gp->gp_form); | |
7555 | ret->gp_av = av_dup_inc(gp->gp_av); | |
7556 | ret->gp_hv = hv_dup_inc(gp->gp_hv); | |
7557 | ret->gp_egv = gv_dup(gp->gp_egv); /* GvEGV is not refcounted */ | |
7558 | ret->gp_cv = cv_dup_inc(gp->gp_cv); | |
7559 | ret->gp_cvgen = gp->gp_cvgen; | |
7560 | ret->gp_flags = gp->gp_flags; | |
7561 | ret->gp_line = gp->gp_line; | |
7562 | ret->gp_file = gp->gp_file; /* points to COP.cop_file */ | |
7563 | return ret; | |
7564 | } | |
7565 | ||
7566 | MAGIC * | |
7567 | Perl_mg_dup(pTHX_ MAGIC *mg) | |
7568 | { | |
7569 | MAGIC *mgret = (MAGIC*)NULL; | |
7570 | MAGIC *mgprev; | |
7571 | if (!mg) | |
7572 | return (MAGIC*)NULL; | |
7573 | /* look for it in the table first */ | |
7574 | mgret = (MAGIC*)ptr_table_fetch(PL_ptr_table, mg); | |
7575 | if (mgret) | |
7576 | return mgret; | |
7577 | ||
7578 | for (; mg; mg = mg->mg_moremagic) { | |
7579 | MAGIC *nmg; | |
7580 | Newz(0, nmg, 1, MAGIC); | |
7581 | if (!mgret) | |
7582 | mgret = nmg; | |
7583 | else | |
7584 | mgprev->mg_moremagic = nmg; | |
7585 | nmg->mg_virtual = mg->mg_virtual; /* XXX copy dynamic vtable? */ | |
7586 | nmg->mg_private = mg->mg_private; | |
7587 | nmg->mg_type = mg->mg_type; | |
7588 | nmg->mg_flags = mg->mg_flags; | |
7589 | if (mg->mg_type == 'r') { | |
7590 | nmg->mg_obj = (SV*)re_dup((REGEXP*)mg->mg_obj); | |
7591 | } | |
7592 | else { | |
7593 | nmg->mg_obj = (mg->mg_flags & MGf_REFCOUNTED) | |
7594 | ? sv_dup_inc(mg->mg_obj) | |
7595 | : sv_dup(mg->mg_obj); | |
7596 | } | |
7597 | nmg->mg_len = mg->mg_len; | |
7598 | nmg->mg_ptr = mg->mg_ptr; /* XXX random ptr? */ | |
7599 | if (mg->mg_ptr && mg->mg_type != 'g') { | |
7600 | if (mg->mg_len >= 0) { | |
7601 | nmg->mg_ptr = SAVEPVN(mg->mg_ptr, mg->mg_len); | |
7602 | if (mg->mg_type == 'c' && AMT_AMAGIC((AMT*)mg->mg_ptr)) { | |
7603 | AMT *amtp = (AMT*)mg->mg_ptr; | |
7604 | AMT *namtp = (AMT*)nmg->mg_ptr; | |
7605 | I32 i; | |
7606 | for (i = 1; i < NofAMmeth; i++) { | |
7607 | namtp->table[i] = cv_dup_inc(amtp->table[i]); | |
7608 | } | |
7609 | } | |
7610 | } | |
7611 | else if (mg->mg_len == HEf_SVKEY) | |
7612 | nmg->mg_ptr = (char*)sv_dup_inc((SV*)mg->mg_ptr); | |
7613 | } | |
7614 | mgprev = nmg; | |
7615 | } | |
7616 | return mgret; | |
7617 | } | |
7618 | ||
7619 | PTR_TBL_t * | |
7620 | Perl_ptr_table_new(pTHX) | |
7621 | { | |
7622 | PTR_TBL_t *tbl; | |
7623 | Newz(0, tbl, 1, PTR_TBL_t); | |
7624 | tbl->tbl_max = 511; | |
7625 | tbl->tbl_items = 0; | |
7626 | Newz(0, tbl->tbl_ary, tbl->tbl_max + 1, PTR_TBL_ENT_t*); | |
7627 | return tbl; | |
7628 | } | |
7629 | ||
7630 | void * | |
7631 | Perl_ptr_table_fetch(pTHX_ PTR_TBL_t *tbl, void *sv) | |
7632 | { | |
7633 | PTR_TBL_ENT_t *tblent; | |
d2a79402 | 7634 | UV hash = PTR2UV(sv); |
1d7c1841 GS |
7635 | assert(tbl); |
7636 | tblent = tbl->tbl_ary[hash & tbl->tbl_max]; | |
7637 | for (; tblent; tblent = tblent->next) { | |
7638 | if (tblent->oldval == sv) | |
7639 | return tblent->newval; | |
7640 | } | |
7641 | return (void*)NULL; | |
7642 | } | |
7643 | ||
7644 | void | |
7645 | Perl_ptr_table_store(pTHX_ PTR_TBL_t *tbl, void *oldv, void *newv) | |
7646 | { | |
7647 | PTR_TBL_ENT_t *tblent, **otblent; | |
7648 | /* XXX this may be pessimal on platforms where pointers aren't good | |
7649 | * hash values e.g. if they grow faster in the most significant | |
7650 | * bits */ | |
d2a79402 | 7651 | UV hash = PTR2UV(oldv); |
1d7c1841 GS |
7652 | bool i = 1; |
7653 | ||
7654 | assert(tbl); | |
7655 | otblent = &tbl->tbl_ary[hash & tbl->tbl_max]; | |
7656 | for (tblent = *otblent; tblent; i=0, tblent = tblent->next) { | |
7657 | if (tblent->oldval == oldv) { | |
7658 | tblent->newval = newv; | |
7659 | tbl->tbl_items++; | |
7660 | return; | |
7661 | } | |
7662 | } | |
7663 | Newz(0, tblent, 1, PTR_TBL_ENT_t); | |
7664 | tblent->oldval = oldv; | |
7665 | tblent->newval = newv; | |
7666 | tblent->next = *otblent; | |
7667 | *otblent = tblent; | |
7668 | tbl->tbl_items++; | |
7669 | if (i && tbl->tbl_items > tbl->tbl_max) | |
7670 | ptr_table_split(tbl); | |
7671 | } | |
7672 | ||
7673 | void | |
7674 | Perl_ptr_table_split(pTHX_ PTR_TBL_t *tbl) | |
7675 | { | |
7676 | PTR_TBL_ENT_t **ary = tbl->tbl_ary; | |
7677 | UV oldsize = tbl->tbl_max + 1; | |
7678 | UV newsize = oldsize * 2; | |
7679 | UV i; | |
7680 | ||
7681 | Renew(ary, newsize, PTR_TBL_ENT_t*); | |
7682 | Zero(&ary[oldsize], newsize-oldsize, PTR_TBL_ENT_t*); | |
7683 | tbl->tbl_max = --newsize; | |
7684 | tbl->tbl_ary = ary; | |
7685 | for (i=0; i < oldsize; i++, ary++) { | |
7686 | PTR_TBL_ENT_t **curentp, **entp, *ent; | |
7687 | if (!*ary) | |
7688 | continue; | |
7689 | curentp = ary + oldsize; | |
7690 | for (entp = ary, ent = *ary; ent; ent = *entp) { | |
d2a79402 | 7691 | if ((newsize & PTR2UV(ent->oldval)) != i) { |
1d7c1841 GS |
7692 | *entp = ent->next; |
7693 | ent->next = *curentp; | |
7694 | *curentp = ent; | |
7695 | continue; | |
7696 | } | |
7697 | else | |
7698 | entp = &ent->next; | |
7699 | } | |
7700 | } | |
7701 | } | |
7702 | ||
7703 | #ifdef DEBUGGING | |
7704 | char *PL_watch_pvx; | |
7705 | #endif | |
7706 | ||
7707 | SV * | |
7708 | Perl_sv_dup(pTHX_ SV *sstr) | |
7709 | { | |
1d7c1841 GS |
7710 | SV *dstr; |
7711 | ||
7712 | if (!sstr || SvTYPE(sstr) == SVTYPEMASK) | |
7713 | return Nullsv; | |
7714 | /* look for it in the table first */ | |
7715 | dstr = (SV*)ptr_table_fetch(PL_ptr_table, sstr); | |
7716 | if (dstr) | |
7717 | return dstr; | |
7718 | ||
7719 | /* create anew and remember what it is */ | |
7720 | new_SV(dstr); | |
7721 | ptr_table_store(PL_ptr_table, sstr, dstr); | |
7722 | ||
7723 | /* clone */ | |
7724 | SvFLAGS(dstr) = SvFLAGS(sstr); | |
7725 | SvFLAGS(dstr) &= ~SVf_OOK; /* don't propagate OOK hack */ | |
7726 | SvREFCNT(dstr) = 0; /* must be before any other dups! */ | |
7727 | ||
7728 | #ifdef DEBUGGING | |
7729 | if (SvANY(sstr) && PL_watch_pvx && SvPVX(sstr) == PL_watch_pvx) | |
7730 | PerlIO_printf(Perl_debug_log, "watch at %p hit, found string \"%s\"\n", | |
7731 | PL_watch_pvx, SvPVX(sstr)); | |
7732 | #endif | |
7733 | ||
7734 | switch (SvTYPE(sstr)) { | |
7735 | case SVt_NULL: | |
7736 | SvANY(dstr) = NULL; | |
7737 | break; | |
7738 | case SVt_IV: | |
7739 | SvANY(dstr) = new_XIV(); | |
7740 | SvIVX(dstr) = SvIVX(sstr); | |
7741 | break; | |
7742 | case SVt_NV: | |
7743 | SvANY(dstr) = new_XNV(); | |
7744 | SvNVX(dstr) = SvNVX(sstr); | |
7745 | break; | |
7746 | case SVt_RV: | |
7747 | SvANY(dstr) = new_XRV(); | |
7748 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7749 | break; | |
7750 | case SVt_PV: | |
7751 | SvANY(dstr) = new_XPV(); | |
7752 | SvCUR(dstr) = SvCUR(sstr); | |
7753 | SvLEN(dstr) = SvLEN(sstr); | |
7754 | if (SvROK(sstr)) | |
7755 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7756 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
7757 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
7758 | else | |
7759 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
7760 | break; | |
7761 | case SVt_PVIV: | |
7762 | SvANY(dstr) = new_XPVIV(); | |
7763 | SvCUR(dstr) = SvCUR(sstr); | |
7764 | SvLEN(dstr) = SvLEN(sstr); | |
7765 | SvIVX(dstr) = SvIVX(sstr); | |
7766 | if (SvROK(sstr)) | |
7767 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7768 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
7769 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
7770 | else | |
7771 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
7772 | break; | |
7773 | case SVt_PVNV: | |
7774 | SvANY(dstr) = new_XPVNV(); | |
7775 | SvCUR(dstr) = SvCUR(sstr); | |
7776 | SvLEN(dstr) = SvLEN(sstr); | |
7777 | SvIVX(dstr) = SvIVX(sstr); | |
7778 | SvNVX(dstr) = SvNVX(sstr); | |
7779 | if (SvROK(sstr)) | |
7780 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7781 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
7782 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
7783 | else | |
7784 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
7785 | break; | |
7786 | case SVt_PVMG: | |
7787 | SvANY(dstr) = new_XPVMG(); | |
7788 | SvCUR(dstr) = SvCUR(sstr); | |
7789 | SvLEN(dstr) = SvLEN(sstr); | |
7790 | SvIVX(dstr) = SvIVX(sstr); | |
7791 | SvNVX(dstr) = SvNVX(sstr); | |
7792 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
7793 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
7794 | if (SvROK(sstr)) | |
7795 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7796 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
7797 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
7798 | else | |
7799 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
7800 | break; | |
7801 | case SVt_PVBM: | |
7802 | SvANY(dstr) = new_XPVBM(); | |
7803 | SvCUR(dstr) = SvCUR(sstr); | |
7804 | SvLEN(dstr) = SvLEN(sstr); | |
7805 | SvIVX(dstr) = SvIVX(sstr); | |
7806 | SvNVX(dstr) = SvNVX(sstr); | |
7807 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
7808 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
7809 | if (SvROK(sstr)) | |
7810 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7811 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
7812 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
7813 | else | |
7814 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
7815 | BmRARE(dstr) = BmRARE(sstr); | |
7816 | BmUSEFUL(dstr) = BmUSEFUL(sstr); | |
7817 | BmPREVIOUS(dstr)= BmPREVIOUS(sstr); | |
7818 | break; | |
7819 | case SVt_PVLV: | |
7820 | SvANY(dstr) = new_XPVLV(); | |
7821 | SvCUR(dstr) = SvCUR(sstr); | |
7822 | SvLEN(dstr) = SvLEN(sstr); | |
7823 | SvIVX(dstr) = SvIVX(sstr); | |
7824 | SvNVX(dstr) = SvNVX(sstr); | |
7825 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
7826 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
7827 | if (SvROK(sstr)) | |
7828 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7829 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
7830 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
7831 | else | |
7832 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
7833 | LvTARGOFF(dstr) = LvTARGOFF(sstr); /* XXX sometimes holds PMOP* when DEBUGGING */ | |
7834 | LvTARGLEN(dstr) = LvTARGLEN(sstr); | |
7835 | LvTARG(dstr) = sv_dup_inc(LvTARG(sstr)); | |
7836 | LvTYPE(dstr) = LvTYPE(sstr); | |
7837 | break; | |
7838 | case SVt_PVGV: | |
7839 | SvANY(dstr) = new_XPVGV(); | |
7840 | SvCUR(dstr) = SvCUR(sstr); | |
7841 | SvLEN(dstr) = SvLEN(sstr); | |
7842 | SvIVX(dstr) = SvIVX(sstr); | |
7843 | SvNVX(dstr) = SvNVX(sstr); | |
7844 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
7845 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
7846 | if (SvROK(sstr)) | |
7847 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7848 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
7849 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
7850 | else | |
7851 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
7852 | GvNAMELEN(dstr) = GvNAMELEN(sstr); | |
7853 | GvNAME(dstr) = SAVEPVN(GvNAME(sstr), GvNAMELEN(sstr)); | |
7854 | GvSTASH(dstr) = hv_dup_inc(GvSTASH(sstr)); | |
7855 | GvFLAGS(dstr) = GvFLAGS(sstr); | |
7856 | GvGP(dstr) = gp_dup(GvGP(sstr)); | |
7857 | (void)GpREFCNT_inc(GvGP(dstr)); | |
7858 | break; | |
7859 | case SVt_PVIO: | |
7860 | SvANY(dstr) = new_XPVIO(); | |
7861 | SvCUR(dstr) = SvCUR(sstr); | |
7862 | SvLEN(dstr) = SvLEN(sstr); | |
7863 | SvIVX(dstr) = SvIVX(sstr); | |
7864 | SvNVX(dstr) = SvNVX(sstr); | |
7865 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
7866 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
7867 | if (SvROK(sstr)) | |
7868 | SvRV(dstr) = sv_dup_inc(SvRV(sstr)); | |
7869 | else if (SvPVX(sstr) && SvLEN(sstr)) | |
7870 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
7871 | else | |
7872 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
7873 | IoIFP(dstr) = fp_dup(IoIFP(sstr), IoTYPE(sstr)); | |
7874 | if (IoOFP(sstr) == IoIFP(sstr)) | |
7875 | IoOFP(dstr) = IoIFP(dstr); | |
7876 | else | |
7877 | IoOFP(dstr) = fp_dup(IoOFP(sstr), IoTYPE(sstr)); | |
7878 | /* PL_rsfp_filters entries have fake IoDIRP() */ | |
7879 | if (IoDIRP(sstr) && !(IoFLAGS(sstr) & IOf_FAKE_DIRP)) | |
7880 | IoDIRP(dstr) = dirp_dup(IoDIRP(sstr)); | |
7881 | else | |
7882 | IoDIRP(dstr) = IoDIRP(sstr); | |
7883 | IoLINES(dstr) = IoLINES(sstr); | |
7884 | IoPAGE(dstr) = IoPAGE(sstr); | |
7885 | IoPAGE_LEN(dstr) = IoPAGE_LEN(sstr); | |
7886 | IoLINES_LEFT(dstr) = IoLINES_LEFT(sstr); | |
7887 | IoTOP_NAME(dstr) = SAVEPV(IoTOP_NAME(sstr)); | |
7888 | IoTOP_GV(dstr) = gv_dup(IoTOP_GV(sstr)); | |
7889 | IoFMT_NAME(dstr) = SAVEPV(IoFMT_NAME(sstr)); | |
7890 | IoFMT_GV(dstr) = gv_dup(IoFMT_GV(sstr)); | |
7891 | IoBOTTOM_NAME(dstr) = SAVEPV(IoBOTTOM_NAME(sstr)); | |
7892 | IoBOTTOM_GV(dstr) = gv_dup(IoBOTTOM_GV(sstr)); | |
7893 | IoSUBPROCESS(dstr) = IoSUBPROCESS(sstr); | |
7894 | IoTYPE(dstr) = IoTYPE(sstr); | |
7895 | IoFLAGS(dstr) = IoFLAGS(sstr); | |
7896 | break; | |
7897 | case SVt_PVAV: | |
7898 | SvANY(dstr) = new_XPVAV(); | |
7899 | SvCUR(dstr) = SvCUR(sstr); | |
7900 | SvLEN(dstr) = SvLEN(sstr); | |
7901 | SvIVX(dstr) = SvIVX(sstr); | |
7902 | SvNVX(dstr) = SvNVX(sstr); | |
7903 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
7904 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
7905 | AvARYLEN((AV*)dstr) = sv_dup_inc(AvARYLEN((AV*)sstr)); | |
7906 | AvFLAGS((AV*)dstr) = AvFLAGS((AV*)sstr); | |
7907 | if (AvARRAY((AV*)sstr)) { | |
7908 | SV **dst_ary, **src_ary; | |
7909 | SSize_t items = AvFILLp((AV*)sstr) + 1; | |
7910 | ||
7911 | src_ary = AvARRAY((AV*)sstr); | |
7912 | Newz(0, dst_ary, AvMAX((AV*)sstr)+1, SV*); | |
7913 | ptr_table_store(PL_ptr_table, src_ary, dst_ary); | |
7914 | SvPVX(dstr) = (char*)dst_ary; | |
7915 | AvALLOC((AV*)dstr) = dst_ary; | |
7916 | if (AvREAL((AV*)sstr)) { | |
7917 | while (items-- > 0) | |
7918 | *dst_ary++ = sv_dup_inc(*src_ary++); | |
7919 | } | |
7920 | else { | |
7921 | while (items-- > 0) | |
7922 | *dst_ary++ = sv_dup(*src_ary++); | |
7923 | } | |
7924 | items = AvMAX((AV*)sstr) - AvFILLp((AV*)sstr); | |
7925 | while (items-- > 0) { | |
7926 | *dst_ary++ = &PL_sv_undef; | |
7927 | } | |
7928 | } | |
7929 | else { | |
7930 | SvPVX(dstr) = Nullch; | |
7931 | AvALLOC((AV*)dstr) = (SV**)NULL; | |
7932 | } | |
7933 | break; | |
7934 | case SVt_PVHV: | |
7935 | SvANY(dstr) = new_XPVHV(); | |
7936 | SvCUR(dstr) = SvCUR(sstr); | |
7937 | SvLEN(dstr) = SvLEN(sstr); | |
7938 | SvIVX(dstr) = SvIVX(sstr); | |
7939 | SvNVX(dstr) = SvNVX(sstr); | |
7940 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
7941 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
7942 | HvRITER((HV*)dstr) = HvRITER((HV*)sstr); | |
7943 | if (HvARRAY((HV*)sstr)) { | |
1d7c1841 GS |
7944 | STRLEN i = 0; |
7945 | XPVHV *dxhv = (XPVHV*)SvANY(dstr); | |
7946 | XPVHV *sxhv = (XPVHV*)SvANY(sstr); | |
7947 | Newz(0, dxhv->xhv_array, | |
7948 | PERL_HV_ARRAY_ALLOC_BYTES(dxhv->xhv_max+1), char); | |
7949 | while (i <= sxhv->xhv_max) { | |
7950 | ((HE**)dxhv->xhv_array)[i] = he_dup(((HE**)sxhv->xhv_array)[i], | |
7951 | !!HvSHAREKEYS(sstr)); | |
7952 | ++i; | |
7953 | } | |
7954 | dxhv->xhv_eiter = he_dup(sxhv->xhv_eiter, !!HvSHAREKEYS(sstr)); | |
7955 | } | |
7956 | else { | |
7957 | SvPVX(dstr) = Nullch; | |
7958 | HvEITER((HV*)dstr) = (HE*)NULL; | |
7959 | } | |
7960 | HvPMROOT((HV*)dstr) = HvPMROOT((HV*)sstr); /* XXX */ | |
7961 | HvNAME((HV*)dstr) = SAVEPV(HvNAME((HV*)sstr)); | |
7962 | break; | |
7963 | case SVt_PVFM: | |
7964 | SvANY(dstr) = new_XPVFM(); | |
7965 | FmLINES(dstr) = FmLINES(sstr); | |
7966 | goto dup_pvcv; | |
7967 | /* NOTREACHED */ | |
7968 | case SVt_PVCV: | |
7969 | SvANY(dstr) = new_XPVCV(); | |
7970 | dup_pvcv: | |
7971 | SvCUR(dstr) = SvCUR(sstr); | |
7972 | SvLEN(dstr) = SvLEN(sstr); | |
7973 | SvIVX(dstr) = SvIVX(sstr); | |
7974 | SvNVX(dstr) = SvNVX(sstr); | |
7975 | SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr)); | |
7976 | SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr)); | |
7977 | if (SvPVX(sstr) && SvLEN(sstr)) | |
7978 | SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1); | |
7979 | else | |
7980 | SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */ | |
7981 | CvSTASH(dstr) = hv_dup(CvSTASH(sstr));/* NOTE: not refcounted */ | |
7982 | CvSTART(dstr) = CvSTART(sstr); | |
7983 | CvROOT(dstr) = OpREFCNT_inc(CvROOT(sstr)); | |
7984 | CvXSUB(dstr) = CvXSUB(sstr); | |
7985 | CvXSUBANY(dstr) = CvXSUBANY(sstr); | |
7986 | CvGV(dstr) = gv_dup_inc(CvGV(sstr)); | |
7987 | CvDEPTH(dstr) = CvDEPTH(sstr); | |
7988 | if (CvPADLIST(sstr) && !AvREAL(CvPADLIST(sstr))) { | |
7989 | /* XXX padlists are real, but pretend to be not */ | |
7990 | AvREAL_on(CvPADLIST(sstr)); | |
7991 | CvPADLIST(dstr) = av_dup_inc(CvPADLIST(sstr)); | |
7992 | AvREAL_off(CvPADLIST(sstr)); | |
7993 | AvREAL_off(CvPADLIST(dstr)); | |
7994 | } | |
7995 | else | |
7996 | CvPADLIST(dstr) = av_dup_inc(CvPADLIST(sstr)); | |
7997 | CvOUTSIDE(dstr) = cv_dup_inc(CvOUTSIDE(sstr)); | |
7998 | CvFLAGS(dstr) = CvFLAGS(sstr); | |
7999 | break; | |
8000 | default: | |
8001 | Perl_croak(aTHX_ "Bizarre SvTYPE [%d]", SvTYPE(sstr)); | |
8002 | break; | |
8003 | } | |
8004 | ||
8005 | if (SvOBJECT(dstr) && SvTYPE(dstr) != SVt_PVIO) | |
8006 | ++PL_sv_objcount; | |
8007 | ||
8008 | return dstr; | |
8009 | } | |
8010 | ||
8011 | PERL_CONTEXT * | |
8012 | Perl_cx_dup(pTHX_ PERL_CONTEXT *cxs, I32 ix, I32 max) | |
8013 | { | |
8014 | PERL_CONTEXT *ncxs; | |
8015 | ||
8016 | if (!cxs) | |
8017 | return (PERL_CONTEXT*)NULL; | |
8018 | ||
8019 | /* look for it in the table first */ | |
8020 | ncxs = (PERL_CONTEXT*)ptr_table_fetch(PL_ptr_table, cxs); | |
8021 | if (ncxs) | |
8022 | return ncxs; | |
8023 | ||
8024 | /* create anew and remember what it is */ | |
8025 | Newz(56, ncxs, max + 1, PERL_CONTEXT); | |
8026 | ptr_table_store(PL_ptr_table, cxs, ncxs); | |
8027 | ||
8028 | while (ix >= 0) { | |
8029 | PERL_CONTEXT *cx = &cxs[ix]; | |
8030 | PERL_CONTEXT *ncx = &ncxs[ix]; | |
8031 | ncx->cx_type = cx->cx_type; | |
8032 | if (CxTYPE(cx) == CXt_SUBST) { | |
8033 | Perl_croak(aTHX_ "Cloning substitution context is unimplemented"); | |
8034 | } | |
8035 | else { | |
8036 | ncx->blk_oldsp = cx->blk_oldsp; | |
8037 | ncx->blk_oldcop = cx->blk_oldcop; | |
8038 | ncx->blk_oldretsp = cx->blk_oldretsp; | |
8039 | ncx->blk_oldmarksp = cx->blk_oldmarksp; | |
8040 | ncx->blk_oldscopesp = cx->blk_oldscopesp; | |
8041 | ncx->blk_oldpm = cx->blk_oldpm; | |
8042 | ncx->blk_gimme = cx->blk_gimme; | |
8043 | switch (CxTYPE(cx)) { | |
8044 | case CXt_SUB: | |
8045 | ncx->blk_sub.cv = (cx->blk_sub.olddepth == 0 | |
8046 | ? cv_dup_inc(cx->blk_sub.cv) | |
8047 | : cv_dup(cx->blk_sub.cv)); | |
8048 | ncx->blk_sub.argarray = (cx->blk_sub.hasargs | |
8049 | ? av_dup_inc(cx->blk_sub.argarray) | |
8050 | : Nullav); | |
8051 | ncx->blk_sub.savearray = av_dup(cx->blk_sub.savearray); | |
8052 | ncx->blk_sub.olddepth = cx->blk_sub.olddepth; | |
8053 | ncx->blk_sub.hasargs = cx->blk_sub.hasargs; | |
8054 | ncx->blk_sub.lval = cx->blk_sub.lval; | |
8055 | break; | |
8056 | case CXt_EVAL: | |
8057 | ncx->blk_eval.old_in_eval = cx->blk_eval.old_in_eval; | |
8058 | ncx->blk_eval.old_op_type = cx->blk_eval.old_op_type; | |
0f79a09d | 8059 | ncx->blk_eval.old_namesv = sv_dup_inc(cx->blk_eval.old_namesv); |
1d7c1841 GS |
8060 | ncx->blk_eval.old_eval_root = cx->blk_eval.old_eval_root; |
8061 | ncx->blk_eval.cur_text = sv_dup(cx->blk_eval.cur_text); | |
8062 | break; | |
8063 | case CXt_LOOP: | |
8064 | ncx->blk_loop.label = cx->blk_loop.label; | |
8065 | ncx->blk_loop.resetsp = cx->blk_loop.resetsp; | |
8066 | ncx->blk_loop.redo_op = cx->blk_loop.redo_op; | |
8067 | ncx->blk_loop.next_op = cx->blk_loop.next_op; | |
8068 | ncx->blk_loop.last_op = cx->blk_loop.last_op; | |
8069 | ncx->blk_loop.iterdata = (CxPADLOOP(cx) | |
8070 | ? cx->blk_loop.iterdata | |
8071 | : gv_dup((GV*)cx->blk_loop.iterdata)); | |
a4b82a6f GS |
8072 | ncx->blk_loop.oldcurpad |
8073 | = (SV**)ptr_table_fetch(PL_ptr_table, | |
8074 | cx->blk_loop.oldcurpad); | |
1d7c1841 GS |
8075 | ncx->blk_loop.itersave = sv_dup_inc(cx->blk_loop.itersave); |
8076 | ncx->blk_loop.iterlval = sv_dup_inc(cx->blk_loop.iterlval); | |
8077 | ncx->blk_loop.iterary = av_dup_inc(cx->blk_loop.iterary); | |
8078 | ncx->blk_loop.iterix = cx->blk_loop.iterix; | |
8079 | ncx->blk_loop.itermax = cx->blk_loop.itermax; | |
8080 | break; | |
8081 | case CXt_FORMAT: | |
8082 | ncx->blk_sub.cv = cv_dup(cx->blk_sub.cv); | |
8083 | ncx->blk_sub.gv = gv_dup(cx->blk_sub.gv); | |
8084 | ncx->blk_sub.dfoutgv = gv_dup_inc(cx->blk_sub.dfoutgv); | |
8085 | ncx->blk_sub.hasargs = cx->blk_sub.hasargs; | |
8086 | break; | |
8087 | case CXt_BLOCK: | |
8088 | case CXt_NULL: | |
8089 | break; | |
8090 | } | |
8091 | } | |
8092 | --ix; | |
8093 | } | |
8094 | return ncxs; | |
8095 | } | |
8096 | ||
8097 | PERL_SI * | |
8098 | Perl_si_dup(pTHX_ PERL_SI *si) | |
8099 | { | |
8100 | PERL_SI *nsi; | |
8101 | ||
8102 | if (!si) | |
8103 | return (PERL_SI*)NULL; | |
8104 | ||
8105 | /* look for it in the table first */ | |
8106 | nsi = (PERL_SI*)ptr_table_fetch(PL_ptr_table, si); | |
8107 | if (nsi) | |
8108 | return nsi; | |
8109 | ||
8110 | /* create anew and remember what it is */ | |
8111 | Newz(56, nsi, 1, PERL_SI); | |
8112 | ptr_table_store(PL_ptr_table, si, nsi); | |
8113 | ||
8114 | nsi->si_stack = av_dup_inc(si->si_stack); | |
8115 | nsi->si_cxix = si->si_cxix; | |
8116 | nsi->si_cxmax = si->si_cxmax; | |
8117 | nsi->si_cxstack = cx_dup(si->si_cxstack, si->si_cxix, si->si_cxmax); | |
8118 | nsi->si_type = si->si_type; | |
8119 | nsi->si_prev = si_dup(si->si_prev); | |
8120 | nsi->si_next = si_dup(si->si_next); | |
8121 | nsi->si_markoff = si->si_markoff; | |
8122 | ||
8123 | return nsi; | |
8124 | } | |
8125 | ||
8126 | #define POPINT(ss,ix) ((ss)[--(ix)].any_i32) | |
8127 | #define TOPINT(ss,ix) ((ss)[ix].any_i32) | |
8128 | #define POPLONG(ss,ix) ((ss)[--(ix)].any_long) | |
8129 | #define TOPLONG(ss,ix) ((ss)[ix].any_long) | |
8130 | #define POPIV(ss,ix) ((ss)[--(ix)].any_iv) | |
8131 | #define TOPIV(ss,ix) ((ss)[ix].any_iv) | |
8132 | #define POPPTR(ss,ix) ((ss)[--(ix)].any_ptr) | |
8133 | #define TOPPTR(ss,ix) ((ss)[ix].any_ptr) | |
8134 | #define POPDPTR(ss,ix) ((ss)[--(ix)].any_dptr) | |
8135 | #define TOPDPTR(ss,ix) ((ss)[ix].any_dptr) | |
8136 | #define POPDXPTR(ss,ix) ((ss)[--(ix)].any_dxptr) | |
8137 | #define TOPDXPTR(ss,ix) ((ss)[ix].any_dxptr) | |
8138 | ||
8139 | /* XXXXX todo */ | |
8140 | #define pv_dup_inc(p) SAVEPV(p) | |
8141 | #define pv_dup(p) SAVEPV(p) | |
8142 | #define svp_dup_inc(p,pp) any_dup(p,pp) | |
8143 | ||
8144 | void * | |
8145 | Perl_any_dup(pTHX_ void *v, PerlInterpreter *proto_perl) | |
8146 | { | |
8147 | void *ret; | |
8148 | ||
8149 | if (!v) | |
8150 | return (void*)NULL; | |
8151 | ||
8152 | /* look for it in the table first */ | |
8153 | ret = ptr_table_fetch(PL_ptr_table, v); | |
8154 | if (ret) | |
8155 | return ret; | |
8156 | ||
8157 | /* see if it is part of the interpreter structure */ | |
8158 | if (v >= (void*)proto_perl && v < (void*)(proto_perl+1)) | |
8159 | ret = (void*)(((char*)aTHXo) + (((char*)v) - (char*)proto_perl)); | |
8160 | else | |
8161 | ret = v; | |
8162 | ||
8163 | return ret; | |
8164 | } | |
8165 | ||
8166 | ANY * | |
8167 | Perl_ss_dup(pTHX_ PerlInterpreter *proto_perl) | |
8168 | { | |
8169 | ANY *ss = proto_perl->Tsavestack; | |
8170 | I32 ix = proto_perl->Tsavestack_ix; | |
8171 | I32 max = proto_perl->Tsavestack_max; | |
8172 | ANY *nss; | |
8173 | SV *sv; | |
8174 | GV *gv; | |
8175 | AV *av; | |
8176 | HV *hv; | |
8177 | void* ptr; | |
8178 | int intval; | |
8179 | long longval; | |
8180 | GP *gp; | |
8181 | IV iv; | |
8182 | I32 i; | |
8183 | char *c; | |
8184 | void (*dptr) (void*); | |
8185 | void (*dxptr) (pTHXo_ void*); | |
e977893f | 8186 | OP *o; |
1d7c1841 GS |
8187 | |
8188 | Newz(54, nss, max, ANY); | |
8189 | ||
8190 | while (ix > 0) { | |
8191 | i = POPINT(ss,ix); | |
8192 | TOPINT(nss,ix) = i; | |
8193 | switch (i) { | |
8194 | case SAVEt_ITEM: /* normal string */ | |
8195 | sv = (SV*)POPPTR(ss,ix); | |
8196 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8197 | sv = (SV*)POPPTR(ss,ix); | |
8198 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8199 | break; | |
8200 | case SAVEt_SV: /* scalar reference */ | |
8201 | sv = (SV*)POPPTR(ss,ix); | |
8202 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8203 | gv = (GV*)POPPTR(ss,ix); | |
8204 | TOPPTR(nss,ix) = gv_dup_inc(gv); | |
8205 | break; | |
f4dd75d9 GS |
8206 | case SAVEt_GENERIC_PVREF: /* generic char* */ |
8207 | c = (char*)POPPTR(ss,ix); | |
8208 | TOPPTR(nss,ix) = pv_dup(c); | |
8209 | ptr = POPPTR(ss,ix); | |
8210 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8211 | break; | |
1d7c1841 GS |
8212 | case SAVEt_GENERIC_SVREF: /* generic sv */ |
8213 | case SAVEt_SVREF: /* scalar reference */ | |
8214 | sv = (SV*)POPPTR(ss,ix); | |
8215 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8216 | ptr = POPPTR(ss,ix); | |
8217 | TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */ | |
8218 | break; | |
8219 | case SAVEt_AV: /* array reference */ | |
8220 | av = (AV*)POPPTR(ss,ix); | |
8221 | TOPPTR(nss,ix) = av_dup_inc(av); | |
8222 | gv = (GV*)POPPTR(ss,ix); | |
8223 | TOPPTR(nss,ix) = gv_dup(gv); | |
8224 | break; | |
8225 | case SAVEt_HV: /* hash reference */ | |
8226 | hv = (HV*)POPPTR(ss,ix); | |
8227 | TOPPTR(nss,ix) = hv_dup_inc(hv); | |
8228 | gv = (GV*)POPPTR(ss,ix); | |
8229 | TOPPTR(nss,ix) = gv_dup(gv); | |
8230 | break; | |
8231 | case SAVEt_INT: /* int reference */ | |
8232 | ptr = POPPTR(ss,ix); | |
8233 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8234 | intval = (int)POPINT(ss,ix); | |
8235 | TOPINT(nss,ix) = intval; | |
8236 | break; | |
8237 | case SAVEt_LONG: /* long reference */ | |
8238 | ptr = POPPTR(ss,ix); | |
8239 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8240 | longval = (long)POPLONG(ss,ix); | |
8241 | TOPLONG(nss,ix) = longval; | |
8242 | break; | |
8243 | case SAVEt_I32: /* I32 reference */ | |
8244 | case SAVEt_I16: /* I16 reference */ | |
8245 | case SAVEt_I8: /* I8 reference */ | |
8246 | ptr = POPPTR(ss,ix); | |
8247 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8248 | i = POPINT(ss,ix); | |
8249 | TOPINT(nss,ix) = i; | |
8250 | break; | |
8251 | case SAVEt_IV: /* IV reference */ | |
8252 | ptr = POPPTR(ss,ix); | |
8253 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8254 | iv = POPIV(ss,ix); | |
8255 | TOPIV(nss,ix) = iv; | |
8256 | break; | |
8257 | case SAVEt_SPTR: /* SV* reference */ | |
8258 | ptr = POPPTR(ss,ix); | |
8259 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8260 | sv = (SV*)POPPTR(ss,ix); | |
8261 | TOPPTR(nss,ix) = sv_dup(sv); | |
8262 | break; | |
8263 | case SAVEt_VPTR: /* random* reference */ | |
8264 | ptr = POPPTR(ss,ix); | |
8265 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8266 | ptr = POPPTR(ss,ix); | |
8267 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8268 | break; | |
8269 | case SAVEt_PPTR: /* char* reference */ | |
8270 | ptr = POPPTR(ss,ix); | |
8271 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8272 | c = (char*)POPPTR(ss,ix); | |
8273 | TOPPTR(nss,ix) = pv_dup(c); | |
8274 | break; | |
8275 | case SAVEt_HPTR: /* HV* reference */ | |
8276 | ptr = POPPTR(ss,ix); | |
8277 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8278 | hv = (HV*)POPPTR(ss,ix); | |
8279 | TOPPTR(nss,ix) = hv_dup(hv); | |
8280 | break; | |
8281 | case SAVEt_APTR: /* AV* reference */ | |
8282 | ptr = POPPTR(ss,ix); | |
8283 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8284 | av = (AV*)POPPTR(ss,ix); | |
8285 | TOPPTR(nss,ix) = av_dup(av); | |
8286 | break; | |
8287 | case SAVEt_NSTAB: | |
8288 | gv = (GV*)POPPTR(ss,ix); | |
8289 | TOPPTR(nss,ix) = gv_dup(gv); | |
8290 | break; | |
8291 | case SAVEt_GP: /* scalar reference */ | |
8292 | gp = (GP*)POPPTR(ss,ix); | |
8293 | TOPPTR(nss,ix) = gp = gp_dup(gp); | |
8294 | (void)GpREFCNT_inc(gp); | |
8295 | gv = (GV*)POPPTR(ss,ix); | |
8296 | TOPPTR(nss,ix) = gv_dup_inc(c); | |
8297 | c = (char*)POPPTR(ss,ix); | |
8298 | TOPPTR(nss,ix) = pv_dup(c); | |
8299 | iv = POPIV(ss,ix); | |
8300 | TOPIV(nss,ix) = iv; | |
8301 | iv = POPIV(ss,ix); | |
8302 | TOPIV(nss,ix) = iv; | |
8303 | break; | |
8304 | case SAVEt_FREESV: | |
8305 | sv = (SV*)POPPTR(ss,ix); | |
8306 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8307 | break; | |
8308 | case SAVEt_FREEOP: | |
8309 | ptr = POPPTR(ss,ix); | |
8310 | if (ptr && (((OP*)ptr)->op_private & OPpREFCOUNTED)) { | |
8311 | /* these are assumed to be refcounted properly */ | |
8312 | switch (((OP*)ptr)->op_type) { | |
8313 | case OP_LEAVESUB: | |
8314 | case OP_LEAVESUBLV: | |
8315 | case OP_LEAVEEVAL: | |
8316 | case OP_LEAVE: | |
8317 | case OP_SCOPE: | |
8318 | case OP_LEAVEWRITE: | |
e977893f GS |
8319 | TOPPTR(nss,ix) = ptr; |
8320 | o = (OP*)ptr; | |
8321 | OpREFCNT_inc(o); | |
1d7c1841 GS |
8322 | break; |
8323 | default: | |
8324 | TOPPTR(nss,ix) = Nullop; | |
8325 | break; | |
8326 | } | |
8327 | } | |
8328 | else | |
8329 | TOPPTR(nss,ix) = Nullop; | |
8330 | break; | |
8331 | case SAVEt_FREEPV: | |
8332 | c = (char*)POPPTR(ss,ix); | |
8333 | TOPPTR(nss,ix) = pv_dup_inc(c); | |
8334 | break; | |
8335 | case SAVEt_CLEARSV: | |
8336 | longval = POPLONG(ss,ix); | |
8337 | TOPLONG(nss,ix) = longval; | |
8338 | break; | |
8339 | case SAVEt_DELETE: | |
8340 | hv = (HV*)POPPTR(ss,ix); | |
8341 | TOPPTR(nss,ix) = hv_dup_inc(hv); | |
8342 | c = (char*)POPPTR(ss,ix); | |
8343 | TOPPTR(nss,ix) = pv_dup_inc(c); | |
8344 | i = POPINT(ss,ix); | |
8345 | TOPINT(nss,ix) = i; | |
8346 | break; | |
8347 | case SAVEt_DESTRUCTOR: | |
8348 | ptr = POPPTR(ss,ix); | |
8349 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */ | |
8350 | dptr = POPDPTR(ss,ix); | |
ef75a179 | 8351 | TOPDPTR(nss,ix) = (void (*)(void*))any_dup((void *)dptr, proto_perl); |
1d7c1841 GS |
8352 | break; |
8353 | case SAVEt_DESTRUCTOR_X: | |
8354 | ptr = POPPTR(ss,ix); | |
8355 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */ | |
8356 | dxptr = POPDXPTR(ss,ix); | |
ef75a179 | 8357 | TOPDXPTR(nss,ix) = (void (*)(pTHXo_ void*))any_dup((void *)dxptr, proto_perl); |
1d7c1841 GS |
8358 | break; |
8359 | case SAVEt_REGCONTEXT: | |
8360 | case SAVEt_ALLOC: | |
8361 | i = POPINT(ss,ix); | |
8362 | TOPINT(nss,ix) = i; | |
8363 | ix -= i; | |
8364 | break; | |
8365 | case SAVEt_STACK_POS: /* Position on Perl stack */ | |
8366 | i = POPINT(ss,ix); | |
8367 | TOPINT(nss,ix) = i; | |
8368 | break; | |
8369 | case SAVEt_AELEM: /* array element */ | |
8370 | sv = (SV*)POPPTR(ss,ix); | |
8371 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8372 | i = POPINT(ss,ix); | |
8373 | TOPINT(nss,ix) = i; | |
8374 | av = (AV*)POPPTR(ss,ix); | |
8375 | TOPPTR(nss,ix) = av_dup_inc(av); | |
8376 | break; | |
8377 | case SAVEt_HELEM: /* hash element */ | |
8378 | sv = (SV*)POPPTR(ss,ix); | |
8379 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8380 | sv = (SV*)POPPTR(ss,ix); | |
8381 | TOPPTR(nss,ix) = sv_dup_inc(sv); | |
8382 | hv = (HV*)POPPTR(ss,ix); | |
8383 | TOPPTR(nss,ix) = hv_dup_inc(hv); | |
8384 | break; | |
8385 | case SAVEt_OP: | |
8386 | ptr = POPPTR(ss,ix); | |
8387 | TOPPTR(nss,ix) = ptr; | |
8388 | break; | |
8389 | case SAVEt_HINTS: | |
8390 | i = POPINT(ss,ix); | |
8391 | TOPINT(nss,ix) = i; | |
8392 | break; | |
c4410b1b GS |
8393 | case SAVEt_COMPPAD: |
8394 | av = (AV*)POPPTR(ss,ix); | |
8395 | TOPPTR(nss,ix) = av_dup(av); | |
8396 | break; | |
c3564e5c GS |
8397 | case SAVEt_PADSV: |
8398 | longval = (long)POPLONG(ss,ix); | |
8399 | TOPLONG(nss,ix) = longval; | |
8400 | ptr = POPPTR(ss,ix); | |
8401 | TOPPTR(nss,ix) = any_dup(ptr, proto_perl); | |
8402 | sv = (SV*)POPPTR(ss,ix); | |
8403 | TOPPTR(nss,ix) = sv_dup(sv); | |
8404 | break; | |
1d7c1841 GS |
8405 | default: |
8406 | Perl_croak(aTHX_ "panic: ss_dup inconsistency"); | |
8407 | } | |
8408 | } | |
8409 | ||
8410 | return nss; | |
8411 | } | |
8412 | ||
8413 | #ifdef PERL_OBJECT | |
8414 | #include "XSUB.h" | |
8415 | #endif | |
8416 | ||
8417 | PerlInterpreter * | |
8418 | perl_clone(PerlInterpreter *proto_perl, UV flags) | |
8419 | { | |
8420 | #ifdef PERL_OBJECT | |
8421 | CPerlObj *pPerl = (CPerlObj*)proto_perl; | |
8422 | #endif | |
8423 | ||
8424 | #ifdef PERL_IMPLICIT_SYS | |
8425 | return perl_clone_using(proto_perl, flags, | |
8426 | proto_perl->IMem, | |
8427 | proto_perl->IMemShared, | |
8428 | proto_perl->IMemParse, | |
8429 | proto_perl->IEnv, | |
8430 | proto_perl->IStdIO, | |
8431 | proto_perl->ILIO, | |
8432 | proto_perl->IDir, | |
8433 | proto_perl->ISock, | |
8434 | proto_perl->IProc); | |
8435 | } | |
8436 | ||
8437 | PerlInterpreter * | |
8438 | perl_clone_using(PerlInterpreter *proto_perl, UV flags, | |
8439 | struct IPerlMem* ipM, struct IPerlMem* ipMS, | |
8440 | struct IPerlMem* ipMP, struct IPerlEnv* ipE, | |
8441 | struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO, | |
8442 | struct IPerlDir* ipD, struct IPerlSock* ipS, | |
8443 | struct IPerlProc* ipP) | |
8444 | { | |
8445 | /* XXX many of the string copies here can be optimized if they're | |
8446 | * constants; they need to be allocated as common memory and just | |
8447 | * their pointers copied. */ | |
8448 | ||
8449 | IV i; | |
1d7c1841 GS |
8450 | # ifdef PERL_OBJECT |
8451 | CPerlObj *pPerl = new(ipM) CPerlObj(ipM, ipMS, ipMP, ipE, ipStd, ipLIO, | |
8452 | ipD, ipS, ipP); | |
ba869deb | 8453 | PERL_SET_THX(pPerl); |
1d7c1841 GS |
8454 | # else /* !PERL_OBJECT */ |
8455 | PerlInterpreter *my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter)); | |
ba869deb | 8456 | PERL_SET_THX(my_perl); |
1d7c1841 GS |
8457 | |
8458 | # ifdef DEBUGGING | |
8459 | memset(my_perl, 0xab, sizeof(PerlInterpreter)); | |
8460 | PL_markstack = 0; | |
8461 | PL_scopestack = 0; | |
8462 | PL_savestack = 0; | |
8463 | PL_retstack = 0; | |
66fe0623 | 8464 | PL_sig_pending = 0; |
1d7c1841 GS |
8465 | # else /* !DEBUGGING */ |
8466 | Zero(my_perl, 1, PerlInterpreter); | |
8467 | # endif /* DEBUGGING */ | |
8468 | ||
8469 | /* host pointers */ | |
8470 | PL_Mem = ipM; | |
8471 | PL_MemShared = ipMS; | |
8472 | PL_MemParse = ipMP; | |
8473 | PL_Env = ipE; | |
8474 | PL_StdIO = ipStd; | |
8475 | PL_LIO = ipLIO; | |
8476 | PL_Dir = ipD; | |
8477 | PL_Sock = ipS; | |
8478 | PL_Proc = ipP; | |
8479 | # endif /* PERL_OBJECT */ | |
8480 | #else /* !PERL_IMPLICIT_SYS */ | |
8481 | IV i; | |
1d7c1841 | 8482 | PerlInterpreter *my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter)); |
ba869deb | 8483 | PERL_SET_THX(my_perl); |
1d7c1841 GS |
8484 | |
8485 | # ifdef DEBUGGING | |
8486 | memset(my_perl, 0xab, sizeof(PerlInterpreter)); | |
8487 | PL_markstack = 0; | |
8488 | PL_scopestack = 0; | |
8489 | PL_savestack = 0; | |
8490 | PL_retstack = 0; | |
66fe0623 | 8491 | PL_sig_pending = 0; |
1d7c1841 GS |
8492 | # else /* !DEBUGGING */ |
8493 | Zero(my_perl, 1, PerlInterpreter); | |
8494 | # endif /* DEBUGGING */ | |
8495 | #endif /* PERL_IMPLICIT_SYS */ | |
8496 | ||
8497 | /* arena roots */ | |
8498 | PL_xiv_arenaroot = NULL; | |
8499 | PL_xiv_root = NULL; | |
612f20c3 | 8500 | PL_xnv_arenaroot = NULL; |
1d7c1841 | 8501 | PL_xnv_root = NULL; |
612f20c3 | 8502 | PL_xrv_arenaroot = NULL; |
1d7c1841 | 8503 | PL_xrv_root = NULL; |
612f20c3 | 8504 | PL_xpv_arenaroot = NULL; |
1d7c1841 | 8505 | PL_xpv_root = NULL; |
612f20c3 | 8506 | PL_xpviv_arenaroot = NULL; |
1d7c1841 | 8507 | PL_xpviv_root = NULL; |
612f20c3 | 8508 | PL_xpvnv_arenaroot = NULL; |
1d7c1841 | 8509 | PL_xpvnv_root = NULL; |
612f20c3 | 8510 | PL_xpvcv_arenaroot = NULL; |
1d7c1841 | 8511 | PL_xpvcv_root = NULL; |
612f20c3 | 8512 | PL_xpvav_arenaroot = NULL; |
1d7c1841 | 8513 | PL_xpvav_root = NULL; |
612f20c3 | 8514 | PL_xpvhv_arenaroot = NULL; |
1d7c1841 | 8515 | PL_xpvhv_root = NULL; |
612f20c3 | 8516 | PL_xpvmg_arenaroot = NULL; |
1d7c1841 | 8517 | PL_xpvmg_root = NULL; |
612f20c3 | 8518 | PL_xpvlv_arenaroot = NULL; |
1d7c1841 | 8519 | PL_xpvlv_root = NULL; |
612f20c3 | 8520 | PL_xpvbm_arenaroot = NULL; |
1d7c1841 | 8521 | PL_xpvbm_root = NULL; |
612f20c3 | 8522 | PL_he_arenaroot = NULL; |
1d7c1841 GS |
8523 | PL_he_root = NULL; |
8524 | PL_nice_chunk = NULL; | |
8525 | PL_nice_chunk_size = 0; | |
8526 | PL_sv_count = 0; | |
8527 | PL_sv_objcount = 0; | |
8528 | PL_sv_root = Nullsv; | |
8529 | PL_sv_arenaroot = Nullsv; | |
8530 | ||
8531 | PL_debug = proto_perl->Idebug; | |
8532 | ||
8533 | /* create SV map for pointer relocation */ | |
8534 | PL_ptr_table = ptr_table_new(); | |
8535 | ||
8536 | /* initialize these special pointers as early as possible */ | |
8537 | SvANY(&PL_sv_undef) = NULL; | |
8538 | SvREFCNT(&PL_sv_undef) = (~(U32)0)/2; | |
8539 | SvFLAGS(&PL_sv_undef) = SVf_READONLY|SVt_NULL; | |
8540 | ptr_table_store(PL_ptr_table, &proto_perl->Isv_undef, &PL_sv_undef); | |
8541 | ||
8542 | #ifdef PERL_OBJECT | |
8543 | SvUPGRADE(&PL_sv_no, SVt_PVNV); | |
8544 | #else | |
8545 | SvANY(&PL_sv_no) = new_XPVNV(); | |
8546 | #endif | |
8547 | SvREFCNT(&PL_sv_no) = (~(U32)0)/2; | |
8548 | SvFLAGS(&PL_sv_no) = SVp_NOK|SVf_NOK|SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV; | |
8549 | SvPVX(&PL_sv_no) = SAVEPVN(PL_No, 0); | |
8550 | SvCUR(&PL_sv_no) = 0; | |
8551 | SvLEN(&PL_sv_no) = 1; | |
8552 | SvNVX(&PL_sv_no) = 0; | |
8553 | ptr_table_store(PL_ptr_table, &proto_perl->Isv_no, &PL_sv_no); | |
8554 | ||
8555 | #ifdef PERL_OBJECT | |
8556 | SvUPGRADE(&PL_sv_yes, SVt_PVNV); | |
8557 | #else | |
8558 | SvANY(&PL_sv_yes) = new_XPVNV(); | |
8559 | #endif | |
8560 | SvREFCNT(&PL_sv_yes) = (~(U32)0)/2; | |
8561 | SvFLAGS(&PL_sv_yes) = SVp_NOK|SVf_NOK|SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV; | |
8562 | SvPVX(&PL_sv_yes) = SAVEPVN(PL_Yes, 1); | |
8563 | SvCUR(&PL_sv_yes) = 1; | |
8564 | SvLEN(&PL_sv_yes) = 2; | |
8565 | SvNVX(&PL_sv_yes) = 1; | |
8566 | ptr_table_store(PL_ptr_table, &proto_perl->Isv_yes, &PL_sv_yes); | |
8567 | ||
8568 | /* create shared string table */ | |
8569 | PL_strtab = newHV(); | |
8570 | HvSHAREKEYS_off(PL_strtab); | |
8571 | hv_ksplit(PL_strtab, 512); | |
8572 | ptr_table_store(PL_ptr_table, proto_perl->Istrtab, PL_strtab); | |
8573 | ||
8574 | PL_compiling = proto_perl->Icompiling; | |
8575 | PL_compiling.cop_stashpv = SAVEPV(PL_compiling.cop_stashpv); | |
8576 | PL_compiling.cop_file = SAVEPV(PL_compiling.cop_file); | |
8577 | ptr_table_store(PL_ptr_table, &proto_perl->Icompiling, &PL_compiling); | |
8578 | if (!specialWARN(PL_compiling.cop_warnings)) | |
8579 | PL_compiling.cop_warnings = sv_dup_inc(PL_compiling.cop_warnings); | |
ac27b0f5 NIS |
8580 | if (!specialCopIO(PL_compiling.cop_io)) |
8581 | PL_compiling.cop_io = sv_dup_inc(PL_compiling.cop_io); | |
1d7c1841 GS |
8582 | PL_curcop = (COP*)any_dup(proto_perl->Tcurcop, proto_perl); |
8583 | ||
8584 | /* pseudo environmental stuff */ | |
8585 | PL_origargc = proto_perl->Iorigargc; | |
8586 | i = PL_origargc; | |
8587 | New(0, PL_origargv, i+1, char*); | |
8588 | PL_origargv[i] = '\0'; | |
8589 | while (i-- > 0) { | |
8590 | PL_origargv[i] = SAVEPV(proto_perl->Iorigargv[i]); | |
8591 | } | |
8592 | PL_envgv = gv_dup(proto_perl->Ienvgv); | |
8593 | PL_incgv = gv_dup(proto_perl->Iincgv); | |
8594 | PL_hintgv = gv_dup(proto_perl->Ihintgv); | |
8595 | PL_origfilename = SAVEPV(proto_perl->Iorigfilename); | |
8596 | PL_diehook = sv_dup_inc(proto_perl->Idiehook); | |
8597 | PL_warnhook = sv_dup_inc(proto_perl->Iwarnhook); | |
8598 | ||
8599 | /* switches */ | |
8600 | PL_minus_c = proto_perl->Iminus_c; | |
a7cb1f99 | 8601 | PL_patchlevel = sv_dup_inc(proto_perl->Ipatchlevel); |
1d7c1841 GS |
8602 | PL_localpatches = proto_perl->Ilocalpatches; |
8603 | PL_splitstr = proto_perl->Isplitstr; | |
8604 | PL_preprocess = proto_perl->Ipreprocess; | |
8605 | PL_minus_n = proto_perl->Iminus_n; | |
8606 | PL_minus_p = proto_perl->Iminus_p; | |
8607 | PL_minus_l = proto_perl->Iminus_l; | |
8608 | PL_minus_a = proto_perl->Iminus_a; | |
8609 | PL_minus_F = proto_perl->Iminus_F; | |
8610 | PL_doswitches = proto_perl->Idoswitches; | |
8611 | PL_dowarn = proto_perl->Idowarn; | |
8612 | PL_doextract = proto_perl->Idoextract; | |
8613 | PL_sawampersand = proto_perl->Isawampersand; | |
8614 | PL_unsafe = proto_perl->Iunsafe; | |
8615 | PL_inplace = SAVEPV(proto_perl->Iinplace); | |
8616 | PL_e_script = sv_dup_inc(proto_perl->Ie_script); | |
8617 | PL_perldb = proto_perl->Iperldb; | |
8618 | PL_perl_destruct_level = proto_perl->Iperl_destruct_level; | |
8619 | ||
8620 | /* magical thingies */ | |
8621 | /* XXX time(&PL_basetime) when asked for? */ | |
8622 | PL_basetime = proto_perl->Ibasetime; | |
8623 | PL_formfeed = sv_dup(proto_perl->Iformfeed); | |
8624 | ||
8625 | PL_maxsysfd = proto_perl->Imaxsysfd; | |
8626 | PL_multiline = proto_perl->Imultiline; | |
8627 | PL_statusvalue = proto_perl->Istatusvalue; | |
8628 | #ifdef VMS | |
8629 | PL_statusvalue_vms = proto_perl->Istatusvalue_vms; | |
8630 | #endif | |
8631 | ||
8632 | /* shortcuts to various I/O objects */ | |
8633 | PL_stdingv = gv_dup(proto_perl->Istdingv); | |
8634 | PL_stderrgv = gv_dup(proto_perl->Istderrgv); | |
8635 | PL_defgv = gv_dup(proto_perl->Idefgv); | |
8636 | PL_argvgv = gv_dup(proto_perl->Iargvgv); | |
8637 | PL_argvoutgv = gv_dup(proto_perl->Iargvoutgv); | |
8638 | PL_argvout_stack = av_dup(proto_perl->Iargvout_stack); | |
8639 | ||
8640 | /* shortcuts to regexp stuff */ | |
8641 | PL_replgv = gv_dup(proto_perl->Ireplgv); | |
8642 | ||
8643 | /* shortcuts to misc objects */ | |
8644 | PL_errgv = gv_dup(proto_perl->Ierrgv); | |
8645 | ||
8646 | /* shortcuts to debugging objects */ | |
8647 | PL_DBgv = gv_dup(proto_perl->IDBgv); | |
8648 | PL_DBline = gv_dup(proto_perl->IDBline); | |
8649 | PL_DBsub = gv_dup(proto_perl->IDBsub); | |
8650 | PL_DBsingle = sv_dup(proto_perl->IDBsingle); | |
8651 | PL_DBtrace = sv_dup(proto_perl->IDBtrace); | |
8652 | PL_DBsignal = sv_dup(proto_perl->IDBsignal); | |
8653 | PL_lineary = av_dup(proto_perl->Ilineary); | |
8654 | PL_dbargs = av_dup(proto_perl->Idbargs); | |
8655 | ||
8656 | /* symbol tables */ | |
8657 | PL_defstash = hv_dup_inc(proto_perl->Tdefstash); | |
8658 | PL_curstash = hv_dup(proto_perl->Tcurstash); | |
8659 | PL_debstash = hv_dup(proto_perl->Idebstash); | |
8660 | PL_globalstash = hv_dup(proto_perl->Iglobalstash); | |
8661 | PL_curstname = sv_dup_inc(proto_perl->Icurstname); | |
8662 | ||
8663 | PL_beginav = av_dup_inc(proto_perl->Ibeginav); | |
8664 | PL_endav = av_dup_inc(proto_perl->Iendav); | |
7d30b5c4 | 8665 | PL_checkav = av_dup_inc(proto_perl->Icheckav); |
1d7c1841 GS |
8666 | PL_initav = av_dup_inc(proto_perl->Iinitav); |
8667 | ||
8668 | PL_sub_generation = proto_perl->Isub_generation; | |
8669 | ||
8670 | /* funky return mechanisms */ | |
8671 | PL_forkprocess = proto_perl->Iforkprocess; | |
8672 | ||
8673 | /* subprocess state */ | |
8674 | PL_fdpid = av_dup_inc(proto_perl->Ifdpid); | |
8675 | ||
8676 | /* internal state */ | |
8677 | PL_tainting = proto_perl->Itainting; | |
8678 | PL_maxo = proto_perl->Imaxo; | |
8679 | if (proto_perl->Iop_mask) | |
8680 | PL_op_mask = SAVEPVN(proto_perl->Iop_mask, PL_maxo); | |
8681 | else | |
8682 | PL_op_mask = Nullch; | |
8683 | ||
8684 | /* current interpreter roots */ | |
8685 | PL_main_cv = cv_dup_inc(proto_perl->Imain_cv); | |
8686 | PL_main_root = OpREFCNT_inc(proto_perl->Imain_root); | |
8687 | PL_main_start = proto_perl->Imain_start; | |
e977893f | 8688 | PL_eval_root = proto_perl->Ieval_root; |
1d7c1841 GS |
8689 | PL_eval_start = proto_perl->Ieval_start; |
8690 | ||
8691 | /* runtime control stuff */ | |
8692 | PL_curcopdb = (COP*)any_dup(proto_perl->Icurcopdb, proto_perl); | |
8693 | PL_copline = proto_perl->Icopline; | |
8694 | ||
8695 | PL_filemode = proto_perl->Ifilemode; | |
8696 | PL_lastfd = proto_perl->Ilastfd; | |
8697 | PL_oldname = proto_perl->Ioldname; /* XXX not quite right */ | |
8698 | PL_Argv = NULL; | |
8699 | PL_Cmd = Nullch; | |
8700 | PL_gensym = proto_perl->Igensym; | |
8701 | PL_preambled = proto_perl->Ipreambled; | |
8702 | PL_preambleav = av_dup_inc(proto_perl->Ipreambleav); | |
8703 | PL_laststatval = proto_perl->Ilaststatval; | |
8704 | PL_laststype = proto_perl->Ilaststype; | |
8705 | PL_mess_sv = Nullsv; | |
8706 | ||
7889fe52 | 8707 | PL_ors_sv = sv_dup_inc(proto_perl->Iors_sv); |
1d7c1841 GS |
8708 | PL_ofmt = SAVEPV(proto_perl->Iofmt); |
8709 | ||
8710 | /* interpreter atexit processing */ | |
8711 | PL_exitlistlen = proto_perl->Iexitlistlen; | |
8712 | if (PL_exitlistlen) { | |
8713 | New(0, PL_exitlist, PL_exitlistlen, PerlExitListEntry); | |
8714 | Copy(proto_perl->Iexitlist, PL_exitlist, PL_exitlistlen, PerlExitListEntry); | |
8715 | } | |
8716 | else | |
8717 | PL_exitlist = (PerlExitListEntry*)NULL; | |
8718 | PL_modglobal = hv_dup_inc(proto_perl->Imodglobal); | |
8719 | ||
8720 | PL_profiledata = NULL; | |
8721 | PL_rsfp = fp_dup(proto_perl->Irsfp, '<'); | |
8722 | /* PL_rsfp_filters entries have fake IoDIRP() */ | |
8723 | PL_rsfp_filters = av_dup_inc(proto_perl->Irsfp_filters); | |
8724 | ||
8725 | PL_compcv = cv_dup(proto_perl->Icompcv); | |
8726 | PL_comppad = av_dup(proto_perl->Icomppad); | |
8727 | PL_comppad_name = av_dup(proto_perl->Icomppad_name); | |
8728 | PL_comppad_name_fill = proto_perl->Icomppad_name_fill; | |
8729 | PL_comppad_name_floor = proto_perl->Icomppad_name_floor; | |
8730 | PL_curpad = (SV**)ptr_table_fetch(PL_ptr_table, | |
8731 | proto_perl->Tcurpad); | |
8732 | ||
8733 | #ifdef HAVE_INTERP_INTERN | |
8734 | sys_intern_dup(&proto_perl->Isys_intern, &PL_sys_intern); | |
8735 | #endif | |
8736 | ||
8737 | /* more statics moved here */ | |
8738 | PL_generation = proto_perl->Igeneration; | |
8739 | PL_DBcv = cv_dup(proto_perl->IDBcv); | |
1d7c1841 GS |
8740 | |
8741 | PL_in_clean_objs = proto_perl->Iin_clean_objs; | |
8742 | PL_in_clean_all = proto_perl->Iin_clean_all; | |
8743 | ||
8744 | PL_uid = proto_perl->Iuid; | |
8745 | PL_euid = proto_perl->Ieuid; | |
8746 | PL_gid = proto_perl->Igid; | |
8747 | PL_egid = proto_perl->Iegid; | |
8748 | PL_nomemok = proto_perl->Inomemok; | |
8749 | PL_an = proto_perl->Ian; | |
8750 | PL_cop_seqmax = proto_perl->Icop_seqmax; | |
8751 | PL_op_seqmax = proto_perl->Iop_seqmax; | |
8752 | PL_evalseq = proto_perl->Ievalseq; | |
8753 | PL_origenviron = proto_perl->Iorigenviron; /* XXX not quite right */ | |
8754 | PL_origalen = proto_perl->Iorigalen; | |
8755 | PL_pidstatus = newHV(); /* XXX flag for cloning? */ | |
8756 | PL_osname = SAVEPV(proto_perl->Iosname); | |
8757 | PL_sh_path = SAVEPV(proto_perl->Ish_path); | |
8758 | PL_sighandlerp = proto_perl->Isighandlerp; | |
8759 | ||
8760 | ||
8761 | PL_runops = proto_perl->Irunops; | |
8762 | ||
8763 | Copy(proto_perl->Itokenbuf, PL_tokenbuf, 256, char); | |
8764 | ||
8765 | #ifdef CSH | |
8766 | PL_cshlen = proto_perl->Icshlen; | |
8767 | PL_cshname = SAVEPVN(proto_perl->Icshname, PL_cshlen); | |
8768 | #endif | |
8769 | ||
8770 | PL_lex_state = proto_perl->Ilex_state; | |
8771 | PL_lex_defer = proto_perl->Ilex_defer; | |
8772 | PL_lex_expect = proto_perl->Ilex_expect; | |
8773 | PL_lex_formbrack = proto_perl->Ilex_formbrack; | |
8774 | PL_lex_dojoin = proto_perl->Ilex_dojoin; | |
8775 | PL_lex_starts = proto_perl->Ilex_starts; | |
8776 | PL_lex_stuff = sv_dup_inc(proto_perl->Ilex_stuff); | |
8777 | PL_lex_repl = sv_dup_inc(proto_perl->Ilex_repl); | |
8778 | PL_lex_op = proto_perl->Ilex_op; | |
8779 | PL_lex_inpat = proto_perl->Ilex_inpat; | |
8780 | PL_lex_inwhat = proto_perl->Ilex_inwhat; | |
8781 | PL_lex_brackets = proto_perl->Ilex_brackets; | |
8782 | i = (PL_lex_brackets < 120 ? 120 : PL_lex_brackets); | |
8783 | PL_lex_brackstack = SAVEPVN(proto_perl->Ilex_brackstack,i); | |
8784 | PL_lex_casemods = proto_perl->Ilex_casemods; | |
8785 | i = (PL_lex_casemods < 12 ? 12 : PL_lex_casemods); | |
8786 | PL_lex_casestack = SAVEPVN(proto_perl->Ilex_casestack,i); | |
8787 | ||
8788 | Copy(proto_perl->Inextval, PL_nextval, 5, YYSTYPE); | |
8789 | Copy(proto_perl->Inexttype, PL_nexttype, 5, I32); | |
8790 | PL_nexttoke = proto_perl->Inexttoke; | |
8791 | ||
8792 | PL_linestr = sv_dup_inc(proto_perl->Ilinestr); | |
8793 | i = proto_perl->Ibufptr - SvPVX(proto_perl->Ilinestr); | |
8794 | PL_bufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
8795 | i = proto_perl->Ioldbufptr - SvPVX(proto_perl->Ilinestr); | |
8796 | PL_oldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
8797 | i = proto_perl->Ioldoldbufptr - SvPVX(proto_perl->Ilinestr); | |
8798 | PL_oldoldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
8799 | PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr); | |
8800 | i = proto_perl->Ilinestart - SvPVX(proto_perl->Ilinestr); | |
8801 | PL_linestart = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
8802 | PL_pending_ident = proto_perl->Ipending_ident; | |
8803 | PL_sublex_info = proto_perl->Isublex_info; /* XXX not quite right */ | |
8804 | ||
8805 | PL_expect = proto_perl->Iexpect; | |
8806 | ||
8807 | PL_multi_start = proto_perl->Imulti_start; | |
8808 | PL_multi_end = proto_perl->Imulti_end; | |
8809 | PL_multi_open = proto_perl->Imulti_open; | |
8810 | PL_multi_close = proto_perl->Imulti_close; | |
8811 | ||
8812 | PL_error_count = proto_perl->Ierror_count; | |
8813 | PL_subline = proto_perl->Isubline; | |
8814 | PL_subname = sv_dup_inc(proto_perl->Isubname); | |
8815 | ||
8816 | PL_min_intro_pending = proto_perl->Imin_intro_pending; | |
8817 | PL_max_intro_pending = proto_perl->Imax_intro_pending; | |
8818 | PL_padix = proto_perl->Ipadix; | |
8819 | PL_padix_floor = proto_perl->Ipadix_floor; | |
8820 | PL_pad_reset_pending = proto_perl->Ipad_reset_pending; | |
8821 | ||
8822 | i = proto_perl->Ilast_uni - SvPVX(proto_perl->Ilinestr); | |
8823 | PL_last_uni = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
8824 | i = proto_perl->Ilast_lop - SvPVX(proto_perl->Ilinestr); | |
8825 | PL_last_lop = SvPVX(PL_linestr) + (i < 0 ? 0 : i); | |
8826 | PL_last_lop_op = proto_perl->Ilast_lop_op; | |
8827 | PL_in_my = proto_perl->Iin_my; | |
8828 | PL_in_my_stash = hv_dup(proto_perl->Iin_my_stash); | |
8829 | #ifdef FCRYPT | |
8830 | PL_cryptseen = proto_perl->Icryptseen; | |
8831 | #endif | |
8832 | ||
8833 | PL_hints = proto_perl->Ihints; | |
8834 | ||
8835 | PL_amagic_generation = proto_perl->Iamagic_generation; | |
8836 | ||
8837 | #ifdef USE_LOCALE_COLLATE | |
8838 | PL_collation_ix = proto_perl->Icollation_ix; | |
8839 | PL_collation_name = SAVEPV(proto_perl->Icollation_name); | |
8840 | PL_collation_standard = proto_perl->Icollation_standard; | |
8841 | PL_collxfrm_base = proto_perl->Icollxfrm_base; | |
8842 | PL_collxfrm_mult = proto_perl->Icollxfrm_mult; | |
8843 | #endif /* USE_LOCALE_COLLATE */ | |
8844 | ||
8845 | #ifdef USE_LOCALE_NUMERIC | |
8846 | PL_numeric_name = SAVEPV(proto_perl->Inumeric_name); | |
8847 | PL_numeric_standard = proto_perl->Inumeric_standard; | |
8848 | PL_numeric_local = proto_perl->Inumeric_local; | |
ac634a9a | 8849 | PL_numeric_radix = sv_dup_inc(proto_perl->Inumeric_radix); |
1d7c1841 GS |
8850 | #endif /* !USE_LOCALE_NUMERIC */ |
8851 | ||
8852 | /* utf8 character classes */ | |
8853 | PL_utf8_alnum = sv_dup_inc(proto_perl->Iutf8_alnum); | |
8854 | PL_utf8_alnumc = sv_dup_inc(proto_perl->Iutf8_alnumc); | |
8855 | PL_utf8_ascii = sv_dup_inc(proto_perl->Iutf8_ascii); | |
8856 | PL_utf8_alpha = sv_dup_inc(proto_perl->Iutf8_alpha); | |
8857 | PL_utf8_space = sv_dup_inc(proto_perl->Iutf8_space); | |
8858 | PL_utf8_cntrl = sv_dup_inc(proto_perl->Iutf8_cntrl); | |
8859 | PL_utf8_graph = sv_dup_inc(proto_perl->Iutf8_graph); | |
8860 | PL_utf8_digit = sv_dup_inc(proto_perl->Iutf8_digit); | |
8861 | PL_utf8_upper = sv_dup_inc(proto_perl->Iutf8_upper); | |
8862 | PL_utf8_lower = sv_dup_inc(proto_perl->Iutf8_lower); | |
8863 | PL_utf8_print = sv_dup_inc(proto_perl->Iutf8_print); | |
8864 | PL_utf8_punct = sv_dup_inc(proto_perl->Iutf8_punct); | |
8865 | PL_utf8_xdigit = sv_dup_inc(proto_perl->Iutf8_xdigit); | |
8866 | PL_utf8_mark = sv_dup_inc(proto_perl->Iutf8_mark); | |
8867 | PL_utf8_toupper = sv_dup_inc(proto_perl->Iutf8_toupper); | |
8868 | PL_utf8_totitle = sv_dup_inc(proto_perl->Iutf8_totitle); | |
8869 | PL_utf8_tolower = sv_dup_inc(proto_perl->Iutf8_tolower); | |
8870 | ||
8871 | /* swatch cache */ | |
8872 | PL_last_swash_hv = Nullhv; /* reinits on demand */ | |
8873 | PL_last_swash_klen = 0; | |
8874 | PL_last_swash_key[0]= '\0'; | |
8875 | PL_last_swash_tmps = (U8*)NULL; | |
8876 | PL_last_swash_slen = 0; | |
8877 | ||
8878 | /* perly.c globals */ | |
8879 | PL_yydebug = proto_perl->Iyydebug; | |
8880 | PL_yynerrs = proto_perl->Iyynerrs; | |
8881 | PL_yyerrflag = proto_perl->Iyyerrflag; | |
8882 | PL_yychar = proto_perl->Iyychar; | |
8883 | PL_yyval = proto_perl->Iyyval; | |
8884 | PL_yylval = proto_perl->Iyylval; | |
8885 | ||
8886 | PL_glob_index = proto_perl->Iglob_index; | |
8887 | PL_srand_called = proto_perl->Isrand_called; | |
8888 | PL_uudmap['M'] = 0; /* reinits on demand */ | |
8889 | PL_bitcount = Nullch; /* reinits on demand */ | |
8890 | ||
66fe0623 NIS |
8891 | if (proto_perl->Ipsig_pend) { |
8892 | Newz(0, PL_psig_pend, SIG_SIZE, int); | |
9dd79c3f | 8893 | } |
66fe0623 NIS |
8894 | else { |
8895 | PL_psig_pend = (int*)NULL; | |
8896 | } | |
8897 | ||
1d7c1841 | 8898 | if (proto_perl->Ipsig_ptr) { |
76d3c696 JH |
8899 | Newz(0, PL_psig_ptr, SIG_SIZE, SV*); |
8900 | Newz(0, PL_psig_name, SIG_SIZE, SV*); | |
76d3c696 JH |
8901 | for (i = 1; i < SIG_SIZE; i++) { |
8902 | PL_psig_ptr[i] = sv_dup_inc(proto_perl->Ipsig_ptr[i]); | |
1d7c1841 GS |
8903 | PL_psig_name[i] = sv_dup_inc(proto_perl->Ipsig_name[i]); |
8904 | } | |
8905 | } | |
8906 | else { | |
8907 | PL_psig_ptr = (SV**)NULL; | |
8908 | PL_psig_name = (SV**)NULL; | |
8909 | } | |
8910 | ||
8911 | /* thrdvar.h stuff */ | |
8912 | ||
8913 | if (flags & 1) { | |
8914 | /* next allocation will be PL_tmps_stack[PL_tmps_ix+1] */ | |
8915 | PL_tmps_ix = proto_perl->Ttmps_ix; | |
8916 | PL_tmps_max = proto_perl->Ttmps_max; | |
8917 | PL_tmps_floor = proto_perl->Ttmps_floor; | |
8918 | Newz(50, PL_tmps_stack, PL_tmps_max, SV*); | |
8919 | i = 0; | |
8920 | while (i <= PL_tmps_ix) { | |
8921 | PL_tmps_stack[i] = sv_dup_inc(proto_perl->Ttmps_stack[i]); | |
8922 | ++i; | |
8923 | } | |
8924 | ||
8925 | /* next PUSHMARK() sets *(PL_markstack_ptr+1) */ | |
8926 | i = proto_perl->Tmarkstack_max - proto_perl->Tmarkstack; | |
8927 | Newz(54, PL_markstack, i, I32); | |
8928 | PL_markstack_max = PL_markstack + (proto_perl->Tmarkstack_max | |
8929 | - proto_perl->Tmarkstack); | |
8930 | PL_markstack_ptr = PL_markstack + (proto_perl->Tmarkstack_ptr | |
8931 | - proto_perl->Tmarkstack); | |
8932 | Copy(proto_perl->Tmarkstack, PL_markstack, | |
8933 | PL_markstack_ptr - PL_markstack + 1, I32); | |
8934 | ||
8935 | /* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix] | |
8936 | * NOTE: unlike the others! */ | |
8937 | PL_scopestack_ix = proto_perl->Tscopestack_ix; | |
8938 | PL_scopestack_max = proto_perl->Tscopestack_max; | |
8939 | Newz(54, PL_scopestack, PL_scopestack_max, I32); | |
8940 | Copy(proto_perl->Tscopestack, PL_scopestack, PL_scopestack_ix, I32); | |
8941 | ||
8942 | /* next push_return() sets PL_retstack[PL_retstack_ix] | |
8943 | * NOTE: unlike the others! */ | |
8944 | PL_retstack_ix = proto_perl->Tretstack_ix; | |
8945 | PL_retstack_max = proto_perl->Tretstack_max; | |
8946 | Newz(54, PL_retstack, PL_retstack_max, OP*); | |
8947 | Copy(proto_perl->Tretstack, PL_retstack, PL_retstack_ix, I32); | |
8948 | ||
8949 | /* NOTE: si_dup() looks at PL_markstack */ | |
8950 | PL_curstackinfo = si_dup(proto_perl->Tcurstackinfo); | |
8951 | ||
8952 | /* PL_curstack = PL_curstackinfo->si_stack; */ | |
8953 | PL_curstack = av_dup(proto_perl->Tcurstack); | |
8954 | PL_mainstack = av_dup(proto_perl->Tmainstack); | |
8955 | ||
8956 | /* next PUSHs() etc. set *(PL_stack_sp+1) */ | |
8957 | PL_stack_base = AvARRAY(PL_curstack); | |
8958 | PL_stack_sp = PL_stack_base + (proto_perl->Tstack_sp | |
8959 | - proto_perl->Tstack_base); | |
8960 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); | |
8961 | ||
8962 | /* next SSPUSHFOO() sets PL_savestack[PL_savestack_ix] | |
8963 | * NOTE: unlike the others! */ | |
8964 | PL_savestack_ix = proto_perl->Tsavestack_ix; | |
8965 | PL_savestack_max = proto_perl->Tsavestack_max; | |
8966 | /*Newz(54, PL_savestack, PL_savestack_max, ANY);*/ | |
8967 | PL_savestack = ss_dup(proto_perl); | |
8968 | } | |
8969 | else { | |
8970 | init_stacks(); | |
985e7056 | 8971 | ENTER; /* perl_destruct() wants to LEAVE; */ |
1d7c1841 GS |
8972 | } |
8973 | ||
8974 | PL_start_env = proto_perl->Tstart_env; /* XXXXXX */ | |
8975 | PL_top_env = &PL_start_env; | |
8976 | ||
8977 | PL_op = proto_perl->Top; | |
8978 | ||
8979 | PL_Sv = Nullsv; | |
8980 | PL_Xpv = (XPV*)NULL; | |
8981 | PL_na = proto_perl->Tna; | |
8982 | ||
8983 | PL_statbuf = proto_perl->Tstatbuf; | |
8984 | PL_statcache = proto_perl->Tstatcache; | |
8985 | PL_statgv = gv_dup(proto_perl->Tstatgv); | |
8986 | PL_statname = sv_dup_inc(proto_perl->Tstatname); | |
8987 | #ifdef HAS_TIMES | |
8988 | PL_timesbuf = proto_perl->Ttimesbuf; | |
8989 | #endif | |
8990 | ||
8991 | PL_tainted = proto_perl->Ttainted; | |
8992 | PL_curpm = proto_perl->Tcurpm; /* XXX No PMOP ref count */ | |
8993 | PL_nrs = sv_dup_inc(proto_perl->Tnrs); | |
8994 | PL_rs = sv_dup_inc(proto_perl->Trs); | |
8995 | PL_last_in_gv = gv_dup(proto_perl->Tlast_in_gv); | |
7889fe52 | 8996 | PL_ofs_sv = sv_dup_inc(proto_perl->Tofs_sv); |
1d7c1841 GS |
8997 | PL_defoutgv = gv_dup_inc(proto_perl->Tdefoutgv); |
8998 | PL_chopset = proto_perl->Tchopset; /* XXX never deallocated */ | |
8999 | PL_toptarget = sv_dup_inc(proto_perl->Ttoptarget); | |
9000 | PL_bodytarget = sv_dup_inc(proto_perl->Tbodytarget); | |
9001 | PL_formtarget = sv_dup(proto_perl->Tformtarget); | |
9002 | ||
9003 | PL_restartop = proto_perl->Trestartop; | |
9004 | PL_in_eval = proto_perl->Tin_eval; | |
9005 | PL_delaymagic = proto_perl->Tdelaymagic; | |
9006 | PL_dirty = proto_perl->Tdirty; | |
9007 | PL_localizing = proto_perl->Tlocalizing; | |
9008 | ||
14dd3ad8 | 9009 | #ifdef PERL_FLEXIBLE_EXCEPTIONS |
1d7c1841 | 9010 | PL_protect = proto_perl->Tprotect; |
14dd3ad8 | 9011 | #endif |
1d7c1841 GS |
9012 | PL_errors = sv_dup_inc(proto_perl->Terrors); |
9013 | PL_av_fetch_sv = Nullsv; | |
9014 | PL_hv_fetch_sv = Nullsv; | |
9015 | Zero(&PL_hv_fetch_ent_mh, 1, HE); /* XXX */ | |
9016 | PL_modcount = proto_perl->Tmodcount; | |
9017 | PL_lastgotoprobe = Nullop; | |
9018 | PL_dumpindent = proto_perl->Tdumpindent; | |
9019 | ||
9020 | PL_sortcop = (OP*)any_dup(proto_perl->Tsortcop, proto_perl); | |
9021 | PL_sortstash = hv_dup(proto_perl->Tsortstash); | |
9022 | PL_firstgv = gv_dup(proto_perl->Tfirstgv); | |
9023 | PL_secondgv = gv_dup(proto_perl->Tsecondgv); | |
9024 | PL_sortcxix = proto_perl->Tsortcxix; | |
9025 | PL_efloatbuf = Nullch; /* reinits on demand */ | |
9026 | PL_efloatsize = 0; /* reinits on demand */ | |
9027 | ||
9028 | /* regex stuff */ | |
9029 | ||
9030 | PL_screamfirst = NULL; | |
9031 | PL_screamnext = NULL; | |
9032 | PL_maxscream = -1; /* reinits on demand */ | |
9033 | PL_lastscream = Nullsv; | |
9034 | ||
9035 | PL_watchaddr = NULL; | |
9036 | PL_watchok = Nullch; | |
9037 | ||
9038 | PL_regdummy = proto_perl->Tregdummy; | |
9039 | PL_regcomp_parse = Nullch; | |
9040 | PL_regxend = Nullch; | |
9041 | PL_regcode = (regnode*)NULL; | |
9042 | PL_regnaughty = 0; | |
9043 | PL_regsawback = 0; | |
9044 | PL_regprecomp = Nullch; | |
9045 | PL_regnpar = 0; | |
9046 | PL_regsize = 0; | |
9047 | PL_regflags = 0; | |
9048 | PL_regseen = 0; | |
9049 | PL_seen_zerolen = 0; | |
9050 | PL_seen_evals = 0; | |
9051 | PL_regcomp_rx = (regexp*)NULL; | |
9052 | PL_extralen = 0; | |
9053 | PL_colorset = 0; /* reinits PL_colors[] */ | |
9054 | /*PL_colors[6] = {0,0,0,0,0,0};*/ | |
9055 | PL_reg_whilem_seen = 0; | |
9056 | PL_reginput = Nullch; | |
9057 | PL_regbol = Nullch; | |
9058 | PL_regeol = Nullch; | |
9059 | PL_regstartp = (I32*)NULL; | |
9060 | PL_regendp = (I32*)NULL; | |
9061 | PL_reglastparen = (U32*)NULL; | |
9062 | PL_regtill = Nullch; | |
9063 | PL_regprev = '\n'; | |
9064 | PL_reg_start_tmp = (char**)NULL; | |
9065 | PL_reg_start_tmpl = 0; | |
9066 | PL_regdata = (struct reg_data*)NULL; | |
9067 | PL_bostr = Nullch; | |
9068 | PL_reg_flags = 0; | |
9069 | PL_reg_eval_set = 0; | |
9070 | PL_regnarrate = 0; | |
9071 | PL_regprogram = (regnode*)NULL; | |
9072 | PL_regindent = 0; | |
9073 | PL_regcc = (CURCUR*)NULL; | |
9074 | PL_reg_call_cc = (struct re_cc_state*)NULL; | |
9075 | PL_reg_re = (regexp*)NULL; | |
9076 | PL_reg_ganch = Nullch; | |
9077 | PL_reg_sv = Nullsv; | |
9078 | PL_reg_magic = (MAGIC*)NULL; | |
9079 | PL_reg_oldpos = 0; | |
9080 | PL_reg_oldcurpm = (PMOP*)NULL; | |
9081 | PL_reg_curpm = (PMOP*)NULL; | |
9082 | PL_reg_oldsaved = Nullch; | |
9083 | PL_reg_oldsavedlen = 0; | |
9084 | PL_reg_maxiter = 0; | |
9085 | PL_reg_leftiter = 0; | |
9086 | PL_reg_poscache = Nullch; | |
9087 | PL_reg_poscache_size= 0; | |
9088 | ||
9089 | /* RE engine - function pointers */ | |
9090 | PL_regcompp = proto_perl->Tregcompp; | |
9091 | PL_regexecp = proto_perl->Tregexecp; | |
9092 | PL_regint_start = proto_perl->Tregint_start; | |
9093 | PL_regint_string = proto_perl->Tregint_string; | |
9094 | PL_regfree = proto_perl->Tregfree; | |
9095 | ||
9096 | PL_reginterp_cnt = 0; | |
9097 | PL_reg_starttry = 0; | |
9098 | ||
9099 | #ifdef PERL_OBJECT | |
9100 | return (PerlInterpreter*)pPerl; | |
9101 | #else | |
9102 | return my_perl; | |
9103 | #endif | |
9104 | } | |
9105 | ||
9106 | #else /* !USE_ITHREADS */ | |
51371543 GS |
9107 | |
9108 | #ifdef PERL_OBJECT | |
51371543 GS |
9109 | #include "XSUB.h" |
9110 | #endif | |
9111 | ||
1d7c1841 GS |
9112 | #endif /* USE_ITHREADS */ |
9113 | ||
51371543 GS |
9114 | static void |
9115 | do_report_used(pTHXo_ SV *sv) | |
9116 | { | |
9117 | if (SvTYPE(sv) != SVTYPEMASK) { | |
bf49b057 | 9118 | PerlIO_printf(Perl_debug_log, "****\n"); |
51371543 GS |
9119 | sv_dump(sv); |
9120 | } | |
9121 | } | |
9122 | ||
9123 | static void | |
9124 | do_clean_objs(pTHXo_ SV *sv) | |
9125 | { | |
9126 | SV* rv; | |
9127 | ||
9128 | if (SvROK(sv) && SvOBJECT(rv = SvRV(sv))) { | |
9129 | DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(sv));) | |
8b6e653b HS |
9130 | if (SvWEAKREF(sv)) { |
9131 | sv_del_backref(sv); | |
9132 | SvWEAKREF_off(sv); | |
9133 | SvRV(sv) = 0; | |
9134 | } else { | |
9135 | SvROK_off(sv); | |
9136 | SvRV(sv) = 0; | |
9137 | SvREFCNT_dec(rv); | |
9138 | } | |
51371543 GS |
9139 | } |
9140 | ||
9141 | /* XXX Might want to check arrays, etc. */ | |
9142 | } | |
9143 | ||
9144 | #ifndef DISABLE_DESTRUCTOR_KLUDGE | |
9145 | static void | |
9146 | do_clean_named_objs(pTHXo_ SV *sv) | |
9147 | { | |
f472eb5c | 9148 | if (SvTYPE(sv) == SVt_PVGV && GvGP(sv)) { |
51371543 | 9149 | if ( SvOBJECT(GvSV(sv)) || |
155aba94 GS |
9150 | (GvAV(sv) && SvOBJECT(GvAV(sv))) || |
9151 | (GvHV(sv) && SvOBJECT(GvHV(sv))) || | |
9152 | (GvIO(sv) && SvOBJECT(GvIO(sv))) || | |
9153 | (GvCV(sv) && SvOBJECT(GvCV(sv))) ) | |
51371543 GS |
9154 | { |
9155 | DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning named glob object:\n "), sv_dump(sv));) | |
9156 | SvREFCNT_dec(sv); | |
9157 | } | |
9158 | } | |
9159 | } | |
9160 | #endif | |
9161 | ||
9162 | static void | |
9163 | do_clean_all(pTHXo_ SV *sv) | |
9164 | { | |
1d7c1841 | 9165 | DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) );) |
51371543 GS |
9166 | SvFLAGS(sv) |= SVf_BREAK; |
9167 | SvREFCNT_dec(sv); | |
9168 | } | |
8af02333 | 9169 |