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
104 #ifndef I_WORKAROUND_REQUIRE_PROPAGATION
105 # define I_WORKAROUND_REQUIRE_PROPAGATION !I_HAS_PERL(5, 10, 1)
108 /* ... Thread safety and multiplicity ...................................... */
110 /* Safe unless stated otherwise in Makefile.PL */
112 # define I_FORKSAFE 1
115 #ifndef I_MULTIPLICITY
116 # if defined(MULTIPLICITY)
117 # define I_MULTIPLICITY 1
119 # define I_MULTIPLICITY 0
123 # ifndef PERL_IMPLICIT_CONTEXT
124 # error MULTIPLICITY builds must set PERL_IMPLICIT_CONTEXT
127 #if I_MULTIPLICITY && !defined(tTHX)
128 # define tTHX PerlInterpreter*
131 #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))
132 # define I_THREADSAFE 1
133 # ifndef MY_CXT_CLONE
134 # define MY_CXT_CLONE \
136 my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
137 Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
138 sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
141 # define I_THREADSAFE 0
143 # define dMY_CXT dNOOP
145 # define MY_CXT indirect_globaldata
147 # define START_MY_CXT static my_cxt_t MY_CXT;
149 # define MY_CXT_INIT NOOP
151 # define MY_CXT_CLONE NOOP
154 #if defined(OP_CHECK_MUTEX_LOCK) && defined(OP_CHECK_MUTEX_UNLOCK)
155 # define I_CHECK_MUTEX_LOCK OP_CHECK_MUTEX_LOCK
156 # define I_CHECK_MUTEX_UNLOCK OP_CHECK_MUTEX_UNLOCK
158 # define I_CHECK_MUTEX_LOCK OP_REFCNT_LOCK
159 # define I_CHECK_MUTEX_UNLOCK OP_REFCNT_UNLOCK
162 typedef OP *(*indirect_ck_t)(pTHX_ OP *);
164 #ifdef wrap_op_checker
166 # define indirect_ck_replace(T, NC, OCP) wrap_op_checker((T), (NC), (OCP))
170 static void indirect_ck_replace(pTHX_ OPCODE type, indirect_ck_t new_ck, indirect_ck_t *old_ck_p) {
171 #define indirect_ck_replace(T, NC, OCP) indirect_ck_replace(aTHX_ (T), (NC), (OCP))
174 *old_ck_p = PL_check[type];
175 PL_check[type] = new_ck;
177 I_CHECK_MUTEX_UNLOCK;
182 static void indirect_ck_restore(pTHX_ OPCODE type, indirect_ck_t *old_ck_p) {
183 #define indirect_ck_restore(T, OCP) indirect_ck_restore(aTHX_ (T), (OCP))
186 PL_check[type] = *old_ck_p;
189 I_CHECK_MUTEX_UNLOCK;
192 /* --- Helpers ------------------------------------------------------------- */
194 /* ... Check if the module is loaded ....................................... */
198 #define PTABLE_NAME ptable_loaded
199 #define PTABLE_VAL_FREE(V) NOOP
203 #define ptable_loaded_store(T, K, V) ptable_loaded_store(aPTBLMS_ (T), (K), (V))
204 #define ptable_loaded_delete(T, K) ptable_loaded_delete(aPTBLMS_ (T), (K))
205 #define ptable_loaded_free(T) ptable_loaded_free(aPTBLMS_ (T))
207 #define indirect_loaded()
209 static ptable *indirect_loaded_cxts = NULL;
210 static U32 indirect_loaded_cxts_refcount = 0;
212 /* We must use preexistent global mutexes or we will never be able to destroy
214 #if I_HAS_PERL(5, 9, 3)
215 # define I_LOADED_LOCK MUTEX_LOCK(&PL_my_ctx_mutex)
216 # define I_LOADED_UNLOCK MUTEX_UNLOCK(&PL_my_ctx_mutex)
218 # define I_LOADED_LOCK OP_REFCNT_LOCK
219 # define I_LOADED_UNLOCK OP_REFCNT_UNLOCK
222 static int indirect_is_loaded(pTHX_ void *cxt) {
223 #define indirect_is_loaded(C) indirect_is_loaded(aTHX_ (C))
227 if (indirect_loaded_cxts && ptable_fetch(indirect_loaded_cxts, cxt))
234 static void indirect_set_loaded(pTHX_ void *cxt) {
235 #define indirect_set_loaded(C) indirect_set_loaded(aTHX_ (C))
237 if (!indirect_loaded_cxts) {
238 indirect_loaded_cxts = ptable_new();
239 indirect_loaded_cxts_refcount = 0;
241 ++indirect_loaded_cxts_refcount;
242 ptable_loaded_store(indirect_loaded_cxts, cxt, cxt);
246 static void indirect_clear_loaded(pTHX_ void *cxt) {
247 #define indirect_clear_loaded(C) indirect_clear_loaded(aTHX_ (C))
249 if (indirect_loaded_cxts_refcount <= 1) {
250 ptable_loaded_free(indirect_loaded_cxts);
251 indirect_loaded_cxts = NULL;
252 indirect_loaded_cxts_refcount = 0;
254 --indirect_loaded_cxts_refcount;
255 ptable_loaded_delete(indirect_loaded_cxts, cxt);
262 #define indirect_is_loaded(C) (1)
263 #define indirect_set_loaded(C) NOOP
264 #define indirect_clear_loaded(C) NOOP
268 /* ... Thread-safe hints ................................................... */
270 #if I_WORKAROUND_REQUIRE_PROPAGATION
277 #define I_HINT_STRUCT 1
279 #define I_HINT_CODE(H) ((H)->code)
281 #define I_HINT_FREE(H) { \
282 indirect_hint_t *h = (H); \
283 SvREFCNT_dec(h->code); \
284 PerlMemShared_free(h); \
287 #else /* I_WORKAROUND_REQUIRE_PROPAGATION */
289 typedef SV indirect_hint_t;
291 #define I_HINT_STRUCT 0
293 #define I_HINT_CODE(H) (H)
295 #define I_HINT_FREE(H) SvREFCNT_dec(H);
297 #endif /* !I_WORKAROUND_REQUIRE_PROPAGATION */
301 #define PTABLE_NAME ptable_hints
302 #define PTABLE_VAL_FREE(V) I_HINT_FREE(V)
311 #define ptable_hints_store(T, K, V) ptable_hints_store(aTHX_ (T), (K), (V))
312 #define ptable_hints_free(T) ptable_hints_free(aTHX_ (T))
314 #endif /* I_THREADSAFE */
316 /* Define the op->str ptable here because we need to be able to clean it during
325 } indirect_op_info_t;
327 #define PTABLE_NAME ptable
328 #define PTABLE_VAL_FREE(V) if (V) { Safefree(((indirect_op_info_t *) (V))->buf); Safefree(V); }
337 #define ptable_store(T, K, V) ptable_store(aTHX_ (T), (K), (V))
338 #define ptable_delete(T, K) ptable_delete(aTHX_ (T), (K))
339 #define ptable_clear(T) ptable_clear(aTHX_ (T))
340 #define ptable_free(T) ptable_free(aTHX_ (T))
342 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
346 ptable *tbl; /* It really is a ptable_hints */
359 #if I_HAS_PERL(5, 13, 2)
360 CLONE_PARAMS *params;
364 } indirect_ptable_clone_ud;
366 #if I_HAS_PERL(5, 13, 2)
367 # define indirect_ptable_clone_ud_init(U, T, O) \
369 (U).params = Perl_clone_params_new((O), aTHX)
370 # define indirect_ptable_clone_ud_deinit(U) Perl_clone_params_del((U).params)
371 # define indirect_dup_inc(S, U) SvREFCNT_inc(sv_dup((S), (U)->params))
373 # define indirect_ptable_clone_ud_init(U, T, O) \
375 (U).params.stashes = newAV(); \
376 (U).params.flags = 0; \
377 (U).params.proto_perl = (O)
378 # define indirect_ptable_clone_ud_deinit(U) SvREFCNT_dec((U).params.stashes)
379 # define indirect_dup_inc(S, U) SvREFCNT_inc(sv_dup((S), &((U)->params)))
382 static void indirect_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
383 indirect_ptable_clone_ud *ud = ud_;
384 indirect_hint_t *h1 = ent->val;
389 h2 = PerlMemShared_malloc(sizeof *h2);
390 h2->code = indirect_dup_inc(h1->code, ud);
391 #if I_WORKAROUND_REQUIRE_PROPAGATION
392 h2->require_tag = PTR2IV(indirect_dup_inc(INT2PTR(SV *, h1->require_tag), ud));
395 #else /* I_HINT_STRUCT */
397 h2 = indirect_dup_inc(h1, ud);
399 #endif /* !I_HINT_STRUCT */
401 ptable_hints_store(ud->tbl, ent->key, h2);
404 static void indirect_thread_cleanup(pTHX_ void *ud) {
407 indirect_clear_loaded(&MY_CXT);
409 SvREFCNT_dec(MY_CXT.global_code);
410 MY_CXT.global_code = NULL;
412 ptable_free(MY_CXT.map);
415 ptable_hints_free(MY_CXT.tbl);
419 static int indirect_endav_free(pTHX_ SV *sv, MAGIC *mg) {
420 SAVEDESTRUCTOR_X(indirect_thread_cleanup, NULL);
425 static MGVTBL indirect_endav_vtbl = {
442 #endif /* I_THREADSAFE */
444 #if I_WORKAROUND_REQUIRE_PROPAGATION
445 static IV indirect_require_tag(pTHX) {
446 #define indirect_require_tag() indirect_require_tag(aTHX)
447 const CV *cv, *outside;
452 /* If for some reason the pragma is operational at run-time, try to discover
453 * the current cv in use. */
456 for (si = PL_curstackinfo; si; si = si->si_prev) {
459 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
460 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
462 switch (CxTYPE(cx)) {
465 /* The propagation workaround is only needed up to 5.10.0 and at that
466 * time format and sub contexts were still identical. And even later the
467 * cv members offsets should have been kept the same. */
469 goto get_enclosing_cv;
471 cv = cx->blk_eval.cv;
472 goto get_enclosing_cv;
483 for (outside = CvOUTSIDE(cv); outside; outside = CvOUTSIDE(cv))
488 #endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
490 static SV *indirect_tag(pTHX_ SV *value) {
491 #define indirect_tag(V) indirect_tag(aTHX_ (V))
499 #endif /* I_THREADSAFE */
503 if (SvTYPE(value) >= SVt_PVCV) {
505 SvREFCNT_inc_simple_void_NN(code);
510 h = PerlMemShared_malloc(sizeof *h);
512 # if I_WORKAROUND_REQUIRE_PROPAGATION
513 h->require_tag = indirect_require_tag();
514 # endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
515 #else /* I_HINT_STRUCT */
517 #endif /* !I_HINT_STRUCT */
520 /* We only need for the key to be an unique tag for looking up the value later
521 * Allocated memory provides convenient unique identifiers, so that's why we
522 * use the hint as the key itself. */
523 ptable_hints_store(MY_CXT.tbl, h, h);
524 #endif /* I_THREADSAFE */
526 return newSViv(PTR2IV(h));
529 static SV *indirect_detag(pTHX_ const SV *hint) {
530 #define indirect_detag(H) indirect_detag(aTHX_ (H))
532 #if I_THREADSAFE || I_WORKAROUND_REQUIRE_PROPAGATION
539 #endif /* I_THREADSAFE */
541 h = INT2PTR(indirect_hint_t *, SvIVX(hint));
543 h = ptable_fetch(MY_CXT.tbl, h);
544 #endif /* I_THREADSAFE */
546 #if I_WORKAROUND_REQUIRE_PROPAGATION
547 if (indirect_require_tag() != h->require_tag)
548 return MY_CXT.global_code;
549 #endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
551 return I_HINT_CODE(h);
554 static VOL U32 indirect_hash = 0;
556 static SV *indirect_hint(pTHX) {
557 #define indirect_hint() indirect_hint(aTHX)
563 #if I_HAS_PERL(5, 10, 0) || defined(PL_parser)
568 #ifdef cop_hints_fetch_pvn
569 hint = cop_hints_fetch_pvn(PL_curcop, __PACKAGE__, __PACKAGE_LEN__,
571 #elif I_HAS_PERL(5, 9, 5)
572 hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
574 __PACKAGE__, __PACKAGE_LEN__,
579 SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, 0);
585 if (hint && SvIOK(hint)) {
586 return indirect_detag(hint);
589 if (indirect_is_loaded(&MY_CXT))
590 return MY_CXT.global_code;
596 /* ... op -> source position ............................................... */
598 static void indirect_map_store(pTHX_ const OP *o, STRLEN pos, SV *sv, line_t line) {
599 #define indirect_map_store(O, P, N, L) indirect_map_store(aTHX_ (O), (P), (N), (L))
600 indirect_op_info_t *oi;
605 /* No need to check for MY_CXT.map != NULL because this code path is always
606 * guarded by indirect_hint(). */
608 if (!(oi = ptable_fetch(MY_CXT.map, o))) {
609 Newx(oi, 1, indirect_op_info_t);
610 ptable_store(MY_CXT.map, o, oi);
616 s = SvPV_const(sv, len);
622 if (len > oi->size) {
624 Newx(oi->buf, len, char);
627 Copy(s, oi->buf, len, char);
634 static const indirect_op_info_t *indirect_map_fetch(pTHX_ const OP *o) {
635 #define indirect_map_fetch(O) indirect_map_fetch(aTHX_ (O))
638 /* No need to check for MY_CXT.map != NULL because this code path is always
639 * guarded by indirect_hint(). */
641 return ptable_fetch(MY_CXT.map, o);
644 static void indirect_map_delete(pTHX_ const OP *o) {
645 #define indirect_map_delete(O) indirect_map_delete(aTHX_ (O))
648 if (indirect_is_loaded(&MY_CXT) && MY_CXT.map)
649 ptable_delete(MY_CXT.map, o);
652 /* --- Check functions ----------------------------------------------------- */
654 static int indirect_find(pTHX_ SV *name_sv, const char *line_bufptr, STRLEN *name_pos) {
655 #define indirect_find(NSV, LBP, NP) indirect_find(aTHX_ (NSV), (LBP), (NP))
656 STRLEN name_len, line_len;
657 const char *name, *name_end;
658 const char *line, *line_end;
661 line = SvPV_const(PL_linestr, line_len);
662 line_end = line + line_len;
664 name = SvPV_const(name_sv, name_len);
665 if (name_len >= 1 && *name == '$') {
668 while (line_bufptr < line_end && *line_bufptr != '$')
670 if (line_bufptr >= line_end)
673 name_end = name + name_len;
677 p = ninstr(p, line_end, name, name_end);
680 if (!isALNUM(p[name_len]))
682 /* p points to a word that has name as prefix, skip the rest of the word */
688 *name_pos = p - line;
693 /* ... ck_const ............................................................ */
695 static OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
697 static OP *indirect_ck_const(pTHX_ OP *o) {
698 o = indirect_old_ck_const(aTHX_ o);
700 if (indirect_hint()) {
703 if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV)) {
706 if (indirect_find(sv, PL_oldbufptr, &pos)) {
709 /* If the constant is equal to the current package name, try to look for
710 * a "__PACKAGE__" coming before what we got. We only need to check this
711 * when we already had a match because __PACKAGE__ can only appear in
712 * direct method calls ("new __PACKAGE__" is a syntax error). */
715 && len == (STRLEN) HvNAMELEN_get(PL_curstash)
716 && memcmp(SvPVX(sv), HvNAME_get(PL_curstash), len) == 0) {
718 SV *pkg = sv_newmortal();
719 sv_setpvn(pkg, "__PACKAGE__", sizeof("__PACKAGE__")-1);
721 if (indirect_find(pkg, PL_oldbufptr, &pos_pkg) && pos_pkg < pos) {
727 indirect_map_store(o, pos, sv, CopLINE(&PL_compiling));
733 indirect_map_delete(o);
737 /* ... ck_rv2sv ............................................................ */
739 static OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
741 static OP *indirect_ck_rv2sv(pTHX_ OP *o) {
742 if (indirect_hint()) {
743 OP *op = cUNOPo->op_first;
745 const char *name = NULL;
747 OPCODE type = (OPCODE) op->op_type;
752 GV *gv = cGVOPx_gv(op);
758 if ((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP) {
759 SV *nsv = cSVOPx_sv(op);
760 if (SvPOK(nsv) && (SvTYPE(nsv) >= SVt_PV))
761 name = SvPV_const(nsv, len);
767 sv = sv_2mortal(newSVpvn("$", 1));
768 sv_catpvn_nomg(sv, name, len);
769 if (!indirect_find(sv, PL_oldbufptr, &pos)) {
770 /* If it failed, retry without the current stash */
771 const char *stash = HvNAME_get(PL_curstash);
772 STRLEN stashlen = HvNAMELEN_get(PL_curstash);
774 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
775 || name[stashlen] != ':' || name[stashlen+1] != ':') {
776 /* Failed again ? Try to remove main */
779 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
780 || name[stashlen] != ':' || name[stashlen+1] != ':')
784 sv_setpvn(sv, "$", 1);
786 sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
787 if (!indirect_find(sv, PL_oldbufptr, &pos))
791 o = indirect_old_ck_rv2sv(aTHX_ o);
793 indirect_map_store(o, pos, sv, CopLINE(&PL_compiling));
798 o = indirect_old_ck_rv2sv(aTHX_ o);
800 indirect_map_delete(o);
804 /* ... ck_padany ........................................................... */
806 static OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
808 static OP *indirect_ck_padany(pTHX_ OP *o) {
809 o = indirect_old_ck_padany(aTHX_ o);
811 if (indirect_hint()) {
813 const char *s = PL_oldbufptr, *t = PL_bufptr - 1;
815 while (s < t && isSPACE(*s)) ++s;
816 if (*s == '$' && ++s <= t) {
817 while (s < t && isSPACE(*s)) ++s;
818 while (s < t && isSPACE(*t)) --t;
819 sv = sv_2mortal(newSVpvn("$", 1));
820 sv_catpvn_nomg(sv, s, t - s + 1);
821 indirect_map_store(o, s - SvPVX_const(PL_linestr),
822 sv, CopLINE(&PL_compiling));
827 indirect_map_delete(o);
831 /* ... ck_scope ............................................................ */
833 static OP *(*indirect_old_ck_scope) (pTHX_ OP *) = 0;
834 static OP *(*indirect_old_ck_lineseq)(pTHX_ OP *) = 0;
836 static OP *indirect_ck_scope(pTHX_ OP *o) {
837 OP *(*old_ck)(pTHX_ OP *) = 0;
839 switch (o->op_type) {
840 case OP_SCOPE: old_ck = indirect_old_ck_scope; break;
841 case OP_LINESEQ: old_ck = indirect_old_ck_lineseq; break;
845 if (indirect_hint()) {
846 indirect_map_store(o, PL_oldbufptr - SvPVX_const(PL_linestr),
847 NULL, CopLINE(&PL_compiling));
851 indirect_map_delete(o);
855 /* We don't need to clean the map entries for leave ops because they can only
856 * be created by mutating from a lineseq. */
858 /* ... ck_method ........................................................... */
860 static OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
862 static OP *indirect_ck_method(pTHX_ OP *o) {
863 if (indirect_hint()) {
864 OP *op = cUNOPo->op_first;
866 /* Indirect method call is only possible when the method is a bareword, so
867 * don't trip up on $obj->$meth. */
868 if (op && op->op_type == OP_CONST) {
869 const indirect_op_info_t *oi = indirect_map_fetch(op);
877 sv = sv_2mortal(newSVpvn(oi->buf, oi->len));
879 /* Keep the old line so that we really point to the first line of the
883 o = indirect_old_ck_method(aTHX_ o);
884 /* o may now be a method_named */
886 indirect_map_store(o, pos, sv, line);
892 o = indirect_old_ck_method(aTHX_ o);
894 indirect_map_delete(o);
898 /* ... ck_method_named ..................................................... */
900 /* "use foo/no foo" compiles its call to import/unimport directly to a
901 * method_named op. */
903 static OP *(*indirect_old_ck_method_named)(pTHX_ OP *) = 0;
905 static OP *indirect_ck_method_named(pTHX_ OP *o) {
906 if (indirect_hint()) {
912 if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
914 sv = sv_mortalcopy(sv);
916 if (!indirect_find(sv, PL_oldbufptr, &pos))
918 line = CopLINE(&PL_compiling);
920 o = indirect_old_ck_method_named(aTHX_ o);
922 indirect_map_store(o, pos, sv, line);
927 o = indirect_old_ck_method_named(aTHX_ o);
929 indirect_map_delete(o);
933 /* ... ck_entersub ......................................................... */
935 static OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
937 static OP *indirect_ck_entersub(pTHX_ OP *o) {
938 SV *code = indirect_hint();
940 o = indirect_old_ck_entersub(aTHX_ o);
943 const indirect_op_info_t *moi, *ooi;
949 lop = (LISTOP *) oop;
950 if (!(lop->op_flags & OPf_KIDS))
953 } while (oop->op_type != OP_PUSHMARK);
954 oop = OpSIBLING(oop);
960 switch (oop->op_type) {
971 if (mop->op_type == OP_METHOD)
972 mop = cUNOPx(mop)->op_first;
973 else if (mop->op_type != OP_METHOD_NAMED)
976 moi = indirect_map_fetch(mop);
980 ooi = indirect_map_fetch(oop);
984 /* When positions are identical, the method and the object must have the
985 * same name. But it also means that it is an indirect call, as "foo->foo"
986 * results in different positions. */
987 if ( moi->line < ooi->line
988 || (moi->line == ooi->line && moi->pos <= ooi->pos)) {
996 file = sv_2mortal(newSVpv(CopFILE(&PL_compiling), 0));
998 file = sv_mortalcopy(CopFILESV(&PL_compiling));
1003 mPUSHp(ooi->buf, ooi->len);
1004 mPUSHp(moi->buf, moi->len);
1009 call_sv(code, G_VOID);
1022 /* --- Global setup/teardown ------------------------------------------------ */
1024 static VOL U32 indirect_initialized = 0;
1026 static void indirect_global_teardown(pTHX_ void *root) {
1027 if (!indirect_initialized)
1035 indirect_ck_restore(OP_CONST, &indirect_old_ck_const);
1036 indirect_ck_restore(OP_RV2SV, &indirect_old_ck_rv2sv);
1037 indirect_ck_restore(OP_PADANY, &indirect_old_ck_padany);
1038 indirect_ck_restore(OP_SCOPE, &indirect_old_ck_scope);
1039 indirect_ck_restore(OP_LINESEQ, &indirect_old_ck_lineseq);
1041 indirect_ck_restore(OP_METHOD, &indirect_old_ck_method);
1042 indirect_ck_restore(OP_METHOD_NAMED, &indirect_old_ck_method_named);
1043 indirect_ck_restore(OP_ENTERSUB, &indirect_old_ck_entersub);
1045 indirect_initialized = 0;
1050 static void indirect_global_setup(pTHX) {
1051 #define indirect_global_setup() indirect_global_setup(aTHX)
1054 if (indirect_initialized)
1057 PERL_HASH(indirect_hash, __PACKAGE__, __PACKAGE_LEN__);
1059 stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
1060 newCONSTSUB(stash, "I_THREADSAFE", newSVuv(I_THREADSAFE));
1061 newCONSTSUB(stash, "I_FORKSAFE", newSVuv(I_FORKSAFE));
1063 indirect_ck_replace(OP_CONST, indirect_ck_const, &indirect_old_ck_const);
1064 indirect_ck_replace(OP_RV2SV, indirect_ck_rv2sv, &indirect_old_ck_rv2sv);
1065 indirect_ck_replace(OP_PADANY, indirect_ck_padany, &indirect_old_ck_padany);
1066 indirect_ck_replace(OP_SCOPE, indirect_ck_scope, &indirect_old_ck_scope);
1067 indirect_ck_replace(OP_LINESEQ, indirect_ck_scope, &indirect_old_ck_lineseq);
1069 indirect_ck_replace(OP_METHOD, indirect_ck_method,
1070 &indirect_old_ck_method);
1071 indirect_ck_replace(OP_METHOD_NAMED, indirect_ck_method_named,
1072 &indirect_old_ck_method_named);
1073 indirect_ck_replace(OP_ENTERSUB, indirect_ck_entersub,
1074 &indirect_old_ck_entersub);
1077 call_atexit(indirect_global_teardown, aTHX);
1079 call_atexit(indirect_global_teardown, NULL);
1082 indirect_initialized = 1;
1087 /* --- Interpreter setup/teardown ------------------------------------------ */
1089 static void indirect_local_teardown(pTHX_ void *param) {
1092 indirect_clear_loaded(&MY_CXT);
1094 ptable_free(MY_CXT.map);
1098 ptable_hints_free(MY_CXT.tbl);
1105 static void indirect_local_setup(pTHX) {
1106 #define indirect_local_setup() indirect_local_setup(aTHX)
1110 MY_CXT.tbl = ptable_new();
1111 MY_CXT.owner = aTHX;
1114 MY_CXT.map = ptable_new();
1115 MY_CXT.global_code = NULL;
1117 indirect_set_loaded(&MY_CXT);
1119 call_atexit(indirect_local_teardown, NULL);
1124 /* --- XS ------------------------------------------------------------------ */
1126 MODULE = indirect PACKAGE = indirect
1132 indirect_global_setup();
1133 indirect_local_setup();
1143 SV *global_code_dup;
1147 indirect_ptable_clone_ud ud;
1150 indirect_ptable_clone_ud_init(ud, t, MY_CXT.owner);
1151 ptable_walk(MY_CXT.tbl, indirect_ptable_clone, &ud);
1152 global_code_dup = indirect_dup_inc(MY_CXT.global_code, &ud);
1153 indirect_ptable_clone_ud_deinit(ud);
1157 MY_CXT.map = ptable_new();
1159 MY_CXT.owner = aTHX;
1160 MY_CXT.global_code = global_code_dup;
1161 indirect_set_loaded(&MY_CXT);
1163 gv = gv_fetchpv(__PACKAGE__ "::_THREAD_CLEANUP", 0, SVt_PVCV);
1169 if (!av_store(PL_endav, av_len(PL_endav) + 1, (SV *) cv))
1171 sv_magicext((SV *) PL_endav, NULL, PERL_MAGIC_ext, &indirect_endav_vtbl, NULL, 0);
1176 _THREAD_CLEANUP(...)
1179 indirect_thread_cleanup(aTHX_ NULL);
1188 RETVAL = indirect_tag(value);
1198 else if (SvROK(code))
1202 SvREFCNT_dec(MY_CXT.global_code);
1203 MY_CXT.global_code = SvREFCNT_inc(code);