| 1 | /* scope.c |
| 2 | * |
| 3 | * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, |
| 4 | * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others |
| 5 | * |
| 6 | * You may distribute under the terms of either the GNU General Public |
| 7 | * License or the Artistic License, as specified in the README file. |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * For the fashion of Minas Tirith was such that it was built on seven |
| 13 | * levels... |
| 14 | * |
| 15 | * [p.751 of _The Lord of the Rings_, V/i: "Minas Tirith"] |
| 16 | */ |
| 17 | |
| 18 | /* This file contains functions to manipulate several of Perl's stacks; |
| 19 | * in particular it contains code to push various types of things onto |
| 20 | * the savestack, then to pop them off and perform the correct restorative |
| 21 | * action for each one. This corresponds to the cleanup Perl does at |
| 22 | * each scope exit. |
| 23 | */ |
| 24 | |
| 25 | #include "EXTERN.h" |
| 26 | #define PERL_IN_SCOPE_C |
| 27 | #include "perl.h" |
| 28 | |
| 29 | SV** |
| 30 | Perl_stack_grow(pTHX_ SV **sp, SV **p, int n) |
| 31 | { |
| 32 | dVAR; |
| 33 | |
| 34 | PERL_ARGS_ASSERT_STACK_GROW; |
| 35 | |
| 36 | PL_stack_sp = sp; |
| 37 | #ifndef STRESS_REALLOC |
| 38 | av_extend(PL_curstack, (p - PL_stack_base) + (n) + 128); |
| 39 | #else |
| 40 | av_extend(PL_curstack, (p - PL_stack_base) + (n) + 1); |
| 41 | #endif |
| 42 | return PL_stack_sp; |
| 43 | } |
| 44 | |
| 45 | #ifndef STRESS_REALLOC |
| 46 | #define GROW(old) ((old) * 3 / 2) |
| 47 | #else |
| 48 | #define GROW(old) ((old) + 1) |
| 49 | #endif |
| 50 | |
| 51 | PERL_SI * |
| 52 | Perl_new_stackinfo(pTHX_ I32 stitems, I32 cxitems) |
| 53 | { |
| 54 | dVAR; |
| 55 | PERL_SI *si; |
| 56 | Newx(si, 1, PERL_SI); |
| 57 | si->si_stack = newAV(); |
| 58 | AvREAL_off(si->si_stack); |
| 59 | av_extend(si->si_stack, stitems > 0 ? stitems-1 : 0); |
| 60 | AvALLOC(si->si_stack)[0] = &PL_sv_undef; |
| 61 | AvFILLp(si->si_stack) = 0; |
| 62 | si->si_prev = 0; |
| 63 | si->si_next = 0; |
| 64 | si->si_cxmax = cxitems - 1; |
| 65 | si->si_cxix = -1; |
| 66 | si->si_type = PERLSI_UNDEF; |
| 67 | Newx(si->si_cxstack, cxitems, PERL_CONTEXT); |
| 68 | /* Without any kind of initialising PUSHSUBST() |
| 69 | * in pp_subst() will read uninitialised heap. */ |
| 70 | PoisonNew(si->si_cxstack, cxitems, PERL_CONTEXT); |
| 71 | return si; |
| 72 | } |
| 73 | |
| 74 | I32 |
| 75 | Perl_cxinc(pTHX) |
| 76 | { |
| 77 | dVAR; |
| 78 | const IV old_max = cxstack_max; |
| 79 | cxstack_max = GROW(cxstack_max); |
| 80 | Renew(cxstack, cxstack_max + 1, PERL_CONTEXT); |
| 81 | /* Without any kind of initialising deep enough recursion |
| 82 | * will end up reading uninitialised PERL_CONTEXTs. */ |
| 83 | PoisonNew(cxstack + old_max + 1, cxstack_max - old_max, PERL_CONTEXT); |
| 84 | return cxstack_ix + 1; |
| 85 | } |
| 86 | |
| 87 | void |
| 88 | Perl_push_scope(pTHX) |
| 89 | { |
| 90 | dVAR; |
| 91 | if (PL_scopestack_ix == PL_scopestack_max) { |
| 92 | PL_scopestack_max = GROW(PL_scopestack_max); |
| 93 | Renew(PL_scopestack, PL_scopestack_max, I32); |
| 94 | #ifdef DEBUGGING |
| 95 | Renew(PL_scopestack_name, PL_scopestack_max, const char*); |
| 96 | #endif |
| 97 | } |
| 98 | #ifdef DEBUGGING |
| 99 | PL_scopestack_name[PL_scopestack_ix] = "unknown"; |
| 100 | #endif |
| 101 | PL_scopestack[PL_scopestack_ix++] = PL_savestack_ix; |
| 102 | |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | Perl_pop_scope(pTHX) |
| 107 | { |
| 108 | dVAR; |
| 109 | const I32 oldsave = PL_scopestack[--PL_scopestack_ix]; |
| 110 | LEAVE_SCOPE(oldsave); |
| 111 | } |
| 112 | |
| 113 | void |
| 114 | Perl_markstack_grow(pTHX) |
| 115 | { |
| 116 | dVAR; |
| 117 | const I32 oldmax = PL_markstack_max - PL_markstack; |
| 118 | const I32 newmax = GROW(oldmax); |
| 119 | |
| 120 | Renew(PL_markstack, newmax, I32); |
| 121 | PL_markstack_ptr = PL_markstack + oldmax; |
| 122 | PL_markstack_max = PL_markstack + newmax; |
| 123 | } |
| 124 | |
| 125 | void |
| 126 | Perl_savestack_grow(pTHX) |
| 127 | { |
| 128 | dVAR; |
| 129 | PL_savestack_max = GROW(PL_savestack_max) + 4; |
| 130 | Renew(PL_savestack, PL_savestack_max, ANY); |
| 131 | } |
| 132 | |
| 133 | void |
| 134 | Perl_savestack_grow_cnt(pTHX_ I32 need) |
| 135 | { |
| 136 | dVAR; |
| 137 | PL_savestack_max = PL_savestack_ix + need; |
| 138 | Renew(PL_savestack, PL_savestack_max, ANY); |
| 139 | } |
| 140 | |
| 141 | #undef GROW |
| 142 | |
| 143 | void |
| 144 | Perl_tmps_grow(pTHX_ I32 n) |
| 145 | { |
| 146 | dVAR; |
| 147 | #ifndef STRESS_REALLOC |
| 148 | if (n < 128) |
| 149 | n = (PL_tmps_max < 512) ? 128 : 512; |
| 150 | #endif |
| 151 | PL_tmps_max = PL_tmps_ix + n + 1; |
| 152 | Renew(PL_tmps_stack, PL_tmps_max, SV*); |
| 153 | } |
| 154 | |
| 155 | |
| 156 | void |
| 157 | Perl_free_tmps(pTHX) |
| 158 | { |
| 159 | dVAR; |
| 160 | /* XXX should tmps_floor live in cxstack? */ |
| 161 | const I32 myfloor = PL_tmps_floor; |
| 162 | while (PL_tmps_ix > myfloor) { /* clean up after last statement */ |
| 163 | SV* const sv = PL_tmps_stack[PL_tmps_ix]; |
| 164 | PL_tmps_stack[PL_tmps_ix--] = NULL; |
| 165 | if (sv && sv != &PL_sv_undef) { |
| 166 | SvTEMP_off(sv); |
| 167 | SvREFCNT_dec(sv); /* note, can modify tmps_ix!!! */ |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | STATIC SV * |
| 173 | S_save_scalar_at(pTHX_ SV **sptr, const U32 flags) |
| 174 | { |
| 175 | dVAR; |
| 176 | SV * osv; |
| 177 | register SV *sv; |
| 178 | |
| 179 | PERL_ARGS_ASSERT_SAVE_SCALAR_AT; |
| 180 | |
| 181 | osv = *sptr; |
| 182 | sv = (flags & SAVEf_KEEPOLDELEM) ? osv : (*sptr = newSV(0)); |
| 183 | |
| 184 | if (SvTYPE(osv) >= SVt_PVMG && SvMAGIC(osv) && SvTYPE(osv) != SVt_PVGV) { |
| 185 | if (SvGMAGICAL(osv)) { |
| 186 | SvFLAGS(osv) |= (SvFLAGS(osv) & |
| 187 | (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT; |
| 188 | } |
| 189 | if (!(flags & SAVEf_KEEPOLDELEM)) |
| 190 | mg_localize(osv, sv, (flags & SAVEf_SETMAGIC) != 0); |
| 191 | } |
| 192 | |
| 193 | return sv; |
| 194 | } |
| 195 | |
| 196 | void |
| 197 | Perl_save_pushptrptr(pTHX_ void *const ptr1, void *const ptr2, const int type) |
| 198 | { |
| 199 | dVAR; |
| 200 | SSCHECK(3); |
| 201 | SSPUSHPTR(ptr1); |
| 202 | SSPUSHPTR(ptr2); |
| 203 | SSPUSHUV(type); |
| 204 | } |
| 205 | |
| 206 | SV * |
| 207 | Perl_save_scalar(pTHX_ GV *gv) |
| 208 | { |
| 209 | dVAR; |
| 210 | SV ** const sptr = &GvSVn(gv); |
| 211 | |
| 212 | PERL_ARGS_ASSERT_SAVE_SCALAR; |
| 213 | |
| 214 | PL_localizing = 1; |
| 215 | SvGETMAGIC(*sptr); |
| 216 | PL_localizing = 0; |
| 217 | save_pushptrptr(SvREFCNT_inc_simple(gv), SvREFCNT_inc(*sptr), SAVEt_SV); |
| 218 | return save_scalar_at(sptr, SAVEf_SETMAGIC); /* XXX - FIXME - see #60360 */ |
| 219 | } |
| 220 | |
| 221 | /* Like save_sptr(), but also SvREFCNT_dec()s the new value. Can be used to |
| 222 | * restore a global SV to its prior contents, freeing new value. */ |
| 223 | void |
| 224 | Perl_save_generic_svref(pTHX_ SV **sptr) |
| 225 | { |
| 226 | dVAR; |
| 227 | |
| 228 | PERL_ARGS_ASSERT_SAVE_GENERIC_SVREF; |
| 229 | |
| 230 | save_pushptrptr(sptr, SvREFCNT_inc(*sptr), SAVEt_GENERIC_SVREF); |
| 231 | } |
| 232 | |
| 233 | /* Like save_pptr(), but also Safefree()s the new value if it is different |
| 234 | * from the old one. Can be used to restore a global char* to its prior |
| 235 | * contents, freeing new value. */ |
| 236 | void |
| 237 | Perl_save_generic_pvref(pTHX_ char **str) |
| 238 | { |
| 239 | dVAR; |
| 240 | |
| 241 | PERL_ARGS_ASSERT_SAVE_GENERIC_PVREF; |
| 242 | |
| 243 | save_pushptrptr(*str, str, SAVEt_GENERIC_PVREF); |
| 244 | } |
| 245 | |
| 246 | /* Like save_generic_pvref(), but uses PerlMemShared_free() rather than Safefree(). |
| 247 | * Can be used to restore a shared global char* to its prior |
| 248 | * contents, freeing new value. */ |
| 249 | void |
| 250 | Perl_save_shared_pvref(pTHX_ char **str) |
| 251 | { |
| 252 | dVAR; |
| 253 | |
| 254 | PERL_ARGS_ASSERT_SAVE_SHARED_PVREF; |
| 255 | |
| 256 | save_pushptrptr(str, *str, SAVEt_SHARED_PVREF); |
| 257 | } |
| 258 | |
| 259 | /* set the SvFLAGS specified by mask to the values in val */ |
| 260 | |
| 261 | void |
| 262 | Perl_save_set_svflags(pTHX_ SV* sv, U32 mask, U32 val) |
| 263 | { |
| 264 | dVAR; |
| 265 | |
| 266 | PERL_ARGS_ASSERT_SAVE_SET_SVFLAGS; |
| 267 | |
| 268 | SSCHECK(4); |
| 269 | SSPUSHPTR(sv); |
| 270 | SSPUSHINT(mask); |
| 271 | SSPUSHINT(val); |
| 272 | SSPUSHUV(SAVEt_SET_SVFLAGS); |
| 273 | } |
| 274 | |
| 275 | void |
| 276 | Perl_save_gp(pTHX_ GV *gv, I32 empty) |
| 277 | { |
| 278 | dVAR; |
| 279 | |
| 280 | PERL_ARGS_ASSERT_SAVE_GP; |
| 281 | |
| 282 | SSCHECK(4); |
| 283 | SSPUSHINT(SvFAKE(gv)); |
| 284 | SSPUSHPTR(GvGP(gv)); |
| 285 | SSPUSHPTR(SvREFCNT_inc(gv)); |
| 286 | SSPUSHUV(SAVEt_GP); |
| 287 | |
| 288 | /* Don't let the localized GV coerce into non-glob, otherwise we would |
| 289 | * not be able to restore GP upon leave from context if that happened */ |
| 290 | SvFAKE_off(gv); |
| 291 | |
| 292 | if (empty) { |
| 293 | GP *gp = Perl_newGP(aTHX_ gv); |
| 294 | |
| 295 | if (GvCVu(gv)) |
| 296 | mro_method_changed_in(GvSTASH(gv)); /* taking a method out of circulation ("local")*/ |
| 297 | if (GvIOp(gv) && (IoFLAGS(GvIOp(gv)) & IOf_ARGV)) { |
| 298 | gp->gp_io = newIO(); |
| 299 | IoFLAGS(gp->gp_io) |= IOf_ARGV|IOf_START; |
| 300 | } |
| 301 | #ifdef PERL_DONT_CREATE_GVSV |
| 302 | if (gv == PL_errgv) { |
| 303 | /* We could scatter this logic everywhere by changing the |
| 304 | definition of ERRSV from GvSV() to GvSVn(), but it seems more |
| 305 | efficient to do this check once here. */ |
| 306 | gp->gp_sv = newSV(0); |
| 307 | } |
| 308 | #endif |
| 309 | GvGP(gv) = gp; |
| 310 | } |
| 311 | else { |
| 312 | gp_ref(GvGP(gv)); |
| 313 | GvINTRO_on(gv); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | AV * |
| 318 | Perl_save_ary(pTHX_ GV *gv) |
| 319 | { |
| 320 | dVAR; |
| 321 | AV * const oav = GvAVn(gv); |
| 322 | AV *av; |
| 323 | |
| 324 | PERL_ARGS_ASSERT_SAVE_ARY; |
| 325 | |
| 326 | if (!AvREAL(oav) && AvREIFY(oav)) |
| 327 | av_reify(oav); |
| 328 | save_pushptrptr(gv, oav, SAVEt_AV); |
| 329 | |
| 330 | GvAV(gv) = NULL; |
| 331 | av = GvAVn(gv); |
| 332 | if (SvMAGIC(oav)) |
| 333 | mg_localize(MUTABLE_SV(oav), MUTABLE_SV(av), TRUE); |
| 334 | return av; |
| 335 | } |
| 336 | |
| 337 | HV * |
| 338 | Perl_save_hash(pTHX_ GV *gv) |
| 339 | { |
| 340 | dVAR; |
| 341 | HV *ohv, *hv; |
| 342 | |
| 343 | PERL_ARGS_ASSERT_SAVE_HASH; |
| 344 | |
| 345 | save_pushptrptr(gv, (ohv = GvHVn(gv)), SAVEt_HV); |
| 346 | |
| 347 | GvHV(gv) = NULL; |
| 348 | hv = GvHVn(gv); |
| 349 | if (SvMAGIC(ohv)) |
| 350 | mg_localize(MUTABLE_SV(ohv), MUTABLE_SV(hv), TRUE); |
| 351 | return hv; |
| 352 | } |
| 353 | |
| 354 | void |
| 355 | Perl_save_item(pTHX_ register SV *item) |
| 356 | { |
| 357 | dVAR; |
| 358 | register SV * const sv = newSVsv(item); |
| 359 | |
| 360 | PERL_ARGS_ASSERT_SAVE_ITEM; |
| 361 | |
| 362 | save_pushptrptr(item, /* remember the pointer */ |
| 363 | sv, /* remember the value */ |
| 364 | SAVEt_ITEM); |
| 365 | } |
| 366 | |
| 367 | void |
| 368 | Perl_save_bool(pTHX_ bool *boolp) |
| 369 | { |
| 370 | dVAR; |
| 371 | |
| 372 | PERL_ARGS_ASSERT_SAVE_BOOL; |
| 373 | |
| 374 | SSCHECK(2); |
| 375 | SSPUSHPTR(boolp); |
| 376 | SSPUSHUV(SAVEt_BOOL | (*boolp << 8)); |
| 377 | } |
| 378 | |
| 379 | void |
| 380 | Perl_save_pushi32ptr(pTHX_ const I32 i, void *const ptr, const int type) |
| 381 | { |
| 382 | dVAR; |
| 383 | SSCHECK(3); |
| 384 | SSPUSHINT(i); |
| 385 | SSPUSHPTR(ptr); |
| 386 | SSPUSHUV(type); |
| 387 | } |
| 388 | |
| 389 | void |
| 390 | Perl_save_int(pTHX_ int *intp) |
| 391 | { |
| 392 | dVAR; |
| 393 | const UV shifted = (UV)*intp << SAVE_TIGHT_SHIFT; |
| 394 | |
| 395 | PERL_ARGS_ASSERT_SAVE_INT; |
| 396 | |
| 397 | if ((int)(shifted >> SAVE_TIGHT_SHIFT) == *intp) { |
| 398 | SSCHECK(2); |
| 399 | SSPUSHPTR(intp); |
| 400 | SSPUSHUV(SAVEt_INT_SMALL | shifted); |
| 401 | } else |
| 402 | save_pushi32ptr(*intp, intp, SAVEt_INT); |
| 403 | } |
| 404 | |
| 405 | void |
| 406 | Perl_save_I8(pTHX_ I8 *bytep) |
| 407 | { |
| 408 | dVAR; |
| 409 | |
| 410 | PERL_ARGS_ASSERT_SAVE_I8; |
| 411 | |
| 412 | SSCHECK(2); |
| 413 | SSPUSHPTR(bytep); |
| 414 | SSPUSHUV(SAVEt_I8 | ((UV)*bytep << 8)); |
| 415 | } |
| 416 | |
| 417 | void |
| 418 | Perl_save_I16(pTHX_ I16 *intp) |
| 419 | { |
| 420 | dVAR; |
| 421 | |
| 422 | PERL_ARGS_ASSERT_SAVE_I16; |
| 423 | |
| 424 | SSCHECK(2); |
| 425 | SSPUSHPTR(intp); |
| 426 | SSPUSHUV(SAVEt_I16 | ((UV)*intp << 8)); |
| 427 | } |
| 428 | |
| 429 | void |
| 430 | Perl_save_I32(pTHX_ I32 *intp) |
| 431 | { |
| 432 | dVAR; |
| 433 | const UV shifted = (UV)*intp << SAVE_TIGHT_SHIFT; |
| 434 | |
| 435 | PERL_ARGS_ASSERT_SAVE_I32; |
| 436 | |
| 437 | if ((I32)(shifted >> SAVE_TIGHT_SHIFT) == *intp) { |
| 438 | SSCHECK(2); |
| 439 | SSPUSHPTR(intp); |
| 440 | SSPUSHUV(SAVEt_I32_SMALL | shifted); |
| 441 | } else |
| 442 | save_pushi32ptr(*intp, intp, SAVEt_I32); |
| 443 | } |
| 444 | |
| 445 | /* Cannot use save_sptr() to store a char* since the SV** cast will |
| 446 | * force word-alignment and we'll miss the pointer. |
| 447 | */ |
| 448 | void |
| 449 | Perl_save_pptr(pTHX_ char **pptr) |
| 450 | { |
| 451 | dVAR; |
| 452 | |
| 453 | PERL_ARGS_ASSERT_SAVE_PPTR; |
| 454 | |
| 455 | save_pushptrptr(*pptr, pptr, SAVEt_PPTR); |
| 456 | } |
| 457 | |
| 458 | void |
| 459 | Perl_save_vptr(pTHX_ void *ptr) |
| 460 | { |
| 461 | dVAR; |
| 462 | |
| 463 | PERL_ARGS_ASSERT_SAVE_VPTR; |
| 464 | |
| 465 | save_pushptrptr(*(char**)ptr, ptr, SAVEt_VPTR); |
| 466 | } |
| 467 | |
| 468 | void |
| 469 | Perl_save_sptr(pTHX_ SV **sptr) |
| 470 | { |
| 471 | dVAR; |
| 472 | |
| 473 | PERL_ARGS_ASSERT_SAVE_SPTR; |
| 474 | |
| 475 | save_pushptrptr(*sptr, sptr, SAVEt_SPTR); |
| 476 | } |
| 477 | |
| 478 | void |
| 479 | Perl_save_padsv_and_mortalize(pTHX_ PADOFFSET off) |
| 480 | { |
| 481 | dVAR; |
| 482 | SSCHECK(4); |
| 483 | ASSERT_CURPAD_ACTIVE("save_padsv"); |
| 484 | SSPUSHPTR(SvREFCNT_inc_simple_NN(PL_curpad[off])); |
| 485 | SSPUSHPTR(PL_comppad); |
| 486 | SSPUSHLONG((long)off); |
| 487 | SSPUSHUV(SAVEt_PADSV_AND_MORTALIZE); |
| 488 | } |
| 489 | |
| 490 | void |
| 491 | Perl_save_hptr(pTHX_ HV **hptr) |
| 492 | { |
| 493 | dVAR; |
| 494 | |
| 495 | PERL_ARGS_ASSERT_SAVE_HPTR; |
| 496 | |
| 497 | save_pushptrptr(*hptr, hptr, SAVEt_HPTR); |
| 498 | } |
| 499 | |
| 500 | void |
| 501 | Perl_save_aptr(pTHX_ AV **aptr) |
| 502 | { |
| 503 | dVAR; |
| 504 | |
| 505 | PERL_ARGS_ASSERT_SAVE_APTR; |
| 506 | |
| 507 | save_pushptrptr(*aptr, aptr, SAVEt_APTR); |
| 508 | } |
| 509 | |
| 510 | void |
| 511 | Perl_save_pushptr(pTHX_ void *const ptr, const int type) |
| 512 | { |
| 513 | dVAR; |
| 514 | SSCHECK(2); |
| 515 | SSPUSHPTR(ptr); |
| 516 | SSPUSHUV(type); |
| 517 | } |
| 518 | |
| 519 | void |
| 520 | Perl_save_clearsv(pTHX_ SV **svp) |
| 521 | { |
| 522 | dVAR; |
| 523 | const UV offset = svp - PL_curpad; |
| 524 | const UV offset_shifted = offset << SAVE_TIGHT_SHIFT; |
| 525 | |
| 526 | PERL_ARGS_ASSERT_SAVE_CLEARSV; |
| 527 | |
| 528 | ASSERT_CURPAD_ACTIVE("save_clearsv"); |
| 529 | if ((offset_shifted >> SAVE_TIGHT_SHIFT) != offset) |
| 530 | Perl_croak(aTHX_ "panic: pad offset %"UVuf" out of range (%p-%p)", |
| 531 | offset, svp, PL_curpad); |
| 532 | |
| 533 | SSCHECK(1); |
| 534 | SSPUSHUV(offset_shifted | SAVEt_CLEARSV); |
| 535 | SvPADSTALE_off(*svp); /* mark lexical as active */ |
| 536 | } |
| 537 | |
| 538 | void |
| 539 | Perl_save_delete(pTHX_ HV *hv, char *key, I32 klen) |
| 540 | { |
| 541 | dVAR; |
| 542 | |
| 543 | PERL_ARGS_ASSERT_SAVE_DELETE; |
| 544 | |
| 545 | save_pushptri32ptr(key, klen, SvREFCNT_inc_simple(hv), SAVEt_DELETE); |
| 546 | } |
| 547 | |
| 548 | void |
| 549 | Perl_save_hdelete(pTHX_ HV *hv, SV *keysv) |
| 550 | { |
| 551 | STRLEN len; |
| 552 | I32 klen; |
| 553 | const char *key; |
| 554 | |
| 555 | PERL_ARGS_ASSERT_SAVE_HDELETE; |
| 556 | |
| 557 | key = SvPV_const(keysv, len); |
| 558 | klen = SvUTF8(keysv) ? -(I32)len : (I32)len; |
| 559 | SvREFCNT_inc_simple_void_NN(hv); |
| 560 | save_pushptri32ptr(savepvn(key, len), klen, hv, SAVEt_DELETE); |
| 561 | } |
| 562 | |
| 563 | void |
| 564 | Perl_save_adelete(pTHX_ AV *av, I32 key) |
| 565 | { |
| 566 | dVAR; |
| 567 | |
| 568 | PERL_ARGS_ASSERT_SAVE_ADELETE; |
| 569 | |
| 570 | SvREFCNT_inc_void(av); |
| 571 | save_pushi32ptr(key, av, SAVEt_ADELETE); |
| 572 | } |
| 573 | |
| 574 | void |
| 575 | Perl_save_destructor(pTHX_ DESTRUCTORFUNC_NOCONTEXT_t f, void* p) |
| 576 | { |
| 577 | dVAR; |
| 578 | |
| 579 | PERL_ARGS_ASSERT_SAVE_DESTRUCTOR; |
| 580 | |
| 581 | SSCHECK(3); |
| 582 | SSPUSHDPTR(f); |
| 583 | SSPUSHPTR(p); |
| 584 | SSPUSHUV(SAVEt_DESTRUCTOR); |
| 585 | } |
| 586 | |
| 587 | void |
| 588 | Perl_save_destructor_x(pTHX_ DESTRUCTORFUNC_t f, void* p) |
| 589 | { |
| 590 | dVAR; |
| 591 | SSCHECK(3); |
| 592 | SSPUSHDXPTR(f); |
| 593 | SSPUSHPTR(p); |
| 594 | SSPUSHUV(SAVEt_DESTRUCTOR_X); |
| 595 | } |
| 596 | |
| 597 | void |
| 598 | Perl_save_hints(pTHX) |
| 599 | { |
| 600 | dVAR; |
| 601 | COPHH *save_cophh = cophh_copy(CopHINTHASH_get(&PL_compiling)); |
| 602 | if (PL_hints & HINT_LOCALIZE_HH) { |
| 603 | save_pushptri32ptr(GvHV(PL_hintgv), PL_hints, save_cophh, SAVEt_HINTS); |
| 604 | GvHV(PL_hintgv) = hv_copy_hints_hv(GvHV(PL_hintgv)); |
| 605 | } else { |
| 606 | save_pushi32ptr(PL_hints, save_cophh, SAVEt_HINTS); |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | static void |
| 611 | S_save_pushptri32ptr(pTHX_ void *const ptr1, const I32 i, void *const ptr2, |
| 612 | const int type) |
| 613 | { |
| 614 | SSCHECK(4); |
| 615 | SSPUSHPTR(ptr1); |
| 616 | SSPUSHINT(i); |
| 617 | SSPUSHPTR(ptr2); |
| 618 | SSPUSHUV(type); |
| 619 | } |
| 620 | |
| 621 | void |
| 622 | Perl_save_aelem_flags(pTHX_ AV *av, I32 idx, SV **sptr, const U32 flags) |
| 623 | { |
| 624 | dVAR; |
| 625 | SV *sv; |
| 626 | |
| 627 | PERL_ARGS_ASSERT_SAVE_AELEM_FLAGS; |
| 628 | |
| 629 | SvGETMAGIC(*sptr); |
| 630 | save_pushptri32ptr(SvREFCNT_inc_simple(av), idx, SvREFCNT_inc(*sptr), |
| 631 | SAVEt_AELEM); |
| 632 | /* if it gets reified later, the restore will have the wrong refcnt */ |
| 633 | if (!AvREAL(av) && AvREIFY(av)) |
| 634 | SvREFCNT_inc_void(*sptr); |
| 635 | save_scalar_at(sptr, flags); /* XXX - FIXME - see #60360 */ |
| 636 | if (flags & SAVEf_KEEPOLDELEM) |
| 637 | return; |
| 638 | sv = *sptr; |
| 639 | /* If we're localizing a tied array element, this new sv |
| 640 | * won't actually be stored in the array - so it won't get |
| 641 | * reaped when the localize ends. Ensure it gets reaped by |
| 642 | * mortifying it instead. DAPM */ |
| 643 | if (SvTIED_mg((const SV *)av, PERL_MAGIC_tied)) |
| 644 | sv_2mortal(sv); |
| 645 | } |
| 646 | |
| 647 | void |
| 648 | Perl_save_helem_flags(pTHX_ HV *hv, SV *key, SV **sptr, const U32 flags) |
| 649 | { |
| 650 | dVAR; |
| 651 | SV *sv; |
| 652 | |
| 653 | PERL_ARGS_ASSERT_SAVE_HELEM_FLAGS; |
| 654 | |
| 655 | SvGETMAGIC(*sptr); |
| 656 | SSCHECK(4); |
| 657 | SSPUSHPTR(SvREFCNT_inc_simple(hv)); |
| 658 | SSPUSHPTR(newSVsv(key)); |
| 659 | SSPUSHPTR(SvREFCNT_inc(*sptr)); |
| 660 | SSPUSHUV(SAVEt_HELEM); |
| 661 | save_scalar_at(sptr, flags); |
| 662 | if (flags & SAVEf_KEEPOLDELEM) |
| 663 | return; |
| 664 | sv = *sptr; |
| 665 | /* If we're localizing a tied hash element, this new sv |
| 666 | * won't actually be stored in the hash - so it won't get |
| 667 | * reaped when the localize ends. Ensure it gets reaped by |
| 668 | * mortifying it instead. DAPM */ |
| 669 | if (SvTIED_mg((const SV *)hv, PERL_MAGIC_tied)) |
| 670 | sv_2mortal(sv); |
| 671 | } |
| 672 | |
| 673 | SV* |
| 674 | Perl_save_svref(pTHX_ SV **sptr) |
| 675 | { |
| 676 | dVAR; |
| 677 | |
| 678 | PERL_ARGS_ASSERT_SAVE_SVREF; |
| 679 | |
| 680 | SvGETMAGIC(*sptr); |
| 681 | save_pushptrptr(sptr, SvREFCNT_inc(*sptr), SAVEt_SVREF); |
| 682 | return save_scalar_at(sptr, SAVEf_SETMAGIC); /* XXX - FIXME - see #60360 */ |
| 683 | } |
| 684 | |
| 685 | I32 |
| 686 | Perl_save_alloc(pTHX_ I32 size, I32 pad) |
| 687 | { |
| 688 | dVAR; |
| 689 | register const I32 start = pad + ((char*)&PL_savestack[PL_savestack_ix] |
| 690 | - (char*)PL_savestack); |
| 691 | const UV elems = 1 + ((size + pad - 1) / sizeof(*PL_savestack)); |
| 692 | const UV elems_shifted = elems << SAVE_TIGHT_SHIFT; |
| 693 | |
| 694 | if ((elems_shifted >> SAVE_TIGHT_SHIFT) != elems) |
| 695 | Perl_croak(aTHX_ "panic: save_alloc elems %"UVuf" out of range (%ld-%ld)", |
| 696 | elems, size, pad); |
| 697 | |
| 698 | SSGROW(elems + 1); |
| 699 | |
| 700 | PL_savestack_ix += elems; |
| 701 | SSPUSHUV(SAVEt_ALLOC | elems_shifted); |
| 702 | return start; |
| 703 | } |
| 704 | |
| 705 | void |
| 706 | Perl_leave_scope(pTHX_ I32 base) |
| 707 | { |
| 708 | dVAR; |
| 709 | register SV *sv; |
| 710 | register SV *value; |
| 711 | register GV *gv; |
| 712 | register AV *av; |
| 713 | register HV *hv; |
| 714 | void* ptr; |
| 715 | register char* str; |
| 716 | I32 i; |
| 717 | /* Localise the effects of the TAINT_NOT inside the loop. */ |
| 718 | const bool was = PL_tainted; |
| 719 | |
| 720 | if (base < -1) |
| 721 | Perl_croak(aTHX_ "panic: corrupt saved stack index"); |
| 722 | DEBUG_l(Perl_deb(aTHX_ "savestack: releasing items %ld -> %ld\n", |
| 723 | (long)PL_savestack_ix, (long)base)); |
| 724 | while (PL_savestack_ix > base) { |
| 725 | UV uv = SSPOPUV; |
| 726 | const U8 type = (U8)uv & SAVE_MASK; |
| 727 | TAINT_NOT; |
| 728 | |
| 729 | switch (type) { |
| 730 | case SAVEt_ITEM: /* normal string */ |
| 731 | value = MUTABLE_SV(SSPOPPTR); |
| 732 | sv = MUTABLE_SV(SSPOPPTR); |
| 733 | sv_replace(sv,value); |
| 734 | PL_localizing = 2; |
| 735 | SvSETMAGIC(sv); |
| 736 | PL_localizing = 0; |
| 737 | break; |
| 738 | case SAVEt_SV: /* scalar reference */ |
| 739 | value = MUTABLE_SV(SSPOPPTR); |
| 740 | gv = MUTABLE_GV(SSPOPPTR); |
| 741 | ptr = &GvSV(gv); |
| 742 | av = MUTABLE_AV(gv); /* what to refcnt_dec */ |
| 743 | restore_sv: |
| 744 | sv = *(SV**)ptr; |
| 745 | *(SV**)ptr = value; |
| 746 | SvREFCNT_dec(sv); |
| 747 | PL_localizing = 2; |
| 748 | SvSETMAGIC(value); |
| 749 | PL_localizing = 0; |
| 750 | SvREFCNT_dec(value); |
| 751 | if (av) /* actually an av, hv or gv */ |
| 752 | SvREFCNT_dec(av); |
| 753 | break; |
| 754 | case SAVEt_GENERIC_PVREF: /* generic pv */ |
| 755 | ptr = SSPOPPTR; |
| 756 | str = (char*)SSPOPPTR; |
| 757 | if (*(char**)ptr != str) { |
| 758 | Safefree(*(char**)ptr); |
| 759 | *(char**)ptr = str; |
| 760 | } |
| 761 | break; |
| 762 | case SAVEt_SHARED_PVREF: /* shared pv */ |
| 763 | str = (char*)SSPOPPTR; |
| 764 | ptr = SSPOPPTR; |
| 765 | if (*(char**)ptr != str) { |
| 766 | #ifdef NETWARE |
| 767 | PerlMem_free(*(char**)ptr); |
| 768 | #else |
| 769 | PerlMemShared_free(*(char**)ptr); |
| 770 | #endif |
| 771 | *(char**)ptr = str; |
| 772 | } |
| 773 | break; |
| 774 | case SAVEt_GVSV: /* scalar slot in GV */ |
| 775 | value = MUTABLE_SV(SSPOPPTR); |
| 776 | gv = MUTABLE_GV(SSPOPPTR); |
| 777 | ptr = &GvSV(gv); |
| 778 | goto restore_svp; |
| 779 | case SAVEt_GENERIC_SVREF: /* generic sv */ |
| 780 | value = MUTABLE_SV(SSPOPPTR); |
| 781 | ptr = SSPOPPTR; |
| 782 | restore_svp: |
| 783 | sv = *(SV**)ptr; |
| 784 | *(SV**)ptr = value; |
| 785 | SvREFCNT_dec(sv); |
| 786 | SvREFCNT_dec(value); |
| 787 | break; |
| 788 | case SAVEt_AV: /* array reference */ |
| 789 | av = MUTABLE_AV(SSPOPPTR); |
| 790 | gv = MUTABLE_GV(SSPOPPTR); |
| 791 | SvREFCNT_dec(GvAV(gv)); |
| 792 | GvAV(gv) = av; |
| 793 | if (SvMAGICAL(av)) { |
| 794 | PL_localizing = 2; |
| 795 | SvSETMAGIC(MUTABLE_SV(av)); |
| 796 | PL_localizing = 0; |
| 797 | } |
| 798 | break; |
| 799 | case SAVEt_HV: /* hash reference */ |
| 800 | hv = MUTABLE_HV(SSPOPPTR); |
| 801 | gv = MUTABLE_GV(SSPOPPTR); |
| 802 | SvREFCNT_dec(GvHV(gv)); |
| 803 | GvHV(gv) = hv; |
| 804 | if (SvMAGICAL(hv)) { |
| 805 | PL_localizing = 2; |
| 806 | SvSETMAGIC(MUTABLE_SV(hv)); |
| 807 | PL_localizing = 0; |
| 808 | } |
| 809 | break; |
| 810 | case SAVEt_INT_SMALL: |
| 811 | ptr = SSPOPPTR; |
| 812 | *(int*)ptr = (int)(uv >> SAVE_TIGHT_SHIFT); |
| 813 | break; |
| 814 | case SAVEt_INT: /* int reference */ |
| 815 | ptr = SSPOPPTR; |
| 816 | *(int*)ptr = (int)SSPOPINT; |
| 817 | break; |
| 818 | case SAVEt_BOOL: /* bool reference */ |
| 819 | ptr = SSPOPPTR; |
| 820 | *(bool*)ptr = cBOOL(uv >> 8); |
| 821 | break; |
| 822 | case SAVEt_I32_SMALL: |
| 823 | ptr = SSPOPPTR; |
| 824 | *(I32*)ptr = (I32)(uv >> SAVE_TIGHT_SHIFT); |
| 825 | break; |
| 826 | case SAVEt_I32: /* I32 reference */ |
| 827 | ptr = SSPOPPTR; |
| 828 | #ifdef PERL_DEBUG_READONLY_OPS |
| 829 | { |
| 830 | const I32 val = SSPOPINT; |
| 831 | if (*(I32*)ptr != val) |
| 832 | *(I32*)ptr = val; |
| 833 | } |
| 834 | #else |
| 835 | *(I32*)ptr = (I32)SSPOPINT; |
| 836 | #endif |
| 837 | break; |
| 838 | case SAVEt_SPTR: /* SV* reference */ |
| 839 | ptr = SSPOPPTR; |
| 840 | *(SV**)ptr = MUTABLE_SV(SSPOPPTR); |
| 841 | break; |
| 842 | case SAVEt_VPTR: /* random* reference */ |
| 843 | case SAVEt_PPTR: /* char* reference */ |
| 844 | ptr = SSPOPPTR; |
| 845 | *(char**)ptr = (char*)SSPOPPTR; |
| 846 | break; |
| 847 | case SAVEt_HPTR: /* HV* reference */ |
| 848 | ptr = SSPOPPTR; |
| 849 | *(HV**)ptr = MUTABLE_HV(SSPOPPTR); |
| 850 | break; |
| 851 | case SAVEt_APTR: /* AV* reference */ |
| 852 | ptr = SSPOPPTR; |
| 853 | *(AV**)ptr = MUTABLE_AV(SSPOPPTR); |
| 854 | break; |
| 855 | case SAVEt_GP: /* scalar reference */ |
| 856 | gv = MUTABLE_GV(SSPOPPTR); |
| 857 | gp_free(gv); |
| 858 | GvGP(gv) = (GP*)SSPOPPTR; |
| 859 | if (SSPOPINT) |
| 860 | SvFAKE_on(gv); |
| 861 | /* putting a method back into circulation ("local")*/ |
| 862 | if (GvCVu(gv) && (hv=GvSTASH(gv)) && HvENAME_get(hv)) |
| 863 | mro_method_changed_in(hv); |
| 864 | SvREFCNT_dec(gv); |
| 865 | break; |
| 866 | case SAVEt_FREESV: |
| 867 | ptr = SSPOPPTR; |
| 868 | SvREFCNT_dec(MUTABLE_SV(ptr)); |
| 869 | break; |
| 870 | case SAVEt_FREECOPHH: |
| 871 | ptr = SSPOPPTR; |
| 872 | cophh_free((COPHH *)ptr); |
| 873 | break; |
| 874 | case SAVEt_MORTALIZESV: |
| 875 | ptr = SSPOPPTR; |
| 876 | sv_2mortal(MUTABLE_SV(ptr)); |
| 877 | break; |
| 878 | case SAVEt_FREEOP: |
| 879 | ptr = SSPOPPTR; |
| 880 | ASSERT_CURPAD_LEGAL("SAVEt_FREEOP"); /* XXX DAPM tmp */ |
| 881 | op_free((OP*)ptr); |
| 882 | break; |
| 883 | case SAVEt_FREEPV: |
| 884 | ptr = SSPOPPTR; |
| 885 | Safefree(ptr); |
| 886 | break; |
| 887 | case SAVEt_CLEARSV: |
| 888 | ptr = (void*)&PL_curpad[uv >> SAVE_TIGHT_SHIFT]; |
| 889 | sv = *(SV**)ptr; |
| 890 | |
| 891 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, |
| 892 | "Pad 0x%"UVxf"[0x%"UVxf"] clearsv: %ld sv=0x%"UVxf"<%"IVdf"> %s\n", |
| 893 | PTR2UV(PL_comppad), PTR2UV(PL_curpad), |
| 894 | (long)((SV **)ptr-PL_curpad), PTR2UV(sv), (IV)SvREFCNT(sv), |
| 895 | (SvREFCNT(sv) <= 1 && !SvOBJECT(sv)) ? "clear" : "abandon" |
| 896 | )); |
| 897 | |
| 898 | /* Can clear pad variable in place? */ |
| 899 | if (SvREFCNT(sv) <= 1 && !SvOBJECT(sv)) { |
| 900 | /* |
| 901 | * if a my variable that was made readonly is going out of |
| 902 | * scope, we want to remove the readonlyness so that it can |
| 903 | * go out of scope quietly |
| 904 | */ |
| 905 | if (SvPADMY(sv) && !SvFAKE(sv)) |
| 906 | SvREADONLY_off(sv); |
| 907 | |
| 908 | if (SvTHINKFIRST(sv)) |
| 909 | sv_force_normal_flags(sv, SV_IMMEDIATE_UNREF); |
| 910 | if (SvMAGICAL(sv)) |
| 911 | mg_free(sv); |
| 912 | |
| 913 | switch (SvTYPE(sv)) { |
| 914 | case SVt_NULL: |
| 915 | break; |
| 916 | case SVt_PVAV: |
| 917 | av_clear(MUTABLE_AV(sv)); |
| 918 | break; |
| 919 | case SVt_PVHV: |
| 920 | hv_clear(MUTABLE_HV(sv)); |
| 921 | break; |
| 922 | case SVt_PVCV: |
| 923 | Perl_croak(aTHX_ "panic: leave_scope pad code"); |
| 924 | default: |
| 925 | SvOK_off(sv); |
| 926 | break; |
| 927 | } |
| 928 | SvPADSTALE_on(sv); /* mark as no longer live */ |
| 929 | } |
| 930 | else { /* Someone has a claim on this, so abandon it. */ |
| 931 | const U32 padflags = SvFLAGS(sv) & (SVs_PADMY|SVs_PADTMP); |
| 932 | switch (SvTYPE(sv)) { /* Console ourselves with a new value */ |
| 933 | case SVt_PVAV: *(SV**)ptr = MUTABLE_SV(newAV()); break; |
| 934 | case SVt_PVHV: *(SV**)ptr = MUTABLE_SV(newHV()); break; |
| 935 | default: *(SV**)ptr = newSV(0); break; |
| 936 | } |
| 937 | SvREFCNT_dec(sv); /* Cast current value to the winds. */ |
| 938 | /* preserve pad nature, but also mark as not live |
| 939 | * for any closure capturing */ |
| 940 | SvFLAGS(*(SV**)ptr) |= padflags | SVs_PADSTALE; |
| 941 | } |
| 942 | break; |
| 943 | case SAVEt_DELETE: |
| 944 | ptr = SSPOPPTR; |
| 945 | hv = MUTABLE_HV(ptr); |
| 946 | i = SSPOPINT; |
| 947 | ptr = SSPOPPTR; |
| 948 | (void)hv_delete(hv, (char*)ptr, i, G_DISCARD); |
| 949 | SvREFCNT_dec(hv); |
| 950 | Safefree(ptr); |
| 951 | break; |
| 952 | case SAVEt_ADELETE: |
| 953 | ptr = SSPOPPTR; |
| 954 | av = MUTABLE_AV(ptr); |
| 955 | i = SSPOPINT; |
| 956 | (void)av_delete(av, i, G_DISCARD); |
| 957 | SvREFCNT_dec(av); |
| 958 | break; |
| 959 | case SAVEt_DESTRUCTOR_X: |
| 960 | ptr = SSPOPPTR; |
| 961 | (*SSPOPDXPTR)(aTHX_ ptr); |
| 962 | break; |
| 963 | case SAVEt_REGCONTEXT: |
| 964 | /* regexp must have croaked */ |
| 965 | case SAVEt_ALLOC: |
| 966 | PL_savestack_ix -= uv >> SAVE_TIGHT_SHIFT; |
| 967 | break; |
| 968 | case SAVEt_STACK_POS: /* Position on Perl stack */ |
| 969 | i = SSPOPINT; |
| 970 | PL_stack_sp = PL_stack_base + i; |
| 971 | break; |
| 972 | case SAVEt_STACK_CXPOS: /* blk_oldsp on context stack */ |
| 973 | i = SSPOPINT; |
| 974 | cxstack[i].blk_oldsp = SSPOPINT; |
| 975 | break; |
| 976 | case SAVEt_AELEM: /* array element */ |
| 977 | value = MUTABLE_SV(SSPOPPTR); |
| 978 | i = SSPOPINT; |
| 979 | av = MUTABLE_AV(SSPOPPTR); |
| 980 | ptr = av_fetch(av,i,1); |
| 981 | if (!AvREAL(av) && AvREIFY(av)) /* undo reify guard */ |
| 982 | SvREFCNT_dec(value); |
| 983 | if (ptr) { |
| 984 | sv = *(SV**)ptr; |
| 985 | if (sv && sv != &PL_sv_undef) { |
| 986 | if (SvTIED_mg((const SV *)av, PERL_MAGIC_tied)) |
| 987 | SvREFCNT_inc_void_NN(sv); |
| 988 | goto restore_sv; |
| 989 | } |
| 990 | } |
| 991 | SvREFCNT_dec(av); |
| 992 | SvREFCNT_dec(value); |
| 993 | break; |
| 994 | case SAVEt_HELEM: /* hash element */ |
| 995 | value = MUTABLE_SV(SSPOPPTR); |
| 996 | sv = MUTABLE_SV(SSPOPPTR); |
| 997 | hv = MUTABLE_HV(SSPOPPTR); |
| 998 | ptr = hv_fetch_ent(hv, sv, 1, 0); |
| 999 | SvREFCNT_dec(sv); |
| 1000 | if (ptr) { |
| 1001 | const SV * const oval = HeVAL((HE*)ptr); |
| 1002 | if (oval && oval != &PL_sv_undef) { |
| 1003 | ptr = &HeVAL((HE*)ptr); |
| 1004 | if (SvTIED_mg((const SV *)hv, PERL_MAGIC_tied)) |
| 1005 | SvREFCNT_inc_void(*(SV**)ptr); |
| 1006 | av = MUTABLE_AV(hv); /* what to refcnt_dec */ |
| 1007 | goto restore_sv; |
| 1008 | } |
| 1009 | } |
| 1010 | SvREFCNT_dec(hv); |
| 1011 | SvREFCNT_dec(value); |
| 1012 | break; |
| 1013 | case SAVEt_OP: |
| 1014 | PL_op = (OP*)SSPOPPTR; |
| 1015 | break; |
| 1016 | case SAVEt_HINTS: |
| 1017 | if ((PL_hints & HINT_LOCALIZE_HH) && GvHV(PL_hintgv)) { |
| 1018 | SvREFCNT_dec(MUTABLE_SV(GvHV(PL_hintgv))); |
| 1019 | GvHV(PL_hintgv) = NULL; |
| 1020 | } |
| 1021 | cophh_free(CopHINTHASH_get(&PL_compiling)); |
| 1022 | CopHINTHASH_set(&PL_compiling, (COPHH*)SSPOPPTR); |
| 1023 | *(I32*)&PL_hints = (I32)SSPOPINT; |
| 1024 | if (PL_hints & HINT_LOCALIZE_HH) { |
| 1025 | SvREFCNT_dec(MUTABLE_SV(GvHV(PL_hintgv))); |
| 1026 | GvHV(PL_hintgv) = MUTABLE_HV(SSPOPPTR); |
| 1027 | assert(GvHV(PL_hintgv)); |
| 1028 | } else if (!GvHV(PL_hintgv)) { |
| 1029 | /* Need to add a new one manually, else gv_fetchpv() can |
| 1030 | add one in this code: |
| 1031 | |
| 1032 | if (SvTYPE(gv) == SVt_PVGV) { |
| 1033 | if (add) { |
| 1034 | GvMULTI_on(gv); |
| 1035 | gv_init_sv(gv, sv_type); |
| 1036 | if (*name=='!' && sv_type == SVt_PVHV && len==1) |
| 1037 | require_errno(gv); |
| 1038 | } |
| 1039 | return gv; |
| 1040 | } |
| 1041 | |
| 1042 | and it won't have the magic set. */ |
| 1043 | |
| 1044 | HV *const hv = newHV(); |
| 1045 | hv_magic(hv, NULL, PERL_MAGIC_hints); |
| 1046 | GvHV(PL_hintgv) = hv; |
| 1047 | } |
| 1048 | assert(GvHV(PL_hintgv)); |
| 1049 | break; |
| 1050 | case SAVEt_COMPPAD: |
| 1051 | PL_comppad = (PAD*)SSPOPPTR; |
| 1052 | if (PL_comppad) |
| 1053 | PL_curpad = AvARRAY(PL_comppad); |
| 1054 | else |
| 1055 | PL_curpad = NULL; |
| 1056 | break; |
| 1057 | case SAVEt_PADSV_AND_MORTALIZE: |
| 1058 | { |
| 1059 | const PADOFFSET off = (PADOFFSET)SSPOPLONG; |
| 1060 | SV **svp; |
| 1061 | ptr = SSPOPPTR; |
| 1062 | assert (ptr); |
| 1063 | svp = AvARRAY((PAD*)ptr) + off; |
| 1064 | /* This mortalizing used to be done by POPLOOP() via itersave. |
| 1065 | But as we have all the information here, we can do it here, |
| 1066 | save even having to have itersave in the struct. */ |
| 1067 | sv_2mortal(*svp); |
| 1068 | *svp = MUTABLE_SV(SSPOPPTR); |
| 1069 | } |
| 1070 | break; |
| 1071 | case SAVEt_SAVESWITCHSTACK: |
| 1072 | { |
| 1073 | dSP; |
| 1074 | AV *const t = MUTABLE_AV(SSPOPPTR); |
| 1075 | AV *const f = MUTABLE_AV(SSPOPPTR); |
| 1076 | SWITCHSTACK(t,f); |
| 1077 | PL_curstackinfo->si_stack = f; |
| 1078 | } |
| 1079 | break; |
| 1080 | case SAVEt_SET_SVFLAGS: |
| 1081 | { |
| 1082 | const U32 val = (U32)SSPOPINT; |
| 1083 | const U32 mask = (U32)SSPOPINT; |
| 1084 | sv = MUTABLE_SV(SSPOPPTR); |
| 1085 | SvFLAGS(sv) &= ~mask; |
| 1086 | SvFLAGS(sv) |= val; |
| 1087 | } |
| 1088 | break; |
| 1089 | |
| 1090 | /* This would be a mathom, but Perl_save_svref() calls a static |
| 1091 | function, S_save_scalar_at(), so has to stay in this file. */ |
| 1092 | case SAVEt_SVREF: /* scalar reference */ |
| 1093 | value = MUTABLE_SV(SSPOPPTR); |
| 1094 | ptr = SSPOPPTR; |
| 1095 | av = NULL; /* what to refcnt_dec */ |
| 1096 | goto restore_sv; |
| 1097 | |
| 1098 | /* These are only saved in mathoms.c */ |
| 1099 | case SAVEt_NSTAB: |
| 1100 | gv = MUTABLE_GV(SSPOPPTR); |
| 1101 | (void)sv_clear(MUTABLE_SV(gv)); |
| 1102 | break; |
| 1103 | case SAVEt_LONG: /* long reference */ |
| 1104 | ptr = SSPOPPTR; |
| 1105 | *(long*)ptr = (long)SSPOPLONG; |
| 1106 | break; |
| 1107 | case SAVEt_IV: /* IV reference */ |
| 1108 | ptr = SSPOPPTR; |
| 1109 | *(IV*)ptr = (IV)SSPOPIV; |
| 1110 | break; |
| 1111 | |
| 1112 | case SAVEt_I16: /* I16 reference */ |
| 1113 | ptr = SSPOPPTR; |
| 1114 | *(I16*)ptr = (I16)(uv >> 8); |
| 1115 | break; |
| 1116 | case SAVEt_I8: /* I8 reference */ |
| 1117 | ptr = SSPOPPTR; |
| 1118 | *(I8*)ptr = (I8)(uv >> 8); |
| 1119 | break; |
| 1120 | case SAVEt_DESTRUCTOR: |
| 1121 | ptr = SSPOPPTR; |
| 1122 | (*SSPOPDPTR)(ptr); |
| 1123 | break; |
| 1124 | case SAVEt_COP_ARYBASE: |
| 1125 | ptr = SSPOPPTR; |
| 1126 | i = SSPOPINT; |
| 1127 | CopARYBASE_set((COP *)ptr, i); |
| 1128 | break; |
| 1129 | case SAVEt_COMPILE_WARNINGS: |
| 1130 | ptr = SSPOPPTR; |
| 1131 | |
| 1132 | if (!specialWARN(PL_compiling.cop_warnings)) |
| 1133 | PerlMemShared_free(PL_compiling.cop_warnings); |
| 1134 | |
| 1135 | PL_compiling.cop_warnings = (STRLEN*)ptr; |
| 1136 | break; |
| 1137 | case SAVEt_RE_STATE: |
| 1138 | { |
| 1139 | const struct re_save_state *const state |
| 1140 | = (struct re_save_state *) |
| 1141 | (PL_savestack + PL_savestack_ix |
| 1142 | - SAVESTACK_ALLOC_FOR_RE_SAVE_STATE); |
| 1143 | PL_savestack_ix -= SAVESTACK_ALLOC_FOR_RE_SAVE_STATE; |
| 1144 | |
| 1145 | if (PL_reg_start_tmp != state->re_state_reg_start_tmp) { |
| 1146 | Safefree(PL_reg_start_tmp); |
| 1147 | } |
| 1148 | if (PL_reg_poscache != state->re_state_reg_poscache) { |
| 1149 | Safefree(PL_reg_poscache); |
| 1150 | } |
| 1151 | Copy(state, &PL_reg_state, 1, struct re_save_state); |
| 1152 | } |
| 1153 | break; |
| 1154 | case SAVEt_PARSER: |
| 1155 | ptr = SSPOPPTR; |
| 1156 | parser_free((yy_parser *) ptr); |
| 1157 | break; |
| 1158 | default: |
| 1159 | Perl_croak(aTHX_ "panic: leave_scope inconsistency"); |
| 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | PL_tainted = was; |
| 1164 | |
| 1165 | PERL_ASYNC_CHECK(); |
| 1166 | } |
| 1167 | |
| 1168 | void |
| 1169 | Perl_cx_dump(pTHX_ PERL_CONTEXT *cx) |
| 1170 | { |
| 1171 | dVAR; |
| 1172 | |
| 1173 | PERL_ARGS_ASSERT_CX_DUMP; |
| 1174 | |
| 1175 | #ifdef DEBUGGING |
| 1176 | PerlIO_printf(Perl_debug_log, "CX %ld = %s\n", (long)(cx - cxstack), PL_block_type[CxTYPE(cx)]); |
| 1177 | if (CxTYPE(cx) != CXt_SUBST) { |
| 1178 | PerlIO_printf(Perl_debug_log, "BLK_OLDSP = %ld\n", (long)cx->blk_oldsp); |
| 1179 | PerlIO_printf(Perl_debug_log, "BLK_OLDCOP = 0x%"UVxf"\n", |
| 1180 | PTR2UV(cx->blk_oldcop)); |
| 1181 | PerlIO_printf(Perl_debug_log, "BLK_OLDMARKSP = %ld\n", (long)cx->blk_oldmarksp); |
| 1182 | PerlIO_printf(Perl_debug_log, "BLK_OLDSCOPESP = %ld\n", (long)cx->blk_oldscopesp); |
| 1183 | PerlIO_printf(Perl_debug_log, "BLK_OLDPM = 0x%"UVxf"\n", |
| 1184 | PTR2UV(cx->blk_oldpm)); |
| 1185 | PerlIO_printf(Perl_debug_log, "BLK_GIMME = %s\n", cx->blk_gimme ? "LIST" : "SCALAR"); |
| 1186 | } |
| 1187 | switch (CxTYPE(cx)) { |
| 1188 | case CXt_NULL: |
| 1189 | case CXt_BLOCK: |
| 1190 | break; |
| 1191 | case CXt_FORMAT: |
| 1192 | PerlIO_printf(Perl_debug_log, "BLK_FORMAT.CV = 0x%"UVxf"\n", |
| 1193 | PTR2UV(cx->blk_format.cv)); |
| 1194 | PerlIO_printf(Perl_debug_log, "BLK_FORMAT.GV = 0x%"UVxf"\n", |
| 1195 | PTR2UV(cx->blk_format.gv)); |
| 1196 | PerlIO_printf(Perl_debug_log, "BLK_FORMAT.DFOUTGV = 0x%"UVxf"\n", |
| 1197 | PTR2UV(cx->blk_format.dfoutgv)); |
| 1198 | PerlIO_printf(Perl_debug_log, "BLK_FORMAT.HASARGS = %d\n", |
| 1199 | (int)CxHASARGS(cx)); |
| 1200 | PerlIO_printf(Perl_debug_log, "BLK_FORMAT.RETOP = 0x%"UVxf"\n", |
| 1201 | PTR2UV(cx->blk_format.retop)); |
| 1202 | break; |
| 1203 | case CXt_SUB: |
| 1204 | PerlIO_printf(Perl_debug_log, "BLK_SUB.CV = 0x%"UVxf"\n", |
| 1205 | PTR2UV(cx->blk_sub.cv)); |
| 1206 | PerlIO_printf(Perl_debug_log, "BLK_SUB.OLDDEPTH = %ld\n", |
| 1207 | (long)cx->blk_sub.olddepth); |
| 1208 | PerlIO_printf(Perl_debug_log, "BLK_SUB.HASARGS = %d\n", |
| 1209 | (int)CxHASARGS(cx)); |
| 1210 | PerlIO_printf(Perl_debug_log, "BLK_SUB.LVAL = %d\n", (int)CxLVAL(cx)); |
| 1211 | PerlIO_printf(Perl_debug_log, "BLK_SUB.RETOP = 0x%"UVxf"\n", |
| 1212 | PTR2UV(cx->blk_sub.retop)); |
| 1213 | break; |
| 1214 | case CXt_EVAL: |
| 1215 | PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_IN_EVAL = %ld\n", |
| 1216 | (long)CxOLD_IN_EVAL(cx)); |
| 1217 | PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_OP_TYPE = %s (%s)\n", |
| 1218 | PL_op_name[CxOLD_OP_TYPE(cx)], |
| 1219 | PL_op_desc[CxOLD_OP_TYPE(cx)]); |
| 1220 | if (cx->blk_eval.old_namesv) |
| 1221 | PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_NAME = %s\n", |
| 1222 | SvPVX_const(cx->blk_eval.old_namesv)); |
| 1223 | PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_EVAL_ROOT = 0x%"UVxf"\n", |
| 1224 | PTR2UV(cx->blk_eval.old_eval_root)); |
| 1225 | PerlIO_printf(Perl_debug_log, "BLK_EVAL.RETOP = 0x%"UVxf"\n", |
| 1226 | PTR2UV(cx->blk_eval.retop)); |
| 1227 | break; |
| 1228 | |
| 1229 | case CXt_LOOP_LAZYIV: |
| 1230 | case CXt_LOOP_LAZYSV: |
| 1231 | case CXt_LOOP_FOR: |
| 1232 | case CXt_LOOP_PLAIN: |
| 1233 | PerlIO_printf(Perl_debug_log, "BLK_LOOP.LABEL = %s\n", CxLABEL(cx)); |
| 1234 | PerlIO_printf(Perl_debug_log, "BLK_LOOP.RESETSP = %ld\n", |
| 1235 | (long)cx->blk_loop.resetsp); |
| 1236 | PerlIO_printf(Perl_debug_log, "BLK_LOOP.MY_OP = 0x%"UVxf"\n", |
| 1237 | PTR2UV(cx->blk_loop.my_op)); |
| 1238 | /* XXX: not accurate for LAZYSV/IV */ |
| 1239 | PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERARY = 0x%"UVxf"\n", |
| 1240 | PTR2UV(cx->blk_loop.state_u.ary.ary)); |
| 1241 | PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERIX = %ld\n", |
| 1242 | (long)cx->blk_loop.state_u.ary.ix); |
| 1243 | PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERVAR = 0x%"UVxf"\n", |
| 1244 | PTR2UV(CxITERVAR(cx))); |
| 1245 | break; |
| 1246 | |
| 1247 | case CXt_SUBST: |
| 1248 | PerlIO_printf(Perl_debug_log, "SB_ITERS = %ld\n", |
| 1249 | (long)cx->sb_iters); |
| 1250 | PerlIO_printf(Perl_debug_log, "SB_MAXITERS = %ld\n", |
| 1251 | (long)cx->sb_maxiters); |
| 1252 | PerlIO_printf(Perl_debug_log, "SB_RFLAGS = %ld\n", |
| 1253 | (long)cx->sb_rflags); |
| 1254 | PerlIO_printf(Perl_debug_log, "SB_ONCE = %ld\n", |
| 1255 | (long)CxONCE(cx)); |
| 1256 | PerlIO_printf(Perl_debug_log, "SB_ORIG = %s\n", |
| 1257 | cx->sb_orig); |
| 1258 | PerlIO_printf(Perl_debug_log, "SB_DSTR = 0x%"UVxf"\n", |
| 1259 | PTR2UV(cx->sb_dstr)); |
| 1260 | PerlIO_printf(Perl_debug_log, "SB_TARG = 0x%"UVxf"\n", |
| 1261 | PTR2UV(cx->sb_targ)); |
| 1262 | PerlIO_printf(Perl_debug_log, "SB_S = 0x%"UVxf"\n", |
| 1263 | PTR2UV(cx->sb_s)); |
| 1264 | PerlIO_printf(Perl_debug_log, "SB_M = 0x%"UVxf"\n", |
| 1265 | PTR2UV(cx->sb_m)); |
| 1266 | PerlIO_printf(Perl_debug_log, "SB_STREND = 0x%"UVxf"\n", |
| 1267 | PTR2UV(cx->sb_strend)); |
| 1268 | PerlIO_printf(Perl_debug_log, "SB_RXRES = 0x%"UVxf"\n", |
| 1269 | PTR2UV(cx->sb_rxres)); |
| 1270 | break; |
| 1271 | } |
| 1272 | #else |
| 1273 | PERL_UNUSED_CONTEXT; |
| 1274 | PERL_UNUSED_ARG(cx); |
| 1275 | #endif /* DEBUGGING */ |
| 1276 | } |
| 1277 | |
| 1278 | /* |
| 1279 | * Local variables: |
| 1280 | * c-indentation-style: bsd |
| 1281 | * c-basic-offset: 4 |
| 1282 | * indent-tabs-mode: t |
| 1283 | * End: |
| 1284 | * |
| 1285 | * ex: set ts=8 sts=4 sw=4 noet: |
| 1286 | */ |