1 /* This file is part of the indirect Perl module.
2 * See http://search.cpan.org/dist/indirect/ */
4 #define PERL_NO_GET_CONTEXT
9 #define __PACKAGE__ "indirect"
10 #define __PACKAGE_LEN__ (sizeof(__PACKAGE__)-1)
12 /* --- Compatibility wrappers ---------------------------------------------- */
23 # define Newx(v, n, c) New(0, v, n, c)
27 # define SvPV_const SvPV
30 #ifndef SvPV_nolen_const
31 # define SvPV_nolen_const SvPV_nolen
35 # define SvPVX_const SvPVX
38 #ifndef SvREFCNT_inc_simple_void_NN
39 # ifdef SvREFCNT_inc_simple_NN
40 # define SvREFCNT_inc_simple_void_NN SvREFCNT_inc_simple_NN
42 # define SvREFCNT_inc_simple_void_NN SvREFCNT_inc
46 #ifndef sv_catpvn_nomg
47 # define sv_catpvn_nomg sv_catpvn
51 # define mPUSHp(P, L) PUSHs(sv_2mortal(newSVpvn((P), (L))))
55 # define mPUSHu(U) PUSHs(sv_2mortal(newSVuv(U)))
59 # define HvNAME_get(H) HvNAME(H)
63 # define HvNAMELEN_get(H) strlen(HvNAME_get(H))
68 # define OpSIBLING(O) OP_SIBLING(O)
70 # define OpSIBLING(O) ((O)->op_sibling)
74 #define I_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
76 #if I_HAS_PERL(5, 10, 0) || defined(PL_parser)
78 # define PL_linestr PL_parser->linestr
81 # define PL_bufptr PL_parser->bufptr
84 # define PL_oldbufptr PL_parser->oldbufptr
86 # ifndef PL_lex_inwhat
87 # define PL_lex_inwhat PL_parser->lex_inwhat
91 # define PL_linestr PL_Ilinestr
94 # define PL_bufptr PL_Ibufptr
97 # define PL_oldbufptr PL_Ioldbufptr
99 # ifndef PL_lex_inwhat
100 # define PL_lex_inwhat PL_Ilex_inwhat
105 # define I_ASSERT(C) assert(C)
110 #ifndef I_WORKAROUND_REQUIRE_PROPAGATION
111 # define I_WORKAROUND_REQUIRE_PROPAGATION !I_HAS_PERL(5, 10, 1)
114 /* ... Thread safety and multiplicity ...................................... */
116 /* Safe unless stated otherwise in Makefile.PL */
118 # define I_FORKSAFE 1
121 #ifndef I_MULTIPLICITY
122 # if defined(MULTIPLICITY)
123 # define I_MULTIPLICITY 1
125 # define I_MULTIPLICITY 0
129 # ifndef PERL_IMPLICIT_CONTEXT
130 # error MULTIPLICITY builds must set PERL_IMPLICIT_CONTEXT
133 #if I_MULTIPLICITY && !defined(tTHX)
134 # define tTHX PerlInterpreter*
137 #if I_MULTIPLICITY && defined(USE_ITHREADS) && defined(dMY_CXT) && defined(MY_CXT) && defined(START_MY_CXT) && defined(MY_CXT_INIT) && (defined(MY_CXT_CLONE) || defined(dMY_CXT_SV))
138 # define I_THREADSAFE 1
139 # ifndef MY_CXT_CLONE
140 # define MY_CXT_CLONE \
142 my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
143 Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
144 sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
147 # define I_THREADSAFE 0
149 # define dMY_CXT dNOOP
151 # define MY_CXT indirect_globaldata
153 # define START_MY_CXT static my_cxt_t MY_CXT;
155 # define MY_CXT_INIT NOOP
157 # define MY_CXT_CLONE NOOP
161 /* We must use preexistent global mutexes or we will never be able to destroy
163 # if I_HAS_PERL(5, 9, 3)
164 # define I_LOADED_LOCK MUTEX_LOCK(&PL_my_ctx_mutex)
165 # define I_LOADED_UNLOCK MUTEX_UNLOCK(&PL_my_ctx_mutex)
167 # define I_LOADED_LOCK OP_REFCNT_LOCK
168 # define I_LOADED_UNLOCK OP_REFCNT_UNLOCK
171 # define I_LOADED_LOCK NOOP
172 # define I_LOADED_UNLOCK NOOP
175 #if defined(OP_CHECK_MUTEX_LOCK) && defined(OP_CHECK_MUTEX_UNLOCK)
176 # define I_CHECK_LOCK OP_CHECK_MUTEX_LOCK
177 # define I_CHECK_UNLOCK OP_CHECK_MUTEX_UNLOCK
178 #elif I_HAS_PERL(5, 9, 3)
179 # define I_CHECK_LOCK OP_REFCNT_LOCK
180 # define I_CHECK_UNLOCK OP_REFCNT_UNLOCK
182 /* Before perl 5.9.3, indirect_ck_*() calls are already protected by the
183 * I_LOADED mutex, which falls back to the OP_REFCNT mutex. Make sure we don't
185 # define I_CHECK_LOCK NOOP
186 # define I_CHECK_UNLOCK NOOP
189 typedef OP *(*indirect_ck_t)(pTHX_ OP *);
191 #ifdef wrap_op_checker
193 # define indirect_ck_replace(T, NC, OCP) wrap_op_checker((T), (NC), (OCP))
197 static void indirect_ck_replace(pTHX_ OPCODE type, indirect_ck_t new_ck, indirect_ck_t *old_ck_p) {
198 #define indirect_ck_replace(T, NC, OCP) indirect_ck_replace(aTHX_ (T), (NC), (OCP))
201 *old_ck_p = PL_check[type];
202 PL_check[type] = new_ck;
209 static void indirect_ck_restore(pTHX_ OPCODE type, indirect_ck_t *old_ck_p) {
210 #define indirect_ck_restore(T, OCP) indirect_ck_restore(aTHX_ (T), (OCP))
213 PL_check[type] = *old_ck_p;
219 /* --- Helpers ------------------------------------------------------------- */
221 /* ... Check if the module is loaded ....................................... */
223 static I32 indirect_loaded = 0;
227 #define PTABLE_NAME ptable_loaded
228 #define PTABLE_NEED_DELETE 1
229 #define PTABLE_NEED_WALK 0
233 #define ptable_loaded_store(T, K, V) ptable_loaded_store(aPTBLMS_ (T), (K), (V))
234 #define ptable_loaded_delete(T, K) ptable_loaded_delete(aPTBLMS_ (T), (K))
235 #define ptable_loaded_free(T) ptable_loaded_free(aPTBLMS_ (T))
237 static ptable *indirect_loaded_cxts = NULL;
239 static int indirect_is_loaded(pTHX_ void *cxt) {
240 #define indirect_is_loaded(C) indirect_is_loaded(aTHX_ (C))
244 if (indirect_loaded_cxts && ptable_fetch(indirect_loaded_cxts, cxt))
251 static int indirect_set_loaded_locked(pTHX_ void *cxt) {
252 #define indirect_set_loaded_locked(C) indirect_set_loaded_locked(aTHX_ (C))
253 int global_setup = 0;
255 if (indirect_loaded <= 0) {
256 I_ASSERT(indirect_loaded == 0);
257 I_ASSERT(!indirect_loaded_cxts);
258 indirect_loaded_cxts = ptable_new();
262 I_ASSERT(indirect_loaded_cxts);
263 ptable_loaded_store(indirect_loaded_cxts, cxt, cxt);
268 static int indirect_clear_loaded_locked(pTHX_ void *cxt) {
269 #define indirect_clear_loaded_locked(C) indirect_clear_loaded_locked(aTHX_ (C))
270 int global_teardown = 0;
272 if (indirect_loaded > 1) {
273 I_ASSERT(indirect_loaded_cxts);
274 ptable_loaded_delete(indirect_loaded_cxts, cxt);
276 } else if (indirect_loaded_cxts) {
277 I_ASSERT(indirect_loaded == 1);
278 ptable_loaded_free(indirect_loaded_cxts);
279 indirect_loaded_cxts = NULL;
284 return global_teardown;
289 #define indirect_is_loaded(C) (indirect_loaded > 0)
290 #define indirect_set_loaded_locked(C) ((indirect_loaded++ <= 0) ? 1 : 0)
291 #define indirect_clear_loaded_locked(C) ((--indirect_loaded <= 0) ? 1 : 0)
295 /* ... Thread-safe hints ................................................... */
297 #if I_WORKAROUND_REQUIRE_PROPAGATION
304 #define I_HINT_STRUCT 1
306 #define I_HINT_CODE(H) ((H)->code)
308 #define I_HINT_FREE(H) { \
309 indirect_hint_t *h = (H); \
310 SvREFCNT_dec(h->code); \
311 PerlMemShared_free(h); \
314 #else /* I_WORKAROUND_REQUIRE_PROPAGATION */
316 typedef SV indirect_hint_t;
318 #define I_HINT_STRUCT 0
320 #define I_HINT_CODE(H) (H)
322 #define I_HINT_FREE(H) SvREFCNT_dec(H);
324 #endif /* !I_WORKAROUND_REQUIRE_PROPAGATION */
328 #define PTABLE_NAME ptable_hints
329 #define PTABLE_VAL_FREE(V) I_HINT_FREE(V)
330 #define PTABLE_NEED_DELETE 0
331 #define PTABLE_NEED_WALK 1
340 #define ptable_hints_store(T, K, V) ptable_hints_store(aTHX_ (T), (K), (V))
341 #define ptable_hints_free(T) ptable_hints_free(aTHX_ (T))
343 #endif /* I_THREADSAFE */
345 /* Define the op->str ptable here because we need to be able to clean it during
354 } indirect_op_info_t;
356 #define PTABLE_NAME ptable
357 #define PTABLE_VAL_FREE(V) if (V) { Safefree(((indirect_op_info_t *) (V))->buf); Safefree(V); }
358 #define PTABLE_NEED_DELETE 1
359 #define PTABLE_NEED_WALK 0
368 #define ptable_store(T, K, V) ptable_store(aTHX_ (T), (K), (V))
369 #define ptable_delete(T, K) ptable_delete(aTHX_ (T), (K))
370 #define ptable_clear(T) ptable_clear(aTHX_ (T))
371 #define ptable_free(T) ptable_free(aTHX_ (T))
373 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
377 ptable *tbl; /* It really is a ptable_hints */
390 #if I_HAS_PERL(5, 13, 2)
391 CLONE_PARAMS *params;
395 } indirect_ptable_clone_ud;
397 #if I_HAS_PERL(5, 13, 2)
398 # define indirect_ptable_clone_ud_init(U, T, O) \
400 (U).params = Perl_clone_params_new((O), aTHX)
401 # define indirect_ptable_clone_ud_deinit(U) Perl_clone_params_del((U).params)
402 # define indirect_dup_inc(S, U) SvREFCNT_inc(sv_dup((S), (U)->params))
404 # define indirect_ptable_clone_ud_init(U, T, O) \
406 (U).params.stashes = newAV(); \
407 (U).params.flags = 0; \
408 (U).params.proto_perl = (O)
409 # define indirect_ptable_clone_ud_deinit(U) SvREFCNT_dec((U).params.stashes)
410 # define indirect_dup_inc(S, U) SvREFCNT_inc(sv_dup((S), &((U)->params)))
413 static void indirect_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
414 indirect_ptable_clone_ud *ud = ud_;
415 indirect_hint_t *h1 = ent->val;
420 h2 = PerlMemShared_malloc(sizeof *h2);
421 h2->code = indirect_dup_inc(h1->code, ud);
422 #if I_WORKAROUND_REQUIRE_PROPAGATION
423 h2->require_tag = PTR2IV(indirect_dup_inc(INT2PTR(SV *, h1->require_tag), ud));
426 #else /* I_HINT_STRUCT */
428 h2 = indirect_dup_inc(h1, ud);
430 #endif /* !I_HINT_STRUCT */
432 ptable_hints_store(ud->tbl, ent->key, h2);
435 #endif /* I_THREADSAFE */
437 #if I_WORKAROUND_REQUIRE_PROPAGATION
439 static IV indirect_require_tag(pTHX) {
440 #define indirect_require_tag() indirect_require_tag(aTHX)
441 const CV *cv, *outside;
446 /* If for some reason the pragma is operational at run-time, try to discover
447 * the current cv in use. */
450 for (si = PL_curstackinfo; si; si = si->si_prev) {
453 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
454 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
456 switch (CxTYPE(cx)) {
459 /* The propagation workaround is only needed up to 5.10.0 and at that
460 * time format and sub contexts were still identical. And even later the
461 * cv members offsets should have been kept the same. */
463 goto get_enclosing_cv;
465 cv = cx->blk_eval.cv;
466 goto get_enclosing_cv;
477 for (outside = CvOUTSIDE(cv); outside; outside = CvOUTSIDE(cv))
483 #endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
485 static SV *indirect_tag(pTHX_ SV *value) {
486 #define indirect_tag(V) indirect_tag(aTHX_ (V))
494 #endif /* I_THREADSAFE */
498 if (SvTYPE(value) >= SVt_PVCV) {
500 SvREFCNT_inc_simple_void_NN(code);
505 h = PerlMemShared_malloc(sizeof *h);
507 # if I_WORKAROUND_REQUIRE_PROPAGATION
508 h->require_tag = indirect_require_tag();
509 # endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
510 #else /* I_HINT_STRUCT */
512 #endif /* !I_HINT_STRUCT */
515 /* We only need for the key to be an unique tag for looking up the value later
516 * Allocated memory provides convenient unique identifiers, so that's why we
517 * use the hint as the key itself. */
518 ptable_hints_store(MY_CXT.tbl, h, h);
519 #endif /* I_THREADSAFE */
521 return newSViv(PTR2IV(h));
524 static SV *indirect_detag(pTHX_ const SV *hint) {
525 #define indirect_detag(H) indirect_detag(aTHX_ (H))
527 #if I_THREADSAFE || I_WORKAROUND_REQUIRE_PROPAGATION
534 #endif /* I_THREADSAFE */
536 h = INT2PTR(indirect_hint_t *, SvIVX(hint));
538 h = ptable_fetch(MY_CXT.tbl, h);
539 #endif /* I_THREADSAFE */
541 #if I_WORKAROUND_REQUIRE_PROPAGATION
542 if (indirect_require_tag() != h->require_tag)
543 return MY_CXT.global_code;
544 #endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
546 return I_HINT_CODE(h);
549 static VOL U32 indirect_hash = 0;
551 static SV *indirect_hint(pTHX) {
552 #define indirect_hint() indirect_hint(aTHX)
558 #if I_HAS_PERL(5, 10, 0) || defined(PL_parser)
563 #ifdef cop_hints_fetch_pvn
564 hint = cop_hints_fetch_pvn(PL_curcop, __PACKAGE__, __PACKAGE_LEN__,
566 #elif I_HAS_PERL(5, 9, 5)
567 hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
569 __PACKAGE__, __PACKAGE_LEN__,
574 SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, 0);
580 if (hint && SvIOK(hint)) {
581 return indirect_detag(hint);
584 if (indirect_is_loaded(&MY_CXT))
585 return MY_CXT.global_code;
591 /* ... op -> source position ............................................... */
593 static void indirect_map_store(pTHX_ const OP *o, STRLEN pos, SV *sv, line_t line) {
594 #define indirect_map_store(O, P, N, L) indirect_map_store(aTHX_ (O), (P), (N), (L))
595 indirect_op_info_t *oi;
600 /* No need to check for MY_CXT.map != NULL because this code path is always
601 * guarded by indirect_hint(). */
603 if (!(oi = ptable_fetch(MY_CXT.map, o))) {
604 Newx(oi, 1, indirect_op_info_t);
605 ptable_store(MY_CXT.map, o, oi);
611 s = SvPV_const(sv, len);
617 if (len > oi->size) {
619 Newx(oi->buf, len, char);
622 Copy(s, oi->buf, len, char);
629 static const indirect_op_info_t *indirect_map_fetch(pTHX_ const OP *o) {
630 #define indirect_map_fetch(O) indirect_map_fetch(aTHX_ (O))
633 /* No need to check for MY_CXT.map != NULL because this code path is always
634 * guarded by indirect_hint(). */
636 return ptable_fetch(MY_CXT.map, o);
639 static void indirect_map_delete(pTHX_ const OP *o) {
640 #define indirect_map_delete(O) indirect_map_delete(aTHX_ (O))
643 if (indirect_is_loaded(&MY_CXT) && MY_CXT.map)
644 ptable_delete(MY_CXT.map, o);
647 /* --- Safe version of call_sv() ------------------------------------------- */
649 static I32 indirect_call_sv(pTHX_ SV *sv, I32 flags) {
650 #define indirect_call_sv(S, F) indirect_call_sv(aTHX_ (S), (F))
652 PERL_CONTEXT saved_cx;
653 SV *saved_errsv = NULL;
656 if (IN_PERL_COMPILETIME && PL_errors)
657 sv_catsv(PL_errors, ERRSV);
659 saved_errsv = newSVsv(ERRSV);
663 cxix = (cxstack_ix < cxstack_max) ? (cxstack_ix + 1) : Perl_cxinc(aTHX);
664 /* The last popped context will be reused by call_sv(), but our callers may
665 * still need its previous value. Back it up so that it isn't clobbered. */
666 saved_cx = cxstack[cxix];
668 ret = call_sv(sv, flags | G_EVAL);
670 cxstack[cxix] = saved_cx;
673 /* Discard the old ERRSV, and reuse the variable to temporarily store the
676 sv_setsv(saved_errsv, ERRSV);
678 saved_errsv = newSVsv(ERRSV);
680 /* Immediately flush all errors. */
681 if (IN_PERL_COMPILETIME) {
682 #if I_HAS_PERL(5, 10, 0) || defined(PL_parser)
684 ++PL_parser->error_count;
685 #elif defined(PL_error_count)
691 sv_setsv(ERRSV, PL_errors);
692 SvCUR_set(PL_errors, 0);
695 sv_catsv(ERRSV, saved_errsv);
696 SvREFCNT_dec(saved_errsv);
698 } else if (saved_errsv) {
699 /* If IN_PERL_COMPILETIME && PL_errors, then the old ERRSV has already been
700 * added to PL_errors. Otherwise, just restore it to ERRSV, as if no eval
701 * block has ever been executed. */
702 sv_setsv(ERRSV, saved_errsv);
703 SvREFCNT_dec(saved_errsv);
709 /* --- Check functions ----------------------------------------------------- */
711 static int indirect_find(pTHX_ SV *name_sv, const char *line_bufptr, STRLEN *name_pos) {
712 #define indirect_find(NSV, LBP, NP) indirect_find(aTHX_ (NSV), (LBP), (NP))
713 STRLEN name_len, line_len;
714 const char *name, *name_end;
715 const char *line, *line_end;
718 line = SvPV_const(PL_linestr, line_len);
719 line_end = line + line_len;
721 name = SvPV_const(name_sv, name_len);
722 if (name_len >= 1 && *name == '$') {
725 while (line_bufptr < line_end && *line_bufptr != '$')
727 if (line_bufptr >= line_end)
730 name_end = name + name_len;
734 p = ninstr(p, line_end, name, name_end);
737 if (!isALNUM(p[name_len]))
739 /* p points to a word that has name as prefix, skip the rest of the word */
745 *name_pos = p - line;
750 /* ... ck_const ............................................................ */
752 static OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
754 static OP *indirect_ck_const(pTHX_ OP *o) {
755 o = indirect_old_ck_const(aTHX_ o);
757 if (indirect_hint()) {
760 if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV)) {
763 if (indirect_find(sv, PL_oldbufptr, &pos)) {
766 /* If the constant is equal to the current package name, try to look for
767 * a "__PACKAGE__" coming before what we got. We only need to check this
768 * when we already had a match because __PACKAGE__ can only appear in
769 * direct method calls ("new __PACKAGE__" is a syntax error). */
772 && len == (STRLEN) HvNAMELEN_get(PL_curstash)
773 && memcmp(SvPVX(sv), HvNAME_get(PL_curstash), len) == 0) {
775 SV *pkg = sv_newmortal();
776 sv_setpvn(pkg, "__PACKAGE__", sizeof("__PACKAGE__")-1);
778 if (indirect_find(pkg, PL_oldbufptr, &pos_pkg) && pos_pkg < pos) {
784 indirect_map_store(o, pos, sv, CopLINE(&PL_compiling));
790 indirect_map_delete(o);
794 /* ... ck_rv2sv ............................................................ */
796 static OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
798 static OP *indirect_ck_rv2sv(pTHX_ OP *o) {
799 if (indirect_hint()) {
800 OP *op = cUNOPo->op_first;
802 const char *name = NULL;
804 OPCODE type = (OPCODE) op->op_type;
809 GV *gv = cGVOPx_gv(op);
815 if ((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP) {
816 SV *nsv = cSVOPx_sv(op);
817 if (SvPOK(nsv) && (SvTYPE(nsv) >= SVt_PV))
818 name = SvPV_const(nsv, len);
824 sv = sv_2mortal(newSVpvn("$", 1));
825 sv_catpvn_nomg(sv, name, len);
826 if (!indirect_find(sv, PL_oldbufptr, &pos)) {
827 /* If it failed, retry without the current stash */
828 const char *stash = HvNAME_get(PL_curstash);
829 STRLEN stashlen = HvNAMELEN_get(PL_curstash);
831 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
832 || name[stashlen] != ':' || name[stashlen+1] != ':') {
833 /* Failed again ? Try to remove main */
836 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
837 || name[stashlen] != ':' || name[stashlen+1] != ':')
841 sv_setpvn(sv, "$", 1);
843 sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
844 if (!indirect_find(sv, PL_oldbufptr, &pos))
848 o = indirect_old_ck_rv2sv(aTHX_ o);
850 indirect_map_store(o, pos, sv, CopLINE(&PL_compiling));
855 o = indirect_old_ck_rv2sv(aTHX_ o);
857 indirect_map_delete(o);
861 /* ... ck_padany ........................................................... */
863 static OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
865 static OP *indirect_ck_padany(pTHX_ OP *o) {
866 o = indirect_old_ck_padany(aTHX_ o);
868 if (indirect_hint()) {
870 const char *s = PL_oldbufptr, *t = PL_bufptr - 1;
872 while (s < t && isSPACE(*s)) ++s;
873 if (*s == '$' && ++s <= t) {
874 while (s < t && isSPACE(*s)) ++s;
875 while (s < t && isSPACE(*t)) --t;
876 sv = sv_2mortal(newSVpvn("$", 1));
877 sv_catpvn_nomg(sv, s, t - s + 1);
878 indirect_map_store(o, s - SvPVX_const(PL_linestr),
879 sv, CopLINE(&PL_compiling));
884 indirect_map_delete(o);
888 /* ... ck_scope ............................................................ */
890 static OP *(*indirect_old_ck_scope) (pTHX_ OP *) = 0;
891 static OP *(*indirect_old_ck_lineseq)(pTHX_ OP *) = 0;
893 static OP *indirect_ck_scope(pTHX_ OP *o) {
894 OP *(*old_ck)(pTHX_ OP *) = 0;
896 switch (o->op_type) {
897 case OP_SCOPE: old_ck = indirect_old_ck_scope; break;
898 case OP_LINESEQ: old_ck = indirect_old_ck_lineseq; break;
902 if (indirect_hint()) {
903 indirect_map_store(o, PL_oldbufptr - SvPVX_const(PL_linestr),
904 NULL, CopLINE(&PL_compiling));
908 indirect_map_delete(o);
912 /* We don't need to clean the map entries for leave ops because they can only
913 * be created by mutating from a lineseq. */
915 /* ... ck_method ........................................................... */
917 static OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
919 static OP *indirect_ck_method(pTHX_ OP *o) {
920 if (indirect_hint()) {
921 OP *op = cUNOPo->op_first;
923 /* Indirect method call is only possible when the method is a bareword, so
924 * don't trip up on $obj->$meth. */
925 if (op && op->op_type == OP_CONST) {
926 const indirect_op_info_t *oi = indirect_map_fetch(op);
934 sv = sv_2mortal(newSVpvn(oi->buf, oi->len));
936 /* Keep the old line so that we really point to the first line of the
940 o = indirect_old_ck_method(aTHX_ o);
941 /* o may now be a method_named */
943 indirect_map_store(o, pos, sv, line);
949 o = indirect_old_ck_method(aTHX_ o);
951 indirect_map_delete(o);
955 /* ... ck_method_named ..................................................... */
957 /* "use foo/no foo" compiles its call to import/unimport directly to a
958 * method_named op. */
960 static OP *(*indirect_old_ck_method_named)(pTHX_ OP *) = 0;
962 static OP *indirect_ck_method_named(pTHX_ OP *o) {
963 if (indirect_hint()) {
969 if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
971 sv = sv_mortalcopy(sv);
973 if (!indirect_find(sv, PL_oldbufptr, &pos))
975 line = CopLINE(&PL_compiling);
977 o = indirect_old_ck_method_named(aTHX_ o);
979 indirect_map_store(o, pos, sv, line);
984 o = indirect_old_ck_method_named(aTHX_ o);
986 indirect_map_delete(o);
990 /* ... ck_entersub ......................................................... */
992 static OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
994 static OP *indirect_ck_entersub(pTHX_ OP *o) {
995 SV *code = indirect_hint();
997 o = indirect_old_ck_entersub(aTHX_ o);
1000 const indirect_op_info_t *moi, *ooi;
1006 lop = (LISTOP *) oop;
1007 if (!(lop->op_flags & OPf_KIDS))
1009 oop = lop->op_first;
1010 } while (oop->op_type != OP_PUSHMARK);
1011 oop = OpSIBLING(oop);
1017 switch (oop->op_type) {
1028 if (mop->op_type == OP_METHOD)
1029 mop = cUNOPx(mop)->op_first;
1030 else if (mop->op_type != OP_METHOD_NAMED)
1033 moi = indirect_map_fetch(mop);
1037 ooi = indirect_map_fetch(oop);
1041 /* When positions are identical, the method and the object must have the
1042 * same name. But it also means that it is an indirect call, as "foo->foo"
1043 * results in different positions. */
1044 if ( moi->line < ooi->line
1045 || (moi->line == ooi->line && moi->pos <= ooi->pos)) {
1053 file = sv_2mortal(newSVpv(CopFILE(&PL_compiling), 0));
1055 file = sv_mortalcopy(CopFILESV(&PL_compiling));
1060 mPUSHp(ooi->buf, ooi->len);
1061 mPUSHp(moi->buf, moi->len);
1066 indirect_call_sv(code, G_VOID);
1079 /* --- Module setup/teardown ----------------------------------------------- */
1081 static void indirect_teardown(pTHX_ void *interp) {
1086 if (indirect_clear_loaded_locked(&MY_CXT)) {
1087 indirect_ck_restore(OP_CONST, &indirect_old_ck_const);
1088 indirect_ck_restore(OP_RV2SV, &indirect_old_ck_rv2sv);
1089 indirect_ck_restore(OP_PADANY, &indirect_old_ck_padany);
1090 indirect_ck_restore(OP_SCOPE, &indirect_old_ck_scope);
1091 indirect_ck_restore(OP_LINESEQ, &indirect_old_ck_lineseq);
1093 indirect_ck_restore(OP_METHOD, &indirect_old_ck_method);
1094 indirect_ck_restore(OP_METHOD_NAMED, &indirect_old_ck_method_named);
1095 indirect_ck_restore(OP_ENTERSUB, &indirect_old_ck_entersub);
1100 SvREFCNT_dec(MY_CXT.global_code);
1101 MY_CXT.global_code = NULL;
1103 ptable_free(MY_CXT.map);
1107 ptable_hints_free(MY_CXT.tbl);
1114 static void indirect_setup(pTHX) {
1115 #define indirect_setup() indirect_setup(aTHX)
1116 MY_CXT_INIT; /* Takes/release PL_my_ctx_mutex */
1120 if (indirect_set_loaded_locked(&MY_CXT)) {
1121 PERL_HASH(indirect_hash, __PACKAGE__, __PACKAGE_LEN__);
1123 indirect_ck_replace(OP_CONST, indirect_ck_const, &indirect_old_ck_const);
1124 indirect_ck_replace(OP_RV2SV, indirect_ck_rv2sv, &indirect_old_ck_rv2sv);
1125 indirect_ck_replace(OP_PADANY, indirect_ck_padany, &indirect_old_ck_padany);
1126 indirect_ck_replace(OP_SCOPE, indirect_ck_scope, &indirect_old_ck_scope);
1127 indirect_ck_replace(OP_LINESEQ, indirect_ck_scope, &indirect_old_ck_lineseq);
1129 indirect_ck_replace(OP_METHOD, indirect_ck_method,
1130 &indirect_old_ck_method);
1131 indirect_ck_replace(OP_METHOD_NAMED, indirect_ck_method_named,
1132 &indirect_old_ck_method_named);
1133 indirect_ck_replace(OP_ENTERSUB, indirect_ck_entersub,
1134 &indirect_old_ck_entersub);
1142 stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
1143 newCONSTSUB(stash, "I_THREADSAFE", newSVuv(I_THREADSAFE));
1144 newCONSTSUB(stash, "I_FORKSAFE", newSVuv(I_FORKSAFE));
1147 MY_CXT.tbl = ptable_new();
1148 MY_CXT.owner = aTHX;
1151 MY_CXT.map = ptable_new();
1152 MY_CXT.global_code = NULL;
1155 call_atexit(indirect_teardown, NULL);
1160 /* --- XS ------------------------------------------------------------------ */
1162 MODULE = indirect PACKAGE = indirect
1178 SV *global_code_dup;
1181 indirect_ptable_clone_ud ud;
1184 indirect_ptable_clone_ud_init(ud, t, MY_CXT.owner);
1185 ptable_walk(MY_CXT.tbl, indirect_ptable_clone, &ud);
1186 global_code_dup = indirect_dup_inc(MY_CXT.global_code, &ud);
1187 indirect_ptable_clone_ud_deinit(ud);
1191 MY_CXT.map = ptable_new();
1193 MY_CXT.owner = aTHX;
1194 MY_CXT.global_code = global_code_dup;
1198 global_setup = indirect_set_loaded_locked(&MY_CXT);
1199 I_ASSERT(!global_setup);
1205 #endif /* I_THREADSAFE */
1211 RETVAL = indirect_tag(value);
1221 else if (SvROK(code))
1225 SvREFCNT_dec(MY_CXT.global_code);
1226 MY_CXT.global_code = SvREFCNT_inc(code);